The decoded data from the first frame is not returned; it is used to
prime the decode engine. The container should therefore treat the
first packet's duration as zero for trimming so total duration is
correct.

When the first and last packet are in the same page, the last packet's
duration was previously wrong because the first packet's duration was
included. Ignoring the first packet's duration in the container fixes
this.

Fixes: https://trac.ffmpeg.org/ticket/6367

The fate ref ogg-vorbis-chained-meta.txt is updated because the fix
changes the first packet's duration (and thus PTS) for the container;
the test output now reflects the corrected timestamps. NOPTS for the
last packet of the first chain is expected (the Ogg demuxer assigns the
page granule to the next packet, not the current one).

Signed-off-by: Guangyu Sun <[email protected]>
---
 libavformat/oggparsevorbis.c               | 14 +++++++++++++-
 tests/ref/fate/ogg-vorbis-chained-meta.txt | 20 ++++++++++----------
 2 files changed, 23 insertions(+), 11 deletions(-)

diff --git a/libavformat/oggparsevorbis.c b/libavformat/oggparsevorbis.c
index ed81a431f6..599bc53499 100644
--- a/libavformat/oggparsevorbis.c
+++ b/libavformat/oggparsevorbis.c
@@ -454,6 +454,7 @@ static int vorbis_packet(AVFormatContext *s, int idx)
     struct oggvorbis_private *priv = os->private;
     int duration, flags = 0;
     int skip_packet = 0;
+    int first_page;
     int ret, new_extradata_size;
     PutByteContext pb;
 
@@ -464,7 +465,8 @@ static int vorbis_packet(AVFormatContext *s, int idx)
      * here we parse the duration of each packet in the first page and compare
      * the total duration to the page granule to find the encoder delay and
      * set the first timestamp */
-    if ((!os->lastpts || os->lastpts == AV_NOPTS_VALUE) && !(os->flags & 
OGG_FLAG_EOS) && (int64_t)os->granule>=0) {
+    first_page = !os->lastpts || os->lastpts == AV_NOPTS_VALUE;
+    if (first_page && !(os->flags & OGG_FLAG_EOS) && (int64_t)os->granule>=0) {
         int seg, d;
         uint8_t *last_pkt  = os->buf + os->pstart;
         uint8_t *next_pkt  = last_pkt;
@@ -563,6 +565,10 @@ static int vorbis_packet(AVFormatContext *s, int idx)
         }
 
         os->pduration = duration;
+        /* First frame only primes the decoder; no samples are output.
+         * Use zero duration for the container so trimming is correct. */
+        if (first_page && os->segp == 1)
+            os->pduration = 0;
     }
 
     /* final packet handling
diff --git a/tests/ref/fate/ogg-vorbis-chained-meta.txt 
b/tests/ref/fate/ogg-vorbis-chained-meta.txt
index 5ec756663d..c4ec3cd7c1 100644
--- a/tests/ref/fate/ogg-vorbis-chained-meta.txt
+++ b/tests/ref/fate/ogg-vorbis-chained-meta.txt
@@ -1,14 +1,14 @@
 Stream ID: 0, codec name: vorbis, metadata: encoder=Lavc61.19.100 
libvorbis:title=First Stream
 Stream ID: 0, packet PTS: 0, packet DTS: 0
 Stream ID: 0, new metadata: encoder=Lavc61.19.100 libvorbis:title=First Stream
-Stream ID: 0, packet PTS: 128, packet DTS: 128
-Stream ID: 0, frame PTS: 128, metadata: N/A
-Stream ID: 0, packet PTS: 704, packet DTS: 704
-Stream ID: 0, frame PTS: 704, metadata: N/A
-Stream ID: 0, packet PTS: 1323, packet DTS: 1323
+Stream ID: 0, packet PTS: 0, packet DTS: 0
+Stream ID: 0, frame PTS: 0, metadata: N/A
+Stream ID: 0, packet PTS: 576, packet DTS: 576
+Stream ID: 0, frame PTS: 576, metadata: N/A
+Stream ID: 0, packet PTS: NOPTS, packet DTS: NOPTS
 Stream ID: 0, new metadata: encoder=Lavc61.19.100 libvorbis:title=Second Stream
-Stream ID: 0, frame PTS: 1323, metadata: encoder=Lavc61.19.100 
libvorbis:title=Second Stream
-Stream ID: 0, packet PTS: 1451, packet DTS: 1451
-Stream ID: 0, frame PTS: 1451, metadata: N/A
-Stream ID: 0, packet PTS: 2027, packet DTS: 2027
-Stream ID: 0, frame PTS: 2027, metadata: N/A
+Stream ID: 0, frame PTS: NOPTS, metadata: encoder=Lavc61.19.100 
libvorbis:title=Second Stream
+Stream ID: 0, packet PTS: 1323, packet DTS: 1323
+Stream ID: 0, frame PTS: 1323, metadata: N/A
+Stream ID: 0, packet PTS: 1899, packet DTS: 1899
+Stream ID: 0, frame PTS: 1899, metadata: N/A
_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to