]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/oggdec.c
Factor tag reading code out of wav read_header()
[ffmpeg] / libavformat / oggdec.c
index bdbbf3f081277ab3f4d6ee5ff56a9be86b923656..4fcf8ad58c83dca4c02cf6024aa485f46fd6dd06 100644 (file)
@@ -33,6 +33,7 @@
 #include <stdio.h>
 #include "oggdec.h"
 #include "avformat.h"
+#include "vorbiscomment.h"
 
 #define MAX_PAGE_SIZE 65307
 #define DECODER_BUFFER_SIZE MAX_PAGE_SIZE
@@ -361,24 +362,43 @@ ogg_packet (AVFormatContext * s, int *str, int *dstart, int *dsize, int64_t *fpo
             idx, os->psize, os->pstart);
 #endif
 
+    if (os->granule == -1)
+        av_log(s, AV_LOG_WARNING, "Page at %"PRId64" is missing granule\n", os->page_pos);
+
     ogg->curidx = idx;
     os->incomplete = 0;
 
-    if (!ogg->headers){
-        int hdr = os->codec->header (s, idx);
-        os->header = os->seq;
-        if (!hdr){
+    if (os->header) {
+        os->header = os->codec->header (s, idx);
+        if (!os->header){
             os->segp = segp;
             os->psize = psize;
+
+            // We have reached the first non-header packet. All header
+            // packets must be complete before the first non-header
+            // one, so everything that follows must be non-header.
             ogg->headers = 1;
+
+            // Update the header state for all streams and
+            // compute the data_offset.
             s->data_offset = os->sync_pos;
+            for (i = 0; i < ogg->nstreams; i++) {
+                struct ogg_stream *cur_os = ogg->streams + i;
+                // Set stream header state to 0 if its last packet
+                // was a header.
+                if (cur_os->header > 0)
+                    cur_os->header = 0;
+
+                // if we have a partial non-header packet, its start is
+                // obviously at or after the data start
+                if (cur_os->incomplete)
+                    s->data_offset = FFMIN(s->data_offset, cur_os->sync_pos);
+            }
         }else{
             os->pstart += os->psize;
             os->psize = 0;
         }
-    }
-
-    if (os->header > -1 && os->seq > os->header){
+    } else {
         os->pflags = 0;
         os->pduration = 0;
         if (os->codec && os->codec->packet)
@@ -405,7 +425,6 @@ ogg_packet (AVFormatContext * s, int *str, int *dstart, int *dsize, int64_t *fpo
             break;
         }
 
-    os->seq++;
     if (os->segp == os->nsegs)
         ogg->curidx = -1;
 
@@ -433,7 +452,7 @@ static int
 ogg_get_length (AVFormatContext * s)
 {
     struct ogg *ogg = s->priv_data;
-    int idx = -1, i;
+    int i;
     int64_t size, end;
 
     if(url_is_streamed(s->pb))
@@ -453,18 +472,14 @@ ogg_get_length (AVFormatContext * s)
 
     while (!ogg_read_page (s, &i)){
         if (ogg->streams[i].granule != -1 && ogg->streams[i].granule != 0 &&
-            ogg->streams[i].codec)
-            idx = i;
-    }
-
-    if (idx != -1){
-        s->streams[idx]->duration =
-            ogg_gptopts (s, idx, ogg->streams[idx].granule, NULL);
-        if (s->streams[idx]->start_time != AV_NOPTS_VALUE)
-            s->streams[idx]->duration -= s->streams[idx]->start_time;
+            ogg->streams[i].codec) {
+            s->streams[i]->duration =
+                ogg_gptopts (s, i, ogg->streams[i].granule, NULL);
+            if (s->streams[i]->start_time != AV_NOPTS_VALUE)
+                s->streams[i]->duration -= s->streams[i]->start_time;
+        }
     }
 
-    ogg->size = size;
     ogg_restore (s, 0);
 
     return 0;
@@ -493,6 +508,35 @@ ogg_read_header (AVFormatContext * s, AVFormatParameters * ap)
     return 0;
 }
 
+static int64_t ogg_calc_pts(AVFormatContext *s, int idx, int64_t *dts)
+{
+    struct ogg *ogg = s->priv_data;
+    struct ogg_stream *os = ogg->streams + idx;
+    int64_t pts = AV_NOPTS_VALUE;
+
+    if (dts)
+        *dts = AV_NOPTS_VALUE;
+
+    if (os->lastpts != AV_NOPTS_VALUE) {
+        pts = os->lastpts;
+        os->lastpts = AV_NOPTS_VALUE;
+    }
+    if (os->lastdts != AV_NOPTS_VALUE) {
+        if (dts)
+            *dts = os->lastdts;
+        os->lastdts = AV_NOPTS_VALUE;
+    }
+    if (os->page_end) {
+        if (os->granule != -1LL) {
+            if (os->codec && os->codec->granule_is_start)
+                pts = ogg_gptopts(s, idx, os->granule, dts);
+            else
+                os->lastpts = ogg_gptopts(s, idx, os->granule, &os->lastdts);
+            os->granule = -1LL;
+        }
+    }
+    return pts;
+}
 
 static int
 ogg_read_packet (AVFormatContext * s, AVPacket * pkt)
@@ -501,9 +545,10 @@ ogg_read_packet (AVFormatContext * s, AVPacket * pkt)
     struct ogg_stream *os;
     int idx = -1;
     int pstart, psize;
-    int64_t fpos;
+    int64_t fpos, pts, dts;
 
     //Get an ogg packet
+retry:
     do{
         if (ogg_packet (s, &idx, &pstart, &psize, &fpos) < 0)
             return AVERROR(EIO);
@@ -512,31 +557,21 @@ ogg_read_packet (AVFormatContext * s, AVPacket * pkt)
     ogg = s->priv_data;
     os = ogg->streams + idx;
 
+    // pflags might not be set until after this
+    pts = ogg_calc_pts(s, idx, &dts);
+
+    if (os->keyframe_seek && !(os->pflags & AV_PKT_FLAG_KEY))
+        goto retry;
+    os->keyframe_seek = 0;
+
     //Alloc a pkt
     if (av_new_packet (pkt, psize) < 0)
         return AVERROR(EIO);
     pkt->stream_index = idx;
     memcpy (pkt->data, os->buf + pstart, psize);
 
-    if (os->lastpts != AV_NOPTS_VALUE) {
-        pkt->pts = os->lastpts;
-        os->lastpts = AV_NOPTS_VALUE;
-    }
-    if (os->lastdts != AV_NOPTS_VALUE) {
-        pkt->dts = os->lastdts;
-        os->lastdts = AV_NOPTS_VALUE;
-    }
-    if (os->page_end) {
-        if (os->granule != -1LL) {
-            if (os->codec && os->codec->granule_is_start)
-                pkt->pts    = ogg_gptopts(s, idx, os->granule, &pkt->dts);
-            else
-                os->lastpts = ogg_gptopts(s, idx, os->granule, &os->lastdts);
-            os->granule = -1LL;
-        } else
-            av_log(s, AV_LOG_WARNING, "Packet is missing granule\n");
-    }
-
+    pkt->pts = pts;
+    pkt->dts = dts;
     pkt->flags = os->pflags;
     pkt->duration = os->pduration;
     pkt->pos = fpos;
@@ -565,24 +600,44 @@ ogg_read_timestamp (AVFormatContext * s, int stream_index, int64_t * pos_arg,
                     int64_t pos_limit)
 {
     struct ogg *ogg = s->priv_data;
+    struct ogg_stream *os = ogg->streams + stream_index;
     ByteIOContext *bc = s->pb;
     int64_t pts = AV_NOPTS_VALUE;
     int i;
     url_fseek(bc, *pos_arg, SEEK_SET);
-    while (url_ftell(bc) < pos_limit && !ogg_read_page (s, &i)) {
-        if (ogg->streams[i].granule != -1 && ogg->streams[i].granule != 0 &&
-            ogg->streams[i].codec && i == stream_index) {
-            pts = ogg_gptopts(s, i, ogg->streams[i].granule, NULL);
-            // FIXME: this is the position of the packet after the one with above
-            // pts.
-            *pos_arg = url_ftell(bc);
-            break;
+    ogg_reset(ogg);
+
+    while (url_ftell(bc) < pos_limit && !ogg_packet(s, &i, NULL, NULL, pos_arg)) {
+        if (i == stream_index) {
+            pts = ogg_calc_pts(s, i, NULL);
+            if (os->keyframe_seek && !(os->pflags & AV_PKT_FLAG_KEY))
+                pts = AV_NOPTS_VALUE;
         }
+        if (pts != AV_NOPTS_VALUE)
+            break;
     }
     ogg_reset(ogg);
     return pts;
 }
 
+static int ogg_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
+{
+    struct ogg *ogg = s->priv_data;
+    struct ogg_stream *os = ogg->streams + stream_index;
+    int ret;
+
+    // Try seeking to a keyframe first. If this fails (very possible),
+    // av_seek_frame will fall back to ignoring keyframes
+    if (s->streams[stream_index]->codec->codec_type == AVMEDIA_TYPE_VIDEO
+        && !(flags & AVSEEK_FLAG_ANY))
+        os->keyframe_seek = 1;
+
+    ret = av_seek_frame_binary(s, stream_index, timestamp, flags);
+    if (ret < 0)
+        os->keyframe_seek = 0;
+    return ret;
+}
+
 static int ogg_probe(AVProbeData *p)
 {
     if (p->buf[0] == 'O' && p->buf[1] == 'g' &&
@@ -601,8 +656,8 @@ AVInputFormat ogg_demuxer = {
     ogg_read_header,
     ogg_read_packet,
     ogg_read_close,
-    NULL,
+    ogg_read_seek,
     ogg_read_timestamp,
     .extensions = "ogg",
-    .metadata_conv = ff_vorbiscomment_metadata_conv,
+    .flags = AVFMT_GENERIC_INDEX,
 };