mp3dec.c
Go to the documentation of this file.
1 /*
2  * MP3 demuxer
3  * Copyright (c) 2003 Fabrice Bellard
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21 
22 #include "libavutil/avstring.h"
23 #include "libavutil/intreadwrite.h"
24 #include "libavutil/dict.h"
25 #include "libavutil/mathematics.h"
26 #include "avformat.h"
27 #include "internal.h"
28 #include "id3v2.h"
29 #include "id3v1.h"
31 
32 #define XING_FLAG_FRAMES 0x01
33 #define XING_FLAG_SIZE 0x02
34 #define XING_FLAG_TOC 0x04
35 
36 #define XING_TOC_COUNT 100
37 
38 typedef struct {
39  int64_t filesize;
40  int xing_toc;
41  int start_pad;
42  int end_pad;
44 
45 /* mp3 read */
46 
48 {
49  int max_frames, first_frames = 0;
50  int fsize, frames, sample_rate;
51  uint32_t header;
52  const uint8_t *buf, *buf0, *buf2, *end;
53  AVCodecContext avctx;
54 
55  buf0 = p->buf;
56  end = p->buf + p->buf_size - sizeof(uint32_t);
57  while(buf0 < end && !*buf0)
58  buf0++;
59 
60  max_frames = 0;
61  buf = buf0;
62 
63  for(; buf < end; buf= buf2+1) {
64  buf2 = buf;
65 
66  for(frames = 0; buf2 < end; frames++) {
67  header = AV_RB32(buf2);
68  fsize = avpriv_mpa_decode_header(&avctx, header, &sample_rate, &sample_rate, &sample_rate, &sample_rate);
69  if(fsize < 0)
70  break;
71  buf2 += fsize;
72  }
73  max_frames = FFMAX(max_frames, frames);
74  if(buf == buf0)
75  first_frames= frames;
76  }
77  // keep this in sync with ac3 probe, both need to avoid
78  // issues with MPEG-files!
79  if (first_frames>=4) return AVPROBE_SCORE_MAX/2+1;
80  else if(max_frames>200)return AVPROBE_SCORE_MAX/2;
81  else if(max_frames>=4) return AVPROBE_SCORE_MAX/4;
82  else if(ff_id3v2_match(buf0, ID3v2_DEFAULT_MAGIC) && 2*ff_id3v2_tag_len(buf0) >= p->buf_size)
83  return AVPROBE_SCORE_MAX/8;
84  else if(max_frames>=1) return 1;
85  else return 0;
86 //mpegps_mp3_unrecognized_format.mpg has max_frames=3
87 }
88 
89 static void read_xing_toc(AVFormatContext *s, int64_t filesize, int64_t duration)
90 {
91  int i;
93 
94  if (!filesize &&
95  !(filesize = avio_size(s->pb))) {
96  av_log(s, AV_LOG_WARNING, "Cannot determine file size, skipping TOC table.\n");
97  return;
98  }
99 
100  for (i = 0; i < XING_TOC_COUNT; i++) {
101  uint8_t b = avio_r8(s->pb);
102 
104  av_rescale(b, filesize, 256),
105  av_rescale(i, duration, XING_TOC_COUNT),
106  0, 0, AVINDEX_KEYFRAME);
107  }
108  mp3->xing_toc = 1;
109 }
110 
111 /**
112  * Try to find Xing/Info/VBRI tags and compute duration from info therein
113  */
114 static int mp3_parse_vbr_tags(AVFormatContext *s, AVStream *st, int64_t base)
115 {
117  uint32_t v, spf;
118  unsigned frames = 0; /* Total number of frames in file */
119  unsigned size = 0; /* Total number of bytes in the stream */
120  const int64_t xing_offtbl[2][2] = {{32, 17}, {17,9}};
122  int vbrtag_size = 0;
123  int is_cbr;
124 
125  v = avio_rb32(s->pb);
126  if(ff_mpa_check_header(v) < 0)
127  return -1;
128 
129  if (avpriv_mpegaudio_decode_header(&c, v) == 0)
130  vbrtag_size = c.frame_size;
131  if(c.layer != 3)
132  return -1;
133 
134  spf = c.lsf ? 576 : 1152; /* Samples per frame, layer 3 */
135 
136  /* Check for Xing / Info tag */
137  avio_skip(s->pb, xing_offtbl[c.lsf == 1][c.nb_channels == 1]);
138  v = avio_rb32(s->pb);
139  is_cbr = v == MKBETAG('I', 'n', 'f', 'o');
140  if (v == MKBETAG('X', 'i', 'n', 'g') || is_cbr) {
141  v = avio_rb32(s->pb);
142  if(v & XING_FLAG_FRAMES)
143  frames = avio_rb32(s->pb);
144  if(v & XING_FLAG_SIZE)
145  size = avio_rb32(s->pb);
146  if (v & XING_FLAG_TOC && frames)
147  read_xing_toc(s, size, av_rescale_q(frames, (AVRational){spf, c.sample_rate},
148  st->time_base));
149  if(v & 8)
150  avio_skip(s->pb, 4);
151 
152  v = avio_rb32(s->pb);
153  if(v == MKBETAG('L', 'A', 'M', 'E') || v == MKBETAG('L', 'a', 'v', 'f')) {
154  avio_skip(s->pb, 21-4);
155  v= avio_rb24(s->pb);
156  mp3->start_pad = v>>12;
157  mp3-> end_pad = v&4095;
158  st->skip_samples = mp3->start_pad + 528 + 1;
159  av_log(s, AV_LOG_DEBUG, "pad %d %d\n", mp3->start_pad, mp3-> end_pad);
160  }
161  }
162 
163  /* Check for VBRI tag (always 32 bytes after end of mpegaudio header) */
164  avio_seek(s->pb, base + 4 + 32, SEEK_SET);
165  v = avio_rb32(s->pb);
166  if(v == MKBETAG('V', 'B', 'R', 'I')) {
167  /* Check tag version */
168  if(avio_rb16(s->pb) == 1) {
169  /* skip delay and quality */
170  avio_skip(s->pb, 4);
171  size = avio_rb32(s->pb);
172  frames = avio_rb32(s->pb);
173  }
174  }
175 
176  if(!frames && !size)
177  return -1;
178 
179  /* Skip the vbr tag frame */
180  avio_seek(s->pb, base + vbrtag_size, SEEK_SET);
181 
182  if(frames)
183  st->duration = av_rescale_q(frames, (AVRational){spf, c.sample_rate},
184  st->time_base);
185  if (size && frames && !is_cbr)
186  st->codec->bit_rate = av_rescale(size, 8 * c.sample_rate, frames * (int64_t)spf);
187 
188  return 0;
189 }
190 
192 {
194  AVStream *st;
195  int64_t off;
196 
197  st = avformat_new_stream(s, NULL);
198  if (!st)
199  return AVERROR(ENOMEM);
200 
204  st->start_time = 0;
205 
206  // lcm of all mp3 sample rates
207  avpriv_set_pts_info(st, 64, 1, 14112000);
208 
209  s->pb->maxsize = -1;
210  off = avio_tell(s->pb);
211 
213  ff_id3v1_read(s);
214 
215  if(s->pb->seekable)
216  mp3->filesize = avio_size(s->pb);
217 
218  if (mp3_parse_vbr_tags(s, st, off) < 0)
219  avio_seek(s->pb, off, SEEK_SET);
220 
221  /* the parameters will be extracted from the compressed bitstream */
222  return 0;
223 }
224 
225 #define MP3_PACKET_SIZE 1024
226 
228 {
230  int ret, size;
231  int64_t pos;
232 
233  size= MP3_PACKET_SIZE;
234  pos = avio_tell(s->pb);
235  if(mp3->filesize > ID3v1_TAG_SIZE && pos < mp3->filesize)
236  size= FFMIN(size, mp3->filesize - pos);
237 
238  ret= av_get_packet(s->pb, pkt, size);
239  if (ret <= 0) {
240  if(ret<0)
241  return ret;
242  return AVERROR_EOF;
243  }
244 
245  pkt->flags &= ~AV_PKT_FLAG_CORRUPT;
246  pkt->stream_index = 0;
247 
248  if (ret >= ID3v1_TAG_SIZE &&
249  memcmp(&pkt->data[ret - ID3v1_TAG_SIZE], "TAG", 3) == 0)
250  ret -= ID3v1_TAG_SIZE;
251 
252  /* note: we need to modify the packet size here to handle the last
253  packet */
254  pkt->size = ret;
255  return ret;
256 }
257 
258 static int check(AVFormatContext *s, int64_t pos)
259 {
260  int64_t ret = avio_seek(s->pb, pos, SEEK_SET);
261  unsigned header;
262  MPADecodeHeader sd;
263  if (ret < 0)
264  return ret;
265  header = avio_rb32(s->pb);
266  if (ff_mpa_check_header(header) < 0)
267  return -1;
268  if (avpriv_mpegaudio_decode_header(&sd, header) == 1)
269  return -1;
270  return sd.frame_size;
271 }
272 
273 static int mp3_seek(AVFormatContext *s, int stream_index, int64_t timestamp,
274  int flags)
275 {
277  AVIndexEntry *ie;
278  AVStream *st = s->streams[0];
279  int64_t ret = av_index_search_timestamp(st, timestamp, flags);
280  int i, j;
281 
282  if (!mp3->xing_toc) {
283  st->skip_samples = timestamp <= 0 ? mp3->start_pad + 528 + 1 : 0;
284 
285  return -1;
286  }
287 
288  if (ret < 0)
289  return ret;
290 
291  ie = &st->index_entries[ret];
292  ret = avio_seek(s->pb, ie->pos, SEEK_SET);
293  if (ret < 0)
294  return ret;
295 
296 #define MIN_VALID 3
297  for(i=0; i<4096; i++) {
298  int64_t pos = ie->pos + i;
299  for(j=0; j<MIN_VALID; j++) {
300  ret = check(s, pos);
301  if(ret < 0)
302  break;
303  pos += ret;
304  }
305  if(j==MIN_VALID)
306  break;
307  }
308  if(j!=MIN_VALID)
309  i=0;
310 
311  ret = avio_seek(s->pb, ie->pos + i, SEEK_SET);
312  if (ret < 0)
313  return ret;
314  ff_update_cur_dts(s, st, ie->timestamp);
315  st->skip_samples = ie->timestamp <= 0 ? mp3->start_pad + 528 + 1 : 0;
316  return 0;
317 }
318 
320  .name = "mp3",
321  .long_name = NULL_IF_CONFIG_SMALL("MP2/3 (MPEG audio layer 2/3)"),
322  .read_probe = mp3_read_probe,
323  .read_header = mp3_read_header,
324  .read_packet = mp3_read_packet,
325  .read_seek = mp3_seek,
326  .priv_data_size = sizeof(MP3DecContext),
328  .extensions = "mp2,mp3,m2a", /* XXX: use probe */
329 };
float v
const char * s
Definition: avisynth_c.h:668
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:261
int av_add_index_entry(AVStream *st, int64_t pos, int64_t timestamp, int size, int distance, int flags)
Add an index entry into a sorted list.
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
int64_t pos
Definition: avformat.h:592
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:154
void ff_id3v1_read(AVFormatContext *s)
Read an ID3v1 tag.
Definition: id3v1.c:228
#define ID3v2_DEFAULT_MAGIC
Default magic bytes for ID3v2 header: "ID3".
Definition: id3v2.h:35
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:199
AVIndexEntry * index_entries
Only used if the format does not support seeking natively.
Definition: avformat.h:822
static void read_xing_toc(AVFormatContext *s, int64_t filesize, int64_t duration)
Definition: mp3dec.c:89
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:256
unsigned int avio_rb16(AVIOContext *s)
Definition: aviobuf.c:595
int64_t maxsize
max filesize, used to limit allocations This field is internal to libavformat and access from outside...
Definition: avio.h:123
AVDictionaryEntry * av_dict_get(AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition: dict.c:39
static int check(AVFormatContext *s, int64_t pos)
Definition: mp3dec.c:258
Format I/O context.
Definition: avformat.h:944
void ff_update_cur_dts(AVFormatContext *s, AVStream *ref_st, int64_t timestamp)
Update cur_dts of all streams based on the given timestamp and AVStream.
Public dictionary API.
static int mp3_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: mp3dec.c:227
uint8_t
#define XING_FLAG_SIZE
Definition: mp3dec.c:33
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:610
static int mp3_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Definition: mp3dec.c:273
#define AV_RB32
static AVPacket pkt
Definition: demuxing.c:56
enum AVStreamParseType need_parsing
Definition: avformat.h:811
#define b
Definition: input.c:42
end end
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
AVStream ** streams
Definition: avformat.h:992
uint8_t * data
int avpriv_mpegaudio_decode_header(MPADecodeHeader *s, uint32_t header)
static const uint8_t xing_offtbl[2][2]
Definition: mp3enc.c:113
#define AVERROR_EOF
End of file.
Definition: error.h:55
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:248
static int64_t duration
Definition: ffplay.c:294
full parsing and repack with timestamp and position generation by parser for raw this assumes that ea...
Definition: avformat.h:586
int64_t filesize
Definition: mp3dec.c:39
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:130
#define AVINDEX_KEYFRAME
Definition: avformat.h:599
AVDictionary * metadata
Definition: avformat.h:1092
int av_index_search_timestamp(AVStream *st, int64_t timestamp, int flags)
Get the index for a specific timestamp.
int64_t timestamp
Timestamp in AVStream.time_base units, preferably the time from which on correctly decoded frames are...
Definition: avformat.h:593
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
static int mp3_read_header(AVFormatContext *s)
Definition: mp3dec.c:191
preferred ID for decoding MPEG audio layer 1, 2 or 3
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
static int ff_mpa_check_header(uint32_t header)
int skip_samples
Number of samples to skip at the start of the frame decoded from the next packet. ...
Definition: avformat.h:844
#define FFMAX(a, b)
Definition: common.h:56
int size
int flags
A combination of AV_PKT_FLAG values.
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:469
int avpriv_mpa_decode_header(AVCodecContext *avctx, uint32_t head, int *sample_rate, int *channels, int *frame_size, int *bit_rate)
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:662
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:337
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:336
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:117
int bit_rate
the average bitrate
unsigned int avio_rb24(AVIOContext *s)
Definition: aviobuf.c:603
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
Definition: mathematics.c:118
#define MP3_PACKET_SIZE
Definition: mp3dec.c:225
#define FFMIN(a, b)
Definition: common.h:58
ret
Definition: avfilter.c:821
int end_pad
Definition: mp3dec.c:42
if it could not because there are no more frames
int xing_toc
Definition: mp3dec.c:40
#define MIN_VALID
Stream structure.
Definition: avformat.h:643
static int mp3_read_probe(AVProbeData *p)
Definition: mp3dec.c:47
NULL
Definition: eval.c:55
sample_rate
enum AVMediaType codec_type
enum AVCodecID codec_id
AVIOContext * pb
I/O context.
Definition: avformat.h:977
main external API structure.
#define XING_FLAG_FRAMES
Definition: mp3dec.c:32
void * buf
Definition: avisynth_c.h:594
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:353
synthesis window for stochastic i
rational number numerator/denominator
Definition: rational.h:43
This structure contains the data a format has to probe a file.
Definition: avformat.h:334
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Buffer references ownership and permissions
static int flags
Definition: cpu.c:23
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:696
#define AVPROBE_SCORE_MAX
maximum score, half of that is used for file-extension-based detection
Definition: avformat.h:340
MPEG Audio header decoder.
Main libavformat public API header.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:162
int ff_id3v2_match(const uint8_t *buf, const char *magic)
Detect ID3v2 Header.
Definition: id3v2.c:139
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base...
Definition: avformat.h:689
AVInputFormat ff_mp3_demuxer
Definition: mp3dec.c:319
static double c[64]
#define AV_PKT_FLAG_CORRUPT
The packet content is corrupted.
#define MKBETAG(a, b, c, d)
Definition: common.h:283
#define XING_FLAG_TOC
Definition: mp3dec.c:34
int ff_id3v2_tag_len(const uint8_t *buf)
Get the length of an ID3v2 tag.
Definition: id3v2.c:152
void * priv_data
Format private data.
Definition: avformat.h:964
#define XING_TOC_COUNT
Definition: mp3dec.c:36
static int mp3_parse_vbr_tags(AVFormatContext *s, AVStream *st, int64_t base)
Try to find Xing/Info/VBRI tags and compute duration from info therein.
Definition: mp3dec.c:114
#define ID3v1_TAG_SIZE
Definition: id3v1.h:27
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:461
#define AV_DICT_IGNORE_SUFFIX
Definition: dict.h:68
int start_pad
Definition: mp3dec.c:41
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:679
This structure stores compressed data.