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

The ProRes frame header can only carry the colour codes listed in SMPTE RDD 36 
(Tables 5 and 6). The encoders didn't follow that list:

- prores_ks (and the Vulkan encoder) wrote the FFmpeg enum values unchecked, so 
reserved codes ended up in the header: e.g. trc bt2020-10 as 14, matrix bt470bg 
as 5, primaries smpte240m as 7.
- prores_aw dropped tags that have a valid equivalent: trc 
smpte170m/bt2020-10/bt2020-12 and matrix bt470bg were written as 0 (unknown).

A shared helper in proresdata.c now maps transfer smpte170m/bt2020-10/bt2020-12 
to 1, matrix bt470bg to 6, and any other value outside RDD 36 to 2 
(unspecified). Both encoders use it.

Tested by encoding a frame for each primaries, transfer and matrix value with 
both encoders and reading the header bytes back; all are now RDD 36 codes. New 
FATE tests fate-prores-aw-color-tags and fate-prores-ks-color-tags; all prores 
FATE tests pass. The Vulkan encoder change was not compiled (no Vulkan in this 
build).

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



From 857ced451042e9fa8ae0631e86eaf5fd95cff492 Mon Sep 17 00:00:00 2001
From: Baptiste Coudurier <[email protected]>
Date: Thu, 12 Nov 2026 15:57:48 -0800
Subject: [PATCH] avcodec/proresenc: write only RDD 36 colour codes in the
 frame header

prores_ks wrote reserved codes as-is and prores_aw dropped equivalent ones;
map bt470bg matrix and bt601/bt2020 transfers to their codes, others to 
unspecified.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Baptiste Coudurier <[email protected]>
---
 libavcodec/proresdata.c              | 51 ++++++++++++++++++++++++++++
 libavcodec/proresdata.h              | 10 ++++++
 libavcodec/proresenc_anatoliy.c      | 46 ++-----------------------
 libavcodec/proresenc_kostya.c        |  6 ++--
 libavcodec/proresenc_kostya_common.c |  6 ++--
 libavcodec/proresenc_kostya_vulkan.c |  6 ++--
 tests/fate/prores.mak                | 13 +++++++
 tests/ref/fate/prores-aw-color-tags  | 11 ++++++
 tests/ref/fate/prores-ks-color-tags  | 11 ++++++
 9 files changed, 108 insertions(+), 52 deletions(-)
 create mode 100644 tests/ref/fate/prores-aw-color-tags
 create mode 100644 tests/ref/fate/prores-ks-color-tags

diff --git a/libavcodec/proresdata.c b/libavcodec/proresdata.c
index 00af97d4f17..d386008f9d4 100644
--- a/libavcodec/proresdata.c
+++ b/libavcodec/proresdata.c
@@ -51,3 +51,54 @@ const uint8_t ff_prores_run_to_cb[16] = { 0x06, 0x06, 0x05, 
0x05, 0x04, 0x29,
 
 const uint8_t ff_prores_level_to_cb[10] = { 0x04, 0x0A, 0x05, 0x06, 0x04, 0x28,
         0x28, 0x28, 0x28, 0x4C };
+
+int ff_prores_color_primaries(enum AVColorPrimaries color_primaries)
+{
+    switch (color_primaries) {
+    case AVCOL_PRI_RESERVED0:
+    case AVCOL_PRI_BT709:
+    case AVCOL_PRI_UNSPECIFIED:
+    case AVCOL_PRI_BT470BG:
+    case AVCOL_PRI_SMPTE170M:
+    case AVCOL_PRI_BT2020:
+    case AVCOL_PRI_SMPTE431:
+    case AVCOL_PRI_SMPTE432:
+        return color_primaries;
+    default:
+        return AVCOL_PRI_UNSPECIFIED;
+    }
+}
+
+int ff_prores_color_trc(enum AVColorTransferCharacteristic color_trc)
+{
+    switch (color_trc) {
+    case AVCOL_TRC_RESERVED0:
+    case AVCOL_TRC_BT709:
+    case AVCOL_TRC_UNSPECIFIED:
+    case AVCOL_TRC_SMPTE2084:
+    case AVCOL_TRC_ARIB_STD_B67:
+        return color_trc;
+    case AVCOL_TRC_SMPTE170M:
+    case AVCOL_TRC_BT2020_10:
+    case AVCOL_TRC_BT2020_12:
+        return AVCOL_TRC_BT709;
+    default:
+        return AVCOL_TRC_UNSPECIFIED;
+    }
+}
+
+int ff_prores_colorspace(enum AVColorSpace colorspace)
+{
+    switch (colorspace) {
+    case AVCOL_SPC_RGB:
+    case AVCOL_SPC_BT709:
+    case AVCOL_SPC_UNSPECIFIED:
+    case AVCOL_SPC_SMPTE170M:
+    case AVCOL_SPC_BT2020_NCL:
+        return colorspace;
+    case AVCOL_SPC_BT470BG:
+        return AVCOL_SPC_SMPTE170M;
+    default:
+        return AVCOL_SPC_UNSPECIFIED;
+    }
+}
diff --git a/libavcodec/proresdata.h b/libavcodec/proresdata.h
index d859e93e88b..1b727d26263 100644
--- a/libavcodec/proresdata.h
+++ b/libavcodec/proresdata.h
@@ -25,6 +25,8 @@
 
 #include <stdint.h>
 
+#include "libavutil/pixfmt.h"
+
 #define FRAME_ID MKBETAG('i', 'c', 'p', 'f')
 
 extern const uint8_t ff_prores_progressive_scan[64];
@@ -35,4 +37,12 @@ extern const uint8_t ff_prores_level_to_cb[10];
 
 #define FIRST_DC_CB 0xB8 // rice_order = 5, exp_golomb_order = 6, switch_bits 
= 0
 
+/**
+ * Map color properties to the codes a frame header can carry (SMPTE RDD 36),
+ * substituting an equivalent code or unspecified for the others.
+ */
+int ff_prores_color_primaries(enum AVColorPrimaries color_primaries);
+int ff_prores_color_trc(enum AVColorTransferCharacteristic color_trc);
+int ff_prores_colorspace(enum AVColorSpace colorspace);
+
 #endif /* AVCODEC_PRORESDATA_H */
diff --git a/libavcodec/proresenc_anatoliy.c b/libavcodec/proresenc_anatoliy.c
index d8c5a483004..2cbbfdceb0d 100644
--- a/libavcodec/proresenc_anatoliy.c
+++ b/libavcodec/proresenc_anatoliy.c
@@ -56,13 +56,6 @@ static const int qp_start_table[] = {  8, 3, 2, 1, 1, 1};
 static const int qp_end_table[]   = { 13, 9, 6, 6, 5, 4};
 static const int bitrate_table[]  = { 1000, 2100, 3500, 5400, 7000, 10000};
 
-static const int valid_primaries[]  = { AVCOL_PRI_RESERVED0, AVCOL_PRI_BT709, 
AVCOL_PRI_UNSPECIFIED, AVCOL_PRI_BT470BG,
-                                        AVCOL_PRI_SMPTE170M, AVCOL_PRI_BT2020, 
AVCOL_PRI_SMPTE431, AVCOL_PRI_SMPTE432, INT_MAX };
-static const int valid_trc[]        = { AVCOL_TRC_RESERVED0, AVCOL_TRC_BT709, 
AVCOL_TRC_UNSPECIFIED, AVCOL_TRC_SMPTE2084,
-                                        AVCOL_TRC_ARIB_STD_B67, INT_MAX };
-static const int valid_colorspace[] = { AVCOL_SPC_BT709, 
AVCOL_SPC_UNSPECIFIED, AVCOL_SPC_SMPTE170M,
-                                        AVCOL_SPC_BT2020_NCL, INT_MAX };
-
 static const uint8_t QMAT_LUMA[6][64] = {
     {
          4,  7,  9, 11, 13, 14, 15, 63,
@@ -199,35 +192,6 @@ typedef struct {
     char *vendor;
 } ProresContext;
 
-/**
- * Check if a value is in the list. If not, return the default value
- *
- * @param ctx                Context for the log msg
- * @param val_name           Name of the checked value, for log msg
- * @param array_valid_values Array of valid int, ended with INT_MAX
- * @param default_value      Value return if checked value is not in the array
- * @return                   Value or default_value.
- */
-static int int_from_list_or_default(void *ctx, const char *val_name, int val,
-                                    const int *array_valid_values, int 
default_value)
-{
-    int i = 0;
-
-    while (1) {
-        int ref_val = array_valid_values[i];
-        if (ref_val == INT_MAX)
-            break;
-        if (val == ref_val)
-            return val;
-        i++;
-    }
-    /* val is not a valid value */
-    av_log(ctx, AV_LOG_DEBUG,
-           "%s %d are not supported. Set to default value : %d\n",
-           val_name, val, default_value);
-    return default_value;
-}
-
 static void encode_vlc_codeword(PutBitContext *pb, unsigned codebook, int val)
 {
     unsigned int rice_order, exp_order, switch_bits, switch_val;
@@ -769,13 +733,9 @@ static int prores_encode_frame(AVCodecContext *avctx, 
AVPacket *pkt,
     }
     *buf++ = frame_flags;
     *buf++ = 0; /* reserved */
-    /* only write color properties, if valid value. set to unspecified 
otherwise */
-    *buf++ = int_from_list_or_default(avctx, "frame color primaries",
-                                      pict->color_primaries, valid_primaries, 
0);
-    *buf++ = int_from_list_or_default(avctx, "frame color trc",
-                                      pict->color_trc, valid_trc, 0);
-    *buf++ = int_from_list_or_default(avctx, "frame colorspace",
-                                      pict->colorspace, valid_colorspace, 0);
+    *buf++ = ff_prores_color_primaries(pict->color_primaries);
+    *buf++ = ff_prores_color_trc(pict->color_trc);
+    *buf++ = ff_prores_colorspace(pict->colorspace);
     *buf++ = ctx->need_alpha ? 0x2 /* 16-bit alpha */ : 0;
     *buf++ = 0; /* reserved */
     *buf++ = 3; /* luma and chroma matrix present */
diff --git a/libavcodec/proresenc_kostya.c b/libavcodec/proresenc_kostya.c
index bf7c4bda43a..944a6113ef1 100644
--- a/libavcodec/proresenc_kostya.c
+++ b/libavcodec/proresenc_kostya.c
@@ -792,9 +792,9 @@ static int encode_frame(AVCodecContext *avctx, AVPacket 
*pkt,
     bytestream_put_byte  (&buf, frame_flags);
 
     bytestream_put_byte  (&buf, 0);             // reserved
-    bytestream_put_byte  (&buf, pic->color_primaries);
-    bytestream_put_byte  (&buf, pic->color_trc);
-    bytestream_put_byte  (&buf, pic->colorspace);
+    bytestream_put_byte  (&buf, 
ff_prores_color_primaries(pic->color_primaries));
+    bytestream_put_byte  (&buf, ff_prores_color_trc(pic->color_trc));
+    bytestream_put_byte  (&buf, ff_prores_colorspace(pic->colorspace));
     bytestream_put_byte  (&buf, ctx->alpha_bits >> 3);
     bytestream_put_byte  (&buf, 0);             // reserved
     if (ctx->quant_sel != QUANT_MAT_DEFAULT) {
diff --git a/libavcodec/proresenc_kostya_common.c 
b/libavcodec/proresenc_kostya_common.c
index 173268fff84..4ae5c23bbe2 100644
--- a/libavcodec/proresenc_kostya_common.c
+++ b/libavcodec/proresenc_kostya_common.c
@@ -347,9 +347,9 @@ uint8_t *ff_prores_kostya_write_frame_header(AVCodecContext 
*avctx, ProresContex
     bytestream_put_byte  (&buf, frame_flags);
 
     bytestream_put_byte  (&buf, 0);             // reserved
-    bytestream_put_byte  (&buf, color_primaries);
-    bytestream_put_byte  (&buf, color_trc);
-    bytestream_put_byte  (&buf, colorspace);
+    bytestream_put_byte  (&buf, ff_prores_color_primaries(color_primaries));
+    bytestream_put_byte  (&buf, ff_prores_color_trc(color_trc));
+    bytestream_put_byte  (&buf, ff_prores_colorspace(colorspace));
     bytestream_put_byte  (&buf, ctx->alpha_bits >> 3);
     bytestream_put_byte  (&buf, 0);             // reserved
     if (ctx->quant_sel != QUANT_MAT_DEFAULT) {
diff --git a/libavcodec/proresenc_kostya_vulkan.c 
b/libavcodec/proresenc_kostya_vulkan.c
index 8fa0d7f1289..7ae24949627 100644
--- a/libavcodec/proresenc_kostya_vulkan.c
+++ b/libavcodec/proresenc_kostya_vulkan.c
@@ -621,9 +621,9 @@ static uint8_t *write_frame_header(AVCodecContext *avctx, 
ProresContext *ctx,
     bytestream_put_byte  (&buf, frame_flags);
 
     bytestream_put_byte  (&buf, 0);             // reserved
-    bytestream_put_byte  (&buf, color_primaries);
-    bytestream_put_byte  (&buf, color_trc);
-    bytestream_put_byte  (&buf, colorspace);
+    bytestream_put_byte  (&buf, ff_prores_color_primaries(color_primaries));
+    bytestream_put_byte  (&buf, ff_prores_color_trc(color_trc));
+    bytestream_put_byte  (&buf, ff_prores_colorspace(colorspace));
     bytestream_put_byte  (&buf, ctx->alpha_bits >> 3);
     bytestream_put_byte  (&buf, 0);             // reserved
     if (ctx->quant_sel != QUANT_MAT_DEFAULT) {
diff --git a/tests/fate/prores.mak b/tests/fate/prores.mak
index 7bc7d7de873..244cbc29acc 100644
--- a/tests/fate/prores.mak
+++ b/tests/fate/prores.mak
@@ -40,5 +40,18 @@ fate-prores-color-tags-fallback: CMD = run 
ffprobe$(PROGSSUF)$(EXESUF) -v error
     -show_entries frame=color_primaries,color_transfer,color_space -of 
default=nw=1 \
     $(TARGET_SAMPLES)/prores/color-tags-fallback.mov
 
+# Frame tags without a ProRes code get an equivalent code or unspecified in 
the frame header.
+# The decoder is told the container tags are unspecified so the decoded tags 
come from the frame header.
+PRORES_COLOR_TAGS_SRC = 
"testsrc2=s=64x64:d=0.04,setparams=color_primaries=smpte240m:color_trc=bt2020-10:colorspace=bt470bg,format=yuv422p10"
+PRORES_COLOR_TAGS_PROBE = "-color_primaries unknown -color_trc unknown 
-colorspace unknown -show_entries 
frame=color_primaries,color_transfer,color_space -of default=nw=1"
+FATE_PRORES_COLOR_TAGS-$(call TRANSCODE, PRORES_AW PRORES, MOV, LAVFI_INDEV 
WRAPPED_AVFRAME_DECODER TESTSRC2_FILTER SETPARAMS_FILTER FORMAT_FILTER) += 
fate-prores-aw-color-tags
+fate-prores-aw-color-tags: CMD = transcode lavfi $(PRORES_COLOR_TAGS_SRC) mov \
+    "-c:v prores_aw -frames:v 1" "-c:v copy" $(PRORES_COLOR_TAGS_PROBE)
+FATE_PRORES_COLOR_TAGS-$(call TRANSCODE, PRORES_KS PRORES, MOV, LAVFI_INDEV 
WRAPPED_AVFRAME_DECODER TESTSRC2_FILTER SETPARAMS_FILTER FORMAT_FILTER) += 
fate-prores-ks-color-tags
+fate-prores-ks-color-tags: CMD = transcode lavfi $(PRORES_COLOR_TAGS_SRC) mov \
+    "-c:v prores_ks -frames:v 1" "-c:v copy" $(PRORES_COLOR_TAGS_PROBE)
+FATE_FFMPEG_FFPROBE += $(FATE_PRORES_COLOR_TAGS-yes)
+fate-prores-color-tags: $(FATE_PRORES_COLOR_TAGS-yes)
+
 FATE_SAMPLES_FFMPEG-$(call DEMMUX, MOV, MOV, PRORES_METADATA_BSF) += 
$(FATE_PRORES_METADATA_BSF)
 FATE_SAMPLES_FFPROBE += $(FATE_PRORES_FFPROBE-yes)
diff --git a/tests/ref/fate/prores-aw-color-tags 
b/tests/ref/fate/prores-aw-color-tags
new file mode 100644
index 00000000000..507d6d9355c
--- /dev/null
+++ b/tests/ref/fate/prores-aw-color-tags
@@ -0,0 +1,11 @@
+fd74c6be369cd51cbe79938d32f00543 *tests/data/fate/prores-aw-color-tags.mov
+4655 tests/data/fate/prores-aw-color-tags.mov
+#tb 0: 1/12800
+#media_type 0: video
+#codec_id 0: prores
+#dimensions 0: 64x64
+#sar 0: 1/1
+0,          0,          0,      512,     3928, 0x646d8844
+color_space=smpte170m
+color_primaries=unknown
+color_transfer=bt709
diff --git a/tests/ref/fate/prores-ks-color-tags 
b/tests/ref/fate/prores-ks-color-tags
new file mode 100644
index 00000000000..db893e8670f
--- /dev/null
+++ b/tests/ref/fate/prores-ks-color-tags
@@ -0,0 +1,11 @@
+307522f6ceaf7d0b98b3c06016ecfb22 *tests/data/fate/prores-ks-color-tags.mov
+3992 tests/data/fate/prores-ks-color-tags.mov
+#tb 0: 1/12800
+#media_type 0: video
+#codec_id 0: prores
+#dimensions 0: 64x64
+#sar 0: 1/1
+0,          0,          0,      512,     3265, 0x30f04d72
+color_space=smpte170m
+color_primaries=unknown
+color_transfer=bt709
-- 
2.54.0

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

Reply via email to