Commit 994f7f1557 for ffmpeg

commit 994f7f155749063307a5565e0623ad589eed57dc
Author: Baptiste Coudurier <baptiste.coudurier@gmail.com>
Date:   Sun Nov 22 22:36:26 2026 -0800

    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. Keep it from rejected
    packets too, as the container sends each palette once.

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

diff --git a/libavcodec/qtrle.c b/libavcodec/qtrle.c
index e89fef031f..46be1e7da0 100644
--- a/libavcodec/qtrle.c
+++ b/libavcodec/qtrle.c
@@ -39,6 +39,7 @@
 #include "codec_internal.h"

 #include "libavutil/attributes.h"
+#include "libavutil/dict.h"

 typedef struct QtrleContext {
     AVCodecContext *avctx;
@@ -46,6 +47,7 @@ typedef struct QtrleContext {

     GetByteContext g;
     uint32_t pal[256];
+    int palette_pending;
 } QtrleContext;

 #define CHECK_PIXEL_PTR(n)                                                            \
@@ -451,12 +453,17 @@ 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 duplicate = 0;
+    int duplicate = 0, update_palette;
     int ret, size;

     bytestream2_init(&s->g, avpkt->data, avpkt->size);

+    /* the container sends each palette once: keep it even if the packet
+     * is rejected or does not change the picture */
+    if (avctx->pix_fmt == AV_PIX_FMT_PAL8 && avpkt->side_data_elems &&
+        ff_copy_palette(s->pal, avpkt, avctx))
+        s->palette_pending = 1;
+
     /* check if this frame is even supposed to change */
     if (avpkt->size < 8) {
         duplicate = 1;
@@ -499,25 +506,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,22 +541,39 @@ 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) {
-        // ff_reget_buffer() isn't needed when frames don't change, so just update
-        // frame props.
+    update_palette = duplicate ? s->palette_pending &&
+                                 memcmp(s->frame->data[1], s->pal, AVPALETTE_SIZE)
+                               : avctx->pix_fmt == AV_PIX_FMT_PAL8;
+    if (duplicate && update_palette && !av_frame_is_writable(s->frame)) {
+        /* the picture stays, the palette does not: copy the picture,
+         * keeping the reference if that fails */
+        ret = ff_get_buffer(avctx, rframe, AV_GET_BUFFER_FLAG_REF);
+        if (ret >= 0)
+            ret = av_frame_copy(rframe, s->frame);
+        if (ret < 0) {
+            av_frame_unref(rframe);
+            return ret;
+        }
+        av_frame_unref(s->frame);
+        av_frame_move_ref(s->frame, rframe);
+    } else if (duplicate && update_palette) {
+        av_dict_free(&s->frame->metadata);
+        ret = ff_reget_buffer(avctx, s->frame, 0);
+        if (ret < 0)
+            return ret;
+    } else if (duplicate) {
+        // The picture and palette are unchanged, so just update frame props.
         ret = ff_decode_frame_props(avctx, s->frame);
         if (ret < 0)
             return ret;
     }
+    /* make the palette available on the way out */
+    if (update_palette)
+        memcpy(s->frame->data[1], s->pal, AVPALETTE_SIZE);
+    s->palette_pending = 0;

     if ((ret = av_frame_ref(rframe, s->frame)) < 0)
         return ret;
diff --git a/tests/api/Makefile b/tests/api/Makefile
index 56fdec1d92..7214e719e6 100644
--- a/tests/api/Makefile
+++ b/tests/api/Makefile
@@ -14,6 +14,7 @@ APITESTPROGS-$(call DEMDEC, H263, H263) += api-band
 APITESTPROGS-$(HAVE_THREADS) += api-threadmessage
 APITESTPROGS-$(CONFIG_AVCODEC) += api-enc-info
 APITESTPROGS-$(CONFIG_PCM_S16LE_DECODER) += api-skip-samples
+APITESTPROGS-$(CONFIG_QTRLE_DECODER) += api-qtrle-palette
 APITESTPROGS-$(call ALLYES, H261_ENCODER H261_PARSER) += api-enc-parser
 APITESTPROGS-$(CONFIG_VC1_PARSER) += api-vc1-parser
 APITESTPROGS += $(APITESTPROGS-yes)
diff --git a/tests/api/api-qtrle-palette-test.c b/tests/api/api-qtrle-palette-test.c
new file mode 100644
index 0000000000..86cc9a4e1a
--- /dev/null
+++ b/tests/api/api-qtrle-palette-test.c
@@ -0,0 +1,81 @@
+/*
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include <stdio.h>
+#include <string.h>
+
+#include "libavcodec/avcodec.h"
+#include "libavutil/intreadwrite.h"
+
+static int decode(AVCodecContext *ctx, const AVPacket *packet, AVFrame *frame)
+{
+    int ret = avcodec_send_packet(ctx, packet);
+
+    return ret < 0 ? ret : avcodec_receive_frame(ctx, frame);
+}
+
+int main(void)
+{
+    /* One literal run of four palette indices. */
+    static const uint8_t coded[] = { 0, 0, 0, 13, 0, 0, 1, 1, 1, 2, 3, 4, 255 };
+    static const uint8_t no_change[] = { 0, 0, 0, 7, 0, 0, 0 };
+    static const uint8_t pixels[] = { 1, 2, 3, 4 };
+    AVCodecContext *ctx = avcodec_alloc_context3(avcodec_find_decoder(AV_CODEC_ID_QTRLE));
+    AVPacket *packet = av_packet_alloc();
+    AVFrame *held = av_frame_alloc();
+    AVFrame *duplicate = av_frame_alloc();
+    uint8_t *palette;
+    int ret = 1;
+
+    if (!ctx || !packet || !held || !duplicate)
+        goto end;
+    ctx->width = 4;
+    ctx->height = 1;
+    ctx->bits_per_coded_sample = 8;
+    if (avcodec_open2(ctx, NULL, NULL) < 0 ||
+        av_new_packet(packet, sizeof(coded)) < 0)
+        goto end;
+    memcpy(packet->data, coded, sizeof(coded));
+    palette = av_packet_new_side_data(packet, AV_PKT_DATA_PALETTE, AVPALETTE_SIZE);
+    if (!palette)
+        goto end;
+    AV_WN32(palette + 4, 0xff123456);
+    if (decode(ctx, packet, held) < 0)
+        goto end;
+
+    /* A "no change" packet with a new palette recolors the picture
+     * without touching the frame returned before. */
+    av_shrink_packet(packet, sizeof(no_change));
+    memcpy(packet->data, no_change, sizeof(no_change));
+    AV_WN32(palette + 4, 0xff654321);
+    if (decode(ctx, packet, duplicate) < 0 ||
+        memcmp(duplicate->data[0], pixels, sizeof(pixels)) ||
+        AV_RN32(duplicate->data[1] + 4) != 0xff654321 ||
+        AV_RN32(held->data[1] + 4) != 0xff123456)
+        goto end;
+    ret = 0;
+
+end:
+    if (ret)
+        fprintf(stderr, "QTRLE duplicate palette test failed\n");
+    av_frame_free(&duplicate);
+    av_frame_free(&held);
+    av_packet_free(&packet);
+    avcodec_free_context(&ctx);
+    return ret;
+}
diff --git a/tests/fate/api.mak b/tests/fate/api.mak
index ea9cb55d1f..3020066bf0 100644
--- a/tests/fate/api.mak
+++ b/tests/fate/api.mak
@@ -24,6 +24,11 @@ FATE_API_LIBAVCODEC-$(CONFIG_PCM_S16LE_DECODER) += fate-api-skip-samples
 fate-api-skip-samples: $(APITESTSDIR)/api-skip-samples-test$(EXESUF)
 fate-api-skip-samples: CMD = run $(APITESTSDIR)/api-skip-samples-test$(EXESUF)

+FATE_API_LIBAVCODEC-$(CONFIG_QTRLE_DECODER) += fate-api-qtrle-palette
+fate-api-qtrle-palette: $(APITESTSDIR)/api-qtrle-palette-test$(EXESUF)
+fate-api-qtrle-palette: CMD = run $(APITESTSDIR)/api-qtrle-palette-test$(EXESUF)
+fate-api-qtrle-palette: CMP = null
+
 FATE_API_LIBAVCODEC-$(CONFIG_MOVTEXT_ENCODER) += fate-api-movtext
 fate-api-movtext: $(APITESTSDIR)/api-movtext-test$(EXESUF)
 fate-api-movtext: CMD = run $(APITESTSDIR)/api-movtext-test$(EXESUF)