]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/ogg2.c
Fix linking when RTP is disabled and libraries are dynamic
[ffmpeg] / libavformat / ogg2.c
index 05147f89fbf2dee14c6ec8a2eaddfb814555d108..3602f216756958ff16fbfb8da608344721114a7a 100644 (file)
@@ -2,7 +2,7 @@
  * Ogg bitstream support
  * Luca Barbato <lu_zero@gentoo.org>
  * Based on tcvp implementation
- * 
+ *
  */
 
 /**
@@ -41,10 +41,13 @@ static ogg_codec_t *ogg_codecs[] = {
     &vorbis_codec,
     &theora_codec,
     &flac_codec,
+    &ogm_video_codec,
+    &ogm_audio_codec,
+    &ogm_old_codec,
     NULL
 };
 
-#if 0                           // CONFIG_ENCODERS
+#if 0                           // CONFIG_MUXERS
 static int
 ogg_write_header (AVFormatContext * avfcontext)
 {
@@ -62,10 +65,10 @@ ogg_write_trailer (AVFormatContext * avfcontext)
 }
 
 
-static AVOutputFormat ogg_oformat = {
+AVOutputFormat ogg_muxer = {
     "ogg",
-    "Ogg Vorbis",
-    "audio/x-vorbis",
+    "Ogg format",
+    "application/ogg",
     "ogg",
     sizeof (OggContext),
     CODEC_ID_VORBIS,
@@ -74,7 +77,7 @@ static AVOutputFormat ogg_oformat = {
     ogg_write_packet,
     ogg_write_trailer,
 };
-#endif //CONFIG_ENCODERS
+#endif //CONFIG_MUXERS
 
 //FIXME We could avoid some structure duplication
 static int
@@ -87,6 +90,7 @@ ogg_save (AVFormatContext * s)
     ost->pos = url_ftell (&s->pb);;
     ost->curidx = ogg->curidx;
     ost->next = ogg->state;
+    ost->nstreams = ogg->nstreams;
     memcpy(ost->streams, ogg->streams, ogg->nstreams * sizeof(*ogg->streams));
 
     for (i = 0; i < ogg->nstreams; i++){
@@ -120,8 +124,9 @@ ogg_restore (AVFormatContext * s, int discard)
 
         url_fseek (bc, ost->pos, SEEK_SET);
         ogg->curidx = ost->curidx;
-        memcpy (ogg->streams, ost->streams,
-        ogg->nstreams * sizeof (*ogg->streams));
+        ogg->nstreams = ost->nstreams;
+        memcpy(ogg->streams, ost->streams,
+               ost->nstreams * sizeof(*ogg->streams));
     }
 
     av_free (ost);
@@ -190,20 +195,35 @@ ogg_new_stream (AVFormatContext * s, uint32_t serial)
     os = ogg->streams + idx;
     os->serial = serial;
     os->bufsize = DECODER_BUFFER_SIZE;
-    os->buf = av_malloc (os->bufsize);
-    memset (os->buf, 0, os->bufsize);
+    os->buf = av_malloc(os->bufsize);
     os->header = -1;
 
     st = av_new_stream (s, idx);
     if (!st)
-        return AVERROR_NOMEM;
+        return AVERROR(ENOMEM);
 
     av_set_pts_info(st, 64, 1, 1000000);
-    st->start_time = 0;
 
     return idx;
 }
 
+static int
+ogg_new_buf(ogg_t *ogg, int idx)
+{
+    ogg_stream_t *os = ogg->streams + idx;
+    uint8_t *nb = av_malloc(os->bufsize);
+    int size = os->bufpos - os->pstart;
+    if(os->buf){
+        memcpy(nb, os->buf + os->pstart, size);
+        av_free(os->buf);
+    }
+    os->buf = nb;
+    os->bufpos = size;
+    os->pstart = 0;
+
+    return 0;
+}
+
 static int
 ogg_read_page (AVFormatContext * s, int *str)
 {
@@ -217,7 +237,7 @@ ogg_read_page (AVFormatContext * s, int *str)
     uint32_t seq;
     uint32_t crc;
     int size, idx;
-    char sync[4];
+    uint8_t sync[4];
     int sp = 0;
 
     if (get_buffer (bc, sync, 4) < 4)
@@ -261,6 +281,9 @@ ogg_read_page (AVFormatContext * s, int *str)
 
     os = ogg->streams + idx;
 
+    if(os->psize > 0)
+        ogg_new_buf(ogg, idx);
+
     if (get_buffer (bc, os->segments, nsegs) < nsegs)
         return -1;
 
@@ -286,7 +309,6 @@ ogg_read_page (AVFormatContext * s, int *str)
 
     if (os->bufsize - os->bufpos < size){
         uint8_t *nb = av_malloc (os->bufsize *= 2);
-        memset (nb, 0, os->bufsize);
         memcpy (nb, os->buf, os->bufpos);
         av_free (os->buf);
         os->buf = nb;
@@ -307,7 +329,7 @@ ogg_read_page (AVFormatContext * s, int *str)
 }
 
 static int
-ogg_packet (AVFormatContext * s, int *str)
+ogg_packet (AVFormatContext * s, int *str, int *dstart, int *dsize)
 {
     ogg_t *ogg = s->priv_data;
     int idx;
@@ -360,14 +382,6 @@ ogg_packet (AVFormatContext * s, int *str)
         }
 
         if (!complete && os->segp == os->nsegs){
-            uint8_t *nb = av_malloc (os->bufsize);
-            int size = os->bufpos - os->pstart;
-            memset (nb, 0, os->bufsize);
-            memcpy (nb, os->buf + os->pstart, size);
-            av_free (os->buf);
-            os->buf = nb;
-            os->bufpos = size;
-            os->pstart = 0;
             ogg->curidx = -1;
         }
     }while (!complete);
@@ -398,6 +412,12 @@ ogg_packet (AVFormatContext * s, int *str)
             os->codec->packet (s, idx);
         if (str)
             *str = idx;
+        if (dstart)
+            *dstart = os->pstart;
+        if (dsize)
+            *dsize = os->psize;
+        os->pstart += os->psize;
+        os->psize = 0;
     }
 
     os->seq++;
@@ -413,7 +433,7 @@ ogg_get_headers (AVFormatContext * s)
     ogg_t *ogg = s->priv_data;
 
     do{
-        if (ogg_packet (s, NULL) < 0)
+        if (ogg_packet (s, NULL, NULL, NULL) < 0)
             return -1;
     }while (!ogg->headers);
 
@@ -429,15 +449,11 @@ ogg_gptopts (AVFormatContext * s, int i, uint64_t gp)
 {
     ogg_t *ogg = s->priv_data;
     ogg_stream_t *os = ogg->streams + i;
-    AVStream *st = s->streams[i];
-    AVCodecContext *codec = &st->codec;
     uint64_t pts = AV_NOPTS_VALUE;
 
     if(os->codec->gptopts){
-       pts = os->codec->gptopts(s, i, gp);
-    } else if (codec->codec_type == CODEC_TYPE_AUDIO){
-        pts = gp * 1000000LL / codec->sample_rate;
-    }else if (codec->codec_type == CODEC_TYPE_VIDEO){
+        pts = os->codec->gptopts(s, i, gp);
+    } else {
         pts = gp;
     }
 
@@ -449,21 +465,27 @@ static int
 ogg_get_length (AVFormatContext * s)
 {
     ogg_t *ogg = s->priv_data;
-    URLContext *h = url_fileno (&s->pb);
     int idx = -1, i;
-//FIXME: get the right ctx flag to know if is seekable or not
-//    if(ogg->f->flags & URL_FLAG_STREAMED)
-//  return 0;
+    offset_t size, end;
+
+    if(s->pb.is_streamed)
+        return 0;
 
 // already set
     if (s->duration != AV_NOPTS_VALUE)
         return 0;
 
+    size = url_fsize(&s->pb);
+    if(size < 0)
+        return 0;
+    end = size > MAX_PAGE_SIZE? size - MAX_PAGE_SIZE: size;
+
     ogg_save (s);
-    url_seek (h, -MAX_PAGE_SIZE, SEEK_END);
+    url_fseek (&s->pb, end, SEEK_SET);
 
     while (!ogg_read_page (s, &i)){
-        if (ogg->streams[i].granule != -1 && ogg->streams[i].granule != 0)
+        if (ogg->streams[i].granule != -1 && ogg->streams[i].granule != 0 &&
+            ogg->streams[i].codec)
             idx = i;
     }
 
@@ -472,7 +494,17 @@ ogg_get_length (AVFormatContext * s)
             ogg_gptopts (s, idx, ogg->streams[idx].granule);
     }
 
-    ogg->size = url_filesize(h);
+    ogg->size = size;
+    ogg_restore (s, 0);
+    ogg_save (s);
+    while (!ogg_read_page (s, &i)) {
+        if (i == idx && ogg->streams[i].granule != -1 && ogg->streams[i].granule != 0)
+            break;
+    }
+    if (i == idx) {
+        s->streams[idx]->start_time = ogg_gptopts (s, idx, ogg->streams[idx].granule);
+        s->streams[idx]->duration -= s->streams[idx]->start_time;
+    }
     ogg_restore (s, 0);
 
     return 0;
@@ -503,29 +535,28 @@ ogg_read_packet (AVFormatContext * s, AVPacket * pkt)
     ogg_t *ogg;
     ogg_stream_t *os;
     int idx = -1;
+    int pstart, psize;
 
-    //Get an ogg packet 
+    //Get an ogg packet
     do{
-        if (ogg_packet (s, &idx) < 0)
-            return AVERROR_IO;
+        if (ogg_packet (s, &idx, &pstart, &psize) < 0)
+            return AVERROR(EIO);
     }while (idx < 0 || !s->streams[idx]);
 
     ogg = s->priv_data;
     os = ogg->streams + idx;
 
     //Alloc a pkt
-    if (av_new_packet (pkt, os->psize) < 0)
-        return AVERROR_IO;
+    if (av_new_packet (pkt, psize) < 0)
+        return AVERROR(EIO);
     pkt->stream_index = idx;
-    memcpy (pkt->data, os->buf + os->pstart, os->psize);
+    memcpy (pkt->data, os->buf + pstart, psize);
     if (os->lastgp != -1LL){
         pkt->pts = ogg_gptopts (s, idx, os->lastgp);
         os->lastgp = -1;
     }
-    //next
-    os->pstart += os->psize;
-    os->psize = 0;
-    return os->psize;
+
+    return psize;
 }
 
 
@@ -538,7 +569,6 @@ ogg_read_close (AVFormatContext * s)
     for (i = 0; i < ogg->nstreams; i++){
         av_free (ogg->streams[i].buf);
         av_free (ogg->streams[i].private);
-        av_freep (&s->streams[i]->codec.extradata);
     }
     av_free (ogg->streams);
     return 0;
@@ -549,22 +579,26 @@ static int
 ogg_read_seek (AVFormatContext * s, int stream_index, int64_t target_ts,
                int flags)
 {
+    AVStream *st = s->streams[stream_index];
     ogg_t *ogg = s->priv_data;
     ByteIOContext *bc = &s->pb;
     uint64_t min = 0, max = ogg->size;
-    uint64_t tmin = 0, tmax = s->duration;
+    uint64_t tmin = st->start_time, tmax = st->start_time + st->duration;
     int64_t pts = AV_NOPTS_VALUE;
 
     ogg_save (s);
 
-    while (min <= max){
+    if ((uint64_t)target_ts < tmin || target_ts < 0)
+        target_ts = tmin;
+    while (min <= max && tmin < tmax){
         uint64_t p = min + (max - min) * (target_ts - tmin) / (tmax - tmin);
         int i = -1;
 
         url_fseek (bc, p, SEEK_SET);
 
         while (!ogg_read_page (s, &i)){
-            if (ogg->streams[i].granule != 0 && ogg->streams[i].granule != -1)
+            if (i == stream_index && ogg->streams[i].granule != 0 &&
+                ogg->streams[i].granule != -1)
                 break;
         }
 
@@ -574,19 +608,35 @@ ogg_read_seek (AVFormatContext * s, int stream_index, int64_t target_ts,
         pts = ogg_gptopts (s, i, ogg->streams[i].granule);
         p = url_ftell (bc);
 
-        if (ABS (pts - target_ts) < 1000000LL)
+        if (FFABS (pts - target_ts) * st->time_base.num < st->time_base.den)
             break;
 
         if (pts > target_ts){
+            if (max == p && tmax == pts) {
+                // probably our tmin is wrong, causing us to always end up too late in the file
+                tmin = (target_ts + tmin + 1) / 2;
+                if (tmin == target_ts) {
+                    url_fseek(bc, min, SEEK_SET);
+                    break;
+                }
+            }
             max = p;
             tmax = pts;
         }else{
+            if (min == p && tmin == pts) {
+                // probably our tmax is wrong, causing us to always end up too early in the file
+                tmax = (target_ts + tmax) / 2;
+                if (tmax == target_ts) {
+                    url_fseek(bc, max, SEEK_SET);
+                    break;
+                }
+            }
             min = p;
             tmin = pts;
         }
     }
 
-    if (ABS (pts - target_ts) < 1000000LL){
+    if (FFABS (pts - target_ts) * st->time_base.num < st->time_base.den){
         ogg_restore (s, 1);
         ogg_reset (ogg);
     }else{
@@ -594,7 +644,8 @@ ogg_read_seek (AVFormatContext * s, int stream_index, int64_t target_ts,
         pts = AV_NOPTS_VALUE;
     }
 
-    return pts;
+    av_update_cur_dts(s, st, pts);
+    return 0;
 
 #if 0
     //later...
@@ -623,25 +674,25 @@ ogg_read_timestamp (AVFormatContext * s, int stream_index, int64_t * pos_arg,
 }
 #endif
 
-static AVInputFormat ogg_iformat = {
+static int ogg_probe(AVProbeData *p)
+{
+    if (p->buf[0] == 'O' && p->buf[1] == 'g' &&
+        p->buf[2] == 'g' && p->buf[3] == 'S' &&
+        p->buf[4] == 0x0 && p->buf[5] <= 0x7 )
+        return AVPROBE_SCORE_MAX;
+    else
+        return 0;
+}
+
+AVInputFormat ogg_demuxer = {
     "ogg",
     "Ogg",
     sizeof (ogg_t),
-    NULL,
+    ogg_probe,
     ogg_read_header,
     ogg_read_packet,
     ogg_read_close,
     ogg_read_seek,
-// ogg_read_timestamp, 
+// ogg_read_timestamp,
     .extensions = "ogg",
 };
-
-int
-ogg_init (void)
-{
-#if 0 // CONFIG_ENCODERS
-    av_register_output_format (&ogg_oformat);
-#endif
-    av_register_input_format (&ogg_iformat);
-    return 0;
-}