FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/demux.c
Date: 2024-04-25 05:10:44
Exec Total Coverage
Lines: 1643 1906 86.2%
Functions: 48 48 100.0%
Branches: 1497 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 "avio_internal.h"
47 #include "demux.h"
48 #include "id3v2.h"
49 #include "internal.h"
50 #include "url.h"
51
52 2423726 static int64_t wrap_timestamp(const AVStream *st, int64_t timestamp)
53 {
54 2423726 const FFStream *const sti = cffstream(st);
55
4/4
✓ Branch 0 taken 146428 times.
✓ Branch 1 taken 2277298 times.
✓ Branch 2 taken 141962 times.
✓ Branch 3 taken 4466 times.
2423726 if (sti->pts_wrap_behavior != AV_PTS_WRAP_IGNORE && st->pts_wrap_bits < 64 &&
56
3/4
✓ Branch 0 taken 141962 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 84363 times.
✓ Branch 3 taken 57599 times.
141962 sti->pts_wrap_reference != AV_NOPTS_VALUE && timestamp != AV_NOPTS_VALUE) {
57
2/2
✓ Branch 0 taken 84159 times.
✓ Branch 1 taken 204 times.
84363 if (sti->pts_wrap_behavior == AV_PTS_WRAP_ADD_OFFSET &&
58
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 84159 times.
84159 timestamp < sti->pts_wrap_reference)
59 return timestamp + (1ULL << st->pts_wrap_bits);
60
2/2
✓ Branch 0 taken 204 times.
✓ Branch 1 taken 84159 times.
84363 else if (sti->pts_wrap_behavior == AV_PTS_WRAP_SUB_OFFSET &&
61
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 154 times.
204 timestamp >= sti->pts_wrap_reference)
62 50 return timestamp - (1ULL << st->pts_wrap_bits);
63 }
64 2423676 return timestamp;
65 }
66
67 298944 int64_t ff_wrap_timestamp(const AVStream *st, int64_t timestamp)
68 {
69 298944 return wrap_timestamp(st, timestamp);
70 }
71
72 9153 static const AVCodec *find_probe_decoder(AVFormatContext *s, const AVStream *st, enum AVCodecID codec_id)
73 {
74 const AVCodec *codec;
75
76 #if CONFIG_H264_DECODER
77 /* Other parts of the code assume this decoder to be used for h264,
78 * so force it if possible. */
79
2/2
✓ Branch 0 taken 326 times.
✓ Branch 1 taken 8827 times.
9153 if (codec_id == AV_CODEC_ID_H264)
80 326 return avcodec_find_decoder_by_name("h264");
81 #endif
82
83 8827 codec = ff_find_decoder(s, st, codec_id);
84
2/2
✓ Branch 0 taken 167 times.
✓ Branch 1 taken 8660 times.
8827 if (!codec)
85 167 return NULL;
86
87
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8660 times.
8660 if (codec->capabilities & AV_CODEC_CAP_AVOID_PROBING) {
88 const AVCodec *probe_codec = NULL;
89 void *iter = NULL;
90 while ((probe_codec = av_codec_iterate(&iter))) {
91 if (probe_codec->id == codec->id &&
92 av_codec_is_decoder(probe_codec) &&
93 !(probe_codec->capabilities & (AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_EXPERIMENTAL))) {
94 return probe_codec;
95 }
96 }
97 }
98
99 8660 return codec;
100 }
101
102 3005 static int set_codec_from_probe_data(AVFormatContext *s, AVStream *st,
103 AVProbeData *pd)
104 {
105 static const struct {
106 const char *name;
107 enum AVCodecID id;
108 enum AVMediaType type;
109 } fmt_id_type[] = {
110 { "aac", AV_CODEC_ID_AAC, AVMEDIA_TYPE_AUDIO },
111 { "ac3", AV_CODEC_ID_AC3, AVMEDIA_TYPE_AUDIO },
112 { "aptx", AV_CODEC_ID_APTX, AVMEDIA_TYPE_AUDIO },
113 { "dts", AV_CODEC_ID_DTS, AVMEDIA_TYPE_AUDIO },
114 { "dvbsub", AV_CODEC_ID_DVB_SUBTITLE, AVMEDIA_TYPE_SUBTITLE },
115 { "dvbtxt", AV_CODEC_ID_DVB_TELETEXT, AVMEDIA_TYPE_SUBTITLE },
116 { "eac3", AV_CODEC_ID_EAC3, AVMEDIA_TYPE_AUDIO },
117 { "h264", AV_CODEC_ID_H264, AVMEDIA_TYPE_VIDEO },
118 { "hevc", AV_CODEC_ID_HEVC, AVMEDIA_TYPE_VIDEO },
119 { "loas", AV_CODEC_ID_AAC_LATM, AVMEDIA_TYPE_AUDIO },
120 { "m4v", AV_CODEC_ID_MPEG4, AVMEDIA_TYPE_VIDEO },
121 { "mjpeg_2000", AV_CODEC_ID_JPEG2000, AVMEDIA_TYPE_VIDEO },
122 { "mp3", AV_CODEC_ID_MP3, AVMEDIA_TYPE_AUDIO },
123 { "mpegvideo", AV_CODEC_ID_MPEG2VIDEO, AVMEDIA_TYPE_VIDEO },
124 { "truehd", AV_CODEC_ID_TRUEHD, AVMEDIA_TYPE_AUDIO },
125 { "evc", AV_CODEC_ID_EVC, AVMEDIA_TYPE_VIDEO },
126 { "vvc", AV_CODEC_ID_VVC, AVMEDIA_TYPE_VIDEO },
127 { 0 }
128 };
129 int score;
130 3005 const AVInputFormat *fmt = av_probe_input_format3(pd, 1, &score);
131 3005 FFStream *const sti = ffstream(st);
132
133
2/2
✓ Branch 0 taken 932 times.
✓ Branch 1 taken 2073 times.
3005 if (fmt) {
134 932 av_log(s, AV_LOG_DEBUG,
135 "Probe with size=%d, packets=%d detected %s with score=%d\n",
136 932 pd->buf_size, s->max_probe_packets - sti->probe_packets,
137 932 fmt->name, score);
138
2/2
✓ Branch 0 taken 15699 times.
✓ Branch 1 taken 910 times.
16609 for (int i = 0; fmt_id_type[i].name; i++) {
139
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 15675 times.
15699 if (!strcmp(fmt->name, fmt_id_type[i].name)) {
140
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 13 times.
24 if (fmt_id_type[i].type != AVMEDIA_TYPE_AUDIO &&
141
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 st->codecpar->sample_rate)
142 continue;
143
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 18 times.
24 if (sti->request_probe > score &&
144
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
6 st->codecpar->codec_id != fmt_id_type[i].id)
145 2 continue;
146 22 st->codecpar->codec_id = fmt_id_type[i].id;
147 22 st->codecpar->codec_type = fmt_id_type[i].type;
148 22 sti->need_context_update = 1;
149 22 return score;
150 }
151 }
152 }
153 2983 return 0;
154 }
155
156 7167 static int init_input(AVFormatContext *s, const char *filename,
157 AVDictionary **options)
158 {
159 int ret;
160 7167 AVProbeData pd = { filename, NULL, 0 };
161 7167 int score = AVPROBE_SCORE_RETRY;
162
163
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 7155 times.
7167 if (s->pb) {
164 12 s->flags |= AVFMT_FLAG_CUSTOM_IO;
165
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (!s->iformat)
166 return av_probe_input_buffer2(s->pb, &s->iformat, filename,
167 s, 0, s->format_probesize);
168
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 else if (s->iformat->flags & AVFMT_NOFILE)
169 av_log(s, AV_LOG_WARNING, "Custom AVIOContext makes no sense and "
170 "will be ignored with AVFMT_NOFILE format.\n");
171 12 return 0;
172 }
173
174
4/4
✓ Branch 0 taken 3360 times.
✓ Branch 1 taken 3795 times.
✓ Branch 2 taken 816 times.
✓ Branch 3 taken 2544 times.
7155 if ((s->iformat && s->iformat->flags & AVFMT_NOFILE) ||
175
4/4
✓ Branch 0 taken 3795 times.
✓ Branch 1 taken 816 times.
✓ Branch 3 taken 190 times.
✓ Branch 4 taken 3605 times.
4611 (!s->iformat && (s->iformat = av_probe_input_format2(&pd, 0, &score))))
176 2734 return score;
177
178
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4421 times.
4421 if ((ret = s->io_open(s, &s->pb, filename, AVIO_FLAG_READ | s->avio_flags, options)) < 0)
179 return ret;
180
181
2/2
✓ Branch 0 taken 816 times.
✓ Branch 1 taken 3605 times.
4421 if (s->iformat)
182 816 return 0;
183 3605 return av_probe_input_buffer2(s->pb, &s->iformat, filename,
184 3605 s, 0, s->format_probesize);
185 }
186
187 7167 static int update_stream_avctx(AVFormatContext *s)
188 {
189 int ret;
190
2/2
✓ Branch 0 taken 7750 times.
✓ Branch 1 taken 7167 times.
14917 for (unsigned i = 0; i < s->nb_streams; i++) {
191 7750 AVStream *const st = s->streams[i];
192 7750 FFStream *const sti = ffstream(st);
193
194
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7750 times.
7750 if (!sti->need_context_update)
195 continue;
196
197 /* close parser, because it depends on the codec */
198
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 7750 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
7750 if (sti->parser && sti->avctx->codec_id != st->codecpar->codec_id) {
199 av_parser_close(sti->parser);
200 sti->parser = NULL;
201 }
202
203 /* update internal codec context, for the parser */
204 7750 ret = avcodec_parameters_to_context(sti->avctx, st->codecpar);
205
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7750 times.
7750 if (ret < 0)
206 return ret;
207
208 7750 sti->codec_desc = avcodec_descriptor_get(sti->avctx->codec_id);
209
210 7750 sti->need_context_update = 0;
211 }
212 7167 return 0;
213 }
214
215 7167 int avformat_open_input(AVFormatContext **ps, const char *filename,
216 const AVInputFormat *fmt, AVDictionary **options)
217 {
218 7167 AVFormatContext *s = *ps;
219 FFFormatContext *si;
220 7167 AVDictionary *tmp = NULL;
221 7167 ID3v2ExtraMeta *id3v2_extra_meta = NULL;
222 7167 int ret = 0;
223
224
3/4
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 7145 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 22 times.
7167 if (!s && !(s = avformat_alloc_context()))
225 return AVERROR(ENOMEM);
226 7167 si = ffformatcontext(s);
227
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7167 times.
7167 if (!s->av_class) {
228 av_log(NULL, AV_LOG_ERROR, "Input context has not been properly allocated by avformat_alloc_context() and is not NULL either\n");
229 return AVERROR(EINVAL);
230 }
231
2/2
✓ Branch 0 taken 3372 times.
✓ Branch 1 taken 3795 times.
7167 if (fmt)
232 3372 s->iformat = fmt;
233
234
2/2
✓ Branch 0 taken 7157 times.
✓ Branch 1 taken 10 times.
7167 if (options)
235 7157 av_dict_copy(&tmp, *options, 0);
236
237
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 7155 times.
7167 if (s->pb) // must be before any goto fail
238 12 s->flags |= AVFMT_FLAG_CUSTOM_IO;
239
240
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7167 times.
7167 if ((ret = av_opt_set_dict(s, &tmp)) < 0)
241 goto fail;
242
243
2/4
✓ Branch 0 taken 7167 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 7167 times.
7167 if (!(s->url = av_strdup(filename ? filename : ""))) {
244 ret = AVERROR(ENOMEM);
245 goto fail;
246 }
247
248
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7167 times.
7167 if ((ret = init_input(s, filename, &tmp)) < 0)
249 goto fail;
250 7167 s->probe_score = ret;
251
252
6/6
✓ Branch 0 taken 7084 times.
✓ Branch 1 taken 83 times.
✓ Branch 2 taken 4350 times.
✓ Branch 3 taken 2734 times.
✓ Branch 4 taken 4349 times.
✓ Branch 5 taken 1 times.
7167 if (!s->protocol_whitelist && s->pb && s->pb->protocol_whitelist) {
253 4349 s->protocol_whitelist = av_strdup(s->pb->protocol_whitelist);
254
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4349 times.
4349 if (!s->protocol_whitelist) {
255 ret = AVERROR(ENOMEM);
256 goto fail;
257 }
258 }
259
260
4/6
✓ Branch 0 taken 7167 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4433 times.
✓ Branch 3 taken 2734 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 4433 times.
7167 if (!s->protocol_blacklist && s->pb && s->pb->protocol_blacklist) {
261 s->protocol_blacklist = av_strdup(s->pb->protocol_blacklist);
262 if (!s->protocol_blacklist) {
263 ret = AVERROR(ENOMEM);
264 goto fail;
265 }
266 }
267
268
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 7167 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
7167 if (s->format_whitelist && av_match_list(s->iformat->name, s->format_whitelist, ',') <= 0) {
269 av_log(s, AV_LOG_ERROR, "Format not on whitelist \'%s\'\n", s->format_whitelist);
270 ret = AVERROR(EINVAL);
271 goto fail;
272 }
273
274 7167 avio_skip(s->pb, s->skip_initial_bytes);
275
276 /* Check filename in case an image number is expected. */
277
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7167 times.
7167 if (s->iformat->flags & AVFMT_NEEDNUMBER) {
278 if (!av_filename_number_test(filename)) {
279 ret = AVERROR(EINVAL);
280 goto fail;
281 }
282 }
283
284 7167 s->duration = s->start_time = AV_NOPTS_VALUE;
285
286 /* Allocate private data. */
287
2/2
✓ Branch 1 taken 7010 times.
✓ Branch 2 taken 157 times.
7167 if (ffifmt(s->iformat)->priv_data_size > 0) {
288
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 7010 times.
7010 if (!(s->priv_data = av_mallocz(ffifmt(s->iformat)->priv_data_size))) {
289 ret = AVERROR(ENOMEM);
290 goto fail;
291 }
292
2/2
✓ Branch 0 taken 6188 times.
✓ Branch 1 taken 822 times.
7010 if (s->iformat->priv_class) {
293 6188 *(const AVClass **) s->priv_data = s->iformat->priv_class;
294 6188 av_opt_set_defaults(s->priv_data);
295
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6188 times.
6188 if ((ret = av_opt_set_dict(s->priv_data, &tmp)) < 0)
296 goto fail;
297 }
298 }
299
300 /* e.g. AVFMT_NOFILE formats will not have an AVIOContext */
301
2/2
✓ Branch 0 taken 4433 times.
✓ Branch 1 taken 2734 times.
7167 if (s->pb)
302 4433 ff_id3v2_read_dict(s->pb, &si->id3v2_meta, ID3v2_DEFAULT_MAGIC, &id3v2_extra_meta);
303
304
1/2
✓ Branch 1 taken 7167 times.
✗ Branch 2 not taken.
7167 if (ffifmt(s->iformat)->read_header)
305
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 7167 times.
7167 if ((ret = ffifmt(s->iformat)->read_header(s)) < 0) {
306 if (ffifmt(s->iformat)->flags_internal & FF_INFMT_FLAG_INIT_CLEANUP)
307 goto close;
308 goto fail;
309 }
310
311
2/2
✓ Branch 0 taken 5911 times.
✓ Branch 1 taken 1256 times.
7167 if (!s->metadata) {
312 5911 s->metadata = si->id3v2_meta;
313 5911 si->id3v2_meta = NULL;
314
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1256 times.
1256 } else if (si->id3v2_meta) {
315 av_log(s, AV_LOG_WARNING, "Discarding ID3 tags because more suitable tags were found.\n");
316 av_dict_free(&si->id3v2_meta);
317 }
318
319
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 7157 times.
7167 if (id3v2_extra_meta) {
320
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") ||
321 !strcmp(s->iformat->name, "tta") || !strcmp(s->iformat->name, "wav")) {
322
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
10 if ((ret = ff_id3v2_parse_apic(s, id3v2_extra_meta)) < 0)
323 goto close;
324
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
10 if ((ret = ff_id3v2_parse_chapters(s, id3v2_extra_meta)) < 0)
325 goto close;
326
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
10 if ((ret = ff_id3v2_parse_priv(s, id3v2_extra_meta)) < 0)
327 goto close;
328 } else
329 av_log(s, AV_LOG_DEBUG, "demuxer does not support additional id3 data, skipping\n");
330 10 ff_id3v2_free_extra_meta(&id3v2_extra_meta);
331 }
332
333
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7167 times.
7167 if ((ret = avformat_queue_attached_pictures(s)) < 0)
334 goto close;
335
336
4/4
✓ Branch 0 taken 4433 times.
✓ Branch 1 taken 2734 times.
✓ Branch 2 taken 3998 times.
✓ Branch 3 taken 435 times.
7167 if (s->pb && !si->data_offset)
337 3998 si->data_offset = avio_tell(s->pb);
338
339 7167 si->raw_packet_buffer_size = 0;
340
341 7167 update_stream_avctx(s);
342
343
2/2
✓ Branch 0 taken 7157 times.
✓ Branch 1 taken 10 times.
7167 if (options) {
344 7157 av_dict_free(options);
345 7157 *options = tmp;
346 }
347 7167 *ps = s;
348 7167 return 0;
349
350 close:
351 if (ffifmt(s->iformat)->read_close)
352 ffifmt(s->iformat)->read_close(s);
353 fail:
354 ff_id3v2_free_extra_meta(&id3v2_extra_meta);
355 av_dict_free(&tmp);
356 if (s->pb && !(s->flags & AVFMT_FLAG_CUSTOM_IO))
357 avio_closep(&s->pb);
358 avformat_free_context(s);
359 *ps = NULL;
360 return ret;
361 }
362
363 7167 void avformat_close_input(AVFormatContext **ps)
364 {
365 AVFormatContext *s;
366 AVIOContext *pb;
367
368
2/4
✓ Branch 0 taken 7167 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 7167 times.
7167 if (!ps || !*ps)
369 return;
370
371 7167 s = *ps;
372 7167 pb = s->pb;
373
374
5/6
✓ Branch 0 taken 7167 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4451 times.
✓ Branch 3 taken 2716 times.
✓ Branch 4 taken 4408 times.
✓ Branch 5 taken 43 times.
7167 if ((s->iformat && strcmp(s->iformat->name, "image2") && s->iformat->flags & AVFMT_NOFILE) ||
375
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 7112 times.
7124 (s->flags & AVFMT_FLAG_CUSTOM_IO))
376 55 pb = NULL;
377
378
1/2
✓ Branch 0 taken 7167 times.
✗ Branch 1 not taken.
7167 if (s->iformat)
379
2/2
✓ Branch 1 taken 4560 times.
✓ Branch 2 taken 2607 times.
7167 if (ffifmt(s->iformat)->read_close)
380 4560 ffifmt(s->iformat)->read_close(s);
381
382 7167 avformat_free_context(s);
383
384 7167 *ps = NULL;
385
386 7167 avio_close(pb);
387 }
388
389 1065395 static void force_codec_ids(AVFormatContext *s, AVStream *st)
390 {
391
5/5
✓ Branch 0 taken 720389 times.
✓ Branch 1 taken 341907 times.
✓ Branch 2 taken 2969 times.
✓ Branch 3 taken 128 times.
✓ Branch 4 taken 2 times.
1065395 switch (st->codecpar->codec_type) {
392 720389 case AVMEDIA_TYPE_VIDEO:
393
2/2
✓ Branch 0 taken 103484 times.
✓ Branch 1 taken 616905 times.
720389 if (s->video_codec_id)
394 103484 st->codecpar->codec_id = s->video_codec_id;
395 720389 break;
396 341907 case AVMEDIA_TYPE_AUDIO:
397
2/2
✓ Branch 0 taken 2558 times.
✓ Branch 1 taken 339349 times.
341907 if (s->audio_codec_id)
398 2558 st->codecpar->codec_id = s->audio_codec_id;
399 341907 break;
400 2969 case AVMEDIA_TYPE_SUBTITLE:
401
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2969 times.
2969 if (s->subtitle_codec_id)
402 st->codecpar->codec_id = s->subtitle_codec_id;
403 2969 break;
404 128 case AVMEDIA_TYPE_DATA:
405
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 128 times.
128 if (s->data_codec_id)
406 st->codecpar->codec_id = s->data_codec_id;
407 128 break;
408 }
409 1065395 }
410
411 16086 static int probe_codec(AVFormatContext *s, AVStream *st, const AVPacket *pkt)
412 {
413 16086 FFFormatContext *const si = ffformatcontext(s);
414 16086 FFStream *const sti = ffstream(st);
415
416
2/2
✓ Branch 0 taken 15846 times.
✓ Branch 1 taken 240 times.
16086 if (sti->request_probe > 0) {
417 15846 AVProbeData *const pd = &sti->probe_data;
418 int end;
419 15846 av_log(s, AV_LOG_DEBUG, "probing stream %d pp:%d\n", st->index, sti->probe_packets);
420 15846 --sti->probe_packets;
421
422
2/2
✓ Branch 0 taken 15835 times.
✓ Branch 1 taken 11 times.
15846 if (pkt) {
423 15835 uint8_t *new_buf = av_realloc(pd->buf, pd->buf_size+pkt->size+AVPROBE_PADDING_SIZE);
424
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15835 times.
15835 if (!new_buf) {
425 av_log(s, AV_LOG_WARNING,
426 "Failed to reallocate probe buffer for stream %d\n",
427 st->index);
428 goto no_packet;
429 }
430 15835 pd->buf = new_buf;
431 15835 memcpy(pd->buf + pd->buf_size, pkt->data, pkt->size);
432 15835 pd->buf_size += pkt->size;
433 15835 memset(pd->buf + pd->buf_size, 0, AVPROBE_PADDING_SIZE);
434 } else {
435 11 no_packet:
436 11 sti->probe_packets = 0;
437
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 if (!pd->buf_size) {
438 av_log(s, AV_LOG_WARNING,
439 "nothing to probe for stream %d\n", st->index);
440 }
441 }
442
443 31692 end = si->raw_packet_buffer_size >= s->probesize
444
3/4
✓ Branch 0 taken 15846 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 494 times.
✓ Branch 3 taken 15352 times.
15846 || sti->probe_packets <= 0;
445
446
4/4
✓ Branch 0 taken 15352 times.
✓ Branch 1 taken 494 times.
✓ Branch 2 taken 2511 times.
✓ Branch 3 taken 12841 times.
15846 if (end || av_log2(pd->buf_size) != av_log2(pd->buf_size - pkt->size)) {
447 3005 int score = set_codec_from_probe_data(s, st, pd);
448
4/4
✓ Branch 0 taken 2997 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 2979 times.
✓ Branch 3 taken 18 times.
3005 if ( (st->codecpar->codec_id != AV_CODEC_ID_NONE && score > AVPROBE_SCORE_STREAM_RETRY)
449
2/2
✓ Branch 0 taken 494 times.
✓ Branch 1 taken 2493 times.
2987 || end) {
450 512 pd->buf_size = 0;
451 512 av_freep(&pd->buf);
452 512 sti->request_probe = -1;
453
1/2
✓ Branch 0 taken 512 times.
✗ Branch 1 not taken.
512 if (st->codecpar->codec_id != AV_CODEC_ID_NONE) {
454 512 av_log(s, AV_LOG_DEBUG, "probed stream %d\n", st->index);
455 } else
456 av_log(s, AV_LOG_WARNING, "probed stream %d failed\n", st->index);
457 }
458 3005 force_codec_ids(s, st);
459 }
460 }
461 16086 return 0;
462 }
463
464 1062390 static int update_wrap_reference(AVFormatContext *s, AVStream *st, int stream_index, AVPacket *pkt)
465 {
466 1062390 FFStream *const sti = ffstream(st);
467 1062390 int64_t ref = pkt->dts;
468 int pts_wrap_behavior;
469 int64_t pts_wrap_reference;
470 AVProgram *first_program;
471
472
2/2
✓ Branch 0 taken 868391 times.
✓ Branch 1 taken 193999 times.
1062390 if (ref == AV_NOPTS_VALUE)
473 868391 ref = pkt->pts;
474
7/8
✓ Branch 0 taken 996356 times.
✓ Branch 1 taken 66034 times.
✓ Branch 2 taken 48533 times.
✓ Branch 3 taken 947823 times.
✓ Branch 4 taken 278 times.
✓ Branch 5 taken 48255 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 278 times.
1062390 if (sti->pts_wrap_reference != AV_NOPTS_VALUE || st->pts_wrap_bits >= 63 || ref == AV_NOPTS_VALUE || !s->correct_ts_overflow)
475 1062112 return 0;
476 278 ref &= (1LL << st->pts_wrap_bits)-1;
477
478 // reference time stamp should be 60 s before first time stamp
479 278 pts_wrap_reference = ref - av_rescale(60, st->time_base.den, st->time_base.num);
480 // if first time stamp is not more than 1/8 and 60s before the wrap point, subtract rather than add wrap offset
481 557 pts_wrap_behavior = (ref < (1LL << st->pts_wrap_bits) - (1LL << st->pts_wrap_bits-3)) ||
482
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)) ?
483
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 277 times.
279 AV_PTS_WRAP_ADD_OFFSET : AV_PTS_WRAP_SUB_OFFSET;
484
485 278 first_program = av_find_program_from_stream(s, NULL, stream_index);
486
487
2/2
✓ Branch 0 taken 187 times.
✓ Branch 1 taken 91 times.
278 if (!first_program) {
488 187 int default_stream_index = av_find_default_stream_index(s);
489 187 FFStream *const default_sti = ffstream(s->streams[default_stream_index]);
490
2/2
✓ Branch 0 taken 176 times.
✓ Branch 1 taken 11 times.
187 if (default_sti->pts_wrap_reference == AV_NOPTS_VALUE) {
491
2/2
✓ Branch 0 taken 291 times.
✓ Branch 1 taken 176 times.
467 for (unsigned i = 0; i < s->nb_streams; i++) {
492 291 FFStream *const sti = ffstream(s->streams[i]);
493
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 291 times.
291 if (av_find_program_from_stream(s, NULL, i))
494 continue;
495 291 sti->pts_wrap_reference = pts_wrap_reference;
496 291 sti->pts_wrap_behavior = pts_wrap_behavior;
497 }
498 } else {
499 11 sti->pts_wrap_reference = default_sti->pts_wrap_reference;
500 11 sti->pts_wrap_behavior = default_sti->pts_wrap_behavior;
501 }
502 } else {
503 91 AVProgram *program = first_program;
504
2/2
✓ Branch 0 taken 91 times.
✓ Branch 1 taken 65 times.
156 while (program) {
505
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 65 times.
91 if (program->pts_wrap_reference != AV_NOPTS_VALUE) {
506 26 pts_wrap_reference = program->pts_wrap_reference;
507 26 pts_wrap_behavior = program->pts_wrap_behavior;
508 26 break;
509 }
510 65 program = av_find_program_from_stream(s, program, stream_index);
511 }
512
513 // update every program with differing pts_wrap_reference
514 91 program = first_program;
515
2/2
✓ Branch 0 taken 91 times.
✓ Branch 1 taken 91 times.
182 while (program) {
516
2/2
✓ Branch 0 taken 65 times.
✓ Branch 1 taken 26 times.
91 if (program->pts_wrap_reference != pts_wrap_reference) {
517
2/2
✓ Branch 0 taken 103 times.
✓ Branch 1 taken 65 times.
168 for (unsigned i = 0; i < program->nb_stream_indexes; i++) {
518 103 FFStream *const sti = ffstream(s->streams[program->stream_index[i]]);
519 103 sti->pts_wrap_reference = pts_wrap_reference;
520 103 sti->pts_wrap_behavior = pts_wrap_behavior;
521 }
522
523 65 program->pts_wrap_reference = pts_wrap_reference;
524 65 program->pts_wrap_behavior = pts_wrap_behavior;
525 }
526 91 program = av_find_program_from_stream(s, program, stream_index);
527 }
528 }
529 278 return 1;
530 }
531
532 1062390 static void update_timestamps(AVFormatContext *s, AVStream *st, AVPacket *pkt)
533 {
534 1062390 FFStream *const sti = ffstream(st);
535
536
4/4
✓ Branch 1 taken 278 times.
✓ Branch 2 taken 1062112 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 277 times.
1062390 if (update_wrap_reference(s, st, pkt->stream_index, pkt) && sti->pts_wrap_behavior == AV_PTS_WRAP_SUB_OFFSET) {
537 // correct first time stamps to negative values
538
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 if (!is_relative(sti->first_dts))
539 1 sti->first_dts = wrap_timestamp(st, sti->first_dts);
540
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 if (!is_relative(st->start_time))
541 1 st->start_time = wrap_timestamp(st, st->start_time);
542
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (!is_relative(sti->cur_dts))
543 sti->cur_dts = wrap_timestamp(st, sti->cur_dts);
544 }
545
546 1062390 pkt->dts = wrap_timestamp(st, pkt->dts);
547 1062390 pkt->pts = wrap_timestamp(st, pkt->pts);
548
549 1062390 force_codec_ids(s, st);
550
551 /* TODO: audio: time filter; video: frame reordering (pts != dts) */
552
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1062390 times.
1062390 if (s->use_wallclock_as_timestamps)
553 pkt->dts = pkt->pts = av_rescale_q(av_gettime(), AV_TIME_BASE_Q, st->time_base);
554 1062390 }
555
556 /**
557 * Handle a new packet and either return it directly if possible and
558 * allow_passthrough is true or queue the packet (or drop the packet
559 * if corrupt).
560 *
561 * @return < 0 on error, 0 if the packet was passed through,
562 * 1 if it was queued or dropped
563 */
564 1062390 static int handle_new_packet(AVFormatContext *s, AVPacket *pkt, int allow_passthrough)
565 {
566 1062390 FFFormatContext *const si = ffformatcontext(s);
567 AVStream *st;
568 FFStream *sti;
569 int err;
570
571
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1062390 times.
1062390 av_assert0(pkt->stream_index < (unsigned)s->nb_streams &&
572 "Invalid stream index.\n");
573
574
2/2
✓ Branch 0 taken 193 times.
✓ Branch 1 taken 1062197 times.
1062390 if (pkt->flags & AV_PKT_FLAG_CORRUPT) {
575 386 av_log(s, AV_LOG_WARNING,
576 "Packet corrupt (stream = %d, dts = %s)%s.\n",
577 193 pkt->stream_index, av_ts2str(pkt->dts),
578
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 193 times.
193 s->flags & AVFMT_FLAG_DISCARD_CORRUPT ? ", dropping it" : "");
579
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 193 times.
193 if (s->flags & AVFMT_FLAG_DISCARD_CORRUPT) {
580 av_packet_unref(pkt);
581 return 1;
582 }
583 }
584
585 1062390 st = s->streams[pkt->stream_index];
586 1062390 sti = ffstream(st);
587
588 1062390 update_timestamps(s, st, pkt);
589
590
6/6
✓ Branch 0 taken 1046555 times.
✓ Branch 1 taken 15835 times.
✓ Branch 2 taken 1046357 times.
✓ Branch 3 taken 198 times.
✓ Branch 4 taken 1046315 times.
✓ Branch 5 taken 42 times.
1062390 if (sti->request_probe <= 0 && allow_passthrough && !si->raw_packet_buffer.head)
591 1046315 return 0;
592
593 16075 err = avpriv_packet_list_put(&si->raw_packet_buffer, pkt, NULL, 0);
594
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16075 times.
16075 if (err < 0) {
595 av_packet_unref(pkt);
596 return err;
597 }
598
599 16075 pkt = &si->raw_packet_buffer.tail->pkt;
600 16075 si->raw_packet_buffer_size += pkt->size;
601
602 16075 err = probe_codec(s, st, pkt);
603
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16075 times.
16075 if (err < 0)
604 return err;
605
606 16075 return 1;
607 }
608
609 198 int ff_buffer_packet(AVFormatContext *s, AVPacket *pkt)
610 {
611 198 int err = handle_new_packet(s, pkt, 0);
612
613 198 return err < 0 ? err : 0;
614 }
615
616 1068782 int ff_read_packet(AVFormatContext *s, AVPacket *pkt)
617 {
618 1068782 FFFormatContext *const si = ffformatcontext(s);
619 int err;
620
621 #if FF_API_INIT_PACKET
622 FF_DISABLE_DEPRECATION_WARNINGS
623 1068782 pkt->data = NULL;
624 1068782 pkt->size = 0;
625 1068782 av_init_packet(pkt);
626 FF_ENABLE_DEPRECATION_WARNINGS
627 #else
628 av_packet_unref(pkt);
629 #endif
630
631 16373 for (;;) {
632 1085155 PacketListEntry *pktl = si->raw_packet_buffer.head;
633
634
2/2
✓ Branch 0 taken 31498 times.
✓ Branch 1 taken 1053657 times.
1085155 if (pktl) {
635 31498 AVStream *const st = s->streams[pktl->pkt.stream_index];
636
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31498 times.
31498 if (si->raw_packet_buffer_size >= s->probesize)
637 if ((err = probe_codec(s, st, NULL)) < 0)
638 return err;
639
2/2
✓ Branch 1 taken 16122 times.
✓ Branch 2 taken 15376 times.
31498 if (ffstream(st)->request_probe <= 0) {
640 16122 avpriv_packet_list_get(&si->raw_packet_buffer, pkt);
641 16122 si->raw_packet_buffer_size -= pkt->size;
642 16122 return 0;
643 }
644 }
645
646 1069033 err = ffifmt(s->iformat)->read_packet(s, pkt);
647
2/2
✓ Branch 0 taken 6841 times.
✓ Branch 1 taken 1062192 times.
1069033 if (err < 0) {
648 6841 av_packet_unref(pkt);
649
650 /* Some demuxers return FFERROR_REDO when they consume
651 data and discard it (ignored streams, junk, extradata).
652 We must re-call the demuxer to get the real packet. */
653
2/2
✓ Branch 0 taken 485 times.
✓ Branch 1 taken 6356 times.
6841 if (err == FFERROR_REDO)
654 485 continue;
655
3/4
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 6345 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 11 times.
6356 if (!pktl || err == AVERROR(EAGAIN))
656 6345 return err;
657
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 11 times.
22 for (unsigned i = 0; i < s->nb_streams; i++) {
658 11 AVStream *const st = s->streams[i];
659 11 FFStream *const sti = ffstream(st);
660
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)
661
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
11 if ((err = probe_codec(s, st, NULL)) < 0)
662 return err;
663
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 av_assert0(sti->request_probe <= 0);
664 }
665 11 continue;
666 }
667
668 1062192 err = av_packet_make_refcounted(pkt);
669
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1062192 times.
1062192 if (err < 0) {
670 av_packet_unref(pkt);
671 return err;
672 }
673
674 1062192 err = handle_new_packet(s, pkt, 1);
675
2/2
✓ Branch 0 taken 1046315 times.
✓ Branch 1 taken 15877 times.
1062192 if (err <= 0) /* Error or passthrough */
676 1046315 return err;
677 }
678 }
679
680 /**
681 * Return the frame duration in seconds. Return 0 if not available.
682 */
683 311908 static void compute_frame_duration(AVFormatContext *s, int *pnum, int *pden,
684 AVStream *st, AVCodecParserContext *pc,
685 AVPacket *pkt)
686 {
687 311908 FFStream *const sti = ffstream(st);
688 311908 AVRational codec_framerate = sti->avctx->framerate;
689 int frame_size, sample_rate;
690
691 311908 *pnum = 0;
692 311908 *pden = 0;
693
3/3
✓ Branch 0 taken 209083 times.
✓ Branch 1 taken 101443 times.
✓ Branch 2 taken 1382 times.
311908 switch (st->codecpar->codec_type) {
694 209083 case AVMEDIA_TYPE_VIDEO:
695
6/6
✓ Branch 0 taken 180303 times.
✓ Branch 1 taken 28780 times.
✓ Branch 2 taken 42347 times.
✓ Branch 3 taken 137956 times.
✓ Branch 4 taken 28850 times.
✓ Branch 5 taken 13497 times.
209083 if (st->r_frame_rate.num && (!pc || !codec_framerate.num)) {
696 166806 *pnum = st->r_frame_rate.den;
697 166806 *pden = st->r_frame_rate.num;
698
2/2
✓ Branch 0 taken 23054 times.
✓ Branch 1 taken 19223 times.
42277 } else if ((s->iformat->flags & AVFMT_NOTIMESTAMPS) &&
699
2/2
✓ Branch 0 taken 14298 times.
✓ Branch 1 taken 8756 times.
23054 !codec_framerate.num &&
700
3/4
✓ Branch 0 taken 14296 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 14296 times.
✗ Branch 3 not taken.
14298 st->avg_frame_rate.num && st->avg_frame_rate.den) {
701 14296 *pnum = st->avg_frame_rate.den;
702 14296 *pden = st->avg_frame_rate.num;
703
2/2
✓ Branch 0 taken 9659 times.
✓ Branch 1 taken 18322 times.
27981 } else if (st->time_base.num * 1000LL > st->time_base.den) {
704 9659 *pnum = st->time_base.num;
705 9659 *pden = st->time_base.den;
706
2/2
✓ Branch 0 taken 18276 times.
✓ Branch 1 taken 46 times.
18322 } else if (codec_framerate.den * 1000LL > codec_framerate.num) {
707 54828 int ticks_per_frame = (sti->codec_desc &&
708
3/4
✓ Branch 0 taken 18276 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13434 times.
✓ Branch 3 taken 4842 times.
18276 (sti->codec_desc->props & AV_CODEC_PROP_FIELDS)) ? 2 : 1;
709 18276 av_reduce(pnum, pden,
710 18276 codec_framerate.den,
711 18276 codec_framerate.num * (int64_t)ticks_per_frame,
712 INT_MAX);
713
714
4/4
✓ Branch 0 taken 16443 times.
✓ Branch 1 taken 1833 times.
✓ Branch 2 taken 12728 times.
✓ Branch 3 taken 3715 times.
18276 if (pc && pc->repeat_pict) {
715 12728 av_reduce(pnum, pden,
716 12728 (*pnum) * (1LL + pc->repeat_pict),
717 12728 (*pden),
718 INT_MAX);
719 }
720 /* If this codec can be interlaced or progressive then we need
721 * a parser to compute duration of a packet. Thus if we have
722 * no parser in such case leave duration undefined. */
723
1/2
✓ Branch 0 taken 18276 times.
✗ Branch 1 not taken.
18276 if (sti->codec_desc &&
724
3/4
✓ Branch 0 taken 13434 times.
✓ Branch 1 taken 4842 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 13434 times.
18276 (sti->codec_desc->props & AV_CODEC_PROP_FIELDS) && !pc)
725 *pnum = *pden = 0;
726 }
727 209083 break;
728 101443 case AVMEDIA_TYPE_AUDIO:
729
2/2
✓ Branch 0 taken 45404 times.
✓ Branch 1 taken 56039 times.
101443 if (sti->avctx_inited) {
730 45404 frame_size = av_get_audio_frame_duration(sti->avctx, pkt->size);
731 45404 sample_rate = sti->avctx->sample_rate;
732 } else {
733 56039 frame_size = av_get_audio_frame_duration2(st->codecpar, pkt->size);
734 56039 sample_rate = st->codecpar->sample_rate;
735 }
736
4/4
✓ Branch 0 taken 95256 times.
✓ Branch 1 taken 6187 times.
✓ Branch 2 taken 95253 times.
✓ Branch 3 taken 3 times.
101443 if (frame_size <= 0 || sample_rate <= 0)
737 break;
738 95253 *pnum = frame_size;
739 95253 *pden = sample_rate;
740 95253 break;
741 1382 default:
742 1382 break;
743 }
744 311908 }
745
746 688079 static int has_decode_delay_been_guessed(AVStream *st)
747 {
748 688079 FFStream *const sti = ffstream(st);
749
2/2
✓ Branch 0 taken 671724 times.
✓ Branch 1 taken 16355 times.
688079 if (st->codecpar->codec_id != AV_CODEC_ID_H264) return 1;
750
2/2
✓ Branch 0 taken 6016 times.
✓ Branch 1 taken 10339 times.
16355 if (!sti->info) // if we have left find_stream_info then nb_decoded_frames won't increase anymore for stream copy
751 6016 return 1;
752 #if CONFIG_H264_DECODER
753
2/2
✓ Branch 0 taken 6133 times.
✓ Branch 1 taken 4206 times.
10339 if (sti->avctx->has_b_frames &&
754
2/2
✓ Branch 1 taken 911 times.
✓ Branch 2 taken 5222 times.
6133 avpriv_h264_has_num_reorder_frames(sti->avctx) == sti->avctx->has_b_frames)
755 911 return 1;
756 #endif
757
2/2
✓ Branch 0 taken 9240 times.
✓ Branch 1 taken 188 times.
9428 if (sti->avctx->has_b_frames < 3)
758 9240 return sti->nb_decoded_frames >= 7;
759
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 152 times.
188 else if (sti->avctx->has_b_frames < 4)
760 36 return sti->nb_decoded_frames >= 18;
761 else
762 152 return sti->nb_decoded_frames >= 20;
763 }
764
765 35709 static PacketListEntry *get_next_pkt(AVFormatContext *s, AVStream *st,
766 PacketListEntry *pktl)
767 {
768 35709 FFFormatContext *const si = ffformatcontext(s);
769
2/2
✓ Branch 0 taken 33925 times.
✓ Branch 1 taken 1784 times.
35709 if (pktl->next)
770 33925 return pktl->next;
771
2/2
✓ Branch 0 taken 1754 times.
✓ Branch 1 taken 30 times.
1784 if (pktl == si->packet_buffer.tail)
772 1754 return si->parse_queue.head;
773 30 return NULL;
774 }
775
776 503176 static int64_t select_from_pts_buffer(AVStream *st, int64_t *pts_buffer, int64_t dts)
777 {
778 503176 FFStream *const sti = ffstream(st);
779 1502543 int onein_oneout = st->codecpar->codec_id != AV_CODEC_ID_H264 &&
780
4/4
✓ Branch 0 taken 496191 times.
✓ Branch 1 taken 6985 times.
✓ Branch 2 taken 495516 times.
✓ Branch 3 taken 675 times.
998692 st->codecpar->codec_id != AV_CODEC_ID_HEVC &&
781
1/2
✓ Branch 0 taken 495516 times.
✗ Branch 1 not taken.
495516 st->codecpar->codec_id != AV_CODEC_ID_VVC;
782
783
2/2
✓ Branch 0 taken 7660 times.
✓ Branch 1 taken 495516 times.
503176 if (!onein_oneout) {
784 7660 int delay = sti->avctx->has_b_frames;
785
786
2/2
✓ Branch 0 taken 1208 times.
✓ Branch 1 taken 6452 times.
7660 if (dts == AV_NOPTS_VALUE) {
787 1208 int64_t best_score = INT64_MAX;
788
2/2
✓ Branch 0 taken 1387 times.
✓ Branch 1 taken 1208 times.
2595 for (int i = 0; i < delay; i++) {
789
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1387 times.
1387 if (sti->pts_reorder_error_count[i]) {
790 int64_t score = sti->pts_reorder_error[i] / sti->pts_reorder_error_count[i];
791 if (score < best_score) {
792 best_score = score;
793 dts = pts_buffer[i];
794 }
795 }
796 }
797 } else {
798
2/2
✓ Branch 0 taken 11093 times.
✓ Branch 1 taken 6452 times.
17545 for (int i = 0; i < delay; i++) {
799
2/2
✓ Branch 0 taken 10333 times.
✓ Branch 1 taken 760 times.
11093 if (pts_buffer[i] != AV_NOPTS_VALUE) {
800 10333 int64_t diff = FFABS(pts_buffer[i] - dts)
801 10333 + (uint64_t)sti->pts_reorder_error[i];
802 10333 diff = FFMAX(diff, sti->pts_reorder_error[i]);
803 10333 sti->pts_reorder_error[i] = diff;
804 10333 sti->pts_reorder_error_count[i]++;
805
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 10330 times.
10333 if (sti->pts_reorder_error_count[i] > 250) {
806 3 sti->pts_reorder_error[i] >>= 1;
807 3 sti->pts_reorder_error_count[i] >>= 1;
808 }
809 }
810 }
811 }
812 }
813
814
2/2
✓ Branch 0 taken 1385 times.
✓ Branch 1 taken 501791 times.
503176 if (dts == AV_NOPTS_VALUE)
815 1385 dts = pts_buffer[0];
816
817 503176 return dts;
818 }
819
820 /**
821 * Updates the dts of packets of a stream in pkt_buffer, by re-ordering the pts
822 * of the packets in a window.
823 */
824 5684 static void update_dts_from_pts(AVFormatContext *s, int stream_index,
825 PacketListEntry *pkt_buffer)
826 {
827 5684 AVStream *const st = s->streams[stream_index];
828 5684 int delay = ffstream(st)->avctx->has_b_frames;
829
830 int64_t pts_buffer[MAX_REORDER_DELAY+1];
831
832
2/2
✓ Branch 0 taken 96628 times.
✓ Branch 1 taken 5684 times.
102312 for (int i = 0; i < MAX_REORDER_DELAY + 1; i++)
833 96628 pts_buffer[i] = AV_NOPTS_VALUE;
834
835
2/2
✓ Branch 1 taken 5205 times.
✓ Branch 2 taken 5684 times.
10889 for (; pkt_buffer; pkt_buffer = get_next_pkt(s, st, pkt_buffer)) {
836
2/2
✓ Branch 0 taken 4547 times.
✓ Branch 1 taken 658 times.
5205 if (pkt_buffer->pkt.stream_index != stream_index)
837 4547 continue;
838
839
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) {
840 561 pts_buffer[0] = pkt_buffer->pkt.pts;
841
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++)
842 212 FFSWAP(int64_t, pts_buffer[i], pts_buffer[i + 1]);
843
844 561 pkt_buffer->pkt.dts = select_from_pts_buffer(st, pts_buffer, pkt_buffer->pkt.dts);
845 }
846 }
847 5684 }
848
849 543803 static void update_initial_timestamps(AVFormatContext *s, int stream_index,
850 int64_t dts, int64_t pts, AVPacket *pkt)
851 {
852 543803 FFFormatContext *const si = ffformatcontext(s);
853 543803 AVStream *const st = s->streams[stream_index];
854 543803 FFStream *const sti = ffstream(st);
855
2/2
✓ Branch 0 taken 171399 times.
✓ Branch 1 taken 372404 times.
543803 PacketListEntry *pktl = si->packet_buffer.head ? si->packet_buffer.head : si->parse_queue.head;
856
857 uint64_t shift;
858
859
4/4
✓ Branch 0 taken 183568 times.
✓ Branch 1 taken 360235 times.
✓ Branch 2 taken 5740 times.
✓ Branch 3 taken 177828 times.
543803 if (sti->first_dts != AV_NOPTS_VALUE ||
860 5740 dts == AV_NOPTS_VALUE ||
861
1/2
✓ Branch 0 taken 5740 times.
✗ Branch 1 not taken.
5740 sti->cur_dts == AV_NOPTS_VALUE ||
862
1/2
✓ Branch 0 taken 5740 times.
✗ Branch 1 not taken.
5740 sti->cur_dts < INT_MIN + RELATIVE_TS_BASE ||
863
2/4
✓ Branch 0 taken 5740 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5740 times.
11480 dts < INT_MIN + (sti->cur_dts - RELATIVE_TS_BASE) ||
864 5740 is_relative(dts))
865 538063 return;
866
867 5740 sti->first_dts = dts - (sti->cur_dts - RELATIVE_TS_BASE);
868 5740 sti->cur_dts = dts;
869 5740 shift = (uint64_t)sti->first_dts - RELATIVE_TS_BASE;
870
871
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5740 times.
5740 if (is_relative(pts))
872 pts += shift;
873
874
2/2
✓ Branch 1 taken 4940 times.
✓ Branch 2 taken 5740 times.
10680 for (PacketListEntry *pktl_it = pktl; pktl_it; pktl_it = get_next_pkt(s, st, pktl_it)) {
875
2/2
✓ Branch 0 taken 4470 times.
✓ Branch 1 taken 470 times.
4940 if (pktl_it->pkt.stream_index != stream_index)
876 4470 continue;
877
2/2
✓ Branch 1 taken 205 times.
✓ Branch 2 taken 265 times.
470 if (is_relative(pktl_it->pkt.pts))
878 205 pktl_it->pkt.pts += shift;
879
880
2/2
✓ Branch 1 taken 233 times.
✓ Branch 2 taken 237 times.
470 if (is_relative(pktl_it->pkt.dts))
881 233 pktl_it->pkt.dts += shift;
882
883
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) {
884 41 st->start_time = pktl_it->pkt.pts;
885
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)
886 st->start_time = av_sat_add64(st->start_time, av_rescale_q(sti->skip_samples, (AVRational){1, st->codecpar->sample_rate}, st->time_base));
887 }
888 }
889
890
2/2
✓ Branch 1 taken 5666 times.
✓ Branch 2 taken 74 times.
5740 if (has_decode_delay_been_guessed(st))
891 5666 update_dts_from_pts(s, stream_index, pktl);
892
893
2/2
✓ Branch 0 taken 1639 times.
✓ Branch 1 taken 4101 times.
5740 if (st->start_time == AV_NOPTS_VALUE) {
894
3/4
✓ Branch 0 taken 1183 times.
✓ Branch 1 taken 456 times.
✓ Branch 2 taken 1183 times.
✗ Branch 3 not taken.
1639 if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO || !(pkt->flags & AV_PKT_FLAG_DISCARD)) {
895 1639 st->start_time = pts;
896 }
897
4/4
✓ Branch 0 taken 456 times.
✓ Branch 1 taken 1183 times.
✓ Branch 2 taken 385 times.
✓ Branch 3 taken 71 times.
1639 if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && st->codecpar->sample_rate)
898 385 st->start_time = av_sat_add64(st->start_time, av_rescale_q(sti->skip_samples, (AVRational){1, st->codecpar->sample_rate}, st->time_base));
899 }
900 }
901
902 215665 static void update_initial_durations(AVFormatContext *s, AVStream *st,
903 int stream_index, int64_t duration)
904 {
905 215665 FFFormatContext *const si = ffformatcontext(s);
906 215665 FFStream *const sti = ffstream(st);
907
2/2
✓ Branch 0 taken 168606 times.
✓ Branch 1 taken 47059 times.
215665 PacketListEntry *pktl = si->packet_buffer.head ? si->packet_buffer.head : si->parse_queue.head;
908 215665 int64_t cur_dts = RELATIVE_TS_BASE;
909
910
2/2
✓ Branch 0 taken 112138 times.
✓ Branch 1 taken 103527 times.
215665 if (sti->first_dts != AV_NOPTS_VALUE) {
911
2/2
✓ Branch 0 taken 108847 times.
✓ Branch 1 taken 3291 times.
112138 if (sti->update_initial_durations_done)
912 108847 return;
913 3291 sti->update_initial_durations_done = 1;
914 3291 cur_dts = sti->first_dts;
915
1/2
✓ Branch 1 taken 5011 times.
✗ Branch 2 not taken.
5011 for (; pktl; pktl = get_next_pkt(s, st, pktl)) {
916
2/2
✓ Branch 0 taken 3306 times.
✓ Branch 1 taken 1705 times.
5011 if (pktl->pkt.stream_index == stream_index) {
917
2/2
✓ Branch 0 taken 3204 times.
✓ Branch 1 taken 102 times.
3306 if (pktl->pkt.pts != pktl->pkt.dts ||
918
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 3189 times.
3204 pktl->pkt.dts != AV_NOPTS_VALUE ||
919
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
15 pktl->pkt.duration)
920 break;
921 15 cur_dts -= duration;
922 }
923 }
924
3/4
✓ Branch 0 taken 3291 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
✓ Branch 3 taken 3258 times.
3291 if (pktl && pktl->pkt.dts != sti->first_dts) {
925 33 av_log(s, AV_LOG_DEBUG, "first_dts %s not matching first dts %s (pts %s, duration %"PRId64") in the queue\n",
926 33 av_ts2str(sti->first_dts), av_ts2str(pktl->pkt.dts), av_ts2str(pktl->pkt.pts), pktl->pkt.duration);
927 33 return;
928 }
929
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3258 times.
3258 if (!pktl) {
930 av_log(s, AV_LOG_DEBUG, "first_dts %s but no packet with dts in the queue\n", av_ts2str(sti->first_dts));
931 return;
932 }
933
2/2
✓ Branch 0 taken 3237 times.
✓ Branch 1 taken 21 times.
3258 pktl = si->packet_buffer.head ? si->packet_buffer.head : si->parse_queue.head;
934 3258 sti->first_dts = cur_dts;
935
2/2
✓ Branch 0 taken 79594 times.
✓ Branch 1 taken 23933 times.
103527 } else if (sti->cur_dts != RELATIVE_TS_BASE)
936 79594 return;
937
938
2/2
✓ Branch 1 taken 50426 times.
✓ Branch 2 taken 609 times.
51035 for (; pktl; pktl = get_next_pkt(s, st, pktl)) {
939
2/2
✓ Branch 0 taken 23694 times.
✓ Branch 1 taken 26732 times.
50426 if (pktl->pkt.stream_index != stream_index)
940 23694 continue;
941
2/2
✓ Branch 0 taken 558 times.
✓ Branch 1 taken 26174 times.
26732 if ((pktl->pkt.pts == pktl->pkt.dts ||
942
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 544 times.
558 pktl->pkt.pts == AV_NOPTS_VALUE) &&
943
2/2
✓ Branch 0 taken 3336 times.
✓ Branch 1 taken 22852 times.
26188 (pktl->pkt.dts == AV_NOPTS_VALUE ||
944
2/2
✓ Branch 0 taken 178 times.
✓ Branch 1 taken 3158 times.
3336 pktl->pkt.dts == sti->first_dts ||
945
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 176 times.
178 pktl->pkt.dts == RELATIVE_TS_BASE) &&
946
2/2
✓ Branch 0 taken 150 times.
✓ Branch 1 taken 25862 times.
26012 !pktl->pkt.duration &&
947
1/2
✓ Branch 1 taken 150 times.
✗ Branch 2 not taken.
150 av_sat_add64(cur_dts, duration) == cur_dts + (uint64_t)duration
948 ) {
949 150 pktl->pkt.dts = cur_dts;
950
2/2
✓ Branch 0 taken 134 times.
✓ Branch 1 taken 16 times.
150 if (!sti->avctx->has_b_frames)
951 134 pktl->pkt.pts = cur_dts;
952 150 pktl->pkt.duration = duration;
953 } else
954 break;
955 150 cur_dts = pktl->pkt.dts + pktl->pkt.duration;
956 }
957
2/2
✓ Branch 0 taken 609 times.
✓ Branch 1 taken 26582 times.
27191 if (!pktl)
958 609 sti->cur_dts = cur_dts;
959 }
960
961 550811 static void compute_pkt_fields(AVFormatContext *s, AVStream *st,
962 AVCodecParserContext *pc, AVPacket *pkt,
963 int64_t next_dts, int64_t next_pts)
964 {
965 550811 FFFormatContext *const si = ffformatcontext(s);
966 550811 FFStream *const sti = ffstream(st);
967 int num, den, presentation_delayed, delay;
968 int64_t offset;
969 AVRational duration;
970 1618974 int onein_oneout = st->codecpar->codec_id != AV_CODEC_ID_H264 &&
971
4/4
✓ Branch 0 taken 517352 times.
✓ Branch 1 taken 33459 times.
✓ Branch 2 taken 505068 times.
✓ Branch 3 taken 12284 times.
1055879 st->codecpar->codec_id != AV_CODEC_ID_HEVC &&
972
2/2
✓ Branch 0 taken 502115 times.
✓ Branch 1 taken 2953 times.
505068 st->codecpar->codec_id != AV_CODEC_ID_VVC;
973
974
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 550811 times.
550811 if (s->flags & AVFMT_FLAG_NOFILLIN)
975 return;
976
977
4/4
✓ Branch 0 taken 229487 times.
✓ Branch 1 taken 321324 times.
✓ Branch 2 taken 70588 times.
✓ Branch 3 taken 158899 times.
550811 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && pkt->dts != AV_NOPTS_VALUE) {
978
4/4
✓ Branch 0 taken 41505 times.
✓ Branch 1 taken 29083 times.
✓ Branch 2 taken 40389 times.
✓ Branch 3 taken 1116 times.
70588 if (pkt->dts == pkt->pts && sti->last_dts_for_order_check != AV_NOPTS_VALUE) {
979
2/2
✓ Branch 0 taken 40380 times.
✓ Branch 1 taken 9 times.
40389 if (sti->last_dts_for_order_check <= pkt->dts) {
980 40380 sti->dts_ordered++;
981 } else {
982
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,
983 "DTS %"PRIi64" < %"PRIi64" out of order\n",
984 pkt->dts,
985 sti->last_dts_for_order_check);
986 9 sti->dts_misordered++;
987 }
988
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 40360 times.
40389 if (sti->dts_ordered + sti->dts_misordered > 250) {
989 29 sti->dts_ordered >>= 1;
990 29 sti->dts_misordered >>= 1;
991 }
992 }
993
994 70588 sti->last_dts_for_order_check = pkt->dts;
995
4/4
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 70551 times.
✓ Branch 2 taken 20 times.
✓ Branch 3 taken 17 times.
70588 if (sti->dts_ordered < 8 * sti->dts_misordered && pkt->dts == pkt->pts)
996 20 pkt->dts = AV_NOPTS_VALUE;
997 }
998
999
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 550811 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
550811 if ((s->flags & AVFMT_FLAG_IGNDTS) && pkt->pts != AV_NOPTS_VALUE)
1000 pkt->dts = AV_NOPTS_VALUE;
1001
1002
4/4
✓ Branch 0 taken 182201 times.
✓ Branch 1 taken 368610 times.
✓ Branch 2 taken 27014 times.
✓ Branch 3 taken 155187 times.
550811 if (pc && pc->pict_type == AV_PICTURE_TYPE_B
1003
2/2
✓ Branch 0 taken 141 times.
✓ Branch 1 taken 26873 times.
27014 && !sti->avctx->has_b_frames)
1004 //FIXME Set low_delay = 0 when has_b_frames = 1
1005 141 sti->avctx->has_b_frames = 1;
1006
1007 /* do we have a video B-frame ? */
1008 550811 delay = sti->avctx->has_b_frames;
1009 550811 presentation_delayed = 0;
1010
1011 /* XXX: need has_b_frame, but cannot get it if the codec is
1012 * not initialized */
1013
4/4
✓ Branch 0 taken 45691 times.
✓ Branch 1 taken 505120 times.
✓ Branch 2 taken 41693 times.
✓ Branch 3 taken 3998 times.
550811 if (delay &&
1014
2/2
✓ Branch 0 taken 14679 times.
✓ Branch 1 taken 27014 times.
41693 pc && pc->pict_type != AV_PICTURE_TYPE_B)
1015 14679 presentation_delayed = 1;
1016
1017
4/4
✓ Branch 0 taken 322679 times.
✓ Branch 1 taken 228132 times.
✓ Branch 2 taken 160687 times.
✓ Branch 3 taken 161992 times.
550811 if (pkt->pts != AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE &&
1018
3/4
✓ Branch 0 taken 25951 times.
✓ Branch 1 taken 134736 times.
✓ Branch 2 taken 25951 times.
✗ Branch 3 not taken.
160687 st->pts_wrap_bits < 63 && pkt->dts > INT64_MIN + (1LL << st->pts_wrap_bits) &&
1019
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25951 times.
25951 pkt->dts - (1LL << (st->pts_wrap_bits - 1)) > pkt->pts) {
1020 if (is_relative(sti->cur_dts) || pkt->dts - (1LL<<(st->pts_wrap_bits - 1)) > sti->cur_dts) {
1021 pkt->dts -= 1LL << st->pts_wrap_bits;
1022 } else
1023 pkt->pts += 1LL << st->pts_wrap_bits;
1024 }
1025
1026 /* Some MPEG-2 in MPEG-PS lack dts (issue #171 / input_file.mpg).
1027 * We take the conservative approach and discard both.
1028 * Note: If this is misbehaving for an H.264 file, then possibly
1029 * presentation_delayed is not set correctly. */
1030
4/4
✓ Branch 0 taken 28474 times.
✓ Branch 1 taken 522337 times.
✓ Branch 2 taken 23288 times.
✓ Branch 3 taken 5186 times.
550811 if (delay == 1 && pkt->dts == pkt->pts &&
1031
4/4
✓ Branch 0 taken 6184 times.
✓ Branch 1 taken 17104 times.
✓ Branch 2 taken 656 times.
✓ Branch 3 taken 5528 times.
23288 pkt->dts != AV_NOPTS_VALUE && presentation_delayed) {
1032 656 av_log(s, AV_LOG_DEBUG, "invalid dts/pts combination %"PRIi64"\n", pkt->dts);
1033
2/2
✓ Branch 0 taken 321 times.
✓ Branch 1 taken 335 times.
656 if ( strcmp(s->iformat->name, "mov,mp4,m4a,3gp,3g2,mj2")
1034
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 271 times.
321 && strcmp(s->iformat->name, "flv")) // otherwise we discard correct timestamps for vc1-wmapro.ism
1035 50 pkt->dts = AV_NOPTS_VALUE;
1036 }
1037
1038 550811 duration = av_mul_q((AVRational) {pkt->duration, 1}, st->time_base);
1039
2/2
✓ Branch 0 taken 309115 times.
✓ Branch 1 taken 241696 times.
550811 if (pkt->duration <= 0) {
1040 309115 compute_frame_duration(s, &num, &den, st, pc, pkt);
1041
3/4
✓ Branch 0 taken 299161 times.
✓ Branch 1 taken 9954 times.
✓ Branch 2 taken 299161 times.
✗ Branch 3 not taken.
309115 if (den && num) {
1042 299161 duration = (AVRational) {num, den};
1043 299161 pkt->duration = av_rescale_rnd(1,
1044 299161 num * (int64_t) st->time_base.den,
1045 299161 den * (int64_t) st->time_base.num,
1046 AV_ROUND_DOWN);
1047 }
1048 }
1049
1050
6/6
✓ Branch 0 taken 538035 times.
✓ Branch 1 taken 12776 times.
✓ Branch 2 taken 369429 times.
✓ Branch 3 taken 168606 times.
✓ Branch 4 taken 47059 times.
✓ Branch 5 taken 322370 times.
550811 if (pkt->duration > 0 && (si->packet_buffer.head || si->parse_queue.head))
1051 215665 update_initial_durations(s, st, pkt->stream_index, pkt->duration);
1052
1053 /* Correct timestamps with byte offset if demuxers only have timestamps
1054 * on packet boundaries */
1055
5/6
✓ Branch 0 taken 182201 times.
✓ Branch 1 taken 368610 times.
✓ Branch 2 taken 960 times.
✓ Branch 3 taken 181241 times.
✓ Branch 4 taken 960 times.
✗ Branch 5 not taken.
550811 if (pc && sti->need_parsing == AVSTREAM_PARSE_TIMESTAMPS && pkt->size) {
1056 /* this will estimate bitrate based on this frame's duration and size */
1057 960 offset = av_rescale(pc->offset, pkt->duration, pkt->size);
1058
2/2
✓ Branch 0 taken 575 times.
✓ Branch 1 taken 385 times.
960 if (pkt->pts != AV_NOPTS_VALUE)
1059 575 pkt->pts += offset;
1060
2/2
✓ Branch 0 taken 121 times.
✓ Branch 1 taken 839 times.
960 if (pkt->dts != AV_NOPTS_VALUE)
1061 121 pkt->dts += offset;
1062 }
1063
1064 /* This may be redundant, but it should not hurt. */
1065
2/2
✓ Branch 0 taken 190129 times.
✓ Branch 1 taken 360682 times.
550811 if (pkt->dts != AV_NOPTS_VALUE &&
1066
2/2
✓ Branch 0 taken 160637 times.
✓ Branch 1 taken 29492 times.
190129 pkt->pts != AV_NOPTS_VALUE &&
1067
2/2
✓ Branch 0 taken 4877 times.
✓ Branch 1 taken 155760 times.
160637 pkt->pts > pkt->dts)
1068 4877 presentation_delayed = 1;
1069
1070
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 550811 times.
550811 if (s->debug & FF_FDEBUG_TS)
1071 av_log(s, AV_LOG_DEBUG,
1072 "IN delayed:%d pts:%s, dts:%s cur_dts:%s st:%d pc:%p duration:%"PRId64" delay:%d onein_oneout:%d\n",
1073 presentation_delayed, av_ts2str(pkt->pts), av_ts2str(pkt->dts), av_ts2str(sti->cur_dts),
1074 pkt->stream_index, pc, pkt->duration, delay, onein_oneout);
1075
1076 /* Interpolate PTS and DTS if they are not present. We skip H264
1077 * currently because delay and has_b_frames are not reliably set. */
1078
8/8
✓ Branch 0 taken 45691 times.
✓ Branch 1 taken 505120 times.
✓ Branch 2 taken 28474 times.
✓ Branch 3 taken 17217 times.
✓ Branch 4 taken 25020 times.
✓ Branch 5 taken 3454 times.
✓ Branch 6 taken 498674 times.
✓ Branch 7 taken 31466 times.
550811 if ((delay == 0 || (delay == 1 && pc)) &&
1079 onein_oneout) {
1080
2/2
✓ Branch 0 taken 5518 times.
✓ Branch 1 taken 493156 times.
498674 if (presentation_delayed) {
1081 /* DTS = decompression timestamp */
1082 /* PTS = presentation timestamp */
1083
2/2
✓ Branch 0 taken 2885 times.
✓ Branch 1 taken 2633 times.
5518 if (pkt->dts == AV_NOPTS_VALUE)
1084 2885 pkt->dts = sti->last_IP_pts;
1085 5518 update_initial_timestamps(s, pkt->stream_index, pkt->dts, pkt->pts, pkt);
1086
2/2
✓ Branch 0 taken 2517 times.
✓ Branch 1 taken 3001 times.
5518 if (pkt->dts == AV_NOPTS_VALUE)
1087 2517 pkt->dts = sti->cur_dts;
1088
1089 /* This is tricky: the dts must be incremented by the duration
1090 * of the frame we are displaying, i.e. the last I- or P-frame. */
1091
3/4
✓ Branch 0 taken 233 times.
✓ Branch 1 taken 5285 times.
✓ Branch 2 taken 233 times.
✗ Branch 3 not taken.
5518 if (sti->last_IP_duration == 0 && (uint64_t)pkt->duration <= INT32_MAX)
1092 233 sti->last_IP_duration = pkt->duration;
1093
2/2
✓ Branch 0 taken 5516 times.
✓ Branch 1 taken 2 times.
5518 if (pkt->dts != AV_NOPTS_VALUE)
1094 5516 sti->cur_dts = av_sat_add64(pkt->dts, sti->last_IP_duration);
1095
2/2
✓ Branch 0 taken 5516 times.
✓ Branch 1 taken 2 times.
5518 if (pkt->dts != AV_NOPTS_VALUE &&
1096
2/2
✓ Branch 0 taken 3239 times.
✓ Branch 1 taken 2277 times.
5516 pkt->pts == AV_NOPTS_VALUE &&
1097
2/2
✓ Branch 0 taken 3237 times.
✓ Branch 1 taken 2 times.
3239 sti->last_IP_duration > 0 &&
1098
4/4
✓ Branch 0 taken 739 times.
✓ Branch 1 taken 2498 times.
✓ Branch 2 taken 733 times.
✓ Branch 3 taken 6 times.
3237 ((uint64_t)sti->cur_dts - (uint64_t)next_dts + 1) <= 2 &&
1099
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 722 times.
733 next_dts != next_pts &&
1100 next_pts != AV_NOPTS_VALUE)
1101 11 pkt->pts = next_dts;
1102
1103
1/2
✓ Branch 0 taken 5518 times.
✗ Branch 1 not taken.
5518 if ((uint64_t)pkt->duration <= INT32_MAX)
1104 5518 sti->last_IP_duration = pkt->duration;
1105 5518 sti->last_IP_pts = pkt->pts;
1106 /* Cannot compute PTS if not present (we can compute it only
1107 * by knowing the future. */
1108
2/2
✓ Branch 0 taken 184114 times.
✓ Branch 1 taken 309042 times.
493156 } else if (pkt->pts != AV_NOPTS_VALUE ||
1109
2/2
✓ Branch 0 taken 155423 times.
✓ Branch 1 taken 28691 times.
184114 pkt->dts != AV_NOPTS_VALUE ||
1110
2/2
✓ Branch 0 taken 151856 times.
✓ Branch 1 taken 3567 times.
155423 pkt->duration > 0 ) {
1111
1112 /* presentation is not delayed : PTS and DTS are the same */
1113
2/2
✓ Branch 0 taken 180547 times.
✓ Branch 1 taken 309042 times.
489589 if (pkt->pts == AV_NOPTS_VALUE)
1114 180547 pkt->pts = pkt->dts;
1115 489589 update_initial_timestamps(s, pkt->stream_index, pkt->pts,
1116 pkt->pts, pkt);
1117
2/2
✓ Branch 0 taken 151856 times.
✓ Branch 1 taken 337733 times.
489589 if (pkt->pts == AV_NOPTS_VALUE)
1118 151856 pkt->pts = sti->cur_dts;
1119 489589 pkt->dts = pkt->pts;
1120
3/4
✓ Branch 0 taken 489589 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 489581 times.
✓ Branch 3 taken 8 times.
489589 if (pkt->pts != AV_NOPTS_VALUE && duration.num >= 0)
1121 489581 sti->cur_dts = av_add_stable(st->time_base, pkt->pts, duration, 1);
1122 }
1123 }
1124
1125
3/4
✓ Branch 0 taken 503237 times.
✓ Branch 1 taken 47574 times.
✓ Branch 2 taken 503237 times.
✗ Branch 3 not taken.
550811 if (pkt->pts != AV_NOPTS_VALUE && delay <= MAX_REORDER_DELAY) {
1126 503237 sti->pts_buffer[0] = pkt->pts;
1127
4/4
✓ Branch 0 taken 21542 times.
✓ Branch 1 taken 498130 times.
✓ Branch 2 taken 16435 times.
✓ Branch 3 taken 5107 times.
519672 for (int i = 0; i < delay && sti->pts_buffer[i] > sti->pts_buffer[i + 1]; i++)
1128 16435 FFSWAP(int64_t, sti->pts_buffer[i], sti->pts_buffer[i + 1]);
1129
1130
2/2
✓ Branch 1 taken 502615 times.
✓ Branch 2 taken 622 times.
503237 if (has_decode_delay_been_guessed(st))
1131 502615 pkt->dts = select_from_pts_buffer(st, sti->pts_buffer, pkt->dts);
1132 }
1133 // We skipped it above so we try here.
1134
2/2
✓ Branch 0 taken 48696 times.
✓ Branch 1 taken 502115 times.
550811 if (!onein_oneout)
1135 // This should happen on the first packet
1136 48696 update_initial_timestamps(s, pkt->stream_index, pkt->dts, pkt->pts, pkt);
1137
2/2
✓ Branch 0 taken 7464 times.
✓ Branch 1 taken 543347 times.
550811 if (pkt->dts > sti->cur_dts)
1138 7464 sti->cur_dts = pkt->dts;
1139
1140
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 550811 times.
550811 if (s->debug & FF_FDEBUG_TS)
1141 av_log(s, AV_LOG_DEBUG, "OUTdelayed:%d/%d pts:%s, dts:%s cur_dts:%s st:%d (%d)\n",
1142 presentation_delayed, delay, av_ts2str(pkt->pts), av_ts2str(pkt->dts), av_ts2str(sti->cur_dts), st->index, st->id);
1143
1144 /* update flags */
1145
4/4
✓ Branch 0 taken 550726 times.
✓ Branch 1 taken 85 times.
✓ Branch 3 taken 420586 times.
✓ Branch 4 taken 130140 times.
550811 if (st->codecpar->codec_type == AVMEDIA_TYPE_DATA || ff_is_intra_only(st->codecpar->codec_id))
1146 420671 pkt->flags |= AV_PKT_FLAG_KEY;
1147 }
1148
1149 /**
1150 * Parse a packet, add all split parts to parse_queue.
1151 *
1152 * @param pkt Packet to parse; must not be NULL.
1153 * @param flush Indicates whether to flush. If set, pkt must be blank.
1154 */
1155 689134 static int parse_packet(AVFormatContext *s, AVPacket *pkt,
1156 int stream_index, int flush)
1157 {
1158 689134 FFFormatContext *const si = ffformatcontext(s);
1159 689134 AVPacket *out_pkt = si->parse_pkt;
1160 689134 AVStream *st = s->streams[stream_index];
1161 689134 FFStream *const sti = ffstream(st);
1162 689134 const uint8_t *data = pkt->data;
1163 689134 int size = pkt->size;
1164 689134 int ret = 0, got_output = flush;
1165
1166
3/6
✓ Branch 0 taken 1980 times.
✓ Branch 1 taken 687154 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1980 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
689134 if (!size && !flush && sti->parser->flags & PARSER_FLAG_COMPLETE_FRAMES) {
1167 // preserve 0-size sync packets
1168 compute_pkt_fields(s, st, sti->parser, pkt, AV_NOPTS_VALUE, AV_NOPTS_VALUE);
1169 }
1170
1171
6/6
✓ Branch 0 taken 802522 times.
✓ Branch 1 taken 692252 times.
✓ Branch 2 taken 5098 times.
✓ Branch 3 taken 687154 times.
✓ Branch 4 taken 3118 times.
✓ Branch 5 taken 1980 times.
1494774 while (size > 0 || (flush && got_output)) {
1172 805640 int64_t next_pts = pkt->pts;
1173 805640 int64_t next_dts = pkt->dts;
1174 int len;
1175
1176 805640 len = av_parser_parse2(sti->parser, sti->avctx,
1177 &out_pkt->data, &out_pkt->size, data, size,
1178 pkt->pts, pkt->dts, pkt->pos);
1179
1180 805640 pkt->pts = pkt->dts = AV_NOPTS_VALUE;
1181 805640 pkt->pos = -1;
1182 /* increment read pointer */
1183 av_assert1(data || !len);
1184
2/2
✓ Branch 0 taken 792866 times.
✓ Branch 1 taken 12774 times.
805640 data = len ? data + len : data;
1185 805640 size -= len;
1186
1187 805640 got_output = !!out_pkt->size;
1188
1189
2/2
✓ Branch 0 taken 623439 times.
✓ Branch 1 taken 182201 times.
805640 if (!out_pkt->size)
1190 623439 continue;
1191
1192
4/4
✓ Branch 0 taken 181063 times.
✓ Branch 1 taken 1138 times.
✓ Branch 2 taken 65332 times.
✓ Branch 3 taken 115731 times.
182201 if (pkt->buf && out_pkt->data == pkt->data) {
1193 /* reference pkt->buf only when out_pkt->data is guaranteed to point
1194 * to data in it and not in the parser's internal buffer. */
1195 /* XXX: Ensure this is the case with all parsers when sti->parser->flags
1196 * is PARSER_FLAG_COMPLETE_FRAMES and check for that instead? */
1197 65332 out_pkt->buf = av_buffer_ref(pkt->buf);
1198
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 65332 times.
65332 if (!out_pkt->buf) {
1199 ret = AVERROR(ENOMEM);
1200 goto fail;
1201 }
1202 } else {
1203 116869 ret = av_packet_make_refcounted(out_pkt);
1204
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 116869 times.
116869 if (ret < 0)
1205 goto fail;
1206 }
1207
1208
2/2
✓ Branch 0 taken 9152 times.
✓ Branch 1 taken 173049 times.
182201 if (pkt->side_data) {
1209 9152 out_pkt->side_data = pkt->side_data;
1210 9152 out_pkt->side_data_elems = pkt->side_data_elems;
1211 9152 pkt->side_data = NULL;
1212 9152 pkt->side_data_elems = 0;
1213 }
1214
1215 /* set the duration */
1216
2/2
✓ Branch 0 taken 45123 times.
✓ Branch 1 taken 137078 times.
182201 out_pkt->duration = (sti->parser->flags & PARSER_FLAG_COMPLETE_FRAMES) ? pkt->duration : 0;
1217
2/2
✓ Branch 0 taken 107843 times.
✓ Branch 1 taken 74358 times.
182201 if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
1218
2/2
✓ Branch 0 taken 107737 times.
✓ Branch 1 taken 106 times.
107843 if (sti->avctx->sample_rate > 0) {
1219 107737 out_pkt->duration =
1220 107737 av_rescale_q_rnd(sti->parser->duration,
1221 107737 (AVRational) { 1, sti->avctx->sample_rate },
1222 st->time_base,
1223 AV_ROUND_DOWN);
1224 }
1225
2/2
✓ Branch 0 taken 1822 times.
✓ Branch 1 taken 72536 times.
74358 } else if (st->codecpar->codec_id == AV_CODEC_ID_GIF) {
1226
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 &&
1227
1/2
✓ Branch 0 taken 1822 times.
✗ Branch 1 not taken.
1822 sti->parser->duration) {
1228 1822 out_pkt->duration = sti->parser->duration;
1229 }
1230 }
1231
1232 182201 out_pkt->stream_index = st->index;
1233 182201 out_pkt->pts = sti->parser->pts;
1234 182201 out_pkt->dts = sti->parser->dts;
1235 182201 out_pkt->pos = sti->parser->pos;
1236 182201 out_pkt->flags |= pkt->flags & (AV_PKT_FLAG_DISCARD | AV_PKT_FLAG_CORRUPT);
1237
1238
2/2
✓ Branch 0 taken 114254 times.
✓ Branch 1 taken 67947 times.
182201 if (sti->need_parsing == AVSTREAM_PARSE_FULL_RAW)
1239 114254 out_pkt->pos = sti->parser->frame_offset;
1240
1241
2/2
✓ Branch 0 taken 176306 times.
✓ Branch 1 taken 5895 times.
182201 if (sti->parser->key_frame == 1 ||
1242
2/2
✓ Branch 0 taken 97999 times.
✓ Branch 1 taken 78307 times.
176306 (sti->parser->key_frame == -1 &&
1243
2/2
✓ Branch 0 taken 86312 times.
✓ Branch 1 taken 11687 times.
97999 sti->parser->pict_type == AV_PICTURE_TYPE_I))
1244 92207 out_pkt->flags |= AV_PKT_FLAG_KEY;
1245
1246
6/6
✓ Branch 0 taken 97999 times.
✓ Branch 1 taken 84202 times.
✓ Branch 2 taken 315 times.
✓ Branch 3 taken 97684 times.
✓ Branch 4 taken 286 times.
✓ Branch 5 taken 29 times.
182201 if (sti->parser->key_frame == -1 && sti->parser->pict_type ==AV_PICTURE_TYPE_NONE && (pkt->flags&AV_PKT_FLAG_KEY))
1247 286 out_pkt->flags |= AV_PKT_FLAG_KEY;
1248
1249 182201 compute_pkt_fields(s, st, sti->parser, out_pkt, next_dts, next_pts);
1250
1251 182201 ret = avpriv_packet_list_put(&si->parse_queue,
1252 out_pkt, NULL, 0);
1253
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 182201 times.
182201 if (ret < 0)
1254 goto fail;
1255 }
1256
1257 /* end of the stream => close and free the parser */
1258
2/2
✓ Branch 0 taken 687154 times.
✓ Branch 1 taken 1980 times.
689134 if (flush) {
1259 1980 av_parser_close(sti->parser);
1260 1980 sti->parser = NULL;
1261 }
1262
1263 687154 fail:
1264
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 689134 times.
689134 if (ret < 0)
1265 av_packet_unref(out_pkt);
1266 689134 av_packet_unref(pkt);
1267 689134 return ret;
1268 }
1269
1270 8774 static int64_t ts_to_samples(AVStream *st, int64_t ts)
1271 {
1272 8774 return av_rescale(ts, st->time_base.num * st->codecpar->sample_rate, st->time_base.den);
1273 }
1274
1275 7893 static int codec_close(FFStream *sti)
1276 {
1277 7893 AVCodecContext *avctx_new = NULL;
1278 7893 AVCodecParameters *par_tmp = NULL;
1279 int ret;
1280
1281 7893 avctx_new = avcodec_alloc_context3(sti->avctx->codec);
1282
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7893 times.
7893 if (!avctx_new) {
1283 ret = AVERROR(ENOMEM);
1284 goto fail;
1285 }
1286
1287 7893 par_tmp = avcodec_parameters_alloc();
1288
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7893 times.
7893 if (!par_tmp) {
1289 ret = AVERROR(ENOMEM);
1290 goto fail;
1291 }
1292
1293 7893 ret = avcodec_parameters_from_context(par_tmp, sti->avctx);
1294
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7893 times.
7893 if (ret < 0)
1295 goto fail;
1296
1297 7893 ret = avcodec_parameters_to_context(avctx_new, par_tmp);
1298
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7893 times.
7893 if (ret < 0)
1299 goto fail;
1300
1301 7893 avctx_new->pkt_timebase = sti->avctx->pkt_timebase;
1302
1303 #if FF_API_TICKS_PER_FRAME
1304 FF_DISABLE_DEPRECATION_WARNINGS
1305 7893 avctx_new->ticks_per_frame = sti->avctx->ticks_per_frame;
1306 FF_ENABLE_DEPRECATION_WARNINGS
1307 #endif
1308
1309 7893 avcodec_free_context(&sti->avctx);
1310 7893 sti->avctx = avctx_new;
1311
1312 7893 avctx_new = NULL;
1313 7893 ret = 0;
1314
1315 7893 fail:
1316 7893 avcodec_free_context(&avctx_new);
1317 7893 avcodec_parameters_free(&par_tmp);
1318
1319 7893 return ret;
1320 }
1321
1322 static int extract_extradata(FFFormatContext *si, AVStream *st, const AVPacket *pkt);
1323
1324 555142 static int read_frame_internal(AVFormatContext *s, AVPacket *pkt)
1325 {
1326 555142 FFFormatContext *const si = ffformatcontext(s);
1327 555142 int ret, got_packet = 0;
1328 555142 AVDictionary *metadata = NULL;
1329
1330
4/4
✓ Branch 0 taken 1242427 times.
✓ Branch 1 taken 368610 times.
✓ Branch 2 taken 1062172 times.
✓ Branch 3 taken 180255 times.
1611037 while (!got_packet && !si->parse_queue.head) {
1331 AVStream *st;
1332 FFStream *sti;
1333
1334 /* read next packet */
1335 1062172 ret = ff_read_packet(s, pkt);
1336
2/2
✓ Branch 0 taken 6277 times.
✓ Branch 1 taken 1055895 times.
1062172 if (ret < 0) {
1337
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6276 times.
6277 if (ret == AVERROR(EAGAIN))
1338 1 return ret;
1339 /* flush the parsers */
1340
2/2
✓ Branch 0 taken 7225 times.
✓ Branch 1 taken 6276 times.
13501 for (unsigned i = 0; i < s->nb_streams; i++) {
1341 7225 AVStream *const st = s->streams[i];
1342 7225 FFStream *const sti = ffstream(st);
1343
4/4
✓ Branch 0 taken 2604 times.
✓ Branch 1 taken 4621 times.
✓ Branch 2 taken 1980 times.
✓ Branch 3 taken 624 times.
7225 if (sti->parser && sti->need_parsing)
1344 1980 parse_packet(s, pkt, st->index, 1);
1345 }
1346 /* all remaining packets are now in parse_queue =>
1347 * really terminate parsing */
1348 6276 break;
1349 }
1350 1055895 ret = 0;
1351 1055895 st = s->streams[pkt->stream_index];
1352 1055895 sti = ffstream(st);
1353
1354 1055895 st->event_flags |= AVSTREAM_EVENT_FLAG_NEW_PACKETS;
1355
1356 /* update context if required */
1357
2/2
✓ Branch 0 taken 164 times.
✓ Branch 1 taken 1055731 times.
1055895 if (sti->need_context_update) {
1358
2/2
✓ Branch 1 taken 13 times.
✓ Branch 2 taken 151 times.
164 if (avcodec_is_open(sti->avctx)) {
1359 13 av_log(s, AV_LOG_DEBUG, "Demuxer context update while decoder is open, closing and trying to re-open\n");
1360 13 ret = codec_close(sti);
1361 13 sti->info->found_decoder = 0;
1362
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 if (ret < 0)
1363 return ret;
1364 }
1365
1366 /* close parser, because it depends on the codec */
1367
4/4
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 151 times.
✓ Branch 2 taken 10 times.
✓ Branch 3 taken 3 times.
164 if (sti->parser && sti->avctx->codec_id != st->codecpar->codec_id) {
1368 10 av_parser_close(sti->parser);
1369 10 sti->parser = NULL;
1370 }
1371
1372 164 ret = avcodec_parameters_to_context(sti->avctx, st->codecpar);
1373
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 164 times.
164 if (ret < 0) {
1374 av_packet_unref(pkt);
1375 return ret;
1376 }
1377
1378
2/2
✓ Branch 0 taken 144 times.
✓ Branch 1 taken 20 times.
164 if (!sti->avctx->extradata) {
1379 144 sti->extract_extradata.inited = 0;
1380
1381 144 ret = extract_extradata(si, st, pkt);
1382
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 144 times.
144 if (ret < 0) {
1383 av_packet_unref(pkt);
1384 return ret;
1385 }
1386 }
1387
1388 164 sti->codec_desc = avcodec_descriptor_get(sti->avctx->codec_id);
1389
1390 164 sti->need_context_update = 0;
1391 }
1392
1393
2/2
✓ Branch 0 taken 320078 times.
✓ Branch 1 taken 735817 times.
1055895 if (pkt->pts != AV_NOPTS_VALUE &&
1394
2/2
✓ Branch 0 taken 161705 times.
✓ Branch 1 taken 158373 times.
320078 pkt->dts != AV_NOPTS_VALUE &&
1395
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 161705 times.
161705 pkt->pts < pkt->dts) {
1396 av_log(s, AV_LOG_WARNING,
1397 "Invalid timestamps stream=%d, pts=%s, dts=%s, size=%d\n",
1398 pkt->stream_index,
1399 av_ts2str(pkt->pts),
1400 av_ts2str(pkt->dts),
1401 pkt->size);
1402 }
1403
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1055895 times.
1055895 if (s->debug & FF_FDEBUG_TS)
1404 av_log(s, AV_LOG_DEBUG,
1405 "ff_read_packet stream=%d, pts=%s, dts=%s, size=%d, duration=%"PRId64", flags=%d\n",
1406 pkt->stream_index,
1407 av_ts2str(pkt->pts),
1408 av_ts2str(pkt->dts),
1409 pkt->size, pkt->duration, pkt->flags);
1410
1411
5/6
✓ Branch 0 taken 688469 times.
✓ Branch 1 taken 367426 times.
✓ Branch 2 taken 2754 times.
✓ Branch 3 taken 685715 times.
✓ Branch 4 taken 2754 times.
✗ Branch 5 not taken.
1055895 if (sti->need_parsing && !sti->parser && !(s->flags & AVFMT_FLAG_NOPARSE)) {
1412 2754 sti->parser = av_parser_init(st->codecpar->codec_id);
1413
2/2
✓ Branch 0 taken 1184 times.
✓ Branch 1 taken 1570 times.
2754 if (!sti->parser) {
1414 1184 av_log(s, AV_LOG_VERBOSE, "parser not found for codec "
1415 "%s, packets or times may be invalid.\n",
1416 1184 avcodec_get_name(st->codecpar->codec_id));
1417 /* no parser available: just output the raw packets */
1418 1184 sti->need_parsing = AVSTREAM_PARSE_NONE;
1419
2/2
✓ Branch 0 taken 678 times.
✓ Branch 1 taken 892 times.
1570 } else if (sti->need_parsing == AVSTREAM_PARSE_HEADERS)
1420 678 sti->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES;
1421
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 892 times.
892 else if (sti->need_parsing == AVSTREAM_PARSE_FULL_ONCE)
1422 sti->parser->flags |= PARSER_FLAG_ONCE;
1423
2/2
✓ Branch 0 taken 471 times.
✓ Branch 1 taken 421 times.
892 else if (sti->need_parsing == AVSTREAM_PARSE_FULL_RAW)
1424 471 sti->parser->flags |= PARSER_FLAG_USE_CODEC_TS;
1425 }
1426
1427
3/4
✓ Branch 0 taken 687285 times.
✓ Branch 1 taken 368610 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 687285 times.
1055895 if (!sti->need_parsing || !sti->parser) {
1428 /* no parsing needed: we just output the packet as is */
1429 368610 compute_pkt_fields(s, st, NULL, pkt, AV_NOPTS_VALUE, AV_NOPTS_VALUE);
1430
2/2
✓ Branch 0 taken 93238 times.
✓ Branch 1 taken 275372 times.
368610 if ((s->iformat->flags & AVFMT_GENERIC_INDEX) &&
1431
4/4
✓ Branch 0 taken 92529 times.
✓ Branch 1 taken 709 times.
✓ Branch 2 taken 91201 times.
✓ Branch 3 taken 1328 times.
93238 (pkt->flags & AV_PKT_FLAG_KEY) && pkt->dts != AV_NOPTS_VALUE) {
1432 91201 ff_reduce_index(s, st->index);
1433 91201 av_add_index_entry(st, pkt->pos, pkt->dts,
1434 0, 0, AVINDEX_KEYFRAME);
1435 }
1436 368610 got_packet = 1;
1437
2/2
✓ Branch 0 taken 687154 times.
✓ Branch 1 taken 131 times.
687285 } else if (st->discard < AVDISCARD_ALL) {
1438
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 687154 times.
687154 if ((ret = parse_packet(s, pkt, pkt->stream_index, 0)) < 0)
1439 return ret;
1440 687154 st->codecpar->sample_rate = sti->avctx->sample_rate;
1441 687154 st->codecpar->bit_rate = sti->avctx->bit_rate;
1442 687154 ret = av_channel_layout_copy(&st->codecpar->ch_layout, &sti->avctx->ch_layout);
1443
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 687154 times.
687154 if (ret < 0)
1444 return ret;
1445 687154 st->codecpar->codec_id = sti->avctx->codec_id;
1446 } else {
1447 /* free packet */
1448 131 av_packet_unref(pkt);
1449 }
1450
2/2
✓ Branch 0 taken 344042 times.
✓ Branch 1 taken 711853 times.
1055895 if (pkt->flags & AV_PKT_FLAG_KEY)
1451 344042 sti->skip_to_keyframe = 0;
1452
2/2
✓ Branch 0 taken 85 times.
✓ Branch 1 taken 1055810 times.
1055895 if (sti->skip_to_keyframe) {
1453 85 av_packet_unref(pkt);
1454 85 got_packet = 0;
1455 }
1456 }
1457
1458
4/4
✓ Branch 0 taken 186531 times.
✓ Branch 1 taken 368610 times.
✓ Branch 2 taken 181141 times.
✓ Branch 3 taken 5390 times.
555141 if (!got_packet && si->parse_queue.head)
1459 181141 ret = avpriv_packet_list_get(&si->parse_queue, pkt);
1460
1461
2/2
✓ Branch 0 taken 549751 times.
✓ Branch 1 taken 5390 times.
555141 if (ret >= 0) {
1462 549751 AVStream *const st = s->streams[pkt->stream_index];
1463 549751 FFStream *const sti = ffstream(st);
1464 549751 int discard_padding = 0;
1465
3/4
✓ Branch 0 taken 4387 times.
✓ Branch 1 taken 545364 times.
✓ Branch 2 taken 4387 times.
✗ Branch 3 not taken.
549751 if (sti->first_discard_sample && pkt->pts != AV_NOPTS_VALUE) {
1466
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);
1467 4387 int64_t sample = ts_to_samples(st, pts);
1468 4387 int64_t duration = ts_to_samples(st, pkt->duration);
1469 4387 int64_t end_sample = sample + duration;
1470
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 &&
1471
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 sample < sti->last_discard_sample)
1472 14 discard_padding = FFMIN(end_sample - sti->first_discard_sample, duration);
1473 }
1474
6/6
✓ Branch 0 taken 4387 times.
✓ Branch 1 taken 545364 times.
✓ Branch 2 taken 4371 times.
✓ Branch 3 taken 16 times.
✓ Branch 4 taken 20 times.
✓ Branch 5 taken 4351 times.
549751 if (sti->start_skip_samples && (pkt->pts == 0 || pkt->pts == RELATIVE_TS_BASE))
1475 36 sti->skip_samples = sti->start_skip_samples;
1476 549751 sti->skip_samples = FFMAX(0, sti->skip_samples);
1477
4/4
✓ Branch 0 taken 549665 times.
✓ Branch 1 taken 86 times.
✓ Branch 2 taken 14 times.
✓ Branch 3 taken 549651 times.
549751 if (sti->skip_samples || discard_padding) {
1478 100 uint8_t *p = av_packet_new_side_data(pkt, AV_PKT_DATA_SKIP_SAMPLES, 10);
1479
1/2
✓ Branch 0 taken 100 times.
✗ Branch 1 not taken.
100 if (p) {
1480 100 AV_WL32(p, sti->skip_samples);
1481 100 AV_WL32(p + 4, discard_padding);
1482 100 av_log(s, AV_LOG_DEBUG, "demuxer injecting skip %u / discard %u\n",
1483 100 (unsigned)sti->skip_samples, (unsigned)discard_padding);
1484 }
1485 100 sti->skip_samples = 0;
1486 }
1487
1488 #if FF_API_AVSTREAM_SIDE_DATA
1489
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 549751 times.
549751 if (sti->inject_global_side_data) {
1490 for (int i = 0; i < st->codecpar->nb_coded_side_data; i++) {
1491 const AVPacketSideData *const src_sd = &st->codecpar->coded_side_data[i];
1492 uint8_t *dst_data;
1493
1494 if (av_packet_get_side_data(pkt, src_sd->type, NULL))
1495 continue;
1496
1497 dst_data = av_packet_new_side_data(pkt, src_sd->type, src_sd->size);
1498 if (!dst_data) {
1499 av_log(s, AV_LOG_WARNING, "Could not inject global side data\n");
1500 continue;
1501 }
1502
1503 memcpy(dst_data, src_sd->data, src_sd->size);
1504 }
1505 sti->inject_global_side_data = 0;
1506 }
1507 #endif
1508 }
1509
1510
2/2
✓ Branch 0 taken 7156 times.
✓ Branch 1 taken 547985 times.
555141 if (!si->metafree) {
1511 7156 int metaret = av_opt_get_dict_val(s, "metadata", AV_OPT_SEARCH_CHILDREN, &metadata);
1512
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7156 times.
7156 if (metadata) {
1513 s->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
1514 av_dict_copy(&s->metadata, metadata, 0);
1515 av_dict_free(&metadata);
1516 av_opt_set_dict_val(s, "metadata", NULL, AV_OPT_SEARCH_CHILDREN);
1517 }
1518 7156 si->metafree = metaret == AVERROR_OPTION_NOT_FOUND;
1519 }
1520
1521
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 555141 times.
555141 if (s->debug & FF_FDEBUG_TS)
1522 av_log(s, AV_LOG_DEBUG,
1523 "read_frame_internal stream=%d, pts=%s, dts=%s, "
1524 "size=%d, duration=%"PRId64", flags=%d\n",
1525 pkt->stream_index,
1526 av_ts2str(pkt->pts),
1527 av_ts2str(pkt->dts),
1528 pkt->size, pkt->duration, pkt->flags);
1529
1530 /* A demuxer might have returned EOF because of an IO error, let's
1531 * propagate this back to the user. */
1532
5/8
✓ Branch 0 taken 5281 times.
✓ Branch 1 taken 549860 times.
✓ Branch 2 taken 5067 times.
✓ Branch 3 taken 214 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 5067 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
555141 if (ret == AVERROR_EOF && s->pb && s->pb->error < 0 && s->pb->error != AVERROR(EAGAIN))
1533 ret = s->pb->error;
1534
1535 555141 return ret;
1536 }
1537
1538 495266 int av_read_frame(AVFormatContext *s, AVPacket *pkt)
1539 {
1540 495266 FFFormatContext *const si = ffformatcontext(s);
1541 495266 const int genpts = s->flags & AVFMT_FLAG_GENPTS;
1542 495266 int eof = 0;
1543 int ret;
1544 AVStream *st;
1545
1546
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 495266 times.
495266 if (!genpts) {
1547 990532 ret = si->packet_buffer.head
1548 122843 ? avpriv_packet_list_get(&si->packet_buffer, pkt)
1549
2/2
✓ Branch 0 taken 122843 times.
✓ Branch 1 taken 372423 times.
495266 : read_frame_internal(s, pkt);
1550
2/2
✓ Branch 0 taken 4233 times.
✓ Branch 1 taken 491033 times.
495266 if (ret < 0)
1551 4233 return ret;
1552 491033 goto return_packet;
1553 }
1554
1555 for (;;) {
1556 PacketListEntry *pktl = si->packet_buffer.head;
1557
1558 if (pktl) {
1559 AVPacket *next_pkt = &pktl->pkt;
1560
1561 if (next_pkt->dts != AV_NOPTS_VALUE) {
1562 int wrap_bits = s->streams[next_pkt->stream_index]->pts_wrap_bits;
1563 // last dts seen for this stream. if any of packets following
1564 // current one had no dts, we will set this to AV_NOPTS_VALUE.
1565 int64_t last_dts = next_pkt->dts;
1566 av_assert2(wrap_bits <= 64);
1567 while (pktl && next_pkt->pts == AV_NOPTS_VALUE) {
1568 if (pktl->pkt.stream_index == next_pkt->stream_index &&
1569 av_compare_mod(next_pkt->dts, pktl->pkt.dts, 2ULL << (wrap_bits - 1)) < 0) {
1570 if (av_compare_mod(pktl->pkt.pts, pktl->pkt.dts, 2ULL << (wrap_bits - 1))) {
1571 // not B-frame
1572 next_pkt->pts = pktl->pkt.dts;
1573 }
1574 if (last_dts != AV_NOPTS_VALUE) {
1575 // Once last dts was set to AV_NOPTS_VALUE, we don't change it.
1576 last_dts = pktl->pkt.dts;
1577 }
1578 }
1579 pktl = pktl->next;
1580 }
1581 if (eof && next_pkt->pts == AV_NOPTS_VALUE && last_dts != AV_NOPTS_VALUE) {
1582 // Fixing the last reference frame had none pts issue (For MXF etc).
1583 // We only do this when
1584 // 1. eof.
1585 // 2. we are not able to resolve a pts value for current packet.
1586 // 3. the packets for this stream at the end of the files had valid dts.
1587 next_pkt->pts = last_dts + next_pkt->duration;
1588 }
1589 pktl = si->packet_buffer.head;
1590 }
1591
1592 /* read packet from packet buffer, if there is data */
1593 st = s->streams[next_pkt->stream_index];
1594 if (!(next_pkt->pts == AV_NOPTS_VALUE && st->discard < AVDISCARD_ALL &&
1595 next_pkt->dts != AV_NOPTS_VALUE && !eof)) {
1596 ret = avpriv_packet_list_get(&si->packet_buffer, pkt);
1597 goto return_packet;
1598 }
1599 }
1600
1601 ret = read_frame_internal(s, pkt);
1602 if (ret < 0) {
1603 if (pktl && ret != AVERROR(EAGAIN)) {
1604 eof = 1;
1605 continue;
1606 } else
1607 return ret;
1608 }
1609
1610 ret = avpriv_packet_list_put(&si->packet_buffer,
1611 pkt, NULL, 0);
1612 if (ret < 0) {
1613 av_packet_unref(pkt);
1614 return ret;
1615 }
1616 }
1617
1618 491033 return_packet:
1619 491033 st = s->streams[pkt->stream_index];
1620
4/4
✓ Branch 0 taken 192628 times.
✓ Branch 1 taken 298405 times.
✓ Branch 2 taken 119734 times.
✓ Branch 3 taken 72894 times.
491033 if ((s->iformat->flags & AVFMT_GENERIC_INDEX) && pkt->flags & AV_PKT_FLAG_KEY) {
1621 119734 ff_reduce_index(s, st->index);
1622 119734 av_add_index_entry(st, pkt->pos, pkt->dts, 0, 0, AVINDEX_KEYFRAME);
1623 }
1624
1625
2/2
✓ Branch 1 taken 119286 times.
✓ Branch 2 taken 371747 times.
491033 if (is_relative(pkt->dts))
1626 119286 pkt->dts -= RELATIVE_TS_BASE;
1627
2/2
✓ Branch 1 taken 117592 times.
✓ Branch 2 taken 373441 times.
491033 if (is_relative(pkt->pts))
1628 117592 pkt->pts -= RELATIVE_TS_BASE;
1629
1630 491033 return ret;
1631 }
1632
1633 /**
1634 * Return TRUE if the stream has accurate duration in any stream.
1635 *
1636 * @return TRUE if the stream has accurate duration for at least one component.
1637 */
1638 7078 static int has_duration(AVFormatContext *ic)
1639 {
1640
2/2
✓ Branch 0 taken 7322 times.
✓ Branch 1 taken 2361 times.
9683 for (unsigned i = 0; i < ic->nb_streams; i++) {
1641 7322 const AVStream *const st = ic->streams[i];
1642
2/2
✓ Branch 0 taken 4717 times.
✓ Branch 1 taken 2605 times.
7322 if (st->duration != AV_NOPTS_VALUE)
1643 4717 return 1;
1644 }
1645
2/2
✓ Branch 0 taken 433 times.
✓ Branch 1 taken 1928 times.
2361 if (ic->duration != AV_NOPTS_VALUE)
1646 433 return 1;
1647 1928 return 0;
1648 }
1649
1650 /**
1651 * Estimate the stream timings from the one of each components.
1652 *
1653 * Also computes the global bitrate if possible.
1654 */
1655 12364 static void update_stream_timings(AVFormatContext *ic)
1656 {
1657 int64_t start_time, start_time1, start_time_text, end_time, end_time1, end_time_text;
1658 int64_t duration, duration1, duration_text, filesize;
1659
1660 12364 start_time = INT64_MAX;
1661 12364 start_time_text = INT64_MAX;
1662 12364 end_time = INT64_MIN;
1663 12364 end_time_text = INT64_MIN;
1664 12364 duration = INT64_MIN;
1665 12364 duration_text = INT64_MIN;
1666
1667
2/2
✓ Branch 0 taken 13706 times.
✓ Branch 1 taken 12364 times.
26070 for (unsigned i = 0; i < ic->nb_streams; i++) {
1668 13706 AVStream *const st = ic->streams[i];
1669
2/2
✓ Branch 0 taken 13581 times.
✓ Branch 1 taken 125 times.
27287 int is_text = st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE ||
1670
2/2
✓ Branch 0 taken 143 times.
✓ Branch 1 taken 13438 times.
13581 st->codecpar->codec_type == AVMEDIA_TYPE_DATA;
1671
1672
3/4
✓ Branch 0 taken 11109 times.
✓ Branch 1 taken 2597 times.
✓ Branch 2 taken 11109 times.
✗ Branch 3 not taken.
13706 if (st->start_time != AV_NOPTS_VALUE && st->time_base.den) {
1673 11109 start_time1 = av_rescale_q(st->start_time, st->time_base,
1674 11109 AV_TIME_BASE_Q);
1675
2/2
✓ Branch 0 taken 152 times.
✓ Branch 1 taken 10957 times.
11109 if (is_text)
1676 152 start_time_text = FFMIN(start_time_text, start_time1);
1677 else
1678 10957 start_time = FFMIN(start_time, start_time1);
1679 11109 end_time1 = av_rescale_q_rnd(st->duration, st->time_base,
1680 11109 AV_TIME_BASE_Q,
1681 AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
1682
5/6
✓ Branch 0 taken 9795 times.
✓ Branch 1 taken 1314 times.
✓ Branch 2 taken 9766 times.
✓ Branch 3 taken 29 times.
✓ Branch 4 taken 9795 times.
✗ Branch 5 not taken.
11109 if (end_time1 != AV_NOPTS_VALUE && (end_time1 > 0 ? start_time1 <= INT64_MAX - end_time1 : start_time1 >= INT64_MIN - end_time1)) {
1683 9795 end_time1 += start_time1;
1684
2/2
✓ Branch 0 taken 137 times.
✓ Branch 1 taken 9658 times.
9795 if (is_text)
1685 137 end_time_text = FFMAX(end_time_text, end_time1);
1686 else
1687 9658 end_time = FFMAX(end_time, end_time1);
1688 }
1689
2/2
✓ Branch 1 taken 192 times.
✓ Branch 2 taken 11109 times.
11301 for (AVProgram *p = NULL; (p = av_find_program_from_stream(ic, p, i)); ) {
1690
4/4
✓ Branch 0 taken 137 times.
✓ Branch 1 taken 55 times.
✓ Branch 2 taken 24 times.
✓ Branch 3 taken 113 times.
192 if (p->start_time == AV_NOPTS_VALUE || p->start_time > start_time1)
1691 79 p->start_time = start_time1;
1692
2/2
✓ Branch 0 taken 49 times.
✓ Branch 1 taken 143 times.
192 if (p->end_time < end_time1)
1693 49 p->end_time = end_time1;
1694 }
1695 }
1696
2/2
✓ Branch 0 taken 11245 times.
✓ Branch 1 taken 2461 times.
13706 if (st->duration != AV_NOPTS_VALUE) {
1697 11245 duration1 = av_rescale_q(st->duration, st->time_base,
1698 11245 AV_TIME_BASE_Q);
1699
2/2
✓ Branch 0 taken 161 times.
✓ Branch 1 taken 11084 times.
11245 if (is_text)
1700 161 duration_text = FFMAX(duration_text, duration1);
1701 else
1702 11084 duration = FFMAX(duration, duration1);
1703 }
1704 }
1705
3/6
✓ Branch 0 taken 9958 times.
✓ Branch 1 taken 2406 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 9958 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
12364 if (start_time == INT64_MAX || (start_time > start_time_text && start_time - (uint64_t)start_time_text < AV_TIME_BASE))
1706 2406 start_time = start_time_text;
1707
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9958 times.
9958 else if (start_time > start_time_text)
1708 av_log(ic, AV_LOG_VERBOSE, "Ignoring outlier non primary stream starttime %f\n", start_time_text / (float)AV_TIME_BASE);
1709
1710
5/6
✓ Branch 0 taken 8994 times.
✓ Branch 1 taken 3370 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 8992 times.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
12364 if (end_time == INT64_MIN || (end_time < end_time_text && end_time_text - (uint64_t)end_time < AV_TIME_BASE))
1711 3372 end_time = end_time_text;
1712
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8992 times.
8992 else if (end_time < end_time_text)
1713 av_log(ic, AV_LOG_VERBOSE, "Ignoring outlier non primary stream endtime %f\n", end_time_text / (float)AV_TIME_BASE);
1714
1715
5/6
✓ Branch 0 taken 10405 times.
✓ Branch 1 taken 1959 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 10399 times.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
12364 if (duration == INT64_MIN || (duration < duration_text && (uint64_t)duration_text - duration < AV_TIME_BASE))
1716 1965 duration = duration_text;
1717
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10399 times.
10399 else if (duration < duration_text)
1718 av_log(ic, AV_LOG_VERBOSE, "Ignoring outlier non primary stream duration %f\n", duration_text / (float)AV_TIME_BASE);
1719
1720
2/2
✓ Branch 0 taken 9979 times.
✓ Branch 1 taken 2385 times.
12364 if (start_time != INT64_MAX) {
1721 9979 ic->start_time = start_time;
1722
2/2
✓ Branch 0 taken 9011 times.
✓ Branch 1 taken 968 times.
9979 if (end_time != INT64_MIN) {
1723
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9011 times.
9011 if (ic->nb_programs > 1) {
1724 for (unsigned i = 0; i < ic->nb_programs; i++) {
1725 AVProgram *const p = ic->programs[i];
1726
1727 if (p->start_time != AV_NOPTS_VALUE &&
1728 p->end_time > p->start_time &&
1729 p->end_time - (uint64_t)p->start_time <= INT64_MAX)
1730 duration = FFMAX(duration, p->end_time - p->start_time);
1731 }
1732
2/4
✓ Branch 0 taken 9011 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9011 times.
✗ Branch 3 not taken.
9011 } else if (end_time >= start_time && end_time - (uint64_t)start_time <= INT64_MAX) {
1733 9011 duration = FFMAX(duration, end_time - start_time);
1734 }
1735 }
1736 }
1737
6/6
✓ Branch 0 taken 10435 times.
✓ Branch 1 taken 1929 times.
✓ Branch 2 taken 10399 times.
✓ Branch 3 taken 36 times.
✓ Branch 4 taken 5601 times.
✓ Branch 5 taken 4798 times.
12364 if (duration != INT64_MIN && duration > 0 && ic->duration == AV_NOPTS_VALUE) {
1738 5601 ic->duration = duration;
1739 }
1740
5/6
✓ Branch 0 taken 6939 times.
✓ Branch 1 taken 5425 times.
✓ Branch 3 taken 6939 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 5852 times.
✓ Branch 6 taken 1087 times.
12364 if (ic->pb && (filesize = avio_size(ic->pb)) > 0 && ic->duration > 0) {
1741 /* compute the bitrate */
1742 5852 double bitrate = (double) filesize * 8.0 * AV_TIME_BASE /
1743 5852 (double) ic->duration;
1744
2/4
✓ Branch 0 taken 5852 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5852 times.
✗ Branch 3 not taken.
5852 if (bitrate >= 0 && bitrate <= INT64_MAX)
1745 5852 ic->bit_rate = bitrate;
1746 }
1747 12364 }
1748
1749 5218 static void fill_all_stream_timings(AVFormatContext *ic)
1750 {
1751 5218 update_stream_timings(ic);
1752
2/2
✓ Branch 0 taken 5826 times.
✓ Branch 1 taken 5218 times.
11044 for (unsigned i = 0; i < ic->nb_streams; i++) {
1753 5826 AVStream *const st = ic->streams[i];
1754
1755
2/2
✓ Branch 0 taken 765 times.
✓ Branch 1 taken 5061 times.
5826 if (st->start_time == AV_NOPTS_VALUE) {
1756
2/2
✓ Branch 0 taken 125 times.
✓ Branch 1 taken 640 times.
765 if (ic->start_time != AV_NOPTS_VALUE)
1757 125 st->start_time = av_rescale_q(ic->start_time, AV_TIME_BASE_Q,
1758 st->time_base);
1759
2/2
✓ Branch 0 taken 750 times.
✓ Branch 1 taken 15 times.
765 if (ic->duration != AV_NOPTS_VALUE)
1760 750 st->duration = av_rescale_q(ic->duration, AV_TIME_BASE_Q,
1761 st->time_base);
1762 }
1763 }
1764 5218 }
1765
1766 1928 static void estimate_timings_from_bit_rate(AVFormatContext *ic)
1767 {
1768 1928 FFFormatContext *const si = ffformatcontext(ic);
1769 1928 int show_warning = 0;
1770
1771 /* if bit_rate is already set, we believe it */
1772
2/2
✓ Branch 0 taken 1899 times.
✓ Branch 1 taken 29 times.
1928 if (ic->bit_rate <= 0) {
1773 1899 int64_t bit_rate = 0;
1774
2/2
✓ Branch 0 taken 2004 times.
✓ Branch 1 taken 1268 times.
3272 for (unsigned i = 0; i < ic->nb_streams; i++) {
1775 2004 const AVStream *const st = ic->streams[i];
1776 2004 const FFStream *const sti = cffstream(st);
1777
4/4
✓ Branch 0 taken 1256 times.
✓ Branch 1 taken 748 times.
✓ Branch 2 taken 115 times.
✓ Branch 3 taken 1141 times.
2004 if (st->codecpar->bit_rate <= 0 && sti->avctx->bit_rate > 0)
1778 115 st->codecpar->bit_rate = sti->avctx->bit_rate;
1779
2/2
✓ Branch 0 taken 863 times.
✓ Branch 1 taken 1141 times.
2004 if (st->codecpar->bit_rate > 0) {
1780
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 863 times.
863 if (INT64_MAX - st->codecpar->bit_rate < bit_rate) {
1781 bit_rate = 0;
1782 break;
1783 }
1784 863 bit_rate += st->codecpar->bit_rate;
1785
4/4
✓ Branch 0 taken 994 times.
✓ Branch 1 taken 147 times.
✓ Branch 2 taken 631 times.
✓ Branch 3 taken 363 times.
1141 } else if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && sti->codec_info_nb_frames > 1) {
1786 // If we have a videostream with packets but without a bitrate
1787 // then consider the sum not known
1788 631 bit_rate = 0;
1789 631 break;
1790 }
1791 }
1792 1899 ic->bit_rate = bit_rate;
1793 }
1794
1795 /* if duration is already set, we believe it */
1796
1/2
✓ Branch 0 taken 1928 times.
✗ Branch 1 not taken.
1928 if (ic->duration == AV_NOPTS_VALUE &&
1797
2/2
✓ Branch 0 taken 864 times.
✓ Branch 1 taken 1064 times.
1928 ic->bit_rate != 0) {
1798
2/2
✓ Branch 0 taken 840 times.
✓ Branch 1 taken 24 times.
864 int64_t filesize = ic->pb ? avio_size(ic->pb) : 0;
1799
2/2
✓ Branch 0 taken 834 times.
✓ Branch 1 taken 30 times.
864 if (filesize > si->data_offset) {
1800 834 filesize -= si->data_offset;
1801
2/2
✓ Branch 0 taken 872 times.
✓ Branch 1 taken 834 times.
1706 for (unsigned i = 0; i < ic->nb_streams; i++) {
1802 872 AVStream *const st = ic->streams[i];
1803
1804
1/2
✓ Branch 0 taken 872 times.
✗ Branch 1 not taken.
872 if ( st->time_base.num <= INT64_MAX / ic->bit_rate
1805
1/2
✓ Branch 0 taken 872 times.
✗ Branch 1 not taken.
872 && st->duration == AV_NOPTS_VALUE) {
1806 872 st->duration = av_rescale(filesize, 8LL * st->time_base.den,
1807 872 ic->bit_rate *
1808 872 (int64_t) st->time_base.num);
1809 872 show_warning = 1;
1810 }
1811 }
1812 }
1813 }
1814
2/2
✓ Branch 0 taken 834 times.
✓ Branch 1 taken 1094 times.
1928 if (show_warning)
1815 834 av_log(ic, AV_LOG_WARNING,
1816 "Estimating duration from bitrate, this may be inaccurate\n");
1817 1928 }
1818
1819 #define DURATION_DEFAULT_MAX_READ_SIZE 250000LL
1820 #define DURATION_DEFAULT_MAX_RETRY 6
1821 #define DURATION_MAX_RETRY 1
1822
1823 /* only usable for MPEG-PS streams */
1824 68 static void estimate_timings_from_pts(AVFormatContext *ic, int64_t old_offset)
1825 {
1826 68 FFFormatContext *const si = ffformatcontext(ic);
1827 68 AVPacket *const pkt = si->pkt;
1828 int num, den, read_size, ret;
1829
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 68 times.
68 int64_t duration_max_read_size = ic->duration_probesize ? ic->duration_probesize >> DURATION_MAX_RETRY : DURATION_DEFAULT_MAX_READ_SIZE;
1830
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 68 times.
68 int duration_max_retry = ic->duration_probesize ? DURATION_MAX_RETRY : DURATION_DEFAULT_MAX_RETRY;
1831 68 int found_duration = 0;
1832 int is_end;
1833 int64_t filesize, offset, duration;
1834 68 int retry = 0;
1835
1836 /* flush packet queue */
1837 68 ff_flush_packet_queue(ic);
1838
1839
2/2
✓ Branch 0 taken 133 times.
✓ Branch 1 taken 68 times.
201 for (unsigned i = 0; i < ic->nb_streams; i++) {
1840 133 AVStream *const st = ic->streams[i];
1841 133 FFStream *const sti = ffstream(st);
1842
1843
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 115 times.
133 if (st->start_time == AV_NOPTS_VALUE &&
1844
1/2
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
18 sti->first_dts == AV_NOPTS_VALUE &&
1845
1/2
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
18 st->codecpar->codec_type != AVMEDIA_TYPE_UNKNOWN)
1846 18 av_log(ic, AV_LOG_WARNING,
1847 "start time for stream %d is not set in estimate_timings_from_pts\n", i);
1848
1849
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 109 times.
133 if (sti->parser) {
1850 24 av_parser_close(sti->parser);
1851 24 sti->parser = NULL;
1852 }
1853 }
1854
1855
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 68 times.
68 if (ic->skip_estimate_duration_from_pts) {
1856 av_log(ic, AV_LOG_INFO, "Skipping duration calculation in estimate_timings_from_pts\n");
1857 goto skip_duration_calc;
1858 }
1859
1860 68 av_opt_set_int(ic, "skip_changes", 1, AV_OPT_SEARCH_CHILDREN);
1861 /* estimate the end time (duration) */
1862 /* XXX: may need to support wrapping */
1863
1/2
✓ Branch 0 taken 68 times.
✗ Branch 1 not taken.
68 filesize = ic->pb ? avio_size(ic->pb) : 0;
1864 do {
1865 74 is_end = found_duration;
1866 74 offset = filesize - (duration_max_read_size << retry);
1867
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 49 times.
74 if (offset < 0)
1868 25 offset = 0;
1869
1870 74 avio_seek(ic->pb, offset, SEEK_SET);
1871 74 read_size = 0;
1872 6542 for (;;) {
1873 AVStream *st;
1874 FFStream *sti;
1875
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6610 times.
6616 if (read_size >= duration_max_read_size << (FFMAX(retry - 1, 0)))
1876 6 break;
1877
1878 do {
1879 6610 ret = ff_read_packet(ic, pkt);
1880
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6610 times.
6610 } while (ret == AVERROR(EAGAIN));
1881
2/2
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 6542 times.
6610 if (ret != 0)
1882 68 break;
1883 6542 read_size += pkt->size;
1884 6542 st = ic->streams[pkt->stream_index];
1885 6542 sti = ffstream(st);
1886
2/2
✓ Branch 0 taken 2793 times.
✓ Branch 1 taken 3749 times.
6542 if (pkt->pts != AV_NOPTS_VALUE &&
1887
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2793 times.
2793 (st->start_time != AV_NOPTS_VALUE ||
1888 sti->first_dts != AV_NOPTS_VALUE)) {
1889
1/2
✓ Branch 0 taken 2793 times.
✗ Branch 1 not taken.
2793 if (pkt->duration == 0) {
1890 2793 compute_frame_duration(ic, &num, &den, st, sti->parser, pkt);
1891
3/4
✓ Branch 0 taken 2333 times.
✓ Branch 1 taken 460 times.
✓ Branch 2 taken 2333 times.
✗ Branch 3 not taken.
2793 if (den && num) {
1892 2333 pkt->duration = av_rescale_rnd(1,
1893 2333 num * (int64_t) st->time_base.den,
1894 2333 den * (int64_t) st->time_base.num,
1895 AV_ROUND_DOWN);
1896 }
1897 }
1898 2793 duration = pkt->pts + pkt->duration;
1899 2793 found_duration = 1;
1900
1/2
✓ Branch 0 taken 2793 times.
✗ Branch 1 not taken.
2793 if (st->start_time != AV_NOPTS_VALUE)
1901 2793 duration -= st->start_time;
1902 else
1903 duration -= sti->first_dts;
1904
2/2
✓ Branch 0 taken 2784 times.
✓ Branch 1 taken 9 times.
2793 if (duration > 0) {
1905
3/4
✓ Branch 0 taken 2677 times.
✓ Branch 1 taken 107 times.
✓ Branch 2 taken 2677 times.
✗ Branch 3 not taken.
2784 if (st->duration == AV_NOPTS_VALUE || sti->info->last_duration<= 0 ||
1906
3/4
✓ Branch 0 taken 2427 times.
✓ Branch 1 taken 250 times.
✓ Branch 2 taken 2427 times.
✗ Branch 3 not taken.
2677 (st->duration < duration && FFABS(duration - sti->info->last_duration) < 60LL*st->time_base.den / st->time_base.num))
1907 2534 st->duration = duration;
1908 2784 sti->info->last_duration = duration;
1909 }
1910 }
1911 6542 av_packet_unref(pkt);
1912 }
1913
1914 /* check if all audio/video streams have valid duration */
1915
2/2
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 6 times.
74 if (!is_end) {
1916 68 is_end = 1;
1917
2/2
✓ Branch 0 taken 133 times.
✓ Branch 1 taken 68 times.
201 for (unsigned i = 0; i < ic->nb_streams; i++) {
1918 133 const AVStream *const st = ic->streams[i];
1919
2/2
✓ Branch 0 taken 117 times.
✓ Branch 1 taken 16 times.
133 switch (st->codecpar->codec_type) {
1920 117 case AVMEDIA_TYPE_VIDEO:
1921 case AVMEDIA_TYPE_AUDIO:
1922
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 98 times.
117 if (st->duration == AV_NOPTS_VALUE)
1923 19 is_end = 0;
1924 }
1925 }
1926 }
1927
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2 times.
8 } while (!is_end &&
1928
3/4
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 66 times.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
80 offset &&
1929 ++retry <= duration_max_retry);
1930
1931 68 av_opt_set_int(ic, "skip_changes", 0, AV_OPT_SEARCH_CHILDREN);
1932
1933 /* warn about audio/video streams which duration could not be estimated */
1934
2/2
✓ Branch 0 taken 133 times.
✓ Branch 1 taken 68 times.
201 for (unsigned i = 0; i < ic->nb_streams; i++) {
1935 133 const AVStream *const st = ic->streams[i];
1936 133 const FFStream *const sti = cffstream(st);
1937
1938
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 107 times.
133 if (st->duration == AV_NOPTS_VALUE) {
1939
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 13 times.
26 switch (st->codecpar->codec_type) {
1940 13 case AVMEDIA_TYPE_VIDEO:
1941 case AVMEDIA_TYPE_AUDIO:
1942
3/4
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
13 if (st->start_time != AV_NOPTS_VALUE || sti->first_dts != AV_NOPTS_VALUE) {
1943 5 av_log(ic, AV_LOG_WARNING, "stream %d : no PTS found at end of file, duration not set\n", i);
1944 } else
1945 8 av_log(ic, AV_LOG_WARNING, "stream %d : no TS found at start of file, duration not set\n", i);
1946 }
1947 }
1948 }
1949 68 skip_duration_calc:
1950 68 fill_all_stream_timings(ic);
1951
1952 68 avio_seek(ic->pb, old_offset, SEEK_SET);
1953
2/2
✓ Branch 0 taken 133 times.
✓ Branch 1 taken 68 times.
201 for (unsigned i = 0; i < ic->nb_streams; i++) {
1954 133 AVStream *const st = ic->streams[i];
1955 133 FFStream *const sti = ffstream(st);
1956
1957 133 sti->cur_dts = sti->first_dts;
1958 133 sti->last_IP_pts = AV_NOPTS_VALUE;
1959 133 sti->last_dts_for_order_check = AV_NOPTS_VALUE;
1960
2/2
✓ Branch 0 taken 2261 times.
✓ Branch 1 taken 133 times.
2394 for (int j = 0; j < MAX_REORDER_DELAY + 1; j++)
1961 2261 sti->pts_buffer[j] = AV_NOPTS_VALUE;
1962 }
1963 68 }
1964
1965 /* 1:1 map to AVDurationEstimationMethod */
1966 static const char *const duration_name[] = {
1967 [AVFMT_DURATION_FROM_PTS] = "pts",
1968 [AVFMT_DURATION_FROM_STREAM] = "stream",
1969 [AVFMT_DURATION_FROM_BITRATE] = "bit rate",
1970 };
1971
1972 7146 static const char *duration_estimate_name(enum AVDurationEstimationMethod method)
1973 {
1974 7146 return duration_name[method];
1975 }
1976
1977 7146 static void estimate_timings(AVFormatContext *ic, int64_t old_offset)
1978 {
1979 int64_t file_size;
1980
1981 /* get the file size, if possible */
1982
2/2
✓ Branch 0 taken 2759 times.
✓ Branch 1 taken 4387 times.
7146 if (ic->iformat->flags & AVFMT_NOFILE) {
1983 2759 file_size = 0;
1984 } else {
1985 4387 file_size = avio_size(ic->pb);
1986 4387 file_size = FFMAX(0, file_size);
1987 }
1988
1989
2/2
✓ Branch 0 taken 7121 times.
✓ Branch 1 taken 25 times.
7146 if ((!strcmp(ic->iformat->name, "mpeg") ||
1990
3/4
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 7078 times.
✓ Branch 2 taken 68 times.
✗ Branch 3 not taken.
7146 !strcmp(ic->iformat->name, "mpegts")) &&
1991
1/2
✓ Branch 0 taken 68 times.
✗ Branch 1 not taken.
68 file_size && (ic->pb->seekable & AVIO_SEEKABLE_NORMAL)) {
1992 /* get accurate estimate from the PTSes */
1993 68 estimate_timings_from_pts(ic, old_offset);
1994 68 ic->duration_estimation_method = AVFMT_DURATION_FROM_PTS;
1995
2/2
✓ Branch 1 taken 5150 times.
✓ Branch 2 taken 1928 times.
7078 } else if (has_duration(ic)) {
1996 /* at least one component has timings - we use them for all
1997 * the components */
1998 5150 fill_all_stream_timings(ic);
1999 /* nut demuxer estimate the duration from PTS */
2000
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 5120 times.
5150 if (!strcmp(ic->iformat->name, "nut"))
2001 30 ic->duration_estimation_method = AVFMT_DURATION_FROM_PTS;
2002 else
2003 5120 ic->duration_estimation_method = AVFMT_DURATION_FROM_STREAM;
2004 } else {
2005 /* less precise: use bitrate info */
2006 1928 estimate_timings_from_bit_rate(ic);
2007 1928 ic->duration_estimation_method = AVFMT_DURATION_FROM_BITRATE;
2008 }
2009 7146 update_stream_timings(ic);
2010
2011
2/2
✓ Branch 0 taken 7880 times.
✓ Branch 1 taken 7146 times.
15026 for (unsigned i = 0; i < ic->nb_streams; i++) {
2012 7880 AVStream *const st = ic->streams[i];
2013
1/2
✓ Branch 0 taken 7880 times.
✗ Branch 1 not taken.
7880 if (st->time_base.den)
2014 7880 av_log(ic, AV_LOG_TRACE, "stream %u: start_time: %s duration: %s\n", i,
2015 7880 av_ts2timestr(st->start_time, &st->time_base),
2016 7880 av_ts2timestr(st->duration, &st->time_base));
2017 }
2018 7146 av_log(ic, AV_LOG_TRACE,
2019 "format: start_time: %s duration: %s (estimate from %s) bitrate=%"PRId64" kb/s\n",
2020 7146 av_ts2timestr(ic->start_time, &AV_TIME_BASE_Q),
2021 7146 av_ts2timestr(ic->duration, &AV_TIME_BASE_Q),
2022 duration_estimate_name(ic->duration_estimation_method),
2023 7146 (int64_t)ic->bit_rate / 1000);
2024 7146 }
2025
2026 106688 static int determinable_frame_size(const AVCodecContext *avctx)
2027 {
2028
2/2
✓ Branch 0 taken 2187 times.
✓ Branch 1 taken 104501 times.
106688 switch(avctx->codec_id) {
2029 2187 case AV_CODEC_ID_MP1:
2030 case AV_CODEC_ID_MP2:
2031 case AV_CODEC_ID_MP3:
2032 case AV_CODEC_ID_CODEC2:
2033 2187 return 1;
2034 }
2035
2036 104501 return 0;
2037 }
2038
2039 396956 static int has_codec_parameters(const AVStream *st, const char **errmsg_ptr)
2040 {
2041 396956 const FFStream *const sti = cffstream(st);
2042 396956 const AVCodecContext *const avctx = sti->avctx;
2043
2044 #define FAIL(errmsg) do { \
2045 if (errmsg_ptr) \
2046 *errmsg_ptr = errmsg; \
2047 return 0; \
2048 } while (0)
2049
2050
2/2
✓ Branch 0 taken 635 times.
✓ Branch 1 taken 396321 times.
396956 if ( avctx->codec_id == AV_CODEC_ID_NONE
2051
2/2
✓ Branch 0 taken 416 times.
✓ Branch 1 taken 219 times.
635 && avctx->codec_type != AVMEDIA_TYPE_DATA)
2052
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 411 times.
416 FAIL("unknown codec");
2053
4/5
✓ Branch 0 taken 121507 times.
✓ Branch 1 taken 273501 times.
✓ Branch 2 taken 629 times.
✓ Branch 3 taken 903 times.
✗ Branch 4 not taken.
396540 switch (avctx->codec_type) {
2054 121507 case AVMEDIA_TYPE_AUDIO:
2055
4/4
✓ Branch 0 taken 106688 times.
✓ Branch 1 taken 14819 times.
✓ Branch 3 taken 2187 times.
✓ Branch 4 taken 104501 times.
121507 if (!avctx->frame_size && determinable_frame_size(avctx))
2056
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 2175 times.
2187 FAIL("unspecified frame size");
2057
1/2
✓ Branch 0 taken 119320 times.
✗ Branch 1 not taken.
119320 if (sti->info->found_decoder >= 0 &&
2058
2/2
✓ Branch 0 taken 2384 times.
✓ Branch 1 taken 116936 times.
119320 avctx->sample_fmt == AV_SAMPLE_FMT_NONE)
2059
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2384 times.
2384 FAIL("unspecified sample format");
2060
2/2
✓ Branch 0 taken 126 times.
✓ Branch 1 taken 116810 times.
116936 if (!avctx->sample_rate)
2061
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 122 times.
126 FAIL("unspecified sample rate");
2062
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 116793 times.
116810 if (!avctx->ch_layout.nb_channels)
2063
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
17 FAIL("unspecified number of channels");
2064
4/6
✓ Branch 0 taken 116793 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 80234 times.
✓ Branch 3 taken 36559 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 80234 times.
116793 if (sti->info->found_decoder >= 0 && !sti->nb_decoded_frames && avctx->codec_id == AV_CODEC_ID_DTS)
2065 FAIL("no decodable DTS frames");
2066 116793 break;
2067 273501 case AVMEDIA_TYPE_VIDEO:
2068
2/2
✓ Branch 0 taken 11734 times.
✓ Branch 1 taken 261767 times.
273501 if (!avctx->width)
2069
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 11726 times.
11734 FAIL("unspecified size");
2070
4/4
✓ Branch 0 taken 260967 times.
✓ Branch 1 taken 800 times.
✓ Branch 2 taken 4123 times.
✓ Branch 3 taken 256844 times.
261767 if (sti->info->found_decoder >= 0 && avctx->pix_fmt == AV_PIX_FMT_NONE)
2071
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 4120 times.
4123 FAIL("unspecified pixel format");
2072
4/4
✓ Branch 0 taken 257544 times.
✓ Branch 1 taken 100 times.
✓ Branch 2 taken 86 times.
✓ Branch 3 taken 257458 times.
257644 if (st->codecpar->codec_id == AV_CODEC_ID_RV30 || st->codecpar->codec_id == AV_CODEC_ID_RV40)
2073
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)
2074
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 67 times.
67 FAIL("no frame in rv30/40 and no sar");
2075 257577 break;
2076 629 case AVMEDIA_TYPE_SUBTITLE:
2077
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)
2078
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 FAIL("unspecified size");
2079 608 break;
2080 903 case AVMEDIA_TYPE_DATA:
2081
2/2
✓ Branch 0 taken 219 times.
✓ Branch 1 taken 684 times.
903 if (avctx->codec_id == AV_CODEC_ID_NONE) return 1;
2082 }
2083
2084 375662 return 1;
2085 }
2086
2087 /* returns 1 or 0 if or if not decoded data was returned, or a negative error */
2088 185602 static int try_decode_frame(AVFormatContext *s, AVStream *st,
2089 const AVPacket *pkt, AVDictionary **options)
2090 {
2091 185602 FFStream *const sti = ffstream(st);
2092 185602 AVCodecContext *const avctx = sti->avctx;
2093 const AVCodec *codec;
2094 185602 int got_picture = 1, ret = 0;
2095 185602 AVFrame *frame = av_frame_alloc();
2096 AVSubtitle subtitle;
2097 185602 int do_skip_frame = 0;
2098 enum AVDiscard skip_frame;
2099 185602 int pkt_to_send = pkt->size > 0;
2100
2101
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 185602 times.
185602 if (!frame)
2102 return AVERROR(ENOMEM);
2103
2104
2/2
✓ Branch 1 taken 4032 times.
✓ Branch 2 taken 181570 times.
185602 if (!avcodec_is_open(avctx) &&
2105
1/2
✓ Branch 0 taken 4032 times.
✗ Branch 1 not taken.
4032 sti->info->found_decoder <= 0 &&
2106
4/4
✓ Branch 0 taken 2698 times.
✓ Branch 1 taken 1334 times.
✓ Branch 2 taken 63 times.
✓ Branch 3 taken 2635 times.
4032 (st->codecpar->codec_id != -sti->info->found_decoder || !st->codecpar->codec_id)) {
2107 1397 AVDictionary *thread_opt = NULL;
2108
2109 1397 codec = find_probe_decoder(s, st, st->codecpar->codec_id);
2110
2111
2/2
✓ Branch 0 taken 76 times.
✓ Branch 1 taken 1321 times.
1397 if (!codec) {
2112 76 sti->info->found_decoder = -st->codecpar->codec_id;
2113 76 ret = -1;
2114 114 goto fail;
2115 }
2116
2117 /* Force thread count to 1 since the H.264 decoder will not extract
2118 * SPS and PPS to extradata during multi-threaded decoding. */
2119
2/2
✓ Branch 0 taken 131 times.
✓ Branch 1 taken 1190 times.
1321 av_dict_set(options ? options : &thread_opt, "threads", "1", 0);
2120 /* Force lowres to 0. The decoder might reduce the video size by the
2121 * lowres factor, and we don't want that propagated to the stream's
2122 * codecpar */
2123
2/2
✓ Branch 0 taken 131 times.
✓ Branch 1 taken 1190 times.
1321 av_dict_set(options ? options : &thread_opt, "lowres", "0", 0);
2124
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1321 times.
1321 if (s->codec_whitelist)
2125 av_dict_set(options ? options : &thread_opt, "codec_whitelist", s->codec_whitelist, 0);
2126
2/2
✓ Branch 0 taken 131 times.
✓ Branch 1 taken 1190 times.
1321 ret = avcodec_open2(avctx, codec, options ? options : &thread_opt);
2127
2/2
✓ Branch 0 taken 131 times.
✓ Branch 1 taken 1190 times.
1321 if (!options)
2128 131 av_dict_free(&thread_opt);
2129
2/2
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 1283 times.
1321 if (ret < 0) {
2130 38 sti->info->found_decoder = -avctx->codec_id;
2131 38 goto fail;
2132 }
2133 1283 sti->info->found_decoder = 1;
2134
2/2
✓ Branch 0 taken 6309 times.
✓ Branch 1 taken 177896 times.
184205 } else if (!sti->info->found_decoder)
2135 6309 sti->info->found_decoder = 1;
2136
2137
2/2
✓ Branch 0 taken 2635 times.
✓ Branch 1 taken 182853 times.
185488 if (sti->info->found_decoder < 0) {
2138 2635 ret = -1;
2139 2635 goto fail;
2140 }
2141
2142
2/2
✓ Branch 1 taken 97270 times.
✓ Branch 2 taken 85583 times.
182853 if (avpriv_codec_get_cap_skip_frame_fill_param(avctx->codec)) {
2143 97270 do_skip_frame = 1;
2144 97270 skip_frame = avctx->skip_frame;
2145 97270 avctx->skip_frame = AVDISCARD_ALL;
2146 }
2147
2148
5/6
✓ Branch 0 taken 7509 times.
✓ Branch 1 taken 4168 times.
✓ Branch 2 taken 24 times.
✓ Branch 3 taken 4144 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 182861 times.
373247 while ((pkt_to_send || (!pkt->data && got_picture)) &&
2149
4/4
✓ Branch 0 taken 11677 times.
✓ Branch 1 taken 178717 times.
✓ Branch 2 taken 5151 times.
✓ Branch 3 taken 177710 times.
373255 ret >= 0 &&
2150
2/2
✓ Branch 2 taken 2229 times.
✓ Branch 3 taken 175481 times.
360571 (!has_codec_parameters(st, NULL) || !has_decode_delay_been_guessed(st) ||
2151
2/2
✓ Branch 0 taken 172760 times.
✓ Branch 1 taken 2721 times.
175481 (!sti->codec_info_nb_frames &&
2152
2/2
✓ Branch 0 taken 427 times.
✓ Branch 1 taken 2294 times.
2721 (avctx->codec->capabilities & AV_CODEC_CAP_CHANNEL_CONF)))) {
2153 7807 got_picture = 0;
2154
2/2
✓ Branch 0 taken 696 times.
✓ Branch 1 taken 7111 times.
7807 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO ||
2155
2/2
✓ Branch 0 taken 689 times.
✓ Branch 1 taken 7 times.
696 avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
2156 7800 ret = avcodec_send_packet(avctx, pkt);
2157
5/6
✓ Branch 0 taken 275 times.
✓ Branch 1 taken 7525 times.
✓ Branch 2 taken 275 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 266 times.
✓ Branch 5 taken 9 times.
7800 if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
2158 266 break;
2159
2/2
✓ Branch 0 taken 7525 times.
✓ Branch 1 taken 9 times.
7534 if (ret >= 0)
2160 7525 pkt_to_send = 0;
2161 7534 ret = avcodec_receive_frame(avctx, frame);
2162
2/2
✓ Branch 0 taken 3142 times.
✓ Branch 1 taken 4392 times.
7534 if (ret >= 0)
2163 3142 got_picture = 1;
2164
4/4
✓ Branch 0 taken 3166 times.
✓ Branch 1 taken 4368 times.
✓ Branch 2 taken 24 times.
✓ Branch 3 taken 3142 times.
7534 if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
2165 4392 ret = 0;
2166
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 } else if (avctx->codec_type == AVMEDIA_TYPE_SUBTITLE) {
2167 7 ret = avcodec_decode_subtitle2(avctx, &subtitle,
2168 &got_picture, pkt);
2169
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 5 times.
7 if (got_picture)
2170 2 avsubtitle_free(&subtitle);
2171
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 if (ret >= 0)
2172 7 pkt_to_send = 0;
2173 }
2174
1/2
✓ Branch 0 taken 7541 times.
✗ Branch 1 not taken.
7541 if (ret >= 0) {
2175
2/2
✓ Branch 0 taken 3144 times.
✓ Branch 1 taken 4397 times.
7541 if (got_picture)
2176 3144 sti->nb_decoded_frames++;
2177 7541 ret = got_picture;
2178 }
2179 }
2180
2181 182587 fail:
2182
2/2
✓ Branch 0 taken 97270 times.
✓ Branch 1 taken 88332 times.
185602 if (do_skip_frame) {
2183 97270 avctx->skip_frame = skip_frame;
2184 }
2185
2186 185602 av_frame_free(&frame);
2187 185602 return ret;
2188 }
2189
2190 57 static int chapter_start_cmp(const void *p1, const void *p2)
2191 {
2192 57 const AVChapter *const ch1 = *(AVChapter**)p1;
2193 57 const AVChapter *const ch2 = *(AVChapter**)p2;
2194 57 int delta = av_compare_ts(ch1->start, ch1->time_base, ch2->start, ch2->time_base);
2195
1/2
✓ Branch 0 taken 57 times.
✗ Branch 1 not taken.
57 if (delta)
2196 57 return delta;
2197 return FFDIFFSIGN(ch1->id, ch2->id);
2198 }
2199
2200 7146 static int compute_chapters_end(AVFormatContext *s)
2201 {
2202 7146 int64_t max_time = 0;
2203 AVChapter **timetable;
2204
2205
2/2
✓ Branch 0 taken 7120 times.
✓ Branch 1 taken 26 times.
7146 if (!s->nb_chapters)
2206 7120 return 0;
2207
2208
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)
2209 26 max_time = s->duration +
2210
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 3 times.
26 ((s->start_time == AV_NOPTS_VALUE) ? 0 : s->start_time);
2211
2212 26 timetable = av_memdup(s->chapters, s->nb_chapters * sizeof(*timetable));
2213
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
26 if (!timetable)
2214 return AVERROR(ENOMEM);
2215 26 qsort(timetable, s->nb_chapters, sizeof(*timetable), chapter_start_cmp);
2216
2217
2/2
✓ Branch 0 taken 71 times.
✓ Branch 1 taken 26 times.
97 for (unsigned i = 0; i < s->nb_chapters; i++)
2218
2/2
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 31 times.
71 if (timetable[i]->end == AV_NOPTS_VALUE) {
2219 40 AVChapter *const ch = timetable[i];
2220 40 int64_t end = max_time ? av_rescale_q(max_time, AV_TIME_BASE_Q,
2221 ch->time_base)
2222
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
40 : INT64_MAX;
2223
2224
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 19 times.
40 if (i + 1 < s->nb_chapters) {
2225 21 const AVChapter *const ch1 = timetable[i + 1];
2226 21 int64_t next_start = av_rescale_q(ch1->start, ch1->time_base,
2227 ch->time_base);
2228
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)
2229 21 end = next_start;
2230 }
2231
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;
2232 }
2233 26 av_free(timetable);
2234 26 return 0;
2235 }
2236
2237 17029956 static int get_std_framerate(int i)
2238 {
2239
2/2
✓ Branch 0 taken 15468939 times.
✓ Branch 1 taken 1561017 times.
17029956 if (i < 30*12)
2240 15468939 return (i + 1) * 1001;
2241 1561017 i -= 30*12;
2242
2243
2/2
✓ Branch 0 taken 1186265 times.
✓ Branch 1 taken 374752 times.
1561017 if (i < 30)
2244 1186265 return (i + 31) * 1001 * 12;
2245 374752 i -= 30;
2246
2247
2/2
✓ Branch 0 taken 119676 times.
✓ Branch 1 taken 255076 times.
374752 if (i < 3)
2248 119676 return ((const int[]) { 80, 120, 240})[i] * 1001 * 12;
2249
2250 255076 i -= 3;
2251
2252 255076 return ((const int[]) { 24, 30, 60, 12, 15, 48 })[i] * 1000 * 12;
2253 }
2254
2255 /* Is the time base unreliable?
2256 * This is a heuristic to balance between quick acceptance of the values in
2257 * the headers vs. some extra checks.
2258 * Old DivX and Xvid often have nonsense timebases like 1fps or 2fps.
2259 * MPEG-2 commonly misuses field repeat flags to store different framerates.
2260 * And there are "variable" fps files this needs to detect as well. */
2261 194677 static int tb_unreliable(AVFormatContext *ic, AVStream *st)
2262 {
2263 194677 FFStream *const sti = ffstream(st);
2264 194677 const AVCodecDescriptor *desc = sti->codec_desc;
2265 194677 AVCodecContext *c = sti->avctx;
2266
4/4
✓ Branch 0 taken 194247 times.
✓ Branch 1 taken 430 times.
✓ Branch 2 taken 19507 times.
✓ Branch 3 taken 174740 times.
194677 AVRational mul = (AVRational){ desc && (desc->props & AV_CODEC_PROP_FIELDS) ? 2 : 1, 1 };
2267 194677 AVRational time_base = c->framerate.num ? av_inv_q(av_mul_q(c->framerate, mul))
2268 /* NOHEADER check added to not break existing behavior */
2269
2/2
✓ Branch 0 taken 16093 times.
✓ Branch 1 taken 178584 times.
194677 : (((ic->ctx_flags & AVFMTCTX_NOHEADER) ||
2270
2/2
✓ Branch 0 taken 44138 times.
✓ Branch 1 taken 29711 times.
73849 st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) ? (AVRational){0, 1}
2271
2/2
✓ Branch 0 taken 73849 times.
✓ Branch 1 taken 104735 times.
178584 : st->time_base);
2272
2273
2/2
✓ Branch 0 taken 22211 times.
✓ Branch 1 taken 172466 times.
194677 if (time_base.den >= 101LL * time_base.num ||
2274
2/2
✓ Branch 0 taken 21817 times.
✓ Branch 1 taken 394 times.
22211 time_base.den < 5LL * time_base.num ||
2275 // c->codec_tag == AV_RL32("DIVX") ||
2276 // c->codec_tag == AV_RL32("XVID") ||
2277
2/2
✓ Branch 0 taken 21743 times.
✓ Branch 1 taken 74 times.
21817 c->codec_tag == AV_RL32("mp4v") ||
2278
2/2
✓ Branch 0 taken 12915 times.
✓ Branch 1 taken 8828 times.
21743 c->codec_id == AV_CODEC_ID_MPEG2VIDEO ||
2279
2/2
✓ Branch 0 taken 12385 times.
✓ Branch 1 taken 530 times.
12915 c->codec_id == AV_CODEC_ID_GIF ||
2280
2/2
✓ Branch 0 taken 11751 times.
✓ Branch 1 taken 634 times.
12385 c->codec_id == AV_CODEC_ID_HEVC ||
2281
2/2
✓ Branch 0 taken 2019 times.
✓ Branch 1 taken 9732 times.
11751 c->codec_id == AV_CODEC_ID_H264)
2282 184945 return 1;
2283 9732 return 0;
2284 }
2285
2286 129901 int ff_rfps_add_frame(AVFormatContext *ic, AVStream *st, int64_t ts)
2287 {
2288 129901 FFStream *const sti = ffstream(st);
2289 129901 FFStreamInfo *info = sti->info;
2290 129901 int64_t last = info->last_dts;
2291
2292
6/6
✓ Branch 0 taken 114291 times.
✓ Branch 1 taken 15610 times.
✓ Branch 2 taken 108722 times.
✓ Branch 3 taken 5569 times.
✓ Branch 4 taken 108694 times.
✓ Branch 5 taken 28 times.
129901 if ( ts != AV_NOPTS_VALUE && last != AV_NOPTS_VALUE && ts > last
2293
1/2
✓ Branch 0 taken 108694 times.
✗ Branch 1 not taken.
108694 && ts - (uint64_t)last < INT64_MAX) {
2294
2/2
✓ Branch 1 taken 6480 times.
✓ Branch 2 taken 102214 times.
108694 double dts = (is_relative(ts) ? ts - RELATIVE_TS_BASE : ts) * av_q2d(st->time_base);
2295 108694 int64_t duration = ts - last;
2296
2297
2/2
✓ Branch 0 taken 3552 times.
✓ Branch 1 taken 105142 times.
108694 if (!info->duration_error)
2298 3552 info->duration_error = av_mallocz(sizeof(info->duration_error[0])*2);
2299
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 108694 times.
108694 if (!info->duration_error)
2300 return AVERROR(ENOMEM);
2301
2302 // if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
2303 // av_log(NULL, AV_LOG_ERROR, "%f\n", dts);
2304
2/2
✓ Branch 0 taken 43368906 times.
✓ Branch 1 taken 108694 times.
43477600 for (int i = 0; i < MAX_STD_TIMEBASES; i++) {
2305
2/2
✓ Branch 0 taken 16681508 times.
✓ Branch 1 taken 26687398 times.
43368906 if (info->duration_error[0][1][i] < 1e10) {
2306 16681508 int framerate = get_std_framerate(i);
2307 16681508 double sdts = dts*framerate/(1001*12);
2308
2/2
✓ Branch 0 taken 33363016 times.
✓ Branch 1 taken 16681508 times.
50044524 for (int j = 0; j < 2; j++) {
2309 33363016 int64_t ticks = llrint(sdts+j*0.5);
2310 33363016 double error = sdts - ticks + j*0.5;
2311 33363016 info->duration_error[j][0][i] += error;
2312 33363016 info->duration_error[j][1][i] += error*error;
2313 }
2314 }
2315 }
2316
1/2
✓ Branch 0 taken 108694 times.
✗ Branch 1 not taken.
108694 if (info->rfps_duration_sum <= INT64_MAX - duration) {
2317 108694 info->duration_count++;
2318 108694 info->rfps_duration_sum += duration;
2319 }
2320
2321
2/2
✓ Branch 0 taken 9875 times.
✓ Branch 1 taken 98819 times.
108694 if (info->duration_count % 10 == 0) {
2322 9875 int n = info->duration_count;
2323
2/2
✓ Branch 0 taken 3940125 times.
✓ Branch 1 taken 9875 times.
3950000 for (int i = 0; i < MAX_STD_TIMEBASES; i++) {
2324
2/2
✓ Branch 0 taken 1593825 times.
✓ Branch 1 taken 2346300 times.
3940125 if (info->duration_error[0][1][i] < 1e10) {
2325 1593825 double a0 = info->duration_error[0][0][i] / n;
2326 1593825 double error0 = info->duration_error[0][1][i] / n - a0*a0;
2327 1593825 double a1 = info->duration_error[1][0][i] / n;
2328 1593825 double error1 = info->duration_error[1][1][i] / n - a1*a1;
2329
4/4
✓ Branch 0 taken 1305292 times.
✓ Branch 1 taken 288533 times.
✓ Branch 2 taken 1218346 times.
✓ Branch 3 taken 86946 times.
1593825 if (error0 > 0.04 && error1 > 0.04) {
2330 1218346 info->duration_error[0][1][i] = 2e10;
2331 1218346 info->duration_error[1][1][i] = 2e10;
2332 }
2333 }
2334 }
2335 }
2336
2337 // ignore the first 4 values, they might have some random jitter
2338
3/4
✓ Branch 0 taken 98123 times.
✓ Branch 1 taken 10571 times.
✓ Branch 4 taken 98123 times.
✗ Branch 5 not taken.
108694 if (info->duration_count > 3 && is_relative(ts) == is_relative(last))
2339 98123 info->duration_gcd = av_gcd(info->duration_gcd, duration);
2340 }
2341
2/2
✓ Branch 0 taken 114291 times.
✓ Branch 1 taken 15610 times.
129901 if (ts != AV_NOPTS_VALUE)
2342 114291 info->last_dts = ts;
2343
2344 129901 return 0;
2345 }
2346
2347 7621 void ff_rfps_calculate(AVFormatContext *ic)
2348 {
2349
2/2
✓ Branch 0 taken 8512 times.
✓ Branch 1 taken 7621 times.
16133 for (unsigned i = 0; i < ic->nb_streams; i++) {
2350 8512 AVStream *const st = ic->streams[i];
2351 8512 FFStream *const sti = ffstream(st);
2352
2353
2/2
✓ Branch 0 taken 2374 times.
✓ Branch 1 taken 6138 times.
8512 if (st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO)
2354 2374 continue;
2355 // the check for tb_unreliable() is not completely correct, since this is not about handling
2356 // an unreliable/inexact time base, but a time base that is finer than necessary, as e.g.
2357 // ipmovie.c produces.
2358
8/8
✓ Branch 1 taken 4799 times.
✓ Branch 2 taken 1339 times.
✓ Branch 3 taken 3042 times.
✓ Branch 4 taken 1757 times.
✓ Branch 5 taken 284 times.
✓ Branch 6 taken 2758 times.
✓ Branch 7 taken 145 times.
✓ Branch 8 taken 139 times.
6138 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 &&
2359
1/2
✓ Branch 0 taken 145 times.
✗ Branch 1 not taken.
145 sti->info->duration_gcd < INT64_MAX / st->time_base.num)
2360 145 av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den, st->time_base.den, st->time_base.num * sti->info->duration_gcd, INT_MAX);
2361
4/4
✓ Branch 0 taken 3515 times.
✓ Branch 1 taken 2623 times.
✓ Branch 2 taken 387 times.
✓ Branch 3 taken 3128 times.
6138 if (sti->info->duration_count > 1 && !st->r_frame_rate.num
2362
2/2
✓ Branch 1 taken 284 times.
✓ Branch 2 taken 103 times.
387 && tb_unreliable(ic, st)) {
2363 284 int num = 0;
2364 284 double best_error = 0.01;
2365
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 284 times.
284 AVRational ref_rate = st->r_frame_rate.num ? st->r_frame_rate : av_inv_q(st->time_base);
2366
2367
2/2
✓ Branch 0 taken 113316 times.
✓ Branch 1 taken 284 times.
113600 for (int j = 0; j < MAX_STD_TIMEBASES; j++) {
2368
2/2
✓ Branch 0 taken 85386 times.
✓ Branch 1 taken 27930 times.
113316 if (sti->info->codec_info_duration &&
2369
2/2
✓ Branch 2 taken 9466 times.
✓ Branch 3 taken 75920 times.
85386 sti->info->codec_info_duration*av_q2d(st->time_base) < (1001*11.5)/get_std_framerate(j))
2370 9466 continue;
2371
4/4
✓ Branch 0 taken 27930 times.
✓ Branch 1 taken 75920 times.
✓ Branch 3 taken 770 times.
✓ Branch 4 taken 27160 times.
103850 if (!sti->info->codec_info_duration && get_std_framerate(j) < 1001*12)
2372 770 continue;
2373
2374
2/2
✓ Branch 2 taken 47759 times.
✓ Branch 3 taken 55321 times.
103080 if (av_q2d(st->time_base) * sti->info->rfps_duration_sum / sti->info->duration_count < (1001*12.0 * 0.8)/get_std_framerate(j))
2375 47759 continue;
2376
2377
2/2
✓ Branch 0 taken 110642 times.
✓ Branch 1 taken 55321 times.
165963 for (int k = 0; k < 2; k++) {
2378 110642 int n = sti->info->duration_count;
2379 110642 double a = sti->info->duration_error[k][0][j] / n;
2380 110642 double error = sti->info->duration_error[k][1][j]/n - a*a;
2381
2382
4/4
✓ Branch 0 taken 4267 times.
✓ Branch 1 taken 106375 times.
✓ Branch 2 taken 4198 times.
✓ Branch 3 taken 69 times.
110642 if (error < best_error && best_error> 0.000000001) {
2383 4198 best_error= error;
2384 4198 num = get_std_framerate(j);
2385 }
2386
2/2
✓ Branch 0 taken 23316 times.
✓ Branch 1 taken 87326 times.
110642 if (error < 0.02)
2387 23316 av_log(ic, AV_LOG_DEBUG, "rfps: %f %f\n", get_std_framerate(j) / 12.0/1001, error);
2388 }
2389 }
2390 // do not increase frame rate by more than 1 % in order to match a standard rate.
2391
5/6
✓ Branch 0 taken 278 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 278 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 276 times.
✓ Branch 6 taken 2 times.
284 if (num && (!ref_rate.num || (double)num/(12*1001) < 1.01 * av_q2d(ref_rate)))
2392 276 av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den, num, 12*1001, INT_MAX);
2393 }
2394
2/2
✓ Branch 0 taken 1049 times.
✓ Branch 1 taken 5089 times.
6138 if ( !st->avg_frame_rate.num
2395
4/4
✓ Branch 0 taken 269 times.
✓ Branch 1 taken 780 times.
✓ Branch 2 taken 265 times.
✓ Branch 3 taken 4 times.
1049 && st->r_frame_rate.num && sti->info->rfps_duration_sum
2396
2/2
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 201 times.
265 && sti->info->codec_info_duration <= 0
2397
2/2
✓ Branch 0 taken 62 times.
✓ Branch 1 taken 2 times.
64 && sti->info->duration_count > 2
2398
2/2
✓ Branch 2 taken 48 times.
✓ Branch 3 taken 14 times.
62 && 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
2399 ) {
2400 48 av_log(ic, AV_LOG_DEBUG, "Setting avg frame rate based on r frame rate\n");
2401 48 st->avg_frame_rate = st->r_frame_rate;
2402 }
2403
2404 6138 av_freep(&sti->info->duration_error);
2405 6138 sti->info->last_dts = AV_NOPTS_VALUE;
2406 6138 sti->info->duration_count = 0;
2407 6138 sti->info->rfps_duration_sum = 0;
2408 }
2409 7621 }
2410
2411 8571 static int extract_extradata_check(AVStream *st)
2412 {
2413 8571 const AVBitStreamFilter *const f = av_bsf_get_by_name("extract_extradata");
2414
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8571 times.
8571 if (!f)
2415 return 0;
2416
2417
1/2
✓ Branch 0 taken 8571 times.
✗ Branch 1 not taken.
8571 if (f->codec_ids) {
2418 const enum AVCodecID *ids;
2419
2/2
✓ Branch 0 taken 90703 times.
✓ Branch 1 taken 7699 times.
98402 for (ids = f->codec_ids; *ids != AV_CODEC_ID_NONE; ids++)
2420
2/2
✓ Branch 0 taken 872 times.
✓ Branch 1 taken 89831 times.
90703 if (*ids == st->codecpar->codec_id)
2421 872 return 1;
2422 }
2423
2424 7699 return 0;
2425 }
2426
2427 6699 static int extract_extradata_init(AVStream *st)
2428 {
2429 6699 FFStream *const sti = ffstream(st);
2430 const AVBitStreamFilter *f;
2431 int ret;
2432
2433 6699 f = av_bsf_get_by_name("extract_extradata");
2434
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6699 times.
6699 if (!f)
2435 goto finish;
2436
2437 /* check that the codec id is supported */
2438 6699 ret = extract_extradata_check(st);
2439
2/2
✓ Branch 0 taken 5937 times.
✓ Branch 1 taken 762 times.
6699 if (!ret)
2440 5937 goto finish;
2441
2442 762 av_bsf_free(&sti->extract_extradata.bsf);
2443 762 ret = av_bsf_alloc(f, &sti->extract_extradata.bsf);
2444
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 762 times.
762 if (ret < 0)
2445 return ret;
2446
2447 762 ret = avcodec_parameters_copy(sti->extract_extradata.bsf->par_in,
2448 762 st->codecpar);
2449
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 762 times.
762 if (ret < 0)
2450 goto fail;
2451
2452 762 sti->extract_extradata.bsf->time_base_in = st->time_base;
2453
2454 762 ret = av_bsf_init(sti->extract_extradata.bsf);
2455
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 762 times.
762 if (ret < 0)
2456 goto fail;
2457
2458 762 finish:
2459 6699 sti->extract_extradata.inited = 1;
2460
2461 6699 return 0;
2462 fail:
2463 av_bsf_free(&sti->extract_extradata.bsf);
2464 return ret;
2465 }
2466
2467 152595 static int extract_extradata(FFFormatContext *si, AVStream *st, const AVPacket *pkt)
2468 {
2469 152595 FFStream *const sti = ffstream(st);
2470 152595 AVPacket *const pkt_ref = si->parse_pkt;
2471 int ret;
2472
2473
2/2
✓ Branch 0 taken 6699 times.
✓ Branch 1 taken 145896 times.
152595 if (!sti->extract_extradata.inited) {
2474 6699 ret = extract_extradata_init(st);
2475
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6699 times.
6699 if (ret < 0)
2476 return ret;
2477 }
2478
2479
3/4
✓ Branch 0 taken 152595 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 151454 times.
✓ Branch 3 taken 1141 times.
152595 if (sti->extract_extradata.inited && !sti->extract_extradata.bsf)
2480 151454 return 0;
2481
2482 1141 ret = av_packet_ref(pkt_ref, pkt);
2483
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1141 times.
1141 if (ret < 0)
2484 return ret;
2485
2486 1141 ret = av_bsf_send_packet(sti->extract_extradata.bsf, pkt_ref);
2487
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1141 times.
1141 if (ret < 0) {
2488 av_packet_unref(pkt_ref);
2489 return ret;
2490 }
2491
2492
4/4
✓ Branch 0 taken 2282 times.
✓ Branch 1 taken 376 times.
✓ Branch 2 taken 1517 times.
✓ Branch 3 taken 765 times.
2658 while (ret >= 0 && !sti->avctx->extradata) {
2493 1517 ret = av_bsf_receive_packet(sti->extract_extradata.bsf, pkt_ref);
2494
2/2
✓ Branch 0 taken 376 times.
✓ Branch 1 taken 1141 times.
1517 if (ret < 0) {
2495
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 376 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
376 if (ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
2496 return ret;
2497 376 continue;
2498 }
2499
2500
2/2
✓ Branch 0 taken 1005 times.
✓ Branch 1 taken 376 times.
1381 for (int i = 0; i < pkt_ref->side_data_elems; i++) {
2501 1005 AVPacketSideData *const side_data = &pkt_ref->side_data[i];
2502
2/2
✓ Branch 0 taken 765 times.
✓ Branch 1 taken 240 times.
1005 if (side_data->type == AV_PKT_DATA_NEW_EXTRADATA) {
2503 765 sti->avctx->extradata = side_data->data;
2504 765 sti->avctx->extradata_size = side_data->size;
2505 765 side_data->data = NULL;
2506 765 side_data->size = 0;
2507 765 break;
2508 }
2509 }
2510 1141 av_packet_unref(pkt_ref);
2511 }
2512
2513 1141 return 0;
2514 }
2515
2516 7146 int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
2517 {
2518 7146 FFFormatContext *const si = ffformatcontext(ic);
2519 7146 int count = 0, ret = 0;
2520 int64_t read_size;
2521 7146 AVPacket *pkt1 = si->pkt;
2522 7146 int64_t old_offset = avio_tell(ic->pb);
2523 // new streams might appear, no options for those
2524 7146 int orig_nb_streams = ic->nb_streams;
2525 int flush_codecs;
2526 7146 int64_t max_analyze_duration = ic->max_analyze_duration;
2527 int64_t max_stream_analyze_duration;
2528 int64_t max_subtitle_analyze_duration;
2529 7146 int64_t probesize = ic->probesize;
2530 7146 int eof_reached = 0;
2531 7146 int *missing_streams = av_opt_ptr(ic->iformat->priv_class, ic->priv_data, "missing_streams");
2532
2533 7146 flush_codecs = probesize > 0;
2534
2535 7146 av_opt_set_int(ic, "skip_clear", 1, AV_OPT_SEARCH_CHILDREN);
2536
2537 7146 max_stream_analyze_duration = max_analyze_duration;
2538 7146 max_subtitle_analyze_duration = max_analyze_duration;
2539
2/2
✓ Branch 0 taken 7145 times.
✓ Branch 1 taken 1 times.
7146 if (!max_analyze_duration) {
2540 7145 max_stream_analyze_duration =
2541 7145 max_analyze_duration = 5*AV_TIME_BASE;
2542 7145 max_subtitle_analyze_duration = 30*AV_TIME_BASE;
2543
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 7112 times.
7145 if (!strcmp(ic->iformat->name, "flv"))
2544 33 max_stream_analyze_duration = 90*AV_TIME_BASE;
2545
4/4
✓ Branch 0 taken 7120 times.
✓ Branch 1 taken 25 times.
✓ Branch 2 taken 43 times.
✓ Branch 3 taken 7077 times.
7145 if (!strcmp(ic->iformat->name, "mpeg") || !strcmp(ic->iformat->name, "mpegts"))
2546 68 max_stream_analyze_duration = 7*AV_TIME_BASE;
2547 }
2548
2549
2/2
✓ Branch 0 taken 4412 times.
✓ Branch 1 taken 2734 times.
7146 if (ic->pb) {
2550 4412 FFIOContext *const ctx = ffiocontext(ic->pb);
2551 4412 av_log(ic, AV_LOG_DEBUG, "Before avformat_find_stream_info() pos: %"PRId64" bytes read:%"PRId64" seeks:%d nb_streams:%d\n",
2552 avio_tell(ic->pb), ctx->bytes_read, ctx->seek_count, ic->nb_streams);
2553 }
2554
2555
2/2
✓ Branch 0 taken 7732 times.
✓ Branch 1 taken 7146 times.
14878 for (unsigned i = 0; i < ic->nb_streams; i++) {
2556 const AVCodec *codec;
2557 7732 AVDictionary *thread_opt = NULL;
2558 7732 AVStream *const st = ic->streams[i];
2559 7732 FFStream *const sti = ffstream(st);
2560 7732 AVCodecContext *const avctx = sti->avctx;
2561
2562 /* check if the caller has overridden the codec id */
2563 // only for the split stuff
2564
4/6
✓ Branch 0 taken 7732 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7732 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 7231 times.
✓ Branch 5 taken 501 times.
7732 if (!sti->parser && !(ic->flags & AVFMT_FLAG_NOPARSE) && sti->request_probe <= 0) {
2565 7231 sti->parser = av_parser_init(st->codecpar->codec_id);
2566
2/2
✓ Branch 0 taken 4908 times.
✓ Branch 1 taken 2323 times.
7231 if (sti->parser) {
2567
2/2
✓ Branch 0 taken 702 times.
✓ Branch 1 taken 4206 times.
4908 if (sti->need_parsing == AVSTREAM_PARSE_HEADERS) {
2568 702 sti->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES;
2569
2/2
✓ Branch 0 taken 801 times.
✓ Branch 1 taken 3405 times.
4206 } else if (sti->need_parsing == AVSTREAM_PARSE_FULL_RAW) {
2570 801 sti->parser->flags |= PARSER_FLAG_USE_CODEC_TS;
2571 }
2572
2/2
✓ Branch 0 taken 682 times.
✓ Branch 1 taken 1641 times.
2323 } else if (sti->need_parsing) {
2573 682 av_log(ic, AV_LOG_VERBOSE, "parser not found for codec "
2574 "%s, packets or times may be invalid.\n",
2575 682 avcodec_get_name(st->codecpar->codec_id));
2576 }
2577 }
2578
2579 7732 ret = avcodec_parameters_to_context(avctx, st->codecpar);
2580
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7732 times.
7732 if (ret < 0)
2581 goto find_stream_info_err;
2582
2/2
✓ Branch 0 taken 7231 times.
✓ Branch 1 taken 501 times.
7732 if (sti->request_probe <= 0)
2583 7231 sti->avctx_inited = 1;
2584
2585 7732 codec = find_probe_decoder(ic, st, st->codecpar->codec_id);
2586
2587 /* Force thread count to 1 since the H.264 decoder will not extract
2588 * SPS and PPS to extradata during multi-threaded decoding. */
2589
2/2
✓ Branch 0 taken 7448 times.
✓ Branch 1 taken 284 times.
7732 av_dict_set(options ? &options[i] : &thread_opt, "threads", "1", 0);
2590 /* Force lowres to 0. The decoder might reduce the video size by the
2591 * lowres factor, and we don't want that propagated to the stream's
2592 * codecpar */
2593
2/2
✓ Branch 0 taken 7448 times.
✓ Branch 1 taken 284 times.
7732 av_dict_set(options ? &options[i] : &thread_opt, "lowres", "0", 0);
2594
2595
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7732 times.
7732 if (ic->codec_whitelist)
2596 av_dict_set(options ? &options[i] : &thread_opt, "codec_whitelist", ic->codec_whitelist, 0);
2597
2598 // Try to just open decoders, in case this is enough to get parameters.
2599 // Also ensure that subtitle_header is properly set.
2600
4/4
✓ Branch 1 taken 6947 times.
✓ Branch 2 taken 785 times.
✓ Branch 3 taken 500 times.
✓ Branch 4 taken 6447 times.
7732 if (!has_codec_parameters(st, NULL) && sti->request_probe <= 0 ||
2601
2/2
✓ Branch 0 taken 78 times.
✓ Branch 1 taken 1207 times.
1285 st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) {
2602
3/4
✓ Branch 0 taken 6506 times.
✓ Branch 1 taken 19 times.
✓ Branch 2 taken 6506 times.
✗ Branch 3 not taken.
6525 if (codec && !avctx->codec)
2603
4/4
✓ Branch 0 taken 6226 times.
✓ Branch 1 taken 280 times.
✓ Branch 3 taken 32 times.
✓ Branch 4 taken 6474 times.
6506 if (avcodec_open2(avctx, codec, options ? &options[i] : &thread_opt) < 0)
2604 32 av_log(ic, AV_LOG_WARNING,
2605 "Failed to open codec in %s\n", __func__);
2606 }
2607
2/2
✓ Branch 0 taken 284 times.
✓ Branch 1 taken 7448 times.
7732 if (!options)
2608 284 av_dict_free(&thread_opt);
2609 }
2610
2611 7146 read_size = 0;
2612 181467 for (;;) {
2613 const AVPacket *pkt;
2614 AVStream *st;
2615 FFStream *sti;
2616 AVCodecContext *avctx;
2617 int analyzed_all_streams;
2618 unsigned i;
2619
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 188613 times.
188613 if (ff_check_interrupt(&ic->interrupt_callback)) {
2620 ret = AVERROR_EXIT;
2621 av_log(ic, AV_LOG_DEBUG, "interrupted\n");
2622 break;
2623 }
2624
2625 /* check if one codec still needs to be handled */
2626
2/2
✓ Branch 0 taken 197073 times.
✓ Branch 1 taken 100537 times.
297610 for (i = 0; i < ic->nb_streams; i++) {
2627 197073 AVStream *const st = ic->streams[i];
2628 197073 FFStream *const sti = ffstream(st);
2629 197073 int fps_analyze_framecount = 20;
2630 int count;
2631
2632
2/2
✓ Branch 1 taken 8921 times.
✓ Branch 2 taken 188152 times.
197073 if (!has_codec_parameters(st, NULL))
2633 8921 break;
2634 /* If the timebase is coarse (like the usual millisecond precision
2635 * of mkv), we need to analyze more frames to reliably arrive at
2636 * the correct fps. */
2637
2/2
✓ Branch 1 taken 108987 times.
✓ Branch 2 taken 79165 times.
188152 if (av_q2d(st->time_base) > 0.0005)
2638 108987 fps_analyze_framecount *= 2;
2639
2/2
✓ Branch 1 taken 8290 times.
✓ Branch 2 taken 179862 times.
188152 if (!tb_unreliable(ic, st))
2640 8290 fps_analyze_framecount = 0;
2641
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 188152 times.
188152 if (ic->fps_probe_size >= 0)
2642 fps_analyze_framecount = ic->fps_probe_size;
2643
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 188108 times.
188152 if (st->disposition & AV_DISPOSITION_ATTACHED_PIC)
2644 44 fps_analyze_framecount = 0;
2645 /* variable fps and no guess at the real fps */
2646
2/2
✓ Branch 0 taken 18790 times.
✓ Branch 1 taken 169362 times.
188152 count = (ic->iformat->flags & AVFMT_NOTIMESTAMPS) ?
2647 18790 sti->info->codec_info_duration_fields/2 :
2648 169362 sti->info->duration_count;
2649
4/4
✓ Branch 0 taken 91319 times.
✓ Branch 1 taken 96833 times.
✓ Branch 2 taken 223 times.
✓ Branch 3 taken 91096 times.
188152 if (!(st->r_frame_rate.num && st->avg_frame_rate.num) &&
2650
2/2
✓ Branch 0 taken 40840 times.
✓ Branch 1 taken 56216 times.
97056 st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
2651
2/2
✓ Branch 0 taken 23155 times.
✓ Branch 1 taken 17685 times.
40840 if (count < fps_analyze_framecount)
2652 23155 break;
2653 }
2654 // Look at the first 3 frames if there is evidence of frame delay
2655 // but the decoder delay is not set.
2656
6/6
✓ Branch 0 taken 2894 times.
✓ Branch 1 taken 162103 times.
✓ Branch 2 taken 126 times.
✓ Branch 3 taken 2768 times.
✓ Branch 4 taken 12 times.
✓ Branch 5 taken 114 times.
164997 if (sti->info->frame_delay_evidence && count < 2 && sti->avctx->has_b_frames == 0)
2657 12 break;
2658
2/2
✓ Branch 0 taken 145176 times.
✓ Branch 1 taken 19809 times.
164985 if (!sti->avctx->extradata &&
2659
6/6
✓ Branch 0 taken 143414 times.
✓ Branch 1 taken 1762 times.
✓ Branch 2 taken 110 times.
✓ Branch 3 taken 143304 times.
✓ Branch 4 taken 110 times.
✓ Branch 5 taken 1762 times.
147048 (!sti->extract_extradata.inited || sti->extract_extradata.bsf) &&
2660 1872 extract_extradata_check(st))
2661 110 break;
2662
2/2
✓ Branch 0 taken 59699 times.
✓ Branch 1 taken 105176 times.
164875 if (sti->first_dts == AV_NOPTS_VALUE &&
2663
6/6
✓ Branch 0 taken 9146 times.
✓ Branch 1 taken 50553 times.
✓ Branch 2 taken 9087 times.
✓ Branch 3 taken 59 times.
✓ Branch 4 taken 56659 times.
✓ Branch 5 taken 2981 times.
119339 (!(ic->iformat->flags & AVFMT_NOTIMESTAMPS) || sti->need_parsing == AVSTREAM_PARSE_FULL_RAW) &&
2664
2/2
✓ Branch 0 taken 59596 times.
✓ Branch 1 taken 44 times.
59640 sti->codec_info_nb_frames < ((st->disposition & AV_DISPOSITION_ATTACHED_PIC) ? 1 : ic->max_ts_probe) &&
2665
2/2
✓ Branch 0 taken 41229 times.
✓ Branch 1 taken 15430 times.
56659 (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ||
2666
2/2
✓ Branch 0 taken 781 times.
✓ Branch 1 taken 40448 times.
41229 st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO))
2667 break;
2668 }
2669 188613 analyzed_all_streams = 0;
2670
4/4
✓ Branch 0 taken 1982 times.
✓ Branch 1 taken 186631 times.
✓ Branch 2 taken 1948 times.
✓ Branch 3 taken 34 times.
188613 if (!missing_streams || !*missing_streams)
2671
2/2
✓ Branch 0 taken 100503 times.
✓ Branch 1 taken 88076 times.
188579 if (i == ic->nb_streams) {
2672 100503 analyzed_all_streams = 1;
2673 /* NOTE: If the format has no header, then we need to read some
2674 * packets to get most of the streams, so we cannot stop here. */
2675
2/2
✓ Branch 0 taken 3222 times.
✓ Branch 1 taken 97281 times.
100503 if (!(ic->ctx_flags & AVFMTCTX_NOHEADER)) {
2676 /* If we found the info for all the codecs, we can stop. */
2677 3222 ret = count;
2678 3222 av_log(ic, AV_LOG_DEBUG, "All info found\n");
2679 3222 flush_codecs = 0;
2680 3222 break;
2681 }
2682 }
2683 /* We did not get all the codec info, but we read too much data. */
2684
2/2
✓ Branch 0 taken 2672 times.
✓ Branch 1 taken 182719 times.
185391 if (read_size >= probesize) {
2685 2672 ret = count;
2686 2672 av_log(ic, AV_LOG_DEBUG,
2687 "Probe buffer size limit of %"PRId64" bytes reached\n", probesize);
2688
2/2
✓ Branch 0 taken 2686 times.
✓ Branch 1 taken 2672 times.
5358 for (unsigned i = 0; i < ic->nb_streams; i++) {
2689 2686 AVStream *const st = ic->streams[i];
2690 2686 FFStream *const sti = ffstream(st);
2691
2/2
✓ Branch 0 taken 58 times.
✓ Branch 1 taken 2628 times.
2686 if (!st->r_frame_rate.num &&
2692
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 43 times.
58 sti->info->duration_count <= 1 &&
2693
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 14 times.
15 st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
2694
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 strcmp(ic->iformat->name, "image2"))
2695 1 av_log(ic, AV_LOG_WARNING,
2696 "Stream #%d: not enough frames to estimate rate; "
2697 "consider increasing probesize\n", i);
2698 }
2699 2672 break;
2700 }
2701
2702 /* NOTE: A new stream can be added there if no header in file
2703 * (AVFMTCTX_NOHEADER). */
2704 182719 ret = read_frame_internal(ic, pkt1);
2705
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 182719 times.
182719 if (ret == AVERROR(EAGAIN))
2706 continue;
2707
2708
2/2
✓ Branch 0 taken 1158 times.
✓ Branch 1 taken 181561 times.
182719 if (ret < 0) {
2709 /* EOF or error*/
2710 1158 eof_reached = 1;
2711 1158 break;
2712 }
2713
2714
1/2
✓ Branch 0 taken 181561 times.
✗ Branch 1 not taken.
181561 if (!(ic->flags & AVFMT_FLAG_NOBUFFER)) {
2715 181561 ret = avpriv_packet_list_put(&si->packet_buffer,
2716 pkt1, NULL, 0);
2717
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 181561 times.
181561 if (ret < 0)
2718 goto unref_then_goto_end;
2719
2720 181561 pkt = &si->packet_buffer.tail->pkt;
2721 } else {
2722 pkt = pkt1;
2723 }
2724
2725 181561 st = ic->streams[pkt->stream_index];
2726 181561 sti = ffstream(st);
2727
2/2
✓ Branch 0 taken 181505 times.
✓ Branch 1 taken 56 times.
181561 if (!(st->disposition & AV_DISPOSITION_ATTACHED_PIC))
2728 181505 read_size += pkt->size;
2729
2730 181561 avctx = sti->avctx;
2731
2/2
✓ Branch 0 taken 639 times.
✓ Branch 1 taken 180922 times.
181561 if (!sti->avctx_inited) {
2732 639 ret = avcodec_parameters_to_context(avctx, st->codecpar);
2733
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 639 times.
639 if (ret < 0)
2734 goto unref_then_goto_end;
2735 639 sti->avctx_inited = 1;
2736 }
2737
2738
4/4
✓ Branch 0 taken 164532 times.
✓ Branch 1 taken 17029 times.
✓ Branch 2 taken 152968 times.
✓ Branch 3 taken 11564 times.
181561 if (pkt->dts != AV_NOPTS_VALUE && sti->codec_info_nb_frames > 1) {
2739 /* check for non-increasing dts */
2740
2/2
✓ Branch 0 taken 148450 times.
✓ Branch 1 taken 4518 times.
152968 if (sti->info->fps_last_dts != AV_NOPTS_VALUE &&
2741
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 148427 times.
148450 sti->info->fps_last_dts >= pkt->dts) {
2742 23 av_log(ic, AV_LOG_DEBUG,
2743 "Non-increasing DTS in stream %d: packet %d with DTS "
2744 "%"PRId64", packet %d with DTS %"PRId64"\n",
2745 23 st->index, sti->info->fps_last_dts_idx,
2746 23 sti->info->fps_last_dts, sti->codec_info_nb_frames,
2747 23 pkt->dts);
2748 23 sti->info->fps_first_dts =
2749 23 sti->info->fps_last_dts = AV_NOPTS_VALUE;
2750 }
2751 /* Check for a discontinuity in dts. If the difference in dts
2752 * is more than 1000 times the average packet duration in the
2753 * sequence, we treat it as a discontinuity. */
2754
2/2
✓ Branch 0 taken 148427 times.
✓ Branch 1 taken 4541 times.
152968 if (sti->info->fps_last_dts != AV_NOPTS_VALUE &&
2755
2/2
✓ Branch 0 taken 143937 times.
✓ Branch 1 taken 4490 times.
148427 sti->info->fps_last_dts_idx > sti->info->fps_first_dts_idx &&
2756 143937 (pkt->dts - (uint64_t)sti->info->fps_last_dts) / 1000 >
2757 143937 (sti->info->fps_last_dts - (uint64_t)sti->info->fps_first_dts) /
2758
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 143937 times.
143937 (sti->info->fps_last_dts_idx - sti->info->fps_first_dts_idx)) {
2759 av_log(ic, AV_LOG_WARNING,
2760 "DTS discontinuity in stream %d: packet %d with DTS "
2761 "%"PRId64", packet %d with DTS %"PRId64"\n",
2762 st->index, sti->info->fps_last_dts_idx,
2763 sti->info->fps_last_dts, sti->codec_info_nb_frames,
2764 pkt->dts);
2765 sti->info->fps_first_dts =
2766 sti->info->fps_last_dts = AV_NOPTS_VALUE;
2767 }
2768
2769 /* update stored dts values */
2770
2/2
✓ Branch 0 taken 4541 times.
✓ Branch 1 taken 148427 times.
152968 if (sti->info->fps_first_dts == AV_NOPTS_VALUE) {
2771 4541 sti->info->fps_first_dts = pkt->dts;
2772 4541 sti->info->fps_first_dts_idx = sti->codec_info_nb_frames;
2773 }
2774 152968 sti->info->fps_last_dts = pkt->dts;
2775 152968 sti->info->fps_last_dts_idx = sti->codec_info_nb_frames;
2776 }
2777
2/2
✓ Branch 0 taken 168852 times.
✓ Branch 1 taken 12709 times.
181561 if (sti->codec_info_nb_frames > 1) {
2778 168852 int64_t t = 0;
2779 int64_t limit;
2780
2781
1/2
✓ Branch 0 taken 168852 times.
✗ Branch 1 not taken.
168852 if (st->time_base.den > 0)
2782 168852 t = av_rescale_q(sti->info->codec_info_duration, st->time_base, AV_TIME_BASE_Q);
2783
2/2
✓ Branch 0 taken 102770 times.
✓ Branch 1 taken 66082 times.
168852 if (st->avg_frame_rate.num > 0)
2784
2/2
✓ Branch 1 taken 102243 times.
✓ Branch 2 taken 527 times.
102770 t = FFMAX(t, av_rescale_q(sti->codec_info_nb_frames, av_inv_q(st->avg_frame_rate), AV_TIME_BASE_Q));
2785
2786
2/2
✓ Branch 0 taken 4766 times.
✓ Branch 1 taken 164086 times.
168852 if ( t == 0
2787
2/2
✓ Branch 0 taken 1507 times.
✓ Branch 1 taken 3259 times.
4766 && sti->codec_info_nb_frames > 30
2788
2/2
✓ Branch 0 taken 328 times.
✓ Branch 1 taken 1179 times.
1507 && sti->info->fps_first_dts != AV_NOPTS_VALUE
2789
1/2
✓ Branch 0 taken 328 times.
✗ Branch 1 not taken.
328 && sti->info->fps_last_dts != AV_NOPTS_VALUE) {
2790 328 int64_t dur = av_sat_sub64(sti->info->fps_last_dts, sti->info->fps_first_dts);
2791
1/2
✓ Branch 0 taken 328 times.
✗ Branch 1 not taken.
328 t = FFMAX(t, av_rescale_q(dur, st->time_base, AV_TIME_BASE_Q));
2792 }
2793
2794
2/2
✓ Branch 0 taken 91679 times.
✓ Branch 1 taken 77173 times.
168852 if (analyzed_all_streams) limit = max_analyze_duration;
2795
2/2
✓ Branch 0 taken 91 times.
✓ Branch 1 taken 77082 times.
77173 else if (avctx->codec_type == AVMEDIA_TYPE_SUBTITLE) limit = max_subtitle_analyze_duration;
2796 77082 else limit = max_stream_analyze_duration;
2797
2798
2/2
✓ Branch 0 taken 94 times.
✓ Branch 1 taken 168758 times.
168852 if (t >= limit) {
2799 94 av_log(ic, AV_LOG_VERBOSE, "max_analyze_duration %"PRId64" reached at %"PRId64" microseconds st:%d\n",
2800 limit,
2801 94 t, pkt->stream_index);
2802
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 94 times.
94 if (ic->flags & AVFMT_FLAG_NOBUFFER)
2803 av_packet_unref(pkt1);
2804 94 break;
2805 }
2806
2/2
✓ Branch 0 taken 164418 times.
✓ Branch 1 taken 4340 times.
168758 if (pkt->duration > 0) {
2807
4/4
✓ Branch 0 taken 164374 times.
✓ Branch 1 taken 44 times.
✓ Branch 2 taken 13924 times.
✓ Branch 3 taken 150450 times.
164418 const int fields = sti->codec_desc && (sti->codec_desc->props & AV_CODEC_PROP_FIELDS);
2808
1/8
✗ Branch 0 not taken.
✓ Branch 1 taken 164418 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.
164418 if (avctx->codec_type == AVMEDIA_TYPE_SUBTITLE && pkt->pts != AV_NOPTS_VALUE && st->start_time != AV_NOPTS_VALUE && pkt->pts >= st->start_time
2809 && (uint64_t)pkt->pts - st->start_time < INT64_MAX
2810 ) {
2811 sti->info->codec_info_duration = FFMIN(pkt->pts - st->start_time, sti->info->codec_info_duration + pkt->duration);
2812 } else
2813 164418 sti->info->codec_info_duration += pkt->duration;
2814
4/4
✓ Branch 0 taken 34186 times.
✓ Branch 1 taken 82553 times.
✓ Branch 2 taken 13552 times.
✓ Branch 3 taken 20634 times.
281157 sti->info->codec_info_duration_fields += sti->parser && sti->need_parsing && fields
2815
2/2
✓ Branch 0 taken 116739 times.
✓ Branch 1 taken 47679 times.
281157 ? sti->parser->repeat_pict + 1 : 2;
2816 }
2817 }
2818
2/2
✓ Branch 0 taken 120110 times.
✓ Branch 1 taken 61357 times.
181467 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
2819 #if FF_API_R_FRAME_RATE
2820 120110 ff_rfps_add_frame(ic, st, pkt->dts);
2821 #endif
2822
6/6
✓ Branch 0 taken 3954 times.
✓ Branch 1 taken 116156 times.
✓ Branch 2 taken 3715 times.
✓ Branch 3 taken 239 times.
✓ Branch 4 taken 1689 times.
✓ Branch 5 taken 2026 times.
120110 if (pkt->dts != pkt->pts && pkt->dts != AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE)
2823 1689 sti->info->frame_delay_evidence = 1;
2824 }
2825
2/2
✓ Branch 0 taken 152451 times.
✓ Branch 1 taken 29016 times.
181467 if (!sti->avctx->extradata) {
2826 152451 ret = extract_extradata(si, st, pkt);
2827
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 152451 times.
152451 if (ret < 0)
2828 goto unref_then_goto_end;
2829 }
2830
2831 /* If still no information, we try to open the codec and to
2832 * decompress the frame. We try to avoid that in most cases as
2833 * it takes longer and uses more memory. For MPEG-4, we need to
2834 * decompress for QuickTime.
2835 *
2836 * If AV_CODEC_CAP_CHANNEL_CONF is set this will force decoding of at
2837 * least one frame of codec data, this makes sure the codec initializes
2838 * the channel configuration and does not only trust the values from
2839 * the container. */
2840
2/2
✓ Branch 0 taken 164660 times.
✓ Branch 1 taken 16807 times.
346127 try_decode_frame(ic, st, pkt,
2841
2/2
✓ Branch 0 taken 78301 times.
✓ Branch 1 taken 86359 times.
164660 (options && i < orig_nb_streams) ? &options[i] : NULL);
2842
2843
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 181467 times.
181467 if (ic->flags & AVFMT_FLAG_NOBUFFER)
2844 av_packet_unref(pkt1);
2845
2846 181467 sti->codec_info_nb_frames++;
2847 181467 count++;
2848 }
2849
2850
2/2
✓ Branch 0 taken 1158 times.
✓ Branch 1 taken 5988 times.
7146 if (eof_reached) {
2851
2/2
✓ Branch 0 taken 1410 times.
✓ Branch 1 taken 1158 times.
2568 for (unsigned stream_index = 0; stream_index < ic->nb_streams; stream_index++) {
2852 1410 AVStream *const st = ic->streams[stream_index];
2853 1410 AVCodecContext *const avctx = ffstream(st)->avctx;
2854
2/2
✓ Branch 1 taken 24 times.
✓ Branch 2 taken 1386 times.
1410 if (!has_codec_parameters(st, NULL)) {
2855 24 const AVCodec *codec = find_probe_decoder(ic, st, st->codecpar->codec_id);
2856
4/4
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 12 times.
24 if (codec && !avctx->codec) {
2857 6 AVDictionary *opts = NULL;
2858
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (ic->codec_whitelist)
2859 av_dict_set(&opts, "codec_whitelist", ic->codec_whitelist, 0);
2860
4/6
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 4 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 6 times.
6 if (avcodec_open2(avctx, codec, (options && stream_index < orig_nb_streams) ? &options[stream_index] : &opts) < 0)
2861 av_log(ic, AV_LOG_WARNING,
2862 "Failed to open codec in %s\n", __func__);
2863 6 av_dict_free(&opts);
2864 }
2865 }
2866
2867 // EOF already reached while reading the stream above.
2868 // So continue with reoordering DTS with whatever delay we have.
2869
4/4
✓ Branch 0 taken 1392 times.
✓ Branch 1 taken 18 times.
✓ Branch 3 taken 18 times.
✓ Branch 4 taken 1374 times.
1410 if (si->packet_buffer.head && !has_decode_delay_been_guessed(st)) {
2870 18 update_dts_from_pts(ic, stream_index, si->packet_buffer.head);
2871 }
2872 }
2873 }
2874
2875
2/2
✓ Branch 0 taken 3924 times.
✓ Branch 1 taken 3222 times.
7146 if (flush_codecs) {
2876 3924 AVPacket *empty_pkt = si->pkt;
2877 3924 int err = 0;
2878 3924 av_packet_unref(empty_pkt);
2879
2880
2/2
✓ Branch 0 taken 4228 times.
✓ Branch 1 taken 3924 times.
8152 for (unsigned i = 0; i < ic->nb_streams; i++) {
2881 4228 AVStream *const st = ic->streams[i];
2882 4228 FFStream *const sti = ffstream(st);
2883
2884 /* flush the decoders */
2885
2/2
✓ Branch 0 taken 4135 times.
✓ Branch 1 taken 93 times.
4228 if (sti->info->found_decoder == 1) {
2886
2/2
✓ Branch 0 taken 3850 times.
✓ Branch 1 taken 285 times.
7985 err = try_decode_frame(ic, st, empty_pkt,
2887
2/2
✓ Branch 0 taken 3836 times.
✓ Branch 1 taken 14 times.
3850 (options && i < orig_nb_streams)
2888 3836 ? &options[i] : NULL);
2889
2890
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4135 times.
4135 if (err < 0) {
2891 av_log(ic, AV_LOG_INFO,
2892 "decoding for stream %d failed\n", st->index);
2893 }
2894 }
2895 }
2896 }
2897
2898 7146 ff_rfps_calculate(ic);
2899
2900
2/2
✓ Branch 0 taken 7880 times.
✓ Branch 1 taken 7146 times.
15026 for (unsigned i = 0; i < ic->nb_streams; i++) {
2901 7880 AVStream *const st = ic->streams[i];
2902 7880 FFStream *const sti = ffstream(st);
2903 7880 AVCodecContext *const avctx = sti->avctx;
2904
2905
2/2
✓ Branch 0 taken 5819 times.
✓ Branch 1 taken 2061 times.
7880 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
2906
6/6
✓ Branch 0 taken 596 times.
✓ Branch 1 taken 5223 times.
✓ Branch 2 taken 560 times.
✓ Branch 3 taken 36 times.
✓ Branch 4 taken 544 times.
✓ Branch 5 taken 16 times.
5819 if (avctx->codec_id == AV_CODEC_ID_RAWVIDEO && !avctx->codec_tag && !avctx->bits_per_coded_sample) {
2907 544 uint32_t tag= avcodec_pix_fmt_to_codec_tag(avctx->pix_fmt);
2908
2/2
✓ Branch 1 taken 540 times.
✓ Branch 2 taken 4 times.
544 if (avpriv_pix_fmt_find(PIX_FMT_LIST_RAW, tag) == avctx->pix_fmt)
2909 540 avctx->codec_tag= tag;
2910 }
2911
2912 /* estimate average framerate if not set by demuxer */
2913
2/2
✓ Branch 0 taken 3661 times.
✓ Branch 1 taken 2158 times.
5819 if (sti->info->codec_info_duration_fields &&
2914
2/2
✓ Branch 0 taken 262 times.
✓ Branch 1 taken 3399 times.
3661 !st->avg_frame_rate.num &&
2915
1/2
✓ Branch 0 taken 262 times.
✗ Branch 1 not taken.
262 sti->info->codec_info_duration) {
2916 262 int best_fps = 0;
2917 262 double best_error = 0.01;
2918 262 AVRational codec_frame_rate = avctx->framerate;
2919
2920
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||
2921
1/2
✓ Branch 0 taken 262 times.
✗ Branch 1 not taken.
262 sti->info->codec_info_duration_fields >= INT64_MAX / st->time_base.den ||
2922
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 262 times.
262 sti->info->codec_info_duration < 0)
2923 continue;
2924 262 av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den,
2925 262 sti->info->codec_info_duration_fields * (int64_t) st->time_base.den,
2926 262 sti->info->codec_info_duration * 2 * (int64_t) st->time_base.num, 60000);
2927
2928 /* Round guessed framerate to a "standard" framerate if it's
2929 * within 1% of the original estimate. */
2930
2/2
✓ Branch 0 taken 104538 times.
✓ Branch 1 taken 262 times.
104800 for (int j = 0; j < MAX_STD_TIMEBASES; j++) {
2931 104538 AVRational std_fps = { get_std_framerate(j), 12 * 1001 };
2932 104538 double error = fabs(av_q2d(st->avg_frame_rate) /
2933 104538 av_q2d(std_fps) - 1);
2934
2935
2/2
✓ Branch 0 taken 710 times.
✓ Branch 1 taken 103828 times.
104538 if (error < best_error) {
2936 710 best_error = error;
2937 710 best_fps = std_fps.num;
2938 }
2939
2940
4/6
✓ Branch 0 taken 12768 times.
✓ Branch 1 taken 91770 times.
✓ Branch 2 taken 12768 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 12768 times.
✗ Branch 5 not taken.
104538 if (si->prefer_codec_framerate && codec_frame_rate.num > 0 && codec_frame_rate.den > 0) {
2941 12768 error = fabs(av_q2d(codec_frame_rate) /
2942 12768 av_q2d(std_fps) - 1);
2943
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 12758 times.
12768 if (error < best_error) {
2944 10 best_error = error;
2945 10 best_fps = std_fps.num;
2946 }
2947 }
2948 }
2949
2/2
✓ Branch 0 taken 260 times.
✓ Branch 1 taken 2 times.
262 if (best_fps)
2950 260 av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den,
2951 best_fps, 12 * 1001, INT_MAX);
2952 }
2953
2/2
✓ Branch 0 taken 1778 times.
✓ Branch 1 taken 4041 times.
5819 if (!st->r_frame_rate.num) {
2954 1778 const AVCodecDescriptor *desc = sti->codec_desc;
2955
3/4
✓ Branch 0 taken 1778 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 244 times.
✓ Branch 3 taken 1534 times.
1778 AVRational mul = (AVRational){ desc && (desc->props & AV_CODEC_PROP_FIELDS) ? 2 : 1, 1 };
2956 1778 AVRational fr = av_mul_q(avctx->framerate, mul);
2957
2958
5/6
✓ Branch 0 taken 178 times.
✓ Branch 1 taken 1600 times.
✓ Branch 2 taken 178 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 171 times.
✓ Branch 7 taken 7 times.
1778 if (fr.num && fr.den && av_cmp_q(st->time_base, av_inv_q(fr)) <= 0) {
2959 171 st->r_frame_rate = fr;
2960 } else {
2961 1607 st->r_frame_rate.num = st->time_base.den;
2962 1607 st->r_frame_rate.den = st->time_base.num;
2963 }
2964 }
2965 5819 st->codecpar->framerate = avctx->framerate;
2966
3/4
✓ Branch 0 taken 116 times.
✓ Branch 1 taken 5703 times.
✓ Branch 2 taken 116 times.
✗ Branch 3 not taken.
5819 if (sti->display_aspect_ratio.num && sti->display_aspect_ratio.den) {
2967 116 AVRational hw_ratio = { avctx->height, avctx->width };
2968 116 st->sample_aspect_ratio = av_mul_q(sti->display_aspect_ratio,
2969 hw_ratio);
2970 }
2971
2/2
✓ Branch 0 taken 1896 times.
✓ Branch 1 taken 165 times.
2061 } else if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
2972
2/2
✓ Branch 0 taken 589 times.
✓ Branch 1 taken 1307 times.
1896 if (!avctx->bits_per_coded_sample)
2973 589 avctx->bits_per_coded_sample =
2974 589 av_get_bits_per_sample(avctx->codec_id);
2975 // set stream disposition based on audio service type
2976
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 1896 times.
1896 switch (avctx->audio_service_type) {
2977 case AV_AUDIO_SERVICE_TYPE_EFFECTS:
2978 st->disposition = AV_DISPOSITION_CLEAN_EFFECTS;
2979 break;
2980 case AV_AUDIO_SERVICE_TYPE_VISUALLY_IMPAIRED:
2981 st->disposition = AV_DISPOSITION_VISUAL_IMPAIRED;
2982 break;
2983 case AV_AUDIO_SERVICE_TYPE_HEARING_IMPAIRED:
2984 st->disposition = AV_DISPOSITION_HEARING_IMPAIRED;
2985 break;
2986 case AV_AUDIO_SERVICE_TYPE_COMMENTARY:
2987 st->disposition = AV_DISPOSITION_COMMENT;
2988 break;
2989 case AV_AUDIO_SERVICE_TYPE_KARAOKE:
2990 st->disposition = AV_DISPOSITION_KARAOKE;
2991 break;
2992 }
2993 }
2994 }
2995
2996
1/2
✓ Branch 0 taken 7146 times.
✗ Branch 1 not taken.
7146 if (probesize)
2997 7146 estimate_timings(ic, old_offset);
2998
2999 7146 av_opt_set_int(ic, "skip_clear", 0, AV_OPT_SEARCH_CHILDREN);
3000
3001
3/4
✓ Branch 0 taken 5988 times.
✓ Branch 1 taken 1158 times.
✓ Branch 2 taken 5988 times.
✗ Branch 3 not taken.
7146 if (ret >= 0 && ic->nb_streams)
3002 /* We could not have all the codec parameters before EOF. */
3003 5988 ret = -1;
3004
2/2
✓ Branch 0 taken 7880 times.
✓ Branch 1 taken 7146 times.
15026 for (unsigned i = 0; i < ic->nb_streams; i++) {
3005 7880 AVStream *const st = ic->streams[i];
3006 7880 FFStream *const sti = ffstream(st);
3007 const char *errmsg;
3008
3009 /* if no packet was ever seen, update context now for has_codec_parameters */
3010
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 7870 times.
7880 if (!sti->avctx_inited) {
3011
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 4 times.
10 if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
3012
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 st->codecpar->format == AV_SAMPLE_FMT_NONE)
3013 6 st->codecpar->format = sti->avctx->sample_fmt;
3014 10 ret = avcodec_parameters_to_context(sti->avctx, st->codecpar);
3015
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (ret < 0)
3016 goto find_stream_info_err;
3017 }
3018
2/2
✓ Branch 1 taken 32 times.
✓ Branch 2 taken 7848 times.
7880 if (!has_codec_parameters(st, &errmsg)) {
3019 char buf[256];
3020 32 avcodec_string(buf, sizeof(buf), sti->avctx, 0);
3021 32 av_log(ic, AV_LOG_WARNING,
3022 "Could not find codec parameters for stream %d (%s): %s\n"
3023 "Consider increasing the value for the 'analyzeduration' (%"PRId64") and 'probesize' (%"PRId64") options\n",
3024 i, buf, errmsg, ic->max_analyze_duration, ic->probesize);
3025 } else {
3026 7848 ret = 0;
3027 }
3028 }
3029
3030 7146 ret = compute_chapters_end(ic);
3031
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7146 times.
7146 if (ret < 0)
3032 goto find_stream_info_err;
3033
3034 /* update the stream parameters from the internal codec contexts */
3035
2/2
✓ Branch 0 taken 7880 times.
✓ Branch 1 taken 7146 times.
15026 for (unsigned i = 0; i < ic->nb_streams; i++) {
3036 7880 AVStream *const st = ic->streams[i];
3037 7880 FFStream *const sti = ffstream(st);
3038
3039
2/2
✓ Branch 0 taken 7870 times.
✓ Branch 1 taken 10 times.
7880 if (sti->avctx_inited) {
3040 7870 ret = avcodec_parameters_from_context(st->codecpar, sti->avctx);
3041
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7870 times.
7870 if (ret < 0)
3042 goto find_stream_info_err;
3043
3044
3/4
✓ Branch 0 taken 7686 times.
✓ Branch 1 taken 184 times.
✓ Branch 2 taken 7686 times.
✗ Branch 3 not taken.
7870 if (sti->avctx->rc_buffer_size > 0 || sti->avctx->rc_max_rate > 0 ||
3045
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7686 times.
7686 sti->avctx->rc_min_rate) {
3046 size_t cpb_size;
3047 184 AVCPBProperties *props = av_cpb_properties_alloc(&cpb_size);
3048
1/2
✓ Branch 0 taken 184 times.
✗ Branch 1 not taken.
184 if (props) {
3049
1/2
✓ Branch 0 taken 184 times.
✗ Branch 1 not taken.
184 if (sti->avctx->rc_buffer_size > 0)
3050 184 props->buffer_size = sti->avctx->rc_buffer_size;
3051
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 184 times.
184 if (sti->avctx->rc_min_rate > 0)
3052 props->min_bitrate = sti->avctx->rc_min_rate;
3053
2/2
✓ Branch 0 taken 77 times.
✓ Branch 1 taken 107 times.
184 if (sti->avctx->rc_max_rate > 0)
3054 77 props->max_bitrate = sti->avctx->rc_max_rate;
3055
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 184 times.
184 if (!av_packet_side_data_add(&st->codecpar->coded_side_data,
3056 184 &st->codecpar->nb_coded_side_data,
3057 AV_PKT_DATA_CPB_PROPERTIES,
3058 (uint8_t *)props, cpb_size, 0))
3059 av_free(props);
3060 }
3061 }
3062 }
3063
3064 7880 sti->avctx_inited = 0;
3065 #if FF_API_AVSTREAM_SIDE_DATA
3066 FF_DISABLE_DEPRECATION_WARNINGS
3067
2/2
✓ Branch 0 taken 465 times.
✓ Branch 1 taken 7415 times.
7880 if (st->codecpar->nb_coded_side_data > 0) {
3068
2/4
✓ Branch 0 taken 465 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 465 times.
465 av_assert0(!st->side_data && !st->nb_side_data);
3069 465 st->side_data = av_calloc(st->codecpar->nb_coded_side_data, sizeof(*st->side_data));
3070
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 465 times.
465 if (!st->side_data) {
3071 ret = AVERROR(ENOMEM);
3072 goto find_stream_info_err;
3073 }
3074
3075
2/2
✓ Branch 0 taken 491 times.
✓ Branch 1 taken 465 times.
956 for (int j = 0; j < st->codecpar->nb_coded_side_data; j++) {
3076 491 uint8_t *data = av_memdup(st->codecpar->coded_side_data[j].data,
3077 491 st->codecpar->coded_side_data[j].size);
3078
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 491 times.
491 if (!data) {
3079 ret = AVERROR(ENOMEM);
3080 goto find_stream_info_err;
3081 }
3082 491 st->side_data[j].type = st->codecpar->coded_side_data[j].type;
3083 491 st->side_data[j].size = st->codecpar->coded_side_data[j].size;
3084 491 st->side_data[j].data = data;
3085 491 st->nb_side_data++;
3086 }
3087 }
3088 FF_ENABLE_DEPRECATION_WARNINGS
3089 #endif
3090 }
3091
3092 7146 find_stream_info_err:
3093
2/2
✓ Branch 0 taken 7880 times.
✓ Branch 1 taken 7146 times.
15026 for (unsigned i = 0; i < ic->nb_streams; i++) {
3094 7880 AVStream *const st = ic->streams[i];
3095 7880 FFStream *const sti = ffstream(st);
3096 int err;
3097
3098
1/2
✓ Branch 0 taken 7880 times.
✗ Branch 1 not taken.
7880 if (sti->info) {
3099 7880 av_freep(&sti->info->duration_error);
3100 7880 av_freep(&sti->info);
3101 }
3102
3103 7880 err = codec_close(sti);
3104
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 7880 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
7880 if (err < 0 && ret >= 0)
3105 ret = err;
3106 7880 av_bsf_free(&sti->extract_extradata.bsf);
3107 }
3108
2/2
✓ Branch 0 taken 4412 times.
✓ Branch 1 taken 2734 times.
7146 if (ic->pb) {
3109 4412 FFIOContext *const ctx = ffiocontext(ic->pb);
3110 4412 av_log(ic, AV_LOG_DEBUG, "After avformat_find_stream_info() pos: %"PRId64" bytes read:%"PRId64" seeks:%d frames:%d\n",
3111 avio_tell(ic->pb), ctx->bytes_read, ctx->seek_count, count);
3112 }
3113 7146 return ret;
3114
3115 unref_then_goto_end:
3116 av_packet_unref(pkt1);
3117 goto find_stream_info_err;
3118 }
3119