mp3dec: move XING/Info and VBRI parsing into their own functions
[libav.git] / libavformat / mp3dec.c
CommitLineData
115329f1 1/*
d08928bb 2 * MP3 demuxer
406792e7 3 * Copyright (c) 2003 Fabrice Bellard
6a58e151 4 *
2912e87a 5 * This file is part of Libav.
b78e7197 6 *
2912e87a 7 * Libav is free software; you can redistribute it and/or
6a58e151
FB
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
b78e7197 10 * version 2.1 of the License, or (at your option) any later version.
6a58e151 11 *
2912e87a 12 * Libav is distributed in the hope that it will be useful,
6a58e151
FB
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.
16 *
17 * You should have received a copy of the GNU Lesser General Public
2912e87a 18 * License along with Libav; if not, write to the Free Software
5509bffa 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
6a58e151 20 */
245976da
DB
21
22#include "libavutil/avstring.h"
ff06d76a 23#include "libavutil/intreadwrite.h"
d2d67e42 24#include "libavutil/dict.h"
0ebcdf5c 25#include "libavutil/mathematics.h"
6a58e151 26#include "avformat.h"
c3f9ebf7 27#include "internal.h"
2ea512a6 28#include "id3v2.h"
75411182 29#include "id3v1.h"
15c5a8d2
AK
30#include "replaygain.h"
31
de653ba3
DB
32#include "libavcodec/mpegaudiodecheader.h"
33
2d1a1a7f
AK
34#define XING_FLAG_FRAMES 0x01
35#define XING_FLAG_SIZE 0x02
50d1f443
AK
36#define XING_FLAG_TOC 0x04
37
38#define XING_TOC_COUNT 100
2d1a1a7f 39
505642f1
MN
40typedef struct MP3DecContext {
41 int xing_toc;
fbd8e042
AG
42 unsigned frames; /* Total number of frames in file */
43 unsigned size; /* Total number of bytes in the stream */
44 int is_cbr;
505642f1
MN
45} MP3DecContext;
46
6a58e151 47/* mp3 read */
dc4ed3b1
MR
48
49static int mp3_read_probe(AVProbeData *p)
50{
ea7599ba 51 int max_frames, first_frames = 0;
498c544a 52 int fsize, frames, sample_rate;
abade142 53 uint32_t header;
ac3ef4a4 54 uint8_t *buf, *buf0, *buf2, *end;
abade142 55 AVCodecContext avctx;
dc4ed3b1 56
ac3ef4a4 57 buf0 = p->buf;
5692ed3a
MN
58 end = p->buf + p->buf_size - sizeof(uint32_t);
59 while(buf0 < end && !*buf0)
60 buf0++;
dc4ed3b1 61
abade142 62 max_frames = 0;
ac3ef4a4 63 buf = buf0;
dc4ed3b1 64
5ef5501b 65 for(; buf < end; buf= buf2+1) {
abade142 66 buf2 = buf;
dc4ed3b1 67
d0702de9 68 for(frames = 0; buf2 < end; frames++) {
80fb8234 69 header = AV_RB32(buf2);
82ab61f9 70 fsize = avpriv_mpa_decode_header(&avctx, header, &sample_rate, &sample_rate, &sample_rate, &sample_rate);
abade142
71 if(fsize < 0)
72 break;
73 buf2 += fsize;
74 }
75 max_frames = FFMAX(max_frames, frames);
ac3ef4a4 76 if(buf == buf0)
22668218 77 first_frames= frames;
abade142 78 }
4b4ac5c7
RD
79 // keep this in sync with ac3 probe, both need to avoid
80 // issues with MPEG-files!
e0f8be64 81 if (first_frames >= 4) return AVPROBE_SCORE_EXTENSION + 1;
64bde805
RB
82
83 if (max_frames) {
84 int pes = 0, i;
85 unsigned int code = -1;
86
87#define VIDEO_ID 0x000001e0
88#define AUDIO_ID 0x000001c0
89 /* do a search for mpegps headers to be able to properly bias
90 * towards mpegps if we detect this stream as both. */
91 for (i = 0; i<p->buf_size; i++) {
92 code = (code << 8) + p->buf[i];
93 if ((code & 0xffffff00) == 0x100) {
94 if ((code & 0x1f0) == VIDEO_ID) pes++;
95 else if((code & 0x1e0) == AUDIO_ID) pes++;
96 }
97 }
98
99 if (pes)
100 max_frames = (max_frames + pes - 1) / pes;
101 }
e0f8be64
DB
102 if (max_frames > 500) return AVPROBE_SCORE_EXTENSION;
103 else if (max_frames >= 4) return AVPROBE_SCORE_EXTENSION / 2;
64bde805
RB
104 else if (max_frames >= 1) return 1;
105 else return 0;
f9f2ab82 106//mpegps_mp3_unrecognized_format.mpg has max_frames=3
dc4ed3b1
MR
107}
108
50d1f443
AK
109static void read_xing_toc(AVFormatContext *s, int64_t filesize, int64_t duration)
110{
111 int i;
505642f1 112 MP3DecContext *mp3 = s->priv_data;
50d1f443
AK
113
114 if (!filesize &&
115 !(filesize = avio_size(s->pb))) {
116 av_log(s, AV_LOG_WARNING, "Cannot determine file size, skipping TOC table.\n");
117 return;
118 }
119
120 for (i = 0; i < XING_TOC_COUNT; i++) {
121 uint8_t b = avio_r8(s->pb);
122
123 av_add_index_entry(s->streams[0],
124 av_rescale(b, filesize, 256),
125 av_rescale(i, duration, XING_TOC_COUNT),
126 0, 0, AVINDEX_KEYFRAME);
127 }
505642f1 128 mp3->xing_toc = 1;
50d1f443
AK
129}
130
fbd8e042
AG
131
132static void mp3_parse_info_tag(AVFormatContext *s, AVStream *st,
133 MPADecodeHeader *c, uint32_t spf)
134{
135 uint32_t v;
136 MP3DecContext *mp3 = s->priv_data;
137 const int64_t xing_offtbl[2][2] = {{32, 17}, {17,9}};
138
139 /* Check for Xing / Info tag */
140 avio_skip(s->pb, xing_offtbl[c->lsf == 1][c->nb_channels == 1]);
141 v = avio_rb32(s->pb);
142 mp3->is_cbr = v == MKBETAG('I', 'n', 'f', 'o');
143 if (v == MKBETAG('X', 'i', 'n', 'g') || mp3->is_cbr) {
144 v = avio_rb32(s->pb);
145 if (v & XING_FLAG_FRAMES)
146 mp3->frames = avio_rb32(s->pb);
147 if (v & XING_FLAG_SIZE)
148 mp3->size = avio_rb32(s->pb);
149 if (v & XING_FLAG_TOC && mp3->frames)
150 read_xing_toc(s, mp3->size,
151 av_rescale_q(mp3->frames,
152 (AVRational){spf, c->sample_rate},
153 st->time_base));
154 }
155}
156
157static void mp3_parse_vbri_tag(AVFormatContext *s, AVStream *st, int64_t base)
158{
159 uint32_t v;
160 MP3DecContext *mp3 = s->priv_data;
161
162 /* Check for VBRI tag (always 32 bytes after end of mpegaudio header) */
163 avio_seek(s->pb, base + 4 + 32, SEEK_SET);
164 v = avio_rb32(s->pb);
165 if (v == MKBETAG('V', 'B', 'R', 'I')) {
166 /* Check tag version */
167 if (avio_rb16(s->pb) == 1) {
168 /* skip delay and quality */
169 avio_skip(s->pb, 4);
170 mp3->size = avio_rb32(s->pb);
171 mp3->frames = avio_rb32(s->pb);
172 }
173 }
174}
175
ca851a3a 176/**
8862fb41 177 * Try to find Xing/Info/VBRI tags and compute duration from info therein
ca851a3a 178 */
853a9753 179static int mp3_parse_vbr_tags(AVFormatContext *s, AVStream *st, int64_t base)
ca851a3a 180{
db233e83 181 uint32_t v, spf;
cf92cec7 182 MPADecodeHeader c;
853a9753 183 int vbrtag_size = 0;
fbd8e042 184 MP3DecContext *mp3 = s->priv_data;
ca851a3a 185
b7effd4e 186 v = avio_rb32(s->pb);
5b5bd7c9 187 if(ff_mpa_check_header(v) < 0)
853a9753 188 return -1;
5b5bd7c9 189
82ab61f9 190 if (avpriv_mpegaudio_decode_header(&c, v) == 0)
853a9753 191 vbrtag_size = c.frame_size;
db233e83 192 if(c.layer != 3)
853a9753 193 return -1;
ca851a3a 194
50d1f443
AK
195 spf = c.lsf ? 576 : 1152; /* Samples per frame, layer 3 */
196
fbd8e042
AG
197 mp3->frames = 0;
198 mp3->size = 0;
db233e83 199
fbd8e042
AG
200 mp3_parse_info_tag(s, st, &c, spf);
201 mp3_parse_vbri_tag(s, st, base);
8862fb41 202
fbd8e042 203 if (!mp3->frames && !mp3->size)
853a9753
YS
204 return -1;
205
206 /* Skip the vbr tag frame */
6b4aa5da 207 avio_seek(s->pb, base + vbrtag_size, SEEK_SET);
db233e83 208
fbd8e042
AG
209 if (mp3->frames)
210 st->duration = av_rescale_q(mp3->frames, (AVRational){spf, c.sample_rate},
49d7ef28 211 st->time_base);
fbd8e042
AG
212 if (mp3->size && mp3->frames && !mp3->is_cbr)
213 st->codec->bit_rate = av_rescale(mp3->size, 8 * c.sample_rate, mp3->frames * (int64_t)spf);
49d7ef28 214
853a9753 215 return 0;
ca851a3a
216}
217
6e9651d1 218static int mp3_read_header(AVFormatContext *s)
6a58e151
FB
219{
220 AVStream *st;
bc5c918e 221 int64_t off;
15c5a8d2 222 int ret;
6a58e151 223
3b3bbdd3 224 st = avformat_new_stream(s, NULL);
6a58e151 225 if (!st)
769e10f0 226 return AVERROR(ENOMEM);
6a58e151 227
72415b2a 228 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
36ef5369 229 st->codec->codec_id = AV_CODEC_ID_MP3;
57004ff1 230 st->need_parsing = AVSTREAM_PARSE_FULL;
f97931c1 231 st->start_time = 0;
115329f1 232
9c5a9e6b 233 // lcm of all mp3 sample rates
c3f9ebf7 234 avpriv_set_pts_info(st, 64, 1, 14112000);
9c5a9e6b 235
a2704c97 236 off = avio_tell(s->pb);
80242f9b 237
d2d67e42 238 if (!av_dict_get(s->metadata, "", NULL, AV_DICT_IGNORE_SUFFIX))
7fd5aeb3 239 ff_id3v1_read(s);
6a58e151 240
853a9753 241 if (mp3_parse_vbr_tags(s, st, off) < 0)
6b4aa5da 242 avio_seek(s->pb, off, SEEK_SET);
ca851a3a 243
15c5a8d2
AK
244 ret = ff_replaygain_export(st, s->metadata);
245 if (ret < 0)
246 return ret;
247
6a58e151
FB
248 /* the parameters will be extracted from the compressed bitstream */
249 return 0;
250}
251
252#define MP3_PACKET_SIZE 1024
253
254static int mp3_read_packet(AVFormatContext *s, AVPacket *pkt)
255{
67b1156f 256 int ret;
115329f1 257
67b1156f 258 ret = av_get_packet(s->pb, pkt, MP3_PACKET_SIZE);
f73e3938
AK
259 if (ret < 0)
260 return ret;
6a58e151
FB
261
262 pkt->stream_index = 0;
045b80e5
CB
263
264 if (ret > ID3v1_TAG_SIZE &&
265 memcmp(&pkt->data[ret - ID3v1_TAG_SIZE], "TAG", 3) == 0)
266 ret -= ID3v1_TAG_SIZE;
267
6a58e151
FB
268 /* note: we need to modify the packet size here to handle the last
269 packet */
270 pkt->size = ret;
271 return ret;
272}
273
50d1f443
AK
274static int mp3_seek(AVFormatContext *s, int stream_index, int64_t timestamp,
275 int flags)
276{
505642f1 277 MP3DecContext *mp3 = s->priv_data;
50d1f443
AK
278 AVIndexEntry *ie;
279 AVStream *st = s->streams[0];
280 int64_t ret = av_index_search_timestamp(st, timestamp, flags);
281 uint32_t header = 0;
282
505642f1
MN
283 if (!mp3->xing_toc)
284 return AVERROR(ENOSYS);
285
50d1f443
AK
286 if (ret < 0)
287 return ret;
288
289 ie = &st->index_entries[ret];
290 ret = avio_seek(s->pb, ie->pos, SEEK_SET);
291 if (ret < 0)
292 return ret;
293
294 while (!s->pb->eof_reached) {
295 header = (header << 8) + avio_r8(s->pb);
296 if (ff_mpa_check_header(header) >= 0) {
297 ff_update_cur_dts(s, st, ie->timestamp);
298 ret = avio_seek(s->pb, -4, SEEK_CUR);
299 return (ret >= 0) ? 0 : ret;
300 }
301 }
302
303 return AVERROR_EOF;
304}
305
c6610a21 306AVInputFormat ff_mp3_demuxer = {
dfc2c4d9 307 .name = "mp3",
0177b7d2 308 .long_name = NULL_IF_CONFIG_SMALL("MP2/3 (MPEG audio layer 2/3)"),
dfc2c4d9
AK
309 .read_probe = mp3_read_probe,
310 .read_header = mp3_read_header,
311 .read_packet = mp3_read_packet,
50d1f443 312 .read_seek = mp3_seek,
505642f1 313 .priv_data_size = sizeof(MP3DecContext),
20234a4b 314 .flags = AVFMT_GENERIC_INDEX,
c0779a67 315 .extensions = "mp2,mp3,m2a,mpa", /* XXX: use probe */
de653ba3 316};