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

A palette change on a qtrle "no change" frame was lost, so later frames kept 
the old colors.

Depends on https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26191 (its two commits 
are included here); only the last commit is new.

New sample: qtrle/Animation-4bit-palette-change.mov (uploaded), written by 
QuickTime 7: two sample descriptions with different color tables, switching on 
7-byte no-change frames. Frames QuickTime draws match it bit-exact.

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



From 199af68e7f2fa5cc5173c7c89d875b53fb867f5f Mon Sep 17 00:00:00 2001
From: Baptiste Coudurier <[email protected]>
Date: Sun, 22 Nov 2026 12:23:59 -0800
Subject: [PATCH 1/3] avformat/mov: keep the palette of each sample description

Send the one selected for the sample by its chunk or fragment.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Baptiste Coudurier <[email protected]>
---
 libavformat/isom.h |  2 +
 libavformat/mov.c  | 95 +++++++++++++++++++++++++++++++++++++++-------
 2 files changed, 83 insertions(+), 14 deletions(-)

diff --git a/libavformat/isom.h b/libavformat/isom.h
index 7ad42f52809..3f041617f03 100644
--- a/libavformat/isom.h
+++ b/libavformat/isom.h
@@ -237,6 +237,8 @@ typedef struct MOVStreamContext {
     int v_spacing;        ///< pasp vSpacing
     int dts_shift;        ///< dts shift when ctts is negative
     uint32_t palette[256];
+    uint32_t **palettes;  ///< palette of each sample description
+    const uint32_t *last_palette; ///< last palette sent
     int has_palette;
     int64_t data_size;
     uint32_t tmcd_flags;  ///< tmcd track flags
diff --git a/libavformat/mov.c b/libavformat/mov.c
index 6d31dc729ad..5b13a60faba 100644
--- a/libavformat/mov.c
+++ b/libavformat/mov.c
@@ -3090,6 +3090,7 @@ static void mov_parse_stsd_video(MOVContext *c, 
AVIOContext *pb,
 
     avio_seek(pb, stsd_start, SEEK_SET);
 
+    memset(sc->palette, 0, sizeof(sc->palette));
     if (ff_get_qtpalette(st->codecpar->codec_id, pb, sc->palette)) {
         st->codecpar->bits_per_coded_sample &= 0x1F;
         sc->has_palette = 1;
@@ -3563,6 +3564,13 @@ int ff_mov_read_stsd_entries(MOVContext *c, AVIOContext 
*pb, int entries)
         if (ret < 0)
             return ret;
 
+        if (sc->has_palette && sc->palettes) {
+            sc->palettes[pseudo_stream_id] = av_memdup(sc->palette, 
AVPALETTE_SIZE);
+            if (!sc->palettes[pseudo_stream_id])
+                return AVERROR(ENOMEM);
+            sc->has_palette = 0;
+        }
+
         if (sc->extradata && st->codecpar->extradata) {
             int extra_size = st->codecpar->extradata_size;
 
@@ -3622,7 +3630,8 @@ static int mov_read_stsd(MOVContext *c, AVIOContext *pb, 
MOVAtom atom)
         return AVERROR(ENOMEM);
 
     sc->extradata_size = av_calloc(entries, sizeof(*sc->extradata_size));
-    if (!sc->extradata_size) {
+    sc->palettes       = av_calloc(entries, sizeof(*sc->palettes));
+    if (!sc->extradata_size || !sc->palettes) {
         ret = AVERROR(ENOMEM);
         goto fail;
     }
@@ -3643,6 +3652,10 @@ static int mov_read_stsd(MOVContext *c, AVIOContext *pb, 
MOVAtom atom)
 
     return mov_finalize_stsd_codec(c, pb, st, sc);
 fail:
+    if (sc->palettes)
+        for (int j = 0; j < entries; j++)
+            av_freep(&sc->palettes[j]);
+    av_freep(&sc->palettes);
     if (sc->extradata) {
         int j;
         for (j = 0; j < sc->stsd_count; j++)
@@ -5839,8 +5852,10 @@ static int mov_read_trak(MOVContext *c, AVIOContext *pb, 
MOVAtom atom)
         if (!stts_constant)
             ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL;
     }
-    /* Do not need those anymore. */
-    av_freep(&sc->chunk_offsets);
+    /* Do not need those anymore, except chunk offsets to find the sample
+     * description of a packet. */
+    if (sc->stsd_count < 2)
+        av_freep(&sc->chunk_offsets);
     av_freep(&sc->sample_sizes);
     av_freep(&sc->keyframes);
     av_freep(&sc->stps_data);
@@ -11074,6 +11089,11 @@ static void mov_free_stream_context(AVFormatContext 
*s, AVStream *st)
     av_freep(&sc->extradata);
     av_freep(&sc->extradata_size);
 
+    if (sc->palettes)
+        for (int i = 0; i < sc->stsd_count; i++)
+            av_freep(&sc->palettes[i]);
+    av_freep(&sc->palettes);
+
     mov_free_encryption_index(&sc->cenc.encryption_index);
     av_encryption_info_free(sc->cenc.default_encrypted_sample);
     av_aes_ctr_free(sc->cenc.aes_ctr);
@@ -12941,6 +12961,50 @@ static int get_eia608_packet(AVIOContext *pb, AVPacket 
*pkt, int src_size)
     return ret;
 }
 
+/* Index of the sample description of the sample in pkt, or -1. */
+static int mov_get_stsd_index(MOVContext *mov, MOVStreamContext *sc, AVPacket 
*pkt)
+{
+    MOVFragmentIndex *frag_index = &mov->frag_index;
+    int lo, hi, id;
+
+    if (sc->stsd_count < 2)
+        return 0;
+
+    if (frag_index->nb_items && frag_index->item[0].moof_offset < pkt->pos) {
+        MOVFragmentStreamInfo *frag = 
get_frag_stream_info_from_pkt(frag_index, pkt, sc->id);
+        id = frag ? frag->stsd_id : 0;
+        return id > 0 && id <= sc->stsd_count ? id - 1 : -1;
+    }
+
+    if (!sc->chunk_offsets || !sc->chunk_count || !sc->stsc_count)
+        return -1;
+
+    /* The last chunk starting at or before the sample */
+    lo = 0;
+    hi = sc->chunk_count - 1;
+    while (lo < hi) {
+        int mid = lo + (hi - lo + 1) / 2;
+        if (sc->chunk_offsets[mid] <= pkt->pos)
+            lo = mid;
+        else
+            hi = mid - 1;
+    }
+    id = lo + 1;
+
+    /* The last stsc entry starting at or before that chunk */
+    lo = 0;
+    hi = sc->stsc_count - 1;
+    while (lo < hi) {
+        int mid = lo + (hi - lo + 1) / 2;
+        if (sc->stsc_data[mid].first <= id)
+            lo = mid;
+        else
+            hi = mid - 1;
+    }
+    id = sc->stsc_data[lo].id;
+    return id > 0 && id <= sc->stsd_count ? id - 1 : -1;
+}
+
 static int mov_finalize_packet(AVFormatContext *s, AVStream *st, AVIndexEntry 
*sample,
                                 int64_t current_index, AVPacket *pkt)
 {
@@ -13022,6 +13086,20 @@ static int mov_finalize_packet(AVFormatContext *s, 
AVStream *st, AVIndexEntry *s
         }
     }
 
+    if (sc->palettes && st->discard != AVDISCARD_ALL) {
+        int index = mov_get_stsd_index(s->priv_data, sc, pkt);
+        const uint32_t *palette = index >= 0 ? sc->palettes[index] : NULL;
+
+        if (palette && palette != sc->last_palette) {
+            uint8_t *side = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE,
+                                                    AVPALETTE_SIZE);
+            if (!side)
+                return AVERROR(ENOMEM);
+            memcpy(side, palette, AVPALETTE_SIZE);
+            sc->last_palette = palette;
+        }
+    }
+
     return 0;
 }
 
@@ -13171,17 +13249,6 @@ static int mov_read_packet(AVFormatContext *s, 
AVPacket *pkt)
                 return ret;
         }
 #endif
-        if (sc->has_palette) {
-            uint8_t *pal;
-
-            pal = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE, 
AVPALETTE_SIZE);
-            if (!pal) {
-                av_log(mov->fc, AV_LOG_ERROR, "Cannot append palette to 
packet\n");
-            } else {
-                memcpy(pal, sc->palette, AVPALETTE_SIZE);
-                sc->has_palette = 0;
-            }
-        }
         if (st->codecpar->codec_id == AV_CODEC_ID_MP3 && 
!ffstream(st)->need_parsing && pkt->size > 4) {
             if (ff_mpa_check_header(AV_RB32(pkt->data)) < 0)
                 ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL;
-- 
2.54.1


From c6de95336c611289f1520813c0db7df8322ee979 Mon Sep 17 00:00:00 2001
From: Baptiste Coudurier <[email protected]>
Date: Sun, 22 Nov 2026 12:24:00 -0800
Subject: [PATCH 2/3] 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 ++++++++++++++++++++++---------
 libavformat/qtpalette.h           |  4 +--
 tests/fate/qtrle.mak              |  6 +++++
 tests/ref/fate/qtrle-4bit-clut36  |  9 +++++++
 tests/ref/fate/qtrle-8bit-clut127 |  9 +++++++
 5 files changed, 59 insertions(+), 14 deletions(-)
 create mode 100644 tests/ref/fate/qtrle-4bit-clut36
 create mode 100644 tests/ref/fate/qtrle-8bit-clut127

diff --git a/libavformat/qtpalette.c b/libavformat/qtpalette.c
index 046daf0acb8..617066e1006 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, as in QuickTime 7 */
+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. -1 and unrecognized IDs
+         * use 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/libavformat/qtpalette.h b/libavformat/qtpalette.h
index 18a108ebaf7..42577c3cb71 100644
--- a/libavformat/qtpalette.h
+++ b/libavformat/qtpalette.h
@@ -28,8 +28,8 @@
 
 /**
  * Retrieve the palette (or "color table" in QuickTime terms), either
- * from the video sample description, or from the default Macintosh
- * palette.
+ * from the video sample description or from a named or depth-default
+ * Macintosh color table.
  *
  * The file offset of the AVIOContext pointed to by the 'pb' variable
  * should be the start of the video sample description (the sample
diff --git a/tests/fate/qtrle.mak b/tests/fate/qtrle.mak
index 0be345d8de4..860849a93a2 100644
--- a/tests/fate/qtrle.mak
+++ b/tests/fate/qtrle.mak
@@ -7,9 +7,15 @@ 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
 
+FATE_QTRLE_SCALE += fate-qtrle-8bit-clut127
+fate-qtrle-8bit-clut127: CMD = framecrc -i 
$(TARGET_SAMPLES)/qtrle/Animation-8bit-clut127.mov -pix_fmt rgb24 -an -vf scale
+
 FATE_QTRLE_SCALE += fate-qtrle-16bit
 fate-qtrle-16bit: CMD = framecrc -i $(TARGET_SAMPLES)/qtrle/mr-cork-rle.mov 
-pix_fmt rgb24 -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
diff --git a/tests/ref/fate/qtrle-8bit-clut127 
b/tests/ref/fate/qtrle-8bit-clut127
new file mode 100644
index 00000000000..1e5c1d23299
--- /dev/null
+++ b/tests/ref/fate/qtrle-8bit-clut127
@@ -0,0 +1,9 @@
+#tb 0: 1/30
+#media_type 0: video
+#codec_id 0: rawvideo
+#dimensions 0: 40x24
+#sar 0: 0/1
+0,          0,          0,        1,     2880, 0xc53977bc
+0,          1,          1,        1,     2880, 0xe09f86bc
+0,          2,          2,        1,     2880, 0x561495bc
+0,          3,          3,        1,     2880, 0x2589a4bc
-- 
2.54.1


From d340e140eeb3ee2c03baa780de0ecf672b653e5d Mon Sep 17 00:00:00 2001
From: Baptiste Coudurier <[email protected]>
Date: Sun, 22 Nov 2026 22:36:26 -0800
Subject: [PATCH 3/3] avcodec/qtrle: apply a new palette to duplicate frames

Duplicate frames returned before reading the palette side data,
so a palette change on such a frame was lost.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Baptiste Coudurier <[email protected]>
---
 libavcodec/qtrle.c                       | 25 ++++++++++++------------
 tests/fate/qtrle.mak                     |  3 +++
 tests/ref/fate/qtrle-4bit-palette-change | 13 ++++++++++++
 3 files changed, 29 insertions(+), 12 deletions(-)
 create mode 100644 tests/ref/fate/qtrle-4bit-palette-change

diff --git a/libavcodec/qtrle.c b/libavcodec/qtrle.c
index e89fef031f8..11801f9ac8d 100644
--- a/libavcodec/qtrle.c
+++ b/libavcodec/qtrle.c
@@ -451,12 +451,16 @@ static int qtrle_decode_frame(AVCodecContext *avctx, 
AVFrame *rframe,
     QtrleContext *s = avctx->priv_data;
     int header, start_line;
     int height, row_ptr;
-    int has_palette = 0;
+    int new_palette = 0;
     int duplicate = 0;
     int ret, size;
 
     bytestream2_init(&s->g, avpkt->data, avpkt->size);
 
+    /* a new palette also applies to frames that do not change */
+    if (avctx->pix_fmt == AV_PIX_FMT_PAL8)
+        new_palette = ff_copy_palette(s->pal, avpkt, avctx);
+
     /* check if this frame is even supposed to change */
     if (avpkt->size < 8) {
         duplicate = 1;
@@ -499,25 +503,21 @@ static int qtrle_decode_frame(AVCodecContext *avctx, 
AVFrame *rframe,
     case 1:
     case 33:
         qtrle_decode_1bpp(s, row_ptr, height);
-        has_palette = 1;
         break;
 
     case 2:
     case 34:
         qtrle_decode_2n4bpp(s, row_ptr, height, 2);
-        has_palette = 1;
         break;
 
     case 4:
     case 36:
         qtrle_decode_2n4bpp(s, row_ptr, height, 4);
-        has_palette = 1;
         break;
 
     case 8:
     case 40:
         qtrle_decode_8bpp(s, row_ptr, height);
-        has_palette = 1;
         break;
 
     case 16:
@@ -538,21 +538,22 @@ static int qtrle_decode_frame(AVCodecContext *avctx, 
AVFrame *rframe,
         break;
     }
 
-    if(has_palette) {
-        ff_copy_palette(s->pal, avpkt, avctx);
-        /* make the palette available on the way out */
-        memcpy(s->frame->data[1], s->pal, AVPALETTE_SIZE);
-    }
-
 done:
     if (!s->frame->data[0])
         return AVERROR_INVALIDDATA;
-    if (duplicate) {
+    if (duplicate && !new_palette) {
         // ff_reget_buffer() isn't needed when frames don't change, so just 
update
         // frame props.
         ret = ff_decode_frame_props(avctx, s->frame);
         if (ret < 0)
             return ret;
+    } else {
+        // the picture of a duplicate frame stays, its palette may not
+        if (duplicate && (ret = ff_reget_buffer(avctx, s->frame, 0)) < 0)
+            return ret;
+        /* make the palette available on the way out */
+        if (avctx->pix_fmt == AV_PIX_FMT_PAL8)
+            memcpy(s->frame->data[1], s->pal, AVPALETTE_SIZE);
     }
 
     if ((ret = av_frame_ref(rframe, s->frame)) < 0)
diff --git a/tests/fate/qtrle.mak b/tests/fate/qtrle.mak
index 860849a93a2..bebc769e2f7 100644
--- a/tests/fate/qtrle.mak
+++ b/tests/fate/qtrle.mak
@@ -10,6 +10,9 @@ fate-qtrle-4bit: CMD = framecrc -i 
$(TARGET_SAMPLES)/qtrle/Animation-16Greys.mov
 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-4bit-palette-change
+fate-qtrle-4bit-palette-change: CMD = framecrc -i 
$(TARGET_SAMPLES)/qtrle/Animation-4bit-palette-change.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-palette-change 
b/tests/ref/fate/qtrle-4bit-palette-change
new file mode 100644
index 00000000000..e20aaf38a43
--- /dev/null
+++ b/tests/ref/fate/qtrle-4bit-palette-change
@@ -0,0 +1,13 @@
+#tb 0: 1/30
+#media_type 0: video
+#codec_id 0: rawvideo
+#dimensions 0: 64x48
+#sar 0: 0/1
+0,          0,          0,        1,     9216, 0xc5bd1d2c
+0,          1,          1,        1,     9216, 0x3c6d082c
+0,          2,          2,        1,     9216, 0x350ef31d
+0,          3,          3,        1,     9216, 0x90a5eb0e
+0,          4,          4,        1,     9216, 0x90a5eb0e
+0,          5,          5,        1,     9216, 0x3a13101d
+0,          6,          6,        1,     9216, 0xafa0de1d
+0,          7,          7,        1,     9216, 0xac32c91d
-- 
2.54.1

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

Reply via email to