FFmpeg  1.2.4
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  int64_t header_filesize;
41  int xing_toc;
42  int start_pad;
43  int end_pad;
44  int is_cbr;
45 } MP3Context;
46 
47 /* mp3 read */
48 
50 {
51  int max_frames, first_frames = 0;
52  int fsize, frames, sample_rate;
53  uint32_t header;
54  const uint8_t *buf, *buf0, *buf2, *end;
55  AVCodecContext avctx;
56 
57  buf0 = p->buf;
58  end = p->buf + p->buf_size - sizeof(uint32_t);
59  while(buf0 < end && !*buf0)
60  buf0++;
61 
62  max_frames = 0;
63  buf = buf0;
64 
65  for(; buf < end; buf= buf2+1) {
66  buf2 = buf;
67 
68  for(frames = 0; buf2 < end; frames++) {
69  header = AV_RB32(buf2);
70  fsize = avpriv_mpa_decode_header(&avctx, header, &sample_rate, &sample_rate, &sample_rate, &sample_rate);
71  if(fsize < 0)
72  break;
73  buf2 += fsize;
74  }
75  max_frames = FFMAX(max_frames, frames);
76  if(buf == buf0)
77  first_frames= frames;
78  }
79  // keep this in sync with ac3 probe, both need to avoid
80  // issues with MPEG-files!
81  if (first_frames>=4) return AVPROBE_SCORE_MAX/2+1;
82  else if(max_frames>200)return AVPROBE_SCORE_MAX/2;
83  else if(max_frames>=4) return AVPROBE_SCORE_MAX/4;
84  else if(ff_id3v2_match(buf0, ID3v2_DEFAULT_MAGIC) && 2*ff_id3v2_tag_len(buf0) >= p->buf_size)
85  return AVPROBE_SCORE_MAX/8;
86  else if(max_frames>=1) return 1;
87  else return 0;
88 //mpegps_mp3_unrecognized_format.mpg has max_frames=3
89 }
90 
91 static void read_xing_toc(AVFormatContext *s, int64_t filesize, int64_t duration)
92 {
93  int i;
94  MP3Context *mp3 = s->priv_data;
95 
96  if (!filesize &&
97  !(filesize = avio_size(s->pb))) {
98  av_log(s, AV_LOG_WARNING, "Cannot determine file size, skipping TOC table.\n");
99  return;
100  }
101 
102  for (i = 0; i < XING_TOC_COUNT; i++) {
103  uint8_t b = avio_r8(s->pb);
104 
106  av_rescale(b, filesize, 256),
107  av_rescale(i, duration, XING_TOC_COUNT),
108  0, 0, AVINDEX_KEYFRAME);
109  }
110  mp3->xing_toc = 1;
111 }
112 
116 static int mp3_parse_vbr_tags(AVFormatContext *s, AVStream *st, int64_t base)
117 {
118  MP3Context *mp3 = s->priv_data;
119  uint32_t v, spf;
120  unsigned frames = 0; /* Total number of frames in file */
121  unsigned size = 0; /* Total number of bytes in the stream */
122  const int64_t xing_offtbl[2][2] = {{32, 17}, {17,9}};
124  int vbrtag_size = 0;
125  int is_cbr;
126 
127  v = avio_rb32(s->pb);
128  if(ff_mpa_check_header(v) < 0)
129  return -1;
130 
131  if (avpriv_mpegaudio_decode_header(&c, v) == 0)
132  vbrtag_size = c.frame_size;
133  if(c.layer != 3)
134  return -1;
135 
136  spf = c.lsf ? 576 : 1152; /* Samples per frame, layer 3 */
137 
138  /* Check for Xing / Info tag */
139  avio_skip(s->pb, xing_offtbl[c.lsf == 1][c.nb_channels == 1]);
140  v = avio_rb32(s->pb);
141  is_cbr = v == MKBETAG('I', 'n', 'f', 'o');
142  if (v == MKBETAG('X', 'i', 'n', 'g') || is_cbr) {
143  v = avio_rb32(s->pb);
144  if(v & XING_FLAG_FRAMES)
145  frames = avio_rb32(s->pb);
146  if(v & XING_FLAG_SIZE)
147  size = avio_rb32(s->pb);
148  if (v & XING_FLAG_TOC && frames)
149  read_xing_toc(s, size, av_rescale_q(frames, (AVRational){spf, c.sample_rate},
150  st->time_base));
151  if(v & 8)
152  avio_skip(s->pb, 4);
153 
154  v = avio_rb32(s->pb);
155  if(v == MKBETAG('L', 'A', 'M', 'E') || v == MKBETAG('L', 'a', 'v', 'f')) {
156  avio_skip(s->pb, 21-4);
157  v= avio_rb24(s->pb);
158  mp3->start_pad = v>>12;
159  mp3-> end_pad = v&4095;
160  st->skip_samples = mp3->start_pad + 528 + 1;
161  av_log(s, AV_LOG_DEBUG, "pad %d %d\n", mp3->start_pad, mp3-> end_pad);
162  }
163  }
164 
165  /* Check for VBRI tag (always 32 bytes after end of mpegaudio header) */
166  avio_seek(s->pb, base + 4 + 32, SEEK_SET);
167  v = avio_rb32(s->pb);
168  if(v == MKBETAG('V', 'B', 'R', 'I')) {
169  /* Check tag version */
170  if(avio_rb16(s->pb) == 1) {
171  /* skip delay and quality */
172  avio_skip(s->pb, 4);
173  size = avio_rb32(s->pb);
174  frames = avio_rb32(s->pb);
175  }
176  }
177 
178  if(!frames && !size)
179  return -1;
180 
181  /* Skip the vbr tag frame */
182  avio_seek(s->pb, base + vbrtag_size, SEEK_SET);
183 
184  if(frames)
185  st->duration = av_rescale_q(frames, (AVRational){spf, c.sample_rate},
186  st->time_base);
187  if (size && frames && !is_cbr)
188  st->codec->bit_rate = av_rescale(size, 8 * c.sample_rate, frames * (int64_t)spf);
189 
190  mp3->is_cbr = is_cbr;
191  mp3->header_filesize = size;
192 
193  return 0;
194 }
195 
197 {
198  MP3Context *mp3 = s->priv_data;
199  AVStream *st;
200  int64_t off;
201 
202  st = avformat_new_stream(s, NULL);
203  if (!st)
204  return AVERROR(ENOMEM);
205 
209  st->start_time = 0;
210 
211  // lcm of all mp3 sample rates
212  avpriv_set_pts_info(st, 64, 1, 14112000);
213 
214  s->pb->maxsize = -1;
215  off = avio_tell(s->pb);
216 
218  ff_id3v1_read(s);
219 
220  if(s->pb->seekable)
221  mp3->filesize = avio_size(s->pb);
222 
223  if (mp3_parse_vbr_tags(s, st, off) < 0)
224  avio_seek(s->pb, off, SEEK_SET);
225 
226  /* the parameters will be extracted from the compressed bitstream */
227  return 0;
228 }
229 
230 #define MP3_PACKET_SIZE 1024
231 
233 {
234  MP3Context *mp3 = s->priv_data;
235  int ret, size;
236  int64_t pos;
237 
238  size= MP3_PACKET_SIZE;
239  pos = avio_tell(s->pb);
240  if(mp3->filesize > ID3v1_TAG_SIZE && pos < mp3->filesize)
241  size= FFMIN(size, mp3->filesize - pos);
242 
243  ret= av_get_packet(s->pb, pkt, size);
244  if (ret <= 0) {
245  if(ret<0)
246  return ret;
247  return AVERROR_EOF;
248  }
249 
250  pkt->flags &= ~AV_PKT_FLAG_CORRUPT;
251  pkt->stream_index = 0;
252 
253  if (ret >= ID3v1_TAG_SIZE &&
254  memcmp(&pkt->data[ret - ID3v1_TAG_SIZE], "TAG", 3) == 0)
255  ret -= ID3v1_TAG_SIZE;
256 
257  /* note: we need to modify the packet size here to handle the last
258  packet */
259  pkt->size = ret;
260  return ret;
261 }
262 
263 static int check(AVFormatContext *s, int64_t pos)
264 {
265  int64_t ret = avio_seek(s->pb, pos, SEEK_SET);
266  unsigned header;
267  MPADecodeHeader sd;
268  if (ret < 0)
269  return ret;
270  header = avio_rb32(s->pb);
271  if (ff_mpa_check_header(header) < 0)
272  return -1;
273  if (avpriv_mpegaudio_decode_header(&sd, header) == 1)
274  return -1;
275  return sd.frame_size;
276 }
277 
278 static int mp3_seek(AVFormatContext *s, int stream_index, int64_t timestamp,
279  int flags)
280 {
281  MP3Context *mp3 = s->priv_data;
282  AVIndexEntry *ie, ie1;
283  AVStream *st = s->streams[0];
284  int64_t ret = av_index_search_timestamp(st, timestamp, flags);
285  int i, j;
286 
287  if (mp3->is_cbr && st->duration > 0 && mp3->header_filesize > s->data_offset) {
288  int64_t filesize = avio_size(s->pb);
289  int64_t duration;
290  if (filesize <= s->data_offset)
291  filesize = mp3->header_filesize;
292  filesize -= s->data_offset;
293  duration = av_rescale(st->duration, filesize, mp3->header_filesize - s->data_offset);
294  ie = &ie1;
295  timestamp = av_clip64(timestamp, 0, duration);
296  ie->timestamp = timestamp;
297  ie->pos = av_rescale(timestamp, filesize, duration) + s->data_offset;
298  } else if (mp3->xing_toc) {
299  if (ret < 0)
300  return ret;
301 
302  ie = &st->index_entries[ret];
303  } else {
304  st->skip_samples = timestamp <= 0 ? mp3->start_pad + 528 + 1 : 0;
305 
306  return -1;
307  }
308 
309  ret = avio_seek(s->pb, ie->pos, SEEK_SET);
310  if (ret < 0)
311  return ret;
312 
313 #define MIN_VALID 3
314  for(i=0; i<4096; i++) {
315  int64_t pos = ie->pos + i;
316  for(j=0; j<MIN_VALID; j++) {
317  ret = check(s, pos);
318  if(ret < 0)
319  break;
320  pos += ret;
321  }
322  if(j==MIN_VALID)
323  break;
324  }
325  if(j!=MIN_VALID)
326  i=0;
327 
328  ret = avio_seek(s->pb, ie->pos + i, SEEK_SET);
329  if (ret < 0)
330  return ret;
331  ff_update_cur_dts(s, st, ie->timestamp);
332  st->skip_samples = ie->timestamp <= 0 ? mp3->start_pad + 528 + 1 : 0;
333  return 0;
334 }
335 
337  .name = "mp3",
338  .long_name = NULL_IF_CONFIG_SMALL("MP2/3 (MPEG audio layer 2/3)"),
339  .priv_data_size = sizeof(MP3Context),
343  .read_seek = mp3_seek,
345  .extensions = "mp2,mp3,m2a", /* XXX: use probe */
346 };