Commit 94c67b7c41 for ffmpeg

commit 94c67b7c41fde53b30cdf8464a9677eff3365d5c
Author: Romain Beauxis <romain.beauxis@gmail.com>
Date:   Fri Nov 27 08:05:36 2026 -0600

    avformat/mov: trim the end of Vorbis from the packet length

    Like Opus, the decoded length of a Vorbis packet is only in the packet.
    It also depends on the setup header and on the block size of the packet
    before, so keep a parse context for each sample description, and reset
    it on seek and when a packet switches description.

    The packet's own sample description gives the setup header, which also
    holds after a seek within a later description.

    The end trim counts samples at the stream sample rate, so the packets
    of a description with another rate are left untrimmed.

diff --git a/libavformat/isom.h b/libavformat/isom.h
index 5866d39106..4d9091e992 100644
--- a/libavformat/isom.h
+++ b/libavformat/isom.h
@@ -194,6 +194,15 @@ typedef struct MOVIndexRange {
     int64_t end;
 } MOVIndexRange;

+/**
+ * What a Vorbis sample description needs to read the decoded length of its
+ * packets.
+ */
+typedef struct MOVVorbisDescription {
+    struct AVVorbisParseContext *parse;
+    uint32_t sample_rate; ///< from the identification header
+} MOVVorbisDescription;
+
 typedef struct MOVStreamContext {
     AVIOContext *pb;
     int refcount;
@@ -279,6 +288,9 @@ typedef struct MOVStreamContext {
     int *extradata_size;
     int last_stsd_index;
     int stsd_count;
+    /** for each sample description, to read the length of Vorbis packets */
+    MOVVorbisDescription *vorbis;
+    int64_t vorbis_next_index; ///< next media sample for which block history applies
     int stsd_gama_trc;  ///< transfer from the gama atom of the sample description being read
     int stsd_colr_trc;  ///< the colr atom of the sample description being read sets the transfer
     int stsd_version;
diff --git a/libavformat/mov.c b/libavformat/mov.c
index a5798b391f..188b4ff639 100644
--- a/libavformat/mov.c
+++ b/libavformat/mov.c
@@ -73,6 +73,8 @@
 #include "libavcodec/get_bits.h"
 #include "libavcodec/opus/parse.h"
 #include "libavcodec/opus/tab.h"
+#include "libavcodec/vorbis_parser.h"
+#include "libavcodec/xiph.h"
 #include "id3v1.h"
 #include "mov_chan.h"
 #include "replaygain.h"
@@ -3334,6 +3336,35 @@ static int mov_parse_stsd_data(MOVContext *c, AVIOContext *pb,
     return 0;
 }

+#define VORBIS_ID_HEADER_SIZE 30
+
+/**
+ * Prepare reading the decoded length of Vorbis packets, which depends on
+ * the setup header of their sample description.
+ */
+static int mov_init_vorbis_parse(MOVStreamContext *sc)
+{
+    sc->vorbis = av_calloc(sc->stsd_count, sizeof(*sc->vorbis));
+    if (!sc->vorbis)
+        return AVERROR(ENOMEM);
+    // The packets of a description that cannot be read are left untrimmed.
+    for (int i = 0; i < sc->stsd_count; i++) {
+        const uint8_t *header[3];
+        int header_len[3];
+        MOVVorbisDescription *desc = &sc->vorbis[i];
+
+        if (avpriv_split_xiph_headers(sc->extradata[i], sc->extradata_size[i],
+                                      VORBIS_ID_HEADER_SIZE, header,
+                                      header_len) < 0 ||
+            header_len[0] < VORBIS_ID_HEADER_SIZE)
+            continue;
+        desc->parse = av_vorbis_parse_init(sc->extradata[i],
+                                           sc->extradata_size[i]);
+        desc->sample_rate = AV_RL32(header[0] + 12);
+    }
+    return 0;
+}
+
 static int mov_finalize_stsd_codec(MOVContext *c, AVIOContext *pb,
                                    AVStream *st, MOVStreamContext *sc)
 {
@@ -3433,6 +3464,8 @@ static int mov_finalize_stsd_codec(MOVContext *c, AVIOContext *pb,
     case AV_CODEC_ID_H264:
         sti->need_parsing = AVSTREAM_PARSE_HEADERS;
         break;
+    case AV_CODEC_ID_VORBIS:
+        return mov_init_vorbis_parse(sc);
     default:
         break;
     }
@@ -4642,6 +4675,9 @@ static void mov_current_sample_set(MOVStreamContext *sc, int current_sample)
 {
     int64_t range_size;

+    // Do not carry Vorbis block history across a seek.
+    sc->vorbis_next_index = -1;
+
     sc->current_sample = current_sample;
     sc->current_index = current_sample;
     if (!sc->index_ranges) {
@@ -9196,15 +9232,41 @@ static int cenc_filter(MOVContext *mov, AVStream* st, MOVStreamContext *sc, AVPa
  * @return the decoded length of a packet, in samples, read from its payload,
  *         or 0 when unknown
  */
-static int64_t mov_packet_samples(AVStream *st, const AVPacket *pkt)
+static int64_t mov_packet_samples(AVStream *st, const AVPacket *pkt,
+                                  int64_t current_index)
 {
-    int samples;
+    MOVStreamContext *sc = st->priv_data;
+    MOVVorbisDescription *vorbis;
+    int samples, flags = 0;

     switch (st->codecpar->codec_id) {
     case AV_CODEC_ID_OPUS:
         samples = ff_opus_packet_samples(pkt->data, pkt->size);
         return samples > 0 ? av_rescale(samples, st->codecpar->sample_rate,
                                         OPUS_TIMESTAMP_RATE) : 0;
+    case AV_CODEC_ID_VORBIS:
+        // The sample description of the packet, also after a seek.
+        if (!sc->vorbis || sc->last_stsd_index >= sc->stsd_count)
+            return 0;
+        vorbis = &sc->vorbis[sc->last_stsd_index];
+        if (!vorbis->parse)
+            return 0;
+        // A skipped packet leaves the preceding block size unknown.
+        if (sc->vorbis_next_index != current_index)
+            av_vorbis_parse_reset(vorbis->parse);
+        // Header packets are not audio and have no length.
+        samples = av_vorbis_parse_frame_flags(vorbis->parse, pkt->data,
+                                              pkt->size, &flags);
+        sc->vorbis_next_index = samples > 0 ? current_index + 1 : -1;
+        // The decoder can reload the configuration from in-band headers.
+        if (flags & (VORBIS_FLAG_HEADER | VORBIS_FLAG_SETUP)) {
+            av_vorbis_parse_free(&vorbis->parse);
+            return 0;
+        }
+        // The generic trim and its boundaries use the stream sample rate.
+        if (vorbis->sample_rate != st->codecpar->sample_rate)
+            return 0;
+        return FFMAX(samples, 0);
     default:
         return 0;
     }
@@ -9214,7 +9276,7 @@ static int64_t mov_packet_samples(AVStream *st, const AVPacket *pkt)
  * Apply the end trim with the decoded length of the packet, for codecs
  * whose length is only in the payload.
  */
-static int mov_end_trim(AVStream *st, AVPacket *pkt)
+static int mov_end_trim(AVStream *st, AVPacket *pkt, int64_t current_index)
 {
     int64_t samples;

@@ -9223,11 +9285,13 @@ static int mov_end_trim(AVStream *st, AVPacket *pkt)
         av_packet_get_side_data(pkt, AV_PKT_DATA_ENCRYPTION_INFO, NULL))
         return 0;

-    // Without a discard range, no packet is trimmed.
-    if (!ffstream(st)->first_discard_sample)
+    // Without a discard range, no packet is trimmed. The length of a
+    // Vorbis packet depends on the one before, so keep reading those.
+    if (!ffstream(st)->first_discard_sample &&
+        st->codecpar->codec_id != AV_CODEC_ID_VORBIS)
         return 0;

-    samples = mov_packet_samples(st, pkt);
+    samples = mov_packet_samples(st, pkt, current_index);
     return samples > 0 ? ff_end_trim_packet(st, pkt, samples) : 0;
 }

@@ -11210,6 +11274,11 @@ static void mov_free_stream_context(AVFormatContext *s, AVStream *st)
         av_freep(&sc->tref_tags[i].id);
     av_freep(&sc->tref_tags);

+    if (sc->vorbis)
+        for (int i = 0; i < sc->stsd_count; i++)
+            av_vorbis_parse_free(&sc->vorbis[i].parse);
+    av_freep(&sc->vorbis);
+
     if (sc->extradata)
         for (int i = 0; i < sc->stsd_count; i++)
             av_free(sc->extradata[i]);
@@ -13111,6 +13180,10 @@ static int mov_change_extradata(AVStream *st, AVPacket *pkt)
     /* Save the current index. */
     sc->last_stsd_index = sc->stsc_data[sc->stsc_index].id - 1;

+    /* The last packet read with this description need not be the preceding packet. */
+    if (sc->vorbis && sc->vorbis[sc->last_stsd_index].parse)
+        av_vorbis_parse_reset(sc->vorbis[sc->last_stsd_index].parse);
+
     /* Notify the decoder that extradata changed. */
     extradata_size = sc->extradata_size[sc->last_stsd_index];
     extradata = sc->extradata[sc->last_stsd_index];
@@ -13495,7 +13568,7 @@ static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
         return ret;
     }

-    return mov_end_trim(st, pkt);
+    return mov_end_trim(st, pkt, current_index);
 }

 static int mov_seek_fragment(AVFormatContext *s, AVStream *st, int64_t timestamp)
diff --git a/tests/ref/fate/mov-mp4-vorbis-trim b/tests/ref/fate/mov-mp4-vorbis-trim
index 2d599070d2..6d2ae6a024 100644
--- a/tests/ref/fate/mov-mp4-vorbis-trim
+++ b/tests/ref/fate/mov-mp4-vorbis-trim
@@ -7,4 +7,4 @@
 #sample_rate 0: 48000
 #channel_layout_name 0: stereo
 0,          0,          0,     1024,       30, 0x03e00022
-0,       1024,       1024,     1008,       30, 0x03e00022
+0,       1024,       1024,     1008,       30, 0x03e00022, S=1, Skip Samples,       10, 0x00600010