Commit 292e7e1b8f for ffmpeg

commit 292e7e1b8fc673c66625c29ec5ca49a444489bf9
Author: Romain Beauxis <romain.beauxis@gmail.com>
Date:   Thu Dec 3 10:42:44 2026 -0600

    avformat/ogg/muxer: trim the start of Vorbis links

    A Vorbis decoder outputs nothing for its first packet, so a cut keeps
    only the last packet before the start as its primer, and the granule of
    the first page, which completes it and the next packet, trims the start
    (Vorbis I A.2).

    A link starting after zero keeps its timestamps, while a later link
    restarts at the first output of its decoder.

    The cut, edit-list and chained copies now decode to their source, while
    the limited_input_seek copies and lavf-fate/vp8.ogg keep the start of
    their first presented packet.

diff --git a/doc/muxers.texi b/doc/muxers.texi
index dbcdc0e17a..a1e8e35e92 100644
--- a/doc/muxers.texi
+++ b/doc/muxers.texi
@@ -3521,7 +3521,13 @@ They then play the samples their pre-skip cannot skip, never skipping
 samples after the start.
 Only Opus keeps a pre-roll of a set duration, of at most the 65535 samples its
 pre-skip can hold.
-Vorbis keeps all of it, which is played.
+Vorbis keeps the last packet before the start, on which its decoder primes,
+and trims the start in the granule of the first audio page.
+A link ending on that page, a primer and first packet too large for one page,
+or a flush before the first presented packet play the samples before the start
+instead, with a warning.
+The granule only trims samples before timestamp zero: a start skip reaching
+past it is played from there, with a warning.
 Other codecs drop it and play the part of the first packet before the start.

 @end table
diff --git a/libavformat/ogg/muxer.c b/libavformat/ogg/muxer.c
index 9fbc47b4b6..17ee32ea45 100644
--- a/libavformat/ogg/muxer.c
+++ b/libavformat/ogg/muxer.c
@@ -95,6 +95,7 @@ static void ogg_cleanup_stream(AVStream *st)

     for (int i = 0; i < 3; i++)
         av_freep(&oggstream->header[i]);
+    av_packet_free(&oggstream->primer);
     if (oggstream->priv_data && oggstream->mapping->deinit)
         oggstream->mapping->deinit(st);
     av_freep(&oggstream->priv_data);
@@ -235,9 +236,10 @@ static int ogg_buffer_data(AVFormatContext *s, AVStream *st,
         flush = 1;
     }

-    // avoid a continued page
-    if (!header && oggstream->page.size > 0 &&
-        MAX_PAGE_SIZE - oggstream->page.size < size) {
+    /* A page kept open can be full. Otherwise avoid a continued page. */
+    if (oggstream->page.segments_count == 255 ||
+        (!header && oggstream->page.size > 0 &&
+         MAX_PAGE_SIZE - oggstream->page.size < size)) {
         if ((ret = ogg_buffer_page(s, oggstream)) < 0)
             return ret;
     }
@@ -790,6 +792,17 @@ static int64_t ogg_preroll_packet_start(const AVStream *st, const AVPacket *pkt,
     return link_pts;
 }

+static void ogg_log_dropped_preroll(AVFormatContext *s, const AVStream *st)
+{
+    const OGGStreamContext *oggstream = st->priv_data;
+
+    if (oggstream->dropped_packets)
+        av_log(s, AV_LOG_VERBOSE, "Stream %d: dropped %d %s pre-roll packets "
+               "(%"PRId64" samples) before the start\n", st->index,
+               oggstream->dropped_packets, avcodec_get_name(st->codecpar->codec_id),
+               oggstream->dropped_samples);
+}
+
 /**
  * Write the start trim of a link, and report what its headers cannot
  * signal.
@@ -812,10 +825,7 @@ static int64_t ogg_write_start_trim(AVFormatContext *s, AVStream *st, int64_t tr
     else
         written = FFMIN(trim, mapping->max_start_trim);

-    if (oggstream->dropped_packets)
-        av_log(s, AV_LOG_VERBOSE, "Stream %d: dropped %d %s pre-roll packets "
-               "(%"PRId64" samples) before the start\n", st->index,
-               oggstream->dropped_packets, codec, oggstream->dropped_samples);
+    ogg_log_dropped_preroll(s, st);
     if (trim > written)
         av_log(s, AV_LOG_WARNING, "Stream %d: %"PRId64" samples before the start "
                "cannot be trimmed in %s and are played\n", st->index,
@@ -835,6 +845,109 @@ static int64_t ogg_write_start_trim(AVFormatContext *s, AVStream *st, int64_t tr
     return written;
 }

+/**
+ * Buffer the held primer, ending at the link timestamp end.
+ */
+static int ogg_write_primer(AVFormatContext *s, AVStream *st, int64_t end)
+{
+    OGGStreamContext *oggstream = st->priv_data;
+    int ret = ogg_buffer_packet(s, st, oggstream->primer,
+                                end - oggstream->primer->duration, 1);
+
+    av_packet_free(&oggstream->primer);
+    return ret;
+}
+
+/**
+ * Write a primer held when no presented packet follows yet, as the start of
+ * its link. Called by a flush and by the trailer.
+ */
+static int ogg_commit_primer(AVFormatContext *s, AVStream *st, int flush)
+{
+    OGGStreamContext *oggstream = st->priv_data;
+    /* A page completing only the primer cannot have a negative granule. */
+    int64_t end = FFMAX(oggstream->coded_start, 0);
+
+    if (!oggstream->primer)
+        return 0;
+    if (flush && oggstream->coded_start < 0)
+        av_log(s, AV_LOG_WARNING, "Stream %d: flushed before the first presented "
+               "packet, %"PRId64" samples before the start are played\n", st->index,
+               -oggstream->coded_start);
+    oggstream->start_pending   = 0;
+    oggstream->link_pts_offset = oggstream->primer->pts + oggstream->primer->duration - end;
+    oggstream->next_link_pts   = end;
+    return ogg_write_primer(s, st, end);
+}
+
+/**
+ * Handle a packet of an OGG_START_PREROLL_PRIMER link of which no packet is
+ * written yet. The last packet ending before the start is held as the
+ * primer, earlier ones are dropped. The packet after the primer is the first
+ * presented: both go on the first page, whose granule trims the start, see
+ * Vorbis I A.2.
+ *
+ * @return 1 when pkt is to be written, 0 when it was dropped or held
+ */
+static int ogg_start_primed_link(AVFormatContext *s, AVStream *st,
+                                 const AVPacket *pkt, int64_t link_pts)
+{
+    OGGStreamContext *oggstream = st->priv_data;
+    int64_t coded = ogg_preroll_packet_start(st, pkt, link_pts);
+    int64_t trim  = FFMAX(oggstream->start_skip, -coded);
+    AVPacket *primer = oggstream->primer;
+    int ret;
+
+    /* The decoder outputs nothing for the first packet it is given. */
+    if (!primer || (pkt->duration > 0 && trim >= pkt->duration)) {
+        if (primer) {
+            oggstream->dropped_packets++;
+            oggstream->dropped_samples += primer->duration;
+        } else if (!(oggstream->primer = primer = av_packet_alloc())) {
+            return AVERROR(ENOMEM);
+        }
+        av_packet_unref(primer);
+        if ((ret = av_packet_ref(primer, pkt)) < 0) {
+            av_packet_free(&oggstream->primer);
+            return ret;
+        }
+        oggstream->start_skip       = FFMAX(oggstream->start_skip - pkt->duration, 0);
+        oggstream->coded_start      = coded + pkt->duration;
+        /* A primer clamped to the start does not repeat a pre-roll timestamp. */
+        oggstream->last_dropped_pts = link_pts < 0 ? link_pts : AV_NOPTS_VALUE;
+        return 0;
+    }
+
+    ogg_log_dropped_preroll(s, st);
+    /* A later link restarts its timestamps where its decoder starts output. */
+    if (oggstream->rebased)
+        coded = -trim;
+    if (trim > 0 && coded > -trim) {
+        int64_t written = FFMAX(-coded, 0);
+
+        av_log(s, AV_LOG_WARNING, "Stream %d: %"PRId64" samples skipped by the input "
+               "cannot be trimmed at this Vorbis start timestamp and are played\n",
+               st->index, trim - written);
+        trim = written;
+    }
+    if (trim > 0 && (primer->size / 255 + pkt->size / 255 + 2 > 255 ||
+                     primer->size + pkt->size > MAX_PAGE_SIZE)) {
+        av_log(s, AV_LOG_WARNING, "Stream %d: the primer and the first presented "
+               "packet do not fit in one page, %"PRId64" samples before the start "
+               "are played\n", st->index, trim);
+        coded = 0;
+        trim  = 0;
+    }
+    oggstream->start_pending   = 0;
+    oggstream->link_pts_offset = pkt->pts - coded;
+    oggstream->start_page_trim = trim;
+    /* A start in the granules must not share its page with the end trim,
+     * see Vorbis I A.2. */
+    oggstream->start_offset    = FFMAX(coded, 0);
+    ret = ogg_write_primer(s, st, coded);
+    return ret < 0 ? ret : 1;
+}
+
 /**
  * Handle a packet of a link of which no packet is written yet. Packets
  * presenting no sample are dropped once the pre-roll kept covers what the
@@ -853,10 +966,8 @@ static int ogg_start_link(AVFormatContext *s, AVStream *st,
                                                : oggstream->codec_start_trim;
     int64_t coded, trim, next_trim, written;

-    if (mapping->start_preroll == OGG_START_PREROLL_ALL) {
-        oggstream->start_pending = 0;
-        return 1;
-    }
+    if (mapping->start_preroll == OGG_START_PREROLL_PRIMER)
+        return ogg_start_primed_link(s, st, pkt, link_pts);
     coded     = ogg_preroll_packet_start(st, pkt, link_pts);
     trim      = FFMAX(oggstream->start_skip, -coded);
     next_trim = FFMAX(oggstream->start_skip - pkt->duration, -(coded + pkt->duration));
@@ -883,7 +994,7 @@ static int ogg_start_link(AVFormatContext *s, AVStream *st,
     /* A start in the granules must not share its page with the end trim,
      * see RFC 7845 4.5. */
     if (mapping->set_start_trim)
-        oggstream->opus_start_offset = FFMAX(coded + oggstream->packet_granule_offset, 0);
+        oggstream->start_offset = FFMAX(coded + oggstream->packet_granule_offset, 0);

     return 1;
 }
@@ -906,13 +1017,15 @@ static int ogg_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
         return ret;
     oggstream->packet_seen = 1;

-    /* Keep a positive-start page open until the next packet needs its own
-     * page. A queued last page can block other streams. */
-    if (oggstream->opus_start_offset) {
+    /* Keep a start page open until the next packet needs its own page. A
+     * queued last page can block other streams. */
+    if (oggstream->start_offset || oggstream->start_page_trim) {
         if (oggstream->page.segments_count &&
             (ret = ogg_buffer_page(s, oggstream)) < 0)
             return ret;
-        oggstream->opus_start_offset = 0;
+        oggstream->start_offset          = 0;
+        oggstream->start_page_trim       = 0;
+        oggstream->start_primer_segments = 0;
     }

     if (oggstream->mapping->packet_samples) {
@@ -943,6 +1056,18 @@ static int ogg_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
         if (ret <= 0)
             return ret;
         link_pts = pkt->pts - oggstream->link_pts_offset;
+        /* If a positive-start Vorbis link ends on this packet, its end
+         * trim needs a page of its own. Defer the split when both packets
+         * fit, so an interior trim does not split the initial page. */
+        if (oggstream->start_offset && end_trim &&
+            oggstream->mapping->start_preroll == OGG_START_PREROLL_PRIMER) {
+            int primer_segments = oggstream->page.segments_count;
+
+            if (primer_segments + pkt->size / 255 + 1 <= 255)
+                oggstream->start_primer_segments = primer_segments;
+            else if ((ret = ogg_buffer_page(s, oggstream)) < 0)
+                return ret;
+        }
     }
     /* The first packet of a later link may carry the timestamps of the
      * previous link. */
@@ -962,7 +1087,8 @@ static int ogg_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
          FFABS(link_pts - oggstream->next_link_pts) <= ogg_timestamp_rounding(st)))
         link_pts = oggstream->next_link_pts;

-    ret = ogg_buffer_packet(s, st, pkt, link_pts, !!oggstream->opus_start_offset);
+    ret = ogg_buffer_packet(s, st, pkt, link_pts,
+                            oggstream->start_offset || oggstream->start_page_trim);
     if (ret < 0)
         return ret;
     if (oggstream->mapping->packet_samples) {
@@ -1014,12 +1140,13 @@ static void ogg_apply_end_trim(AVFormatContext *s, AVStream *st)
         last->granule -= end_trim;
         /* A single Opus page cannot carry both a start offset and an end
          * trim, see RFC 7845 4.5. The pre-skip must not exceed the granule. */
-        if (oggstream->opus_start_offset) {
-            last->granule = FFMAX(last->granule - oggstream->opus_start_offset,
+        if (oggstream->start_offset &&
+            oggstream->mapping->start_preroll != OGG_START_PREROLL_PRIMER) {
+            last->granule = FFMAX(last->granule - oggstream->start_offset,
                                   oggstream->packet_granule_offset);
-            av_log(s, AV_LOG_WARNING, "Stream %d: keeping the Opus end trim, "
+            av_log(s, AV_LOG_WARNING, "Stream %d: keeping the end trim, "
                    "the positive start offset of %"PRId64" samples is lost\n",
-                   st->index, oggstream->opus_start_offset);
+                   st->index, oggstream->start_offset);
         }
     } else {
         av_log(s, AV_LOG_WARNING, "Stream %d: the last page was flushed before "
@@ -1037,8 +1164,11 @@ static int ogg_write_packet(AVFormatContext *s, AVPacket *pkt)

     for (i = 0; i < s->nb_streams; i++) {
         OGGStreamContext *oggstream = s->streams[i]->priv_data;
+        if ((ret = ogg_commit_primer(s, s->streams[i], 1)) < 0)
+            return ret;
         /* A flush writes the headers, whatever the start trim. */
         oggstream->headers_held = 0;
+        oggstream->start_primer_segments = 0;
         if (oggstream->page.segments_count &&
             (ret = ogg_buffer_page(s, oggstream)) < 0)
             return ret;
@@ -1064,6 +1194,37 @@ static void ogg_end_preroll_link(AVFormatContext *s, AVStream *st)
     ogg_set_start_trim(s, st, end_granule);
 }

+/**
+ * Let the primer establish the positive start on its own page, so the EOS
+ * page can trim the following packet.
+ */
+static int ogg_split_primer_page(AVFormatContext *s, OGGStreamContext *oggstream)
+{
+    OGGPage *page = &oggstream->page;
+    int primer_segments = oggstream->start_primer_segments;
+    int primer_size = (primer_segments - 1) * 255 + page->segments[primer_segments - 1];
+    int audio_segments = page->segments_count - primer_segments;
+    int audio_size = page->size - primer_size;
+    int64_t granule = page->granule;
+    int ret;
+
+    page->segments_count = primer_segments;
+    page->size           = primer_size;
+    page->granule        = oggstream->start_offset;
+    ret = ogg_buffer_page(s, oggstream);
+    if (ret < 0)
+        return ret;
+
+    /* ogg_buffer_page() resets the counters but leaves the arrays intact. */
+    memmove(page->segments, page->segments + primer_segments, audio_segments);
+    memmove(page->data, page->data + primer_size, audio_size);
+    page->segments_count = audio_segments;
+    page->size           = audio_size;
+    page->granule        = granule;
+    oggstream->start_primer_segments = 0;
+    return 0;
+}
+
 static int ogg_write_trailer(AVFormatContext *s)
 {
     int i, ret;
@@ -1073,6 +1234,21 @@ static int ogg_write_trailer(AVFormatContext *s)
         AVStream *st = s->streams[i];
         OGGStreamContext *oggstream = st->priv_data;

+        if ((ret = ogg_commit_primer(s, st, 0)) < 0)
+            return ret;
+        if (oggstream->start_primer_segments && oggstream->end_trim &&
+            (ret = ogg_split_primer_page(s, oggstream)) < 0)
+            return ret;
+        /* An EOS granule only trims the end, see Vorbis I A.2: the samples
+         * before the start are played, then all of the link. */
+        if (oggstream->start_page_trim && oggstream->page.segments_count) {
+            av_log(s, AV_LOG_WARNING, "Stream %d: the link ends on its first page, "
+                   "%"PRId64" samples before the start are played\n", st->index,
+                   oggstream->start_page_trim);
+            oggstream->page.granule += oggstream->start_page_trim;
+            oggstream->last_granule  = oggstream->page.granule;
+        }
+        oggstream->start_page_trim = 0;
         ogg_end_preroll_link(s, st);
         ogg_apply_end_trim(s, st);

diff --git a/libavformat/ogg/muxer.h b/libavformat/ogg/muxer.h
index 88116e760f..2ba9493626 100644
--- a/libavformat/ogg/muxer.h
+++ b/libavformat/ogg/muxer.h
@@ -50,8 +50,11 @@ enum OGGStartPreroll {
     OGG_START_PREROLL_NONE,
     /** OGGMuxerMapping.start_preroll_us, for the decoder to converge. */
     OGG_START_PREROLL_DURATION,
-    /** All of it: the codec has no start trim and needs the packets before. */
-    OGG_START_PREROLL_ALL,
+    /**
+     * The last packet before the start: the decoder outputs nothing for its
+     * first packet, and the granule of the first page trims the next one.
+     */
+    OGG_START_PREROLL_PRIMER,
 };

 typedef struct OGGStreamContext {
@@ -91,7 +94,11 @@ typedef struct OGGStreamContext {
     int64_t coded_start;
     int64_t last_dropped_pts; ///< input link timestamp of the last dropped packet
     int64_t start_skip; ///< samples the input decoder still skips before the start
-    int64_t opus_start_offset; ///< positive start offset until a second packet arrives
+    int64_t start_offset; ///< positive start offset until a second packet arrives
+    AVPacket *primer; ///< packet before the start of an OGG_START_PREROLL_PRIMER link
+    /** Start trim in the granule of the open first page, ended by the next packet. */
+    int64_t start_page_trim;
+    int start_primer_segments; ///< primer laces at the start of the open page
     int dropped_packets; ///< pre-roll packets dropped at the start of the link
     int64_t dropped_samples; ///< duration of dropped_packets
 } OGGStreamContext;
diff --git a/libavformat/ogg/muxer/vorbis.c b/libavformat/ogg/muxer/vorbis.c
index e8ab13340a..b9eb8495bd 100644
--- a/libavformat/ogg/muxer/vorbis.c
+++ b/libavformat/ogg/muxer/vorbis.c
@@ -73,8 +73,7 @@ const OGGMuxerMapping ff_ogg_vorbis_muxer_mapping = {
     .codec_id       = AV_CODEC_ID_VORBIS,
     .new_links      = 1,
     .priv_data_size = sizeof(VorbisMuxerContext),
-    /* A Vorbis start trim needs its own page layout, see Vorbis I A.2. */
-    .start_preroll  = OGG_START_PREROLL_ALL,
+    .start_preroll  = OGG_START_PREROLL_PRIMER,
     .init           = vorbis_init,
     .packet_samples = vorbis_packet_samples,
     .deinit         = vorbis_deinit,
diff --git a/tests/fate/ogg-copy-trim.mak b/tests/fate/ogg-copy-trim.mak
index 9d33bb65fc..89fcbf2d2e 100644
--- a/tests/fate/ogg-copy-trim.mak
+++ b/tests/fate/ogg-copy-trim.mak
@@ -200,6 +200,20 @@ fate-ogg-copy-trim-vorbis-primer-flush: CMD = \
     run libavformat/tests/ogg_pages$(EXESUF) $(TARGET_PATH)/$(OGG_COPY_TRIM_PRIMER_FLUSH)-0.ogg
 fate-ogg-copy-trim-vorbis-primer-flush: CLEANFILES = $(OGG_COPY_TRIM_PRIMER_FLUSH)-0.ogg

+# Timestamps repeated across the start: the copy matches the one of the same
+# packets with advancing timestamps.
+OGG_COPY_TRIM_VORBIS_REPEATED = tests/data/fate/ogg-copy-trim-vorbis-repeated
+FATE_OGG_COPY_TRIM-$(call ALLYES, SETTS_BSF $(OGG_COPY_TRIM_DEPS)) += fate-ogg-copy-trim-vorbis-repeated
+fate-ogg-copy-trim-vorbis-repeated: CMD = \
+    ffmpeg -i $(TARGET_SAMPLES)/vorbis/6.ogg -map 0:a -c copy \
+           -bsf:a "setts=ts=if(lt(N\,20)\,-3128\,PTS-3000)" \
+           -avoid_negative_ts disabled -fflags +bitexact -f ogg -y $(TARGET_PATH)/$(OGG_COPY_TRIM_VORBIS_REPEATED).ogg && \
+    ffmpeg -i $(TARGET_SAMPLES)/vorbis/6.ogg -map 0:a -c copy \
+           -bsf:a "setts=ts=PTS-3000" \
+           -avoid_negative_ts disabled -fflags +bitexact -f ogg -y $(TARGET_PATH)/$(OGG_COPY_TRIM_VORBIS_REPEATED).control.ogg && \
+    $(call OGG_CHAIN_COPY_COMPARE,$(OGG_COPY_TRIM_VORBIS_REPEATED).control.ogg,$(OGG_COPY_TRIM_VORBIS_REPEATED).ogg,copy)
+fate-ogg-copy-trim-vorbis-repeated: CLEANFILES = $(addprefix $(OGG_COPY_TRIM_VORBIS_REPEATED),.control.ogg .ogg)
+
 # Vorbis whose MP4 edit list starts after the first second of samples.
 OGG_COPY_TRIM_VORBIS_EDIT = tests/data/fate/ogg-copy-trim-vorbis-edit-list
 FATE_OGG_COPY_TRIM-$(call ALLYES, MOV_DEMUXER VORBIS_DECODER $(OGG_COPY_TRIM_DEPS)) += fate-ogg-copy-trim-vorbis-edit-list
diff --git a/tests/ref/fate/limited_input_seek b/tests/ref/fate/limited_input_seek
index b54ace3505..425852817f 100644
--- a/tests/ref/fate/limited_input_seek
+++ b/tests/ref/fate/limited_input_seek
@@ -1 +1 @@
-8f500a3ccdb5390b14fd744385a17994
+c7fd03d9eddbc5ed76e4f9cfdbab546f
diff --git a/tests/ref/fate/limited_input_seek-copyts b/tests/ref/fate/limited_input_seek-copyts
index 5aecbf38c4..deb6cf9e30 100644
--- a/tests/ref/fate/limited_input_seek-copyts
+++ b/tests/ref/fate/limited_input_seek-copyts
@@ -1 +1 @@
-a6d5487102ad442679ee164a47df80b7
+29b44ae4b710ec1a6b42797dfb951ff0
diff --git a/tests/ref/fate/ogg-copy-trim-vorbis-chained b/tests/ref/fate/ogg-copy-trim-vorbis-chained
index b030610389..bb36579020 100644
--- a/tests/ref/fate/ogg-copy-trim-vorbis-chained
+++ b/tests/ref/fate/ogg-copy-trim-vorbis-chained
@@ -9,7 +9,7 @@
 0,        576,        576,     1024,       85, 0x34322b3b, S=1, Skip Samples,       10, 0x00830016
 0,       1600,       1600,      128,       28, 0xa5cb0b38, S=2, Strings Metadata,       52, 0xbfe6111a, New Extradata,     3302, 0xe4d01005
 0,       1728,       1728,      576,       59, 0xf30f1b21
-0,       2304,       2304,     1024,       85, 0x34322b3b, S=1, Skip Samples,       10, 0x037e0095
+0,       2304,       2304,     1024,       85, 0x34322b3b, S=1, Skip Samples,       10, 0x00830016
 streams.stream.0.initial_padding=0
 streams.stream.0.start_time="0.000000"
-decoded copy: 5548 bytes, decoded source: 5292 bytes
+decoded copy: 5292 bytes, decoded source: 5292 bytes
diff --git a/tests/ref/fate/ogg-copy-trim-vorbis-cut b/tests/ref/fate/ogg-copy-trim-vorbis-cut
index a701f5298c..aaac43acc5 100644
--- a/tests/ref/fate/ogg-copy-trim-vorbis-cut
+++ b/tests/ref/fate/ogg-copy-trim-vorbis-cut
@@ -4,13 +4,13 @@
 #codec_id 0: vorbis
 #sample_rate 0: 44100
 #channel_layout_name 0: stereo
-0,       -576,       -576,      576,      267, 0x5b048bc9, S=1, Strings Metadata,      121, 0xc2832208
-0,          0,          0,     1024,      275, 0x777286f5
-0,       1024,       1024,     1024,      201, 0x0d106229
-0,       2048,       2048,     1024,      204, 0xc1766392
-0,       3072,       3072,     1024,      212, 0xde0b6eca
-0,       4096,       4096,     1024,      224, 0xdba36edc
+0,      -1330,      -1330,     1024,      248, 0x101c6ae2, S=1, Strings Metadata,      121, 0xc2832208
+0,       -306,       -306,     1024,      251, 0x80357455, S=1, Skip Samples,       10, 0x01fd0033
+0,        718,        718,     1024,      255, 0x8f756ca4
+0,       1742,       1742,     1024,      243, 0xf2b36de7
+0,       2766,       2766,     1024,      251, 0x9a657269
+0,       3790,       3790,     1024,      256, 0xce2a727f
 streams.stream.0.initial_padding=0
 streams.stream.0.start_time="0.000000"
-decoded copy: 351744 bytes, decoded source: 125240 bytes
-decoded copy: different
+decoded copy: 125240 bytes, decoded source: 125240 bytes
+decoded copy: same
diff --git a/tests/ref/fate/ogg-copy-trim-vorbis-cut-last b/tests/ref/fate/ogg-copy-trim-vorbis-cut-last
index bd2ba99aff..82093d6f5a 100644
--- a/tests/ref/fate/ogg-copy-trim-vorbis-cut-last
+++ b/tests/ref/fate/ogg-copy-trim-vorbis-cut-last
@@ -4,13 +4,9 @@
 #codec_id 0: vorbis
 #sample_rate 0: 44100
 #channel_layout_name 0: 5.1
-0,       -576,       -576,      576,      294, 0xd5816a89, S=1, Strings Metadata,       28, 0x60960600
-0,          0,          0,     1024,      314, 0x05f271fd
-0,       1024,       1024,     1024,      368, 0xbc588efa
-0,       2048,       2048,     1024,      400, 0x3dc39616
-0,       3072,       3072,     1024,      395, 0xc72d9723
-0,       4096,       4096,     1024,      399, 0xa8668fda
+0,      -1024,      -1024,     1024,       11, 0x1ae104b1, S=1, Strings Metadata,       28, 0x60960600
+0,          0,          0,     1024,       10, 0x150003d9, S=1, Skip Samples,       10, 0x017d0040
 streams.stream.0.initial_padding=0
 streams.stream.0.start_time="0.000000"
-decoded copy: 3082020 bytes, decoded source: 2052 bytes
+decoded copy: 2340 bytes, decoded source: 2052 bytes
 end of the decoded copy: same
diff --git a/tests/ref/fate/ogg-copy-trim-vorbis-cut-short b/tests/ref/fate/ogg-copy-trim-vorbis-cut-short
index f9e6116cc7..a99c05d182 100644
--- a/tests/ref/fate/ogg-copy-trim-vorbis-cut-short
+++ b/tests/ref/fate/ogg-copy-trim-vorbis-cut-short
@@ -4,13 +4,9 @@
 #codec_id 0: vorbis
 #sample_rate 0: 44100
 #channel_layout_name 0: stereo
-0,       -576,       -576,      576,      267, 0x5b048bc9, S=1, Strings Metadata,      121, 0xc2832208
-0,          0,          0,     1024,      275, 0x777286f5
-0,       1024,       1024,     1024,      201, 0x0d106229
-0,       2048,       2048,     1024,      204, 0xc1766392
-0,       3072,       3072,     1024,      212, 0xde0b6eca
-0,       4096,       4096,     1024,      224, 0xdba36edc
+0,      -1024,      -1024,     1024,      248, 0x101c6ae2, S=1, Strings Metadata,      121, 0xc2832208
+0,          0,          0,     1024,      251, 0x80357455
 streams.stream.0.initial_padding=0
 streams.stream.0.start_time="0.000000"
-decoded copy: 229376 bytes, decoded source: 125240 bytes
+decoded copy: 4096 bytes, decoded source: 125240 bytes
 start of the decoded source: different
diff --git a/tests/ref/fate/ogg-copy-trim-vorbis-edit-list b/tests/ref/fate/ogg-copy-trim-vorbis-edit-list
index 75ad60d7ee..dd3297d368 100644
--- a/tests/ref/fate/ogg-copy-trim-vorbis-edit-list
+++ b/tests/ref/fate/ogg-copy-trim-vorbis-edit-list
@@ -4,13 +4,13 @@
 #codec_id 0: vorbis
 #sample_rate 0: 44100
 #channel_layout_name 0: stereo
-0,     -45056,     -45056,     1024,      270, 0x9f588389, S=1, Strings Metadata,       62, 0xc650157d
-0,     -44032,     -44032,     1024,      259, 0xcfeb7fad
-0,     -43008,     -43008,     1024,      260, 0xf34d83b8
-0,     -41984,     -41984,     1024,      274, 0xbe048550
-0,     -40960,     -40960,     1024,      273, 0x36e68bf3
-0,     -39936,     -39936,     1024,      289, 0x0d399472
+0,      -1024,      -1024,     1024,      254, 0x01b176f0, S=1, Strings Metadata,       62, 0xc650157d
+0,          0,          0,     1024,      269, 0x8d648490
+0,       1024,       1024,     1024,      264, 0x04d88285
+0,       2048,       2048,     1024,      280, 0x5d3f91a5
+0,       3072,       3072,     1024,      272, 0x3cac8969
+0,       4096,       4096,     1024,      280, 0x77d9936b
 streams.stream.0.initial_padding=0
 streams.stream.0.start_time="0.000000"
-decoded copy: 1130496 bytes, decoded source: 954368 bytes
-decoded copy: different
+decoded copy: 954368 bytes, decoded source: 954368 bytes
+decoded copy: same
diff --git a/tests/ref/fate/ogg-copy-trim-vorbis-offset b/tests/ref/fate/ogg-copy-trim-vorbis-offset
index 590879af89..6643e069e5 100644
--- a/tests/ref/fate/ogg-copy-trim-vorbis-offset
+++ b/tests/ref/fate/ogg-copy-trim-vorbis-offset
@@ -6,12 +6,12 @@
 #channel_layout_name 0: mono
 0,       -128,       -128,      128,       28, 0xa5cb0b38, S=1, Strings Metadata,       79, 0xca1515d3
 0,          0,          0,      576,       59, 0xf30f1b21
-0,        576,        576,     1024,       85, 0x34322b3b, S=1, Skip Samples,       10, 0x037e0095
+0,        704,        704,     1024,       85, 0x34322b3b, S=1, Skip Samples,       10, 0x00830016
 streams.stream.0.initial_padding=0
 streams.stream.0.start_time="0.000000"
-decoded copy: 2902 bytes, decoded source: 2646 bytes
-decoded copy: different
+decoded copy: 2646 bytes, decoded source: 2646 bytes
+decoded copy: same
 frame|pts=0|nb_samples=576
-frame|pts=576|nb_samples=875
+frame|pts=704|nb_samples=747
 frame|pts=0|nb_samples=576
-frame|pts=576|nb_samples=875
+frame|pts=704|nb_samples=747
diff --git a/tests/ref/fate/ogg-copy-trim-vorbis-offset-end b/tests/ref/fate/ogg-copy-trim-vorbis-offset-end
index 2cfc466940..262e272c77 100644
--- a/tests/ref/fate/ogg-copy-trim-vorbis-offset-end
+++ b/tests/ref/fate/ogg-copy-trim-vorbis-offset-end
@@ -1 +1 @@
-frame|pts=0|nb_samples=576
+frame|pts=44100|nb_samples=132
diff --git a/tests/ref/fate/ogg-copy-trim-vorbis-repeated b/tests/ref/fate/ogg-copy-trim-vorbis-repeated
new file mode 100644
index 0000000000..af01010efa
--- /dev/null
+++ b/tests/ref/fate/ogg-copy-trim-vorbis-repeated
@@ -0,0 +1 @@
+copy: same
diff --git a/tests/ref/fate/ogg-copy-trim-vorbis-shifted b/tests/ref/fate/ogg-copy-trim-vorbis-shifted
index 39a030682c..77e427ce1f 100644
--- a/tests/ref/fate/ogg-copy-trim-vorbis-shifted
+++ b/tests/ref/fate/ogg-copy-trim-vorbis-shifted
@@ -1 +1 @@
-frame|pts=0|nb_samples=956
+frame|pts=0|nb_samples=824
diff --git a/tests/ref/fate/ogg-vorbis-copy-chained b/tests/ref/fate/ogg-vorbis-copy-chained
index 1e49314ee5..09a734a9e7 100644
--- a/tests/ref/fate/ogg-vorbis-copy-chained
+++ b/tests/ref/fate/ogg-vorbis-copy-chained
@@ -8,4 +8,4 @@ flags=-b-
 flags=--e
 copy headers: same
 cut headers: different
-decoded copy: different
+decoded copy: same
diff --git a/tests/ref/lavf-fate/vp8.ogg b/tests/ref/lavf-fate/vp8.ogg
index d3bbd0c6a8..87cfc57cfc 100644
--- a/tests/ref/lavf-fate/vp8.ogg
+++ b/tests/ref/lavf-fate/vp8.ogg
@@ -1,3 +1,3 @@
-8589ef98be2326f0827a0107665e785a *tests/data/lavf-fate/lavf.vp8.ogg
-95009 tests/data/lavf-fate/lavf.vp8.ogg
-tests/data/lavf-fate/lavf.vp8.ogg CRC=0x10157a66
+a91520b9a96f6b712183ef12fb739e4e *tests/data/lavf-fate/lavf.vp8.ogg
+95036 tests/data/lavf-fate/lavf.vp8.ogg
+tests/data/lavf-fate/lavf.vp8.ogg CRC=0x60597a66