PR #26011 opened by Baptiste Coudurier (bcoudurier)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26011
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26011.patch

MXF colour handling, checked against SMPTE ST 377-1:2019, the SMPTE Labels 
Register (ST 400, published register v86) and the specs it refers to.

- mxf: map the registered labels FFmpeg has values for but was missing: sRGB 
transfer, ST 2115 Camera Log V / Camera Gamut V (identical to V-Log / V-Gamut), 
BT.2100 ICtCp coding equations, and on read the ST 2067-40 Cinema Mezzanine 
transfers (linear, and DCDM which the spec states is equivalent to ST 428-1).
- mxfenc: warn once per stream when a primaries/transfer/matrix value has no 
label; the property is still omitted.
- mxfdec: when only some of Black Ref Level / White Ref Level / Color Range are 
present, use the defaults of ST 377-1 G.2.32-G.2.34 (from the Signal Standard 
if set) instead of treating absent items as 0. With none present the range 
stays unspecified.
- mxf: read the ITU-R BT.470 PAL transfer label as gamma 2.8 (bt470bg) instead 
of 2.2; gamma 2.2 still writes it. Sony-00001 and Avid-00005 now probe as 
bt470bg.
- avc: parse the VUI video signal type (range flag and colour description).
- mxfenc: take colour from the essence bitstream where it carries it (MPEG-2 
sequence_display_extension, H.264 VUI, ProRes frame header limited to the codes 
defined in RDD 36 Tables 5 and 6); a bitstream value overrides the stream 
parameters with a warning.

Tested with MPEG-2, H.264 and ProRes in MXF/MOV/MP4 remux and transcode, 
dumping the descriptor labels, and with Apple AVFoundation's MXF importer. FATE 
passes with samples; mxf-probe-d10, mxf-probe-dv25, mxf-d10-user-comments and 
matroska-encoding-delay change only because of the PAL transfer value (4 -> 5).

🤖 Generated with [Claude Code](https://claude.com/claude-code)



From 8f990f145e4ca924ee8b4c49cef0cec20126db09 Mon Sep 17 00:00:00 2001
From: Baptiste Coudurier <[email protected]>
Date: Thu, 12 Nov 2026 16:19:14 -0800
Subject: [PATCH 1/6] avformat/mxf: map more colour labels from the SMPTE
 register

Add sRGB, ST 2115 Camera Log V/Gamut V, ICtCp and the Cinema Mezzanine 
transfers.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Baptiste Coudurier <[email protected]>
---
 libavformat/mxf.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/libavformat/mxf.c b/libavformat/mxf.c
index b6c1f176a17..8d93965476a 100644
--- a/libavformat/mxf.c
+++ b/libavformat/mxf.c
@@ -104,6 +104,7 @@ const MXFCodecUL ff_mxf_color_primaries_uls[] = {
     { { 
0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0D,0x04,0x01,0x01,0x01,0x03,0x06,0x00,0x00 
}, 14, AVCOL_PRI_SMPTE432 }, /* P3D65 */
     { { 
0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0D,0x04,0x01,0x01,0x01,0x03,0x08,0x00,0x00 
}, 14, AVCOL_PRI_SMPTE428 }, /* Cinema Mezzanine */
     { { 
0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0D,0x04,0x01,0x01,0x01,0x03,0x0a,0x00,0x00 
}, 14, AVCOL_PRI_SMPTE431 }, /* P3DCI */
+    { { 
0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0D,0x04,0x01,0x01,0x01,0x03,0x0d,0x00,0x00 
}, 14, AVCOL_PRI_V_GAMUT }, /* SMPTE ST 2115 Camera Gamut V */
     /* alternate mappings for encoding */
     { { 
0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x06,0x04,0x01,0x01,0x01,0x03,0x01,0x00,0x00 
}, 14, AVCOL_PRI_SMPTE240M }, /* = AVCOL_PRI_SMPTE170M */
 
@@ -122,6 +123,10 @@ const MXFCodecUL ff_mxf_color_trc_uls[] = {
     { { 
0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0E,0x04,0x01,0x01,0x01,0x01,0x09,0x00,0x00 
}, 14, AVCOL_TRC_BT2020_10 }, /* ITU-R BT.2020 */
     { { 
0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0D,0x04,0x01,0x01,0x01,0x01,0x0A,0x00,0x00 
}, 14, AVCOL_TRC_SMPTE2084 }, /* SMPTE ST 2084 */
     { { 
0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0D,0x04,0x01,0x01,0x01,0x01,0x0B,0x00,0x00 
}, 14, AVCOL_TRC_ARIB_STD_B67 }, /* Hybrid Log-Gamma OETF */
+    { { 
0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0D,0x04,0x01,0x01,0x01,0x01,0x0D,0x00,0x00 
}, 14, AVCOL_TRC_IEC61966_2_1 }, /* sRGB */
+    { { 
0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0D,0x04,0x01,0x01,0x01,0x01,0x0F,0x00,0x00 
}, 14, AVCOL_TRC_V_LOG }, /* SMPTE ST 2115 Camera Log V */
+    { { 
0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0D,0x04,0x01,0x01,0x01,0x01,0x12,0x00,0x00 
}, 14, AVCOL_TRC_LINEAR }, /* Cinema Mezzanine Linear (must appear after 
Linear) */
+    { { 
0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0D,0x04,0x01,0x01,0x01,0x01,0x13,0x00,0x00 
}, 14, AVCOL_TRC_SMPTE428 }, /* Cinema Mezzanine DCDM (must appear after 
SMPTE-DC28 DCDM) */
     /* alternate mappings for encoding */
     { { 
0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x01,0x01,0x01,0x01,0x00,0x00 
}, 14, AVCOL_TRC_GAMMA28 }, /* = AVCOL_TRC_GAMMA22 */
     { { 
0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x01,0x01,0x01,0x02,0x00,0x00 
}, 14, AVCOL_TRC_SMPTE170M }, /* = AVCOL_TRC_BT709 */
@@ -138,6 +143,7 @@ const MXFCodecUL ff_mxf_color_space_uls[] = {
     { { 
0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0D,0x04,0x01,0x01,0x01,0x02,0x04,0x00,0x00 
}, 14, AVCOL_SPC_YCGCO }, /* YCgCo */
     { { 
0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0D,0x04,0x01,0x01,0x01,0x02,0x05,0x00,0x00 
}, 14, AVCOL_SPC_RGB }, /* GBR */
     { { 
0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0D,0x04,0x01,0x01,0x01,0x02,0x06,0x00,0x00 
}, 14, AVCOL_SPC_BT2020_NCL }, /* ITU-R BT.2020 Non-Constant Luminance */
+    { { 
0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0D,0x04,0x01,0x01,0x01,0x02,0x07,0x00,0x00 
}, 14, AVCOL_SPC_ICTCP }, /* ITU-R BT.2100 ICtCp */
     /* alternate mappings for encoding */
     { { 
0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x01,0x01,0x02,0x01,0x00,0x00 
}, 14, AVCOL_SPC_SMPTE170M }, /* = AVCOL_SPC_BT470BG */
 
-- 
2.54.0


From 1ede33b88206e47ccac7e3327d74acdce7c105e6 Mon Sep 17 00:00:00 2001
From: Baptiste Coudurier <[email protected]>
Date: Thu, 12 Nov 2026 16:19:14 -0800
Subject: [PATCH 2/6] avformat/mxfenc: warn about colour values that have no
 label

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Baptiste Coudurier <[email protected]>
---
 libavformat/mxfenc.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

diff --git a/libavformat/mxfenc.c b/libavformat/mxfenc.c
index 867a4abd4d2..ea5b0cea2d5 100644
--- a/libavformat/mxfenc.c
+++ b/libavformat/mxfenc.c
@@ -124,6 +124,7 @@ typedef struct MXFStreamContext {
     enum MXFMetadataSetType sub_descriptor;
     const UID *picture_descriptor_key;
     int cid;                 ///< compression id, used by dnxhd
+    int color_warned;        ///< unsupported color properties have been 
reported
 } MXFStreamContext;
 
 typedef struct MXFContainerEssenceEntry {
@@ -1225,6 +1226,11 @@ static inline uint32_t rescale_mastering_luma(AVRational 
q)
     return av_rescale(q.num, FF_MXF_MASTERING_LUMA_DEN, q.den);
 }
 
+static const char *color_name(const char *name)
+{
+    return name ? name : "reserved";
+}
+
 static int64_t mxf_write_generic_picture_desc(AVFormatContext *s, AVStream *st)
 {
     MXFStreamContext *sc = st->priv_data;
@@ -1246,6 +1252,19 @@ static int64_t 
mxf_write_generic_picture_desc(AVFormatContext *s, AVStream *st)
     color_trc_ul       = mxf_get_codec_ul_by_id(ff_mxf_color_trc_uls, 
st->codecpar->color_trc);
     color_space_ul     = mxf_get_codec_ul_by_id(ff_mxf_color_space_uls, 
st->codecpar->color_space);
 
+    if (!sc->color_warned) {
+        if (!color_primaries_ul->uid[0] && st->codecpar->color_primaries != 
AVCOL_PRI_UNSPECIFIED)
+            av_log(s, AV_LOG_WARNING, "Color primaries %s have no MXF label, 
not writing them\n",
+                   
color_name(av_color_primaries_name(st->codecpar->color_primaries)));
+        if (!color_trc_ul->uid[0] && st->codecpar->color_trc != 
AVCOL_TRC_UNSPECIFIED)
+            av_log(s, AV_LOG_WARNING, "Color transfer %s has no MXF label, not 
writing it\n",
+                   
color_name(av_color_transfer_name(st->codecpar->color_trc)));
+        if (!color_space_ul->uid[0] && st->codecpar->color_space != 
AVCOL_SPC_UNSPECIFIED)
+            av_log(s, AV_LOG_WARNING, "Color matrix %s has no MXF label, not 
writing it\n",
+                   color_name(av_color_space_name(st->codecpar->color_space)));
+        sc->color_warned = 1;
+    }
+
     if (st->codecpar->codec_id == AV_CODEC_ID_DVVIDEO) {
         if (st->codecpar->height == 1080)
             stored_width = 1920;
-- 
2.54.0


From ef4352ce5c44185d50dce908f5077b042e2beb78 Mon Sep 17 00:00:00 2001
From: Baptiste Coudurier <[email protected]>
Date: Thu, 12 Nov 2026 16:30:49 -0800
Subject: [PATCH 3/6] avformat/mxfdec: use the ST 377-1 defaults for missing
 range items

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Baptiste Coudurier <[email protected]>
---
 libavformat/mxfdec.c | 65 ++++++++++++++++++++++++++++++++++----------
 1 file changed, 50 insertions(+), 15 deletions(-)

diff --git a/libavformat/mxfdec.c b/libavformat/mxfdec.c
index 280a20aad33..a47a01812f1 100644
--- a/libavformat/mxfdec.c
+++ b/libavformat/mxfdec.c
@@ -192,6 +192,10 @@ typedef struct {
     int64_t origin;
 } MXFTrack;
 
+#define MXF_BLACK_REF_LEVEL_PRESENT (1 << 0)
+#define MXF_WHITE_REF_LEVEL_PRESENT (1 << 1)
+#define MXF_COLOR_RANGE_PRESENT     (1 << 2)
+
 typedef struct MXFDescriptor {
     MXFMetadataSet meta;
     UID essence_container_ul;
@@ -214,6 +218,8 @@ typedef struct MXFDescriptor {
     unsigned int black_ref_level;
     unsigned int white_ref_level;
     unsigned int color_range;
+    unsigned int range_items;  ///< MXF_*_PRESENT flags of the range items 
that were read
+    int signal_standard;
     unsigned int horiz_subsampling;
     unsigned int vert_subsampling;
     UID *file_descriptors_refs;
@@ -1410,12 +1416,18 @@ static int mxf_read_generic_descriptor(void *arg, 
AVIOContext *pb, int tag, int
         break;
     case 0x3304:
         descriptor->black_ref_level = avio_rb32(pb);
+        descriptor->range_items |= MXF_BLACK_REF_LEVEL_PRESENT;
         break;
     case 0x3305:
         descriptor->white_ref_level = avio_rb32(pb);
+        descriptor->range_items |= MXF_WHITE_REF_LEVEL_PRESENT;
         break;
     case 0x3306:
         descriptor->color_range = avio_rb32(pb);
+        descriptor->range_items |= MXF_COLOR_RANGE_PRESENT;
+        break;
+    case 0x3215:
+        descriptor->signal_standard = avio_r8(pb);
         break;
     case 0x3308:
         descriptor->vert_subsampling = avio_rb32(pb);
@@ -2528,24 +2540,47 @@ static int mxf_add_metadata_stream(MXFContext *mxf, 
MXFTrack *track)
 
 static enum AVColorRange mxf_get_color_range(MXFContext *mxf, MXFDescriptor 
*descriptor)
 {
-    if (descriptor->black_ref_level || descriptor->white_ref_level || 
descriptor->color_range) {
-        /* CDCI range metadata */
-        if (!descriptor->component_depth)
-            return AVCOL_RANGE_UNSPECIFIED;
-        if (descriptor->black_ref_level == 0 && descriptor->component_depth < 
31 &&
-            descriptor->white_ref_level == ((1<<descriptor->component_depth) - 
1) &&
-            (descriptor->color_range    == (1<<descriptor->component_depth) ||
-             descriptor->color_range    == ((1<<descriptor->component_depth) - 
1)))
+    unsigned depth = descriptor->component_depth;
+    unsigned black = descriptor->black_ref_level;
+    unsigned white = descriptor->white_ref_level;
+    unsigned range = descriptor->color_range;
+
+    /* CDCI range metadata */
+    if (!descriptor->range_items || !depth)
+        return AVCOL_RANGE_UNSPECIFIED;
+
+    if (depth < 31) {
+        unsigned def_black, def_white, def_range;
+
+        /* defaults for absent items, SMPTE ST 377-1 G.2.32 to G.2.34 */
+        if (descriptor->signal_standard >= 1 && descriptor->signal_standard <= 
6 && depth >= 8) {
+            /* the interface standards of G.2.3 all use these levels */
+            def_black = 16  << (depth - 8);
+            def_white = 235 << (depth - 8);
+            def_range = (224 << (depth - 8)) + 1;
+        } else {
+            def_black = depth == 8 ? 16  : depth == 10 ? 64  : 0;
+            def_white = depth == 8 ? 235 : depth == 10 ? 940 : (1U << depth) - 
1;
+            def_range = (1U << depth) - 1;
+        }
+        if (!(descriptor->range_items & MXF_BLACK_REF_LEVEL_PRESENT))
+            black = def_black;
+        if (!(descriptor->range_items & MXF_WHITE_REF_LEVEL_PRESENT))
+            white = def_white;
+        if (!(descriptor->range_items & MXF_COLOR_RANGE_PRESENT))
+            range = def_range;
+
+        if (black == 0 && white == (1U << depth) - 1 &&
+            (range == 1U << depth || range == (1U << depth) - 1))
             return AVCOL_RANGE_JPEG;
-        if (descriptor->component_depth >= 8 && descriptor->component_depth < 
31 &&
-            descriptor->black_ref_level == (1  <<(descriptor->component_depth 
- 4)) &&
-            descriptor->white_ref_level == (235<<(descriptor->component_depth 
- 8)) &&
-            descriptor->color_range     == ((14<<(descriptor->component_depth 
- 4)) + 1))
+        if (depth >= 8 &&
+            black == 16  << (depth - 8) &&
+            white == 235 << (depth - 8) &&
+            range == (224 << (depth - 8)) + 1)
             return AVCOL_RANGE_MPEG;
-        avpriv_request_sample(mxf->fc, "Unrecognized CDCI color range (color 
diff range %d, b %d, w %d, depth %d)",
-                              descriptor->color_range, 
descriptor->black_ref_level,
-                              descriptor->white_ref_level, 
descriptor->component_depth);
     }
+    avpriv_request_sample(mxf->fc, "Unrecognized CDCI color range (color diff 
range %u, b %u, w %u, depth %u)",
+                          range, black, white, depth);
 
     return AVCOL_RANGE_UNSPECIFIED;
 }
-- 
2.54.0


From c4f772bde00772a92047f1809caa62158cbede3e Mon Sep 17 00:00:00 2001
From: Baptiste Coudurier <[email protected]>
Date: Thu, 12 Nov 2026 16:30:49 -0800
Subject: [PATCH 4/6] avformat/mxf: read the ITU-R BT.470 PAL transfer label as
 gamma 2.8

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Baptiste Coudurier <[email protected]>
---
 libavformat/mxf.c                      | 4 ++--
 tests/ref/fate/matroska-encoding-delay | 2 +-
 tests/ref/fate/mxf-d10-user-comments   | 6 +++---
 tests/ref/fate/mxf-probe-d10           | 2 +-
 tests/ref/fate/mxf-probe-dv25          | 2 +-
 5 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/libavformat/mxf.c b/libavformat/mxf.c
index 8d93965476a..d74d226301d 100644
--- a/libavformat/mxf.c
+++ b/libavformat/mxf.c
@@ -112,7 +112,7 @@ const MXFCodecUL ff_mxf_color_primaries_uls[] = {
 };
 
 const MXFCodecUL ff_mxf_color_trc_uls[] = {
-    { { 
0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x01,0x01,0x01,0x01,0x00,0x00 
}, 14, AVCOL_TRC_GAMMA22 }, /* ITU-R BT.470 */
+    { { 
0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x01,0x01,0x01,0x01,0x00,0x00 
}, 14, AVCOL_TRC_GAMMA28 }, /* ITU-R BT.470 PAL */
     { { 
0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x01,0x01,0x01,0x02,0x00,0x00 
}, 14, AVCOL_TRC_BT709 }, /* ITU-R BT.709 */
     { { 
0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x01,0x01,0x01,0x03,0x00,0x00 
}, 14, AVCOL_TRC_SMPTE240M }, /* SMPTE 240M */
     { { 
0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x01,0x01,0x01,0x04,0x00,0x00 
}, 14, AVCOL_TRC_BT709 }, /* SMPTE 274/296M (must appear after ITU-R BT.709) */
@@ -128,7 +128,7 @@ const MXFCodecUL ff_mxf_color_trc_uls[] = {
     { { 
0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0D,0x04,0x01,0x01,0x01,0x01,0x12,0x00,0x00 
}, 14, AVCOL_TRC_LINEAR }, /* Cinema Mezzanine Linear (must appear after 
Linear) */
     { { 
0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0D,0x04,0x01,0x01,0x01,0x01,0x13,0x00,0x00 
}, 14, AVCOL_TRC_SMPTE428 }, /* Cinema Mezzanine DCDM (must appear after 
SMPTE-DC28 DCDM) */
     /* alternate mappings for encoding */
-    { { 
0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x01,0x01,0x01,0x01,0x00,0x00 
}, 14, AVCOL_TRC_GAMMA28 }, /* = AVCOL_TRC_GAMMA22 */
+    { { 
0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x01,0x01,0x01,0x01,0x00,0x00 
}, 14, AVCOL_TRC_GAMMA22 }, /* = AVCOL_TRC_GAMMA28, also covers System M */
     { { 
0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x01,0x01,0x01,0x02,0x00,0x00 
}, 14, AVCOL_TRC_SMPTE170M }, /* = AVCOL_TRC_BT709 */
     { { 
0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0E,0x04,0x01,0x01,0x01,0x01,0x09,0x00,0x00 
}, 14, AVCOL_TRC_BT2020_12 }, /* = AVCOL_TRC_BT2020_10 */
 
diff --git a/tests/ref/fate/matroska-encoding-delay 
b/tests/ref/fate/matroska-encoding-delay
index 162eaf2edfc..833ce827e20 100644
--- a/tests/ref/fate/matroska-encoding-delay
+++ b/tests/ref/fate/matroska-encoding-delay
@@ -1,4 +1,4 @@
-e0a9bc2eff3a462c6664488d9126b1f0 
*tests/data/fate/matroska-encoding-delay.matroska
+ef9e4953d3814dcd548c37ecb743ff65 
*tests/data/fate/matroska-encoding-delay.matroska
 961246 tests/data/fate/matroska-encoding-delay.matroska
 #extradata 0:       22, 0x32ea0490
 #tb 0: 1/1000
diff --git a/tests/ref/fate/mxf-d10-user-comments 
b/tests/ref/fate/mxf-d10-user-comments
index 9f3092f4af0..614c7c19a3b 100644
--- a/tests/ref/fate/mxf-d10-user-comments
+++ b/tests/ref/fate/mxf-d10-user-comments
@@ -1,12 +1,12 @@
-d0dae277c0cb07f517ae2b456658f123 *tests/data/fate/mxf-d10-user-comments.mxf_d10
+ca2d9a476892e92928d48ada9e72b152 *tests/data/fate/mxf-d10-user-comments.mxf_d10
 3782189 tests/data/fate/mxf-d10-user-comments.mxf_d10
-#extradata 0:       34, 0x716b05c4
+#extradata 0:       34, 0x717105c5
 #tb 0: 1/25
 #media_type 0: video
 #codec_id 0: mpeg2video
 #dimensions 0: 1280x720
 #sar 0: 3/4
-0,         -1,          0,        1,   150000, 0xa811adc1
+0,         -1,          0,        1,   150000, 0xf203adc2
 0,          0,          1,        1,   150000, 0x4952bac6, F=0x0
 0,          1,          2,        1,   150000, 0x6b437346, F=0x0
 0,          2,          3,        1,   150000, 0x36dbac43, F=0x0
diff --git a/tests/ref/fate/mxf-probe-d10 b/tests/ref/fate/mxf-probe-d10
index e5d701e1de4..a6467baac87 100644
--- a/tests/ref/fate/mxf-probe-d10
+++ b/tests/ref/fate/mxf-probe-d10
@@ -16,7 +16,7 @@ display_aspect_ratio=4:3
 pix_fmt=yuv422p
 color_range=tv
 color_space=unknown
-color_transfer=bt470m
+color_transfer=bt470bg
 color_primaries=unknown
 chroma_location=topleft
 field_order=tt
diff --git a/tests/ref/fate/mxf-probe-dv25 b/tests/ref/fate/mxf-probe-dv25
index 052c3e29eb9..50cf6c7cee9 100644
--- a/tests/ref/fate/mxf-probe-dv25
+++ b/tests/ref/fate/mxf-probe-dv25
@@ -16,7 +16,7 @@ display_aspect_ratio=4:3
 pix_fmt=yuv420p
 color_range=tv
 color_space=unknown
-color_transfer=bt470m
+color_transfer=bt470bg
 color_primaries=unknown
 chroma_location=topleft
 field_order=bb
-- 
2.54.0


From 2c7f38c948d9bbcdcb1cd808c00be9b821b9a8fd Mon Sep 17 00:00:00 2001
From: Baptiste Coudurier <[email protected]>
Date: Thu, 12 Nov 2026 16:59:47 -0800
Subject: [PATCH 5/6] avformat/avc: parse the VUI video signal type

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Baptiste Coudurier <[email protected]>
---
 libavformat/avc.c | 13 +++++++++++++
 libavformat/avc.h |  6 ++++++
 2 files changed, 19 insertions(+)

diff --git a/libavformat/avc.c b/libavformat/avc.c
index 84a3c3d0f43..00df03fcc2c 100644
--- a/libavformat/avc.c
+++ b/libavformat/avc.c
@@ -299,6 +299,19 @@ int ff_avc_decode_sps(H264SPS *sps, const uint8_t *buf, 
int buf_size)
                 sps->sar = avc_sample_aspect_ratio[aspect_ratio_idc];
             }
         }
+        if (get_bits1(&gb)) // overscan_info_present_flag
+            skip_bits1(&gb); // overscan_appropriate_flag
+        sps->video_signal_type_present_flag = get_bits1(&gb);
+        if (sps->video_signal_type_present_flag) {
+            skip_bits(&gb, 3); // video_format
+            sps->video_full_range_flag = get_bits1(&gb);
+            sps->colour_description_present_flag = get_bits1(&gb);
+            if (sps->colour_description_present_flag) {
+                sps->colour_primaries         = get_bits(&gb, 8);
+                sps->transfer_characteristics = get_bits(&gb, 8);
+                sps->matrix_coefficients      = get_bits(&gb, 8);
+            }
+        }
     }
 
     if (!sps->sar.den) {
diff --git a/libavformat/avc.h b/libavformat/avc.h
index e36108afce6..56a64f47942 100644
--- a/libavformat/avc.h
+++ b/libavformat/avc.h
@@ -39,6 +39,12 @@ typedef struct {
     uint8_t bit_depth_chroma;
     uint8_t frame_mbs_only_flag;
     AVRational sar;
+    uint8_t video_signal_type_present_flag;
+    uint8_t video_full_range_flag;
+    uint8_t colour_description_present_flag;
+    uint8_t colour_primaries;
+    uint8_t transfer_characteristics;
+    uint8_t matrix_coefficients;
 } H264SPS;
 
 int ff_avc_decode_sps(H264SPS *sps, const uint8_t *buf, int buf_size);
-- 
2.54.0


From 5f4f616d9a67a6004d1498f752bb23ed732d2c2e Mon Sep 17 00:00:00 2001
From: Baptiste Coudurier <[email protected]>
Date: Thu, 12 Nov 2026 16:59:47 -0800
Subject: [PATCH 6/6] avformat/mxfenc: take colour from the essence bitstream

Use the MPEG-2, H.264 and ProRes colour fields over the stream parameters.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Baptiste Coudurier <[email protected]>
---
 libavformat/mxfenc.c | 116 +++++++++++++++++++++++++++++++++++++++----
 1 file changed, 105 insertions(+), 11 deletions(-)

diff --git a/libavformat/mxfenc.c b/libavformat/mxfenc.c
index ea5b0cea2d5..ca1c7a025e7 100644
--- a/libavformat/mxfenc.c
+++ b/libavformat/mxfenc.c
@@ -125,6 +125,11 @@ typedef struct MXFStreamContext {
     const UID *picture_descriptor_key;
     int cid;                 ///< compression id, used by dnxhd
     int color_warned;        ///< unsupported color properties have been 
reported
+    /* color properties found in the essence bitstream, unspecified if absent 
*/
+    enum AVColorPrimaries color_primaries;
+    enum AVColorTransferCharacteristic color_trc;
+    enum AVColorSpace color_space;
+    enum AVColorRange color_range;
 } MXFStreamContext;
 
 typedef struct MXFContainerEssenceEntry {
@@ -1248,20 +1253,56 @@ static int64_t 
mxf_write_generic_picture_desc(AVFormatContext *s, AVStream *st)
     const AVPacketSideData *side_data;
     int component_depth = pix_desc->comp[0].depth;
 
-    color_primaries_ul = mxf_get_codec_ul_by_id(ff_mxf_color_primaries_uls, 
st->codecpar->color_primaries);
-    color_trc_ul       = mxf_get_codec_ul_by_id(ff_mxf_color_trc_uls, 
st->codecpar->color_trc);
-    color_space_ul     = mxf_get_codec_ul_by_id(ff_mxf_color_space_uls, 
st->codecpar->color_space);
+    /* the essence bitstream takes precedence where it describes the color */
+    if (!sc->color_warned) {
+        if (sc->color_primaries != AVCOL_PRI_UNSPECIFIED &&
+            st->codecpar->color_primaries != AVCOL_PRI_UNSPECIFIED &&
+            st->codecpar->color_primaries != sc->color_primaries)
+            av_log(s, AV_LOG_WARNING, "Color primaries %s in the bitstream 
override %s\n",
+                   color_name(av_color_primaries_name(sc->color_primaries)),
+                   
color_name(av_color_primaries_name(st->codecpar->color_primaries)));
+        if (sc->color_trc != AVCOL_TRC_UNSPECIFIED &&
+            st->codecpar->color_trc != AVCOL_TRC_UNSPECIFIED &&
+            st->codecpar->color_trc != sc->color_trc)
+            av_log(s, AV_LOG_WARNING, "Color transfer %s in the bitstream 
overrides %s\n",
+                   color_name(av_color_transfer_name(sc->color_trc)),
+                   
color_name(av_color_transfer_name(st->codecpar->color_trc)));
+        if (sc->color_space != AVCOL_SPC_UNSPECIFIED &&
+            st->codecpar->color_space != AVCOL_SPC_UNSPECIFIED &&
+            st->codecpar->color_space != sc->color_space)
+            av_log(s, AV_LOG_WARNING, "Color matrix %s in the bitstream 
overrides %s\n",
+                   color_name(av_color_space_name(sc->color_space)),
+                   color_name(av_color_space_name(st->codecpar->color_space)));
+        if (sc->color_range != AVCOL_RANGE_UNSPECIFIED &&
+            st->codecpar->color_range != AVCOL_RANGE_UNSPECIFIED &&
+            st->codecpar->color_range != sc->color_range)
+            av_log(s, AV_LOG_WARNING, "Color range %s in the bitstream 
overrides %s\n",
+                   color_name(av_color_range_name(sc->color_range)),
+                   color_name(av_color_range_name(st->codecpar->color_range)));
+    }
+    if (sc->color_primaries == AVCOL_PRI_UNSPECIFIED)
+        sc->color_primaries = st->codecpar->color_primaries;
+    if (sc->color_trc == AVCOL_TRC_UNSPECIFIED)
+        sc->color_trc = st->codecpar->color_trc;
+    if (sc->color_space == AVCOL_SPC_UNSPECIFIED)
+        sc->color_space = st->codecpar->color_space;
+    if (sc->color_range == AVCOL_RANGE_UNSPECIFIED)
+        sc->color_range = st->codecpar->color_range;
+
+    color_primaries_ul = mxf_get_codec_ul_by_id(ff_mxf_color_primaries_uls, 
sc->color_primaries);
+    color_trc_ul       = mxf_get_codec_ul_by_id(ff_mxf_color_trc_uls, 
sc->color_trc);
+    color_space_ul     = mxf_get_codec_ul_by_id(ff_mxf_color_space_uls, 
sc->color_space);
 
     if (!sc->color_warned) {
-        if (!color_primaries_ul->uid[0] && st->codecpar->color_primaries != 
AVCOL_PRI_UNSPECIFIED)
+        if (!color_primaries_ul->uid[0] && sc->color_primaries != 
AVCOL_PRI_UNSPECIFIED)
             av_log(s, AV_LOG_WARNING, "Color primaries %s have no MXF label, 
not writing them\n",
-                   
color_name(av_color_primaries_name(st->codecpar->color_primaries)));
-        if (!color_trc_ul->uid[0] && st->codecpar->color_trc != 
AVCOL_TRC_UNSPECIFIED)
+                   color_name(av_color_primaries_name(sc->color_primaries)));
+        if (!color_trc_ul->uid[0] && sc->color_trc != AVCOL_TRC_UNSPECIFIED)
             av_log(s, AV_LOG_WARNING, "Color transfer %s has no MXF label, not 
writing it\n",
-                   
color_name(av_color_transfer_name(st->codecpar->color_trc)));
-        if (!color_space_ul->uid[0] && st->codecpar->color_space != 
AVCOL_SPC_UNSPECIFIED)
+                   color_name(av_color_transfer_name(sc->color_trc)));
+        if (!color_space_ul->uid[0] && sc->color_space != 
AVCOL_SPC_UNSPECIFIED)
             av_log(s, AV_LOG_WARNING, "Color matrix %s has no MXF label, not 
writing it\n",
-                   color_name(av_color_space_name(st->codecpar->color_space)));
+                   color_name(av_color_space_name(sc->color_space)));
         sc->color_warned = 1;
     }
 
@@ -1410,11 +1451,11 @@ static int64_t 
mxf_write_generic_picture_desc(AVFormatContext *s, AVStream *st)
         mxf_write_local_tag(s, 2, 0x3307);
         avio_wb16(pb, 0);
 
-        if (st->codecpar->color_range != AVCOL_RANGE_UNSPECIFIED) {
+        if (sc->color_range != AVCOL_RANGE_UNSPECIFIED) {
             int black = 0,
                 white = (1<<component_depth) - 1,
                 color = (1<<component_depth);
-            if (st->codecpar->color_range == AVCOL_RANGE_MPEG) {
+            if (sc->color_range == AVCOL_RANGE_MPEG) {
                 black = 1   << (component_depth - 4);
                 white = 235 << (component_depth - 8);
                 color = (14 << (component_depth - 4)) + 1;
@@ -2327,6 +2368,18 @@ static const struct {
     { AV_PROFILE_PRORES_XQ,       { 
0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0d,0x04,0x01,0x02,0x02,0x03,0x06,0x06,0x00 
} },
 };
 
+/* Store the H.273 color code points of an MPEG-2 or H.264 bitstream,
+ * skipping reserved and unassigned values like the decoders do. */
+static void mxf_set_bitstream_color(MXFStreamContext *sc, int pri, int trc, 
int spc)
+{
+    if (pri != AVCOL_PRI_RESERVED0 && pri != AVCOL_PRI_RESERVED && 
av_color_primaries_name(pri))
+        sc->color_primaries = pri;
+    if (trc != AVCOL_TRC_RESERVED0 && trc != AVCOL_TRC_RESERVED && 
av_color_transfer_name(trc))
+        sc->color_trc = trc;
+    if (spc != AVCOL_SPC_RESERVED && av_color_space_name(spc))
+        sc->color_space = spc;
+}
+
 static int mxf_parse_prores_frame(AVFormatContext *s, AVStream *st, AVPacket 
*pkt)
 {
     MXFContext *mxf = s->priv_data;
@@ -2346,6 +2399,32 @@ static int mxf_parse_prores_frame(AVFormatContext *s, 
AVStream *st, AVPacket *pk
     if (i == FF_ARRAY_ELEMS(mxf_prores_codec_uls))
         return 0;
 
+    /* frame header color fields, SMPTE RDD 36 Tables 5 and 6;
+     * the defined codes match H.273, all others are reserved */
+    if (pkt->size >= 25 && AV_RL32(pkt->data + 4) == AV_RL32("icpf")) {
+        switch (pkt->data[22]) {
+        case AVCOL_PRI_BT709:
+        case AVCOL_PRI_BT470BG:
+        case AVCOL_PRI_SMPTE170M:
+        case AVCOL_PRI_BT2020:
+        case AVCOL_PRI_SMPTE431:
+        case AVCOL_PRI_SMPTE432:
+            sc->color_primaries = pkt->data[22];
+        }
+        switch (pkt->data[23]) {
+        case AVCOL_TRC_BT709:
+        case AVCOL_TRC_SMPTE2084:
+        case AVCOL_TRC_ARIB_STD_B67:
+            sc->color_trc = pkt->data[23];
+        }
+        switch (pkt->data[24]) {
+        case AVCOL_SPC_BT709:
+        case AVCOL_SPC_SMPTE170M:
+        case AVCOL_SPC_BT2020_NCL:
+            sc->color_space = pkt->data[24];
+        }
+    }
+
     sc->frame_size = pkt->size;
 
     return 1;
@@ -2578,6 +2657,14 @@ static int mxf_parse_h264_frame(AVFormatContext *s, 
AVStream *st,
             intra_only = (sps->constraint_set_flags >> 3) & 1;
             sc->interlaced = !sps->frame_mbs_only_flag;
 
+            if (sps->video_signal_type_present_flag) {
+                sc->color_range = sps->video_full_range_flag ? 
AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
+                if (sps->colour_description_present_flag)
+                    mxf_set_bitstream_color(sc, sps->colour_primaries,
+                                            sps->transfer_characteristics,
+                                            sps->matrix_coefficients);
+            }
+
             buf = nal_end;
             break;
         case H264_NAL_PPS:
@@ -2825,6 +2912,9 @@ static int mxf_parse_mpeg2_frame(AVFormatContext *s, 
AVStream *st,
                 st->codecpar->profile = pkt->data[i+1] & 0x07;
                 st->codecpar->level   = pkt->data[i+2] >> 4;
                 sc->low_delay = pkt->data[i+6] >> 7;
+            } else if (i + 4 < pkt->size && (pkt->data[i+1] & 0xf0) == 0x20) { 
// seq display ext
+                if (pkt->data[i+1] & 0x01) // colour_description
+                    mxf_set_bitstream_color(sc, pkt->data[i+2], 
pkt->data[i+3], pkt->data[i+4]);
             } else if (i + 5 < pkt->size && (pkt->data[i+1] & 0xf0) == 0x80) { 
// pict coding ext
                 sc->interlaced = !(pkt->data[i+5] & 0x80); // progressive frame
                 if (sc->interlaced)
@@ -2981,6 +3071,10 @@ static int mxf_init(AVFormatContext *s)
             return AVERROR(ENOMEM);
         st->priv_data = sc;
         sc->index = -1;
+        sc->color_primaries = AVCOL_PRI_UNSPECIFIED;
+        sc->color_trc       = AVCOL_TRC_UNSPECIFIED;
+        sc->color_space     = AVCOL_SPC_UNSPECIFIED;
+        sc->color_range     = AVCOL_RANGE_UNSPECIFIED;
 
         if (((i == 0) ^ (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)) && 
!IS_OPATOM(s)) {
             av_log(s, AV_LOG_ERROR, "there must be exactly one video stream 
and it must be the first one\n");
-- 
2.54.0

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to