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