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