Commit bc5cb8f725 for ffmpeg

commit bc5cb8f72516fe1344b68e4569466a524885eced
Author: Baptiste Coudurier <baptiste.coudurier@gmail.com>
Date:   Thu Nov 12 18:10:21 2026 -0800

    avformat/mxfdec: read the RGBA color range

    From Component Min/Max Ref, as written by Resolve (64/940 for video levels).

    Accept nominal and DNxHD peak levels, and count both parts of split RGB
    components. Treat legacy 16-bit FFV1 references as ambiguous unless the
    coding label identifies an integer-only version.

    Signed-off-by: Baptiste Coudurier <baptiste.coudurier@gmail.com>
    Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

diff --git a/libavformat/mxfdec.c b/libavformat/mxfdec.c
index 2416b00660..a3b5c88ed1 100644
--- a/libavformat/mxfdec.c
+++ b/libavformat/mxfdec.c
@@ -195,6 +195,8 @@ typedef struct {
 #define MXF_BLACK_REF_LEVEL_PRESENT (1 << 0)
 #define MXF_WHITE_REF_LEVEL_PRESENT (1 << 1)
 #define MXF_COLOR_RANGE_PRESENT     (1 << 2)
+#define MXF_COMPONENT_MAX_REF_PRESENT (1 << 3)
+#define MXF_COMPONENT_MIN_REF_PRESENT (1 << 4)

 typedef struct MXFDescriptor {
     MXFMetadataSet meta;
@@ -222,6 +224,9 @@ typedef struct MXFDescriptor {
     unsigned int color_range;
     unsigned int range_items;  ///< MXF_*_PRESENT flags of the range items that were read
     int signal_standard;
+    unsigned int rgb_depth;    ///< from the RGBA pixel layout
+    unsigned int component_max_ref;
+    unsigned int component_min_ref;
     unsigned int horiz_subsampling;
     unsigned int vert_subsampling;
     UID *file_descriptors_refs;
@@ -1332,6 +1337,7 @@ static int mxf_read_index_table_segment(void *arg, AVIOContext *pb, int tag, int

 static void mxf_read_pixel_layout(AVIOContext *pb, MXFDescriptor *descriptor)
 {
+    unsigned depth[3] = { 0 };
     int code, value, ofs = 0;
     char layout[16] = {0}; /* not for printing, may end up not terminated on purpose */

@@ -1343,10 +1349,16 @@ static void mxf_read_pixel_layout(AVIOContext *pb, MXFDescriptor *descriptor)
         if (ofs <= 14) {
             layout[ofs++] = code;
             layout[ofs++] = value;
+            switch (code) {
+            case 'R': case 'r': depth[0] += value; break;
+            case 'G': case 'g': depth[1] += value; break;
+            case 'B': case 'b': depth[2] += value; break;
+            }
         } else
             break;  /* don't read byte by byte on sneaky files filled with lots of non-zeroes */
     } while (code != 0); /* SMPTE 377M E.2.46 */

+    descriptor->rgb_depth = FFMAX3(depth[0], depth[1], depth[2]);
     ff_mxf_decode_pixel_layout(layout, &descriptor->pix_fmt);
 }

@@ -1458,6 +1470,18 @@ static int mxf_read_generic_descriptor(void *arg, AVIOContext *pb, int tag, int
     case 0x3401:
         mxf_read_pixel_layout(pb, descriptor);
         break;
+    case 0x3406:
+        if (size != 4)
+            break;
+        descriptor->component_max_ref = avio_rb32(pb);
+        descriptor->range_items |= MXF_COMPONENT_MAX_REF_PRESENT;
+        break;
+    case 0x3407:
+        if (size != 4)
+            break;
+        descriptor->component_min_ref = avio_rb32(pb);
+        descriptor->range_items |= MXF_COMPONENT_MIN_REF_PRESENT;
+        break;
     default:
         /* Private uid used by SONY C0023S01.mxf */
         if (IS_KLV_KEY(uid, mxf_sony_mpeg4_extradata)) {
@@ -2548,13 +2572,48 @@ static int mxf_add_metadata_stream(MXFContext *mxf, MXFTrack *track)
     return 0;
 }

-static enum AVColorRange mxf_get_color_range(MXFContext *mxf, MXFDescriptor *descriptor)
+static enum AVColorRange mxf_get_color_range(MXFContext *mxf, MXFDescriptor *descriptor,
+                                            enum AVCodecID codec_id, const MXFCodecUL *codec_ul)
 {
     unsigned depth = descriptor->component_depth;
     unsigned black = descriptor->black_ref_level;
     unsigned white = descriptor->white_ref_level;
     unsigned range = descriptor->color_range;

+    if (descriptor->rgb_depth ||
+        descriptor->range_items & (MXF_COMPONENT_MAX_REF_PRESENT | MXF_COMPONENT_MIN_REF_PRESENT)) {
+        /* RGBA range metadata, absent items default to 255 and 0 */
+        unsigned rgb_depth = descriptor->rgb_depth;
+        unsigned max_ref = descriptor->range_items & MXF_COMPONENT_MAX_REF_PRESENT ?
+                           descriptor->component_max_ref : 255;
+        unsigned min_ref = descriptor->component_min_ref;
+        if (!rgb_depth || rgb_depth >= 31)
+            return AVCOL_RANGE_UNSPECIFIED;
+        /* Legacy 16-bit FFV1 layouts are ambiguous unless the coding label
+         * identifies an integer-only version. */
+        if (rgb_depth == 16 && codec_id == AV_CODEC_ID_FFV1 &&
+            !(codec_ul->id == AV_CODEC_ID_FFV1 && codec_ul->uid[14] <= 4) &&
+            min_ref == 0 && max_ref == 65535)
+            return AVCOL_RANGE_UNSPECIFIED;
+        /* the defaults only describe 8-bit components */
+        if (!(descriptor->range_items & (MXF_COMPONENT_MAX_REF_PRESENT | MXF_COMPONENT_MIN_REF_PRESENT)) &&
+            rgb_depth != 8)
+            return AVCOL_RANGE_UNSPECIFIED;
+        if (min_ref == 0 && max_ref == (1U << rgb_depth) - 1)
+            return AVCOL_RANGE_JPEG;
+        if (rgb_depth >= 8 &&
+            min_ref == 16 << (rgb_depth - 8) && max_ref == 235 << (rgb_depth - 8))
+            return AVCOL_RANGE_MPEG;
+        /* ST 2019-4 Table 7 gives peak excursions for DNxHD. */
+        if (codec_id == AV_CODEC_ID_DNXHD &&
+            ((rgb_depth == 10 && min_ref == 4  && max_ref == 1019) ||
+             (rgb_depth == 12 && min_ref == 16 && max_ref == 4079)))
+            return AVCOL_RANGE_MPEG;
+        avpriv_request_sample(mxf->fc, "Unrecognized RGBA color range (min %u, max %u, depth %u)",
+                              min_ref, max_ref, rgb_depth);
+        return AVCOL_RANGE_UNSPECIFIED;
+    }
+
     /* CDCI range metadata */
     if (!descriptor->range_items || !depth)
         return AVCOL_RANGE_UNSPECIFIED;
@@ -3099,7 +3158,7 @@ static int mxf_parse_structural_metadata(MXFContext *mxf)
             }
             if (descriptor->aspect_ratio.num && descriptor->aspect_ratio.den)
                 sti->display_aspect_ratio = descriptor->aspect_ratio;
-            st->codecpar->color_range     = mxf_get_color_range(mxf, descriptor);
+            st->codecpar->color_range     = mxf_get_color_range(mxf, descriptor, st->codecpar->codec_id, codec_ul);
             st->codecpar->color_primaries = mxf_get_codec_ul(ff_mxf_color_primaries_uls, &descriptor->color_primaries_ul)->id;
             st->codecpar->color_trc       = mxf_get_codec_ul(ff_mxf_color_trc_uls, &descriptor->color_trc_ul)->id;
             st->codecpar->color_space     = mxf_get_codec_ul(ff_mxf_color_space_uls, &descriptor->color_space_ul)->id;
diff --git a/tests/ref/fate/mxf-probe-j2k b/tests/ref/fate/mxf-probe-j2k
index b30a18edd1..e2fe9c11aa 100644
--- a/tests/ref/fate/mxf-probe-j2k
+++ b/tests/ref/fate/mxf-probe-j2k
@@ -14,7 +14,7 @@ has_b_frames=0
 sample_aspect_ratio=1:1
 display_aspect_ratio=16:9
 pix_fmt=rgb48
-color_range=unknown
+color_range=pc
 color_space=unknown
 color_transfer=bt709
 color_primaries=bt709