This is an automated email from the git hooks/post-receive script.

Git pushed a commit to branch master
in repository ffmpeg.

commit 08c9af5730b84e6b562802baf1f3070efe502f0c
Author:     Romain Beauxis <[email protected]>
AuthorDate: Sun Nov 22 08:35:23 2026 -0500
Commit:     Romain Beauxis <[email protected]>
CommitDate: Mon Nov 30 09:49:26 2026 -0600

    avformat/ogg/demuxer/vorbis: trim the samples before time zero
    
    The granule position of the first audio page places the first packet. A
    negative start time means the samples before time zero are discarded
    (Vorbis I, A.2), which was ignored: the stream was assumed to start at
    zero and played the trimmed samples.
    
    Derive the timestamp of the first packet from the first page, unless it
    is also the last one, and trim the samples before zero from the packet
    following it, the priming packet of the link returning none. A larger
    trim, which A.2 does not allow, is ignored. The page where the primer
    starts is indexed at zero so that seeking there does not skip it.
---
 libavformat/ogg/demuxer/vorbis.c            | 68 +++++++++++++++++++++++------
 tests/ref/fate/ogg-vorbis-start-trim-rewind | 12 ++---
 2 files changed, 60 insertions(+), 20 deletions(-)

diff --git a/libavformat/ogg/demuxer/vorbis.c b/libavformat/ogg/demuxer/vorbis.c
index eec067dcc1..23aa5e0dab 100644
--- a/libavformat/ogg/demuxer/vorbis.c
+++ b/libavformat/ogg/demuxer/vorbis.c
@@ -69,6 +69,8 @@ struct oggvorbis_private {
     int ts_offset;
     int64_t last_page_granule;
     int eos_page_duration;
+    int start_trim; ///< samples before time zero, trimmed after the priming 
packet
+    int initial_link_started; ///< the first data page of the initial link was 
read
 };
 
 static int fixup_vorbis_headers(AVFormatContext *as,
@@ -349,15 +351,18 @@ static int vorbis_check_packet_order(AVFormatContext *s,
 
 /**
  * Timestamp of the current packet, the first one of its page, from the page
- * granule and the durations of the packets the page completes.
+ * granule and the durations of the packets the page completes. If requested,
+ * also report the duration of the next packet, if it completes on this page.
  */
 static int64_t vorbis_page_start_pts(struct ogg_stream *os,
-                                     struct oggvorbis_private *priv)
+                                     struct oggvorbis_private *priv,
+                                     int *next_duration)
 {
     int64_t page_duration = os->pduration;
     int flags = 0;
-    uint8_t *last_pkt = os->buf + os->pstart + os->psize;
-    uint8_t *next_pkt = last_pkt;
+    uint8_t *first_next = os->buf + os->pstart + os->psize;
+    uint8_t *last_pkt = first_next;
+    uint8_t *next_pkt = first_next;
 
     for (int seg = os->segp; seg < os->nsegs; seg++) {
         next_pkt += os->segments[seg];
@@ -368,6 +373,8 @@ static int64_t vorbis_page_start_pts(struct ogg_stream *os,
                 page_duration = os->granule;
                 break;
             }
+            if (next_duration && last_pkt == first_next)
+                *next_duration = d;
             page_duration += d;
             last_pkt = next_pkt;
         }
@@ -400,11 +407,19 @@ static int vorbis_packet(AVFormatContext *s, int idx)
 
     bos = os->page_start && os->page_pos == priv->bos_page_pos;
 
-    if (bos || (os->page_start && os->lastpts == AV_NOPTS_VALUE))
+    if (bos || (os->page_start && os->lastpts == AV_NOPTS_VALUE)) {
         av_vorbis_parse_reset(priv->vp);
+        priv->start_trim = 0;
+    }
     if (bos)
         priv->last_page_granule = 0;
 
+    /* The priming packet returns no samples, so the packet after it carries
+     * the start trim. It is on the same page: a seek may read the priming
+     * packet alone, then resume anywhere. */
+    os->start_trimming = os->page_pos == priv->bos_page_pos ? priv->start_trim 
: 0;
+    priv->start_trim   = 0;
+
     duration = av_vorbis_parse_frame_flags(priv->vp, os->buf + os->pstart, 1, 
&flags);
     if (duration < 0) {
         os->pflags |= AV_PKT_FLAG_CORRUPT;
@@ -422,20 +437,45 @@ static int vorbis_packet(AVFormatContext *s, int idx)
     /* Initial position shift to account for encoder delay, or page scan
      * to recover the page start timestamp after a seek. */
     if (bos) {
-        if (os->lastpts != AV_NOPTS_VALUE) {
-            if (s->streams[idx]->start_time == AV_NOPTS_VALUE) {
-                s->streams[idx]->start_time = os->lastpts;
-                if (s->streams[idx]->duration != AV_NOPTS_VALUE)
-                    s->streams[idx]->duration -= s->streams[idx]->start_time;
+        int64_t first_pts = AV_NOPTS_VALUE;
+        int64_t trim = 0;
+        int overlap = 0;
+
+        if (!(os->flags & OGG_FLAG_EOS) && (int64_t)os->granule > 0)
+            first_pts = vorbis_page_start_pts(os, priv, &overlap);
+        if (first_pts != AV_NOPTS_VALUE)
+            trim = -first_pts - os->pduration;
+
+        /* Vorbis I A.2 limits start trim to the overlap of the first
+         * two audio packets. Do not discard later packets on a bad granule. */
+        if (trim > 0 && trim <= overlap) {
+            priv->start_trim = trim;
+            os->lastpts =
+            os->lastdts = first_pts;
+            if (s->streams[idx]->start_time == AV_NOPTS_VALUE)
+                s->streams[idx]->start_time = 0;
+            /* The packets of this page are indexed before zero, and the
+             * primer may start on an earlier page. */
+            if (!priv->initial_link_started)
+                av_add_index_entry(s->streams[idx], os->sync_pos, 0, 0, 0,
+                                   AVINDEX_KEYFRAME);
+        } else {
+            if (os->lastpts != AV_NOPTS_VALUE) {
+                if (s->streams[idx]->start_time == AV_NOPTS_VALUE) {
+                    s->streams[idx]->start_time = os->lastpts;
+                    if (s->streams[idx]->duration != AV_NOPTS_VALUE)
+                        s->streams[idx]->duration -= 
s->streams[idx]->start_time;
+                }
+                os->lastpts -= priv->ts_offset;
             }
-            os->lastpts -= priv->ts_offset;
+            if (os->lastdts != AV_NOPTS_VALUE)
+                os->lastdts -= priv->ts_offset;
         }
-        if (os->lastdts != AV_NOPTS_VALUE)
-            os->lastdts -= priv->ts_offset;
+        priv->initial_link_started = 1;
     } else if (os->page_start && os->lastpts == AV_NOPTS_VALUE &&
                !(os->flags & OGG_FLAG_EOS) && (int64_t)os->granule >= 0) {
         os->lastpts =
-        os->lastdts = vorbis_page_start_pts(os, priv);
+        os->lastdts = vorbis_page_start_pts(os, priv, NULL);
     }
 
     /* Broken files may have granule=0 on pages that contain audio data.
diff --git a/tests/ref/fate/ogg-vorbis-start-trim-rewind 
b/tests/ref/fate/ogg-vorbis-start-trim-rewind
index dcda42a556..9489ffc051 100644
--- a/tests/ref/fate/ogg-vorbis-start-trim-rewind
+++ b/tests/ref/fate/ogg-vorbis-start-trim-rewind
@@ -1,10 +1,10 @@
-packet|pts=-128
-packet|pts=0
-frame|pts=0|nb_samples=576
+packet|pts=-228
+packet|pts=-100
+frame|pts=0|nb_samples=476
 packet|pts=476
 frame|pts=476|nb_samples=1024
-packet|pts=-128
-packet|pts=0
-frame|pts=0|nb_samples=576
+packet|pts=-228
+packet|pts=-100
+frame|pts=0|nb_samples=476
 packet|pts=476
 frame|pts=476|nb_samples=1024

-- 
To stop receiving notification emails like this one, please contact
[email protected].
_______________________________________________
ffmpeg-cvslog mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to