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

Fixes DNxHD/DNxHR color signalling against SMPTE ST 2019-1:2016 and ST 2019-4, 
checked with Avid's decoder shipped in macOS (VideoToolbox `DNXDecoder.bundle`).

- **RGB color format (CLF)**: CLF=1 means RGB, with channels R, G, B (ST 2019-1 
7.2.5, 8.1). Both were inverted, so Avid's decoder read our RGB 4:4:4 as YCbCr; 
fixing only the flag gave R/B swapped colors. The decoder now outputs YUV 4:4:4 
for CLF=0, tracks the per-macroblock ACF flag (Avid RGB streams with ACF=0 came 
out as yuv444p10), and tags RGB frames `gbr`. The encoder uses ACF only for 
BT.709 4:4:4 YCbCr; other matrices use a YCbCr bitstream.
- **Color volume (CLV)**: the encoder always wrote BT.709; it now maps BT.709, 
BT.2020 NCL/CL, or out-of-band.
- **Decoder colorspace**: the container wins again, as intended by c6a905b91d 
and as Avid's decoder does; CLV is the fallback.
- **mxfenc**: RGBA descriptor only for RGB (CLF=1) DNxHR 444. YCbCr 4:4:4 in an 
RGBA descriptor fails in Avid's decoder; ST 2019-4 requires the descriptor to 
match the bitstream.

RGB 4:4:4 files written by earlier ffmpeg versions now decode with wrong colors 
here too (Avid already decoded them that way).

FATE passes; no reference changes.

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



From d5a6fbe64ddcca0bfcabdb7252f36e85d366a6be Mon Sep 17 00:00:00 2001
From: Baptiste Coudurier <[email protected]>
Date: Thu, 12 Nov 2026 16:24:04 -0800
Subject: [PATCH 1/4] avcodec/dnxhd: fix the RGB color format

CLF=1 is RGB with channels R, G, B (ST 2019-1 7.2.5, 8.1); both
were inverted, so Avid's decoder misread our RGB streams.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Baptiste Coudurier <[email protected]>
---
 libavcodec/dnxhddec.c | 111 ++++++++++++++++++++++++------------------
 libavcodec/dnxhdenc.c |  24 ++++++---
 libavcodec/dnxhdenc.h |   2 +
 3 files changed, 83 insertions(+), 54 deletions(-)

diff --git a/libavcodec/dnxhddec.c b/libavcodec/dnxhddec.c
index 86c72fcbe3b..41634659d94 100644
--- a/libavcodec/dnxhddec.c
+++ b/libavcodec/dnxhddec.c
@@ -47,7 +47,7 @@ typedef struct RowContext {
     int last_dc[3];
     int last_qscale;
     int errors;
-    /** -1:not set yet  0:off=RGB  1:on=YUV  2:variable */
+    /** ACF of the macroblocks: -1:none yet  0:RGB  1:YCbCr  2:variable */
     int format;
 } RowContext;
 
@@ -73,7 +73,7 @@ typedef struct DNXHDContext {
     int alpha;
     int lla;
     int mbaff;
-    int act;
+    int rgb;                            ///< CLF: bitstream uses the RGB format
     int (*decode_dct_block)(const struct DNXHDContext *ctx,
                             RowContext *row, int n);
 } DNXHDContext;
@@ -246,10 +246,10 @@ static int dnxhd_decode_header(DNXHDContext *ctx, AVFrame 
*frame,
     case 3: frame->colorspace = AVCOL_SPC_UNSPECIFIED; break;
     }
 
-    ctx->act = buf[0x2C] & 1;
-    if (ctx->act && ctx->cid_table->cid != 1256 && ctx->cid_table->cid != 1270)
+    ctx->rgb = buf[0x2C] & 1;
+    if (ctx->rgb && ctx->cid_table->cid != 1256 && ctx->cid_table->cid != 1270)
         av_log(ctx->avctx, AV_LOG_WARNING,
-               "Adaptive color transform in an unsupported profile.\n");
+               "RGB color format in an unsupported profile.\n");
 
     ctx->is_444 = (buf[0x2C] >> 6) & 1;
     if (ctx->is_444) {
@@ -258,12 +258,17 @@ static int dnxhd_decode_header(DNXHDContext *ctx, AVFrame 
*frame,
             return AVERROR_INVALIDDATA;
         } else if (bitdepth == 10) {
             ctx->decode_dct_block = dnxhd_decode_dct_block_10_444;
-            ctx->pix_fmt = ctx->act ? AV_PIX_FMT_YUV444P10
-                                    : AV_PIX_FMT_GBRP10;
+            /* For RGB, the macroblock ACF flags decide after decoding. */
+            if (!ctx->rgb)
+                ctx->pix_fmt = AV_PIX_FMT_YUV444P10;
+            else if (ctx->pix_fmt != AV_PIX_FMT_YUV444P10)
+                ctx->pix_fmt = AV_PIX_FMT_GBRP10;
         } else {
             ctx->decode_dct_block = dnxhd_decode_dct_block_12_444;
-            ctx->pix_fmt = ctx->act ? AV_PIX_FMT_YUV444P12
-                                    : AV_PIX_FMT_GBRP12;
+            if (!ctx->rgb)
+                ctx->pix_fmt = AV_PIX_FMT_YUV444P12;
+            else if (ctx->pix_fmt != AV_PIX_FMT_YUV444P12)
+                ctx->pix_fmt = AV_PIX_FMT_GBRP12;
         }
     } else if (bitdepth == 12) {
         ctx->decode_dct_block = dnxhd_decode_dct_block_12;
@@ -309,9 +314,9 @@ static int dnxhd_decode_header(DNXHDContext *ctx, AVFrame 
*frame,
     if ((ctx->height + 15) >> 4 == ctx->mb_height && (frame->flags & 
AV_FRAME_FLAG_INTERLACED))
         ctx->height <<= 1;
 
-    av_log(ctx->avctx, AV_LOG_VERBOSE, "%dx%d, 4:%s %d bits, MBAFF=%d 
ACT=%d\n",
+    av_log(ctx->avctx, AV_LOG_VERBOSE, "%dx%d, 4:%s %d bits, MBAFF=%d 
RGB=%d\n",
            ctx->width, ctx->height, ctx->is_444 ? "4:4" : "2:2",
-           ctx->bit_depth, ctx->mbaff, ctx->act);
+           ctx->bit_depth, ctx->mbaff, ctx->rgb);
 
     // Newer format supports variable mb_scan_index sizes
     if (ctx->mb_height > 68 && ff_dnxhd_check_header_prefix_hr(header_prefix)) 
{
@@ -494,11 +499,11 @@ static int dnxhd_decode_macroblock(const DNXHDContext 
*ctx, RowContext *row,
                                    AVFrame *frame, int x, int y)
 {
     int shift1 = ctx->bit_depth >= 10;
-    int dct_linesize_luma   = frame->linesize[0];
-    int dct_linesize_chroma = frame->linesize[1];
+    int dct_linesize_luma, dct_linesize_chroma;
     uint8_t *dest_y, *dest_u, *dest_v;
     int dct_y_offset, dct_x_offset;
-    int qscale, i, act;
+    int qscale, i, acf;
+    int p0 = 0, p1 = 1, p2 = 2;
     int interlaced_mb = 0;
 
     if (ctx->mbaff) {
@@ -507,21 +512,28 @@ static int dnxhd_decode_macroblock(const DNXHDContext 
*ctx, RowContext *row,
     } else {
         qscale = get_bits(&row->gb, 11);
     }
-    act = get_bits1(&row->gb);
-    if (act) {
-        if (!ctx->act) {
-            static int act_warned;
-            if (!act_warned) {
-                act_warned = 1;
-                av_log(ctx->avctx, AV_LOG_ERROR,
-                       "ACT flag set, in violation of frame header.\n");
-            }
-        } else if (row->format == -1) {
-            row->format = act;
-        } else if (row->format != act) {
+    acf = get_bits1(&row->gb);
+    if (ctx->rgb) {
+        if (row->format == -1)
+            row->format = acf;
+        else if (row->format != acf)
             row->format = 2; // Variable
+        /* RGB mode: channels 1, 2, 3 are R, G, B */
+        if (!acf) {
+            p0 = 2;
+            p1 = 0;
+            p2 = 1;
+        }
+    } else if (acf) {
+        static int acf_warned;
+        if (!acf_warned) {
+            acf_warned = 1;
+            av_log(ctx->avctx, AV_LOG_ERROR,
+                   "ACF flag set, in violation of frame header.\n");
         }
     }
+    dct_linesize_luma   = frame->linesize[p0];
+    dct_linesize_chroma = frame->linesize[p1];
 
     if (qscale != row->last_qscale) {
         for (i = 0; i < 64; i++) {
@@ -541,21 +553,21 @@ static int dnxhd_decode_macroblock(const DNXHDContext 
*ctx, RowContext *row,
         dct_linesize_chroma <<= 1;
     }
 
-    dest_y = frame->data[0] + ((y * dct_linesize_luma)   << 4) + (x << (4 + 
shift1));
-    dest_u = frame->data[1] + ((y * dct_linesize_chroma) << 4) + (x << (3 + 
shift1 + ctx->is_444));
-    dest_v = frame->data[2] + ((y * dct_linesize_chroma) << 4) + (x << (3 + 
shift1 + ctx->is_444));
+    dest_y = frame->data[p0] + ((y * dct_linesize_luma)   << 4) + (x << (4 + 
shift1));
+    dest_u = frame->data[p1] + ((y * dct_linesize_chroma) << 4) + (x << (3 + 
shift1 + ctx->is_444));
+    dest_v = frame->data[p2] + ((y * dct_linesize_chroma) << 4) + (x << (3 + 
shift1 + ctx->is_444));
 
     if ((frame->flags & AV_FRAME_FLAG_INTERLACED) && ctx->cur_field) {
-        dest_y += frame->linesize[0];
-        dest_u += frame->linesize[1];
-        dest_v += frame->linesize[2];
+        dest_y += frame->linesize[p0];
+        dest_u += frame->linesize[p1];
+        dest_v += frame->linesize[p2];
     }
     if (interlaced_mb) {
         dct_linesize_luma   <<= 1;
         dct_linesize_chroma <<= 1;
     }
 
-    dct_y_offset = interlaced_mb ? frame->linesize[0] : (dct_linesize_luma << 
3);
+    dct_y_offset = interlaced_mb ? frame->linesize[p0] : (dct_linesize_luma << 
3);
     dct_x_offset = 8 << shift1;
     if (!ctx->is_444) {
         ctx->idsp.idct_put(dest_y,                               
dct_linesize_luma, row->blocks[0]);
@@ -564,7 +576,7 @@ static int dnxhd_decode_macroblock(const DNXHDContext *ctx, 
RowContext *row,
         ctx->idsp.idct_put(dest_y + dct_y_offset + dct_x_offset, 
dct_linesize_luma, row->blocks[5]);
 
         if (!(ctx->avctx->flags & AV_CODEC_FLAG_GRAY)) {
-            dct_y_offset = interlaced_mb ? frame->linesize[1] : 
(dct_linesize_chroma << 3);
+            dct_y_offset = interlaced_mb ? frame->linesize[p1] : 
(dct_linesize_chroma << 3);
             ctx->idsp.idct_put(dest_u,                dct_linesize_chroma, 
row->blocks[2]);
             ctx->idsp.idct_put(dest_v,                dct_linesize_chroma, 
row->blocks[3]);
             ctx->idsp.idct_put(dest_u + dct_y_offset, dct_linesize_chroma, 
row->blocks[6]);
@@ -577,7 +589,7 @@ static int dnxhd_decode_macroblock(const DNXHDContext *ctx, 
RowContext *row,
         ctx->idsp.idct_put(dest_y + dct_y_offset + dct_x_offset, 
dct_linesize_luma, row->blocks[7]);
 
         if (!(ctx->avctx->flags & AV_CODEC_FLAG_GRAY)) {
-            dct_y_offset = interlaced_mb ? frame->linesize[1] : 
(dct_linesize_chroma << 3);
+            dct_y_offset = interlaced_mb ? frame->linesize[p1] : 
(dct_linesize_chroma << 3);
             ctx->idsp.idct_put(dest_u,                               
dct_linesize_chroma, row->blocks[2]);
             ctx->idsp.idct_put(dest_u + dct_x_offset,                
dct_linesize_chroma, row->blocks[3]);
             ctx->idsp.idct_put(dest_u + dct_y_offset,                
dct_linesize_chroma, row->blocks[8]);
@@ -676,23 +688,21 @@ decode_coding_unit:
         ctx->rows[i].errors = 0;
     }
 
-    if (ctx->act) {
-        static int act_warned;
-        int format = ctx->rows[0].format;
-        for (i = 1; i < avctx->thread_count; i++) {
-            if (ctx->rows[i].format != format &&
-                ctx->rows[i].format != -1 /* not run */) {
-                format = 2;
-                break;
-            }
+    if (ctx->rgb) {
+        static int acf_warned;
+        int format = -1;
+        for (i = 0; i < avctx->thread_count; i++) {
+            int row_format = ctx->rows[i].format;
+            if (row_format == -1) /* not run */
+                continue;
+            format = format == -1 || format == row_format ? row_format : 2;
         }
         switch (format) {
-        case -1:
         case 2:
-            if (!act_warned) {
-                act_warned = 1;
+            if (!acf_warned) {
+                acf_warned = 1;
                 av_log(ctx->avctx, AV_LOG_ERROR,
-                       "Unsupported: variable ACT flag.\n");
+                       "Unsupported: variable ACF flag.\n");
             }
             break;
         case 0:
@@ -704,6 +714,11 @@ decode_coding_unit:
                          ? AV_PIX_FMT_YUV444P10 : AV_PIX_FMT_YUV444P12;
             break;
         }
+        picture->format = ctx->pix_fmt;
+        /* ACF macroblocks use the BT.709 matrix regardless of CLV */
+        picture->colorspace = ctx->pix_fmt == AV_PIX_FMT_GBRP10 ||
+                              ctx->pix_fmt == AV_PIX_FMT_GBRP12 ?
+                              AVCOL_SPC_RGB : AVCOL_SPC_BT709;
     }
     avctx->pix_fmt = ctx->pix_fmt;
     if (ret) {
diff --git a/libavcodec/dnxhdenc.c b/libavcodec/dnxhdenc.c
index 844731f6c40..8f7ca4b618a 100644
--- a/libavcodec/dnxhdenc.c
+++ b/libavcodec/dnxhdenc.c
@@ -390,6 +390,11 @@ static av_cold int dnxhd_encode_init(AVCodecContext *avctx)
     }
 
     ctx->is_444 = ctx->profile == AV_PROFILE_DNXHR_444;
+    ctx->rgb    = avctx->pix_fmt == AV_PIX_FMT_GBRP10;
+    /* ACF implies the BT.709 matrix, otherwise use a YCbCr bitstream */
+    ctx->acf    = avctx->pix_fmt == AV_PIX_FMT_YUV444P10 &&
+                  (avctx->colorspace == AVCOL_SPC_BT709 ||
+                   avctx->colorspace == AVCOL_SPC_UNSPECIFIED);
     avctx->profile = ctx->profile;
     ctx->cid = ff_dnxhd_find_cid(avctx, ctx->bit_depth);
     if (!ctx->cid) {
@@ -541,7 +546,7 @@ static int dnxhd_write_header(AVCodecContext *avctx, 
uint8_t *buf)
     buf[0x21] = ctx->bit_depth == 10 ? 0x58 : 0x38;
     buf[0x22] = 0x88 + (ctx->interlaced << 2);
     AV_WB32(buf + 0x28, ctx->cid); // CID
-    buf[0x2c] = (!ctx->interlaced << 7) | (ctx->is_444 << 6) | (avctx->pix_fmt 
== AV_PIX_FMT_YUV444P10);
+    buf[0x2c] = (!ctx->interlaced << 7) | (ctx->is_444 << 6) | (ctx->rgb || 
ctx->acf);
 
     buf[0x5f] = 0x01; // UDL
 
@@ -889,7 +894,7 @@ static int dnxhd_encode_thread(AVCodecContext *avctx, void 
*arg,
         int i;
 
         put_bits(pb, 11, qscale);
-        put_bits(pb, 1, avctx->pix_fmt == AV_PIX_FMT_YUV444P10);
+        put_bits(pb, 1, ctx->acf);
 
         dnxhd_get_blocks(ctx, mb_x, mb_y);
 
@@ -1229,11 +1234,17 @@ retry:
     return 0;
 }
 
+/* RGB mode channels are R, G, B */
+static const uint8_t rgb_planes[3] = { 2, 0, 1 };
+static const uint8_t yuv_planes[3] = { 0, 1, 2 };
+
 static void dnxhd_load_picture(DNXHDEncContext *ctx, const AVFrame *frame)
 {
+    const uint8_t *planes = ctx->rgb ? rgb_planes : yuv_planes;
+
     for (int i = 0; i < ctx->m.c.avctx->thread_count; i++) {
-        ctx->thread[i]->m.c.linesize    = frame->linesize[0] << 
ctx->interlaced;
-        ctx->thread[i]->m.c.uvlinesize  = frame->linesize[1] << 
ctx->interlaced;
+        ctx->thread[i]->m.c.linesize    = frame->linesize[planes[0]] << 
ctx->interlaced;
+        ctx->thread[i]->m.c.uvlinesize  = frame->linesize[planes[1]] << 
ctx->interlaced;
         ctx->thread[i]->dct_y_offset    = ctx->m.c.linesize  *8;
         ctx->thread[i]->dct_uv_offset   = ctx->m.c.uvlinesize*8;
     }
@@ -1258,9 +1269,10 @@ static int dnxhd_encode_picture(AVCodecContext *avctx, 
AVPacket *pkt,
 
 encode_coding_unit:
     for (i = 0; i < 3; i++) {
-        ctx->src[i] = frame->data[i];
+        int plane = ctx->rgb ? rgb_planes[i] : i;
+        ctx->src[i] = frame->data[plane];
         if (ctx->interlaced && ctx->cur_field)
-            ctx->src[i] += frame->linesize[i];
+            ctx->src[i] += frame->linesize[plane];
     }
 
     dnxhd_write_header(avctx, buf);
diff --git a/libavcodec/dnxhdenc.h b/libavcodec/dnxhdenc.h
index 7540607cdc3..4aeb1f83d99 100644
--- a/libavcodec/dnxhdenc.h
+++ b/libavcodec/dnxhdenc.h
@@ -49,6 +49,8 @@ typedef struct DNXHDEncContext {
     int profile;
     int bit_depth;
     int is_444;
+    int rgb;  ///< CLF: bitstream uses the RGB format
+    int acf;  ///< macroblocks are coded as BT.709 YCbCr in an RGB bitstream
     const CIDEntry *cid_table;
     uint8_t *msip; ///< Macroblock Scan Indexes Payload
     uint32_t *slice_size;
-- 
2.54.0


From 3fd60beb7e55daaf05936d5365155ecde4e8c8af Mon Sep 17 00:00:00 2001
From: Baptiste Coudurier <[email protected]>
Date: Thu, 12 Nov 2026 16:24:04 -0800
Subject: [PATCH 2/4] avcodec/dnxhdenc: signal the color volume

CLV was always BT.709.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Baptiste Coudurier <[email protected]>
---
 libavcodec/dnxhdenc.c | 25 ++++++++++++++++++++++++-
 libavcodec/dnxhdenc.h |  1 +
 2 files changed, 25 insertions(+), 1 deletion(-)

diff --git a/libavcodec/dnxhdenc.c b/libavcodec/dnxhdenc.c
index 8f7ca4b618a..8f46a7dd0ce 100644
--- a/libavcodec/dnxhdenc.c
+++ b/libavcodec/dnxhdenc.c
@@ -350,6 +350,28 @@ static av_cold int dnxhd_init_rc(DNXHDEncContext *ctx)
     return 0;
 }
 
+static int dnxhd_color_volume(const AVCodecContext *avctx, int cid, int rgb)
+{
+    int hr = cid >= 1270 && cid <= 1274;
+
+    if (rgb) {
+        switch (avctx->color_primaries) {
+        case AVCOL_PRI_UNSPECIFIED:
+        case AVCOL_PRI_BT709:      return 0;
+        case AVCOL_PRI_BT2020:     return hr ? 1 : 3;
+        default:                   return 3;
+        }
+    }
+
+    switch (avctx->colorspace) {
+    case AVCOL_SPC_UNSPECIFIED:
+    case AVCOL_SPC_BT709:      return 0;
+    case AVCOL_SPC_BT2020_NCL: return hr ? 1 : 3;
+    case AVCOL_SPC_BT2020_CL:  return hr ? 2 : 3;
+    default:                   return 3; // out-of-band
+    }
+}
+
 static av_cold int dnxhd_encode_init(AVCodecContext *avctx)
 {
     DNXHDEncContext *ctx = avctx->priv_data;
@@ -403,6 +425,7 @@ static av_cold int dnxhd_encode_init(AVCodecContext *avctx)
         ff_dnxhd_print_profiles(avctx, AV_LOG_ERROR);
         return AVERROR(EINVAL);
     }
+    ctx->clv = dnxhd_color_volume(avctx, ctx->cid, ctx->rgb);
     av_log(avctx, AV_LOG_DEBUG, "cid %d\n", ctx->cid);
 
     if (ctx->cid >= 1270 && ctx->cid <= 1274)
@@ -546,7 +569,7 @@ static int dnxhd_write_header(AVCodecContext *avctx, 
uint8_t *buf)
     buf[0x21] = ctx->bit_depth == 10 ? 0x58 : 0x38;
     buf[0x22] = 0x88 + (ctx->interlaced << 2);
     AV_WB32(buf + 0x28, ctx->cid); // CID
-    buf[0x2c] = (!ctx->interlaced << 7) | (ctx->is_444 << 6) | (ctx->rgb || 
ctx->acf);
+    buf[0x2c] = (!ctx->interlaced << 7) | (ctx->is_444 << 6) | (ctx->clv << 1) 
| (ctx->rgb || ctx->acf);
 
     buf[0x5f] = 0x01; // UDL
 
diff --git a/libavcodec/dnxhdenc.h b/libavcodec/dnxhdenc.h
index 4aeb1f83d99..b6cf4da38d0 100644
--- a/libavcodec/dnxhdenc.h
+++ b/libavcodec/dnxhdenc.h
@@ -51,6 +51,7 @@ typedef struct DNXHDEncContext {
     int is_444;
     int rgb;  ///< CLF: bitstream uses the RGB format
     int acf;  ///< macroblocks are coded as BT.709 YCbCr in an RGB bitstream
+    int clv;  ///< color volume
     const CIDEntry *cid_table;
     uint8_t *msip; ///< Macroblock Scan Indexes Payload
     uint32_t *slice_size;
-- 
2.54.0


From 48fd134ccc12f67ee106a5b27e93c5a42d1f2f43 Mon Sep 17 00:00:00 2001
From: Baptiste Coudurier <[email protected]>
Date: Thu, 12 Nov 2026 16:23:57 -0800
Subject: [PATCH 3/4] avcodec/dnxhddec: prefer the container color space

As intended by c6a905b91d and done by Avid's decoder; CLV is the fallback.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Baptiste Coudurier <[email protected]>
---
 libavcodec/dnxhddec.c | 28 ++++++++++++++++------------
 1 file changed, 16 insertions(+), 12 deletions(-)

diff --git a/libavcodec/dnxhddec.c b/libavcodec/dnxhddec.c
index 41634659d94..53ed8b5e778 100644
--- a/libavcodec/dnxhddec.c
+++ b/libavcodec/dnxhddec.c
@@ -74,6 +74,7 @@ typedef struct DNXHDContext {
     int lla;
     int mbaff;
     int rgb;                            ///< CLF: bitstream uses the RGB format
+    int clv;                            ///< color volume
     int (*decode_dct_block)(const struct DNXHDContext *ctx,
                             RowContext *row, int n);
 } DNXHDContext;
@@ -98,9 +99,6 @@ static av_cold int dnxhd_decode_init(AVCodecContext *avctx)
 
     ctx->avctx = avctx;
     ctx->cid = -1;
-    if (avctx->colorspace == AVCOL_SPC_UNSPECIFIED) {
-        avctx->colorspace = AVCOL_SPC_BT709;
-    }
 
     avctx->coded_width  = FFALIGN(avctx->width,  16);
     avctx->coded_height = FFALIGN(avctx->height, 16);
@@ -239,12 +237,7 @@ static int dnxhd_decode_header(DNXHDContext *ctx, AVFrame 
*frame,
         av_log(ctx->avctx, AV_LOG_WARNING,
                "Adaptive MB interlace flag in an unsupported profile.\n");
 
-    switch ((buf[0x2C] >> 1) & 3) {
-    case 0: frame->colorspace = AVCOL_SPC_BT709;       break;
-    case 1: frame->colorspace = AVCOL_SPC_BT2020_NCL;  break;
-    case 2: frame->colorspace = AVCOL_SPC_BT2020_CL;   break;
-    case 3: frame->colorspace = AVCOL_SPC_UNSPECIFIED; break;
-    }
+    ctx->clv = (buf[0x2C] >> 1) & 3;
 
     ctx->rgb = buf[0x2C] & 1;
     if (ctx->rgb && ctx->cid_table->cid != 1256 && ctx->cid_table->cid != 1270)
@@ -715,10 +708,21 @@ decode_coding_unit:
             break;
         }
         picture->format = ctx->pix_fmt;
+    }
+
+    if (ctx->pix_fmt == AV_PIX_FMT_GBRP10 || ctx->pix_fmt == 
AV_PIX_FMT_GBRP12) {
+        picture->colorspace = AVCOL_SPC_RGB;
+    } else if (ctx->rgb) {
         /* ACF macroblocks use the BT.709 matrix regardless of CLV */
-        picture->colorspace = ctx->pix_fmt == AV_PIX_FMT_GBRP10 ||
-                              ctx->pix_fmt == AV_PIX_FMT_GBRP12 ?
-                              AVCOL_SPC_RGB : AVCOL_SPC_BT709;
+        picture->colorspace = AVCOL_SPC_BT709;
+    } else if (avctx->colorspace == AVCOL_SPC_UNSPECIFIED ||
+               avctx->colorspace == AVCOL_SPC_RGB) {
+        /* Like the reference decoder, prefer the container color space */
+        static const enum AVColorSpace clv_csp[4] = {
+            AVCOL_SPC_BT709, AVCOL_SPC_BT2020_NCL,
+            AVCOL_SPC_BT2020_CL, AVCOL_SPC_UNSPECIFIED,
+        };
+        picture->colorspace = clv_csp[ctx->clv];
     }
     avctx->pix_fmt = ctx->pix_fmt;
     if (ret) {
-- 
2.54.0


From 74e792fcbfe72fd18c69965728f2d8a0ae242325 Mon Sep 17 00:00:00 2001
From: Baptiste Coudurier <[email protected]>
Date: Thu, 12 Nov 2026 16:28:07 -0800
Subject: [PATCH 4/4] avformat/mxfenc: use the RGBA descriptor only for RGB
 DNxHR

DNxHR 444 may be YCbCr (CLF=0).

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

diff --git a/libavformat/mxfenc.c b/libavformat/mxfenc.c
index 867a4abd4d2..8dcbec20d7b 100644
--- a/libavformat/mxfenc.c
+++ b/libavformat/mxfenc.c
@@ -2368,7 +2368,7 @@ static int mxf_parse_dnxhd_frame(AVFormatContext *s, 
AVStream *st, AVPacket *pkt
     if (mxf->header_written)
         return 1;
 
-    if (pkt->size < 43)
+    if (pkt->size < 0x2d)
         return 0;
 
     sc->cid = AV_RB32(pkt->data + 0x28);
@@ -2392,7 +2392,7 @@ static int mxf_parse_dnxhd_frame(AVFormatContext *s, 
AVStream *st, AVPacket *pkt
 
     sc->frame_size = pkt->size;
 
-    if (sc->cid == 1270 || sc->cid == 1256)
+    if ((sc->cid == 1270 || sc->cid == 1256) && pkt->data[0x2c] & 1) // CLF: 
RGB
         sc->picture_descriptor_key = &mxf_rgba_descriptor_key;
 
     return 1;
-- 
2.54.0

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

Reply via email to