FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/demux.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 1650 1914 86.2%
Functions: 48 48 100.0%
Branches: 1496 1873 79.9%

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 2466095 static int64_t wrap_timestamp(const AVStream *st, int64_t timestamp)
54 {
55 2466095 const FFStream *const sti = cffstream(st);
56
4/4
✓ Branch 0 taken 146860 times.
✓ Branch 1 taken 2319235 times.
✓ Branch 2 taken 142276 times.
✓ Branch 3 taken 4584 times.
2466095 if (sti->pts_wrap_behavior != AV_PTS_WRAP_IGNORE && st->pts_wrap_bits < 64 &&
57
3/4
✓ Branch 0 taken 142276 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 84564 times.
✓ Branch 3 taken 57712 times.
142276 sti->pts_wrap_reference != AV_NOPTS_VALUE && timestamp != AV_NOPTS_VALUE) {
58
2/2
✓ Branch 0 taken 84360 times.
✓ Branch 1 taken 204 times.
84564 if (sti->pts_wrap_behavior == AV_PTS_WRAP_ADD_OFFSET &&
59
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 84360 times.
84360 timestamp < sti->pts_wrap_reference)
60 return timestamp + (1ULL << st->pts_wrap_bits);
61
2/2
✓ Branch 0 taken 204 times.
✓ Branch 1 taken 84360 times.
84564 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 2466045 return timestamp;
66 }
67
68 302567 int64_t ff_wrap_timestamp(const AVStream *st, int64_t timestamp)
69 {
70 302567 return wrap_timestamp(st, timestamp);
71 }
72
73 9564 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 328 times.
✓ Branch 1 taken 9236 times.
9564 if (codec_id == AV_CODEC_ID_H264)
81 328 return avcodec_find_decoder_by_name("h264");
82 #endif
83
84 9236 codec = ff_find_decoder(s, st, codec_id);
85
2/2
✓ Branch 0 taken 171 times.
✓ Branch 1 taken 9065 times.
9236 if (!codec)
86 171 return NULL;
87
88
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9065 times.
9065 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 9065 return codec;
101 }
102
103 3018 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 { "dts", AV_CODEC_ID_DTS, AVMEDIA_TYPE_AUDIO },
115 { "dvbsub", AV_CODEC_ID_DVB_SUBTITLE, AVMEDIA_TYPE_SUBTITLE },
116 { "dvbtxt", AV_CODEC_ID_DVB_TELETEXT, AVMEDIA_TYPE_SUBTITLE },
117 { "eac3", AV_CODEC_ID_EAC3, AVMEDIA_TYPE_AUDIO },
118 { "h264", AV_CODEC_ID_H264, AVMEDIA_TYPE_VIDEO },
119 { "hevc", AV_CODEC_ID_HEVC, AVMEDIA_TYPE_VIDEO },
120 { "loas", AV_CODEC_ID_AAC_LATM, AVMEDIA_TYPE_AUDIO },
121 { "m4v", AV_CODEC_ID_MPEG4, AVMEDIA_TYPE_VIDEO },
122 { "mjpeg_2000", AV_CODEC_ID_JPEG2000, AVMEDIA_TYPE_VIDEO },
123 { "mp3", AV_CODEC_ID_MP3, AVMEDIA_TYPE_AUDIO },
124 { "mpegvideo", AV_CODEC_ID_MPEG2VIDEO, AVMEDIA_TYPE_VIDEO },
125 { "truehd", AV_CODEC_ID_TRUEHD, AVMEDIA_TYPE_AUDIO },
126 { "evc", AV_CODEC_ID_EVC, AVMEDIA_TYPE_VIDEO },
127 { "vvc", AV_CODEC_ID_VVC, AVMEDIA_TYPE_VIDEO },
128 { 0 }
129 };
130 int score;
131 3018 const AVInputFormat *fmt = av_probe_input_format3(pd, 1, &score);
132 3018 FFStream *const sti = ffstream(st);
133
134
2/2
✓ Branch 0 taken 939 times.
✓ Branch 1 taken 2079 times.
3018 if (fmt) {
135 939 av_log(s, AV_LOG_DEBUG,
136 "Probe with size=%d, packets=%d detected %s with score=%d\n",
137 939 pd->buf_size, s->max_probe_packets - sti->probe_packets,
138 939 fmt->name, score);
139
2/2
✓ Branch 0 taken 15818 times.
✓ Branch 1 taken 917 times.
16735 for (int i = 0; fmt_id_type[i].name; i++) {
140
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 15794 times.
15818 if (!strcmp(fmt->name, fmt_id_type[i].name)) {
141
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 13 times.
24 if (fmt_id_type[i].type != AVMEDIA_TYPE_AUDIO &&
142
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 st->codecpar->sample_rate)
143 continue;
144
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 18 times.
24 if (sti->request_probe > score &&
145
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
6 st->codecpar->codec_id != fmt_id_type[i].id)
146 2 continue;
147 22 st->codecpar->codec_id = fmt_id_type[i].id;
148 22 st->codecpar->codec_type = fmt_id_type[i].type;
149 22 sti->need_context_update = 1;
150 22 return score;
151 }
152 }
153 }
154 2996 return 0;
155 }
156
157 7545 static int init_input(AVFormatContext *s, const char *filename,
158 AVDictionary **options)
159 {
160 int ret;
161 7545 AVProbeData pd = { filename, NULL, 0 };
162 7545 int score = AVPROBE_SCORE_RETRY;
163
164
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 7533 times.
7545 if (s->pb) {
165 12 s->flags |= AVFMT_FLAG_CUSTOM_IO;
166
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (!s->iformat)
167 return av_probe_input_buffer2(s->pb, &s->iformat, filename,
168 s, 0, s->format_probesize);
169
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 else if (s->iformat->flags & AVFMT_NOFILE)
170 av_log(s, AV_LOG_WARNING, "Custom AVIOContext makes no sense and "
171 "will be ignored with AVFMT_NOFILE format.\n");
172 12 return 0;
173 }
174
175
4/4
✓ Branch 0 taken 3690 times.
✓ Branch 1 taken 3843 times.
✓ Branch 2 taken 847 times.
✓ Branch 3 taken 2843 times.
7533 if ((s->iformat && s->iformat->flags & AVFMT_NOFILE) ||
176
4/4
✓ Branch 0 taken 3843 times.
✓ Branch 1 taken 847 times.
✓ Branch 3 taken 193 times.
✓ Branch 4 taken 3650 times.
4690 (!s->iformat && (s->iformat = av_probe_input_format2(&pd, 0, &score))))
177 3036 return score;
178
179
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4497 times.
4497 if ((ret = s->io_open(s, &s->pb, filename, AVIO_FLAG_READ | s->avio_flags, options)) < 0)
180 return ret;
181
182
2/2
✓ Branch 0 taken 847 times.
✓ Branch 1 taken 3650 times.
4497 if (s->iformat)
183 847 return 0;
184 3650 return av_probe_input_buffer2(s->pb, &s->iformat, filename,
185 3650 s, 0, s->format_probesize);
186 }
187
188 7545 static int update_stream_avctx(AVFormatContext *s)
189 {
190 int ret;
191
2/2
✓ Branch 0 taken 8153 times.
✓ Branch 1 taken 7545 times.
15698 for (unsigned i = 0; i < s->nb_streams; i++) {
192 8153 AVStream *const st = s->streams[i];
193 8153 FFStream *const sti = ffstream(st);
194
195
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8153 times.
8153 if (!sti->need_context_update)
196 continue;
197
198 /* close parser, because it depends on the codec */
199
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 8153 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
8153 if (sti->parser && sti->avctx->codec_id != st->codecpar->codec_id) {
200 av_parser_close(sti->parser);
201 sti->parser = NULL;
202 }
203
204 /* update internal codec context, for the parser */
205 8153 ret = avcodec_parameters_to_context(sti->avctx, st->codecpar);
206
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8153 times.
8153 if (ret < 0)
207 return ret;
208
209 8153 sti->codec_desc = avcodec_descriptor_get(sti->avctx->codec_id);
210
211 8153 sti->need_context_update = 0;
212 }
213 7545 return 0;
214 }
215
216 7545 int avformat_open_input(AVFormatContext **ps, const char *filename,
217 const AVInputFormat *fmt, AVDictionary **options)
218 {
219 FormatContextInternal *fci;
220 7545 AVFormatContext *s = *ps;
221 FFFormatContext *si;
222 7545 AVDictionary *tmp = NULL;
223 7545 ID3v2ExtraMeta *id3v2_extra_meta = NULL;
224 7545 int ret = 0;
225
226
3/4
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 7523 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 22 times.
7545 if (!s && !(s = avformat_alloc_context()))
227 return AVERROR(ENOMEM);
228 7545 fci = ff_fc_internal(s);
229 7545 si = &fci->fc;
230
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7545 times.
7545 if (!s->av_class) {
231 av_log(NULL, AV_LOG_ERROR, "Input context has not been properly allocated by avformat_alloc_context() and is not NULL either\n");
232 return AVERROR(EINVAL);
233 }
234
2/2
✓ Branch 0 taken 3702 times.
✓ Branch 1 taken 3843 times.
7545 if (fmt)
235 3702 s->iformat = fmt;
236
237
2/2
✓ Branch 0 taken 7535 times.
✓ Branch 1 taken 10 times.
7545 if (options)
238 7535 av_dict_copy(&tmp, *options, 0);
239
240
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 7533 times.
7545 if (s->pb) // must be before any goto fail
241 12 s->flags |= AVFMT_FLAG_CUSTOM_IO;
242
243
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7545 times.
7545 if ((ret = av_opt_set_dict(s, &tmp)) < 0)
244 goto fail;
245
246
2/4
✓ Branch 0 taken 7545 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 7545 times.
7545 if (!(s->url = av_strdup(filename ? filename : ""))) {
247 ret = AVERROR(ENOMEM);
248 goto fail;
249 }
250
251
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7545 times.
7545 if ((ret = init_input(s, filename, &tmp)) < 0)
252 goto fail;
253 7545 s->probe_score = ret;
254
255
6/6
✓ Branch 0 taken 7462 times.
✓ Branch 1 taken 83 times.
✓ Branch 2 taken 4426 times.
✓ Branch 3 taken 3036 times.
✓ Branch 4 taken 4425 times.
✓ Branch 5 taken 1 times.
7545 if (!s->protocol_whitelist && s->pb && s->pb->protocol_whitelist) {
256 4425 s->protocol_whitelist = av_strdup(s->pb->protocol_whitelist);
257
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4425 times.
4425 if (!s->protocol_whitelist) {
258 ret = AVERROR(ENOMEM);
259 goto fail;
260 }
261 }
262
263
4/6
✓ Branch 0 taken 7545 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4509 times.
✓ Branch 3 taken 3036 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 4509 times.
7545 if (!s->protocol_blacklist && s->pb && s->pb->protocol_blacklist) {
264 s->protocol_blacklist = av_strdup(s->pb->protocol_blacklist);
265 if (!s->protocol_blacklist) {
266 ret = AVERROR(ENOMEM);
267 goto fail;
268 }
269 }
270
271
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 7545 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
7545 if (s->format_whitelist && av_match_list(s->iformat->name, s->format_whitelist, ',') <= 0) {
272 av_log(s, AV_LOG_ERROR, "Format not on whitelist \'%s\'\n", s->format_whitelist);
273 ret = AVERROR(EINVAL);
274 goto fail;
275 }
276
277 7545 avio_skip(s->pb, s->skip_initial_bytes);
278
279 /* Check filename in case an image number is expected. */
280
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7545 times.
7545 if (s->iformat->flags & AVFMT_NEEDNUMBER) {
281 if (!av_filename_number_test(filename)) {
282 ret = AVERROR(EINVAL);
283 goto fail;
284 }
285 }
286
287 7545 s->duration = s->start_time = AV_NOPTS_VALUE;
288
289 /* Allocate private data. */
290
2/2
✓ Branch 1 taken 7388 times.
✓ Branch 2 taken 157 times.
7545 if (ffifmt(s->iformat)->priv_data_size > 0) {
291
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 7388 times.
7388 if (!(s->priv_data = av_mallocz(ffifmt(s->iformat)->priv_data_size))) {
292 ret = AVERROR(ENOMEM);
293 goto fail;
294 }
295
2/2
✓ Branch 0 taken 6562 times.
✓ Branch 1 taken 826 times.
7388 if (s->iformat->priv_class) {
296 6562 *(const AVClass **) s->priv_data = s->iformat->priv_class;
297 6562 av_opt_set_defaults(s->priv_data);
298
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6562 times.
6562 if ((ret = av_opt_set_dict(s->priv_data, &tmp)) < 0)
299 goto fail;
300 }
301 }
302
303 /* e.g. AVFMT_NOFILE formats will not have an AVIOContext */
304
2/2
✓ Branch 0 taken 4509 times.
✓ Branch 1 taken 3036 times.
7545 if (s->pb)
305 4509 ff_id3v2_read_dict(s->pb, &si->id3v2_meta, ID3v2_DEFAULT_MAGIC, &id3v2_extra_meta);
306
307
1/2
✓ Branch 1 taken 7545 times.
✗ Branch 2 not taken.
7545 if (ffifmt(s->iformat)->read_header)
308
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 7545 times.
7545 if ((ret = ffifmt(s->iformat)->read_header(s)) < 0) {
309 if (ffifmt(s->iformat)->flags_internal & FF_INFMT_FLAG_INIT_CLEANUP)
310 goto close;
311 goto fail;
312 }
313
314
2/2
✓ Branch 0 taken 6272 times.
✓ Branch 1 taken 1273 times.
7545 if (!s->metadata) {
315 6272 s->metadata = si->id3v2_meta;
316 6272 si->id3v2_meta = NULL;
317
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1273 times.
1273 } else if (si->id3v2_meta) {
318 av_log(s, AV_LOG_WARNING, "Discarding ID3 tags because more suitable tags were found.\n");
319 av_dict_free(&si->id3v2_meta);
320 }
321
322
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 7535 times.
7545 if (id3v2_extra_meta) {
323
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") ||
324 !strcmp(s->iformat->name, "tta") || !strcmp(s->iformat->name, "wav")) {
325
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
10 if ((ret = ff_id3v2_parse_apic(s, id3v2_extra_meta)) < 0)
326 goto close;
327
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
10 if ((ret = ff_id3v2_parse_chapters(s, id3v2_extra_meta)) < 0)
328 goto close;
329
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
10 if ((ret = ff_id3v2_parse_priv(s, id3v2_extra_meta)) < 0)
330 goto close;
331 } else
332 av_log(s, AV_LOG_DEBUG, "demuxer does not support additional id3 data, skipping\n");
333 10 ff_id3v2_free_extra_meta(&id3v2_extra_meta);
334 }
335
336
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7545 times.
7545 if ((ret = avformat_queue_attached_pictures(s)) < 0)
337 goto close;
338
339
4/4
✓ Branch 0 taken 4509 times.
✓ Branch 1 taken 3036 times.
✓ Branch 2 taken 4074 times.
✓ Branch 3 taken 435 times.
7545 if (s->pb && !si->data_offset)
340 4074 si->data_offset = avio_tell(s->pb);
341
342 7545 fci->raw_packet_buffer_size = 0;
343
344 7545 update_stream_avctx(s);
345
346
2/2
✓ Branch 0 taken 7535 times.
✓ Branch 1 taken 10 times.
7545 if (options) {
347 7535 av_dict_free(options);
348 7535 *options = tmp;
349 }
350 7545 *ps = s;
351 7545 return 0;
352
353 close:
354 if (ffifmt(s->iformat)->read_close)
355 ffifmt(s->iformat)->read_close(s);
356 fail:
357 ff_id3v2_free_extra_meta(&id3v2_extra_meta);
358 av_dict_free(&tmp);
359 if (s->pb && !(s->flags & AVFMT_FLAG_CUSTOM_IO))
360 avio_closep(&s->pb);
361 avformat_free_context(s);
362 *ps = NULL;
363 return ret;
364 }
365
366 7545 void avformat_close_input(AVFormatContext **ps)
367 {
368 AVFormatContext *s;
369 AVIOContext *pb;
370
371
2/4
✓ Branch 0 taken 7545 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 7545 times.
7545 if (!ps || !*ps)
372 return;
373
374 7545 s = *ps;
375 7545 pb = s->pb;
376
377
5/6
✓ Branch 0 taken 7545 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4527 times.
✓ Branch 3 taken 3018 times.
✓ Branch 4 taken 4484 times.
✓ Branch 5 taken 43 times.
7545 if ((s->iformat && strcmp(s->iformat->name, "image2") && s->iformat->flags & AVFMT_NOFILE) ||
378
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 7490 times.
7502 (s->flags & AVFMT_FLAG_CUSTOM_IO))
379 55 pb = NULL;
380
381
1/2
✓ Branch 0 taken 7545 times.
✗ Branch 1 not taken.
7545 if (s->iformat)
382
2/2
✓ Branch 1 taken 4894 times.
✓ Branch 2 taken 2651 times.
7545 if (ffifmt(s->iformat)->read_close)
383 4894 ffifmt(s->iformat)->read_close(s);
384
385 7545 avformat_free_context(s);
386
387 7545 *ps = NULL;
388
389 7545 avio_close(pb);
390 }
391
392 1084781 static void force_codec_ids(AVFormatContext *s, AVStream *st)
393 {
394
5/5
✓ Branch 0 taken 735794 times.
✓ Branch 1 taken 345890 times.
✓ Branch 2 taken 2967 times.
✓ Branch 3 taken 128 times.
✓ Branch 4 taken 2 times.
1084781 switch (st->codecpar->codec_type) {
395 735794 case AVMEDIA_TYPE_VIDEO:
396
2/2
✓ Branch 0 taken 113554 times.
✓ Branch 1 taken 622240 times.
735794 if (s->video_codec_id)
397 113554 st->codecpar->codec_id = s->video_codec_id;
398 735794 break;
399 345890 case AVMEDIA_TYPE_AUDIO:
400
2/2
✓ Branch 0 taken 2558 times.
✓ Branch 1 taken 343332 times.
345890 if (s->audio_codec_id)
401 2558 st->codecpar->codec_id = s->audio_codec_id;
402 345890 break;
403 2967 case AVMEDIA_TYPE_SUBTITLE:
404
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2967 times.
2967 if (s->subtitle_codec_id)
405 st->codecpar->codec_id = s->subtitle_codec_id;
406 2967 break;
407 128 case AVMEDIA_TYPE_DATA:
408
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 128 times.
128 if (s->data_codec_id)
409 st->codecpar->codec_id = s->data_codec_id;
410 128 break;
411 }
412 1084781 }
413
414 16227 static int probe_codec(AVFormatContext *s, AVStream *st, const AVPacket *pkt)
415 {
416 16227 FormatContextInternal *const fci = ff_fc_internal(s);
417 16227 FFStream *const sti = ffstream(st);
418
419
2/2
✓ Branch 0 taken 15910 times.
✓ Branch 1 taken 317 times.
16227 if (sti->request_probe > 0) {
420 15910 AVProbeData *const pd = &sti->probe_data;
421 int end;
422 15910 av_log(s, AV_LOG_DEBUG, "probing stream %d pp:%d\n", st->index, sti->probe_packets);
423 15910 --sti->probe_packets;
424
425
2/2
✓ Branch 0 taken 15899 times.
✓ Branch 1 taken 11 times.
15910 if (pkt) {
426 15899 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 15899 times.
15899 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 15899 pd->buf = new_buf;
434 15899 memcpy(pd->buf + pd->buf_size, pkt->data, pkt->size);
435 15899 pd->buf_size += pkt->size;
436 15899 memset(pd->buf + pd->buf_size, 0, AVPROBE_PADDING_SIZE);
437 } else {
438 11 no_packet:
439 11 sti->probe_packets = 0;
440
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 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 15910 times.
✗ Branch 1 not taken.
31820 end = fci->raw_packet_buffer_size >= s->probesize ||
447
2/2
✓ Branch 0 taken 496 times.
✓ Branch 1 taken 15414 times.
15910 sti->probe_packets <= 0;
448
449
4/4
✓ Branch 0 taken 15414 times.
✓ Branch 1 taken 496 times.
✓ Branch 2 taken 2522 times.
✓ Branch 3 taken 12892 times.
15910 if (end || av_log2(pd->buf_size) != av_log2(pd->buf_size - pkt->size)) {
450 3018 int score = set_codec_from_probe_data(s, st, pd);
451
4/4
✓ Branch 0 taken 3010 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 2992 times.
✓ Branch 3 taken 18 times.
3018 if ( (st->codecpar->codec_id != AV_CODEC_ID_NONE && score > AVPROBE_SCORE_STREAM_RETRY)
452
2/2
✓ Branch 0 taken 496 times.
✓ Branch 1 taken 2504 times.
3000 || end) {
453 514 pd->buf_size = 0;
454 514 av_freep(&pd->buf);
455 514 sti->request_probe = -1;
456
1/2
✓ Branch 0 taken 514 times.
✗ Branch 1 not taken.
514 if (st->codecpar->codec_id != AV_CODEC_ID_NONE) {
457 514 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 3018 force_codec_ids(s, st);
462 }
463 }
464 16227 return 0;
465 }
466
467 1081763 static int update_wrap_reference(AVFormatContext *s, AVStream *st, int stream_index, AVPacket *pkt)
468 {
469 1081763 FFStream *const sti = ffstream(st);
470 1081763 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 881638 times.
✓ Branch 1 taken 200125 times.
1081763 if (ref == AV_NOPTS_VALUE)
476 881638 ref = pkt->pts;
477
7/8
✓ Branch 0 taken 1015519 times.
✓ Branch 1 taken 66244 times.
✓ Branch 2 taken 48554 times.
✓ Branch 3 taken 966965 times.
✓ Branch 4 taken 297 times.
✓ Branch 5 taken 48257 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 297 times.
1081763 if (sti->pts_wrap_reference != AV_NOPTS_VALUE || st->pts_wrap_bits >= 63 || ref == AV_NOPTS_VALUE || !s->correct_ts_overflow)
478 1081466 return 0;
479 297 ref &= (1LL << st->pts_wrap_bits)-1;
480
481 // reference time stamp should be 60 s before first time stamp
482 297 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 595 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 296 times.
298 AV_PTS_WRAP_ADD_OFFSET : AV_PTS_WRAP_SUB_OFFSET;
487
488 297 first_program = av_find_program_from_stream(s, NULL, stream_index);
489
490
2/2
✓ Branch 0 taken 191 times.
✓ Branch 1 taken 106 times.
297 if (!first_program) {
491 191 int default_stream_index = av_find_default_stream_index(s);
492 191 FFStream *const default_sti = ffstream(s->streams[default_stream_index]);
493
2/2
✓ Branch 0 taken 180 times.
✓ Branch 1 taken 11 times.
191 if (default_sti->pts_wrap_reference == AV_NOPTS_VALUE) {
494
2/2
✓ Branch 0 taken 313 times.
✓ Branch 1 taken 180 times.
493 for (unsigned i = 0; i < s->nb_streams; i++) {
495 313 FFStream *const sti = ffstream(s->streams[i]);
496
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 313 times.
313 if (av_find_program_from_stream(s, NULL, i))
497 continue;
498 313 sti->pts_wrap_reference = pts_wrap_reference;
499 313 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 297 return 1;
533 }
534
535 1081763 static void update_timestamps(AVFormatContext *s, AVStream *st, AVPacket *pkt)
536 {
537 1081763 FFStream *const sti = ffstream(st);
538
539
4/4
✓ Branch 1 taken 297 times.
✓ Branch 2 taken 1081466 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 296 times.
1081763 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 1081763 pkt->dts = wrap_timestamp(st, pkt->dts);
550 1081763 pkt->pts = wrap_timestamp(st, pkt->pts);
551
552 1081763 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 1081763 times.
1081763 if (s->use_wallclock_as_timestamps)
556 pkt->dts = pkt->pts = av_rescale_q(av_gettime(), AV_TIME_BASE_Q, st->time_base);
557 1081763 }
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 1081763 static int handle_new_packet(AVFormatContext *s, AVPacket *pkt, int allow_passthrough)
568 {
569 1081763 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 1081763 times.
1081763 av_assert0(pkt->stream_index < (unsigned)s->nb_streams &&
575 "Invalid stream index.\n");
576
577
2/2
✓ Branch 0 taken 201 times.
✓ Branch 1 taken 1081562 times.
1081763 if (pkt->flags & AV_PKT_FLAG_CORRUPT) {
578 402 av_log(s, AV_LOG_WARNING,
579 "Packet corrupt (stream = %d, dts = %s)%s.\n",
580 201 pkt->stream_index, av_ts2str(pkt->dts),
581
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 201 times.
201 s->flags & AVFMT_FLAG_DISCARD_CORRUPT ? ", dropping it" : "");
582
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 201 times.
201 if (s->flags & AVFMT_FLAG_DISCARD_CORRUPT) {
583 av_packet_unref(pkt);
584 return 1;
585 }
586 }
587
588 1081763 st = s->streams[pkt->stream_index];
589 1081763 sti = ffstream(st);
590
591 1081763 update_timestamps(s, st, pkt);
592
593
6/6
✓ Branch 0 taken 1065864 times.
✓ Branch 1 taken 15899 times.
✓ Branch 2 taken 1065589 times.
✓ Branch 3 taken 275 times.
✓ Branch 4 taken 1065547 times.
✓ Branch 5 taken 42 times.
1081763 if (sti->request_probe <= 0 && allow_passthrough && !fci->raw_packet_buffer.head)
594 1065547 return 0;
595
596 16216 err = avpriv_packet_list_put(&fci->raw_packet_buffer, pkt, NULL, 0);
597
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16216 times.
16216 if (err < 0) {
598 av_packet_unref(pkt);
599 return err;
600 }
601
602 16216 pkt = &fci->raw_packet_buffer.tail->pkt;
603 16216 fci->raw_packet_buffer_size += pkt->size;
604
605 16216 err = probe_codec(s, st, pkt);
606
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16216 times.
16216 if (err < 0)
607 return err;
608
609 16216 return 1;
610 }
611
612 275 int ff_buffer_packet(AVFormatContext *s, AVPacket *pkt)
613 {
614 275 int err = handle_new_packet(s, pkt, 0);
615
616 275 return err < 0 ? err : 0;
617 }
618
619 1088254 int ff_read_packet(AVFormatContext *s, AVPacket *pkt)
620 {
621 1088254 FormatContextInternal *const fci = ff_fc_internal(s);
622 int err;
623
624 #if FF_API_INIT_PACKET
625 FF_DISABLE_DEPRECATION_WARNINGS
626 1088254 pkt->data = NULL;
627 1088254 pkt->size = 0;
628 1088254 av_init_packet(pkt);
629 FF_ENABLE_DEPRECATION_WARNINGS
630 #else
631 av_packet_unref(pkt);
632 #endif
633
634 16448 for (;;) {
635 1104702 PacketListEntry *pktl = fci->raw_packet_buffer.head;
636
637
2/2
✓ Branch 0 taken 31701 times.
✓ Branch 1 taken 1073001 times.
1104702 if (pktl) {
638 31701 AVStream *const st = s->streams[pktl->pkt.stream_index];
639
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31701 times.
31701 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 16263 times.
✓ Branch 2 taken 15438 times.
31701 if (ffstream(st)->request_probe <= 0) {
643 16263 avpriv_packet_list_get(&fci->raw_packet_buffer, pkt);
644 16263 fci->raw_packet_buffer_size -= pkt->size;
645 16263 return 0;
646 }
647 }
648
649 1088439 err = ffifmt(s->iformat)->read_packet(s, pkt);
650
2/2
✓ Branch 0 taken 6951 times.
✓ Branch 1 taken 1081488 times.
1088439 if (err < 0) {
651 6951 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 496 times.
✓ Branch 1 taken 6455 times.
6951 if (err == FFERROR_REDO)
657 496 continue;
658
3/4
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 6444 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 11 times.
6455 if (!pktl || err == AVERROR(EAGAIN))
659 6444 return err;
660
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 11 times.
22 for (unsigned i = 0; i < s->nb_streams; i++) {
661 11 AVStream *const st = s->streams[i];
662 11 FFStream *const sti = ffstream(st);
663
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
11 if (sti->probe_packets || sti->request_probe > 0)
664
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
11 if ((err = probe_codec(s, st, NULL)) < 0)
665 return err;
666
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 av_assert0(sti->request_probe <= 0);
667 }
668 11 continue;
669 }
670
671 1081488 err = av_packet_make_refcounted(pkt);
672
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1081488 times.
1081488 if (err < 0) {
673 av_packet_unref(pkt);
674 return err;
675 }
676
677 1081488 err = handle_new_packet(s, pkt, 1);
678
2/2
✓ Branch 0 taken 1065547 times.
✓ Branch 1 taken 15941 times.
1081488 if (err <= 0) /* Error or passthrough */
679 1065547 return err;
680 }
681 }
682
683 /**
684 * Return the frame duration in seconds. Return 0 if not available.
685 */
686 324016 static void compute_frame_duration(AVFormatContext *s, int *pnum, int *pden,
687 AVStream *st, AVCodecParserContext *pc,
688 AVPacket *pkt)
689 {
690 324016 FFStream *const sti = ffstream(st);
691 324016 AVRational codec_framerate = sti->avctx->framerate;
692 int frame_size, sample_rate;
693
694 324016 *pnum = 0;
695 324016 *pden = 0;
696
3/3
✓ Branch 0 taken 221318 times.
✓ Branch 1 taken 101318 times.
✓ Branch 2 taken 1380 times.
324016 switch (st->codecpar->codec_type) {
697 221318 case AVMEDIA_TYPE_VIDEO:
698
6/6
✓ Branch 0 taken 192052 times.
✓ Branch 1 taken 29266 times.
✓ Branch 2 taken 42722 times.
✓ Branch 3 taken 149330 times.
✓ Branch 4 taken 28982 times.
✓ Branch 5 taken 13740 times.
221318 if (st->r_frame_rate.num && (!pc || !codec_framerate.num)) {
699 178312 *pnum = st->r_frame_rate.den;
700 178312 *pden = st->r_frame_rate.num;
701
2/2
✓ Branch 0 taken 23505 times.
✓ Branch 1 taken 19501 times.
43006 } else if ((s->iformat->flags & AVFMT_NOTIMESTAMPS) &&
702
2/2
✓ Branch 0 taken 14643 times.
✓ Branch 1 taken 8862 times.
23505 !codec_framerate.num &&
703
3/4
✓ Branch 0 taken 14641 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 14641 times.
✗ Branch 3 not taken.
14643 st->avg_frame_rate.num && st->avg_frame_rate.den) {
704 14641 *pnum = st->avg_frame_rate.den;
705 14641 *pden = st->avg_frame_rate.num;
706
2/2
✓ Branch 0 taken 9901 times.
✓ Branch 1 taken 18464 times.
28365 } else if (st->time_base.num * 1000LL > st->time_base.den) {
707 9901 *pnum = st->time_base.num;
708 9901 *pden = st->time_base.den;
709
2/2
✓ Branch 0 taken 18418 times.
✓ Branch 1 taken 46 times.
18464 } else if (codec_framerate.den * 1000LL > codec_framerate.num) {
710 55254 int ticks_per_frame = (sti->codec_desc &&
711
3/4
✓ Branch 0 taken 18418 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13435 times.
✓ Branch 3 taken 4983 times.
18418 (sti->codec_desc->props & AV_CODEC_PROP_FIELDS)) ? 2 : 1;
712 18418 av_reduce(pnum, pden,
713 18418 codec_framerate.den,
714 18418 codec_framerate.num * (int64_t)ticks_per_frame,
715 INT_MAX);
716
717
4/4
✓ Branch 0 taken 16550 times.
✓ Branch 1 taken 1868 times.
✓ Branch 2 taken 12729 times.
✓ Branch 3 taken 3821 times.
18418 if (pc && pc->repeat_pict) {
718 12729 av_reduce(pnum, pden,
719 12729 (*pnum) * (1LL + pc->repeat_pict),
720 12729 (*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 18418 times.
✗ Branch 1 not taken.
18418 if (sti->codec_desc &&
727
3/4
✓ Branch 0 taken 13435 times.
✓ Branch 1 taken 4983 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 13435 times.
18418 (sti->codec_desc->props & AV_CODEC_PROP_FIELDS) && !pc)
728 *pnum = *pden = 0;
729 }
730 221318 break;
731 101318 case AVMEDIA_TYPE_AUDIO:
732
2/2
✓ Branch 0 taken 45554 times.
✓ Branch 1 taken 55764 times.
101318 if (sti->avctx_inited) {
733 45554 frame_size = av_get_audio_frame_duration(sti->avctx, pkt->size);
734 45554 sample_rate = sti->avctx->sample_rate;
735 } else {
736 55764 frame_size = av_get_audio_frame_duration2(st->codecpar, pkt->size);
737 55764 sample_rate = st->codecpar->sample_rate;
738 }
739
4/4
✓ Branch 0 taken 95136 times.
✓ Branch 1 taken 6182 times.
✓ Branch 2 taken 95133 times.
✓ Branch 3 taken 3 times.
101318 if (frame_size <= 0 || sample_rate <= 0)
740 break;
741 95133 *pnum = frame_size;
742 95133 *pden = sample_rate;
743 95133 break;
744 1380 default:
745 1380 break;
746 }
747 324016 }
748
749 715653 static int has_decode_delay_been_guessed(AVStream *st)
750 {
751 715653 FFStream *const sti = ffstream(st);
752
2/2
✓ Branch 0 taken 699213 times.
✓ Branch 1 taken 16440 times.
715653 if (st->codecpar->codec_id != AV_CODEC_ID_H264) return 1;
753
2/2
✓ Branch 0 taken 6050 times.
✓ Branch 1 taken 10390 times.
16440 if (!sti->info) // if we have left find_stream_info then nb_decoded_frames won't increase anymore for stream copy
754 6050 return 1;
755 #if CONFIG_H264_DECODER
756
2/2
✓ Branch 0 taken 6182 times.
✓ Branch 1 taken 4208 times.
10390 if (sti->avctx->has_b_frames &&
757
2/2
✓ Branch 1 taken 911 times.
✓ Branch 2 taken 5271 times.
6182 avpriv_h264_has_num_reorder_frames(sti->avctx) == sti->avctx->has_b_frames)
758 911 return 1;
759 #endif
760
2/2
✓ Branch 0 taken 9291 times.
✓ Branch 1 taken 188 times.
9479 if (sti->avctx->has_b_frames < 3)
761 9291 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 36035 static PacketListEntry *get_next_pkt(AVFormatContext *s, AVStream *st,
769 PacketListEntry *pktl)
770 {
771 36035 FormatContextInternal *const fci = ff_fc_internal(s);
772 36035 FFFormatContext *const si = &fci->fc;
773
2/2
✓ Branch 0 taken 34178 times.
✓ Branch 1 taken 1857 times.
36035 if (pktl->next)
774 34178 return pktl->next;
775
2/2
✓ Branch 0 taken 1827 times.
✓ Branch 1 taken 30 times.
1857 if (pktl == si->packet_buffer.tail)
776 1827 return fci->parse_queue.head;
777 30 return NULL;
778 }
779
780 519214 static int64_t select_from_pts_buffer(AVStream *st, int64_t *pts_buffer, int64_t dts)
781 {
782 519214 FFStream *const sti = ffstream(st);
783 1550623 int onein_oneout = st->codecpar->codec_id != AV_CODEC_ID_H264 &&
784
4/4
✓ Branch 0 taken 512195 times.
✓ Branch 1 taken 7019 times.
✓ Branch 2 taken 511387 times.
✓ Branch 3 taken 808 times.
1030601 st->codecpar->codec_id != AV_CODEC_ID_HEVC &&
785
2/2
✓ Branch 0 taken 511377 times.
✓ Branch 1 taken 10 times.
511387 st->codecpar->codec_id != AV_CODEC_ID_VVC;
786
787
2/2
✓ Branch 0 taken 7837 times.
✓ Branch 1 taken 511377 times.
519214 if (!onein_oneout) {
788 7837 int delay = sti->avctx->has_b_frames;
789
790
2/2
✓ Branch 0 taken 1204 times.
✓ Branch 1 taken 6633 times.
7837 if (dts == AV_NOPTS_VALUE) {
791 1204 int64_t best_score = INT64_MAX;
792
2/2
✓ Branch 0 taken 1379 times.
✓ Branch 1 taken 1204 times.
2583 for (int i = 0; i < delay; i++) {
793
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1379 times.
1379 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 11241 times.
✓ Branch 1 taken 6633 times.
17874 for (int i = 0; i < delay; i++) {
803
2/2
✓ Branch 0 taken 10471 times.
✓ Branch 1 taken 770 times.
11241 if (pts_buffer[i] != AV_NOPTS_VALUE) {
804 10471 int64_t diff = FFABS(pts_buffer[i] - dts)
805 10471 + (uint64_t)sti->pts_reorder_error[i];
806 10471 diff = FFMAX(diff, sti->pts_reorder_error[i]);
807 10471 sti->pts_reorder_error[i] = diff;
808 10471 sti->pts_reorder_error_count[i]++;
809
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 10468 times.
10471 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 1379 times.
✓ Branch 1 taken 517835 times.
519214 if (dts == AV_NOPTS_VALUE)
819 1379 dts = pts_buffer[0];
820
821 519214 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 6064 static void update_dts_from_pts(AVFormatContext *s, int stream_index,
829 PacketListEntry *pkt_buffer)
830 {
831 6064 AVStream *const st = s->streams[stream_index];
832 6064 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 103088 times.
✓ Branch 1 taken 6064 times.
109152 for (int i = 0; i < MAX_REORDER_DELAY + 1; i++)
837 103088 pts_buffer[i] = AV_NOPTS_VALUE;
838
839
2/2
✓ Branch 1 taken 5300 times.
✓ Branch 2 taken 6064 times.
11364 for (; pkt_buffer; pkt_buffer = get_next_pkt(s, st, pkt_buffer)) {
840
2/2
✓ Branch 0 taken 4642 times.
✓ Branch 1 taken 658 times.
5300 if (pkt_buffer->pkt.stream_index != stream_index)
841 4642 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 6064 }
852
853 560387 static void update_initial_timestamps(AVFormatContext *s, int stream_index,
854 int64_t dts, int64_t pts, AVPacket *pkt)
855 {
856 560387 FormatContextInternal *const fci = ff_fc_internal(s);
857 560387 FFFormatContext *const si = &fci->fc;
858 560387 AVStream *const st = s->streams[stream_index];
859 560387 FFStream *const sti = ffstream(st);
860
2/2
✓ Branch 0 taken 181480 times.
✓ Branch 1 taken 378907 times.
560387 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 184546 times.
✓ Branch 1 taken 375841 times.
✓ Branch 2 taken 6121 times.
✓ Branch 3 taken 178425 times.
560387 if (sti->first_dts != AV_NOPTS_VALUE ||
865 6121 dts == AV_NOPTS_VALUE ||
866
1/2
✓ Branch 0 taken 6121 times.
✗ Branch 1 not taken.
6121 sti->cur_dts == AV_NOPTS_VALUE ||
867
1/2
✓ Branch 0 taken 6121 times.
✗ Branch 1 not taken.
6121 sti->cur_dts < INT_MIN + RELATIVE_TS_BASE ||
868
2/4
✓ Branch 0 taken 6121 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6121 times.
12242 dts < INT_MIN + (sti->cur_dts - RELATIVE_TS_BASE) ||
869 6121 is_relative(dts))
870 554266 return;
871
872 6121 sti->first_dts = dts - (sti->cur_dts - RELATIVE_TS_BASE);
873 6121 sti->cur_dts = dts;
874 6121 shift = (uint64_t)sti->first_dts - RELATIVE_TS_BASE;
875
876
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6121 times.
6121 if (is_relative(pts))
877 pts += shift;
878
879
2/2
✓ Branch 1 taken 5035 times.
✓ Branch 2 taken 6121 times.
11156 for (PacketListEntry *pktl_it = pktl; pktl_it; pktl_it = get_next_pkt(s, st, pktl_it)) {
880
2/2
✓ Branch 0 taken 4565 times.
✓ Branch 1 taken 470 times.
5035 if (pktl_it->pkt.stream_index != stream_index)
881 4565 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 not taken.
✓ Branch 3 taken 1 times.
41 if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && st->codecpar->sample_rate)
891 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 6046 times.
✓ Branch 2 taken 75 times.
6121 if (has_decode_delay_been_guessed(st))
896 6046 update_dts_from_pts(s, stream_index, pktl);
897
898
2/2
✓ Branch 0 taken 1671 times.
✓ Branch 1 taken 4450 times.
6121 if (st->start_time == AV_NOPTS_VALUE) {
899
3/4
✓ Branch 0 taken 1210 times.
✓ Branch 1 taken 461 times.
✓ Branch 2 taken 1210 times.
✗ Branch 3 not taken.
1671 if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO || !(pkt->flags & AV_PKT_FLAG_DISCARD)) {
900 1671 st->start_time = pts;
901 }
902
4/4
✓ Branch 0 taken 461 times.
✓ Branch 1 taken 1210 times.
✓ Branch 2 taken 386 times.
✓ Branch 3 taken 75 times.
1671 if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && st->codecpar->sample_rate)
903 386 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 225725 static void update_initial_durations(AVFormatContext *s, AVStream *st,
908 int stream_index, int64_t duration)
909 {
910 225725 FormatContextInternal *const fci = ff_fc_internal(s);
911 225725 FFFormatContext *const si = &fci->fc;
912 225725 FFStream *const sti = ffstream(st);
913
2/2
✓ Branch 0 taken 178647 times.
✓ Branch 1 taken 47078 times.
225725 PacketListEntry *pktl = si->packet_buffer.head ? si->packet_buffer.head : fci->parse_queue.head;
914 225725 int64_t cur_dts = RELATIVE_TS_BASE;
915
916
2/2
✓ Branch 0 taken 121616 times.
✓ Branch 1 taken 104109 times.
225725 if (sti->first_dts != AV_NOPTS_VALUE) {
917
2/2
✓ Branch 0 taken 118019 times.
✓ Branch 1 taken 3597 times.
121616 if (sti->update_initial_durations_done)
918 118019 return;
919 3597 sti->update_initial_durations_done = 1;
920 3597 cur_dts = sti->first_dts;
921
1/2
✓ Branch 1 taken 5338 times.
✗ Branch 2 not taken.
5338 for (; pktl; pktl = get_next_pkt(s, st, pktl)) {
922
2/2
✓ Branch 0 taken 3612 times.
✓ Branch 1 taken 1726 times.
5338 if (pktl->pkt.stream_index == stream_index) {
923
2/2
✓ Branch 0 taken 3510 times.
✓ Branch 1 taken 102 times.
3612 if (pktl->pkt.pts != pktl->pkt.dts ||
924
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 3495 times.
3510 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 3597 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
✓ Branch 3 taken 3564 times.
3597 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 3564 times.
3564 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 3543 times.
✓ Branch 1 taken 21 times.
3564 pktl = si->packet_buffer.head ? si->packet_buffer.head : fci->parse_queue.head;
940 3564 sti->first_dts = cur_dts;
941
2/2
✓ Branch 0 taken 79741 times.
✓ Branch 1 taken 24368 times.
104109 } else if (sti->cur_dts != RELATIVE_TS_BASE)
942 79741 return;
943
944
2/2
✓ Branch 1 taken 51259 times.
✓ Branch 2 taken 632 times.
51891 for (; pktl; pktl = get_next_pkt(s, st, pktl)) {
945
2/2
✓ Branch 0 taken 23809 times.
✓ Branch 1 taken 27450 times.
51259 if (pktl->pkt.stream_index != stream_index)
946 23809 continue;
947
2/2
✓ Branch 0 taken 558 times.
✓ Branch 1 taken 26892 times.
27450 if ((pktl->pkt.pts == pktl->pkt.dts ||
948
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 544 times.
558 pktl->pkt.pts == AV_NOPTS_VALUE) &&
949
2/2
✓ Branch 0 taken 3642 times.
✓ Branch 1 taken 23264 times.
26906 (pktl->pkt.dts == AV_NOPTS_VALUE ||
950
2/2
✓ Branch 0 taken 178 times.
✓ Branch 1 taken 3464 times.
3642 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 150 times.
✓ Branch 1 taken 26580 times.
26730 !pktl->pkt.duration &&
953
1/2
✓ Branch 1 taken 150 times.
✗ Branch 2 not taken.
150 av_sat_add64(cur_dts, duration) == cur_dts + (uint64_t)duration
954 ) {
955 150 pktl->pkt.dts = cur_dts;
956
2/2
✓ Branch 0 taken 134 times.
✓ Branch 1 taken 16 times.
150 if (!sti->avctx->has_b_frames)
957 134 pktl->pkt.pts = cur_dts;
958 150 pktl->pkt.duration = duration;
959 } else
960 break;
961 150 cur_dts = pktl->pkt.dts + pktl->pkt.duration;
962 }
963
2/2
✓ Branch 0 taken 632 times.
✓ Branch 1 taken 27300 times.
27932 if (!pktl)
964 632 sti->cur_dts = cur_dts;
965 }
966
967 567395 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 567395 FormatContextInternal *const fci = ff_fc_internal(s);
972 567395 FFFormatContext *const si = &fci->fc;
973 567395 FFStream *const sti = ffstream(st);
974 int num, den, presentation_delayed, delay;
975 int64_t offset;
976 AVRational duration;
977 1668612 int onein_oneout = st->codecpar->codec_id != AV_CODEC_ID_H264 &&
978
4/4
✓ Branch 0 taken 533822 times.
✓ Branch 1 taken 33573 times.
✓ Branch 2 taken 520973 times.
✓ Branch 3 taken 12849 times.
1088368 st->codecpar->codec_id != AV_CODEC_ID_HEVC &&
979
2/2
✓ Branch 0 taken 517966 times.
✓ Branch 1 taken 3007 times.
520973 st->codecpar->codec_id != AV_CODEC_ID_VVC;
980
981
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 567395 times.
567395 if (s->flags & AVFMT_FLAG_NOFILLIN)
982 return;
983
984
4/4
✓ Branch 0 taken 242101 times.
✓ Branch 1 taken 325294 times.
✓ Branch 2 taken 72611 times.
✓ Branch 3 taken 169490 times.
567395 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && pkt->dts != AV_NOPTS_VALUE) {
985
4/4
✓ Branch 0 taken 42788 times.
✓ Branch 1 taken 29823 times.
✓ Branch 2 taken 41633 times.
✓ Branch 3 taken 1155 times.
72611 if (pkt->dts == pkt->pts && sti->last_dts_for_order_check != AV_NOPTS_VALUE) {
986
2/2
✓ Branch 0 taken 41624 times.
✓ Branch 1 taken 9 times.
41633 if (sti->last_dts_for_order_check <= pkt->dts) {
987 41624 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 41604 times.
41633 if (sti->dts_ordered + sti->dts_misordered > 250) {
996 29 sti->dts_ordered >>= 1;
997 29 sti->dts_misordered >>= 1;
998 }
999 }
1000
1001 72611 sti->last_dts_for_order_check = pkt->dts;
1002
4/4
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 72574 times.
✓ Branch 2 taken 20 times.
✓ Branch 3 taken 17 times.
72611 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 567395 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
567395 if ((s->flags & AVFMT_FLAG_IGNDTS) && pkt->pts != AV_NOPTS_VALUE)
1007 pkt->dts = AV_NOPTS_VALUE;
1008
1009
4/4
✓ Branch 0 taken 183015 times.
✓ Branch 1 taken 384380 times.
✓ Branch 2 taken 27397 times.
✓ Branch 3 taken 155618 times.
567395 if (pc && pc->pict_type == AV_PICTURE_TYPE_B
1010
2/2
✓ Branch 0 taken 79 times.
✓ Branch 1 taken 27318 times.
27397 && !sti->avctx->has_b_frames)
1011 //FIXME Set low_delay = 0 when has_b_frames = 1
1012 79 sti->avctx->has_b_frames = 1;
1013
1014 /* do we have a video B-frame ? */
1015 567395 delay = sti->avctx->has_b_frames;
1016 567395 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 46080 times.
✓ Branch 1 taken 521315 times.
✓ Branch 2 taken 42049 times.
✓ Branch 3 taken 4031 times.
567395 if (delay &&
1021
2/2
✓ Branch 0 taken 14652 times.
✓ Branch 1 taken 27397 times.
42049 pc && pc->pict_type != AV_PICTURE_TYPE_B)
1022 14652 presentation_delayed = 1;
1023
1024
4/4
✓ Branch 0 taken 337956 times.
✓ Branch 1 taken 229439 times.
✓ Branch 2 taken 166066 times.
✓ Branch 3 taken 171890 times.
567395 if (pkt->pts != AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE &&
1025
3/4
✓ Branch 0 taken 26050 times.
✓ Branch 1 taken 140016 times.
✓ Branch 2 taken 26050 times.
✗ Branch 3 not taken.
166066 st->pts_wrap_bits < 63 && pkt->dts > INT64_MIN + (1LL << st->pts_wrap_bits) &&
1026
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26050 times.
26050 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 28551 times.
✓ Branch 1 taken 538844 times.
✓ Branch 2 taken 23398 times.
✓ Branch 3 taken 5153 times.
567395 if (delay == 1 && pkt->dts == pkt->pts &&
1038
4/4
✓ Branch 0 taken 6134 times.
✓ Branch 1 taken 17264 times.
✓ Branch 2 taken 628 times.
✓ Branch 3 taken 5506 times.
23398 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 567395 duration = av_mul_q((AVRational) {pkt->duration, 1}, st->time_base);
1046
2/2
✓ Branch 0 taken 321211 times.
✓ Branch 1 taken 246184 times.
567395 if (pkt->duration <= 0) {
1047 321211 compute_frame_duration(s, &num, &den, st, pc, pkt);
1048
3/4
✓ Branch 0 taken 311222 times.
✓ Branch 1 taken 9989 times.
✓ Branch 2 taken 311222 times.
✗ Branch 3 not taken.
321211 if (den && num) {
1049 311222 duration = (AVRational) {num, den};
1050 311222 pkt->duration = av_rescale_rnd(1,
1051 311222 num * (int64_t) st->time_base.den,
1052 311222 den * (int64_t) st->time_base.num,
1053 AV_ROUND_DOWN);
1054 }
1055 }
1056
1057
6/6
✓ Branch 0 taken 554584 times.
✓ Branch 1 taken 12811 times.
✓ Branch 2 taken 375937 times.
✓ Branch 3 taken 178647 times.
✓ Branch 4 taken 47078 times.
✓ Branch 5 taken 328859 times.
567395 if (pkt->duration > 0 && (si->packet_buffer.head || fci->parse_queue.head))
1058 225725 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 183015 times.
✓ Branch 1 taken 384380 times.
✓ Branch 2 taken 960 times.
✓ Branch 3 taken 182055 times.
✓ Branch 4 taken 960 times.
✗ Branch 5 not taken.
567395 if (pc && sti->need_parsing == AVSTREAM_PARSE_TIMESTAMPS && pkt->size) {
1063 /* this will estimate bitrate based on this frame's duration and size */
1064 960 offset = av_rescale(pc->offset, pkt->duration, pkt->size);
1065
2/2
✓ Branch 0 taken 575 times.
✓ Branch 1 taken 385 times.
960 if (pkt->pts != AV_NOPTS_VALUE)
1066 575 pkt->pts += offset;
1067
2/2
✓ Branch 0 taken 121 times.
✓ Branch 1 taken 839 times.
960 if (pkt->dts != AV_NOPTS_VALUE)
1068 121 pkt->dts += offset;
1069 }
1070
1071 /* This may be redundant, but it should not hurt. */
1072
2/2
✓ Branch 0 taken 196267 times.
✓ Branch 1 taken 371128 times.
567395 if (pkt->dts != AV_NOPTS_VALUE &&
1073
2/2
✓ Branch 0 taken 166044 times.
✓ Branch 1 taken 30223 times.
196267 pkt->pts != AV_NOPTS_VALUE &&
1074
2/2
✓ Branch 0 taken 4892 times.
✓ Branch 1 taken 161152 times.
166044 pkt->pts > pkt->dts)
1075 4892 presentation_delayed = 1;
1076
1077
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 567395 times.
567395 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 46080 times.
✓ Branch 1 taken 521315 times.
✓ Branch 2 taken 28551 times.
✓ Branch 3 taken 17529 times.
✓ Branch 4 taken 25097 times.
✓ Branch 5 taken 3454 times.
✓ Branch 6 taken 514525 times.
✓ Branch 7 taken 31887 times.
567395 if ((delay == 0 || (delay == 1 && pc)) &&
1086 onein_oneout) {
1087
2/2
✓ Branch 0 taken 5461 times.
✓ Branch 1 taken 509064 times.
514525 if (presentation_delayed) {
1088 /* DTS = decompression timestamp */
1089 /* PTS = presentation timestamp */
1090
2/2
✓ Branch 0 taken 2848 times.
✓ Branch 1 taken 2613 times.
5461 if (pkt->dts == AV_NOPTS_VALUE)
1091 2848 pkt->dts = sti->last_IP_pts;
1092 5461 update_initial_timestamps(s, pkt->stream_index, pkt->dts, pkt->pts, pkt);
1093
2/2
✓ Branch 0 taken 2508 times.
✓ Branch 1 taken 2953 times.
5461 if (pkt->dts == AV_NOPTS_VALUE)
1094 2508 pkt->dts = sti->cur_dts;
1095
1096 /* This is tricky: the dts must be incremented by the duration
1097 * of the frame we are displaying, i.e. the last I- or P-frame. */
1098
3/4
✓ Branch 0 taken 238 times.
✓ Branch 1 taken 5223 times.
✓ Branch 2 taken 238 times.
✗ Branch 3 not taken.
5461 if (sti->last_IP_duration == 0 && (uint64_t)pkt->duration <= INT32_MAX)
1099 238 sti->last_IP_duration = pkt->duration;
1100
1/2
✓ Branch 0 taken 5461 times.
✗ Branch 1 not taken.
5461 if (pkt->dts != AV_NOPTS_VALUE)
1101 5461 sti->cur_dts = av_sat_add64(pkt->dts, sti->last_IP_duration);
1102
1/2
✓ Branch 0 taken 5461 times.
✗ Branch 1 not taken.
5461 if (pkt->dts != AV_NOPTS_VALUE &&
1103
2/2
✓ Branch 0 taken 3229 times.
✓ Branch 1 taken 2232 times.
5461 pkt->pts == AV_NOPTS_VALUE &&
1104
2/2
✓ Branch 0 taken 3227 times.
✓ Branch 1 taken 2 times.
3229 sti->last_IP_duration > 0 &&
1105
4/4
✓ Branch 0 taken 738 times.
✓ Branch 1 taken 2489 times.
✓ Branch 2 taken 732 times.
✓ Branch 3 taken 6 times.
3227 ((uint64_t)sti->cur_dts - (uint64_t)next_dts + 1) <= 2 &&
1106
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 721 times.
732 next_dts != next_pts &&
1107 next_pts != AV_NOPTS_VALUE)
1108 11 pkt->pts = next_dts;
1109
1110
1/2
✓ Branch 0 taken 5461 times.
✗ Branch 1 not taken.
5461 if ((uint64_t)pkt->duration <= INT32_MAX)
1111 5461 sti->last_IP_duration = pkt->duration;
1112 5461 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 184876 times.
✓ Branch 1 taken 324188 times.
509064 } else if (pkt->pts != AV_NOPTS_VALUE ||
1116
2/2
✓ Branch 0 taken 155453 times.
✓ Branch 1 taken 29423 times.
184876 pkt->dts != AV_NOPTS_VALUE ||
1117
2/2
✓ Branch 0 taken 151886 times.
✓ Branch 1 taken 3567 times.
155453 pkt->duration > 0 ) {
1118
1119 /* presentation is not delayed : PTS and DTS are the same */
1120
2/2
✓ Branch 0 taken 181309 times.
✓ Branch 1 taken 324188 times.
505497 if (pkt->pts == AV_NOPTS_VALUE)
1121 181309 pkt->pts = pkt->dts;
1122 505497 update_initial_timestamps(s, pkt->stream_index, pkt->pts,
1123 pkt->pts, pkt);
1124
2/2
✓ Branch 0 taken 151886 times.
✓ Branch 1 taken 353611 times.
505497 if (pkt->pts == AV_NOPTS_VALUE)
1125 151886 pkt->pts = sti->cur_dts;
1126 505497 pkt->dts = pkt->pts;
1127
3/4
✓ Branch 0 taken 505497 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 505489 times.
✓ Branch 3 taken 8 times.
505497 if (pkt->pts != AV_NOPTS_VALUE && duration.num >= 0)
1128 505489 sti->cur_dts = av_add_stable(st->time_base, pkt->pts, duration, 1);
1129 }
1130 }
1131
1132
3/4
✓ Branch 0 taken 519276 times.
✓ Branch 1 taken 48119 times.
✓ Branch 2 taken 519276 times.
✗ Branch 3 not taken.
567395 if (pkt->pts != AV_NOPTS_VALUE && delay <= MAX_REORDER_DELAY) {
1133 519276 sti->pts_buffer[0] = pkt->pts;
1134
4/4
✓ Branch 0 taken 21558 times.
✓ Branch 1 taken 514180 times.
✓ Branch 2 taken 16462 times.
✓ Branch 3 taken 5096 times.
535738 for (int i = 0; i < delay && sti->pts_buffer[i] > sti->pts_buffer[i + 1]; i++)
1135 16462 FFSWAP(int64_t, sti->pts_buffer[i], sti->pts_buffer[i + 1]);
1136
1137
2/2
✓ Branch 1 taken 518653 times.
✓ Branch 2 taken 623 times.
519276 if (has_decode_delay_been_guessed(st))
1138 518653 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 49429 times.
✓ Branch 1 taken 517966 times.
567395 if (!onein_oneout)
1142 // This should happen on the first packet
1143 49429 update_initial_timestamps(s, pkt->stream_index, pkt->dts, pkt->pts, pkt);
1144
2/2
✓ Branch 0 taken 7632 times.
✓ Branch 1 taken 559763 times.
567395 if (pkt->dts > sti->cur_dts)
1145 7632 sti->cur_dts = pkt->dts;
1146
1147
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 567395 times.
567395 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 567310 times.
✓ Branch 1 taken 85 times.
✓ Branch 3 taken 435592 times.
✓ Branch 4 taken 131718 times.
567395 if (st->codecpar->codec_type == AVMEDIA_TYPE_DATA || ff_is_intra_only(st->codecpar->codec_id))
1153 435677 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 692747 static int parse_packet(AVFormatContext *s, AVPacket *pkt,
1163 int stream_index, int flush)
1164 {
1165 692747 FormatContextInternal *const fci = ff_fc_internal(s);
1166 692747 FFFormatContext *const si = &fci->fc;
1167 692747 AVPacket *out_pkt = si->parse_pkt;
1168 692747 AVStream *st = s->streams[stream_index];
1169 692747 FFStream *const sti = ffstream(st);
1170 692747 const uint8_t *data = pkt->data;
1171 692747 int size = pkt->size;
1172 692747 int ret = 0, got_output = flush;
1173
1174
3/6
✓ Branch 0 taken 2002 times.
✓ Branch 1 taken 690745 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2002 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
692747 if (!size && !flush && sti->parser->flags & PARSER_FLAG_COMPLETE_FRAMES) {
1175 // preserve 0-size sync packets
1176 compute_pkt_fields(s, st, sti->parser, pkt, AV_NOPTS_VALUE, AV_NOPTS_VALUE);
1177 }
1178
1179
6/6
✓ Branch 0 taken 806670 times.
✓ Branch 1 taken 695908 times.
✓ Branch 2 taken 5163 times.
✓ Branch 3 taken 690745 times.
✓ Branch 4 taken 3161 times.
✓ Branch 5 taken 2002 times.
1502578 while (size > 0 || (flush && got_output)) {
1180 809831 int64_t next_pts = pkt->pts;
1181 809831 int64_t next_dts = pkt->dts;
1182 int len;
1183
1184 809831 len = av_parser_parse2(sti->parser, sti->avctx,
1185 &out_pkt->data, &out_pkt->size, data, size,
1186 pkt->pts, pkt->dts, pkt->pos);
1187
1188 809831 pkt->pts = pkt->dts = AV_NOPTS_VALUE;
1189 809831 pkt->pos = -1;
1190 /* increment read pointer */
1191 av_assert1(data || !len);
1192
2/2
✓ Branch 0 taken 797005 times.
✓ Branch 1 taken 12826 times.
809831 data = len ? data + len : data;
1193 809831 size -= len;
1194
1195 809831 got_output = !!out_pkt->size;
1196
1197
2/2
✓ Branch 0 taken 626816 times.
✓ Branch 1 taken 183015 times.
809831 if (!out_pkt->size)
1198 626816 continue;
1199
1200
4/4
✓ Branch 0 taken 181856 times.
✓ Branch 1 taken 1159 times.
✓ Branch 2 taken 65580 times.
✓ Branch 3 taken 116276 times.
183015 if (pkt->buf && out_pkt->data == pkt->data) {
1201 /* reference pkt->buf only when out_pkt->data is guaranteed to point
1202 * to data in it and not in the parser's internal buffer. */
1203 /* XXX: Ensure this is the case with all parsers when sti->parser->flags
1204 * is PARSER_FLAG_COMPLETE_FRAMES and check for that instead? */
1205 65580 out_pkt->buf = av_buffer_ref(pkt->buf);
1206
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 65580 times.
65580 if (!out_pkt->buf) {
1207 ret = AVERROR(ENOMEM);
1208 goto fail;
1209 }
1210 } else {
1211 117435 ret = av_packet_make_refcounted(out_pkt);
1212
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 117435 times.
117435 if (ret < 0)
1213 goto fail;
1214 }
1215
1216
2/2
✓ Branch 0 taken 9163 times.
✓ Branch 1 taken 173852 times.
183015 if (pkt->side_data) {
1217 9163 out_pkt->side_data = pkt->side_data;
1218 9163 out_pkt->side_data_elems = pkt->side_data_elems;
1219 9163 pkt->side_data = NULL;
1220 9163 pkt->side_data_elems = 0;
1221 }
1222
1223 /* set the duration */
1224
2/2
✓ Branch 0 taken 45350 times.
✓ Branch 1 taken 137665 times.
183015 out_pkt->duration = (sti->parser->flags & PARSER_FLAG_COMPLETE_FRAMES) ? pkt->duration : 0;
1225
2/2
✓ Branch 0 taken 107870 times.
✓ Branch 1 taken 75145 times.
183015 if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
1226
2/2
✓ Branch 0 taken 107764 times.
✓ Branch 1 taken 106 times.
107870 if (sti->avctx->sample_rate > 0) {
1227 107764 out_pkt->duration =
1228 107764 av_rescale_q_rnd(sti->parser->duration,
1229 107764 (AVRational) { 1, sti->avctx->sample_rate },
1230 st->time_base,
1231 AV_ROUND_DOWN);
1232 }
1233
2/2
✓ Branch 0 taken 1822 times.
✓ Branch 1 taken 73323 times.
75145 } else if (st->codecpar->codec_id == AV_CODEC_ID_GIF) {
1234
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 &&
1235
1/2
✓ Branch 0 taken 1822 times.
✗ Branch 1 not taken.
1822 sti->parser->duration) {
1236 1822 out_pkt->duration = sti->parser->duration;
1237 }
1238 }
1239
1240 183015 out_pkt->stream_index = st->index;
1241 183015 out_pkt->pts = sti->parser->pts;
1242 183015 out_pkt->dts = sti->parser->dts;
1243 183015 out_pkt->pos = sti->parser->pos;
1244 183015 out_pkt->flags |= pkt->flags & (AV_PKT_FLAG_DISCARD | AV_PKT_FLAG_CORRUPT);
1245
1246
2/2
✓ Branch 0 taken 114809 times.
✓ Branch 1 taken 68206 times.
183015 if (sti->need_parsing == AVSTREAM_PARSE_FULL_RAW)
1247 114809 out_pkt->pos = sti->parser->frame_offset;
1248
1249
2/2
✓ Branch 0 taken 177034 times.
✓ Branch 1 taken 5981 times.
183015 if (sti->parser->key_frame == 1 ||
1250
2/2
✓ Branch 0 taken 98226 times.
✓ Branch 1 taken 78808 times.
177034 (sti->parser->key_frame == -1 &&
1251
2/2
✓ Branch 0 taken 86326 times.
✓ Branch 1 taken 11900 times.
98226 sti->parser->pict_type == AV_PICTURE_TYPE_I))
1252 92307 out_pkt->flags |= AV_PKT_FLAG_KEY;
1253
1254
6/6
✓ Branch 0 taken 98226 times.
✓ Branch 1 taken 84789 times.
✓ Branch 2 taken 317 times.
✓ Branch 3 taken 97909 times.
✓ Branch 4 taken 288 times.
✓ Branch 5 taken 29 times.
183015 if (sti->parser->key_frame == -1 && sti->parser->pict_type ==AV_PICTURE_TYPE_NONE && (pkt->flags&AV_PKT_FLAG_KEY))
1255 288 out_pkt->flags |= AV_PKT_FLAG_KEY;
1256
1257 183015 compute_pkt_fields(s, st, sti->parser, out_pkt, next_dts, next_pts);
1258
1259 183015 ret = avpriv_packet_list_put(&fci->parse_queue,
1260 out_pkt, NULL, 0);
1261
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 183015 times.
183015 if (ret < 0)
1262 goto fail;
1263 }
1264
1265 /* end of the stream => close and free the parser */
1266
2/2
✓ Branch 0 taken 690745 times.
✓ Branch 1 taken 2002 times.
692747 if (flush) {
1267 2002 av_parser_close(sti->parser);
1268 2002 sti->parser = NULL;
1269 }
1270
1271 690745 fail:
1272
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 692747 times.
692747 if (ret < 0)
1273 av_packet_unref(out_pkt);
1274 692747 av_packet_unref(pkt);
1275 692747 return ret;
1276 }
1277
1278 8774 static int64_t ts_to_samples(AVStream *st, int64_t ts)
1279 {
1280 8774 return av_rescale(ts, st->time_base.num * st->codecpar->sample_rate, st->time_base.den);
1281 }
1282
1283 8193 static int codec_close(FFStream *sti)
1284 {
1285 8193 AVCodecContext *avctx_new = NULL;
1286 8193 AVCodecParameters *par_tmp = NULL;
1287 int ret;
1288
1289 8193 avctx_new = avcodec_alloc_context3(sti->avctx->codec);
1290
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8193 times.
8193 if (!avctx_new) {
1291 ret = AVERROR(ENOMEM);
1292 goto fail;
1293 }
1294
1295 8193 par_tmp = avcodec_parameters_alloc();
1296
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8193 times.
8193 if (!par_tmp) {
1297 ret = AVERROR(ENOMEM);
1298 goto fail;
1299 }
1300
1301 8193 ret = avcodec_parameters_from_context(par_tmp, sti->avctx);
1302
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8193 times.
8193 if (ret < 0)
1303 goto fail;
1304
1305 8193 ret = avcodec_parameters_to_context(avctx_new, par_tmp);
1306
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8193 times.
8193 if (ret < 0)
1307 goto fail;
1308
1309 8193 avctx_new->pkt_timebase = sti->avctx->pkt_timebase;
1310
1311 #if FF_API_TICKS_PER_FRAME
1312 FF_DISABLE_DEPRECATION_WARNINGS
1313 8193 avctx_new->ticks_per_frame = sti->avctx->ticks_per_frame;
1314 FF_ENABLE_DEPRECATION_WARNINGS
1315 #endif
1316
1317 8193 avcodec_free_context(&sti->avctx);
1318 8193 sti->avctx = avctx_new;
1319
1320 8193 avctx_new = NULL;
1321 8193 ret = 0;
1322
1323 8193 fail:
1324 8193 avcodec_free_context(&avctx_new);
1325 8193 avcodec_parameters_free(&par_tmp);
1326
1327 8193 return ret;
1328 }
1329
1330 static int extract_extradata(FFFormatContext *si, AVStream *st, const AVPacket *pkt);
1331
1332 571804 static int read_frame_internal(AVFormatContext *s, AVPacket *pkt)
1333 {
1334 571804 FormatContextInternal *const fci = ff_fc_internal(s);
1335 571804 FFFormatContext *const si = &fci->fc;
1336 571804 int ret, got_packet = 0;
1337 571804 AVDictionary *metadata = NULL;
1338
1339
4/4
✓ Branch 0 taken 1262680 times.
✓ Branch 1 taken 384380 times.
✓ Branch 2 taken 1081631 times.
✓ Branch 3 taken 181049 times.
1647060 while (!got_packet && !fci->parse_queue.head) {
1340 AVStream *st;
1341 FFStream *sti;
1342
1343 /* read next packet */
1344 1081631 ret = ff_read_packet(s, pkt);
1345
2/2
✓ Branch 0 taken 6375 times.
✓ Branch 1 taken 1075256 times.
1081631 if (ret < 0) {
1346
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6374 times.
6375 if (ret == AVERROR(EAGAIN))
1347 1 return ret;
1348 /* flush the parsers */
1349
2/2
✓ Branch 0 taken 7363 times.
✓ Branch 1 taken 6374 times.
13737 for (unsigned i = 0; i < s->nb_streams; i++) {
1350 7363 AVStream *const st = s->streams[i];
1351 7363 FFStream *const sti = ffstream(st);
1352
4/4
✓ Branch 0 taken 2644 times.
✓ Branch 1 taken 4719 times.
✓ Branch 2 taken 2002 times.
✓ Branch 3 taken 642 times.
7363 if (sti->parser && sti->need_parsing)
1353 2002 parse_packet(s, pkt, st->index, 1);
1354 }
1355 /* all remaining packets are now in parse_queue =>
1356 * really terminate parsing */
1357 6374 break;
1358 }
1359 1075256 ret = 0;
1360 1075256 st = s->streams[pkt->stream_index];
1361 1075256 sti = ffstream(st);
1362
1363 1075256 st->event_flags |= AVSTREAM_EVENT_FLAG_NEW_PACKETS;
1364
1365 /* update context if required */
1366
2/2
✓ Branch 0 taken 166 times.
✓ Branch 1 taken 1075090 times.
1075256 if (sti->need_context_update) {
1367
2/2
✓ Branch 1 taken 13 times.
✓ Branch 2 taken 153 times.
166 if (avcodec_is_open(sti->avctx)) {
1368 13 av_log(s, AV_LOG_DEBUG, "Demuxer context update while decoder is open, closing and trying to re-open\n");
1369 13 ret = codec_close(sti);
1370 13 sti->info->found_decoder = 0;
1371
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 if (ret < 0)
1372 return ret;
1373 }
1374
1375 /* close parser, because it depends on the codec */
1376
4/4
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 153 times.
✓ Branch 2 taken 10 times.
✓ Branch 3 taken 3 times.
166 if (sti->parser && sti->avctx->codec_id != st->codecpar->codec_id) {
1377 10 av_parser_close(sti->parser);
1378 10 sti->parser = NULL;
1379 }
1380
1381 166 ret = avcodec_parameters_to_context(sti->avctx, st->codecpar);
1382
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 166 times.
166 if (ret < 0) {
1383 av_packet_unref(pkt);
1384 return ret;
1385 }
1386
1387
2/2
✓ Branch 0 taken 146 times.
✓ Branch 1 taken 20 times.
166 if (!sti->avctx->extradata) {
1388 146 sti->extract_extradata.inited = 0;
1389
1390 146 ret = extract_extradata(si, st, pkt);
1391
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 146 times.
146 if (ret < 0) {
1392 av_packet_unref(pkt);
1393 return ret;
1394 }
1395 }
1396
1397 166 sti->codec_desc = avcodec_descriptor_get(sti->avctx->codec_id);
1398
1399 166 sti->need_context_update = 0;
1400 }
1401
1402
2/2
✓ Branch 0 taken 335359 times.
✓ Branch 1 taken 739897 times.
1075256 if (pkt->pts != AV_NOPTS_VALUE &&
1403
2/2
✓ Branch 0 taken 167088 times.
✓ Branch 1 taken 168271 times.
335359 pkt->dts != AV_NOPTS_VALUE &&
1404
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 167088 times.
167088 pkt->pts < pkt->dts) {
1405 av_log(s, AV_LOG_WARNING,
1406 "Invalid timestamps stream=%d, pts=%s, dts=%s, size=%d\n",
1407 pkt->stream_index,
1408 av_ts2str(pkt->pts),
1409 av_ts2str(pkt->dts),
1410 pkt->size);
1411 }
1412
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1075256 times.
1075256 if (s->debug & FF_FDEBUG_TS)
1413 av_log(s, AV_LOG_DEBUG,
1414 "ff_read_packet stream=%d, pts=%s, dts=%s, size=%d, duration=%"PRId64", flags=%d\n",
1415 pkt->stream_index,
1416 av_ts2str(pkt->pts),
1417 av_ts2str(pkt->dts),
1418 pkt->size, pkt->duration, pkt->flags);
1419
1420
5/6
✓ Branch 0 taken 692072 times.
✓ Branch 1 taken 383184 times.
✓ Branch 2 taken 2764 times.
✓ Branch 3 taken 689308 times.
✓ Branch 4 taken 2764 times.
✗ Branch 5 not taken.
1075256 if (sti->need_parsing && !sti->parser && !(s->flags & AVFMT_FLAG_NOPARSE)) {
1421 2764 sti->parser = av_parser_init(st->codecpar->codec_id);
1422
2/2
✓ Branch 0 taken 1196 times.
✓ Branch 1 taken 1568 times.
2764 if (!sti->parser) {
1423 1196 av_log(s, AV_LOG_VERBOSE, "parser not found for codec "
1424 "%s, packets or times may be invalid.\n",
1425 1196 avcodec_get_name(st->codecpar->codec_id));
1426 /* no parser available: just output the raw packets */
1427 1196 sti->need_parsing = AVSTREAM_PARSE_NONE;
1428
2/2
✓ Branch 0 taken 673 times.
✓ Branch 1 taken 895 times.
1568 } else if (sti->need_parsing == AVSTREAM_PARSE_HEADERS)
1429 673 sti->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES;
1430
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 895 times.
895 else if (sti->need_parsing == AVSTREAM_PARSE_FULL_ONCE)
1431 sti->parser->flags |= PARSER_FLAG_ONCE;
1432
2/2
✓ Branch 0 taken 471 times.
✓ Branch 1 taken 424 times.
895 else if (sti->need_parsing == AVSTREAM_PARSE_FULL_RAW)
1433 471 sti->parser->flags |= PARSER_FLAG_USE_CODEC_TS;
1434 }
1435
1436
3/4
✓ Branch 0 taken 690876 times.
✓ Branch 1 taken 384380 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 690876 times.
1075256 if (!sti->need_parsing || !sti->parser) {
1437 /* no parsing needed: we just output the packet as is */
1438 384380 compute_pkt_fields(s, st, NULL, pkt, AV_NOPTS_VALUE, AV_NOPTS_VALUE);
1439
2/2
✓ Branch 0 taken 94093 times.
✓ Branch 1 taken 290287 times.
384380 if ((s->iformat->flags & AVFMT_GENERIC_INDEX) &&
1440
4/4
✓ Branch 0 taken 93384 times.
✓ Branch 1 taken 709 times.
✓ Branch 2 taken 92056 times.
✓ Branch 3 taken 1328 times.
94093 (pkt->flags & AV_PKT_FLAG_KEY) && pkt->dts != AV_NOPTS_VALUE) {
1441 92056 ff_reduce_index(s, st->index);
1442 92056 av_add_index_entry(st, pkt->pos, pkt->dts,
1443 0, 0, AVINDEX_KEYFRAME);
1444 }
1445 384380 got_packet = 1;
1446
2/2
✓ Branch 0 taken 690745 times.
✓ Branch 1 taken 131 times.
690876 } else if (st->discard < AVDISCARD_ALL) {
1447
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 690745 times.
690745 if ((ret = parse_packet(s, pkt, pkt->stream_index, 0)) < 0)
1448 return ret;
1449 690745 st->codecpar->sample_rate = sti->avctx->sample_rate;
1450 690745 st->codecpar->bit_rate = sti->avctx->bit_rate;
1451 690745 ret = av_channel_layout_copy(&st->codecpar->ch_layout, &sti->avctx->ch_layout);
1452
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 690745 times.
690745 if (ret < 0)
1453 return ret;
1454 690745 st->codecpar->codec_id = sti->avctx->codec_id;
1455 } else {
1456 /* free packet */
1457 131 av_packet_unref(pkt);
1458 }
1459
2/2
✓ Branch 0 taken 359080 times.
✓ Branch 1 taken 716176 times.
1075256 if (pkt->flags & AV_PKT_FLAG_KEY)
1460 359080 sti->skip_to_keyframe = 0;
1461
2/2
✓ Branch 0 taken 85 times.
✓ Branch 1 taken 1075171 times.
1075256 if (sti->skip_to_keyframe) {
1462 85 av_packet_unref(pkt);
1463 85 got_packet = 0;
1464 }
1465 }
1466
1467
4/4
✓ Branch 0 taken 187423 times.
✓ Branch 1 taken 384380 times.
✓ Branch 2 taken 181951 times.
✓ Branch 3 taken 5472 times.
571803 if (!got_packet && fci->parse_queue.head)
1468 181951 ret = avpriv_packet_list_get(&fci->parse_queue, pkt);
1469
1470
2/2
✓ Branch 0 taken 566331 times.
✓ Branch 1 taken 5472 times.
571803 if (ret >= 0) {
1471 566331 AVStream *const st = s->streams[pkt->stream_index];
1472 566331 FFStream *const sti = ffstream(st);
1473 566331 int discard_padding = 0;
1474
3/4
✓ Branch 0 taken 4387 times.
✓ Branch 1 taken 561944 times.
✓ Branch 2 taken 4387 times.
✗ Branch 3 not taken.
566331 if (sti->first_discard_sample && pkt->pts != AV_NOPTS_VALUE) {
1475
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);
1476 4387 int64_t sample = ts_to_samples(st, pts);
1477 4387 int64_t duration = ts_to_samples(st, pkt->duration);
1478 4387 int64_t end_sample = sample + duration;
1479
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 &&
1480
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 sample < sti->last_discard_sample)
1481 14 discard_padding = FFMIN(end_sample - sti->first_discard_sample, duration);
1482 }
1483
6/6
✓ Branch 0 taken 4387 times.
✓ Branch 1 taken 561944 times.
✓ Branch 2 taken 4371 times.
✓ Branch 3 taken 16 times.
✓ Branch 4 taken 20 times.
✓ Branch 5 taken 4351 times.
566331 if (sti->start_skip_samples && (pkt->pts == 0 || pkt->pts == RELATIVE_TS_BASE))
1484 36 sti->skip_samples = sti->start_skip_samples;
1485 566331 sti->skip_samples = FFMAX(0, sti->skip_samples);
1486
4/4
✓ Branch 0 taken 566243 times.
✓ Branch 1 taken 88 times.
✓ Branch 2 taken 14 times.
✓ Branch 3 taken 566229 times.
566331 if (sti->skip_samples || discard_padding) {
1487 102 uint8_t *p = av_packet_new_side_data(pkt, AV_PKT_DATA_SKIP_SAMPLES, 10);
1488
1/2
✓ Branch 0 taken 102 times.
✗ Branch 1 not taken.
102 if (p) {
1489 102 AV_WL32(p, sti->skip_samples);
1490 102 AV_WL32(p + 4, discard_padding);
1491 102 av_log(s, AV_LOG_DEBUG, "demuxer injecting skip %u / discard %u\n",
1492 102 (unsigned)sti->skip_samples, (unsigned)discard_padding);
1493 }
1494 102 sti->skip_samples = 0;
1495 }
1496
1497 #if FF_API_AVSTREAM_SIDE_DATA
1498
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 566331 times.
566331 if (sti->inject_global_side_data) {
1499 for (int i = 0; i < st->codecpar->nb_coded_side_data; i++) {
1500 const AVPacketSideData *const src_sd = &st->codecpar->coded_side_data[i];
1501 uint8_t *dst_data;
1502
1503 if (av_packet_get_side_data(pkt, src_sd->type, NULL))
1504 continue;
1505
1506 dst_data = av_packet_new_side_data(pkt, src_sd->type, src_sd->size);
1507 if (!dst_data) {
1508 av_log(s, AV_LOG_WARNING, "Could not inject global side data\n");
1509 continue;
1510 }
1511
1512 memcpy(dst_data, src_sd->data, src_sd->size);
1513 }
1514 sti->inject_global_side_data = 0;
1515 }
1516 #endif
1517 }
1518
1519
2/2
✓ Branch 0 taken 7533 times.
✓ Branch 1 taken 564270 times.
571803 if (!fci->metafree) {
1520 7533 int metaret = av_opt_get_dict_val(s, "metadata", AV_OPT_SEARCH_CHILDREN, &metadata);
1521
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7533 times.
7533 if (metadata) {
1522 s->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
1523 av_dict_copy(&s->metadata, metadata, 0);
1524 av_dict_free(&metadata);
1525 av_opt_set_dict_val(s, "metadata", NULL, AV_OPT_SEARCH_CHILDREN);
1526 }
1527 7533 fci->metafree = metaret == AVERROR_OPTION_NOT_FOUND;
1528 }
1529
1530
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 571803 times.
571803 if (s->debug & FF_FDEBUG_TS)
1531 av_log(s, AV_LOG_DEBUG,
1532 "read_frame_internal stream=%d, pts=%s, dts=%s, "
1533 "size=%d, duration=%"PRId64", flags=%d\n",
1534 pkt->stream_index,
1535 av_ts2str(pkt->pts),
1536 av_ts2str(pkt->dts),
1537 pkt->size, pkt->duration, pkt->flags);
1538
1539 /* A demuxer might have returned EOF because of an IO error, let's
1540 * propagate this back to the user. */
1541
5/8
✓ Branch 0 taken 5360 times.
✓ Branch 1 taken 566443 times.
✓ Branch 2 taken 5143 times.
✓ Branch 3 taken 217 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 5143 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
571803 if (ret == AVERROR_EOF && s->pb && s->pb->error < 0 && s->pb->error != AVERROR(EAGAIN))
1542 ret = s->pb->error;
1543
1544 571803 return ret;
1545 }
1546
1547 510490 int av_read_frame(AVFormatContext *s, AVPacket *pkt)
1548 {
1549 510490 FFFormatContext *const si = ffformatcontext(s);
1550 510490 const int genpts = s->flags & AVFMT_FLAG_GENPTS;
1551 510490 int eof = 0;
1552 int ret;
1553 AVStream *st;
1554
1555
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 510490 times.
510490 if (!genpts) {
1556 1020980 ret = si->packet_buffer.head
1557 131879 ? avpriv_packet_list_get(&si->packet_buffer, pkt)
1558
2/2
✓ Branch 0 taken 131879 times.
✓ Branch 1 taken 378611 times.
510490 : read_frame_internal(s, pkt);
1559
2/2
✓ Branch 0 taken 4299 times.
✓ Branch 1 taken 506191 times.
510490 if (ret < 0)
1560 4299 return ret;
1561 506191 goto return_packet;
1562 }
1563
1564 for (;;) {
1565 PacketListEntry *pktl = si->packet_buffer.head;
1566
1567 if (pktl) {
1568 AVPacket *next_pkt = &pktl->pkt;
1569
1570 if (next_pkt->dts != AV_NOPTS_VALUE) {
1571 int wrap_bits = s->streams[next_pkt->stream_index]->pts_wrap_bits;
1572 // last dts seen for this stream. if any of packets following
1573 // current one had no dts, we will set this to AV_NOPTS_VALUE.
1574 int64_t last_dts = next_pkt->dts;
1575 av_assert2(wrap_bits <= 64);
1576 while (pktl && next_pkt->pts == AV_NOPTS_VALUE) {
1577 if (pktl->pkt.stream_index == next_pkt->stream_index &&
1578 av_compare_mod(next_pkt->dts, pktl->pkt.dts, 2ULL << (wrap_bits - 1)) < 0) {
1579 if (av_compare_mod(pktl->pkt.pts, pktl->pkt.dts, 2ULL << (wrap_bits - 1))) {
1580 // not B-frame
1581 next_pkt->pts = pktl->pkt.dts;
1582 }
1583 if (last_dts != AV_NOPTS_VALUE) {
1584 // Once last dts was set to AV_NOPTS_VALUE, we don't change it.
1585 last_dts = pktl->pkt.dts;
1586 }
1587 }
1588 pktl = pktl->next;
1589 }
1590 if (eof && next_pkt->pts == AV_NOPTS_VALUE && last_dts != AV_NOPTS_VALUE) {
1591 // Fixing the last reference frame had none pts issue (For MXF etc).
1592 // We only do this when
1593 // 1. eof.
1594 // 2. we are not able to resolve a pts value for current packet.
1595 // 3. the packets for this stream at the end of the files had valid dts.
1596 next_pkt->pts = last_dts + next_pkt->duration;
1597 }
1598 pktl = si->packet_buffer.head;
1599 }
1600
1601 /* read packet from packet buffer, if there is data */
1602 st = s->streams[next_pkt->stream_index];
1603 if (!(next_pkt->pts == AV_NOPTS_VALUE && st->discard < AVDISCARD_ALL &&
1604 next_pkt->dts != AV_NOPTS_VALUE && !eof)) {
1605 ret = avpriv_packet_list_get(&si->packet_buffer, pkt);
1606 goto return_packet;
1607 }
1608 }
1609
1610 ret = read_frame_internal(s, pkt);
1611 if (ret < 0) {
1612 if (pktl && ret != AVERROR(EAGAIN)) {
1613 eof = 1;
1614 continue;
1615 } else
1616 return ret;
1617 }
1618
1619 ret = avpriv_packet_list_put(&si->packet_buffer,
1620 pkt, NULL, 0);
1621 if (ret < 0) {
1622 av_packet_unref(pkt);
1623 return ret;
1624 }
1625 }
1626
1627 506191 return_packet:
1628 506191 st = s->streams[pkt->stream_index];
1629
4/4
✓ Branch 0 taken 194017 times.
✓ Branch 1 taken 312174 times.
✓ Branch 2 taken 120654 times.
✓ Branch 3 taken 73363 times.
506191 if ((s->iformat->flags & AVFMT_GENERIC_INDEX) && pkt->flags & AV_PKT_FLAG_KEY) {
1630 120654 ff_reduce_index(s, st->index);
1631 120654 av_add_index_entry(st, pkt->pos, pkt->dts, 0, 0, AVINDEX_KEYFRAME);
1632 }
1633
1634
2/2
✓ Branch 1 taken 119332 times.
✓ Branch 2 taken 386859 times.
506191 if (is_relative(pkt->dts))
1635 119332 pkt->dts -= RELATIVE_TS_BASE;
1636
2/2
✓ Branch 1 taken 117637 times.
✓ Branch 2 taken 388554 times.
506191 if (is_relative(pkt->pts))
1637 117637 pkt->pts -= RELATIVE_TS_BASE;
1638
1639 506191 return ret;
1640 }
1641
1642 /**
1643 * Return TRUE if the stream has accurate duration in any stream.
1644 *
1645 * @return TRUE if the stream has accurate duration for at least one component.
1646 */
1647 7455 static int has_duration(AVFormatContext *ic)
1648 {
1649
2/2
✓ Branch 0 taken 7701 times.
✓ Branch 1 taken 2407 times.
10108 for (unsigned i = 0; i < ic->nb_streams; i++) {
1650 7701 const AVStream *const st = ic->streams[i];
1651
2/2
✓ Branch 0 taken 5048 times.
✓ Branch 1 taken 2653 times.
7701 if (st->duration != AV_NOPTS_VALUE)
1652 5048 return 1;
1653 }
1654
2/2
✓ Branch 0 taken 433 times.
✓ Branch 1 taken 1974 times.
2407 if (ic->duration != AV_NOPTS_VALUE)
1655 433 return 1;
1656 1974 return 0;
1657 }
1658
1659 /**
1660 * Estimate the stream timings from the one of each components.
1661 *
1662 * Also computes the global bitrate if possible.
1663 */
1664 13074 static void update_stream_timings(AVFormatContext *ic)
1665 {
1666 int64_t start_time, start_time1, start_time_text, end_time, end_time1, end_time_text;
1667 int64_t duration, duration1, duration_text, filesize;
1668
1669 13074 start_time = INT64_MAX;
1670 13074 start_time_text = INT64_MAX;
1671 13074 end_time = INT64_MIN;
1672 13074 end_time_text = INT64_MIN;
1673 13074 duration = INT64_MIN;
1674 13074 duration_text = INT64_MIN;
1675
1676
2/2
✓ Branch 0 taken 14474 times.
✓ Branch 1 taken 13074 times.
27548 for (unsigned i = 0; i < ic->nb_streams; i++) {
1677 14474 AVStream *const st = ic->streams[i];
1678
2/2
✓ Branch 0 taken 14349 times.
✓ Branch 1 taken 125 times.
28823 int is_text = st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE ||
1679
2/2
✓ Branch 0 taken 151 times.
✓ Branch 1 taken 14198 times.
14349 st->codecpar->codec_type == AVMEDIA_TYPE_DATA;
1680
1681
3/4
✓ Branch 0 taken 11850 times.
✓ Branch 1 taken 2624 times.
✓ Branch 2 taken 11850 times.
✗ Branch 3 not taken.
14474 if (st->start_time != AV_NOPTS_VALUE && st->time_base.den) {
1682 11850 start_time1 = av_rescale_q(st->start_time, st->time_base,
1683 11850 AV_TIME_BASE_Q);
1684
2/2
✓ Branch 0 taken 158 times.
✓ Branch 1 taken 11692 times.
11850 if (is_text)
1685 158 start_time_text = FFMIN(start_time_text, start_time1);
1686 else
1687 11692 start_time = FFMIN(start_time, start_time1);
1688 11850 end_time1 = av_rescale_q_rnd(st->duration, st->time_base,
1689 11850 AV_TIME_BASE_Q,
1690 AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
1691
5/6
✓ Branch 0 taken 10528 times.
✓ Branch 1 taken 1322 times.
✓ Branch 2 taken 10499 times.
✓ Branch 3 taken 29 times.
✓ Branch 4 taken 10528 times.
✗ Branch 5 not taken.
11850 if (end_time1 != AV_NOPTS_VALUE && (end_time1 > 0 ? start_time1 <= INT64_MAX - end_time1 : start_time1 >= INT64_MIN - end_time1)) {
1692 10528 end_time1 += start_time1;
1693
2/2
✓ Branch 0 taken 143 times.
✓ Branch 1 taken 10385 times.
10528 if (is_text)
1694 143 end_time_text = FFMAX(end_time_text, end_time1);
1695 else
1696 10385 end_time = FFMAX(end_time, end_time1);
1697 }
1698
2/2
✓ Branch 1 taken 208 times.
✓ Branch 2 taken 11850 times.
12058 for (AVProgram *p = NULL; (p = av_find_program_from_stream(ic, p, i)); ) {
1699
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)
1700 81 p->start_time = start_time1;
1701
2/2
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 156 times.
208 if (p->end_time < end_time1)
1702 52 p->end_time = end_time1;
1703 }
1704 }
1705
2/2
✓ Branch 0 taken 11983 times.
✓ Branch 1 taken 2491 times.
14474 if (st->duration != AV_NOPTS_VALUE) {
1706 11983 duration1 = av_rescale_q(st->duration, st->time_base,
1707 11983 AV_TIME_BASE_Q);
1708
2/2
✓ Branch 0 taken 167 times.
✓ Branch 1 taken 11816 times.
11983 if (is_text)
1709 167 duration_text = FFMAX(duration_text, duration1);
1710 else
1711 11816 duration = FFMAX(duration, duration1);
1712 }
1713 }
1714
3/6
✓ Branch 0 taken 10645 times.
✓ Branch 1 taken 2429 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 10645 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
13074 if (start_time == INT64_MAX || (start_time > start_time_text && start_time - (uint64_t)start_time_text < AV_TIME_BASE))
1715 2429 start_time = start_time_text;
1716
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10645 times.
10645 else if (start_time > start_time_text)
1717 av_log(ic, AV_LOG_VERBOSE, "Ignoring outlier non primary stream starttime %f\n", start_time_text / (float)AV_TIME_BASE);
1718
1719
5/6
✓ Branch 0 taken 9679 times.
✓ Branch 1 taken 3395 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 9677 times.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
13074 if (end_time == INT64_MIN || (end_time < end_time_text && end_time_text - (uint64_t)end_time < AV_TIME_BASE))
1720 3397 end_time = end_time_text;
1721
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9677 times.
9677 else if (end_time < end_time_text)
1722 av_log(ic, AV_LOG_VERBOSE, "Ignoring outlier non primary stream endtime %f\n", end_time_text / (float)AV_TIME_BASE);
1723
1724
5/6
✓ Branch 0 taken 11095 times.
✓ Branch 1 taken 1979 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 11089 times.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
13074 if (duration == INT64_MIN || (duration < duration_text && (uint64_t)duration_text - duration < AV_TIME_BASE))
1725 1985 duration = duration_text;
1726
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11089 times.
11089 else if (duration < duration_text)
1727 av_log(ic, AV_LOG_VERBOSE, "Ignoring outlier non primary stream duration %f\n", duration_text / (float)AV_TIME_BASE);
1728
1729
2/2
✓ Branch 0 taken 10666 times.
✓ Branch 1 taken 2408 times.
13074 if (start_time != INT64_MAX) {
1730 10666 ic->start_time = start_time;
1731
2/2
✓ Branch 0 taken 9696 times.
✓ Branch 1 taken 970 times.
10666 if (end_time != INT64_MIN) {
1732
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9696 times.
9696 if (ic->nb_programs > 1) {
1733 for (unsigned i = 0; i < ic->nb_programs; i++) {
1734 AVProgram *const p = ic->programs[i];
1735
1736 if (p->start_time != AV_NOPTS_VALUE &&
1737 p->end_time > p->start_time &&
1738 p->end_time - (uint64_t)p->start_time <= INT64_MAX)
1739 duration = FFMAX(duration, p->end_time - p->start_time);
1740 }
1741
2/4
✓ Branch 0 taken 9696 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9696 times.
✗ Branch 3 not taken.
9696 } else if (end_time >= start_time && end_time - (uint64_t)start_time <= INT64_MAX) {
1742 9696 duration = FFMAX(duration, end_time - start_time);
1743 }
1744 }
1745 }
1746
6/6
✓ Branch 0 taken 11125 times.
✓ Branch 1 taken 1949 times.
✓ Branch 2 taken 11089 times.
✓ Branch 3 taken 36 times.
✓ Branch 4 taken 5959 times.
✓ Branch 5 taken 5130 times.
13074 if (duration != INT64_MIN && duration > 0 && ic->duration == AV_NOPTS_VALUE) {
1747 5959 ic->duration = duration;
1748 }
1749
5/6
✓ Branch 0 taken 7045 times.
✓ Branch 1 taken 6029 times.
✓ Branch 3 taken 7045 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 5938 times.
✓ Branch 6 taken 1107 times.
13074 if (ic->pb && (filesize = avio_size(ic->pb)) > 0 && ic->duration > 0) {
1750 /* compute the bitrate */
1751 5938 double bitrate = (double) filesize * 8.0 * AV_TIME_BASE /
1752 5938 (double) ic->duration;
1753
2/4
✓ Branch 0 taken 5938 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5938 times.
✗ Branch 3 not taken.
5938 if (bitrate >= 0 && bitrate <= INT64_MAX)
1754 5938 ic->bit_rate = bitrate;
1755 }
1756 13074 }
1757
1758 5550 static void fill_all_stream_timings(AVFormatContext *ic)
1759 {
1760 5550 update_stream_timings(ic);
1761
2/2
✓ Branch 0 taken 6186 times.
✓ Branch 1 taken 5550 times.
11736 for (unsigned i = 0; i < ic->nb_streams; i++) {
1762 6186 AVStream *const st = ic->streams[i];
1763
1764
2/2
✓ Branch 0 taken 771 times.
✓ Branch 1 taken 5415 times.
6186 if (st->start_time == AV_NOPTS_VALUE) {
1765
2/2
✓ Branch 0 taken 129 times.
✓ Branch 1 taken 642 times.
771 if (ic->start_time != AV_NOPTS_VALUE)
1766 129 st->start_time = av_rescale_q(ic->start_time, AV_TIME_BASE_Q,
1767 st->time_base);
1768
2/2
✓ Branch 0 taken 756 times.
✓ Branch 1 taken 15 times.
771 if (ic->duration != AV_NOPTS_VALUE)
1769 756 st->duration = av_rescale_q(ic->duration, AV_TIME_BASE_Q,
1770 st->time_base);
1771 }
1772 }
1773 5550 }
1774
1775 1974 static void estimate_timings_from_bit_rate(AVFormatContext *ic)
1776 {
1777 1974 FFFormatContext *const si = ffformatcontext(ic);
1778 1974 int show_warning = 0;
1779
1780 /* if bit_rate is already set, we believe it */
1781
2/2
✓ Branch 0 taken 1945 times.
✓ Branch 1 taken 29 times.
1974 if (ic->bit_rate <= 0) {
1782 1945 int64_t bit_rate = 0;
1783
2/2
✓ Branch 0 taken 2052 times.
✓ Branch 1 taken 1302 times.
3354 for (unsigned i = 0; i < ic->nb_streams; i++) {
1784 2052 const AVStream *const st = ic->streams[i];
1785 2052 const FFStream *const sti = cffstream(st);
1786
4/4
✓ Branch 0 taken 1279 times.
✓ Branch 1 taken 773 times.
✓ Branch 2 taken 116 times.
✓ Branch 3 taken 1163 times.
2052 if (st->codecpar->bit_rate <= 0 && sti->avctx->bit_rate > 0)
1787 116 st->codecpar->bit_rate = sti->avctx->bit_rate;
1788
2/2
✓ Branch 0 taken 889 times.
✓ Branch 1 taken 1163 times.
2052 if (st->codecpar->bit_rate > 0) {
1789
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 889 times.
889 if (INT64_MAX - st->codecpar->bit_rate < bit_rate) {
1790 bit_rate = 0;
1791 break;
1792 }
1793 889 bit_rate += st->codecpar->bit_rate;
1794
4/4
✓ Branch 0 taken 1014 times.
✓ Branch 1 taken 149 times.
✓ Branch 2 taken 643 times.
✓ Branch 3 taken 371 times.
1163 } else if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && sti->codec_info_nb_frames > 1) {
1795 // If we have a videostream with packets but without a bitrate
1796 // then consider the sum not known
1797 643 bit_rate = 0;
1798 643 break;
1799 }
1800 }
1801 1945 ic->bit_rate = bit_rate;
1802 }
1803
1804 /* if duration is already set, we believe it */
1805
1/2
✓ Branch 0 taken 1974 times.
✗ Branch 1 not taken.
1974 if (ic->duration == AV_NOPTS_VALUE &&
1806
2/2
✓ Branch 0 taken 890 times.
✓ Branch 1 taken 1084 times.
1974 ic->bit_rate != 0) {
1807
2/2
✓ Branch 0 taken 866 times.
✓ Branch 1 taken 24 times.
890 int64_t filesize = ic->pb ? avio_size(ic->pb) : 0;
1808
2/2
✓ Branch 0 taken 860 times.
✓ Branch 1 taken 30 times.
890 if (filesize > si->data_offset) {
1809 860 filesize -= si->data_offset;
1810
2/2
✓ Branch 0 taken 898 times.
✓ Branch 1 taken 860 times.
1758 for (unsigned i = 0; i < ic->nb_streams; i++) {
1811 898 AVStream *const st = ic->streams[i];
1812
1813
1/2
✓ Branch 0 taken 898 times.
✗ Branch 1 not taken.
898 if ( st->time_base.num <= INT64_MAX / ic->bit_rate
1814
1/2
✓ Branch 0 taken 898 times.
✗ Branch 1 not taken.
898 && st->duration == AV_NOPTS_VALUE) {
1815 898 st->duration = av_rescale(filesize, 8LL * st->time_base.den,
1816 898 ic->bit_rate *
1817 898 (int64_t) st->time_base.num);
1818 898 show_warning = 1;
1819 }
1820 }
1821 }
1822 }
1823
2/2
✓ Branch 0 taken 860 times.
✓ Branch 1 taken 1114 times.
1974 if (show_warning)
1824 860 av_log(ic, AV_LOG_WARNING,
1825 "Estimating duration from bitrate, this may be inaccurate\n");
1826 1974 }
1827
1828 #define DURATION_DEFAULT_MAX_READ_SIZE 250000LL
1829 #define DURATION_DEFAULT_MAX_RETRY 6
1830 #define DURATION_MAX_RETRY 1
1831
1832 /* only usable for MPEG-PS streams */
1833 69 static void estimate_timings_from_pts(AVFormatContext *ic, int64_t old_offset)
1834 {
1835 69 FFFormatContext *const si = ffformatcontext(ic);
1836 69 AVPacket *const pkt = si->pkt;
1837 int num, den, read_size, ret;
1838
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;
1839
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;
1840 69 int found_duration = 0;
1841 int is_end;
1842 int64_t filesize, offset, duration;
1843 69 int retry = 0;
1844
1845 /* flush packet queue */
1846 69 ff_flush_packet_queue(ic);
1847
1848
2/2
✓ Branch 0 taken 143 times.
✓ Branch 1 taken 69 times.
212 for (unsigned i = 0; i < ic->nb_streams; i++) {
1849 143 AVStream *const st = ic->streams[i];
1850 143 FFStream *const sti = ffstream(st);
1851
1852
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 121 times.
143 if (st->start_time == AV_NOPTS_VALUE &&
1853
1/2
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
22 sti->first_dts == AV_NOPTS_VALUE &&
1854
1/2
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
22 st->codecpar->codec_type != AVMEDIA_TYPE_UNKNOWN)
1855 22 av_log(ic, AV_LOG_WARNING,
1856 "start time for stream %d is not set in estimate_timings_from_pts\n", i);
1857
1858
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 119 times.
143 if (sti->parser) {
1859 24 av_parser_close(sti->parser);
1860 24 sti->parser = NULL;
1861 }
1862 }
1863
1864
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 69 times.
69 if (ic->skip_estimate_duration_from_pts) {
1865 av_log(ic, AV_LOG_INFO, "Skipping duration calculation in estimate_timings_from_pts\n");
1866 goto skip_duration_calc;
1867 }
1868
1869 69 av_opt_set_int(ic, "skip_changes", 1, AV_OPT_SEARCH_CHILDREN);
1870 /* estimate the end time (duration) */
1871 /* XXX: may need to support wrapping */
1872
1/2
✓ Branch 0 taken 69 times.
✗ Branch 1 not taken.
69 filesize = ic->pb ? avio_size(ic->pb) : 0;
1873 do {
1874 76 is_end = found_duration;
1875 76 offset = filesize - (duration_max_read_size << retry);
1876
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 50 times.
76 if (offset < 0)
1877 26 offset = 0;
1878
1879 76 avio_seek(ic->pb, offset, SEEK_SET);
1880 76 read_size = 0;
1881 6554 for (;;) {
1882 AVStream *st;
1883 FFStream *sti;
1884
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 6623 times.
6630 if (read_size >= duration_max_read_size << (FFMAX(retry - 1, 0)))
1885 7 break;
1886
1887 do {
1888 6623 ret = ff_read_packet(ic, pkt);
1889
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6623 times.
6623 } while (ret == AVERROR(EAGAIN));
1890
2/2
✓ Branch 0 taken 69 times.
✓ Branch 1 taken 6554 times.
6623 if (ret != 0)
1891 69 break;
1892 6554 read_size += pkt->size;
1893 6554 st = ic->streams[pkt->stream_index];
1894 6554 sti = ffstream(st);
1895
2/2
✓ Branch 0 taken 2805 times.
✓ Branch 1 taken 3749 times.
6554 if (pkt->pts != AV_NOPTS_VALUE &&
1896
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2805 times.
2805 (st->start_time != AV_NOPTS_VALUE ||
1897 sti->first_dts != AV_NOPTS_VALUE)) {
1898
1/2
✓ Branch 0 taken 2805 times.
✗ Branch 1 not taken.
2805 if (pkt->duration == 0) {
1899 2805 compute_frame_duration(ic, &num, &den, st, sti->parser, pkt);
1900
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) {
1901 2345 pkt->duration = av_rescale_rnd(1,
1902 2345 num * (int64_t) st->time_base.den,
1903 2345 den * (int64_t) st->time_base.num,
1904 AV_ROUND_DOWN);
1905 }
1906 }
1907 2805 duration = pkt->pts + pkt->duration;
1908 2805 found_duration = 1;
1909
1/2
✓ Branch 0 taken 2805 times.
✗ Branch 1 not taken.
2805 if (st->start_time != AV_NOPTS_VALUE)
1910 2805 duration -= st->start_time;
1911 else
1912 duration -= sti->first_dts;
1913
2/2
✓ Branch 0 taken 2796 times.
✓ Branch 1 taken 9 times.
2805 if (duration > 0) {
1914
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 ||
1915
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))
1916 2542 st->duration = duration;
1917 2796 sti->info->last_duration = duration;
1918 }
1919 }
1920 6554 av_packet_unref(pkt);
1921 }
1922
1923 /* check if all audio/video streams have valid duration */
1924
2/2
✓ Branch 0 taken 69 times.
✓ Branch 1 taken 7 times.
76 if (!is_end) {
1925 69 is_end = 1;
1926
2/2
✓ Branch 0 taken 143 times.
✓ Branch 1 taken 69 times.
212 for (unsigned i = 0; i < ic->nb_streams; i++) {
1927 143 const AVStream *const st = ic->streams[i];
1928
2/2
✓ Branch 0 taken 125 times.
✓ Branch 1 taken 18 times.
143 switch (st->codecpar->codec_type) {
1929 125 case AVMEDIA_TYPE_VIDEO:
1930 case AVMEDIA_TYPE_AUDIO:
1931
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 101 times.
125 if (st->duration == AV_NOPTS_VALUE)
1932 24 is_end = 0;
1933 }
1934 }
1935 }
1936
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 2 times.
9 } while (!is_end &&
1937
3/4
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 67 times.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
83 offset &&
1938 ++retry <= duration_max_retry);
1939
1940 69 av_opt_set_int(ic, "skip_changes", 0, AV_OPT_SEARCH_CHILDREN);
1941
1942 /* warn about audio/video streams which duration could not be estimated */
1943
2/2
✓ Branch 0 taken 143 times.
✓ Branch 1 taken 69 times.
212 for (unsigned i = 0; i < ic->nb_streams; i++) {
1944 143 const AVStream *const st = ic->streams[i];
1945 143 const FFStream *const sti = cffstream(st);
1946
1947
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 111 times.
143 if (st->duration == AV_NOPTS_VALUE) {
1948
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 15 times.
32 switch (st->codecpar->codec_type) {
1949 17 case AVMEDIA_TYPE_VIDEO:
1950 case AVMEDIA_TYPE_AUDIO:
1951
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) {
1952 7 av_log(ic, AV_LOG_WARNING, "stream %d : no PTS found at end of file, duration not set\n", i);
1953 } else
1954 10 av_log(ic, AV_LOG_WARNING, "stream %d : no TS found at start of file, duration not set\n", i);
1955 }
1956 }
1957 }
1958 69 skip_duration_calc:
1959 69 fill_all_stream_timings(ic);
1960
1961 69 avio_seek(ic->pb, old_offset, SEEK_SET);
1962
2/2
✓ Branch 0 taken 143 times.
✓ Branch 1 taken 69 times.
212 for (unsigned i = 0; i < ic->nb_streams; i++) {
1963 143 AVStream *const st = ic->streams[i];
1964 143 FFStream *const sti = ffstream(st);
1965
1966 143 sti->cur_dts = sti->first_dts;
1967 143 sti->last_IP_pts = AV_NOPTS_VALUE;
1968 143 sti->last_dts_for_order_check = AV_NOPTS_VALUE;
1969
2/2
✓ Branch 0 taken 2431 times.
✓ Branch 1 taken 143 times.
2574 for (int j = 0; j < MAX_REORDER_DELAY + 1; j++)
1970 2431 sti->pts_buffer[j] = AV_NOPTS_VALUE;
1971 }
1972 69 }
1973
1974 /* 1:1 map to AVDurationEstimationMethod */
1975 static const char *const duration_name[] = {
1976 [AVFMT_DURATION_FROM_PTS] = "pts",
1977 [AVFMT_DURATION_FROM_STREAM] = "stream",
1978 [AVFMT_DURATION_FROM_BITRATE] = "bit rate",
1979 };
1980
1981 7524 static const char *duration_estimate_name(enum AVDurationEstimationMethod method)
1982 {
1983 7524 return duration_name[method];
1984 }
1985
1986 7524 static void estimate_timings(AVFormatContext *ic, int64_t old_offset)
1987 {
1988 int64_t file_size;
1989
1990 /* get the file size, if possible */
1991
2/2
✓ Branch 0 taken 3061 times.
✓ Branch 1 taken 4463 times.
7524 if (ic->iformat->flags & AVFMT_NOFILE) {
1992 3061 file_size = 0;
1993 } else {
1994 4463 file_size = avio_size(ic->pb);
1995 4463 file_size = FFMAX(0, file_size);
1996 }
1997
1998
2/2
✓ Branch 0 taken 7499 times.
✓ Branch 1 taken 25 times.
7524 if ((!strcmp(ic->iformat->name, "mpeg") ||
1999
3/4
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 7455 times.
✓ Branch 2 taken 69 times.
✗ Branch 3 not taken.
7524 !strcmp(ic->iformat->name, "mpegts")) &&
2000
1/2
✓ Branch 0 taken 69 times.
✗ Branch 1 not taken.
69 file_size && (ic->pb->seekable & AVIO_SEEKABLE_NORMAL)) {
2001 /* get accurate estimate from the PTSes */
2002 69 estimate_timings_from_pts(ic, old_offset);
2003 69 ic->duration_estimation_method = AVFMT_DURATION_FROM_PTS;
2004
2/2
✓ Branch 1 taken 5481 times.
✓ Branch 2 taken 1974 times.
7455 } else if (has_duration(ic)) {
2005 /* at least one component has timings - we use them for all
2006 * the components */
2007 5481 fill_all_stream_timings(ic);
2008 /* nut demuxer estimate the duration from PTS */
2009
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 5451 times.
5481 if (!strcmp(ic->iformat->name, "nut"))
2010 30 ic->duration_estimation_method = AVFMT_DURATION_FROM_PTS;
2011 else
2012 5451 ic->duration_estimation_method = AVFMT_DURATION_FROM_STREAM;
2013 } else {
2014 /* less precise: use bitrate info */
2015 1974 estimate_timings_from_bit_rate(ic);
2016 1974 ic->duration_estimation_method = AVFMT_DURATION_FROM_BITRATE;
2017 }
2018 7524 update_stream_timings(ic);
2019
2020
2/2
✓ Branch 0 taken 8288 times.
✓ Branch 1 taken 7524 times.
15812 for (unsigned i = 0; i < ic->nb_streams; i++) {
2021 8288 AVStream *const st = ic->streams[i];
2022
1/2
✓ Branch 0 taken 8288 times.
✗ Branch 1 not taken.
8288 if (st->time_base.den)
2023 8288 av_log(ic, AV_LOG_TRACE, "stream %u: start_time: %s duration: %s\n", i,
2024 8288 av_ts2timestr(st->start_time, &st->time_base),
2025 8288 av_ts2timestr(st->duration, &st->time_base));
2026 }
2027 7524 av_log(ic, AV_LOG_TRACE,
2028 "format: start_time: %s duration: %s (estimate from %s) bitrate=%"PRId64" kb/s\n",
2029 7524 av_ts2timestr(ic->start_time, &AV_TIME_BASE_Q),
2030 7524 av_ts2timestr(ic->duration, &AV_TIME_BASE_Q),
2031 duration_estimate_name(ic->duration_estimation_method),
2032 7524 (int64_t)ic->bit_rate / 1000);
2033 7524 }
2034
2035 107080 static int determinable_frame_size(const AVCodecContext *avctx)
2036 {
2037
2/2
✓ Branch 0 taken 2187 times.
✓ Branch 1 taken 104893 times.
107080 switch(avctx->codec_id) {
2038 2187 case AV_CODEC_ID_MP1:
2039 case AV_CODEC_ID_MP2:
2040 case AV_CODEC_ID_MP3:
2041 case AV_CODEC_ID_CODEC2:
2042 2187 return 1;
2043 }
2044
2045 104893 return 0;
2046 }
2047
2048 420247 static int has_codec_parameters(const AVStream *st, const char **errmsg_ptr)
2049 {
2050 420247 const FFStream *const sti = cffstream(st);
2051 420247 const AVCodecContext *const avctx = sti->avctx;
2052
2053 #define FAIL(errmsg) do { \
2054 if (errmsg_ptr) \
2055 *errmsg_ptr = errmsg; \
2056 return 0; \
2057 } while (0)
2058
2059
2/2
✓ Branch 0 taken 644 times.
✓ Branch 1 taken 419603 times.
420247 if ( avctx->codec_id == AV_CODEC_ID_NONE
2060
2/2
✓ Branch 0 taken 419 times.
✓ Branch 1 taken 225 times.
644 && avctx->codec_type != AVMEDIA_TYPE_DATA)
2061
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 414 times.
419 FAIL("unknown codec");
2062
4/5
✓ Branch 0 taken 122035 times.
✓ Branch 1 taken 296251 times.
✓ Branch 2 taken 629 times.
✓ Branch 3 taken 913 times.
✗ Branch 4 not taken.
419828 switch (avctx->codec_type) {
2063 122035 case AVMEDIA_TYPE_AUDIO:
2064
4/4
✓ Branch 0 taken 107080 times.
✓ Branch 1 taken 14955 times.
✓ Branch 3 taken 2187 times.
✓ Branch 4 taken 104893 times.
122035 if (!avctx->frame_size && determinable_frame_size(avctx))
2065
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 2175 times.
2187 FAIL("unspecified frame size");
2066
1/2
✓ Branch 0 taken 119848 times.
✗ Branch 1 not taken.
119848 if (sti->info->found_decoder >= 0 &&
2067
2/2
✓ Branch 0 taken 2411 times.
✓ Branch 1 taken 117437 times.
119848 avctx->sample_fmt == AV_SAMPLE_FMT_NONE)
2068
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2411 times.
2411 FAIL("unspecified sample format");
2069
2/2
✓ Branch 0 taken 134 times.
✓ Branch 1 taken 117303 times.
117437 if (!avctx->sample_rate)
2070
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 128 times.
134 FAIL("unspecified sample rate");
2071
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 117286 times.
117303 if (!avctx->ch_layout.nb_channels)
2072
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
17 FAIL("unspecified number of channels");
2073
4/6
✓ Branch 0 taken 117286 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 80586 times.
✓ Branch 3 taken 36700 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 80586 times.
117286 if (sti->info->found_decoder >= 0 && !sti->nb_decoded_frames && avctx->codec_id == AV_CODEC_ID_DTS)
2074 FAIL("no decodable DTS frames");
2075 117286 break;
2076 296251 case AVMEDIA_TYPE_VIDEO:
2077
2/2
✓ Branch 0 taken 12716 times.
✓ Branch 1 taken 283535 times.
296251 if (!avctx->width)
2078
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 12706 times.
12716 FAIL("unspecified size");
2079
4/4
✓ Branch 0 taken 283480 times.
✓ Branch 1 taken 55 times.
✓ Branch 2 taken 4208 times.
✓ Branch 3 taken 279272 times.
283535 if (sti->info->found_decoder >= 0 && avctx->pix_fmt == AV_PIX_FMT_NONE)
2080
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 4205 times.
4208 FAIL("unspecified pixel format");
2081
4/4
✓ Branch 0 taken 279227 times.
✓ Branch 1 taken 100 times.
✓ Branch 2 taken 86 times.
✓ Branch 3 taken 279141 times.
279327 if (st->codecpar->codec_id == AV_CODEC_ID_RV30 || st->codecpar->codec_id == AV_CODEC_ID_RV40)
2082
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)
2083
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 67 times.
67 FAIL("no frame in rv30/40 and no sar");
2084 279260 break;
2085 629 case AVMEDIA_TYPE_SUBTITLE:
2086
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)
2087
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 FAIL("unspecified size");
2088 608 break;
2089 913 case AVMEDIA_TYPE_DATA:
2090
2/2
✓ Branch 0 taken 225 times.
✓ Branch 1 taken 688 times.
913 if (avctx->codec_id == AV_CODEC_ID_NONE) return 1;
2091 }
2092
2093 397842 return 1;
2094 }
2095
2096 /* returns 1 or 0 if or if not decoded data was returned, or a negative error */
2097 196397 static int try_decode_frame(AVFormatContext *s, AVStream *st,
2098 const AVPacket *pkt, AVDictionary **options)
2099 {
2100 196397 FFStream *const sti = ffstream(st);
2101 196397 AVCodecContext *const avctx = sti->avctx;
2102 const AVCodec *codec;
2103 196397 int got_picture = 1, ret = 0;
2104 196397 AVFrame *frame = av_frame_alloc();
2105 AVSubtitle subtitle;
2106 196397 int do_skip_frame = 0;
2107 enum AVDiscard skip_frame;
2108 196397 int pkt_to_send = pkt->size > 0;
2109
2110
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 196397 times.
196397 if (!frame)
2111 return AVERROR(ENOMEM);
2112
2113
2/2
✓ Branch 1 taken 3361 times.
✓ Branch 2 taken 193036 times.
196397 if (!avcodec_is_open(avctx) &&
2114
1/2
✓ Branch 0 taken 3361 times.
✗ Branch 1 not taken.
3361 sti->info->found_decoder <= 0 &&
2115
4/4
✓ Branch 0 taken 2024 times.
✓ Branch 1 taken 1337 times.
✓ Branch 2 taken 63 times.
✓ Branch 3 taken 1961 times.
3361 (st->codecpar->codec_id != -sti->info->found_decoder || !st->codecpar->codec_id)) {
2116 1400 AVDictionary *thread_opt = NULL;
2117
2118 1400 codec = find_probe_decoder(s, st, st->codecpar->codec_id);
2119
2120
2/2
✓ Branch 0 taken 76 times.
✓ Branch 1 taken 1324 times.
1400 if (!codec) {
2121 76 sti->info->found_decoder = -st->codecpar->codec_id;
2122 76 ret = -1;
2123 88 goto fail;
2124 }
2125
2126 /* Force thread count to 1 since the H.264 decoder will not extract
2127 * SPS and PPS to extradata during multi-threaded decoding. */
2128
2/2
✓ Branch 0 taken 131 times.
✓ Branch 1 taken 1193 times.
1324 av_dict_set(options ? options : &thread_opt, "threads", "1", 0);
2129 /* Force lowres to 0. The decoder might reduce the video size by the
2130 * lowres factor, and we don't want that propagated to the stream's
2131 * codecpar */
2132
2/2
✓ Branch 0 taken 131 times.
✓ Branch 1 taken 1193 times.
1324 av_dict_set(options ? options : &thread_opt, "lowres", "0", 0);
2133
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1324 times.
1324 if (s->codec_whitelist)
2134 av_dict_set(options ? options : &thread_opt, "codec_whitelist", s->codec_whitelist, 0);
2135
2/2
✓ Branch 0 taken 131 times.
✓ Branch 1 taken 1193 times.
1324 ret = avcodec_open2(avctx, codec, options ? options : &thread_opt);
2136
2/2
✓ Branch 0 taken 131 times.
✓ Branch 1 taken 1193 times.
1324 if (!options)
2137 131 av_dict_free(&thread_opt);
2138
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 1312 times.
1324 if (ret < 0) {
2139 12 sti->info->found_decoder = -avctx->codec_id;
2140 12 goto fail;
2141 }
2142 1312 sti->info->found_decoder = 1;
2143
2/2
✓ Branch 0 taken 6706 times.
✓ Branch 1 taken 188291 times.
194997 } else if (!sti->info->found_decoder)
2144 6706 sti->info->found_decoder = 1;
2145
2146
2/2
✓ Branch 0 taken 1961 times.
✓ Branch 1 taken 194348 times.
196309 if (sti->info->found_decoder < 0) {
2147 1961 ret = -1;
2148 1961 goto fail;
2149 }
2150
2151
2/2
✓ Branch 1 taken 107362 times.
✓ Branch 2 taken 86986 times.
194348 if (avpriv_codec_get_cap_skip_frame_fill_param(avctx->codec)) {
2152 107362 do_skip_frame = 1;
2153 107362 skip_frame = avctx->skip_frame;
2154 107362 avctx->skip_frame = AVDISCARD_ALL;
2155 }
2156
2157
5/6
✓ Branch 0 taken 7933 times.
✓ Branch 1 taken 4514 times.
✓ Branch 2 taken 26 times.
✓ Branch 3 taken 4488 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 194356 times.
396663 while ((pkt_to_send || (!pkt->data && got_picture)) &&
2158
4/4
✓ Branch 0 taken 12447 times.
✓ Branch 1 taken 189868 times.
✓ Branch 2 taken 5519 times.
✓ Branch 3 taken 188837 times.
396671 ret >= 0 &&
2159
2/2
✓ Branch 2 taken 2241 times.
✓ Branch 3 taken 186596 times.
383193 (!has_codec_parameters(st, NULL) || !has_decode_delay_been_guessed(st) ||
2160
2/2
✓ Branch 0 taken 183810 times.
✓ Branch 1 taken 2786 times.
186596 (!sti->codec_info_nb_frames &&
2161
2/2
✓ Branch 0 taken 447 times.
✓ Branch 1 taken 2339 times.
2786 (avctx->codec->capabilities & AV_CODEC_CAP_CHANNEL_CONF)))) {
2162 8207 got_picture = 0;
2163
2/2
✓ Branch 0 taken 716 times.
✓ Branch 1 taken 7491 times.
8207 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO ||
2164
2/2
✓ Branch 0 taken 709 times.
✓ Branch 1 taken 7 times.
716 avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
2165 8200 ret = avcodec_send_packet(avctx, pkt);
2166
5/6
✓ Branch 0 taken 249 times.
✓ Branch 1 taken 7951 times.
✓ Branch 2 taken 249 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 240 times.
✓ Branch 5 taken 9 times.
8200 if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
2167 240 break;
2168
2/2
✓ Branch 0 taken 7951 times.
✓ Branch 1 taken 9 times.
7960 if (ret >= 0)
2169 7951 pkt_to_send = 0;
2170 7960 ret = avcodec_receive_frame(avctx, frame);
2171
2/2
✓ Branch 0 taken 3179 times.
✓ Branch 1 taken 4781 times.
7960 if (ret >= 0)
2172 3179 got_picture = 1;
2173
4/4
✓ Branch 0 taken 3205 times.
✓ Branch 1 taken 4755 times.
✓ Branch 2 taken 26 times.
✓ Branch 3 taken 3179 times.
7960 if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
2174 4781 ret = 0;
2175
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 } else if (avctx->codec_type == AVMEDIA_TYPE_SUBTITLE) {
2176 7 ret = avcodec_decode_subtitle2(avctx, &subtitle,
2177 &got_picture, pkt);
2178
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 5 times.
7 if (got_picture)
2179 2 avsubtitle_free(&subtitle);
2180
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 if (ret >= 0)
2181 7 pkt_to_send = 0;
2182 }
2183
1/2
✓ Branch 0 taken 7967 times.
✗ Branch 1 not taken.
7967 if (ret >= 0) {
2184
2/2
✓ Branch 0 taken 3181 times.
✓ Branch 1 taken 4786 times.
7967 if (got_picture)
2185 3181 sti->nb_decoded_frames++;
2186 7967 ret = got_picture;
2187 }
2188 }
2189
2190 194108 fail:
2191
2/2
✓ Branch 0 taken 107362 times.
✓ Branch 1 taken 89035 times.
196397 if (do_skip_frame) {
2192 107362 avctx->skip_frame = skip_frame;
2193 }
2194
2195 196397 av_frame_free(&frame);
2196 196397 return ret;
2197 }
2198
2199 57 static int chapter_start_cmp(const void *p1, const void *p2)
2200 {
2201 57 const AVChapter *const ch1 = *(AVChapter**)p1;
2202 57 const AVChapter *const ch2 = *(AVChapter**)p2;
2203 57 int delta = av_compare_ts(ch1->start, ch1->time_base, ch2->start, ch2->time_base);
2204
1/2
✓ Branch 0 taken 57 times.
✗ Branch 1 not taken.
57 if (delta)
2205 57 return delta;
2206 return FFDIFFSIGN(ch1->id, ch2->id);
2207 }
2208
2209 7524 static int compute_chapters_end(AVFormatContext *s)
2210 {
2211 7524 int64_t max_time = 0;
2212 AVChapter **timetable;
2213
2214
2/2
✓ Branch 0 taken 7498 times.
✓ Branch 1 taken 26 times.
7524 if (!s->nb_chapters)
2215 7498 return 0;
2216
2217
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)
2218 26 max_time = s->duration +
2219
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 3 times.
26 ((s->start_time == AV_NOPTS_VALUE) ? 0 : s->start_time);
2220
2221 26 timetable = av_memdup(s->chapters, s->nb_chapters * sizeof(*timetable));
2222
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
26 if (!timetable)
2223 return AVERROR(ENOMEM);
2224 26 qsort(timetable, s->nb_chapters, sizeof(*timetable), chapter_start_cmp);
2225
2226
2/2
✓ Branch 0 taken 71 times.
✓ Branch 1 taken 26 times.
97 for (unsigned i = 0; i < s->nb_chapters; i++)
2227
2/2
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 31 times.
71 if (timetable[i]->end == AV_NOPTS_VALUE) {
2228 40 AVChapter *const ch = timetable[i];
2229 40 int64_t end = max_time ? av_rescale_q(max_time, AV_TIME_BASE_Q,
2230 ch->time_base)
2231
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
40 : INT64_MAX;
2232
2233
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 19 times.
40 if (i + 1 < s->nb_chapters) {
2234 21 const AVChapter *const ch1 = timetable[i + 1];
2235 21 int64_t next_start = av_rescale_q(ch1->start, ch1->time_base,
2236 ch->time_base);
2237
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)
2238 21 end = next_start;
2239 }
2240
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;
2241 }
2242 26 av_free(timetable);
2243 26 return 0;
2244 }
2245
2246 18599175 static int get_std_framerate(int i)
2247 {
2248
2/2
✓ Branch 0 taken 16898909 times.
✓ Branch 1 taken 1700266 times.
18599175 if (i < 30*12)
2249 16898909 return (i + 1) * 1001;
2250 1700266 i -= 30*12;
2251
2252
2/2
✓ Branch 0 taken 1294065 times.
✓ Branch 1 taken 406201 times.
1700266 if (i < 30)
2253 1294065 return (i + 31) * 1001 * 12;
2254 406201 i -= 30;
2255
2256
2/2
✓ Branch 0 taken 129135 times.
✓ Branch 1 taken 277066 times.
406201 if (i < 3)
2257 129135 return ((const int[]) { 80, 120, 240})[i] * 1001 * 12;
2258
2259 277066 i -= 3;
2260
2261 277066 return ((const int[]) { 24, 30, 60, 12, 15, 48 })[i] * 1000 * 12;
2262 }
2263
2264 /* Is the time base unreliable?
2265 * This is a heuristic to balance between quick acceptance of the values in
2266 * the headers vs. some extra checks.
2267 * Old DivX and Xvid often have nonsense timebases like 1fps or 2fps.
2268 * MPEG-2 commonly misuses field repeat flags to store different framerates.
2269 * And there are "variable" fps files this needs to detect as well. */
2270 205681 static int tb_unreliable(AVFormatContext *ic, AVStream *st)
2271 {
2272 205681 FFStream *const sti = ffstream(st);
2273 205681 const AVCodecDescriptor *desc = sti->codec_desc;
2274 205681 AVCodecContext *c = sti->avctx;
2275
4/4
✓ Branch 0 taken 205230 times.
✓ Branch 1 taken 451 times.
✓ Branch 2 taken 19565 times.
✓ Branch 3 taken 185665 times.
205681 AVRational mul = (AVRational){ desc && (desc->props & AV_CODEC_PROP_FIELDS) ? 2 : 1, 1 };
2276 205681 AVRational time_base = c->framerate.num ? av_inv_q(av_mul_q(c->framerate, mul))
2277 /* NOHEADER check added to not break existing behavior */
2278
2/2
✓ Branch 0 taken 16205 times.
✓ Branch 1 taken 189476 times.
205681 : (((ic->ctx_flags & AVFMTCTX_NOHEADER) ||
2279
2/2
✓ Branch 0 taken 44381 times.
✓ Branch 1 taken 30239 times.
74620 st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) ? (AVRational){0, 1}
2280
2/2
✓ Branch 0 taken 74620 times.
✓ Branch 1 taken 114856 times.
189476 : st->time_base);
2281
2282
2/2
✓ Branch 0 taken 22413 times.
✓ Branch 1 taken 183268 times.
205681 if (time_base.den >= 101LL * time_base.num ||
2283
2/2
✓ Branch 0 taken 22005 times.
✓ Branch 1 taken 408 times.
22413 time_base.den < 5LL * time_base.num ||
2284 // c->codec_tag == AV_RL32("DIVX") ||
2285 // c->codec_tag == AV_RL32("XVID") ||
2286
2/2
✓ Branch 0 taken 21906 times.
✓ Branch 1 taken 99 times.
22005 c->codec_tag == AV_RL32("mp4v") ||
2287
2/2
✓ Branch 0 taken 13077 times.
✓ Branch 1 taken 8829 times.
21906 c->codec_id == AV_CODEC_ID_MPEG2VIDEO ||
2288
2/2
✓ Branch 0 taken 12547 times.
✓ Branch 1 taken 530 times.
13077 c->codec_id == AV_CODEC_ID_GIF ||
2289
2/2
✓ Branch 0 taken 11848 times.
✓ Branch 1 taken 699 times.
12547 c->codec_id == AV_CODEC_ID_HEVC ||
2290
2/2
✓ Branch 0 taken 2019 times.
✓ Branch 1 taken 9829 times.
11848 c->codec_id == AV_CODEC_ID_H264)
2291 195852 return 1;
2292 9829 return 0;
2293 }
2294
2295 140693 int ff_rfps_add_frame(AVFormatContext *ic, AVStream *st, int64_t ts)
2296 {
2297 140693 FFStream *const sti = ffstream(st);
2298 140693 FFStreamInfo *info = sti->info;
2299 140693 int64_t last = info->last_dts;
2300
2301
6/6
✓ Branch 0 taken 124679 times.
✓ Branch 1 taken 16014 times.
✓ Branch 2 taken 118728 times.
✓ Branch 3 taken 5951 times.
✓ Branch 4 taken 118700 times.
✓ Branch 5 taken 28 times.
140693 if ( ts != AV_NOPTS_VALUE && last != AV_NOPTS_VALUE && ts > last
2302
1/2
✓ Branch 0 taken 118700 times.
✗ Branch 1 not taken.
118700 && ts - (uint64_t)last < INT64_MAX) {
2303
2/2
✓ Branch 1 taken 6480 times.
✓ Branch 2 taken 112220 times.
118700 double dts = (is_relative(ts) ? ts - RELATIVE_TS_BASE : ts) * av_q2d(st->time_base);
2304 118700 int64_t duration = ts - last;
2305
2306
2/2
✓ Branch 0 taken 3872 times.
✓ Branch 1 taken 114828 times.
118700 if (!info->duration_error)
2307 3872 info->duration_error = av_mallocz(sizeof(info->duration_error[0])*2);
2308
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 118700 times.
118700 if (!info->duration_error)
2309 return AVERROR(ENOMEM);
2310
2311 // if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
2312 // av_log(NULL, AV_LOG_ERROR, "%f\n", dts);
2313
2/2
✓ Branch 0 taken 47361300 times.
✓ Branch 1 taken 118700 times.
47480000 for (int i = 0; i < MAX_STD_TIMEBASES; i++) {
2314
2/2
✓ Branch 0 taken 18247468 times.
✓ Branch 1 taken 29113832 times.
47361300 if (info->duration_error[0][1][i] < 1e10) {
2315 18247468 int framerate = get_std_framerate(i);
2316 18247468 double sdts = dts*framerate/(1001*12);
2317
2/2
✓ Branch 0 taken 36494936 times.
✓ Branch 1 taken 18247468 times.
54742404 for (int j = 0; j < 2; j++) {
2318 36494936 int64_t ticks = llrint(sdts+j*0.5);
2319 36494936 double error = sdts - ticks + j*0.5;
2320 36494936 info->duration_error[j][0][i] += error;
2321 36494936 info->duration_error[j][1][i] += error*error;
2322 }
2323 }
2324 }
2325
1/2
✓ Branch 0 taken 118700 times.
✗ Branch 1 not taken.
118700 if (info->rfps_duration_sum <= INT64_MAX - duration) {
2326 118700 info->duration_count++;
2327 118700 info->rfps_duration_sum += duration;
2328 }
2329
2330
2/2
✓ Branch 0 taken 10805 times.
✓ Branch 1 taken 107895 times.
118700 if (info->duration_count % 10 == 0) {
2331 10805 int n = info->duration_count;
2332
2/2
✓ Branch 0 taken 4311195 times.
✓ Branch 1 taken 10805 times.
4322000 for (int i = 0; i < MAX_STD_TIMEBASES; i++) {
2333
2/2
✓ Branch 0 taken 1747404 times.
✓ Branch 1 taken 2563791 times.
4311195 if (info->duration_error[0][1][i] < 1e10) {
2334 1747404 double a0 = info->duration_error[0][0][i] / n;
2335 1747404 double error0 = info->duration_error[0][1][i] / n - a0*a0;
2336 1747404 double a1 = info->duration_error[1][0][i] / n;
2337 1747404 double error1 = info->duration_error[1][1][i] / n - a1*a1;
2338
4/4
✓ Branch 0 taken 1431017 times.
✓ Branch 1 taken 316387 times.
✓ Branch 2 taken 1335675 times.
✓ Branch 3 taken 95342 times.
1747404 if (error0 > 0.04 && error1 > 0.04) {
2339 1335675 info->duration_error[0][1][i] = 2e10;
2340 1335675 info->duration_error[1][1][i] = 2e10;
2341 }
2342 }
2343 }
2344 }
2345
2346 // ignore the first 4 values, they might have some random jitter
2347
3/4
✓ Branch 0 taken 107170 times.
✓ Branch 1 taken 11530 times.
✓ Branch 4 taken 107170 times.
✗ Branch 5 not taken.
118700 if (info->duration_count > 3 && is_relative(ts) == is_relative(last))
2348 107170 info->duration_gcd = av_gcd(info->duration_gcd, duration);
2349 }
2350
2/2
✓ Branch 0 taken 124679 times.
✓ Branch 1 taken 16014 times.
140693 if (ts != AV_NOPTS_VALUE)
2351 124679 info->last_dts = ts;
2352
2353 140693 return 0;
2354 }
2355
2356 8016 void ff_rfps_calculate(AVFormatContext *ic)
2357 {
2358
2/2
✓ Branch 0 taken 8958 times.
✓ Branch 1 taken 8016 times.
16974 for (unsigned i = 0; i < ic->nb_streams; i++) {
2359 8958 AVStream *const st = ic->streams[i];
2360 8958 FFStream *const sti = ffstream(st);
2361
2362
2/2
✓ Branch 0 taken 2425 times.
✓ Branch 1 taken 6533 times.
8958 if (st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO)
2363 2425 continue;
2364 // the check for tb_unreliable() is not completely correct, since this is not about handling
2365 // an unreliable/inexact time base, but a time base that is finer than necessary, as e.g.
2366 // ipmovie.c produces.
2367
8/8
✓ Branch 1 taken 5159 times.
✓ Branch 2 taken 1374 times.
✓ Branch 3 taken 3349 times.
✓ Branch 4 taken 1810 times.
✓ Branch 5 taken 294 times.
✓ Branch 6 taken 3055 times.
✓ Branch 7 taken 144 times.
✓ Branch 8 taken 150 times.
6533 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 &&
2368
1/2
✓ Branch 0 taken 144 times.
✗ Branch 1 not taken.
144 sti->info->duration_gcd < INT64_MAX / st->time_base.num)
2369 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);
2370
4/4
✓ Branch 0 taken 3835 times.
✓ Branch 1 taken 2698 times.
✓ Branch 2 taken 391 times.
✓ Branch 3 taken 3444 times.
6533 if (sti->info->duration_count > 1 && !st->r_frame_rate.num
2371
2/2
✓ Branch 1 taken 288 times.
✓ Branch 2 taken 103 times.
391 && tb_unreliable(ic, st)) {
2372 288 int num = 0;
2373 288 double best_error = 0.01;
2374
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 288 times.
288 AVRational ref_rate = st->r_frame_rate.num ? st->r_frame_rate : av_inv_q(st->time_base);
2375
2376
2/2
✓ Branch 0 taken 114912 times.
✓ Branch 1 taken 288 times.
115200 for (int j = 0; j < MAX_STD_TIMEBASES; j++) {
2377
2/2
✓ Branch 0 taken 85785 times.
✓ Branch 1 taken 29127 times.
114912 if (sti->info->codec_info_duration &&
2378
2/2
✓ Branch 2 taken 9835 times.
✓ Branch 3 taken 75950 times.
85785 sti->info->codec_info_duration*av_q2d(st->time_base) < (1001*11.5)/get_std_framerate(j))
2379 9835 continue;
2380
4/4
✓ Branch 0 taken 29127 times.
✓ Branch 1 taken 75950 times.
✓ Branch 3 taken 803 times.
✓ Branch 4 taken 28324 times.
105077 if (!sti->info->codec_info_duration && get_std_framerate(j) < 1001*12)
2381 803 continue;
2382
2383
2/2
✓ Branch 2 taken 48532 times.
✓ Branch 3 taken 55742 times.
104274 if (av_q2d(st->time_base) * sti->info->rfps_duration_sum / sti->info->duration_count < (1001*12.0 * 0.8)/get_std_framerate(j))
2384 48532 continue;
2385
2386
2/2
✓ Branch 0 taken 111484 times.
✓ Branch 1 taken 55742 times.
167226 for (int k = 0; k < 2; k++) {
2387 111484 int n = sti->info->duration_count;
2388 111484 double a = sti->info->duration_error[k][0][j] / n;
2389 111484 double error = sti->info->duration_error[k][1][j]/n - a*a;
2390
2391
4/4
✓ Branch 0 taken 4427 times.
✓ Branch 1 taken 107057 times.
✓ Branch 2 taken 4356 times.
✓ Branch 3 taken 71 times.
111484 if (error < best_error && best_error> 0.000000001) {
2392 4356 best_error= error;
2393 4356 num = get_std_framerate(j);
2394 }
2395
2/2
✓ Branch 0 taken 23627 times.
✓ Branch 1 taken 87857 times.
111484 if (error < 0.02)
2396 23627 av_log(ic, AV_LOG_DEBUG, "rfps: %f %f\n", get_std_framerate(j) / 12.0/1001, error);
2397 }
2398 }
2399 // do not increase frame rate by more than 1 % in order to match a standard rate.
2400
5/6
✓ Branch 0 taken 282 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 282 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 280 times.
✓ Branch 6 taken 2 times.
288 if (num && (!ref_rate.num || (double)num/(12*1001) < 1.01 * av_q2d(ref_rate)))
2401 280 av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den, num, 12*1001, INT_MAX);
2402 }
2403
2/2
✓ Branch 0 taken 1077 times.
✓ Branch 1 taken 5456 times.
6533 if ( !st->avg_frame_rate.num
2404
4/4
✓ Branch 0 taken 272 times.
✓ Branch 1 taken 805 times.
✓ Branch 2 taken 268 times.
✓ Branch 3 taken 4 times.
1077 && st->r_frame_rate.num && sti->info->rfps_duration_sum
2405
2/2
✓ Branch 0 taken 67 times.
✓ Branch 1 taken 201 times.
268 && sti->info->codec_info_duration <= 0
2406
2/2
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 3 times.
67 && sti->info->duration_count > 2
2407
2/2
✓ Branch 2 taken 49 times.
✓ Branch 3 taken 15 times.
64 && 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
2408 ) {
2409 49 av_log(ic, AV_LOG_DEBUG, "Setting avg frame rate based on r frame rate\n");
2410 49 st->avg_frame_rate = st->r_frame_rate;
2411 }
2412
2413 6533 av_freep(&sti->info->duration_error);
2414 6533 sti->info->last_dts = AV_NOPTS_VALUE;
2415 6533 sti->info->duration_count = 0;
2416 6533 sti->info->rfps_duration_sum = 0;
2417 }
2418 8016 }
2419
2420 8965 static int extract_extradata_check(AVStream *st)
2421 {
2422 8965 const AVBitStreamFilter *const f = av_bsf_get_by_name("extract_extradata");
2423
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8965 times.
8965 if (!f)
2424 return 0;
2425
2426
1/2
✓ Branch 0 taken 8965 times.
✗ Branch 1 not taken.
8965 if (f->codec_ids) {
2427 const enum AVCodecID *ids;
2428
2/2
✓ Branch 0 taken 94981 times.
✓ Branch 1 taken 8078 times.
103059 for (ids = f->codec_ids; *ids != AV_CODEC_ID_NONE; ids++)
2429
2/2
✓ Branch 0 taken 887 times.
✓ Branch 1 taken 94094 times.
94981 if (*ids == st->codecpar->codec_id)
2430 887 return 1;
2431 }
2432
2433 8078 return 0;
2434 }
2435
2436 7058 static int extract_extradata_init(AVStream *st)
2437 {
2438 7058 FFStream *const sti = ffstream(st);
2439 const AVBitStreamFilter *f;
2440 int ret;
2441
2442 7058 f = av_bsf_get_by_name("extract_extradata");
2443
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7058 times.
7058 if (!f)
2444 goto finish;
2445
2446 /* check that the codec id is supported */
2447 7058 ret = extract_extradata_check(st);
2448
2/2
✓ Branch 0 taken 6281 times.
✓ Branch 1 taken 777 times.
7058 if (!ret)
2449 6281 goto finish;
2450
2451 777 av_bsf_free(&sti->extract_extradata.bsf);
2452 777 ret = av_bsf_alloc(f, &sti->extract_extradata.bsf);
2453
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 777 times.
777 if (ret < 0)
2454 return ret;
2455
2456 777 ret = avcodec_parameters_copy(sti->extract_extradata.bsf->par_in,
2457 777 st->codecpar);
2458
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 777 times.
777 if (ret < 0)
2459 goto fail;
2460
2461 777 sti->extract_extradata.bsf->time_base_in = st->time_base;
2462
2463 777 ret = av_bsf_init(sti->extract_extradata.bsf);
2464
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 777 times.
777 if (ret < 0)
2465 goto fail;
2466
2467 777 finish:
2468 7058 sti->extract_extradata.inited = 1;
2469
2470 7058 return 0;
2471 fail:
2472 av_bsf_free(&sti->extract_extradata.bsf);
2473 return ret;
2474 }
2475
2476 162582 static int extract_extradata(FFFormatContext *si, AVStream *st, const AVPacket *pkt)
2477 {
2478 162582 FFStream *const sti = ffstream(st);
2479 162582 AVPacket *const pkt_ref = si->parse_pkt;
2480 int ret;
2481
2482
2/2
✓ Branch 0 taken 7058 times.
✓ Branch 1 taken 155524 times.
162582 if (!sti->extract_extradata.inited) {
2483 7058 ret = extract_extradata_init(st);
2484
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7058 times.
7058 if (ret < 0)
2485 return ret;
2486 }
2487
2488
3/4
✓ Branch 0 taken 162582 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 161420 times.
✓ Branch 3 taken 1162 times.
162582 if (sti->extract_extradata.inited && !sti->extract_extradata.bsf)
2489 161420 return 0;
2490
2491 1162 ret = av_packet_ref(pkt_ref, pkt);
2492
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1162 times.
1162 if (ret < 0)
2493 return ret;
2494
2495 1162 ret = av_bsf_send_packet(sti->extract_extradata.bsf, pkt_ref);
2496
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1162 times.
1162 if (ret < 0) {
2497 av_packet_unref(pkt_ref);
2498 return ret;
2499 }
2500
2501
4/4
✓ Branch 0 taken 2324 times.
✓ Branch 1 taken 384 times.
✓ Branch 2 taken 1546 times.
✓ Branch 3 taken 778 times.
2708 while (ret >= 0 && !sti->avctx->extradata) {
2502 1546 ret = av_bsf_receive_packet(sti->extract_extradata.bsf, pkt_ref);
2503
2/2
✓ Branch 0 taken 384 times.
✓ Branch 1 taken 1162 times.
1546 if (ret < 0) {
2504
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)
2505 return ret;
2506 384 continue;
2507 }
2508
2509
2/2
✓ Branch 0 taken 1024 times.
✓ Branch 1 taken 384 times.
1408 for (int i = 0; i < pkt_ref->side_data_elems; i++) {
2510 1024 AVPacketSideData *const side_data = &pkt_ref->side_data[i];
2511
2/2
✓ Branch 0 taken 778 times.
✓ Branch 1 taken 246 times.
1024 if (side_data->type == AV_PKT_DATA_NEW_EXTRADATA) {
2512 778 sti->avctx->extradata = side_data->data;
2513 778 sti->avctx->extradata_size = side_data->size;
2514 778 side_data->data = NULL;
2515 778 side_data->size = 0;
2516 778 break;
2517 }
2518 }
2519 1162 av_packet_unref(pkt_ref);
2520 }
2521
2522 1162 return 0;
2523 }
2524
2525 7524 int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
2526 {
2527 7524 FFFormatContext *const si = ffformatcontext(ic);
2528 7524 int count = 0, ret = 0, err;
2529 int64_t read_size;
2530 7524 AVPacket *pkt1 = si->pkt;
2531 7524 int64_t old_offset = avio_tell(ic->pb);
2532 // new streams might appear, no options for those
2533 7524 int orig_nb_streams = ic->nb_streams;
2534 int flush_codecs;
2535 7524 int64_t max_analyze_duration = ic->max_analyze_duration;
2536 int64_t max_stream_analyze_duration;
2537 int64_t max_subtitle_analyze_duration;
2538 7524 int64_t probesize = ic->probesize;
2539 7524 int eof_reached = 0;
2540
2541 7524 flush_codecs = probesize > 0;
2542
2543 7524 av_opt_set_int(ic, "skip_clear", 1, AV_OPT_SEARCH_CHILDREN);
2544
2545 7524 max_stream_analyze_duration = max_analyze_duration;
2546 7524 max_subtitle_analyze_duration = max_analyze_duration;
2547
2/2
✓ Branch 0 taken 7523 times.
✓ Branch 1 taken 1 times.
7524 if (!max_analyze_duration) {
2548 7523 max_stream_analyze_duration =
2549 7523 max_analyze_duration = 5*AV_TIME_BASE;
2550 7523 max_subtitle_analyze_duration = 30*AV_TIME_BASE;
2551
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 7490 times.
7523 if (!strcmp(ic->iformat->name, "flv"))
2552 33 max_stream_analyze_duration = 90*AV_TIME_BASE;
2553
4/4
✓ Branch 0 taken 7498 times.
✓ Branch 1 taken 25 times.
✓ Branch 2 taken 44 times.
✓ Branch 3 taken 7454 times.
7523 if (!strcmp(ic->iformat->name, "mpeg") || !strcmp(ic->iformat->name, "mpegts"))
2554 69 max_stream_analyze_duration = 7*AV_TIME_BASE;
2555 }
2556
2557
2/2
✓ Branch 0 taken 4488 times.
✓ Branch 1 taken 3036 times.
7524 if (ic->pb) {
2558 4488 FFIOContext *const ctx = ffiocontext(ic->pb);
2559 4488 av_log(ic, AV_LOG_DEBUG, "Before avformat_find_stream_info() pos: %"PRId64" bytes read:%"PRId64" seeks:%d nb_streams:%d\n",
2560 avio_tell(ic->pb), ctx->bytes_read, ctx->seek_count, ic->nb_streams);
2561 }
2562
2563
2/2
✓ Branch 0 taken 8135 times.
✓ Branch 1 taken 7524 times.
15659 for (unsigned i = 0; i < ic->nb_streams; i++) {
2564 const AVCodec *codec;
2565 8135 AVDictionary *thread_opt = NULL;
2566 8135 AVStream *const st = ic->streams[i];
2567 8135 FFStream *const sti = ffstream(st);
2568 8135 AVCodecContext *const avctx = sti->avctx;
2569
2570 /* check if the caller has overridden the codec id */
2571 // only for the split stuff
2572
4/6
✓ Branch 0 taken 8135 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8135 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 7632 times.
✓ Branch 5 taken 503 times.
8135 if (!sti->parser && !(ic->flags & AVFMT_FLAG_NOPARSE) && sti->request_probe <= 0) {
2573 7632 sti->parser = av_parser_init(st->codecpar->codec_id);
2574
2/2
✓ Branch 0 taken 5265 times.
✓ Branch 1 taken 2367 times.
7632 if (sti->parser) {
2575
2/2
✓ Branch 0 taken 705 times.
✓ Branch 1 taken 4560 times.
5265 if (sti->need_parsing == AVSTREAM_PARSE_HEADERS) {
2576 705 sti->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES;
2577
2/2
✓ Branch 0 taken 814 times.
✓ Branch 1 taken 3746 times.
4560 } else if (sti->need_parsing == AVSTREAM_PARSE_FULL_RAW) {
2578 814 sti->parser->flags |= PARSER_FLAG_USE_CODEC_TS;
2579 }
2580
2/2
✓ Branch 0 taken 692 times.
✓ Branch 1 taken 1675 times.
2367 } else if (sti->need_parsing) {
2581 692 av_log(ic, AV_LOG_VERBOSE, "parser not found for codec "
2582 "%s, packets or times may be invalid.\n",
2583 692 avcodec_get_name(st->codecpar->codec_id));
2584 }
2585 }
2586
2587 8135 ret = avcodec_parameters_to_context(avctx, st->codecpar);
2588
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8135 times.
8135 if (ret < 0)
2589 goto find_stream_info_err;
2590
2/2
✓ Branch 0 taken 7632 times.
✓ Branch 1 taken 503 times.
8135 if (sti->request_probe <= 0)
2591 7632 sti->avctx_inited = 1;
2592
2593 8135 codec = find_probe_decoder(ic, st, st->codecpar->codec_id);
2594
2595 /* Force thread count to 1 since the H.264 decoder will not extract
2596 * SPS and PPS to extradata during multi-threaded decoding. */
2597
2/2
✓ Branch 0 taken 7851 times.
✓ Branch 1 taken 284 times.
8135 av_dict_set(options ? &options[i] : &thread_opt, "threads", "1", 0);
2598 /* Force lowres to 0. The decoder might reduce the video size by the
2599 * lowres factor, and we don't want that propagated to the stream's
2600 * codecpar */
2601
2/2
✓ Branch 0 taken 7851 times.
✓ Branch 1 taken 284 times.
8135 av_dict_set(options ? &options[i] : &thread_opt, "lowres", "0", 0);
2602
2603
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8135 times.
8135 if (ic->codec_whitelist)
2604 av_dict_set(options ? &options[i] : &thread_opt, "codec_whitelist", ic->codec_whitelist, 0);
2605
2606 // Try to just open decoders, in case this is enough to get parameters.
2607 // Also ensure that subtitle_header is properly set.
2608
4/4
✓ Branch 1 taken 7322 times.
✓ Branch 2 taken 813 times.
✓ Branch 3 taken 502 times.
✓ Branch 4 taken 6820 times.
8135 if (!has_codec_parameters(st, NULL) && sti->request_probe <= 0 ||
2609
2/2
✓ Branch 0 taken 78 times.
✓ Branch 1 taken 1237 times.
1315 st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) {
2610
3/4
✓ Branch 0 taken 6879 times.
✓ Branch 1 taken 19 times.
✓ Branch 2 taken 6879 times.
✗ Branch 3 not taken.
6898 if (codec && !avctx->codec)
2611
4/4
✓ Branch 0 taken 6599 times.
✓ Branch 1 taken 280 times.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 6873 times.
6879 if (avcodec_open2(avctx, codec, options ? &options[i] : &thread_opt) < 0)
2612 6 av_log(ic, AV_LOG_WARNING,
2613 "Failed to open codec in %s\n", __func__);
2614 }
2615
2/2
✓ Branch 0 taken 284 times.
✓ Branch 1 taken 7851 times.
8135 if (!options)
2616 284 av_dict_free(&thread_opt);
2617 }
2618
2619 7524 read_size = 0;
2620 191918 for (;;) {
2621 const AVPacket *pkt;
2622 AVStream *st;
2623 FFStream *sti;
2624 AVCodecContext *avctx;
2625 int analyzed_all_streams;
2626 unsigned i;
2627
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 199442 times.
199442 if (ff_check_interrupt(&ic->interrupt_callback)) {
2628 ret = AVERROR_EXIT;
2629 av_log(ic, AV_LOG_DEBUG, "interrupted\n");
2630 break;
2631 }
2632
2633 /* check if one codec still needs to be handled */
2634
2/2
✓ Branch 0 taken 208031 times.
✓ Branch 1 taken 110367 times.
318398 for (i = 0; i < ic->nb_streams; i++) {
2635 208031 AVStream *const st = ic->streams[i];
2636 208031 FFStream *const sti = ffstream(st);
2637 208031 int fps_analyze_framecount = 20;
2638 int count;
2639
2640
2/2
✓ Branch 1 taken 9274 times.
✓ Branch 2 taken 198757 times.
208031 if (!has_codec_parameters(st, NULL))
2641 9274 break;
2642 /* If the timebase is coarse (like the usual millisecond precision
2643 * of mkv), we need to analyze more frames to reliably arrive at
2644 * the correct fps. */
2645
2/2
✓ Branch 1 taken 118872 times.
✓ Branch 2 taken 79885 times.
198757 if (av_q2d(st->time_base) > 0.0005)
2646 118872 fps_analyze_framecount *= 2;
2647
2/2
✓ Branch 1 taken 8352 times.
✓ Branch 2 taken 190405 times.
198757 if (!tb_unreliable(ic, st))
2648 8352 fps_analyze_framecount = 0;
2649
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 198757 times.
198757 if (ic->fps_probe_size >= 0)
2650 fps_analyze_framecount = ic->fps_probe_size;
2651
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 198713 times.
198757 if (st->disposition & AV_DISPOSITION_ATTACHED_PIC)
2652 44 fps_analyze_framecount = 0;
2653 /* variable fps and no guess at the real fps */
2654
2/2
✓ Branch 0 taken 19196 times.
✓ Branch 1 taken 179561 times.
198757 count = (ic->iformat->flags & AVFMT_NOTIMESTAMPS) ?
2655 19196 sti->info->codec_info_duration_fields/2 :
2656 179561 sti->info->duration_count;
2657
4/4
✓ Branch 0 taken 101132 times.
✓ Branch 1 taken 97625 times.
✓ Branch 2 taken 223 times.
✓ Branch 3 taken 100909 times.
198757 if (!(st->r_frame_rate.num && st->avg_frame_rate.num) &&
2658
2/2
✓ Branch 0 taken 41348 times.
✓ Branch 1 taken 56500 times.
97848 st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
2659
2/2
✓ Branch 0 taken 23406 times.
✓ Branch 1 taken 17942 times.
41348 if (count < fps_analyze_framecount)
2660 23406 break;
2661 }
2662 // Look at the first 3 frames if there is evidence of frame delay
2663 // but the decoder delay is not set.
2664
6/6
✓ Branch 0 taken 2898 times.
✓ Branch 1 taken 172453 times.
✓ Branch 2 taken 130 times.
✓ Branch 3 taken 2768 times.
✓ Branch 4 taken 12 times.
✓ Branch 5 taken 118 times.
175351 if (sti->info->frame_delay_evidence && count < 2 && sti->avctx->has_b_frames == 0)
2665 12 break;
2666
2/2
✓ Branch 0 taken 155205 times.
✓ Branch 1 taken 20134 times.
175339 if (!sti->avctx->extradata &&
2667
6/6
✓ Branch 0 taken 153408 times.
✓ Branch 1 taken 1797 times.
✓ Branch 2 taken 110 times.
✓ Branch 3 taken 153298 times.
✓ Branch 4 taken 110 times.
✓ Branch 5 taken 1797 times.
157112 (!sti->extract_extradata.inited || sti->extract_extradata.bsf) &&
2668 1907 extract_extradata_check(st))
2669 110 break;
2670
2/2
✓ Branch 0 taken 60105 times.
✓ Branch 1 taken 115124 times.
175229 if (sti->first_dts == AV_NOPTS_VALUE &&
2671
6/6
✓ Branch 0 taken 9343 times.
✓ Branch 1 taken 50762 times.
✓ Branch 2 taken 9282 times.
✓ Branch 3 taken 61 times.
✓ Branch 4 taken 57056 times.
✓ Branch 5 taken 2988 times.
120149 (!(ic->iformat->flags & AVFMT_NOTIMESTAMPS) || sti->need_parsing == AVSTREAM_PARSE_FULL_RAW) &&
2672
2/2
✓ Branch 0 taken 60000 times.
✓ Branch 1 taken 44 times.
60044 sti->codec_info_nb_frames < ((st->disposition & AV_DISPOSITION_ATTACHED_PIC) ? 1 : ic->max_ts_probe) &&
2673
2/2
✓ Branch 0 taken 41396 times.
✓ Branch 1 taken 15660 times.
57056 (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ||
2674
2/2
✓ Branch 0 taken 783 times.
✓ Branch 1 taken 40613 times.
41396 st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO))
2675 break;
2676 }
2677 199442 analyzed_all_streams = 0;
2678
4/4
✓ Branch 0 taken 110367 times.
✓ Branch 1 taken 89075 times.
✓ Branch 2 taken 110333 times.
✓ Branch 3 taken 34 times.
199442 if (i == ic->nb_streams && !si->missing_streams) {
2679 110333 analyzed_all_streams = 1;
2680 /* NOTE: If the format has no header, then we need to read some
2681 * packets to get most of the streams, so we cannot stop here. */
2682
2/2
✓ Branch 0 taken 3282 times.
✓ Branch 1 taken 107051 times.
110333 if (!(ic->ctx_flags & AVFMTCTX_NOHEADER)) {
2683 /* If we found the info for all the codecs, we can stop. */
2684 3282 ret = count;
2685 3282 av_log(ic, AV_LOG_DEBUG, "All info found\n");
2686 3282 flush_codecs = 0;
2687 3282 break;
2688 }
2689 }
2690 /* We did not get all the codec info, but we read too much data. */
2691
2/2
✓ Branch 0 taken 2967 times.
✓ Branch 1 taken 193193 times.
196160 if (read_size >= probesize) {
2692 2967 ret = count;
2693 2967 av_log(ic, AV_LOG_DEBUG,
2694 "Probe buffer size limit of %"PRId64" bytes reached\n", probesize);
2695
2/2
✓ Branch 0 taken 2981 times.
✓ Branch 1 taken 2967 times.
5948 for (unsigned i = 0; i < ic->nb_streams; i++) {
2696 2981 AVStream *const st = ic->streams[i];
2697 2981 FFStream *const sti = ffstream(st);
2698
2/2
✓ Branch 0 taken 58 times.
✓ Branch 1 taken 2923 times.
2981 if (!st->r_frame_rate.num &&
2699
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 43 times.
58 sti->info->duration_count <= 1 &&
2700
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 14 times.
15 st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
2701
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 strcmp(ic->iformat->name, "image2"))
2702 1 av_log(ic, AV_LOG_WARNING,
2703 "Stream #%d: not enough frames to estimate rate; "
2704 "consider increasing probesize\n", i);
2705 }
2706 2967 break;
2707 }
2708
2709 /* NOTE: A new stream can be added there if no header in file
2710 * (AVFMTCTX_NOHEADER). */
2711 193193 ret = read_frame_internal(ic, pkt1);
2712
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 193193 times.
193193 if (ret == AVERROR(EAGAIN))
2713 continue;
2714
2715
2/2
✓ Branch 0 taken 1174 times.
✓ Branch 1 taken 192019 times.
193193 if (ret < 0) {
2716 /* EOF or error*/
2717 1174 eof_reached = 1;
2718 1174 break;
2719 }
2720
2721
1/2
✓ Branch 0 taken 192019 times.
✗ Branch 1 not taken.
192019 if (!(ic->flags & AVFMT_FLAG_NOBUFFER)) {
2722 192019 ret = avpriv_packet_list_put(&si->packet_buffer,
2723 pkt1, NULL, 0);
2724
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 192019 times.
192019 if (ret < 0)
2725 goto unref_then_goto_end;
2726
2727 192019 pkt = &si->packet_buffer.tail->pkt;
2728 } else {
2729 pkt = pkt1;
2730 }
2731
2732 192019 st = ic->streams[pkt->stream_index];
2733 192019 sti = ffstream(st);
2734
2/2
✓ Branch 0 taken 191963 times.
✓ Branch 1 taken 56 times.
192019 if (!(st->disposition & AV_DISPOSITION_ATTACHED_PIC))
2735 191963 read_size += pkt->size;
2736
2737 192019 avctx = sti->avctx;
2738
2/2
✓ Branch 0 taken 643 times.
✓ Branch 1 taken 191376 times.
192019 if (!sti->avctx_inited) {
2739 643 ret = avcodec_parameters_to_context(avctx, st->codecpar);
2740
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 643 times.
643 if (ret < 0)
2741 goto unref_then_goto_end;
2742 643 sti->avctx_inited = 1;
2743 }
2744
2745
4/4
✓ Branch 0 taken 174586 times.
✓ Branch 1 taken 17433 times.
✓ Branch 2 taken 162323 times.
✓ Branch 3 taken 12263 times.
192019 if (pkt->dts != AV_NOPTS_VALUE && sti->codec_info_nb_frames > 1) {
2746 /* check for non-increasing dts */
2747
2/2
✓ Branch 0 taken 157496 times.
✓ Branch 1 taken 4827 times.
162323 if (sti->info->fps_last_dts != AV_NOPTS_VALUE &&
2748
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 157473 times.
157496 sti->info->fps_last_dts >= pkt->dts) {
2749 23 av_log(ic, AV_LOG_DEBUG,
2750 "Non-increasing DTS in stream %d: packet %d with DTS "
2751 "%"PRId64", packet %d with DTS %"PRId64"\n",
2752 23 st->index, sti->info->fps_last_dts_idx,
2753 23 sti->info->fps_last_dts, sti->codec_info_nb_frames,
2754 23 pkt->dts);
2755 23 sti->info->fps_first_dts =
2756 23 sti->info->fps_last_dts = AV_NOPTS_VALUE;
2757 }
2758 /* Check for a discontinuity in dts. If the difference in dts
2759 * is more than 1000 times the average packet duration in the
2760 * sequence, we treat it as a discontinuity. */
2761
2/2
✓ Branch 0 taken 157473 times.
✓ Branch 1 taken 4850 times.
162323 if (sti->info->fps_last_dts != AV_NOPTS_VALUE &&
2762
2/2
✓ Branch 0 taken 152675 times.
✓ Branch 1 taken 4798 times.
157473 sti->info->fps_last_dts_idx > sti->info->fps_first_dts_idx &&
2763 152675 (pkt->dts - (uint64_t)sti->info->fps_last_dts) / 1000 >
2764 152675 (sti->info->fps_last_dts - (uint64_t)sti->info->fps_first_dts) /
2765
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 152675 times.
152675 (sti->info->fps_last_dts_idx - sti->info->fps_first_dts_idx)) {
2766 av_log(ic, AV_LOG_WARNING,
2767 "DTS discontinuity in stream %d: packet %d with DTS "
2768 "%"PRId64", packet %d with DTS %"PRId64"\n",
2769 st->index, sti->info->fps_last_dts_idx,
2770 sti->info->fps_last_dts, sti->codec_info_nb_frames,
2771 pkt->dts);
2772 sti->info->fps_first_dts =
2773 sti->info->fps_last_dts = AV_NOPTS_VALUE;
2774 }
2775
2776 /* update stored dts values */
2777
2/2
✓ Branch 0 taken 4850 times.
✓ Branch 1 taken 157473 times.
162323 if (sti->info->fps_first_dts == AV_NOPTS_VALUE) {
2778 4850 sti->info->fps_first_dts = pkt->dts;
2779 4850 sti->info->fps_first_dts_idx = sti->codec_info_nb_frames;
2780 }
2781 162323 sti->info->fps_last_dts = pkt->dts;
2782 162323 sti->info->fps_last_dts_idx = sti->codec_info_nb_frames;
2783 }
2784
2/2
✓ Branch 0 taken 178586 times.
✓ Branch 1 taken 13433 times.
192019 if (sti->codec_info_nb_frames > 1) {
2785 178586 int64_t t = 0;
2786 int64_t limit;
2787
2788
1/2
✓ Branch 0 taken 178586 times.
✗ Branch 1 not taken.
178586 if (st->time_base.den > 0)
2789 178586 t = av_rescale_q(sti->info->codec_info_duration, st->time_base, AV_TIME_BASE_Q);
2790
2/2
✓ Branch 0 taken 112322 times.
✓ Branch 1 taken 66264 times.
178586 if (st->avg_frame_rate.num > 0)
2791
2/2
✓ Branch 1 taken 111795 times.
✓ Branch 2 taken 527 times.
112322 t = FFMAX(t, av_rescale_q(sti->codec_info_nb_frames, av_inv_q(st->avg_frame_rate), AV_TIME_BASE_Q));
2792
2793
2/2
✓ Branch 0 taken 4806 times.
✓ Branch 1 taken 173780 times.
178586 if ( t == 0
2794
2/2
✓ Branch 0 taken 1511 times.
✓ Branch 1 taken 3295 times.
4806 && sti->codec_info_nb_frames > 30
2795
2/2
✓ Branch 0 taken 332 times.
✓ Branch 1 taken 1179 times.
1511 && sti->info->fps_first_dts != AV_NOPTS_VALUE
2796
1/2
✓ Branch 0 taken 332 times.
✗ Branch 1 not taken.
332 && sti->info->fps_last_dts != AV_NOPTS_VALUE) {
2797 332 int64_t dur = av_sat_sub64(sti->info->fps_last_dts, sti->info->fps_first_dts);
2798
1/2
✓ Branch 0 taken 332 times.
✗ Branch 1 not taken.
332 t = FFMAX(t, av_rescale_q(dur, st->time_base, AV_TIME_BASE_Q));
2799 }
2800
2801
2/2
✓ Branch 0 taken 100852 times.
✓ Branch 1 taken 77734 times.
178586 if (analyzed_all_streams) limit = max_analyze_duration;
2802
2/2
✓ Branch 0 taken 91 times.
✓ Branch 1 taken 77643 times.
77734 else if (avctx->codec_type == AVMEDIA_TYPE_SUBTITLE) limit = max_subtitle_analyze_duration;
2803 77643 else limit = max_stream_analyze_duration;
2804
2805
2/2
✓ Branch 0 taken 101 times.
✓ Branch 1 taken 178485 times.
178586 if (t >= limit) {
2806 101 av_log(ic, AV_LOG_VERBOSE, "max_analyze_duration %"PRId64" reached at %"PRId64" microseconds st:%d\n",
2807 limit,
2808 101 t, pkt->stream_index);
2809
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 101 times.
101 if (ic->flags & AVFMT_FLAG_NOBUFFER)
2810 av_packet_unref(pkt1);
2811 101 break;
2812 }
2813
2/2
✓ Branch 0 taken 174109 times.
✓ Branch 1 taken 4376 times.
178485 if (pkt->duration > 0) {
2814
4/4
✓ Branch 0 taken 174065 times.
✓ Branch 1 taken 44 times.
✓ Branch 2 taken 13972 times.
✓ Branch 3 taken 160093 times.
174109 const int fields = sti->codec_desc && (sti->codec_desc->props & AV_CODEC_PROP_FIELDS);
2815
1/8
✗ Branch 0 not taken.
✓ Branch 1 taken 174109 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.
174109 if (avctx->codec_type == AVMEDIA_TYPE_SUBTITLE && pkt->pts != AV_NOPTS_VALUE && st->start_time != AV_NOPTS_VALUE && pkt->pts >= st->start_time
2816 && (uint64_t)pkt->pts - st->start_time < INT64_MAX
2817 ) {
2818 sti->info->codec_info_duration = FFMIN(pkt->pts - st->start_time, sti->info->codec_info_duration + pkt->duration);
2819 } else
2820 174109 sti->info->codec_info_duration += pkt->duration;
2821
4/4
✓ Branch 0 taken 34558 times.
✓ Branch 1 taken 91719 times.
✓ Branch 2 taken 13600 times.
✓ Branch 3 taken 20958 times.
300386 sti->info->codec_info_duration_fields += sti->parser && sti->need_parsing && fields
2822
2/2
✓ Branch 0 taken 126277 times.
✓ Branch 1 taken 47832 times.
300386 ? sti->parser->repeat_pict + 1 : 2;
2823 }
2824 }
2825
2/2
✓ Branch 0 taken 130384 times.
✓ Branch 1 taken 61534 times.
191918 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
2826 #if FF_API_R_FRAME_RATE
2827 130384 ff_rfps_add_frame(ic, st, pkt->dts);
2828 #endif
2829
6/6
✓ Branch 0 taken 3963 times.
✓ Branch 1 taken 126421 times.
✓ Branch 2 taken 3724 times.
✓ Branch 3 taken 239 times.
✓ Branch 4 taken 1698 times.
✓ Branch 5 taken 2026 times.
130384 if (pkt->dts != pkt->pts && pkt->dts != AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE)
2830 1698 sti->info->frame_delay_evidence = 1;
2831 }
2832
2/2
✓ Branch 0 taken 162436 times.
✓ Branch 1 taken 29482 times.
191918 if (!sti->avctx->extradata) {
2833 162436 ret = extract_extradata(si, st, pkt);
2834
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 162436 times.
162436 if (ret < 0)
2835 goto unref_then_goto_end;
2836 }
2837
2838 /* If still no information, we try to open the codec and to
2839 * decompress the frame. We try to avoid that in most cases as
2840 * it takes longer and uses more memory. For MPEG-4, we need to
2841 * decompress for QuickTime.
2842 *
2843 * If AV_CODEC_CAP_CHANNEL_CONF is set this will force decoding of at
2844 * least one frame of codec data, this makes sure the codec initializes
2845 * the channel configuration and does not only trust the values from
2846 * the container. */
2847
2/2
✓ Branch 0 taken 175111 times.
✓ Branch 1 taken 16807 times.
367029 try_decode_frame(ic, st, pkt,
2848
2/2
✓ Branch 0 taken 79284 times.
✓ Branch 1 taken 95827 times.
175111 (options && i < orig_nb_streams) ? &options[i] : NULL);
2849
2850
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 191918 times.
191918 if (ic->flags & AVFMT_FLAG_NOBUFFER)
2851 av_packet_unref(pkt1);
2852
2853 191918 sti->codec_info_nb_frames++;
2854 191918 count++;
2855 }
2856
2857
2/2
✓ Branch 0 taken 1174 times.
✓ Branch 1 taken 6350 times.
7524 if (eof_reached) {
2858
2/2
✓ Branch 0 taken 1437 times.
✓ Branch 1 taken 1174 times.
2611 for (unsigned stream_index = 0; stream_index < ic->nb_streams; stream_index++) {
2859 1437 AVStream *const st = ic->streams[stream_index];
2860 1437 AVCodecContext *const avctx = ffstream(st)->avctx;
2861
2/2
✓ Branch 1 taken 29 times.
✓ Branch 2 taken 1408 times.
1437 if (!has_codec_parameters(st, NULL)) {
2862 29 const AVCodec *codec = find_probe_decoder(ic, st, st->codecpar->codec_id);
2863
4/4
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 14 times.
29 if (codec && !avctx->codec) {
2864 8 AVDictionary *opts = NULL;
2865
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (ic->codec_whitelist)
2866 av_dict_set(&opts, "codec_whitelist", ic->codec_whitelist, 0);
2867
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)
2868 av_log(ic, AV_LOG_WARNING,
2869 "Failed to open codec in %s\n", __func__);
2870 8 av_dict_free(&opts);
2871 }
2872 }
2873
2874 // EOF already reached while reading the stream above.
2875 // So continue with reoordering DTS with whatever delay we have.
2876
4/4
✓ Branch 0 taken 1419 times.
✓ Branch 1 taken 18 times.
✓ Branch 3 taken 18 times.
✓ Branch 4 taken 1401 times.
1437 if (si->packet_buffer.head && !has_decode_delay_been_guessed(st)) {
2877 18 update_dts_from_pts(ic, stream_index, si->packet_buffer.head);
2878 }
2879 }
2880 }
2881
2882
2/2
✓ Branch 0 taken 4242 times.
✓ Branch 1 taken 3282 times.
7524 if (flush_codecs) {
2883 4242 AVPacket *empty_pkt = si->pkt;
2884 4242 int err = 0;
2885 4242 av_packet_unref(empty_pkt);
2886
2887
2/2
✓ Branch 0 taken 4557 times.
✓ Branch 1 taken 4242 times.
8799 for (unsigned i = 0; i < ic->nb_streams; i++) {
2888 4557 AVStream *const st = ic->streams[i];
2889 4557 FFStream *const sti = ffstream(st);
2890
2891 /* flush the decoders */
2892
2/2
✓ Branch 0 taken 4479 times.
✓ Branch 1 taken 78 times.
4557 if (sti->info->found_decoder == 1) {
2893
2/2
✓ Branch 0 taken 4194 times.
✓ Branch 1 taken 285 times.
8673 err = try_decode_frame(ic, st, empty_pkt,
2894
2/2
✓ Branch 0 taken 4178 times.
✓ Branch 1 taken 16 times.
4194 (options && i < orig_nb_streams)
2895 4178 ? &options[i] : NULL);
2896
2897
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4479 times.
4479 if (err < 0) {
2898 av_log(ic, AV_LOG_INFO,
2899 "decoding for stream %d failed\n", st->index);
2900 }
2901 }
2902 }
2903 }
2904
2905 7524 ff_rfps_calculate(ic);
2906
2907
2/2
✓ Branch 0 taken 8288 times.
✓ Branch 1 taken 7524 times.
15812 for (unsigned i = 0; i < ic->nb_streams; i++) {
2908 8288 AVStream *const st = ic->streams[i];
2909 8288 FFStream *const sti = ffstream(st);
2910 8288 AVCodecContext *const avctx = sti->avctx;
2911
2912
2/2
✓ Branch 0 taken 6195 times.
✓ Branch 1 taken 2093 times.
8288 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
2913
6/6
✓ Branch 0 taken 634 times.
✓ Branch 1 taken 5561 times.
✓ Branch 2 taken 585 times.
✓ Branch 3 taken 49 times.
✓ Branch 4 taken 569 times.
✓ Branch 5 taken 16 times.
6195 if (avctx->codec_id == AV_CODEC_ID_RAWVIDEO && !avctx->codec_tag && !avctx->bits_per_coded_sample) {
2914 569 uint32_t tag= avcodec_pix_fmt_to_codec_tag(avctx->pix_fmt);
2915
2/2
✓ Branch 1 taken 562 times.
✓ Branch 2 taken 7 times.
569 if (avpriv_pix_fmt_find(PIX_FMT_LIST_RAW, tag) == avctx->pix_fmt)
2916 562 avctx->codec_tag= tag;
2917 }
2918
2919 /* estimate average framerate if not set by demuxer */
2920
2/2
✓ Branch 0 taken 3975 times.
✓ Branch 1 taken 2220 times.
6195 if (sti->info->codec_info_duration_fields &&
2921
2/2
✓ Branch 0 taken 262 times.
✓ Branch 1 taken 3713 times.
3975 !st->avg_frame_rate.num &&
2922
1/2
✓ Branch 0 taken 262 times.
✗ Branch 1 not taken.
262 sti->info->codec_info_duration) {
2923 262 int best_fps = 0;
2924 262 double best_error = 0.01;
2925 262 AVRational codec_frame_rate = avctx->framerate;
2926
2927
1/2
✓ Branch 0 taken 262 times.
✗ Branch 1 not taken.
262 if (sti->info->codec_info_duration >= INT64_MAX / st->time_base.num / 2||
2928
1/2
✓ Branch 0 taken 262 times.
✗ Branch 1 not taken.
262 sti->info->codec_info_duration_fields >= INT64_MAX / st->time_base.den ||
2929
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 262 times.
262 sti->info->codec_info_duration < 0)
2930 continue;
2931 262 av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den,
2932 262 sti->info->codec_info_duration_fields * (int64_t) st->time_base.den,
2933 262 sti->info->codec_info_duration * 2 * (int64_t) st->time_base.num, 60000);
2934
2935 /* Round guessed framerate to a "standard" framerate if it's
2936 * within 1% of the original estimate. */
2937
2/2
✓ Branch 0 taken 104538 times.
✓ Branch 1 taken 262 times.
104800 for (int j = 0; j < MAX_STD_TIMEBASES; j++) {
2938 104538 AVRational std_fps = { get_std_framerate(j), 12 * 1001 };
2939 104538 double error = fabs(av_q2d(st->avg_frame_rate) /
2940 104538 av_q2d(std_fps) - 1);
2941
2942
2/2
✓ Branch 0 taken 710 times.
✓ Branch 1 taken 103828 times.
104538 if (error < best_error) {
2943 710 best_error = error;
2944 710 best_fps = std_fps.num;
2945 }
2946
2947
2/2
✓ Branch 1 taken 12768 times.
✓ Branch 2 taken 91770 times.
104538 if ((ffifmt(ic->iformat)->flags_internal & FF_INFMT_FLAG_PREFER_CODEC_FRAMERATE) &&
2948
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) {
2949 12768 error = fabs(av_q2d(codec_frame_rate) /
2950 12768 av_q2d(std_fps) - 1);
2951
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 12758 times.
12768 if (error < best_error) {
2952 10 best_error = error;
2953 10 best_fps = std_fps.num;
2954 }
2955 }
2956 }
2957
2/2
✓ Branch 0 taken 260 times.
✓ Branch 1 taken 2 times.
262 if (best_fps)
2958 260 av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den,
2959 best_fps, 12 * 1001, INT_MAX);
2960 }
2961
2/2
✓ Branch 0 taken 1830 times.
✓ Branch 1 taken 4365 times.
6195 if (!st->r_frame_rate.num) {
2962 1830 const AVCodecDescriptor *desc = sti->codec_desc;
2963
3/4
✓ Branch 0 taken 1830 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 245 times.
✓ Branch 3 taken 1585 times.
1830 AVRational mul = (AVRational){ desc && (desc->props & AV_CODEC_PROP_FIELDS) ? 2 : 1, 1 };
2964 1830 AVRational fr = av_mul_q(avctx->framerate, mul);
2965
2966
5/6
✓ Branch 0 taken 187 times.
✓ Branch 1 taken 1643 times.
✓ Branch 2 taken 187 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 173 times.
✓ Branch 7 taken 14 times.
1830 if (fr.num && fr.den && av_cmp_q(st->time_base, av_inv_q(fr)) <= 0) {
2967 173 st->r_frame_rate = fr;
2968 } else {
2969 1657 st->r_frame_rate.num = st->time_base.den;
2970 1657 st->r_frame_rate.den = st->time_base.num;
2971 }
2972 }
2973 6195 st->codecpar->framerate = avctx->framerate;
2974
3/4
✓ Branch 0 taken 116 times.
✓ Branch 1 taken 6079 times.
✓ Branch 2 taken 116 times.
✗ Branch 3 not taken.
6195 if (sti->display_aspect_ratio.num && sti->display_aspect_ratio.den) {
2975 116 AVRational hw_ratio = { avctx->height, avctx->width };
2976 116 st->sample_aspect_ratio = av_mul_q(sti->display_aspect_ratio,
2977 hw_ratio);
2978 }
2979
2/2
✓ Branch 0 taken 1924 times.
✓ Branch 1 taken 169 times.
2093 } else if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
2980
2/2
✓ Branch 0 taken 611 times.
✓ Branch 1 taken 1313 times.
1924 if (!avctx->bits_per_coded_sample)
2981 611 avctx->bits_per_coded_sample =
2982 611 av_get_bits_per_sample(avctx->codec_id);
2983 // set stream disposition based on audio service type
2984
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 1924 times.
1924 switch (avctx->audio_service_type) {
2985 case AV_AUDIO_SERVICE_TYPE_EFFECTS:
2986 st->disposition = AV_DISPOSITION_CLEAN_EFFECTS;
2987 break;
2988 case AV_AUDIO_SERVICE_TYPE_VISUALLY_IMPAIRED:
2989 st->disposition = AV_DISPOSITION_VISUAL_IMPAIRED;
2990 break;
2991 case AV_AUDIO_SERVICE_TYPE_HEARING_IMPAIRED:
2992 st->disposition = AV_DISPOSITION_HEARING_IMPAIRED;
2993 break;
2994 case AV_AUDIO_SERVICE_TYPE_COMMENTARY:
2995 st->disposition = AV_DISPOSITION_COMMENT;
2996 break;
2997 case AV_AUDIO_SERVICE_TYPE_KARAOKE:
2998 st->disposition = AV_DISPOSITION_KARAOKE;
2999 break;
3000 }
3001 }
3002 }
3003
3004
1/2
✓ Branch 0 taken 7524 times.
✗ Branch 1 not taken.
7524 if (probesize)
3005 7524 estimate_timings(ic, old_offset);
3006
3007 7524 av_opt_set_int(ic, "skip_clear", 0, AV_OPT_SEARCH_CHILDREN);
3008
3009
3/4
✓ Branch 0 taken 6350 times.
✓ Branch 1 taken 1174 times.
✓ Branch 2 taken 6350 times.
✗ Branch 3 not taken.
7524 if (ret >= 0 && ic->nb_streams)
3010 /* We could not have all the codec parameters before EOF. */
3011 6350 ret = -1;
3012
2/2
✓ Branch 0 taken 8288 times.
✓ Branch 1 taken 7524 times.
15812 for (unsigned i = 0; i < ic->nb_streams; i++) {
3013 8288 AVStream *const st = ic->streams[i];
3014 8288 FFStream *const sti = ffstream(st);
3015 const char *errmsg;
3016
3017 /* if no packet was ever seen, update context now for has_codec_parameters */
3018
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 8275 times.
8288 if (!sti->avctx_inited) {
3019
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 5 times.
13 if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
3020
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 st->codecpar->format == AV_SAMPLE_FMT_NONE)
3021 8 st->codecpar->format = sti->avctx->sample_fmt;
3022 13 ret = avcodec_parameters_to_context(sti->avctx, st->codecpar);
3023
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 if (ret < 0)
3024 goto find_stream_info_err;
3025 }
3026
2/2
✓ Branch 1 taken 36 times.
✓ Branch 2 taken 8252 times.
8288 if (!has_codec_parameters(st, &errmsg)) {
3027 char buf[256];
3028 36 avcodec_string(buf, sizeof(buf), sti->avctx, 0);
3029 36 av_log(ic, AV_LOG_WARNING,
3030 "Could not find codec parameters for stream %d (%s): %s\n"
3031 "Consider increasing the value for the 'analyzeduration' (%"PRId64") and 'probesize' (%"PRId64") options\n",
3032 i, buf, errmsg, ic->max_analyze_duration, ic->probesize);
3033 } else {
3034 8252 ret = 0;
3035 }
3036 }
3037
3038 7524 err = compute_chapters_end(ic);
3039
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7524 times.
7524 if (err < 0) {
3040 ret = err;
3041 goto find_stream_info_err;
3042 }
3043
3044 /* update the stream parameters from the internal codec contexts */
3045
2/2
✓ Branch 0 taken 8288 times.
✓ Branch 1 taken 7524 times.
15812 for (unsigned i = 0; i < ic->nb_streams; i++) {
3046 8288 AVStream *const st = ic->streams[i];
3047 8288 FFStream *const sti = ffstream(st);
3048
3049
2/2
✓ Branch 0 taken 8275 times.
✓ Branch 1 taken 13 times.
8288 if (sti->avctx_inited) {
3050 8275 ret = avcodec_parameters_from_context(st->codecpar, sti->avctx);
3051
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8275 times.
8275 if (ret < 0)
3052 goto find_stream_info_err;
3053
3054
3/4
✓ Branch 0 taken 8091 times.
✓ Branch 1 taken 184 times.
✓ Branch 2 taken 8091 times.
✗ Branch 3 not taken.
8275 if (sti->avctx->rc_buffer_size > 0 || sti->avctx->rc_max_rate > 0 ||
3055
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8091 times.
8091 sti->avctx->rc_min_rate) {
3056 size_t cpb_size;
3057 184 AVCPBProperties *props = av_cpb_properties_alloc(&cpb_size);
3058
1/2
✓ Branch 0 taken 184 times.
✗ Branch 1 not taken.
184 if (props) {
3059
1/2
✓ Branch 0 taken 184 times.
✗ Branch 1 not taken.
184 if (sti->avctx->rc_buffer_size > 0)
3060 184 props->buffer_size = sti->avctx->rc_buffer_size;
3061
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 184 times.
184 if (sti->avctx->rc_min_rate > 0)
3062 props->min_bitrate = sti->avctx->rc_min_rate;
3063
2/2
✓ Branch 0 taken 77 times.
✓ Branch 1 taken 107 times.
184 if (sti->avctx->rc_max_rate > 0)
3064 77 props->max_bitrate = sti->avctx->rc_max_rate;
3065
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 184 times.
184 if (!av_packet_side_data_add(&st->codecpar->coded_side_data,
3066 184 &st->codecpar->nb_coded_side_data,
3067 AV_PKT_DATA_CPB_PROPERTIES,
3068 (uint8_t *)props, cpb_size, 0))
3069 av_free(props);
3070 }
3071 }
3072 }
3073
3074 8288 sti->avctx_inited = 0;
3075 #if FF_API_AVSTREAM_SIDE_DATA
3076 FF_DISABLE_DEPRECATION_WARNINGS
3077
2/2
✓ Branch 0 taken 478 times.
✓ Branch 1 taken 7810 times.
8288 if (st->codecpar->nb_coded_side_data > 0) {
3078
2/4
✓ Branch 0 taken 478 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 478 times.
478 av_assert0(!st->side_data && !st->nb_side_data);
3079 478 st->side_data = av_calloc(st->codecpar->nb_coded_side_data, sizeof(*st->side_data));
3080
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 478 times.
478 if (!st->side_data) {
3081 ret = AVERROR(ENOMEM);
3082 goto find_stream_info_err;
3083 }
3084
3085
2/2
✓ Branch 0 taken 509 times.
✓ Branch 1 taken 478 times.
987 for (int j = 0; j < st->codecpar->nb_coded_side_data; j++) {
3086 509 uint8_t *data = av_memdup(st->codecpar->coded_side_data[j].data,
3087 509 st->codecpar->coded_side_data[j].size);
3088
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 509 times.
509 if (!data) {
3089 ret = AVERROR(ENOMEM);
3090 goto find_stream_info_err;
3091 }
3092 509 st->side_data[j].type = st->codecpar->coded_side_data[j].type;
3093 509 st->side_data[j].size = st->codecpar->coded_side_data[j].size;
3094 509 st->side_data[j].data = data;
3095 509 st->nb_side_data++;
3096 }
3097 }
3098 FF_ENABLE_DEPRECATION_WARNINGS
3099 #endif
3100 }
3101
3102 7524 find_stream_info_err:
3103
2/2
✓ Branch 0 taken 8288 times.
✓ Branch 1 taken 7524 times.
15812 for (unsigned i = 0; i < ic->nb_streams; i++) {
3104 8288 AVStream *const st = ic->streams[i];
3105 8288 FFStream *const sti = ffstream(st);
3106 int err;
3107
3108
1/2
✓ Branch 0 taken 8288 times.
✗ Branch 1 not taken.
8288 if (sti->info) {
3109 8288 av_freep(&sti->info->duration_error);
3110 8288 av_freep(&sti->info);
3111 }
3112
3113
2/2
✓ Branch 1 taken 8180 times.
✓ Branch 2 taken 108 times.
8288 if (avcodec_is_open(sti->avctx)) {
3114 8180 err = codec_close(sti);
3115
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 8180 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
8180 if (err < 0 && ret >= 0)
3116 ret = err;
3117 }
3118
3119 8288 av_bsf_free(&sti->extract_extradata.bsf);
3120 }
3121
2/2
✓ Branch 0 taken 4488 times.
✓ Branch 1 taken 3036 times.
7524 if (ic->pb) {
3122 4488 FFIOContext *const ctx = ffiocontext(ic->pb);
3123 4488 av_log(ic, AV_LOG_DEBUG, "After avformat_find_stream_info() pos: %"PRId64" bytes read:%"PRId64" seeks:%d frames:%d\n",
3124 avio_tell(ic->pb), ctx->bytes_read, ctx->seek_count, count);
3125 }
3126 7524 return ret;
3127
3128 unref_then_goto_end:
3129 av_packet_unref(pkt1);
3130 goto find_stream_info_err;
3131 }
3132