../../../ffmpeg/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
312 / 349
89.4%
Branches:
198 / 264
75.0%
Functions:
12 / 12
100.0%

libavformat/mp3dec.c
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 8456 static int mp3_read_probe(const AVProbeData *p)
72 {
73 8456 int max_frames, first_frames = 0;
74 8456 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 8456 MPADecodeHeader2 *h = NULL;
80
81 8456 buf0 = p->buf;
82 8456 end = p->buf + p->buf_size - sizeof(uint32_t);
83
3/4
✓ Branch 4 → 5 taken 120468 times.
✗ Branch 4 → 6 not taken.
✓ Branch 5 → 3 taken 112012 times.
✓ Branch 5 → 6 taken 8456 times.
120468 while (buf0 < end && !*buf0)
84 112012 buf0++;
85
86 8456 max_frames = 0;
87 8456 max_framesizes = 0;
88 8456 buf = buf0;
89
90
2/2
✓ Branch 24 → 7 taken 488508490 times.
✓ Branch 24 → 25 taken 8456 times.
488516946 for (; buf < end; buf = buf2+1) {
91 488508490 buf2 = buf;
92
2/2
✓ Branch 19 → 8 taken 488583893 times.
✓ Branch 19 → 20 taken 2 times.
488583895 for (framesizes = frames = 0; buf2 < end; frames++) {
93 488583893 int header_emu = 0;
94 int available;
95
96 488583893 header = AV_RB32(buf2);
97 488583893 ret = avpriv_mpegaudio_decode_header2(&h, header);
98
2/2
✓ Branch 9 → 10 taken 488506095 times.
✓ Branch 9 → 11 taken 77798 times.
488583893 if (ret != 0)
99 488506095 break;
100
101 77798 available = FFMIN(h->frame_size, end - buf2);
102
2/2
✓ Branch 13 → 12 taken 30615816 times.
✓ Branch 13 → 14 taken 77798 times.
30693614 for (buf3 = buf2 + 4; buf3 < buf2 + available; buf3++) {
103 30615816 uint32_t next_sync = AV_RB32(buf3);
104 30615816 header_emu += (next_sync & MP3_MASK) == (header & MP3_MASK);
105 }
106
2/2
✓ Branch 14 → 15 taken 1185 times.
✓ Branch 14 → 16 taken 76613 times.
77798 if (header_emu > 2)
107 1185 break;
108 76613 framesizes += h->frame_size;
109
2/2
✓ Branch 16 → 17 taken 1208 times.
✓ Branch 16 → 18 taken 75405 times.
76613 if (available < h->frame_size) {
110 1208 frames++;
111 1208 break;
112 }
113 75405 buf2 += h->frame_size;
114 }
115 488508490 max_frames = FFMAX(max_frames, frames);
116 488508490 max_framesizes = FFMAX(max_framesizes, framesizes);
117
2/2
✓ Branch 20 → 21 taken 8456 times.
✓ Branch 20 → 23 taken 488500034 times.
488508490 if (buf == buf0) {
118 8456 first_frames= frames;
119
1/2
✗ Branch 21 → 22 not taken.
✓ Branch 21 → 23 taken 8456 times.
8456 if (buf2 == end + sizeof(uint32_t))
120 ✗ whole_used = 1;
121 }
122 }
123 8456 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 26 → 27 taken 75 times.
✓ Branch 26 → 28 taken 8381 times.
8456 if (first_frames>=7) return AVPROBE_SCORE_EXTENSION + 2;
128
3/4
✓ Branch 28 → 29 taken 1 time.
✓ Branch 28 → 31 taken 8380 times.
✓ Branch 29 → 30 taken 1 time.
✗ Branch 29 → 31 not taken.
8381 else if (max_frames>200 && p->buf_size < 2*max_framesizes)return AVPROBE_SCORE_EXTENSION;
129
4/4
✓ Branch 31 → 32 taken 42 times.
✓ Branch 31 → 34 taken 8338 times.
✓ Branch 32 → 33 taken 39 times.
✓ Branch 32 → 34 taken 3 times.
8380 else if (max_frames>=4 && p->buf_size < 2*max_framesizes) return AVPROBE_SCORE_EXTENSION / 2;
130
3/4
✓ Branch 35 → 36 taken 50 times.
✓ Branch 35 → 42 taken 8291 times.
✓ Branch 37 → 38 taken 50 times.
✗ Branch 37 → 42 not taken.
8341 else if (ff_id3v2_match(buf0, ID3v2_DEFAULT_MAGIC) && 2*ff_id3v2_tag_len(buf0) >= p->buf_size)
131
1/2
✓ Branch 38 → 39 taken 50 times.
✗ Branch 38 → 40 not taken.
50 return p->buf_size < PROBE_BUF_MAX ? AVPROBE_SCORE_EXTENSION / 4 : AVPROBE_SCORE_EXTENSION - 2;
132
3/4
✓ Branch 42 → 43 taken 31 times.
✓ Branch 42 → 45 taken 8260 times.
✗ Branch 43 → 44 not taken.
✓ Branch 43 → 45 taken 31 times.
8291 else if (first_frames > 1 && whole_used) return 5;
133
4/4
✓ Branch 45 → 46 taken 4758 times.
✓ Branch 45 → 48 taken 3533 times.
✓ Branch 46 → 47 taken 1466 times.
✓ Branch 46 → 48 taken 3292 times.
8291 else if (max_frames>=1 && p->buf_size < 10*max_framesizes) return 1;
134 6825 else return 0;
135 //mpegps_mp3_unrecognized_format.mpg has max_frames=3
136 }
137
138 39 static void read_xing_toc(AVFormatContext *s, int64_t filesize, int64_t duration)
139 {
140 int i;
141 39 MP3DecContext *mp3 = s->priv_data;
142 39 int fast_seek = s->flags & AVFMT_FLAG_FAST_SEEK;
143
4/6
✓ Branch 2 → 3 taken 39 times.
✗ Branch 2 → 4 not taken.
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 6 taken 38 times.
✓ Branch 4 → 5 taken 1 time.
✗ Branch 4 → 6 not taken.
39 int fill_index = (mp3->usetoc || fast_seek) && duration > 0;
144
145
1/4
✗ Branch 7 → 8 not taken.
✓ Branch 7 → 12 taken 39 times.
✗ Branch 9 → 10 not taken.
✗ Branch 9 → 12 not taken.
39 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 17 → 13 taken 3900 times.
✓ Branch 17 → 18 taken 39 times.
3939 for (i = 0; i < XING_TOC_COUNT; i++) {
153 3900 uint8_t b = avio_r8(s->pb);
154
2/2
✓ Branch 14 → 15 taken 100 times.
✓ Branch 14 → 16 taken 3800 times.
3900 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 18 → 19 taken 1 time.
✓ Branch 18 → 20 taken 38 times.
39 if (fill_index)
161 1 mp3->xing_toc = 1;
162 39 }
163
164 86 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 86 FFStream *const sti = ffstream(st);
171 uint16_t crc;
172 uint32_t v;
173
174 char version[10];
175
176 86 uint32_t peak = 0;
177 86 int32_t r_gain = INT32_MIN, a_gain = INT32_MIN;
178
179 86 MP3DecContext *mp3 = s->priv_data;
180 static const int64_t xing_offtbl[2][2] = {{32, 17}, {17,9}};
181 86 uint64_t fsize = avio_size(s->pb);
182 86 int64_t pos = avio_tell(s->pb);
183
1/2
✓ Branch 5 → 6 taken 86 times.
✗ Branch 5 → 7 not taken.
86 fsize = fsize >= pos ? fsize - pos : 0;
184
185 /* Check for Xing / Info tag */
186 86 avio_skip(s->pb, xing_offtbl[c->lsf == 1][c->nb_channels == 1]);
187 86 v = avio_rb32(s->pb);
188 86 mp3->is_cbr = v == MKBETAG('I', 'n', 'f', 'o');
189
4/4
✓ Branch 10 → 11 taken 84 times.
✓ Branch 10 → 13 taken 2 times.
✓ Branch 11 → 12 taken 46 times.
✓ Branch 11 → 13 taken 38 times.
86 if (v != MKBETAG('X', 'i', 'n', 'g') && !mp3->is_cbr)
190 46 return;
191
192 40 v = avio_rb32(s->pb);
193
1/2
✓ Branch 14 → 15 taken 40 times.
✗ Branch 14 → 17 not taken.
40 if (v & XING_FLAG_FRAMES)
194 40 mp3->frames = avio_rb32(s->pb);
195
2/2
✓ Branch 17 → 18 taken 39 times.
✓ Branch 17 → 20 taken 1 time.
40 if (v & XING_FLAG_SIZE)
196 39 mp3->header_filesize = avio_rb32(s->pb);
197
3/4
✓ Branch 20 → 21 taken 40 times.
✗ Branch 20 → 27 not taken.
✓ Branch 21 → 22 taken 39 times.
✓ Branch 21 → 27 taken 1 time.
40 if (fsize && mp3->header_filesize) {
198 uint64_t min, delta;
199 39 min = FFMIN(fsize, mp3->header_filesize);
200 39 delta = FFMAX(fsize, mp3->header_filesize) - min;
201
4/4
✓ Branch 22 → 23 taken 14 times.
✓ Branch 22 → 25 taken 25 times.
✓ Branch 23 → 24 taken 1 time.
✓ Branch 23 → 25 taken 13 times.
39 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 25 → 26 taken 1 time.
✓ Branch 25 → 27 taken 37 times.
38 } 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 27 → 28 taken 39 times.
✓ Branch 27 → 30 taken 1 time.
40 if (v & XING_FLAG_TOC)
211 39 read_xing_toc(s, mp3->header_filesize, av_rescale_q(mp3->frames,
212 39 (AVRational){spf, c->sample_rate},
213 st->time_base));
214 /* VBR quality */
215
2/2
✓ Branch 30 → 31 taken 34 times.
✓ Branch 30 → 32 taken 6 times.
40 if (v & XING_FLAC_QSCALE)
216 34 avio_rb32(s->pb);
217
218 /* Encoder short version string */
219 40 memset(version, 0, sizeof(version));
220 40 avio_read(s->pb, version, 9);
221
222 /* Info Tag revision + VBR method */
223 40 avio_r8(s->pb);
224
225 /* Lowpass filter value */
226 40 avio_r8(s->pb);
227
228 /* ReplayGain peak */
229 40 v = avio_rb32(s->pb);
230 40 peak = av_rescale(v, 100000, 1 << 23);
231
232 /* Radio ReplayGain */
233 40 v = avio_rb16(s->pb);
234
235
2/2
✓ Branch 37 → 38 taken 1 time.
✓ Branch 37 → 40 taken 39 times.
40 if (MIDDLE_BITS(v, 13, 15) == 1) {
236 1 r_gain = MIDDLE_BITS(v, 0, 8) * 10000;
237
238
1/2
✓ Branch 38 → 39 taken 1 time.
✗ Branch 38 → 40 not taken.
1 if (v & (1 << 9))
239 1 r_gain *= -1;
240 }
241
242 /* Audiophile ReplayGain */
243 40 v = avio_rb16(s->pb);
244
245
1/2
✗ Branch 41 → 42 not taken.
✓ Branch 41 → 44 taken 40 times.
40 if (MIDDLE_BITS(v, 13, 15) == 2) {
246 ✗ a_gain = MIDDLE_BITS(v, 0, 8) * 10000;
247
248
0/2
✗ Branch 42 → 43 not taken.
✗ Branch 42 → 44 not taken.
✗ if (v & (1 << 9))
249 ✗ a_gain *= -1;
250 }
251
252 /* Encoding flags + ATH Type */
253 40 avio_r8(s->pb);
254
255 /* if ABR {specified bitrate} else {minimal bitrate} */
256 40 avio_r8(s->pb);
257
258 /* Encoder delays */
259 40 v = avio_rb24(s->pb);
260
2/2
✓ Branch 47 → 48 taken 14 times.
✓ Branch 47 → 50 taken 26 times.
40 if (AV_RB32(version) == MKBETAG('L', 'A', 'M', 'E')
261
2/2
✓ Branch 48 → 49 taken 2 times.
✓ Branch 48 → 50 taken 12 times.
14 || AV_RB32(version) == MKBETAG('L', 'a', 'v', 'f')
262
2/2
✓ Branch 49 → 50 taken 1 time.
✓ Branch 49 → 53 taken 1 time.
2 || AV_RB32(version) == MKBETAG('L', 'a', 'v', 'c')
263 ) {
264
265 39 mp3->start_pad = v>>12;
266 39 mp3-> end_pad = v&4095;
267 39 sti->start_skip_samples = mp3->start_pad + 528 + 1;
268
2/2
✓ Branch 50 → 51 taken 38 times.
✓ Branch 50 → 52 taken 1 time.
39 if (mp3->frames) {
269 38 sti->first_discard_sample = -mp3->end_pad + 528 + 1 + mp3->frames * (int64_t)spf;
270 38 sti->last_discard_sample = mp3->frames * (int64_t)spf;
271 }
272 39 av_log(s, AV_LOG_DEBUG, "pad %d %d\n", mp3->start_pad, mp3-> end_pad);
273 }
274
275 /* Misc */
276 40 avio_r8(s->pb);
277
278 /* MP3 gain */
279 40 avio_r8(s->pb);
280
281 /* Preset and surround info */
282 40 avio_rb16(s->pb);
283
284 /* Music length */
285 40 avio_rb32(s->pb);
286
287 /* Music CRC */
288 40 avio_rb16(s->pb);
289
290 /* Info Tag CRC */
291 40 crc = ffio_get_checksum(s->pb);
292 40 v = avio_rb16(s->pb);
293
294
2/2
✓ Branch 60 → 61 taken 24 times.
✓ Branch 60 → 63 taken 16 times.
40 if (v == crc) {
295 24 ff_replaygain_export_raw(st, r_gain, peak, a_gain, 0);
296 24 av_dict_set(&st->metadata, "encoder", version, 0);
297 }
298 }
299
300 86 static void mp3_parse_vbri_tag(AVFormatContext *s, AVStream *st, int64_t base)
301 {
302 uint32_t v;
303 86 MP3DecContext *mp3 = s->priv_data;
304
305 /* Check for VBRI tag (always 32 bytes after end of mpegaudio header) */
306 86 avio_seek(s->pb, base + 4 + 32, SEEK_SET);
307 86 v = avio_rb32(s->pb);
308
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 11 taken 86 times.
86 if (v == MKBETAG('V', 'B', 'R', 'I')) {
309 /* Check tag version */
310
0/2
✗ Branch 6 → 7 not taken.
✗ Branch 6 → 11 not taken.
✗ 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 86 }
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 47 static const AVDictionaryEntry *find_itunes_smpb(const AVDictionary *metadata)
329 {
330 static const char comment[] = "comment-iTunSMPB";
331 47 const AVDictionaryEntry *de = NULL;
332
333
2/2
✓ Branch 9 → 3 taken 18 times.
✓ Branch 9 → 10 taken 29 times.
47 while ((de = av_dict_get(metadata, comment, de, AV_DICT_IGNORE_SUFFIX))) {
334 18 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 3 → 4 taken 18 times.
✗ Branch 3 → 6 not taken.
✓ Branch 4 → 5 taken 18 times.
✗ Branch 4 → 7 not taken.
✓ Branch 5 → 6 taken 18 times.
✗ Branch 5 → 7 not taken.
18 if (!*lang || (*lang == '-' && strlen(lang + 1) == 3))
338 18 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 86 static void mp3_parse_itunes_smpb(AVFormatContext *s, AVStream *st,
349 const MPADecodeHeader2 *c, uint32_t spf)
350 {
351 86 FFStream *const sti = ffstream(st);
352 86 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 3 → 4 taken 39 times.
✓ Branch 3 → 5 taken 47 times.
86 if (sti->start_skip_samples)
358 68 return;
359
360
2/2
✓ Branch 6 → 7 taken 29 times.
✓ Branch 6 → 8 taken 18 times.
47 if (!(de = find_itunes_smpb(s->metadata)))
361 29 return;
362
363
1/2
✗ Branch 9 → 10 not taken.
✓ Branch 9 → 11 taken 18 times.
18 if (ff_itunes_parse_smpb(de->value, &priming, &remainder, &samples) < 0)
364 ✗ return;
365
366
2/4
✓ Branch 11 → 12 taken 18 times.
✗ Branch 11 → 13 not taken.
✗ Branch 12 → 13 not taken.
✓ Branch 12 → 14 taken 18 times.
18 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 14 → 15 not taken.
✓ Branch 14 → 17 taken 18 times.
18 if (mp3->frames &&
371
0/2
✗ Branch 15 → 16 not taken.
✗ Branch 15 → 17 not taken.
✗ 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 18 sti->start_skip_samples = priming;
376 18 sti->first_discard_sample = priming + samples;
377 18 sti->last_discard_sample = priming + samples + remainder;
378
379 18 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 91 static int mp3_parse_vbr_tags(AVFormatContext *s, AVStream *st, int64_t base)
387 {
388 uint32_t v, spf;
389 91 MPADecodeHeader2 *c = NULL;
390 91 int vbrtag_size = 0;
391 91 MP3DecContext *mp3 = s->priv_data;
392 int ret;
393
394 91 ffio_init_checksum(s->pb, ff_crcA001_update, 0);
395
396 91 v = avio_rb32(s->pb);
397
398 91 ret = avpriv_mpegaudio_decode_header2(&c, v);
399
2/2
✓ Branch 5 → 6 taken 2 times.
✓ Branch 5 → 7 taken 89 times.
91 if (ret < 0)
400 2 goto end;
401
1/2
✓ Branch 7 → 8 taken 89 times.
✗ Branch 7 → 9 not taken.
89 else if (ret == 0)
402 89 vbrtag_size = c->frame_size;
403
2/2
✓ Branch 9 → 10 taken 3 times.
✓ Branch 9 → 11 taken 86 times.
89 if (c->layer != 3) {
404 3 ret = -1;
405 3 goto end;
406 }
407
408
2/2
✓ Branch 11 → 12 taken 1 time.
✓ Branch 11 → 13 taken 85 times.
86 spf = c->lsf ? 576 : 1152; /* Samples per frame, layer 3 */
409
410 86 mp3->frames = 0;
411 86 mp3->header_filesize = 0;
412 86 mp3->frame_duration = av_rescale_q(spf, (AVRational){1, c->sample_rate}, st->time_base);
413
414 86 mp3_parse_info_tag(s, st, c, spf);
415 86 mp3_parse_vbri_tag(s, st, base);
416 86 mp3_parse_itunes_smpb(s, st, c, spf);
417
418 /* Packets keep the skipped samples, so shift the timeline instead. */
419
2/2
✓ Branch 18 → 19 taken 57 times.
✓ Branch 18 → 21 taken 29 times.
86 if (ffstream(st)->start_skip_samples)
420 57 st->start_time = av_rescale_q(ffstream(st)->start_skip_samples,
421 57 (AVRational){1, c->sample_rate},
422 st->time_base);
423
424
4/4
✓ Branch 21 → 22 taken 47 times.
✓ Branch 21 → 24 taken 39 times.
✓ Branch 22 → 23 taken 46 times.
✓ Branch 22 → 24 taken 1 time.
86 if (!mp3->frames && !mp3->header_filesize) {
425 46 ret = -1;
426 46 goto end;
427 }
428
429 /* Skip the vbr tag frame */
430 40 avio_seek(s->pb, base + vbrtag_size, SEEK_SET);
431
432
2/2
✓ Branch 25 → 26 taken 39 times.
✓ Branch 25 → 31 taken 1 time.
40 if (mp3->frames) {
433 int64_t full_duration_samples;
434
435 39 full_duration_samples = mp3->frames * (int64_t)spf;
436
1/2
✓ Branch 26 → 27 taken 39 times.
✗ Branch 26 → 28 not taken.
39 if (st->duration == AV_NOPTS_VALUE)
437 39 st->duration = av_rescale_q(full_duration_samples - mp3->start_pad - mp3->end_pad,
438 39 (AVRational){1, c->sample_rate},
439 st->time_base);
440
441
4/4
✓ Branch 28 → 29 taken 38 times.
✓ Branch 28 → 31 taken 1 time.
✓ Branch 29 → 30 taken 1 time.
✓ Branch 29 → 31 taken 37 times.
39 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 40 ret = 0;
447
448 91 end:
449 91 av_freep(&c);
450 91 return ret;
451 }
452
453 91 static int mp3_read_header(AVFormatContext *s)
454 {
455 91 FFFormatContext *const si = ffformatcontext(s);
456 91 MP3DecContext *mp3 = s->priv_data;
457 AVStream *st;
458 FFStream *sti;
459 int64_t off;
460 int ret;
461 int i;
462
463 91 s->metadata = si->id3v2_meta;
464 91 si->id3v2_meta = NULL;
465
466 91 st = avformat_new_stream(s, NULL);
467
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 91 times.
91 if (!st)
468 ✗ return AVERROR(ENOMEM);
469 91 sti = ffstream(st);
470
471 91 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
472 91 st->codecpar->codec_id = AV_CODEC_ID_MP3;
473 91 sti->need_parsing = AVSTREAM_PARSE_FULL_RAW;
474 91 st->start_time = 0;
475
476 // lcm of all mp3 sample rates
477 91 avpriv_set_pts_info(st, 64, 1, 14112000);
478
479 91 ffiocontext(s->pb)->maxsize = -1;
480 91 off = avio_tell(s->pb);
481
482
2/2
✓ Branch 11 → 12 taken 17 times.
✓ Branch 11 → 13 taken 74 times.
91 if (!av_dict_count(s->metadata))
483 17 ff_id3v1_read(s);
484
485
1/2
✓ Branch 13 → 14 taken 91 times.
✗ Branch 13 → 16 not taken.
91 if (s->pb->seekable & AVIO_SEEKABLE_NORMAL)
486 91 mp3->filesize = avio_size(s->pb);
487
488
2/2
✓ Branch 17 → 18 taken 51 times.
✓ Branch 17 → 19 taken 40 times.
91 if (mp3_parse_vbr_tags(s, st, off) < 0)
489 51 avio_seek(s->pb, off, SEEK_SET);
490
491 91 ret = ff_replaygain_export(st, s->metadata);
492
1/2
✗ Branch 20 → 21 not taken.
✓ Branch 20 → 22 taken 91 times.
91 if (ret < 0)
493 ✗ return ret;
494
495 91 ret = ffio_ensure_seekback(s->pb, 64 * 1024 + MPA_MAX_CODED_FRAME_SIZE + 4);
496
1/2
✗ Branch 23 → 24 not taken.
✓ Branch 23 → 25 taken 91 times.
91 if (ret < 0)
497 ✗ return ret;
498
499 91 off = avio_tell(s->pb);
500
1/2
✓ Branch 38 → 27 taken 717 times.
✗ Branch 38 → 39 not taken.
717 for (i = 0; i < 64 * 1024; i++) {
501 uint32_t header, header2;
502 int frame_size;
503 717 frame_size = check(s->pb, off + i, &header);
504
2/2
✓ Branch 28 → 29 taken 90 times.
✓ Branch 28 → 33 taken 627 times.
717 if (frame_size > 0) {
505 90 ret = check(s->pb, off + i + frame_size, &header2);
506
2/4
✓ Branch 30 → 31 taken 90 times.
✗ Branch 30 → 36 not taken.
✓ Branch 31 → 32 taken 90 times.
✗ Branch 31 → 36 not taken.
90 if (ret >= 0 && (header & MP3_MASK) == (header2 & MP3_MASK))
507 90 break;
508
2/2
✓ Branch 33 → 34 taken 1 time.
✓ Branch 33 → 36 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 39 → 40 not taken.
✓ Branch 39 → 41 taken 90 times.
90 if (i == 64 * 1024) {
514 ✗ off = avio_seek(s->pb, off, SEEK_SET);
515 } else {
516
2/2
✓ Branch 41 → 42 taken 1 time.
✓ Branch 41 → 43 taken 89 times.
90 av_log(s, i > 0 ? AV_LOG_INFO : AV_LOG_VERBOSE, "Skipping %d bytes of junk at %"PRId64".\n", i, off);
517 90 off = avio_seek(s->pb, off + i, SEEK_SET);
518 }
519
1/2
✗ Branch 46 → 47 not taken.
✓ Branch 46 → 48 taken 90 times.
90 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 50 → 49 taken 100 times.
✓ Branch 50 → 51 taken 90 times.
190 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 90 return 0;
528 }
529
530 #define MP3_PACKET_SIZE 1024
531
532 5990 static int mp3_read_packet(AVFormatContext *s, AVPacket *pkt)
533 {
534 5990 MP3DecContext *mp3 = s->priv_data;
535 int ret, size;
536 int64_t pos;
537
538 5990 size = MP3_PACKET_SIZE;
539 5990 pos = avio_tell(s->pb);
540
3/4
✓ Branch 3 → 4 taken 5990 times.
✗ Branch 3 → 6 not taken.
✓ Branch 4 → 5 taken 5887 times.
✓ Branch 4 → 6 taken 103 times.
5990 if (mp3->filesize > ID3v1_TAG_SIZE && pos < mp3->filesize)
541 5887 size= FFMIN(size, mp3->filesize - pos);
542
543 5990 ret = av_get_packet(s->pb, pkt, size);
544
2/2
✓ Branch 7 → 8 taken 103 times.
✓ Branch 7 → 11 taken 5887 times.
5990 if (ret <= 0) {
545
1/2
✓ Branch 8 → 9 taken 103 times.
✗ Branch 8 → 10 not taken.
103 if(ret<0)
546 103 return ret;
547 ✗ return AVERROR_EOF;
548 }
549
550 5887 pkt->flags &= ~AV_PKT_FLAG_CORRUPT;
551 5887 pkt->stream_index = 0;
552
553 5887 return ret;
554 }
555
556 #define SEEK_WINDOW 4096
557
558 17843 static int check(AVIOContext *pb, int64_t pos, uint32_t *ret_header)
559 {
560 17843 int64_t ret = avio_seek(pb, pos, SEEK_SET);
561 uint8_t header_buf[4];
562 unsigned header;
563 17843 MPADecodeHeader2 *sd = NULL;
564 int frame_size;
565
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 17843 times.
17843 if (ret < 0)
566 ✗ return CHECK_SEEK_FAILED;
567
568 17843 ret = avio_read(pb, &header_buf[0], 4);
569 /* We should always find four bytes for a valid mpa header. */
570
2/2
✓ Branch 6 → 7 taken 1 time.
✓ Branch 6 → 8 taken 17842 times.
17843 if (ret < 4)
571 1 return CHECK_SEEK_FAILED;
572
573 17842 header = AV_RB32(&header_buf[0]);
574
2/2
✓ Branch 9 → 10 taken 17490 times.
✓ Branch 9 → 11 taken 352 times.
17842 if (ff_mpa_check_header(header) < 0)
575 17490 return CHECK_WRONG_HEADER;
576
1/2
✗ Branch 12 → 13 not taken.
✓ Branch 12 → 15 taken 352 times.
352 if (avpriv_mpegaudio_decode_header2(&sd, header)) {
577 ✗ av_freep(&sd);
578 ✗ return CHECK_WRONG_HEADER;
579 }
580 352 frame_size = sd->frame_size;
581 352 av_freep(&sd);
582
583
2/2
✓ Branch 16 → 17 taken 180 times.
✓ Branch 16 → 18 taken 172 times.
352 if (ret_header)
584 180 *ret_header = header;
585 352 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 2 → 3 taken 13 times.
✓ Branch 2 → 4 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 7 → 8 not taken.
✓ Branch 7 → 9 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 38 → 10 taken 16916 times.
✗ Branch 38 → 39 not taken.
16916 for (i = 0; i < SEEK_WINDOW; i++) {
604
2/2
✓ Branch 10 → 11 taken 9621 times.
✓ Branch 10 → 12 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 13 → 14 not taken.
✓ Branch 13 → 15 taken 16916 times.
16916 if (pos < 0)
609 ✗ continue;
610
611
2/2
✓ Branch 32 → 16 taken 17036 times.
✓ Branch 32 → 33 taken 52 times.
17088 for (j = 0; j < MIN_VALID; j++) {
612 17036 ret = check(s->pb, pos, NULL);
613
2/2
✓ Branch 17 → 18 taken 16864 times.
✓ Branch 17 → 23 taken 172 times.
17036 if (ret < 0) {
614
1/2
✓ Branch 18 → 19 taken 16864 times.
✗ Branch 18 → 20 not taken.
16864 if (ret == CHECK_WRONG_HEADER) {
615 16864 break;
616
0/2
✗ Branch 20 → 21 not taken.
✗ Branch 20 → 23 not taken.
✗ } 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 23 → 24 taken 81 times.
✓ Branch 23 → 31 taken 91 times.
✓ Branch 24 → 25 taken 59 times.
✓ Branch 24 → 26 taken 22 times.
✓ Branch 27 → 28 taken 68 times.
✓ Branch 27 → 31 taken 13 times.
172 if ((target_pos - pos)*dir <= 0 && FFABS(MIN_VALID/2-j) < score) {
622 68 candidate = pos;
623
2/2
✓ Branch 28 → 29 taken 59 times.
✓ Branch 28 → 30 taken 9 times.
68 score = FFABS(MIN_VALID/2-j);
624 }
625 172 pos += ret;
626 }
627
4/4
✓ Branch 33 → 34 taken 49 times.
✓ Branch 33 → 37 taken 16867 times.
✓ Branch 34 → 35 taken 48 times.
✓ Branch 34 → 37 taken 1 time.
16916 if (best_score > score && j == MIN_VALID) {
628 48 best_pos = candidate;
629 48 best_score = score;
630
2/2
✓ Branch 35 → 36 taken 26 times.
✓ Branch 35 → 37 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 146 static int mp3_seek(AVFormatContext *s, int stream_index, int64_t timestamp,
639 int flags)
640 {
641 146 FFFormatContext *const si = ffformatcontext(s);
642 146 MP3DecContext *mp3 = s->priv_data;
643 AVIndexEntry *ie, ie1;
644 146 AVStream *st = s->streams[0];
645 146 FFStream *const sti = ffstream(st);
646 int64_t best_pos;
647 146 int fast_seek = s->flags & AVFMT_FLAG_FAST_SEEK;
648 146 int64_t filesize = mp3->header_filesize;
649
650
2/2
✓ Branch 4 → 5 taken 60 times.
✓ Branch 4 → 9 taken 86 times.
146 if (filesize <= 0) {
651 60 int64_t size = avio_size(s->pb);
652
2/4
✓ Branch 6 → 7 taken 60 times.
✗ Branch 6 → 9 not taken.
✓ Branch 7 → 8 taken 60 times.
✗ Branch 7 → 9 not taken.
60 if (size > 0 && size > si->data_offset)
653 60 filesize = size - si->data_offset;
654 }
655
656
5/8
✓ Branch 9 → 10 taken 26 times.
✓ Branch 9 → 18 taken 120 times.
✓ Branch 10 → 11 taken 26 times.
✗ Branch 10 → 13 not taken.
✓ Branch 11 → 12 taken 26 times.
✗ Branch 11 → 18 not taken.
✗ Branch 12 → 13 not taken.
✓ Branch 12 → 18 taken 26 times.
146 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
0/2
✗ Branch 15 → 16 not taken.
✗ Branch 15 → 17 not taken.
✗ if (ret < 0)
664 ✗ return ret;
665
666 ✗ ie = &sti->index_entries[ret];
667
4/6
✓ Branch 18 → 19 taken 26 times.
✓ Branch 18 → 24 taken 120 times.
✓ Branch 19 → 20 taken 26 times.
✗ Branch 19 → 24 not taken.
✓ Branch 20 → 21 taken 26 times.
✗ Branch 20 → 24 not taken.
146 } else if (fast_seek && st->duration > 0 && filesize > 0) {
668
1/2
✗ Branch 21 → 22 not taken.
✓ Branch 21 → 23 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 120 return -1; // generic index code
677 }
678
679 26 best_pos = mp3_sync(s, ie->pos, flags);
680
1/2
✗ Branch 26 → 27 not taken.
✓ Branch 26 → 28 taken 26 times.
26 if (best_pos < 0)
681 ✗ return best_pos;
682
683
4/8
✓ Branch 28 → 29 taken 26 times.
✗ Branch 28 → 33 not taken.
✓ Branch 29 → 30 taken 26 times.
✗ Branch 29 → 33 not taken.
✓ Branch 30 → 31 taken 26 times.
✗ Branch 30 → 33 not taken.
✓ Branch 31 → 32 taken 26 times.
✗ Branch 31 → 33 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