FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/demux.c
Date: 2025-06-01 09:29:47
Exec Total Coverage
Lines: 1641 1890 86.8%
Functions: 48 48 100.0%
Branches: 1493 1859 80.3%

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