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 "avformat.h"
00025 #include "id3v2.h"
00026 #include "id3v1.h"
00027 #include "libavcodec/mpegaudiodecheader.h"
00028
00029
00030
00031 static int mp3_read_probe(AVProbeData *p)
00032 {
00033 int max_frames, first_frames = 0;
00034 int fsize, frames, sample_rate;
00035 uint32_t header;
00036 uint8_t *buf, *buf0, *buf2, *end;
00037 AVCodecContext avctx;
00038
00039 buf0 = p->buf;
00040 end = p->buf + p->buf_size - sizeof(uint32_t);
00041 while(buf0 < end && !*buf0)
00042 buf0++;
00043
00044 max_frames = 0;
00045 buf = buf0;
00046
00047 for(; buf < end; buf= buf2+1) {
00048 buf2 = buf;
00049
00050 for(frames = 0; buf2 < end; frames++) {
00051 header = AV_RB32(buf2);
00052 fsize = ff_mpa_decode_header(&avctx, header, &sample_rate, &sample_rate, &sample_rate, &sample_rate);
00053 if(fsize < 0)
00054 break;
00055 buf2 += fsize;
00056 }
00057 max_frames = FFMAX(max_frames, frames);
00058 if(buf == buf0)
00059 first_frames= frames;
00060 }
00061
00062
00063 if (first_frames>=4) return AVPROBE_SCORE_MAX/2+1;
00064 else if(max_frames>500)return AVPROBE_SCORE_MAX/2;
00065 else if(max_frames>=4) return AVPROBE_SCORE_MAX/4;
00066 else if(max_frames>=1) return 1;
00067 else return 0;
00068
00069 }
00070
00074 static int mp3_parse_vbr_tags(AVFormatContext *s, AVStream *st, int64_t base)
00075 {
00076 uint32_t v, spf;
00077 unsigned frames = 0;
00078 unsigned size = 0;
00079 const int64_t xing_offtbl[2][2] = {{32, 17}, {17,9}};
00080 MPADecodeHeader c;
00081 int vbrtag_size = 0;
00082
00083 v = avio_rb32(s->pb);
00084 if(ff_mpa_check_header(v) < 0)
00085 return -1;
00086
00087 if (ff_mpegaudio_decode_header(&c, v) == 0)
00088 vbrtag_size = c.frame_size;
00089 if(c.layer != 3)
00090 return -1;
00091
00092
00093 avio_seek(s->pb, xing_offtbl[c.lsf == 1][c.nb_channels == 1], SEEK_CUR);
00094 v = avio_rb32(s->pb);
00095 if(v == MKBETAG('X', 'i', 'n', 'g') || v == MKBETAG('I', 'n', 'f', 'o')) {
00096 v = avio_rb32(s->pb);
00097 if(v & 0x1)
00098 frames = avio_rb32(s->pb);
00099 if(v & 0x2)
00100 size = avio_rb32(s->pb);
00101 }
00102
00103
00104 avio_seek(s->pb, base + 4 + 32, SEEK_SET);
00105 v = avio_rb32(s->pb);
00106 if(v == MKBETAG('V', 'B', 'R', 'I')) {
00107
00108 if(avio_rb16(s->pb) == 1) {
00109
00110 avio_seek(s->pb, 4, SEEK_CUR);
00111 frames = avio_rb32(s->pb);
00112 size = avio_rb32(s->pb);
00113 }
00114 }
00115
00116 if(!frames && !size)
00117 return -1;
00118
00119
00120 avio_seek(s->pb, base + vbrtag_size, SEEK_SET);
00121
00122 spf = c.lsf ? 576 : 1152;
00123 if(frames)
00124 st->duration = av_rescale_q(frames, (AVRational){spf, c.sample_rate},
00125 st->time_base);
00126 if(size && frames)
00127 st->codec->bit_rate = av_rescale(size, 8 * c.sample_rate, frames * (int64_t)spf);
00128
00129 return 0;
00130 }
00131
00132 static int mp3_read_header(AVFormatContext *s,
00133 AVFormatParameters *ap)
00134 {
00135 AVStream *st;
00136 int64_t off;
00137
00138 st = av_new_stream(s, 0);
00139 if (!st)
00140 return AVERROR(ENOMEM);
00141
00142 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00143 st->codec->codec_id = CODEC_ID_MP3;
00144 st->need_parsing = AVSTREAM_PARSE_FULL;
00145 st->start_time = 0;
00146
00147
00148 av_set_pts_info(st, 64, 1, 14112000);
00149
00150 off = avio_tell(s->pb);
00151
00152 if (!av_metadata_get(s->metadata, "", NULL, AV_METADATA_IGNORE_SUFFIX))
00153 ff_id3v1_read(s);
00154
00155 if (mp3_parse_vbr_tags(s, st, off) < 0)
00156 avio_seek(s->pb, off, SEEK_SET);
00157
00158
00159 return 0;
00160 }
00161
00162 #define MP3_PACKET_SIZE 1024
00163
00164 static int mp3_read_packet(AVFormatContext *s, AVPacket *pkt)
00165 {
00166 int ret, size;
00167
00168
00169 size= MP3_PACKET_SIZE;
00170
00171 ret= av_get_packet(s->pb, pkt, size);
00172
00173 pkt->stream_index = 0;
00174 if (ret <= 0) {
00175 return AVERROR(EIO);
00176 }
00177
00178 if (ret > ID3v1_TAG_SIZE &&
00179 memcmp(&pkt->data[ret - ID3v1_TAG_SIZE], "TAG", 3) == 0)
00180 ret -= ID3v1_TAG_SIZE;
00181
00182
00183
00184 pkt->size = ret;
00185 return ret;
00186 }
00187
00188 AVInputFormat ff_mp3_demuxer = {
00189 "mp3",
00190 NULL_IF_CONFIG_SMALL("MPEG audio layer 2/3"),
00191 0,
00192 mp3_read_probe,
00193 mp3_read_header,
00194 mp3_read_packet,
00195 .flags= AVFMT_GENERIC_INDEX,
00196 .extensions = "mp2,mp3,m2a",
00197 };