Commit 925dff3a19 for ffmpeg

commit 925dff3a1919fd4f8d18a93b1a1028638d424281
Author: Baptiste Coudurier <baptiste.coudurier@gmail.com>
Date:   Tue Nov 24 17:05:42 2026 -0800

    avformat: read QuickTime palettes without seeking back

    Read the depth and palette in one forward pass. This keeps
    nonseekable inputs positioned for codec data.

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

diff --git a/libavformat/matroskadec.c b/libavformat/matroskadec.c
index 0971e54a9b..9cedfb38e4 100644
--- a/libavformat/matroskadec.c
+++ b/libavformat/matroskadec.c
@@ -2990,14 +2990,12 @@ static int mkv_parse_video_codec(MatroskaTrack *track, AVCodecParameters *par,
                    "mov FourCC not found %s.\n", av_fourcc2str(fourcc));
         if (track->codec_priv.size >= 86) {
             FFIOContext b;
-            unsigned bit_depth = AV_RB16(track->codec_priv.data + 82);
-            ffio_init_read_context(&b, track->codec_priv.data,
-                                   track->codec_priv.size);
-            if (ff_get_qtpalette(codec_id, &b.pub, track->palette)) {
-                bit_depth         &= 0x1F;
+
+            ffio_init_read_context(&b, track->codec_priv.data + 82,
+                                   track->codec_priv.size - 82);
+            if (ff_get_qtpalette(codec_id, &b.pub, &par->bits_per_coded_sample,
+                                 track->palette))
                 track->has_palette = 1;
-            }
-            par->bits_per_coded_sample = bit_depth;
         }
         par->codec_tag = fourcc;
         return 0;
diff --git a/libavformat/mov.c b/libavformat/mov.c
index 90656f9231..fa2be6483a 100644
--- a/libavformat/mov.c
+++ b/libavformat/mov.c
@@ -3060,14 +3060,9 @@ static void mov_parse_stsd_video(MOVContext *c, AVIOContext *pb,
                                  AVStream *st, MOVStreamContext *sc)
 {
     uint8_t codec_name[32] = { 0 };
-    int64_t stsd_start;
     unsigned int len;
     uint32_t id = 0;

-    /* The first 16 bytes of the video sample description are already
-     * read in ff_mov_read_stsd_entries() */
-    stsd_start = avio_tell(pb) - 16;
-
     if (c->isom) {
         avio_skip(pb, 2);  /* pre_defined */
         avio_skip(pb, 2);  /* reserved */
@@ -3110,14 +3105,9 @@ static void mov_parse_stsd_video(MOVContext *c, AVIOContext *pb,
         !strncmp(codec_name, "Sorenson H263", 13))
         st->codecpar->codec_id = AV_CODEC_ID_FLV1;

-    st->codecpar->bits_per_coded_sample = avio_rb16(pb); /* depth */
-
-    avio_seek(pb, stsd_start, SEEK_SET);
-
-    if (ff_get_qtpalette(st->codecpar->codec_id, pb, sc->palette)) {
-        st->codecpar->bits_per_coded_sample &= 0x1F;
+    if (ff_get_qtpalette(st->codecpar->codec_id, pb,
+                         &st->codecpar->bits_per_coded_sample, sc->palette))
         sc->has_palette = 1;
-    }
 }

 static int mov_parse_stsd_audio(MOVContext *c, AVIOContext *pb,
diff --git a/libavformat/qtpalette.c b/libavformat/qtpalette.c
index 046daf0acb..1e6adfe921 100644
--- a/libavformat/qtpalette.c
+++ b/libavformat/qtpalette.c
@@ -320,16 +320,14 @@ static const uint8_t qt_default_palette_256[256 * 3] = {
   /* 255, 0xFF */  0x00, 0x00, 0x00
 };

-int ff_get_qtpalette(int codec_id, AVIOContext *pb, uint32_t *palette)
+int ff_get_qtpalette(int codec_id, AVIOContext *pb, int *depth, uint32_t *palette)
 {
-    int tmp, bit_depth, color_table_id, greyscale, i;
+    int tmp = avio_rb16(pb);
+    int bit_depth = tmp & 0x1F;
+    int greyscale = tmp & 0x20;
+    int color_table_id, i;

-    avio_seek(pb, 82, SEEK_CUR);
-
-    /* Get the bit depth and greyscale state */
-    tmp = avio_rb16(pb);
-    bit_depth = tmp & 0x1F;
-    greyscale = tmp & 0x20;
+    *depth = tmp;

     /* Get the color table ID */
     color_table_id = avio_rb16(pb);
@@ -401,6 +399,7 @@ int ff_get_qtpalette(int codec_id, AVIOContext *pb, uint32_t *palette)
             }
         }

+        *depth = bit_depth;
         return 1;
     }

diff --git a/libavformat/qtpalette.h b/libavformat/qtpalette.h
index 18a108ebaf..75639b01d6 100644
--- a/libavformat/qtpalette.h
+++ b/libavformat/qtpalette.h
@@ -31,10 +31,9 @@
  * from the video sample description, or from the default Macintosh
  * palette.
  *
- * The file offset of the AVIOContext pointed to by the 'pb' variable
- * should be the start of the video sample description (the sample
- * description size and the data format).
+ * @param pb Positioned at the depth field of the video sample description.
+ * @param depth Receives the depth field, masked to the bit depth if palettized.
  */
-int ff_get_qtpalette(int codec_id, AVIOContext *pb, uint32_t *palette);
+int ff_get_qtpalette(int codec_id, AVIOContext *pb, int *depth, uint32_t *palette);

 #endif /* AVFORMAT_QTPALETTE_H */