]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/ffmdec.c
oggflac: fix bitstream reader usage
[ffmpeg] / libavformat / ffmdec.c
index 9c0ea89eb48b3c479b444a967f4e3a7f9a144d2c..345e62e6a355eb273fa8351b5db4345702e48e19 100644 (file)
@@ -24,7 +24,7 @@
 #ifdef CONFIG_FFSERVER
 #include <unistd.h>
 
-offset_t ffm_read_write_index(int fd)
+int64_t ffm_read_write_index(int fd)
 {
     uint8_t buf[8];
 
@@ -33,7 +33,7 @@ offset_t ffm_read_write_index(int fd)
     return AV_RB64(buf);
 }
 
-void ffm_write_write_index(int fd, offset_t pos)
+void ffm_write_write_index(int fd, int64_t pos)
 {
     uint8_t buf[8];
     int i;
@@ -44,7 +44,7 @@ void ffm_write_write_index(int fd, offset_t pos)
     write(fd, buf, 8);
 }
 
-void ffm_set_write_index(AVFormatContext *s, offset_t pos, offset_t file_size)
+void ffm_set_write_index(AVFormatContext *s, int64_t pos, int64_t file_size)
 {
     FFMContext *ffm = s->priv_data;
     ffm->write_index = pos;
@@ -55,7 +55,7 @@ void ffm_set_write_index(AVFormatContext *s, offset_t pos, offset_t file_size)
 static int ffm_is_avail_data(AVFormatContext *s, int size)
 {
     FFMContext *ffm = s->priv_data;
-    offset_t pos, avail_size;
+    int64_t pos, avail_size;
     int len;
 
     len = ffm->packet_end - ffm->packet_ptr;
@@ -99,8 +99,7 @@ static int ffm_read_data(AVFormatContext *s,
     retry_read:
             get_be16(pb); /* PACKET_ID */
             fill_size = get_be16(pb);
-            ffm->pts = get_be64(pb);
-            ffm->first_frame_in_packet = 1;
+            ffm->dts = get_be64(pb);
             frame_offset = get_be16(pb);
             get_buffer(pb, ffm->packet, ffm->packet_size - FFM_HEADER_SIZE);
             ffm->packet_end = ffm->packet + (ffm->packet_size - FFM_HEADER_SIZE - fill_size);
@@ -142,11 +141,11 @@ static int ffm_read_data(AVFormatContext *s,
 
 /* pos is between 0 and file_size - FFM_PACKET_SIZE. It is translated
    by the write position inside this function */
-static void ffm_seek1(AVFormatContext *s, offset_t pos1)
+static void ffm_seek1(AVFormatContext *s, int64_t pos1)
 {
     FFMContext *ffm = s->priv_data;
     ByteIOContext *pb = s->pb;
-    offset_t pos;
+    int64_t pos;
 
     pos = pos1 + ffm->write_index;
     if (pos >= ffm->file_size)
@@ -157,18 +156,18 @@ static void ffm_seek1(AVFormatContext *s, offset_t pos1)
     url_fseek(pb, pos, SEEK_SET);
 }
 
-static int64_t get_pts(AVFormatContext *s, offset_t pos)
+static int64_t get_dts(AVFormatContext *s, int64_t pos)
 {
     ByteIOContext *pb = s->pb;
-    int64_t pts;
+    int64_t dts;
 
     ffm_seek1(s, pos);
     url_fskip(pb, 4);
-    pts = get_be64(pb);
+    dts = get_be64(pb);
 #ifdef DEBUG_SEEK
     av_log(s, AV_LOG_DEBUG, "pts=%0.6f\n", pts / 1000000.0);
 #endif
-    return pts;
+    return dts;
 }
 
 static void adjust_write_index(AVFormatContext *s)
@@ -176,31 +175,31 @@ static void adjust_write_index(AVFormatContext *s)
     FFMContext *ffm = s->priv_data;
     ByteIOContext *pb = s->pb;
     int64_t pts;
-    //offset_t orig_write_index = ffm->write_index;
-    offset_t pos_min, pos_max;
+    //int64_t orig_write_index = ffm->write_index;
+    int64_t pos_min, pos_max;
     int64_t pts_start;
-    offset_t ptr = url_ftell(pb);
+    int64_t ptr = url_ftell(pb);
 
 
     pos_min = 0;
     pos_max = ffm->file_size - 2 * FFM_PACKET_SIZE;
 
-    pts_start = get_pts(s, pos_min);
+    pts_start = get_dts(s, pos_min);
 
-    pts = get_pts(s, pos_max);
+    pts = get_dts(s, pos_max);
 
     if (pts - 100000 > pts_start)
         goto end;
 
     ffm->write_index = FFM_PACKET_SIZE;
 
-    pts_start = get_pts(s, pos_min);
+    pts_start = get_dts(s, pos_min);
 
-    pts = get_pts(s, pos_max);
+    pts = get_dts(s, pos_max);
 
     if (pts - 100000 <= pts_start) {
         while (1) {
-            offset_t newpos;
+            int64_t newpos;
             int64_t newpts;
 
             newpos = ((pos_max + pos_min) / (2 * FFM_PACKET_SIZE)) * FFM_PACKET_SIZE;
@@ -208,7 +207,7 @@ static void adjust_write_index(AVFormatContext *s)
             if (newpos == pos_min)
                 break;
 
-            newpts = get_pts(s, newpos);
+            newpts = get_dts(s, newpos);
 
             if (newpts - 100000 <= pts) {
                 pos_max = newpos;
@@ -221,7 +220,7 @@ static void adjust_write_index(AVFormatContext *s)
     }
 
     //printf("Adjusted write index from %"PRId64" to %"PRId64": pts=%0.6f\n", orig_write_index, ffm->write_index, pts / 1000000.);
-    //printf("pts range %0.6f - %0.6f\n", get_pts(s, 0) / 1000000. , get_pts(s, ffm->file_size - 2 * FFM_PACKET_SIZE) / 1000000. );
+    //printf("pts range %0.6f - %0.6f\n", get_dts(s, 0) / 1000000. , get_dts(s, ffm->file_size - 2 * FFM_PACKET_SIZE) / 1000000. );
 
  end:
     url_fseek(pb, ptr, SEEK_SET);
@@ -311,6 +310,7 @@ static int ffm_read_header(AVFormatContext *s, AVFormatParameters *ap)
             codec->frame_skip_cmp = get_be32(pb);
             codec->rc_buffer_aggressivity = av_int2dbl(get_be64(pb));
             codec->codec_tag = get_be32(pb);
+            codec->thread_count = get_byte(pb);
             break;
         case CODEC_TYPE_AUDIO:
             codec->sample_rate = get_be32(pb);
@@ -320,7 +320,13 @@ static int ffm_read_header(AVFormatContext *s, AVFormatParameters *ap)
         default:
             goto fail;
         }
-
+        if (codec->flags & CODEC_FLAG_GLOBAL_HEADER) {
+            codec->extradata_size = get_be32(pb);
+            codec->extradata = av_malloc(codec->extradata_size);
+            if (!codec->extradata)
+                return AVERROR(ENOMEM);
+            get_buffer(pb, codec->extradata, codec->extradata_size);
+        }
     }
 
     /* get until end of block reached */
@@ -331,7 +337,7 @@ static int ffm_read_header(AVFormatContext *s, AVFormatParameters *ap)
     ffm->packet_ptr = ffm->packet;
     ffm->packet_end = ffm->packet;
     ffm->frame_offset = 0;
-    ffm->pts = 0;
+    ffm->dts = 0;
     ffm->read_state = READ_HEADER;
     ffm->first_packet = 1;
     return 0;
@@ -339,7 +345,6 @@ static int ffm_read_header(AVFormatContext *s, AVFormatParameters *ap)
     for(i=0;i<s->nb_streams;i++) {
         st = s->streams[i];
         if (st) {
-            av_freep(&st->priv_data);
             av_free(st);
         }
     }
@@ -355,14 +360,17 @@ static int ffm_read_packet(AVFormatContext *s, AVPacket *pkt)
 
     switch(ffm->read_state) {
     case READ_HEADER:
-        if (!ffm_is_avail_data(s, FRAME_HEADER_SIZE)) {
+        if (!ffm_is_avail_data(s, FRAME_HEADER_SIZE+4)) {
             return AVERROR(EAGAIN);
         }
         dprintf(s, "pos=%08"PRIx64" spos=%"PRIx64", write_index=%"PRIx64" size=%"PRIx64"\n",
-               url_ftell(s->pb), s->pb.pos, ffm->write_index, ffm->file_size);
+               url_ftell(s->pb), s->pb->pos, ffm->write_index, ffm->file_size);
         if (ffm_read_data(s, ffm->header, FRAME_HEADER_SIZE, 1) !=
             FRAME_HEADER_SIZE)
             return AVERROR(EAGAIN);
+        if (ffm->header[1] & FLAG_DTS)
+            if (ffm_read_data(s, ffm->header+16, 4, 1) != 4)
+                return AVERROR(EAGAIN);
 #if 0
         av_hexdump_log(s, AV_LOG_DEBUG, ffm->header, FRAME_HEADER_SIZE);
 #endif
@@ -394,11 +402,11 @@ static int ffm_read_packet(AVFormatContext *s, AVPacket *pkt)
             av_free_packet(pkt);
             return AVERROR(EAGAIN);
         }
-        if (ffm->first_frame_in_packet)
-        {
-            pkt->pts = ffm->pts;
-            ffm->first_frame_in_packet = 0;
-        }
+        pkt->pts = AV_RB64(ffm->header+8);
+        if (ffm->header[1] & FLAG_DTS)
+            pkt->dts = pkt->pts - AV_RB32(ffm->header+16);
+        else
+            pkt->dts = pkt->pts;
         pkt->duration = duration;
         break;
     }
@@ -411,7 +419,7 @@ static int ffm_read_packet(AVFormatContext *s, AVPacket *pkt)
 static int ffm_seek(AVFormatContext *s, int stream_index, int64_t wanted_pts, int flags)
 {
     FFMContext *ffm = s->priv_data;
-    offset_t pos_min, pos_max, pos;
+    int64_t pos_min, pos_max, pos;
     int64_t pts_min, pts_max, pts;
     double pos1;
 
@@ -423,8 +431,8 @@ static int ffm_seek(AVFormatContext *s, int stream_index, int64_t wanted_pts, in
     pos_min = 0;
     pos_max = ffm->file_size - 2 * FFM_PACKET_SIZE;
     while (pos_min <= pos_max) {
-        pts_min = get_pts(s, pos_min);
-        pts_max = get_pts(s, pos_max);
+        pts_min = get_dts(s, pos_min);
+        pts_max = get_dts(s, pos_max);
         /* linear interpolation */
         pos1 = (double)(pos_max - pos_min) * (double)(wanted_pts - pts_min) /
             (double)(pts_max - pts_min);
@@ -433,7 +441,7 @@ static int ffm_seek(AVFormatContext *s, int stream_index, int64_t wanted_pts, in
             pos = pos_min;
         else if (pos >= pos_max)
             pos = pos_max;
-        pts = get_pts(s, pos);
+        pts = get_dts(s, pos);
         /* check if we are lucky */
         if (pts == wanted_pts) {
             goto found;
@@ -458,18 +466,6 @@ static int ffm_seek(AVFormatContext *s, int stream_index, int64_t wanted_pts, in
     return 0;
 }
 
-static int ffm_read_close(AVFormatContext *s)
-{
-    AVStream *st;
-    int i;
-
-    for(i=0;i<s->nb_streams;i++) {
-        st = s->streams[i];
-        av_freep(&st->priv_data);
-    }
-    return 0;
-}
-
 static int ffm_probe(AVProbeData *p)
 {
     if (
@@ -481,11 +477,11 @@ static int ffm_probe(AVProbeData *p)
 
 AVInputFormat ffm_demuxer = {
     "ffm",
-    NULL_IF_CONFIG_SMALL("ffm format"),
+    NULL_IF_CONFIG_SMALL("FFM (FFserver live feed) format"),
     sizeof(FFMContext),
     ffm_probe,
     ffm_read_header,
     ffm_read_packet,
-    ffm_read_close,
+    NULL,
     ffm_seek,
 };