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

A non-zero clut ID in a palettised video sample description names a standard 
Macintosh color table (Inside Macintosh: QuickTime, ImageDescription clutID): 
the depth for color, 32 + depth for greyscale, 127 for the eight QuickDraw 
colors. FFmpeg treated every non-zero ID as -1 and picked the table from the 
depth alone.

QuickTime writes the table ID it drew with, e.g. 4-bit video with the grey ramp 
is stored as depth 4, clut 36; FFmpeg decoded that in color.

- Use the named table (1, 2, 4, 8, 33, 34, 36, 40, 127); other IDs keep the 
default for the depth.
- Matches QuickTime 7's decodes on QuickTime-written rle/raw files at depth 
2/4/8 with each of these tables.
- New test fate-qtrle-4bit-clut36; sample qtrle/Animation-4bit-clut36.mov is 
uploaded to fate-suite.
- No other FATE ref changes.

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



From cba68355fb0bb4966d56ded3326b6b7f2d662ff3 Mon Sep 17 00:00:00 2001
From: Baptiste Coudurier <[email protected]>
Date: Wed, 18 Nov 2026 16:05:52 -0800
Subject: [PATCH] avformat/qtpalette: use the standard color table named by the
 clut ID

QuickTime writes e.g. 4-bit video with the grey table (clut 36); matches 
QuickTime 7.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Baptiste Coudurier <[email protected]>
---
 libavformat/qtpalette.c          | 45 +++++++++++++++++++++++---------
 tests/fate/qtrle.mak             |  3 +++
 tests/ref/fate/qtrle-4bit-clut36 |  9 +++++++
 3 files changed, 45 insertions(+), 12 deletions(-)
 create mode 100644 tests/ref/fate/qtrle-4bit-clut36

diff --git a/libavformat/qtpalette.c b/libavformat/qtpalette.c
index 046daf0acb8..0bc438d8775 100644
--- a/libavformat/qtpalette.c
+++ b/libavformat/qtpalette.c
@@ -24,6 +24,7 @@
 #include <stdio.h>
 #include <stdint.h>
 
+#include "libavutil/macros.h"
 #include "libavcodec/codec_id.h"
 #include "avio.h"
 #include "qtpalette.h"
@@ -33,6 +34,18 @@ static const uint8_t qt_default_palette_2[2 * 3] = {
   0x00, 0x00, 0x00
 };
 
+/* The eight QuickDraw colors, 'clut' 127 */
+static const uint8_t qt_default_palette_8[8 * 3] = {
+  0x00, 0x00, 0x00,
+  0xFC, 0xF3, 0x05,
+  0xF2, 0x08, 0x84,
+  0xDD, 0x08, 0x06,
+  0x02, 0xAB, 0xEA,
+  0x00, 0x80, 0x11,
+  0x00, 0x00, 0xD4,
+  0xFF, 0xFF, 0xFF
+};
+
 /* From a screenshot of the "Monitors & Sound" control panel in Mac OS 7.5.5 */
 static const uint8_t qt_default_palette_4[4 * 3] = {
   0xFF, 0xFF, 0xFF,
@@ -343,14 +356,24 @@ int ff_get_qtpalette(int codec_id, AVIOContext *pb, 
uint32_t *palette)
         uint32_t color_count, color_start, color_end;
         uint32_t r, g, b;
 
-        /* Ignore the greyscale bit for 1-bit video and sample
-         * descriptions containing a color table. */
-        if (greyscale && bit_depth > 1 && color_table_id) {
+        /* A non-zero color table ID is the ID of a standard Macintosh
+         * color table: the depth for color, 32 + depth for greyscale,
+         * 127 for the eight QuickDraw colors. Any other value (-1) means
+         * the default table for the depth. The greyscale bit is ignored
+         * for 1-bit video. */
+        int table_id = color_table_id, table_size;
+        if (table_id != 1  && table_id != 2  && table_id != 4  && table_id != 
8 &&
+            table_id != 33 && table_id != 34 && table_id != 36 && table_id != 
40 &&
+            table_id != 127)
+            table_id = bit_depth | (greyscale && bit_depth > 1 ? 32 : 0);
+        table_size  = table_id == 127 ? 8 : 1 << (table_id & 31);
+        color_count = FFMIN(1 << bit_depth, table_size);
+
+        if (color_table_id && table_id > 33 && table_id <= 40) {
             int color_index, color_dec;
             /* compute the greyscale palette */
-            color_count = 1 << bit_depth;
             color_index = 255;
-            color_dec   = 256 / (color_count - 1);
+            color_dec   = 256 / (table_size - 1);
             for (i = 0; i < color_count; i++) {
                 r = g = b = color_index;
                 palette[i] = (0xFFU << 24) | (r << 16) | (g << 8) | (b);
@@ -359,16 +382,14 @@ int ff_get_qtpalette(int codec_id, AVIOContext *pb, 
uint32_t *palette)
                     color_index = 0;
             }
         } else if (color_table_id) {
-            /* The color table ID is non-zero. Interpret this as
-             * being -1, which means use the default Macintosh
-             * color table */
             const uint8_t *color_table;
-            color_count = 1 << bit_depth;
-            if (bit_depth == 1)
+            if (table_id == 127)
+                color_table = qt_default_palette_8;
+            else if (table_size == 2)
                 color_table = qt_default_palette_2;
-            else if (bit_depth == 2)
+            else if (table_size == 4)
                 color_table = qt_default_palette_4;
-            else if (bit_depth == 4)
+            else if (table_size == 16)
                 color_table = qt_default_palette_16;
             else
                 color_table = qt_default_palette_256;
diff --git a/tests/fate/qtrle.mak b/tests/fate/qtrle.mak
index 3adefd2009f..91ad36ae2a3 100644
--- a/tests/fate/qtrle.mak
+++ b/tests/fate/qtrle.mak
@@ -7,6 +7,9 @@ fate-qtrle-2bit: CMD = framecrc -i 
$(TARGET_SAMPLES)/qtrle/Animation-4Greys.mov
 FATE_QTRLE_SCALE += fate-qtrle-4bit
 fate-qtrle-4bit: CMD = framecrc -i 
$(TARGET_SAMPLES)/qtrle/Animation-16Greys.mov -pix_fmt rgb24 -an -vf scale
 
+FATE_QTRLE_SCALE += fate-qtrle-4bit-clut36
+fate-qtrle-4bit-clut36: CMD = framecrc -i 
$(TARGET_SAMPLES)/qtrle/Animation-4bit-clut36.mov -pix_fmt rgb24 -an -vf scale
+
 FATE_QTRLE_SCALE += fate-qtrle-8bit
 fate-qtrle-8bit: CMD = framecrc -i 
$(TARGET_SAMPLES)/qtrle/criticalpath-credits.mov -pix_fmt rgb24 -an -vf scale
 
diff --git a/tests/ref/fate/qtrle-4bit-clut36 b/tests/ref/fate/qtrle-4bit-clut36
new file mode 100644
index 00000000000..96eadbaa7bd
--- /dev/null
+++ b/tests/ref/fate/qtrle-4bit-clut36
@@ -0,0 +1,9 @@
+#tb 0: 1/30
+#media_type 0: video
+#codec_id 0: rawvideo
+#dimensions 0: 64x48
+#sar 0: 0/1
+0,          0,          0,        1,     9216, 0x3281853b
+0,          1,          1,        1,     9216, 0xa5dfec2c
+0,          2,          2,        1,     9216, 0x8310532c
+0,          3,          3,        1,     9216, 0xca05ba1d
-- 
2.54.1

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

Reply via email to