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