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 #define XING_FLAG_FRAMES 0x01
00033 #define XING_FLAG_SIZE 0x02
00034 #define XING_FLAG_TOC 0x04
00035
00036 #define XING_TOC_COUNT 100
00037
00038 typedef struct {
00039 int64_t filesize;
00040 int xing_toc;
00041 int start_pad;
00042 int end_pad;
00043 } MP3Context;
00044
00045
00046
00047 static int mp3_read_probe(AVProbeData *p)
00048 {
00049 int max_frames, first_frames = 0;
00050 int fsize, frames, sample_rate;
00051 uint32_t header;
00052 uint8_t *buf, *buf0, *buf2, *end;
00053 AVCodecContext avctx;
00054
00055 buf0 = p->buf;
00056 end = p->buf + p->buf_size - sizeof(uint32_t);
00057 while(buf0 < end && !*buf0)
00058 buf0++;
00059
00060 max_frames = 0;
00061 buf = buf0;
00062
00063 for(; buf < end; buf= buf2+1) {
00064 buf2 = buf;
00065
00066 for(frames = 0; buf2 < end; frames++) {
00067 header = AV_RB32(buf2);
00068 fsize = avpriv_mpa_decode_header(&avctx, header, &sample_rate, &sample_rate, &sample_rate, &sample_rate);
00069 if(fsize < 0)
00070 break;
00071 buf2 += fsize;
00072 }
00073 max_frames = FFMAX(max_frames, frames);
00074 if(buf == buf0)
00075 first_frames= frames;
00076 }
00077
00078
00079 if (first_frames>=4) return AVPROBE_SCORE_MAX/2+1;
00080 else if(max_frames>200)return AVPROBE_SCORE_MAX/2;
00081 else if(max_frames>=4) return AVPROBE_SCORE_MAX/4;
00082 else if(ff_id3v2_match(buf0, ID3v2_DEFAULT_MAGIC) && 2*ff_id3v2_tag_len(buf0) >= p->buf_size)
00083 return AVPROBE_SCORE_MAX/8;
00084 else if(max_frames>=1) return 1;
00085 else return 0;
00086
00087 }
00088
00089 static void read_xing_toc(AVFormatContext *s, int64_t filesize, int64_t duration)
00090 {
00091 int i;
00092 MP3Context *mp3 = s->priv_data;
00093
00094 if (!filesize &&
00095 !(filesize = avio_size(s->pb))) {
00096 av_log(s, AV_LOG_WARNING, "Cannot determine file size, skipping TOC table.\n");
00097 return;
00098 }
00099
00100 for (i = 0; i < XING_TOC_COUNT; i++) {
00101 uint8_t b = avio_r8(s->pb);
00102
00103 av_add_index_entry(s->streams[0],
00104 av_rescale(b, filesize, 256),
00105 av_rescale(i, duration, XING_TOC_COUNT),
00106 0, 0, AVINDEX_KEYFRAME);
00107 }
00108 mp3->xing_toc = 1;
00109 }
00110
00114 static int mp3_parse_vbr_tags(AVFormatContext *s, AVStream *st, int64_t base)
00115 {
00116 MP3Context *mp3 = s->priv_data;
00117 uint32_t v, spf;
00118 unsigned frames = 0;
00119 unsigned size = 0;
00120 const int64_t xing_offtbl[2][2] = {{32, 17}, {17,9}};
00121 MPADecodeHeader c;
00122 int vbrtag_size = 0;
00123
00124 v = avio_rb32(s->pb);
00125 if(ff_mpa_check_header(v) < 0)
00126 return -1;
00127
00128 if (avpriv_mpegaudio_decode_header(&c, v) == 0)
00129 vbrtag_size = c.frame_size;
00130 if(c.layer != 3)
00131 return -1;
00132
00133 spf = c.lsf ? 576 : 1152;
00134
00135
00136 avio_skip(s->pb, xing_offtbl[c.lsf == 1][c.nb_channels == 1]);
00137 v = avio_rb32(s->pb);
00138 if(v == MKBETAG('X', 'i', 'n', 'g') || v == MKBETAG('I', 'n', 'f', 'o')) {
00139 v = avio_rb32(s->pb);
00140 if(v & XING_FLAG_FRAMES)
00141 frames = avio_rb32(s->pb);
00142 if(v & XING_FLAG_SIZE)
00143 size = avio_rb32(s->pb);
00144 if (v & XING_FLAG_TOC && frames)
00145 read_xing_toc(s, size, av_rescale_q(frames, (AVRational){spf, c.sample_rate},
00146 st->time_base));
00147 if(v & 8)
00148 avio_skip(s->pb, 4);
00149
00150 v = avio_rb32(s->pb);
00151 if(v == MKBETAG('L', 'A', 'M', 'E') || v == MKBETAG('L', 'a', 'v', 'f')) {
00152 avio_skip(s->pb, 21-4);
00153 v= avio_rb24(s->pb);
00154 mp3->start_pad = v>>12;
00155 mp3-> end_pad = v&4095;
00156 st->skip_samples = mp3->start_pad + 528 + 1;
00157 av_log(s, AV_LOG_DEBUG, "pad %d %d\n", mp3->start_pad, mp3-> end_pad);
00158 }
00159 }
00160
00161
00162 avio_seek(s->pb, base + 4 + 32, SEEK_SET);
00163 v = avio_rb32(s->pb);
00164 if(v == MKBETAG('V', 'B', 'R', 'I')) {
00165
00166 if(avio_rb16(s->pb) == 1) {
00167
00168 avio_skip(s->pb, 4);
00169 size = avio_rb32(s->pb);
00170 frames = avio_rb32(s->pb);
00171 }
00172 }
00173
00174 if(!frames && !size)
00175 return -1;
00176
00177
00178 avio_seek(s->pb, base + vbrtag_size, SEEK_SET);
00179
00180 if(frames)
00181 st->duration = av_rescale_q(frames, (AVRational){spf, c.sample_rate},
00182 st->time_base);
00183 if(size && frames)
00184 st->codec->bit_rate = av_rescale(size, 8 * c.sample_rate, frames * (int64_t)spf);
00185
00186 return 0;
00187 }
00188
00189 static int mp3_read_header(AVFormatContext *s)
00190 {
00191 MP3Context *mp3 = s->priv_data;
00192 AVStream *st;
00193 int64_t off;
00194
00195 st = avformat_new_stream(s, NULL);
00196 if (!st)
00197 return AVERROR(ENOMEM);
00198
00199 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00200 st->codec->codec_id = AV_CODEC_ID_MP3;
00201 st->need_parsing = AVSTREAM_PARSE_FULL_RAW;
00202 st->start_time = 0;
00203
00204
00205 avpriv_set_pts_info(st, 64, 1, 14112000);
00206
00207 s->pb->maxsize = -1;
00208 off = avio_tell(s->pb);
00209
00210 if (!av_dict_get(s->metadata, "", NULL, AV_DICT_IGNORE_SUFFIX))
00211 ff_id3v1_read(s);
00212
00213 if(s->pb->seekable)
00214 mp3->filesize = avio_size(s->pb);
00215
00216 if (mp3_parse_vbr_tags(s, st, off) < 0)
00217 avio_seek(s->pb, off, SEEK_SET);
00218
00219
00220 return 0;
00221 }
00222
00223 #define MP3_PACKET_SIZE 1024
00224
00225 static int mp3_read_packet(AVFormatContext *s, AVPacket *pkt)
00226 {
00227 MP3Context *mp3 = s->priv_data;
00228 int ret, size;
00229 int64_t pos;
00230
00231 size= MP3_PACKET_SIZE;
00232 pos = avio_tell(s->pb);
00233 if(mp3->filesize > ID3v1_TAG_SIZE && pos < mp3->filesize)
00234 size= FFMIN(size, mp3->filesize - pos);
00235
00236 ret= av_get_packet(s->pb, pkt, size);
00237 if (ret <= 0) {
00238 if(ret<0)
00239 return ret;
00240 return AVERROR_EOF;
00241 }
00242
00243 pkt->flags &= ~AV_PKT_FLAG_CORRUPT;
00244 pkt->stream_index = 0;
00245
00246 if (ret >= ID3v1_TAG_SIZE &&
00247 memcmp(&pkt->data[ret - ID3v1_TAG_SIZE], "TAG", 3) == 0)
00248 ret -= ID3v1_TAG_SIZE;
00249
00250
00251
00252 pkt->size = ret;
00253 return ret;
00254 }
00255
00256 static int mp3_seek(AVFormatContext *s, int stream_index, int64_t timestamp,
00257 int flags)
00258 {
00259 MP3Context *mp3 = s->priv_data;
00260 AVIndexEntry *ie;
00261 AVStream *st = s->streams[0];
00262 int64_t ret = av_index_search_timestamp(st, timestamp, flags);
00263 uint32_t header = 0;
00264
00265 if (!mp3->xing_toc) {
00266 st->skip_samples = timestamp <= 0 ? mp3->start_pad + 528 + 1 : 0;
00267
00268 return -1;
00269 }
00270
00271 if (ret < 0)
00272 return ret;
00273
00274 ie = &st->index_entries[ret];
00275 ret = avio_seek(s->pb, ie->pos, SEEK_SET);
00276 if (ret < 0)
00277 return ret;
00278
00279 while (!s->pb->eof_reached) {
00280 header = (header << 8) + avio_r8(s->pb);
00281 if (ff_mpa_check_header(header) >= 0) {
00282 ff_update_cur_dts(s, st, ie->timestamp);
00283 ret = avio_seek(s->pb, -4, SEEK_CUR);
00284
00285 st->skip_samples = ie->timestamp <= 0 ? mp3->start_pad + 528 + 1 : 0;
00286
00287 return (ret >= 0) ? 0 : ret;
00288 }
00289 }
00290
00291 return AVERROR_EOF;
00292 }
00293
00294 AVInputFormat ff_mp3_demuxer = {
00295 .name = "mp3",
00296 .long_name = NULL_IF_CONFIG_SMALL("MP2/3 (MPEG audio layer 2/3)"),
00297 .priv_data_size = sizeof(MP3Context),
00298 .read_probe = mp3_read_probe,
00299 .read_header = mp3_read_header,
00300 .read_packet = mp3_read_packet,
00301 .read_seek = mp3_seek,
00302 .flags = AVFMT_GENERIC_INDEX,
00303 .extensions = "mp2,mp3,m2a",
00304 };