vulkan: fix host copy stride
[ffmpeg.git] / libavformat / demux.c
1 /*
2 * Core demuxing component
3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include <stdint.h>
23
24 #include "config_components.h"
25
26 #include "libavutil/avassert.h"
27 #include "libavutil/avstring.h"
28 #include "libavutil/dict.h"
29 #include "libavutil/internal.h"
30 #include "libavutil/intreadwrite.h"
31 #include "libavutil/mathematics.h"
32 #include "libavutil/mem.h"
33 #include "libavutil/opt.h"
34 #include "libavutil/pixfmt.h"
35 #include "libavutil/time.h"
36 #include "libavutil/timestamp.h"
37
38 #include "libavcodec/avcodec.h"
39 #include "libavcodec/bsf.h"
40 #include "libavcodec/codec_desc.h"
41 #include "libavcodec/internal.h"
42 #include "libavcodec/packet_internal.h"
43 #include "libavcodec/raw.h"
44
45 #include "avformat.h"
46 #include "avformat_internal.h"
47 #include "avio_internal.h"
48 #include "demux.h"
49 #include "id3v2.h"
50 #include "internal.h"
51 #include "url.h"
52
53 static int64_t wrap_timestamp(const AVStream *st, int64_t timestamp)
54 {
55 const FFStream *const sti = cffstream(st);
56 if (sti->pts_wrap_behavior != AV_PTS_WRAP_IGNORE && st->pts_wrap_bits < 64 &&
57 sti->pts_wrap_reference != AV_NOPTS_VALUE && timestamp != AV_NOPTS_VALUE) {
58 if (sti->pts_wrap_behavior == AV_PTS_WRAP_ADD_OFFSET &&
59 timestamp < sti->pts_wrap_reference)
60 return timestamp + (1ULL << st->pts_wrap_bits);
61 else if (sti->pts_wrap_behavior == AV_PTS_WRAP_SUB_OFFSET &&
62 timestamp >= sti->pts_wrap_reference)
63 return timestamp - (1ULL << st->pts_wrap_bits);
64 }
65 return timestamp;
66 }
67
68 int64_t ff_wrap_timestamp(const AVStream *st, int64_t timestamp)
69 {
70 return wrap_timestamp(st, timestamp);
71 }
72
73 static const AVCodec *find_probe_decoder(AVFormatContext *s, const AVStream *st, enum AVCodecID codec_id)
74 {
75 const AVCodec *codec;
76
77 #if CONFIG_H264_DECODER
78 /* Other parts of the code assume this decoder to be used for h264,
79 * so force it if possible. */
80 if (codec_id == AV_CODEC_ID_H264)
81 return avcodec_find_decoder_by_name("h264");
82 #endif
83
84 codec = ff_find_decoder(s, st, codec_id);
85 if (!codec)
86 return NULL;
87
88 if (codec->capabilities & AV_CODEC_CAP_AVOID_PROBING) {
89 const AVCodec *probe_codec = NULL;
90 void *iter = NULL;
91 while ((probe_codec = av_codec_iterate(&iter))) {
92 if (probe_codec->id == codec->id &&
93 av_codec_is_decoder(probe_codec) &&
94 !(probe_codec->capabilities & (AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_EXPERIMENTAL))) {
95 return probe_codec;
96 }
97 }
98 }
99
100 return codec;
101 }
102
103 static int set_codec_from_probe_data(AVFormatContext *s, AVStream *st,
104 AVProbeData *pd)
105 {
106 static const struct {
107 const char *name;
108 enum AVCodecID id;
109 enum AVMediaType type;
110 } fmt_id_type[] = {
111 { "aac", AV_CODEC_ID_AAC, AVMEDIA_TYPE_AUDIO },
112 { "ac3", AV_CODEC_ID_AC3, AVMEDIA_TYPE_AUDIO },
113 { "aptx", AV_CODEC_ID_APTX, AVMEDIA_TYPE_AUDIO },
114 { "av1", AV_CODEC_ID_AV1, AVMEDIA_TYPE_VIDEO },
115 { "dts", AV_CODEC_ID_DTS, AVMEDIA_TYPE_AUDIO },
116 { "dvbsub", AV_CODEC_ID_DVB_SUBTITLE, AVMEDIA_TYPE_SUBTITLE },
117 { "dvbtxt", AV_CODEC_ID_DVB_TELETEXT, AVMEDIA_TYPE_SUBTITLE },
118 { "eac3", AV_CODEC_ID_EAC3, AVMEDIA_TYPE_AUDIO },
119 { "h264", AV_CODEC_ID_H264, AVMEDIA_TYPE_VIDEO },
120 { "hevc", AV_CODEC_ID_HEVC, AVMEDIA_TYPE_VIDEO },
121 { "loas", AV_CODEC_ID_AAC_LATM, AVMEDIA_TYPE_AUDIO },
122 { "m4v", AV_CODEC_ID_MPEG4, AVMEDIA_TYPE_VIDEO },
123 { "mjpeg_2000", AV_CODEC_ID_JPEG2000, AVMEDIA_TYPE_VIDEO },
124 { "mp3", AV_CODEC_ID_MP3, AVMEDIA_TYPE_AUDIO },
125 { "mpegvideo", AV_CODEC_ID_MPEG2VIDEO, AVMEDIA_TYPE_VIDEO },
126 { "truehd", AV_CODEC_ID_TRUEHD, AVMEDIA_TYPE_AUDIO },
127 { "evc", AV_CODEC_ID_EVC, AVMEDIA_TYPE_VIDEO },
128 { "vvc", AV_CODEC_ID_VVC, AVMEDIA_TYPE_VIDEO },
129 { 0 }
130 };
131 int score;
132 const AVInputFormat *fmt = av_probe_input_format3(pd, 1, &score);
133 FFStream *const sti = ffstream(st);
134
135 if (fmt) {
136 av_log(s, AV_LOG_DEBUG,
137 "Probe with size=%d, packets=%d detected %s with score=%d\n",
138 pd->buf_size, s->max_probe_packets - sti->probe_packets,
139 fmt->name, score);
140 for (int i = 0; fmt_id_type[i].name; i++) {
141 if (!strcmp(fmt->name, fmt_id_type[i].name)) {
142 if (fmt_id_type[i].type != AVMEDIA_TYPE_AUDIO &&
143 st->codecpar->sample_rate)
144 continue;
145 if (sti->request_probe > score &&
146 st->codecpar->codec_id != fmt_id_type[i].id)
147 continue;
148 st->codecpar->codec_id = fmt_id_type[i].id;
149 st->codecpar->codec_type = fmt_id_type[i].type;
150 sti->need_context_update = 1;
151 return score;
152 }
153 }
154 }
155 return 0;
156 }
157
158 static int init_input(AVFormatContext *s, const char *filename,
159 AVDictionary **options)
160 {
161 int ret;
162 AVProbeData pd = { filename, NULL, 0 };
163 int score = AVPROBE_SCORE_RETRY;
164
165 if (s->pb) {
166 s->flags |= AVFMT_FLAG_CUSTOM_IO;
167 if (!s->iformat)
168 return av_probe_input_buffer2(s->pb, &s->iformat, filename,
169 s, 0, s->format_probesize);
170 else if (s->iformat->flags & AVFMT_NOFILE)
171 av_log(s, AV_LOG_WARNING, "Custom AVIOContext makes no sense and "
172 "will be ignored with AVFMT_NOFILE format.\n");
173 return 0;
174 }
175
176 if ((s->iformat && s->iformat->flags & AVFMT_NOFILE) ||
177 (!s->iformat && (s->iformat = av_probe_input_format2(&pd, 0, &score))))
178 return score;
179
180 if ((ret = s->io_open(s, &s->pb, filename, AVIO_FLAG_READ | s->avio_flags, options)) < 0)
181 return ret;
182
183 if (s->iformat)
184 return 0;
185 return av_probe_input_buffer2(s->pb, &s->iformat, filename,
186 s, 0, s->format_probesize);
187 }
188
189 static int update_stream_avctx(AVFormatContext *s)
190 {
191 int ret;
192 for (unsigned i = 0; i < s->nb_streams; i++) {
193 AVStream *const st = s->streams[i];
194 FFStream *const sti = ffstream(st);
195
196 if (!sti->need_context_update)
197 continue;
198
199 /* close parser, because it depends on the codec */
200 if (sti->parser && sti->avctx->codec_id != st->codecpar->codec_id) {
201 av_parser_close(sti->parser);
202 sti->parser = NULL;
203 }
204
205 /* update internal codec context, for the parser */
206 ret = avcodec_parameters_to_context(sti->avctx, st->codecpar);
207 if (ret < 0)
208 return ret;
209
210 sti->codec_desc = avcodec_descriptor_get(sti->avctx->codec_id);
211
212 sti->need_context_update = 0;
213 }
214 return 0;
215 }
216
217 static av_always_inline int is_id3v2_format(const AVInputFormat *fmt) {
218 return ffifmt(fmt)->flags_internal & FF_INFMT_FLAG_ID3V2_AUTO;
219 }
220
221 int avformat_open_input(AVFormatContext **ps, const char *filename,
222 const AVInputFormat *fmt, AVDictionary **options)
223 {
224 FormatContextInternal *fci;
225 AVFormatContext *s = *ps;
226 FFFormatContext *si;
227 AVDictionary *tmp = NULL;
228 ID3v2ExtraMeta *id3v2_extra_meta = NULL;
229 int ret = 0;
230
231 if (!s && !(s = avformat_alloc_context()))
232 return AVERROR(ENOMEM);
233 fci = ff_fc_internal(s);
234 si = &fci->fc;
235 if (!s->av_class) {
236 av_log(NULL, AV_LOG_ERROR, "Input context has not been properly allocated by avformat_alloc_context() and is not NULL either\n");
237 return AVERROR(EINVAL);
238 }
239 if (fmt)
240 s->iformat = fmt;
241
242 if (options)
243 av_dict_copy(&tmp, *options, 0);
244
245 if (s->pb) // must be before any goto fail
246 s->flags |= AVFMT_FLAG_CUSTOM_IO;
247
248 if ((ret = av_opt_set_dict(s, &tmp)) < 0)
249 goto fail;
250
251 if (!(s->url = av_strdup(filename ? filename : ""))) {
252 ret = AVERROR(ENOMEM);
253 goto fail;
254 }
255
256 if ((ret = init_input(s, filename, &tmp)) < 0)
257 goto fail;
258 s->probe_score = ret;
259
260 if (!s->protocol_whitelist && s->pb && s->pb->protocol_whitelist) {
261 s->protocol_whitelist = av_strdup(s->pb->protocol_whitelist);
262 if (!s->protocol_whitelist) {
263 ret = AVERROR(ENOMEM);
264 goto fail;
265 }
266 }
267
268 if (!s->protocol_blacklist && s->pb && s->pb->protocol_blacklist) {
269 s->protocol_blacklist = av_strdup(s->pb->protocol_blacklist);
270 if (!s->protocol_blacklist) {
271 ret = AVERROR(ENOMEM);
272 goto fail;
273 }
274 }
275
276 if (s->format_whitelist && av_match_list(s->iformat->name, s->format_whitelist, ',') <= 0) {
277 av_log(s, AV_LOG_ERROR, "Format not on whitelist \'%s\'\n", s->format_whitelist);
278 ret = AVERROR(EINVAL);
279 goto fail;
280 }
281
282 avio_skip(s->pb, s->skip_initial_bytes);
283
284 /* Check filename in case an image number is expected. */
285 if (s->iformat->flags & AVFMT_NEEDNUMBER) {
286 if (!av_filename_number_test(filename)) {
287 ret = AVERROR(EINVAL);
288 goto fail;
289 }
290 }
291
292 s->duration = s->start_time = AV_NOPTS_VALUE;
293
294 /* Allocate private data. */
295 if (ffifmt(s->iformat)->priv_data_size > 0) {
296 if (!(s->priv_data = av_mallocz(ffifmt(s->iformat)->priv_data_size))) {
297 ret = AVERROR(ENOMEM);
298 goto fail;
299 }
300 if (s->iformat->priv_class) {
301 *(const AVClass **) s->priv_data = s->iformat->priv_class;
302 av_opt_set_defaults(s->priv_data);
303 if ((ret = av_opt_set_dict(s->priv_data, &tmp)) < 0)
304 goto fail;
305 }
306 }
307
308 /* e.g. AVFMT_NOFILE formats will not have an AVIOContext */
309 if (s->pb && is_id3v2_format(s->iformat))
310 ff_id3v2_read_dict(s->pb, &si->id3v2_meta, ID3v2_DEFAULT_MAGIC, &id3v2_extra_meta);
311
312 if (ffifmt(s->iformat)->read_header)
313 if ((ret = ffifmt(s->iformat)->read_header(s)) < 0) {
314 if (ffifmt(s->iformat)->flags_internal & FF_INFMT_FLAG_INIT_CLEANUP)
315 goto close;
316 goto fail;
317 }
318
319 if (!s->metadata) {
320 s->metadata = si->id3v2_meta;
321 si->id3v2_meta = NULL;
322 } else if (si->id3v2_meta) {
323 av_log(s, AV_LOG_WARNING, "Discarding ID3 tags because more suitable tags were found.\n");
324 av_dict_free(&si->id3v2_meta);
325 }
326
327 if (id3v2_extra_meta) {
328 if ((ret = ff_id3v2_parse_apic(s, id3v2_extra_meta)) < 0)
329 goto close;
330 if ((ret = ff_id3v2_parse_chapters(s, id3v2_extra_meta)) < 0)
331 goto close;
332 if ((ret = ff_id3v2_parse_priv(s, id3v2_extra_meta)) < 0)
333 goto close;
334 ff_id3v2_free_extra_meta(&id3v2_extra_meta);
335 }
336
337 if ((ret = avformat_queue_attached_pictures(s)) < 0)
338 goto close;
339
340 if (s->pb && !si->data_offset)
341 si->data_offset = avio_tell(s->pb);
342
343 fci->raw_packet_buffer_size = 0;
344
345 update_stream_avctx(s);
346
347 if (options) {
348 av_dict_free(options);
349 *options = tmp;
350 }
351 *ps = s;
352 return 0;
353
354 close:
355 if (ffifmt(s->iformat)->read_close)
356 ffifmt(s->iformat)->read_close(s);
357 fail:
358 ff_id3v2_free_extra_meta(&id3v2_extra_meta);
359 av_dict_free(&tmp);
360 if (s->pb && !(s->flags & AVFMT_FLAG_CUSTOM_IO))
361 avio_closep(&s->pb);
362 avformat_free_context(s);
363 *ps = NULL;
364 return ret;
365 }
366
367 void avformat_close_input(AVFormatContext **ps)
368 {
369 AVFormatContext *s;
370 AVIOContext *pb;
371
372 if (!ps || !*ps)
373 return;
374
375 s = *ps;
376 pb = s->pb;
377
378 if ((s->iformat && strcmp(s->iformat->name, "image2") && s->iformat->flags & AVFMT_NOFILE) ||
379 (s->flags & AVFMT_FLAG_CUSTOM_IO))
380 pb = NULL;
381
382 if (s->iformat)
383 if (ffifmt(s->iformat)->read_close)
384 ffifmt(s->iformat)->read_close(s);
385
386 ff_format_io_close(s, &pb);
387 avformat_free_context(s);
388
389 *ps = NULL;
390 }
391
392 static void force_codec_ids(AVFormatContext *s, AVStream *st)
393 {
394 switch (st->codecpar->codec_type) {
395 case AVMEDIA_TYPE_VIDEO:
396 if (s->video_codec_id)
397 st->codecpar->codec_id = s->video_codec_id;
398 break;
399 case AVMEDIA_TYPE_AUDIO:
400 if (s->audio_codec_id)
401 st->codecpar->codec_id = s->audio_codec_id;
402 break;
403 case AVMEDIA_TYPE_SUBTITLE:
404 if (s->subtitle_codec_id)
405 st->codecpar->codec_id = s->subtitle_codec_id;
406 break;
407 case AVMEDIA_TYPE_DATA:
408 if (s->data_codec_id)
409 st->codecpar->codec_id = s->data_codec_id;
410 break;
411 }
412 }
413
414 static int probe_codec(AVFormatContext *s, AVStream *st, const AVPacket *pkt)
415 {
416 FormatContextInternal *const fci = ff_fc_internal(s);
417 FFStream *const sti = ffstream(st);
418
419 if (sti->request_probe > 0) {
420 AVProbeData *const pd = &sti->probe_data;
421 int end;
422 av_log(s, AV_LOG_DEBUG, "probing stream %d pp:%d\n", st->index, sti->probe_packets);
423 --sti->probe_packets;
424
425 if (pkt) {
426 uint8_t *new_buf = av_realloc(pd->buf, pd->buf_size+pkt->size+AVPROBE_PADDING_SIZE);
427 if (!new_buf) {
428 av_log(s, AV_LOG_WARNING,
429 "Failed to reallocate probe buffer for stream %d\n",
430 st->index);
431 goto no_packet;
432 }
433 pd->buf = new_buf;
434 memcpy(pd->buf + pd->buf_size, pkt->data, pkt->size);
435 pd->buf_size += pkt->size;
436 memset(pd->buf + pd->buf_size, 0, AVPROBE_PADDING_SIZE);
437 } else {
438 no_packet:
439 sti->probe_packets = 0;
440 if (!pd->buf_size) {
441 av_log(s, AV_LOG_WARNING,
442 "nothing to probe for stream %d\n", st->index);
443 }
444 }
445
446 end = fci->raw_packet_buffer_size >= s->probesize ||
447 sti->probe_packets <= 0;
448
449 if (end || av_log2(pd->buf_size) != av_log2(pd->buf_size - pkt->size)) {
450 int score = set_codec_from_probe_data(s, st, pd);
451 if ( (st->codecpar->codec_id != AV_CODEC_ID_NONE && score > AVPROBE_SCORE_STREAM_RETRY)
452 || end) {
453 pd->buf_size = 0;
454 av_freep(&pd->buf);
455 sti->request_probe = -1;
456 if (st->codecpar->codec_id != AV_CODEC_ID_NONE) {
457 av_log(s, AV_LOG_DEBUG, "probed stream %d\n", st->index);
458 } else
459 av_log(s, AV_LOG_WARNING, "probed stream %d failed\n", st->index);
460 }
461 force_codec_ids(s, st);
462 }
463 }
464 return 0;
465 }
466
467 static int update_wrap_reference(AVFormatContext *s, AVStream *st, int stream_index, AVPacket *pkt)
468 {
469 FFStream *const sti = ffstream(st);
470 int64_t ref = pkt->dts;
471 int pts_wrap_behavior;
472 int64_t pts_wrap_reference;
473 AVProgram *first_program;
474
475 if (ref == AV_NOPTS_VALUE)
476 ref = pkt->pts;
477 if (sti->pts_wrap_reference != AV_NOPTS_VALUE || st->pts_wrap_bits >= 63 || ref == AV_NOPTS_VALUE || !s->correct_ts_overflow)
478 return 0;
479 ref &= (1LL << st->pts_wrap_bits)-1;
480
481 // reference time stamp should be 60 s before first time stamp
482 pts_wrap_reference = ref - av_rescale(60, st->time_base.den, st->time_base.num);
483 // if first time stamp is not more than 1/8 and 60s before the wrap point, subtract rather than add wrap offset
484 pts_wrap_behavior = (ref < (1LL << st->pts_wrap_bits) - (1LL << st->pts_wrap_bits-3)) ||
485 (ref < (1LL << st->pts_wrap_bits) - av_rescale(60, st->time_base.den, st->time_base.num)) ?
486 AV_PTS_WRAP_ADD_OFFSET : AV_PTS_WRAP_SUB_OFFSET;
487
488 first_program = av_find_program_from_stream(s, NULL, stream_index);
489
490 if (!first_program) {
491 int default_stream_index = av_find_default_stream_index(s);
492 FFStream *const default_sti = ffstream(s->streams[default_stream_index]);
493 if (default_sti->pts_wrap_reference == AV_NOPTS_VALUE) {
494 for (unsigned i = 0; i < s->nb_streams; i++) {
495 FFStream *const sti = ffstream(s->streams[i]);
496 if (av_find_program_from_stream(s, NULL, i))
497 continue;
498 sti->pts_wrap_reference = pts_wrap_reference;
499 sti->pts_wrap_behavior = pts_wrap_behavior;
500 }
501 } else {
502 sti->pts_wrap_reference = default_sti->pts_wrap_reference;
503 sti->pts_wrap_behavior = default_sti->pts_wrap_behavior;
504 }
505 } else {
506 AVProgram *program = first_program;
507 while (program) {
508 if (program->pts_wrap_reference != AV_NOPTS_VALUE) {
509 pts_wrap_reference = program->pts_wrap_reference;
510 pts_wrap_behavior = program->pts_wrap_behavior;
511 break;
512 }
513 program = av_find_program_from_stream(s, program, stream_index);
514 }
515
516 // update every program with differing pts_wrap_reference
517 program = first_program;
518 while (program) {
519 if (program->pts_wrap_reference != pts_wrap_reference) {
520 for (unsigned i = 0; i < program->nb_stream_indexes; i++) {
521 FFStream *const sti = ffstream(s->streams[program->stream_index[i]]);
522 sti->pts_wrap_reference = pts_wrap_reference;
523 sti->pts_wrap_behavior = pts_wrap_behavior;
524 }
525
526 program->pts_wrap_reference = pts_wrap_reference;
527 program->pts_wrap_behavior = pts_wrap_behavior;
528 }
529 program = av_find_program_from_stream(s, program, stream_index);
530 }
531 }
532 return 1;
533 }
534
535 static void update_timestamps(AVFormatContext *s, AVStream *st, AVPacket *pkt)
536 {
537 FFStream *const sti = ffstream(st);
538
539 if (update_wrap_reference(s, st, pkt->stream_index, pkt) && sti->pts_wrap_behavior == AV_PTS_WRAP_SUB_OFFSET) {
540 // correct first time stamps to negative values
541 if (!is_relative(sti->first_dts))
542 sti->first_dts = wrap_timestamp(st, sti->first_dts);
543 if (!is_relative(st->start_time))
544 st->start_time = wrap_timestamp(st, st->start_time);
545 if (!is_relative(sti->cur_dts))
546 sti->cur_dts = wrap_timestamp(st, sti->cur_dts);
547 }
548
549 pkt->dts = wrap_timestamp(st, pkt->dts);
550 pkt->pts = wrap_timestamp(st, pkt->pts);
551
552 force_codec_ids(s, st);
553
554 /* TODO: audio: time filter; video: frame reordering (pts != dts) */
555 if (s->use_wallclock_as_timestamps)
556 pkt->dts = pkt->pts = av_rescale_q(av_gettime(), AV_TIME_BASE_Q, st->time_base);
557 }
558
559 /**
560 * Handle a new packet and either return it directly if possible and
561 * allow_passthrough is true or queue the packet (or drop the packet
562 * if corrupt).
563 *
564 * @return < 0 on error, 0 if the packet was passed through,
565 * 1 if it was queued or dropped
566 */
567 static int handle_new_packet(AVFormatContext *s, AVPacket *pkt, int allow_passthrough)
568 {
569 FormatContextInternal *const fci = ff_fc_internal(s);
570 AVStream *st;
571 FFStream *sti;
572 int err;
573
574 av_assert0(pkt->stream_index < (unsigned)s->nb_streams &&
575 "Invalid stream index.\n");
576
577 if (pkt->flags & AV_PKT_FLAG_CORRUPT) {
578 av_log(s, AV_LOG_WARNING,
579 "Packet corrupt (stream = %d, dts = %s)%s.\n",
580 pkt->stream_index, av_ts2str(pkt->dts),
581 s->flags & AVFMT_FLAG_DISCARD_CORRUPT ? ", dropping it" : "");
582 if (s->flags & AVFMT_FLAG_DISCARD_CORRUPT) {
583 av_packet_unref(pkt);
584 return 1;
585 }
586 }
587
588 st = s->streams[pkt->stream_index];
589 sti = ffstream(st);
590
591 update_timestamps(s, st, pkt);
592
593 if (sti->request_probe <= 0 && allow_passthrough && !fci->raw_packet_buffer.head)
594 return 0;
595
596 err = avpriv_packet_list_put(&fci->raw_packet_buffer, pkt, NULL, 0);
597 if (err < 0) {
598 av_packet_unref(pkt);
599 return err;
600 }
601
602 pkt = &fci->raw_packet_buffer.tail->pkt;
603 fci->raw_packet_buffer_size += pkt->size;
604
605 err = probe_codec(s, st, pkt);
606 if (err < 0)
607 return err;
608
609 return 1;
610 }
611
612 int ff_buffer_packet(AVFormatContext *s, AVPacket *pkt)
613 {
614 int err = handle_new_packet(s, pkt, 0);
615
616 return err < 0 ? err : 0;
617 }
618
619 int ff_read_packet(AVFormatContext *s, AVPacket *pkt)
620 {
621 FormatContextInternal *const fci = ff_fc_internal(s);
622 int err;
623
624 #if FF_API_INIT_PACKET
625 FF_DISABLE_DEPRECATION_WARNINGS
626 pkt->data = NULL;
627 pkt->size = 0;
628 av_init_packet(pkt);
629 FF_ENABLE_DEPRECATION_WARNINGS
630 #else
631 av_packet_unref(pkt);
632 #endif
633
634 for (;;) {
635 PacketListEntry *pktl = fci->raw_packet_buffer.head;
636
637 if (pktl) {
638 AVStream *const st = s->streams[pktl->pkt.stream_index];
639 if (fci->raw_packet_buffer_size >= s->probesize)
640 if ((err = probe_codec(s, st, NULL)) < 0)
641 return err;
642 if (ffstream(st)->request_probe <= 0) {
643 avpriv_packet_list_get(&fci->raw_packet_buffer, pkt);
644 fci->raw_packet_buffer_size -= pkt->size;
645 return 0;
646 }
647 }
648
649 err = ffifmt(s->iformat)->read_packet(s, pkt);
650 if (err < 0) {
651 av_packet_unref(pkt);
652
653 /* Some demuxers return FFERROR_REDO when they consume
654 data and discard it (ignored streams, junk, extradata).
655 We must re-call the demuxer to get the real packet. */
656 if (err == FFERROR_REDO)
657 continue;
658 if (!pktl || err == AVERROR(EAGAIN))
659 return err;
660 for (unsigned i = 0; i < s->nb_streams; i++) {
661 AVStream *const st = s->streams[i];
662 FFStream *const sti = ffstream(st);
663 if (sti->probe_packets || sti->request_probe > 0)
664 if ((err = probe_codec(s, st, NULL)) < 0)
665 return err;
666 av_assert0(sti->request_probe <= 0);
667 }
668 continue;
669 }
670
671 err = av_packet_make_refcounted(pkt);
672 if (err < 0) {
673 av_packet_unref(pkt);
674 return err;
675 }
676
677 err = handle_new_packet(s, pkt, 1);
678 if (err <= 0) /* Error or passthrough */
679 return err;
680 }
681 }
682
683 /**
684 * Return the frame duration in seconds. Return 0 if not available.
685 */
686 static void compute_frame_duration(AVFormatContext *s, int *pnum, int *pden,
687 AVStream *st, AVCodecParserContext *pc,
688 AVPacket *pkt)
689 {
690 FFStream *const sti = ffstream(st);
691 AVRational codec_framerate = sti->avctx->framerate;
692 int frame_size, sample_rate;
693
694 *pnum = 0;
695 *pden = 0;
696 switch (st->codecpar->codec_type) {
697 case AVMEDIA_TYPE_VIDEO:
698 if (st->r_frame_rate.num && (!pc || !codec_framerate.num)) {
699 *pnum = st->r_frame_rate.den;
700 *pden = st->r_frame_rate.num;
701 } else if ((s->iformat->flags & AVFMT_NOTIMESTAMPS) &&
702 !codec_framerate.num &&
703 st->avg_frame_rate.num && st->avg_frame_rate.den) {
704 *pnum = st->avg_frame_rate.den;
705 *pden = st->avg_frame_rate.num;
706 } else if (st->time_base.num * 1000LL > st->time_base.den) {
707 *pnum = st->time_base.num;
708 *pden = st->time_base.den;
709 } else if (codec_framerate.den * 1000LL > codec_framerate.num) {
710 int ticks_per_frame = (sti->codec_desc &&
711 (sti->codec_desc->props & AV_CODEC_PROP_FIELDS)) ? 2 : 1;
712 av_reduce(pnum, pden,
713 codec_framerate.den,
714 codec_framerate.num * (int64_t)ticks_per_frame,
715 INT_MAX);
716
717 if (pc && pc->repeat_pict) {
718 av_reduce(pnum, pden,
719 (*pnum) * (1LL + pc->repeat_pict),
720 (*pden),
721 INT_MAX);
722 }
723 /* If this codec can be interlaced or progressive then we need
724 * a parser to compute duration of a packet. Thus if we have
725 * no parser in such case leave duration undefined. */
726 if (sti->codec_desc &&
727 (sti->codec_desc->props & AV_CODEC_PROP_FIELDS) && !pc)
728 *pnum = *pden = 0;
729 }
730 break;
731 case AVMEDIA_TYPE_AUDIO:
732 if (sti->avctx_inited) {
733 frame_size = av_get_audio_frame_duration(sti->avctx, pkt->size);
734 sample_rate = sti->avctx->sample_rate;
735 } else {
736 frame_size = av_get_audio_frame_duration2(st->codecpar, pkt->size);
737 sample_rate = st->codecpar->sample_rate;
738 }
739 if (frame_size <= 0 || sample_rate <= 0)
740 break;
741 *pnum = frame_size;
742 *pden = sample_rate;
743 break;
744 default:
745 break;
746 }
747 }
748
749 static int has_decode_delay_been_guessed(AVStream *st)
750 {
751 FFStream *const sti = ffstream(st);
752 if (st->codecpar->codec_id != AV_CODEC_ID_H264) return 1;
753 if (!sti->info) // if we have left find_stream_info then nb_decoded_frames won't increase anymore for stream copy
754 return 1;
755 #if CONFIG_H264_DECODER
756 if (sti->avctx->has_b_frames &&
757 avpriv_h264_has_num_reorder_frames(sti->avctx) == sti->avctx->has_b_frames)
758 return 1;
759 #endif
760 if (sti->avctx->has_b_frames < 3)
761 return sti->nb_decoded_frames >= 7;
762 else if (sti->avctx->has_b_frames < 4)
763 return sti->nb_decoded_frames >= 18;
764 else
765 return sti->nb_decoded_frames >= 20;
766 }
767
768 static PacketListEntry *get_next_pkt(AVFormatContext *s, AVStream *st,
769 PacketListEntry *pktl)
770 {
771 FormatContextInternal *const fci = ff_fc_internal(s);
772 FFFormatContext *const si = &fci->fc;
773 if (pktl->next)
774 return pktl->next;
775 if (pktl == si->packet_buffer.tail)
776 return fci->parse_queue.head;
777 return NULL;
778 }
779
780 static int64_t select_from_pts_buffer(AVStream *st, int64_t *pts_buffer, int64_t dts)
781 {
782 FFStream *const sti = ffstream(st);
783 int onein_oneout = st->codecpar->codec_id != AV_CODEC_ID_H264 &&
784 st->codecpar->codec_id != AV_CODEC_ID_HEVC &&
785 st->codecpar->codec_id != AV_CODEC_ID_VVC;
786
787 if (!onein_oneout) {
788 int delay = sti->avctx->has_b_frames;
789
790 if (dts == AV_NOPTS_VALUE) {
791 int64_t best_score = INT64_MAX;
792 for (int i = 0; i < delay; i++) {
793 if (sti->pts_reorder_error_count[i]) {
794 int64_t score = sti->pts_reorder_error[i] / sti->pts_reorder_error_count[i];
795 if (score < best_score) {
796 best_score = score;
797 dts = pts_buffer[i];
798 }
799 }
800 }
801 } else {
802 for (int i = 0; i < delay; i++) {
803 if (pts_buffer[i] != AV_NOPTS_VALUE) {
804 int64_t diff = FFABS(pts_buffer[i] - dts)
805 + (uint64_t)sti->pts_reorder_error[i];
806 diff = FFMAX(diff, sti->pts_reorder_error[i]);
807 sti->pts_reorder_error[i] = diff;
808 sti->pts_reorder_error_count[i]++;
809 if (sti->pts_reorder_error_count[i] > 250) {
810 sti->pts_reorder_error[i] >>= 1;
811 sti->pts_reorder_error_count[i] >>= 1;
812 }
813 }
814 }
815 }
816 }
817
818 if (dts == AV_NOPTS_VALUE)
819 dts = pts_buffer[0];
820
821 return dts;
822 }
823
824 /**
825 * Updates the dts of packets of a stream in pkt_buffer, by re-ordering the pts
826 * of the packets in a window.
827 */
828 static void update_dts_from_pts(AVFormatContext *s, int stream_index,
829 PacketListEntry *pkt_buffer)
830 {
831 AVStream *const st = s->streams[stream_index];
832 int delay = ffstream(st)->avctx->has_b_frames;
833
834 int64_t pts_buffer[MAX_REORDER_DELAY+1];
835
836 for (int i = 0; i < MAX_REORDER_DELAY + 1; i++)
837 pts_buffer[i] = AV_NOPTS_VALUE;
838
839 for (; pkt_buffer; pkt_buffer = get_next_pkt(s, st, pkt_buffer)) {
840 if (pkt_buffer->pkt.stream_index != stream_index)
841 continue;
842
843 if (pkt_buffer->pkt.pts != AV_NOPTS_VALUE && delay <= MAX_REORDER_DELAY) {
844 pts_buffer[0] = pkt_buffer->pkt.pts;
845 for (int i = 0; i < delay && pts_buffer[i] > pts_buffer[i + 1]; i++)
846 FFSWAP(int64_t, pts_buffer[i], pts_buffer[i + 1]);
847
848 pkt_buffer->pkt.dts = select_from_pts_buffer(st, pts_buffer, pkt_buffer->pkt.dts);
849 }
850 }
851 }
852
853 static void update_initial_timestamps(AVFormatContext *s, int stream_index,
854 int64_t dts, int64_t pts, AVPacket *pkt)
855 {
856 FormatContextInternal *const fci = ff_fc_internal(s);
857 FFFormatContext *const si = &fci->fc;
858 AVStream *const st = s->streams[stream_index];
859 FFStream *const sti = ffstream(st);
860 PacketListEntry *pktl = si->packet_buffer.head ? si->packet_buffer.head : fci->parse_queue.head;
861
862 uint64_t shift;
863
864 if (sti->first_dts != AV_NOPTS_VALUE ||
865 dts == AV_NOPTS_VALUE ||
866 sti->cur_dts == AV_NOPTS_VALUE ||
867 sti->cur_dts < INT_MIN + RELATIVE_TS_BASE ||
868 dts < INT_MIN + (sti->cur_dts - RELATIVE_TS_BASE) ||
869 is_relative(dts))
870 return;
871
872 sti->first_dts = dts - (sti->cur_dts - RELATIVE_TS_BASE);
873 sti->cur_dts = dts;
874 shift = (uint64_t)sti->first_dts - RELATIVE_TS_BASE;
875
876 if (is_relative(pts))
877 pts += shift;
878
879 for (PacketListEntry *pktl_it = pktl; pktl_it; pktl_it = get_next_pkt(s, st, pktl_it)) {
880 if (pktl_it->pkt.stream_index != stream_index)
881 continue;
882 if (is_relative(pktl_it->pkt.pts))
883 pktl_it->pkt.pts += shift;
884
885 if (is_relative(pktl_it->pkt.dts))
886 pktl_it->pkt.dts += shift;
887
888 if (st->start_time == AV_NOPTS_VALUE && pktl_it->pkt.pts != AV_NOPTS_VALUE) {
889 st->start_time = pktl_it->pkt.pts;
890 if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && st->codecpar->sample_rate)
891 st->start_time = av_sat_add64(st->start_time, av_rescale_q(sti->skip_samples, (AVRational){1, st->codecpar->sample_rate}, st->time_base));
892 }
893 }
894
895 if (has_decode_delay_been_guessed(st))
896 update_dts_from_pts(s, stream_index, pktl);
897
898 if (st->start_time == AV_NOPTS_VALUE) {
899 if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO || !(pkt->flags & AV_PKT_FLAG_DISCARD)) {
900 st->start_time = pts;
901 }
902 if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && st->codecpar->sample_rate)
903 st->start_time = av_sat_add64(st->start_time, av_rescale_q(sti->skip_samples, (AVRational){1, st->codecpar->sample_rate}, st->time_base));
904 }
905 }
906
907 static void update_initial_durations(AVFormatContext *s, AVStream *st,
908 int stream_index, int64_t duration)
909 {
910 FormatContextInternal *const fci = ff_fc_internal(s);
911 FFFormatContext *const si = &fci->fc;
912 FFStream *const sti = ffstream(st);
913 PacketListEntry *pktl = si->packet_buffer.head ? si->packet_buffer.head : fci->parse_queue.head;
914 int64_t cur_dts = RELATIVE_TS_BASE;
915
916 if (sti->first_dts != AV_NOPTS_VALUE) {
917 if (sti->update_initial_durations_done)
918 return;
919 sti->update_initial_durations_done = 1;
920 cur_dts = sti->first_dts;
921 for (; pktl; pktl = get_next_pkt(s, st, pktl)) {
922 if (pktl->pkt.stream_index == stream_index) {
923 if (pktl->pkt.pts != pktl->pkt.dts ||
924 pktl->pkt.dts != AV_NOPTS_VALUE ||
925 pktl->pkt.duration)
926 break;
927 cur_dts -= duration;
928 }
929 }
930 if (pktl && pktl->pkt.dts != sti->first_dts) {
931 av_log(s, AV_LOG_DEBUG, "first_dts %s not matching first dts %s (pts %s, duration %"PRId64") in the queue\n",
932 av_ts2str(sti->first_dts), av_ts2str(pktl->pkt.dts), av_ts2str(pktl->pkt.pts), pktl->pkt.duration);
933 return;
934 }
935 if (!pktl) {
936 av_log(s, AV_LOG_DEBUG, "first_dts %s but no packet with dts in the queue\n", av_ts2str(sti->first_dts));
937 return;
938 }
939 pktl = si->packet_buffer.head ? si->packet_buffer.head : fci->parse_queue.head;
940 sti->first_dts = cur_dts;
941 } else if (sti->cur_dts != RELATIVE_TS_BASE)
942 return;
943
944 for (; pktl; pktl = get_next_pkt(s, st, pktl)) {
945 if (pktl->pkt.stream_index != stream_index)
946 continue;
947 if ((pktl->pkt.pts == pktl->pkt.dts ||
948 pktl->pkt.pts == AV_NOPTS_VALUE) &&
949 (pktl->pkt.dts == AV_NOPTS_VALUE ||
950 pktl->pkt.dts == sti->first_dts ||
951 pktl->pkt.dts == RELATIVE_TS_BASE) &&
952 !pktl->pkt.duration &&
953 av_sat_add64(cur_dts, duration) == cur_dts + (uint64_t)duration
954 ) {
955 pktl->pkt.dts = cur_dts;
956 if (!sti->avctx->has_b_frames)
957 pktl->pkt.pts = cur_dts;
958 pktl->pkt.duration = duration;
959 } else
960 break;
961 cur_dts = pktl->pkt.dts + pktl->pkt.duration;
962 }
963 if (!pktl)
964 sti->cur_dts = cur_dts;
965 }
966
967 static void compute_pkt_fields(AVFormatContext *s, AVStream *st,
968 AVCodecParserContext *pc, AVPacket *pkt,
969 int64_t next_dts, int64_t next_pts)
970 {
971 FormatContextInternal *const fci = ff_fc_internal(s);
972 FFFormatContext *const si = &fci->fc;
973 FFStream *const sti = ffstream(st);
974 int num, den, presentation_delayed, delay;
975 int64_t offset;
976 AVRational duration;
977 int onein_oneout = st->codecpar->codec_id != AV_CODEC_ID_H264 &&
978 st->codecpar->codec_id != AV_CODEC_ID_HEVC &&
979 st->codecpar->codec_id != AV_CODEC_ID_VVC;
980
981 if (s->flags & AVFMT_FLAG_NOFILLIN)
982 return;
983
984 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && pkt->dts != AV_NOPTS_VALUE) {
985 if (pkt->dts == pkt->pts && sti->last_dts_for_order_check != AV_NOPTS_VALUE) {
986 if (sti->last_dts_for_order_check <= pkt->dts) {
987 sti->dts_ordered++;
988 } else {
989 av_log(s, sti->dts_misordered ? AV_LOG_DEBUG : AV_LOG_WARNING,
990 "DTS %"PRIi64" < %"PRIi64" out of order\n",
991 pkt->dts,
992 sti->last_dts_for_order_check);
993 sti->dts_misordered++;
994 }
995 if (sti->dts_ordered + sti->dts_misordered > 250) {
996 sti->dts_ordered >>= 1;
997 sti->dts_misordered >>= 1;
998 }
999 }
1000
1001 sti->last_dts_for_order_check = pkt->dts;
1002 if (sti->dts_ordered < 8 * sti->dts_misordered && pkt->dts == pkt->pts)
1003 pkt->dts = AV_NOPTS_VALUE;
1004 }
1005
1006 if ((s->flags & AVFMT_FLAG_IGNDTS) && pkt->pts != AV_NOPTS_VALUE)
1007 pkt->dts = AV_NOPTS_VALUE;
1008
1009 if (pc && pc->pict_type == AV_PICTURE_TYPE_B
1010 && !sti->avctx->has_b_frames)
1011 //FIXME Set low_delay = 0 when has_b_frames = 1
1012 sti->avctx->has_b_frames = 1;
1013
1014 /* do we have a video B-frame ? */
1015 delay = sti->avctx->has_b_frames;
1016 presentation_delayed = 0;
1017
1018 /* XXX: need has_b_frame, but cannot get it if the codec is
1019 * not initialized */
1020 if (delay &&
1021 pc && pc->pict_type != AV_PICTURE_TYPE_B)
1022 presentation_delayed = 1;
1023
1024 if (pkt->pts != AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE &&
1025 st->pts_wrap_bits < 63 && pkt->dts > INT64_MIN + (1LL << st->pts_wrap_bits) &&
1026 pkt->dts - (1LL << (st->pts_wrap_bits - 1)) > pkt->pts) {
1027 if (is_relative(sti->cur_dts) || pkt->dts - (1LL<<(st->pts_wrap_bits - 1)) > sti->cur_dts) {
1028 pkt->dts -= 1LL << st->pts_wrap_bits;
1029 } else
1030 pkt->pts += 1LL << st->pts_wrap_bits;
1031 }
1032
1033 /* Some MPEG-2 in MPEG-PS lack dts (issue #171 / input_file.mpg).
1034 * We take the conservative approach and discard both.
1035 * Note: If this is misbehaving for an H.264 file, then possibly
1036 * presentation_delayed is not set correctly. */
1037 if (delay == 1 && pkt->dts == pkt->pts &&
1038 pkt->dts != AV_NOPTS_VALUE && presentation_delayed) {
1039 av_log(s, AV_LOG_DEBUG, "invalid dts/pts combination %"PRIi64"\n", pkt->dts);
1040 if ( strcmp(s->iformat->name, "mov,mp4,m4a,3gp,3g2,mj2")
1041 && strcmp(s->iformat->name, "flv")) // otherwise we discard correct timestamps for vc1-wmapro.ism
1042 pkt->dts = AV_NOPTS_VALUE;
1043 }
1044
1045 duration = av_mul_q((AVRational) {pkt->duration, 1}, st->time_base);
1046 if (pkt->duration <= 0) {
1047 compute_frame_duration(s, &num, &den, st, pc, pkt);
1048 if (den && num) {
1049 duration = (AVRational) {num, den};
1050 pkt->duration = av_rescale_rnd(1,
1051 num * (int64_t) st->time_base.den,
1052 den * (int64_t) st->time_base.num,
1053 AV_ROUND_DOWN);
1054 }
1055 }
1056
1057 if (pkt->duration > 0 && (si->packet_buffer.head || fci->parse_queue.head))
1058 update_initial_durations(s, st, pkt->stream_index, pkt->duration);
1059
1060 /* Correct timestamps with byte offset if demuxers only have timestamps
1061 * on packet boundaries */
1062 if (pc && sti->need_parsing == AVSTREAM_PARSE_TIMESTAMPS && pkt->size) {
1063 /* this will estimate bitrate based on this frame's duration and size */
1064 offset = av_rescale(pc->offset, pkt->duration, pkt->size);
1065 if (pkt->pts != AV_NOPTS_VALUE)
1066 pkt->pts += offset;
1067 if (pkt->dts != AV_NOPTS_VALUE)
1068 pkt->dts += offset;
1069 }
1070
1071 /* This may be redundant, but it should not hurt. */
1072 if (pkt->dts != AV_NOPTS_VALUE &&
1073 pkt->pts != AV_NOPTS_VALUE &&
1074 pkt->pts > pkt->dts)
1075 presentation_delayed = 1;
1076
1077 if (s->debug & FF_FDEBUG_TS)
1078 av_log(s, AV_LOG_DEBUG,
1079 "IN delayed:%d pts:%s, dts:%s cur_dts:%s st:%d pc:%p duration:%"PRId64" delay:%d onein_oneout:%d\n",
1080 presentation_delayed, av_ts2str(pkt->pts), av_ts2str(pkt->dts), av_ts2str(sti->cur_dts),
1081 pkt->stream_index, pc, pkt->duration, delay, onein_oneout);
1082
1083 /* Interpolate PTS and DTS if they are not present. We skip H264
1084 * currently because delay and has_b_frames are not reliably set. */
1085 if ((delay == 0 || (delay == 1 && pc)) &&
1086 onein_oneout) {
1087 if (presentation_delayed) {
1088 /* DTS = decompression timestamp */
1089 /* PTS = presentation timestamp */
1090 if (pkt->dts == AV_NOPTS_VALUE)
1091 pkt->dts = sti->last_IP_pts;
1092 update_initial_timestamps(s, pkt->stream_index, pkt->dts, pkt->pts, pkt);
1093 if (pkt->dts == AV_NOPTS_VALUE)
1094 pkt->dts = sti->cur_dts;
1095
1096 /* This is tricky: the dts must be incremented by the duration
1097 * of the frame we are displaying, i.e. the last I- or P-frame. */
1098 if (sti->last_IP_duration == 0 && (uint64_t)pkt->duration <= INT32_MAX)
1099 sti->last_IP_duration = pkt->duration;
1100 if (pkt->dts != AV_NOPTS_VALUE)
1101 sti->cur_dts = av_sat_add64(pkt->dts, sti->last_IP_duration);
1102 if (pkt->dts != AV_NOPTS_VALUE &&
1103 pkt->pts == AV_NOPTS_VALUE &&
1104 sti->last_IP_duration > 0 &&
1105 ((uint64_t)sti->cur_dts - (uint64_t)next_dts + 1) <= 2 &&
1106 next_dts != next_pts &&
1107 next_pts != AV_NOPTS_VALUE)
1108 pkt->pts = next_dts;
1109
1110 if ((uint64_t)pkt->duration <= INT32_MAX)
1111 sti->last_IP_duration = pkt->duration;
1112 sti->last_IP_pts = pkt->pts;
1113 /* Cannot compute PTS if not present (we can compute it only
1114 * by knowing the future. */
1115 } else if (pkt->pts != AV_NOPTS_VALUE ||
1116 pkt->dts != AV_NOPTS_VALUE ||
1117 pkt->duration > 0 ) {
1118
1119 /* presentation is not delayed : PTS and DTS are the same */
1120 if (pkt->pts == AV_NOPTS_VALUE)
1121 pkt->pts = pkt->dts;
1122 update_initial_timestamps(s, pkt->stream_index, pkt->pts,
1123 pkt->pts, pkt);
1124 if (pkt->pts == AV_NOPTS_VALUE)
1125 pkt->pts = sti->cur_dts;
1126 pkt->dts = pkt->pts;
1127 if (pkt->pts != AV_NOPTS_VALUE && duration.num >= 0)
1128 sti->cur_dts = av_add_stable(st->time_base, pkt->pts, duration, 1);
1129 }
1130 }
1131
1132 if (pkt->pts != AV_NOPTS_VALUE && delay <= MAX_REORDER_DELAY) {
1133 sti->pts_buffer[0] = pkt->pts;
1134 for (int i = 0; i < delay && sti->pts_buffer[i] > sti->pts_buffer[i + 1]; i++)
1135 FFSWAP(int64_t, sti->pts_buffer[i], sti->pts_buffer[i + 1]);
1136
1137 if (has_decode_delay_been_guessed(st))
1138 pkt->dts = select_from_pts_buffer(st, sti->pts_buffer, pkt->dts);
1139 }
1140 // We skipped it above so we try here.
1141 if (!onein_oneout)
1142 // This should happen on the first packet
1143 update_initial_timestamps(s, pkt->stream_index, pkt->dts, pkt->pts, pkt);
1144 if (pkt->dts > sti->cur_dts)
1145 sti->cur_dts = pkt->dts;
1146
1147 if (s->debug & FF_FDEBUG_TS)
1148 av_log(s, AV_LOG_DEBUG, "OUTdelayed:%d/%d pts:%s, dts:%s cur_dts:%s st:%d (%d)\n",
1149 presentation_delayed, delay, av_ts2str(pkt->pts), av_ts2str(pkt->dts), av_ts2str(sti->cur_dts), st->index, st->id);
1150
1151 /* update flags */
1152 if (st->codecpar->codec_type == AVMEDIA_TYPE_DATA || ff_is_intra_only(st->codecpar->codec_id))
1153 pkt->flags |= AV_PKT_FLAG_KEY;
1154 }
1155
1156 /**
1157 * Parse a packet, add all split parts to parse_queue.
1158 *
1159 * @param pkt Packet to parse; must not be NULL.
1160 * @param flush Indicates whether to flush. If set, pkt must be blank.
1161 */
1162 static int parse_packet(AVFormatContext *s, AVPacket *pkt,
1163 int stream_index, int flush)
1164 {
1165 FormatContextInternal *const fci = ff_fc_internal(s);
1166 FFFormatContext *const si = &fci->fc;
1167 AVPacket *out_pkt = si->parse_pkt;
1168 AVStream *st = s->streams[stream_index];
1169 FFStream *const sti = ffstream(st);
1170 const AVPacketSideData *sd = NULL;
1171 const uint8_t *data = pkt->data;
1172 uint8_t *extradata = sti->avctx->extradata;
1173 int extradata_size = sti->avctx->extradata_size;
1174 int size = pkt->size;
1175 int ret = 0, got_output = flush;
1176
1177 if (!size && !flush && sti->parser->flags & PARSER_FLAG_COMPLETE_FRAMES) {
1178 // preserve 0-size sync packets
1179 compute_pkt_fields(s, st, sti->parser, pkt, pkt->dts, pkt->pts);
1180
1181 // Theora has valid 0-sized packets that need to be output
1182 if (st->codecpar->codec_id == AV_CODEC_ID_THEORA) {
1183 ret = avpriv_packet_list_put(&fci->parse_queue,
1184 pkt, NULL, 0);
1185 if (ret < 0)
1186 goto fail;
1187 }
1188 }
1189
1190 if (pkt->side_data_elems)
1191 sd = av_packet_side_data_get(pkt->side_data, pkt->side_data_elems,
1192 AV_PKT_DATA_NEW_EXTRADATA);
1193 if (sd) {
1194 av_assert1(size && !flush);
1195
1196 sti->avctx->extradata = sd->data;
1197 sti->avctx->extradata_size = sd->size;
1198 }
1199
1200 while (size > 0 || (flush && got_output)) {
1201 int64_t next_pts = pkt->pts;
1202 int64_t next_dts = pkt->dts;
1203 int len;
1204
1205 len = av_parser_parse2(sti->parser, sti->avctx,
1206 &out_pkt->data, &out_pkt->size, data, size,
1207 pkt->pts, pkt->dts, pkt->pos);
1208
1209 pkt->pts = pkt->dts = AV_NOPTS_VALUE;
1210 pkt->pos = -1;
1211 /* increment read pointer */
1212 av_assert1(data || !len);
1213 data = len ? data + len : data;
1214 size -= len;
1215
1216 got_output = !!out_pkt->size;
1217
1218 if (!out_pkt->size)
1219 continue;
1220
1221 if (pkt->buf && out_pkt->data == pkt->data) {
1222 /* reference pkt->buf only when out_pkt->data is guaranteed to point
1223 * to data in it and not in the parser's internal buffer. */
1224 /* XXX: Ensure this is the case with all parsers when sti->parser->flags
1225 * is PARSER_FLAG_COMPLETE_FRAMES and check for that instead? */
1226 out_pkt->buf = av_buffer_ref(pkt->buf);
1227 if (!out_pkt->buf) {
1228 ret = AVERROR(ENOMEM);
1229 goto fail;
1230 }
1231 } else {
1232 ret = av_packet_make_refcounted(out_pkt);
1233 if (ret < 0)
1234 goto fail;
1235 }
1236
1237 if (pkt->side_data) {
1238 out_pkt->side_data = pkt->side_data;
1239 out_pkt->side_data_elems = pkt->side_data_elems;
1240 pkt->side_data = NULL;
1241 pkt->side_data_elems = 0;
1242 }
1243
1244 /* set the duration */
1245 out_pkt->duration = (sti->parser->flags & PARSER_FLAG_COMPLETE_FRAMES) ? pkt->duration : 0;
1246 if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
1247 if (sti->avctx->sample_rate > 0) {
1248 out_pkt->duration =
1249 av_rescale_q_rnd(sti->parser->duration,
1250 (AVRational) { 1, sti->avctx->sample_rate },
1251 st->time_base,
1252 AV_ROUND_DOWN);
1253 }
1254 } else if (st->codecpar->codec_id == AV_CODEC_ID_GIF) {
1255 if (st->time_base.num > 0 && st->time_base.den > 0 &&
1256 sti->parser->duration) {
1257 out_pkt->duration = sti->parser->duration;
1258 }
1259 }
1260
1261 out_pkt->stream_index = st->index;
1262 out_pkt->pts = sti->parser->pts;
1263 out_pkt->dts = sti->parser->dts;
1264 out_pkt->pos = sti->parser->pos;
1265 out_pkt->flags |= pkt->flags & (AV_PKT_FLAG_DISCARD | AV_PKT_FLAG_CORRUPT);
1266
1267 if (sti->need_parsing == AVSTREAM_PARSE_FULL_RAW)
1268 out_pkt->pos = sti->parser->frame_offset;
1269
1270 if (sti->parser->key_frame == 1 ||
1271 (sti->parser->key_frame == -1 &&
1272 sti->parser->pict_type == AV_PICTURE_TYPE_I))
1273 out_pkt->flags |= AV_PKT_FLAG_KEY;
1274
1275 if (sti->parser->key_frame == -1 && sti->parser->pict_type ==AV_PICTURE_TYPE_NONE && (pkt->flags&AV_PKT_FLAG_KEY))
1276 out_pkt->flags |= AV_PKT_FLAG_KEY;
1277
1278 compute_pkt_fields(s, st, sti->parser, out_pkt, next_dts, next_pts);
1279
1280 ret = avpriv_packet_list_put(&fci->parse_queue,
1281 out_pkt, NULL, 0);
1282 if (ret < 0)
1283 goto fail;
1284 }
1285
1286 /* end of the stream => close and free the parser */
1287 if (flush) {
1288 av_parser_close(sti->parser);
1289 sti->parser = NULL;
1290 }
1291
1292 fail:
1293 if (sd) {
1294 sti->avctx->extradata = extradata;
1295 sti->avctx->extradata_size = extradata_size;
1296 }
1297
1298 if (ret < 0)
1299 av_packet_unref(out_pkt);
1300 av_packet_unref(pkt);
1301 return ret;
1302 }
1303
1304 static int64_t ts_to_samples(AVStream *st, int64_t ts)
1305 {
1306 return av_rescale(ts, st->time_base.num * st->codecpar->sample_rate, st->time_base.den);
1307 }
1308
1309 static int codec_close(FFStream *sti)
1310 {
1311 AVCodecContext *avctx_new = NULL;
1312 AVCodecParameters *par_tmp = NULL;
1313 const AVCodec *new_codec = NULL;
1314 int ret;
1315
1316 new_codec =
1317 (sti->avctx->codec_id != sti->pub.codecpar->codec_id) ?
1318 avcodec_find_decoder(sti->pub.codecpar->codec_id) :
1319 sti->avctx->codec;
1320
1321 avctx_new = avcodec_alloc_context3(new_codec);
1322 if (!avctx_new) {
1323 ret = AVERROR(ENOMEM);
1324 goto fail;
1325 }
1326
1327 par_tmp = avcodec_parameters_alloc();
1328 if (!par_tmp) {
1329 ret = AVERROR(ENOMEM);
1330 goto fail;
1331 }
1332
1333 ret = avcodec_parameters_from_context(par_tmp, sti->avctx);
1334 if (ret < 0)
1335 goto fail;
1336
1337 ret = avcodec_parameters_to_context(avctx_new, par_tmp);
1338 if (ret < 0)
1339 goto fail;
1340
1341 avctx_new->pkt_timebase = sti->avctx->pkt_timebase;
1342
1343 avcodec_free_context(&sti->avctx);
1344 sti->avctx = avctx_new;
1345
1346 avctx_new = NULL;
1347 ret = 0;
1348
1349 fail:
1350 avcodec_free_context(&avctx_new);
1351 avcodec_parameters_free(&par_tmp);
1352
1353 return ret;
1354 }
1355
1356 static int extract_extradata(FFFormatContext *si, AVStream *st, const AVPacket *pkt);
1357
1358 static int read_frame_internal(AVFormatContext *s, AVPacket *pkt)
1359 {
1360 FormatContextInternal *const fci = ff_fc_internal(s);
1361 FFFormatContext *const si = &fci->fc;
1362 int ret, got_packet = 0;
1363 AVDictionary *metadata = NULL;
1364
1365 while (!got_packet && !fci->parse_queue.head) {
1366 AVStream *st;
1367 FFStream *sti;
1368
1369 /* read next packet */
1370 ret = ff_read_packet(s, pkt);
1371 if (ret < 0) {
1372 if (ret == AVERROR(EAGAIN))
1373 return ret;
1374 /* flush the parsers */
1375 for (unsigned i = 0; i < s->nb_streams; i++) {
1376 AVStream *const st = s->streams[i];
1377 FFStream *const sti = ffstream(st);
1378 if (sti->parser && sti->need_parsing)
1379 parse_packet(s, pkt, st->index, 1);
1380 }
1381 /* all remaining packets are now in parse_queue =>
1382 * really terminate parsing */
1383 break;
1384 }
1385 ret = 0;
1386 st = s->streams[pkt->stream_index];
1387 sti = ffstream(st);
1388
1389 st->event_flags |= AVSTREAM_EVENT_FLAG_NEW_PACKETS;
1390
1391 int new_extradata = !!av_packet_side_data_get(pkt->side_data, pkt->side_data_elems,
1392 AV_PKT_DATA_NEW_EXTRADATA);
1393 if (new_extradata)
1394 sti->need_context_update = 1;
1395
1396 /* update context if required */
1397 if (sti->need_context_update) {
1398 if (avcodec_is_open(sti->avctx)) {
1399 av_log(s, AV_LOG_DEBUG, "Demuxer context update while decoder is open, closing and trying to re-open\n");
1400 ret = codec_close(sti);
1401 sti->info->found_decoder = 0;
1402 if (ret < 0)
1403 return ret;
1404 }
1405
1406 /* close parser, because it depends on the codec and extradata */
1407 if (sti->parser &&
1408 (sti->avctx->codec_id != st->codecpar->codec_id || new_extradata)) {
1409 av_parser_close(sti->parser);
1410 sti->parser = NULL;
1411 }
1412
1413 ret = avcodec_parameters_to_context(sti->avctx, st->codecpar);
1414 if (ret < 0) {
1415 av_packet_unref(pkt);
1416 return ret;
1417 }
1418
1419 if (!sti->avctx->extradata) {
1420 sti->extract_extradata.inited = 0;
1421
1422 ret = extract_extradata(si, st, pkt);
1423 if (ret < 0) {
1424 av_packet_unref(pkt);
1425 return ret;
1426 }
1427 }
1428
1429 sti->codec_desc = avcodec_descriptor_get(sti->avctx->codec_id);
1430
1431 sti->need_context_update = 0;
1432 }
1433
1434 if (pkt->pts != AV_NOPTS_VALUE &&
1435 pkt->dts != AV_NOPTS_VALUE &&
1436 pkt->pts < pkt->dts) {
1437 av_log(s, AV_LOG_WARNING,
1438 "Invalid timestamps stream=%d, pts=%s, dts=%s, size=%d\n",
1439 pkt->stream_index,
1440 av_ts2str(pkt->pts),
1441 av_ts2str(pkt->dts),
1442 pkt->size);
1443 }
1444 if (s->debug & FF_FDEBUG_TS)
1445 av_log(s, AV_LOG_DEBUG,
1446 "ff_read_packet stream=%d, pts=%s, dts=%s, size=%d, duration=%"PRId64", flags=%d\n",
1447 pkt->stream_index,
1448 av_ts2str(pkt->pts),
1449 av_ts2str(pkt->dts),
1450 pkt->size, pkt->duration, pkt->flags);
1451
1452 if (sti->need_parsing && !sti->parser && !(s->flags & AVFMT_FLAG_NOPARSE)) {
1453 sti->parser = av_parser_init(st->codecpar->codec_id);
1454 if (!sti->parser) {
1455 av_log(s, AV_LOG_VERBOSE, "parser not found for codec "
1456 "%s, packets or times may be invalid.\n",
1457 avcodec_get_name(st->codecpar->codec_id));
1458 /* no parser available: just output the raw packets */
1459 sti->need_parsing = AVSTREAM_PARSE_NONE;
1460 } else if (sti->need_parsing == AVSTREAM_PARSE_HEADERS)
1461 sti->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES;
1462 else if (sti->need_parsing == AVSTREAM_PARSE_FULL_ONCE)
1463 sti->parser->flags |= PARSER_FLAG_ONCE;
1464 else if (sti->need_parsing == AVSTREAM_PARSE_FULL_RAW)
1465 sti->parser->flags |= PARSER_FLAG_USE_CODEC_TS;
1466 }
1467
1468 if (!sti->need_parsing || !sti->parser) {
1469 /* no parsing needed: we just output the packet as is */
1470 compute_pkt_fields(s, st, NULL, pkt, AV_NOPTS_VALUE, AV_NOPTS_VALUE);
1471 if ((s->iformat->flags & AVFMT_GENERIC_INDEX) &&
1472 (pkt->flags & AV_PKT_FLAG_KEY) && pkt->dts != AV_NOPTS_VALUE) {
1473 ff_reduce_index(s, st->index);
1474 av_add_index_entry(st, pkt->pos, pkt->dts,
1475 0, 0, AVINDEX_KEYFRAME);
1476 }
1477 got_packet = 1;
1478 } else if (st->discard < AVDISCARD_ALL) {
1479 if ((ret = parse_packet(s, pkt, pkt->stream_index, 0)) < 0)
1480 return ret;
1481 st->codecpar->sample_rate = sti->avctx->sample_rate;
1482 st->codecpar->bit_rate = sti->avctx->bit_rate;
1483 ret = av_channel_layout_copy(&st->codecpar->ch_layout, &sti->avctx->ch_layout);
1484 if (ret < 0)
1485 return ret;
1486 st->codecpar->codec_id = sti->avctx->codec_id;
1487 } else {
1488 /* free packet */
1489 av_packet_unref(pkt);
1490 }
1491 if (pkt->flags & AV_PKT_FLAG_KEY)
1492 sti->skip_to_keyframe = 0;
1493 if (sti->skip_to_keyframe) {
1494 av_packet_unref(pkt);
1495 got_packet = 0;
1496 }
1497 }
1498
1499 if (!got_packet && fci->parse_queue.head)
1500 ret = avpriv_packet_list_get(&fci->parse_queue, pkt);
1501
1502 if (ret >= 0) {
1503 AVStream *const st = s->streams[pkt->stream_index];
1504 FFStream *const sti = ffstream(st);
1505 int discard_padding = 0;
1506 if (sti->first_discard_sample && pkt->pts != AV_NOPTS_VALUE) {
1507 int64_t pts = pkt->pts - (is_relative(pkt->pts) ? RELATIVE_TS_BASE : 0);
1508 int64_t sample = ts_to_samples(st, pts);
1509 int64_t duration = ts_to_samples(st, pkt->duration);
1510 int64_t end_sample = sample + duration;
1511 if (duration > 0 && end_sample >= sti->first_discard_sample &&
1512 sample < sti->last_discard_sample)
1513 discard_padding = FFMIN(end_sample - sti->first_discard_sample, duration);
1514 }
1515 if (sti->start_skip_samples && (pkt->pts == 0 || pkt->pts == RELATIVE_TS_BASE))
1516 sti->skip_samples = sti->start_skip_samples;
1517 sti->skip_samples = FFMAX(0, sti->skip_samples);
1518 if (sti->skip_samples || discard_padding) {
1519 uint8_t *p = av_packet_new_side_data(pkt, AV_PKT_DATA_SKIP_SAMPLES, 10);
1520 if (p) {
1521 AV_WL32(p, sti->skip_samples);
1522 AV_WL32(p + 4, discard_padding);
1523 av_log(s, AV_LOG_DEBUG, "demuxer injecting skip %u / discard %u\n",
1524 (unsigned)sti->skip_samples, (unsigned)discard_padding);
1525 }
1526 sti->skip_samples = 0;
1527 }
1528 }
1529
1530 if (!fci->metafree) {
1531 int metaret = av_opt_get_dict_val(s, "metadata", AV_OPT_SEARCH_CHILDREN, &metadata);
1532 if (metadata) {
1533 s->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
1534 av_dict_copy(&s->metadata, metadata, 0);
1535 av_dict_free(&metadata);
1536 av_opt_set_dict_val(s, "metadata", NULL, AV_OPT_SEARCH_CHILDREN);
1537 }
1538 fci->metafree = metaret == AVERROR_OPTION_NOT_FOUND;
1539 }
1540
1541 if (s->debug & FF_FDEBUG_TS)
1542 av_log(s, AV_LOG_DEBUG,
1543 "read_frame_internal stream=%d, pts=%s, dts=%s, "
1544 "size=%d, duration=%"PRId64", flags=%d\n",
1545 pkt->stream_index,
1546 av_ts2str(pkt->pts),
1547 av_ts2str(pkt->dts),
1548 pkt->size, pkt->duration, pkt->flags);
1549
1550 /* A demuxer might have returned EOF because of an IO error, let's
1551 * propagate this back to the user. */
1552 if (ret == AVERROR_EOF && s->pb && s->pb->error < 0 && s->pb->error != AVERROR(EAGAIN))
1553 ret = s->pb->error;
1554
1555 return ret;
1556 }
1557
1558 int av_read_frame(AVFormatContext *s, AVPacket *pkt)
1559 {
1560 FFFormatContext *const si = ffformatcontext(s);
1561 const int genpts = s->flags & AVFMT_FLAG_GENPTS;
1562 int eof = 0;
1563 int ret;
1564 AVStream *st;
1565
1566 if (!genpts) {
1567 ret = si->packet_buffer.head
1568 ? avpriv_packet_list_get(&si->packet_buffer, pkt)
1569 : read_frame_internal(s, pkt);
1570 if (ret < 0)
1571 return ret;
1572 goto return_packet;
1573 }
1574
1575 for (;;) {
1576 PacketListEntry *pktl = si->packet_buffer.head;
1577
1578 if (pktl) {
1579 AVPacket *next_pkt = &pktl->pkt;
1580
1581 if (next_pkt->dts != AV_NOPTS_VALUE) {
1582 int wrap_bits = s->streams[next_pkt->stream_index]->pts_wrap_bits;
1583 // last dts seen for this stream. if any of packets following
1584 // current one had no dts, we will set this to AV_NOPTS_VALUE.
1585 int64_t last_dts = next_pkt->dts;
1586 av_assert2(wrap_bits <= 64);
1587 while (pktl && next_pkt->pts == AV_NOPTS_VALUE) {
1588 if (pktl->pkt.stream_index == next_pkt->stream_index &&
1589 av_compare_mod(next_pkt->dts, pktl->pkt.dts, 2ULL << (wrap_bits - 1)) < 0) {
1590 if (av_compare_mod(pktl->pkt.pts, pktl->pkt.dts, 2ULL << (wrap_bits - 1))) {
1591 // not B-frame
1592 next_pkt->pts = pktl->pkt.dts;
1593 }
1594 if (last_dts != AV_NOPTS_VALUE) {
1595 // Once last dts was set to AV_NOPTS_VALUE, we don't change it.
1596 last_dts = pktl->pkt.dts;
1597 }
1598 }
1599 pktl = pktl->next;
1600 }
1601 if (eof && next_pkt->pts == AV_NOPTS_VALUE && last_dts != AV_NOPTS_VALUE) {
1602 // Fixing the last reference frame had none pts issue (For MXF etc).
1603 // We only do this when
1604 // 1. eof.
1605 // 2. we are not able to resolve a pts value for current packet.
1606 // 3. the packets for this stream at the end of the files had valid dts.
1607 next_pkt->pts = last_dts + next_pkt->duration;
1608 }
1609 pktl = si->packet_buffer.head;
1610 }
1611
1612 /* read packet from packet buffer, if there is data */
1613 st = s->streams[next_pkt->stream_index];
1614 if (!(next_pkt->pts == AV_NOPTS_VALUE && st->discard < AVDISCARD_ALL &&
1615 next_pkt->dts != AV_NOPTS_VALUE && !eof)) {
1616 ret = avpriv_packet_list_get(&si->packet_buffer, pkt);
1617 goto return_packet;
1618 }
1619 }
1620
1621 ret = read_frame_internal(s, pkt);
1622 if (ret < 0) {
1623 if (pktl && ret != AVERROR(EAGAIN)) {
1624 eof = 1;
1625 continue;
1626 } else
1627 return ret;
1628 }
1629
1630 ret = avpriv_packet_list_put(&si->packet_buffer,
1631 pkt, NULL, 0);
1632 if (ret < 0) {
1633 av_packet_unref(pkt);
1634 return ret;
1635 }
1636 }
1637
1638 return_packet:
1639 st = s->streams[pkt->stream_index];
1640 if ((s->iformat->flags & AVFMT_GENERIC_INDEX) && pkt->flags & AV_PKT_FLAG_KEY) {
1641 ff_reduce_index(s, st->index);
1642 av_add_index_entry(st, pkt->pos, pkt->dts, 0, 0, AVINDEX_KEYFRAME);
1643 }
1644
1645 if (is_relative(pkt->dts))
1646 pkt->dts -= RELATIVE_TS_BASE;
1647 if (is_relative(pkt->pts))
1648 pkt->pts -= RELATIVE_TS_BASE;
1649
1650 return ret;
1651 }
1652
1653 /**
1654 * Return TRUE if the stream has accurate duration in any stream.
1655 *
1656 * @return TRUE if the stream has accurate duration for at least one component.
1657 */
1658 static int has_duration(AVFormatContext *ic)
1659 {
1660 for (unsigned i = 0; i < ic->nb_streams; i++) {
1661 const AVStream *const st = ic->streams[i];
1662 if (st->duration != AV_NOPTS_VALUE)
1663 return 1;
1664 }
1665 if (ic->duration != AV_NOPTS_VALUE)
1666 return 1;
1667 return 0;
1668 }
1669
1670 /**
1671 * Estimate the stream timings from the one of each components.
1672 *
1673 * Also computes the global bitrate if possible.
1674 */
1675 static void update_stream_timings(AVFormatContext *ic)
1676 {
1677 int64_t start_time, start_time1, start_time_text, end_time, end_time1, end_time_text;
1678 int64_t duration, duration1, duration_text, filesize;
1679
1680 start_time = INT64_MAX;
1681 start_time_text = INT64_MAX;
1682 end_time = INT64_MIN;
1683 end_time_text = INT64_MIN;
1684 duration = INT64_MIN;
1685 duration_text = INT64_MIN;
1686
1687 for (unsigned i = 0; i < ic->nb_streams; i++) {
1688 AVStream *const st = ic->streams[i];
1689 int is_text = st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE ||
1690 st->codecpar->codec_type == AVMEDIA_TYPE_DATA;
1691
1692 if (st->start_time != AV_NOPTS_VALUE && st->time_base.den) {
1693 start_time1 = av_rescale_q(st->start_time, st->time_base,
1694 AV_TIME_BASE_Q);
1695 if (is_text)
1696 start_time_text = FFMIN(start_time_text, start_time1);
1697 else
1698 start_time = FFMIN(start_time, start_time1);
1699 end_time1 = av_rescale_q_rnd(st->duration, st->time_base,
1700 AV_TIME_BASE_Q,
1701 AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
1702 if (end_time1 != AV_NOPTS_VALUE && (end_time1 > 0 ? start_time1 <= INT64_MAX - end_time1 : start_time1 >= INT64_MIN - end_time1)) {
1703 end_time1 += start_time1;
1704 if (is_text)
1705 end_time_text = FFMAX(end_time_text, end_time1);
1706 else
1707 end_time = FFMAX(end_time, end_time1);
1708 }
1709 for (AVProgram *p = NULL; (p = av_find_program_from_stream(ic, p, i)); ) {
1710 if (p->start_time == AV_NOPTS_VALUE || p->start_time > start_time1)
1711 p->start_time = start_time1;
1712 if (p->end_time < end_time1)
1713 p->end_time = end_time1;
1714 }
1715 }
1716 if (st->duration != AV_NOPTS_VALUE) {
1717 duration1 = av_rescale_q(st->duration, st->time_base,
1718 AV_TIME_BASE_Q);
1719 if (is_text)
1720 duration_text = FFMAX(duration_text, duration1);
1721 else
1722 duration = FFMAX(duration, duration1);
1723 }
1724 }
1725 if (start_time == INT64_MAX || (start_time > start_time_text && start_time - (uint64_t)start_time_text < AV_TIME_BASE))
1726 start_time = start_time_text;
1727 else if (start_time > start_time_text)
1728 av_log(ic, AV_LOG_VERBOSE, "Ignoring outlier non primary stream starttime %f\n", start_time_text / (float)AV_TIME_BASE);
1729
1730 if (end_time == INT64_MIN || (end_time < end_time_text && end_time_text - (uint64_t)end_time < AV_TIME_BASE))
1731 end_time = end_time_text;
1732 else if (end_time < end_time_text)
1733 av_log(ic, AV_LOG_VERBOSE, "Ignoring outlier non primary stream endtime %f\n", end_time_text / (float)AV_TIME_BASE);
1734
1735 if (duration == INT64_MIN || (duration < duration_text && (uint64_t)duration_text - duration < AV_TIME_BASE))
1736 duration = duration_text;
1737 else if (duration < duration_text)
1738 av_log(ic, AV_LOG_VERBOSE, "Ignoring outlier non primary stream duration %f\n", duration_text / (float)AV_TIME_BASE);
1739
1740 if (start_time != INT64_MAX) {
1741 ic->start_time = start_time;
1742 if (end_time != INT64_MIN) {
1743 if (ic->nb_programs > 1) {
1744 for (unsigned i = 0; i < ic->nb_programs; i++) {
1745 AVProgram *const p = ic->programs[i];
1746
1747 if (p->start_time != AV_NOPTS_VALUE &&
1748 p->end_time > p->start_time &&
1749 p->end_time - (uint64_t)p->start_time <= INT64_MAX)
1750 duration = FFMAX(duration, p->end_time - p->start_time);
1751 }
1752 } else if (end_time >= start_time && end_time - (uint64_t)start_time <= INT64_MAX) {
1753 duration = FFMAX(duration, end_time - start_time);
1754 }
1755 }
1756 }
1757 if (duration != INT64_MIN && duration > 0 && ic->duration == AV_NOPTS_VALUE) {
1758 ic->duration = duration;
1759 }
1760 if (ic->pb && (filesize = avio_size(ic->pb)) > 0 && ic->duration > 0) {
1761 /* compute the bitrate */
1762 double bitrate = (double) filesize * 8.0 * AV_TIME_BASE /
1763 (double) ic->duration;
1764 if (bitrate >= 0 && bitrate <= INT64_MAX)
1765 ic->bit_rate = bitrate;
1766 }
1767 }
1768
1769 static void fill_all_stream_timings(AVFormatContext *ic)
1770 {
1771 update_stream_timings(ic);
1772 for (unsigned i = 0; i < ic->nb_streams; i++) {
1773 AVStream *const st = ic->streams[i];
1774
1775 if (st->start_time == AV_NOPTS_VALUE) {
1776 if (ic->start_time != AV_NOPTS_VALUE)
1777 st->start_time = av_rescale_q(ic->start_time, AV_TIME_BASE_Q,
1778 st->time_base);
1779 if (ic->duration != AV_NOPTS_VALUE)
1780 st->duration = av_rescale_q(ic->duration, AV_TIME_BASE_Q,
1781 st->time_base);
1782 }
1783 }
1784 }
1785
1786 static void estimate_timings_from_bit_rate(AVFormatContext *ic)
1787 {
1788 FFFormatContext *const si = ffformatcontext(ic);
1789 int show_warning = 0;
1790
1791 /* if bit_rate is already set, we believe it */
1792 if (ic->bit_rate <= 0) {
1793 int64_t bit_rate = 0;
1794 for (unsigned i = 0; i < ic->nb_streams; i++) {
1795 const AVStream *const st = ic->streams[i];
1796 const FFStream *const sti = cffstream(st);
1797 if (st->codecpar->bit_rate <= 0 && sti->avctx->bit_rate > 0)
1798 st->codecpar->bit_rate = sti->avctx->bit_rate;
1799 if (st->codecpar->bit_rate > 0) {
1800 if (INT64_MAX - st->codecpar->bit_rate < bit_rate) {
1801 bit_rate = 0;
1802 break;
1803 }
1804 bit_rate += st->codecpar->bit_rate;
1805 } else if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && sti->codec_info_nb_frames > 1) {
1806 // If we have a videostream with packets but without a bitrate
1807 // then consider the sum not known
1808 bit_rate = 0;
1809 break;
1810 }
1811 }
1812 ic->bit_rate = bit_rate;
1813 }
1814
1815 /* if duration is already set, we believe it */
1816 if (ic->duration == AV_NOPTS_VALUE &&
1817 ic->bit_rate != 0) {
1818 int64_t filesize = ic->pb ? avio_size(ic->pb) : 0;
1819 if (filesize > si->data_offset) {
1820 filesize -= si->data_offset;
1821 for (unsigned i = 0; i < ic->nb_streams; i++) {
1822 AVStream *const st = ic->streams[i];
1823
1824 if ( st->time_base.num <= INT64_MAX / ic->bit_rate
1825 && st->duration == AV_NOPTS_VALUE) {
1826 st->duration = av_rescale(filesize, 8LL * st->time_base.den,
1827 ic->bit_rate *
1828 (int64_t) st->time_base.num);
1829 show_warning = 1;
1830 }
1831 }
1832 }
1833 }
1834 if (show_warning)
1835 av_log(ic, AV_LOG_WARNING,
1836 "Estimating duration from bitrate, this may be inaccurate\n");
1837 }
1838
1839 #define DURATION_DEFAULT_MAX_READ_SIZE 250000LL
1840 #define DURATION_DEFAULT_MAX_RETRY 6
1841 #define DURATION_MAX_RETRY 1
1842
1843 /* only usable for MPEG-PS streams */
1844 static void estimate_timings_from_pts(AVFormatContext *ic, int64_t old_offset)
1845 {
1846 FFFormatContext *const si = ffformatcontext(ic);
1847 AVPacket *const pkt = si->pkt;
1848 int num, den, read_size, ret;
1849 int64_t duration_max_read_size = ic->duration_probesize ? ic->duration_probesize >> DURATION_MAX_RETRY : DURATION_DEFAULT_MAX_READ_SIZE;
1850 int duration_max_retry = ic->duration_probesize ? DURATION_MAX_RETRY : DURATION_DEFAULT_MAX_RETRY;
1851 int found_duration = 0;
1852 int is_end;
1853 int64_t filesize, offset, duration;
1854 int retry = 0;
1855
1856 /* flush packet queue */
1857 ff_flush_packet_queue(ic);
1858
1859 for (unsigned i = 0; i < ic->nb_streams; i++) {
1860 AVStream *const st = ic->streams[i];
1861 FFStream *const sti = ffstream(st);
1862
1863 if (st->start_time == AV_NOPTS_VALUE &&
1864 sti->first_dts == AV_NOPTS_VALUE &&
1865 st->codecpar->codec_type != AVMEDIA_TYPE_UNKNOWN)
1866 av_log(ic, AV_LOG_WARNING,
1867 "start time for stream %d is not set in estimate_timings_from_pts\n", i);
1868
1869 if (sti->parser) {
1870 av_parser_close(sti->parser);
1871 sti->parser = NULL;
1872 }
1873 }
1874
1875 if (ic->skip_estimate_duration_from_pts) {
1876 av_log(ic, AV_LOG_INFO, "Skipping duration calculation in estimate_timings_from_pts\n");
1877 goto skip_duration_calc;
1878 }
1879
1880 av_opt_set_int(ic, "skip_changes", 1, AV_OPT_SEARCH_CHILDREN);
1881 /* estimate the end time (duration) */
1882 /* XXX: may need to support wrapping */
1883 filesize = ic->pb ? avio_size(ic->pb) : 0;
1884 do {
1885 is_end = found_duration;
1886 offset = filesize - (duration_max_read_size << retry);
1887 if (offset < 0)
1888 offset = 0;
1889
1890 avio_seek(ic->pb, offset, SEEK_SET);
1891 read_size = 0;
1892 for (;;) {
1893 AVStream *st;
1894 FFStream *sti;
1895 if (read_size >= duration_max_read_size << (FFMAX(retry - 1, 0)))
1896 break;
1897
1898 do {
1899 ret = ff_read_packet(ic, pkt);
1900 } while (ret == AVERROR(EAGAIN));
1901 if (ret != 0)
1902 break;
1903 read_size += pkt->size;
1904 st = ic->streams[pkt->stream_index];
1905 sti = ffstream(st);
1906 if (pkt->pts != AV_NOPTS_VALUE &&
1907 (st->start_time != AV_NOPTS_VALUE ||
1908 sti->first_dts != AV_NOPTS_VALUE)) {
1909 if (pkt->duration == 0) {
1910 compute_frame_duration(ic, &num, &den, st, sti->parser, pkt);
1911 if (den && num) {
1912 pkt->duration = av_rescale_rnd(1,
1913 num * (int64_t) st->time_base.den,
1914 den * (int64_t) st->time_base.num,
1915 AV_ROUND_DOWN);
1916 }
1917 }
1918 duration = pkt->pts + pkt->duration;
1919 found_duration = 1;
1920 if (st->start_time != AV_NOPTS_VALUE)
1921 duration -= st->start_time;
1922 else
1923 duration -= sti->first_dts;
1924 if (duration > 0) {
1925 if (st->duration == AV_NOPTS_VALUE || sti->info->last_duration<= 0 ||
1926 (st->duration < duration && FFABS(duration - sti->info->last_duration) < 60LL*st->time_base.den / st->time_base.num))
1927 st->duration = duration;
1928 sti->info->last_duration = duration;
1929 }
1930 }
1931 av_packet_unref(pkt);
1932 }
1933
1934 /* check if all audio/video streams have valid duration */
1935 if (!is_end) {
1936 is_end = 1;
1937 for (unsigned i = 0; i < ic->nb_streams; i++) {
1938 const AVStream *const st = ic->streams[i];
1939 switch (st->codecpar->codec_type) {
1940 case AVMEDIA_TYPE_VIDEO:
1941 case AVMEDIA_TYPE_AUDIO:
1942 if (st->duration == AV_NOPTS_VALUE)
1943 is_end = 0;
1944 }
1945 }
1946 }
1947 } while (!is_end &&
1948 offset &&
1949 ++retry <= duration_max_retry);
1950
1951 av_opt_set_int(ic, "skip_changes", 0, AV_OPT_SEARCH_CHILDREN);
1952
1953 /* warn about audio/video streams which duration could not be estimated */
1954 for (unsigned i = 0; i < ic->nb_streams; i++) {
1955 const AVStream *const st = ic->streams[i];
1956 const FFStream *const sti = cffstream(st);
1957
1958 if (st->duration == AV_NOPTS_VALUE) {
1959 switch (st->codecpar->codec_type) {
1960 case AVMEDIA_TYPE_VIDEO:
1961 case AVMEDIA_TYPE_AUDIO:
1962 if (st->start_time != AV_NOPTS_VALUE || sti->first_dts != AV_NOPTS_VALUE) {
1963 av_log(ic, AV_LOG_WARNING, "stream %d : no PTS found at end of file, duration not set\n", i);
1964 } else
1965 av_log(ic, AV_LOG_WARNING, "stream %d : no TS found at start of file, duration not set\n", i);
1966 }
1967 }
1968 }
1969 skip_duration_calc:
1970 fill_all_stream_timings(ic);
1971
1972 avio_seek(ic->pb, old_offset, SEEK_SET);
1973 for (unsigned i = 0; i < ic->nb_streams; i++) {
1974 AVStream *const st = ic->streams[i];
1975 FFStream *const sti = ffstream(st);
1976
1977 sti->cur_dts = sti->first_dts;
1978 sti->last_IP_pts = AV_NOPTS_VALUE;
1979 sti->last_dts_for_order_check = AV_NOPTS_VALUE;
1980 for (int j = 0; j < MAX_REORDER_DELAY + 1; j++)
1981 sti->pts_buffer[j] = AV_NOPTS_VALUE;
1982 }
1983 }
1984
1985 /* 1:1 map to AVDurationEstimationMethod */
1986 static const char *const duration_name[] = {
1987 [AVFMT_DURATION_FROM_PTS] = "pts",
1988 [AVFMT_DURATION_FROM_STREAM] = "stream",
1989 [AVFMT_DURATION_FROM_BITRATE] = "bit rate",
1990 };
1991
1992 static const char *duration_estimate_name(enum AVDurationEstimationMethod method)
1993 {
1994 return duration_name[method];
1995 }
1996
1997 static void estimate_timings(AVFormatContext *ic, int64_t old_offset)
1998 {
1999 int64_t file_size;
2000
2001 /* get the file size, if possible */
2002 if (ic->iformat->flags & AVFMT_NOFILE) {
2003 file_size = 0;
2004 } else {
2005 file_size = avio_size(ic->pb);
2006 file_size = FFMAX(0, file_size);
2007 }
2008
2009 if ((!strcmp(ic->iformat->name, "mpeg") ||
2010 !strcmp(ic->iformat->name, "mpegts")) &&
2011 file_size && (ic->pb->seekable & AVIO_SEEKABLE_NORMAL)) {
2012 /* get accurate estimate from the PTSes */
2013 estimate_timings_from_pts(ic, old_offset);
2014 ic->duration_estimation_method = AVFMT_DURATION_FROM_PTS;
2015 } else if (has_duration(ic)) {
2016 /* at least one component has timings - we use them for all
2017 * the components */
2018 fill_all_stream_timings(ic);
2019 /* nut demuxer estimate the duration from PTS */
2020 if (!strcmp(ic->iformat->name, "nut"))
2021 ic->duration_estimation_method = AVFMT_DURATION_FROM_PTS;
2022 else
2023 ic->duration_estimation_method = AVFMT_DURATION_FROM_STREAM;
2024 } else {
2025 /* less precise: use bitrate info */
2026 estimate_timings_from_bit_rate(ic);
2027 ic->duration_estimation_method = AVFMT_DURATION_FROM_BITRATE;
2028 }
2029 update_stream_timings(ic);
2030
2031 for (unsigned i = 0; i < ic->nb_streams; i++) {
2032 AVStream *const st = ic->streams[i];
2033 if (st->time_base.den)
2034 av_log(ic, AV_LOG_TRACE, "stream %u: start_time: %s duration: %s\n", i,
2035 av_ts2timestr(st->start_time, &st->time_base),
2036 av_ts2timestr(st->duration, &st->time_base));
2037 }
2038 av_log(ic, AV_LOG_TRACE,
2039 "format: start_time: %s duration: %s (estimate from %s) bitrate=%"PRId64" kb/s\n",
2040 av_ts2timestr(ic->start_time, &AV_TIME_BASE_Q),
2041 av_ts2timestr(ic->duration, &AV_TIME_BASE_Q),
2042 duration_estimate_name(ic->duration_estimation_method),
2043 (int64_t)ic->bit_rate / 1000);
2044 }
2045
2046 static int determinable_frame_size(const AVCodecContext *avctx)
2047 {
2048 switch(avctx->codec_id) {
2049 case AV_CODEC_ID_MP1:
2050 case AV_CODEC_ID_MP2:
2051 case AV_CODEC_ID_MP3:
2052 case AV_CODEC_ID_CODEC2:
2053 return 1;
2054 }
2055
2056 return 0;
2057 }
2058
2059 static int has_codec_parameters(const AVStream *st, const char **errmsg_ptr)
2060 {
2061 const FFStream *const sti = cffstream(st);
2062 const AVCodecContext *const avctx = sti->avctx;
2063
2064 #define FAIL(errmsg) do { \
2065 if (errmsg_ptr) \
2066 *errmsg_ptr = errmsg; \
2067 return 0; \
2068 } while (0)
2069
2070 if ( avctx->codec_id == AV_CODEC_ID_NONE
2071 && avctx->codec_type != AVMEDIA_TYPE_DATA)
2072 FAIL("unknown codec");
2073 switch (avctx->codec_type) {
2074 case AVMEDIA_TYPE_AUDIO:
2075 if (!avctx->frame_size && determinable_frame_size(avctx))
2076 FAIL("unspecified frame size");
2077 if (sti->info->found_decoder >= 0 &&
2078 avctx->sample_fmt == AV_SAMPLE_FMT_NONE)
2079 FAIL("unspecified sample format");
2080 if (!avctx->sample_rate)
2081 FAIL("unspecified sample rate");
2082 if (!avctx->ch_layout.nb_channels)
2083 FAIL("unspecified number of channels");
2084 if (sti->info->found_decoder >= 0 && !sti->nb_decoded_frames && avctx->codec_id == AV_CODEC_ID_DTS)
2085 FAIL("no decodable DTS frames");
2086 break;
2087 case AVMEDIA_TYPE_VIDEO:
2088 if (!avctx->width)
2089 FAIL("unspecified size");
2090 if (sti->info->found_decoder >= 0 && avctx->pix_fmt == AV_PIX_FMT_NONE)
2091 FAIL("unspecified pixel format");
2092 if (st->codecpar->codec_id == AV_CODEC_ID_RV30 || st->codecpar->codec_id == AV_CODEC_ID_RV40)
2093 if (!st->sample_aspect_ratio.num && !st->codecpar->sample_aspect_ratio.num && !sti->codec_info_nb_frames)
2094 FAIL("no frame in rv30/40 and no sar");
2095 break;
2096 case AVMEDIA_TYPE_SUBTITLE:
2097 if (avctx->codec_id == AV_CODEC_ID_HDMV_PGS_SUBTITLE && !avctx->width)
2098 FAIL("unspecified size");
2099 break;
2100 case AVMEDIA_TYPE_DATA:
2101 if (avctx->codec_id == AV_CODEC_ID_NONE) return 1;
2102 }
2103
2104 return 1;
2105 }
2106
2107 /* returns 1 or 0 if or if not decoded data was returned, or a negative error */
2108 static int try_decode_frame(AVFormatContext *s, AVStream *st,
2109 const AVPacket *pkt, AVDictionary **options)
2110 {
2111 FFStream *const sti = ffstream(st);
2112 AVCodecContext *const avctx = sti->avctx;
2113 const AVCodec *codec;
2114 int got_picture = 1, ret = 0;
2115 AVFrame *frame = av_frame_alloc();
2116 AVSubtitle subtitle;
2117 int do_skip_frame = 0;
2118 enum AVDiscard skip_frame;
2119 int pkt_to_send = pkt->size > 0;
2120
2121 if (!frame)
2122 return AVERROR(ENOMEM);
2123
2124 if (!avcodec_is_open(avctx) &&
2125 sti->info->found_decoder <= 0 &&
2126 (st->codecpar->codec_id != -sti->info->found_decoder || !st->codecpar->codec_id)) {
2127 AVDictionary *thread_opt = NULL;
2128
2129 codec = find_probe_decoder(s, st, st->codecpar->codec_id);
2130
2131 if (!codec) {
2132 sti->info->found_decoder = -st->codecpar->codec_id;
2133 ret = -1;
2134 goto fail;
2135 }
2136
2137 /* Force thread count to 1 since the H.264 decoder will not extract
2138 * SPS and PPS to extradata during multi-threaded decoding. */
2139 av_dict_set(options ? options : &thread_opt, "threads", "1", 0);
2140 /* Force lowres to 0. The decoder might reduce the video size by the
2141 * lowres factor, and we don't want that propagated to the stream's
2142 * codecpar */
2143 av_dict_set(options ? options : &thread_opt, "lowres", "0", 0);
2144 if (s->codec_whitelist)
2145 av_dict_set(options ? options : &thread_opt, "codec_whitelist", s->codec_whitelist, 0);
2146 ret = avcodec_open2(avctx, codec, options ? options : &thread_opt);
2147 if (!options)
2148 av_dict_free(&thread_opt);
2149 if (ret < 0) {
2150 sti->info->found_decoder = -avctx->codec_id;
2151 goto fail;
2152 }
2153 sti->info->found_decoder = 1;
2154 } else if (!sti->info->found_decoder)
2155 sti->info->found_decoder = 1;
2156
2157 if (sti->info->found_decoder < 0) {
2158 ret = -1;
2159 goto fail;
2160 }
2161
2162 if (avpriv_codec_get_cap_skip_frame_fill_param(avctx->codec)) {
2163 do_skip_frame = 1;
2164 skip_frame = avctx->skip_frame;
2165 avctx->skip_frame = AVDISCARD_ALL;
2166 }
2167
2168 while ((pkt_to_send || (!pkt->data && got_picture)) &&
2169 ret >= 0 &&
2170 (!has_codec_parameters(st, NULL) || !has_decode_delay_been_guessed(st) ||
2171 (!sti->codec_info_nb_frames &&
2172 (avctx->codec->capabilities & AV_CODEC_CAP_CHANNEL_CONF)))) {
2173 got_picture = 0;
2174 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO ||
2175 avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
2176 ret = avcodec_send_packet(avctx, pkt);
2177 if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
2178 break;
2179 if (ret >= 0)
2180 pkt_to_send = 0;
2181 ret = avcodec_receive_frame(avctx, frame);
2182 if (ret >= 0)
2183 got_picture = 1;
2184 if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
2185 ret = 0;
2186 } else if (avctx->codec_type == AVMEDIA_TYPE_SUBTITLE) {
2187 ret = avcodec_decode_subtitle2(avctx, &subtitle,
2188 &got_picture, pkt);
2189 if (got_picture)
2190 avsubtitle_free(&subtitle);
2191 if (ret >= 0)
2192 pkt_to_send = 0;
2193 }
2194 if (ret >= 0) {
2195 if (got_picture)
2196 sti->nb_decoded_frames++;
2197 ret = got_picture;
2198 }
2199 }
2200
2201 fail:
2202 if (do_skip_frame) {
2203 avctx->skip_frame = skip_frame;
2204 }
2205
2206 av_frame_free(&frame);
2207 return ret;
2208 }
2209
2210 static int chapter_start_cmp(const void *p1, const void *p2)
2211 {
2212 const AVChapter *const ch1 = *(AVChapter**)p1;
2213 const AVChapter *const ch2 = *(AVChapter**)p2;
2214 int delta = av_compare_ts(ch1->start, ch1->time_base, ch2->start, ch2->time_base);
2215 if (delta)
2216 return delta;
2217 return FFDIFFSIGN(ch1->id, ch2->id);
2218 }
2219
2220 static int compute_chapters_end(AVFormatContext *s)
2221 {
2222 int64_t max_time = 0;
2223 AVChapter **timetable;
2224
2225 if (!s->nb_chapters)
2226 return 0;
2227
2228 if (s->duration > 0 && s->start_time < INT64_MAX - s->duration)
2229 max_time = s->duration +
2230 ((s->start_time == AV_NOPTS_VALUE) ? 0 : s->start_time);
2231
2232 timetable = av_memdup(s->chapters, s->nb_chapters * sizeof(*timetable));
2233 if (!timetable)
2234 return AVERROR(ENOMEM);
2235 qsort(timetable, s->nb_chapters, sizeof(*timetable), chapter_start_cmp);
2236
2237 for (unsigned i = 0; i < s->nb_chapters; i++)
2238 if (timetable[i]->end == AV_NOPTS_VALUE) {
2239 AVChapter *const ch = timetable[i];
2240 int64_t end = max_time ? av_rescale_q(max_time, AV_TIME_BASE_Q,
2241 ch->time_base)
2242 : INT64_MAX;
2243
2244 if (i + 1 < s->nb_chapters) {
2245 const AVChapter *const ch1 = timetable[i + 1];
2246 int64_t next_start = av_rescale_q(ch1->start, ch1->time_base,
2247 ch->time_base);
2248 if (next_start > ch->start && next_start < end)
2249 end = next_start;
2250 }
2251 ch->end = (end == INT64_MAX || end < ch->start) ? ch->start : end;
2252 }
2253 av_free(timetable);
2254 return 0;
2255 }
2256
2257 static int get_std_framerate(int i)
2258 {
2259 if (i < 30*12)
2260 return (i + 1) * 1001;
2261 i -= 30*12;
2262
2263 if (i < 30)
2264 return (i + 31) * 1001 * 12;
2265 i -= 30;
2266
2267 if (i < 3)
2268 return ((const int[]) { 80, 120, 240})[i] * 1001 * 12;
2269
2270 i -= 3;
2271
2272 return ((const int[]) { 24, 30, 60, 12, 15, 48 })[i] * 1000 * 12;
2273 }
2274
2275 /* Is the time base unreliable?
2276 * This is a heuristic to balance between quick acceptance of the values in
2277 * the headers vs. some extra checks.
2278 * Old DivX and Xvid often have nonsense timebases like 1fps or 2fps.
2279 * MPEG-2 commonly misuses field repeat flags to store different framerates.
2280 * And there are "variable" fps files this needs to detect as well. */
2281 static int tb_unreliable(AVFormatContext *ic, AVStream *st)
2282 {
2283 FFStream *const sti = ffstream(st);
2284 const AVCodecDescriptor *desc = sti->codec_desc;
2285 AVCodecContext *c = sti->avctx;
2286 AVRational mul = (AVRational){ desc && (desc->props & AV_CODEC_PROP_FIELDS) ? 2 : 1, 1 };
2287 AVRational time_base = c->framerate.num ? av_inv_q(av_mul_q(c->framerate, mul))
2288 /* NOHEADER check added to not break existing behavior */
2289 : (((ic->ctx_flags & AVFMTCTX_NOHEADER) ||
2290 st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) ? (AVRational){0, 1}
2291 : st->time_base);
2292
2293 if (time_base.den >= 101LL * time_base.num ||
2294 time_base.den < 5LL * time_base.num ||
2295 // c->codec_tag == AV_RL32("DIVX") ||
2296 // c->codec_tag == AV_RL32("XVID") ||
2297 c->codec_tag == AV_RL32("mp4v") ||
2298 c->codec_id == AV_CODEC_ID_MPEG2VIDEO ||
2299 c->codec_id == AV_CODEC_ID_GIF ||
2300 c->codec_id == AV_CODEC_ID_HEVC ||
2301 c->codec_id == AV_CODEC_ID_H264)
2302 return 1;
2303 return 0;
2304 }
2305
2306 int ff_rfps_add_frame(AVFormatContext *ic, AVStream *st, int64_t ts)
2307 {
2308 FFStream *const sti = ffstream(st);
2309 FFStreamInfo *info = sti->info;
2310 int64_t last = info->last_dts;
2311
2312 if ( ts != AV_NOPTS_VALUE && last != AV_NOPTS_VALUE && ts > last
2313 && ts - (uint64_t)last < INT64_MAX) {
2314 double dts = (is_relative(ts) ? ts - RELATIVE_TS_BASE : ts) * av_q2d(st->time_base);
2315 int64_t duration = ts - last;
2316
2317 if (!info->duration_error)
2318 info->duration_error = av_mallocz(sizeof(info->duration_error[0])*2);
2319 if (!info->duration_error)
2320 return AVERROR(ENOMEM);
2321
2322 // if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
2323 // av_log(NULL, AV_LOG_ERROR, "%f\n", dts);
2324 for (int i = 0; i < MAX_STD_TIMEBASES; i++) {
2325 if (info->duration_error[0][1][i] < 1e10) {
2326 int framerate = get_std_framerate(i);
2327 double sdts = dts*framerate/(1001*12);
2328 for (int j = 0; j < 2; j++) {
2329 int64_t ticks = llrint(sdts+j*0.5);
2330 double error = sdts - ticks + j*0.5;
2331 info->duration_error[j][0][i] += error;
2332 info->duration_error[j][1][i] += error*error;
2333 }
2334 }
2335 }
2336 if (info->rfps_duration_sum <= INT64_MAX - duration) {
2337 info->duration_count++;
2338 info->rfps_duration_sum += duration;
2339 }
2340
2341 if (info->duration_count % 10 == 0) {
2342 int n = info->duration_count;
2343 for (int i = 0; i < MAX_STD_TIMEBASES; i++) {
2344 if (info->duration_error[0][1][i] < 1e10) {
2345 double a0 = info->duration_error[0][0][i] / n;
2346 double error0 = info->duration_error[0][1][i] / n - a0*a0;
2347 double a1 = info->duration_error[1][0][i] / n;
2348 double error1 = info->duration_error[1][1][i] / n - a1*a1;
2349 if (error0 > 0.04 && error1 > 0.04) {
2350 info->duration_error[0][1][i] = 2e10;
2351 info->duration_error[1][1][i] = 2e10;
2352 }
2353 }
2354 }
2355 }
2356
2357 // ignore the first 4 values, they might have some random jitter
2358 if (info->duration_count > 3 && is_relative(ts) == is_relative(last))
2359 info->duration_gcd = av_gcd(info->duration_gcd, duration);
2360 }
2361 if (ts != AV_NOPTS_VALUE)
2362 info->last_dts = ts;
2363
2364 return 0;
2365 }
2366
2367 void ff_rfps_calculate(AVFormatContext *ic)
2368 {
2369 for (unsigned i = 0; i < ic->nb_streams; i++) {
2370 AVStream *const st = ic->streams[i];
2371 FFStream *const sti = ffstream(st);
2372
2373 if (st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO)
2374 continue;
2375 // the check for tb_unreliable() is not completely correct, since this is not about handling
2376 // an unreliable/inexact time base, but a time base that is finer than necessary, as e.g.
2377 // ipmovie.c produces.
2378 if (tb_unreliable(ic, st) && sti->info->duration_count > 15 && sti->info->duration_gcd > FFMAX(1, st->time_base.den/(500LL*st->time_base.num)) && !st->r_frame_rate.num &&
2379 sti->info->duration_gcd < INT64_MAX / st->time_base.num)
2380 av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den, st->time_base.den, st->time_base.num * sti->info->duration_gcd, INT_MAX);
2381 if (sti->info->duration_count > 1 && !st->r_frame_rate.num
2382 && tb_unreliable(ic, st)) {
2383 int num = 0;
2384 double best_error = 0.01;
2385 AVRational ref_rate = st->r_frame_rate.num ? st->r_frame_rate : av_inv_q(st->time_base);
2386
2387 for (int j = 0; j < MAX_STD_TIMEBASES; j++) {
2388 if (sti->info->codec_info_duration &&
2389 sti->info->codec_info_duration*av_q2d(st->time_base) < (1001*11.5)/get_std_framerate(j))
2390 continue;
2391 if (!sti->info->codec_info_duration && get_std_framerate(j) < 1001*12)
2392 continue;
2393
2394 if (av_q2d(st->time_base) * sti->info->rfps_duration_sum / sti->info->duration_count < (1001*12.0 * 0.8)/get_std_framerate(j))
2395 continue;
2396
2397 for (int k = 0; k < 2; k++) {
2398 int n = sti->info->duration_count;
2399 double a = sti->info->duration_error[k][0][j] / n;
2400 double error = sti->info->duration_error[k][1][j]/n - a*a;
2401
2402 if (error < best_error && best_error> 0.000000001) {
2403 best_error= error;
2404 num = get_std_framerate(j);
2405 }
2406 if (error < 0.02)
2407 av_log(ic, AV_LOG_DEBUG, "rfps: %f %f\n", get_std_framerate(j) / 12.0/1001, error);
2408 }
2409 }
2410 // do not increase frame rate by more than 1 % in order to match a standard rate.
2411 if (num && (!ref_rate.num || (double)num/(12*1001) < 1.01 * av_q2d(ref_rate)))
2412 av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den, num, 12*1001, INT_MAX);
2413 }
2414 if ( !st->avg_frame_rate.num
2415 && st->r_frame_rate.num && sti->info->rfps_duration_sum
2416 && sti->info->codec_info_duration <= 0
2417 && sti->info->duration_count > 2
2418 && fabs(1.0 / (av_q2d(st->r_frame_rate) * av_q2d(st->time_base)) - sti->info->rfps_duration_sum / (double)sti->info->duration_count) <= 1.0
2419 ) {
2420 av_log(ic, AV_LOG_DEBUG, "Setting avg frame rate based on r frame rate\n");
2421 st->avg_frame_rate = st->r_frame_rate;
2422 }
2423
2424 av_freep(&sti->info->duration_error);
2425 sti->info->last_dts = AV_NOPTS_VALUE;
2426 sti->info->duration_count = 0;
2427 sti->info->rfps_duration_sum = 0;
2428 }
2429 }
2430
2431 static int extract_extradata_check(AVStream *st)
2432 {
2433 const AVBitStreamFilter *const f = av_bsf_get_by_name("extract_extradata");
2434 if (!f)
2435 return 0;
2436
2437 if (f->codec_ids) {
2438 const enum AVCodecID *ids;
2439 for (ids = f->codec_ids; *ids != AV_CODEC_ID_NONE; ids++)
2440 if (*ids == st->codecpar->codec_id)
2441 return 1;
2442 }
2443
2444 return 0;
2445 }
2446
2447 static int extract_extradata_init(AVStream *st)
2448 {
2449 FFStream *const sti = ffstream(st);
2450 const AVBitStreamFilter *f;
2451 int ret;
2452
2453 f = av_bsf_get_by_name("extract_extradata");
2454 if (!f)
2455 goto finish;
2456
2457 /* check that the codec id is supported */
2458 ret = extract_extradata_check(st);
2459 if (!ret)
2460 goto finish;
2461
2462 av_bsf_free(&sti->extract_extradata.bsf);
2463 ret = av_bsf_alloc(f, &sti->extract_extradata.bsf);
2464 if (ret < 0)
2465 return ret;
2466
2467 ret = avcodec_parameters_copy(sti->extract_extradata.bsf->par_in,
2468 st->codecpar);
2469 if (ret < 0)
2470 goto fail;
2471
2472 sti->extract_extradata.bsf->time_base_in = st->time_base;
2473
2474 ret = av_bsf_init(sti->extract_extradata.bsf);
2475 if (ret < 0)
2476 goto fail;
2477
2478 finish:
2479 sti->extract_extradata.inited = 1;
2480
2481 return 0;
2482 fail:
2483 av_bsf_free(&sti->extract_extradata.bsf);
2484 return ret;
2485 }
2486
2487 static int extract_extradata(FFFormatContext *si, AVStream *st, const AVPacket *pkt)
2488 {
2489 FFStream *const sti = ffstream(st);
2490 AVPacket *const pkt_ref = si->parse_pkt;
2491 int ret;
2492
2493 if (!sti->extract_extradata.inited) {
2494 ret = extract_extradata_init(st);
2495 if (ret < 0)
2496 return ret;
2497 }
2498
2499 if (sti->extract_extradata.inited && !sti->extract_extradata.bsf)
2500 return 0;
2501
2502 ret = av_packet_ref(pkt_ref, pkt);
2503 if (ret < 0)
2504 return ret;
2505
2506 ret = av_bsf_send_packet(sti->extract_extradata.bsf, pkt_ref);
2507 if (ret < 0) {
2508 av_packet_unref(pkt_ref);
2509 return ret;
2510 }
2511
2512 while (ret >= 0 && !sti->avctx->extradata) {
2513 ret = av_bsf_receive_packet(sti->extract_extradata.bsf, pkt_ref);
2514 if (ret < 0) {
2515 if (ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
2516 return ret;
2517 continue;
2518 }
2519
2520 for (int i = 0; i < pkt_ref->side_data_elems; i++) {
2521 AVPacketSideData *const side_data = &pkt_ref->side_data[i];
2522 if (side_data->type == AV_PKT_DATA_NEW_EXTRADATA) {
2523 sti->avctx->extradata = side_data->data;
2524 sti->avctx->extradata_size = side_data->size;
2525 side_data->data = NULL;
2526 side_data->size = 0;
2527 break;
2528 }
2529 }
2530 av_packet_unref(pkt_ref);
2531 }
2532
2533 return 0;
2534 }
2535
2536 static int parameters_from_context(AVFormatContext *ic, AVCodecParameters *par,
2537 const AVCodecContext *avctx)
2538 {
2539 AVCodecParameters *par_tmp;
2540 int ret;
2541
2542 par_tmp = avcodec_parameters_alloc();
2543 if (!par_tmp)
2544 return AVERROR(ENOMEM);
2545
2546 ret = avcodec_parameters_copy(par_tmp, par);
2547 if (ret < 0)
2548 goto fail;
2549
2550 ret = avcodec_parameters_from_context(par, avctx);
2551 if (ret < 0)
2552 goto fail;
2553
2554 /* Restore some values if they are signaled at the container level
2555 * given they may have been replaced by codec level values as read
2556 * internally by avformat_find_stream_info().
2557 */
2558 if (par_tmp->color_range != AVCOL_RANGE_UNSPECIFIED)
2559 par->color_range = par_tmp->color_range;
2560 if (par_tmp->color_primaries != AVCOL_PRI_UNSPECIFIED ||
2561 par_tmp->color_trc != AVCOL_TRC_UNSPECIFIED ||
2562 par_tmp->color_space != AVCOL_SPC_UNSPECIFIED) {
2563 par->color_primaries = par_tmp->color_primaries;
2564 par->color_trc = par_tmp->color_trc;
2565 par->color_space = par_tmp->color_space;
2566 }
2567 if (par_tmp->chroma_location != AVCHROMA_LOC_UNSPECIFIED)
2568 par->chroma_location = par_tmp->chroma_location;
2569
2570 ret = 0;
2571 fail:
2572 avcodec_parameters_free(&par_tmp);
2573
2574 return ret;
2575 }
2576
2577 int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
2578 {
2579 FFFormatContext *const si = ffformatcontext(ic);
2580 int count = 0, ret = 0, err;
2581 int64_t read_size;
2582 AVPacket *pkt1 = si->pkt;
2583 int64_t old_offset = avio_tell(ic->pb);
2584 // new streams might appear, no options for those
2585 int orig_nb_streams = ic->nb_streams;
2586 int flush_codecs;
2587 int64_t max_analyze_duration = ic->max_analyze_duration;
2588 int64_t max_stream_analyze_duration;
2589 int64_t max_subtitle_analyze_duration;
2590 int64_t probesize = ic->probesize;
2591 int eof_reached = 0;
2592
2593 flush_codecs = probesize > 0;
2594
2595 av_opt_set_int(ic, "skip_clear", 1, AV_OPT_SEARCH_CHILDREN);
2596
2597 max_stream_analyze_duration = max_analyze_duration;
2598 max_subtitle_analyze_duration = max_analyze_duration;
2599 if (!max_analyze_duration) {
2600 max_stream_analyze_duration =
2601 max_analyze_duration = 5*AV_TIME_BASE;
2602 max_subtitle_analyze_duration = 30*AV_TIME_BASE;
2603 if (!strcmp(ic->iformat->name, "flv"))
2604 max_stream_analyze_duration = 90*AV_TIME_BASE;
2605 if (!strcmp(ic->iformat->name, "mpeg") || !strcmp(ic->iformat->name, "mpegts"))
2606 max_stream_analyze_duration = 7*AV_TIME_BASE;
2607 }
2608
2609 if (ic->pb) {
2610 FFIOContext *const ctx = ffiocontext(ic->pb);
2611 av_log(ic, AV_LOG_DEBUG, "Before avformat_find_stream_info() pos: %"PRId64" bytes read:%"PRId64" seeks:%d nb_streams:%d\n",
2612 avio_tell(ic->pb), ctx->bytes_read, ctx->seek_count, ic->nb_streams);
2613 }
2614
2615 for (unsigned i = 0; i < ic->nb_streams; i++) {
2616 const AVCodec *codec;
2617 AVDictionary *thread_opt = NULL;
2618 AVStream *const st = ic->streams[i];
2619 FFStream *const sti = ffstream(st);
2620 AVCodecContext *const avctx = sti->avctx;
2621
2622 /* check if the caller has overridden the codec id */
2623 // only for the split stuff
2624 if (!sti->parser && !(ic->flags & AVFMT_FLAG_NOPARSE) && sti->request_probe <= 0) {
2625 sti->parser = av_parser_init(st->codecpar->codec_id);
2626 if (sti->parser) {
2627 if (sti->need_parsing == AVSTREAM_PARSE_HEADERS) {
2628 sti->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES;
2629 } else if (sti->need_parsing == AVSTREAM_PARSE_FULL_RAW) {
2630 sti->parser->flags |= PARSER_FLAG_USE_CODEC_TS;
2631 }
2632 } else if (sti->need_parsing) {
2633 av_log(ic, AV_LOG_VERBOSE, "parser not found for codec "
2634 "%s, packets or times may be invalid.\n",
2635 avcodec_get_name(st->codecpar->codec_id));
2636 }
2637 }
2638
2639 ret = avcodec_parameters_to_context(avctx, st->codecpar);
2640 if (ret < 0)
2641 goto find_stream_info_err;
2642 if (sti->request_probe <= 0)
2643 sti->avctx_inited = 1;
2644
2645 codec = find_probe_decoder(ic, st, st->codecpar->codec_id);
2646
2647 /* Force thread count to 1 since the H.264 decoder will not extract
2648 * SPS and PPS to extradata during multi-threaded decoding. */
2649 av_dict_set(options ? &options[i] : &thread_opt, "threads", "1", 0);
2650 /* Force lowres to 0. The decoder might reduce the video size by the
2651 * lowres factor, and we don't want that propagated to the stream's
2652 * codecpar */
2653 av_dict_set(options ? &options[i] : &thread_opt, "lowres", "0", 0);
2654
2655 if (ic->codec_whitelist)
2656 av_dict_set(options ? &options[i] : &thread_opt, "codec_whitelist", ic->codec_whitelist, 0);
2657
2658 // Try to just open decoders, in case this is enough to get parameters.
2659 // Also ensure that subtitle_header is properly set.
2660 if (!has_codec_parameters(st, NULL) && sti->request_probe <= 0 ||
2661 st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) {
2662 if (codec && !avctx->codec)
2663 if (avcodec_open2(avctx, codec, options ? &options[i] : &thread_opt) < 0)
2664 av_log(ic, AV_LOG_WARNING,
2665 "Failed to open codec in %s\n", __func__);
2666 }
2667 if (!options)
2668 av_dict_free(&thread_opt);
2669 }
2670
2671 read_size = 0;
2672 for (;;) {
2673 const AVPacket *pkt;
2674 AVStream *st;
2675 FFStream *sti;
2676 AVCodecContext *avctx;
2677 int analyzed_all_streams;
2678 unsigned i;
2679 if (ff_check_interrupt(&ic->interrupt_callback)) {
2680 ret = AVERROR_EXIT;
2681 av_log(ic, AV_LOG_DEBUG, "interrupted\n");
2682 break;
2683 }
2684
2685 /* check if one codec still needs to be handled */
2686 for (i = 0; i < ic->nb_streams; i++) {
2687 AVStream *const st = ic->streams[i];
2688 FFStream *const sti = ffstream(st);
2689 int fps_analyze_framecount = 20;
2690 int count;
2691
2692 if (!has_codec_parameters(st, NULL))
2693 break;
2694 /* If the timebase is coarse (like the usual millisecond precision
2695 * of mkv), we need to analyze more frames to reliably arrive at
2696 * the correct fps. */
2697 if (av_q2d(st->time_base) > 0.0005)
2698 fps_analyze_framecount *= 2;
2699 if (!tb_unreliable(ic, st))
2700 fps_analyze_framecount = 0;
2701 if (ic->fps_probe_size >= 0)
2702 fps_analyze_framecount = ic->fps_probe_size;
2703 if (st->disposition & AV_DISPOSITION_ATTACHED_PIC)
2704 fps_analyze_framecount = 0;
2705 /* variable fps and no guess at the real fps */
2706 count = (ic->iformat->flags & AVFMT_NOTIMESTAMPS) ?
2707 sti->info->codec_info_duration_fields/2 :
2708 sti->info->duration_count;
2709 if (!(st->r_frame_rate.num && st->avg_frame_rate.num) &&
2710 st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
2711 if (count < fps_analyze_framecount)
2712 break;
2713 }
2714 // Look at the first 3 frames if there is evidence of frame delay
2715 // but the decoder delay is not set.
2716 if (sti->info->frame_delay_evidence && count < 2 && sti->avctx->has_b_frames == 0)
2717 break;
2718 if (!sti->avctx->extradata &&
2719 (!sti->extract_extradata.inited || sti->extract_extradata.bsf) &&
2720 extract_extradata_check(st))
2721 break;
2722 if (sti->first_dts == AV_NOPTS_VALUE &&
2723 (!(ic->iformat->flags & AVFMT_NOTIMESTAMPS) || sti->need_parsing == AVSTREAM_PARSE_FULL_RAW) &&
2724 sti->codec_info_nb_frames < ((st->disposition & AV_DISPOSITION_ATTACHED_PIC) ? 1 : ic->max_ts_probe) &&
2725 (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ||
2726 st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO))
2727 break;
2728 }
2729 analyzed_all_streams = 0;
2730 if (i == ic->nb_streams && !si->missing_streams) {
2731 analyzed_all_streams = 1;
2732 /* NOTE: If the format has no header, then we need to read some
2733 * packets to get most of the streams, so we cannot stop here. */
2734 if (!(ic->ctx_flags & AVFMTCTX_NOHEADER)) {
2735 /* If we found the info for all the codecs, we can stop. */
2736 ret = count;
2737 av_log(ic, AV_LOG_DEBUG, "All info found\n");
2738 flush_codecs = 0;
2739 break;
2740 }
2741 }
2742 /* We did not get all the codec info, but we read too much data. */
2743 if (read_size >= probesize) {
2744 ret = count;
2745 av_log(ic, AV_LOG_DEBUG,
2746 "Probe buffer size limit of %"PRId64" bytes reached\n", probesize);
2747 for (unsigned i = 0; i < ic->nb_streams; i++) {
2748 AVStream *const st = ic->streams[i];
2749 FFStream *const sti = ffstream(st);
2750 if (!st->r_frame_rate.num &&
2751 sti->info->duration_count <= 1 &&
2752 st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
2753 strcmp(ic->iformat->name, "image2"))
2754 av_log(ic, AV_LOG_WARNING,
2755 "Stream #%d: not enough frames to estimate rate; "
2756 "consider increasing probesize\n", i);
2757 }
2758 break;
2759 }
2760
2761 /* NOTE: A new stream can be added there if no header in file
2762 * (AVFMTCTX_NOHEADER). */
2763 ret = read_frame_internal(ic, pkt1);
2764 if (ret == AVERROR(EAGAIN))
2765 continue;
2766
2767 if (ret < 0) {
2768 /* EOF or error*/
2769 eof_reached = 1;
2770 break;
2771 }
2772
2773 if (!(ic->flags & AVFMT_FLAG_NOBUFFER)) {
2774 ret = avpriv_packet_list_put(&si->packet_buffer,
2775 pkt1, NULL, 0);
2776 if (ret < 0)
2777 goto unref_then_goto_end;
2778
2779 pkt = &si->packet_buffer.tail->pkt;
2780 } else {
2781 pkt = pkt1;
2782 }
2783
2784 st = ic->streams[pkt->stream_index];
2785 sti = ffstream(st);
2786 if (!(st->disposition & AV_DISPOSITION_ATTACHED_PIC))
2787 read_size += pkt->size;
2788
2789 avctx = sti->avctx;
2790 if (!sti->avctx_inited) {
2791 ret = avcodec_parameters_to_context(avctx, st->codecpar);
2792 if (ret < 0)
2793 goto unref_then_goto_end;
2794 sti->avctx_inited = 1;
2795 }
2796
2797 if (pkt->dts != AV_NOPTS_VALUE && sti->codec_info_nb_frames > 1) {
2798 /* check for non-increasing dts */
2799 if (sti->info->fps_last_dts != AV_NOPTS_VALUE &&
2800 sti->info->fps_last_dts >= pkt->dts) {
2801 av_log(ic, AV_LOG_DEBUG,
2802 "Non-increasing DTS in stream %d: packet %d with DTS "
2803 "%"PRId64", packet %d with DTS %"PRId64"\n",
2804 st->index, sti->info->fps_last_dts_idx,
2805 sti->info->fps_last_dts, sti->codec_info_nb_frames,
2806 pkt->dts);
2807 sti->info->fps_first_dts =
2808 sti->info->fps_last_dts = AV_NOPTS_VALUE;
2809 }
2810 /* Check for a discontinuity in dts. If the difference in dts
2811 * is more than 1000 times the average packet duration in the
2812 * sequence, we treat it as a discontinuity. */
2813 if (sti->info->fps_last_dts != AV_NOPTS_VALUE &&
2814 sti->info->fps_last_dts_idx > sti->info->fps_first_dts_idx &&
2815 (pkt->dts - (uint64_t)sti->info->fps_last_dts) / 1000 >
2816 (sti->info->fps_last_dts - (uint64_t)sti->info->fps_first_dts) /
2817 (sti->info->fps_last_dts_idx - sti->info->fps_first_dts_idx)) {
2818 av_log(ic, AV_LOG_WARNING,
2819 "DTS discontinuity in stream %d: packet %d with DTS "
2820 "%"PRId64", packet %d with DTS %"PRId64"\n",
2821 st->index, sti->info->fps_last_dts_idx,
2822 sti->info->fps_last_dts, sti->codec_info_nb_frames,
2823 pkt->dts);
2824 sti->info->fps_first_dts =
2825 sti->info->fps_last_dts = AV_NOPTS_VALUE;
2826 }
2827
2828 /* update stored dts values */
2829 if (sti->info->fps_first_dts == AV_NOPTS_VALUE) {
2830 sti->info->fps_first_dts = pkt->dts;
2831 sti->info->fps_first_dts_idx = sti->codec_info_nb_frames;
2832 }
2833 sti->info->fps_last_dts = pkt->dts;
2834 sti->info->fps_last_dts_idx = sti->codec_info_nb_frames;
2835 }
2836 if (sti->codec_info_nb_frames > 1) {
2837 int64_t t = 0;
2838 int64_t limit;
2839
2840 if (st->time_base.den > 0)
2841 t = av_rescale_q(sti->info->codec_info_duration, st->time_base, AV_TIME_BASE_Q);
2842 if (st->avg_frame_rate.num > 0)
2843 t = FFMAX(t, av_rescale_q(sti->codec_info_nb_frames, av_inv_q(st->avg_frame_rate), AV_TIME_BASE_Q));
2844
2845 if ( t == 0
2846 && sti->codec_info_nb_frames > 30
2847 && sti->info->fps_first_dts != AV_NOPTS_VALUE
2848 && sti->info->fps_last_dts != AV_NOPTS_VALUE) {
2849 int64_t dur = av_sat_sub64(sti->info->fps_last_dts, sti->info->fps_first_dts);
2850 t = FFMAX(t, av_rescale_q(dur, st->time_base, AV_TIME_BASE_Q));
2851 }
2852
2853 if (analyzed_all_streams) limit = max_analyze_duration;
2854 else if (avctx->codec_type == AVMEDIA_TYPE_SUBTITLE) limit = max_subtitle_analyze_duration;
2855 else limit = max_stream_analyze_duration;
2856
2857 if (t >= limit) {
2858 av_log(ic, AV_LOG_VERBOSE, "max_analyze_duration %"PRId64" reached at %"PRId64" microseconds st:%d\n",
2859 limit,
2860 t, pkt->stream_index);
2861 if (ic->flags & AVFMT_FLAG_NOBUFFER)
2862 av_packet_unref(pkt1);
2863 break;
2864 }
2865 if (pkt->duration > 0 && pkt->duration < INT64_MAX - sti->info->codec_info_duration) {
2866 const int fields = sti->codec_desc && (sti->codec_desc->props & AV_CODEC_PROP_FIELDS);
2867 if (avctx->codec_type == AVMEDIA_TYPE_SUBTITLE && pkt->pts != AV_NOPTS_VALUE && st->start_time != AV_NOPTS_VALUE && pkt->pts >= st->start_time
2868 && (uint64_t)pkt->pts - st->start_time < INT64_MAX
2869 ) {
2870 sti->info->codec_info_duration = FFMIN(pkt->pts - st->start_time, sti->info->codec_info_duration + pkt->duration);
2871 } else
2872 sti->info->codec_info_duration += pkt->duration;
2873 sti->info->codec_info_duration_fields += sti->parser && sti->need_parsing && fields
2874 ? sti->parser->repeat_pict + 1 : 2;
2875 }
2876 }
2877 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
2878 #if FF_API_R_FRAME_RATE
2879 ff_rfps_add_frame(ic, st, pkt->dts);
2880 #endif
2881 if (pkt->dts != pkt->pts && pkt->dts != AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE)
2882 sti->info->frame_delay_evidence = 1;
2883 }
2884 if (!sti->avctx->extradata) {
2885 ret = extract_extradata(si, st, pkt);
2886 if (ret < 0)
2887 goto unref_then_goto_end;
2888 }
2889
2890 /* If still no information, we try to open the codec and to
2891 * decompress the frame. We try to avoid that in most cases as
2892 * it takes longer and uses more memory. For MPEG-4, we need to
2893 * decompress for QuickTime.
2894 *
2895 * If AV_CODEC_CAP_CHANNEL_CONF is set this will force decoding of at
2896 * least one frame of codec data, this makes sure the codec initializes
2897 * the channel configuration and does not only trust the values from
2898 * the container. */
2899 try_decode_frame(ic, st, pkt,
2900 (options && i < orig_nb_streams) ? &options[i] : NULL);
2901
2902 if (ic->flags & AVFMT_FLAG_NOBUFFER)
2903 av_packet_unref(pkt1);
2904
2905 sti->codec_info_nb_frames++;
2906 count++;
2907 }
2908
2909 if (eof_reached) {
2910 for (unsigned stream_index = 0; stream_index < ic->nb_streams; stream_index++) {
2911 AVStream *const st = ic->streams[stream_index];
2912 AVCodecContext *const avctx = ffstream(st)->avctx;
2913 if (!has_codec_parameters(st, NULL)) {
2914 const AVCodec *codec = find_probe_decoder(ic, st, st->codecpar->codec_id);
2915 if (codec && !avctx->codec) {
2916 AVDictionary *opts = NULL;
2917 if (ic->codec_whitelist)
2918 av_dict_set(&opts, "codec_whitelist", ic->codec_whitelist, 0);
2919 if (avcodec_open2(avctx, codec, (options && stream_index < orig_nb_streams) ? &options[stream_index] : &opts) < 0)
2920 av_log(ic, AV_LOG_WARNING,
2921 "Failed to open codec in %s\n", __func__);
2922 av_dict_free(&opts);
2923 }
2924 }
2925
2926 // EOF already reached while reading the stream above.
2927 // So continue with reoordering DTS with whatever delay we have.
2928 if (si->packet_buffer.head && !has_decode_delay_been_guessed(st)) {
2929 update_dts_from_pts(ic, stream_index, si->packet_buffer.head);
2930 }
2931 }
2932 }
2933
2934 if (flush_codecs) {
2935 AVPacket *empty_pkt = si->pkt;
2936 int err = 0;
2937 av_packet_unref(empty_pkt);
2938
2939 for (unsigned i = 0; i < ic->nb_streams; i++) {
2940 AVStream *const st = ic->streams[i];
2941 FFStream *const sti = ffstream(st);
2942
2943 /* flush the decoders */
2944 if (sti->info->found_decoder == 1) {
2945 err = try_decode_frame(ic, st, empty_pkt,
2946 (options && i < orig_nb_streams)
2947 ? &options[i] : NULL);
2948
2949 if (err < 0) {
2950 av_log(ic, AV_LOG_INFO,
2951 "decoding for stream %d failed\n", st->index);
2952 }
2953 }
2954 }
2955 }
2956
2957 ff_rfps_calculate(ic);
2958
2959 for (unsigned i = 0; i < ic->nb_streams; i++) {
2960 AVStream *const st = ic->streams[i];
2961 FFStream *const sti = ffstream(st);
2962 AVCodecContext *const avctx = sti->avctx;
2963
2964 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
2965 if (avctx->codec_id == AV_CODEC_ID_RAWVIDEO && !avctx->codec_tag && !avctx->bits_per_coded_sample) {
2966 uint32_t tag= avcodec_pix_fmt_to_codec_tag(avctx->pix_fmt);
2967 if (avpriv_pix_fmt_find(PIX_FMT_LIST_RAW, tag) == avctx->pix_fmt)
2968 avctx->codec_tag= tag;
2969 }
2970
2971 /* estimate average framerate if not set by demuxer */
2972 if (sti->info->codec_info_duration_fields &&
2973 !st->avg_frame_rate.num &&
2974 sti->info->codec_info_duration) {
2975 int best_fps = 0;
2976 double best_error = 0.01;
2977 AVRational codec_frame_rate = avctx->framerate;
2978
2979 if (sti->info->codec_info_duration >= INT64_MAX / st->time_base.num / 2||
2980 sti->info->codec_info_duration_fields >= INT64_MAX / st->time_base.den ||
2981 sti->info->codec_info_duration < 0)
2982 continue;
2983 av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den,
2984 sti->info->codec_info_duration_fields * (int64_t) st->time_base.den,
2985 sti->info->codec_info_duration * 2 * (int64_t) st->time_base.num, 60000);
2986
2987 /* Round guessed framerate to a "standard" framerate if it's
2988 * within 1% of the original estimate. */
2989 for (int j = 0; j < MAX_STD_TIMEBASES; j++) {
2990 AVRational std_fps = { get_std_framerate(j), 12 * 1001 };
2991 double error = fabs(av_q2d(st->avg_frame_rate) /
2992 av_q2d(std_fps) - 1);
2993
2994 if (error < best_error) {
2995 best_error = error;
2996 best_fps = std_fps.num;
2997 }
2998
2999 if ((ffifmt(ic->iformat)->flags_internal & FF_INFMT_FLAG_PREFER_CODEC_FRAMERATE) &&
3000 codec_frame_rate.num > 0 && codec_frame_rate.den > 0) {
3001 error = fabs(av_q2d(codec_frame_rate) /
3002 av_q2d(std_fps) - 1);
3003 if (error < best_error) {
3004 best_error = error;
3005 best_fps = std_fps.num;
3006 }
3007 }
3008 }
3009 if (best_fps)
3010 av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den,
3011 best_fps, 12 * 1001, INT_MAX);
3012 }
3013 if (!st->r_frame_rate.num) {
3014 const AVCodecDescriptor *desc = sti->codec_desc;
3015 AVRational mul = (AVRational){ desc && (desc->props & AV_CODEC_PROP_FIELDS) ? 2 : 1, 1 };
3016 AVRational fr = av_mul_q(avctx->framerate, mul);
3017
3018 if (fr.num && fr.den && av_cmp_q(st->time_base, av_inv_q(fr)) <= 0) {
3019 st->r_frame_rate = fr;
3020 } else {
3021 st->r_frame_rate.num = st->time_base.den;
3022 st->r_frame_rate.den = st->time_base.num;
3023 }
3024 }
3025 st->codecpar->framerate = avctx->framerate;
3026 if (sti->display_aspect_ratio.num && sti->display_aspect_ratio.den) {
3027 AVRational hw_ratio = { avctx->height, avctx->width };
3028 st->sample_aspect_ratio = av_mul_q(sti->display_aspect_ratio,
3029 hw_ratio);
3030 }
3031 } else if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
3032 if (!avctx->bits_per_coded_sample)
3033 avctx->bits_per_coded_sample =
3034 av_get_bits_per_sample(avctx->codec_id);
3035 // set stream disposition based on audio service type
3036 switch (avctx->audio_service_type) {
3037 case AV_AUDIO_SERVICE_TYPE_EFFECTS:
3038 st->disposition = AV_DISPOSITION_CLEAN_EFFECTS;
3039 break;
3040 case AV_AUDIO_SERVICE_TYPE_VISUALLY_IMPAIRED:
3041 st->disposition = AV_DISPOSITION_VISUAL_IMPAIRED;
3042 break;
3043 case AV_AUDIO_SERVICE_TYPE_HEARING_IMPAIRED:
3044 st->disposition = AV_DISPOSITION_HEARING_IMPAIRED;
3045 break;
3046 case AV_AUDIO_SERVICE_TYPE_COMMENTARY:
3047 st->disposition = AV_DISPOSITION_COMMENT;
3048 break;
3049 case AV_AUDIO_SERVICE_TYPE_KARAOKE:
3050 st->disposition = AV_DISPOSITION_KARAOKE;
3051 break;
3052 }
3053 }
3054 }
3055
3056 if (probesize)
3057 estimate_timings(ic, old_offset);
3058
3059 av_opt_set_int(ic, "skip_clear", 0, AV_OPT_SEARCH_CHILDREN);
3060
3061 if (ret >= 0 && ic->nb_streams)
3062 /* We could not have all the codec parameters before EOF. */
3063 ret = -1;
3064 for (unsigned i = 0; i < ic->nb_streams; i++) {
3065 AVStream *const st = ic->streams[i];
3066 FFStream *const sti = ffstream(st);
3067 const char *errmsg;
3068
3069 /* if no packet was ever seen, update context now for has_codec_parameters */
3070 if (!sti->avctx_inited) {
3071 if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
3072 st->codecpar->format == AV_SAMPLE_FMT_NONE)
3073 st->codecpar->format = sti->avctx->sample_fmt;
3074 ret = avcodec_parameters_to_context(sti->avctx, st->codecpar);
3075 if (ret < 0)
3076 goto find_stream_info_err;
3077 }
3078 if (!has_codec_parameters(st, &errmsg)) {
3079 char buf[256];
3080 avcodec_string(buf, sizeof(buf), sti->avctx, 0);
3081 av_log(ic, AV_LOG_WARNING,
3082 "Could not find codec parameters for stream %d (%s): %s\n"
3083 "Consider increasing the value for the 'analyzeduration' (%"PRId64") and 'probesize' (%"PRId64") options\n",
3084 i, buf, errmsg, ic->max_analyze_duration, ic->probesize);
3085 } else {
3086 ret = 0;
3087 }
3088 }
3089
3090 err = compute_chapters_end(ic);
3091 if (err < 0) {
3092 ret = err;
3093 goto find_stream_info_err;
3094 }
3095
3096 /* update the stream parameters from the internal codec contexts */
3097 for (unsigned i = 0; i < ic->nb_streams; i++) {
3098 AVStream *const st = ic->streams[i];
3099 FFStream *const sti = ffstream(st);
3100
3101 if (sti->avctx_inited) {
3102 ret = parameters_from_context(ic, st->codecpar, sti->avctx);
3103 if (ret < 0)
3104 goto find_stream_info_err;
3105
3106 if (sti->avctx->rc_buffer_size > 0 || sti->avctx->rc_max_rate > 0 ||
3107 sti->avctx->rc_min_rate) {
3108 size_t cpb_size;
3109 AVCPBProperties *props = av_cpb_properties_alloc(&cpb_size);
3110 if (props) {
3111 if (sti->avctx->rc_buffer_size > 0)
3112 props->buffer_size = sti->avctx->rc_buffer_size;
3113 if (sti->avctx->rc_min_rate > 0)
3114 props->min_bitrate = sti->avctx->rc_min_rate;
3115 if (sti->avctx->rc_max_rate > 0)
3116 props->max_bitrate = sti->avctx->rc_max_rate;
3117 if (!av_packet_side_data_add(&st->codecpar->coded_side_data,
3118 &st->codecpar->nb_coded_side_data,
3119 AV_PKT_DATA_CPB_PROPERTIES,
3120 (uint8_t *)props, cpb_size, 0))
3121 av_free(props);
3122 }
3123 }
3124 }
3125
3126 sti->avctx_inited = 0;
3127 }
3128
3129 find_stream_info_err:
3130 for (unsigned i = 0; i < ic->nb_streams; i++) {
3131 AVStream *const st = ic->streams[i];
3132 FFStream *const sti = ffstream(st);
3133 int err;
3134
3135 if (sti->info) {
3136 av_freep(&sti->info->duration_error);
3137 av_freep(&sti->info);
3138 }
3139
3140 if (avcodec_is_open(sti->avctx)) {
3141 err = codec_close(sti);
3142 if (err < 0 && ret >= 0)
3143 ret = err;
3144 }
3145
3146 av_bsf_free(&sti->extract_extradata.bsf);
3147 }
3148 if (ic->pb) {
3149 FFIOContext *const ctx = ffiocontext(ic->pb);
3150 av_log(ic, AV_LOG_DEBUG, "After avformat_find_stream_info() pos: %"PRId64" bytes read:%"PRId64" seeks:%d frames:%d\n",
3151 avio_tell(ic->pb), ctx->bytes_read, ctx->seek_count, count);
3152 }
3153 return ret;
3154
3155 unref_then_goto_end:
3156 av_packet_unref(pkt1);
3157 goto find_stream_info_err;
3158 }