FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/demux.c
Date: 2026-05-02 03:33:10
Exec Total Coverage
Lines: 1721 1963 87.7%
Functions: 50 50 100.0%
Branches: 1559 1921 81.2%

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