FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/demux.c
Date: 2026-10-29 07:45:57
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 2679022 static int64_t wrap_timestamp(const AVStream *st, int64_t timestamp)
54 {
55 2679022 const FFStream *const sti = cffstream(st);
56
4/4
✓ Branch 0 taken 180624 times.
✓ Branch 1 taken 2498398 times.
✓ Branch 2 taken 175594 times.
✓ Branch 3 taken 5030 times.
2679022 if (sti->pts_wrap_behavior != AV_PTS_WRAP_IGNORE && st->pts_wrap_bits < 64 &&
57
4/4
✓ Branch 0 taken 174310 times.
✓ Branch 1 taken 1284 times.
✓ Branch 2 taken 111387 times.
✓ Branch 3 taken 62923 times.
175594 sti->pts_wrap_reference != AV_NOPTS_VALUE && timestamp != AV_NOPTS_VALUE) {
58
2/2
✓ Branch 0 taken 111183 times.
✓ Branch 1 taken 204 times.
111387 if (sti->pts_wrap_behavior == AV_PTS_WRAP_ADD_OFFSET &&
59
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 111183 times.
111183 timestamp < sti->pts_wrap_reference)
60 ✗ return timestamp + (1ULL << st->pts_wrap_bits);
61
2/2
✓ Branch 0 taken 204 times.
✓ Branch 1 taken 111183 times.
111387 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 2678972 return timestamp;
66 }
67
68 369194 int64_t ff_wrap_timestamp(const AVStream *st, int64_t timestamp)
69 {
70 369194 return wrap_timestamp(st, timestamp);
71 }
72
73 11123 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 10733 times.
11123 if (codec_id == AV_CODEC_ID_H264)
81 390 return avcodec_find_decoder_by_name("h264");
82 #endif
83
84 10733 codec = ff_find_decoder(s, st, codec_id);
85
2/2
✓ Branch 0 taken 247 times.
✓ Branch 1 taken 10486 times.
10733 if (!codec)
86 247 return NULL;
87
88
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10486 times.
10486 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 10486 return codec;
101 }
102
103 3446 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 3446 const AVInputFormat *fmt = av_probe_input_format3(pd, 1, &score);
133 3446 FFStream *const sti = ffstream(st);
134
135
2/2
✓ Branch 0 taken 1252 times.
✓ Branch 1 taken 2194 times.
3446 if (fmt) {
136 1252 av_log(s, AV_LOG_DEBUG,
137 "Probe with size=%d, packets=%d detected %s with score=%d\n",
138 1252 pd->buf_size, s->max_probe_packets - sti->probe_packets,
139 1252 fmt->name, score);
140
2/2
✓ Branch 0 taken 22243 times.
✓ Branch 1 taken 1198 times.
23441 for (int i = 0; fmt_id_type[i].name; i++) {
141
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 22183 times.
22243 if (!strcmp(fmt->name, fmt_id_type[i].name)) {
142
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 47 times.
60 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 36 times.
✓ Branch 1 taken 24 times.
60 if (sti->request_probe > score &&
146
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 30 times.
36 st->codecpar->codec_id != fmt_id_type[i].id)
147 6 continue;
148 54 st->codecpar->codec_id = fmt_id_type[i].id;
149 54 st->codecpar->codec_type = fmt_id_type[i].type;
150 54 sti->need_context_update = 1;
151 54 return score;
152 }
153 }
154 }
155 3392 return 0;
156 }
157
158 8514 static int init_input(AVFormatContext *s, const char *filename,
159 AVDictionary **options)
160 {
161 int ret;
162 8514 AVProbeData pd = { filename, NULL, 0 };
163 8514 int score = AVPROBE_SCORE_RETRY;
164
165
2/2
✓ Branch 0 taken 41 times.
✓ Branch 1 taken 8473 times.
8514 if (s->pb) {
166 41 s->flags |= AVFMT_FLAG_CUSTOM_IO;
167
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 41 times.
41 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 41 times.
41 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 41 return 0;
174 }
175
176
4/4
✓ Branch 0 taken 4002 times.
✓ Branch 1 taken 4471 times.
✓ Branch 2 taken 991 times.
✓ Branch 3 taken 3011 times.
8473 if ((s->iformat && s->iformat->flags & AVFMT_NOFILE) ||
177
4/4
✓ Branch 0 taken 4471 times.
✓ Branch 1 taken 991 times.
✓ Branch 3 taken 199 times.
✓ Branch 4 taken 4272 times.
5462 (!s->iformat && (s->iformat = av_probe_input_format2(&pd, 0, &score))))
178 3210 return score;
179
180
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5263 times.
5263 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 991 times.
✓ Branch 1 taken 4272 times.
5263 if (s->iformat)
184 991 return 0;
185 4272 return av_probe_input_buffer2(s->pb, &s->iformat, filename,
186 4272 s, 0, s->format_probesize);
187 }
188
189 static int codec_close(FFStream *sti);
190
191 225831 static int update_stream_avctx(AVFormatContext *s)
192 {
193 int ret;
194
2/2
✓ Branch 0 taken 411763 times.
✓ Branch 1 taken 225831 times.
637594 for (unsigned i = 0; i < s->nb_streams; i++) {
195 411763 AVStream *const st = s->streams[i];
196 411763 FFStream *const sti = ffstream(st);
197
198
2/2
✓ Branch 0 taken 402112 times.
✓ Branch 1 taken 9651 times.
411763 if (!sti->need_context_update)
199 402112 continue;
200
201
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 9649 times.
9651 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 9649 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
9651 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 9651 ret = avcodec_parameters_to_context(sti->avctx, st->codecpar);
217
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9651 times.
9651 if (ret < 0)
218 ✗ return ret;
219
220 9651 sti->codec_desc = avcodec_descriptor_get(sti->avctx->codec_id);
221
222 9651 sti->need_context_update = 0;
223 }
224 225831 return 0;
225 }
226
227 5304 static av_always_inline int is_id3v2_format(const AVInputFormat *fmt) {
228 5304 return ffifmt(fmt)->flags_internal & FF_INFMT_FLAG_ID3V2_AUTO;
229 }
230
231 8514 int avformat_open_input(AVFormatContext **ps, const char *filename,
232 const AVInputFormat *fmt, AVDictionary **options)
233 {
234 FormatContextInternal *fci;
235 8514 AVFormatContext *s = *ps;
236 FFFormatContext *si;
237 8514 AVDictionary *tmp = NULL;
238 8514 ID3v2ExtraMeta *id3v2_extra_meta = NULL;
239 8514 int ret = 0;
240
241
3/4
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 8483 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 31 times.
8514 if (!s && !(s = avformat_alloc_context()))
242 ✗ return AVERROR(ENOMEM);
243 8514 fci = ff_fc_internal(s);
244 8514 si = &fci->fc;
245
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8514 times.
8514 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 4043 times.
✓ Branch 1 taken 4471 times.
8514 if (fmt)
250 4043 s->iformat = fmt;
251
252
2/2
✓ Branch 0 taken 8486 times.
✓ Branch 1 taken 28 times.
8514 if (options)
253 8486 av_dict_copy(&tmp, *options, 0);
254
255
2/2
✓ Branch 0 taken 41 times.
✓ Branch 1 taken 8473 times.
8514 if (s->pb) // must be before any goto fail
256 41 s->flags |= AVFMT_FLAG_CUSTOM_IO;
257
258
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8514 times.
8514 if ((ret = av_opt_set_dict(s, &tmp)) < 0)
259 ✗ goto fail;
260
261
3/4
✓ Branch 0 taken 8504 times.
✓ Branch 1 taken 10 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8514 times.
8514 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 8514 times.
8514 if ((ret = init_input(s, filename, &tmp)) < 0)
267 ✗ goto fail;
268 8514 s->probe_score = ret;
269
270
6/6
✓ Branch 0 taken 8400 times.
✓ Branch 1 taken 114 times.
✓ Branch 2 taken 5190 times.
✓ Branch 3 taken 3210 times.
✓ Branch 4 taken 5179 times.
✓ Branch 5 taken 11 times.
8514 if (!s->protocol_whitelist && s->pb && s->pb->protocol_whitelist) {
271 5179 s->protocol_whitelist = av_strdup(s->pb->protocol_whitelist);
272
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5179 times.
5179 if (!s->protocol_whitelist) {
273 ✗ ret = AVERROR(ENOMEM);
274 ✗ goto fail;
275 }
276 }
277
278
4/6
✓ Branch 0 taken 8514 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5304 times.
✓ Branch 3 taken 3210 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 5304 times.
8514 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 8514 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
8514 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 8514 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 8514 times.
8514 if (s->iformat->flags & AVFMT_NEEDNUMBER) {
296 ✗ if (!av_filename_number_test(filename)) {
297 ✗ ret = AVERROR(EINVAL);
298 ✗ goto fail;
299 }
300 }
301
302 8514 s->duration = s->start_time = AV_NOPTS_VALUE;
303
304 /* Allocate private data. */
305
2/2
✓ Branch 1 taken 8308 times.
✓ Branch 2 taken 206 times.
8514 if (ffifmt(s->iformat)->priv_data_size > 0) {
306
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 8308 times.
8308 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 7289 times.
✓ Branch 1 taken 1019 times.
8308 if (s->iformat->priv_class) {
311 7289 *(const AVClass **) s->priv_data = s->iformat->priv_class;
312 7289 av_opt_set_defaults(s->priv_data);
313
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7289 times.
7289 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 5304 times.
✓ Branch 1 taken 3210 times.
✓ Branch 3 taken 825 times.
✓ Branch 4 taken 4479 times.
8514 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 8514 times.
✗ Branch 2 not taken.
8514 if (ffifmt(s->iformat)->read_header)
323
2/2
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 8510 times.
8514 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 6839 times.
✓ Branch 1 taken 1671 times.
8510 if (!s->metadata) {
330 6839 s->metadata = si->id3v2_meta;
331 6839 si->id3v2_meta = NULL;
332
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1671 times.
1671 } 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 8473 times.
8510 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 8510 times.
8510 if ((ret = avformat_queue_attached_pictures(s)) < 0)
348 ✗ goto close;
349
350
4/4
✓ Branch 0 taken 5300 times.
✓ Branch 1 taken 3210 times.
✓ Branch 2 taken 4744 times.
✓ Branch 3 taken 556 times.
8510 if (s->pb && !si->data_offset)
351 4744 si->data_offset = avio_tell(s->pb);
352
353 8510 fci->raw_packet_buffer_size = 0;
354
355 8510 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 8510 times.
8673 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 8482 times.
✓ Branch 1 taken 28 times.
8510 if (options) {
381 8482 av_dict_free(options);
382 8482 *options = tmp;
383 }
384 8510 *ps = s;
385 8510 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 8514 void avformat_close_input(AVFormatContext **ps)
401 {
402 AVFormatContext *s;
403 AVIOContext *pb;
404
405
3/4
✓ Branch 0 taken 8514 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 8510 times.
8514 if (!ps || !*ps)
406 4 return;
407
408 8510 s = *ps;
409 8510 pb = s->pb;
410
411
6/6
✓ Branch 0 taken 8508 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 5358 times.
✓ Branch 3 taken 3150 times.
✓ Branch 4 taken 5271 times.
✓ Branch 5 taken 87 times.
8510 if ((s->iformat && strcmp(s->iformat->name, "image2") && s->iformat->flags & AVFMT_NOFILE) ||
412
2/2
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 8384 times.
8423 (s->flags & AVFMT_FLAG_CUSTOM_IO))
413 126 pb = NULL;
414
415
2/2
✓ Branch 0 taken 8508 times.
✓ Branch 1 taken 2 times.
8510 if (s->iformat)
416
2/2
✓ Branch 1 taken 5497 times.
✓ Branch 2 taken 3011 times.
8508 if (ffifmt(s->iformat)->read_close)
417 5497 ffifmt(s->iformat)->read_close(s);
418
419 8510 ff_format_io_close(s, &pb);
420 8510 avformat_free_context(s);
421
422 8510 *ps = NULL;
423 }
424
425 1158359 static void force_codec_ids(AVFormatContext *s, AVStream *st)
426 {
427
5/5
✓ Branch 0 taken 759135 times.
✓ Branch 1 taken 395543 times.
✓ Branch 2 taken 3465 times.
✓ Branch 3 taken 214 times.
✓ Branch 4 taken 2 times.
1158359 switch (st->codecpar->codec_type) {
428 759135 case AVMEDIA_TYPE_VIDEO:
429
2/2
✓ Branch 0 taken 118025 times.
✓ Branch 1 taken 641110 times.
759135 if (s->video_codec_id)
430 118025 st->codecpar->codec_id = s->video_codec_id;
431 759135 break;
432 395543 case AVMEDIA_TYPE_AUDIO:
433
2/2
✓ Branch 0 taken 4620 times.
✓ Branch 1 taken 390923 times.
395543 if (s->audio_codec_id)
434 4620 st->codecpar->codec_id = s->audio_codec_id;
435 395543 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 1158359 }
446
447 26185 static int probe_codec(AVFormatContext *s, AVStream *st, const AVPacket *pkt)
448 {
449 26185 FormatContextInternal *const fci = ff_fc_internal(s);
450 26185 FFStream *const sti = ffstream(st);
451
452
2/2
✓ Branch 0 taken 17920 times.
✓ Branch 1 taken 8265 times.
26185 if (sti->request_probe > 0) {
453 17920 AVProbeData *const pd = &sti->probe_data;
454 int end;
455 17920 av_log(s, AV_LOG_DEBUG, "probing stream %d pp:%d\n", st->index, sti->probe_packets);
456 17920 --sti->probe_packets;
457
458
2/2
✓ Branch 0 taken 17901 times.
✓ Branch 1 taken 19 times.
17920 if (pkt) {
459 17901 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 17901 times.
17901 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 17901 pd->buf = new_buf;
467 17901 memcpy(pd->buf + pd->buf_size, pkt->data, pkt->size);
468 17901 pd->buf_size += pkt->size;
469 17901 memset(pd->buf + pd->buf_size, 0, AVPROBE_PADDING_SIZE);
470 } else {
471 19 no_packet:
472 19 sti->probe_packets = 0;
473
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 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 17920 times.
✗ Branch 1 not taken.
35840 end = fci->raw_packet_buffer_size >= s->probesize ||
480
2/2
✓ Branch 0 taken 563 times.
✓ Branch 1 taken 17357 times.
17920 sti->probe_packets <= 0;
481
482
4/4
✓ Branch 0 taken 17357 times.
✓ Branch 1 taken 563 times.
✓ Branch 2 taken 2883 times.
✓ Branch 3 taken 14474 times.
17920 if (end || av_log2(pd->buf_size) != av_log2(pd->buf_size - pkt->size)) {
483 3446 int score = set_codec_from_probe_data(s, st, pd);
484
4/4
✓ Branch 0 taken 3438 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 3409 times.
✓ Branch 3 taken 29 times.
3446 if ( (st->codecpar->codec_id != AV_CODEC_ID_NONE && score > AVPROBE_SCORE_STREAM_RETRY)
485
2/2
✓ Branch 0 taken 563 times.
✓ Branch 1 taken 2854 times.
3417 || end) {
486 592 pd->buf_size = 0;
487 592 av_freep(&pd->buf);
488 592 sti->request_probe = -1;
489
1/2
✓ Branch 0 taken 592 times.
✗ Branch 1 not taken.
592 if (st->codecpar->codec_id != AV_CODEC_ID_NONE) {
490 592 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 3446 force_codec_ids(s, st);
495 }
496 }
497 26185 return 0;
498 }
499
500 1154913 static int update_wrap_reference(AVFormatContext *s, AVStream *st, int stream_index, AVPacket *pkt)
501 {
502 1154913 FFStream *const sti = ffstream(st);
503 1154913 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 933763 times.
✓ Branch 1 taken 221150 times.
1154913 if (ref == AV_NOPTS_VALUE)
509 933763 ref = pkt->pts;
510
7/8
✓ Branch 0 taken 1074568 times.
✓ Branch 1 taken 80345 times.
✓ Branch 2 taken 58950 times.
✓ Branch 3 taken 1015618 times.
✓ Branch 4 taken 1033 times.
✓ Branch 5 taken 57917 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 1033 times.
1154913 if (sti->pts_wrap_reference != AV_NOPTS_VALUE || st->pts_wrap_bits >= 63 || ref == AV_NOPTS_VALUE || !s->correct_ts_overflow)
511 1153880 return 0;
512 1033 ref &= (1LL << st->pts_wrap_bits)-1;
513
514 // reference time stamp should be 60 s before first time stamp
515 1033 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 2067 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 1032 times.
1034 AV_PTS_WRAP_ADD_OFFSET : AV_PTS_WRAP_SUB_OFFSET;
520
521 1033 first_program = av_find_program_from_stream(s, NULL, stream_index);
522
523
2/2
✓ Branch 0 taken 232 times.
✓ Branch 1 taken 801 times.
1033 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 801 AVProgram *program = first_program;
540
2/2
✓ Branch 0 taken 802 times.
✓ Branch 1 taken 119 times.
921 while (program) {
541
2/2
✓ Branch 0 taken 682 times.
✓ Branch 1 taken 120 times.
802 if (program->pts_wrap_reference != AV_NOPTS_VALUE) {
542 682 pts_wrap_reference = program->pts_wrap_reference;
543 682 pts_wrap_behavior = program->pts_wrap_behavior;
544 682 break;
545 }
546 120 program = av_find_program_from_stream(s, program, stream_index);
547 }
548
549 // update every program with differing pts_wrap_reference
550 801 program = first_program;
551
2/2
✓ Branch 0 taken 802 times.
✓ Branch 1 taken 801 times.
1603 while (program) {
552
2/2
✓ Branch 0 taken 120 times.
✓ Branch 1 taken 682 times.
802 if (program->pts_wrap_reference != pts_wrap_reference) {
553
2/2
✓ Branch 0 taken 182 times.
✓ Branch 1 taken 120 times.
302 for (unsigned i = 0; i < program->nb_stream_indexes; i++) {
554 182 FFStream *const sti = ffstream(s->streams[program->stream_index[i]]);
555 182 sti->pts_wrap_reference = pts_wrap_reference;
556 182 sti->pts_wrap_behavior = pts_wrap_behavior;
557 }
558
559 120 program->pts_wrap_reference = pts_wrap_reference;
560 120 program->pts_wrap_behavior = pts_wrap_behavior;
561 }
562 802 program = av_find_program_from_stream(s, program, stream_index);
563 }
564 }
565 1033 return 1;
566 }
567
568 1154913 static void update_timestamps(AVFormatContext *s, AVStream *st, AVPacket *pkt)
569 {
570 1154913 FFStream *const sti = ffstream(st);
571
572
4/4
✓ Branch 1 taken 1033 times.
✓ Branch 2 taken 1153880 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 1032 times.
1154913 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 1154913 pkt->dts = wrap_timestamp(st, pkt->dts);
583 1154913 pkt->pts = wrap_timestamp(st, pkt->pts);
584
585 1154913 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 1154913 times.
1154913 if (s->use_wallclock_as_timestamps)
589 ✗ pkt->dts = pkt->pts = av_rescale_q(av_gettime(), AV_TIME_BASE_Q, st->time_base);
590 1154913 }
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 1154913 static int handle_new_packet(AVFormatContext *s, AVPacket *pkt, int allow_passthrough)
601 {
602 1154913 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 1154913 times.
1154913 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 1154700 times.
1154913 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 1154913 st = s->streams[pkt->stream_index];
622 1154913 sti = ffstream(st);
623
624 1154913 update_timestamps(s, st, pkt);
625
626
6/6
✓ Branch 0 taken 1137012 times.
✓ Branch 1 taken 17901 times.
✓ Branch 2 taken 1128789 times.
✓ Branch 3 taken 8223 times.
✓ Branch 4 taken 1128747 times.
✓ Branch 5 taken 42 times.
1154913 if (sti->request_probe <= 0 && allow_passthrough && !fci->raw_packet_buffer.head)
627 1128747 return 0;
628
629 26166 err = ff_packet_list_put(&fci->raw_packet_buffer, pkt, NULL, 0);
630
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26166 times.
26166 if (err < 0) {
631 ✗ av_packet_unref(pkt);
632 ✗ return err;
633 }
634
635 26166 pkt = &fci->raw_packet_buffer.tail->pkt;
636 26166 fci->raw_packet_buffer_size += pkt->size;
637
638 26166 err = probe_codec(s, st, pkt);
639
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26166 times.
26166 if (err < 0)
640 ✗ return err;
641
642 26166 return 1;
643 }
644
645 8223 int ff_buffer_packet(AVFormatContext *s, AVPacket *pkt)
646 {
647 8223 int err = handle_new_packet(s, pkt, 0);
648
649 8223 return err < 0 ? err : 0;
650 }
651
652 1162395 int ff_read_packet(AVFormatContext *s, AVPacket *pkt)
653 {
654 1162395 FormatContextInternal *const fci = ff_fc_internal(s);
655 int err;
656
657 #if FF_API_INIT_PACKET
658 FF_DISABLE_DEPRECATION_WARNINGS
659 1162395 pkt->data = NULL;
660 1162395 pkt->size = 0;
661 1162395 av_init_packet(pkt);
662 FF_ENABLE_DEPRECATION_WARNINGS
663 #else
664 av_packet_unref(pkt);
665 #endif
666
667 26504 for (;;) {
668 1188899 PacketListEntry *pktl = fci->raw_packet_buffer.head;
669
670
2/2
✓ Branch 0 taken 43592 times.
✓ Branch 1 taken 1145307 times.
1188899 if (pktl) {
671 43592 AVStream *const st = s->streams[pktl->pkt.stream_index];
672
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 43592 times.
43592 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 26222 times.
✓ Branch 2 taken 17370 times.
43592 if (ffstream(st)->request_probe <= 0) {
676 26222 ff_packet_list_get(&fci->raw_packet_buffer, pkt);
677 26222 fci->raw_packet_buffer_size -= pkt->size;
678 26222 return 0;
679 }
680 }
681
682 1162677 err = ffifmt(s->iformat)->read_packet(s, pkt);
683
2/2
✓ Branch 0 taken 15987 times.
✓ Branch 1 taken 1146690 times.
1162677 if (err < 0) {
684 15987 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 8542 times.
✓ Branch 1 taken 7445 times.
15987 if (err == FFERROR_REDO)
690 8542 continue;
691
3/4
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 7426 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 19 times.
7445 if (!pktl || err == AVERROR(EAGAIN))
692 7426 return err;
693
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 19 times.
38 for (unsigned i = 0; i < s->nb_streams; i++) {
694 19 AVStream *const st = s->streams[i];
695 19 FFStream *const sti = ffstream(st);
696
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
19 if (sti->probe_packets || sti->request_probe > 0)
697
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
19 if ((err = probe_codec(s, st, NULL)) < 0)
698 ✗ return err;
699
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 av_assert0(sti->request_probe <= 0);
700 }
701 19 continue;
702 }
703
704 1146690 err = av_packet_make_refcounted(pkt);
705
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1146690 times.
1146690 if (err < 0) {
706 ✗ av_packet_unref(pkt);
707 ✗ return err;
708 }
709
710 1146690 err = handle_new_packet(s, pkt, 1);
711
2/2
✓ Branch 0 taken 1128747 times.
✓ Branch 1 taken 17943 times.
1146690 if (err <= 0) /* Error or passthrough */
712 1128747 return err;
713 }
714 }
715
716 /**
717 * Return the frame duration in seconds. Return 0 if not available.
718 */
719 345084 static void compute_frame_duration(AVFormatContext *s, int *pnum, int *pden,
720 AVStream *st, AVCodecParserContext *pc,
721 AVPacket *pkt)
722 {
723 345084 FFStream *const sti = ffstream(st);
724 345084 AVRational codec_framerate = sti->avctx->framerate;
725 int frame_size, sample_rate;
726
727 345084 *pnum = 0;
728 345084 *pden = 0;
729
3/3
✓ Branch 0 taken 229838 times.
✓ Branch 1 taken 113647 times.
✓ Branch 2 taken 1599 times.
345084 switch (st->codecpar->codec_type) {
730 229838 case AVMEDIA_TYPE_VIDEO:
731
6/6
✓ Branch 0 taken 197449 times.
✓ Branch 1 taken 32389 times.
✓ Branch 2 taken 44523 times.
✓ Branch 3 taken 152926 times.
✓ Branch 4 taken 31433 times.
✓ Branch 5 taken 13090 times.
229838 if (st->r_frame_rate.num && (!pc || !codec_framerate.num)) {
732 184359 *pnum = st->r_frame_rate.den;
733 184359 *pden = st->r_frame_rate.num;
734
2/2
✓ Branch 0 taken 24872 times.
✓ Branch 1 taken 20607 times.
45479 } 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 9403 times.
✓ Branch 1 taken 20697 times.
30100 } else if (st->time_base.num * 1000LL > st->time_base.den) {
740 9403 *pnum = st->time_base.num;
741 9403 *pden = st->time_base.den;
742
2/2
✓ Branch 0 taken 20672 times.
✓ Branch 1 taken 25 times.
20697 } else if (codec_framerate.den * 1000LL > codec_framerate.num) {
743 62016 int ticks_per_frame = (sti->codec_desc &&
744
3/4
✓ Branch 0 taken 20672 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14375 times.
✓ Branch 3 taken 6297 times.
20672 (sti->codec_desc->props & AV_CODEC_PROP_FIELDS)) ? 2 : 1;
745 20672 av_reduce(pnum, pden,
746 20672 codec_framerate.den,
747 20672 codec_framerate.num * (int64_t)ticks_per_frame,
748 INT_MAX);
749
750
4/4
✓ Branch 0 taken 18634 times.
✓ Branch 1 taken 2038 times.
✓ Branch 2 taken 13745 times.
✓ Branch 3 taken 4889 times.
20672 if (pc && pc->repeat_pict) {
751 13745 av_reduce(pnum, pden,
752 13745 (*pnum) * (1LL + pc->repeat_pict),
753 13745 (*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 20672 times.
✗ Branch 1 not taken.
20672 if (sti->codec_desc &&
760
3/4
✓ Branch 0 taken 14375 times.
✓ Branch 1 taken 6297 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 14375 times.
20672 (sti->codec_desc->props & AV_CODEC_PROP_FIELDS) && !pc)
761 ✗ *pnum = *pden = 0;
762 }
763 229838 break;
764 113647 case AVMEDIA_TYPE_AUDIO:
765
2/2
✓ Branch 0 taken 50287 times.
✓ Branch 1 taken 63360 times.
113647 if (sti->avctx_inited) {
766 50287 frame_size = av_get_audio_frame_duration(sti->avctx, pkt->size);
767 50287 sample_rate = sti->avctx->sample_rate;
768 } else {
769 63360 frame_size = av_get_audio_frame_duration2(st->codecpar, pkt->size);
770 63360 sample_rate = st->codecpar->sample_rate;
771 }
772
4/4
✓ Branch 0 taken 107181 times.
✓ Branch 1 taken 6466 times.
✓ Branch 2 taken 107176 times.
✓ Branch 3 taken 5 times.
113647 if (frame_size <= 0 || sample_rate <= 0)
773 break;
774 107176 *pnum = frame_size;
775 107176 *pden = sample_rate;
776 107176 break;
777 1599 default:
778 1599 break;
779 }
780 345084 }
781
782 831727 static int has_decode_delay_been_guessed(AVStream *st)
783 {
784 831727 FFStream *const sti = ffstream(st);
785
2/2
✓ Branch 0 taken 812258 times.
✓ Branch 1 taken 19469 times.
831727 if (st->codecpar->codec_id != AV_CODEC_ID_H264) return 1;
786
2/2
✓ Branch 0 taken 7264 times.
✓ Branch 1 taken 12205 times.
19469 if (!sti->info) // if we have left find_stream_info then nb_decoded_frames won't increase anymore for stream copy
787 7264 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 384234 static PacketListEntry *get_next_pkt(AVFormatContext *s, AVStream *st,
803 PacketListEntry *pktl)
804 {
805 384234 FormatContextInternal *const fci = ff_fc_internal(s);
806 384234 FFFormatContext *const si = &fci->fc;
807
2/2
✓ Branch 0 taken 381506 times.
✓ Branch 1 taken 2728 times.
384234 if (pktl->next)
808 381506 return pktl->next;
809
2/2
✓ Branch 0 taken 2694 times.
✓ Branch 1 taken 34 times.
2728 if (pktl == si->packet_buffer.tail)
810 2694 return fci->parse_queue.head;
811 34 return NULL;
812 }
813
814 615538 static int64_t select_from_pts_buffer(AVStream *st, int64_t *pts_buffer, int64_t dts)
815 {
816 615538 FFStream *const sti = ffstream(st);
817 1837691 int onein_oneout = st->codecpar->codec_id != AV_CODEC_ID_H264 &&
818
4/4
✓ Branch 0 taken 606615 times.
✓ Branch 1 taken 8923 times.
✓ Branch 2 taken 604885 times.
✓ Branch 3 taken 1730 times.
1220423 st->codecpar->codec_id != AV_CODEC_ID_HEVC &&
819
2/2
✓ Branch 0 taken 604669 times.
✓ Branch 1 taken 216 times.
604885 st->codecpar->codec_id != AV_CODEC_ID_VVC;
820
821
2/2
✓ Branch 0 taken 10869 times.
✓ Branch 1 taken 604669 times.
615538 if (!onein_oneout) {
822 10869 int delay = sti->avctx->has_b_frames;
823
824
2/2
✓ Branch 0 taken 1434 times.
✓ Branch 1 taken 9435 times.
10869 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 15973 times.
✓ Branch 1 taken 9435 times.
25408 for (int i = 0; i < delay; i++) {
837
2/2
✓ Branch 0 taken 14889 times.
✓ Branch 1 taken 1084 times.
15973 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 10674 times.
14889 uint64_t diff = ABSDIFF(pts_buffer[i], dts);
840
841
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14889 times.
14889 if (diff > INT64_MAX - sti->pts_reorder_error[i]) {
842 ✗ diff = INT64_MAX;
843 } else
844 14889 diff += sti->pts_reorder_error[i];
845
846 14889 sti->pts_reorder_error[i] = diff;
847 14889 sti->pts_reorder_error_count[i]++;
848
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 14882 times.
14889 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 613729 times.
615538 if (dts == AV_NOPTS_VALUE)
858 1809 dts = pts_buffer[0];
859
860 615538 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 6984 static void update_dts_from_pts(AVFormatContext *s, int stream_index,
868 PacketListEntry *pkt_buffer)
869 {
870 6984 AVStream *const st = s->streams[stream_index];
871 6984 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 118728 times.
✓ Branch 1 taken 6984 times.
125712 for (int i = 0; i < MAX_REORDER_DELAY + 1; i++)
876 118728 pts_buffer[i] = AV_NOPTS_VALUE;
877
878
2/2
✓ Branch 1 taken 75917 times.
✓ Branch 2 taken 6984 times.
82901 for (; pkt_buffer; pkt_buffer = get_next_pkt(s, st, pkt_buffer)) {
879
2/2
✓ Branch 0 taken 75184 times.
✓ Branch 1 taken 733 times.
75917 if (pkt_buffer->pkt.stream_index != stream_index)
880 75184 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 6984 }
891
892 658050 static void update_initial_timestamps(AVFormatContext *s, int stream_index,
893 int64_t dts, int64_t pts, AVPacket *pkt)
894 {
895 658050 FormatContextInternal *const fci = ff_fc_internal(s);
896 658050 FFFormatContext *const si = &fci->fc;
897 658050 AVStream *const st = s->streams[stream_index];
898 658050 FFStream *const sti = ffstream(st);
899
2/2
✓ Branch 0 taken 197675 times.
✓ Branch 1 taken 460375 times.
658050 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 236688 times.
✓ Branch 1 taken 421362 times.
✓ Branch 2 taken 7083 times.
✓ Branch 3 taken 229605 times.
658050 if (sti->first_dts != AV_NOPTS_VALUE ||
904 7083 dts == AV_NOPTS_VALUE ||
905
1/2
✓ Branch 0 taken 7083 times.
✗ Branch 1 not taken.
7083 sti->cur_dts == AV_NOPTS_VALUE ||
906
1/2
✓ Branch 0 taken 7083 times.
✗ Branch 1 not taken.
7083 sti->cur_dts < INT_MIN + RELATIVE_TS_BASE ||
907
2/4
✓ Branch 0 taken 7083 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 7083 times.
14166 dts < INT_MIN + (sti->cur_dts - RELATIVE_TS_BASE) ||
908 7083 is_relative(dts))
909 650967 return;
910
911 7083 sti->first_dts = dts - (sti->cur_dts - RELATIVE_TS_BASE);
912 7083 sti->cur_dts = dts;
913 7083 shift = (uint64_t)sti->first_dts - RELATIVE_TS_BASE;
914
915
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7083 times.
7083 if (is_relative(pts))
916 ✗ pts += shift;
917
918
2/2
✓ Branch 1 taken 75733 times.
✓ Branch 2 taken 7083 times.
82816 for (PacketListEntry *pktl_it = pktl; pktl_it; pktl_it = get_next_pkt(s, st, pktl_it)) {
919
2/2
✓ Branch 0 taken 75203 times.
✓ Branch 1 taken 530 times.
75733 if (pktl_it->pkt.stream_index != stream_index)
920 75203 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 6963 times.
✓ Branch 2 taken 120 times.
7083 if (has_decode_delay_been_guessed(st))
935 6963 update_dts_from_pts(s, stream_index, pktl);
936
937
2/2
✓ Branch 0 taken 2063 times.
✓ Branch 1 taken 5020 times.
7083 if (st->start_time == AV_NOPTS_VALUE) {
938
3/4
✓ Branch 0 taken 1372 times.
✓ Branch 1 taken 691 times.
✓ Branch 2 taken 1372 times.
✗ Branch 3 not taken.
2063 if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO || !(pkt->flags & AV_PKT_FLAG_DISCARD)) {
939 2063 st->start_time = pts;
940 }
941
4/4
✓ Branch 0 taken 691 times.
✓ Branch 1 taken 1372 times.
✓ Branch 2 taken 582 times.
✓ Branch 3 taken 109 times.
2063 if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && st->codecpar->sample_rate)
942 582 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 284115 static void update_initial_durations(AVFormatContext *s, AVStream *st,
947 int stream_index, int64_t duration)
948 {
949 284115 FormatContextInternal *const fci = ff_fc_internal(s);
950 284115 FFFormatContext *const si = &fci->fc;
951 284115 FFStream *const sti = ffstream(st);
952
2/2
✓ Branch 0 taken 194107 times.
✓ Branch 1 taken 90008 times.
284115 PacketListEntry *pktl = si->packet_buffer.head ? si->packet_buffer.head : fci->parse_queue.head;
953 284115 int64_t cur_dts = RELATIVE_TS_BASE;
954
955
2/2
✓ Branch 0 taken 139132 times.
✓ Branch 1 taken 144983 times.
284115 if (sti->first_dts != AV_NOPTS_VALUE) {
956
2/2
✓ Branch 0 taken 135190 times.
✓ Branch 1 taken 3942 times.
139132 if (sti->update_initial_durations_done)
957 135190 return;
958 3942 sti->update_initial_durations_done = 1;
959 3942 cur_dts = sti->first_dts;
960
2/2
✓ Branch 1 taken 73757 times.
✓ Branch 2 taken 2 times.
73759 for (; pktl; pktl = get_next_pkt(s, st, pktl)) {
961
2/2
✓ Branch 0 taken 3956 times.
✓ Branch 1 taken 69801 times.
73757 if (pktl->pkt.stream_index == stream_index) {
962
2/2
✓ Branch 0 taken 3808 times.
✓ Branch 1 taken 148 times.
3956 if (pktl->pkt.pts != pktl->pkt.dts ||
963
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 3792 times.
3808 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 3940 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 41 times.
✓ Branch 3 taken 3899 times.
3942 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 3899 times.
3901 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 3875 times.
✓ Branch 1 taken 24 times.
3899 pktl = si->packet_buffer.head ? si->packet_buffer.head : fci->parse_queue.head;
979 3899 sti->first_dts = cur_dts;
980
2/2
✓ Branch 0 taken 118977 times.
✓ Branch 1 taken 26006 times.
144983 } else if (sti->cur_dts != RELATIVE_TS_BASE)
981 118977 return;
982
983
2/2
✓ Branch 1 taken 191748 times.
✓ Branch 2 taken 924 times.
192672 for (; pktl; pktl = get_next_pkt(s, st, pktl)) {
984
2/2
✓ Branch 0 taken 162548 times.
✓ Branch 1 taken 29200 times.
191748 if (pktl->pkt.stream_index != stream_index)
985 162548 continue;
986
2/2
✓ Branch 0 taken 871 times.
✓ Branch 1 taken 28329 times.
29200 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 3941 times.
✓ Branch 1 taken 24403 times.
28344 (pktl->pkt.dts == AV_NOPTS_VALUE ||
989
2/2
✓ Branch 0 taken 179 times.
✓ Branch 1 taken 3762 times.
3941 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 27948 times.
28167 !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 28981 times.
29905 if (!pktl)
1003 924 sti->cur_dts = cur_dts;
1004 }
1005
1006 664474 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 664474 FormatContextInternal *const fci = ff_fc_internal(s);
1011 664474 FFFormatContext *const si = &fci->fc;
1012 664474 FFStream *const sti = ffstream(st);
1013 int num, den, presentation_delayed, delay;
1014 int64_t offset;
1015 AVRational duration;
1016 1957114 int onein_oneout = st->codecpar->codec_id != AV_CODEC_ID_H264 &&
1017
2/2
✓ Branch 0 taken 613802 times.
✓ Branch 1 taken 14364 times.
628166 st->codecpar->codec_id != AV_CODEC_ID_HEVC &&
1018
4/4
✓ Branch 0 taken 628166 times.
✓ Branch 1 taken 36308 times.
✓ Branch 2 taken 610407 times.
✓ Branch 3 taken 3395 times.
1903047 st->codecpar->codec_id != AV_CODEC_ID_VVC &&
1019
2/2
✓ Branch 0 taken 610397 times.
✓ Branch 1 taken 10 times.
610407 st->codecpar->codec_id != AV_CODEC_ID_IMM6;
1020
1021
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 664464 times.
664474 if (s->flags & AVFMT_FLAG_NOFILLIN)
1022 10 return;
1023
1024
4/4
✓ Branch 0 taken 254812 times.
✓ Branch 1 taken 409652 times.
✓ Branch 2 taken 77586 times.
✓ Branch 3 taken 177226 times.
664464 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && pkt->dts != AV_NOPTS_VALUE) {
1025
4/4
✓ Branch 0 taken 44518 times.
✓ Branch 1 taken 33068 times.
✓ Branch 2 taken 43173 times.
✓ Branch 3 taken 1345 times.
77586 if (pkt->dts == pkt->pts && sti->last_dts_for_order_check != AV_NOPTS_VALUE) {
1026
2/2
✓ Branch 0 taken 43164 times.
✓ Branch 1 taken 9 times.
43173 if (sti->last_dts_for_order_check <= pkt->dts) {
1027 43164 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 43144 times.
43173 if (sti->dts_ordered + sti->dts_misordered > 250) {
1036 29 sti->dts_ordered >>= 1;
1037 29 sti->dts_misordered >>= 1;
1038 }
1039 }
1040
1041 77586 sti->last_dts_for_order_check = pkt->dts;
1042
4/4
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 77549 times.
✓ Branch 2 taken 20 times.
✓ Branch 3 taken 17 times.
77586 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 664464 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
664464 if ((s->flags & AVFMT_FLAG_IGNDTS) && pkt->pts != AV_NOPTS_VALUE)
1047 ✗ pkt->dts = AV_NOPTS_VALUE;
1048
1049
4/4
✓ Branch 0 taken 245191 times.
✓ Branch 1 taken 419273 times.
✓ Branch 2 taken 30184 times.
✓ Branch 3 taken 215007 times.
664464 if (pc && pc->pict_type == AV_PICTURE_TYPE_B
1050
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 30100 times.
30184 && !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 664464 delay = sti->avctx->has_b_frames;
1056 664464 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 50988 times.
✓ Branch 1 taken 613476 times.
✓ Branch 2 taken 46240 times.
✓ Branch 3 taken 4748 times.
664464 if (delay &&
1061
2/2
✓ Branch 0 taken 16056 times.
✓ Branch 1 taken 30184 times.
46240 pc && pc->pict_type != AV_PICTURE_TYPE_B)
1062 16056 presentation_delayed = 1;
1063
1064
4/4
✓ Branch 0 taken 365174 times.
✓ Branch 1 taken 299290 times.
✓ Branch 2 taken 184069 times.
✓ Branch 3 taken 181105 times.
664464 if (pkt->pts != AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE &&
1065
3/4
✓ Branch 0 taken 34787 times.
✓ Branch 1 taken 149282 times.
✓ Branch 2 taken 34787 times.
✗ Branch 3 not taken.
184069 st->pts_wrap_bits < 63 && pkt->dts > INT64_MIN + (1LL << st->pts_wrap_bits) &&
1066
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34787 times.
34787 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 32972 times.
✓ Branch 1 taken 631492 times.
✓ Branch 2 taken 26166 times.
✓ Branch 3 taken 6806 times.
664464 if (delay == 1 && pkt->dts == pkt->pts &&
1078
4/4
✓ Branch 0 taken 6760 times.
✓ Branch 1 taken 19406 times.
✓ Branch 2 taken 634 times.
✓ Branch 3 taken 6126 times.
26166 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 664464 duration = av_mul_q((AVRational) {pkt->duration, 1}, st->time_base);
1086
2/2
✓ Branch 0 taken 339857 times.
✓ Branch 1 taken 324607 times.
664464 if (pkt->duration <= 0) {
1087 339857 compute_frame_duration(s, &num, &den, st, pc, pkt);
1088
3/4
✓ Branch 0 taken 329552 times.
✓ Branch 1 taken 10305 times.
✓ Branch 2 taken 329552 times.
✗ Branch 3 not taken.
339857 if (den && num) {
1089 329552 duration = (AVRational) {num, den};
1090 329552 pkt->duration = av_rescale_rnd(1,
1091 329552 num * (int64_t) st->time_base.den,
1092 329552 den * (int64_t) st->time_base.num,
1093 AV_ROUND_DOWN);
1094 }
1095 }
1096
1097
6/6
✓ Branch 0 taken 651337 times.
✓ Branch 1 taken 13127 times.
✓ Branch 2 taken 457230 times.
✓ Branch 3 taken 194107 times.
✓ Branch 4 taken 90008 times.
✓ Branch 5 taken 367222 times.
664464 if (pkt->duration > 0 && (si->packet_buffer.head || fci->parse_queue.head))
1098 284115 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 245191 times.
✓ Branch 1 taken 419273 times.
✓ Branch 2 taken 948 times.
✓ Branch 3 taken 244243 times.
✓ Branch 4 taken 948 times.
✗ Branch 5 not taken.
664464 if (pc && sti->need_parsing == AVSTREAM_PARSE_TIMESTAMPS && pkt->size) {
1103 /* this will estimate bitrate based on this frame's duration and size */
1104 948 offset = av_rescale(pc->offset, pkt->duration, pkt->size);
1105
2/2
✓ Branch 0 taken 563 times.
✓ Branch 1 taken 385 times.
948 if (pkt->pts != AV_NOPTS_VALUE)
1106 563 pkt->pts += offset;
1107
2/2
✓ Branch 0 taken 121 times.
✓ Branch 1 taken 827 times.
948 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 214765 times.
✓ Branch 1 taken 449699 times.
664464 if (pkt->dts != AV_NOPTS_VALUE &&
1113
2/2
✓ Branch 0 taken 184047 times.
✓ Branch 1 taken 30718 times.
214765 pkt->pts != AV_NOPTS_VALUE &&
1114
2/2
✓ Branch 0 taken 7665 times.
✓ Branch 1 taken 176382 times.
184047 pkt->pts > pkt->dts)
1115 7665 presentation_delayed = 1;
1116
1117
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 664464 times.
664464 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 50988 times.
✓ Branch 1 taken 613476 times.
✓ Branch 2 taken 32972 times.
✓ Branch 3 taken 18016 times.
✓ Branch 4 taken 29169 times.
✓ Branch 5 taken 3803 times.
✓ Branch 6 taken 606597 times.
✓ Branch 7 taken 36048 times.
664464 if ((delay == 0 || (delay == 1 && pc)) &&
1126 onein_oneout) {
1127
2/2
✓ Branch 0 taken 6166 times.
✓ Branch 1 taken 600431 times.
606597 if (presentation_delayed) {
1128 /* DTS = decompression timestamp */
1129 /* PTS = presentation timestamp */
1130
2/2
✓ Branch 0 taken 2843 times.
✓ Branch 1 taken 3323 times.
6166 if (pkt->dts == AV_NOPTS_VALUE)
1131 2843 pkt->dts = sti->last_IP_pts;
1132 6166 update_initial_timestamps(s, pkt->stream_index, pkt->dts, pkt->pts, pkt);
1133
2/2
✓ Branch 0 taken 2506 times.
✓ Branch 1 taken 3660 times.
6166 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 5697 times.
✓ Branch 2 taken 469 times.
✗ Branch 3 not taken.
6166 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 6166 times.
✗ Branch 1 not taken.
6166 if (pkt->dts != AV_NOPTS_VALUE)
1141 6166 sti->cur_dts = av_sat_add64(pkt->dts, sti->last_IP_duration);
1142
1/2
✓ Branch 0 taken 6166 times.
✗ Branch 1 not taken.
6166 if (pkt->dts != AV_NOPTS_VALUE &&
1143
2/2
✓ Branch 0 taken 3304 times.
✓ Branch 1 taken 2862 times.
6166 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 6166 times.
✗ Branch 1 not taken.
6166 if ((uint64_t)pkt->duration <= INT32_MAX)
1151 6166 sti->last_IP_duration = pkt->duration;
1152 6166 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 253078 times.
✓ Branch 1 taken 347353 times.
600431 } else if (pkt->pts != AV_NOPTS_VALUE ||
1156
2/2
✓ Branch 0 taken 223243 times.
✓ Branch 1 taken 29835 times.
253078 pkt->dts != AV_NOPTS_VALUE ||
1157
2/2
✓ Branch 0 taken 220619 times.
✓ Branch 1 taken 2624 times.
223243 pkt->duration > 0 ) {
1158
1159 /* presentation is not delayed : PTS and DTS are the same */
1160
2/2
✓ Branch 0 taken 250454 times.
✓ Branch 1 taken 347353 times.
597807 if (pkt->pts == AV_NOPTS_VALUE)
1161 250454 pkt->pts = pkt->dts;
1162 597807 update_initial_timestamps(s, pkt->stream_index, pkt->pts,
1163 pkt->pts, pkt);
1164
2/2
✓ Branch 0 taken 220619 times.
✓ Branch 1 taken 377188 times.
597807 if (pkt->pts == AV_NOPTS_VALUE)
1165 220619 pkt->pts = sti->cur_dts;
1166 597807 pkt->dts = pkt->pts;
1167
4/4
✓ Branch 0 taken 597804 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 597795 times.
✓ Branch 3 taken 9 times.
597807 if (pkt->pts != AV_NOPTS_VALUE && duration.num >= 0)
1168 597795 sti->cur_dts = av_add_stable(st->time_base, pkt->pts, duration, 1);
1169 }
1170 }
1171
1172
3/4
✓ Branch 0 taken 615636 times.
✓ Branch 1 taken 48828 times.
✓ Branch 2 taken 615636 times.
✗ Branch 3 not taken.
664464 if (pkt->pts != AV_NOPTS_VALUE && delay <= MAX_REORDER_DELAY) {
1173 615636 sti->pts_buffer[0] = pkt->pts;
1174
4/4
✓ Branch 0 taken 26972 times.
✓ Branch 1 taken 608401 times.
✓ Branch 2 taken 19737 times.
✓ Branch 3 taken 7235 times.
635373 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 614903 times.
✓ Branch 2 taken 733 times.
615636 if (has_decode_delay_been_guessed(st))
1178 614903 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 54077 times.
✓ Branch 1 taken 610387 times.
664464 if (!onein_oneout)
1182 // This should happen on the first packet
1183 54077 update_initial_timestamps(s, pkt->stream_index, pkt->dts, pkt->pts, pkt);
1184
2/2
✓ Branch 0 taken 10588 times.
✓ Branch 1 taken 653876 times.
664464 if (pkt->dts > sti->cur_dts)
1185 10588 sti->cur_dts = pkt->dts;
1186
1187
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 664464 times.
664464 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 664299 times.
✓ Branch 1 taken 165 times.
✓ Branch 3 taken 448675 times.
✓ Branch 4 taken 215624 times.
664464 if (st->codecpar->codec_type == AVMEDIA_TYPE_DATA || ff_is_intra_only(st->codecpar->codec_id))
1193 448840 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 728625 static int parse_packet(AVFormatContext *s, AVPacket *pkt,
1203 int stream_index, int flush)
1204 {
1205 728625 FormatContextInternal *const fci = ff_fc_internal(s);
1206 728625 AVStream *st = s->streams[stream_index];
1207 728625 FFStream *const sti = ffstream(st);
1208 728625 AVPacket *out_pkt = sti->parse_pkt;
1209 728625 const AVPacketSideData *sd = NULL;
1210 728625 const uint8_t *data = pkt->data;
1211 728625 uint8_t *extradata = sti->avctx->extradata;
1212 728625 int extradata_size = sti->avctx->extradata_size;
1213 728625 int size = pkt->size;
1214 728625 int ret = 0, got_output = flush, pkt_side_data_consumed = 0;
1215
1216
5/6
✓ Branch 0 taken 2568 times.
✓ Branch 1 taken 726057 times.
✓ Branch 2 taken 164 times.
✓ Branch 3 taken 2404 times.
✓ Branch 4 taken 164 times.
✗ Branch 5 not taken.
728625 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 29551 times.
✓ Branch 1 taken 699074 times.
728625 if (pkt->side_data_elems)
1230 29551 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 728619 times.
728625 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 896199 times.
✓ Branch 1 taken 732345 times.
✓ Branch 2 taken 6124 times.
✓ Branch 3 taken 726221 times.
✓ Branch 4 taken 3720 times.
✓ Branch 5 taken 2404 times.
1628544 while (size > 0 || (flush && got_output)) {
1240 899919 int64_t next_pts = pkt->pts;
1241 899919 int64_t next_dts = pkt->dts;
1242 int len;
1243
1244 899919 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 899919 pkt->pts = pkt->dts = AV_NOPTS_VALUE;
1249 899919 pkt->pos = -1;
1250 /* increment read pointer */
1251 av_assert1(data || !len);
1252
2/2
✓ Branch 0 taken 883559 times.
✓ Branch 1 taken 16360 times.
899919 data = len ? data + len : data;
1253 899919 size -= len;
1254
1255 899919 got_output = !!out_pkt->size;
1256
1257
4/4
✓ Branch 0 taken 40534 times.
✓ Branch 1 taken 859385 times.
✓ Branch 2 taken 18429 times.
✓ Branch 3 taken 22105 times.
899919 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 12618 times.
✓ Branch 1 taken 5811 times.
18429 if (!pkt_side_data_consumed) {
1261 12618 pkt_side_data_consumed = 1;
1262 12618 out_pkt->side_data = pkt->side_data;
1263 12618 out_pkt->side_data_elems = pkt->side_data_elems;
1264
2/2
✓ Branch 0 taken 5811 times.
✓ Branch 1 taken 5811 times.
11622 } else for (int i = 0; i < pkt->side_data_elems; i++) {
1265 5811 const AVPacketSideData *const src_sd = &pkt->side_data[i];
1266 5811 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 5811 times.
5811 if (!dst_data) {
1268 ✗ ret = AVERROR(ENOMEM);
1269 ✗ goto fail;
1270 }
1271 5811 memcpy(dst_data, src_sd->data, src_sd->size);
1272 }
1273 }
1274
1275
2/2
✓ Branch 0 taken 654892 times.
✓ Branch 1 taken 245027 times.
899919 if (!out_pkt->size)
1276 654892 continue;
1277
1278
4/4
✓ Branch 0 taken 243711 times.
✓ Branch 1 taken 1316 times.
✓ Branch 2 taken 72638 times.
✓ Branch 3 taken 171073 times.
245027 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 72638 out_pkt->buf = av_buffer_ref(pkt->buf);
1284
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 72638 times.
72638 if (!out_pkt->buf) {
1285 ✗ ret = AVERROR(ENOMEM);
1286 ✗ goto fail;
1287 }
1288 } else {
1289 172389 ret = av_packet_make_refcounted(out_pkt);
1290
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 172389 times.
172389 if (ret < 0)
1291 ✗ goto fail;
1292 }
1293
1294 /* set the duration */
1295
2/2
✓ Branch 0 taken 52644 times.
✓ Branch 1 taken 192383 times.
245027 out_pkt->duration = (sti->parser->flags & PARSER_FLAG_COMPLETE_FRAMES) ? pkt->duration : 0;
1296
2/2
✓ Branch 0 taken 160859 times.
✓ Branch 1 taken 84168 times.
245027 if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
1297
6/6
✓ Branch 0 taken 160695 times.
✓ Branch 1 taken 164 times.
✓ Branch 2 taken 160432 times.
✓ Branch 3 taken 263 times.
✓ Branch 4 taken 147919 times.
✓ Branch 5 taken 12513 times.
160859 if (sti->avctx->sample_rate > 0 && !out_pkt->duration && sti->parser->duration > 0) {
1298 147919 out_pkt->duration =
1299 147919 av_rescale_q_rnd(sti->parser->duration,
1300 147919 (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 82346 times.
84168 } 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 245027 out_pkt->stream_index = st->index;
1312 245027 out_pkt->pts = sti->parser->pts;
1313 245027 out_pkt->dts = sti->parser->dts;
1314 245027 out_pkt->pos = sti->parser->pos;
1315 245027 out_pkt->flags |= pkt->flags & (AV_PKT_FLAG_DISCARD | AV_PKT_FLAG_CORRUPT);
1316
1317
2/2
✓ Branch 0 taken 165065 times.
✓ Branch 1 taken 79962 times.
245027 if (sti->need_parsing == AVSTREAM_PARSE_FULL_RAW)
1318 165065 out_pkt->pos = sti->parser->frame_offset;
1319
1320
2/2
✓ Branch 0 taken 232932 times.
✓ Branch 1 taken 12095 times.
245027 if (sti->parser->key_frame == 1 ||
1321
2/2
✓ Branch 0 taken 147069 times.
✓ Branch 1 taken 85863 times.
232932 (sti->parser->key_frame == -1 &&
1322
2/2
✓ Branch 0 taken 134554 times.
✓ Branch 1 taken 12515 times.
147069 sti->parser->pict_type == AV_PICTURE_TYPE_I))
1323 146649 out_pkt->flags |= AV_PKT_FLAG_KEY;
1324
1325
6/6
✓ Branch 0 taken 147069 times.
✓ Branch 1 taken 97958 times.
✓ Branch 2 taken 325 times.
✓ Branch 3 taken 146744 times.
✓ Branch 4 taken 294 times.
✓ Branch 5 taken 31 times.
245027 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 245027 compute_pkt_fields(s, st, sti->parser, out_pkt, next_dts, next_pts);
1329
1330 245027 ret = ff_packet_list_put(&fci->parse_queue,
1331 out_pkt, NULL, 0);
1332
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 245027 times.
245027 if (ret < 0)
1333 ✗ goto fail;
1334 }
1335
1336 /* end of the stream => close and free the parser */
1337
2/2
✓ Branch 0 taken 726221 times.
✓ Branch 1 taken 2404 times.
728625 if (flush) {
1338 2404 av_parser_close(sti->parser);
1339 2404 sti->parser = NULL;
1340 }
1341
1342 726221 fail:
1343
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 728619 times.
728625 if (sd) {
1344 6 sti->avctx->extradata = extradata;
1345 6 sti->avctx->extradata_size = extradata_size;
1346 }
1347
2/2
✓ Branch 0 taken 12618 times.
✓ Branch 1 taken 716007 times.
728625 if (pkt_side_data_consumed) {
1348 12618 pkt->side_data = NULL;
1349 12618 pkt->side_data_elems = 0;
1350 }
1351
1352
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 728625 times.
728625 if (ret < 0)
1353 ✗ av_packet_unref(out_pkt);
1354 728625 av_packet_unref(pkt);
1355 728625 return ret;
1356 }
1357
1358 52549 static int64_t ts_to_samples(AVStream *st, int64_t ts)
1359 {
1360 52549 return av_rescale(ts, st->time_base.num * st->codecpar->sample_rate, st->time_base.den);
1361 }
1362
1363 9524 static int codec_close(FFStream *sti)
1364 {
1365 9524 AVCodecContext *avctx_new = NULL;
1366 9524 AVCodecParameters *par_tmp = NULL;
1367 9524 const AVCodec *new_codec = NULL;
1368 int ret;
1369
1370 9524 new_codec =
1371 9524 (sti->avctx->codec_id != sti->pub.codecpar->codec_id) ?
1372
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 9512 times.
9524 avcodec_find_decoder(sti->pub.codecpar->codec_id) :
1373 9512 sti->avctx->codec;
1374
1375 9524 avctx_new = avcodec_alloc_context3(new_codec);
1376
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9524 times.
9524 if (!avctx_new) {
1377 ✗ ret = AVERROR(ENOMEM);
1378 ✗ goto fail;
1379 }
1380
1381 9524 par_tmp = avcodec_parameters_alloc();
1382
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9524 times.
9524 if (!par_tmp) {
1383 ✗ ret = AVERROR(ENOMEM);
1384 ✗ goto fail;
1385 }
1386
1387 9524 ret = avcodec_parameters_from_context(par_tmp, sti->avctx);
1388
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9524 times.
9524 if (ret < 0)
1389 ✗ goto fail;
1390
1391 9524 ret = avcodec_parameters_to_context(avctx_new, par_tmp);
1392
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9524 times.
9524 if (ret < 0)
1393 ✗ goto fail;
1394
1395 9524 avctx_new->pkt_timebase = sti->avctx->pkt_timebase;
1396
1397 9524 avcodec_free_context(&sti->avctx);
1398 9524 sti->avctx = avctx_new;
1399
1400 9524 avctx_new = NULL;
1401 9524 ret = 0;
1402
1403 9524 fail:
1404 9524 avcodec_free_context(&avctx_new);
1405 9524 avcodec_parameters_free(&par_tmp);
1406
1407 9524 return ret;
1408 }
1409
1410 static int extract_extradata(FFFormatContext *si, AVStream *st, const AVPacket *pkt);
1411
1412 669632 static int read_frame_internal(AVFormatContext *s, AVPacket *pkt)
1413 {
1414 669632 FormatContextInternal *const fci = ff_fc_internal(s);
1415 669632 FFFormatContext *const si = &fci->fc;
1416 669632 int ret, got_packet = 0;
1417 669632 AVDictionary *metadata = NULL;
1418
1419
4/4
✓ Branch 0 taken 1395986 times.
✓ Branch 1 taken 419283 times.
✓ Branch 2 taken 1152959 times.
✓ Branch 3 taken 243027 times.
1815269 while (!got_packet && !fci->parse_queue.head) {
1420 AVStream *st;
1421 FFStream *sti;
1422
1423 /* read next packet */
1424 1152959 ret = ff_read_packet(s, pkt);
1425
2/2
✓ Branch 0 taken 7322 times.
✓ Branch 1 taken 1145637 times.
1152959 if (ret < 0) {
1426
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7321 times.
7322 if (ret == AVERROR(EAGAIN))
1427 1 return ret;
1428 /* flush the parsers */
1429
2/2
✓ Branch 0 taken 8827 times.
✓ Branch 1 taken 7321 times.
16148 for (unsigned i = 0; i < s->nb_streams; i++) {
1430 8827 AVStream *const st = s->streams[i];
1431 8827 FFStream *const sti = ffstream(st);
1432
4/4
✓ Branch 0 taken 3476 times.
✓ Branch 1 taken 5351 times.
✓ Branch 2 taken 2404 times.
✓ Branch 3 taken 1072 times.
8827 if (sti->parser && sti->need_parsing)
1433 2404 parse_packet(s, pkt, st->index, 1);
1434 }
1435 /* all remaining packets are now in parse_queue =>
1436 * really terminate parsing */
1437 7321 break;
1438 }
1439 1145637 ret = 0;
1440 1145637 st = s->streams[pkt->stream_index];
1441 1145637 sti = ffstream(st);
1442
1443 1145637 st->event_flags |= AVSTREAM_EVENT_FLAG_NEW_PACKETS;
1444
1445 1145637 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 1145615 times.
1145637 if (new_extradata)
1448 22 sti->need_context_update = 1;
1449
1450 /* update context if required */
1451
2/2
✓ Branch 0 taken 218 times.
✓ Branch 1 taken 1145419 times.
1145637 if (sti->need_context_update) {
1452
2/2
✓ Branch 1 taken 20 times.
✓ Branch 2 taken 198 times.
218 if (avcodec_is_open(sti->avctx)) {
1453 20 av_log(s, AV_LOG_DEBUG, "Demuxer context update while decoder is open, closing and trying to re-open\n");
1454 20 ret = codec_close(sti);
1455 20 sti->info->found_decoder = 0;
1456
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 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 41 times.
✓ Branch 1 taken 177 times.
218 if (sti->parser &&
1462
4/4
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 19 times.
✓ Branch 2 taken 15 times.
✓ Branch 3 taken 7 times.
41 (sti->avctx->codec_id != st->codecpar->codec_id || new_extradata)) {
1463 34 av_parser_close(sti->parser);
1464 34 sti->parser = NULL;
1465 }
1466
1467 218 ret = avcodec_parameters_to_context(sti->avctx, st->codecpar);
1468
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 218 times.
218 if (ret < 0) {
1469 ✗ av_packet_unref(pkt);
1470 ✗ return ret;
1471 }
1472
1473
2/2
✓ Branch 0 taken 165 times.
✓ Branch 1 taken 53 times.
218 if (!sti->avctx->extradata) {
1474 165 sti->extract_extradata.inited = 0;
1475
1476 165 ret = extract_extradata(si, st, pkt);
1477
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 165 times.
165 if (ret < 0) {
1478 ✗ av_packet_unref(pkt);
1479 ✗ return ret;
1480 }
1481 }
1482
1483 218 sti->codec_desc = avcodec_descriptor_get(sti->avctx->codec_id);
1484
1485 218 sti->need_context_update = 0;
1486 }
1487
1488
2/2
✓ Branch 0 taken 362935 times.
✓ Branch 1 taken 782702 times.
1145637 if (pkt->pts != AV_NOPTS_VALUE &&
1489
2/2
✓ Branch 0 taken 185196 times.
✓ Branch 1 taken 177739 times.
362935 pkt->dts != AV_NOPTS_VALUE &&
1490
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 185196 times.
185196 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 1145637 times.
1145637 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 727617 times.
✓ Branch 1 taken 418020 times.
✓ Branch 2 taken 3089 times.
✓ Branch 3 taken 724528 times.
✓ Branch 4 taken 3084 times.
✓ Branch 5 taken 5 times.
1145637 if (sti->need_parsing && !sti->parser && !(s->flags & AVFMT_FLAG_NOPARSE)) {
1507 3084 sti->parser = av_parser_init(st->codecpar->codec_id);
1508
2/2
✓ Branch 0 taken 1258 times.
✓ Branch 1 taken 1826 times.
3084 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 1060 times.
1826 } 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 1060 times.
1060 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 529 times.
1060 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 726359 times.
✓ Branch 1 taken 419278 times.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 726354 times.
1145637 if (!sti->need_parsing || !sti->parser) {
1523 /* no parsing needed: we just output the packet as is */
1524 419283 compute_pkt_fields(s, st, NULL, pkt, AV_NOPTS_VALUE, AV_NOPTS_VALUE);
1525
2/2
✓ Branch 0 taken 102114 times.
✓ Branch 1 taken 317169 times.
419283 if ((s->iformat->flags & AVFMT_GENERIC_INDEX) &&
1526
4/4
✓ Branch 0 taken 101235 times.
✓ Branch 1 taken 879 times.
✓ Branch 2 taken 100960 times.
✓ Branch 3 taken 275 times.
102114 (pkt->flags & AV_PKT_FLAG_KEY) && pkt->dts != AV_NOPTS_VALUE) {
1527 100960 ff_reduce_index(s, st->index);
1528 100960 av_add_index_entry(st, pkt->pos, pkt->dts,
1529 0, 0, AVINDEX_KEYFRAME);
1530 }
1531 419283 got_packet = 1;
1532
2/2
✓ Branch 0 taken 726221 times.
✓ Branch 1 taken 133 times.
726354 } else if (st->discard < AVDISCARD_ALL) {
1533
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 726221 times.
726221 if ((ret = parse_packet(s, pkt, pkt->stream_index, 0)) < 0)
1534 ✗ return ret;
1535 726221 st->codecpar->sample_rate = sti->avctx->sample_rate;
1536 726221 st->codecpar->bit_rate = sti->avctx->bit_rate;
1537 726221 ret = av_channel_layout_copy(&st->codecpar->ch_layout, &sti->avctx->ch_layout);
1538
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 726221 times.
726221 if (ret < 0)
1539 ✗ return ret;
1540 726221 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 392979 times.
✓ Branch 1 taken 752658 times.
1145637 if (pkt->flags & AV_PKT_FLAG_KEY)
1546 392979 sti->skip_to_keyframe = 0;
1547
2/2
✓ Branch 0 taken 127 times.
✓ Branch 1 taken 1145510 times.
1145637 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 250348 times.
✓ Branch 1 taken 419283 times.
✓ Branch 2 taken 244070 times.
✓ Branch 3 taken 6278 times.
669631 if (!got_packet && fci->parse_queue.head)
1554 244070 ret = ff_packet_list_get(&fci->parse_queue, pkt);
1555
1556
2/2
✓ Branch 0 taken 663353 times.
✓ Branch 1 taken 6278 times.
669631 if (ret >= 0) {
1557 663353 AVStream *const st = s->streams[pkt->stream_index];
1558 663353 FFStream *const sti = ffstream(st);
1559 663353 int discard_padding = 0;
1560
3/4
✓ Branch 0 taken 17504 times.
✓ Branch 1 taken 645849 times.
✓ Branch 2 taken 17504 times.
✗ Branch 3 not taken.
663353 if (sti->first_discard_sample && pkt->pts != AV_NOPTS_VALUE) {
1561
2/2
✓ Branch 1 taken 7466 times.
✓ Branch 2 taken 10038 times.
17504 int64_t pts = pkt->pts - (is_relative(pkt->pts) ? RELATIVE_TS_BASE : 0);
1562 17504 int64_t sample = ts_to_samples(st, pts);
1563
2/2
✓ Branch 1 taken 17 times.
✓ Branch 2 taken 17487 times.
17504 int64_t duration = FFMAX(st->codecpar->frame_size, ts_to_samples(st, pkt->duration));
1564 17504 int64_t end_sample = sample + duration;
1565
3/4
✓ Branch 0 taken 17504 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 338 times.
✓ Branch 3 taken 17166 times.
17504 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 2883 times.
✓ Branch 1 taken 660470 times.
663353 if (sti->seek_discard) {
1570 2883 const AVCodecParameters *par = st->codecpar;
1571
1572
3/4
✓ Branch 0 taken 1383 times.
✓ Branch 1 taken 1500 times.
✓ Branch 2 taken 1383 times.
✗ Branch 3 not taken.
2883 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 2883 sti->seek_discard = 0;
1594 }
1595
6/6
✓ Branch 0 taken 10184 times.
✓ Branch 1 taken 653169 times.
✓ Branch 2 taken 10166 times.
✓ Branch 3 taken 18 times.
✓ Branch 4 taken 140 times.
✓ Branch 5 taken 10026 times.
663353 if (sti->start_skip_samples && (pkt->pts == 0 || pkt->pts == RELATIVE_TS_BASE))
1596 158 sti->skip_samples = sti->start_skip_samples;
1597 663353 sti->skip_samples = FFMAX(0, sti->skip_samples);
1598
4/4
✓ Branch 0 taken 662866 times.
✓ Branch 1 taken 487 times.
✓ Branch 2 taken 134 times.
✓ Branch 3 taken 662732 times.
663353 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 8468 times.
✓ Branch 1 taken 661163 times.
669631 if (!fci->metafree) {
1618 8468 int metaret = av_opt_get_dict_val(s, "metadata", AV_OPT_SEARCH_CHILDREN, &metadata);
1619
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8468 times.
8468 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 8468 fci->metafree = metaret == AVERROR_OPTION_NOT_FOUND;
1626 }
1627
1628
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 669631 times.
669631 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 6168 times.
✓ Branch 1 taken 663463 times.
✓ Branch 2 taken 5914 times.
✓ Branch 3 taken 254 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 5914 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
669631 if (ret == AVERROR_EOF && s->pb && s->pb->error < 0 && s->pb->error != AVERROR(EAGAIN))
1640 ✗ ret = s->pb->error;
1641
1642 669631 return ret;
1643 }
1644
1645 563517 int av_read_frame(AVFormatContext *s, AVPacket *pkt)
1646 {
1647 563517 FFFormatContext *const si = ffformatcontext(s);
1648 563517 const int genpts = s->flags & AVFMT_FLAG_GENPTS;
1649 563517 int eof = 0;
1650 int ret;
1651 AVStream *st;
1652
1653
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 563517 times.
563517 if (!genpts) {
1654 1127034 ret = si->packet_buffer.head
1655 104302 ? ff_packet_list_get(&si->packet_buffer, pkt)
1656
2/2
✓ Branch 0 taken 104302 times.
✓ Branch 1 taken 459215 times.
563517 : read_frame_internal(s, pkt);
1657
2/2
✓ Branch 0 taken 4823 times.
✓ Branch 1 taken 558694 times.
563517 if (ret < 0)
1658 4823 return ret;
1659 558694 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 558694 return_packet:
1726 558694 st = s->streams[pkt->stream_index];
1727
4/4
✓ Branch 0 taken 247532 times.
✓ Branch 1 taken 311162 times.
✓ Branch 2 taken 172305 times.
✓ Branch 3 taken 75227 times.
558694 if ((s->iformat->flags & AVFMT_GENERIC_INDEX) && pkt->flags & AV_PKT_FLAG_KEY) {
1728 172305 ff_reduce_index(s, st->index);
1729 172305 av_add_index_entry(st, pkt->pos, pkt->dts, 0, 0, AVINDEX_KEYFRAME);
1730 }
1731
1732
2/2
✓ Branch 1 taken 161753 times.
✓ Branch 2 taken 396941 times.
558694 if (is_relative(pkt->dts))
1733 161753 pkt->dts -= RELATIVE_TS_BASE;
1734
2/2
✓ Branch 1 taken 160060 times.
✓ Branch 2 taken 398634 times.
558694 if (is_relative(pkt->pts))
1735 160060 pkt->pts -= RELATIVE_TS_BASE;
1736
1737 558694 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 8358 static int has_duration(AVFormatContext *ic)
1746 {
1747
2/2
✓ Branch 0 taken 8951 times.
✓ Branch 1 taken 2790 times.
11741 for (unsigned i = 0; i < ic->nb_streams; i++) {
1748 8951 const AVStream *const st = ic->streams[i];
1749
2/2
✓ Branch 0 taken 5568 times.
✓ Branch 1 taken 3383 times.
8951 if (st->duration != AV_NOPTS_VALUE)
1750 5568 return 1;
1751 }
1752
2/2
✓ Branch 0 taken 543 times.
✓ Branch 1 taken 2247 times.
2790 if (ic->duration != AV_NOPTS_VALUE)
1753 543 return 1;
1754 2247 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 14677 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 14677 start_time = INT64_MAX;
1768 14677 start_time_text = INT64_MAX;
1769 14677 end_time = INT64_MIN;
1770 14677 end_time_text = INT64_MIN;
1771 14677 duration = INT64_MIN;
1772 14677 duration_text = INT64_MIN;
1773
1774
2/2
✓ Branch 0 taken 16741 times.
✓ Branch 1 taken 14677 times.
31418 for (unsigned i = 0; i < ic->nb_streams; i++) {
1775 16741 AVStream *const st = ic->streams[i];
1776
2/2
✓ Branch 0 taken 16594 times.
✓ Branch 1 taken 147 times.
33335 int is_text = st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE ||
1777
2/2
✓ Branch 0 taken 187 times.
✓ Branch 1 taken 16407 times.
16594 st->codecpar->codec_type == AVMEDIA_TYPE_DATA;
1778
1779
3/4
✓ Branch 0 taken 13667 times.
✓ Branch 1 taken 3074 times.
✓ Branch 2 taken 13667 times.
✗ Branch 3 not taken.
16741 if (st->start_time != AV_NOPTS_VALUE && st->time_base.den) {
1780 13667 start_time1 = av_rescale_q(st->start_time, st->time_base,
1781 13667 AV_TIME_BASE_Q);
1782
2/2
✓ Branch 0 taken 194 times.
✓ Branch 1 taken 13473 times.
13667 if (is_text)
1783 194 start_time_text = FFMIN(start_time_text, start_time1);
1784 else
1785 13473 start_time = FFMIN(start_time, start_time1);
1786 13667 end_time1 = av_rescale_q_rnd(st->duration, st->time_base,
1787 13667 AV_TIME_BASE_Q,
1788 AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
1789
5/6
✓ Branch 0 taken 11742 times.
✓ Branch 1 taken 1925 times.
✓ Branch 2 taken 11713 times.
✓ Branch 3 taken 29 times.
✓ Branch 4 taken 11742 times.
✗ Branch 5 not taken.
13667 if (end_time1 != AV_NOPTS_VALUE && (end_time1 > 0 ? start_time1 <= INT64_MAX - end_time1 : start_time1 >= INT64_MIN - end_time1)) {
1790 11742 end_time1 += start_time1;
1791
2/2
✓ Branch 0 taken 173 times.
✓ Branch 1 taken 11569 times.
11742 if (is_text)
1792 173 end_time_text = FFMAX(end_time_text, end_time1);
1793 else
1794 11569 end_time = FFMAX(end_time, end_time1);
1795 }
1796
2/2
✓ Branch 1 taken 531 times.
✓ Branch 2 taken 13667 times.
14198 for (AVProgram *p = NULL; (p = av_find_program_from_stream(ic, p, i)); ) {
1797
4/4
✓ Branch 0 taken 427 times.
✓ Branch 1 taken 104 times.
✓ Branch 2 taken 28 times.
✓ Branch 3 taken 399 times.
531 if (p->start_time == AV_NOPTS_VALUE || p->start_time > start_time1)
1798 132 p->start_time = start_time1;
1799
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 447 times.
531 if (p->end_time < end_time1)
1800 84 p->end_time = end_time1;
1801 }
1802 }
1803
2/2
✓ Branch 0 taken 13455 times.
✓ Branch 1 taken 3286 times.
16741 if (st->duration != AV_NOPTS_VALUE) {
1804 13455 duration1 = av_rescale_q(st->duration, st->time_base,
1805 13455 AV_TIME_BASE_Q);
1806
2/2
✓ Branch 0 taken 203 times.
✓ Branch 1 taken 13252 times.
13455 if (is_text)
1807 203 duration_text = FFMAX(duration_text, duration1);
1808 else
1809 13252 duration = FFMAX(duration, duration1);
1810 }
1811 }
1812
3/6
✓ Branch 0 taken 11944 times.
✓ Branch 1 taken 2733 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 11944 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
14677 if (start_time == INT64_MAX || (start_time > start_time_text && start_time - (uint64_t)start_time_text < AV_TIME_BASE))
1813 2733 start_time = start_time_text;
1814
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11944 times.
11944 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 10755 times.
✓ Branch 1 taken 3922 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 10749 times.
✓ Branch 4 taken 4 times.
✓ Branch 5 taken 2 times.
14677 if (end_time == INT64_MIN || (end_time < end_time_text && end_time_text - (uint64_t)end_time < AV_TIME_BASE))
1818 3926 end_time = end_time_text;
1819
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 10749 times.
10751 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 12345 times.
✓ Branch 1 taken 2332 times.
✓ Branch 2 taken 15 times.
✓ Branch 3 taken 12330 times.
✓ Branch 4 taken 13 times.
✓ Branch 5 taken 2 times.
14677 if (duration == INT64_MIN || (duration < duration_text && (uint64_t)duration_text - duration < AV_TIME_BASE))
1823 2345 duration = duration_text;
1824
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 12330 times.
12332 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 11967 times.
✓ Branch 1 taken 2710 times.
14677 if (start_time != INT64_MAX) {
1828 11967 ic->start_time = start_time;
1829
2/2
✓ Branch 0 taken 10772 times.
✓ Branch 1 taken 1195 times.
11967 if (end_time != INT64_MIN) {
1830
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10772 times.
10772 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 10772 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10772 times.
✗ Branch 3 not taken.
10772 } else if (end_time >= start_time && end_time - (uint64_t)start_time <= INT64_MAX) {
1840 10772 duration = FFMAX(duration, end_time - start_time);
1841 }
1842 }
1843 }
1844
6/6
✓ Branch 0 taken 12375 times.
✓ Branch 1 taken 2302 times.
✓ Branch 2 taken 12339 times.
✓ Branch 3 taken 36 times.
✓ Branch 4 taken 6610 times.
✓ Branch 5 taken 5729 times.
14677 if (duration != INT64_MIN && duration > 0 && ic->duration == AV_NOPTS_VALUE) {
1845 6610 ic->duration = duration;
1846 }
1847
6/6
✓ Branch 0 taken 8345 times.
✓ Branch 1 taken 6332 times.
✓ Branch 3 taken 8340 times.
✓ Branch 4 taken 5 times.
✓ Branch 5 taken 7106 times.
✓ Branch 6 taken 1234 times.
14677 if (ic->pb && (filesize = avio_size(ic->pb)) > 0 && ic->duration > 0) {
1848 /* compute the bitrate */
1849 7106 double bitrate = (double) filesize * 8.0 * AV_TIME_BASE /
1850 7106 (double) ic->duration;
1851
2/4
✓ Branch 0 taken 7106 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7106 times.
✗ Branch 3 not taken.
7106 if (bitrate >= 0 && bitrate <= INT64_MAX)
1852 7106 ic->bit_rate = bitrate;
1853 }
1854 14677 }
1855
1856 6215 static void fill_all_stream_timings(AVFormatContext *ic)
1857 {
1858 6215 update_stream_timings(ic);
1859
2/2
✓ Branch 0 taken 7087 times.
✓ Branch 1 taken 6215 times.
13302 for (unsigned i = 0; i < ic->nb_streams; i++) {
1860 7087 AVStream *const st = ic->streams[i];
1861
1862
2/2
✓ Branch 0 taken 879 times.
✓ Branch 1 taken 6208 times.
7087 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 6215 }
1872
1873 2247 static void estimate_timings_from_bit_rate(AVFormatContext *ic)
1874 {
1875 2247 FFFormatContext *const si = ffformatcontext(ic);
1876 2247 int show_warning = 0;
1877
1878 /* if bit_rate is already set, we believe it */
1879
2/2
✓ Branch 0 taken 2212 times.
✓ Branch 1 taken 35 times.
2247 if (ic->bit_rate <= 0) {
1880 2212 int64_t bit_rate = 0;
1881
2/2
✓ Branch 0 taken 2508 times.
✓ Branch 1 taken 1526 times.
4034 for (unsigned i = 0; i < ic->nb_streams; i++) {
1882 2508 const AVStream *const st = ic->streams[i];
1883 2508 const FFStream *const sti = cffstream(st);
1884
4/4
✓ Branch 0 taken 1638 times.
✓ Branch 1 taken 870 times.
✓ Branch 2 taken 221 times.
✓ Branch 3 taken 1417 times.
2508 if (st->codecpar->bit_rate <= 0 && sti->avctx->bit_rate > 0)
1885 221 st->codecpar->bit_rate = sti->avctx->bit_rate;
1886
2/2
✓ Branch 0 taken 1091 times.
✓ Branch 1 taken 1417 times.
2508 if (st->codecpar->bit_rate > 0) {
1887
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1091 times.
1091 if (INT64_MAX - st->codecpar->bit_rate < bit_rate) {
1888 ✗ bit_rate = 0;
1889 ✗ break;
1890 }
1891 1091 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 2212 ic->bit_rate = bit_rate;
1900 }
1901
1902 /* if duration is already set, we believe it */
1903
1/2
✓ Branch 0 taken 2247 times.
✗ Branch 1 not taken.
2247 if (ic->duration == AV_NOPTS_VALUE &&
1904
2/2
✓ Branch 0 taken 1021 times.
✓ Branch 1 taken 1226 times.
2247 ic->bit_rate != 0) {
1905
2/2
✓ Branch 0 taken 979 times.
✓ Branch 1 taken 42 times.
1021 int64_t filesize = ic->pb ? avio_size(ic->pb) : 0;
1906
2/2
✓ Branch 0 taken 956 times.
✓ Branch 1 taken 65 times.
1021 if (filesize > si->data_offset) {
1907 956 filesize -= si->data_offset;
1908
2/2
✓ Branch 0 taken 1070 times.
✓ Branch 1 taken 956 times.
2026 for (unsigned i = 0; i < ic->nb_streams; i++) {
1909 1070 AVStream *const st = ic->streams[i];
1910
1911
1/2
✓ Branch 0 taken 1070 times.
✗ Branch 1 not taken.
1070 if ( st->time_base.num <= INT64_MAX / ic->bit_rate
1912
1/2
✓ Branch 0 taken 1070 times.
✗ Branch 1 not taken.
1070 && st->duration == AV_NOPTS_VALUE) {
1913 1070 st->duration = av_rescale(filesize, 8LL * st->time_base.den,
1914 1070 ic->bit_rate *
1915 1070 (int64_t) st->time_base.num);
1916 1070 show_warning = 1;
1917 }
1918 }
1919 }
1920 }
1921
2/2
✓ Branch 0 taken 956 times.
✓ Branch 1 taken 1291 times.
2247 if (show_warning)
1922 956 av_log(ic, AV_LOG_WARNING,
1923 "Estimating duration from bitrate, this may be inaccurate\n");
1924 2247 }
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 8462 static const char *duration_estimate_name(enum AVDurationEstimationMethod method)
2080 {
2081 8462 return duration_name[method];
2082 }
2083
2084 8462 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 3237 times.
✓ Branch 1 taken 5225 times.
8462 if (ic->iformat->flags & AVFMT_NOFILE) {
2090 3237 file_size = 0;
2091 } else {
2092 5225 file_size = avio_size(ic->pb);
2093 5225 file_size = FFMAX(0, file_size);
2094 }
2095
2096
2/2
✓ Branch 0 taken 8434 times.
✓ Branch 1 taken 28 times.
8462 if ((!strcmp(ic->iformat->name, "mpeg") ||
2097
4/4
✓ Branch 0 taken 80 times.
✓ Branch 1 taken 8354 times.
✓ Branch 2 taken 104 times.
✓ Branch 3 taken 4 times.
8462 !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 6111 times.
✓ Branch 2 taken 2247 times.
8358 } else if (has_duration(ic)) {
2103 /* at least one component has timings - we use them for all
2104 * the components */
2105 6111 fill_all_stream_timings(ic);
2106 /* nut demuxer estimate the duration from PTS */
2107
2/2
✓ Branch 0 taken 69 times.
✓ Branch 1 taken 6042 times.
6111 if (!strcmp(ic->iformat->name, "nut"))
2108 69 ic->duration_estimation_method = AVFMT_DURATION_FROM_PTS;
2109 else
2110 6042 ic->duration_estimation_method = AVFMT_DURATION_FROM_STREAM;
2111 } else {
2112 /* less precise: use bitrate info */
2113 2247 estimate_timings_from_bit_rate(ic);
2114 2247 ic->duration_estimation_method = AVFMT_DURATION_FROM_BITRATE;
2115 }
2116 8462 update_stream_timings(ic);
2117
2118
2/2
✓ Branch 0 taken 9654 times.
✓ Branch 1 taken 8462 times.
18116 for (unsigned i = 0; i < ic->nb_streams; i++) {
2119 9654 AVStream *const st = ic->streams[i];
2120
1/2
✓ Branch 0 taken 9654 times.
✗ Branch 1 not taken.
9654 if (st->time_base.den)
2121 9654 av_log(ic, AV_LOG_TRACE, "stream %u: start_time: %s duration: %s\n", i,
2122 9654 av_ts2timestr(st->start_time, &st->time_base),
2123 9654 av_ts2timestr(st->duration, &st->time_base));
2124 }
2125 8462 av_log(ic, AV_LOG_TRACE,
2126 "format: start_time: %s duration: %s (estimate from %s) bitrate=%"PRId64" kb/s\n",
2127 8462 av_ts2timestr(ic->start_time, &AV_TIME_BASE_Q),
2128 8462 av_ts2timestr(ic->duration, &AV_TIME_BASE_Q),
2129 duration_estimate_name(ic->duration_estimation_method),
2130 8462 (int64_t)ic->bit_rate / 1000);
2131 8462 }
2132
2133 116010 static int determinable_frame_size(const AVCodecContext *avctx)
2134 {
2135
2/2
✓ Branch 0 taken 2054 times.
✓ Branch 1 taken 113956 times.
116010 switch(avctx->codec_id) {
2136 2054 case AV_CODEC_ID_MP1:
2137 case AV_CODEC_ID_MP2:
2138 case AV_CODEC_ID_MP3:
2139 case AV_CODEC_ID_CODEC2:
2140 2054 return 1;
2141 }
2142
2143 113956 return 0;
2144 }
2145
2146 531963 static int has_codec_parameters(const AVStream *st, const char **errmsg_ptr)
2147 {
2148 531963 const FFStream *const sti = cffstream(st);
2149 531963 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 531201 times.
531963 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 212344 times.
✓ Branch 1 taken 317398 times.
✓ Branch 2 taken 679 times.
✓ Branch 3 taken 1029 times.
✗ Branch 4 not taken.
531450 switch (avctx->codec_type) {
2161 212344 case AVMEDIA_TYPE_AUDIO:
2162
4/4
✓ Branch 0 taken 116010 times.
✓ Branch 1 taken 96334 times.
✓ Branch 3 taken 2054 times.
✓ Branch 4 taken 113956 times.
212344 if (!avctx->frame_size && determinable_frame_size(avctx))
2163
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2052 times.
2054 FAIL("unspecified frame size");
2164
2/2
✓ Branch 0 taken 210280 times.
✓ Branch 1 taken 10 times.
210290 if (sti->info->found_decoder >= 0 &&
2165
2/2
✓ Branch 0 taken 3096 times.
✓ Branch 1 taken 207184 times.
210280 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 206972 times.
207194 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 206950 times.
206972 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 206940 times.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 85194 times.
✓ Branch 3 taken 121746 times.
✓ Branch 4 taken 110 times.
✓ Branch 5 taken 85084 times.
206950 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 206840 break;
2174 317398 case AVMEDIA_TYPE_VIDEO:
2175
2/2
✓ Branch 0 taken 13460 times.
✓ Branch 1 taken 303938 times.
317398 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 303590 times.
✓ Branch 1 taken 348 times.
✓ Branch 2 taken 5043 times.
✓ Branch 3 taken 298547 times.
303938 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 298795 times.
✓ Branch 1 taken 100 times.
✓ Branch 2 taken 86 times.
✓ Branch 3 taken 298709 times.
298895 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 298828 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 507106 return 1;
2192 }
2193
2194 /* returns 1 or 0 if or if not decoded data was returned, or a negative error */
2195 213865 static int try_decode_frame(AVFormatContext *s, AVStream *st,
2196 const AVPacket *pkt, AVDictionary **options)
2197 {
2198 213865 FFStream *const sti = ffstream(st);
2199 213865 AVCodecContext *const avctx = sti->avctx;
2200 const AVCodec *codec;
2201 213865 int got_picture = 1, ret = 0;
2202 213865 AVFrame *frame = av_frame_alloc();
2203 AVSubtitle subtitle;
2204 213865 int do_skip_frame = 0;
2205 enum AVDiscard skip_frame;
2206 213865 int pkt_to_send = pkt->size > 0;
2207
2208
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 213865 times.
213865 if (!frame)
2209 ✗ return AVERROR(ENOMEM);
2210
2211
2/2
✓ Branch 1 taken 1940 times.
✓ Branch 2 taken 211925 times.
213865 if (!avcodec_is_open(avctx) &&
2212
1/2
✓ Branch 0 taken 1940 times.
✗ Branch 1 not taken.
1940 sti->info->found_decoder <= 0 &&
2213
4/4
✓ Branch 0 taken 368 times.
✓ Branch 1 taken 1572 times.
✓ Branch 2 taken 57 times.
✓ Branch 3 taken 311 times.
1940 (st->codecpar->codec_id != -sti->info->found_decoder || !st->codecpar->codec_id)) {
2214 1629 AVDictionary *thread_opt = NULL;
2215
2216 1629 codec = find_probe_decoder(s, st, st->codecpar->codec_id);
2217
2218
2/2
✓ Branch 0 taken 95 times.
✓ Branch 1 taken 1534 times.
1629 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 182 times.
✓ Branch 1 taken 1352 times.
1534 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 182 times.
✓ Branch 1 taken 1352 times.
1534 av_dict_set(options ? options : &thread_opt, "lowres", "0", 0);
2231
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1534 times.
1534 if (s->codec_whitelist)
2232 ✗ av_dict_set(options ? options : &thread_opt, "codec_whitelist", s->codec_whitelist, 0);
2233
2/2
✓ Branch 0 taken 182 times.
✓ Branch 1 taken 1352 times.
1534 ret = avcodec_open2(avctx, codec, options ? options : &thread_opt);
2234
2/2
✓ Branch 0 taken 182 times.
✓ Branch 1 taken 1352 times.
1534 if (!options)
2235 182 av_dict_free(&thread_opt);
2236
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1532 times.
1534 if (ret < 0) {
2237 2 sti->info->found_decoder = -avctx->codec_id;
2238 2 goto fail;
2239 }
2240 1532 sti->info->found_decoder = 1;
2241
2/2
✓ Branch 0 taken 7587 times.
✓ Branch 1 taken 204649 times.
212236 } else if (!sti->info->found_decoder)
2242 7587 sti->info->found_decoder = 1;
2243
2244
2/2
✓ Branch 0 taken 311 times.
✓ Branch 1 taken 213457 times.
213768 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 92790 times.
213457 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 9024 times.
✓ Branch 1 taken 5045 times.
✓ Branch 2 taken 30 times.
✓ Branch 3 taken 5015 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 213465 times.
435976 while ((pkt_to_send || (!pkt->data && got_picture)) &&
2256
4/4
✓ Branch 0 taken 14069 times.
✓ Branch 1 taken 208450 times.
✓ Branch 2 taken 6294 times.
✓ Branch 3 taken 207171 times.
435984 ret >= 0 &&
2257
2/2
✓ Branch 2 taken 2364 times.
✓ Branch 3 taken 204807 times.
420636 (!has_codec_parameters(st, NULL) || !has_decode_delay_been_guessed(st) ||
2258
2/2
✓ Branch 0 taken 201589 times.
✓ Branch 1 taken 3218 times.
204807 (!sti->codec_info_nb_frames &&
2259
2/2
✓ Branch 0 taken 654 times.
✓ Branch 1 taken 2564 times.
3218 (avctx->codec->capabilities & AV_CODEC_CAP_CHANNEL_CONF)))) {
2260 9312 got_picture = 0;
2261
2/2
✓ Branch 0 taken 1246 times.
✓ Branch 1 taken 8066 times.
9312 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO ||
2262
2/2
✓ Branch 0 taken 1239 times.
✓ Branch 1 taken 7 times.
1246 avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
2263 9305 ret = avcodec_send_packet(avctx, pkt);
2264
5/6
✓ Branch 0 taken 259 times.
✓ Branch 1 taken 9046 times.
✓ Branch 2 taken 259 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 250 times.
✓ Branch 5 taken 9 times.
9305 if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
2265 250 break;
2266
2/2
✓ Branch 0 taken 9046 times.
✓ Branch 1 taken 9 times.
9055 if (ret >= 0)
2267 9046 pkt_to_send = 0;
2268 9055 ret = avcodec_receive_frame(avctx, frame);
2269
2/2
✓ Branch 0 taken 3550 times.
✓ Branch 1 taken 5505 times.
9055 if (ret >= 0)
2270 3550 got_picture = 1;
2271
4/4
✓ Branch 0 taken 3580 times.
✓ Branch 1 taken 5475 times.
✓ Branch 2 taken 30 times.
✓ Branch 3 taken 3550 times.
9055 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 9062 times.
✗ Branch 1 not taken.
9062 if (ret >= 0) {
2282
2/2
✓ Branch 0 taken 3552 times.
✓ Branch 1 taken 5510 times.
9062 if (got_picture)
2283 3552 sti->nb_decoded_frames++;
2284 9062 ret = got_picture;
2285 }
2286 }
2287
2288 213207 fail:
2289
2/2
✓ Branch 0 taken 120667 times.
✓ Branch 1 taken 93198 times.
213865 if (do_skip_frame) {
2290 120667 avctx->skip_frame = skip_frame;
2291 }
2292
2293 213865 av_frame_free(&frame);
2294 213865 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 8462 static int compute_chapters_end(AVFormatContext *s)
2308 {
2309 8462 int64_t max_time = 0;
2310 AVChapter **timetable;
2311
2312
2/2
✓ Branch 0 taken 8405 times.
✓ Branch 1 taken 57 times.
8462 if (!s->nb_chapters)
2313 8405 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 295541 static int tb_unreliable(AVFormatContext *ic, AVStream *st)
2369 {
2370 295541 FFStream *const sti = ffstream(st);
2371 295541 const AVCodecDescriptor *desc = sti->codec_desc;
2372 295541 AVCodecContext *c = sti->avctx;
2373
4/4
✓ Branch 0 taken 294908 times.
✓ Branch 1 taken 633 times.
✓ Branch 2 taken 22961 times.
✓ Branch 3 taken 271947 times.
295541 AVRational mul = (AVRational){ desc && (desc->props & AV_CODEC_PROP_FIELDS) ? 2 : 1, 1 };
2374 295541 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 275258 times.
295541 : (((ic->ctx_flags & AVFMTCTX_NOHEADER) ||
2377
2/2
✓ Branch 0 taken 121093 times.
✓ Branch 1 taken 33052 times.
154145 st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) ? (AVRational){0, 1}
2378
2/2
✓ Branch 0 taken 154145 times.
✓ Branch 1 taken 121113 times.
275258 : st->time_base);
2379
2380
2/2
✓ Branch 0 taken 26785 times.
✓ Branch 1 taken 268756 times.
295541 if (time_base.den >= 101LL * time_base.num ||
2381
2/2
✓ Branch 0 taken 26094 times.
✓ Branch 1 taken 691 times.
26785 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 25883 times.
✓ Branch 1 taken 211 times.
26094 c->codec_tag == AV_RL32("mp4v") ||
2385
2/2
✓ Branch 0 taken 16356 times.
✓ Branch 1 taken 9527 times.
25883 c->codec_id == AV_CODEC_ID_MPEG2VIDEO ||
2386
2/2
✓ Branch 0 taken 15826 times.
✓ Branch 1 taken 530 times.
16356 c->codec_id == AV_CODEC_ID_GIF ||
2387
2/2
✓ Branch 0 taken 14443 times.
✓ Branch 1 taken 1383 times.
15826 c->codec_id == AV_CODEC_ID_HEVC ||
2388
2/2
✓ Branch 0 taken 4110 times.
✓ Branch 1 taken 10333 times.
14443 c->codec_id == AV_CODEC_ID_H264)
2389 285208 return 1;
2390 10333 return 0;
2391 }
2392
2393 150746 int ff_rfps_add_frame(AVFormatContext *ic, AVStream *st, int64_t ts)
2394 {
2395 150746 FFStream *const sti = ffstream(st);
2396 150746 FFStreamInfo *info = sti->info;
2397 150746 int64_t last = info->last_dts;
2398
2399
6/6
✓ Branch 0 taken 133692 times.
✓ Branch 1 taken 17054 times.
✓ Branch 2 taken 127054 times.
✓ Branch 3 taken 6638 times.
✓ Branch 4 taken 127027 times.
✓ Branch 5 taken 27 times.
150746 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 133692 times.
✓ Branch 1 taken 17054 times.
150746 if (ts != AV_NOPTS_VALUE)
2449 133692 info->last_dts = ts;
2450
2451 150746 return 0;
2452 }
2453
2454 9127 void ff_rfps_calculate(AVFormatContext *ic)
2455 {
2456
2/2
✓ Branch 0 taken 10709 times.
✓ Branch 1 taken 9127 times.
19836 for (unsigned i = 0; i < ic->nb_streams; i++) {
2457 10709 AVStream *const st = ic->streams[i];
2458 10709 FFStream *const sti = ffstream(st);
2459
2460
2/2
✓ Branch 0 taken 3422 times.
✓ Branch 1 taken 7287 times.
10709 if (st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO)
2461 3422 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 1464 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.
7287 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 3149 times.
✓ Branch 2 taken 440 times.
✓ Branch 3 taken 3698 times.
7287 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 1200 times.
✓ Branch 1 taken 6087 times.
7287 if ( !st->avg_frame_rate.num
2502
4/4
✓ Branch 0 taken 352 times.
✓ Branch 1 taken 848 times.
✓ Branch 2 taken 347 times.
✓ Branch 3 taken 5 times.
1200 && 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 7287 av_freep(&sti->info->duration_error);
2512 7287 sti->info->last_dts = AV_NOPTS_VALUE;
2513 7287 sti->info->duration_count = 0;
2514 7287 sti->info->rfps_duration_sum = 0;
2515 }
2516 9127 }
2517
2518 9955 static int extract_extradata_check(AVStream *st)
2519 {
2520 9955 const AVBitStreamFilter *const f = av_bsf_get_by_name("extract_extradata");
2521
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9955 times.
9955 if (!f)
2522 ✗ return 0;
2523
2524
1/2
✓ Branch 0 taken 9955 times.
✗ Branch 1 not taken.
9955 if (f->codec_ids) {
2525 const enum AVCodecID *ids;
2526
2/2
✓ Branch 0 taken 114921 times.
✓ Branch 1 taken 8968 times.
123889 for (ids = f->codec_ids; *ids != AV_CODEC_ID_NONE; ids++)
2527
2/2
✓ Branch 0 taken 987 times.
✓ Branch 1 taken 113934 times.
114921 if (*ids == st->codecpar->codec_id)
2528 987 return 1;
2529 }
2530
2531 8968 return 0;
2532 }
2533
2534 7771 static int extract_extradata_init(AVStream *st)
2535 {
2536 7771 FFStream *const sti = ffstream(st);
2537 const AVBitStreamFilter *f;
2538 int ret;
2539
2540 7771 f = av_bsf_get_by_name("extract_extradata");
2541
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7771 times.
7771 if (!f)
2542 ✗ goto finish;
2543
2544 /* check that the codec id is supported */
2545 7771 ret = extract_extradata_check(st);
2546
2/2
✓ Branch 0 taken 6894 times.
✓ Branch 1 taken 877 times.
7771 if (!ret)
2547 6894 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 7771 sti->extract_extradata.inited = 1;
2567
2568 7771 return 0;
2569 ✗ fail:
2570 ✗ av_bsf_free(&sti->extract_extradata.bsf);
2571 ✗ return ret;
2572 }
2573
2574 173542 static int extract_extradata(FFFormatContext *si, AVStream *st, const AVPacket *pkt)
2575 {
2576 173542 FFStream *const sti = ffstream(st);
2577 173542 AVPacket *const pkt_ref = si->parse_pkt;
2578 int ret;
2579
2580
2/2
✓ Branch 0 taken 7771 times.
✓ Branch 1 taken 165771 times.
173542 if (!sti->extract_extradata.inited) {
2581 7771 ret = extract_extradata_init(st);
2582
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7771 times.
7771 if (ret < 0)
2583 ✗ return ret;
2584 }
2585
2586
3/4
✓ Branch 0 taken 173542 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 172235 times.
✓ Branch 3 taken 1307 times.
173542 if (sti->extract_extradata.inited && !sti->extract_extradata.bsf)
2587 172235 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 9638 static int parameters_from_context(AVFormatContext *ic, AVCodecParameters *par,
2624 const AVCodecContext *avctx)
2625 {
2626 AVCodecParameters *par_tmp;
2627 int ret;
2628
2629 9638 par_tmp = avcodec_parameters_alloc();
2630
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9638 times.
9638 if (!par_tmp)
2631 ✗ return AVERROR(ENOMEM);
2632
2633 9638 ret = avcodec_parameters_copy(par_tmp, par);
2634
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9638 times.
9638 if (ret < 0)
2635 ✗ goto fail;
2636
2637 9638 ret = avcodec_parameters_from_context(par, avctx);
2638
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9638 times.
9638 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 9373 times.
9638 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 9526 times.
9638 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 9486 times.
9638 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 9510 times.
9638 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 9570 times.
9638 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 2588 times.
✓ Branch 1 taken 7050 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 2587 times.
9638 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 9638 ret = 0;
2667 9638 fail:
2668 9638 avcodec_parameters_free(&par_tmp);
2669
2670 9638 return ret;
2671 }
2672
2673 8462 int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
2674 {
2675 8462 FFFormatContext *const si = ffformatcontext(ic);
2676 8462 int count = 0, ret = 0, err;
2677 int64_t read_size;
2678 8462 AVPacket *pkt1 = si->pkt;
2679 8462 int64_t old_offset = avio_tell(ic->pb);
2680 // new streams might appear, no options for those
2681 8462 int orig_nb_streams = ic->nb_streams;
2682 int flush_codecs;
2683 8462 int64_t max_analyze_duration = ic->max_analyze_duration;
2684 int64_t max_stream_analyze_duration;
2685 int64_t max_subtitle_analyze_duration;
2686 8462 int64_t probesize = ic->probesize;
2687 8462 int eof_reached = 0;
2688
2689 8462 flush_codecs = probesize > 0;
2690
2691 8462 av_opt_set_int(ic, "skip_clear", 1, AV_OPT_SEARCH_CHILDREN);
2692
2693 8462 max_stream_analyze_duration = max_analyze_duration;
2694 8462 max_subtitle_analyze_duration = max_analyze_duration;
2695
2/2
✓ Branch 0 taken 8457 times.
✓ Branch 1 taken 5 times.
8462 if (!max_analyze_duration) {
2696 8457 max_stream_analyze_duration =
2697 8457 max_analyze_duration = 5*AV_TIME_BASE;
2698 8457 max_subtitle_analyze_duration = 30*AV_TIME_BASE;
2699
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 8413 times.
8457 if (!strcmp(ic->iformat->name, "flv"))
2700 44 max_stream_analyze_duration = 90*AV_TIME_BASE;
2701
4/4
✓ Branch 0 taken 8429 times.
✓ Branch 1 taken 28 times.
✓ Branch 2 taken 76 times.
✓ Branch 3 taken 8353 times.
8457 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 5252 times.
✓ Branch 1 taken 3210 times.
8462 if (ic->pb) {
2706 5252 FFIOContext *const ctx = ffiocontext(ic->pb);
2707 5252 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 9453 times.
✓ Branch 1 taken 8462 times.
17915 for (unsigned i = 0; i < ic->nb_streams; i++) {
2712 const AVCodec *codec;
2713 9453 AVDictionary *thread_opt = NULL;
2714 9453 AVStream *const st = ic->streams[i];
2715 9453 FFStream *const sti = ffstream(st);
2716 9453 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 9453 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9451 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 8873 times.
✓ Branch 5 taken 578 times.
9453 if (!sti->parser && !(ic->flags & AVFMT_FLAG_NOPARSE) && sti->request_probe <= 0) {
2721 8873 sti->parser = av_parser_init(st->codecpar->codec_id);
2722
2/2
✓ Branch 0 taken 6273 times.
✓ Branch 1 taken 2600 times.
8873 if (sti->parser) {
2723
2/2
✓ Branch 0 taken 880 times.
✓ Branch 1 taken 5393 times.
6273 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 4397 times.
5393 } 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 1912 times.
2600 } 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 9453 ret = avcodec_parameters_to_context(avctx, st->codecpar);
2736
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9453 times.
9453 if (ret < 0)
2737 ✗ goto find_stream_info_err;
2738
2/2
✓ Branch 0 taken 8875 times.
✓ Branch 1 taken 578 times.
9453 if (sti->request_probe <= 0)
2739 8875 sti->avctx_inited = 1;
2740
2741 9453 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 9138 times.
✓ Branch 1 taken 315 times.
9453 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 9138 times.
✓ Branch 1 taken 315 times.
9453 av_dict_set(options ? &options[i] : &thread_opt, "lowres", "0", 0);
2750
2751
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9453 times.
9453 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 8523 times.
✓ Branch 2 taken 930 times.
✓ Branch 3 taken 577 times.
✓ Branch 4 taken 7946 times.
9453 if (!has_codec_parameters(st, NULL) && sti->request_probe <= 0 ||
2757
2/2
✓ Branch 0 taken 94 times.
✓ Branch 1 taken 1413 times.
1507 st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) {
2758
3/4
✓ Branch 0 taken 7988 times.
✓ Branch 1 taken 52 times.
✓ Branch 2 taken 7988 times.
✗ Branch 3 not taken.
8040 if (codec && !avctx->codec)
2759
4/4
✓ Branch 0 taken 7683 times.
✓ Branch 1 taken 305 times.
✓ Branch 3 taken 7 times.
✓ Branch 4 taken 7981 times.
7988 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 315 times.
✓ Branch 1 taken 9138 times.
9453 if (!options)
2764 315 av_dict_free(&thread_opt);
2765 }
2766
2767 8462 read_size = 0;
2768 208859 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 217321 times.
217321 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 217321 ret = update_stream_avctx(ic);
2785
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 217321 times.
217321 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 297524 times.
✓ Branch 1 taken 116779 times.
414303 for (i = 0; i < ic->nb_streams; i++) {
2790 297524 AVStream *const st = ic->streams[i];
2791 297524 FFStream *const sti = ffstream(st);
2792 297524 int fps_analyze_framecount = 20;
2793 int count;
2794
2795
2/2
✓ Branch 1 taken 9710 times.
✓ Branch 2 taken 287814 times.
297524 if (!has_codec_parameters(st, NULL))
2796 9710 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 128695 times.
✓ Branch 2 taken 159119 times.
287814 if (av_q2d(st->time_base) > 0.0005)
2801 128695 fps_analyze_framecount *= 2;
2802
2/2
✓ Branch 1 taken 8759 times.
✓ Branch 2 taken 279055 times.
287814 if (!tb_unreliable(ic, st))
2803 8759 fps_analyze_framecount = 0;
2804
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 287814 times.
287814 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 287764 times.
287814 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 267492 times.
287814 count = (ic->iformat->flags & AVFMT_NOTIMESTAMPS) ?
2810 20322 sti->info->codec_info_duration_fields/2 :
2811 267492 sti->info->duration_count;
2812
4/4
✓ Branch 0 taken 106119 times.
✓ Branch 1 taken 181695 times.
✓ Branch 2 taken 423 times.
✓ Branch 3 taken 105696 times.
287814 if (!(st->r_frame_rate.num && st->avg_frame_rate.num) &&
2813
2/2
✓ Branch 0 taken 47425 times.
✓ Branch 1 taken 134693 times.
182118 st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
2814
2/2
✓ Branch 0 taken 28398 times.
✓ Branch 1 taken 19027 times.
47425 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 255843 times.
✓ Branch 2 taken 189 times.
✓ Branch 3 taken 3384 times.
✓ Branch 4 taken 12 times.
✓ Branch 5 taken 177 times.
259416 if (sti->info->frame_delay_evidence && count < 2 && sti->avctx->has_b_frames == 0)
2820 12 break;
2821
2/2
✓ Branch 0 taken 235616 times.
✓ Branch 1 taken 23788 times.
259404 if (!sti->avctx->extradata &&
2822
6/6
✓ Branch 0 taken 233542 times.
✓ Branch 1 taken 2074 times.
✓ Branch 2 taken 110 times.
✓ Branch 3 taken 233432 times.
✓ Branch 4 taken 110 times.
✓ Branch 5 taken 2074 times.
237800 (!sti->extract_extradata.inited || sti->extract_extradata.bsf) &&
2823 2184 extract_extradata_check(st))
2824 110 break;
2825
2/2
✓ Branch 0 taken 65812 times.
✓ Branch 1 taken 193482 times.
259294 if (sti->first_dts == AV_NOPTS_VALUE &&
2826
6/6
✓ Branch 0 taken 9936 times.
✓ Branch 1 taken 55876 times.
✓ Branch 2 taken 9670 times.
✓ Branch 3 taken 266 times.
✓ Branch 4 taken 63185 times.
✓ Branch 5 taken 2361 times.
131358 (!(ic->iformat->flags & AVFMT_NOTIMESTAMPS) || sti->need_parsing == AVSTREAM_PARSE_FULL_RAW) &&
2827
2/2
✓ Branch 0 taken 65496 times.
✓ Branch 1 taken 50 times.
65546 sti->codec_info_nb_frames < ((st->disposition & AV_DISPOSITION_ATTACHED_PIC) ? 1 : ic->max_ts_probe) &&
2828
2/2
✓ Branch 0 taken 46734 times.
✓ Branch 1 taken 16451 times.
63185 (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ||
2829
2/2
✓ Branch 0 taken 873 times.
✓ Branch 1 taken 45861 times.
46734 st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO))
2830 break;
2831 }
2832 217321 analyzed_all_streams = 0;
2833
4/4
✓ Branch 0 taken 116779 times.
✓ Branch 1 taken 100542 times.
✓ Branch 2 taken 116734 times.
✓ Branch 3 taken 45 times.
217321 if (i == ic->nb_streams && !si->missing_streams) {
2834 116734 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 3810 times.
✓ Branch 1 taken 112924 times.
116734 if (!(ic->ctx_flags & AVFMTCTX_NOHEADER)) {
2838 /* If we found the info for all the codecs, we can stop. */
2839 3810 ret = count;
2840 3810 av_log(ic, AV_LOG_DEBUG, "All info found\n");
2841 3810 flush_codecs = 0;
2842 3810 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 210417 times.
213511 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 210417 ret = read_frame_internal(ic, pkt1);
2867
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 210417 times.
210417 if (ret == AVERROR(EAGAIN))
2868 ✗ continue;
2869
2870
2/2
✓ Branch 0 taken 1456 times.
✓ Branch 1 taken 208961 times.
210417 if (ret < 0) {
2871 /* EOF or error*/
2872 1456 eof_reached = 1;
2873 1456 break;
2874 }
2875
2876
1/2
✓ Branch 0 taken 208961 times.
✗ Branch 1 not taken.
208961 if (!(ic->flags & AVFMT_FLAG_NOBUFFER)) {
2877 208961 ret = ff_packet_list_put(&si->packet_buffer,
2878 pkt1, NULL, 0);
2879
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 208961 times.
208961 if (ret < 0)
2880 ✗ goto unref_then_goto_end;
2881
2882 208961 pkt = &si->packet_buffer.tail->pkt;
2883 } else {
2884 ✗ pkt = pkt1;
2885 }
2886
2887 208961 st = ic->streams[pkt->stream_index];
2888 208961 sti = ffstream(st);
2889
2/2
✓ Branch 0 taken 208896 times.
✓ Branch 1 taken 65 times.
208961 if (!(st->disposition & AV_DISPOSITION_ATTACHED_PIC))
2890 208896 read_size += pkt->size;
2891
2892 208961 avctx = sti->avctx;
2893
2/2
✓ Branch 0 taken 763 times.
✓ Branch 1 taken 208198 times.
208961 if (!sti->avctx_inited) {
2894 763 ret = avcodec_parameters_to_context(avctx, st->codecpar);
2895
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 763 times.
763 if (ret < 0)
2896 ✗ goto unref_then_goto_end;
2897 763 sti->avctx_inited = 1;
2898 }
2899
2900
4/4
✓ Branch 0 taken 191497 times.
✓ Branch 1 taken 17464 times.
✓ Branch 2 taken 177691 times.
✓ Branch 3 taken 13806 times.
208961 if (pkt->dts != AV_NOPTS_VALUE && sti->codec_info_nb_frames > 1) {
2901 /* check for non-increasing dts */
2902
2/2
✓ Branch 0 taken 172298 times.
✓ Branch 1 taken 5393 times.
177691 if (sti->info->fps_last_dts != AV_NOPTS_VALUE &&
2903
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 172273 times.
172298 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 172273 times.
✓ Branch 1 taken 5418 times.
177691 if (sti->info->fps_last_dts != AV_NOPTS_VALUE &&
2917
2/2
✓ Branch 0 taken 166921 times.
✓ Branch 1 taken 5352 times.
172273 sti->info->fps_last_dts_idx > sti->info->fps_first_dts_idx &&
2918 166921 (pkt->dts - (uint64_t)sti->info->fps_last_dts) / 1000 >
2919 166921 (sti->info->fps_last_dts - (uint64_t)sti->info->fps_first_dts) /
2920
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 166921 times.
166921 (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 5418 times.
✓ Branch 1 taken 172273 times.
177691 if (sti->info->fps_first_dts == AV_NOPTS_VALUE) {
2933 5418 sti->info->fps_first_dts = pkt->dts;
2934 5418 sti->info->fps_first_dts_idx = sti->codec_info_nb_frames;
2935 }
2936 177691 sti->info->fps_last_dts = pkt->dts;
2937 177691 sti->info->fps_last_dts_idx = sti->codec_info_nb_frames;
2938 }
2939
2/2
✓ Branch 0 taken 193808 times.
✓ Branch 1 taken 15153 times.
208961 if (sti->codec_info_nb_frames > 1) {
2940 193808 int64_t t = 0;
2941 int64_t limit;
2942
2943
1/2
✓ Branch 0 taken 193808 times.
✗ Branch 1 not taken.
193808 if (st->time_base.den > 0)
2944 193808 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 76104 times.
193808 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 5165 times.
✓ Branch 1 taken 188643 times.
193808 if ( t == 0
2949
2/2
✓ Branch 0 taken 614 times.
✓ Branch 1 taken 4551 times.
5165 && 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 87376 times.
193808 if (analyzed_all_streams) limit = max_analyze_duration;
2957
2/2
✓ Branch 0 taken 91 times.
✓ Branch 1 taken 87285 times.
87376 else if (avctx->codec_type == AVMEDIA_TYPE_SUBTITLE) limit = max_subtitle_analyze_duration;
2958 87285 else limit = max_stream_analyze_duration;
2959
2960
2/2
✓ Branch 0 taken 102 times.
✓ Branch 1 taken 193706 times.
193808 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 189350 times.
✓ Branch 1 taken 4356 times.
✓ Branch 2 taken 189350 times.
✗ Branch 3 not taken.
193706 if (pkt->duration > 0 && pkt->duration < INT64_MAX - sti->info->codec_info_duration) {
2969
4/4
✓ Branch 0 taken 189310 times.
✓ Branch 1 taken 40 times.
✓ Branch 2 taken 15374 times.
✓ Branch 3 taken 173936 times.
189350 const int fields = sti->codec_desc && (sti->codec_desc->props & AV_CODEC_PROP_FIELDS);
2970
1/8
✗ Branch 0 not taken.
✓ Branch 1 taken 189350 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.
189350 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 189350 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.
329466 sti->info->codec_info_duration_fields += sti->parser && sti->need_parsing && fields
2977
2/2
✓ Branch 0 taken 140116 times.
✓ Branch 1 taken 49234 times.
329466 ? sti->parser->repeat_pict + 1 : 2;
2978 }
2979 }
2980
2/2
✓ Branch 0 taken 138567 times.
✓ Branch 1 taken 70292 times.
208859 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
2981 #if FF_API_R_FRAME_RATE
2982 138567 ff_rfps_add_frame(ic, st, pkt->dts);
2983 #endif
2984
6/6
✓ Branch 0 taken 5508 times.
✓ Branch 1 taken 133059 times.
✓ Branch 2 taken 5174 times.
✓ Branch 3 taken 334 times.
✓ Branch 4 taken 3144 times.
✓ Branch 5 taken 2030 times.
138567 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 173377 times.
✓ Branch 1 taken 35482 times.
208859 if (!sti->avctx->extradata) {
2988 173377 ret = extract_extradata(si, st, pkt);
2989
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 173377 times.
173377 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 190207 times.
✓ Branch 1 taken 18652 times.
399066 try_decode_frame(ic, st, pkt,
3003
2/2
✓ Branch 0 taken 88244 times.
✓ Branch 1 taken 101963 times.
190207 (options && i < orig_nb_streams) ? &options[i] : NULL);
3004
3005
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 208859 times.
208859 if (ic->flags & AVFMT_FLAG_NOBUFFER)
3006 ✗ av_packet_unref(pkt1);
3007
3008 208859 sti->codec_info_nb_frames++;
3009 208859 count++;
3010 }
3011
3012
2/2
✓ Branch 0 taken 1456 times.
✓ Branch 1 taken 7006 times.
8462 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 3810 times.
8462 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 8462 ff_rfps_calculate(ic);
3061
3062
2/2
✓ Branch 0 taken 9654 times.
✓ Branch 1 taken 8462 times.
18116 for (unsigned i = 0; i < ic->nb_streams; i++) {
3063 9654 AVStream *const st = ic->streams[i];
3064 9654 FFStream *const sti = ffstream(st);
3065 9654 AVCodecContext *const avctx = sti->avctx;
3066
3067
2/2
✓ Branch 0 taken 6781 times.
✓ Branch 1 taken 2873 times.
9654 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
3068
6/6
✓ Branch 0 taken 658 times.
✓ Branch 1 taken 6123 times.
✓ Branch 2 taken 609 times.
✓ Branch 3 taken 49 times.
✓ Branch 4 taken 593 times.
✓ Branch 5 taken 16 times.
6781 if (avctx->codec_id == AV_CODEC_ID_RAWVIDEO && !avctx->codec_tag && !avctx->bits_per_coded_sample) {
3069 593 uint32_t tag= avcodec_pix_fmt_to_codec_tag(avctx->pix_fmt);
3070
2/2
✓ Branch 1 taken 586 times.
✓ Branch 2 taken 7 times.
593 if (avpriv_pix_fmt_find(PIX_FMT_LIST_RAW, tag) == avctx->pix_fmt)
3071 586 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 2547 times.
6781 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 2026 times.
✓ Branch 1 taken 4755 times.
6781 if (!st->r_frame_rate.num) {
3117 2026 const AVCodecDescriptor *desc = sti->codec_desc;
3118
3/4
✓ Branch 0 taken 2026 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 256 times.
✓ Branch 3 taken 1770 times.
2026 AVRational mul = (AVRational){ desc && (desc->props & AV_CODEC_PROP_FIELDS) ? 2 : 1, 1 };
3119 2026 AVRational fr = av_mul_q(avctx->framerate, mul);
3120
3121
5/6
✓ Branch 0 taken 206 times.
✓ Branch 1 taken 1820 times.
✓ Branch 2 taken 206 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 192 times.
✓ Branch 7 taken 14 times.
2026 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 1834 st->r_frame_rate.num = st->time_base.den;
3125 1834 st->r_frame_rate.den = st->time_base.num;
3126 }
3127 }
3128 6781 st->codecpar->framerate = avctx->framerate;
3129
3/4
✓ Branch 0 taken 122 times.
✓ Branch 1 taken 6659 times.
✓ Branch 2 taken 122 times.
✗ Branch 3 not taken.
6781 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 2659 times.
✓ Branch 1 taken 214 times.
2873 } else if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
3135
2/2
✓ Branch 0 taken 1035 times.
✓ Branch 1 taken 1624 times.
2659 if (!avctx->bits_per_coded_sample)
3136 1035 avctx->bits_per_coded_sample =
3137 1035 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 2659 times.
2659 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 8462 times.
✗ Branch 1 not taken.
8462 if (probesize)
3160 8462 estimate_timings(ic, old_offset);
3161
3162 8462 av_opt_set_int(ic, "skip_clear", 0, AV_OPT_SEARCH_CHILDREN);
3163
3164
4/4
✓ Branch 0 taken 7006 times.
✓ Branch 1 taken 1456 times.
✓ Branch 2 taken 7004 times.
✓ Branch 3 taken 2 times.
8462 if (ret >= 0 && ic->nb_streams)
3165 /* We could not have all the codec parameters before EOF. */
3166 7004 ret = -1;
3167
2/2
✓ Branch 0 taken 9654 times.
✓ Branch 1 taken 8462 times.
18116 for (unsigned i = 0; i < ic->nb_streams; i++) {
3168 9654 AVStream *const st = ic->streams[i];
3169 9654 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 9638 times.
9654 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 9614 times.
9654 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 9614 ret = 0;
3190 }
3191 }
3192
3193 8462 err = compute_chapters_end(ic);
3194
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8462 times.
8462 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 9654 times.
✓ Branch 1 taken 8462 times.
18116 for (unsigned i = 0; i < ic->nb_streams; i++) {
3201 9654 AVStream *const st = ic->streams[i];
3202 9654 FFStream *const sti = ffstream(st);
3203
3204
2/2
✓ Branch 0 taken 9638 times.
✓ Branch 1 taken 16 times.
9654 if (sti->avctx_inited) {
3205 9638 ret = parameters_from_context(ic, st->codecpar, sti->avctx);
3206
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9638 times.
9638 if (ret < 0)
3207 ✗ goto find_stream_info_err;
3208
3209
3/4
✓ Branch 0 taken 9421 times.
✓ Branch 1 taken 217 times.
✓ Branch 2 taken 9421 times.
✗ Branch 3 not taken.
9638 if (sti->avctx->rc_buffer_size > 0 || sti->avctx->rc_max_rate > 0 ||
3210
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9421 times.
9421 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 9654 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 8462 times.
8625 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 8462 find_stream_info_err:
3266
2/2
✓ Branch 0 taken 9654 times.
✓ Branch 1 taken 8462 times.
18116 for (unsigned i = 0; i < ic->nb_streams; i++) {
3267 9654 AVStream *const st = ic->streams[i];
3268 9654 FFStream *const sti = ffstream(st);
3269 int err;
3270
3271
1/2
✓ Branch 0 taken 9654 times.
✗ Branch 1 not taken.
9654 if (sti->info) {
3272 9654 av_freep(&sti->info->duration_error);
3273 9654 av_freep(&sti->info);
3274 }
3275
3276
2/2
✓ Branch 1 taken 9502 times.
✓ Branch 2 taken 152 times.
9654 if (avcodec_is_open(sti->avctx)) {
3277 9502 err = codec_close(sti);
3278
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 9502 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
9502 if (err < 0 && ret >= 0)
3279 ✗ ret = err;
3280 }
3281
3282 9654 av_bsf_free(&sti->extract_extradata.bsf);
3283 }
3284
2/2
✓ Branch 0 taken 5252 times.
✓ Branch 1 taken 3210 times.
8462 if (ic->pb) {
3285 5252 FFIOContext *const ctx = ffiocontext(ic->pb);
3286 5252 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 8462 return ret;
3290
3291 ✗ unref_then_goto_end:
3292 ✗ av_packet_unref(pkt1);
3293 ✗ goto find_stream_info_err;
3294 }
3295