FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/demux.c
Date: 2025-07-28 20:30:09
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 2491049 static int64_t wrap_timestamp(const AVStream *st, int64_t timestamp)
54 {
55 2491049 const FFStream *const sti = cffstream(st);
56
4/4
✓ Branch 0 taken 150581 times.
✓ Branch 1 taken 2340468 times.
✓ Branch 2 taken 145995 times.
✓ Branch 3 taken 4586 times.
2491049 if (sti->pts_wrap_behavior != AV_PTS_WRAP_IGNORE && st->pts_wrap_bits < 64 &&
57
3/4
✓ Branch 0 taken 145995 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 88253 times.
✓ Branch 3 taken 57742 times.
145995 sti->pts_wrap_reference != AV_NOPTS_VALUE && timestamp != AV_NOPTS_VALUE) {
58
2/2
✓ Branch 0 taken 88049 times.
✓ Branch 1 taken 204 times.
88253 if (sti->pts_wrap_behavior == AV_PTS_WRAP_ADD_OFFSET &&
59
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 88049 times.
88049 timestamp < sti->pts_wrap_reference)
60 return timestamp + (1ULL << st->pts_wrap_bits);
61
2/2
✓ Branch 0 taken 204 times.
✓ Branch 1 taken 88049 times.
88253 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 2490999 return timestamp;
66 }
67
68 304353 int64_t ff_wrap_timestamp(const AVStream *st, int64_t timestamp)
69 {
70 304353 return wrap_timestamp(st, timestamp);
71 }
72
73 9799 static const AVCodec *find_probe_decoder(AVFormatContext *s, const AVStream *st, enum AVCodecID codec_id)
74 {
75 const AVCodec *codec;
76
77 #if CONFIG_H264_DECODER
78 /* Other parts of the code assume this decoder to be used for h264,
79 * so force it if possible. */
80
2/2
✓ Branch 0 taken 335 times.
✓ Branch 1 taken 9464 times.
9799 if (codec_id == AV_CODEC_ID_H264)
81 335 return avcodec_find_decoder_by_name("h264");
82 #endif
83
84 9464 codec = ff_find_decoder(s, st, codec_id);
85
2/2
✓ Branch 0 taken 171 times.
✓ Branch 1 taken 9293 times.
9464 if (!codec)
86 171 return NULL;
87
88
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9293 times.
9293 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 9293 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 7709 static int init_input(AVFormatContext *s, const char *filename,
159 AVDictionary **options)
160 {
161 int ret;
162 7709 AVProbeData pd = { filename, NULL, 0 };
163 7709 int score = AVPROBE_SCORE_RETRY;
164
165
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 7697 times.
7709 if (s->pb) {
166 12 s->flags |= AVFMT_FLAG_CUSTOM_IO;
167
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (!s->iformat)
168 return av_probe_input_buffer2(s->pb, &s->iformat, filename,
169 s, 0, s->format_probesize);
170
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 else if (s->iformat->flags & AVFMT_NOFILE)
171 av_log(s, AV_LOG_WARNING, "Custom AVIOContext makes no sense and "
172 "will be ignored with AVFMT_NOFILE format.\n");
173 12 return 0;
174 }
175
176
4/4
✓ Branch 0 taken 3821 times.
✓ Branch 1 taken 3876 times.
✓ Branch 2 taken 857 times.
✓ Branch 3 taken 2964 times.
7697 if ((s->iformat && s->iformat->flags & AVFMT_NOFILE) ||
177
4/4
✓ Branch 0 taken 3876 times.
✓ Branch 1 taken 857 times.
✓ Branch 3 taken 193 times.
✓ Branch 4 taken 3683 times.
4733 (!s->iformat && (s->iformat = av_probe_input_format2(&pd, 0, &score))))
178 3157 return score;
179
180
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4540 times.
4540 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 857 times.
✓ Branch 1 taken 3683 times.
4540 if (s->iformat)
184 857 return 0;
185 3683 return av_probe_input_buffer2(s->pb, &s->iformat, filename,
186 3683 s, 0, s->format_probesize);
187 }
188
189 7709 static int update_stream_avctx(AVFormatContext *s)
190 {
191 int ret;
192
2/2
✓ Branch 0 taken 8342 times.
✓ Branch 1 taken 7709 times.
16051 for (unsigned i = 0; i < s->nb_streams; i++) {
193 8342 AVStream *const st = s->streams[i];
194 8342 FFStream *const sti = ffstream(st);
195
196
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8342 times.
8342 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 8342 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
8342 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 8342 ret = avcodec_parameters_to_context(sti->avctx, st->codecpar);
207
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8342 times.
8342 if (ret < 0)
208 return ret;
209
210 8342 sti->codec_desc = avcodec_descriptor_get(sti->avctx->codec_id);
211
212 8342 sti->need_context_update = 0;
213 }
214 7709 return 0;
215 }
216
217 7709 int avformat_open_input(AVFormatContext **ps, const char *filename,
218 const AVInputFormat *fmt, AVDictionary **options)
219 {
220 FormatContextInternal *fci;
221 7709 AVFormatContext *s = *ps;
222 FFFormatContext *si;
223 7709 AVDictionary *tmp = NULL;
224 7709 ID3v2ExtraMeta *id3v2_extra_meta = NULL;
225 7709 int ret = 0;
226
227
3/4
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 7684 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 25 times.
7709 if (!s && !(s = avformat_alloc_context()))
228 return AVERROR(ENOMEM);
229 7709 fci = ff_fc_internal(s);
230 7709 si = &fci->fc;
231
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7709 times.
7709 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 3833 times.
✓ Branch 1 taken 3876 times.
7709 if (fmt)
236 3833 s->iformat = fmt;
237
238
2/2
✓ Branch 0 taken 7696 times.
✓ Branch 1 taken 13 times.
7709 if (options)
239 7696 av_dict_copy(&tmp, *options, 0);
240
241
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 7697 times.
7709 if (s->pb) // must be before any goto fail
242 12 s->flags |= AVFMT_FLAG_CUSTOM_IO;
243
244
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7709 times.
7709 if ((ret = av_opt_set_dict(s, &tmp)) < 0)
245 goto fail;
246
247
2/4
✓ Branch 0 taken 7709 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 7709 times.
7709 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 7709 times.
7709 if ((ret = init_input(s, filename, &tmp)) < 0)
253 goto fail;
254 7709 s->probe_score = ret;
255
256
6/6
✓ Branch 0 taken 7626 times.
✓ Branch 1 taken 83 times.
✓ Branch 2 taken 4469 times.
✓ Branch 3 taken 3157 times.
✓ Branch 4 taken 4468 times.
✓ Branch 5 taken 1 times.
7709 if (!s->protocol_whitelist && s->pb && s->pb->protocol_whitelist) {
257 4468 s->protocol_whitelist = av_strdup(s->pb->protocol_whitelist);
258
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4468 times.
4468 if (!s->protocol_whitelist) {
259 ret = AVERROR(ENOMEM);
260 goto fail;
261 }
262 }
263
264
4/6
✓ Branch 0 taken 7709 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4552 times.
✓ Branch 3 taken 3157 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 4552 times.
7709 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 7709 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
7709 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 7709 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 7709 times.
7709 if (s->iformat->flags & AVFMT_NEEDNUMBER) {
282 if (!av_filename_number_test(filename)) {
283 ret = AVERROR(EINVAL);
284 goto fail;
285 }
286 }
287
288 7709 s->duration = s->start_time = AV_NOPTS_VALUE;
289
290 /* Allocate private data. */
291
2/2
✓ Branch 1 taken 7552 times.
✓ Branch 2 taken 157 times.
7709 if (ffifmt(s->iformat)->priv_data_size > 0) {
292
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 7552 times.
7552 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 6718 times.
✓ Branch 1 taken 834 times.
7552 if (s->iformat->priv_class) {
297 6718 *(const AVClass **) s->priv_data = s->iformat->priv_class;
298 6718 av_opt_set_defaults(s->priv_data);
299
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6718 times.
6718 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 4552 times.
✓ Branch 1 taken 3157 times.
7709 if (s->pb)
306 4552 ff_id3v2_read_dict(s->pb, &si->id3v2_meta, ID3v2_DEFAULT_MAGIC, &id3v2_extra_meta);
307
308
1/2
✓ Branch 1 taken 7709 times.
✗ Branch 2 not taken.
7709 if (ffifmt(s->iformat)->read_header)
309
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 7709 times.
7709 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 6425 times.
✓ Branch 1 taken 1284 times.
7709 if (!s->metadata) {
316 6425 s->metadata = si->id3v2_meta;
317 6425 si->id3v2_meta = NULL;
318
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1284 times.
1284 } 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 7699 times.
7709 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 7709 times.
7709 if ((ret = avformat_queue_attached_pictures(s)) < 0)
338 goto close;
339
340
4/4
✓ Branch 0 taken 4552 times.
✓ Branch 1 taken 3157 times.
✓ Branch 2 taken 4110 times.
✓ Branch 3 taken 442 times.
7709 if (s->pb && !si->data_offset)
341 4110 si->data_offset = avio_tell(s->pb);
342
343 7709 fci->raw_packet_buffer_size = 0;
344
345 7709 update_stream_avctx(s);
346
347
2/2
✓ Branch 0 taken 7696 times.
✓ Branch 1 taken 13 times.
7709 if (options) {
348 7696 av_dict_free(options);
349 7696 *options = tmp;
350 }
351 7709 *ps = s;
352 7709 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 7709 void avformat_close_input(AVFormatContext **ps)
368 {
369 AVFormatContext *s;
370 AVIOContext *pb;
371
372
2/4
✓ Branch 0 taken 7709 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 7709 times.
7709 if (!ps || !*ps)
373 return;
374
375 7709 s = *ps;
376 7709 pb = s->pb;
377
378
5/6
✓ Branch 0 taken 7709 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4575 times.
✓ Branch 3 taken 3134 times.
✓ Branch 4 taken 4527 times.
✓ Branch 5 taken 48 times.
7709 if ((s->iformat && strcmp(s->iformat->name, "image2") && s->iformat->flags & AVFMT_NOFILE) ||
379
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 7649 times.
7661 (s->flags & AVFMT_FLAG_CUSTOM_IO))
380 60 pb = NULL;
381
382
1/2
✓ Branch 0 taken 7709 times.
✗ Branch 1 not taken.
7709 if (s->iformat)
383
2/2
✓ Branch 1 taken 5040 times.
✓ Branch 2 taken 2669 times.
7709 if (ffifmt(s->iformat)->read_close)
384 5040 ffifmt(s->iformat)->read_close(s);
385
386 7709 ff_format_io_close(s, &pb);
387 7709 avformat_free_context(s);
388
389 7709 *ps = NULL;
390 }
391
392 1096384 static void force_codec_ids(AVFormatContext *s, AVStream *st)
393 {
394
5/5
✓ Branch 0 taken 746626 times.
✓ Branch 1 taken 346659 times.
✓ Branch 2 taken 2969 times.
✓ Branch 3 taken 128 times.
✓ Branch 4 taken 2 times.
1096384 switch (st->codecpar->codec_type) {
395 746626 case AVMEDIA_TYPE_VIDEO:
396
2/2
✓ Branch 0 taken 118497 times.
✓ Branch 1 taken 628129 times.
746626 if (s->video_codec_id)
397 118497 st->codecpar->codec_id = s->video_codec_id;
398 746626 break;
399 346659 case AVMEDIA_TYPE_AUDIO:
400
2/2
✓ Branch 0 taken 2558 times.
✓ Branch 1 taken 344101 times.
346659 if (s->audio_codec_id)
401 2558 st->codecpar->codec_id = s->audio_codec_id;
402 346659 break;
403 2969 case AVMEDIA_TYPE_SUBTITLE:
404
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2969 times.
2969 if (s->subtitle_codec_id)
405 st->codecpar->codec_id = s->subtitle_codec_id;
406 2969 break;
407 128 case AVMEDIA_TYPE_DATA:
408
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 128 times.
128 if (s->data_codec_id)
409 st->codecpar->codec_id = s->data_codec_id;
410 128 break;
411 }
412 1096384 }
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 1093347 static int update_wrap_reference(AVFormatContext *s, AVStream *st, int stream_index, AVPacket *pkt)
468 {
469 1093347 FFStream *const sti = ffstream(st);
470 1093347 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 891071 times.
✓ Branch 1 taken 202276 times.
1093347 if (ref == AV_NOPTS_VALUE)
476 891071 ref = pkt->pts;
477
7/8
✓ Branch 0 taken 1025684 times.
✓ Branch 1 taken 67663 times.
✓ Branch 2 taken 48575 times.
✓ Branch 3 taken 977109 times.
✓ Branch 4 taken 305 times.
✓ Branch 5 taken 48270 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 305 times.
1093347 if (sti->pts_wrap_reference != AV_NOPTS_VALUE || st->pts_wrap_bits >= 63 || ref == AV_NOPTS_VALUE || !s->correct_ts_overflow)
478 1093042 return 0;
479 305 ref &= (1LL << st->pts_wrap_bits)-1;
480
481 // reference time stamp should be 60 s before first time stamp
482 305 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 611 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 304 times.
306 AV_PTS_WRAP_ADD_OFFSET : AV_PTS_WRAP_SUB_OFFSET;
487
488 305 first_program = av_find_program_from_stream(s, NULL, stream_index);
489
490
2/2
✓ Branch 0 taken 199 times.
✓ Branch 1 taken 106 times.
305 if (!first_program) {
491 199 int default_stream_index = av_find_default_stream_index(s);
492 199 FFStream *const default_sti = ffstream(s->streams[default_stream_index]);
493
2/2
✓ Branch 0 taken 188 times.
✓ Branch 1 taken 11 times.
199 if (default_sti->pts_wrap_reference == AV_NOPTS_VALUE) {
494
2/2
✓ Branch 0 taken 353 times.
✓ Branch 1 taken 188 times.
541 for (unsigned i = 0; i < s->nb_streams; i++) {
495 353 FFStream *const sti = ffstream(s->streams[i]);
496
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 353 times.
353 if (av_find_program_from_stream(s, NULL, i))
497 continue;
498 353 sti->pts_wrap_reference = pts_wrap_reference;
499 353 sti->pts_wrap_behavior = pts_wrap_behavior;
500 }
501 } else {
502 11 sti->pts_wrap_reference = default_sti->pts_wrap_reference;
503 11 sti->pts_wrap_behavior = default_sti->pts_wrap_behavior;
504 }
505 } else {
506 106 AVProgram *program = first_program;
507
2/2
✓ Branch 0 taken 106 times.
✓ Branch 1 taken 66 times.
172 while (program) {
508
2/2
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 66 times.
106 if (program->pts_wrap_reference != AV_NOPTS_VALUE) {
509 40 pts_wrap_reference = program->pts_wrap_reference;
510 40 pts_wrap_behavior = program->pts_wrap_behavior;
511 40 break;
512 }
513 66 program = av_find_program_from_stream(s, program, stream_index);
514 }
515
516 // update every program with differing pts_wrap_reference
517 106 program = first_program;
518
2/2
✓ Branch 0 taken 106 times.
✓ Branch 1 taken 106 times.
212 while (program) {
519
2/2
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 40 times.
106 if (program->pts_wrap_reference != pts_wrap_reference) {
520
2/2
✓ Branch 0 taken 108 times.
✓ Branch 1 taken 66 times.
174 for (unsigned i = 0; i < program->nb_stream_indexes; i++) {
521 108 FFStream *const sti = ffstream(s->streams[program->stream_index[i]]);
522 108 sti->pts_wrap_reference = pts_wrap_reference;
523 108 sti->pts_wrap_behavior = pts_wrap_behavior;
524 }
525
526 66 program->pts_wrap_reference = pts_wrap_reference;
527 66 program->pts_wrap_behavior = pts_wrap_behavior;
528 }
529 106 program = av_find_program_from_stream(s, program, stream_index);
530 }
531 }
532 305 return 1;
533 }
534
535 1093347 static void update_timestamps(AVFormatContext *s, AVStream *st, AVPacket *pkt)
536 {
537 1093347 FFStream *const sti = ffstream(st);
538
539
4/4
✓ Branch 1 taken 305 times.
✓ Branch 2 taken 1093042 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 304 times.
1093347 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 1093347 pkt->dts = wrap_timestamp(st, pkt->dts);
550 1093347 pkt->pts = wrap_timestamp(st, pkt->pts);
551
552 1093347 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 1093347 times.
1093347 if (s->use_wallclock_as_timestamps)
556 pkt->dts = pkt->pts = av_rescale_q(av_gettime(), AV_TIME_BASE_Q, st->time_base);
557 1093347 }
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 1093347 static int handle_new_packet(AVFormatContext *s, AVPacket *pkt, int allow_passthrough)
568 {
569 1093347 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 1093347 times.
1093347 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 1093144 times.
1093347 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 1093347 st = s->streams[pkt->stream_index];
589 1093347 sti = ffstream(st);
590
591 1093347 update_timestamps(s, st, pkt);
592
593
6/6
✓ Branch 0 taken 1077376 times.
✓ Branch 1 taken 15971 times.
✓ Branch 2 taken 1071251 times.
✓ Branch 3 taken 6125 times.
✓ Branch 4 taken 1071209 times.
✓ Branch 5 taken 42 times.
1093347 if (sti->request_probe <= 0 && allow_passthrough && !fci->raw_packet_buffer.head)
594 1071209 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 1099902 int ff_read_packet(AVFormatContext *s, AVPacket *pkt)
620 {
621 1099902 FormatContextInternal *const fci = ff_fc_internal(s);
622 int err;
623
624 #if FF_API_INIT_PACKET
625 FF_DISABLE_DEPRECATION_WARNINGS
626 1099902 pkt->data = NULL;
627 1099902 pkt->size = 0;
628 1099902 av_init_packet(pkt);
629 FF_ENABLE_DEPRECATION_WARNINGS
630 #else
631 av_packet_unref(pkt);
632 #endif
633
634 22427 for (;;) {
635 1122329 PacketListEntry *pktl = fci->raw_packet_buffer.head;
636
637
2/2
✓ Branch 0 taken 37692 times.
✓ Branch 1 taken 1084637 times.
1122329 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 1100144 err = ffifmt(s->iformat)->read_packet(s, pkt);
650
2/2
✓ Branch 0 taken 12922 times.
✓ Branch 1 taken 1087222 times.
1100144 if (err < 0) {
651 12922 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 6520 times.
12922 if (err == FFERROR_REDO)
657 6402 continue;
658
3/4
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 6508 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 12 times.
6520 if (!pktl || err == AVERROR(EAGAIN))
659 6508 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 1087222 err = av_packet_make_refcounted(pkt);
672
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1087222 times.
1087222 if (err < 0) {
673 av_packet_unref(pkt);
674 return err;
675 }
676
677 1087222 err = handle_new_packet(s, pkt, 1);
678
2/2
✓ Branch 0 taken 1071209 times.
✓ Branch 1 taken 16013 times.
1087222 if (err <= 0) /* Error or passthrough */
679 1071209 return err;
680 }
681 }
682
683 /**
684 * Return the frame duration in seconds. Return 0 if not available.
685 */
686 325951 static void compute_frame_duration(AVFormatContext *s, int *pnum, int *pden,
687 AVStream *st, AVCodecParserContext *pc,
688 AVPacket *pkt)
689 {
690 325951 FFStream *const sti = ffstream(st);
691 325951 AVRational codec_framerate = sti->avctx->framerate;
692 int frame_size, sample_rate;
693
694 325951 *pnum = 0;
695 325951 *pden = 0;
696
3/3
✓ Branch 0 taken 224079 times.
✓ Branch 1 taken 100490 times.
✓ Branch 2 taken 1382 times.
325951 switch (st->codecpar->codec_type) {
697 224079 case AVMEDIA_TYPE_VIDEO:
698
6/6
✓ Branch 0 taken 194206 times.
✓ Branch 1 taken 29873 times.
✓ Branch 2 taken 42285 times.
✓ Branch 3 taken 151921 times.
✓ Branch 4 taken 30844 times.
✓ Branch 5 taken 11441 times.
224079 if (st->r_frame_rate.num && (!pc || !codec_framerate.num)) {
699 182765 *pnum = st->r_frame_rate.den;
700 182765 *pden = st->r_frame_rate.num;
701
2/2
✓ Branch 0 taken 23741 times.
✓ Branch 1 taken 17573 times.
41314 } else if ((s->iformat->flags & AVFMT_NOTIMESTAMPS) &&
702
2/2
✓ Branch 0 taken 14779 times.
✓ Branch 1 taken 8962 times.
23741 !codec_framerate.num &&
703
3/4
✓ Branch 0 taken 14777 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 14777 times.
✗ Branch 3 not taken.
14779 st->avg_frame_rate.num && st->avg_frame_rate.den) {
704 14777 *pnum = st->avg_frame_rate.den;
705 14777 *pden = st->avg_frame_rate.num;
706
2/2
✓ Branch 0 taken 9158 times.
✓ Branch 1 taken 17379 times.
26537 } else if (st->time_base.num * 1000LL > st->time_base.den) {
707 9158 *pnum = st->time_base.num;
708 9158 *pden = st->time_base.den;
709
2/2
✓ Branch 0 taken 17354 times.
✓ Branch 1 taken 25 times.
17379 } else if (codec_framerate.den * 1000LL > codec_framerate.num) {
710 52062 int ticks_per_frame = (sti->codec_desc &&
711
3/4
✓ Branch 0 taken 17354 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12086 times.
✓ Branch 3 taken 5268 times.
17354 (sti->codec_desc->props & AV_CODEC_PROP_FIELDS)) ? 2 : 1;
712 17354 av_reduce(pnum, pden,
713 17354 codec_framerate.den,
714 17354 codec_framerate.num * (int64_t)ticks_per_frame,
715 INT_MAX);
716
717
4/4
✓ Branch 0 taken 15528 times.
✓ Branch 1 taken 1826 times.
✓ Branch 2 taken 11457 times.
✓ Branch 3 taken 4071 times.
17354 if (pc && pc->repeat_pict) {
718 11457 av_reduce(pnum, pden,
719 11457 (*pnum) * (1LL + pc->repeat_pict),
720 11457 (*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 17354 times.
✗ Branch 1 not taken.
17354 if (sti->codec_desc &&
727
3/4
✓ Branch 0 taken 12086 times.
✓ Branch 1 taken 5268 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 12086 times.
17354 (sti->codec_desc->props & AV_CODEC_PROP_FIELDS) && !pc)
728 *pnum = *pden = 0;
729 }
730 224079 break;
731 100490 case AVMEDIA_TYPE_AUDIO:
732
2/2
✓ Branch 0 taken 45156 times.
✓ Branch 1 taken 55334 times.
100490 if (sti->avctx_inited) {
733 45156 frame_size = av_get_audio_frame_duration(sti->avctx, pkt->size);
734 45156 sample_rate = sti->avctx->sample_rate;
735 } else {
736 55334 frame_size = av_get_audio_frame_duration2(st->codecpar, pkt->size);
737 55334 sample_rate = st->codecpar->sample_rate;
738 }
739
4/4
✓ Branch 0 taken 95223 times.
✓ Branch 1 taken 5267 times.
✓ Branch 2 taken 95218 times.
✓ Branch 3 taken 5 times.
100490 if (frame_size <= 0 || sample_rate <= 0)
740 break;
741 95218 *pnum = frame_size;
742 95218 *pden = sample_rate;
743 95218 break;
744 1382 default:
745 1382 break;
746 }
747 325951 }
748
749 728517 static int has_decode_delay_been_guessed(AVStream *st)
750 {
751 728517 FFStream *const sti = ffstream(st);
752
2/2
✓ Branch 0 taken 711551 times.
✓ Branch 1 taken 16966 times.
728517 if (st->codecpar->codec_id != AV_CODEC_ID_H264) return 1;
753
2/2
✓ Branch 0 taken 6059 times.
✓ Branch 1 taken 10907 times.
16966 if (!sti->info) // if we have left find_stream_info then nb_decoded_frames won't increase anymore for stream copy
754 6059 return 1;
755 #if CONFIG_H264_DECODER
756
2/2
✓ Branch 0 taken 6687 times.
✓ Branch 1 taken 4220 times.
10907 if (sti->avctx->has_b_frames &&
757
2/2
✓ Branch 1 taken 1416 times.
✓ Branch 2 taken 5271 times.
6687 avpriv_h264_has_num_reorder_frames(sti->avctx) == sti->avctx->has_b_frames)
758 1416 return 1;
759 #endif
760
2/2
✓ Branch 0 taken 9303 times.
✓ Branch 1 taken 188 times.
9491 if (sti->avctx->has_b_frames < 3)
761 9303 return sti->nb_decoded_frames >= 7;
762
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 152 times.
188 else if (sti->avctx->has_b_frames < 4)
763 36 return sti->nb_decoded_frames >= 18;
764 else
765 152 return sti->nb_decoded_frames >= 20;
766 }
767
768 36737 static PacketListEntry *get_next_pkt(AVFormatContext *s, AVStream *st,
769 PacketListEntry *pktl)
770 {
771 36737 FormatContextInternal *const fci = ff_fc_internal(s);
772 36737 FFFormatContext *const si = &fci->fc;
773
2/2
✓ Branch 0 taken 34787 times.
✓ Branch 1 taken 1950 times.
36737 if (pktl->next)
774 34787 return pktl->next;
775
2/2
✓ Branch 0 taken 1920 times.
✓ Branch 1 taken 30 times.
1950 if (pktl == si->packet_buffer.tail)
776 1920 return fci->parse_queue.head;
777 30 return NULL;
778 }
779
780 526881 static int64_t select_from_pts_buffer(AVStream *st, int64_t *pts_buffer, int64_t dts)
781 {
782 526881 FFStream *const sti = ffstream(st);
783 1573393 int onein_oneout = st->codecpar->codec_id != AV_CODEC_ID_H264 &&
784
4/4
✓ Branch 0 taken 519631 times.
✓ Branch 1 taken 7250 times.
✓ Branch 2 taken 518655 times.
✓ Branch 3 taken 976 times.
1045536 st->codecpar->codec_id != AV_CODEC_ID_HEVC &&
785
2/2
✓ Branch 0 taken 518636 times.
✓ Branch 1 taken 19 times.
518655 st->codecpar->codec_id != AV_CODEC_ID_VVC;
786
787
2/2
✓ Branch 0 taken 8245 times.
✓ Branch 1 taken 518636 times.
526881 if (!onein_oneout) {
788 8245 int delay = sti->avctx->has_b_frames;
789
790
2/2
✓ Branch 0 taken 1221 times.
✓ Branch 1 taken 7024 times.
8245 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 11902 times.
✓ Branch 1 taken 7024 times.
18926 for (int i = 0; i < delay; i++) {
803
2/2
✓ Branch 0 taken 11105 times.
✓ Branch 1 taken 797 times.
11902 if (pts_buffer[i] != AV_NOPTS_VALUE) {
804 11105 int64_t diff = FFABS(pts_buffer[i] - dts)
805 11105 + (uint64_t)sti->pts_reorder_error[i];
806 11105 diff = FFMAX(diff, sti->pts_reorder_error[i]);
807 11105 sti->pts_reorder_error[i] = diff;
808 11105 sti->pts_reorder_error_count[i]++;
809
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 11102 times.
11105 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 525485 times.
526881 if (dts == AV_NOPTS_VALUE)
819 1396 dts = pts_buffer[0];
820
821 526881 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 6242 static void update_dts_from_pts(AVFormatContext *s, int stream_index,
829 PacketListEntry *pkt_buffer)
830 {
831 6242 AVStream *const st = s->streams[stream_index];
832 6242 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 106114 times.
✓ Branch 1 taken 6242 times.
112356 for (int i = 0; i < MAX_REORDER_DELAY + 1; i++)
837 106114 pts_buffer[i] = AV_NOPTS_VALUE;
838
839
2/2
✓ Branch 1 taken 5522 times.
✓ Branch 2 taken 6242 times.
11764 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 6242 }
852
853 568364 static void update_initial_timestamps(AVFormatContext *s, int stream_index,
854 int64_t dts, int64_t pts, AVPacket *pkt)
855 {
856 568364 FormatContextInternal *const fci = ff_fc_internal(s);
857 568364 FFFormatContext *const si = &fci->fc;
858 568364 AVStream *const st = s->streams[stream_index];
859 568364 FFStream *const sti = ffstream(st);
860
2/2
✓ Branch 0 taken 185492 times.
✓ Branch 1 taken 382872 times.
568364 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 186078 times.
✓ Branch 1 taken 382286 times.
✓ Branch 2 taken 6305 times.
✓ Branch 3 taken 179773 times.
568364 if (sti->first_dts != AV_NOPTS_VALUE ||
865 6305 dts == AV_NOPTS_VALUE ||
866
1/2
✓ Branch 0 taken 6305 times.
✗ Branch 1 not taken.
6305 sti->cur_dts == AV_NOPTS_VALUE ||
867
1/2
✓ Branch 0 taken 6305 times.
✗ Branch 1 not taken.
6305 sti->cur_dts < INT_MIN + RELATIVE_TS_BASE ||
868
2/4
✓ Branch 0 taken 6305 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6305 times.
12610 dts < INT_MIN + (sti->cur_dts - RELATIVE_TS_BASE) ||
869 6305 is_relative(dts))
870 562059 return;
871
872 6305 sti->first_dts = dts - (sti->cur_dts - RELATIVE_TS_BASE);
873 6305 sti->cur_dts = dts;
874 6305 shift = (uint64_t)sti->first_dts - RELATIVE_TS_BASE;
875
876
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6305 times.
6305 if (is_relative(pts))
877 pts += shift;
878
879
2/2
✓ Branch 1 taken 5263 times.
✓ Branch 2 taken 6305 times.
11568 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 6224 times.
✓ Branch 2 taken 81 times.
6305 if (has_decode_delay_been_guessed(st))
896 6224 update_dts_from_pts(s, stream_index, pktl);
897
898
2/2
✓ Branch 0 taken 1719 times.
✓ Branch 1 taken 4586 times.
6305 if (st->start_time == AV_NOPTS_VALUE) {
899
3/4
✓ Branch 0 taken 1240 times.
✓ Branch 1 taken 479 times.
✓ Branch 2 taken 1240 times.
✗ Branch 3 not taken.
1719 if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO || !(pkt->flags & AV_PKT_FLAG_DISCARD)) {
900 1719 st->start_time = pts;
901 }
902
4/4
✓ Branch 0 taken 479 times.
✓ Branch 1 taken 1240 times.
✓ Branch 2 taken 394 times.
✓ Branch 3 taken 85 times.
1719 if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && st->codecpar->sample_rate)
903 394 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 229310 static void update_initial_durations(AVFormatContext *s, AVStream *st,
908 int stream_index, int64_t duration)
909 {
910 229310 FormatContextInternal *const fci = ff_fc_internal(s);
911 229310 FFFormatContext *const si = &fci->fc;
912 229310 FFStream *const sti = ffstream(st);
913
2/2
✓ Branch 0 taken 182184 times.
✓ Branch 1 taken 47126 times.
229310 PacketListEntry *pktl = si->packet_buffer.head ? si->packet_buffer.head : fci->parse_queue.head;
914 229310 int64_t cur_dts = RELATIVE_TS_BASE;
915
916
2/2
✓ Branch 0 taken 124334 times.
✓ Branch 1 taken 104976 times.
229310 if (sti->first_dts != AV_NOPTS_VALUE) {
917
2/2
✓ Branch 0 taken 120609 times.
✓ Branch 1 taken 3725 times.
124334 if (sti->update_initial_durations_done)
918 120609 return;
919 3725 sti->update_initial_durations_done = 1;
920 3725 cur_dts = sti->first_dts;
921
1/2
✓ Branch 1 taken 5541 times.
✗ Branch 2 not taken.
5541 for (; pktl; pktl = get_next_pkt(s, st, pktl)) {
922
2/2
✓ Branch 0 taken 3740 times.
✓ Branch 1 taken 1801 times.
5541 if (pktl->pkt.stream_index == stream_index) {
923
2/2
✓ Branch 0 taken 3629 times.
✓ Branch 1 taken 111 times.
3740 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 3725 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
✓ Branch 3 taken 3692 times.
3725 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 3692 times.
3692 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 3671 times.
✓ Branch 1 taken 21 times.
3692 pktl = si->packet_buffer.head ? si->packet_buffer.head : fci->parse_queue.head;
940 3692 sti->first_dts = cur_dts;
941
2/2
✓ Branch 0 taken 80374 times.
✓ Branch 1 taken 24602 times.
104976 } else if (sti->cur_dts != RELATIVE_TS_BASE)
942 80374 return;
943
944
2/2
✓ Branch 1 taken 51776 times.
✓ Branch 2 taken 654 times.
52430 for (; pktl; pktl = get_next_pkt(s, st, pktl)) {
945
2/2
✓ Branch 0 taken 23981 times.
✓ Branch 1 taken 27795 times.
51776 if (pktl->pkt.stream_index != stream_index)
946 23981 continue;
947
2/2
✓ Branch 0 taken 567 times.
✓ Branch 1 taken 27228 times.
27795 if ((pktl->pkt.pts == pktl->pkt.dts ||
948
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 553 times.
567 pktl->pkt.pts == AV_NOPTS_VALUE) &&
949
2/2
✓ Branch 0 taken 3761 times.
✓ Branch 1 taken 23481 times.
27242 (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 26911 times.
27066 !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 654 times.
✓ Branch 1 taken 27640 times.
28294 if (!pktl)
964 654 sti->cur_dts = cur_dts;
965 }
966
967 574189 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 574189 FormatContextInternal *const fci = ff_fc_internal(s);
972 574189 FFFormatContext *const si = &fci->fc;
973 574189 FFStream *const sti = ffstream(st);
974 int num, den, presentation_delayed, delay;
975 int64_t offset;
976 AVRational duration;
977 1688641 int onein_oneout = st->codecpar->codec_id != AV_CODEC_ID_H264 &&
978
4/4
✓ Branch 0 taken 540263 times.
✓ Branch 1 taken 33926 times.
✓ Branch 2 taken 527135 times.
✓ Branch 3 taken 13128 times.
1101324 st->codecpar->codec_id != AV_CODEC_ID_HEVC &&
979
2/2
✓ Branch 0 taken 524088 times.
✓ Branch 1 taken 3047 times.
527135 st->codecpar->codec_id != AV_CODEC_ID_VVC;
980
981
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 574189 times.
574189 if (s->flags & AVFMT_FLAG_NOFILLIN)
982 return;
983
984
4/4
✓ Branch 0 taken 248133 times.
✓ Branch 1 taken 326056 times.
✓ Branch 2 taken 73925 times.
✓ Branch 3 taken 174208 times.
574189 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && pkt->dts != AV_NOPTS_VALUE) {
985
4/4
✓ Branch 0 taken 43545 times.
✓ Branch 1 taken 30380 times.
✓ Branch 2 taken 42364 times.
✓ Branch 3 taken 1181 times.
73925 if (pkt->dts == pkt->pts && sti->last_dts_for_order_check != AV_NOPTS_VALUE) {
986
2/2
✓ Branch 0 taken 42355 times.
✓ Branch 1 taken 9 times.
42364 if (sti->last_dts_for_order_check <= pkt->dts) {
987 42355 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 42335 times.
42364 if (sti->dts_ordered + sti->dts_misordered > 250) {
996 29 sti->dts_ordered >>= 1;
997 29 sti->dts_misordered >>= 1;
998 }
999 }
1000
1001 73925 sti->last_dts_for_order_check = pkt->dts;
1002
4/4
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 73888 times.
✓ Branch 2 taken 20 times.
✓ Branch 3 taken 17 times.
73925 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 574189 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
574189 if ((s->flags & AVFMT_FLAG_IGNDTS) && pkt->pts != AV_NOPTS_VALUE)
1007 pkt->dts = AV_NOPTS_VALUE;
1008
1009
4/4
✓ Branch 0 taken 187055 times.
✓ Branch 1 taken 387134 times.
✓ Branch 2 taken 27775 times.
✓ Branch 3 taken 159280 times.
574189 if (pc && pc->pict_type == AV_PICTURE_TYPE_B
1010
2/2
✓ Branch 0 taken 81 times.
✓ Branch 1 taken 27694 times.
27775 && !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 574189 delay = sti->avctx->has_b_frames;
1016 574189 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 46642 times.
✓ Branch 1 taken 527547 times.
✓ Branch 2 taken 42611 times.
✓ Branch 3 taken 4031 times.
574189 if (delay &&
1021
2/2
✓ Branch 0 taken 14836 times.
✓ Branch 1 taken 27775 times.
42611 pc && pc->pict_type != AV_PICTURE_TYPE_B)
1022 14836 presentation_delayed = 1;
1023
1024
4/4
✓ Branch 0 taken 344269 times.
✓ Branch 1 taken 229920 times.
✓ Branch 2 taken 167924 times.
✓ Branch 3 taken 176345 times.
574189 if (pkt->pts != AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE &&
1025
3/4
✓ Branch 0 taken 27455 times.
✓ Branch 1 taken 140469 times.
✓ Branch 2 taken 27455 times.
✗ Branch 3 not taken.
167924 st->pts_wrap_bits < 63 && pkt->dts > INT64_MIN + (1LL << st->pts_wrap_bits) &&
1026
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27455 times.
27455 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 30089 times.
✓ Branch 1 taken 544100 times.
✓ Branch 2 taken 24942 times.
✓ Branch 3 taken 5147 times.
574189 if (delay == 1 && pkt->dts == pkt->pts &&
1038
4/4
✓ Branch 0 taken 6136 times.
✓ Branch 1 taken 18806 times.
✓ Branch 2 taken 628 times.
✓ Branch 3 taken 5508 times.
24942 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 574189 duration = av_mul_q((AVRational) {pkt->duration, 1}, st->time_base);
1046
2/2
✓ Branch 0 taken 323146 times.
✓ Branch 1 taken 251043 times.
574189 if (pkt->duration <= 0) {
1047 323146 compute_frame_duration(s, &num, &den, st, pc, pkt);
1048
3/4
✓ Branch 0 taken 313856 times.
✓ Branch 1 taken 9290 times.
✓ Branch 2 taken 313856 times.
✗ Branch 3 not taken.
323146 if (den && num) {
1049 313856 duration = (AVRational) {num, den};
1050 313856 pkt->duration = av_rescale_rnd(1,
1051 313856 num * (int64_t) st->time_base.den,
1052 313856 den * (int64_t) st->time_base.num,
1053 AV_ROUND_DOWN);
1054 }
1055 }
1056
1057
6/6
✓ Branch 0 taken 562077 times.
✓ Branch 1 taken 12112 times.
✓ Branch 2 taken 379893 times.
✓ Branch 3 taken 182184 times.
✓ Branch 4 taken 47126 times.
✓ Branch 5 taken 332767 times.
574189 if (pkt->duration > 0 && (si->packet_buffer.head || fci->parse_queue.head))
1058 229310 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 187055 times.
✓ Branch 1 taken 387134 times.
✓ Branch 2 taken 961 times.
✓ Branch 3 taken 186094 times.
✓ Branch 4 taken 961 times.
✗ Branch 5 not taken.
574189 if (pc && sti->need_parsing == AVSTREAM_PARSE_TIMESTAMPS && pkt->size) {
1063 /* this will estimate bitrate based on this frame's duration and size */
1064 961 offset = av_rescale(pc->offset, pkt->duration, pkt->size);
1065
2/2
✓ Branch 0 taken 576 times.
✓ Branch 1 taken 385 times.
961 if (pkt->pts != AV_NOPTS_VALUE)
1066 576 pkt->pts += offset;
1067
2/2
✓ Branch 0 taken 121 times.
✓ Branch 1 taken 840 times.
961 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 198418 times.
✓ Branch 1 taken 375771 times.
574189 if (pkt->dts != AV_NOPTS_VALUE &&
1073
2/2
✓ Branch 0 taken 167902 times.
✓ Branch 1 taken 30516 times.
198418 pkt->pts != AV_NOPTS_VALUE &&
1074
2/2
✓ Branch 0 taken 5161 times.
✓ Branch 1 taken 162741 times.
167902 pkt->pts > pkt->dts)
1075 5161 presentation_delayed = 1;
1076
1077
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 574189 times.
574189 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 46642 times.
✓ Branch 1 taken 527547 times.
✓ Branch 2 taken 30089 times.
✓ Branch 3 taken 16553 times.
✓ Branch 4 taken 26635 times.
✓ Branch 5 taken 3454 times.
✓ Branch 6 taken 520647 times.
✓ Branch 7 taken 33535 times.
574189 if ((delay == 0 || (delay == 1 && pc)) &&
1086 onein_oneout) {
1087
2/2
✓ Branch 0 taken 5506 times.
✓ Branch 1 taken 515141 times.
520647 if (presentation_delayed) {
1088 /* DTS = decompression timestamp */
1089 /* PTS = presentation timestamp */
1090
2/2
✓ Branch 0 taken 2846 times.
✓ Branch 1 taken 2660 times.
5506 if (pkt->dts == AV_NOPTS_VALUE)
1091 2846 pkt->dts = sti->last_IP_pts;
1092 5506 update_initial_timestamps(s, pkt->stream_index, pkt->dts, pkt->pts, pkt);
1093
2/2
✓ Branch 0 taken 2507 times.
✓ Branch 1 taken 2999 times.
5506 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 237 times.
✓ Branch 1 taken 5269 times.
✓ Branch 2 taken 237 times.
✗ Branch 3 not taken.
5506 if (sti->last_IP_duration == 0 && (uint64_t)pkt->duration <= INT32_MAX)
1099 237 sti->last_IP_duration = pkt->duration;
1100
1/2
✓ Branch 0 taken 5506 times.
✗ Branch 1 not taken.
5506 if (pkt->dts != AV_NOPTS_VALUE)
1101 5506 sti->cur_dts = av_sat_add64(pkt->dts, sti->last_IP_duration);
1102
1/2
✓ Branch 0 taken 5506 times.
✗ Branch 1 not taken.
5506 if (pkt->dts != AV_NOPTS_VALUE &&
1103
2/2
✓ Branch 0 taken 3275 times.
✓ Branch 1 taken 2231 times.
5506 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 5506 times.
✗ Branch 1 not taken.
5506 if ((uint64_t)pkt->duration <= INT32_MAX)
1111 5506 sti->last_IP_duration = pkt->duration;
1112 5506 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 185053 times.
✓ Branch 1 taken 330088 times.
515141 } else if (pkt->pts != AV_NOPTS_VALUE ||
1116
2/2
✓ Branch 0 taken 155384 times.
✓ Branch 1 taken 29669 times.
185053 pkt->dts != AV_NOPTS_VALUE ||
1117
2/2
✓ Branch 0 taken 153000 times.
✓ Branch 1 taken 2384 times.
155384 pkt->duration > 0 ) {
1118
1119 /* presentation is not delayed : PTS and DTS are the same */
1120
2/2
✓ Branch 0 taken 182669 times.
✓ Branch 1 taken 330088 times.
512757 if (pkt->pts == AV_NOPTS_VALUE)
1121 182669 pkt->pts = pkt->dts;
1122 512757 update_initial_timestamps(s, pkt->stream_index, pkt->pts,
1123 pkt->pts, pkt);
1124
2/2
✓ Branch 0 taken 153000 times.
✓ Branch 1 taken 359757 times.
512757 if (pkt->pts == AV_NOPTS_VALUE)
1125 153000 pkt->pts = sti->cur_dts;
1126 512757 pkt->dts = pkt->pts;
1127
3/4
✓ Branch 0 taken 512757 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 512749 times.
✓ Branch 3 taken 8 times.
512757 if (pkt->pts != AV_NOPTS_VALUE && duration.num >= 0)
1128 512749 sti->cur_dts = av_add_stable(st->time_base, pkt->pts, duration, 1);
1129 }
1130 }
1131
1132
3/4
✓ Branch 0 taken 526949 times.
✓ Branch 1 taken 47240 times.
✓ Branch 2 taken 526949 times.
✗ Branch 3 not taken.
574189 if (pkt->pts != AV_NOPTS_VALUE && delay <= MAX_REORDER_DELAY) {
1133 526949 sti->pts_buffer[0] = pkt->pts;
1134
4/4
✓ Branch 0 taken 22103 times.
✓ Branch 1 taken 521645 times.
✓ Branch 2 taken 16799 times.
✓ Branch 3 taken 5304 times.
543748 for (int i = 0; i < delay && sti->pts_buffer[i] > sti->pts_buffer[i + 1]; i++)
1135 16799 FFSWAP(int64_t, sti->pts_buffer[i], sti->pts_buffer[i + 1]);
1136
1137
2/2
✓ Branch 1 taken 526320 times.
✓ Branch 2 taken 629 times.
526949 if (has_decode_delay_been_guessed(st))
1138 526320 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 50101 times.
✓ Branch 1 taken 524088 times.
574189 if (!onein_oneout)
1142 // This should happen on the first packet
1143 50101 update_initial_timestamps(s, pkt->stream_index, pkt->dts, pkt->pts, pkt);
1144
2/2
✓ Branch 0 taken 8023 times.
✓ Branch 1 taken 566166 times.
574189 if (pkt->dts > sti->cur_dts)
1145 8023 sti->cur_dts = pkt->dts;
1146
1147
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 574189 times.
574189 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 574104 times.
✓ Branch 1 taken 85 times.
✓ Branch 3 taken 377369 times.
✓ Branch 4 taken 196735 times.
574189 if (st->codecpar->codec_type == AVMEDIA_TYPE_DATA || ff_is_intra_only(st->codecpar->codec_id))
1153 377454 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 701659 static int parse_packet(AVFormatContext *s, AVPacket *pkt,
1163 int stream_index, int flush)
1164 {
1165 701659 FormatContextInternal *const fci = ff_fc_internal(s);
1166 701659 FFFormatContext *const si = &fci->fc;
1167 701659 AVPacket *out_pkt = si->parse_pkt;
1168 701659 AVStream *st = s->streams[stream_index];
1169 701659 FFStream *const sti = ffstream(st);
1170 701659 const uint8_t *data = pkt->data;
1171 701659 int size = pkt->size;
1172 701659 int ret = 0, got_output = flush;
1173
1174
5/6
✓ Branch 0 taken 2248 times.
✓ Branch 1 taken 699411 times.
✓ Branch 2 taken 164 times.
✓ Branch 3 taken 2084 times.
✓ Branch 4 taken 164 times.
✗ Branch 5 not taken.
701659 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 815596 times.
✓ Branch 1 taken 704909 times.
✓ Branch 2 taken 5334 times.
✓ Branch 3 taken 699575 times.
✓ Branch 4 taken 3250 times.
✓ Branch 5 taken 2084 times.
1520505 while (size > 0 || (flush && got_output)) {
1188 818846 int64_t next_pts = pkt->pts;
1189 818846 int64_t next_dts = pkt->dts;
1190 int len;
1191
1192 818846 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 818846 pkt->pts = pkt->dts = AV_NOPTS_VALUE;
1197 818846 pkt->pos = -1;
1198 /* increment read pointer */
1199 av_assert1(data || !len);
1200
2/2
✓ Branch 0 taken 805925 times.
✓ Branch 1 taken 12921 times.
818846 data = len ? data + len : data;
1201 818846 size -= len;
1202
1203 818846 got_output = !!out_pkt->size;
1204
1205
2/2
✓ Branch 0 taken 631955 times.
✓ Branch 1 taken 186891 times.
818846 if (!out_pkt->size)
1206 631955 continue;
1207
1208
4/4
✓ Branch 0 taken 185725 times.
✓ Branch 1 taken 1166 times.
✓ Branch 2 taken 69191 times.
✓ Branch 3 taken 116534 times.
186891 if (pkt->buf && out_pkt->data == pkt->data) {
1209 /* reference pkt->buf only when out_pkt->data is guaranteed to point
1210 * to data in it and not in the parser's internal buffer. */
1211 /* XXX: Ensure this is the case with all parsers when sti->parser->flags
1212 * is PARSER_FLAG_COMPLETE_FRAMES and check for that instead? */
1213 69191 out_pkt->buf = av_buffer_ref(pkt->buf);
1214
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 69191 times.
69191 if (!out_pkt->buf) {
1215 ret = AVERROR(ENOMEM);
1216 goto fail;
1217 }
1218 } else {
1219 117700 ret = av_packet_make_refcounted(out_pkt);
1220
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 117700 times.
117700 if (ret < 0)
1221 goto fail;
1222 }
1223
1224
2/2
✓ Branch 0 taken 9166 times.
✓ Branch 1 taken 177725 times.
186891 if (pkt->side_data) {
1225 9166 out_pkt->side_data = pkt->side_data;
1226 9166 out_pkt->side_data_elems = pkt->side_data_elems;
1227 9166 pkt->side_data = NULL;
1228 9166 pkt->side_data_elems = 0;
1229 }
1230
1231 /* set the duration */
1232
2/2
✓ Branch 0 taken 49387 times.
✓ Branch 1 taken 137504 times.
186891 out_pkt->duration = (sti->parser->flags & PARSER_FLAG_COMPLETE_FRAMES) ? pkt->duration : 0;
1233
2/2
✓ Branch 0 taken 108865 times.
✓ Branch 1 taken 78026 times.
186891 if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
1234
2/2
✓ Branch 0 taken 108759 times.
✓ Branch 1 taken 106 times.
108865 if (sti->avctx->sample_rate > 0) {
1235 108759 out_pkt->duration =
1236 108759 av_rescale_q_rnd(sti->parser->duration,
1237 108759 (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 76204 times.
78026 } 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 186891 out_pkt->stream_index = st->index;
1249 186891 out_pkt->pts = sti->parser->pts;
1250 186891 out_pkt->dts = sti->parser->dts;
1251 186891 out_pkt->pos = sti->parser->pos;
1252 186891 out_pkt->flags |= pkt->flags & (AV_PKT_FLAG_DISCARD | AV_PKT_FLAG_CORRUPT);
1253
1254
2/2
✓ Branch 0 taken 114660 times.
✓ Branch 1 taken 72231 times.
186891 if (sti->need_parsing == AVSTREAM_PARSE_FULL_RAW)
1255 114660 out_pkt->pos = sti->parser->frame_offset;
1256
1257
2/2
✓ Branch 0 taken 175103 times.
✓ Branch 1 taken 11788 times.
186891 if (sti->parser->key_frame == 1 ||
1258
2/2
✓ Branch 0 taken 93687 times.
✓ Branch 1 taken 81416 times.
175103 (sti->parser->key_frame == -1 &&
1259
2/2
✓ Branch 0 taken 81800 times.
✓ Branch 1 taken 11887 times.
93687 sti->parser->pict_type == AV_PICTURE_TYPE_I))
1260 93588 out_pkt->flags |= AV_PKT_FLAG_KEY;
1261
1262
6/6
✓ Branch 0 taken 93687 times.
✓ Branch 1 taken 93204 times.
✓ Branch 2 taken 317 times.
✓ Branch 3 taken 93370 times.
✓ Branch 4 taken 288 times.
✓ Branch 5 taken 29 times.
186891 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 186891 compute_pkt_fields(s, st, sti->parser, out_pkt, next_dts, next_pts);
1266
1267 186891 ret = avpriv_packet_list_put(&fci->parse_queue,
1268 out_pkt, NULL, 0);
1269
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 186891 times.
186891 if (ret < 0)
1270 goto fail;
1271 }
1272
1273 /* end of the stream => close and free the parser */
1274
2/2
✓ Branch 0 taken 699575 times.
✓ Branch 1 taken 2084 times.
701659 if (flush) {
1275 2084 av_parser_close(sti->parser);
1276 2084 sti->parser = NULL;
1277 }
1278
1279 699575 fail:
1280
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 701659 times.
701659 if (ret < 0)
1281 av_packet_unref(out_pkt);
1282 701659 av_packet_unref(pkt);
1283 701659 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 8425 static int codec_close(FFStream *sti)
1292 {
1293 8425 AVCodecContext *avctx_new = NULL;
1294 8425 AVCodecParameters *par_tmp = NULL;
1295 8425 const AVCodec *new_codec = NULL;
1296 int ret;
1297
1298 8425 new_codec =
1299 8425 (sti->avctx->codec_id != sti->pub.codecpar->codec_id) ?
1300
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 8415 times.
8425 avcodec_find_decoder(sti->pub.codecpar->codec_id) :
1301 8415 sti->avctx->codec;
1302
1303 8425 avctx_new = avcodec_alloc_context3(new_codec);
1304
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8425 times.
8425 if (!avctx_new) {
1305 ret = AVERROR(ENOMEM);
1306 goto fail;
1307 }
1308
1309 8425 par_tmp = avcodec_parameters_alloc();
1310
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8425 times.
8425 if (!par_tmp) {
1311 ret = AVERROR(ENOMEM);
1312 goto fail;
1313 }
1314
1315 8425 ret = avcodec_parameters_from_context(par_tmp, sti->avctx);
1316
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8425 times.
8425 if (ret < 0)
1317 goto fail;
1318
1319 8425 ret = avcodec_parameters_to_context(avctx_new, par_tmp);
1320
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8425 times.
8425 if (ret < 0)
1321 goto fail;
1322
1323 8425 avctx_new->pkt_timebase = sti->avctx->pkt_timebase;
1324
1325 8425 avcodec_free_context(&sti->avctx);
1326 8425 sti->avctx = avctx_new;
1327
1328 8425 avctx_new = NULL;
1329 8425 ret = 0;
1330
1331 8425 fail:
1332 8425 avcodec_free_context(&avctx_new);
1333 8425 avcodec_parameters_free(&par_tmp);
1334
1335 8425 return ret;
1336 }
1337
1338 static int extract_extradata(FFFormatContext *si, AVStream *st, const AVPacket *pkt);
1339
1340 578641 static int read_frame_internal(AVFormatContext *s, AVPacket *pkt)
1341 {
1342 578641 FormatContextInternal *const fci = ff_fc_internal(s);
1343 578641 FFFormatContext *const si = &fci->fc;
1344 578641 int ret, got_packet = 0;
1345 578641 AVDictionary *metadata = NULL;
1346
1347
4/4
✓ Branch 0 taken 1278347 times.
✓ Branch 1 taken 387134 times.
✓ Branch 2 taken 1093279 times.
✓ Branch 3 taken 185068 times.
1665481 while (!got_packet && !fci->parse_queue.head) {
1348 AVStream *st;
1349 FFStream *sti;
1350
1351 /* read next packet */
1352 1093279 ret = ff_read_packet(s, pkt);
1353
2/2
✓ Branch 0 taken 6439 times.
✓ Branch 1 taken 1086840 times.
1093279 if (ret < 0) {
1354
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6438 times.
6439 if (ret == AVERROR(EAGAIN))
1355 1 return ret;
1356 /* flush the parsers */
1357
2/2
✓ Branch 0 taken 7499 times.
✓ Branch 1 taken 6438 times.
13937 for (unsigned i = 0; i < s->nb_streams; i++) {
1358 7499 AVStream *const st = s->streams[i];
1359 7499 FFStream *const sti = ffstream(st);
1360
4/4
✓ Branch 0 taken 2761 times.
✓ Branch 1 taken 4738 times.
✓ Branch 2 taken 2084 times.
✓ Branch 3 taken 677 times.
7499 if (sti->parser && sti->need_parsing)
1361 2084 parse_packet(s, pkt, st->index, 1);
1362 }
1363 /* all remaining packets are now in parse_queue =>
1364 * really terminate parsing */
1365 6438 break;
1366 }
1367 1086840 ret = 0;
1368 1086840 st = s->streams[pkt->stream_index];
1369 1086840 sti = ffstream(st);
1370
1371 1086840 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 1086637 times.
1086840 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 341673 times.
✓ Branch 1 taken 745167 times.
1086840 if (pkt->pts != AV_NOPTS_VALUE &&
1411
2/2
✓ Branch 0 taken 168946 times.
✓ Branch 1 taken 172727 times.
341673 pkt->dts != AV_NOPTS_VALUE &&
1412
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 168946 times.
168946 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 1086840 times.
1086840 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 700869 times.
✓ Branch 1 taken 385971 times.
✓ Branch 2 taken 2777 times.
✓ Branch 3 taken 698092 times.
✓ Branch 4 taken 2777 times.
✗ Branch 5 not taken.
1086840 if (sti->need_parsing && !sti->parser && !(s->flags & AVFMT_FLAG_NOPARSE)) {
1429 2777 sti->parser = av_parser_init(st->codecpar->codec_id);
1430
2/2
✓ Branch 0 taken 1163 times.
✓ Branch 1 taken 1614 times.
2777 if (!sti->parser) {
1431 1163 av_log(s, AV_LOG_VERBOSE, "parser not found for codec "
1432 "%s, packets or times may be invalid.\n",
1433 1163 avcodec_get_name(st->codecpar->codec_id));
1434 /* no parser available: just output the raw packets */
1435 1163 sti->need_parsing = AVSTREAM_PARSE_NONE;
1436
2/2
✓ Branch 0 taken 718 times.
✓ Branch 1 taken 896 times.
1614 } 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 896 times.
896 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 424 times.
896 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 699706 times.
✓ Branch 1 taken 387134 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 699706 times.
1086840 if (!sti->need_parsing || !sti->parser) {
1445 /* no parsing needed: we just output the packet as is */
1446 387134 compute_pkt_fields(s, st, NULL, pkt, AV_NOPTS_VALUE, AV_NOPTS_VALUE);
1447
2/2
✓ Branch 0 taken 93276 times.
✓ Branch 1 taken 293858 times.
387134 if ((s->iformat->flags & AVFMT_GENERIC_INDEX) &&
1448
4/4
✓ Branch 0 taken 92540 times.
✓ Branch 1 taken 736 times.
✓ Branch 2 taken 92395 times.
✓ Branch 3 taken 145 times.
93276 (pkt->flags & AV_PKT_FLAG_KEY) && pkt->dts != AV_NOPTS_VALUE) {
1449 92395 ff_reduce_index(s, st->index);
1450 92395 av_add_index_entry(st, pkt->pos, pkt->dts,
1451 0, 0, AVINDEX_KEYFRAME);
1452 }
1453 387134 got_packet = 1;
1454
2/2
✓ Branch 0 taken 699575 times.
✓ Branch 1 taken 131 times.
699706 } else if (st->discard < AVDISCARD_ALL) {
1455
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 699575 times.
699575 if ((ret = parse_packet(s, pkt, pkt->stream_index, 0)) < 0)
1456 return ret;
1457 699575 st->codecpar->sample_rate = sti->avctx->sample_rate;
1458 699575 st->codecpar->bit_rate = sti->avctx->bit_rate;
1459 699575 ret = av_channel_layout_copy(&st->codecpar->ch_layout, &sti->avctx->ch_layout);
1460
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 699575 times.
699575 if (ret < 0)
1461 return ret;
1462 699575 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 363098 times.
✓ Branch 1 taken 723742 times.
1086840 if (pkt->flags & AV_PKT_FLAG_KEY)
1468 363098 sti->skip_to_keyframe = 0;
1469
2/2
✓ Branch 0 taken 85 times.
✓ Branch 1 taken 1086755 times.
1086840 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 191506 times.
✓ Branch 1 taken 387134 times.
✓ Branch 2 taken 185977 times.
✓ Branch 3 taken 5529 times.
578640 if (!got_packet && fci->parse_queue.head)
1476 185977 ret = avpriv_packet_list_get(&fci->parse_queue, pkt);
1477
1478
2/2
✓ Branch 0 taken 573111 times.
✓ Branch 1 taken 5529 times.
578640 if (ret >= 0) {
1479 573111 AVStream *const st = s->streams[pkt->stream_index];
1480 573111 FFStream *const sti = ffstream(st);
1481 573111 int discard_padding = 0;
1482
3/4
✓ Branch 0 taken 4387 times.
✓ Branch 1 taken 568724 times.
✓ Branch 2 taken 4387 times.
✗ Branch 3 not taken.
573111 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 568724 times.
✓ Branch 2 taken 4371 times.
✓ Branch 3 taken 16 times.
✓ Branch 4 taken 20 times.
✓ Branch 5 taken 4351 times.
573111 if (sti->start_skip_samples && (pkt->pts == 0 || pkt->pts == RELATIVE_TS_BASE))
1492 36 sti->skip_samples = sti->start_skip_samples;
1493 573111 sti->skip_samples = FFMAX(0, sti->skip_samples);
1494
4/4
✓ Branch 0 taken 573023 times.
✓ Branch 1 taken 88 times.
✓ Branch 2 taken 14 times.
✓ Branch 3 taken 573009 times.
573111 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 7696 times.
✓ Branch 1 taken 570944 times.
578640 if (!fci->metafree) {
1507 7696 int metaret = av_opt_get_dict_val(s, "metadata", AV_OPT_SEARCH_CHILDREN, &metadata);
1508
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7696 times.
7696 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 7696 fci->metafree = metaret == AVERROR_OPTION_NOT_FOUND;
1515 }
1516
1517
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 578640 times.
578640 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 5417 times.
✓ Branch 1 taken 573223 times.
✓ Branch 2 taken 5194 times.
✓ Branch 3 taken 223 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 5194 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
578640 if (ret == AVERROR_EOF && s->pb && s->pb->error < 0 && s->pb->error != AVERROR(EAGAIN))
1529 ret = s->pb->error;
1530
1531 578640 return ret;
1532 }
1533
1534 517961 int av_read_frame(AVFormatContext *s, AVPacket *pkt)
1535 {
1536 517961 FFFormatContext *const si = ffformatcontext(s);
1537 517961 const int genpts = s->flags & AVFMT_FLAG_GENPTS;
1538 517961 int eof = 0;
1539 int ret;
1540 AVStream *st;
1541
1542
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 517961 times.
517961 if (!genpts) {
1543 1035922 ret = si->packet_buffer.head
1544 135530 ? avpriv_packet_list_get(&si->packet_buffer, pkt)
1545
2/2
✓ Branch 0 taken 135530 times.
✓ Branch 1 taken 382431 times.
517961 : read_frame_internal(s, pkt);
1546
2/2
✓ Branch 0 taken 4337 times.
✓ Branch 1 taken 513624 times.
517961 if (ret < 0)
1547 4337 return ret;
1548 513624 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 513624 return_packet:
1615 513624 st = s->streams[pkt->stream_index];
1616
4/4
✓ Branch 0 taken 194508 times.
✓ Branch 1 taken 319116 times.
✓ Branch 2 taken 120770 times.
✓ Branch 3 taken 73738 times.
513624 if ((s->iformat->flags & AVFMT_GENERIC_INDEX) && pkt->flags & AV_PKT_FLAG_KEY) {
1617 120770 ff_reduce_index(s, st->index);
1618 120770 av_add_index_entry(st, pkt->pos, pkt->dts, 0, 0, AVINDEX_KEYFRAME);
1619 }
1620
1621
2/2
✓ Branch 1 taken 120353 times.
✓ Branch 2 taken 393271 times.
513624 if (is_relative(pkt->dts))
1622 120353 pkt->dts -= RELATIVE_TS_BASE;
1623
2/2
✓ Branch 1 taken 118659 times.
✓ Branch 2 taken 394965 times.
513624 if (is_relative(pkt->pts))
1624 118659 pkt->pts -= RELATIVE_TS_BASE;
1625
1626 513624 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 7619 static int has_duration(AVFormatContext *ic)
1635 {
1636
2/2
✓ Branch 0 taken 7920 times.
✓ Branch 1 taken 2440 times.
10360 for (unsigned i = 0; i < ic->nb_streams; i++) {
1637 7920 const AVStream *const st = ic->streams[i];
1638
2/2
✓ Branch 0 taken 5179 times.
✓ Branch 1 taken 2741 times.
7920 if (st->duration != AV_NOPTS_VALUE)
1639 5179 return 1;
1640 }
1641
2/2
✓ Branch 0 taken 441 times.
✓ Branch 1 taken 1999 times.
2440 if (ic->duration != AV_NOPTS_VALUE)
1642 441 return 1;
1643 1999 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 13377 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 13377 start_time = INT64_MAX;
1657 13377 start_time_text = INT64_MAX;
1658 13377 end_time = INT64_MIN;
1659 13377 end_time_text = INT64_MIN;
1660 13377 duration = INT64_MIN;
1661 13377 duration_text = INT64_MIN;
1662
1663
2/2
✓ Branch 0 taken 14867 times.
✓ Branch 1 taken 13377 times.
28244 for (unsigned i = 0; i < ic->nb_streams; i++) {
1664 14867 AVStream *const st = ic->streams[i];
1665
2/2
✓ Branch 0 taken 14742 times.
✓ Branch 1 taken 125 times.
29609 int is_text = st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE ||
1666
2/2
✓ Branch 0 taken 151 times.
✓ Branch 1 taken 14591 times.
14742 st->codecpar->codec_type == AVMEDIA_TYPE_DATA;
1667
1668
3/4
✓ Branch 0 taken 12203 times.
✓ Branch 1 taken 2664 times.
✓ Branch 2 taken 12203 times.
✗ Branch 3 not taken.
14867 if (st->start_time != AV_NOPTS_VALUE && st->time_base.den) {
1669 12203 start_time1 = av_rescale_q(st->start_time, st->time_base,
1670 12203 AV_TIME_BASE_Q);
1671
2/2
✓ Branch 0 taken 158 times.
✓ Branch 1 taken 12045 times.
12203 if (is_text)
1672 158 start_time_text = FFMIN(start_time_text, start_time1);
1673 else
1674 12045 start_time = FFMIN(start_time, start_time1);
1675 12203 end_time1 = av_rescale_q_rnd(st->duration, st->time_base,
1676 12203 AV_TIME_BASE_Q,
1677 AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
1678
5/6
✓ Branch 0 taken 10788 times.
✓ Branch 1 taken 1415 times.
✓ Branch 2 taken 10759 times.
✓ Branch 3 taken 29 times.
✓ Branch 4 taken 10788 times.
✗ Branch 5 not taken.
12203 if (end_time1 != AV_NOPTS_VALUE && (end_time1 > 0 ? start_time1 <= INT64_MAX - end_time1 : start_time1 >= INT64_MIN - end_time1)) {
1679 10788 end_time1 += start_time1;
1680
2/2
✓ Branch 0 taken 143 times.
✓ Branch 1 taken 10645 times.
10788 if (is_text)
1681 143 end_time_text = FFMAX(end_time_text, end_time1);
1682 else
1683 10645 end_time = FFMAX(end_time, end_time1);
1684 }
1685
2/2
✓ Branch 1 taken 208 times.
✓ Branch 2 taken 12203 times.
12411 for (AVProgram *p = NULL; (p = av_find_program_from_stream(ic, p, i)); ) {
1686
4/4
✓ Branch 0 taken 152 times.
✓ Branch 1 taken 56 times.
✓ Branch 2 taken 25 times.
✓ Branch 3 taken 127 times.
208 if (p->start_time == AV_NOPTS_VALUE || p->start_time > start_time1)
1687 81 p->start_time = start_time1;
1688
2/2
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 156 times.
208 if (p->end_time < end_time1)
1689 52 p->end_time = end_time1;
1690 }
1691 }
1692
2/2
✓ Branch 0 taken 12250 times.
✓ Branch 1 taken 2617 times.
14867 if (st->duration != AV_NOPTS_VALUE) {
1693 12250 duration1 = av_rescale_q(st->duration, st->time_base,
1694 12250 AV_TIME_BASE_Q);
1695
2/2
✓ Branch 0 taken 167 times.
✓ Branch 1 taken 12083 times.
12250 if (is_text)
1696 167 duration_text = FFMAX(duration_text, duration1);
1697 else
1698 12083 duration = FFMAX(duration, duration1);
1699 }
1700 }
1701
3/6
✓ Branch 0 taken 10928 times.
✓ Branch 1 taken 2449 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 10928 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
13377 if (start_time == INT64_MAX || (start_time > start_time_text && start_time - (uint64_t)start_time_text < AV_TIME_BASE))
1702 2449 start_time = start_time_text;
1703
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10928 times.
10928 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 9938 times.
✓ Branch 1 taken 3439 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 9936 times.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
13377 if (end_time == INT64_MIN || (end_time < end_time_text && end_time_text - (uint64_t)end_time < AV_TIME_BASE))
1707 3441 end_time = end_time_text;
1708
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9936 times.
9936 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 11361 times.
✓ Branch 1 taken 2016 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 11355 times.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
13377 if (duration == INT64_MIN || (duration < duration_text && (uint64_t)duration_text - duration < AV_TIME_BASE))
1712 2022 duration = duration_text;
1713
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11355 times.
11355 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 10949 times.
✓ Branch 1 taken 2428 times.
13377 if (start_time != INT64_MAX) {
1717 10949 ic->start_time = start_time;
1718
2/2
✓ Branch 0 taken 9955 times.
✓ Branch 1 taken 994 times.
10949 if (end_time != INT64_MIN) {
1719
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9955 times.
9955 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 9955 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9955 times.
✗ Branch 3 not taken.
9955 } else if (end_time >= start_time && end_time - (uint64_t)start_time <= INT64_MAX) {
1729 9955 duration = FFMAX(duration, end_time - start_time);
1730 }
1731 }
1732 }
1733
6/6
✓ Branch 0 taken 11391 times.
✓ Branch 1 taken 1986 times.
✓ Branch 2 taken 11355 times.
✓ Branch 3 taken 36 times.
✓ Branch 4 taken 6094 times.
✓ Branch 5 taken 5261 times.
13377 if (duration != INT64_MIN && duration > 0 && ic->duration == AV_NOPTS_VALUE) {
1734 6094 ic->duration = duration;
1735 }
1736
5/6
✓ Branch 0 taken 7111 times.
✓ Branch 1 taken 6266 times.
✓ Branch 3 taken 7111 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 5988 times.
✓ Branch 6 taken 1123 times.
13377 if (ic->pb && (filesize = avio_size(ic->pb)) > 0 && ic->duration > 0) {
1737 /* compute the bitrate */
1738 5988 double bitrate = (double) filesize * 8.0 * AV_TIME_BASE /
1739 5988 (double) ic->duration;
1740
2/4
✓ Branch 0 taken 5988 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5988 times.
✗ Branch 3 not taken.
5988 if (bitrate >= 0 && bitrate <= INT64_MAX)
1741 5988 ic->bit_rate = bitrate;
1742 }
1743 13377 }
1744
1745 5689 static void fill_all_stream_timings(AVFormatContext *ic)
1746 {
1747 5689 update_stream_timings(ic);
1748
2/2
✓ Branch 0 taken 6358 times.
✓ Branch 1 taken 5689 times.
12047 for (unsigned i = 0; i < ic->nb_streams; i++) {
1749 6358 AVStream *const st = ic->streams[i];
1750
1751
2/2
✓ Branch 0 taken 774 times.
✓ Branch 1 taken 5584 times.
6358 if (st->start_time == AV_NOPTS_VALUE) {
1752
2/2
✓ Branch 0 taken 129 times.
✓ Branch 1 taken 645 times.
774 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 759 times.
✓ Branch 1 taken 15 times.
774 if (ic->duration != AV_NOPTS_VALUE)
1756 759 st->duration = av_rescale_q(ic->duration, AV_TIME_BASE_Q,
1757 st->time_base);
1758 }
1759 }
1760 5689 }
1761
1762 1999 static void estimate_timings_from_bit_rate(AVFormatContext *ic)
1763 {
1764 1999 FFFormatContext *const si = ffformatcontext(ic);
1765 1999 int show_warning = 0;
1766
1767 /* if bit_rate is already set, we believe it */
1768
2/2
✓ Branch 0 taken 1964 times.
✓ Branch 1 taken 35 times.
1999 if (ic->bit_rate <= 0) {
1769 1964 int64_t bit_rate = 0;
1770
2/2
✓ Branch 0 taken 2093 times.
✓ Branch 1 taken 1314 times.
3407 for (unsigned i = 0; i < ic->nb_streams; i++) {
1771 2093 const AVStream *const st = ic->streams[i];
1772 2093 const FFStream *const sti = cffstream(st);
1773
4/4
✓ Branch 0 taken 1316 times.
✓ Branch 1 taken 777 times.
✓ Branch 2 taken 112 times.
✓ Branch 3 taken 1204 times.
2093 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 889 times.
✓ Branch 1 taken 1204 times.
2093 if (st->codecpar->bit_rate > 0) {
1776
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 889 times.
889 if (INT64_MAX - st->codecpar->bit_rate < bit_rate) {
1777 bit_rate = 0;
1778 break;
1779 }
1780 889 bit_rate += st->codecpar->bit_rate;
1781
4/4
✓ Branch 0 taken 1036 times.
✓ Branch 1 taken 168 times.
✓ Branch 2 taken 650 times.
✓ Branch 3 taken 386 times.
1204 } 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 650 bit_rate = 0;
1785 650 break;
1786 }
1787 }
1788 1964 ic->bit_rate = bit_rate;
1789 }
1790
1791 /* if duration is already set, we believe it */
1792
1/2
✓ Branch 0 taken 1999 times.
✗ Branch 1 not taken.
1999 if (ic->duration == AV_NOPTS_VALUE &&
1793
2/2
✓ Branch 0 taken 896 times.
✓ Branch 1 taken 1103 times.
1999 ic->bit_rate != 0) {
1794
2/2
✓ Branch 0 taken 870 times.
✓ Branch 1 taken 26 times.
896 int64_t filesize = ic->pb ? avio_size(ic->pb) : 0;
1795
2/2
✓ Branch 0 taken 864 times.
✓ Branch 1 taken 32 times.
896 if (filesize > si->data_offset) {
1796 864 filesize -= si->data_offset;
1797
2/2
✓ Branch 0 taken 901 times.
✓ Branch 1 taken 864 times.
1765 for (unsigned i = 0; i < ic->nb_streams; i++) {
1798 901 AVStream *const st = ic->streams[i];
1799
1800
1/2
✓ Branch 0 taken 901 times.
✗ Branch 1 not taken.
901 if ( st->time_base.num <= INT64_MAX / ic->bit_rate
1801
1/2
✓ Branch 0 taken 901 times.
✗ Branch 1 not taken.
901 && st->duration == AV_NOPTS_VALUE) {
1802 901 st->duration = av_rescale(filesize, 8LL * st->time_base.den,
1803 901 ic->bit_rate *
1804 901 (int64_t) st->time_base.num);
1805 901 show_warning = 1;
1806 }
1807 }
1808 }
1809 }
1810
2/2
✓ Branch 0 taken 864 times.
✓ Branch 1 taken 1135 times.
1999 if (show_warning)
1811 864 av_log(ic, AV_LOG_WARNING,
1812 "Estimating duration from bitrate, this may be inaccurate\n");
1813 1999 }
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 69 static void estimate_timings_from_pts(AVFormatContext *ic, int64_t old_offset)
1821 {
1822 69 FFFormatContext *const si = ffformatcontext(ic);
1823 69 AVPacket *const pkt = si->pkt;
1824 int num, den, read_size, ret;
1825
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 69 times.
69 int64_t duration_max_read_size = ic->duration_probesize ? ic->duration_probesize >> DURATION_MAX_RETRY : DURATION_DEFAULT_MAX_READ_SIZE;
1826
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 69 times.
69 int duration_max_retry = ic->duration_probesize ? DURATION_MAX_RETRY : DURATION_DEFAULT_MAX_RETRY;
1827 69 int found_duration = 0;
1828 int is_end;
1829 int64_t filesize, offset, duration;
1830 69 int retry = 0;
1831
1832 /* flush packet queue */
1833 69 ff_flush_packet_queue(ic);
1834
1835
2/2
✓ Branch 0 taken 143 times.
✓ Branch 1 taken 69 times.
212 for (unsigned i = 0; i < ic->nb_streams; i++) {
1836 143 AVStream *const st = ic->streams[i];
1837 143 FFStream *const sti = ffstream(st);
1838
1839
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 121 times.
143 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 24 times.
✓ Branch 1 taken 119 times.
143 if (sti->parser) {
1846 24 av_parser_close(sti->parser);
1847 24 sti->parser = NULL;
1848 }
1849 }
1850
1851
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 69 times.
69 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 69 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 69 times.
✗ Branch 1 not taken.
69 filesize = ic->pb ? avio_size(ic->pb) : 0;
1860 do {
1861 76 is_end = found_duration;
1862 76 offset = filesize - (duration_max_read_size << retry);
1863
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 50 times.
76 if (offset < 0)
1864 26 offset = 0;
1865
1866 76 avio_seek(ic->pb, offset, SEEK_SET);
1867 76 read_size = 0;
1868 6554 for (;;) {
1869 AVStream *st;
1870 FFStream *sti;
1871
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 6623 times.
6630 if (read_size >= duration_max_read_size << (FFMAX(retry - 1, 0)))
1872 7 break;
1873
1874 do {
1875 6623 ret = ff_read_packet(ic, pkt);
1876
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6623 times.
6623 } while (ret == AVERROR(EAGAIN));
1877
2/2
✓ Branch 0 taken 69 times.
✓ Branch 1 taken 6554 times.
6623 if (ret != 0)
1878 69 break;
1879 6554 read_size += pkt->size;
1880 6554 st = ic->streams[pkt->stream_index];
1881 6554 sti = ffstream(st);
1882
2/2
✓ Branch 0 taken 2805 times.
✓ Branch 1 taken 3749 times.
6554 if (pkt->pts != AV_NOPTS_VALUE &&
1883
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2805 times.
2805 (st->start_time != AV_NOPTS_VALUE ||
1884 sti->first_dts != AV_NOPTS_VALUE)) {
1885
1/2
✓ Branch 0 taken 2805 times.
✗ Branch 1 not taken.
2805 if (pkt->duration == 0) {
1886 2805 compute_frame_duration(ic, &num, &den, st, sti->parser, pkt);
1887
3/4
✓ Branch 0 taken 2345 times.
✓ Branch 1 taken 460 times.
✓ Branch 2 taken 2345 times.
✗ Branch 3 not taken.
2805 if (den && num) {
1888 2345 pkt->duration = av_rescale_rnd(1,
1889 2345 num * (int64_t) st->time_base.den,
1890 2345 den * (int64_t) st->time_base.num,
1891 AV_ROUND_DOWN);
1892 }
1893 }
1894 2805 duration = pkt->pts + pkt->duration;
1895 2805 found_duration = 1;
1896
1/2
✓ Branch 0 taken 2805 times.
✗ Branch 1 not taken.
2805 if (st->start_time != AV_NOPTS_VALUE)
1897 2805 duration -= st->start_time;
1898 else
1899 duration -= sti->first_dts;
1900
2/2
✓ Branch 0 taken 2796 times.
✓ Branch 1 taken 9 times.
2805 if (duration > 0) {
1901
3/4
✓ Branch 0 taken 2685 times.
✓ Branch 1 taken 111 times.
✓ Branch 2 taken 2685 times.
✗ Branch 3 not taken.
2796 if (st->duration == AV_NOPTS_VALUE || sti->info->last_duration<= 0 ||
1902
3/4
✓ Branch 0 taken 2431 times.
✓ Branch 1 taken 254 times.
✓ Branch 2 taken 2431 times.
✗ Branch 3 not taken.
2685 (st->duration < duration && FFABS(duration - sti->info->last_duration) < 60LL*st->time_base.den / st->time_base.num))
1903 2542 st->duration = duration;
1904 2796 sti->info->last_duration = duration;
1905 }
1906 }
1907 6554 av_packet_unref(pkt);
1908 }
1909
1910 /* check if all audio/video streams have valid duration */
1911
2/2
✓ Branch 0 taken 69 times.
✓ Branch 1 taken 7 times.
76 if (!is_end) {
1912 69 is_end = 1;
1913
2/2
✓ Branch 0 taken 143 times.
✓ Branch 1 taken 69 times.
212 for (unsigned i = 0; i < ic->nb_streams; i++) {
1914 143 const AVStream *const st = ic->streams[i];
1915
2/2
✓ Branch 0 taken 125 times.
✓ Branch 1 taken 18 times.
143 switch (st->codecpar->codec_type) {
1916 125 case AVMEDIA_TYPE_VIDEO:
1917 case AVMEDIA_TYPE_AUDIO:
1918
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 101 times.
125 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 67 times.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
83 offset &&
1925 ++retry <= duration_max_retry);
1926
1927 69 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 143 times.
✓ Branch 1 taken 69 times.
212 for (unsigned i = 0; i < ic->nb_streams; i++) {
1931 143 const AVStream *const st = ic->streams[i];
1932 143 const FFStream *const sti = cffstream(st);
1933
1934
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 111 times.
143 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 69 skip_duration_calc:
1946 69 fill_all_stream_timings(ic);
1947
1948 69 avio_seek(ic->pb, old_offset, SEEK_SET);
1949
2/2
✓ Branch 0 taken 143 times.
✓ Branch 1 taken 69 times.
212 for (unsigned i = 0; i < ic->nb_streams; i++) {
1950 143 AVStream *const st = ic->streams[i];
1951 143 FFStream *const sti = ffstream(st);
1952
1953 143 sti->cur_dts = sti->first_dts;
1954 143 sti->last_IP_pts = AV_NOPTS_VALUE;
1955 143 sti->last_dts_for_order_check = AV_NOPTS_VALUE;
1956
2/2
✓ Branch 0 taken 2431 times.
✓ Branch 1 taken 143 times.
2574 for (int j = 0; j < MAX_REORDER_DELAY + 1; j++)
1957 2431 sti->pts_buffer[j] = AV_NOPTS_VALUE;
1958 }
1959 69 }
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 7688 static const char *duration_estimate_name(enum AVDurationEstimationMethod method)
1969 {
1970 7688 return duration_name[method];
1971 }
1972
1973 7688 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 3182 times.
✓ Branch 1 taken 4506 times.
7688 if (ic->iformat->flags & AVFMT_NOFILE) {
1979 3182 file_size = 0;
1980 } else {
1981 4506 file_size = avio_size(ic->pb);
1982 4506 file_size = FFMAX(0, file_size);
1983 }
1984
1985
2/2
✓ Branch 0 taken 7663 times.
✓ Branch 1 taken 25 times.
7688 if ((!strcmp(ic->iformat->name, "mpeg") ||
1986
3/4
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 7619 times.
✓ Branch 2 taken 69 times.
✗ Branch 3 not taken.
7688 !strcmp(ic->iformat->name, "mpegts")) &&
1987
1/2
✓ Branch 0 taken 69 times.
✗ Branch 1 not taken.
69 file_size && (ic->pb->seekable & AVIO_SEEKABLE_NORMAL)) {
1988 /* get accurate estimate from the PTSes */
1989 69 estimate_timings_from_pts(ic, old_offset);
1990 69 ic->duration_estimation_method = AVFMT_DURATION_FROM_PTS;
1991
2/2
✓ Branch 1 taken 5620 times.
✓ Branch 2 taken 1999 times.
7619 } else if (has_duration(ic)) {
1992 /* at least one component has timings - we use them for all
1993 * the components */
1994 5620 fill_all_stream_timings(ic);
1995 /* nut demuxer estimate the duration from PTS */
1996
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 5590 times.
5620 if (!strcmp(ic->iformat->name, "nut"))
1997 30 ic->duration_estimation_method = AVFMT_DURATION_FROM_PTS;
1998 else
1999 5590 ic->duration_estimation_method = AVFMT_DURATION_FROM_STREAM;
2000 } else {
2001 /* less precise: use bitrate info */
2002 1999 estimate_timings_from_bit_rate(ic);
2003 1999 ic->duration_estimation_method = AVFMT_DURATION_FROM_BITRATE;
2004 }
2005 7688 update_stream_timings(ic);
2006
2007
2/2
✓ Branch 0 taken 8509 times.
✓ Branch 1 taken 7688 times.
16197 for (unsigned i = 0; i < ic->nb_streams; i++) {
2008 8509 AVStream *const st = ic->streams[i];
2009
1/2
✓ Branch 0 taken 8509 times.
✗ Branch 1 not taken.
8509 if (st->time_base.den)
2010 8509 av_log(ic, AV_LOG_TRACE, "stream %u: start_time: %s duration: %s\n", i,
2011 8509 av_ts2timestr(st->start_time, &st->time_base),
2012 8509 av_ts2timestr(st->duration, &st->time_base));
2013 }
2014 7688 av_log(ic, AV_LOG_TRACE,
2015 "format: start_time: %s duration: %s (estimate from %s) bitrate=%"PRId64" kb/s\n",
2016 7688 av_ts2timestr(ic->start_time, &AV_TIME_BASE_Q),
2017 7688 av_ts2timestr(ic->duration, &AV_TIME_BASE_Q),
2018 duration_estimate_name(ic->duration_estimation_method),
2019 7688 (int64_t)ic->bit_rate / 1000);
2020 7688 }
2021
2022 104510 static int determinable_frame_size(const AVCodecContext *avctx)
2023 {
2024
2/2
✓ Branch 0 taken 248 times.
✓ Branch 1 taken 104262 times.
104510 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 104262 return 0;
2033 }
2034
2035 429038 static int has_codec_parameters(const AVStream *st, const char **errmsg_ptr)
2036 {
2037 429038 const FFStream *const sti = cffstream(st);
2038 429038 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 644 times.
✓ Branch 1 taken 428394 times.
429038 if ( avctx->codec_id == AV_CODEC_ID_NONE
2047
2/2
✓ Branch 0 taken 419 times.
✓ Branch 1 taken 225 times.
644 && 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 119889 times.
✓ Branch 1 taken 307188 times.
✓ Branch 2 taken 629 times.
✓ Branch 3 taken 913 times.
✗ Branch 4 not taken.
428619 switch (avctx->codec_type) {
2050 119889 case AVMEDIA_TYPE_AUDIO:
2051
4/4
✓ Branch 0 taken 104510 times.
✓ Branch 1 taken 15379 times.
✓ Branch 3 taken 248 times.
✓ Branch 4 taken 104262 times.
119889 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 119641 times.
✗ Branch 1 not taken.
119641 if (sti->info->found_decoder >= 0 &&
2054
2/2
✓ Branch 0 taken 2443 times.
✓ Branch 1 taken 117198 times.
119641 avctx->sample_fmt == AV_SAMPLE_FMT_NONE)
2055
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2443 times.
2443 FAIL("unspecified sample format");
2056
2/2
✓ Branch 0 taken 137 times.
✓ Branch 1 taken 117061 times.
117198 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 117044 times.
117061 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 117044 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 80914 times.
✓ Branch 3 taken 36130 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 80914 times.
117044 if (sti->info->found_decoder >= 0 && !sti->nb_decoded_frames && avctx->codec_id == AV_CODEC_ID_DTS)
2061 FAIL("no decodable DTS frames");
2062 117044 break;
2063 307188 case AVMEDIA_TYPE_VIDEO:
2064
2/2
✓ Branch 0 taken 13089 times.
✓ Branch 1 taken 294099 times.
307188 if (!avctx->width)
2065
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 13079 times.
13089 FAIL("unspecified size");
2066
4/4
✓ Branch 0 taken 294044 times.
✓ Branch 1 taken 55 times.
✓ Branch 2 taken 4270 times.
✓ Branch 3 taken 289774 times.
294099 if (sti->info->found_decoder >= 0 && avctx->pix_fmt == AV_PIX_FMT_NONE)
2067
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 4267 times.
4270 FAIL("unspecified pixel format");
2068
4/4
✓ Branch 0 taken 289729 times.
✓ Branch 1 taken 100 times.
✓ Branch 2 taken 86 times.
✓ Branch 3 taken 289643 times.
289829 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 289762 break;
2072 629 case AVMEDIA_TYPE_SUBTITLE:
2073
4/4
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 594 times.
✓ Branch 2 taken 21 times.
✓ Branch 3 taken 14 times.
629 if (avctx->codec_id == AV_CODEC_ID_HDMV_PGS_SUBTITLE && !avctx->width)
2074
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 FAIL("unspecified size");
2075 608 break;
2076 913 case AVMEDIA_TYPE_DATA:
2077
2/2
✓ Branch 0 taken 225 times.
✓ Branch 1 taken 688 times.
913 if (avctx->codec_id == AV_CODEC_ID_NONE) return 1;
2078 }
2079
2080 408102 return 1;
2081 }
2082
2083 /* returns 1 or 0 if or if not decoded data was returned, or a negative error */
2084 199573 static int try_decode_frame(AVFormatContext *s, AVStream *st,
2085 const AVPacket *pkt, AVDictionary **options)
2086 {
2087 199573 FFStream *const sti = ffstream(st);
2088 199573 AVCodecContext *const avctx = sti->avctx;
2089 const AVCodec *codec;
2090 199573 int got_picture = 1, ret = 0;
2091 199573 AVFrame *frame = av_frame_alloc();
2092 AVSubtitle subtitle;
2093 199573 int do_skip_frame = 0;
2094 enum AVDiscard skip_frame;
2095 199573 int pkt_to_send = pkt->size > 0;
2096
2097
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 199573 times.
199573 if (!frame)
2098 return AVERROR(ENOMEM);
2099
2100
2/2
✓ Branch 1 taken 1480 times.
✓ Branch 2 taken 198093 times.
199573 if (!avcodec_is_open(avctx) &&
2101
1/2
✓ Branch 0 taken 1480 times.
✗ Branch 1 not taken.
1480 sti->info->found_decoder <= 0 &&
2102
4/4
✓ Branch 0 taken 96 times.
✓ Branch 1 taken 1384 times.
✓ Branch 2 taken 63 times.
✓ Branch 3 taken 33 times.
1480 (st->codecpar->codec_id != -sti->info->found_decoder || !st->codecpar->codec_id)) {
2103 1447 AVDictionary *thread_opt = NULL;
2104
2105 1447 codec = find_probe_decoder(s, st, st->codecpar->codec_id);
2106
2107
2/2
✓ Branch 0 taken 76 times.
✓ Branch 1 taken 1371 times.
1447 if (!codec) {
2108 76 sti->info->found_decoder = -st->codecpar->codec_id;
2109 76 ret = -1;
2110 78 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 1208 times.
1371 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 1208 times.
1371 av_dict_set(options ? options : &thread_opt, "lowres", "0", 0);
2120
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1371 times.
1371 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 1208 times.
1371 ret = avcodec_open2(avctx, codec, options ? options : &thread_opt);
2123
2/2
✓ Branch 0 taken 163 times.
✓ Branch 1 taken 1208 times.
1371 if (!options)
2124 163 av_dict_free(&thread_opt);
2125
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1369 times.
1371 if (ret < 0) {
2126 2 sti->info->found_decoder = -avctx->codec_id;
2127 2 goto fail;
2128 }
2129 1369 sti->info->found_decoder = 1;
2130
2/2
✓ Branch 0 taken 6859 times.
✓ Branch 1 taken 191267 times.
198126 } else if (!sti->info->found_decoder)
2131 6859 sti->info->found_decoder = 1;
2132
2133
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 199462 times.
199495 if (sti->info->found_decoder < 0) {
2134 33 ret = -1;
2135 33 goto fail;
2136 }
2137
2138
2/2
✓ Branch 1 taken 111499 times.
✓ Branch 2 taken 87963 times.
199462 if (avpriv_codec_get_cap_skip_frame_fill_param(avctx->codec)) {
2139 111499 do_skip_frame = 1;
2140 111499 skip_frame = avctx->skip_frame;
2141 111499 avctx->skip_frame = AVDISCARD_ALL;
2142 }
2143
2144
5/6
✓ Branch 0 taken 8109 times.
✓ Branch 1 taken 4685 times.
✓ Branch 2 taken 26 times.
✓ Branch 3 taken 4659 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 199470 times.
407067 while ((pkt_to_send || (!pkt->data && got_picture)) &&
2145
4/4
✓ Branch 0 taken 12794 times.
✓ Branch 1 taken 194811 times.
✓ Branch 2 taken 5682 times.
✓ Branch 3 taken 193788 times.
407075 ret >= 0 &&
2146
2/2
✓ Branch 2 taken 2241 times.
✓ Branch 3 taken 191547 times.
393258 (!has_codec_parameters(st, NULL) || !has_decode_delay_been_guessed(st) ||
2147
2/2
✓ Branch 0 taken 188714 times.
✓ Branch 1 taken 2833 times.
191547 (!sti->codec_info_nb_frames &&
2148
2/2
✓ Branch 0 taken 460 times.
✓ Branch 1 taken 2373 times.
2833 (avctx->codec->capabilities & AV_CODEC_CAP_CHANNEL_CONF)))) {
2149 8383 got_picture = 0;
2150
2/2
✓ Branch 0 taken 744 times.
✓ Branch 1 taken 7639 times.
8383 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO ||
2151
2/2
✓ Branch 0 taken 737 times.
✓ Branch 1 taken 7 times.
744 avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
2152 8376 ret = avcodec_send_packet(avctx, pkt);
2153
5/6
✓ Branch 0 taken 249 times.
✓ Branch 1 taken 8127 times.
✓ Branch 2 taken 249 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 240 times.
✓ Branch 5 taken 9 times.
8376 if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
2154 240 break;
2155
2/2
✓ Branch 0 taken 8127 times.
✓ Branch 1 taken 9 times.
8136 if (ret >= 0)
2156 8127 pkt_to_send = 0;
2157 8136 ret = avcodec_receive_frame(avctx, frame);
2158
2/2
✓ Branch 0 taken 3177 times.
✓ Branch 1 taken 4959 times.
8136 if (ret >= 0)
2159 3177 got_picture = 1;
2160
4/4
✓ Branch 0 taken 3203 times.
✓ Branch 1 taken 4933 times.
✓ Branch 2 taken 26 times.
✓ Branch 3 taken 3177 times.
8136 if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
2161 4959 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 8143 times.
✗ Branch 1 not taken.
8143 if (ret >= 0) {
2171
2/2
✓ Branch 0 taken 3179 times.
✓ Branch 1 taken 4964 times.
8143 if (got_picture)
2172 3179 sti->nb_decoded_frames++;
2173 8143 ret = got_picture;
2174 }
2175 }
2176
2177 199222 fail:
2178
2/2
✓ Branch 0 taken 111499 times.
✓ Branch 1 taken 88074 times.
199573 if (do_skip_frame) {
2179 111499 avctx->skip_frame = skip_frame;
2180 }
2181
2182 199573 av_frame_free(&frame);
2183 199573 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 7688 static int compute_chapters_end(AVFormatContext *s)
2197 {
2198 7688 int64_t max_time = 0;
2199 AVChapter **timetable;
2200
2201
2/2
✓ Branch 0 taken 7662 times.
✓ Branch 1 taken 26 times.
7688 if (!s->nb_chapters)
2202 7662 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 19288376 static int get_std_framerate(int i)
2234 {
2235
2/2
✓ Branch 0 taken 17525593 times.
✓ Branch 1 taken 1762783 times.
19288376 if (i < 30*12)
2236 17525593 return (i + 1) * 1001;
2237 1762783 i -= 30*12;
2238
2239
2/2
✓ Branch 0 taken 1341243 times.
✓ Branch 1 taken 421540 times.
1762783 if (i < 30)
2240 1341243 return (i + 31) * 1001 * 12;
2241 421540 i -= 30;
2242
2243
2/2
✓ Branch 0 taken 134145 times.
✓ Branch 1 taken 287395 times.
421540 if (i < 3)
2244 134145 return ((const int[]) { 80, 120, 240})[i] * 1001 * 12;
2245
2246 287395 i -= 3;
2247
2248 287395 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 210898 static int tb_unreliable(AVFormatContext *ic, AVStream *st)
2258 {
2259 210898 FFStream *const sti = ffstream(st);
2260 210898 const AVCodecDescriptor *desc = sti->codec_desc;
2261 210898 AVCodecContext *c = sti->avctx;
2262
4/4
✓ Branch 0 taken 210434 times.
✓ Branch 1 taken 464 times.
✓ Branch 2 taken 21029 times.
✓ Branch 3 taken 189405 times.
210898 AVRational mul = (AVRational){ desc && (desc->props & AV_CODEC_PROP_FIELDS) ? 2 : 1, 1 };
2263 210898 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 17686 times.
✓ Branch 1 taken 193212 times.
210898 : (((ic->ctx_flags & AVFMTCTX_NOHEADER) ||
2266
2/2
✓ Branch 0 taken 44493 times.
✓ Branch 1 taken 30613 times.
75106 st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) ? (AVRational){0, 1}
2267
2/2
✓ Branch 0 taken 75106 times.
✓ Branch 1 taken 118106 times.
193212 : st->time_base);
2268
2269
2/2
✓ Branch 0 taken 24073 times.
✓ Branch 1 taken 186825 times.
210898 if (time_base.den >= 101LL * time_base.num ||
2270
2/2
✓ Branch 0 taken 23638 times.
✓ Branch 1 taken 435 times.
24073 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 23539 times.
✓ Branch 1 taken 99 times.
23638 c->codec_tag == AV_RL32("mp4v") ||
2274
2/2
✓ Branch 0 taken 14710 times.
✓ Branch 1 taken 8829 times.
23539 c->codec_id == AV_CODEC_ID_MPEG2VIDEO ||
2275
2/2
✓ Branch 0 taken 14180 times.
✓ Branch 1 taken 530 times.
14710 c->codec_id == AV_CODEC_ID_GIF ||
2276
2/2
✓ Branch 0 taken 13475 times.
✓ Branch 1 taken 705 times.
14180 c->codec_id == AV_CODEC_ID_HEVC ||
2277
2/2
✓ Branch 0 taken 3483 times.
✓ Branch 1 taken 9992 times.
13475 c->codec_id == AV_CODEC_ID_H264)
2278 200906 return 1;
2279 9992 return 0;
2280 }
2281
2282 145428 int ff_rfps_add_frame(AVFormatContext *ic, AVStream *st, int64_t ts)
2283 {
2284 145428 FFStream *const sti = ffstream(st);
2285 145428 FFStreamInfo *info = sti->info;
2286 145428 int64_t last = info->last_dts;
2287
2288
6/6
✓ Branch 0 taken 129232 times.
✓ Branch 1 taken 16196 times.
✓ Branch 2 taken 123099 times.
✓ Branch 3 taken 6133 times.
✓ Branch 4 taken 123071 times.
✓ Branch 5 taken 28 times.
145428 if ( ts != AV_NOPTS_VALUE && last != AV_NOPTS_VALUE && ts > last
2289
1/2
✓ Branch 0 taken 123071 times.
✗ Branch 1 not taken.
123071 && ts - (uint64_t)last < INT64_MAX) {
2290
2/2
✓ Branch 1 taken 6523 times.
✓ Branch 2 taken 116548 times.
123071 double dts = (is_relative(ts) ? ts - RELATIVE_TS_BASE : ts) * av_q2d(st->time_base);
2291 123071 int64_t duration = ts - last;
2292
2293
2/2
✓ Branch 0 taken 4008 times.
✓ Branch 1 taken 119063 times.
123071 if (!info->duration_error)
2294 4008 info->duration_error = av_mallocz(sizeof(info->duration_error[0])*2);
2295
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 123071 times.
123071 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 49105329 times.
✓ Branch 1 taken 123071 times.
49228400 for (int i = 0; i < MAX_STD_TIMEBASES; i++) {
2301
2/2
✓ Branch 0 taken 18921700 times.
✓ Branch 1 taken 30183629 times.
49105329 if (info->duration_error[0][1][i] < 1e10) {
2302 18921700 int framerate = get_std_framerate(i);
2303 18921700 double sdts = dts*framerate/(1001*12);
2304
2/2
✓ Branch 0 taken 37843400 times.
✓ Branch 1 taken 18921700 times.
56765100 for (int j = 0; j < 2; j++) {
2305 37843400 int64_t ticks = llrint(sdts+j*0.5);
2306 37843400 double error = sdts - ticks + j*0.5;
2307 37843400 info->duration_error[j][0][i] += error;
2308 37843400 info->duration_error[j][1][i] += error*error;
2309 }
2310 }
2311 }
2312
1/2
✓ Branch 0 taken 123071 times.
✗ Branch 1 not taken.
123071 if (info->rfps_duration_sum <= INT64_MAX - duration) {
2313 123071 info->duration_count++;
2314 123071 info->rfps_duration_sum += duration;
2315 }
2316
2317
2/2
✓ Branch 0 taken 11208 times.
✓ Branch 1 taken 111863 times.
123071 if (info->duration_count % 10 == 0) {
2318 11208 int n = info->duration_count;
2319
2/2
✓ Branch 0 taken 4471992 times.
✓ Branch 1 taken 11208 times.
4483200 for (int i = 0; i < MAX_STD_TIMEBASES; i++) {
2320
2/2
✓ Branch 0 taken 1814312 times.
✓ Branch 1 taken 2657680 times.
4471992 if (info->duration_error[0][1][i] < 1e10) {
2321 1814312 double a0 = info->duration_error[0][0][i] / n;
2322 1814312 double error0 = info->duration_error[0][1][i] / n - a0*a0;
2323 1814312 double a1 = info->duration_error[1][0][i] / n;
2324 1814312 double error1 = info->duration_error[1][1][i] / n - a1*a1;
2325
4/4
✓ Branch 0 taken 1485791 times.
✓ Branch 1 taken 328521 times.
✓ Branch 2 taken 1386723 times.
✓ Branch 3 taken 99068 times.
1814312 if (error0 > 0.04 && error1 > 0.04) {
2326 1386723 info->duration_error[0][1][i] = 2e10;
2327 1386723 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 111136 times.
✓ Branch 1 taken 11935 times.
✓ Branch 4 taken 111136 times.
✗ Branch 5 not taken.
123071 if (info->duration_count > 3 && is_relative(ts) == is_relative(last))
2335 111136 info->duration_gcd = av_gcd(info->duration_gcd, duration);
2336 }
2337
2/2
✓ Branch 0 taken 129232 times.
✓ Branch 1 taken 16196 times.
145428 if (ts != AV_NOPTS_VALUE)
2338 129232 info->last_dts = ts;
2339
2340 145428 return 0;
2341 }
2342
2343 8187 void ff_rfps_calculate(AVFormatContext *ic)
2344 {
2345
2/2
✓ Branch 0 taken 9192 times.
✓ Branch 1 taken 8187 times.
17379 for (unsigned i = 0; i < ic->nb_streams; i++) {
2346 9192 AVStream *const st = ic->streams[i];
2347 9192 FFStream *const sti = ffstream(st);
2348
2349
2/2
✓ Branch 0 taken 2469 times.
✓ Branch 1 taken 6723 times.
9192 if (st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO)
2350 2469 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 5332 times.
✓ Branch 2 taken 1391 times.
✓ Branch 3 taken 3481 times.
✓ Branch 4 taken 1851 times.
✓ Branch 5 taken 295 times.
✓ Branch 6 taken 3186 times.
✓ Branch 7 taken 144 times.
✓ Branch 8 taken 151 times.
6723 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 144 times.
✗ Branch 1 not taken.
144 sti->info->duration_gcd < INT64_MAX / st->time_base.num)
2356 144 av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den, st->time_base.den, st->time_base.num * sti->info->duration_gcd, INT_MAX);
2357
4/4
✓ Branch 0 taken 3970 times.
✓ Branch 1 taken 2753 times.
✓ Branch 2 taken 407 times.
✓ Branch 3 taken 3563 times.
6723 if (sti->info->duration_count > 1 && !st->r_frame_rate.num
2358
2/2
✓ Branch 1 taken 303 times.
✓ Branch 2 taken 104 times.
407 && 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 1094 times.
✓ Branch 1 taken 5629 times.
6723 if ( !st->avg_frame_rate.num
2391
4/4
✓ Branch 0 taken 284 times.
✓ Branch 1 taken 810 times.
✓ Branch 2 taken 280 times.
✓ Branch 3 taken 4 times.
1094 && st->r_frame_rate.num && sti->info->rfps_duration_sum
2392
2/2
✓ Branch 0 taken 73 times.
✓ Branch 1 taken 207 times.
280 && 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 6723 av_freep(&sti->info->duration_error);
2401 6723 sti->info->last_dts = AV_NOPTS_VALUE;
2402 6723 sti->info->duration_count = 0;
2403 6723 sti->info->rfps_duration_sum = 0;
2404 }
2405 8187 }
2406
2407 9150 static int extract_extradata_check(AVStream *st)
2408 {
2409 9150 const AVBitStreamFilter *const f = av_bsf_get_by_name("extract_extradata");
2410
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9150 times.
9150 if (!f)
2411 return 0;
2412
2413
1/2
✓ Branch 0 taken 9150 times.
✗ Branch 1 not taken.
9150 if (f->codec_ids) {
2414 const enum AVCodecID *ids;
2415
2/2
✓ Branch 0 taken 96997 times.
✓ Branch 1 taken 8257 times.
105254 for (ids = f->codec_ids; *ids != AV_CODEC_ID_NONE; ids++)
2416
2/2
✓ Branch 0 taken 893 times.
✓ Branch 1 taken 96104 times.
96997 if (*ids == st->codecpar->codec_id)
2417 893 return 1;
2418 }
2419
2420 8257 return 0;
2421 }
2422
2423 7208 static int extract_extradata_init(AVStream *st)
2424 {
2425 7208 FFStream *const sti = ffstream(st);
2426 const AVBitStreamFilter *f;
2427 int ret;
2428
2429 7208 f = av_bsf_get_by_name("extract_extradata");
2430
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7208 times.
7208 if (!f)
2431 goto finish;
2432
2433 /* check that the codec id is supported */
2434 7208 ret = extract_extradata_check(st);
2435
2/2
✓ Branch 0 taken 6425 times.
✓ Branch 1 taken 783 times.
7208 if (!ret)
2436 6425 goto finish;
2437
2438 783 av_bsf_free(&sti->extract_extradata.bsf);
2439 783 ret = av_bsf_alloc(f, &sti->extract_extradata.bsf);
2440
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 783 times.
783 if (ret < 0)
2441 return ret;
2442
2443 783 ret = avcodec_parameters_copy(sti->extract_extradata.bsf->par_in,
2444 783 st->codecpar);
2445
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 783 times.
783 if (ret < 0)
2446 goto fail;
2447
2448 783 sti->extract_extradata.bsf->time_base_in = st->time_base;
2449
2450 783 ret = av_bsf_init(sti->extract_extradata.bsf);
2451
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 783 times.
783 if (ret < 0)
2452 goto fail;
2453
2454 783 finish:
2455 7208 sti->extract_extradata.inited = 1;
2456
2457 7208 return 0;
2458 fail:
2459 av_bsf_free(&sti->extract_extradata.bsf);
2460 return ret;
2461 }
2462
2463 164103 static int extract_extradata(FFFormatContext *si, AVStream *st, const AVPacket *pkt)
2464 {
2465 164103 FFStream *const sti = ffstream(st);
2466 164103 AVPacket *const pkt_ref = si->parse_pkt;
2467 int ret;
2468
2469
2/2
✓ Branch 0 taken 7208 times.
✓ Branch 1 taken 156895 times.
164103 if (!sti->extract_extradata.inited) {
2470 7208 ret = extract_extradata_init(st);
2471
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7208 times.
7208 if (ret < 0)
2472 return ret;
2473 }
2474
2475
3/4
✓ Branch 0 taken 164103 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 162935 times.
✓ Branch 3 taken 1168 times.
164103 if (sti->extract_extradata.inited && !sti->extract_extradata.bsf)
2476 162935 return 0;
2477
2478 1168 ret = av_packet_ref(pkt_ref, pkt);
2479
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1168 times.
1168 if (ret < 0)
2480 return ret;
2481
2482 1168 ret = av_bsf_send_packet(sti->extract_extradata.bsf, pkt_ref);
2483
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1168 times.
1168 if (ret < 0) {
2484 av_packet_unref(pkt_ref);
2485 return ret;
2486 }
2487
2488
4/4
✓ Branch 0 taken 2336 times.
✓ Branch 1 taken 384 times.
✓ Branch 2 taken 1552 times.
✓ Branch 3 taken 784 times.
2720 while (ret >= 0 && !sti->avctx->extradata) {
2489 1552 ret = av_bsf_receive_packet(sti->extract_extradata.bsf, pkt_ref);
2490
2/2
✓ Branch 0 taken 384 times.
✓ Branch 1 taken 1168 times.
1552 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 1030 times.
✓ Branch 1 taken 384 times.
1414 for (int i = 0; i < pkt_ref->side_data_elems; i++) {
2497 1030 AVPacketSideData *const side_data = &pkt_ref->side_data[i];
2498
2/2
✓ Branch 0 taken 784 times.
✓ Branch 1 taken 246 times.
1030 if (side_data->type == AV_PKT_DATA_NEW_EXTRADATA) {
2499 784 sti->avctx->extradata = side_data->data;
2500 784 sti->avctx->extradata_size = side_data->size;
2501 784 side_data->data = NULL;
2502 784 side_data->size = 0;
2503 784 break;
2504 }
2505 }
2506 1168 av_packet_unref(pkt_ref);
2507 }
2508
2509 1168 return 0;
2510 }
2511
2512 7688 int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
2513 {
2514 7688 FFFormatContext *const si = ffformatcontext(ic);
2515 7688 int count = 0, ret = 0, err;
2516 int64_t read_size;
2517 7688 AVPacket *pkt1 = si->pkt;
2518 7688 int64_t old_offset = avio_tell(ic->pb);
2519 // new streams might appear, no options for those
2520 7688 int orig_nb_streams = ic->nb_streams;
2521 int flush_codecs;
2522 7688 int64_t max_analyze_duration = ic->max_analyze_duration;
2523 int64_t max_stream_analyze_duration;
2524 int64_t max_subtitle_analyze_duration;
2525 7688 int64_t probesize = ic->probesize;
2526 7688 int eof_reached = 0;
2527
2528 7688 flush_codecs = probesize > 0;
2529
2530 7688 av_opt_set_int(ic, "skip_clear", 1, AV_OPT_SEARCH_CHILDREN);
2531
2532 7688 max_stream_analyze_duration = max_analyze_duration;
2533 7688 max_subtitle_analyze_duration = max_analyze_duration;
2534
2/2
✓ Branch 0 taken 7687 times.
✓ Branch 1 taken 1 times.
7688 if (!max_analyze_duration) {
2535 7687 max_stream_analyze_duration =
2536 7687 max_analyze_duration = 5*AV_TIME_BASE;
2537 7687 max_subtitle_analyze_duration = 30*AV_TIME_BASE;
2538
2/2
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 7649 times.
7687 if (!strcmp(ic->iformat->name, "flv"))
2539 38 max_stream_analyze_duration = 90*AV_TIME_BASE;
2540
4/4
✓ Branch 0 taken 7662 times.
✓ Branch 1 taken 25 times.
✓ Branch 2 taken 44 times.
✓ Branch 3 taken 7618 times.
7687 if (!strcmp(ic->iformat->name, "mpeg") || !strcmp(ic->iformat->name, "mpegts"))
2541 69 max_stream_analyze_duration = 7*AV_TIME_BASE;
2542 }
2543
2544
2/2
✓ Branch 0 taken 4531 times.
✓ Branch 1 taken 3157 times.
7688 if (ic->pb) {
2545 4531 FFIOContext *const ctx = ffiocontext(ic->pb);
2546 4531 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 8324 times.
✓ Branch 1 taken 7688 times.
16012 for (unsigned i = 0; i < ic->nb_streams; i++) {
2551 const AVCodec *codec;
2552 8324 AVDictionary *thread_opt = NULL;
2553 8324 AVStream *const st = ic->streams[i];
2554 8324 FFStream *const sti = ffstream(st);
2555 8324 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 8324 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8324 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 7817 times.
✓ Branch 5 taken 507 times.
8324 if (!sti->parser && !(ic->flags & AVFMT_FLAG_NOPARSE) && sti->request_probe <= 0) {
2560 7817 sti->parser = av_parser_init(st->codecpar->codec_id);
2561
2/2
✓ Branch 0 taken 5474 times.
✓ Branch 1 taken 2343 times.
7817 if (sti->parser) {
2562
2/2
✓ Branch 0 taken 756 times.
✓ Branch 1 taken 4718 times.
5474 if (sti->need_parsing == AVSTREAM_PARSE_HEADERS) {
2563 756 sti->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES;
2564
2/2
✓ Branch 0 taken 820 times.
✓ Branch 1 taken 3898 times.
4718 } else if (sti->need_parsing == AVSTREAM_PARSE_FULL_RAW) {
2565 820 sti->parser->flags |= PARSER_FLAG_USE_CODEC_TS;
2566 }
2567
2/2
✓ Branch 0 taken 656 times.
✓ Branch 1 taken 1687 times.
2343 } else if (sti->need_parsing) {
2568 656 av_log(ic, AV_LOG_VERBOSE, "parser not found for codec "
2569 "%s, packets or times may be invalid.\n",
2570 656 avcodec_get_name(st->codecpar->codec_id));
2571 }
2572 }
2573
2574 8324 ret = avcodec_parameters_to_context(avctx, st->codecpar);
2575
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8324 times.
8324 if (ret < 0)
2576 goto find_stream_info_err;
2577
2/2
✓ Branch 0 taken 7817 times.
✓ Branch 1 taken 507 times.
8324 if (sti->request_probe <= 0)
2578 7817 sti->avctx_inited = 1;
2579
2580 8324 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 8037 times.
✓ Branch 1 taken 287 times.
8324 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 8037 times.
✓ Branch 1 taken 287 times.
8324 av_dict_set(options ? &options[i] : &thread_opt, "lowres", "0", 0);
2589
2590
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8324 times.
8324 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 7501 times.
✓ Branch 2 taken 823 times.
✓ Branch 3 taken 506 times.
✓ Branch 4 taken 6995 times.
8324 if (!has_codec_parameters(st, NULL) && sti->request_probe <= 0 ||
2596
2/2
✓ Branch 0 taken 78 times.
✓ Branch 1 taken 1251 times.
1329 st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) {
2597
3/4
✓ Branch 0 taken 7054 times.
✓ Branch 1 taken 19 times.
✓ Branch 2 taken 7054 times.
✗ Branch 3 not taken.
7073 if (codec && !avctx->codec)
2598
4/4
✓ Branch 0 taken 6771 times.
✓ Branch 1 taken 283 times.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 7048 times.
7054 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 287 times.
✓ Branch 1 taken 8037 times.
8324 if (!options)
2603 287 av_dict_free(&thread_opt);
2604 }
2605
2606 7688 read_size = 0;
2607 194923 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 202611 times.
202611 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 211242 times.
✓ Branch 1 taken 113542 times.
324784 for (i = 0; i < ic->nb_streams; i++) {
2622 211242 AVStream *const st = ic->streams[i];
2623 211242 FFStream *const sti = ffstream(st);
2624 211242 int fps_analyze_framecount = 20;
2625 int count;
2626
2627
2/2
✓ Branch 1 taken 7474 times.
✓ Branch 2 taken 203768 times.
211242 if (!has_codec_parameters(st, NULL))
2628 7474 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 124271 times.
✓ Branch 2 taken 79497 times.
203768 if (av_q2d(st->time_base) > 0.0005)
2633 124271 fps_analyze_framecount *= 2;
2634
2/2
✓ Branch 1 taken 8497 times.
✓ Branch 2 taken 195271 times.
203768 if (!tb_unreliable(ic, st))
2635 8497 fps_analyze_framecount = 0;
2636
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 203768 times.
203768 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 203724 times.
203768 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 19402 times.
✓ Branch 1 taken 184366 times.
203768 count = (ic->iformat->flags & AVFMT_NOTIMESTAMPS) ?
2642 19402 sti->info->codec_info_duration_fields/2 :
2643 184366 sti->info->duration_count;
2644
4/4
✓ Branch 0 taken 105026 times.
✓ Branch 1 taken 98742 times.
✓ Branch 2 taken 223 times.
✓ Branch 3 taken 104803 times.
203768 if (!(st->r_frame_rate.num && st->avg_frame_rate.num) &&
2645
2/2
✓ Branch 0 taken 43054 times.
✓ Branch 1 taken 55911 times.
98965 st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
2646
2/2
✓ Branch 0 taken 24943 times.
✓ Branch 1 taken 18111 times.
43054 if (count < fps_analyze_framecount)
2647 24943 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 2898 times.
✓ Branch 1 taken 175927 times.
✓ Branch 2 taken 130 times.
✓ Branch 3 taken 2768 times.
✓ Branch 4 taken 12 times.
✓ Branch 5 taken 118 times.
178825 if (sti->info->frame_delay_evidence && count < 2 && sti->avctx->has_b_frames == 0)
2652 12 break;
2653
2/2
✓ Branch 0 taken 158561 times.
✓ Branch 1 taken 20252 times.
178813 if (!sti->avctx->extradata &&
2654
6/6
✓ Branch 0 taken 156729 times.
✓ Branch 1 taken 1832 times.
✓ Branch 2 taken 110 times.
✓ Branch 3 taken 156619 times.
✓ Branch 4 taken 110 times.
✓ Branch 5 taken 1832 times.
160503 (!sti->extract_extradata.inited || sti->extract_extradata.bsf) &&
2655 1942 extract_extradata_check(st))
2656 110 break;
2657
2/2
✓ Branch 0 taken 59689 times.
✓ Branch 1 taken 119014 times.
178703 if (sti->first_dts == AV_NOPTS_VALUE &&
2658
6/6
✓ Branch 0 taken 9434 times.
✓ Branch 1 taken 50255 times.
✓ Branch 2 taken 9349 times.
✓ Branch 3 taken 85 times.
✓ Branch 4 taken 57313 times.
✓ Branch 5 taken 2291 times.
119293 (!(ic->iformat->flags & AVFMT_NOTIMESTAMPS) || sti->need_parsing == AVSTREAM_PARSE_FULL_RAW) &&
2659
2/2
✓ Branch 0 taken 59560 times.
✓ Branch 1 taken 44 times.
59604 sti->codec_info_nb_frames < ((st->disposition & AV_DISPOSITION_ATTACHED_PIC) ? 1 : ic->max_ts_probe) &&
2660
2/2
✓ Branch 0 taken 41472 times.
✓ Branch 1 taken 15841 times.
57313 (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ||
2661
2/2
✓ Branch 0 taken 783 times.
✓ Branch 1 taken 40689 times.
41472 st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO))
2662 break;
2663 }
2664 202611 analyzed_all_streams = 0;
2665
4/4
✓ Branch 0 taken 113542 times.
✓ Branch 1 taken 89069 times.
✓ Branch 2 taken 113503 times.
✓ Branch 3 taken 39 times.
202611 if (i == ic->nb_streams && !si->missing_streams) {
2666 113503 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 3322 times.
✓ Branch 1 taken 110181 times.
113503 if (!(ic->ctx_flags & AVFMTCTX_NOHEADER)) {
2670 /* If we found the info for all the codecs, we can stop. */
2671 3322 ret = count;
2672 3322 av_log(ic, AV_LOG_DEBUG, "All info found\n");
2673 3322 flush_codecs = 0;
2674 3322 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 196210 times.
199289 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 196210 ret = read_frame_internal(ic, pkt1);
2699
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 196210 times.
196210 if (ret == AVERROR(EAGAIN))
2700 continue;
2701
2702
2/2
✓ Branch 0 taken 1193 times.
✓ Branch 1 taken 195017 times.
196210 if (ret < 0) {
2703 /* EOF or error*/
2704 1193 eof_reached = 1;
2705 1193 break;
2706 }
2707
2708
1/2
✓ Branch 0 taken 195017 times.
✗ Branch 1 not taken.
195017 if (!(ic->flags & AVFMT_FLAG_NOBUFFER)) {
2709 195017 ret = avpriv_packet_list_put(&si->packet_buffer,
2710 pkt1, NULL, 0);
2711
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 195017 times.
195017 if (ret < 0)
2712 goto unref_then_goto_end;
2713
2714 195017 pkt = &si->packet_buffer.tail->pkt;
2715 } else {
2716 pkt = pkt1;
2717 }
2718
2719 195017 st = ic->streams[pkt->stream_index];
2720 195017 sti = ffstream(st);
2721
2/2
✓ Branch 0 taken 194961 times.
✓ Branch 1 taken 56 times.
195017 if (!(st->disposition & AV_DISPOSITION_ATTACHED_PIC))
2722 194961 read_size += pkt->size;
2723
2724 195017 avctx = sti->avctx;
2725
2/2
✓ Branch 0 taken 679 times.
✓ Branch 1 taken 194338 times.
195017 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 178585 times.
✓ Branch 1 taken 16432 times.
✓ Branch 2 taken 165979 times.
✓ Branch 3 taken 12606 times.
195017 if (pkt->dts != AV_NOPTS_VALUE && sti->codec_info_nb_frames > 1) {
2733 /* check for non-increasing dts */
2734
2/2
✓ Branch 0 taken 161009 times.
✓ Branch 1 taken 4970 times.
165979 if (sti->info->fps_last_dts != AV_NOPTS_VALUE &&
2735
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 160986 times.
161009 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 160986 times.
✓ Branch 1 taken 4993 times.
165979 if (sti->info->fps_last_dts != AV_NOPTS_VALUE &&
2749
2/2
✓ Branch 0 taken 156045 times.
✓ Branch 1 taken 4941 times.
160986 sti->info->fps_last_dts_idx > sti->info->fps_first_dts_idx &&
2750 156045 (pkt->dts - (uint64_t)sti->info->fps_last_dts) / 1000 >
2751 156045 (sti->info->fps_last_dts - (uint64_t)sti->info->fps_first_dts) /
2752
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 156045 times.
156045 (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 4993 times.
✓ Branch 1 taken 160986 times.
165979 if (sti->info->fps_first_dts == AV_NOPTS_VALUE) {
2765 4993 sti->info->fps_first_dts = pkt->dts;
2766 4993 sti->info->fps_first_dts_idx = sti->codec_info_nb_frames;
2767 }
2768 165979 sti->info->fps_last_dts = pkt->dts;
2769 165979 sti->info->fps_last_dts_idx = sti->codec_info_nb_frames;
2770 }
2771
2/2
✓ Branch 0 taken 181237 times.
✓ Branch 1 taken 13780 times.
195017 if (sti->codec_info_nb_frames > 1) {
2772 181237 int64_t t = 0;
2773 int64_t limit;
2774
2775
1/2
✓ Branch 0 taken 181237 times.
✗ Branch 1 not taken.
181237 if (st->time_base.den > 0)
2776 181237 t = av_rescale_q(sti->info->codec_info_duration, st->time_base, AV_TIME_BASE_Q);
2777
2/2
✓ Branch 0 taken 116222 times.
✓ Branch 1 taken 65015 times.
181237 if (st->avg_frame_rate.num > 0)
2778
2/2
✓ Branch 1 taken 115695 times.
✓ Branch 2 taken 527 times.
116222 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 4108 times.
✓ Branch 1 taken 177129 times.
181237 if ( t == 0
2781
2/2
✓ Branch 0 taken 535 times.
✓ Branch 1 taken 3573 times.
4108 && sti->codec_info_nb_frames > 30
2782
2/2
✓ Branch 0 taken 497 times.
✓ Branch 1 taken 38 times.
535 && 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 77486 times.
181237 if (analyzed_all_streams) limit = max_analyze_duration;
2789
2/2
✓ Branch 0 taken 91 times.
✓ Branch 1 taken 77395 times.
77486 else if (avctx->codec_type == AVMEDIA_TYPE_SUBTITLE) limit = max_subtitle_analyze_duration;
2790 77395 else limit = max_stream_analyze_duration;
2791
2792
2/2
✓ Branch 0 taken 94 times.
✓ Branch 1 taken 181143 times.
181237 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 177497 times.
✓ Branch 1 taken 3646 times.
✓ Branch 2 taken 177497 times.
✗ Branch 3 not taken.
181143 if (pkt->duration > 0 && pkt->duration < INT64_MAX - sti->info->codec_info_duration) {
2801
4/4
✓ Branch 0 taken 177453 times.
✓ Branch 1 taken 44 times.
✓ Branch 2 taken 14236 times.
✓ Branch 3 taken 163217 times.
177497 const int fields = sti->codec_desc && (sti->codec_desc->props & AV_CODEC_PROP_FIELDS);
2802
1/8
✗ Branch 0 not taken.
✓ Branch 1 taken 177497 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.
177497 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 177497 sti->info->codec_info_duration += pkt->duration;
2808
4/4
✓ Branch 0 taken 35697 times.
✓ Branch 1 taken 95337 times.
✓ Branch 2 taken 13864 times.
✓ Branch 3 taken 21833 times.
308531 sti->info->codec_info_duration_fields += sti->parser && sti->need_parsing && fields
2809
2/2
✓ Branch 0 taken 131034 times.
✓ Branch 1 taken 46463 times.
308531 ? sti->parser->repeat_pict + 1 : 2;
2810 }
2811 }
2812
2/2
✓ Branch 0 taken 135055 times.
✓ Branch 1 taken 59868 times.
194923 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
2813 #if FF_API_R_FRAME_RATE
2814 135055 ff_rfps_add_frame(ic, st, pkt->dts);
2815 #endif
2816
6/6
✓ Branch 0 taken 4229 times.
✓ Branch 1 taken 130826 times.
✓ Branch 2 taken 3990 times.
✓ Branch 3 taken 239 times.
✓ Branch 4 taken 1965 times.
✓ Branch 5 taken 2025 times.
135055 if (pkt->dts != pkt->pts && pkt->dts != AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE)
2817 1965 sti->info->frame_delay_evidence = 1;
2818 }
2819
2/2
✓ Branch 0 taken 163949 times.
✓ Branch 1 taken 30974 times.
194923 if (!sti->avctx->extradata) {
2820 163949 ret = extract_extradata(si, st, pkt);
2821
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 163949 times.
163949 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 177411 times.
✓ Branch 1 taken 17512 times.
372334 try_decode_frame(ic, st, pkt,
2835
2/2
✓ Branch 0 taken 77869 times.
✓ Branch 1 taken 99542 times.
177411 (options && i < orig_nb_streams) ? &options[i] : NULL);
2836
2837
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 194923 times.
194923 if (ic->flags & AVFMT_FLAG_NOBUFFER)
2838 av_packet_unref(pkt1);
2839
2840 194923 sti->codec_info_nb_frames++;
2841 194923 count++;
2842 }
2843
2844
2/2
✓ Branch 0 taken 1193 times.
✓ Branch 1 taken 6495 times.
7688 if (eof_reached) {
2845
2/2
✓ Branch 0 taken 1493 times.
✓ Branch 1 taken 1193 times.
2686 for (unsigned stream_index = 0; stream_index < ic->nb_streams; stream_index++) {
2846 1493 AVStream *const st = ic->streams[stream_index];
2847 1493 AVCodecContext *const avctx = ffstream(st)->avctx;
2848
2/2
✓ Branch 1 taken 28 times.
✓ Branch 2 taken 1465 times.
1493 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 1475 times.
✓ Branch 1 taken 18 times.
✓ Branch 3 taken 18 times.
✓ Branch 4 taken 1457 times.
1493 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 4366 times.
✓ Branch 1 taken 3322 times.
7688 if (flush_codecs) {
2870 4366 AVPacket *empty_pkt = si->pkt;
2871 4366 int err = 0;
2872 4366 av_packet_unref(empty_pkt);
2873
2874
2/2
✓ Branch 0 taken 4719 times.
✓ Branch 1 taken 4366 times.
9085 for (unsigned i = 0; i < ic->nb_streams; i++) {
2875 4719 AVStream *const st = ic->streams[i];
2876 4719 FFStream *const sti = ffstream(st);
2877
2878 /* flush the decoders */
2879
2/2
✓ Branch 0 taken 4650 times.
✓ Branch 1 taken 69 times.
4719 if (sti->info->found_decoder == 1) {
2880
2/2
✓ Branch 0 taken 4333 times.
✓ Branch 1 taken 317 times.
8983 err = try_decode_frame(ic, st, empty_pkt,
2881
2/2
✓ Branch 0 taken 4317 times.
✓ Branch 1 taken 16 times.
4333 (options && i < orig_nb_streams)
2882 4317 ? &options[i] : NULL);
2883
2884
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4650 times.
4650 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 7688 ff_rfps_calculate(ic);
2893
2894
2/2
✓ Branch 0 taken 8509 times.
✓ Branch 1 taken 7688 times.
16197 for (unsigned i = 0; i < ic->nb_streams; i++) {
2895 8509 AVStream *const st = ic->streams[i];
2896 8509 FFStream *const sti = ffstream(st);
2897 8509 AVCodecContext *const avctx = sti->avctx;
2898
2899
2/2
✓ Branch 0 taken 6372 times.
✓ Branch 1 taken 2137 times.
8509 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
2900
6/6
✓ Branch 0 taken 638 times.
✓ Branch 1 taken 5734 times.
✓ Branch 2 taken 589 times.
✓ Branch 3 taken 49 times.
✓ Branch 4 taken 573 times.
✓ Branch 5 taken 16 times.
6372 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 4108 times.
✓ Branch 1 taken 2264 times.
6372 if (sti->info->codec_info_duration_fields &&
2908
2/2
✓ Branch 0 taken 269 times.
✓ Branch 1 taken 3839 times.
4108 !st->avg_frame_rate.num &&
2909
1/2
✓ Branch 0 taken 269 times.
✗ Branch 1 not taken.
269 sti->info->codec_info_duration) {
2910 269 int best_fps = 0;
2911 269 double best_error = 0.01;
2912 269 AVRational codec_frame_rate = avctx->framerate;
2913
2914
1/2
✓ Branch 0 taken 269 times.
✗ Branch 1 not taken.
269 if (sti->info->codec_info_duration >= INT64_MAX / st->time_base.num / 2||
2915
1/2
✓ Branch 0 taken 269 times.
✗ Branch 1 not taken.
269 sti->info->codec_info_duration_fields >= INT64_MAX / st->time_base.den ||
2916
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 269 times.
269 sti->info->codec_info_duration < 0)
2917 continue;
2918 269 av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den,
2919 269 sti->info->codec_info_duration_fields * (int64_t) st->time_base.den,
2920 269 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 107331 times.
✓ Branch 1 taken 269 times.
107600 for (int j = 0; j < MAX_STD_TIMEBASES; j++) {
2925 107331 AVRational std_fps = { get_std_framerate(j), 12 * 1001 };
2926 107331 double error = fabs(av_q2d(st->avg_frame_rate) /
2927 107331 av_q2d(std_fps) - 1);
2928
2929
2/2
✓ Branch 0 taken 736 times.
✓ Branch 1 taken 106595 times.
107331 if (error < best_error) {
2930 736 best_error = error;
2931 736 best_fps = std_fps.num;
2932 }
2933
2934
2/2
✓ Branch 1 taken 12768 times.
✓ Branch 2 taken 94563 times.
107331 if ((ffifmt(ic->iformat)->flags_internal & FF_INFMT_FLAG_PREFER_CODEC_FRAMERATE) &&
2935
2/4
✓ Branch 0 taken 12768 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12768 times.
✗ Branch 3 not taken.
12768 codec_frame_rate.num > 0 && codec_frame_rate.den > 0) {
2936 12768 error = fabs(av_q2d(codec_frame_rate) /
2937 12768 av_q2d(std_fps) - 1);
2938
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 12758 times.
12768 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 267 times.
✓ Branch 1 taken 2 times.
269 if (best_fps)
2945 267 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 1866 times.
✓ Branch 1 taken 4506 times.
6372 if (!st->r_frame_rate.num) {
2949 1866 const AVCodecDescriptor *desc = sti->codec_desc;
2950
3/4
✓ Branch 0 taken 1866 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 246 times.
✓ Branch 3 taken 1620 times.
1866 AVRational mul = (AVRational){ desc && (desc->props & AV_CODEC_PROP_FIELDS) ? 2 : 1, 1 };
2951 1866 AVRational fr = av_mul_q(avctx->framerate, mul);
2952
2953
5/6
✓ Branch 0 taken 190 times.
✓ Branch 1 taken 1676 times.
✓ Branch 2 taken 190 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 176 times.
✓ Branch 7 taken 14 times.
1866 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 1690 st->r_frame_rate.num = st->time_base.den;
2957 1690 st->r_frame_rate.den = st->time_base.num;
2958 }
2959 }
2960 6372 st->codecpar->framerate = avctx->framerate;
2961
3/4
✓ Branch 0 taken 116 times.
✓ Branch 1 taken 6256 times.
✓ Branch 2 taken 116 times.
✗ Branch 3 not taken.
6372 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 1968 times.
✓ Branch 1 taken 169 times.
2137 } else if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
2967
2/2
✓ Branch 0 taken 647 times.
✓ Branch 1 taken 1321 times.
1968 if (!avctx->bits_per_coded_sample)
2968 647 avctx->bits_per_coded_sample =
2969 647 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 1968 times.
1968 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 7688 times.
✗ Branch 1 not taken.
7688 if (probesize)
2992 7688 estimate_timings(ic, old_offset);
2993
2994 7688 av_opt_set_int(ic, "skip_clear", 0, AV_OPT_SEARCH_CHILDREN);
2995
2996
3/4
✓ Branch 0 taken 6495 times.
✓ Branch 1 taken 1193 times.
✓ Branch 2 taken 6495 times.
✗ Branch 3 not taken.
7688 if (ret >= 0 && ic->nb_streams)
2997 /* We could not have all the codec parameters before EOF. */
2998 6495 ret = -1;
2999
2/2
✓ Branch 0 taken 8509 times.
✓ Branch 1 taken 7688 times.
16197 for (unsigned i = 0; i < ic->nb_streams; i++) {
3000 8509 AVStream *const st = ic->streams[i];
3001 8509 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 8496 times.
8509 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 8483 times.
8509 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 8483 ret = 0;
3022 }
3023 }
3024
3025 7688 err = compute_chapters_end(ic);
3026
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7688 times.
7688 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 8509 times.
✓ Branch 1 taken 7688 times.
16197 for (unsigned i = 0; i < ic->nb_streams; i++) {
3033 8509 AVStream *const st = ic->streams[i];
3034 8509 FFStream *const sti = ffstream(st);
3035
3036
2/2
✓ Branch 0 taken 8496 times.
✓ Branch 1 taken 13 times.
8509 if (sti->avctx_inited) {
3037 8496 ret = avcodec_parameters_from_context(st->codecpar, sti->avctx);
3038
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8496 times.
8496 if (ret < 0)
3039 goto find_stream_info_err;
3040
3041
3/4
✓ Branch 0 taken 8312 times.
✓ Branch 1 taken 184 times.
✓ Branch 2 taken 8312 times.
✗ Branch 3 not taken.
8496 if (sti->avctx->rc_buffer_size > 0 || sti->avctx->rc_max_rate > 0 ||
3042
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8312 times.
8312 sti->avctx->rc_min_rate) {
3043 size_t cpb_size;
3044 184 AVCPBProperties *props = av_cpb_properties_alloc(&cpb_size);
3045
1/2
✓ Branch 0 taken 184 times.
✗ Branch 1 not taken.
184 if (props) {
3046
1/2
✓ Branch 0 taken 184 times.
✗ Branch 1 not taken.
184 if (sti->avctx->rc_buffer_size > 0)
3047 184 props->buffer_size = sti->avctx->rc_buffer_size;
3048
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 184 times.
184 if (sti->avctx->rc_min_rate > 0)
3049 props->min_bitrate = sti->avctx->rc_min_rate;
3050
2/2
✓ Branch 0 taken 77 times.
✓ Branch 1 taken 107 times.
184 if (sti->avctx->rc_max_rate > 0)
3051 77 props->max_bitrate = sti->avctx->rc_max_rate;
3052
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 184 times.
184 if (!av_packet_side_data_add(&st->codecpar->coded_side_data,
3053 184 &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 8509 sti->avctx_inited = 0;
3062 }
3063
3064 7688 find_stream_info_err:
3065
2/2
✓ Branch 0 taken 8509 times.
✓ Branch 1 taken 7688 times.
16197 for (unsigned i = 0; i < ic->nb_streams; i++) {
3066 8509 AVStream *const st = ic->streams[i];
3067 8509 FFStream *const sti = ffstream(st);
3068 int err;
3069
3070
1/2
✓ Branch 0 taken 8509 times.
✗ Branch 1 not taken.
8509 if (sti->info) {
3071 8509 av_freep(&sti->info->duration_error);
3072 8509 av_freep(&sti->info);
3073 }
3074
3075
2/2
✓ Branch 1 taken 8410 times.
✓ Branch 2 taken 99 times.
8509 if (avcodec_is_open(sti->avctx)) {
3076 8410 err = codec_close(sti);
3077
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 8410 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
8410 if (err < 0 && ret >= 0)
3078 ret = err;
3079 }
3080
3081 8509 av_bsf_free(&sti->extract_extradata.bsf);
3082 }
3083
2/2
✓ Branch 0 taken 4531 times.
✓ Branch 1 taken 3157 times.
7688 if (ic->pb) {
3084 4531 FFIOContext *const ctx = ffiocontext(ic->pb);
3085 4531 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 7688 return ret;
3089
3090 unref_then_goto_end:
3091 av_packet_unref(pkt1);
3092 goto find_stream_info_err;
3093 }
3094