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