FFmpeg
Loading...
Searching...
No Matches
mp3dec.c
Go to the documentation of this file.
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"
26#include "libavutil/dict.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"
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
48typedef struct {
49 AVClass *class;
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;
60
65
66static int check(AVIOContext *pb, int64_t pos, uint32_t *header);
67
68/* mp3 read */
69
70static int mp3_read_probe(const AVProbeData *p)
71{
72 int max_frames, first_frames = 0;
73 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 buf0 = p->buf;
80 end = p->buf + p->buf_size - sizeof(uint32_t);
81 while (buf0 < end && !*buf0)
82 buf0++;
83
84 max_frames = 0;
85 max_framesizes = 0;
86 buf = buf0;
87
88 for (; buf < end; buf = buf2+1) {
89 buf2 = buf;
90 for (framesizes = frames = 0; buf2 < end; frames++) {
92 int header_emu = 0;
93 int available;
94
95 header = AV_RB32(buf2);
97 if (ret != 0)
98 break;
99
100 available = FFMIN(h.frame_size, end - buf2);
101 for (buf3 = buf2 + 4; buf3 < buf2 + available; buf3++) {
102 uint32_t next_sync = AV_RB32(buf3);
103 header_emu += (next_sync & MP3_MASK) == (header & MP3_MASK);
104 }
105 if (header_emu > 2)
106 break;
107 framesizes += h.frame_size;
108 if (available < h.frame_size) {
109 frames++;
110 break;
111 }
112 buf2 += h.frame_size;
113 }
114 max_frames = FFMAX(max_frames, frames);
115 max_framesizes = FFMAX(max_framesizes, framesizes);
116 if (buf == buf0) {
117 first_frames= frames;
118 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 if (first_frames>=7) return AVPROBE_SCORE_EXTENSION + 2;
125 else if (max_frames>200 && p->buf_size < 2*max_framesizes)return AVPROBE_SCORE_EXTENSION;
126 else if (max_frames>=4 && p->buf_size < 2*max_framesizes) return AVPROBE_SCORE_EXTENSION / 2;
127 else if (ff_id3v2_match(buf0, ID3v2_DEFAULT_MAGIC) && 2*ff_id3v2_tag_len(buf0) >= p->buf_size)
128 return p->buf_size < PROBE_BUF_MAX ? AVPROBE_SCORE_EXTENSION / 4 : AVPROBE_SCORE_EXTENSION - 2;
129 else if (first_frames > 1 && whole_used) return 5;
130 else if (max_frames>=1 && p->buf_size < 10*max_framesizes) return 1;
131 else return 0;
132 //mpegps_mp3_unrecognized_format.mpg has max_frames=3
133}
134
136{
137 int i;
138 MP3DecContext *mp3 = s->priv_data;
139 int fast_seek = s->flags & AVFMT_FLAG_FAST_SEEK;
140 int fill_index = (mp3->usetoc || fast_seek) && duration > 0;
141
142 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 for (i = 0; i < XING_TOC_COUNT; i++) {
150 uint8_t b = avio_r8(s->pb);
151 if (fill_index)
152 av_add_index_entry(s->streams[0],
153 av_rescale(b, filesize, 256),
155 0, 0, AVINDEX_KEYFRAME);
156 }
157 if (fill_index)
158 mp3->xing_toc = 1;
159}
160
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 FFStream *const sti = ffstream(st);
168 uint16_t crc;
169 uint32_t v;
170
171 char version[10];
172
173 uint32_t peak = 0;
174 int32_t r_gain = INT32_MIN, a_gain = INT32_MIN;
175
176 MP3DecContext *mp3 = s->priv_data;
177 static const int64_t xing_offtbl[2][2] = {{32, 17}, {17,9}};
178 uint64_t fsize = avio_size(s->pb);
179 int64_t pos = avio_tell(s->pb);
180 fsize = fsize >= pos ? fsize - pos : 0;
181
182 /* Check for Xing / Info tag */
183 avio_skip(s->pb, xing_offtbl[c->lsf == 1][c->nb_channels == 1]);
184 v = avio_rb32(s->pb);
185 mp3->is_cbr = v == MKBETAG('I', 'n', 'f', 'o');
186 if (v != MKBETAG('X', 'i', 'n', 'g') && !mp3->is_cbr)
187 return;
188
189 v = avio_rb32(s->pb);
190 if (v & XING_FLAG_FRAMES)
191 mp3->frames = avio_rb32(s->pb);
192 if (v & XING_FLAG_SIZE)
193 mp3->header_filesize = avio_rb32(s->pb);
194 if (fsize && mp3->header_filesize) {
195 uint64_t min, delta;
198 if (fsize > mp3->header_filesize && delta > min >> 4) {
199 mp3->frames = 0;
201 "invalid concatenated file detected - using bitrate for duration\n");
202 } else if (delta > min >> 4) {
204 "filesize and duration do not match (growing file?)\n");
205 }
206 }
207 if (v & XING_FLAG_TOC)
209 (AVRational){spf, c->sample_rate},
210 st->time_base));
211 /* VBR quality */
212 if (v & XING_FLAC_QSCALE)
213 avio_rb32(s->pb);
214
215 /* Encoder short version string */
216 memset(version, 0, sizeof(version));
217 avio_read(s->pb, version, 9);
218
219 /* Info Tag revision + VBR method */
220 avio_r8(s->pb);
221
222 /* Lowpass filter value */
223 avio_r8(s->pb);
224
225 /* ReplayGain peak */
226 v = avio_rb32(s->pb);
227 peak = av_rescale(v, 100000, 1 << 23);
228
229 /* Radio ReplayGain */
230 v = avio_rb16(s->pb);
231
232 if (MIDDLE_BITS(v, 13, 15) == 1) {
233 r_gain = MIDDLE_BITS(v, 0, 8) * 10000;
234
235 if (v & (1 << 9))
236 r_gain *= -1;
237 }
238
239 /* Audiophile ReplayGain */
240 v = avio_rb16(s->pb);
241
242 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 avio_r8(s->pb);
251
252 /* if ABR {specified bitrate} else {minimal bitrate} */
253 avio_r8(s->pb);
254
255 /* Encoder delays */
256 v = avio_rb24(s->pb);
257 if (AV_RB32(version) == MKBETAG('L', 'A', 'M', 'E')
258 || AV_RB32(version) == MKBETAG('L', 'a', 'v', 'f')
259 || AV_RB32(version) == MKBETAG('L', 'a', 'v', 'c')
260 ) {
261
262 mp3->start_pad = v>>12;
263 mp3-> end_pad = v&4095;
264 sti->start_skip_samples = mp3->start_pad + 528 + 1;
265 if (mp3->frames) {
266 sti->first_discard_sample = -mp3->end_pad + 528 + 1 + mp3->frames * (int64_t)spf;
267 sti->last_discard_sample = mp3->frames * (int64_t)spf;
268 }
269 av_log(s, AV_LOG_DEBUG, "pad %d %d\n", mp3->start_pad, mp3-> end_pad);
270 }
271
272 /* Misc */
273 avio_r8(s->pb);
274
275 /* MP3 gain */
276 avio_r8(s->pb);
277
278 /* Preset and surround info */
279 avio_rb16(s->pb);
280
281 /* Music length */
282 avio_rb32(s->pb);
283
284 /* Music CRC */
285 avio_rb16(s->pb);
286
287 /* Info Tag CRC */
288 crc = ffio_get_checksum(s->pb);
289 v = avio_rb16(s->pb);
290
291 if (v == crc) {
292 ff_replaygain_export_raw(st, r_gain, peak, a_gain, 0);
293 av_dict_set(&st->metadata, "encoder", version, 0);
294 }
295}
296
298{
299 uint32_t v;
300 MP3DecContext *mp3 = s->priv_data;
301
302 /* Check for VBRI tag (always 32 bytes after end of mpegaudio header) */
303 avio_seek(s->pb, base + 4 + 32, SEEK_SET);
304 v = avio_rb32(s->pb);
305 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}
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 */
326{
327 static const char comment[] = "comment-iTunSMPB";
328 const AVDictionaryEntry *de = NULL;
329
330 while ((de = av_dict_get(metadata, comment, de, AV_DICT_IGNORE_SUFFIX))) {
331 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 if (!*lang || (*lang == '-' && strlen(lang + 1) == 3))
335 return de;
336 }
337
338 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 */
346 const MPADecodeHeader *c, uint32_t spf)
347{
348 FFStream *const sti = ffstream(st);
349 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 if (sti->start_skip_samples)
355 return;
356
357 if (!(de = find_itunes_smpb(s->metadata)))
358 return;
359
360 if (ff_itunes_parse_smpb(de->value, &priming, &remainder, &samples) < 0)
361 return;
362
363 if (priming > 16384 || !samples)
364 return;
365
366 /* The three counts must add up to what the frames actually decode to. */
367 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 sti->start_skip_samples = priming;
373 sti->first_discard_sample = priming + samples;
374 sti->last_discard_sample = priming + samples + remainder;
375
376 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 */
384{
385 uint32_t v, spf;
387 int vbrtag_size = 0;
388 MP3DecContext *mp3 = s->priv_data;
389 int ret;
390
392
393 v = avio_rb32(s->pb);
394
396 if (ret < 0)
397 return ret;
398 else if (ret == 0)
399 vbrtag_size = c.frame_size;
400 if (c.layer != 3)
401 return -1;
402
403 spf = c.lsf ? 576 : 1152; /* Samples per frame, layer 3 */
404
405 mp3->frames = 0;
406 mp3->header_filesize = 0;
407 mp3->frame_duration = av_rescale_q(spf, (AVRational){1, c.sample_rate}, st->time_base);
408
409 mp3_parse_info_tag(s, st, &c, spf);
411 mp3_parse_itunes_smpb(s, st, &c, spf);
412
413 /* Packets keep the skipped samples, so shift the timeline instead. */
414 if (ffstream(st)->start_skip_samples)
415 st->start_time = av_rescale_q(ffstream(st)->start_skip_samples,
416 (AVRational){1, c.sample_rate},
417 st->time_base);
418
419 if (!mp3->frames && !mp3->header_filesize)
420 return -1;
421
422 /* Skip the vbr tag frame */
423 avio_seek(s->pb, base + vbrtag_size, SEEK_SET);
424
425 if (mp3->frames) {
426 int64_t full_duration_samples;
427
428 full_duration_samples = mp3->frames * (int64_t)spf;
429 if (st->duration == AV_NOPTS_VALUE)
430 st->duration = av_rescale_q(full_duration_samples - mp3->start_pad - mp3->end_pad,
431 (AVRational){1, c.sample_rate},
432 st->time_base);
433
434 if (mp3->header_filesize && !mp3->is_cbr)
435 st->codecpar->bit_rate = av_rescale(mp3->header_filesize, 8 * c.sample_rate,
436 full_duration_samples);
437 }
438
439 return 0;
440}
441
443{
444 FFFormatContext *const si = ffformatcontext(s);
445 MP3DecContext *mp3 = s->priv_data;
446 AVStream *st;
447 FFStream *sti;
448 int64_t off;
449 int ret;
450 int i;
451
452 s->metadata = si->id3v2_meta;
453 si->id3v2_meta = NULL;
454
456 if (!st)
457 return AVERROR(ENOMEM);
458 sti = ffstream(st);
459
463 st->start_time = 0;
464
465 // lcm of all mp3 sample rates
466 avpriv_set_pts_info(st, 64, 1, 14112000);
467
468 ffiocontext(s->pb)->maxsize = -1;
469 off = avio_tell(s->pb);
470
471 if (!av_dict_count(s->metadata))
473
474 if (s->pb->seekable & AVIO_SEEKABLE_NORMAL)
475 mp3->filesize = avio_size(s->pb);
476
477 if (mp3_parse_vbr_tags(s, st, off) < 0)
478 avio_seek(s->pb, off, SEEK_SET);
479
480 ret = ff_replaygain_export(st, s->metadata);
481 if (ret < 0)
482 return ret;
483
484 ret = ffio_ensure_seekback(s->pb, 64 * 1024 + MPA_MAX_CODED_FRAME_SIZE + 4);
485 if (ret < 0)
486 return ret;
487
488 off = avio_tell(s->pb);
489 for (i = 0; i < 64 * 1024; i++) {
490 uint32_t header, header2;
491 int frame_size;
492 frame_size = check(s->pb, off + i, &header);
493 if (frame_size > 0) {
494 ret = check(s->pb, off + i + frame_size, &header2);
495 if (ret >= 0 && (header & MP3_MASK) == (header2 & MP3_MASK))
496 break;
497 } else if (frame_size == CHECK_SEEK_FAILED) {
498 av_log(s, AV_LOG_ERROR, "Failed to find two consecutive MPEG audio frames.\n");
499 return AVERROR_INVALIDDATA;
500 }
501 }
502 if (i == 64 * 1024) {
503 off = avio_seek(s->pb, off, SEEK_SET);
504 } else {
505 av_log(s, i > 0 ? AV_LOG_INFO : AV_LOG_VERBOSE, "Skipping %d bytes of junk at %"PRId64".\n", i, off);
506 off = avio_seek(s->pb, off + i, SEEK_SET);
507 }
508 if (off < 0)
509 return off;
510
511 // the seek index is relative to the end of the xing vbr headers
512 for (int i = 0; i < sti->nb_index_entries; i++)
513 sti->index_entries[i].pos += off;
514
515 /* the parameters will be extracted from the compressed bitstream */
516 return 0;
517}
518
519#define MP3_PACKET_SIZE 1024
520
522{
523 MP3DecContext *mp3 = s->priv_data;
524 int ret, size;
525 int64_t pos;
526
528 pos = avio_tell(s->pb);
530 size= FFMIN(size, mp3->filesize - pos);
531
532 ret = av_get_packet(s->pb, pkt, size);
533 if (ret <= 0) {
534 if(ret<0)
535 return ret;
536 return AVERROR_EOF;
537 }
538
539 pkt->flags &= ~AV_PKT_FLAG_CORRUPT;
540 pkt->stream_index = 0;
541
542 return ret;
543}
544
545#define SEEK_WINDOW 4096
546
547static int check(AVIOContext *pb, int64_t pos, uint32_t *ret_header)
548{
549 int64_t ret = avio_seek(pb, pos, SEEK_SET);
550 uint8_t header_buf[4];
551 unsigned header;
553 if (ret < 0)
554 return CHECK_SEEK_FAILED;
555
556 ret = avio_read(pb, &header_buf[0], 4);
557 /* We should always find four bytes for a valid mpa header. */
558 if (ret < 4)
559 return CHECK_SEEK_FAILED;
560
561 header = AV_RB32(&header_buf[0]);
563 return CHECK_WRONG_HEADER;
565 return CHECK_WRONG_HEADER;
566
567 if (ret_header)
568 *ret_header = header;
569 return sd.frame_size;
570}
571
573{
574 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 avio_seek(s->pb, FFMAX(target_pos - SEEK_WINDOW, 0), SEEK_SET);
580 ret = avio_seek(s->pb, target_pos, SEEK_SET);
581 if (ret < 0)
582 return ret;
583
584#define MIN_VALID 3
585 best_pos = target_pos;
586 best_score = 999;
587 for (i = 0; i < SEEK_WINDOW; i++) {
588 int64_t pos = target_pos + (dir > 0 ? i - SEEK_WINDOW/4 : -i);
589 int64_t candidate = -1;
590 int score = 999;
591
592 if (pos < 0)
593 continue;
594
595 for (j = 0; j < MIN_VALID; j++) {
596 ret = check(s->pb, pos, NULL);
597 if (ret < 0) {
598 if (ret == CHECK_WRONG_HEADER) {
599 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 if ((target_pos - pos)*dir <= 0 && FFABS(MIN_VALID/2-j) < score) {
606 candidate = pos;
607 score = FFABS(MIN_VALID/2-j);
608 }
609 pos += ret;
610 }
611 if (best_score > score && j == MIN_VALID) {
612 best_pos = candidate;
613 best_score = score;
614 if(score == 0)
615 break;
616 }
617 }
618
619 return avio_seek(s->pb, best_pos, SEEK_SET);
620}
621
622static int mp3_seek(AVFormatContext *s, int stream_index, int64_t timestamp,
623 int flags)
624{
625 FFFormatContext *const si = ffformatcontext(s);
626 MP3DecContext *mp3 = s->priv_data;
627 AVIndexEntry *ie, ie1;
628 AVStream *st = s->streams[0];
629 FFStream *const sti = ffstream(st);
630 int64_t best_pos;
631 int fast_seek = s->flags & AVFMT_FLAG_FAST_SEEK;
633
634 if (filesize <= 0) {
635 int64_t size = avio_size(s->pb);
636 if (size > 0 && size > si->data_offset)
637 filesize = size - si->data_offset;
638 }
639
640 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 } else if (fast_seek && st->duration > 0 && filesize > 0) {
652 if (!mp3->is_cbr)
653 av_log(s, AV_LOG_WARNING, "Using scaling to seek VBR MP3; may be imprecise.\n");
654
655 ie = &ie1;
656 timestamp = av_clip64(timestamp, 0, st->duration);
657 ie->timestamp = timestamp;
658 ie->pos = av_rescale(timestamp, filesize, st->duration) + si->data_offset;
659 } else {
660 return -1; // generic index code
661 }
662
663 best_pos = mp3_sync(s, ie->pos, flags);
664 if (best_pos < 0)
665 return best_pos;
666
667 if (mp3->is_cbr && ie == &ie1 && mp3->frames && mp3->header_filesize > 0) {
668 ie1.timestamp = mp3->frame_duration * av_rescale(best_pos - si->data_offset, mp3->frames, mp3->header_filesize);
669 }
670
672 return 0;
673}
674
675static 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
680static 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
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};
const FFInputFormat ff_mp3_demuxer
Definition mp3dec.c:688
static int frames
static const AVClass demuxer_class
Definition apngdec.c:418
int32_t
static int64_t fsize(FILE *f)
Definition audiomatch.c:29
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition avformat.c:834
Main libavformat public API header.
#define AVINDEX_KEYFRAME
Definition avformat.h:628
#define AVFMT_FLAG_FAST_SEEK
Enable fast, but inaccurate seeks for some formats.
Definition avformat.h:1503
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition avformat.h:481
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition utils.c:98
#define AVSEEK_FLAG_BACKWARD
seek backward
Definition avformat.h:2616
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition avformat.h:499
@ AVSTREAM_PARSE_FULL_RAW
full parsing and repack with timestamp and position generation by parser for raw this assumes that ea...
Definition avformat.h:615
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition aviobuf.c:236
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition avio.h:41
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition aviobuf.c:326
unsigned int avio_rb16(AVIOContext *s)
Definition aviobuf.c:749
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition avio.h:494
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition aviobuf.c:321
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition aviobuf.c:615
unsigned int avio_rb24(AVIOContext *s)
Definition aviobuf.c:757
unsigned int avio_rb32(AVIOContext *s)
Definition aviobuf.c:764
int avio_r8(AVIOContext *s)
Definition aviobuf.c:606
void ffio_init_checksum(AVIOContext *s, unsigned long(*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len), unsigned long checksum)
Definition aviobuf.c:594
unsigned long ff_crcA001_update(unsigned long checksum, const uint8_t *buf, unsigned int len)
Definition aviobuf.c:580
static av_always_inline FFIOContext * ffiocontext(AVIOContext *ctx)
unsigned long ffio_get_checksum(AVIOContext *s)
Definition aviobuf.c:586
int ffio_ensure_seekback(AVIOContext *s, int64_t buf_size)
Ensures that the requested seekback buffer size will be available.
Definition aviobuf.c:1026
static int FUNC metadata(CodedBitstreamContext *ctx, RWContext *rw, APVRawMetadata *current)
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
static int FUNC comment(CodedBitstreamContext *ctx, RWContext *rw, JPEGRawComment *current)
#define s(width, name)
Definition cbs_vp9.c:198
#define av_clip64
Definition common.h:103
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition common.h:74
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
#define min(a, b)
#define FF_INFMT_FLAG_ID3V2_AUTO
Automatically parse ID3v2 metadata.
Definition demux.h:45
int ff_itunes_parse_smpb(const char *value, int64_t *priming, int64_t *remainder, int64_t *samples)
Parse the gapless playback information Apple encoders store in an iTunSMPB tag.
void avpriv_update_cur_dts(AVFormatContext *s, AVStream *ref_st, int64_t timestamp)
Update cur_dts of all streams based on the given timestamp and AVStream.
Definition seek.c:37
static AVPacket * pkt
Public dictionary API.
static int64_t filesize(AVIOContext *pb)
Definition ffmpeg_mux.c:51
static int64_t duration
Definition ffplay.c:330
static const uint8_t frame_size[4]
Definition g723_1.h:222
#define AV_OPT_FLAG_DECODING_PARAM
A generic parameter which can be set by the user for demuxing or decoding.
Definition opt.h:355
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition opt.h:326
@ AV_CODEC_ID_MP3
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition codec_id.h:454
#define AV_PKT_FLAG_CORRUPT
The packet content is corrupted.
Definition packet.h:651
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
int av_add_index_entry(AVStream *st, int64_t pos, int64_t timestamp, int size, int distance, int flags)
Add an index entry into a sorted list.
Definition seek.c:122
int av_index_search_timestamp(AVStream *st, int64_t timestamp, int flags)
Get the index for a specific timestamp.
Definition seek.c:245
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition dict.c:60
#define AV_DICT_IGNORE_SUFFIX
Return first entry in a dictionary whose first part corresponds to the search key,...
Definition dict.h:75
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition dict.c:86
int av_dict_count(const AVDictionary *m)
Get number of entries in dictionary.
Definition dict.c:37
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_VERBOSE
Detailed information.
Definition log.h:226
#define AV_LOG_INFO
Standard information.
Definition log.h:221
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition avutil.h:247
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
void ff_id3v1_read(AVFormatContext *s)
Read an ID3v1 tag.
Definition id3v1.c:278
#define ID3v1_TAG_SIZE
Definition id3v1.h:27
int ff_id3v2_tag_len(const uint8_t *buf)
Get the length of an ID3v2 tag.
Definition id3v2.c:165
int ff_id3v2_match(const uint8_t *buf, const char *magic)
Detect ID3v2 Header.
Definition id3v2.c:152
#define ID3v2_DEFAULT_MAGIC
Default magic bytes for ID3v2 header: "ID3".
Definition id3v2.h:35
#define b
Definition input.c:43
#define AV_RB32(p)
static av_always_inline FFStream * ffstream(AVStream *st)
Definition internal.h:365
#define PROBE_BUF_MAX
Definition internal.h:34
static av_always_inline FFFormatContext * ffformatcontext(AVFormatContext *s)
Definition internal.h:130
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
version
Definition libkvazaar.c:313
@ AV_CLASS_CATEGORY_DEMUXER
Definition log.h:33
#define FFMIN(a, b)
Definition macros.h:49
#define MKBETAG(a, b, c, d)
Definition macros.h:56
#define FFMAX(a, b)
Definition macros.h:47
#define check(x, y, S, v)
CheckRet
Definition mp3dec.c:61
@ CHECK_SEEK_FAILED
Definition mp3dec.c:63
@ CHECK_WRONG_HEADER
Definition mp3dec.c:62
static int mp3_read_header(AVFormatContext *s)
Definition mp3dec.c:442
#define XING_FLAC_QSCALE
Definition mp3dec.c:43
#define MIN_VALID
static int mp3_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Definition mp3dec.c:622
#define MP3_PACKET_SIZE
Definition mp3dec.c:519
static void mp3_parse_info_tag(AVFormatContext *s, AVStream *st, MPADecodeHeader *c, uint32_t spf)
Definition mp3dec.c:161
#define XING_FLAG_FRAMES
Definition mp3dec.c:40
static int64_t mp3_sync(AVFormatContext *s, int64_t target_pos, int flags)
Definition mp3dec.c:572
static void mp3_parse_vbri_tag(AVFormatContext *s, AVStream *st, int64_t base)
Definition mp3dec.c:297
#define MIDDLE_BITS(k, m, n)
#define SEEK_WINDOW
Definition mp3dec.c:545
#define XING_TOC_COUNT
Definition mp3dec.c:45
static void read_xing_toc(AVFormatContext *s, int64_t filesize, int64_t duration)
Definition mp3dec.c:135
static int mp3_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition mp3dec.c:521
static int mp3_parse_vbr_tags(AVFormatContext *s, AVStream *st, int64_t base)
Try to find Xing/Info/VBRI tags and compute duration from info therein.
Definition mp3dec.c:383
static void mp3_parse_itunes_smpb(AVFormatContext *s, AVStream *st, const MPADecodeHeader *c, uint32_t spf)
Parse the gapless playback information Apple encoders store in an iTunSMPB comment instead of in the ...
Definition mp3dec.c:345
#define XING_FLAG_SIZE
Definition mp3dec.c:41
static int mp3_read_probe(const AVProbeData *p)
Definition mp3dec.c:70
#define XING_FLAG_TOC
Definition mp3dec.c:42
static const AVDictionaryEntry * find_itunes_smpb(const AVDictionary *metadata)
Look the iTunSMPB tag up under every key it can land on.
Definition mp3dec.c:325
static const uint8_t xing_offtbl[2][2]
Definition mp3enc.c:141
mpeg audio declarations for both encoder and decoder.
#define MPA_MAX_CODED_FRAME_SIZE
Definition mpegaudio.h:40
int avpriv_mpegaudio_decode_header(MPADecodeHeader *s, uint32_t header)
MPEG Audio header decoder.
static int ff_mpa_check_header(uint32_t header)
#define MP3_MASK
AVOptions.
int ff_replaygain_export(AVStream *st, AVDictionary *metadata)
Parse replaygain tags and export them as per-stream side data.
Definition replaygain.c:94
int ff_replaygain_export_raw(AVStream *st, int32_t tg, uint32_t tp, int32_t ag, uint32_t ap)
Export already decoded replaygain values as per-stream side data.
Definition replaygain.c:69
static const uint8_t header[24]
Definition sdr2.c:68
unsigned int pos
Definition spdifenc.c:431
Describe the class of an AVClass context structure.
Definition log.h:76
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition codec_par.h:99
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
char * key
Definition dict.h:91
char * value
Definition dict.h:92
Format I/O context.
Definition avformat.h:1333
Bytestream IO Context.
Definition avio.h:160
int64_t pos
Definition avformat.h:621
int64_t timestamp
Timestamp in AVStream.time_base units, preferably the time from which on correctly decoded frames are...
Definition avformat.h:622
AVOption.
Definition opt.h:428
This structure stores compressed data.
Definition packet.h:580
This structure contains the data a format has to probe a file.
Definition avformat.h:471
Rational number (pair of numerator and denominator).
Definition rational.h:58
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition avformat.h:825
AVDictionary * metadata
Definition avformat.h:846
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base.
Definition avformat.h:815
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition avformat.h:805
AVDictionary * id3v2_meta
ID3v2 tag useful for MP3 demuxing.
Definition internal.h:118
int64_t data_offset
offset of the first packet
Definition internal.h:89
int64_t maxsize
max filesize, used to limit allocations
int64_t last_discard_sample
The sample after last sample that is intended to be discarded after first_discard_sample.
Definition internal.h:239
int64_t start_skip_samples
If not 0, the number of samples that should be skipped from the start of the stream (the samples are ...
Definition internal.h:224
int64_t first_discard_sample
If not 0, the first audio sample that should be discarded from the stream.
Definition internal.h:232
int nb_index_entries
Definition internal.h:193
AVIndexEntry * index_entries
Only used if the format does not support seeking natively.
Definition internal.h:191
enum AVStreamParseType need_parsing
Definition internal.h:321
int end_pad
Definition mp3dec.c:53
int64_t filesize
Definition mp3dec.c:50
unsigned frames
Definition mp3dec.c:55
unsigned frame_duration
Definition mp3dec.c:57
unsigned header_filesize
Definition mp3dec.c:56
int start_pad
Definition mp3dec.c:52
int xing_toc
Definition mp3dec.c:51
#define av_log(a,...)
int size
float delta
uint8_t base
Definition vp3data.h:128
static double c[64]