]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/mp3dec.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavformat / mp3dec.c
index 31d64b2921f5fb770afb03bcd91708f2e7674c71..61405f71b73fc47f6c1b4211179a0d9530bfcbf2 100644 (file)
 #include "id3v1.h"
 #include "libavcodec/mpegaudiodecheader.h"
 
+typedef struct {
+    int64_t filesize;
+} MP3Context;
+
 /* mp3 read */
 
 static int mp3_read_probe(AVProbeData *p)
@@ -136,6 +140,7 @@ static int mp3_parse_vbr_tags(AVFormatContext *s, AVStream *st, int64_t base)
 
 static int mp3_read_header(AVFormatContext *s)
 {
+    MP3Context *mp3 = s->priv_data;
     AVStream *st;
     int64_t off;
 
@@ -157,6 +162,9 @@ static int mp3_read_header(AVFormatContext *s)
     if (!av_dict_get(s->metadata, "", NULL, AV_DICT_IGNORE_SUFFIX))
         ff_id3v1_read(s);
 
+    if(s->pb->seekable)
+        mp3->filesize = avio_size(s->pb);
+
     if (mp3_parse_vbr_tags(s, st, off) < 0)
         avio_seek(s->pb, off, SEEK_SET);
 
@@ -168,13 +176,19 @@ static int mp3_read_header(AVFormatContext *s)
 
 static int mp3_read_packet(AVFormatContext *s, AVPacket *pkt)
 {
+    MP3Context *mp3 = s->priv_data;
     int ret, size;
+    int64_t pos;
     //    AVStream *st = s->streams[0];
 
     size= MP3_PACKET_SIZE;
+    pos = avio_tell(s->pb);
+    if(mp3->filesize > ID3v1_TAG_SIZE && pos < mp3->filesize)
+        size= FFMIN(size, mp3->filesize - pos);
 
     ret= av_get_packet(s->pb, pkt, size);
 
+    pkt->flags &= ~AV_PKT_FLAG_CORRUPT;
     pkt->stream_index = 0;
     if (ret <= 0) {
         if(ret<0)
@@ -182,7 +196,7 @@ static int mp3_read_packet(AVFormatContext *s, AVPacket *pkt)
         return AVERROR_EOF;
     }
 
-    if (ret > ID3v1_TAG_SIZE &&
+    if (ret >= ID3v1_TAG_SIZE &&
         memcmp(&pkt->data[ret - ID3v1_TAG_SIZE], "TAG", 3) == 0)
         ret -= ID3v1_TAG_SIZE;
 
@@ -195,6 +209,7 @@ static int mp3_read_packet(AVFormatContext *s, AVPacket *pkt)
 AVInputFormat ff_mp3_demuxer = {
     .name           = "mp3",
     .long_name      = NULL_IF_CONFIG_SMALL("MPEG audio layer 2/3"),
+    .priv_data_size = sizeof(MP3Context),
     .read_probe     = mp3_read_probe,
     .read_header    = mp3_read_header,
     .read_packet    = mp3_read_packet,