FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/mp3dec.c
Date: 2026-09-19 14:27:34
Exec Total Coverage
Lines: 312 349 89.4%
Functions: 12 12 100.0%
Branches: 198 264 75.0%

Line Branch Exec Source
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 <string.h>
23
24 #include "libavutil/opt.h"
25 #include "libavutil/intreadwrite.h"
26 #include "libavutil/dict.h"
27 #include "libavutil/mathematics.h"
28 #include "libavutil/mem.h"
29 #include "avformat.h"
30 #include "internal.h"
31 #include "avio_internal.h"
32 #include "demux.h"
33 #include "id3v2.h"
34 #include "id3v1.h"
35 #include "replaygain.h"
36
37 #include "libavcodec/codec_id.h"
38 #include "libavcodec/mpegaudio.h"
39 #include "libavcodec/mpegaudiodecheader.h"
40
41 #define XING_FLAG_FRAMES 0x01
42 #define XING_FLAG_SIZE 0x02
43 #define XING_FLAG_TOC 0x04
44 #define XING_FLAC_QSCALE 0x08
45
46 #define XING_TOC_COUNT 100
47
48
49 typedef struct {
50 AVClass *class;
51 int64_t filesize;
52 int xing_toc;
53 int start_pad;
54 int end_pad;
55 int usetoc;
56 unsigned frames; /* Total number of frames in file */
57 unsigned header_filesize; /* Total number of bytes in the stream */
58 unsigned frame_duration; /* Frame duration in st->time_base */
59 int is_cbr;
60 } MP3DecContext;
61
62 enum CheckRet {
63 CHECK_WRONG_HEADER = -1,
64 CHECK_SEEK_FAILED = -2,
65 };
66
67 static int check(AVIOContext *pb, int64_t pos, uint32_t *header);
68
69 /* mp3 read */
70
71 8059 static int mp3_read_probe(const AVProbeData *p)
72 {
73 8059 int max_frames, first_frames = 0;
74 8059 int whole_used = 0;
75 int frames, ret;
76 int framesizes, max_framesizes;
77 uint32_t header;
78 const uint8_t *buf, *buf0, *buf2, *buf3, *end;
79 8059 MPADecodeHeader2 *h = NULL;
80
81 8059 buf0 = p->buf;
82 8059 end = p->buf + p->buf_size - sizeof(uint32_t);
83
3/4
✓ Branch 0 taken 119548 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 111489 times.
✓ Branch 3 taken 8059 times.
119548 while (buf0 < end && !*buf0)
84 111489 buf0++;
85
86 8059 max_frames = 0;
87 8059 max_framesizes = 0;
88 8059 buf = buf0;
89
90
2/2
✓ Branch 0 taken 463854446 times.
✓ Branch 1 taken 8059 times.
463862505 for (; buf < end; buf = buf2+1) {
91 463854446 buf2 = buf;
92
2/2
✓ Branch 0 taken 463927279 times.
✓ Branch 1 taken 1 times.
463927280 for (framesizes = frames = 0; buf2 < end; frames++) {
93 463927279 int header_emu = 0;
94 int available;
95
96 463927279 header = AV_RB32(buf2);
97 463927279 ret = avpriv_mpegaudio_decode_header2(&h, header);
98
2/2
✓ Branch 0 taken 463852168 times.
✓ Branch 1 taken 75111 times.
463927279 if (ret != 0)
99 463852168 break;
100
101 75111 available = FFMIN(h->frame_size, end - buf2);
102
2/2
✓ Branch 0 taken 29586986 times.
✓ Branch 1 taken 75111 times.
29662097 for (buf3 = buf2 + 4; buf3 < buf2 + available; buf3++) {
103 29586986 uint32_t next_sync = AV_RB32(buf3);
104 29586986 header_emu += (next_sync & MP3_MASK) == (header & MP3_MASK);
105 }
106
2/2
✓ Branch 0 taken 1160 times.
✓ Branch 1 taken 73951 times.
75111 if (header_emu > 2)
107 1160 break;
108 73951 framesizes += h->frame_size;
109
2/2
✓ Branch 0 taken 1117 times.
✓ Branch 1 taken 72834 times.
73951 if (available < h->frame_size) {
110 1117 frames++;
111 1117 break;
112 }
113 72834 buf2 += h->frame_size;
114 }
115 463854446 max_frames = FFMAX(max_frames, frames);
116 463854446 max_framesizes = FFMAX(max_framesizes, framesizes);
117
2/2
✓ Branch 0 taken 8059 times.
✓ Branch 1 taken 463846387 times.
463854446 if (buf == buf0) {
118 8059 first_frames= frames;
119
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8059 times.
8059 if (buf2 == end + sizeof(uint32_t))
120 whole_used = 1;
121 }
122 }
123 8059 av_freep(&h);
124
125 // keep this in sync with ac3 probe, both need to avoid
126 // issues with MPEG-files!
127
2/2
✓ Branch 0 taken 67 times.
✓ Branch 1 taken 7992 times.
8059 if (first_frames>=7) return AVPROBE_SCORE_EXTENSION + 2;
128
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7991 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
7992 else if (max_frames>200 && p->buf_size < 2*max_framesizes)return AVPROBE_SCORE_EXTENSION;
129
4/4
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 7958 times.
✓ Branch 2 taken 30 times.
✓ Branch 3 taken 3 times.
7991 else if (max_frames>=4 && p->buf_size < 2*max_framesizes) return AVPROBE_SCORE_EXTENSION / 2;
130
3/4
✓ Branch 1 taken 46 times.
✓ Branch 2 taken 7915 times.
✓ Branch 4 taken 46 times.
✗ Branch 5 not taken.
7961 else if (ff_id3v2_match(buf0, ID3v2_DEFAULT_MAGIC) && 2*ff_id3v2_tag_len(buf0) >= p->buf_size)
131
1/2
✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
46 return p->buf_size < PROBE_BUF_MAX ? AVPROBE_SCORE_EXTENSION / 4 : AVPROBE_SCORE_EXTENSION - 2;
132
3/4
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 7902 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 13 times.
7915 else if (first_frames > 1 && whole_used) return 5;
133
4/4
✓ Branch 0 taken 4578 times.
✓ Branch 1 taken 3337 times.
✓ Branch 2 taken 1373 times.
✓ Branch 3 taken 3205 times.
7915 else if (max_frames>=1 && p->buf_size < 10*max_framesizes) return 1;
134 6542 else return 0;
135 //mpegps_mp3_unrecognized_format.mpg has max_frames=3
136 }
137
138 35 static void read_xing_toc(AVFormatContext *s, int64_t filesize, int64_t duration)
139 {
140 int i;
141 35 MP3DecContext *mp3 = s->priv_data;
142 35 int fast_seek = s->flags & AVFMT_FLAG_FAST_SEEK;
143
4/6
✓ Branch 0 taken 35 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 34 times.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
35 int fill_index = (mp3->usetoc || fast_seek) && duration > 0;
144
145
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 35 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
35 if (!filesize &&
146 (filesize = avio_size(s->pb)) <= 0) {
147 av_log(s, AV_LOG_WARNING, "Cannot determine file size, skipping TOC table.\n");
148 fill_index = 0;
149 filesize = 0;
150 }
151
152
2/2
✓ Branch 0 taken 3500 times.
✓ Branch 1 taken 35 times.
3535 for (i = 0; i < XING_TOC_COUNT; i++) {
153 3500 uint8_t b = avio_r8(s->pb);
154
2/2
✓ Branch 0 taken 100 times.
✓ Branch 1 taken 3400 times.
3500 if (fill_index)
155 100 av_add_index_entry(s->streams[0],
156 av_rescale(b, filesize, 256),
157 av_rescale(i, duration, XING_TOC_COUNT),
158 0, 0, AVINDEX_KEYFRAME);
159 }
160
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 34 times.
35 if (fill_index)
161 1 mp3->xing_toc = 1;
162 35 }
163
164 78 static void mp3_parse_info_tag(AVFormatContext *s, AVStream *st,
165 MPADecodeHeader2 *c, uint32_t spf)
166 {
167 #define LAST_BITS(k, n) ((k) & ((1 << (n)) - 1))
168 #define MIDDLE_BITS(k, m, n) LAST_BITS((k) >> (m), ((n) - (m) + 1))
169
170 78 FFStream *const sti = ffstream(st);
171 uint16_t crc;
172 uint32_t v;
173
174 char version[10];
175
176 78 uint32_t peak = 0;
177 78 int32_t r_gain = INT32_MIN, a_gain = INT32_MIN;
178
179 78 MP3DecContext *mp3 = s->priv_data;
180 static const int64_t xing_offtbl[2][2] = {{32, 17}, {17,9}};
181 78 uint64_t fsize = avio_size(s->pb);
182 78 int64_t pos = avio_tell(s->pb);
183
1/2
✓ Branch 0 taken 78 times.
✗ Branch 1 not taken.
78 fsize = fsize >= pos ? fsize - pos : 0;
184
185 /* Check for Xing / Info tag */
186 78 avio_skip(s->pb, xing_offtbl[c->lsf == 1][c->nb_channels == 1]);
187 78 v = avio_rb32(s->pb);
188 78 mp3->is_cbr = v == MKBETAG('I', 'n', 'f', 'o');
189
4/4
✓ Branch 0 taken 76 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 42 times.
✓ Branch 3 taken 34 times.
78 if (v != MKBETAG('X', 'i', 'n', 'g') && !mp3->is_cbr)
190 42 return;
191
192 36 v = avio_rb32(s->pb);
193
1/2
✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
36 if (v & XING_FLAG_FRAMES)
194 36 mp3->frames = avio_rb32(s->pb);
195
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 1 times.
36 if (v & XING_FLAG_SIZE)
196 35 mp3->header_filesize = avio_rb32(s->pb);
197
3/4
✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 35 times.
✓ Branch 3 taken 1 times.
36 if (fsize && mp3->header_filesize) {
198 uint64_t min, delta;
199 35 min = FFMIN(fsize, mp3->header_filesize);
200 35 delta = FFMAX(fsize, mp3->header_filesize) - min;
201
4/4
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 25 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 9 times.
35 if (fsize > mp3->header_filesize && delta > min >> 4) {
202 1 mp3->frames = 0;
203 1 av_log(s, AV_LOG_WARNING,
204 "invalid concatenated file detected - using bitrate for duration\n");
205
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 33 times.
34 } else if (delta > min >> 4) {
206 1 av_log(s, AV_LOG_WARNING,
207 "filesize and duration do not match (growing file?)\n");
208 }
209 }
210
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 1 times.
36 if (v & XING_FLAG_TOC)
211 35 read_xing_toc(s, mp3->header_filesize, av_rescale_q(mp3->frames,
212 35 (AVRational){spf, c->sample_rate},
213 st->time_base));
214 /* VBR quality */
215
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 6 times.
36 if (v & XING_FLAC_QSCALE)
216 30 avio_rb32(s->pb);
217
218 /* Encoder short version string */
219 36 memset(version, 0, sizeof(version));
220 36 avio_read(s->pb, version, 9);
221
222 /* Info Tag revision + VBR method */
223 36 avio_r8(s->pb);
224
225 /* Lowpass filter value */
226 36 avio_r8(s->pb);
227
228 /* ReplayGain peak */
229 36 v = avio_rb32(s->pb);
230 36 peak = av_rescale(v, 100000, 1 << 23);
231
232 /* Radio ReplayGain */
233 36 v = avio_rb16(s->pb);
234
235
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 35 times.
36 if (MIDDLE_BITS(v, 13, 15) == 1) {
236 1 r_gain = MIDDLE_BITS(v, 0, 8) * 10000;
237
238
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (v & (1 << 9))
239 1 r_gain *= -1;
240 }
241
242 /* Audiophile ReplayGain */
243 36 v = avio_rb16(s->pb);
244
245
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36 times.
36 if (MIDDLE_BITS(v, 13, 15) == 2) {
246 a_gain = MIDDLE_BITS(v, 0, 8) * 10000;
247
248 if (v & (1 << 9))
249 a_gain *= -1;
250 }
251
252 /* Encoding flags + ATH Type */
253 36 avio_r8(s->pb);
254
255 /* if ABR {specified bitrate} else {minimal bitrate} */
256 36 avio_r8(s->pb);
257
258 /* Encoder delays */
259 36 v = avio_rb24(s->pb);
260
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 22 times.
36 if (AV_RB32(version) == MKBETAG('L', 'A', 'M', 'E')
261
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 12 times.
14 || AV_RB32(version) == MKBETAG('L', 'a', 'v', 'f')
262
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 || AV_RB32(version) == MKBETAG('L', 'a', 'v', 'c')
263 ) {
264
265 35 mp3->start_pad = v>>12;
266 35 mp3-> end_pad = v&4095;
267 35 sti->start_skip_samples = mp3->start_pad + 528 + 1;
268
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 1 times.
35 if (mp3->frames) {
269 34 sti->first_discard_sample = -mp3->end_pad + 528 + 1 + mp3->frames * (int64_t)spf;
270 34 sti->last_discard_sample = mp3->frames * (int64_t)spf;
271 }
272 35 av_log(s, AV_LOG_DEBUG, "pad %d %d\n", mp3->start_pad, mp3-> end_pad);
273 }
274
275 /* Misc */
276 36 avio_r8(s->pb);
277
278 /* MP3 gain */
279 36 avio_r8(s->pb);
280
281 /* Preset and surround info */
282 36 avio_rb16(s->pb);
283
284 /* Music length */
285 36 avio_rb32(s->pb);
286
287 /* Music CRC */
288 36 avio_rb16(s->pb);
289
290 /* Info Tag CRC */
291 36 crc = ffio_get_checksum(s->pb);
292 36 v = avio_rb16(s->pb);
293
294
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 16 times.
36 if (v == crc) {
295 20 ff_replaygain_export_raw(st, r_gain, peak, a_gain, 0);
296 20 av_dict_set(&st->metadata, "encoder", version, 0);
297 }
298 }
299
300 78 static void mp3_parse_vbri_tag(AVFormatContext *s, AVStream *st, int64_t base)
301 {
302 uint32_t v;
303 78 MP3DecContext *mp3 = s->priv_data;
304
305 /* Check for VBRI tag (always 32 bytes after end of mpegaudio header) */
306 78 avio_seek(s->pb, base + 4 + 32, SEEK_SET);
307 78 v = avio_rb32(s->pb);
308
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 78 times.
78 if (v == MKBETAG('V', 'B', 'R', 'I')) {
309 /* Check tag version */
310 if (avio_rb16(s->pb) == 1) {
311 /* skip delay and quality */
312 avio_skip(s->pb, 4);
313 mp3->header_filesize = avio_rb32(s->pb);
314 mp3->frames = avio_rb32(s->pb);
315 }
316 }
317 78 }
318
319 /**
320 * Look the iTunSMPB tag up under every key it can land on. A COMM frame keeps
321 * its language in the key, and while Apple writes eng, anything that retags
322 * the file is free to write its own, valid or not.
323 *
324 * The bare key is for files remuxed by versions that exposed a COMM descriptor
325 * as a metadata key of its own: those wrote the tag back out as a TXXX frame,
326 * which carries no language and is keyed by its description alone.
327 */
328 43 static const AVDictionaryEntry *find_itunes_smpb(const AVDictionary *metadata)
329 {
330 static const char comment[] = "comment-iTunSMPB";
331 43 const AVDictionaryEntry *de = NULL;
332
333
2/2
✓ Branch 1 taken 14 times.
✓ Branch 2 taken 29 times.
43 while ((de = av_dict_get(metadata, comment, de, AV_DICT_IGNORE_SUFFIX))) {
334 14 const char *lang = de->key + sizeof(comment) - 1;
335
336 /* und and xxx come without a suffix, the rest as a three letter one. */
337
3/6
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 14 times.
✗ Branch 5 not taken.
14 if (!*lang || (*lang == '-' && strlen(lang + 1) == 3))
338 14 return de;
339 }
340
341 29 return av_dict_get(metadata, "iTunSMPB", NULL, 0);
342 }
343
344 /**
345 * Parse the gapless playback information Apple encoders store in an iTunSMPB
346 * comment instead of in the LAME extension of the Xing tag.
347 */
348 78 static void mp3_parse_itunes_smpb(AVFormatContext *s, AVStream *st,
349 const MPADecodeHeader2 *c, uint32_t spf)
350 {
351 78 FFStream *const sti = ffstream(st);
352 78 MP3DecContext *mp3 = s->priv_data;
353 const AVDictionaryEntry *de;
354 int64_t priming, remainder, samples;
355
356 /* A LAME tag, if any, already described the padding. */
357
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 43 times.
78 if (sti->start_skip_samples)
358 64 return;
359
360
2/2
✓ Branch 1 taken 29 times.
✓ Branch 2 taken 14 times.
43 if (!(de = find_itunes_smpb(s->metadata)))
361 29 return;
362
363
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
14 if (ff_itunes_parse_smpb(de->value, &priming, &remainder, &samples) < 0)
364 return;
365
366
2/4
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 14 times.
14 if (priming > 16384 || !samples)
367 return;
368
369 /* The three counts must add up to what the frames actually decode to. */
370
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (mp3->frames &&
371 priming + samples + remainder != mp3->frames * (int64_t)spf)
372 return;
373
374 /* Contrary to the LAME tag, the priming count covers the decoder delay. */
375 14 sti->start_skip_samples = priming;
376 14 sti->first_discard_sample = priming + samples;
377 14 sti->last_discard_sample = priming + samples + remainder;
378
379 14 st->duration = av_rescale_q(samples, (AVRational){ 1, c->sample_rate },
380 st->time_base);
381 }
382
383 /**
384 * Try to find Xing/Info/VBRI tags and compute duration from info therein
385 */
386 83 static int mp3_parse_vbr_tags(AVFormatContext *s, AVStream *st, int64_t base)
387 {
388 uint32_t v, spf;
389 83 MPADecodeHeader2 *c = NULL;
390 83 int vbrtag_size = 0;
391 83 MP3DecContext *mp3 = s->priv_data;
392 int ret;
393
394 83 ffio_init_checksum(s->pb, ff_crcA001_update, 0);
395
396 83 v = avio_rb32(s->pb);
397
398 83 ret = avpriv_mpegaudio_decode_header2(&c, v);
399
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 81 times.
83 if (ret < 0)
400 2 goto end;
401
1/2
✓ Branch 0 taken 81 times.
✗ Branch 1 not taken.
81 else if (ret == 0)
402 81 vbrtag_size = c->frame_size;
403
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 78 times.
81 if (c->layer != 3) {
404 3 ret = -1;
405 3 goto end;
406 }
407
408
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 77 times.
78 spf = c->lsf ? 576 : 1152; /* Samples per frame, layer 3 */
409
410 78 mp3->frames = 0;
411 78 mp3->header_filesize = 0;
412 78 mp3->frame_duration = av_rescale_q(spf, (AVRational){1, c->sample_rate}, st->time_base);
413
414 78 mp3_parse_info_tag(s, st, c, spf);
415 78 mp3_parse_vbri_tag(s, st, base);
416 78 mp3_parse_itunes_smpb(s, st, c, spf);
417
418 /* Packets keep the skipped samples, so shift the timeline instead. */
419
2/2
✓ Branch 1 taken 49 times.
✓ Branch 2 taken 29 times.
78 if (ffstream(st)->start_skip_samples)
420 49 st->start_time = av_rescale_q(ffstream(st)->start_skip_samples,
421 49 (AVRational){1, c->sample_rate},
422 st->time_base);
423
424
4/4
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 35 times.
✓ Branch 2 taken 42 times.
✓ Branch 3 taken 1 times.
78 if (!mp3->frames && !mp3->header_filesize) {
425 42 ret = -1;
426 42 goto end;
427 }
428
429 /* Skip the vbr tag frame */
430 36 avio_seek(s->pb, base + vbrtag_size, SEEK_SET);
431
432
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 1 times.
36 if (mp3->frames) {
433 int64_t full_duration_samples;
434
435 35 full_duration_samples = mp3->frames * (int64_t)spf;
436
1/2
✓ Branch 0 taken 35 times.
✗ Branch 1 not taken.
35 if (st->duration == AV_NOPTS_VALUE)
437 35 st->duration = av_rescale_q(full_duration_samples - mp3->start_pad - mp3->end_pad,
438 35 (AVRational){1, c->sample_rate},
439 st->time_base);
440
441
4/4
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 33 times.
35 if (mp3->header_filesize && !mp3->is_cbr)
442 1 st->codecpar->bit_rate = av_rescale(mp3->header_filesize, 8 * c->sample_rate,
443 full_duration_samples);
444 }
445
446 36 ret = 0;
447
448 83 end:
449 83 av_freep(&c);
450 83 return ret;
451 }
452
453 83 static int mp3_read_header(AVFormatContext *s)
454 {
455 83 FFFormatContext *const si = ffformatcontext(s);
456 83 MP3DecContext *mp3 = s->priv_data;
457 AVStream *st;
458 FFStream *sti;
459 int64_t off;
460 int ret;
461 int i;
462
463 83 s->metadata = si->id3v2_meta;
464 83 si->id3v2_meta = NULL;
465
466 83 st = avformat_new_stream(s, NULL);
467
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 83 times.
83 if (!st)
468 return AVERROR(ENOMEM);
469 83 sti = ffstream(st);
470
471 83 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
472 83 st->codecpar->codec_id = AV_CODEC_ID_MP3;
473 83 sti->need_parsing = AVSTREAM_PARSE_FULL_RAW;
474 83 st->start_time = 0;
475
476 // lcm of all mp3 sample rates
477 83 avpriv_set_pts_info(st, 64, 1, 14112000);
478
479 83 ffiocontext(s->pb)->maxsize = -1;
480 83 off = avio_tell(s->pb);
481
482
2/2
✓ Branch 1 taken 17 times.
✓ Branch 2 taken 66 times.
83 if (!av_dict_count(s->metadata))
483 17 ff_id3v1_read(s);
484
485
1/2
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
83 if (s->pb->seekable & AVIO_SEEKABLE_NORMAL)
486 83 mp3->filesize = avio_size(s->pb);
487
488
2/2
✓ Branch 1 taken 47 times.
✓ Branch 2 taken 36 times.
83 if (mp3_parse_vbr_tags(s, st, off) < 0)
489 47 avio_seek(s->pb, off, SEEK_SET);
490
491 83 ret = ff_replaygain_export(st, s->metadata);
492
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 83 times.
83 if (ret < 0)
493 return ret;
494
495 83 ret = ffio_ensure_seekback(s->pb, 64 * 1024 + MPA_MAX_CODED_FRAME_SIZE + 4);
496
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 83 times.
83 if (ret < 0)
497 return ret;
498
499 83 off = avio_tell(s->pb);
500
1/2
✓ Branch 0 taken 709 times.
✗ Branch 1 not taken.
709 for (i = 0; i < 64 * 1024; i++) {
501 uint32_t header, header2;
502 int frame_size;
503 709 frame_size = check(s->pb, off + i, &header);
504
2/2
✓ Branch 0 taken 82 times.
✓ Branch 1 taken 627 times.
709 if (frame_size > 0) {
505 82 ret = check(s->pb, off + i + frame_size, &header2);
506
2/4
✓ Branch 0 taken 82 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 82 times.
✗ Branch 3 not taken.
82 if (ret >= 0 && (header & MP3_MASK) == (header2 & MP3_MASK))
507 82 break;
508
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 626 times.
627 } else if (frame_size == CHECK_SEEK_FAILED) {
509 1 av_log(s, AV_LOG_ERROR, "Failed to find two consecutive MPEG audio frames.\n");
510 1 return AVERROR_INVALIDDATA;
511 }
512 }
513
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 82 times.
82 if (i == 64 * 1024) {
514 off = avio_seek(s->pb, off, SEEK_SET);
515 } else {
516
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 81 times.
82 av_log(s, i > 0 ? AV_LOG_INFO : AV_LOG_VERBOSE, "Skipping %d bytes of junk at %"PRId64".\n", i, off);
517 82 off = avio_seek(s->pb, off + i, SEEK_SET);
518 }
519
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 82 times.
82 if (off < 0)
520 return off;
521
522 // the seek index is relative to the end of the xing vbr headers
523
2/2
✓ Branch 0 taken 100 times.
✓ Branch 1 taken 82 times.
182 for (int i = 0; i < sti->nb_index_entries; i++)
524 100 sti->index_entries[i].pos += off;
525
526 /* the parameters will be extracted from the compressed bitstream */
527 82 return 0;
528 }
529
530 #define MP3_PACKET_SIZE 1024
531
532 5709 static int mp3_read_packet(AVFormatContext *s, AVPacket *pkt)
533 {
534 5709 MP3DecContext *mp3 = s->priv_data;
535 int ret, size;
536 int64_t pos;
537
538 5709 size = MP3_PACKET_SIZE;
539 5709 pos = avio_tell(s->pb);
540
3/4
✓ Branch 0 taken 5709 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5606 times.
✓ Branch 3 taken 103 times.
5709 if (mp3->filesize > ID3v1_TAG_SIZE && pos < mp3->filesize)
541 5606 size= FFMIN(size, mp3->filesize - pos);
542
543 5709 ret = av_get_packet(s->pb, pkt, size);
544
2/2
✓ Branch 0 taken 103 times.
✓ Branch 1 taken 5606 times.
5709 if (ret <= 0) {
545
1/2
✓ Branch 0 taken 103 times.
✗ Branch 1 not taken.
103 if(ret<0)
546 103 return ret;
547 return AVERROR_EOF;
548 }
549
550 5606 pkt->flags &= ~AV_PKT_FLAG_CORRUPT;
551 5606 pkt->stream_index = 0;
552
553 5606 return ret;
554 }
555
556 #define SEEK_WINDOW 4096
557
558 17827 static int check(AVIOContext *pb, int64_t pos, uint32_t *ret_header)
559 {
560 17827 int64_t ret = avio_seek(pb, pos, SEEK_SET);
561 uint8_t header_buf[4];
562 unsigned header;
563 17827 MPADecodeHeader2 *sd = NULL;
564 int frame_size;
565
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17827 times.
17827 if (ret < 0)
566 return CHECK_SEEK_FAILED;
567
568 17827 ret = avio_read(pb, &header_buf[0], 4);
569 /* We should always find four bytes for a valid mpa header. */
570
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 17826 times.
17827 if (ret < 4)
571 1 return CHECK_SEEK_FAILED;
572
573 17826 header = AV_RB32(&header_buf[0]);
574
2/2
✓ Branch 1 taken 17490 times.
✓ Branch 2 taken 336 times.
17826 if (ff_mpa_check_header(header) < 0)
575 17490 return CHECK_WRONG_HEADER;
576
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 336 times.
336 if (avpriv_mpegaudio_decode_header2(&sd, header)) {
577 av_freep(&sd);
578 return CHECK_WRONG_HEADER;
579 }
580 336 frame_size = sd->frame_size;
581 336 av_freep(&sd);
582
583
2/2
✓ Branch 0 taken 164 times.
✓ Branch 1 taken 172 times.
336 if (ret_header)
584 164 *ret_header = header;
585 336 return frame_size;
586 }
587
588 26 static int64_t mp3_sync(AVFormatContext *s, int64_t target_pos, int flags)
589 {
590
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 13 times.
26 int dir = (flags&AVSEEK_FLAG_BACKWARD) ? -1 : 1;
591 int64_t best_pos;
592 int best_score, i, j;
593 int64_t ret;
594
595 26 avio_seek(s->pb, FFMAX(target_pos - SEEK_WINDOW, 0), SEEK_SET);
596 26 ret = avio_seek(s->pb, target_pos, SEEK_SET);
597
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
26 if (ret < 0)
598 return ret;
599
600 #define MIN_VALID 3
601 26 best_pos = target_pos;
602 26 best_score = 999;
603
1/2
✓ Branch 0 taken 16916 times.
✗ Branch 1 not taken.
16916 for (i = 0; i < SEEK_WINDOW; i++) {
604
2/2
✓ Branch 0 taken 9621 times.
✓ Branch 1 taken 7295 times.
16916 int64_t pos = target_pos + (dir > 0 ? i - SEEK_WINDOW/4 : -i);
605 16916 int64_t candidate = -1;
606 16916 int score = 999;
607
608
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16916 times.
16916 if (pos < 0)
609 continue;
610
611
2/2
✓ Branch 0 taken 17036 times.
✓ Branch 1 taken 52 times.
17088 for (j = 0; j < MIN_VALID; j++) {
612 17036 ret = check(s->pb, pos, NULL);
613
2/2
✓ Branch 0 taken 16864 times.
✓ Branch 1 taken 172 times.
17036 if (ret < 0) {
614
1/2
✓ Branch 0 taken 16864 times.
✗ Branch 1 not taken.
16864 if (ret == CHECK_WRONG_HEADER) {
615 16864 break;
616 } else if (ret == CHECK_SEEK_FAILED) {
617 av_log(s, AV_LOG_ERROR, "Could not seek to %"PRId64".\n", pos);
618 return AVERROR(EINVAL);
619 }
620 }
621
6/6
✓ Branch 0 taken 81 times.
✓ Branch 1 taken 91 times.
✓ Branch 2 taken 59 times.
✓ Branch 3 taken 22 times.
✓ Branch 4 taken 68 times.
✓ Branch 5 taken 13 times.
172 if ((target_pos - pos)*dir <= 0 && FFABS(MIN_VALID/2-j) < score) {
622 68 candidate = pos;
623
2/2
✓ Branch 0 taken 59 times.
✓ Branch 1 taken 9 times.
68 score = FFABS(MIN_VALID/2-j);
624 }
625 172 pos += ret;
626 }
627
4/4
✓ Branch 0 taken 49 times.
✓ Branch 1 taken 16867 times.
✓ Branch 2 taken 48 times.
✓ Branch 3 taken 1 times.
16916 if (best_score > score && j == MIN_VALID) {
628 48 best_pos = candidate;
629 48 best_score = score;
630
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 22 times.
48 if(score == 0)
631 26 break;
632 }
633 }
634
635 26 return avio_seek(s->pb, best_pos, SEEK_SET);
636 }
637
638 134 static int mp3_seek(AVFormatContext *s, int stream_index, int64_t timestamp,
639 int flags)
640 {
641 134 FFFormatContext *const si = ffformatcontext(s);
642 134 MP3DecContext *mp3 = s->priv_data;
643 AVIndexEntry *ie, ie1;
644 134 AVStream *st = s->streams[0];
645 134 FFStream *const sti = ffstream(st);
646 int64_t best_pos;
647 134 int fast_seek = s->flags & AVFMT_FLAG_FAST_SEEK;
648 134 int64_t filesize = mp3->header_filesize;
649
650
2/2
✓ Branch 0 taken 54 times.
✓ Branch 1 taken 80 times.
134 if (filesize <= 0) {
651 54 int64_t size = avio_size(s->pb);
652
2/4
✓ Branch 0 taken 54 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 54 times.
✗ Branch 3 not taken.
54 if (size > 0 && size > si->data_offset)
653 54 filesize = size - si->data_offset;
654 }
655
656
5/8
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 108 times.
✓ Branch 2 taken 26 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 26 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 26 times.
134 if (mp3->xing_toc && (mp3->usetoc || (fast_seek && !mp3->is_cbr))) {
657 int64_t ret = av_index_search_timestamp(st, timestamp, flags);
658
659 // NOTE: The MP3 TOC is not a precise lookup table. Accuracy is worse
660 // for bigger files.
661 av_log(s, AV_LOG_WARNING, "Using MP3 TOC to seek; may be imprecise.\n");
662
663 if (ret < 0)
664 return ret;
665
666 ie = &sti->index_entries[ret];
667
4/6
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 108 times.
✓ Branch 2 taken 26 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 26 times.
✗ Branch 5 not taken.
134 } else if (fast_seek && st->duration > 0 && filesize > 0) {
668
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
26 if (!mp3->is_cbr)
669 av_log(s, AV_LOG_WARNING, "Using scaling to seek VBR MP3; may be imprecise.\n");
670
671 26 ie = &ie1;
672 26 timestamp = av_clip64(timestamp, 0, st->duration);
673 26 ie->timestamp = timestamp;
674 26 ie->pos = av_rescale(timestamp, filesize, st->duration) + si->data_offset;
675 } else {
676 108 return -1; // generic index code
677 }
678
679 26 best_pos = mp3_sync(s, ie->pos, flags);
680
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
26 if (best_pos < 0)
681 return best_pos;
682
683
4/8
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 26 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 26 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 26 times.
✗ Branch 7 not taken.
26 if (mp3->is_cbr && ie == &ie1 && mp3->frames && mp3->header_filesize > 0) {
684 26 ie1.timestamp = mp3->frame_duration * av_rescale(best_pos - si->data_offset, mp3->frames, mp3->header_filesize);
685 }
686
687 26 avpriv_update_cur_dts(s, st, ie->timestamp);
688 26 return 0;
689 }
690
691 static const AVOption options[] = {
692 { "usetoc", "use table of contents", offsetof(MP3DecContext, usetoc), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, AV_OPT_FLAG_DECODING_PARAM},
693 { NULL },
694 };
695
696 static const AVClass demuxer_class = {
697 .class_name = "mp3",
698 .item_name = av_default_item_name,
699 .option = options,
700 .version = LIBAVUTIL_VERSION_INT,
701 .category = AV_CLASS_CATEGORY_DEMUXER,
702 };
703
704 const FFInputFormat ff_mp3_demuxer = {
705 .p.name = "mp3",
706 .p.long_name = NULL_IF_CONFIG_SMALL("MP2/3 (MPEG audio layer 2/3)"),
707 .p.flags = AVFMT_GENERIC_INDEX,
708 .p.extensions = "mp2,mp3,m2a,mpa", /* XXX: use probe */
709 .p.priv_class = &demuxer_class,
710 .p.mime_type = "audio/mpeg",
711 .flags_internal = FF_INFMT_FLAG_ID3V2_AUTO,
712 .read_probe = mp3_read_probe,
713 .read_header = mp3_read_header,
714 .read_packet = mp3_read_packet,
715 .read_seek = mp3_seek,
716 .priv_data_size = sizeof(MP3DecContext),
717 };
718