]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/mpc8.c
get_bits/put_bits: K&R formatting cosmetics
[ffmpeg] / libavformat / mpc8.c
index 2634ee33b954cd818ccbfbcba989a194f4d6a343..f60a314b77fe5745739c8f15f06da86495cb83bd 100644 (file)
@@ -21,7 +21,9 @@
 
 #include "libavcodec/get_bits.h"
 #include "libavcodec/unary.h"
+#include "apetag.h"
 #include "avformat.h"
+#include "internal.h"
 #include "avio_internal.h"
 
 /// Two-byte MPC tag
@@ -47,9 +49,10 @@ static const int mpc8_rate[8] = { 44100, 48000, 37800, 32000, -1, -1, -1, -1 };
 
 typedef struct {
     int ver;
-    int frame;
     int64_t header_pos;
     int64_t samples;
+
+    int64_t apetag_start;
 } MPCContext;
 
 static inline int64_t bs_get_v(uint8_t **bs)
@@ -187,7 +190,7 @@ static void mpc8_handle_chunk(AVFormatContext *s, int tag, int64_t chunk_pos, in
     }
 }
 
-static int mpc8_read_header(AVFormatContext *s, AVFormatParameters *ap)
+static int mpc8_read_header(AVFormatContext *s)
 {
     MPCContext *c = s->priv_data;
     AVIOContext *pb = s->pb;
@@ -198,7 +201,7 @@ static int mpc8_read_header(AVFormatContext *s, AVFormatParameters *ap)
     c->header_pos = avio_tell(pb);
     if(avio_rl32(pb) != TAG_MPCK){
         av_log(s, AV_LOG_ERROR, "Not a Musepack8 file\n");
-        return -1;
+        return AVERROR_INVALIDDATA;
     }
 
     while(!pb->eof_reached){
@@ -210,23 +213,23 @@ static int mpc8_read_header(AVFormatContext *s, AVFormatParameters *ap)
     }
     if(tag != TAG_STREAMHDR){
         av_log(s, AV_LOG_ERROR, "Stream header not found\n");
-        return -1;
+        return AVERROR_INVALIDDATA;
     }
     pos = avio_tell(pb);
     avio_skip(pb, 4); //CRC
     c->ver = avio_r8(pb);
     if(c->ver != 8){
         av_log(s, AV_LOG_ERROR, "Unknown stream version %d\n", c->ver);
-        return -1;
+        return AVERROR_PATCHWELCOME;
     }
     c->samples = ffio_read_varlen(pb);
     ffio_read_varlen(pb); //silence samples at the beginning
 
-    st = av_new_stream(s, 0);
+    st = avformat_new_stream(s, NULL);
     if (!st)
         return AVERROR(ENOMEM);
     st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
-    st->codec->codec_id = CODEC_ID_MUSEPACK8;
+    st->codec->codec_id = AV_CODEC_ID_MUSEPACK8;
     st->codec->bits_per_coded_sample = 16;
 
     st->codec->extradata_size = 2;
@@ -235,10 +238,17 @@ static int mpc8_read_header(AVFormatContext *s, AVFormatParameters *ap)
 
     st->codec->channels = (st->codec->extradata[1] >> 4) + 1;
     st->codec->sample_rate = mpc8_rate[st->codec->extradata[0] >> 5];
-    av_set_pts_info(st, 32, 1152  << (st->codec->extradata[1]&3)*2, st->codec->sample_rate);
+    avpriv_set_pts_info(st, 32, 1152  << (st->codec->extradata[1]&3)*2, st->codec->sample_rate);
+    st->start_time = 0;
     st->duration = c->samples / (1152 << (st->codec->extradata[1]&3)*2);
     size -= avio_tell(pb) - pos;
 
+    if (pb->seekable) {
+        int64_t pos = avio_tell(s->pb);
+        c->apetag_start = ff_ape_parse_tag(s);
+        avio_seek(s->pb, pos, SEEK_SET);
+    }
+
     return 0;
 }
 
@@ -250,6 +260,11 @@ static int mpc8_read_packet(AVFormatContext *s, AVPacket *pkt)
 
     while(!s->pb->eof_reached){
         pos = avio_tell(s->pb);
+
+        /* don't return bogus packets with the ape tag data */
+        if (c->apetag_start && pos >= c->apetag_start)
+            return AVERROR_EOF;
+
         mpc8_get_chunk_header(s->pb, &tag, &size);
         if (size < 0)
             return -1;
@@ -257,7 +272,7 @@ static int mpc8_read_packet(AVFormatContext *s, AVPacket *pkt)
             if(av_get_packet(s->pb, pkt, size) < 0)
                 return AVERROR(ENOMEM);
             pkt->stream_index = 0;
-            pkt->pts = c->frame;
+            pkt->duration     = 1;
             return 0;
         }
         if(tag == TAG_STREAMEND)
@@ -270,12 +285,12 @@ static int mpc8_read_packet(AVFormatContext *s, AVPacket *pkt)
 static int mpc8_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
 {
     AVStream *st = s->streams[stream_index];
-    MPCContext *c = s->priv_data;
     int index = av_index_search_timestamp(st, timestamp, flags);
 
     if(index < 0) return -1;
-    avio_seek(s->pb, st->index_entries[index].pos, SEEK_SET);
-    c->frame = st->index_entries[index].timestamp;
+    if (avio_seek(s->pb, st->index_entries[index].pos, SEEK_SET) < 0)
+        return -1;
+    ff_update_cur_dts(s, st, st->index_entries[index].timestamp);
     return 0;
 }