FFmpeg coverage


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