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

Since 9a7686e545, encoder options are applied before `enc_open()` copies the 
first frame's colour properties, so `-color_primaries` and `-color_trc` were 
silently replaced by the frame tags when re-encoding. `-colorspace` and 
`-color_range` are not affected, as the filter graph negotiates them.

Restore the earlier behaviour for primaries and transfer: use the frame value 
only when the option is unset, as suggested in #20736. Like before 7.1, the 
options only tag the output; nothing is converted.

New FATE test `fate-ffmpeg-color-tags`: frames tagged BT.709 encoded with 
`-color_primaries bt2020 -color_trc smpte2084`. It fails without the fix (bt709 
is written) and passes with it. Full FATE passes.

Fixes #20736

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



From 80cc3ebdaf95cfb9a898e09a365bfc39fdfcf2e1 Mon Sep 17 00:00:00 2001
From: Baptiste Coudurier <[email protected]>
Date: Thu, 12 Nov 2026 15:54:25 -0800
Subject: [PATCH] fftools/ffmpeg_enc: honor user set color primaries and
 transfer

They are not negotiated by the filter graph, so the frame tags overrode them.

Fixes: #20736
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Baptiste Coudurier <[email protected]>
---
 fftools/ffmpeg_enc.c             | 12 ++++++++----
 tests/fate/ffmpeg.mak            |  8 ++++++++
 tests/ref/fate/ffmpeg-color-tags | 11 +++++++++++
 3 files changed, 27 insertions(+), 4 deletions(-)
 create mode 100644 tests/ref/fate/ffmpeg-color-tags

diff --git a/fftools/ffmpeg_enc.c b/fftools/ffmpeg_enc.c
index 37e67b78cef..49c6a8fb0bc 100644
--- a/fftools/ffmpeg_enc.c
+++ b/fftools/ffmpeg_enc.c
@@ -330,16 +330,20 @@ static int enc_reopen(void *opaque, const AVFrame *frame,
                                                  
av_pix_fmt_desc_get(enc_ctx->pix_fmt)->comp[0].depth);
 
         /**
-         * The video color properties should always be in sync with the user-
-         * requested values, since we forward them to the filter graph.
+         * These video color properties should always be in sync with the
+         * user-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;
 
+        /* Not part of filter graph negotiation; a user value only retags */
+        if (enc_ctx->color_primaries == AVCOL_PRI_UNSPECIFIED)
+            enc_ctx->color_primaries = frame->color_primaries;
+        if (enc_ctx->color_trc == AVCOL_TRC_UNSPECIFIED)
+            enc_ctx->color_trc = frame->color_trc;
+
         if (enc_ctx->flags & (AV_CODEC_FLAG_INTERLACED_DCT | 
AV_CODEC_FLAG_INTERLACED_ME) ||
             (frame->flags & AV_FRAME_FLAG_INTERLACED)) {
             int top_field_first = !!(frame->flags & 
AV_FRAME_FLAG_TOP_FIELD_FIRST);
diff --git a/tests/fate/ffmpeg.mak b/tests/fate/ffmpeg.mak
index b46eff0cf3b..2899ee0045f 100644
--- a/tests/fate/ffmpeg.mak
+++ b/tests/fate/ffmpeg.mak
@@ -19,6 +19,14 @@ fate-ffmpeg-lavfi: CMD = framecrc -lavfi color=d=1:r=5 
-fflags +bitexact
 FATE_FFMPEG-$(call FILTERFRAMECRC, TESTSRC2 FORMAT SETPARAMS SCALE, 
LAVFI_INDEV WRAPPED_AVFRAME_DECODER MPEG2VIDEO_ENCODER) += 
fate-ffmpeg-chroma-location
 fate-ffmpeg-chroma-location: CMD = framecrc -auto_conversion_filters -f lavfi 
-i testsrc2=s=64x64:r=25:d=1,format=yuv420p,setparams=chroma_location=topleft 
-sws_flags +accurate_rnd+bitexact -flags +bitexact -idct simple -dct fastint 
-threads 1 -c:v mpeg2video -qscale:v 2 -frames:v 1
 
+# -color_primaries and -color_trc are not negotiated by the filter graph; they
+# must override the frame tags instead of being replaced by them
+FATE_FFMPEG_FFPROBE-$(call TRANSCODE, MPEG2VIDEO, MPEG2VIDEO MPEGVIDEO, 
LAVFI_INDEV WRAPPED_AVFRAME_DECODER TESTSRC2_FILTER FORMAT_FILTER 
SETPARAMS_FILTER) += fate-ffmpeg-color-tags
+fate-ffmpeg-color-tags: 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_primaries bt2020 
-color_trc smpte2084" "-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 00000000000..423784cc824
--- /dev/null
+++ b/tests/ref/fate/ffmpeg-color-tags
@@ -0,0 +1,11 @@
+d7187cbb9616f63309cd0040b89dd502 *tests/data/fate/ffmpeg-color-tags.mpeg2video
+2760 tests/data/fate/ffmpeg-color-tags.mpeg2video
+#extradata 0:       34, 0x89d50657
+#tb 0: 1/1200000
+#media_type 0: video
+#codec_id 0: mpeg2video
+#dimensions 0: 64x64
+#sar 0: 1/1
+0,          0, -9223372036854775808,    48000,     2760, 0x3a74e974
+color_transfer=smpte2084
+color_primaries=bt2020
-- 
2.54.0

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

Reply via email to