FFmpeg
Loading...
Searching...
No Matches
demux.c
Go to the documentation of this file.
1/*
2 * Core demuxing component
3 * Copyright (c) 2000, 2001, 2002 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 <stdint.h>
23
24#include "config_components.h"
25
26#include "libavutil/avassert.h"
27#include "libavutil/avstring.h"
28#include "libavutil/dict.h"
29#include "libavutil/internal.h"
32#include "libavutil/mem.h"
33#include "libavutil/opt.h"
34#include "libavutil/pixfmt.h"
35#include "libavutil/time.h"
36#include "libavutil/timestamp.h"
37
38#include "libavcodec/avcodec.h"
39#include "libavcodec/bsf.h"
41#include "libavcodec/internal.h"
42#include "packet_internal.h"
43#include "libavcodec/raw.h"
44
45#include "avformat.h"
46#include "avformat_internal.h"
47#include "avio_internal.h"
48#include "demux.h"
49#include "id3v2.h"
50#include "internal.h"
51#include "url.h"
52
53static int64_t wrap_timestamp(const AVStream *st, int64_t timestamp)
54{
55 const FFStream *const sti = cffstream(st);
56 if (sti->pts_wrap_behavior != AV_PTS_WRAP_IGNORE && st->pts_wrap_bits < 64 &&
57 sti->pts_wrap_reference != AV_NOPTS_VALUE && timestamp != AV_NOPTS_VALUE) {
59 timestamp < sti->pts_wrap_reference)
60 return timestamp + (1ULL << st->pts_wrap_bits);
62 timestamp >= sti->pts_wrap_reference)
63 return timestamp - (1ULL << st->pts_wrap_bits);
64 }
65 return timestamp;
66}
67
69{
70 return wrap_timestamp(st, timestamp);
71}
72
74{
75 const AVCodec *codec;
76
77#if CONFIG_H264_DECODER
78 /* Other parts of the code assume this decoder to be used for h264,
79 * so force it if possible. */
81 return avcodec_find_decoder_by_name("h264");
82#endif
83
84 codec = ff_find_decoder(s, st, codec_id);
85 if (!codec)
86 return NULL;
87
89 const AVCodec *probe_codec = NULL;
90 void *iter = NULL;
91 while ((probe_codec = av_codec_iterate(&iter))) {
92 if (probe_codec->id == codec->id &&
95 return probe_codec;
96 }
97 }
98 }
99
100 return codec;
101}
102
104 AVProbeData *pd)
105{
106 static const struct {
107 const char *name;
108 enum AVCodecID id;
109 enum AVMediaType type;
110 } fmt_id_type[] = {
123 { "mjpeg_2000", AV_CODEC_ID_JPEG2000, AVMEDIA_TYPE_VIDEO },
129 { 0 }
130 };
131 int score;
132 const AVInputFormat *fmt = av_probe_input_format3(pd, 1, &score);
133 FFStream *const sti = ffstream(st);
134
135 if (fmt) {
137 "Probe with size=%d, packets=%d detected %s with score=%d\n",
138 pd->buf_size, s->max_probe_packets - sti->probe_packets,
139 fmt->name, score);
140 for (int i = 0; fmt_id_type[i].name; i++) {
141 if (!strcmp(fmt->name, fmt_id_type[i].name)) {
142 if (fmt_id_type[i].type != AVMEDIA_TYPE_AUDIO &&
144 continue;
145 if (sti->request_probe > score &&
146 st->codecpar->codec_id != fmt_id_type[i].id)
147 continue;
148 st->codecpar->codec_id = fmt_id_type[i].id;
149 st->codecpar->codec_type = fmt_id_type[i].type;
150 sti->need_context_update = 1;
151 return score;
152 }
153 }
154 }
155 return 0;
156}
157
158static int init_input(AVFormatContext *s, const char *filename,
160{
161 int ret;
162 AVProbeData pd = { filename, NULL, 0 };
163 int score = AVPROBE_SCORE_RETRY;
164
165 if (s->pb) {
166 s->flags |= AVFMT_FLAG_CUSTOM_IO;
167 if (!s->iformat)
168 return av_probe_input_buffer2(s->pb, &s->iformat, filename,
169 s, 0, s->format_probesize);
170 else if (s->iformat->flags & AVFMT_NOFILE)
171 av_log(s, AV_LOG_WARNING, "Custom AVIOContext makes no sense and "
172 "will be ignored with AVFMT_NOFILE format.\n");
173 return 0;
174 }
175
176 if ((s->iformat && s->iformat->flags & AVFMT_NOFILE) ||
177 (!s->iformat && (s->iformat = av_probe_input_format2(&pd, 0, &score))))
178 return score;
179
180 if ((ret = s->io_open(s, &s->pb, filename, AVIO_FLAG_READ | s->avio_flags, options)) < 0)
181 return ret;
182
183 if (s->iformat)
184 return 0;
185 return av_probe_input_buffer2(s->pb, &s->iformat, filename,
186 s, 0, s->format_probesize);
187}
188
189static int codec_close(AVFormatContext *s, FFStream *sti);
190
192{
193 int ret;
194 for (unsigned i = 0; i < s->nb_streams; i++) {
195 AVStream *const st = s->streams[i];
196 FFStream *const sti = ffstream(st);
197
198 if (!sti->need_context_update)
199 continue;
200
201 if (avcodec_is_open(sti->avctx)) {
202 av_log(s, AV_LOG_DEBUG, "Demuxer context update while decoder is open, closing and trying to re-open\n");
203 ret = codec_close(s, sti);
204 sti->info->found_decoder = 0;
205 if (ret < 0)
206 return ret;
207 }
208
209 /* close parser, because it depends on the codec */
210 if (sti->parser && sti->avctx->codec_id != st->codecpar->codec_id) {
212 sti->parser = NULL;
213 }
214
215 /* update internal codec context, for the parser */
217 if (ret < 0)
218 return ret;
219
221
222 sti->need_context_update = 0;
223 }
224 return 0;
225}
226
230
231int avformat_open_input(AVFormatContext **ps, const char *filename,
232 const AVInputFormat *fmt, AVDictionary **options)
233{
235 AVFormatContext *s = *ps;
236 FFFormatContext *si;
238 ID3v2ExtraMeta *id3v2_extra_meta = NULL;
239 int ret = 0;
240
241 if (!s && !(s = avformat_alloc_context()))
242 return AVERROR(ENOMEM);
243 fci = ff_fc_internal(s);
244 si = &fci->fc;
245 if (!s->av_class) {
246 av_log(NULL, AV_LOG_ERROR, "Input context has not been properly allocated by avformat_alloc_context() and is not NULL either\n");
247 return AVERROR(EINVAL);
248 }
249 if (fmt)
250 s->iformat = fmt;
251
252 if (options)
253 av_dict_copy(&tmp, *options, 0);
254
255 if (s->pb) // must be before any goto fail
256 s->flags |= AVFMT_FLAG_CUSTOM_IO;
257
258 if ((ret = av_opt_set_dict(s, &tmp)) < 0)
259 goto fail;
260
261 if (!(s->url = av_strdup(filename ? filename : ""))) {
262 ret = AVERROR(ENOMEM);
263 goto fail;
264 }
265
266 if ((ret = init_input(s, filename, &tmp)) < 0)
267 goto fail;
268 s->probe_score = ret;
269
270 if (!s->protocol_whitelist && s->pb && s->pb->protocol_whitelist) {
271 s->protocol_whitelist = av_strdup(s->pb->protocol_whitelist);
272 if (!s->protocol_whitelist) {
273 ret = AVERROR(ENOMEM);
274 goto fail;
275 }
276 }
277
278 if (!s->protocol_blacklist && s->pb && s->pb->protocol_blacklist) {
279 s->protocol_blacklist = av_strdup(s->pb->protocol_blacklist);
280 if (!s->protocol_blacklist) {
281 ret = AVERROR(ENOMEM);
282 goto fail;
283 }
284 }
285
286 if (s->format_whitelist && av_match_list(s->iformat->name, s->format_whitelist, ',') <= 0) {
287 av_log(s, AV_LOG_ERROR, "Format not on whitelist \'%s\'\n", s->format_whitelist);
288 ret = AVERROR(EINVAL);
289 goto fail;
290 }
291
292 avio_skip(s->pb, s->skip_initial_bytes);
293
294 /* Check filename in case an image number is expected. */
295 if (s->iformat->flags & AVFMT_NEEDNUMBER) {
296 if (!av_filename_number_test(filename)) {
297 ret = AVERROR(EINVAL);
298 goto fail;
299 }
300 }
301
302 s->duration = s->start_time = AV_NOPTS_VALUE;
303
304 /* Allocate private data. */
305 if (ffifmt(s->iformat)->priv_data_size > 0) {
306 if (!(s->priv_data = av_mallocz(ffifmt(s->iformat)->priv_data_size))) {
307 ret = AVERROR(ENOMEM);
308 goto fail;
309 }
310 if (s->iformat->priv_class) {
311 *(const AVClass **) s->priv_data = s->iformat->priv_class;
312 av_opt_set_defaults(s->priv_data);
313 if ((ret = av_opt_set_dict(s->priv_data, &tmp)) < 0)
314 goto fail;
315 }
316 }
317
318 /* e.g. AVFMT_NOFILE formats will not have an AVIOContext */
319 if (s->pb && is_id3v2_format(s->iformat))
320 ff_id3v2_read_dict(s, s->pb, &si->id3v2_meta, ID3v2_DEFAULT_MAGIC, &id3v2_extra_meta);
321
322 if (ffifmt(s->iformat)->read_header)
323 if ((ret = ffifmt(s->iformat)->read_header(s)) < 0) {
325 goto close;
326 goto fail;
327 }
328
329 if (!s->metadata) {
330 s->metadata = si->id3v2_meta;
331 si->id3v2_meta = NULL;
332 } else if (si->id3v2_meta) {
333 av_log(s, AV_LOG_WARNING, "Discarding ID3 tags because more suitable tags were found.\n");
335 }
336
337 if (id3v2_extra_meta) {
338 if ((ret = ff_id3v2_parse_apic(s, id3v2_extra_meta)) < 0)
339 goto close;
340 if ((ret = ff_id3v2_parse_chapters(s, id3v2_extra_meta)) < 0)
341 goto close;
342 if ((ret = ff_id3v2_parse_priv(s, id3v2_extra_meta)) < 0)
343 goto close;
344 ff_id3v2_free_extra_meta(&id3v2_extra_meta);
345 }
346
348 goto close;
349
350 if (s->pb && !si->data_offset)
351 si->data_offset = avio_tell(s->pb);
352
353 fci->raw_packet_buffer_size = 0;
354
356
357#if FF_API_STREAM_GROUP_ENTRIES
359 for (unsigned i = 0; i < s->nb_stream_groups; i++) {
360 AVStreamGroup *const stg = s->stream_groups[i];
362 AVStreamGroupTileGrid *tile_grid = stg->params.tile_grid;
363 for (int j = 0; j < stg->nb_coded_side_data; j++) {
364 const AVPacketSideData *src = &stg->coded_side_data[j];
365 AVPacketSideData *dst = av_packet_side_data_new(&tile_grid->coded_side_data,
366 &tile_grid->nb_coded_side_data,
367 src->type, src->size, 0);
368 if (!dst) {
369 ret = AVERROR(ENOMEM);
370 goto close;
371 }
372
373 memcpy(dst->data, src->data, src->size);
374 }
375 }
376 }
378#endif
379
380 if (options) {
382 *options = tmp;
383 }
384 *ps = s;
385 return 0;
386
387close:
388 if (ffifmt(s->iformat)->read_close)
389 ffifmt(s->iformat)->read_close(s);
390fail:
391 ff_id3v2_free_extra_meta(&id3v2_extra_meta);
393 if (s->pb && !(s->flags & AVFMT_FLAG_CUSTOM_IO))
394 ff_format_io_close(s, &s->pb);
396 *ps = NULL;
397 return ret;
398}
399
401{
403 AVIOContext *pb;
404
405 if (!ps || !*ps)
406 return;
407
408 s = *ps;
409 pb = s->pb;
410
411 if ((s->iformat && strcmp(s->iformat->name, "image2") && s->iformat->flags & AVFMT_NOFILE) ||
412 (s->flags & AVFMT_FLAG_CUSTOM_IO))
413 pb = NULL;
414
415 if (s->iformat)
416 if (ffifmt(s->iformat)->read_close)
417 ffifmt(s->iformat)->read_close(s);
418
419 ff_format_io_close(s, &pb);
421
422 *ps = NULL;
423}
424
426{
427 switch (st->codecpar->codec_type) {
429 if (s->video_codec_id)
430 st->codecpar->codec_id = s->video_codec_id;
431 break;
433 if (s->audio_codec_id)
434 st->codecpar->codec_id = s->audio_codec_id;
435 break;
437 if (s->subtitle_codec_id)
438 st->codecpar->codec_id = s->subtitle_codec_id;
439 break;
441 if (s->data_codec_id)
442 st->codecpar->codec_id = s->data_codec_id;
443 break;
444 }
445}
446
448{
450 FFStream *const sti = ffstream(st);
451
452 if (sti->request_probe > 0) {
453 AVProbeData *const pd = &sti->probe_data;
454 int end;
455 av_log(s, AV_LOG_DEBUG, "probing stream %d pp:%d\n", st->index, sti->probe_packets);
456 --sti->probe_packets;
457
458 if (pkt) {
459 uint8_t *new_buf = av_realloc(pd->buf, pd->buf_size+pkt->size+AVPROBE_PADDING_SIZE);
460 if (!new_buf) {
462 "Failed to reallocate probe buffer for stream %d\n",
463 st->index);
464 goto no_packet;
465 }
466 pd->buf = new_buf;
467 memcpy(pd->buf + pd->buf_size, pkt->data, pkt->size);
468 pd->buf_size += pkt->size;
469 memset(pd->buf + pd->buf_size, 0, AVPROBE_PADDING_SIZE);
470 } else {
471no_packet:
472 sti->probe_packets = 0;
473 if (!pd->buf_size) {
475 "nothing to probe for stream %d\n", st->index);
476 }
477 }
478
479 end = fci->raw_packet_buffer_size >= s->probesize ||
480 sti->probe_packets <= 0;
481
482 if (end || av_log2(pd->buf_size) != av_log2(pd->buf_size - pkt->size)) {
483 int score = set_codec_from_probe_data(s, st, pd);
485 || end) {
486 pd->buf_size = 0;
487 av_freep(&pd->buf);
488 sti->request_probe = -1;
489 if (st->codecpar->codec_id != AV_CODEC_ID_NONE) {
490 av_log(s, AV_LOG_DEBUG, "probed stream %d\n", st->index);
491 } else
492 av_log(s, AV_LOG_WARNING, "probed stream %d failed\n", st->index);
493 }
494 force_codec_ids(s, st);
495 }
496 }
497 return 0;
498}
499
500static int update_wrap_reference(AVFormatContext *s, AVStream *st, int stream_index, AVPacket *pkt)
501{
502 FFStream *const sti = ffstream(st);
503 int64_t ref = pkt->dts;
504 int pts_wrap_behavior;
505 int64_t pts_wrap_reference;
506 AVProgram *first_program;
507
508 if (ref == AV_NOPTS_VALUE)
509 ref = pkt->pts;
510 if (sti->pts_wrap_reference != AV_NOPTS_VALUE || st->pts_wrap_bits >= 63 || ref == AV_NOPTS_VALUE || !s->correct_ts_overflow)
511 return 0;
512 ref &= (1LL << st->pts_wrap_bits)-1;
513
514 // reference time stamp should be 60 s before first time stamp
515 pts_wrap_reference = ref - av_rescale(60, st->time_base.den, st->time_base.num);
516 // if first time stamp is not more than 1/8 and 60s before the wrap point, subtract rather than add wrap offset
517 pts_wrap_behavior = (ref < (1LL << st->pts_wrap_bits) - (1LL << st->pts_wrap_bits-3)) ||
518 (ref < (1LL << st->pts_wrap_bits) - av_rescale(60, st->time_base.den, st->time_base.num)) ?
520
521 first_program = av_find_program_from_stream(s, NULL, stream_index);
522
523 if (!first_program) {
524 int default_stream_index = av_find_default_stream_index(s);
525 FFStream *const default_sti = ffstream(s->streams[default_stream_index]);
526 if (default_sti->pts_wrap_reference == AV_NOPTS_VALUE) {
527 for (unsigned i = 0; i < s->nb_streams; i++) {
528 FFStream *const sti = ffstream(s->streams[i]);
530 continue;
531 sti->pts_wrap_reference = pts_wrap_reference;
532 sti->pts_wrap_behavior = pts_wrap_behavior;
533 }
534 } else {
535 sti->pts_wrap_reference = default_sti->pts_wrap_reference;
536 sti->pts_wrap_behavior = default_sti->pts_wrap_behavior;
537 }
538 } else {
539 AVProgram *program = first_program;
540 while (program) {
541 if (program->pts_wrap_reference != AV_NOPTS_VALUE) {
542 pts_wrap_reference = program->pts_wrap_reference;
543 pts_wrap_behavior = program->pts_wrap_behavior;
544 break;
545 }
546 program = av_find_program_from_stream(s, program, stream_index);
547 }
548
549 // update every program with differing pts_wrap_reference
550 program = first_program;
551 while (program) {
552 if (program->pts_wrap_reference != pts_wrap_reference) {
553 for (unsigned i = 0; i < program->nb_stream_indexes; i++) {
554 FFStream *const sti = ffstream(s->streams[program->stream_index[i]]);
555 sti->pts_wrap_reference = pts_wrap_reference;
556 sti->pts_wrap_behavior = pts_wrap_behavior;
557 }
558
559 program->pts_wrap_reference = pts_wrap_reference;
560 program->pts_wrap_behavior = pts_wrap_behavior;
561 }
562 program = av_find_program_from_stream(s, program, stream_index);
563 }
564 }
565 return 1;
566}
567
569{
570 FFStream *const sti = ffstream(st);
571
572 if (update_wrap_reference(s, st, pkt->stream_index, pkt) && sti->pts_wrap_behavior == AV_PTS_WRAP_SUB_OFFSET) {
573 // correct first time stamps to negative values
574 if (!is_relative(sti->first_dts))
575 sti->first_dts = wrap_timestamp(st, sti->first_dts);
576 if (!is_relative(st->start_time))
577 st->start_time = wrap_timestamp(st, st->start_time);
578 if (!is_relative(sti->cur_dts))
579 sti->cur_dts = wrap_timestamp(st, sti->cur_dts);
580 }
581
582 pkt->dts = wrap_timestamp(st, pkt->dts);
583 pkt->pts = wrap_timestamp(st, pkt->pts);
584
585 force_codec_ids(s, st);
586
587 /* TODO: audio: time filter; video: frame reordering (pts != dts) */
588 if (s->use_wallclock_as_timestamps)
589 pkt->dts = pkt->pts = av_rescale_q(av_gettime(), AV_TIME_BASE_Q, st->time_base);
590}
591
592/**
593 * Handle a new packet and either return it directly if possible and
594 * allow_passthrough is true or queue the packet (or drop the packet
595 * if corrupt).
596 *
597 * @return < 0 on error, 0 if the packet was passed through,
598 * 1 if it was queued or dropped
599 */
600static int handle_new_packet(AVFormatContext *s, AVPacket *pkt, int allow_passthrough)
601{
603 AVStream *st;
604 FFStream *sti;
605 int err;
606
607 av_assert0(pkt->stream_index < (unsigned)s->nb_streams &&
608 "Invalid stream index.\n");
609
610 if (pkt->flags & AV_PKT_FLAG_CORRUPT) {
612 "Packet corrupt (stream = %d, dts = %s)%s.\n",
613 pkt->stream_index, av_ts2str(pkt->dts),
614 s->flags & AVFMT_FLAG_DISCARD_CORRUPT ? ", dropping it" : "");
615 if (s->flags & AVFMT_FLAG_DISCARD_CORRUPT) {
617 return 1;
618 }
619 }
620
621 st = s->streams[pkt->stream_index];
622 sti = ffstream(st);
623
624 update_timestamps(s, st, pkt);
625
626 if (sti->request_probe <= 0 && allow_passthrough && !fci->raw_packet_buffer.head)
627 return 0;
628
630 if (err < 0) {
632 return err;
633 }
634
635 pkt = &fci->raw_packet_buffer.tail->pkt;
636 fci->raw_packet_buffer_size += pkt->size;
637
638 err = probe_codec(s, st, pkt);
639 if (err < 0)
640 return err;
641
642 return 1;
643}
644
646{
647 int err = handle_new_packet(s, pkt, 0);
648
649 return err < 0 ? err : 0;
650}
651
653{
655 int err;
656
657#if FF_API_INIT_PACKET
659 pkt->data = NULL;
660 pkt->size = 0;
661 av_init_packet(pkt);
663#else
665#endif
666
667 for (;;) {
669
670 if (pktl) {
671 AVStream *const st = s->streams[pktl->pkt.stream_index];
672 if (fci->raw_packet_buffer_size >= s->probesize)
673 if ((err = probe_codec(s, st, NULL)) < 0)
674 return err;
675 if (ffstream(st)->request_probe <= 0) {
677 fci->raw_packet_buffer_size -= pkt->size;
678 return 0;
679 }
680 }
681
682 err = ffifmt(s->iformat)->read_packet(s, pkt);
683 if (err < 0) {
685
686 /* Some demuxers return FFERROR_REDO when they consume
687 data and discard it (ignored streams, junk, extradata).
688 We must re-call the demuxer to get the real packet. */
689 if (err == FFERROR_REDO)
690 continue;
691 if (!pktl || err == AVERROR(EAGAIN))
692 return err;
693 for (unsigned i = 0; i < s->nb_streams; i++) {
694 AVStream *const st = s->streams[i];
695 FFStream *const sti = ffstream(st);
696 if (sti->probe_packets || sti->request_probe > 0)
697 if ((err = probe_codec(s, st, NULL)) < 0)
698 return err;
699 av_assert0(sti->request_probe <= 0);
700 }
701 continue;
702 }
703
705 if (err < 0) {
707 return err;
708 }
709
710 err = handle_new_packet(s, pkt, 1);
711 if (err <= 0) /* Error or passthrough */
712 return err;
713 }
714}
715
716/**
717 * Return the frame duration in seconds. Return 0 if not available.
718 */
719static void compute_frame_duration(AVFormatContext *s, int *pnum, int *pden,
721 AVPacket *pkt)
722{
723 FFStream *const sti = ffstream(st);
724 AVRational codec_framerate = sti->avctx->framerate;
725 int frame_size, sample_rate;
726
727 *pnum = 0;
728 *pden = 0;
729 switch (st->codecpar->codec_type) {
731 if (st->r_frame_rate.num && (!pc || !codec_framerate.num)) {
732 *pnum = st->r_frame_rate.den;
733 *pden = st->r_frame_rate.num;
734 } else if ((s->iformat->flags & AVFMT_NOTIMESTAMPS) &&
735 !codec_framerate.num &&
737 *pnum = st->avg_frame_rate.den;
738 *pden = st->avg_frame_rate.num;
739 } else if (st->time_base.num * 1000LL > st->time_base.den) {
740 *pnum = st->time_base.num;
741 *pden = st->time_base.den;
742 } else if (codec_framerate.den * 1000LL > codec_framerate.num) {
743 int ticks_per_frame = (sti->codec_desc &&
744 (sti->codec_desc->props & AV_CODEC_PROP_FIELDS)) ? 2 : 1;
745 av_reduce(pnum, pden,
746 codec_framerate.den,
747 codec_framerate.num * (int64_t)ticks_per_frame,
748 INT_MAX);
749
750 if (pc && pc->repeat_pict) {
751 av_reduce(pnum, pden,
752 (*pnum) * (1LL + pc->repeat_pict),
753 (*pden),
754 INT_MAX);
755 }
756 /* If this codec can be interlaced or progressive then we need
757 * a parser to compute duration of a packet. Thus if we have
758 * no parser in such case leave duration undefined. */
759 if (sti->codec_desc &&
760 (sti->codec_desc->props & AV_CODEC_PROP_FIELDS) && !pc)
761 *pnum = *pden = 0;
762 }
763 break;
765 if (sti->avctx_inited) {
767 sample_rate = sti->avctx->sample_rate;
768 } else {
770 sample_rate = st->codecpar->sample_rate;
771 }
772 if (frame_size <= 0 || sample_rate <= 0)
773 break;
774 *pnum = frame_size;
775 *pden = sample_rate;
776 break;
777 default:
778 break;
779 }
780}
781
783{
784 FFStream *const sti = ffstream(st);
785 if (st->codecpar->codec_id != AV_CODEC_ID_H264) return 1;
786 if (!sti->info) // if we have left find_stream_info then nb_decoded_frames won't increase anymore for stream copy
787 return 1;
789#if CONFIG_H264_DECODER
790 if (sti->avctx->has_b_frames && avcodec_is_open(sti->avctx) &&
792 return 1;
793#endif
794 if (sti->avctx->has_b_frames < 3)
795 return sti->nb_decoded_frames >= 7;
796 else if (sti->avctx->has_b_frames < 4)
797 return sti->nb_decoded_frames >= 18;
798 else
799 return sti->nb_decoded_frames >= 20;
800}
801
803 PacketListEntry *pktl)
804{
806 FFFormatContext *const si = &fci->fc;
807 if (pktl->next)
808 return pktl->next;
809 if (pktl == si->packet_buffer.tail)
810 return fci->parse_queue.head;
811 return NULL;
812}
813
815{
816 FFStream *const sti = ffstream(st);
817 int onein_oneout = st->codecpar->codec_id != AV_CODEC_ID_H264 &&
820
821 if (!onein_oneout) {
822 int delay = sti->avctx->has_b_frames;
823
824 if (dts == AV_NOPTS_VALUE) {
825 int64_t best_score = INT64_MAX;
826 for (int i = 0; i < delay; i++) {
827 if (sti->pts_reorder_error_count[i]) {
829 if (score < best_score) {
830 best_score = score;
831 dts = pts_buffer[i];
832 }
833 }
834 }
835 } else {
836 for (int i = 0; i < delay; i++) {
837 if (pts_buffer[i] != AV_NOPTS_VALUE) {
838#define ABSDIFF(a,b) (((a) < (b)) ? (b) - (uint64_t)(a) : ((a) - (uint64_t)(b)))
839 uint64_t diff = ABSDIFF(pts_buffer[i], dts);
840
841 if (diff > INT64_MAX - sti->pts_reorder_error[i]) {
842 diff = INT64_MAX;
843 } else
844 diff += sti->pts_reorder_error[i];
845
846 sti->pts_reorder_error[i] = diff;
848 if (sti->pts_reorder_error_count[i] > 250) {
849 sti->pts_reorder_error[i] >>= 1;
850 sti->pts_reorder_error_count[i] >>= 1;
851 }
852 }
853 }
854 }
855 }
856
857 if (dts == AV_NOPTS_VALUE)
858 dts = pts_buffer[0];
859
860 return dts;
861}
862
863/**
864 * Updates the dts of packets of a stream in pkt_buffer, by re-ordering the pts
865 * of the packets in a window.
866 */
867static void update_dts_from_pts(AVFormatContext *s, int stream_index,
868 PacketListEntry *pkt_buffer)
869{
870 AVStream *const st = s->streams[stream_index];
871 int delay = ffstream(st)->avctx->has_b_frames;
872
873 int64_t pts_buffer[MAX_REORDER_DELAY+1];
874
875 for (int i = 0; i < MAX_REORDER_DELAY + 1; i++)
876 pts_buffer[i] = AV_NOPTS_VALUE;
877
878 for (; pkt_buffer; pkt_buffer = get_next_pkt(s, st, pkt_buffer)) {
879 if (pkt_buffer->pkt.stream_index != stream_index)
880 continue;
881
882 if (pkt_buffer->pkt.pts != AV_NOPTS_VALUE && delay <= MAX_REORDER_DELAY) {
883 pts_buffer[0] = pkt_buffer->pkt.pts;
884 for (int i = 0; i < delay && pts_buffer[i] > pts_buffer[i + 1]; i++)
885 FFSWAP(int64_t, pts_buffer[i], pts_buffer[i + 1]);
886
887 pkt_buffer->pkt.dts = select_from_pts_buffer(st, pts_buffer, pkt_buffer->pkt.dts);
888 }
889 }
890}
891
892static void update_initial_timestamps(AVFormatContext *s, int stream_index,
894{
896 FFFormatContext *const si = &fci->fc;
897 AVStream *const st = s->streams[stream_index];
898 FFStream *const sti = ffstream(st);
900
901 uint64_t shift;
902
903 if (sti->first_dts != AV_NOPTS_VALUE ||
904 dts == AV_NOPTS_VALUE ||
905 sti->cur_dts == AV_NOPTS_VALUE ||
906 sti->cur_dts < INT_MIN + RELATIVE_TS_BASE ||
907 dts < INT_MIN + (sti->cur_dts - RELATIVE_TS_BASE) ||
908 is_relative(dts))
909 return;
910
911 sti->first_dts = dts - (sti->cur_dts - RELATIVE_TS_BASE);
912 sti->cur_dts = dts;
913 shift = (uint64_t)sti->first_dts - RELATIVE_TS_BASE;
914
915 if (is_relative(pts))
916 pts += shift;
917
918 for (PacketListEntry *pktl_it = pktl; pktl_it; pktl_it = get_next_pkt(s, st, pktl_it)) {
919 if (pktl_it->pkt.stream_index != stream_index)
920 continue;
921 if (is_relative(pktl_it->pkt.pts))
922 pktl_it->pkt.pts += shift;
923
924 if (is_relative(pktl_it->pkt.dts))
925 pktl_it->pkt.dts += shift;
926
927 if (st->start_time == AV_NOPTS_VALUE && pktl_it->pkt.pts != AV_NOPTS_VALUE) {
928 st->start_time = pktl_it->pkt.pts;
930 st->start_time = av_sat_add64(st->start_time, av_rescale_q(sti->skip_samples, (AVRational){1, st->codecpar->sample_rate}, st->time_base));
931 }
932 }
933
935 update_dts_from_pts(s, stream_index, pktl);
936
937 if (st->start_time == AV_NOPTS_VALUE) {
938 if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO || !(pkt->flags & AV_PKT_FLAG_DISCARD)) {
939 st->start_time = pts;
940 }
942 st->start_time = av_sat_add64(st->start_time, av_rescale_q(sti->skip_samples, (AVRational){1, st->codecpar->sample_rate}, st->time_base));
943 }
944}
945
947 int stream_index, int64_t duration)
948{
950 FFFormatContext *const si = &fci->fc;
951 FFStream *const sti = ffstream(st);
953 int64_t cur_dts = RELATIVE_TS_BASE;
954
955 if (sti->first_dts != AV_NOPTS_VALUE) {
957 return;
959 cur_dts = sti->first_dts;
960 for (; pktl; pktl = get_next_pkt(s, st, pktl)) {
961 if (pktl->pkt.stream_index == stream_index) {
962 if (pktl->pkt.pts != pktl->pkt.dts ||
963 pktl->pkt.dts != AV_NOPTS_VALUE ||
964 pktl->pkt.duration)
965 break;
966 cur_dts -= duration;
967 }
968 }
969 if (pktl && pktl->pkt.dts != sti->first_dts) {
970 av_log(s, AV_LOG_DEBUG, "first_dts %s not matching first dts %s (pts %s, duration %"PRId64") in the queue\n",
971 av_ts2str(sti->first_dts), av_ts2str(pktl->pkt.dts), av_ts2str(pktl->pkt.pts), pktl->pkt.duration);
972 return;
973 }
974 if (!pktl) {
975 av_log(s, AV_LOG_DEBUG, "first_dts %s but no packet with dts in the queue\n", av_ts2str(sti->first_dts));
976 return;
977 }
978 pktl = si->packet_buffer.head ? si->packet_buffer.head : fci->parse_queue.head;
979 sti->first_dts = cur_dts;
980 } else if (sti->cur_dts != RELATIVE_TS_BASE)
981 return;
982
983 for (; pktl; pktl = get_next_pkt(s, st, pktl)) {
984 if (pktl->pkt.stream_index != stream_index)
985 continue;
986 if ((pktl->pkt.pts == pktl->pkt.dts ||
987 pktl->pkt.pts == AV_NOPTS_VALUE) &&
988 (pktl->pkt.dts == AV_NOPTS_VALUE ||
989 pktl->pkt.dts == sti->first_dts ||
990 pktl->pkt.dts == RELATIVE_TS_BASE) &&
991 !pktl->pkt.duration &&
992 av_sat_add64(cur_dts, duration) == cur_dts + (uint64_t)duration
993 ) {
994 pktl->pkt.dts = cur_dts;
995 if (!sti->avctx->has_b_frames)
996 pktl->pkt.pts = cur_dts;
997 pktl->pkt.duration = duration;
998 } else
999 break;
1000 cur_dts = pktl->pkt.dts + pktl->pkt.duration;
1001 }
1002 if (!pktl)
1003 sti->cur_dts = cur_dts;
1004}
1005
1008 int64_t next_dts, int64_t next_pts)
1009{
1011 FFFormatContext *const si = &fci->fc;
1012 FFStream *const sti = ffstream(st);
1013 int num, den, presentation_delayed, delay;
1016 int onein_oneout = st->codecpar->codec_id != AV_CODEC_ID_H264 &&
1020
1021 if (s->flags & AVFMT_FLAG_NOFILLIN)
1022 return;
1023
1024 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && pkt->dts != AV_NOPTS_VALUE) {
1025 if (pkt->dts == pkt->pts && sti->last_dts_for_order_check != AV_NOPTS_VALUE) {
1026 if (sti->last_dts_for_order_check <= pkt->dts) {
1027 sti->dts_ordered++;
1028 } else {
1030 "DTS %"PRIi64" < %"PRIi64" out of order\n",
1031 pkt->dts,
1033 sti->dts_misordered++;
1034 }
1035 if (sti->dts_ordered + sti->dts_misordered > 250) {
1036 sti->dts_ordered >>= 1;
1037 sti->dts_misordered >>= 1;
1038 }
1039 }
1040
1041 sti->last_dts_for_order_check = pkt->dts;
1042 if (sti->dts_ordered < 8 * sti->dts_misordered && pkt->dts == pkt->pts)
1043 pkt->dts = AV_NOPTS_VALUE;
1044 }
1045
1046 if ((s->flags & AVFMT_FLAG_IGNDTS) && pkt->pts != AV_NOPTS_VALUE)
1047 pkt->dts = AV_NOPTS_VALUE;
1048
1049 if (pc && pc->pict_type == AV_PICTURE_TYPE_B
1050 && !sti->avctx->has_b_frames)
1051 //FIXME Set low_delay = 0 when has_b_frames = 1
1052 sti->avctx->has_b_frames = 1;
1053
1054 /* do we have a video B-frame ? */
1055 delay = sti->avctx->has_b_frames;
1056 presentation_delayed = 0;
1057
1058 /* XXX: need has_b_frame, but cannot get it if the codec is
1059 * not initialized */
1060 if (delay &&
1061 pc && pc->pict_type != AV_PICTURE_TYPE_B)
1062 presentation_delayed = 1;
1063
1064 if (pkt->pts != AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE &&
1065 st->pts_wrap_bits < 63 && pkt->dts > INT64_MIN + (1LL << st->pts_wrap_bits) &&
1066 pkt->dts - (1LL << (st->pts_wrap_bits - 1)) > pkt->pts) {
1067 if (is_relative(sti->cur_dts) || pkt->dts - (1LL<<(st->pts_wrap_bits - 1)) > sti->cur_dts) {
1068 pkt->dts -= 1LL << st->pts_wrap_bits;
1069 } else
1070 pkt->pts += 1LL << st->pts_wrap_bits;
1071 }
1072
1073 /* Some MPEG-2 in MPEG-PS lack dts (issue #171 / input_file.mpg).
1074 * We take the conservative approach and discard both.
1075 * Note: If this is misbehaving for an H.264 file, then possibly
1076 * presentation_delayed is not set correctly. */
1077 if (delay == 1 && pkt->dts == pkt->pts &&
1078 pkt->dts != AV_NOPTS_VALUE && presentation_delayed) {
1079 av_log(s, AV_LOG_DEBUG, "invalid dts/pts combination %"PRIi64"\n", pkt->dts);
1080 if ( strcmp(s->iformat->name, "mov,mp4,m4a,3gp,3g2,mj2")
1081 && strcmp(s->iformat->name, "flv")) // otherwise we discard correct timestamps for vc1-wmapro.ism
1082 pkt->dts = AV_NOPTS_VALUE;
1083 }
1084
1085 duration = av_mul_q((AVRational) {pkt->duration, 1}, st->time_base);
1086 if (pkt->duration <= 0) {
1087 compute_frame_duration(s, &num, &den, st, pc, pkt);
1088 if (den && num) {
1089 duration = (AVRational) {num, den};
1090 pkt->duration = av_rescale_rnd(1,
1091 num * (int64_t) st->time_base.den,
1092 den * (int64_t) st->time_base.num,
1094 }
1095 }
1096
1097 if (pkt->duration > 0 && (si->packet_buffer.head || fci->parse_queue.head))
1098 update_initial_durations(s, st, pkt->stream_index, pkt->duration);
1099
1100 /* Correct timestamps with byte offset if demuxers only have timestamps
1101 * on packet boundaries */
1102 if (pc && sti->need_parsing == AVSTREAM_PARSE_TIMESTAMPS && pkt->size) {
1103 /* this will estimate bitrate based on this frame's duration and size */
1104 offset = av_rescale(pc->offset, pkt->duration, pkt->size);
1105 if (pkt->pts != AV_NOPTS_VALUE)
1106 pkt->pts += offset;
1107 if (pkt->dts != AV_NOPTS_VALUE)
1108 pkt->dts += offset;
1109 }
1110
1111 /* This may be redundant, but it should not hurt. */
1112 if (pkt->dts != AV_NOPTS_VALUE &&
1113 pkt->pts != AV_NOPTS_VALUE &&
1114 pkt->pts > pkt->dts)
1115 presentation_delayed = 1;
1116
1117 if (s->debug & AV_FDEBUG_TS)
1119 "IN delayed:%d pts:%s, dts:%s cur_dts:%s st:%d pc:%p duration:%"PRId64" delay:%d onein_oneout:%d\n",
1120 presentation_delayed, av_ts2str(pkt->pts), av_ts2str(pkt->dts), av_ts2str(sti->cur_dts),
1121 pkt->stream_index, pc, pkt->duration, delay, onein_oneout);
1122
1123 /* Interpolate PTS and DTS if they are not present. We skip H264
1124 * currently because delay and has_b_frames are not reliably set. */
1125 if ((delay == 0 || (delay == 1 && pc)) &&
1126 onein_oneout) {
1127 if (presentation_delayed) {
1128 /* DTS = decompression timestamp */
1129 /* PTS = presentation timestamp */
1130 if (pkt->dts == AV_NOPTS_VALUE)
1131 pkt->dts = sti->last_IP_pts;
1132 update_initial_timestamps(s, pkt->stream_index, pkt->dts, pkt->pts, pkt);
1133 if (pkt->dts == AV_NOPTS_VALUE)
1134 pkt->dts = sti->cur_dts;
1135
1136 /* This is tricky: the dts must be incremented by the duration
1137 * of the frame we are displaying, i.e. the last I- or P-frame. */
1138 if (sti->last_IP_duration == 0 && (uint64_t)pkt->duration <= INT32_MAX)
1139 sti->last_IP_duration = pkt->duration;
1140 if (pkt->dts != AV_NOPTS_VALUE)
1141 sti->cur_dts = av_sat_add64(pkt->dts, sti->last_IP_duration);
1142 if (pkt->dts != AV_NOPTS_VALUE &&
1143 pkt->pts == AV_NOPTS_VALUE &&
1144 sti->last_IP_duration > 0 &&
1145 ((uint64_t)sti->cur_dts - (uint64_t)next_dts + 1) <= 2 &&
1146 next_dts != next_pts &&
1147 next_pts != AV_NOPTS_VALUE)
1148 pkt->pts = next_dts;
1149
1150 if ((uint64_t)pkt->duration <= INT32_MAX)
1151 sti->last_IP_duration = pkt->duration;
1152 sti->last_IP_pts = pkt->pts;
1153 /* Cannot compute PTS if not present (we can compute it only
1154 * by knowing the future. */
1155 } else if (pkt->pts != AV_NOPTS_VALUE ||
1156 pkt->dts != AV_NOPTS_VALUE ||
1157 pkt->duration > 0 ) {
1158
1159 /* presentation is not delayed : PTS and DTS are the same */
1160 if (pkt->pts == AV_NOPTS_VALUE)
1161 pkt->pts = pkt->dts;
1162 update_initial_timestamps(s, pkt->stream_index, pkt->pts,
1163 pkt->pts, pkt);
1164 if (pkt->pts == AV_NOPTS_VALUE)
1165 pkt->pts = sti->cur_dts;
1166 pkt->dts = pkt->pts;
1167 if (pkt->pts != AV_NOPTS_VALUE && duration.num >= 0)
1168 sti->cur_dts = av_add_stable(st->time_base, pkt->pts, duration, 1);
1169 }
1170 }
1171
1172 if (pkt->pts != AV_NOPTS_VALUE && delay <= MAX_REORDER_DELAY) {
1173 sti->pts_buffer[0] = pkt->pts;
1174 for (int i = 0; i < delay && sti->pts_buffer[i] > sti->pts_buffer[i + 1]; i++)
1175 FFSWAP(int64_t, sti->pts_buffer[i], sti->pts_buffer[i + 1]);
1176
1178 pkt->dts = select_from_pts_buffer(st, sti->pts_buffer, pkt->dts);
1179 }
1180 // We skipped it above so we try here.
1181 if (!onein_oneout)
1182 // This should happen on the first packet
1183 update_initial_timestamps(s, pkt->stream_index, pkt->dts, pkt->pts, pkt);
1184 if (pkt->dts > sti->cur_dts)
1185 sti->cur_dts = pkt->dts;
1186
1187 if (s->debug & AV_FDEBUG_TS)
1188 av_log(s, AV_LOG_DEBUG, "OUTdelayed:%d/%d pts:%s, dts:%s cur_dts:%s st:%d (%d)\n",
1189 presentation_delayed, delay, av_ts2str(pkt->pts), av_ts2str(pkt->dts), av_ts2str(sti->cur_dts), st->index, st->id);
1190
1191 /* update flags */
1193 pkt->flags |= AV_PKT_FLAG_KEY;
1194}
1195
1196/**
1197 * Parse a packet, add all split parts to parse_queue.
1198 *
1199 * @param pkt Packet to parse; must not be NULL.
1200 * @param flush Indicates whether to flush. If set, pkt must be blank.
1201 */
1203 int stream_index, int flush)
1204{
1206 AVStream *st = s->streams[stream_index];
1207 FFStream *const sti = ffstream(st);
1208 AVPacket *out_pkt = sti->parse_pkt;
1209 const AVPacketSideData *sd = NULL;
1210 const uint8_t *data = pkt->data;
1211 uint8_t *extradata = sti->avctx->extradata;
1212 int extradata_size = sti->avctx->extradata_size;
1213 int size = pkt->size;
1214 int ret = 0, got_output = flush, pkt_side_data_consumed = 0;
1215
1216 if (!size && !flush && sti->parser->flags & PARSER_FLAG_COMPLETE_FRAMES) {
1217 // preserve 0-size sync packets
1218 compute_pkt_fields(s, st, sti->parser, pkt, pkt->dts, pkt->pts);
1219
1220 // Theora has valid 0-sized packets that need to be output
1221 if (st->codecpar->codec_id == AV_CODEC_ID_THEORA) {
1223 pkt, NULL, 0);
1224 if (ret < 0)
1225 goto fail;
1226 }
1227 }
1228
1229 if (pkt->side_data_elems)
1230 sd = av_packet_side_data_get(pkt->side_data, pkt->side_data_elems,
1232 if (sd) {
1233 av_assert1(size && !flush);
1234
1235 sti->avctx->extradata = sd->data;
1236 sti->avctx->extradata_size = sd->size;
1237 }
1238
1239 while (size > 0 || (flush && got_output)) {
1240 int64_t next_pts = pkt->pts;
1241 int64_t next_dts = pkt->dts;
1242 int len;
1243
1244 len = av_parser_parse2(sti->parser, sti->avctx,
1245 &out_pkt->data, &out_pkt->size, data, size,
1246 pkt->pts, pkt->dts, pkt->pos);
1247
1248 pkt->pts = pkt->dts = AV_NOPTS_VALUE;
1249 pkt->pos = -1;
1250 /* increment read pointer */
1251 av_assert1(data || !len);
1252 data = len ? data + len : data;
1253 size -= len;
1254
1255 got_output = !!out_pkt->size;
1256
1257 if (pkt->side_data && !out_pkt->side_data) {
1258 /* for the first iteration, side_data are simply moved to output.
1259 * in case of additional iterations, they are duplicated each time. */
1260 if (!pkt_side_data_consumed) {
1261 pkt_side_data_consumed = 1;
1262 out_pkt->side_data = pkt->side_data;
1263 out_pkt->side_data_elems = pkt->side_data_elems;
1264 } else for (int i = 0; i < pkt->side_data_elems; i++) {
1265 const AVPacketSideData *const src_sd = &pkt->side_data[i];
1266 uint8_t *dst_data = av_packet_new_side_data(out_pkt, src_sd->type, src_sd->size);
1267 if (!dst_data) {
1268 ret = AVERROR(ENOMEM);
1269 goto fail;
1270 }
1271 memcpy(dst_data, src_sd->data, src_sd->size);
1272 }
1273 }
1274
1275 if (!out_pkt->size)
1276 continue;
1277
1278 if (pkt->buf && out_pkt->data == pkt->data) {
1279 /* reference pkt->buf only when out_pkt->data is guaranteed to point
1280 * to data in it and not in the parser's internal buffer. */
1281 /* XXX: Ensure this is the case with all parsers when sti->parser->flags
1282 * is PARSER_FLAG_COMPLETE_FRAMES and check for that instead? */
1283 out_pkt->buf = av_buffer_ref(pkt->buf);
1284 if (!out_pkt->buf) {
1285 ret = AVERROR(ENOMEM);
1286 goto fail;
1287 }
1288 } else {
1289 ret = av_packet_make_refcounted(out_pkt);
1290 if (ret < 0)
1291 goto fail;
1292 }
1293
1294 /* set the duration */
1295 out_pkt->duration = (sti->parser->flags & PARSER_FLAG_COMPLETE_FRAMES) ? pkt->duration : 0;
1297 if (sti->avctx->sample_rate > 0 && !out_pkt->duration && sti->parser->duration > 0) {
1298 out_pkt->duration =
1300 (AVRational) { 1, sti->avctx->sample_rate },
1301 st->time_base,
1303 }
1304 } else if (st->codecpar->codec_id == AV_CODEC_ID_GIF) {
1305 if (st->time_base.num > 0 && st->time_base.den > 0 &&
1306 sti->parser->duration) {
1307 out_pkt->duration = sti->parser->duration;
1308 }
1309 }
1310
1311 out_pkt->stream_index = st->index;
1312 out_pkt->pts = sti->parser->pts;
1313 out_pkt->dts = sti->parser->dts;
1314 out_pkt->pos = sti->parser->pos;
1315 out_pkt->flags |= pkt->flags & (AV_PKT_FLAG_DISCARD | AV_PKT_FLAG_CORRUPT);
1316
1318 out_pkt->pos = sti->parser->frame_offset;
1319
1320 if (sti->parser->key_frame == 1 ||
1321 (sti->parser->key_frame == -1 &&
1323 out_pkt->flags |= AV_PKT_FLAG_KEY;
1324
1325 if (sti->parser->key_frame == -1 && sti->parser->pict_type ==AV_PICTURE_TYPE_NONE && (pkt->flags&AV_PKT_FLAG_KEY))
1326 out_pkt->flags |= AV_PKT_FLAG_KEY;
1327
1328 compute_pkt_fields(s, st, sti->parser, out_pkt, next_dts, next_pts);
1329
1331 out_pkt, NULL, 0);
1332 if (ret < 0)
1333 goto fail;
1334 }
1335
1336 /* end of the stream => close and free the parser */
1337 if (flush) {
1338 av_parser_close(sti->parser);
1339 sti->parser = NULL;
1340 }
1341
1342fail:
1343 if (sd) {
1344 sti->avctx->extradata = extradata;
1345 sti->avctx->extradata_size = extradata_size;
1346 }
1347 if (pkt_side_data_consumed) {
1348 pkt->side_data = NULL;
1349 pkt->side_data_elems = 0;
1350 }
1351
1352 if (ret < 0)
1353 av_packet_unref(out_pkt);
1355 return ret;
1356}
1357
1359{
1360 return av_rescale(ts, st->time_base.num * st->codecpar->sample_rate, st->time_base.den);
1361}
1362
1364{
1365 AVCodecContext *avctx_new = NULL;
1366 AVCodecParameters *par_tmp = NULL;
1367 const AVCodec *new_codec = NULL;
1368 int ret;
1369
1370 new_codec =
1371 (sti->avctx->codec_id != sti->pub.codecpar->codec_id) ?
1372 find_probe_decoder(s, &sti->pub, sti->pub.codecpar->codec_id) :
1373 sti->avctx->codec;
1374
1375 avctx_new = avcodec_alloc_context3(new_codec);
1376 if (!avctx_new) {
1377 ret = AVERROR(ENOMEM);
1378 goto fail;
1379 }
1380
1381 par_tmp = avcodec_parameters_alloc();
1382 if (!par_tmp) {
1383 ret = AVERROR(ENOMEM);
1384 goto fail;
1385 }
1386
1387 ret = avcodec_parameters_from_context(par_tmp, sti->avctx);
1388 if (ret < 0)
1389 goto fail;
1390
1391 ret = avcodec_parameters_to_context(avctx_new, par_tmp);
1392 if (ret < 0)
1393 goto fail;
1394
1395 avctx_new->pkt_timebase = sti->avctx->pkt_timebase;
1396
1398 sti->avctx = avctx_new;
1399
1400 avctx_new = NULL;
1401 ret = 0;
1402
1403fail:
1404 avcodec_free_context(&avctx_new);
1405 avcodec_parameters_free(&par_tmp);
1406
1407 return ret;
1408}
1409
1410static int extract_extradata(FFFormatContext *si, AVStream *st, const AVPacket *pkt);
1411
1413{
1415 FFFormatContext *const si = &fci->fc;
1416 int ret, got_packet = 0;
1418
1419 while (!got_packet && !fci->parse_queue.head) {
1420 AVStream *st;
1421 FFStream *sti;
1422
1423 /* read next packet */
1424 ret = ff_read_packet(s, pkt);
1425 if (ret < 0) {
1426 if (ret == AVERROR(EAGAIN))
1427 return ret;
1428 /* flush the parsers */
1429 for (unsigned i = 0; i < s->nb_streams; i++) {
1430 AVStream *const st = s->streams[i];
1431 FFStream *const sti = ffstream(st);
1432 if (sti->parser && sti->need_parsing)
1433 parse_packet(s, pkt, st->index, 1);
1434 }
1435 /* all remaining packets are now in parse_queue =>
1436 * really terminate parsing */
1437 break;
1438 }
1439 ret = 0;
1440 st = s->streams[pkt->stream_index];
1441 sti = ffstream(st);
1442
1444
1445 int new_extradata = !!av_packet_side_data_get(pkt->side_data, pkt->side_data_elems,
1447 if (new_extradata)
1448 sti->need_context_update = 1;
1449
1450 /* update context if required */
1451 if (sti->need_context_update) {
1452 if (avcodec_is_open(sti->avctx)) {
1453 av_log(s, AV_LOG_DEBUG, "Demuxer context update while decoder is open, closing and trying to re-open\n");
1454 ret = codec_close(s, sti);
1455 sti->info->found_decoder = 0;
1456 if (ret < 0)
1457 return ret;
1458 }
1459
1460 /* close parser, because it depends on the codec and extradata */
1461 if (sti->parser &&
1462 (sti->avctx->codec_id != st->codecpar->codec_id || new_extradata)) {
1463 av_parser_close(sti->parser);
1464 sti->parser = NULL;
1465 }
1466
1468 if (ret < 0) {
1470 return ret;
1471 }
1472
1473 if (!sti->avctx->extradata) {
1474 sti->extract_extradata.inited = 0;
1475
1476 ret = extract_extradata(si, st, pkt);
1477 if (ret < 0) {
1479 return ret;
1480 }
1481 }
1482
1484
1485 sti->need_context_update = 0;
1486 }
1487
1488 if (pkt->pts != AV_NOPTS_VALUE &&
1489 pkt->dts != AV_NOPTS_VALUE &&
1490 pkt->pts < pkt->dts) {
1492 "Invalid timestamps stream=%d, pts=%s, dts=%s, size=%d\n",
1493 pkt->stream_index,
1494 av_ts2str(pkt->pts),
1495 av_ts2str(pkt->dts),
1496 pkt->size);
1497 }
1498 if (s->debug & AV_FDEBUG_TS)
1500 "ff_read_packet stream=%d, pts=%s, dts=%s, size=%d, duration=%"PRId64", flags=%d\n",
1501 pkt->stream_index,
1502 av_ts2str(pkt->pts),
1503 av_ts2str(pkt->dts),
1504 pkt->size, pkt->duration, pkt->flags);
1505
1506 if (sti->need_parsing && !sti->parser && !(s->flags & AVFMT_FLAG_NOPARSE)) {
1508 if (!sti->parser) {
1509 av_log(s, AV_LOG_VERBOSE, "parser not found for codec "
1510 "%s, packets or times may be invalid.\n",
1512 /* no parser available: just output the raw packets */
1514 } else if (sti->need_parsing == AVSTREAM_PARSE_HEADERS)
1516 else if (sti->need_parsing == AVSTREAM_PARSE_FULL_ONCE)
1517 sti->parser->flags |= PARSER_FLAG_ONCE;
1518 else if (sti->need_parsing == AVSTREAM_PARSE_FULL_RAW)
1520 }
1521
1522 if (!sti->need_parsing || !sti->parser) {
1523 /* no parsing needed: we just output the packet as is */
1525 if ((s->iformat->flags & AVFMT_GENERIC_INDEX) &&
1526 (pkt->flags & AV_PKT_FLAG_KEY) && pkt->dts != AV_NOPTS_VALUE) {
1527 ff_reduce_index(s, st->index);
1528 av_add_index_entry(st, pkt->pos, pkt->dts,
1529 0, 0, AVINDEX_KEYFRAME);
1530 }
1531 got_packet = 1;
1532 } else if (st->discard < AVDISCARD_ALL) {
1533 if ((ret = parse_packet(s, pkt, pkt->stream_index, 0)) < 0)
1534 return ret;
1536 st->codecpar->bit_rate = sti->avctx->bit_rate;
1538 if (ret < 0)
1539 return ret;
1540 st->codecpar->codec_id = sti->avctx->codec_id;
1541 } else {
1542 /* free packet */
1544 }
1545 if (pkt->flags & AV_PKT_FLAG_KEY)
1546 sti->skip_to_keyframe = 0;
1547 if (sti->skip_to_keyframe) {
1549 got_packet = 0;
1550 }
1551 }
1552
1553 if (!got_packet && fci->parse_queue.head)
1555
1556 if (ret >= 0) {
1557 AVStream *const st = s->streams[pkt->stream_index];
1558 FFStream *const sti = ffstream(st);
1559 int discard_padding = 0;
1560 if (sti->first_discard_sample && pkt->pts != AV_NOPTS_VALUE) {
1561 int64_t pts = pkt->pts - (is_relative(pkt->pts) ? RELATIVE_TS_BASE : 0);
1563 int64_t duration = FFMAX(st->codecpar->frame_size, ts_to_samples(st, pkt->duration));
1564 int64_t end_sample = sample + duration;
1565 if (duration > 0 && end_sample >= sti->first_discard_sample &&
1566 sample < sti->last_discard_sample)
1567 discard_padding = FFMIN(end_sample - sti->first_discard_sample, duration);
1568 }
1569 if (sti->seek_discard) {
1570 const AVCodecParameters *par = st->codecpar;
1571
1572 if (par->codec_type == AVMEDIA_TYPE_AUDIO && par->sample_rate > 0) {
1573 int first = pkt->dts != AV_NOPTS_VALUE && sti->first_dts != AV_NOPTS_VALUE &&
1574 pkt->dts <= sti->first_dts;
1575 int in_priming = pkt->pts != AV_NOPTS_VALUE &&
1576 st->start_time != AV_NOPTS_VALUE &&
1577 pkt->pts < st->start_time;
1578 int64_t skip = 0;
1579
1580 // The priming ends at the start time.
1581 if (in_priming)
1582 skip = ts_to_samples(st, st->start_time - pkt->pts);
1583 if (first) {
1584 if (par->initial_padding > 0)
1585 skip = par->initial_padding;
1586 } else if (sti->seek_preroll &&
1587 !(sti->seek_to_start && pkt->pts != AV_NOPTS_VALUE && !in_priming)) {
1588 // Past the first packet, the decoder also misses what
1589 // came before.
1590 skip = FFMAX(skip, par->seek_preroll);
1591 }
1592 sti->skip_samples = FFMAX(sti->skip_samples, FFMIN(skip, INT_MAX));
1593 }
1594 sti->seek_discard = 0;
1595 }
1596 if (sti->start_skip_samples && (pkt->pts == 0 || pkt->pts == RELATIVE_TS_BASE))
1597 sti->skip_samples = sti->start_skip_samples;
1598 sti->skip_samples = FFMAX(0, sti->skip_samples);
1599 if (sti->skip_samples || discard_padding) {
1600 size_t size;
1602 // Keep what the demuxer set, such as the padding at the end.
1603 if (p && size >= 10) {
1604 sti->skip_samples = FFMAX(sti->skip_samples, (int)AV_RL32(p));
1605 discard_padding = FFMAX(discard_padding, AV_RL32(p + 4));
1606 } else
1608 if (p) {
1609 AV_WL32(p, sti->skip_samples);
1610 AV_WL32(p + 4, discard_padding);
1611 av_log(s, AV_LOG_DEBUG, "demuxer injecting skip %u / discard %u\n",
1612 (unsigned)sti->skip_samples, (unsigned)discard_padding);
1613 }
1614 sti->skip_samples = 0;
1615 }
1616 }
1617
1618 if (!fci->metafree) {
1619 int metaret = av_opt_get_dict_val(s, "metadata", AV_OPT_SEARCH_CHILDREN, &metadata);
1620 if (metadata) {
1621 s->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
1622 av_dict_copy(&s->metadata, metadata, 0);
1625 }
1626 fci->metafree = metaret == AVERROR_OPTION_NOT_FOUND;
1627 }
1628
1629 if (s->debug & AV_FDEBUG_TS)
1631 "read_frame_internal stream=%d, pts=%s, dts=%s, "
1632 "size=%d, duration=%"PRId64", flags=%d\n",
1633 pkt->stream_index,
1634 av_ts2str(pkt->pts),
1635 av_ts2str(pkt->dts),
1636 pkt->size, pkt->duration, pkt->flags);
1637
1638 /* A demuxer might have returned EOF because of an IO error, let's
1639 * propagate this back to the user. */
1640 if (ret == AVERROR_EOF && s->pb && s->pb->error < 0 && s->pb->error != AVERROR(EAGAIN))
1641 ret = s->pb->error;
1642
1643 return ret;
1644}
1645
1647{
1648 FFFormatContext *const si = ffformatcontext(s);
1649 const int genpts = s->flags & AVFMT_FLAG_GENPTS;
1650 int eof = 0;
1651 int ret;
1652 AVStream *st;
1653
1654 if (!genpts) {
1655 ret = si->packet_buffer.head
1658 if (ret < 0)
1659 return ret;
1660 goto return_packet;
1661 }
1662
1663 for (;;) {
1665
1666 if (pktl) {
1667 AVPacket *next_pkt = &pktl->pkt;
1668
1669 if (next_pkt->dts != AV_NOPTS_VALUE) {
1670 int wrap_bits = s->streams[next_pkt->stream_index]->pts_wrap_bits;
1671 // last dts seen for this stream. if any of packets following
1672 // current one had no dts, we will set this to AV_NOPTS_VALUE.
1673 int64_t last_dts = next_pkt->dts;
1674 av_assert2(wrap_bits <= 64);
1675 while (pktl && next_pkt->pts == AV_NOPTS_VALUE) {
1676 if (pktl->pkt.stream_index == next_pkt->stream_index &&
1677 av_compare_mod(next_pkt->dts, pktl->pkt.dts, 2ULL << (wrap_bits - 1)) < 0) {
1678 if (av_compare_mod(pktl->pkt.pts, pktl->pkt.dts, 2ULL << (wrap_bits - 1))) {
1679 // not B-frame
1680 next_pkt->pts = pktl->pkt.dts;
1681 }
1682 if (last_dts != AV_NOPTS_VALUE) {
1683 // Once last dts was set to AV_NOPTS_VALUE, we don't change it.
1684 last_dts = pktl->pkt.dts;
1685 }
1686 }
1687 pktl = pktl->next;
1688 }
1689 if (eof && next_pkt->pts == AV_NOPTS_VALUE && last_dts != AV_NOPTS_VALUE) {
1690 // Fixing the last reference frame had none pts issue (For MXF etc).
1691 // We only do this when
1692 // 1. eof.
1693 // 2. we are not able to resolve a pts value for current packet.
1694 // 3. the packets for this stream at the end of the files had valid dts.
1695 next_pkt->pts = last_dts + next_pkt->duration;
1696 }
1697 pktl = si->packet_buffer.head;
1698 }
1699
1700 /* read packet from packet buffer, if there is data */
1701 st = s->streams[next_pkt->stream_index];
1702 if (!(next_pkt->pts == AV_NOPTS_VALUE && st->discard < AVDISCARD_ALL &&
1703 next_pkt->dts != AV_NOPTS_VALUE && !eof)) {
1705 goto return_packet;
1706 }
1707 }
1708
1710 if (ret < 0) {
1711 if (pktl && ret != AVERROR(EAGAIN)) {
1712 eof = 1;
1713 continue;
1714 } else
1715 return ret;
1716 }
1717
1719 pkt, NULL, 0);
1720 if (ret < 0) {
1722 return ret;
1723 }
1724 }
1725
1726return_packet:
1727 st = s->streams[pkt->stream_index];
1728 if ((s->iformat->flags & AVFMT_GENERIC_INDEX) && pkt->flags & AV_PKT_FLAG_KEY) {
1729 ff_reduce_index(s, st->index);
1730 av_add_index_entry(st, pkt->pos, pkt->dts, 0, 0, AVINDEX_KEYFRAME);
1731 }
1732
1733 if (is_relative(pkt->dts))
1734 pkt->dts -= RELATIVE_TS_BASE;
1735 if (is_relative(pkt->pts))
1736 pkt->pts -= RELATIVE_TS_BASE;
1737
1738 return ret;
1739}
1740
1741/**
1742 * Return TRUE if the stream has accurate duration in any stream.
1743 *
1744 * @return TRUE if the stream has accurate duration for at least one component.
1745 */
1747{
1748 for (unsigned i = 0; i < ic->nb_streams; i++) {
1749 const AVStream *const st = ic->streams[i];
1750 if (st->duration != AV_NOPTS_VALUE)
1751 return 1;
1752 }
1753 if (ic->duration != AV_NOPTS_VALUE)
1754 return 1;
1755 return 0;
1756}
1757
1758/**
1759 * Estimate the stream timings from the one of each components.
1760 *
1761 * Also computes the global bitrate if possible.
1762 */
1764{
1765 int64_t start_time, start_time1, start_time_text, end_time, end_time1, end_time_text;
1766 int64_t duration, duration1, duration_text, filesize;
1767
1768 start_time = INT64_MAX;
1769 start_time_text = INT64_MAX;
1770 end_time = INT64_MIN;
1771 end_time_text = INT64_MIN;
1772 duration = INT64_MIN;
1773 duration_text = INT64_MIN;
1774
1775 for (unsigned i = 0; i < ic->nb_streams; i++) {
1776 AVStream *const st = ic->streams[i];
1777 int is_text = st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE ||
1779
1780 if (st->start_time != AV_NOPTS_VALUE && st->time_base.den) {
1781 start_time1 = av_rescale_q(st->start_time, st->time_base,
1783 if (is_text)
1784 start_time_text = FFMIN(start_time_text, start_time1);
1785 else
1786 start_time = FFMIN(start_time, start_time1);
1787 end_time1 = av_rescale_q_rnd(st->duration, st->time_base,
1790 if (end_time1 != AV_NOPTS_VALUE && (end_time1 > 0 ? start_time1 <= INT64_MAX - end_time1 : start_time1 >= INT64_MIN - end_time1)) {
1791 end_time1 += start_time1;
1792 if (is_text)
1793 end_time_text = FFMAX(end_time_text, end_time1);
1794 else
1795 end_time = FFMAX(end_time, end_time1);
1796 }
1797 for (AVProgram *p = NULL; (p = av_find_program_from_stream(ic, p, i)); ) {
1798 if (p->start_time == AV_NOPTS_VALUE || p->start_time > start_time1)
1799 p->start_time = start_time1;
1800 if (p->end_time < end_time1)
1801 p->end_time = end_time1;
1802 }
1803 }
1804 if (st->duration != AV_NOPTS_VALUE) {
1805 duration1 = av_rescale_q(st->duration, st->time_base,
1807 if (is_text)
1808 duration_text = FFMAX(duration_text, duration1);
1809 else
1810 duration = FFMAX(duration, duration1);
1811 }
1812 }
1813 if (start_time == INT64_MAX || (start_time > start_time_text && start_time - (uint64_t)start_time_text < AV_TIME_BASE))
1814 start_time = start_time_text;
1815 else if (start_time > start_time_text)
1816 av_log(ic, AV_LOG_VERBOSE, "Ignoring outlier non primary stream starttime %f\n", start_time_text / (float)AV_TIME_BASE);
1817
1818 if (end_time == INT64_MIN || (end_time < end_time_text && end_time_text - (uint64_t)end_time < AV_TIME_BASE))
1819 end_time = end_time_text;
1820 else if (end_time < end_time_text)
1821 av_log(ic, AV_LOG_VERBOSE, "Ignoring outlier non primary stream endtime %f\n", end_time_text / (float)AV_TIME_BASE);
1822
1823 if (duration == INT64_MIN || (duration < duration_text && (uint64_t)duration_text - duration < AV_TIME_BASE))
1824 duration = duration_text;
1825 else if (duration < duration_text)
1826 av_log(ic, AV_LOG_VERBOSE, "Ignoring outlier non primary stream duration %f\n", duration_text / (float)AV_TIME_BASE);
1827
1828 if (start_time != INT64_MAX) {
1829 ic->start_time = start_time;
1830 if (end_time != INT64_MIN) {
1831 if (ic->nb_programs > 1) {
1832 for (unsigned i = 0; i < ic->nb_programs; i++) {
1833 AVProgram *const p = ic->programs[i];
1834
1835 if (p->start_time != AV_NOPTS_VALUE &&
1836 p->end_time > p->start_time &&
1837 p->end_time - (uint64_t)p->start_time <= INT64_MAX)
1838 duration = FFMAX(duration, p->end_time - p->start_time);
1839 }
1840 } else if (end_time >= start_time && end_time - (uint64_t)start_time <= INT64_MAX) {
1841 duration = FFMAX(duration, end_time - start_time);
1842 }
1843 }
1844 }
1845 if (duration != INT64_MIN && duration > 0 && ic->duration == AV_NOPTS_VALUE) {
1846 ic->duration = duration;
1847 }
1848 if (ic->pb && (filesize = avio_size(ic->pb)) > 0 && ic->duration > 0) {
1849 /* compute the bitrate */
1850 double bitrate = (double) filesize * 8.0 * AV_TIME_BASE /
1851 (double) ic->duration;
1852 if (bitrate >= 0 && bitrate <= INT64_MAX)
1853 ic->bit_rate = bitrate;
1854 }
1855}
1856
1858{
1860 for (unsigned i = 0; i < ic->nb_streams; i++) {
1861 AVStream *const st = ic->streams[i];
1862
1863 if (st->start_time == AV_NOPTS_VALUE) {
1864 if (ic->start_time != AV_NOPTS_VALUE)
1866 st->time_base);
1867 if (ic->duration != AV_NOPTS_VALUE)
1869 st->time_base);
1870 }
1871 }
1872}
1873
1875{
1876 FFFormatContext *const si = ffformatcontext(ic);
1877 int show_warning = 0;
1878
1879 /* if bit_rate is already set, we believe it */
1880 if (ic->bit_rate <= 0) {
1881 int64_t bit_rate = 0;
1882 for (unsigned i = 0; i < ic->nb_streams; i++) {
1883 const AVStream *const st = ic->streams[i];
1884 const FFStream *const sti = cffstream(st);
1885 if (st->codecpar->bit_rate <= 0 && sti->avctx->bit_rate > 0)
1886 st->codecpar->bit_rate = sti->avctx->bit_rate;
1887 if (st->codecpar->bit_rate > 0) {
1888 if (INT64_MAX - st->codecpar->bit_rate < bit_rate) {
1889 bit_rate = 0;
1890 break;
1891 }
1892 bit_rate += st->codecpar->bit_rate;
1893 } else if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && sti->codec_info_nb_frames > 1) {
1894 // If we have a videostream with packets but without a bitrate
1895 // then consider the sum not known
1896 bit_rate = 0;
1897 break;
1898 }
1899 }
1900 ic->bit_rate = bit_rate;
1901 }
1902
1903 /* if duration is already set, we believe it */
1904 if (ic->duration == AV_NOPTS_VALUE &&
1905 ic->bit_rate != 0) {
1906 int64_t filesize = ic->pb ? avio_size(ic->pb) : 0;
1907 if (filesize > si->data_offset) {
1908 filesize -= si->data_offset;
1909 for (unsigned i = 0; i < ic->nb_streams; i++) {
1910 AVStream *const st = ic->streams[i];
1911
1912 if ( st->time_base.num <= INT64_MAX / ic->bit_rate
1913 && st->duration == AV_NOPTS_VALUE) {
1914 st->duration = av_rescale(filesize, 8LL * st->time_base.den,
1915 ic->bit_rate *
1916 (int64_t) st->time_base.num);
1917 show_warning = 1;
1918 }
1919 }
1920 }
1921 }
1922 if (show_warning)
1924 "Estimating duration from bitrate, this may be inaccurate\n");
1925}
1926
1927#define DURATION_DEFAULT_MAX_READ_SIZE 250000LL
1928#define DURATION_DEFAULT_MAX_RETRY 6
1929#define DURATION_MAX_RETRY 1
1930
1931/* only usable for MPEG-PS streams */
1933{
1934 FFFormatContext *const si = ffformatcontext(ic);
1935 AVPacket *const pkt = si->pkt;
1936 int num, den, read_size, ret;
1938 int duration_max_retry = ic->duration_probesize ? DURATION_MAX_RETRY : DURATION_DEFAULT_MAX_RETRY;
1939 int found_duration = 0;
1940 int is_end;
1942 int retry = 0;
1943
1944 /* flush packet queue */
1946
1947 for (unsigned i = 0; i < ic->nb_streams; i++) {
1948 AVStream *const st = ic->streams[i];
1949 FFStream *const sti = ffstream(st);
1950
1951 if (st->start_time == AV_NOPTS_VALUE &&
1952 sti->first_dts == AV_NOPTS_VALUE &&
1955 "start time for stream %d is not set in estimate_timings_from_pts\n", i);
1956
1957 if (sti->parser) {
1958 av_parser_close(sti->parser);
1959 sti->parser = NULL;
1960 }
1961 }
1962
1964 av_log(ic, AV_LOG_INFO, "Skipping duration calculation in estimate_timings_from_pts\n");
1965 goto skip_duration_calc;
1966 }
1967
1968 av_opt_set_int(ic, "skip_changes", 1, AV_OPT_SEARCH_CHILDREN);
1969 /* estimate the end time (duration) */
1970 /* XXX: may need to support wrapping */
1971 filesize = ic->pb ? avio_size(ic->pb) : 0;
1972 do {
1973 is_end = found_duration;
1974 offset = filesize - (duration_max_read_size << retry);
1975 if (offset < 0)
1976 offset = 0;
1977
1978 avio_seek(ic->pb, offset, SEEK_SET);
1979 read_size = 0;
1980 for (;;) {
1981 AVStream *st;
1982 FFStream *sti;
1983 if (read_size >= duration_max_read_size << (FFMAX(retry - 1, 0)))
1984 break;
1985
1986 do {
1987 ret = ff_read_packet(ic, pkt);
1988 } while (ret == AVERROR(EAGAIN));
1989 if (ret != 0)
1990 break;
1991 read_size += pkt->size;
1992 st = ic->streams[pkt->stream_index];
1993 sti = ffstream(st);
1994 if (pkt->pts != AV_NOPTS_VALUE &&
1995 (st->start_time != AV_NOPTS_VALUE ||
1996 sti->first_dts != AV_NOPTS_VALUE)) {
1997 if (pkt->duration == 0) {
1998 compute_frame_duration(ic, &num, &den, st, sti->parser, pkt);
1999 if (den && num) {
2000 pkt->duration = av_rescale_rnd(1,
2001 num * (int64_t) st->time_base.den,
2002 den * (int64_t) st->time_base.num,
2004 }
2005 }
2006 duration = pkt->pts + pkt->duration;
2007 found_duration = 1;
2008 if (st->start_time != AV_NOPTS_VALUE)
2009 duration -= st->start_time;
2010 else
2011 duration -= sti->first_dts;
2012 if (duration > 0) {
2013 if (st->duration == AV_NOPTS_VALUE || sti->info->last_duration<= 0 ||
2014 (st->duration < duration && FFABS(duration - sti->info->last_duration) < 60LL*st->time_base.den / st->time_base.num))
2015 st->duration = duration;
2016 sti->info->last_duration = duration;
2017 }
2018 }
2020 }
2021
2022 /* check if all audio/video streams have valid duration */
2023 if (!is_end) {
2024 is_end = 1;
2025 for (unsigned i = 0; i < ic->nb_streams; i++) {
2026 const AVStream *const st = ic->streams[i];
2027 switch (st->codecpar->codec_type) {
2028 case AVMEDIA_TYPE_VIDEO:
2029 case AVMEDIA_TYPE_AUDIO:
2030 if (st->duration == AV_NOPTS_VALUE)
2031 is_end = 0;
2032 }
2033 }
2034 }
2035 } while (!is_end &&
2036 offset &&
2037 ++retry <= duration_max_retry);
2038
2039 av_opt_set_int(ic, "skip_changes", 0, AV_OPT_SEARCH_CHILDREN);
2040
2041 /* warn about audio/video streams which duration could not be estimated */
2042 for (unsigned i = 0; i < ic->nb_streams; i++) {
2043 const AVStream *const st = ic->streams[i];
2044 const FFStream *const sti = cffstream(st);
2045
2046 if (st->duration == AV_NOPTS_VALUE) {
2047 switch (st->codecpar->codec_type) {
2048 case AVMEDIA_TYPE_VIDEO:
2049 case AVMEDIA_TYPE_AUDIO:
2050 if (st->start_time != AV_NOPTS_VALUE || sti->first_dts != AV_NOPTS_VALUE) {
2051 av_log(ic, AV_LOG_WARNING, "stream %d : no PTS found at end of file, duration not set\n", i);
2052 } else
2053 av_log(ic, AV_LOG_WARNING, "stream %d : no TS found at start of file, duration not set\n", i);
2054 }
2055 }
2056 }
2057skip_duration_calc:
2059
2060 avio_seek(ic->pb, old_offset, SEEK_SET);
2061 for (unsigned i = 0; i < ic->nb_streams; i++) {
2062 AVStream *const st = ic->streams[i];
2063 FFStream *const sti = ffstream(st);
2064
2065 sti->cur_dts = sti->first_dts;
2068 for (int j = 0; j < MAX_REORDER_DELAY + 1; j++)
2069 sti->pts_buffer[j] = AV_NOPTS_VALUE;
2070 }
2071}
2072
2073/* 1:1 map to AVDurationEstimationMethod */
2074static const char *const duration_name[] = {
2075 [AVFMT_DURATION_FROM_PTS] = "pts",
2076 [AVFMT_DURATION_FROM_STREAM] = "stream",
2077 [AVFMT_DURATION_FROM_BITRATE] = "bit rate",
2078};
2079
2081{
2082 return duration_name[method];
2083}
2084
2085static void estimate_timings(AVFormatContext *ic, int64_t old_offset)
2086{
2087 int64_t file_size;
2088
2089 /* get the file size, if possible */
2090 if (ic->iformat->flags & AVFMT_NOFILE) {
2091 file_size = 0;
2092 } else {
2093 file_size = avio_size(ic->pb);
2094 file_size = FFMAX(0, file_size);
2095 }
2096
2097 if ((!strcmp(ic->iformat->name, "mpeg") ||
2098 !strcmp(ic->iformat->name, "mpegts")) &&
2099 file_size && (ic->pb->seekable & AVIO_SEEKABLE_NORMAL)) {
2100 /* get accurate estimate from the PTSes */
2101 estimate_timings_from_pts(ic, old_offset);
2103 } else if (has_duration(ic)) {
2104 /* at least one component has timings - we use them for all
2105 * the components */
2107 /* nut demuxer estimate the duration from PTS */
2108 if (!strcmp(ic->iformat->name, "nut"))
2110 else
2112 } else {
2113 /* less precise: use bitrate info */
2116 }
2118
2119 for (unsigned i = 0; i < ic->nb_streams; i++) {
2120 AVStream *const st = ic->streams[i];
2121 if (st->time_base.den)
2122 av_log(ic, AV_LOG_TRACE, "stream %u: start_time: %s duration: %s\n", i,
2124 av_ts2timestr(st->duration, &st->time_base));
2125 }
2126 av_log(ic, AV_LOG_TRACE,
2127 "format: start_time: %s duration: %s (estimate from %s) bitrate=%"PRId64" kb/s\n",
2131 (int64_t)ic->bit_rate / 1000);
2132}
2133
2135{
2136 switch(avctx->codec_id) {
2137 case AV_CODEC_ID_MP1:
2138 case AV_CODEC_ID_MP2:
2139 case AV_CODEC_ID_MP3:
2140 case AV_CODEC_ID_CODEC2:
2141 return 1;
2142 }
2143
2144 return 0;
2145}
2146
2147static int has_codec_parameters(const AVStream *st, const char **errmsg_ptr)
2148{
2149 const FFStream *const sti = cffstream(st);
2150 const AVCodecContext *const avctx = sti->avctx;
2151
2152#define FAIL(errmsg) do { \
2153 if (errmsg_ptr) \
2154 *errmsg_ptr = errmsg; \
2155 return 0; \
2156 } while (0)
2157
2158 if ( avctx->codec_id == AV_CODEC_ID_NONE
2159 && avctx->codec_type != AVMEDIA_TYPE_DATA)
2160 FAIL("unknown codec");
2161 switch (avctx->codec_type) {
2162 case AVMEDIA_TYPE_AUDIO:
2163 if (!avctx->frame_size && determinable_frame_size(avctx))
2164 FAIL("unspecified frame size");
2165 if (sti->info->found_decoder >= 0 &&
2167 FAIL("unspecified sample format");
2168 if (!avctx->sample_rate)
2169 FAIL("unspecified sample rate");
2170 if (!avctx->ch_layout.nb_channels)
2171 FAIL("unspecified number of channels");
2172 if (sti->info->found_decoder >= 0 && !sti->nb_decoded_frames && avctx->codec_id == AV_CODEC_ID_DTS)
2173 FAIL("no decodable DTS frames");
2174 break;
2175 case AVMEDIA_TYPE_VIDEO:
2176 if (!avctx->width)
2177 FAIL("unspecified size");
2178 if (sti->info->found_decoder >= 0 && avctx->pix_fmt == AV_PIX_FMT_NONE)
2179 FAIL("unspecified pixel format");
2182 FAIL("no frame in rv30/40 and no sar");
2183 break;
2185 if (avctx->codec_id == AV_CODEC_ID_HDMV_PGS_SUBTITLE && !avctx->width)
2186 FAIL("unspecified size");
2187 break;
2188 case AVMEDIA_TYPE_DATA:
2189 if (avctx->codec_id == AV_CODEC_ID_NONE) return 1;
2190 }
2191
2192 return 1;
2193}
2194
2195/* returns 1 or 0 if or if not decoded data was returned, or a negative error */
2197 const AVPacket *pkt, AVDictionary **options)
2198{
2199 FFStream *const sti = ffstream(st);
2200 AVCodecContext *const avctx = sti->avctx;
2201 const AVCodec *codec;
2202 int got_picture = 1, ret = 0;
2204 AVSubtitle subtitle;
2205 int do_skip_frame = 0;
2206 enum AVDiscard skip_frame;
2207 int pkt_to_send = pkt->size > 0;
2208
2209 if (!frame)
2210 return AVERROR(ENOMEM);
2211
2212 if (!avcodec_is_open(avctx) &&
2213 sti->info->found_decoder <= 0 &&
2214 (st->codecpar->codec_id != -sti->info->found_decoder || !st->codecpar->codec_id)) {
2215 AVDictionary *thread_opt = NULL;
2216
2217 codec = find_probe_decoder(s, st, st->codecpar->codec_id);
2218
2219 if (!codec) {
2220 sti->info->found_decoder = -st->codecpar->codec_id;
2221 ret = -1;
2222 goto fail;
2223 }
2224
2225 /* Force thread count to 1 since the H.264 decoder will not extract
2226 * SPS and PPS to extradata during multi-threaded decoding. */
2227 av_dict_set(options ? options : &thread_opt, "threads", "1", 0);
2228 /* Force lowres to 0. The decoder might reduce the video size by the
2229 * lowres factor, and we don't want that propagated to the stream's
2230 * codecpar */
2231 av_dict_set(options ? options : &thread_opt, "lowres", "0", 0);
2232 if (s->codec_whitelist)
2233 av_dict_set(options ? options : &thread_opt, "codec_whitelist", s->codec_whitelist, 0);
2234 ret = avcodec_open2(avctx, codec, options ? options : &thread_opt);
2235 if (!options)
2236 av_dict_free(&thread_opt);
2237 if (ret < 0) {
2238 sti->info->found_decoder = -avctx->codec_id;
2239 goto fail;
2240 }
2241 sti->info->found_decoder = 1;
2242 } else if (!sti->info->found_decoder)
2243 sti->info->found_decoder = 1;
2244
2245 if (sti->info->found_decoder < 0) {
2246 ret = -1;
2247 goto fail;
2248 }
2249
2251 do_skip_frame = 1;
2252 skip_frame = avctx->skip_frame;
2253 avctx->skip_frame = AVDISCARD_ALL;
2254 }
2255
2256 while ((pkt_to_send || (!pkt->data && got_picture)) &&
2257 ret >= 0 &&
2259 (!sti->codec_info_nb_frames &&
2261 got_picture = 0;
2262 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO ||
2263 avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
2264 ret = avcodec_send_packet(avctx, pkt);
2265 if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
2266 break;
2267 if (ret >= 0)
2268 pkt_to_send = 0;
2270 if (ret >= 0)
2271 got_picture = 1;
2272 if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
2273 ret = 0;
2274 } else if (avctx->codec_type == AVMEDIA_TYPE_SUBTITLE) {
2275 ret = avcodec_decode_subtitle2(avctx, &subtitle,
2276 &got_picture, pkt);
2277 if (got_picture)
2278 avsubtitle_free(&subtitle);
2279 if (ret >= 0)
2280 pkt_to_send = 0;
2281 }
2282 if (ret >= 0) {
2283 if (got_picture)
2284 sti->nb_decoded_frames++;
2285 ret = got_picture;
2286 }
2287 }
2288
2289fail:
2290 if (do_skip_frame) {
2291 avctx->skip_frame = skip_frame;
2292 }
2293
2295 return ret;
2296}
2297
2298static int chapter_start_cmp(const void *p1, const void *p2)
2299{
2300 const AVChapter *const ch1 = *(AVChapter**)p1;
2301 const AVChapter *const ch2 = *(AVChapter**)p2;
2302 int delta = av_compare_ts(ch1->start, ch1->time_base, ch2->start, ch2->time_base);
2303 if (delta)
2304 return delta;
2305 return FFDIFFSIGN(ch1->id, ch2->id);
2306}
2307
2309{
2310 int64_t max_time = 0;
2311 AVChapter **timetable;
2312
2313 if (!s->nb_chapters)
2314 return 0;
2315
2316 if (s->duration > 0 && s->start_time < INT64_MAX - s->duration)
2317 max_time = s->duration +
2318 ((s->start_time == AV_NOPTS_VALUE) ? 0 : s->start_time);
2319
2320 timetable = av_memdup(s->chapters, s->nb_chapters * sizeof(*timetable));
2321 if (!timetable)
2322 return AVERROR(ENOMEM);
2323 qsort(timetable, s->nb_chapters, sizeof(*timetable), chapter_start_cmp);
2324
2325 for (unsigned i = 0; i < s->nb_chapters; i++)
2326 if (timetable[i]->end == AV_NOPTS_VALUE) {
2327 AVChapter *const ch = timetable[i];
2328 int64_t end = max_time ? av_rescale_q(max_time, AV_TIME_BASE_Q,
2329 ch->time_base)
2330 : INT64_MAX;
2331
2332 if (i + 1 < s->nb_chapters) {
2333 const AVChapter *const ch1 = timetable[i + 1];
2334 int64_t next_start = av_rescale_q(ch1->start, ch1->time_base,
2335 ch->time_base);
2336 if (next_start > ch->start && next_start < end)
2337 end = next_start;
2338 }
2339 ch->end = (end == INT64_MAX || end < ch->start) ? ch->start : end;
2340 }
2341 av_free(timetable);
2342 return 0;
2343}
2344
2345static int get_std_framerate(int i)
2346{
2347 if (i < 30*12)
2348 return (i + 1) * 1001;
2349 i -= 30*12;
2350
2351 if (i < 30)
2352 return (i + 31) * 1001 * 12;
2353 i -= 30;
2354
2355 if (i < 3)
2356 return ((const int[]) { 80, 120, 240})[i] * 1001 * 12;
2357
2358 i -= 3;
2359
2360 return ((const int[]) { 24, 30, 60, 12, 15, 48 })[i] * 1000 * 12;
2361}
2362
2363/* Is the time base unreliable?
2364 * This is a heuristic to balance between quick acceptance of the values in
2365 * the headers vs. some extra checks.
2366 * Old DivX and Xvid often have nonsense timebases like 1fps or 2fps.
2367 * MPEG-2 commonly misuses field repeat flags to store different framerates.
2368 * And there are "variable" fps files this needs to detect as well. */
2370{
2371 FFStream *const sti = ffstream(st);
2372 const AVCodecDescriptor *desc = sti->codec_desc;
2373 AVCodecContext *c = sti->avctx;
2374 AVRational mul = (AVRational){ desc && (desc->props & AV_CODEC_PROP_FIELDS) ? 2 : 1, 1 };
2375 AVRational time_base = c->framerate.num ? av_inv_q(av_mul_q(c->framerate, mul))
2376 /* NOHEADER check added to not break existing behavior */
2377 : (((ic->ctx_flags & AVFMTCTX_NOHEADER) ||
2379 : st->time_base);
2380
2381 if (time_base.den >= 101LL * time_base.num ||
2382 time_base.den < 5LL * time_base.num ||
2383 // c->codec_tag == AV_RL32("DIVX") ||
2384 // c->codec_tag == AV_RL32("XVID") ||
2385 c->codec_tag == AV_RL32("mp4v") ||
2386 c->codec_id == AV_CODEC_ID_MPEG2VIDEO ||
2387 c->codec_id == AV_CODEC_ID_GIF ||
2388 c->codec_id == AV_CODEC_ID_HEVC ||
2389 c->codec_id == AV_CODEC_ID_H264)
2390 return 1;
2391 return 0;
2392}
2393
2395{
2396 FFStream *const sti = ffstream(st);
2397 FFStreamInfo *info = sti->info;
2398 int64_t last = info->last_dts;
2399
2400 if ( ts != AV_NOPTS_VALUE && last != AV_NOPTS_VALUE && ts > last
2401 && ts - (uint64_t)last < INT64_MAX) {
2402 double dts = (is_relative(ts) ? ts - RELATIVE_TS_BASE : ts) * av_q2d(st->time_base);
2403 int64_t duration = ts - last;
2404
2405 if (!info->duration_error)
2406 info->duration_error = av_mallocz(sizeof(info->duration_error[0])*2);
2407 if (!info->duration_error)
2408 return AVERROR(ENOMEM);
2409
2410// if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
2411// av_log(NULL, AV_LOG_ERROR, "%f\n", dts);
2412 for (int i = 0; i < MAX_STD_TIMEBASES; i++) {
2413 if (info->duration_error[0][1][i] < 1e10) {
2415 double sdts = dts*framerate/(1001*12);
2416 for (int j = 0; j < 2; j++) {
2417 int64_t ticks = llrint(sdts+j*0.5);
2418 double error = sdts - ticks + j*0.5;
2419 info->duration_error[j][0][i] += error;
2420 info->duration_error[j][1][i] += error*error;
2421 }
2422 }
2423 }
2424 if (info->rfps_duration_sum <= INT64_MAX - duration) {
2425 info->duration_count++;
2426 info->rfps_duration_sum += duration;
2427 }
2428
2429 if (info->duration_count % 10 == 0) {
2430 int n = info->duration_count;
2431 for (int i = 0; i < MAX_STD_TIMEBASES; i++) {
2432 if (info->duration_error[0][1][i] < 1e10) {
2433 double a0 = info->duration_error[0][0][i] / n;
2434 double error0 = info->duration_error[0][1][i] / n - a0*a0;
2435 double a1 = info->duration_error[1][0][i] / n;
2436 double error1 = info->duration_error[1][1][i] / n - a1*a1;
2437 if (error0 > 0.04 && error1 > 0.04) {
2438 info->duration_error[0][1][i] = 2e10;
2439 info->duration_error[1][1][i] = 2e10;
2440 }
2441 }
2442 }
2443 }
2444
2445 // ignore the first 4 values, they might have some random jitter
2446 if (info->duration_count > 3 && is_relative(ts) == is_relative(last))
2447 info->duration_gcd = av_gcd(info->duration_gcd, duration);
2448 }
2449 if (ts != AV_NOPTS_VALUE)
2450 info->last_dts = ts;
2451
2452 return 0;
2453}
2454
2456{
2457 for (unsigned i = 0; i < ic->nb_streams; i++) {
2458 AVStream *const st = ic->streams[i];
2459 FFStream *const sti = ffstream(st);
2460
2462 continue;
2463 // the check for tb_unreliable() is not completely correct, since this is not about handling
2464 // an unreliable/inexact time base, but a time base that is finer than necessary, as e.g.
2465 // ipmovie.c produces.
2466 if (tb_unreliable(ic, st) && sti->info->duration_count > 15 && sti->info->duration_gcd > FFMAX(1, st->time_base.den/(500LL*st->time_base.num)) && !st->r_frame_rate.num &&
2467 sti->info->duration_gcd < INT64_MAX / st->time_base.num)
2468 av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den, st->time_base.den, st->time_base.num * sti->info->duration_gcd, INT_MAX);
2469 if (sti->info->duration_count > 1 && !st->r_frame_rate.num
2470 && tb_unreliable(ic, st)) {
2471 int num = 0;
2472 double best_error = 0.01;
2473 AVRational ref_rate = st->r_frame_rate.num ? st->r_frame_rate : av_inv_q(st->time_base);
2474
2475 for (int j = 0; j < MAX_STD_TIMEBASES; j++) {
2476 if (sti->info->codec_info_duration &&
2477 sti->info->codec_info_duration*av_q2d(st->time_base) < (1001*11.5)/get_std_framerate(j))
2478 continue;
2479 if (!sti->info->codec_info_duration && get_std_framerate(j) < 1001*12)
2480 continue;
2481
2482 if (av_q2d(st->time_base) * sti->info->rfps_duration_sum / sti->info->duration_count < (1001*12.0 * 0.8)/get_std_framerate(j))
2483 continue;
2484
2485 for (int k = 0; k < 2; k++) {
2486 int n = sti->info->duration_count;
2487 double a = sti->info->duration_error[k][0][j] / n;
2488 double error = sti->info->duration_error[k][1][j]/n - a*a;
2489
2490 if (error < best_error && best_error> 0.000000001) {
2491 best_error= error;
2493 }
2494 if (error < 0.02)
2495 av_log(ic, AV_LOG_DEBUG, "rfps: %f %f\n", get_std_framerate(j) / 12.0/1001, error);
2496 }
2497 }
2498 // do not increase frame rate by more than 1 % in order to match a standard rate.
2499 if (num && (!ref_rate.num || (double)num/(12*1001) < 1.01 * av_q2d(ref_rate)))
2500 av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den, num, 12*1001, INT_MAX);
2501 }
2502 if ( !st->avg_frame_rate.num
2503 && st->r_frame_rate.num && sti->info->rfps_duration_sum
2504 && sti->info->codec_info_duration <= 0
2505 && sti->info->duration_count > 2
2506 && fabs(1.0 / (av_q2d(st->r_frame_rate) * av_q2d(st->time_base)) - sti->info->rfps_duration_sum / (double)sti->info->duration_count) <= 1.0
2507 ) {
2508 av_log(ic, AV_LOG_DEBUG, "Setting avg frame rate based on r frame rate\n");
2509 st->avg_frame_rate = st->r_frame_rate;
2510 }
2511
2514 sti->info->duration_count = 0;
2515 sti->info->rfps_duration_sum = 0;
2516 }
2517}
2518
2520{
2521 const AVBitStreamFilter *const f = av_bsf_get_by_name("extract_extradata");
2522 if (!f)
2523 return 0;
2524
2525 if (f->codec_ids) {
2526 const enum AVCodecID *ids;
2527 for (ids = f->codec_ids; *ids != AV_CODEC_ID_NONE; ids++)
2528 if (*ids == st->codecpar->codec_id)
2529 return 1;
2530 }
2531
2532 return 0;
2533}
2534
2536{
2537 FFStream *const sti = ffstream(st);
2538 const AVBitStreamFilter *f;
2539 int ret;
2540
2541 f = av_bsf_get_by_name("extract_extradata");
2542 if (!f)
2543 goto finish;
2544
2545 /* check that the codec id is supported */
2547 if (!ret)
2548 goto finish;
2549
2552 if (ret < 0)
2553 return ret;
2554
2556 st->codecpar);
2557 if (ret < 0)
2558 goto fail;
2559
2561
2563 if (ret < 0)
2564 goto fail;
2565
2566finish:
2567 sti->extract_extradata.inited = 1;
2568
2569 return 0;
2570fail:
2572 return ret;
2573}
2574
2576{
2577 FFStream *const sti = ffstream(st);
2578 AVPacket *const pkt_ref = si->parse_pkt;
2579 int ret;
2580
2581 if (!sti->extract_extradata.inited) {
2583 if (ret < 0)
2584 return ret;
2585 }
2586
2588 return 0;
2589
2590 ret = av_packet_ref(pkt_ref, pkt);
2591 if (ret < 0)
2592 return ret;
2593
2595 if (ret < 0) {
2596 av_packet_unref(pkt_ref);
2597 return ret;
2598 }
2599
2600 while (ret >= 0 && !sti->avctx->extradata) {
2602 if (ret < 0) {
2603 if (ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
2604 return ret;
2605 continue;
2606 }
2607
2608 for (int i = 0; i < pkt_ref->side_data_elems; i++) {
2609 AVPacketSideData *const side_data = &pkt_ref->side_data[i];
2610 if (side_data->type == AV_PKT_DATA_NEW_EXTRADATA) {
2611 sti->avctx->extradata = side_data->data;
2612 sti->avctx->extradata_size = side_data->size;
2613 side_data->data = NULL;
2614 side_data->size = 0;
2615 break;
2616 }
2617 }
2618 av_packet_unref(pkt_ref);
2619 }
2620
2621 return 0;
2622}
2623
2625 const AVCodecContext *avctx)
2626{
2627 AVCodecParameters *par_tmp;
2628 int ret;
2629
2630 par_tmp = avcodec_parameters_alloc();
2631 if (!par_tmp)
2632 return AVERROR(ENOMEM);
2633
2634 ret = avcodec_parameters_copy(par_tmp, par);
2635 if (ret < 0)
2636 goto fail;
2637
2639 if (ret < 0)
2640 goto fail;
2641
2642 /* Restore some values if they are signaled at the container level
2643 * given they may have been replaced by codec level values as read
2644 * internally by avformat_find_stream_info().
2645 */
2646 if (par_tmp->color_range != AVCOL_RANGE_UNSPECIFIED)
2647 par->color_range = par_tmp->color_range;
2648 if (par_tmp->color_primaries != AVCOL_PRI_UNSPECIFIED)
2649 par->color_primaries = par_tmp->color_primaries;
2650 if (par_tmp->color_trc != AVCOL_TRC_UNSPECIFIED)
2651 par->color_trc = par_tmp->color_trc;
2652 if (par_tmp->color_space != AVCOL_SPC_UNSPECIFIED)
2653 par->color_space = par_tmp->color_space;
2655 par->chroma_location = par_tmp->chroma_location;
2656
2657 /* A failed avcodec_open2() zeroes ch_layout through
2658 * ff_codec_close() -> av_opt_free(); do not copy that empty layout
2659 * over a container-signaled one. Other AVOption types used here are
2660 * integers and are not cleared. */
2661 if (par_tmp->ch_layout.nb_channels > 0 && !par->ch_layout.nb_channels) {
2662 ret = av_channel_layout_copy(&par->ch_layout, &par_tmp->ch_layout);
2663 if (ret < 0)
2664 goto fail;
2665 }
2666
2667 ret = 0;
2668fail:
2669 avcodec_parameters_free(&par_tmp);
2670
2671 return ret;
2672}
2673
2675{
2676 FFFormatContext *const si = ffformatcontext(ic);
2677 int count = 0, ret = 0, err;
2678 int64_t read_size;
2679 AVPacket *pkt1 = si->pkt;
2680 int64_t old_offset = avio_tell(ic->pb);
2681 // new streams might appear, no options for those
2682 int orig_nb_streams = ic->nb_streams;
2683 int flush_codecs;
2684 int64_t max_analyze_duration = ic->max_analyze_duration;
2685 int64_t max_stream_analyze_duration;
2686 int64_t max_subtitle_analyze_duration;
2687 int64_t probesize = ic->probesize;
2688 int eof_reached = 0;
2689
2690 flush_codecs = probesize > 0;
2691
2692 av_opt_set_int(ic, "skip_clear", 1, AV_OPT_SEARCH_CHILDREN);
2693
2694 max_stream_analyze_duration = max_analyze_duration;
2695 max_subtitle_analyze_duration = max_analyze_duration;
2696 if (!max_analyze_duration) {
2697 max_stream_analyze_duration =
2698 max_analyze_duration = 5*AV_TIME_BASE;
2699 max_subtitle_analyze_duration = 30*AV_TIME_BASE;
2700 if (!strcmp(ic->iformat->name, "flv"))
2701 max_stream_analyze_duration = 90*AV_TIME_BASE;
2702 if (!strcmp(ic->iformat->name, "mpeg") || !strcmp(ic->iformat->name, "mpegts"))
2703 max_stream_analyze_duration = 7*AV_TIME_BASE;
2704 }
2705
2706 if (ic->pb) {
2707 FFIOContext *const ctx = ffiocontext(ic->pb);
2708 av_log(ic, AV_LOG_DEBUG, "Before avformat_find_stream_info() pos: %"PRId64" bytes read:%"PRId64" seeks:%d nb_streams:%d\n",
2709 avio_tell(ic->pb), ctx->bytes_read, ctx->seek_count, ic->nb_streams);
2710 }
2711
2712 for (unsigned i = 0; i < ic->nb_streams; i++) {
2713 const AVCodec *codec;
2714 AVDictionary *thread_opt = NULL;
2715 AVStream *const st = ic->streams[i];
2716 FFStream *const sti = ffstream(st);
2717 AVCodecContext *const avctx = sti->avctx;
2718
2719 /* check if the caller has overridden the codec id */
2720 // only for the split stuff
2721 if (!sti->parser && !(ic->flags & AVFMT_FLAG_NOPARSE) && sti->request_probe <= 0) {
2723 if (sti->parser) {
2726 } else if (sti->need_parsing == AVSTREAM_PARSE_FULL_RAW) {
2728 }
2729 } else if (sti->need_parsing) {
2730 av_log(ic, AV_LOG_VERBOSE, "parser not found for codec "
2731 "%s, packets or times may be invalid.\n",
2733 }
2734 }
2735
2737 if (ret < 0)
2738 goto find_stream_info_err;
2739 if (sti->request_probe <= 0)
2740 sti->avctx_inited = 1;
2741
2742 codec = find_probe_decoder(ic, st, st->codecpar->codec_id);
2743
2744 /* Force thread count to 1 since the H.264 decoder will not extract
2745 * SPS and PPS to extradata during multi-threaded decoding. */
2746 av_dict_set(options ? &options[i] : &thread_opt, "threads", "1", 0);
2747 /* Force lowres to 0. The decoder might reduce the video size by the
2748 * lowres factor, and we don't want that propagated to the stream's
2749 * codecpar */
2750 av_dict_set(options ? &options[i] : &thread_opt, "lowres", "0", 0);
2751
2752 if (ic->codec_whitelist)
2753 av_dict_set(options ? &options[i] : &thread_opt, "codec_whitelist", ic->codec_whitelist, 0);
2754
2755 // Try to just open decoders, in case this is enough to get parameters.
2756 // Also ensure that subtitle_header is properly set.
2757 if (!has_codec_parameters(st, NULL) && sti->request_probe <= 0 ||
2759 if (codec && !avctx->codec)
2760 if (avcodec_open2(avctx, codec, options ? &options[i] : &thread_opt) < 0)
2762 "Failed to open codec in %s\n", __func__);
2763 }
2764 if (!options)
2765 av_dict_free(&thread_opt);
2766 }
2767
2768 read_size = 0;
2769 for (;;) {
2770 const AVPacket *pkt;
2771 AVStream *st;
2772 FFStream *sti;
2773 AVCodecContext *avctx;
2774 int analyzed_all_streams;
2775 unsigned i;
2777 ret = AVERROR_EXIT;
2778 av_log(ic, AV_LOG_DEBUG, "interrupted\n");
2779 break;
2780 }
2781
2782 /* read_frame_internal() in a previous iteration of this loop may
2783 * have made changes to streams without returning a packet for them.
2784 * Handle that here. */
2786 if (ret < 0)
2787 goto unref_then_goto_end;
2788
2789 /* check if one codec still needs to be handled */
2790 for (i = 0; i < ic->nb_streams; i++) {
2791 AVStream *const st = ic->streams[i];
2792 FFStream *const sti = ffstream(st);
2793 int fps_analyze_framecount = 20;
2794 int count;
2795
2796 if (!has_codec_parameters(st, NULL))
2797 break;
2798 /* If the timebase is coarse (like the usual millisecond precision
2799 * of mkv), we need to analyze more frames to reliably arrive at
2800 * the correct fps. */
2801 if (av_q2d(st->time_base) > 0.0005)
2802 fps_analyze_framecount *= 2;
2803 if (!tb_unreliable(ic, st))
2804 fps_analyze_framecount = 0;
2805 if (ic->fps_probe_size >= 0)
2806 fps_analyze_framecount = ic->fps_probe_size;
2808 fps_analyze_framecount = 0;
2809 /* variable fps and no guess at the real fps */
2810 count = (ic->iformat->flags & AVFMT_NOTIMESTAMPS) ?
2812 sti->info->duration_count;
2813 if (!(st->r_frame_rate.num && st->avg_frame_rate.num) &&
2815 if (count < fps_analyze_framecount)
2816 break;
2817 }
2818 // Look at the first 3 frames if there is evidence of frame delay
2819 // but the decoder delay is not set.
2820 if (sti->info->frame_delay_evidence && count < 2 && sti->avctx->has_b_frames == 0)
2821 break;
2822 if (!sti->avctx->extradata &&
2825 break;
2826 if (sti->first_dts == AV_NOPTS_VALUE &&
2831 break;
2832 }
2833 analyzed_all_streams = 0;
2834 if (i == ic->nb_streams && !si->missing_streams) {
2835 analyzed_all_streams = 1;
2836 /* NOTE: If the format has no header, then we need to read some
2837 * packets to get most of the streams, so we cannot stop here. */
2838 if (!(ic->ctx_flags & AVFMTCTX_NOHEADER)) {
2839 /* If we found the info for all the codecs, we can stop. */
2840 ret = count;
2841 av_log(ic, AV_LOG_DEBUG, "All info found\n");
2842 flush_codecs = 0;
2843 break;
2844 }
2845 }
2846 /* We did not get all the codec info, but we read too much data. */
2847 if (read_size >= probesize) {
2848 ret = count;
2849 av_log(ic, AV_LOG_DEBUG,
2850 "Probe buffer size limit of %"PRId64" bytes reached\n", probesize);
2851 for (unsigned i = 0; i < ic->nb_streams; i++) {
2852 AVStream *const st = ic->streams[i];
2853 FFStream *const sti = ffstream(st);
2854 if (!st->r_frame_rate.num &&
2855 sti->info->duration_count <= 1 &&
2857 strcmp(ic->iformat->name, "image2"))
2859 "Stream #%d: not enough frames to estimate rate; "
2860 "consider increasing probesize\n", i);
2861 }
2862 break;
2863 }
2864
2865 /* NOTE: A new stream can be added there if no header in file
2866 * (AVFMTCTX_NOHEADER). */
2867 ret = read_frame_internal(ic, pkt1);
2868 if (ret == AVERROR(EAGAIN))
2869 continue;
2870
2871 if (ret < 0) {
2872 /* EOF or error*/
2873 eof_reached = 1;
2874 break;
2875 }
2876
2877 if (!(ic->flags & AVFMT_FLAG_NOBUFFER)) {
2879 pkt1, NULL, 0);
2880 if (ret < 0)
2881 goto unref_then_goto_end;
2882
2883 pkt = &si->packet_buffer.tail->pkt;
2884 } else {
2885 pkt = pkt1;
2886 }
2887
2888 st = ic->streams[pkt->stream_index];
2889 sti = ffstream(st);
2891 read_size += pkt->size;
2892
2893 avctx = sti->avctx;
2894 if (!sti->avctx_inited) {
2896 if (ret < 0)
2897 goto unref_then_goto_end;
2898 sti->avctx_inited = 1;
2899 }
2900
2901 if (pkt->dts != AV_NOPTS_VALUE && sti->codec_info_nb_frames > 1) {
2902 /* check for non-increasing dts */
2903 if (sti->info->fps_last_dts != AV_NOPTS_VALUE &&
2904 sti->info->fps_last_dts >= pkt->dts) {
2905 av_log(ic, AV_LOG_DEBUG,
2906 "Non-increasing DTS in stream %d: packet %d with DTS "
2907 "%"PRId64", packet %d with DTS %"PRId64"\n",
2908 st->index, sti->info->fps_last_dts_idx,
2910 pkt->dts);
2911 sti->info->fps_first_dts =
2913 }
2914 /* Check for a discontinuity in dts. If the difference in dts
2915 * is more than 1000 times the average packet duration in the
2916 * sequence, we treat it as a discontinuity. */
2917 if (sti->info->fps_last_dts != AV_NOPTS_VALUE &&
2919 (pkt->dts - (uint64_t)sti->info->fps_last_dts) / 1000 >
2920 (sti->info->fps_last_dts - (uint64_t)sti->info->fps_first_dts) /
2921 (sti->info->fps_last_dts_idx - sti->info->fps_first_dts_idx)) {
2923 "DTS discontinuity in stream %d: packet %d with DTS "
2924 "%"PRId64", packet %d with DTS %"PRId64"\n",
2925 st->index, sti->info->fps_last_dts_idx,
2927 pkt->dts);
2928 sti->info->fps_first_dts =
2930 }
2931
2932 /* update stored dts values */
2933 if (sti->info->fps_first_dts == AV_NOPTS_VALUE) {
2934 sti->info->fps_first_dts = pkt->dts;
2936 }
2937 sti->info->fps_last_dts = pkt->dts;
2939 }
2940 if (sti->codec_info_nb_frames > 1) {
2941 int64_t t = 0;
2942 int64_t limit;
2943
2944 if (st->time_base.den > 0)
2946 if (st->avg_frame_rate.num > 0)
2948
2949 if ( t == 0
2950 && sti->codec_info_nb_frames > 30
2952 && sti->info->fps_last_dts != AV_NOPTS_VALUE) {
2954 t = FFMAX(t, av_rescale_q(dur, st->time_base, AV_TIME_BASE_Q));
2955 }
2956
2957 if (analyzed_all_streams) limit = max_analyze_duration;
2958 else if (avctx->codec_type == AVMEDIA_TYPE_SUBTITLE) limit = max_subtitle_analyze_duration;
2959 else limit = max_stream_analyze_duration;
2960
2961 if (t >= limit) {
2962 av_log(ic, AV_LOG_VERBOSE, "max_analyze_duration %"PRId64" reached at %"PRId64" microseconds st:%d\n",
2963 limit,
2964 t, pkt->stream_index);
2965 if (ic->flags & AVFMT_FLAG_NOBUFFER)
2966 av_packet_unref(pkt1);
2967 break;
2968 }
2969 if (pkt->duration > 0 && pkt->duration < INT64_MAX - sti->info->codec_info_duration) {
2970 const int fields = sti->codec_desc && (sti->codec_desc->props & AV_CODEC_PROP_FIELDS);
2971 if (avctx->codec_type == AVMEDIA_TYPE_SUBTITLE && pkt->pts != AV_NOPTS_VALUE && st->start_time != AV_NOPTS_VALUE && pkt->pts >= st->start_time
2972 && (uint64_t)pkt->pts - st->start_time < INT64_MAX
2973 ) {
2974 sti->info->codec_info_duration = FFMIN(pkt->pts - st->start_time, sti->info->codec_info_duration + pkt->duration);
2975 } else
2976 sti->info->codec_info_duration += pkt->duration;
2977 /* A corrupt packet may hold only part of a frame. Keep its
2978 * duration in the probing budget, but exclude it from FPS. */
2979 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO &&
2980 !(pkt->flags & AV_PKT_FLAG_CORRUPT) &&
2981 pkt->duration < INT64_MAX - sti->info->fps_duration) {
2982 sti->info->fps_duration += pkt->duration;
2983 sti->info->codec_info_duration_fields += sti->parser && sti->need_parsing && fields
2984 ? sti->parser->repeat_pict + 1 : 2;
2985 }
2986 }
2987 }
2989#if FF_API_R_FRAME_RATE
2990 ff_rfps_add_frame(ic, st, pkt->dts);
2991#endif
2992 if (pkt->dts != pkt->pts && pkt->dts != AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE)
2993 sti->info->frame_delay_evidence = 1;
2994 }
2995 if (!sti->avctx->extradata) {
2996 ret = extract_extradata(si, st, pkt);
2997 if (ret < 0)
2998 goto unref_then_goto_end;
2999 }
3000
3001 /* If still no information, we try to open the codec and to
3002 * decompress the frame. We try to avoid that in most cases as
3003 * it takes longer and uses more memory. For MPEG-4, we need to
3004 * decompress for QuickTime.
3005 *
3006 * If AV_CODEC_CAP_CHANNEL_CONF is set this will force decoding of at
3007 * least one frame of codec data, this makes sure the codec initializes
3008 * the channel configuration and does not only trust the values from
3009 * the container. */
3010 try_decode_frame(ic, st, pkt,
3011 (options && i < orig_nb_streams) ? &options[i] : NULL);
3012
3013 if (ic->flags & AVFMT_FLAG_NOBUFFER)
3014 av_packet_unref(pkt1);
3015
3016 sti->codec_info_nb_frames++;
3017 count++;
3018 }
3019
3020 if (eof_reached) {
3021 for (unsigned stream_index = 0; stream_index < ic->nb_streams; stream_index++) {
3022 AVStream *const st = ic->streams[stream_index];
3023 AVCodecContext *const avctx = ffstream(st)->avctx;
3024 if (!has_codec_parameters(st, NULL)) {
3025 const AVCodec *codec = find_probe_decoder(ic, st, st->codecpar->codec_id);
3026 if (codec && !avctx->codec) {
3028 if (ic->codec_whitelist)
3029 av_dict_set(&opts, "codec_whitelist", ic->codec_whitelist, 0);
3030 if (avcodec_open2(avctx, codec, (options && stream_index < orig_nb_streams) ? &options[stream_index] : &opts) < 0)
3032 "Failed to open codec in %s\n", __func__);
3034 }
3035 }
3036
3037 // EOF already reached while reading the stream above.
3038 // So continue with reoordering DTS with whatever delay we have.
3040 update_dts_from_pts(ic, stream_index, si->packet_buffer.head);
3041 }
3042 }
3043 }
3044
3045 if (flush_codecs) {
3046 AVPacket *empty_pkt = si->pkt;
3047 int err = 0;
3048 av_packet_unref(empty_pkt);
3049
3050 for (unsigned i = 0; i < ic->nb_streams; i++) {
3051 AVStream *const st = ic->streams[i];
3052 FFStream *const sti = ffstream(st);
3053
3054 /* flush the decoders */
3055 if (sti->info->found_decoder == 1) {
3056 err = try_decode_frame(ic, st, empty_pkt,
3057 (options && i < orig_nb_streams)
3058 ? &options[i] : NULL);
3059
3060 if (err < 0) {
3061 av_log(ic, AV_LOG_INFO,
3062 "decoding for stream %d failed\n", st->index);
3063 }
3064 }
3065 }
3066 }
3067
3069
3070 for (unsigned i = 0; i < ic->nb_streams; i++) {
3071 AVStream *const st = ic->streams[i];
3072 FFStream *const sti = ffstream(st);
3073 AVCodecContext *const avctx = sti->avctx;
3074
3075 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
3076 if (avctx->codec_id == AV_CODEC_ID_RAWVIDEO && !avctx->codec_tag && !avctx->bits_per_coded_sample) {
3077 uint32_t tag= avcodec_pix_fmt_to_codec_tag(avctx->pix_fmt);
3079 avctx->codec_tag= tag;
3080 }
3081
3082 /* estimate average framerate if not set by demuxer */
3083 if (sti->info->codec_info_duration_fields &&
3084 !st->avg_frame_rate.num &&
3085 sti->info->fps_duration) {
3086 int best_fps = 0;
3087 double best_error = 0.01;
3088 AVRational codec_frame_rate = avctx->framerate;
3089
3090 if (sti->info->fps_duration >= INT64_MAX / st->time_base.num / 2||
3091 sti->info->codec_info_duration_fields >= INT64_MAX / st->time_base.den ||
3092 sti->info->fps_duration < 0)
3093 continue;
3096 sti->info->fps_duration * 2 * (int64_t) st->time_base.num, 60000);
3097
3098 /* Round guessed framerate to a "standard" framerate if it's
3099 * within 1% of the original estimate. */
3100 for (int j = 0; j < MAX_STD_TIMEBASES; j++) {
3101 AVRational std_fps = { get_std_framerate(j), 12 * 1001 };
3102 double error = fabs(av_q2d(st->avg_frame_rate) /
3103 av_q2d(std_fps) - 1);
3104
3105 if (error < best_error) {
3106 best_error = error;
3107 best_fps = std_fps.num;
3108 }
3109
3111 codec_frame_rate.num > 0 && codec_frame_rate.den > 0) {
3112 error = fabs(av_q2d(codec_frame_rate) /
3113 av_q2d(std_fps) - 1);
3114 if (error < best_error) {
3115 best_error = error;
3116 best_fps = std_fps.num;
3117 }
3118 }
3119 }
3120 if (best_fps)
3122 best_fps, 12 * 1001, INT_MAX);
3123 }
3124 if (!st->r_frame_rate.num) {
3125 const AVCodecDescriptor *desc = sti->codec_desc;
3126 AVRational mul = (AVRational){ desc && (desc->props & AV_CODEC_PROP_FIELDS) ? 2 : 1, 1 };
3127 AVRational fr = av_mul_q(avctx->framerate, mul);
3128
3129 if (fr.num && fr.den && av_cmp_q(st->time_base, av_inv_q(fr)) <= 0) {
3130 st->r_frame_rate = fr;
3131 } else {
3132 st->r_frame_rate.num = st->time_base.den;
3133 st->r_frame_rate.den = st->time_base.num;
3134 }
3135 }
3136 st->codecpar->framerate = avctx->framerate;
3138 AVRational hw_ratio = { avctx->height, avctx->width };
3140 hw_ratio);
3141 }
3142 } else if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
3143 if (!avctx->bits_per_coded_sample)
3144 avctx->bits_per_coded_sample =
3146 // set stream disposition based on audio service type
3147 switch (avctx->audio_service_type) {
3150 break;
3153 break;
3156 break;
3159 break;
3162 break;
3163 }
3164 }
3165 }
3166
3167 if (probesize)
3168 estimate_timings(ic, old_offset);
3169
3170 av_opt_set_int(ic, "skip_clear", 0, AV_OPT_SEARCH_CHILDREN);
3171
3172 if (ret >= 0 && ic->nb_streams)
3173 /* We could not have all the codec parameters before EOF. */
3174 ret = -1;
3175 for (unsigned i = 0; i < ic->nb_streams; i++) {
3176 AVStream *const st = ic->streams[i];
3177 FFStream *const sti = ffstream(st);
3178 const char *errmsg;
3179
3180 /* if no packet was ever seen, update context now for has_codec_parameters */
3181 if (!sti->avctx_inited) {
3184 st->codecpar->format = sti->avctx->sample_fmt;
3186 if (ret < 0)
3187 goto find_stream_info_err;
3188 }
3189 if (!has_codec_parameters(st, &errmsg)) {
3190 char buf[256];
3191 avcodec_string(buf, sizeof(buf), sti->avctx, 0);
3193 "Could not find codec parameters for stream %d (%s): %s\n"
3194 "Consider increasing the value for the 'analyzeduration' (%"PRId64") and 'probesize' (%"PRId64") options\n",
3195 i, buf, errmsg, ic->max_analyze_duration, ic->probesize);
3196 } else {
3197 ret = 0;
3198 }
3199 }
3200
3201 err = compute_chapters_end(ic);
3202 if (err < 0) {
3203 ret = err;
3204 goto find_stream_info_err;
3205 }
3206
3207 /* update the stream parameters from the internal codec contexts */
3208 for (unsigned i = 0; i < ic->nb_streams; i++) {
3209 AVStream *const st = ic->streams[i];
3210 FFStream *const sti = ffstream(st);
3211
3212 if (sti->avctx_inited) {
3213 ret = parameters_from_context(ic, st->codecpar, sti->avctx);
3214 if (ret < 0)
3215 goto find_stream_info_err;
3216
3217 if (sti->avctx->rc_buffer_size > 0 || sti->avctx->rc_max_rate > 0 ||
3218 sti->avctx->rc_min_rate) {
3219 size_t cpb_size;
3220 AVCPBProperties *props = av_cpb_properties_alloc(&cpb_size);
3221 if (props) {
3222 if (sti->avctx->rc_buffer_size > 0)
3223 props->buffer_size = sti->avctx->rc_buffer_size;
3224 if (sti->avctx->rc_min_rate > 0)
3225 props->min_bitrate = sti->avctx->rc_min_rate;
3226 if (sti->avctx->rc_max_rate > 0)
3227 props->max_bitrate = sti->avctx->rc_max_rate;
3231 (uint8_t *)props, cpb_size, 0))
3232 av_free(props);
3233 }
3234 }
3235 }
3236
3237 sti->avctx_inited = 0;
3238 }
3239
3240 /* update the stream group parameters from the stream contexts if needed */
3241 for (unsigned i = 0; i < ic->nb_stream_groups; i++) {
3242 AVStreamGroup *const stg = ic->stream_groups[i];
3244 const AVStreamGroupEntry *stge;
3245
3246 switch (stg->type) {
3248 /* For LCEVC in mpegts, the parser is needed to get the
3249 * enhancement layer dimensions */
3250 stglv = stg->params.layered_video;
3251 stge = stg->entries[stglv->el_index];
3252 break;
3254 /* A gain map is presented at the size of its base rendition */
3255 stglv = stg->params.layered_video;
3256 if (stg->nb_entries != 2 || stglv->el_index >= 2)
3257 continue;
3258 stge = stg->entries[!stglv->el_index];
3260 continue;
3261 break;
3262 default:
3263 continue;
3264 }
3265
3266 if (stglv->width && stglv->height)
3267 continue;
3269 stglv->width = stge->stream->codecpar->width;
3270 stglv->height = stge->stream->codecpar->height;
3271 }
3272
3273find_stream_info_err:
3274 for (unsigned i = 0; i < ic->nb_streams; i++) {
3275 AVStream *const st = ic->streams[i];
3276 FFStream *const sti = ffstream(st);
3277 int err;
3278
3279 if (sti->info) {
3281 av_freep(&sti->info);
3282 }
3283
3284 if (avcodec_is_open(sti->avctx)) {
3285 err = codec_close(ic, sti);
3286 if (err < 0 && ret >= 0)
3287 ret = err;
3288 }
3289
3291 }
3292 if (ic->pb) {
3293 FFIOContext *const ctx = ffiocontext(ic->pb);
3294 av_log(ic, AV_LOG_DEBUG, "After avformat_find_stream_info() pos: %"PRId64" bytes read:%"PRId64" seeks:%d frames:%d\n",
3295 avio_tell(ic->pb), ctx->bytes_read, ctx->seek_count, count);
3296 }
3297 return ret;
3298
3299unref_then_goto_end:
3300 av_packet_unref(pkt1);
3301 goto find_stream_info_err;
3302}
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:97
static AVFormatContext * ctx
static void finish(void)
static AVDictionary * opts
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition avassert.h:68
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition avassert.h:58
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
Libavcodec external API header.
#define PARSER_FLAG_USE_CODEC_TS
Definition avcodec.h:2676
#define PARSER_FLAG_ONCE
Definition avcodec.h:2673
#define PARSER_FLAG_COMPLETE_FRAMES
Definition avcodec.h:2672
void ff_flush_packet_queue(AVFormatContext *s)
Definition avformat.c:154
int ff_format_io_close(AVFormatContext *s, AVIOContext **pb)
Definition avformat.c:992
int ff_is_intra_only(enum AVCodecID id)
Definition avformat.c:943
const AVCodec * ff_find_decoder(AVFormatContext *s, const AVStream *st, enum AVCodecID codec_id)
Definition avformat.c:892
Main libavformat public API header.
#define AVFMT_EVENT_FLAG_METADATA_UPDATED
Definition avformat.h:1796
#define AV_PTS_WRAP_SUB_OFFSET
subtract the format specific offset on wrap detection
Definition avformat.h:759
@ AV_STREAM_GROUP_ENTRY_STREAM
Definition avformat.h:1173
#define AVPROBE_SCORE_STREAM_RETRY
Definition avformat.h:481
#define AV_PTS_WRAP_IGNORE
Options for behavior on timestamp wrap detection.
Definition avformat.h:757
#define AVINDEX_KEYFRAME
Definition avformat.h:630
#define AVPROBE_PADDING_SIZE
extra allocated bytes at the end of the probe buffer
Definition avformat.h:487
#define AV_DISPOSITION_HEARING_IMPAIRED
The stream is intended for hearing impaired audiences.
Definition avformat.h:678
#define AV_DISPOSITION_COMMENT
The stream is a commentary track.
Definition avformat.h:659
@ AV_STREAM_GROUP_PARAMS_TILE_GRID
Definition avformat.h:1161
@ AV_STREAM_GROUP_PARAMS_LCEVC
Definition avformat.h:1162
@ AV_STREAM_GROUP_PARAMS_GAIN_MAP
Definition avformat.h:1165
#define AVFMTCTX_NOHEADER
signal that no header is present (streams are added dynamically)
Definition avformat.h:1352
#define AVFMT_FLAG_IGNDTS
Ignore DTS on frames that contain both DTS & PTS.
Definition avformat.h:1557
#define AV_FDEBUG_TS
Definition avformat.h:1693
#define AV_DISPOSITION_KARAOKE
The stream contains karaoke audio.
Definition avformat.h:667
#define AVFMT_FLAG_GENPTS
Generate missing pts even if it requires parsing future frames.
Definition avformat.h:1554
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition avformat.h:490
AVDurationEstimationMethod
The duration of a video can be estimated through various ways, and this enum can be used to know how ...
Definition avformat.h:1381
@ AVFMT_DURATION_FROM_BITRATE
Duration estimated from bitrate (less accurate)
Definition avformat.h:1384
@ AVFMT_DURATION_FROM_PTS
Duration accurately estimated from PTSes.
Definition avformat.h:1382
@ AVFMT_DURATION_FROM_STREAM
Duration estimated from a stream with a known duration.
Definition avformat.h:1383
#define AVSTREAM_EVENT_FLAG_NEW_PACKETS
Definition avformat.h:892
#define AVFMT_FLAG_DISCARD_CORRUPT
Discard frames marked corrupted.
Definition avformat.h:1562
#define AV_DISPOSITION_CLEAN_EFFECTS
The audio stream contains music and sound effects without voice.
Definition avformat.h:686
#define AV_DISPOSITION_VISUAL_IMPAIRED
The stream is intended for visually impaired audiences.
Definition avformat.h:682
#define AV_DISPOSITION_ATTACHED_PIC
The stream is stored in the file as an attached picture/"cover art" (e.g.
Definition avformat.h:694
#define AVFMT_FLAG_CUSTOM_IO
The caller has supplied a custom AVIOContext, don't avio_close() it.
Definition avformat.h:1561
#define AVFMT_FLAG_NOFILLIN
Do not infer any values from other values, just return what is stored in the container.
Definition avformat.h:1558
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition avformat.h:501
#define AV_PTS_WRAP_ADD_OFFSET
add the format specific offset on wrap detection
Definition avformat.h:758
#define AVPROBE_SCORE_RETRY
Definition avformat.h:480
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition avformat.h:500
#define AVFMT_FLAG_NOBUFFER
Do not buffer frames when possible.
Definition avformat.h:1560
#define AVFMT_NEEDNUMBER
Needs 'd' in filename.
Definition avformat.h:491
#define AVFMT_FLAG_NOPARSE
Do not use AVParsers, you also must set AVFMT_FLAG_NOFILLIN as the filling code works on frames and n...
Definition avformat.h:1559
@ AVSTREAM_PARSE_TIMESTAMPS
full parsing and interpolation of timestamps for frames not starting on a packet boundary
Definition avformat.h:615
@ AVSTREAM_PARSE_FULL_ONCE
full parsing and repack of the first frame only, only implemented for H.264 currently
Definition avformat.h:616
@ 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
@ AVSTREAM_PARSE_HEADERS
Only parse headers, do not repack.
Definition avformat.h:614
@ AVSTREAM_PARSE_NONE
Definition avformat.h:612
#define RELATIVE_TS_BASE
static av_always_inline FormatContextInternal * ff_fc_internal(AVFormatContext *s)
static av_always_inline int is_relative(int64_t ts)
int ff_check_interrupt(AVIOInterruptCB *cb)
Check if the user has requested to interrupt a blocking function associated with cb.
Definition avio.c:935
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
#define AVIO_FLAG_READ
read-only
Definition avio.h:630
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition aviobuf.c:326
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition avio.h:507
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition aviobuf.c:321
static av_always_inline FFIOContext * ffiocontext(AVIOContext *ctx)
static void BS_FUNC skip(BSCTX *bc, unsigned int n)
Skip n bits in the buffer.
static int FUNC metadata(CodedBitstreamContext *ctx, RWContext *rw, APVRawMetadata *current)
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define f(width, name)
Definition cbs_vp8.c:236
#define s(width, name)
Definition cbs_vp9.c:198
int avcodec_parameters_from_context(AVCodecParameters *par, const AVCodecContext *codec)
Definition codec_par.c:138
AVCodecParameters * avcodec_parameters_alloc(void)
Definition codec_par.c:57
int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
Definition codec_par.c:107
void avcodec_parameters_free(AVCodecParameters **ppar)
Definition codec_par.c:67
int avcodec_parameters_to_context(AVCodecContext *codec, const AVCodecParameters *par)
Definition codec_par.c:206
#define av_sat_sub64
Definition common.h:142
#define av_sat_add64
Definition common.h:139
#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
static __device__ float fabs(float a)
AVCPBProperties * av_cpb_properties_alloc(size_t *size)
Allocate a CPB properties structure and initialize its fields to default values.
Definition utils.c:986
@ AV_AUDIO_SERVICE_TYPE_EFFECTS
Definition defs.h:253
@ AV_AUDIO_SERVICE_TYPE_KARAOKE
Definition defs.h:260
@ AV_AUDIO_SERVICE_TYPE_HEARING_IMPAIRED
Definition defs.h:255
@ AV_AUDIO_SERVICE_TYPE_COMMENTARY
Definition defs.h:257
@ AV_AUDIO_SERVICE_TYPE_VISUALLY_IMPAIRED
Definition defs.h:254
int ff_buffer_packet(AVFormatContext *s, AVPacket *pkt)
Definition demux.c:645
static int determinable_frame_size(const AVCodecContext *avctx)
Definition demux.c:2134
static void update_initial_timestamps(AVFormatContext *s, int stream_index, int64_t dts, int64_t pts, AVPacket *pkt)
Definition demux.c:892
static int update_wrap_reference(AVFormatContext *s, AVStream *st, int stream_index, AVPacket *pkt)
Definition demux.c:500
static int has_decode_delay_been_guessed(AVStream *st)
Definition demux.c:782
static void fill_all_stream_timings(AVFormatContext *ic)
Definition demux.c:1857
static int init_input(AVFormatContext *s, const char *filename, AVDictionary **options)
Definition demux.c:158
static int has_codec_parameters(const AVStream *st, const char **errmsg_ptr)
Definition demux.c:2147
static void force_codec_ids(AVFormatContext *s, AVStream *st)
Definition demux.c:425
#define DURATION_DEFAULT_MAX_READ_SIZE
Definition demux.c:1927
static int64_t select_from_pts_buffer(AVStream *st, int64_t *pts_buffer, int64_t dts)
Definition demux.c:814
static int try_decode_frame(AVFormatContext *s, AVStream *st, const AVPacket *pkt, AVDictionary **options)
Definition demux.c:2196
static void compute_pkt_fields(AVFormatContext *s, AVStream *st, AVCodecParserContext *pc, AVPacket *pkt, int64_t next_dts, int64_t next_pts)
Definition demux.c:1006
#define ABSDIFF(a, b)
#define FAIL(errmsg)
static const char * duration_estimate_name(enum AVDurationEstimationMethod method)
Definition demux.c:2080
static void update_initial_durations(AVFormatContext *s, AVStream *st, int stream_index, int64_t duration)
Definition demux.c:946
void ff_rfps_calculate(AVFormatContext *ic)
Definition demux.c:2455
static int probe_codec(AVFormatContext *s, AVStream *st, const AVPacket *pkt)
Definition demux.c:447
static void update_dts_from_pts(AVFormatContext *s, int stream_index, PacketListEntry *pkt_buffer)
Updates the dts of packets of a stream in pkt_buffer, by re-ordering the pts of the packets in a wind...
Definition demux.c:867
static int compute_chapters_end(AVFormatContext *s)
Definition demux.c:2308
static void estimate_timings(AVFormatContext *ic, int64_t old_offset)
Definition demux.c:2085
#define DURATION_DEFAULT_MAX_RETRY
Definition demux.c:1928
int ff_rfps_add_frame(AVFormatContext *ic, AVStream *st, int64_t ts)
add frame for rfps calculation.
Definition demux.c:2394
static int set_codec_from_probe_data(AVFormatContext *s, AVStream *st, AVProbeData *pd)
Definition demux.c:103
static const char *const duration_name[]
Definition demux.c:2074
static int64_t ts_to_samples(AVStream *st, int64_t ts)
Definition demux.c:1358
static int chapter_start_cmp(const void *p1, const void *p2)
Definition demux.c:2298
static void estimate_timings_from_bit_rate(AVFormatContext *ic)
Definition demux.c:1874
static int codec_close(AVFormatContext *s, FFStream *sti)
Definition demux.c:1363
static void estimate_timings_from_pts(AVFormatContext *ic, int64_t old_offset)
Definition demux.c:1932
static int64_t wrap_timestamp(const AVStream *st, int64_t timestamp)
Definition demux.c:53
static int extract_extradata_init(AVStream *st)
Definition demux.c:2535
static void update_timestamps(AVFormatContext *s, AVStream *st, AVPacket *pkt)
Definition demux.c:568
static void compute_frame_duration(AVFormatContext *s, int *pnum, int *pden, AVStream *st, AVCodecParserContext *pc, AVPacket *pkt)
Return the frame duration in seconds.
Definition demux.c:719
static int parameters_from_context(AVFormatContext *ic, AVCodecParameters *par, const AVCodecContext *avctx)
Definition demux.c:2624
static int handle_new_packet(AVFormatContext *s, AVPacket *pkt, int allow_passthrough)
Handle a new packet and either return it directly if possible and allow_passthrough is true or queue ...
Definition demux.c:600
static int get_std_framerate(int i)
Definition demux.c:2345
static int has_duration(AVFormatContext *ic)
Return TRUE if the stream has accurate duration in any stream.
Definition demux.c:1746
static int update_stream_avctx(AVFormatContext *s)
Definition demux.c:191
static int tb_unreliable(AVFormatContext *ic, AVStream *st)
Definition demux.c:2369
int64_t ff_wrap_timestamp(const AVStream *st, int64_t timestamp)
Wrap a given time stamp, if there is an indication for an overflow.
Definition demux.c:68
#define DURATION_MAX_RETRY
Definition demux.c:1929
static PacketListEntry * get_next_pkt(AVFormatContext *s, AVStream *st, PacketListEntry *pktl)
Definition demux.c:802
static void update_stream_timings(AVFormatContext *ic)
Estimate the stream timings from the one of each components.
Definition demux.c:1763
static int parse_packet(AVFormatContext *s, AVPacket *pkt, int stream_index, int flush)
Parse a packet, add all split parts to parse_queue.
Definition demux.c:1202
static int extract_extradata_check(AVStream *st)
Definition demux.c:2519
static int read_frame_internal(AVFormatContext *s, AVPacket *pkt)
Definition demux.c:1412
static int extract_extradata(FFFormatContext *si, AVStream *st, const AVPacket *pkt)
Definition demux.c:2575
int ff_read_packet(AVFormatContext *s, AVPacket *pkt)
Read a transport packet from a media file.
Definition demux.c:652
static const AVCodec * find_probe_decoder(AVFormatContext *s, const AVStream *st, enum AVCodecID codec_id)
Definition demux.c:73
static av_always_inline int is_id3v2_format(const AVInputFormat *fmt)
Definition demux.c:227
#define FF_INFMT_FLAG_PREFER_CODEC_FRAMERATE
Definition demux.h:40
#define FF_INFMT_FLAG_INIT_CLEANUP
For an FFInputFormat with this flag set read_close() needs to be called by the caller upon read_heade...
Definition demux.h:35
#define FF_INFMT_FLAG_ID3V2_AUTO
Automatically parse ID3v2 metadata.
Definition demux.h:45
#define MAX_STD_TIMEBASES
Definition demux.h:200
static const FFInputFormat * ffifmt(const AVInputFormat *fmt)
Definition demux.h:195
#define FFERROR_REDO
Returned by demuxers to indicate that data was consumed but discarded (ignored streams or junk data).
Definition demux.h:234
void ff_reduce_index(AVFormatContext *s, int stream_index)
Ensure the index uses less memory than the maximum specified in AVFormatContext.max_index_size by dis...
Definition seek.c:50
static AVPacket * pkt
static AVFrame * frame
Public dictionary API.
void(* flush)(AVBSFContext *ctx)
Definition dts2pts.c:615
enum AVCodecID id
Definition dts2pts.c:612
static int64_t filesize(AVIOContext *pb)
Definition ffmpeg_mux.c:52
static int64_t duration
Definition ffplay.c:359
static int genpts
Definition ffplay.c:361
int ret
Definition ffplay.c:391
static int64_t start_time
Definition ffplay.c:358
#define sample
static const uint8_t frame_size[4]
Definition g723_1.h:222
#define fail
Definition test.h:479
void av_bsf_free(AVBSFContext **pctx)
Free a bitstream filter context and everything associated with it; write NULL into the supplied point...
Definition bsf.c:47
int av_bsf_init(AVBSFContext *ctx)
Prepare the filter for use, after all the parameters and options have been set.
Definition bsf.c:147
int av_bsf_alloc(const AVBitStreamFilter *filter, AVBSFContext **pctx)
Allocate a context for a given bitstream filter.
Definition bsf.c:99
int av_bsf_receive_packet(AVBSFContext *ctx, AVPacket *pkt)
Retrieve a filtered packet.
Definition bsf.c:228
int av_bsf_send_packet(AVBSFContext *ctx, AVPacket *pkt)
Submit a packet for filtering.
Definition bsf.c:200
const AVBitStreamFilter * av_bsf_get_by_name(const char *name)
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
Definition utils.c:557
const AVCodecDescriptor * avcodec_descriptor_get(enum AVCodecID id)
int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
Definition avcodec.c:142
#define AV_CODEC_CAP_AVOID_PROBING
Decoder is not a preferred choice for probing.
Definition codec.h:126
#define AV_CODEC_PROP_FIELDS
Video codec supports separate coding of fields in interlaced frames.
Definition codec_desc.h:97
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
Definition options.c:149
int av_codec_is_decoder(const AVCodec *codec)
Definition utils.c:85
const AVCodec * avcodec_find_decoder_by_name(const char *name)
Find a registered decoder with the specified name.
Definition allcodecs.c:1027
void avsubtitle_free(AVSubtitle *sub)
Free all allocated data in the given subtitle struct.
Definition avcodec.c:419
const char * avcodec_get_name(enum AVCodecID id)
Get the name of a codec.
Definition utils.c:421
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition codec_id.h:47
#define AV_CODEC_CAP_CHANNEL_CONF
Codec should fill in channel configuration and samplerate instead of container.
Definition codec.h:94
const AVCodec * av_codec_iterate(void **opaque)
Iterate over all registered codecs.
Definition allcodecs.c:952
void avcodec_free_context(AVCodecContext **avctx)
Free the codec context and everything associated with it and write NULL to the provided pointer.
Definition options.c:168
#define AV_CODEC_CAP_EXPERIMENTAL
Codec is experimental and is thus avoided in favor of non experimental encoders.
Definition codec.h:90
@ AV_CODEC_ID_HDMV_PGS_SUBTITLE
Definition codec_id.h:578
@ AV_CODEC_ID_EAC3
Definition codec_id.h:499
@ AV_CODEC_ID_DVB_SUBTITLE
Definition codec_id.h:573
@ AV_CODEC_ID_RAWVIDEO
Definition codec_id.h:63
@ AV_CODEC_ID_GIF
Definition codec_id.h:147
@ AV_CODEC_ID_H264
Definition codec_id.h:77
@ AV_CODEC_ID_APTX
Definition codec_id.h:544
@ AV_CODEC_ID_NONE
Definition codec_id.h:48
@ AV_CODEC_ID_JPEG2000
Definition codec_id.h:138
@ AV_CODEC_ID_MP1
Definition codec_id.h:501
@ AV_CODEC_ID_VVC
Definition codec_id.h:247
@ AV_CODEC_ID_AV1
Definition codec_id.h:275
@ AV_CODEC_ID_THEORA
Definition codec_id.h:80
@ AV_CODEC_ID_CODEC2
Definition codec_id.h:526
@ AV_CODEC_ID_MP2
Definition codec_id.h:459
@ AV_CODEC_ID_RV40
Definition codec_id.h:119
@ AV_CODEC_ID_DTS
Definition codec_id.h:463
@ AV_CODEC_ID_HEVC
Definition codec_id.h:223
@ AV_CODEC_ID_RV30
Definition codec_id.h:118
@ AV_CODEC_ID_AAC
Definition codec_id.h:461
@ AV_CODEC_ID_TRUEHD
Definition codec_id.h:503
@ AV_CODEC_ID_AC3
Definition codec_id.h:462
@ AV_CODEC_ID_DVB_TELETEXT
Definition codec_id.h:579
@ AV_CODEC_ID_IMM6
Definition codec_id.h:329
@ AV_CODEC_ID_MPEG4
Definition codec_id.h:62
@ AV_CODEC_ID_EVC
Definition codec_id.h:316
@ AV_CODEC_ID_MP3
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition codec_id.h:460
@ AV_CODEC_ID_AAC_LATM
Definition codec_id.h:508
@ AV_CODEC_ID_MPEG2VIDEO
preferred ID for MPEG-1/2 video decoding
Definition codec_id.h:52
int avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
Alias for avcodec_receive_frame_flags(avctx, frame, 0).
Definition avcodec.c:718
AVDiscard
Definition defs.h:239
int avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt)
Supply raw packet data as input to a decoder.
Definition decode.c:738
int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub, int *got_sub_ptr, const AVPacket *avpkt)
Decode a subtitle message.
Definition decode.c:955
@ AVDISCARD_ALL
discard all
Definition defs.h:248
unsigned int avcodec_pix_fmt_to_codec_tag(enum AVPixelFormat pix_fmt)
Return a value representing the fourCC code associated to the pixel format pix_fmt,...
Definition raw.c:31
int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
Return audio frame duration.
Definition utils.c:821
void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
Definition avcodec.c:512
int avcodec_is_open(AVCodecContext *s)
Definition avcodec.c:700
AVPacketSideData * av_packet_side_data_new(AVPacketSideData **psd, int *pnb_sd, enum AVPacketSideDataType type, size_t size, int flags)
Allocate a new packet side data.
Definition packet.c:621
AVPacketSideData * av_packet_side_data_add(AVPacketSideData **psd, int *pnb_sd, enum AVPacketSideDataType type, void *data, size_t size, int flags)
Wrap existing data as packet side data.
Definition packet.c:614
const AVPacketSideData * av_packet_side_data_get(const AVPacketSideData *sd, int nb_sd, enum AVPacketSideDataType type)
Get side information from a side data array.
Definition packet.c:571
@ AV_PKT_DATA_SKIP_SAMPLES
Recommends skipping the specified number of samples.
Definition packet.h:153
@ AV_PKT_DATA_CPB_PROPERTIES
This side data corresponds to the AVCPBProperties struct.
Definition packet.h:142
@ AV_PKT_DATA_NEW_EXTRADATA
The AV_PKT_DATA_NEW_EXTRADATA is used to notify the codec or the format that the extradata buffer was...
Definition packet.h:56
#define AV_PKT_FLAG_DISCARD
Flag is used to discard packets which are required to maintain valid decoder state but are not requir...
Definition packet.h:664
#define AV_PKT_FLAG_CORRUPT
The packet content is corrupted.
Definition packet.h:658
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition packet.c:435
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, size_t size)
Allocate new information of a packet.
Definition packet.c:231
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition packet.h:657
int av_packet_make_refcounted(AVPacket *pkt)
Ensure the data described by a given packet is reference counted.
Definition packet.c:498
uint8_t * av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type, size_t *size)
Get side information from packet.
Definition packet.c:252
int av_packet_ref(AVPacket *dst, const AVPacket *src)
Setup a new reference to the data described by a given packet.
Definition packet.c:443
AVCodecParserContext * av_parser_init(enum AVCodecID codec_id)
Definition parser.c:35
void av_parser_close(AVCodecParserContext *s)
Definition parser.c:203
int av_parser_parse2(AVCodecParserContext *s, AVCodecContext *avctx, uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size, int64_t pts, int64_t dts, int64_t pos)
Parse a packet.
Definition parser.c:120
AVFormatContext * avformat_alloc_context(void)
Allocate an AVFormatContext.
Definition options.c:165
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
Definition avformat.c:165
AVProgram * av_find_program_from_stream(AVFormatContext *ic, AVProgram *last, int s)
Find the programs which belong to a given stream.
Definition avformat.c:478
const AVInputFormat * av_probe_input_format3(const AVProbeData *pd, int is_opened, int *score_ret)
Guess the file format.
Definition format.c:245
int av_read_frame(AVFormatContext *s, AVPacket *pkt)
Return the next frame of a stream.
Definition demux.c:1646
int av_probe_input_buffer2(AVIOContext *pb, const AVInputFormat **fmt, const char *url, void *logctx, unsigned int offset, unsigned int max_probe_size)
Probe a bytestream to determine the input format.
Definition format.c:270
int avformat_open_input(AVFormatContext **ps, const char *filename, const AVInputFormat *fmt, AVDictionary **options)
Open an input stream and read the header.
Definition demux.c:231
const AVInputFormat * av_probe_input_format2(const AVProbeData *pd, int is_opened, int *score_max)
Guess the file format.
Definition format.c:251
int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
Read packets of a media file to get stream information.
Definition demux.c:2674
void avformat_close_input(AVFormatContext **ps)
Close an opened input AVFormatContext.
Definition demux.c:400
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 avformat_queue_attached_pictures(AVFormatContext *s)
Definition demux_utils.c:87
int av_find_default_stream_index(AVFormatContext *s)
Definition avformat.c:493
int av_filename_number_test(const char *filename)
Check whether filename actually is a numbered sequence generator.
Definition utils.c:121
int av_channel_layout_copy(AVChannelLayout *dst, const AVChannelLayout *src)
Make a copy of a channel layout.
AVBufferRef * av_buffer_ref(const AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition buffer.c:104
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values.
Definition dict.c:236
int av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags)
Copy entries from one AVDictionary struct into another.
Definition dict.c:250
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:87
#define AVERROR_EXIT
Immediate exit was requested; the called function should not be restarted.
Definition error.h:58
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
#define AVERROR_OPTION_NOT_FOUND
Option not found.
Definition error.h:63
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:65
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition frame.c:53
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition log.h:236
#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
AVRational av_mul_q(AVRational b, AVRational c)
Multiply two rationals.
Definition rational.c:80
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
Definition rational.c:35
static double av_q2d(AVRational a)
Convert an AVRational to a double.
Definition rational.h:104
static int av_cmp_q(AVRational a, AVRational b)
Compare two rationals.
Definition rational.h:89
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition rational.h:159
int64_t av_add_stable(AVRational ts_tb, int64_t ts, AVRational inc_tb, int64_t inc)
Add a value to a timestamp.
int av_compare_ts(int64_t ts_a, AVRational tb_a, int64_t ts_b, AVRational tb_b)
Compare two timestamps each in its own time base.
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_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding rnd)
Rescale a 64-bit integer with specified rounding.
Definition mathematics.c:58
int64_t av_gcd(int64_t a, int64_t b)
Compute the greatest common divisor of two integer operands.
Definition mathematics.c:37
int64_t av_rescale_q_rnd(int64_t a, AVRational bq, AVRational cq, enum AVRounding rnd)
Rescale a 64-bit integer by 2 rational numbers with specified rounding.
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
int64_t av_compare_mod(uint64_t a, uint64_t b, uint64_t mod)
Compare the remainders of two integer operands divided by a common divisor.
@ AV_ROUND_DOWN
Round toward -infinity.
@ AV_ROUND_PASS_MINMAX
Flag telling rescaling functions to pass INT64_MIN/MAX through unchanged, avoiding special cases for ...
@ AV_ROUND_NEAR_INF
Round to nearest and halfway cases away from zero.
void * av_memdup(const void *p, size_t size)
Duplicate a buffer with av_malloc().
Definition mem.c:408
AVMediaType
Definition avutil.h:198
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AVMEDIA_TYPE_SUBTITLE
Definition avutil.h:203
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
@ AVMEDIA_TYPE_DATA
Opaque data information usually continuous.
Definition avutil.h:202
@ AVMEDIA_TYPE_UNKNOWN
Usually treated as AVMEDIA_TYPE_DATA.
Definition avutil.h:199
@ AV_PICTURE_TYPE_I
Intra.
Definition avutil.h:278
@ AV_PICTURE_TYPE_NONE
Undefined.
Definition avutil.h:277
@ AV_PICTURE_TYPE_B
Bi-dir predicted.
Definition avutil.h:280
@ AV_SAMPLE_FMT_NONE
Definition samplefmt.h:56
int av_match_list(const char *name, const char *list, char separator)
Check if a name is in a list.
Definition avstring.c:443
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition avutil.h:247
#define AV_TIME_BASE
Internal time base represented as integer.
Definition avutil.h:253
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition avutil.h:263
int av_opt_get_dict_val(void *obj, const char *name, int search_flags, AVDictionary **out_val)
Definition opt.c:1476
#define AV_OPT_SEARCH_CHILDREN
Search in possible children of the given object first.
Definition opt.h:604
void av_opt_set_defaults(void *s)
Set the values of all AVOption fields to their default values.
Definition opt.c:1771
int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
Definition opt.c:951
int av_opt_set_dict_val(void *obj, const char *name, const AVDictionary *val, int search_flags)
Definition opt.c:1066
int av_opt_set_dict(void *obj, AVDictionary **options)
Set all the options from a given dictionary on an object.
Definition opt.c:2079
int a
#define MAX_REORDER_DELAY
#define ID3v2_DEFAULT_MAGIC
Default magic bytes for ID3v2 header: "ID3".
Definition id3v2.h:35
void ff_id3v2_free_extra_meta(ID3v2ExtraMeta **extra_meta)
Free memory allocated parsing special (non-text) metadata.
Definition reader.c:1231
int ff_id3v2_parse_apic(AVFormatContext *s, ID3v2ExtraMeta *extra_meta)
Create a stream for each APIC (attached picture) extracted from the ID3v2 header.
Definition reader.c:1247
int ff_id3v2_parse_priv(AVFormatContext *s, ID3v2ExtraMeta *extra_meta)
Add metadata for all PRIV tags in the ID3v2 header.
Definition reader.c:1343
int ff_id3v2_parse_chapters(AVFormatContext *s, ID3v2ExtraMeta *extra_meta)
Create chapters for all CHAP tags found in the ID3v2 header.
Definition reader.c:1278
void ff_id3v2_read_dict(AVFormatContext *s, AVIOContext *pb, AVDictionary **metadata, const char *magic, ID3v2ExtraMeta **extra_meta)
Read an ID3v2 tag into specified dictionary and retrieve supported extra metadata.
Definition reader.c:1219
#define av_log2
Definition intmath.h:84
#define AV_WL32(p, v)
#define AV_RL32(p)
enum MPFType type
Definition jpegmpfenc.c:55
unsigned offset
Definition libaomenc.c:763
static int shift(int a, int b)
Definition bonk.c:261
av_cold void CBS_FUNC close(CodedBitstreamContext **ctx_ptr)
Close a context and free all internal state.
Definition cbs.c:118
int avpriv_h264_has_num_reorder_frames(AVCodecContext *avctx)
Definition h264dec.c:62
common internal api header.
int avpriv_codec_get_cap_skip_frame_fill_param(const AVCodec *codec)
Definition utils.c:417
int av_get_audio_frame_duration2(AVCodecParameters *par, int frame_bytes)
Definition utils.c:834
static av_always_inline FFStream * ffstream(AVStream *st)
Definition internal.h:390
static av_always_inline const FFStream * cffstream(const AVStream *st)
Definition internal.h:395
static av_always_inline FFFormatContext * ffformatcontext(AVFormatContext *s)
Definition internal.h:135
int ff_packet_list_get(PacketList *list, AVPacket *pkt)
Remove the oldest AVPacket in the list and return it.
Definition packet_list.c:89
int ff_packet_list_put(PacketList *list, AVPacket *pkt, int(*copy)(AVPacket *dst, const AVPacket *src), int flags)
Append an AVPacket to the list.
Definition packet_list.c:40
#define av_always_inline
Definition attributes.h:72
common internal API header
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition internal.h:71
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition internal.h:72
const char * desc
Definition libsvtav1.c:83
#define FFSWAP(type, a, b)
Definition macros.h:52
#define FFMIN(a, b)
Definition macros.h:49
#define FFMAX(a, b)
Definition macros.h:47
#define FFDIFFSIGN(x, y)
Comparator.
Definition macros.h:45
Memory handling functions.
uint32_t tag
Definition movenc.c:2128
const char data[16]
Definition mxf.c:149
#define av_strdup(s)
Definition ops_static.c:55
#define av_realloc(p, s)
Definition ops_static.c:54
AVOptions.
pixel format definitions
@ AVCHROMA_LOC_UNSPECIFIED
Definition pixfmt.h:812
@ AVCOL_RANGE_UNSPECIFIED
Definition pixfmt.h:758
@ AV_PIX_FMT_NONE
Definition pixfmt.h:72
@ AVCOL_PRI_UNSPECIFIED
Definition pixfmt.h:650
@ AVCOL_TRC_UNSPECIFIED
Definition pixfmt.h:680
@ AVCOL_SPC_UNSPECIFIED
Definition pixfmt.h:714
const char * name
Definition qsvenc.c:142
enum AVPixelFormat avpriv_pix_fmt_find(enum PixelFormatTagLists list, unsigned fourcc)
Definition raw.c:78
Raw Video Codec.
@ PIX_FMT_LIST_RAW
Definition raw.h:38
AVCodecParameters * par_in
Parameters of the input stream.
Definition bsf.h:90
AVRational time_base_in
The timebase used for the timestamps of the input packets.
Definition bsf.h:102
This structure describes the bitrate properties of an encoded bitstream.
Definition defs.h:298
int64_t max_bitrate
Maximum bitrate of the stream, in bits per second.
Definition defs.h:303
int64_t buffer_size
The size of the buffer to which the ratecontrol is applied, in bits.
Definition defs.h:319
int64_t min_bitrate
Minimum bitrate of the stream, in bits per second.
Definition defs.h:308
int nb_channels
Number of channels in this layout.
int64_t id
unique ID to identify the chapter
Definition avformat.h:1361
int64_t start
Definition avformat.h:1363
int64_t end
chapter start/end time in time_base units
Definition avformat.h:1363
AVRational time_base
time base in which the start/end timestamps are specified
Definition avformat.h:1362
Describe the class of an AVClass context structure.
Definition log.h:76
main external API structure.
Definition avcodec.h:443
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition avcodec.h:644
int width
picture width / height.
Definition avcodec.h:605
int rc_buffer_size
decoder bitstream buffer size
Definition avcodec.h:1278
AVChannelLayout ch_layout
Audio channel layout.
Definition avcodec.h:1059
enum AVSampleFormat sample_fmt
audio sample format
Definition avcodec.h:1051
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition avcodec.h:468
AVRational pkt_timebase
Timebase in which pkt_dts/pts and AVPacket.dts/pts are expressed.
Definition avcodec.h:554
enum AVAudioServiceType audio_service_type
Type of service that the audio stream conveys.
Definition avcodec.h:1093
enum AVMediaType codec_type
Definition avcodec.h:451
AVRational framerate
Definition avcodec.h:563
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition avcodec.h:1574
int has_b_frames
Size of the frame reordering buffer in the decoder.
Definition avcodec.h:713
int64_t bit_rate
the average bitrate
Definition avcodec.h:493
const struct AVCodec * codec
Definition avcodec.h:452
int sample_rate
samples per second
Definition avcodec.h:1044
int64_t rc_max_rate
maximum bitrate
Definition avcodec.h:1293
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
Definition avcodec.h:526
int64_t rc_min_rate
minimum bitrate
Definition avcodec.h:1300
enum AVCodecID codec_id
Definition avcodec.h:453
int extradata_size
Definition avcodec.h:527
int frame_size
Number of samples per channel in an audio frame.
Definition avcodec.h:1072
enum AVDiscard skip_frame
Skip decoding for selected frames.
Definition avcodec.h:1677
This struct describes the properties of a single codec described by an AVCodecID.
Definition codec_desc.h:38
int props
Codec properties, a combination of AV_CODEC_PROP_* flags.
Definition codec_desc.h:54
This struct describes the properties of an encoded stream.
Definition codec_par.h:49
enum AVColorSpace color_space
Definition codec_par.h:192
int frame_size
Audio frame size, if known.
Definition codec_par.h:227
int height
The height of the video frame in pixels.
Definition codec_par.h:150
int nb_coded_side_data
Amount of entries in coded_side_data.
Definition codec_par.h:88
AVChannelLayout ch_layout
The channel layout and number of channels.
Definition codec_par.h:207
int width
The width of the video frame in pixels.
Definition codec_par.h:143
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition codec_par.h:99
int seek_preroll
Number of audio samples to skip after a discontinuity.
Definition codec_par.h:256
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
AVRational framerate
Number of frames per second, for streams with constant frame durations.
Definition codec_par.h:175
AVRational sample_aspect_ratio
The aspect ratio (width/height) which a single pixel should have when displayed.
Definition codec_par.h:161
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
enum AVColorPrimaries color_primaries
Definition codec_par.h:190
int sample_rate
The number of audio samples per second.
Definition codec_par.h:213
enum AVColorTransferCharacteristic color_trc
Definition codec_par.h:191
int initial_padding
Number of padding audio samples at the start.
Definition codec_par.h:239
AVPacketSideData * coded_side_data
Additional data associated with the entire stream.
Definition codec_par.h:83
enum AVChromaLocation chroma_location
Definition codec_par.h:193
enum AVColorRange color_range
Additional colorspace characteristics.
Definition codec_par.h:189
int duration
Duration of the current frame.
Definition avcodec.h:2752
int64_t pos
Byte position of currently parsed frame in stream.
Definition avcodec.h:2740
int repeat_pict
This field is used for proper frame duration computation in lavf.
Definition avcodec.h:2656
int key_frame
Set by parser to 1 for key frames and 0 for non-key frames.
Definition avcodec.h:2687
int64_t offset
byte offset from starting packet start
Definition avcodec.h:2678
AVCodec.
Definition codec.h:175
enum AVCodecID id
Definition codec.h:189
int capabilities
Codec capabilities.
Definition codec.h:194
Format I/O context.
Definition avformat.h:1401
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition avformat.h:1457
AVStreamGroup ** stream_groups
A list of all stream groups in the file.
Definition avformat.h:1489
AVIOContext * pb
I/O context.
Definition avformat.h:1443
int64_t start_time
Position of the first frame of the component, in AV_TIME_BASE fractional seconds.
Definition avformat.h:1527
int flags
Flags modifying the (de)muxer behaviour.
Definition avformat.h:1553
AVProgram ** programs
Definition avformat.h:1615
int64_t max_analyze_duration
Maximum duration (in AV_TIME_BASE units) of the data read from input in avformat_find_stream_info().
Definition avformat.h:1609
int ctx_flags
Flags signalling stream properties.
Definition avformat.h:1450
int fps_probe_size
The number of frames used for determining the framerate in avformat_find_stream_info().
Definition avformat.h:1669
unsigned int nb_programs
Definition avformat.h:1614
AVIOInterruptCB interrupt_callback
Custom interrupt callbacks for the I/O layer.
Definition avformat.h:1687
const struct AVInputFormat * iformat
The input container format.
Definition avformat.h:1413
enum AVDurationEstimationMethod duration_estimation_method
The duration field can be estimated through various ways, and this field can be used to know how the ...
Definition avformat.h:1850
int64_t bit_rate
Total stream bitrate in bit/s, 0 if not available.
Definition avformat.h:1544
int64_t duration_probesize
Maximum number of bytes read from input in order to determine stream durations when using estimate_ti...
Definition avformat.h:2047
char * codec_whitelist
',' separated list of allowed decoders.
Definition avformat.h:1911
unsigned int nb_stream_groups
Number of elements in AVFormatContext.stream_groups.
Definition avformat.h:1476
int64_t probesize
Maximum number of bytes read from input in order to determine stream properties.
Definition avformat.h:1601
AVStream ** streams
A list of all streams in the file.
Definition avformat.h:1469
int64_t duration
Duration of the stream, in AV_TIME_BASE fractional seconds.
Definition avformat.h:1537
int max_ts_probe
Maximum number of packets to read while waiting for the first timestamp.
Definition avformat.h:1747
int skip_estimate_duration_from_pts
Skip duration calculation in estimate_timings_from_pts.
Definition avformat.h:1835
This structure describes decoded (raw) audio or video data.
Definition frame.h:479
Bytestream IO Context.
Definition avio.h:160
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition avio.h:261
int flags
Can use flags: AVFMT_NOFILE, AVFMT_NEEDNUMBER, AVFMT_EXPERIMENTAL, AVFMT_SHOW_IDS,...
Definition avformat.h:587
const char * name
A comma separated list of short names for the format.
Definition avformat.h:572
This structure stores auxiliary information for decoding, presenting, or otherwise processing the cod...
Definition packet.h:431
uint8_t * data
Definition packet.h:432
enum AVPacketSideDataType type
Definition packet.h:434
This structure stores compressed data.
Definition packet.h:587
int stream_index
Definition packet.h:612
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
Definition packet.h:593
int flags
A combination of AV_PKT_FLAG values.
Definition packet.h:616
int size
Definition packet.h:611
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition packet.h:628
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition packet.h:603
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition packet.h:609
uint8_t * data
Definition packet.h:610
int64_t pos
byte position in stream, -1 if unknown
Definition packet.h:630
AVPacketSideData * side_data
Additional packet data that can be provided by the container.
Definition packet.h:621
int side_data_elems
Definition packet.h:622
This structure contains the data a format has to probe a file.
Definition avformat.h:473
int buf_size
Size of buf except extra allocated bytes.
Definition avformat.h:476
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition avformat.h:475
New fields can be added to the end with minor version bumps.
Definition avformat.h:1325
unsigned int nb_stream_indexes
Definition avformat.h:1330
unsigned int * stream_index
Definition avformat.h:1329
int64_t pts_wrap_reference
reference dts for wrap detection
Definition avformat.h:1348
int pts_wrap_behavior
behavior on wrap detection
Definition avformat.h:1349
Rational number (pair of numerator and denominator).
Definition rational.h:58
int num
Numerator.
Definition rational.h:59
int den
Denominator.
Definition rational.h:60
enum AVStreamGroupEntryType type
Definition avformat.h:1178
AVStream * stream
Definition avformat.h:1180
AVStreamGroupLayeredVideo is meant to define the relation between a base layer video stream and a sep...
Definition avformat.h:1104
int height
Height of the final image for presentation.
Definition avformat.h:1129
unsigned int el_index
Index of the enhancement layer stream in AVStreamGroup.
Definition avformat.h:1113
int width
Width of the final stream for presentation.
Definition avformat.h:1125
AVStreamGroupTileGrid holds information on how to combine several independent images on a single canv...
Definition avformat.h:975
unsigned int nb_entries
Number of elements in AVStreamGroup.entries.
Definition avformat.h:1286
enum AVStreamGroupParamsType type
Group type.
Definition avformat.h:1212
AVStreamGroupEntry ** entries
A list of streams or child groups in the group.
Definition avformat.h:1299
struct AVStreamGroupTileGrid * tile_grid
Definition avformat.h:1220
int nb_coded_side_data
Amount of entries in coded_side_data.
Definition avformat.h:1312
union AVStreamGroup::@313244315204024013007202273061153315146016101155 params
Group type-specific parameters.
AVPacketSideData * coded_side_data
Additional data associated with the group.
Definition avformat.h:1307
struct AVStreamGroupLayeredVideo * layered_video
Definition avformat.h:1221
Stream structure.
Definition avformat.h:768
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:791
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition avformat.h:846
enum AVDiscard discard
Selects which packets can be discarded at will and do not need to be demuxed.
Definition avformat.h:839
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition avformat.h:827
int id
Format-specific stream ID.
Definition avformat.h:780
int index
stream index in AVFormatContext
Definition avformat.h:774
int pts_wrap_bits
Number of bits in timestamps.
Definition avformat.h:911
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 avg_frame_rate
Average framerate.
Definition avformat.h:857
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition avformat.h:807
int event_flags
Flags indicating events happening on the stream, a combination of AVSTREAM_EVENT_FLAG_*.
Definition avformat.h:879
AVRational r_frame_rate
Real base framerate of the stream.
Definition avformat.h:902
int disposition
Stream disposition - a combination of AV_DISPOSITION_* flags.
Definition avformat.h:837
PacketList packet_buffer
This buffer is only needed when packets were already buffered but not decoded, for example to get the...
Definition internal.h:86
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
AVPacket * parse_pkt
The generic code uses this as a temporary packet to parse packets or for muxing, especially flushing.
Definition internal.h:104
AVPacket * pkt
Used to hold temporary packets for the generic demuxing code.
Definition internal.h:111
int priv_data_size
Size of private data so that it can be allocated in the wrapper.
Definition demux.h:80
int flags_internal
Internal flags.
Definition demux.h:85
int(* read_packet)(struct AVFormatContext *, AVPacket *pkt)
Read one packet and put it in 'pkt'.
Definition demux.h:118
int(* read_close)(struct AVFormatContext *)
Close the stream.
Definition demux.h:124
int(* read_header)(struct AVFormatContext *)
Read the format header and initialize the AVFormatContext structure.
Definition demux.h:108
int fps_last_dts_idx
Definition demux.h:227
int64_t codec_info_duration_fields
Definition demux.h:209
int found_decoder
0 -> decoder has not been searched for yet.
Definition demux.h:217
int64_t duration_gcd
Definition demux.h:203
int64_t fps_first_dts
Those are used for average framerate estimation.
Definition demux.h:224
int fps_first_dts_idx
Definition demux.h:225
int64_t fps_duration
Duration of non-corrupt FPS samples.
Definition demux.h:208
int frame_delay_evidence
Definition demux.h:210
double(* duration_error)[2][MAX_STD_TIMEBASES]
Definition demux.h:206
int64_t rfps_duration_sum
Definition demux.h:205
int64_t codec_info_duration
Duration used for the probing budget.
Definition demux.h:207
int64_t last_duration
Definition demux.h:219
int64_t last_dts
Definition demux.h:202
int64_t fps_last_dts
Definition demux.h:226
int duration_count
Definition demux.h:204
int seek_to_start
Set with seek_discard when the target of the seek is at or before the start of this stream.
Definition internal.h:264
int64_t pts_wrap_reference
Internal data to check for wrapping of the time stamp.
Definition internal.h:287
int last_IP_duration
Definition internal.h:338
int probe_packets
Number of packets to buffer for codec probing.
Definition internal.h:343
int skip_to_keyframe
Indicates that everything up to the next keyframe should be discarded.
Definition internal.h:215
struct AVCodecContext * avctx
The codec context used by avformat_find_stream_info, the parser, etc.
Definition internal.h:168
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:229
int64_t cur_dts
Definition internal.h:385
int avctx_inited
1 if avctx has been initialized with the values from the codec parameters
Definition internal.h:172
int request_probe
stream probing state -1 -> probing finished 0 -> no probing requested rest -> perform probing with re...
Definition internal.h:210
AVProbeData probe_data
Definition internal.h:330
struct AVBSFContext * bsf
Definition internal.h:178
int64_t first_discard_sample
If not 0, the first audio sample that should be discarded from the stream.
Definition internal.h:237
int64_t pts_buffer[MAX_REORDER_DELAY+1]
Definition internal.h:314
int inited
Definition internal.h:179
uint8_t pts_reorder_error_count[MAX_REORDER_DELAY+1]
Definition internal.h:312
uint8_t dts_ordered
Definition internal.h:320
int64_t last_dts_for_order_check
Internal data to analyze DTS and detect faulty mpeg streams.
Definition internal.h:319
uint8_t dts_misordered
Definition internal.h:321
int skip_samples
Number of samples to skip at the start of the frame decoded from the next packet.
Definition internal.h:220
int seek_discard
Set by a seek, cleared on the next packet of the stream, which gets in skip_samples the rest of the p...
Definition internal.h:253
const struct AVCodecDescriptor * codec_desc
Definition internal.h:387
struct AVCodecParserContext * parser
Definition internal.h:347
int seek_preroll
Set with seek_discard when the seek had AVSEEK_FLAG_PREROLL.
Definition internal.h:258
AVStream pub
The public context.
Definition internal.h:144
int update_initial_durations_done
Internal data to prevent doing update_initial_durations() twice.
Definition internal.h:304
int nb_decoded_frames
Number of internally decoded frames, used internally in libavformat, do not access its lifetime diffe...
Definition internal.h:270
int64_t first_dts
Timestamp corresponding to the last dts sync point.
Definition internal.h:384
int pts_wrap_behavior
Options for behavior, when a wrap is detected.
Definition internal.h:299
int need_context_update
Whether the internal avctx needs to be updated from codecpar (after a late change to codecpar)
Definition internal.h:185
struct FFStreamInfo * info
Stream information used internally by avformat_find_stream_info()
Definition internal.h:194
AVRational display_aspect_ratio
display aspect ratio (0 if unknown)
Definition internal.h:328
int64_t last_IP_pts
Definition internal.h:337
AVPacket * parse_pkt
The generic code uses this as a temporary packet to parse packets or for muxing, especially flushing.
Definition internal.h:362
struct FFStream::@143263315346311247247365102337045232073206113210 extract_extradata
enum AVStreamParseType need_parsing
Definition internal.h:346
int codec_info_nb_frames
Number of frames that have been demuxed during avformat_find_stream_info()
Definition internal.h:367
int64_t pts_reorder_error[MAX_REORDER_DELAY+1]
Internal data to generate dts from pts.
Definition internal.h:311
PacketList parse_queue
Packets split by the parser get queued here.
PacketList raw_packet_buffer
Raw packets from the demuxer, prior to parsing and decoding.
int metafree
Contexts and child contexts do not contain a metadata option.
int raw_packet_buffer_size
Sum of the size of packets in raw_packet_buffer, in bytes.
struct PacketListEntry * next
PacketListEntry * tail
PacketListEntry * head
#define llrint
Definition tablegen.h:47
#define av_free(p)
#define av_mallocz(s)
#define av_freep(p)
#define av_log(a,...)
static void error(const char *err)
static uint8_t tmp[40]
Definition aes_ctr.c:52
float framerate
Definition av1_levels.c:29
int64_t bitrate
Definition av1_levels.c:47
#define src
Definition vp8dsp.c:248
int num
Definition error.c:23
static int ref[MAX_W *MAX_W]
int64_t av_gettime(void)
Get the current time in microseconds.
Definition time.c:40
timestamp utils, mostly useful for debugging/logging purposes
#define av_ts2str(ts)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition timestamp.h:54
#define av_ts2timestr(ts, tb)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition timestamp.h:83
static int64_t pts
int size
unbuffered private I/O API
enum AVCodecID codec_id
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)
static double limit(double x)
static double a0(void *priv, double x, double y)
Definition vf_xfade.c:2035
static double a1(void *priv, double x, double y)
Definition vf_xfade.c:2036
float delta
int len
static double c[64]