00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "libavutil/avstring.h"
00023 #include "libavutil/intreadwrite.h"
00024 #include "libavutil/dict.h"
00025 #include "libavutil/mathematics.h"
00026 #include "avformat.h"
00027 #include "internal.h"
00028 #include "id3v2.h"
00029 #include "id3v1.h"
00030 #include "libavcodec/mpegaudiodecheader.h"
00031
00032 typedef struct {
00033 int64_t filesize;
00034 } MP3Context;
00035
00036
00037
00038 static int mp3_read_probe(AVProbeData *p)
00039 {
00040 int max_frames, first_frames = 0;
00041 int fsize, frames, sample_rate;
00042 uint32_t header;
00043 uint8_t *buf, *buf0, *buf2, *end;
00044 AVCodecContext avctx;
00045
00046 buf0 = p->buf;
00047 end = p->buf + p->buf_size - sizeof(uint32_t);
00048 while(buf0 < end && !*buf0)
00049 buf0++;
00050
00051 max_frames = 0;
00052 buf = buf0;
00053
00054 for(; buf < end; buf= buf2+1) {
00055 buf2 = buf;
00056
00057 for(frames = 0; buf2 < end; frames++) {
00058 header = AV_RB32(buf2);
00059 fsize = avpriv_mpa_decode_header(&avctx, header, &sample_rate, &sample_rate, &sample_rate, &sample_rate);
00060 if(fsize < 0)
00061 break;
00062 buf2 += fsize;
00063 }
00064 max_frames = FFMAX(max_frames, frames);
00065 if(buf == buf0)
00066 first_frames= frames;
00067 }
00068
00069
00070 if (first_frames>=4) return AVPROBE_SCORE_MAX/2+1;
00071 else if(max_frames>200)return AVPROBE_SCORE_MAX/2;
00072 else if(max_frames>=4) return AVPROBE_SCORE_MAX/4;
00073 else if(ff_id3v2_match(buf0, ID3v2_DEFAULT_MAGIC) && 2*ff_id3v2_tag_len(buf0) >= p->buf_size)
00074 return AVPROBE_SCORE_MAX/8;
00075 else if(max_frames>=1) return 1;
00076 else return 0;
00077
00078 }
00079
00083 static int mp3_parse_vbr_tags(AVFormatContext *s, AVStream *st, int64_t base)
00084 {
00085 uint32_t v, spf;
00086 unsigned frames = 0;
00087 unsigned size = 0;
00088 const int64_t xing_offtbl[2][2] = {{32, 17}, {17,9}};
00089 MPADecodeHeader c;
00090 int vbrtag_size = 0;
00091
00092 v = avio_rb32(s->pb);
00093 if(ff_mpa_check_header(v) < 0)
00094 return -1;
00095
00096 if (avpriv_mpegaudio_decode_header(&c, v) == 0)
00097 vbrtag_size = c.frame_size;
00098 if(c.layer != 3)
00099 return -1;
00100
00101
00102 avio_skip(s->pb, xing_offtbl[c.lsf == 1][c.nb_channels == 1]);
00103 v = avio_rb32(s->pb);
00104 if(v == MKBETAG('X', 'i', 'n', 'g') || v == MKBETAG('I', 'n', 'f', 'o')) {
00105 v = avio_rb32(s->pb);
00106 if(v & 0x1)
00107 frames = avio_rb32(s->pb);
00108 if(v & 0x2)
00109 size = avio_rb32(s->pb);
00110 }
00111
00112
00113 avio_seek(s->pb, base + 4 + 32, SEEK_SET);
00114 v = avio_rb32(s->pb);
00115 if(v == MKBETAG('V', 'B', 'R', 'I')) {
00116
00117 if(avio_rb16(s->pb) == 1) {
00118
00119 avio_skip(s->pb, 4);
00120 size = avio_rb32(s->pb);
00121 frames = avio_rb32(s->pb);
00122 }
00123 }
00124
00125 if(!frames && !size)
00126 return -1;
00127
00128
00129 avio_seek(s->pb, base + vbrtag_size, SEEK_SET);
00130
00131 spf = c.lsf ? 576 : 1152;
00132 if(frames)
00133 st->duration = av_rescale_q(frames, (AVRational){spf, c.sample_rate},
00134 st->time_base);
00135 if(size && frames)
00136 st->codec->bit_rate = av_rescale(size, 8 * c.sample_rate, frames * (int64_t)spf);
00137
00138 return 0;
00139 }
00140
00141 static int mp3_read_header(AVFormatContext *s)
00142 {
00143 MP3Context *mp3 = s->priv_data;
00144 AVStream *st;
00145 int64_t off;
00146
00147 st = avformat_new_stream(s, NULL);
00148 if (!st)
00149 return AVERROR(ENOMEM);
00150
00151 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00152 st->codec->codec_id = CODEC_ID_MP3;
00153 st->need_parsing = AVSTREAM_PARSE_FULL;
00154 st->start_time = 0;
00155
00156
00157 avpriv_set_pts_info(st, 64, 1, 14112000);
00158
00159 s->pb->maxsize = -1;
00160 off = avio_tell(s->pb);
00161
00162 if (!av_dict_get(s->metadata, "", NULL, AV_DICT_IGNORE_SUFFIX))
00163 ff_id3v1_read(s);
00164
00165 if(s->pb->seekable)
00166 mp3->filesize = avio_size(s->pb);
00167
00168 if (mp3_parse_vbr_tags(s, st, off) < 0)
00169 avio_seek(s->pb, off, SEEK_SET);
00170
00171
00172 return 0;
00173 }
00174
00175 #define MP3_PACKET_SIZE 1024
00176
00177 static int mp3_read_packet(AVFormatContext *s, AVPacket *pkt)
00178 {
00179 MP3Context *mp3 = s->priv_data;
00180 int ret, size;
00181 int64_t pos;
00182
00183
00184 size= MP3_PACKET_SIZE;
00185 pos = avio_tell(s->pb);
00186 if(mp3->filesize > ID3v1_TAG_SIZE && pos < mp3->filesize)
00187 size= FFMIN(size, mp3->filesize - pos);
00188
00189 ret= av_get_packet(s->pb, pkt, size);
00190
00191 pkt->flags &= ~AV_PKT_FLAG_CORRUPT;
00192 pkt->stream_index = 0;
00193 if (ret <= 0) {
00194 if(ret<0)
00195 return ret;
00196 return AVERROR_EOF;
00197 }
00198
00199 if (ret >= ID3v1_TAG_SIZE &&
00200 memcmp(&pkt->data[ret - ID3v1_TAG_SIZE], "TAG", 3) == 0)
00201 ret -= ID3v1_TAG_SIZE;
00202
00203
00204
00205 pkt->size = ret;
00206 return ret;
00207 }
00208
00209 AVInputFormat ff_mp3_demuxer = {
00210 .name = "mp3",
00211 .long_name = NULL_IF_CONFIG_SMALL("MPEG audio layer 2/3"),
00212 .priv_data_size = sizeof(MP3Context),
00213 .read_probe = mp3_read_probe,
00214 .read_header = mp3_read_header,
00215 .read_packet = mp3_read_packet,
00216 .flags = AVFMT_GENERIC_INDEX,
00217 .extensions = "mp2,mp3,m2a",
00218 };