FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/demux.c
Date: 2025-08-19 23:55:23
Exec Total Coverage
Lines: 1646 1895 86.9%
Functions: 48 48 100.0%
Branches: 1495 1861 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 2494148 static int64_t wrap_timestamp(const AVStream *st, int64_t timestamp)
54 {
55 2494148 const FFStream *const sti = cffstream(st);
56
4/4
✓ Branch 0 taken 151827 times.
✓ Branch 1 taken 2342321 times.
✓ Branch 2 taken 146837 times.
✓ Branch 3 taken 4990 times.
2494148 if (sti->pts_wrap_behavior != AV_PTS_WRAP_IGNORE && st->pts_wrap_bits < 64 &&
57
3/4
✓ Branch 0 taken 146837 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 88938 times.
✓ Branch 3 taken 57899 times.
146837 sti->pts_wrap_reference != AV_NOPTS_VALUE && timestamp != AV_NOPTS_VALUE) {
58
2/2
✓ Branch 0 taken 88734 times.
✓ Branch 1 taken 204 times.
88938 if (sti->pts_wrap_behavior == AV_PTS_WRAP_ADD_OFFSET &&
59
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 88734 times.
88734 timestamp < sti->pts_wrap_reference)
60 return timestamp + (1ULL << st->pts_wrap_bits);
61
2/2
✓ Branch 0 taken 204 times.
✓ Branch 1 taken 88734 times.
88938 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 2494098 return timestamp;
66 }
67
68 304560 int64_t ff_wrap_timestamp(const AVStream *st, int64_t timestamp)
69 {
70 304560 return wrap_timestamp(st, timestamp);
71 }
72
73 9822 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 337 times.
✓ Branch 1 taken 9485 times.
9822 if (codec_id == AV_CODEC_ID_H264)
81 337 return avcodec_find_decoder_by_name("h264");
82 #endif
83
84 9485 codec = ff_find_decoder(s, st, codec_id);
85
2/2
✓ Branch 0 taken 168 times.
✓ Branch 1 taken 9317 times.
9485 if (!codec)
86 168 return NULL;
87
88
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9317 times.
9317 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 9317 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 7735 static int init_input(AVFormatContext *s, const char *filename,
159 AVDictionary **options)
160 {
161 int ret;
162 7735 AVProbeData pd = { filename, NULL, 0 };
163 7735 int score = AVPROBE_SCORE_RETRY;
164
165
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 7722 times.
7735 if (s->pb) {
166 13 s->flags |= AVFMT_FLAG_CUSTOM_IO;
167
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 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 13 times.
13 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 13 return 0;
174 }
175
176
4/4
✓ Branch 0 taken 3827 times.
✓ Branch 1 taken 3895 times.
✓ Branch 2 taken 862 times.
✓ Branch 3 taken 2965 times.
7722 if ((s->iformat && s->iformat->flags & AVFMT_NOFILE) ||
177
4/4
✓ Branch 0 taken 3895 times.
✓ Branch 1 taken 862 times.
✓ Branch 3 taken 193 times.
✓ Branch 4 taken 3702 times.
4757 (!s->iformat && (s->iformat = av_probe_input_format2(&pd, 0, &score))))
178 3158 return score;
179
180
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4564 times.
4564 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 862 times.
✓ Branch 1 taken 3702 times.
4564 if (s->iformat)
184 862 return 0;
185 3702 return av_probe_input_buffer2(s->pb, &s->iformat, filename,
186 3702 s, 0, s->format_probesize);
187 }
188
189 7735 static int update_stream_avctx(AVFormatContext *s)
190 {
191 int ret;
192
2/2
✓ Branch 0 taken 8370 times.
✓ Branch 1 taken 7735 times.
16105 for (unsigned i = 0; i < s->nb_streams; i++) {
193 8370 AVStream *const st = s->streams[i];
194 8370 FFStream *const sti = ffstream(st);
195
196
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8370 times.
8370 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 8370 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
8370 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 8370 ret = avcodec_parameters_to_context(sti->avctx, st->codecpar);
207
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8370 times.
8370 if (ret < 0)
208 return ret;
209
210 8370 sti->codec_desc = avcodec_descriptor_get(sti->avctx->codec_id);
211
212 8370 sti->need_context_update = 0;
213 }
214 7735 return 0;
215 }
216
217 7735 int avformat_open_input(AVFormatContext **ps, const char *filename,
218 const AVInputFormat *fmt, AVDictionary **options)
219 {
220 FormatContextInternal *fci;
221 7735 AVFormatContext *s = *ps;
222 FFFormatContext *si;
223 7735 AVDictionary *tmp = NULL;
224 7735 ID3v2ExtraMeta *id3v2_extra_meta = NULL;
225 7735 int ret = 0;
226
227
3/4
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 7709 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 26 times.
7735 if (!s && !(s = avformat_alloc_context()))
228 return AVERROR(ENOMEM);
229 7735 fci = ff_fc_internal(s);
230 7735 si = &fci->fc;
231
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7735 times.
7735 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 3840 times.
✓ Branch 1 taken 3895 times.
7735 if (fmt)
236 3840 s->iformat = fmt;
237
238
2/2
✓ Branch 0 taken 7722 times.
✓ Branch 1 taken 13 times.
7735 if (options)
239 7722 av_dict_copy(&tmp, *options, 0);
240
241
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 7722 times.
7735 if (s->pb) // must be before any goto fail
242 13 s->flags |= AVFMT_FLAG_CUSTOM_IO;
243
244
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7735 times.
7735 if ((ret = av_opt_set_dict(s, &tmp)) < 0)
245 goto fail;
246
247
2/4
✓ Branch 0 taken 7735 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 7735 times.
7735 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 7735 times.
7735 if ((ret = init_input(s, filename, &tmp)) < 0)
253 goto fail;
254 7735 s->probe_score = ret;
255
256
6/6
✓ Branch 0 taken 7651 times.
✓ Branch 1 taken 84 times.
✓ Branch 2 taken 4493 times.
✓ Branch 3 taken 3158 times.
✓ Branch 4 taken 4492 times.
✓ Branch 5 taken 1 times.
7735 if (!s->protocol_whitelist && s->pb && s->pb->protocol_whitelist) {
257 4492 s->protocol_whitelist = av_strdup(s->pb->protocol_whitelist);
258
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4492 times.
4492 if (!s->protocol_whitelist) {
259 ret = AVERROR(ENOMEM);
260 goto fail;
261 }
262 }
263
264
4/6
✓ Branch 0 taken 7735 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4577 times.
✓ Branch 3 taken 3158 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 4577 times.
7735 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 7735 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
7735 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 7735 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 7735 times.
7735 if (s->iformat->flags & AVFMT_NEEDNUMBER) {
282 if (!av_filename_number_test(filename)) {
283 ret = AVERROR(EINVAL);
284 goto fail;
285 }
286 }
287
288 7735 s->duration = s->start_time = AV_NOPTS_VALUE;
289
290 /* Allocate private data. */
291
2/2
✓ Branch 1 taken 7577 times.
✓ Branch 2 taken 158 times.
7735 if (ffifmt(s->iformat)->priv_data_size > 0) {
292
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 7577 times.
7577 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 6740 times.
✓ Branch 1 taken 837 times.
7577 if (s->iformat->priv_class) {
297 6740 *(const AVClass **) s->priv_data = s->iformat->priv_class;
298 6740 av_opt_set_defaults(s->priv_data);
299
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6740 times.
6740 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 4577 times.
✓ Branch 1 taken 3158 times.
7735 if (s->pb)
306 4577 ff_id3v2_read_dict(s->pb, &si->id3v2_meta, ID3v2_DEFAULT_MAGIC, &id3v2_extra_meta);
307
308
1/2
✓ Branch 1 taken 7735 times.
✗ Branch 2 not taken.
7735 if (ffifmt(s->iformat)->read_header)
309
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 7735 times.
7735 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 6438 times.
✓ Branch 1 taken 1297 times.
7735 if (!s->metadata) {
316 6438 s->metadata = si->id3v2_meta;
317 6438 si->id3v2_meta = NULL;
318
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1297 times.
1297 } 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 7725 times.
7735 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 7735 times.
7735 if ((ret = avformat_queue_attached_pictures(s)) < 0)
338 goto close;
339
340
4/4
✓ Branch 0 taken 4577 times.
✓ Branch 1 taken 3158 times.
✓ Branch 2 taken 4135 times.
✓ Branch 3 taken 442 times.
7735 if (s->pb && !si->data_offset)
341 4135 si->data_offset = avio_tell(s->pb);
342
343 7735 fci->raw_packet_buffer_size = 0;
344
345 7735 update_stream_avctx(s);
346
347
2/2
✓ Branch 0 taken 7722 times.
✓ Branch 1 taken 13 times.
7735 if (options) {
348 7722 av_dict_free(options);
349 7722 *options = tmp;
350 }
351 7735 *ps = s;
352 7735 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 7735 void avformat_close_input(AVFormatContext **ps)
368 {
369 AVFormatContext *s;
370 AVIOContext *pb;
371
372
2/4
✓ Branch 0 taken 7735 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 7735 times.
7735 if (!ps || !*ps)
373 return;
374
375 7735 s = *ps;
376 7735 pb = s->pb;
377
378
5/6
✓ Branch 0 taken 7735 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4601 times.
✓ Branch 3 taken 3134 times.
✓ Branch 4 taken 4552 times.
✓ Branch 5 taken 49 times.
7735 if ((s->iformat && strcmp(s->iformat->name, "image2") && s->iformat->flags & AVFMT_NOFILE) ||
379
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 7673 times.
7686 (s->flags & AVFMT_FLAG_CUSTOM_IO))
380 62 pb = NULL;
381
382
1/2
✓ Branch 0 taken 7735 times.
✗ Branch 1 not taken.
7735 if (s->iformat)
383
2/2
✓ Branch 1 taken 5057 times.
✓ Branch 2 taken 2678 times.
7735 if (ffifmt(s->iformat)->read_close)
384 5057 ffifmt(s->iformat)->read_close(s);
385
386 7735 ff_format_io_close(s, &pb);
387 7735 avformat_free_context(s);
388
389 7735 *ps = NULL;
390 }
391
392 1097830 static void force_codec_ids(AVFormatContext *s, AVStream *st)
393 {
394
5/5
✓ Branch 0 taken 747380 times.
✓ Branch 1 taken 346936 times.
✓ Branch 2 taken 3320 times.
✓ Branch 3 taken 192 times.
✓ Branch 4 taken 2 times.
1097830 switch (st->codecpar->codec_type) {
395 747380 case AVMEDIA_TYPE_VIDEO:
396
2/2
✓ Branch 0 taken 118736 times.
✓ Branch 1 taken 628644 times.
747380 if (s->video_codec_id)
397 118736 st->codecpar->codec_id = s->video_codec_id;
398 747380 break;
399 346936 case AVMEDIA_TYPE_AUDIO:
400
2/2
✓ Branch 0 taken 2558 times.
✓ Branch 1 taken 344378 times.
346936 if (s->audio_codec_id)
401 2558 st->codecpar->codec_id = s->audio_codec_id;
402 346936 break;
403 3320 case AVMEDIA_TYPE_SUBTITLE:
404
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3320 times.
3320 if (s->subtitle_codec_id)
405 st->codecpar->codec_id = s->subtitle_codec_id;
406 3320 break;
407 192 case AVMEDIA_TYPE_DATA:
408
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 192 times.
192 if (s->data_codec_id)
409 st->codecpar->codec_id = s->data_codec_id;
410 192 break;
411 }
412 1097830 }
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 1094793 static int update_wrap_reference(AVFormatContext *s, AVStream *st, int stream_index, AVPacket *pkt)
468 {
469 1094793 FFStream *const sti = ffstream(st);
470 1094793 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 891860 times.
✓ Branch 1 taken 202933 times.
1094793 if (ref == AV_NOPTS_VALUE)
476 891860 ref = pkt->pts;
477
7/8
✓ Branch 0 taken 1026501 times.
✓ Branch 1 taken 68292 times.
✓ Branch 2 taken 48598 times.
✓ Branch 3 taken 977903 times.
✓ Branch 4 taken 307 times.
✓ Branch 5 taken 48291 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 307 times.
1094793 if (sti->pts_wrap_reference != AV_NOPTS_VALUE || st->pts_wrap_bits >= 63 || ref == AV_NOPTS_VALUE || !s->correct_ts_overflow)
478 1094486 return 0;
479 307 ref &= (1LL << st->pts_wrap_bits)-1;
480
481 // reference time stamp should be 60 s before first time stamp
482 307 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 615 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 306 times.
308 AV_PTS_WRAP_ADD_OFFSET : AV_PTS_WRAP_SUB_OFFSET;
487
488 307 first_program = av_find_program_from_stream(s, NULL, stream_index);
489
490
2/2
✓ Branch 0 taken 200 times.
✓ Branch 1 taken 107 times.
307 if (!first_program) {
491 200 int default_stream_index = av_find_default_stream_index(s);
492 200 FFStream *const default_sti = ffstream(s->streams[default_stream_index]);
493
2/2
✓ Branch 0 taken 189 times.
✓ Branch 1 taken 11 times.
200 if (default_sti->pts_wrap_reference == AV_NOPTS_VALUE) {
494
2/2
✓ Branch 0 taken 355 times.
✓ Branch 1 taken 189 times.
544 for (unsigned i = 0; i < s->nb_streams; i++) {
495 355 FFStream *const sti = ffstream(s->streams[i]);
496
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 355 times.
355 if (av_find_program_from_stream(s, NULL, i))
497 continue;
498 355 sti->pts_wrap_reference = pts_wrap_reference;
499 355 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 107 AVProgram *program = first_program;
507
2/2
✓ Branch 0 taken 107 times.
✓ Branch 1 taken 67 times.
174 while (program) {
508
2/2
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 67 times.
107 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 67 program = av_find_program_from_stream(s, program, stream_index);
514 }
515
516 // update every program with differing pts_wrap_reference
517 107 program = first_program;
518
2/2
✓ Branch 0 taken 107 times.
✓ Branch 1 taken 107 times.
214 while (program) {
519
2/2
✓ Branch 0 taken 67 times.
✓ Branch 1 taken 40 times.
107 if (program->pts_wrap_reference != pts_wrap_reference) {
520
2/2
✓ Branch 0 taken 109 times.
✓ Branch 1 taken 67 times.
176 for (unsigned i = 0; i < program->nb_stream_indexes; i++) {
521 109 FFStream *const sti = ffstream(s->streams[program->stream_index[i]]);
522 109 sti->pts_wrap_reference = pts_wrap_reference;
523 109 sti->pts_wrap_behavior = pts_wrap_behavior;
524 }
525
526 67 program->pts_wrap_reference = pts_wrap_reference;
527 67 program->pts_wrap_behavior = pts_wrap_behavior;
528 }
529 107 program = av_find_program_from_stream(s, program, stream_index);
530 }
531 }
532 307 return 1;
533 }
534
535 1094793 static void update_timestamps(AVFormatContext *s, AVStream *st, AVPacket *pkt)
536 {
537 1094793 FFStream *const sti = ffstream(st);
538
539
4/4
✓ Branch 1 taken 307 times.
✓ Branch 2 taken 1094486 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 306 times.
1094793 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 1094793 pkt->dts = wrap_timestamp(st, pkt->dts);
550 1094793 pkt->pts = wrap_timestamp(st, pkt->pts);
551
552 1094793 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 1094793 times.
1094793 if (s->use_wallclock_as_timestamps)
556 pkt->dts = pkt->pts = av_rescale_q(av_gettime(), AV_TIME_BASE_Q, st->time_base);
557 1094793 }
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 1094793 static int handle_new_packet(AVFormatContext *s, AVPacket *pkt, int allow_passthrough)
568 {
569 1094793 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 1094793 times.
1094793 av_assert0(pkt->stream_index < (unsigned)s->nb_streams &&
575 "Invalid stream index.\n");
576
577
2/2
✓ Branch 0 taken 203 times.
✓ Branch 1 taken 1094590 times.
1094793 if (pkt->flags & AV_PKT_FLAG_CORRUPT) {
578 406 av_log(s, AV_LOG_WARNING,
579 "Packet corrupt (stream = %d, dts = %s)%s.\n",
580 203 pkt->stream_index, av_ts2str(pkt->dts),
581
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 203 times.
203 s->flags & AVFMT_FLAG_DISCARD_CORRUPT ? ", dropping it" : "");
582
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 203 times.
203 if (s->flags & AVFMT_FLAG_DISCARD_CORRUPT) {
583 av_packet_unref(pkt);
584 return 1;
585 }
586 }
587
588 1094793 st = s->streams[pkt->stream_index];
589 1094793 sti = ffstream(st);
590
591 1094793 update_timestamps(s, st, pkt);
592
593
6/6
✓ Branch 0 taken 1078822 times.
✓ Branch 1 taken 15971 times.
✓ Branch 2 taken 1072697 times.
✓ Branch 3 taken 6125 times.
✓ Branch 4 taken 1072655 times.
✓ Branch 5 taken 42 times.
1094793 if (sti->request_probe <= 0 && allow_passthrough && !fci->raw_packet_buffer.head)
594 1072655 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 1101386 int ff_read_packet(AVFormatContext *s, AVPacket *pkt)
620 {
621 1101386 FormatContextInternal *const fci = ff_fc_internal(s);
622 int err;
623
624 #if FF_API_INIT_PACKET
625 FF_DISABLE_DEPRECATION_WARNINGS
626 1101386 pkt->data = NULL;
627 1101386 pkt->size = 0;
628 1101386 av_init_packet(pkt);
629 FF_ENABLE_DEPRECATION_WARNINGS
630 #else
631 av_packet_unref(pkt);
632 #endif
633
634 22427 for (;;) {
635 1123813 PacketListEntry *pktl = fci->raw_packet_buffer.head;
636
637
2/2
✓ Branch 0 taken 37692 times.
✓ Branch 1 taken 1086121 times.
1123813 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 1101628 err = ffifmt(s->iformat)->read_packet(s, pkt);
650
2/2
✓ Branch 0 taken 12960 times.
✓ Branch 1 taken 1088668 times.
1101628 if (err < 0) {
651 12960 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 6558 times.
12960 if (err == FFERROR_REDO)
657 6402 continue;
658
3/4
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 6546 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 12 times.
6558 if (!pktl || err == AVERROR(EAGAIN))
659 6546 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 1088668 err = av_packet_make_refcounted(pkt);
672
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1088668 times.
1088668 if (err < 0) {
673 av_packet_unref(pkt);
674 return err;
675 }
676
677 1088668 err = handle_new_packet(s, pkt, 1);
678
2/2
✓ Branch 0 taken 1072655 times.
✓ Branch 1 taken 16013 times.
1088668 if (err <= 0) /* Error or passthrough */
679 1072655 return err;
680 }
681 }
682
683 /**
684 * Return the frame duration in seconds. Return 0 if not available.
685 */
686 326862 static void compute_frame_duration(AVFormatContext *s, int *pnum, int *pden,
687 AVStream *st, AVCodecParserContext *pc,
688 AVPacket *pkt)
689 {
690 326862 FFStream *const sti = ffstream(st);
691 326862 AVRational codec_framerate = sti->avctx->framerate;
692 int frame_size, sample_rate;
693
694 326862 *pnum = 0;
695 326862 *pden = 0;
696
3/3
✓ Branch 0 taken 224677 times.
✓ Branch 1 taken 100600 times.
✓ Branch 2 taken 1585 times.
326862 switch (st->codecpar->codec_type) {
697 224677 case AVMEDIA_TYPE_VIDEO:
698
6/6
✓ Branch 0 taken 194690 times.
✓ Branch 1 taken 29987 times.
✓ Branch 2 taken 42501 times.
✓ Branch 3 taken 152189 times.
✓ Branch 4 taken 30858 times.
✓ Branch 5 taken 11643 times.
224677 if (st->r_frame_rate.num && (!pc || !codec_framerate.num)) {
699 183047 *pnum = st->r_frame_rate.den;
700 183047 *pden = st->r_frame_rate.num;
701
2/2
✓ Branch 0 taken 23832 times.
✓ Branch 1 taken 17798 times.
41630 } else if ((s->iformat->flags & AVFMT_NOTIMESTAMPS) &&
702
2/2
✓ Branch 0 taken 14868 times.
✓ Branch 1 taken 8964 times.
23832 !codec_framerate.num &&
703
3/4
✓ Branch 0 taken 14866 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 14866 times.
✗ Branch 3 not taken.
14868 st->avg_frame_rate.num && st->avg_frame_rate.den) {
704 14866 *pnum = st->avg_frame_rate.den;
705 14866 *pden = st->avg_frame_rate.num;
706
2/2
✓ Branch 0 taken 9162 times.
✓ Branch 1 taken 17602 times.
26764 } else if (st->time_base.num * 1000LL > st->time_base.den) {
707 9162 *pnum = st->time_base.num;
708 9162 *pden = st->time_base.den;
709
2/2
✓ Branch 0 taken 17577 times.
✓ Branch 1 taken 25 times.
17602 } else if (codec_framerate.den * 1000LL > codec_framerate.num) {
710 52731 int ticks_per_frame = (sti->codec_desc &&
711
3/4
✓ Branch 0 taken 17577 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12309 times.
✓ Branch 3 taken 5268 times.
17577 (sti->codec_desc->props & AV_CODEC_PROP_FIELDS)) ? 2 : 1;
712 17577 av_reduce(pnum, pden,
713 17577 codec_framerate.den,
714 17577 codec_framerate.num * (int64_t)ticks_per_frame,
715 INT_MAX);
716
717
4/4
✓ Branch 0 taken 15751 times.
✓ Branch 1 taken 1826 times.
✓ Branch 2 taken 11680 times.
✓ Branch 3 taken 4071 times.
17577 if (pc && pc->repeat_pict) {
718 11680 av_reduce(pnum, pden,
719 11680 (*pnum) * (1LL + pc->repeat_pict),
720 11680 (*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 17577 times.
✗ Branch 1 not taken.
17577 if (sti->codec_desc &&
727
3/4
✓ Branch 0 taken 12309 times.
✓ Branch 1 taken 5268 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 12309 times.
17577 (sti->codec_desc->props & AV_CODEC_PROP_FIELDS) && !pc)
728 *pnum = *pden = 0;
729 }
730 224677 break;
731 100600 case AVMEDIA_TYPE_AUDIO:
732
2/2
✓ Branch 0 taken 45278 times.
✓ Branch 1 taken 55322 times.
100600 if (sti->avctx_inited) {
733 45278 frame_size = av_get_audio_frame_duration(sti->avctx, pkt->size);
734 45278 sample_rate = sti->avctx->sample_rate;
735 } else {
736 55322 frame_size = av_get_audio_frame_duration2(st->codecpar, pkt->size);
737 55322 sample_rate = st->codecpar->sample_rate;
738 }
739
4/4
✓ Branch 0 taken 95204 times.
✓ Branch 1 taken 5396 times.
✓ Branch 2 taken 95199 times.
✓ Branch 3 taken 5 times.
100600 if (frame_size <= 0 || sample_rate <= 0)
740 break;
741 95199 *pnum = frame_size;
742 95199 *pden = sample_rate;
743 95199 break;
744 1585 default:
745 1585 break;
746 }
747 326862 }
748
749 729845 static int has_decode_delay_been_guessed(AVStream *st)
750 {
751 729845 FFStream *const sti = ffstream(st);
752
2/2
✓ Branch 0 taken 712865 times.
✓ Branch 1 taken 16980 times.
729845 if (st->codecpar->codec_id != AV_CODEC_ID_H264) return 1;
753
2/2
✓ Branch 0 taken 6069 times.
✓ Branch 1 taken 10911 times.
16980 if (!sti->info) // if we have left find_stream_info then nb_decoded_frames won't increase anymore for stream copy
754 6069 return 1;
755 #if CONFIG_H264_DECODER
756
2/2
✓ Branch 0 taken 6687 times.
✓ Branch 1 taken 4224 times.
10911 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 9307 times.
✓ Branch 1 taken 188 times.
9495 if (sti->avctx->has_b_frames < 3)
761 9307 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 36739 static PacketListEntry *get_next_pkt(AVFormatContext *s, AVStream *st,
769 PacketListEntry *pktl)
770 {
771 36739 FormatContextInternal *const fci = ff_fc_internal(s);
772 36739 FFFormatContext *const si = &fci->fc;
773
2/2
✓ Branch 0 taken 34788 times.
✓ Branch 1 taken 1951 times.
36739 if (pktl->next)
774 34788 return pktl->next;
775
2/2
✓ Branch 0 taken 1921 times.
✓ Branch 1 taken 30 times.
1951 if (pktl == si->packet_buffer.tail)
776 1921 return fci->parse_queue.head;
777 30 return NULL;
778 }
779
780 527923 static int64_t select_from_pts_buffer(AVStream *st, int64_t *pts_buffer, int64_t dts)
781 {
782 527923 FFStream *const sti = ffstream(st);
783 1576509 int onein_oneout = st->codecpar->codec_id != AV_CODEC_ID_H264 &&
784
4/4
✓ Branch 0 taken 520663 times.
✓ Branch 1 taken 7260 times.
✓ Branch 2 taken 519687 times.
✓ Branch 3 taken 976 times.
1047610 st->codecpar->codec_id != AV_CODEC_ID_HEVC &&
785
2/2
✓ Branch 0 taken 519668 times.
✓ Branch 1 taken 19 times.
519687 st->codecpar->codec_id != AV_CODEC_ID_VVC;
786
787
2/2
✓ Branch 0 taken 8255 times.
✓ Branch 1 taken 519668 times.
527923 if (!onein_oneout) {
788 8255 int delay = sti->avctx->has_b_frames;
789
790
2/2
✓ Branch 0 taken 1221 times.
✓ Branch 1 taken 7034 times.
8255 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 11922 times.
✓ Branch 1 taken 7034 times.
18956 for (int i = 0; i < delay; i++) {
803
2/2
✓ Branch 0 taken 11119 times.
✓ Branch 1 taken 803 times.
11922 if (pts_buffer[i] != AV_NOPTS_VALUE) {
804 11119 int64_t diff = FFABS(pts_buffer[i] - dts)
805 11119 + (uint64_t)sti->pts_reorder_error[i];
806 11119 diff = FFMAX(diff, sti->pts_reorder_error[i]);
807 11119 sti->pts_reorder_error[i] = diff;
808 11119 sti->pts_reorder_error_count[i]++;
809
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 11116 times.
11119 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 526527 times.
527923 if (dts == AV_NOPTS_VALUE)
819 1396 dts = pts_buffer[0];
820
821 527923 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 6258 static void update_dts_from_pts(AVFormatContext *s, int stream_index,
829 PacketListEntry *pkt_buffer)
830 {
831 6258 AVStream *const st = s->streams[stream_index];
832 6258 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 106386 times.
✓ Branch 1 taken 6258 times.
112644 for (int i = 0; i < MAX_REORDER_DELAY + 1; i++)
837 106386 pts_buffer[i] = AV_NOPTS_VALUE;
838
839
2/2
✓ Branch 1 taken 5522 times.
✓ Branch 2 taken 6258 times.
11780 for (; pkt_buffer; pkt_buffer = get_next_pkt(s, st, pkt_buffer)) {
840
2/2
✓ Branch 0 taken 4864 times.
✓ Branch 1 taken 658 times.
5522 if (pkt_buffer->pkt.stream_index != stream_index)
841 4864 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 6258 }
852
853 569511 static void update_initial_timestamps(AVFormatContext *s, int stream_index,
854 int64_t dts, int64_t pts, AVPacket *pkt)
855 {
856 569511 FormatContextInternal *const fci = ff_fc_internal(s);
857 569511 FFFormatContext *const si = &fci->fc;
858 569511 AVStream *const st = s->streams[stream_index];
859 569511 FFStream *const sti = ffstream(st);
860
2/2
✓ Branch 0 taken 185618 times.
✓ Branch 1 taken 383893 times.
569511 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 186241 times.
✓ Branch 1 taken 383270 times.
✓ Branch 2 taken 6323 times.
✓ Branch 3 taken 179918 times.
569511 if (sti->first_dts != AV_NOPTS_VALUE ||
865 6323 dts == AV_NOPTS_VALUE ||
866
1/2
✓ Branch 0 taken 6323 times.
✗ Branch 1 not taken.
6323 sti->cur_dts == AV_NOPTS_VALUE ||
867
1/2
✓ Branch 0 taken 6323 times.
✗ Branch 1 not taken.
6323 sti->cur_dts < INT_MIN + RELATIVE_TS_BASE ||
868
2/4
✓ Branch 0 taken 6323 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6323 times.
12646 dts < INT_MIN + (sti->cur_dts - RELATIVE_TS_BASE) ||
869 6323 is_relative(dts))
870 563188 return;
871
872 6323 sti->first_dts = dts - (sti->cur_dts - RELATIVE_TS_BASE);
873 6323 sti->cur_dts = dts;
874 6323 shift = (uint64_t)sti->first_dts - RELATIVE_TS_BASE;
875
876
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6323 times.
6323 if (is_relative(pts))
877 pts += shift;
878
879
2/2
✓ Branch 1 taken 5263 times.
✓ Branch 2 taken 6323 times.
11586 for (PacketListEntry *pktl_it = pktl; pktl_it; pktl_it = get_next_pkt(s, st, pktl_it)) {
880
2/2
✓ Branch 0 taken 4793 times.
✓ Branch 1 taken 470 times.
5263 if (pktl_it->pkt.stream_index != stream_index)
881 4793 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 6240 times.
✓ Branch 2 taken 83 times.
6323 if (has_decode_delay_been_guessed(st))
896 6240 update_dts_from_pts(s, stream_index, pktl);
897
898
2/2
✓ Branch 0 taken 1731 times.
✓ Branch 1 taken 4592 times.
6323 if (st->start_time == AV_NOPTS_VALUE) {
899
3/4
✓ Branch 0 taken 1249 times.
✓ Branch 1 taken 482 times.
✓ Branch 2 taken 1249 times.
✗ Branch 3 not taken.
1731 if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO || !(pkt->flags & AV_PKT_FLAG_DISCARD)) {
900 1731 st->start_time = pts;
901 }
902
4/4
✓ Branch 0 taken 482 times.
✓ Branch 1 taken 1249 times.
✓ Branch 2 taken 397 times.
✓ Branch 3 taken 85 times.
1731 if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && st->codecpar->sample_rate)
903 397 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 229436 static void update_initial_durations(AVFormatContext *s, AVStream *st,
908 int stream_index, int64_t duration)
909 {
910 229436 FormatContextInternal *const fci = ff_fc_internal(s);
911 229436 FFFormatContext *const si = &fci->fc;
912 229436 FFStream *const sti = ffstream(st);
913
2/2
✓ Branch 0 taken 182310 times.
✓ Branch 1 taken 47126 times.
229436 PacketListEntry *pktl = si->packet_buffer.head ? si->packet_buffer.head : fci->parse_queue.head;
914 229436 int64_t cur_dts = RELATIVE_TS_BASE;
915
916
2/2
✓ Branch 0 taken 124344 times.
✓ Branch 1 taken 105092 times.
229436 if (sti->first_dts != AV_NOPTS_VALUE) {
917
2/2
✓ Branch 0 taken 120618 times.
✓ Branch 1 taken 3726 times.
124344 if (sti->update_initial_durations_done)
918 120618 return;
919 3726 sti->update_initial_durations_done = 1;
920 3726 cur_dts = sti->first_dts;
921
1/2
✓ Branch 1 taken 5542 times.
✗ Branch 2 not taken.
5542 for (; pktl; pktl = get_next_pkt(s, st, pktl)) {
922
2/2
✓ Branch 0 taken 3741 times.
✓ Branch 1 taken 1801 times.
5542 if (pktl->pkt.stream_index == stream_index) {
923
2/2
✓ Branch 0 taken 3629 times.
✓ Branch 1 taken 112 times.
3741 if (pktl->pkt.pts != pktl->pkt.dts ||
924
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 3614 times.
3629 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 3726 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
✓ Branch 3 taken 3693 times.
3726 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 3693 times.
3693 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 3672 times.
✓ Branch 1 taken 21 times.
3693 pktl = si->packet_buffer.head ? si->packet_buffer.head : fci->parse_queue.head;
940 3693 sti->first_dts = cur_dts;
941
2/2
✓ Branch 0 taken 80394 times.
✓ Branch 1 taken 24698 times.
105092 } else if (sti->cur_dts != RELATIVE_TS_BASE)
942 80394 return;
943
944
2/2
✓ Branch 1 taken 51874 times.
✓ Branch 2 taken 655 times.
52529 for (; pktl; pktl = get_next_pkt(s, st, pktl)) {
945
2/2
✓ Branch 0 taken 23983 times.
✓ Branch 1 taken 27891 times.
51874 if (pktl->pkt.stream_index != stream_index)
946 23983 continue;
947
2/2
✓ Branch 0 taken 568 times.
✓ Branch 1 taken 27323 times.
27891 if ((pktl->pkt.pts == pktl->pkt.dts ||
948
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 554 times.
568 pktl->pkt.pts == AV_NOPTS_VALUE) &&
949
2/2
✓ Branch 0 taken 3761 times.
✓ Branch 1 taken 23576 times.
27337 (pktl->pkt.dts == AV_NOPTS_VALUE ||
950
2/2
✓ Branch 0 taken 178 times.
✓ Branch 1 taken 3583 times.
3761 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 27006 times.
27161 !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 655 times.
✓ Branch 1 taken 27736 times.
28391 if (!pktl)
964 655 sti->cur_dts = cur_dts;
965 }
966
967 575461 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 575461 FormatContextInternal *const fci = ff_fc_internal(s);
972 575461 FFFormatContext *const si = &fci->fc;
973 575461 FFStream *const sti = ffstream(st);
974 int num, den, presentation_delayed, delay;
975 int64_t offset;
976 AVRational duration;
977 1692443 int onein_oneout = st->codecpar->codec_id != AV_CODEC_ID_H264 &&
978
4/4
✓ Branch 0 taken 541521 times.
✓ Branch 1 taken 33940 times.
✓ Branch 2 taken 528393 times.
✓ Branch 3 taken 13128 times.
1103854 st->codecpar->codec_id != AV_CODEC_ID_HEVC &&
979
2/2
✓ Branch 0 taken 525244 times.
✓ Branch 1 taken 3149 times.
528393 st->codecpar->codec_id != AV_CODEC_ID_VVC;
980
981
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 575461 times.
575461 if (s->flags & AVFMT_FLAG_NOFILLIN)
982 return;
983
984
4/4
✓ Branch 0 taken 248723 times.
✓ Branch 1 taken 326738 times.
✓ Branch 2 taken 74192 times.
✓ Branch 3 taken 174531 times.
575461 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && pkt->dts != AV_NOPTS_VALUE) {
985
4/4
✓ Branch 0 taken 43730 times.
✓ Branch 1 taken 30462 times.
✓ Branch 2 taken 42547 times.
✓ Branch 3 taken 1183 times.
74192 if (pkt->dts == pkt->pts && sti->last_dts_for_order_check != AV_NOPTS_VALUE) {
986
2/2
✓ Branch 0 taken 42538 times.
✓ Branch 1 taken 9 times.
42547 if (sti->last_dts_for_order_check <= pkt->dts) {
987 42538 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 42518 times.
42547 if (sti->dts_ordered + sti->dts_misordered > 250) {
996 29 sti->dts_ordered >>= 1;
997 29 sti->dts_misordered >>= 1;
998 }
999 }
1000
1001 74192 sti->last_dts_for_order_check = pkt->dts;
1002
4/4
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 74155 times.
✓ Branch 2 taken 20 times.
✓ Branch 3 taken 17 times.
74192 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 575461 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
575461 if ((s->flags & AVFMT_FLAG_IGNDTS) && pkt->pts != AV_NOPTS_VALUE)
1007 pkt->dts = AV_NOPTS_VALUE;
1008
1009
4/4
✓ Branch 0 taken 187372 times.
✓ Branch 1 taken 388089 times.
✓ Branch 2 taken 28023 times.
✓ Branch 3 taken 159349 times.
575461 if (pc && pc->pict_type == AV_PICTURE_TYPE_B
1010
2/2
✓ Branch 0 taken 81 times.
✓ Branch 1 taken 27942 times.
28023 && !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 575461 delay = sti->avctx->has_b_frames;
1016 575461 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 46974 times.
✓ Branch 1 taken 528487 times.
✓ Branch 2 taken 42942 times.
✓ Branch 3 taken 4032 times.
575461 if (delay &&
1021
2/2
✓ Branch 0 taken 14919 times.
✓ Branch 1 taken 28023 times.
42942 pc && pc->pict_type != AV_PICTURE_TYPE_B)
1022 14919 presentation_delayed = 1;
1023
1024
4/4
✓ Branch 0 taken 345290 times.
✓ Branch 1 taken 230171 times.
✓ Branch 2 taken 168539 times.
✓ Branch 3 taken 176751 times.
575461 if (pkt->pts != AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE &&
1025
3/4
✓ Branch 0 taken 27666 times.
✓ Branch 1 taken 140873 times.
✓ Branch 2 taken 27666 times.
✗ Branch 3 not taken.
168539 st->pts_wrap_bits < 63 && pkt->dts > INT64_MIN + (1LL << st->pts_wrap_bits) &&
1026
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27666 times.
27666 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 30411 times.
✓ Branch 1 taken 545050 times.
✓ Branch 2 taken 25194 times.
✓ Branch 3 taken 5217 times.
575461 if (delay == 1 && pkt->dts == pkt->pts &&
1038
4/4
✓ Branch 0 taken 6287 times.
✓ Branch 1 taken 18907 times.
✓ Branch 2 taken 628 times.
✓ Branch 3 taken 5659 times.
25194 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 575461 duration = av_mul_q((AVRational) {pkt->duration, 1}, st->time_base);
1046
2/2
✓ Branch 0 taken 324013 times.
✓ Branch 1 taken 251448 times.
575461 if (pkt->duration <= 0) {
1047 324013 compute_frame_duration(s, &num, &den, st, pc, pkt);
1048
3/4
✓ Branch 0 taken 314391 times.
✓ Branch 1 taken 9622 times.
✓ Branch 2 taken 314391 times.
✗ Branch 3 not taken.
324013 if (den && num) {
1049 314391 duration = (AVRational) {num, den};
1050 314391 pkt->duration = av_rescale_rnd(1,
1051 314391 num * (int64_t) st->time_base.den,
1052 314391 den * (int64_t) st->time_base.num,
1053 AV_ROUND_DOWN);
1054 }
1055 }
1056
1057
6/6
✓ Branch 0 taken 563017 times.
✓ Branch 1 taken 12444 times.
✓ Branch 2 taken 380707 times.
✓ Branch 3 taken 182310 times.
✓ Branch 4 taken 47126 times.
✓ Branch 5 taken 333581 times.
575461 if (pkt->duration > 0 && (si->packet_buffer.head || fci->parse_queue.head))
1058 229436 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 187372 times.
✓ Branch 1 taken 388089 times.
✓ Branch 2 taken 960 times.
✓ Branch 3 taken 186412 times.
✓ Branch 4 taken 960 times.
✗ Branch 5 not taken.
575461 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 199030 times.
✓ Branch 1 taken 376431 times.
575461 if (pkt->dts != AV_NOPTS_VALUE &&
1073
2/2
✓ Branch 0 taken 168517 times.
✓ Branch 1 taken 30513 times.
199030 pkt->pts != AV_NOPTS_VALUE &&
1074
2/2
✓ Branch 0 taken 5240 times.
✓ Branch 1 taken 163277 times.
168517 pkt->pts > pkt->dts)
1075 5240 presentation_delayed = 1;
1076
1077
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 575461 times.
575461 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 46974 times.
✓ Branch 1 taken 528487 times.
✓ Branch 2 taken 30411 times.
✓ Branch 3 taken 16563 times.
✓ Branch 4 taken 26956 times.
✓ Branch 5 taken 3455 times.
✓ Branch 6 taken 521802 times.
✓ Branch 7 taken 33641 times.
575461 if ((delay == 0 || (delay == 1 && pc)) &&
1086 onein_oneout) {
1087
2/2
✓ Branch 0 taken 5579 times.
✓ Branch 1 taken 516223 times.
521802 if (presentation_delayed) {
1088 /* DTS = decompression timestamp */
1089 /* PTS = presentation timestamp */
1090
2/2
✓ Branch 0 taken 2844 times.
✓ Branch 1 taken 2735 times.
5579 if (pkt->dts == AV_NOPTS_VALUE)
1091 2844 pkt->dts = sti->last_IP_pts;
1092 5579 update_initial_timestamps(s, pkt->stream_index, pkt->dts, pkt->pts, pkt);
1093
2/2
✓ Branch 0 taken 2507 times.
✓ Branch 1 taken 3072 times.
5579 if (pkt->dts == AV_NOPTS_VALUE)
1094 2507 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 238 times.
✓ Branch 1 taken 5341 times.
✓ Branch 2 taken 238 times.
✗ Branch 3 not taken.
5579 if (sti->last_IP_duration == 0 && (uint64_t)pkt->duration <= INT32_MAX)
1099 238 sti->last_IP_duration = pkt->duration;
1100
1/2
✓ Branch 0 taken 5579 times.
✗ Branch 1 not taken.
5579 if (pkt->dts != AV_NOPTS_VALUE)
1101 5579 sti->cur_dts = av_sat_add64(pkt->dts, sti->last_IP_duration);
1102
1/2
✓ Branch 0 taken 5579 times.
✗ Branch 1 not taken.
5579 if (pkt->dts != AV_NOPTS_VALUE &&
1103
2/2
✓ Branch 0 taken 3275 times.
✓ Branch 1 taken 2304 times.
5579 pkt->pts == AV_NOPTS_VALUE &&
1104
2/2
✓ Branch 0 taken 3273 times.
✓ Branch 1 taken 2 times.
3275 sti->last_IP_duration > 0 &&
1105
4/4
✓ Branch 0 taken 785 times.
✓ Branch 1 taken 2488 times.
✓ Branch 2 taken 779 times.
✓ Branch 3 taken 6 times.
3273 ((uint64_t)sti->cur_dts - (uint64_t)next_dts + 1) <= 2 &&
1106
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 768 times.
779 next_dts != next_pts &&
1107 next_pts != AV_NOPTS_VALUE)
1108 11 pkt->pts = next_dts;
1109
1110
1/2
✓ Branch 0 taken 5579 times.
✗ Branch 1 not taken.
5579 if ((uint64_t)pkt->duration <= INT32_MAX)
1111 5579 sti->last_IP_duration = pkt->duration;
1112 5579 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 185200 times.
✓ Branch 1 taken 331023 times.
516223 } else if (pkt->pts != AV_NOPTS_VALUE ||
1116
2/2
✓ Branch 0 taken 155534 times.
✓ Branch 1 taken 29666 times.
185200 pkt->dts != AV_NOPTS_VALUE ||
1117
2/2
✓ Branch 0 taken 153026 times.
✓ Branch 1 taken 2508 times.
155534 pkt->duration > 0 ) {
1118
1119 /* presentation is not delayed : PTS and DTS are the same */
1120
2/2
✓ Branch 0 taken 182692 times.
✓ Branch 1 taken 331023 times.
513715 if (pkt->pts == AV_NOPTS_VALUE)
1121 182692 pkt->pts = pkt->dts;
1122 513715 update_initial_timestamps(s, pkt->stream_index, pkt->pts,
1123 pkt->pts, pkt);
1124
2/2
✓ Branch 0 taken 153026 times.
✓ Branch 1 taken 360689 times.
513715 if (pkt->pts == AV_NOPTS_VALUE)
1125 153026 pkt->pts = sti->cur_dts;
1126 513715 pkt->dts = pkt->pts;
1127
3/4
✓ Branch 0 taken 513715 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 513705 times.
✓ Branch 3 taken 10 times.
513715 if (pkt->pts != AV_NOPTS_VALUE && duration.num >= 0)
1128 513705 sti->cur_dts = av_add_stable(st->time_base, pkt->pts, duration, 1);
1129 }
1130 }
1131
1132
3/4
✓ Branch 0 taken 527993 times.
✓ Branch 1 taken 47468 times.
✓ Branch 2 taken 527993 times.
✗ Branch 3 not taken.
575461 if (pkt->pts != AV_NOPTS_VALUE && delay <= MAX_REORDER_DELAY) {
1133 527993 sti->pts_buffer[0] = pkt->pts;
1134
4/4
✓ Branch 0 taken 22344 times.
✓ Branch 1 taken 522539 times.
✓ Branch 2 taken 16890 times.
✓ Branch 3 taken 5454 times.
544883 for (int i = 0; i < delay && sti->pts_buffer[i] > sti->pts_buffer[i + 1]; i++)
1135 16890 FFSWAP(int64_t, sti->pts_buffer[i], sti->pts_buffer[i + 1]);
1136
1137
2/2
✓ Branch 1 taken 527362 times.
✓ Branch 2 taken 631 times.
527993 if (has_decode_delay_been_guessed(st))
1138 527362 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 50217 times.
✓ Branch 1 taken 525244 times.
575461 if (!onein_oneout)
1142 // This should happen on the first packet
1143 50217 update_initial_timestamps(s, pkt->stream_index, pkt->dts, pkt->pts, pkt);
1144
2/2
✓ Branch 0 taken 8033 times.
✓ Branch 1 taken 567428 times.
575461 if (pkt->dts > sti->cur_dts)
1145 8033 sti->cur_dts = pkt->dts;
1146
1147
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 575461 times.
575461 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 575312 times.
✓ Branch 1 taken 149 times.
✓ Branch 3 taken 377880 times.
✓ Branch 4 taken 197432 times.
575461 if (st->codecpar->codec_type == AVMEDIA_TYPE_DATA || ff_is_intra_only(st->codecpar->codec_id))
1153 378029 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 702114 static int parse_packet(AVFormatContext *s, AVPacket *pkt,
1163 int stream_index, int flush)
1164 {
1165 702114 FormatContextInternal *const fci = ff_fc_internal(s);
1166 702114 FFFormatContext *const si = &fci->fc;
1167 702114 AVPacket *out_pkt = si->parse_pkt;
1168 702114 AVStream *st = s->streams[stream_index];
1169 702114 FFStream *const sti = ffstream(st);
1170 702114 const uint8_t *data = pkt->data;
1171 702114 int size = pkt->size;
1172 702114 int ret = 0, got_output = flush;
1173
1174
5/6
✓ Branch 0 taken 2256 times.
✓ Branch 1 taken 699858 times.
✓ Branch 2 taken 164 times.
✓ Branch 3 taken 2092 times.
✓ Branch 4 taken 164 times.
✗ Branch 5 not taken.
702114 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 816354 times.
✓ Branch 1 taken 705376 times.
✓ Branch 2 taken 5354 times.
✓ Branch 3 taken 700022 times.
✓ Branch 4 taken 3262 times.
✓ Branch 5 taken 2092 times.
1521730 while (size > 0 || (flush && got_output)) {
1188 819616 int64_t next_pts = pkt->pts;
1189 819616 int64_t next_dts = pkt->dts;
1190 int len;
1191
1192 819616 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 819616 pkt->pts = pkt->dts = AV_NOPTS_VALUE;
1197 819616 pkt->pos = -1;
1198 /* increment read pointer */
1199 av_assert1(data || !len);
1200
2/2
✓ Branch 0 taken 806462 times.
✓ Branch 1 taken 13154 times.
819616 data = len ? data + len : data;
1201 819616 size -= len;
1202
1203 819616 got_output = !!out_pkt->size;
1204
1205
2/2
✓ Branch 0 taken 632408 times.
✓ Branch 1 taken 187208 times.
819616 if (!out_pkt->size)
1206 632408 continue;
1207
1208
4/4
✓ Branch 0 taken 186038 times.
✓ Branch 1 taken 1170 times.
✓ Branch 2 taken 69194 times.
✓ Branch 3 taken 116844 times.
187208 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 69194 out_pkt->buf = av_buffer_ref(pkt->buf);
1214
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 69194 times.
69194 if (!out_pkt->buf) {
1215 ret = AVERROR(ENOMEM);
1216 goto fail;
1217 }
1218 } else {
1219 118014 ret = av_packet_make_refcounted(out_pkt);
1220
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 118014 times.
118014 if (ret < 0)
1221 goto fail;
1222 }
1223
1224
2/2
✓ Branch 0 taken 9387 times.
✓ Branch 1 taken 177821 times.
187208 if (pkt->side_data) {
1225 9387 out_pkt->side_data = pkt->side_data;
1226 9387 out_pkt->side_data_elems = pkt->side_data_elems;
1227 9387 pkt->side_data = NULL;
1228 9387 pkt->side_data_elems = 0;
1229 }
1230
1231 /* set the duration */
1232
2/2
✓ Branch 0 taken 49401 times.
✓ Branch 1 taken 137807 times.
187208 out_pkt->duration = (sti->parser->flags & PARSER_FLAG_COMPLETE_FRAMES) ? pkt->duration : 0;
1233
2/2
✓ Branch 0 taken 108845 times.
✓ Branch 1 taken 78363 times.
187208 if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
1234
2/2
✓ Branch 0 taken 108739 times.
✓ Branch 1 taken 106 times.
108845 if (sti->avctx->sample_rate > 0) {
1235 108739 out_pkt->duration =
1236 108739 av_rescale_q_rnd(sti->parser->duration,
1237 108739 (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 76541 times.
78363 } 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 187208 out_pkt->stream_index = st->index;
1249 187208 out_pkt->pts = sti->parser->pts;
1250 187208 out_pkt->dts = sti->parser->dts;
1251 187208 out_pkt->pos = sti->parser->pos;
1252 187208 out_pkt->flags |= pkt->flags & (AV_PKT_FLAG_DISCARD | AV_PKT_FLAG_CORRUPT);
1253
1254
2/2
✓ Branch 0 taken 114758 times.
✓ Branch 1 taken 72450 times.
187208 if (sti->need_parsing == AVSTREAM_PARSE_FULL_RAW)
1255 114758 out_pkt->pos = sti->parser->frame_offset;
1256
1257
2/2
✓ Branch 0 taken 175410 times.
✓ Branch 1 taken 11798 times.
187208 if (sti->parser->key_frame == 1 ||
1258
2/2
✓ Branch 0 taken 93886 times.
✓ Branch 1 taken 81524 times.
175410 (sti->parser->key_frame == -1 &&
1259
2/2
✓ Branch 0 taken 81784 times.
✓ Branch 1 taken 12102 times.
93886 sti->parser->pict_type == AV_PICTURE_TYPE_I))
1260 93582 out_pkt->flags |= AV_PKT_FLAG_KEY;
1261
1262
6/6
✓ Branch 0 taken 93886 times.
✓ Branch 1 taken 93322 times.
✓ Branch 2 taken 317 times.
✓ Branch 3 taken 93569 times.
✓ Branch 4 taken 288 times.
✓ Branch 5 taken 29 times.
187208 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 187208 compute_pkt_fields(s, st, sti->parser, out_pkt, next_dts, next_pts);
1266
1267 187208 ret = avpriv_packet_list_put(&fci->parse_queue,
1268 out_pkt, NULL, 0);
1269
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 187208 times.
187208 if (ret < 0)
1270 goto fail;
1271 }
1272
1273 /* end of the stream => close and free the parser */
1274
2/2
✓ Branch 0 taken 700022 times.
✓ Branch 1 taken 2092 times.
702114 if (flush) {
1275 2092 av_parser_close(sti->parser);
1276 2092 sti->parser = NULL;
1277 }
1278
1279 700022 fail:
1280
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 702114 times.
702114 if (ret < 0)
1281 av_packet_unref(out_pkt);
1282 702114 av_packet_unref(pkt);
1283 702114 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 8450 static int codec_close(FFStream *sti)
1292 {
1293 8450 AVCodecContext *avctx_new = NULL;
1294 8450 AVCodecParameters *par_tmp = NULL;
1295 8450 const AVCodec *new_codec = NULL;
1296 int ret;
1297
1298 8450 new_codec =
1299 8450 (sti->avctx->codec_id != sti->pub.codecpar->codec_id) ?
1300
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 8440 times.
8450 avcodec_find_decoder(sti->pub.codecpar->codec_id) :
1301 8440 sti->avctx->codec;
1302
1303 8450 avctx_new = avcodec_alloc_context3(new_codec);
1304
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8450 times.
8450 if (!avctx_new) {
1305 ret = AVERROR(ENOMEM);
1306 goto fail;
1307 }
1308
1309 8450 par_tmp = avcodec_parameters_alloc();
1310
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8450 times.
8450 if (!par_tmp) {
1311 ret = AVERROR(ENOMEM);
1312 goto fail;
1313 }
1314
1315 8450 ret = avcodec_parameters_from_context(par_tmp, sti->avctx);
1316
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8450 times.
8450 if (ret < 0)
1317 goto fail;
1318
1319 8450 ret = avcodec_parameters_to_context(avctx_new, par_tmp);
1320
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8450 times.
8450 if (ret < 0)
1321 goto fail;
1322
1323 8450 avctx_new->pkt_timebase = sti->avctx->pkt_timebase;
1324
1325 8450 avcodec_free_context(&sti->avctx);
1326 8450 sti->avctx = avctx_new;
1327
1328 8450 avctx_new = NULL;
1329 8450 ret = 0;
1330
1331 8450 fail:
1332 8450 avcodec_free_context(&avctx_new);
1333 8450 avcodec_parameters_free(&par_tmp);
1334
1335 8450 return ret;
1336 }
1337
1338 static int extract_extradata(FFFormatContext *si, AVStream *st, const AVPacket *pkt);
1339
1340 579943 static int read_frame_internal(AVFormatContext *s, AVPacket *pkt)
1341 {
1342 579943 FormatContextInternal *const fci = ff_fc_internal(s);
1343 579943 FFFormatContext *const si = &fci->fc;
1344 579943 int ret, got_packet = 0;
1345 579943 AVDictionary *metadata = NULL;
1346
1347
4/4
✓ Branch 0 taken 1280096 times.
✓ Branch 1 taken 388089 times.
✓ Branch 2 taken 1094718 times.
✓ Branch 3 taken 185378 times.
1668185 while (!got_packet && !fci->parse_queue.head) {
1348 AVStream *st;
1349 FFStream *sti;
1350
1351 /* read next packet */
1352 1094718 ret = ff_read_packet(s, pkt);
1353
2/2
✓ Branch 0 taken 6476 times.
✓ Branch 1 taken 1088242 times.
1094718 if (ret < 0) {
1354
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6475 times.
6476 if (ret == AVERROR(EAGAIN))
1355 1 return ret;
1356 /* flush the parsers */
1357
2/2
✓ Branch 0 taken 7539 times.
✓ Branch 1 taken 6475 times.
14014 for (unsigned i = 0; i < s->nb_streams; i++) {
1358 7539 AVStream *const st = s->streams[i];
1359 7539 FFStream *const sti = ffstream(st);
1360
4/4
✓ Branch 0 taken 2770 times.
✓ Branch 1 taken 4769 times.
✓ Branch 2 taken 2092 times.
✓ Branch 3 taken 678 times.
7539 if (sti->parser && sti->need_parsing)
1361 2092 parse_packet(s, pkt, st->index, 1);
1362 }
1363 /* all remaining packets are now in parse_queue =>
1364 * really terminate parsing */
1365 6475 break;
1366 }
1367 1088242 ret = 0;
1368 1088242 st = s->streams[pkt->stream_index];
1369 1088242 sti = ffstream(st);
1370
1371 1088242 st->event_flags |= AVSTREAM_EVENT_FLAG_NEW_PACKETS;
1372
1373 /* update context if required */
1374
2/2
✓ Branch 0 taken 203 times.
✓ Branch 1 taken 1088039 times.
1088242 if (sti->need_context_update) {
1375
2/2
✓ Branch 1 taken 15 times.
✓ Branch 2 taken 188 times.
203 if (avcodec_is_open(sti->avctx)) {
1376 15 av_log(s, AV_LOG_DEBUG, "Demuxer context update while decoder is open, closing and trying to re-open\n");
1377 15 ret = codec_close(sti);
1378 15 sti->info->found_decoder = 0;
1379
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 if (ret < 0)
1380 return ret;
1381 }
1382
1383 /* close parser, because it depends on the codec */
1384
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) {
1385 10 av_parser_close(sti->parser);
1386 10 sti->parser = NULL;
1387 }
1388
1389 203 ret = avcodec_parameters_to_context(sti->avctx, st->codecpar);
1390
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 203 times.
203 if (ret < 0) {
1391 av_packet_unref(pkt);
1392 return ret;
1393 }
1394
1395
2/2
✓ Branch 0 taken 154 times.
✓ Branch 1 taken 49 times.
203 if (!sti->avctx->extradata) {
1396 154 sti->extract_extradata.inited = 0;
1397
1398 154 ret = extract_extradata(si, st, pkt);
1399
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 154 times.
154 if (ret < 0) {
1400 av_packet_unref(pkt);
1401 return ret;
1402 }
1403 }
1404
1405 203 sti->codec_desc = avcodec_descriptor_get(sti->avctx->codec_id);
1406
1407 203 sti->need_context_update = 0;
1408 }
1409
1410
2/2
✓ Branch 0 taken 342695 times.
✓ Branch 1 taken 745547 times.
1088242 if (pkt->pts != AV_NOPTS_VALUE &&
1411
2/2
✓ Branch 0 taken 169562 times.
✓ Branch 1 taken 173133 times.
342695 pkt->dts != AV_NOPTS_VALUE &&
1412
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 169562 times.
169562 pkt->pts < pkt->dts) {
1413 av_log(s, AV_LOG_WARNING,
1414 "Invalid timestamps stream=%d, pts=%s, dts=%s, size=%d\n",
1415 pkt->stream_index,
1416 av_ts2str(pkt->pts),
1417 av_ts2str(pkt->dts),
1418 pkt->size);
1419 }
1420
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1088242 times.
1088242 if (s->debug & FF_FDEBUG_TS)
1421 av_log(s, AV_LOG_DEBUG,
1422 "ff_read_packet stream=%d, pts=%s, dts=%s, size=%d, duration=%"PRId64", flags=%d\n",
1423 pkt->stream_index,
1424 av_ts2str(pkt->pts),
1425 av_ts2str(pkt->dts),
1426 pkt->size, pkt->duration, pkt->flags);
1427
1428
5/6
✓ Branch 0 taken 701323 times.
✓ Branch 1 taken 386919 times.
✓ Branch 2 taken 2785 times.
✓ Branch 3 taken 698538 times.
✓ Branch 4 taken 2785 times.
✗ Branch 5 not taken.
1088242 if (sti->need_parsing && !sti->parser && !(s->flags & AVFMT_FLAG_NOPARSE)) {
1429 2785 sti->parser = av_parser_init(st->codecpar->codec_id);
1430
2/2
✓ Branch 0 taken 1170 times.
✓ Branch 1 taken 1615 times.
2785 if (!sti->parser) {
1431 1170 av_log(s, AV_LOG_VERBOSE, "parser not found for codec "
1432 "%s, packets or times may be invalid.\n",
1433 1170 avcodec_get_name(st->codecpar->codec_id));
1434 /* no parser available: just output the raw packets */
1435 1170 sti->need_parsing = AVSTREAM_PARSE_NONE;
1436
2/2
✓ Branch 0 taken 718 times.
✓ Branch 1 taken 897 times.
1615 } else if (sti->need_parsing == AVSTREAM_PARSE_HEADERS)
1437 718 sti->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES;
1438
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 897 times.
897 else if (sti->need_parsing == AVSTREAM_PARSE_FULL_ONCE)
1439 sti->parser->flags |= PARSER_FLAG_ONCE;
1440
2/2
✓ Branch 0 taken 472 times.
✓ Branch 1 taken 425 times.
897 else if (sti->need_parsing == AVSTREAM_PARSE_FULL_RAW)
1441 472 sti->parser->flags |= PARSER_FLAG_USE_CODEC_TS;
1442 }
1443
1444
3/4
✓ Branch 0 taken 700153 times.
✓ Branch 1 taken 388089 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 700153 times.
1088242 if (!sti->need_parsing || !sti->parser) {
1445 /* no parsing needed: we just output the packet as is */
1446 388089 compute_pkt_fields(s, st, NULL, pkt, AV_NOPTS_VALUE, AV_NOPTS_VALUE);
1447
2/2
✓ Branch 0 taken 93454 times.
✓ Branch 1 taken 294635 times.
388089 if ((s->iformat->flags & AVFMT_GENERIC_INDEX) &&
1448
4/4
✓ Branch 0 taken 92708 times.
✓ Branch 1 taken 746 times.
✓ Branch 2 taken 92439 times.
✓ Branch 3 taken 269 times.
93454 (pkt->flags & AV_PKT_FLAG_KEY) && pkt->dts != AV_NOPTS_VALUE) {
1449 92439 ff_reduce_index(s, st->index);
1450 92439 av_add_index_entry(st, pkt->pos, pkt->dts,
1451 0, 0, AVINDEX_KEYFRAME);
1452 }
1453 388089 got_packet = 1;
1454
2/2
✓ Branch 0 taken 700022 times.
✓ Branch 1 taken 131 times.
700153 } else if (st->discard < AVDISCARD_ALL) {
1455
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 700022 times.
700022 if ((ret = parse_packet(s, pkt, pkt->stream_index, 0)) < 0)
1456 return ret;
1457 700022 st->codecpar->sample_rate = sti->avctx->sample_rate;
1458 700022 st->codecpar->bit_rate = sti->avctx->bit_rate;
1459 700022 ret = av_channel_layout_copy(&st->codecpar->ch_layout, &sti->avctx->ch_layout);
1460
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 700022 times.
700022 if (ret < 0)
1461 return ret;
1462 700022 st->codecpar->codec_id = sti->avctx->codec_id;
1463 } else {
1464 /* free packet */
1465 131 av_packet_unref(pkt);
1466 }
1467
2/2
✓ Branch 0 taken 363839 times.
✓ Branch 1 taken 724403 times.
1088242 if (pkt->flags & AV_PKT_FLAG_KEY)
1468 363839 sti->skip_to_keyframe = 0;
1469
2/2
✓ Branch 0 taken 85 times.
✓ Branch 1 taken 1088157 times.
1088242 if (sti->skip_to_keyframe) {
1470 85 av_packet_unref(pkt);
1471 85 got_packet = 0;
1472 }
1473 }
1474
1475
4/4
✓ Branch 0 taken 191853 times.
✓ Branch 1 taken 388089 times.
✓ Branch 2 taken 186291 times.
✓ Branch 3 taken 5562 times.
579942 if (!got_packet && fci->parse_queue.head)
1476 186291 ret = avpriv_packet_list_get(&fci->parse_queue, pkt);
1477
1478
2/2
✓ Branch 0 taken 574380 times.
✓ Branch 1 taken 5562 times.
579942 if (ret >= 0) {
1479 574380 AVStream *const st = s->streams[pkt->stream_index];
1480 574380 FFStream *const sti = ffstream(st);
1481 574380 int discard_padding = 0;
1482
3/4
✓ Branch 0 taken 4387 times.
✓ Branch 1 taken 569993 times.
✓ Branch 2 taken 4387 times.
✗ Branch 3 not taken.
574380 if (sti->first_discard_sample && pkt->pts != AV_NOPTS_VALUE) {
1483
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);
1484 4387 int64_t sample = ts_to_samples(st, pts);
1485 4387 int64_t duration = ts_to_samples(st, pkt->duration);
1486 4387 int64_t end_sample = sample + duration;
1487
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 &&
1488
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 sample < sti->last_discard_sample)
1489 14 discard_padding = FFMIN(end_sample - sti->first_discard_sample, duration);
1490 }
1491
6/6
✓ Branch 0 taken 4387 times.
✓ Branch 1 taken 569993 times.
✓ Branch 2 taken 4371 times.
✓ Branch 3 taken 16 times.
✓ Branch 4 taken 20 times.
✓ Branch 5 taken 4351 times.
574380 if (sti->start_skip_samples && (pkt->pts == 0 || pkt->pts == RELATIVE_TS_BASE))
1492 36 sti->skip_samples = sti->start_skip_samples;
1493 574380 sti->skip_samples = FFMAX(0, sti->skip_samples);
1494
4/4
✓ Branch 0 taken 574292 times.
✓ Branch 1 taken 88 times.
✓ Branch 2 taken 14 times.
✓ Branch 3 taken 574278 times.
574380 if (sti->skip_samples || discard_padding) {
1495 102 uint8_t *p = av_packet_new_side_data(pkt, AV_PKT_DATA_SKIP_SAMPLES, 10);
1496
1/2
✓ Branch 0 taken 102 times.
✗ Branch 1 not taken.
102 if (p) {
1497 102 AV_WL32(p, sti->skip_samples);
1498 102 AV_WL32(p + 4, discard_padding);
1499 102 av_log(s, AV_LOG_DEBUG, "demuxer injecting skip %u / discard %u\n",
1500 102 (unsigned)sti->skip_samples, (unsigned)discard_padding);
1501 }
1502 102 sti->skip_samples = 0;
1503 }
1504 }
1505
1506
2/2
✓ Branch 0 taken 7722 times.
✓ Branch 1 taken 572220 times.
579942 if (!fci->metafree) {
1507 7722 int metaret = av_opt_get_dict_val(s, "metadata", AV_OPT_SEARCH_CHILDREN, &metadata);
1508
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7722 times.
7722 if (metadata) {
1509 s->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
1510 av_dict_copy(&s->metadata, metadata, 0);
1511 av_dict_free(&metadata);
1512 av_opt_set_dict_val(s, "metadata", NULL, AV_OPT_SEARCH_CHILDREN);
1513 }
1514 7722 fci->metafree = metaret == AVERROR_OPTION_NOT_FOUND;
1515 }
1516
1517
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 579942 times.
579942 if (s->debug & FF_FDEBUG_TS)
1518 av_log(s, AV_LOG_DEBUG,
1519 "read_frame_internal stream=%d, pts=%s, dts=%s, "
1520 "size=%d, duration=%"PRId64", flags=%d\n",
1521 pkt->stream_index,
1522 av_ts2str(pkt->pts),
1523 av_ts2str(pkt->dts),
1524 pkt->size, pkt->duration, pkt->flags);
1525
1526 /* A demuxer might have returned EOF because of an IO error, let's
1527 * propagate this back to the user. */
1528
5/8
✓ Branch 0 taken 5450 times.
✓ Branch 1 taken 574492 times.
✓ Branch 2 taken 5226 times.
✓ Branch 3 taken 224 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 5226 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
579942 if (ret == AVERROR_EOF && s->pb && s->pb->error < 0 && s->pb->error != AVERROR(EAGAIN))
1529 ret = s->pb->error;
1530
1531 579942 return ret;
1532 }
1533
1534 519166 int av_read_frame(AVFormatContext *s, AVPacket *pkt)
1535 {
1536 519166 FFFormatContext *const si = ffformatcontext(s);
1537 519166 const int genpts = s->flags & AVFMT_FLAG_GENPTS;
1538 519166 int eof = 0;
1539 int ret;
1540 AVStream *st;
1541
1542
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 519166 times.
519166 if (!genpts) {
1543 1038332 ret = si->packet_buffer.head
1544 135702 ? avpriv_packet_list_get(&si->packet_buffer, pkt)
1545
2/2
✓ Branch 0 taken 135702 times.
✓ Branch 1 taken 383464 times.
519166 : read_frame_internal(s, pkt);
1546
2/2
✓ Branch 0 taken 4364 times.
✓ Branch 1 taken 514802 times.
519166 if (ret < 0)
1547 4364 return ret;
1548 514802 goto return_packet;
1549 }
1550
1551 for (;;) {
1552 PacketListEntry *pktl = si->packet_buffer.head;
1553
1554 if (pktl) {
1555 AVPacket *next_pkt = &pktl->pkt;
1556
1557 if (next_pkt->dts != AV_NOPTS_VALUE) {
1558 int wrap_bits = s->streams[next_pkt->stream_index]->pts_wrap_bits;
1559 // last dts seen for this stream. if any of packets following
1560 // current one had no dts, we will set this to AV_NOPTS_VALUE.
1561 int64_t last_dts = next_pkt->dts;
1562 av_assert2(wrap_bits <= 64);
1563 while (pktl && next_pkt->pts == AV_NOPTS_VALUE) {
1564 if (pktl->pkt.stream_index == next_pkt->stream_index &&
1565 av_compare_mod(next_pkt->dts, pktl->pkt.dts, 2ULL << (wrap_bits - 1)) < 0) {
1566 if (av_compare_mod(pktl->pkt.pts, pktl->pkt.dts, 2ULL << (wrap_bits - 1))) {
1567 // not B-frame
1568 next_pkt->pts = pktl->pkt.dts;
1569 }
1570 if (last_dts != AV_NOPTS_VALUE) {
1571 // Once last dts was set to AV_NOPTS_VALUE, we don't change it.
1572 last_dts = pktl->pkt.dts;
1573 }
1574 }
1575 pktl = pktl->next;
1576 }
1577 if (eof && next_pkt->pts == AV_NOPTS_VALUE && last_dts != AV_NOPTS_VALUE) {
1578 // Fixing the last reference frame had none pts issue (For MXF etc).
1579 // We only do this when
1580 // 1. eof.
1581 // 2. we are not able to resolve a pts value for current packet.
1582 // 3. the packets for this stream at the end of the files had valid dts.
1583 next_pkt->pts = last_dts + next_pkt->duration;
1584 }
1585 pktl = si->packet_buffer.head;
1586 }
1587
1588 /* read packet from packet buffer, if there is data */
1589 st = s->streams[next_pkt->stream_index];
1590 if (!(next_pkt->pts == AV_NOPTS_VALUE && st->discard < AVDISCARD_ALL &&
1591 next_pkt->dts != AV_NOPTS_VALUE && !eof)) {
1592 ret = avpriv_packet_list_get(&si->packet_buffer, pkt);
1593 goto return_packet;
1594 }
1595 }
1596
1597 ret = read_frame_internal(s, pkt);
1598 if (ret < 0) {
1599 if (pktl && ret != AVERROR(EAGAIN)) {
1600 eof = 1;
1601 continue;
1602 } else
1603 return ret;
1604 }
1605
1606 ret = avpriv_packet_list_put(&si->packet_buffer,
1607 pkt, NULL, 0);
1608 if (ret < 0) {
1609 av_packet_unref(pkt);
1610 return ret;
1611 }
1612 }
1613
1614 514802 return_packet:
1615 514802 st = s->streams[pkt->stream_index];
1616
4/4
✓ Branch 0 taken 194785 times.
✓ Branch 1 taken 320017 times.
✓ Branch 2 taken 120938 times.
✓ Branch 3 taken 73847 times.
514802 if ((s->iformat->flags & AVFMT_GENERIC_INDEX) && pkt->flags & AV_PKT_FLAG_KEY) {
1617 120938 ff_reduce_index(s, st->index);
1618 120938 av_add_index_entry(st, pkt->pos, pkt->dts, 0, 0, AVINDEX_KEYFRAME);
1619 }
1620
1621
2/2
✓ Branch 1 taken 120362 times.
✓ Branch 2 taken 394440 times.
514802 if (is_relative(pkt->dts))
1622 120362 pkt->dts -= RELATIVE_TS_BASE;
1623
2/2
✓ Branch 1 taken 118668 times.
✓ Branch 2 taken 396134 times.
514802 if (is_relative(pkt->pts))
1624 118668 pkt->pts -= RELATIVE_TS_BASE;
1625
1626 514802 return ret;
1627 }
1628
1629 /**
1630 * Return TRUE if the stream has accurate duration in any stream.
1631 *
1632 * @return TRUE if the stream has accurate duration for at least one component.
1633 */
1634 7643 static int has_duration(AVFormatContext *ic)
1635 {
1636
2/2
✓ Branch 0 taken 7946 times.
✓ Branch 1 taken 2454 times.
10400 for (unsigned i = 0; i < ic->nb_streams; i++) {
1637 7946 const AVStream *const st = ic->streams[i];
1638
2/2
✓ Branch 0 taken 5189 times.
✓ Branch 1 taken 2757 times.
7946 if (st->duration != AV_NOPTS_VALUE)
1639 5189 return 1;
1640 }
1641
2/2
✓ Branch 0 taken 442 times.
✓ Branch 1 taken 2012 times.
2454 if (ic->duration != AV_NOPTS_VALUE)
1642 442 return 1;
1643 2012 return 0;
1644 }
1645
1646 /**
1647 * Estimate the stream timings from the one of each components.
1648 *
1649 * Also computes the global bitrate if possible.
1650 */
1651 13414 static void update_stream_timings(AVFormatContext *ic)
1652 {
1653 int64_t start_time, start_time1, start_time_text, end_time, end_time1, end_time_text;
1654 int64_t duration, duration1, duration_text, filesize;
1655
1656 13414 start_time = INT64_MAX;
1657 13414 start_time_text = INT64_MAX;
1658 13414 end_time = INT64_MIN;
1659 13414 end_time_text = INT64_MIN;
1660 13414 duration = INT64_MIN;
1661 13414 duration_text = INT64_MIN;
1662
1663
2/2
✓ Branch 0 taken 14906 times.
✓ Branch 1 taken 13414 times.
28320 for (unsigned i = 0; i < ic->nb_streams; i++) {
1664 14906 AVStream *const st = ic->streams[i];
1665
2/2
✓ Branch 0 taken 14776 times.
✓ Branch 1 taken 130 times.
29682 int is_text = st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE ||
1666
2/2
✓ Branch 0 taken 153 times.
✓ Branch 1 taken 14623 times.
14776 st->codecpar->codec_type == AVMEDIA_TYPE_DATA;
1667
1668
3/4
✓ Branch 0 taken 12222 times.
✓ Branch 1 taken 2684 times.
✓ Branch 2 taken 12222 times.
✗ Branch 3 not taken.
14906 if (st->start_time != AV_NOPTS_VALUE && st->time_base.den) {
1669 12222 start_time1 = av_rescale_q(st->start_time, st->time_base,
1670 12222 AV_TIME_BASE_Q);
1671
2/2
✓ Branch 0 taken 158 times.
✓ Branch 1 taken 12064 times.
12222 if (is_text)
1672 158 start_time_text = FFMIN(start_time_text, start_time1);
1673 else
1674 12064 start_time = FFMIN(start_time, start_time1);
1675 12222 end_time1 = av_rescale_q_rnd(st->duration, st->time_base,
1676 12222 AV_TIME_BASE_Q,
1677 AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
1678
5/6
✓ Branch 0 taken 10804 times.
✓ Branch 1 taken 1418 times.
✓ Branch 2 taken 10775 times.
✓ Branch 3 taken 29 times.
✓ Branch 4 taken 10804 times.
✗ Branch 5 not taken.
12222 if (end_time1 != AV_NOPTS_VALUE && (end_time1 > 0 ? start_time1 <= INT64_MAX - end_time1 : start_time1 >= INT64_MIN - end_time1)) {
1679 10804 end_time1 += start_time1;
1680
2/2
✓ Branch 0 taken 143 times.
✓ Branch 1 taken 10661 times.
10804 if (is_text)
1681 143 end_time_text = FFMAX(end_time_text, end_time1);
1682 else
1683 10661 end_time = FFMAX(end_time, end_time1);
1684 }
1685
2/2
✓ Branch 1 taken 212 times.
✓ Branch 2 taken 12222 times.
12434 for (AVProgram *p = NULL; (p = av_find_program_from_stream(ic, p, i)); ) {
1686
4/4
✓ Branch 0 taken 154 times.
✓ Branch 1 taken 58 times.
✓ Branch 2 taken 25 times.
✓ Branch 3 taken 129 times.
212 if (p->start_time == AV_NOPTS_VALUE || p->start_time > start_time1)
1687 83 p->start_time = start_time1;
1688
2/2
✓ Branch 0 taken 53 times.
✓ Branch 1 taken 159 times.
212 if (p->end_time < end_time1)
1689 53 p->end_time = end_time1;
1690 }
1691 }
1692
2/2
✓ Branch 0 taken 12273 times.
✓ Branch 1 taken 2633 times.
14906 if (st->duration != AV_NOPTS_VALUE) {
1693 12273 duration1 = av_rescale_q(st->duration, st->time_base,
1694 12273 AV_TIME_BASE_Q);
1695
2/2
✓ Branch 0 taken 167 times.
✓ Branch 1 taken 12106 times.
12273 if (is_text)
1696 167 duration_text = FFMAX(duration_text, duration1);
1697 else
1698 12106 duration = FFMAX(duration, duration1);
1699 }
1700 }
1701
3/6
✓ Branch 0 taken 10947 times.
✓ Branch 1 taken 2467 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 10947 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
13414 if (start_time == INT64_MAX || (start_time > start_time_text && start_time - (uint64_t)start_time_text < AV_TIME_BASE))
1702 2467 start_time = start_time_text;
1703
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10947 times.
10947 else if (start_time > start_time_text)
1704 av_log(ic, AV_LOG_VERBOSE, "Ignoring outlier non primary stream starttime %f\n", start_time_text / (float)AV_TIME_BASE);
1705
1706
5/6
✓ Branch 0 taken 9954 times.
✓ Branch 1 taken 3460 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 9952 times.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
13414 if (end_time == INT64_MIN || (end_time < end_time_text && end_time_text - (uint64_t)end_time < AV_TIME_BASE))
1707 3462 end_time = end_time_text;
1708
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9952 times.
9952 else if (end_time < end_time_text)
1709 av_log(ic, AV_LOG_VERBOSE, "Ignoring outlier non primary stream endtime %f\n", end_time_text / (float)AV_TIME_BASE);
1710
1711
5/6
✓ Branch 0 taken 11384 times.
✓ Branch 1 taken 2030 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 11378 times.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
13414 if (duration == INT64_MIN || (duration < duration_text && (uint64_t)duration_text - duration < AV_TIME_BASE))
1712 2036 duration = duration_text;
1713
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11378 times.
11378 else if (duration < duration_text)
1714 av_log(ic, AV_LOG_VERBOSE, "Ignoring outlier non primary stream duration %f\n", duration_text / (float)AV_TIME_BASE);
1715
1716
2/2
✓ Branch 0 taken 10968 times.
✓ Branch 1 taken 2446 times.
13414 if (start_time != INT64_MAX) {
1717 10968 ic->start_time = start_time;
1718
2/2
✓ Branch 0 taken 9971 times.
✓ Branch 1 taken 997 times.
10968 if (end_time != INT64_MIN) {
1719
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9971 times.
9971 if (ic->nb_programs > 1) {
1720 for (unsigned i = 0; i < ic->nb_programs; i++) {
1721 AVProgram *const p = ic->programs[i];
1722
1723 if (p->start_time != AV_NOPTS_VALUE &&
1724 p->end_time > p->start_time &&
1725 p->end_time - (uint64_t)p->start_time <= INT64_MAX)
1726 duration = FFMAX(duration, p->end_time - p->start_time);
1727 }
1728
2/4
✓ Branch 0 taken 9971 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9971 times.
✗ Branch 3 not taken.
9971 } else if (end_time >= start_time && end_time - (uint64_t)start_time <= INT64_MAX) {
1729 9971 duration = FFMAX(duration, end_time - start_time);
1730 }
1731 }
1732 }
1733
6/6
✓ Branch 0 taken 11414 times.
✓ Branch 1 taken 2000 times.
✓ Branch 2 taken 11378 times.
✓ Branch 3 taken 36 times.
✓ Branch 4 taken 6106 times.
✓ Branch 5 taken 5272 times.
13414 if (duration != INT64_MIN && duration > 0 && ic->duration == AV_NOPTS_VALUE) {
1734 6106 ic->duration = duration;
1735 }
1736
5/6
✓ Branch 0 taken 7147 times.
✓ Branch 1 taken 6267 times.
✓ Branch 3 taken 7147 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 6013 times.
✓ Branch 6 taken 1134 times.
13414 if (ic->pb && (filesize = avio_size(ic->pb)) > 0 && ic->duration > 0) {
1737 /* compute the bitrate */
1738 6013 double bitrate = (double) filesize * 8.0 * AV_TIME_BASE /
1739 6013 (double) ic->duration;
1740
2/4
✓ Branch 0 taken 6013 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6013 times.
✗ Branch 3 not taken.
6013 if (bitrate >= 0 && bitrate <= INT64_MAX)
1741 6013 ic->bit_rate = bitrate;
1742 }
1743 13414 }
1744
1745 5701 static void fill_all_stream_timings(AVFormatContext *ic)
1746 {
1747 5701 update_stream_timings(ic);
1748
2/2
✓ Branch 0 taken 6370 times.
✓ Branch 1 taken 5701 times.
12071 for (unsigned i = 0; i < ic->nb_streams; i++) {
1749 6370 AVStream *const st = ic->streams[i];
1750
1751
2/2
✓ Branch 0 taken 777 times.
✓ Branch 1 taken 5593 times.
6370 if (st->start_time == AV_NOPTS_VALUE) {
1752
2/2
✓ Branch 0 taken 129 times.
✓ Branch 1 taken 648 times.
777 if (ic->start_time != AV_NOPTS_VALUE)
1753 129 st->start_time = av_rescale_q(ic->start_time, AV_TIME_BASE_Q,
1754 st->time_base);
1755
2/2
✓ Branch 0 taken 762 times.
✓ Branch 1 taken 15 times.
777 if (ic->duration != AV_NOPTS_VALUE)
1756 762 st->duration = av_rescale_q(ic->duration, AV_TIME_BASE_Q,
1757 st->time_base);
1758 }
1759 }
1760 5701 }
1761
1762 2012 static void estimate_timings_from_bit_rate(AVFormatContext *ic)
1763 {
1764 2012 FFFormatContext *const si = ffformatcontext(ic);
1765 2012 int show_warning = 0;
1766
1767 /* if bit_rate is already set, we believe it */
1768
2/2
✓ Branch 0 taken 1977 times.
✓ Branch 1 taken 35 times.
2012 if (ic->bit_rate <= 0) {
1769 1977 int64_t bit_rate = 0;
1770
2/2
✓ Branch 0 taken 2107 times.
✓ Branch 1 taken 1323 times.
3430 for (unsigned i = 0; i < ic->nb_streams; i++) {
1771 2107 const AVStream *const st = ic->streams[i];
1772 2107 const FFStream *const sti = cffstream(st);
1773
4/4
✓ Branch 0 taken 1329 times.
✓ Branch 1 taken 778 times.
✓ Branch 2 taken 112 times.
✓ Branch 3 taken 1217 times.
2107 if (st->codecpar->bit_rate <= 0 && sti->avctx->bit_rate > 0)
1774 112 st->codecpar->bit_rate = sti->avctx->bit_rate;
1775
2/2
✓ Branch 0 taken 890 times.
✓ Branch 1 taken 1217 times.
2107 if (st->codecpar->bit_rate > 0) {
1776
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 890 times.
890 if (INT64_MAX - st->codecpar->bit_rate < bit_rate) {
1777 bit_rate = 0;
1778 break;
1779 }
1780 890 bit_rate += st->codecpar->bit_rate;
1781
4/4
✓ Branch 0 taken 1042 times.
✓ Branch 1 taken 175 times.
✓ Branch 2 taken 654 times.
✓ Branch 3 taken 388 times.
1217 } else if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && sti->codec_info_nb_frames > 1) {
1782 // If we have a videostream with packets but without a bitrate
1783 // then consider the sum not known
1784 654 bit_rate = 0;
1785 654 break;
1786 }
1787 }
1788 1977 ic->bit_rate = bit_rate;
1789 }
1790
1791 /* if duration is already set, we believe it */
1792
1/2
✓ Branch 0 taken 2012 times.
✗ Branch 1 not taken.
2012 if (ic->duration == AV_NOPTS_VALUE &&
1793
2/2
✓ Branch 0 taken 897 times.
✓ Branch 1 taken 1115 times.
2012 ic->bit_rate != 0) {
1794
2/2
✓ Branch 0 taken 871 times.
✓ Branch 1 taken 26 times.
897 int64_t filesize = ic->pb ? avio_size(ic->pb) : 0;
1795
2/2
✓ Branch 0 taken 865 times.
✓ Branch 1 taken 32 times.
897 if (filesize > si->data_offset) {
1796 865 filesize -= si->data_offset;
1797
2/2
✓ Branch 0 taken 902 times.
✓ Branch 1 taken 865 times.
1767 for (unsigned i = 0; i < ic->nb_streams; i++) {
1798 902 AVStream *const st = ic->streams[i];
1799
1800
1/2
✓ Branch 0 taken 902 times.
✗ Branch 1 not taken.
902 if ( st->time_base.num <= INT64_MAX / ic->bit_rate
1801
1/2
✓ Branch 0 taken 902 times.
✗ Branch 1 not taken.
902 && st->duration == AV_NOPTS_VALUE) {
1802 902 st->duration = av_rescale(filesize, 8LL * st->time_base.den,
1803 902 ic->bit_rate *
1804 902 (int64_t) st->time_base.num);
1805 902 show_warning = 1;
1806 }
1807 }
1808 }
1809 }
1810
2/2
✓ Branch 0 taken 865 times.
✓ Branch 1 taken 1147 times.
2012 if (show_warning)
1811 865 av_log(ic, AV_LOG_WARNING,
1812 "Estimating duration from bitrate, this may be inaccurate\n");
1813 2012 }
1814
1815 #define DURATION_DEFAULT_MAX_READ_SIZE 250000LL
1816 #define DURATION_DEFAULT_MAX_RETRY 6
1817 #define DURATION_MAX_RETRY 1
1818
1819 /* only usable for MPEG-PS streams */
1820 70 static void estimate_timings_from_pts(AVFormatContext *ic, int64_t old_offset)
1821 {
1822 70 FFFormatContext *const si = ffformatcontext(ic);
1823 70 AVPacket *const pkt = si->pkt;
1824 int num, den, read_size, ret;
1825
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 70 times.
70 int64_t duration_max_read_size = ic->duration_probesize ? ic->duration_probesize >> DURATION_MAX_RETRY : DURATION_DEFAULT_MAX_READ_SIZE;
1826
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 70 times.
70 int duration_max_retry = ic->duration_probesize ? DURATION_MAX_RETRY : DURATION_DEFAULT_MAX_RETRY;
1827 70 int found_duration = 0;
1828 int is_end;
1829 int64_t filesize, offset, duration;
1830 70 int retry = 0;
1831
1832 /* flush packet queue */
1833 70 ff_flush_packet_queue(ic);
1834
1835
2/2
✓ Branch 0 taken 144 times.
✓ Branch 1 taken 70 times.
214 for (unsigned i = 0; i < ic->nb_streams; i++) {
1836 144 AVStream *const st = ic->streams[i];
1837 144 FFStream *const sti = ffstream(st);
1838
1839
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 122 times.
144 if (st->start_time == AV_NOPTS_VALUE &&
1840
1/2
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
22 sti->first_dts == AV_NOPTS_VALUE &&
1841
1/2
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
22 st->codecpar->codec_type != AVMEDIA_TYPE_UNKNOWN)
1842 22 av_log(ic, AV_LOG_WARNING,
1843 "start time for stream %d is not set in estimate_timings_from_pts\n", i);
1844
1845
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 119 times.
144 if (sti->parser) {
1846 25 av_parser_close(sti->parser);
1847 25 sti->parser = NULL;
1848 }
1849 }
1850
1851
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 70 times.
70 if (ic->skip_estimate_duration_from_pts) {
1852 av_log(ic, AV_LOG_INFO, "Skipping duration calculation in estimate_timings_from_pts\n");
1853 goto skip_duration_calc;
1854 }
1855
1856 70 av_opt_set_int(ic, "skip_changes", 1, AV_OPT_SEARCH_CHILDREN);
1857 /* estimate the end time (duration) */
1858 /* XXX: may need to support wrapping */
1859
1/2
✓ Branch 0 taken 70 times.
✗ Branch 1 not taken.
70 filesize = ic->pb ? avio_size(ic->pb) : 0;
1860 do {
1861 77 is_end = found_duration;
1862 77 offset = filesize - (duration_max_read_size << retry);
1863
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 51 times.
77 if (offset < 0)
1864 26 offset = 0;
1865
1866 77 avio_seek(ic->pb, offset, SEEK_SET);
1867 77 read_size = 0;
1868 6598 for (;;) {
1869 AVStream *st;
1870 FFStream *sti;
1871
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 6668 times.
6675 if (read_size >= duration_max_read_size << (FFMAX(retry - 1, 0)))
1872 7 break;
1873
1874 do {
1875 6668 ret = ff_read_packet(ic, pkt);
1876
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6668 times.
6668 } while (ret == AVERROR(EAGAIN));
1877
2/2
✓ Branch 0 taken 70 times.
✓ Branch 1 taken 6598 times.
6668 if (ret != 0)
1878 70 break;
1879 6598 read_size += pkt->size;
1880 6598 st = ic->streams[pkt->stream_index];
1881 6598 sti = ffstream(st);
1882
2/2
✓ Branch 0 taken 2849 times.
✓ Branch 1 taken 3749 times.
6598 if (pkt->pts != AV_NOPTS_VALUE &&
1883
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2849 times.
2849 (st->start_time != AV_NOPTS_VALUE ||
1884 sti->first_dts != AV_NOPTS_VALUE)) {
1885
1/2
✓ Branch 0 taken 2849 times.
✗ Branch 1 not taken.
2849 if (pkt->duration == 0) {
1886 2849 compute_frame_duration(ic, &num, &den, st, sti->parser, pkt);
1887
3/4
✓ Branch 0 taken 2389 times.
✓ Branch 1 taken 460 times.
✓ Branch 2 taken 2389 times.
✗ Branch 3 not taken.
2849 if (den && num) {
1888 2389 pkt->duration = av_rescale_rnd(1,
1889 2389 num * (int64_t) st->time_base.den,
1890 2389 den * (int64_t) st->time_base.num,
1891 AV_ROUND_DOWN);
1892 }
1893 }
1894 2849 duration = pkt->pts + pkt->duration;
1895 2849 found_duration = 1;
1896
1/2
✓ Branch 0 taken 2849 times.
✗ Branch 1 not taken.
2849 if (st->start_time != AV_NOPTS_VALUE)
1897 2849 duration -= st->start_time;
1898 else
1899 duration -= sti->first_dts;
1900
2/2
✓ Branch 0 taken 2840 times.
✓ Branch 1 taken 9 times.
2849 if (duration > 0) {
1901
3/4
✓ Branch 0 taken 2728 times.
✓ Branch 1 taken 112 times.
✓ Branch 2 taken 2728 times.
✗ Branch 3 not taken.
2840 if (st->duration == AV_NOPTS_VALUE || sti->info->last_duration<= 0 ||
1902
3/4
✓ Branch 0 taken 2446 times.
✓ Branch 1 taken 282 times.
✓ Branch 2 taken 2446 times.
✗ Branch 3 not taken.
2728 (st->duration < duration && FFABS(duration - sti->info->last_duration) < 60LL*st->time_base.den / st->time_base.num))
1903 2558 st->duration = duration;
1904 2840 sti->info->last_duration = duration;
1905 }
1906 }
1907 6598 av_packet_unref(pkt);
1908 }
1909
1910 /* check if all audio/video streams have valid duration */
1911
2/2
✓ Branch 0 taken 70 times.
✓ Branch 1 taken 7 times.
77 if (!is_end) {
1912 70 is_end = 1;
1913
2/2
✓ Branch 0 taken 144 times.
✓ Branch 1 taken 70 times.
214 for (unsigned i = 0; i < ic->nb_streams; i++) {
1914 144 const AVStream *const st = ic->streams[i];
1915
2/2
✓ Branch 0 taken 126 times.
✓ Branch 1 taken 18 times.
144 switch (st->codecpar->codec_type) {
1916 126 case AVMEDIA_TYPE_VIDEO:
1917 case AVMEDIA_TYPE_AUDIO:
1918
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 102 times.
126 if (st->duration == AV_NOPTS_VALUE)
1919 24 is_end = 0;
1920 }
1921 }
1922 }
1923
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 2 times.
9 } while (!is_end &&
1924
3/4
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 68 times.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
84 offset &&
1925 ++retry <= duration_max_retry);
1926
1927 70 av_opt_set_int(ic, "skip_changes", 0, AV_OPT_SEARCH_CHILDREN);
1928
1929 /* warn about audio/video streams which duration could not be estimated */
1930
2/2
✓ Branch 0 taken 144 times.
✓ Branch 1 taken 70 times.
214 for (unsigned i = 0; i < ic->nb_streams; i++) {
1931 144 const AVStream *const st = ic->streams[i];
1932 144 const FFStream *const sti = cffstream(st);
1933
1934
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 112 times.
144 if (st->duration == AV_NOPTS_VALUE) {
1935
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 15 times.
32 switch (st->codecpar->codec_type) {
1936 17 case AVMEDIA_TYPE_VIDEO:
1937 case AVMEDIA_TYPE_AUDIO:
1938
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) {
1939 7 av_log(ic, AV_LOG_WARNING, "stream %d : no PTS found at end of file, duration not set\n", i);
1940 } else
1941 10 av_log(ic, AV_LOG_WARNING, "stream %d : no TS found at start of file, duration not set\n", i);
1942 }
1943 }
1944 }
1945 70 skip_duration_calc:
1946 70 fill_all_stream_timings(ic);
1947
1948 70 avio_seek(ic->pb, old_offset, SEEK_SET);
1949
2/2
✓ Branch 0 taken 144 times.
✓ Branch 1 taken 70 times.
214 for (unsigned i = 0; i < ic->nb_streams; i++) {
1950 144 AVStream *const st = ic->streams[i];
1951 144 FFStream *const sti = ffstream(st);
1952
1953 144 sti->cur_dts = sti->first_dts;
1954 144 sti->last_IP_pts = AV_NOPTS_VALUE;
1955 144 sti->last_dts_for_order_check = AV_NOPTS_VALUE;
1956
2/2
✓ Branch 0 taken 2448 times.
✓ Branch 1 taken 144 times.
2592 for (int j = 0; j < MAX_REORDER_DELAY + 1; j++)
1957 2448 sti->pts_buffer[j] = AV_NOPTS_VALUE;
1958 }
1959 70 }
1960
1961 /* 1:1 map to AVDurationEstimationMethod */
1962 static const char *const duration_name[] = {
1963 [AVFMT_DURATION_FROM_PTS] = "pts",
1964 [AVFMT_DURATION_FROM_STREAM] = "stream",
1965 [AVFMT_DURATION_FROM_BITRATE] = "bit rate",
1966 };
1967
1968 7713 static const char *duration_estimate_name(enum AVDurationEstimationMethod method)
1969 {
1970 7713 return duration_name[method];
1971 }
1972
1973 7713 static void estimate_timings(AVFormatContext *ic, int64_t old_offset)
1974 {
1975 int64_t file_size;
1976
1977 /* get the file size, if possible */
1978
2/2
✓ Branch 0 taken 3183 times.
✓ Branch 1 taken 4530 times.
7713 if (ic->iformat->flags & AVFMT_NOFILE) {
1979 3183 file_size = 0;
1980 } else {
1981 4530 file_size = avio_size(ic->pb);
1982 4530 file_size = FFMAX(0, file_size);
1983 }
1984
1985
2/2
✓ Branch 0 taken 7688 times.
✓ Branch 1 taken 25 times.
7713 if ((!strcmp(ic->iformat->name, "mpeg") ||
1986
3/4
✓ Branch 0 taken 45 times.
✓ Branch 1 taken 7643 times.
✓ Branch 2 taken 70 times.
✗ Branch 3 not taken.
7713 !strcmp(ic->iformat->name, "mpegts")) &&
1987
1/2
✓ Branch 0 taken 70 times.
✗ Branch 1 not taken.
70 file_size && (ic->pb->seekable & AVIO_SEEKABLE_NORMAL)) {
1988 /* get accurate estimate from the PTSes */
1989 70 estimate_timings_from_pts(ic, old_offset);
1990 70 ic->duration_estimation_method = AVFMT_DURATION_FROM_PTS;
1991
2/2
✓ Branch 1 taken 5631 times.
✓ Branch 2 taken 2012 times.
7643 } else if (has_duration(ic)) {
1992 /* at least one component has timings - we use them for all
1993 * the components */
1994 5631 fill_all_stream_timings(ic);
1995 /* nut demuxer estimate the duration from PTS */
1996
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 5601 times.
5631 if (!strcmp(ic->iformat->name, "nut"))
1997 30 ic->duration_estimation_method = AVFMT_DURATION_FROM_PTS;
1998 else
1999 5601 ic->duration_estimation_method = AVFMT_DURATION_FROM_STREAM;
2000 } else {
2001 /* less precise: use bitrate info */
2002 2012 estimate_timings_from_bit_rate(ic);
2003 2012 ic->duration_estimation_method = AVFMT_DURATION_FROM_BITRATE;
2004 }
2005 7713 update_stream_timings(ic);
2006
2007
2/2
✓ Branch 0 taken 8536 times.
✓ Branch 1 taken 7713 times.
16249 for (unsigned i = 0; i < ic->nb_streams; i++) {
2008 8536 AVStream *const st = ic->streams[i];
2009
1/2
✓ Branch 0 taken 8536 times.
✗ Branch 1 not taken.
8536 if (st->time_base.den)
2010 8536 av_log(ic, AV_LOG_TRACE, "stream %u: start_time: %s duration: %s\n", i,
2011 8536 av_ts2timestr(st->start_time, &st->time_base),
2012 8536 av_ts2timestr(st->duration, &st->time_base));
2013 }
2014 7713 av_log(ic, AV_LOG_TRACE,
2015 "format: start_time: %s duration: %s (estimate from %s) bitrate=%"PRId64" kb/s\n",
2016 7713 av_ts2timestr(ic->start_time, &AV_TIME_BASE_Q),
2017 7713 av_ts2timestr(ic->duration, &AV_TIME_BASE_Q),
2018 duration_estimate_name(ic->duration_estimation_method),
2019 7713 (int64_t)ic->bit_rate / 1000);
2020 7713 }
2021
2022 104796 static int determinable_frame_size(const AVCodecContext *avctx)
2023 {
2024
2/2
✓ Branch 0 taken 248 times.
✓ Branch 1 taken 104548 times.
104796 switch(avctx->codec_id) {
2025 248 case AV_CODEC_ID_MP1:
2026 case AV_CODEC_ID_MP2:
2027 case AV_CODEC_ID_MP3:
2028 case AV_CODEC_ID_CODEC2:
2029 248 return 1;
2030 }
2031
2032 104548 return 0;
2033 }
2034
2035 429658 static int has_codec_parameters(const AVStream *st, const char **errmsg_ptr)
2036 {
2037 429658 const FFStream *const sti = cffstream(st);
2038 429658 const AVCodecContext *const avctx = sti->avctx;
2039
2040 #define FAIL(errmsg) do { \
2041 if (errmsg_ptr) \
2042 *errmsg_ptr = errmsg; \
2043 return 0; \
2044 } while (0)
2045
2046
2/2
✓ Branch 0 taken 641 times.
✓ Branch 1 taken 429017 times.
429658 if ( avctx->codec_id == AV_CODEC_ID_NONE
2047
2/2
✓ Branch 0 taken 419 times.
✓ Branch 1 taken 222 times.
641 && avctx->codec_type != AVMEDIA_TYPE_DATA)
2048
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 414 times.
419 FAIL("unknown codec");
2049
4/5
✓ Branch 0 taken 120179 times.
✓ Branch 1 taken 307497 times.
✓ Branch 2 taken 644 times.
✓ Branch 3 taken 919 times.
✗ Branch 4 not taken.
429239 switch (avctx->codec_type) {
2050 120179 case AVMEDIA_TYPE_AUDIO:
2051
4/4
✓ Branch 0 taken 104796 times.
✓ Branch 1 taken 15383 times.
✓ Branch 3 taken 248 times.
✓ Branch 4 taken 104548 times.
120179 if (!avctx->frame_size && determinable_frame_size(avctx))
2052
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 246 times.
248 FAIL("unspecified frame size");
2053
1/2
✓ Branch 0 taken 119931 times.
✗ Branch 1 not taken.
119931 if (sti->info->found_decoder >= 0 &&
2054
2/2
✓ Branch 0 taken 2450 times.
✓ Branch 1 taken 117481 times.
119931 avctx->sample_fmt == AV_SAMPLE_FMT_NONE)
2055
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2450 times.
2450 FAIL("unspecified sample format");
2056
2/2
✓ Branch 0 taken 137 times.
✓ Branch 1 taken 117344 times.
117481 if (!avctx->sample_rate)
2057
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 131 times.
137 FAIL("unspecified sample rate");
2058
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 117327 times.
117344 if (!avctx->ch_layout.nb_channels)
2059
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
17 FAIL("unspecified number of channels");
2060
4/6
✓ Branch 0 taken 117327 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 81187 times.
✓ Branch 3 taken 36140 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 81187 times.
117327 if (sti->info->found_decoder >= 0 && !sti->nb_decoded_frames && avctx->codec_id == AV_CODEC_ID_DTS)
2061 FAIL("no decodable DTS frames");
2062 117327 break;
2063 307497 case AVMEDIA_TYPE_VIDEO:
2064
2/2
✓ Branch 0 taken 13099 times.
✓ Branch 1 taken 294398 times.
307497 if (!avctx->width)
2065
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 13089 times.
13099 FAIL("unspecified size");
2066
4/4
✓ Branch 0 taken 294343 times.
✓ Branch 1 taken 55 times.
✓ Branch 2 taken 4294 times.
✓ Branch 3 taken 290049 times.
294398 if (sti->info->found_decoder >= 0 && avctx->pix_fmt == AV_PIX_FMT_NONE)
2067
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 4291 times.
4294 FAIL("unspecified pixel format");
2068
4/4
✓ Branch 0 taken 290004 times.
✓ Branch 1 taken 100 times.
✓ Branch 2 taken 86 times.
✓ Branch 3 taken 289918 times.
290104 if (st->codecpar->codec_id == AV_CODEC_ID_RV30 || st->codecpar->codec_id == AV_CODEC_ID_RV40)
2069
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)
2070
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 67 times.
67 FAIL("no frame in rv30/40 and no sar");
2071 290037 break;
2072 644 case AVMEDIA_TYPE_SUBTITLE:
2073
4/4
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 609 times.
✓ Branch 2 taken 21 times.
✓ Branch 3 taken 14 times.
644 if (avctx->codec_id == AV_CODEC_ID_HDMV_PGS_SUBTITLE && !avctx->width)
2074
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 FAIL("unspecified size");
2075 623 break;
2076 919 case AVMEDIA_TYPE_DATA:
2077
2/2
✓ Branch 0 taken 222 times.
✓ Branch 1 taken 697 times.
919 if (avctx->codec_id == AV_CODEC_ID_NONE) return 1;
2078 }
2079
2080 408684 return 1;
2081 }
2082
2083 /* returns 1 or 0 if or if not decoded data was returned, or a negative error */
2084 199843 static int try_decode_frame(AVFormatContext *s, AVStream *st,
2085 const AVPacket *pkt, AVDictionary **options)
2086 {
2087 199843 FFStream *const sti = ffstream(st);
2088 199843 AVCodecContext *const avctx = sti->avctx;
2089 const AVCodec *codec;
2090 199843 int got_picture = 1, ret = 0;
2091 199843 AVFrame *frame = av_frame_alloc();
2092 AVSubtitle subtitle;
2093 199843 int do_skip_frame = 0;
2094 enum AVDiscard skip_frame;
2095 199843 int pkt_to_send = pkt->size > 0;
2096
2097
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 199843 times.
199843 if (!frame)
2098 return AVERROR(ENOMEM);
2099
2100
2/2
✓ Branch 1 taken 1481 times.
✓ Branch 2 taken 198362 times.
199843 if (!avcodec_is_open(avctx) &&
2101
1/2
✓ Branch 0 taken 1481 times.
✗ Branch 1 not taken.
1481 sti->info->found_decoder <= 0 &&
2102
4/4
✓ Branch 0 taken 95 times.
✓ Branch 1 taken 1386 times.
✓ Branch 2 taken 57 times.
✓ Branch 3 taken 38 times.
1481 (st->codecpar->codec_id != -sti->info->found_decoder || !st->codecpar->codec_id)) {
2103 1443 AVDictionary *thread_opt = NULL;
2104
2105 1443 codec = find_probe_decoder(s, st, st->codecpar->codec_id);
2106
2107
2/2
✓ Branch 0 taken 71 times.
✓ Branch 1 taken 1372 times.
1443 if (!codec) {
2108 71 sti->info->found_decoder = -st->codecpar->codec_id;
2109 71 ret = -1;
2110 73 goto fail;
2111 }
2112
2113 /* Force thread count to 1 since the H.264 decoder will not extract
2114 * SPS and PPS to extradata during multi-threaded decoding. */
2115
2/2
✓ Branch 0 taken 163 times.
✓ Branch 1 taken 1209 times.
1372 av_dict_set(options ? options : &thread_opt, "threads", "1", 0);
2116 /* Force lowres to 0. The decoder might reduce the video size by the
2117 * lowres factor, and we don't want that propagated to the stream's
2118 * codecpar */
2119
2/2
✓ Branch 0 taken 163 times.
✓ Branch 1 taken 1209 times.
1372 av_dict_set(options ? options : &thread_opt, "lowres", "0", 0);
2120
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1372 times.
1372 if (s->codec_whitelist)
2121 av_dict_set(options ? options : &thread_opt, "codec_whitelist", s->codec_whitelist, 0);
2122
2/2
✓ Branch 0 taken 163 times.
✓ Branch 1 taken 1209 times.
1372 ret = avcodec_open2(avctx, codec, options ? options : &thread_opt);
2123
2/2
✓ Branch 0 taken 163 times.
✓ Branch 1 taken 1209 times.
1372 if (!options)
2124 163 av_dict_free(&thread_opt);
2125
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1370 times.
1372 if (ret < 0) {
2126 2 sti->info->found_decoder = -avctx->codec_id;
2127 2 goto fail;
2128 }
2129 1370 sti->info->found_decoder = 1;
2130
2/2
✓ Branch 0 taken 6878 times.
✓ Branch 1 taken 191522 times.
198400 } else if (!sti->info->found_decoder)
2131 6878 sti->info->found_decoder = 1;
2132
2133
2/2
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 199732 times.
199770 if (sti->info->found_decoder < 0) {
2134 38 ret = -1;
2135 38 goto fail;
2136 }
2137
2138
2/2
✓ Branch 1 taken 111526 times.
✓ Branch 2 taken 88206 times.
199732 if (avpriv_codec_get_cap_skip_frame_fill_param(avctx->codec)) {
2139 111526 do_skip_frame = 1;
2140 111526 skip_frame = avctx->skip_frame;
2141 111526 avctx->skip_frame = AVDISCARD_ALL;
2142 }
2143
2144
5/6
✓ Branch 0 taken 8123 times.
✓ Branch 1 taken 4692 times.
✓ Branch 2 taken 26 times.
✓ Branch 3 taken 4666 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 199740 times.
407621 while ((pkt_to_send || (!pkt->data && got_picture)) &&
2145
4/4
✓ Branch 0 taken 12815 times.
✓ Branch 1 taken 195074 times.
✓ Branch 2 taken 5693 times.
✓ Branch 3 taken 194047 times.
407629 ret >= 0 &&
2146
2/2
✓ Branch 2 taken 2241 times.
✓ Branch 3 taken 191806 times.
393787 (!has_codec_parameters(st, NULL) || !has_decode_delay_been_guessed(st) ||
2147
2/2
✓ Branch 0 taken 188964 times.
✓ Branch 1 taken 2842 times.
191806 (!sti->codec_info_nb_frames &&
2148
2/2
✓ Branch 0 taken 463 times.
✓ Branch 1 taken 2379 times.
2842 (avctx->codec->capabilities & AV_CODEC_CAP_CHANNEL_CONF)))) {
2149 8397 got_picture = 0;
2150
2/2
✓ Branch 0 taken 747 times.
✓ Branch 1 taken 7650 times.
8397 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO ||
2151
2/2
✓ Branch 0 taken 740 times.
✓ Branch 1 taken 7 times.
747 avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
2152 8390 ret = avcodec_send_packet(avctx, pkt);
2153
5/6
✓ Branch 0 taken 249 times.
✓ Branch 1 taken 8141 times.
✓ Branch 2 taken 249 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 240 times.
✓ Branch 5 taken 9 times.
8390 if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
2154 240 break;
2155
2/2
✓ Branch 0 taken 8141 times.
✓ Branch 1 taken 9 times.
8150 if (ret >= 0)
2156 8141 pkt_to_send = 0;
2157 8150 ret = avcodec_receive_frame(avctx, frame);
2158
2/2
✓ Branch 0 taken 3179 times.
✓ Branch 1 taken 4971 times.
8150 if (ret >= 0)
2159 3179 got_picture = 1;
2160
4/4
✓ Branch 0 taken 3205 times.
✓ Branch 1 taken 4945 times.
✓ Branch 2 taken 26 times.
✓ Branch 3 taken 3179 times.
8150 if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
2161 4971 ret = 0;
2162
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 } else if (avctx->codec_type == AVMEDIA_TYPE_SUBTITLE) {
2163 7 ret = avcodec_decode_subtitle2(avctx, &subtitle,
2164 &got_picture, pkt);
2165
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 5 times.
7 if (got_picture)
2166 2 avsubtitle_free(&subtitle);
2167
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 if (ret >= 0)
2168 7 pkt_to_send = 0;
2169 }
2170
1/2
✓ Branch 0 taken 8157 times.
✗ Branch 1 not taken.
8157 if (ret >= 0) {
2171
2/2
✓ Branch 0 taken 3181 times.
✓ Branch 1 taken 4976 times.
8157 if (got_picture)
2172 3181 sti->nb_decoded_frames++;
2173 8157 ret = got_picture;
2174 }
2175 }
2176
2177 199492 fail:
2178
2/2
✓ Branch 0 taken 111526 times.
✓ Branch 1 taken 88317 times.
199843 if (do_skip_frame) {
2179 111526 avctx->skip_frame = skip_frame;
2180 }
2181
2182 199843 av_frame_free(&frame);
2183 199843 return ret;
2184 }
2185
2186 57 static int chapter_start_cmp(const void *p1, const void *p2)
2187 {
2188 57 const AVChapter *const ch1 = *(AVChapter**)p1;
2189 57 const AVChapter *const ch2 = *(AVChapter**)p2;
2190 57 int delta = av_compare_ts(ch1->start, ch1->time_base, ch2->start, ch2->time_base);
2191
1/2
✓ Branch 0 taken 57 times.
✗ Branch 1 not taken.
57 if (delta)
2192 57 return delta;
2193 return FFDIFFSIGN(ch1->id, ch2->id);
2194 }
2195
2196 7713 static int compute_chapters_end(AVFormatContext *s)
2197 {
2198 7713 int64_t max_time = 0;
2199 AVChapter **timetable;
2200
2201
2/2
✓ Branch 0 taken 7687 times.
✓ Branch 1 taken 26 times.
7713 if (!s->nb_chapters)
2202 7687 return 0;
2203
2204
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)
2205 26 max_time = s->duration +
2206
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 3 times.
26 ((s->start_time == AV_NOPTS_VALUE) ? 0 : s->start_time);
2207
2208 26 timetable = av_memdup(s->chapters, s->nb_chapters * sizeof(*timetable));
2209
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
26 if (!timetable)
2210 return AVERROR(ENOMEM);
2211 26 qsort(timetable, s->nb_chapters, sizeof(*timetable), chapter_start_cmp);
2212
2213
2/2
✓ Branch 0 taken 71 times.
✓ Branch 1 taken 26 times.
97 for (unsigned i = 0; i < s->nb_chapters; i++)
2214
2/2
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 31 times.
71 if (timetable[i]->end == AV_NOPTS_VALUE) {
2215 40 AVChapter *const ch = timetable[i];
2216 40 int64_t end = max_time ? av_rescale_q(max_time, AV_TIME_BASE_Q,
2217 ch->time_base)
2218
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
40 : INT64_MAX;
2219
2220
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 19 times.
40 if (i + 1 < s->nb_chapters) {
2221 21 const AVChapter *const ch1 = timetable[i + 1];
2222 21 int64_t next_start = av_rescale_q(ch1->start, ch1->time_base,
2223 ch->time_base);
2224
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)
2225 21 end = next_start;
2226 }
2227
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;
2228 }
2229 26 av_free(timetable);
2230 26 return 0;
2231 }
2232
2233 19300128 static int get_std_framerate(int i)
2234 {
2235
2/2
✓ Branch 0 taken 17536173 times.
✓ Branch 1 taken 1763955 times.
19300128 if (i < 30*12)
2236 17536173 return (i + 1) * 1001;
2237 1763955 i -= 30*12;
2238
2239
2/2
✓ Branch 0 taken 1342123 times.
✓ Branch 1 taken 421832 times.
1763955 if (i < 30)
2240 1342123 return (i + 31) * 1001 * 12;
2241 421832 i -= 30;
2242
2243
2/2
✓ Branch 0 taken 134249 times.
✓ Branch 1 taken 287583 times.
421832 if (i < 3)
2244 134249 return ((const int[]) { 80, 120, 240})[i] * 1001 * 12;
2245
2246 287583 i -= 3;
2247
2248 287583 return ((const int[]) { 24, 30, 60, 12, 15, 48 })[i] * 1000 * 12;
2249 }
2250
2251 /* Is the time base unreliable?
2252 * This is a heuristic to balance between quick acceptance of the values in
2253 * the headers vs. some extra checks.
2254 * Old DivX and Xvid often have nonsense timebases like 1fps or 2fps.
2255 * MPEG-2 commonly misuses field repeat flags to store different framerates.
2256 * And there are "variable" fps files this needs to detect as well. */
2257 211192 static int tb_unreliable(AVFormatContext *ic, AVStream *st)
2258 {
2259 211192 FFStream *const sti = ffstream(st);
2260 211192 const AVCodecDescriptor *desc = sti->codec_desc;
2261 211192 AVCodecContext *c = sti->avctx;
2262
4/4
✓ Branch 0 taken 210726 times.
✓ Branch 1 taken 466 times.
✓ Branch 2 taken 21055 times.
✓ Branch 3 taken 189671 times.
211192 AVRational mul = (AVRational){ desc && (desc->props & AV_CODEC_PROP_FIELDS) ? 2 : 1, 1 };
2263 211192 AVRational time_base = c->framerate.num ? av_inv_q(av_mul_q(c->framerate, mul))
2264 /* NOHEADER check added to not break existing behavior */
2265
2/2
✓ Branch 0 taken 17712 times.
✓ Branch 1 taken 193480 times.
211192 : (((ic->ctx_flags & AVFMTCTX_NOHEADER) ||
2266
2/2
✓ Branch 0 taken 44625 times.
✓ Branch 1 taken 30749 times.
75374 st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) ? (AVRational){0, 1}
2267
2/2
✓ Branch 0 taken 75374 times.
✓ Branch 1 taken 118106 times.
193480 : st->time_base);
2268
2269
2/2
✓ Branch 0 taken 24138 times.
✓ Branch 1 taken 187054 times.
211192 if (time_base.den >= 101LL * time_base.num ||
2270
2/2
✓ Branch 0 taken 23703 times.
✓ Branch 1 taken 435 times.
24138 time_base.den < 5LL * time_base.num ||
2271 // c->codec_tag == AV_RL32("DIVX") ||
2272 // c->codec_tag == AV_RL32("XVID") ||
2273
2/2
✓ Branch 0 taken 23604 times.
✓ Branch 1 taken 99 times.
23703 c->codec_tag == AV_RL32("mp4v") ||
2274
2/2
✓ Branch 0 taken 14753 times.
✓ Branch 1 taken 8851 times.
23604 c->codec_id == AV_CODEC_ID_MPEG2VIDEO ||
2275
2/2
✓ Branch 0 taken 14223 times.
✓ Branch 1 taken 530 times.
14753 c->codec_id == AV_CODEC_ID_GIF ||
2276
2/2
✓ Branch 0 taken 13518 times.
✓ Branch 1 taken 705 times.
14223 c->codec_id == AV_CODEC_ID_HEVC ||
2277
2/2
✓ Branch 0 taken 3487 times.
✓ Branch 1 taken 10031 times.
13518 c->codec_id == AV_CODEC_ID_H264)
2278 201161 return 1;
2279 10031 return 0;
2280 }
2281
2282 145563 int ff_rfps_add_frame(AVFormatContext *ic, AVStream *st, int64_t ts)
2283 {
2284 145563 FFStream *const sti = ffstream(st);
2285 145563 FFStreamInfo *info = sti->info;
2286 145563 int64_t last = info->last_dts;
2287
2288
6/6
✓ Branch 0 taken 129280 times.
✓ Branch 1 taken 16283 times.
✓ Branch 2 taken 123135 times.
✓ Branch 3 taken 6145 times.
✓ Branch 4 taken 123107 times.
✓ Branch 5 taken 28 times.
145563 if ( ts != AV_NOPTS_VALUE && last != AV_NOPTS_VALUE && ts > last
2289
1/2
✓ Branch 0 taken 123107 times.
✗ Branch 1 not taken.
123107 && ts - (uint64_t)last < INT64_MAX) {
2290
2/2
✓ Branch 1 taken 6533 times.
✓ Branch 2 taken 116574 times.
123107 double dts = (is_relative(ts) ? ts - RELATIVE_TS_BASE : ts) * av_q2d(st->time_base);
2291 123107 int64_t duration = ts - last;
2292
2293
2/2
✓ Branch 0 taken 4012 times.
✓ Branch 1 taken 119095 times.
123107 if (!info->duration_error)
2294 4012 info->duration_error = av_mallocz(sizeof(info->duration_error[0])*2);
2295
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 123107 times.
123107 if (!info->duration_error)
2296 return AVERROR(ENOMEM);
2297
2298 // if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
2299 // av_log(NULL, AV_LOG_ERROR, "%f\n", dts);
2300
2/2
✓ Branch 0 taken 49119693 times.
✓ Branch 1 taken 123107 times.
49242800 for (int i = 0; i < MAX_STD_TIMEBASES; i++) {
2301
2/2
✓ Branch 0 taken 18932654 times.
✓ Branch 1 taken 30187039 times.
49119693 if (info->duration_error[0][1][i] < 1e10) {
2302 18932654 int framerate = get_std_framerate(i);
2303 18932654 double sdts = dts*framerate/(1001*12);
2304
2/2
✓ Branch 0 taken 37865308 times.
✓ Branch 1 taken 18932654 times.
56797962 for (int j = 0; j < 2; j++) {
2305 37865308 int64_t ticks = llrint(sdts+j*0.5);
2306 37865308 double error = sdts - ticks + j*0.5;
2307 37865308 info->duration_error[j][0][i] += error;
2308 37865308 info->duration_error[j][1][i] += error*error;
2309 }
2310 }
2311 }
2312
1/2
✓ Branch 0 taken 123107 times.
✗ Branch 1 not taken.
123107 if (info->rfps_duration_sum <= INT64_MAX - duration) {
2313 123107 info->duration_count++;
2314 123107 info->rfps_duration_sum += duration;
2315 }
2316
2317
2/2
✓ Branch 0 taken 11211 times.
✓ Branch 1 taken 111896 times.
123107 if (info->duration_count % 10 == 0) {
2318 11211 int n = info->duration_count;
2319
2/2
✓ Branch 0 taken 4473189 times.
✓ Branch 1 taken 11211 times.
4484400 for (int i = 0; i < MAX_STD_TIMEBASES; i++) {
2320
2/2
✓ Branch 0 taken 1815168 times.
✓ Branch 1 taken 2658021 times.
4473189 if (info->duration_error[0][1][i] < 1e10) {
2321 1815168 double a0 = info->duration_error[0][0][i] / n;
2322 1815168 double error0 = info->duration_error[0][1][i] / n - a0*a0;
2323 1815168 double a1 = info->duration_error[1][0][i] / n;
2324 1815168 double error1 = info->duration_error[1][1][i] / n - a1*a1;
2325
4/4
✓ Branch 0 taken 1486544 times.
✓ Branch 1 taken 328624 times.
✓ Branch 2 taken 1387437 times.
✓ Branch 3 taken 99107 times.
1815168 if (error0 > 0.04 && error1 > 0.04) {
2326 1387437 info->duration_error[0][1][i] = 2e10;
2327 1387437 info->duration_error[1][1][i] = 2e10;
2328 }
2329 }
2330 }
2331 }
2332
2333 // ignore the first 4 values, they might have some random jitter
2334
3/4
✓ Branch 0 taken 111160 times.
✓ Branch 1 taken 11947 times.
✓ Branch 4 taken 111160 times.
✗ Branch 5 not taken.
123107 if (info->duration_count > 3 && is_relative(ts) == is_relative(last))
2335 111160 info->duration_gcd = av_gcd(info->duration_gcd, duration);
2336 }
2337
2/2
✓ Branch 0 taken 129280 times.
✓ Branch 1 taken 16283 times.
145563 if (ts != AV_NOPTS_VALUE)
2338 129280 info->last_dts = ts;
2339
2340 145563 return 0;
2341 }
2342
2343 8216 void ff_rfps_calculate(AVFormatContext *ic)
2344 {
2345
2/2
✓ Branch 0 taken 9223 times.
✓ Branch 1 taken 8216 times.
17439 for (unsigned i = 0; i < ic->nb_streams; i++) {
2346 9223 AVStream *const st = ic->streams[i];
2347 9223 FFStream *const sti = ffstream(st);
2348
2349
2/2
✓ Branch 0 taken 2485 times.
✓ Branch 1 taken 6738 times.
9223 if (st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO)
2350 2485 continue;
2351 // the check for tb_unreliable() is not completely correct, since this is not about handling
2352 // an unreliable/inexact time base, but a time base that is finer than necessary, as e.g.
2353 // ipmovie.c produces.
2354
8/8
✓ Branch 1 taken 5340 times.
✓ Branch 2 taken 1398 times.
✓ Branch 3 taken 3482 times.
✓ Branch 4 taken 1858 times.
✓ Branch 5 taken 296 times.
✓ Branch 6 taken 3186 times.
✓ Branch 7 taken 145 times.
✓ Branch 8 taken 151 times.
6738 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 &&
2355
1/2
✓ Branch 0 taken 145 times.
✗ Branch 1 not taken.
145 sti->info->duration_gcd < INT64_MAX / st->time_base.num)
2356 145 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);
2357
4/4
✓ Branch 0 taken 3974 times.
✓ Branch 1 taken 2764 times.
✓ Branch 2 taken 408 times.
✓ Branch 3 taken 3566 times.
6738 if (sti->info->duration_count > 1 && !st->r_frame_rate.num
2358
2/2
✓ Branch 1 taken 303 times.
✓ Branch 2 taken 105 times.
408 && tb_unreliable(ic, st)) {
2359 303 int num = 0;
2360 303 double best_error = 0.01;
2361
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);
2362
2363
2/2
✓ Branch 0 taken 120897 times.
✓ Branch 1 taken 303 times.
121200 for (int j = 0; j < MAX_STD_TIMEBASES; j++) {
2364
2/2
✓ Branch 0 taken 89775 times.
✓ Branch 1 taken 31122 times.
120897 if (sti->info->codec_info_duration &&
2365
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))
2366 9919 continue;
2367
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)
2368 858 continue;
2369
2370
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))
2371 51873 continue;
2372
2373
2/2
✓ Branch 0 taken 116494 times.
✓ Branch 1 taken 58247 times.
174741 for (int k = 0; k < 2; k++) {
2374 116494 int n = sti->info->duration_count;
2375 116494 double a = sti->info->duration_error[k][0][j] / n;
2376 116494 double error = sti->info->duration_error[k][1][j]/n - a*a;
2377
2378
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) {
2379 4434 best_error= error;
2380 4434 num = get_std_framerate(j);
2381 }
2382
2/2
✓ Branch 0 taken 23894 times.
✓ Branch 1 taken 92600 times.
116494 if (error < 0.02)
2383 23894 av_log(ic, AV_LOG_DEBUG, "rfps: %f %f\n", get_std_framerate(j) / 12.0/1001, error);
2384 }
2385 }
2386 // do not increase frame rate by more than 1 % in order to match a standard rate.
2387
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)))
2388 295 av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den, num, 12*1001, INT_MAX);
2389 }
2390
2/2
✓ Branch 0 taken 1096 times.
✓ Branch 1 taken 5642 times.
6738 if ( !st->avg_frame_rate.num
2391
4/4
✓ Branch 0 taken 285 times.
✓ Branch 1 taken 811 times.
✓ Branch 2 taken 281 times.
✓ Branch 3 taken 4 times.
1096 && st->r_frame_rate.num && sti->info->rfps_duration_sum
2392
2/2
✓ Branch 0 taken 73 times.
✓ Branch 1 taken 208 times.
281 && sti->info->codec_info_duration <= 0
2393
2/2
✓ Branch 0 taken 70 times.
✓ Branch 1 taken 3 times.
73 && sti->info->duration_count > 2
2394
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
2395 ) {
2396 55 av_log(ic, AV_LOG_DEBUG, "Setting avg frame rate based on r frame rate\n");
2397 55 st->avg_frame_rate = st->r_frame_rate;
2398 }
2399
2400 6738 av_freep(&sti->info->duration_error);
2401 6738 sti->info->last_dts = AV_NOPTS_VALUE;
2402 6738 sti->info->duration_count = 0;
2403 6738 sti->info->rfps_duration_sum = 0;
2404 }
2405 8216 }
2406
2407 9167 static int extract_extradata_check(AVStream *st)
2408 {
2409 9167 const AVBitStreamFilter *const f = av_bsf_get_by_name("extract_extradata");
2410
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9167 times.
9167 if (!f)
2411 return 0;
2412
2413
1/2
✓ Branch 0 taken 9167 times.
✗ Branch 1 not taken.
9167 if (f->codec_ids) {
2414 const enum AVCodecID *ids;
2415
2/2
✓ Branch 0 taken 97181 times.
✓ Branch 1 taken 8270 times.
105451 for (ids = f->codec_ids; *ids != AV_CODEC_ID_NONE; ids++)
2416
2/2
✓ Branch 0 taken 897 times.
✓ Branch 1 taken 96284 times.
97181 if (*ids == st->codecpar->codec_id)
2417 897 return 1;
2418 }
2419
2420 8270 return 0;
2421 }
2422
2423 7216 static int extract_extradata_init(AVStream *st)
2424 {
2425 7216 FFStream *const sti = ffstream(st);
2426 const AVBitStreamFilter *f;
2427 int ret;
2428
2429 7216 f = av_bsf_get_by_name("extract_extradata");
2430
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7216 times.
7216 if (!f)
2431 goto finish;
2432
2433 /* check that the codec id is supported */
2434 7216 ret = extract_extradata_check(st);
2435
2/2
✓ Branch 0 taken 6429 times.
✓ Branch 1 taken 787 times.
7216 if (!ret)
2436 6429 goto finish;
2437
2438 787 av_bsf_free(&sti->extract_extradata.bsf);
2439 787 ret = av_bsf_alloc(f, &sti->extract_extradata.bsf);
2440
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 787 times.
787 if (ret < 0)
2441 return ret;
2442
2443 787 ret = avcodec_parameters_copy(sti->extract_extradata.bsf->par_in,
2444 787 st->codecpar);
2445
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 787 times.
787 if (ret < 0)
2446 goto fail;
2447
2448 787 sti->extract_extradata.bsf->time_base_in = st->time_base;
2449
2450 787 ret = av_bsf_init(sti->extract_extradata.bsf);
2451
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 787 times.
787 if (ret < 0)
2452 goto fail;
2453
2454 787 finish:
2455 7216 sti->extract_extradata.inited = 1;
2456
2457 7216 return 0;
2458 fail:
2459 av_bsf_free(&sti->extract_extradata.bsf);
2460 return ret;
2461 }
2462
2463 164121 static int extract_extradata(FFFormatContext *si, AVStream *st, const AVPacket *pkt)
2464 {
2465 164121 FFStream *const sti = ffstream(st);
2466 164121 AVPacket *const pkt_ref = si->parse_pkt;
2467 int ret;
2468
2469
2/2
✓ Branch 0 taken 7216 times.
✓ Branch 1 taken 156905 times.
164121 if (!sti->extract_extradata.inited) {
2470 7216 ret = extract_extradata_init(st);
2471
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7216 times.
7216 if (ret < 0)
2472 return ret;
2473 }
2474
2475
3/4
✓ Branch 0 taken 164121 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 162949 times.
✓ Branch 3 taken 1172 times.
164121 if (sti->extract_extradata.inited && !sti->extract_extradata.bsf)
2476 162949 return 0;
2477
2478 1172 ret = av_packet_ref(pkt_ref, pkt);
2479
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1172 times.
1172 if (ret < 0)
2480 return ret;
2481
2482 1172 ret = av_bsf_send_packet(sti->extract_extradata.bsf, pkt_ref);
2483
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1172 times.
1172 if (ret < 0) {
2484 av_packet_unref(pkt_ref);
2485 return ret;
2486 }
2487
2488
4/4
✓ Branch 0 taken 2344 times.
✓ Branch 1 taken 384 times.
✓ Branch 2 taken 1556 times.
✓ Branch 3 taken 788 times.
2728 while (ret >= 0 && !sti->avctx->extradata) {
2489 1556 ret = av_bsf_receive_packet(sti->extract_extradata.bsf, pkt_ref);
2490
2/2
✓ Branch 0 taken 384 times.
✓ Branch 1 taken 1172 times.
1556 if (ret < 0) {
2491
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)
2492 return ret;
2493 384 continue;
2494 }
2495
2496
2/2
✓ Branch 0 taken 1035 times.
✓ Branch 1 taken 384 times.
1419 for (int i = 0; i < pkt_ref->side_data_elems; i++) {
2497 1035 AVPacketSideData *const side_data = &pkt_ref->side_data[i];
2498
2/2
✓ Branch 0 taken 788 times.
✓ Branch 1 taken 247 times.
1035 if (side_data->type == AV_PKT_DATA_NEW_EXTRADATA) {
2499 788 sti->avctx->extradata = side_data->data;
2500 788 sti->avctx->extradata_size = side_data->size;
2501 788 side_data->data = NULL;
2502 788 side_data->size = 0;
2503 788 break;
2504 }
2505 }
2506 1172 av_packet_unref(pkt_ref);
2507 }
2508
2509 1172 return 0;
2510 }
2511
2512 7713 int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
2513 {
2514 7713 FFFormatContext *const si = ffformatcontext(ic);
2515 7713 int count = 0, ret = 0, err;
2516 int64_t read_size;
2517 7713 AVPacket *pkt1 = si->pkt;
2518 7713 int64_t old_offset = avio_tell(ic->pb);
2519 // new streams might appear, no options for those
2520 7713 int orig_nb_streams = ic->nb_streams;
2521 int flush_codecs;
2522 7713 int64_t max_analyze_duration = ic->max_analyze_duration;
2523 int64_t max_stream_analyze_duration;
2524 int64_t max_subtitle_analyze_duration;
2525 7713 int64_t probesize = ic->probesize;
2526 7713 int eof_reached = 0;
2527
2528 7713 flush_codecs = probesize > 0;
2529
2530 7713 av_opt_set_int(ic, "skip_clear", 1, AV_OPT_SEARCH_CHILDREN);
2531
2532 7713 max_stream_analyze_duration = max_analyze_duration;
2533 7713 max_subtitle_analyze_duration = max_analyze_duration;
2534
2/2
✓ Branch 0 taken 7712 times.
✓ Branch 1 taken 1 times.
7713 if (!max_analyze_duration) {
2535 7712 max_stream_analyze_duration =
2536 7712 max_analyze_duration = 5*AV_TIME_BASE;
2537 7712 max_subtitle_analyze_duration = 30*AV_TIME_BASE;
2538
2/2
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 7674 times.
7712 if (!strcmp(ic->iformat->name, "flv"))
2539 38 max_stream_analyze_duration = 90*AV_TIME_BASE;
2540
4/4
✓ Branch 0 taken 7687 times.
✓ Branch 1 taken 25 times.
✓ Branch 2 taken 45 times.
✓ Branch 3 taken 7642 times.
7712 if (!strcmp(ic->iformat->name, "mpeg") || !strcmp(ic->iformat->name, "mpegts"))
2541 70 max_stream_analyze_duration = 7*AV_TIME_BASE;
2542 }
2543
2544
2/2
✓ Branch 0 taken 4555 times.
✓ Branch 1 taken 3158 times.
7713 if (ic->pb) {
2545 4555 FFIOContext *const ctx = ffiocontext(ic->pb);
2546 4555 av_log(ic, AV_LOG_DEBUG, "Before avformat_find_stream_info() pos: %"PRId64" bytes read:%"PRId64" seeks:%d nb_streams:%d\n",
2547 avio_tell(ic->pb), ctx->bytes_read, ctx->seek_count, ic->nb_streams);
2548 }
2549
2550
2/2
✓ Branch 0 taken 8351 times.
✓ Branch 1 taken 7713 times.
16064 for (unsigned i = 0; i < ic->nb_streams; i++) {
2551 const AVCodec *codec;
2552 8351 AVDictionary *thread_opt = NULL;
2553 8351 AVStream *const st = ic->streams[i];
2554 8351 FFStream *const sti = ffstream(st);
2555 8351 AVCodecContext *const avctx = sti->avctx;
2556
2557 /* check if the caller has overridden the codec id */
2558 // only for the split stuff
2559
4/6
✓ Branch 0 taken 8351 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8351 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 7844 times.
✓ Branch 5 taken 507 times.
8351 if (!sti->parser && !(ic->flags & AVFMT_FLAG_NOPARSE) && sti->request_probe <= 0) {
2560 7844 sti->parser = av_parser_init(st->codecpar->codec_id);
2561
2/2
✓ Branch 0 taken 5483 times.
✓ Branch 1 taken 2361 times.
7844 if (sti->parser) {
2562
2/2
✓ Branch 0 taken 759 times.
✓ Branch 1 taken 4724 times.
5483 if (sti->need_parsing == AVSTREAM_PARSE_HEADERS) {
2563 759 sti->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES;
2564
2/2
✓ Branch 0 taken 823 times.
✓ Branch 1 taken 3901 times.
4724 } else if (sti->need_parsing == AVSTREAM_PARSE_FULL_RAW) {
2565 823 sti->parser->flags |= PARSER_FLAG_USE_CODEC_TS;
2566 }
2567
2/2
✓ Branch 0 taken 663 times.
✓ Branch 1 taken 1698 times.
2361 } else if (sti->need_parsing) {
2568 663 av_log(ic, AV_LOG_VERBOSE, "parser not found for codec "
2569 "%s, packets or times may be invalid.\n",
2570 663 avcodec_get_name(st->codecpar->codec_id));
2571 }
2572 }
2573
2574 8351 ret = avcodec_parameters_to_context(avctx, st->codecpar);
2575
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8351 times.
8351 if (ret < 0)
2576 goto find_stream_info_err;
2577
2/2
✓ Branch 0 taken 7844 times.
✓ Branch 1 taken 507 times.
8351 if (sti->request_probe <= 0)
2578 7844 sti->avctx_inited = 1;
2579
2580 8351 codec = find_probe_decoder(ic, st, st->codecpar->codec_id);
2581
2582 /* Force thread count to 1 since the H.264 decoder will not extract
2583 * SPS and PPS to extradata during multi-threaded decoding. */
2584
2/2
✓ Branch 0 taken 8063 times.
✓ Branch 1 taken 288 times.
8351 av_dict_set(options ? &options[i] : &thread_opt, "threads", "1", 0);
2585 /* Force lowres to 0. The decoder might reduce the video size by the
2586 * lowres factor, and we don't want that propagated to the stream's
2587 * codecpar */
2588
2/2
✓ Branch 0 taken 8063 times.
✓ Branch 1 taken 288 times.
8351 av_dict_set(options ? &options[i] : &thread_opt, "lowres", "0", 0);
2589
2590
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8351 times.
8351 if (ic->codec_whitelist)
2591 av_dict_set(options ? &options[i] : &thread_opt, "codec_whitelist", ic->codec_whitelist, 0);
2592
2593 // Try to just open decoders, in case this is enough to get parameters.
2594 // Also ensure that subtitle_header is properly set.
2595
4/4
✓ Branch 1 taken 7520 times.
✓ Branch 2 taken 831 times.
✓ Branch 3 taken 506 times.
✓ Branch 4 taken 7014 times.
8351 if (!has_codec_parameters(st, NULL) && sti->request_probe <= 0 ||
2596
2/2
✓ Branch 0 taken 83 times.
✓ Branch 1 taken 1254 times.
1337 st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) {
2597
3/4
✓ Branch 0 taken 7078 times.
✓ Branch 1 taken 19 times.
✓ Branch 2 taken 7078 times.
✗ Branch 3 not taken.
7097 if (codec && !avctx->codec)
2598
4/4
✓ Branch 0 taken 6794 times.
✓ Branch 1 taken 284 times.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 7072 times.
7078 if (avcodec_open2(avctx, codec, options ? &options[i] : &thread_opt) < 0)
2599 6 av_log(ic, AV_LOG_WARNING,
2600 "Failed to open codec in %s\n", __func__);
2601 }
2602
2/2
✓ Branch 0 taken 288 times.
✓ Branch 1 taken 8063 times.
8351 if (!options)
2603 288 av_dict_free(&thread_opt);
2604 }
2605
2606 7713 read_size = 0;
2607 195186 for (;;) {
2608 const AVPacket *pkt;
2609 AVStream *st;
2610 FFStream *sti;
2611 AVCodecContext *avctx;
2612 int analyzed_all_streams;
2613 unsigned i;
2614
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 202899 times.
202899 if (ff_check_interrupt(&ic->interrupt_callback)) {
2615 ret = AVERROR_EXIT;
2616 av_log(ic, AV_LOG_DEBUG, "interrupted\n");
2617 break;
2618 }
2619
2620 /* check if one codec still needs to be handled */
2621
2/2
✓ Branch 0 taken 211531 times.
✓ Branch 1 taken 113561 times.
325092 for (i = 0; i < ic->nb_streams; i++) {
2622 211531 AVStream *const st = ic->streams[i];
2623 211531 FFStream *const sti = ffstream(st);
2624 211531 int fps_analyze_framecount = 20;
2625 int count;
2626
2627
2/2
✓ Branch 1 taken 7485 times.
✓ Branch 2 taken 204046 times.
211531 if (!has_codec_parameters(st, NULL))
2628 7485 break;
2629 /* If the timebase is coarse (like the usual millisecond precision
2630 * of mkv), we need to analyze more frames to reliably arrive at
2631 * the correct fps. */
2632
2/2
✓ Branch 1 taken 124302 times.
✓ Branch 2 taken 79744 times.
204046 if (av_q2d(st->time_base) > 0.0005)
2633 124302 fps_analyze_framecount *= 2;
2634
2/2
✓ Branch 1 taken 8528 times.
✓ Branch 2 taken 195518 times.
204046 if (!tb_unreliable(ic, st))
2635 8528 fps_analyze_framecount = 0;
2636
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 204046 times.
204046 if (ic->fps_probe_size >= 0)
2637 fps_analyze_framecount = ic->fps_probe_size;
2638
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 204002 times.
204046 if (st->disposition & AV_DISPOSITION_ATTACHED_PIC)
2639 44 fps_analyze_framecount = 0;
2640 /* variable fps and no guess at the real fps */
2641
2/2
✓ Branch 0 taken 19490 times.
✓ Branch 1 taken 184556 times.
204046 count = (ic->iformat->flags & AVFMT_NOTIMESTAMPS) ?
2642 19490 sti->info->codec_info_duration_fields/2 :
2643 184556 sti->info->duration_count;
2644
4/4
✓ Branch 0 taken 105030 times.
✓ Branch 1 taken 99016 times.
✓ Branch 2 taken 223 times.
✓ Branch 3 taken 104807 times.
204046 if (!(st->r_frame_rate.num && st->avg_frame_rate.num) &&
2645
2/2
✓ Branch 0 taken 43189 times.
✓ Branch 1 taken 56050 times.
99239 st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
2646
2/2
✓ Branch 0 taken 25021 times.
✓ Branch 1 taken 18168 times.
43189 if (count < fps_analyze_framecount)
2647 25021 break;
2648 }
2649 // Look at the first 3 frames if there is evidence of frame delay
2650 // but the decoder delay is not set.
2651
6/6
✓ Branch 0 taken 2899 times.
✓ Branch 1 taken 176126 times.
✓ Branch 2 taken 130 times.
✓ Branch 3 taken 2769 times.
✓ Branch 4 taken 12 times.
✓ Branch 5 taken 118 times.
179025 if (sti->info->frame_delay_evidence && count < 2 && sti->avctx->has_b_frames == 0)
2652 12 break;
2653
2/2
✓ Branch 0 taken 158574 times.
✓ Branch 1 taken 20439 times.
179013 if (!sti->avctx->extradata &&
2654
6/6
✓ Branch 0 taken 156733 times.
✓ Branch 1 taken 1841 times.
✓ Branch 2 taken 110 times.
✓ Branch 3 taken 156623 times.
✓ Branch 4 taken 110 times.
✓ Branch 5 taken 1841 times.
160525 (!sti->extract_extradata.inited || sti->extract_extradata.bsf) &&
2655 1951 extract_extradata_check(st))
2656 110 break;
2657
2/2
✓ Branch 0 taken 59879 times.
✓ Branch 1 taken 119024 times.
178903 if (sti->first_dts == AV_NOPTS_VALUE &&
2658
6/6
✓ Branch 0 taken 9464 times.
✓ Branch 1 taken 50415 times.
✓ Branch 2 taken 9378 times.
✓ Branch 3 taken 86 times.
✓ Branch 4 taken 57500 times.
✓ Branch 5 taken 2293 times.
119672 (!(ic->iformat->flags & AVFMT_NOTIMESTAMPS) || sti->need_parsing == AVSTREAM_PARSE_FULL_RAW) &&
2659
2/2
✓ Branch 0 taken 59749 times.
✓ Branch 1 taken 44 times.
59793 sti->codec_info_nb_frames < ((st->disposition & AV_DISPOSITION_ATTACHED_PIC) ? 1 : ic->max_ts_probe) &&
2660
2/2
✓ Branch 0 taken 41608 times.
✓ Branch 1 taken 15892 times.
57500 (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ||
2661
2/2
✓ Branch 0 taken 790 times.
✓ Branch 1 taken 40818 times.
41608 st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO))
2662 break;
2663 }
2664 202899 analyzed_all_streams = 0;
2665
4/4
✓ Branch 0 taken 113561 times.
✓ Branch 1 taken 89338 times.
✓ Branch 2 taken 113522 times.
✓ Branch 3 taken 39 times.
202899 if (i == ic->nb_streams && !si->missing_streams) {
2666 113522 analyzed_all_streams = 1;
2667 /* NOTE: If the format has no header, then we need to read some
2668 * packets to get most of the streams, so we cannot stop here. */
2669
2/2
✓ Branch 0 taken 3341 times.
✓ Branch 1 taken 110181 times.
113522 if (!(ic->ctx_flags & AVFMTCTX_NOHEADER)) {
2670 /* If we found the info for all the codecs, we can stop. */
2671 3341 ret = count;
2672 3341 av_log(ic, AV_LOG_DEBUG, "All info found\n");
2673 3341 flush_codecs = 0;
2674 3341 break;
2675 }
2676 }
2677 /* We did not get all the codec info, but we read too much data. */
2678
2/2
✓ Branch 0 taken 3079 times.
✓ Branch 1 taken 196479 times.
199558 if (read_size >= probesize) {
2679 3079 ret = count;
2680 3079 av_log(ic, AV_LOG_DEBUG,
2681 "Probe buffer size limit of %"PRId64" bytes reached\n", probesize);
2682
2/2
✓ Branch 0 taken 3093 times.
✓ Branch 1 taken 3079 times.
6172 for (unsigned i = 0; i < ic->nb_streams; i++) {
2683 3093 AVStream *const st = ic->streams[i];
2684 3093 FFStream *const sti = ffstream(st);
2685
2/2
✓ Branch 0 taken 55 times.
✓ Branch 1 taken 3038 times.
3093 if (!st->r_frame_rate.num &&
2686
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 40 times.
55 sti->info->duration_count <= 1 &&
2687
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 14 times.
15 st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
2688
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 strcmp(ic->iformat->name, "image2"))
2689 1 av_log(ic, AV_LOG_WARNING,
2690 "Stream #%d: not enough frames to estimate rate; "
2691 "consider increasing probesize\n", i);
2692 }
2693 3079 break;
2694 }
2695
2696 /* NOTE: A new stream can be added there if no header in file
2697 * (AVFMTCTX_NOHEADER). */
2698 196479 ret = read_frame_internal(ic, pkt1);
2699
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 196479 times.
196479 if (ret == AVERROR(EAGAIN))
2700 continue;
2701
2702
2/2
✓ Branch 0 taken 1199 times.
✓ Branch 1 taken 195280 times.
196479 if (ret < 0) {
2703 /* EOF or error*/
2704 1199 eof_reached = 1;
2705 1199 break;
2706 }
2707
2708
1/2
✓ Branch 0 taken 195280 times.
✗ Branch 1 not taken.
195280 if (!(ic->flags & AVFMT_FLAG_NOBUFFER)) {
2709 195280 ret = avpriv_packet_list_put(&si->packet_buffer,
2710 pkt1, NULL, 0);
2711
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 195280 times.
195280 if (ret < 0)
2712 goto unref_then_goto_end;
2713
2714 195280 pkt = &si->packet_buffer.tail->pkt;
2715 } else {
2716 pkt = pkt1;
2717 }
2718
2719 195280 st = ic->streams[pkt->stream_index];
2720 195280 sti = ffstream(st);
2721
2/2
✓ Branch 0 taken 195224 times.
✓ Branch 1 taken 56 times.
195280 if (!(st->disposition & AV_DISPOSITION_ATTACHED_PIC))
2722 195224 read_size += pkt->size;
2723
2724 195280 avctx = sti->avctx;
2725
2/2
✓ Branch 0 taken 679 times.
✓ Branch 1 taken 194601 times.
195280 if (!sti->avctx_inited) {
2726 679 ret = avcodec_parameters_to_context(avctx, st->codecpar);
2727
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 679 times.
679 if (ret < 0)
2728 goto unref_then_goto_end;
2729 679 sti->avctx_inited = 1;
2730 }
2731
2732
4/4
✓ Branch 0 taken 178639 times.
✓ Branch 1 taken 16641 times.
✓ Branch 2 taken 166015 times.
✓ Branch 3 taken 12624 times.
195280 if (pkt->dts != AV_NOPTS_VALUE && sti->codec_info_nb_frames > 1) {
2733 /* check for non-increasing dts */
2734
2/2
✓ Branch 0 taken 161042 times.
✓ Branch 1 taken 4973 times.
166015 if (sti->info->fps_last_dts != AV_NOPTS_VALUE &&
2735
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 161019 times.
161042 sti->info->fps_last_dts >= pkt->dts) {
2736 23 av_log(ic, AV_LOG_DEBUG,
2737 "Non-increasing DTS in stream %d: packet %d with DTS "
2738 "%"PRId64", packet %d with DTS %"PRId64"\n",
2739 23 st->index, sti->info->fps_last_dts_idx,
2740 23 sti->info->fps_last_dts, sti->codec_info_nb_frames,
2741 23 pkt->dts);
2742 23 sti->info->fps_first_dts =
2743 23 sti->info->fps_last_dts = AV_NOPTS_VALUE;
2744 }
2745 /* Check for a discontinuity in dts. If the difference in dts
2746 * is more than 1000 times the average packet duration in the
2747 * sequence, we treat it as a discontinuity. */
2748
2/2
✓ Branch 0 taken 161019 times.
✓ Branch 1 taken 4996 times.
166015 if (sti->info->fps_last_dts != AV_NOPTS_VALUE &&
2749
2/2
✓ Branch 0 taken 156075 times.
✓ Branch 1 taken 4944 times.
161019 sti->info->fps_last_dts_idx > sti->info->fps_first_dts_idx &&
2750 156075 (pkt->dts - (uint64_t)sti->info->fps_last_dts) / 1000 >
2751 156075 (sti->info->fps_last_dts - (uint64_t)sti->info->fps_first_dts) /
2752
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 156075 times.
156075 (sti->info->fps_last_dts_idx - sti->info->fps_first_dts_idx)) {
2753 av_log(ic, AV_LOG_WARNING,
2754 "DTS discontinuity in stream %d: packet %d with DTS "
2755 "%"PRId64", packet %d with DTS %"PRId64"\n",
2756 st->index, sti->info->fps_last_dts_idx,
2757 sti->info->fps_last_dts, sti->codec_info_nb_frames,
2758 pkt->dts);
2759 sti->info->fps_first_dts =
2760 sti->info->fps_last_dts = AV_NOPTS_VALUE;
2761 }
2762
2763 /* update stored dts values */
2764
2/2
✓ Branch 0 taken 4996 times.
✓ Branch 1 taken 161019 times.
166015 if (sti->info->fps_first_dts == AV_NOPTS_VALUE) {
2765 4996 sti->info->fps_first_dts = pkt->dts;
2766 4996 sti->info->fps_first_dts_idx = sti->codec_info_nb_frames;
2767 }
2768 166015 sti->info->fps_last_dts = pkt->dts;
2769 166015 sti->info->fps_last_dts_idx = sti->codec_info_nb_frames;
2770 }
2771
2/2
✓ Branch 0 taken 181470 times.
✓ Branch 1 taken 13810 times.
195280 if (sti->codec_info_nb_frames > 1) {
2772 181470 int64_t t = 0;
2773 int64_t limit;
2774
2775
1/2
✓ Branch 0 taken 181470 times.
✗ Branch 1 not taken.
181470 if (st->time_base.den > 0)
2776 181470 t = av_rescale_q(sti->info->codec_info_duration, st->time_base, AV_TIME_BASE_Q);
2777
2/2
✓ Branch 0 taken 116303 times.
✓ Branch 1 taken 65167 times.
181470 if (st->avg_frame_rate.num > 0)
2778
2/2
✓ Branch 1 taken 115776 times.
✓ Branch 2 taken 527 times.
116303 t = FFMAX(t, av_rescale_q(sti->codec_info_nb_frames, av_inv_q(st->avg_frame_rate), AV_TIME_BASE_Q));
2779
2780
2/2
✓ Branch 0 taken 4227 times.
✓ Branch 1 taken 177243 times.
181470 if ( t == 0
2781
2/2
✓ Branch 0 taken 564 times.
✓ Branch 1 taken 3663 times.
4227 && sti->codec_info_nb_frames > 30
2782
2/2
✓ Branch 0 taken 497 times.
✓ Branch 1 taken 67 times.
564 && sti->info->fps_first_dts != AV_NOPTS_VALUE
2783
1/2
✓ Branch 0 taken 497 times.
✗ Branch 1 not taken.
497 && sti->info->fps_last_dts != AV_NOPTS_VALUE) {
2784 497 int64_t dur = av_sat_sub64(sti->info->fps_last_dts, sti->info->fps_first_dts);
2785
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));
2786 }
2787
2788
2/2
✓ Branch 0 taken 103751 times.
✓ Branch 1 taken 77719 times.
181470 if (analyzed_all_streams) limit = max_analyze_duration;
2789
2/2
✓ Branch 0 taken 91 times.
✓ Branch 1 taken 77628 times.
77719 else if (avctx->codec_type == AVMEDIA_TYPE_SUBTITLE) limit = max_subtitle_analyze_duration;
2790 77628 else limit = max_stream_analyze_duration;
2791
2792
2/2
✓ Branch 0 taken 94 times.
✓ Branch 1 taken 181376 times.
181470 if (t >= limit) {
2793 94 av_log(ic, AV_LOG_VERBOSE, "max_analyze_duration %"PRId64" reached at %"PRId64" microseconds st:%d\n",
2794 limit,
2795 94 t, pkt->stream_index);
2796
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 94 times.
94 if (ic->flags & AVFMT_FLAG_NOBUFFER)
2797 av_packet_unref(pkt1);
2798 94 break;
2799 }
2800
3/4
✓ Branch 0 taken 177614 times.
✓ Branch 1 taken 3762 times.
✓ Branch 2 taken 177614 times.
✗ Branch 3 not taken.
181376 if (pkt->duration > 0 && pkt->duration < INT64_MAX - sti->info->codec_info_duration) {
2801
4/4
✓ Branch 0 taken 177574 times.
✓ Branch 1 taken 40 times.
✓ Branch 2 taken 14255 times.
✓ Branch 3 taken 163319 times.
177614 const int fields = sti->codec_desc && (sti->codec_desc->props & AV_CODEC_PROP_FIELDS);
2802
1/8
✗ Branch 0 not taken.
✓ Branch 1 taken 177614 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.
177614 if (avctx->codec_type == AVMEDIA_TYPE_SUBTITLE && pkt->pts != AV_NOPTS_VALUE && st->start_time != AV_NOPTS_VALUE && pkt->pts >= st->start_time
2803 && (uint64_t)pkt->pts - st->start_time < INT64_MAX
2804 ) {
2805 sti->info->codec_info_duration = FFMIN(pkt->pts - st->start_time, sti->info->codec_info_duration + pkt->duration);
2806 } else
2807 177614 sti->info->codec_info_duration += pkt->duration;
2808
4/4
✓ Branch 0 taken 35795 times.
✓ Branch 1 taken 95337 times.
✓ Branch 2 taken 13883 times.
✓ Branch 3 taken 21912 times.
308746 sti->info->codec_info_duration_fields += sti->parser && sti->need_parsing && fields
2809
2/2
✓ Branch 0 taken 131132 times.
✓ Branch 1 taken 46482 times.
308746 ? sti->parser->repeat_pict + 1 : 2;
2810 }
2811 }
2812
2/2
✓ Branch 0 taken 135182 times.
✓ Branch 1 taken 60004 times.
195186 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
2813 #if FF_API_R_FRAME_RATE
2814 135182 ff_rfps_add_frame(ic, st, pkt->dts);
2815 #endif
2816
6/6
✓ Branch 0 taken 4236 times.
✓ Branch 1 taken 130946 times.
✓ Branch 2 taken 3997 times.
✓ Branch 3 taken 239 times.
✓ Branch 4 taken 1972 times.
✓ Branch 5 taken 2025 times.
135182 if (pkt->dts != pkt->pts && pkt->dts != AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE)
2817 1972 sti->info->frame_delay_evidence = 1;
2818 }
2819
2/2
✓ Branch 0 taken 163967 times.
✓ Branch 1 taken 31219 times.
195186 if (!sti->avctx->extradata) {
2820 163967 ret = extract_extradata(si, st, pkt);
2821
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 163967 times.
163967 if (ret < 0)
2822 goto unref_then_goto_end;
2823 }
2824
2825 /* If still no information, we try to open the codec and to
2826 * decompress the frame. We try to avoid that in most cases as
2827 * it takes longer and uses more memory. For MPEG-4, we need to
2828 * decompress for QuickTime.
2829 *
2830 * If AV_CODEC_CAP_CHANNEL_CONF is set this will force decoding of at
2831 * least one frame of codec data, this makes sure the codec initializes
2832 * the channel configuration and does not only trust the values from
2833 * the container. */
2834
2/2
✓ Branch 0 taken 177653 times.
✓ Branch 1 taken 17533 times.
372839 try_decode_frame(ic, st, pkt,
2835
2/2
✓ Branch 0 taken 78111 times.
✓ Branch 1 taken 99542 times.
177653 (options && i < orig_nb_streams) ? &options[i] : NULL);
2836
2837
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 195186 times.
195186 if (ic->flags & AVFMT_FLAG_NOBUFFER)
2838 av_packet_unref(pkt1);
2839
2840 195186 sti->codec_info_nb_frames++;
2841 195186 count++;
2842 }
2843
2844
2/2
✓ Branch 0 taken 1199 times.
✓ Branch 1 taken 6514 times.
7713 if (eof_reached) {
2845
2/2
✓ Branch 0 taken 1500 times.
✓ Branch 1 taken 1199 times.
2699 for (unsigned stream_index = 0; stream_index < ic->nb_streams; stream_index++) {
2846 1500 AVStream *const st = ic->streams[stream_index];
2847 1500 AVCodecContext *const avctx = ffstream(st)->avctx;
2848
2/2
✓ Branch 1 taken 28 times.
✓ Branch 2 taken 1472 times.
1500 if (!has_codec_parameters(st, NULL)) {
2849 28 const AVCodec *codec = find_probe_decoder(ic, st, st->codecpar->codec_id);
2850
4/4
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 13 times.
28 if (codec && !avctx->codec) {
2851 8 AVDictionary *opts = NULL;
2852
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (ic->codec_whitelist)
2853 av_dict_set(&opts, "codec_whitelist", ic->codec_whitelist, 0);
2854
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)
2855 av_log(ic, AV_LOG_WARNING,
2856 "Failed to open codec in %s\n", __func__);
2857 8 av_dict_free(&opts);
2858 }
2859 }
2860
2861 // EOF already reached while reading the stream above.
2862 // So continue with reoordering DTS with whatever delay we have.
2863
4/4
✓ Branch 0 taken 1482 times.
✓ Branch 1 taken 18 times.
✓ Branch 3 taken 18 times.
✓ Branch 4 taken 1464 times.
1500 if (si->packet_buffer.head && !has_decode_delay_been_guessed(st)) {
2864 18 update_dts_from_pts(ic, stream_index, si->packet_buffer.head);
2865 }
2866 }
2867 }
2868
2869
2/2
✓ Branch 0 taken 4372 times.
✓ Branch 1 taken 3341 times.
7713 if (flush_codecs) {
2870 4372 AVPacket *empty_pkt = si->pkt;
2871 4372 int err = 0;
2872 4372 av_packet_unref(empty_pkt);
2873
2874
2/2
✓ Branch 0 taken 4726 times.
✓ Branch 1 taken 4372 times.
9098 for (unsigned i = 0; i < ic->nb_streams; i++) {
2875 4726 AVStream *const st = ic->streams[i];
2876 4726 FFStream *const sti = ffstream(st);
2877
2878 /* flush the decoders */
2879
2/2
✓ Branch 0 taken 4657 times.
✓ Branch 1 taken 69 times.
4726 if (sti->info->found_decoder == 1) {
2880
2/2
✓ Branch 0 taken 4340 times.
✓ Branch 1 taken 317 times.
8997 err = try_decode_frame(ic, st, empty_pkt,
2881
2/2
✓ Branch 0 taken 4324 times.
✓ Branch 1 taken 16 times.
4340 (options && i < orig_nb_streams)
2882 4324 ? &options[i] : NULL);
2883
2884
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4657 times.
4657 if (err < 0) {
2885 av_log(ic, AV_LOG_INFO,
2886 "decoding for stream %d failed\n", st->index);
2887 }
2888 }
2889 }
2890 }
2891
2892 7713 ff_rfps_calculate(ic);
2893
2894
2/2
✓ Branch 0 taken 8536 times.
✓ Branch 1 taken 7713 times.
16249 for (unsigned i = 0; i < ic->nb_streams; i++) {
2895 8536 AVStream *const st = ic->streams[i];
2896 8536 FFStream *const sti = ffstream(st);
2897 8536 AVCodecContext *const avctx = sti->avctx;
2898
2899
2/2
✓ Branch 0 taken 6385 times.
✓ Branch 1 taken 2151 times.
8536 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
2900
6/6
✓ Branch 0 taken 638 times.
✓ Branch 1 taken 5747 times.
✓ Branch 2 taken 589 times.
✓ Branch 3 taken 49 times.
✓ Branch 4 taken 573 times.
✓ Branch 5 taken 16 times.
6385 if (avctx->codec_id == AV_CODEC_ID_RAWVIDEO && !avctx->codec_tag && !avctx->bits_per_coded_sample) {
2901 573 uint32_t tag= avcodec_pix_fmt_to_codec_tag(avctx->pix_fmt);
2902
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)
2903 566 avctx->codec_tag= tag;
2904 }
2905
2906 /* estimate average framerate if not set by demuxer */
2907
2/2
✓ Branch 0 taken 4113 times.
✓ Branch 1 taken 2272 times.
6385 if (sti->info->codec_info_duration_fields &&
2908
2/2
✓ Branch 0 taken 271 times.
✓ Branch 1 taken 3842 times.
4113 !st->avg_frame_rate.num &&
2909
1/2
✓ Branch 0 taken 271 times.
✗ Branch 1 not taken.
271 sti->info->codec_info_duration) {
2910 271 int best_fps = 0;
2911 271 double best_error = 0.01;
2912 271 AVRational codec_frame_rate = avctx->framerate;
2913
2914
1/2
✓ Branch 0 taken 271 times.
✗ Branch 1 not taken.
271 if (sti->info->codec_info_duration >= INT64_MAX / st->time_base.num / 2||
2915
1/2
✓ Branch 0 taken 271 times.
✗ Branch 1 not taken.
271 sti->info->codec_info_duration_fields >= INT64_MAX / st->time_base.den ||
2916
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 271 times.
271 sti->info->codec_info_duration < 0)
2917 continue;
2918 271 av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den,
2919 271 sti->info->codec_info_duration_fields * (int64_t) st->time_base.den,
2920 271 sti->info->codec_info_duration * 2 * (int64_t) st->time_base.num, 60000);
2921
2922 /* Round guessed framerate to a "standard" framerate if it's
2923 * within 1% of the original estimate. */
2924
2/2
✓ Branch 0 taken 108129 times.
✓ Branch 1 taken 271 times.
108400 for (int j = 0; j < MAX_STD_TIMEBASES; j++) {
2925 108129 AVRational std_fps = { get_std_framerate(j), 12 * 1001 };
2926 108129 double error = fabs(av_q2d(st->avg_frame_rate) /
2927 108129 av_q2d(std_fps) - 1);
2928
2929
2/2
✓ Branch 0 taken 743 times.
✓ Branch 1 taken 107386 times.
108129 if (error < best_error) {
2930 743 best_error = error;
2931 743 best_fps = std_fps.num;
2932 }
2933
2934
2/2
✓ Branch 1 taken 13167 times.
✓ Branch 2 taken 94962 times.
108129 if ((ffifmt(ic->iformat)->flags_internal & FF_INFMT_FLAG_PREFER_CODEC_FRAMERATE) &&
2935
2/4
✓ Branch 0 taken 13167 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13167 times.
✗ Branch 3 not taken.
13167 codec_frame_rate.num > 0 && codec_frame_rate.den > 0) {
2936 13167 error = fabs(av_q2d(codec_frame_rate) /
2937 13167 av_q2d(std_fps) - 1);
2938
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 13157 times.
13167 if (error < best_error) {
2939 10 best_error = error;
2940 10 best_fps = std_fps.num;
2941 }
2942 }
2943 }
2944
2/2
✓ Branch 0 taken 269 times.
✓ Branch 1 taken 2 times.
271 if (best_fps)
2945 269 av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den,
2946 best_fps, 12 * 1001, INT_MAX);
2947 }
2948
2/2
✓ Branch 0 taken 1875 times.
✓ Branch 1 taken 4510 times.
6385 if (!st->r_frame_rate.num) {
2949 1875 const AVCodecDescriptor *desc = sti->codec_desc;
2950
3/4
✓ Branch 0 taken 1875 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 246 times.
✓ Branch 3 taken 1629 times.
1875 AVRational mul = (AVRational){ desc && (desc->props & AV_CODEC_PROP_FIELDS) ? 2 : 1, 1 };
2951 1875 AVRational fr = av_mul_q(avctx->framerate, mul);
2952
2953
5/6
✓ Branch 0 taken 190 times.
✓ Branch 1 taken 1685 times.
✓ Branch 2 taken 190 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 176 times.
✓ Branch 7 taken 14 times.
1875 if (fr.num && fr.den && av_cmp_q(st->time_base, av_inv_q(fr)) <= 0) {
2954 176 st->r_frame_rate = fr;
2955 } else {
2956 1699 st->r_frame_rate.num = st->time_base.den;
2957 1699 st->r_frame_rate.den = st->time_base.num;
2958 }
2959 }
2960 6385 st->codecpar->framerate = avctx->framerate;
2961
3/4
✓ Branch 0 taken 116 times.
✓ Branch 1 taken 6269 times.
✓ Branch 2 taken 116 times.
✗ Branch 3 not taken.
6385 if (sti->display_aspect_ratio.num && sti->display_aspect_ratio.den) {
2962 116 AVRational hw_ratio = { avctx->height, avctx->width };
2963 116 st->sample_aspect_ratio = av_mul_q(sti->display_aspect_ratio,
2964 hw_ratio);
2965 }
2966
2/2
✓ Branch 0 taken 1975 times.
✓ Branch 1 taken 176 times.
2151 } else if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
2967
2/2
✓ Branch 0 taken 648 times.
✓ Branch 1 taken 1327 times.
1975 if (!avctx->bits_per_coded_sample)
2968 648 avctx->bits_per_coded_sample =
2969 648 av_get_bits_per_sample(avctx->codec_id);
2970 // set stream disposition based on audio service type
2971
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 1975 times.
1975 switch (avctx->audio_service_type) {
2972 case AV_AUDIO_SERVICE_TYPE_EFFECTS:
2973 st->disposition = AV_DISPOSITION_CLEAN_EFFECTS;
2974 break;
2975 case AV_AUDIO_SERVICE_TYPE_VISUALLY_IMPAIRED:
2976 st->disposition = AV_DISPOSITION_VISUAL_IMPAIRED;
2977 break;
2978 case AV_AUDIO_SERVICE_TYPE_HEARING_IMPAIRED:
2979 st->disposition = AV_DISPOSITION_HEARING_IMPAIRED;
2980 break;
2981 case AV_AUDIO_SERVICE_TYPE_COMMENTARY:
2982 st->disposition = AV_DISPOSITION_COMMENT;
2983 break;
2984 case AV_AUDIO_SERVICE_TYPE_KARAOKE:
2985 st->disposition = AV_DISPOSITION_KARAOKE;
2986 break;
2987 }
2988 }
2989 }
2990
2991
1/2
✓ Branch 0 taken 7713 times.
✗ Branch 1 not taken.
7713 if (probesize)
2992 7713 estimate_timings(ic, old_offset);
2993
2994 7713 av_opt_set_int(ic, "skip_clear", 0, AV_OPT_SEARCH_CHILDREN);
2995
2996
3/4
✓ Branch 0 taken 6514 times.
✓ Branch 1 taken 1199 times.
✓ Branch 2 taken 6514 times.
✗ Branch 3 not taken.
7713 if (ret >= 0 && ic->nb_streams)
2997 /* We could not have all the codec parameters before EOF. */
2998 6514 ret = -1;
2999
2/2
✓ Branch 0 taken 8536 times.
✓ Branch 1 taken 7713 times.
16249 for (unsigned i = 0; i < ic->nb_streams; i++) {
3000 8536 AVStream *const st = ic->streams[i];
3001 8536 FFStream *const sti = ffstream(st);
3002 const char *errmsg;
3003
3004 /* if no packet was ever seen, update context now for has_codec_parameters */
3005
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 8523 times.
8536 if (!sti->avctx_inited) {
3006
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 5 times.
13 if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
3007
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 st->codecpar->format == AV_SAMPLE_FMT_NONE)
3008 8 st->codecpar->format = sti->avctx->sample_fmt;
3009 13 ret = avcodec_parameters_to_context(sti->avctx, st->codecpar);
3010
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 if (ret < 0)
3011 goto find_stream_info_err;
3012 }
3013
2/2
✓ Branch 1 taken 26 times.
✓ Branch 2 taken 8510 times.
8536 if (!has_codec_parameters(st, &errmsg)) {
3014 char buf[256];
3015 26 avcodec_string(buf, sizeof(buf), sti->avctx, 0);
3016 26 av_log(ic, AV_LOG_WARNING,
3017 "Could not find codec parameters for stream %d (%s): %s\n"
3018 "Consider increasing the value for the 'analyzeduration' (%"PRId64") and 'probesize' (%"PRId64") options\n",
3019 i, buf, errmsg, ic->max_analyze_duration, ic->probesize);
3020 } else {
3021 8510 ret = 0;
3022 }
3023 }
3024
3025 7713 err = compute_chapters_end(ic);
3026
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7713 times.
7713 if (err < 0) {
3027 ret = err;
3028 goto find_stream_info_err;
3029 }
3030
3031 /* update the stream parameters from the internal codec contexts */
3032
2/2
✓ Branch 0 taken 8536 times.
✓ Branch 1 taken 7713 times.
16249 for (unsigned i = 0; i < ic->nb_streams; i++) {
3033 8536 AVStream *const st = ic->streams[i];
3034 8536 FFStream *const sti = ffstream(st);
3035
3036
2/2
✓ Branch 0 taken 8523 times.
✓ Branch 1 taken 13 times.
8536 if (sti->avctx_inited) {
3037 8523 ret = avcodec_parameters_from_context(st->codecpar, sti->avctx);
3038
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8523 times.
8523 if (ret < 0)
3039 goto find_stream_info_err;
3040
3041
3/4
✓ Branch 0 taken 8338 times.
✓ Branch 1 taken 185 times.
✓ Branch 2 taken 8338 times.
✗ Branch 3 not taken.
8523 if (sti->avctx->rc_buffer_size > 0 || sti->avctx->rc_max_rate > 0 ||
3042
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8338 times.
8338 sti->avctx->rc_min_rate) {
3043 size_t cpb_size;
3044 185 AVCPBProperties *props = av_cpb_properties_alloc(&cpb_size);
3045
1/2
✓ Branch 0 taken 185 times.
✗ Branch 1 not taken.
185 if (props) {
3046
1/2
✓ Branch 0 taken 185 times.
✗ Branch 1 not taken.
185 if (sti->avctx->rc_buffer_size > 0)
3047 185 props->buffer_size = sti->avctx->rc_buffer_size;
3048
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 185 times.
185 if (sti->avctx->rc_min_rate > 0)
3049 props->min_bitrate = sti->avctx->rc_min_rate;
3050
2/2
✓ Branch 0 taken 78 times.
✓ Branch 1 taken 107 times.
185 if (sti->avctx->rc_max_rate > 0)
3051 78 props->max_bitrate = sti->avctx->rc_max_rate;
3052
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 185 times.
185 if (!av_packet_side_data_add(&st->codecpar->coded_side_data,
3053 185 &st->codecpar->nb_coded_side_data,
3054 AV_PKT_DATA_CPB_PROPERTIES,
3055 (uint8_t *)props, cpb_size, 0))
3056 av_free(props);
3057 }
3058 }
3059 }
3060
3061 8536 sti->avctx_inited = 0;
3062 }
3063
3064 7713 find_stream_info_err:
3065
2/2
✓ Branch 0 taken 8536 times.
✓ Branch 1 taken 7713 times.
16249 for (unsigned i = 0; i < ic->nb_streams; i++) {
3066 8536 AVStream *const st = ic->streams[i];
3067 8536 FFStream *const sti = ffstream(st);
3068 int err;
3069
3070
1/2
✓ Branch 0 taken 8536 times.
✗ Branch 1 not taken.
8536 if (sti->info) {
3071 8536 av_freep(&sti->info->duration_error);
3072 8536 av_freep(&sti->info);
3073 }
3074
3075
2/2
✓ Branch 1 taken 8435 times.
✓ Branch 2 taken 101 times.
8536 if (avcodec_is_open(sti->avctx)) {
3076 8435 err = codec_close(sti);
3077
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 8435 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
8435 if (err < 0 && ret >= 0)
3078 ret = err;
3079 }
3080
3081 8536 av_bsf_free(&sti->extract_extradata.bsf);
3082 }
3083
2/2
✓ Branch 0 taken 4555 times.
✓ Branch 1 taken 3158 times.
7713 if (ic->pb) {
3084 4555 FFIOContext *const ctx = ffiocontext(ic->pb);
3085 4555 av_log(ic, AV_LOG_DEBUG, "After avformat_find_stream_info() pos: %"PRId64" bytes read:%"PRId64" seeks:%d frames:%d\n",
3086 avio_tell(ic->pb), ctx->bytes_read, ctx->seek_count, count);
3087 }
3088 7713 return ret;
3089
3090 unref_then_goto_end:
3091 av_packet_unref(pkt1);
3092 goto find_stream_info_err;
3093 }
3094