3 * Copyright (c) 2003 Fabrice Bellard
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libavutil/opt.h"
23 #include "libavutil/avstring.h"
24 #include "libavutil/intreadwrite.h"
25 #include "libavutil/dict.h"
26 #include "libavutil/mathematics.h"
31 #include "libavcodec/mpegaudiodecheader.h"
33 #define XING_FLAG_FRAMES 0x01
34 #define XING_FLAG_SIZE 0x02
35 #define XING_FLAG_TOC 0x04
37 #define XING_TOC_COUNT 100
50 static int mp3_read_probe(AVProbeData *p)
52 int max_frames, first_frames = 0;
53 int fsize, frames, sample_rate;
55 const uint8_t *buf, *buf0, *buf2, *end;
59 end = p->buf + p->buf_size - sizeof(uint32_t);
60 while(buf0 < end && !*buf0)
66 for(; buf < end; buf= buf2+1) {
69 for(frames = 0; buf2 < end; frames++) {
70 header = AV_RB32(buf2);
71 fsize = avpriv_mpa_decode_header(&avctx, header, &sample_rate, &sample_rate, &sample_rate, &sample_rate);
76 max_frames = FFMAX(max_frames, frames);
80 // keep this in sync with ac3 probe, both need to avoid
81 // issues with MPEG-files!
82 if (first_frames>=4) return AVPROBE_SCORE_EXTENSION + 1;
83 else if(max_frames>200)return AVPROBE_SCORE_EXTENSION;
84 else if(max_frames>=4) return AVPROBE_SCORE_EXTENSION / 2;
85 else if(ff_id3v2_match(buf0, ID3v2_DEFAULT_MAGIC) && 2*ff_id3v2_tag_len(buf0) >= p->buf_size)
86 return AVPROBE_SCORE_EXTENSION / 4;
87 else if(max_frames>=1) return 1;
89 //mpegps_mp3_unrecognized_format.mpg has max_frames=3
92 static void read_xing_toc(AVFormatContext *s, int64_t filesize, int64_t duration)
95 MP3DecContext *mp3 = s->priv_data;
101 !(filesize = avio_size(s->pb))) {
102 av_log(s, AV_LOG_WARNING, "Cannot determine file size, skipping TOC table.\n");
106 for (i = 0; i < XING_TOC_COUNT; i++) {
107 uint8_t b = avio_r8(s->pb);
109 av_add_index_entry(s->streams[0],
110 av_rescale(b, filesize, 256),
111 av_rescale(i, duration, XING_TOC_COUNT),
112 0, 0, AVINDEX_KEYFRAME);
118 * Try to find Xing/Info/VBRI tags and compute duration from info therein
120 static int mp3_parse_vbr_tags(AVFormatContext *s, AVStream *st, int64_t base)
122 MP3DecContext *mp3 = s->priv_data;
124 unsigned frames = 0; /* Total number of frames in file */
125 unsigned size = 0; /* Total number of bytes in the stream */
126 const int64_t xing_offtbl[2][2] = {{32, 17}, {17,9}};
131 v = avio_rb32(s->pb);
132 if(ff_mpa_check_header(v) < 0)
135 if (avpriv_mpegaudio_decode_header(&c, v) == 0)
136 vbrtag_size = c.frame_size;
140 spf = c.lsf ? 576 : 1152; /* Samples per frame, layer 3 */
142 /* Check for Xing / Info tag */
143 avio_skip(s->pb, xing_offtbl[c.lsf == 1][c.nb_channels == 1]);
144 v = avio_rb32(s->pb);
145 is_cbr = v == MKBETAG('I', 'n', 'f', 'o');
146 if (v == MKBETAG('X', 'i', 'n', 'g') || is_cbr) {
147 v = avio_rb32(s->pb);
148 if(v & XING_FLAG_FRAMES)
149 frames = avio_rb32(s->pb);
150 if(v & XING_FLAG_SIZE)
151 size = avio_rb32(s->pb);
152 if (v & XING_FLAG_TOC && frames)
153 read_xing_toc(s, size, av_rescale_q(frames, (AVRational){spf, c.sample_rate},
158 v = avio_rb32(s->pb);
159 if(v == MKBETAG('L', 'A', 'M', 'E') || v == MKBETAG('L', 'a', 'v', 'f')) {
160 avio_skip(s->pb, 21-4);
162 mp3->start_pad = v>>12;
163 mp3-> end_pad = v&4095;
164 st->skip_samples = mp3->start_pad + 528 + 1;
165 av_log(s, AV_LOG_DEBUG, "pad %d %d\n", mp3->start_pad, mp3-> end_pad);
169 /* Check for VBRI tag (always 32 bytes after end of mpegaudio header) */
170 avio_seek(s->pb, base + 4 + 32, SEEK_SET);
171 v = avio_rb32(s->pb);
172 if(v == MKBETAG('V', 'B', 'R', 'I')) {
173 /* Check tag version */
174 if(avio_rb16(s->pb) == 1) {
175 /* skip delay and quality */
177 size = avio_rb32(s->pb);
178 frames = avio_rb32(s->pb);
185 /* Skip the vbr tag frame */
186 avio_seek(s->pb, base + vbrtag_size, SEEK_SET);
189 st->duration = av_rescale_q(frames, (AVRational){spf, c.sample_rate},
191 if (size && frames && !is_cbr)
192 st->codec->bit_rate = av_rescale(size, 8 * c.sample_rate, frames * (int64_t)spf);
197 static int mp3_read_header(AVFormatContext *s)
199 MP3DecContext *mp3 = s->priv_data;
203 st = avformat_new_stream(s, NULL);
205 return AVERROR(ENOMEM);
207 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
208 st->codec->codec_id = AV_CODEC_ID_MP3;
209 st->need_parsing = AVSTREAM_PARSE_FULL_RAW;
212 // lcm of all mp3 sample rates
213 avpriv_set_pts_info(st, 64, 1, 14112000);
216 off = avio_tell(s->pb);
218 if (!av_dict_get(s->metadata, "", NULL, AV_DICT_IGNORE_SUFFIX))
222 mp3->filesize = avio_size(s->pb);
224 if (mp3_parse_vbr_tags(s, st, off) < 0)
225 avio_seek(s->pb, off, SEEK_SET);
227 /* the parameters will be extracted from the compressed bitstream */
231 #define MP3_PACKET_SIZE 1024
233 static int mp3_read_packet(AVFormatContext *s, AVPacket *pkt)
235 MP3DecContext *mp3 = s->priv_data;
239 size= MP3_PACKET_SIZE;
240 pos = avio_tell(s->pb);
241 if(mp3->filesize > ID3v1_TAG_SIZE && pos < mp3->filesize)
242 size= FFMIN(size, mp3->filesize - pos);
244 ret= av_get_packet(s->pb, pkt, size);
251 pkt->flags &= ~AV_PKT_FLAG_CORRUPT;
252 pkt->stream_index = 0;
254 if (ret >= ID3v1_TAG_SIZE &&
255 memcmp(&pkt->data[ret - ID3v1_TAG_SIZE], "TAG", 3) == 0)
256 ret -= ID3v1_TAG_SIZE;
258 /* note: we need to modify the packet size here to handle the last
264 static int check(AVFormatContext *s, int64_t pos)
266 int64_t ret = avio_seek(s->pb, pos, SEEK_SET);
271 header = avio_rb32(s->pb);
272 if (ff_mpa_check_header(header) < 0)
274 if (avpriv_mpegaudio_decode_header(&sd, header) == 1)
276 return sd.frame_size;
279 static int mp3_seek(AVFormatContext *s, int stream_index, int64_t timestamp,
282 MP3DecContext *mp3 = s->priv_data;
284 AVStream *st = s->streams[0];
285 int64_t ret = av_index_search_timestamp(st, timestamp, flags);
288 if (!mp3->xing_toc) {
289 st->skip_samples = timestamp <= 0 ? mp3->start_pad + 528 + 1 : 0;
297 ie = &st->index_entries[ret];
298 ret = avio_seek(s->pb, ie->pos, SEEK_SET);
303 for(i=0; i<4096; i++) {
304 int64_t pos = ie->pos + i;
305 for(j=0; j<MIN_VALID; j++) {
317 ret = avio_seek(s->pb, ie->pos + i, SEEK_SET);
320 ff_update_cur_dts(s, st, ie->timestamp);
321 st->skip_samples = ie->timestamp <= 0 ? mp3->start_pad + 528 + 1 : 0;
325 static const AVOption options[] = {
326 { "usetoc", "use table of contents", offsetof(MP3DecContext, usetoc), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 1, AV_OPT_FLAG_DECODING_PARAM},
330 static const AVClass demuxer_class = {
332 .item_name = av_default_item_name,
334 .version = LIBAVUTIL_VERSION_INT,
335 .category = AV_CLASS_CATEGORY_DEMUXER,
338 AVInputFormat ff_mp3_demuxer = {
340 .long_name = NULL_IF_CONFIG_SMALL("MP2/3 (MPEG audio layer 2/3)"),
341 .read_probe = mp3_read_probe,
342 .read_header = mp3_read_header,
343 .read_packet = mp3_read_packet,
344 .read_seek = mp3_seek,
345 .priv_data_size = sizeof(MP3DecContext),
346 .flags = AVFMT_GENERIC_INDEX,
347 .extensions = "mp2,mp3,m2a", /* XXX: use probe */
348 .priv_class = &demuxer_class,