Commit 201bb8737d for ffmpeg

commit 201bb8737db0f372f190893e3fe837630a302648
Author: Baptiste Coudurier <baptiste.coudurier@gmail.com>
Date:   Thu Nov 12 15:54:25 2026 -0800

    fftools/ffmpeg_enc: honor user-set color primaries and transfer

    Take the frame tags before applying encoder options, so explicit
    -color_primaries and -color_trc are no longer overwritten.

    Fixes: #20736

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

diff --git a/fftools/ffmpeg_enc.c b/fftools/ffmpeg_enc.c
index 37e67b78ce..3094950372 100644
--- a/fftools/ffmpeg_enc.c
+++ b/fftools/ffmpeg_enc.c
@@ -244,6 +244,12 @@ static int enc_reopen(void *opaque, const AVFrame *frame,
     if (ret < 0)
         return ret;

+    // Use frame tags as defaults for properties the output filter does not negotiate.
+    if (frame && enc->type == AVMEDIA_TYPE_VIDEO) {
+        enc_ctx->color_primaries = frame->color_primaries;
+        enc_ctx->color_trc       = frame->color_trc;
+    }
+
     threads_manual = !!av_dict_get(encoder_opts, "threads", NULL, 0);
     ret = apply_enc_options(e, &encoder_opts);
     av_dict_free(&encoder_opts);
@@ -334,8 +340,6 @@ static int enc_reopen(void *opaque, const AVFrame *frame,
          * requested values, since we forward them to the filter graph.
          */
         enc_ctx->color_range            = frame->color_range;
-        enc_ctx->color_primaries        = frame->color_primaries;
-        enc_ctx->color_trc              = frame->color_trc;
         enc_ctx->colorspace             = frame->colorspace;
         enc_ctx->alpha_mode             = frame->alpha_mode;
         enc_ctx->chroma_sample_location = frame->chroma_location;
diff --git a/tests/fate/ffmpeg.mak b/tests/fate/ffmpeg.mak
index 63b1f214eb..9a21550341 100644
--- a/tests/fate/ffmpeg.mak
+++ b/tests/fate/ffmpeg.mak
@@ -22,6 +22,21 @@ fate-ffmpeg-chroma-location: CMD = framecrc -auto_conversion_filters -f lavfi -i
 FATE_FFMPEG-$(call FILTERFRAMECRC, TESTSRC2 FORMAT, LAVFI_INDEV WRAPPED_AVFRAME_DECODER SPEEDHQ_ENCODER) += fate-ffmpeg-speedhq-444-psnr
 fate-ffmpeg-speedhq-444-psnr: CMD = framecrc -f lavfi -i testsrc2=s=128x64:r=25:d=1,format=yuv444p -flags +bitexact+psnr -idct simple -dct fastint -threads 1 -c:v speedhq -qscale:v 3 -frames:v 1

+# Explicit -color_primaries and -color_trc override the frame defaults in the encoder context.
+FATE_FFMPEG_FFPROBE-$(call REMUX, MPEG2VIDEO MPEGVIDEO, \
+    MPEG2VIDEO_ENCODER LAVFI_INDEV WRAPPED_AVFRAME_DECODER TESTSRC2_FILTER \
+    FORMAT_FILTER SETPARAMS_FILTER MPEG1VIDEO_DECODER MPEGVIDEO_PARSER \
+    EXTRACT_EXTRADATA_BSF) += \
+    fate-ffmpeg-color-tags fate-ffmpeg-color-tags-primaries fate-ffmpeg-color-tags-unknown
+fate-ffmpeg-color-tags:           COLOR_OPTS = -color_primaries bt470bg -color_trc linear
+fate-ffmpeg-color-tags-primaries: COLOR_OPTS = -color_primaries bt470bg
+# Keep the colour description present to check the explicit unspecified codes.
+fate-ffmpeg-color-tags-unknown:   COLOR_OPTS = -color_primaries unknown -color_trc unknown -seq_disp_ext always
+fate-ffmpeg-color-tags fate-ffmpeg-color-tags-primaries fate-ffmpeg-color-tags-unknown: CMD = transcode lavfi "testsrc2=s=64x64:r=25:d=0.04,format=yuv420p,setparams=color_primaries=bt709:color_trc=bt709" mpeg2video \
+    "-c:v mpeg2video -qscale:v 2 -frames:v 1 $(COLOR_OPTS)" "-c copy" \
+    "-show_entries stream=color_primaries,color_transfer -of default=nw=1"
+FATE_FFMPEG_FFPROBE += $(FATE_FFMPEG_FFPROBE-yes)
+
 FATE_FFMPEG-$(if $(CONFIG_SMALL),no,$(call ALLYES, TESTSRC2_FILTER SCALE_FILTER FORMAT_FILTER LAVFI_INDEV WRAPPED_AVFRAME_DECODER RAWVIDEO_ENCODER NULL_MUXER)) += fate-ffmpeg-print-graphs-compact
 fate-ffmpeg-print-graphs-compact: CMD = ffmpeg -f lavfi -i testsrc2=s=64x64:r=25:d=0.08 -filter_complex "[0:v]scale=32:32,format=yuv420p[v]" -map "[v]" -c:v rawvideo -print_graphs_file - -print_graphs_format compact -f null -

diff --git a/tests/ref/fate/ffmpeg-color-tags b/tests/ref/fate/ffmpeg-color-tags
new file mode 100644
index 0000000000..8619bd0b10
--- /dev/null
+++ b/tests/ref/fate/ffmpeg-color-tags
@@ -0,0 +1,11 @@
+2db1ebde9012eca6c8f07a780d2d17e3 *tests/data/fate/ffmpeg-color-tags.mpeg2video
+2760 tests/data/fate/ffmpeg-color-tags.mpeg2video
+#extradata 0:       34, 0x8989064b
+#tb 0: 1/1200000
+#media_type 0: video
+#codec_id 0: mpeg2video
+#dimensions 0: 64x64
+#sar 0: 1/1
+0,          0, -9223372036854775808,    48000,     2760, 0xba51e968
+color_transfer=linear
+color_primaries=bt470bg
diff --git a/tests/ref/fate/ffmpeg-color-tags-primaries b/tests/ref/fate/ffmpeg-color-tags-primaries
new file mode 100644
index 0000000000..f9f6a56188
--- /dev/null
+++ b/tests/ref/fate/ffmpeg-color-tags-primaries
@@ -0,0 +1,11 @@
+3d09aaa84b96113bea89c070feb9591d *tests/data/fate/ffmpeg-color-tags-primaries.mpeg2video
+2760 tests/data/fate/ffmpeg-color-tags-primaries.mpeg2video
+#extradata 0:       34, 0x895f0644
+#tb 0: 1/1200000
+#media_type 0: video
+#codec_id 0: mpeg2video
+#dimensions 0: 64x64
+#sar 0: 1/1
+0,          0, -9223372036854775808,    48000,     2760, 0x6f9de961
+color_transfer=bt709
+color_primaries=bt470bg
diff --git a/tests/ref/fate/ffmpeg-color-tags-unknown b/tests/ref/fate/ffmpeg-color-tags-unknown
new file mode 100644
index 0000000000..84cf9c5648
--- /dev/null
+++ b/tests/ref/fate/ffmpeg-color-tags-unknown
@@ -0,0 +1,11 @@
+615e34ec9ea35c28f5acbb846d7e9f6b *tests/data/fate/ffmpeg-color-tags-unknown.mpeg2video
+2760 tests/data/fate/ffmpeg-color-tags-unknown.mpeg2video
+#extradata 0:       34, 0x89500642
+#tb 0: 1/1200000
+#media_type 0: video
+#codec_id 0: mpeg2video
+#dimensions 0: 64x64
+#sar 0: 1/1
+0,          0, -9223372036854775808,    48000,     2760, 0x5a42e95f
+color_transfer=unknown
+color_primaries=unknown