Commit 6aa7951220 for ffmpeg

commit 6aa7951220fe62d7d119d15c2f855384615856e6
Author: Baptiste Coudurier <baptiste.coudurier@gmail.com>
Date:   Thu Nov 12 16:24:04 2026 -0800

    avcodec/dnxhd: fix the color format and color volume

    CLF=1 is RGB with channels R, G, B (ST 2019-1 7.2.5, 8.1), both were
    inverted. The decoder sets per-frame color metadata from CLV and CLF.

    Select the MXF descriptor from CLF and 4:4:4 sampling. Apply the video-
    range default only to in-band RGB without an explicit range. Keep using
    green for RGB rate control.

    Fill missing MXF primaries and matrix from the in-band CLV.

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

diff --git a/libavcodec/dnxhddec.c b/libavcodec/dnxhddec.c
index 86c72fcbe3..ac4452d668 100644
--- a/libavcodec/dnxhddec.c
+++ b/libavcodec/dnxhddec.c
@@ -47,8 +47,6 @@ typedef struct RowContext {
     int last_dc[3];
     int last_qscale;
     int errors;
-    /** -1:not set yet  0:off=RGB  1:on=YUV  2:variable */
-    int format;
 } RowContext;

 typedef struct DNXHDContext {
@@ -73,7 +71,8 @@ typedef struct DNXHDContext {
     int alpha;
     int lla;
     int mbaff;
-    int act;
+    int rgb;                            ///< CLF: bitstream uses the RGB format
+    int acf;                            ///< ACF of the first macroblock
     int (*decode_dct_block)(const struct DNXHDContext *ctx,
                             RowContext *row, int n);
 } DNXHDContext;
@@ -98,9 +97,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);
@@ -179,7 +175,7 @@ static int dnxhd_decode_header(DNXHDContext *ctx, AVFrame *frame,
                                const uint8_t *buf, int buf_size,
                                int first_field)
 {
-    int i, cid, ret;
+    int i, cid, clv, ret;
     int old_bit_depth = ctx->bit_depth, bitdepth;
     uint64_t header_prefix;
     if (buf_size < 0x280) {
@@ -239,31 +235,37 @@ 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) {
+    clv = (buf[0x2C] >> 1) & 3;
+    switch (clv) {
     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;
     }
+    /* older encoders wrote CLV=0 for any primaries */
+    if (clv == 1 || clv == 2)
+        frame->color_primaries = AVCOL_PRI_BT2020;

-    ctx->act = buf[0x2C] & 1;
-    if (ctx->act && 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");
-
+    ctx->rgb    = buf[0x2C] & 1;
     ctx->is_444 = (buf[0x2C] >> 6) & 1;
+    if (ctx->rgb && ctx->cid_table->cid != 1256 && ctx->cid_table->cid != 1270)
+        av_log(ctx->avctx, AV_LOG_WARNING,
+               "RGB color format in an unsupported profile.\n");
+    if (ctx->rgb && !ctx->is_444) {
+        av_log(ctx->avctx, AV_LOG_WARNING,
+               "RGB color format without 4:4:4 sampling, decoding as YCbCr.\n");
+        ctx->rgb = 0;
+    }
     if (ctx->is_444) {
         if (bitdepth == 8) {
             avpriv_request_sample(ctx->avctx, "4:4:4 8 bits");
             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;
+            ctx->pix_fmt = AV_PIX_FMT_YUV444P10;
         } else {
             ctx->decode_dct_block = dnxhd_decode_dct_block_12_444;
-            ctx->pix_fmt = ctx->act ? AV_PIX_FMT_YUV444P12
-                                    : AV_PIX_FMT_GBRP12;
+            ctx->pix_fmt = AV_PIX_FMT_YUV444P12;
         }
     } else if (bitdepth == 12) {
         ctx->decode_dct_block = dnxhd_decode_dct_block_12;
@@ -309,9 +311,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)) {
@@ -354,6 +356,22 @@ static int dnxhd_decode_header(DNXHDContext *ctx, AVFrame *frame,
         }
     }

+    ctx->acf = 0;
+    if (ctx->rgb) {
+        uint32_t offset = ctx->data_offset + ctx->mb_scan_index[0];
+
+        if (!ctx->mb_height || buf_size - offset < 2)
+            return AVERROR_INVALIDDATA;
+        /* the first macroblock's ACF selects the output format */
+        ctx->acf = buf[offset + 1] >> 4 & 1;
+        if (!ctx->acf)
+            ctx->pix_fmt = bitdepth == 10 ? AV_PIX_FMT_GBRP10 : AV_PIX_FMT_GBRP12;
+        frame->colorspace = ctx->acf ? AVCOL_SPC_BT709 : AVCOL_SPC_RGB;
+        /* in-band color volumes are video range */
+        if (clv < 3 && ctx->avctx->color_range == AVCOL_RANGE_UNSPECIFIED)
+            frame->color_range = AVCOL_RANGE_MPEG;
+    }
+
     return 0;
 }

@@ -494,11 +512,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 linesize_y, linesize_u, linesize_v;
     uint8_t *dest_y, *dest_u, *dest_v;
-    int dct_y_offset, dct_x_offset;
-    int qscale, i, act;
+    int dct_y_offset, dct_u_offset, dct_v_offset, dct_x_offset;
+    int qscale, i, acf;
+    int p0 = 0, p1 = 1, p2 = 2;
     int interlaced_mb = 0;

     if (ctx->mbaff) {
@@ -507,21 +525,30 @@ 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) {
-            row->format = 2; // Variable
+    acf = get_bits1(&row->gb);
+    if (ctx->rgb) {
+        if (acf != ctx->acf) {
+            av_log(ctx->avctx, AV_LOG_ERROR,
+                   "Mixed ACF macroblocks are not supported.\n");
+            return AVERROR_PATCHWELCOME;
+        }
+        /* 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");
         }
     }
+    linesize_y = frame->linesize[p0];
+    linesize_u = frame->linesize[p1];
+    linesize_v = frame->linesize[p2];

     if (qscale != row->last_qscale) {
         for (i = 0; i < 64; i++) {
@@ -537,55 +564,61 @@ static int dnxhd_decode_macroblock(const DNXHDContext *ctx, RowContext *row,
     }

     if (frame->flags & AV_FRAME_FLAG_INTERLACED) {
-        dct_linesize_luma   <<= 1;
-        dct_linesize_chroma <<= 1;
+        linesize_y *= 2;
+        linesize_u *= 2;
+        linesize_v *= 2;
     }

-    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] + (ptrdiff_t)y * 16 * linesize_y + (x << (4 + shift1));
+    dest_u = frame->data[p1] + (ptrdiff_t)y * 16 * linesize_u + (x << (3 + shift1 + ctx->is_444));
+    dest_v = frame->data[p2] + (ptrdiff_t)y * 16 * linesize_v + (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 = frame->linesize[p0];
+        dct_u_offset = frame->linesize[p1];
+        dct_v_offset = frame->linesize[p2];
+        linesize_y *= 2;
+        linesize_u *= 2;
+        linesize_v *= 2;
+    } else {
+        dct_y_offset = linesize_y * 8;
+        dct_u_offset = linesize_u * 8;
+        dct_v_offset = linesize_v * 8;
     }

-    dct_y_offset = interlaced_mb ? frame->linesize[0] : (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]);
-        ctx->idsp.idct_put(dest_y + dct_x_offset,                dct_linesize_luma, row->blocks[1]);
-        ctx->idsp.idct_put(dest_y + dct_y_offset,                dct_linesize_luma, row->blocks[4]);
-        ctx->idsp.idct_put(dest_y + dct_y_offset + dct_x_offset, dct_linesize_luma, row->blocks[5]);
+        ctx->idsp.idct_put(dest_y,                               linesize_y, row->blocks[0]);
+        ctx->idsp.idct_put(dest_y + dct_x_offset,                linesize_y, row->blocks[1]);
+        ctx->idsp.idct_put(dest_y + dct_y_offset,                linesize_y, row->blocks[4]);
+        ctx->idsp.idct_put(dest_y + dct_y_offset + dct_x_offset, linesize_y, row->blocks[5]);

         if (!(ctx->avctx->flags & AV_CODEC_FLAG_GRAY)) {
-            dct_y_offset = interlaced_mb ? frame->linesize[1] : (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]);
-            ctx->idsp.idct_put(dest_v + dct_y_offset, dct_linesize_chroma, row->blocks[7]);
+            ctx->idsp.idct_put(dest_u,                linesize_u, row->blocks[2]);
+            ctx->idsp.idct_put(dest_v,                linesize_v, row->blocks[3]);
+            ctx->idsp.idct_put(dest_u + dct_u_offset, linesize_u, row->blocks[6]);
+            ctx->idsp.idct_put(dest_v + dct_v_offset, linesize_v, row->blocks[7]);
         }
     } else {
-        ctx->idsp.idct_put(dest_y,                               dct_linesize_luma, row->blocks[0]);
-        ctx->idsp.idct_put(dest_y + dct_x_offset,                dct_linesize_luma, row->blocks[1]);
-        ctx->idsp.idct_put(dest_y + dct_y_offset,                dct_linesize_luma, row->blocks[6]);
-        ctx->idsp.idct_put(dest_y + dct_y_offset + dct_x_offset, dct_linesize_luma, row->blocks[7]);
+        ctx->idsp.idct_put(dest_y,                               linesize_y, row->blocks[0]);
+        ctx->idsp.idct_put(dest_y + dct_x_offset,                linesize_y, row->blocks[1]);
+        ctx->idsp.idct_put(dest_y + dct_y_offset,                linesize_y, row->blocks[6]);
+        ctx->idsp.idct_put(dest_y + dct_y_offset + dct_x_offset, linesize_y, row->blocks[7]);

         if (!(ctx->avctx->flags & AV_CODEC_FLAG_GRAY)) {
-            dct_y_offset = interlaced_mb ? frame->linesize[1] : (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]);
-            ctx->idsp.idct_put(dest_u + dct_y_offset + dct_x_offset, dct_linesize_chroma, row->blocks[9]);
-            ctx->idsp.idct_put(dest_v,                               dct_linesize_chroma, row->blocks[4]);
-            ctx->idsp.idct_put(dest_v + dct_x_offset,                dct_linesize_chroma, row->blocks[5]);
-            ctx->idsp.idct_put(dest_v + dct_y_offset,                dct_linesize_chroma, row->blocks[10]);
-            ctx->idsp.idct_put(dest_v + dct_y_offset + dct_x_offset, dct_linesize_chroma, row->blocks[11]);
+            ctx->idsp.idct_put(dest_u,                               linesize_u, row->blocks[2]);
+            ctx->idsp.idct_put(dest_u + dct_x_offset,                linesize_u, row->blocks[3]);
+            ctx->idsp.idct_put(dest_u + dct_u_offset,                linesize_u, row->blocks[8]);
+            ctx->idsp.idct_put(dest_u + dct_u_offset + dct_x_offset, linesize_u, row->blocks[9]);
+            ctx->idsp.idct_put(dest_v,                               linesize_v, row->blocks[4]);
+            ctx->idsp.idct_put(dest_v + dct_x_offset,                linesize_v, row->blocks[5]);
+            ctx->idsp.idct_put(dest_v + dct_v_offset,                linesize_v, row->blocks[10]);
+            ctx->idsp.idct_put(dest_v + dct_v_offset + dct_x_offset, linesize_v, row->blocks[11]);
         }
     }

@@ -630,9 +663,6 @@ static int dnxhd_decode_frame(AVCodecContext *avctx, AVFrame *picture,

     ff_dlog(avctx, "frame size %d\n", buf_size);

-    for (i = 0; i < avctx->thread_count; i++)
-        ctx->rows[i].format = -1;
-
 decode_coding_unit:
     if ((ret = dnxhd_decode_header(ctx, picture, buf, buf_size, first_field)) < 0)
         return ret;
@@ -644,6 +674,8 @@ decode_coding_unit:
         first_field = 1;
     }
     if (avctx->pix_fmt != AV_PIX_FMT_NONE && avctx->pix_fmt != ctx->pix_fmt) {
+        if (!first_field && ctx->rgb)
+            return AVERROR_PATCHWELCOME;
         av_log(avctx, AV_LOG_WARNING, "pix_fmt changed: %s -> %s\n",
                av_get_pix_fmt_name(avctx->pix_fmt), av_get_pix_fmt_name(ctx->pix_fmt));
         first_field = 1;
@@ -676,36 +708,6 @@ 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;
-            }
-        }
-        switch (format) {
-        case -1:
-        case 2:
-            if (!act_warned) {
-                act_warned = 1;
-                av_log(ctx->avctx, AV_LOG_ERROR,
-                       "Unsupported: variable ACT flag.\n");
-            }
-            break;
-        case 0:
-            ctx->pix_fmt = ctx->bit_depth==10
-                         ? AV_PIX_FMT_GBRP10 : AV_PIX_FMT_GBRP12;
-            break;
-        case 1:
-            ctx->pix_fmt = ctx->bit_depth==10
-                         ? AV_PIX_FMT_YUV444P10 : AV_PIX_FMT_YUV444P12;
-            break;
-        }
-    }
-    avctx->pix_fmt = ctx->pix_fmt;
     if (ret) {
         av_log(ctx->avctx, AV_LOG_ERROR, "%d lines with errors\n", ret);
         return AVERROR_INVALIDDATA;
diff --git a/libavcodec/dnxhdenc.c b/libavcodec/dnxhdenc.c
index 844731f6c4..137addcd0a 100644
--- a/libavcodec/dnxhdenc.c
+++ b/libavcodec/dnxhdenc.c
@@ -350,6 +350,40 @@ static av_cold int dnxhd_init_rc(DNXHDEncContext *ctx)
     return 0;
 }

+/* CLV: 0 BT.709, 1 BT.2020 NCL, 2 BT.2020 CL, 3 out-of-band */
+static int dnxhd_color_volume(const AVCodecContext *avctx, int hr, int rgb)
+{
+    enum AVColorPrimaries pri = avctx->color_primaries;
+    enum AVColorTransferCharacteristic trc = avctx->color_trc;
+    int clv;
+
+    if (rgb)
+        clv = pri == AVCOL_PRI_BT2020;
+    else if (avctx->colorspace == AVCOL_SPC_BT709 ||
+             avctx->colorspace == AVCOL_SPC_UNSPECIFIED)
+        clv = 0;
+    else if (avctx->colorspace == AVCOL_SPC_BT2020_NCL)
+        clv = 1;
+    else if (avctx->colorspace == AVCOL_SPC_BT2020_CL)
+        clv = 2;
+    else
+        return 3;
+
+    /* in-band color volumes are video range, BT.2020 is only for DNxHR */
+    if (avctx->color_range == AVCOL_RANGE_JPEG || (clv && !hr) ||
+        (pri != AVCOL_PRI_UNSPECIFIED &&
+         pri != (clv ? AVCOL_PRI_BT2020 : AVCOL_PRI_BT709)))
+        return 3;
+    /* ST 2067-70 uses CLV=1 for BT.2020 PQ and HLG */
+    if (trc == AVCOL_TRC_SMPTE2084 || trc == AVCOL_TRC_ARIB_STD_B67)
+        return clv == 1 ? 1 : 3;
+    if (trc != AVCOL_TRC_UNSPECIFIED && trc != AVCOL_TRC_BT709 &&
+        trc != AVCOL_TRC_SMPTE170M && trc != AVCOL_TRC_BT2020_10 &&
+        trc != AVCOL_TRC_BT2020_12)
+        return 3;
+    return clv;
+}
+
 static av_cold int dnxhd_encode_init(AVCodecContext *avctx)
 {
     DNXHDEncContext *ctx = avctx->priv_data;
@@ -390,6 +424,7 @@ 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;
     avctx->profile = ctx->profile;
     ctx->cid = ff_dnxhd_find_cid(avctx, ctx->bit_depth);
     if (!ctx->cid) {
@@ -398,6 +433,9 @@ 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 >= 1270 && ctx->cid <= 1274, ctx->rgb);
+    /* ACF implies the BT.709 matrix, otherwise use a YCbCr bitstream */
+    ctx->acf = avctx->pix_fmt == AV_PIX_FMT_YUV444P10 && !ctx->clv;
     av_log(avctx, AV_LOG_DEBUG, "cid %d\n", ctx->cid);

     if (ctx->cid >= 1270 && ctx->cid <= 1274)
@@ -541,7 +579,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->clv << 1) | (ctx->rgb || ctx->acf);

     buf[0x5f] = 0x01; // UDL

@@ -678,14 +716,16 @@ void dnxhd_get_blocks(DNXHDEncContext *ctx, int mb_x, int mb_y)
     const int bw = 1 << bs;
     int dct_y_offset = ctx->dct_y_offset;
     int dct_uv_offset = ctx->dct_uv_offset;
+    int dct_v_offset = ctx->vlinesize * 8;
     int linesize = ctx->m.c.linesize;
     int uvlinesize = ctx->m.c.uvlinesize;
+    int vlinesize = ctx->vlinesize;
     const uint8_t *ptr_y = ctx->thread[0]->src[0] +
                            ((mb_y << 4) * ctx->m.c.linesize) + (mb_x << bs + 1);
     const uint8_t *ptr_u = ctx->thread[0]->src[1] +
                            ((mb_y << 4) * ctx->m.c.uvlinesize) + (mb_x << bs + ctx->is_444);
     const uint8_t *ptr_v = ctx->thread[0]->src[2] +
-                           ((mb_y << 4) * ctx->m.c.uvlinesize) + (mb_x << bs + ctx->is_444);
+                           ((mb_y << 4) * ctx->vlinesize) + (mb_x << bs + ctx->is_444);
     PixblockDSPContext *pdsp = &ctx->m.pdsp;
     VideoDSPContext *vdsp = &ctx->m.c.vdsp;

@@ -707,12 +747,14 @@ void dnxhd_get_blocks(DNXHDEncContext *ctx, int mb_x, int mb_y)
                                uvlinesize, 16,
                                0, 0, uv_w, uv_h);
         vdsp->emulated_edge_mc(&ctx->edge_buf_uv[1][0], ptr_v,
-                               uvlinesize, ctx->m.c.uvlinesize,
+                               uvlinesize, ctx->vlinesize,
                                uvlinesize, 16,
                                0, 0, uv_w, uv_h);

         dct_y_offset =  bw * linesize;
         dct_uv_offset = bw * uvlinesize;
+        dct_v_offset  = dct_uv_offset;
+        vlinesize     = uvlinesize;
         ptr_y = &ctx->edge_buf_y[0];
         ptr_u = &ctx->edge_buf_uv[0][0];
         ptr_v = &ctx->edge_buf_uv[1][0];
@@ -734,12 +776,14 @@ void dnxhd_get_blocks(DNXHDEncContext *ctx, int mb_x, int mb_y)
                                uvlinesize / 2, 16,
                                0, 0, uv_w, uv_h);
         vdsp->emulated_edge_mc(&ctx->edge_buf_uv[1][0], ptr_v,
-                               uvlinesize, ctx->m.c.uvlinesize,
+                               uvlinesize, ctx->vlinesize,
                                uvlinesize / 2, 16,
                                0, 0, uv_w, uv_h);

         dct_y_offset =  bw * linesize / 2;
         dct_uv_offset = bw * uvlinesize / 2;
+        dct_v_offset  = dct_uv_offset;
+        vlinesize     = uvlinesize;
         ptr_y = &ctx->edge_buf_y[0];
         ptr_u = &ctx->edge_buf_uv[0][0];
         ptr_v = &ctx->edge_buf_uv[1][0];
@@ -749,7 +793,7 @@ void dnxhd_get_blocks(DNXHDEncContext *ctx, int mb_x, int mb_y)
         pdsp->get_pixels(ctx->blocks[0], ptr_y,      linesize);
         pdsp->get_pixels(ctx->blocks[1], ptr_y + bw, linesize);
         pdsp->get_pixels(ctx->blocks[2], ptr_u,      uvlinesize);
-        pdsp->get_pixels(ctx->blocks[3], ptr_v,      uvlinesize);
+        pdsp->get_pixels(ctx->blocks[3], ptr_v,      vlinesize);

         if (mb_y + 1 == ctx->m.c.mb_height && ctx->m.c.avctx->height == 1080) {
             if (ctx->interlaced) {
@@ -763,8 +807,8 @@ void dnxhd_get_blocks(DNXHDEncContext *ctx, int mb_x, int mb_y)
                                         ptr_u + dct_uv_offset,
                                         uvlinesize);
                 ctx->get_pixels_8x4_sym(ctx->blocks[7],
-                                        ptr_v + dct_uv_offset,
-                                        uvlinesize);
+                                        ptr_v + dct_v_offset,
+                                        vlinesize);
             } else {
                 ctx->m.c.bdsp.clear_block(ctx->blocks[4]);
                 ctx->m.c.bdsp.clear_block(ctx->blocks[5]);
@@ -779,7 +823,7 @@ void dnxhd_get_blocks(DNXHDEncContext *ctx, int mb_x, int mb_y)
             pdsp->get_pixels(ctx->blocks[6],
                              ptr_u + dct_uv_offset, uvlinesize);
             pdsp->get_pixels(ctx->blocks[7],
-                             ptr_v + dct_uv_offset, uvlinesize);
+                             ptr_v + dct_v_offset, vlinesize);
         }
     } else {
         pdsp->get_pixels(ctx->blocks[0], ptr_y,      linesize);
@@ -792,10 +836,10 @@ void dnxhd_get_blocks(DNXHDEncContext *ctx, int mb_x, int mb_y)
         pdsp->get_pixels(ctx->blocks[8], ptr_u + dct_uv_offset, uvlinesize);
         pdsp->get_pixels(ctx->blocks[9], ptr_u + dct_uv_offset + bw, uvlinesize);

-        pdsp->get_pixels(ctx->blocks[4], ptr_v,      uvlinesize);
-        pdsp->get_pixels(ctx->blocks[5], ptr_v + bw, uvlinesize);
-        pdsp->get_pixels(ctx->blocks[10], ptr_v + dct_uv_offset, uvlinesize);
-        pdsp->get_pixels(ctx->blocks[11], ptr_v + dct_uv_offset + bw, uvlinesize);
+        pdsp->get_pixels(ctx->blocks[4], ptr_v,      vlinesize);
+        pdsp->get_pixels(ctx->blocks[5], ptr_v + bw, vlinesize);
+        pdsp->get_pixels(ctx->blocks[10], ptr_v + dct_v_offset, vlinesize);
+        pdsp->get_pixels(ctx->blocks[11], ptr_v + dct_v_offset + bw, vlinesize);
     }
 }

@@ -889,7 +933,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);

@@ -962,10 +1006,11 @@ static int dnxhd_mb_var_thread(AVCodecContext *avctx, void *arg,
             ctx->mb_cmp[mb].mb    = mb;
         }
     } else { // 10-bit
-        const int linesize = ctx->m.c.linesize >> 1;
+        const int linesize = (ctx->rgb ? ctx->m.c.uvlinesize : ctx->m.c.linesize) / 2;
+        /* Keep using green for RGB variance, independently of coding order. */
+        const uint16_t *plane = (const uint16_t *)ctx->thread[0]->src[ctx->rgb ? 1 : 0];
         for (int mb_x = 0; mb_x < ctx->m.c.mb_width; ++mb_x) {
-            const uint16_t *pix = (const uint16_t *)ctx->thread[0]->src[0] +
-                                     ((mb_y << 4) * linesize) + (mb_x << 4);
+            const uint16_t *pix = plane + ((mb_y << 4) * linesize) + (mb_x << 4);
             unsigned mb  = mb_y * ctx->m.c.mb_width + mb_x;
             int sum = 0;
             int sqsum = 0;
@@ -1229,11 +1274,19 @@ 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;
+    const int field_scale = 1 << ctx->interlaced;
+
     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]] * field_scale;
+        ctx->thread[i]->m.c.uvlinesize  = frame->linesize[planes[1]] * field_scale;
+        ctx->thread[i]->vlinesize       = frame->linesize[planes[2]] * field_scale;
         ctx->thread[i]->dct_y_offset    = ctx->m.c.linesize  *8;
         ctx->thread[i]->dct_uv_offset   = ctx->m.c.uvlinesize*8;
     }
@@ -1258,9 +1311,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 : yuv_planes)[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 7540607cdc..cfd2746e66 100644
--- a/libavcodec/dnxhdenc.h
+++ b/libavcodec/dnxhdenc.h
@@ -49,6 +49,9 @@ 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
+    int clv;  ///< color volume
     const CIDEntry *cid_table;
     uint8_t *msip; ///< Macroblock Scan Indexes Payload
     uint32_t *slice_size;
@@ -60,6 +63,7 @@ typedef struct DNXHDEncContext {
     // encoding respectively, these refer either to bytes or to two-byte words.
     unsigned dct_y_offset;
     unsigned dct_uv_offset;
+    int vlinesize;             ///< third plane, may differ from uvlinesize
     unsigned block_width_l2;

     int frame_size;
diff --git a/libavformat/mxfenc.c b/libavformat/mxfenc.c
index 15af21f41b..fb89638cf3 100644
--- a/libavformat/mxfenc.c
+++ b/libavformat/mxfenc.c
@@ -2504,12 +2504,12 @@ static int mxf_parse_dnxhd_frame(AVFormatContext *s, AVStream *st, AVPacket *pkt
 {
     MXFContext *mxf = s->priv_data;
     MXFStreamContext *sc = st->priv_data;
-    int i;
+    int i, clv, rgb;

     if (mxf->header_written)
         return 1;

-    if (pkt->size < 43)
+    if (pkt->size < 0x2d)
         return 0;

     sc->cid = AV_RB32(pkt->data + 0x28);
@@ -2533,9 +2533,26 @@ static int mxf_parse_dnxhd_frame(AVFormatContext *s, AVStream *st, AVPacket *pkt

     sc->frame_size = pkt->size;

-    if (sc->cid == 1270 || sc->cid == 1256)
+    /* RGB color format with 4:4:4 sampling */
+    rgb = (sc->cid == 1270 || sc->cid == 1256) && (pkt->data[0x2c] & 0x41) == 0x41;
+    if (rgb)
         sc->picture_descriptor_key = &mxf_rgba_descriptor_key;

+    clv = (pkt->data[0x2c] >> 1) & 3;
+    if (clv < 3) { // in-band color volume, video range
+        static const enum AVColorSpace clv_spaces[3] = {
+            AVCOL_SPC_BT709, AVCOL_SPC_BT2020_NCL, AVCOL_SPC_BT2020_CL,
+        };
+        AVCodecParameters *par = st->codecpar;
+        /* older encoders wrote CLV=0 for any primaries */
+        if (clv && par->color_primaries == AVCOL_PRI_UNSPECIFIED)
+            par->color_primaries = AVCOL_PRI_BT2020;
+        if (par->color_space == AVCOL_SPC_UNSPECIFIED)
+            par->color_space = rgb ? AVCOL_SPC_RGB : clv_spaces[clv];
+        if (rgb && par->color_range == AVCOL_RANGE_UNSPECIFIED)
+            par->color_range = AVCOL_RANGE_MPEG;
+    }
+
     return 1;
 }

diff --git a/tests/ref/lavf/mxf_opatom b/tests/ref/lavf/mxf_opatom
index 56c2a7d8c7..6184ac980f 100644
--- a/tests/ref/lavf/mxf_opatom
+++ b/tests/ref/lavf/mxf_opatom
@@ -1,3 +1,3 @@
-72f2f998abb3a51a5ab7adec6ed2b9a3 *tests/data/lavf/lavf.mxf_opatom
+1be9e9771694cbee20ec9426d348f71f *tests/data/lavf/lavf.mxf_opatom
 4717625 tests/data/lavf/lavf.mxf_opatom
 tests/data/lavf/lavf.mxf_opatom CRC=0xb13ba2f8