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
34
36
37 #define XING_FLAG_FRAMES 0x01
38 #define XING_FLAG_SIZE 0x02
39 #define XING_FLAG_TOC 0x04
40 #define XING_FLAC_QSCALE 0x08
41
42 #define XING_TOC_COUNT 100
43
51 unsigned frames;
/* Total number of frames in file */
55
56 /* mp3 read */
57
59 {
60 int max_frames, first_frames = 0;
61 int fsize, frames;
65
66 if (!avctx)
67 return 0;
68
71 while(buf0 < end && !*buf0)
72 buf0++;
73
74 max_frames = 0;
75 buf = buf0;
76
77 for(; buf <
end; buf= buf2+1) {
80 continue;
81
82 for(frames = 0; buf2 <
end; frames++) {
86 if(fsize < 0)
87 break;
88 buf2 += fsize;
89 }
90 max_frames =
FFMAX(max_frames, frames);
91 if(buf == buf0)
92 first_frames= frames;
93 }
95 // keep this in sync with ac3 probe, both need to avoid
96 // issues with MPEG-files!
102 else if(max_frames>=1 && max_frames >= p->
buf_size/10000)
return 1;
103 else return 0;
104 //mpegps_mp3_unrecognized_format.mpg has max_frames=3
105 }
106
108 {
109 int i;
111 int fill_index = mp3->
usetoc && duration > 0;
112
113 if (!filesize &&
116 fill_index = 0;
117 }
118
121 if (fill_index)
126 }
127 if (fill_index)
129 }
130
133 {
134 #define LAST_BITS(k, n) ((k) & ((1 << (n)) - 1))
135 #define MIDDLE_BITS(k, m, n) LAST_BITS((k) >> (m), ((n) - (m)))
136
137 uint16_t crc;
139
141
142 uint32_t peak = 0;
143 int32_t r_gain = INT32_MIN, a_gain = INT32_MIN;
144
146 static const int64_t
xing_offtbl[2][2] = {{32, 17}, {17,9}};
148
149 /* Check for Xing / Info tag */
150 avio_skip(s->
pb, xing_offtbl[c->lsf == 1][c->nb_channels == 1]);
154 return;
155
167 } else if (delta > min >> 4) {
169 "filesize and duration do not match (growing file?)\n");
170 }
171 }
175 st->time_base));
176 /* VBR quality */
179
180 /* Encoder short version string */
183
184 /* Info Tag revision + VBR method */
186
187 /* Lowpass filter value */
189
190 /* ReplayGain peak */
193
194 /* Radio ReplayGain */
196
199
201 r_gain *= -1;
202 }
203
204 /* Audiophile ReplayGain */
206
209
211 a_gain *= -1;
212 }
213
214 /* Encoding flags + ATH Type */
216
217 /* if ABR {specified bitrate} else {minimal bitrate} */
219
220 /* Encoder delays */
225 ) {
226
227 mp3->start_pad =
v>>12;
228 mp3-> end_pad =
v&4095;
229 st->skip_samples = mp3->start_pad + 528 + 1;
230 if (mp3->frames) {
231 st->first_discard_sample = -mp3->end_pad + 528 + 1 + mp3->frames * (int64_t)spf;
232 st->last_discard_sample = mp3->frames * (int64_t)spf;
233 }
234 if (!st->start_time)
237 st->time_base);
239 }
240
241 /* Misc */
243
244 /* MP3 gain */
246
247 /* Preset and surround info */
249
250 /* Music length */
252
253 /* Music CRC */
255
256 /* Info Tag CRC */
259
263 }
264 }
265
267 {
270
271 /* Check for VBRI tag (always 32 bytes after end of mpegaudio header) */
274 if (v ==
MKBETAG(
'V',
'B',
'R',
'I')) {
275 /* Check tag version */
277 /* skip delay and quality */
281 }
282 }
283 }
284
285 /**
286 * Try to find Xing/Info/VBRI tags and compute duration from info therein
287 */
289 {
292 int vbrtag_size = 0;
294
296
299 return -1;
300
302 vbrtag_size = c.frame_size;
303 if(c.layer != 3)
304 return -1;
305
306 spf = c.lsf ? 576 : 1152; /* Samples per frame, layer 3 */
307
310
313
315 return -1;
316
317 /* Skip the vbr tag frame */
319
322 st->time_base);
323 if (mp3->header_filesize && mp3->frames && !mp3->is_cbr)
324 st->codec->bit_rate =
av_rescale(mp3->header_filesize, 8 *
c.sample_rate, mp3->frames * (int64_t)spf);
325
326 return 0;
327 }
328
330 {
333 int64_t off;
335
337 if (!st)
339
344
345 // lcm of all mp3 sample rates
347
350
353
356
359
361 if (ret < 0)
363
364 /* the parameters will be extracted from the compressed bitstream */
365 return 0;
366 }
367
368 #define MP3_PACKET_SIZE 1024
369
371 {
374 int64_t pos;
375
380
382 if (ret <= 0) {
383 if(ret<0)
386 }
387
390
394
395 /* note: we need to modify the packet size here to handle the last
396 packet */
399 }
400
402 {
406 if (ret < 0)
410 return -1;
412 return -1;
413 return sd.frame_size;
414 }
415
418 {
423 int i, j;
425 int64_t best_pos;
426 int best_score;
427
434 if (filesize <= s->internal->data_offset)
438 ie = &ie1;
439 timestamp = av_clip64(timestamp, 0, duration);
443 if (ret < 0)
445
447 } else {
449
450 return -1;
451 }
452
455 if (ret < 0)
457
458 #define MIN_VALID 3
460 best_score = 999;
461 for(i=0; i<4096; i++) {
462 int64_t pos = ie->
pos + (dir > 0 ? i - 1024 : -i);
463 int64_t candidate = -1;
464 int score = 999;
465
466 if (pos < 0)
467 continue;
468
471 if(ret < 0)
472 break;
473 if ((ie->
pos - pos)*dir <= 0 && abs(MIN_VALID/2-j) < score) {
474 candidate = pos;
475 score = abs(MIN_VALID/2-j);
476 }
478 }
479 if (best_score > score && j == MIN_VALID) {
480 best_pos = candidate;
481 best_score = score;
482 if(score == 0)
483 break;
484 }
485 }
486
488 if (ret < 0)
492 return 0;
493 }
494
498 };
499
506 };
507
517 .extensions = "mp2,mp3,m2a,mpa", /* XXX: use probe */
518 .priv_class = &demuxer_class,
519 };