• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * various utility functions for use within FFmpeg
3  * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include <stdint.h>
23 
24 #include "config.h"
25 
26 #include "libavutil/avassert.h"
27 #include "libavutil/avstring.h"
28 #include "libavutil/dict.h"
29 #include "libavutil/internal.h"
30 #include "libavutil/mathematics.h"
31 #include "libavutil/opt.h"
32 #include "libavutil/parseutils.h"
33 #include "libavutil/pixfmt.h"
34 #include "libavutil/thread.h"
35 #include "libavutil/time.h"
36 #include "libavutil/timestamp.h"
37 
38 #include "libavcodec/bytestream.h"
39 #include "libavcodec/internal.h"
40 #include "libavcodec/packet_internal.h"
41 #include "libavcodec/raw.h"
42 
43 #include "avformat.h"
44 #include "avio_internal.h"
45 #include "id3v2.h"
46 #include "internal.h"
47 #if CONFIG_NETWORK
48 #include "network.h"
49 #endif
50 #include "url.h"
51 
52 #include "libavutil/ffversion.h"
53 const char av_format_ffversion[] = "FFmpeg version " FFMPEG_VERSION;
54 
55 static AVMutex avformat_mutex = AV_MUTEX_INITIALIZER;
56 
57 /**
58  * @file
59  * various utility functions for use within FFmpeg
60  */
61 
avformat_version(void)62 unsigned avformat_version(void)
63 {
64     av_assert0(LIBAVFORMAT_VERSION_MICRO >= 100);
65     return LIBAVFORMAT_VERSION_INT;
66 }
67 
avformat_configuration(void)68 const char *avformat_configuration(void)
69 {
70     return FFMPEG_CONFIGURATION;
71 }
72 
avformat_license(void)73 const char *avformat_license(void)
74 {
75 #define LICENSE_PREFIX "libavformat license: "
76     return &LICENSE_PREFIX FFMPEG_LICENSE[sizeof(LICENSE_PREFIX) - 1];
77 }
78 
ff_lock_avformat(void)79 int ff_lock_avformat(void)
80 {
81     return ff_mutex_lock(&avformat_mutex) ? -1 : 0;
82 }
83 
ff_unlock_avformat(void)84 int ff_unlock_avformat(void)
85 {
86     return ff_mutex_unlock(&avformat_mutex) ? -1 : 0;
87 }
88 
89 #define RELATIVE_TS_BASE (INT64_MAX - (1LL<<48))
90 
is_relative(int64_t ts)91 static int is_relative(int64_t ts) {
92     return ts > (RELATIVE_TS_BASE - (1LL<<48));
93 }
94 
95 /**
96  * Wrap a given time stamp, if there is an indication for an overflow
97  *
98  * @param st stream
99  * @param timestamp the time stamp to wrap
100  * @return resulting time stamp
101  */
wrap_timestamp(const AVStream * st,int64_t timestamp)102 static int64_t wrap_timestamp(const AVStream *st, int64_t timestamp)
103 {
104     if (st->internal->pts_wrap_behavior != AV_PTS_WRAP_IGNORE && st->pts_wrap_bits < 64 &&
105         st->internal->pts_wrap_reference != AV_NOPTS_VALUE && timestamp != AV_NOPTS_VALUE) {
106         if (st->internal->pts_wrap_behavior == AV_PTS_WRAP_ADD_OFFSET &&
107             timestamp < st->internal->pts_wrap_reference)
108             return timestamp + (1ULL << st->pts_wrap_bits);
109         else if (st->internal->pts_wrap_behavior == AV_PTS_WRAP_SUB_OFFSET &&
110             timestamp >= st->internal->pts_wrap_reference)
111             return timestamp - (1ULL << st->pts_wrap_bits);
112     }
113     return timestamp;
114 }
115 
116 #if FF_API_FORMAT_GET_SET
MAKE_ACCESSORS(AVStream,stream,AVRational,r_frame_rate)117 MAKE_ACCESSORS(AVStream, stream, AVRational, r_frame_rate)
118 #if FF_API_LAVF_FFSERVER
119 FF_DISABLE_DEPRECATION_WARNINGS
120 MAKE_ACCESSORS(AVStream, stream, char *, recommended_encoder_configuration)
121 FF_ENABLE_DEPRECATION_WARNINGS
122 #endif
123 MAKE_ACCESSORS(AVFormatContext, format, AVCodec *, video_codec)
124 MAKE_ACCESSORS(AVFormatContext, format, AVCodec *, audio_codec)
125 MAKE_ACCESSORS(AVFormatContext, format, AVCodec *, subtitle_codec)
126 MAKE_ACCESSORS(AVFormatContext, format, AVCodec *, data_codec)
127 MAKE_ACCESSORS(AVFormatContext, format, int, metadata_header_padding)
128 MAKE_ACCESSORS(AVFormatContext, format, void *, opaque)
129 MAKE_ACCESSORS(AVFormatContext, format, av_format_control_message, control_message_cb)
130 #if FF_API_OLD_OPEN_CALLBACKS
131 FF_DISABLE_DEPRECATION_WARNINGS
132 MAKE_ACCESSORS(AVFormatContext, format, AVOpenCallback, open_cb)
133 FF_ENABLE_DEPRECATION_WARNINGS
134 #endif
135 #endif
136 
137 int64_t av_stream_get_end_pts(const AVStream *st)
138 {
139     if (st->internal->priv_pts) {
140         return st->internal->priv_pts->val;
141     } else
142         return AV_NOPTS_VALUE;
143 }
144 
av_stream_get_parser(const AVStream * st)145 struct AVCodecParserContext *av_stream_get_parser(const AVStream *st)
146 {
147     return st->parser;
148 }
149 
av_format_inject_global_side_data(AVFormatContext * s)150 void av_format_inject_global_side_data(AVFormatContext *s)
151 {
152     int i;
153     s->internal->inject_global_side_data = 1;
154     for (i = 0; i < s->nb_streams; i++) {
155         AVStream *st = s->streams[i];
156         st->internal->inject_global_side_data = 1;
157     }
158 }
159 
ff_copy_whiteblacklists(AVFormatContext * dst,const AVFormatContext * src)160 int ff_copy_whiteblacklists(AVFormatContext *dst, const AVFormatContext *src)
161 {
162     av_assert0(!dst->codec_whitelist &&
163                !dst->format_whitelist &&
164                !dst->protocol_whitelist &&
165                !dst->protocol_blacklist);
166     dst-> codec_whitelist = av_strdup(src->codec_whitelist);
167     dst->format_whitelist = av_strdup(src->format_whitelist);
168     dst->protocol_whitelist = av_strdup(src->protocol_whitelist);
169     dst->protocol_blacklist = av_strdup(src->protocol_blacklist);
170     if (   (src-> codec_whitelist && !dst-> codec_whitelist)
171         || (src->  format_whitelist && !dst->  format_whitelist)
172         || (src->protocol_whitelist && !dst->protocol_whitelist)
173         || (src->protocol_blacklist && !dst->protocol_blacklist)) {
174         av_log(dst, AV_LOG_ERROR, "Failed to duplicate black/whitelist\n");
175         return AVERROR(ENOMEM);
176     }
177     return 0;
178 }
179 
find_decoder(AVFormatContext * s,const AVStream * st,enum AVCodecID codec_id)180 static const AVCodec *find_decoder(AVFormatContext *s, const AVStream *st, enum AVCodecID codec_id)
181 {
182 #if FF_API_LAVF_AVCTX
183 FF_DISABLE_DEPRECATION_WARNINGS
184     if (st->codec->codec)
185         return st->codec->codec;
186 FF_ENABLE_DEPRECATION_WARNINGS
187 #endif
188 
189     switch (st->codecpar->codec_type) {
190     case AVMEDIA_TYPE_VIDEO:
191         if (s->video_codec)    return s->video_codec;
192         break;
193     case AVMEDIA_TYPE_AUDIO:
194         if (s->audio_codec)    return s->audio_codec;
195         break;
196     case AVMEDIA_TYPE_SUBTITLE:
197         if (s->subtitle_codec) return s->subtitle_codec;
198         break;
199     }
200 
201     return avcodec_find_decoder(codec_id);
202 }
203 
find_probe_decoder(AVFormatContext * s,const AVStream * st,enum AVCodecID codec_id)204 static const AVCodec *find_probe_decoder(AVFormatContext *s, const AVStream *st, enum AVCodecID codec_id)
205 {
206     const AVCodec *codec;
207 
208 #if CONFIG_H264_DECODER
209     /* Other parts of the code assume this decoder to be used for h264,
210      * so force it if possible. */
211     if (codec_id == AV_CODEC_ID_H264)
212         return avcodec_find_decoder_by_name("h264");
213 #endif
214 
215     codec = find_decoder(s, st, codec_id);
216     if (!codec)
217         return NULL;
218 
219     if (codec->capabilities & AV_CODEC_CAP_AVOID_PROBING) {
220         const AVCodec *probe_codec = NULL;
221         void *iter = NULL;
222         while ((probe_codec = av_codec_iterate(&iter))) {
223             if (probe_codec->id == codec->id &&
224                     av_codec_is_decoder(probe_codec) &&
225                     !(probe_codec->capabilities & (AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_EXPERIMENTAL))) {
226                 return probe_codec;
227             }
228         }
229     }
230 
231     return codec;
232 }
233 
234 #if FF_API_FORMAT_GET_SET
av_format_get_probe_score(const AVFormatContext * s)235 int av_format_get_probe_score(const AVFormatContext *s)
236 {
237     return s->probe_score;
238 }
239 #endif
240 
241 /* an arbitrarily chosen "sane" max packet size -- 50M */
242 #define SANE_CHUNK_SIZE (50000000)
243 
ffio_limit(AVIOContext * s,int size)244 int ffio_limit(AVIOContext *s, int size)
245 {
246     if (s->maxsize>= 0) {
247         int64_t pos = avio_tell(s);
248         int64_t remaining= s->maxsize - pos;
249         if (remaining < size) {
250             int64_t newsize = avio_size(s);
251             if (!s->maxsize || s->maxsize<newsize)
252                 s->maxsize = newsize - !newsize;
253             if (pos > s->maxsize && s->maxsize >= 0)
254                 s->maxsize = AVERROR(EIO);
255             if (s->maxsize >= 0)
256                 remaining = s->maxsize - pos;
257         }
258 
259         if (s->maxsize >= 0 && remaining < size && size > 1) {
260             av_log(NULL, remaining ? AV_LOG_ERROR : AV_LOG_DEBUG,
261                    "Truncating packet of size %d to %"PRId64"\n",
262                    size, remaining + !remaining);
263             size = remaining + !remaining;
264         }
265     }
266     return size;
267 }
268 
269 /* Read the data in sane-sized chunks and append to pkt.
270  * Return the number of bytes read or an error. */
append_packet_chunked(AVIOContext * s,AVPacket * pkt,int size)271 static int append_packet_chunked(AVIOContext *s, AVPacket *pkt, int size)
272 {
273     int orig_size      = pkt->size;
274     int ret;
275 
276     do {
277         int prev_size = pkt->size;
278         int read_size;
279 
280         /* When the caller requests a lot of data, limit it to the amount
281          * left in file or SANE_CHUNK_SIZE when it is not known. */
282         read_size = size;
283         if (read_size > SANE_CHUNK_SIZE/10) {
284             read_size = ffio_limit(s, read_size);
285             // If filesize/maxsize is unknown, limit to SANE_CHUNK_SIZE
286             if (s->maxsize < 0)
287                 read_size = FFMIN(read_size, SANE_CHUNK_SIZE);
288         }
289 
290         ret = av_grow_packet(pkt, read_size);
291         if (ret < 0)
292             break;
293 
294         ret = avio_read(s, pkt->data + prev_size, read_size);
295         if (ret != read_size) {
296             av_shrink_packet(pkt, prev_size + FFMAX(ret, 0));
297             break;
298         }
299 
300         size -= read_size;
301     } while (size > 0);
302     if (size > 0)
303         pkt->flags |= AV_PKT_FLAG_CORRUPT;
304 
305     if (!pkt->size)
306         av_packet_unref(pkt);
307     return pkt->size > orig_size ? pkt->size - orig_size : ret;
308 }
309 
av_get_packet(AVIOContext * s,AVPacket * pkt,int size)310 int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
311 {
312 #if FF_API_INIT_PACKET
313 FF_DISABLE_DEPRECATION_WARNINGS
314     av_init_packet(pkt);
315     pkt->data = NULL;
316     pkt->size = 0;
317 FF_ENABLE_DEPRECATION_WARNINGS
318 #else
319     av_packet_unref(pkt);
320 #endif
321     pkt->pos  = avio_tell(s);
322 
323     return append_packet_chunked(s, pkt, size);
324 }
325 
av_append_packet(AVIOContext * s,AVPacket * pkt,int size)326 int av_append_packet(AVIOContext *s, AVPacket *pkt, int size)
327 {
328     if (!pkt->size)
329         return av_get_packet(s, pkt, size);
330     return append_packet_chunked(s, pkt, size);
331 }
332 
av_filename_number_test(const char * filename)333 int av_filename_number_test(const char *filename)
334 {
335     char buf[1024];
336     return filename &&
337            (av_get_frame_filename(buf, sizeof(buf), filename, 1) >= 0);
338 }
339 
set_codec_from_probe_data(AVFormatContext * s,AVStream * st,AVProbeData * pd)340 static int set_codec_from_probe_data(AVFormatContext *s, AVStream *st,
341                                      AVProbeData *pd)
342 {
343     static const struct {
344         const char *name;
345         enum AVCodecID id;
346         enum AVMediaType type;
347     } fmt_id_type[] = {
348         { "aac",       AV_CODEC_ID_AAC,        AVMEDIA_TYPE_AUDIO },
349         { "ac3",       AV_CODEC_ID_AC3,        AVMEDIA_TYPE_AUDIO },
350         { "aptx",      AV_CODEC_ID_APTX,       AVMEDIA_TYPE_AUDIO },
351         { "dts",       AV_CODEC_ID_DTS,        AVMEDIA_TYPE_AUDIO },
352         { "dvbsub",    AV_CODEC_ID_DVB_SUBTITLE,AVMEDIA_TYPE_SUBTITLE },
353         { "dvbtxt",    AV_CODEC_ID_DVB_TELETEXT,AVMEDIA_TYPE_SUBTITLE },
354         { "eac3",      AV_CODEC_ID_EAC3,       AVMEDIA_TYPE_AUDIO },
355         { "h264",      AV_CODEC_ID_H264,       AVMEDIA_TYPE_VIDEO },
356         { "hevc",      AV_CODEC_ID_HEVC,       AVMEDIA_TYPE_VIDEO },
357         { "loas",      AV_CODEC_ID_AAC_LATM,   AVMEDIA_TYPE_AUDIO },
358         { "m4v",       AV_CODEC_ID_MPEG4,      AVMEDIA_TYPE_VIDEO },
359         { "mjpeg_2000",AV_CODEC_ID_JPEG2000,   AVMEDIA_TYPE_VIDEO },
360         { "mp3",       AV_CODEC_ID_MP3,        AVMEDIA_TYPE_AUDIO },
361         { "mpegvideo", AV_CODEC_ID_MPEG2VIDEO, AVMEDIA_TYPE_VIDEO },
362         { "truehd",    AV_CODEC_ID_TRUEHD,     AVMEDIA_TYPE_AUDIO },
363         { 0 }
364     };
365     int score;
366     const AVInputFormat *fmt = av_probe_input_format3(pd, 1, &score);
367 
368     if (fmt) {
369         int i;
370         av_log(s, AV_LOG_DEBUG,
371                "Probe with size=%d, packets=%d detected %s with score=%d\n",
372                pd->buf_size, s->max_probe_packets - st->probe_packets,
373                fmt->name, score);
374         for (i = 0; fmt_id_type[i].name; i++) {
375             if (!strcmp(fmt->name, fmt_id_type[i].name)) {
376                 if (fmt_id_type[i].type != AVMEDIA_TYPE_AUDIO &&
377                     st->codecpar->sample_rate)
378                     continue;
379                 if (st->internal->request_probe > score &&
380                     st->codecpar->codec_id != fmt_id_type[i].id)
381                     continue;
382                 st->codecpar->codec_id   = fmt_id_type[i].id;
383                 st->codecpar->codec_type = fmt_id_type[i].type;
384                 st->internal->need_context_update = 1;
385 #if FF_API_LAVF_AVCTX
386 FF_DISABLE_DEPRECATION_WARNINGS
387                 st->codec->codec_type = st->codecpar->codec_type;
388                 st->codec->codec_id   = st->codecpar->codec_id;
389 FF_ENABLE_DEPRECATION_WARNINGS
390 #endif
391                 return score;
392             }
393         }
394     }
395     return 0;
396 }
397 
398 /************************************************************/
399 /* input media file */
400 
401 #if FF_API_DEMUXER_OPEN
av_demuxer_open(AVFormatContext * ic)402 int av_demuxer_open(AVFormatContext *ic) {
403     int err;
404 
405     if (ic->format_whitelist && av_match_list(ic->iformat->name, ic->format_whitelist, ',') <= 0) {
406         av_log(ic, AV_LOG_ERROR, "Format not on whitelist \'%s\'\n", ic->format_whitelist);
407         return AVERROR(EINVAL);
408     }
409 
410     if (ic->iformat->read_header) {
411         err = ic->iformat->read_header(ic);
412         if (err < 0)
413             return err;
414     }
415 
416     if (ic->pb && !ic->internal->data_offset)
417         ic->internal->data_offset = avio_tell(ic->pb);
418 
419     return 0;
420 }
421 #endif
422 /* Open input file and probe the format if necessary. */
init_input(AVFormatContext * s,const char * filename,AVDictionary ** options)423 static int init_input(AVFormatContext *s, const char *filename,
424                       AVDictionary **options)
425 {
426     int ret;
427     AVProbeData pd = { filename, NULL, 0 };
428     int score = AVPROBE_SCORE_RETRY;
429 
430     if (s->pb) {
431         s->flags |= AVFMT_FLAG_CUSTOM_IO;
432         if (!s->iformat)
433             return av_probe_input_buffer2(s->pb, &s->iformat, filename,
434                                          s, 0, s->format_probesize);
435         else if (s->iformat->flags & AVFMT_NOFILE)
436             av_log(s, AV_LOG_WARNING, "Custom AVIOContext makes no sense and "
437                                       "will be ignored with AVFMT_NOFILE format.\n");
438         return 0;
439     }
440 
441     if ((s->iformat && s->iformat->flags & AVFMT_NOFILE) ||
442         (!s->iformat && (s->iformat = av_probe_input_format2(&pd, 0, &score))))
443         return score;
444 
445     if ((ret = s->io_open(s, &s->pb, filename, AVIO_FLAG_READ | s->avio_flags, options)) < 0)
446         return ret;
447 
448     if (s->iformat)
449         return 0;
450     return av_probe_input_buffer2(s->pb, &s->iformat, filename,
451                                  s, 0, s->format_probesize);
452 }
453 
avformat_queue_attached_pictures(AVFormatContext * s)454 int avformat_queue_attached_pictures(AVFormatContext *s)
455 {
456     int i, ret;
457     for (i = 0; i < s->nb_streams; i++)
458         if (s->streams[i]->disposition & AV_DISPOSITION_ATTACHED_PIC &&
459             s->streams[i]->discard < AVDISCARD_ALL) {
460             if (s->streams[i]->attached_pic.size <= 0) {
461                 av_log(s, AV_LOG_WARNING,
462                     "Attached picture on stream %d has invalid size, "
463                     "ignoring\n", i);
464                 continue;
465             }
466 
467             ret = avpriv_packet_list_put(&s->internal->raw_packet_buffer,
468                                      &s->internal->raw_packet_buffer_end,
469                                      &s->streams[i]->attached_pic,
470                                      av_packet_ref, 0);
471             if (ret < 0)
472                 return ret;
473         }
474     return 0;
475 }
476 
update_stream_avctx(AVFormatContext * s)477 static int update_stream_avctx(AVFormatContext *s)
478 {
479     int i, ret;
480     for (i = 0; i < s->nb_streams; i++) {
481         AVStream *st = s->streams[i];
482 
483         if (!st->internal->need_context_update)
484             continue;
485 
486         /* close parser, because it depends on the codec */
487         if (st->parser && st->internal->avctx->codec_id != st->codecpar->codec_id) {
488             av_parser_close(st->parser);
489             st->parser = NULL;
490         }
491 
492         /* update internal codec context, for the parser */
493         ret = avcodec_parameters_to_context(st->internal->avctx, st->codecpar);
494         if (ret < 0)
495             return ret;
496 
497 #if FF_API_LAVF_AVCTX
498 FF_DISABLE_DEPRECATION_WARNINGS
499         /* update deprecated public codec context */
500         ret = avcodec_parameters_to_context(st->codec, st->codecpar);
501         if (ret < 0)
502             return ret;
503 FF_ENABLE_DEPRECATION_WARNINGS
504 #endif
505 
506         st->internal->need_context_update = 0;
507     }
508     return 0;
509 }
510 
511 
avformat_open_input(AVFormatContext ** ps,const char * filename,ff_const59 AVInputFormat * fmt,AVDictionary ** options)512 int avformat_open_input(AVFormatContext **ps, const char *filename,
513                         ff_const59 AVInputFormat *fmt, AVDictionary **options)
514 {
515     AVFormatContext *s = *ps;
516     int i, ret = 0;
517     AVDictionary *tmp = NULL;
518     ID3v2ExtraMeta *id3v2_extra_meta = NULL;
519 
520     if (!s && !(s = avformat_alloc_context()))
521         return AVERROR(ENOMEM);
522     if (!s->av_class) {
523         av_log(NULL, AV_LOG_ERROR, "Input context has not been properly allocated by avformat_alloc_context() and is not NULL either\n");
524         return AVERROR(EINVAL);
525     }
526     if (fmt)
527         s->iformat = fmt;
528 
529     if (options)
530         av_dict_copy(&tmp, *options, 0);
531 
532     if (s->pb) // must be before any goto fail
533         s->flags |= AVFMT_FLAG_CUSTOM_IO;
534 
535     if ((ret = av_opt_set_dict(s, &tmp)) < 0)
536         goto fail;
537 
538     if (!(s->url = av_strdup(filename ? filename : ""))) {
539         ret = AVERROR(ENOMEM);
540         goto fail;
541     }
542 
543 #if FF_API_FORMAT_FILENAME
544 FF_DISABLE_DEPRECATION_WARNINGS
545     av_strlcpy(s->filename, filename ? filename : "", sizeof(s->filename));
546 FF_ENABLE_DEPRECATION_WARNINGS
547 #endif
548     if ((ret = init_input(s, filename, &tmp)) < 0)
549         goto fail;
550     s->probe_score = ret;
551 
552     if (!s->protocol_whitelist && s->pb && s->pb->protocol_whitelist) {
553         s->protocol_whitelist = av_strdup(s->pb->protocol_whitelist);
554         if (!s->protocol_whitelist) {
555             ret = AVERROR(ENOMEM);
556             goto fail;
557         }
558     }
559 
560     if (!s->protocol_blacklist && s->pb && s->pb->protocol_blacklist) {
561         s->protocol_blacklist = av_strdup(s->pb->protocol_blacklist);
562         if (!s->protocol_blacklist) {
563             ret = AVERROR(ENOMEM);
564             goto fail;
565         }
566     }
567 
568     if (s->format_whitelist && av_match_list(s->iformat->name, s->format_whitelist, ',') <= 0) {
569         av_log(s, AV_LOG_ERROR, "Format not on whitelist \'%s\'\n", s->format_whitelist);
570         ret = AVERROR(EINVAL);
571         goto fail;
572     }
573 
574     avio_skip(s->pb, s->skip_initial_bytes);
575 
576     /* Check filename in case an image number is expected. */
577     if (s->iformat->flags & AVFMT_NEEDNUMBER) {
578         if (!av_filename_number_test(filename)) {
579             ret = AVERROR(EINVAL);
580             goto fail;
581         }
582     }
583 
584     s->duration = s->start_time = AV_NOPTS_VALUE;
585 
586     /* Allocate private data. */
587     if (s->iformat->priv_data_size > 0) {
588         if (!(s->priv_data = av_mallocz(s->iformat->priv_data_size))) {
589             ret = AVERROR(ENOMEM);
590             goto fail;
591         }
592         if (s->iformat->priv_class) {
593             *(const AVClass **) s->priv_data = s->iformat->priv_class;
594             av_opt_set_defaults(s->priv_data);
595             if ((ret = av_opt_set_dict(s->priv_data, &tmp)) < 0)
596                 goto fail;
597         }
598     }
599 
600     /* e.g. AVFMT_NOFILE formats will not have a AVIOContext */
601     if (s->pb)
602         ff_id3v2_read_dict(s->pb, &s->internal->id3v2_meta, ID3v2_DEFAULT_MAGIC, &id3v2_extra_meta);
603 
604 #if FF_API_DEMUXER_OPEN
605     if (!(s->flags&AVFMT_FLAG_PRIV_OPT) && s->iformat->read_header)
606 #else
607     if (s->iformat->read_header)
608 #endif
609         if ((ret = s->iformat->read_header(s)) < 0)
610             goto fail;
611 
612     if (!s->metadata) {
613         s->metadata = s->internal->id3v2_meta;
614         s->internal->id3v2_meta = NULL;
615     } else if (s->internal->id3v2_meta) {
616         av_log(s, AV_LOG_WARNING, "Discarding ID3 tags because more suitable tags were found.\n");
617         av_dict_free(&s->internal->id3v2_meta);
618     }
619 
620     if (id3v2_extra_meta) {
621         if (!strcmp(s->iformat->name, "mp3") || !strcmp(s->iformat->name, "aac") ||
622             !strcmp(s->iformat->name, "tta") || !strcmp(s->iformat->name, "wav")) {
623             if ((ret = ff_id3v2_parse_apic(s, id3v2_extra_meta)) < 0)
624                 goto close;
625             if ((ret = ff_id3v2_parse_chapters(s, id3v2_extra_meta)) < 0)
626                 goto close;
627             if ((ret = ff_id3v2_parse_priv(s, id3v2_extra_meta)) < 0)
628                 goto close;
629         } else
630             av_log(s, AV_LOG_DEBUG, "demuxer does not support additional id3 data, skipping\n");
631     }
632     ff_id3v2_free_extra_meta(&id3v2_extra_meta);
633 
634     if ((ret = avformat_queue_attached_pictures(s)) < 0)
635         goto close;
636 
637 #if FF_API_DEMUXER_OPEN
638     if (!(s->flags&AVFMT_FLAG_PRIV_OPT) && s->pb && !s->internal->data_offset)
639 #else
640     if (s->pb && !s->internal->data_offset)
641 #endif
642         s->internal->data_offset = avio_tell(s->pb);
643 
644     s->internal->raw_packet_buffer_remaining_size = RAW_PACKET_BUFFER_SIZE;
645 
646     update_stream_avctx(s);
647 
648     for (i = 0; i < s->nb_streams; i++)
649         s->streams[i]->internal->orig_codec_id = s->streams[i]->codecpar->codec_id;
650 
651     if (options) {
652         av_dict_free(options);
653         *options = tmp;
654     }
655     *ps = s;
656     return 0;
657 
658 close:
659     if (s->iformat->read_close)
660         s->iformat->read_close(s);
661 fail:
662     ff_id3v2_free_extra_meta(&id3v2_extra_meta);
663     av_dict_free(&tmp);
664     if (s->pb && !(s->flags & AVFMT_FLAG_CUSTOM_IO))
665         avio_closep(&s->pb);
666     avformat_free_context(s);
667     *ps = NULL;
668     return ret;
669 }
670 
671 /*******************************************************/
672 
force_codec_ids(AVFormatContext * s,AVStream * st)673 static void force_codec_ids(AVFormatContext *s, AVStream *st)
674 {
675     switch (st->codecpar->codec_type) {
676     case AVMEDIA_TYPE_VIDEO:
677         if (s->video_codec_id)
678             st->codecpar->codec_id = s->video_codec_id;
679         break;
680     case AVMEDIA_TYPE_AUDIO:
681         if (s->audio_codec_id)
682             st->codecpar->codec_id = s->audio_codec_id;
683         break;
684     case AVMEDIA_TYPE_SUBTITLE:
685         if (s->subtitle_codec_id)
686             st->codecpar->codec_id = s->subtitle_codec_id;
687         break;
688     case AVMEDIA_TYPE_DATA:
689         if (s->data_codec_id)
690             st->codecpar->codec_id = s->data_codec_id;
691         break;
692     }
693 }
694 
probe_codec(AVFormatContext * s,AVStream * st,const AVPacket * pkt)695 static int probe_codec(AVFormatContext *s, AVStream *st, const AVPacket *pkt)
696 {
697     if (st->internal->request_probe>0) {
698         AVProbeData *pd = &st->internal->probe_data;
699         int end;
700         av_log(s, AV_LOG_DEBUG, "probing stream %d pp:%d\n", st->index, st->probe_packets);
701         --st->probe_packets;
702 
703         if (pkt) {
704             uint8_t *new_buf = av_realloc(pd->buf, pd->buf_size+pkt->size+AVPROBE_PADDING_SIZE);
705             if (!new_buf) {
706                 av_log(s, AV_LOG_WARNING,
707                        "Failed to reallocate probe buffer for stream %d\n",
708                        st->index);
709                 goto no_packet;
710             }
711             pd->buf = new_buf;
712             memcpy(pd->buf + pd->buf_size, pkt->data, pkt->size);
713             pd->buf_size += pkt->size;
714             memset(pd->buf + pd->buf_size, 0, AVPROBE_PADDING_SIZE);
715         } else {
716 no_packet:
717             st->probe_packets = 0;
718             if (!pd->buf_size) {
719                 av_log(s, AV_LOG_WARNING,
720                        "nothing to probe for stream %d\n", st->index);
721             }
722         }
723 
724         end=    s->internal->raw_packet_buffer_remaining_size <= 0
725                 || st->probe_packets<= 0;
726 
727         if (end || av_log2(pd->buf_size) != av_log2(pd->buf_size - pkt->size)) {
728             int score = set_codec_from_probe_data(s, st, pd);
729             if (    (st->codecpar->codec_id != AV_CODEC_ID_NONE && score > AVPROBE_SCORE_STREAM_RETRY)
730                 || end) {
731                 pd->buf_size = 0;
732                 av_freep(&pd->buf);
733                 st->internal->request_probe = -1;
734                 if (st->codecpar->codec_id != AV_CODEC_ID_NONE) {
735                     av_log(s, AV_LOG_DEBUG, "probed stream %d\n", st->index);
736                 } else
737                     av_log(s, AV_LOG_WARNING, "probed stream %d failed\n", st->index);
738             }
739             force_codec_ids(s, st);
740         }
741     }
742     return 0;
743 }
744 
update_wrap_reference(AVFormatContext * s,AVStream * st,int stream_index,AVPacket * pkt)745 static int update_wrap_reference(AVFormatContext *s, AVStream *st, int stream_index, AVPacket *pkt)
746 {
747     int64_t ref = pkt->dts;
748     int i, pts_wrap_behavior;
749     int64_t pts_wrap_reference;
750     AVProgram *first_program;
751 
752     if (ref == AV_NOPTS_VALUE)
753         ref = pkt->pts;
754     if (st->internal->pts_wrap_reference != AV_NOPTS_VALUE || st->pts_wrap_bits >= 63 || ref == AV_NOPTS_VALUE || !s->correct_ts_overflow)
755         return 0;
756     ref &= (1LL << st->pts_wrap_bits)-1;
757 
758     // reference time stamp should be 60 s before first time stamp
759     pts_wrap_reference = ref - av_rescale(60, st->time_base.den, st->time_base.num);
760     // if first time stamp is not more than 1/8 and 60s before the wrap point, subtract rather than add wrap offset
761     pts_wrap_behavior = (ref < (1LL << st->pts_wrap_bits) - (1LL << st->pts_wrap_bits-3)) ||
762         (ref < (1LL << st->pts_wrap_bits) - av_rescale(60, st->time_base.den, st->time_base.num)) ?
763         AV_PTS_WRAP_ADD_OFFSET : AV_PTS_WRAP_SUB_OFFSET;
764 
765     first_program = av_find_program_from_stream(s, NULL, stream_index);
766 
767     if (!first_program) {
768         int default_stream_index = av_find_default_stream_index(s);
769         if (s->streams[default_stream_index]->internal->pts_wrap_reference == AV_NOPTS_VALUE) {
770             for (i = 0; i < s->nb_streams; i++) {
771                 if (av_find_program_from_stream(s, NULL, i))
772                     continue;
773                 s->streams[i]->internal->pts_wrap_reference = pts_wrap_reference;
774                 s->streams[i]->internal->pts_wrap_behavior = pts_wrap_behavior;
775             }
776         }
777         else {
778             st->internal->pts_wrap_reference = s->streams[default_stream_index]->internal->pts_wrap_reference;
779             st->internal->pts_wrap_behavior = s->streams[default_stream_index]->internal->pts_wrap_behavior;
780         }
781     }
782     else {
783         AVProgram *program = first_program;
784         while (program) {
785             if (program->pts_wrap_reference != AV_NOPTS_VALUE) {
786                 pts_wrap_reference = program->pts_wrap_reference;
787                 pts_wrap_behavior = program->pts_wrap_behavior;
788                 break;
789             }
790             program = av_find_program_from_stream(s, program, stream_index);
791         }
792 
793         // update every program with differing pts_wrap_reference
794         program = first_program;
795         while (program) {
796             if (program->pts_wrap_reference != pts_wrap_reference) {
797                 for (i = 0; i<program->nb_stream_indexes; i++) {
798                     s->streams[program->stream_index[i]]->internal->pts_wrap_reference = pts_wrap_reference;
799                     s->streams[program->stream_index[i]]->internal->pts_wrap_behavior = pts_wrap_behavior;
800                 }
801 
802                 program->pts_wrap_reference = pts_wrap_reference;
803                 program->pts_wrap_behavior = pts_wrap_behavior;
804             }
805             program = av_find_program_from_stream(s, program, stream_index);
806         }
807     }
808     return 1;
809 }
810 
ff_read_packet(AVFormatContext * s,AVPacket * pkt)811 int ff_read_packet(AVFormatContext *s, AVPacket *pkt)
812 {
813     int ret, i, err;
814     AVStream *st;
815 
816 #if FF_API_INIT_PACKET
817 FF_DISABLE_DEPRECATION_WARNINGS
818     pkt->data = NULL;
819     pkt->size = 0;
820     av_init_packet(pkt);
821 FF_ENABLE_DEPRECATION_WARNINGS
822 #else
823     av_packet_unref(pkt);
824 #endif
825 
826     for (;;) {
827         PacketList *pktl = s->internal->raw_packet_buffer;
828         const AVPacket *pkt1;
829 
830         if (pktl) {
831             st = s->streams[pktl->pkt.stream_index];
832             if (s->internal->raw_packet_buffer_remaining_size <= 0)
833                 if ((err = probe_codec(s, st, NULL)) < 0)
834                     return err;
835             if (st->internal->request_probe <= 0) {
836                 avpriv_packet_list_get(&s->internal->raw_packet_buffer,
837                                    &s->internal->raw_packet_buffer_end, pkt);
838                 s->internal->raw_packet_buffer_remaining_size += pkt->size;
839                 return 0;
840             }
841         }
842 
843         ret = s->iformat->read_packet(s, pkt);
844         if (ret < 0) {
845             av_packet_unref(pkt);
846 
847             /* Some demuxers return FFERROR_REDO when they consume
848                data and discard it (ignored streams, junk, extradata).
849                We must re-call the demuxer to get the real packet. */
850             if (ret == FFERROR_REDO)
851                 continue;
852             if (!pktl || ret == AVERROR(EAGAIN))
853                 return ret;
854             for (i = 0; i < s->nb_streams; i++) {
855                 st = s->streams[i];
856                 if (st->probe_packets || st->internal->request_probe > 0)
857                     if ((err = probe_codec(s, st, NULL)) < 0)
858                         return err;
859                 av_assert0(st->internal->request_probe <= 0);
860             }
861             continue;
862         }
863 
864         err = av_packet_make_refcounted(pkt);
865         if (err < 0) {
866             av_packet_unref(pkt);
867             return err;
868         }
869 
870         if (pkt->flags & AV_PKT_FLAG_CORRUPT) {
871             av_log(s, AV_LOG_WARNING,
872                    "Packet corrupt (stream = %d, dts = %s)",
873                    pkt->stream_index, av_ts2str(pkt->dts));
874             if (s->flags & AVFMT_FLAG_DISCARD_CORRUPT) {
875                 av_log(s, AV_LOG_WARNING, ", dropping it.\n");
876                 av_packet_unref(pkt);
877                 continue;
878             }
879             av_log(s, AV_LOG_WARNING, ".\n");
880         }
881 
882         av_assert0(pkt->stream_index < (unsigned)s->nb_streams &&
883                    "Invalid stream index.\n");
884 
885         st = s->streams[pkt->stream_index];
886 
887         if (update_wrap_reference(s, st, pkt->stream_index, pkt) && st->internal->pts_wrap_behavior == AV_PTS_WRAP_SUB_OFFSET) {
888             // correct first time stamps to negative values
889             if (!is_relative(st->first_dts))
890                 st->first_dts = wrap_timestamp(st, st->first_dts);
891             if (!is_relative(st->start_time))
892                 st->start_time = wrap_timestamp(st, st->start_time);
893             if (!is_relative(st->cur_dts))
894                 st->cur_dts = wrap_timestamp(st, st->cur_dts);
895         }
896 
897         pkt->dts = wrap_timestamp(st, pkt->dts);
898         pkt->pts = wrap_timestamp(st, pkt->pts);
899 
900         force_codec_ids(s, st);
901 
902         /* TODO: audio: time filter; video: frame reordering (pts != dts) */
903         if (s->use_wallclock_as_timestamps)
904             pkt->dts = pkt->pts = av_rescale_q(av_gettime(), AV_TIME_BASE_Q, st->time_base);
905 
906         if (!pktl && st->internal->request_probe <= 0)
907             return ret;
908 
909         err = avpriv_packet_list_put(&s->internal->raw_packet_buffer,
910                                  &s->internal->raw_packet_buffer_end,
911                                  pkt, NULL, 0);
912         if (err < 0) {
913             av_packet_unref(pkt);
914             return err;
915         }
916         pkt1 = &s->internal->raw_packet_buffer_end->pkt;
917         s->internal->raw_packet_buffer_remaining_size -= pkt1->size;
918 
919         if ((err = probe_codec(s, st, pkt1)) < 0)
920             return err;
921     }
922 }
923 
924 
925 /**********************************************************/
926 
determinable_frame_size(AVCodecContext * avctx)927 static int determinable_frame_size(AVCodecContext *avctx)
928 {
929     switch(avctx->codec_id) {
930     case AV_CODEC_ID_MP1:
931     case AV_CODEC_ID_MP2:
932     case AV_CODEC_ID_MP3:
933     case AV_CODEC_ID_CODEC2:
934         return 1;
935     }
936 
937     return 0;
938 }
939 
940 /**
941  * Return the frame duration in seconds. Return 0 if not available.
942  */
ff_compute_frame_duration(AVFormatContext * s,int * pnum,int * pden,AVStream * st,AVCodecParserContext * pc,AVPacket * pkt)943 void ff_compute_frame_duration(AVFormatContext *s, int *pnum, int *pden, AVStream *st,
944                                AVCodecParserContext *pc, AVPacket *pkt)
945 {
946     AVRational codec_framerate = s->iformat ? st->internal->avctx->framerate :
947                                               av_mul_q(av_inv_q(st->internal->avctx->time_base), (AVRational){1, st->internal->avctx->ticks_per_frame});
948     int frame_size, sample_rate;
949 
950 #if FF_API_LAVF_AVCTX
951 FF_DISABLE_DEPRECATION_WARNINGS
952     if ((!codec_framerate.den || !codec_framerate.num) && st->codec->time_base.den && st->codec->time_base.num)
953         codec_framerate = av_mul_q(av_inv_q(st->codec->time_base), (AVRational){1, st->codec->ticks_per_frame});
954 FF_ENABLE_DEPRECATION_WARNINGS
955 #endif
956 
957     *pnum = 0;
958     *pden = 0;
959     switch (st->codecpar->codec_type) {
960     case AVMEDIA_TYPE_VIDEO:
961         if (st->r_frame_rate.num && !pc && s->iformat) {
962             *pnum = st->r_frame_rate.den;
963             *pden = st->r_frame_rate.num;
964         } else if (st->time_base.num * 1000LL > st->time_base.den) {
965             *pnum = st->time_base.num;
966             *pden = st->time_base.den;
967         } else if (codec_framerate.den * 1000LL > codec_framerate.num) {
968             av_assert0(st->internal->avctx->ticks_per_frame);
969             av_reduce(pnum, pden,
970                       codec_framerate.den,
971                       codec_framerate.num * (int64_t)st->internal->avctx->ticks_per_frame,
972                       INT_MAX);
973 
974             if (pc && pc->repeat_pict) {
975                 av_assert0(s->iformat); // this may be wrong for interlaced encoding but its not used for that case
976                 av_reduce(pnum, pden,
977                           (*pnum) * (1LL + pc->repeat_pict),
978                           (*pden),
979                           INT_MAX);
980             }
981             /* If this codec can be interlaced or progressive then we need
982              * a parser to compute duration of a packet. Thus if we have
983              * no parser in such case leave duration undefined. */
984             if (st->internal->avctx->ticks_per_frame > 1 && !pc)
985                 *pnum = *pden = 0;
986         }
987         break;
988     case AVMEDIA_TYPE_AUDIO:
989         if (st->internal->avctx_inited) {
990             frame_size = av_get_audio_frame_duration(st->internal->avctx, pkt->size);
991             sample_rate = st->internal->avctx->sample_rate;
992         } else {
993             frame_size = av_get_audio_frame_duration2(st->codecpar, pkt->size);
994             sample_rate = st->codecpar->sample_rate;
995         }
996         if (frame_size <= 0 || sample_rate <= 0)
997             break;
998         *pnum = frame_size;
999         *pden = sample_rate;
1000         break;
1001     default:
1002         break;
1003     }
1004 }
1005 
ff_is_intra_only(enum AVCodecID id)1006 int ff_is_intra_only(enum AVCodecID id)
1007 {
1008     const AVCodecDescriptor *d = avcodec_descriptor_get(id);
1009     if (!d)
1010         return 0;
1011     if ((d->type == AVMEDIA_TYPE_VIDEO || d->type == AVMEDIA_TYPE_AUDIO) &&
1012         !(d->props & AV_CODEC_PROP_INTRA_ONLY))
1013         return 0;
1014     return 1;
1015 }
1016 
has_decode_delay_been_guessed(AVStream * st)1017 static int has_decode_delay_been_guessed(AVStream *st)
1018 {
1019     if (st->codecpar->codec_id != AV_CODEC_ID_H264) return 1;
1020     if (!st->internal->info) // if we have left find_stream_info then nb_decoded_frames won't increase anymore for stream copy
1021         return 1;
1022 #if CONFIG_H264_DECODER
1023     if (st->internal->avctx->has_b_frames &&
1024        avpriv_h264_has_num_reorder_frames(st->internal->avctx) == st->internal->avctx->has_b_frames)
1025         return 1;
1026 #endif
1027     if (st->internal->avctx->has_b_frames<3)
1028         return st->internal->nb_decoded_frames >= 7;
1029     else if (st->internal->avctx->has_b_frames<4)
1030         return st->internal->nb_decoded_frames >= 18;
1031     else
1032         return st->internal->nb_decoded_frames >= 20;
1033 }
1034 
get_next_pkt(AVFormatContext * s,AVStream * st,PacketList * pktl)1035 static PacketList *get_next_pkt(AVFormatContext *s, AVStream *st, PacketList *pktl)
1036 {
1037     if (pktl->next)
1038         return pktl->next;
1039     if (pktl == s->internal->packet_buffer_end)
1040         return s->internal->parse_queue;
1041     return NULL;
1042 }
1043 
select_from_pts_buffer(AVStream * st,int64_t * pts_buffer,int64_t dts)1044 static int64_t select_from_pts_buffer(AVStream *st, int64_t *pts_buffer, int64_t dts) {
1045     int onein_oneout = st->codecpar->codec_id != AV_CODEC_ID_H264 &&
1046                        st->codecpar->codec_id != AV_CODEC_ID_HEVC;
1047 
1048     if(!onein_oneout) {
1049         int delay = st->internal->avctx->has_b_frames;
1050         int i;
1051 
1052         if (dts == AV_NOPTS_VALUE) {
1053             int64_t best_score = INT64_MAX;
1054             for (i = 0; i<delay; i++) {
1055                 if (st->internal->pts_reorder_error_count[i]) {
1056                     int64_t score = st->internal->pts_reorder_error[i] / st->internal->pts_reorder_error_count[i];
1057                     if (score < best_score) {
1058                         best_score = score;
1059                         dts = pts_buffer[i];
1060                     }
1061                 }
1062             }
1063         } else {
1064             for (i = 0; i<delay; i++) {
1065                 if (pts_buffer[i] != AV_NOPTS_VALUE) {
1066                     int64_t diff =  FFABS(pts_buffer[i] - dts)
1067                                     + (uint64_t)st->internal->pts_reorder_error[i];
1068                     diff = FFMAX(diff, st->internal->pts_reorder_error[i]);
1069                     st->internal->pts_reorder_error[i] = diff;
1070                     st->internal->pts_reorder_error_count[i]++;
1071                     if (st->internal->pts_reorder_error_count[i] > 250) {
1072                         st->internal->pts_reorder_error[i] >>= 1;
1073                         st->internal->pts_reorder_error_count[i] >>= 1;
1074                     }
1075                 }
1076             }
1077         }
1078     }
1079 
1080     if (dts == AV_NOPTS_VALUE)
1081         dts = pts_buffer[0];
1082 
1083     return dts;
1084 }
1085 
1086 /**
1087  * Updates the dts of packets of a stream in pkt_buffer, by re-ordering the pts
1088  * of the packets in a window.
1089  */
update_dts_from_pts(AVFormatContext * s,int stream_index,PacketList * pkt_buffer)1090 static void update_dts_from_pts(AVFormatContext *s, int stream_index,
1091                                 PacketList *pkt_buffer)
1092 {
1093     AVStream *st       = s->streams[stream_index];
1094     int delay          = st->internal->avctx->has_b_frames;
1095     int i;
1096 
1097     int64_t pts_buffer[MAX_REORDER_DELAY+1];
1098 
1099     for (i = 0; i<MAX_REORDER_DELAY+1; i++)
1100         pts_buffer[i] = AV_NOPTS_VALUE;
1101 
1102     for (; pkt_buffer; pkt_buffer = get_next_pkt(s, st, pkt_buffer)) {
1103         if (pkt_buffer->pkt.stream_index != stream_index)
1104             continue;
1105 
1106         if (pkt_buffer->pkt.pts != AV_NOPTS_VALUE && delay <= MAX_REORDER_DELAY) {
1107             pts_buffer[0] = pkt_buffer->pkt.pts;
1108             for (i = 0; i<delay && pts_buffer[i] > pts_buffer[i + 1]; i++)
1109                 FFSWAP(int64_t, pts_buffer[i], pts_buffer[i + 1]);
1110 
1111             pkt_buffer->pkt.dts = select_from_pts_buffer(st, pts_buffer, pkt_buffer->pkt.dts);
1112         }
1113     }
1114 }
1115 
update_initial_timestamps(AVFormatContext * s,int stream_index,int64_t dts,int64_t pts,AVPacket * pkt)1116 static void update_initial_timestamps(AVFormatContext *s, int stream_index,
1117                                       int64_t dts, int64_t pts, AVPacket *pkt)
1118 {
1119     AVStream *st       = s->streams[stream_index];
1120     PacketList *pktl = s->internal->packet_buffer ? s->internal->packet_buffer : s->internal->parse_queue;
1121     PacketList *pktl_it;
1122 
1123     uint64_t shift;
1124 
1125     if (st->first_dts != AV_NOPTS_VALUE ||
1126         dts           == AV_NOPTS_VALUE ||
1127         st->cur_dts   == AV_NOPTS_VALUE ||
1128         st->cur_dts < INT_MIN + RELATIVE_TS_BASE ||
1129         dts  < INT_MIN + (st->cur_dts - RELATIVE_TS_BASE) ||
1130         is_relative(dts))
1131         return;
1132 
1133     st->first_dts = dts - (st->cur_dts - RELATIVE_TS_BASE);
1134     st->cur_dts   = dts;
1135     shift         = (uint64_t)st->first_dts - RELATIVE_TS_BASE;
1136 
1137     if (is_relative(pts))
1138         pts += shift;
1139 
1140     for (pktl_it = pktl; pktl_it; pktl_it = get_next_pkt(s, st, pktl_it)) {
1141         if (pktl_it->pkt.stream_index != stream_index)
1142             continue;
1143         if (is_relative(pktl_it->pkt.pts))
1144             pktl_it->pkt.pts += shift;
1145 
1146         if (is_relative(pktl_it->pkt.dts))
1147             pktl_it->pkt.dts += shift;
1148 
1149         if (st->start_time == AV_NOPTS_VALUE && pktl_it->pkt.pts != AV_NOPTS_VALUE) {
1150             st->start_time = pktl_it->pkt.pts;
1151             if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && st->codecpar->sample_rate)
1152                 st->start_time = av_sat_add64(st->start_time, av_rescale_q(st->internal->skip_samples, (AVRational){1, st->codecpar->sample_rate}, st->time_base));
1153         }
1154     }
1155 
1156     if (has_decode_delay_been_guessed(st)) {
1157         update_dts_from_pts(s, stream_index, pktl);
1158     }
1159 
1160     if (st->start_time == AV_NOPTS_VALUE) {
1161         if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO || !(pkt->flags & AV_PKT_FLAG_DISCARD)) {
1162             st->start_time = pts;
1163         }
1164         if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && st->codecpar->sample_rate)
1165             st->start_time = av_sat_add64(st->start_time, av_rescale_q(st->internal->skip_samples, (AVRational){1, st->codecpar->sample_rate}, st->time_base));
1166     }
1167 }
1168 
update_initial_durations(AVFormatContext * s,AVStream * st,int stream_index,int64_t duration)1169 static void update_initial_durations(AVFormatContext *s, AVStream *st,
1170                                      int stream_index, int64_t duration)
1171 {
1172     PacketList *pktl = s->internal->packet_buffer ? s->internal->packet_buffer : s->internal->parse_queue;
1173     int64_t cur_dts    = RELATIVE_TS_BASE;
1174 
1175     if (st->first_dts != AV_NOPTS_VALUE) {
1176         if (st->internal->update_initial_durations_done)
1177             return;
1178         st->internal->update_initial_durations_done = 1;
1179         cur_dts = st->first_dts;
1180         for (; pktl; pktl = get_next_pkt(s, st, pktl)) {
1181             if (pktl->pkt.stream_index == stream_index) {
1182                 if (pktl->pkt.pts != pktl->pkt.dts  ||
1183                     pktl->pkt.dts != AV_NOPTS_VALUE ||
1184                     pktl->pkt.duration)
1185                     break;
1186                 cur_dts -= duration;
1187             }
1188         }
1189         if (pktl && pktl->pkt.dts != st->first_dts) {
1190             av_log(s, AV_LOG_DEBUG, "first_dts %s not matching first dts %s (pts %s, duration %"PRId64") in the queue\n",
1191                    av_ts2str(st->first_dts), av_ts2str(pktl->pkt.dts), av_ts2str(pktl->pkt.pts), pktl->pkt.duration);
1192             return;
1193         }
1194         if (!pktl) {
1195             av_log(s, AV_LOG_DEBUG, "first_dts %s but no packet with dts in the queue\n", av_ts2str(st->first_dts));
1196             return;
1197         }
1198         pktl          = s->internal->packet_buffer ? s->internal->packet_buffer : s->internal->parse_queue;
1199         st->first_dts = cur_dts;
1200     } else if (st->cur_dts != RELATIVE_TS_BASE)
1201         return;
1202 
1203     for (; pktl; pktl = get_next_pkt(s, st, pktl)) {
1204         if (pktl->pkt.stream_index != stream_index)
1205             continue;
1206         if ((pktl->pkt.pts == pktl->pkt.dts ||
1207              pktl->pkt.pts == AV_NOPTS_VALUE) &&
1208             (pktl->pkt.dts == AV_NOPTS_VALUE ||
1209              pktl->pkt.dts == st->first_dts ||
1210              pktl->pkt.dts == RELATIVE_TS_BASE) &&
1211             !pktl->pkt.duration &&
1212             av_sat_add64(cur_dts, duration) == cur_dts + (uint64_t)duration
1213         ) {
1214             pktl->pkt.dts = cur_dts;
1215             if (!st->internal->avctx->has_b_frames)
1216                 pktl->pkt.pts = cur_dts;
1217             pktl->pkt.duration = duration;
1218         } else
1219             break;
1220         cur_dts = pktl->pkt.dts + pktl->pkt.duration;
1221     }
1222     if (!pktl)
1223         st->cur_dts = cur_dts;
1224 }
1225 
compute_pkt_fields(AVFormatContext * s,AVStream * st,AVCodecParserContext * pc,AVPacket * pkt,int64_t next_dts,int64_t next_pts)1226 static void compute_pkt_fields(AVFormatContext *s, AVStream *st,
1227                                AVCodecParserContext *pc, AVPacket *pkt,
1228                                int64_t next_dts, int64_t next_pts)
1229 {
1230     int num, den, presentation_delayed, delay, i;
1231     int64_t offset;
1232     AVRational duration;
1233     int onein_oneout = st->codecpar->codec_id != AV_CODEC_ID_H264 &&
1234                        st->codecpar->codec_id != AV_CODEC_ID_HEVC;
1235 
1236     if (s->flags & AVFMT_FLAG_NOFILLIN)
1237         return;
1238 
1239     if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && pkt->dts != AV_NOPTS_VALUE) {
1240         if (pkt->dts == pkt->pts && st->internal->last_dts_for_order_check != AV_NOPTS_VALUE) {
1241             if (st->internal->last_dts_for_order_check <= pkt->dts) {
1242                 st->internal->dts_ordered++;
1243             } else {
1244                 av_log(s, st->internal->dts_misordered ? AV_LOG_DEBUG : AV_LOG_WARNING,
1245                        "DTS %"PRIi64" < %"PRIi64" out of order\n",
1246                        pkt->dts,
1247                        st->internal->last_dts_for_order_check);
1248                 st->internal->dts_misordered++;
1249             }
1250             if (st->internal->dts_ordered + st->internal->dts_misordered > 250) {
1251                 st->internal->dts_ordered    >>= 1;
1252                 st->internal->dts_misordered >>= 1;
1253             }
1254         }
1255 
1256         st->internal->last_dts_for_order_check = pkt->dts;
1257         if (st->internal->dts_ordered < 8*st->internal->dts_misordered && pkt->dts == pkt->pts)
1258             pkt->dts = AV_NOPTS_VALUE;
1259     }
1260 
1261     if ((s->flags & AVFMT_FLAG_IGNDTS) && pkt->pts != AV_NOPTS_VALUE)
1262         pkt->dts = AV_NOPTS_VALUE;
1263 
1264     if (pc && pc->pict_type == AV_PICTURE_TYPE_B
1265         && !st->internal->avctx->has_b_frames)
1266         //FIXME Set low_delay = 0 when has_b_frames = 1
1267         st->internal->avctx->has_b_frames = 1;
1268 
1269     /* do we have a video B-frame ? */
1270     delay = st->internal->avctx->has_b_frames;
1271     presentation_delayed = 0;
1272 
1273     /* XXX: need has_b_frame, but cannot get it if the codec is
1274      *  not initialized */
1275     if (delay &&
1276         pc && pc->pict_type != AV_PICTURE_TYPE_B)
1277         presentation_delayed = 1;
1278 
1279     if (pkt->pts != AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE &&
1280         st->pts_wrap_bits < 63 && pkt->dts > INT64_MIN + (1LL << st->pts_wrap_bits) &&
1281         pkt->dts - (1LL << (st->pts_wrap_bits - 1)) > pkt->pts) {
1282         if (is_relative(st->cur_dts) || pkt->dts - (1LL<<(st->pts_wrap_bits - 1)) > st->cur_dts) {
1283             pkt->dts -= 1LL << st->pts_wrap_bits;
1284         } else
1285             pkt->pts += 1LL << st->pts_wrap_bits;
1286     }
1287 
1288     /* Some MPEG-2 in MPEG-PS lack dts (issue #171 / input_file.mpg).
1289      * We take the conservative approach and discard both.
1290      * Note: If this is misbehaving for an H.264 file, then possibly
1291      * presentation_delayed is not set correctly. */
1292     if (delay == 1 && pkt->dts == pkt->pts &&
1293         pkt->dts != AV_NOPTS_VALUE && presentation_delayed) {
1294         av_log(s, AV_LOG_DEBUG, "invalid dts/pts combination %"PRIi64"\n", pkt->dts);
1295         if (    strcmp(s->iformat->name, "mov,mp4,m4a,3gp,3g2,mj2")
1296              && strcmp(s->iformat->name, "flv")) // otherwise we discard correct timestamps for vc1-wmapro.ism
1297             pkt->dts = AV_NOPTS_VALUE;
1298     }
1299 
1300     duration = av_mul_q((AVRational) {pkt->duration, 1}, st->time_base);
1301     if (pkt->duration <= 0) {
1302         ff_compute_frame_duration(s, &num, &den, st, pc, pkt);
1303         if (den && num) {
1304             duration = (AVRational) {num, den};
1305             pkt->duration = av_rescale_rnd(1,
1306                                            num * (int64_t) st->time_base.den,
1307                                            den * (int64_t) st->time_base.num,
1308                                            AV_ROUND_DOWN);
1309         }
1310     }
1311 
1312     if (pkt->duration > 0 && (s->internal->packet_buffer || s->internal->parse_queue))
1313         update_initial_durations(s, st, pkt->stream_index, pkt->duration);
1314 
1315     /* Correct timestamps with byte offset if demuxers only have timestamps
1316      * on packet boundaries */
1317     if (pc && st->need_parsing == AVSTREAM_PARSE_TIMESTAMPS && pkt->size) {
1318         /* this will estimate bitrate based on this frame's duration and size */
1319         offset = av_rescale(pc->offset, pkt->duration, pkt->size);
1320         if (pkt->pts != AV_NOPTS_VALUE)
1321             pkt->pts += offset;
1322         if (pkt->dts != AV_NOPTS_VALUE)
1323             pkt->dts += offset;
1324     }
1325 
1326     /* This may be redundant, but it should not hurt. */
1327     if (pkt->dts != AV_NOPTS_VALUE &&
1328         pkt->pts != AV_NOPTS_VALUE &&
1329         pkt->pts > pkt->dts)
1330         presentation_delayed = 1;
1331 
1332     if (s->debug & FF_FDEBUG_TS)
1333         av_log(s, AV_LOG_DEBUG,
1334             "IN delayed:%d pts:%s, dts:%s cur_dts:%s st:%d pc:%p duration:%"PRId64" delay:%d onein_oneout:%d\n",
1335             presentation_delayed, av_ts2str(pkt->pts), av_ts2str(pkt->dts), av_ts2str(st->cur_dts),
1336             pkt->stream_index, pc, pkt->duration, delay, onein_oneout);
1337 
1338     /* Interpolate PTS and DTS if they are not present. We skip H264
1339      * currently because delay and has_b_frames are not reliably set. */
1340     if ((delay == 0 || (delay == 1 && pc)) &&
1341         onein_oneout) {
1342         if (presentation_delayed) {
1343             /* DTS = decompression timestamp */
1344             /* PTS = presentation timestamp */
1345             if (pkt->dts == AV_NOPTS_VALUE)
1346                 pkt->dts = st->last_IP_pts;
1347             update_initial_timestamps(s, pkt->stream_index, pkt->dts, pkt->pts, pkt);
1348             if (pkt->dts == AV_NOPTS_VALUE)
1349                 pkt->dts = st->cur_dts;
1350 
1351             /* This is tricky: the dts must be incremented by the duration
1352              * of the frame we are displaying, i.e. the last I- or P-frame. */
1353             if (st->last_IP_duration == 0 && (uint64_t)pkt->duration <= INT32_MAX)
1354                 st->last_IP_duration = pkt->duration;
1355             if (pkt->dts != AV_NOPTS_VALUE)
1356                 st->cur_dts = av_sat_add64(pkt->dts, st->last_IP_duration);
1357             if (pkt->dts != AV_NOPTS_VALUE &&
1358                 pkt->pts == AV_NOPTS_VALUE &&
1359                 st->last_IP_duration > 0 &&
1360                 ((uint64_t)st->cur_dts - (uint64_t)next_dts + 1) <= 2 &&
1361                 next_dts != next_pts &&
1362                 next_pts != AV_NOPTS_VALUE)
1363                 pkt->pts = next_dts;
1364 
1365             if ((uint64_t)pkt->duration <= INT32_MAX)
1366                 st->last_IP_duration = pkt->duration;
1367             st->last_IP_pts      = pkt->pts;
1368             /* Cannot compute PTS if not present (we can compute it only
1369              * by knowing the future. */
1370         } else if (pkt->pts != AV_NOPTS_VALUE ||
1371                    pkt->dts != AV_NOPTS_VALUE ||
1372                    pkt->duration > 0             ) {
1373 
1374             /* presentation is not delayed : PTS and DTS are the same */
1375             if (pkt->pts == AV_NOPTS_VALUE)
1376                 pkt->pts = pkt->dts;
1377             update_initial_timestamps(s, pkt->stream_index, pkt->pts,
1378                                       pkt->pts, pkt);
1379             if (pkt->pts == AV_NOPTS_VALUE)
1380                 pkt->pts = st->cur_dts;
1381             pkt->dts = pkt->pts;
1382             if (pkt->pts != AV_NOPTS_VALUE && duration.num >= 0)
1383                 st->cur_dts = av_add_stable(st->time_base, pkt->pts, duration, 1);
1384         }
1385     }
1386 
1387     if (pkt->pts != AV_NOPTS_VALUE && delay <= MAX_REORDER_DELAY) {
1388         st->internal->pts_buffer[0] = pkt->pts;
1389         for (i = 0; i<delay && st->internal->pts_buffer[i] > st->internal->pts_buffer[i + 1]; i++)
1390             FFSWAP(int64_t, st->internal->pts_buffer[i], st->internal->pts_buffer[i + 1]);
1391 
1392         if(has_decode_delay_been_guessed(st))
1393             pkt->dts = select_from_pts_buffer(st, st->internal->pts_buffer, pkt->dts);
1394     }
1395     // We skipped it above so we try here.
1396     if (!onein_oneout)
1397         // This should happen on the first packet
1398         update_initial_timestamps(s, pkt->stream_index, pkt->dts, pkt->pts, pkt);
1399     if (pkt->dts > st->cur_dts)
1400         st->cur_dts = pkt->dts;
1401 
1402     if (s->debug & FF_FDEBUG_TS)
1403         av_log(s, AV_LOG_DEBUG, "OUTdelayed:%d/%d pts:%s, dts:%s cur_dts:%s st:%d (%d)\n",
1404             presentation_delayed, delay, av_ts2str(pkt->pts), av_ts2str(pkt->dts), av_ts2str(st->cur_dts), st->index, st->id);
1405 
1406     /* update flags */
1407     if (st->codecpar->codec_type == AVMEDIA_TYPE_DATA || ff_is_intra_only(st->codecpar->codec_id))
1408         pkt->flags |= AV_PKT_FLAG_KEY;
1409 #if FF_API_CONVERGENCE_DURATION
1410 FF_DISABLE_DEPRECATION_WARNINGS
1411     if (pc)
1412         pkt->convergence_duration = pc->convergence_duration;
1413 FF_ENABLE_DEPRECATION_WARNINGS
1414 #endif
1415 }
1416 
1417 /**
1418  * Parse a packet, add all split parts to parse_queue.
1419  *
1420  * @param pkt   Packet to parse; must not be NULL.
1421  * @param flush Indicates whether to flush. If set, pkt must be blank.
1422  */
parse_packet(AVFormatContext * s,AVPacket * pkt,int stream_index,int flush)1423 static int parse_packet(AVFormatContext *s, AVPacket *pkt,
1424                         int stream_index, int flush)
1425 {
1426     AVPacket *out_pkt = s->internal->parse_pkt;
1427     AVStream *st = s->streams[stream_index];
1428     uint8_t *data = pkt->data;
1429     int size      = pkt->size;
1430     int ret = 0, got_output = flush;
1431 
1432     if (size || flush) {
1433         av_packet_unref(out_pkt);
1434     } else if (st->parser->flags & PARSER_FLAG_COMPLETE_FRAMES) {
1435         // preserve 0-size sync packets
1436         compute_pkt_fields(s, st, st->parser, pkt, AV_NOPTS_VALUE, AV_NOPTS_VALUE);
1437     }
1438 
1439     while (size > 0 || (flush && got_output)) {
1440         int len;
1441         int64_t next_pts = pkt->pts;
1442         int64_t next_dts = pkt->dts;
1443 
1444         len = av_parser_parse2(st->parser, st->internal->avctx,
1445                                &out_pkt->data, &out_pkt->size, data, size,
1446                                pkt->pts, pkt->dts, pkt->pos);
1447 
1448         pkt->pts = pkt->dts = AV_NOPTS_VALUE;
1449         pkt->pos = -1;
1450         /* increment read pointer */
1451         av_assert1(data || !len);
1452         data  = len ? data + len : data;
1453         size -= len;
1454 
1455         got_output = !!out_pkt->size;
1456 
1457         if (!out_pkt->size)
1458             continue;
1459 
1460         if (pkt->buf && out_pkt->data == pkt->data) {
1461             /* reference pkt->buf only when out_pkt->data is guaranteed to point
1462              * to data in it and not in the parser's internal buffer. */
1463             /* XXX: Ensure this is the case with all parsers when st->parser->flags
1464              * is PARSER_FLAG_COMPLETE_FRAMES and check for that instead? */
1465             out_pkt->buf = av_buffer_ref(pkt->buf);
1466             if (!out_pkt->buf) {
1467                 ret = AVERROR(ENOMEM);
1468                 goto fail;
1469             }
1470         } else {
1471             ret = av_packet_make_refcounted(out_pkt);
1472             if (ret < 0)
1473                 goto fail;
1474         }
1475 
1476         if (pkt->side_data) {
1477             out_pkt->side_data       = pkt->side_data;
1478             out_pkt->side_data_elems = pkt->side_data_elems;
1479             pkt->side_data          = NULL;
1480             pkt->side_data_elems    = 0;
1481         }
1482 
1483         /* set the duration */
1484         out_pkt->duration = (st->parser->flags & PARSER_FLAG_COMPLETE_FRAMES) ? pkt->duration : 0;
1485         if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
1486             if (st->internal->avctx->sample_rate > 0) {
1487                 out_pkt->duration =
1488                     av_rescale_q_rnd(st->parser->duration,
1489                                      (AVRational) { 1, st->internal->avctx->sample_rate },
1490                                      st->time_base,
1491                                      AV_ROUND_DOWN);
1492             }
1493         }
1494 
1495         out_pkt->stream_index = st->index;
1496         out_pkt->pts          = st->parser->pts;
1497         out_pkt->dts          = st->parser->dts;
1498         out_pkt->pos          = st->parser->pos;
1499         out_pkt->flags       |= pkt->flags & AV_PKT_FLAG_DISCARD;
1500 
1501         if (st->need_parsing == AVSTREAM_PARSE_FULL_RAW)
1502             out_pkt->pos = st->parser->frame_offset;
1503 
1504         if (st->parser->key_frame == 1 ||
1505             (st->parser->key_frame == -1 &&
1506              st->parser->pict_type == AV_PICTURE_TYPE_I))
1507             out_pkt->flags |= AV_PKT_FLAG_KEY;
1508 
1509         if (st->parser->key_frame == -1 && st->parser->pict_type ==AV_PICTURE_TYPE_NONE && (pkt->flags&AV_PKT_FLAG_KEY))
1510             out_pkt->flags |= AV_PKT_FLAG_KEY;
1511 
1512         compute_pkt_fields(s, st, st->parser, out_pkt, next_dts, next_pts);
1513 
1514         ret = avpriv_packet_list_put(&s->internal->parse_queue,
1515                                  &s->internal->parse_queue_end,
1516                                  out_pkt, NULL, 0);
1517         if (ret < 0) {
1518             av_packet_unref(out_pkt);
1519             goto fail;
1520         }
1521     }
1522 
1523     /* end of the stream => close and free the parser */
1524     if (flush) {
1525         av_parser_close(st->parser);
1526         st->parser = NULL;
1527     }
1528 
1529 fail:
1530     av_packet_unref(pkt);
1531     return ret;
1532 }
1533 
ts_to_samples(AVStream * st,int64_t ts)1534 static int64_t ts_to_samples(AVStream *st, int64_t ts)
1535 {
1536     return av_rescale(ts, st->time_base.num * st->codecpar->sample_rate, st->time_base.den);
1537 }
1538 
read_frame_internal(AVFormatContext * s,AVPacket * pkt)1539 static int read_frame_internal(AVFormatContext *s, AVPacket *pkt)
1540 {
1541     int ret, i, got_packet = 0;
1542     AVDictionary *metadata = NULL;
1543 
1544     while (!got_packet && !s->internal->parse_queue) {
1545         AVStream *st;
1546 
1547         /* read next packet */
1548         ret = ff_read_packet(s, pkt);
1549         if (ret < 0) {
1550             if (ret == AVERROR(EAGAIN))
1551                 return ret;
1552             /* flush the parsers */
1553             for (i = 0; i < s->nb_streams; i++) {
1554                 st = s->streams[i];
1555                 if (st->parser && st->need_parsing)
1556                     parse_packet(s, pkt, st->index, 1);
1557             }
1558             /* all remaining packets are now in parse_queue =>
1559              * really terminate parsing */
1560             break;
1561         }
1562         ret = 0;
1563         st  = s->streams[pkt->stream_index];
1564 
1565         st->event_flags |= AVSTREAM_EVENT_FLAG_NEW_PACKETS;
1566 
1567         /* update context if required */
1568         if (st->internal->need_context_update) {
1569             if (avcodec_is_open(st->internal->avctx)) {
1570                 av_log(s, AV_LOG_DEBUG, "Demuxer context update while decoder is open, closing and trying to re-open\n");
1571                 avcodec_close(st->internal->avctx);
1572                 st->internal->info->found_decoder = 0;
1573             }
1574 
1575             /* close parser, because it depends on the codec */
1576             if (st->parser && st->internal->avctx->codec_id != st->codecpar->codec_id) {
1577                 av_parser_close(st->parser);
1578                 st->parser = NULL;
1579             }
1580 
1581             ret = avcodec_parameters_to_context(st->internal->avctx, st->codecpar);
1582             if (ret < 0) {
1583                 av_packet_unref(pkt);
1584                 return ret;
1585             }
1586 
1587 #if FF_API_LAVF_AVCTX
1588 FF_DISABLE_DEPRECATION_WARNINGS
1589             /* update deprecated public codec context */
1590             ret = avcodec_parameters_to_context(st->codec, st->codecpar);
1591             if (ret < 0) {
1592                 av_packet_unref(pkt);
1593                 return ret;
1594             }
1595 FF_ENABLE_DEPRECATION_WARNINGS
1596 #endif
1597 
1598             st->internal->need_context_update = 0;
1599         }
1600 
1601         if (pkt->pts != AV_NOPTS_VALUE &&
1602             pkt->dts != AV_NOPTS_VALUE &&
1603             pkt->pts < pkt->dts) {
1604             av_log(s, AV_LOG_WARNING,
1605                    "Invalid timestamps stream=%d, pts=%s, dts=%s, size=%d\n",
1606                    pkt->stream_index,
1607                    av_ts2str(pkt->pts),
1608                    av_ts2str(pkt->dts),
1609                    pkt->size);
1610         }
1611         if (s->debug & FF_FDEBUG_TS)
1612             av_log(s, AV_LOG_DEBUG,
1613                    "ff_read_packet stream=%d, pts=%s, dts=%s, size=%d, duration=%"PRId64", flags=%d\n",
1614                    pkt->stream_index,
1615                    av_ts2str(pkt->pts),
1616                    av_ts2str(pkt->dts),
1617                    pkt->size, pkt->duration, pkt->flags);
1618 
1619         if (st->need_parsing && !st->parser && !(s->flags & AVFMT_FLAG_NOPARSE)) {
1620             st->parser = av_parser_init(st->codecpar->codec_id);
1621             if (!st->parser) {
1622                 av_log(s, AV_LOG_VERBOSE, "parser not found for codec "
1623                        "%s, packets or times may be invalid.\n",
1624                        avcodec_get_name(st->codecpar->codec_id));
1625                 /* no parser available: just output the raw packets */
1626                 st->need_parsing = AVSTREAM_PARSE_NONE;
1627             } else if (st->need_parsing == AVSTREAM_PARSE_HEADERS)
1628                 st->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES;
1629             else if (st->need_parsing == AVSTREAM_PARSE_FULL_ONCE)
1630                 st->parser->flags |= PARSER_FLAG_ONCE;
1631             else if (st->need_parsing == AVSTREAM_PARSE_FULL_RAW)
1632                 st->parser->flags |= PARSER_FLAG_USE_CODEC_TS;
1633         }
1634 
1635         if (!st->need_parsing || !st->parser) {
1636             /* no parsing needed: we just output the packet as is */
1637             compute_pkt_fields(s, st, NULL, pkt, AV_NOPTS_VALUE, AV_NOPTS_VALUE);
1638             if ((s->iformat->flags & AVFMT_GENERIC_INDEX) &&
1639                 (pkt->flags & AV_PKT_FLAG_KEY) && pkt->dts != AV_NOPTS_VALUE) {
1640                 ff_reduce_index(s, st->index);
1641                 av_add_index_entry(st, pkt->pos, pkt->dts,
1642                                    0, 0, AVINDEX_KEYFRAME);
1643             }
1644             got_packet = 1;
1645         } else if (st->discard < AVDISCARD_ALL) {
1646             if ((ret = parse_packet(s, pkt, pkt->stream_index, 0)) < 0)
1647                 return ret;
1648             st->codecpar->sample_rate = st->internal->avctx->sample_rate;
1649             st->codecpar->bit_rate = st->internal->avctx->bit_rate;
1650             st->codecpar->channels = st->internal->avctx->channels;
1651             st->codecpar->channel_layout = st->internal->avctx->channel_layout;
1652             st->codecpar->codec_id = st->internal->avctx->codec_id;
1653         } else {
1654             /* free packet */
1655             av_packet_unref(pkt);
1656         }
1657         if (pkt->flags & AV_PKT_FLAG_KEY)
1658             st->internal->skip_to_keyframe = 0;
1659         if (st->internal->skip_to_keyframe) {
1660             av_packet_unref(pkt);
1661             got_packet = 0;
1662         }
1663     }
1664 
1665     if (!got_packet && s->internal->parse_queue)
1666         ret = avpriv_packet_list_get(&s->internal->parse_queue, &s->internal->parse_queue_end, pkt);
1667 
1668     if (ret >= 0) {
1669         AVStream *st = s->streams[pkt->stream_index];
1670         int discard_padding = 0;
1671         if (st->internal->first_discard_sample && pkt->pts != AV_NOPTS_VALUE) {
1672             int64_t pts = pkt->pts - (is_relative(pkt->pts) ? RELATIVE_TS_BASE : 0);
1673             int64_t sample = ts_to_samples(st, pts);
1674             int duration = ts_to_samples(st, pkt->duration);
1675             int64_t end_sample = sample + duration;
1676             if (duration > 0 && end_sample >= st->internal->first_discard_sample &&
1677                 sample < st->internal->last_discard_sample)
1678                 discard_padding = FFMIN(end_sample - st->internal->first_discard_sample, duration);
1679         }
1680         if (st->internal->start_skip_samples && (pkt->pts == 0 || pkt->pts == RELATIVE_TS_BASE))
1681             st->internal->skip_samples = st->internal->start_skip_samples;
1682         if (st->internal->skip_samples || discard_padding) {
1683             uint8_t *p = av_packet_new_side_data(pkt, AV_PKT_DATA_SKIP_SAMPLES, 10);
1684             if (p) {
1685                 AV_WL32(p, st->internal->skip_samples);
1686                 AV_WL32(p + 4, discard_padding);
1687                 av_log(s, AV_LOG_DEBUG, "demuxer injecting skip %d / discard %d\n", st->internal->skip_samples, discard_padding);
1688             }
1689             st->internal->skip_samples = 0;
1690         }
1691 
1692         if (st->internal->inject_global_side_data) {
1693             for (i = 0; i < st->nb_side_data; i++) {
1694                 AVPacketSideData *src_sd = &st->side_data[i];
1695                 uint8_t *dst_data;
1696 
1697                 if (av_packet_get_side_data(pkt, src_sd->type, NULL))
1698                     continue;
1699 
1700                 dst_data = av_packet_new_side_data(pkt, src_sd->type, src_sd->size);
1701                 if (!dst_data) {
1702                     av_log(s, AV_LOG_WARNING, "Could not inject global side data\n");
1703                     continue;
1704                 }
1705 
1706                 memcpy(dst_data, src_sd->data, src_sd->size);
1707             }
1708             st->internal->inject_global_side_data = 0;
1709         }
1710     }
1711 
1712     av_opt_get_dict_val(s, "metadata", AV_OPT_SEARCH_CHILDREN, &metadata);
1713     if (metadata) {
1714         s->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
1715         av_dict_copy(&s->metadata, metadata, 0);
1716         av_dict_free(&metadata);
1717         av_opt_set_dict_val(s, "metadata", NULL, AV_OPT_SEARCH_CHILDREN);
1718     }
1719 
1720 #if FF_API_LAVF_AVCTX
1721     update_stream_avctx(s);
1722 #endif
1723 
1724     if (s->debug & FF_FDEBUG_TS)
1725         av_log(s, AV_LOG_DEBUG,
1726                "read_frame_internal stream=%d, pts=%s, dts=%s, "
1727                "size=%d, duration=%"PRId64", flags=%d\n",
1728                pkt->stream_index,
1729                av_ts2str(pkt->pts),
1730                av_ts2str(pkt->dts),
1731                pkt->size, pkt->duration, pkt->flags);
1732 
1733     /* A demuxer might have returned EOF because of an IO error, let's
1734      * propagate this back to the user. */
1735     if (ret == AVERROR_EOF && s->pb && s->pb->error < 0 && s->pb->error != AVERROR(EAGAIN))
1736         ret = s->pb->error;
1737 
1738     return ret;
1739 }
1740 
av_read_frame(AVFormatContext * s,AVPacket * pkt)1741 int av_read_frame(AVFormatContext *s, AVPacket *pkt)
1742 {
1743     const int genpts = s->flags & AVFMT_FLAG_GENPTS;
1744     int eof = 0;
1745     int ret;
1746     AVStream *st;
1747 
1748     if (!genpts) {
1749         ret = s->internal->packet_buffer
1750               ? avpriv_packet_list_get(&s->internal->packet_buffer,
1751                                         &s->internal->packet_buffer_end, pkt)
1752               : read_frame_internal(s, pkt);
1753         if (ret < 0)
1754             return ret;
1755         goto return_packet;
1756     }
1757 
1758     for (;;) {
1759         PacketList *pktl = s->internal->packet_buffer;
1760 
1761         if (pktl) {
1762             AVPacket *next_pkt = &pktl->pkt;
1763 
1764             if (next_pkt->dts != AV_NOPTS_VALUE) {
1765                 int wrap_bits = s->streams[next_pkt->stream_index]->pts_wrap_bits;
1766                 // last dts seen for this stream. if any of packets following
1767                 // current one had no dts, we will set this to AV_NOPTS_VALUE.
1768                 int64_t last_dts = next_pkt->dts;
1769                 av_assert2(wrap_bits <= 64);
1770                 while (pktl && next_pkt->pts == AV_NOPTS_VALUE) {
1771                     if (pktl->pkt.stream_index == next_pkt->stream_index &&
1772                         av_compare_mod(next_pkt->dts, pktl->pkt.dts, 2ULL << (wrap_bits - 1)) < 0) {
1773                         if (av_compare_mod(pktl->pkt.pts, pktl->pkt.dts, 2ULL << (wrap_bits - 1))) {
1774                             // not B-frame
1775                             next_pkt->pts = pktl->pkt.dts;
1776                         }
1777                         if (last_dts != AV_NOPTS_VALUE) {
1778                             // Once last dts was set to AV_NOPTS_VALUE, we don't change it.
1779                             last_dts = pktl->pkt.dts;
1780                         }
1781                     }
1782                     pktl = pktl->next;
1783                 }
1784                 if (eof && next_pkt->pts == AV_NOPTS_VALUE && last_dts != AV_NOPTS_VALUE) {
1785                     // Fixing the last reference frame had none pts issue (For MXF etc).
1786                     // We only do this when
1787                     // 1. eof.
1788                     // 2. we are not able to resolve a pts value for current packet.
1789                     // 3. the packets for this stream at the end of the files had valid dts.
1790                     next_pkt->pts = last_dts + next_pkt->duration;
1791                 }
1792                 pktl = s->internal->packet_buffer;
1793             }
1794 
1795             /* read packet from packet buffer, if there is data */
1796             st = s->streams[next_pkt->stream_index];
1797             if (!(next_pkt->pts == AV_NOPTS_VALUE && st->discard < AVDISCARD_ALL &&
1798                   next_pkt->dts != AV_NOPTS_VALUE && !eof)) {
1799                 ret = avpriv_packet_list_get(&s->internal->packet_buffer,
1800                                                &s->internal->packet_buffer_end, pkt);
1801                 goto return_packet;
1802             }
1803         }
1804 
1805         ret = read_frame_internal(s, pkt);
1806         if (ret < 0) {
1807             if (pktl && ret != AVERROR(EAGAIN)) {
1808                 eof = 1;
1809                 continue;
1810             } else
1811                 return ret;
1812         }
1813 
1814         ret = avpriv_packet_list_put(&s->internal->packet_buffer,
1815                                  &s->internal->packet_buffer_end,
1816                                  pkt, NULL, 0);
1817         if (ret < 0) {
1818             av_packet_unref(pkt);
1819             return ret;
1820         }
1821     }
1822 
1823 return_packet:
1824 
1825     st = s->streams[pkt->stream_index];
1826     if ((s->iformat->flags & AVFMT_GENERIC_INDEX) && pkt->flags & AV_PKT_FLAG_KEY) {
1827         ff_reduce_index(s, st->index);
1828         av_add_index_entry(st, pkt->pos, pkt->dts, 0, 0, AVINDEX_KEYFRAME);
1829     }
1830 
1831     if (is_relative(pkt->dts))
1832         pkt->dts -= RELATIVE_TS_BASE;
1833     if (is_relative(pkt->pts))
1834         pkt->pts -= RELATIVE_TS_BASE;
1835 
1836     return ret;
1837 }
1838 
1839 /* XXX: suppress the packet queue */
flush_packet_queue(AVFormatContext * s)1840 static void flush_packet_queue(AVFormatContext *s)
1841 {
1842     if (!s->internal)
1843         return;
1844     avpriv_packet_list_free(&s->internal->parse_queue,       &s->internal->parse_queue_end);
1845     avpriv_packet_list_free(&s->internal->packet_buffer,     &s->internal->packet_buffer_end);
1846     avpriv_packet_list_free(&s->internal->raw_packet_buffer, &s->internal->raw_packet_buffer_end);
1847 
1848     s->internal->raw_packet_buffer_remaining_size = RAW_PACKET_BUFFER_SIZE;
1849 }
1850 
1851 /*******************************************************/
1852 /* seek support */
1853 
av_find_default_stream_index(AVFormatContext * s)1854 int av_find_default_stream_index(AVFormatContext *s)
1855 {
1856     int i;
1857     AVStream *st;
1858     int best_stream = 0;
1859     int best_score = INT_MIN;
1860 
1861     if (s->nb_streams <= 0)
1862         return -1;
1863     for (i = 0; i < s->nb_streams; i++) {
1864         int score = 0;
1865         st = s->streams[i];
1866         if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
1867             if (st->disposition & AV_DISPOSITION_ATTACHED_PIC)
1868                 score -= 400;
1869             if (st->codecpar->width && st->codecpar->height)
1870                 score += 50;
1871             score+= 25;
1872         }
1873         if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
1874             if (st->codecpar->sample_rate)
1875                 score += 50;
1876         }
1877         if (st->codec_info_nb_frames)
1878             score += 12;
1879 
1880         if (st->discard != AVDISCARD_ALL)
1881             score += 200;
1882 
1883         if (score > best_score) {
1884             best_score = score;
1885             best_stream = i;
1886         }
1887     }
1888     return best_stream;
1889 }
1890 
1891 /** Flush the frame reader. */
ff_read_frame_flush(AVFormatContext * s)1892 void ff_read_frame_flush(AVFormatContext *s)
1893 {
1894     AVStream *st;
1895     int i, j;
1896 
1897     flush_packet_queue(s);
1898 
1899     /* Reset read state for each stream. */
1900     for (i = 0; i < s->nb_streams; i++) {
1901         st = s->streams[i];
1902 
1903         if (st->parser) {
1904             av_parser_close(st->parser);
1905             st->parser = NULL;
1906         }
1907         st->last_IP_pts = AV_NOPTS_VALUE;
1908         st->internal->last_dts_for_order_check = AV_NOPTS_VALUE;
1909         if (st->first_dts == AV_NOPTS_VALUE)
1910             st->cur_dts = RELATIVE_TS_BASE;
1911         else
1912             /* We set the current DTS to an unspecified origin. */
1913             st->cur_dts = AV_NOPTS_VALUE;
1914 
1915         st->probe_packets = s->max_probe_packets;
1916 
1917         for (j = 0; j < MAX_REORDER_DELAY + 1; j++)
1918             st->internal->pts_buffer[j] = AV_NOPTS_VALUE;
1919 
1920         if (s->internal->inject_global_side_data)
1921             st->internal->inject_global_side_data = 1;
1922 
1923         st->internal->skip_samples = 0;
1924     }
1925 }
1926 
ff_update_cur_dts(AVFormatContext * s,AVStream * ref_st,int64_t timestamp)1927 void ff_update_cur_dts(AVFormatContext *s, AVStream *ref_st, int64_t timestamp)
1928 {
1929     int i;
1930 
1931     for (i = 0; i < s->nb_streams; i++) {
1932         AVStream *st = s->streams[i];
1933 
1934         st->cur_dts =
1935             av_rescale(timestamp,
1936                        st->time_base.den * (int64_t) ref_st->time_base.num,
1937                        st->time_base.num * (int64_t) ref_st->time_base.den);
1938     }
1939 }
1940 
ff_reduce_index(AVFormatContext * s,int stream_index)1941 void ff_reduce_index(AVFormatContext *s, int stream_index)
1942 {
1943     AVStream *st             = s->streams[stream_index];
1944     unsigned int max_entries = s->max_index_size / sizeof(AVIndexEntry);
1945 
1946     if ((unsigned) st->nb_index_entries >= max_entries) {
1947         int i;
1948         for (i = 0; 2 * i < st->nb_index_entries; i++)
1949             st->index_entries[i] = st->index_entries[2 * i];
1950         st->nb_index_entries = i;
1951     }
1952 }
1953 
ff_add_index_entry(AVIndexEntry ** index_entries,int * nb_index_entries,unsigned int * index_entries_allocated_size,int64_t pos,int64_t timestamp,int size,int distance,int flags)1954 int ff_add_index_entry(AVIndexEntry **index_entries,
1955                        int *nb_index_entries,
1956                        unsigned int *index_entries_allocated_size,
1957                        int64_t pos, int64_t timestamp,
1958                        int size, int distance, int flags)
1959 {
1960     AVIndexEntry *entries, *ie;
1961     int index;
1962 
1963     if ((unsigned) *nb_index_entries + 1 >= UINT_MAX / sizeof(AVIndexEntry))
1964         return -1;
1965 
1966     if (timestamp == AV_NOPTS_VALUE)
1967         return AVERROR(EINVAL);
1968 
1969     if (size < 0 || size > 0x3FFFFFFF)
1970         return AVERROR(EINVAL);
1971 
1972     if (is_relative(timestamp)) //FIXME this maintains previous behavior but we should shift by the correct offset once known
1973         timestamp -= RELATIVE_TS_BASE;
1974 
1975     entries = av_fast_realloc(*index_entries,
1976                               index_entries_allocated_size,
1977                               (*nb_index_entries + 1) *
1978                               sizeof(AVIndexEntry));
1979     if (!entries)
1980         return -1;
1981 
1982     *index_entries = entries;
1983 
1984     index = ff_index_search_timestamp(*index_entries, *nb_index_entries,
1985                                       timestamp, AVSEEK_FLAG_ANY);
1986 
1987     if (index < 0) {
1988         index = (*nb_index_entries)++;
1989         ie    = &entries[index];
1990         av_assert0(index == 0 || ie[-1].timestamp < timestamp);
1991     } else {
1992         ie = &entries[index];
1993         if (ie->timestamp != timestamp) {
1994             if (ie->timestamp <= timestamp)
1995                 return -1;
1996             memmove(entries + index + 1, entries + index,
1997                     sizeof(AVIndexEntry) * (*nb_index_entries - index));
1998             (*nb_index_entries)++;
1999         } else if (ie->pos == pos && distance < ie->min_distance)
2000             // do not reduce the distance
2001             distance = ie->min_distance;
2002     }
2003 
2004     ie->pos          = pos;
2005     ie->timestamp    = timestamp;
2006     ie->min_distance = distance;
2007     ie->size         = size;
2008     ie->flags        = flags;
2009 
2010     return index;
2011 }
2012 
av_add_index_entry(AVStream * st,int64_t pos,int64_t timestamp,int size,int distance,int flags)2013 int av_add_index_entry(AVStream *st, int64_t pos, int64_t timestamp,
2014                        int size, int distance, int flags)
2015 {
2016     timestamp = wrap_timestamp(st, timestamp);
2017     return ff_add_index_entry(&st->index_entries, &st->nb_index_entries,
2018                               &st->index_entries_allocated_size, pos,
2019                               timestamp, size, distance, flags);
2020 }
2021 
ff_index_search_timestamp(const AVIndexEntry * entries,int nb_entries,int64_t wanted_timestamp,int flags)2022 int ff_index_search_timestamp(const AVIndexEntry *entries, int nb_entries,
2023                               int64_t wanted_timestamp, int flags)
2024 {
2025     int a, b, m;
2026     int64_t timestamp;
2027 
2028     a = -1;
2029     b = nb_entries;
2030 
2031     // Optimize appending index entries at the end.
2032     if (b && entries[b - 1].timestamp < wanted_timestamp)
2033         a = b - 1;
2034 
2035     while (b - a > 1) {
2036         m         = (a + b) >> 1;
2037 
2038         // Search for the next non-discarded packet.
2039         while ((entries[m].flags & AVINDEX_DISCARD_FRAME) && m < b && m < nb_entries - 1) {
2040             m++;
2041             if (m == b && entries[m].timestamp >= wanted_timestamp) {
2042                 m = b - 1;
2043                 break;
2044             }
2045         }
2046 
2047         timestamp = entries[m].timestamp;
2048         if (timestamp >= wanted_timestamp)
2049             b = m;
2050         if (timestamp <= wanted_timestamp)
2051             a = m;
2052     }
2053     m = (flags & AVSEEK_FLAG_BACKWARD) ? a : b;
2054 
2055     if (!(flags & AVSEEK_FLAG_ANY))
2056         while (m >= 0 && m < nb_entries &&
2057                !(entries[m].flags & AVINDEX_KEYFRAME))
2058             m += (flags & AVSEEK_FLAG_BACKWARD) ? -1 : 1;
2059 
2060     if (m == nb_entries)
2061         return -1;
2062     return m;
2063 }
2064 
ff_configure_buffers_for_index(AVFormatContext * s,int64_t time_tolerance)2065 void ff_configure_buffers_for_index(AVFormatContext *s, int64_t time_tolerance)
2066 {
2067     int ist1, ist2;
2068     int64_t pos_delta = 0;
2069     int64_t skip = 0;
2070     //We could use URLProtocol flags here but as many user applications do not use URLProtocols this would be unreliable
2071     const char *proto = avio_find_protocol_name(s->url);
2072 
2073     av_assert0(time_tolerance >= 0);
2074 
2075     if (!proto) {
2076         av_log(s, AV_LOG_INFO,
2077                "Protocol name not provided, cannot determine if input is local or "
2078                "a network protocol, buffers and access patterns cannot be configured "
2079                "optimally without knowing the protocol\n");
2080     }
2081 
2082     if (proto && !(strcmp(proto, "file") && strcmp(proto, "pipe") && strcmp(proto, "cache")))
2083         return;
2084 
2085     for (ist1 = 0; ist1 < s->nb_streams; ist1++) {
2086         AVStream *st1 = s->streams[ist1];
2087         for (ist2 = 0; ist2 < s->nb_streams; ist2++) {
2088             AVStream *st2 = s->streams[ist2];
2089             int i1, i2;
2090 
2091             if (ist1 == ist2)
2092                 continue;
2093 
2094             for (i1 = i2 = 0; i1 < st1->nb_index_entries; i1++) {
2095                 AVIndexEntry *e1 = &st1->index_entries[i1];
2096                 int64_t e1_pts = av_rescale_q(e1->timestamp, st1->time_base, AV_TIME_BASE_Q);
2097 
2098                 skip = FFMAX(skip, e1->size);
2099                 for (; i2 < st2->nb_index_entries; i2++) {
2100                     AVIndexEntry *e2 = &st2->index_entries[i2];
2101                     int64_t e2_pts = av_rescale_q(e2->timestamp, st2->time_base, AV_TIME_BASE_Q);
2102                     if (e2_pts < e1_pts || e2_pts - (uint64_t)e1_pts < time_tolerance)
2103                         continue;
2104                     pos_delta = FFMAX(pos_delta, e1->pos - e2->pos);
2105                     break;
2106                 }
2107             }
2108         }
2109     }
2110 
2111     pos_delta *= 2;
2112     /* XXX This could be adjusted depending on protocol*/
2113     if (s->pb->buffer_size < pos_delta && pos_delta < (1<<24)) {
2114         av_log(s, AV_LOG_VERBOSE, "Reconfiguring buffers to size %"PRId64"\n", pos_delta);
2115 
2116         /* realloc the buffer and the original data will be retained */
2117         if (ffio_realloc_buf(s->pb, pos_delta)) {
2118             av_log(s, AV_LOG_ERROR, "Realloc buffer fail.\n");
2119             return;
2120         }
2121 
2122         s->pb->short_seek_threshold = FFMAX(s->pb->short_seek_threshold, pos_delta/2);
2123     }
2124 
2125     if (skip < (1<<23)) {
2126         s->pb->short_seek_threshold = FFMAX(s->pb->short_seek_threshold, skip);
2127     }
2128 }
2129 
av_index_search_timestamp(AVStream * st,int64_t wanted_timestamp,int flags)2130 int av_index_search_timestamp(AVStream *st, int64_t wanted_timestamp, int flags)
2131 {
2132     return ff_index_search_timestamp(st->index_entries, st->nb_index_entries,
2133                                      wanted_timestamp, flags);
2134 }
2135 
ff_read_timestamp(AVFormatContext * s,int stream_index,int64_t * ppos,int64_t pos_limit,int64_t (* read_timestamp)(struct AVFormatContext *,int,int64_t *,int64_t))2136 static int64_t ff_read_timestamp(AVFormatContext *s, int stream_index, int64_t *ppos, int64_t pos_limit,
2137                                  int64_t (*read_timestamp)(struct AVFormatContext *, int , int64_t *, int64_t ))
2138 {
2139     int64_t ts = read_timestamp(s, stream_index, ppos, pos_limit);
2140     if (stream_index >= 0)
2141         ts = wrap_timestamp(s->streams[stream_index], ts);
2142     return ts;
2143 }
2144 
ff_seek_frame_binary(AVFormatContext * s,int stream_index,int64_t target_ts,int flags)2145 int ff_seek_frame_binary(AVFormatContext *s, int stream_index,
2146                          int64_t target_ts, int flags)
2147 {
2148     const AVInputFormat *avif = s->iformat;
2149     int64_t av_uninit(pos_min), av_uninit(pos_max), pos, pos_limit;
2150     int64_t ts_min, ts_max, ts;
2151     int index;
2152     int64_t ret;
2153     AVStream *st;
2154 
2155     if (stream_index < 0)
2156         return -1;
2157 
2158     av_log(s, AV_LOG_TRACE, "read_seek: %d %s\n", stream_index, av_ts2str(target_ts));
2159 
2160     ts_max =
2161     ts_min = AV_NOPTS_VALUE;
2162     pos_limit = -1; // GCC falsely says it may be uninitialized.
2163 
2164     st = s->streams[stream_index];
2165     if (st->index_entries) {
2166         AVIndexEntry *e;
2167 
2168         /* FIXME: Whole function must be checked for non-keyframe entries in
2169          * index case, especially read_timestamp(). */
2170         index = av_index_search_timestamp(st, target_ts,
2171                                           flags | AVSEEK_FLAG_BACKWARD);
2172         index = FFMAX(index, 0);
2173         e     = &st->index_entries[index];
2174 
2175         if (e->timestamp <= target_ts || e->pos == e->min_distance) {
2176             pos_min = e->pos;
2177             ts_min  = e->timestamp;
2178             av_log(s, AV_LOG_TRACE, "using cached pos_min=0x%"PRIx64" dts_min=%s\n",
2179                     pos_min, av_ts2str(ts_min));
2180         } else {
2181             av_assert1(index == 0);
2182         }
2183 
2184         index = av_index_search_timestamp(st, target_ts,
2185                                           flags & ~AVSEEK_FLAG_BACKWARD);
2186         av_assert0(index < st->nb_index_entries);
2187         if (index >= 0) {
2188             e = &st->index_entries[index];
2189             av_assert1(e->timestamp >= target_ts);
2190             pos_max   = e->pos;
2191             ts_max    = e->timestamp;
2192             pos_limit = pos_max - e->min_distance;
2193             av_log(s, AV_LOG_TRACE, "using cached pos_max=0x%"PRIx64" pos_limit=0x%"PRIx64
2194                     " dts_max=%s\n", pos_max, pos_limit, av_ts2str(ts_max));
2195         }
2196     }
2197 
2198     pos = ff_gen_search(s, stream_index, target_ts, pos_min, pos_max, pos_limit,
2199                         ts_min, ts_max, flags, &ts, avif->read_timestamp);
2200     if (pos < 0)
2201         return -1;
2202 
2203     /* do the seek */
2204     if ((ret = avio_seek(s->pb, pos, SEEK_SET)) < 0)
2205         return ret;
2206 
2207     ff_read_frame_flush(s);
2208     ff_update_cur_dts(s, st, ts);
2209 
2210     return 0;
2211 }
2212 
ff_find_last_ts(AVFormatContext * s,int stream_index,int64_t * ts,int64_t * pos,int64_t (* read_timestamp)(struct AVFormatContext *,int,int64_t *,int64_t))2213 int ff_find_last_ts(AVFormatContext *s, int stream_index, int64_t *ts, int64_t *pos,
2214                     int64_t (*read_timestamp)(struct AVFormatContext *, int , int64_t *, int64_t ))
2215 {
2216     int64_t step = 1024;
2217     int64_t limit, ts_max;
2218     int64_t filesize = avio_size(s->pb);
2219     int64_t pos_max  = filesize - 1;
2220     do {
2221         limit = pos_max;
2222         pos_max = FFMAX(0, (pos_max) - step);
2223         ts_max  = ff_read_timestamp(s, stream_index,
2224                                     &pos_max, limit, read_timestamp);
2225         step   += step;
2226     } while (ts_max == AV_NOPTS_VALUE && 2*limit > step);
2227     if (ts_max == AV_NOPTS_VALUE)
2228         return -1;
2229 
2230     for (;;) {
2231         int64_t tmp_pos = pos_max + 1;
2232         int64_t tmp_ts  = ff_read_timestamp(s, stream_index,
2233                                             &tmp_pos, INT64_MAX, read_timestamp);
2234         if (tmp_ts == AV_NOPTS_VALUE)
2235             break;
2236         av_assert0(tmp_pos > pos_max);
2237         ts_max  = tmp_ts;
2238         pos_max = tmp_pos;
2239         if (tmp_pos >= filesize)
2240             break;
2241     }
2242 
2243     if (ts)
2244         *ts = ts_max;
2245     if (pos)
2246         *pos = pos_max;
2247 
2248     return 0;
2249 }
2250 
ff_gen_search(AVFormatContext * s,int stream_index,int64_t target_ts,int64_t pos_min,int64_t pos_max,int64_t pos_limit,int64_t ts_min,int64_t ts_max,int flags,int64_t * ts_ret,int64_t (* read_timestamp)(struct AVFormatContext *,int,int64_t *,int64_t))2251 int64_t ff_gen_search(AVFormatContext *s, int stream_index, int64_t target_ts,
2252                       int64_t pos_min, int64_t pos_max, int64_t pos_limit,
2253                       int64_t ts_min, int64_t ts_max,
2254                       int flags, int64_t *ts_ret,
2255                       int64_t (*read_timestamp)(struct AVFormatContext *, int,
2256                                                 int64_t *, int64_t))
2257 {
2258     int64_t pos, ts;
2259     int64_t start_pos;
2260     int no_change;
2261     int ret;
2262 
2263     av_log(s, AV_LOG_TRACE, "gen_seek: %d %s\n", stream_index, av_ts2str(target_ts));
2264 
2265     if (ts_min == AV_NOPTS_VALUE) {
2266         pos_min = s->internal->data_offset;
2267         ts_min  = ff_read_timestamp(s, stream_index, &pos_min, INT64_MAX, read_timestamp);
2268         if (ts_min == AV_NOPTS_VALUE)
2269             return -1;
2270     }
2271 
2272     if (ts_min >= target_ts) {
2273         *ts_ret = ts_min;
2274         return pos_min;
2275     }
2276 
2277     if (ts_max == AV_NOPTS_VALUE) {
2278         if ((ret = ff_find_last_ts(s, stream_index, &ts_max, &pos_max, read_timestamp)) < 0)
2279             return ret;
2280         pos_limit = pos_max;
2281     }
2282 
2283     if (ts_max <= target_ts) {
2284         *ts_ret = ts_max;
2285         return pos_max;
2286     }
2287 
2288     av_assert0(ts_min < ts_max);
2289 
2290     no_change = 0;
2291     while (pos_min < pos_limit) {
2292         av_log(s, AV_LOG_TRACE,
2293                 "pos_min=0x%"PRIx64" pos_max=0x%"PRIx64" dts_min=%s dts_max=%s\n",
2294                 pos_min, pos_max, av_ts2str(ts_min), av_ts2str(ts_max));
2295         av_assert0(pos_limit <= pos_max);
2296 
2297         if (no_change == 0) {
2298             int64_t approximate_keyframe_distance = pos_max - pos_limit;
2299             // interpolate position (better than dichotomy)
2300             pos = av_rescale(target_ts - ts_min, pos_max - pos_min,
2301                              ts_max - ts_min) +
2302                   pos_min - approximate_keyframe_distance;
2303         } else if (no_change == 1) {
2304             // bisection if interpolation did not change min / max pos last time
2305             pos = (pos_min + pos_limit) >> 1;
2306         } else {
2307             /* linear search if bisection failed, can only happen if there
2308              * are very few or no keyframes between min/max */
2309             pos = pos_min;
2310         }
2311         if (pos <= pos_min)
2312             pos = pos_min + 1;
2313         else if (pos > pos_limit)
2314             pos = pos_limit;
2315         start_pos = pos;
2316 
2317         // May pass pos_limit instead of -1.
2318         ts = ff_read_timestamp(s, stream_index, &pos, INT64_MAX, read_timestamp);
2319         if (pos == pos_max)
2320             no_change++;
2321         else
2322             no_change = 0;
2323         av_log(s, AV_LOG_TRACE, "%"PRId64" %"PRId64" %"PRId64" / %s %s %s"
2324                 " target:%s limit:%"PRId64" start:%"PRId64" noc:%d\n",
2325                 pos_min, pos, pos_max,
2326                 av_ts2str(ts_min), av_ts2str(ts), av_ts2str(ts_max), av_ts2str(target_ts),
2327                 pos_limit, start_pos, no_change);
2328         if (ts == AV_NOPTS_VALUE) {
2329             av_log(s, AV_LOG_ERROR, "read_timestamp() failed in the middle\n");
2330             return -1;
2331         }
2332         if (target_ts <= ts) {
2333             pos_limit = start_pos - 1;
2334             pos_max   = pos;
2335             ts_max    = ts;
2336         }
2337         if (target_ts >= ts) {
2338             pos_min = pos;
2339             ts_min  = ts;
2340         }
2341     }
2342 
2343     pos     = (flags & AVSEEK_FLAG_BACKWARD) ? pos_min : pos_max;
2344     ts      = (flags & AVSEEK_FLAG_BACKWARD) ? ts_min  : ts_max;
2345 #if 0
2346     pos_min = pos;
2347     ts_min  = ff_read_timestamp(s, stream_index, &pos_min, INT64_MAX, read_timestamp);
2348     pos_min++;
2349     ts_max = ff_read_timestamp(s, stream_index, &pos_min, INT64_MAX, read_timestamp);
2350     av_log(s, AV_LOG_TRACE, "pos=0x%"PRIx64" %s<=%s<=%s\n",
2351             pos, av_ts2str(ts_min), av_ts2str(target_ts), av_ts2str(ts_max));
2352 #endif
2353     *ts_ret = ts;
2354     return pos;
2355 }
2356 
seek_frame_byte(AVFormatContext * s,int stream_index,int64_t pos,int flags)2357 static int seek_frame_byte(AVFormatContext *s, int stream_index,
2358                            int64_t pos, int flags)
2359 {
2360     int64_t pos_min, pos_max;
2361 
2362     pos_min = s->internal->data_offset;
2363     pos_max = avio_size(s->pb) - 1;
2364 
2365     if (pos < pos_min)
2366         pos = pos_min;
2367     else if (pos > pos_max)
2368         pos = pos_max;
2369 
2370     avio_seek(s->pb, pos, SEEK_SET);
2371 
2372     s->io_repositioned = 1;
2373 
2374     return 0;
2375 }
2376 
seek_frame_generic(AVFormatContext * s,int stream_index,int64_t timestamp,int flags)2377 static int seek_frame_generic(AVFormatContext *s, int stream_index,
2378                               int64_t timestamp, int flags)
2379 {
2380     int index;
2381     int64_t ret;
2382     AVStream *st;
2383     AVIndexEntry *ie;
2384 
2385     st = s->streams[stream_index];
2386 
2387     index = av_index_search_timestamp(st, timestamp, flags);
2388 
2389     if (index < 0 && st->nb_index_entries &&
2390         timestamp < st->index_entries[0].timestamp)
2391         return -1;
2392 
2393 #ifdef OHOS_OPT_COMPAT
2394 /**
2395  * ohos.opt.compat.0002
2396  * When the audio file is very large, and st->index_entries has limited capacity.
2397  * At this time, the function ff_reduce_index() will be called more than once and halved st->index_entries.
2398  * The timestamp queried is far from the actual timestamp, and the st->index_entries needs to be refreshed.
2399  */
2400     int need_read_frame = 0;
2401     if (index < 0 || index == st->nb_index_entries - 1) {
2402         if (st->nb_index_entries) {
2403             av_assert0(st->index_entries);
2404             ie = &st->index_entries[st->nb_index_entries - 1];
2405             if ((ret = avio_seek(s->pb, ie->pos, SEEK_SET)) < 0)
2406                 return ret;
2407             ff_update_cur_dts(s, st, ie->timestamp);
2408         } else {
2409             if ((ret = avio_seek(s->pb, s->internal->data_offset, SEEK_SET)) < 0)
2410                 return ret;
2411         }
2412         need_read_frame = 1;
2413     } else {
2414         av_assert0(st->index_entries);
2415         ie = &st->index_entries[index];
2416         if (timestamp - ie->timestamp >= 10240) { // 1024 samples are recorded for each index, 10 samples are 10240
2417             need_read_frame = 1;
2418             if ((ret = avio_seek(s->pb, ie->pos, SEEK_SET)) < 0)
2419                 return ret;
2420             ff_update_cur_dts(s, st, ie->timestamp);
2421         }
2422     }
2423 
2424     if (need_read_frame > 0) {
2425         AVPacket *pkt = s->internal->pkt;
2426         int nonkey = 0;
2427 #else
2428     if (index < 0 || index == st->nb_index_entries - 1) {
2429         AVPacket *pkt = s->internal->pkt;
2430         int nonkey = 0;
2431 
2432         if (st->nb_index_entries) {
2433             av_assert0(st->index_entries);
2434             ie = &st->index_entries[st->nb_index_entries - 1];
2435             if ((ret = avio_seek(s->pb, ie->pos, SEEK_SET)) < 0)
2436                 return ret;
2437             ff_update_cur_dts(s, st, ie->timestamp);
2438         } else {
2439             if ((ret = avio_seek(s->pb, s->internal->data_offset, SEEK_SET)) < 0)
2440                 return ret;
2441         }
2442 #endif
2443         av_packet_unref(pkt);
2444         for (;;) {
2445             int read_status;
2446             do {
2447                 read_status = av_read_frame(s, pkt);
2448             } while (read_status == AVERROR(EAGAIN));
2449             if (read_status < 0)
2450                 break;
2451             if (stream_index == pkt->stream_index && pkt->dts > timestamp) {
2452                 if (pkt->flags & AV_PKT_FLAG_KEY) {
2453                     av_packet_unref(pkt);
2454                     break;
2455                 }
2456                 if (nonkey++ > 1000 && st->codecpar->codec_id != AV_CODEC_ID_CDGRAPHICS) {
2457                     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);
2458                     av_packet_unref(pkt);
2459                     break;
2460                 }
2461             }
2462             av_packet_unref(pkt);
2463         }
2464         index = av_index_search_timestamp(st, timestamp, flags);
2465     }
2466     if (index < 0)
2467         return -1;
2468 
2469     ff_read_frame_flush(s);
2470     if (s->iformat->read_seek)
2471         if (s->iformat->read_seek(s, stream_index, timestamp, flags) >= 0)
2472             return 0;
2473     ie = &st->index_entries[index];
2474     if ((ret = avio_seek(s->pb, ie->pos, SEEK_SET)) < 0)
2475         return ret;
2476     ff_update_cur_dts(s, st, ie->timestamp);
2477 
2478     return 0;
2479 }
2480 
2481 static int seek_frame_internal(AVFormatContext *s, int stream_index,
2482                                int64_t timestamp, int flags)
2483 {
2484     int ret;
2485     AVStream *st;
2486 
2487     if (flags & AVSEEK_FLAG_BYTE) {
2488         if (s->iformat->flags & AVFMT_NO_BYTE_SEEK)
2489             return -1;
2490         ff_read_frame_flush(s);
2491         return seek_frame_byte(s, stream_index, timestamp, flags);
2492     }
2493 
2494     if (stream_index < 0) {
2495         stream_index = av_find_default_stream_index(s);
2496         if (stream_index < 0)
2497             return -1;
2498 
2499         st = s->streams[stream_index];
2500         /* timestamp for default must be expressed in AV_TIME_BASE units */
2501         timestamp = av_rescale(timestamp, st->time_base.den,
2502                                AV_TIME_BASE * (int64_t) st->time_base.num);
2503     }
2504 
2505     /* first, we try the format specific seek */
2506     if (s->iformat->read_seek) {
2507         ff_read_frame_flush(s);
2508         ret = s->iformat->read_seek(s, stream_index, timestamp, flags);
2509     } else
2510         ret = -1;
2511     if (ret >= 0)
2512         return 0;
2513 
2514     if (s->iformat->read_timestamp &&
2515         !(s->iformat->flags & AVFMT_NOBINSEARCH)) {
2516         ff_read_frame_flush(s);
2517         return ff_seek_frame_binary(s, stream_index, timestamp, flags);
2518     } else if (!(s->iformat->flags & AVFMT_NOGENSEARCH)) {
2519         ff_read_frame_flush(s);
2520         return seek_frame_generic(s, stream_index, timestamp, flags);
2521     } else
2522         return -1;
2523 }
2524 
2525 int av_seek_frame(AVFormatContext *s, int stream_index,
2526                   int64_t timestamp, int flags)
2527 {
2528     int ret;
2529 
2530     if (s->iformat->read_seek2 && !s->iformat->read_seek) {
2531         int64_t min_ts = INT64_MIN, max_ts = INT64_MAX;
2532         if ((flags & AVSEEK_FLAG_BACKWARD))
2533             max_ts = timestamp;
2534         else
2535             min_ts = timestamp;
2536         return avformat_seek_file(s, stream_index, min_ts, timestamp, max_ts,
2537                                   flags & ~AVSEEK_FLAG_BACKWARD);
2538     }
2539 
2540     ret = seek_frame_internal(s, stream_index, timestamp, flags);
2541 
2542     if (ret >= 0)
2543         ret = avformat_queue_attached_pictures(s);
2544 
2545     return ret;
2546 }
2547 
2548 int avformat_seek_file(AVFormatContext *s, int stream_index, int64_t min_ts,
2549                        int64_t ts, int64_t max_ts, int flags)
2550 {
2551     if (min_ts > ts || max_ts < ts)
2552         return -1;
2553     if (stream_index < -1 || stream_index >= (int)s->nb_streams)
2554         return AVERROR(EINVAL);
2555 
2556     if (s->seek2any>0)
2557         flags |= AVSEEK_FLAG_ANY;
2558     flags &= ~AVSEEK_FLAG_BACKWARD;
2559 
2560     if (s->iformat->read_seek2) {
2561         int ret;
2562         ff_read_frame_flush(s);
2563 
2564         if (stream_index == -1 && s->nb_streams == 1) {
2565             AVRational time_base = s->streams[0]->time_base;
2566             ts = av_rescale_q(ts, AV_TIME_BASE_Q, time_base);
2567             min_ts = av_rescale_rnd(min_ts, time_base.den,
2568                                     time_base.num * (int64_t)AV_TIME_BASE,
2569                                     AV_ROUND_UP   | AV_ROUND_PASS_MINMAX);
2570             max_ts = av_rescale_rnd(max_ts, time_base.den,
2571                                     time_base.num * (int64_t)AV_TIME_BASE,
2572                                     AV_ROUND_DOWN | AV_ROUND_PASS_MINMAX);
2573             stream_index = 0;
2574         }
2575 
2576         ret = s->iformat->read_seek2(s, stream_index, min_ts,
2577                                      ts, max_ts, flags);
2578 
2579         if (ret >= 0)
2580             ret = avformat_queue_attached_pictures(s);
2581         return ret;
2582     }
2583 
2584     if (s->iformat->read_timestamp) {
2585         // try to seek via read_timestamp()
2586     }
2587 
2588     // Fall back on old API if new is not implemented but old is.
2589     // Note the old API has somewhat different semantics.
2590     if (s->iformat->read_seek || 1) {
2591         int dir = (ts - (uint64_t)min_ts > (uint64_t)max_ts - ts ? AVSEEK_FLAG_BACKWARD : 0);
2592         int ret = av_seek_frame(s, stream_index, ts, flags | dir);
2593         if (ret<0 && ts != min_ts && max_ts != ts) {
2594             ret = av_seek_frame(s, stream_index, dir ? max_ts : min_ts, flags | dir);
2595             if (ret >= 0)
2596                 ret = av_seek_frame(s, stream_index, ts, flags | (dir^AVSEEK_FLAG_BACKWARD));
2597         }
2598         return ret;
2599     }
2600 
2601     // try some generic seek like seek_frame_generic() but with new ts semantics
2602     return -1; //unreachable
2603 }
2604 
2605 int avformat_flush(AVFormatContext *s)
2606 {
2607     ff_read_frame_flush(s);
2608     return 0;
2609 }
2610 
2611 /*******************************************************/
2612 
2613 /**
2614  * Return TRUE if the stream has accurate duration in any stream.
2615  *
2616  * @return TRUE if the stream has accurate duration for at least one component.
2617  */
2618 static int has_duration(AVFormatContext *ic)
2619 {
2620     int i;
2621     AVStream *st;
2622 
2623     for (i = 0; i < ic->nb_streams; i++) {
2624         st = ic->streams[i];
2625         if (st->duration != AV_NOPTS_VALUE)
2626             return 1;
2627     }
2628     if (ic->duration != AV_NOPTS_VALUE)
2629         return 1;
2630     return 0;
2631 }
2632 
2633 /**
2634  * Estimate the stream timings from the one of each components.
2635  *
2636  * Also computes the global bitrate if possible.
2637  */
2638 static void update_stream_timings(AVFormatContext *ic)
2639 {
2640     int64_t start_time, start_time1, start_time_text, end_time, end_time1, end_time_text;
2641     int64_t duration, duration1, duration_text, filesize;
2642     int i;
2643     AVProgram *p;
2644 
2645     start_time = INT64_MAX;
2646     start_time_text = INT64_MAX;
2647     end_time   = INT64_MIN;
2648     end_time_text   = INT64_MIN;
2649     duration   = INT64_MIN;
2650     duration_text = INT64_MIN;
2651 
2652     for (i = 0; i < ic->nb_streams; i++) {
2653         AVStream *st = ic->streams[i];
2654         int is_text = st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE ||
2655                       st->codecpar->codec_type == AVMEDIA_TYPE_DATA;
2656         if (st->start_time != AV_NOPTS_VALUE && st->time_base.den) {
2657             start_time1 = av_rescale_q(st->start_time, st->time_base,
2658                                        AV_TIME_BASE_Q);
2659             if (is_text)
2660                 start_time_text = FFMIN(start_time_text, start_time1);
2661             else
2662                 start_time = FFMIN(start_time, start_time1);
2663             end_time1 = av_rescale_q_rnd(st->duration, st->time_base,
2664                                          AV_TIME_BASE_Q,
2665                                          AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
2666             if (end_time1 != AV_NOPTS_VALUE && (end_time1 > 0 ? start_time1 <= INT64_MAX - end_time1 : start_time1 >= INT64_MIN - end_time1)) {
2667                 end_time1 += start_time1;
2668                 if (is_text)
2669                     end_time_text = FFMAX(end_time_text, end_time1);
2670                 else
2671                     end_time = FFMAX(end_time, end_time1);
2672             }
2673             for (p = NULL; (p = av_find_program_from_stream(ic, p, i)); ) {
2674                 if (p->start_time == AV_NOPTS_VALUE || p->start_time > start_time1)
2675                     p->start_time = start_time1;
2676                 if (p->end_time < end_time1)
2677                     p->end_time = end_time1;
2678             }
2679         }
2680         if (st->duration != AV_NOPTS_VALUE) {
2681             duration1 = av_rescale_q(st->duration, st->time_base,
2682                                      AV_TIME_BASE_Q);
2683             if (is_text)
2684                 duration_text = FFMAX(duration_text, duration1);
2685             else
2686                 duration = FFMAX(duration, duration1);
2687         }
2688     }
2689     if (start_time == INT64_MAX || (start_time > start_time_text && start_time - (uint64_t)start_time_text < AV_TIME_BASE))
2690         start_time = start_time_text;
2691     else if (start_time > start_time_text)
2692         av_log(ic, AV_LOG_VERBOSE, "Ignoring outlier non primary stream starttime %f\n", start_time_text / (float)AV_TIME_BASE);
2693 
2694     if (end_time == INT64_MIN || (end_time < end_time_text && end_time_text - (uint64_t)end_time < AV_TIME_BASE))
2695         end_time = end_time_text;
2696     else if (end_time < end_time_text)
2697         av_log(ic, AV_LOG_VERBOSE, "Ignoring outlier non primary stream endtime %f\n", end_time_text / (float)AV_TIME_BASE);
2698 
2699      if (duration == INT64_MIN || (duration < duration_text && duration_text - duration < AV_TIME_BASE))
2700          duration = duration_text;
2701      else if (duration < duration_text)
2702          av_log(ic, AV_LOG_VERBOSE, "Ignoring outlier non primary stream duration %f\n", duration_text / (float)AV_TIME_BASE);
2703 
2704     if (start_time != INT64_MAX) {
2705         ic->start_time = start_time;
2706         if (end_time != INT64_MIN) {
2707             if (ic->nb_programs > 1) {
2708                 for (i = 0; i < ic->nb_programs; i++) {
2709                     p = ic->programs[i];
2710                     if (p->start_time != AV_NOPTS_VALUE &&
2711                         p->end_time > p->start_time &&
2712                         p->end_time - (uint64_t)p->start_time <= INT64_MAX)
2713                         duration = FFMAX(duration, p->end_time - p->start_time);
2714                 }
2715             } else if (end_time >= start_time && end_time - (uint64_t)start_time <= INT64_MAX) {
2716                 duration = FFMAX(duration, end_time - start_time);
2717             }
2718         }
2719     }
2720     if (duration != INT64_MIN && duration > 0 && ic->duration == AV_NOPTS_VALUE) {
2721         ic->duration = duration;
2722     }
2723     if (ic->pb && (filesize = avio_size(ic->pb)) > 0 && ic->duration > 0) {
2724         /* compute the bitrate */
2725         double bitrate = (double) filesize * 8.0 * AV_TIME_BASE /
2726                          (double) ic->duration;
2727         if (bitrate >= 0 && bitrate <= INT64_MAX)
2728             ic->bit_rate = bitrate;
2729     }
2730 }
2731 
2732 static void fill_all_stream_timings(AVFormatContext *ic)
2733 {
2734     int i;
2735     AVStream *st;
2736 
2737     update_stream_timings(ic);
2738     for (i = 0; i < ic->nb_streams; i++) {
2739         st = ic->streams[i];
2740         if (st->start_time == AV_NOPTS_VALUE) {
2741             if (ic->start_time != AV_NOPTS_VALUE)
2742                 st->start_time = av_rescale_q(ic->start_time, AV_TIME_BASE_Q,
2743                                               st->time_base);
2744             if (ic->duration != AV_NOPTS_VALUE)
2745                 st->duration = av_rescale_q(ic->duration, AV_TIME_BASE_Q,
2746                                             st->time_base);
2747         }
2748     }
2749 }
2750 
2751 static void estimate_timings_from_bit_rate(AVFormatContext *ic)
2752 {
2753     int64_t filesize, duration;
2754     int i, show_warning = 0;
2755     AVStream *st;
2756 
2757     /* if bit_rate is already set, we believe it */
2758     if (ic->bit_rate <= 0) {
2759         int64_t bit_rate = 0;
2760         for (i = 0; i < ic->nb_streams; i++) {
2761             st = ic->streams[i];
2762             if (st->codecpar->bit_rate <= 0 && st->internal->avctx->bit_rate > 0)
2763                 st->codecpar->bit_rate = st->internal->avctx->bit_rate;
2764             if (st->codecpar->bit_rate > 0) {
2765                 if (INT64_MAX - st->codecpar->bit_rate < bit_rate) {
2766                     bit_rate = 0;
2767                     break;
2768                 }
2769                 bit_rate += st->codecpar->bit_rate;
2770             } else if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && st->codec_info_nb_frames > 1) {
2771                 // If we have a videostream with packets but without a bitrate
2772                 // then consider the sum not known
2773                 bit_rate = 0;
2774                 break;
2775             }
2776         }
2777         ic->bit_rate = bit_rate;
2778     }
2779 
2780     /* if duration is already set, we believe it */
2781     if (ic->duration == AV_NOPTS_VALUE &&
2782         ic->bit_rate != 0) {
2783         filesize = ic->pb ? avio_size(ic->pb) : 0;
2784         if (filesize > ic->internal->data_offset) {
2785             filesize -= ic->internal->data_offset;
2786             for (i = 0; i < ic->nb_streams; i++) {
2787                 st      = ic->streams[i];
2788                 if (   st->time_base.num <= INT64_MAX / ic->bit_rate
2789                     && st->duration == AV_NOPTS_VALUE) {
2790                     duration = av_rescale(filesize, 8LL * st->time_base.den,
2791                                           ic->bit_rate *
2792                                           (int64_t) st->time_base.num);
2793                     st->duration = duration;
2794                     show_warning = 1;
2795                 }
2796             }
2797         }
2798     }
2799     if (show_warning)
2800         av_log(ic, AV_LOG_WARNING,
2801                "Estimating duration from bitrate, this may be inaccurate\n");
2802 }
2803 
2804 #define DURATION_MAX_READ_SIZE 250000LL
2805 #define DURATION_MAX_RETRY 6
2806 
2807 /* only usable for MPEG-PS streams */
2808 static void estimate_timings_from_pts(AVFormatContext *ic, int64_t old_offset)
2809 {
2810     AVPacket *pkt = ic->internal->pkt;
2811     AVStream *st;
2812     int num, den, read_size, i, ret;
2813     int found_duration = 0;
2814     int is_end;
2815     int64_t filesize, offset, duration;
2816     int retry = 0;
2817 
2818     /* flush packet queue */
2819     flush_packet_queue(ic);
2820 
2821     for (i = 0; i < ic->nb_streams; i++) {
2822         st = ic->streams[i];
2823         if (st->start_time == AV_NOPTS_VALUE &&
2824             st->first_dts == AV_NOPTS_VALUE &&
2825             st->codecpar->codec_type != AVMEDIA_TYPE_UNKNOWN)
2826             av_log(ic, AV_LOG_WARNING,
2827                    "start time for stream %d is not set in estimate_timings_from_pts\n", i);
2828 
2829         if (st->parser) {
2830             av_parser_close(st->parser);
2831             st->parser = NULL;
2832         }
2833     }
2834 
2835     if (ic->skip_estimate_duration_from_pts) {
2836         av_log(ic, AV_LOG_INFO, "Skipping duration calculation in estimate_timings_from_pts\n");
2837         goto skip_duration_calc;
2838     }
2839 
2840     av_opt_set(ic, "skip_changes", "1", AV_OPT_SEARCH_CHILDREN);
2841     /* estimate the end time (duration) */
2842     /* XXX: may need to support wrapping */
2843     filesize = ic->pb ? avio_size(ic->pb) : 0;
2844     do {
2845         is_end = found_duration;
2846         offset = filesize - (DURATION_MAX_READ_SIZE << retry);
2847         if (offset < 0)
2848             offset = 0;
2849 
2850         avio_seek(ic->pb, offset, SEEK_SET);
2851         read_size = 0;
2852         for (;;) {
2853             if (read_size >= DURATION_MAX_READ_SIZE << (FFMAX(retry - 1, 0)))
2854                 break;
2855 
2856             do {
2857                 ret = ff_read_packet(ic, pkt);
2858             } while (ret == AVERROR(EAGAIN));
2859             if (ret != 0)
2860                 break;
2861             read_size += pkt->size;
2862             st         = ic->streams[pkt->stream_index];
2863             if (pkt->pts != AV_NOPTS_VALUE &&
2864                 (st->start_time != AV_NOPTS_VALUE ||
2865                  st->first_dts  != AV_NOPTS_VALUE)) {
2866                 if (pkt->duration == 0) {
2867                     ff_compute_frame_duration(ic, &num, &den, st, st->parser, pkt);
2868                     if (den && num) {
2869                         pkt->duration = av_rescale_rnd(1,
2870                                            num * (int64_t) st->time_base.den,
2871                                            den * (int64_t) st->time_base.num,
2872                                            AV_ROUND_DOWN);
2873                     }
2874                 }
2875                 duration = pkt->pts + pkt->duration;
2876                 found_duration = 1;
2877                 if (st->start_time != AV_NOPTS_VALUE)
2878                     duration -= st->start_time;
2879                 else
2880                     duration -= st->first_dts;
2881                 if (duration > 0) {
2882                     if (st->duration == AV_NOPTS_VALUE || st->internal->info->last_duration<= 0 ||
2883                         (st->duration < duration && FFABS(duration - st->internal->info->last_duration) < 60LL*st->time_base.den / st->time_base.num))
2884                         st->duration = duration;
2885                     st->internal->info->last_duration = duration;
2886                 }
2887             }
2888             av_packet_unref(pkt);
2889         }
2890 
2891         /* check if all audio/video streams have valid duration */
2892         if (!is_end) {
2893             is_end = 1;
2894             for (i = 0; i < ic->nb_streams; i++) {
2895                 st = ic->streams[i];
2896                 switch (st->codecpar->codec_type) {
2897                     case AVMEDIA_TYPE_VIDEO:
2898                     case AVMEDIA_TYPE_AUDIO:
2899                         if (st->duration == AV_NOPTS_VALUE)
2900                             is_end = 0;
2901                 }
2902             }
2903         }
2904     } while (!is_end &&
2905              offset &&
2906              ++retry <= DURATION_MAX_RETRY);
2907 
2908     av_opt_set(ic, "skip_changes", "0", AV_OPT_SEARCH_CHILDREN);
2909 
2910     /* warn about audio/video streams which duration could not be estimated */
2911     for (i = 0; i < ic->nb_streams; i++) {
2912         st = ic->streams[i];
2913         if (st->duration == AV_NOPTS_VALUE) {
2914             switch (st->codecpar->codec_type) {
2915             case AVMEDIA_TYPE_VIDEO:
2916             case AVMEDIA_TYPE_AUDIO:
2917                 if (st->start_time != AV_NOPTS_VALUE || st->first_dts  != AV_NOPTS_VALUE) {
2918                     av_log(ic, AV_LOG_WARNING, "stream %d : no PTS found at end of file, duration not set\n", i);
2919                 } else
2920                     av_log(ic, AV_LOG_WARNING, "stream %d : no TS found at start of file, duration not set\n", i);
2921             }
2922         }
2923     }
2924 skip_duration_calc:
2925     fill_all_stream_timings(ic);
2926 
2927     avio_seek(ic->pb, old_offset, SEEK_SET);
2928     for (i = 0; i < ic->nb_streams; i++) {
2929         int j;
2930 
2931         st              = ic->streams[i];
2932         st->cur_dts     = st->first_dts;
2933         st->last_IP_pts = AV_NOPTS_VALUE;
2934         st->internal->last_dts_for_order_check = AV_NOPTS_VALUE;
2935         for (j = 0; j < MAX_REORDER_DELAY + 1; j++)
2936             st->internal->pts_buffer[j] = AV_NOPTS_VALUE;
2937     }
2938 }
2939 
2940 /* 1:1 map to AVDurationEstimationMethod */
2941 static const char *const duration_name[] = {
2942     [AVFMT_DURATION_FROM_PTS]     = "pts",
2943     [AVFMT_DURATION_FROM_STREAM]  = "stream",
2944     [AVFMT_DURATION_FROM_BITRATE] = "bit rate",
2945 };
2946 
2947 static const char *duration_estimate_name(enum AVDurationEstimationMethod method)
2948 {
2949     return duration_name[method];
2950 }
2951 
2952 static void estimate_timings(AVFormatContext *ic, int64_t old_offset)
2953 {
2954     int64_t file_size;
2955 
2956     /* get the file size, if possible */
2957     if (ic->iformat->flags & AVFMT_NOFILE) {
2958         file_size = 0;
2959     } else {
2960         file_size = avio_size(ic->pb);
2961         file_size = FFMAX(0, file_size);
2962     }
2963 
2964     if ((!strcmp(ic->iformat->name, "mpeg") ||
2965          !strcmp(ic->iformat->name, "mpegts")) &&
2966         file_size && (ic->pb->seekable & AVIO_SEEKABLE_NORMAL)) {
2967         /* get accurate estimate from the PTSes */
2968         estimate_timings_from_pts(ic, old_offset);
2969         ic->duration_estimation_method = AVFMT_DURATION_FROM_PTS;
2970     } else if (has_duration(ic)) {
2971         /* at least one component has timings - we use them for all
2972          * the components */
2973         fill_all_stream_timings(ic);
2974         /* nut demuxer estimate the duration from PTS */
2975         if(!strcmp(ic->iformat->name, "nut"))
2976             ic->duration_estimation_method = AVFMT_DURATION_FROM_PTS;
2977         else
2978             ic->duration_estimation_method = AVFMT_DURATION_FROM_STREAM;
2979     } else {
2980         /* less precise: use bitrate info */
2981         estimate_timings_from_bit_rate(ic);
2982         ic->duration_estimation_method = AVFMT_DURATION_FROM_BITRATE;
2983     }
2984     update_stream_timings(ic);
2985 
2986     {
2987         int i;
2988         AVStream av_unused *st;
2989         for (i = 0; i < ic->nb_streams; i++) {
2990             st = ic->streams[i];
2991             if (st->time_base.den)
2992                 av_log(ic, AV_LOG_TRACE, "stream %d: start_time: %s duration: %s\n", i,
2993                        av_ts2timestr(st->start_time, &st->time_base),
2994                        av_ts2timestr(st->duration, &st->time_base));
2995         }
2996         av_log(ic, AV_LOG_TRACE,
2997                "format: start_time: %s duration: %s (estimate from %s) bitrate=%"PRId64" kb/s\n",
2998                av_ts2timestr(ic->start_time, &AV_TIME_BASE_Q),
2999                av_ts2timestr(ic->duration, &AV_TIME_BASE_Q),
3000                duration_estimate_name(ic->duration_estimation_method),
3001                (int64_t)ic->bit_rate / 1000);
3002     }
3003 }
3004 
3005 static int has_codec_parameters(AVStream *st, const char **errmsg_ptr)
3006 {
3007     AVCodecContext *avctx = st->internal->avctx;
3008 
3009 #define FAIL(errmsg) do {                                         \
3010         if (errmsg_ptr)                                           \
3011             *errmsg_ptr = errmsg;                                 \
3012         return 0;                                                 \
3013     } while (0)
3014 
3015     if (   avctx->codec_id == AV_CODEC_ID_NONE
3016         && avctx->codec_type != AVMEDIA_TYPE_DATA)
3017         FAIL("unknown codec");
3018     switch (avctx->codec_type) {
3019     case AVMEDIA_TYPE_AUDIO:
3020         if (!avctx->frame_size && determinable_frame_size(avctx))
3021             FAIL("unspecified frame size");
3022         if (st->internal->info->found_decoder >= 0 &&
3023             avctx->sample_fmt == AV_SAMPLE_FMT_NONE)
3024             FAIL("unspecified sample format");
3025         if (!avctx->sample_rate)
3026             FAIL("unspecified sample rate");
3027         if (!avctx->channels)
3028             FAIL("unspecified number of channels");
3029         if (st->internal->info->found_decoder >= 0 && !st->internal->nb_decoded_frames && avctx->codec_id == AV_CODEC_ID_DTS)
3030             FAIL("no decodable DTS frames");
3031         break;
3032     case AVMEDIA_TYPE_VIDEO:
3033         if (!avctx->width)
3034             FAIL("unspecified size");
3035         if (st->internal->info->found_decoder >= 0 && avctx->pix_fmt == AV_PIX_FMT_NONE)
3036             FAIL("unspecified pixel format");
3037         if (st->codecpar->codec_id == AV_CODEC_ID_RV30 || st->codecpar->codec_id == AV_CODEC_ID_RV40)
3038             if (!st->sample_aspect_ratio.num && !st->codecpar->sample_aspect_ratio.num && !st->codec_info_nb_frames)
3039                 FAIL("no frame in rv30/40 and no sar");
3040         break;
3041     case AVMEDIA_TYPE_SUBTITLE:
3042         if (avctx->codec_id == AV_CODEC_ID_HDMV_PGS_SUBTITLE && !avctx->width)
3043             FAIL("unspecified size");
3044         break;
3045     case AVMEDIA_TYPE_DATA:
3046         if (avctx->codec_id == AV_CODEC_ID_NONE) return 1;
3047     }
3048 
3049     return 1;
3050 }
3051 
3052 /* returns 1 or 0 if or if not decoded data was returned, or a negative error */
3053 static int try_decode_frame(AVFormatContext *s, AVStream *st,
3054                             const AVPacket *avpkt, AVDictionary **options)
3055 {
3056     AVCodecContext *avctx = st->internal->avctx;
3057     const AVCodec *codec;
3058     int got_picture = 1, ret = 0;
3059     AVFrame *frame = av_frame_alloc();
3060     AVSubtitle subtitle;
3061     AVPacket pkt = *avpkt;
3062     int do_skip_frame = 0;
3063     enum AVDiscard skip_frame;
3064 
3065     if (!frame)
3066         return AVERROR(ENOMEM);
3067 
3068     if (!avcodec_is_open(avctx) &&
3069         st->internal->info->found_decoder <= 0 &&
3070         (st->codecpar->codec_id != -st->internal->info->found_decoder || !st->codecpar->codec_id)) {
3071         AVDictionary *thread_opt = NULL;
3072 
3073         codec = find_probe_decoder(s, st, st->codecpar->codec_id);
3074 
3075         if (!codec) {
3076             st->internal->info->found_decoder = -st->codecpar->codec_id;
3077             ret                     = -1;
3078             goto fail;
3079         }
3080 
3081         /* Force thread count to 1 since the H.264 decoder will not extract
3082          * SPS and PPS to extradata during multi-threaded decoding. */
3083         av_dict_set(options ? options : &thread_opt, "threads", "1", 0);
3084         /* Force lowres to 0. The decoder might reduce the video size by the
3085          * lowres factor, and we don't want that propagated to the stream's
3086          * codecpar */
3087         av_dict_set(options ? options : &thread_opt, "lowres", "0", 0);
3088         if (s->codec_whitelist)
3089             av_dict_set(options ? options : &thread_opt, "codec_whitelist", s->codec_whitelist, 0);
3090         ret = avcodec_open2(avctx, codec, options ? options : &thread_opt);
3091         if (!options)
3092             av_dict_free(&thread_opt);
3093         if (ret < 0) {
3094             st->internal->info->found_decoder = -avctx->codec_id;
3095             goto fail;
3096         }
3097         st->internal->info->found_decoder = 1;
3098     } else if (!st->internal->info->found_decoder)
3099         st->internal->info->found_decoder = 1;
3100 
3101     if (st->internal->info->found_decoder < 0) {
3102         ret = -1;
3103         goto fail;
3104     }
3105 
3106     if (avpriv_codec_get_cap_skip_frame_fill_param(avctx->codec)) {
3107         do_skip_frame = 1;
3108         skip_frame = avctx->skip_frame;
3109         avctx->skip_frame = AVDISCARD_ALL;
3110     }
3111 
3112     while ((pkt.size > 0 || (!pkt.data && got_picture)) &&
3113            ret >= 0 &&
3114            (!has_codec_parameters(st, NULL) || !has_decode_delay_been_guessed(st) ||
3115             (!st->codec_info_nb_frames &&
3116              (avctx->codec->capabilities & AV_CODEC_CAP_CHANNEL_CONF)))) {
3117         got_picture = 0;
3118         if (avctx->codec_type == AVMEDIA_TYPE_VIDEO ||
3119             avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
3120             ret = avcodec_send_packet(avctx, &pkt);
3121             if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
3122                 break;
3123             if (ret >= 0)
3124                 pkt.size = 0;
3125             ret = avcodec_receive_frame(avctx, frame);
3126             if (ret >= 0)
3127                 got_picture = 1;
3128             if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
3129                 ret = 0;
3130         } else if (avctx->codec_type == AVMEDIA_TYPE_SUBTITLE) {
3131             ret = avcodec_decode_subtitle2(avctx, &subtitle,
3132                                            &got_picture, &pkt);
3133             if (got_picture)
3134                 avsubtitle_free(&subtitle);
3135             if (ret >= 0)
3136                 pkt.size = 0;
3137         }
3138         if (ret >= 0) {
3139             if (got_picture)
3140                 st->internal->nb_decoded_frames++;
3141             ret       = got_picture;
3142         }
3143     }
3144 
3145     if (!pkt.data && !got_picture)
3146         ret = -1;
3147 
3148 fail:
3149     if (do_skip_frame) {
3150         avctx->skip_frame = skip_frame;
3151     }
3152 
3153     av_frame_free(&frame);
3154     return ret;
3155 }
3156 
3157 unsigned int ff_codec_get_tag(const AVCodecTag *tags, enum AVCodecID id)
3158 {
3159     while (tags->id != AV_CODEC_ID_NONE) {
3160         if (tags->id == id)
3161             return tags->tag;
3162         tags++;
3163     }
3164     return 0;
3165 }
3166 
3167 enum AVCodecID ff_codec_get_id(const AVCodecTag *tags, unsigned int tag)
3168 {
3169     int i;
3170     for (i = 0; tags[i].id != AV_CODEC_ID_NONE; i++)
3171         if (tag == tags[i].tag)
3172             return tags[i].id;
3173     for (i = 0; tags[i].id != AV_CODEC_ID_NONE; i++)
3174         if (avpriv_toupper4(tag) == avpriv_toupper4(tags[i].tag))
3175             return tags[i].id;
3176     return AV_CODEC_ID_NONE;
3177 }
3178 
3179 enum AVCodecID ff_get_pcm_codec_id(int bps, int flt, int be, int sflags)
3180 {
3181     if (bps <= 0 || bps > 64)
3182         return AV_CODEC_ID_NONE;
3183 
3184     if (flt) {
3185         switch (bps) {
3186         case 32:
3187             return be ? AV_CODEC_ID_PCM_F32BE : AV_CODEC_ID_PCM_F32LE;
3188         case 64:
3189             return be ? AV_CODEC_ID_PCM_F64BE : AV_CODEC_ID_PCM_F64LE;
3190         default:
3191             return AV_CODEC_ID_NONE;
3192         }
3193     } else {
3194         bps  += 7;
3195         bps >>= 3;
3196         if (sflags & (1 << (bps - 1))) {
3197             switch (bps) {
3198             case 1:
3199                 return AV_CODEC_ID_PCM_S8;
3200             case 2:
3201                 return be ? AV_CODEC_ID_PCM_S16BE : AV_CODEC_ID_PCM_S16LE;
3202             case 3:
3203                 return be ? AV_CODEC_ID_PCM_S24BE : AV_CODEC_ID_PCM_S24LE;
3204             case 4:
3205                 return be ? AV_CODEC_ID_PCM_S32BE : AV_CODEC_ID_PCM_S32LE;
3206             case 8:
3207                 return be ? AV_CODEC_ID_PCM_S64BE : AV_CODEC_ID_PCM_S64LE;
3208             default:
3209                 return AV_CODEC_ID_NONE;
3210             }
3211         } else {
3212             switch (bps) {
3213             case 1:
3214                 return AV_CODEC_ID_PCM_U8;
3215             case 2:
3216                 return be ? AV_CODEC_ID_PCM_U16BE : AV_CODEC_ID_PCM_U16LE;
3217             case 3:
3218                 return be ? AV_CODEC_ID_PCM_U24BE : AV_CODEC_ID_PCM_U24LE;
3219             case 4:
3220                 return be ? AV_CODEC_ID_PCM_U32BE : AV_CODEC_ID_PCM_U32LE;
3221             default:
3222                 return AV_CODEC_ID_NONE;
3223             }
3224         }
3225     }
3226 }
3227 
3228 unsigned int av_codec_get_tag(const AVCodecTag *const *tags, enum AVCodecID id)
3229 {
3230     unsigned int tag;
3231     if (!av_codec_get_tag2(tags, id, &tag))
3232         return 0;
3233     return tag;
3234 }
3235 
3236 int av_codec_get_tag2(const AVCodecTag * const *tags, enum AVCodecID id,
3237                       unsigned int *tag)
3238 {
3239     int i;
3240     for (i = 0; tags && tags[i]; i++) {
3241         const AVCodecTag *codec_tags = tags[i];
3242         while (codec_tags->id != AV_CODEC_ID_NONE) {
3243             if (codec_tags->id == id) {
3244                 *tag = codec_tags->tag;
3245                 return 1;
3246             }
3247             codec_tags++;
3248         }
3249     }
3250     return 0;
3251 }
3252 
3253 enum AVCodecID av_codec_get_id(const AVCodecTag *const *tags, unsigned int tag)
3254 {
3255     int i;
3256     for (i = 0; tags && tags[i]; i++) {
3257         enum AVCodecID id = ff_codec_get_id(tags[i], tag);
3258         if (id != AV_CODEC_ID_NONE)
3259             return id;
3260     }
3261     return AV_CODEC_ID_NONE;
3262 }
3263 
3264 static int chapter_start_cmp(const void *p1, const void *p2)
3265 {
3266     AVChapter *ch1 = *(AVChapter**)p1;
3267     AVChapter *ch2 = *(AVChapter**)p2;
3268     int delta = av_compare_ts(ch1->start, ch1->time_base, ch2->start, ch2->time_base);
3269     if (delta)
3270         return delta;
3271     return (ch1 > ch2) - (ch1 < ch2);
3272 }
3273 
3274 static int compute_chapters_end(AVFormatContext *s)
3275 {
3276     unsigned int i;
3277     int64_t max_time = 0;
3278     AVChapter **timetable = av_malloc(s->nb_chapters * sizeof(*timetable));
3279 
3280     if (!timetable)
3281         return AVERROR(ENOMEM);
3282 
3283     if (s->duration > 0 && s->start_time < INT64_MAX - s->duration)
3284         max_time = s->duration +
3285                        ((s->start_time == AV_NOPTS_VALUE) ? 0 : s->start_time);
3286 
3287     for (i = 0; i < s->nb_chapters; i++)
3288         timetable[i] = s->chapters[i];
3289     qsort(timetable, s->nb_chapters, sizeof(*timetable), chapter_start_cmp);
3290 
3291     for (i = 0; i < s->nb_chapters; i++)
3292         if (timetable[i]->end == AV_NOPTS_VALUE) {
3293             AVChapter *ch = timetable[i];
3294             int64_t end = max_time ? av_rescale_q(max_time, AV_TIME_BASE_Q,
3295                                                 ch->time_base)
3296                                 : INT64_MAX;
3297 
3298             if (i + 1 < s->nb_chapters) {
3299                 AVChapter *ch1     = timetable[i + 1];
3300                 int64_t next_start = av_rescale_q(ch1->start, ch1->time_base,
3301                                                 ch->time_base);
3302                 if (next_start > ch->start && next_start < end)
3303                     end = next_start;
3304             }
3305             ch->end = (end == INT64_MAX || end < ch->start) ? ch->start : end;
3306         }
3307     av_free(timetable);
3308     return 0;
3309 }
3310 
3311 static int get_std_framerate(int i)
3312 {
3313     if (i < 30*12)
3314         return (i + 1) * 1001;
3315     i -= 30*12;
3316 
3317     if (i < 30)
3318         return (i + 31) * 1001 * 12;
3319     i -= 30;
3320 
3321     if (i < 3)
3322         return ((const int[]) { 80, 120, 240})[i] * 1001 * 12;
3323 
3324     i -= 3;
3325 
3326     return ((const int[]) { 24, 30, 60, 12, 15, 48 })[i] * 1000 * 12;
3327 }
3328 
3329 /* Is the time base unreliable?
3330  * This is a heuristic to balance between quick acceptance of the values in
3331  * the headers vs. some extra checks.
3332  * Old DivX and Xvid often have nonsense timebases like 1fps or 2fps.
3333  * MPEG-2 commonly misuses field repeat flags to store different framerates.
3334  * And there are "variable" fps files this needs to detect as well. */
3335 static int tb_unreliable(AVCodecContext *c)
3336 {
3337     if (c->time_base.den >= 101LL * c->time_base.num ||
3338         c->time_base.den <    5LL * c->time_base.num ||
3339         // c->codec_tag == AV_RL32("DIVX") ||
3340         // c->codec_tag == AV_RL32("XVID") ||
3341         c->codec_tag == AV_RL32("mp4v") ||
3342         c->codec_id == AV_CODEC_ID_MPEG2VIDEO ||
3343         c->codec_id == AV_CODEC_ID_GIF ||
3344         c->codec_id == AV_CODEC_ID_HEVC ||
3345         c->codec_id == AV_CODEC_ID_H264)
3346         return 1;
3347     return 0;
3348 }
3349 
3350 int ff_alloc_extradata(AVCodecParameters *par, int size)
3351 {
3352     av_freep(&par->extradata);
3353     par->extradata_size = 0;
3354 
3355     if (size < 0 || size >= INT32_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
3356         return AVERROR(EINVAL);
3357 
3358     par->extradata = av_malloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
3359     if (!par->extradata)
3360         return AVERROR(ENOMEM);
3361 
3362     memset(par->extradata + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
3363     par->extradata_size = size;
3364 
3365     return 0;
3366 }
3367 
3368 int ff_get_extradata(AVFormatContext *s, AVCodecParameters *par, AVIOContext *pb, int size)
3369 {
3370     int ret = ff_alloc_extradata(par, size);
3371     if (ret < 0)
3372         return ret;
3373     ret = avio_read(pb, par->extradata, size);
3374     if (ret != size) {
3375         av_freep(&par->extradata);
3376         par->extradata_size = 0;
3377         av_log(s, AV_LOG_ERROR, "Failed to read extradata of size %d\n", size);
3378         return ret < 0 ? ret : AVERROR_INVALIDDATA;
3379     }
3380 
3381     return ret;
3382 }
3383 
3384 int ff_rfps_add_frame(AVFormatContext *ic, AVStream *st, int64_t ts)
3385 {
3386     int i, j;
3387     int64_t last = st->internal->info->last_dts;
3388 
3389     if (   ts != AV_NOPTS_VALUE && last != AV_NOPTS_VALUE && ts > last
3390        && ts - (uint64_t)last < INT64_MAX) {
3391         double dts = (is_relative(ts) ?  ts - RELATIVE_TS_BASE : ts) * av_q2d(st->time_base);
3392         int64_t duration = ts - last;
3393 
3394         if (!st->internal->info->duration_error)
3395             st->internal->info->duration_error = av_mallocz(sizeof(st->internal->info->duration_error[0])*2);
3396         if (!st->internal->info->duration_error)
3397             return AVERROR(ENOMEM);
3398 
3399 //         if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
3400 //             av_log(NULL, AV_LOG_ERROR, "%f\n", dts);
3401         for (i = 0; i<MAX_STD_TIMEBASES; i++) {
3402             if (st->internal->info->duration_error[0][1][i] < 1e10) {
3403                 int framerate = get_std_framerate(i);
3404                 double sdts = dts*framerate/(1001*12);
3405                 for (j= 0; j<2; j++) {
3406                     int64_t ticks = llrint(sdts+j*0.5);
3407                     double error= sdts - ticks + j*0.5;
3408                     st->internal->info->duration_error[j][0][i] += error;
3409                     st->internal->info->duration_error[j][1][i] += error*error;
3410                 }
3411             }
3412         }
3413         if (st->internal->info->rfps_duration_sum <= INT64_MAX - duration) {
3414             st->internal->info->duration_count++;
3415             st->internal->info->rfps_duration_sum += duration;
3416         }
3417 
3418         if (st->internal->info->duration_count % 10 == 0) {
3419             int n = st->internal->info->duration_count;
3420             for (i = 0; i<MAX_STD_TIMEBASES; i++) {
3421                 if (st->internal->info->duration_error[0][1][i] < 1e10) {
3422                     double a0     = st->internal->info->duration_error[0][0][i] / n;
3423                     double error0 = st->internal->info->duration_error[0][1][i] / n - a0*a0;
3424                     double a1     = st->internal->info->duration_error[1][0][i] / n;
3425                     double error1 = st->internal->info->duration_error[1][1][i] / n - a1*a1;
3426                     if (error0 > 0.04 && error1 > 0.04) {
3427                         st->internal->info->duration_error[0][1][i] = 2e10;
3428                         st->internal->info->duration_error[1][1][i] = 2e10;
3429                     }
3430                 }
3431             }
3432         }
3433 
3434         // ignore the first 4 values, they might have some random jitter
3435         if (st->internal->info->duration_count > 3 && is_relative(ts) == is_relative(last))
3436             st->internal->info->duration_gcd = av_gcd(st->internal->info->duration_gcd, duration);
3437     }
3438     if (ts != AV_NOPTS_VALUE)
3439         st->internal->info->last_dts = ts;
3440 
3441     return 0;
3442 }
3443 
3444 void ff_rfps_calculate(AVFormatContext *ic)
3445 {
3446     int i, j;
3447 
3448     for (i = 0; i < ic->nb_streams; i++) {
3449         AVStream *st = ic->streams[i];
3450 
3451         if (st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO)
3452             continue;
3453         // the check for tb_unreliable() is not completely correct, since this is not about handling
3454         // an unreliable/inexact time base, but a time base that is finer than necessary, as e.g.
3455         // ipmovie.c produces.
3456         if (tb_unreliable(st->internal->avctx) && st->internal->info->duration_count > 15 && st->internal->info->duration_gcd > FFMAX(1, st->time_base.den/(500LL*st->time_base.num)) && !st->r_frame_rate.num &&
3457             st->internal->info->duration_gcd < INT64_MAX / st->time_base.num)
3458             av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den, st->time_base.den, st->time_base.num * st->internal->info->duration_gcd, INT_MAX);
3459         if (st->internal->info->duration_count>1 && !st->r_frame_rate.num
3460             && tb_unreliable(st->internal->avctx)) {
3461             int num = 0;
3462             double best_error= 0.01;
3463             AVRational ref_rate = st->r_frame_rate.num ? st->r_frame_rate : av_inv_q(st->time_base);
3464 
3465             for (j= 0; j<MAX_STD_TIMEBASES; j++) {
3466                 int k;
3467 
3468                 if (st->internal->info->codec_info_duration &&
3469                     st->internal->info->codec_info_duration*av_q2d(st->time_base) < (1001*11.5)/get_std_framerate(j))
3470                     continue;
3471                 if (!st->internal->info->codec_info_duration && get_std_framerate(j) < 1001*12)
3472                     continue;
3473 
3474                 if (av_q2d(st->time_base) * st->internal->info->rfps_duration_sum / st->internal->info->duration_count < (1001*12.0 * 0.8)/get_std_framerate(j))
3475                     continue;
3476 
3477                 for (k= 0; k<2; k++) {
3478                     int n = st->internal->info->duration_count;
3479                     double a= st->internal->info->duration_error[k][0][j] / n;
3480                     double error= st->internal->info->duration_error[k][1][j]/n - a*a;
3481 
3482                     if (error < best_error && best_error> 0.000000001) {
3483                         best_error= error;
3484                         num = get_std_framerate(j);
3485                     }
3486                     if (error < 0.02)
3487                         av_log(ic, AV_LOG_DEBUG, "rfps: %f %f\n", get_std_framerate(j) / 12.0/1001, error);
3488                 }
3489             }
3490             // do not increase frame rate by more than 1 % in order to match a standard rate.
3491             if (num && (!ref_rate.num || (double)num/(12*1001) < 1.01 * av_q2d(ref_rate)))
3492                 av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den, num, 12*1001, INT_MAX);
3493         }
3494         if (   !st->avg_frame_rate.num
3495             && st->r_frame_rate.num && st->internal->info->rfps_duration_sum
3496             && st->internal->info->codec_info_duration <= 0
3497             && st->internal->info->duration_count > 2
3498             && fabs(1.0 / (av_q2d(st->r_frame_rate) * av_q2d(st->time_base)) - st->internal->info->rfps_duration_sum / (double)st->internal->info->duration_count) <= 1.0
3499             ) {
3500             av_log(ic, AV_LOG_DEBUG, "Setting avg frame rate based on r frame rate\n");
3501             st->avg_frame_rate = st->r_frame_rate;
3502         }
3503 
3504         av_freep(&st->internal->info->duration_error);
3505         st->internal->info->last_dts = AV_NOPTS_VALUE;
3506         st->internal->info->duration_count = 0;
3507         st->internal->info->rfps_duration_sum = 0;
3508     }
3509 }
3510 
3511 static int extract_extradata_check(AVStream *st)
3512 {
3513     const AVBitStreamFilter *f;
3514 
3515     f = av_bsf_get_by_name("extract_extradata");
3516     if (!f)
3517         return 0;
3518 
3519     if (f->codec_ids) {
3520         const enum AVCodecID *ids;
3521         for (ids = f->codec_ids; *ids != AV_CODEC_ID_NONE; ids++)
3522             if (*ids == st->codecpar->codec_id)
3523                 return 1;
3524     }
3525 
3526     return 0;
3527 }
3528 
3529 static int extract_extradata_init(AVStream *st)
3530 {
3531     AVStreamInternal *sti = st->internal;
3532     const AVBitStreamFilter *f;
3533     int ret;
3534 
3535     f = av_bsf_get_by_name("extract_extradata");
3536     if (!f)
3537         goto finish;
3538 
3539     /* check that the codec id is supported */
3540     ret = extract_extradata_check(st);
3541     if (!ret)
3542         goto finish;
3543 
3544     sti->extract_extradata.pkt = av_packet_alloc();
3545     if (!sti->extract_extradata.pkt)
3546         return AVERROR(ENOMEM);
3547 
3548     ret = av_bsf_alloc(f, &sti->extract_extradata.bsf);
3549     if (ret < 0)
3550         goto fail;
3551 
3552     ret = avcodec_parameters_copy(sti->extract_extradata.bsf->par_in,
3553                                   st->codecpar);
3554     if (ret < 0)
3555         goto fail;
3556 
3557     sti->extract_extradata.bsf->time_base_in = st->time_base;
3558 
3559     ret = av_bsf_init(sti->extract_extradata.bsf);
3560     if (ret < 0)
3561         goto fail;
3562 
3563 finish:
3564     sti->extract_extradata.inited = 1;
3565 
3566     return 0;
3567 fail:
3568     av_bsf_free(&sti->extract_extradata.bsf);
3569     av_packet_free(&sti->extract_extradata.pkt);
3570     return ret;
3571 }
3572 
3573 static int extract_extradata(AVStream *st, const AVPacket *pkt)
3574 {
3575     AVStreamInternal *sti = st->internal;
3576     AVPacket *pkt_ref;
3577     int ret;
3578 
3579     if (!sti->extract_extradata.inited) {
3580         ret = extract_extradata_init(st);
3581         if (ret < 0)
3582             return ret;
3583     }
3584 
3585     if (sti->extract_extradata.inited && !sti->extract_extradata.bsf)
3586         return 0;
3587 
3588     pkt_ref = sti->extract_extradata.pkt;
3589     ret = av_packet_ref(pkt_ref, pkt);
3590     if (ret < 0)
3591         return ret;
3592 
3593     ret = av_bsf_send_packet(sti->extract_extradata.bsf, pkt_ref);
3594     if (ret < 0) {
3595         av_packet_unref(pkt_ref);
3596         return ret;
3597     }
3598 
3599     while (ret >= 0 && !sti->avctx->extradata) {
3600         ret = av_bsf_receive_packet(sti->extract_extradata.bsf, pkt_ref);
3601         if (ret < 0) {
3602             if (ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
3603                 return ret;
3604             continue;
3605         }
3606 
3607         for (int i = 0; i < pkt_ref->side_data_elems; i++) {
3608             AVPacketSideData *side_data = &pkt_ref->side_data[i];
3609             if (side_data->type == AV_PKT_DATA_NEW_EXTRADATA) {
3610                 sti->avctx->extradata      = side_data->data;
3611                 sti->avctx->extradata_size = side_data->size;
3612                 side_data->data = NULL;
3613                 side_data->size = 0;
3614                 break;
3615             }
3616         }
3617         av_packet_unref(pkt_ref);
3618     }
3619 
3620     return 0;
3621 }
3622 
3623 static int add_coded_side_data(AVStream *st, AVCodecContext *avctx)
3624 {
3625     int i;
3626 
3627     for (i = 0; i < avctx->nb_coded_side_data; i++) {
3628         const AVPacketSideData *sd_src = &avctx->coded_side_data[i];
3629         uint8_t *dst_data;
3630         dst_data = av_stream_new_side_data(st, sd_src->type, sd_src->size);
3631         if (!dst_data)
3632             return AVERROR(ENOMEM);
3633         memcpy(dst_data, sd_src->data, sd_src->size);
3634     }
3635     return 0;
3636 }
3637 
3638 int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
3639 {
3640     int i, count = 0, ret = 0, j;
3641     int64_t read_size;
3642     AVStream *st;
3643     AVCodecContext *avctx;
3644     AVPacket *pkt1 = ic->internal->pkt;
3645     int64_t old_offset  = avio_tell(ic->pb);
3646     // new streams might appear, no options for those
3647     int orig_nb_streams = ic->nb_streams;
3648     int flush_codecs;
3649     int64_t max_analyze_duration = ic->max_analyze_duration;
3650     int64_t max_stream_analyze_duration;
3651     int64_t max_subtitle_analyze_duration;
3652     int64_t probesize = ic->probesize;
3653     int eof_reached = 0;
3654     int *missing_streams = av_opt_ptr(ic->iformat->priv_class, ic->priv_data, "missing_streams");
3655 
3656     flush_codecs = probesize > 0;
3657 
3658     av_opt_set(ic, "skip_clear", "1", AV_OPT_SEARCH_CHILDREN);
3659 
3660     max_stream_analyze_duration = max_analyze_duration;
3661     max_subtitle_analyze_duration = max_analyze_duration;
3662     if (!max_analyze_duration) {
3663         max_stream_analyze_duration =
3664         max_analyze_duration        = 5*AV_TIME_BASE;
3665         max_subtitle_analyze_duration = 30*AV_TIME_BASE;
3666         if (!strcmp(ic->iformat->name, "flv"))
3667             max_stream_analyze_duration = 90*AV_TIME_BASE;
3668         if (!strcmp(ic->iformat->name, "mpeg") || !strcmp(ic->iformat->name, "mpegts"))
3669             max_stream_analyze_duration = 7*AV_TIME_BASE;
3670     }
3671 
3672     if (ic->pb)
3673         av_log(ic, AV_LOG_DEBUG, "Before avformat_find_stream_info() pos: %"PRId64" bytes read:%"PRId64" seeks:%d nb_streams:%d\n",
3674                avio_tell(ic->pb), ic->pb->bytes_read, ic->pb->seek_count, ic->nb_streams);
3675 
3676     for (i = 0; i < ic->nb_streams; i++) {
3677         const AVCodec *codec;
3678         AVDictionary *thread_opt = NULL;
3679         st = ic->streams[i];
3680         avctx = st->internal->avctx;
3681 
3682         if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ||
3683             st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) {
3684 /*            if (!st->time_base.num)
3685                 st->time_base = */
3686             if (!avctx->time_base.num)
3687                 avctx->time_base = st->time_base;
3688         }
3689 
3690         /* check if the caller has overridden the codec id */
3691 #if FF_API_LAVF_AVCTX
3692 FF_DISABLE_DEPRECATION_WARNINGS
3693         if (st->codec->codec_id != st->internal->orig_codec_id) {
3694             st->codecpar->codec_id   = st->codec->codec_id;
3695             st->codecpar->codec_type = st->codec->codec_type;
3696             st->internal->orig_codec_id = st->codec->codec_id;
3697         }
3698 FF_ENABLE_DEPRECATION_WARNINGS
3699 #endif
3700         // only for the split stuff
3701         if (!st->parser && !(ic->flags & AVFMT_FLAG_NOPARSE) && st->internal->request_probe <= 0) {
3702             st->parser = av_parser_init(st->codecpar->codec_id);
3703             if (st->parser) {
3704                 if (st->need_parsing == AVSTREAM_PARSE_HEADERS) {
3705                     st->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES;
3706                 } else if (st->need_parsing == AVSTREAM_PARSE_FULL_RAW) {
3707                     st->parser->flags |= PARSER_FLAG_USE_CODEC_TS;
3708                 }
3709             } else if (st->need_parsing) {
3710                 av_log(ic, AV_LOG_VERBOSE, "parser not found for codec "
3711                        "%s, packets or times may be invalid.\n",
3712                        avcodec_get_name(st->codecpar->codec_id));
3713             }
3714         }
3715 
3716         if (st->codecpar->codec_id != st->internal->orig_codec_id)
3717             st->internal->orig_codec_id = st->codecpar->codec_id;
3718 
3719         ret = avcodec_parameters_to_context(avctx, st->codecpar);
3720         if (ret < 0)
3721             goto find_stream_info_err;
3722         if (st->internal->request_probe <= 0)
3723             st->internal->avctx_inited = 1;
3724 
3725         codec = find_probe_decoder(ic, st, st->codecpar->codec_id);
3726 
3727         /* Force thread count to 1 since the H.264 decoder will not extract
3728          * SPS and PPS to extradata during multi-threaded decoding. */
3729         av_dict_set(options ? &options[i] : &thread_opt, "threads", "1", 0);
3730         /* Force lowres to 0. The decoder might reduce the video size by the
3731          * lowres factor, and we don't want that propagated to the stream's
3732          * codecpar */
3733         av_dict_set(options ? &options[i] : &thread_opt, "lowres", "0", 0);
3734 
3735         if (ic->codec_whitelist)
3736             av_dict_set(options ? &options[i] : &thread_opt, "codec_whitelist", ic->codec_whitelist, 0);
3737 
3738         /* Ensure that subtitle_header is properly set. */
3739         if (st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE
3740             && codec && !avctx->codec) {
3741             if (avcodec_open2(avctx, codec, options ? &options[i] : &thread_opt) < 0)
3742                 av_log(ic, AV_LOG_WARNING,
3743                        "Failed to open codec in %s\n",__FUNCTION__);
3744         }
3745 
3746         // Try to just open decoders, in case this is enough to get parameters.
3747         if (!has_codec_parameters(st, NULL) && st->internal->request_probe <= 0) {
3748             if (codec && !avctx->codec)
3749                 if (avcodec_open2(avctx, codec, options ? &options[i] : &thread_opt) < 0)
3750                     av_log(ic, AV_LOG_WARNING,
3751                            "Failed to open codec in %s\n",__FUNCTION__);
3752         }
3753         if (!options)
3754             av_dict_free(&thread_opt);
3755     }
3756 
3757     for (i = 0; i < ic->nb_streams; i++) {
3758 #if FF_API_R_FRAME_RATE
3759         ic->streams[i]->internal->info->last_dts = AV_NOPTS_VALUE;
3760 #endif
3761         ic->streams[i]->internal->info->fps_first_dts = AV_NOPTS_VALUE;
3762         ic->streams[i]->internal->info->fps_last_dts  = AV_NOPTS_VALUE;
3763     }
3764 
3765     read_size = 0;
3766     for (;;) {
3767         const AVPacket *pkt;
3768         int analyzed_all_streams;
3769         if (ff_check_interrupt(&ic->interrupt_callback)) {
3770             ret = AVERROR_EXIT;
3771             av_log(ic, AV_LOG_DEBUG, "interrupted\n");
3772             break;
3773         }
3774 
3775         /* check if one codec still needs to be handled */
3776         for (i = 0; i < ic->nb_streams; i++) {
3777             int fps_analyze_framecount = 20;
3778             int count;
3779 
3780             st = ic->streams[i];
3781             if (!has_codec_parameters(st, NULL))
3782                 break;
3783             /* If the timebase is coarse (like the usual millisecond precision
3784              * of mkv), we need to analyze more frames to reliably arrive at
3785              * the correct fps. */
3786             if (av_q2d(st->time_base) > 0.0005)
3787                 fps_analyze_framecount *= 2;
3788             if (!tb_unreliable(st->internal->avctx))
3789                 fps_analyze_framecount = 0;
3790             if (ic->fps_probe_size >= 0)
3791                 fps_analyze_framecount = ic->fps_probe_size;
3792             if (st->disposition & AV_DISPOSITION_ATTACHED_PIC)
3793                 fps_analyze_framecount = 0;
3794             /* variable fps and no guess at the real fps */
3795             count = (ic->iformat->flags & AVFMT_NOTIMESTAMPS) ?
3796                        st->internal->info->codec_info_duration_fields/2 :
3797                        st->internal->info->duration_count;
3798             if (!(st->r_frame_rate.num && st->avg_frame_rate.num) &&
3799                 st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
3800                 if (count < fps_analyze_framecount)
3801                     break;
3802             }
3803             // Look at the first 3 frames if there is evidence of frame delay
3804             // but the decoder delay is not set.
3805             if (st->internal->info->frame_delay_evidence && count < 2 && st->internal->avctx->has_b_frames == 0)
3806                 break;
3807             if (!st->internal->avctx->extradata &&
3808                 (!st->internal->extract_extradata.inited ||
3809                  st->internal->extract_extradata.bsf) &&
3810                 extract_extradata_check(st))
3811                 break;
3812             if (st->first_dts == AV_NOPTS_VALUE &&
3813                 !(ic->iformat->flags & AVFMT_NOTIMESTAMPS) &&
3814                 st->codec_info_nb_frames < ((st->disposition & AV_DISPOSITION_ATTACHED_PIC) ? 1 : ic->max_ts_probe) &&
3815                 (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ||
3816                  st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO))
3817                 break;
3818         }
3819         analyzed_all_streams = 0;
3820         if (!missing_streams || !*missing_streams)
3821             if (i == ic->nb_streams) {
3822                 analyzed_all_streams = 1;
3823                 /* NOTE: If the format has no header, then we need to read some
3824                  * packets to get most of the streams, so we cannot stop here. */
3825                 if (!(ic->ctx_flags & AVFMTCTX_NOHEADER)) {
3826                     /* If we found the info for all the codecs, we can stop. */
3827                     ret = count;
3828                     av_log(ic, AV_LOG_DEBUG, "All info found\n");
3829                     flush_codecs = 0;
3830                     break;
3831                 }
3832             }
3833         /* We did not get all the codec info, but we read too much data. */
3834         if (read_size >= probesize) {
3835             ret = count;
3836             av_log(ic, AV_LOG_DEBUG,
3837                    "Probe buffer size limit of %"PRId64" bytes reached\n", probesize);
3838             for (i = 0; i < ic->nb_streams; i++)
3839                 if (!ic->streams[i]->r_frame_rate.num &&
3840                     ic->streams[i]->internal->info->duration_count <= 1 &&
3841                     ic->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
3842                     strcmp(ic->iformat->name, "image2"))
3843                     av_log(ic, AV_LOG_WARNING,
3844                            "Stream #%d: not enough frames to estimate rate; "
3845                            "consider increasing probesize\n", i);
3846             break;
3847         }
3848 
3849         /* NOTE: A new stream can be added there if no header in file
3850          * (AVFMTCTX_NOHEADER). */
3851         ret = read_frame_internal(ic, pkt1);
3852         if (ret == AVERROR(EAGAIN))
3853             continue;
3854 
3855         if (ret < 0) {
3856             /* EOF or error*/
3857             eof_reached = 1;
3858             break;
3859         }
3860 
3861         if (!(ic->flags & AVFMT_FLAG_NOBUFFER)) {
3862             ret = avpriv_packet_list_put(&ic->internal->packet_buffer,
3863                                      &ic->internal->packet_buffer_end,
3864                                      pkt1, NULL, 0);
3865             if (ret < 0)
3866                 goto unref_then_goto_end;
3867 
3868             pkt = &ic->internal->packet_buffer_end->pkt;
3869         } else {
3870             pkt = pkt1;
3871         }
3872 
3873         st = ic->streams[pkt->stream_index];
3874         if (!(st->disposition & AV_DISPOSITION_ATTACHED_PIC))
3875             read_size += pkt->size;
3876 
3877         avctx = st->internal->avctx;
3878         if (!st->internal->avctx_inited) {
3879             ret = avcodec_parameters_to_context(avctx, st->codecpar);
3880             if (ret < 0)
3881                 goto unref_then_goto_end;
3882             st->internal->avctx_inited = 1;
3883         }
3884 
3885         if (pkt->dts != AV_NOPTS_VALUE && st->codec_info_nb_frames > 1) {
3886             /* check for non-increasing dts */
3887             if (st->internal->info->fps_last_dts != AV_NOPTS_VALUE &&
3888                 st->internal->info->fps_last_dts >= pkt->dts) {
3889                 av_log(ic, AV_LOG_DEBUG,
3890                        "Non-increasing DTS in stream %d: packet %d with DTS "
3891                        "%"PRId64", packet %d with DTS %"PRId64"\n",
3892                        st->index, st->internal->info->fps_last_dts_idx,
3893                        st->internal->info->fps_last_dts, st->codec_info_nb_frames,
3894                        pkt->dts);
3895                 st->internal->info->fps_first_dts =
3896                 st->internal->info->fps_last_dts  = AV_NOPTS_VALUE;
3897             }
3898             /* Check for a discontinuity in dts. If the difference in dts
3899              * is more than 1000 times the average packet duration in the
3900              * sequence, we treat it as a discontinuity. */
3901             if (st->internal->info->fps_last_dts != AV_NOPTS_VALUE &&
3902                 st->internal->info->fps_last_dts_idx > st->internal->info->fps_first_dts_idx &&
3903                 (pkt->dts - (uint64_t)st->internal->info->fps_last_dts) / 1000 >
3904                 (st->internal->info->fps_last_dts     - (uint64_t)st->internal->info->fps_first_dts) /
3905                 (st->internal->info->fps_last_dts_idx - st->internal->info->fps_first_dts_idx)) {
3906                 av_log(ic, AV_LOG_WARNING,
3907                        "DTS discontinuity in stream %d: packet %d with DTS "
3908                        "%"PRId64", packet %d with DTS %"PRId64"\n",
3909                        st->index, st->internal->info->fps_last_dts_idx,
3910                        st->internal->info->fps_last_dts, st->codec_info_nb_frames,
3911                        pkt->dts);
3912                 st->internal->info->fps_first_dts =
3913                 st->internal->info->fps_last_dts  = AV_NOPTS_VALUE;
3914             }
3915 
3916             /* update stored dts values */
3917             if (st->internal->info->fps_first_dts == AV_NOPTS_VALUE) {
3918                 st->internal->info->fps_first_dts     = pkt->dts;
3919                 st->internal->info->fps_first_dts_idx = st->codec_info_nb_frames;
3920             }
3921             st->internal->info->fps_last_dts     = pkt->dts;
3922             st->internal->info->fps_last_dts_idx = st->codec_info_nb_frames;
3923         }
3924         if (st->codec_info_nb_frames>1) {
3925             int64_t t = 0;
3926             int64_t limit;
3927 
3928             if (st->time_base.den > 0)
3929                 t = av_rescale_q(st->internal->info->codec_info_duration, st->time_base, AV_TIME_BASE_Q);
3930             if (st->avg_frame_rate.num > 0)
3931                 t = FFMAX(t, av_rescale_q(st->codec_info_nb_frames, av_inv_q(st->avg_frame_rate), AV_TIME_BASE_Q));
3932 
3933             if (   t == 0
3934                 && st->codec_info_nb_frames>30
3935                 && st->internal->info->fps_first_dts != AV_NOPTS_VALUE
3936                 && st->internal->info->fps_last_dts  != AV_NOPTS_VALUE) {
3937                 int64_t dur = av_sat_sub64(st->internal->info->fps_last_dts, st->internal->info->fps_first_dts);
3938                 t = FFMAX(t, av_rescale_q(dur, st->time_base, AV_TIME_BASE_Q));
3939             }
3940 
3941             if (analyzed_all_streams)                                limit = max_analyze_duration;
3942             else if (avctx->codec_type == AVMEDIA_TYPE_SUBTITLE) limit = max_subtitle_analyze_duration;
3943             else                                                     limit = max_stream_analyze_duration;
3944 
3945             if (t >= limit) {
3946                 av_log(ic, AV_LOG_VERBOSE, "max_analyze_duration %"PRId64" reached at %"PRId64" microseconds st:%d\n",
3947                        limit,
3948                        t, pkt->stream_index);
3949                 if (ic->flags & AVFMT_FLAG_NOBUFFER)
3950                     av_packet_unref(pkt1);
3951                 break;
3952             }
3953             if (pkt->duration > 0) {
3954                 if (avctx->codec_type == AVMEDIA_TYPE_SUBTITLE && pkt->pts != AV_NOPTS_VALUE && st->start_time != AV_NOPTS_VALUE && pkt->pts >= st->start_time
3955                     && (uint64_t)pkt->pts - st->start_time < INT64_MAX
3956                 ) {
3957                     st->internal->info->codec_info_duration = FFMIN(pkt->pts - st->start_time, st->internal->info->codec_info_duration + pkt->duration);
3958                 } else
3959                     st->internal->info->codec_info_duration += pkt->duration;
3960                 st->internal->info->codec_info_duration_fields += st->parser && st->need_parsing && avctx->ticks_per_frame ==2 ? st->parser->repeat_pict + 1 : 2;
3961             }
3962         }
3963         if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
3964 #if FF_API_R_FRAME_RATE
3965             ff_rfps_add_frame(ic, st, pkt->dts);
3966 #endif
3967             if (pkt->dts != pkt->pts && pkt->dts != AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE)
3968                 st->internal->info->frame_delay_evidence = 1;
3969         }
3970         if (!st->internal->avctx->extradata) {
3971             ret = extract_extradata(st, pkt);
3972             if (ret < 0)
3973                 goto unref_then_goto_end;
3974         }
3975 
3976         /* If still no information, we try to open the codec and to
3977          * decompress the frame. We try to avoid that in most cases as
3978          * it takes longer and uses more memory. For MPEG-4, we need to
3979          * decompress for QuickTime.
3980          *
3981          * If AV_CODEC_CAP_CHANNEL_CONF is set this will force decoding of at
3982          * least one frame of codec data, this makes sure the codec initializes
3983          * the channel configuration and does not only trust the values from
3984          * the container. */
3985         try_decode_frame(ic, st, pkt,
3986                          (options && i < orig_nb_streams) ? &options[i] : NULL);
3987 
3988         if (ic->flags & AVFMT_FLAG_NOBUFFER)
3989             av_packet_unref(pkt1);
3990 
3991         st->codec_info_nb_frames++;
3992         count++;
3993     }
3994 
3995     if (eof_reached) {
3996         int stream_index;
3997         for (stream_index = 0; stream_index < ic->nb_streams; stream_index++) {
3998             st = ic->streams[stream_index];
3999             avctx = st->internal->avctx;
4000             if (!has_codec_parameters(st, NULL)) {
4001                 const AVCodec *codec = find_probe_decoder(ic, st, st->codecpar->codec_id);
4002                 if (codec && !avctx->codec) {
4003                     AVDictionary *opts = NULL;
4004                     if (ic->codec_whitelist)
4005                         av_dict_set(&opts, "codec_whitelist", ic->codec_whitelist, 0);
4006                     if (avcodec_open2(avctx, codec, (options && stream_index < orig_nb_streams) ? &options[stream_index] : &opts) < 0)
4007                         av_log(ic, AV_LOG_WARNING,
4008                                "Failed to open codec in %s\n",__FUNCTION__);
4009                     av_dict_free(&opts);
4010                 }
4011             }
4012 
4013             // EOF already reached while reading the stream above.
4014             // So continue with reoordering DTS with whatever delay we have.
4015             if (ic->internal->packet_buffer && !has_decode_delay_been_guessed(st)) {
4016                 update_dts_from_pts(ic, stream_index, ic->internal->packet_buffer);
4017             }
4018         }
4019     }
4020 
4021     if (flush_codecs) {
4022         AVPacket *empty_pkt = ic->internal->pkt;
4023         int err = 0;
4024         av_packet_unref(empty_pkt);
4025 
4026         for (i = 0; i < ic->nb_streams; i++) {
4027 
4028             st = ic->streams[i];
4029 
4030             /* flush the decoders */
4031             if (st->internal->info->found_decoder == 1) {
4032                 do {
4033                     err = try_decode_frame(ic, st, empty_pkt,
4034                                             (options && i < orig_nb_streams)
4035                                             ? &options[i] : NULL);
4036                 } while (err > 0 && !has_codec_parameters(st, NULL));
4037 
4038                 if (err < 0) {
4039                     av_log(ic, AV_LOG_INFO,
4040                         "decoding for stream %d failed\n", st->index);
4041                 }
4042             }
4043         }
4044     }
4045 
4046     ff_rfps_calculate(ic);
4047 
4048     for (i = 0; i < ic->nb_streams; i++) {
4049         st = ic->streams[i];
4050         avctx = st->internal->avctx;
4051         if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
4052             if (avctx->codec_id == AV_CODEC_ID_RAWVIDEO && !avctx->codec_tag && !avctx->bits_per_coded_sample) {
4053                 uint32_t tag= avcodec_pix_fmt_to_codec_tag(avctx->pix_fmt);
4054                 if (avpriv_find_pix_fmt(avpriv_get_raw_pix_fmt_tags(), tag) == avctx->pix_fmt)
4055                     avctx->codec_tag= tag;
4056             }
4057 
4058             /* estimate average framerate if not set by demuxer */
4059             if (st->internal->info->codec_info_duration_fields &&
4060                 !st->avg_frame_rate.num &&
4061                 st->internal->info->codec_info_duration) {
4062                 int best_fps      = 0;
4063                 double best_error = 0.01;
4064                 AVRational codec_frame_rate = avctx->framerate;
4065 
4066                 if (st->internal->info->codec_info_duration        >= INT64_MAX / st->time_base.num / 2||
4067                     st->internal->info->codec_info_duration_fields >= INT64_MAX / st->time_base.den ||
4068                     st->internal->info->codec_info_duration        < 0)
4069                     continue;
4070                 av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den,
4071                           st->internal->info->codec_info_duration_fields * (int64_t) st->time_base.den,
4072                           st->internal->info->codec_info_duration * 2 * (int64_t) st->time_base.num, 60000);
4073 
4074                 /* Round guessed framerate to a "standard" framerate if it's
4075                  * within 1% of the original estimate. */
4076                 for (j = 0; j < MAX_STD_TIMEBASES; j++) {
4077                     AVRational std_fps = { get_std_framerate(j), 12 * 1001 };
4078                     double error       = fabs(av_q2d(st->avg_frame_rate) /
4079                                               av_q2d(std_fps) - 1);
4080 
4081                     if (error < best_error) {
4082                         best_error = error;
4083                         best_fps   = std_fps.num;
4084                     }
4085 
4086                     if (ic->internal->prefer_codec_framerate && codec_frame_rate.num > 0 && codec_frame_rate.den > 0) {
4087                         error       = fabs(av_q2d(codec_frame_rate) /
4088                                            av_q2d(std_fps) - 1);
4089                         if (error < best_error) {
4090                             best_error = error;
4091                             best_fps   = std_fps.num;
4092                         }
4093                     }
4094                 }
4095                 if (best_fps)
4096                     av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den,
4097                               best_fps, 12 * 1001, INT_MAX);
4098             }
4099 
4100             if (!st->r_frame_rate.num) {
4101                 if (    avctx->time_base.den * (int64_t) st->time_base.num
4102                     <= avctx->time_base.num * (uint64_t)avctx->ticks_per_frame * st->time_base.den) {
4103                     av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den,
4104                               avctx->time_base.den, (int64_t)avctx->time_base.num * avctx->ticks_per_frame, INT_MAX);
4105                 } else {
4106                     st->r_frame_rate.num = st->time_base.den;
4107                     st->r_frame_rate.den = st->time_base.num;
4108                 }
4109             }
4110             if (st->internal->display_aspect_ratio.num && st->internal->display_aspect_ratio.den) {
4111                 AVRational hw_ratio = { avctx->height, avctx->width };
4112                 st->sample_aspect_ratio = av_mul_q(st->internal->display_aspect_ratio,
4113                                                    hw_ratio);
4114             }
4115         } else if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
4116             if (!avctx->bits_per_coded_sample)
4117                 avctx->bits_per_coded_sample =
4118                     av_get_bits_per_sample(avctx->codec_id);
4119             // set stream disposition based on audio service type
4120             switch (avctx->audio_service_type) {
4121             case AV_AUDIO_SERVICE_TYPE_EFFECTS:
4122                 st->disposition = AV_DISPOSITION_CLEAN_EFFECTS;
4123                 break;
4124             case AV_AUDIO_SERVICE_TYPE_VISUALLY_IMPAIRED:
4125                 st->disposition = AV_DISPOSITION_VISUAL_IMPAIRED;
4126                 break;
4127             case AV_AUDIO_SERVICE_TYPE_HEARING_IMPAIRED:
4128                 st->disposition = AV_DISPOSITION_HEARING_IMPAIRED;
4129                 break;
4130             case AV_AUDIO_SERVICE_TYPE_COMMENTARY:
4131                 st->disposition = AV_DISPOSITION_COMMENT;
4132                 break;
4133             case AV_AUDIO_SERVICE_TYPE_KARAOKE:
4134                 st->disposition = AV_DISPOSITION_KARAOKE;
4135                 break;
4136             }
4137         }
4138     }
4139 
4140     if (probesize)
4141         estimate_timings(ic, old_offset);
4142 
4143     av_opt_set(ic, "skip_clear", "0", AV_OPT_SEARCH_CHILDREN);
4144 
4145     if (ret >= 0 && ic->nb_streams)
4146         /* We could not have all the codec parameters before EOF. */
4147         ret = -1;
4148     for (i = 0; i < ic->nb_streams; i++) {
4149         const char *errmsg;
4150         st = ic->streams[i];
4151 
4152         /* if no packet was ever seen, update context now for has_codec_parameters */
4153         if (!st->internal->avctx_inited) {
4154             if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
4155                 st->codecpar->format == AV_SAMPLE_FMT_NONE)
4156                 st->codecpar->format = st->internal->avctx->sample_fmt;
4157             ret = avcodec_parameters_to_context(st->internal->avctx, st->codecpar);
4158             if (ret < 0)
4159                 goto find_stream_info_err;
4160         }
4161         if (!has_codec_parameters(st, &errmsg)) {
4162             char buf[256];
4163             avcodec_string(buf, sizeof(buf), st->internal->avctx, 0);
4164             av_log(ic, AV_LOG_WARNING,
4165                    "Could not find codec parameters for stream %d (%s): %s\n"
4166                    "Consider increasing the value for the 'analyzeduration' (%"PRId64") and 'probesize' (%"PRId64") options\n",
4167                    i, buf, errmsg, ic->max_analyze_duration, ic->probesize);
4168         } else {
4169             ret = 0;
4170         }
4171     }
4172 
4173     ret = compute_chapters_end(ic);
4174     if (ret < 0)
4175         goto find_stream_info_err;
4176 
4177     /* update the stream parameters from the internal codec contexts */
4178     for (i = 0; i < ic->nb_streams; i++) {
4179         st = ic->streams[i];
4180 
4181         if (st->internal->avctx_inited) {
4182             ret = avcodec_parameters_from_context(st->codecpar, st->internal->avctx);
4183             if (ret < 0)
4184                 goto find_stream_info_err;
4185             ret = add_coded_side_data(st, st->internal->avctx);
4186             if (ret < 0)
4187                 goto find_stream_info_err;
4188         }
4189 
4190 #if FF_API_LAVF_AVCTX
4191 FF_DISABLE_DEPRECATION_WARNINGS
4192         ret = avcodec_parameters_to_context(st->codec, st->codecpar);
4193         if (ret < 0)
4194             goto find_stream_info_err;
4195 
4196         // The old API (AVStream.codec) "requires" the resolution to be adjusted
4197         // by the lowres factor.
4198         if (st->internal->avctx->lowres && st->internal->avctx->width) {
4199             st->codec->lowres = st->internal->avctx->lowres;
4200             st->codec->width = st->internal->avctx->width;
4201             st->codec->height = st->internal->avctx->height;
4202         }
4203 
4204         if (st->codec->codec_tag != MKTAG('t','m','c','d')) {
4205             st->codec->time_base = st->internal->avctx->time_base;
4206             st->codec->ticks_per_frame = st->internal->avctx->ticks_per_frame;
4207         }
4208         st->codec->framerate = st->avg_frame_rate;
4209 
4210         if (st->internal->avctx->subtitle_header) {
4211             st->codec->subtitle_header = av_malloc(st->internal->avctx->subtitle_header_size);
4212             if (!st->codec->subtitle_header)
4213                 goto find_stream_info_err;
4214             st->codec->subtitle_header_size = st->internal->avctx->subtitle_header_size;
4215             memcpy(st->codec->subtitle_header, st->internal->avctx->subtitle_header,
4216                    st->codec->subtitle_header_size);
4217         }
4218 
4219         // Fields unavailable in AVCodecParameters
4220         st->codec->coded_width = st->internal->avctx->coded_width;
4221         st->codec->coded_height = st->internal->avctx->coded_height;
4222         st->codec->properties = st->internal->avctx->properties;
4223 FF_ENABLE_DEPRECATION_WARNINGS
4224 #endif
4225 
4226         st->internal->avctx_inited = 0;
4227     }
4228 
4229 find_stream_info_err:
4230     for (i = 0; i < ic->nb_streams; i++) {
4231         st = ic->streams[i];
4232         if (st->internal->info)
4233             av_freep(&st->internal->info->duration_error);
4234         avcodec_close(ic->streams[i]->internal->avctx);
4235         av_freep(&ic->streams[i]->internal->info);
4236         av_bsf_free(&ic->streams[i]->internal->extract_extradata.bsf);
4237         av_packet_free(&ic->streams[i]->internal->extract_extradata.pkt);
4238     }
4239     if (ic->pb)
4240         av_log(ic, AV_LOG_DEBUG, "After avformat_find_stream_info() pos: %"PRId64" bytes read:%"PRId64" seeks:%d frames:%d\n",
4241                avio_tell(ic->pb), ic->pb->bytes_read, ic->pb->seek_count, count);
4242     return ret;
4243 
4244 unref_then_goto_end:
4245     av_packet_unref(pkt1);
4246     goto find_stream_info_err;
4247 }
4248 
4249 AVProgram *av_find_program_from_stream(AVFormatContext *ic, AVProgram *last, int s)
4250 {
4251     int i, j;
4252 
4253     for (i = 0; i < ic->nb_programs; i++) {
4254         if (ic->programs[i] == last) {
4255             last = NULL;
4256         } else {
4257             if (!last)
4258                 for (j = 0; j < ic->programs[i]->nb_stream_indexes; j++)
4259                     if (ic->programs[i]->stream_index[j] == s)
4260                         return ic->programs[i];
4261         }
4262     }
4263     return NULL;
4264 }
4265 
4266 int av_find_best_stream(AVFormatContext *ic, enum AVMediaType type,
4267                         int wanted_stream_nb, int related_stream,
4268                         AVCodec **decoder_ret, int flags)
4269 {
4270     int i, nb_streams = ic->nb_streams;
4271     int ret = AVERROR_STREAM_NOT_FOUND;
4272     int best_count = -1, best_multiframe = -1, best_disposition = -1;
4273     int count, multiframe, disposition;
4274     int64_t best_bitrate = -1;
4275     int64_t bitrate;
4276     unsigned *program = NULL;
4277     const AVCodec *decoder = NULL, *best_decoder = NULL;
4278 
4279     if (related_stream >= 0 && wanted_stream_nb < 0) {
4280         AVProgram *p = av_find_program_from_stream(ic, NULL, related_stream);
4281         if (p) {
4282             program    = p->stream_index;
4283             nb_streams = p->nb_stream_indexes;
4284         }
4285     }
4286     for (i = 0; i < nb_streams; i++) {
4287         int real_stream_index = program ? program[i] : i;
4288         AVStream *st          = ic->streams[real_stream_index];
4289         AVCodecParameters *par = st->codecpar;
4290         if (par->codec_type != type)
4291             continue;
4292         if (wanted_stream_nb >= 0 && real_stream_index != wanted_stream_nb)
4293             continue;
4294         if (type == AVMEDIA_TYPE_AUDIO && !(par->channels && par->sample_rate))
4295             continue;
4296         if (decoder_ret) {
4297             decoder = find_decoder(ic, st, par->codec_id);
4298             if (!decoder) {
4299                 if (ret < 0)
4300                     ret = AVERROR_DECODER_NOT_FOUND;
4301                 continue;
4302             }
4303         }
4304         disposition = !(st->disposition & (AV_DISPOSITION_HEARING_IMPAIRED | AV_DISPOSITION_VISUAL_IMPAIRED))
4305                       + !! (st->disposition & AV_DISPOSITION_DEFAULT);
4306         count = st->codec_info_nb_frames;
4307         bitrate = par->bit_rate;
4308         multiframe = FFMIN(5, count);
4309         if ((best_disposition >  disposition) ||
4310             (best_disposition == disposition && best_multiframe >  multiframe) ||
4311             (best_disposition == disposition && best_multiframe == multiframe && best_bitrate >  bitrate) ||
4312             (best_disposition == disposition && best_multiframe == multiframe && best_bitrate == bitrate && best_count >= count))
4313             continue;
4314         best_disposition = disposition;
4315         best_count   = count;
4316         best_bitrate = bitrate;
4317         best_multiframe = multiframe;
4318         ret          = real_stream_index;
4319         best_decoder = decoder;
4320         if (program && i == nb_streams - 1 && ret < 0) {
4321             program    = NULL;
4322             nb_streams = ic->nb_streams;
4323             /* no related stream found, try again with everything */
4324             i = 0;
4325         }
4326     }
4327     if (decoder_ret)
4328         *decoder_ret = (AVCodec*)best_decoder;
4329     return ret;
4330 }
4331 
4332 /*******************************************************/
4333 
4334 int av_read_play(AVFormatContext *s)
4335 {
4336     if (s->iformat->read_play)
4337         return s->iformat->read_play(s);
4338     if (s->pb)
4339         return avio_pause(s->pb, 0);
4340     return AVERROR(ENOSYS);
4341 }
4342 
4343 int av_read_pause(AVFormatContext *s)
4344 {
4345     if (s->iformat->read_pause)
4346         return s->iformat->read_pause(s);
4347     if (s->pb)
4348         return avio_pause(s->pb, 1);
4349     return AVERROR(ENOSYS);
4350 }
4351 
4352 int ff_stream_encode_params_copy(AVStream *dst, const AVStream *src)
4353 {
4354     int ret, i;
4355 
4356     dst->id                  = src->id;
4357     dst->time_base           = src->time_base;
4358     dst->nb_frames           = src->nb_frames;
4359     dst->disposition         = src->disposition;
4360     dst->sample_aspect_ratio = src->sample_aspect_ratio;
4361     dst->avg_frame_rate      = src->avg_frame_rate;
4362     dst->r_frame_rate        = src->r_frame_rate;
4363 
4364     av_dict_free(&dst->metadata);
4365     ret = av_dict_copy(&dst->metadata, src->metadata, 0);
4366     if (ret < 0)
4367         return ret;
4368 
4369     ret = avcodec_parameters_copy(dst->codecpar, src->codecpar);
4370     if (ret < 0)
4371         return ret;
4372 
4373     /* Free existing side data*/
4374     for (i = 0; i < dst->nb_side_data; i++)
4375         av_free(dst->side_data[i].data);
4376     av_freep(&dst->side_data);
4377     dst->nb_side_data = 0;
4378 
4379     /* Copy side data if present */
4380     if (src->nb_side_data) {
4381         dst->side_data = av_mallocz_array(src->nb_side_data,
4382                                           sizeof(AVPacketSideData));
4383         if (!dst->side_data)
4384             return AVERROR(ENOMEM);
4385         dst->nb_side_data = src->nb_side_data;
4386 
4387         for (i = 0; i < src->nb_side_data; i++) {
4388             uint8_t *data = av_memdup(src->side_data[i].data,
4389                                       src->side_data[i].size);
4390             if (!data)
4391                 return AVERROR(ENOMEM);
4392             dst->side_data[i].type = src->side_data[i].type;
4393             dst->side_data[i].size = src->side_data[i].size;
4394             dst->side_data[i].data = data;
4395         }
4396     }
4397 
4398 #if FF_API_LAVF_FFSERVER
4399 FF_DISABLE_DEPRECATION_WARNINGS
4400     av_freep(&dst->recommended_encoder_configuration);
4401     if (src->recommended_encoder_configuration) {
4402         const char *conf_str = src->recommended_encoder_configuration;
4403         dst->recommended_encoder_configuration = av_strdup(conf_str);
4404         if (!dst->recommended_encoder_configuration)
4405             return AVERROR(ENOMEM);
4406     }
4407 FF_ENABLE_DEPRECATION_WARNINGS
4408 #endif
4409 
4410     return 0;
4411 }
4412 
4413 static void free_stream(AVStream **pst)
4414 {
4415     AVStream *st = *pst;
4416     int i;
4417 
4418     if (!st)
4419         return;
4420 
4421     for (i = 0; i < st->nb_side_data; i++)
4422         av_freep(&st->side_data[i].data);
4423     av_freep(&st->side_data);
4424 
4425     if (st->parser)
4426         av_parser_close(st->parser);
4427 
4428     if (st->attached_pic.data)
4429         av_packet_unref(&st->attached_pic);
4430 
4431     if (st->internal) {
4432         avcodec_free_context(&st->internal->avctx);
4433         av_bsf_free(&st->internal->bsfc);
4434         av_freep(&st->internal->priv_pts);
4435         av_freep(&st->index_entries);
4436         av_freep(&st->internal->probe_data.buf);
4437 
4438         av_bsf_free(&st->internal->extract_extradata.bsf);
4439         av_packet_free(&st->internal->extract_extradata.pkt);
4440 
4441         if (st->internal->info)
4442             av_freep(&st->internal->info->duration_error);
4443         av_freep(&st->internal->info);
4444     }
4445     av_freep(&st->internal);
4446 
4447     av_dict_free(&st->metadata);
4448     avcodec_parameters_free(&st->codecpar);
4449 #if FF_API_LAVF_AVCTX
4450 FF_DISABLE_DEPRECATION_WARNINGS
4451     avcodec_free_context(&st->codec);
4452 FF_ENABLE_DEPRECATION_WARNINGS
4453 #endif
4454     av_freep(&st->priv_data);
4455 #if FF_API_LAVF_FFSERVER
4456 FF_DISABLE_DEPRECATION_WARNINGS
4457     av_freep(&st->recommended_encoder_configuration);
4458 FF_ENABLE_DEPRECATION_WARNINGS
4459 #endif
4460 
4461     av_freep(pst);
4462 }
4463 
4464 void ff_free_stream(AVFormatContext *s, AVStream *st)
4465 {
4466     av_assert0(s->nb_streams>0);
4467     av_assert0(s->streams[ s->nb_streams - 1 ] == st);
4468 
4469     free_stream(&s->streams[ --s->nb_streams ]);
4470 }
4471 
4472 void avformat_free_context(AVFormatContext *s)
4473 {
4474     int i;
4475 
4476     if (!s)
4477         return;
4478 
4479     if (s->oformat && s->oformat->deinit && s->internal->initialized)
4480         s->oformat->deinit(s);
4481 
4482     av_opt_free(s);
4483     if (s->iformat && s->iformat->priv_class && s->priv_data)
4484         av_opt_free(s->priv_data);
4485     if (s->oformat && s->oformat->priv_class && s->priv_data)
4486         av_opt_free(s->priv_data);
4487 
4488     for (i = 0; i < s->nb_streams; i++)
4489         free_stream(&s->streams[i]);
4490     s->nb_streams = 0;
4491 
4492     for (i = 0; i < s->nb_programs; i++) {
4493         av_dict_free(&s->programs[i]->metadata);
4494         av_freep(&s->programs[i]->stream_index);
4495         av_freep(&s->programs[i]);
4496     }
4497     s->nb_programs = 0;
4498 
4499     av_freep(&s->programs);
4500     av_freep(&s->priv_data);
4501     while (s->nb_chapters--) {
4502         av_dict_free(&s->chapters[s->nb_chapters]->metadata);
4503         av_freep(&s->chapters[s->nb_chapters]);
4504     }
4505     av_freep(&s->chapters);
4506     av_dict_free(&s->metadata);
4507     av_dict_free(&s->internal->id3v2_meta);
4508     av_packet_free(&s->internal->pkt);
4509     av_packet_free(&s->internal->parse_pkt);
4510     av_freep(&s->streams);
4511     flush_packet_queue(s);
4512     av_freep(&s->internal);
4513     av_freep(&s->url);
4514     av_free(s);
4515 }
4516 
4517 void avformat_close_input(AVFormatContext **ps)
4518 {
4519     AVFormatContext *s;
4520     AVIOContext *pb;
4521 
4522     if (!ps || !*ps)
4523         return;
4524 
4525     s  = *ps;
4526     pb = s->pb;
4527 
4528     if ((s->iformat && strcmp(s->iformat->name, "image2") && s->iformat->flags & AVFMT_NOFILE) ||
4529         (s->flags & AVFMT_FLAG_CUSTOM_IO))
4530         pb = NULL;
4531 
4532     flush_packet_queue(s);
4533 
4534     if (s->iformat)
4535         if (s->iformat->read_close)
4536             s->iformat->read_close(s);
4537 
4538     avformat_free_context(s);
4539 
4540     *ps = NULL;
4541 
4542     avio_close(pb);
4543 }
4544 
4545 AVStream *avformat_new_stream(AVFormatContext *s, const AVCodec *c)
4546 {
4547     AVStream *st;
4548     int i;
4549     AVStream **streams;
4550 
4551     if (s->nb_streams >= FFMIN(s->max_streams, INT_MAX/sizeof(*streams))) {
4552         if (s->max_streams < INT_MAX/sizeof(*streams))
4553             av_log(s, AV_LOG_ERROR, "Number of streams exceeds max_streams parameter (%d), see the documentation if you wish to increase it\n", s->max_streams);
4554         return NULL;
4555     }
4556     streams = av_realloc_array(s->streams, s->nb_streams + 1, sizeof(*streams));
4557     if (!streams)
4558         return NULL;
4559     s->streams = streams;
4560 
4561     st = av_mallocz(sizeof(AVStream));
4562     if (!st)
4563         return NULL;
4564 
4565 #if FF_API_LAVF_AVCTX
4566 FF_DISABLE_DEPRECATION_WARNINGS
4567     st->codec = avcodec_alloc_context3(c);
4568     if (!st->codec) {
4569         av_free(st);
4570         return NULL;
4571     }
4572 FF_ENABLE_DEPRECATION_WARNINGS
4573 #endif
4574 
4575     st->internal = av_mallocz(sizeof(*st->internal));
4576     if (!st->internal)
4577         goto fail;
4578 
4579     st->internal->info = av_mallocz(sizeof(*st->internal->info));
4580     if (!st->internal->info)
4581         goto fail;
4582     st->internal->info->last_dts = AV_NOPTS_VALUE;
4583 
4584     st->codecpar = avcodec_parameters_alloc();
4585     if (!st->codecpar)
4586         goto fail;
4587 
4588     st->internal->avctx = avcodec_alloc_context3(NULL);
4589     if (!st->internal->avctx)
4590         goto fail;
4591 
4592     if (s->iformat) {
4593 #if FF_API_LAVF_AVCTX
4594 FF_DISABLE_DEPRECATION_WARNINGS
4595         /* no default bitrate if decoding */
4596         st->codec->bit_rate = 0;
4597 FF_ENABLE_DEPRECATION_WARNINGS
4598 #endif
4599 
4600         /* default pts setting is MPEG-like */
4601         avpriv_set_pts_info(st, 33, 1, 90000);
4602         /* we set the current DTS to 0 so that formats without any timestamps
4603          * but durations get some timestamps, formats with some unknown
4604          * timestamps have their first few packets buffered and the
4605          * timestamps corrected before they are returned to the user */
4606         st->cur_dts = RELATIVE_TS_BASE;
4607     } else {
4608         st->cur_dts = AV_NOPTS_VALUE;
4609     }
4610 
4611     st->index      = s->nb_streams;
4612     st->start_time = AV_NOPTS_VALUE;
4613     st->duration   = AV_NOPTS_VALUE;
4614     st->first_dts     = AV_NOPTS_VALUE;
4615     st->probe_packets = s->max_probe_packets;
4616     st->internal->pts_wrap_reference = AV_NOPTS_VALUE;
4617     st->internal->pts_wrap_behavior = AV_PTS_WRAP_IGNORE;
4618 
4619     st->last_IP_pts = AV_NOPTS_VALUE;
4620     st->internal->last_dts_for_order_check = AV_NOPTS_VALUE;
4621     for (i = 0; i < MAX_REORDER_DELAY + 1; i++)
4622         st->internal->pts_buffer[i] = AV_NOPTS_VALUE;
4623 
4624     st->sample_aspect_ratio = (AVRational) { 0, 1 };
4625 
4626 #if FF_API_R_FRAME_RATE
4627     st->internal->info->last_dts      = AV_NOPTS_VALUE;
4628 #endif
4629     st->internal->info->fps_first_dts = AV_NOPTS_VALUE;
4630     st->internal->info->fps_last_dts  = AV_NOPTS_VALUE;
4631 
4632     st->internal->inject_global_side_data = s->internal->inject_global_side_data;
4633 
4634     st->internal->need_context_update = 1;
4635 
4636     s->streams[s->nb_streams++] = st;
4637     return st;
4638 fail:
4639     free_stream(&st);
4640     return NULL;
4641 }
4642 
4643 AVProgram *av_new_program(AVFormatContext *ac, int id)
4644 {
4645     AVProgram *program = NULL;
4646     int i, ret;
4647 
4648     av_log(ac, AV_LOG_TRACE, "new_program: id=0x%04x\n", id);
4649 
4650     for (i = 0; i < ac->nb_programs; i++)
4651         if (ac->programs[i]->id == id)
4652             program = ac->programs[i];
4653 
4654     if (!program) {
4655         program = av_mallocz(sizeof(AVProgram));
4656         if (!program)
4657             return NULL;
4658         ret = av_dynarray_add_nofree(&ac->programs, &ac->nb_programs, program);
4659         if (ret < 0) {
4660             av_free(program);
4661             return NULL;
4662         }
4663         program->discard = AVDISCARD_NONE;
4664         program->pmt_version = -1;
4665         program->id = id;
4666         program->pts_wrap_reference = AV_NOPTS_VALUE;
4667         program->pts_wrap_behavior = AV_PTS_WRAP_IGNORE;
4668         program->start_time =
4669         program->end_time   = AV_NOPTS_VALUE;
4670     }
4671     return program;
4672 }
4673 
4674 #if FF_API_CHAPTER_ID_INT
4675 AVChapter *avpriv_new_chapter(AVFormatContext *s, int id, AVRational time_base,
4676 #else
4677 AVChapter *avpriv_new_chapter(AVFormatContext *s, int64_t id, AVRational time_base,
4678 #endif
4679                               int64_t start, int64_t end, const char *title)
4680 {
4681     AVChapter *chapter = NULL;
4682     int i, ret;
4683 
4684     if (end != AV_NOPTS_VALUE && start > end) {
4685         av_log(s, AV_LOG_ERROR, "Chapter end time %"PRId64" before start %"PRId64"\n", end, start);
4686         return NULL;
4687     }
4688 
4689     if (!s->nb_chapters) {
4690         s->internal->chapter_ids_monotonic = 1;
4691     } else if (!s->internal->chapter_ids_monotonic || s->chapters[s->nb_chapters-1]->id >= id) {
4692         s->internal->chapter_ids_monotonic = 0;
4693         for (i = 0; i < s->nb_chapters; i++)
4694             if (s->chapters[i]->id == id)
4695                 chapter = s->chapters[i];
4696     }
4697 
4698     if (!chapter) {
4699         chapter = av_mallocz(sizeof(AVChapter));
4700         if (!chapter)
4701             return NULL;
4702         ret = av_dynarray_add_nofree(&s->chapters, &s->nb_chapters, chapter);
4703         if (ret < 0) {
4704             av_free(chapter);
4705             return NULL;
4706         }
4707     }
4708     av_dict_set(&chapter->metadata, "title", title, 0);
4709     chapter->id        = id;
4710     chapter->time_base = time_base;
4711     chapter->start     = start;
4712     chapter->end       = end;
4713 
4714     return chapter;
4715 }
4716 
4717 void av_program_add_stream_index(AVFormatContext *ac, int progid, unsigned idx)
4718 {
4719     int i, j;
4720     AVProgram *program = NULL;
4721     void *tmp;
4722 
4723     if (idx >= ac->nb_streams) {
4724         av_log(ac, AV_LOG_ERROR, "stream index %d is not valid\n", idx);
4725         return;
4726     }
4727 
4728     for (i = 0; i < ac->nb_programs; i++) {
4729         if (ac->programs[i]->id != progid)
4730             continue;
4731         program = ac->programs[i];
4732         for (j = 0; j < program->nb_stream_indexes; j++)
4733             if (program->stream_index[j] == idx)
4734                 return;
4735 
4736         tmp = av_realloc_array(program->stream_index, program->nb_stream_indexes+1, sizeof(unsigned int));
4737         if (!tmp)
4738             return;
4739         program->stream_index = tmp;
4740         program->stream_index[program->nb_stream_indexes++] = idx;
4741         return;
4742     }
4743 }
4744 
4745 uint64_t ff_ntp_time(void)
4746 {
4747     return (av_gettime() / 1000) * 1000 + NTP_OFFSET_US;
4748 }
4749 
4750 uint64_t ff_get_formatted_ntp_time(uint64_t ntp_time_us)
4751 {
4752     uint64_t ntp_ts, frac_part, sec;
4753     uint32_t usec;
4754 
4755     //current ntp time in seconds and micro seconds
4756     sec = ntp_time_us / 1000000;
4757     usec = ntp_time_us % 1000000;
4758 
4759     //encoding in ntp timestamp format
4760     frac_part = usec * 0xFFFFFFFFULL;
4761     frac_part /= 1000000;
4762 
4763     if (sec > 0xFFFFFFFFULL)
4764         av_log(NULL, AV_LOG_WARNING, "NTP time format roll over detected\n");
4765 
4766     ntp_ts = sec << 32;
4767     ntp_ts |= frac_part;
4768 
4769     return ntp_ts;
4770 }
4771 
4772 int av_get_frame_filename2(char *buf, int buf_size, const char *path, int number, int flags)
4773 {
4774     const char *p;
4775     char *q, buf1[20], c;
4776     int nd, len, percentd_found;
4777 
4778     q = buf;
4779     p = path;
4780     percentd_found = 0;
4781     for (;;) {
4782         c = *p++;
4783         if (c == '\0')
4784             break;
4785         if (c == '%') {
4786             do {
4787                 nd = 0;
4788                 while (av_isdigit(*p)) {
4789                     if (nd >= INT_MAX / 10 - 255)
4790                         goto fail;
4791                     nd = nd * 10 + *p++ - '0';
4792                 }
4793                 c = *p++;
4794             } while (av_isdigit(c));
4795 
4796             switch (c) {
4797             case '%':
4798                 goto addchar;
4799             case 'd':
4800                 if (!(flags & AV_FRAME_FILENAME_FLAGS_MULTIPLE) && percentd_found)
4801                     goto fail;
4802                 percentd_found = 1;
4803                 if (number < 0)
4804                     nd += 1;
4805                 snprintf(buf1, sizeof(buf1), "%0*d", nd, number);
4806                 len = strlen(buf1);
4807                 if ((q - buf + len) > buf_size - 1)
4808                     goto fail;
4809                 memcpy(q, buf1, len);
4810                 q += len;
4811                 break;
4812             default:
4813                 goto fail;
4814             }
4815         } else {
4816 addchar:
4817             if ((q - buf) < buf_size - 1)
4818                 *q++ = c;
4819         }
4820     }
4821     if (!percentd_found)
4822         goto fail;
4823     *q = '\0';
4824     return 0;
4825 fail:
4826     *q = '\0';
4827     return -1;
4828 }
4829 
4830 int av_get_frame_filename(char *buf, int buf_size, const char *path, int number)
4831 {
4832     return av_get_frame_filename2(buf, buf_size, path, number, 0);
4833 }
4834 
4835 void av_url_split(char *proto, int proto_size,
4836                   char *authorization, int authorization_size,
4837                   char *hostname, int hostname_size,
4838                   int *port_ptr, char *path, int path_size, const char *url)
4839 {
4840     const char *p, *ls, *at, *at2, *col, *brk;
4841 
4842     if (port_ptr)
4843         *port_ptr = -1;
4844     if (proto_size > 0)
4845         proto[0] = 0;
4846     if (authorization_size > 0)
4847         authorization[0] = 0;
4848     if (hostname_size > 0)
4849         hostname[0] = 0;
4850     if (path_size > 0)
4851         path[0] = 0;
4852 
4853     /* parse protocol */
4854     if ((p = strchr(url, ':'))) {
4855         av_strlcpy(proto, url, FFMIN(proto_size, p + 1 - url));
4856         p++; /* skip ':' */
4857         if (*p == '/')
4858             p++;
4859         if (*p == '/')
4860             p++;
4861     } else {
4862         /* no protocol means plain filename */
4863         av_strlcpy(path, url, path_size);
4864         return;
4865     }
4866 
4867     /* separate path from hostname */
4868     ls = p + strcspn(p, "/?#");
4869     av_strlcpy(path, ls, path_size);
4870 
4871     /* the rest is hostname, use that to parse auth/port */
4872     if (ls != p) {
4873         /* authorization (user[:pass]@hostname) */
4874         at2 = p;
4875         while ((at = strchr(p, '@')) && at < ls) {
4876             av_strlcpy(authorization, at2,
4877                        FFMIN(authorization_size, at + 1 - at2));
4878             p = at + 1; /* skip '@' */
4879         }
4880 
4881         if (*p == '[' && (brk = strchr(p, ']')) && brk < ls) {
4882             /* [host]:port */
4883             av_strlcpy(hostname, p + 1,
4884                        FFMIN(hostname_size, brk - p));
4885             if (brk[1] == ':' && port_ptr)
4886                 *port_ptr = atoi(brk + 2);
4887         } else if ((col = strchr(p, ':')) && col < ls) {
4888             av_strlcpy(hostname, p,
4889                        FFMIN(col + 1 - p, hostname_size));
4890             if (port_ptr)
4891                 *port_ptr = atoi(col + 1);
4892         } else
4893             av_strlcpy(hostname, p,
4894                        FFMIN(ls + 1 - p, hostname_size));
4895     }
4896 }
4897 
4898 int ff_mkdir_p(const char *path)
4899 {
4900     int ret = 0;
4901     char *temp = av_strdup(path);
4902     char *pos = temp;
4903     char tmp_ch = '\0';
4904 
4905     if (!path || !temp) {
4906         return -1;
4907     }
4908 
4909     if (!av_strncasecmp(temp, "/", 1) || !av_strncasecmp(temp, "\\", 1)) {
4910         pos++;
4911     } else if (!av_strncasecmp(temp, "./", 2) || !av_strncasecmp(temp, ".\\", 2)) {
4912         pos += 2;
4913     }
4914 
4915     for ( ; *pos != '\0'; ++pos) {
4916         if (*pos == '/' || *pos == '\\') {
4917             tmp_ch = *pos;
4918             *pos = '\0';
4919             ret = mkdir(temp, 0755);
4920             *pos = tmp_ch;
4921         }
4922     }
4923 
4924     if ((*(pos - 1) != '/') || (*(pos - 1) != '\\')) {
4925         ret = mkdir(temp, 0755);
4926     }
4927 
4928     av_free(temp);
4929     return ret;
4930 }
4931 
4932 char *ff_data_to_hex(char *buff, const uint8_t *src, int s, int lowercase)
4933 {
4934     int i;
4935     static const char hex_table_uc[16] = { '0', '1', '2', '3',
4936                                            '4', '5', '6', '7',
4937                                            '8', '9', 'A', 'B',
4938                                            'C', 'D', 'E', 'F' };
4939     static const char hex_table_lc[16] = { '0', '1', '2', '3',
4940                                            '4', '5', '6', '7',
4941                                            '8', '9', 'a', 'b',
4942                                            'c', 'd', 'e', 'f' };
4943     const char *hex_table = lowercase ? hex_table_lc : hex_table_uc;
4944 
4945     for (i = 0; i < s; i++) {
4946         buff[i * 2]     = hex_table[src[i] >> 4];
4947         buff[i * 2 + 1] = hex_table[src[i] & 0xF];
4948     }
4949 
4950     return buff;
4951 }
4952 
4953 int ff_hex_to_data(uint8_t *data, const char *p)
4954 {
4955     int c, len, v;
4956 
4957     len = 0;
4958     v   = 1;
4959     for (;;) {
4960         p += strspn(p, SPACE_CHARS);
4961         if (*p == '\0')
4962             break;
4963         c = av_toupper((unsigned char) *p++);
4964         if (c >= '0' && c <= '9')
4965             c = c - '0';
4966         else if (c >= 'A' && c <= 'F')
4967             c = c - 'A' + 10;
4968         else
4969             break;
4970         v = (v << 4) | c;
4971         if (v & 0x100) {
4972             if (data)
4973                 data[len] = v;
4974             len++;
4975             v = 1;
4976         }
4977     }
4978     return len;
4979 }
4980 
4981 void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits,
4982                          unsigned int pts_num, unsigned int pts_den)
4983 {
4984     AVRational new_tb;
4985     if (av_reduce(&new_tb.num, &new_tb.den, pts_num, pts_den, INT_MAX)) {
4986         if (new_tb.num != pts_num)
4987             av_log(NULL, AV_LOG_DEBUG,
4988                    "st:%d removing common factor %d from timebase\n",
4989                    s->index, pts_num / new_tb.num);
4990     } else
4991         av_log(NULL, AV_LOG_WARNING,
4992                "st:%d has too large timebase, reducing\n", s->index);
4993 
4994     if (new_tb.num <= 0 || new_tb.den <= 0) {
4995         av_log(NULL, AV_LOG_ERROR,
4996                "Ignoring attempt to set invalid timebase %d/%d for st:%d\n",
4997                new_tb.num, new_tb.den,
4998                s->index);
4999         return;
5000     }
5001     s->time_base     = new_tb;
5002 #if FF_API_LAVF_AVCTX
5003 FF_DISABLE_DEPRECATION_WARNINGS
5004     s->codec->pkt_timebase = new_tb;
5005 FF_ENABLE_DEPRECATION_WARNINGS
5006 #endif
5007     s->internal->avctx->pkt_timebase = new_tb;
5008     s->pts_wrap_bits = pts_wrap_bits;
5009 }
5010 
5011 void ff_parse_key_value(const char *str, ff_parse_key_val_cb callback_get_buf,
5012                         void *context)
5013 {
5014     const char *ptr = str;
5015 
5016     /* Parse key=value pairs. */
5017     for (;;) {
5018         const char *key;
5019         char *dest = NULL, *dest_end;
5020         int key_len, dest_len = 0;
5021 
5022         /* Skip whitespace and potential commas. */
5023         while (*ptr && (av_isspace(*ptr) || *ptr == ','))
5024             ptr++;
5025         if (!*ptr)
5026             break;
5027 
5028         key = ptr;
5029 
5030         if (!(ptr = strchr(key, '=')))
5031             break;
5032         ptr++;
5033         key_len = ptr - key;
5034 
5035         callback_get_buf(context, key, key_len, &dest, &dest_len);
5036         dest_end = dest + dest_len - 1;
5037 
5038         if (*ptr == '\"') {
5039             ptr++;
5040             while (*ptr && *ptr != '\"') {
5041                 if (*ptr == '\\') {
5042                     if (!ptr[1])
5043                         break;
5044                     if (dest && dest < dest_end)
5045                         *dest++ = ptr[1];
5046                     ptr += 2;
5047                 } else {
5048                     if (dest && dest < dest_end)
5049                         *dest++ = *ptr;
5050                     ptr++;
5051                 }
5052             }
5053             if (*ptr == '\"')
5054                 ptr++;
5055         } else {
5056             for (; *ptr && !(av_isspace(*ptr) || *ptr == ','); ptr++)
5057                 if (dest && dest < dest_end)
5058                     *dest++ = *ptr;
5059         }
5060         if (dest)
5061             *dest = 0;
5062     }
5063 }
5064 
5065 int ff_find_stream_index(AVFormatContext *s, int id)
5066 {
5067     int i;
5068     for (i = 0; i < s->nb_streams; i++)
5069         if (s->streams[i]->id == id)
5070             return i;
5071     return -1;
5072 }
5073 
5074 int avformat_query_codec(const AVOutputFormat *ofmt, enum AVCodecID codec_id,
5075                          int std_compliance)
5076 {
5077     if (ofmt) {
5078         unsigned int codec_tag;
5079         if (ofmt->query_codec)
5080             return ofmt->query_codec(codec_id, std_compliance);
5081         else if (ofmt->codec_tag)
5082             return !!av_codec_get_tag2(ofmt->codec_tag, codec_id, &codec_tag);
5083         else if (codec_id == ofmt->video_codec ||
5084                  codec_id == ofmt->audio_codec ||
5085                  codec_id == ofmt->subtitle_codec ||
5086                  codec_id == ofmt->data_codec)
5087             return 1;
5088     }
5089     return AVERROR_PATCHWELCOME;
5090 }
5091 
5092 int avformat_network_init(void)
5093 {
5094 #if CONFIG_NETWORK
5095     int ret;
5096     if ((ret = ff_network_init()) < 0)
5097         return ret;
5098     if ((ret = ff_tls_init()) < 0)
5099         return ret;
5100 #endif
5101     return 0;
5102 }
5103 
5104 int avformat_network_deinit(void)
5105 {
5106 #if CONFIG_NETWORK
5107     ff_network_close();
5108     ff_tls_deinit();
5109 #endif
5110     return 0;
5111 }
5112 
5113 int ff_add_param_change(AVPacket *pkt, int32_t channels,
5114                         uint64_t channel_layout, int32_t sample_rate,
5115                         int32_t width, int32_t height)
5116 {
5117     uint32_t flags = 0;
5118     int size = 4;
5119     uint8_t *data;
5120     if (!pkt)
5121         return AVERROR(EINVAL);
5122     if (channels) {
5123         size  += 4;
5124         flags |= AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT;
5125     }
5126     if (channel_layout) {
5127         size  += 8;
5128         flags |= AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT;
5129     }
5130     if (sample_rate) {
5131         size  += 4;
5132         flags |= AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE;
5133     }
5134     if (width || height) {
5135         size  += 8;
5136         flags |= AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS;
5137     }
5138     data = av_packet_new_side_data(pkt, AV_PKT_DATA_PARAM_CHANGE, size);
5139     if (!data)
5140         return AVERROR(ENOMEM);
5141     bytestream_put_le32(&data, flags);
5142     if (channels)
5143         bytestream_put_le32(&data, channels);
5144     if (channel_layout)
5145         bytestream_put_le64(&data, channel_layout);
5146     if (sample_rate)
5147         bytestream_put_le32(&data, sample_rate);
5148     if (width || height) {
5149         bytestream_put_le32(&data, width);
5150         bytestream_put_le32(&data, height);
5151     }
5152     return 0;
5153 }
5154 
5155 AVRational av_guess_sample_aspect_ratio(AVFormatContext *format, AVStream *stream, AVFrame *frame)
5156 {
5157     AVRational undef = {0, 1};
5158     AVRational stream_sample_aspect_ratio = stream ? stream->sample_aspect_ratio : undef;
5159     AVRational codec_sample_aspect_ratio  = stream && stream->codecpar ? stream->codecpar->sample_aspect_ratio : undef;
5160     AVRational frame_sample_aspect_ratio  = frame  ? frame->sample_aspect_ratio  : codec_sample_aspect_ratio;
5161 
5162     av_reduce(&stream_sample_aspect_ratio.num, &stream_sample_aspect_ratio.den,
5163                stream_sample_aspect_ratio.num,  stream_sample_aspect_ratio.den, INT_MAX);
5164     if (stream_sample_aspect_ratio.num <= 0 || stream_sample_aspect_ratio.den <= 0)
5165         stream_sample_aspect_ratio = undef;
5166 
5167     av_reduce(&frame_sample_aspect_ratio.num, &frame_sample_aspect_ratio.den,
5168                frame_sample_aspect_ratio.num,  frame_sample_aspect_ratio.den, INT_MAX);
5169     if (frame_sample_aspect_ratio.num <= 0 || frame_sample_aspect_ratio.den <= 0)
5170         frame_sample_aspect_ratio = undef;
5171 
5172     if (stream_sample_aspect_ratio.num)
5173         return stream_sample_aspect_ratio;
5174     else
5175         return frame_sample_aspect_ratio;
5176 }
5177 
5178 AVRational av_guess_frame_rate(AVFormatContext *format, AVStream *st, AVFrame *frame)
5179 {
5180     AVRational fr = st->r_frame_rate;
5181     AVRational codec_fr = st->internal->avctx->framerate;
5182     AVRational   avg_fr = st->avg_frame_rate;
5183 
5184     if (avg_fr.num > 0 && avg_fr.den > 0 && fr.num > 0 && fr.den > 0 &&
5185         av_q2d(avg_fr) < 70 && av_q2d(fr) > 210) {
5186         fr = avg_fr;
5187     }
5188 
5189 
5190     if (st->internal->avctx->ticks_per_frame > 1) {
5191         if (   codec_fr.num > 0 && codec_fr.den > 0 &&
5192             (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))
5193             fr = codec_fr;
5194     }
5195 
5196     return fr;
5197 }
5198 
5199 /**
5200  * Matches a stream specifier (but ignores requested index).
5201  *
5202  * @param indexptr set to point to the requested stream index if there is one
5203  *
5204  * @return <0 on error
5205  *         0  if st is NOT a matching stream
5206  *         >0 if st is a matching stream
5207  */
5208 static int match_stream_specifier(AVFormatContext *s, AVStream *st,
5209                                   const char *spec, const char **indexptr, AVProgram **p)
5210 {
5211     int match = 1;                      /* Stores if the specifier matches so far. */
5212     while (*spec) {
5213         if (*spec <= '9' && *spec >= '0') { /* opt:index */
5214             if (indexptr)
5215                 *indexptr = spec;
5216             return match;
5217         } else if (*spec == 'v' || *spec == 'a' || *spec == 's' || *spec == 'd' ||
5218                    *spec == 't' || *spec == 'V') { /* opt:[vasdtV] */
5219             enum AVMediaType type;
5220             int nopic = 0;
5221 
5222             switch (*spec++) {
5223             case 'v': type = AVMEDIA_TYPE_VIDEO;      break;
5224             case 'a': type = AVMEDIA_TYPE_AUDIO;      break;
5225             case 's': type = AVMEDIA_TYPE_SUBTITLE;   break;
5226             case 'd': type = AVMEDIA_TYPE_DATA;       break;
5227             case 't': type = AVMEDIA_TYPE_ATTACHMENT; break;
5228             case 'V': type = AVMEDIA_TYPE_VIDEO; nopic = 1; break;
5229             default:  av_assert0(0);
5230             }
5231             if (*spec && *spec++ != ':')         /* If we are not at the end, then another specifier must follow. */
5232                 return AVERROR(EINVAL);
5233 
5234 #if FF_API_LAVF_AVCTX
5235 FF_DISABLE_DEPRECATION_WARNINGS
5236             if (type != st->codecpar->codec_type
5237                && (st->codecpar->codec_type != AVMEDIA_TYPE_UNKNOWN || st->codec->codec_type != type))
5238                 match = 0;
5239     FF_ENABLE_DEPRECATION_WARNINGS
5240 #else
5241             if (type != st->codecpar->codec_type)
5242                 match = 0;
5243 #endif
5244             if (nopic && (st->disposition & AV_DISPOSITION_ATTACHED_PIC))
5245                 match = 0;
5246         } else if (*spec == 'p' && *(spec + 1) == ':') {
5247             int prog_id, i, j;
5248             int found = 0;
5249             char *endptr;
5250             spec += 2;
5251             prog_id = strtol(spec, &endptr, 0);
5252             /* Disallow empty id and make sure that if we are not at the end, then another specifier must follow. */
5253             if (spec == endptr || (*endptr && *endptr++ != ':'))
5254                 return AVERROR(EINVAL);
5255             spec = endptr;
5256             if (match) {
5257                 for (i = 0; i < s->nb_programs; i++) {
5258                     if (s->programs[i]->id != prog_id)
5259                         continue;
5260 
5261                     for (j = 0; j < s->programs[i]->nb_stream_indexes; j++) {
5262                         if (st->index == s->programs[i]->stream_index[j]) {
5263                             found = 1;
5264                             if (p)
5265                                 *p = s->programs[i];
5266                             i = s->nb_programs;
5267                             break;
5268                         }
5269                     }
5270                 }
5271             }
5272             if (!found)
5273                 match = 0;
5274         } else if (*spec == '#' ||
5275                    (*spec == 'i' && *(spec + 1) == ':')) {
5276             int stream_id;
5277             char *endptr;
5278             spec += 1 + (*spec == 'i');
5279             stream_id = strtol(spec, &endptr, 0);
5280             if (spec == endptr || *endptr)                /* Disallow empty id and make sure we are at the end. */
5281                 return AVERROR(EINVAL);
5282             return match && (stream_id == st->id);
5283         } else if (*spec == 'm' && *(spec + 1) == ':') {
5284             AVDictionaryEntry *tag;
5285             char *key, *val;
5286             int ret;
5287 
5288             if (match) {
5289                spec += 2;
5290                val = strchr(spec, ':');
5291 
5292                key = val ? av_strndup(spec, val - spec) : av_strdup(spec);
5293                if (!key)
5294                    return AVERROR(ENOMEM);
5295 
5296                tag = av_dict_get(st->metadata, key, NULL, 0);
5297                if (tag) {
5298                    if (!val || !strcmp(tag->value, val + 1))
5299                        ret = 1;
5300                    else
5301                        ret = 0;
5302                } else
5303                    ret = 0;
5304 
5305                av_freep(&key);
5306             }
5307             return match && ret;
5308         } else if (*spec == 'u' && *(spec + 1) == '\0') {
5309             AVCodecParameters *par = st->codecpar;
5310 #if FF_API_LAVF_AVCTX
5311 FF_DISABLE_DEPRECATION_WARNINGS
5312             AVCodecContext *codec = st->codec;
5313 FF_ENABLE_DEPRECATION_WARNINGS
5314 #endif
5315             int val;
5316             switch (par->codec_type) {
5317             case AVMEDIA_TYPE_AUDIO:
5318                 val = par->sample_rate && par->channels;
5319 #if FF_API_LAVF_AVCTX
5320                 val = val || (codec->sample_rate && codec->channels);
5321 #endif
5322                 if (par->format == AV_SAMPLE_FMT_NONE
5323 #if FF_API_LAVF_AVCTX
5324                     && codec->sample_fmt == AV_SAMPLE_FMT_NONE
5325 #endif
5326                     )
5327                     return 0;
5328                 break;
5329             case AVMEDIA_TYPE_VIDEO:
5330                 val = par->width && par->height;
5331 #if FF_API_LAVF_AVCTX
5332                 val = val || (codec->width && codec->height);
5333 #endif
5334                 if (par->format == AV_PIX_FMT_NONE
5335 #if FF_API_LAVF_AVCTX
5336                     && codec->pix_fmt == AV_PIX_FMT_NONE
5337 #endif
5338                     )
5339                     return 0;
5340                 break;
5341             case AVMEDIA_TYPE_UNKNOWN:
5342                 val = 0;
5343                 break;
5344             default:
5345                 val = 1;
5346                 break;
5347             }
5348 #if FF_API_LAVF_AVCTX
5349             return match && ((par->codec_id != AV_CODEC_ID_NONE || codec->codec_id != AV_CODEC_ID_NONE) && val != 0);
5350 #else
5351             return match && (par->codec_id != AV_CODEC_ID_NONE && val != 0);
5352 #endif
5353         } else {
5354             return AVERROR(EINVAL);
5355         }
5356     }
5357 
5358     return match;
5359 }
5360 
5361 
5362 int avformat_match_stream_specifier(AVFormatContext *s, AVStream *st,
5363                                     const char *spec)
5364 {
5365     int ret, index;
5366     char *endptr;
5367     const char *indexptr = NULL;
5368     AVProgram *p = NULL;
5369     int nb_streams;
5370 
5371     ret = match_stream_specifier(s, st, spec, &indexptr, &p);
5372     if (ret < 0)
5373         goto error;
5374 
5375     if (!indexptr)
5376         return ret;
5377 
5378     index = strtol(indexptr, &endptr, 0);
5379     if (*endptr) {                  /* We can't have anything after the requested index. */
5380         ret = AVERROR(EINVAL);
5381         goto error;
5382     }
5383 
5384     /* This is not really needed but saves us a loop for simple stream index specifiers. */
5385     if (spec == indexptr)
5386         return (index == st->index);
5387 
5388     /* If we requested a matching stream index, we have to ensure st is that. */
5389     nb_streams = p ? p->nb_stream_indexes : s->nb_streams;
5390     for (int i = 0; i < nb_streams && index >= 0; i++) {
5391         AVStream *candidate = p ? s->streams[p->stream_index[i]] : s->streams[i];
5392         ret = match_stream_specifier(s, candidate, spec, NULL, NULL);
5393         if (ret < 0)
5394             goto error;
5395         if (ret > 0 && index-- == 0 && st == candidate)
5396             return 1;
5397     }
5398     return 0;
5399 
5400 error:
5401     if (ret == AVERROR(EINVAL))
5402         av_log(s, AV_LOG_ERROR, "Invalid stream specifier: %s.\n", spec);
5403     return ret;
5404 }
5405 
5406 int ff_generate_avci_extradata(AVStream *st)
5407 {
5408     static const uint8_t avci100_1080p_extradata[] = {
5409         // SPS
5410         0x00, 0x00, 0x00, 0x01, 0x67, 0x7a, 0x10, 0x29,
5411         0xb6, 0xd4, 0x20, 0x22, 0x33, 0x19, 0xc6, 0x63,
5412         0x23, 0x21, 0x01, 0x11, 0x98, 0xce, 0x33, 0x19,
5413         0x18, 0x21, 0x02, 0x56, 0xb9, 0x3d, 0x7d, 0x7e,
5414         0x4f, 0xe3, 0x3f, 0x11, 0xf1, 0x9e, 0x08, 0xb8,
5415         0x8c, 0x54, 0x43, 0xc0, 0x78, 0x02, 0x27, 0xe2,
5416         0x70, 0x1e, 0x30, 0x10, 0x10, 0x14, 0x00, 0x00,
5417         0x03, 0x00, 0x04, 0x00, 0x00, 0x03, 0x00, 0xca,
5418         0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
5419         // PPS
5420         0x00, 0x00, 0x00, 0x01, 0x68, 0xce, 0x33, 0x48,
5421         0xd0
5422     };
5423     static const uint8_t avci100_1080i_extradata[] = {
5424         // SPS
5425         0x00, 0x00, 0x00, 0x01, 0x67, 0x7a, 0x10, 0x29,
5426         0xb6, 0xd4, 0x20, 0x22, 0x33, 0x19, 0xc6, 0x63,
5427         0x23, 0x21, 0x01, 0x11, 0x98, 0xce, 0x33, 0x19,
5428         0x18, 0x21, 0x03, 0x3a, 0x46, 0x65, 0x6a, 0x65,
5429         0x24, 0xad, 0xe9, 0x12, 0x32, 0x14, 0x1a, 0x26,
5430         0x34, 0xad, 0xa4, 0x41, 0x82, 0x23, 0x01, 0x50,
5431         0x2b, 0x1a, 0x24, 0x69, 0x48, 0x30, 0x40, 0x2e,
5432         0x11, 0x12, 0x08, 0xc6, 0x8c, 0x04, 0x41, 0x28,
5433         0x4c, 0x34, 0xf0, 0x1e, 0x01, 0x13, 0xf2, 0xe0,
5434         0x3c, 0x60, 0x20, 0x20, 0x28, 0x00, 0x00, 0x03,
5435         0x00, 0x08, 0x00, 0x00, 0x03, 0x01, 0x94, 0x20,
5436         // PPS
5437         0x00, 0x00, 0x00, 0x01, 0x68, 0xce, 0x33, 0x48,
5438         0xd0
5439     };
5440     static const uint8_t avci50_1080p_extradata[] = {
5441         // SPS
5442         0x00, 0x00, 0x00, 0x01, 0x67, 0x6e, 0x10, 0x28,
5443         0xa6, 0xd4, 0x20, 0x32, 0x33, 0x0c, 0x71, 0x18,
5444         0x88, 0x62, 0x10, 0x19, 0x19, 0x86, 0x38, 0x8c,
5445         0x44, 0x30, 0x21, 0x02, 0x56, 0x4e, 0x6f, 0x37,
5446         0xcd, 0xf9, 0xbf, 0x81, 0x6b, 0xf3, 0x7c, 0xde,
5447         0x6e, 0x6c, 0xd3, 0x3c, 0x05, 0xa0, 0x22, 0x7e,
5448         0x5f, 0xfc, 0x00, 0x0c, 0x00, 0x13, 0x8c, 0x04,
5449         0x04, 0x05, 0x00, 0x00, 0x03, 0x00, 0x01, 0x00,
5450         0x00, 0x03, 0x00, 0x32, 0x84, 0x00, 0x00, 0x00,
5451         // PPS
5452         0x00, 0x00, 0x00, 0x01, 0x68, 0xee, 0x31, 0x12,
5453         0x11
5454     };
5455     static const uint8_t avci50_1080i_extradata[] = {
5456         // SPS
5457         0x00, 0x00, 0x00, 0x01, 0x67, 0x6e, 0x10, 0x28,
5458         0xa6, 0xd4, 0x20, 0x32, 0x33, 0x0c, 0x71, 0x18,
5459         0x88, 0x62, 0x10, 0x19, 0x19, 0x86, 0x38, 0x8c,
5460         0x44, 0x30, 0x21, 0x02, 0x56, 0x4e, 0x6e, 0x61,
5461         0x87, 0x3e, 0x73, 0x4d, 0x98, 0x0c, 0x03, 0x06,
5462         0x9c, 0x0b, 0x73, 0xe6, 0xc0, 0xb5, 0x18, 0x63,
5463         0x0d, 0x39, 0xe0, 0x5b, 0x02, 0xd4, 0xc6, 0x19,
5464         0x1a, 0x79, 0x8c, 0x32, 0x34, 0x24, 0xf0, 0x16,
5465         0x81, 0x13, 0xf7, 0xff, 0x80, 0x02, 0x00, 0x01,
5466         0xf1, 0x80, 0x80, 0x80, 0xa0, 0x00, 0x00, 0x03,
5467         0x00, 0x20, 0x00, 0x00, 0x06, 0x50, 0x80, 0x00,
5468         // PPS
5469         0x00, 0x00, 0x00, 0x01, 0x68, 0xee, 0x31, 0x12,
5470         0x11
5471     };
5472     static const uint8_t avci100_720p_extradata[] = {
5473         // SPS
5474         0x00, 0x00, 0x00, 0x01, 0x67, 0x7a, 0x10, 0x29,
5475         0xb6, 0xd4, 0x20, 0x2a, 0x33, 0x1d, 0xc7, 0x62,
5476         0xa1, 0x08, 0x40, 0x54, 0x66, 0x3b, 0x8e, 0xc5,
5477         0x42, 0x02, 0x10, 0x25, 0x64, 0x2c, 0x89, 0xe8,
5478         0x85, 0xe4, 0x21, 0x4b, 0x90, 0x83, 0x06, 0x95,
5479         0xd1, 0x06, 0x46, 0x97, 0x20, 0xc8, 0xd7, 0x43,
5480         0x08, 0x11, 0xc2, 0x1e, 0x4c, 0x91, 0x0f, 0x01,
5481         0x40, 0x16, 0xec, 0x07, 0x8c, 0x04, 0x04, 0x05,
5482         0x00, 0x00, 0x03, 0x00, 0x01, 0x00, 0x00, 0x03,
5483         0x00, 0x64, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00,
5484         // PPS
5485         0x00, 0x00, 0x00, 0x01, 0x68, 0xce, 0x31, 0x12,
5486         0x11
5487     };
5488     static const uint8_t avci50_720p_extradata[] = {
5489         // SPS
5490         0x00, 0x00, 0x00, 0x01, 0x67, 0x6e, 0x10, 0x20,
5491         0xa6, 0xd4, 0x20, 0x32, 0x33, 0x0c, 0x71, 0x18,
5492         0x88, 0x62, 0x10, 0x19, 0x19, 0x86, 0x38, 0x8c,
5493         0x44, 0x30, 0x21, 0x02, 0x56, 0x4e, 0x6f, 0x37,
5494         0xcd, 0xf9, 0xbf, 0x81, 0x6b, 0xf3, 0x7c, 0xde,
5495         0x6e, 0x6c, 0xd3, 0x3c, 0x0f, 0x01, 0x6e, 0xff,
5496         0xc0, 0x00, 0xc0, 0x01, 0x38, 0xc0, 0x40, 0x40,
5497         0x50, 0x00, 0x00, 0x03, 0x00, 0x10, 0x00, 0x00,
5498         0x06, 0x48, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00,
5499         // PPS
5500         0x00, 0x00, 0x00, 0x01, 0x68, 0xee, 0x31, 0x12,
5501         0x11
5502     };
5503 
5504     const uint8_t *data = NULL;
5505     int ret, size       = 0;
5506 
5507     if (st->codecpar->width == 1920) {
5508         if (st->codecpar->field_order == AV_FIELD_PROGRESSIVE) {
5509             data = avci100_1080p_extradata;
5510             size = sizeof(avci100_1080p_extradata);
5511         } else {
5512             data = avci100_1080i_extradata;
5513             size = sizeof(avci100_1080i_extradata);
5514         }
5515     } else if (st->codecpar->width == 1440) {
5516         if (st->codecpar->field_order == AV_FIELD_PROGRESSIVE) {
5517             data = avci50_1080p_extradata;
5518             size = sizeof(avci50_1080p_extradata);
5519         } else {
5520             data = avci50_1080i_extradata;
5521             size = sizeof(avci50_1080i_extradata);
5522         }
5523     } else if (st->codecpar->width == 1280) {
5524         data = avci100_720p_extradata;
5525         size = sizeof(avci100_720p_extradata);
5526     } else if (st->codecpar->width == 960) {
5527         data = avci50_720p_extradata;
5528         size = sizeof(avci50_720p_extradata);
5529     }
5530 
5531     if (!size)
5532         return 0;
5533 
5534     if ((ret = ff_alloc_extradata(st->codecpar, size)) < 0)
5535         return ret;
5536     memcpy(st->codecpar->extradata, data, size);
5537 
5538     return 0;
5539 }
5540 
5541 uint8_t *av_stream_get_side_data(const AVStream *st,
5542                                  enum AVPacketSideDataType type, buffer_size_t *size)
5543 {
5544     int i;
5545 
5546     for (i = 0; i < st->nb_side_data; i++) {
5547         if (st->side_data[i].type == type) {
5548             if (size)
5549                 *size = st->side_data[i].size;
5550             return st->side_data[i].data;
5551         }
5552     }
5553     if (size)
5554         *size = 0;
5555     return NULL;
5556 }
5557 
5558 int av_stream_add_side_data(AVStream *st, enum AVPacketSideDataType type,
5559                             uint8_t *data, size_t size)
5560 {
5561     AVPacketSideData *sd, *tmp;
5562     int i;
5563 
5564     for (i = 0; i < st->nb_side_data; i++) {
5565         sd = &st->side_data[i];
5566 
5567         if (sd->type == type) {
5568             av_freep(&sd->data);
5569             sd->data = data;
5570             sd->size = size;
5571             return 0;
5572         }
5573     }
5574 
5575     if ((unsigned)st->nb_side_data + 1 >= INT_MAX / sizeof(*st->side_data))
5576         return AVERROR(ERANGE);
5577 
5578     tmp = av_realloc(st->side_data, (st->nb_side_data + 1) * sizeof(*tmp));
5579     if (!tmp) {
5580         return AVERROR(ENOMEM);
5581     }
5582 
5583     st->side_data = tmp;
5584     st->nb_side_data++;
5585 
5586     sd = &st->side_data[st->nb_side_data - 1];
5587     sd->type = type;
5588     sd->data = data;
5589     sd->size = size;
5590 
5591     return 0;
5592 }
5593 
5594 uint8_t *av_stream_new_side_data(AVStream *st, enum AVPacketSideDataType type,
5595                                  buffer_size_t size)
5596 {
5597     int ret;
5598     uint8_t *data = av_malloc(size);
5599 
5600     if (!data)
5601         return NULL;
5602 
5603     ret = av_stream_add_side_data(st, type, data, size);
5604     if (ret < 0) {
5605         av_freep(&data);
5606         return NULL;
5607     }
5608 
5609     return data;
5610 }
5611 
5612 int ff_stream_add_bitstream_filter(AVStream *st, const char *name, const char *args)
5613 {
5614     int ret;
5615     const AVBitStreamFilter *bsf;
5616     AVBSFContext *bsfc;
5617 
5618     av_assert0(!st->internal->bsfc);
5619 
5620     if (!(bsf = av_bsf_get_by_name(name))) {
5621         av_log(NULL, AV_LOG_ERROR, "Unknown bitstream filter '%s'\n", name);
5622         return AVERROR_BSF_NOT_FOUND;
5623     }
5624 
5625     if ((ret = av_bsf_alloc(bsf, &bsfc)) < 0)
5626         return ret;
5627 
5628     bsfc->time_base_in = st->time_base;
5629     if ((ret = avcodec_parameters_copy(bsfc->par_in, st->codecpar)) < 0) {
5630         av_bsf_free(&bsfc);
5631         return ret;
5632     }
5633 
5634     if (args && bsfc->filter->priv_class) {
5635         const AVOption *opt = av_opt_next(bsfc->priv_data, NULL);
5636         const char * shorthand[2] = {NULL};
5637 
5638         if (opt)
5639             shorthand[0] = opt->name;
5640 
5641         if ((ret = av_opt_set_from_string(bsfc->priv_data, args, shorthand, "=", ":")) < 0) {
5642             av_bsf_free(&bsfc);
5643             return ret;
5644         }
5645     }
5646 
5647     if ((ret = av_bsf_init(bsfc)) < 0) {
5648         av_bsf_free(&bsfc);
5649         return ret;
5650     }
5651 
5652     st->internal->bsfc = bsfc;
5653 
5654     av_log(NULL, AV_LOG_VERBOSE,
5655            "Automatically inserted bitstream filter '%s'; args='%s'\n",
5656            name, args ? args : "");
5657     return 1;
5658 }
5659 
5660 #if FF_API_OLD_BSF
5661 FF_DISABLE_DEPRECATION_WARNINGS
5662 int av_apply_bitstream_filters(AVCodecContext *codec, AVPacket *pkt,
5663                                AVBitStreamFilterContext *bsfc)
5664 {
5665     int ret = 0;
5666     while (bsfc) {
5667         AVPacket new_pkt = *pkt;
5668         int a = av_bitstream_filter_filter(bsfc, codec, NULL,
5669                                            &new_pkt.data, &new_pkt.size,
5670                                            pkt->data, pkt->size,
5671                                            pkt->flags & AV_PKT_FLAG_KEY);
5672         if (a == 0 && new_pkt.size == 0 && new_pkt.side_data_elems == 0) {
5673             av_packet_unref(pkt);
5674             memset(pkt, 0, sizeof(*pkt));
5675             return 0;
5676         }
5677         if(a == 0 && new_pkt.data != pkt->data) {
5678             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
5679             if (t) {
5680                 memcpy(t, new_pkt.data, new_pkt.size);
5681                 memset(t + new_pkt.size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
5682                 new_pkt.data = t;
5683                 new_pkt.buf = NULL;
5684                 a = 1;
5685             } else {
5686                 a = AVERROR(ENOMEM);
5687             }
5688         }
5689         if (a > 0) {
5690             new_pkt.buf = av_buffer_create(new_pkt.data, new_pkt.size,
5691                                            av_buffer_default_free, NULL, 0);
5692             if (new_pkt.buf) {
5693                 pkt->side_data = NULL;
5694                 pkt->side_data_elems = 0;
5695                 av_packet_unref(pkt);
5696             } else {
5697                 av_freep(&new_pkt.data);
5698                 a = AVERROR(ENOMEM);
5699             }
5700         }
5701         if (a < 0) {
5702             av_log(codec, AV_LOG_ERROR,
5703                    "Failed to open bitstream filter %s for stream %d with codec %s",
5704                    bsfc->filter->name, pkt->stream_index,
5705                    codec->codec ? codec->codec->name : "copy");
5706             ret = a;
5707             break;
5708         }
5709         *pkt = new_pkt;
5710 
5711         bsfc = bsfc->next;
5712     }
5713     return ret;
5714 }
5715 FF_ENABLE_DEPRECATION_WARNINGS
5716 #endif
5717 
5718 int ff_format_output_open(AVFormatContext *s, const char *url, AVDictionary **options)
5719 {
5720     if (!s->oformat)
5721         return AVERROR(EINVAL);
5722 
5723     if (!(s->oformat->flags & AVFMT_NOFILE))
5724         return s->io_open(s, &s->pb, url, AVIO_FLAG_WRITE, options);
5725     return 0;
5726 }
5727 
5728 void ff_format_io_close(AVFormatContext *s, AVIOContext **pb)
5729 {
5730     if (*pb)
5731         s->io_close(s, *pb);
5732     *pb = NULL;
5733 }
5734 
5735 int ff_is_http_proto(char *filename) {
5736     const char *proto = avio_find_protocol_name(filename);
5737     return proto ? (!av_strcasecmp(proto, "http") || !av_strcasecmp(proto, "https")) : 0;
5738 }
5739 
5740 int ff_parse_creation_time_metadata(AVFormatContext *s, int64_t *timestamp, int return_seconds)
5741 {
5742     AVDictionaryEntry *entry;
5743     int64_t parsed_timestamp;
5744     int ret;
5745     if ((entry = av_dict_get(s->metadata, "creation_time", NULL, 0))) {
5746         if ((ret = av_parse_time(&parsed_timestamp, entry->value, 0)) >= 0) {
5747             *timestamp = return_seconds ? parsed_timestamp / 1000000 : parsed_timestamp;
5748             return 1;
5749         } else {
5750             av_log(s, AV_LOG_WARNING, "Failed to parse creation_time %s\n", entry->value);
5751             return ret;
5752         }
5753     }
5754     return 0;
5755 }
5756 
5757 int ff_standardize_creation_time(AVFormatContext *s)
5758 {
5759     int64_t timestamp;
5760     int ret = ff_parse_creation_time_metadata(s, &timestamp, 0);
5761     if (ret == 1)
5762         return avpriv_dict_set_timestamp(&s->metadata, "creation_time", timestamp);
5763     return ret;
5764 }
5765 
5766 int ff_get_packet_palette(AVFormatContext *s, AVPacket *pkt, int ret, uint32_t *palette)
5767 {
5768     uint8_t *side_data;
5769     buffer_size_t size;
5770 
5771     side_data = av_packet_get_side_data(pkt, AV_PKT_DATA_PALETTE, &size);
5772     if (side_data) {
5773         if (size != AVPALETTE_SIZE) {
5774             av_log(s, AV_LOG_ERROR, "Invalid palette side data\n");
5775             return AVERROR_INVALIDDATA;
5776         }
5777         memcpy(palette, side_data, AVPALETTE_SIZE);
5778         return 1;
5779     }
5780 
5781     if (ret == CONTAINS_PAL) {
5782         int i;
5783         for (i = 0; i < AVPALETTE_COUNT; i++)
5784             palette[i] = AV_RL32(pkt->data + pkt->size - AVPALETTE_SIZE + i*4);
5785         return 1;
5786     }
5787 
5788     return 0;
5789 }
5790 
5791 int ff_bprint_to_codecpar_extradata(AVCodecParameters *par, struct AVBPrint *buf)
5792 {
5793     int ret;
5794     char *str;
5795 
5796     ret = av_bprint_finalize(buf, &str);
5797     if (ret < 0)
5798         return ret;
5799     if (!av_bprint_is_complete(buf)) {
5800         av_free(str);
5801         return AVERROR(ENOMEM);
5802     }
5803 
5804     par->extradata = str;
5805     /* Note: the string is NUL terminated (so extradata can be read as a
5806      * string), but the ending character is not accounted in the size (in
5807      * binary formats you are likely not supposed to mux that character). When
5808      * extradata is copied, it is also padded with AV_INPUT_BUFFER_PADDING_SIZE
5809      * zeros. */
5810     par->extradata_size = buf->len;
5811     return 0;
5812 }
5813 
5814 int avformat_transfer_internal_stream_timing_info(const AVOutputFormat *ofmt,
5815                                                   AVStream *ost, const AVStream *ist,
5816                                                   enum AVTimebaseSource copy_tb)
5817 {
5818     //TODO: use [io]st->internal->avctx
5819     const AVCodecContext *dec_ctx;
5820     AVCodecContext       *enc_ctx;
5821 
5822 #if FF_API_LAVF_AVCTX
5823 FF_DISABLE_DEPRECATION_WARNINGS
5824     dec_ctx = ist->codec;
5825     enc_ctx = ost->codec;
5826 FF_ENABLE_DEPRECATION_WARNINGS
5827 #else
5828     dec_ctx = ist->internal->avctx;
5829     enc_ctx = ost->internal->avctx;
5830 #endif
5831 
5832     enc_ctx->time_base = ist->time_base;
5833     /*
5834      * Avi is a special case here because it supports variable fps but
5835      * having the fps and timebase differe significantly adds quite some
5836      * overhead
5837      */
5838     if (!strcmp(ofmt->name, "avi")) {
5839 #if FF_API_R_FRAME_RATE
5840         if (copy_tb == AVFMT_TBCF_AUTO && ist->r_frame_rate.num
5841             && av_q2d(ist->r_frame_rate) >= av_q2d(ist->avg_frame_rate)
5842             && 0.5/av_q2d(ist->r_frame_rate) > av_q2d(ist->time_base)
5843             && 0.5/av_q2d(ist->r_frame_rate) > av_q2d(dec_ctx->time_base)
5844             && av_q2d(ist->time_base) < 1.0/500 && av_q2d(dec_ctx->time_base) < 1.0/500
5845             || copy_tb == AVFMT_TBCF_R_FRAMERATE) {
5846             enc_ctx->time_base.num = ist->r_frame_rate.den;
5847             enc_ctx->time_base.den = 2*ist->r_frame_rate.num;
5848             enc_ctx->ticks_per_frame = 2;
5849         } else
5850 #endif
5851             if (copy_tb == AVFMT_TBCF_AUTO && av_q2d(dec_ctx->time_base)*dec_ctx->ticks_per_frame > 2*av_q2d(ist->time_base)
5852                    && av_q2d(ist->time_base) < 1.0/500
5853                    || copy_tb == AVFMT_TBCF_DECODER) {
5854             enc_ctx->time_base = dec_ctx->time_base;
5855             enc_ctx->time_base.num *= dec_ctx->ticks_per_frame;
5856             enc_ctx->time_base.den *= 2;
5857             enc_ctx->ticks_per_frame = 2;
5858         }
5859     } else if (!(ofmt->flags & AVFMT_VARIABLE_FPS)
5860                && !av_match_name(ofmt->name, "mov,mp4,3gp,3g2,psp,ipod,ismv,f4v")) {
5861         if (copy_tb == AVFMT_TBCF_AUTO && dec_ctx->time_base.den
5862             && av_q2d(dec_ctx->time_base)*dec_ctx->ticks_per_frame > av_q2d(ist->time_base)
5863             && av_q2d(ist->time_base) < 1.0/500
5864             || copy_tb == AVFMT_TBCF_DECODER) {
5865             enc_ctx->time_base = dec_ctx->time_base;
5866             enc_ctx->time_base.num *= dec_ctx->ticks_per_frame;
5867         }
5868     }
5869 
5870     if ((enc_ctx->codec_tag == AV_RL32("tmcd") || ost->codecpar->codec_tag == AV_RL32("tmcd"))
5871         && dec_ctx->time_base.num < dec_ctx->time_base.den
5872         && dec_ctx->time_base.num > 0
5873         && 121LL*dec_ctx->time_base.num > dec_ctx->time_base.den) {
5874         enc_ctx->time_base = dec_ctx->time_base;
5875     }
5876 
5877     if (ost->avg_frame_rate.num)
5878         enc_ctx->time_base = av_inv_q(ost->avg_frame_rate);
5879 
5880     av_reduce(&enc_ctx->time_base.num, &enc_ctx->time_base.den,
5881               enc_ctx->time_base.num, enc_ctx->time_base.den, INT_MAX);
5882 
5883     return 0;
5884 }
5885 
5886 AVRational av_stream_get_codec_timebase(const AVStream *st)
5887 {
5888     // See avformat_transfer_internal_stream_timing_info() TODO.
5889 #if FF_API_LAVF_AVCTX
5890 FF_DISABLE_DEPRECATION_WARNINGS
5891     return st->codec->time_base;
5892 FF_ENABLE_DEPRECATION_WARNINGS
5893 #else
5894     return st->internal->avctx->time_base;
5895 #endif
5896 }
5897 
5898 void ff_format_set_url(AVFormatContext *s, char *url)
5899 {
5900     av_assert0(url);
5901     av_freep(&s->url);
5902     s->url = url;
5903 #if FF_API_FORMAT_FILENAME
5904 FF_DISABLE_DEPRECATION_WARNINGS
5905     av_strlcpy(s->filename, url, sizeof(s->filename));
5906 FF_ENABLE_DEPRECATION_WARNINGS
5907 #endif
5908 }
5909