FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/demux.c
Date: 2022-11-26 13:19:19
Exec Total Coverage
Lines: 1578 1822 86.6%
Branches: 1451 1789 81.1%

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/opt.h"
33 #include "libavutil/pixfmt.h"
34 #include "libavutil/time.h"
35 #include "libavutil/timestamp.h"
36
37 #include "libavcodec/avcodec.h"
38 #include "libavcodec/bsf.h"
39 #include "libavcodec/internal.h"
40 #include "libavcodec/packet_internal.h"
41 #include "libavcodec/raw.h"
42
43 #include "avformat.h"
44 #include "avio_internal.h"
45 #include "demux.h"
46 #include "id3v2.h"
47 #include "internal.h"
48 #include "url.h"
49
50 2439038 static int64_t wrap_timestamp(const AVStream *st, int64_t timestamp)
51 {
52 2439038 const FFStream *const sti = cffstream(st);
53
4/4
✓ Branch 0 taken 127899 times.
✓ Branch 1 taken 2311139 times.
✓ Branch 2 taken 123965 times.
✓ Branch 3 taken 3934 times.
2439038 if (sti->pts_wrap_behavior != AV_PTS_WRAP_IGNORE && st->pts_wrap_bits < 64 &&
54
3/4
✓ Branch 0 taken 123965 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 81500 times.
✓ Branch 3 taken 42465 times.
123965 sti->pts_wrap_reference != AV_NOPTS_VALUE && timestamp != AV_NOPTS_VALUE) {
55
2/2
✓ Branch 0 taken 81296 times.
✓ Branch 1 taken 204 times.
81500 if (sti->pts_wrap_behavior == AV_PTS_WRAP_ADD_OFFSET &&
56
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 81296 times.
81296 timestamp < sti->pts_wrap_reference)
57 return timestamp + (1ULL << st->pts_wrap_bits);
58
2/2
✓ Branch 0 taken 204 times.
✓ Branch 1 taken 81296 times.
81500 else if (sti->pts_wrap_behavior == AV_PTS_WRAP_SUB_OFFSET &&
59
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 154 times.
204 timestamp >= sti->pts_wrap_reference)
60 50 return timestamp - (1ULL << st->pts_wrap_bits);
61 }
62 2438988 return timestamp;
63 }
64
65 383128 int64_t ff_wrap_timestamp(const AVStream *st, int64_t timestamp)
66 {
67 383128 return wrap_timestamp(st, timestamp);
68 }
69
70 8651 static const AVCodec *find_probe_decoder(AVFormatContext *s, const AVStream *st, enum AVCodecID codec_id)
71 {
72 const AVCodec *codec;
73
74 #if CONFIG_H264_DECODER
75 /* Other parts of the code assume this decoder to be used for h264,
76 * so force it if possible. */
77
2/2
✓ Branch 0 taken 308 times.
✓ Branch 1 taken 8343 times.
8651 if (codec_id == AV_CODEC_ID_H264)
78 308 return avcodec_find_decoder_by_name("h264");
79 #endif
80
81 8343 codec = ff_find_decoder(s, st, codec_id);
82
2/2
✓ Branch 0 taken 139 times.
✓ Branch 1 taken 8204 times.
8343 if (!codec)
83 139 return NULL;
84
85
2/2
✓ Branch 0 taken 41 times.
✓ Branch 1 taken 8163 times.
8204 if (codec->capabilities & AV_CODEC_CAP_AVOID_PROBING) {
86 41 const AVCodec *probe_codec = NULL;
87 41 void *iter = NULL;
88
2/2
✓ Branch 1 taken 27798 times.
✓ Branch 2 taken 41 times.
27839 while ((probe_codec = av_codec_iterate(&iter))) {
89
3/4
✓ Branch 0 taken 41 times.
✓ Branch 1 taken 27757 times.
✓ Branch 2 taken 41 times.
✗ Branch 3 not taken.
27839 if (probe_codec->id == codec->id &&
90 41 av_codec_is_decoder(probe_codec) &&
91
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 41 times.
41 !(probe_codec->capabilities & (AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_EXPERIMENTAL))) {
92 return probe_codec;
93 }
94 }
95 }
96
97 8204 return codec;
98 }
99
100 2854 static int set_codec_from_probe_data(AVFormatContext *s, AVStream *st,
101 AVProbeData *pd)
102 {
103 static const struct {
104 const char *name;
105 enum AVCodecID id;
106 enum AVMediaType type;
107 } fmt_id_type[] = {
108 { "aac", AV_CODEC_ID_AAC, AVMEDIA_TYPE_AUDIO },
109 { "ac3", AV_CODEC_ID_AC3, AVMEDIA_TYPE_AUDIO },
110 { "aptx", AV_CODEC_ID_APTX, AVMEDIA_TYPE_AUDIO },
111 { "dts", AV_CODEC_ID_DTS, AVMEDIA_TYPE_AUDIO },
112 { "dvbsub", AV_CODEC_ID_DVB_SUBTITLE, AVMEDIA_TYPE_SUBTITLE },
113 { "dvbtxt", AV_CODEC_ID_DVB_TELETEXT, AVMEDIA_TYPE_SUBTITLE },
114 { "eac3", AV_CODEC_ID_EAC3, AVMEDIA_TYPE_AUDIO },
115 { "h264", AV_CODEC_ID_H264, AVMEDIA_TYPE_VIDEO },
116 { "hevc", AV_CODEC_ID_HEVC, AVMEDIA_TYPE_VIDEO },
117 { "loas", AV_CODEC_ID_AAC_LATM, AVMEDIA_TYPE_AUDIO },
118 { "m4v", AV_CODEC_ID_MPEG4, AVMEDIA_TYPE_VIDEO },
119 { "mjpeg_2000", AV_CODEC_ID_JPEG2000, AVMEDIA_TYPE_VIDEO },
120 { "mp3", AV_CODEC_ID_MP3, AVMEDIA_TYPE_AUDIO },
121 { "mpegvideo", AV_CODEC_ID_MPEG2VIDEO, AVMEDIA_TYPE_VIDEO },
122 { "truehd", AV_CODEC_ID_TRUEHD, AVMEDIA_TYPE_AUDIO },
123 { 0 }
124 };
125 int score;
126 2854 const AVInputFormat *fmt = av_probe_input_format3(pd, 1, &score);
127 2854 FFStream *const sti = ffstream(st);
128
129
2/2
✓ Branch 0 taken 931 times.
✓ Branch 1 taken 1923 times.
2854 if (fmt) {
130 931 av_log(s, AV_LOG_DEBUG,
131 "Probe with size=%d, packets=%d detected %s with score=%d\n",
132 931 pd->buf_size, s->max_probe_packets - sti->probe_packets,
133 931 fmt->name, score);
134
2/2
✓ Branch 0 taken 13869 times.
✓ Branch 1 taken 910 times.
14779 for (int i = 0; fmt_id_type[i].name; i++) {
135
2/2
✓ Branch 0 taken 75 times.
✓ Branch 1 taken 13794 times.
13869 if (!strcmp(fmt->name, fmt_id_type[i].name)) {
136
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 64 times.
75 if (fmt_id_type[i].type != AVMEDIA_TYPE_AUDIO &&
137
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 st->codecpar->sample_rate)
138 continue;
139
2/2
✓ Branch 0 taken 58 times.
✓ Branch 1 taken 17 times.
75 if (sti->request_probe > score &&
140
2/2
✓ Branch 0 taken 54 times.
✓ Branch 1 taken 4 times.
58 st->codecpar->codec_id != fmt_id_type[i].id)
141 54 continue;
142 21 st->codecpar->codec_id = fmt_id_type[i].id;
143 21 st->codecpar->codec_type = fmt_id_type[i].type;
144 21 sti->need_context_update = 1;
145 21 return score;
146 }
147 }
148 }
149 2833 return 0;
150 }
151
152 6834 static int init_input(AVFormatContext *s, const char *filename,
153 AVDictionary **options)
154 {
155 int ret;
156 6834 AVProbeData pd = { filename, NULL, 0 };
157 6834 int score = AVPROBE_SCORE_RETRY;
158
159
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 6822 times.
6834 if (s->pb) {
160 12 s->flags |= AVFMT_FLAG_CUSTOM_IO;
161
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (!s->iformat)
162 return av_probe_input_buffer2(s->pb, &s->iformat, filename,
163 s, 0, s->format_probesize);
164
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 else if (s->iformat->flags & AVFMT_NOFILE)
165 av_log(s, AV_LOG_WARNING, "Custom AVIOContext makes no sense and "
166 "will be ignored with AVFMT_NOFILE format.\n");
167 12 return 0;
168 }
169
170
4/4
✓ Branch 0 taken 3224 times.
✓ Branch 1 taken 3598 times.
✓ Branch 2 taken 759 times.
✓ Branch 3 taken 2465 times.
6822 if ((s->iformat && s->iformat->flags & AVFMT_NOFILE) ||
171
4/4
✓ Branch 0 taken 3598 times.
✓ Branch 1 taken 759 times.
✓ Branch 3 taken 188 times.
✓ Branch 4 taken 3410 times.
4357 (!s->iformat && (s->iformat = av_probe_input_format2(&pd, 0, &score))))
172 2653 return score;
173
174
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4169 times.
4169 if ((ret = s->io_open(s, &s->pb, filename, AVIO_FLAG_READ | s->avio_flags, options)) < 0)
175 return ret;
176
177
2/2
✓ Branch 0 taken 759 times.
✓ Branch 1 taken 3410 times.
4169 if (s->iformat)
178 759 return 0;
179 3410 return av_probe_input_buffer2(s->pb, &s->iformat, filename,
180 3410 s, 0, s->format_probesize);
181 }
182
183 6834 static int update_stream_avctx(AVFormatContext *s)
184 {
185 int ret;
186
2/2
✓ Branch 0 taken 7308 times.
✓ Branch 1 taken 6834 times.
14142 for (unsigned i = 0; i < s->nb_streams; i++) {
187 7308 AVStream *const st = s->streams[i];
188 7308 FFStream *const sti = ffstream(st);
189
190
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7308 times.
7308 if (!sti->need_context_update)
191 continue;
192
193 /* close parser, because it depends on the codec */
194
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 7308 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
7308 if (sti->parser && sti->avctx->codec_id != st->codecpar->codec_id) {
195 av_parser_close(sti->parser);
196 sti->parser = NULL;
197 }
198
199 #if FF_API_OLD_CHANNEL_LAYOUT
200 FF_DISABLE_DEPRECATION_WARNINGS
201
2/2
✓ Branch 0 taken 1481 times.
✓ Branch 1 taken 5827 times.
7308 if (st->codecpar->ch_layout.nb_channels &&
202
2/2
✓ Branch 0 taken 1475 times.
✓ Branch 1 taken 6 times.
1481 !st->codecpar->channels) {
203 1475 st->codecpar->channels = st->codecpar->ch_layout.nb_channels;
204 1475 st->codecpar->channel_layout = st->codecpar->ch_layout.order == AV_CHANNEL_ORDER_NATIVE ?
205
2/2
✓ Branch 0 taken 280 times.
✓ Branch 1 taken 1195 times.
1475 st->codecpar->ch_layout.u.mask : 0;
206
207 }
208 FF_ENABLE_DEPRECATION_WARNINGS
209 #endif
210
211 /* update internal codec context, for the parser */
212 7308 ret = avcodec_parameters_to_context(sti->avctx, st->codecpar);
213
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7308 times.
7308 if (ret < 0)
214 return ret;
215
216 7308 sti->need_context_update = 0;
217 }
218 6834 return 0;
219 }
220
221 6834 int avformat_open_input(AVFormatContext **ps, const char *filename,
222 const AVInputFormat *fmt, AVDictionary **options)
223 {
224 6834 AVFormatContext *s = *ps;
225 FFFormatContext *si;
226 6834 AVDictionary *tmp = NULL;
227 6834 ID3v2ExtraMeta *id3v2_extra_meta = NULL;
228 6834 int ret = 0;
229
230
3/4
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 6813 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 21 times.
6834 if (!s && !(s = avformat_alloc_context()))
231 return AVERROR(ENOMEM);
232 6834 si = ffformatcontext(s);
233
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6834 times.
6834 if (!s->av_class) {
234 av_log(NULL, AV_LOG_ERROR, "Input context has not been properly allocated by avformat_alloc_context() and is not NULL either\n");
235 return AVERROR(EINVAL);
236 }
237
2/2
✓ Branch 0 taken 3236 times.
✓ Branch 1 taken 3598 times.
6834 if (fmt)
238 3236 s->iformat = fmt;
239
240
2/2
✓ Branch 0 taken 6824 times.
✓ Branch 1 taken 10 times.
6834 if (options)
241 6824 av_dict_copy(&tmp, *options, 0);
242
243
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 6822 times.
6834 if (s->pb) // must be before any goto fail
244 12 s->flags |= AVFMT_FLAG_CUSTOM_IO;
245
246
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6834 times.
6834 if ((ret = av_opt_set_dict(s, &tmp)) < 0)
247 goto fail;
248
249
2/4
✓ Branch 0 taken 6834 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 6834 times.
6834 if (!(s->url = av_strdup(filename ? filename : ""))) {
250 ret = AVERROR(ENOMEM);
251 goto fail;
252 }
253
254
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6834 times.
6834 if ((ret = init_input(s, filename, &tmp)) < 0)
255 goto fail;
256 6834 s->probe_score = ret;
257
258
6/6
✓ Branch 0 taken 6751 times.
✓ Branch 1 taken 83 times.
✓ Branch 2 taken 4098 times.
✓ Branch 3 taken 2653 times.
✓ Branch 4 taken 4097 times.
✓ Branch 5 taken 1 times.
6834 if (!s->protocol_whitelist && s->pb && s->pb->protocol_whitelist) {
259 4097 s->protocol_whitelist = av_strdup(s->pb->protocol_whitelist);
260
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4097 times.
4097 if (!s->protocol_whitelist) {
261 ret = AVERROR(ENOMEM);
262 goto fail;
263 }
264 }
265
266
4/6
✓ Branch 0 taken 6834 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4181 times.
✓ Branch 3 taken 2653 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 4181 times.
6834 if (!s->protocol_blacklist && s->pb && s->pb->protocol_blacklist) {
267 s->protocol_blacklist = av_strdup(s->pb->protocol_blacklist);
268 if (!s->protocol_blacklist) {
269 ret = AVERROR(ENOMEM);
270 goto fail;
271 }
272 }
273
274
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 6834 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
6834 if (s->format_whitelist && av_match_list(s->iformat->name, s->format_whitelist, ',') <= 0) {
275 av_log(s, AV_LOG_ERROR, "Format not on whitelist \'%s\'\n", s->format_whitelist);
276 ret = AVERROR(EINVAL);
277 goto fail;
278 }
279
280 6834 avio_skip(s->pb, s->skip_initial_bytes);
281
282 /* Check filename in case an image number is expected. */
283
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6834 times.
6834 if (s->iformat->flags & AVFMT_NEEDNUMBER) {
284 if (!av_filename_number_test(filename)) {
285 ret = AVERROR(EINVAL);
286 goto fail;
287 }
288 }
289
290 6834 s->duration = s->start_time = AV_NOPTS_VALUE;
291
292 /* Allocate private data. */
293
2/2
✓ Branch 0 taken 6686 times.
✓ Branch 1 taken 148 times.
6834 if (s->iformat->priv_data_size > 0) {
294
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6686 times.
6686 if (!(s->priv_data = av_mallocz(s->iformat->priv_data_size))) {
295 ret = AVERROR(ENOMEM);
296 goto fail;
297 }
298
2/2
✓ Branch 0 taken 5905 times.
✓ Branch 1 taken 781 times.
6686 if (s->iformat->priv_class) {
299 5905 *(const AVClass **) s->priv_data = s->iformat->priv_class;
300 5905 av_opt_set_defaults(s->priv_data);
301
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5905 times.
5905 if ((ret = av_opt_set_dict(s->priv_data, &tmp)) < 0)
302 goto fail;
303 }
304 }
305
306 /* e.g. AVFMT_NOFILE formats will not have an AVIOContext */
307
2/2
✓ Branch 0 taken 4181 times.
✓ Branch 1 taken 2653 times.
6834 if (s->pb)
308 4181 ff_id3v2_read_dict(s->pb, &si->id3v2_meta, ID3v2_DEFAULT_MAGIC, &id3v2_extra_meta);
309
310
1/2
✓ Branch 0 taken 6834 times.
✗ Branch 1 not taken.
6834 if (s->iformat->read_header)
311
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6834 times.
6834 if ((ret = s->iformat->read_header(s)) < 0) {
312 if (s->iformat->flags_internal & FF_FMT_INIT_CLEANUP)
313 goto close;
314 goto fail;
315 }
316
317
2/2
✓ Branch 0 taken 5646 times.
✓ Branch 1 taken 1188 times.
6834 if (!s->metadata) {
318 5646 s->metadata = si->id3v2_meta;
319 5646 si->id3v2_meta = NULL;
320
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1188 times.
1188 } else if (si->id3v2_meta) {
321 av_log(s, AV_LOG_WARNING, "Discarding ID3 tags because more suitable tags were found.\n");
322 av_dict_free(&si->id3v2_meta);
323 }
324
325
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 6824 times.
6834 if (id3v2_extra_meta) {
326
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") ||
327 !strcmp(s->iformat->name, "tta") || !strcmp(s->iformat->name, "wav")) {
328
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
10 if ((ret = ff_id3v2_parse_apic(s, id3v2_extra_meta)) < 0)
329 goto close;
330
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
10 if ((ret = ff_id3v2_parse_chapters(s, id3v2_extra_meta)) < 0)
331 goto close;
332
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
10 if ((ret = ff_id3v2_parse_priv(s, id3v2_extra_meta)) < 0)
333 goto close;
334 } else
335 av_log(s, AV_LOG_DEBUG, "demuxer does not support additional id3 data, skipping\n");
336 10 ff_id3v2_free_extra_meta(&id3v2_extra_meta);
337 }
338
339
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6834 times.
6834 if ((ret = avformat_queue_attached_pictures(s)) < 0)
340 goto close;
341
342
4/4
✓ Branch 0 taken 4181 times.
✓ Branch 1 taken 2653 times.
✓ Branch 2 taken 3768 times.
✓ Branch 3 taken 413 times.
6834 if (s->pb && !si->data_offset)
343 3768 si->data_offset = avio_tell(s->pb);
344
345 6834 si->raw_packet_buffer_size = 0;
346
347 6834 update_stream_avctx(s);
348
349
2/2
✓ Branch 0 taken 6824 times.
✓ Branch 1 taken 10 times.
6834 if (options) {
350 6824 av_dict_free(options);
351 6824 *options = tmp;
352 }
353 6834 *ps = s;
354 6834 return 0;
355
356 close:
357 if (s->iformat->read_close)
358 s->iformat->read_close(s);
359 fail:
360 ff_id3v2_free_extra_meta(&id3v2_extra_meta);
361 av_dict_free(&tmp);
362 if (s->pb && !(s->flags & AVFMT_FLAG_CUSTOM_IO))
363 avio_closep(&s->pb);
364 avformat_free_context(s);
365 *ps = NULL;
366 return ret;
367 }
368
369 6834 void avformat_close_input(AVFormatContext **ps)
370 {
371 AVFormatContext *s;
372 AVIOContext *pb;
373
374
2/4
✓ Branch 0 taken 6834 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6834 times.
6834 if (!ps || !*ps)
375 return;
376
377 6834 s = *ps;
378 6834 pb = s->pb;
379
380
5/6
✓ Branch 0 taken 6834 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4204 times.
✓ Branch 3 taken 2630 times.
✓ Branch 4 taken 4163 times.
✓ Branch 5 taken 41 times.
6834 if ((s->iformat && strcmp(s->iformat->name, "image2") && s->iformat->flags & AVFMT_NOFILE) ||
381
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 6781 times.
6793 (s->flags & AVFMT_FLAG_CUSTOM_IO))
382 53 pb = NULL;
383
384
1/2
✓ Branch 0 taken 6834 times.
✗ Branch 1 not taken.
6834 if (s->iformat)
385
2/2
✓ Branch 0 taken 4331 times.
✓ Branch 1 taken 2503 times.
6834 if (s->iformat->read_close)
386 4331 s->iformat->read_close(s);
387
388 6834 avformat_free_context(s);
389
390 6834 *ps = NULL;
391
392 6834 avio_close(pb);
393 }
394
395 1030808 static void force_codec_ids(AVFormatContext *s, AVStream *st)
396 {
397
5/5
✓ Branch 0 taken 642865 times.
✓ Branch 1 taken 385110 times.
✓ Branch 2 taken 2709 times.
✓ Branch 3 taken 122 times.
✓ Branch 4 taken 2 times.
1030808 switch (st->codecpar->codec_type) {
398 642865 case AVMEDIA_TYPE_VIDEO:
399
2/2
✓ Branch 0 taken 96621 times.
✓ Branch 1 taken 546244 times.
642865 if (s->video_codec_id)
400 96621 st->codecpar->codec_id = s->video_codec_id;
401 642865 break;
402 385110 case AVMEDIA_TYPE_AUDIO:
403
2/2
✓ Branch 0 taken 2465 times.
✓ Branch 1 taken 382645 times.
385110 if (s->audio_codec_id)
404 2465 st->codecpar->codec_id = s->audio_codec_id;
405 385110 break;
406 2709 case AVMEDIA_TYPE_SUBTITLE:
407
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2709 times.
2709 if (s->subtitle_codec_id)
408 st->codecpar->codec_id = s->subtitle_codec_id;
409 2709 break;
410 122 case AVMEDIA_TYPE_DATA:
411
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 122 times.
122 if (s->data_codec_id)
412 st->codecpar->codec_id = s->data_codec_id;
413 122 break;
414 }
415 1030808 }
416
417 14194 static int probe_codec(AVFormatContext *s, AVStream *st, const AVPacket *pkt)
418 {
419 14194 FFFormatContext *const si = ffformatcontext(s);
420 14194 FFStream *const sti = ffstream(st);
421
422
2/2
✓ Branch 0 taken 14152 times.
✓ Branch 1 taken 42 times.
14194 if (sti->request_probe > 0) {
423 14152 AVProbeData *const pd = &sti->probe_data;
424 int end;
425 14152 av_log(s, AV_LOG_DEBUG, "probing stream %d pp:%d\n", st->index, sti->probe_packets);
426 14152 --sti->probe_packets;
427
428
2/2
✓ Branch 0 taken 14018 times.
✓ Branch 1 taken 134 times.
14152 if (pkt) {
429 14018 uint8_t *new_buf = av_realloc(pd->buf, pd->buf_size+pkt->size+AVPROBE_PADDING_SIZE);
430
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14018 times.
14018 if (!new_buf) {
431 av_log(s, AV_LOG_WARNING,
432 "Failed to reallocate probe buffer for stream %d\n",
433 st->index);
434 goto no_packet;
435 }
436 14018 pd->buf = new_buf;
437 14018 memcpy(pd->buf + pd->buf_size, pkt->data, pkt->size);
438 14018 pd->buf_size += pkt->size;
439 14018 memset(pd->buf + pd->buf_size, 0, AVPROBE_PADDING_SIZE);
440 } else {
441 134 no_packet:
442 134 sti->probe_packets = 0;
443
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 134 times.
134 if (!pd->buf_size) {
444 av_log(s, AV_LOG_WARNING,
445 "nothing to probe for stream %d\n", st->index);
446 }
447 }
448
449 28304 end = si->raw_packet_buffer_size >= s->probesize
450
3/4
✓ Branch 0 taken 14152 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 484 times.
✓ Branch 3 taken 13668 times.
14152 || sti->probe_packets <= 0;
451
452
4/4
✓ Branch 0 taken 13668 times.
✓ Branch 1 taken 484 times.
✓ Branch 2 taken 2370 times.
✓ Branch 3 taken 11298 times.
14152 if (end || av_log2(pd->buf_size) != av_log2(pd->buf_size - pkt->size)) {
453 2854 int score = set_codec_from_probe_data(s, st, pd);
454
4/4
✓ Branch 0 taken 2846 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 2829 times.
✓ Branch 3 taken 17 times.
2854 if ( (st->codecpar->codec_id != AV_CODEC_ID_NONE && score > AVPROBE_SCORE_STREAM_RETRY)
455
2/2
✓ Branch 0 taken 484 times.
✓ Branch 1 taken 2353 times.
2837 || end) {
456 501 pd->buf_size = 0;
457 501 av_freep(&pd->buf);
458 501 sti->request_probe = -1;
459
1/2
✓ Branch 0 taken 501 times.
✗ Branch 1 not taken.
501 if (st->codecpar->codec_id != AV_CODEC_ID_NONE) {
460 501 av_log(s, AV_LOG_DEBUG, "probed stream %d\n", st->index);
461 } else
462 av_log(s, AV_LOG_WARNING, "probed stream %d failed\n", st->index);
463 }
464 2854 force_codec_ids(s, st);
465 }
466 }
467 14194 return 0;
468 }
469
470 1027954 static int update_wrap_reference(AVFormatContext *s, AVStream *st, int stream_index, AVPacket *pkt)
471 {
472 1027954 FFStream *const sti = ffstream(st);
473 1027954 int64_t ref = pkt->dts;
474 int pts_wrap_behavior;
475 int64_t pts_wrap_reference;
476 AVProgram *first_program;
477
478
2/2
✓ Branch 0 taken 840305 times.
✓ Branch 1 taken 187649 times.
1027954 if (ref == AV_NOPTS_VALUE)
479 840305 ref = pkt->pts;
480
7/8
✓ Branch 0 taken 970982 times.
✓ Branch 1 taken 56972 times.
✓ Branch 2 taken 49753 times.
✓ Branch 3 taken 921229 times.
✓ Branch 4 taken 250 times.
✓ Branch 5 taken 49503 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 250 times.
1027954 if (sti->pts_wrap_reference != AV_NOPTS_VALUE || st->pts_wrap_bits >= 63 || ref == AV_NOPTS_VALUE || !s->correct_ts_overflow)
481 1027704 return 0;
482 250 ref &= (1LL << st->pts_wrap_bits)-1;
483
484 // reference time stamp should be 60 s before first time stamp
485 250 pts_wrap_reference = ref - av_rescale(60, st->time_base.den, st->time_base.num);
486 // if first time stamp is not more than 1/8 and 60s before the wrap point, subtract rather than add wrap offset
487 501 pts_wrap_behavior = (ref < (1LL << st->pts_wrap_bits) - (1LL << st->pts_wrap_bits-3)) ||
488
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)) ?
489
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 249 times.
251 AV_PTS_WRAP_ADD_OFFSET : AV_PTS_WRAP_SUB_OFFSET;
490
491 250 first_program = av_find_program_from_stream(s, NULL, stream_index);
492
493
2/2
✓ Branch 0 taken 166 times.
✓ Branch 1 taken 84 times.
250 if (!first_program) {
494 166 int default_stream_index = av_find_default_stream_index(s);
495 166 FFStream *const default_sti = ffstream(s->streams[default_stream_index]);
496
2/2
✓ Branch 0 taken 155 times.
✓ Branch 1 taken 11 times.
166 if (default_sti->pts_wrap_reference == AV_NOPTS_VALUE) {
497
2/2
✓ Branch 0 taken 231 times.
✓ Branch 1 taken 155 times.
386 for (unsigned i = 0; i < s->nb_streams; i++) {
498 231 FFStream *const sti = ffstream(s->streams[i]);
499
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 231 times.
231 if (av_find_program_from_stream(s, NULL, i))
500 continue;
501 231 sti->pts_wrap_reference = pts_wrap_reference;
502 231 sti->pts_wrap_behavior = pts_wrap_behavior;
503 }
504 } else {
505 11 sti->pts_wrap_reference = default_sti->pts_wrap_reference;
506 11 sti->pts_wrap_behavior = default_sti->pts_wrap_behavior;
507 }
508 } else {
509 84 AVProgram *program = first_program;
510
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 58 times.
142 while (program) {
511
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 58 times.
84 if (program->pts_wrap_reference != AV_NOPTS_VALUE) {
512 26 pts_wrap_reference = program->pts_wrap_reference;
513 26 pts_wrap_behavior = program->pts_wrap_behavior;
514 26 break;
515 }
516 58 program = av_find_program_from_stream(s, program, stream_index);
517 }
518
519 // update every program with differing pts_wrap_reference
520 84 program = first_program;
521
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 84 times.
168 while (program) {
522
2/2
✓ Branch 0 taken 58 times.
✓ Branch 1 taken 26 times.
84 if (program->pts_wrap_reference != pts_wrap_reference) {
523
2/2
✓ Branch 0 taken 95 times.
✓ Branch 1 taken 58 times.
153 for (unsigned i = 0; i < program->nb_stream_indexes; i++) {
524 95 FFStream *const sti = ffstream(s->streams[program->stream_index[i]]);
525 95 sti->pts_wrap_reference = pts_wrap_reference;
526 95 sti->pts_wrap_behavior = pts_wrap_behavior;
527 }
528
529 58 program->pts_wrap_reference = pts_wrap_reference;
530 58 program->pts_wrap_behavior = pts_wrap_behavior;
531 }
532 84 program = av_find_program_from_stream(s, program, stream_index);
533 }
534 }
535 250 return 1;
536 }
537
538 1033961 int ff_read_packet(AVFormatContext *s, AVPacket *pkt)
539 {
540 1033961 FFFormatContext *const si = ffformatcontext(s);
541 int err;
542
543 #if FF_API_INIT_PACKET
544 FF_DISABLE_DEPRECATION_WARNINGS
545 1033961 pkt->data = NULL;
546 1033961 pkt->size = 0;
547 1033961 av_init_packet(pkt);
548 FF_ENABLE_DEPRECATION_WARNINGS
549 #else
550 av_packet_unref(pkt);
551 #endif
552
553 14622 for (;;) {
554 1048583 PacketListEntry *pktl = si->raw_packet_buffer.head;
555 AVStream *st;
556 FFStream *sti;
557 const AVPacket *pkt1;
558
559
2/2
✓ Branch 0 taken 27694 times.
✓ Branch 1 taken 1020889 times.
1048583 if (pktl) {
560 27694 AVStream *const st = s->streams[pktl->pkt.stream_index];
561
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27694 times.
27694 if (si->raw_packet_buffer_size >= s->probesize)
562 if ((err = probe_codec(s, st, NULL)) < 0)
563 return err;
564
2/2
✓ Branch 1 taken 14001 times.
✓ Branch 2 taken 13693 times.
27694 if (ffstream(st)->request_probe <= 0) {
565 14001 avpriv_packet_list_get(&si->raw_packet_buffer, pkt);
566 14001 si->raw_packet_buffer_size -= pkt->size;
567 14001 return 0;
568 }
569 }
570
571 1034582 err = s->iformat->read_packet(s, pkt);
572
2/2
✓ Branch 0 taken 6628 times.
✓ Branch 1 taken 1027954 times.
1034582 if (err < 0) {
573 6628 av_packet_unref(pkt);
574
575 /* Some demuxers return FFERROR_REDO when they consume
576 data and discard it (ignored streams, junk, extradata).
577 We must re-call the demuxer to get the real packet. */
578
2/2
✓ Branch 0 taken 428 times.
✓ Branch 1 taken 6200 times.
6628 if (err == FFERROR_REDO)
579 428 continue;
580
3/4
✓ Branch 0 taken 134 times.
✓ Branch 1 taken 6066 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 134 times.
6200 if (!pktl || err == AVERROR(EAGAIN))
581 6066 return err;
582
2/2
✓ Branch 0 taken 134 times.
✓ Branch 1 taken 134 times.
268 for (unsigned i = 0; i < s->nb_streams; i++) {
583 134 AVStream *const st = s->streams[i];
584 134 FFStream *const sti = ffstream(st);
585
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 134 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
134 if (sti->probe_packets || sti->request_probe > 0)
586
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 134 times.
134 if ((err = probe_codec(s, st, NULL)) < 0)
587 return err;
588
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 134 times.
134 av_assert0(sti->request_probe <= 0);
589 }
590 134 continue;
591 }
592
593 1027954 err = av_packet_make_refcounted(pkt);
594
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1027954 times.
1027954 if (err < 0) {
595 av_packet_unref(pkt);
596 return err;
597 }
598
599
2/2
✓ Branch 0 taken 174 times.
✓ Branch 1 taken 1027780 times.
1027954 if (pkt->flags & AV_PKT_FLAG_CORRUPT) {
600 174 av_log(s, AV_LOG_WARNING,
601 "Packet corrupt (stream = %d, dts = %s)",
602 174 pkt->stream_index, av_ts2str(pkt->dts));
603
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 174 times.
174 if (s->flags & AVFMT_FLAG_DISCARD_CORRUPT) {
604 av_log(s, AV_LOG_WARNING, ", dropping it.\n");
605 av_packet_unref(pkt);
606 continue;
607 }
608 174 av_log(s, AV_LOG_WARNING, ".\n");
609 }
610
611
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1027954 times.
1027954 av_assert0(pkt->stream_index < (unsigned)s->nb_streams &&
612 "Invalid stream index.\n");
613
614 1027954 st = s->streams[pkt->stream_index];
615 1027954 sti = ffstream(st);
616
617
4/4
✓ Branch 1 taken 250 times.
✓ Branch 2 taken 1027704 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 249 times.
1027954 if (update_wrap_reference(s, st, pkt->stream_index, pkt) && sti->pts_wrap_behavior == AV_PTS_WRAP_SUB_OFFSET) {
618 // correct first time stamps to negative values
619
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 if (!is_relative(sti->first_dts))
620 1 sti->first_dts = wrap_timestamp(st, sti->first_dts);
621
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 if (!is_relative(st->start_time))
622 1 st->start_time = wrap_timestamp(st, st->start_time);
623
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (!is_relative(sti->cur_dts))
624 sti->cur_dts = wrap_timestamp(st, sti->cur_dts);
625 }
626
627 1027954 pkt->dts = wrap_timestamp(st, pkt->dts);
628 1027954 pkt->pts = wrap_timestamp(st, pkt->pts);
629
630 1027954 force_codec_ids(s, st);
631
632 /* TODO: audio: time filter; video: frame reordering (pts != dts) */
633
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1027954 times.
1027954 if (s->use_wallclock_as_timestamps)
634 pkt->dts = pkt->pts = av_rescale_q(av_gettime(), AV_TIME_BASE_Q, st->time_base);
635
636
4/4
✓ Branch 0 taken 1014395 times.
✓ Branch 1 taken 13559 times.
✓ Branch 2 taken 1013894 times.
✓ Branch 3 taken 501 times.
1027954 if (!pktl && sti->request_probe <= 0)
637 1013894 return 0;
638
639 14060 err = avpriv_packet_list_put(&si->raw_packet_buffer,
640 pkt, NULL, 0);
641
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14060 times.
14060 if (err < 0) {
642 av_packet_unref(pkt);
643 return err;
644 }
645 14060 pkt1 = &si->raw_packet_buffer.tail->pkt;
646 14060 si->raw_packet_buffer_size += pkt1->size;
647
648
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 14060 times.
14060 if ((err = probe_codec(s, st, pkt1)) < 0)
649 return err;
650 }
651 }
652
653 /**
654 * Return the frame duration in seconds. Return 0 if not available.
655 */
656 344464 static void compute_frame_duration(AVFormatContext *s, int *pnum, int *pden,
657 AVStream *st, AVCodecParserContext *pc,
658 AVPacket *pkt)
659 {
660 344464 FFStream *const sti = ffstream(st);
661 344464 AVRational codec_framerate = sti->avctx->framerate;
662 int frame_size, sample_rate;
663
664 344464 *pnum = 0;
665 344464 *pden = 0;
666
3/3
✓ Branch 0 taken 196291 times.
✓ Branch 1 taken 146936 times.
✓ Branch 2 taken 1237 times.
344464 switch (st->codecpar->codec_type) {
667 196291 case AVMEDIA_TYPE_VIDEO:
668
6/6
✓ Branch 0 taken 159289 times.
✓ Branch 1 taken 37002 times.
✓ Branch 2 taken 28047 times.
✓ Branch 3 taken 131242 times.
✓ Branch 4 taken 5108 times.
✓ Branch 5 taken 22939 times.
196291 if (st->r_frame_rate.num && (!pc || !codec_framerate.num)) {
669 136350 *pnum = st->r_frame_rate.den;
670 136350 *pden = st->r_frame_rate.num;
671
2/2
✓ Branch 0 taken 10058 times.
✓ Branch 1 taken 49883 times.
59941 } else if (st->time_base.num * 1000LL > st->time_base.den) {
672 10058 *pnum = st->time_base.num;
673 10058 *pden = st->time_base.den;
674
2/2
✓ Branch 0 taken 49754 times.
✓ Branch 1 taken 129 times.
49883 } else if (codec_framerate.den * 1000LL > codec_framerate.num) {
675
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49754 times.
49754 av_assert0(sti->avctx->ticks_per_frame);
676 49754 av_reduce(pnum, pden,
677 49754 codec_framerate.den,
678 49754 codec_framerate.num * (int64_t)sti->avctx->ticks_per_frame,
679 INT_MAX);
680
681
4/4
✓ Branch 0 taken 47942 times.
✓ Branch 1 taken 1812 times.
✓ Branch 2 taken 27384 times.
✓ Branch 3 taken 20558 times.
49754 if (pc && pc->repeat_pict) {
682 27384 av_reduce(pnum, pden,
683 27384 (*pnum) * (1LL + pc->repeat_pict),
684 27384 (*pden),
685 INT_MAX);
686 }
687 /* If this codec can be interlaced or progressive then we need
688 * a parser to compute duration of a packet. Thus if we have
689 * no parser in such case leave duration undefined. */
690
3/4
✓ Branch 0 taken 32948 times.
✓ Branch 1 taken 16806 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 32948 times.
49754 if (sti->avctx->ticks_per_frame > 1 && !pc)
691 *pnum = *pden = 0;
692 }
693 196291 break;
694 146936 case AVMEDIA_TYPE_AUDIO:
695
2/2
✓ Branch 0 taken 41349 times.
✓ Branch 1 taken 105587 times.
146936 if (sti->avctx_inited) {
696 41349 frame_size = av_get_audio_frame_duration(sti->avctx, pkt->size);
697 41349 sample_rate = sti->avctx->sample_rate;
698 } else {
699 105587 frame_size = av_get_audio_frame_duration2(st->codecpar, pkt->size);
700 105587 sample_rate = st->codecpar->sample_rate;
701 }
702
4/4
✓ Branch 0 taken 141170 times.
✓ Branch 1 taken 5766 times.
✓ Branch 2 taken 141167 times.
✓ Branch 3 taken 3 times.
146936 if (frame_size <= 0 || sample_rate <= 0)
703 break;
704 141167 *pnum = frame_size;
705 141167 *pden = sample_rate;
706 141167 break;
707 1237 default:
708 1237 break;
709 }
710 344464 }
711
712 721066 static int has_decode_delay_been_guessed(AVStream *st)
713 {
714 721066 FFStream *const sti = ffstream(st);
715
2/2
✓ Branch 0 taken 698224 times.
✓ Branch 1 taken 22842 times.
721066 if (st->codecpar->codec_id != AV_CODEC_ID_H264) return 1;
716
2/2
✓ Branch 0 taken 5377 times.
✓ Branch 1 taken 17465 times.
22842 if (!sti->info) // if we have left find_stream_info then nb_decoded_frames won't increase anymore for stream copy
717 5377 return 1;
718 #if CONFIG_H264_DECODER
719
2/2
✓ Branch 0 taken 10938 times.
✓ Branch 1 taken 6527 times.
17465 if (sti->avctx->has_b_frames &&
720
2/2
✓ Branch 1 taken 1310 times.
✓ Branch 2 taken 9628 times.
10938 avpriv_h264_has_num_reorder_frames(sti->avctx) == sti->avctx->has_b_frames)
721 1310 return 1;
722 #endif
723
2/2
✓ Branch 0 taken 15655 times.
✓ Branch 1 taken 500 times.
16155 if (sti->avctx->has_b_frames < 3)
724 15655 return sti->nb_decoded_frames >= 7;
725
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 464 times.
500 else if (sti->avctx->has_b_frames < 4)
726 36 return sti->nb_decoded_frames >= 18;
727 else
728 464 return sti->nb_decoded_frames >= 20;
729 }
730
731 34483 static PacketListEntry *get_next_pkt(AVFormatContext *s, AVStream *st,
732 PacketListEntry *pktl)
733 {
734 34483 FFFormatContext *const si = ffformatcontext(s);
735
2/2
✓ Branch 0 taken 32900 times.
✓ Branch 1 taken 1583 times.
34483 if (pktl->next)
736 32900 return pktl->next;
737
2/2
✓ Branch 0 taken 1554 times.
✓ Branch 1 taken 29 times.
1583 if (pktl == si->packet_buffer.tail)
738 1554 return si->parse_queue.head;
739 29 return NULL;
740 }
741
742 536534 static int64_t select_from_pts_buffer(AVStream *st, int64_t *pts_buffer, int64_t dts)
743 {
744 536534 FFStream *const sti = ffstream(st);
745
2/2
✓ Branch 0 taken 530207 times.
✓ Branch 1 taken 6327 times.
1066741 int onein_oneout = st->codecpar->codec_id != AV_CODEC_ID_H264 &&
746
2/2
✓ Branch 0 taken 529850 times.
✓ Branch 1 taken 357 times.
530207 st->codecpar->codec_id != AV_CODEC_ID_HEVC;
747
748
2/2
✓ Branch 0 taken 6684 times.
✓ Branch 1 taken 529850 times.
536534 if (!onein_oneout) {
749 6684 int delay = sti->avctx->has_b_frames;
750
751
2/2
✓ Branch 0 taken 1105 times.
✓ Branch 1 taken 5579 times.
6684 if (dts == AV_NOPTS_VALUE) {
752 1105 int64_t best_score = INT64_MAX;
753
2/2
✓ Branch 0 taken 1239 times.
✓ Branch 1 taken 1105 times.
2344 for (int i = 0; i < delay; i++) {
754
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1239 times.
1239 if (sti->pts_reorder_error_count[i]) {
755 int64_t score = sti->pts_reorder_error[i] / sti->pts_reorder_error_count[i];
756 if (score < best_score) {
757 best_score = score;
758 dts = pts_buffer[i];
759 }
760 }
761 }
762 } else {
763
2/2
✓ Branch 0 taken 9345 times.
✓ Branch 1 taken 5579 times.
14924 for (int i = 0; i < delay; i++) {
764
2/2
✓ Branch 0 taken 8643 times.
✓ Branch 1 taken 702 times.
9345 if (pts_buffer[i] != AV_NOPTS_VALUE) {
765 8643 int64_t diff = FFABS(pts_buffer[i] - dts)
766 8643 + (uint64_t)sti->pts_reorder_error[i];
767 8643 diff = FFMAX(diff, sti->pts_reorder_error[i]);
768 8643 sti->pts_reorder_error[i] = diff;
769 8643 sti->pts_reorder_error_count[i]++;
770
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 8640 times.
8643 if (sti->pts_reorder_error_count[i] > 250) {
771 3 sti->pts_reorder_error[i] >>= 1;
772 3 sti->pts_reorder_error_count[i] >>= 1;
773 }
774 }
775 }
776 }
777 }
778
779
2/2
✓ Branch 0 taken 1282 times.
✓ Branch 1 taken 535252 times.
536534 if (dts == AV_NOPTS_VALUE)
780 1282 dts = pts_buffer[0];
781
782 536534 return dts;
783 }
784
785 /**
786 * Updates the dts of packets of a stream in pkt_buffer, by re-ordering the pts
787 * of the packets in a window.
788 */
789 5448 static void update_dts_from_pts(AVFormatContext *s, int stream_index,
790 PacketListEntry *pkt_buffer)
791 {
792 5448 AVStream *const st = s->streams[stream_index];
793 5448 int delay = ffstream(st)->avctx->has_b_frames;
794
795 int64_t pts_buffer[MAX_REORDER_DELAY+1];
796
797
2/2
✓ Branch 0 taken 92616 times.
✓ Branch 1 taken 5448 times.
98064 for (int i = 0; i < MAX_REORDER_DELAY + 1; i++)
798 92616 pts_buffer[i] = AV_NOPTS_VALUE;
799
800
2/2
✓ Branch 1 taken 4865 times.
✓ Branch 2 taken 5448 times.
10313 for (; pkt_buffer; pkt_buffer = get_next_pkt(s, st, pkt_buffer)) {
801
2/2
✓ Branch 0 taken 4236 times.
✓ Branch 1 taken 629 times.
4865 if (pkt_buffer->pkt.stream_index != stream_index)
802 4236 continue;
803
804
3/4
✓ Branch 0 taken 537 times.
✓ Branch 1 taken 92 times.
✓ Branch 2 taken 537 times.
✗ Branch 3 not taken.
629 if (pkt_buffer->pkt.pts != AV_NOPTS_VALUE && delay <= MAX_REORDER_DELAY) {
805 537 pts_buffer[0] = pkt_buffer->pkt.pts;
806
4/4
✓ Branch 0 taken 347 times.
✓ Branch 1 taken 377 times.
✓ Branch 2 taken 187 times.
✓ Branch 3 taken 160 times.
724 for (int i = 0; i < delay && pts_buffer[i] > pts_buffer[i + 1]; i++)
807 187 FFSWAP(int64_t, pts_buffer[i], pts_buffer[i + 1]);
808
809 537 pkt_buffer->pkt.dts = select_from_pts_buffer(st, pts_buffer, pkt_buffer->pkt.dts);
810 }
811 }
812 5448 }
813
814 572655 static void update_initial_timestamps(AVFormatContext *s, int stream_index,
815 int64_t dts, int64_t pts, AVPacket *pkt)
816 {
817 572655 FFFormatContext *const si = ffformatcontext(s);
818 572655 AVStream *const st = s->streams[stream_index];
819 572655 FFStream *const sti = ffstream(st);
820
2/2
✓ Branch 0 taken 169716 times.
✓ Branch 1 taken 402939 times.
572655 PacketListEntry *pktl = si->packet_buffer.head ? si->packet_buffer.head : si->parse_queue.head;
821
822 uint64_t shift;
823
824
4/4
✓ Branch 0 taken 220517 times.
✓ Branch 1 taken 352138 times.
✓ Branch 2 taken 5495 times.
✓ Branch 3 taken 215022 times.
572655 if (sti->first_dts != AV_NOPTS_VALUE ||
825 5495 dts == AV_NOPTS_VALUE ||
826
1/2
✓ Branch 0 taken 5495 times.
✗ Branch 1 not taken.
5495 sti->cur_dts == AV_NOPTS_VALUE ||
827
1/2
✓ Branch 0 taken 5495 times.
✗ Branch 1 not taken.
5495 sti->cur_dts < INT_MIN + RELATIVE_TS_BASE ||
828
2/4
✓ Branch 0 taken 5495 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5495 times.
10990 dts < INT_MIN + (sti->cur_dts - RELATIVE_TS_BASE) ||
829 5495 is_relative(dts))
830 567160 return;
831
832 5495 sti->first_dts = dts - (sti->cur_dts - RELATIVE_TS_BASE);
833 5495 sti->cur_dts = dts;
834 5495 shift = (uint64_t)sti->first_dts - RELATIVE_TS_BASE;
835
836
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5495 times.
5495 if (is_relative(pts))
837 pts += shift;
838
839
2/2
✓ Branch 1 taken 4610 times.
✓ Branch 2 taken 5495 times.
10105 for (PacketListEntry *pktl_it = pktl; pktl_it; pktl_it = get_next_pkt(s, st, pktl_it)) {
840
2/2
✓ Branch 0 taken 4159 times.
✓ Branch 1 taken 451 times.
4610 if (pktl_it->pkt.stream_index != stream_index)
841 4159 continue;
842
2/2
✓ Branch 1 taken 205 times.
✓ Branch 2 taken 246 times.
451 if (is_relative(pktl_it->pkt.pts))
843 205 pktl_it->pkt.pts += shift;
844
845
2/2
✓ Branch 1 taken 233 times.
✓ Branch 2 taken 218 times.
451 if (is_relative(pktl_it->pkt.dts))
846 233 pktl_it->pkt.dts += shift;
847
848
4/4
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 391 times.
✓ Branch 2 taken 37 times.
✓ Branch 3 taken 23 times.
451 if (st->start_time == AV_NOPTS_VALUE && pktl_it->pkt.pts != AV_NOPTS_VALUE) {
849 37 st->start_time = pktl_it->pkt.pts;
850
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
37 if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && st->codecpar->sample_rate)
851 st->start_time = av_sat_add64(st->start_time, av_rescale_q(sti->skip_samples, (AVRational){1, st->codecpar->sample_rate}, st->time_base));
852 }
853 }
854
855
2/2
✓ Branch 1 taken 5432 times.
✓ Branch 2 taken 63 times.
5495 if (has_decode_delay_been_guessed(st))
856 5432 update_dts_from_pts(s, stream_index, pktl);
857
858
2/2
✓ Branch 0 taken 1586 times.
✓ Branch 1 taken 3909 times.
5495 if (st->start_time == AV_NOPTS_VALUE) {
859
3/4
✓ Branch 0 taken 1141 times.
✓ Branch 1 taken 445 times.
✓ Branch 2 taken 1141 times.
✗ Branch 3 not taken.
1586 if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO || !(pkt->flags & AV_PKT_FLAG_DISCARD)) {
860 1586 st->start_time = pts;
861 }
862
4/4
✓ Branch 0 taken 445 times.
✓ Branch 1 taken 1141 times.
✓ Branch 2 taken 375 times.
✓ Branch 3 taken 70 times.
1586 if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && st->codecpar->sample_rate)
863 375 st->start_time = av_sat_add64(st->start_time, av_rescale_q(sti->skip_samples, (AVRational){1, st->codecpar->sample_rate}, st->time_base));
864 }
865 }
866
867 209565 static void update_initial_durations(AVFormatContext *s, AVStream *st,
868 int stream_index, int64_t duration)
869 {
870 209565 FFFormatContext *const si = ffformatcontext(s);
871 209565 FFStream *const sti = ffstream(st);
872
2/2
✓ Branch 0 taken 166122 times.
✓ Branch 1 taken 43443 times.
209565 PacketListEntry *pktl = si->packet_buffer.head ? si->packet_buffer.head : si->parse_queue.head;
873 209565 int64_t cur_dts = RELATIVE_TS_BASE;
874
875
2/2
✓ Branch 0 taken 107959 times.
✓ Branch 1 taken 101606 times.
209565 if (sti->first_dts != AV_NOPTS_VALUE) {
876
2/2
✓ Branch 0 taken 104756 times.
✓ Branch 1 taken 3203 times.
107959 if (sti->update_initial_durations_done)
877 104756 return;
878 3203 sti->update_initial_durations_done = 1;
879 3203 cur_dts = sti->first_dts;
880
1/2
✓ Branch 1 taken 4796 times.
✗ Branch 2 not taken.
4796 for (; pktl; pktl = get_next_pkt(s, st, pktl)) {
881
2/2
✓ Branch 0 taken 3213 times.
✓ Branch 1 taken 1583 times.
4796 if (pktl->pkt.stream_index == stream_index) {
882
2/2
✓ Branch 0 taken 3116 times.
✓ Branch 1 taken 97 times.
3213 if (pktl->pkt.pts != pktl->pkt.dts ||
883
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 3106 times.
3116 pktl->pkt.dts != AV_NOPTS_VALUE ||
884
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 pktl->pkt.duration)
885 break;
886 10 cur_dts -= duration;
887 }
888 }
889
3/4
✓ Branch 0 taken 3203 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 29 times.
✓ Branch 3 taken 3174 times.
3203 if (pktl && pktl->pkt.dts != sti->first_dts) {
890 29 av_log(s, AV_LOG_DEBUG, "first_dts %s not matching first dts %s (pts %s, duration %"PRId64") in the queue\n",
891 29 av_ts2str(sti->first_dts), av_ts2str(pktl->pkt.dts), av_ts2str(pktl->pkt.pts), pktl->pkt.duration);
892 29 return;
893 }
894
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3174 times.
3174 if (!pktl) {
895 av_log(s, AV_LOG_DEBUG, "first_dts %s but no packet with dts in the queue\n", av_ts2str(sti->first_dts));
896 return;
897 }
898
2/2
✓ Branch 0 taken 3153 times.
✓ Branch 1 taken 21 times.
3174 pktl = si->packet_buffer.head ? si->packet_buffer.head : si->parse_queue.head;
899 3174 sti->first_dts = cur_dts;
900
2/2
✓ Branch 0 taken 73569 times.
✓ Branch 1 taken 28037 times.
101606 } else if (sti->cur_dts != RELATIVE_TS_BASE)
901 73569 return;
902
903
2/2
✓ Branch 1 taken 54078 times.
✓ Branch 2 taken 548 times.
54626 for (; pktl; pktl = get_next_pkt(s, st, pktl)) {
904
2/2
✓ Branch 0 taken 23277 times.
✓ Branch 1 taken 30801 times.
54078 if (pktl->pkt.stream_index != stream_index)
905 23277 continue;
906
2/2
✓ Branch 0 taken 534 times.
✓ Branch 1 taken 30267 times.
30801 if ((pktl->pkt.pts == pktl->pkt.dts ||
907
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 520 times.
534 pktl->pkt.pts == AV_NOPTS_VALUE) &&
908
2/2
✓ Branch 0 taken 3254 times.
✓ Branch 1 taken 27027 times.
30281 (pktl->pkt.dts == AV_NOPTS_VALUE ||
909
2/2
✓ Branch 0 taken 173 times.
✓ Branch 1 taken 3081 times.
3254 pktl->pkt.dts == sti->first_dts ||
910
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 171 times.
173 pktl->pkt.dts == RELATIVE_TS_BASE) &&
911
2/2
✓ Branch 0 taken 138 times.
✓ Branch 1 taken 29972 times.
30110 !pktl->pkt.duration &&
912
1/2
✓ Branch 1 taken 138 times.
✗ Branch 2 not taken.
138 av_sat_add64(cur_dts, duration) == cur_dts + (uint64_t)duration
913 ) {
914 138 pktl->pkt.dts = cur_dts;
915
2/2
✓ Branch 0 taken 122 times.
✓ Branch 1 taken 16 times.
138 if (!sti->avctx->has_b_frames)
916 122 pktl->pkt.pts = cur_dts;
917 138 pktl->pkt.duration = duration;
918 } else
919 break;
920 138 cur_dts = pktl->pkt.dts + pktl->pkt.duration;
921 }
922
2/2
✓ Branch 0 taken 548 times.
✓ Branch 1 taken 30663 times.
31211 if (!pktl)
923 548 sti->cur_dts = cur_dts;
924 }
925
926 578561 static void compute_pkt_fields(AVFormatContext *s, AVStream *st,
927 AVCodecParserContext *pc, AVPacket *pkt,
928 int64_t next_dts, int64_t next_pts)
929 {
930 578561 FFFormatContext *const si = ffformatcontext(s);
931 578561 FFStream *const sti = ffstream(st);
932 int num, den, presentation_delayed, delay;
933 int64_t offset;
934 AVRational duration;
935
2/2
✓ Branch 0 taken 547072 times.
✓ Branch 1 taken 31489 times.
1125633 int onein_oneout = st->codecpar->codec_id != AV_CODEC_ID_H264 &&
936
2/2
✓ Branch 0 taken 535292 times.
✓ Branch 1 taken 11780 times.
547072 st->codecpar->codec_id != AV_CODEC_ID_HEVC;
937
938
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 578561 times.
578561 if (s->flags & AVFMT_FLAG_NOFILLIN)
939 return;
940
941
4/4
✓ Branch 0 taken 214400 times.
✓ Branch 1 taken 364161 times.
✓ Branch 2 taken 65025 times.
✓ Branch 3 taken 149375 times.
578561 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && pkt->dts != AV_NOPTS_VALUE) {
942
4/4
✓ Branch 0 taken 37323 times.
✓ Branch 1 taken 27702 times.
✓ Branch 2 taken 36272 times.
✓ Branch 3 taken 1051 times.
65025 if (pkt->dts == pkt->pts && sti->last_dts_for_order_check != AV_NOPTS_VALUE) {
943
2/2
✓ Branch 0 taken 36263 times.
✓ Branch 1 taken 9 times.
36272 if (sti->last_dts_for_order_check <= pkt->dts) {
944 36263 sti->dts_ordered++;
945 } else {
946
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,
947 "DTS %"PRIi64" < %"PRIi64" out of order\n",
948 pkt->dts,
949 sti->last_dts_for_order_check);
950 9 sti->dts_misordered++;
951 }
952
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 36244 times.
36272 if (sti->dts_ordered + sti->dts_misordered > 250) {
953 28 sti->dts_ordered >>= 1;
954 28 sti->dts_misordered >>= 1;
955 }
956 }
957
958 65025 sti->last_dts_for_order_check = pkt->dts;
959
4/4
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 64988 times.
✓ Branch 2 taken 20 times.
✓ Branch 3 taken 17 times.
65025 if (sti->dts_ordered < 8 * sti->dts_misordered && pkt->dts == pkt->pts)
960 20 pkt->dts = AV_NOPTS_VALUE;
961 }
962
963
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 578561 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
578561 if ((s->flags & AVFMT_FLAG_IGNDTS) && pkt->pts != AV_NOPTS_VALUE)
964 pkt->dts = AV_NOPTS_VALUE;
965
966
4/4
✓ Branch 0 taken 171605 times.
✓ Branch 1 taken 406956 times.
✓ Branch 2 taken 23490 times.
✓ Branch 3 taken 148115 times.
578561 if (pc && pc->pict_type == AV_PICTURE_TYPE_B
967
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 23430 times.
23490 && !sti->avctx->has_b_frames)
968 //FIXME Set low_delay = 0 when has_b_frames = 1
969 60 sti->avctx->has_b_frames = 1;
970
971 /* do we have a video B-frame ? */
972 578561 delay = sti->avctx->has_b_frames;
973 578561 presentation_delayed = 0;
974
975 /* XXX: need has_b_frame, but cannot get it if the codec is
976 * not initialized */
977
4/4
✓ Branch 0 taken 40824 times.
✓ Branch 1 taken 537737 times.
✓ Branch 2 taken 36980 times.
✓ Branch 3 taken 3844 times.
578561 if (delay &&
978
2/2
✓ Branch 0 taken 13490 times.
✓ Branch 1 taken 23490 times.
36980 pc && pc->pict_type != AV_PICTURE_TYPE_B)
979 13490 presentation_delayed = 1;
980
981
4/4
✓ Branch 0 taken 314834 times.
✓ Branch 1 taken 263727 times.
✓ Branch 2 taken 155310 times.
✓ Branch 3 taken 159524 times.
578561 if (pkt->pts != AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE &&
982
3/4
✓ Branch 0 taken 25241 times.
✓ Branch 1 taken 130069 times.
✓ Branch 2 taken 25241 times.
✗ Branch 3 not taken.
155310 st->pts_wrap_bits < 63 && pkt->dts > INT64_MIN + (1LL << st->pts_wrap_bits) &&
983
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25241 times.
25241 pkt->dts - (1LL << (st->pts_wrap_bits - 1)) > pkt->pts) {
984 if (is_relative(sti->cur_dts) || pkt->dts - (1LL<<(st->pts_wrap_bits - 1)) > sti->cur_dts) {
985 pkt->dts -= 1LL << st->pts_wrap_bits;
986 } else
987 pkt->pts += 1LL << st->pts_wrap_bits;
988 }
989
990 /* Some MPEG-2 in MPEG-PS lack dts (issue #171 / input_file.mpg).
991 * We take the conservative approach and discard both.
992 * Note: If this is misbehaving for an H.264 file, then possibly
993 * presentation_delayed is not set correctly. */
994
4/4
✓ Branch 0 taken 26198 times.
✓ Branch 1 taken 552363 times.
✓ Branch 2 taken 21161 times.
✓ Branch 3 taken 5037 times.
578561 if (delay == 1 && pkt->dts == pkt->pts &&
995
4/4
✓ Branch 0 taken 5872 times.
✓ Branch 1 taken 15289 times.
✓ Branch 2 taken 656 times.
✓ Branch 3 taken 5216 times.
21161 pkt->dts != AV_NOPTS_VALUE && presentation_delayed) {
996 656 av_log(s, AV_LOG_DEBUG, "invalid dts/pts combination %"PRIi64"\n", pkt->dts);
997
2/2
✓ Branch 0 taken 321 times.
✓ Branch 1 taken 335 times.
656 if ( strcmp(s->iformat->name, "mov,mp4,m4a,3gp,3g2,mj2")
998
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
999 50 pkt->dts = AV_NOPTS_VALUE;
1000 }
1001
1002 578561 duration = av_mul_q((AVRational) {pkt->duration, 1}, st->time_base);
1003
2/2
✓ Branch 0 taken 341787 times.
✓ Branch 1 taken 236774 times.
578561 if (pkt->duration <= 0) {
1004 341787 compute_frame_duration(s, &num, &den, st, pc, pkt);
1005
3/4
✓ Branch 0 taken 332478 times.
✓ Branch 1 taken 9309 times.
✓ Branch 2 taken 332478 times.
✗ Branch 3 not taken.
341787 if (den && num) {
1006 332478 duration = (AVRational) {num, den};
1007 332478 pkt->duration = av_rescale_rnd(1,
1008 332478 num * (int64_t) st->time_base.den,
1009 332478 den * (int64_t) st->time_base.num,
1010 AV_ROUND_DOWN);
1011 }
1012 }
1013
1014
6/6
✓ Branch 0 taken 566430 times.
✓ Branch 1 taken 12131 times.
✓ Branch 2 taken 400308 times.
✓ Branch 3 taken 166122 times.
✓ Branch 4 taken 43443 times.
✓ Branch 5 taken 356865 times.
578561 if (pkt->duration > 0 && (si->packet_buffer.head || si->parse_queue.head))
1015 209565 update_initial_durations(s, st, pkt->stream_index, pkt->duration);
1016
1017 /* Correct timestamps with byte offset if demuxers only have timestamps
1018 * on packet boundaries */
1019
5/6
✓ Branch 0 taken 171605 times.
✓ Branch 1 taken 406956 times.
✓ Branch 2 taken 944 times.
✓ Branch 3 taken 170661 times.
✓ Branch 4 taken 944 times.
✗ Branch 5 not taken.
578561 if (pc && sti->need_parsing == AVSTREAM_PARSE_TIMESTAMPS && pkt->size) {
1020 /* this will estimate bitrate based on this frame's duration and size */
1021 944 offset = av_rescale(pc->offset, pkt->duration, pkt->size);
1022
2/2
✓ Branch 0 taken 559 times.
✓ Branch 1 taken 385 times.
944 if (pkt->pts != AV_NOPTS_VALUE)
1023 559 pkt->pts += offset;
1024
2/2
✓ Branch 0 taken 121 times.
✓ Branch 1 taken 823 times.
944 if (pkt->dts != AV_NOPTS_VALUE)
1025 121 pkt->dts += offset;
1026 }
1027
1028 /* This may be redundant, but it should not hurt. */
1029
2/2
✓ Branch 0 taken 184284 times.
✓ Branch 1 taken 394277 times.
578561 if (pkt->dts != AV_NOPTS_VALUE &&
1030
2/2
✓ Branch 0 taken 155260 times.
✓ Branch 1 taken 29024 times.
184284 pkt->pts != AV_NOPTS_VALUE &&
1031
2/2
✓ Branch 0 taken 4179 times.
✓ Branch 1 taken 151081 times.
155260 pkt->pts > pkt->dts)
1032 4179 presentation_delayed = 1;
1033
1034
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 578561 times.
578561 if (s->debug & FF_FDEBUG_TS)
1035 av_log(s, AV_LOG_DEBUG,
1036 "IN delayed:%d pts:%s, dts:%s cur_dts:%s st:%d pc:%p duration:%"PRId64" delay:%d onein_oneout:%d\n",
1037 presentation_delayed, av_ts2str(pkt->pts), av_ts2str(pkt->dts), av_ts2str(sti->cur_dts),
1038 pkt->stream_index, pc, pkt->duration, delay, onein_oneout);
1039
1040 /* Interpolate PTS and DTS if they are not present. We skip H264
1041 * currently because delay and has_b_frames are not reliably set. */
1042
8/8
✓ Branch 0 taken 40824 times.
✓ Branch 1 taken 537737 times.
✓ Branch 2 taken 26198 times.
✓ Branch 3 taken 14626 times.
✓ Branch 4 taken 22753 times.
✓ Branch 5 taken 3445 times.
✓ Branch 6 taken 531860 times.
✓ Branch 7 taken 28630 times.
578561 if ((delay == 0 || (delay == 1 && pc)) &&
1043 onein_oneout) {
1044
2/2
✓ Branch 0 taken 5443 times.
✓ Branch 1 taken 526417 times.
531860 if (presentation_delayed) {
1045 /* DTS = decompression timestamp */
1046 /* PTS = presentation timestamp */
1047
2/2
✓ Branch 0 taken 2828 times.
✓ Branch 1 taken 2615 times.
5443 if (pkt->dts == AV_NOPTS_VALUE)
1048 2828 pkt->dts = sti->last_IP_pts;
1049 5443 update_initial_timestamps(s, pkt->stream_index, pkt->dts, pkt->pts, pkt);
1050
2/2
✓ Branch 0 taken 2467 times.
✓ Branch 1 taken 2976 times.
5443 if (pkt->dts == AV_NOPTS_VALUE)
1051 2467 pkt->dts = sti->cur_dts;
1052
1053 /* This is tricky: the dts must be incremented by the duration
1054 * of the frame we are displaying, i.e. the last I- or P-frame. */
1055
3/4
✓ Branch 0 taken 231 times.
✓ Branch 1 taken 5212 times.
✓ Branch 2 taken 231 times.
✗ Branch 3 not taken.
5443 if (sti->last_IP_duration == 0 && (uint64_t)pkt->duration <= INT32_MAX)
1056 231 sti->last_IP_duration = pkt->duration;
1057
2/2
✓ Branch 0 taken 5441 times.
✓ Branch 1 taken 2 times.
5443 if (pkt->dts != AV_NOPTS_VALUE)
1058 5441 sti->cur_dts = av_sat_add64(pkt->dts, sti->last_IP_duration);
1059
2/2
✓ Branch 0 taken 5441 times.
✓ Branch 1 taken 2 times.
5443 if (pkt->dts != AV_NOPTS_VALUE &&
1060
2/2
✓ Branch 0 taken 3175 times.
✓ Branch 1 taken 2266 times.
5441 pkt->pts == AV_NOPTS_VALUE &&
1061
2/2
✓ Branch 0 taken 3173 times.
✓ Branch 1 taken 2 times.
3175 sti->last_IP_duration > 0 &&
1062
4/4
✓ Branch 0 taken 723 times.
✓ Branch 1 taken 2450 times.
✓ Branch 2 taken 717 times.
✓ Branch 3 taken 6 times.
3173 ((uint64_t)sti->cur_dts - (uint64_t)next_dts + 1) <= 2 &&
1063
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 706 times.
717 next_dts != next_pts &&
1064 next_pts != AV_NOPTS_VALUE)
1065 11 pkt->pts = next_dts;
1066
1067
1/2
✓ Branch 0 taken 5443 times.
✗ Branch 1 not taken.
5443 if ((uint64_t)pkt->duration <= INT32_MAX)
1068 5443 sti->last_IP_duration = pkt->duration;
1069 5443 sti->last_IP_pts = pkt->pts;
1070 /* Cannot compute PTS if not present (we can compute it only
1071 * by knowing the future. */
1072
2/2
✓ Branch 0 taken 224219 times.
✓ Branch 1 taken 302198 times.
526417 } else if (pkt->pts != AV_NOPTS_VALUE ||
1073
2/2
✓ Branch 0 taken 195980 times.
✓ Branch 1 taken 28239 times.
224219 pkt->dts != AV_NOPTS_VALUE ||
1074
2/2
✓ Branch 0 taken 193506 times.
✓ Branch 1 taken 2474 times.
195980 pkt->duration > 0 ) {
1075
1076 /* presentation is not delayed : PTS and DTS are the same */
1077
2/2
✓ Branch 0 taken 221745 times.
✓ Branch 1 taken 302198 times.
523943 if (pkt->pts == AV_NOPTS_VALUE)
1078 221745 pkt->pts = pkt->dts;
1079 523943 update_initial_timestamps(s, pkt->stream_index, pkt->pts,
1080 pkt->pts, pkt);
1081
2/2
✓ Branch 0 taken 193506 times.
✓ Branch 1 taken 330437 times.
523943 if (pkt->pts == AV_NOPTS_VALUE)
1082 193506 pkt->pts = sti->cur_dts;
1083 523943 pkt->dts = pkt->pts;
1084
3/4
✓ Branch 0 taken 523943 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 523935 times.
✓ Branch 3 taken 8 times.
523943 if (pkt->pts != AV_NOPTS_VALUE && duration.num >= 0)
1085 523935 sti->cur_dts = av_add_stable(st->time_base, pkt->pts, duration, 1);
1086 }
1087 }
1088
1089
3/4
✓ Branch 0 taken 536590 times.
✓ Branch 1 taken 41971 times.
✓ Branch 2 taken 536590 times.
✗ Branch 3 not taken.
578561 if (pkt->pts != AV_NOPTS_VALUE && delay <= MAX_REORDER_DELAY) {
1090 536590 sti->pts_buffer[0] = pkt->pts;
1091
4/4
✓ Branch 0 taken 19083 times.
✓ Branch 1 taken 532141 times.
✓ Branch 2 taken 14634 times.
✓ Branch 3 taken 4449 times.
551224 for (int i = 0; i < delay && sti->pts_buffer[i] > sti->pts_buffer[i + 1]; i++)
1092 14634 FFSWAP(int64_t, sti->pts_buffer[i], sti->pts_buffer[i + 1]);
1093
1094
2/2
✓ Branch 1 taken 535997 times.
✓ Branch 2 taken 593 times.
536590 if (has_decode_delay_been_guessed(st))
1095 535997 pkt->dts = select_from_pts_buffer(st, sti->pts_buffer, pkt->dts);
1096 }
1097 // We skipped it above so we try here.
1098
2/2
✓ Branch 0 taken 43269 times.
✓ Branch 1 taken 535292 times.
578561 if (!onein_oneout)
1099 // This should happen on the first packet
1100 43269 update_initial_timestamps(s, pkt->stream_index, pkt->dts, pkt->pts, pkt);
1101
2/2
✓ Branch 0 taken 6550 times.
✓ Branch 1 taken 572011 times.
578561 if (pkt->dts > sti->cur_dts)
1102 6550 sti->cur_dts = pkt->dts;
1103
1104
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 578561 times.
578561 if (s->debug & FF_FDEBUG_TS)
1105 av_log(s, AV_LOG_DEBUG, "OUTdelayed:%d/%d pts:%s, dts:%s cur_dts:%s st:%d (%d)\n",
1106 presentation_delayed, delay, av_ts2str(pkt->pts), av_ts2str(pkt->dts), av_ts2str(sti->cur_dts), st->index, st->id);
1107
1108 /* update flags */
1109
4/4
✓ Branch 0 taken 578478 times.
✓ Branch 1 taken 83 times.
✓ Branch 3 taken 456332 times.
✓ Branch 4 taken 122146 times.
578561 if (st->codecpar->codec_type == AVMEDIA_TYPE_DATA || ff_is_intra_only(st->codecpar->codec_id))
1110 456415 pkt->flags |= AV_PKT_FLAG_KEY;
1111 }
1112
1113 /**
1114 * Parse a packet, add all split parts to parse_queue.
1115 *
1116 * @param pkt Packet to parse; must not be NULL.
1117 * @param flush Indicates whether to flush. If set, pkt must be blank.
1118 */
1119 617140 static int parse_packet(AVFormatContext *s, AVPacket *pkt,
1120 int stream_index, int flush)
1121 {
1122 617140 FFFormatContext *const si = ffformatcontext(s);
1123 617140 AVPacket *out_pkt = si->parse_pkt;
1124 617140 AVStream *st = s->streams[stream_index];
1125 617140 FFStream *const sti = ffstream(st);
1126 617140 const uint8_t *data = pkt->data;
1127 617140 int size = pkt->size;
1128 617140 int ret = 0, got_output = flush;
1129
1130
3/6
✓ Branch 0 taken 1797 times.
✓ Branch 1 taken 615343 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1797 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
617140 if (!size && !flush && sti->parser->flags & PARSER_FLAG_COMPLETE_FRAMES) {
1131 // preserve 0-size sync packets
1132 compute_pkt_fields(s, st, sti->parser, pkt, AV_NOPTS_VALUE, AV_NOPTS_VALUE);
1133 }
1134
1135
6/6
✓ Branch 0 taken 722431 times.
✓ Branch 1 taken 619949 times.
✓ Branch 2 taken 4606 times.
✓ Branch 3 taken 615343 times.
✓ Branch 4 taken 2809 times.
✓ Branch 5 taken 1797 times.
1342380 while (size > 0 || (flush && got_output)) {
1136 725240 int64_t next_pts = pkt->pts;
1137 725240 int64_t next_dts = pkt->dts;
1138 int len;
1139
1140 725240 len = av_parser_parse2(sti->parser, sti->avctx,
1141 &out_pkt->data, &out_pkt->size, data, size,
1142 pkt->pts, pkt->dts, pkt->pos);
1143
1144 725240 pkt->pts = pkt->dts = AV_NOPTS_VALUE;
1145 725240 pkt->pos = -1;
1146 /* increment read pointer */
1147 av_assert1(data || !len);
1148
2/2
✓ Branch 0 taken 713999 times.
✓ Branch 1 taken 11241 times.
725240 data = len ? data + len : data;
1149 725240 size -= len;
1150
1151 725240 got_output = !!out_pkt->size;
1152
1153
2/2
✓ Branch 0 taken 553635 times.
✓ Branch 1 taken 171605 times.
725240 if (!out_pkt->size)
1154 553635 continue;
1155
1156
4/4
✓ Branch 0 taken 170593 times.
✓ Branch 1 taken 1012 times.
✓ Branch 2 taken 63099 times.
✓ Branch 3 taken 107494 times.
171605 if (pkt->buf && out_pkt->data == pkt->data) {
1157 /* reference pkt->buf only when out_pkt->data is guaranteed to point
1158 * to data in it and not in the parser's internal buffer. */
1159 /* XXX: Ensure this is the case with all parsers when sti->parser->flags
1160 * is PARSER_FLAG_COMPLETE_FRAMES and check for that instead? */
1161 63099 out_pkt->buf = av_buffer_ref(pkt->buf);
1162
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 63099 times.
63099 if (!out_pkt->buf) {
1163 ret = AVERROR(ENOMEM);
1164 goto fail;
1165 }
1166 } else {
1167 108506 ret = av_packet_make_refcounted(out_pkt);
1168
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 108506 times.
108506 if (ret < 0)
1169 goto fail;
1170 }
1171
1172
2/2
✓ Branch 0 taken 8675 times.
✓ Branch 1 taken 162930 times.
171605 if (pkt->side_data) {
1173 8675 out_pkt->side_data = pkt->side_data;
1174 8675 out_pkt->side_data_elems = pkt->side_data_elems;
1175 8675 pkt->side_data = NULL;
1176 8675 pkt->side_data_elems = 0;
1177 }
1178
1179 /* set the duration */
1180
2/2
✓ Branch 0 taken 42991 times.
✓ Branch 1 taken 128614 times.
171605 out_pkt->duration = (sti->parser->flags & PARSER_FLAG_COMPLETE_FRAMES) ? pkt->duration : 0;
1181
2/2
✓ Branch 0 taken 106458 times.
✓ Branch 1 taken 65147 times.
171605 if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
1182
2/2
✓ Branch 0 taken 106360 times.
✓ Branch 1 taken 98 times.
106458 if (sti->avctx->sample_rate > 0) {
1183 106360 out_pkt->duration =
1184 106360 av_rescale_q_rnd(sti->parser->duration,
1185 106360 (AVRational) { 1, sti->avctx->sample_rate },
1186 st->time_base,
1187 AV_ROUND_DOWN);
1188 }
1189 }
1190
1191 171605 out_pkt->stream_index = st->index;
1192 171605 out_pkt->pts = sti->parser->pts;
1193 171605 out_pkt->dts = sti->parser->dts;
1194 171605 out_pkt->pos = sti->parser->pos;
1195 171605 out_pkt->flags |= pkt->flags & (AV_PKT_FLAG_DISCARD | AV_PKT_FLAG_CORRUPT);
1196
1197
2/2
✓ Branch 0 taken 107111 times.
✓ Branch 1 taken 64494 times.
171605 if (sti->need_parsing == AVSTREAM_PARSE_FULL_RAW)
1198 107111 out_pkt->pos = sti->parser->frame_offset;
1199
1200
2/2
✓ Branch 0 taken 166267 times.
✓ Branch 1 taken 5338 times.
171605 if (sti->parser->key_frame == 1 ||
1201
2/2
✓ Branch 0 taken 95324 times.
✓ Branch 1 taken 70943 times.
166267 (sti->parser->key_frame == -1 &&
1202
2/2
✓ Branch 0 taken 84369 times.
✓ Branch 1 taken 10955 times.
95324 sti->parser->pict_type == AV_PICTURE_TYPE_I))
1203 89707 out_pkt->flags |= AV_PKT_FLAG_KEY;
1204
1205
6/6
✓ Branch 0 taken 95324 times.
✓ Branch 1 taken 76281 times.
✓ Branch 2 taken 280 times.
✓ Branch 3 taken 95044 times.
✓ Branch 4 taken 261 times.
✓ Branch 5 taken 19 times.
171605 if (sti->parser->key_frame == -1 && sti->parser->pict_type ==AV_PICTURE_TYPE_NONE && (pkt->flags&AV_PKT_FLAG_KEY))
1206 261 out_pkt->flags |= AV_PKT_FLAG_KEY;
1207
1208 171605 compute_pkt_fields(s, st, sti->parser, out_pkt, next_dts, next_pts);
1209
1210 171605 ret = avpriv_packet_list_put(&si->parse_queue,
1211 out_pkt, NULL, 0);
1212
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 171605 times.
171605 if (ret < 0)
1213 goto fail;
1214 }
1215
1216 /* end of the stream => close and free the parser */
1217
2/2
✓ Branch 0 taken 615343 times.
✓ Branch 1 taken 1797 times.
617140 if (flush) {
1218 1797 av_parser_close(sti->parser);
1219 1797 sti->parser = NULL;
1220 }
1221
1222 615343 fail:
1223
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 617140 times.
617140 if (ret < 0)
1224 av_packet_unref(out_pkt);
1225 617140 av_packet_unref(pkt);
1226 617140 return ret;
1227 }
1228
1229 8774 static int64_t ts_to_samples(AVStream *st, int64_t ts)
1230 {
1231 8774 return av_rescale(ts, st->time_base.num * st->codecpar->sample_rate, st->time_base.den);
1232 }
1233
1234 582729 static int read_frame_internal(AVFormatContext *s, AVPacket *pkt)
1235 {
1236 582729 FFFormatContext *const si = ffformatcontext(s);
1237 582729 int ret, got_packet = 0;
1238 582729 AVDictionary *metadata = NULL;
1239
1240
4/4
✓ Branch 0 taken 1198203 times.
✓ Branch 1 taken 406956 times.
✓ Branch 2 taken 1028435 times.
✓ Branch 3 taken 169768 times.
1605159 while (!got_packet && !si->parse_queue.head) {
1241 AVStream *st;
1242 FFStream *sti;
1243
1244 /* read next packet */
1245 1028435 ret = ff_read_packet(s, pkt);
1246
2/2
✓ Branch 0 taken 6005 times.
✓ Branch 1 taken 1022430 times.
1028435 if (ret < 0) {
1247
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6004 times.
6005 if (ret == AVERROR(EAGAIN))
1248 1 return ret;
1249 /* flush the parsers */
1250
2/2
✓ Branch 0 taken 6857 times.
✓ Branch 1 taken 6004 times.
12861 for (unsigned i = 0; i < s->nb_streams; i++) {
1251 6857 AVStream *const st = s->streams[i];
1252 6857 FFStream *const sti = ffstream(st);
1253
4/4
✓ Branch 0 taken 2336 times.
✓ Branch 1 taken 4521 times.
✓ Branch 2 taken 1797 times.
✓ Branch 3 taken 539 times.
6857 if (sti->parser && sti->need_parsing)
1254 1797 parse_packet(s, pkt, st->index, 1);
1255 }
1256 /* all remaining packets are now in parse_queue =>
1257 * really terminate parsing */
1258 6004 break;
1259 }
1260 1022430 ret = 0;
1261 1022430 st = s->streams[pkt->stream_index];
1262 1022430 sti = ffstream(st);
1263
1264 1022430 st->event_flags |= AVSTREAM_EVENT_FLAG_NEW_PACKETS;
1265
1266 /* update context if required */
1267
2/2
✓ Branch 0 taken 156 times.
✓ Branch 1 taken 1022274 times.
1022430 if (sti->need_context_update) {
1268
2/2
✓ Branch 1 taken 13 times.
✓ Branch 2 taken 143 times.
156 if (avcodec_is_open(sti->avctx)) {
1269 13 av_log(s, AV_LOG_DEBUG, "Demuxer context update while decoder is open, closing and trying to re-open\n");
1270 13 avcodec_close(sti->avctx);
1271 13 sti->info->found_decoder = 0;
1272 }
1273
1274 /* close parser, because it depends on the codec */
1275
4/4
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 143 times.
✓ Branch 2 taken 10 times.
✓ Branch 3 taken 3 times.
156 if (sti->parser && sti->avctx->codec_id != st->codecpar->codec_id) {
1276 10 av_parser_close(sti->parser);
1277 10 sti->parser = NULL;
1278 }
1279
1280 156 ret = avcodec_parameters_to_context(sti->avctx, st->codecpar);
1281
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 156 times.
156 if (ret < 0) {
1282 av_packet_unref(pkt);
1283 return ret;
1284 }
1285
1286 156 sti->need_context_update = 0;
1287 }
1288
1289
2/2
✓ Branch 0 taken 311821 times.
✓ Branch 1 taken 710609 times.
1022430 if (pkt->pts != AV_NOPTS_VALUE &&
1290
2/2
✓ Branch 0 taken 155908 times.
✓ Branch 1 taken 155913 times.
311821 pkt->dts != AV_NOPTS_VALUE &&
1291
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 155908 times.
155908 pkt->pts < pkt->dts) {
1292 av_log(s, AV_LOG_WARNING,
1293 "Invalid timestamps stream=%d, pts=%s, dts=%s, size=%d\n",
1294 pkt->stream_index,
1295 av_ts2str(pkt->pts),
1296 av_ts2str(pkt->dts),
1297 pkt->size);
1298 }
1299
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1022430 times.
1022430 if (s->debug & FF_FDEBUG_TS)
1300 av_log(s, AV_LOG_DEBUG,
1301 "ff_read_packet stream=%d, pts=%s, dts=%s, size=%d, duration=%"PRId64", flags=%d\n",
1302 pkt->stream_index,
1303 av_ts2str(pkt->pts),
1304 av_ts2str(pkt->dts),
1305 pkt->size, pkt->duration, pkt->flags);
1306
1307
5/6
✓ Branch 0 taken 616633 times.
✓ Branch 1 taken 405797 times.
✓ Branch 2 taken 2680 times.
✓ Branch 3 taken 613953 times.
✓ Branch 4 taken 2680 times.
✗ Branch 5 not taken.
1022430 if (sti->need_parsing && !sti->parser && !(s->flags & AVFMT_FLAG_NOPARSE)) {
1308 2680 sti->parser = av_parser_init(st->codecpar->codec_id);
1309
2/2
✓ Branch 0 taken 1159 times.
✓ Branch 1 taken 1521 times.
2680 if (!sti->parser) {
1310 1159 av_log(s, AV_LOG_VERBOSE, "parser not found for codec "
1311 "%s, packets or times may be invalid.\n",
1312 1159 avcodec_get_name(st->codecpar->codec_id));
1313 /* no parser available: just output the raw packets */
1314 1159 sti->need_parsing = AVSTREAM_PARSE_NONE;
1315
2/2
✓ Branch 0 taken 672 times.
✓ Branch 1 taken 849 times.
1521 } else if (sti->need_parsing == AVSTREAM_PARSE_HEADERS)
1316 672 sti->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES;
1317
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 849 times.
849 else if (sti->need_parsing == AVSTREAM_PARSE_FULL_ONCE)
1318 sti->parser->flags |= PARSER_FLAG_ONCE;
1319
2/2
✓ Branch 0 taken 436 times.
✓ Branch 1 taken 413 times.
849 else if (sti->need_parsing == AVSTREAM_PARSE_FULL_RAW)
1320 436 sti->parser->flags |= PARSER_FLAG_USE_CODEC_TS;
1321 }
1322
1323
3/4
✓ Branch 0 taken 615474 times.
✓ Branch 1 taken 406956 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 615474 times.
1022430 if (!sti->need_parsing || !sti->parser) {
1324 /* no parsing needed: we just output the packet as is */
1325 406956 compute_pkt_fields(s, st, NULL, pkt, AV_NOPTS_VALUE, AV_NOPTS_VALUE);
1326
2/2
✓ Branch 0 taken 135603 times.
✓ Branch 1 taken 271353 times.
406956 if ((s->iformat->flags & AVFMT_GENERIC_INDEX) &&
1327
4/4
✓ Branch 0 taken 133147 times.
✓ Branch 1 taken 2456 times.
✓ Branch 2 taken 131868 times.
✓ Branch 3 taken 1279 times.
135603 (pkt->flags & AV_PKT_FLAG_KEY) && pkt->dts != AV_NOPTS_VALUE) {
1328 131868 ff_reduce_index(s, st->index);
1329 131868 av_add_index_entry(st, pkt->pos, pkt->dts,
1330 0, 0, AVINDEX_KEYFRAME);
1331 }
1332 406956 got_packet = 1;
1333
2/2
✓ Branch 0 taken 615343 times.
✓ Branch 1 taken 131 times.
615474 } else if (st->discard < AVDISCARD_ALL) {
1334
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 615343 times.
615343 if ((ret = parse_packet(s, pkt, pkt->stream_index, 0)) < 0)
1335 return ret;
1336 615343 st->codecpar->sample_rate = sti->avctx->sample_rate;
1337 615343 st->codecpar->bit_rate = sti->avctx->bit_rate;
1338 #if FF_API_OLD_CHANNEL_LAYOUT
1339 FF_DISABLE_DEPRECATION_WARNINGS
1340 615343 st->codecpar->channels = sti->avctx->ch_layout.nb_channels;
1341 1230686 st->codecpar->channel_layout = sti->avctx->ch_layout.order == AV_CHANNEL_ORDER_NATIVE ?
1342
2/2
✓ Branch 0 taken 123081 times.
✓ Branch 1 taken 492262 times.
615343 sti->avctx->ch_layout.u.mask : 0;
1343 FF_ENABLE_DEPRECATION_WARNINGS
1344 #endif
1345 615343 ret = av_channel_layout_copy(&st->codecpar->ch_layout, &sti->avctx->ch_layout);
1346
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 615343 times.
615343 if (ret < 0)
1347 return ret;
1348 615343 st->codecpar->codec_id = sti->avctx->codec_id;
1349 } else {
1350 /* free packet */
1351 131 av_packet_unref(pkt);
1352 }
1353
2/2
✓ Branch 0 taken 381493 times.
✓ Branch 1 taken 640937 times.
1022430 if (pkt->flags & AV_PKT_FLAG_KEY)
1354 381493 sti->skip_to_keyframe = 0;
1355
2/2
✓ Branch 0 taken 49 times.
✓ Branch 1 taken 1022381 times.
1022430 if (sti->skip_to_keyframe) {
1356 49 av_packet_unref(pkt);
1357 49 got_packet = 0;
1358 }
1359 }
1360
1361
4/4
✓ Branch 0 taken 175772 times.
✓ Branch 1 taken 406956 times.
✓ Branch 2 taken 170537 times.
✓ Branch 3 taken 5235 times.
582728 if (!got_packet && si->parse_queue.head)
1362 170537 ret = avpriv_packet_list_get(&si->parse_queue, pkt);
1363
1364
2/2
✓ Branch 0 taken 577493 times.
✓ Branch 1 taken 5235 times.
582728 if (ret >= 0) {
1365 577493 AVStream *const st = s->streams[pkt->stream_index];
1366 577493 FFStream *const sti = ffstream(st);
1367 577493 int discard_padding = 0;
1368
3/4
✓ Branch 0 taken 4387 times.
✓ Branch 1 taken 573106 times.
✓ Branch 2 taken 4387 times.
✗ Branch 3 not taken.
577493 if (sti->first_discard_sample && pkt->pts != AV_NOPTS_VALUE) {
1369
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);
1370 4387 int64_t sample = ts_to_samples(st, pts);
1371 4387 int64_t duration = ts_to_samples(st, pkt->duration);
1372 4387 int64_t end_sample = sample + duration;
1373
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 &&
1374
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 sample < sti->last_discard_sample)
1375 14 discard_padding = FFMIN(end_sample - sti->first_discard_sample, duration);
1376 }
1377
6/6
✓ Branch 0 taken 4387 times.
✓ Branch 1 taken 573106 times.
✓ Branch 2 taken 4371 times.
✓ Branch 3 taken 16 times.
✓ Branch 4 taken 20 times.
✓ Branch 5 taken 4351 times.
577493 if (sti->start_skip_samples && (pkt->pts == 0 || pkt->pts == RELATIVE_TS_BASE))
1378 36 sti->skip_samples = sti->start_skip_samples;
1379 577493 sti->skip_samples = FFMAX(0, sti->skip_samples);
1380
4/4
✓ Branch 0 taken 577407 times.
✓ Branch 1 taken 86 times.
✓ Branch 2 taken 14 times.
✓ Branch 3 taken 577393 times.
577493 if (sti->skip_samples || discard_padding) {
1381 100 uint8_t *p = av_packet_new_side_data(pkt, AV_PKT_DATA_SKIP_SAMPLES, 10);
1382
1/2
✓ Branch 0 taken 100 times.
✗ Branch 1 not taken.
100 if (p) {
1383 100 AV_WL32(p, sti->skip_samples);
1384 100 AV_WL32(p + 4, discard_padding);
1385 100 av_log(s, AV_LOG_DEBUG, "demuxer injecting skip %u / discard %u\n",
1386 100 (unsigned)sti->skip_samples, (unsigned)discard_padding);
1387 }
1388 100 sti->skip_samples = 0;
1389 }
1390
1391
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 577493 times.
577493 if (sti->inject_global_side_data) {
1392 for (int i = 0; i < st->nb_side_data; i++) {
1393 const AVPacketSideData *const src_sd = &st->side_data[i];
1394 uint8_t *dst_data;
1395
1396 if (av_packet_get_side_data(pkt, src_sd->type, NULL))
1397 continue;
1398
1399 dst_data = av_packet_new_side_data(pkt, src_sd->type, src_sd->size);
1400 if (!dst_data) {
1401 av_log(s, AV_LOG_WARNING, "Could not inject global side data\n");
1402 continue;
1403 }
1404
1405 memcpy(dst_data, src_sd->data, src_sd->size);
1406 }
1407 sti->inject_global_side_data = 0;
1408 }
1409 }
1410
1411
2/2
✓ Branch 0 taken 6829 times.
✓ Branch 1 taken 575899 times.
582728 if (!si->metafree) {
1412 6829 int metaret = av_opt_get_dict_val(s, "metadata", AV_OPT_SEARCH_CHILDREN, &metadata);
1413
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6829 times.
6829 if (metadata) {
1414 s->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
1415 av_dict_copy(&s->metadata, metadata, 0);
1416 av_dict_free(&metadata);
1417 av_opt_set_dict_val(s, "metadata", NULL, AV_OPT_SEARCH_CHILDREN);
1418 }
1419 6829 si->metafree = metaret == AVERROR_OPTION_NOT_FOUND;
1420 }
1421
1422
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 582728 times.
582728 if (s->debug & FF_FDEBUG_TS)
1423 av_log(s, AV_LOG_DEBUG,
1424 "read_frame_internal stream=%d, pts=%s, dts=%s, "
1425 "size=%d, duration=%"PRId64", flags=%d\n",
1426 pkt->stream_index,
1427 av_ts2str(pkt->pts),
1428 av_ts2str(pkt->dts),
1429 pkt->size, pkt->duration, pkt->flags);
1430
1431 /* A demuxer might have returned EOF because of an IO error, let's
1432 * propagate this back to the user. */
1433
5/8
✓ Branch 0 taken 5109 times.
✓ Branch 1 taken 577619 times.
✓ Branch 2 taken 4898 times.
✓ Branch 3 taken 211 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 4898 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
582728 if (ret == AVERROR_EOF && s->pb && s->pb->error < 0 && s->pb->error != AVERROR(EAGAIN))
1434 ret = s->pb->error;
1435
1436 582728 return ret;
1437 }
1438
1439 485311 int av_read_frame(AVFormatContext *s, AVPacket *pkt)
1440 {
1441 485311 FFFormatContext *const si = ffformatcontext(s);
1442 485311 const int genpts = s->flags & AVFMT_FLAG_GENPTS;
1443 485311 int eof = 0;
1444 int ret;
1445 AVStream *st;
1446
1447
1/2
✓ Branch 0 taken 485311 times.
✗ Branch 1 not taken.
485311 if (!genpts) {
1448 970622 ret = si->packet_buffer.head
1449 83322 ? avpriv_packet_list_get(&si->packet_buffer, pkt)
1450
2/2
✓ Branch 0 taken 83322 times.
✓ Branch 1 taken 401989 times.
485311 : read_frame_internal(s, pkt);
1451
2/2
✓ Branch 0 taken 3987 times.
✓ Branch 1 taken 481324 times.
485311 if (ret < 0)
1452 3987 return ret;
1453 481324 goto return_packet;
1454 }
1455
1456 for (;;) {
1457 PacketListEntry *pktl = si->packet_buffer.head;
1458
1459 if (pktl) {
1460 AVPacket *next_pkt = &pktl->pkt;
1461
1462 if (next_pkt->dts != AV_NOPTS_VALUE) {
1463 int wrap_bits = s->streams[next_pkt->stream_index]->pts_wrap_bits;
1464 // last dts seen for this stream. if any of packets following
1465 // current one had no dts, we will set this to AV_NOPTS_VALUE.
1466 int64_t last_dts = next_pkt->dts;
1467 av_assert2(wrap_bits <= 64);
1468 while (pktl && next_pkt->pts == AV_NOPTS_VALUE) {
1469 if (pktl->pkt.stream_index == next_pkt->stream_index &&
1470 av_compare_mod(next_pkt->dts, pktl->pkt.dts, 2ULL << (wrap_bits - 1)) < 0) {
1471 if (av_compare_mod(pktl->pkt.pts, pktl->pkt.dts, 2ULL << (wrap_bits - 1))) {
1472 // not B-frame
1473 next_pkt->pts = pktl->pkt.dts;
1474 }
1475 if (last_dts != AV_NOPTS_VALUE) {
1476 // Once last dts was set to AV_NOPTS_VALUE, we don't change it.
1477 last_dts = pktl->pkt.dts;
1478 }
1479 }
1480 pktl = pktl->next;
1481 }
1482 if (eof && next_pkt->pts == AV_NOPTS_VALUE && last_dts != AV_NOPTS_VALUE) {
1483 // Fixing the last reference frame had none pts issue (For MXF etc).
1484 // We only do this when
1485 // 1. eof.
1486 // 2. we are not able to resolve a pts value for current packet.
1487 // 3. the packets for this stream at the end of the files had valid dts.
1488 next_pkt->pts = last_dts + next_pkt->duration;
1489 }
1490 pktl = si->packet_buffer.head;
1491 }
1492
1493 /* read packet from packet buffer, if there is data */
1494 st = s->streams[next_pkt->stream_index];
1495 if (!(next_pkt->pts == AV_NOPTS_VALUE && st->discard < AVDISCARD_ALL &&
1496 next_pkt->dts != AV_NOPTS_VALUE && !eof)) {
1497 ret = avpriv_packet_list_get(&si->packet_buffer, pkt);
1498 goto return_packet;
1499 }
1500 }
1501
1502 ret = read_frame_internal(s, pkt);
1503 if (ret < 0) {
1504 if (pktl && ret != AVERROR(EAGAIN)) {
1505 eof = 1;
1506 continue;
1507 } else
1508 return ret;
1509 }
1510
1511 ret = avpriv_packet_list_put(&si->packet_buffer,
1512 pkt, NULL, 0);
1513 if (ret < 0) {
1514 av_packet_unref(pkt);
1515 return ret;
1516 }
1517 }
1518
1519 481324 return_packet:
1520 481324 st = s->streams[pkt->stream_index];
1521
4/4
✓ Branch 0 taken 224957 times.
✓ Branch 1 taken 256367 times.
✓ Branch 2 taken 156716 times.
✓ Branch 3 taken 68241 times.
481324 if ((s->iformat->flags & AVFMT_GENERIC_INDEX) && pkt->flags & AV_PKT_FLAG_KEY) {
1522 156716 ff_reduce_index(s, st->index);
1523 156716 av_add_index_entry(st, pkt->pos, pkt->dts, 0, 0, AVINDEX_KEYFRAME);
1524 }
1525
1526
2/2
✓ Branch 1 taken 159225 times.
✓ Branch 2 taken 322099 times.
481324 if (is_relative(pkt->dts))
1527 159225 pkt->dts -= RELATIVE_TS_BASE;
1528
2/2
✓ Branch 1 taken 157581 times.
✓ Branch 2 taken 323743 times.
481324 if (is_relative(pkt->pts))
1529 157581 pkt->pts -= RELATIVE_TS_BASE;
1530
1531 481324 return ret;
1532 }
1533
1534 /**
1535 * Return TRUE if the stream has accurate duration in any stream.
1536 *
1537 * @return TRUE if the stream has accurate duration for at least one component.
1538 */
1539 6752 static int has_duration(AVFormatContext *ic)
1540 {
1541
2/2
✓ Branch 0 taken 6920 times.
✓ Branch 1 taken 2191 times.
9111 for (unsigned i = 0; i < ic->nb_streams; i++) {
1542 6920 const AVStream *const st = ic->streams[i];
1543
2/2
✓ Branch 0 taken 4561 times.
✓ Branch 1 taken 2359 times.
6920 if (st->duration != AV_NOPTS_VALUE)
1544 4561 return 1;
1545 }
1546
2/2
✓ Branch 0 taken 408 times.
✓ Branch 1 taken 1783 times.
2191 if (ic->duration != AV_NOPTS_VALUE)
1547 408 return 1;
1548 1783 return 0;
1549 }
1550
1551 /**
1552 * Estimate the stream timings from the one of each components.
1553 *
1554 * Also computes the global bitrate if possible.
1555 */
1556 11843 static void update_stream_timings(AVFormatContext *ic)
1557 {
1558 int64_t start_time, start_time1, start_time_text, end_time, end_time1, end_time_text;
1559 int64_t duration, duration1, duration_text, filesize;
1560
1561 11843 start_time = INT64_MAX;
1562 11843 start_time_text = INT64_MAX;
1563 11843 end_time = INT64_MIN;
1564 11843 end_time_text = INT64_MIN;
1565 11843 duration = INT64_MIN;
1566 11843 duration_text = INT64_MIN;
1567
1568
2/2
✓ Branch 0 taken 13006 times.
✓ Branch 1 taken 11843 times.
24849 for (unsigned i = 0; i < ic->nb_streams; i++) {
1569 13006 AVStream *const st = ic->streams[i];
1570
2/2
✓ Branch 0 taken 12899 times.
✓ Branch 1 taken 107 times.
25905 int is_text = st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE ||
1571
2/2
✓ Branch 0 taken 141 times.
✓ Branch 1 taken 12758 times.
12899 st->codecpar->codec_type == AVMEDIA_TYPE_DATA;
1572
1573
3/4
✓ Branch 0 taken 10619 times.
✓ Branch 1 taken 2387 times.
✓ Branch 2 taken 10619 times.
✗ Branch 3 not taken.
13006 if (st->start_time != AV_NOPTS_VALUE && st->time_base.den) {
1574 10619 start_time1 = av_rescale_q(st->start_time, st->time_base,
1575 10619 AV_TIME_BASE_Q);
1576
2/2
✓ Branch 0 taken 139 times.
✓ Branch 1 taken 10480 times.
10619 if (is_text)
1577 139 start_time_text = FFMIN(start_time_text, start_time1);
1578 else
1579 10480 start_time = FFMIN(start_time, start_time1);
1580 10619 end_time1 = av_rescale_q_rnd(st->duration, st->time_base,
1581 10619 AV_TIME_BASE_Q,
1582 AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
1583
5/6
✓ Branch 0 taken 9381 times.
✓ Branch 1 taken 1238 times.
✓ Branch 2 taken 9350 times.
✓ Branch 3 taken 31 times.
✓ Branch 4 taken 9381 times.
✗ Branch 5 not taken.
10619 if (end_time1 != AV_NOPTS_VALUE && (end_time1 > 0 ? start_time1 <= INT64_MAX - end_time1 : start_time1 >= INT64_MIN - end_time1)) {
1584 9381 end_time1 += start_time1;
1585
2/2
✓ Branch 0 taken 124 times.
✓ Branch 1 taken 9257 times.
9381 if (is_text)
1586 124 end_time_text = FFMAX(end_time_text, end_time1);
1587 else
1588 9257 end_time = FFMAX(end_time, end_time1);
1589 }
1590
2/2
✓ Branch 1 taken 176 times.
✓ Branch 2 taken 10619 times.
10795 for (AVProgram *p = NULL; (p = av_find_program_from_stream(ic, p, i)); ) {
1591
4/4
✓ Branch 0 taken 128 times.
✓ Branch 1 taken 48 times.
✓ Branch 2 taken 23 times.
✓ Branch 3 taken 105 times.
176 if (p->start_time == AV_NOPTS_VALUE || p->start_time > start_time1)
1592 71 p->start_time = start_time1;
1593
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 134 times.
176 if (p->end_time < end_time1)
1594 42 p->end_time = end_time1;
1595 }
1596 }
1597
2/2
✓ Branch 0 taken 10800 times.
✓ Branch 1 taken 2206 times.
13006 if (st->duration != AV_NOPTS_VALUE) {
1598 10800 duration1 = av_rescale_q(st->duration, st->time_base,
1599 10800 AV_TIME_BASE_Q);
1600
2/2
✓ Branch 0 taken 148 times.
✓ Branch 1 taken 10652 times.
10800 if (is_text)
1601 148 duration_text = FFMAX(duration_text, duration1);
1602 else
1603 10652 duration = FFMAX(duration, duration1);
1604 }
1605 }
1606
3/6
✓ Branch 0 taken 9596 times.
✓ Branch 1 taken 2247 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 9596 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
11843 if (start_time == INT64_MAX || (start_time > start_time_text && start_time - (uint64_t)start_time_text < AV_TIME_BASE))
1607 2247 start_time = start_time_text;
1608
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9596 times.
9596 else if (start_time > start_time_text)
1609 av_log(ic, AV_LOG_VERBOSE, "Ignoring outlier non primary stream starttime %f\n", start_time_text / (float)AV_TIME_BASE);
1610
1611
5/6
✓ Branch 0 taken 8664 times.
✓ Branch 1 taken 3179 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 8662 times.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
11843 if (end_time == INT64_MIN || (end_time < end_time_text && end_time_text - (uint64_t)end_time < AV_TIME_BASE))
1612 3181 end_time = end_time_text;
1613
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8662 times.
8662 else if (end_time < end_time_text)
1614 av_log(ic, AV_LOG_VERBOSE, "Ignoring outlier non primary stream endtime %f\n", end_time_text / (float)AV_TIME_BASE);
1615
1616
5/6
✓ Branch 0 taken 10044 times.
✓ Branch 1 taken 1799 times.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 10039 times.
✓ Branch 4 taken 5 times.
✗ Branch 5 not taken.
11843 if (duration == INT64_MIN || (duration < duration_text && (uint64_t)duration_text - duration < AV_TIME_BASE))
1617 1804 duration = duration_text;
1618
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10039 times.
10039 else if (duration < duration_text)
1619 av_log(ic, AV_LOG_VERBOSE, "Ignoring outlier non primary stream duration %f\n", duration_text / (float)AV_TIME_BASE);
1620
1621
2/2
✓ Branch 0 taken 9615 times.
✓ Branch 1 taken 2228 times.
11843 if (start_time != INT64_MAX) {
1622 9615 ic->start_time = start_time;
1623
2/2
✓ Branch 0 taken 8677 times.
✓ Branch 1 taken 938 times.
9615 if (end_time != INT64_MIN) {
1624
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8677 times.
8677 if (ic->nb_programs > 1) {
1625 for (unsigned i = 0; i < ic->nb_programs; i++) {
1626 AVProgram *const p = ic->programs[i];
1627
1628 if (p->start_time != AV_NOPTS_VALUE &&
1629 p->end_time > p->start_time &&
1630 p->end_time - (uint64_t)p->start_time <= INT64_MAX)
1631 duration = FFMAX(duration, p->end_time - p->start_time);
1632 }
1633
2/4
✓ Branch 0 taken 8677 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8677 times.
✗ Branch 3 not taken.
8677 } else if (end_time >= start_time && end_time - (uint64_t)start_time <= INT64_MAX) {
1634 8677 duration = FFMAX(duration, end_time - start_time);
1635 }
1636 }
1637 }
1638
6/6
✓ Branch 0 taken 10070 times.
✓ Branch 1 taken 1773 times.
✓ Branch 2 taken 10033 times.
✓ Branch 3 taken 37 times.
✓ Branch 4 taken 5405 times.
✓ Branch 5 taken 4628 times.
11843 if (duration != INT64_MIN && duration > 0 && ic->duration == AV_NOPTS_VALUE) {
1639 5405 ic->duration = duration;
1640 }
1641
5/6
✓ Branch 0 taken 6578 times.
✓ Branch 1 taken 5265 times.
✓ Branch 3 taken 6578 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 5601 times.
✓ Branch 6 taken 977 times.
11843 if (ic->pb && (filesize = avio_size(ic->pb)) > 0 && ic->duration > 0) {
1642 /* compute the bitrate */
1643 5601 double bitrate = (double) filesize * 8.0 * AV_TIME_BASE /
1644 5601 (double) ic->duration;
1645
2/4
✓ Branch 0 taken 5601 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5601 times.
✗ Branch 3 not taken.
5601 if (bitrate >= 0 && bitrate <= INT64_MAX)
1646 5601 ic->bit_rate = bitrate;
1647 }
1648 11843 }
1649
1650 5030 static void fill_all_stream_timings(AVFormatContext *ic)
1651 {
1652 5030 update_stream_timings(ic);
1653
2/2
✓ Branch 0 taken 5575 times.
✓ Branch 1 taken 5030 times.
10605 for (unsigned i = 0; i < ic->nb_streams; i++) {
1654 5575 AVStream *const st = ic->streams[i];
1655
1656
2/2
✓ Branch 0 taken 743 times.
✓ Branch 1 taken 4832 times.
5575 if (st->start_time == AV_NOPTS_VALUE) {
1657
2/2
✓ Branch 0 taken 116 times.
✓ Branch 1 taken 627 times.
743 if (ic->start_time != AV_NOPTS_VALUE)
1658 116 st->start_time = av_rescale_q(ic->start_time, AV_TIME_BASE_Q,
1659 st->time_base);
1660
2/2
✓ Branch 0 taken 728 times.
✓ Branch 1 taken 15 times.
743 if (ic->duration != AV_NOPTS_VALUE)
1661 728 st->duration = av_rescale_q(ic->duration, AV_TIME_BASE_Q,
1662 st->time_base);
1663 }
1664 }
1665 5030 }
1666
1667 1783 static void estimate_timings_from_bit_rate(AVFormatContext *ic)
1668 {
1669 1783 FFFormatContext *const si = ffformatcontext(ic);
1670 1783 int show_warning = 0;
1671
1672 /* if bit_rate is already set, we believe it */
1673
2/2
✓ Branch 0 taken 1755 times.
✓ Branch 1 taken 28 times.
1783 if (ic->bit_rate <= 0) {
1674 1755 int64_t bit_rate = 0;
1675
2/2
✓ Branch 0 taken 1807 times.
✓ Branch 1 taken 1195 times.
3002 for (unsigned i = 0; i < ic->nb_streams; i++) {
1676 1807 const AVStream *const st = ic->streams[i];
1677 1807 const FFStream *const sti = cffstream(st);
1678
4/4
✓ Branch 0 taken 1088 times.
✓ Branch 1 taken 719 times.
✓ Branch 2 taken 112 times.
✓ Branch 3 taken 976 times.
1807 if (st->codecpar->bit_rate <= 0 && sti->avctx->bit_rate > 0)
1679 112 st->codecpar->bit_rate = sti->avctx->bit_rate;
1680
2/2
✓ Branch 0 taken 831 times.
✓ Branch 1 taken 976 times.
1807 if (st->codecpar->bit_rate > 0) {
1681
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 831 times.
831 if (INT64_MAX - st->codecpar->bit_rate < bit_rate) {
1682 bit_rate = 0;
1683 break;
1684 }
1685 831 bit_rate += st->codecpar->bit_rate;
1686
4/4
✓ Branch 0 taken 887 times.
✓ Branch 1 taken 89 times.
✓ Branch 2 taken 560 times.
✓ Branch 3 taken 327 times.
976 } else if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && sti->codec_info_nb_frames > 1) {
1687 // If we have a videostream with packets but without a bitrate
1688 // then consider the sum not known
1689 560 bit_rate = 0;
1690 560 break;
1691 }
1692 }
1693 1755 ic->bit_rate = bit_rate;
1694 }
1695
1696 /* if duration is already set, we believe it */
1697
1/2
✓ Branch 0 taken 1783 times.
✗ Branch 1 not taken.
1783 if (ic->duration == AV_NOPTS_VALUE &&
1698
2/2
✓ Branch 0 taken 831 times.
✓ Branch 1 taken 952 times.
1783 ic->bit_rate != 0) {
1699
2/2
✓ Branch 0 taken 808 times.
✓ Branch 1 taken 23 times.
831 int64_t filesize = ic->pb ? avio_size(ic->pb) : 0;
1700
2/2
✓ Branch 0 taken 802 times.
✓ Branch 1 taken 29 times.
831 if (filesize > si->data_offset) {
1701 802 filesize -= si->data_offset;
1702
2/2
✓ Branch 0 taken 840 times.
✓ Branch 1 taken 802 times.
1642 for (unsigned i = 0; i < ic->nb_streams; i++) {
1703 840 AVStream *const st = ic->streams[i];
1704
1705
1/2
✓ Branch 0 taken 840 times.
✗ Branch 1 not taken.
840 if ( st->time_base.num <= INT64_MAX / ic->bit_rate
1706
1/2
✓ Branch 0 taken 840 times.
✗ Branch 1 not taken.
840 && st->duration == AV_NOPTS_VALUE) {
1707 840 st->duration = av_rescale(filesize, 8LL * st->time_base.den,
1708 840 ic->bit_rate *
1709 840 (int64_t) st->time_base.num);
1710 840 show_warning = 1;
1711 }
1712 }
1713 }
1714 }
1715
2/2
✓ Branch 0 taken 802 times.
✓ Branch 1 taken 981 times.
1783 if (show_warning)
1716 802 av_log(ic, AV_LOG_WARNING,
1717 "Estimating duration from bitrate, this may be inaccurate\n");
1718 1783 }
1719
1720 #define DURATION_MAX_READ_SIZE 250000LL
1721 #define DURATION_MAX_RETRY 6
1722
1723 /* only usable for MPEG-PS streams */
1724 61 static void estimate_timings_from_pts(AVFormatContext *ic, int64_t old_offset)
1725 {
1726 61 FFFormatContext *const si = ffformatcontext(ic);
1727 61 AVPacket *const pkt = si->pkt;
1728 int num, den, read_size, ret;
1729 61 int found_duration = 0;
1730 int is_end;
1731 int64_t filesize, offset, duration;
1732 61 int retry = 0;
1733
1734 /* flush packet queue */
1735 61 ff_flush_packet_queue(ic);
1736
1737
2/2
✓ Branch 0 taken 124 times.
✓ Branch 1 taken 61 times.
185 for (unsigned i = 0; i < ic->nb_streams; i++) {
1738 124 AVStream *const st = ic->streams[i];
1739 124 FFStream *const sti = ffstream(st);
1740
1741
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 107 times.
124 if (st->start_time == AV_NOPTS_VALUE &&
1742
1/2
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
17 sti->first_dts == AV_NOPTS_VALUE &&
1743
1/2
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
17 st->codecpar->codec_type != AVMEDIA_TYPE_UNKNOWN)
1744 17 av_log(ic, AV_LOG_WARNING,
1745 "start time for stream %d is not set in estimate_timings_from_pts\n", i);
1746
1747
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 100 times.
124 if (sti->parser) {
1748 24 av_parser_close(sti->parser);
1749 24 sti->parser = NULL;
1750 }
1751 }
1752
1753
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 61 times.
61 if (ic->skip_estimate_duration_from_pts) {
1754 av_log(ic, AV_LOG_INFO, "Skipping duration calculation in estimate_timings_from_pts\n");
1755 goto skip_duration_calc;
1756 }
1757
1758 61 av_opt_set_int(ic, "skip_changes", 1, AV_OPT_SEARCH_CHILDREN);
1759 /* estimate the end time (duration) */
1760 /* XXX: may need to support wrapping */
1761
1/2
✓ Branch 0 taken 61 times.
✗ Branch 1 not taken.
61 filesize = ic->pb ? avio_size(ic->pb) : 0;
1762 do {
1763 67 is_end = found_duration;
1764 67 offset = filesize - (DURATION_MAX_READ_SIZE << retry);
1765
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 44 times.
67 if (offset < 0)
1766 23 offset = 0;
1767
1768 67 avio_seek(ic->pb, offset, SEEK_SET);
1769 67 read_size = 0;
1770 5465 for (;;) {
1771 AVStream *st;
1772 FFStream *sti;
1773
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 5526 times.
5532 if (read_size >= DURATION_MAX_READ_SIZE << (FFMAX(retry - 1, 0)))
1774 6 break;
1775
1776 do {
1777 5526 ret = ff_read_packet(ic, pkt);
1778
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5526 times.
5526 } while (ret == AVERROR(EAGAIN));
1779
2/2
✓ Branch 0 taken 61 times.
✓ Branch 1 taken 5465 times.
5526 if (ret != 0)
1780 61 break;
1781 5465 read_size += pkt->size;
1782 5465 st = ic->streams[pkt->stream_index];
1783 5465 sti = ffstream(st);
1784
2/2
✓ Branch 0 taken 2677 times.
✓ Branch 1 taken 2788 times.
5465 if (pkt->pts != AV_NOPTS_VALUE &&
1785
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2677 times.
2677 (st->start_time != AV_NOPTS_VALUE ||
1786 sti->first_dts != AV_NOPTS_VALUE)) {
1787
1/2
✓ Branch 0 taken 2677 times.
✗ Branch 1 not taken.
2677 if (pkt->duration == 0) {
1788 2677 compute_frame_duration(ic, &num, &den, st, sti->parser, pkt);
1789
3/4
✓ Branch 0 taken 2189 times.
✓ Branch 1 taken 488 times.
✓ Branch 2 taken 2189 times.
✗ Branch 3 not taken.
2677 if (den && num) {
1790 2189 pkt->duration = av_rescale_rnd(1,
1791 2189 num * (int64_t) st->time_base.den,
1792 2189 den * (int64_t) st->time_base.num,
1793 AV_ROUND_DOWN);
1794 }
1795 }
1796 2677 duration = pkt->pts + pkt->duration;
1797 2677 found_duration = 1;
1798
1/2
✓ Branch 0 taken 2677 times.
✗ Branch 1 not taken.
2677 if (st->start_time != AV_NOPTS_VALUE)
1799 2677 duration -= st->start_time;
1800 else
1801 duration -= sti->first_dts;
1802
2/2
✓ Branch 0 taken 2670 times.
✓ Branch 1 taken 7 times.
2677 if (duration > 0) {
1803
3/4
✓ Branch 0 taken 2571 times.
✓ Branch 1 taken 99 times.
✓ Branch 2 taken 2571 times.
✗ Branch 3 not taken.
2670 if (st->duration == AV_NOPTS_VALUE || sti->info->last_duration<= 0 ||
1804
3/4
✓ Branch 0 taken 2333 times.
✓ Branch 1 taken 238 times.
✓ Branch 2 taken 2333 times.
✗ Branch 3 not taken.
2571 (st->duration < duration && FFABS(duration - sti->info->last_duration) < 60LL*st->time_base.den / st->time_base.num))
1805 2432 st->duration = duration;
1806 2670 sti->info->last_duration = duration;
1807 }
1808 }
1809 5465 av_packet_unref(pkt);
1810 }
1811
1812 /* check if all audio/video streams have valid duration */
1813
2/2
✓ Branch 0 taken 61 times.
✓ Branch 1 taken 6 times.
67 if (!is_end) {
1814 61 is_end = 1;
1815
2/2
✓ Branch 0 taken 124 times.
✓ Branch 1 taken 61 times.
185 for (unsigned i = 0; i < ic->nb_streams; i++) {
1816 124 const AVStream *const st = ic->streams[i];
1817
2/2
✓ Branch 0 taken 110 times.
✓ Branch 1 taken 14 times.
124 switch (st->codecpar->codec_type) {
1818 110 case AVMEDIA_TYPE_VIDEO:
1819 case AVMEDIA_TYPE_AUDIO:
1820
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 91 times.
110 if (st->duration == AV_NOPTS_VALUE)
1821 19 is_end = 0;
1822 }
1823 }
1824 }
1825
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2 times.
8 } while (!is_end &&
1826
3/4
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 59 times.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
73 offset &&
1827 ++retry <= DURATION_MAX_RETRY);
1828
1829 61 av_opt_set_int(ic, "skip_changes", 0, AV_OPT_SEARCH_CHILDREN);
1830
1831 /* warn about audio/video streams which duration could not be estimated */
1832
2/2
✓ Branch 0 taken 124 times.
✓ Branch 1 taken 61 times.
185 for (unsigned i = 0; i < ic->nb_streams; i++) {
1833 124 const AVStream *const st = ic->streams[i];
1834 124 const FFStream *const sti = cffstream(st);
1835
1836
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 99 times.
124 if (st->duration == AV_NOPTS_VALUE) {
1837
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 12 times.
25 switch (st->codecpar->codec_type) {
1838 13 case AVMEDIA_TYPE_VIDEO:
1839 case AVMEDIA_TYPE_AUDIO:
1840
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) {
1841 5 av_log(ic, AV_LOG_WARNING, "stream %d : no PTS found at end of file, duration not set\n", i);
1842 } else
1843 8 av_log(ic, AV_LOG_WARNING, "stream %d : no TS found at start of file, duration not set\n", i);
1844 }
1845 }
1846 }
1847 61 skip_duration_calc:
1848 61 fill_all_stream_timings(ic);
1849
1850 61 avio_seek(ic->pb, old_offset, SEEK_SET);
1851
2/2
✓ Branch 0 taken 124 times.
✓ Branch 1 taken 61 times.
185 for (unsigned i = 0; i < ic->nb_streams; i++) {
1852 124 AVStream *const st = ic->streams[i];
1853 124 FFStream *const sti = ffstream(st);
1854
1855 124 sti->cur_dts = sti->first_dts;
1856 124 sti->last_IP_pts = AV_NOPTS_VALUE;
1857 124 sti->last_dts_for_order_check = AV_NOPTS_VALUE;
1858
2/2
✓ Branch 0 taken 2108 times.
✓ Branch 1 taken 124 times.
2232 for (int j = 0; j < MAX_REORDER_DELAY + 1; j++)
1859 2108 sti->pts_buffer[j] = AV_NOPTS_VALUE;
1860 }
1861 61 }
1862
1863 /* 1:1 map to AVDurationEstimationMethod */
1864 static const char *const duration_name[] = {
1865 [AVFMT_DURATION_FROM_PTS] = "pts",
1866 [AVFMT_DURATION_FROM_STREAM] = "stream",
1867 [AVFMT_DURATION_FROM_BITRATE] = "bit rate",
1868 };
1869
1870 6813 static const char *duration_estimate_name(enum AVDurationEstimationMethod method)
1871 {
1872 6813 return duration_name[method];
1873 }
1874
1875 6813 static void estimate_timings(AVFormatContext *ic, int64_t old_offset)
1876 {
1877 int64_t file_size;
1878
1879 /* get the file size, if possible */
1880
2/2
✓ Branch 0 taken 2671 times.
✓ Branch 1 taken 4142 times.
6813 if (ic->iformat->flags & AVFMT_NOFILE) {
1881 2671 file_size = 0;
1882 } else {
1883 4142 file_size = avio_size(ic->pb);
1884 4142 file_size = FFMAX(0, file_size);
1885 }
1886
1887
2/2
✓ Branch 0 taken 6788 times.
✓ Branch 1 taken 25 times.
6813 if ((!strcmp(ic->iformat->name, "mpeg") ||
1888
3/4
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 6752 times.
✓ Branch 2 taken 61 times.
✗ Branch 3 not taken.
6813 !strcmp(ic->iformat->name, "mpegts")) &&
1889
1/2
✓ Branch 0 taken 61 times.
✗ Branch 1 not taken.
61 file_size && (ic->pb->seekable & AVIO_SEEKABLE_NORMAL)) {
1890 /* get accurate estimate from the PTSes */
1891 61 estimate_timings_from_pts(ic, old_offset);
1892 61 ic->duration_estimation_method = AVFMT_DURATION_FROM_PTS;
1893
2/2
✓ Branch 1 taken 4969 times.
✓ Branch 2 taken 1783 times.
6752 } else if (has_duration(ic)) {
1894 /* at least one component has timings - we use them for all
1895 * the components */
1896 4969 fill_all_stream_timings(ic);
1897 /* nut demuxer estimate the duration from PTS */
1898
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 4940 times.
4969 if (!strcmp(ic->iformat->name, "nut"))
1899 29 ic->duration_estimation_method = AVFMT_DURATION_FROM_PTS;
1900 else
1901 4940 ic->duration_estimation_method = AVFMT_DURATION_FROM_STREAM;
1902 } else {
1903 /* less precise: use bitrate info */
1904 1783 estimate_timings_from_bit_rate(ic);
1905 1783 ic->duration_estimation_method = AVFMT_DURATION_FROM_BITRATE;
1906 }
1907 6813 update_stream_timings(ic);
1908
1909
2/2
✓ Branch 0 taken 7431 times.
✓ Branch 1 taken 6813 times.
14244 for (unsigned i = 0; i < ic->nb_streams; i++) {
1910 7431 AVStream *const st = ic->streams[i];
1911
1/2
✓ Branch 0 taken 7431 times.
✗ Branch 1 not taken.
7431 if (st->time_base.den)
1912 7431 av_log(ic, AV_LOG_TRACE, "stream %u: start_time: %s duration: %s\n", i,
1913 7431 av_ts2timestr(st->start_time, &st->time_base),
1914 7431 av_ts2timestr(st->duration, &st->time_base));
1915 }
1916 6813 av_log(ic, AV_LOG_TRACE,
1917 "format: start_time: %s duration: %s (estimate from %s) bitrate=%"PRId64" kb/s\n",
1918 6813 av_ts2timestr(ic->start_time, &AV_TIME_BASE_Q),
1919 6813 av_ts2timestr(ic->duration, &AV_TIME_BASE_Q),
1920 duration_estimate_name(ic->duration_estimation_method),
1921 6813 (int64_t)ic->bit_rate / 1000);
1922 6813 }
1923
1924 95397 static int determinable_frame_size(const AVCodecContext *avctx)
1925 {
1926
2/2
✓ Branch 0 taken 248 times.
✓ Branch 1 taken 95149 times.
95397 switch(avctx->codec_id) {
1927 248 case AV_CODEC_ID_MP1:
1928 case AV_CODEC_ID_MP2:
1929 case AV_CODEC_ID_MP3:
1930 case AV_CODEC_ID_CODEC2:
1931 248 return 1;
1932 }
1933
1934 95149 return 0;
1935 }
1936
1937 394270 static int has_codec_parameters(const AVStream *st, const char **errmsg_ptr)
1938 {
1939 394270 const FFStream *const sti = cffstream(st);
1940 394270 const AVCodecContext *const avctx = sti->avctx;
1941
1942 #define FAIL(errmsg) do { \
1943 if (errmsg_ptr) \
1944 *errmsg_ptr = errmsg; \
1945 return 0; \
1946 } while (0)
1947
1948
2/2
✓ Branch 0 taken 605 times.
✓ Branch 1 taken 393665 times.
394270 if ( avctx->codec_id == AV_CODEC_ID_NONE
1949
2/2
✓ Branch 0 taken 383 times.
✓ Branch 1 taken 222 times.
605 && avctx->codec_type != AVMEDIA_TYPE_DATA)
1950
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 381 times.
383 FAIL("unknown codec");
1951
4/5
✓ Branch 0 taken 108766 times.
✓ Branch 1 taken 283624 times.
✓ Branch 2 taken 599 times.
✓ Branch 3 taken 898 times.
✗ Branch 4 not taken.
393887 switch (avctx->codec_type) {
1952 108766 case AVMEDIA_TYPE_AUDIO:
1953
4/4
✓ Branch 0 taken 95397 times.
✓ Branch 1 taken 13369 times.
✓ Branch 3 taken 248 times.
✓ Branch 4 taken 95149 times.
108766 if (!avctx->frame_size && determinable_frame_size(avctx))
1954
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 246 times.
248 FAIL("unspecified frame size");
1955
2/2
✓ Branch 0 taken 107405 times.
✓ Branch 1 taken 1113 times.
108518 if (sti->info->found_decoder >= 0 &&
1956
2/2
✓ Branch 0 taken 2242 times.
✓ Branch 1 taken 105163 times.
107405 avctx->sample_fmt == AV_SAMPLE_FMT_NONE)
1957
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2242 times.
2242 FAIL("unspecified sample format");
1958
2/2
✓ Branch 0 taken 120 times.
✓ Branch 1 taken 106156 times.
106276 if (!avctx->sample_rate)
1959
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 116 times.
120 FAIL("unspecified sample rate");
1960
2/2
✓ Branch 0 taken 1126 times.
✓ Branch 1 taken 105030 times.
106156 if (!avctx->ch_layout.nb_channels)
1961
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1123 times.
1126 FAIL("unspecified number of channels");
1962
4/6
✓ Branch 0 taken 105030 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 70443 times.
✓ Branch 3 taken 34587 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 70443 times.
105030 if (sti->info->found_decoder >= 0 && !sti->nb_decoded_frames && avctx->codec_id == AV_CODEC_ID_DTS)
1963 FAIL("no decodable DTS frames");
1964 105030 break;
1965 283624 case AVMEDIA_TYPE_VIDEO:
1966
2/2
✓ Branch 0 taken 11559 times.
✓ Branch 1 taken 272065 times.
283624 if (!avctx->width)
1967
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 11551 times.
11559 FAIL("unspecified size");
1968
4/4
✓ Branch 0 taken 272061 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 4175 times.
✓ Branch 3 taken 267886 times.
272065 if (sti->info->found_decoder >= 0 && avctx->pix_fmt == AV_PIX_FMT_NONE)
1969
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 4151 times.
4175 FAIL("unspecified pixel format");
1970
4/4
✓ Branch 0 taken 267790 times.
✓ Branch 1 taken 100 times.
✓ Branch 2 taken 86 times.
✓ Branch 3 taken 267704 times.
267890 if (st->codecpar->codec_id == AV_CODEC_ID_RV30 || st->codecpar->codec_id == AV_CODEC_ID_RV40)
1971
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)
1972
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 67 times.
67 FAIL("no frame in rv30/40 and no sar");
1973 267823 break;
1974 599 case AVMEDIA_TYPE_SUBTITLE:
1975
4/4
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 564 times.
✓ Branch 2 taken 21 times.
✓ Branch 3 taken 14 times.
599 if (avctx->codec_id == AV_CODEC_ID_HDMV_PGS_SUBTITLE && !avctx->width)
1976
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 FAIL("unspecified size");
1977 578 break;
1978 898 case AVMEDIA_TYPE_DATA:
1979
2/2
✓ Branch 0 taken 222 times.
✓ Branch 1 taken 676 times.
898 if (avctx->codec_id == AV_CODEC_ID_NONE) return 1;
1980 }
1981
1982 374107 return 1;
1983 }
1984
1985 /* returns 1 or 0 if or if not decoded data was returned, or a negative error */
1986 183554 static int try_decode_frame(AVFormatContext *s, AVStream *st,
1987 const AVPacket *avpkt, AVDictionary **options)
1988 {
1989 183554 FFStream *const sti = ffstream(st);
1990 183554 AVCodecContext *const avctx = sti->avctx;
1991 const AVCodec *codec;
1992 183554 int got_picture = 1, ret = 0;
1993 183554 AVFrame *frame = av_frame_alloc();
1994 AVSubtitle subtitle;
1995 183554 AVPacket pkt = *avpkt;
1996 183554 int do_skip_frame = 0;
1997 enum AVDiscard skip_frame;
1998
1999
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 183554 times.
183554 if (!frame)
2000 return AVERROR(ENOMEM);
2001
2002
2/2
✓ Branch 1 taken 2073 times.
✓ Branch 2 taken 181481 times.
183554 if (!avcodec_is_open(avctx) &&
2003
1/2
✓ Branch 0 taken 2073 times.
✗ Branch 1 not taken.
2073 sti->info->found_decoder <= 0 &&
2004
4/4
✓ Branch 0 taken 815 times.
✓ Branch 1 taken 1258 times.
✓ Branch 2 taken 63 times.
✓ Branch 3 taken 752 times.
2073 (st->codecpar->codec_id != -sti->info->found_decoder || !st->codecpar->codec_id)) {
2005 1321 AVDictionary *thread_opt = NULL;
2006
2007 1321 codec = find_probe_decoder(s, st, st->codecpar->codec_id);
2008
2009
2/2
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 1255 times.
1321 if (!codec) {
2010 66 sti->info->found_decoder = -st->codecpar->codec_id;
2011 66 ret = -1;
2012 71 goto fail;
2013 }
2014
2015 /* Force thread count to 1 since the H.264 decoder will not extract
2016 * SPS and PPS to extradata during multi-threaded decoding. */
2017
2/2
✓ Branch 0 taken 125 times.
✓ Branch 1 taken 1130 times.
1255 av_dict_set(options ? options : &thread_opt, "threads", "1", 0);
2018 /* Force lowres to 0. The decoder might reduce the video size by the
2019 * lowres factor, and we don't want that propagated to the stream's
2020 * codecpar */
2021
2/2
✓ Branch 0 taken 125 times.
✓ Branch 1 taken 1130 times.
1255 av_dict_set(options ? options : &thread_opt, "lowres", "0", 0);
2022
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1255 times.
1255 if (s->codec_whitelist)
2023 av_dict_set(options ? options : &thread_opt, "codec_whitelist", s->codec_whitelist, 0);
2024
2/2
✓ Branch 0 taken 125 times.
✓ Branch 1 taken 1130 times.
1255 ret = avcodec_open2(avctx, codec, options ? options : &thread_opt);
2025
2/2
✓ Branch 0 taken 125 times.
✓ Branch 1 taken 1130 times.
1255 if (!options)
2026 125 av_dict_free(&thread_opt);
2027
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1250 times.
1255 if (ret < 0) {
2028 5 sti->info->found_decoder = -avctx->codec_id;
2029 5 goto fail;
2030 }
2031 1250 sti->info->found_decoder = 1;
2032
2/2
✓ Branch 0 taken 6005 times.
✓ Branch 1 taken 176228 times.
182233 } else if (!sti->info->found_decoder)
2033 6005 sti->info->found_decoder = 1;
2034
2035
2/2
✓ Branch 0 taken 752 times.
✓ Branch 1 taken 182731 times.
183483 if (sti->info->found_decoder < 0) {
2036 752 ret = -1;
2037 752 goto fail;
2038 }
2039
2040
2/2
✓ Branch 1 taken 94122 times.
✓ Branch 2 taken 88609 times.
182731 if (avpriv_codec_get_cap_skip_frame_fill_param(avctx->codec)) {
2041 94122 do_skip_frame = 1;
2042 94122 skip_frame = avctx->skip_frame;
2043 94122 avctx->skip_frame = AVDISCARD_ALL;
2044 }
2045
2046
6/6
✓ Branch 0 taken 7232 times.
✓ Branch 1 taken 4343 times.
✓ Branch 2 taken 44 times.
✓ Branch 3 taken 4299 times.
✓ Branch 4 taken 67 times.
✓ Branch 5 taken 182737 times.
372811 while ((pkt.size > 0 || (!pkt.data && got_picture)) &&
2047
4/4
✓ Branch 0 taken 11575 times.
✓ Branch 1 taken 178505 times.
✓ Branch 2 taken 5225 times.
✓ Branch 3 taken 177512 times.
372817 ret >= 0 &&
2048
2/2
✓ Branch 2 taken 2293 times.
✓ Branch 3 taken 175219 times.
360249 (!has_codec_parameters(st, NULL) || !has_decode_delay_been_guessed(st) ||
2049
2/2
✓ Branch 0 taken 172644 times.
✓ Branch 1 taken 2575 times.
175219 (!sti->codec_info_nb_frames &&
2050
2/2
✓ Branch 0 taken 375 times.
✓ Branch 1 taken 2200 times.
2575 (avctx->codec->capabilities & AV_CODEC_CAP_CHANNEL_CONF)))) {
2051 7893 got_picture = 0;
2052
2/2
✓ Branch 0 taken 646 times.
✓ Branch 1 taken 7247 times.
7893 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO ||
2053
2/2
✓ Branch 0 taken 639 times.
✓ Branch 1 taken 7 times.
646 avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
2054 7886 ret = avcodec_send_packet(avctx, &pkt);
2055
6/6
✓ Branch 0 taken 618 times.
✓ Branch 1 taken 7268 times.
✓ Branch 2 taken 551 times.
✓ Branch 3 taken 67 times.
✓ Branch 4 taken 544 times.
✓ Branch 5 taken 7 times.
7886 if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
2056 544 break;
2057
2/2
✓ Branch 0 taken 7268 times.
✓ Branch 1 taken 74 times.
7342 if (ret >= 0)
2058 7268 pkt.size = 0;
2059 7342 ret = avcodec_receive_frame(avctx, frame);
2060
2/2
✓ Branch 0 taken 3063 times.
✓ Branch 1 taken 4279 times.
7342 if (ret >= 0)
2061 3063 got_picture = 1;
2062
4/4
✓ Branch 0 taken 3174 times.
✓ Branch 1 taken 4168 times.
✓ Branch 2 taken 44 times.
✓ Branch 3 taken 3130 times.
7342 if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
2063 4212 ret = 0;
2064
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 } else if (avctx->codec_type == AVMEDIA_TYPE_SUBTITLE) {
2065 7 ret = avcodec_decode_subtitle2(avctx, &subtitle,
2066 &got_picture, &pkt);
2067
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 5 times.
7 if (got_picture)
2068 2 avsubtitle_free(&subtitle);
2069
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 if (ret >= 0)
2070 7 pkt.size = 0;
2071 }
2072
2/2
✓ Branch 0 taken 7282 times.
✓ Branch 1 taken 67 times.
7349 if (ret >= 0) {
2073
2/2
✓ Branch 0 taken 3065 times.
✓ Branch 1 taken 4217 times.
7282 if (got_picture)
2074 3065 sti->nb_decoded_frames++;
2075 7282 ret = got_picture;
2076 }
2077 }
2078
2079 182187 fail:
2080
2/2
✓ Branch 0 taken 94122 times.
✓ Branch 1 taken 89432 times.
183554 if (do_skip_frame) {
2081 94122 avctx->skip_frame = skip_frame;
2082 }
2083
2084 183554 av_frame_free(&frame);
2085 183554 return ret;
2086 }
2087
2088 54 static int chapter_start_cmp(const void *p1, const void *p2)
2089 {
2090 54 const AVChapter *const ch1 = *(AVChapter**)p1;
2091 54 const AVChapter *const ch2 = *(AVChapter**)p2;
2092 54 int delta = av_compare_ts(ch1->start, ch1->time_base, ch2->start, ch2->time_base);
2093
1/2
✓ Branch 0 taken 54 times.
✗ Branch 1 not taken.
54 if (delta)
2094 54 return delta;
2095 return FFDIFFSIGN(ch1->id, ch2->id);
2096 }
2097
2098 6813 static int compute_chapters_end(AVFormatContext *s)
2099 {
2100 6813 int64_t max_time = 0;
2101 AVChapter **timetable;
2102
2103
2/2
✓ Branch 0 taken 6790 times.
✓ Branch 1 taken 23 times.
6813 if (!s->nb_chapters)
2104 6790 return 0;
2105
2106
2/4
✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 23 times.
✗ Branch 3 not taken.
23 if (s->duration > 0 && s->start_time < INT64_MAX - s->duration)
2107 23 max_time = s->duration +
2108
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 3 times.
23 ((s->start_time == AV_NOPTS_VALUE) ? 0 : s->start_time);
2109
2110 23 timetable = av_memdup(s->chapters, s->nb_chapters * sizeof(*timetable));
2111
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 23 times.
23 if (!timetable)
2112 return AVERROR(ENOMEM);
2113 23 qsort(timetable, s->nb_chapters, sizeof(*timetable), chapter_start_cmp);
2114
2115
2/2
✓ Branch 0 taken 65 times.
✓ Branch 1 taken 23 times.
88 for (unsigned i = 0; i < s->nb_chapters; i++)
2116
2/2
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 28 times.
65 if (timetable[i]->end == AV_NOPTS_VALUE) {
2117 37 AVChapter *const ch = timetable[i];
2118 37 int64_t end = max_time ? av_rescale_q(max_time, AV_TIME_BASE_Q,
2119 ch->time_base)
2120
1/2
✓ Branch 0 taken 37 times.
✗ Branch 1 not taken.
37 : INT64_MAX;
2121
2122
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 16 times.
37 if (i + 1 < s->nb_chapters) {
2123 21 const AVChapter *const ch1 = timetable[i + 1];
2124 21 int64_t next_start = av_rescale_q(ch1->start, ch1->time_base,
2125 ch->time_base);
2126
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)
2127 21 end = next_start;
2128 }
2129
2/4
✓ Branch 0 taken 37 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 37 times.
37 ch->end = (end == INT64_MAX || end < ch->start) ? ch->start : end;
2130 }
2131 23 av_free(timetable);
2132 23 return 0;
2133 }
2134
2135 16594400 static int get_std_framerate(int i)
2136 {
2137
2/2
✓ Branch 0 taken 15071950 times.
✓ Branch 1 taken 1522450 times.
16594400 if (i < 30*12)
2138 15071950 return (i + 1) * 1001;
2139 1522450 i -= 30*12;
2140
2141
2/2
✓ Branch 0 taken 1156114 times.
✓ Branch 1 taken 366336 times.
1522450 if (i < 30)
2142 1156114 return (i + 31) * 1001 * 12;
2143 366336 i -= 30;
2144
2145
2/2
✓ Branch 0 taken 117238 times.
✓ Branch 1 taken 249098 times.
366336 if (i < 3)
2146 117238 return ((const int[]) { 80, 120, 240})[i] * 1001 * 12;
2147
2148 249098 i -= 3;
2149
2150 249098 return ((const int[]) { 24, 30, 60, 12, 15, 48 })[i] * 1000 * 12;
2151 }
2152
2153 /* Is the time base unreliable?
2154 * This is a heuristic to balance between quick acceptance of the values in
2155 * the headers vs. some extra checks.
2156 * Old DivX and Xvid often have nonsense timebases like 1fps or 2fps.
2157 * MPEG-2 commonly misuses field repeat flags to store different framerates.
2158 * And there are "variable" fps files this needs to detect as well. */
2159 193411 static int tb_unreliable(AVCodecContext *c)
2160 {
2161
2/2
✓ Branch 0 taken 143113 times.
✓ Branch 1 taken 50298 times.
193411 if (c->time_base.den >= 101LL * c->time_base.num ||
2162
2/2
✓ Branch 0 taken 131393 times.
✓ Branch 1 taken 11720 times.
143113 c->time_base.den < 5LL * c->time_base.num ||
2163 // c->codec_tag == AV_RL32("DIVX") ||
2164 // c->codec_tag == AV_RL32("XVID") ||
2165
2/2
✓ Branch 0 taken 131319 times.
✓ Branch 1 taken 74 times.
131393 c->codec_tag == AV_RL32("mp4v") ||
2166
2/2
✓ Branch 0 taken 122832 times.
✓ Branch 1 taken 8487 times.
131319 c->codec_id == AV_CODEC_ID_MPEG2VIDEO ||
2167
2/2
✓ Branch 0 taken 122302 times.
✓ Branch 1 taken 530 times.
122832 c->codec_id == AV_CODEC_ID_GIF ||
2168
2/2
✓ Branch 0 taken 113918 times.
✓ Branch 1 taken 8384 times.
122302 c->codec_id == AV_CODEC_ID_HEVC ||
2169
2/2
✓ Branch 0 taken 16189 times.
✓ Branch 1 taken 97729 times.
113918 c->codec_id == AV_CODEC_ID_H264)
2170 95682 return 1;
2171 97729 return 0;
2172 }
2173
2174 133454 int ff_rfps_add_frame(AVFormatContext *ic, AVStream *st, int64_t ts)
2175 {
2176 133454 FFStream *const sti = ffstream(st);
2177 133454 FFStreamInfo *info = sti->info;
2178 133454 int64_t last = info->last_dts;
2179
2180
6/6
✓ Branch 0 taken 110164 times.
✓ Branch 1 taken 23290 times.
✓ Branch 2 taken 104872 times.
✓ Branch 3 taken 5292 times.
✓ Branch 4 taken 104844 times.
✓ Branch 5 taken 28 times.
133454 if ( ts != AV_NOPTS_VALUE && last != AV_NOPTS_VALUE && ts > last
2181
1/2
✓ Branch 0 taken 104844 times.
✗ Branch 1 not taken.
104844 && ts - (uint64_t)last < INT64_MAX) {
2182
2/2
✓ Branch 1 taken 6051 times.
✓ Branch 2 taken 98793 times.
104844 double dts = (is_relative(ts) ? ts - RELATIVE_TS_BASE : ts) * av_q2d(st->time_base);
2183 104844 int64_t duration = ts - last;
2184
2185
2/2
✓ Branch 0 taken 3431 times.
✓ Branch 1 taken 101413 times.
104844 if (!info->duration_error)
2186 3431 info->duration_error = av_mallocz(sizeof(info->duration_error[0])*2);
2187
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 104844 times.
104844 if (!info->duration_error)
2188 return AVERROR(ENOMEM);
2189
2190 // if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
2191 // av_log(NULL, AV_LOG_ERROR, "%f\n", dts);
2192
2/2
✓ Branch 0 taken 41832756 times.
✓ Branch 1 taken 104844 times.
41937600 for (int i = 0; i < MAX_STD_TIMEBASES; i++) {
2193
2/2
✓ Branch 0 taken 16109381 times.
✓ Branch 1 taken 25723375 times.
41832756 if (info->duration_error[0][1][i] < 1e10) {
2194 16109381 int framerate = get_std_framerate(i);
2195 16109381 double sdts = dts*framerate/(1001*12);
2196
2/2
✓ Branch 0 taken 32218762 times.
✓ Branch 1 taken 16109381 times.
48328143 for (int j = 0; j < 2; j++) {
2197 32218762 int64_t ticks = llrint(sdts+j*0.5);
2198 32218762 double error = sdts - ticks + j*0.5;
2199 32218762 info->duration_error[j][0][i] += error;
2200 32218762 info->duration_error[j][1][i] += error*error;
2201 }
2202 }
2203 }
2204
1/2
✓ Branch 0 taken 104844 times.
✗ Branch 1 not taken.
104844 if (info->rfps_duration_sum <= INT64_MAX - duration) {
2205 104844 info->duration_count++;
2206 104844 info->rfps_duration_sum += duration;
2207 }
2208
2209
2/2
✓ Branch 0 taken 9534 times.
✓ Branch 1 taken 95310 times.
104844 if (info->duration_count % 10 == 0) {
2210 9534 int n = info->duration_count;
2211
2/2
✓ Branch 0 taken 3804066 times.
✓ Branch 1 taken 9534 times.
3813600 for (int i = 0; i < MAX_STD_TIMEBASES; i++) {
2212
2/2
✓ Branch 0 taken 1540411 times.
✓ Branch 1 taken 2263655 times.
3804066 if (info->duration_error[0][1][i] < 1e10) {
2213 1540411 double a0 = info->duration_error[0][0][i] / n;
2214 1540411 double error0 = info->duration_error[0][1][i] / n - a0*a0;
2215 1540411 double a1 = info->duration_error[1][0][i] / n;
2216 1540411 double error1 = info->duration_error[1][1][i] / n - a1*a1;
2217
4/4
✓ Branch 0 taken 1261501 times.
✓ Branch 1 taken 278910 times.
✓ Branch 2 taken 1177502 times.
✓ Branch 3 taken 83999 times.
1540411 if (error0 > 0.04 && error1 > 0.04) {
2218 1177502 info->duration_error[0][1][i] = 2e10;
2219 1177502 info->duration_error[1][1][i] = 2e10;
2220 }
2221 }
2222 }
2223 }
2224
2225 // ignore the first 4 values, they might have some random jitter
2226
3/4
✓ Branch 0 taken 94641 times.
✓ Branch 1 taken 10203 times.
✓ Branch 4 taken 94641 times.
✗ Branch 5 not taken.
104844 if (info->duration_count > 3 && is_relative(ts) == is_relative(last))
2227 94641 info->duration_gcd = av_gcd(info->duration_gcd, duration);
2228 }
2229
2/2
✓ Branch 0 taken 110164 times.
✓ Branch 1 taken 23290 times.
133454 if (ts != AV_NOPTS_VALUE)
2230 110164 info->last_dts = ts;
2231
2232 133454 return 0;
2233 }
2234
2235 7244 void ff_rfps_calculate(AVFormatContext *ic)
2236 {
2237
2/2
✓ Branch 0 taken 7973 times.
✓ Branch 1 taken 7244 times.
15217 for (unsigned i = 0; i < ic->nb_streams; i++) {
2238 7973 AVStream *const st = ic->streams[i];
2239 7973 FFStream *const sti = ffstream(st);
2240
2241
2/2
✓ Branch 0 taken 2177 times.
✓ Branch 1 taken 5796 times.
7973 if (st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO)
2242 2177 continue;
2243 // the check for tb_unreliable() is not completely correct, since this is not about handling
2244 // an unreliable/inexact time base, but a time base that is finer than necessary, as e.g.
2245 // ipmovie.c produces.
2246
8/8
✓ Branch 1 taken 1634 times.
✓ Branch 2 taken 4162 times.
✓ Branch 3 taken 411 times.
✓ Branch 4 taken 1223 times.
✓ Branch 5 taken 258 times.
✓ Branch 6 taken 153 times.
✓ Branch 7 taken 131 times.
✓ Branch 8 taken 127 times.
5796 if (tb_unreliable(sti->avctx) && 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 &&
2247
1/2
✓ Branch 0 taken 131 times.
✗ Branch 1 not taken.
131 sti->info->duration_gcd < INT64_MAX / st->time_base.num)
2248 131 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);
2249
4/4
✓ Branch 0 taken 3392 times.
✓ Branch 1 taken 2404 times.
✓ Branch 2 taken 382 times.
✓ Branch 3 taken 3010 times.
5796 if (sti->info->duration_count > 1 && !st->r_frame_rate.num
2250
2/2
✓ Branch 1 taken 213 times.
✓ Branch 2 taken 169 times.
382 && tb_unreliable(sti->avctx)) {
2251 213 int num = 0;
2252 213 double best_error = 0.01;
2253
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 213 times.
213 AVRational ref_rate = st->r_frame_rate.num ? st->r_frame_rate : av_inv_q(st->time_base);
2254
2255
2/2
✓ Branch 0 taken 84987 times.
✓ Branch 1 taken 213 times.
85200 for (int j = 0; j < MAX_STD_TIMEBASES; j++) {
2256
2/2
✓ Branch 0 taken 57456 times.
✓ Branch 1 taken 27531 times.
84987 if (sti->info->codec_info_duration &&
2257
2/2
✓ Branch 2 taken 4032 times.
✓ Branch 3 taken 53424 times.
57456 sti->info->codec_info_duration*av_q2d(st->time_base) < (1001*11.5)/get_std_framerate(j))
2258 4032 continue;
2259
4/4
✓ Branch 0 taken 27531 times.
✓ Branch 1 taken 53424 times.
✓ Branch 3 taken 759 times.
✓ Branch 4 taken 26772 times.
80955 if (!sti->info->codec_info_duration && get_std_framerate(j) < 1001*12)
2260 759 continue;
2261
2262
2/2
✓ Branch 2 taken 39123 times.
✓ Branch 3 taken 41073 times.
80196 if (av_q2d(st->time_base) * sti->info->rfps_duration_sum / sti->info->duration_count < (1001*12.0 * 0.8)/get_std_framerate(j))
2263 39123 continue;
2264
2265
2/2
✓ Branch 0 taken 82146 times.
✓ Branch 1 taken 41073 times.
123219 for (int k = 0; k < 2; k++) {
2266 82146 int n = sti->info->duration_count;
2267 82146 double a = sti->info->duration_error[k][0][j] / n;
2268 82146 double error = sti->info->duration_error[k][1][j]/n - a*a;
2269
2270
4/4
✓ Branch 0 taken 2373 times.
✓ Branch 1 taken 79773 times.
✓ Branch 2 taken 2316 times.
✓ Branch 3 taken 57 times.
82146 if (error < best_error && best_error> 0.000000001) {
2271 2316 best_error= error;
2272 2316 num = get_std_framerate(j);
2273 }
2274
2/2
✓ Branch 0 taken 11886 times.
✓ Branch 1 taken 70260 times.
82146 if (error < 0.02)
2275 11886 av_log(ic, AV_LOG_DEBUG, "rfps: %f %f\n", get_std_framerate(j) / 12.0/1001, error);
2276 }
2277 }
2278 // do not increase frame rate by more than 1 % in order to match a standard rate.
2279
5/6
✓ Branch 0 taken 207 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 207 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 205 times.
✓ Branch 6 taken 2 times.
213 if (num && (!ref_rate.num || (double)num/(12*1001) < 1.01 * av_q2d(ref_rate)))
2280 205 av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den, num, 12*1001, INT_MAX);
2281 }
2282
2/2
✓ Branch 0 taken 1548 times.
✓ Branch 1 taken 4248 times.
5796 if ( !st->avg_frame_rate.num
2283
4/4
✓ Branch 0 taken 310 times.
✓ Branch 1 taken 1238 times.
✓ Branch 2 taken 306 times.
✓ Branch 3 taken 4 times.
1548 && st->r_frame_rate.num && sti->info->rfps_duration_sum
2284
2/2
✓ Branch 0 taken 63 times.
✓ Branch 1 taken 243 times.
306 && sti->info->codec_info_duration <= 0
2285
2/2
✓ Branch 0 taken 61 times.
✓ Branch 1 taken 2 times.
63 && sti->info->duration_count > 2
2286
2/2
✓ Branch 2 taken 45 times.
✓ Branch 3 taken 16 times.
61 && 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
2287 ) {
2288 45 av_log(ic, AV_LOG_DEBUG, "Setting avg frame rate based on r frame rate\n");
2289 45 st->avg_frame_rate = st->r_frame_rate;
2290 }
2291
2292 5796 av_freep(&sti->info->duration_error);
2293 5796 sti->info->last_dts = AV_NOPTS_VALUE;
2294 5796 sti->info->duration_count = 0;
2295 5796 sti->info->rfps_duration_sum = 0;
2296 }
2297 7244 }
2298
2299 8860 static int extract_extradata_check(AVStream *st)
2300 {
2301 8860 const AVBitStreamFilter *const f = av_bsf_get_by_name("extract_extradata");
2302
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8860 times.
8860 if (!f)
2303 return 0;
2304
2305
1/2
✓ Branch 0 taken 8860 times.
✗ Branch 1 not taken.
8860 if (f->codec_ids) {
2306 const enum AVCodecID *ids;
2307
2/2
✓ Branch 0 taken 85908 times.
✓ Branch 1 taken 8057 times.
93965 for (ids = f->codec_ids; *ids != AV_CODEC_ID_NONE; ids++)
2308
2/2
✓ Branch 0 taken 803 times.
✓ Branch 1 taken 85105 times.
85908 if (*ids == st->codecpar->codec_id)
2309 803 return 1;
2310 }
2311
2312 8057 return 0;
2313 }
2314
2315 6428 static int extract_extradata_init(AVStream *st)
2316 {
2317 6428 FFStream *const sti = ffstream(st);
2318 const AVBitStreamFilter *f;
2319 int ret;
2320
2321 6428 f = av_bsf_get_by_name("extract_extradata");
2322
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6428 times.
6428 if (!f)
2323 goto finish;
2324
2325 /* check that the codec id is supported */
2326 6428 ret = extract_extradata_check(st);
2327
2/2
✓ Branch 0 taken 5735 times.
✓ Branch 1 taken 693 times.
6428 if (!ret)
2328 5735 goto finish;
2329
2330 693 ret = av_bsf_alloc(f, &sti->extract_extradata.bsf);
2331
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 693 times.
693 if (ret < 0)
2332 return ret;
2333
2334 693 ret = avcodec_parameters_copy(sti->extract_extradata.bsf->par_in,
2335 693 st->codecpar);
2336
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 693 times.
693 if (ret < 0)
2337 goto fail;
2338
2339 693 sti->extract_extradata.bsf->time_base_in = st->time_base;
2340
2341 693 ret = av_bsf_init(sti->extract_extradata.bsf);
2342
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 693 times.
693 if (ret < 0)
2343 goto fail;
2344
2345 693 finish:
2346 6428 sti->extract_extradata.inited = 1;
2347
2348 6428 return 0;
2349 fail:
2350 av_bsf_free(&sti->extract_extradata.bsf);
2351 return ret;
2352 }
2353
2354 142206 static int extract_extradata(FFFormatContext *si, AVStream *st, const AVPacket *pkt)
2355 {
2356 142206 FFStream *const sti = ffstream(st);
2357 142206 AVPacket *const pkt_ref = si->parse_pkt;
2358 int ret;
2359
2360
2/2
✓ Branch 0 taken 6428 times.
✓ Branch 1 taken 135778 times.
142206 if (!sti->extract_extradata.inited) {
2361 6428 ret = extract_extradata_init(st);
2362
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6428 times.
6428 if (ret < 0)
2363 return ret;
2364 }
2365
2366
3/4
✓ Branch 0 taken 142206 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 141210 times.
✓ Branch 3 taken 996 times.
142206 if (sti->extract_extradata.inited && !sti->extract_extradata.bsf)
2367 141210 return 0;
2368
2369 996 ret = av_packet_ref(pkt_ref, pkt);
2370
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 996 times.
996 if (ret < 0)
2371 return ret;
2372
2373 996 ret = av_bsf_send_packet(sti->extract_extradata.bsf, pkt_ref);
2374
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 996 times.
996 if (ret < 0) {
2375 av_packet_unref(pkt_ref);
2376 return ret;
2377 }
2378
2379
4/4
✓ Branch 0 taken 1992 times.
✓ Branch 1 taken 311 times.
✓ Branch 2 taken 1307 times.
✓ Branch 3 taken 685 times.
2303 while (ret >= 0 && !sti->avctx->extradata) {
2380 1307 ret = av_bsf_receive_packet(sti->extract_extradata.bsf, pkt_ref);
2381
2/2
✓ Branch 0 taken 311 times.
✓ Branch 1 taken 996 times.
1307 if (ret < 0) {
2382
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 311 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
311 if (ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
2383 return ret;
2384 311 continue;
2385 }
2386
2387
2/2
✓ Branch 0 taken 856 times.
✓ Branch 1 taken 311 times.
1167 for (int i = 0; i < pkt_ref->side_data_elems; i++) {
2388 856 AVPacketSideData *const side_data = &pkt_ref->side_data[i];
2389
2/2
✓ Branch 0 taken 685 times.
✓ Branch 1 taken 171 times.
856 if (side_data->type == AV_PKT_DATA_NEW_EXTRADATA) {
2390 685 sti->avctx->extradata = side_data->data;
2391 685 sti->avctx->extradata_size = side_data->size;
2392 685 side_data->data = NULL;
2393 685 side_data->size = 0;
2394 685 break;
2395 }
2396 }
2397 996 av_packet_unref(pkt_ref);
2398 }
2399
2400 996 return 0;
2401 }
2402
2403 7421 static int add_coded_side_data(AVStream *st, AVCodecContext *avctx)
2404 {
2405
2/2
✓ Branch 0 taken 163 times.
✓ Branch 1 taken 7421 times.
7584 for (int i = 0; i < avctx->nb_coded_side_data; i++) {
2406 163 const AVPacketSideData *const sd_src = &avctx->coded_side_data[i];
2407 uint8_t *dst_data;
2408 163 dst_data = av_stream_new_side_data(st, sd_src->type, sd_src->size);
2409
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 163 times.
163 if (!dst_data)
2410 return AVERROR(ENOMEM);
2411 163 memcpy(dst_data, sd_src->data, sd_src->size);
2412 }
2413 7421 return 0;
2414 }
2415
2416 6813 int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
2417 {
2418 6813 FFFormatContext *const si = ffformatcontext(ic);
2419 6813 int count = 0, ret = 0;
2420 int64_t read_size;
2421 6813 AVPacket *pkt1 = si->pkt;
2422 6813 int64_t old_offset = avio_tell(ic->pb);
2423 // new streams might appear, no options for those
2424 6813 int orig_nb_streams = ic->nb_streams;
2425 int flush_codecs;
2426 6813 int64_t max_analyze_duration = ic->max_analyze_duration;
2427 int64_t max_stream_analyze_duration;
2428 int64_t max_subtitle_analyze_duration;
2429 6813 int64_t probesize = ic->probesize;
2430 6813 int eof_reached = 0;
2431 6813 int *missing_streams = av_opt_ptr(ic->iformat->priv_class, ic->priv_data, "missing_streams");
2432
2433 6813 flush_codecs = probesize > 0;
2434
2435 6813 av_opt_set_int(ic, "skip_clear", 1, AV_OPT_SEARCH_CHILDREN);
2436
2437 6813 max_stream_analyze_duration = max_analyze_duration;
2438 6813 max_subtitle_analyze_duration = max_analyze_duration;
2439
2/2
✓ Branch 0 taken 6812 times.
✓ Branch 1 taken 1 times.
6813 if (!max_analyze_duration) {
2440 6812 max_stream_analyze_duration =
2441 6812 max_analyze_duration = 5*AV_TIME_BASE;
2442 6812 max_subtitle_analyze_duration = 30*AV_TIME_BASE;
2443
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 6783 times.
6812 if (!strcmp(ic->iformat->name, "flv"))
2444 29 max_stream_analyze_duration = 90*AV_TIME_BASE;
2445
4/4
✓ Branch 0 taken 6787 times.
✓ Branch 1 taken 25 times.
✓ Branch 2 taken 36 times.
✓ Branch 3 taken 6751 times.
6812 if (!strcmp(ic->iformat->name, "mpeg") || !strcmp(ic->iformat->name, "mpegts"))
2446 61 max_stream_analyze_duration = 7*AV_TIME_BASE;
2447 }
2448
2449
2/2
✓ Branch 0 taken 4160 times.
✓ Branch 1 taken 2653 times.
6813 if (ic->pb) {
2450 4160 FFIOContext *const ctx = ffiocontext(ic->pb);
2451 4160 av_log(ic, AV_LOG_DEBUG, "Before avformat_find_stream_info() pos: %"PRId64" bytes read:%"PRId64" seeks:%d nb_streams:%d\n",
2452 avio_tell(ic->pb), ctx->bytes_read, ctx->seek_count, ic->nb_streams);
2453 }
2454
2455
2/2
✓ Branch 0 taken 7290 times.
✓ Branch 1 taken 6813 times.
14103 for (unsigned i = 0; i < ic->nb_streams; i++) {
2456 const AVCodec *codec;
2457 7290 AVDictionary *thread_opt = NULL;
2458 7290 AVStream *const st = ic->streams[i];
2459 7290 FFStream *const sti = ffstream(st);
2460 7290 AVCodecContext *const avctx = sti->avctx;
2461
2462
2/2
✓ Branch 0 taken 1829 times.
✓ Branch 1 taken 5461 times.
7290 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ||
2463
2/2
✓ Branch 0 taken 76 times.
✓ Branch 1 taken 1753 times.
1829 st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) {
2464 /* if (!st->time_base.num)
2465 st->time_base = */
2466
1/2
✓ Branch 0 taken 5537 times.
✗ Branch 1 not taken.
5537 if (!avctx->time_base.num)
2467 5537 avctx->time_base = st->time_base;
2468 }
2469
2470 /* check if the caller has overridden the codec id */
2471 // only for the split stuff
2472
4/6
✓ Branch 0 taken 7290 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7290 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6800 times.
✓ Branch 5 taken 490 times.
7290 if (!sti->parser && !(ic->flags & AVFMT_FLAG_NOPARSE) && sti->request_probe <= 0) {
2473 6800 sti->parser = av_parser_init(st->codecpar->codec_id);
2474
2/2
✓ Branch 0 taken 4560 times.
✓ Branch 1 taken 2240 times.
6800 if (sti->parser) {
2475
2/2
✓ Branch 0 taken 654 times.
✓ Branch 1 taken 3906 times.
4560 if (sti->need_parsing == AVSTREAM_PARSE_HEADERS) {
2476 654 sti->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES;
2477
2/2
✓ Branch 0 taken 722 times.
✓ Branch 1 taken 3184 times.
3906 } else if (sti->need_parsing == AVSTREAM_PARSE_FULL_RAW) {
2478 722 sti->parser->flags |= PARSER_FLAG_USE_CODEC_TS;
2479 }
2480
2/2
✓ Branch 0 taken 667 times.
✓ Branch 1 taken 1573 times.
2240 } else if (sti->need_parsing) {
2481 667 av_log(ic, AV_LOG_VERBOSE, "parser not found for codec "
2482 "%s, packets or times may be invalid.\n",
2483 667 avcodec_get_name(st->codecpar->codec_id));
2484 }
2485 }
2486
2487 7290 ret = avcodec_parameters_to_context(avctx, st->codecpar);
2488
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7290 times.
7290 if (ret < 0)
2489 goto find_stream_info_err;
2490
2/2
✓ Branch 0 taken 6800 times.
✓ Branch 1 taken 490 times.
7290 if (sti->request_probe <= 0)
2491 6800 sti->avctx_inited = 1;
2492
2493 7290 codec = find_probe_decoder(ic, st, st->codecpar->codec_id);
2494
2495 /* Force thread count to 1 since the H.264 decoder will not extract
2496 * SPS and PPS to extradata during multi-threaded decoding. */
2497
2/2
✓ Branch 0 taken 7007 times.
✓ Branch 1 taken 283 times.
7290 av_dict_set(options ? &options[i] : &thread_opt, "threads", "1", 0);
2498 /* Force lowres to 0. The decoder might reduce the video size by the
2499 * lowres factor, and we don't want that propagated to the stream's
2500 * codecpar */
2501
2/2
✓ Branch 0 taken 7007 times.
✓ Branch 1 taken 283 times.
7290 av_dict_set(options ? &options[i] : &thread_opt, "lowres", "0", 0);
2502
2503
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7290 times.
7290 if (ic->codec_whitelist)
2504 av_dict_set(options ? &options[i] : &thread_opt, "codec_whitelist", ic->codec_whitelist, 0);
2505
2506 // Try to just open decoders, in case this is enough to get parameters.
2507 // Also ensure that subtitle_header is properly set.
2508
4/4
✓ Branch 1 taken 6541 times.
✓ Branch 2 taken 749 times.
✓ Branch 3 taken 489 times.
✓ Branch 4 taken 6052 times.
7290 if (!has_codec_parameters(st, NULL) && sti->request_probe <= 0 ||
2509
2/2
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 1170 times.
1238 st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) {
2510
3/4
✓ Branch 0 taken 6116 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 6116 times.
✗ Branch 3 not taken.
6120 if (codec && !avctx->codec)
2511
4/4
✓ Branch 0 taken 5837 times.
✓ Branch 1 taken 279 times.
✓ Branch 3 taken 9 times.
✓ Branch 4 taken 6107 times.
6116 if (avcodec_open2(avctx, codec, options ? &options[i] : &thread_opt) < 0)
2512 9 av_log(ic, AV_LOG_WARNING,
2513 "Failed to open codec in %s\n",__FUNCTION__);
2514 }
2515
2/2
✓ Branch 0 taken 283 times.
✓ Branch 1 taken 7007 times.
7290 if (!options)
2516 283 av_dict_free(&thread_opt);
2517 }
2518
2519 6813 read_size = 0;
2520 179262 for (;;) {
2521 const AVPacket *pkt;
2522 AVStream *st;
2523 FFStream *sti;
2524 AVCodecContext *avctx;
2525 int analyzed_all_streams;
2526 unsigned i;
2527
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 186075 times.
186075 if (ff_check_interrupt(&ic->interrupt_callback)) {
2528 ret = AVERROR_EXIT;
2529 av_log(ic, AV_LOG_DEBUG, "interrupted\n");
2530 break;
2531 }
2532
2533 /* check if one codec still needs to be handled */
2534
2/2
✓ Branch 0 taken 195325 times.
✓ Branch 1 taken 97370 times.
292695 for (i = 0; i < ic->nb_streams; i++) {
2535 195325 AVStream *const st = ic->streams[i];
2536 195325 FFStream *const sti = ffstream(st);
2537 195325 int fps_analyze_framecount = 20;
2538 int count;
2539
2540
2/2
✓ Branch 1 taken 8092 times.
✓ Branch 2 taken 187233 times.
195325 if (!has_codec_parameters(st, NULL))
2541 8092 break;
2542 /* If the timebase is coarse (like the usual millisecond precision
2543 * of mkv), we need to analyze more frames to reliably arrive at
2544 * the correct fps. */
2545
2/2
✓ Branch 1 taken 107686 times.
✓ Branch 2 taken 79547 times.
187233 if (av_q2d(st->time_base) > 0.0005)
2546 107686 fps_analyze_framecount *= 2;
2547
2/2
✓ Branch 1 taken 93398 times.
✓ Branch 2 taken 93835 times.
187233 if (!tb_unreliable(sti->avctx))
2548 93398 fps_analyze_framecount = 0;
2549
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 187233 times.
187233 if (ic->fps_probe_size >= 0)
2550 fps_analyze_framecount = ic->fps_probe_size;
2551
2/2
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 187190 times.
187233 if (st->disposition & AV_DISPOSITION_ATTACHED_PIC)
2552 43 fps_analyze_framecount = 0;
2553 /* variable fps and no guess at the real fps */
2554
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 187217 times.
187233 count = (ic->iformat->flags & AVFMT_NOTIMESTAMPS) ?
2555 16 sti->info->codec_info_duration_fields/2 :
2556 187217 sti->info->duration_count;
2557
4/4
✓ Branch 0 taken 88826 times.
✓ Branch 1 taken 98407 times.
✓ Branch 2 taken 223 times.
✓ Branch 3 taken 88603 times.
187233 if (!(st->r_frame_rate.num && st->avg_frame_rate.num) &&
2558
2/2
✓ Branch 0 taken 48441 times.
✓ Branch 1 taken 50189 times.
98630 st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
2559
2/2
✓ Branch 0 taken 36823 times.
✓ Branch 1 taken 11618 times.
48441 if (count < fps_analyze_framecount)
2560 36823 break;
2561 }
2562 // Look at the first 3 frames if there is evidence of frame delay
2563 // but the decoder delay is not set.
2564
6/6
✓ Branch 0 taken 2857 times.
✓ Branch 1 taken 147553 times.
✓ Branch 2 taken 108 times.
✓ Branch 3 taken 2749 times.
✓ Branch 4 taken 11 times.
✓ Branch 5 taken 97 times.
150410 if (sti->info->frame_delay_evidence && count < 2 && sti->avctx->has_b_frames == 0)
2565 11 break;
2566
2/2
✓ Branch 0 taken 137152 times.
✓ Branch 1 taken 13247 times.
150399 if (!sti->avctx->extradata &&
2567
6/6
✓ Branch 0 taken 134830 times.
✓ Branch 1 taken 2322 times.
✓ Branch 2 taken 110 times.
✓ Branch 3 taken 134720 times.
✓ Branch 4 taken 110 times.
✓ Branch 5 taken 2322 times.
139584 (!sti->extract_extradata.inited || sti->extract_extradata.bsf) &&
2568 2432 extract_extradata_check(st))
2569 110 break;
2570
2/2
✓ Branch 0 taken 46954 times.
✓ Branch 1 taken 103335 times.
150289 if (sti->first_dts == AV_NOPTS_VALUE &&
2571
4/4
✓ Branch 0 taken 46938 times.
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 44440 times.
✓ Branch 3 taken 2498 times.
93892 !(ic->iformat->flags & AVFMT_NOTIMESTAMPS) &&
2572
2/2
✓ Branch 0 taken 46895 times.
✓ Branch 1 taken 43 times.
46938 sti->codec_info_nb_frames < ((st->disposition & AV_DISPOSITION_ATTACHED_PIC) ? 1 : ic->max_ts_probe) &&
2573
2/2
✓ Branch 0 taken 35572 times.
✓ Branch 1 taken 8868 times.
44440 (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ||
2574
2/2
✓ Branch 0 taken 771 times.
✓ Branch 1 taken 34801 times.
35572 st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO))
2575 break;
2576 }
2577 186075 analyzed_all_streams = 0;
2578
4/4
✓ Branch 0 taken 1798 times.
✓ Branch 1 taken 184277 times.
✓ Branch 2 taken 1768 times.
✓ Branch 3 taken 30 times.
186075 if (!missing_streams || !*missing_streams)
2579
2/2
✓ Branch 0 taken 97340 times.
✓ Branch 1 taken 88705 times.
186045 if (i == ic->nb_streams) {
2580 97340 analyzed_all_streams = 1;
2581 /* NOTE: If the format has no header, then we need to read some
2582 * packets to get most of the streams, so we cannot stop here. */
2583
2/2
✓ Branch 0 taken 2745 times.
✓ Branch 1 taken 94595 times.
97340 if (!(ic->ctx_flags & AVFMTCTX_NOHEADER)) {
2584 /* If we found the info for all the codecs, we can stop. */
2585 2745 ret = count;
2586 2745 av_log(ic, AV_LOG_DEBUG, "All info found\n");
2587 2745 flush_codecs = 0;
2588 2745 break;
2589 }
2590 }
2591 /* We did not get all the codec info, but we read too much data. */
2592
2/2
✓ Branch 0 taken 2590 times.
✓ Branch 1 taken 180740 times.
183330 if (read_size >= probesize) {
2593 2590 ret = count;
2594 2590 av_log(ic, AV_LOG_DEBUG,
2595 "Probe buffer size limit of %"PRId64" bytes reached\n", probesize);
2596
2/2
✓ Branch 0 taken 2603 times.
✓ Branch 1 taken 2590 times.
5193 for (unsigned i = 0; i < ic->nb_streams; i++) {
2597 2603 AVStream *const st = ic->streams[i];
2598 2603 FFStream *const sti = ffstream(st);
2599
2/2
✓ Branch 0 taken 57 times.
✓ Branch 1 taken 2546 times.
2603 if (!st->r_frame_rate.num &&
2600
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 42 times.
57 sti->info->duration_count <= 1 &&
2601
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 13 times.
15 st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
2602
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 strcmp(ic->iformat->name, "image2"))
2603 2 av_log(ic, AV_LOG_WARNING,
2604 "Stream #%d: not enough frames to estimate rate; "
2605 "consider increasing probesize\n", i);
2606 }
2607 2590 break;
2608 }
2609
2610 /* NOTE: A new stream can be added there if no header in file
2611 * (AVFMTCTX_NOHEADER). */
2612 180740 ret = read_frame_internal(ic, pkt1);
2613
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 180740 times.
180740 if (ret == AVERROR(EAGAIN))
2614 continue;
2615
2616
2/2
✓ Branch 0 taken 1249 times.
✓ Branch 1 taken 179491 times.
180740 if (ret < 0) {
2617 /* EOF or error*/
2618 1249 eof_reached = 1;
2619 1249 break;
2620 }
2621
2622
1/2
✓ Branch 0 taken 179491 times.
✗ Branch 1 not taken.
179491 if (!(ic->flags & AVFMT_FLAG_NOBUFFER)) {
2623 179491 ret = avpriv_packet_list_put(&si->packet_buffer,
2624 pkt1, NULL, 0);
2625
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 179491 times.
179491 if (ret < 0)
2626 goto unref_then_goto_end;
2627
2628 179491 pkt = &si->packet_buffer.tail->pkt;
2629 } else {
2630 pkt = pkt1;
2631 }
2632
2633 179491 st = ic->streams[pkt->stream_index];
2634 179491 sti = ffstream(st);
2635
2/2
✓ Branch 0 taken 179436 times.
✓ Branch 1 taken 55 times.
179491 if (!(st->disposition & AV_DISPOSITION_ATTACHED_PIC))
2636 179436 read_size += pkt->size;
2637
2638 179491 avctx = sti->avctx;
2639
2/2
✓ Branch 0 taken 621 times.
✓ Branch 1 taken 178870 times.
179491 if (!sti->avctx_inited) {
2640 621 ret = avcodec_parameters_to_context(avctx, st->codecpar);
2641
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 621 times.
621 if (ret < 0)
2642 goto unref_then_goto_end;
2643 621 sti->avctx_inited = 1;
2644 }
2645
2646
4/4
✓ Branch 0 taken 154777 times.
✓ Branch 1 taken 24714 times.
✓ Branch 2 taken 143661 times.
✓ Branch 3 taken 11116 times.
179491 if (pkt->dts != AV_NOPTS_VALUE && sti->codec_info_nb_frames > 1) {
2647 /* check for non-increasing dts */
2648
2/2
✓ Branch 0 taken 139299 times.
✓ Branch 1 taken 4362 times.
143661 if (sti->info->fps_last_dts != AV_NOPTS_VALUE &&
2649
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 139276 times.
139299 sti->info->fps_last_dts >= pkt->dts) {
2650 23 av_log(ic, AV_LOG_DEBUG,
2651 "Non-increasing DTS in stream %d: packet %d with DTS "
2652 "%"PRId64", packet %d with DTS %"PRId64"\n",
2653 23 st->index, sti->info->fps_last_dts_idx,
2654 23 sti->info->fps_last_dts, sti->codec_info_nb_frames,
2655 23 pkt->dts);
2656 23 sti->info->fps_first_dts =
2657 23 sti->info->fps_last_dts = AV_NOPTS_VALUE;
2658 }
2659 /* Check for a discontinuity in dts. If the difference in dts
2660 * is more than 1000 times the average packet duration in the
2661 * sequence, we treat it as a discontinuity. */
2662
2/2
✓ Branch 0 taken 139276 times.
✓ Branch 1 taken 4385 times.
143661 if (sti->info->fps_last_dts != AV_NOPTS_VALUE &&
2663
2/2
✓ Branch 0 taken 134943 times.
✓ Branch 1 taken 4333 times.
139276 sti->info->fps_last_dts_idx > sti->info->fps_first_dts_idx &&
2664 134943 (pkt->dts - (uint64_t)sti->info->fps_last_dts) / 1000 >
2665 134943 (sti->info->fps_last_dts - (uint64_t)sti->info->fps_first_dts) /
2666
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 134943 times.
134943 (sti->info->fps_last_dts_idx - sti->info->fps_first_dts_idx)) {
2667 av_log(ic, AV_LOG_WARNING,
2668 "DTS discontinuity in stream %d: packet %d with DTS "
2669 "%"PRId64", packet %d with DTS %"PRId64"\n",
2670 st->index, sti->info->fps_last_dts_idx,
2671 sti->info->fps_last_dts, sti->codec_info_nb_frames,
2672 pkt->dts);
2673 sti->info->fps_first_dts =
2674 sti->info->fps_last_dts = AV_NOPTS_VALUE;
2675 }
2676
2677 /* update stored dts values */
2678
2/2
✓ Branch 0 taken 4385 times.
✓ Branch 1 taken 139276 times.
143661 if (sti->info->fps_first_dts == AV_NOPTS_VALUE) {
2679 4385 sti->info->fps_first_dts = pkt->dts;
2680 4385 sti->info->fps_first_dts_idx = sti->codec_info_nb_frames;
2681 }
2682 143661 sti->info->fps_last_dts = pkt->dts;
2683 143661 sti->info->fps_last_dts_idx = sti->codec_info_nb_frames;
2684 }
2685
2/2
✓ Branch 0 taken 167372 times.
✓ Branch 1 taken 12119 times.
179491 if (sti->codec_info_nb_frames > 1) {
2686 167372 int64_t t = 0;
2687 int64_t limit;
2688
2689
1/2
✓ Branch 0 taken 167372 times.
✗ Branch 1 not taken.
167372 if (st->time_base.den > 0)
2690 167372 t = av_rescale_q(sti->info->codec_info_duration, st->time_base, AV_TIME_BASE_Q);
2691
2/2
✓ Branch 0 taken 82989 times.
✓ Branch 1 taken 84383 times.
167372 if (st->avg_frame_rate.num > 0)
2692
1/2
✓ Branch 1 taken 82989 times.
✗ Branch 2 not taken.
82989 t = FFMAX(t, av_rescale_q(sti->codec_info_nb_frames, av_inv_q(st->avg_frame_rate), AV_TIME_BASE_Q));
2693
2694
2/2
✓ Branch 0 taken 6010 times.
✓ Branch 1 taken 161362 times.
167372 if ( t == 0
2695
2/2
✓ Branch 0 taken 2310 times.
✓ Branch 1 taken 3700 times.
6010 && sti->codec_info_nb_frames > 30
2696
2/2
✓ Branch 0 taken 1150 times.
✓ Branch 1 taken 1160 times.
2310 && sti->info->fps_first_dts != AV_NOPTS_VALUE
2697
1/2
✓ Branch 0 taken 1150 times.
✗ Branch 1 not taken.
1150 && sti->info->fps_last_dts != AV_NOPTS_VALUE) {
2698 1150 int64_t dur = av_sat_sub64(sti->info->fps_last_dts, sti->info->fps_first_dts);
2699
1/2
✓ Branch 0 taken 1150 times.
✗ Branch 1 not taken.
1150 t = FFMAX(t, av_rescale_q(dur, st->time_base, AV_TIME_BASE_Q));
2700 }
2701
2702
2/2
✓ Branch 0 taken 89169 times.
✓ Branch 1 taken 78203 times.
167372 if (analyzed_all_streams) limit = max_analyze_duration;
2703
2/2
✓ Branch 0 taken 91 times.
✓ Branch 1 taken 78112 times.
78203 else if (avctx->codec_type == AVMEDIA_TYPE_SUBTITLE) limit = max_subtitle_analyze_duration;
2704 78112 else limit = max_stream_analyze_duration;
2705
2706
2/2
✓ Branch 0 taken 229 times.
✓ Branch 1 taken 167143 times.
167372 if (t >= limit) {
2707 229 av_log(ic, AV_LOG_VERBOSE, "max_analyze_duration %"PRId64" reached at %"PRId64" microseconds st:%d\n",
2708 limit,
2709 229 t, pkt->stream_index);
2710
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 229 times.
229 if (ic->flags & AVFMT_FLAG_NOBUFFER)
2711 av_packet_unref(pkt1);
2712 229 break;
2713 }
2714
2/2
✓ Branch 0 taken 162046 times.
✓ Branch 1 taken 5097 times.
167143 if (pkt->duration > 0) {
2715
1/8
✗ Branch 0 not taken.
✓ Branch 1 taken 162046 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.
162046 if (avctx->codec_type == AVMEDIA_TYPE_SUBTITLE && pkt->pts != AV_NOPTS_VALUE && st->start_time != AV_NOPTS_VALUE && pkt->pts >= st->start_time
2716 && (uint64_t)pkt->pts - st->start_time < INT64_MAX
2717 ) {
2718 sti->info->codec_info_duration = FFMIN(pkt->pts - st->start_time, sti->info->codec_info_duration + pkt->duration);
2719 } else
2720 162046 sti->info->codec_info_duration += pkt->duration;
2721
4/4
✓ Branch 0 taken 40493 times.
✓ Branch 1 taken 80491 times.
✓ Branch 2 taken 19569 times.
✓ Branch 3 taken 20924 times.
283030 sti->info->codec_info_duration_fields += sti->parser && sti->need_parsing && avctx->ticks_per_frame == 2
2722
2/2
✓ Branch 0 taken 120984 times.
✓ Branch 1 taken 41062 times.
283030 ? sti->parser->repeat_pict + 1 : 2;
2723 }
2724 }
2725
2/2
✓ Branch 0 taken 124886 times.
✓ Branch 1 taken 54376 times.
179262 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
2726 #if FF_API_R_FRAME_RATE
2727 124886 ff_rfps_add_frame(ic, st, pkt->dts);
2728 #endif
2729
6/6
✓ Branch 0 taken 3683 times.
✓ Branch 1 taken 121203 times.
✓ Branch 2 taken 3468 times.
✓ Branch 3 taken 215 times.
✓ Branch 4 taken 1599 times.
✓ Branch 5 taken 1869 times.
124886 if (pkt->dts != pkt->pts && pkt->dts != AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE)
2730 1599 sti->info->frame_delay_evidence = 1;
2731 }
2732
2/2
✓ Branch 0 taken 142206 times.
✓ Branch 1 taken 37056 times.
179262 if (!sti->avctx->extradata) {
2733 142206 ret = extract_extradata(si, st, pkt);
2734
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 142206 times.
142206 if (ret < 0)
2735 goto unref_then_goto_end;
2736 }
2737
2738 /* If still no information, we try to open the codec and to
2739 * decompress the frame. We try to avoid that in most cases as
2740 * it takes longer and uses more memory. For MPEG-4, we need to
2741 * decompress for QuickTime.
2742 *
2743 * If AV_CODEC_CAP_CHANNEL_CONF is set this will force decoding of at
2744 * least one frame of codec data, this makes sure the codec initializes
2745 * the channel configuration and does not only trust the values from
2746 * the container. */
2747
2/2
✓ Branch 0 taken 162700 times.
✓ Branch 1 taken 16562 times.
341962 try_decode_frame(ic, st, pkt,
2748
2/2
✓ Branch 0 taken 78804 times.
✓ Branch 1 taken 83896 times.
162700 (options && i < orig_nb_streams) ? &options[i] : NULL);
2749
2750
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 179262 times.
179262 if (ic->flags & AVFMT_FLAG_NOBUFFER)
2751 av_packet_unref(pkt1);
2752
2753 179262 sti->codec_info_nb_frames++;
2754 179262 count++;
2755 }
2756
2757
2/2
✓ Branch 0 taken 1249 times.
✓ Branch 1 taken 5564 times.
6813 if (eof_reached) {
2758
2/2
✓ Branch 0 taken 1487 times.
✓ Branch 1 taken 1249 times.
2736 for (unsigned stream_index = 0; stream_index < ic->nb_streams; stream_index++) {
2759 1487 AVStream *const st = ic->streams[stream_index];
2760 1487 AVCodecContext *const avctx = ffstream(st)->avctx;
2761
2/2
✓ Branch 1 taken 40 times.
✓ Branch 2 taken 1447 times.
1487 if (!has_codec_parameters(st, NULL)) {
2762 40 const AVCodec *codec = find_probe_decoder(ic, st, st->codecpar->codec_id);
2763
4/4
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 31 times.
40 if (codec && !avctx->codec) {
2764 6 AVDictionary *opts = NULL;
2765
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (ic->codec_whitelist)
2766 av_dict_set(&opts, "codec_whitelist", ic->codec_whitelist, 0);
2767
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)
2768 av_log(ic, AV_LOG_WARNING,
2769 "Failed to open codec in %s\n",__FUNCTION__);
2770 6 av_dict_free(&opts);
2771 }
2772 }
2773
2774 // EOF already reached while reading the stream above.
2775 // So continue with reoordering DTS with whatever delay we have.
2776
4/4
✓ Branch 0 taken 1469 times.
✓ Branch 1 taken 18 times.
✓ Branch 3 taken 16 times.
✓ Branch 4 taken 1453 times.
1487 if (si->packet_buffer.head && !has_decode_delay_been_guessed(st)) {
2777 16 update_dts_from_pts(ic, stream_index, si->packet_buffer.head);
2778 }
2779 }
2780 }
2781
2782
2/2
✓ Branch 0 taken 4068 times.
✓ Branch 1 taken 2745 times.
6813 if (flush_codecs) {
2783 4068 AVPacket *empty_pkt = si->pkt;
2784 4068 int err = 0;
2785 4068 av_packet_unref(empty_pkt);
2786
2787
2/2
✓ Branch 0 taken 4348 times.
✓ Branch 1 taken 4068 times.
8416 for (unsigned i = 0; i < ic->nb_streams; i++) {
2788 4348 AVStream *const st = ic->streams[i];
2789 4348 FFStream *const sti = ffstream(st);
2790
2791 /* flush the decoders */
2792
2/2
✓ Branch 0 taken 4292 times.
✓ Branch 1 taken 56 times.
4348 if (sti->info->found_decoder == 1) {
2793
2/2
✓ Branch 0 taken 4013 times.
✓ Branch 1 taken 279 times.
8305 err = try_decode_frame(ic, st, empty_pkt,
2794
2/2
✓ Branch 0 taken 3999 times.
✓ Branch 1 taken 14 times.
4013 (options && i < orig_nb_streams)
2795 3999 ? &options[i] : NULL);
2796
2797
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4292 times.
4292 if (err < 0) {
2798 av_log(ic, AV_LOG_INFO,
2799 "decoding for stream %d failed\n", st->index);
2800 }
2801 }
2802 }
2803 }
2804
2805 6813 ff_rfps_calculate(ic);
2806
2807
2/2
✓ Branch 0 taken 7431 times.
✓ Branch 1 taken 6813 times.
14244 for (unsigned i = 0; i < ic->nb_streams; i++) {
2808 7431 AVStream *const st = ic->streams[i];
2809 7431 FFStream *const sti = ffstream(st);
2810 7431 AVCodecContext *const avctx = sti->avctx;
2811
2812
2/2
✓ Branch 0 taken 5520 times.
✓ Branch 1 taken 1911 times.
7431 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
2813
6/6
✓ Branch 0 taken 570 times.
✓ Branch 1 taken 4950 times.
✓ Branch 2 taken 536 times.
✓ Branch 3 taken 34 times.
✓ Branch 4 taken 520 times.
✓ Branch 5 taken 16 times.
5520 if (avctx->codec_id == AV_CODEC_ID_RAWVIDEO && !avctx->codec_tag && !avctx->bits_per_coded_sample) {
2814 520 uint32_t tag= avcodec_pix_fmt_to_codec_tag(avctx->pix_fmt);
2815
2/2
✓ Branch 1 taken 516 times.
✓ Branch 2 taken 4 times.
520 if (avpriv_pix_fmt_find(PIX_FMT_LIST_RAW, tag) == avctx->pix_fmt)
2816 516 avctx->codec_tag= tag;
2817 }
2818
2819 /* estimate average framerate if not set by demuxer */
2820
2/2
✓ Branch 0 taken 3503 times.
✓ Branch 1 taken 2017 times.
5520 if (sti->info->codec_info_duration_fields &&
2821
2/2
✓ Branch 0 taken 766 times.
✓ Branch 1 taken 2737 times.
3503 !st->avg_frame_rate.num &&
2822
1/2
✓ Branch 0 taken 766 times.
✗ Branch 1 not taken.
766 sti->info->codec_info_duration) {
2823 766 int best_fps = 0;
2824 766 double best_error = 0.01;
2825 766 AVRational codec_frame_rate = avctx->framerate;
2826
2827
1/2
✓ Branch 0 taken 766 times.
✗ Branch 1 not taken.
766 if (sti->info->codec_info_duration >= INT64_MAX / st->time_base.num / 2||
2828
1/2
✓ Branch 0 taken 766 times.
✗ Branch 1 not taken.
766 sti->info->codec_info_duration_fields >= INT64_MAX / st->time_base.den ||
2829
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 766 times.
766 sti->info->codec_info_duration < 0)
2830 continue;
2831 766 av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den,
2832 766 sti->info->codec_info_duration_fields * (int64_t) st->time_base.den,
2833 766 sti->info->codec_info_duration * 2 * (int64_t) st->time_base.num, 60000);
2834
2835 /* Round guessed framerate to a "standard" framerate if it's
2836 * within 1% of the original estimate. */
2837
2/2
✓ Branch 0 taken 305634 times.
✓ Branch 1 taken 766 times.
306400 for (int j = 0; j < MAX_STD_TIMEBASES; j++) {
2838 305634 AVRational std_fps = { get_std_framerate(j), 12 * 1001 };
2839 305634 double error = fabs(av_q2d(st->avg_frame_rate) /
2840 305634 av_q2d(std_fps) - 1);
2841
2842
2/2
✓ Branch 0 taken 2252 times.
✓ Branch 1 taken 303382 times.
305634 if (error < best_error) {
2843 2252 best_error = error;
2844 2252 best_fps = std_fps.num;
2845 }
2846
2847
4/6
✓ Branch 0 taken 10374 times.
✓ Branch 1 taken 295260 times.
✓ Branch 2 taken 10374 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 10374 times.
✗ Branch 5 not taken.
305634 if (si->prefer_codec_framerate && codec_frame_rate.num > 0 && codec_frame_rate.den > 0) {
2848 10374 error = fabs(av_q2d(codec_frame_rate) /
2849 10374 av_q2d(std_fps) - 1);
2850
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10364 times.
10374 if (error < best_error) {
2851 10 best_error = error;
2852 10 best_fps = std_fps.num;
2853 }
2854 }
2855 }
2856
2/2
✓ Branch 0 taken 763 times.
✓ Branch 1 taken 3 times.
766 if (best_fps)
2857 763 av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den,
2858 best_fps, 12 * 1001, INT_MAX);
2859 }
2860
2861
2/2
✓ Branch 0 taken 1710 times.
✓ Branch 1 taken 3810 times.
5520 if (!st->r_frame_rate.num) {
2862 1710 if ( avctx->time_base.den * (int64_t) st->time_base.num
2863
2/2
✓ Branch 0 taken 1701 times.
✓ Branch 1 taken 9 times.
1710 <= avctx->time_base.num * (uint64_t)avctx->ticks_per_frame * st->time_base.den) {
2864 1701 av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den,
2865 1701 avctx->time_base.den, (int64_t)avctx->time_base.num * avctx->ticks_per_frame, INT_MAX);
2866 } else {
2867 9 st->r_frame_rate.num = st->time_base.den;
2868 9 st->r_frame_rate.den = st->time_base.num;
2869 }
2870 }
2871
3/4
✓ Branch 0 taken 114 times.
✓ Branch 1 taken 5406 times.
✓ Branch 2 taken 114 times.
✗ Branch 3 not taken.
5520 if (sti->display_aspect_ratio.num && sti->display_aspect_ratio.den) {
2872 114 AVRational hw_ratio = { avctx->height, avctx->width };
2873 114 st->sample_aspect_ratio = av_mul_q(sti->display_aspect_ratio,
2874 hw_ratio);
2875 }
2876
2/2
✓ Branch 0 taken 1760 times.
✓ Branch 1 taken 151 times.
1911 } else if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
2877
2/2
✓ Branch 0 taken 482 times.
✓ Branch 1 taken 1278 times.
1760 if (!avctx->bits_per_coded_sample)
2878 482 avctx->bits_per_coded_sample =
2879 482 av_get_bits_per_sample(avctx->codec_id);
2880 // set stream disposition based on audio service type
2881
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 1760 times.
1760 switch (avctx->audio_service_type) {
2882 case AV_AUDIO_SERVICE_TYPE_EFFECTS:
2883 st->disposition = AV_DISPOSITION_CLEAN_EFFECTS;
2884 break;
2885 case AV_AUDIO_SERVICE_TYPE_VISUALLY_IMPAIRED:
2886 st->disposition = AV_DISPOSITION_VISUAL_IMPAIRED;
2887 break;
2888 case AV_AUDIO_SERVICE_TYPE_HEARING_IMPAIRED:
2889 st->disposition = AV_DISPOSITION_HEARING_IMPAIRED;
2890 break;
2891 case AV_AUDIO_SERVICE_TYPE_COMMENTARY:
2892 st->disposition = AV_DISPOSITION_COMMENT;
2893 break;
2894 case AV_AUDIO_SERVICE_TYPE_KARAOKE:
2895 st->disposition = AV_DISPOSITION_KARAOKE;
2896 break;
2897 }
2898 }
2899 }
2900
2901
1/2
✓ Branch 0 taken 6813 times.
✗ Branch 1 not taken.
6813 if (probesize)
2902 6813 estimate_timings(ic, old_offset);
2903
2904 6813 av_opt_set_int(ic, "skip_clear", 0, AV_OPT_SEARCH_CHILDREN);
2905
2906
3/4
✓ Branch 0 taken 5564 times.
✓ Branch 1 taken 1249 times.
✓ Branch 2 taken 5564 times.
✗ Branch 3 not taken.
6813 if (ret >= 0 && ic->nb_streams)
2907 /* We could not have all the codec parameters before EOF. */
2908 5564 ret = -1;
2909
2/2
✓ Branch 0 taken 7431 times.
✓ Branch 1 taken 6813 times.
14244 for (unsigned i = 0; i < ic->nb_streams; i++) {
2910 7431 AVStream *const st = ic->streams[i];
2911 7431 FFStream *const sti = ffstream(st);
2912 const char *errmsg;
2913
2914 /* if no packet was ever seen, update context now for has_codec_parameters */
2915
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 7421 times.
7431 if (!sti->avctx_inited) {
2916
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 4 times.
10 if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
2917
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 st->codecpar->format == AV_SAMPLE_FMT_NONE)
2918 6 st->codecpar->format = sti->avctx->sample_fmt;
2919 10 ret = avcodec_parameters_to_context(sti->avctx, st->codecpar);
2920
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (ret < 0)
2921 goto find_stream_info_err;
2922 }
2923
2/2
✓ Branch 1 taken 43 times.
✓ Branch 2 taken 7388 times.
7431 if (!has_codec_parameters(st, &errmsg)) {
2924 char buf[256];
2925 43 avcodec_string(buf, sizeof(buf), sti->avctx, 0);
2926 43 av_log(ic, AV_LOG_WARNING,
2927 "Could not find codec parameters for stream %d (%s): %s\n"
2928 "Consider increasing the value for the 'analyzeduration' (%"PRId64") and 'probesize' (%"PRId64") options\n",
2929 i, buf, errmsg, ic->max_analyze_duration, ic->probesize);
2930 } else {
2931 7388 ret = 0;
2932 }
2933 }
2934
2935 6813 ret = compute_chapters_end(ic);
2936
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6813 times.
6813 if (ret < 0)
2937 goto find_stream_info_err;
2938
2939 /* update the stream parameters from the internal codec contexts */
2940
2/2
✓ Branch 0 taken 7431 times.
✓ Branch 1 taken 6813 times.
14244 for (unsigned i = 0; i < ic->nb_streams; i++) {
2941 7431 AVStream *const st = ic->streams[i];
2942 7431 FFStream *const sti = ffstream(st);
2943
2944
2/2
✓ Branch 0 taken 7421 times.
✓ Branch 1 taken 10 times.
7431 if (sti->avctx_inited) {
2945 7421 ret = avcodec_parameters_from_context(st->codecpar, sti->avctx);
2946
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7421 times.
7421 if (ret < 0)
2947 goto find_stream_info_err;
2948 7421 ret = add_coded_side_data(st, sti->avctx);
2949
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7421 times.
7421 if (ret < 0)
2950 goto find_stream_info_err;
2951 }
2952
2953 7431 sti->avctx_inited = 0;
2954 }
2955
2956 6813 find_stream_info_err:
2957
2/2
✓ Branch 0 taken 7431 times.
✓ Branch 1 taken 6813 times.
14244 for (unsigned i = 0; i < ic->nb_streams; i++) {
2958 7431 AVStream *const st = ic->streams[i];
2959 7431 FFStream *const sti = ffstream(st);
2960
1/2
✓ Branch 0 taken 7431 times.
✗ Branch 1 not taken.
7431 if (sti->info) {
2961 7431 av_freep(&sti->info->duration_error);
2962 7431 av_freep(&sti->info);
2963 }
2964 7431 avcodec_close(sti->avctx);
2965 // FIXME: avcodec_close() frees AVOption settable fields which includes ch_layout,
2966 // so we need to restore it.
2967 7431 av_channel_layout_copy(&sti->avctx->ch_layout, &st->codecpar->ch_layout);
2968 7431 av_bsf_free(&sti->extract_extradata.bsf);
2969 }
2970
2/2
✓ Branch 0 taken 4160 times.
✓ Branch 1 taken 2653 times.
6813 if (ic->pb) {