2 * various utility functions for use within FFmpeg
3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
5 * This file is part of FFmpeg.
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.
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.
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
27 #include "libavutil/avassert.h"
28 #include "libavutil/avstring.h"
29 #include "libavutil/dict.h"
30 #include "libavutil/internal.h"
31 #include "libavutil/mathematics.h"
32 #include "libavutil/opt.h"
33 #include "libavutil/parseutils.h"
34 #include "libavutil/pixdesc.h"
35 #include "libavutil/time.h"
36 #include "libavutil/timestamp.h"
38 #include "libavcodec/bytestream.h"
39 #include "libavcodec/internal.h"
40 #include "libavcodec/raw.h"
42 #include "audiointerleave.h"
44 #include "avio_internal.h"
54 #include "libavutil/ffversion.h"
55 const char av_format_ffversion[] = "FFmpeg version " FFMPEG_VERSION;
59 * various utility functions for use within FFmpeg
62 unsigned avformat_version(void)
64 av_assert0(LIBAVFORMAT_VERSION_MICRO >= 100);
65 return LIBAVFORMAT_VERSION_INT;
68 const char *avformat_configuration(void)
70 return FFMPEG_CONFIGURATION;
73 const char *avformat_license(void)
75 #define LICENSE_PREFIX "libavformat license: "
76 return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
79 #define RELATIVE_TS_BASE (INT64_MAX - (1LL<<48))
81 static int is_relative(int64_t ts) {
82 return ts > (RELATIVE_TS_BASE - (1LL<<48));
86 * Wrap a given time stamp, if there is an indication for an overflow
89 * @param timestamp the time stamp to wrap
90 * @return resulting time stamp
92 static int64_t wrap_timestamp(AVStream *st, int64_t timestamp)
94 if (st->pts_wrap_behavior != AV_PTS_WRAP_IGNORE &&
95 st->pts_wrap_reference != AV_NOPTS_VALUE && timestamp != AV_NOPTS_VALUE) {
96 if (st->pts_wrap_behavior == AV_PTS_WRAP_ADD_OFFSET &&
97 timestamp < st->pts_wrap_reference)
98 return timestamp + (1ULL << st->pts_wrap_bits);
99 else if (st->pts_wrap_behavior == AV_PTS_WRAP_SUB_OFFSET &&
100 timestamp >= st->pts_wrap_reference)
101 return timestamp - (1ULL << st->pts_wrap_bits);
106 MAKE_ACCESSORS(AVStream, stream, AVRational, r_frame_rate)
107 MAKE_ACCESSORS(AVStream, stream, char *, recommended_encoder_configuration)
108 MAKE_ACCESSORS(AVFormatContext, format, AVCodec *, video_codec)
109 MAKE_ACCESSORS(AVFormatContext, format, AVCodec *, audio_codec)
110 MAKE_ACCESSORS(AVFormatContext, format, AVCodec *, subtitle_codec)
111 MAKE_ACCESSORS(AVFormatContext, format, AVCodec *, data_codec)
112 MAKE_ACCESSORS(AVFormatContext, format, int, metadata_header_padding)
113 MAKE_ACCESSORS(AVFormatContext, format, void *, opaque)
114 MAKE_ACCESSORS(AVFormatContext, format, av_format_control_message, control_message_cb)
115 #if FF_API_OLD_OPEN_CALLBACKS
116 FF_DISABLE_DEPRECATION_WARNINGS
117 MAKE_ACCESSORS(AVFormatContext, format, AVOpenCallback, open_cb)
118 FF_ENABLE_DEPRECATION_WARNINGS
121 int64_t av_stream_get_end_pts(const AVStream *st)
124 return st->priv_pts->val;
126 return AV_NOPTS_VALUE;
129 struct AVCodecParserContext *av_stream_get_parser(const AVStream *st)
134 void av_format_inject_global_side_data(AVFormatContext *s)
137 s->internal->inject_global_side_data = 1;
138 for (i = 0; i < s->nb_streams; i++) {
139 AVStream *st = s->streams[i];
140 st->inject_global_side_data = 1;
144 int ff_copy_whitelists(AVFormatContext *dst, AVFormatContext *src)
146 av_assert0(!dst->codec_whitelist &&
147 !dst->format_whitelist &&
148 !dst->protocol_whitelist);
149 dst-> codec_whitelist = av_strdup(src->codec_whitelist);
150 dst->format_whitelist = av_strdup(src->format_whitelist);
151 dst->protocol_whitelist = av_strdup(src->protocol_whitelist);
152 if ( (src-> codec_whitelist && !dst-> codec_whitelist)
153 || (src-> format_whitelist && !dst-> format_whitelist)
154 || (src->protocol_whitelist && !dst->protocol_whitelist)) {
155 av_log(dst, AV_LOG_ERROR, "Failed to duplicate whitelist\n");
156 return AVERROR(ENOMEM);
161 static const AVCodec *find_decoder(AVFormatContext *s, AVStream *st, enum AVCodecID codec_id)
163 if (st->codec->codec)
164 return st->codec->codec;
166 switch (st->codec->codec_type) {
167 case AVMEDIA_TYPE_VIDEO:
168 if (s->video_codec) return s->video_codec;
170 case AVMEDIA_TYPE_AUDIO:
171 if (s->audio_codec) return s->audio_codec;
173 case AVMEDIA_TYPE_SUBTITLE:
174 if (s->subtitle_codec) return s->subtitle_codec;
178 return avcodec_find_decoder(codec_id);
181 int av_format_get_probe_score(const AVFormatContext *s)
183 return s->probe_score;
186 /* an arbitrarily chosen "sane" max packet size -- 50M */
187 #define SANE_CHUNK_SIZE (50000000)
189 int ffio_limit(AVIOContext *s, int size)
191 if (s->maxsize>= 0) {
192 int64_t remaining= s->maxsize - avio_tell(s);
193 if (remaining < size) {
194 int64_t newsize = avio_size(s);
195 if (!s->maxsize || s->maxsize<newsize)
196 s->maxsize = newsize - !newsize;
197 remaining= s->maxsize - avio_tell(s);
198 remaining= FFMAX(remaining, 0);
201 if (s->maxsize>= 0 && remaining+1 < size) {
202 av_log(NULL, remaining ? AV_LOG_ERROR : AV_LOG_DEBUG, "Truncating packet of size %d to %"PRId64"\n", size, remaining+1);
209 /* Read the data in sane-sized chunks and append to pkt.
210 * Return the number of bytes read or an error. */
211 static int append_packet_chunked(AVIOContext *s, AVPacket *pkt, int size)
213 int64_t orig_pos = pkt->pos; // av_grow_packet might reset pos
214 int orig_size = pkt->size;
218 int prev_size = pkt->size;
221 /* When the caller requests a lot of data, limit it to the amount
222 * left in file or SANE_CHUNK_SIZE when it is not known. */
224 if (read_size > SANE_CHUNK_SIZE/10) {
225 read_size = ffio_limit(s, read_size);
226 // If filesize/maxsize is unknown, limit to SANE_CHUNK_SIZE
228 read_size = FFMIN(read_size, SANE_CHUNK_SIZE);
231 ret = av_grow_packet(pkt, read_size);
235 ret = avio_read(s, pkt->data + prev_size, read_size);
236 if (ret != read_size) {
237 av_shrink_packet(pkt, prev_size + FFMAX(ret, 0));
244 pkt->flags |= AV_PKT_FLAG_CORRUPT;
248 av_packet_unref(pkt);
249 return pkt->size > orig_size ? pkt->size - orig_size : ret;
252 int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
257 pkt->pos = avio_tell(s);
259 return append_packet_chunked(s, pkt, size);
262 int av_append_packet(AVIOContext *s, AVPacket *pkt, int size)
265 return av_get_packet(s, pkt, size);
266 return append_packet_chunked(s, pkt, size);
269 int av_filename_number_test(const char *filename)
273 (av_get_frame_filename(buf, sizeof(buf), filename, 1) >= 0);
276 static int set_codec_from_probe_data(AVFormatContext *s, AVStream *st,
279 static const struct {
282 enum AVMediaType type;
284 { "aac", AV_CODEC_ID_AAC, AVMEDIA_TYPE_AUDIO },
285 { "ac3", AV_CODEC_ID_AC3, AVMEDIA_TYPE_AUDIO },
286 { "dts", AV_CODEC_ID_DTS, AVMEDIA_TYPE_AUDIO },
287 { "dvbsub", AV_CODEC_ID_DVB_SUBTITLE,AVMEDIA_TYPE_SUBTITLE },
288 { "eac3", AV_CODEC_ID_EAC3, AVMEDIA_TYPE_AUDIO },
289 { "h264", AV_CODEC_ID_H264, AVMEDIA_TYPE_VIDEO },
290 { "hevc", AV_CODEC_ID_HEVC, AVMEDIA_TYPE_VIDEO },
291 { "loas", AV_CODEC_ID_AAC_LATM, AVMEDIA_TYPE_AUDIO },
292 { "m4v", AV_CODEC_ID_MPEG4, AVMEDIA_TYPE_VIDEO },
293 { "mp3", AV_CODEC_ID_MP3, AVMEDIA_TYPE_AUDIO },
294 { "mpegvideo", AV_CODEC_ID_MPEG2VIDEO, AVMEDIA_TYPE_VIDEO },
298 AVInputFormat *fmt = av_probe_input_format3(pd, 1, &score);
300 if (fmt && st->request_probe <= score) {
302 av_log(s, AV_LOG_DEBUG,
303 "Probe with size=%d, packets=%d detected %s with score=%d\n",
304 pd->buf_size, MAX_PROBE_PACKETS - st->probe_packets,
306 for (i = 0; fmt_id_type[i].name; i++) {
307 if (!strcmp(fmt->name, fmt_id_type[i].name)) {
308 st->codec->codec_id = fmt_id_type[i].id;
309 st->codec->codec_type = fmt_id_type[i].type;
317 /************************************************************/
318 /* input media file */
320 int av_demuxer_open(AVFormatContext *ic) {
323 if (ic->format_whitelist && av_match_list(ic->iformat->name, ic->format_whitelist, ',') <= 0) {
324 av_log(ic, AV_LOG_ERROR, "Format not on whitelist \'%s\'\n", ic->format_whitelist);
325 return AVERROR(EINVAL);
328 if (ic->iformat->read_header) {
329 err = ic->iformat->read_header(ic);
334 if (ic->pb && !ic->internal->data_offset)
335 ic->internal->data_offset = avio_tell(ic->pb);
340 /* Open input file and probe the format if necessary. */
341 static int init_input(AVFormatContext *s, const char *filename,
342 AVDictionary **options)
345 AVProbeData pd = { filename, NULL, 0 };
346 int score = AVPROBE_SCORE_RETRY;
349 s->flags |= AVFMT_FLAG_CUSTOM_IO;
351 return av_probe_input_buffer2(s->pb, &s->iformat, filename,
352 s, 0, s->format_probesize);
353 else if (s->iformat->flags & AVFMT_NOFILE)
354 av_log(s, AV_LOG_WARNING, "Custom AVIOContext makes no sense and "
355 "will be ignored with AVFMT_NOFILE format.\n");
359 if ((s->iformat && s->iformat->flags & AVFMT_NOFILE) ||
360 (!s->iformat && (s->iformat = av_probe_input_format2(&pd, 0, &score))))
363 if ((ret = s->io_open(s, &s->pb, filename, AVIO_FLAG_READ | s->avio_flags, options)) < 0)
368 return av_probe_input_buffer2(s->pb, &s->iformat, filename,
369 s, 0, s->format_probesize);
372 static int add_to_pktbuf(AVPacketList **packet_buffer, AVPacket *pkt,
373 AVPacketList **plast_pktl, int ref)
375 AVPacketList *pktl = av_mallocz(sizeof(AVPacketList));
379 return AVERROR(ENOMEM);
382 if ((ret = av_packet_ref(&pktl->pkt, pkt)) < 0) {
391 (*plast_pktl)->next = pktl;
393 *packet_buffer = pktl;
395 /* Add the packet in the buffered packet list. */
400 int avformat_queue_attached_pictures(AVFormatContext *s)
403 for (i = 0; i < s->nb_streams; i++)
404 if (s->streams[i]->disposition & AV_DISPOSITION_ATTACHED_PIC &&
405 s->streams[i]->discard < AVDISCARD_ALL) {
406 if (s->streams[i]->attached_pic.size <= 0) {
407 av_log(s, AV_LOG_WARNING,
408 "Attached picture on stream %d has invalid size, "
413 ret = add_to_pktbuf(&s->internal->raw_packet_buffer,
414 &s->streams[i]->attached_pic,
415 &s->internal->raw_packet_buffer_end, 1);
422 int avformat_open_input(AVFormatContext **ps, const char *filename,
423 AVInputFormat *fmt, AVDictionary **options)
425 AVFormatContext *s = *ps;
427 AVDictionary *tmp = NULL;
428 ID3v2ExtraMeta *id3v2_extra_meta = NULL;
430 if (!s && !(s = avformat_alloc_context()))
431 return AVERROR(ENOMEM);
433 av_log(NULL, AV_LOG_ERROR, "Input context has not been properly allocated by avformat_alloc_context() and is not NULL either\n");
434 return AVERROR(EINVAL);
440 av_dict_copy(&tmp, *options, 0);
442 if (s->pb) // must be before any goto fail
443 s->flags |= AVFMT_FLAG_CUSTOM_IO;
445 if ((ret = av_opt_set_dict(s, &tmp)) < 0)
448 if ((ret = init_input(s, filename, &tmp)) < 0)
450 s->probe_score = ret;
452 if (!s->protocol_whitelist && s->pb && s->pb->protocol_whitelist) {
453 s->protocol_whitelist = av_strdup(s->pb->protocol_whitelist);
454 if (!s->protocol_whitelist) {
455 ret = AVERROR(ENOMEM);
460 if (s->format_whitelist && av_match_list(s->iformat->name, s->format_whitelist, ',') <= 0) {
461 av_log(s, AV_LOG_ERROR, "Format not on whitelist \'%s\'\n", s->format_whitelist);
462 ret = AVERROR(EINVAL);
466 avio_skip(s->pb, s->skip_initial_bytes);
468 /* Check filename in case an image number is expected. */
469 if (s->iformat->flags & AVFMT_NEEDNUMBER) {
470 if (!av_filename_number_test(filename)) {
471 ret = AVERROR(EINVAL);
476 s->duration = s->start_time = AV_NOPTS_VALUE;
477 av_strlcpy(s->filename, filename ? filename : "", sizeof(s->filename));
479 /* Allocate private data. */
480 if (s->iformat->priv_data_size > 0) {
481 if (!(s->priv_data = av_mallocz(s->iformat->priv_data_size))) {
482 ret = AVERROR(ENOMEM);
485 if (s->iformat->priv_class) {
486 *(const AVClass **) s->priv_data = s->iformat->priv_class;
487 av_opt_set_defaults(s->priv_data);
488 if ((ret = av_opt_set_dict(s->priv_data, &tmp)) < 0)
493 /* e.g. AVFMT_NOFILE formats will not have a AVIOContext */
495 ff_id3v2_read(s, ID3v2_DEFAULT_MAGIC, &id3v2_extra_meta, 0);
497 if (!(s->flags&AVFMT_FLAG_PRIV_OPT) && s->iformat->read_header)
498 if ((ret = s->iformat->read_header(s)) < 0)
501 if (id3v2_extra_meta) {
502 if (!strcmp(s->iformat->name, "mp3") || !strcmp(s->iformat->name, "aac") ||
503 !strcmp(s->iformat->name, "tta")) {
504 if ((ret = ff_id3v2_parse_apic(s, &id3v2_extra_meta)) < 0)
507 av_log(s, AV_LOG_DEBUG, "demuxer does not support additional id3 data, skipping\n");
509 ff_id3v2_free_extra_meta(&id3v2_extra_meta);
511 if ((ret = avformat_queue_attached_pictures(s)) < 0)
514 if (!(s->flags&AVFMT_FLAG_PRIV_OPT) && s->pb && !s->internal->data_offset)
515 s->internal->data_offset = avio_tell(s->pb);
517 s->internal->raw_packet_buffer_remaining_size = RAW_PACKET_BUFFER_SIZE;
520 av_dict_free(options);
527 ff_id3v2_free_extra_meta(&id3v2_extra_meta);
529 if (s->pb && !(s->flags & AVFMT_FLAG_CUSTOM_IO))
531 avformat_free_context(s);
536 /*******************************************************/
538 static void force_codec_ids(AVFormatContext *s, AVStream *st)
540 switch (st->codec->codec_type) {
541 case AVMEDIA_TYPE_VIDEO:
542 if (s->video_codec_id)
543 st->codec->codec_id = s->video_codec_id;
545 case AVMEDIA_TYPE_AUDIO:
546 if (s->audio_codec_id)
547 st->codec->codec_id = s->audio_codec_id;
549 case AVMEDIA_TYPE_SUBTITLE:
550 if (s->subtitle_codec_id)
551 st->codec->codec_id = s->subtitle_codec_id;
556 static int probe_codec(AVFormatContext *s, AVStream *st, const AVPacket *pkt)
558 if (st->request_probe>0) {
559 AVProbeData *pd = &st->probe_data;
561 av_log(s, AV_LOG_DEBUG, "probing stream %d pp:%d\n", st->index, st->probe_packets);
565 uint8_t *new_buf = av_realloc(pd->buf, pd->buf_size+pkt->size+AVPROBE_PADDING_SIZE);
567 av_log(s, AV_LOG_WARNING,
568 "Failed to reallocate probe buffer for stream %d\n",
573 memcpy(pd->buf + pd->buf_size, pkt->data, pkt->size);
574 pd->buf_size += pkt->size;
575 memset(pd->buf + pd->buf_size, 0, AVPROBE_PADDING_SIZE);
578 st->probe_packets = 0;
580 av_log(s, AV_LOG_WARNING,
581 "nothing to probe for stream %d\n", st->index);
585 end= s->internal->raw_packet_buffer_remaining_size <= 0
586 || st->probe_packets<= 0;
588 if (end || av_log2(pd->buf_size) != av_log2(pd->buf_size - pkt->size)) {
589 int score = set_codec_from_probe_data(s, st, pd);
590 if ( (st->codec->codec_id != AV_CODEC_ID_NONE && score > AVPROBE_SCORE_STREAM_RETRY)
594 st->request_probe = -1;
595 if (st->codec->codec_id != AV_CODEC_ID_NONE) {
596 av_log(s, AV_LOG_DEBUG, "probed stream %d\n", st->index);
598 av_log(s, AV_LOG_WARNING, "probed stream %d failed\n", st->index);
600 force_codec_ids(s, st);
606 static int update_wrap_reference(AVFormatContext *s, AVStream *st, int stream_index, AVPacket *pkt)
608 int64_t ref = pkt->dts;
609 int i, pts_wrap_behavior;
610 int64_t pts_wrap_reference;
611 AVProgram *first_program;
613 if (ref == AV_NOPTS_VALUE)
615 if (st->pts_wrap_reference != AV_NOPTS_VALUE || st->pts_wrap_bits >= 63 || ref == AV_NOPTS_VALUE || !s->correct_ts_overflow)
617 ref &= (1LL << st->pts_wrap_bits)-1;
619 // reference time stamp should be 60 s before first time stamp
620 pts_wrap_reference = ref - av_rescale(60, st->time_base.den, st->time_base.num);
621 // if first time stamp is not more than 1/8 and 60s before the wrap point, subtract rather than add wrap offset
622 pts_wrap_behavior = (ref < (1LL << st->pts_wrap_bits) - (1LL << st->pts_wrap_bits-3)) ||
623 (ref < (1LL << st->pts_wrap_bits) - av_rescale(60, st->time_base.den, st->time_base.num)) ?
624 AV_PTS_WRAP_ADD_OFFSET : AV_PTS_WRAP_SUB_OFFSET;
626 first_program = av_find_program_from_stream(s, NULL, stream_index);
628 if (!first_program) {
629 int default_stream_index = av_find_default_stream_index(s);
630 if (s->streams[default_stream_index]->pts_wrap_reference == AV_NOPTS_VALUE) {
631 for (i = 0; i < s->nb_streams; i++) {
632 if (av_find_program_from_stream(s, NULL, i))
634 s->streams[i]->pts_wrap_reference = pts_wrap_reference;
635 s->streams[i]->pts_wrap_behavior = pts_wrap_behavior;
639 st->pts_wrap_reference = s->streams[default_stream_index]->pts_wrap_reference;
640 st->pts_wrap_behavior = s->streams[default_stream_index]->pts_wrap_behavior;
644 AVProgram *program = first_program;
646 if (program->pts_wrap_reference != AV_NOPTS_VALUE) {
647 pts_wrap_reference = program->pts_wrap_reference;
648 pts_wrap_behavior = program->pts_wrap_behavior;
651 program = av_find_program_from_stream(s, program, stream_index);
654 // update every program with differing pts_wrap_reference
655 program = first_program;
657 if (program->pts_wrap_reference != pts_wrap_reference) {
658 for (i = 0; i<program->nb_stream_indexes; i++) {
659 s->streams[program->stream_index[i]]->pts_wrap_reference = pts_wrap_reference;
660 s->streams[program->stream_index[i]]->pts_wrap_behavior = pts_wrap_behavior;
663 program->pts_wrap_reference = pts_wrap_reference;
664 program->pts_wrap_behavior = pts_wrap_behavior;
666 program = av_find_program_from_stream(s, program, stream_index);
672 int ff_read_packet(AVFormatContext *s, AVPacket *pkt)
678 AVPacketList *pktl = s->internal->raw_packet_buffer;
682 st = s->streams[pkt->stream_index];
683 if (s->internal->raw_packet_buffer_remaining_size <= 0)
684 if ((err = probe_codec(s, st, NULL)) < 0)
686 if (st->request_probe <= 0) {
687 s->internal->raw_packet_buffer = pktl->next;
688 s->internal->raw_packet_buffer_remaining_size += pkt->size;
697 ret = s->iformat->read_packet(s, pkt);
699 /* Some demuxers return FFERROR_REDO when they consume
700 data and discard it (ignored streams, junk, extradata).
701 We must re-call the demuxer to get the real packet. */
702 if (ret == FFERROR_REDO)
704 if (!pktl || ret == AVERROR(EAGAIN))
706 for (i = 0; i < s->nb_streams; i++) {
708 if (st->probe_packets)
709 if ((err = probe_codec(s, st, NULL)) < 0)
711 av_assert0(st->request_probe <= 0);
717 AVPacket tmp = { 0 };
718 ret = av_packet_ref(&tmp, pkt);
724 if ((s->flags & AVFMT_FLAG_DISCARD_CORRUPT) &&
725 (pkt->flags & AV_PKT_FLAG_CORRUPT)) {
726 av_log(s, AV_LOG_WARNING,
727 "Dropped corrupted packet (stream = %d)\n",
729 av_packet_unref(pkt);
733 if (pkt->stream_index >= (unsigned)s->nb_streams) {
734 av_log(s, AV_LOG_ERROR, "Invalid stream index %d\n", pkt->stream_index);
738 st = s->streams[pkt->stream_index];
740 if (update_wrap_reference(s, st, pkt->stream_index, pkt) && st->pts_wrap_behavior == AV_PTS_WRAP_SUB_OFFSET) {
741 // correct first time stamps to negative values
742 if (!is_relative(st->first_dts))
743 st->first_dts = wrap_timestamp(st, st->first_dts);
744 if (!is_relative(st->start_time))
745 st->start_time = wrap_timestamp(st, st->start_time);
746 if (!is_relative(st->cur_dts))
747 st->cur_dts = wrap_timestamp(st, st->cur_dts);
750 pkt->dts = wrap_timestamp(st, pkt->dts);
751 pkt->pts = wrap_timestamp(st, pkt->pts);
753 force_codec_ids(s, st);
755 /* TODO: audio: time filter; video: frame reordering (pts != dts) */
756 if (s->use_wallclock_as_timestamps)
757 pkt->dts = pkt->pts = av_rescale_q(av_gettime(), AV_TIME_BASE_Q, st->time_base);
759 if (!pktl && st->request_probe <= 0)
762 err = add_to_pktbuf(&s->internal->raw_packet_buffer, pkt,
763 &s->internal->raw_packet_buffer_end, 0);
766 s->internal->raw_packet_buffer_remaining_size -= pkt->size;
768 if ((err = probe_codec(s, st, pkt)) < 0)
774 /**********************************************************/
776 static int determinable_frame_size(AVCodecContext *avctx)
778 if (/*avctx->codec_id == AV_CODEC_ID_AAC ||*/
779 avctx->codec_id == AV_CODEC_ID_MP1 ||
780 avctx->codec_id == AV_CODEC_ID_MP2 ||
781 avctx->codec_id == AV_CODEC_ID_MP3/* ||
782 avctx->codec_id == AV_CODEC_ID_CELT*/)
788 * Return the frame duration in seconds. Return 0 if not available.
790 void ff_compute_frame_duration(AVFormatContext *s, int *pnum, int *pden, AVStream *st,
791 AVCodecParserContext *pc, AVPacket *pkt)
793 AVRational codec_framerate = s->iformat ? st->codec->framerate :
794 av_mul_q(av_inv_q(st->codec->time_base), (AVRational){1, st->codec->ticks_per_frame});
799 switch (st->codec->codec_type) {
800 case AVMEDIA_TYPE_VIDEO:
801 if (st->r_frame_rate.num && !pc && s->iformat) {
802 *pnum = st->r_frame_rate.den;
803 *pden = st->r_frame_rate.num;
804 } else if (st->time_base.num * 1000LL > st->time_base.den) {
805 *pnum = st->time_base.num;
806 *pden = st->time_base.den;
807 } else if (codec_framerate.den * 1000LL > codec_framerate.num) {
808 av_assert0(st->codec->ticks_per_frame);
809 av_reduce(pnum, pden,
811 codec_framerate.num * (int64_t)st->codec->ticks_per_frame,
814 if (pc && pc->repeat_pict) {
815 av_assert0(s->iformat); // this may be wrong for interlaced encoding but its not used for that case
816 av_reduce(pnum, pden,
817 (*pnum) * (1LL + pc->repeat_pict),
821 /* If this codec can be interlaced or progressive then we need
822 * a parser to compute duration of a packet. Thus if we have
823 * no parser in such case leave duration undefined. */
824 if (st->codec->ticks_per_frame > 1 && !pc)
828 case AVMEDIA_TYPE_AUDIO:
829 frame_size = av_get_audio_frame_duration(st->codec, pkt->size);
830 if (frame_size <= 0 || st->codec->sample_rate <= 0)
833 *pden = st->codec->sample_rate;
840 static int is_intra_only(AVCodecContext *enc) {
841 const AVCodecDescriptor *desc;
843 if (enc->codec_type != AVMEDIA_TYPE_VIDEO)
846 desc = av_codec_get_codec_descriptor(enc);
848 desc = avcodec_descriptor_get(enc->codec_id);
849 av_codec_set_codec_descriptor(enc, desc);
852 return !!(desc->props & AV_CODEC_PROP_INTRA_ONLY);
856 static int has_decode_delay_been_guessed(AVStream *st)
858 if (st->codec->codec_id != AV_CODEC_ID_H264) return 1;
859 if (!st->info) // if we have left find_stream_info then nb_decoded_frames won't increase anymore for stream copy
861 #if CONFIG_H264_DECODER
862 if (st->codec->has_b_frames &&
863 avpriv_h264_has_num_reorder_frames(st->codec) == st->codec->has_b_frames)
866 if (st->codec->has_b_frames<3)
867 return st->nb_decoded_frames >= 7;
868 else if (st->codec->has_b_frames<4)
869 return st->nb_decoded_frames >= 18;
871 return st->nb_decoded_frames >= 20;
874 static AVPacketList *get_next_pkt(AVFormatContext *s, AVStream *st, AVPacketList *pktl)
878 if (pktl == s->internal->packet_buffer_end)
879 return s->internal->parse_queue;
883 static int64_t select_from_pts_buffer(AVStream *st, int64_t *pts_buffer, int64_t dts) {
884 int onein_oneout = st->codec->codec_id != AV_CODEC_ID_H264 &&
885 st->codec->codec_id != AV_CODEC_ID_HEVC;
888 int delay = st->codec->has_b_frames;
891 if (dts == AV_NOPTS_VALUE) {
892 int64_t best_score = INT64_MAX;
893 for (i = 0; i<delay; i++) {
894 if (st->pts_reorder_error_count[i]) {
895 int64_t score = st->pts_reorder_error[i] / st->pts_reorder_error_count[i];
896 if (score < best_score) {
903 for (i = 0; i<delay; i++) {
904 if (pts_buffer[i] != AV_NOPTS_VALUE) {
905 int64_t diff = FFABS(pts_buffer[i] - dts)
906 + (uint64_t)st->pts_reorder_error[i];
907 diff = FFMAX(diff, st->pts_reorder_error[i]);
908 st->pts_reorder_error[i] = diff;
909 st->pts_reorder_error_count[i]++;
910 if (st->pts_reorder_error_count[i] > 250) {
911 st->pts_reorder_error[i] >>= 1;
912 st->pts_reorder_error_count[i] >>= 1;
919 if (dts == AV_NOPTS_VALUE)
925 static void update_initial_timestamps(AVFormatContext *s, int stream_index,
926 int64_t dts, int64_t pts, AVPacket *pkt)
928 AVStream *st = s->streams[stream_index];
929 AVPacketList *pktl = s->internal->packet_buffer ? s->internal->packet_buffer : s->internal->parse_queue;
930 int64_t pts_buffer[MAX_REORDER_DELAY+1];
934 if (st->first_dts != AV_NOPTS_VALUE ||
935 dts == AV_NOPTS_VALUE ||
936 st->cur_dts == AV_NOPTS_VALUE ||
940 delay = st->codec->has_b_frames;
941 st->first_dts = dts - (st->cur_dts - RELATIVE_TS_BASE);
943 shift = (uint64_t)st->first_dts - RELATIVE_TS_BASE;
945 for (i = 0; i<MAX_REORDER_DELAY+1; i++)
946 pts_buffer[i] = AV_NOPTS_VALUE;
948 if (is_relative(pts))
951 for (; pktl; pktl = get_next_pkt(s, st, pktl)) {
952 if (pktl->pkt.stream_index != stream_index)
954 if (is_relative(pktl->pkt.pts))
955 pktl->pkt.pts += shift;
957 if (is_relative(pktl->pkt.dts))
958 pktl->pkt.dts += shift;
960 if (st->start_time == AV_NOPTS_VALUE && pktl->pkt.pts != AV_NOPTS_VALUE)
961 st->start_time = pktl->pkt.pts;
963 if (pktl->pkt.pts != AV_NOPTS_VALUE && delay <= MAX_REORDER_DELAY && has_decode_delay_been_guessed(st)) {
964 pts_buffer[0] = pktl->pkt.pts;
965 for (i = 0; i<delay && pts_buffer[i] > pts_buffer[i + 1]; i++)
966 FFSWAP(int64_t, pts_buffer[i], pts_buffer[i + 1]);
968 pktl->pkt.dts = select_from_pts_buffer(st, pts_buffer, pktl->pkt.dts);
972 if (st->start_time == AV_NOPTS_VALUE)
973 st->start_time = pts;
976 static void update_initial_durations(AVFormatContext *s, AVStream *st,
977 int stream_index, int duration)
979 AVPacketList *pktl = s->internal->packet_buffer ? s->internal->packet_buffer : s->internal->parse_queue;
980 int64_t cur_dts = RELATIVE_TS_BASE;
982 if (st->first_dts != AV_NOPTS_VALUE) {
983 if (st->update_initial_durations_done)
985 st->update_initial_durations_done = 1;
986 cur_dts = st->first_dts;
987 for (; pktl; pktl = get_next_pkt(s, st, pktl)) {
988 if (pktl->pkt.stream_index == stream_index) {
989 if (pktl->pkt.pts != pktl->pkt.dts ||
990 pktl->pkt.dts != AV_NOPTS_VALUE ||
996 if (pktl && pktl->pkt.dts != st->first_dts) {
997 av_log(s, AV_LOG_DEBUG, "first_dts %s not matching first dts %s (pts %s, duration %"PRId64") in the queue\n",
998 av_ts2str(st->first_dts), av_ts2str(pktl->pkt.dts), av_ts2str(pktl->pkt.pts), pktl->pkt.duration);
1002 av_log(s, AV_LOG_DEBUG, "first_dts %s but no packet with dts in the queue\n", av_ts2str(st->first_dts));
1005 pktl = s->internal->packet_buffer ? s->internal->packet_buffer : s->internal->parse_queue;
1006 st->first_dts = cur_dts;
1007 } else if (st->cur_dts != RELATIVE_TS_BASE)
1010 for (; pktl; pktl = get_next_pkt(s, st, pktl)) {
1011 if (pktl->pkt.stream_index != stream_index)
1013 if (pktl->pkt.pts == pktl->pkt.dts &&
1014 (pktl->pkt.dts == AV_NOPTS_VALUE || pktl->pkt.dts == st->first_dts) &&
1015 !pktl->pkt.duration) {
1016 pktl->pkt.dts = cur_dts;
1017 if (!st->codec->has_b_frames)
1018 pktl->pkt.pts = cur_dts;
1019 // if (st->codec->codec_type != AVMEDIA_TYPE_AUDIO)
1020 pktl->pkt.duration = duration;
1023 cur_dts = pktl->pkt.dts + pktl->pkt.duration;
1026 st->cur_dts = cur_dts;
1029 static void compute_pkt_fields(AVFormatContext *s, AVStream *st,
1030 AVCodecParserContext *pc, AVPacket *pkt,
1031 int64_t next_dts, int64_t next_pts)
1033 int num, den, presentation_delayed, delay, i;
1035 AVRational duration;
1036 int onein_oneout = st->codec->codec_id != AV_CODEC_ID_H264 &&
1037 st->codec->codec_id != AV_CODEC_ID_HEVC;
1039 if (s->flags & AVFMT_FLAG_NOFILLIN)
1042 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && pkt->dts != AV_NOPTS_VALUE) {
1043 if (pkt->dts == pkt->pts && st->last_dts_for_order_check != AV_NOPTS_VALUE) {
1044 if (st->last_dts_for_order_check <= pkt->dts) {
1047 av_log(s, st->dts_misordered ? AV_LOG_DEBUG : AV_LOG_WARNING,
1048 "DTS %"PRIi64" < %"PRIi64" out of order\n",
1050 st->last_dts_for_order_check);
1051 st->dts_misordered++;
1053 if (st->dts_ordered + st->dts_misordered > 250) {
1054 st->dts_ordered >>= 1;
1055 st->dts_misordered >>= 1;
1059 st->last_dts_for_order_check = pkt->dts;
1060 if (st->dts_ordered < 8*st->dts_misordered && pkt->dts == pkt->pts)
1061 pkt->dts = AV_NOPTS_VALUE;
1064 if ((s->flags & AVFMT_FLAG_IGNDTS) && pkt->pts != AV_NOPTS_VALUE)
1065 pkt->dts = AV_NOPTS_VALUE;
1067 if (pc && pc->pict_type == AV_PICTURE_TYPE_B
1068 && !st->codec->has_b_frames)
1069 //FIXME Set low_delay = 0 when has_b_frames = 1
1070 st->codec->has_b_frames = 1;
1072 /* do we have a video B-frame ? */
1073 delay = st->codec->has_b_frames;
1074 presentation_delayed = 0;
1076 /* XXX: need has_b_frame, but cannot get it if the codec is
1077 * not initialized */
1079 pc && pc->pict_type != AV_PICTURE_TYPE_B)
1080 presentation_delayed = 1;
1082 if (pkt->pts != AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE &&
1083 st->pts_wrap_bits < 63 &&
1084 pkt->dts - (1LL << (st->pts_wrap_bits - 1)) > pkt->pts) {
1085 if (is_relative(st->cur_dts) || pkt->dts - (1LL<<(st->pts_wrap_bits - 1)) > st->cur_dts) {
1086 pkt->dts -= 1LL << st->pts_wrap_bits;
1088 pkt->pts += 1LL << st->pts_wrap_bits;
1091 /* Some MPEG-2 in MPEG-PS lack dts (issue #171 / input_file.mpg).
1092 * We take the conservative approach and discard both.
1093 * Note: If this is misbehaving for an H.264 file, then possibly
1094 * presentation_delayed is not set correctly. */
1095 if (delay == 1 && pkt->dts == pkt->pts &&
1096 pkt->dts != AV_NOPTS_VALUE && presentation_delayed) {
1097 av_log(s, AV_LOG_DEBUG, "invalid dts/pts combination %"PRIi64"\n", pkt->dts);
1098 if ( strcmp(s->iformat->name, "mov,mp4,m4a,3gp,3g2,mj2")
1099 && strcmp(s->iformat->name, "flv")) // otherwise we discard correct timestamps for vc1-wmapro.ism
1100 pkt->dts = AV_NOPTS_VALUE;
1103 duration = av_mul_q((AVRational) {pkt->duration, 1}, st->time_base);
1104 if (pkt->duration == 0) {
1105 ff_compute_frame_duration(s, &num, &den, st, pc, pkt);
1107 duration = (AVRational) {num, den};
1108 pkt->duration = av_rescale_rnd(1,
1109 num * (int64_t) st->time_base.den,
1110 den * (int64_t) st->time_base.num,
1115 if (pkt->duration != 0 && (s->internal->packet_buffer || s->internal->parse_queue))
1116 update_initial_durations(s, st, pkt->stream_index, pkt->duration);
1118 /* Correct timestamps with byte offset if demuxers only have timestamps
1119 * on packet boundaries */
1120 if (pc && st->need_parsing == AVSTREAM_PARSE_TIMESTAMPS && pkt->size) {
1121 /* this will estimate bitrate based on this frame's duration and size */
1122 offset = av_rescale(pc->offset, pkt->duration, pkt->size);
1123 if (pkt->pts != AV_NOPTS_VALUE)
1125 if (pkt->dts != AV_NOPTS_VALUE)
1129 /* This may be redundant, but it should not hurt. */
1130 if (pkt->dts != AV_NOPTS_VALUE &&
1131 pkt->pts != AV_NOPTS_VALUE &&
1132 pkt->pts > pkt->dts)
1133 presentation_delayed = 1;
1135 if (s->debug & FF_FDEBUG_TS)
1136 av_log(s, AV_LOG_TRACE,
1137 "IN delayed:%d pts:%s, dts:%s cur_dts:%s st:%d pc:%p duration:%"PRId64" delay:%d onein_oneout:%d\n",
1138 presentation_delayed, av_ts2str(pkt->pts), av_ts2str(pkt->dts), av_ts2str(st->cur_dts),
1139 pkt->stream_index, pc, pkt->duration, delay, onein_oneout);
1141 /* Interpolate PTS and DTS if they are not present. We skip H264
1142 * currently because delay and has_b_frames are not reliably set. */
1143 if ((delay == 0 || (delay == 1 && pc)) &&
1145 if (presentation_delayed) {
1146 /* DTS = decompression timestamp */
1147 /* PTS = presentation timestamp */
1148 if (pkt->dts == AV_NOPTS_VALUE)
1149 pkt->dts = st->last_IP_pts;
1150 update_initial_timestamps(s, pkt->stream_index, pkt->dts, pkt->pts, pkt);
1151 if (pkt->dts == AV_NOPTS_VALUE)
1152 pkt->dts = st->cur_dts;
1154 /* This is tricky: the dts must be incremented by the duration
1155 * of the frame we are displaying, i.e. the last I- or P-frame. */
1156 if (st->last_IP_duration == 0)
1157 st->last_IP_duration = pkt->duration;
1158 if (pkt->dts != AV_NOPTS_VALUE)
1159 st->cur_dts = pkt->dts + st->last_IP_duration;
1160 if (pkt->dts != AV_NOPTS_VALUE &&
1161 pkt->pts == AV_NOPTS_VALUE &&
1162 st->last_IP_duration > 0 &&
1163 ((uint64_t)st->cur_dts - (uint64_t)next_dts + 1) <= 2 &&
1164 next_dts != next_pts &&
1165 next_pts != AV_NOPTS_VALUE)
1166 pkt->pts = next_dts;
1168 st->last_IP_duration = pkt->duration;
1169 st->last_IP_pts = pkt->pts;
1170 /* Cannot compute PTS if not present (we can compute it only
1171 * by knowing the future. */
1172 } else if (pkt->pts != AV_NOPTS_VALUE ||
1173 pkt->dts != AV_NOPTS_VALUE ||
1176 /* presentation is not delayed : PTS and DTS are the same */
1177 if (pkt->pts == AV_NOPTS_VALUE)
1178 pkt->pts = pkt->dts;
1179 update_initial_timestamps(s, pkt->stream_index, pkt->pts,
1181 if (pkt->pts == AV_NOPTS_VALUE)
1182 pkt->pts = st->cur_dts;
1183 pkt->dts = pkt->pts;
1184 if (pkt->pts != AV_NOPTS_VALUE)
1185 st->cur_dts = av_add_stable(st->time_base, pkt->pts, duration, 1);
1189 if (pkt->pts != AV_NOPTS_VALUE && delay <= MAX_REORDER_DELAY && has_decode_delay_been_guessed(st)) {
1190 st->pts_buffer[0] = pkt->pts;
1191 for (i = 0; i<delay && st->pts_buffer[i] > st->pts_buffer[i + 1]; i++)
1192 FFSWAP(int64_t, st->pts_buffer[i], st->pts_buffer[i + 1]);
1194 pkt->dts = select_from_pts_buffer(st, st->pts_buffer, pkt->dts);
1196 // We skipped it above so we try here.
1198 // This should happen on the first packet
1199 update_initial_timestamps(s, pkt->stream_index, pkt->dts, pkt->pts, pkt);
1200 if (pkt->dts > st->cur_dts)
1201 st->cur_dts = pkt->dts;
1203 if (s->debug & FF_FDEBUG_TS)
1204 av_log(s, AV_LOG_TRACE, "OUTdelayed:%d/%d pts:%s, dts:%s cur_dts:%s\n",
1205 presentation_delayed, delay, av_ts2str(pkt->pts), av_ts2str(pkt->dts), av_ts2str(st->cur_dts));
1208 if (is_intra_only(st->codec))
1209 pkt->flags |= AV_PKT_FLAG_KEY;
1210 #if FF_API_CONVERGENCE_DURATION
1211 FF_DISABLE_DEPRECATION_WARNINGS
1213 pkt->convergence_duration = pc->convergence_duration;
1214 FF_ENABLE_DEPRECATION_WARNINGS
1218 static void free_packet_buffer(AVPacketList **pkt_buf, AVPacketList **pkt_buf_end)
1221 AVPacketList *pktl = *pkt_buf;
1222 *pkt_buf = pktl->next;
1223 av_packet_unref(&pktl->pkt);
1226 *pkt_buf_end = NULL;
1230 * Parse a packet, add all split parts to parse_queue.
1232 * @param pkt Packet to parse, NULL when flushing the parser at end of stream.
1234 static int parse_packet(AVFormatContext *s, AVPacket *pkt, int stream_index)
1236 AVPacket out_pkt = { 0 }, flush_pkt = { 0 };
1237 AVStream *st = s->streams[stream_index];
1238 uint8_t *data = pkt ? pkt->data : NULL;
1239 int size = pkt ? pkt->size : 0;
1240 int ret = 0, got_output = 0;
1243 av_init_packet(&flush_pkt);
1246 } else if (!size && st->parser->flags & PARSER_FLAG_COMPLETE_FRAMES) {
1247 // preserve 0-size sync packets
1248 compute_pkt_fields(s, st, st->parser, pkt, AV_NOPTS_VALUE, AV_NOPTS_VALUE);
1251 while (size > 0 || (pkt == &flush_pkt && got_output)) {
1253 int64_t next_pts = pkt->pts;
1254 int64_t next_dts = pkt->dts;
1256 av_init_packet(&out_pkt);
1257 len = av_parser_parse2(st->parser, st->codec,
1258 &out_pkt.data, &out_pkt.size, data, size,
1259 pkt->pts, pkt->dts, pkt->pos);
1261 pkt->pts = pkt->dts = AV_NOPTS_VALUE;
1263 /* increment read pointer */
1267 got_output = !!out_pkt.size;
1272 if (pkt->side_data) {
1273 out_pkt.side_data = pkt->side_data;
1274 out_pkt.side_data_elems = pkt->side_data_elems;
1275 pkt->side_data = NULL;
1276 pkt->side_data_elems = 0;
1279 /* set the duration */
1280 out_pkt.duration = (st->parser->flags & PARSER_FLAG_COMPLETE_FRAMES) ? pkt->duration : 0;
1281 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
1282 if (st->codec->sample_rate > 0) {
1284 av_rescale_q_rnd(st->parser->duration,
1285 (AVRational) { 1, st->codec->sample_rate },
1291 out_pkt.stream_index = st->index;
1292 out_pkt.pts = st->parser->pts;
1293 out_pkt.dts = st->parser->dts;
1294 out_pkt.pos = st->parser->pos;
1296 if (st->need_parsing == AVSTREAM_PARSE_FULL_RAW)
1297 out_pkt.pos = st->parser->frame_offset;
1299 if (st->parser->key_frame == 1 ||
1300 (st->parser->key_frame == -1 &&
1301 st->parser->pict_type == AV_PICTURE_TYPE_I))
1302 out_pkt.flags |= AV_PKT_FLAG_KEY;
1304 if (st->parser->key_frame == -1 && st->parser->pict_type ==AV_PICTURE_TYPE_NONE && (pkt->flags&AV_PKT_FLAG_KEY))
1305 out_pkt.flags |= AV_PKT_FLAG_KEY;
1307 compute_pkt_fields(s, st, st->parser, &out_pkt, next_dts, next_pts);
1309 ret = add_to_pktbuf(&s->internal->parse_queue, &out_pkt,
1310 &s->internal->parse_queue_end, 1);
1311 av_packet_unref(&out_pkt);
1316 /* end of the stream => close and free the parser */
1317 if (pkt == &flush_pkt) {
1318 av_parser_close(st->parser);
1323 av_packet_unref(pkt);
1327 static int read_from_packet_buffer(AVPacketList **pkt_buffer,
1328 AVPacketList **pkt_buffer_end,
1332 av_assert0(*pkt_buffer);
1335 *pkt_buffer = pktl->next;
1337 *pkt_buffer_end = NULL;
1342 static int64_t ts_to_samples(AVStream *st, int64_t ts)
1344 return av_rescale(ts, st->time_base.num * st->codec->sample_rate, st->time_base.den);
1347 static int read_frame_internal(AVFormatContext *s, AVPacket *pkt)
1349 int ret = 0, i, got_packet = 0;
1350 AVDictionary *metadata = NULL;
1352 av_init_packet(pkt);
1354 while (!got_packet && !s->internal->parse_queue) {
1358 /* read next packet */
1359 ret = ff_read_packet(s, &cur_pkt);
1361 if (ret == AVERROR(EAGAIN))
1363 /* flush the parsers */
1364 for (i = 0; i < s->nb_streams; i++) {
1366 if (st->parser && st->need_parsing)
1367 parse_packet(s, NULL, st->index);
1369 /* all remaining packets are now in parse_queue =>
1370 * really terminate parsing */
1374 st = s->streams[cur_pkt.stream_index];
1376 if (cur_pkt.pts != AV_NOPTS_VALUE &&
1377 cur_pkt.dts != AV_NOPTS_VALUE &&
1378 cur_pkt.pts < cur_pkt.dts) {
1379 av_log(s, AV_LOG_WARNING,
1380 "Invalid timestamps stream=%d, pts=%s, dts=%s, size=%d\n",
1381 cur_pkt.stream_index,
1382 av_ts2str(cur_pkt.pts),
1383 av_ts2str(cur_pkt.dts),
1386 if (s->debug & FF_FDEBUG_TS)
1387 av_log(s, AV_LOG_DEBUG,
1388 "ff_read_packet stream=%d, pts=%s, dts=%s, size=%d, duration=%"PRId64", flags=%d\n",
1389 cur_pkt.stream_index,
1390 av_ts2str(cur_pkt.pts),
1391 av_ts2str(cur_pkt.dts),
1392 cur_pkt.size, cur_pkt.duration, cur_pkt.flags);
1394 if (st->need_parsing && !st->parser && !(s->flags & AVFMT_FLAG_NOPARSE)) {
1395 st->parser = av_parser_init(st->codec->codec_id);
1397 av_log(s, AV_LOG_VERBOSE, "parser not found for codec "
1398 "%s, packets or times may be invalid.\n",
1399 avcodec_get_name(st->codec->codec_id));
1400 /* no parser available: just output the raw packets */
1401 st->need_parsing = AVSTREAM_PARSE_NONE;
1402 } else if (st->need_parsing == AVSTREAM_PARSE_HEADERS)
1403 st->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES;
1404 else if (st->need_parsing == AVSTREAM_PARSE_FULL_ONCE)
1405 st->parser->flags |= PARSER_FLAG_ONCE;
1406 else if (st->need_parsing == AVSTREAM_PARSE_FULL_RAW)
1407 st->parser->flags |= PARSER_FLAG_USE_CODEC_TS;
1410 if (!st->need_parsing || !st->parser) {
1411 /* no parsing needed: we just output the packet as is */
1413 compute_pkt_fields(s, st, NULL, pkt, AV_NOPTS_VALUE, AV_NOPTS_VALUE);
1414 if ((s->iformat->flags & AVFMT_GENERIC_INDEX) &&
1415 (pkt->flags & AV_PKT_FLAG_KEY) && pkt->dts != AV_NOPTS_VALUE) {
1416 ff_reduce_index(s, st->index);
1417 av_add_index_entry(st, pkt->pos, pkt->dts,
1418 0, 0, AVINDEX_KEYFRAME);
1421 } else if (st->discard < AVDISCARD_ALL) {
1422 if ((ret = parse_packet(s, &cur_pkt, cur_pkt.stream_index)) < 0)
1426 av_packet_unref(&cur_pkt);
1428 if (pkt->flags & AV_PKT_FLAG_KEY)
1429 st->skip_to_keyframe = 0;
1430 if (st->skip_to_keyframe) {
1431 av_packet_unref(&cur_pkt);
1439 if (!got_packet && s->internal->parse_queue)
1440 ret = read_from_packet_buffer(&s->internal->parse_queue, &s->internal->parse_queue_end, pkt);
1443 AVStream *st = s->streams[pkt->stream_index];
1444 int discard_padding = 0;
1445 if (st->first_discard_sample && pkt->pts != AV_NOPTS_VALUE) {
1446 int64_t pts = pkt->pts - (is_relative(pkt->pts) ? RELATIVE_TS_BASE : 0);
1447 int64_t sample = ts_to_samples(st, pts);
1448 int duration = ts_to_samples(st, pkt->duration);
1449 int64_t end_sample = sample + duration;
1450 if (duration > 0 && end_sample >= st->first_discard_sample &&
1451 sample < st->last_discard_sample)
1452 discard_padding = FFMIN(end_sample - st->first_discard_sample, duration);
1454 if (st->start_skip_samples && (pkt->pts == 0 || pkt->pts == RELATIVE_TS_BASE))
1455 st->skip_samples = st->start_skip_samples;
1456 if (st->skip_samples || discard_padding) {
1457 uint8_t *p = av_packet_new_side_data(pkt, AV_PKT_DATA_SKIP_SAMPLES, 10);
1459 AV_WL32(p, st->skip_samples);
1460 AV_WL32(p + 4, discard_padding);
1461 av_log(s, AV_LOG_DEBUG, "demuxer injecting skip %d / discard %d\n", st->skip_samples, discard_padding);
1463 st->skip_samples = 0;
1466 if (st->inject_global_side_data) {
1467 for (i = 0; i < st->nb_side_data; i++) {
1468 AVPacketSideData *src_sd = &st->side_data[i];
1471 if (av_packet_get_side_data(pkt, src_sd->type, NULL))
1474 dst_data = av_packet_new_side_data(pkt, src_sd->type, src_sd->size);
1476 av_log(s, AV_LOG_WARNING, "Could not inject global side data\n");
1480 memcpy(dst_data, src_sd->data, src_sd->size);
1482 st->inject_global_side_data = 0;
1485 if (!(s->flags & AVFMT_FLAG_KEEP_SIDE_DATA))
1486 av_packet_merge_side_data(pkt);
1489 av_opt_get_dict_val(s, "metadata", AV_OPT_SEARCH_CHILDREN, &metadata);
1491 s->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
1492 av_dict_copy(&s->metadata, metadata, 0);
1493 av_dict_free(&metadata);
1494 av_opt_set_dict_val(s, "metadata", NULL, AV_OPT_SEARCH_CHILDREN);
1497 if (s->debug & FF_FDEBUG_TS)
1498 av_log(s, AV_LOG_DEBUG,
1499 "read_frame_internal stream=%d, pts=%s, dts=%s, "
1500 "size=%d, duration=%"PRId64", flags=%d\n",
1502 av_ts2str(pkt->pts),
1503 av_ts2str(pkt->dts),
1504 pkt->size, pkt->duration, pkt->flags);
1509 int av_read_frame(AVFormatContext *s, AVPacket *pkt)
1511 const int genpts = s->flags & AVFMT_FLAG_GENPTS;
1517 ret = s->internal->packet_buffer
1518 ? read_from_packet_buffer(&s->internal->packet_buffer,
1519 &s->internal->packet_buffer_end, pkt)
1520 : read_frame_internal(s, pkt);
1527 AVPacketList *pktl = s->internal->packet_buffer;
1530 AVPacket *next_pkt = &pktl->pkt;
1532 if (next_pkt->dts != AV_NOPTS_VALUE) {
1533 int wrap_bits = s->streams[next_pkt->stream_index]->pts_wrap_bits;
1534 // last dts seen for this stream. if any of packets following
1535 // current one had no dts, we will set this to AV_NOPTS_VALUE.
1536 int64_t last_dts = next_pkt->dts;
1537 while (pktl && next_pkt->pts == AV_NOPTS_VALUE) {
1538 if (pktl->pkt.stream_index == next_pkt->stream_index &&
1539 (av_compare_mod(next_pkt->dts, pktl->pkt.dts, 2LL << (wrap_bits - 1)) < 0)) {
1540 if (av_compare_mod(pktl->pkt.pts, pktl->pkt.dts, 2LL << (wrap_bits - 1))) {
1542 next_pkt->pts = pktl->pkt.dts;
1544 if (last_dts != AV_NOPTS_VALUE) {
1545 // Once last dts was set to AV_NOPTS_VALUE, we don't change it.
1546 last_dts = pktl->pkt.dts;
1551 if (eof && next_pkt->pts == AV_NOPTS_VALUE && last_dts != AV_NOPTS_VALUE) {
1552 // Fixing the last reference frame had none pts issue (For MXF etc).
1553 // We only do this when
1555 // 2. we are not able to resolve a pts value for current packet.
1556 // 3. the packets for this stream at the end of the files had valid dts.
1557 next_pkt->pts = last_dts + next_pkt->duration;
1559 pktl = s->internal->packet_buffer;
1562 /* read packet from packet buffer, if there is data */
1563 st = s->streams[next_pkt->stream_index];
1564 if (!(next_pkt->pts == AV_NOPTS_VALUE && st->discard < AVDISCARD_ALL &&
1565 next_pkt->dts != AV_NOPTS_VALUE && !eof)) {
1566 ret = read_from_packet_buffer(&s->internal->packet_buffer,
1567 &s->internal->packet_buffer_end, pkt);
1572 ret = read_frame_internal(s, pkt);
1574 if (pktl && ret != AVERROR(EAGAIN)) {
1581 ret = add_to_pktbuf(&s->internal->packet_buffer, pkt,
1582 &s->internal->packet_buffer_end, 1);
1583 av_packet_unref(pkt);
1590 st = s->streams[pkt->stream_index];
1591 if ((s->iformat->flags & AVFMT_GENERIC_INDEX) && pkt->flags & AV_PKT_FLAG_KEY) {
1592 ff_reduce_index(s, st->index);
1593 av_add_index_entry(st, pkt->pos, pkt->dts, 0, 0, AVINDEX_KEYFRAME);
1596 if (is_relative(pkt->dts))
1597 pkt->dts -= RELATIVE_TS_BASE;
1598 if (is_relative(pkt->pts))
1599 pkt->pts -= RELATIVE_TS_BASE;
1604 /* XXX: suppress the packet queue */
1605 static void flush_packet_queue(AVFormatContext *s)
1609 free_packet_buffer(&s->internal->parse_queue, &s->internal->parse_queue_end);
1610 free_packet_buffer(&s->internal->packet_buffer, &s->internal->packet_buffer_end);
1611 free_packet_buffer(&s->internal->raw_packet_buffer, &s->internal->raw_packet_buffer_end);
1613 s->internal->raw_packet_buffer_remaining_size = RAW_PACKET_BUFFER_SIZE;
1616 /*******************************************************/
1619 int av_find_default_stream_index(AVFormatContext *s)
1623 int best_stream = 0;
1624 int best_score = INT_MIN;
1626 if (s->nb_streams <= 0)
1628 for (i = 0; i < s->nb_streams; i++) {
1631 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
1632 if (st->disposition & AV_DISPOSITION_ATTACHED_PIC)
1634 if (st->codec->width && st->codec->height)
1638 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
1639 if (st->codec->sample_rate)
1642 if (st->codec_info_nb_frames)
1645 if (st->discard != AVDISCARD_ALL)
1648 if (score > best_score) {
1656 /** Flush the frame reader. */
1657 void ff_read_frame_flush(AVFormatContext *s)
1662 flush_packet_queue(s);
1664 /* Reset read state for each stream. */
1665 for (i = 0; i < s->nb_streams; i++) {
1669 av_parser_close(st->parser);
1672 st->last_IP_pts = AV_NOPTS_VALUE;
1673 st->last_dts_for_order_check = AV_NOPTS_VALUE;
1674 if (st->first_dts == AV_NOPTS_VALUE)
1675 st->cur_dts = RELATIVE_TS_BASE;
1677 /* We set the current DTS to an unspecified origin. */
1678 st->cur_dts = AV_NOPTS_VALUE;
1680 st->probe_packets = MAX_PROBE_PACKETS;
1682 for (j = 0; j < MAX_REORDER_DELAY + 1; j++)
1683 st->pts_buffer[j] = AV_NOPTS_VALUE;
1685 if (s->internal->inject_global_side_data)
1686 st->inject_global_side_data = 1;
1688 st->skip_samples = 0;
1692 void ff_update_cur_dts(AVFormatContext *s, AVStream *ref_st, int64_t timestamp)
1696 for (i = 0; i < s->nb_streams; i++) {
1697 AVStream *st = s->streams[i];
1700 av_rescale(timestamp,
1701 st->time_base.den * (int64_t) ref_st->time_base.num,
1702 st->time_base.num * (int64_t) ref_st->time_base.den);
1706 void ff_reduce_index(AVFormatContext *s, int stream_index)
1708 AVStream *st = s->streams[stream_index];
1709 unsigned int max_entries = s->max_index_size / sizeof(AVIndexEntry);
1711 if ((unsigned) st->nb_index_entries >= max_entries) {
1713 for (i = 0; 2 * i < st->nb_index_entries; i++)
1714 st->index_entries[i] = st->index_entries[2 * i];
1715 st->nb_index_entries = i;
1719 int ff_add_index_entry(AVIndexEntry **index_entries,
1720 int *nb_index_entries,
1721 unsigned int *index_entries_allocated_size,
1722 int64_t pos, int64_t timestamp,
1723 int size, int distance, int flags)
1725 AVIndexEntry *entries, *ie;
1728 if ((unsigned) *nb_index_entries + 1 >= UINT_MAX / sizeof(AVIndexEntry))
1731 if (timestamp == AV_NOPTS_VALUE)
1732 return AVERROR(EINVAL);
1734 if (size < 0 || size > 0x3FFFFFFF)
1735 return AVERROR(EINVAL);
1737 if (is_relative(timestamp)) //FIXME this maintains previous behavior but we should shift by the correct offset once known
1738 timestamp -= RELATIVE_TS_BASE;
1740 entries = av_fast_realloc(*index_entries,
1741 index_entries_allocated_size,
1742 (*nb_index_entries + 1) *
1743 sizeof(AVIndexEntry));
1747 *index_entries = entries;
1749 index = ff_index_search_timestamp(*index_entries, *nb_index_entries,
1750 timestamp, AVSEEK_FLAG_ANY);
1753 index = (*nb_index_entries)++;
1754 ie = &entries[index];
1755 av_assert0(index == 0 || ie[-1].timestamp < timestamp);
1757 ie = &entries[index];
1758 if (ie->timestamp != timestamp) {
1759 if (ie->timestamp <= timestamp)
1761 memmove(entries + index + 1, entries + index,
1762 sizeof(AVIndexEntry) * (*nb_index_entries - index));
1763 (*nb_index_entries)++;
1764 } else if (ie->pos == pos && distance < ie->min_distance)
1765 // do not reduce the distance
1766 distance = ie->min_distance;
1770 ie->timestamp = timestamp;
1771 ie->min_distance = distance;
1778 int av_add_index_entry(AVStream *st, int64_t pos, int64_t timestamp,
1779 int size, int distance, int flags)
1781 timestamp = wrap_timestamp(st, timestamp);
1782 return ff_add_index_entry(&st->index_entries, &st->nb_index_entries,
1783 &st->index_entries_allocated_size, pos,
1784 timestamp, size, distance, flags);
1787 int ff_index_search_timestamp(const AVIndexEntry *entries, int nb_entries,
1788 int64_t wanted_timestamp, int flags)
1796 // Optimize appending index entries at the end.
1797 if (b && entries[b - 1].timestamp < wanted_timestamp)
1802 timestamp = entries[m].timestamp;
1803 if (timestamp >= wanted_timestamp)
1805 if (timestamp <= wanted_timestamp)
1808 m = (flags & AVSEEK_FLAG_BACKWARD) ? a : b;
1810 if (!(flags & AVSEEK_FLAG_ANY))
1811 while (m >= 0 && m < nb_entries &&
1812 !(entries[m].flags & AVINDEX_KEYFRAME))
1813 m += (flags & AVSEEK_FLAG_BACKWARD) ? -1 : 1;
1815 if (m == nb_entries)
1820 void ff_configure_buffers_for_index(AVFormatContext *s, int64_t time_tolerance)
1823 int64_t pos_delta = 0;
1825 //We could use URLProtocol flags here but as many user applications do not use URLProtocols this would be unreliable
1826 const char *proto = avio_find_protocol_name(s->filename);
1829 av_log(s, AV_LOG_INFO,
1830 "Protocol name not provided, cannot determine if input is local or "
1831 "a network protocol, buffers and access patterns cannot be configured "
1832 "optimally without knowing the protocol\n");
1835 if (proto && !(strcmp(proto, "file") && strcmp(proto, "pipe") && strcmp(proto, "cache")))
1838 for (ist1 = 0; ist1 < s->nb_streams; ist1++) {
1839 AVStream *st1 = s->streams[ist1];
1840 for (ist2 = 0; ist2 < s->nb_streams; ist2++) {
1841 AVStream *st2 = s->streams[ist2];
1847 for (i1 = i2 = 0; i1 < st1->nb_index_entries; i1++) {
1848 AVIndexEntry *e1 = &st1->index_entries[i1];
1849 int64_t e1_pts = av_rescale_q(e1->timestamp, st1->time_base, AV_TIME_BASE_Q);
1851 skip = FFMAX(skip, e1->size);
1852 for (; i2 < st2->nb_index_entries; i2++) {
1853 AVIndexEntry *e2 = &st2->index_entries[i2];
1854 int64_t e2_pts = av_rescale_q(e2->timestamp, st2->time_base, AV_TIME_BASE_Q);
1855 if (e2_pts - e1_pts < time_tolerance)
1857 pos_delta = FFMAX(pos_delta, e1->pos - e2->pos);
1865 /* XXX This could be adjusted depending on protocol*/
1866 if (s->pb->buffer_size < pos_delta && pos_delta < (1<<24)) {
1867 av_log(s, AV_LOG_VERBOSE, "Reconfiguring buffers to size %"PRId64"\n", pos_delta);
1868 ffio_set_buf_size(s->pb, pos_delta);
1869 s->pb->short_seek_threshold = FFMAX(s->pb->short_seek_threshold, pos_delta/2);
1872 if (skip < (1<<23)) {
1873 s->pb->short_seek_threshold = FFMAX(s->pb->short_seek_threshold, skip);
1877 int av_index_search_timestamp(AVStream *st, int64_t wanted_timestamp, int flags)
1879 return ff_index_search_timestamp(st->index_entries, st->nb_index_entries,
1880 wanted_timestamp, flags);
1883 static int64_t ff_read_timestamp(AVFormatContext *s, int stream_index, int64_t *ppos, int64_t pos_limit,
1884 int64_t (*read_timestamp)(struct AVFormatContext *, int , int64_t *, int64_t ))
1886 int64_t ts = read_timestamp(s, stream_index, ppos, pos_limit);
1887 if (stream_index >= 0)
1888 ts = wrap_timestamp(s->streams[stream_index], ts);
1892 int ff_seek_frame_binary(AVFormatContext *s, int stream_index,
1893 int64_t target_ts, int flags)
1895 AVInputFormat *avif = s->iformat;
1896 int64_t av_uninit(pos_min), av_uninit(pos_max), pos, pos_limit;
1897 int64_t ts_min, ts_max, ts;
1902 if (stream_index < 0)
1905 av_log(s, AV_LOG_TRACE, "read_seek: %d %s\n", stream_index, av_ts2str(target_ts));
1908 ts_min = AV_NOPTS_VALUE;
1909 pos_limit = -1; // GCC falsely says it may be uninitialized.
1911 st = s->streams[stream_index];
1912 if (st->index_entries) {
1915 /* FIXME: Whole function must be checked for non-keyframe entries in
1916 * index case, especially read_timestamp(). */
1917 index = av_index_search_timestamp(st, target_ts,
1918 flags | AVSEEK_FLAG_BACKWARD);
1919 index = FFMAX(index, 0);
1920 e = &st->index_entries[index];
1922 if (e->timestamp <= target_ts || e->pos == e->min_distance) {
1924 ts_min = e->timestamp;
1925 av_log(s, AV_LOG_TRACE, "using cached pos_min=0x%"PRIx64" dts_min=%s\n",
1926 pos_min, av_ts2str(ts_min));
1928 av_assert1(index == 0);
1931 index = av_index_search_timestamp(st, target_ts,
1932 flags & ~AVSEEK_FLAG_BACKWARD);
1933 av_assert0(index < st->nb_index_entries);
1935 e = &st->index_entries[index];
1936 av_assert1(e->timestamp >= target_ts);
1938 ts_max = e->timestamp;
1939 pos_limit = pos_max - e->min_distance;
1940 av_log(s, AV_LOG_TRACE, "using cached pos_max=0x%"PRIx64" pos_limit=0x%"PRIx64
1941 " dts_max=%s\n", pos_max, pos_limit, av_ts2str(ts_max));
1945 pos = ff_gen_search(s, stream_index, target_ts, pos_min, pos_max, pos_limit,
1946 ts_min, ts_max, flags, &ts, avif->read_timestamp);
1951 if ((ret = avio_seek(s->pb, pos, SEEK_SET)) < 0)
1954 ff_read_frame_flush(s);
1955 ff_update_cur_dts(s, st, ts);
1960 int ff_find_last_ts(AVFormatContext *s, int stream_index, int64_t *ts, int64_t *pos,
1961 int64_t (*read_timestamp)(struct AVFormatContext *, int , int64_t *, int64_t ))
1963 int64_t step = 1024;
1964 int64_t limit, ts_max;
1965 int64_t filesize = avio_size(s->pb);
1966 int64_t pos_max = filesize - 1;
1969 pos_max = FFMAX(0, (pos_max) - step);
1970 ts_max = ff_read_timestamp(s, stream_index,
1971 &pos_max, limit, read_timestamp);
1973 } while (ts_max == AV_NOPTS_VALUE && 2*limit > step);
1974 if (ts_max == AV_NOPTS_VALUE)
1978 int64_t tmp_pos = pos_max + 1;
1979 int64_t tmp_ts = ff_read_timestamp(s, stream_index,
1980 &tmp_pos, INT64_MAX, read_timestamp);
1981 if (tmp_ts == AV_NOPTS_VALUE)
1983 av_assert0(tmp_pos > pos_max);
1986 if (tmp_pos >= filesize)
1998 int64_t ff_gen_search(AVFormatContext *s, int stream_index, int64_t target_ts,
1999 int64_t pos_min, int64_t pos_max, int64_t pos_limit,
2000 int64_t ts_min, int64_t ts_max,
2001 int flags, int64_t *ts_ret,
2002 int64_t (*read_timestamp)(struct AVFormatContext *, int,
2003 int64_t *, int64_t))
2010 av_log(s, AV_LOG_TRACE, "gen_seek: %d %s\n", stream_index, av_ts2str(target_ts));
2012 if (ts_min == AV_NOPTS_VALUE) {
2013 pos_min = s->internal->data_offset;
2014 ts_min = ff_read_timestamp(s, stream_index, &pos_min, INT64_MAX, read_timestamp);
2015 if (ts_min == AV_NOPTS_VALUE)
2019 if (ts_min >= target_ts) {
2024 if (ts_max == AV_NOPTS_VALUE) {
2025 if ((ret = ff_find_last_ts(s, stream_index, &ts_max, &pos_max, read_timestamp)) < 0)
2027 pos_limit = pos_max;
2030 if (ts_max <= target_ts) {
2035 av_assert0(ts_min < ts_max);
2038 while (pos_min < pos_limit) {
2039 av_log(s, AV_LOG_TRACE,
2040 "pos_min=0x%"PRIx64" pos_max=0x%"PRIx64" dts_min=%s dts_max=%s\n",
2041 pos_min, pos_max, av_ts2str(ts_min), av_ts2str(ts_max));
2042 av_assert0(pos_limit <= pos_max);
2044 if (no_change == 0) {
2045 int64_t approximate_keyframe_distance = pos_max - pos_limit;
2046 // interpolate position (better than dichotomy)
2047 pos = av_rescale(target_ts - ts_min, pos_max - pos_min,
2049 pos_min - approximate_keyframe_distance;
2050 } else if (no_change == 1) {
2051 // bisection if interpolation did not change min / max pos last time
2052 pos = (pos_min + pos_limit) >> 1;
2054 /* linear search if bisection failed, can only happen if there
2055 * are very few or no keyframes between min/max */
2060 else if (pos > pos_limit)
2064 // May pass pos_limit instead of -1.
2065 ts = ff_read_timestamp(s, stream_index, &pos, INT64_MAX, read_timestamp);
2070 av_log(s, AV_LOG_TRACE, "%"PRId64" %"PRId64" %"PRId64" / %s %s %s"
2071 " target:%s limit:%"PRId64" start:%"PRId64" noc:%d\n",
2072 pos_min, pos, pos_max,
2073 av_ts2str(ts_min), av_ts2str(ts), av_ts2str(ts_max), av_ts2str(target_ts),
2074 pos_limit, start_pos, no_change);
2075 if (ts == AV_NOPTS_VALUE) {
2076 av_log(s, AV_LOG_ERROR, "read_timestamp() failed in the middle\n");
2079 if (target_ts <= ts) {
2080 pos_limit = start_pos - 1;
2084 if (target_ts >= ts) {
2090 pos = (flags & AVSEEK_FLAG_BACKWARD) ? pos_min : pos_max;
2091 ts = (flags & AVSEEK_FLAG_BACKWARD) ? ts_min : ts_max;
2094 ts_min = ff_read_timestamp(s, stream_index, &pos_min, INT64_MAX, read_timestamp);
2096 ts_max = ff_read_timestamp(s, stream_index, &pos_min, INT64_MAX, read_timestamp);
2097 av_log(s, AV_LOG_TRACE, "pos=0x%"PRIx64" %s<=%s<=%s\n",
2098 pos, av_ts2str(ts_min), av_ts2str(target_ts), av_ts2str(ts_max));
2104 static int seek_frame_byte(AVFormatContext *s, int stream_index,
2105 int64_t pos, int flags)
2107 int64_t pos_min, pos_max;
2109 pos_min = s->internal->data_offset;
2110 pos_max = avio_size(s->pb) - 1;
2114 else if (pos > pos_max)
2117 avio_seek(s->pb, pos, SEEK_SET);
2119 s->io_repositioned = 1;
2124 static int seek_frame_generic(AVFormatContext *s, int stream_index,
2125 int64_t timestamp, int flags)
2132 st = s->streams[stream_index];
2134 index = av_index_search_timestamp(st, timestamp, flags);
2136 if (index < 0 && st->nb_index_entries &&
2137 timestamp < st->index_entries[0].timestamp)
2140 if (index < 0 || index == st->nb_index_entries - 1) {
2144 if (st->nb_index_entries) {
2145 av_assert0(st->index_entries);
2146 ie = &st->index_entries[st->nb_index_entries - 1];
2147 if ((ret = avio_seek(s->pb, ie->pos, SEEK_SET)) < 0)
2149 ff_update_cur_dts(s, st, ie->timestamp);
2151 if ((ret = avio_seek(s->pb, s->internal->data_offset, SEEK_SET)) < 0)
2157 read_status = av_read_frame(s, &pkt);
2158 } while (read_status == AVERROR(EAGAIN));
2159 if (read_status < 0)
2161 if (stream_index == pkt.stream_index && pkt.dts > timestamp) {
2162 if (pkt.flags & AV_PKT_FLAG_KEY) {
2163 av_packet_unref(&pkt);
2166 if (nonkey++ > 1000 && st->codec->codec_id != AV_CODEC_ID_CDGRAPHICS) {
2167 av_log(s, AV_LOG_ERROR,"seek_frame_generic failed as this stream seems to contain no keyframes after the target timestamp, %d non keyframes found\n", nonkey);
2168 av_packet_unref(&pkt);
2172 av_packet_unref(&pkt);
2174 index = av_index_search_timestamp(st, timestamp, flags);
2179 ff_read_frame_flush(s);
2180 if (s->iformat->read_seek)
2181 if (s->iformat->read_seek(s, stream_index, timestamp, flags) >= 0)
2183 ie = &st->index_entries[index];
2184 if ((ret = avio_seek(s->pb, ie->pos, SEEK_SET)) < 0)
2186 ff_update_cur_dts(s, st, ie->timestamp);
2191 static int seek_frame_internal(AVFormatContext *s, int stream_index,
2192 int64_t timestamp, int flags)
2197 if (flags & AVSEEK_FLAG_BYTE) {
2198 if (s->iformat->flags & AVFMT_NO_BYTE_SEEK)
2200 ff_read_frame_flush(s);
2201 return seek_frame_byte(s, stream_index, timestamp, flags);
2204 if (stream_index < 0) {
2205 stream_index = av_find_default_stream_index(s);
2206 if (stream_index < 0)
2209 st = s->streams[stream_index];
2210 /* timestamp for default must be expressed in AV_TIME_BASE units */
2211 timestamp = av_rescale(timestamp, st->time_base.den,
2212 AV_TIME_BASE * (int64_t) st->time_base.num);
2215 /* first, we try the format specific seek */
2216 if (s->iformat->read_seek) {
2217 ff_read_frame_flush(s);
2218 ret = s->iformat->read_seek(s, stream_index, timestamp, flags);
2224 if (s->iformat->read_timestamp &&
2225 !(s->iformat->flags & AVFMT_NOBINSEARCH)) {
2226 ff_read_frame_flush(s);
2227 return ff_seek_frame_binary(s, stream_index, timestamp, flags);
2228 } else if (!(s->iformat->flags & AVFMT_NOGENSEARCH)) {
2229 ff_read_frame_flush(s);
2230 return seek_frame_generic(s, stream_index, timestamp, flags);
2235 int av_seek_frame(AVFormatContext *s, int stream_index,
2236 int64_t timestamp, int flags)
2240 if (s->iformat->read_seek2 && !s->iformat->read_seek) {
2241 int64_t min_ts = INT64_MIN, max_ts = INT64_MAX;
2242 if ((flags & AVSEEK_FLAG_BACKWARD))
2246 return avformat_seek_file(s, stream_index, min_ts, timestamp, max_ts,
2247 flags & ~AVSEEK_FLAG_BACKWARD);
2250 ret = seek_frame_internal(s, stream_index, timestamp, flags);
2253 ret = avformat_queue_attached_pictures(s);
2258 int avformat_seek_file(AVFormatContext *s, int stream_index, int64_t min_ts,
2259 int64_t ts, int64_t max_ts, int flags)
2261 if (min_ts > ts || max_ts < ts)
2263 if (stream_index < -1 || stream_index >= (int)s->nb_streams)
2264 return AVERROR(EINVAL);
2267 flags |= AVSEEK_FLAG_ANY;
2268 flags &= ~AVSEEK_FLAG_BACKWARD;
2270 if (s->iformat->read_seek2) {
2272 ff_read_frame_flush(s);
2274 if (stream_index == -1 && s->nb_streams == 1) {
2275 AVRational time_base = s->streams[0]->time_base;
2276 ts = av_rescale_q(ts, AV_TIME_BASE_Q, time_base);
2277 min_ts = av_rescale_rnd(min_ts, time_base.den,
2278 time_base.num * (int64_t)AV_TIME_BASE,
2279 AV_ROUND_UP | AV_ROUND_PASS_MINMAX);
2280 max_ts = av_rescale_rnd(max_ts, time_base.den,
2281 time_base.num * (int64_t)AV_TIME_BASE,
2282 AV_ROUND_DOWN | AV_ROUND_PASS_MINMAX);
2285 ret = s->iformat->read_seek2(s, stream_index, min_ts,
2289 ret = avformat_queue_attached_pictures(s);
2293 if (s->iformat->read_timestamp) {
2294 // try to seek via read_timestamp()
2297 // Fall back on old API if new is not implemented but old is.
2298 // Note the old API has somewhat different semantics.
2299 if (s->iformat->read_seek || 1) {
2300 int dir = (ts - (uint64_t)min_ts > (uint64_t)max_ts - ts ? AVSEEK_FLAG_BACKWARD : 0);
2301 int ret = av_seek_frame(s, stream_index, ts, flags | dir);
2302 if (ret<0 && ts != min_ts && max_ts != ts) {
2303 ret = av_seek_frame(s, stream_index, dir ? max_ts : min_ts, flags | dir);
2305 ret = av_seek_frame(s, stream_index, ts, flags | (dir^AVSEEK_FLAG_BACKWARD));
2310 // try some generic seek like seek_frame_generic() but with new ts semantics
2311 return -1; //unreachable
2314 int avformat_flush(AVFormatContext *s)
2316 ff_read_frame_flush(s);
2320 /*******************************************************/
2323 * Return TRUE if the stream has accurate duration in any stream.
2325 * @return TRUE if the stream has accurate duration for at least one component.
2327 static int has_duration(AVFormatContext *ic)
2332 for (i = 0; i < ic->nb_streams; i++) {
2333 st = ic->streams[i];
2334 if (st->duration != AV_NOPTS_VALUE)
2337 if (ic->duration != AV_NOPTS_VALUE)
2343 * Estimate the stream timings from the one of each components.
2345 * Also computes the global bitrate if possible.
2347 static void update_stream_timings(AVFormatContext *ic)
2349 int64_t start_time, start_time1, start_time_text, end_time, end_time1;
2350 int64_t duration, duration1, filesize;
2355 start_time = INT64_MAX;
2356 start_time_text = INT64_MAX;
2357 end_time = INT64_MIN;
2358 duration = INT64_MIN;
2359 for (i = 0; i < ic->nb_streams; i++) {
2360 st = ic->streams[i];
2361 if (st->start_time != AV_NOPTS_VALUE && st->time_base.den) {
2362 start_time1 = av_rescale_q(st->start_time, st->time_base,
2364 if (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE || st->codec->codec_type == AVMEDIA_TYPE_DATA) {
2365 if (start_time1 < start_time_text)
2366 start_time_text = start_time1;
2368 start_time = FFMIN(start_time, start_time1);
2369 end_time1 = av_rescale_q_rnd(st->duration, st->time_base,
2371 AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
2372 if (end_time1 != AV_NOPTS_VALUE) {
2373 end_time1 += start_time1;
2374 end_time = FFMAX(end_time, end_time1);
2376 for (p = NULL; (p = av_find_program_from_stream(ic, p, i)); ) {
2377 if (p->start_time == AV_NOPTS_VALUE || p->start_time > start_time1)
2378 p->start_time = start_time1;
2379 if (p->end_time < end_time1)
2380 p->end_time = end_time1;
2383 if (st->duration != AV_NOPTS_VALUE) {
2384 duration1 = av_rescale_q(st->duration, st->time_base,
2386 duration = FFMAX(duration, duration1);
2389 if (start_time == INT64_MAX || (start_time > start_time_text && start_time - start_time_text < AV_TIME_BASE))
2390 start_time = start_time_text;
2391 else if (start_time > start_time_text)
2392 av_log(ic, AV_LOG_VERBOSE, "Ignoring outlier non primary stream starttime %f\n", start_time_text / (float)AV_TIME_BASE);
2394 if (start_time != INT64_MAX) {
2395 ic->start_time = start_time;
2396 if (end_time != INT64_MIN) {
2397 if (ic->nb_programs) {
2398 for (i = 0; i < ic->nb_programs; i++) {
2399 p = ic->programs[i];
2400 if (p->start_time != AV_NOPTS_VALUE && p->end_time > p->start_time)
2401 duration = FFMAX(duration, p->end_time - p->start_time);
2404 duration = FFMAX(duration, end_time - start_time);
2407 if (duration != INT64_MIN && duration > 0 && ic->duration == AV_NOPTS_VALUE) {
2408 ic->duration = duration;
2410 if (ic->pb && (filesize = avio_size(ic->pb)) > 0 && ic->duration != AV_NOPTS_VALUE) {
2411 /* compute the bitrate */
2412 double bitrate = (double) filesize * 8.0 * AV_TIME_BASE /
2413 (double) ic->duration;
2414 if (bitrate >= 0 && bitrate <= INT64_MAX)
2415 ic->bit_rate = bitrate;
2419 static void fill_all_stream_timings(AVFormatContext *ic)
2424 update_stream_timings(ic);
2425 for (i = 0; i < ic->nb_streams; i++) {
2426 st = ic->streams[i];
2427 if (st->start_time == AV_NOPTS_VALUE) {
2428 if (ic->start_time != AV_NOPTS_VALUE)
2429 st->start_time = av_rescale_q(ic->start_time, AV_TIME_BASE_Q,
2431 if (ic->duration != AV_NOPTS_VALUE)
2432 st->duration = av_rescale_q(ic->duration, AV_TIME_BASE_Q,
2438 static void estimate_timings_from_bit_rate(AVFormatContext *ic)
2440 int64_t filesize, duration;
2441 int i, show_warning = 0;
2444 /* if bit_rate is already set, we believe it */
2445 if (ic->bit_rate <= 0) {
2447 for (i = 0; i < ic->nb_streams; i++) {
2448 st = ic->streams[i];
2449 if (st->codec->bit_rate > 0) {
2450 if (INT_MAX - st->codec->bit_rate < bit_rate) {
2454 bit_rate += st->codec->bit_rate;
2455 } else if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && st->codec_info_nb_frames > 1) {
2456 // If we have a videostream with packets but without a bitrate
2457 // then consider the sum not known
2462 ic->bit_rate = bit_rate;
2465 /* if duration is already set, we believe it */
2466 if (ic->duration == AV_NOPTS_VALUE &&
2467 ic->bit_rate != 0) {
2468 filesize = ic->pb ? avio_size(ic->pb) : 0;
2469 if (filesize > ic->internal->data_offset) {
2470 filesize -= ic->internal->data_offset;
2471 for (i = 0; i < ic->nb_streams; i++) {
2472 st = ic->streams[i];
2473 if ( st->time_base.num <= INT64_MAX / ic->bit_rate
2474 && st->duration == AV_NOPTS_VALUE) {
2475 duration = av_rescale(8 * filesize, st->time_base.den,
2477 (int64_t) st->time_base.num);
2478 st->duration = duration;
2485 av_log(ic, AV_LOG_WARNING,
2486 "Estimating duration from bitrate, this may be inaccurate\n");
2489 #define DURATION_MAX_READ_SIZE 250000LL
2490 #define DURATION_MAX_RETRY 6
2492 /* only usable for MPEG-PS streams */
2493 static void estimate_timings_from_pts(AVFormatContext *ic, int64_t old_offset)
2495 AVPacket pkt1, *pkt = &pkt1;
2497 int num, den, read_size, i, ret;
2498 int found_duration = 0;
2500 int64_t filesize, offset, duration;
2503 /* flush packet queue */
2504 flush_packet_queue(ic);
2506 for (i = 0; i < ic->nb_streams; i++) {
2507 st = ic->streams[i];
2508 if (st->start_time == AV_NOPTS_VALUE &&
2509 st->first_dts == AV_NOPTS_VALUE &&
2510 st->codec->codec_type != AVMEDIA_TYPE_UNKNOWN)
2511 av_log(st->codec, AV_LOG_WARNING,
2512 "start time for stream %d is not set in estimate_timings_from_pts\n", i);
2515 av_parser_close(st->parser);
2520 av_opt_set(ic, "skip_changes", "1", AV_OPT_SEARCH_CHILDREN);
2521 /* estimate the end time (duration) */
2522 /* XXX: may need to support wrapping */
2523 filesize = ic->pb ? avio_size(ic->pb) : 0;
2525 is_end = found_duration;
2526 offset = filesize - (DURATION_MAX_READ_SIZE << retry);
2530 avio_seek(ic->pb, offset, SEEK_SET);
2533 if (read_size >= DURATION_MAX_READ_SIZE << (FFMAX(retry - 1, 0)))
2537 ret = ff_read_packet(ic, pkt);
2538 } while (ret == AVERROR(EAGAIN));
2541 read_size += pkt->size;
2542 st = ic->streams[pkt->stream_index];
2543 if (pkt->pts != AV_NOPTS_VALUE &&
2544 (st->start_time != AV_NOPTS_VALUE ||
2545 st->first_dts != AV_NOPTS_VALUE)) {
2546 if (pkt->duration == 0) {
2547 ff_compute_frame_duration(ic, &num, &den, st, st->parser, pkt);
2549 pkt->duration = av_rescale_rnd(1,
2550 num * (int64_t) st->time_base.den,
2551 den * (int64_t) st->time_base.num,
2555 duration = pkt->pts + pkt->duration;
2557 if (st->start_time != AV_NOPTS_VALUE)
2558 duration -= st->start_time;
2560 duration -= st->first_dts;
2562 if (st->duration == AV_NOPTS_VALUE || st->info->last_duration<= 0 ||
2563 (st->duration < duration && FFABS(duration - st->info->last_duration) < 60LL*st->time_base.den / st->time_base.num))
2564 st->duration = duration;
2565 st->info->last_duration = duration;
2568 av_packet_unref(pkt);
2571 /* check if all audio/video streams have valid duration */
2574 for (i = 0; i < ic->nb_streams; i++) {
2575 st = ic->streams[i];
2576 switch (st->codec->codec_type) {
2577 case AVMEDIA_TYPE_VIDEO:
2578 case AVMEDIA_TYPE_AUDIO:
2579 if (st->duration == AV_NOPTS_VALUE)
2586 ++retry <= DURATION_MAX_RETRY);
2588 av_opt_set(ic, "skip_changes", "0", AV_OPT_SEARCH_CHILDREN);
2590 /* warn about audio/video streams which duration could not be estimated */
2591 for (i = 0; i < ic->nb_streams; i++) {
2592 st = ic->streams[i];
2593 if (st->duration == AV_NOPTS_VALUE) {
2594 switch (st->codec->codec_type) {
2595 case AVMEDIA_TYPE_VIDEO:
2596 case AVMEDIA_TYPE_AUDIO:
2597 if (st->start_time != AV_NOPTS_VALUE || st->first_dts != AV_NOPTS_VALUE) {
2598 av_log(ic, AV_LOG_DEBUG, "stream %d : no PTS found at end of file, duration not set\n", i);
2600 av_log(ic, AV_LOG_DEBUG, "stream %d : no TS found at start of file, duration not set\n", i);
2604 fill_all_stream_timings(ic);
2606 avio_seek(ic->pb, old_offset, SEEK_SET);
2607 for (i = 0; i < ic->nb_streams; i++) {
2610 st = ic->streams[i];
2611 st->cur_dts = st->first_dts;
2612 st->last_IP_pts = AV_NOPTS_VALUE;
2613 st->last_dts_for_order_check = AV_NOPTS_VALUE;
2614 for (j = 0; j < MAX_REORDER_DELAY + 1; j++)
2615 st->pts_buffer[j] = AV_NOPTS_VALUE;
2619 static void estimate_timings(AVFormatContext *ic, int64_t old_offset)
2623 /* get the file size, if possible */
2624 if (ic->iformat->flags & AVFMT_NOFILE) {
2627 file_size = avio_size(ic->pb);
2628 file_size = FFMAX(0, file_size);
2631 if ((!strcmp(ic->iformat->name, "mpeg") ||
2632 !strcmp(ic->iformat->name, "mpegts")) &&
2633 file_size && ic->pb->seekable) {
2634 /* get accurate estimate from the PTSes */
2635 estimate_timings_from_pts(ic, old_offset);
2636 ic->duration_estimation_method = AVFMT_DURATION_FROM_PTS;
2637 } else if (has_duration(ic)) {
2638 /* at least one component has timings - we use them for all
2640 fill_all_stream_timings(ic);
2641 ic->duration_estimation_method = AVFMT_DURATION_FROM_STREAM;
2643 /* less precise: use bitrate info */
2644 estimate_timings_from_bit_rate(ic);
2645 ic->duration_estimation_method = AVFMT_DURATION_FROM_BITRATE;
2647 update_stream_timings(ic);
2651 AVStream av_unused *st;
2652 for (i = 0; i < ic->nb_streams; i++) {
2653 st = ic->streams[i];
2654 av_log(ic, AV_LOG_TRACE, "%d: start_time: %0.3f duration: %0.3f\n", i,
2655 (double) st->start_time / AV_TIME_BASE,
2656 (double) st->duration / AV_TIME_BASE);
2658 av_log(ic, AV_LOG_TRACE,
2659 "stream: start_time: %0.3f duration: %0.3f bitrate=%"PRId64" kb/s\n",
2660 (double) ic->start_time / AV_TIME_BASE,
2661 (double) ic->duration / AV_TIME_BASE,
2662 (int64_t)ic->bit_rate / 1000);
2666 static int has_codec_parameters(AVStream *st, const char **errmsg_ptr)
2668 AVCodecContext *avctx = st->codec;
2670 #define FAIL(errmsg) do { \
2672 *errmsg_ptr = errmsg; \
2676 if ( avctx->codec_id == AV_CODEC_ID_NONE
2677 && avctx->codec_type != AVMEDIA_TYPE_DATA)
2678 FAIL("unknown codec");
2679 switch (avctx->codec_type) {
2680 case AVMEDIA_TYPE_AUDIO:
2681 if (!avctx->frame_size && determinable_frame_size(avctx))
2682 FAIL("unspecified frame size");
2683 if (st->info->found_decoder >= 0 &&
2684 avctx->sample_fmt == AV_SAMPLE_FMT_NONE)
2685 FAIL("unspecified sample format");
2686 if (!avctx->sample_rate)
2687 FAIL("unspecified sample rate");
2688 if (!avctx->channels)
2689 FAIL("unspecified number of channels");
2690 if (st->info->found_decoder >= 0 && !st->nb_decoded_frames && avctx->codec_id == AV_CODEC_ID_DTS)
2691 FAIL("no decodable DTS frames");
2693 case AVMEDIA_TYPE_VIDEO:
2695 FAIL("unspecified size");
2696 if (st->info->found_decoder >= 0 && avctx->pix_fmt == AV_PIX_FMT_NONE)
2697 FAIL("unspecified pixel format");
2698 if (st->codec->codec_id == AV_CODEC_ID_RV30 || st->codec->codec_id == AV_CODEC_ID_RV40)
2699 if (!st->sample_aspect_ratio.num && !st->codec->sample_aspect_ratio.num && !st->codec_info_nb_frames)
2700 FAIL("no frame in rv30/40 and no sar");
2702 case AVMEDIA_TYPE_SUBTITLE:
2703 if (avctx->codec_id == AV_CODEC_ID_HDMV_PGS_SUBTITLE && !avctx->width)
2704 FAIL("unspecified size");
2706 case AVMEDIA_TYPE_DATA:
2707 if (avctx->codec_id == AV_CODEC_ID_NONE) return 1;
2713 /* returns 1 or 0 if or if not decoded data was returned, or a negative error */
2714 static int try_decode_frame(AVFormatContext *s, AVStream *st, AVPacket *avpkt,
2715 AVDictionary **options)
2717 const AVCodec *codec;
2718 int got_picture = 1, ret = 0;
2719 AVFrame *frame = av_frame_alloc();
2720 AVSubtitle subtitle;
2721 AVPacket pkt = *avpkt;
2722 int do_skip_frame = 0;
2723 enum AVDiscard skip_frame;
2726 return AVERROR(ENOMEM);
2728 if (!avcodec_is_open(st->codec) &&
2729 st->info->found_decoder <= 0 &&
2730 (st->codec->codec_id != -st->info->found_decoder || !st->codec->codec_id)) {
2731 AVDictionary *thread_opt = NULL;
2733 codec = find_decoder(s, st, st->codec->codec_id);
2736 st->info->found_decoder = -st->codec->codec_id;
2741 /* Force thread count to 1 since the H.264 decoder will not extract
2742 * SPS and PPS to extradata during multi-threaded decoding. */
2743 av_dict_set(options ? options : &thread_opt, "threads", "1", 0);
2744 if (s->codec_whitelist)
2745 av_dict_set(options ? options : &thread_opt, "codec_whitelist", s->codec_whitelist, 0);
2746 ret = avcodec_open2(st->codec, codec, options ? options : &thread_opt);
2748 av_dict_free(&thread_opt);
2750 st->info->found_decoder = -st->codec->codec_id;
2753 st->info->found_decoder = 1;
2754 } else if (!st->info->found_decoder)
2755 st->info->found_decoder = 1;
2757 if (st->info->found_decoder < 0) {
2762 if (st->codec->codec->caps_internal & FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM) {
2764 skip_frame = st->codec->skip_frame;
2765 st->codec->skip_frame = AVDISCARD_ALL;
2768 while ((pkt.size > 0 || (!pkt.data && got_picture)) &&
2770 (!has_codec_parameters(st, NULL) || !has_decode_delay_been_guessed(st) ||
2771 (!st->codec_info_nb_frames &&
2772 (st->codec->codec->capabilities & AV_CODEC_CAP_CHANNEL_CONF)))) {
2774 switch (st->codec->codec_type) {
2775 case AVMEDIA_TYPE_VIDEO:
2776 ret = avcodec_decode_video2(st->codec, frame,
2777 &got_picture, &pkt);
2779 case AVMEDIA_TYPE_AUDIO:
2780 ret = avcodec_decode_audio4(st->codec, frame, &got_picture, &pkt);
2782 case AVMEDIA_TYPE_SUBTITLE:
2783 ret = avcodec_decode_subtitle2(st->codec, &subtitle,
2784 &got_picture, &pkt);
2792 st->nb_decoded_frames++;
2799 if (!pkt.data && !got_picture)
2803 if (do_skip_frame) {
2804 st->codec->skip_frame = skip_frame;
2807 av_frame_free(&frame);
2811 unsigned int ff_codec_get_tag(const AVCodecTag *tags, enum AVCodecID id)
2813 while (tags->id != AV_CODEC_ID_NONE) {
2821 enum AVCodecID ff_codec_get_id(const AVCodecTag *tags, unsigned int tag)
2824 for (i = 0; tags[i].id != AV_CODEC_ID_NONE; i++)
2825 if (tag == tags[i].tag)
2827 for (i = 0; tags[i].id != AV_CODEC_ID_NONE; i++)
2828 if (avpriv_toupper4(tag) == avpriv_toupper4(tags[i].tag))
2830 return AV_CODEC_ID_NONE;
2833 enum AVCodecID ff_get_pcm_codec_id(int bps, int flt, int be, int sflags)
2838 return be ? AV_CODEC_ID_PCM_F32BE : AV_CODEC_ID_PCM_F32LE;
2840 return be ? AV_CODEC_ID_PCM_F64BE : AV_CODEC_ID_PCM_F64LE;
2842 return AV_CODEC_ID_NONE;
2847 if (sflags & (1 << (bps - 1))) {
2850 return AV_CODEC_ID_PCM_S8;
2852 return be ? AV_CODEC_ID_PCM_S16BE : AV_CODEC_ID_PCM_S16LE;
2854 return be ? AV_CODEC_ID_PCM_S24BE : AV_CODEC_ID_PCM_S24LE;
2856 return be ? AV_CODEC_ID_PCM_S32BE : AV_CODEC_ID_PCM_S32LE;
2858 return AV_CODEC_ID_NONE;
2863 return AV_CODEC_ID_PCM_U8;
2865 return be ? AV_CODEC_ID_PCM_U16BE : AV_CODEC_ID_PCM_U16LE;
2867 return be ? AV_CODEC_ID_PCM_U24BE : AV_CODEC_ID_PCM_U24LE;
2869 return be ? AV_CODEC_ID_PCM_U32BE : AV_CODEC_ID_PCM_U32LE;
2871 return AV_CODEC_ID_NONE;
2877 unsigned int av_codec_get_tag(const AVCodecTag *const *tags, enum AVCodecID id)
2880 if (!av_codec_get_tag2(tags, id, &tag))
2885 int av_codec_get_tag2(const AVCodecTag * const *tags, enum AVCodecID id,
2889 for (i = 0; tags && tags[i]; i++) {
2890 const AVCodecTag *codec_tags = tags[i];
2891 while (codec_tags->id != AV_CODEC_ID_NONE) {
2892 if (codec_tags->id == id) {
2893 *tag = codec_tags->tag;
2902 enum AVCodecID av_codec_get_id(const AVCodecTag *const *tags, unsigned int tag)
2905 for (i = 0; tags && tags[i]; i++) {
2906 enum AVCodecID id = ff_codec_get_id(tags[i], tag);
2907 if (id != AV_CODEC_ID_NONE)
2910 return AV_CODEC_ID_NONE;
2913 static void compute_chapters_end(AVFormatContext *s)
2916 int64_t max_time = 0;
2918 if (s->duration > 0)
2919 max_time = s->duration +
2920 ((s->start_time == AV_NOPTS_VALUE) ? 0 : s->start_time);
2922 for (i = 0; i < s->nb_chapters; i++)
2923 if (s->chapters[i]->end == AV_NOPTS_VALUE) {
2924 AVChapter *ch = s->chapters[i];
2925 int64_t end = max_time ? av_rescale_q(max_time, AV_TIME_BASE_Q,
2929 for (j = 0; j < s->nb_chapters; j++) {
2930 AVChapter *ch1 = s->chapters[j];
2931 int64_t next_start = av_rescale_q(ch1->start, ch1->time_base,
2933 if (j != i && next_start > ch->start && next_start < end)
2936 ch->end = (end == INT64_MAX) ? ch->start : end;
2940 static int get_std_framerate(int i)
2943 return (i + 1) * 1001;
2947 return (i + 31) * 1001 * 12;
2951 return ((const int[]) { 80, 120, 240})[i] * 1001 * 12;
2955 return ((const int[]) { 24, 30, 60, 12, 15, 48 })[i] * 1000 * 12;
2958 /* Is the time base unreliable?
2959 * This is a heuristic to balance between quick acceptance of the values in
2960 * the headers vs. some extra checks.
2961 * Old DivX and Xvid often have nonsense timebases like 1fps or 2fps.
2962 * MPEG-2 commonly misuses field repeat flags to store different framerates.
2963 * And there are "variable" fps files this needs to detect as well. */
2964 static int tb_unreliable(AVCodecContext *c)
2966 if (c->time_base.den >= 101LL * c->time_base.num ||
2967 c->time_base.den < 5LL * c->time_base.num ||
2968 // c->codec_tag == AV_RL32("DIVX") ||
2969 // c->codec_tag == AV_RL32("XVID") ||
2970 c->codec_tag == AV_RL32("mp4v") ||
2971 c->codec_id == AV_CODEC_ID_MPEG2VIDEO ||
2972 c->codec_id == AV_CODEC_ID_GIF ||
2973 c->codec_id == AV_CODEC_ID_HEVC ||
2974 c->codec_id == AV_CODEC_ID_H264)
2979 int ff_alloc_extradata(AVCodecContext *avctx, int size)
2983 if (size < 0 || size >= INT32_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
2984 avctx->extradata = NULL;
2985 avctx->extradata_size = 0;
2986 return AVERROR(EINVAL);
2988 avctx->extradata = av_malloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
2989 if (avctx->extradata) {
2990 memset(avctx->extradata + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
2991 avctx->extradata_size = size;
2994 avctx->extradata_size = 0;
2995 ret = AVERROR(ENOMEM);
3000 int ff_get_extradata(AVCodecContext *avctx, AVIOContext *pb, int size)
3002 int ret = ff_alloc_extradata(avctx, size);
3005 ret = avio_read(pb, avctx->extradata, size);
3007 av_freep(&avctx->extradata);
3008 avctx->extradata_size = 0;
3009 av_log(avctx, AV_LOG_ERROR, "Failed to read extradata of size %d\n", size);
3010 return ret < 0 ? ret : AVERROR_INVALIDDATA;
3016 int ff_rfps_add_frame(AVFormatContext *ic, AVStream *st, int64_t ts)
3019 int64_t last = st->info->last_dts;
3021 if ( ts != AV_NOPTS_VALUE && last != AV_NOPTS_VALUE && ts > last
3022 && ts - (uint64_t)last < INT64_MAX) {
3023 double dts = (is_relative(ts) ? ts - RELATIVE_TS_BASE : ts) * av_q2d(st->time_base);
3024 int64_t duration = ts - last;
3026 if (!st->info->duration_error)
3027 st->info->duration_error = av_mallocz(sizeof(st->info->duration_error[0])*2);
3028 if (!st->info->duration_error)
3029 return AVERROR(ENOMEM);
3031 // if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
3032 // av_log(NULL, AV_LOG_ERROR, "%f\n", dts);
3033 for (i = 0; i<MAX_STD_TIMEBASES; i++) {
3034 if (st->info->duration_error[0][1][i] < 1e10) {
3035 int framerate = get_std_framerate(i);
3036 double sdts = dts*framerate/(1001*12);
3037 for (j= 0; j<2; j++) {
3038 int64_t ticks = llrint(sdts+j*0.5);
3039 double error= sdts - ticks + j*0.5;
3040 st->info->duration_error[j][0][i] += error;
3041 st->info->duration_error[j][1][i] += error*error;
3045 st->info->duration_count++;
3046 st->info->rfps_duration_sum += duration;
3048 if (st->info->duration_count % 10 == 0) {
3049 int n = st->info->duration_count;
3050 for (i = 0; i<MAX_STD_TIMEBASES; i++) {
3051 if (st->info->duration_error[0][1][i] < 1e10) {
3052 double a0 = st->info->duration_error[0][0][i] / n;
3053 double error0 = st->info->duration_error[0][1][i] / n - a0*a0;
3054 double a1 = st->info->duration_error[1][0][i] / n;
3055 double error1 = st->info->duration_error[1][1][i] / n - a1*a1;
3056 if (error0 > 0.04 && error1 > 0.04) {
3057 st->info->duration_error[0][1][i] = 2e10;
3058 st->info->duration_error[1][1][i] = 2e10;
3064 // ignore the first 4 values, they might have some random jitter
3065 if (st->info->duration_count > 3 && is_relative(ts) == is_relative(last))
3066 st->info->duration_gcd = av_gcd(st->info->duration_gcd, duration);
3068 if (ts != AV_NOPTS_VALUE)
3069 st->info->last_dts = ts;
3074 void ff_rfps_calculate(AVFormatContext *ic)
3078 for (i = 0; i < ic->nb_streams; i++) {
3079 AVStream *st = ic->streams[i];
3081 if (st->codec->codec_type != AVMEDIA_TYPE_VIDEO)
3083 // the check for tb_unreliable() is not completely correct, since this is not about handling
3084 // a unreliable/inexact time base, but a time base that is finer than necessary, as e.g.
3085 // ipmovie.c produces.
3086 if (tb_unreliable(st->codec) && st->info->duration_count > 15 && st->info->duration_gcd > FFMAX(1, st->time_base.den/(500LL*st->time_base.num)) && !st->r_frame_rate.num)
3087 av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den, st->time_base.den, st->time_base.num * st->info->duration_gcd, INT_MAX);
3088 if (st->info->duration_count>1 && !st->r_frame_rate.num
3089 && tb_unreliable(st->codec)) {
3091 double best_error= 0.01;
3092 AVRational ref_rate = st->r_frame_rate.num ? st->r_frame_rate : av_inv_q(st->time_base);
3094 for (j= 0; j<MAX_STD_TIMEBASES; j++) {
3097 if (st->info->codec_info_duration && st->info->codec_info_duration*av_q2d(st->time_base) < (1001*12.0)/get_std_framerate(j))
3099 if (!st->info->codec_info_duration && get_std_framerate(j) < 1001*12)
3102 if (av_q2d(st->time_base) * st->info->rfps_duration_sum / st->info->duration_count < (1001*12.0 * 0.8)/get_std_framerate(j))
3105 for (k= 0; k<2; k++) {
3106 int n = st->info->duration_count;
3107 double a= st->info->duration_error[k][0][j] / n;
3108 double error= st->info->duration_error[k][1][j]/n - a*a;
3110 if (error < best_error && best_error> 0.000000001) {
3112 num = get_std_framerate(j);
3115 av_log(ic, AV_LOG_DEBUG, "rfps: %f %f\n", get_std_framerate(j) / 12.0/1001, error);
3118 // do not increase frame rate by more than 1 % in order to match a standard rate.
3119 if (num && (!ref_rate.num || (double)num/(12*1001) < 1.01 * av_q2d(ref_rate)))
3120 av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den, num, 12*1001, INT_MAX);
3122 if ( !st->avg_frame_rate.num
3123 && st->r_frame_rate.num && st->info->rfps_duration_sum
3124 && st->info->codec_info_duration <= 0
3125 && st->info->duration_count > 2
3126 && fabs(1.0 / (av_q2d(st->r_frame_rate) * av_q2d(st->time_base)) - st->info->rfps_duration_sum / (double)st->info->duration_count) <= 1.0
3128 av_log(ic, AV_LOG_DEBUG, "Setting avg frame rate based on r frame rate\n");
3129 st->avg_frame_rate = st->r_frame_rate;
3132 av_freep(&st->info->duration_error);
3133 st->info->last_dts = AV_NOPTS_VALUE;
3134 st->info->duration_count = 0;
3135 st->info->rfps_duration_sum = 0;
3139 int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
3141 int i, count, ret = 0, j;
3144 AVPacket pkt1, *pkt;
3145 int64_t old_offset = avio_tell(ic->pb);
3146 // new streams might appear, no options for those
3147 int orig_nb_streams = ic->nb_streams;
3149 int64_t max_analyze_duration = ic->max_analyze_duration;
3150 int64_t max_stream_analyze_duration;
3151 int64_t max_subtitle_analyze_duration;
3152 int64_t probesize = ic->probesize;
3154 flush_codecs = probesize > 0;
3156 av_opt_set(ic, "skip_clear", "1", AV_OPT_SEARCH_CHILDREN);
3158 max_stream_analyze_duration = max_analyze_duration;
3159 max_subtitle_analyze_duration = max_analyze_duration;
3160 if (!max_analyze_duration) {
3161 max_stream_analyze_duration =
3162 max_analyze_duration = 5*AV_TIME_BASE;
3163 max_subtitle_analyze_duration = 30*AV_TIME_BASE;
3164 if (!strcmp(ic->iformat->name, "flv"))
3165 max_stream_analyze_duration = 90*AV_TIME_BASE;
3169 av_log(ic, AV_LOG_DEBUG, "Before avformat_find_stream_info() pos: %"PRId64" bytes read:%"PRId64" seeks:%d\n",
3170 avio_tell(ic->pb), ic->pb->bytes_read, ic->pb->seek_count);
3172 for (i = 0; i < ic->nb_streams; i++) {
3173 const AVCodec *codec;
3174 AVDictionary *thread_opt = NULL;
3175 st = ic->streams[i];
3177 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO ||
3178 st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
3179 /* if (!st->time_base.num)
3181 if (!st->codec->time_base.num)
3182 st->codec->time_base = st->time_base;
3184 // only for the split stuff
3185 if (!st->parser && !(ic->flags & AVFMT_FLAG_NOPARSE) && st->request_probe <= 0) {
3186 st->parser = av_parser_init(st->codec->codec_id);
3188 if (st->need_parsing == AVSTREAM_PARSE_HEADERS) {
3189 st->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES;
3190 } else if (st->need_parsing == AVSTREAM_PARSE_FULL_RAW) {
3191 st->parser->flags |= PARSER_FLAG_USE_CODEC_TS;
3193 } else if (st->need_parsing) {
3194 av_log(ic, AV_LOG_VERBOSE, "parser not found for codec "
3195 "%s, packets or times may be invalid.\n",
3196 avcodec_get_name(st->codec->codec_id));
3199 codec = find_decoder(ic, st, st->codec->codec_id);
3201 /* Force thread count to 1 since the H.264 decoder will not extract
3202 * SPS and PPS to extradata during multi-threaded decoding. */
3203 av_dict_set(options ? &options[i] : &thread_opt, "threads", "1", 0);
3205 if (ic->codec_whitelist)
3206 av_dict_set(options ? &options[i] : &thread_opt, "codec_whitelist", ic->codec_whitelist, 0);
3208 /* Ensure that subtitle_header is properly set. */
3209 if (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE
3210 && codec && !st->codec->codec) {
3211 if (avcodec_open2(st->codec, codec, options ? &options[i] : &thread_opt) < 0)
3212 av_log(ic, AV_LOG_WARNING,
3213 "Failed to open codec in av_find_stream_info\n");
3216 // Try to just open decoders, in case this is enough to get parameters.
3217 if (!has_codec_parameters(st, NULL) && st->request_probe <= 0) {
3218 if (codec && !st->codec->codec)
3219 if (avcodec_open2(st->codec, codec, options ? &options[i] : &thread_opt) < 0)
3220 av_log(ic, AV_LOG_WARNING,
3221 "Failed to open codec in av_find_stream_info\n");
3224 av_dict_free(&thread_opt);
3227 for (i = 0; i < ic->nb_streams; i++) {
3228 #if FF_API_R_FRAME_RATE
3229 ic->streams[i]->info->last_dts = AV_NOPTS_VALUE;
3231 ic->streams[i]->info->fps_first_dts = AV_NOPTS_VALUE;
3232 ic->streams[i]->info->fps_last_dts = AV_NOPTS_VALUE;
3238 int analyzed_all_streams;
3239 if (ff_check_interrupt(&ic->interrupt_callback)) {
3241 av_log(ic, AV_LOG_DEBUG, "interrupted\n");
3245 /* check if one codec still needs to be handled */
3246 for (i = 0; i < ic->nb_streams; i++) {
3247 int fps_analyze_framecount = 20;
3249 st = ic->streams[i];
3250 if (!has_codec_parameters(st, NULL))
3252 /* If the timebase is coarse (like the usual millisecond precision
3253 * of mkv), we need to analyze more frames to reliably arrive at
3254 * the correct fps. */
3255 if (av_q2d(st->time_base) > 0.0005)
3256 fps_analyze_framecount *= 2;
3257 if (!tb_unreliable(st->codec))
3258 fps_analyze_framecount = 0;
3259 if (ic->fps_probe_size >= 0)
3260 fps_analyze_framecount = ic->fps_probe_size;
3261 if (st->disposition & AV_DISPOSITION_ATTACHED_PIC)
3262 fps_analyze_framecount = 0;
3263 /* variable fps and no guess at the real fps */
3264 if (!(st->r_frame_rate.num && st->avg_frame_rate.num) &&
3265 st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
3266 int count = (ic->iformat->flags & AVFMT_NOTIMESTAMPS) ?
3267 st->info->codec_info_duration_fields/2 :
3268 st->info->duration_count;
3269 if (count < fps_analyze_framecount)
3272 if (st->parser && st->parser->parser->split &&
3273 !st->codec->extradata)
3275 if (st->first_dts == AV_NOPTS_VALUE &&
3276 !(ic->iformat->flags & AVFMT_NOTIMESTAMPS) &&
3277 st->codec_info_nb_frames < ic->max_ts_probe &&
3278 (st->codec->codec_type == AVMEDIA_TYPE_VIDEO ||
3279 st->codec->codec_type == AVMEDIA_TYPE_AUDIO))
3282 analyzed_all_streams = 0;
3283 if (i == ic->nb_streams) {
3284 analyzed_all_streams = 1;
3285 /* NOTE: If the format has no header, then we need to read some
3286 * packets to get most of the streams, so we cannot stop here. */
3287 if (!(ic->ctx_flags & AVFMTCTX_NOHEADER)) {
3288 /* If we found the info for all the codecs, we can stop. */
3290 av_log(ic, AV_LOG_DEBUG, "All info found\n");
3295 /* We did not get all the codec info, but we read too much data. */
3296 if (read_size >= probesize) {
3298 av_log(ic, AV_LOG_DEBUG,
3299 "Probe buffer size limit of %"PRId64" bytes reached\n", probesize);
3300 for (i = 0; i < ic->nb_streams; i++)
3301 if (!ic->streams[i]->r_frame_rate.num &&
3302 ic->streams[i]->info->duration_count <= 1 &&
3303 ic->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
3304 strcmp(ic->iformat->name, "image2"))
3305 av_log(ic, AV_LOG_WARNING,
3306 "Stream #%d: not enough frames to estimate rate; "
3307 "consider increasing probesize\n", i);
3311 /* NOTE: A new stream can be added there if no header in file
3312 * (AVFMTCTX_NOHEADER). */
3313 ret = read_frame_internal(ic, &pkt1);
3314 if (ret == AVERROR(EAGAIN))
3324 if (!(ic->flags & AVFMT_FLAG_NOBUFFER)) {
3325 ret = add_to_pktbuf(&ic->internal->packet_buffer, pkt,
3326 &ic->internal->packet_buffer_end, 0);
3328 goto find_stream_info_err;
3331 st = ic->streams[pkt->stream_index];
3332 if (!(st->disposition & AV_DISPOSITION_ATTACHED_PIC))
3333 read_size += pkt->size;
3335 if (pkt->dts != AV_NOPTS_VALUE && st->codec_info_nb_frames > 1) {
3336 /* check for non-increasing dts */
3337 if (st->info->fps_last_dts != AV_NOPTS_VALUE &&
3338 st->info->fps_last_dts >= pkt->dts) {
3339 av_log(ic, AV_LOG_DEBUG,
3340 "Non-increasing DTS in stream %d: packet %d with DTS "
3341 "%"PRId64", packet %d with DTS %"PRId64"\n",
3342 st->index, st->info->fps_last_dts_idx,
3343 st->info->fps_last_dts, st->codec_info_nb_frames,
3345 st->info->fps_first_dts =
3346 st->info->fps_last_dts = AV_NOPTS_VALUE;
3348 /* Check for a discontinuity in dts. If the difference in dts
3349 * is more than 1000 times the average packet duration in the
3350 * sequence, we treat it as a discontinuity. */
3351 if (st->info->fps_last_dts != AV_NOPTS_VALUE &&
3352 st->info->fps_last_dts_idx > st->info->fps_first_dts_idx &&
3353 (pkt->dts - st->info->fps_last_dts) / 1000 >
3354 (st->info->fps_last_dts - st->info->fps_first_dts) /
3355 (st->info->fps_last_dts_idx - st->info->fps_first_dts_idx)) {
3356 av_log(ic, AV_LOG_WARNING,
3357 "DTS discontinuity in stream %d: packet %d with DTS "
3358 "%"PRId64", packet %d with DTS %"PRId64"\n",
3359 st->index, st->info->fps_last_dts_idx,
3360 st->info->fps_last_dts, st->codec_info_nb_frames,
3362 st->info->fps_first_dts =
3363 st->info->fps_last_dts = AV_NOPTS_VALUE;
3366 /* update stored dts values */
3367 if (st->info->fps_first_dts == AV_NOPTS_VALUE) {
3368 st->info->fps_first_dts = pkt->dts;
3369 st->info->fps_first_dts_idx = st->codec_info_nb_frames;
3371 st->info->fps_last_dts = pkt->dts;
3372 st->info->fps_last_dts_idx = st->codec_info_nb_frames;
3374 if (st->codec_info_nb_frames>1) {
3378 if (st->time_base.den > 0)
3379 t = av_rescale_q(st->info->codec_info_duration, st->time_base, AV_TIME_BASE_Q);
3380 if (st->avg_frame_rate.num > 0)
3381 t = FFMAX(t, av_rescale_q(st->codec_info_nb_frames, av_inv_q(st->avg_frame_rate), AV_TIME_BASE_Q));
3384 && st->codec_info_nb_frames>30
3385 && st->info->fps_first_dts != AV_NOPTS_VALUE
3386 && st->info->fps_last_dts != AV_NOPTS_VALUE)
3387 t = FFMAX(t, av_rescale_q(st->info->fps_last_dts - st->info->fps_first_dts, st->time_base, AV_TIME_BASE_Q));
3389 if (analyzed_all_streams) limit = max_analyze_duration;
3390 else if (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) limit = max_subtitle_analyze_duration;
3391 else limit = max_stream_analyze_duration;
3394 av_log(ic, AV_LOG_VERBOSE, "max_analyze_duration %"PRId64" reached at %"PRId64" microseconds st:%d\n",
3396 t, pkt->stream_index);
3397 if (ic->flags & AVFMT_FLAG_NOBUFFER)
3398 av_packet_unref(pkt);
3401 if (pkt->duration) {
3402 if (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE && pkt->pts != AV_NOPTS_VALUE && pkt->pts >= st->start_time) {
3403 st->info->codec_info_duration = FFMIN(pkt->pts - st->start_time, st->info->codec_info_duration + pkt->duration);
3405 st->info->codec_info_duration += pkt->duration;
3406 st->info->codec_info_duration_fields += st->parser && st->need_parsing && st->codec->ticks_per_frame ==2 ? st->parser->repeat_pict + 1 : 2;
3409 #if FF_API_R_FRAME_RATE
3410 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
3411 ff_rfps_add_frame(ic, st, pkt->dts);
3413 if (st->parser && st->parser->parser->split && !st->codec->extradata) {
3414 int i = st->parser->parser->split(st->codec, pkt->data, pkt->size);
3415 if (i > 0 && i < FF_MAX_EXTRADATA_SIZE) {
3416 if (ff_alloc_extradata(st->codec, i))
3417 return AVERROR(ENOMEM);
3418 memcpy(st->codec->extradata, pkt->data,
3419 st->codec->extradata_size);
3423 /* If still no information, we try to open the codec and to
3424 * decompress the frame. We try to avoid that in most cases as
3425 * it takes longer and uses more memory. For MPEG-4, we need to
3426 * decompress for QuickTime.
3428 * If AV_CODEC_CAP_CHANNEL_CONF is set this will force decoding of at
3429 * least one frame of codec data, this makes sure the codec initializes
3430 * the channel configuration and does not only trust the values from
3432 try_decode_frame(ic, st, pkt,
3433 (options && i < orig_nb_streams) ? &options[i] : NULL);
3435 if (ic->flags & AVFMT_FLAG_NOBUFFER)
3436 av_packet_unref(pkt);
3438 st->codec_info_nb_frames++;
3443 AVPacket empty_pkt = { 0 };
3445 av_init_packet(&empty_pkt);
3447 for (i = 0; i < ic->nb_streams; i++) {
3449 st = ic->streams[i];
3451 /* flush the decoders */
3452 if (st->info->found_decoder == 1) {
3454 err = try_decode_frame(ic, st, &empty_pkt,
3455 (options && i < orig_nb_streams)
3456 ? &options[i] : NULL);
3457 } while (err > 0 && !has_codec_parameters(st, NULL));
3460 av_log(ic, AV_LOG_INFO,
3461 "decoding for stream %d failed\n", st->index);
3467 // close codecs which were opened in try_decode_frame()
3468 for (i = 0; i < ic->nb_streams; i++) {
3469 st = ic->streams[i];
3470 avcodec_close(st->codec);
3473 ff_rfps_calculate(ic);
3475 for (i = 0; i < ic->nb_streams; i++) {
3476 st = ic->streams[i];
3477 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
3478 if (st->codec->codec_id == AV_CODEC_ID_RAWVIDEO && !st->codec->codec_tag && !st->codec->bits_per_coded_sample) {
3479 uint32_t tag= avcodec_pix_fmt_to_codec_tag(st->codec->pix_fmt);
3480 if (avpriv_find_pix_fmt(avpriv_get_raw_pix_fmt_tags(), tag) == st->codec->pix_fmt)
3481 st->codec->codec_tag= tag;
3484 /* estimate average framerate if not set by demuxer */
3485 if (st->info->codec_info_duration_fields &&
3486 !st->avg_frame_rate.num &&
3487 st->info->codec_info_duration) {
3489 double best_error = 0.01;
3491 if (st->info->codec_info_duration >= INT64_MAX / st->time_base.num / 2||
3492 st->info->codec_info_duration_fields >= INT64_MAX / st->time_base.den ||
3493 st->info->codec_info_duration < 0)
3495 av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den,
3496 st->info->codec_info_duration_fields * (int64_t) st->time_base.den,
3497 st->info->codec_info_duration * 2 * (int64_t) st->time_base.num, 60000);
3499 /* Round guessed framerate to a "standard" framerate if it's
3500 * within 1% of the original estimate. */
3501 for (j = 0; j < MAX_STD_TIMEBASES; j++) {
3502 AVRational std_fps = { get_std_framerate(j), 12 * 1001 };
3503 double error = fabs(av_q2d(st->avg_frame_rate) /
3504 av_q2d(std_fps) - 1);
3506 if (error < best_error) {
3508 best_fps = std_fps.num;
3512 av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den,
3513 best_fps, 12 * 1001, INT_MAX);
3516 if (!st->r_frame_rate.num) {
3517 if ( st->codec->time_base.den * (int64_t) st->time_base.num
3518 <= st->codec->time_base.num * st->codec->ticks_per_frame * (int64_t) st->time_base.den) {
3519 st->r_frame_rate.num = st->codec->time_base.den;
3520 st->r_frame_rate.den = st->codec->time_base.num * st->codec->ticks_per_frame;
3522 st->r_frame_rate.num = st->time_base.den;
3523 st->r_frame_rate.den = st->time_base.num;
3526 if (st->display_aspect_ratio.num && st->display_aspect_ratio.den) {
3527 AVRational hw_ratio = { st->codec->height, st->codec->width };
3528 st->sample_aspect_ratio = av_mul_q(st->display_aspect_ratio,
3531 } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
3532 if (!st->codec->bits_per_coded_sample)
3533 st->codec->bits_per_coded_sample =
3534 av_get_bits_per_sample(st->codec->codec_id);
3535 // set stream disposition based on audio service type
3536 switch (st->codec->audio_service_type) {
3537 case AV_AUDIO_SERVICE_TYPE_EFFECTS:
3538 st->disposition = AV_DISPOSITION_CLEAN_EFFECTS;
3540 case AV_AUDIO_SERVICE_TYPE_VISUALLY_IMPAIRED:
3541 st->disposition = AV_DISPOSITION_VISUAL_IMPAIRED;
3543 case AV_AUDIO_SERVICE_TYPE_HEARING_IMPAIRED:
3544 st->disposition = AV_DISPOSITION_HEARING_IMPAIRED;
3546 case AV_AUDIO_SERVICE_TYPE_COMMENTARY:
3547 st->disposition = AV_DISPOSITION_COMMENT;
3549 case AV_AUDIO_SERVICE_TYPE_KARAOKE:
3550 st->disposition = AV_DISPOSITION_KARAOKE;
3557 estimate_timings(ic, old_offset);
3559 av_opt_set(ic, "skip_clear", "0", AV_OPT_SEARCH_CHILDREN);
3561 if (ret >= 0 && ic->nb_streams)
3562 /* We could not have all the codec parameters before EOF. */
3564 for (i = 0; i < ic->nb_streams; i++) {
3566 st = ic->streams[i];
3567 if (!has_codec_parameters(st, &errmsg)) {
3569 avcodec_string(buf, sizeof(buf), st->codec, 0);
3570 av_log(ic, AV_LOG_WARNING,
3571 "Could not find codec parameters for stream %d (%s): %s\n"
3572 "Consider increasing the value for the 'analyzeduration' and 'probesize' options\n",
3579 compute_chapters_end(ic);
3581 find_stream_info_err:
3582 for (i = 0; i < ic->nb_streams; i++) {
3583 st = ic->streams[i];
3584 if (ic->streams[i]->codec->codec_type != AVMEDIA_TYPE_AUDIO)
3585 ic->streams[i]->codec->thread_count = 0;
3587 av_freep(&st->info->duration_error);
3588 av_freep(&ic->streams[i]->info);
3591 av_log(ic, AV_LOG_DEBUG, "After avformat_find_stream_info() pos: %"PRId64" bytes read:%"PRId64" seeks:%d frames:%d\n",
3592 avio_tell(ic->pb), ic->pb->bytes_read, ic->pb->seek_count, count);
3596 AVProgram *av_find_program_from_stream(AVFormatContext *ic, AVProgram *last, int s)
3600 for (i = 0; i < ic->nb_programs; i++) {
3601 if (ic->programs[i] == last) {
3605 for (j = 0; j < ic->programs[i]->nb_stream_indexes; j++)
3606 if (ic->programs[i]->stream_index[j] == s)
3607 return ic->programs[i];
3613 int av_find_best_stream(AVFormatContext *ic, enum AVMediaType type,
3614 int wanted_stream_nb, int related_stream,
3615 AVCodec **decoder_ret, int flags)
3617 int i, nb_streams = ic->nb_streams;
3618 int ret = AVERROR_STREAM_NOT_FOUND, best_count = -1, best_bitrate = -1, best_multiframe = -1, count, bitrate, multiframe;
3619 unsigned *program = NULL;
3620 const AVCodec *decoder = NULL, *best_decoder = NULL;
3622 if (related_stream >= 0 && wanted_stream_nb < 0) {
3623 AVProgram *p = av_find_program_from_stream(ic, NULL, related_stream);
3625 program = p->stream_index;
3626 nb_streams = p->nb_stream_indexes;
3629 for (i = 0; i < nb_streams; i++) {
3630 int real_stream_index = program ? program[i] : i;
3631 AVStream *st = ic->streams[real_stream_index];
3632 AVCodecContext *avctx = st->codec;
3633 if (avctx->codec_type != type)
3635 if (wanted_stream_nb >= 0 && real_stream_index != wanted_stream_nb)
3637 if (wanted_stream_nb != real_stream_index &&
3638 st->disposition & (AV_DISPOSITION_HEARING_IMPAIRED |
3639 AV_DISPOSITION_VISUAL_IMPAIRED))
3641 if (type == AVMEDIA_TYPE_AUDIO && !(avctx->channels && avctx->sample_rate))
3644 decoder = find_decoder(ic, st, st->codec->codec_id);
3647 ret = AVERROR_DECODER_NOT_FOUND;
3651 count = st->codec_info_nb_frames;
3652 bitrate = avctx->bit_rate;
3654 bitrate = avctx->rc_max_rate;
3655 multiframe = FFMIN(5, count);
3656 if ((best_multiframe > multiframe) ||
3657 (best_multiframe == multiframe && best_bitrate > bitrate) ||
3658 (best_multiframe == multiframe && best_bitrate == bitrate && best_count >= count))
3661 best_bitrate = bitrate;
3662 best_multiframe = multiframe;
3663 ret = real_stream_index;
3664 best_decoder = decoder;
3665 if (program && i == nb_streams - 1 && ret < 0) {
3667 nb_streams = ic->nb_streams;
3668 /* no related stream found, try again with everything */
3673 *decoder_ret = (AVCodec*)best_decoder;
3677 /*******************************************************/
3679 int av_read_play(AVFormatContext *s)
3681 if (s->iformat->read_play)
3682 return s->iformat->read_play(s);
3684 return avio_pause(s->pb, 0);
3685 return AVERROR(ENOSYS);
3688 int av_read_pause(AVFormatContext *s)
3690 if (s->iformat->read_pause)
3691 return s->iformat->read_pause(s);
3693 return avio_pause(s->pb, 1);
3694 return AVERROR(ENOSYS);
3697 static void free_stream(AVStream **pst)
3699 AVStream *st = *pst;
3705 for (i = 0; i < st->nb_side_data; i++)
3706 av_freep(&st->side_data[i].data);
3707 av_freep(&st->side_data);
3710 av_parser_close(st->parser);
3712 if (st->attached_pic.data)
3713 av_packet_unref(&st->attached_pic);
3715 av_freep(&st->internal);
3717 av_dict_free(&st->metadata);
3718 av_freep(&st->probe_data.buf);
3719 av_freep(&st->index_entries);
3720 av_freep(&st->codec->extradata);
3721 av_freep(&st->codec->subtitle_header);
3722 av_freep(&st->codec);
3723 av_freep(&st->priv_data);
3725 av_freep(&st->info->duration_error);
3726 av_freep(&st->info);
3727 av_freep(&st->recommended_encoder_configuration);
3728 av_freep(&st->priv_pts);
3733 void ff_free_stream(AVFormatContext *s, AVStream *st)
3735 av_assert0(s->nb_streams>0);
3736 av_assert0(s->streams[ s->nb_streams - 1 ] == st);
3738 free_stream(&s->streams[ --s->nb_streams ]);
3741 void avformat_free_context(AVFormatContext *s)
3749 if (s->iformat && s->iformat->priv_class && s->priv_data)
3750 av_opt_free(s->priv_data);
3751 if (s->oformat && s->oformat->priv_class && s->priv_data)
3752 av_opt_free(s->priv_data);
3754 for (i = s->nb_streams - 1; i >= 0; i--)
3755 ff_free_stream(s, s->streams[i]);
3758 for (i = s->nb_programs - 1; i >= 0; i--) {
3759 av_dict_free(&s->programs[i]->metadata);
3760 av_freep(&s->programs[i]->stream_index);
3761 av_freep(&s->programs[i]);
3763 av_freep(&s->programs);
3764 av_freep(&s->priv_data);
3765 while (s->nb_chapters--) {
3766 av_dict_free(&s->chapters[s->nb_chapters]->metadata);
3767 av_freep(&s->chapters[s->nb_chapters]);
3769 av_freep(&s->chapters);
3770 av_dict_free(&s->metadata);
3771 av_freep(&s->streams);
3772 av_freep(&s->internal);
3773 flush_packet_queue(s);
3777 void avformat_close_input(AVFormatContext **ps)
3788 if ((s->iformat && strcmp(s->iformat->name, "image2") && s->iformat->flags & AVFMT_NOFILE) ||
3789 (s->flags & AVFMT_FLAG_CUSTOM_IO))
3792 flush_packet_queue(s);
3795 if (s->iformat->read_close)
3796 s->iformat->read_close(s);
3798 avformat_free_context(s);
3805 AVStream *avformat_new_stream(AVFormatContext *s, const AVCodec *c)
3811 if (s->nb_streams >= INT_MAX/sizeof(*streams))
3813 streams = av_realloc_array(s->streams, s->nb_streams + 1, sizeof(*streams));
3816 s->streams = streams;
3818 st = av_mallocz(sizeof(AVStream));
3821 if (!(st->info = av_mallocz(sizeof(*st->info)))) {
3825 st->info->last_dts = AV_NOPTS_VALUE;
3827 st->codec = avcodec_alloc_context3(c);
3834 st->internal = av_mallocz(sizeof(*st->internal));
3839 /* no default bitrate if decoding */
3840 st->codec->bit_rate = 0;
3842 /* default pts setting is MPEG-like */
3843 avpriv_set_pts_info(st, 33, 1, 90000);
3844 /* we set the current DTS to 0 so that formats without any timestamps
3845 * but durations get some timestamps, formats with some unknown
3846 * timestamps have their first few packets buffered and the
3847 * timestamps corrected before they are returned to the user */
3848 st->cur_dts = RELATIVE_TS_BASE;
3850 st->cur_dts = AV_NOPTS_VALUE;
3853 st->index = s->nb_streams;
3854 st->start_time = AV_NOPTS_VALUE;
3855 st->duration = AV_NOPTS_VALUE;
3856 st->first_dts = AV_NOPTS_VALUE;
3857 st->probe_packets = MAX_PROBE_PACKETS;
3858 st->pts_wrap_reference = AV_NOPTS_VALUE;
3859 st->pts_wrap_behavior = AV_PTS_WRAP_IGNORE;
3861 st->last_IP_pts = AV_NOPTS_VALUE;
3862 st->last_dts_for_order_check = AV_NOPTS_VALUE;
3863 for (i = 0; i < MAX_REORDER_DELAY + 1; i++)
3864 st->pts_buffer[i] = AV_NOPTS_VALUE;
3866 st->sample_aspect_ratio = (AVRational) { 0, 1 };
3868 #if FF_API_R_FRAME_RATE
3869 st->info->last_dts = AV_NOPTS_VALUE;
3871 st->info->fps_first_dts = AV_NOPTS_VALUE;
3872 st->info->fps_last_dts = AV_NOPTS_VALUE;
3874 st->inject_global_side_data = s->internal->inject_global_side_data;
3876 s->streams[s->nb_streams++] = st;
3883 AVProgram *av_new_program(AVFormatContext *ac, int id)
3885 AVProgram *program = NULL;
3888 av_log(ac, AV_LOG_TRACE, "new_program: id=0x%04x\n", id);
3890 for (i = 0; i < ac->nb_programs; i++)
3891 if (ac->programs[i]->id == id)
3892 program = ac->programs[i];
3895 program = av_mallocz(sizeof(AVProgram));
3898 dynarray_add(&ac->programs, &ac->nb_programs, program);
3899 program->discard = AVDISCARD_NONE;
3902 program->pts_wrap_reference = AV_NOPTS_VALUE;
3903 program->pts_wrap_behavior = AV_PTS_WRAP_IGNORE;
3905 program->start_time =
3906 program->end_time = AV_NOPTS_VALUE;
3911 AVChapter *avpriv_new_chapter(AVFormatContext *s, int id, AVRational time_base,
3912 int64_t start, int64_t end, const char *title)
3914 AVChapter *chapter = NULL;
3917 if (end != AV_NOPTS_VALUE && start > end) {
3918 av_log(s, AV_LOG_ERROR, "Chapter end time %"PRId64" before start %"PRId64"\n", end, start);
3922 for (i = 0; i < s->nb_chapters; i++)
3923 if (s->chapters[i]->id == id)
3924 chapter = s->chapters[i];
3927 chapter = av_mallocz(sizeof(AVChapter));
3930 dynarray_add(&s->chapters, &s->nb_chapters, chapter);
3932 av_dict_set(&chapter->metadata, "title", title, 0);
3934 chapter->time_base = time_base;
3935 chapter->start = start;
3941 void av_program_add_stream_index(AVFormatContext *ac, int progid, unsigned idx)
3944 AVProgram *program = NULL;
3947 if (idx >= ac->nb_streams) {
3948 av_log(ac, AV_LOG_ERROR, "stream index %d is not valid\n", idx);
3952 for (i = 0; i < ac->nb_programs; i++) {
3953 if (ac->programs[i]->id != progid)
3955 program = ac->programs[i];
3956 for (j = 0; j < program->nb_stream_indexes; j++)
3957 if (program->stream_index[j] == idx)
3960 tmp = av_realloc_array(program->stream_index, program->nb_stream_indexes+1, sizeof(unsigned int));
3963 program->stream_index = tmp;
3964 program->stream_index[program->nb_stream_indexes++] = idx;
3969 uint64_t ff_ntp_time(void)
3971 return (av_gettime() / 1000) * 1000 + NTP_OFFSET_US;
3974 int av_get_frame_filename(char *buf, int buf_size, const char *path, int number)
3977 char *q, buf1[20], c;
3978 int nd, len, percentd_found;
3990 while (av_isdigit(*p))
3991 nd = nd * 10 + *p++ - '0';
3993 } while (av_isdigit(c));
4004 snprintf(buf1, sizeof(buf1), "%0*d", nd, number);
4006 if ((q - buf + len) > buf_size - 1)
4008 memcpy(q, buf1, len);
4016 if ((q - buf) < buf_size - 1)
4020 if (!percentd_found)
4029 void av_url_split(char *proto, int proto_size,
4030 char *authorization, int authorization_size,
4031 char *hostname, int hostname_size,
4032 int *port_ptr, char *path, int path_size, const char *url)
4034 const char *p, *ls, *ls2, *at, *at2, *col, *brk;
4040 if (authorization_size > 0)
4041 authorization[0] = 0;
4042 if (hostname_size > 0)
4047 /* parse protocol */
4048 if ((p = strchr(url, ':'))) {
4049 av_strlcpy(proto, url, FFMIN(proto_size, p + 1 - url));
4056 /* no protocol means plain filename */
4057 av_strlcpy(path, url, path_size);
4061 /* separate path from hostname */
4062 ls = strchr(p, '/');
4063 ls2 = strchr(p, '?');
4067 ls = FFMIN(ls, ls2);
4069 av_strlcpy(path, ls, path_size);
4071 ls = &p[strlen(p)]; // XXX
4073 /* the rest is hostname, use that to parse auth/port */
4075 /* authorization (user[:pass]@hostname) */
4077 while ((at = strchr(p, '@')) && at < ls) {
4078 av_strlcpy(authorization, at2,
4079 FFMIN(authorization_size, at + 1 - at2));
4080 p = at + 1; /* skip '@' */
4083 if (*p == '[' && (brk = strchr(p, ']')) && brk < ls) {
4085 av_strlcpy(hostname, p + 1,
4086 FFMIN(hostname_size, brk - p));
4087 if (brk[1] == ':' && port_ptr)
4088 *port_ptr = atoi(brk + 2);
4089 } else if ((col = strchr(p, ':')) && col < ls) {
4090 av_strlcpy(hostname, p,
4091 FFMIN(col + 1 - p, hostname_size));
4093 *port_ptr = atoi(col + 1);
4095 av_strlcpy(hostname, p,
4096 FFMIN(ls + 1 - p, hostname_size));
4100 char *ff_data_to_hex(char *buff, const uint8_t *src, int s, int lowercase)
4103 static const char hex_table_uc[16] = { '0', '1', '2', '3',
4106 'C', 'D', 'E', 'F' };
4107 static const char hex_table_lc[16] = { '0', '1', '2', '3',
4110 'c', 'd', 'e', 'f' };
4111 const char *hex_table = lowercase ? hex_table_lc : hex_table_uc;
4113 for (i = 0; i < s; i++) {
4114 buff[i * 2] = hex_table[src[i] >> 4];
4115 buff[i * 2 + 1] = hex_table[src[i] & 0xF];
4121 int ff_hex_to_data(uint8_t *data, const char *p)
4128 p += strspn(p, SPACE_CHARS);
4131 c = av_toupper((unsigned char) *p++);
4132 if (c >= '0' && c <= '9')
4134 else if (c >= 'A' && c <= 'F')
4149 void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits,
4150 unsigned int pts_num, unsigned int pts_den)
4153 if (av_reduce(&new_tb.num, &new_tb.den, pts_num, pts_den, INT_MAX)) {
4154 if (new_tb.num != pts_num)
4155 av_log(NULL, AV_LOG_DEBUG,
4156 "st:%d removing common factor %d from timebase\n",
4157 s->index, pts_num / new_tb.num);
4159 av_log(NULL, AV_LOG_WARNING,
4160 "st:%d has too large timebase, reducing\n", s->index);
4162 if (new_tb.num <= 0 || new_tb.den <= 0) {
4163 av_log(NULL, AV_LOG_ERROR,
4164 "Ignoring attempt to set invalid timebase %d/%d for st:%d\n",
4165 new_tb.num, new_tb.den,
4169 s->time_base = new_tb;
4170 av_codec_set_pkt_timebase(s->codec, new_tb);
4171 s->pts_wrap_bits = pts_wrap_bits;
4174 void ff_parse_key_value(const char *str, ff_parse_key_val_cb callback_get_buf,
4177 const char *ptr = str;
4179 /* Parse key=value pairs. */
4182 char *dest = NULL, *dest_end;
4183 int key_len, dest_len = 0;
4185 /* Skip whitespace and potential commas. */
4186 while (*ptr && (av_isspace(*ptr) || *ptr == ','))
4193 if (!(ptr = strchr(key, '=')))
4196 key_len = ptr - key;
4198 callback_get_buf(context, key, key_len, &dest, &dest_len);
4199 dest_end = dest + dest_len - 1;
4203 while (*ptr && *ptr != '\"') {
4207 if (dest && dest < dest_end)
4211 if (dest && dest < dest_end)
4219 for (; *ptr && !(av_isspace(*ptr) || *ptr == ','); ptr++)
4220 if (dest && dest < dest_end)
4228 int ff_find_stream_index(AVFormatContext *s, int id)
4231 for (i = 0; i < s->nb_streams; i++)
4232 if (s->streams[i]->id == id)
4237 int avformat_query_codec(const AVOutputFormat *ofmt, enum AVCodecID codec_id,
4241 unsigned int codec_tag;
4242 if (ofmt->query_codec)
4243 return ofmt->query_codec(codec_id, std_compliance);
4244 else if (ofmt->codec_tag)
4245 return !!av_codec_get_tag2(ofmt->codec_tag, codec_id, &codec_tag);
4246 else if (codec_id == ofmt->video_codec ||
4247 codec_id == ofmt->audio_codec ||
4248 codec_id == ofmt->subtitle_codec)
4251 return AVERROR_PATCHWELCOME;
4254 int avformat_network_init(void)
4258 ff_network_inited_globally = 1;
4259 if ((ret = ff_network_init()) < 0)
4261 if ((ret = ff_tls_init()) < 0)
4267 int avformat_network_deinit(void)
4272 ff_network_inited_globally = 0;
4277 int ff_add_param_change(AVPacket *pkt, int32_t channels,
4278 uint64_t channel_layout, int32_t sample_rate,
4279 int32_t width, int32_t height)
4285 return AVERROR(EINVAL);
4288 flags |= AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT;
4290 if (channel_layout) {
4292 flags |= AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT;
4296 flags |= AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE;
4298 if (width || height) {
4300 flags |= AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS;
4302 data = av_packet_new_side_data(pkt, AV_PKT_DATA_PARAM_CHANGE, size);
4304 return AVERROR(ENOMEM);
4305 bytestream_put_le32(&data, flags);
4307 bytestream_put_le32(&data, channels);
4309 bytestream_put_le64(&data, channel_layout);
4311 bytestream_put_le32(&data, sample_rate);
4312 if (width || height) {
4313 bytestream_put_le32(&data, width);
4314 bytestream_put_le32(&data, height);
4319 AVRational av_guess_sample_aspect_ratio(AVFormatContext *format, AVStream *stream, AVFrame *frame)
4321 AVRational undef = {0, 1};
4322 AVRational stream_sample_aspect_ratio = stream ? stream->sample_aspect_ratio : undef;
4323 AVRational codec_sample_aspect_ratio = stream && stream->codec ? stream->codec->sample_aspect_ratio : undef;
4324 AVRational frame_sample_aspect_ratio = frame ? frame->sample_aspect_ratio : codec_sample_aspect_ratio;
4326 av_reduce(&stream_sample_aspect_ratio.num, &stream_sample_aspect_ratio.den,
4327 stream_sample_aspect_ratio.num, stream_sample_aspect_ratio.den, INT_MAX);
4328 if (stream_sample_aspect_ratio.num <= 0 || stream_sample_aspect_ratio.den <= 0)
4329 stream_sample_aspect_ratio = undef;
4331 av_reduce(&frame_sample_aspect_ratio.num, &frame_sample_aspect_ratio.den,
4332 frame_sample_aspect_ratio.num, frame_sample_aspect_ratio.den, INT_MAX);
4333 if (frame_sample_aspect_ratio.num <= 0 || frame_sample_aspect_ratio.den <= 0)
4334 frame_sample_aspect_ratio = undef;
4336 if (stream_sample_aspect_ratio.num)
4337 return stream_sample_aspect_ratio;
4339 return frame_sample_aspect_ratio;
4342 AVRational av_guess_frame_rate(AVFormatContext *format, AVStream *st, AVFrame *frame)
4344 AVRational fr = st->r_frame_rate;
4345 AVRational codec_fr = st->codec->framerate;
4346 AVRational avg_fr = st->avg_frame_rate;
4348 if (avg_fr.num > 0 && avg_fr.den > 0 && fr.num > 0 && fr.den > 0 &&
4349 av_q2d(avg_fr) < 70 && av_q2d(fr) > 210) {
4354 if (st->codec->ticks_per_frame > 1) {
4355 if ( codec_fr.num > 0 && codec_fr.den > 0 &&
4356 (fr.num == 0 || av_q2d(codec_fr) < av_q2d(fr)*0.7 && fabs(1.0 - av_q2d(av_div_q(avg_fr, fr))) > 0.1))
4363 int avformat_match_stream_specifier(AVFormatContext *s, AVStream *st,
4366 if (*spec <= '9' && *spec >= '0') /* opt:index */
4367 return strtol(spec, NULL, 0) == st->index;
4368 else if (*spec == 'v' || *spec == 'a' || *spec == 's' || *spec == 'd' ||
4369 *spec == 't' || *spec == 'V') { /* opt:[vasdtV] */
4370 enum AVMediaType type;
4374 case 'v': type = AVMEDIA_TYPE_VIDEO; break;
4375 case 'a': type = AVMEDIA_TYPE_AUDIO; break;
4376 case 's': type = AVMEDIA_TYPE_SUBTITLE; break;
4377 case 'd': type = AVMEDIA_TYPE_DATA; break;
4378 case 't': type = AVMEDIA_TYPE_ATTACHMENT; break;
4379 case 'V': type = AVMEDIA_TYPE_VIDEO; nopic = 1; break;
4380 default: av_assert0(0);
4382 if (type != st->codec->codec_type)
4384 if (nopic && (st->disposition & AV_DISPOSITION_ATTACHED_PIC))
4386 if (*spec++ == ':') { /* possibly followed by :index */
4387 int i, index = strtol(spec, NULL, 0);
4388 for (i = 0; i < s->nb_streams; i++)
4389 if (s->streams[i]->codec->codec_type == type &&
4390 !(nopic && (st->disposition & AV_DISPOSITION_ATTACHED_PIC)) &&
4392 return i == st->index;
4396 } else if (*spec == 'p' && *(spec + 1) == ':') {
4400 prog_id = strtol(spec, &endptr, 0);
4401 for (i = 0; i < s->nb_programs; i++) {
4402 if (s->programs[i]->id != prog_id)
4405 if (*endptr++ == ':') {
4406 int stream_idx = strtol(endptr, NULL, 0);
4407 return stream_idx >= 0 &&
4408 stream_idx < s->programs[i]->nb_stream_indexes &&
4409 st->index == s->programs[i]->stream_index[stream_idx];
4412 for (j = 0; j < s->programs[i]->nb_stream_indexes; j++)
4413 if (st->index == s->programs[i]->stream_index[j])
4417 } else if (*spec == '#' ||
4418 (*spec == 'i' && *(spec + 1) == ':')) {
4421 spec += 1 + (*spec == 'i');
4422 stream_id = strtol(spec, &endptr, 0);
4424 return stream_id == st->id;
4425 } else if (*spec == 'm' && *(spec + 1) == ':') {
4426 AVDictionaryEntry *tag;
4431 val = strchr(spec, ':');
4433 key = val ? av_strndup(spec, val - spec) : av_strdup(spec);
4435 return AVERROR(ENOMEM);
4437 tag = av_dict_get(st->metadata, key, NULL, 0);
4439 if (!val || !strcmp(tag->value, val + 1))
4448 } else if (*spec == 'u') {
4449 AVCodecContext *avctx = st->codec;
4451 switch (avctx->codec_type) {
4452 case AVMEDIA_TYPE_AUDIO:
4453 val = avctx->sample_rate && avctx->channels;
4454 if (avctx->sample_fmt == AV_SAMPLE_FMT_NONE)
4457 case AVMEDIA_TYPE_VIDEO:
4458 val = avctx->width && avctx->height;
4459 if (avctx->pix_fmt == AV_PIX_FMT_NONE)
4462 case AVMEDIA_TYPE_UNKNOWN:
4469 return avctx->codec_id != AV_CODEC_ID_NONE && val != 0;
4470 } else if (!*spec) /* empty specifier, matches everything */
4473 av_log(s, AV_LOG_ERROR, "Invalid stream specifier: %s.\n", spec);
4474 return AVERROR(EINVAL);
4477 int ff_generate_avci_extradata(AVStream *st)
4479 static const uint8_t avci100_1080p_extradata[] = {
4481 0x00, 0x00, 0x00, 0x01, 0x67, 0x7a, 0x10, 0x29,
4482 0xb6, 0xd4, 0x20, 0x22, 0x33, 0x19, 0xc6, 0x63,
4483 0x23, 0x21, 0x01, 0x11, 0x98, 0xce, 0x33, 0x19,
4484 0x18, 0x21, 0x02, 0x56, 0xb9, 0x3d, 0x7d, 0x7e,
4485 0x4f, 0xe3, 0x3f, 0x11, 0xf1, 0x9e, 0x08, 0xb8,
4486 0x8c, 0x54, 0x43, 0xc0, 0x78, 0x02, 0x27, 0xe2,
4487 0x70, 0x1e, 0x30, 0x10, 0x10, 0x14, 0x00, 0x00,
4488 0x03, 0x00, 0x04, 0x00, 0x00, 0x03, 0x00, 0xca,
4489 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4491 0x00, 0x00, 0x00, 0x01, 0x68, 0xce, 0x33, 0x48,
4494 static const uint8_t avci100_1080i_extradata[] = {
4496 0x00, 0x00, 0x00, 0x01, 0x67, 0x7a, 0x10, 0x29,
4497 0xb6, 0xd4, 0x20, 0x22, 0x33, 0x19, 0xc6, 0x63,
4498 0x23, 0x21, 0x01, 0x11, 0x98, 0xce, 0x33, 0x19,
4499 0x18, 0x21, 0x03, 0x3a, 0x46, 0x65, 0x6a, 0x65,
4500 0x24, 0xad, 0xe9, 0x12, 0x32, 0x14, 0x1a, 0x26,
4501 0x34, 0xad, 0xa4, 0x41, 0x82, 0x23, 0x01, 0x50,
4502 0x2b, 0x1a, 0x24, 0x69, 0x48, 0x30, 0x40, 0x2e,
4503 0x11, 0x12, 0x08, 0xc6, 0x8c, 0x04, 0x41, 0x28,
4504 0x4c, 0x34, 0xf0, 0x1e, 0x01, 0x13, 0xf2, 0xe0,
4505 0x3c, 0x60, 0x20, 0x20, 0x28, 0x00, 0x00, 0x03,
4506 0x00, 0x08, 0x00, 0x00, 0x03, 0x01, 0x94, 0x20,
4508 0x00, 0x00, 0x00, 0x01, 0x68, 0xce, 0x33, 0x48,
4511 static const uint8_t avci50_1080p_extradata[] = {
4513 0x00, 0x00, 0x00, 0x01, 0x67, 0x6e, 0x10, 0x28,
4514 0xa6, 0xd4, 0x20, 0x32, 0x33, 0x0c, 0x71, 0x18,
4515 0x88, 0x62, 0x10, 0x19, 0x19, 0x86, 0x38, 0x8c,
4516 0x44, 0x30, 0x21, 0x02, 0x56, 0x4e, 0x6f, 0x37,
4517 0xcd, 0xf9, 0xbf, 0x81, 0x6b, 0xf3, 0x7c, 0xde,
4518 0x6e, 0x6c, 0xd3, 0x3c, 0x05, 0xa0, 0x22, 0x7e,
4519 0x5f, 0xfc, 0x00, 0x0c, 0x00, 0x13, 0x8c, 0x04,
4520 0x04, 0x05, 0x00, 0x00, 0x03, 0x00, 0x01, 0x00,
4521 0x00, 0x03, 0x00, 0x32, 0x84, 0x00, 0x00, 0x00,
4523 0x00, 0x00, 0x00, 0x01, 0x68, 0xee, 0x31, 0x12,
4526 static const uint8_t avci50_1080i_extradata[] = {
4528 0x00, 0x00, 0x00, 0x01, 0x67, 0x6e, 0x10, 0x28,
4529 0xa6, 0xd4, 0x20, 0x32, 0x33, 0x0c, 0x71, 0x18,
4530 0x88, 0x62, 0x10, 0x19, 0x19, 0x86, 0x38, 0x8c,
4531 0x44, 0x30, 0x21, 0x02, 0x56, 0x4e, 0x6e, 0x61,
4532 0x87, 0x3e, 0x73, 0x4d, 0x98, 0x0c, 0x03, 0x06,
4533 0x9c, 0x0b, 0x73, 0xe6, 0xc0, 0xb5, 0x18, 0x63,
4534 0x0d, 0x39, 0xe0, 0x5b, 0x02, 0xd4, 0xc6, 0x19,
4535 0x1a, 0x79, 0x8c, 0x32, 0x34, 0x24, 0xf0, 0x16,
4536 0x81, 0x13, 0xf7, 0xff, 0x80, 0x02, 0x00, 0x01,
4537 0xf1, 0x80, 0x80, 0x80, 0xa0, 0x00, 0x00, 0x03,
4538 0x00, 0x20, 0x00, 0x00, 0x06, 0x50, 0x80, 0x00,
4540 0x00, 0x00, 0x00, 0x01, 0x68, 0xee, 0x31, 0x12,
4543 static const uint8_t avci100_720p_extradata[] = {
4545 0x00, 0x00, 0x00, 0x01, 0x67, 0x7a, 0x10, 0x29,
4546 0xb6, 0xd4, 0x20, 0x2a, 0x33, 0x1d, 0xc7, 0x62,
4547 0xa1, 0x08, 0x40, 0x54, 0x66, 0x3b, 0x8e, 0xc5,
4548 0x42, 0x02, 0x10, 0x25, 0x64, 0x2c, 0x89, 0xe8,
4549 0x85, 0xe4, 0x21, 0x4b, 0x90, 0x83, 0x06, 0x95,
4550 0xd1, 0x06, 0x46, 0x97, 0x20, 0xc8, 0xd7, 0x43,
4551 0x08, 0x11, 0xc2, 0x1e, 0x4c, 0x91, 0x0f, 0x01,
4552 0x40, 0x16, 0xec, 0x07, 0x8c, 0x04, 0x04, 0x05,
4553 0x00, 0x00, 0x03, 0x00, 0x01, 0x00, 0x00, 0x03,
4554 0x00, 0x64, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00,
4556 0x00, 0x00, 0x00, 0x01, 0x68, 0xce, 0x31, 0x12,
4559 static const uint8_t avci50_720p_extradata[] = {
4561 0x00, 0x00, 0x00, 0x01, 0x67, 0x6e, 0x10, 0x20,
4562 0xa6, 0xd4, 0x20, 0x32, 0x33, 0x0c, 0x71, 0x18,
4563 0x88, 0x62, 0x10, 0x19, 0x19, 0x86, 0x38, 0x8c,
4564 0x44, 0x30, 0x21, 0x02, 0x56, 0x4e, 0x6f, 0x37,
4565 0xcd, 0xf9, 0xbf, 0x81, 0x6b, 0xf3, 0x7c, 0xde,
4566 0x6e, 0x6c, 0xd3, 0x3c, 0x0f, 0x01, 0x6e, 0xff,
4567 0xc0, 0x00, 0xc0, 0x01, 0x38, 0xc0, 0x40, 0x40,
4568 0x50, 0x00, 0x00, 0x03, 0x00, 0x10, 0x00, 0x00,
4569 0x06, 0x48, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00,
4571 0x00, 0x00, 0x00, 0x01, 0x68, 0xee, 0x31, 0x12,
4575 const uint8_t *data = NULL;
4578 if (st->codec->width == 1920) {
4579 if (st->codec->field_order == AV_FIELD_PROGRESSIVE) {
4580 data = avci100_1080p_extradata;
4581 size = sizeof(avci100_1080p_extradata);
4583 data = avci100_1080i_extradata;
4584 size = sizeof(avci100_1080i_extradata);
4586 } else if (st->codec->width == 1440) {
4587 if (st->codec->field_order == AV_FIELD_PROGRESSIVE) {
4588 data = avci50_1080p_extradata;
4589 size = sizeof(avci50_1080p_extradata);
4591 data = avci50_1080i_extradata;
4592 size = sizeof(avci50_1080i_extradata);
4594 } else if (st->codec->width == 1280) {
4595 data = avci100_720p_extradata;
4596 size = sizeof(avci100_720p_extradata);
4597 } else if (st->codec->width == 960) {
4598 data = avci50_720p_extradata;
4599 size = sizeof(avci50_720p_extradata);
4605 av_freep(&st->codec->extradata);
4606 if (ff_alloc_extradata(st->codec, size))
4607 return AVERROR(ENOMEM);
4608 memcpy(st->codec->extradata, data, size);
4613 uint8_t *av_stream_get_side_data(AVStream *st, enum AVPacketSideDataType type,
4618 for (i = 0; i < st->nb_side_data; i++) {
4619 if (st->side_data[i].type == type) {
4621 *size = st->side_data[i].size;
4622 return st->side_data[i].data;
4628 uint8_t *av_stream_new_side_data(AVStream *st, enum AVPacketSideDataType type,
4631 AVPacketSideData *sd, *tmp;
4633 uint8_t *data = av_malloc(size);
4638 for (i = 0; i < st->nb_side_data; i++) {
4639 sd = &st->side_data[i];
4641 if (sd->type == type) {
4642 av_freep(&sd->data);
4649 tmp = av_realloc_array(st->side_data, st->nb_side_data + 1, sizeof(*tmp));
4655 st->side_data = tmp;
4658 sd = &st->side_data[st->nb_side_data - 1];
4665 int ff_stream_add_bitstream_filter(AVStream *st, const char *name, const char *args)
4667 AVBitStreamFilterContext *bsfc = NULL;
4668 AVBitStreamFilterContext **dest = &st->internal->bsfc;
4669 while (*dest && (*dest)->next)
4670 dest = &(*dest)->next;
4672 if (!(bsfc = av_bitstream_filter_init(name))) {
4673 av_log(NULL, AV_LOG_ERROR, "Unknown bitstream filter '%s'\n", name);
4674 return AVERROR(EINVAL);
4676 if (args && !(bsfc->args = av_strdup(args))) {
4677 av_bitstream_filter_close(bsfc);
4678 return AVERROR(ENOMEM);
4680 av_log(st->codec, AV_LOG_VERBOSE,
4681 "Automatically inserted bitstream filter '%s'; args='%s'\n",
4682 name, args ? args : "");
4687 int av_apply_bitstream_filters(AVCodecContext *codec, AVPacket *pkt,
4688 AVBitStreamFilterContext *bsfc)
4692 AVPacket new_pkt = *pkt;
4693 int a = av_bitstream_filter_filter(bsfc, codec, NULL,
4694 &new_pkt.data, &new_pkt.size,
4695 pkt->data, pkt->size,
4696 pkt->flags & AV_PKT_FLAG_KEY);
4697 if(a == 0 && new_pkt.data != pkt->data) {
4698 uint8_t *t = av_malloc(new_pkt.size + AV_INPUT_BUFFER_PADDING_SIZE); //the new should be a subset of the old so cannot overflow
4700 memcpy(t, new_pkt.data, new_pkt.size);
4701 memset(t + new_pkt.size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
4706 a = AVERROR(ENOMEM);
4710 new_pkt.buf = av_buffer_create(new_pkt.data, new_pkt.size,
4711 av_buffer_default_free, NULL, 0);
4713 pkt->side_data = NULL;
4714 pkt->side_data_elems = 0;
4715 av_packet_unref(pkt);
4717 av_freep(&new_pkt.data);
4718 a = AVERROR(ENOMEM);
4722 av_log(codec, AV_LOG_ERROR,
4723 "Failed to open bitstream filter %s for stream %d with codec %s",
4724 bsfc->filter->name, pkt->stream_index,
4725 codec->codec ? codec->codec->name : "copy");
4736 void ff_format_io_close(AVFormatContext *s, AVIOContext **pb)
4739 s->io_close(s, *pb);
4743 int ff_parse_creation_time_metadata(AVFormatContext *s, int64_t *timestamp, int return_seconds)
4745 AVDictionaryEntry *entry;
4746 int64_t parsed_timestamp;
4748 if ((entry = av_dict_get(s->metadata, "creation_time", NULL, 0))) {
4749 if ((ret = av_parse_time(&parsed_timestamp, entry->value, 0)) >= 0) {
4750 *timestamp = return_seconds ? parsed_timestamp / 1000000 : parsed_timestamp;
4753 av_log(s, AV_LOG_WARNING, "Failed to parse creation_time %s\n", entry->value);