]> git.sesse.net Git - ffmpeg/blob - libavformat/utils.c
crypto: consistently use size_t as type for length parameters
[ffmpeg] / libavformat / utils.c
1 /*
2  * various utility functions for use within Libav
3  * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 #undef NDEBUG
23 #include <assert.h>
24 #include <stdarg.h>
25 #include <stdint.h>
26
27 #include "config.h"
28
29 #include "libavutil/avassert.h"
30 #include "libavutil/avstring.h"
31 #include "libavutil/dict.h"
32 #include "libavutil/internal.h"
33 #include "libavutil/mathematics.h"
34 #include "libavutil/opt.h"
35 #include "libavutil/parseutils.h"
36 #include "libavutil/pixdesc.h"
37 #include "libavutil/time.h"
38
39 #include "libavcodec/bytestream.h"
40 #include "libavcodec/internal.h"
41
42 #include "audiointerleave.h"
43 #include "avformat.h"
44 #include "id3v2.h"
45 #include "internal.h"
46 #include "metadata.h"
47 #if CONFIG_NETWORK
48 #include "network.h"
49 #endif
50 #include "riff.h"
51 #include "url.h"
52
53 /**
54  * @file
55  * various utility functions for use within Libav
56  */
57
58 unsigned avformat_version(void)
59 {
60     return LIBAVFORMAT_VERSION_INT;
61 }
62
63 const char *avformat_configuration(void)
64 {
65     return LIBAV_CONFIGURATION;
66 }
67
68 const char *avformat_license(void)
69 {
70 #define LICENSE_PREFIX "libavformat license: "
71     return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
72 }
73
74 /* an arbitrarily chosen "sane" max packet size -- 50M */
75 #define SANE_CHUNK_SIZE (50000000)
76
77 /* Read the data in sane-sized chunks and append to pkt.
78  * Return the number of bytes read or an error. */
79 static int append_packet_chunked(AVIOContext *s, AVPacket *pkt, int size)
80 {
81     int64_t chunk_size = size;
82     int64_t orig_pos   = pkt->pos; // av_grow_packet might reset pos
83     int orig_size      = pkt->size;
84     int ret = 0;
85
86     do {
87         int prev_size = pkt->size;
88         int read_size;
89
90         /* When the caller requests a lot of data, limit it to the amount
91          * left in file or SANE_CHUNK_SIZE when it is not known. */
92         if (size > SANE_CHUNK_SIZE) {
93             int64_t filesize = avio_size(s) - avio_tell(s);
94             chunk_size = FFMAX(filesize, SANE_CHUNK_SIZE);
95         }
96         read_size = FFMIN(size, chunk_size);
97
98         ret = av_grow_packet(pkt, read_size);
99         if (ret < 0)
100             break;
101
102         ret = avio_read(s, pkt->data + prev_size, read_size);
103         if (ret != read_size) {
104             av_shrink_packet(pkt, prev_size + FFMAX(ret, 0));
105             break;
106         }
107
108         size -= read_size;
109     } while (size > 0);
110
111     pkt->pos = orig_pos;
112     if (!pkt->size)
113         av_packet_unref(pkt);
114     return pkt->size > orig_size ? pkt->size - orig_size : ret;
115 }
116
117 int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
118 {
119     av_init_packet(pkt);
120     pkt->data = NULL;
121     pkt->size = 0;
122     pkt->pos  = avio_tell(s);
123
124     return append_packet_chunked(s, pkt, size);
125 }
126
127 int av_append_packet(AVIOContext *s, AVPacket *pkt, int size)
128 {
129     if (!pkt->size)
130         return av_get_packet(s, pkt, size);
131     return append_packet_chunked(s, pkt, size);
132 }
133
134 int av_filename_number_test(const char *filename)
135 {
136     char buf[1024];
137     return filename &&
138            (av_get_frame_filename(buf, sizeof(buf), filename, 1) >= 0);
139 }
140
141 static int set_codec_from_probe_data(AVFormatContext *s, AVStream *st,
142                                      AVProbeData *pd, int score)
143 {
144     static const struct {
145         const char *name;
146         enum AVCodecID id;
147         enum AVMediaType type;
148     } fmt_id_type[] = {
149         { "aac",       AV_CODEC_ID_AAC,        AVMEDIA_TYPE_AUDIO },
150         { "ac3",       AV_CODEC_ID_AC3,        AVMEDIA_TYPE_AUDIO },
151         { "dts",       AV_CODEC_ID_DTS,        AVMEDIA_TYPE_AUDIO },
152         { "eac3",      AV_CODEC_ID_EAC3,       AVMEDIA_TYPE_AUDIO },
153         { "h264",      AV_CODEC_ID_H264,       AVMEDIA_TYPE_VIDEO },
154         { "latm",      AV_CODEC_ID_AAC_LATM,   AVMEDIA_TYPE_AUDIO },
155         { "m4v",       AV_CODEC_ID_MPEG4,      AVMEDIA_TYPE_VIDEO },
156         { "mp3",       AV_CODEC_ID_MP3,        AVMEDIA_TYPE_AUDIO },
157         { "mpegvideo", AV_CODEC_ID_MPEG2VIDEO, AVMEDIA_TYPE_VIDEO },
158         { 0 }
159     };
160     AVInputFormat *fmt = av_probe_input_format2(pd, 1, &score);
161
162     if (fmt) {
163         int i;
164         av_log(s, AV_LOG_DEBUG,
165                "Probe with size=%d, packets=%d detected %s with score=%d\n",
166                pd->buf_size, MAX_PROBE_PACKETS - st->probe_packets,
167                fmt->name, score);
168         for (i = 0; fmt_id_type[i].name; i++) {
169             if (!strcmp(fmt->name, fmt_id_type[i].name)) {
170                 st->codecpar->codec_id   = fmt_id_type[i].id;
171                 st->codecpar->codec_type = fmt_id_type[i].type;
172 #if FF_API_LAVF_AVCTX
173 FF_DISABLE_DEPRECATION_WARNINGS
174                 st->codec->codec_type = st->codecpar->codec_type;
175                 st->codec->codec_id   = st->codecpar->codec_id;
176 FF_ENABLE_DEPRECATION_WARNINGS
177 #endif
178                 break;
179             }
180         }
181     }
182     return !!fmt;
183 }
184
185 /************************************************************/
186 /* input media file */
187
188 /* Open input file and probe the format if necessary. */
189 static int init_input(AVFormatContext *s, const char *filename,
190                       AVDictionary **options)
191 {
192     int ret;
193     AVProbeData pd = { filename, NULL, 0 };
194
195     if (s->pb) {
196         s->flags |= AVFMT_FLAG_CUSTOM_IO;
197         if (!s->iformat)
198             return av_probe_input_buffer(s->pb, &s->iformat, filename,
199                                          s, 0, s->probesize);
200         else if (s->iformat->flags & AVFMT_NOFILE)
201             return AVERROR(EINVAL);
202         return 0;
203     }
204
205     if ((s->iformat && s->iformat->flags & AVFMT_NOFILE) ||
206         (!s->iformat && (s->iformat = av_probe_input_format(&pd, 0))))
207         return 0;
208
209     ret = s->io_open(s, &s->pb, filename, AVIO_FLAG_READ, options);
210     if (ret < 0)
211         return ret;
212     if (s->iformat)
213         return 0;
214     return av_probe_input_buffer(s->pb, &s->iformat, filename,
215                                  s, 0, s->probesize);
216 }
217
218 static int add_to_pktbuf(AVPacketList **packet_buffer, AVPacket *pkt,
219                          AVPacketList **plast_pktl, int ref)
220 {
221     AVPacketList *pktl = av_mallocz(sizeof(AVPacketList));
222     int ret;
223
224     if (!pktl)
225         return AVERROR(ENOMEM);
226
227     if (ref) {
228         if ((ret = av_packet_ref(&pktl->pkt, pkt)) < 0) {
229             av_free(pktl);
230             return ret;
231         }
232     } else {
233         pktl->pkt = *pkt;
234     }
235
236     if (*packet_buffer)
237         (*plast_pktl)->next = pktl;
238     else
239         *packet_buffer = pktl;
240
241     /* Add the packet in the buffered packet list. */
242     *plast_pktl = pktl;
243     return 0;
244 }
245
246 static int queue_attached_pictures(AVFormatContext *s)
247 {
248     int i, ret;
249     for (i = 0; i < s->nb_streams; i++)
250         if (s->streams[i]->disposition & AV_DISPOSITION_ATTACHED_PIC &&
251             s->streams[i]->discard < AVDISCARD_ALL) {
252
253             ret = add_to_pktbuf(&s->internal->raw_packet_buffer,
254                                 &s->streams[i]->attached_pic,
255                                 &s->internal->raw_packet_buffer_end, 1);
256             if (ret < 0)
257                 return ret;
258         }
259     return 0;
260 }
261
262 #if FF_API_LAVF_AVCTX
263 FF_DISABLE_DEPRECATION_WARNINGS
264 static int update_stream_avctx(AVFormatContext *s)
265 {
266     int i, ret;
267     for (i = 0; i < s->nb_streams; i++) {
268         AVStream *st = s->streams[i];
269
270         if (!st->internal->need_codec_update)
271             continue;
272
273         ret = avcodec_parameters_to_context(st->codec, st->codecpar);
274         if (ret < 0)
275             return ret;
276
277         st->internal->need_codec_update = 0;
278     }
279     return 0;
280 }
281 FF_ENABLE_DEPRECATION_WARNINGS
282 #endif
283
284 int avformat_open_input(AVFormatContext **ps, const char *filename,
285                         AVInputFormat *fmt, AVDictionary **options)
286 {
287     AVFormatContext *s = *ps;
288     int i, ret = 0;
289     AVDictionary *tmp = NULL;
290     ID3v2ExtraMeta *id3v2_extra_meta = NULL;
291
292     if (!s && !(s = avformat_alloc_context()))
293         return AVERROR(ENOMEM);
294     if (fmt)
295         s->iformat = fmt;
296
297     if (options)
298         av_dict_copy(&tmp, *options, 0);
299
300     if ((ret = av_opt_set_dict(s, &tmp)) < 0)
301         goto fail;
302
303     if ((ret = init_input(s, filename, &tmp)) < 0)
304         goto fail;
305
306     /* Check filename in case an image number is expected. */
307     if (s->iformat->flags & AVFMT_NEEDNUMBER) {
308         if (!av_filename_number_test(filename)) {
309             ret = AVERROR(EINVAL);
310             goto fail;
311         }
312     }
313
314     s->duration = s->start_time = AV_NOPTS_VALUE;
315     av_strlcpy(s->filename, filename ? filename : "", sizeof(s->filename));
316
317     /* Allocate private data. */
318     if (s->iformat->priv_data_size > 0) {
319         if (!(s->priv_data = av_mallocz(s->iformat->priv_data_size))) {
320             ret = AVERROR(ENOMEM);
321             goto fail;
322         }
323         if (s->iformat->priv_class) {
324             *(const AVClass **) s->priv_data = s->iformat->priv_class;
325             av_opt_set_defaults(s->priv_data);
326             if ((ret = av_opt_set_dict(s->priv_data, &tmp)) < 0)
327                 goto fail;
328         }
329     }
330
331     /* e.g. AVFMT_NOFILE formats will not have a AVIOContext */
332     if (s->pb)
333         ff_id3v2_read(s, ID3v2_DEFAULT_MAGIC, &id3v2_extra_meta);
334
335     if (s->iformat->read_header)
336         if ((ret = s->iformat->read_header(s)) < 0)
337             goto fail;
338
339     if (id3v2_extra_meta &&
340         (ret = ff_id3v2_parse_apic(s, &id3v2_extra_meta)) < 0)
341         goto fail;
342     ff_id3v2_free_extra_meta(&id3v2_extra_meta);
343
344     if ((ret = queue_attached_pictures(s)) < 0)
345         goto fail;
346
347     if (s->pb && !s->internal->data_offset)
348         s->internal->data_offset = avio_tell(s->pb);
349
350     s->internal->raw_packet_buffer_remaining_size = RAW_PACKET_BUFFER_SIZE;
351
352 #if FF_API_LAVF_AVCTX
353     update_stream_avctx(s);
354 #endif
355
356     for (i = 0; i < s->nb_streams; i++)
357         s->streams[i]->internal->orig_codec_id = s->streams[i]->codecpar->codec_id;
358
359     if (options) {
360         av_dict_free(options);
361         *options = tmp;
362     }
363     *ps = s;
364     return 0;
365
366 fail:
367     ff_id3v2_free_extra_meta(&id3v2_extra_meta);
368     av_dict_free(&tmp);
369     if (s->pb && !(s->flags & AVFMT_FLAG_CUSTOM_IO))
370         avio_close(s->pb);
371     avformat_free_context(s);
372     *ps = NULL;
373     return ret;
374 }
375
376 /*******************************************************/
377
378 static int probe_codec(AVFormatContext *s, AVStream *st, const AVPacket *pkt)
379 {
380     if (st->codecpar->codec_id == AV_CODEC_ID_PROBE) {
381         AVProbeData *pd = &st->probe_data;
382         av_log(s, AV_LOG_DEBUG, "probing stream %d\n", st->index);
383         --st->probe_packets;
384
385         if (pkt) {
386             int err;
387             if ((err = av_reallocp(&pd->buf, pd->buf_size + pkt->size +
388                                    AVPROBE_PADDING_SIZE)) < 0)
389                 return err;
390             memcpy(pd->buf + pd->buf_size, pkt->data, pkt->size);
391             pd->buf_size += pkt->size;
392             memset(pd->buf + pd->buf_size, 0, AVPROBE_PADDING_SIZE);
393         } else {
394             st->probe_packets = 0;
395             if (!pd->buf_size) {
396                 av_log(s, AV_LOG_ERROR,
397                        "nothing to probe for stream %d\n", st->index);
398                 return 0;
399             }
400         }
401
402         if (!st->probe_packets ||
403             av_log2(pd->buf_size) != av_log2(pd->buf_size - pkt->size)) {
404             set_codec_from_probe_data(s, st, pd, st->probe_packets > 0
405                                                  ? AVPROBE_SCORE_MAX / 4 : 0);
406             if (st->codecpar->codec_id != AV_CODEC_ID_PROBE) {
407                 pd->buf_size = 0;
408                 av_freep(&pd->buf);
409                 av_log(s, AV_LOG_DEBUG, "probed stream %d\n", st->index);
410             }
411         }
412     }
413     return 0;
414 }
415
416 int ff_read_packet(AVFormatContext *s, AVPacket *pkt)
417 {
418     int ret, i, err;
419     AVStream *st;
420
421     for (;;) {
422         AVPacketList *pktl = s->internal->raw_packet_buffer;
423
424         if (pktl) {
425             *pkt = pktl->pkt;
426             st   = s->streams[pkt->stream_index];
427             if (st->codecpar->codec_id != AV_CODEC_ID_PROBE ||
428                 !st->probe_packets ||
429                 s->internal->raw_packet_buffer_remaining_size < pkt->size) {
430                 AVProbeData *pd;
431                 if (st->probe_packets)
432                     if ((err = probe_codec(s, st, NULL)) < 0)
433                         return err;
434                 pd = &st->probe_data;
435                 av_freep(&pd->buf);
436                 pd->buf_size = 0;
437                 s->internal->raw_packet_buffer                 = pktl->next;
438                 s->internal->raw_packet_buffer_remaining_size += pkt->size;
439                 av_free(pktl);
440                 return 0;
441             }
442         }
443
444         pkt->data = NULL;
445         pkt->size = 0;
446         av_init_packet(pkt);
447         ret = s->iformat->read_packet(s, pkt);
448         if (ret < 0) {
449             if (!pktl || ret == AVERROR(EAGAIN))
450                 return ret;
451             for (i = 0; i < s->nb_streams; i++) {
452                 st = s->streams[i];
453                 if (st->probe_packets)
454                     if ((err = probe_codec(s, st, NULL)) < 0)
455                         return err;
456             }
457             continue;
458         }
459
460         if (!pkt->buf) {
461             AVPacket tmp = { 0 };
462             ret = av_packet_ref(&tmp, pkt);
463             if (ret < 0)
464                 return ret;
465             *pkt = tmp;
466         }
467
468         if ((s->flags & AVFMT_FLAG_DISCARD_CORRUPT) &&
469             (pkt->flags & AV_PKT_FLAG_CORRUPT)) {
470             av_log(s, AV_LOG_WARNING,
471                    "Dropped corrupted packet (stream = %d)\n",
472                    pkt->stream_index);
473             av_packet_unref(pkt);
474             continue;
475         }
476
477         st = s->streams[pkt->stream_index];
478
479         switch (st->codecpar->codec_type) {
480         case AVMEDIA_TYPE_VIDEO:
481             if (s->video_codec_id)
482                 st->codecpar->codec_id = s->video_codec_id;
483             break;
484         case AVMEDIA_TYPE_AUDIO:
485             if (s->audio_codec_id)
486                 st->codecpar->codec_id = s->audio_codec_id;
487             break;
488         case AVMEDIA_TYPE_SUBTITLE:
489             if (s->subtitle_codec_id)
490                 st->codecpar->codec_id = s->subtitle_codec_id;
491             break;
492         }
493
494         if (!pktl && (st->codecpar->codec_id != AV_CODEC_ID_PROBE ||
495                       !st->probe_packets))
496             return ret;
497
498         err = add_to_pktbuf(&s->internal->raw_packet_buffer, pkt,
499                             &s->internal->raw_packet_buffer_end, 0);
500         if (err)
501             return err;
502         s->internal->raw_packet_buffer_remaining_size -= pkt->size;
503
504         if ((err = probe_codec(s, st, pkt)) < 0)
505             return err;
506     }
507 }
508
509 /**********************************************************/
510
511 /**
512  * Return the frame duration in seconds. Return 0 if not available.
513  */
514 void ff_compute_frame_duration(AVFormatContext *s, int *pnum, int *pden, AVStream *st,
515                                AVCodecParserContext *pc, AVPacket *pkt)
516 {
517     AVRational codec_framerate = s->iformat ? st->internal->avctx->framerate :
518                                               (AVRational){ 0, 1 };
519     int frame_size;
520
521     *pnum = 0;
522     *pden = 0;
523     switch (st->codecpar->codec_type) {
524     case AVMEDIA_TYPE_VIDEO:
525         if (st->avg_frame_rate.num) {
526             *pnum = st->avg_frame_rate.den;
527             *pden = st->avg_frame_rate.num;
528         } else if (st->time_base.num * 1000LL > st->time_base.den) {
529             *pnum = st->time_base.num;
530             *pden = st->time_base.den;
531         } else if (codec_framerate.den * 1000LL > codec_framerate.num) {
532             *pnum = codec_framerate.den;
533             *pden = codec_framerate.num;
534             if (pc && pc->repeat_pict) {
535                 if (*pnum > INT_MAX / (1 + pc->repeat_pict))
536                     *pden /= 1 + pc->repeat_pict;
537                 else
538                     *pnum *= 1 + pc->repeat_pict;
539             }
540             /* If this codec can be interlaced or progressive then we need
541              * a parser to compute duration of a packet. Thus if we have
542              * no parser in such case leave duration undefined. */
543             if (st->internal->avctx->ticks_per_frame > 1 && !pc)
544                 *pnum = *pden = 0;
545         }
546         break;
547     case AVMEDIA_TYPE_AUDIO:
548         frame_size = av_get_audio_frame_duration2(st->codecpar, pkt->size);
549         if (frame_size <= 0 || st->codecpar->sample_rate <= 0)
550             break;
551         *pnum = frame_size;
552         *pden = st->codecpar->sample_rate;
553         break;
554     default:
555         break;
556     }
557 }
558
559 static int is_intra_only(enum AVCodecID id)
560 {
561     const AVCodecDescriptor *d = avcodec_descriptor_get(id);
562     if (!d)
563         return 0;
564     if (d->type == AVMEDIA_TYPE_VIDEO && !(d->props & AV_CODEC_PROP_INTRA_ONLY))
565         return 0;
566     return 1;
567 }
568
569 static void update_initial_timestamps(AVFormatContext *s, int stream_index,
570                                       int64_t dts, int64_t pts)
571 {
572     AVStream *st       = s->streams[stream_index];
573     AVPacketList *pktl = s->internal->packet_buffer;
574
575     if (st->first_dts != AV_NOPTS_VALUE ||
576         dts           == AV_NOPTS_VALUE ||
577         st->cur_dts   == AV_NOPTS_VALUE)
578         return;
579
580     st->first_dts = dts - st->cur_dts;
581     st->cur_dts   = dts;
582
583     for (; pktl; pktl = pktl->next) {
584         if (pktl->pkt.stream_index != stream_index)
585             continue;
586         // FIXME: think more about this check
587         if (pktl->pkt.pts != AV_NOPTS_VALUE && pktl->pkt.pts == pktl->pkt.dts)
588             pktl->pkt.pts += st->first_dts;
589
590         if (pktl->pkt.dts != AV_NOPTS_VALUE)
591             pktl->pkt.dts += st->first_dts;
592
593         if (st->start_time == AV_NOPTS_VALUE && pktl->pkt.pts != AV_NOPTS_VALUE)
594             st->start_time = pktl->pkt.pts;
595     }
596     if (st->start_time == AV_NOPTS_VALUE)
597         st->start_time = pts;
598 }
599
600 static void update_initial_durations(AVFormatContext *s, AVStream *st,
601                                      int stream_index, int duration)
602 {
603     AVPacketList *pktl = s->internal->packet_buffer;
604     int64_t cur_dts    = 0;
605
606     if (st->first_dts != AV_NOPTS_VALUE) {
607         cur_dts = st->first_dts;
608         for (; pktl; pktl = pktl->next) {
609             if (pktl->pkt.stream_index == stream_index) {
610                 if (pktl->pkt.pts != pktl->pkt.dts  ||
611                     pktl->pkt.dts != AV_NOPTS_VALUE ||
612                     pktl->pkt.duration)
613                     break;
614                 cur_dts -= duration;
615             }
616         }
617         pktl          = s->internal->packet_buffer;
618         st->first_dts = cur_dts;
619     } else if (st->cur_dts)
620         return;
621
622     for (; pktl; pktl = pktl->next) {
623         if (pktl->pkt.stream_index != stream_index)
624             continue;
625         if (pktl->pkt.pts == pktl->pkt.dts  &&
626             pktl->pkt.dts == AV_NOPTS_VALUE &&
627             !pktl->pkt.duration) {
628             pktl->pkt.dts = cur_dts;
629             if (!st->internal->avctx->has_b_frames)
630                 pktl->pkt.pts = cur_dts;
631             cur_dts += duration;
632             if (st->codecpar->codec_type != AVMEDIA_TYPE_AUDIO)
633                 pktl->pkt.duration = duration;
634         } else
635             break;
636     }
637     if (st->first_dts == AV_NOPTS_VALUE)
638         st->cur_dts = cur_dts;
639 }
640
641 static void compute_pkt_fields(AVFormatContext *s, AVStream *st,
642                                AVCodecParserContext *pc, AVPacket *pkt)
643 {
644     int num, den, presentation_delayed, delay, i;
645     int64_t offset;
646
647     if (s->flags & AVFMT_FLAG_NOFILLIN)
648         return;
649
650     if ((s->flags & AVFMT_FLAG_IGNDTS) && pkt->pts != AV_NOPTS_VALUE)
651         pkt->dts = AV_NOPTS_VALUE;
652
653     /* do we have a video B-frame ? */
654     delay = st->internal->avctx->has_b_frames;
655     presentation_delayed = 0;
656
657     /* XXX: need has_b_frame, but cannot get it if the codec is
658      *  not initialized */
659     if (delay &&
660         pc && pc->pict_type != AV_PICTURE_TYPE_B)
661         presentation_delayed = 1;
662
663     if (pkt->pts != AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE &&
664         st->pts_wrap_bits < 63 &&
665         pkt->dts - (1LL << (st->pts_wrap_bits - 1)) > pkt->pts) {
666         pkt->dts -= 1LL << st->pts_wrap_bits;
667     }
668
669     /* Some MPEG-2 in MPEG-PS lack dts (issue #171 / input_file.mpg).
670      * We take the conservative approach and discard both.
671      * Note: If this is misbehaving for an H.264 file, then possibly
672      * presentation_delayed is not set correctly. */
673     if (delay == 1 && pkt->dts == pkt->pts &&
674         pkt->dts != AV_NOPTS_VALUE && presentation_delayed) {
675         av_log(s, AV_LOG_DEBUG, "invalid dts/pts combination\n");
676         pkt->dts = AV_NOPTS_VALUE;
677     }
678
679     if (pkt->duration == 0 && st->codecpar->codec_type != AVMEDIA_TYPE_AUDIO) {
680         ff_compute_frame_duration(s, &num, &den, st, pc, pkt);
681         if (den && num) {
682             pkt->duration = av_rescale_rnd(1, num * (int64_t) st->time_base.den,
683                                            den * (int64_t) st->time_base.num,
684                                            AV_ROUND_DOWN);
685
686             if (pkt->duration != 0 && s->internal->packet_buffer)
687                 update_initial_durations(s, st, pkt->stream_index,
688                                          pkt->duration);
689         }
690     }
691
692     /* Correct timestamps with byte offset if demuxers only have timestamps
693      * on packet boundaries */
694     if (pc && st->need_parsing == AVSTREAM_PARSE_TIMESTAMPS && pkt->size) {
695         /* this will estimate bitrate based on this frame's duration and size */
696         offset = av_rescale(pc->offset, pkt->duration, pkt->size);
697         if (pkt->pts != AV_NOPTS_VALUE)
698             pkt->pts += offset;
699         if (pkt->dts != AV_NOPTS_VALUE)
700             pkt->dts += offset;
701     }
702
703     /* This may be redundant, but it should not hurt. */
704     if (pkt->dts != AV_NOPTS_VALUE &&
705         pkt->pts != AV_NOPTS_VALUE &&
706         pkt->pts > pkt->dts)
707         presentation_delayed = 1;
708
709     av_log(NULL, AV_LOG_TRACE,
710             "IN delayed:%d pts:%"PRId64", dts:%"PRId64" "
711             "cur_dts:%"PRId64" st:%d pc:%p\n",
712             presentation_delayed, pkt->pts, pkt->dts, st->cur_dts,
713             pkt->stream_index, pc);
714     /* Interpolate PTS and DTS if they are not present. We skip H.264
715      * currently because delay and has_b_frames are not reliably set. */
716     if ((delay == 0 || (delay == 1 && pc)) &&
717         st->codecpar->codec_id != AV_CODEC_ID_H264) {
718         if (presentation_delayed) {
719             /* DTS = decompression timestamp */
720             /* PTS = presentation timestamp */
721             if (pkt->dts == AV_NOPTS_VALUE)
722                 pkt->dts = st->last_IP_pts;
723             update_initial_timestamps(s, pkt->stream_index, pkt->dts, pkt->pts);
724             if (pkt->dts == AV_NOPTS_VALUE)
725                 pkt->dts = st->cur_dts;
726
727             /* This is tricky: the dts must be incremented by the duration
728              * of the frame we are displaying, i.e. the last I- or P-frame. */
729             if (st->last_IP_duration == 0)
730                 st->last_IP_duration = pkt->duration;
731             if (pkt->dts != AV_NOPTS_VALUE)
732                 st->cur_dts = pkt->dts + st->last_IP_duration;
733             st->last_IP_duration = pkt->duration;
734             st->last_IP_pts      = pkt->pts;
735             /* Cannot compute PTS if not present (we can compute it only
736              * by knowing the future. */
737         } else if (pkt->pts != AV_NOPTS_VALUE ||
738                    pkt->dts != AV_NOPTS_VALUE ||
739                    pkt->duration              ||
740                    st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
741             int duration = pkt->duration;
742             if (!duration && st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
743                 ff_compute_frame_duration(s, &num, &den, st, pc, pkt);
744                 if (den && num) {
745                     duration = av_rescale_rnd(1,
746                                               num * (int64_t) st->time_base.den,
747                                               den * (int64_t) st->time_base.num,
748                                               AV_ROUND_DOWN);
749                     if (duration != 0 && s->internal->packet_buffer)
750                         update_initial_durations(s, st, pkt->stream_index,
751                                                  duration);
752                 }
753             }
754
755             if (pkt->pts != AV_NOPTS_VALUE || pkt->dts != AV_NOPTS_VALUE ||
756                 duration) {
757                 /* presentation is not delayed : PTS and DTS are the same */
758                 if (pkt->pts == AV_NOPTS_VALUE)
759                     pkt->pts = pkt->dts;
760                 update_initial_timestamps(s, pkt->stream_index, pkt->pts,
761                                           pkt->pts);
762                 if (pkt->pts == AV_NOPTS_VALUE)
763                     pkt->pts = st->cur_dts;
764                 pkt->dts = pkt->pts;
765                 if (pkt->pts != AV_NOPTS_VALUE)
766                     st->cur_dts = pkt->pts + duration;
767             }
768         }
769     }
770
771     if (pkt->pts != AV_NOPTS_VALUE && delay <= MAX_REORDER_DELAY) {
772         st->pts_buffer[0] = pkt->pts;
773         for (i = 0; i<delay && st->pts_buffer[i] > st->pts_buffer[i + 1]; i++)
774             FFSWAP(int64_t, st->pts_buffer[i], st->pts_buffer[i + 1]);
775         if (pkt->dts == AV_NOPTS_VALUE)
776             pkt->dts = st->pts_buffer[0];
777         // We skipped it above so we try here.
778         if (st->codecpar->codec_id == AV_CODEC_ID_H264)
779             // This should happen on the first packet
780             update_initial_timestamps(s, pkt->stream_index, pkt->dts, pkt->pts);
781         if (pkt->dts > st->cur_dts)
782             st->cur_dts = pkt->dts;
783     }
784
785     av_log(NULL, AV_LOG_TRACE,
786             "OUTdelayed:%d/%d pts:%"PRId64", dts:%"PRId64" cur_dts:%"PRId64"\n",
787             presentation_delayed, delay, pkt->pts, pkt->dts, st->cur_dts);
788
789     /* update flags */
790     if (is_intra_only(st->codecpar->codec_id))
791         pkt->flags |= AV_PKT_FLAG_KEY;
792 #if FF_API_CONVERGENCE_DURATION
793 FF_DISABLE_DEPRECATION_WARNINGS
794     if (pc)
795         pkt->convergence_duration = pc->convergence_duration;
796 FF_ENABLE_DEPRECATION_WARNINGS
797 #endif
798 }
799
800 static void free_packet_buffer(AVPacketList **pkt_buf, AVPacketList **pkt_buf_end)
801 {
802     while (*pkt_buf) {
803         AVPacketList *pktl = *pkt_buf;
804         *pkt_buf = pktl->next;
805         av_packet_unref(&pktl->pkt);
806         av_freep(&pktl);
807     }
808     *pkt_buf_end = NULL;
809 }
810
811 /**
812  * Parse a packet, add all split parts to parse_queue.
813  *
814  * @param pkt Packet to parse, NULL when flushing the parser at end of stream.
815  */
816 static int parse_packet(AVFormatContext *s, AVPacket *pkt, int stream_index)
817 {
818     AVPacket out_pkt = { 0 }, flush_pkt = { 0 };
819     AVStream *st = s->streams[stream_index];
820     uint8_t *data = pkt ? pkt->data : NULL;
821     int size      = pkt ? pkt->size : 0;
822     int ret = 0, got_output = 0;
823
824     if (!pkt) {
825         av_init_packet(&flush_pkt);
826         pkt        = &flush_pkt;
827         got_output = 1;
828     }
829
830     while (size > 0 || (pkt == &flush_pkt && got_output)) {
831         int len;
832
833         av_init_packet(&out_pkt);
834         len = av_parser_parse2(st->parser, st->internal->avctx,
835                                &out_pkt.data, &out_pkt.size, data, size,
836                                pkt->pts, pkt->dts, pkt->pos);
837
838         pkt->pts = pkt->dts = AV_NOPTS_VALUE;
839         /* increment read pointer */
840         data += len;
841         size -= len;
842
843         got_output = !!out_pkt.size;
844
845         if (!out_pkt.size)
846             continue;
847
848         if (pkt->side_data) {
849             out_pkt.side_data       = pkt->side_data;
850             out_pkt.side_data_elems = pkt->side_data_elems;
851             pkt->side_data          = NULL;
852             pkt->side_data_elems    = 0;
853         }
854
855         /* set the duration */
856         out_pkt.duration = 0;
857         if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
858             if (st->internal->avctx->sample_rate > 0) {
859                 out_pkt.duration =
860                     av_rescale_q_rnd(st->parser->duration,
861                                      (AVRational) { 1, st->internal->avctx->sample_rate },
862                                      st->time_base,
863                                      AV_ROUND_DOWN);
864             }
865         }
866
867         out_pkt.stream_index = st->index;
868         out_pkt.pts          = st->parser->pts;
869         out_pkt.dts          = st->parser->dts;
870         out_pkt.pos          = st->parser->pos;
871
872         if (st->parser->key_frame == 1 ||
873             (st->parser->key_frame == -1 &&
874              st->parser->pict_type == AV_PICTURE_TYPE_I))
875             out_pkt.flags |= AV_PKT_FLAG_KEY;
876
877         compute_pkt_fields(s, st, st->parser, &out_pkt);
878
879         if ((s->iformat->flags & AVFMT_GENERIC_INDEX) &&
880             out_pkt.flags & AV_PKT_FLAG_KEY) {
881             ff_reduce_index(s, st->index);
882             av_add_index_entry(st, st->parser->frame_offset, out_pkt.dts,
883                                0, 0, AVINDEX_KEYFRAME);
884         }
885
886         if ((ret = add_to_pktbuf(&s->internal->parse_queue, &out_pkt,
887                                  &s->internal->parse_queue_end,
888                                  1))) {
889             av_packet_unref(&out_pkt);
890             goto fail;
891         }
892     }
893
894     /* end of the stream => close and free the parser */
895     if (pkt == &flush_pkt) {
896         av_parser_close(st->parser);
897         st->parser = NULL;
898     }
899
900 fail:
901     av_packet_unref(pkt);
902     return ret;
903 }
904
905 static int read_from_packet_buffer(AVPacketList **pkt_buffer,
906                                    AVPacketList **pkt_buffer_end,
907                                    AVPacket      *pkt)
908 {
909     AVPacketList *pktl;
910     av_assert0(*pkt_buffer);
911     pktl        = *pkt_buffer;
912     *pkt        = pktl->pkt;
913     *pkt_buffer = pktl->next;
914     if (!pktl->next)
915         *pkt_buffer_end = NULL;
916     av_freep(&pktl);
917     return 0;
918 }
919
920 static int read_frame_internal(AVFormatContext *s, AVPacket *pkt)
921 {
922     int ret = 0, i, got_packet = 0;
923     AVDictionary *metadata = NULL;
924
925     av_init_packet(pkt);
926
927     while (!got_packet && !s->internal->parse_queue) {
928         AVStream *st;
929         AVPacket cur_pkt;
930
931         /* read next packet */
932         ret = ff_read_packet(s, &cur_pkt);
933         if (ret < 0) {
934             if (ret == AVERROR(EAGAIN))
935                 return ret;
936             /* flush the parsers */
937             for (i = 0; i < s->nb_streams; i++) {
938                 st = s->streams[i];
939                 if (st->parser && st->need_parsing)
940                     parse_packet(s, NULL, st->index);
941             }
942             /* all remaining packets are now in parse_queue =>
943              * really terminate parsing */
944             break;
945         }
946         ret = 0;
947         st  = s->streams[cur_pkt.stream_index];
948
949         if (cur_pkt.pts != AV_NOPTS_VALUE &&
950             cur_pkt.dts != AV_NOPTS_VALUE &&
951             cur_pkt.pts < cur_pkt.dts) {
952             av_log(s, AV_LOG_WARNING,
953                    "Invalid timestamps stream=%d, pts=%"PRId64", "
954                    "dts=%"PRId64", size=%d\n",
955                    cur_pkt.stream_index, cur_pkt.pts,
956                    cur_pkt.dts, cur_pkt.size);
957         }
958         if (s->debug & FF_FDEBUG_TS)
959             av_log(s, AV_LOG_DEBUG,
960                    "ff_read_packet stream=%d, pts=%"PRId64", dts=%"PRId64", "
961                    "size=%d, duration=%"PRId64", flags=%d\n",
962                    cur_pkt.stream_index, cur_pkt.pts, cur_pkt.dts,
963                    cur_pkt.size, cur_pkt.duration, cur_pkt.flags);
964
965         if (st->need_parsing && !st->parser && !(s->flags & AVFMT_FLAG_NOPARSE)) {
966             st->parser = av_parser_init(st->codecpar->codec_id);
967             if (!st->parser)
968                 /* no parser available: just output the raw packets */
969                 st->need_parsing = AVSTREAM_PARSE_NONE;
970             else if (st->need_parsing == AVSTREAM_PARSE_HEADERS)
971                 st->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES;
972             else if (st->need_parsing == AVSTREAM_PARSE_FULL_ONCE)
973                 st->parser->flags |= PARSER_FLAG_ONCE;
974         }
975
976         if (!st->need_parsing || !st->parser) {
977             /* no parsing needed: we just output the packet as is */
978             *pkt = cur_pkt;
979             compute_pkt_fields(s, st, NULL, pkt);
980             if ((s->iformat->flags & AVFMT_GENERIC_INDEX) &&
981                 (pkt->flags & AV_PKT_FLAG_KEY) && pkt->dts != AV_NOPTS_VALUE) {
982                 ff_reduce_index(s, st->index);
983                 av_add_index_entry(st, pkt->pos, pkt->dts,
984                                    0, 0, AVINDEX_KEYFRAME);
985             }
986             got_packet = 1;
987         } else if (st->discard < AVDISCARD_ALL) {
988             if ((ret = parse_packet(s, &cur_pkt, cur_pkt.stream_index)) < 0)
989                 return ret;
990         } else {
991             /* free packet */
992             av_packet_unref(&cur_pkt);
993         }
994     }
995
996     if (!got_packet && s->internal->parse_queue)
997         ret = read_from_packet_buffer(&s->internal->parse_queue, &s->internal->parse_queue_end, pkt);
998
999     av_opt_get_dict_val(s, "metadata", AV_OPT_SEARCH_CHILDREN, &metadata);
1000     if (metadata) {
1001         s->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
1002         av_dict_copy(&s->metadata, metadata, 0);
1003         av_dict_free(&metadata);
1004         av_opt_set_dict_val(s, "metadata", NULL, AV_OPT_SEARCH_CHILDREN);
1005     }
1006
1007 #if FF_API_LAVF_AVCTX
1008     update_stream_avctx(s);
1009 #endif
1010
1011     if (s->debug & FF_FDEBUG_TS)
1012         av_log(s, AV_LOG_DEBUG,
1013                "read_frame_internal stream=%d, pts=%"PRId64", dts=%"PRId64", "
1014                "size=%d, duration=%"PRId64", flags=%d\n",
1015                pkt->stream_index, pkt->pts, pkt->dts,
1016                pkt->size, pkt->duration, pkt->flags);
1017
1018     return ret;
1019 }
1020
1021 int av_read_frame(AVFormatContext *s, AVPacket *pkt)
1022 {
1023     const int genpts = s->flags & AVFMT_FLAG_GENPTS;
1024     int eof = 0;
1025
1026     if (!genpts)
1027         return s->internal->packet_buffer
1028                ? read_from_packet_buffer(&s->internal->packet_buffer,
1029                                          &s->internal->packet_buffer_end, pkt)
1030                : read_frame_internal(s, pkt);
1031
1032     for (;;) {
1033         int ret;
1034         AVPacketList *pktl = s->internal->packet_buffer;
1035
1036         if (pktl) {
1037             AVPacket *next_pkt = &pktl->pkt;
1038
1039             if (next_pkt->dts != AV_NOPTS_VALUE) {
1040                 int wrap_bits = s->streams[next_pkt->stream_index]->pts_wrap_bits;
1041                 while (pktl && next_pkt->pts == AV_NOPTS_VALUE) {
1042                     if (pktl->pkt.stream_index == next_pkt->stream_index &&
1043                         (av_compare_mod(next_pkt->dts, pktl->pkt.dts, 2LL << (wrap_bits - 1)) < 0) &&
1044                          av_compare_mod(pktl->pkt.pts, pktl->pkt.dts, 2LL << (wrap_bits - 1))) {
1045                         // not B-frame
1046                         next_pkt->pts = pktl->pkt.dts;
1047                     }
1048                     pktl = pktl->next;
1049                 }
1050                 pktl = s->internal->packet_buffer;
1051             }
1052
1053             /* read packet from packet buffer, if there is data */
1054             if (!(next_pkt->pts == AV_NOPTS_VALUE &&
1055                   next_pkt->dts != AV_NOPTS_VALUE && !eof))
1056                 return read_from_packet_buffer(&s->internal->packet_buffer,
1057                                                &s->internal->packet_buffer_end, pkt);
1058         }
1059
1060         ret = read_frame_internal(s, pkt);
1061         if (ret < 0) {
1062             if (pktl && ret != AVERROR(EAGAIN)) {
1063                 eof = 1;
1064                 continue;
1065             } else
1066                 return ret;
1067         }
1068
1069         ret = add_to_pktbuf(&s->internal->packet_buffer, pkt,
1070                             &s->internal->packet_buffer_end, 1);
1071         if (ret < 0)
1072             return ret;
1073     }
1074 }
1075
1076 /* XXX: suppress the packet queue */
1077 static void flush_packet_queue(AVFormatContext *s)
1078 {
1079     free_packet_buffer(&s->internal->parse_queue,       &s->internal->parse_queue_end);
1080     free_packet_buffer(&s->internal->packet_buffer,     &s->internal->packet_buffer_end);
1081     free_packet_buffer(&s->internal->raw_packet_buffer, &s->internal->raw_packet_buffer_end);
1082
1083     s->internal->raw_packet_buffer_remaining_size = RAW_PACKET_BUFFER_SIZE;
1084 }
1085
1086 /*******************************************************/
1087 /* seek support */
1088
1089 int av_find_default_stream_index(AVFormatContext *s)
1090 {
1091     int first_audio_index = -1;
1092     int i;
1093     AVStream *st;
1094
1095     if (s->nb_streams <= 0)
1096         return -1;
1097     for (i = 0; i < s->nb_streams; i++) {
1098         st = s->streams[i];
1099         if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
1100             !(st->disposition & AV_DISPOSITION_ATTACHED_PIC)) {
1101             return i;
1102         }
1103         if (first_audio_index < 0 &&
1104             st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)
1105             first_audio_index = i;
1106     }
1107     return first_audio_index >= 0 ? first_audio_index : 0;
1108 }
1109
1110 /** Flush the frame reader. */
1111 void ff_read_frame_flush(AVFormatContext *s)
1112 {
1113     AVStream *st;
1114     int i, j;
1115
1116     flush_packet_queue(s);
1117
1118     /* Reset read state for each stream. */
1119     for (i = 0; i < s->nb_streams; i++) {
1120         st = s->streams[i];
1121
1122         if (st->parser) {
1123             av_parser_close(st->parser);
1124             st->parser = NULL;
1125         }
1126         st->last_IP_pts = AV_NOPTS_VALUE;
1127         /* We set the current DTS to an unspecified origin. */
1128         st->cur_dts     = AV_NOPTS_VALUE;
1129
1130         st->probe_packets = MAX_PROBE_PACKETS;
1131
1132         for (j = 0; j < MAX_REORDER_DELAY + 1; j++)
1133             st->pts_buffer[j] = AV_NOPTS_VALUE;
1134     }
1135 }
1136
1137 void ff_update_cur_dts(AVFormatContext *s, AVStream *ref_st, int64_t timestamp)
1138 {
1139     int i;
1140
1141     for (i = 0; i < s->nb_streams; i++) {
1142         AVStream *st = s->streams[i];
1143
1144         st->cur_dts =
1145             av_rescale(timestamp,
1146                        st->time_base.den * (int64_t) ref_st->time_base.num,
1147                        st->time_base.num * (int64_t) ref_st->time_base.den);
1148     }
1149 }
1150
1151 void ff_reduce_index(AVFormatContext *s, int stream_index)
1152 {
1153     AVStream *st             = s->streams[stream_index];
1154     unsigned int max_entries = s->max_index_size / sizeof(AVIndexEntry);
1155
1156     if ((unsigned) st->nb_index_entries >= max_entries) {
1157         int i;
1158         for (i = 0; 2 * i < st->nb_index_entries; i++)
1159             st->index_entries[i] = st->index_entries[2 * i];
1160         st->nb_index_entries = i;
1161     }
1162 }
1163
1164 int ff_add_index_entry(AVIndexEntry **index_entries,
1165                        int *nb_index_entries,
1166                        unsigned int *index_entries_allocated_size,
1167                        int64_t pos, int64_t timestamp,
1168                        int size, int distance, int flags)
1169 {
1170     AVIndexEntry *entries, *ie;
1171     int index;
1172
1173     if ((unsigned) *nb_index_entries + 1 >= UINT_MAX / sizeof(AVIndexEntry))
1174         return -1;
1175
1176     entries = av_fast_realloc(*index_entries,
1177                               index_entries_allocated_size,
1178                               (*nb_index_entries + 1) *
1179                               sizeof(AVIndexEntry));
1180     if (!entries)
1181         return -1;
1182
1183     *index_entries = entries;
1184
1185     index = ff_index_search_timestamp(*index_entries, *nb_index_entries,
1186                                       timestamp, AVSEEK_FLAG_ANY);
1187
1188     if (index < 0) {
1189         index = (*nb_index_entries)++;
1190         ie    = &entries[index];
1191         assert(index == 0 || ie[-1].timestamp < timestamp);
1192     } else {
1193         ie = &entries[index];
1194         if (ie->timestamp != timestamp) {
1195             if (ie->timestamp <= timestamp)
1196                 return -1;
1197             memmove(entries + index + 1, entries + index,
1198                     sizeof(AVIndexEntry) * (*nb_index_entries - index));
1199             (*nb_index_entries)++;
1200         } else if (ie->pos == pos && distance < ie->min_distance)
1201             // do not reduce the distance
1202             distance = ie->min_distance;
1203     }
1204
1205     ie->pos          = pos;
1206     ie->timestamp    = timestamp;
1207     ie->min_distance = distance;
1208     ie->size         = size;
1209     ie->flags        = flags;
1210
1211     return index;
1212 }
1213
1214 int av_add_index_entry(AVStream *st, int64_t pos, int64_t timestamp,
1215                        int size, int distance, int flags)
1216 {
1217     return ff_add_index_entry(&st->index_entries, &st->nb_index_entries,
1218                               &st->index_entries_allocated_size, pos,
1219                               timestamp, size, distance, flags);
1220 }
1221
1222 int ff_index_search_timestamp(const AVIndexEntry *entries, int nb_entries,
1223                               int64_t wanted_timestamp, int flags)
1224 {
1225     int a, b, m;
1226     int64_t timestamp;
1227
1228     a = -1;
1229     b = nb_entries;
1230
1231     // Optimize appending index entries at the end.
1232     if (b && entries[b - 1].timestamp < wanted_timestamp)
1233         a = b - 1;
1234
1235     while (b - a > 1) {
1236         m         = (a + b) >> 1;
1237         timestamp = entries[m].timestamp;
1238         if (timestamp >= wanted_timestamp)
1239             b = m;
1240         if (timestamp <= wanted_timestamp)
1241             a = m;
1242     }
1243     m = (flags & AVSEEK_FLAG_BACKWARD) ? a : b;
1244
1245     if (!(flags & AVSEEK_FLAG_ANY))
1246         while (m >= 0 && m < nb_entries &&
1247                !(entries[m].flags & AVINDEX_KEYFRAME))
1248             m += (flags & AVSEEK_FLAG_BACKWARD) ? -1 : 1;
1249
1250     if (m == nb_entries)
1251         return -1;
1252     return m;
1253 }
1254
1255 int av_index_search_timestamp(AVStream *st, int64_t wanted_timestamp, int flags)
1256 {
1257     return ff_index_search_timestamp(st->index_entries, st->nb_index_entries,
1258                                      wanted_timestamp, flags);
1259 }
1260
1261 int ff_seek_frame_binary(AVFormatContext *s, int stream_index,
1262                          int64_t target_ts, int flags)
1263 {
1264     AVInputFormat *avif = s->iformat;
1265     int64_t av_uninit(pos_min), av_uninit(pos_max), pos, pos_limit;
1266     int64_t ts_min, ts_max, ts;
1267     int index;
1268     int64_t ret;
1269     AVStream *st;
1270
1271     if (stream_index < 0)
1272         return -1;
1273
1274     av_log(s, AV_LOG_TRACE, "read_seek: %d %"PRId64"\n", stream_index, target_ts);
1275
1276     ts_max =
1277     ts_min = AV_NOPTS_VALUE;
1278     pos_limit = -1; // GCC falsely says it may be uninitialized.
1279
1280     st = s->streams[stream_index];
1281     if (st->index_entries) {
1282         AVIndexEntry *e;
1283
1284         /* FIXME: Whole function must be checked for non-keyframe entries in
1285          * index case, especially read_timestamp(). */
1286         index = av_index_search_timestamp(st, target_ts,
1287                                           flags | AVSEEK_FLAG_BACKWARD);
1288         index = FFMAX(index, 0);
1289         e     = &st->index_entries[index];
1290
1291         if (e->timestamp <= target_ts || e->pos == e->min_distance) {
1292             pos_min = e->pos;
1293             ts_min  = e->timestamp;
1294             av_log(s, AV_LOG_TRACE, "using cached pos_min=0x%"PRIx64" dts_min=%"PRId64"\n",
1295                     pos_min, ts_min);
1296         } else {
1297             assert(index == 0);
1298         }
1299
1300         index = av_index_search_timestamp(st, target_ts,
1301                                           flags & ~AVSEEK_FLAG_BACKWARD);
1302         assert(index < st->nb_index_entries);
1303         if (index >= 0) {
1304             e = &st->index_entries[index];
1305             assert(e->timestamp >= target_ts);
1306             pos_max   = e->pos;
1307             ts_max    = e->timestamp;
1308             pos_limit = pos_max - e->min_distance;
1309             av_log(s, AV_LOG_TRACE, "using cached pos_max=0x%"PRIx64" pos_limit=0x%"PRIx64
1310                     " dts_max=%"PRId64"\n", pos_max, pos_limit, ts_max);
1311         }
1312     }
1313
1314     pos = ff_gen_search(s, stream_index, target_ts, pos_min, pos_max, pos_limit,
1315                         ts_min, ts_max, flags, &ts, avif->read_timestamp);
1316     if (pos < 0)
1317         return -1;
1318
1319     /* do the seek */
1320     if ((ret = avio_seek(s->pb, pos, SEEK_SET)) < 0)
1321         return ret;
1322
1323     ff_update_cur_dts(s, st, ts);
1324
1325     return 0;
1326 }
1327
1328 int64_t ff_gen_search(AVFormatContext *s, int stream_index, int64_t target_ts,
1329                       int64_t pos_min, int64_t pos_max, int64_t pos_limit,
1330                       int64_t ts_min, int64_t ts_max,
1331                       int flags, int64_t *ts_ret,
1332                       int64_t (*read_timestamp)(struct AVFormatContext *, int,
1333                                                 int64_t *, int64_t))
1334 {
1335     int64_t pos, ts;
1336     int64_t start_pos, filesize;
1337     int no_change;
1338
1339     av_log(s, AV_LOG_TRACE, "gen_seek: %d %"PRId64"\n", stream_index, target_ts);
1340
1341     if (ts_min == AV_NOPTS_VALUE) {
1342         pos_min = s->internal->data_offset;
1343         ts_min  = read_timestamp(s, stream_index, &pos_min, INT64_MAX);
1344         if (ts_min == AV_NOPTS_VALUE)
1345             return -1;
1346     }
1347
1348     if (ts_max == AV_NOPTS_VALUE) {
1349         int step = 1024;
1350         filesize = avio_size(s->pb);
1351         pos_max  = filesize - 1;
1352         do {
1353             pos_max -= step;
1354             ts_max   = read_timestamp(s, stream_index, &pos_max,
1355                                       pos_max + step);
1356             step    += step;
1357         } while (ts_max == AV_NOPTS_VALUE && pos_max >= step);
1358         if (ts_max == AV_NOPTS_VALUE)
1359             return -1;
1360
1361         for (;;) {
1362             int64_t tmp_pos = pos_max + 1;
1363             int64_t tmp_ts  = read_timestamp(s, stream_index,
1364                                              &tmp_pos, INT64_MAX);
1365             if (tmp_ts == AV_NOPTS_VALUE)
1366                 break;
1367             ts_max  = tmp_ts;
1368             pos_max = tmp_pos;
1369             if (tmp_pos >= filesize)
1370                 break;
1371         }
1372         pos_limit = pos_max;
1373     }
1374
1375     if (ts_min > ts_max)
1376         return -1;
1377     else if (ts_min == ts_max)
1378         pos_limit = pos_min;
1379
1380     no_change = 0;
1381     while (pos_min < pos_limit) {
1382         av_log(s, AV_LOG_TRACE, "pos_min=0x%"PRIx64" pos_max=0x%"PRIx64" dts_min=%"PRId64
1383                 " dts_max=%"PRId64"\n", pos_min, pos_max, ts_min, ts_max);
1384         assert(pos_limit <= pos_max);
1385
1386         if (no_change == 0) {
1387             int64_t approximate_keyframe_distance = pos_max - pos_limit;
1388             // interpolate position (better than dichotomy)
1389             pos = av_rescale(target_ts - ts_min, pos_max - pos_min,
1390                              ts_max - ts_min) +
1391                   pos_min - approximate_keyframe_distance;
1392         } else if (no_change == 1) {
1393             // bisection if interpolation did not change min / max pos last time
1394             pos = (pos_min + pos_limit) >> 1;
1395         } else {
1396             /* linear search if bisection failed, can only happen if there
1397              * are very few or no keyframes between min/max */
1398             pos = pos_min;
1399         }
1400         if (pos <= pos_min)
1401             pos = pos_min + 1;
1402         else if (pos > pos_limit)
1403             pos = pos_limit;
1404         start_pos = pos;
1405
1406         // May pass pos_limit instead of -1.
1407         ts = read_timestamp(s, stream_index, &pos, INT64_MAX);
1408         if (pos == pos_max)
1409             no_change++;
1410         else
1411             no_change = 0;
1412         av_log(s, AV_LOG_TRACE, "%"PRId64" %"PRId64" %"PRId64" / %"PRId64" %"PRId64" %"PRId64
1413                 " target:%"PRId64" limit:%"PRId64" start:%"PRId64" noc:%d\n",
1414                 pos_min, pos, pos_max, ts_min, ts, ts_max, target_ts,
1415                 pos_limit, start_pos, no_change);
1416         if (ts == AV_NOPTS_VALUE) {
1417             av_log(s, AV_LOG_ERROR, "read_timestamp() failed in the middle\n");
1418             return -1;
1419         }
1420         assert(ts != AV_NOPTS_VALUE);
1421         if (target_ts <= ts) {
1422             pos_limit = start_pos - 1;
1423             pos_max   = pos;
1424             ts_max    = ts;
1425         }
1426         if (target_ts >= ts) {
1427             pos_min = pos;
1428             ts_min  = ts;
1429         }
1430     }
1431
1432     pos     = (flags & AVSEEK_FLAG_BACKWARD) ? pos_min : pos_max;
1433     ts      = (flags & AVSEEK_FLAG_BACKWARD) ? ts_min  : ts_max;
1434     pos_min = pos;
1435     ts_min  = read_timestamp(s, stream_index, &pos_min, INT64_MAX);
1436     pos_min++;
1437     ts_max = read_timestamp(s, stream_index, &pos_min, INT64_MAX);
1438     av_log(s, AV_LOG_TRACE, "pos=0x%"PRIx64" %"PRId64"<=%"PRId64"<=%"PRId64"\n",
1439             pos, ts_min, target_ts, ts_max);
1440     *ts_ret = ts;
1441     return pos;
1442 }
1443
1444 static int seek_frame_byte(AVFormatContext *s, int stream_index,
1445                            int64_t pos, int flags)
1446 {
1447     int64_t pos_min, pos_max;
1448
1449     pos_min = s->internal->data_offset;
1450     pos_max = avio_size(s->pb) - 1;
1451
1452     if (pos < pos_min)
1453         pos = pos_min;
1454     else if (pos > pos_max)
1455         pos = pos_max;
1456
1457     avio_seek(s->pb, pos, SEEK_SET);
1458
1459     return 0;
1460 }
1461
1462 static int seek_frame_generic(AVFormatContext *s, int stream_index,
1463                               int64_t timestamp, int flags)
1464 {
1465     int index;
1466     int64_t ret;
1467     AVStream *st;
1468     AVIndexEntry *ie;
1469
1470     st = s->streams[stream_index];
1471
1472     index = av_index_search_timestamp(st, timestamp, flags);
1473
1474     if (index < 0 && st->nb_index_entries &&
1475         timestamp < st->index_entries[0].timestamp)
1476         return -1;
1477
1478     if (index < 0 || index == st->nb_index_entries - 1) {
1479         AVPacket pkt;
1480
1481         if (st->nb_index_entries) {
1482             assert(st->index_entries);
1483             ie = &st->index_entries[st->nb_index_entries - 1];
1484             if ((ret = avio_seek(s->pb, ie->pos, SEEK_SET)) < 0)
1485                 return ret;
1486             ff_update_cur_dts(s, st, ie->timestamp);
1487         } else {
1488             if ((ret = avio_seek(s->pb, s->internal->data_offset, SEEK_SET)) < 0)
1489                 return ret;
1490         }
1491         for (;;) {
1492             int read_status;
1493             do {
1494                 read_status = av_read_frame(s, &pkt);
1495             } while (read_status == AVERROR(EAGAIN));
1496             if (read_status < 0)
1497                 break;
1498             av_packet_unref(&pkt);
1499             if (stream_index == pkt.stream_index)
1500                 if ((pkt.flags & AV_PKT_FLAG_KEY) && pkt.dts > timestamp)
1501                     break;
1502         }
1503         index = av_index_search_timestamp(st, timestamp, flags);
1504     }
1505     if (index < 0)
1506         return -1;
1507
1508     ff_read_frame_flush(s);
1509     if (s->iformat->read_seek)
1510         if (s->iformat->read_seek(s, stream_index, timestamp, flags) >= 0)
1511             return 0;
1512     ie = &st->index_entries[index];
1513     if ((ret = avio_seek(s->pb, ie->pos, SEEK_SET)) < 0)
1514         return ret;
1515     ff_update_cur_dts(s, st, ie->timestamp);
1516
1517     return 0;
1518 }
1519
1520 static int seek_frame_internal(AVFormatContext *s, int stream_index,
1521                                int64_t timestamp, int flags)
1522 {
1523     int ret;
1524     AVStream *st;
1525
1526     if (flags & AVSEEK_FLAG_BYTE) {
1527         if (s->iformat->flags & AVFMT_NO_BYTE_SEEK)
1528             return -1;
1529         ff_read_frame_flush(s);
1530         return seek_frame_byte(s, stream_index, timestamp, flags);
1531     }
1532
1533     if (stream_index < 0) {
1534         stream_index = av_find_default_stream_index(s);
1535         if (stream_index < 0)
1536             return -1;
1537
1538         st = s->streams[stream_index];
1539         /* timestamp for default must be expressed in AV_TIME_BASE units */
1540         timestamp = av_rescale(timestamp, st->time_base.den,
1541                                AV_TIME_BASE * (int64_t) st->time_base.num);
1542     }
1543
1544     /* first, we try the format specific seek */
1545     if (s->iformat->read_seek) {
1546         ff_read_frame_flush(s);
1547         ret = s->iformat->read_seek(s, stream_index, timestamp, flags);
1548     } else
1549         ret = -1;
1550     if (ret >= 0)
1551         return 0;
1552
1553     if (s->iformat->read_timestamp &&
1554         !(s->iformat->flags & AVFMT_NOBINSEARCH)) {
1555         ff_read_frame_flush(s);
1556         return ff_seek_frame_binary(s, stream_index, timestamp, flags);
1557     } else if (!(s->iformat->flags & AVFMT_NOGENSEARCH)) {
1558         ff_read_frame_flush(s);
1559         return seek_frame_generic(s, stream_index, timestamp, flags);
1560     } else
1561         return -1;
1562 }
1563
1564 int av_seek_frame(AVFormatContext *s, int stream_index,
1565                   int64_t timestamp, int flags)
1566 {
1567     int ret = seek_frame_internal(s, stream_index, timestamp, flags);
1568
1569     if (ret >= 0)
1570         ret = queue_attached_pictures(s);
1571
1572     return ret;
1573 }
1574
1575 int avformat_seek_file(AVFormatContext *s, int stream_index, int64_t min_ts,
1576                        int64_t ts, int64_t max_ts, int flags)
1577 {
1578     if (min_ts > ts || max_ts < ts)
1579         return -1;
1580
1581     if (s->iformat->read_seek2) {
1582         int ret;
1583         ff_read_frame_flush(s);
1584         ret = s->iformat->read_seek2(s, stream_index, min_ts,
1585                                      ts, max_ts, flags);
1586
1587         if (ret >= 0)
1588             ret = queue_attached_pictures(s);
1589         return ret;
1590     }
1591
1592     if (s->iformat->read_timestamp) {
1593         // try to seek via read_timestamp()
1594     }
1595
1596     // Fall back on old API if new is not implemented but old is.
1597     // Note the old API has somewhat different semantics.
1598     if (s->iformat->read_seek || 1)
1599         return av_seek_frame(s, stream_index, ts,
1600                              flags | ((uint64_t) ts - min_ts >
1601                                       (uint64_t) max_ts - ts
1602                                       ? AVSEEK_FLAG_BACKWARD : 0));
1603
1604     // try some generic seek like seek_frame_generic() but with new ts semantics
1605 }
1606
1607 /*******************************************************/
1608
1609 /**
1610  * Return TRUE if the stream has accurate duration in any stream.
1611  *
1612  * @return TRUE if the stream has accurate duration for at least one component.
1613  */
1614 static int has_duration(AVFormatContext *ic)
1615 {
1616     int i;
1617     AVStream *st;
1618
1619     for (i = 0; i < ic->nb_streams; i++) {
1620         st = ic->streams[i];
1621         if (st->duration != AV_NOPTS_VALUE)
1622             return 1;
1623     }
1624     if (ic->duration != AV_NOPTS_VALUE)
1625         return 1;
1626     return 0;
1627 }
1628
1629 /**
1630  * Estimate the stream timings from the one of each components.
1631  *
1632  * Also computes the global bitrate if possible.
1633  */
1634 static void update_stream_timings(AVFormatContext *ic)
1635 {
1636     int64_t start_time, start_time1, end_time, end_time1;
1637     int64_t duration, duration1, filesize;
1638     int i;
1639     AVStream *st;
1640
1641     start_time = INT64_MAX;
1642     end_time   = INT64_MIN;
1643     duration   = INT64_MIN;
1644     for (i = 0; i < ic->nb_streams; i++) {
1645         st = ic->streams[i];
1646         if (st->start_time != AV_NOPTS_VALUE && st->time_base.den) {
1647             start_time1 = av_rescale_q(st->start_time, st->time_base,
1648                                        AV_TIME_BASE_Q);
1649             start_time  = FFMIN(start_time, start_time1);
1650             if (st->duration != AV_NOPTS_VALUE) {
1651                 end_time1 = start_time1 +
1652                             av_rescale_q(st->duration, st->time_base,
1653                                          AV_TIME_BASE_Q);
1654                 end_time = FFMAX(end_time, end_time1);
1655             }
1656         }
1657         if (st->duration != AV_NOPTS_VALUE) {
1658             duration1 = av_rescale_q(st->duration, st->time_base,
1659                                      AV_TIME_BASE_Q);
1660             duration  = FFMAX(duration, duration1);
1661         }
1662     }
1663     if (start_time != INT64_MAX) {
1664         ic->start_time = start_time;
1665         if (end_time != INT64_MIN)
1666             duration = FFMAX(duration, end_time - start_time);
1667     }
1668     if (duration != INT64_MIN) {
1669         ic->duration = duration;
1670         if (ic->pb && (filesize = avio_size(ic->pb)) > 0)
1671             /* compute the bitrate */
1672             ic->bit_rate = (double) filesize * 8.0 * AV_TIME_BASE /
1673                            (double) ic->duration;
1674     }
1675 }
1676
1677 static void fill_all_stream_timings(AVFormatContext *ic)
1678 {
1679     int i;
1680     AVStream *st;
1681
1682     update_stream_timings(ic);
1683     for (i = 0; i < ic->nb_streams; i++) {
1684         st = ic->streams[i];
1685         if (st->start_time == AV_NOPTS_VALUE) {
1686             if (ic->start_time != AV_NOPTS_VALUE)
1687                 st->start_time = av_rescale_q(ic->start_time, AV_TIME_BASE_Q,
1688                                               st->time_base);
1689             if (ic->duration != AV_NOPTS_VALUE)
1690                 st->duration = av_rescale_q(ic->duration, AV_TIME_BASE_Q,
1691                                             st->time_base);
1692         }
1693     }
1694 }
1695
1696 static void estimate_timings_from_bit_rate(AVFormatContext *ic)
1697 {
1698     int64_t filesize, duration;
1699     int i;
1700     AVStream *st;
1701
1702     /* if bit_rate is already set, we believe it */
1703     if (ic->bit_rate <= 0) {
1704         int bit_rate = 0;
1705         for (i = 0; i < ic->nb_streams; i++) {
1706             st = ic->streams[i];
1707             if (st->codecpar->bit_rate > 0) {
1708                 if (INT_MAX - st->codecpar->bit_rate < bit_rate) {
1709                     bit_rate = 0;
1710                     break;
1711                 }
1712                 bit_rate += st->codecpar->bit_rate;
1713             }
1714         }
1715         ic->bit_rate = bit_rate;
1716     }
1717
1718     /* if duration is already set, we believe it */
1719     if (ic->duration == AV_NOPTS_VALUE &&
1720         ic->bit_rate != 0) {
1721         filesize = ic->pb ? avio_size(ic->pb) : 0;
1722         if (filesize > 0) {
1723             for (i = 0; i < ic->nb_streams; i++) {
1724                 st       = ic->streams[i];
1725                 duration = av_rescale(8 * filesize, st->time_base.den,
1726                                       ic->bit_rate *
1727                                       (int64_t) st->time_base.num);
1728                 if (st->duration == AV_NOPTS_VALUE)
1729                     st->duration = duration;
1730             }
1731         }
1732     }
1733 }
1734
1735 #define DURATION_MAX_READ_SIZE 250000
1736 #define DURATION_MAX_RETRY 3
1737
1738 /* only usable for MPEG-PS streams */
1739 static void estimate_timings_from_pts(AVFormatContext *ic, int64_t old_offset)
1740 {
1741     AVPacket pkt1, *pkt = &pkt1;
1742     AVStream *st;
1743     int read_size, i, ret;
1744     int64_t end_time;
1745     int64_t filesize, offset, duration;
1746     int retry = 0;
1747
1748     /* flush packet queue */
1749     flush_packet_queue(ic);
1750
1751     for (i = 0; i < ic->nb_streams; i++) {
1752         st = ic->streams[i];
1753         if (st->start_time == AV_NOPTS_VALUE && st->first_dts == AV_NOPTS_VALUE)
1754             av_log(ic, AV_LOG_WARNING,
1755                    "start time is not set in estimate_timings_from_pts\n");
1756
1757         if (st->parser) {
1758             av_parser_close(st->parser);
1759             st->parser = NULL;
1760         }
1761     }
1762
1763     /* estimate the end time (duration) */
1764     /* XXX: may need to support wrapping */
1765     filesize = ic->pb ? avio_size(ic->pb) : 0;
1766     end_time = AV_NOPTS_VALUE;
1767     do {
1768         offset = filesize - (DURATION_MAX_READ_SIZE << retry);
1769         if (offset < 0)
1770             offset = 0;
1771
1772         avio_seek(ic->pb, offset, SEEK_SET);
1773         read_size = 0;
1774         for (;;) {
1775             if (read_size >= DURATION_MAX_READ_SIZE << (FFMAX(retry - 1, 0)))
1776                 break;
1777
1778             do {
1779                 ret = ff_read_packet(ic, pkt);
1780             } while (ret == AVERROR(EAGAIN));
1781             if (ret != 0)
1782                 break;
1783             read_size += pkt->size;
1784             st         = ic->streams[pkt->stream_index];
1785             if (pkt->pts != AV_NOPTS_VALUE &&
1786                 (st->start_time != AV_NOPTS_VALUE ||
1787                  st->first_dts  != AV_NOPTS_VALUE)) {
1788                 duration = end_time = pkt->pts;
1789                 if (st->start_time != AV_NOPTS_VALUE)
1790                     duration -= st->start_time;
1791                 else
1792                     duration -= st->first_dts;
1793                 if (duration < 0)
1794                     duration += 1LL << st->pts_wrap_bits;
1795                 if (duration > 0) {
1796                     if (st->duration == AV_NOPTS_VALUE || st->duration < duration)
1797                         st->duration = duration;
1798                 }
1799             }
1800             av_packet_unref(pkt);
1801         }
1802     } while (end_time == AV_NOPTS_VALUE &&
1803              filesize > (DURATION_MAX_READ_SIZE << retry) &&
1804              ++retry <= DURATION_MAX_RETRY);
1805
1806     fill_all_stream_timings(ic);
1807
1808     avio_seek(ic->pb, old_offset, SEEK_SET);
1809     for (i = 0; i < ic->nb_streams; i++) {
1810         st              = ic->streams[i];
1811         st->cur_dts     = st->first_dts;
1812         st->last_IP_pts = AV_NOPTS_VALUE;
1813     }
1814 }
1815
1816 static void estimate_timings(AVFormatContext *ic, int64_t old_offset)
1817 {
1818     int64_t file_size;
1819
1820     /* get the file size, if possible */
1821     if (ic->iformat->flags & AVFMT_NOFILE) {
1822         file_size = 0;
1823     } else {
1824         file_size = avio_size(ic->pb);
1825         file_size = FFMAX(0, file_size);
1826     }
1827
1828     if ((!strcmp(ic->iformat->name, "mpeg") ||
1829          !strcmp(ic->iformat->name, "mpegts")) &&
1830         file_size && (ic->pb->seekable & AVIO_SEEKABLE_NORMAL)) {
1831         /* get accurate estimate from the PTSes */
1832         estimate_timings_from_pts(ic, old_offset);
1833     } else if (has_duration(ic)) {
1834         /* at least one component has timings - we use them for all
1835          * the components */
1836         fill_all_stream_timings(ic);
1837     } else {
1838         av_log(ic, AV_LOG_WARNING,
1839                "Estimating duration from bitrate, this may be inaccurate\n");
1840         /* less precise: use bitrate info */
1841         estimate_timings_from_bit_rate(ic);
1842     }
1843     update_stream_timings(ic);
1844
1845     {
1846         int i;
1847         AVStream av_unused *st;
1848         for (i = 0; i < ic->nb_streams; i++) {
1849             st = ic->streams[i];
1850             av_log(ic, AV_LOG_TRACE, "%d: start_time: %0.3f duration: %0.3f\n", i,
1851                     (double) st->start_time / AV_TIME_BASE,
1852                     (double) st->duration   / AV_TIME_BASE);
1853         }
1854         av_log(ic, AV_LOG_TRACE,
1855                 "stream: start_time: %0.3f duration: %0.3f bitrate=%d kb/s\n",
1856                 (double) ic->start_time / AV_TIME_BASE,
1857                 (double) ic->duration   / AV_TIME_BASE,
1858                 ic->bit_rate / 1000);
1859     }
1860 }
1861
1862 static int has_codec_parameters(AVStream *st)
1863 {
1864     AVCodecContext *avctx = st->internal->avctx;
1865     int val;
1866
1867     switch (avctx->codec_type) {
1868     case AVMEDIA_TYPE_AUDIO:
1869         val = avctx->sample_rate && avctx->channels;
1870         if (st->info->found_decoder >= 0 &&
1871             avctx->sample_fmt == AV_SAMPLE_FMT_NONE)
1872             return 0;
1873         break;
1874     case AVMEDIA_TYPE_VIDEO:
1875         val = avctx->width;
1876         if (st->info->found_decoder >= 0 && avctx->pix_fmt == AV_PIX_FMT_NONE)
1877             return 0;
1878         break;
1879     default:
1880         val = 1;
1881         break;
1882     }
1883     return avctx->codec_id != AV_CODEC_ID_NONE && val != 0;
1884 }
1885
1886 static int has_decode_delay_been_guessed(AVStream *st)
1887 {
1888     return st->internal->avctx->codec_id != AV_CODEC_ID_H264 ||
1889            st->info->nb_decoded_frames >= 6;
1890 }
1891
1892 /* returns 1 or 0 if or if not decoded data was returned, or a negative error */
1893 static int try_decode_frame(AVFormatContext *s, AVStream *st, AVPacket *avpkt,
1894                             AVDictionary **options)
1895 {
1896     AVCodecContext *avctx = st->internal->avctx;
1897     const AVCodec *codec;
1898     int got_picture = 1, ret = 0;
1899     AVFrame *frame = av_frame_alloc();
1900     AVPacket pkt = *avpkt;
1901
1902     if (!frame)
1903         return AVERROR(ENOMEM);
1904
1905     if (!avcodec_is_open(avctx) && !st->info->found_decoder) {
1906         AVDictionary *thread_opt = NULL;
1907
1908 #if FF_API_LAVF_AVCTX
1909 FF_DISABLE_DEPRECATION_WARNINGS
1910         codec = st->codec->codec ? st->codec->codec
1911                                  : avcodec_find_decoder(st->codecpar->codec_id);
1912 FF_ENABLE_DEPRECATION_WARNINGS
1913 #else
1914         codec = avcodec_find_decoder(st->codecpar->codec_id);
1915 #endif
1916
1917         if (!codec) {
1918             st->info->found_decoder = -1;
1919             ret                     = -1;
1920             goto fail;
1921         }
1922
1923         /* Force thread count to 1 since the H.264 decoder will not extract
1924          * SPS and PPS to extradata during multi-threaded decoding. */
1925         av_dict_set(options ? options : &thread_opt, "threads", "1", 0);
1926         ret = avcodec_open2(avctx, codec, options ? options : &thread_opt);
1927         if (!options)
1928             av_dict_free(&thread_opt);
1929         if (ret < 0) {
1930             st->info->found_decoder = -1;
1931             goto fail;
1932         }
1933         st->info->found_decoder = 1;
1934     } else if (!st->info->found_decoder)
1935         st->info->found_decoder = 1;
1936
1937     if (st->info->found_decoder < 0) {
1938         ret = -1;
1939         goto fail;
1940     }
1941
1942     while ((pkt.size > 0 || (!pkt.data && got_picture)) &&
1943            ret >= 0 &&
1944            (!has_codec_parameters(st) || !has_decode_delay_been_guessed(st) ||
1945             (!st->codec_info_nb_frames &&
1946              (avctx->codec->capabilities & AV_CODEC_CAP_CHANNEL_CONF)))) {
1947         got_picture = 0;
1948         if (avctx->codec_type == AVMEDIA_TYPE_VIDEO ||
1949             avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
1950             ret = avcodec_send_packet(avctx, &pkt);
1951             if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
1952                 break;
1953             if (ret >= 0)
1954                 pkt.size = 0;
1955             ret = avcodec_receive_frame(avctx, frame);
1956             if (ret >= 0)
1957                 got_picture = 1;
1958             if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
1959                 ret = 0;
1960         }
1961         if (ret >= 0) {
1962             if (got_picture)
1963                 st->info->nb_decoded_frames++;
1964             ret       = got_picture;
1965         }
1966     }
1967
1968 fail:
1969     av_frame_free(&frame);
1970     return ret;
1971 }
1972
1973 unsigned int ff_codec_get_tag(const AVCodecTag *tags, enum AVCodecID id)
1974 {
1975     while (tags->id != AV_CODEC_ID_NONE) {
1976         if (tags->id == id)
1977             return tags->tag;
1978         tags++;
1979     }
1980     return 0;
1981 }
1982
1983 enum AVCodecID ff_codec_get_id(const AVCodecTag *tags, unsigned int tag)
1984 {
1985     int i;
1986     for (i = 0; tags[i].id != AV_CODEC_ID_NONE; i++)
1987         if (tag == tags[i].tag)
1988             return tags[i].id;
1989     for (i = 0; tags[i].id != AV_CODEC_ID_NONE; i++)
1990         if (avpriv_toupper4(tag) == avpriv_toupper4(tags[i].tag))
1991             return tags[i].id;
1992     return AV_CODEC_ID_NONE;
1993 }
1994
1995 enum AVCodecID ff_get_pcm_codec_id(int bps, int flt, int be, int sflags)
1996 {
1997     if (flt) {
1998         switch (bps) {
1999         case 32:
2000             return be ? AV_CODEC_ID_PCM_F32BE : AV_CODEC_ID_PCM_F32LE;
2001         case 64:
2002             return be ? AV_CODEC_ID_PCM_F64BE : AV_CODEC_ID_PCM_F64LE;
2003         default:
2004             return AV_CODEC_ID_NONE;
2005         }
2006     } else {
2007         bps >>= 3;
2008         if (sflags & (1 << (bps - 1))) {
2009             switch (bps) {
2010             case 1:
2011                 return AV_CODEC_ID_PCM_S8;
2012             case 2:
2013                 return be ? AV_CODEC_ID_PCM_S16BE : AV_CODEC_ID_PCM_S16LE;
2014             case 3:
2015                 return be ? AV_CODEC_ID_PCM_S24BE : AV_CODEC_ID_PCM_S24LE;
2016             case 4:
2017                 return be ? AV_CODEC_ID_PCM_S32BE : AV_CODEC_ID_PCM_S32LE;
2018             default:
2019                 return AV_CODEC_ID_NONE;
2020             }
2021         } else {
2022             switch (bps) {
2023             case 1:
2024                 return AV_CODEC_ID_PCM_U8;
2025             case 2:
2026                 return be ? AV_CODEC_ID_PCM_U16BE : AV_CODEC_ID_PCM_U16LE;
2027             case 3:
2028                 return be ? AV_CODEC_ID_PCM_U24BE : AV_CODEC_ID_PCM_U24LE;
2029             case 4:
2030                 return be ? AV_CODEC_ID_PCM_U32BE : AV_CODEC_ID_PCM_U32LE;
2031             default:
2032                 return AV_CODEC_ID_NONE;
2033             }
2034         }
2035     }
2036 }
2037
2038 unsigned int av_codec_get_tag(const AVCodecTag *const *tags, enum AVCodecID id)
2039 {
2040     int i;
2041     for (i = 0; tags && tags[i]; i++) {
2042         int tag = ff_codec_get_tag(tags[i], id);
2043         if (tag)
2044             return tag;
2045     }
2046     return 0;
2047 }
2048
2049 enum AVCodecID av_codec_get_id(const AVCodecTag *const *tags, unsigned int tag)
2050 {
2051     int i;
2052     for (i = 0; tags && tags[i]; i++) {
2053         enum AVCodecID id = ff_codec_get_id(tags[i], tag);
2054         if (id != AV_CODEC_ID_NONE)
2055             return id;
2056     }
2057     return AV_CODEC_ID_NONE;
2058 }
2059
2060 static void compute_chapters_end(AVFormatContext *s)
2061 {
2062     unsigned int i, j;
2063     int64_t max_time = s->duration +
2064                        ((s->start_time == AV_NOPTS_VALUE) ? 0 : s->start_time);
2065
2066     for (i = 0; i < s->nb_chapters; i++)
2067         if (s->chapters[i]->end == AV_NOPTS_VALUE) {
2068             AVChapter *ch = s->chapters[i];
2069             int64_t end = max_time ? av_rescale_q(max_time, AV_TIME_BASE_Q,
2070                                                   ch->time_base)
2071                                    : INT64_MAX;
2072
2073             for (j = 0; j < s->nb_chapters; j++) {
2074                 AVChapter *ch1     = s->chapters[j];
2075                 int64_t next_start = av_rescale_q(ch1->start, ch1->time_base,
2076                                                   ch->time_base);
2077                 if (j != i && next_start > ch->start && next_start < end)
2078                     end = next_start;
2079             }
2080             ch->end = (end == INT64_MAX) ? ch->start : end;
2081         }
2082 }
2083
2084 static int get_std_framerate(int i)
2085 {
2086     if (i < 60 * 12)
2087         return (i + 1) * 1001;
2088     else
2089         return ((const int[]) { 24, 30, 60, 12, 15 })[i - 60 * 12] * 1000 * 12;
2090 }
2091
2092 static int extract_extradata_init(AVStream *st)
2093 {
2094     AVStreamInternal *i = st->internal;
2095     const AVBitStreamFilter *f;
2096     int ret;
2097
2098     f = av_bsf_get_by_name("extract_extradata");
2099     if (!f)
2100         goto finish;
2101
2102     /* check that the codec id is supported */
2103     if (f->codec_ids) {
2104         const enum AVCodecID *ids;
2105         for (ids = f->codec_ids; *ids != AV_CODEC_ID_NONE; ids++)
2106             if (*ids == st->codecpar->codec_id)
2107                 break;
2108         if (*ids == AV_CODEC_ID_NONE)
2109             goto finish;
2110     }
2111
2112     i->extract_extradata.pkt = av_packet_alloc();
2113     if (!i->extract_extradata.pkt)
2114         return AVERROR(ENOMEM);
2115
2116     ret = av_bsf_alloc(f, &i->extract_extradata.bsf);
2117     if (ret < 0)
2118         goto fail;
2119
2120     ret = avcodec_parameters_copy(i->extract_extradata.bsf->par_in,
2121                                   st->codecpar);
2122     if (ret < 0)
2123         goto fail;
2124
2125     i->extract_extradata.bsf->time_base_in = st->time_base;
2126
2127     /* if init fails here, we assume extracting extradata is just not
2128      * supported for this codec, so we return success */
2129     ret = av_bsf_init(i->extract_extradata.bsf);
2130     if (ret < 0) {
2131         av_bsf_free(&i->extract_extradata.bsf);
2132         ret = 0;
2133     }
2134
2135 finish:
2136     i->extract_extradata.inited = 1;
2137
2138     return 0;
2139 fail:
2140     av_bsf_free(&i->extract_extradata.bsf);
2141     av_packet_free(&i->extract_extradata.pkt);
2142     return ret;
2143 }
2144
2145 static int extract_extradata(AVStream *st, AVPacket *pkt)
2146 {
2147     AVStreamInternal *i = st->internal;
2148     AVPacket *pkt_ref;
2149     int ret;
2150
2151     if (!i->extract_extradata.inited) {
2152         ret = extract_extradata_init(st);
2153         if (ret < 0)
2154             return ret;
2155     }
2156
2157     if (i->extract_extradata.inited && !i->extract_extradata.bsf)
2158         return 0;
2159
2160     pkt_ref = i->extract_extradata.pkt;
2161     ret = av_packet_ref(pkt_ref, pkt);
2162     if (ret < 0)
2163         return ret;
2164
2165     ret = av_bsf_send_packet(i->extract_extradata.bsf, pkt_ref);
2166     if (ret < 0) {
2167         av_packet_unref(pkt_ref);
2168         return ret;
2169     }
2170
2171     while (ret >= 0 && !i->avctx->extradata) {
2172         int extradata_size;
2173         uint8_t *extradata;
2174
2175         ret = av_bsf_receive_packet(i->extract_extradata.bsf, pkt_ref);
2176         if (ret < 0) {
2177             if (ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
2178                 return ret;
2179             continue;
2180         }
2181
2182         extradata = av_packet_get_side_data(pkt_ref, AV_PKT_DATA_NEW_EXTRADATA,
2183                                             &extradata_size);
2184
2185         if (extradata) {
2186             i->avctx->extradata = av_mallocz(extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
2187             if (!i->avctx->extradata) {
2188                 av_packet_unref(pkt_ref);
2189                 return AVERROR(ENOMEM);
2190             }
2191             memcpy(i->avctx->extradata, extradata, extradata_size);
2192             i->avctx->extradata_size = extradata_size;
2193         }
2194         av_packet_unref(pkt_ref);
2195     }
2196
2197     return 0;
2198 }
2199
2200 int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
2201 {
2202     int i, count, ret, read_size, j;
2203     AVStream *st;
2204     AVCodecContext *avctx;
2205     AVPacket pkt1, *pkt;
2206     int64_t old_offset  = avio_tell(ic->pb);
2207     // new streams might appear, no options for those
2208     int orig_nb_streams = ic->nb_streams;
2209
2210     for (i = 0; i < ic->nb_streams; i++) {
2211         const AVCodec *codec;
2212         AVDictionary *thread_opt = NULL;
2213         st = ic->streams[i];
2214         avctx = st->internal->avctx;
2215
2216         // only for the split stuff
2217         if (!st->parser && !(ic->flags & AVFMT_FLAG_NOPARSE)) {
2218             st->parser = av_parser_init(st->codecpar->codec_id);
2219             if (st->need_parsing == AVSTREAM_PARSE_HEADERS && st->parser)
2220                 st->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES;
2221         }
2222
2223         /* check if the caller has overridden the codec id */
2224 #if FF_API_LAVF_AVCTX
2225 FF_DISABLE_DEPRECATION_WARNINGS
2226         if (st->codec->codec_id != st->internal->orig_codec_id) {
2227             st->codecpar->codec_id   = st->codec->codec_id;
2228             st->codecpar->codec_type = st->codec->codec_type;
2229             st->internal->orig_codec_id = st->codec->codec_id;
2230         }
2231 FF_ENABLE_DEPRECATION_WARNINGS
2232 #endif
2233         if (st->codecpar->codec_id != st->internal->orig_codec_id)
2234             st->internal->orig_codec_id = st->codecpar->codec_id;
2235
2236         ret = avcodec_parameters_to_context(avctx, st->codecpar);
2237         if (ret < 0)
2238             goto find_stream_info_err;
2239         if (st->codecpar->codec_id != AV_CODEC_ID_PROBE &&
2240             st->codecpar->codec_id != AV_CODEC_ID_NONE)
2241             st->internal->avctx_inited = 1;
2242
2243 #if FF_API_LAVF_AVCTX
2244 FF_DISABLE_DEPRECATION_WARNINGS
2245         codec = st->codec->codec ? st->codec->codec
2246                                  : avcodec_find_decoder(st->codecpar->codec_id);
2247 FF_ENABLE_DEPRECATION_WARNINGS
2248 #else
2249         codec = avcodec_find_decoder(st->codecpar->codec_id);
2250 #endif
2251
2252         /* Force thread count to 1 since the H.264 decoder will not extract
2253          * SPS and PPS to extradata during multi-threaded decoding. */
2254         av_dict_set(options ? &options[i] : &thread_opt, "threads", "1", 0);
2255
2256         /* Ensure that subtitle_header is properly set. */
2257         if (st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE
2258             && codec && !avctx->codec)
2259             avcodec_open2(avctx, codec,
2260                           options ? &options[i] : &thread_opt);
2261
2262         // Try to just open decoders, in case this is enough to get parameters.
2263         if (!has_codec_parameters(st)) {
2264             if (codec && !avctx->codec)
2265                 avcodec_open2(avctx, codec,
2266                               options ? &options[i] : &thread_opt);
2267         }
2268         if (!options)
2269             av_dict_free(&thread_opt);
2270     }
2271
2272     for (i = 0; i < ic->nb_streams; i++) {
2273         ic->streams[i]->info->fps_first_dts = AV_NOPTS_VALUE;
2274         ic->streams[i]->info->fps_last_dts  = AV_NOPTS_VALUE;
2275     }
2276
2277     count     = 0;
2278     read_size = 0;
2279     for (;;) {
2280         if (ff_check_interrupt(&ic->interrupt_callback)) {
2281             ret = AVERROR_EXIT;
2282             av_log(ic, AV_LOG_DEBUG, "interrupted\n");
2283             break;
2284         }
2285
2286         /* check if one codec still needs to be handled */
2287         for (i = 0; i < ic->nb_streams; i++) {
2288             int fps_analyze_framecount = 20;
2289
2290             st = ic->streams[i];
2291             if (!has_codec_parameters(st))
2292                 break;
2293             /* If the timebase is coarse (like the usual millisecond precision
2294              * of mkv), we need to analyze more frames to reliably arrive at
2295              * the correct fps. */
2296             if (av_q2d(st->time_base) > 0.0005)
2297                 fps_analyze_framecount *= 2;
2298             if (ic->fps_probe_size >= 0)
2299                 fps_analyze_framecount = ic->fps_probe_size;
2300             /* variable fps and no guess at the real fps */
2301             if (!st->avg_frame_rate.num &&
2302                 st->codec_info_nb_frames < fps_analyze_framecount &&
2303                 st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
2304                 break;
2305             if (!st->codecpar->extradata &&
2306                 !st->internal->avctx->extradata &&
2307                 (!st->internal->extract_extradata.inited ||
2308                  st->internal->extract_extradata.bsf))
2309                 break;
2310             if (st->first_dts == AV_NOPTS_VALUE &&
2311                 st->codec_info_nb_frames < ic->max_ts_probe &&
2312                 (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ||
2313                  st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO))
2314                 break;
2315         }
2316         if (i == ic->nb_streams) {
2317             /* NOTE: If the format has no header, then we need to read some
2318              * packets to get most of the streams, so we cannot stop here. */
2319             if (!(ic->ctx_flags & AVFMTCTX_NOHEADER)) {
2320                 /* If we found the info for all the codecs, we can stop. */
2321                 ret = count;
2322                 av_log(ic, AV_LOG_DEBUG, "All info found\n");
2323                 break;
2324             }
2325         }
2326         /* We did not get all the codec info, but we read too much data. */
2327         if (read_size >= ic->probesize) {
2328             ret = count;
2329             av_log(ic, AV_LOG_DEBUG,
2330                    "Probe buffer size limit %d reached\n", ic->probesize);
2331             break;
2332         }
2333
2334         /* NOTE: A new stream can be added there if no header in file
2335          * (AVFMTCTX_NOHEADER). */
2336         ret = read_frame_internal(ic, &pkt1);
2337         if (ret == AVERROR(EAGAIN))
2338             continue;
2339
2340         if (ret < 0) {
2341             /* EOF or error*/
2342             AVPacket empty_pkt = { 0 };
2343             int err = 0;
2344             av_init_packet(&empty_pkt);
2345
2346             /* We could not have all the codec parameters before EOF. */
2347             ret = -1;
2348             for (i = 0; i < ic->nb_streams; i++) {
2349                 st = ic->streams[i];
2350
2351                 /* flush the decoders */
2352                 if (st->info->found_decoder == 1) {
2353                     do {
2354                         err = try_decode_frame(ic, st, &empty_pkt,
2355                                                (options && i < orig_nb_streams)
2356                                                ? &options[i] : NULL);
2357                     } while (err > 0 && !has_codec_parameters(st));
2358                 }
2359
2360                 if (err < 0) {
2361                     av_log(ic, AV_LOG_WARNING,
2362                            "decoding for stream %d failed\n", st->index);
2363                 } else if (!has_codec_parameters(st)) {
2364                     char buf[256];
2365                     avcodec_string(buf, sizeof(buf), st->internal->avctx, 0);
2366                     av_log(ic, AV_LOG_WARNING,
2367                            "Could not find codec parameters (%s)\n", buf);
2368                 } else {
2369                     ret = 0;
2370                 }
2371             }
2372             break;
2373         }
2374
2375         pkt = &pkt1;
2376
2377         if (!(ic->flags & AVFMT_FLAG_NOBUFFER)) {
2378             ret = add_to_pktbuf(&ic->internal->packet_buffer, pkt,
2379                                 &ic->internal->packet_buffer_end, 0);
2380             if (ret < 0)
2381                 goto find_stream_info_err;
2382         }
2383
2384         read_size += pkt->size;
2385
2386         st = ic->streams[pkt->stream_index];
2387         avctx = st->internal->avctx;
2388         if (!st->internal->avctx_inited) {
2389             ret = avcodec_parameters_to_context(avctx, st->codecpar);
2390             if (ret < 0)
2391                 goto find_stream_info_err;
2392             st->internal->avctx_inited = 1;
2393         }
2394
2395         if (pkt->dts != AV_NOPTS_VALUE && st->codec_info_nb_frames > 1) {
2396             /* check for non-increasing dts */
2397             if (st->info->fps_last_dts != AV_NOPTS_VALUE &&
2398                 st->info->fps_last_dts >= pkt->dts) {
2399                 av_log(ic, AV_LOG_WARNING,
2400                        "Non-increasing DTS in stream %d: packet %d with DTS "
2401                        "%"PRId64", packet %d with DTS %"PRId64"\n",
2402                        st->index, st->info->fps_last_dts_idx,
2403                        st->info->fps_last_dts, st->codec_info_nb_frames,
2404                        pkt->dts);
2405                 st->info->fps_first_dts =
2406                 st->info->fps_last_dts  = AV_NOPTS_VALUE;
2407             }
2408             /* Check for a discontinuity in dts. If the difference in dts
2409              * is more than 1000 times the average packet duration in the
2410              * sequence, we treat it as a discontinuity. */
2411             if (st->info->fps_last_dts != AV_NOPTS_VALUE &&
2412                 st->info->fps_last_dts_idx > st->info->fps_first_dts_idx &&
2413                 (pkt->dts - st->info->fps_last_dts) / 1000 >
2414                 (st->info->fps_last_dts     - st->info->fps_first_dts) /
2415                 (st->info->fps_last_dts_idx - st->info->fps_first_dts_idx)) {
2416                 av_log(ic, AV_LOG_WARNING,
2417                        "DTS discontinuity in stream %d: packet %d with DTS "
2418                        "%"PRId64", packet %d with DTS %"PRId64"\n",
2419                        st->index, st->info->fps_last_dts_idx,
2420                        st->info->fps_last_dts, st->codec_info_nb_frames,
2421                        pkt->dts);
2422                 st->info->fps_first_dts =
2423                 st->info->fps_last_dts  = AV_NOPTS_VALUE;
2424             }
2425
2426             /* update stored dts values */
2427             if (st->info->fps_first_dts == AV_NOPTS_VALUE) {
2428                 st->info->fps_first_dts     = pkt->dts;
2429                 st->info->fps_first_dts_idx = st->codec_info_nb_frames;
2430             }
2431             st->info->fps_last_dts     = pkt->dts;
2432             st->info->fps_last_dts_idx = st->codec_info_nb_frames;
2433
2434             /* check max_analyze_duration */
2435             if (av_rescale_q(pkt->dts - st->info->fps_first_dts, st->time_base,
2436                              AV_TIME_BASE_Q) >= ic->max_analyze_duration) {
2437                 av_log(ic, AV_LOG_WARNING, "max_analyze_duration %d reached\n",
2438                        ic->max_analyze_duration);
2439                 if (ic->flags & AVFMT_FLAG_NOBUFFER)
2440                     av_packet_unref(pkt);
2441                 break;
2442             }
2443         }
2444         if (!st->internal->avctx->extradata) {
2445             ret = extract_extradata(st, pkt);
2446             if (ret < 0)
2447                 goto find_stream_info_err;
2448         }
2449
2450         /* If still no information, we try to open the codec and to
2451          * decompress the frame. We try to avoid that in most cases as
2452          * it takes longer and uses more memory. For MPEG-4, we need to
2453          * decompress for QuickTime.
2454          *
2455          * If AV_CODEC_CAP_CHANNEL_CONF is set this will force decoding of at
2456          * least one frame of codec data, this makes sure the codec initializes
2457          * the channel configuration and does not only trust the values from
2458          * the container. */
2459         try_decode_frame(ic, st, pkt,
2460                          (options && i < orig_nb_streams) ? &options[i] : NULL);
2461
2462         if (ic->flags & AVFMT_FLAG_NOBUFFER)
2463             av_packet_unref(pkt);
2464
2465         st->codec_info_nb_frames++;
2466         count++;
2467     }
2468
2469     // close codecs which were opened in try_decode_frame()
2470     for (i = 0; i < ic->nb_streams; i++) {
2471         st = ic->streams[i];
2472         avcodec_close(st->internal->avctx);
2473     }
2474     for (i = 0; i < ic->nb_streams; i++) {
2475         st = ic->streams[i];
2476         avctx = st->internal->avctx;
2477         if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
2478             /* estimate average framerate if not set by demuxer */
2479             if (!st->avg_frame_rate.num &&
2480                 st->info->fps_last_dts != st->info->fps_first_dts) {
2481                 int64_t delta_dts = st->info->fps_last_dts -
2482                                     st->info->fps_first_dts;
2483                 int delta_packets = st->info->fps_last_dts_idx -
2484                                     st->info->fps_first_dts_idx;
2485                 int best_fps      = 0;
2486                 double best_error = 0.01;
2487
2488                 if (delta_dts     >= INT64_MAX / st->time_base.num ||
2489                     delta_packets >= INT64_MAX / st->time_base.den ||
2490                     delta_dts < 0)
2491                     continue;
2492                 av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den,
2493                           delta_packets * (int64_t) st->time_base.den,
2494                           delta_dts * (int64_t) st->time_base.num, 60000);
2495
2496                 /* Round guessed framerate to a "standard" framerate if it's
2497                  * within 1% of the original estimate. */
2498                 for (j = 0; j < MAX_STD_TIMEBASES; j++) {
2499                     AVRational std_fps = { get_std_framerate(j), 12 * 1001 };
2500                     double error       = fabs(av_q2d(st->avg_frame_rate) /
2501                                               av_q2d(std_fps) - 1);
2502
2503                     if (error < best_error) {
2504                         best_error = error;
2505                         best_fps   = std_fps.num;
2506                     }
2507                 }
2508                 if (best_fps)
2509                     av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den,
2510                               best_fps, 12 * 1001, INT_MAX);
2511             }
2512         } else if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
2513             if (!avctx->bits_per_coded_sample)
2514                 avctx->bits_per_coded_sample =
2515                     av_get_bits_per_sample(avctx->codec_id);
2516             // set stream disposition based on audio service type
2517             switch (avctx->audio_service_type) {
2518             case AV_AUDIO_SERVICE_TYPE_EFFECTS:
2519                 st->disposition = AV_DISPOSITION_CLEAN_EFFECTS;
2520                 break;
2521             case AV_AUDIO_SERVICE_TYPE_VISUALLY_IMPAIRED:
2522                 st->disposition = AV_DISPOSITION_VISUAL_IMPAIRED;
2523                 break;
2524             case AV_AUDIO_SERVICE_TYPE_HEARING_IMPAIRED:
2525                 st->disposition = AV_DISPOSITION_HEARING_IMPAIRED;
2526                 break;
2527             case AV_AUDIO_SERVICE_TYPE_COMMENTARY:
2528                 st->disposition = AV_DISPOSITION_COMMENT;
2529                 break;
2530             case AV_AUDIO_SERVICE_TYPE_KARAOKE:
2531                 st->disposition = AV_DISPOSITION_KARAOKE;
2532                 break;
2533             }
2534         }
2535     }
2536
2537     compute_chapters_end(ic);
2538
2539     /* update the stream parameters from the internal codec contexts */
2540     for (i = 0; i < ic->nb_streams; i++) {
2541         st = ic->streams[i];
2542         if (!st->internal->avctx_inited)
2543             continue;
2544
2545         ret = avcodec_parameters_from_context(st->codecpar, st->internal->avctx);
2546         if (ret < 0)
2547             goto find_stream_info_err;
2548
2549 #if FF_API_LAVF_AVCTX
2550 FF_DISABLE_DEPRECATION_WARNINGS
2551         ret = avcodec_parameters_to_context(st->codec, st->codecpar);
2552         if (ret < 0)
2553             goto find_stream_info_err;
2554
2555         if (st->internal->avctx->subtitle_header) {
2556             st->codec->subtitle_header = av_malloc(st->internal->avctx->subtitle_header_size);
2557             if (!st->codec->subtitle_header)
2558                 goto find_stream_info_err;
2559             st->codec->subtitle_header_size = st->internal->avctx->subtitle_header_size;
2560             memcpy(st->codec->subtitle_header, st->internal->avctx->subtitle_header,
2561                    st->codec->subtitle_header_size);
2562         }
2563 FF_ENABLE_DEPRECATION_WARNINGS
2564 #endif
2565
2566         st->internal->avctx_inited = 0;
2567     }
2568
2569     estimate_timings(ic, old_offset);
2570
2571 find_stream_info_err:
2572     for (i = 0; i < ic->nb_streams; i++) {
2573         av_freep(&ic->streams[i]->info);
2574         av_bsf_free(&ic->streams[i]->internal->extract_extradata.bsf);
2575         av_packet_free(&ic->streams[i]->internal->extract_extradata.pkt);
2576     }
2577     return ret;
2578 }
2579
2580 static AVProgram *find_program_from_stream(AVFormatContext *ic, int s)
2581 {
2582     int i, j;
2583
2584     for (i = 0; i < ic->nb_programs; i++)
2585         for (j = 0; j < ic->programs[i]->nb_stream_indexes; j++)
2586             if (ic->programs[i]->stream_index[j] == s)
2587                 return ic->programs[i];
2588     return NULL;
2589 }
2590
2591 int av_find_best_stream(AVFormatContext *ic, enum AVMediaType type,
2592                         int wanted_stream_nb, int related_stream,
2593                         AVCodec **decoder_ret, int flags)
2594 {
2595     int i, nb_streams = ic->nb_streams;
2596     int ret = AVERROR_STREAM_NOT_FOUND, best_count = -1;
2597     unsigned *program = NULL;
2598     AVCodec *decoder = NULL, *best_decoder = NULL;
2599
2600     if (related_stream >= 0 && wanted_stream_nb < 0) {
2601         AVProgram *p = find_program_from_stream(ic, related_stream);
2602         if (p) {
2603             program    = p->stream_index;
2604             nb_streams = p->nb_stream_indexes;
2605         }
2606     }
2607     for (i = 0; i < nb_streams; i++) {
2608         int real_stream_index = program ? program[i] : i;
2609         AVStream *st          = ic->streams[real_stream_index];
2610         AVCodecParameters *par = st->codecpar;
2611         if (par->codec_type != type)
2612             continue;
2613         if (wanted_stream_nb >= 0 && real_stream_index != wanted_stream_nb)
2614             continue;
2615         if (st->disposition & (AV_DISPOSITION_HEARING_IMPAIRED |
2616                                AV_DISPOSITION_VISUAL_IMPAIRED))
2617             continue;
2618         if (decoder_ret) {
2619             decoder = avcodec_find_decoder(par->codec_id);
2620             if (!decoder) {
2621                 if (ret < 0)
2622                     ret = AVERROR_DECODER_NOT_FOUND;
2623                 continue;
2624             }
2625         }
2626         if (best_count >= st->codec_info_nb_frames)
2627             continue;
2628         best_count   = st->codec_info_nb_frames;
2629         ret          = real_stream_index;
2630         best_decoder = decoder;
2631         if (program && i == nb_streams - 1 && ret < 0) {
2632             program    = NULL;
2633             nb_streams = ic->nb_streams;
2634             /* no related stream found, try again with everything */
2635             i = 0;
2636         }
2637     }
2638     if (decoder_ret)
2639         *decoder_ret = best_decoder;
2640     return ret;
2641 }
2642
2643 /*******************************************************/
2644
2645 int av_read_play(AVFormatContext *s)
2646 {
2647     if (s->iformat->read_play)
2648         return s->iformat->read_play(s);
2649     if (s->pb)
2650         return avio_pause(s->pb, 0);
2651     return AVERROR(ENOSYS);
2652 }
2653
2654 int av_read_pause(AVFormatContext *s)
2655 {
2656     if (s->iformat->read_pause)
2657         return s->iformat->read_pause(s);
2658     if (s->pb)
2659         return avio_pause(s->pb, 1);
2660     return AVERROR(ENOSYS);
2661 }
2662
2663 static void free_stream(AVStream **pst)
2664 {
2665     AVStream *st = *pst;
2666     int i;
2667
2668     if (!st)
2669         return;
2670
2671     for (i = 0; i < st->nb_side_data; i++)
2672         av_freep(&st->side_data[i].data);
2673     av_freep(&st->side_data);
2674
2675     if (st->parser)
2676         av_parser_close(st->parser);
2677
2678     if (st->attached_pic.data)
2679         av_packet_unref(&st->attached_pic);
2680
2681     if (st->internal) {
2682         avcodec_free_context(&st->internal->avctx);
2683         av_bsf_free(&st->internal->extract_extradata.bsf);
2684         av_packet_free(&st->internal->extract_extradata.pkt);
2685     }
2686     av_freep(&st->internal);
2687
2688     av_dict_free(&st->metadata);
2689     avcodec_parameters_free(&st->codecpar);
2690     av_freep(&st->probe_data.buf);
2691     av_free(st->index_entries);
2692 #if FF_API_LAVF_AVCTX
2693 FF_DISABLE_DEPRECATION_WARNINGS
2694     av_free(st->codec->extradata);
2695     av_free(st->codec->subtitle_header);
2696     av_free(st->codec);
2697 FF_ENABLE_DEPRECATION_WARNINGS
2698 #endif
2699     av_free(st->priv_data);
2700     av_free(st->info);
2701
2702     av_freep(pst);
2703 }
2704
2705 void avformat_free_context(AVFormatContext *s)
2706 {
2707     int i;
2708
2709     if (!s)
2710         return;
2711
2712     av_opt_free(s);
2713     if (s->iformat && s->iformat->priv_class && s->priv_data)
2714         av_opt_free(s->priv_data);
2715
2716     for (i = 0; i < s->nb_streams; i++)
2717         free_stream(&s->streams[i]);
2718
2719     for (i = s->nb_programs - 1; i >= 0; i--) {
2720         av_dict_free(&s->programs[i]->metadata);
2721         av_freep(&s->programs[i]->stream_index);
2722         av_freep(&s->programs[i]);
2723     }
2724     av_freep(&s->programs);
2725     av_freep(&s->priv_data);
2726     while (s->nb_chapters--) {
2727         av_dict_free(&s->chapters[s->nb_chapters]->metadata);
2728         av_free(s->chapters[s->nb_chapters]);
2729     }
2730     av_freep(&s->chapters);
2731     av_dict_free(&s->metadata);
2732     av_freep(&s->streams);
2733     av_freep(&s->internal);
2734     av_free(s);
2735 }
2736
2737 void avformat_close_input(AVFormatContext **ps)
2738 {
2739     AVFormatContext *s = *ps;
2740     AVIOContext *pb    = s->pb;
2741
2742     if ((s->iformat && s->iformat->flags & AVFMT_NOFILE) ||
2743         (s->flags & AVFMT_FLAG_CUSTOM_IO))
2744         pb = NULL;
2745
2746     flush_packet_queue(s);
2747
2748     if (s->iformat)
2749         if (s->iformat->read_close)
2750             s->iformat->read_close(s);
2751
2752     avformat_free_context(s);
2753
2754     *ps = NULL;
2755
2756     avio_close(pb);
2757 }
2758
2759 AVStream *avformat_new_stream(AVFormatContext *s, const AVCodec *c)
2760 {
2761     AVStream *st;
2762     int i;
2763
2764     if (av_reallocp_array(&s->streams, s->nb_streams + 1,
2765                           sizeof(*s->streams)) < 0) {
2766         s->nb_streams = 0;
2767         return NULL;
2768     }
2769
2770     st = av_mallocz(sizeof(AVStream));
2771     if (!st)
2772         return NULL;
2773     if (!(st->info = av_mallocz(sizeof(*st->info)))) {
2774         av_free(st);
2775         return NULL;
2776     }
2777
2778 #if FF_API_LAVF_AVCTX
2779 FF_DISABLE_DEPRECATION_WARNINGS
2780     st->codec = avcodec_alloc_context3(c);
2781     if (!st->codec) {
2782         av_free(st->info);
2783         av_free(st);
2784         return NULL;
2785     }
2786 FF_ENABLE_DEPRECATION_WARNINGS
2787 #endif
2788
2789     st->internal = av_mallocz(sizeof(*st->internal));
2790     if (!st->internal)
2791         goto fail;
2792
2793     if (s->iformat) {
2794 #if FF_API_LAVF_AVCTX
2795 FF_DISABLE_DEPRECATION_WARNINGS
2796         /* no default bitrate if decoding */
2797         st->codec->bit_rate = 0;
2798 FF_ENABLE_DEPRECATION_WARNINGS
2799 #endif
2800
2801         /* default pts setting is MPEG-like */
2802         avpriv_set_pts_info(st, 33, 1, 90000);
2803         /* we set the current DTS to 0 so that formats without any timestamps
2804          * but durations get some timestamps, formats with some unknown
2805          * timestamps have their first few packets buffered and the
2806          * timestamps corrected before they are returned to the user */
2807         st->cur_dts = 0;
2808     } else {
2809         st->cur_dts = AV_NOPTS_VALUE;
2810     }
2811
2812     st->codecpar = avcodec_parameters_alloc();
2813     if (!st->codecpar)
2814         goto fail;
2815
2816     st->internal->avctx = avcodec_alloc_context3(NULL);
2817     if (!st->internal->avctx)
2818         goto fail;
2819
2820     st->index      = s->nb_streams;
2821     st->start_time = AV_NOPTS_VALUE;
2822     st->duration   = AV_NOPTS_VALUE;
2823     st->first_dts     = AV_NOPTS_VALUE;
2824     st->probe_packets = MAX_PROBE_PACKETS;
2825
2826     st->last_IP_pts = AV_NOPTS_VALUE;
2827     for (i = 0; i < MAX_REORDER_DELAY + 1; i++)
2828         st->pts_buffer[i] = AV_NOPTS_VALUE;
2829
2830     st->sample_aspect_ratio = (AVRational) { 0, 1 };
2831
2832     st->info->fps_first_dts = AV_NOPTS_VALUE;
2833     st->info->fps_last_dts  = AV_NOPTS_VALUE;
2834
2835 #if FF_API_LAVF_AVCTX
2836     st->internal->need_codec_update = 1;
2837 #endif
2838
2839     s->streams[s->nb_streams++] = st;
2840     return st;
2841 fail:
2842     free_stream(&st);
2843     return NULL;
2844 }
2845
2846 AVProgram *av_new_program(AVFormatContext *ac, int id)
2847 {
2848     AVProgram *program = NULL;
2849     int i;
2850
2851     av_log(ac, AV_LOG_TRACE, "new_program: id=0x%04x\n", id);
2852
2853     for (i = 0; i < ac->nb_programs; i++)
2854         if (ac->programs[i]->id == id)
2855             program = ac->programs[i];
2856
2857     if (!program) {
2858         program = av_mallocz(sizeof(AVProgram));
2859         if (!program)
2860             return NULL;
2861         dynarray_add(&ac->programs, &ac->nb_programs, program);
2862         program->discard = AVDISCARD_NONE;
2863     }
2864     program->id = id;
2865
2866     return program;
2867 }
2868
2869 AVChapter *avpriv_new_chapter(AVFormatContext *s, int id, AVRational time_base,
2870                               int64_t start, int64_t end, const char *title)
2871 {
2872     AVChapter *chapter = NULL;
2873     int i;
2874
2875     for (i = 0; i < s->nb_chapters; i++)
2876         if (s->chapters[i]->id == id)
2877             chapter = s->chapters[i];
2878
2879     if (!chapter) {
2880         chapter = av_mallocz(sizeof(AVChapter));
2881         if (!chapter)
2882             return NULL;
2883         dynarray_add(&s->chapters, &s->nb_chapters, chapter);
2884     }
2885     av_dict_set(&chapter->metadata, "title", title, 0);
2886     chapter->id        = id;
2887     chapter->time_base = time_base;
2888     chapter->start     = start;
2889     chapter->end       = end;
2890
2891     return chapter;
2892 }
2893
2894 void ff_program_add_stream_index(AVFormatContext *ac, int progid, unsigned idx)
2895 {
2896     int i, j;
2897     AVProgram *program = NULL;
2898
2899     if (idx >= ac->nb_streams) {
2900         av_log(ac, AV_LOG_ERROR, "stream index %d is not valid\n", idx);
2901         return;
2902     }
2903
2904     for (i = 0; i < ac->nb_programs; i++) {
2905         if (ac->programs[i]->id != progid)
2906             continue;
2907         program = ac->programs[i];
2908         for (j = 0; j < program->nb_stream_indexes; j++)
2909             if (program->stream_index[j] == idx)
2910                 return;
2911
2912         if (av_reallocp_array(&program->stream_index,
2913                               program->nb_stream_indexes + 1,
2914                               sizeof(*program->stream_index)) < 0) {
2915             program->nb_stream_indexes = 0;
2916             return;
2917         }
2918         program->stream_index[program->nb_stream_indexes++] = idx;
2919         return;
2920     }
2921 }
2922
2923 uint64_t ff_ntp_time(void)
2924 {
2925     return (av_gettime() / 1000) * 1000 + NTP_OFFSET_US;
2926 }
2927
2928 int av_get_frame_filename(char *buf, int buf_size, const char *path, int number)
2929 {
2930     const char *p;
2931     char *q, buf1[20], c;
2932     int nd, len, percentd_found;
2933
2934     q = buf;
2935     p = path;
2936     percentd_found = 0;
2937     for (;;) {
2938         c = *p++;
2939         if (c == '\0')
2940             break;
2941         if (c == '%') {
2942             do {
2943                 nd = 0;
2944                 while (av_isdigit(*p))
2945                     nd = nd * 10 + *p++ - '0';
2946                 c = *p++;
2947             } while (av_isdigit(c));
2948
2949             switch (c) {
2950             case '%':
2951                 goto addchar;
2952             case 'd':
2953                 if (percentd_found)
2954                     goto fail;
2955                 percentd_found = 1;
2956                 snprintf(buf1, sizeof(buf1), "%0*d", nd, number);
2957                 len = strlen(buf1);
2958                 if ((q - buf + len) > buf_size - 1)
2959                     goto fail;
2960                 memcpy(q, buf1, len);
2961                 q += len;
2962                 break;
2963             default:
2964                 goto fail;
2965             }
2966         } else {
2967 addchar:
2968             if ((q - buf) < buf_size - 1)
2969                 *q++ = c;
2970         }
2971     }
2972     if (!percentd_found)
2973         goto fail;
2974     *q = '\0';
2975     return 0;
2976 fail:
2977     *q = '\0';
2978     return -1;
2979 }
2980
2981 void av_url_split(char *proto, int proto_size,
2982                   char *authorization, int authorization_size,
2983                   char *hostname, int hostname_size,
2984                   int *port_ptr, char *path, int path_size, const char *url)
2985 {
2986     const char *p, *ls, *at, *col, *brk;
2987
2988     if (port_ptr)
2989         *port_ptr = -1;
2990     if (proto_size > 0)
2991         proto[0] = 0;
2992     if (authorization_size > 0)
2993         authorization[0] = 0;
2994     if (hostname_size > 0)
2995         hostname[0] = 0;
2996     if (path_size > 0)
2997         path[0] = 0;
2998
2999     /* parse protocol */
3000     if ((p = strchr(url, ':'))) {
3001         av_strlcpy(proto, url, FFMIN(proto_size, p + 1 - url));
3002         p++; /* skip ':' */
3003         if (*p == '/')
3004             p++;
3005         if (*p == '/')
3006             p++;
3007     } else {
3008         /* no protocol means plain filename */
3009         av_strlcpy(path, url, path_size);
3010         return;
3011     }
3012
3013     /* separate path from hostname */
3014     ls = strchr(p, '/');
3015     if (!ls)
3016         ls = strchr(p, '?');
3017     if (ls)
3018         av_strlcpy(path, ls, path_size);
3019     else
3020         ls = &p[strlen(p)];  // XXX
3021
3022     /* the rest is hostname, use that to parse auth/port */
3023     if (ls != p) {
3024         /* authorization (user[:pass]@hostname) */
3025         if ((at = strchr(p, '@')) && at < ls) {
3026             av_strlcpy(authorization, p,
3027                        FFMIN(authorization_size, at + 1 - p));
3028             p = at + 1; /* skip '@' */
3029         }
3030
3031         if (*p == '[' && (brk = strchr(p, ']')) && brk < ls) {
3032             /* [host]:port */
3033             av_strlcpy(hostname, p + 1,
3034                        FFMIN(hostname_size, brk - p));
3035             if (brk[1] == ':' && port_ptr)
3036                 *port_ptr = atoi(brk + 2);
3037         } else if ((col = strchr(p, ':')) && col < ls) {
3038             av_strlcpy(hostname, p,
3039                        FFMIN(col + 1 - p, hostname_size));
3040             if (port_ptr)
3041                 *port_ptr = atoi(col + 1);
3042         } else
3043             av_strlcpy(hostname, p,
3044                        FFMIN(ls + 1 - p, hostname_size));
3045     }
3046 }
3047
3048 char *ff_data_to_hex(char *buff, const uint8_t *src, int s, int lowercase)
3049 {
3050     int i;
3051     static const char hex_table_uc[16] = { '0', '1', '2', '3',
3052                                            '4', '5', '6', '7',
3053                                            '8', '9', 'A', 'B',
3054                                            'C', 'D', 'E', 'F' };
3055     static const char hex_table_lc[16] = { '0', '1', '2', '3',
3056                                            '4', '5', '6', '7',
3057                                            '8', '9', 'a', 'b',
3058                                            'c', 'd', 'e', 'f' };
3059     const char *hex_table = lowercase ? hex_table_lc : hex_table_uc;
3060
3061     for (i = 0; i < s; i++) {
3062         buff[i * 2]     = hex_table[src[i] >> 4];
3063         buff[i * 2 + 1] = hex_table[src[i] & 0xF];
3064     }
3065
3066     return buff;
3067 }
3068
3069 int ff_hex_to_data(uint8_t *data, const char *p)
3070 {
3071     int c, len, v;
3072
3073     len = 0;
3074     v   = 1;
3075     for (;;) {
3076         p += strspn(p, SPACE_CHARS);
3077         if (*p == '\0')
3078             break;
3079         c = av_toupper((unsigned char) *p++);
3080         if (c >= '0' && c <= '9')
3081             c = c - '0';
3082         else if (c >= 'A' && c <= 'F')
3083             c = c - 'A' + 10;
3084         else
3085             break;
3086         v = (v << 4) | c;
3087         if (v & 0x100) {
3088             if (data)
3089                 data[len] = v;
3090             len++;
3091             v = 1;
3092         }
3093     }
3094     return len;
3095 }
3096
3097 void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits,
3098                          unsigned int pts_num, unsigned int pts_den)
3099 {
3100     AVRational new_tb;
3101     if (av_reduce(&new_tb.num, &new_tb.den, pts_num, pts_den, INT_MAX)) {
3102         if (new_tb.num != pts_num)
3103             av_log(NULL, AV_LOG_DEBUG,
3104                    "st:%d removing common factor %d from timebase\n",
3105                    s->index, pts_num / new_tb.num);
3106     } else
3107         av_log(NULL, AV_LOG_WARNING,
3108                "st:%d has too large timebase, reducing\n", s->index);
3109
3110     if (new_tb.num <= 0 || new_tb.den <= 0) {
3111         av_log(NULL, AV_LOG_ERROR,
3112                "Ignoring attempt to set invalid timebase for st:%d\n",
3113                s->index);
3114         return;
3115     }
3116     s->time_base     = new_tb;
3117     s->pts_wrap_bits = pts_wrap_bits;
3118 }
3119
3120 void ff_parse_key_value(const char *str, ff_parse_key_val_cb callback_get_buf,
3121                         void *context)
3122 {
3123     const char *ptr = str;
3124
3125     /* Parse key=value pairs. */
3126     for (;;) {
3127         const char *key;
3128         char *dest = NULL, *dest_end;
3129         int key_len, dest_len = 0;
3130
3131         /* Skip whitespace and potential commas. */
3132         while (*ptr && (av_isspace(*ptr) || *ptr == ','))
3133             ptr++;
3134         if (!*ptr)
3135             break;
3136
3137         key = ptr;
3138
3139         if (!(ptr = strchr(key, '=')))
3140             break;
3141         ptr++;
3142         key_len = ptr - key;
3143
3144         callback_get_buf(context, key, key_len, &dest, &dest_len);
3145         dest_end = dest + dest_len - 1;
3146
3147         if (*ptr == '\"') {
3148             ptr++;
3149             while (*ptr && *ptr != '\"') {
3150                 if (*ptr == '\\') {
3151                     if (!ptr[1])
3152                         break;
3153                     if (dest && dest < dest_end)
3154                         *dest++ = ptr[1];
3155                     ptr += 2;
3156                 } else {
3157                     if (dest && dest < dest_end)
3158                         *dest++ = *ptr;
3159                     ptr++;
3160                 }
3161             }
3162             if (*ptr == '\"')
3163                 ptr++;
3164         } else {
3165             for (; *ptr && !(av_isspace(*ptr) || *ptr == ','); ptr++)
3166                 if (dest && dest < dest_end)
3167                     *dest++ = *ptr;
3168         }
3169         if (dest)
3170             *dest = 0;
3171     }
3172 }
3173
3174 int ff_find_stream_index(AVFormatContext *s, int id)
3175 {
3176     int i;
3177     for (i = 0; i < s->nb_streams; i++)
3178         if (s->streams[i]->id == id)
3179             return i;
3180     return -1;
3181 }
3182
3183 int64_t ff_iso8601_to_unix_time(const char *datestr)
3184 {
3185     struct tm time1 = { 0 }, time2 = { 0 };
3186     const char *ret1, *ret2;
3187     ret1 = av_small_strptime(datestr, "%Y - %m - %d %T", &time1);
3188     ret2 = av_small_strptime(datestr, "%Y - %m - %dT%T", &time2);
3189     if (ret2 && !ret1)
3190         return av_timegm(&time2);
3191     else
3192         return av_timegm(&time1);
3193 }
3194
3195 int avformat_query_codec(const AVOutputFormat *ofmt, enum AVCodecID codec_id,
3196                          int std_compliance)
3197 {
3198     if (ofmt) {
3199         if (ofmt->query_codec)
3200             return ofmt->query_codec(codec_id, std_compliance);
3201         else if (ofmt->codec_tag)
3202             return !!av_codec_get_tag(ofmt->codec_tag, codec_id);
3203         else if (codec_id == ofmt->video_codec ||
3204                  codec_id == ofmt->audio_codec ||
3205                  codec_id == ofmt->subtitle_codec)
3206             return 1;
3207     }
3208     return AVERROR_PATCHWELCOME;
3209 }
3210
3211 int avformat_network_init(void)
3212 {
3213 #if CONFIG_NETWORK
3214     int ret;
3215     ff_network_inited_globally = 1;
3216     if ((ret = ff_network_init()) < 0)
3217         return ret;
3218     ff_tls_init();
3219 #endif
3220     return 0;
3221 }
3222
3223 int avformat_network_deinit(void)
3224 {
3225 #if CONFIG_NETWORK
3226     ff_network_close();
3227     ff_tls_deinit();
3228 #endif
3229     return 0;
3230 }
3231
3232 int ff_add_param_change(AVPacket *pkt, int32_t channels,
3233                         uint64_t channel_layout, int32_t sample_rate,
3234                         int32_t width, int32_t height)
3235 {
3236     uint32_t flags = 0;
3237     int size = 4;
3238     uint8_t *data;
3239     if (!pkt)
3240         return AVERROR(EINVAL);
3241     if (channels) {
3242         size  += 4;
3243         flags |= AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT;
3244     }
3245     if (channel_layout) {
3246         size  += 8;
3247         flags |= AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT;
3248     }
3249     if (sample_rate) {
3250         size  += 4;
3251         flags |= AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE;
3252     }
3253     if (width || height) {
3254         size  += 8;
3255         flags |= AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS;
3256     }
3257     data = av_packet_new_side_data(pkt, AV_PKT_DATA_PARAM_CHANGE, size);
3258     if (!data)
3259         return AVERROR(ENOMEM);
3260     bytestream_put_le32(&data, flags);
3261     if (channels)
3262         bytestream_put_le32(&data, channels);
3263     if (channel_layout)
3264         bytestream_put_le64(&data, channel_layout);
3265     if (sample_rate)
3266         bytestream_put_le32(&data, sample_rate);
3267     if (width || height) {
3268         bytestream_put_le32(&data, width);
3269         bytestream_put_le32(&data, height);
3270     }
3271     return 0;
3272 }
3273
3274 int ff_generate_avci_extradata(AVStream *st)
3275 {
3276     static const uint8_t avci100_1080p_extradata[] = {
3277         // SPS
3278         0x00, 0x00, 0x00, 0x01, 0x67, 0x7a, 0x10, 0x29,
3279         0xb6, 0xd4, 0x20, 0x22, 0x33, 0x19, 0xc6, 0x63,
3280         0x23, 0x21, 0x01, 0x11, 0x98, 0xce, 0x33, 0x19,
3281         0x18, 0x21, 0x02, 0x56, 0xb9, 0x3d, 0x7d, 0x7e,
3282         0x4f, 0xe3, 0x3f, 0x11, 0xf1, 0x9e, 0x08, 0xb8,
3283         0x8c, 0x54, 0x43, 0xc0, 0x78, 0x02, 0x27, 0xe2,
3284         0x70, 0x1e, 0x30, 0x10, 0x10, 0x14, 0x00, 0x00,
3285         0x03, 0x00, 0x04, 0x00, 0x00, 0x03, 0x00, 0xca,
3286         0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3287         // PPS
3288         0x00, 0x00, 0x00, 0x01, 0x68, 0xce, 0x33, 0x48,
3289         0xd0
3290     };
3291     static const uint8_t avci100_1080i_extradata[] = {
3292         // SPS
3293         0x00, 0x00, 0x00, 0x01, 0x67, 0x7a, 0x10, 0x29,
3294         0xb6, 0xd4, 0x20, 0x22, 0x33, 0x19, 0xc6, 0x63,
3295         0x23, 0x21, 0x01, 0x11, 0x98, 0xce, 0x33, 0x19,
3296         0x18, 0x21, 0x03, 0x3a, 0x46, 0x65, 0x6a, 0x65,
3297         0x24, 0xad, 0xe9, 0x12, 0x32, 0x14, 0x1a, 0x26,
3298         0x34, 0xad, 0xa4, 0x41, 0x82, 0x23, 0x01, 0x50,
3299         0x2b, 0x1a, 0x24, 0x69, 0x48, 0x30, 0x40, 0x2e,
3300         0x11, 0x12, 0x08, 0xc6, 0x8c, 0x04, 0x41, 0x28,
3301         0x4c, 0x34, 0xf0, 0x1e, 0x01, 0x13, 0xf2, 0xe0,
3302         0x3c, 0x60, 0x20, 0x20, 0x28, 0x00, 0x00, 0x03,
3303         0x00, 0x08, 0x00, 0x00, 0x03, 0x01, 0x94, 0x00,
3304         // PPS
3305         0x00, 0x00, 0x00, 0x01, 0x68, 0xce, 0x33, 0x48,
3306         0xd0
3307     };
3308     static const uint8_t avci50_1080i_extradata[] = {
3309         // SPS
3310         0x00, 0x00, 0x00, 0x01, 0x67, 0x6e, 0x10, 0x28,
3311         0xa6, 0xd4, 0x20, 0x32, 0x33, 0x0c, 0x71, 0x18,
3312         0x88, 0x62, 0x10, 0x19, 0x19, 0x86, 0x38, 0x8c,
3313         0x44, 0x30, 0x21, 0x02, 0x56, 0x4e, 0x6e, 0x61,
3314         0x87, 0x3e, 0x73, 0x4d, 0x98, 0x0c, 0x03, 0x06,
3315         0x9c, 0x0b, 0x73, 0xe6, 0xc0, 0xb5, 0x18, 0x63,
3316         0x0d, 0x39, 0xe0, 0x5b, 0x02, 0xd4, 0xc6, 0x19,
3317         0x1a, 0x79, 0x8c, 0x32, 0x34, 0x24, 0xf0, 0x16,
3318         0x81, 0x13, 0xf7, 0xff, 0x80, 0x01, 0x80, 0x02,
3319         0x71, 0x80, 0x80, 0x80, 0xa0, 0x00, 0x00, 0x03,
3320         0x00, 0x20, 0x00, 0x00, 0x06, 0x50, 0x80, 0x00,
3321         // PPS
3322         0x00, 0x00, 0x00, 0x01, 0x68, 0xee, 0x31, 0x12,
3323         0x11
3324     };
3325     static const uint8_t avci100_720p_extradata[] = {
3326         // SPS
3327         0x00, 0x00, 0x00, 0x01, 0x67, 0x7a, 0x10, 0x29,
3328         0xb6, 0xd4, 0x20, 0x2a, 0x33, 0x1d, 0xc7, 0x62,
3329         0xa1, 0x08, 0x40, 0x54, 0x66, 0x3b, 0x8e, 0xc5,
3330         0x42, 0x02, 0x10, 0x25, 0x64, 0x2c, 0x89, 0xe8,
3331         0x85, 0xe4, 0x21, 0x4b, 0x90, 0x83, 0x06, 0x95,
3332         0xd1, 0x06, 0x46, 0x97, 0x20, 0xc8, 0xd7, 0x43,
3333         0x08, 0x11, 0xc2, 0x1e, 0x4c, 0x91, 0x0f, 0x01,
3334         0x40, 0x16, 0xec, 0x07, 0x8c, 0x04, 0x04, 0x05,
3335         0x00, 0x00, 0x03, 0x00, 0x01, 0x00, 0x00, 0x03,
3336         0x00, 0x64, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00,
3337         // PPS
3338         0x00, 0x00, 0x00, 0x01, 0x68, 0xce, 0x31, 0x12,
3339         0x11
3340     };
3341
3342     const uint8_t *data = NULL;
3343     int size            = 0;
3344
3345     if (st->codecpar->width == 1920) {
3346         if (st->codecpar->field_order == AV_FIELD_PROGRESSIVE) {
3347             data = avci100_1080p_extradata;
3348             size = sizeof(avci100_1080p_extradata);
3349         } else {
3350             data = avci100_1080i_extradata;
3351             size = sizeof(avci100_1080i_extradata);
3352         }
3353     } else if (st->codecpar->width == 1440) {
3354         data = avci50_1080i_extradata;
3355         size = sizeof(avci50_1080i_extradata);
3356     } else if (st->codecpar->width == 1280) {
3357         data = avci100_720p_extradata;
3358         size = sizeof(avci100_720p_extradata);
3359     }
3360
3361     if (!size)
3362         return 0;
3363
3364     av_freep(&st->codecpar->extradata);
3365     st->codecpar->extradata_size = 0;
3366     st->codecpar->extradata      = av_mallocz(size + AV_INPUT_BUFFER_PADDING_SIZE);
3367     if (!st->codecpar->extradata)
3368         return AVERROR(ENOMEM);
3369
3370     memcpy(st->codecpar->extradata, data, size);
3371     st->codecpar->extradata_size = size;
3372
3373     return 0;
3374 }
3375
3376 uint8_t *av_stream_get_side_data(AVStream *st, enum AVPacketSideDataType type,
3377                                  int *size)
3378 {
3379     int i;
3380
3381     for (i = 0; i < st->nb_side_data; i++) {
3382         if (st->side_data[i].type == type) {
3383             if (size)
3384                 *size = st->side_data[i].size;
3385             return st->side_data[i].data;
3386         }
3387     }
3388     return NULL;
3389 }
3390
3391 int av_stream_add_side_data(AVStream *st, enum AVPacketSideDataType type,
3392                             uint8_t *data, size_t size)
3393 {
3394     AVPacketSideData *sd, *tmp;
3395     int i;
3396
3397     for (i = 0; i < st->nb_side_data; i++) {
3398         sd = &st->side_data[i];
3399
3400         if (sd->type == type) {
3401             av_freep(&sd->data);
3402             sd->data = data;
3403             sd->size = size;
3404             return 0;
3405         }
3406     }
3407
3408     if ((unsigned) st->nb_side_data + 1 >= INT_MAX / sizeof(*st->side_data))
3409         return AVERROR(ERANGE);
3410
3411     tmp = av_realloc(st->side_data, (st->nb_side_data + 1) * sizeof(*tmp));
3412     if (!tmp) {
3413         return AVERROR(ENOMEM);
3414     }
3415
3416     st->side_data = tmp;
3417     st->nb_side_data++;
3418
3419     sd = &st->side_data[st->nb_side_data - 1];
3420     sd->type = type;
3421     sd->data = data;
3422     sd->size = size;
3423
3424     return 0;
3425 }
3426
3427 uint8_t *av_stream_new_side_data(AVStream *st, enum AVPacketSideDataType type,
3428                                  int size)
3429 {
3430     int ret;
3431     uint8_t *data = av_malloc(size);
3432
3433     if (!data)
3434         return NULL;
3435
3436     ret = av_stream_add_side_data(st, type, data, size);
3437     if (ret < 0) {
3438         av_freep(&data);
3439         return NULL;
3440     }
3441
3442     return data;
3443 }
3444
3445 void ff_format_io_close(AVFormatContext *s, AVIOContext **pb)
3446 {
3447     if (*pb)
3448         s->io_close(s, *pb);
3449     *pb = NULL;
3450 }