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