FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/demux.c
Date: 2026-10-28 19:13:44
Exec Total Coverage
Lines: 1777 2014 88.2%
Functions: 50 50 100.0%
Branches: 1637 1986 82.4%

Line Branch Exec Source
1 /*
2 * Core demuxing component
3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include <stdint.h>
23
24 #include "config_components.h"
25
26 #include "libavutil/avassert.h"
27 #include "libavutil/avstring.h"
28 #include "libavutil/dict.h"
29 #include "libavutil/internal.h"
30 #include "libavutil/intreadwrite.h"
31 #include "libavutil/mathematics.h"
32 #include "libavutil/mem.h"
33 #include "libavutil/opt.h"
34 #include "libavutil/pixfmt.h"
35 #include "libavutil/time.h"
36 #include "libavutil/timestamp.h"
37
38 #include "libavcodec/avcodec.h"
39 #include "libavcodec/bsf.h"
40 #include "libavcodec/codec_desc.h"
41 #include "libavcodec/internal.h"
42 #include "packet_internal.h"
43 #include "libavcodec/raw.h"
44
45 #include "avformat.h"
46 #include "avformat_internal.h"
47 #include "avio_internal.h"
48 #include "demux.h"
49 #include "id3v2.h"
50 #include "internal.h"
51 #include "url.h"
52
53 2677936 static int64_t wrap_timestamp(const AVStream *st, int64_t timestamp)
54 {
55 2677936 const FFStream *const sti = cffstream(st);
56
4/4
✓ Branch 0 taken 180421 times.
✓ Branch 1 taken 2497515 times.
✓ Branch 2 taken 175391 times.
✓ Branch 3 taken 5030 times.
2677936 if (sti->pts_wrap_behavior != AV_PTS_WRAP_IGNORE && st->pts_wrap_bits < 64 &&
57
4/4
✓ Branch 0 taken 174103 times.
✓ Branch 1 taken 1288 times.
✓ Branch 2 taken 111276 times.
✓ Branch 3 taken 62827 times.
175391 sti->pts_wrap_reference != AV_NOPTS_VALUE && timestamp != AV_NOPTS_VALUE) {
58
2/2
✓ Branch 0 taken 111072 times.
✓ Branch 1 taken 204 times.
111276 if (sti->pts_wrap_behavior == AV_PTS_WRAP_ADD_OFFSET &&
59
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 111072 times.
111072 timestamp < sti->pts_wrap_reference)
60 ✗ return timestamp + (1ULL << st->pts_wrap_bits);
61
2/2
✓ Branch 0 taken 204 times.
✓ Branch 1 taken 111072 times.
111276 else if (sti->pts_wrap_behavior == AV_PTS_WRAP_SUB_OFFSET &&
62
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 154 times.
204 timestamp >= sti->pts_wrap_reference)
63 50 return timestamp - (1ULL << st->pts_wrap_bits);
64 }
65 2677886 return timestamp;
66 }
67
68 368774 int64_t ff_wrap_timestamp(const AVStream *st, int64_t timestamp)
69 {
70 368774 return wrap_timestamp(st, timestamp);
71 }
72
73 11100 static const AVCodec *find_probe_decoder(AVFormatContext *s, const AVStream *st, enum AVCodecID codec_id)
74 {
75 const AVCodec *codec;
76
77 #if CONFIG_H264_DECODER
78 /* Other parts of the code assume this decoder to be used for h264,
79 * so force it if possible. */
80
2/2
✓ Branch 0 taken 390 times.
✓ Branch 1 taken 10710 times.
11100 if (codec_id == AV_CODEC_ID_H264)
81 390 return avcodec_find_decoder_by_name("h264");
82 #endif
83
84 10710 codec = ff_find_decoder(s, st, codec_id);
85
2/2
✓ Branch 0 taken 247 times.
✓ Branch 1 taken 10463 times.
10710 if (!codec)
86 247 return NULL;
87
88
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10463 times.
10463 if (codec->capabilities & AV_CODEC_CAP_AVOID_PROBING) {
89 ✗ const AVCodec *probe_codec = NULL;
90 ✗ void *iter = NULL;
91 ✗ while ((probe_codec = av_codec_iterate(&iter))) {
92 ✗ if (probe_codec->id == codec->id &&
93 ✗ av_codec_is_decoder(probe_codec) &&
94 ✗ !(probe_codec->capabilities & (AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_EXPERIMENTAL))) {
95 ✗ return probe_codec;
96 }
97 }
98 }
99
100 10463 return codec;
101 }
102
103 3424 static int set_codec_from_probe_data(AVFormatContext *s, AVStream *st,
104 AVProbeData *pd)
105 {
106 static const struct {
107 const char *name;
108 enum AVCodecID id;
109 enum AVMediaType type;
110 } fmt_id_type[] = {
111 { "aac", AV_CODEC_ID_AAC, AVMEDIA_TYPE_AUDIO },
112 { "ac3", AV_CODEC_ID_AC3, AVMEDIA_TYPE_AUDIO },
113 { "aptx", AV_CODEC_ID_APTX, AVMEDIA_TYPE_AUDIO },
114 { "av1", AV_CODEC_ID_AV1, AVMEDIA_TYPE_VIDEO },
115 { "dts", AV_CODEC_ID_DTS, AVMEDIA_TYPE_AUDIO },
116 { "dvbsub", AV_CODEC_ID_DVB_SUBTITLE, AVMEDIA_TYPE_SUBTITLE },
117 { "dvbtxt", AV_CODEC_ID_DVB_TELETEXT, AVMEDIA_TYPE_SUBTITLE },
118 { "eac3", AV_CODEC_ID_EAC3, AVMEDIA_TYPE_AUDIO },
119 { "h264", AV_CODEC_ID_H264, AVMEDIA_TYPE_VIDEO },
120 { "hevc", AV_CODEC_ID_HEVC, AVMEDIA_TYPE_VIDEO },
121 { "loas", AV_CODEC_ID_AAC_LATM, AVMEDIA_TYPE_AUDIO },
122 { "m4v", AV_CODEC_ID_MPEG4, AVMEDIA_TYPE_VIDEO },
123 { "mjpeg_2000", AV_CODEC_ID_JPEG2000, AVMEDIA_TYPE_VIDEO },
124 { "mp3", AV_CODEC_ID_MP3, AVMEDIA_TYPE_AUDIO },
125 { "mpegvideo", AV_CODEC_ID_MPEG2VIDEO, AVMEDIA_TYPE_VIDEO },
126 { "truehd", AV_CODEC_ID_TRUEHD, AVMEDIA_TYPE_AUDIO },
127 { "evc", AV_CODEC_ID_EVC, AVMEDIA_TYPE_VIDEO },
128 { "vvc", AV_CODEC_ID_VVC, AVMEDIA_TYPE_VIDEO },
129 { 0 }
130 };
131 int score;
132 3424 const AVInputFormat *fmt = av_probe_input_format3(pd, 1, &score);
133 3424 FFStream *const sti = ffstream(st);
134
135
2/2
✓ Branch 0 taken 1230 times.
✓ Branch 1 taken 2194 times.
3424 if (fmt) {
136 1230 av_log(s, AV_LOG_DEBUG,
137 "Probe with size=%d, packets=%d detected %s with score=%d\n",
138 1230 pd->buf_size, s->max_probe_packets - sti->probe_packets,
139 1230 fmt->name, score);
140
2/2
✓ Branch 0 taken 21935 times.
✓ Branch 1 taken 1198 times.
23133 for (int i = 0; fmt_id_type[i].name; i++) {
141
2/2
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 21897 times.
21935 if (!strcmp(fmt->name, fmt_id_type[i].name)) {
142
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 25 times.
38 if (fmt_id_type[i].type != AVMEDIA_TYPE_AUDIO &&
143
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 st->codecpar->sample_rate)
144 ✗ continue;
145
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 24 times.
38 if (sti->request_probe > score &&
146
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 8 times.
14 st->codecpar->codec_id != fmt_id_type[i].id)
147 6 continue;
148 32 st->codecpar->codec_id = fmt_id_type[i].id;
149 32 st->codecpar->codec_type = fmt_id_type[i].type;
150 32 sti->need_context_update = 1;
151 32 return score;
152 }
153 }
154 }
155 3392 return 0;
156 }
157
158 8500 static int init_input(AVFormatContext *s, const char *filename,
159 AVDictionary **options)
160 {
161 int ret;
162 8500 AVProbeData pd = { filename, NULL, 0 };
163 8500 int score = AVPROBE_SCORE_RETRY;
164
165
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 8465 times.
8500 if (s->pb) {
166 35 s->flags |= AVFMT_FLAG_CUSTOM_IO;
167
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 35 times.
35 if (!s->iformat)
168 ✗ return av_probe_input_buffer2(s->pb, &s->iformat, filename,
169 ✗ s, 0, s->format_probesize);
170
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 35 times.
35 else if (s->iformat->flags & AVFMT_NOFILE)
171 ✗ av_log(s, AV_LOG_WARNING, "Custom AVIOContext makes no sense and "
172 "will be ignored with AVFMT_NOFILE format.\n");
173 35 return 0;
174 }
175
176
4/4
✓ Branch 0 taken 3997 times.
✓ Branch 1 taken 4468 times.
✓ Branch 2 taken 987 times.
✓ Branch 3 taken 3010 times.
8465 if ((s->iformat && s->iformat->flags & AVFMT_NOFILE) ||
177
4/4
✓ Branch 0 taken 4468 times.
✓ Branch 1 taken 987 times.
✓ Branch 3 taken 199 times.
✓ Branch 4 taken 4269 times.
5455 (!s->iformat && (s->iformat = av_probe_input_format2(&pd, 0, &score))))
178 3209 return score;
179
180
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5256 times.
5256 if ((ret = s->io_open(s, &s->pb, filename, AVIO_FLAG_READ | s->avio_flags, options)) < 0)
181 ✗ return ret;
182
183
2/2
✓ Branch 0 taken 987 times.
✓ Branch 1 taken 4269 times.
5256 if (s->iformat)
184 987 return 0;
185 4269 return av_probe_input_buffer2(s->pb, &s->iformat, filename,
186 4269 s, 0, s->format_probesize);
187 }
188
189 static int codec_close(FFStream *sti);
190
191 225790 static int update_stream_avctx(AVFormatContext *s)
192 {
193 int ret;
194
2/2
✓ Branch 0 taken 411708 times.
✓ Branch 1 taken 225790 times.
637498 for (unsigned i = 0; i < s->nb_streams; i++) {
195 411708 AVStream *const st = s->streams[i];
196 411708 FFStream *const sti = ffstream(st);
197
198
2/2
✓ Branch 0 taken 402074 times.
✓ Branch 1 taken 9634 times.
411708 if (!sti->need_context_update)
199 402074 continue;
200
201
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 9632 times.
9634 if (avcodec_is_open(sti->avctx)) {
202 2 av_log(s, AV_LOG_DEBUG, "Demuxer context update while decoder is open, closing and trying to re-open\n");
203 2 ret = codec_close(sti);
204 2 sti->info->found_decoder = 0;
205
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret < 0)
206 ✗ return ret;
207 }
208
209 /* close parser, because it depends on the codec */
210
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 9632 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
9634 if (sti->parser && sti->avctx->codec_id != st->codecpar->codec_id) {
211 ✗ av_parser_close(sti->parser);
212 ✗ sti->parser = NULL;
213 }
214
215 /* update internal codec context, for the parser */
216 9634 ret = avcodec_parameters_to_context(sti->avctx, st->codecpar);
217
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9634 times.
9634 if (ret < 0)
218 ✗ return ret;
219
220 9634 sti->codec_desc = avcodec_descriptor_get(sti->avctx->codec_id);
221
222 9634 sti->need_context_update = 0;
223 }
224 225790 return 0;
225 }
226
227 5291 static av_always_inline int is_id3v2_format(const AVInputFormat *fmt) {
228 5291 return ffifmt(fmt)->flags_internal & FF_INFMT_FLAG_ID3V2_AUTO;
229 }
230
231 8500 int avformat_open_input(AVFormatContext **ps, const char *filename,
232 const AVInputFormat *fmt, AVDictionary **options)
233 {
234 FormatContextInternal *fci;
235 8500 AVFormatContext *s = *ps;
236 FFFormatContext *si;
237 8500 AVDictionary *tmp = NULL;
238 8500 ID3v2ExtraMeta *id3v2_extra_meta = NULL;
239 8500 int ret = 0;
240
241
3/4
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 8469 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 31 times.
8500 if (!s && !(s = avformat_alloc_context()))
242 ✗ return AVERROR(ENOMEM);
243 8500 fci = ff_fc_internal(s);
244 8500 si = &fci->fc;
245
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8500 times.
8500 if (!s->av_class) {
246 ✗ av_log(NULL, AV_LOG_ERROR, "Input context has not been properly allocated by avformat_alloc_context() and is not NULL either\n");
247 ✗ return AVERROR(EINVAL);
248 }
249
2/2
✓ Branch 0 taken 4032 times.
✓ Branch 1 taken 4468 times.
8500 if (fmt)
250 4032 s->iformat = fmt;
251
252
2/2
✓ Branch 0 taken 8472 times.
✓ Branch 1 taken 28 times.
8500 if (options)
253 8472 av_dict_copy(&tmp, *options, 0);
254
255
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 8465 times.
8500 if (s->pb) // must be before any goto fail
256 35 s->flags |= AVFMT_FLAG_CUSTOM_IO;
257
258
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8500 times.
8500 if ((ret = av_opt_set_dict(s, &tmp)) < 0)
259 ✗ goto fail;
260
261
3/4
✓ Branch 0 taken 8490 times.
✓ Branch 1 taken 10 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8500 times.
8500 if (!(s->url = av_strdup(filename ? filename : ""))) {
262 ✗ ret = AVERROR(ENOMEM);
263 ✗ goto fail;
264 }
265
266
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8500 times.
8500 if ((ret = init_input(s, filename, &tmp)) < 0)
267 ✗ goto fail;
268 8500 s->probe_score = ret;
269
270
6/6
✓ Branch 0 taken 8392 times.
✓ Branch 1 taken 108 times.
✓ Branch 2 taken 5183 times.
✓ Branch 3 taken 3209 times.
✓ Branch 4 taken 5172 times.
✓ Branch 5 taken 11 times.
8500 if (!s->protocol_whitelist && s->pb && s->pb->protocol_whitelist) {
271 5172 s->protocol_whitelist = av_strdup(s->pb->protocol_whitelist);
272
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5172 times.
5172 if (!s->protocol_whitelist) {
273 ✗ ret = AVERROR(ENOMEM);
274 ✗ goto fail;
275 }
276 }
277
278
4/6
✓ Branch 0 taken 8500 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5291 times.
✓ Branch 3 taken 3209 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 5291 times.
8500 if (!s->protocol_blacklist && s->pb && s->pb->protocol_blacklist) {
279 ✗ s->protocol_blacklist = av_strdup(s->pb->protocol_blacklist);
280 ✗ if (!s->protocol_blacklist) {
281 ✗ ret = AVERROR(ENOMEM);
282 ✗ goto fail;
283 }
284 }
285
286
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 8500 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
8500 if (s->format_whitelist && av_match_list(s->iformat->name, s->format_whitelist, ',') <= 0) {
287 ✗ av_log(s, AV_LOG_ERROR, "Format not on whitelist \'%s\'\n", s->format_whitelist);
288 ✗ ret = AVERROR(EINVAL);
289 ✗ goto fail;
290 }
291
292 8500 avio_skip(s->pb, s->skip_initial_bytes);
293
294 /* Check filename in case an image number is expected. */
295
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8500 times.
8500 if (s->iformat->flags & AVFMT_NEEDNUMBER) {
296 ✗ if (!av_filename_number_test(filename)) {
297 ✗ ret = AVERROR(EINVAL);
298 ✗ goto fail;
299 }
300 }
301
302 8500 s->duration = s->start_time = AV_NOPTS_VALUE;
303
304 /* Allocate private data. */
305
2/2
✓ Branch 1 taken 8293 times.
✓ Branch 2 taken 207 times.
8500 if (ffifmt(s->iformat)->priv_data_size > 0) {
306
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 8293 times.
8293 if (!(s->priv_data = av_mallocz(ffifmt(s->iformat)->priv_data_size))) {
307 ✗ ret = AVERROR(ENOMEM);
308 ✗ goto fail;
309 }
310
2/2
✓ Branch 0 taken 7274 times.
✓ Branch 1 taken 1019 times.
8293 if (s->iformat->priv_class) {
311 7274 *(const AVClass **) s->priv_data = s->iformat->priv_class;
312 7274 av_opt_set_defaults(s->priv_data);
313
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7274 times.
7274 if ((ret = av_opt_set_dict(s->priv_data, &tmp)) < 0)
314 ✗ goto fail;
315 }
316 }
317
318 /* e.g. AVFMT_NOFILE formats will not have an AVIOContext */
319
4/4
✓ Branch 0 taken 5291 times.
✓ Branch 1 taken 3209 times.
✓ Branch 3 taken 825 times.
✓ Branch 4 taken 4466 times.
8500 if (s->pb && is_id3v2_format(s->iformat))
320 825 ff_id3v2_read_dict(s, s->pb, &si->id3v2_meta, ID3v2_DEFAULT_MAGIC, &id3v2_extra_meta);
321
322
1/2
✓ Branch 1 taken 8500 times.
✗ Branch 2 not taken.
8500 if (ffifmt(s->iformat)->read_header)
323
2/2
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 8496 times.
8500 if ((ret = ffifmt(s->iformat)->read_header(s)) < 0) {
324
2/2
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 1 times.
4 if (ffifmt(s->iformat)->flags_internal & FF_INFMT_FLAG_INIT_CLEANUP)
325 3 goto close;
326 1 goto fail;
327 }
328
329
2/2
✓ Branch 0 taken 6824 times.
✓ Branch 1 taken 1672 times.
8496 if (!s->metadata) {
330 6824 s->metadata = si->id3v2_meta;
331 6824 si->id3v2_meta = NULL;
332
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1672 times.
1672 } else if (si->id3v2_meta) {
333 ✗ av_log(s, AV_LOG_WARNING, "Discarding ID3 tags because more suitable tags were found.\n");
334 ✗ av_dict_free(&si->id3v2_meta);
335 }
336
337
2/2
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 8459 times.
8496 if (id3v2_extra_meta) {
338
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 37 times.
37 if ((ret = ff_id3v2_parse_apic(s, id3v2_extra_meta)) < 0)
339 ✗ goto close;
340
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 37 times.
37 if ((ret = ff_id3v2_parse_chapters(s, id3v2_extra_meta)) < 0)
341 ✗ goto close;
342
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 37 times.
37 if ((ret = ff_id3v2_parse_priv(s, id3v2_extra_meta)) < 0)
343 ✗ goto close;
344 37 ff_id3v2_free_extra_meta(&id3v2_extra_meta);
345 }
346
347
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8496 times.
8496 if ((ret = avformat_queue_attached_pictures(s)) < 0)
348 ✗ goto close;
349
350
4/4
✓ Branch 0 taken 5287 times.
✓ Branch 1 taken 3209 times.
✓ Branch 2 taken 4731 times.
✓ Branch 3 taken 556 times.
8496 if (s->pb && !si->data_offset)
351 4731 si->data_offset = avio_tell(s->pb);
352
353 8496 fci->raw_packet_buffer_size = 0;
354
355 8496 update_stream_avctx(s);
356
357 #if FF_API_STREAM_GROUP_ENTRIES
358 FF_DISABLE_DEPRECATION_WARNINGS
359
2/2
✓ Branch 0 taken 163 times.
✓ Branch 1 taken 8496 times.
8659 for (unsigned i = 0; i < s->nb_stream_groups; i++) {
360 163 AVStreamGroup *const stg = s->stream_groups[i];
361
4/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 159 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 2 times.
163 if (stg->nb_coded_side_data && stg->type == AV_STREAM_GROUP_PARAMS_TILE_GRID) {
362 2 AVStreamGroupTileGrid *tile_grid = stg->params.tile_grid;
363
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
6 for (int j = 0; j < stg->nb_coded_side_data; j++) {
364 4 const AVPacketSideData *src = &stg->coded_side_data[j];
365 4 AVPacketSideData *dst = av_packet_side_data_new(&tile_grid->coded_side_data,
366 &tile_grid->nb_coded_side_data,
367 4 src->type, src->size, 0);
368
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!dst) {
369 ✗ ret = AVERROR(ENOMEM);
370 ✗ goto close;
371 }
372
373 4 memcpy(dst->data, src->data, src->size);
374 }
375 }
376 }
377 FF_ENABLE_DEPRECATION_WARNINGS
378 #endif
379
380
2/2
✓ Branch 0 taken 8468 times.
✓ Branch 1 taken 28 times.
8496 if (options) {
381 8468 av_dict_free(options);
382 8468 *options = tmp;
383 }
384 8496 *ps = s;
385 8496 return 0;
386
387 3 close:
388
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (ffifmt(s->iformat)->read_close)
389 3 ffifmt(s->iformat)->read_close(s);
390 ✗ fail:
391 4 ff_id3v2_free_extra_meta(&id3v2_extra_meta);
392 4 av_dict_free(&tmp);
393
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 if (s->pb && !(s->flags & AVFMT_FLAG_CUSTOM_IO))
394 4 ff_format_io_close(s, &s->pb);
395 4 avformat_free_context(s);
396 4 *ps = NULL;
397 4 return ret;
398 }
399
400 8500 void avformat_close_input(AVFormatContext **ps)
401 {
402 AVFormatContext *s;
403 AVIOContext *pb;
404
405
3/4
✓ Branch 0 taken 8500 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 8496 times.
8500 if (!ps || !*ps)
406 4 return;
407
408 8496 s = *ps;
409 8496 pb = s->pb;
410
411
6/6
✓ Branch 0 taken 8494 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 5344 times.
✓ Branch 3 taken 3150 times.
✓ Branch 4 taken 5258 times.
✓ Branch 5 taken 86 times.
8496 if ((s->iformat && strcmp(s->iformat->name, "image2") && s->iformat->flags & AVFMT_NOFILE) ||
412
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 8377 times.
8410 (s->flags & AVFMT_FLAG_CUSTOM_IO))
413 119 pb = NULL;
414
415
2/2
✓ Branch 0 taken 8494 times.
✓ Branch 1 taken 2 times.
8496 if (s->iformat)
416
2/2
✓ Branch 1 taken 5486 times.
✓ Branch 2 taken 3008 times.
8494 if (ffifmt(s->iformat)->read_close)
417 5486 ffifmt(s->iformat)->read_close(s);
418
419 8496 ff_format_io_close(s, &pb);
420 8496 avformat_free_context(s);
421
422 8496 *ps = NULL;
423 }
424
425 1158004 static void force_codec_ids(AVFormatContext *s, AVStream *st)
426 {
427
5/5
✓ Branch 0 taken 758896 times.
✓ Branch 1 taken 395427 times.
✓ Branch 2 taken 3465 times.
✓ Branch 3 taken 214 times.
✓ Branch 4 taken 2 times.
1158004 switch (st->codecpar->codec_type) {
428 758896 case AVMEDIA_TYPE_VIDEO:
429
2/2
✓ Branch 0 taken 118024 times.
✓ Branch 1 taken 640872 times.
758896 if (s->video_codec_id)
430 118024 st->codecpar->codec_id = s->video_codec_id;
431 758896 break;
432 395427 case AVMEDIA_TYPE_AUDIO:
433
2/2
✓ Branch 0 taken 4620 times.
✓ Branch 1 taken 390807 times.
395427 if (s->audio_codec_id)
434 4620 st->codecpar->codec_id = s->audio_codec_id;
435 395427 break;
436 3465 case AVMEDIA_TYPE_SUBTITLE:
437
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3465 times.
3465 if (s->subtitle_codec_id)
438 ✗ st->codecpar->codec_id = s->subtitle_codec_id;
439 3465 break;
440 214 case AVMEDIA_TYPE_DATA:
441
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 214 times.
214 if (s->data_codec_id)
442 ✗ st->codecpar->codec_id = s->data_codec_id;
443 214 break;
444 }
445 1158004 }
446
447 26156 static int probe_codec(AVFormatContext *s, AVStream *st, const AVPacket *pkt)
448 {
449 26156 FormatContextInternal *const fci = ff_fc_internal(s);
450 26156 FFStream *const sti = ffstream(st);
451
452
2/2
✓ Branch 0 taken 17890 times.
✓ Branch 1 taken 8266 times.
26156 if (sti->request_probe > 0) {
453 17890 AVProbeData *const pd = &sti->probe_data;
454 int end;
455 17890 av_log(s, AV_LOG_DEBUG, "probing stream %d pp:%d\n", st->index, sti->probe_packets);
456 17890 --sti->probe_packets;
457
458
2/2
✓ Branch 0 taken 17873 times.
✓ Branch 1 taken 17 times.
17890 if (pkt) {
459 17873 uint8_t *new_buf = av_realloc(pd->buf, pd->buf_size+pkt->size+AVPROBE_PADDING_SIZE);
460
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17873 times.
17873 if (!new_buf) {
461 ✗ av_log(s, AV_LOG_WARNING,
462 "Failed to reallocate probe buffer for stream %d\n",
463 st->index);
464 ✗ goto no_packet;
465 }
466 17873 pd->buf = new_buf;
467 17873 memcpy(pd->buf + pd->buf_size, pkt->data, pkt->size);
468 17873 pd->buf_size += pkt->size;
469 17873 memset(pd->buf + pd->buf_size, 0, AVPROBE_PADDING_SIZE);
470 } else {
471 17 no_packet:
472 17 sti->probe_packets = 0;
473
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
17 if (!pd->buf_size) {
474 ✗ av_log(s, AV_LOG_WARNING,
475 "nothing to probe for stream %d\n", st->index);
476 }
477 }
478
479
1/2
✓ Branch 0 taken 17890 times.
✗ Branch 1 not taken.
35780 end = fci->raw_packet_buffer_size >= s->probesize ||
480
2/2
✓ Branch 0 taken 561 times.
✓ Branch 1 taken 17329 times.
17890 sti->probe_packets <= 0;
481
482
4/4
✓ Branch 0 taken 17329 times.
✓ Branch 1 taken 561 times.
✓ Branch 2 taken 2863 times.
✓ Branch 3 taken 14466 times.
17890 if (end || av_log2(pd->buf_size) != av_log2(pd->buf_size - pkt->size)) {
483 3424 int score = set_codec_from_probe_data(s, st, pd);
484
4/4
✓ Branch 0 taken 3416 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 3391 times.
✓ Branch 3 taken 25 times.
3424 if ( (st->codecpar->codec_id != AV_CODEC_ID_NONE && score > AVPROBE_SCORE_STREAM_RETRY)
485
2/2
✓ Branch 0 taken 561 times.
✓ Branch 1 taken 2838 times.
3399 || end) {
486 586 pd->buf_size = 0;
487 586 av_freep(&pd->buf);
488 586 sti->request_probe = -1;
489
1/2
✓ Branch 0 taken 586 times.
✗ Branch 1 not taken.
586 if (st->codecpar->codec_id != AV_CODEC_ID_NONE) {
490 586 av_log(s, AV_LOG_DEBUG, "probed stream %d\n", st->index);
491 } else
492 ✗ av_log(s, AV_LOG_WARNING, "probed stream %d failed\n", st->index);
493 }
494 3424 force_codec_ids(s, st);
495 }
496 }
497 26156 return 0;
498 }
499
500 1154580 static int update_wrap_reference(AVFormatContext *s, AVStream *st, int stream_index, AVPacket *pkt)
501 {
502 1154580 FFStream *const sti = ffstream(st);
503 1154580 int64_t ref = pkt->dts;
504 int pts_wrap_behavior;
505 int64_t pts_wrap_reference;
506 AVProgram *first_program;
507
508
2/2
✓ Branch 0 taken 933700 times.
✓ Branch 1 taken 220880 times.
1154580 if (ref == AV_NOPTS_VALUE)
509 933700 ref = pkt->pts;
510
7/8
✓ Branch 0 taken 1074343 times.
✓ Branch 1 taken 80237 times.
✓ Branch 2 taken 58942 times.
✓ Branch 3 taken 1015401 times.
✓ Branch 4 taken 1025 times.
✓ Branch 5 taken 57917 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 1025 times.
1154580 if (sti->pts_wrap_reference != AV_NOPTS_VALUE || st->pts_wrap_bits >= 63 || ref == AV_NOPTS_VALUE || !s->correct_ts_overflow)
511 1153555 return 0;
512 1025 ref &= (1LL << st->pts_wrap_bits)-1;
513
514 // reference time stamp should be 60 s before first time stamp
515 1025 pts_wrap_reference = ref - av_rescale(60, st->time_base.den, st->time_base.num);
516 // if first time stamp is not more than 1/8 and 60s before the wrap point, subtract rather than add wrap offset
517 2051 pts_wrap_behavior = (ref < (1LL << st->pts_wrap_bits) - (1LL << st->pts_wrap_bits-3)) ||
518
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)) ?
519
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1024 times.
1026 AV_PTS_WRAP_ADD_OFFSET : AV_PTS_WRAP_SUB_OFFSET;
520
521 1025 first_program = av_find_program_from_stream(s, NULL, stream_index);
522
523
2/2
✓ Branch 0 taken 232 times.
✓ Branch 1 taken 793 times.
1025 if (!first_program) {
524 232 int default_stream_index = av_find_default_stream_index(s);
525 232 FFStream *const default_sti = ffstream(s->streams[default_stream_index]);
526
2/2
✓ Branch 0 taken 221 times.
✓ Branch 1 taken 11 times.
232 if (default_sti->pts_wrap_reference == AV_NOPTS_VALUE) {
527
2/2
✓ Branch 0 taken 481 times.
✓ Branch 1 taken 221 times.
702 for (unsigned i = 0; i < s->nb_streams; i++) {
528 481 FFStream *const sti = ffstream(s->streams[i]);
529
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 481 times.
481 if (av_find_program_from_stream(s, NULL, i))
530 ✗ continue;
531 481 sti->pts_wrap_reference = pts_wrap_reference;
532 481 sti->pts_wrap_behavior = pts_wrap_behavior;
533 }
534 } else {
535 11 sti->pts_wrap_reference = default_sti->pts_wrap_reference;
536 11 sti->pts_wrap_behavior = default_sti->pts_wrap_behavior;
537 }
538 } else {
539 793 AVProgram *program = first_program;
540
2/2
✓ Branch 0 taken 794 times.
✓ Branch 1 taken 109 times.
903 while (program) {
541
2/2
✓ Branch 0 taken 684 times.
✓ Branch 1 taken 110 times.
794 if (program->pts_wrap_reference != AV_NOPTS_VALUE) {
542 684 pts_wrap_reference = program->pts_wrap_reference;
543 684 pts_wrap_behavior = program->pts_wrap_behavior;
544 684 break;
545 }
546 110 program = av_find_program_from_stream(s, program, stream_index);
547 }
548
549 // update every program with differing pts_wrap_reference
550 793 program = first_program;
551
2/2
✓ Branch 0 taken 794 times.
✓ Branch 1 taken 793 times.
1587 while (program) {
552
2/2
✓ Branch 0 taken 110 times.
✓ Branch 1 taken 684 times.
794 if (program->pts_wrap_reference != pts_wrap_reference) {
553
2/2
✓ Branch 0 taken 170 times.
✓ Branch 1 taken 110 times.
280 for (unsigned i = 0; i < program->nb_stream_indexes; i++) {
554 170 FFStream *const sti = ffstream(s->streams[program->stream_index[i]]);
555 170 sti->pts_wrap_reference = pts_wrap_reference;
556 170 sti->pts_wrap_behavior = pts_wrap_behavior;
557 }
558
559 110 program->pts_wrap_reference = pts_wrap_reference;
560 110 program->pts_wrap_behavior = pts_wrap_behavior;
561 }
562 794 program = av_find_program_from_stream(s, program, stream_index);
563 }
564 }
565 1025 return 1;
566 }
567
568 1154580 static void update_timestamps(AVFormatContext *s, AVStream *st, AVPacket *pkt)
569 {
570 1154580 FFStream *const sti = ffstream(st);
571
572
4/4
✓ Branch 1 taken 1025 times.
✓ Branch 2 taken 1153555 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 1024 times.
1154580 if (update_wrap_reference(s, st, pkt->stream_index, pkt) && sti->pts_wrap_behavior == AV_PTS_WRAP_SUB_OFFSET) {
573 // correct first time stamps to negative values
574
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 if (!is_relative(sti->first_dts))
575 1 sti->first_dts = wrap_timestamp(st, sti->first_dts);
576
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 if (!is_relative(st->start_time))
577 1 st->start_time = wrap_timestamp(st, st->start_time);
578
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (!is_relative(sti->cur_dts))
579 ✗ sti->cur_dts = wrap_timestamp(st, sti->cur_dts);
580 }
581
582 1154580 pkt->dts = wrap_timestamp(st, pkt->dts);
583 1154580 pkt->pts = wrap_timestamp(st, pkt->pts);
584
585 1154580 force_codec_ids(s, st);
586
587 /* TODO: audio: time filter; video: frame reordering (pts != dts) */
588
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1154580 times.
1154580 if (s->use_wallclock_as_timestamps)
589 ✗ pkt->dts = pkt->pts = av_rescale_q(av_gettime(), AV_TIME_BASE_Q, st->time_base);
590 1154580 }
591
592 /**
593 * Handle a new packet and either return it directly if possible and
594 * allow_passthrough is true or queue the packet (or drop the packet
595 * if corrupt).
596 *
597 * @return < 0 on error, 0 if the packet was passed through,
598 * 1 if it was queued or dropped
599 */
600 1154580 static int handle_new_packet(AVFormatContext *s, AVPacket *pkt, int allow_passthrough)
601 {
602 1154580 FormatContextInternal *const fci = ff_fc_internal(s);
603 AVStream *st;
604 FFStream *sti;
605 int err;
606
607
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1154580 times.
1154580 av_assert0(pkt->stream_index < (unsigned)s->nb_streams &&
608 "Invalid stream index.\n");
609
610
2/2
✓ Branch 0 taken 213 times.
✓ Branch 1 taken 1154367 times.
1154580 if (pkt->flags & AV_PKT_FLAG_CORRUPT) {
611 426 av_log(s, AV_LOG_WARNING,
612 "Packet corrupt (stream = %d, dts = %s)%s.\n",
613 213 pkt->stream_index, av_ts2str(pkt->dts),
614
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 213 times.
213 s->flags & AVFMT_FLAG_DISCARD_CORRUPT ? ", dropping it" : "");
615
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 213 times.
213 if (s->flags & AVFMT_FLAG_DISCARD_CORRUPT) {
616 ✗ av_packet_unref(pkt);
617 ✗ return 1;
618 }
619 }
620
621 1154580 st = s->streams[pkt->stream_index];
622 1154580 sti = ffstream(st);
623
624 1154580 update_timestamps(s, st, pkt);
625
626
6/6
✓ Branch 0 taken 1136707 times.
✓ Branch 1 taken 17873 times.
✓ Branch 2 taken 1128483 times.
✓ Branch 3 taken 8224 times.
✓ Branch 4 taken 1128441 times.
✓ Branch 5 taken 42 times.
1154580 if (sti->request_probe <= 0 && allow_passthrough && !fci->raw_packet_buffer.head)
627 1128441 return 0;
628
629 26139 err = ff_packet_list_put(&fci->raw_packet_buffer, pkt, NULL, 0);
630
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26139 times.
26139 if (err < 0) {
631 ✗ av_packet_unref(pkt);
632 ✗ return err;
633 }
634
635 26139 pkt = &fci->raw_packet_buffer.tail->pkt;
636 26139 fci->raw_packet_buffer_size += pkt->size;
637
638 26139 err = probe_codec(s, st, pkt);
639
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26139 times.
26139 if (err < 0)
640 ✗ return err;
641
642 26139 return 1;
643 }
644
645 8224 int ff_buffer_packet(AVFormatContext *s, AVPacket *pkt)
646 {
647 8224 int err = handle_new_packet(s, pkt, 0);
648
649 8224 return err < 0 ? err : 0;
650 }
651
652 1162048 int ff_read_packet(AVFormatContext *s, AVPacket *pkt)
653 {
654 1162048 FormatContextInternal *const fci = ff_fc_internal(s);
655 int err;
656
657 #if FF_API_INIT_PACKET
658 FF_DISABLE_DEPRECATION_WARNINGS
659 1162048 pkt->data = NULL;
660 1162048 pkt->size = 0;
661 1162048 av_init_packet(pkt);
662 FF_ENABLE_DEPRECATION_WARNINGS
663 #else
664 av_packet_unref(pkt);
665 #endif
666
667 26476 for (;;) {
668 1188524 PacketListEntry *pktl = fci->raw_packet_buffer.head;
669
670
2/2
✓ Branch 0 taken 43541 times.
✓ Branch 1 taken 1144983 times.
1188524 if (pktl) {
671 43541 AVStream *const st = s->streams[pktl->pkt.stream_index];
672
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 43541 times.
43541 if (fci->raw_packet_buffer_size >= s->probesize)
673 ✗ if ((err = probe_codec(s, st, NULL)) < 0)
674 ✗ return err;
675
2/2
✓ Branch 1 taken 26195 times.
✓ Branch 2 taken 17346 times.
43541 if (ffstream(st)->request_probe <= 0) {
676 26195 ff_packet_list_get(&fci->raw_packet_buffer, pkt);
677 26195 fci->raw_packet_buffer_size -= pkt->size;
678 26195 return 0;
679 }
680 }
681
682 1162329 err = ffifmt(s->iformat)->read_packet(s, pkt);
683
2/2
✓ Branch 0 taken 15973 times.
✓ Branch 1 taken 1146356 times.
1162329 if (err < 0) {
684 15973 av_packet_unref(pkt);
685
686 /* Some demuxers return FFERROR_REDO when they consume
687 data and discard it (ignored streams, junk, extradata).
688 We must re-call the demuxer to get the real packet. */
689
2/2
✓ Branch 0 taken 8544 times.
✓ Branch 1 taken 7429 times.
15973 if (err == FFERROR_REDO)
690 8544 continue;
691
3/4
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 7412 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 17 times.
7429 if (!pktl || err == AVERROR(EAGAIN))
692 7412 return err;
693
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 17 times.
34 for (unsigned i = 0; i < s->nb_streams; i++) {
694 17 AVStream *const st = s->streams[i];
695 17 FFStream *const sti = ffstream(st);
696
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
17 if (sti->probe_packets || sti->request_probe > 0)
697
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 17 times.
17 if ((err = probe_codec(s, st, NULL)) < 0)
698 ✗ return err;
699
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
17 av_assert0(sti->request_probe <= 0);
700 }
701 17 continue;
702 }
703
704 1146356 err = av_packet_make_refcounted(pkt);
705
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1146356 times.
1146356 if (err < 0) {
706 ✗ av_packet_unref(pkt);
707 ✗ return err;
708 }
709
710 1146356 err = handle_new_packet(s, pkt, 1);
711
2/2
✓ Branch 0 taken 1128441 times.
✓ Branch 1 taken 17915 times.
1146356 if (err <= 0) /* Error or passthrough */
712 1128441 return err;
713 }
714 }
715
716 /**
717 * Return the frame duration in seconds. Return 0 if not available.
718 */
719 344838 static void compute_frame_duration(AVFormatContext *s, int *pnum, int *pden,
720 AVStream *st, AVCodecParserContext *pc,
721 AVPacket *pkt)
722 {
723 344838 FFStream *const sti = ffstream(st);
724 344838 AVRational codec_framerate = sti->avctx->framerate;
725 int frame_size, sample_rate;
726
727 344838 *pnum = 0;
728 344838 *pden = 0;
729
3/3
✓ Branch 0 taken 229641 times.
✓ Branch 1 taken 113598 times.
✓ Branch 2 taken 1599 times.
344838 switch (st->codecpar->codec_type) {
730 229641 case AVMEDIA_TYPE_VIDEO:
731
6/6
✓ Branch 0 taken 197256 times.
✓ Branch 1 taken 32385 times.
✓ Branch 2 taken 44521 times.
✓ Branch 3 taken 152735 times.
✓ Branch 4 taken 31434 times.
✓ Branch 5 taken 13087 times.
229641 if (st->r_frame_rate.num && (!pc || !codec_framerate.num)) {
732 184169 *pnum = st->r_frame_rate.den;
733 184169 *pden = st->r_frame_rate.num;
734
2/2
✓ Branch 0 taken 24872 times.
✓ Branch 1 taken 20600 times.
45472 } else if ((s->iformat->flags & AVFMT_NOTIMESTAMPS) &&
735
2/2
✓ Branch 0 taken 15381 times.
✓ Branch 1 taken 9491 times.
24872 !codec_framerate.num &&
736
3/4
✓ Branch 0 taken 15379 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 15379 times.
✗ Branch 3 not taken.
15381 st->avg_frame_rate.num && st->avg_frame_rate.den) {
737 15379 *pnum = st->avg_frame_rate.den;
738 15379 *pden = st->avg_frame_rate.num;
739
2/2
✓ Branch 0 taken 9399 times.
✓ Branch 1 taken 20694 times.
30093 } else if (st->time_base.num * 1000LL > st->time_base.den) {
740 9399 *pnum = st->time_base.num;
741 9399 *pden = st->time_base.den;
742
2/2
✓ Branch 0 taken 20669 times.
✓ Branch 1 taken 25 times.
20694 } else if (codec_framerate.den * 1000LL > codec_framerate.num) {
743 62007 int ticks_per_frame = (sti->codec_desc &&
744
3/4
✓ Branch 0 taken 20669 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14372 times.
✓ Branch 3 taken 6297 times.
20669 (sti->codec_desc->props & AV_CODEC_PROP_FIELDS)) ? 2 : 1;
745 20669 av_reduce(pnum, pden,
746 20669 codec_framerate.den,
747 20669 codec_framerate.num * (int64_t)ticks_per_frame,
748 INT_MAX);
749
750
4/4
✓ Branch 0 taken 18631 times.
✓ Branch 1 taken 2038 times.
✓ Branch 2 taken 13742 times.
✓ Branch 3 taken 4889 times.
20669 if (pc && pc->repeat_pict) {
751 13742 av_reduce(pnum, pden,
752 13742 (*pnum) * (1LL + pc->repeat_pict),
753 13742 (*pden),
754 INT_MAX);
755 }
756 /* If this codec can be interlaced or progressive then we need
757 * a parser to compute duration of a packet. Thus if we have
758 * no parser in such case leave duration undefined. */
759
1/2
✓ Branch 0 taken 20669 times.
✗ Branch 1 not taken.
20669 if (sti->codec_desc &&
760
3/4
✓ Branch 0 taken 14372 times.
✓ Branch 1 taken 6297 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 14372 times.
20669 (sti->codec_desc->props & AV_CODEC_PROP_FIELDS) && !pc)
761 ✗ *pnum = *pden = 0;
762 }
763 229641 break;
764 113598 case AVMEDIA_TYPE_AUDIO:
765
2/2
✓ Branch 0 taken 50280 times.
✓ Branch 1 taken 63318 times.
113598 if (sti->avctx_inited) {
766 50280 frame_size = av_get_audio_frame_duration(sti->avctx, pkt->size);
767 50280 sample_rate = sti->avctx->sample_rate;
768 } else {
769 63318 frame_size = av_get_audio_frame_duration2(st->codecpar, pkt->size);
770 63318 sample_rate = st->codecpar->sample_rate;
771 }
772
4/4
✓ Branch 0 taken 107147 times.
✓ Branch 1 taken 6451 times.
✓ Branch 2 taken 107142 times.
✓ Branch 3 taken 5 times.
113598 if (frame_size <= 0 || sample_rate <= 0)
773 break;
774 107142 *pnum = frame_size;
775 107142 *pden = sample_rate;
776 107142 break;
777 1599 default:
778 1599 break;
779 }
780 344838 }
781
782 831423 static int has_decode_delay_been_guessed(AVStream *st)
783 {
784 831423 FFStream *const sti = ffstream(st);
785
2/2
✓ Branch 0 taken 811953 times.
✓ Branch 1 taken 19470 times.
831423 if (st->codecpar->codec_id != AV_CODEC_ID_H264) return 1;
786
2/2
✓ Branch 0 taken 7265 times.
✓ Branch 1 taken 12205 times.
19470 if (!sti->info) // if we have left find_stream_info then nb_decoded_frames won't increase anymore for stream copy
787 7265 return 1;
788
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 12205 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
12205 av_assert0(sti->avctx->codec_id == AV_CODEC_ID_H264 || (sti->avctx->codec_id == AV_CODEC_ID_NONE && !avcodec_is_open(sti->avctx)));
789 #if CONFIG_H264_DECODER
790
4/4
✓ Branch 0 taken 7709 times.
✓ Branch 1 taken 4496 times.
✓ Branch 3 taken 7584 times.
✓ Branch 4 taken 125 times.
12205 if (sti->avctx->has_b_frames && avcodec_is_open(sti->avctx) &&
791
2/2
✓ Branch 1 taken 2272 times.
✓ Branch 2 taken 5312 times.
7584 avpriv_h264_has_num_reorder_frames(sti->avctx) == sti->avctx->has_b_frames)
792 2272 return 1;
793 #endif
794
2/2
✓ Branch 0 taken 9743 times.
✓ Branch 1 taken 190 times.
9933 if (sti->avctx->has_b_frames < 3)
795 9743 return sti->nb_decoded_frames >= 7;
796
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 154 times.
190 else if (sti->avctx->has_b_frames < 4)
797 36 return sti->nb_decoded_frames >= 18;
798 else
799 154 return sti->nb_decoded_frames >= 20;
800 }
801
802 384222 static PacketListEntry *get_next_pkt(AVFormatContext *s, AVStream *st,
803 PacketListEntry *pktl)
804 {
805 384222 FormatContextInternal *const fci = ff_fc_internal(s);
806 384222 FFFormatContext *const si = &fci->fc;
807
2/2
✓ Branch 0 taken 381498 times.
✓ Branch 1 taken 2724 times.
384222 if (pktl->next)
808 381498 return pktl->next;
809
2/2
✓ Branch 0 taken 2690 times.
✓ Branch 1 taken 34 times.
2724 if (pktl == si->packet_buffer.tail)
810 2690 return fci->parse_queue.head;
811 34 return NULL;
812 }
813
814 615262 static int64_t select_from_pts_buffer(AVStream *st, int64_t *pts_buffer, int64_t dts)
815 {
816 615262 FFStream *const sti = ffstream(st);
817 1836862 int onein_oneout = st->codecpar->codec_id != AV_CODEC_ID_H264 &&
818
4/4
✓ Branch 0 taken 606338 times.
✓ Branch 1 taken 8924 times.
✓ Branch 2 taken 604608 times.
✓ Branch 3 taken 1730 times.
1219870 st->codecpar->codec_id != AV_CODEC_ID_HEVC &&
819
2/2
✓ Branch 0 taken 604392 times.
✓ Branch 1 taken 216 times.
604608 st->codecpar->codec_id != AV_CODEC_ID_VVC;
820
821
2/2
✓ Branch 0 taken 10870 times.
✓ Branch 1 taken 604392 times.
615262 if (!onein_oneout) {
822 10870 int delay = sti->avctx->has_b_frames;
823
824
2/2
✓ Branch 0 taken 1434 times.
✓ Branch 1 taken 9436 times.
10870 if (dts == AV_NOPTS_VALUE) {
825 1434 int64_t best_score = INT64_MAX;
826
2/2
✓ Branch 0 taken 1733 times.
✓ Branch 1 taken 1434 times.
3167 for (int i = 0; i < delay; i++) {
827
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1733 times.
1733 if (sti->pts_reorder_error_count[i]) {
828 ✗ int64_t score = sti->pts_reorder_error[i] / sti->pts_reorder_error_count[i];
829 ✗ if (score < best_score) {
830 ✗ best_score = score;
831 ✗ dts = pts_buffer[i];
832 }
833 }
834 }
835 } else {
836
2/2
✓ Branch 0 taken 15974 times.
✓ Branch 1 taken 9436 times.
25410 for (int i = 0; i < delay; i++) {
837
2/2
✓ Branch 0 taken 14890 times.
✓ Branch 1 taken 1084 times.
15974 if (pts_buffer[i] != AV_NOPTS_VALUE) {
838 #define ABSDIFF(a,b) (((a) < (b)) ? (b) - (uint64_t)(a) : ((a) - (uint64_t)(b)))
839
2/2
✓ Branch 0 taken 4215 times.
✓ Branch 1 taken 10675 times.
14890 uint64_t diff = ABSDIFF(pts_buffer[i], dts);
840
841
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14890 times.
14890 if (diff > INT64_MAX - sti->pts_reorder_error[i]) {
842 ✗ diff = INT64_MAX;
843 } else
844 14890 diff += sti->pts_reorder_error[i];
845
846 14890 sti->pts_reorder_error[i] = diff;
847 14890 sti->pts_reorder_error_count[i]++;
848
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 14883 times.
14890 if (sti->pts_reorder_error_count[i] > 250) {
849 7 sti->pts_reorder_error[i] >>= 1;
850 7 sti->pts_reorder_error_count[i] >>= 1;
851 }
852 }
853 }
854 }
855 }
856
857
2/2
✓ Branch 0 taken 1809 times.
✓ Branch 1 taken 613453 times.
615262 if (dts == AV_NOPTS_VALUE)
858 1809 dts = pts_buffer[0];
859
860 615262 return dts;
861 }
862
863 /**
864 * Updates the dts of packets of a stream in pkt_buffer, by re-ordering the pts
865 * of the packets in a window.
866 */
867 6967 static void update_dts_from_pts(AVFormatContext *s, int stream_index,
868 PacketListEntry *pkt_buffer)
869 {
870 6967 AVStream *const st = s->streams[stream_index];
871 6967 int delay = ffstream(st)->avctx->has_b_frames;
872
873 int64_t pts_buffer[MAX_REORDER_DELAY+1];
874
875
2/2
✓ Branch 0 taken 118439 times.
✓ Branch 1 taken 6967 times.
125406 for (int i = 0; i < MAX_REORDER_DELAY + 1; i++)
876 118439 pts_buffer[i] = AV_NOPTS_VALUE;
877
878
2/2
✓ Branch 1 taken 75911 times.
✓ Branch 2 taken 6967 times.
82878 for (; pkt_buffer; pkt_buffer = get_next_pkt(s, st, pkt_buffer)) {
879
2/2
✓ Branch 0 taken 75178 times.
✓ Branch 1 taken 733 times.
75911 if (pkt_buffer->pkt.stream_index != stream_index)
880 75178 continue;
881
882
3/4
✓ Branch 0 taken 635 times.
✓ Branch 1 taken 98 times.
✓ Branch 2 taken 635 times.
✗ Branch 3 not taken.
733 if (pkt_buffer->pkt.pts != AV_NOPTS_VALUE && delay <= MAX_REORDER_DELAY) {
883 635 pts_buffer[0] = pkt_buffer->pkt.pts;
884
4/4
✓ Branch 0 taken 451 times.
✓ Branch 1 taken 458 times.
✓ Branch 2 taken 274 times.
✓ Branch 3 taken 177 times.
909 for (int i = 0; i < delay && pts_buffer[i] > pts_buffer[i + 1]; i++)
885 274 FFSWAP(int64_t, pts_buffer[i], pts_buffer[i + 1]);
886
887 635 pkt_buffer->pkt.dts = select_from_pts_buffer(st, pts_buffer, pkt_buffer->pkt.dts);
888 }
889 }
890 6967 }
891
892 657775 static void update_initial_timestamps(AVFormatContext *s, int stream_index,
893 int64_t dts, int64_t pts, AVPacket *pkt)
894 {
895 657775 FormatContextInternal *const fci = ff_fc_internal(s);
896 657775 FFFormatContext *const si = &fci->fc;
897 657775 AVStream *const st = s->streams[stream_index];
898 657775 FFStream *const sti = ffstream(st);
899
2/2
✓ Branch 0 taken 197669 times.
✓ Branch 1 taken 460106 times.
657775 PacketListEntry *pktl = si->packet_buffer.head ? si->packet_buffer.head : fci->parse_queue.head;
900
901 uint64_t shift;
902
903
4/4
✓ Branch 0 taken 236671 times.
✓ Branch 1 taken 421104 times.
✓ Branch 2 taken 7066 times.
✓ Branch 3 taken 229605 times.
657775 if (sti->first_dts != AV_NOPTS_VALUE ||
904 7066 dts == AV_NOPTS_VALUE ||
905
1/2
✓ Branch 0 taken 7066 times.
✗ Branch 1 not taken.
7066 sti->cur_dts == AV_NOPTS_VALUE ||
906
1/2
✓ Branch 0 taken 7066 times.
✗ Branch 1 not taken.
7066 sti->cur_dts < INT_MIN + RELATIVE_TS_BASE ||
907
2/4
✓ Branch 0 taken 7066 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 7066 times.
14132 dts < INT_MIN + (sti->cur_dts - RELATIVE_TS_BASE) ||
908 7066 is_relative(dts))
909 650709 return;
910
911 7066 sti->first_dts = dts - (sti->cur_dts - RELATIVE_TS_BASE);
912 7066 sti->cur_dts = dts;
913 7066 shift = (uint64_t)sti->first_dts - RELATIVE_TS_BASE;
914
915
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7066 times.
7066 if (is_relative(pts))
916 ✗ pts += shift;
917
918
2/2
✓ Branch 1 taken 75727 times.
✓ Branch 2 taken 7066 times.
82793 for (PacketListEntry *pktl_it = pktl; pktl_it; pktl_it = get_next_pkt(s, st, pktl_it)) {
919
2/2
✓ Branch 0 taken 75197 times.
✓ Branch 1 taken 530 times.
75727 if (pktl_it->pkt.stream_index != stream_index)
920 75197 continue;
921
2/2
✓ Branch 1 taken 211 times.
✓ Branch 2 taken 319 times.
530 if (is_relative(pktl_it->pkt.pts))
922 211 pktl_it->pkt.pts += shift;
923
924
2/2
✓ Branch 1 taken 239 times.
✓ Branch 2 taken 291 times.
530 if (is_relative(pktl_it->pkt.dts))
925 239 pktl_it->pkt.dts += shift;
926
927
4/4
✓ Branch 0 taken 88 times.
✓ Branch 1 taken 442 times.
✓ Branch 2 taken 59 times.
✓ Branch 3 taken 29 times.
530 if (st->start_time == AV_NOPTS_VALUE && pktl_it->pkt.pts != AV_NOPTS_VALUE) {
928 59 st->start_time = pktl_it->pkt.pts;
929
3/4
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 54 times.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
59 if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && st->codecpar->sample_rate)
930 5 st->start_time = av_sat_add64(st->start_time, av_rescale_q(sti->skip_samples, (AVRational){1, st->codecpar->sample_rate}, st->time_base));
931 }
932 }
933
934
2/2
✓ Branch 1 taken 6946 times.
✓ Branch 2 taken 120 times.
7066 if (has_decode_delay_been_guessed(st))
935 6946 update_dts_from_pts(s, stream_index, pktl);
936
937
2/2
✓ Branch 0 taken 2052 times.
✓ Branch 1 taken 5014 times.
7066 if (st->start_time == AV_NOPTS_VALUE) {
938
3/4
✓ Branch 0 taken 1368 times.
✓ Branch 1 taken 684 times.
✓ Branch 2 taken 1368 times.
✗ Branch 3 not taken.
2052 if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO || !(pkt->flags & AV_PKT_FLAG_DISCARD)) {
939 2052 st->start_time = pts;
940 }
941
4/4
✓ Branch 0 taken 684 times.
✓ Branch 1 taken 1368 times.
✓ Branch 2 taken 581 times.
✓ Branch 3 taken 103 times.
2052 if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && st->codecpar->sample_rate)
942 581 st->start_time = av_sat_add64(st->start_time, av_rescale_q(sti->skip_samples, (AVRational){1, st->codecpar->sample_rate}, st->time_base));
943 }
944 }
945
946 284117 static void update_initial_durations(AVFormatContext *s, AVStream *st,
947 int stream_index, int64_t duration)
948 {
949 284117 FormatContextInternal *const fci = ff_fc_internal(s);
950 284117 FFFormatContext *const si = &fci->fc;
951 284117 FFStream *const sti = ffstream(st);
952
2/2
✓ Branch 0 taken 194103 times.
✓ Branch 1 taken 90014 times.
284117 PacketListEntry *pktl = si->packet_buffer.head ? si->packet_buffer.head : fci->parse_queue.head;
953 284117 int64_t cur_dts = RELATIVE_TS_BASE;
954
955
2/2
✓ Branch 0 taken 139129 times.
✓ Branch 1 taken 144988 times.
284117 if (sti->first_dts != AV_NOPTS_VALUE) {
956
2/2
✓ Branch 0 taken 135188 times.
✓ Branch 1 taken 3941 times.
139129 if (sti->update_initial_durations_done)
957 135188 return;
958 3941 sti->update_initial_durations_done = 1;
959 3941 cur_dts = sti->first_dts;
960
2/2
✓ Branch 1 taken 73756 times.
✓ Branch 2 taken 2 times.
73758 for (; pktl; pktl = get_next_pkt(s, st, pktl)) {
961
2/2
✓ Branch 0 taken 3955 times.
✓ Branch 1 taken 69801 times.
73756 if (pktl->pkt.stream_index == stream_index) {
962
2/2
✓ Branch 0 taken 3807 times.
✓ Branch 1 taken 148 times.
3955 if (pktl->pkt.pts != pktl->pkt.dts ||
963
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 3791 times.
3807 pktl->pkt.dts != AV_NOPTS_VALUE ||
964
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 pktl->pkt.duration)
965 break;
966 16 cur_dts -= duration;
967 }
968 }
969
4/4
✓ Branch 0 taken 3939 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 41 times.
✓ Branch 3 taken 3898 times.
3941 if (pktl && pktl->pkt.dts != sti->first_dts) {
970 41 av_log(s, AV_LOG_DEBUG, "first_dts %s not matching first dts %s (pts %s, duration %"PRId64") in the queue\n",
971 41 av_ts2str(sti->first_dts), av_ts2str(pktl->pkt.dts), av_ts2str(pktl->pkt.pts), pktl->pkt.duration);
972 41 return;
973 }
974
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 3898 times.
3900 if (!pktl) {
975 2 av_log(s, AV_LOG_DEBUG, "first_dts %s but no packet with dts in the queue\n", av_ts2str(sti->first_dts));
976 2 return;
977 }
978
2/2
✓ Branch 0 taken 3874 times.
✓ Branch 1 taken 24 times.
3898 pktl = si->packet_buffer.head ? si->packet_buffer.head : fci->parse_queue.head;
979 3898 sti->first_dts = cur_dts;
980
2/2
✓ Branch 0 taken 118982 times.
✓ Branch 1 taken 26006 times.
144988 } else if (sti->cur_dts != RELATIVE_TS_BASE)
981 118982 return;
982
983
2/2
✓ Branch 1 taken 191747 times.
✓ Branch 2 taken 924 times.
192671 for (; pktl; pktl = get_next_pkt(s, st, pktl)) {
984
2/2
✓ Branch 0 taken 162548 times.
✓ Branch 1 taken 29199 times.
191747 if (pktl->pkt.stream_index != stream_index)
985 162548 continue;
986
2/2
✓ Branch 0 taken 871 times.
✓ Branch 1 taken 28328 times.
29199 if ((pktl->pkt.pts == pktl->pkt.dts ||
987
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 856 times.
871 pktl->pkt.pts == AV_NOPTS_VALUE) &&
988
2/2
✓ Branch 0 taken 3940 times.
✓ Branch 1 taken 24403 times.
28343 (pktl->pkt.dts == AV_NOPTS_VALUE ||
989
2/2
✓ Branch 0 taken 179 times.
✓ Branch 1 taken 3761 times.
3940 pktl->pkt.dts == sti->first_dts ||
990
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 177 times.
179 pktl->pkt.dts == RELATIVE_TS_BASE) &&
991
2/2
✓ Branch 0 taken 219 times.
✓ Branch 1 taken 27947 times.
28166 !pktl->pkt.duration &&
992
1/2
✓ Branch 1 taken 219 times.
✗ Branch 2 not taken.
219 av_sat_add64(cur_dts, duration) == cur_dts + (uint64_t)duration
993 ) {
994 219 pktl->pkt.dts = cur_dts;
995
2/2
✓ Branch 0 taken 203 times.
✓ Branch 1 taken 16 times.
219 if (!sti->avctx->has_b_frames)
996 203 pktl->pkt.pts = cur_dts;
997 219 pktl->pkt.duration = duration;
998 } else
999 break;
1000 219 cur_dts = pktl->pkt.dts + pktl->pkt.duration;
1001 }
1002
2/2
✓ Branch 0 taken 924 times.
✓ Branch 1 taken 28980 times.
29904 if (!pktl)
1003 924 sti->cur_dts = cur_dts;
1004 }
1005
1006 664198 static void compute_pkt_fields(AVFormatContext *s, AVStream *st,
1007 AVCodecParserContext *pc, AVPacket *pkt,
1008 int64_t next_dts, int64_t next_pts)
1009 {
1010 664198 FormatContextInternal *const fci = ff_fc_internal(s);
1011 664198 FFFormatContext *const si = &fci->fc;
1012 664198 FFStream *const sti = ffstream(st);
1013 int num, den, presentation_delayed, delay;
1014 int64_t offset;
1015 AVRational duration;
1016 1956285 int onein_oneout = st->codecpar->codec_id != AV_CODEC_ID_H264 &&
1017
2/2
✓ Branch 0 taken 613525 times.
✓ Branch 1 taken 14364 times.
627889 st->codecpar->codec_id != AV_CODEC_ID_HEVC &&
1018
4/4
✓ Branch 0 taken 627889 times.
✓ Branch 1 taken 36309 times.
✓ Branch 2 taken 610130 times.
✓ Branch 3 taken 3395 times.
1902217 st->codecpar->codec_id != AV_CODEC_ID_VVC &&
1019
2/2
✓ Branch 0 taken 610120 times.
✓ Branch 1 taken 10 times.
610130 st->codecpar->codec_id != AV_CODEC_ID_IMM6;
1020
1021
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 664188 times.
664198 if (s->flags & AVFMT_FLAG_NOFILLIN)
1022 10 return;
1023
1024
4/4
✓ Branch 0 taken 254624 times.
✓ Branch 1 taken 409564 times.
✓ Branch 2 taken 77381 times.
✓ Branch 3 taken 177243 times.
664188 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && pkt->dts != AV_NOPTS_VALUE) {
1025
4/4
✓ Branch 0 taken 44308 times.
✓ Branch 1 taken 33073 times.
✓ Branch 2 taken 42968 times.
✓ Branch 3 taken 1340 times.
77381 if (pkt->dts == pkt->pts && sti->last_dts_for_order_check != AV_NOPTS_VALUE) {
1026
2/2
✓ Branch 0 taken 42959 times.
✓ Branch 1 taken 9 times.
42968 if (sti->last_dts_for_order_check <= pkt->dts) {
1027 42959 sti->dts_ordered++;
1028 } else {
1029
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,
1030 "DTS %"PRIi64" < %"PRIi64" out of order\n",
1031 pkt->dts,
1032 sti->last_dts_for_order_check);
1033 9 sti->dts_misordered++;
1034 }
1035
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 42939 times.
42968 if (sti->dts_ordered + sti->dts_misordered > 250) {
1036 29 sti->dts_ordered >>= 1;
1037 29 sti->dts_misordered >>= 1;
1038 }
1039 }
1040
1041 77381 sti->last_dts_for_order_check = pkt->dts;
1042
4/4
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 77344 times.
✓ Branch 2 taken 20 times.
✓ Branch 3 taken 17 times.
77381 if (sti->dts_ordered < 8 * sti->dts_misordered && pkt->dts == pkt->pts)
1043 20 pkt->dts = AV_NOPTS_VALUE;
1044 }
1045
1046
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 664188 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
664188 if ((s->flags & AVFMT_FLAG_IGNDTS) && pkt->pts != AV_NOPTS_VALUE)
1047 ✗ pkt->dts = AV_NOPTS_VALUE;
1048
1049
4/4
✓ Branch 0 taken 245168 times.
✓ Branch 1 taken 419020 times.
✓ Branch 2 taken 30185 times.
✓ Branch 3 taken 214983 times.
664188 if (pc && pc->pict_type == AV_PICTURE_TYPE_B
1050
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 30101 times.
30185 && !sti->avctx->has_b_frames)
1051 //FIXME Set low_delay = 0 when has_b_frames = 1
1052 84 sti->avctx->has_b_frames = 1;
1053
1054 /* do we have a video B-frame ? */
1055 664188 delay = sti->avctx->has_b_frames;
1056 664188 presentation_delayed = 0;
1057
1058 /* XXX: need has_b_frame, but cannot get it if the codec is
1059 * not initialized */
1060
4/4
✓ Branch 0 taken 50989 times.
✓ Branch 1 taken 613199 times.
✓ Branch 2 taken 46242 times.
✓ Branch 3 taken 4747 times.
664188 if (delay &&
1061
2/2
✓ Branch 0 taken 16057 times.
✓ Branch 1 taken 30185 times.
46242 pc && pc->pict_type != AV_PICTURE_TYPE_B)
1062 16057 presentation_delayed = 1;
1063
1064
4/4
✓ Branch 0 taken 364893 times.
✓ Branch 1 taken 299295 times.
✓ Branch 2 taken 183798 times.
✓ Branch 3 taken 181095 times.
664188 if (pkt->pts != AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE &&
1065
3/4
✓ Branch 0 taken 34722 times.
✓ Branch 1 taken 149076 times.
✓ Branch 2 taken 34722 times.
✗ Branch 3 not taken.
183798 st->pts_wrap_bits < 63 && pkt->dts > INT64_MIN + (1LL << st->pts_wrap_bits) &&
1066
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34722 times.
34722 pkt->dts - (1LL << (st->pts_wrap_bits - 1)) > pkt->pts) {
1067 ✗ if (is_relative(sti->cur_dts) || pkt->dts - (1LL<<(st->pts_wrap_bits - 1)) > sti->cur_dts) {
1068 ✗ pkt->dts -= 1LL << st->pts_wrap_bits;
1069 } else
1070 ✗ pkt->pts += 1LL << st->pts_wrap_bits;
1071 }
1072
1073 /* Some MPEG-2 in MPEG-PS lack dts (issue #171 / input_file.mpg).
1074 * We take the conservative approach and discard both.
1075 * Note: If this is misbehaving for an H.264 file, then possibly
1076 * presentation_delayed is not set correctly. */
1077
4/4
✓ Branch 0 taken 32973 times.
✓ Branch 1 taken 631215 times.
✓ Branch 2 taken 26165 times.
✓ Branch 3 taken 6808 times.
664188 if (delay == 1 && pkt->dts == pkt->pts &&
1078
4/4
✓ Branch 0 taken 6759 times.
✓ Branch 1 taken 19406 times.
✓ Branch 2 taken 634 times.
✓ Branch 3 taken 6125 times.
26165 pkt->dts != AV_NOPTS_VALUE && presentation_delayed) {
1079 634 av_log(s, AV_LOG_DEBUG, "invalid dts/pts combination %"PRIi64"\n", pkt->dts);
1080
2/2
✓ Branch 0 taken 295 times.
✓ Branch 1 taken 339 times.
634 if ( strcmp(s->iformat->name, "mov,mp4,m4a,3gp,3g2,mj2")
1081
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 273 times.
295 && strcmp(s->iformat->name, "flv")) // otherwise we discard correct timestamps for vc1-wmapro.ism
1082 22 pkt->dts = AV_NOPTS_VALUE;
1083 }
1084
1085 664188 duration = av_mul_q((AVRational) {pkt->duration, 1}, st->time_base);
1086
2/2
✓ Branch 0 taken 339611 times.
✓ Branch 1 taken 324577 times.
664188 if (pkt->duration <= 0) {
1087 339611 compute_frame_duration(s, &num, &den, st, pc, pkt);
1088
3/4
✓ Branch 0 taken 329321 times.
✓ Branch 1 taken 10290 times.
✓ Branch 2 taken 329321 times.
✗ Branch 3 not taken.
339611 if (den && num) {
1089 329321 duration = (AVRational) {num, den};
1090 329321 pkt->duration = av_rescale_rnd(1,
1091 329321 num * (int64_t) st->time_base.den,
1092 329321 den * (int64_t) st->time_base.num,
1093 AV_ROUND_DOWN);
1094 }
1095 }
1096
1097
6/6
✓ Branch 0 taken 651076 times.
✓ Branch 1 taken 13112 times.
✓ Branch 2 taken 456973 times.
✓ Branch 3 taken 194103 times.
✓ Branch 4 taken 90014 times.
✓ Branch 5 taken 366959 times.
664188 if (pkt->duration > 0 && (si->packet_buffer.head || fci->parse_queue.head))
1098 284117 update_initial_durations(s, st, pkt->stream_index, pkt->duration);
1099
1100 /* Correct timestamps with byte offset if demuxers only have timestamps
1101 * on packet boundaries */
1102
5/6
✓ Branch 0 taken 245168 times.
✓ Branch 1 taken 419020 times.
✓ Branch 2 taken 949 times.
✓ Branch 3 taken 244219 times.
✓ Branch 4 taken 949 times.
✗ Branch 5 not taken.
664188 if (pc && sti->need_parsing == AVSTREAM_PARSE_TIMESTAMPS && pkt->size) {
1103 /* this will estimate bitrate based on this frame's duration and size */
1104 949 offset = av_rescale(pc->offset, pkt->duration, pkt->size);
1105
2/2
✓ Branch 0 taken 564 times.
✓ Branch 1 taken 385 times.
949 if (pkt->pts != AV_NOPTS_VALUE)
1106 564 pkt->pts += offset;
1107
2/2
✓ Branch 0 taken 121 times.
✓ Branch 1 taken 828 times.
949 if (pkt->dts != AV_NOPTS_VALUE)
1108 121 pkt->dts += offset;
1109 }
1110
1111 /* This may be redundant, but it should not hurt. */
1112
2/2
✓ Branch 0 taken 214498 times.
✓ Branch 1 taken 449690 times.
664188 if (pkt->dts != AV_NOPTS_VALUE &&
1113
2/2
✓ Branch 0 taken 183776 times.
✓ Branch 1 taken 30722 times.
214498 pkt->pts != AV_NOPTS_VALUE &&
1114
2/2
✓ Branch 0 taken 7666 times.
✓ Branch 1 taken 176110 times.
183776 pkt->pts > pkt->dts)
1115 7666 presentation_delayed = 1;
1116
1117
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 664188 times.
664188 if (s->debug & AV_FDEBUG_TS)
1118 ✗ av_log(s, AV_LOG_DEBUG,
1119 "IN delayed:%d pts:%s, dts:%s cur_dts:%s st:%d pc:%p duration:%"PRId64" delay:%d onein_oneout:%d\n",
1120 ✗ presentation_delayed, av_ts2str(pkt->pts), av_ts2str(pkt->dts), av_ts2str(sti->cur_dts),
1121 pkt->stream_index, pc, pkt->duration, delay, onein_oneout);
1122
1123 /* Interpolate PTS and DTS if they are not present. We skip H264
1124 * currently because delay and has_b_frames are not reliably set. */
1125
8/8
✓ Branch 0 taken 50989 times.
✓ Branch 1 taken 613199 times.
✓ Branch 2 taken 32973 times.
✓ Branch 3 taken 18016 times.
✓ Branch 4 taken 29171 times.
✓ Branch 5 taken 3802 times.
✓ Branch 6 taken 606321 times.
✓ Branch 7 taken 36049 times.
664188 if ((delay == 0 || (delay == 1 && pc)) &&
1126 onein_oneout) {
1127
2/2
✓ Branch 0 taken 6167 times.
✓ Branch 1 taken 600154 times.
606321 if (presentation_delayed) {
1128 /* DTS = decompression timestamp */
1129 /* PTS = presentation timestamp */
1130
2/2
✓ Branch 0 taken 2844 times.
✓ Branch 1 taken 3323 times.
6167 if (pkt->dts == AV_NOPTS_VALUE)
1131 2844 pkt->dts = sti->last_IP_pts;
1132 6167 update_initial_timestamps(s, pkt->stream_index, pkt->dts, pkt->pts, pkt);
1133
2/2
✓ Branch 0 taken 2506 times.
✓ Branch 1 taken 3661 times.
6167 if (pkt->dts == AV_NOPTS_VALUE)
1134 2506 pkt->dts = sti->cur_dts;
1135
1136 /* This is tricky: the dts must be incremented by the duration
1137 * of the frame we are displaying, i.e. the last I- or P-frame. */
1138
3/4
✓ Branch 0 taken 469 times.
✓ Branch 1 taken 5698 times.
✓ Branch 2 taken 469 times.
✗ Branch 3 not taken.
6167 if (sti->last_IP_duration == 0 && (uint64_t)pkt->duration <= INT32_MAX)
1139 469 sti->last_IP_duration = pkt->duration;
1140
1/2
✓ Branch 0 taken 6167 times.
✗ Branch 1 not taken.
6167 if (pkt->dts != AV_NOPTS_VALUE)
1141 6167 sti->cur_dts = av_sat_add64(pkt->dts, sti->last_IP_duration);
1142
1/2
✓ Branch 0 taken 6167 times.
✗ Branch 1 not taken.
6167 if (pkt->dts != AV_NOPTS_VALUE &&
1143
2/2
✓ Branch 0 taken 3304 times.
✓ Branch 1 taken 2863 times.
6167 pkt->pts == AV_NOPTS_VALUE &&
1144
2/2
✓ Branch 0 taken 3302 times.
✓ Branch 1 taken 2 times.
3304 sti->last_IP_duration > 0 &&
1145
4/4
✓ Branch 0 taken 815 times.
✓ Branch 1 taken 2487 times.
✓ Branch 2 taken 805 times.
✓ Branch 3 taken 10 times.
3302 ((uint64_t)sti->cur_dts - (uint64_t)next_dts + 1) <= 2 &&
1146
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 794 times.
805 next_dts != next_pts &&
1147 next_pts != AV_NOPTS_VALUE)
1148 11 pkt->pts = next_dts;
1149
1150
1/2
✓ Branch 0 taken 6167 times.
✗ Branch 1 not taken.
6167 if ((uint64_t)pkt->duration <= INT32_MAX)
1151 6167 sti->last_IP_duration = pkt->duration;
1152 6167 sti->last_IP_pts = pkt->pts;
1153 /* Cannot compute PTS if not present (we can compute it only
1154 * by knowing the future. */
1155
2/2
✓ Branch 0 taken 253083 times.
✓ Branch 1 taken 347071 times.
600154 } else if (pkt->pts != AV_NOPTS_VALUE ||
1156
2/2
✓ Branch 0 taken 223244 times.
✓ Branch 1 taken 29839 times.
253083 pkt->dts != AV_NOPTS_VALUE ||
1157
2/2
✓ Branch 0 taken 220620 times.
✓ Branch 1 taken 2624 times.
223244 pkt->duration > 0 ) {
1158
1159 /* presentation is not delayed : PTS and DTS are the same */
1160
2/2
✓ Branch 0 taken 250459 times.
✓ Branch 1 taken 347071 times.
597530 if (pkt->pts == AV_NOPTS_VALUE)
1161 250459 pkt->pts = pkt->dts;
1162 597530 update_initial_timestamps(s, pkt->stream_index, pkt->pts,
1163 pkt->pts, pkt);
1164
2/2
✓ Branch 0 taken 220620 times.
✓ Branch 1 taken 376910 times.
597530 if (pkt->pts == AV_NOPTS_VALUE)
1165 220620 pkt->pts = sti->cur_dts;
1166 597530 pkt->dts = pkt->pts;
1167
4/4
✓ Branch 0 taken 597527 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 597518 times.
✓ Branch 3 taken 9 times.
597530 if (pkt->pts != AV_NOPTS_VALUE && duration.num >= 0)
1168 597518 sti->cur_dts = av_add_stable(st->time_base, pkt->pts, duration, 1);
1169 }
1170 }
1171
1172
3/4
✓ Branch 0 taken 615360 times.
✓ Branch 1 taken 48828 times.
✓ Branch 2 taken 615360 times.
✗ Branch 3 not taken.
664188 if (pkt->pts != AV_NOPTS_VALUE && delay <= MAX_REORDER_DELAY) {
1173 615360 sti->pts_buffer[0] = pkt->pts;
1174
4/4
✓ Branch 0 taken 26973 times.
✓ Branch 1 taken 608124 times.
✓ Branch 2 taken 19737 times.
✓ Branch 3 taken 7236 times.
635097 for (int i = 0; i < delay && sti->pts_buffer[i] > sti->pts_buffer[i + 1]; i++)
1175 19737 FFSWAP(int64_t, sti->pts_buffer[i], sti->pts_buffer[i + 1]);
1176
1177
2/2
✓ Branch 1 taken 614627 times.
✓ Branch 2 taken 733 times.
615360 if (has_decode_delay_been_guessed(st))
1178 614627 pkt->dts = select_from_pts_buffer(st, sti->pts_buffer, pkt->dts);
1179 }
1180 // We skipped it above so we try here.
1181
2/2
✓ Branch 0 taken 54078 times.
✓ Branch 1 taken 610110 times.
664188 if (!onein_oneout)
1182 // This should happen on the first packet
1183 54078 update_initial_timestamps(s, pkt->stream_index, pkt->dts, pkt->pts, pkt);
1184
2/2
✓ Branch 0 taken 10589 times.
✓ Branch 1 taken 653599 times.
664188 if (pkt->dts > sti->cur_dts)
1185 10589 sti->cur_dts = pkt->dts;
1186
1187
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 664188 times.
664188 if (s->debug & AV_FDEBUG_TS)
1188 ✗ av_log(s, AV_LOG_DEBUG, "OUTdelayed:%d/%d pts:%s, dts:%s cur_dts:%s st:%d (%d)\n",
1189 ✗ presentation_delayed, delay, av_ts2str(pkt->pts), av_ts2str(pkt->dts), av_ts2str(sti->cur_dts), st->index, st->id);
1190
1191 /* update flags */
1192
4/4
✓ Branch 0 taken 664023 times.
✓ Branch 1 taken 165 times.
✓ Branch 3 taken 448360 times.
✓ Branch 4 taken 215663 times.
664188 if (st->codecpar->codec_type == AVMEDIA_TYPE_DATA || ff_is_intra_only(st->codecpar->codec_id))
1193 448525 pkt->flags |= AV_PKT_FLAG_KEY;
1194 }
1195
1196 /**
1197 * Parse a packet, add all split parts to parse_queue.
1198 *
1199 * @param pkt Packet to parse; must not be NULL.
1200 * @param flush Indicates whether to flush. If set, pkt must be blank.
1201 */
1202 728539 static int parse_packet(AVFormatContext *s, AVPacket *pkt,
1203 int stream_index, int flush)
1204 {
1205 728539 FormatContextInternal *const fci = ff_fc_internal(s);
1206 728539 AVStream *st = s->streams[stream_index];
1207 728539 FFStream *const sti = ffstream(st);
1208 728539 AVPacket *out_pkt = sti->parse_pkt;
1209 728539 const AVPacketSideData *sd = NULL;
1210 728539 const uint8_t *data = pkt->data;
1211 728539 uint8_t *extradata = sti->avctx->extradata;
1212 728539 int extradata_size = sti->avctx->extradata_size;
1213 728539 int size = pkt->size;
1214 728539 int ret = 0, got_output = flush, pkt_side_data_consumed = 0;
1215
1216
5/6
✓ Branch 0 taken 2562 times.
✓ Branch 1 taken 725977 times.
✓ Branch 2 taken 164 times.
✓ Branch 3 taken 2398 times.
✓ Branch 4 taken 164 times.
✗ Branch 5 not taken.
728539 if (!size && !flush && sti->parser->flags & PARSER_FLAG_COMPLETE_FRAMES) {
1217 // preserve 0-size sync packets
1218 164 compute_pkt_fields(s, st, sti->parser, pkt, pkt->dts, pkt->pts);
1219
1220 // Theora has valid 0-sized packets that need to be output
1221
2/2
✓ Branch 0 taken 148 times.
✓ Branch 1 taken 16 times.
164 if (st->codecpar->codec_id == AV_CODEC_ID_THEORA) {
1222 148 ret = ff_packet_list_put(&fci->parse_queue,
1223 pkt, NULL, 0);
1224
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 148 times.
148 if (ret < 0)
1225 ✗ goto fail;
1226 }
1227 }
1228
1229
2/2
✓ Branch 0 taken 29465 times.
✓ Branch 1 taken 699074 times.
728539 if (pkt->side_data_elems)
1230 29465 sd = av_packet_side_data_get(pkt->side_data, pkt->side_data_elems,
1231 AV_PKT_DATA_NEW_EXTRADATA);
1232
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 728533 times.
728539 if (sd) {
1233 av_assert1(size && !flush);
1234
1235 6 sti->avctx->extradata = sd->data;
1236 6 sti->avctx->extradata_size = sd->size;
1237 }
1238
1239
6/6
✓ Branch 0 taken 896126 times.
✓ Branch 1 taken 732253 times.
✓ Branch 2 taken 6112 times.
✓ Branch 3 taken 726141 times.
✓ Branch 4 taken 3714 times.
✓ Branch 5 taken 2398 times.
1628379 while (size > 0 || (flush && got_output)) {
1240 899840 int64_t next_pts = pkt->pts;
1241 899840 int64_t next_dts = pkt->dts;
1242 int len;
1243
1244 899840 len = av_parser_parse2(sti->parser, sti->avctx,
1245 &out_pkt->data, &out_pkt->size, data, size,
1246 pkt->pts, pkt->dts, pkt->pos);
1247
1248 899840 pkt->pts = pkt->dts = AV_NOPTS_VALUE;
1249 899840 pkt->pos = -1;
1250 /* increment read pointer */
1251 av_assert1(data || !len);
1252
2/2
✓ Branch 0 taken 883489 times.
✓ Branch 1 taken 16351 times.
899840 data = len ? data + len : data;
1253 899840 size -= len;
1254
1255 899840 got_output = !!out_pkt->size;
1256
1257
4/4
✓ Branch 0 taken 40446 times.
✓ Branch 1 taken 859394 times.
✓ Branch 2 taken 18395 times.
✓ Branch 3 taken 22051 times.
899840 if (pkt->side_data && !out_pkt->side_data) {
1258 /* for the first iteration, side_data are simply moved to output.
1259 * in case of additional iterations, they are duplicated each time. */
1260
2/2
✓ Branch 0 taken 12583 times.
✓ Branch 1 taken 5812 times.
18395 if (!pkt_side_data_consumed) {
1261 12583 pkt_side_data_consumed = 1;
1262 12583 out_pkt->side_data = pkt->side_data;
1263 12583 out_pkt->side_data_elems = pkt->side_data_elems;
1264
2/2
✓ Branch 0 taken 5812 times.
✓ Branch 1 taken 5812 times.
11624 } else for (int i = 0; i < pkt->side_data_elems; i++) {
1265 5812 const AVPacketSideData *const src_sd = &pkt->side_data[i];
1266 5812 uint8_t *dst_data = av_packet_new_side_data(out_pkt, src_sd->type, src_sd->size);
1267
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5812 times.
5812 if (!dst_data) {
1268 ✗ ret = AVERROR(ENOMEM);
1269 ✗ goto fail;
1270 }
1271 5812 memcpy(dst_data, src_sd->data, src_sd->size);
1272 }
1273 }
1274
1275
2/2
✓ Branch 0 taken 654836 times.
✓ Branch 1 taken 245004 times.
899840 if (!out_pkt->size)
1276 654836 continue;
1277
1278
4/4
✓ Branch 0 taken 243688 times.
✓ Branch 1 taken 1316 times.
✓ Branch 2 taken 72607 times.
✓ Branch 3 taken 171081 times.
245004 if (pkt->buf && out_pkt->data == pkt->data) {
1279 /* reference pkt->buf only when out_pkt->data is guaranteed to point
1280 * to data in it and not in the parser's internal buffer. */
1281 /* XXX: Ensure this is the case with all parsers when sti->parser->flags
1282 * is PARSER_FLAG_COMPLETE_FRAMES and check for that instead? */
1283 72607 out_pkt->buf = av_buffer_ref(pkt->buf);
1284
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 72607 times.
72607 if (!out_pkt->buf) {
1285 ✗ ret = AVERROR(ENOMEM);
1286 ✗ goto fail;
1287 }
1288 } else {
1289 172397 ret = av_packet_make_refcounted(out_pkt);
1290
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 172397 times.
172397 if (ret < 0)
1291 ✗ goto fail;
1292 }
1293
1294 /* set the duration */
1295
2/2
✓ Branch 0 taken 52644 times.
✓ Branch 1 taken 192360 times.
245004 out_pkt->duration = (sti->parser->flags & PARSER_FLAG_COMPLETE_FRAMES) ? pkt->duration : 0;
1296
2/2
✓ Branch 0 taken 160837 times.
✓ Branch 1 taken 84167 times.
245004 if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
1297
6/6
✓ Branch 0 taken 160679 times.
✓ Branch 1 taken 158 times.
✓ Branch 2 taken 160417 times.
✓ Branch 3 taken 262 times.
✓ Branch 4 taken 147903 times.
✓ Branch 5 taken 12514 times.
160837 if (sti->avctx->sample_rate > 0 && !out_pkt->duration && sti->parser->duration > 0) {
1298 147903 out_pkt->duration =
1299 147903 av_rescale_q_rnd(sti->parser->duration,
1300 147903 (AVRational) { 1, sti->avctx->sample_rate },
1301 st->time_base,
1302 AV_ROUND_DOWN);
1303 }
1304
2/2
✓ Branch 0 taken 1822 times.
✓ Branch 1 taken 82345 times.
84167 } else if (st->codecpar->codec_id == AV_CODEC_ID_GIF) {
1305
2/4
✓ Branch 0 taken 1822 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1822 times.
✗ Branch 3 not taken.
1822 if (st->time_base.num > 0 && st->time_base.den > 0 &&
1306
1/2
✓ Branch 0 taken 1822 times.
✗ Branch 1 not taken.
1822 sti->parser->duration) {
1307 1822 out_pkt->duration = sti->parser->duration;
1308 }
1309 }
1310
1311 245004 out_pkt->stream_index = st->index;
1312 245004 out_pkt->pts = sti->parser->pts;
1313 245004 out_pkt->dts = sti->parser->dts;
1314 245004 out_pkt->pos = sti->parser->pos;
1315 245004 out_pkt->flags |= pkt->flags & (AV_PKT_FLAG_DISCARD | AV_PKT_FLAG_CORRUPT);
1316
1317
2/2
✓ Branch 0 taken 165075 times.
✓ Branch 1 taken 79929 times.
245004 if (sti->need_parsing == AVSTREAM_PARSE_FULL_RAW)
1318 165075 out_pkt->pos = sti->parser->frame_offset;
1319
1320
2/2
✓ Branch 0 taken 232906 times.
✓ Branch 1 taken 12098 times.
245004 if (sti->parser->key_frame == 1 ||
1321
2/2
✓ Branch 0 taken 147042 times.
✓ Branch 1 taken 85864 times.
232906 (sti->parser->key_frame == -1 &&
1322
2/2
✓ Branch 0 taken 134529 times.
✓ Branch 1 taken 12513 times.
147042 sti->parser->pict_type == AV_PICTURE_TYPE_I))
1323 146627 out_pkt->flags |= AV_PKT_FLAG_KEY;
1324
1325
6/6
✓ Branch 0 taken 147042 times.
✓ Branch 1 taken 97962 times.
✓ Branch 2 taken 325 times.
✓ Branch 3 taken 146717 times.
✓ Branch 4 taken 294 times.
✓ Branch 5 taken 31 times.
245004 if (sti->parser->key_frame == -1 && sti->parser->pict_type ==AV_PICTURE_TYPE_NONE && (pkt->flags&AV_PKT_FLAG_KEY))
1326 294 out_pkt->flags |= AV_PKT_FLAG_KEY;
1327
1328 245004 compute_pkt_fields(s, st, sti->parser, out_pkt, next_dts, next_pts);
1329
1330 245004 ret = ff_packet_list_put(&fci->parse_queue,
1331 out_pkt, NULL, 0);
1332
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 245004 times.
245004 if (ret < 0)
1333 ✗ goto fail;
1334 }
1335
1336 /* end of the stream => close and free the parser */
1337
2/2
✓ Branch 0 taken 726141 times.
✓ Branch 1 taken 2398 times.
728539 if (flush) {
1338 2398 av_parser_close(sti->parser);
1339 2398 sti->parser = NULL;
1340 }
1341
1342 726141 fail:
1343
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 728533 times.
728539 if (sd) {
1344 6 sti->avctx->extradata = extradata;
1345 6 sti->avctx->extradata_size = extradata_size;
1346 }
1347
2/2
✓ Branch 0 taken 12583 times.
✓ Branch 1 taken 715956 times.
728539 if (pkt_side_data_consumed) {
1348 12583 pkt->side_data = NULL;
1349 12583 pkt->side_data_elems = 0;
1350 }
1351
1352
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 728539 times.
728539 if (ret < 0)
1353 ✗ av_packet_unref(out_pkt);
1354 728539 av_packet_unref(pkt);
1355 728539 return ret;
1356 }
1357
1358 52570 static int64_t ts_to_samples(AVStream *st, int64_t ts)
1359 {
1360 52570 return av_rescale(ts, st->time_base.num * st->codecpar->sample_rate, st->time_base.den);
1361 }
1362
1363 9510 static int codec_close(FFStream *sti)
1364 {
1365 9510 AVCodecContext *avctx_new = NULL;
1366 9510 AVCodecParameters *par_tmp = NULL;
1367 9510 const AVCodec *new_codec = NULL;
1368 int ret;
1369
1370 9510 new_codec =
1371 9510 (sti->avctx->codec_id != sti->pub.codecpar->codec_id) ?
1372
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 9499 times.
9510 avcodec_find_decoder(sti->pub.codecpar->codec_id) :
1373 9499 sti->avctx->codec;
1374
1375 9510 avctx_new = avcodec_alloc_context3(new_codec);
1376
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9510 times.
9510 if (!avctx_new) {
1377 ✗ ret = AVERROR(ENOMEM);
1378 ✗ goto fail;
1379 }
1380
1381 9510 par_tmp = avcodec_parameters_alloc();
1382
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9510 times.
9510 if (!par_tmp) {
1383 ✗ ret = AVERROR(ENOMEM);
1384 ✗ goto fail;
1385 }
1386
1387 9510 ret = avcodec_parameters_from_context(par_tmp, sti->avctx);
1388
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9510 times.
9510 if (ret < 0)
1389 ✗ goto fail;
1390
1391 9510 ret = avcodec_parameters_to_context(avctx_new, par_tmp);
1392
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9510 times.
9510 if (ret < 0)
1393 ✗ goto fail;
1394
1395 9510 avctx_new->pkt_timebase = sti->avctx->pkt_timebase;
1396
1397 9510 avcodec_free_context(&sti->avctx);
1398 9510 sti->avctx = avctx_new;
1399
1400 9510 avctx_new = NULL;
1401 9510 ret = 0;
1402
1403 9510 fail:
1404 9510 avcodec_free_context(&avctx_new);
1405 9510 avcodec_parameters_free(&par_tmp);
1406
1407 9510 return ret;
1408 }
1409
1410 static int extract_extradata(FFFormatContext *si, AVStream *st, const AVPacket *pkt);
1411
1412 669341 static int read_frame_internal(AVFormatContext *s, AVPacket *pkt)
1413 {
1414 669341 FormatContextInternal *const fci = ff_fc_internal(s);
1415 669341 FFFormatContext *const si = &fci->fc;
1416 669341 int ret, got_packet = 0;
1417 669341 AVDictionary *metadata = NULL;
1418
1419
4/4
✓ Branch 0 taken 1395615 times.
✓ Branch 1 taken 419030 times.
✓ Branch 2 taken 1152612 times.
✓ Branch 3 taken 243003 times.
1814645 while (!got_packet && !fci->parse_queue.head) {
1420 AVStream *st;
1421 FFStream *sti;
1422
1423 /* read next packet */
1424 1152612 ret = ff_read_packet(s, pkt);
1425
2/2
✓ Branch 0 taken 7308 times.
✓ Branch 1 taken 1145304 times.
1152612 if (ret < 0) {
1426
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7307 times.
7308 if (ret == AVERROR(EAGAIN))
1427 1 return ret;
1428 /* flush the parsers */
1429
2/2
✓ Branch 0 taken 8811 times.
✓ Branch 1 taken 7307 times.
16118 for (unsigned i = 0; i < s->nb_streams; i++) {
1430 8811 AVStream *const st = s->streams[i];
1431 8811 FFStream *const sti = ffstream(st);
1432
4/4
✓ Branch 0 taken 3470 times.
✓ Branch 1 taken 5341 times.
✓ Branch 2 taken 2398 times.
✓ Branch 3 taken 1072 times.
8811 if (sti->parser && sti->need_parsing)
1433 2398 parse_packet(s, pkt, st->index, 1);
1434 }
1435 /* all remaining packets are now in parse_queue =>
1436 * really terminate parsing */
1437 7307 break;
1438 }
1439 1145304 ret = 0;
1440 1145304 st = s->streams[pkt->stream_index];
1441 1145304 sti = ffstream(st);
1442
1443 1145304 st->event_flags |= AVSTREAM_EVENT_FLAG_NEW_PACKETS;
1444
1445 1145304 int new_extradata = !!av_packet_side_data_get(pkt->side_data, pkt->side_data_elems,
1446 AV_PKT_DATA_NEW_EXTRADATA);
1447
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 1145282 times.
1145304 if (new_extradata)
1448 22 sti->need_context_update = 1;
1449
1450 /* update context if required */
1451
2/2
✓ Branch 0 taken 206 times.
✓ Branch 1 taken 1145098 times.
1145304 if (sti->need_context_update) {
1452
2/2
✓ Branch 1 taken 19 times.
✓ Branch 2 taken 187 times.
206 if (avcodec_is_open(sti->avctx)) {
1453 19 av_log(s, AV_LOG_DEBUG, "Demuxer context update while decoder is open, closing and trying to re-open\n");
1454 19 ret = codec_close(sti);
1455 19 sti->info->found_decoder = 0;
1456
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if (ret < 0)
1457 ✗ return ret;
1458 }
1459
1460 /* close parser, because it depends on the codec and extradata */
1461
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 171 times.
206 if (sti->parser &&
1462
4/4
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 13 times.
✓ Branch 2 taken 15 times.
✓ Branch 3 taken 7 times.
35 (sti->avctx->codec_id != st->codecpar->codec_id || new_extradata)) {
1463 28 av_parser_close(sti->parser);
1464 28 sti->parser = NULL;
1465 }
1466
1467 206 ret = avcodec_parameters_to_context(sti->avctx, st->codecpar);
1468
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 206 times.
206 if (ret < 0) {
1469 ✗ av_packet_unref(pkt);
1470 ✗ return ret;
1471 }
1472
1473
2/2
✓ Branch 0 taken 153 times.
✓ Branch 1 taken 53 times.
206 if (!sti->avctx->extradata) {
1474 153 sti->extract_extradata.inited = 0;
1475
1476 153 ret = extract_extradata(si, st, pkt);
1477
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 153 times.
153 if (ret < 0) {
1478 ✗ av_packet_unref(pkt);
1479 ✗ return ret;
1480 }
1481 }
1482
1483 206 sti->codec_desc = avcodec_descriptor_get(sti->avctx->codec_id);
1484
1485 206 sti->need_context_update = 0;
1486 }
1487
1488
2/2
✓ Branch 0 taken 362651 times.
✓ Branch 1 taken 782653 times.
1145304 if (pkt->pts != AV_NOPTS_VALUE &&
1489
2/2
✓ Branch 0 taken 184922 times.
✓ Branch 1 taken 177729 times.
362651 pkt->dts != AV_NOPTS_VALUE &&
1490
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 184922 times.
184922 pkt->pts < pkt->dts) {
1491 ✗ av_log(s, AV_LOG_WARNING,
1492 "Invalid timestamps stream=%d, pts=%s, dts=%s, size=%d\n",
1493 pkt->stream_index,
1494 ✗ av_ts2str(pkt->pts),
1495 ✗ av_ts2str(pkt->dts),
1496 pkt->size);
1497 }
1498
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1145304 times.
1145304 if (s->debug & AV_FDEBUG_TS)
1499 ✗ av_log(s, AV_LOG_DEBUG,
1500 "ff_read_packet stream=%d, pts=%s, dts=%s, size=%d, duration=%"PRId64", flags=%d\n",
1501 pkt->stream_index,
1502 ✗ av_ts2str(pkt->pts),
1503 ✗ av_ts2str(pkt->dts),
1504 pkt->size, pkt->duration, pkt->flags);
1505
1506
6/6
✓ Branch 0 taken 727537 times.
✓ Branch 1 taken 417767 times.
✓ Branch 2 taken 3083 times.
✓ Branch 3 taken 724454 times.
✓ Branch 4 taken 3078 times.
✓ Branch 5 taken 5 times.
1145304 if (sti->need_parsing && !sti->parser && !(s->flags & AVFMT_FLAG_NOPARSE)) {
1507 3078 sti->parser = av_parser_init(st->codecpar->codec_id);
1508
2/2
✓ Branch 0 taken 1258 times.
✓ Branch 1 taken 1820 times.
3078 if (!sti->parser) {
1509 1258 av_log(s, AV_LOG_VERBOSE, "parser not found for codec "
1510 "%s, packets or times may be invalid.\n",
1511 1258 avcodec_get_name(st->codecpar->codec_id));
1512 /* no parser available: just output the raw packets */
1513 1258 sti->need_parsing = AVSTREAM_PARSE_NONE;
1514
2/2
✓ Branch 0 taken 766 times.
✓ Branch 1 taken 1054 times.
1820 } else if (sti->need_parsing == AVSTREAM_PARSE_HEADERS)
1515 766 sti->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES;
1516
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1054 times.
1054 else if (sti->need_parsing == AVSTREAM_PARSE_FULL_ONCE)
1517 ✗ sti->parser->flags |= PARSER_FLAG_ONCE;
1518
2/2
✓ Branch 0 taken 531 times.
✓ Branch 1 taken 523 times.
1054 else if (sti->need_parsing == AVSTREAM_PARSE_FULL_RAW)
1519 531 sti->parser->flags |= PARSER_FLAG_USE_CODEC_TS;
1520 }
1521
1522
4/4
✓ Branch 0 taken 726279 times.
✓ Branch 1 taken 419025 times.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 726274 times.
1145304 if (!sti->need_parsing || !sti->parser) {
1523 /* no parsing needed: we just output the packet as is */
1524 419030 compute_pkt_fields(s, st, NULL, pkt, AV_NOPTS_VALUE, AV_NOPTS_VALUE);
1525
2/2
✓ Branch 0 taken 101882 times.
✓ Branch 1 taken 317148 times.
419030 if ((s->iformat->flags & AVFMT_GENERIC_INDEX) &&
1526
4/4
✓ Branch 0 taken 101003 times.
✓ Branch 1 taken 879 times.
✓ Branch 2 taken 100728 times.
✓ Branch 3 taken 275 times.
101882 (pkt->flags & AV_PKT_FLAG_KEY) && pkt->dts != AV_NOPTS_VALUE) {
1527 100728 ff_reduce_index(s, st->index);
1528 100728 av_add_index_entry(st, pkt->pos, pkt->dts,
1529 0, 0, AVINDEX_KEYFRAME);
1530 }
1531 419030 got_packet = 1;
1532
2/2
✓ Branch 0 taken 726141 times.
✓ Branch 1 taken 133 times.
726274 } else if (st->discard < AVDISCARD_ALL) {
1533
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 726141 times.
726141 if ((ret = parse_packet(s, pkt, pkt->stream_index, 0)) < 0)
1534 ✗ return ret;
1535 726141 st->codecpar->sample_rate = sti->avctx->sample_rate;
1536 726141 st->codecpar->bit_rate = sti->avctx->bit_rate;
1537 726141 ret = av_channel_layout_copy(&st->codecpar->ch_layout, &sti->avctx->ch_layout);
1538
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 726141 times.
726141 if (ret < 0)
1539 ✗ return ret;
1540 726141 st->codecpar->codec_id = sti->avctx->codec_id;
1541 } else {
1542 /* free packet */
1543 133 av_packet_unref(pkt);
1544 }
1545
2/2
✓ Branch 0 taken 392702 times.
✓ Branch 1 taken 752602 times.
1145304 if (pkt->flags & AV_PKT_FLAG_KEY)
1546 392702 sti->skip_to_keyframe = 0;
1547
2/2
✓ Branch 0 taken 127 times.
✓ Branch 1 taken 1145177 times.
1145304 if (sti->skip_to_keyframe) {
1548 127 av_packet_unref(pkt);
1549 127 got_packet = 0;
1550 }
1551 }
1552
1553
4/4
✓ Branch 0 taken 250310 times.
✓ Branch 1 taken 419030 times.
✓ Branch 2 taken 244046 times.
✓ Branch 3 taken 6264 times.
669340 if (!got_packet && fci->parse_queue.head)
1554 244046 ret = ff_packet_list_get(&fci->parse_queue, pkt);
1555
1556
2/2
✓ Branch 0 taken 663076 times.
✓ Branch 1 taken 6264 times.
669340 if (ret >= 0) {
1557 663076 AVStream *const st = s->streams[pkt->stream_index];
1558 663076 FFStream *const sti = ffstream(st);
1559 663076 int discard_padding = 0;
1560
3/4
✓ Branch 0 taken 17511 times.
✓ Branch 1 taken 645565 times.
✓ Branch 2 taken 17511 times.
✗ Branch 3 not taken.
663076 if (sti->first_discard_sample && pkt->pts != AV_NOPTS_VALUE) {
1561
2/2
✓ Branch 1 taken 7466 times.
✓ Branch 2 taken 10045 times.
17511 int64_t pts = pkt->pts - (is_relative(pkt->pts) ? RELATIVE_TS_BASE : 0);
1562 17511 int64_t sample = ts_to_samples(st, pts);
1563
2/2
✓ Branch 1 taken 17 times.
✓ Branch 2 taken 17494 times.
17511 int64_t duration = FFMAX(st->codecpar->frame_size, ts_to_samples(st, pkt->duration));
1564 17511 int64_t end_sample = sample + duration;
1565
3/4
✓ Branch 0 taken 17511 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 338 times.
✓ Branch 3 taken 17173 times.
17511 if (duration > 0 && end_sample >= sti->first_discard_sample &&
1566
2/2
✓ Branch 0 taken 334 times.
✓ Branch 1 taken 4 times.
338 sample < sti->last_discard_sample)
1567 334 discard_padding = FFMIN(end_sample - sti->first_discard_sample, duration);
1568 }
1569
2/2
✓ Branch 0 taken 2882 times.
✓ Branch 1 taken 660194 times.
663076 if (sti->seek_discard) {
1570 2882 const AVCodecParameters *par = st->codecpar;
1571
1572
3/4
✓ Branch 0 taken 1383 times.
✓ Branch 1 taken 1499 times.
✓ Branch 2 taken 1383 times.
✗ Branch 3 not taken.
2882 if (par->codec_type == AVMEDIA_TYPE_AUDIO && par->sample_rate > 0) {
1573
3/4
✓ Branch 0 taken 1383 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 661 times.
✓ Branch 3 taken 722 times.
2044 int first = pkt->dts != AV_NOPTS_VALUE && sti->first_dts != AV_NOPTS_VALUE &&
1574
2/2
✓ Branch 0 taken 228 times.
✓ Branch 1 taken 433 times.
661 pkt->dts <= sti->first_dts;
1575 4149 int in_priming = pkt->pts != AV_NOPTS_VALUE &&
1576
3/4
✓ Branch 0 taken 1383 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 879 times.
✓ Branch 3 taken 504 times.
2262 st->start_time != AV_NOPTS_VALUE &&
1577
2/2
✓ Branch 0 taken 54 times.
✓ Branch 1 taken 825 times.
879 pkt->pts < st->start_time;
1578 1383 int64_t skip = 0;
1579
1580 // The priming ends at the start time.
1581
2/2
✓ Branch 0 taken 54 times.
✓ Branch 1 taken 1329 times.
1383 if (in_priming)
1582 54 skip = ts_to_samples(st, st->start_time - pkt->pts);
1583
2/2
✓ Branch 0 taken 228 times.
✓ Branch 1 taken 1155 times.
1383 if (first) {
1584
2/2
✓ Branch 0 taken 46 times.
✓ Branch 1 taken 182 times.
228 if (par->initial_padding > 0)
1585 46 skip = par->initial_padding;
1586
5/6
✓ Branch 0 taken 79 times.
✓ Branch 1 taken 1076 times.
✓ Branch 2 taken 79 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 17 times.
✓ Branch 5 taken 62 times.
1155 } else if (!(sti->seek_to_start && pkt->pts != AV_NOPTS_VALUE && !in_priming)) {
1587 // Past the first packet, the decoder also misses what
1588 // came before.
1589 1093 skip = FFMAX(skip, par->seek_preroll);
1590 }
1591 1383 sti->skip_samples = FFMAX(sti->skip_samples, FFMIN(skip, INT_MAX));
1592 }
1593 2882 sti->seek_discard = 0;
1594 }
1595
6/6
✓ Branch 0 taken 10191 times.
✓ Branch 1 taken 652885 times.
✓ Branch 2 taken 10173 times.
✓ Branch 3 taken 18 times.
✓ Branch 4 taken 140 times.
✓ Branch 5 taken 10033 times.
663076 if (sti->start_skip_samples && (pkt->pts == 0 || pkt->pts == RELATIVE_TS_BASE))
1596 158 sti->skip_samples = sti->start_skip_samples;
1597 663076 sti->skip_samples = FFMAX(0, sti->skip_samples);
1598
4/4
✓ Branch 0 taken 662589 times.
✓ Branch 1 taken 487 times.
✓ Branch 2 taken 134 times.
✓ Branch 3 taken 662455 times.
663076 if (sti->skip_samples || discard_padding) {
1599 size_t size;
1600 621 uint8_t *p = av_packet_get_side_data(pkt, AV_PKT_DATA_SKIP_SAMPLES, &size);
1601 // Keep what the demuxer set, such as the padding at the end.
1602
3/4
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 579 times.
✓ Branch 2 taken 42 times.
✗ Branch 3 not taken.
621 if (p && size >= 10) {
1603 42 sti->skip_samples = FFMAX(sti->skip_samples, (int)AV_RL32(p));
1604 42 discard_padding = FFMAX(discard_padding, AV_RL32(p + 4));
1605 } else
1606 579 p = av_packet_new_side_data(pkt, AV_PKT_DATA_SKIP_SAMPLES, 10);
1607
1/2
✓ Branch 0 taken 621 times.
✗ Branch 1 not taken.
621 if (p) {
1608 621 AV_WL32(p, sti->skip_samples);
1609 621 AV_WL32(p + 4, discard_padding);
1610 621 av_log(s, AV_LOG_DEBUG, "demuxer injecting skip %u / discard %u\n",
1611 621 (unsigned)sti->skip_samples, (unsigned)discard_padding);
1612 }
1613 621 sti->skip_samples = 0;
1614 }
1615 }
1616
1617
2/2
✓ Branch 0 taken 8453 times.
✓ Branch 1 taken 660887 times.
669340 if (!fci->metafree) {
1618 8453 int metaret = av_opt_get_dict_val(s, "metadata", AV_OPT_SEARCH_CHILDREN, &metadata);
1619
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8453 times.
8453 if (metadata) {
1620 ✗ s->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
1621 ✗ av_dict_copy(&s->metadata, metadata, 0);
1622 ✗ av_dict_free(&metadata);
1623 ✗ av_opt_set_dict_val(s, "metadata", NULL, AV_OPT_SEARCH_CHILDREN);
1624 }
1625 8453 fci->metafree = metaret == AVERROR_OPTION_NOT_FOUND;
1626 }
1627
1628
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 669340 times.
669340 if (s->debug & AV_FDEBUG_TS)
1629 ✗ av_log(s, AV_LOG_DEBUG,
1630 "read_frame_internal stream=%d, pts=%s, dts=%s, "
1631 "size=%d, duration=%"PRId64", flags=%d\n",
1632 pkt->stream_index,
1633 ✗ av_ts2str(pkt->pts),
1634 ✗ av_ts2str(pkt->dts),
1635 pkt->size, pkt->duration, pkt->flags);
1636
1637 /* A demuxer might have returned EOF because of an IO error, let's
1638 * propagate this back to the user. */
1639
5/8
✓ Branch 0 taken 6154 times.
✓ Branch 1 taken 663186 times.
✓ Branch 2 taken 5901 times.
✓ Branch 3 taken 253 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 5901 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
669340 if (ret == AVERROR_EOF && s->pb && s->pb->error < 0 && s->pb->error != AVERROR(EAGAIN))
1640 ✗ ret = s->pb->error;
1641
1642 669340 return ret;
1643 }
1644
1645 563268 int av_read_frame(AVFormatContext *s, AVPacket *pkt)
1646 {
1647 563268 FFFormatContext *const si = ffformatcontext(s);
1648 563268 const int genpts = s->flags & AVFMT_FLAG_GENPTS;
1649 563268 int eof = 0;
1650 int ret;
1651 AVStream *st;
1652
1653
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 563268 times.
563268 if (!genpts) {
1654 1126536 ret = si->packet_buffer.head
1655 104327 ? ff_packet_list_get(&si->packet_buffer, pkt)
1656
2/2
✓ Branch 0 taken 104327 times.
✓ Branch 1 taken 458941 times.
563268 : read_frame_internal(s, pkt);
1657
2/2
✓ Branch 0 taken 4809 times.
✓ Branch 1 taken 558459 times.
563268 if (ret < 0)
1658 4809 return ret;
1659 558459 goto return_packet;
1660 }
1661
1662 ✗ for (;;) {
1663 ✗ PacketListEntry *pktl = si->packet_buffer.head;
1664
1665 ✗ if (pktl) {
1666 ✗ AVPacket *next_pkt = &pktl->pkt;
1667
1668 ✗ if (next_pkt->dts != AV_NOPTS_VALUE) {
1669 ✗ int wrap_bits = s->streams[next_pkt->stream_index]->pts_wrap_bits;
1670 // last dts seen for this stream. if any of packets following
1671 // current one had no dts, we will set this to AV_NOPTS_VALUE.
1672 ✗ int64_t last_dts = next_pkt->dts;
1673 av_assert2(wrap_bits <= 64);
1674 ✗ while (pktl && next_pkt->pts == AV_NOPTS_VALUE) {
1675 ✗ if (pktl->pkt.stream_index == next_pkt->stream_index &&
1676 ✗ av_compare_mod(next_pkt->dts, pktl->pkt.dts, 2ULL << (wrap_bits - 1)) < 0) {
1677 ✗ if (av_compare_mod(pktl->pkt.pts, pktl->pkt.dts, 2ULL << (wrap_bits - 1))) {
1678 // not B-frame
1679 ✗ next_pkt->pts = pktl->pkt.dts;
1680 }
1681 ✗ if (last_dts != AV_NOPTS_VALUE) {
1682 // Once last dts was set to AV_NOPTS_VALUE, we don't change it.
1683 ✗ last_dts = pktl->pkt.dts;
1684 }
1685 }
1686 ✗ pktl = pktl->next;
1687 }
1688 ✗ if (eof && next_pkt->pts == AV_NOPTS_VALUE && last_dts != AV_NOPTS_VALUE) {
1689 // Fixing the last reference frame had none pts issue (For MXF etc).
1690 // We only do this when
1691 // 1. eof.
1692 // 2. we are not able to resolve a pts value for current packet.
1693 // 3. the packets for this stream at the end of the files had valid dts.
1694 ✗ next_pkt->pts = last_dts + next_pkt->duration;
1695 }
1696 ✗ pktl = si->packet_buffer.head;
1697 }
1698
1699 /* read packet from packet buffer, if there is data */
1700 ✗ st = s->streams[next_pkt->stream_index];
1701 ✗ if (!(next_pkt->pts == AV_NOPTS_VALUE && st->discard < AVDISCARD_ALL &&
1702 ✗ next_pkt->dts != AV_NOPTS_VALUE && !eof)) {
1703 ✗ ret = ff_packet_list_get(&si->packet_buffer, pkt);
1704 ✗ goto return_packet;
1705 }
1706 }
1707
1708 ✗ ret = read_frame_internal(s, pkt);
1709 ✗ if (ret < 0) {
1710 ✗ if (pktl && ret != AVERROR(EAGAIN)) {
1711 ✗ eof = 1;
1712 ✗ continue;
1713 } else
1714 ✗ return ret;
1715 }
1716
1717 ✗ ret = ff_packet_list_put(&si->packet_buffer,
1718 pkt, NULL, 0);
1719 ✗ if (ret < 0) {
1720 ✗ av_packet_unref(pkt);
1721 ✗ return ret;
1722 }
1723 }
1724
1725 558459 return_packet:
1726 558459 st = s->streams[pkt->stream_index];
1727
4/4
✓ Branch 0 taken 247337 times.
✓ Branch 1 taken 311122 times.
✓ Branch 2 taken 172111 times.
✓ Branch 3 taken 75226 times.
558459 if ((s->iformat->flags & AVFMT_GENERIC_INDEX) && pkt->flags & AV_PKT_FLAG_KEY) {
1728 172111 ff_reduce_index(s, st->index);
1729 172111 av_add_index_entry(st, pkt->pos, pkt->dts, 0, 0, AVINDEX_KEYFRAME);
1730 }
1731
1732
2/2
✓ Branch 1 taken 161750 times.
✓ Branch 2 taken 396709 times.
558459 if (is_relative(pkt->dts))
1733 161750 pkt->dts -= RELATIVE_TS_BASE;
1734
2/2
✓ Branch 1 taken 160057 times.
✓ Branch 2 taken 398402 times.
558459 if (is_relative(pkt->pts))
1735 160057 pkt->pts -= RELATIVE_TS_BASE;
1736
1737 558459 return ret;
1738 }
1739
1740 /**
1741 * Return TRUE if the stream has accurate duration in any stream.
1742 *
1743 * @return TRUE if the stream has accurate duration for at least one component.
1744 */
1745 8348 static int has_duration(AVFormatContext *ic)
1746 {
1747
2/2
✓ Branch 0 taken 8938 times.
✓ Branch 1 taken 2780 times.
11718 for (unsigned i = 0; i < ic->nb_streams; i++) {
1748 8938 const AVStream *const st = ic->streams[i];
1749
2/2
✓ Branch 0 taken 5568 times.
✓ Branch 1 taken 3370 times.
8938 if (st->duration != AV_NOPTS_VALUE)
1750 5568 return 1;
1751 }
1752
2/2
✓ Branch 0 taken 542 times.
✓ Branch 1 taken 2238 times.
2780 if (ic->duration != AV_NOPTS_VALUE)
1753 542 return 1;
1754 2238 return 0;
1755 }
1756
1757 /**
1758 * Estimate the stream timings from the one of each components.
1759 *
1760 * Also computes the global bitrate if possible.
1761 */
1762 14666 static void update_stream_timings(AVFormatContext *ic)
1763 {
1764 int64_t start_time, start_time1, start_time_text, end_time, end_time1, end_time_text;
1765 int64_t duration, duration1, duration_text, filesize;
1766
1767 14666 start_time = INT64_MAX;
1768 14666 start_time_text = INT64_MAX;
1769 14666 end_time = INT64_MIN;
1770 14666 end_time_text = INT64_MIN;
1771 14666 duration = INT64_MIN;
1772 14666 duration_text = INT64_MIN;
1773
1774
2/2
✓ Branch 0 taken 16727 times.
✓ Branch 1 taken 14666 times.
31393 for (unsigned i = 0; i < ic->nb_streams; i++) {
1775 16727 AVStream *const st = ic->streams[i];
1776
2/2
✓ Branch 0 taken 16580 times.
✓ Branch 1 taken 147 times.
33307 int is_text = st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE ||
1777
2/2
✓ Branch 0 taken 187 times.
✓ Branch 1 taken 16393 times.
16580 st->codecpar->codec_type == AVMEDIA_TYPE_DATA;
1778
1779
3/4
✓ Branch 0 taken 13653 times.
✓ Branch 1 taken 3074 times.
✓ Branch 2 taken 13653 times.
✗ Branch 3 not taken.
16727 if (st->start_time != AV_NOPTS_VALUE && st->time_base.den) {
1780 13653 start_time1 = av_rescale_q(st->start_time, st->time_base,
1781 13653 AV_TIME_BASE_Q);
1782
2/2
✓ Branch 0 taken 194 times.
✓ Branch 1 taken 13459 times.
13653 if (is_text)
1783 194 start_time_text = FFMIN(start_time_text, start_time1);
1784 else
1785 13459 start_time = FFMIN(start_time, start_time1);
1786 13653 end_time1 = av_rescale_q_rnd(st->duration, st->time_base,
1787 13653 AV_TIME_BASE_Q,
1788 AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
1789
5/6
✓ Branch 0 taken 11739 times.
✓ Branch 1 taken 1914 times.
✓ Branch 2 taken 11710 times.
✓ Branch 3 taken 29 times.
✓ Branch 4 taken 11739 times.
✗ Branch 5 not taken.
13653 if (end_time1 != AV_NOPTS_VALUE && (end_time1 > 0 ? start_time1 <= INT64_MAX - end_time1 : start_time1 >= INT64_MIN - end_time1)) {
1790 11739 end_time1 += start_time1;
1791
2/2
✓ Branch 0 taken 173 times.
✓ Branch 1 taken 11566 times.
11739 if (is_text)
1792 173 end_time_text = FFMAX(end_time_text, end_time1);
1793 else
1794 11566 end_time = FFMAX(end_time, end_time1);
1795 }
1796
2/2
✓ Branch 1 taken 522 times.
✓ Branch 2 taken 13653 times.
14175 for (AVProgram *p = NULL; (p = av_find_program_from_stream(ic, p, i)); ) {
1797
4/4
✓ Branch 0 taken 424 times.
✓ Branch 1 taken 98 times.
✓ Branch 2 taken 28 times.
✓ Branch 3 taken 396 times.
522 if (p->start_time == AV_NOPTS_VALUE || p->start_time > start_time1)
1798 126 p->start_time = start_time1;
1799
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 438 times.
522 if (p->end_time < end_time1)
1800 84 p->end_time = end_time1;
1801 }
1802 }
1803
2/2
✓ Branch 0 taken 13452 times.
✓ Branch 1 taken 3275 times.
16727 if (st->duration != AV_NOPTS_VALUE) {
1804 13452 duration1 = av_rescale_q(st->duration, st->time_base,
1805 13452 AV_TIME_BASE_Q);
1806
2/2
✓ Branch 0 taken 203 times.
✓ Branch 1 taken 13249 times.
13452 if (is_text)
1807 203 duration_text = FFMAX(duration_text, duration1);
1808 else
1809 13249 duration = FFMAX(duration, duration1);
1810 }
1811 }
1812
3/6
✓ Branch 0 taken 11932 times.
✓ Branch 1 taken 2734 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 11932 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
14666 if (start_time == INT64_MAX || (start_time > start_time_text && start_time - (uint64_t)start_time_text < AV_TIME_BASE))
1813 2734 start_time = start_time_text;
1814
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11932 times.
11932 else if (start_time > start_time_text)
1815 ✗ av_log(ic, AV_LOG_VERBOSE, "Ignoring outlier non primary stream starttime %f\n", start_time_text / (float)AV_TIME_BASE);
1816
1817
6/6
✓ Branch 0 taken 10752 times.
✓ Branch 1 taken 3914 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 10746 times.
✓ Branch 4 taken 4 times.
✓ Branch 5 taken 2 times.
14666 if (end_time == INT64_MIN || (end_time < end_time_text && end_time_text - (uint64_t)end_time < AV_TIME_BASE))
1818 3918 end_time = end_time_text;
1819
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 10746 times.
10748 else if (end_time < end_time_text)
1820 2 av_log(ic, AV_LOG_VERBOSE, "Ignoring outlier non primary stream endtime %f\n", end_time_text / (float)AV_TIME_BASE);
1821
1822
6/6
✓ Branch 0 taken 12342 times.
✓ Branch 1 taken 2324 times.
✓ Branch 2 taken 15 times.
✓ Branch 3 taken 12327 times.
✓ Branch 4 taken 13 times.
✓ Branch 5 taken 2 times.
14666 if (duration == INT64_MIN || (duration < duration_text && (uint64_t)duration_text - duration < AV_TIME_BASE))
1823 2337 duration = duration_text;
1824
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 12327 times.
12329 else if (duration < duration_text)
1825 2 av_log(ic, AV_LOG_VERBOSE, "Ignoring outlier non primary stream duration %f\n", duration_text / (float)AV_TIME_BASE);
1826
1827
2/2
✓ Branch 0 taken 11955 times.
✓ Branch 1 taken 2711 times.
14666 if (start_time != INT64_MAX) {
1828 11955 ic->start_time = start_time;
1829
2/2
✓ Branch 0 taken 10769 times.
✓ Branch 1 taken 1186 times.
11955 if (end_time != INT64_MIN) {
1830
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10769 times.
10769 if (ic->nb_programs > 1) {
1831 ✗ for (unsigned i = 0; i < ic->nb_programs; i++) {
1832 ✗ AVProgram *const p = ic->programs[i];
1833
1834 ✗ if (p->start_time != AV_NOPTS_VALUE &&
1835 ✗ p->end_time > p->start_time &&
1836 ✗ p->end_time - (uint64_t)p->start_time <= INT64_MAX)
1837 ✗ duration = FFMAX(duration, p->end_time - p->start_time);
1838 }
1839
2/4
✓ Branch 0 taken 10769 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10769 times.
✗ Branch 3 not taken.
10769 } else if (end_time >= start_time && end_time - (uint64_t)start_time <= INT64_MAX) {
1840 10769 duration = FFMAX(duration, end_time - start_time);
1841 }
1842 }
1843 }
1844
6/6
✓ Branch 0 taken 12372 times.
✓ Branch 1 taken 2294 times.
✓ Branch 2 taken 12336 times.
✓ Branch 3 taken 36 times.
✓ Branch 4 taken 6607 times.
✓ Branch 5 taken 5729 times.
14666 if (duration != INT64_MIN && duration > 0 && ic->duration == AV_NOPTS_VALUE) {
1845 6607 ic->duration = duration;
1846 }
1847
6/6
✓ Branch 0 taken 8335 times.
✓ Branch 1 taken 6331 times.
✓ Branch 3 taken 8333 times.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 7101 times.
✓ Branch 6 taken 1232 times.
14666 if (ic->pb && (filesize = avio_size(ic->pb)) > 0 && ic->duration > 0) {
1848 /* compute the bitrate */
1849 7101 double bitrate = (double) filesize * 8.0 * AV_TIME_BASE /
1850 7101 (double) ic->duration;
1851
2/4
✓ Branch 0 taken 7101 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7101 times.
✗ Branch 3 not taken.
7101 if (bitrate >= 0 && bitrate <= INT64_MAX)
1852 7101 ic->bit_rate = bitrate;
1853 }
1854 14666 }
1855
1856 6214 static void fill_all_stream_timings(AVFormatContext *ic)
1857 {
1858 6214 update_stream_timings(ic);
1859
2/2
✓ Branch 0 taken 7086 times.
✓ Branch 1 taken 6214 times.
13300 for (unsigned i = 0; i < ic->nb_streams; i++) {
1860 7086 AVStream *const st = ic->streams[i];
1861
1862
2/2
✓ Branch 0 taken 879 times.
✓ Branch 1 taken 6207 times.
7086 if (st->start_time == AV_NOPTS_VALUE) {
1863
2/2
✓ Branch 0 taken 159 times.
✓ Branch 1 taken 720 times.
879 if (ic->start_time != AV_NOPTS_VALUE)
1864 159 st->start_time = av_rescale_q(ic->start_time, AV_TIME_BASE_Q,
1865 st->time_base);
1866
2/2
✓ Branch 0 taken 864 times.
✓ Branch 1 taken 15 times.
879 if (ic->duration != AV_NOPTS_VALUE)
1867 864 st->duration = av_rescale_q(ic->duration, AV_TIME_BASE_Q,
1868 st->time_base);
1869 }
1870 }
1871 6214 }
1872
1873 2238 static void estimate_timings_from_bit_rate(AVFormatContext *ic)
1874 {
1875 2238 FFFormatContext *const si = ffformatcontext(ic);
1876 2238 int show_warning = 0;
1877
1878 /* if bit_rate is already set, we believe it */
1879
2/2
✓ Branch 0 taken 2203 times.
✓ Branch 1 taken 35 times.
2238 if (ic->bit_rate <= 0) {
1880 2203 int64_t bit_rate = 0;
1881
2/2
✓ Branch 0 taken 2496 times.
✓ Branch 1 taken 1517 times.
4013 for (unsigned i = 0; i < ic->nb_streams; i++) {
1882 2496 const AVStream *const st = ic->streams[i];
1883 2496 const FFStream *const sti = cffstream(st);
1884
4/4
✓ Branch 0 taken 1632 times.
✓ Branch 1 taken 864 times.
✓ Branch 2 taken 215 times.
✓ Branch 3 taken 1417 times.
2496 if (st->codecpar->bit_rate <= 0 && sti->avctx->bit_rate > 0)
1885 215 st->codecpar->bit_rate = sti->avctx->bit_rate;
1886
2/2
✓ Branch 0 taken 1079 times.
✓ Branch 1 taken 1417 times.
2496 if (st->codecpar->bit_rate > 0) {
1887
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1079 times.
1079 if (INT64_MAX - st->codecpar->bit_rate < bit_rate) {
1888 ✗ bit_rate = 0;
1889 ✗ break;
1890 }
1891 1079 bit_rate += st->codecpar->bit_rate;
1892
4/4
✓ Branch 0 taken 1194 times.
✓ Branch 1 taken 223 times.
✓ Branch 2 taken 686 times.
✓ Branch 3 taken 508 times.
1417 } else if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && sti->codec_info_nb_frames > 1) {
1893 // If we have a videostream with packets but without a bitrate
1894 // then consider the sum not known
1895 686 bit_rate = 0;
1896 686 break;
1897 }
1898 }
1899 2203 ic->bit_rate = bit_rate;
1900 }
1901
1902 /* if duration is already set, we believe it */
1903
1/2
✓ Branch 0 taken 2238 times.
✗ Branch 1 not taken.
2238 if (ic->duration == AV_NOPTS_VALUE &&
1904
2/2
✓ Branch 0 taken 1011 times.
✓ Branch 1 taken 1227 times.
2238 ic->bit_rate != 0) {
1905
2/2
✓ Branch 0 taken 970 times.
✓ Branch 1 taken 41 times.
1011 int64_t filesize = ic->pb ? avio_size(ic->pb) : 0;
1906
2/2
✓ Branch 0 taken 953 times.
✓ Branch 1 taken 58 times.
1011 if (filesize > si->data_offset) {
1907 953 filesize -= si->data_offset;
1908
2/2
✓ Branch 0 taken 1067 times.
✓ Branch 1 taken 953 times.
2020 for (unsigned i = 0; i < ic->nb_streams; i++) {
1909 1067 AVStream *const st = ic->streams[i];
1910
1911
1/2
✓ Branch 0 taken 1067 times.
✗ Branch 1 not taken.
1067 if ( st->time_base.num <= INT64_MAX / ic->bit_rate
1912
1/2
✓ Branch 0 taken 1067 times.
✗ Branch 1 not taken.
1067 && st->duration == AV_NOPTS_VALUE) {
1913 1067 st->duration = av_rescale(filesize, 8LL * st->time_base.den,
1914 1067 ic->bit_rate *
1915 1067 (int64_t) st->time_base.num);
1916 1067 show_warning = 1;
1917 }
1918 }
1919 }
1920 }
1921
2/2
✓ Branch 0 taken 953 times.
✓ Branch 1 taken 1285 times.
2238 if (show_warning)
1922 953 av_log(ic, AV_LOG_WARNING,
1923 "Estimating duration from bitrate, this may be inaccurate\n");
1924 2238 }
1925
1926 #define DURATION_DEFAULT_MAX_READ_SIZE 250000LL
1927 #define DURATION_DEFAULT_MAX_RETRY 6
1928 #define DURATION_MAX_RETRY 1
1929
1930 /* only usable for MPEG-PS streams */
1931 104 static void estimate_timings_from_pts(AVFormatContext *ic, int64_t old_offset)
1932 {
1933 104 FFFormatContext *const si = ffformatcontext(ic);
1934 104 AVPacket *const pkt = si->pkt;
1935 int num, den, read_size, ret;
1936
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 104 times.
104 int64_t duration_max_read_size = ic->duration_probesize ? ic->duration_probesize >> DURATION_MAX_RETRY : DURATION_DEFAULT_MAX_READ_SIZE;
1937
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 104 times.
104 int duration_max_retry = ic->duration_probesize ? DURATION_MAX_RETRY : DURATION_DEFAULT_MAX_RETRY;
1938 104 int found_duration = 0;
1939 int is_end;
1940 int64_t filesize, offset, duration;
1941 104 int retry = 0;
1942
1943 /* flush packet queue */
1944 104 ff_flush_packet_queue(ic);
1945
1946
2/2
✓ Branch 0 taken 195 times.
✓ Branch 1 taken 104 times.
299 for (unsigned i = 0; i < ic->nb_streams; i++) {
1947 195 AVStream *const st = ic->streams[i];
1948 195 FFStream *const sti = ffstream(st);
1949
1950
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 168 times.
195 if (st->start_time == AV_NOPTS_VALUE &&
1951
1/2
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
27 sti->first_dts == AV_NOPTS_VALUE &&
1952
1/2
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
27 st->codecpar->codec_type != AVMEDIA_TYPE_UNKNOWN)
1953 27 av_log(ic, AV_LOG_WARNING,
1954 "start time for stream %d is not set in estimate_timings_from_pts\n", i);
1955
1956
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 166 times.
195 if (sti->parser) {
1957 29 av_parser_close(sti->parser);
1958 29 sti->parser = NULL;
1959 }
1960 }
1961
1962
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 104 times.
104 if (ic->skip_estimate_duration_from_pts) {
1963 ✗ av_log(ic, AV_LOG_INFO, "Skipping duration calculation in estimate_timings_from_pts\n");
1964 ✗ goto skip_duration_calc;
1965 }
1966
1967 104 av_opt_set_int(ic, "skip_changes", 1, AV_OPT_SEARCH_CHILDREN);
1968 /* estimate the end time (duration) */
1969 /* XXX: may need to support wrapping */
1970
1/2
✓ Branch 0 taken 104 times.
✗ Branch 1 not taken.
104 filesize = ic->pb ? avio_size(ic->pb) : 0;
1971 do {
1972 111 is_end = found_duration;
1973 111 offset = filesize - (duration_max_read_size << retry);
1974
2/2
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 60 times.
111 if (offset < 0)
1975 51 offset = 0;
1976
1977 111 avio_seek(ic->pb, offset, SEEK_SET);
1978 111 read_size = 0;
1979 9332 for (;;) {
1980 AVStream *st;
1981 FFStream *sti;
1982
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 9436 times.
9443 if (read_size >= duration_max_read_size << (FFMAX(retry - 1, 0)))
1983 7 break;
1984
1985 do {
1986 9436 ret = ff_read_packet(ic, pkt);
1987
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9436 times.
9436 } while (ret == AVERROR(EAGAIN));
1988
2/2
✓ Branch 0 taken 104 times.
✓ Branch 1 taken 9332 times.
9436 if (ret != 0)
1989 104 break;
1990 9332 read_size += pkt->size;
1991 9332 st = ic->streams[pkt->stream_index];
1992 9332 sti = ffstream(st);
1993
2/2
✓ Branch 0 taken 5227 times.
✓ Branch 1 taken 4105 times.
9332 if (pkt->pts != AV_NOPTS_VALUE &&
1994
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5227 times.
5227 (st->start_time != AV_NOPTS_VALUE ||
1995 ✗ sti->first_dts != AV_NOPTS_VALUE)) {
1996
1/2
✓ Branch 0 taken 5227 times.
✗ Branch 1 not taken.
5227 if (pkt->duration == 0) {
1997 5227 compute_frame_duration(ic, &num, &den, st, sti->parser, pkt);
1998
3/4
✓ Branch 0 taken 3755 times.
✓ Branch 1 taken 1472 times.
✓ Branch 2 taken 3755 times.
✗ Branch 3 not taken.
5227 if (den && num) {
1999 3755 pkt->duration = av_rescale_rnd(1,
2000 3755 num * (int64_t) st->time_base.den,
2001 3755 den * (int64_t) st->time_base.num,
2002 AV_ROUND_DOWN);
2003 }
2004 }
2005 5227 duration = pkt->pts + pkt->duration;
2006 5227 found_duration = 1;
2007
1/2
✓ Branch 0 taken 5227 times.
✗ Branch 1 not taken.
5227 if (st->start_time != AV_NOPTS_VALUE)
2008 5227 duration -= st->start_time;
2009 else
2010 ✗ duration -= sti->first_dts;
2011
2/2
✓ Branch 0 taken 5214 times.
✓ Branch 1 taken 13 times.
5227 if (duration > 0) {
2012
3/4
✓ Branch 0 taken 5056 times.
✓ Branch 1 taken 158 times.
✓ Branch 2 taken 5056 times.
✗ Branch 3 not taken.
5214 if (st->duration == AV_NOPTS_VALUE || sti->info->last_duration<= 0 ||
2013
3/4
✓ Branch 0 taken 4164 times.
✓ Branch 1 taken 892 times.
✓ Branch 2 taken 4164 times.
✗ Branch 3 not taken.
5056 (st->duration < duration && FFABS(duration - sti->info->last_duration) < 60LL*st->time_base.den / st->time_base.num))
2014 4322 st->duration = duration;
2015 5214 sti->info->last_duration = duration;
2016 }
2017 }
2018 9332 av_packet_unref(pkt);
2019 }
2020
2021 /* check if all audio/video streams have valid duration */
2022
2/2
✓ Branch 0 taken 104 times.
✓ Branch 1 taken 7 times.
111 if (!is_end) {
2023 104 is_end = 1;
2024
2/2
✓ Branch 0 taken 195 times.
✓ Branch 1 taken 104 times.
299 for (unsigned i = 0; i < ic->nb_streams; i++) {
2025 195 const AVStream *const st = ic->streams[i];
2026
2/2
✓ Branch 0 taken 173 times.
✓ Branch 1 taken 22 times.
195 switch (st->codecpar->codec_type) {
2027 173 case AVMEDIA_TYPE_VIDEO:
2028 case AVMEDIA_TYPE_AUDIO:
2029
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 148 times.
173 if (st->duration == AV_NOPTS_VALUE)
2030 25 is_end = 0;
2031 }
2032 }
2033 }
2034
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 3 times.
10 } while (!is_end &&
2035
3/4
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 101 times.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
118 offset &&
2036 ++retry <= duration_max_retry);
2037
2038 104 av_opt_set_int(ic, "skip_changes", 0, AV_OPT_SEARCH_CHILDREN);
2039
2040 /* warn about audio/video streams which duration could not be estimated */
2041
2/2
✓ Branch 0 taken 195 times.
✓ Branch 1 taken 104 times.
299 for (unsigned i = 0; i < ic->nb_streams; i++) {
2042 195 const AVStream *const st = ic->streams[i];
2043 195 const FFStream *const sti = cffstream(st);
2044
2045
2/2
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 158 times.
195 if (st->duration == AV_NOPTS_VALUE) {
2046
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 19 times.
37 switch (st->codecpar->codec_type) {
2047 18 case AVMEDIA_TYPE_VIDEO:
2048 case AVMEDIA_TYPE_AUDIO:
2049
3/4
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 11 times.
18 if (st->start_time != AV_NOPTS_VALUE || sti->first_dts != AV_NOPTS_VALUE) {
2050 7 av_log(ic, AV_LOG_WARNING, "stream %d : no PTS found at end of file, duration not set\n", i);
2051 } else
2052 11 av_log(ic, AV_LOG_WARNING, "stream %d : no TS found at start of file, duration not set\n", i);
2053 }
2054 }
2055 }
2056 104 skip_duration_calc:
2057 104 fill_all_stream_timings(ic);
2058
2059 104 avio_seek(ic->pb, old_offset, SEEK_SET);
2060
2/2
✓ Branch 0 taken 195 times.
✓ Branch 1 taken 104 times.
299 for (unsigned i = 0; i < ic->nb_streams; i++) {
2061 195 AVStream *const st = ic->streams[i];
2062 195 FFStream *const sti = ffstream(st);
2063
2064 195 sti->cur_dts = sti->first_dts;
2065 195 sti->last_IP_pts = AV_NOPTS_VALUE;
2066 195 sti->last_dts_for_order_check = AV_NOPTS_VALUE;
2067
2/2
✓ Branch 0 taken 3315 times.
✓ Branch 1 taken 195 times.
3510 for (int j = 0; j < MAX_REORDER_DELAY + 1; j++)
2068 3315 sti->pts_buffer[j] = AV_NOPTS_VALUE;
2069 }
2070 104 }
2071
2072 /* 1:1 map to AVDurationEstimationMethod */
2073 static const char *const duration_name[] = {
2074 [AVFMT_DURATION_FROM_PTS] = "pts",
2075 [AVFMT_DURATION_FROM_STREAM] = "stream",
2076 [AVFMT_DURATION_FROM_BITRATE] = "bit rate",
2077 };
2078
2079 8452 static const char *duration_estimate_name(enum AVDurationEstimationMethod method)
2080 {
2081 8452 return duration_name[method];
2082 }
2083
2084 8452 static void estimate_timings(AVFormatContext *ic, int64_t old_offset)
2085 {
2086 int64_t file_size;
2087
2088 /* get the file size, if possible */
2089
2/2
✓ Branch 0 taken 3236 times.
✓ Branch 1 taken 5216 times.
8452 if (ic->iformat->flags & AVFMT_NOFILE) {
2090 3236 file_size = 0;
2091 } else {
2092 5216 file_size = avio_size(ic->pb);
2093 5216 file_size = FFMAX(0, file_size);
2094 }
2095
2096
2/2
✓ Branch 0 taken 8424 times.
✓ Branch 1 taken 28 times.
8452 if ((!strcmp(ic->iformat->name, "mpeg") ||
2097
4/4
✓ Branch 0 taken 78 times.
✓ Branch 1 taken 8346 times.
✓ Branch 2 taken 104 times.
✓ Branch 3 taken 2 times.
8452 !strcmp(ic->iformat->name, "mpegts")) &&
2098
1/2
✓ Branch 0 taken 104 times.
✗ Branch 1 not taken.
104 file_size && (ic->pb->seekable & AVIO_SEEKABLE_NORMAL)) {
2099 /* get accurate estimate from the PTSes */
2100 104 estimate_timings_from_pts(ic, old_offset);
2101 104 ic->duration_estimation_method = AVFMT_DURATION_FROM_PTS;
2102
2/2
✓ Branch 1 taken 6110 times.
✓ Branch 2 taken 2238 times.
8348 } else if (has_duration(ic)) {
2103 /* at least one component has timings - we use them for all
2104 * the components */
2105 6110 fill_all_stream_timings(ic);
2106 /* nut demuxer estimate the duration from PTS */
2107
2/2
✓ Branch 0 taken 69 times.
✓ Branch 1 taken 6041 times.
6110 if (!strcmp(ic->iformat->name, "nut"))
2108 69 ic->duration_estimation_method = AVFMT_DURATION_FROM_PTS;
2109 else
2110 6041 ic->duration_estimation_method = AVFMT_DURATION_FROM_STREAM;
2111 } else {
2112 /* less precise: use bitrate info */
2113 2238 estimate_timings_from_bit_rate(ic);
2114 2238 ic->duration_estimation_method = AVFMT_DURATION_FROM_BITRATE;
2115 }
2116 8452 update_stream_timings(ic);
2117
2118
2/2
✓ Branch 0 taken 9641 times.
✓ Branch 1 taken 8452 times.
18093 for (unsigned i = 0; i < ic->nb_streams; i++) {
2119 9641 AVStream *const st = ic->streams[i];
2120
1/2
✓ Branch 0 taken 9641 times.
✗ Branch 1 not taken.
9641 if (st->time_base.den)
2121 9641 av_log(ic, AV_LOG_TRACE, "stream %u: start_time: %s duration: %s\n", i,
2122 9641 av_ts2timestr(st->start_time, &st->time_base),
2123 9641 av_ts2timestr(st->duration, &st->time_base));
2124 }
2125 8452 av_log(ic, AV_LOG_TRACE,
2126 "format: start_time: %s duration: %s (estimate from %s) bitrate=%"PRId64" kb/s\n",
2127 8452 av_ts2timestr(ic->start_time, &AV_TIME_BASE_Q),
2128 8452 av_ts2timestr(ic->duration, &AV_TIME_BASE_Q),
2129 duration_estimate_name(ic->duration_estimation_method),
2130 8452 (int64_t)ic->bit_rate / 1000);
2131 8452 }
2132
2133 115983 static int determinable_frame_size(const AVCodecContext *avctx)
2134 {
2135
2/2
✓ Branch 0 taken 2032 times.
✓ Branch 1 taken 113951 times.
115983 switch(avctx->codec_id) {
2136 2032 case AV_CODEC_ID_MP1:
2137 case AV_CODEC_ID_MP2:
2138 case AV_CODEC_ID_MP3:
2139 case AV_CODEC_ID_CODEC2:
2140 2032 return 1;
2141 }
2142
2143 113951 return 0;
2144 }
2145
2146 531884 static int has_codec_parameters(const AVStream *st, const char **errmsg_ptr)
2147 {
2148 531884 const FFStream *const sti = cffstream(st);
2149 531884 const AVCodecContext *const avctx = sti->avctx;
2150
2151 #define FAIL(errmsg) do { \
2152 if (errmsg_ptr) \
2153 *errmsg_ptr = errmsg; \
2154 return 0; \
2155 } while (0)
2156
2157
2/2
✓ Branch 0 taken 762 times.
✓ Branch 1 taken 531122 times.
531884 if ( avctx->codec_id == AV_CODEC_ID_NONE
2158
2/2
✓ Branch 0 taken 513 times.
✓ Branch 1 taken 249 times.
762 && avctx->codec_type != AVMEDIA_TYPE_DATA)
2159
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 498 times.
513 FAIL("unknown codec");
2160
4/5
✓ Branch 0 taken 212285 times.
✓ Branch 1 taken 317378 times.
✓ Branch 2 taken 679 times.
✓ Branch 3 taken 1029 times.
✗ Branch 4 not taken.
531371 switch (avctx->codec_type) {
2161 212285 case AVMEDIA_TYPE_AUDIO:
2162
4/4
✓ Branch 0 taken 115983 times.
✓ Branch 1 taken 96302 times.
✓ Branch 3 taken 2032 times.
✓ Branch 4 taken 113951 times.
212285 if (!avctx->frame_size && determinable_frame_size(avctx))
2163
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2030 times.
2032 FAIL("unspecified frame size");
2164
2/2
✓ Branch 0 taken 210243 times.
✓ Branch 1 taken 10 times.
210253 if (sti->info->found_decoder >= 0 &&
2165
2/2
✓ Branch 0 taken 3096 times.
✓ Branch 1 taken 207147 times.
210243 avctx->sample_fmt == AV_SAMPLE_FMT_NONE)
2166
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3095 times.
3096 FAIL("unspecified sample format");
2167
2/2
✓ Branch 0 taken 222 times.
✓ Branch 1 taken 206935 times.
207157 if (!avctx->sample_rate)
2168
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 215 times.
222 FAIL("unspecified sample rate");
2169
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 206913 times.
206935 if (!avctx->ch_layout.nb_channels)
2170
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 FAIL("unspecified number of channels");
2171
6/6
✓ Branch 0 taken 206903 times.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 85183 times.
✓ Branch 3 taken 121720 times.
✓ Branch 4 taken 110 times.
✓ Branch 5 taken 85073 times.
206913 if (sti->info->found_decoder >= 0 && !sti->nb_decoded_frames && avctx->codec_id == AV_CODEC_ID_DTS)
2172
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 110 times.
110 FAIL("no decodable DTS frames");
2173 206803 break;
2174 317378 case AVMEDIA_TYPE_VIDEO:
2175
2/2
✓ Branch 0 taken 13460 times.
✓ Branch 1 taken 303918 times.
317378 if (!avctx->width)
2176
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 13448 times.
13460 FAIL("unspecified size");
2177
4/4
✓ Branch 0 taken 303570 times.
✓ Branch 1 taken 348 times.
✓ Branch 2 taken 5043 times.
✓ Branch 3 taken 298527 times.
303918 if (sti->info->found_decoder >= 0 && avctx->pix_fmt == AV_PIX_FMT_NONE)
2178
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 5040 times.
5043 FAIL("unspecified pixel format");
2179
4/4
✓ Branch 0 taken 298775 times.
✓ Branch 1 taken 100 times.
✓ Branch 2 taken 86 times.
✓ Branch 3 taken 298689 times.
298875 if (st->codecpar->codec_id == AV_CODEC_ID_RV30 || st->codecpar->codec_id == AV_CODEC_ID_RV40)
2180
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)
2181
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 67 times.
67 FAIL("no frame in rv30/40 and no sar");
2182 298808 break;
2183 679 case AVMEDIA_TYPE_SUBTITLE:
2184
4/4
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 644 times.
✓ Branch 2 taken 21 times.
✓ Branch 3 taken 14 times.
679 if (avctx->codec_id == AV_CODEC_ID_HDMV_PGS_SUBTITLE && !avctx->width)
2185
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 FAIL("unspecified size");
2186 658 break;
2187 1029 case AVMEDIA_TYPE_DATA:
2188
2/2
✓ Branch 0 taken 249 times.
✓ Branch 1 taken 780 times.
1029 if (avctx->codec_id == AV_CODEC_ID_NONE) return 1;
2189 }
2190
2191 507049 return 1;
2192 }
2193
2194 /* returns 1 or 0 if or if not decoded data was returned, or a negative error */
2195 213848 static int try_decode_frame(AVFormatContext *s, AVStream *st,
2196 const AVPacket *pkt, AVDictionary **options)
2197 {
2198 213848 FFStream *const sti = ffstream(st);
2199 213848 AVCodecContext *const avctx = sti->avctx;
2200 const AVCodec *codec;
2201 213848 int got_picture = 1, ret = 0;
2202 213848 AVFrame *frame = av_frame_alloc();
2203 AVSubtitle subtitle;
2204 213848 int do_skip_frame = 0;
2205 enum AVDiscard skip_frame;
2206 213848 int pkt_to_send = pkt->size > 0;
2207
2208
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 213848 times.
213848 if (!frame)
2209 ✗ return AVERROR(ENOMEM);
2210
2211
2/2
✓ Branch 1 taken 1930 times.
✓ Branch 2 taken 211918 times.
213848 if (!avcodec_is_open(avctx) &&
2212
1/2
✓ Branch 0 taken 1930 times.
✗ Branch 1 not taken.
1930 sti->info->found_decoder <= 0 &&
2213
4/4
✓ Branch 0 taken 368 times.
✓ Branch 1 taken 1562 times.
✓ Branch 2 taken 57 times.
✓ Branch 3 taken 311 times.
1930 (st->codecpar->codec_id != -sti->info->found_decoder || !st->codecpar->codec_id)) {
2214 1619 AVDictionary *thread_opt = NULL;
2215
2216 1619 codec = find_probe_decoder(s, st, st->codecpar->codec_id);
2217
2218
2/2
✓ Branch 0 taken 95 times.
✓ Branch 1 taken 1524 times.
1619 if (!codec) {
2219 95 sti->info->found_decoder = -st->codecpar->codec_id;
2220 95 ret = -1;
2221 97 goto fail;
2222 }
2223
2224 /* Force thread count to 1 since the H.264 decoder will not extract
2225 * SPS and PPS to extradata during multi-threaded decoding. */
2226
2/2
✓ Branch 0 taken 180 times.
✓ Branch 1 taken 1344 times.
1524 av_dict_set(options ? options : &thread_opt, "threads", "1", 0);
2227 /* Force lowres to 0. The decoder might reduce the video size by the
2228 * lowres factor, and we don't want that propagated to the stream's
2229 * codecpar */
2230
2/2
✓ Branch 0 taken 180 times.
✓ Branch 1 taken 1344 times.
1524 av_dict_set(options ? options : &thread_opt, "lowres", "0", 0);
2231
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1524 times.
1524 if (s->codec_whitelist)
2232 ✗ av_dict_set(options ? options : &thread_opt, "codec_whitelist", s->codec_whitelist, 0);
2233
2/2
✓ Branch 0 taken 180 times.
✓ Branch 1 taken 1344 times.
1524 ret = avcodec_open2(avctx, codec, options ? options : &thread_opt);
2234
2/2
✓ Branch 0 taken 180 times.
✓ Branch 1 taken 1344 times.
1524 if (!options)
2235 180 av_dict_free(&thread_opt);
2236
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1522 times.
1524 if (ret < 0) {
2237 2 sti->info->found_decoder = -avctx->codec_id;
2238 2 goto fail;
2239 }
2240 1522 sti->info->found_decoder = 1;
2241
2/2
✓ Branch 0 taken 7583 times.
✓ Branch 1 taken 204646 times.
212229 } else if (!sti->info->found_decoder)
2242 7583 sti->info->found_decoder = 1;
2243
2244
2/2
✓ Branch 0 taken 311 times.
✓ Branch 1 taken 213440 times.
213751 if (sti->info->found_decoder < 0) {
2245 311 ret = -1;
2246 311 goto fail;
2247 }
2248
2249
2/2
✓ Branch 1 taken 120667 times.
✓ Branch 2 taken 92773 times.
213440 if (avpriv_codec_get_cap_skip_frame_fill_param(avctx->codec)) {
2250 120667 do_skip_frame = 1;
2251 120667 skip_frame = avctx->skip_frame;
2252 120667 avctx->skip_frame = AVDISCARD_ALL;
2253 }
2254
2255
5/6
✓ Branch 0 taken 9016 times.
✓ Branch 1 taken 5045 times.
✓ Branch 2 taken 30 times.
✓ Branch 3 taken 5015 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 213448 times.
435934 while ((pkt_to_send || (!pkt->data && got_picture)) &&
2256
4/4
✓ Branch 0 taken 14061 times.
✓ Branch 1 taken 208433 times.
✓ Branch 2 taken 6288 times.
✓ Branch 3 taken 207160 times.
435942 ret >= 0 &&
2257
2/2
✓ Branch 2 taken 2364 times.
✓ Branch 3 taken 204796 times.
420608 (!has_codec_parameters(st, NULL) || !has_decode_delay_been_guessed(st) ||
2258
2/2
✓ Branch 0 taken 201585 times.
✓ Branch 1 taken 3211 times.
204796 (!sti->codec_info_nb_frames &&
2259
2/2
✓ Branch 0 taken 652 times.
✓ Branch 1 taken 2559 times.
3211 (avctx->codec->capabilities & AV_CODEC_CAP_CHANNEL_CONF)))) {
2260 9304 got_picture = 0;
2261
2/2
✓ Branch 0 taken 1238 times.
✓ Branch 1 taken 8066 times.
9304 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO ||
2262
2/2
✓ Branch 0 taken 1231 times.
✓ Branch 1 taken 7 times.
1238 avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
2263 9297 ret = avcodec_send_packet(avctx, pkt);
2264
5/6
✓ Branch 0 taken 259 times.
✓ Branch 1 taken 9038 times.
✓ Branch 2 taken 259 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 250 times.
✓ Branch 5 taken 9 times.
9297 if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
2265 250 break;
2266
2/2
✓ Branch 0 taken 9038 times.
✓ Branch 1 taken 9 times.
9047 if (ret >= 0)
2267 9038 pkt_to_send = 0;
2268 9047 ret = avcodec_receive_frame(avctx, frame);
2269
2/2
✓ Branch 0 taken 3542 times.
✓ Branch 1 taken 5505 times.
9047 if (ret >= 0)
2270 3542 got_picture = 1;
2271
4/4
✓ Branch 0 taken 3572 times.
✓ Branch 1 taken 5475 times.
✓ Branch 2 taken 30 times.
✓ Branch 3 taken 3542 times.
9047 if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
2272 5505 ret = 0;
2273
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 } else if (avctx->codec_type == AVMEDIA_TYPE_SUBTITLE) {
2274 7 ret = avcodec_decode_subtitle2(avctx, &subtitle,
2275 &got_picture, pkt);
2276
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 5 times.
7 if (got_picture)
2277 2 avsubtitle_free(&subtitle);
2278
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 if (ret >= 0)
2279 7 pkt_to_send = 0;
2280 }
2281
1/2
✓ Branch 0 taken 9054 times.
✗ Branch 1 not taken.
9054 if (ret >= 0) {
2282
2/2
✓ Branch 0 taken 3544 times.
✓ Branch 1 taken 5510 times.
9054 if (got_picture)
2283 3544 sti->nb_decoded_frames++;
2284 9054 ret = got_picture;
2285 }
2286 }
2287
2288 213190 fail:
2289
2/2
✓ Branch 0 taken 120667 times.
✓ Branch 1 taken 93181 times.
213848 if (do_skip_frame) {
2290 120667 avctx->skip_frame = skip_frame;
2291 }
2292
2293 213848 av_frame_free(&frame);
2294 213848 return ret;
2295 }
2296
2297 89 static int chapter_start_cmp(const void *p1, const void *p2)
2298 {
2299 89 const AVChapter *const ch1 = *(AVChapter**)p1;
2300 89 const AVChapter *const ch2 = *(AVChapter**)p2;
2301 89 int delta = av_compare_ts(ch1->start, ch1->time_base, ch2->start, ch2->time_base);
2302
1/2
✓ Branch 0 taken 89 times.
✗ Branch 1 not taken.
89 if (delta)
2303 89 return delta;
2304 ✗ return FFDIFFSIGN(ch1->id, ch2->id);
2305 }
2306
2307 8452 static int compute_chapters_end(AVFormatContext *s)
2308 {
2309 8452 int64_t max_time = 0;
2310 AVChapter **timetable;
2311
2312
2/2
✓ Branch 0 taken 8395 times.
✓ Branch 1 taken 57 times.
8452 if (!s->nb_chapters)
2313 8395 return 0;
2314
2315
2/4
✓ Branch 0 taken 57 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 57 times.
✗ Branch 3 not taken.
57 if (s->duration > 0 && s->start_time < INT64_MAX - s->duration)
2316 57 max_time = s->duration +
2317
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 23 times.
57 ((s->start_time == AV_NOPTS_VALUE) ? 0 : s->start_time);
2318
2319 57 timetable = av_memdup(s->chapters, s->nb_chapters * sizeof(*timetable));
2320
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 57 times.
57 if (!timetable)
2321 ✗ return AVERROR(ENOMEM);
2322 57 qsort(timetable, s->nb_chapters, sizeof(*timetable), chapter_start_cmp);
2323
2324
2/2
✓ Branch 0 taken 134 times.
✓ Branch 1 taken 57 times.
191 for (unsigned i = 0; i < s->nb_chapters; i++)
2325
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 84 times.
134 if (timetable[i]->end == AV_NOPTS_VALUE) {
2326 50 AVChapter *const ch = timetable[i];
2327 50 int64_t end = max_time ? av_rescale_q(max_time, AV_TIME_BASE_Q,
2328 ch->time_base)
2329
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 : INT64_MAX;
2330
2331
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 29 times.
50 if (i + 1 < s->nb_chapters) {
2332 21 const AVChapter *const ch1 = timetable[i + 1];
2333 21 int64_t next_start = av_rescale_q(ch1->start, ch1->time_base,
2334 ch->time_base);
2335
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)
2336 21 end = next_start;
2337 }
2338
2/4
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 50 times.
50 ch->end = (end == INT64_MAX || end < ch->start) ? ch->start : end;
2339 }
2340 57 av_free(timetable);
2341 57 return 0;
2342 }
2343
2344 20030981 static int get_std_framerate(int i)
2345 {
2346
2/2
✓ Branch 0 taken 18191715 times.
✓ Branch 1 taken 1839266 times.
20030981 if (i < 30*12)
2347 18191715 return (i + 1) * 1001;
2348 1839266 i -= 30*12;
2349
2350
2/2
✓ Branch 0 taken 1396314 times.
✓ Branch 1 taken 442952 times.
1839266 if (i < 30)
2351 1396314 return (i + 31) * 1001 * 12;
2352 442952 i -= 30;
2353
2354
2/2
✓ Branch 0 taken 142472 times.
✓ Branch 1 taken 300480 times.
442952 if (i < 3)
2355 142472 return ((const int[]) { 80, 120, 240})[i] * 1001 * 12;
2356
2357 300480 i -= 3;
2358
2359 300480 return ((const int[]) { 24, 30, 60, 12, 15, 48 })[i] * 1000 * 12;
2360 }
2361
2362 /* Is the time base unreliable?
2363 * This is a heuristic to balance between quick acceptance of the values in
2364 * the headers vs. some extra checks.
2365 * Old DivX and Xvid often have nonsense timebases like 1fps or 2fps.
2366 * MPEG-2 commonly misuses field repeat flags to store different framerates.
2367 * And there are "variable" fps files this needs to detect as well. */
2368 295511 static int tb_unreliable(AVFormatContext *ic, AVStream *st)
2369 {
2370 295511 FFStream *const sti = ffstream(st);
2371 295511 const AVCodecDescriptor *desc = sti->codec_desc;
2372 295511 AVCodecContext *c = sti->avctx;
2373
4/4
✓ Branch 0 taken 294878 times.
✓ Branch 1 taken 633 times.
✓ Branch 2 taken 22961 times.
✓ Branch 3 taken 271917 times.
295511 AVRational mul = (AVRational){ desc && (desc->props & AV_CODEC_PROP_FIELDS) ? 2 : 1, 1 };
2374 295511 AVRational time_base = c->framerate.num ? av_inv_q(av_mul_q(c->framerate, mul))
2375 /* NOHEADER check added to not break existing behavior */
2376
2/2
✓ Branch 0 taken 20283 times.
✓ Branch 1 taken 275228 times.
295511 : (((ic->ctx_flags & AVFMTCTX_NOHEADER) ||
2377
2/2
✓ Branch 0 taken 121083 times.
✓ Branch 1 taken 33040 times.
154123 st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) ? (AVRational){0, 1}
2378
2/2
✓ Branch 0 taken 154123 times.
✓ Branch 1 taken 121105 times.
275228 : st->time_base);
2379
2380
2/2
✓ Branch 0 taken 26773 times.
✓ Branch 1 taken 268738 times.
295511 if (time_base.den >= 101LL * time_base.num ||
2381
2/2
✓ Branch 0 taken 26082 times.
✓ Branch 1 taken 691 times.
26773 time_base.den < 5LL * time_base.num ||
2382 // c->codec_tag == AV_RL32("DIVX") ||
2383 // c->codec_tag == AV_RL32("XVID") ||
2384
2/2
✓ Branch 0 taken 25871 times.
✓ Branch 1 taken 211 times.
26082 c->codec_tag == AV_RL32("mp4v") ||
2385
2/2
✓ Branch 0 taken 16344 times.
✓ Branch 1 taken 9527 times.
25871 c->codec_id == AV_CODEC_ID_MPEG2VIDEO ||
2386
2/2
✓ Branch 0 taken 15814 times.
✓ Branch 1 taken 530 times.
16344 c->codec_id == AV_CODEC_ID_GIF ||
2387
2/2
✓ Branch 0 taken 14431 times.
✓ Branch 1 taken 1383 times.
15814 c->codec_id == AV_CODEC_ID_HEVC ||
2388
2/2
✓ Branch 0 taken 4110 times.
✓ Branch 1 taken 10321 times.
14431 c->codec_id == AV_CODEC_ID_H264)
2389 285190 return 1;
2390 10321 return 0;
2391 }
2392
2393 150742 int ff_rfps_add_frame(AVFormatContext *ic, AVStream *st, int64_t ts)
2394 {
2395 150742 FFStream *const sti = ffstream(st);
2396 150742 FFStreamInfo *info = sti->info;
2397 150742 int64_t last = info->last_dts;
2398
2399
6/6
✓ Branch 0 taken 133688 times.
✓ Branch 1 taken 17054 times.
✓ Branch 2 taken 127054 times.
✓ Branch 3 taken 6634 times.
✓ Branch 4 taken 127027 times.
✓ Branch 5 taken 27 times.
150742 if ( ts != AV_NOPTS_VALUE && last != AV_NOPTS_VALUE && ts > last
2400
1/2
✓ Branch 0 taken 127027 times.
✗ Branch 1 not taken.
127027 && ts - (uint64_t)last < INT64_MAX) {
2401
2/2
✓ Branch 1 taken 6634 times.
✓ Branch 2 taken 120393 times.
127027 double dts = (is_relative(ts) ? ts - RELATIVE_TS_BASE : ts) * av_q2d(st->time_base);
2402 127027 int64_t duration = ts - last;
2403
2404
2/2
✓ Branch 0 taken 4182 times.
✓ Branch 1 taken 122845 times.
127027 if (!info->duration_error)
2405 4182 info->duration_error = av_mallocz(sizeof(info->duration_error[0])*2);
2406
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 127027 times.
127027 if (!info->duration_error)
2407 ✗ return AVERROR(ENOMEM);
2408
2409 // if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
2410 // av_log(NULL, AV_LOG_ERROR, "%f\n", dts);
2411
2/2
✓ Branch 0 taken 50683773 times.
✓ Branch 1 taken 127027 times.
50810800 for (int i = 0; i < MAX_STD_TIMEBASES; i++) {
2412
2/2
✓ Branch 0 taken 19618095 times.
✓ Branch 1 taken 31065678 times.
50683773 if (info->duration_error[0][1][i] < 1e10) {
2413 19618095 int framerate = get_std_framerate(i);
2414 19618095 double sdts = dts*framerate/(1001*12);
2415
2/2
✓ Branch 0 taken 39236190 times.
✓ Branch 1 taken 19618095 times.
58854285 for (int j = 0; j < 2; j++) {
2416 39236190 int64_t ticks = llrint(sdts+j*0.5);
2417 39236190 double error = sdts - ticks + j*0.5;
2418 39236190 info->duration_error[j][0][i] += error;
2419 39236190 info->duration_error[j][1][i] += error*error;
2420 }
2421 }
2422 }
2423
1/2
✓ Branch 0 taken 127027 times.
✗ Branch 1 not taken.
127027 if (info->rfps_duration_sum <= INT64_MAX - duration) {
2424 127027 info->duration_count++;
2425 127027 info->rfps_duration_sum += duration;
2426 }
2427
2428
2/2
✓ Branch 0 taken 11519 times.
✓ Branch 1 taken 115508 times.
127027 if (info->duration_count % 10 == 0) {
2429 11519 int n = info->duration_count;
2430
2/2
✓ Branch 0 taken 4596081 times.
✓ Branch 1 taken 11519 times.
4607600 for (int i = 0; i < MAX_STD_TIMEBASES; i++) {
2431
2/2
✓ Branch 0 taken 1869547 times.
✓ Branch 1 taken 2726534 times.
4596081 if (info->duration_error[0][1][i] < 1e10) {
2432 1869547 double a0 = info->duration_error[0][0][i] / n;
2433 1869547 double error0 = info->duration_error[0][1][i] / n - a0*a0;
2434 1869547 double a1 = info->duration_error[1][0][i] / n;
2435 1869547 double error1 = info->duration_error[1][1][i] / n - a1*a1;
2436
4/4
✓ Branch 0 taken 1531880 times.
✓ Branch 1 taken 337667 times.
✓ Branch 2 taken 1429942 times.
✓ Branch 3 taken 101938 times.
1869547 if (error0 > 0.04 && error1 > 0.04) {
2437 1429942 info->duration_error[0][1][i] = 2e10;
2438 1429942 info->duration_error[1][1][i] = 2e10;
2439 }
2440 }
2441 }
2442 }
2443
2444 // ignore the first 4 values, they might have some random jitter
2445
3/4
✓ Branch 0 taken 114587 times.
✓ Branch 1 taken 12440 times.
✓ Branch 4 taken 114587 times.
✗ Branch 5 not taken.
127027 if (info->duration_count > 3 && is_relative(ts) == is_relative(last))
2446 114587 info->duration_gcd = av_gcd(info->duration_gcd, duration);
2447 }
2448
2/2
✓ Branch 0 taken 133688 times.
✓ Branch 1 taken 17054 times.
150742 if (ts != AV_NOPTS_VALUE)
2449 133688 info->last_dts = ts;
2450
2451 150742 return 0;
2452 }
2453
2454 9117 void ff_rfps_calculate(AVFormatContext *ic)
2455 {
2456
2/2
✓ Branch 0 taken 10696 times.
✓ Branch 1 taken 9117 times.
19813 for (unsigned i = 0; i < ic->nb_streams; i++) {
2457 10696 AVStream *const st = ic->streams[i];
2458 10696 FFStream *const sti = ffstream(st);
2459
2460
2/2
✓ Branch 0 taken 3413 times.
✓ Branch 1 taken 7283 times.
10696 if (st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO)
2461 3413 continue;
2462 // the check for tb_unreliable() is not completely correct, since this is not about handling
2463 // an unreliable/inexact time base, but a time base that is finer than necessary, as e.g.
2464 // ipmovie.c produces.
2465
8/8
✓ Branch 1 taken 5823 times.
✓ Branch 2 taken 1460 times.
✓ Branch 3 taken 3586 times.
✓ Branch 4 taken 2237 times.
✓ Branch 5 taken 369 times.
✓ Branch 6 taken 3217 times.
✓ Branch 7 taken 182 times.
✓ Branch 8 taken 187 times.
7283 if (tb_unreliable(ic, st) && sti->info->duration_count > 15 && sti->info->duration_gcd > FFMAX(1, st->time_base.den/(500LL*st->time_base.num)) && !st->r_frame_rate.num &&
2466
1/2
✓ Branch 0 taken 182 times.
✗ Branch 1 not taken.
182 sti->info->duration_gcd < INT64_MAX / st->time_base.num)
2467 182 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);
2468
4/4
✓ Branch 0 taken 4138 times.
✓ Branch 1 taken 3145 times.
✓ Branch 2 taken 440 times.
✓ Branch 3 taken 3698 times.
7283 if (sti->info->duration_count > 1 && !st->r_frame_rate.num
2469
2/2
✓ Branch 1 taken 330 times.
✓ Branch 2 taken 110 times.
440 && tb_unreliable(ic, st)) {
2470 330 int num = 0;
2471 330 double best_error = 0.01;
2472
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 330 times.
330 AVRational ref_rate = st->r_frame_rate.num ? st->r_frame_rate : av_inv_q(st->time_base);
2473
2474
2/2
✓ Branch 0 taken 131670 times.
✓ Branch 1 taken 330 times.
132000 for (int j = 0; j < MAX_STD_TIMEBASES; j++) {
2475
2/2
✓ Branch 0 taken 96159 times.
✓ Branch 1 taken 35511 times.
131670 if (sti->info->codec_info_duration &&
2476
2/2
✓ Branch 2 taken 10222 times.
✓ Branch 3 taken 85937 times.
96159 sti->info->codec_info_duration*av_q2d(st->time_base) < (1001*11.5)/get_std_framerate(j))
2477 10222 continue;
2478
4/4
✓ Branch 0 taken 35511 times.
✓ Branch 1 taken 85937 times.
✓ Branch 3 taken 979 times.
✓ Branch 4 taken 34532 times.
121448 if (!sti->info->codec_info_duration && get_std_framerate(j) < 1001*12)
2479 979 continue;
2480
2481
2/2
✓ Branch 2 taken 56657 times.
✓ Branch 3 taken 63812 times.
120469 if (av_q2d(st->time_base) * sti->info->rfps_duration_sum / sti->info->duration_count < (1001*12.0 * 0.8)/get_std_framerate(j))
2482 56657 continue;
2483
2484
2/2
✓ Branch 0 taken 127624 times.
✓ Branch 1 taken 63812 times.
191436 for (int k = 0; k < 2; k++) {
2485 127624 int n = sti->info->duration_count;
2486 127624 double a = sti->info->duration_error[k][0][j] / n;
2487 127624 double error = sti->info->duration_error[k][1][j]/n - a*a;
2488
2489
4/4
✓ Branch 0 taken 4660 times.
✓ Branch 1 taken 122964 times.
✓ Branch 2 taken 4589 times.
✓ Branch 3 taken 71 times.
127624 if (error < best_error && best_error> 0.000000001) {
2490 4589 best_error= error;
2491 4589 num = get_std_framerate(j);
2492 }
2493
2/2
✓ Branch 0 taken 25685 times.
✓ Branch 1 taken 101939 times.
127624 if (error < 0.02)
2494 25685 av_log(ic, AV_LOG_DEBUG, "rfps: %f %f\n", get_std_framerate(j) / 12.0/1001, error);
2495 }
2496 }
2497 // do not increase frame rate by more than 1 % in order to match a standard rate.
2498
5/6
✓ Branch 0 taken 324 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 324 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 322 times.
✓ Branch 6 taken 2 times.
330 if (num && (!ref_rate.num || (double)num/(12*1001) < 1.01 * av_q2d(ref_rate)))
2499 322 av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den, num, 12*1001, INT_MAX);
2500 }
2501
2/2
✓ Branch 0 taken 1196 times.
✓ Branch 1 taken 6087 times.
7283 if ( !st->avg_frame_rate.num
2502
4/4
✓ Branch 0 taken 352 times.
✓ Branch 1 taken 844 times.
✓ Branch 2 taken 347 times.
✓ Branch 3 taken 5 times.
1196 && st->r_frame_rate.num && sti->info->rfps_duration_sum
2503
2/2
✓ Branch 0 taken 88 times.
✓ Branch 1 taken 259 times.
347 && sti->info->codec_info_duration <= 0
2504
2/2
✓ Branch 0 taken 85 times.
✓ Branch 1 taken 3 times.
88 && sti->info->duration_count > 2
2505
2/2
✓ Branch 2 taken 68 times.
✓ Branch 3 taken 17 times.
85 && 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
2506 ) {
2507 68 av_log(ic, AV_LOG_DEBUG, "Setting avg frame rate based on r frame rate\n");
2508 68 st->avg_frame_rate = st->r_frame_rate;
2509 }
2510
2511 7283 av_freep(&sti->info->duration_error);
2512 7283 sti->info->last_dts = AV_NOPTS_VALUE;
2513 7283 sti->info->duration_count = 0;
2514 7283 sti->info->rfps_duration_sum = 0;
2515 }
2516 9117 }
2517
2518 9925 static int extract_extradata_check(AVStream *st)
2519 {
2520 9925 const AVBitStreamFilter *const f = av_bsf_get_by_name("extract_extradata");
2521
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9925 times.
9925 if (!f)
2522 ✗ return 0;
2523
2524
1/2
✓ Branch 0 taken 9925 times.
✗ Branch 1 not taken.
9925 if (f->codec_ids) {
2525 const enum AVCodecID *ids;
2526
2/2
✓ Branch 0 taken 114561 times.
✓ Branch 1 taken 8938 times.
123499 for (ids = f->codec_ids; *ids != AV_CODEC_ID_NONE; ids++)
2527
2/2
✓ Branch 0 taken 987 times.
✓ Branch 1 taken 113574 times.
114561 if (*ids == st->codecpar->codec_id)
2528 987 return 1;
2529 }
2530
2531 8938 return 0;
2532 }
2533
2534 7748 static int extract_extradata_init(AVStream *st)
2535 {
2536 7748 FFStream *const sti = ffstream(st);
2537 const AVBitStreamFilter *f;
2538 int ret;
2539
2540 7748 f = av_bsf_get_by_name("extract_extradata");
2541
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7748 times.
7748 if (!f)
2542 ✗ goto finish;
2543
2544 /* check that the codec id is supported */
2545 7748 ret = extract_extradata_check(st);
2546
2/2
✓ Branch 0 taken 6871 times.
✓ Branch 1 taken 877 times.
7748 if (!ret)
2547 6871 goto finish;
2548
2549 877 av_bsf_free(&sti->extract_extradata.bsf);
2550 877 ret = av_bsf_alloc(f, &sti->extract_extradata.bsf);
2551
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 877 times.
877 if (ret < 0)
2552 ✗ return ret;
2553
2554 877 ret = avcodec_parameters_copy(sti->extract_extradata.bsf->par_in,
2555 877 st->codecpar);
2556
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 877 times.
877 if (ret < 0)
2557 ✗ goto fail;
2558
2559 877 sti->extract_extradata.bsf->time_base_in = st->time_base;
2560
2561 877 ret = av_bsf_init(sti->extract_extradata.bsf);
2562
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 877 times.
877 if (ret < 0)
2563 ✗ goto fail;
2564
2565 877 finish:
2566 7748 sti->extract_extradata.inited = 1;
2567
2568 7748 return 0;
2569 ✗ fail:
2570 ✗ av_bsf_free(&sti->extract_extradata.bsf);
2571 ✗ return ret;
2572 }
2573
2574 173513 static int extract_extradata(FFFormatContext *si, AVStream *st, const AVPacket *pkt)
2575 {
2576 173513 FFStream *const sti = ffstream(st);
2577 173513 AVPacket *const pkt_ref = si->parse_pkt;
2578 int ret;
2579
2580
2/2
✓ Branch 0 taken 7748 times.
✓ Branch 1 taken 165765 times.
173513 if (!sti->extract_extradata.inited) {
2581 7748 ret = extract_extradata_init(st);
2582
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7748 times.
7748 if (ret < 0)
2583 ✗ return ret;
2584 }
2585
2586
3/4
✓ Branch 0 taken 173513 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 172206 times.
✓ Branch 3 taken 1307 times.
173513 if (sti->extract_extradata.inited && !sti->extract_extradata.bsf)
2587 172206 return 0;
2588
2589 1307 ret = av_packet_ref(pkt_ref, pkt);
2590
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1307 times.
1307 if (ret < 0)
2591 ✗ return ret;
2592
2593 1307 ret = av_bsf_send_packet(sti->extract_extradata.bsf, pkt_ref);
2594
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1307 times.
1307 if (ret < 0) {
2595 ✗ av_packet_unref(pkt_ref);
2596 ✗ return ret;
2597 }
2598
2599
4/4
✓ Branch 0 taken 2614 times.
✓ Branch 1 taken 427 times.
✓ Branch 2 taken 1734 times.
✓ Branch 3 taken 880 times.
3041 while (ret >= 0 && !sti->avctx->extradata) {
2600 1734 ret = av_bsf_receive_packet(sti->extract_extradata.bsf, pkt_ref);
2601
2/2
✓ Branch 0 taken 427 times.
✓ Branch 1 taken 1307 times.
1734 if (ret < 0) {
2602
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 427 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
427 if (ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
2603 ✗ return ret;
2604 427 continue;
2605 }
2606
2607
2/2
✓ Branch 0 taken 1208 times.
✓ Branch 1 taken 427 times.
1635 for (int i = 0; i < pkt_ref->side_data_elems; i++) {
2608 1208 AVPacketSideData *const side_data = &pkt_ref->side_data[i];
2609
2/2
✓ Branch 0 taken 880 times.
✓ Branch 1 taken 328 times.
1208 if (side_data->type == AV_PKT_DATA_NEW_EXTRADATA) {
2610 880 sti->avctx->extradata = side_data->data;
2611 880 sti->avctx->extradata_size = side_data->size;
2612 880 side_data->data = NULL;
2613 880 side_data->size = 0;
2614 880 break;
2615 }
2616 }
2617 1307 av_packet_unref(pkt_ref);
2618 }
2619
2620 1307 return 0;
2621 }
2622
2623 9625 static int parameters_from_context(AVFormatContext *ic, AVCodecParameters *par,
2624 const AVCodecContext *avctx)
2625 {
2626 AVCodecParameters *par_tmp;
2627 int ret;
2628
2629 9625 par_tmp = avcodec_parameters_alloc();
2630
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9625 times.
9625 if (!par_tmp)
2631 ✗ return AVERROR(ENOMEM);
2632
2633 9625 ret = avcodec_parameters_copy(par_tmp, par);
2634
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9625 times.
9625 if (ret < 0)
2635 ✗ goto fail;
2636
2637 9625 ret = avcodec_parameters_from_context(par, avctx);
2638
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9625 times.
9625 if (ret < 0)
2639 ✗ goto fail;
2640
2641 /* Restore some values if they are signaled at the container level
2642 * given they may have been replaced by codec level values as read
2643 * internally by avformat_find_stream_info().
2644 */
2645
2/2
✓ Branch 0 taken 265 times.
✓ Branch 1 taken 9360 times.
9625 if (par_tmp->color_range != AVCOL_RANGE_UNSPECIFIED)
2646 265 par->color_range = par_tmp->color_range;
2647
2/2
✓ Branch 0 taken 112 times.
✓ Branch 1 taken 9513 times.
9625 if (par_tmp->color_primaries != AVCOL_PRI_UNSPECIFIED)
2648 112 par->color_primaries = par_tmp->color_primaries;
2649
2/2
✓ Branch 0 taken 152 times.
✓ Branch 1 taken 9473 times.
9625 if (par_tmp->color_trc != AVCOL_TRC_UNSPECIFIED)
2650 152 par->color_trc = par_tmp->color_trc;
2651
2/2
✓ Branch 0 taken 128 times.
✓ Branch 1 taken 9497 times.
9625 if (par_tmp->color_space != AVCOL_SPC_UNSPECIFIED)
2652 128 par->color_space = par_tmp->color_space;
2653
2/2
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 9557 times.
9625 if (par_tmp->chroma_location != AVCHROMA_LOC_UNSPECIFIED)
2654 68 par->chroma_location = par_tmp->chroma_location;
2655
2656 /* A failed avcodec_open2() zeroes ch_layout through
2657 * ff_codec_close() -> av_opt_free(); do not copy that empty layout
2658 * over a container-signaled one. Other AVOption types used here are
2659 * integers and are not cleared. */
2660
4/4
✓ Branch 0 taken 2585 times.
✓ Branch 1 taken 7040 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 2584 times.
9625 if (par_tmp->ch_layout.nb_channels > 0 && !par->ch_layout.nb_channels) {
2661 1 ret = av_channel_layout_copy(&par->ch_layout, &par_tmp->ch_layout);
2662
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
2663 ✗ goto fail;
2664 }
2665
2666 9625 ret = 0;
2667 9625 fail:
2668 9625 avcodec_parameters_free(&par_tmp);
2669
2670 9625 return ret;
2671 }
2672
2673 8452 int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
2674 {
2675 8452 FFFormatContext *const si = ffformatcontext(ic);
2676 8452 int count = 0, ret = 0, err;
2677 int64_t read_size;
2678 8452 AVPacket *pkt1 = si->pkt;
2679 8452 int64_t old_offset = avio_tell(ic->pb);
2680 // new streams might appear, no options for those
2681 8452 int orig_nb_streams = ic->nb_streams;
2682 int flush_codecs;
2683 8452 int64_t max_analyze_duration = ic->max_analyze_duration;
2684 int64_t max_stream_analyze_duration;
2685 int64_t max_subtitle_analyze_duration;
2686 8452 int64_t probesize = ic->probesize;
2687 8452 int eof_reached = 0;
2688
2689 8452 flush_codecs = probesize > 0;
2690
2691 8452 av_opt_set_int(ic, "skip_clear", 1, AV_OPT_SEARCH_CHILDREN);
2692
2693 8452 max_stream_analyze_duration = max_analyze_duration;
2694 8452 max_subtitle_analyze_duration = max_analyze_duration;
2695
2/2
✓ Branch 0 taken 8449 times.
✓ Branch 1 taken 3 times.
8452 if (!max_analyze_duration) {
2696 8449 max_stream_analyze_duration =
2697 8449 max_analyze_duration = 5*AV_TIME_BASE;
2698 8449 max_subtitle_analyze_duration = 30*AV_TIME_BASE;
2699
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 8405 times.
8449 if (!strcmp(ic->iformat->name, "flv"))
2700 44 max_stream_analyze_duration = 90*AV_TIME_BASE;
2701
4/4
✓ Branch 0 taken 8421 times.
✓ Branch 1 taken 28 times.
✓ Branch 2 taken 76 times.
✓ Branch 3 taken 8345 times.
8449 if (!strcmp(ic->iformat->name, "mpeg") || !strcmp(ic->iformat->name, "mpegts"))
2702 104 max_stream_analyze_duration = 7*AV_TIME_BASE;
2703 }
2704
2705
2/2
✓ Branch 0 taken 5243 times.
✓ Branch 1 taken 3209 times.
8452 if (ic->pb) {
2706 5243 FFIOContext *const ctx = ffiocontext(ic->pb);
2707 5243 av_log(ic, AV_LOG_DEBUG, "Before avformat_find_stream_info() pos: %"PRId64" bytes read:%"PRId64" seeks:%d nb_streams:%d\n",
2708 avio_tell(ic->pb), ctx->bytes_read, ctx->seek_count, ic->nb_streams);
2709 }
2710
2711
2/2
✓ Branch 0 taken 9440 times.
✓ Branch 1 taken 8452 times.
17892 for (unsigned i = 0; i < ic->nb_streams; i++) {
2712 const AVCodec *codec;
2713 9440 AVDictionary *thread_opt = NULL;
2714 9440 AVStream *const st = ic->streams[i];
2715 9440 FFStream *const sti = ffstream(st);
2716 9440 AVCodecContext *const avctx = sti->avctx;
2717
2718 /* check if the caller has overridden the codec id */
2719 // only for the split stuff
2720
5/6
✓ Branch 0 taken 9440 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9438 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 8862 times.
✓ Branch 5 taken 576 times.
9440 if (!sti->parser && !(ic->flags & AVFMT_FLAG_NOPARSE) && sti->request_probe <= 0) {
2721 8862 sti->parser = av_parser_init(st->codecpar->codec_id);
2722
2/2
✓ Branch 0 taken 6267 times.
✓ Branch 1 taken 2595 times.
8862 if (sti->parser) {
2723
2/2
✓ Branch 0 taken 880 times.
✓ Branch 1 taken 5387 times.
6267 if (sti->need_parsing == AVSTREAM_PARSE_HEADERS) {
2724 880 sti->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES;
2725
2/2
✓ Branch 0 taken 996 times.
✓ Branch 1 taken 4391 times.
5387 } else if (sti->need_parsing == AVSTREAM_PARSE_FULL_RAW) {
2726 996 sti->parser->flags |= PARSER_FLAG_USE_CODEC_TS;
2727 }
2728
2/2
✓ Branch 0 taken 688 times.
✓ Branch 1 taken 1907 times.
2595 } else if (sti->need_parsing) {
2729 688 av_log(ic, AV_LOG_VERBOSE, "parser not found for codec "
2730 "%s, packets or times may be invalid.\n",
2731 688 avcodec_get_name(st->codecpar->codec_id));
2732 }
2733 }
2734
2735 9440 ret = avcodec_parameters_to_context(avctx, st->codecpar);
2736
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9440 times.
9440 if (ret < 0)
2737 ✗ goto find_stream_info_err;
2738
2/2
✓ Branch 0 taken 8864 times.
✓ Branch 1 taken 576 times.
9440 if (sti->request_probe <= 0)
2739 8864 sti->avctx_inited = 1;
2740
2741 9440 codec = find_probe_decoder(ic, st, st->codecpar->codec_id);
2742
2743 /* Force thread count to 1 since the H.264 decoder will not extract
2744 * SPS and PPS to extradata during multi-threaded decoding. */
2745
2/2
✓ Branch 0 taken 9127 times.
✓ Branch 1 taken 313 times.
9440 av_dict_set(options ? &options[i] : &thread_opt, "threads", "1", 0);
2746 /* Force lowres to 0. The decoder might reduce the video size by the
2747 * lowres factor, and we don't want that propagated to the stream's
2748 * codecpar */
2749
2/2
✓ Branch 0 taken 9127 times.
✓ Branch 1 taken 313 times.
9440 av_dict_set(options ? &options[i] : &thread_opt, "lowres", "0", 0);
2750
2751
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9440 times.
9440 if (ic->codec_whitelist)
2752 ✗ av_dict_set(options ? &options[i] : &thread_opt, "codec_whitelist", ic->codec_whitelist, 0);
2753
2754 // Try to just open decoders, in case this is enough to get parameters.
2755 // Also ensure that subtitle_header is properly set.
2756
4/4
✓ Branch 1 taken 8517 times.
✓ Branch 2 taken 923 times.
✓ Branch 3 taken 575 times.
✓ Branch 4 taken 7942 times.
9440 if (!has_codec_parameters(st, NULL) && sti->request_probe <= 0 ||
2757
2/2
✓ Branch 0 taken 94 times.
✓ Branch 1 taken 1404 times.
1498 st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) {
2758
3/4
✓ Branch 0 taken 7984 times.
✓ Branch 1 taken 52 times.
✓ Branch 2 taken 7984 times.
✗ Branch 3 not taken.
8036 if (codec && !avctx->codec)
2759
4/4
✓ Branch 0 taken 7679 times.
✓ Branch 1 taken 305 times.
✓ Branch 3 taken 7 times.
✓ Branch 4 taken 7977 times.
7984 if (avcodec_open2(avctx, codec, options ? &options[i] : &thread_opt) < 0)
2760 7 av_log(ic, AV_LOG_WARNING,
2761 "Failed to open codec in %s\n", __func__);
2762 }
2763
2/2
✓ Branch 0 taken 313 times.
✓ Branch 1 taken 9127 times.
9440 if (!options)
2764 313 av_dict_free(&thread_opt);
2765 }
2766
2767 8452 read_size = 0;
2768 208842 for (;;) {
2769 const AVPacket *pkt;
2770 AVStream *st;
2771 FFStream *sti;
2772 AVCodecContext *avctx;
2773 int analyzed_all_streams;
2774 unsigned i;
2775
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 217294 times.
217294 if (ff_check_interrupt(&ic->interrupt_callback)) {
2776 ✗ ret = AVERROR_EXIT;
2777 ✗ av_log(ic, AV_LOG_DEBUG, "interrupted\n");
2778 ✗ break;
2779 }
2780
2781 /* read_frame_internal() in a previous iteration of this loop may
2782 * have made changes to streams without returning a packet for them.
2783 * Handle that here. */
2784 217294 ret = update_stream_avctx(ic);
2785
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 217294 times.
217294 if (ret < 0)
2786 ✗ goto unref_then_goto_end;
2787
2788 /* check if one codec still needs to be handled */
2789
2/2
✓ Branch 0 taken 297488 times.
✓ Branch 1 taken 116769 times.
414257 for (i = 0; i < ic->nb_streams; i++) {
2790 297488 AVStream *const st = ic->streams[i];
2791 297488 FFStream *const sti = ffstream(st);
2792 297488 int fps_analyze_framecount = 20;
2793 int count;
2794
2795
2/2
✓ Branch 1 taken 9700 times.
✓ Branch 2 taken 287788 times.
297488 if (!has_codec_parameters(st, NULL))
2796 9700 break;
2797 /* If the timebase is coarse (like the usual millisecond precision
2798 * of mkv), we need to analyze more frames to reliably arrive at
2799 * the correct fps. */
2800
2/2
✓ Branch 1 taken 128687 times.
✓ Branch 2 taken 159101 times.
287788 if (av_q2d(st->time_base) > 0.0005)
2801 128687 fps_analyze_framecount *= 2;
2802
2/2
✓ Branch 1 taken 8751 times.
✓ Branch 2 taken 279037 times.
287788 if (!tb_unreliable(ic, st))
2803 8751 fps_analyze_framecount = 0;
2804
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 287788 times.
287788 if (ic->fps_probe_size >= 0)
2805 ✗ fps_analyze_framecount = ic->fps_probe_size;
2806
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 287738 times.
287788 if (st->disposition & AV_DISPOSITION_ATTACHED_PIC)
2807 50 fps_analyze_framecount = 0;
2808 /* variable fps and no guess at the real fps */
2809
2/2
✓ Branch 0 taken 20322 times.
✓ Branch 1 taken 267466 times.
287788 count = (ic->iformat->flags & AVFMT_NOTIMESTAMPS) ?
2810 20322 sti->info->codec_info_duration_fields/2 :
2811 267466 sti->info->duration_count;
2812
4/4
✓ Branch 0 taken 106119 times.
✓ Branch 1 taken 181669 times.
✓ Branch 2 taken 423 times.
✓ Branch 3 taken 105696 times.
287788 if (!(st->r_frame_rate.num && st->avg_frame_rate.num) &&
2813
2/2
✓ Branch 0 taken 47417 times.
✓ Branch 1 taken 134675 times.
182092 st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
2814
2/2
✓ Branch 0 taken 28398 times.
✓ Branch 1 taken 19019 times.
47417 if (count < fps_analyze_framecount)
2815 28398 break;
2816 }
2817 // Look at the first 3 frames if there is evidence of frame delay
2818 // but the decoder delay is not set.
2819
6/6
✓ Branch 0 taken 3573 times.
✓ Branch 1 taken 255817 times.
✓ Branch 2 taken 189 times.
✓ Branch 3 taken 3384 times.
✓ Branch 4 taken 12 times.
✓ Branch 5 taken 177 times.
259390 if (sti->info->frame_delay_evidence && count < 2 && sti->avctx->has_b_frames == 0)
2820 12 break;
2821
2/2
✓ Branch 0 taken 235590 times.
✓ Branch 1 taken 23788 times.
259378 if (!sti->avctx->extradata &&
2822
6/6
✓ Branch 0 taken 233523 times.
✓ Branch 1 taken 2067 times.
✓ Branch 2 taken 110 times.
✓ Branch 3 taken 233413 times.
✓ Branch 4 taken 110 times.
✓ Branch 5 taken 2067 times.
237767 (!sti->extract_extradata.inited || sti->extract_extradata.bsf) &&
2823 2177 extract_extradata_check(st))
2824 110 break;
2825
2/2
✓ Branch 0 taken 65805 times.
✓ Branch 1 taken 193463 times.
259268 if (sti->first_dts == AV_NOPTS_VALUE &&
2826
6/6
✓ Branch 0 taken 9936 times.
✓ Branch 1 taken 55869 times.
✓ Branch 2 taken 9670 times.
✓ Branch 3 taken 266 times.
✓ Branch 4 taken 63178 times.
✓ Branch 5 taken 2361 times.
131344 (!(ic->iformat->flags & AVFMT_NOTIMESTAMPS) || sti->need_parsing == AVSTREAM_PARSE_FULL_RAW) &&
2827
2/2
✓ Branch 0 taken 65489 times.
✓ Branch 1 taken 50 times.
65539 sti->codec_info_nb_frames < ((st->disposition & AV_DISPOSITION_ATTACHED_PIC) ? 1 : ic->max_ts_probe) &&
2828
2/2
✓ Branch 0 taken 46731 times.
✓ Branch 1 taken 16447 times.
63178 (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ||
2829
2/2
✓ Branch 0 taken 873 times.
✓ Branch 1 taken 45858 times.
46731 st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO))
2830 break;
2831 }
2832 217294 analyzed_all_streams = 0;
2833
4/4
✓ Branch 0 taken 116769 times.
✓ Branch 1 taken 100525 times.
✓ Branch 2 taken 116724 times.
✓ Branch 3 taken 45 times.
217294 if (i == ic->nb_streams && !si->missing_streams) {
2834 116724 analyzed_all_streams = 1;
2835 /* NOTE: If the format has no header, then we need to read some
2836 * packets to get most of the streams, so we cannot stop here. */
2837
2/2
✓ Branch 0 taken 3800 times.
✓ Branch 1 taken 112924 times.
116724 if (!(ic->ctx_flags & AVFMTCTX_NOHEADER)) {
2838 /* If we found the info for all the codecs, we can stop. */
2839 3800 ret = count;
2840 3800 av_log(ic, AV_LOG_DEBUG, "All info found\n");
2841 3800 flush_codecs = 0;
2842 3800 break;
2843 }
2844 }
2845 /* We did not get all the codec info, but we read too much data. */
2846
2/2
✓ Branch 0 taken 3094 times.
✓ Branch 1 taken 210400 times.
213494 if (read_size >= probesize) {
2847 3094 ret = count;
2848 3094 av_log(ic, AV_LOG_DEBUG,
2849 "Probe buffer size limit of %"PRId64" bytes reached\n", probesize);
2850
2/2
✓ Branch 0 taken 3108 times.
✓ Branch 1 taken 3094 times.
6202 for (unsigned i = 0; i < ic->nb_streams; i++) {
2851 3108 AVStream *const st = ic->streams[i];
2852 3108 FFStream *const sti = ffstream(st);
2853
2/2
✓ Branch 0 taken 58 times.
✓ Branch 1 taken 3050 times.
3108 if (!st->r_frame_rate.num &&
2854
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 40 times.
58 sti->info->duration_count <= 1 &&
2855
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 17 times.
18 st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
2856
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 strcmp(ic->iformat->name, "image2"))
2857 1 av_log(ic, AV_LOG_WARNING,
2858 "Stream #%d: not enough frames to estimate rate; "
2859 "consider increasing probesize\n", i);
2860 }
2861 3094 break;
2862 }
2863
2864 /* NOTE: A new stream can be added there if no header in file
2865 * (AVFMTCTX_NOHEADER). */
2866 210400 ret = read_frame_internal(ic, pkt1);
2867
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 210400 times.
210400 if (ret == AVERROR(EAGAIN))
2868 ✗ continue;
2869
2870
2/2
✓ Branch 0 taken 1456 times.
✓ Branch 1 taken 208944 times.
210400 if (ret < 0) {
2871 /* EOF or error*/
2872 1456 eof_reached = 1;
2873 1456 break;
2874 }
2875
2876
1/2
✓ Branch 0 taken 208944 times.
✗ Branch 1 not taken.
208944 if (!(ic->flags & AVFMT_FLAG_NOBUFFER)) {
2877 208944 ret = ff_packet_list_put(&si->packet_buffer,
2878 pkt1, NULL, 0);
2879
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 208944 times.
208944 if (ret < 0)
2880 ✗ goto unref_then_goto_end;
2881
2882 208944 pkt = &si->packet_buffer.tail->pkt;
2883 } else {
2884 ✗ pkt = pkt1;
2885 }
2886
2887 208944 st = ic->streams[pkt->stream_index];
2888 208944 sti = ffstream(st);
2889
2/2
✓ Branch 0 taken 208879 times.
✓ Branch 1 taken 65 times.
208944 if (!(st->disposition & AV_DISPOSITION_ATTACHED_PIC))
2890 208879 read_size += pkt->size;
2891
2892 208944 avctx = sti->avctx;
2893
2/2
✓ Branch 0 taken 761 times.
✓ Branch 1 taken 208183 times.
208944 if (!sti->avctx_inited) {
2894 761 ret = avcodec_parameters_to_context(avctx, st->codecpar);
2895
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 761 times.
761 if (ret < 0)
2896 ✗ goto unref_then_goto_end;
2897 761 sti->avctx_inited = 1;
2898 }
2899
2900
4/4
✓ Branch 0 taken 191480 times.
✓ Branch 1 taken 17464 times.
✓ Branch 2 taken 177688 times.
✓ Branch 3 taken 13792 times.
208944 if (pkt->dts != AV_NOPTS_VALUE && sti->codec_info_nb_frames > 1) {
2901 /* check for non-increasing dts */
2902
2/2
✓ Branch 0 taken 172296 times.
✓ Branch 1 taken 5392 times.
177688 if (sti->info->fps_last_dts != AV_NOPTS_VALUE &&
2903
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 172271 times.
172296 sti->info->fps_last_dts >= pkt->dts) {
2904 25 av_log(ic, AV_LOG_DEBUG,
2905 "Non-increasing DTS in stream %d: packet %d with DTS "
2906 "%"PRId64", packet %d with DTS %"PRId64"\n",
2907 25 st->index, sti->info->fps_last_dts_idx,
2908 25 sti->info->fps_last_dts, sti->codec_info_nb_frames,
2909 25 pkt->dts);
2910 25 sti->info->fps_first_dts =
2911 25 sti->info->fps_last_dts = AV_NOPTS_VALUE;
2912 }
2913 /* Check for a discontinuity in dts. If the difference in dts
2914 * is more than 1000 times the average packet duration in the
2915 * sequence, we treat it as a discontinuity. */
2916
2/2
✓ Branch 0 taken 172271 times.
✓ Branch 1 taken 5417 times.
177688 if (sti->info->fps_last_dts != AV_NOPTS_VALUE &&
2917
2/2
✓ Branch 0 taken 166920 times.
✓ Branch 1 taken 5351 times.
172271 sti->info->fps_last_dts_idx > sti->info->fps_first_dts_idx &&
2918 166920 (pkt->dts - (uint64_t)sti->info->fps_last_dts) / 1000 >
2919 166920 (sti->info->fps_last_dts - (uint64_t)sti->info->fps_first_dts) /
2920
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 166920 times.
166920 (sti->info->fps_last_dts_idx - sti->info->fps_first_dts_idx)) {
2921 ✗ av_log(ic, AV_LOG_WARNING,
2922 "DTS discontinuity in stream %d: packet %d with DTS "
2923 "%"PRId64", packet %d with DTS %"PRId64"\n",
2924 ✗ st->index, sti->info->fps_last_dts_idx,
2925 ✗ sti->info->fps_last_dts, sti->codec_info_nb_frames,
2926 ✗ pkt->dts);
2927 ✗ sti->info->fps_first_dts =
2928 ✗ sti->info->fps_last_dts = AV_NOPTS_VALUE;
2929 }
2930
2931 /* update stored dts values */
2932
2/2
✓ Branch 0 taken 5417 times.
✓ Branch 1 taken 172271 times.
177688 if (sti->info->fps_first_dts == AV_NOPTS_VALUE) {
2933 5417 sti->info->fps_first_dts = pkt->dts;
2934 5417 sti->info->fps_first_dts_idx = sti->codec_info_nb_frames;
2935 }
2936 177688 sti->info->fps_last_dts = pkt->dts;
2937 177688 sti->info->fps_last_dts_idx = sti->codec_info_nb_frames;
2938 }
2939
2/2
✓ Branch 0 taken 193805 times.
✓ Branch 1 taken 15139 times.
208944 if (sti->codec_info_nb_frames > 1) {
2940 193805 int64_t t = 0;
2941 int64_t limit;
2942
2943
1/2
✓ Branch 0 taken 193805 times.
✗ Branch 1 not taken.
193805 if (st->time_base.den > 0)
2944 193805 t = av_rescale_q(sti->info->codec_info_duration, st->time_base, AV_TIME_BASE_Q);
2945
2/2
✓ Branch 0 taken 117704 times.
✓ Branch 1 taken 76101 times.
193805 if (st->avg_frame_rate.num > 0)
2946
2/2
✓ Branch 1 taken 117177 times.
✓ Branch 2 taken 527 times.
117704 t = FFMAX(t, av_rescale_q(sti->codec_info_nb_frames, av_inv_q(st->avg_frame_rate), AV_TIME_BASE_Q));
2947
2948
2/2
✓ Branch 0 taken 5164 times.
✓ Branch 1 taken 188641 times.
193805 if ( t == 0
2949
2/2
✓ Branch 0 taken 614 times.
✓ Branch 1 taken 4550 times.
5164 && sti->codec_info_nb_frames > 30
2950
2/2
✓ Branch 0 taken 547 times.
✓ Branch 1 taken 67 times.
614 && sti->info->fps_first_dts != AV_NOPTS_VALUE
2951
1/2
✓ Branch 0 taken 547 times.
✗ Branch 1 not taken.
547 && sti->info->fps_last_dts != AV_NOPTS_VALUE) {
2952 547 int64_t dur = av_sat_sub64(sti->info->fps_last_dts, sti->info->fps_first_dts);
2953
1/2
✓ Branch 0 taken 547 times.
✗ Branch 1 not taken.
547 t = FFMAX(t, av_rescale_q(dur, st->time_base, AV_TIME_BASE_Q));
2954 }
2955
2956
2/2
✓ Branch 0 taken 106432 times.
✓ Branch 1 taken 87373 times.
193805 if (analyzed_all_streams) limit = max_analyze_duration;
2957
2/2
✓ Branch 0 taken 91 times.
✓ Branch 1 taken 87282 times.
87373 else if (avctx->codec_type == AVMEDIA_TYPE_SUBTITLE) limit = max_subtitle_analyze_duration;
2958 87282 else limit = max_stream_analyze_duration;
2959
2960
2/2
✓ Branch 0 taken 102 times.
✓ Branch 1 taken 193703 times.
193805 if (t >= limit) {
2961 102 av_log(ic, AV_LOG_VERBOSE, "max_analyze_duration %"PRId64" reached at %"PRId64" microseconds st:%d\n",
2962 limit,
2963 102 t, pkt->stream_index);
2964
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 102 times.
102 if (ic->flags & AVFMT_FLAG_NOBUFFER)
2965 ✗ av_packet_unref(pkt1);
2966 102 break;
2967 }
2968
3/4
✓ Branch 0 taken 189347 times.
✓ Branch 1 taken 4356 times.
✓ Branch 2 taken 189347 times.
✗ Branch 3 not taken.
193703 if (pkt->duration > 0 && pkt->duration < INT64_MAX - sti->info->codec_info_duration) {
2969
4/4
✓ Branch 0 taken 189307 times.
✓ Branch 1 taken 40 times.
✓ Branch 2 taken 15374 times.
✓ Branch 3 taken 173933 times.
189347 const int fields = sti->codec_desc && (sti->codec_desc->props & AV_CODEC_PROP_FIELDS);
2970
1/8
✗ Branch 0 not taken.
✓ Branch 1 taken 189347 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.
189347 if (avctx->codec_type == AVMEDIA_TYPE_SUBTITLE && pkt->pts != AV_NOPTS_VALUE && st->start_time != AV_NOPTS_VALUE && pkt->pts >= st->start_time
2971 ✗ && (uint64_t)pkt->pts - st->start_time < INT64_MAX
2972 ) {
2973 ✗ sti->info->codec_info_duration = FFMIN(pkt->pts - st->start_time, sti->info->codec_info_duration + pkt->duration);
2974 } else
2975 189347 sti->info->codec_info_duration += pkt->duration;
2976
4/4
✓ Branch 0 taken 42585 times.
✓ Branch 1 taken 97531 times.
✓ Branch 2 taken 14695 times.
✓ Branch 3 taken 27890 times.
329463 sti->info->codec_info_duration_fields += sti->parser && sti->need_parsing && fields
2977
2/2
✓ Branch 0 taken 140116 times.
✓ Branch 1 taken 49231 times.
329463 ? sti->parser->repeat_pict + 1 : 2;
2978 }
2979 }
2980
2/2
✓ Branch 0 taken 138563 times.
✓ Branch 1 taken 70279 times.
208842 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
2981 #if FF_API_R_FRAME_RATE
2982 138563 ff_rfps_add_frame(ic, st, pkt->dts);
2983 #endif
2984
6/6
✓ Branch 0 taken 5508 times.
✓ Branch 1 taken 133055 times.
✓ Branch 2 taken 5174 times.
✓ Branch 3 taken 334 times.
✓ Branch 4 taken 3144 times.
✓ Branch 5 taken 2030 times.
138563 if (pkt->dts != pkt->pts && pkt->dts != AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE)
2985 3144 sti->info->frame_delay_evidence = 1;
2986 }
2987
2/2
✓ Branch 0 taken 173360 times.
✓ Branch 1 taken 35482 times.
208842 if (!sti->avctx->extradata) {
2988 173360 ret = extract_extradata(si, st, pkt);
2989
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 173360 times.
173360 if (ret < 0)
2990 ✗ goto unref_then_goto_end;
2991 }
2992
2993 /* If still no information, we try to open the codec and to
2994 * decompress the frame. We try to avoid that in most cases as
2995 * it takes longer and uses more memory. For MPEG-4, we need to
2996 * decompress for QuickTime.
2997 *
2998 * If AV_CODEC_CAP_CHANNEL_CONF is set this will force decoding of at
2999 * least one frame of codec data, this makes sure the codec initializes
3000 * the channel configuration and does not only trust the values from
3001 * the container. */
3002
2/2
✓ Branch 0 taken 190192 times.
✓ Branch 1 taken 18650 times.
399034 try_decode_frame(ic, st, pkt,
3003
2/2
✓ Branch 0 taken 88229 times.
✓ Branch 1 taken 101963 times.
190192 (options && i < orig_nb_streams) ? &options[i] : NULL);
3004
3005
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 208842 times.
208842 if (ic->flags & AVFMT_FLAG_NOBUFFER)
3006 ✗ av_packet_unref(pkt1);
3007
3008 208842 sti->codec_info_nb_frames++;
3009 208842 count++;
3010 }
3011
3012
2/2
✓ Branch 0 taken 1456 times.
✓ Branch 1 taken 6996 times.
8452 if (eof_reached) {
3013
2/2
✓ Branch 0 taken 1867 times.
✓ Branch 1 taken 1456 times.
3323 for (unsigned stream_index = 0; stream_index < ic->nb_streams; stream_index++) {
3014 1867 AVStream *const st = ic->streams[stream_index];
3015 1867 AVCodecContext *const avctx = ffstream(st)->avctx;
3016
2/2
✓ Branch 1 taken 41 times.
✓ Branch 2 taken 1826 times.
1867 if (!has_codec_parameters(st, NULL)) {
3017 41 const AVCodec *codec = find_probe_decoder(ic, st, st->codecpar->codec_id);
3018
4/4
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 15 times.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 14 times.
41 if (codec && !avctx->codec) {
3019 12 AVDictionary *opts = NULL;
3020
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (ic->codec_whitelist)
3021 ✗ av_dict_set(&opts, "codec_whitelist", ic->codec_whitelist, 0);
3022
5/6
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 6 times.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 11 times.
12 if (avcodec_open2(avctx, codec, (options && stream_index < orig_nb_streams) ? &options[stream_index] : &opts) < 0)
3023 1 av_log(ic, AV_LOG_WARNING,
3024 "Failed to open codec in %s\n", __func__);
3025 12 av_dict_free(&opts);
3026 }
3027 }
3028
3029 // EOF already reached while reading the stream above.
3030 // So continue with reoordering DTS with whatever delay we have.
3031
4/4
✓ Branch 0 taken 1837 times.
✓ Branch 1 taken 30 times.
✓ Branch 3 taken 21 times.
✓ Branch 4 taken 1816 times.
1867 if (si->packet_buffer.head && !has_decode_delay_been_guessed(st)) {
3032 21 update_dts_from_pts(ic, stream_index, si->packet_buffer.head);
3033 }
3034 }
3035 }
3036
3037
2/2
✓ Branch 0 taken 4652 times.
✓ Branch 1 taken 3800 times.
8452 if (flush_codecs) {
3038 4652 AVPacket *empty_pkt = si->pkt;
3039 4652 int err = 0;
3040 4652 av_packet_unref(empty_pkt);
3041
3042
2/2
✓ Branch 0 taken 5116 times.
✓ Branch 1 taken 4652 times.
9768 for (unsigned i = 0; i < ic->nb_streams; i++) {
3043 5116 AVStream *const st = ic->streams[i];
3044 5116 FFStream *const sti = ffstream(st);
3045
3046 /* flush the decoders */
3047
2/2
✓ Branch 0 taken 5006 times.
✓ Branch 1 taken 110 times.
5116 if (sti->info->found_decoder == 1) {
3048
2/2
✓ Branch 0 taken 4677 times.
✓ Branch 1 taken 329 times.
9683 err = try_decode_frame(ic, st, empty_pkt,
3049
2/2
✓ Branch 0 taken 4661 times.
✓ Branch 1 taken 16 times.
4677 (options && i < orig_nb_streams)
3050 4661 ? &options[i] : NULL);
3051
3052
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5006 times.
5006 if (err < 0) {
3053 ✗ av_log(ic, AV_LOG_INFO,
3054 "decoding for stream %d failed\n", st->index);
3055 }
3056 }
3057 }
3058 }
3059
3060 8452 ff_rfps_calculate(ic);
3061
3062
2/2
✓ Branch 0 taken 9641 times.
✓ Branch 1 taken 8452 times.
18093 for (unsigned i = 0; i < ic->nb_streams; i++) {
3063 9641 AVStream *const st = ic->streams[i];
3064 9641 FFStream *const sti = ffstream(st);
3065 9641 AVCodecContext *const avctx = sti->avctx;
3066
3067
2/2
✓ Branch 0 taken 6777 times.
✓ Branch 1 taken 2864 times.
9641 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
3068
6/6
✓ Branch 0 taken 654 times.
✓ Branch 1 taken 6123 times.
✓ Branch 2 taken 605 times.
✓ Branch 3 taken 49 times.
✓ Branch 4 taken 589 times.
✓ Branch 5 taken 16 times.
6777 if (avctx->codec_id == AV_CODEC_ID_RAWVIDEO && !avctx->codec_tag && !avctx->bits_per_coded_sample) {
3069 589 uint32_t tag= avcodec_pix_fmt_to_codec_tag(avctx->pix_fmt);
3070
2/2
✓ Branch 1 taken 582 times.
✓ Branch 2 taken 7 times.
589 if (avpriv_pix_fmt_find(PIX_FMT_LIST_RAW, tag) == avctx->pix_fmt)
3071 582 avctx->codec_tag= tag;
3072 }
3073
3074 /* estimate average framerate if not set by demuxer */
3075
2/2
✓ Branch 0 taken 4234 times.
✓ Branch 1 taken 2543 times.
6777 if (sti->info->codec_info_duration_fields &&
3076
2/2
✓ Branch 0 taken 327 times.
✓ Branch 1 taken 3907 times.
4234 !st->avg_frame_rate.num &&
3077
1/2
✓ Branch 0 taken 327 times.
✗ Branch 1 not taken.
327 sti->info->codec_info_duration) {
3078 327 int best_fps = 0;
3079 327 double best_error = 0.01;
3080 327 AVRational codec_frame_rate = avctx->framerate;
3081
3082
1/2
✓ Branch 0 taken 327 times.
✗ Branch 1 not taken.
327 if (sti->info->codec_info_duration >= INT64_MAX / st->time_base.num / 2||
3083
1/2
✓ Branch 0 taken 327 times.
✗ Branch 1 not taken.
327 sti->info->codec_info_duration_fields >= INT64_MAX / st->time_base.den ||
3084
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 327 times.
327 sti->info->codec_info_duration < 0)
3085 ✗ continue;
3086 327 av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den,
3087 327 sti->info->codec_info_duration_fields * (int64_t) st->time_base.den,
3088 327 sti->info->codec_info_duration * 2 * (int64_t) st->time_base.num, 60000);
3089
3090 /* Round guessed framerate to a "standard" framerate if it's
3091 * within 1% of the original estimate. */
3092
2/2
✓ Branch 0 taken 130473 times.
✓ Branch 1 taken 327 times.
130800 for (int j = 0; j < MAX_STD_TIMEBASES; j++) {
3093 130473 AVRational std_fps = { get_std_framerate(j), 12 * 1001 };
3094 130473 double error = fabs(av_q2d(st->avg_frame_rate) /
3095 130473 av_q2d(std_fps) - 1);
3096
3097
2/2
✓ Branch 0 taken 903 times.
✓ Branch 1 taken 129570 times.
130473 if (error < best_error) {
3098 903 best_error = error;
3099 903 best_fps = std_fps.num;
3100 }
3101
3102
2/2
✓ Branch 1 taken 21147 times.
✓ Branch 2 taken 109326 times.
130473 if ((ffifmt(ic->iformat)->flags_internal & FF_INFMT_FLAG_PREFER_CODEC_FRAMERATE) &&
3103
2/4
✓ Branch 0 taken 21147 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 21147 times.
✗ Branch 3 not taken.
21147 codec_frame_rate.num > 0 && codec_frame_rate.den > 0) {
3104 21147 error = fabs(av_q2d(codec_frame_rate) /
3105 21147 av_q2d(std_fps) - 1);
3106
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 21137 times.
21147 if (error < best_error) {
3107 10 best_error = error;
3108 10 best_fps = std_fps.num;
3109 }
3110 }
3111 }
3112
2/2
✓ Branch 0 taken 324 times.
✓ Branch 1 taken 3 times.
327 if (best_fps)
3113 324 av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den,
3114 best_fps, 12 * 1001, INT_MAX);
3115 }
3116
2/2
✓ Branch 0 taken 2022 times.
✓ Branch 1 taken 4755 times.
6777 if (!st->r_frame_rate.num) {
3117 2022 const AVCodecDescriptor *desc = sti->codec_desc;
3118
3/4
✓ Branch 0 taken 2022 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 256 times.
✓ Branch 3 taken 1766 times.
2022 AVRational mul = (AVRational){ desc && (desc->props & AV_CODEC_PROP_FIELDS) ? 2 : 1, 1 };
3119 2022 AVRational fr = av_mul_q(avctx->framerate, mul);
3120
3121
5/6
✓ Branch 0 taken 206 times.
✓ Branch 1 taken 1816 times.
✓ Branch 2 taken 206 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 192 times.
✓ Branch 7 taken 14 times.
2022 if (fr.num && fr.den && av_cmp_q(st->time_base, av_inv_q(fr)) <= 0) {
3122 192 st->r_frame_rate = fr;
3123 } else {
3124 1830 st->r_frame_rate.num = st->time_base.den;
3125 1830 st->r_frame_rate.den = st->time_base.num;
3126 }
3127 }
3128 6777 st->codecpar->framerate = avctx->framerate;
3129
3/4
✓ Branch 0 taken 122 times.
✓ Branch 1 taken 6655 times.
✓ Branch 2 taken 122 times.
✗ Branch 3 not taken.
6777 if (sti->display_aspect_ratio.num && sti->display_aspect_ratio.den) {
3130 122 AVRational hw_ratio = { avctx->height, avctx->width };
3131 122 st->sample_aspect_ratio = av_mul_q(sti->display_aspect_ratio,
3132 hw_ratio);
3133 }
3134
2/2
✓ Branch 0 taken 2650 times.
✓ Branch 1 taken 214 times.
2864 } else if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
3135
2/2
✓ Branch 0 taken 1026 times.
✓ Branch 1 taken 1624 times.
2650 if (!avctx->bits_per_coded_sample)
3136 1026 avctx->bits_per_coded_sample =
3137 1026 av_get_bits_per_sample(avctx->codec_id);
3138 // set stream disposition based on audio service type
3139
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 2650 times.
2650 switch (avctx->audio_service_type) {
3140 ✗ case AV_AUDIO_SERVICE_TYPE_EFFECTS:
3141 ✗ st->disposition = AV_DISPOSITION_CLEAN_EFFECTS;
3142 ✗ break;
3143 ✗ case AV_AUDIO_SERVICE_TYPE_VISUALLY_IMPAIRED:
3144 ✗ st->disposition = AV_DISPOSITION_VISUAL_IMPAIRED;
3145 ✗ break;
3146 ✗ case AV_AUDIO_SERVICE_TYPE_HEARING_IMPAIRED:
3147 ✗ st->disposition = AV_DISPOSITION_HEARING_IMPAIRED;
3148 ✗ break;
3149 ✗ case AV_AUDIO_SERVICE_TYPE_COMMENTARY:
3150 ✗ st->disposition = AV_DISPOSITION_COMMENT;
3151 ✗ break;
3152 ✗ case AV_AUDIO_SERVICE_TYPE_KARAOKE:
3153 ✗ st->disposition = AV_DISPOSITION_KARAOKE;
3154 ✗ break;
3155 }
3156 }
3157 }
3158
3159
1/2
✓ Branch 0 taken 8452 times.
✗ Branch 1 not taken.
8452 if (probesize)
3160 8452 estimate_timings(ic, old_offset);
3161
3162 8452 av_opt_set_int(ic, "skip_clear", 0, AV_OPT_SEARCH_CHILDREN);
3163
3164
4/4
✓ Branch 0 taken 6996 times.
✓ Branch 1 taken 1456 times.
✓ Branch 2 taken 6993 times.
✓ Branch 3 taken 3 times.
8452 if (ret >= 0 && ic->nb_streams)
3165 /* We could not have all the codec parameters before EOF. */
3166 6993 ret = -1;
3167
2/2
✓ Branch 0 taken 9641 times.
✓ Branch 1 taken 8452 times.
18093 for (unsigned i = 0; i < ic->nb_streams; i++) {
3168 9641 AVStream *const st = ic->streams[i];
3169 9641 FFStream *const sti = ffstream(st);
3170 const char *errmsg;
3171
3172 /* if no packet was ever seen, update context now for has_codec_parameters */
3173
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 9625 times.
9641 if (!sti->avctx_inited) {
3174
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 5 times.
16 if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
3175
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 st->codecpar->format == AV_SAMPLE_FMT_NONE)
3176 11 st->codecpar->format = sti->avctx->sample_fmt;
3177 16 ret = avcodec_parameters_to_context(sti->avctx, st->codecpar);
3178
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (ret < 0)
3179 ✗ goto find_stream_info_err;
3180 }
3181
2/2
✓ Branch 1 taken 40 times.
✓ Branch 2 taken 9601 times.
9641 if (!has_codec_parameters(st, &errmsg)) {
3182 char buf[256];
3183 40 avcodec_string(buf, sizeof(buf), sti->avctx, 0);
3184 40 av_log(ic, AV_LOG_WARNING,
3185 "Could not find codec parameters for stream %d (%s): %s\n"
3186 "Consider increasing the value for the 'analyzeduration' (%"PRId64") and 'probesize' (%"PRId64") options\n",
3187 i, buf, errmsg, ic->max_analyze_duration, ic->probesize);
3188 } else {
3189 9601 ret = 0;
3190 }
3191 }
3192
3193 8452 err = compute_chapters_end(ic);
3194
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8452 times.
8452 if (err < 0) {
3195 ✗ ret = err;
3196 ✗ goto find_stream_info_err;
3197 }
3198
3199 /* update the stream parameters from the internal codec contexts */
3200
2/2
✓ Branch 0 taken 9641 times.
✓ Branch 1 taken 8452 times.
18093 for (unsigned i = 0; i < ic->nb_streams; i++) {
3201 9641 AVStream *const st = ic->streams[i];
3202 9641 FFStream *const sti = ffstream(st);
3203
3204
2/2
✓ Branch 0 taken 9625 times.
✓ Branch 1 taken 16 times.
9641 if (sti->avctx_inited) {
3205 9625 ret = parameters_from_context(ic, st->codecpar, sti->avctx);
3206
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9625 times.
9625 if (ret < 0)
3207 ✗ goto find_stream_info_err;
3208
3209
3/4
✓ Branch 0 taken 9408 times.
✓ Branch 1 taken 217 times.
✓ Branch 2 taken 9408 times.
✗ Branch 3 not taken.
9625 if (sti->avctx->rc_buffer_size > 0 || sti->avctx->rc_max_rate > 0 ||
3210
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9408 times.
9408 sti->avctx->rc_min_rate) {
3211 size_t cpb_size;
3212 217 AVCPBProperties *props = av_cpb_properties_alloc(&cpb_size);
3213
1/2
✓ Branch 0 taken 217 times.
✗ Branch 1 not taken.
217 if (props) {
3214
1/2
✓ Branch 0 taken 217 times.
✗ Branch 1 not taken.
217 if (sti->avctx->rc_buffer_size > 0)
3215 217 props->buffer_size = sti->avctx->rc_buffer_size;
3216
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 217 times.
217 if (sti->avctx->rc_min_rate > 0)
3217 ✗ props->min_bitrate = sti->avctx->rc_min_rate;
3218
2/2
✓ Branch 0 taken 79 times.
✓ Branch 1 taken 138 times.
217 if (sti->avctx->rc_max_rate > 0)
3219 79 props->max_bitrate = sti->avctx->rc_max_rate;
3220
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 217 times.
217 if (!av_packet_side_data_add(&st->codecpar->coded_side_data,
3221 217 &st->codecpar->nb_coded_side_data,
3222 AV_PKT_DATA_CPB_PROPERTIES,
3223 (uint8_t *)props, cpb_size, 0))
3224 ✗ av_free(props);
3225 }
3226 }
3227 }
3228
3229 9641 sti->avctx_inited = 0;
3230 }
3231
3232 /* update the stream group parameters from the stream contexts if needed */
3233
2/2
✓ Branch 0 taken 163 times.
✓ Branch 1 taken 8452 times.
8615 for (unsigned i = 0; i < ic->nb_stream_groups; i++) {
3234 163 AVStreamGroup *const stg = ic->stream_groups[i];
3235 AVStreamGroupLayeredVideo *stglv;
3236 const AVStreamGroupEntry *stge;
3237
3238
3/3
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 149 times.
163 switch (stg->type) {
3239 10 case AV_STREAM_GROUP_PARAMS_LCEVC:
3240 /* For LCEVC in mpegts, the parser is needed to get the
3241 * enhancement layer dimensions */
3242 10 stglv = stg->params.layered_video;
3243 10 stge = stg->entries[stglv->el_index];
3244 10 break;
3245 4 case AV_STREAM_GROUP_PARAMS_GAIN_MAP:
3246 /* A gain map is presented at the size of its base rendition */
3247 4 stglv = stg->params.layered_video;
3248
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
4 if (stg->nb_entries != 2 || stglv->el_index >= 2)
3249 ✗ continue;
3250
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 stge = stg->entries[!stglv->el_index];
3251
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 if (stge->type != AV_STREAM_GROUP_ENTRY_STREAM)
3252 2 continue;
3253 2 break;
3254 149 default:
3255 149 continue;
3256 }
3257
3258
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 11 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
12 if (stglv->width && stglv->height)
3259 1 continue;
3260
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 av_assert0(stge->type == AV_STREAM_GROUP_ENTRY_STREAM);
3261 11 stglv->width = stge->stream->codecpar->width;
3262 11 stglv->height = stge->stream->codecpar->height;
3263 }
3264
3265 8452 find_stream_info_err:
3266
2/2
✓ Branch 0 taken 9641 times.
✓ Branch 1 taken 8452 times.
18093 for (unsigned i = 0; i < ic->nb_streams; i++) {
3267 9641 AVStream *const st = ic->streams[i];
3268 9641 FFStream *const sti = ffstream(st);
3269 int err;
3270
3271
1/2
✓ Branch 0 taken 9641 times.
✗ Branch 1 not taken.
9641 if (sti->info) {
3272 9641 av_freep(&sti->info->duration_error);
3273 9641 av_freep(&sti->info);
3274 }
3275
3276
2/2
✓ Branch 1 taken 9489 times.
✓ Branch 2 taken 152 times.
9641 if (avcodec_is_open(sti->avctx)) {
3277 9489 err = codec_close(sti);
3278
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 9489 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
9489 if (err < 0 && ret >= 0)
3279 ✗ ret = err;
3280 }
3281
3282 9641 av_bsf_free(&sti->extract_extradata.bsf);
3283 }
3284
2/2
✓ Branch 0 taken 5243 times.
✓ Branch 1 taken 3209 times.
8452 if (ic->pb) {
3285 5243 FFIOContext *const ctx = ffiocontext(ic->pb);
3286 5243 av_log(ic, AV_LOG_DEBUG, "After avformat_find_stream_info() pos: %"PRId64" bytes read:%"PRId64" seeks:%d frames:%d\n",
3287 avio_tell(ic->pb), ctx->bytes_read, ctx->seek_count, count);
3288 }
3289 8452 return ret;
3290
3291 ✗ unref_then_goto_end:
3292 ✗ av_packet_unref(pkt1);
3293 ✗ goto find_stream_info_err;
3294 }
3295