Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * Core demuxing component | ||
3 | * Copyright (c) 2000, 2001, 2002 Fabrice Bellard | ||
4 | * | ||
5 | * This file is part of FFmpeg. | ||
6 | * | ||
7 | * FFmpeg is free software; you can redistribute it and/or | ||
8 | * modify it under the terms of the GNU Lesser General Public | ||
9 | * License as published by the Free Software Foundation; either | ||
10 | * version 2.1 of the License, or (at your option) any later version. | ||
11 | * | ||
12 | * FFmpeg is distributed in the hope that it will be useful, | ||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
15 | * Lesser General Public License for more details. | ||
16 | * | ||
17 | * You should have received a copy of the GNU Lesser General Public | ||
18 | * License along with FFmpeg; if not, write to the Free Software | ||
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
20 | */ | ||
21 | |||
22 | #include <stdint.h> | ||
23 | |||
24 | #include "config_components.h" | ||
25 | |||
26 | #include "libavutil/avassert.h" | ||
27 | #include "libavutil/avstring.h" | ||
28 | #include "libavutil/dict.h" | ||
29 | #include "libavutil/internal.h" | ||
30 | #include "libavutil/intreadwrite.h" | ||
31 | #include "libavutil/mathematics.h" | ||
32 | #include "libavutil/mem.h" | ||
33 | #include "libavutil/opt.h" | ||
34 | #include "libavutil/pixfmt.h" | ||
35 | #include "libavutil/time.h" | ||
36 | #include "libavutil/timestamp.h" | ||
37 | |||
38 | #include "libavcodec/avcodec.h" | ||
39 | #include "libavcodec/bsf.h" | ||
40 | #include "libavcodec/codec_desc.h" | ||
41 | #include "libavcodec/internal.h" | ||
42 | #include "libavcodec/packet_internal.h" | ||
43 | #include "libavcodec/raw.h" | ||
44 | |||
45 | #include "avformat.h" | ||
46 | #include "avformat_internal.h" | ||
47 | #include "avio_internal.h" | ||
48 | #include "demux.h" | ||
49 | #include "id3v2.h" | ||
50 | #include "internal.h" | ||
51 | #include "url.h" | ||
52 | |||
53 | 2471453 | static int64_t wrap_timestamp(const AVStream *st, int64_t timestamp) | |
54 | { | ||
55 | 2471453 | const FFStream *const sti = cffstream(st); | |
56 |
4/4✓ Branch 0 taken 150446 times.
✓ Branch 1 taken 2321007 times.
✓ Branch 2 taken 145858 times.
✓ Branch 3 taken 4588 times.
|
2471453 | if (sti->pts_wrap_behavior != AV_PTS_WRAP_IGNORE && st->pts_wrap_bits < 64 && |
57 |
3/4✓ Branch 0 taken 145858 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 88227 times.
✓ Branch 3 taken 57631 times.
|
145858 | sti->pts_wrap_reference != AV_NOPTS_VALUE && timestamp != AV_NOPTS_VALUE) { |
58 |
2/2✓ Branch 0 taken 88023 times.
✓ Branch 1 taken 204 times.
|
88227 | if (sti->pts_wrap_behavior == AV_PTS_WRAP_ADD_OFFSET && |
59 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 88023 times.
|
88023 | timestamp < sti->pts_wrap_reference) |
60 | ✗ | return timestamp + (1ULL << st->pts_wrap_bits); | |
61 |
2/2✓ Branch 0 taken 204 times.
✓ Branch 1 taken 88023 times.
|
88227 | 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 | 2471403 | return timestamp; | |
66 | } | ||
67 | |||
68 | 303297 | int64_t ff_wrap_timestamp(const AVStream *st, int64_t timestamp) | |
69 | { | ||
70 | 303297 | return wrap_timestamp(st, timestamp); | |
71 | } | ||
72 | |||
73 | 9621 | 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 335 times.
✓ Branch 1 taken 9286 times.
|
9621 | if (codec_id == AV_CODEC_ID_H264) |
81 | 335 | return avcodec_find_decoder_by_name("h264"); | |
82 | #endif | ||
83 | |||
84 | 9286 | codec = ff_find_decoder(s, st, codec_id); | |
85 |
2/2✓ Branch 0 taken 171 times.
✓ Branch 1 taken 9115 times.
|
9286 | if (!codec) |
86 | 171 | return NULL; | |
87 | |||
88 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9115 times.
|
9115 | 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 | 9115 | return codec; | |
101 | } | ||
102 | |||
103 | 3025 | 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 | 3025 | const AVInputFormat *fmt = av_probe_input_format3(pd, 1, &score); | |
133 | 3025 | FFStream *const sti = ffstream(st); | |
134 | |||
135 |
2/2✓ Branch 0 taken 946 times.
✓ Branch 1 taken 2079 times.
|
3025 | if (fmt) { |
136 | 946 | av_log(s, AV_LOG_DEBUG, | |
137 | "Probe with size=%d, packets=%d detected %s with score=%d\n", | ||
138 | 946 | pd->buf_size, s->max_probe_packets - sti->probe_packets, | |
139 | 946 | fmt->name, score); | |
140 |
2/2✓ Branch 0 taken 16879 times.
✓ Branch 1 taken 924 times.
|
17803 | for (int i = 0; fmt_id_type[i].name; i++) { |
141 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 16855 times.
|
16879 | if (!strcmp(fmt->name, fmt_id_type[i].name)) { |
142 |
2/2✓ Branch 0 taken 11 times.
✓ Branch 1 taken 13 times.
|
24 | if (fmt_id_type[i].type != AVMEDIA_TYPE_AUDIO && |
143 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
|
11 | st->codecpar->sample_rate) |
144 | ✗ | continue; | |
145 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 18 times.
|
24 | if (sti->request_probe > score && |
146 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
|
6 | st->codecpar->codec_id != fmt_id_type[i].id) |
147 | 2 | continue; | |
148 | 22 | st->codecpar->codec_id = fmt_id_type[i].id; | |
149 | 22 | st->codecpar->codec_type = fmt_id_type[i].type; | |
150 | 22 | sti->need_context_update = 1; | |
151 | 22 | return score; | |
152 | } | ||
153 | } | ||
154 | } | ||
155 | 3003 | return 0; | |
156 | } | ||
157 | |||
158 | 7556 | static int init_input(AVFormatContext *s, const char *filename, | |
159 | AVDictionary **options) | ||
160 | { | ||
161 | int ret; | ||
162 | 7556 | AVProbeData pd = { filename, NULL, 0 }; | |
163 | 7556 | int score = AVPROBE_SCORE_RETRY; | |
164 | |||
165 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 7544 times.
|
7556 | if (s->pb) { |
166 | 12 | s->flags |= AVFMT_FLAG_CUSTOM_IO; | |
167 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | 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 12 times.
|
12 | 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 | 12 | return 0; | |
174 | } | ||
175 | |||
176 |
4/4✓ Branch 0 taken 3692 times.
✓ Branch 1 taken 3852 times.
✓ Branch 2 taken 849 times.
✓ Branch 3 taken 2843 times.
|
7544 | if ((s->iformat && s->iformat->flags & AVFMT_NOFILE) || |
177 |
4/4✓ Branch 0 taken 3852 times.
✓ Branch 1 taken 849 times.
✓ Branch 3 taken 193 times.
✓ Branch 4 taken 3659 times.
|
4701 | (!s->iformat && (s->iformat = av_probe_input_format2(&pd, 0, &score)))) |
178 | 3036 | return score; | |
179 | |||
180 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 4508 times.
|
4508 | 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 849 times.
✓ Branch 1 taken 3659 times.
|
4508 | if (s->iformat) |
184 | 849 | return 0; | |
185 | 3659 | return av_probe_input_buffer2(s->pb, &s->iformat, filename, | |
186 | 3659 | s, 0, s->format_probesize); | |
187 | } | ||
188 | |||
189 | 7556 | static int update_stream_avctx(AVFormatContext *s) | |
190 | { | ||
191 | int ret; | ||
192 |
2/2✓ Branch 0 taken 8179 times.
✓ Branch 1 taken 7556 times.
|
15735 | for (unsigned i = 0; i < s->nb_streams; i++) { |
193 | 8179 | AVStream *const st = s->streams[i]; | |
194 | 8179 | FFStream *const sti = ffstream(st); | |
195 | |||
196 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8179 times.
|
8179 | if (!sti->need_context_update) |
197 | ✗ | continue; | |
198 | |||
199 | /* close parser, because it depends on the codec */ | ||
200 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 8179 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
8179 | if (sti->parser && sti->avctx->codec_id != st->codecpar->codec_id) { |
201 | ✗ | av_parser_close(sti->parser); | |
202 | ✗ | sti->parser = NULL; | |
203 | } | ||
204 | |||
205 | /* update internal codec context, for the parser */ | ||
206 | 8179 | ret = avcodec_parameters_to_context(sti->avctx, st->codecpar); | |
207 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8179 times.
|
8179 | if (ret < 0) |
208 | ✗ | return ret; | |
209 | |||
210 | 8179 | sti->codec_desc = avcodec_descriptor_get(sti->avctx->codec_id); | |
211 | |||
212 | 8179 | sti->need_context_update = 0; | |
213 | } | ||
214 | 7556 | return 0; | |
215 | } | ||
216 | |||
217 | 7556 | int avformat_open_input(AVFormatContext **ps, const char *filename, | |
218 | const AVInputFormat *fmt, AVDictionary **options) | ||
219 | { | ||
220 | FormatContextInternal *fci; | ||
221 | 7556 | AVFormatContext *s = *ps; | |
222 | FFFormatContext *si; | ||
223 | 7556 | AVDictionary *tmp = NULL; | |
224 | 7556 | ID3v2ExtraMeta *id3v2_extra_meta = NULL; | |
225 | 7556 | int ret = 0; | |
226 | |||
227 |
3/4✓ Branch 0 taken 22 times.
✓ Branch 1 taken 7534 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 22 times.
|
7556 | if (!s && !(s = avformat_alloc_context())) |
228 | ✗ | return AVERROR(ENOMEM); | |
229 | 7556 | fci = ff_fc_internal(s); | |
230 | 7556 | si = &fci->fc; | |
231 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7556 times.
|
7556 | if (!s->av_class) { |
232 | ✗ | av_log(NULL, AV_LOG_ERROR, "Input context has not been properly allocated by avformat_alloc_context() and is not NULL either\n"); | |
233 | ✗ | return AVERROR(EINVAL); | |
234 | } | ||
235 |
2/2✓ Branch 0 taken 3704 times.
✓ Branch 1 taken 3852 times.
|
7556 | if (fmt) |
236 | 3704 | s->iformat = fmt; | |
237 | |||
238 |
2/2✓ Branch 0 taken 7546 times.
✓ Branch 1 taken 10 times.
|
7556 | if (options) |
239 | 7546 | av_dict_copy(&tmp, *options, 0); | |
240 | |||
241 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 7544 times.
|
7556 | if (s->pb) // must be before any goto fail |
242 | 12 | s->flags |= AVFMT_FLAG_CUSTOM_IO; | |
243 | |||
244 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 7556 times.
|
7556 | if ((ret = av_opt_set_dict(s, &tmp)) < 0) |
245 | ✗ | goto fail; | |
246 | |||
247 |
2/4✓ Branch 0 taken 7556 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 7556 times.
|
7556 | if (!(s->url = av_strdup(filename ? filename : ""))) { |
248 | ✗ | ret = AVERROR(ENOMEM); | |
249 | ✗ | goto fail; | |
250 | } | ||
251 | |||
252 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 7556 times.
|
7556 | if ((ret = init_input(s, filename, &tmp)) < 0) |
253 | ✗ | goto fail; | |
254 | 7556 | s->probe_score = ret; | |
255 | |||
256 |
6/6✓ Branch 0 taken 7473 times.
✓ Branch 1 taken 83 times.
✓ Branch 2 taken 4437 times.
✓ Branch 3 taken 3036 times.
✓ Branch 4 taken 4436 times.
✓ Branch 5 taken 1 times.
|
7556 | if (!s->protocol_whitelist && s->pb && s->pb->protocol_whitelist) { |
257 | 4436 | s->protocol_whitelist = av_strdup(s->pb->protocol_whitelist); | |
258 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4436 times.
|
4436 | if (!s->protocol_whitelist) { |
259 | ✗ | ret = AVERROR(ENOMEM); | |
260 | ✗ | goto fail; | |
261 | } | ||
262 | } | ||
263 | |||
264 |
4/6✓ Branch 0 taken 7556 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4520 times.
✓ Branch 3 taken 3036 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 4520 times.
|
7556 | if (!s->protocol_blacklist && s->pb && s->pb->protocol_blacklist) { |
265 | ✗ | s->protocol_blacklist = av_strdup(s->pb->protocol_blacklist); | |
266 | ✗ | if (!s->protocol_blacklist) { | |
267 | ✗ | ret = AVERROR(ENOMEM); | |
268 | ✗ | goto fail; | |
269 | } | ||
270 | } | ||
271 | |||
272 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 7556 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
7556 | if (s->format_whitelist && av_match_list(s->iformat->name, s->format_whitelist, ',') <= 0) { |
273 | ✗ | av_log(s, AV_LOG_ERROR, "Format not on whitelist \'%s\'\n", s->format_whitelist); | |
274 | ✗ | ret = AVERROR(EINVAL); | |
275 | ✗ | goto fail; | |
276 | } | ||
277 | |||
278 | 7556 | avio_skip(s->pb, s->skip_initial_bytes); | |
279 | |||
280 | /* Check filename in case an image number is expected. */ | ||
281 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7556 times.
|
7556 | if (s->iformat->flags & AVFMT_NEEDNUMBER) { |
282 | ✗ | if (!av_filename_number_test(filename)) { | |
283 | ✗ | ret = AVERROR(EINVAL); | |
284 | ✗ | goto fail; | |
285 | } | ||
286 | } | ||
287 | |||
288 | 7556 | s->duration = s->start_time = AV_NOPTS_VALUE; | |
289 | |||
290 | /* Allocate private data. */ | ||
291 |
2/2✓ Branch 1 taken 7399 times.
✓ Branch 2 taken 157 times.
|
7556 | if (ffifmt(s->iformat)->priv_data_size > 0) { |
292 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 7399 times.
|
7399 | if (!(s->priv_data = av_mallocz(ffifmt(s->iformat)->priv_data_size))) { |
293 | ✗ | ret = AVERROR(ENOMEM); | |
294 | ✗ | goto fail; | |
295 | } | ||
296 |
2/2✓ Branch 0 taken 6570 times.
✓ Branch 1 taken 829 times.
|
7399 | if (s->iformat->priv_class) { |
297 | 6570 | *(const AVClass **) s->priv_data = s->iformat->priv_class; | |
298 | 6570 | av_opt_set_defaults(s->priv_data); | |
299 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 6570 times.
|
6570 | if ((ret = av_opt_set_dict(s->priv_data, &tmp)) < 0) |
300 | ✗ | goto fail; | |
301 | } | ||
302 | } | ||
303 | |||
304 | /* e.g. AVFMT_NOFILE formats will not have an AVIOContext */ | ||
305 |
2/2✓ Branch 0 taken 4520 times.
✓ Branch 1 taken 3036 times.
|
7556 | if (s->pb) |
306 | 4520 | ff_id3v2_read_dict(s->pb, &si->id3v2_meta, ID3v2_DEFAULT_MAGIC, &id3v2_extra_meta); | |
307 | |||
308 |
1/2✓ Branch 1 taken 7556 times.
✗ Branch 2 not taken.
|
7556 | if (ffifmt(s->iformat)->read_header) |
309 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 7556 times.
|
7556 | if ((ret = ffifmt(s->iformat)->read_header(s)) < 0) { |
310 | ✗ | if (ffifmt(s->iformat)->flags_internal & FF_INFMT_FLAG_INIT_CLEANUP) | |
311 | ✗ | goto close; | |
312 | ✗ | goto fail; | |
313 | } | ||
314 | |||
315 |
2/2✓ Branch 0 taken 6281 times.
✓ Branch 1 taken 1275 times.
|
7556 | if (!s->metadata) { |
316 | 6281 | s->metadata = si->id3v2_meta; | |
317 | 6281 | si->id3v2_meta = NULL; | |
318 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1275 times.
|
1275 | } else if (si->id3v2_meta) { |
319 | ✗ | av_log(s, AV_LOG_WARNING, "Discarding ID3 tags because more suitable tags were found.\n"); | |
320 | ✗ | av_dict_free(&si->id3v2_meta); | |
321 | } | ||
322 | |||
323 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 7546 times.
|
7556 | if (id3v2_extra_meta) { |
324 |
3/4✓ Branch 0 taken 2 times.
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
|
10 | if (!strcmp(s->iformat->name, "mp3") || !strcmp(s->iformat->name, "aac") || |
325 | ✗ | !strcmp(s->iformat->name, "tta") || !strcmp(s->iformat->name, "wav")) { | |
326 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
|
10 | if ((ret = ff_id3v2_parse_apic(s, id3v2_extra_meta)) < 0) |
327 | ✗ | goto close; | |
328 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
|
10 | if ((ret = ff_id3v2_parse_chapters(s, id3v2_extra_meta)) < 0) |
329 | ✗ | goto close; | |
330 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
|
10 | if ((ret = ff_id3v2_parse_priv(s, id3v2_extra_meta)) < 0) |
331 | ✗ | goto close; | |
332 | } else | ||
333 | ✗ | av_log(s, AV_LOG_DEBUG, "demuxer does not support additional id3 data, skipping\n"); | |
334 | 10 | ff_id3v2_free_extra_meta(&id3v2_extra_meta); | |
335 | } | ||
336 | |||
337 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 7556 times.
|
7556 | if ((ret = avformat_queue_attached_pictures(s)) < 0) |
338 | ✗ | goto close; | |
339 | |||
340 |
4/4✓ Branch 0 taken 4520 times.
✓ Branch 1 taken 3036 times.
✓ Branch 2 taken 4083 times.
✓ Branch 3 taken 437 times.
|
7556 | if (s->pb && !si->data_offset) |
341 | 4083 | si->data_offset = avio_tell(s->pb); | |
342 | |||
343 | 7556 | fci->raw_packet_buffer_size = 0; | |
344 | |||
345 | 7556 | update_stream_avctx(s); | |
346 | |||
347 |
2/2✓ Branch 0 taken 7546 times.
✓ Branch 1 taken 10 times.
|
7556 | if (options) { |
348 | 7546 | av_dict_free(options); | |
349 | 7546 | *options = tmp; | |
350 | } | ||
351 | 7556 | *ps = s; | |
352 | 7556 | return 0; | |
353 | |||
354 | ✗ | close: | |
355 | ✗ | if (ffifmt(s->iformat)->read_close) | |
356 | ✗ | ffifmt(s->iformat)->read_close(s); | |
357 | ✗ | fail: | |
358 | ✗ | ff_id3v2_free_extra_meta(&id3v2_extra_meta); | |
359 | ✗ | av_dict_free(&tmp); | |
360 | ✗ | if (s->pb && !(s->flags & AVFMT_FLAG_CUSTOM_IO)) | |
361 | ✗ | avio_closep(&s->pb); | |
362 | ✗ | avformat_free_context(s); | |
363 | ✗ | *ps = NULL; | |
364 | ✗ | return ret; | |
365 | } | ||
366 | |||
367 | 7556 | void avformat_close_input(AVFormatContext **ps) | |
368 | { | ||
369 | AVFormatContext *s; | ||
370 | AVIOContext *pb; | ||
371 | |||
372 |
2/4✓ Branch 0 taken 7556 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 7556 times.
|
7556 | if (!ps || !*ps) |
373 | ✗ | return; | |
374 | |||
375 | 7556 | s = *ps; | |
376 | 7556 | pb = s->pb; | |
377 | |||
378 |
5/6✓ Branch 0 taken 7556 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4538 times.
✓ Branch 3 taken 3018 times.
✓ Branch 4 taken 4495 times.
✓ Branch 5 taken 43 times.
|
7556 | if ((s->iformat && strcmp(s->iformat->name, "image2") && s->iformat->flags & AVFMT_NOFILE) || |
379 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 7501 times.
|
7513 | (s->flags & AVFMT_FLAG_CUSTOM_IO)) |
380 | 55 | pb = NULL; | |
381 | |||
382 |
1/2✓ Branch 0 taken 7556 times.
✗ Branch 1 not taken.
|
7556 | if (s->iformat) |
383 |
2/2✓ Branch 1 taken 4902 times.
✓ Branch 2 taken 2654 times.
|
7556 | if (ffifmt(s->iformat)->read_close) |
384 | 4902 | ffifmt(s->iformat)->read_close(s); | |
385 | |||
386 | 7556 | avformat_free_context(s); | |
387 | |||
388 | 7556 | *ps = NULL; | |
389 | |||
390 | 7556 | avio_close(pb); | |
391 | } | ||
392 | |||
393 | 1087102 | static void force_codec_ids(AVFormatContext *s, AVStream *st) | |
394 | { | ||
395 |
5/5✓ Branch 0 taken 737545 times.
✓ Branch 1 taken 346459 times.
✓ Branch 2 taken 2968 times.
✓ Branch 3 taken 128 times.
✓ Branch 4 taken 2 times.
|
1087102 | switch (st->codecpar->codec_type) { |
396 | 737545 | case AVMEDIA_TYPE_VIDEO: | |
397 |
2/2✓ Branch 0 taken 114557 times.
✓ Branch 1 taken 622988 times.
|
737545 | if (s->video_codec_id) |
398 | 114557 | st->codecpar->codec_id = s->video_codec_id; | |
399 | 737545 | break; | |
400 | 346459 | case AVMEDIA_TYPE_AUDIO: | |
401 |
2/2✓ Branch 0 taken 2558 times.
✓ Branch 1 taken 343901 times.
|
346459 | if (s->audio_codec_id) |
402 | 2558 | st->codecpar->codec_id = s->audio_codec_id; | |
403 | 346459 | break; | |
404 | 2968 | case AVMEDIA_TYPE_SUBTITLE: | |
405 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2968 times.
|
2968 | if (s->subtitle_codec_id) |
406 | ✗ | st->codecpar->codec_id = s->subtitle_codec_id; | |
407 | 2968 | break; | |
408 | 128 | case AVMEDIA_TYPE_DATA: | |
409 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 128 times.
|
128 | if (s->data_codec_id) |
410 | ✗ | st->codecpar->codec_id = s->data_codec_id; | |
411 | 128 | break; | |
412 | } | ||
413 | 1087102 | } | |
414 | |||
415 | 22107 | static int probe_codec(AVFormatContext *s, AVStream *st, const AVPacket *pkt) | |
416 | { | ||
417 | 22107 | FormatContextInternal *const fci = ff_fc_internal(s); | |
418 | 22107 | FFStream *const sti = ffstream(st); | |
419 | |||
420 |
2/2✓ Branch 0 taken 15942 times.
✓ Branch 1 taken 6165 times.
|
22107 | if (sti->request_probe > 0) { |
421 | 15942 | AVProbeData *const pd = &sti->probe_data; | |
422 | int end; | ||
423 | 15942 | av_log(s, AV_LOG_DEBUG, "probing stream %d pp:%d\n", st->index, sti->probe_packets); | |
424 | 15942 | --sti->probe_packets; | |
425 | |||
426 |
2/2✓ Branch 0 taken 15931 times.
✓ Branch 1 taken 11 times.
|
15942 | if (pkt) { |
427 | 15931 | uint8_t *new_buf = av_realloc(pd->buf, pd->buf_size+pkt->size+AVPROBE_PADDING_SIZE); | |
428 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 15931 times.
|
15931 | if (!new_buf) { |
429 | ✗ | av_log(s, AV_LOG_WARNING, | |
430 | "Failed to reallocate probe buffer for stream %d\n", | ||
431 | st->index); | ||
432 | ✗ | goto no_packet; | |
433 | } | ||
434 | 15931 | pd->buf = new_buf; | |
435 | 15931 | memcpy(pd->buf + pd->buf_size, pkt->data, pkt->size); | |
436 | 15931 | pd->buf_size += pkt->size; | |
437 | 15931 | memset(pd->buf + pd->buf_size, 0, AVPROBE_PADDING_SIZE); | |
438 | } else { | ||
439 | 11 | no_packet: | |
440 | 11 | sti->probe_packets = 0; | |
441 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
|
11 | if (!pd->buf_size) { |
442 | ✗ | av_log(s, AV_LOG_WARNING, | |
443 | "nothing to probe for stream %d\n", st->index); | ||
444 | } | ||
445 | } | ||
446 | |||
447 |
1/2✓ Branch 0 taken 15942 times.
✗ Branch 1 not taken.
|
31884 | end = fci->raw_packet_buffer_size >= s->probesize || |
448 |
2/2✓ Branch 0 taken 497 times.
✓ Branch 1 taken 15445 times.
|
15942 | sti->probe_packets <= 0; |
449 | |||
450 |
4/4✓ Branch 0 taken 15445 times.
✓ Branch 1 taken 497 times.
✓ Branch 2 taken 2528 times.
✓ Branch 3 taken 12917 times.
|
15942 | if (end || av_log2(pd->buf_size) != av_log2(pd->buf_size - pkt->size)) { |
451 | 3025 | int score = set_codec_from_probe_data(s, st, pd); | |
452 |
4/4✓ Branch 0 taken 3017 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 2999 times.
✓ Branch 3 taken 18 times.
|
3025 | if ( (st->codecpar->codec_id != AV_CODEC_ID_NONE && score > AVPROBE_SCORE_STREAM_RETRY) |
453 |
2/2✓ Branch 0 taken 497 times.
✓ Branch 1 taken 2510 times.
|
3007 | || end) { |
454 | 515 | pd->buf_size = 0; | |
455 | 515 | av_freep(&pd->buf); | |
456 | 515 | sti->request_probe = -1; | |
457 |
1/2✓ Branch 0 taken 515 times.
✗ Branch 1 not taken.
|
515 | if (st->codecpar->codec_id != AV_CODEC_ID_NONE) { |
458 | 515 | av_log(s, AV_LOG_DEBUG, "probed stream %d\n", st->index); | |
459 | } else | ||
460 | ✗ | av_log(s, AV_LOG_WARNING, "probed stream %d failed\n", st->index); | |
461 | } | ||
462 | 3025 | force_codec_ids(s, st); | |
463 | } | ||
464 | } | ||
465 | 22107 | return 0; | |
466 | } | ||
467 | |||
468 | 1084077 | static int update_wrap_reference(AVFormatContext *s, AVStream *st, int stream_index, AVPacket *pkt) | |
469 | { | ||
470 | 1084077 | FFStream *const sti = ffstream(st); | |
471 | 1084077 | int64_t ref = pkt->dts; | |
472 | int pts_wrap_behavior; | ||
473 | int64_t pts_wrap_reference; | ||
474 | AVProgram *first_program; | ||
475 | |||
476 |
2/2✓ Branch 0 taken 882388 times.
✓ Branch 1 taken 201689 times.
|
1084077 | if (ref == AV_NOPTS_VALUE) |
477 | 882388 | ref = pkt->pts; | |
478 |
7/8✓ Branch 0 taken 1016473 times.
✓ Branch 1 taken 67604 times.
✓ Branch 2 taken 48573 times.
✓ Branch 3 taken 967900 times.
✓ Branch 4 taken 300 times.
✓ Branch 5 taken 48273 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 300 times.
|
1084077 | if (sti->pts_wrap_reference != AV_NOPTS_VALUE || st->pts_wrap_bits >= 63 || ref == AV_NOPTS_VALUE || !s->correct_ts_overflow) |
479 | 1083777 | return 0; | |
480 | 300 | ref &= (1LL << st->pts_wrap_bits)-1; | |
481 | |||
482 | // reference time stamp should be 60 s before first time stamp | ||
483 | 300 | pts_wrap_reference = ref - av_rescale(60, st->time_base.den, st->time_base.num); | |
484 | // if first time stamp is not more than 1/8 and 60s before the wrap point, subtract rather than add wrap offset | ||
485 | 601 | pts_wrap_behavior = (ref < (1LL << st->pts_wrap_bits) - (1LL << st->pts_wrap_bits-3)) || | |
486 |
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)) ? |
487 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 299 times.
|
301 | AV_PTS_WRAP_ADD_OFFSET : AV_PTS_WRAP_SUB_OFFSET; |
488 | |||
489 | 300 | first_program = av_find_program_from_stream(s, NULL, stream_index); | |
490 | |||
491 |
2/2✓ Branch 0 taken 194 times.
✓ Branch 1 taken 106 times.
|
300 | if (!first_program) { |
492 | 194 | int default_stream_index = av_find_default_stream_index(s); | |
493 | 194 | FFStream *const default_sti = ffstream(s->streams[default_stream_index]); | |
494 |
2/2✓ Branch 0 taken 183 times.
✓ Branch 1 taken 11 times.
|
194 | if (default_sti->pts_wrap_reference == AV_NOPTS_VALUE) { |
495 |
2/2✓ Branch 0 taken 343 times.
✓ Branch 1 taken 183 times.
|
526 | for (unsigned i = 0; i < s->nb_streams; i++) { |
496 | 343 | FFStream *const sti = ffstream(s->streams[i]); | |
497 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 343 times.
|
343 | if (av_find_program_from_stream(s, NULL, i)) |
498 | ✗ | continue; | |
499 | 343 | sti->pts_wrap_reference = pts_wrap_reference; | |
500 | 343 | sti->pts_wrap_behavior = pts_wrap_behavior; | |
501 | } | ||
502 | } else { | ||
503 | 11 | sti->pts_wrap_reference = default_sti->pts_wrap_reference; | |
504 | 11 | sti->pts_wrap_behavior = default_sti->pts_wrap_behavior; | |
505 | } | ||
506 | } else { | ||
507 | 106 | AVProgram *program = first_program; | |
508 |
2/2✓ Branch 0 taken 106 times.
✓ Branch 1 taken 66 times.
|
172 | while (program) { |
509 |
2/2✓ Branch 0 taken 40 times.
✓ Branch 1 taken 66 times.
|
106 | if (program->pts_wrap_reference != AV_NOPTS_VALUE) { |
510 | 40 | pts_wrap_reference = program->pts_wrap_reference; | |
511 | 40 | pts_wrap_behavior = program->pts_wrap_behavior; | |
512 | 40 | break; | |
513 | } | ||
514 | 66 | program = av_find_program_from_stream(s, program, stream_index); | |
515 | } | ||
516 | |||
517 | // update every program with differing pts_wrap_reference | ||
518 | 106 | program = first_program; | |
519 |
2/2✓ Branch 0 taken 106 times.
✓ Branch 1 taken 106 times.
|
212 | while (program) { |
520 |
2/2✓ Branch 0 taken 66 times.
✓ Branch 1 taken 40 times.
|
106 | if (program->pts_wrap_reference != pts_wrap_reference) { |
521 |
2/2✓ Branch 0 taken 108 times.
✓ Branch 1 taken 66 times.
|
174 | for (unsigned i = 0; i < program->nb_stream_indexes; i++) { |
522 | 108 | FFStream *const sti = ffstream(s->streams[program->stream_index[i]]); | |
523 | 108 | sti->pts_wrap_reference = pts_wrap_reference; | |
524 | 108 | sti->pts_wrap_behavior = pts_wrap_behavior; | |
525 | } | ||
526 | |||
527 | 66 | program->pts_wrap_reference = pts_wrap_reference; | |
528 | 66 | program->pts_wrap_behavior = pts_wrap_behavior; | |
529 | } | ||
530 | 106 | program = av_find_program_from_stream(s, program, stream_index); | |
531 | } | ||
532 | } | ||
533 | 300 | return 1; | |
534 | } | ||
535 | |||
536 | 1084077 | static void update_timestamps(AVFormatContext *s, AVStream *st, AVPacket *pkt) | |
537 | { | ||
538 | 1084077 | FFStream *const sti = ffstream(st); | |
539 | |||
540 |
4/4✓ Branch 1 taken 300 times.
✓ Branch 2 taken 1083777 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 299 times.
|
1084077 | if (update_wrap_reference(s, st, pkt->stream_index, pkt) && sti->pts_wrap_behavior == AV_PTS_WRAP_SUB_OFFSET) { |
541 | // correct first time stamps to negative values | ||
542 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | if (!is_relative(sti->first_dts)) |
543 | 1 | sti->first_dts = wrap_timestamp(st, sti->first_dts); | |
544 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | if (!is_relative(st->start_time)) |
545 | 1 | st->start_time = wrap_timestamp(st, st->start_time); | |
546 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if (!is_relative(sti->cur_dts)) |
547 | ✗ | sti->cur_dts = wrap_timestamp(st, sti->cur_dts); | |
548 | } | ||
549 | |||
550 | 1084077 | pkt->dts = wrap_timestamp(st, pkt->dts); | |
551 | 1084077 | pkt->pts = wrap_timestamp(st, pkt->pts); | |
552 | |||
553 | 1084077 | force_codec_ids(s, st); | |
554 | |||
555 | /* TODO: audio: time filter; video: frame reordering (pts != dts) */ | ||
556 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1084077 times.
|
1084077 | if (s->use_wallclock_as_timestamps) |
557 | ✗ | pkt->dts = pkt->pts = av_rescale_q(av_gettime(), AV_TIME_BASE_Q, st->time_base); | |
558 | 1084077 | } | |
559 | |||
560 | /** | ||
561 | * Handle a new packet and either return it directly if possible and | ||
562 | * allow_passthrough is true or queue the packet (or drop the packet | ||
563 | * if corrupt). | ||
564 | * | ||
565 | * @return < 0 on error, 0 if the packet was passed through, | ||
566 | * 1 if it was queued or dropped | ||
567 | */ | ||
568 | 1084077 | static int handle_new_packet(AVFormatContext *s, AVPacket *pkt, int allow_passthrough) | |
569 | { | ||
570 | 1084077 | FormatContextInternal *const fci = ff_fc_internal(s); | |
571 | AVStream *st; | ||
572 | FFStream *sti; | ||
573 | int err; | ||
574 | |||
575 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1084077 times.
|
1084077 | av_assert0(pkt->stream_index < (unsigned)s->nb_streams && |
576 | "Invalid stream index.\n"); | ||
577 | |||
578 |
2/2✓ Branch 0 taken 200 times.
✓ Branch 1 taken 1083877 times.
|
1084077 | if (pkt->flags & AV_PKT_FLAG_CORRUPT) { |
579 | 400 | av_log(s, AV_LOG_WARNING, | |
580 | "Packet corrupt (stream = %d, dts = %s)%s.\n", | ||
581 | 200 | pkt->stream_index, av_ts2str(pkt->dts), | |
582 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 200 times.
|
200 | s->flags & AVFMT_FLAG_DISCARD_CORRUPT ? ", dropping it" : ""); |
583 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 200 times.
|
200 | if (s->flags & AVFMT_FLAG_DISCARD_CORRUPT) { |
584 | ✗ | av_packet_unref(pkt); | |
585 | ✗ | return 1; | |
586 | } | ||
587 | } | ||
588 | |||
589 | 1084077 | st = s->streams[pkt->stream_index]; | |
590 | 1084077 | sti = ffstream(st); | |
591 | |||
592 | 1084077 | update_timestamps(s, st, pkt); | |
593 | |||
594 |
6/6✓ Branch 0 taken 1068146 times.
✓ Branch 1 taken 15931 times.
✓ Branch 2 taken 1062023 times.
✓ Branch 3 taken 6123 times.
✓ Branch 4 taken 1061981 times.
✓ Branch 5 taken 42 times.
|
1084077 | if (sti->request_probe <= 0 && allow_passthrough && !fci->raw_packet_buffer.head) |
595 | 1061981 | return 0; | |
596 | |||
597 | 22096 | err = avpriv_packet_list_put(&fci->raw_packet_buffer, pkt, NULL, 0); | |
598 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 22096 times.
|
22096 | if (err < 0) { |
599 | ✗ | av_packet_unref(pkt); | |
600 | ✗ | return err; | |
601 | } | ||
602 | |||
603 | 22096 | pkt = &fci->raw_packet_buffer.tail->pkt; | |
604 | 22096 | fci->raw_packet_buffer_size += pkt->size; | |
605 | |||
606 | 22096 | err = probe_codec(s, st, pkt); | |
607 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 22096 times.
|
22096 | if (err < 0) |
608 | ✗ | return err; | |
609 | |||
610 | 22096 | return 1; | |
611 | } | ||
612 | |||
613 | 6123 | int ff_buffer_packet(AVFormatContext *s, AVPacket *pkt) | |
614 | { | ||
615 | 6123 | int err = handle_new_packet(s, pkt, 0); | |
616 | |||
617 | 6123 | return err < 0 ? err : 0; | |
618 | } | ||
619 | |||
620 | 1090581 | int ff_read_packet(AVFormatContext *s, AVPacket *pkt) | |
621 | { | ||
622 | 1090581 | FormatContextInternal *const fci = ff_fc_internal(s); | |
623 | int err; | ||
624 | |||
625 | #if FF_API_INIT_PACKET | ||
626 | FF_DISABLE_DEPRECATION_WARNINGS | ||
627 | 1090581 | pkt->data = NULL; | |
628 | 1090581 | pkt->size = 0; | |
629 | 1090581 | av_init_packet(pkt); | |
630 | FF_ENABLE_DEPRECATION_WARNINGS | ||
631 | #else | ||
632 | av_packet_unref(pkt); | ||
633 | #endif | ||
634 | |||
635 | 22378 | for (;;) { | |
636 | 1112959 | PacketListEntry *pktl = fci->raw_packet_buffer.head; | |
637 | |||
638 |
2/2✓ Branch 0 taken 37612 times.
✓ Branch 1 taken 1075347 times.
|
1112959 | if (pktl) { |
639 | 37612 | AVStream *const st = s->streams[pktl->pkt.stream_index]; | |
640 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 37612 times.
|
37612 | if (fci->raw_packet_buffer_size >= s->probesize) |
641 | ✗ | if ((err = probe_codec(s, st, NULL)) < 0) | |
642 | ✗ | return err; | |
643 |
2/2✓ Branch 1 taken 22143 times.
✓ Branch 2 taken 15469 times.
|
37612 | if (ffstream(st)->request_probe <= 0) { |
644 | 22143 | avpriv_packet_list_get(&fci->raw_packet_buffer, pkt); | |
645 | 22143 | fci->raw_packet_buffer_size -= pkt->size; | |
646 | 22143 | return 0; | |
647 | } | ||
648 | } | ||
649 | |||
650 | 1090816 | err = ffifmt(s->iformat)->read_packet(s, pkt); | |
651 |
2/2✓ Branch 0 taken 12862 times.
✓ Branch 1 taken 1077954 times.
|
1090816 | if (err < 0) { |
652 | 12862 | av_packet_unref(pkt); | |
653 | |||
654 | /* Some demuxers return FFERROR_REDO when they consume | ||
655 | data and discard it (ignored streams, junk, extradata). | ||
656 | We must re-call the demuxer to get the real packet. */ | ||
657 |
2/2✓ Branch 0 taken 6394 times.
✓ Branch 1 taken 6468 times.
|
12862 | if (err == FFERROR_REDO) |
658 | 6394 | continue; | |
659 |
3/4✓ Branch 0 taken 11 times.
✓ Branch 1 taken 6457 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 11 times.
|
6468 | if (!pktl || err == AVERROR(EAGAIN)) |
660 | 6457 | return err; | |
661 |
2/2✓ Branch 0 taken 11 times.
✓ Branch 1 taken 11 times.
|
22 | for (unsigned i = 0; i < s->nb_streams; i++) { |
662 | 11 | AVStream *const st = s->streams[i]; | |
663 | 11 | FFStream *const sti = ffstream(st); | |
664 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
11 | if (sti->probe_packets || sti->request_probe > 0) |
665 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
|
11 | if ((err = probe_codec(s, st, NULL)) < 0) |
666 | ✗ | return err; | |
667 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
|
11 | av_assert0(sti->request_probe <= 0); |
668 | } | ||
669 | 11 | continue; | |
670 | } | ||
671 | |||
672 | 1077954 | err = av_packet_make_refcounted(pkt); | |
673 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1077954 times.
|
1077954 | if (err < 0) { |
674 | ✗ | av_packet_unref(pkt); | |
675 | ✗ | return err; | |
676 | } | ||
677 | |||
678 | 1077954 | err = handle_new_packet(s, pkt, 1); | |
679 |
2/2✓ Branch 0 taken 1061981 times.
✓ Branch 1 taken 15973 times.
|
1077954 | if (err <= 0) /* Error or passthrough */ |
680 | 1061981 | return err; | |
681 | } | ||
682 | } | ||
683 | |||
684 | /** | ||
685 | * Return the frame duration in seconds. Return 0 if not available. | ||
686 | */ | ||
687 | 320596 | static void compute_frame_duration(AVFormatContext *s, int *pnum, int *pden, | |
688 | AVStream *st, AVCodecParserContext *pc, | ||
689 | AVPacket *pkt) | ||
690 | { | ||
691 | 320596 | FFStream *const sti = ffstream(st); | |
692 | 320596 | AVRational codec_framerate = sti->avctx->framerate; | |
693 | int frame_size, sample_rate; | ||
694 | |||
695 | 320596 | *pnum = 0; | |
696 | 320596 | *pden = 0; | |
697 |
3/3✓ Branch 0 taken 218902 times.
✓ Branch 1 taken 100313 times.
✓ Branch 2 taken 1381 times.
|
320596 | switch (st->codecpar->codec_type) { |
698 | 218902 | case AVMEDIA_TYPE_VIDEO: | |
699 |
6/6✓ Branch 0 taken 189154 times.
✓ Branch 1 taken 29748 times.
✓ Branch 2 taken 42282 times.
✓ Branch 3 taken 146872 times.
✓ Branch 4 taken 30826 times.
✓ Branch 5 taken 11456 times.
|
218902 | if (st->r_frame_rate.num && (!pc || !codec_framerate.num)) { |
700 | 177698 | *pnum = st->r_frame_rate.den; | |
701 | 177698 | *pden = st->r_frame_rate.num; | |
702 |
2/2✓ Branch 0 taken 23635 times.
✓ Branch 1 taken 17569 times.
|
41204 | } else if ((s->iformat->flags & AVFMT_NOTIMESTAMPS) && |
703 |
2/2✓ Branch 0 taken 14673 times.
✓ Branch 1 taken 8962 times.
|
23635 | !codec_framerate.num && |
704 |
3/4✓ Branch 0 taken 14671 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 14671 times.
✗ Branch 3 not taken.
|
14673 | st->avg_frame_rate.num && st->avg_frame_rate.den) { |
705 | 14671 | *pnum = st->avg_frame_rate.den; | |
706 | 14671 | *pden = st->avg_frame_rate.num; | |
707 |
2/2✓ Branch 0 taken 9163 times.
✓ Branch 1 taken 17370 times.
|
26533 | } else if (st->time_base.num * 1000LL > st->time_base.den) { |
708 | 9163 | *pnum = st->time_base.num; | |
709 | 9163 | *pden = st->time_base.den; | |
710 |
2/2✓ Branch 0 taken 17345 times.
✓ Branch 1 taken 25 times.
|
17370 | } else if (codec_framerate.den * 1000LL > codec_framerate.num) { |
711 | 52035 | int ticks_per_frame = (sti->codec_desc && | |
712 |
3/4✓ Branch 0 taken 17345 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12080 times.
✓ Branch 3 taken 5265 times.
|
17345 | (sti->codec_desc->props & AV_CODEC_PROP_FIELDS)) ? 2 : 1; |
713 | 17345 | av_reduce(pnum, pden, | |
714 | 17345 | codec_framerate.den, | |
715 | 17345 | codec_framerate.num * (int64_t)ticks_per_frame, | |
716 | INT_MAX); | ||
717 | |||
718 |
4/4✓ Branch 0 taken 15519 times.
✓ Branch 1 taken 1826 times.
✓ Branch 2 taken 11451 times.
✓ Branch 3 taken 4068 times.
|
17345 | if (pc && pc->repeat_pict) { |
719 | 11451 | av_reduce(pnum, pden, | |
720 | 11451 | (*pnum) * (1LL + pc->repeat_pict), | |
721 | 11451 | (*pden), | |
722 | INT_MAX); | ||
723 | } | ||
724 | /* If this codec can be interlaced or progressive then we need | ||
725 | * a parser to compute duration of a packet. Thus if we have | ||
726 | * no parser in such case leave duration undefined. */ | ||
727 |
1/2✓ Branch 0 taken 17345 times.
✗ Branch 1 not taken.
|
17345 | if (sti->codec_desc && |
728 |
3/4✓ Branch 0 taken 12080 times.
✓ Branch 1 taken 5265 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 12080 times.
|
17345 | (sti->codec_desc->props & AV_CODEC_PROP_FIELDS) && !pc) |
729 | ✗ | *pnum = *pden = 0; | |
730 | } | ||
731 | 218902 | break; | |
732 | 100313 | case AVMEDIA_TYPE_AUDIO: | |
733 |
2/2✓ Branch 0 taken 45099 times.
✓ Branch 1 taken 55214 times.
|
100313 | if (sti->avctx_inited) { |
734 | 45099 | frame_size = av_get_audio_frame_duration(sti->avctx, pkt->size); | |
735 | 45099 | sample_rate = sti->avctx->sample_rate; | |
736 | } else { | ||
737 | 55214 | frame_size = av_get_audio_frame_duration2(st->codecpar, pkt->size); | |
738 | 55214 | sample_rate = st->codecpar->sample_rate; | |
739 | } | ||
740 |
4/4✓ Branch 0 taken 95053 times.
✓ Branch 1 taken 5260 times.
✓ Branch 2 taken 95048 times.
✓ Branch 3 taken 5 times.
|
100313 | if (frame_size <= 0 || sample_rate <= 0) |
741 | break; | ||
742 | 95048 | *pnum = frame_size; | |
743 | 95048 | *pden = sample_rate; | |
744 | 95048 | break; | |
745 | 1381 | default: | |
746 | 1381 | break; | |
747 | } | ||
748 | 320596 | } | |
749 | |||
750 | 719033 | static int has_decode_delay_been_guessed(AVStream *st) | |
751 | { | ||
752 | 719033 | FFStream *const sti = ffstream(st); | |
753 |
2/2✓ Branch 0 taken 702067 times.
✓ Branch 1 taken 16966 times.
|
719033 | if (st->codecpar->codec_id != AV_CODEC_ID_H264) return 1; |
754 |
2/2✓ Branch 0 taken 6059 times.
✓ Branch 1 taken 10907 times.
|
16966 | if (!sti->info) // if we have left find_stream_info then nb_decoded_frames won't increase anymore for stream copy |
755 | 6059 | return 1; | |
756 | #if CONFIG_H264_DECODER | ||
757 |
2/2✓ Branch 0 taken 6687 times.
✓ Branch 1 taken 4220 times.
|
10907 | if (sti->avctx->has_b_frames && |
758 |
2/2✓ Branch 1 taken 1416 times.
✓ Branch 2 taken 5271 times.
|
6687 | avpriv_h264_has_num_reorder_frames(sti->avctx) == sti->avctx->has_b_frames) |
759 | 1416 | return 1; | |
760 | #endif | ||
761 |
2/2✓ Branch 0 taken 9303 times.
✓ Branch 1 taken 188 times.
|
9491 | if (sti->avctx->has_b_frames < 3) |
762 | 9303 | return sti->nb_decoded_frames >= 7; | |
763 |
2/2✓ Branch 0 taken 36 times.
✓ Branch 1 taken 152 times.
|
188 | else if (sti->avctx->has_b_frames < 4) |
764 | 36 | return sti->nb_decoded_frames >= 18; | |
765 | else | ||
766 | 152 | return sti->nb_decoded_frames >= 20; | |
767 | } | ||
768 | |||
769 | 36689 | static PacketListEntry *get_next_pkt(AVFormatContext *s, AVStream *st, | |
770 | PacketListEntry *pktl) | ||
771 | { | ||
772 | 36689 | FormatContextInternal *const fci = ff_fc_internal(s); | |
773 | 36689 | FFFormatContext *const si = &fci->fc; | |
774 |
2/2✓ Branch 0 taken 34772 times.
✓ Branch 1 taken 1917 times.
|
36689 | if (pktl->next) |
775 | 34772 | return pktl->next; | |
776 |
2/2✓ Branch 0 taken 1887 times.
✓ Branch 1 taken 30 times.
|
1917 | if (pktl == si->packet_buffer.tail) |
777 | 1887 | return fci->parse_queue.head; | |
778 | 30 | return NULL; | |
779 | } | ||
780 | |||
781 | 521657 | static int64_t select_from_pts_buffer(AVStream *st, int64_t *pts_buffer, int64_t dts) | |
782 | { | ||
783 | 521657 | FFStream *const sti = ffstream(st); | |
784 | 1557721 | int onein_oneout = st->codecpar->codec_id != AV_CODEC_ID_H264 && | |
785 |
4/4✓ Branch 0 taken 514407 times.
✓ Branch 1 taken 7250 times.
✓ Branch 2 taken 513439 times.
✓ Branch 3 taken 968 times.
|
1035096 | st->codecpar->codec_id != AV_CODEC_ID_HEVC && |
786 |
2/2✓ Branch 0 taken 513420 times.
✓ Branch 1 taken 19 times.
|
513439 | st->codecpar->codec_id != AV_CODEC_ID_VVC; |
787 | |||
788 |
2/2✓ Branch 0 taken 8237 times.
✓ Branch 1 taken 513420 times.
|
521657 | if (!onein_oneout) { |
789 | 8237 | int delay = sti->avctx->has_b_frames; | |
790 | |||
791 |
2/2✓ Branch 0 taken 1219 times.
✓ Branch 1 taken 7018 times.
|
8237 | if (dts == AV_NOPTS_VALUE) { |
792 | 1219 | int64_t best_score = INT64_MAX; | |
793 |
2/2✓ Branch 0 taken 1399 times.
✓ Branch 1 taken 1219 times.
|
2618 | for (int i = 0; i < delay; i++) { |
794 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1399 times.
|
1399 | if (sti->pts_reorder_error_count[i]) { |
795 | ✗ | int64_t score = sti->pts_reorder_error[i] / sti->pts_reorder_error_count[i]; | |
796 | ✗ | if (score < best_score) { | |
797 | ✗ | best_score = score; | |
798 | ✗ | dts = pts_buffer[i]; | |
799 | } | ||
800 | } | ||
801 | } | ||
802 | } else { | ||
803 |
2/2✓ Branch 0 taken 11905 times.
✓ Branch 1 taken 7018 times.
|
18923 | for (int i = 0; i < delay; i++) { |
804 |
2/2✓ Branch 0 taken 11108 times.
✓ Branch 1 taken 797 times.
|
11905 | if (pts_buffer[i] != AV_NOPTS_VALUE) { |
805 | 11108 | int64_t diff = FFABS(pts_buffer[i] - dts) | |
806 | 11108 | + (uint64_t)sti->pts_reorder_error[i]; | |
807 | 11108 | diff = FFMAX(diff, sti->pts_reorder_error[i]); | |
808 | 11108 | sti->pts_reorder_error[i] = diff; | |
809 | 11108 | sti->pts_reorder_error_count[i]++; | |
810 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 11105 times.
|
11108 | if (sti->pts_reorder_error_count[i] > 250) { |
811 | 3 | sti->pts_reorder_error[i] >>= 1; | |
812 | 3 | sti->pts_reorder_error_count[i] >>= 1; | |
813 | } | ||
814 | } | ||
815 | } | ||
816 | } | ||
817 | } | ||
818 | |||
819 |
2/2✓ Branch 0 taken 1394 times.
✓ Branch 1 taken 520263 times.
|
521657 | if (dts == AV_NOPTS_VALUE) |
820 | 1394 | dts = pts_buffer[0]; | |
821 | |||
822 | 521657 | return dts; | |
823 | } | ||
824 | |||
825 | /** | ||
826 | * Updates the dts of packets of a stream in pkt_buffer, by re-ordering the pts | ||
827 | * of the packets in a window. | ||
828 | */ | ||
829 | 6089 | static void update_dts_from_pts(AVFormatContext *s, int stream_index, | |
830 | PacketListEntry *pkt_buffer) | ||
831 | { | ||
832 | 6089 | AVStream *const st = s->streams[stream_index]; | |
833 | 6089 | int delay = ffstream(st)->avctx->has_b_frames; | |
834 | |||
835 | int64_t pts_buffer[MAX_REORDER_DELAY+1]; | ||
836 | |||
837 |
2/2✓ Branch 0 taken 103513 times.
✓ Branch 1 taken 6089 times.
|
109602 | for (int i = 0; i < MAX_REORDER_DELAY + 1; i++) |
838 | 103513 | pts_buffer[i] = AV_NOPTS_VALUE; | |
839 | |||
840 |
2/2✓ Branch 1 taken 5506 times.
✓ Branch 2 taken 6089 times.
|
11595 | for (; pkt_buffer; pkt_buffer = get_next_pkt(s, st, pkt_buffer)) { |
841 |
2/2✓ Branch 0 taken 4848 times.
✓ Branch 1 taken 658 times.
|
5506 | if (pkt_buffer->pkt.stream_index != stream_index) |
842 | 4848 | continue; | |
843 | |||
844 |
3/4✓ Branch 0 taken 561 times.
✓ Branch 1 taken 97 times.
✓ Branch 2 taken 561 times.
✗ Branch 3 not taken.
|
658 | if (pkt_buffer->pkt.pts != AV_NOPTS_VALUE && delay <= MAX_REORDER_DELAY) { |
845 | 561 | pts_buffer[0] = pkt_buffer->pkt.pts; | |
846 |
4/4✓ Branch 0 taken 377 times.
✓ Branch 1 taken 396 times.
✓ Branch 2 taken 212 times.
✓ Branch 3 taken 165 times.
|
773 | for (int i = 0; i < delay && pts_buffer[i] > pts_buffer[i + 1]; i++) |
847 | 212 | FFSWAP(int64_t, pts_buffer[i], pts_buffer[i + 1]); | |
848 | |||
849 | 561 | pkt_buffer->pkt.dts = select_from_pts_buffer(st, pts_buffer, pkt_buffer->pkt.dts); | |
850 | } | ||
851 | } | ||
852 | 6089 | } | |
853 | |||
854 | 562983 | static void update_initial_timestamps(AVFormatContext *s, int stream_index, | |
855 | int64_t dts, int64_t pts, AVPacket *pkt) | ||
856 | { | ||
857 | 562983 | FormatContextInternal *const fci = ff_fc_internal(s); | |
858 | 562983 | FFFormatContext *const si = &fci->fc; | |
859 | 562983 | AVStream *const st = s->streams[stream_index]; | |
860 | 562983 | FFStream *const sti = ffstream(st); | |
861 |
2/2✓ Branch 0 taken 183503 times.
✓ Branch 1 taken 379480 times.
|
562983 | PacketListEntry *pktl = si->packet_buffer.head ? si->packet_buffer.head : fci->parse_queue.head; |
862 | |||
863 | uint64_t shift; | ||
864 | |||
865 |
4/4✓ Branch 0 taken 185666 times.
✓ Branch 1 taken 377317 times.
✓ Branch 2 taken 6152 times.
✓ Branch 3 taken 179514 times.
|
562983 | if (sti->first_dts != AV_NOPTS_VALUE || |
866 | 6152 | dts == AV_NOPTS_VALUE || | |
867 |
1/2✓ Branch 0 taken 6152 times.
✗ Branch 1 not taken.
|
6152 | sti->cur_dts == AV_NOPTS_VALUE || |
868 |
1/2✓ Branch 0 taken 6152 times.
✗ Branch 1 not taken.
|
6152 | sti->cur_dts < INT_MIN + RELATIVE_TS_BASE || |
869 |
2/4✓ Branch 0 taken 6152 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6152 times.
|
12304 | dts < INT_MIN + (sti->cur_dts - RELATIVE_TS_BASE) || |
870 | 6152 | is_relative(dts)) | |
871 | 556831 | return; | |
872 | |||
873 | 6152 | sti->first_dts = dts - (sti->cur_dts - RELATIVE_TS_BASE); | |
874 | 6152 | sti->cur_dts = dts; | |
875 | 6152 | shift = (uint64_t)sti->first_dts - RELATIVE_TS_BASE; | |
876 | |||
877 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 6152 times.
|
6152 | if (is_relative(pts)) |
878 | ✗ | pts += shift; | |
879 | |||
880 |
2/2✓ Branch 1 taken 5247 times.
✓ Branch 2 taken 6152 times.
|
11399 | for (PacketListEntry *pktl_it = pktl; pktl_it; pktl_it = get_next_pkt(s, st, pktl_it)) { |
881 |
2/2✓ Branch 0 taken 4777 times.
✓ Branch 1 taken 470 times.
|
5247 | if (pktl_it->pkt.stream_index != stream_index) |
882 | 4777 | continue; | |
883 |
2/2✓ Branch 1 taken 205 times.
✓ Branch 2 taken 265 times.
|
470 | if (is_relative(pktl_it->pkt.pts)) |
884 | 205 | pktl_it->pkt.pts += shift; | |
885 | |||
886 |
2/2✓ Branch 1 taken 233 times.
✓ Branch 2 taken 237 times.
|
470 | if (is_relative(pktl_it->pkt.dts)) |
887 | 233 | pktl_it->pkt.dts += shift; | |
888 | |||
889 |
4/4✓ Branch 0 taken 69 times.
✓ Branch 1 taken 401 times.
✓ Branch 2 taken 41 times.
✓ Branch 3 taken 28 times.
|
470 | if (st->start_time == AV_NOPTS_VALUE && pktl_it->pkt.pts != AV_NOPTS_VALUE) { |
890 | 41 | st->start_time = pktl_it->pkt.pts; | |
891 |
3/4✓ Branch 0 taken 1 times.
✓ Branch 1 taken 40 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
41 | if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && st->codecpar->sample_rate) |
892 | 1 | st->start_time = av_sat_add64(st->start_time, av_rescale_q(sti->skip_samples, (AVRational){1, st->codecpar->sample_rate}, st->time_base)); | |
893 | } | ||
894 | } | ||
895 | |||
896 |
2/2✓ Branch 1 taken 6071 times.
✓ Branch 2 taken 81 times.
|
6152 | if (has_decode_delay_been_guessed(st)) |
897 | 6071 | update_dts_from_pts(s, stream_index, pktl); | |
898 | |||
899 |
2/2✓ Branch 0 taken 1700 times.
✓ Branch 1 taken 4452 times.
|
6152 | if (st->start_time == AV_NOPTS_VALUE) { |
900 |
3/4✓ Branch 0 taken 1225 times.
✓ Branch 1 taken 475 times.
✓ Branch 2 taken 1225 times.
✗ Branch 3 not taken.
|
1700 | if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO || !(pkt->flags & AV_PKT_FLAG_DISCARD)) { |
901 | 1700 | st->start_time = pts; | |
902 | } | ||
903 |
4/4✓ Branch 0 taken 475 times.
✓ Branch 1 taken 1225 times.
✓ Branch 2 taken 391 times.
✓ Branch 3 taken 84 times.
|
1700 | if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && st->codecpar->sample_rate) |
904 | 391 | st->start_time = av_sat_add64(st->start_time, av_rescale_q(sti->skip_samples, (AVRational){1, st->codecpar->sample_rate}, st->time_base)); | |
905 | } | ||
906 | } | ||
907 | |||
908 | 227319 | static void update_initial_durations(AVFormatContext *s, AVStream *st, | |
909 | int stream_index, int64_t duration) | ||
910 | { | ||
911 | 227319 | FormatContextInternal *const fci = ff_fc_internal(s); | |
912 | 227319 | FFFormatContext *const si = &fci->fc; | |
913 | 227319 | FFStream *const sti = ffstream(st); | |
914 |
2/2✓ Branch 0 taken 180195 times.
✓ Branch 1 taken 47124 times.
|
227319 | PacketListEntry *pktl = si->packet_buffer.head ? si->packet_buffer.head : fci->parse_queue.head; |
915 | 227319 | int64_t cur_dts = RELATIVE_TS_BASE; | |
916 | |||
917 |
2/2✓ Branch 0 taken 122531 times.
✓ Branch 1 taken 104788 times.
|
227319 | if (sti->first_dts != AV_NOPTS_VALUE) { |
918 |
2/2✓ Branch 0 taken 118914 times.
✓ Branch 1 taken 3617 times.
|
122531 | if (sti->update_initial_durations_done) |
919 | 118914 | return; | |
920 | 3617 | sti->update_initial_durations_done = 1; | |
921 | 3617 | cur_dts = sti->first_dts; | |
922 |
1/2✓ Branch 1 taken 5433 times.
✗ Branch 2 not taken.
|
5433 | for (; pktl; pktl = get_next_pkt(s, st, pktl)) { |
923 |
2/2✓ Branch 0 taken 3632 times.
✓ Branch 1 taken 1801 times.
|
5433 | if (pktl->pkt.stream_index == stream_index) { |
924 |
2/2✓ Branch 0 taken 3521 times.
✓ Branch 1 taken 111 times.
|
3632 | if (pktl->pkt.pts != pktl->pkt.dts || |
925 |
2/2✓ Branch 0 taken 15 times.
✓ Branch 1 taken 3506 times.
|
3521 | pktl->pkt.dts != AV_NOPTS_VALUE || |
926 |
1/2✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
|
15 | pktl->pkt.duration) |
927 | break; | ||
928 | 15 | cur_dts -= duration; | |
929 | } | ||
930 | } | ||
931 |
3/4✓ Branch 0 taken 3617 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
✓ Branch 3 taken 3584 times.
|
3617 | if (pktl && pktl->pkt.dts != sti->first_dts) { |
932 | 33 | av_log(s, AV_LOG_DEBUG, "first_dts %s not matching first dts %s (pts %s, duration %"PRId64") in the queue\n", | |
933 | 33 | av_ts2str(sti->first_dts), av_ts2str(pktl->pkt.dts), av_ts2str(pktl->pkt.pts), pktl->pkt.duration); | |
934 | 33 | return; | |
935 | } | ||
936 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3584 times.
|
3584 | if (!pktl) { |
937 | ✗ | av_log(s, AV_LOG_DEBUG, "first_dts %s but no packet with dts in the queue\n", av_ts2str(sti->first_dts)); | |
938 | ✗ | return; | |
939 | } | ||
940 |
2/2✓ Branch 0 taken 3563 times.
✓ Branch 1 taken 21 times.
|
3584 | pktl = si->packet_buffer.head ? si->packet_buffer.head : fci->parse_queue.head; |
941 | 3584 | sti->first_dts = cur_dts; | |
942 |
2/2✓ Branch 0 taken 80295 times.
✓ Branch 1 taken 24493 times.
|
104788 | } else if (sti->cur_dts != RELATIVE_TS_BASE) |
943 | 80295 | return; | |
944 | |||
945 |
2/2✓ Branch 1 taken 51554 times.
✓ Branch 2 taken 643 times.
|
52197 | for (; pktl; pktl = get_next_pkt(s, st, pktl)) { |
946 |
2/2✓ Branch 0 taken 23965 times.
✓ Branch 1 taken 27589 times.
|
51554 | if (pktl->pkt.stream_index != stream_index) |
947 | 23965 | continue; | |
948 |
2/2✓ Branch 0 taken 567 times.
✓ Branch 1 taken 27022 times.
|
27589 | if ((pktl->pkt.pts == pktl->pkt.dts || |
949 |
2/2✓ Branch 0 taken 14 times.
✓ Branch 1 taken 553 times.
|
567 | pktl->pkt.pts == AV_NOPTS_VALUE) && |
950 |
2/2✓ Branch 0 taken 3653 times.
✓ Branch 1 taken 23383 times.
|
27036 | (pktl->pkt.dts == AV_NOPTS_VALUE || |
951 |
2/2✓ Branch 0 taken 178 times.
✓ Branch 1 taken 3475 times.
|
3653 | pktl->pkt.dts == sti->first_dts || |
952 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 176 times.
|
178 | pktl->pkt.dts == RELATIVE_TS_BASE) && |
953 |
2/2✓ Branch 0 taken 155 times.
✓ Branch 1 taken 26705 times.
|
26860 | !pktl->pkt.duration && |
954 |
1/2✓ Branch 1 taken 155 times.
✗ Branch 2 not taken.
|
155 | av_sat_add64(cur_dts, duration) == cur_dts + (uint64_t)duration |
955 | ) { | ||
956 | 155 | pktl->pkt.dts = cur_dts; | |
957 |
2/2✓ Branch 0 taken 139 times.
✓ Branch 1 taken 16 times.
|
155 | if (!sti->avctx->has_b_frames) |
958 | 139 | pktl->pkt.pts = cur_dts; | |
959 | 155 | pktl->pkt.duration = duration; | |
960 | } else | ||
961 | break; | ||
962 | 155 | cur_dts = pktl->pkt.dts + pktl->pkt.duration; | |
963 | } | ||
964 |
2/2✓ Branch 0 taken 643 times.
✓ Branch 1 taken 27434 times.
|
28077 | if (!pktl) |
965 | 643 | sti->cur_dts = cur_dts; | |
966 | } | ||
967 | |||
968 | 568810 | static void compute_pkt_fields(AVFormatContext *s, AVStream *st, | |
969 | AVCodecParserContext *pc, AVPacket *pkt, | ||
970 | int64_t next_dts, int64_t next_pts) | ||
971 | { | ||
972 | 568810 | FormatContextInternal *const fci = ff_fc_internal(s); | |
973 | 568810 | FFFormatContext *const si = &fci->fc; | |
974 | 568810 | FFStream *const sti = ffstream(st); | |
975 | int num, den, presentation_delayed, delay; | ||
976 | int64_t offset; | ||
977 | AVRational duration; | ||
978 | 1672504 | int onein_oneout = st->codecpar->codec_id != AV_CODEC_ID_H264 && | |
979 |
4/4✓ Branch 0 taken 534884 times.
✓ Branch 1 taken 33926 times.
✓ Branch 2 taken 521875 times.
✓ Branch 3 taken 13009 times.
|
1090685 | st->codecpar->codec_id != AV_CODEC_ID_HEVC && |
980 |
2/2✓ Branch 0 taken 518829 times.
✓ Branch 1 taken 3046 times.
|
521875 | st->codecpar->codec_id != AV_CODEC_ID_VVC; |
981 | |||
982 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 568810 times.
|
568810 | if (s->flags & AVFMT_FLAG_NOFILLIN) |
983 | ✗ | return; | |
984 | |||
985 |
4/4✓ Branch 0 taken 242953 times.
✓ Branch 1 taken 325857 times.
✓ Branch 2 taken 73355 times.
✓ Branch 3 taken 169598 times.
|
568810 | if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && pkt->dts != AV_NOPTS_VALUE) { |
986 |
4/4✓ Branch 0 taken 43260 times.
✓ Branch 1 taken 30095 times.
✓ Branch 2 taken 42097 times.
✓ Branch 3 taken 1163 times.
|
73355 | if (pkt->dts == pkt->pts && sti->last_dts_for_order_check != AV_NOPTS_VALUE) { |
987 |
2/2✓ Branch 0 taken 42088 times.
✓ Branch 1 taken 9 times.
|
42097 | if (sti->last_dts_for_order_check <= pkt->dts) { |
988 | 42088 | sti->dts_ordered++; | |
989 | } else { | ||
990 |
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, |
991 | "DTS %"PRIi64" < %"PRIi64" out of order\n", | ||
992 | pkt->dts, | ||
993 | sti->last_dts_for_order_check); | ||
994 | 9 | sti->dts_misordered++; | |
995 | } | ||
996 |
2/2✓ Branch 0 taken 29 times.
✓ Branch 1 taken 42068 times.
|
42097 | if (sti->dts_ordered + sti->dts_misordered > 250) { |
997 | 29 | sti->dts_ordered >>= 1; | |
998 | 29 | sti->dts_misordered >>= 1; | |
999 | } | ||
1000 | } | ||
1001 | |||
1002 | 73355 | sti->last_dts_for_order_check = pkt->dts; | |
1003 |
4/4✓ Branch 0 taken 37 times.
✓ Branch 1 taken 73318 times.
✓ Branch 2 taken 20 times.
✓ Branch 3 taken 17 times.
|
73355 | if (sti->dts_ordered < 8 * sti->dts_misordered && pkt->dts == pkt->pts) |
1004 | 20 | pkt->dts = AV_NOPTS_VALUE; | |
1005 | } | ||
1006 | |||
1007 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 568810 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
568810 | if ((s->flags & AVFMT_FLAG_IGNDTS) && pkt->pts != AV_NOPTS_VALUE) |
1008 | ✗ | pkt->dts = AV_NOPTS_VALUE; | |
1009 | |||
1010 |
4/4✓ Branch 0 taken 186941 times.
✓ Branch 1 taken 381869 times.
✓ Branch 2 taken 27734 times.
✓ Branch 3 taken 159207 times.
|
568810 | if (pc && pc->pict_type == AV_PICTURE_TYPE_B |
1011 |
2/2✓ Branch 0 taken 81 times.
✓ Branch 1 taken 27653 times.
|
27734 | && !sti->avctx->has_b_frames) |
1012 | //FIXME Set low_delay = 0 when has_b_frames = 1 | ||
1013 | 81 | sti->avctx->has_b_frames = 1; | |
1014 | |||
1015 | /* do we have a video B-frame ? */ | ||
1016 | 568810 | delay = sti->avctx->has_b_frames; | |
1017 | 568810 | presentation_delayed = 0; | |
1018 | |||
1019 | /* XXX: need has_b_frame, but cannot get it if the codec is | ||
1020 | * not initialized */ | ||
1021 |
4/4✓ Branch 0 taken 46560 times.
✓ Branch 1 taken 522250 times.
✓ Branch 2 taken 42525 times.
✓ Branch 3 taken 4035 times.
|
568810 | if (delay && |
1022 |
2/2✓ Branch 0 taken 14791 times.
✓ Branch 1 taken 27734 times.
|
42525 | pc && pc->pict_type != AV_PICTURE_TYPE_B) |
1023 | 14791 | presentation_delayed = 1; | |
1024 | |||
1025 |
4/4✓ Branch 0 taken 339454 times.
✓ Branch 1 taken 229356 times.
✓ Branch 2 taken 167630 times.
✓ Branch 3 taken 171824 times.
|
568810 | if (pkt->pts != AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE && |
1026 |
3/4✓ Branch 0 taken 27452 times.
✓ Branch 1 taken 140178 times.
✓ Branch 2 taken 27452 times.
✗ Branch 3 not taken.
|
167630 | st->pts_wrap_bits < 63 && pkt->dts > INT64_MIN + (1LL << st->pts_wrap_bits) && |
1027 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 27452 times.
|
27452 | pkt->dts - (1LL << (st->pts_wrap_bits - 1)) > pkt->pts) { |
1028 | ✗ | if (is_relative(sti->cur_dts) || pkt->dts - (1LL<<(st->pts_wrap_bits - 1)) > sti->cur_dts) { | |
1029 | ✗ | pkt->dts -= 1LL << st->pts_wrap_bits; | |
1030 | } else | ||
1031 | ✗ | pkt->pts += 1LL << st->pts_wrap_bits; | |
1032 | } | ||
1033 | |||
1034 | /* Some MPEG-2 in MPEG-PS lack dts (issue #171 / input_file.mpg). | ||
1035 | * We take the conservative approach and discard both. | ||
1036 | * Note: If this is misbehaving for an H.264 file, then possibly | ||
1037 | * presentation_delayed is not set correctly. */ | ||
1038 |
4/4✓ Branch 0 taken 30110 times.
✓ Branch 1 taken 538700 times.
✓ Branch 2 taken 24943 times.
✓ Branch 3 taken 5167 times.
|
568810 | if (delay == 1 && pkt->dts == pkt->pts && |
1039 |
4/4✓ Branch 0 taken 6135 times.
✓ Branch 1 taken 18808 times.
✓ Branch 2 taken 628 times.
✓ Branch 3 taken 5507 times.
|
24943 | pkt->dts != AV_NOPTS_VALUE && presentation_delayed) { |
1040 | 628 | av_log(s, AV_LOG_DEBUG, "invalid dts/pts combination %"PRIi64"\n", pkt->dts); | |
1041 |
2/2✓ Branch 0 taken 293 times.
✓ Branch 1 taken 335 times.
|
628 | if ( strcmp(s->iformat->name, "mov,mp4,m4a,3gp,3g2,mj2") |
1042 |
2/2✓ Branch 0 taken 22 times.
✓ Branch 1 taken 271 times.
|
293 | && strcmp(s->iformat->name, "flv")) // otherwise we discard correct timestamps for vc1-wmapro.ism |
1043 | 22 | pkt->dts = AV_NOPTS_VALUE; | |
1044 | } | ||
1045 | |||
1046 | 568810 | duration = av_mul_q((AVRational) {pkt->duration, 1}, st->time_base); | |
1047 |
2/2✓ Branch 0 taken 317791 times.
✓ Branch 1 taken 251019 times.
|
568810 | if (pkt->duration <= 0) { |
1048 | 317791 | compute_frame_duration(s, &num, &den, st, pc, pkt); | |
1049 |
3/4✓ Branch 0 taken 308511 times.
✓ Branch 1 taken 9280 times.
✓ Branch 2 taken 308511 times.
✗ Branch 3 not taken.
|
317791 | if (den && num) { |
1050 | 308511 | duration = (AVRational) {num, den}; | |
1051 | 308511 | pkt->duration = av_rescale_rnd(1, | |
1052 | 308511 | num * (int64_t) st->time_base.den, | |
1053 | 308511 | den * (int64_t) st->time_base.num, | |
1054 | AV_ROUND_DOWN); | ||
1055 | } | ||
1056 | } | ||
1057 | |||
1058 |
6/6✓ Branch 0 taken 556708 times.
✓ Branch 1 taken 12102 times.
✓ Branch 2 taken 376513 times.
✓ Branch 3 taken 180195 times.
✓ Branch 4 taken 47124 times.
✓ Branch 5 taken 329389 times.
|
568810 | if (pkt->duration > 0 && (si->packet_buffer.head || fci->parse_queue.head)) |
1059 | 227319 | update_initial_durations(s, st, pkt->stream_index, pkt->duration); | |
1060 | |||
1061 | /* Correct timestamps with byte offset if demuxers only have timestamps | ||
1062 | * on packet boundaries */ | ||
1063 |
5/6✓ Branch 0 taken 186941 times.
✓ Branch 1 taken 381869 times.
✓ Branch 2 taken 960 times.
✓ Branch 3 taken 185981 times.
✓ Branch 4 taken 960 times.
✗ Branch 5 not taken.
|
568810 | if (pc && sti->need_parsing == AVSTREAM_PARSE_TIMESTAMPS && pkt->size) { |
1064 | /* this will estimate bitrate based on this frame's duration and size */ | ||
1065 | 960 | offset = av_rescale(pc->offset, pkt->duration, pkt->size); | |
1066 |
2/2✓ Branch 0 taken 575 times.
✓ Branch 1 taken 385 times.
|
960 | if (pkt->pts != AV_NOPTS_VALUE) |
1067 | 575 | pkt->pts += offset; | |
1068 |
2/2✓ Branch 0 taken 121 times.
✓ Branch 1 taken 839 times.
|
960 | if (pkt->dts != AV_NOPTS_VALUE) |
1069 | 121 | pkt->dts += offset; | |
1070 | } | ||
1071 | |||
1072 | /* This may be redundant, but it should not hurt. */ | ||
1073 |
2/2✓ Branch 0 taken 197833 times.
✓ Branch 1 taken 370977 times.
|
568810 | if (pkt->dts != AV_NOPTS_VALUE && |
1074 |
2/2✓ Branch 0 taken 167608 times.
✓ Branch 1 taken 30225 times.
|
197833 | pkt->pts != AV_NOPTS_VALUE && |
1075 |
2/2✓ Branch 0 taken 5164 times.
✓ Branch 1 taken 162444 times.
|
167608 | pkt->pts > pkt->dts) |
1076 | 5164 | presentation_delayed = 1; | |
1077 | |||
1078 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 568810 times.
|
568810 | if (s->debug & FF_FDEBUG_TS) |
1079 | ✗ | av_log(s, AV_LOG_DEBUG, | |
1080 | "IN delayed:%d pts:%s, dts:%s cur_dts:%s st:%d pc:%p duration:%"PRId64" delay:%d onein_oneout:%d\n", | ||
1081 | ✗ | presentation_delayed, av_ts2str(pkt->pts), av_ts2str(pkt->dts), av_ts2str(sti->cur_dts), | |
1082 | pkt->stream_index, pc, pkt->duration, delay, onein_oneout); | ||
1083 | |||
1084 | /* Interpolate PTS and DTS if they are not present. We skip H264 | ||
1085 | * currently because delay and has_b_frames are not reliably set. */ | ||
1086 |
8/8✓ Branch 0 taken 46560 times.
✓ Branch 1 taken 522250 times.
✓ Branch 2 taken 30110 times.
✓ Branch 3 taken 16450 times.
✓ Branch 4 taken 26654 times.
✓ Branch 5 taken 3456 times.
✓ Branch 6 taken 515386 times.
✓ Branch 7 taken 33518 times.
|
568810 | if ((delay == 0 || (delay == 1 && pc)) && |
1087 | onein_oneout) { | ||
1088 |
2/2✓ Branch 0 taken 5463 times.
✓ Branch 1 taken 509923 times.
|
515386 | if (presentation_delayed) { |
1089 | /* DTS = decompression timestamp */ | ||
1090 | /* PTS = presentation timestamp */ | ||
1091 |
2/2✓ Branch 0 taken 2848 times.
✓ Branch 1 taken 2615 times.
|
5463 | if (pkt->dts == AV_NOPTS_VALUE) |
1092 | 2848 | pkt->dts = sti->last_IP_pts; | |
1093 | 5463 | update_initial_timestamps(s, pkt->stream_index, pkt->dts, pkt->pts, pkt); | |
1094 |
2/2✓ Branch 0 taken 2509 times.
✓ Branch 1 taken 2954 times.
|
5463 | if (pkt->dts == AV_NOPTS_VALUE) |
1095 | 2509 | pkt->dts = sti->cur_dts; | |
1096 | |||
1097 | /* This is tricky: the dts must be incremented by the duration | ||
1098 | * of the frame we are displaying, i.e. the last I- or P-frame. */ | ||
1099 |
3/4✓ Branch 0 taken 238 times.
✓ Branch 1 taken 5225 times.
✓ Branch 2 taken 238 times.
✗ Branch 3 not taken.
|
5463 | if (sti->last_IP_duration == 0 && (uint64_t)pkt->duration <= INT32_MAX) |
1100 | 238 | sti->last_IP_duration = pkt->duration; | |
1101 |
1/2✓ Branch 0 taken 5463 times.
✗ Branch 1 not taken.
|
5463 | if (pkt->dts != AV_NOPTS_VALUE) |
1102 | 5463 | sti->cur_dts = av_sat_add64(pkt->dts, sti->last_IP_duration); | |
1103 |
1/2✓ Branch 0 taken 5463 times.
✗ Branch 1 not taken.
|
5463 | if (pkt->dts != AV_NOPTS_VALUE && |
1104 |
2/2✓ Branch 0 taken 3232 times.
✓ Branch 1 taken 2231 times.
|
5463 | pkt->pts == AV_NOPTS_VALUE && |
1105 |
2/2✓ Branch 0 taken 3230 times.
✓ Branch 1 taken 2 times.
|
3232 | sti->last_IP_duration > 0 && |
1106 |
4/4✓ Branch 0 taken 740 times.
✓ Branch 1 taken 2490 times.
✓ Branch 2 taken 734 times.
✓ Branch 3 taken 6 times.
|
3230 | ((uint64_t)sti->cur_dts - (uint64_t)next_dts + 1) <= 2 && |
1107 |
2/2✓ Branch 0 taken 11 times.
✓ Branch 1 taken 723 times.
|
734 | next_dts != next_pts && |
1108 | next_pts != AV_NOPTS_VALUE) | ||
1109 | 11 | pkt->pts = next_dts; | |
1110 | |||
1111 |
1/2✓ Branch 0 taken 5463 times.
✗ Branch 1 not taken.
|
5463 | if ((uint64_t)pkt->duration <= INT32_MAX) |
1112 | 5463 | sti->last_IP_duration = pkt->duration; | |
1113 | 5463 | sti->last_IP_pts = pkt->pts; | |
1114 | /* Cannot compute PTS if not present (we can compute it only | ||
1115 | * by knowing the future. */ | ||
1116 |
2/2✓ Branch 0 taken 184644 times.
✓ Branch 1 taken 325279 times.
|
509923 | } else if (pkt->pts != AV_NOPTS_VALUE || |
1117 |
2/2✓ Branch 0 taken 155221 times.
✓ Branch 1 taken 29423 times.
|
184644 | pkt->dts != AV_NOPTS_VALUE || |
1118 |
2/2✓ Branch 0 taken 152837 times.
✓ Branch 1 taken 2384 times.
|
155221 | pkt->duration > 0 ) { |
1119 | |||
1120 | /* presentation is not delayed : PTS and DTS are the same */ | ||
1121 |
2/2✓ Branch 0 taken 182260 times.
✓ Branch 1 taken 325279 times.
|
507539 | if (pkt->pts == AV_NOPTS_VALUE) |
1122 | 182260 | pkt->pts = pkt->dts; | |
1123 | 507539 | update_initial_timestamps(s, pkt->stream_index, pkt->pts, | |
1124 | pkt->pts, pkt); | ||
1125 |
2/2✓ Branch 0 taken 152837 times.
✓ Branch 1 taken 354702 times.
|
507539 | if (pkt->pts == AV_NOPTS_VALUE) |
1126 | 152837 | pkt->pts = sti->cur_dts; | |
1127 | 507539 | pkt->dts = pkt->pts; | |
1128 |
3/4✓ Branch 0 taken 507539 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 507531 times.
✓ Branch 3 taken 8 times.
|
507539 | if (pkt->pts != AV_NOPTS_VALUE && duration.num >= 0) |
1129 | 507531 | sti->cur_dts = av_add_stable(st->time_base, pkt->pts, duration, 1); | |
1130 | } | ||
1131 | } | ||
1132 | |||
1133 |
3/4✓ Branch 0 taken 521725 times.
✓ Branch 1 taken 47085 times.
✓ Branch 2 taken 521725 times.
✗ Branch 3 not taken.
|
568810 | if (pkt->pts != AV_NOPTS_VALUE && delay <= MAX_REORDER_DELAY) { |
1134 | 521725 | sti->pts_buffer[0] = pkt->pts; | |
1135 |
4/4✓ Branch 0 taken 22176 times.
✓ Branch 1 taken 516420 times.
✓ Branch 2 taken 16871 times.
✓ Branch 3 taken 5305 times.
|
538596 | for (int i = 0; i < delay && sti->pts_buffer[i] > sti->pts_buffer[i + 1]; i++) |
1136 | 16871 | FFSWAP(int64_t, sti->pts_buffer[i], sti->pts_buffer[i + 1]); | |
1137 | |||
1138 |
2/2✓ Branch 1 taken 521096 times.
✓ Branch 2 taken 629 times.
|
521725 | if (has_decode_delay_been_guessed(st)) |
1139 | 521096 | pkt->dts = select_from_pts_buffer(st, sti->pts_buffer, pkt->dts); | |
1140 | } | ||
1141 | // We skipped it above so we try here. | ||
1142 |
2/2✓ Branch 0 taken 49981 times.
✓ Branch 1 taken 518829 times.
|
568810 | if (!onein_oneout) |
1143 | // This should happen on the first packet | ||
1144 | 49981 | update_initial_timestamps(s, pkt->stream_index, pkt->dts, pkt->pts, pkt); | |
1145 |
2/2✓ Branch 0 taken 8027 times.
✓ Branch 1 taken 560783 times.
|
568810 | if (pkt->dts > sti->cur_dts) |
1146 | 8027 | sti->cur_dts = pkt->dts; | |
1147 | |||
1148 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 568810 times.
|
568810 | if (s->debug & FF_FDEBUG_TS) |
1149 | ✗ | av_log(s, AV_LOG_DEBUG, "OUTdelayed:%d/%d pts:%s, dts:%s cur_dts:%s st:%d (%d)\n", | |
1150 | ✗ | presentation_delayed, delay, av_ts2str(pkt->pts), av_ts2str(pkt->dts), av_ts2str(sti->cur_dts), st->index, st->id); | |
1151 | |||
1152 | /* update flags */ | ||
1153 |
4/4✓ Branch 0 taken 568725 times.
✓ Branch 1 taken 85 times.
✓ Branch 3 taken 372814 times.
✓ Branch 4 taken 195911 times.
|
568810 | if (st->codecpar->codec_type == AVMEDIA_TYPE_DATA || ff_is_intra_only(st->codecpar->codec_id)) |
1154 | 372899 | pkt->flags |= AV_PKT_FLAG_KEY; | |
1155 | } | ||
1156 | |||
1157 | /** | ||
1158 | * Parse a packet, add all split parts to parse_queue. | ||
1159 | * | ||
1160 | * @param pkt Packet to parse; must not be NULL. | ||
1161 | * @param flush Indicates whether to flush. If set, pkt must be blank. | ||
1162 | */ | ||
1163 | 697642 | static int parse_packet(AVFormatContext *s, AVPacket *pkt, | |
1164 | int stream_index, int flush) | ||
1165 | { | ||
1166 | 697642 | FormatContextInternal *const fci = ff_fc_internal(s); | |
1167 | 697642 | FFFormatContext *const si = &fci->fc; | |
1168 | 697642 | AVPacket *out_pkt = si->parse_pkt; | |
1169 | 697642 | AVStream *st = s->streams[stream_index]; | |
1170 | 697642 | FFStream *const sti = ffstream(st); | |
1171 | 697642 | const uint8_t *data = pkt->data; | |
1172 | 697642 | int size = pkt->size; | |
1173 | 697642 | int ret = 0, got_output = flush; | |
1174 | |||
1175 |
5/6✓ Branch 0 taken 2236 times.
✓ Branch 1 taken 695406 times.
✓ Branch 2 taken 164 times.
✓ Branch 3 taken 2072 times.
✓ Branch 4 taken 164 times.
✗ Branch 5 not taken.
|
697642 | if (!size && !flush && sti->parser->flags & PARSER_FLAG_COMPLETE_FRAMES) { |
1176 | // preserve 0-size sync packets | ||
1177 | 164 | compute_pkt_fields(s, st, sti->parser, pkt, pkt->dts, pkt->pts); | |
1178 | |||
1179 | // Theora has valid 0-sized packets that need to be output | ||
1180 |
2/2✓ Branch 0 taken 148 times.
✓ Branch 1 taken 16 times.
|
164 | if (st->codecpar->codec_id == AV_CODEC_ID_THEORA) { |
1181 | 148 | ret = avpriv_packet_list_put(&fci->parse_queue, | |
1182 | pkt, NULL, 0); | ||
1183 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 148 times.
|
148 | if (ret < 0) |
1184 | ✗ | goto fail; | |
1185 | } | ||
1186 | } | ||
1187 | |||
1188 |
6/6✓ Branch 0 taken 811465 times.
✓ Branch 1 taken 700875 times.
✓ Branch 2 taken 5305 times.
✓ Branch 3 taken 695570 times.
✓ Branch 4 taken 3233 times.
✓ Branch 5 taken 2072 times.
|
1512340 | while (size > 0 || (flush && got_output)) { |
1189 | 814698 | int64_t next_pts = pkt->pts; | |
1190 | 814698 | int64_t next_dts = pkt->dts; | |
1191 | int len; | ||
1192 | |||
1193 | 814698 | len = av_parser_parse2(sti->parser, sti->avctx, | |
1194 | &out_pkt->data, &out_pkt->size, data, size, | ||
1195 | pkt->pts, pkt->dts, pkt->pos); | ||
1196 | |||
1197 | 814698 | pkt->pts = pkt->dts = AV_NOPTS_VALUE; | |
1198 | 814698 | pkt->pos = -1; | |
1199 | /* increment read pointer */ | ||
1200 | av_assert1(data || !len); | ||
1201 |
2/2✓ Branch 0 taken 801800 times.
✓ Branch 1 taken 12898 times.
|
814698 | data = len ? data + len : data; |
1202 | 814698 | size -= len; | |
1203 | |||
1204 | 814698 | got_output = !!out_pkt->size; | |
1205 | |||
1206 |
2/2✓ Branch 0 taken 627921 times.
✓ Branch 1 taken 186777 times.
|
814698 | if (!out_pkt->size) |
1207 | 627921 | continue; | |
1208 | |||
1209 |
4/4✓ Branch 0 taken 185616 times.
✓ Branch 1 taken 1161 times.
✓ Branch 2 taken 69205 times.
✓ Branch 3 taken 116411 times.
|
186777 | if (pkt->buf && out_pkt->data == pkt->data) { |
1210 | /* reference pkt->buf only when out_pkt->data is guaranteed to point | ||
1211 | * to data in it and not in the parser's internal buffer. */ | ||
1212 | /* XXX: Ensure this is the case with all parsers when sti->parser->flags | ||
1213 | * is PARSER_FLAG_COMPLETE_FRAMES and check for that instead? */ | ||
1214 | 69205 | out_pkt->buf = av_buffer_ref(pkt->buf); | |
1215 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 69205 times.
|
69205 | if (!out_pkt->buf) { |
1216 | ✗ | ret = AVERROR(ENOMEM); | |
1217 | ✗ | goto fail; | |
1218 | } | ||
1219 | } else { | ||
1220 | 117572 | ret = av_packet_make_refcounted(out_pkt); | |
1221 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 117572 times.
|
117572 | if (ret < 0) |
1222 | ✗ | goto fail; | |
1223 | } | ||
1224 | |||
1225 |
2/2✓ Branch 0 taken 9166 times.
✓ Branch 1 taken 177611 times.
|
186777 | if (pkt->side_data) { |
1226 | 9166 | out_pkt->side_data = pkt->side_data; | |
1227 | 9166 | out_pkt->side_data_elems = pkt->side_data_elems; | |
1228 | 9166 | pkt->side_data = NULL; | |
1229 | 9166 | pkt->side_data_elems = 0; | |
1230 | } | ||
1231 | |||
1232 | /* set the duration */ | ||
1233 |
2/2✓ Branch 0 taken 49396 times.
✓ Branch 1 taken 137381 times.
|
186777 | out_pkt->duration = (sti->parser->flags & PARSER_FLAG_COMPLETE_FRAMES) ? pkt->duration : 0; |
1234 |
2/2✓ Branch 0 taken 108862 times.
✓ Branch 1 taken 77915 times.
|
186777 | if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) { |
1235 |
2/2✓ Branch 0 taken 108756 times.
✓ Branch 1 taken 106 times.
|
108862 | if (sti->avctx->sample_rate > 0) { |
1236 | 108756 | out_pkt->duration = | |
1237 | 108756 | av_rescale_q_rnd(sti->parser->duration, | |
1238 | 108756 | (AVRational) { 1, sti->avctx->sample_rate }, | |
1239 | st->time_base, | ||
1240 | AV_ROUND_DOWN); | ||
1241 | } | ||
1242 |
2/2✓ Branch 0 taken 1822 times.
✓ Branch 1 taken 76093 times.
|
77915 | } else if (st->codecpar->codec_id == AV_CODEC_ID_GIF) { |
1243 |
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 && |
1244 |
1/2✓ Branch 0 taken 1822 times.
✗ Branch 1 not taken.
|
1822 | sti->parser->duration) { |
1245 | 1822 | out_pkt->duration = sti->parser->duration; | |
1246 | } | ||
1247 | } | ||
1248 | |||
1249 | 186777 | out_pkt->stream_index = st->index; | |
1250 | 186777 | out_pkt->pts = sti->parser->pts; | |
1251 | 186777 | out_pkt->dts = sti->parser->dts; | |
1252 | 186777 | out_pkt->pos = sti->parser->pos; | |
1253 | 186777 | out_pkt->flags |= pkt->flags & (AV_PKT_FLAG_DISCARD | AV_PKT_FLAG_CORRUPT); | |
1254 | |||
1255 |
2/2✓ Branch 0 taken 114535 times.
✓ Branch 1 taken 72242 times.
|
186777 | if (sti->need_parsing == AVSTREAM_PARSE_FULL_RAW) |
1256 | 114535 | out_pkt->pos = sti->parser->frame_offset; | |
1257 | |||
1258 |
2/2✓ Branch 0 taken 175012 times.
✓ Branch 1 taken 11765 times.
|
186777 | if (sti->parser->key_frame == 1 || |
1259 |
2/2✓ Branch 0 taken 93701 times.
✓ Branch 1 taken 81311 times.
|
175012 | (sti->parser->key_frame == -1 && |
1260 |
2/2✓ Branch 0 taken 81798 times.
✓ Branch 1 taken 11903 times.
|
93701 | sti->parser->pict_type == AV_PICTURE_TYPE_I)) |
1261 | 93563 | out_pkt->flags |= AV_PKT_FLAG_KEY; | |
1262 | |||
1263 |
6/6✓ Branch 0 taken 93701 times.
✓ Branch 1 taken 93076 times.
✓ Branch 2 taken 317 times.
✓ Branch 3 taken 93384 times.
✓ Branch 4 taken 288 times.
✓ Branch 5 taken 29 times.
|
186777 | if (sti->parser->key_frame == -1 && sti->parser->pict_type ==AV_PICTURE_TYPE_NONE && (pkt->flags&AV_PKT_FLAG_KEY)) |
1264 | 288 | out_pkt->flags |= AV_PKT_FLAG_KEY; | |
1265 | |||
1266 | 186777 | compute_pkt_fields(s, st, sti->parser, out_pkt, next_dts, next_pts); | |
1267 | |||
1268 | 186777 | ret = avpriv_packet_list_put(&fci->parse_queue, | |
1269 | out_pkt, NULL, 0); | ||
1270 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 186777 times.
|
186777 | if (ret < 0) |
1271 | ✗ | goto fail; | |
1272 | } | ||
1273 | |||
1274 | /* end of the stream => close and free the parser */ | ||
1275 |
2/2✓ Branch 0 taken 695570 times.
✓ Branch 1 taken 2072 times.
|
697642 | if (flush) { |
1276 | 2072 | av_parser_close(sti->parser); | |
1277 | 2072 | sti->parser = NULL; | |
1278 | } | ||
1279 | |||
1280 | 695570 | fail: | |
1281 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 697642 times.
|
697642 | if (ret < 0) |
1282 | ✗ | av_packet_unref(out_pkt); | |
1283 | 697642 | av_packet_unref(pkt); | |
1284 | 697642 | return ret; | |
1285 | } | ||
1286 | |||
1287 | 8774 | static int64_t ts_to_samples(AVStream *st, int64_t ts) | |
1288 | { | ||
1289 | 8774 | return av_rescale(ts, st->time_base.num * st->codecpar->sample_rate, st->time_base.den); | |
1290 | } | ||
1291 | |||
1292 | 8249 | static int codec_close(FFStream *sti) | |
1293 | { | ||
1294 | 8249 | AVCodecContext *avctx_new = NULL; | |
1295 | 8249 | AVCodecParameters *par_tmp = NULL; | |
1296 | int ret; | ||
1297 | |||
1298 | 8249 | avctx_new = avcodec_alloc_context3(sti->avctx->codec); | |
1299 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8249 times.
|
8249 | if (!avctx_new) { |
1300 | ✗ | ret = AVERROR(ENOMEM); | |
1301 | ✗ | goto fail; | |
1302 | } | ||
1303 | |||
1304 | 8249 | par_tmp = avcodec_parameters_alloc(); | |
1305 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8249 times.
|
8249 | if (!par_tmp) { |
1306 | ✗ | ret = AVERROR(ENOMEM); | |
1307 | ✗ | goto fail; | |
1308 | } | ||
1309 | |||
1310 | 8249 | ret = avcodec_parameters_from_context(par_tmp, sti->avctx); | |
1311 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8249 times.
|
8249 | if (ret < 0) |
1312 | ✗ | goto fail; | |
1313 | |||
1314 | 8249 | ret = avcodec_parameters_to_context(avctx_new, par_tmp); | |
1315 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8249 times.
|
8249 | if (ret < 0) |
1316 | ✗ | goto fail; | |
1317 | |||
1318 | 8249 | avctx_new->pkt_timebase = sti->avctx->pkt_timebase; | |
1319 | |||
1320 | #if FF_API_TICKS_PER_FRAME | ||
1321 | FF_DISABLE_DEPRECATION_WARNINGS | ||
1322 | 8249 | avctx_new->ticks_per_frame = sti->avctx->ticks_per_frame; | |
1323 | FF_ENABLE_DEPRECATION_WARNINGS | ||
1324 | #endif | ||
1325 | |||
1326 | 8249 | avcodec_free_context(&sti->avctx); | |
1327 | 8249 | sti->avctx = avctx_new; | |
1328 | |||
1329 | 8249 | avctx_new = NULL; | |
1330 | 8249 | ret = 0; | |
1331 | |||
1332 | 8249 | fail: | |
1333 | 8249 | avcodec_free_context(&avctx_new); | |
1334 | 8249 | avcodec_parameters_free(&par_tmp); | |
1335 | |||
1336 | 8249 | return ret; | |
1337 | } | ||
1338 | |||
1339 | static int extract_extradata(FFFormatContext *si, AVStream *st, const AVPacket *pkt); | ||
1340 | |||
1341 | 573206 | static int read_frame_internal(AVFormatContext *s, AVPacket *pkt) | |
1342 | { | ||
1343 | 573206 | FormatContextInternal *const fci = ff_fc_internal(s); | |
1344 | 573206 | FFFormatContext *const si = &fci->fc; | |
1345 | 573206 | int ret, got_packet = 0; | |
1346 | 573206 | AVDictionary *metadata = NULL; | |
1347 | |||
1348 |
4/4✓ Branch 0 taken 1268907 times.
✓ Branch 1 taken 381869 times.
✓ Branch 2 taken 1083958 times.
✓ Branch 3 taken 184949 times.
|
1650776 | while (!got_packet && !fci->parse_queue.head) { |
1349 | AVStream *st; | ||
1350 | FFStream *sti; | ||
1351 | |||
1352 | /* read next packet */ | ||
1353 | 1083958 | ret = ff_read_packet(s, pkt); | |
1354 |
2/2✓ Branch 0 taken 6388 times.
✓ Branch 1 taken 1077570 times.
|
1083958 | if (ret < 0) { |
1355 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6387 times.
|
6388 | if (ret == AVERROR(EAGAIN)) |
1356 | 1 | return ret; | |
1357 | /* flush the parsers */ | ||
1358 |
2/2✓ Branch 0 taken 7430 times.
✓ Branch 1 taken 6387 times.
|
13817 | for (unsigned i = 0; i < s->nb_streams; i++) { |
1359 | 7430 | AVStream *const st = s->streams[i]; | |
1360 | 7430 | FFStream *const sti = ffstream(st); | |
1361 |
4/4✓ Branch 0 taken 2727 times.
✓ Branch 1 taken 4703 times.
✓ Branch 2 taken 2072 times.
✓ Branch 3 taken 655 times.
|
7430 | if (sti->parser && sti->need_parsing) |
1362 | 2072 | parse_packet(s, pkt, st->index, 1); | |
1363 | } | ||
1364 | /* all remaining packets are now in parse_queue => | ||
1365 | * really terminate parsing */ | ||
1366 | 6387 | break; | |
1367 | } | ||
1368 | 1077570 | ret = 0; | |
1369 | 1077570 | st = s->streams[pkt->stream_index]; | |
1370 | 1077570 | sti = ffstream(st); | |
1371 | |||
1372 | 1077570 | st->event_flags |= AVSTREAM_EVENT_FLAG_NEW_PACKETS; | |
1373 | |||
1374 | /* update context if required */ | ||
1375 |
2/2✓ Branch 0 taken 196 times.
✓ Branch 1 taken 1077374 times.
|
1077570 | if (sti->need_context_update) { |
1376 |
2/2✓ Branch 1 taken 13 times.
✓ Branch 2 taken 183 times.
|
196 | if (avcodec_is_open(sti->avctx)) { |
1377 | 13 | av_log(s, AV_LOG_DEBUG, "Demuxer context update while decoder is open, closing and trying to re-open\n"); | |
1378 | 13 | ret = codec_close(sti); | |
1379 | 13 | sti->info->found_decoder = 0; | |
1380 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
|
13 | if (ret < 0) |
1381 | ✗ | return ret; | |
1382 | } | ||
1383 | |||
1384 | /* close parser, because it depends on the codec */ | ||
1385 |
4/4✓ Branch 0 taken 13 times.
✓ Branch 1 taken 183 times.
✓ Branch 2 taken 10 times.
✓ Branch 3 taken 3 times.
|
196 | if (sti->parser && sti->avctx->codec_id != st->codecpar->codec_id) { |
1386 | 10 | av_parser_close(sti->parser); | |
1387 | 10 | sti->parser = NULL; | |
1388 | } | ||
1389 | |||
1390 | 196 | ret = avcodec_parameters_to_context(sti->avctx, st->codecpar); | |
1391 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 196 times.
|
196 | if (ret < 0) { |
1392 | ✗ | av_packet_unref(pkt); | |
1393 | ✗ | return ret; | |
1394 | } | ||
1395 | |||
1396 |
2/2✓ Branch 0 taken 149 times.
✓ Branch 1 taken 47 times.
|
196 | if (!sti->avctx->extradata) { |
1397 | 149 | sti->extract_extradata.inited = 0; | |
1398 | |||
1399 | 149 | ret = extract_extradata(si, st, pkt); | |
1400 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 149 times.
|
149 | if (ret < 0) { |
1401 | ✗ | av_packet_unref(pkt); | |
1402 | ✗ | return ret; | |
1403 | } | ||
1404 | } | ||
1405 | |||
1406 | 196 | sti->codec_desc = avcodec_descriptor_get(sti->avctx->codec_id); | |
1407 | |||
1408 | 196 | sti->need_context_update = 0; | |
1409 | } | ||
1410 | |||
1411 |
2/2✓ Branch 0 taken 336856 times.
✓ Branch 1 taken 740714 times.
|
1077570 | if (pkt->pts != AV_NOPTS_VALUE && |
1412 |
2/2✓ Branch 0 taken 168650 times.
✓ Branch 1 taken 168206 times.
|
336856 | pkt->dts != AV_NOPTS_VALUE && |
1413 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 168650 times.
|
168650 | pkt->pts < pkt->dts) { |
1414 | ✗ | av_log(s, AV_LOG_WARNING, | |
1415 | "Invalid timestamps stream=%d, pts=%s, dts=%s, size=%d\n", | ||
1416 | pkt->stream_index, | ||
1417 | ✗ | av_ts2str(pkt->pts), | |
1418 | ✗ | av_ts2str(pkt->dts), | |
1419 | pkt->size); | ||
1420 | } | ||
1421 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1077570 times.
|
1077570 | if (s->debug & FF_FDEBUG_TS) |
1422 | ✗ | av_log(s, AV_LOG_DEBUG, | |
1423 | "ff_read_packet stream=%d, pts=%s, dts=%s, size=%d, duration=%"PRId64", flags=%d\n", | ||
1424 | pkt->stream_index, | ||
1425 | ✗ | av_ts2str(pkt->pts), | |
1426 | ✗ | av_ts2str(pkt->dts), | |
1427 | pkt->size, pkt->duration, pkt->flags); | ||
1428 | |||
1429 |
5/6✓ Branch 0 taken 696854 times.
✓ Branch 1 taken 380716 times.
✓ Branch 2 taken 2764 times.
✓ Branch 3 taken 694090 times.
✓ Branch 4 taken 2764 times.
✗ Branch 5 not taken.
|
1077570 | if (sti->need_parsing && !sti->parser && !(s->flags & AVFMT_FLAG_NOPARSE)) { |
1430 | 2764 | sti->parser = av_parser_init(st->codecpar->codec_id); | |
1431 |
2/2✓ Branch 0 taken 1153 times.
✓ Branch 1 taken 1611 times.
|
2764 | if (!sti->parser) { |
1432 | 1153 | av_log(s, AV_LOG_VERBOSE, "parser not found for codec " | |
1433 | "%s, packets or times may be invalid.\n", | ||
1434 | 1153 | avcodec_get_name(st->codecpar->codec_id)); | |
1435 | /* no parser available: just output the raw packets */ | ||
1436 | 1153 | sti->need_parsing = AVSTREAM_PARSE_NONE; | |
1437 |
2/2✓ Branch 0 taken 716 times.
✓ Branch 1 taken 895 times.
|
1611 | } else if (sti->need_parsing == AVSTREAM_PARSE_HEADERS) |
1438 | 716 | sti->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES; | |
1439 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 895 times.
|
895 | else if (sti->need_parsing == AVSTREAM_PARSE_FULL_ONCE) |
1440 | ✗ | sti->parser->flags |= PARSER_FLAG_ONCE; | |
1441 |
2/2✓ Branch 0 taken 471 times.
✓ Branch 1 taken 424 times.
|
895 | else if (sti->need_parsing == AVSTREAM_PARSE_FULL_RAW) |
1442 | 471 | sti->parser->flags |= PARSER_FLAG_USE_CODEC_TS; | |
1443 | } | ||
1444 | |||
1445 |
3/4✓ Branch 0 taken 695701 times.
✓ Branch 1 taken 381869 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 695701 times.
|
1077570 | if (!sti->need_parsing || !sti->parser) { |
1446 | /* no parsing needed: we just output the packet as is */ | ||
1447 | 381869 | compute_pkt_fields(s, st, NULL, pkt, AV_NOPTS_VALUE, AV_NOPTS_VALUE); | |
1448 |
2/2✓ Branch 0 taken 92938 times.
✓ Branch 1 taken 288931 times.
|
381869 | if ((s->iformat->flags & AVFMT_GENERIC_INDEX) && |
1449 |
4/4✓ Branch 0 taken 92202 times.
✓ Branch 1 taken 736 times.
✓ Branch 2 taken 92057 times.
✓ Branch 3 taken 145 times.
|
92938 | (pkt->flags & AV_PKT_FLAG_KEY) && pkt->dts != AV_NOPTS_VALUE) { |
1450 | 92057 | ff_reduce_index(s, st->index); | |
1451 | 92057 | av_add_index_entry(st, pkt->pos, pkt->dts, | |
1452 | 0, 0, AVINDEX_KEYFRAME); | ||
1453 | } | ||
1454 | 381869 | got_packet = 1; | |
1455 |
2/2✓ Branch 0 taken 695570 times.
✓ Branch 1 taken 131 times.
|
695701 | } else if (st->discard < AVDISCARD_ALL) { |
1456 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 695570 times.
|
695570 | if ((ret = parse_packet(s, pkt, pkt->stream_index, 0)) < 0) |
1457 | ✗ | return ret; | |
1458 | 695570 | st->codecpar->sample_rate = sti->avctx->sample_rate; | |
1459 | 695570 | st->codecpar->bit_rate = sti->avctx->bit_rate; | |
1460 | 695570 | ret = av_channel_layout_copy(&st->codecpar->ch_layout, &sti->avctx->ch_layout); | |
1461 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 695570 times.
|
695570 | if (ret < 0) |
1462 | ✗ | return ret; | |
1463 | 695570 | st->codecpar->codec_id = sti->avctx->codec_id; | |
1464 | } else { | ||
1465 | /* free packet */ | ||
1466 | 131 | av_packet_unref(pkt); | |
1467 | } | ||
1468 |
2/2✓ Branch 0 taken 358437 times.
✓ Branch 1 taken 719133 times.
|
1077570 | if (pkt->flags & AV_PKT_FLAG_KEY) |
1469 | 358437 | sti->skip_to_keyframe = 0; | |
1470 |
2/2✓ Branch 0 taken 85 times.
✓ Branch 1 taken 1077485 times.
|
1077570 | if (sti->skip_to_keyframe) { |
1471 | 85 | av_packet_unref(pkt); | |
1472 | 85 | got_packet = 0; | |
1473 | } | ||
1474 | } | ||
1475 | |||
1476 |
4/4✓ Branch 0 taken 191336 times.
✓ Branch 1 taken 381869 times.
✓ Branch 2 taken 185853 times.
✓ Branch 3 taken 5483 times.
|
573205 | if (!got_packet && fci->parse_queue.head) |
1477 | 185853 | ret = avpriv_packet_list_get(&fci->parse_queue, pkt); | |
1478 | |||
1479 |
2/2✓ Branch 0 taken 567722 times.
✓ Branch 1 taken 5483 times.
|
573205 | if (ret >= 0) { |
1480 | 567722 | AVStream *const st = s->streams[pkt->stream_index]; | |
1481 | 567722 | FFStream *const sti = ffstream(st); | |
1482 | 567722 | int discard_padding = 0; | |
1483 |
3/4✓ Branch 0 taken 4387 times.
✓ Branch 1 taken 563335 times.
✓ Branch 2 taken 4387 times.
✗ Branch 3 not taken.
|
567722 | if (sti->first_discard_sample && pkt->pts != AV_NOPTS_VALUE) { |
1484 |
2/2✓ Branch 1 taken 2632 times.
✓ Branch 2 taken 1755 times.
|
4387 | int64_t pts = pkt->pts - (is_relative(pkt->pts) ? RELATIVE_TS_BASE : 0); |
1485 | 4387 | int64_t sample = ts_to_samples(st, pts); | |
1486 | 4387 | int64_t duration = ts_to_samples(st, pkt->duration); | |
1487 | 4387 | int64_t end_sample = sample + duration; | |
1488 |
3/4✓ Branch 0 taken 4387 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
✓ Branch 3 taken 4373 times.
|
4387 | if (duration > 0 && end_sample >= sti->first_discard_sample && |
1489 |
1/2✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
|
14 | sample < sti->last_discard_sample) |
1490 | 14 | discard_padding = FFMIN(end_sample - sti->first_discard_sample, duration); | |
1491 | } | ||
1492 |
6/6✓ Branch 0 taken 4387 times.
✓ Branch 1 taken 563335 times.
✓ Branch 2 taken 4371 times.
✓ Branch 3 taken 16 times.
✓ Branch 4 taken 20 times.
✓ Branch 5 taken 4351 times.
|
567722 | if (sti->start_skip_samples && (pkt->pts == 0 || pkt->pts == RELATIVE_TS_BASE)) |
1493 | 36 | sti->skip_samples = sti->start_skip_samples; | |
1494 | 567722 | sti->skip_samples = FFMAX(0, sti->skip_samples); | |
1495 |
4/4✓ Branch 0 taken 567634 times.
✓ Branch 1 taken 88 times.
✓ Branch 2 taken 14 times.
✓ Branch 3 taken 567620 times.
|
567722 | if (sti->skip_samples || discard_padding) { |
1496 | 102 | uint8_t *p = av_packet_new_side_data(pkt, AV_PKT_DATA_SKIP_SAMPLES, 10); | |
1497 |
1/2✓ Branch 0 taken 102 times.
✗ Branch 1 not taken.
|
102 | if (p) { |
1498 | 102 | AV_WL32(p, sti->skip_samples); | |
1499 | 102 | AV_WL32(p + 4, discard_padding); | |
1500 | 102 | av_log(s, AV_LOG_DEBUG, "demuxer injecting skip %u / discard %u\n", | |
1501 | 102 | (unsigned)sti->skip_samples, (unsigned)discard_padding); | |
1502 | } | ||
1503 | 102 | sti->skip_samples = 0; | |
1504 | } | ||
1505 | |||
1506 | #if FF_API_AVSTREAM_SIDE_DATA | ||
1507 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 567722 times.
|
567722 | if (sti->inject_global_side_data) { |
1508 | ✗ | for (int i = 0; i < st->codecpar->nb_coded_side_data; i++) { | |
1509 | ✗ | const AVPacketSideData *const src_sd = &st->codecpar->coded_side_data[i]; | |
1510 | uint8_t *dst_data; | ||
1511 | |||
1512 | ✗ | if (av_packet_get_side_data(pkt, src_sd->type, NULL)) | |
1513 | ✗ | continue; | |
1514 | |||
1515 | ✗ | dst_data = av_packet_new_side_data(pkt, src_sd->type, src_sd->size); | |
1516 | ✗ | if (!dst_data) { | |
1517 | ✗ | av_log(s, AV_LOG_WARNING, "Could not inject global side data\n"); | |
1518 | ✗ | continue; | |
1519 | } | ||
1520 | |||
1521 | ✗ | memcpy(dst_data, src_sd->data, src_sd->size); | |
1522 | } | ||
1523 | ✗ | sti->inject_global_side_data = 0; | |
1524 | } | ||
1525 | #endif | ||
1526 | } | ||
1527 | |||
1528 |
2/2✓ Branch 0 taken 7543 times.
✓ Branch 1 taken 565662 times.
|
573205 | if (!fci->metafree) { |
1529 | 7543 | int metaret = av_opt_get_dict_val(s, "metadata", AV_OPT_SEARCH_CHILDREN, &metadata); | |
1530 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7543 times.
|
7543 | if (metadata) { |
1531 | ✗ | s->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED; | |
1532 | ✗ | av_dict_copy(&s->metadata, metadata, 0); | |
1533 | ✗ | av_dict_free(&metadata); | |
1534 | ✗ | av_opt_set_dict_val(s, "metadata", NULL, AV_OPT_SEARCH_CHILDREN); | |
1535 | } | ||
1536 | 7543 | fci->metafree = metaret == AVERROR_OPTION_NOT_FOUND; | |
1537 | } | ||
1538 | |||
1539 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 573205 times.
|
573205 | if (s->debug & FF_FDEBUG_TS) |
1540 | ✗ | av_log(s, AV_LOG_DEBUG, | |
1541 | "read_frame_internal stream=%d, pts=%s, dts=%s, " | ||
1542 | "size=%d, duration=%"PRId64", flags=%d\n", | ||
1543 | pkt->stream_index, | ||
1544 | ✗ | av_ts2str(pkt->pts), | |
1545 | ✗ | av_ts2str(pkt->dts), | |
1546 | pkt->size, pkt->duration, pkt->flags); | ||
1547 | |||
1548 | /* A demuxer might have returned EOF because of an IO error, let's | ||
1549 | * propagate this back to the user. */ | ||
1550 |
5/8✓ Branch 0 taken 5371 times.
✓ Branch 1 taken 567834 times.
✓ Branch 2 taken 5154 times.
✓ Branch 3 taken 217 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 5154 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
573205 | if (ret == AVERROR_EOF && s->pb && s->pb->error < 0 && s->pb->error != AVERROR(EAGAIN)) |
1551 | ✗ | ret = s->pb->error; | |
1552 | |||
1553 | 573205 | return ret; | |
1554 | } | ||
1555 | |||
1556 | 511423 | int av_read_frame(AVFormatContext *s, AVPacket *pkt) | |
1557 | { | ||
1558 | 511423 | FFFormatContext *const si = ffformatcontext(s); | |
1559 | 511423 | const int genpts = s->flags & AVFMT_FLAG_GENPTS; | |
1560 | 511423 | int eof = 0; | |
1561 | int ret; | ||
1562 | AVStream *st; | ||
1563 | |||
1564 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 511423 times.
|
511423 | if (!genpts) { |
1565 | 1022846 | ret = si->packet_buffer.head | |
1566 | 132268 | ? avpriv_packet_list_get(&si->packet_buffer, pkt) | |
1567 |
2/2✓ Branch 0 taken 132268 times.
✓ Branch 1 taken 379155 times.
|
511423 | : read_frame_internal(s, pkt); |
1568 |
2/2✓ Branch 0 taken 4306 times.
✓ Branch 1 taken 507117 times.
|
511423 | if (ret < 0) |
1569 | 4306 | return ret; | |
1570 | 507117 | goto return_packet; | |
1571 | } | ||
1572 | |||
1573 | ✗ | for (;;) { | |
1574 | ✗ | PacketListEntry *pktl = si->packet_buffer.head; | |
1575 | |||
1576 | ✗ | if (pktl) { | |
1577 | ✗ | AVPacket *next_pkt = &pktl->pkt; | |
1578 | |||
1579 | ✗ | if (next_pkt->dts != AV_NOPTS_VALUE) { | |
1580 | ✗ | int wrap_bits = s->streams[next_pkt->stream_index]->pts_wrap_bits; | |
1581 | // last dts seen for this stream. if any of packets following | ||
1582 | // current one had no dts, we will set this to AV_NOPTS_VALUE. | ||
1583 | ✗ | int64_t last_dts = next_pkt->dts; | |
1584 | av_assert2(wrap_bits <= 64); | ||
1585 | ✗ | while (pktl && next_pkt->pts == AV_NOPTS_VALUE) { | |
1586 | ✗ | if (pktl->pkt.stream_index == next_pkt->stream_index && | |
1587 | ✗ | av_compare_mod(next_pkt->dts, pktl->pkt.dts, 2ULL << (wrap_bits - 1)) < 0) { | |
1588 | ✗ | if (av_compare_mod(pktl->pkt.pts, pktl->pkt.dts, 2ULL << (wrap_bits - 1))) { | |
1589 | // not B-frame | ||
1590 | ✗ | next_pkt->pts = pktl->pkt.dts; | |
1591 | } | ||
1592 | ✗ | if (last_dts != AV_NOPTS_VALUE) { | |
1593 | // Once last dts was set to AV_NOPTS_VALUE, we don't change it. | ||
1594 | ✗ | last_dts = pktl->pkt.dts; | |
1595 | } | ||
1596 | } | ||
1597 | ✗ | pktl = pktl->next; | |
1598 | } | ||
1599 | ✗ | if (eof && next_pkt->pts == AV_NOPTS_VALUE && last_dts != AV_NOPTS_VALUE) { | |
1600 | // Fixing the last reference frame had none pts issue (For MXF etc). | ||
1601 | // We only do this when | ||
1602 | // 1. eof. | ||
1603 | // 2. we are not able to resolve a pts value for current packet. | ||
1604 | // 3. the packets for this stream at the end of the files had valid dts. | ||
1605 | ✗ | next_pkt->pts = last_dts + next_pkt->duration; | |
1606 | } | ||
1607 | ✗ | pktl = si->packet_buffer.head; | |
1608 | } | ||
1609 | |||
1610 | /* read packet from packet buffer, if there is data */ | ||
1611 | ✗ | st = s->streams[next_pkt->stream_index]; | |
1612 | ✗ | if (!(next_pkt->pts == AV_NOPTS_VALUE && st->discard < AVDISCARD_ALL && | |
1613 | ✗ | next_pkt->dts != AV_NOPTS_VALUE && !eof)) { | |
1614 | ✗ | ret = avpriv_packet_list_get(&si->packet_buffer, pkt); | |
1615 | ✗ | goto return_packet; | |
1616 | } | ||
1617 | } | ||
1618 | |||
1619 | ✗ | ret = read_frame_internal(s, pkt); | |
1620 | ✗ | if (ret < 0) { | |
1621 | ✗ | if (pktl && ret != AVERROR(EAGAIN)) { | |
1622 | ✗ | eof = 1; | |
1623 | ✗ | continue; | |
1624 | } else | ||
1625 | ✗ | return ret; | |
1626 | } | ||
1627 | |||
1628 | ✗ | ret = avpriv_packet_list_put(&si->packet_buffer, | |
1629 | pkt, NULL, 0); | ||
1630 | ✗ | if (ret < 0) { | |
1631 | ✗ | av_packet_unref(pkt); | |
1632 | ✗ | return ret; | |
1633 | } | ||
1634 | } | ||
1635 | |||
1636 | 507117 | return_packet: | |
1637 | 507117 | st = s->streams[pkt->stream_index]; | |
1638 |
4/4✓ Branch 0 taken 194145 times.
✓ Branch 1 taken 312972 times.
✓ Branch 2 taken 120479 times.
✓ Branch 3 taken 73666 times.
|
507117 | if ((s->iformat->flags & AVFMT_GENERIC_INDEX) && pkt->flags & AV_PKT_FLAG_KEY) { |
1639 | 120479 | ff_reduce_index(s, st->index); | |
1640 | 120479 | av_add_index_entry(st, pkt->pos, pkt->dts, 0, 0, AVINDEX_KEYFRAME); | |
1641 | } | ||
1642 | |||
1643 |
2/2✓ Branch 1 taken 120286 times.
✓ Branch 2 taken 386831 times.
|
507117 | if (is_relative(pkt->dts)) |
1644 | 120286 | pkt->dts -= RELATIVE_TS_BASE; | |
1645 |
2/2✓ Branch 1 taken 118590 times.
✓ Branch 2 taken 388527 times.
|
507117 | if (is_relative(pkt->pts)) |
1646 | 118590 | pkt->pts -= RELATIVE_TS_BASE; | |
1647 | |||
1648 | 507117 | return ret; | |
1649 | } | ||
1650 | |||
1651 | /** | ||
1652 | * Return TRUE if the stream has accurate duration in any stream. | ||
1653 | * | ||
1654 | * @return TRUE if the stream has accurate duration for at least one component. | ||
1655 | */ | ||
1656 | 7466 | static int has_duration(AVFormatContext *ic) | |
1657 | { | ||
1658 |
2/2✓ Branch 0 taken 7757 times.
✓ Branch 1 taken 2415 times.
|
10172 | for (unsigned i = 0; i < ic->nb_streams; i++) { |
1659 | 7757 | const AVStream *const st = ic->streams[i]; | |
1660 |
2/2✓ Branch 0 taken 5051 times.
✓ Branch 1 taken 2706 times.
|
7757 | if (st->duration != AV_NOPTS_VALUE) |
1661 | 5051 | return 1; | |
1662 | } | ||
1663 |
2/2✓ Branch 0 taken 437 times.
✓ Branch 1 taken 1978 times.
|
2415 | if (ic->duration != AV_NOPTS_VALUE) |
1664 | 437 | return 1; | |
1665 | 1978 | return 0; | |
1666 | } | ||
1667 | |||
1668 | /** | ||
1669 | * Estimate the stream timings from the one of each components. | ||
1670 | * | ||
1671 | * Also computes the global bitrate if possible. | ||
1672 | */ | ||
1673 | 13092 | static void update_stream_timings(AVFormatContext *ic) | |
1674 | { | ||
1675 | int64_t start_time, start_time1, start_time_text, end_time, end_time1, end_time_text; | ||
1676 | int64_t duration, duration1, duration_text, filesize; | ||
1677 | |||
1678 | 13092 | start_time = INT64_MAX; | |
1679 | 13092 | start_time_text = INT64_MAX; | |
1680 | 13092 | end_time = INT64_MIN; | |
1681 | 13092 | end_time_text = INT64_MIN; | |
1682 | 13092 | duration = INT64_MIN; | |
1683 | 13092 | duration_text = INT64_MIN; | |
1684 | |||
1685 |
2/2✓ Branch 0 taken 14564 times.
✓ Branch 1 taken 13092 times.
|
27656 | for (unsigned i = 0; i < ic->nb_streams; i++) { |
1686 | 14564 | AVStream *const st = ic->streams[i]; | |
1687 |
2/2✓ Branch 0 taken 14439 times.
✓ Branch 1 taken 125 times.
|
29003 | int is_text = st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE || |
1688 |
2/2✓ Branch 0 taken 151 times.
✓ Branch 1 taken 14288 times.
|
14439 | st->codecpar->codec_type == AVMEDIA_TYPE_DATA; |
1689 | |||
1690 |
3/4✓ Branch 0 taken 11914 times.
✓ Branch 1 taken 2650 times.
✓ Branch 2 taken 11914 times.
✗ Branch 3 not taken.
|
14564 | if (st->start_time != AV_NOPTS_VALUE && st->time_base.den) { |
1691 | 11914 | start_time1 = av_rescale_q(st->start_time, st->time_base, | |
1692 | 11914 | AV_TIME_BASE_Q); | |
1693 |
2/2✓ Branch 0 taken 158 times.
✓ Branch 1 taken 11756 times.
|
11914 | if (is_text) |
1694 | 158 | start_time_text = FFMIN(start_time_text, start_time1); | |
1695 | else | ||
1696 | 11756 | start_time = FFMIN(start_time, start_time1); | |
1697 | 11914 | end_time1 = av_rescale_q_rnd(st->duration, st->time_base, | |
1698 | 11914 | AV_TIME_BASE_Q, | |
1699 | AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX); | ||
1700 |
5/6✓ Branch 0 taken 10530 times.
✓ Branch 1 taken 1384 times.
✓ Branch 2 taken 10501 times.
✓ Branch 3 taken 29 times.
✓ Branch 4 taken 10530 times.
✗ Branch 5 not taken.
|
11914 | if (end_time1 != AV_NOPTS_VALUE && (end_time1 > 0 ? start_time1 <= INT64_MAX - end_time1 : start_time1 >= INT64_MIN - end_time1)) { |
1701 | 10530 | end_time1 += start_time1; | |
1702 |
2/2✓ Branch 0 taken 143 times.
✓ Branch 1 taken 10387 times.
|
10530 | if (is_text) |
1703 | 143 | end_time_text = FFMAX(end_time_text, end_time1); | |
1704 | else | ||
1705 | 10387 | end_time = FFMAX(end_time, end_time1); | |
1706 | } | ||
1707 |
2/2✓ Branch 1 taken 208 times.
✓ Branch 2 taken 11914 times.
|
12122 | for (AVProgram *p = NULL; (p = av_find_program_from_stream(ic, p, i)); ) { |
1708 |
4/4✓ Branch 0 taken 152 times.
✓ Branch 1 taken 56 times.
✓ Branch 2 taken 25 times.
✓ Branch 3 taken 127 times.
|
208 | if (p->start_time == AV_NOPTS_VALUE || p->start_time > start_time1) |
1709 | 81 | p->start_time = start_time1; | |
1710 |
2/2✓ Branch 0 taken 52 times.
✓ Branch 1 taken 156 times.
|
208 | if (p->end_time < end_time1) |
1711 | 52 | p->end_time = end_time1; | |
1712 | } | ||
1713 | } | ||
1714 |
2/2✓ Branch 0 taken 11987 times.
✓ Branch 1 taken 2577 times.
|
14564 | if (st->duration != AV_NOPTS_VALUE) { |
1715 | 11987 | duration1 = av_rescale_q(st->duration, st->time_base, | |
1716 | 11987 | AV_TIME_BASE_Q); | |
1717 |
2/2✓ Branch 0 taken 167 times.
✓ Branch 1 taken 11820 times.
|
11987 | if (is_text) |
1718 | 167 | duration_text = FFMAX(duration_text, duration1); | |
1719 | else | ||
1720 | 11820 | duration = FFMAX(duration, duration1); | |
1721 | } | ||
1722 | } | ||
1723 |
3/6✓ Branch 0 taken 10656 times.
✓ Branch 1 taken 2436 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 10656 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
13092 | if (start_time == INT64_MAX || (start_time > start_time_text && start_time - (uint64_t)start_time_text < AV_TIME_BASE)) |
1724 | 2436 | start_time = start_time_text; | |
1725 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10656 times.
|
10656 | else if (start_time > start_time_text) |
1726 | ✗ | av_log(ic, AV_LOG_VERBOSE, "Ignoring outlier non primary stream starttime %f\n", start_time_text / (float)AV_TIME_BASE); | |
1727 | |||
1728 |
5/6✓ Branch 0 taken 9682 times.
✓ Branch 1 taken 3410 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 9680 times.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
|
13092 | if (end_time == INT64_MIN || (end_time < end_time_text && end_time_text - (uint64_t)end_time < AV_TIME_BASE)) |
1729 | 3412 | end_time = end_time_text; | |
1730 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9680 times.
|
9680 | else if (end_time < end_time_text) |
1731 | ✗ | av_log(ic, AV_LOG_VERBOSE, "Ignoring outlier non primary stream endtime %f\n", end_time_text / (float)AV_TIME_BASE); | |
1732 | |||
1733 |
5/6✓ Branch 0 taken 11100 times.
✓ Branch 1 taken 1992 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 11094 times.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
|
13092 | if (duration == INT64_MIN || (duration < duration_text && (uint64_t)duration_text - duration < AV_TIME_BASE)) |
1734 | 1998 | duration = duration_text; | |
1735 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 11094 times.
|
11094 | else if (duration < duration_text) |
1736 | ✗ | av_log(ic, AV_LOG_VERBOSE, "Ignoring outlier non primary stream duration %f\n", duration_text / (float)AV_TIME_BASE); | |
1737 | |||
1738 |
2/2✓ Branch 0 taken 10677 times.
✓ Branch 1 taken 2415 times.
|
13092 | if (start_time != INT64_MAX) { |
1739 | 10677 | ic->start_time = start_time; | |
1740 |
2/2✓ Branch 0 taken 9699 times.
✓ Branch 1 taken 978 times.
|
10677 | if (end_time != INT64_MIN) { |
1741 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9699 times.
|
9699 | if (ic->nb_programs > 1) { |
1742 | ✗ | for (unsigned i = 0; i < ic->nb_programs; i++) { | |
1743 | ✗ | AVProgram *const p = ic->programs[i]; | |
1744 | |||
1745 | ✗ | if (p->start_time != AV_NOPTS_VALUE && | |
1746 | ✗ | p->end_time > p->start_time && | |
1747 | ✗ | p->end_time - (uint64_t)p->start_time <= INT64_MAX) | |
1748 | ✗ | duration = FFMAX(duration, p->end_time - p->start_time); | |
1749 | } | ||
1750 |
2/4✓ Branch 0 taken 9699 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9699 times.
✗ Branch 3 not taken.
|
9699 | } else if (end_time >= start_time && end_time - (uint64_t)start_time <= INT64_MAX) { |
1751 | 9699 | duration = FFMAX(duration, end_time - start_time); | |
1752 | } | ||
1753 | } | ||
1754 | } | ||
1755 |
6/6✓ Branch 0 taken 11130 times.
✓ Branch 1 taken 1962 times.
✓ Branch 2 taken 11094 times.
✓ Branch 3 taken 36 times.
✓ Branch 4 taken 5961 times.
✓ Branch 5 taken 5133 times.
|
13092 | if (duration != INT64_MIN && duration > 0 && ic->duration == AV_NOPTS_VALUE) { |
1756 | 5961 | ic->duration = duration; | |
1757 | } | ||
1758 |
5/6✓ Branch 0 taken 7063 times.
✓ Branch 1 taken 6029 times.
✓ Branch 3 taken 7063 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 5951 times.
✓ Branch 6 taken 1112 times.
|
13092 | if (ic->pb && (filesize = avio_size(ic->pb)) > 0 && ic->duration > 0) { |
1759 | /* compute the bitrate */ | ||
1760 | 5951 | double bitrate = (double) filesize * 8.0 * AV_TIME_BASE / | |
1761 | 5951 | (double) ic->duration; | |
1762 |
2/4✓ Branch 0 taken 5951 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5951 times.
✗ Branch 3 not taken.
|
5951 | if (bitrate >= 0 && bitrate <= INT64_MAX) |
1763 | 5951 | ic->bit_rate = bitrate; | |
1764 | } | ||
1765 | 13092 | } | |
1766 | |||
1767 | 5557 | static void fill_all_stream_timings(AVFormatContext *ic) | |
1768 | { | ||
1769 | 5557 | update_stream_timings(ic); | |
1770 |
2/2✓ Branch 0 taken 6220 times.
✓ Branch 1 taken 5557 times.
|
11777 | for (unsigned i = 0; i < ic->nb_streams; i++) { |
1771 | 6220 | AVStream *const st = ic->streams[i]; | |
1772 | |||
1773 |
2/2✓ Branch 0 taken 772 times.
✓ Branch 1 taken 5448 times.
|
6220 | if (st->start_time == AV_NOPTS_VALUE) { |
1774 |
2/2✓ Branch 0 taken 129 times.
✓ Branch 1 taken 643 times.
|
772 | if (ic->start_time != AV_NOPTS_VALUE) |
1775 | 129 | st->start_time = av_rescale_q(ic->start_time, AV_TIME_BASE_Q, | |
1776 | st->time_base); | ||
1777 |
2/2✓ Branch 0 taken 757 times.
✓ Branch 1 taken 15 times.
|
772 | if (ic->duration != AV_NOPTS_VALUE) |
1778 | 757 | st->duration = av_rescale_q(ic->duration, AV_TIME_BASE_Q, | |
1779 | st->time_base); | ||
1780 | } | ||
1781 | } | ||
1782 | 5557 | } | |
1783 | |||
1784 | 1978 | static void estimate_timings_from_bit_rate(AVFormatContext *ic) | |
1785 | { | ||
1786 | 1978 | FFFormatContext *const si = ffformatcontext(ic); | |
1787 | 1978 | int show_warning = 0; | |
1788 | |||
1789 | /* if bit_rate is already set, we believe it */ | ||
1790 |
2/2✓ Branch 0 taken 1943 times.
✓ Branch 1 taken 35 times.
|
1978 | if (ic->bit_rate <= 0) { |
1791 | 1943 | int64_t bit_rate = 0; | |
1792 |
2/2✓ Branch 0 taken 2067 times.
✓ Branch 1 taken 1296 times.
|
3363 | for (unsigned i = 0; i < ic->nb_streams; i++) { |
1793 | 2067 | const AVStream *const st = ic->streams[i]; | |
1794 | 2067 | const FFStream *const sti = cffstream(st); | |
1795 |
4/4✓ Branch 0 taken 1295 times.
✓ Branch 1 taken 772 times.
✓ Branch 2 taken 110 times.
✓ Branch 3 taken 1185 times.
|
2067 | if (st->codecpar->bit_rate <= 0 && sti->avctx->bit_rate > 0) |
1796 | 110 | st->codecpar->bit_rate = sti->avctx->bit_rate; | |
1797 |
2/2✓ Branch 0 taken 882 times.
✓ Branch 1 taken 1185 times.
|
2067 | if (st->codecpar->bit_rate > 0) { |
1798 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 882 times.
|
882 | if (INT64_MAX - st->codecpar->bit_rate < bit_rate) { |
1799 | ✗ | bit_rate = 0; | |
1800 | ✗ | break; | |
1801 | } | ||
1802 | 882 | bit_rate += st->codecpar->bit_rate; | |
1803 |
4/4✓ Branch 0 taken 1017 times.
✓ Branch 1 taken 168 times.
✓ Branch 2 taken 647 times.
✓ Branch 3 taken 370 times.
|
1185 | } else if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && sti->codec_info_nb_frames > 1) { |
1804 | // If we have a videostream with packets but without a bitrate | ||
1805 | // then consider the sum not known | ||
1806 | 647 | bit_rate = 0; | |
1807 | 647 | break; | |
1808 | } | ||
1809 | } | ||
1810 | 1943 | ic->bit_rate = bit_rate; | |
1811 | } | ||
1812 | |||
1813 | /* if duration is already set, we believe it */ | ||
1814 |
1/2✓ Branch 0 taken 1978 times.
✗ Branch 1 not taken.
|
1978 | if (ic->duration == AV_NOPTS_VALUE && |
1815 |
2/2✓ Branch 0 taken 889 times.
✓ Branch 1 taken 1089 times.
|
1978 | ic->bit_rate != 0) { |
1816 |
2/2✓ Branch 0 taken 865 times.
✓ Branch 1 taken 24 times.
|
889 | int64_t filesize = ic->pb ? avio_size(ic->pb) : 0; |
1817 |
2/2✓ Branch 0 taken 859 times.
✓ Branch 1 taken 30 times.
|
889 | if (filesize > si->data_offset) { |
1818 | 859 | filesize -= si->data_offset; | |
1819 |
2/2✓ Branch 0 taken 896 times.
✓ Branch 1 taken 859 times.
|
1755 | for (unsigned i = 0; i < ic->nb_streams; i++) { |
1820 | 896 | AVStream *const st = ic->streams[i]; | |
1821 | |||
1822 |
1/2✓ Branch 0 taken 896 times.
✗ Branch 1 not taken.
|
896 | if ( st->time_base.num <= INT64_MAX / ic->bit_rate |
1823 |
1/2✓ Branch 0 taken 896 times.
✗ Branch 1 not taken.
|
896 | && st->duration == AV_NOPTS_VALUE) { |
1824 | 896 | st->duration = av_rescale(filesize, 8LL * st->time_base.den, | |
1825 | 896 | ic->bit_rate * | |
1826 | 896 | (int64_t) st->time_base.num); | |
1827 | 896 | show_warning = 1; | |
1828 | } | ||
1829 | } | ||
1830 | } | ||
1831 | } | ||
1832 |
2/2✓ Branch 0 taken 859 times.
✓ Branch 1 taken 1119 times.
|
1978 | if (show_warning) |
1833 | 859 | av_log(ic, AV_LOG_WARNING, | |
1834 | "Estimating duration from bitrate, this may be inaccurate\n"); | ||
1835 | 1978 | } | |
1836 | |||
1837 | #define DURATION_DEFAULT_MAX_READ_SIZE 250000LL | ||
1838 | #define DURATION_DEFAULT_MAX_RETRY 6 | ||
1839 | #define DURATION_MAX_RETRY 1 | ||
1840 | |||
1841 | /* only usable for MPEG-PS streams */ | ||
1842 | 69 | static void estimate_timings_from_pts(AVFormatContext *ic, int64_t old_offset) | |
1843 | { | ||
1844 | 69 | FFFormatContext *const si = ffformatcontext(ic); | |
1845 | 69 | AVPacket *const pkt = si->pkt; | |
1846 | int num, den, read_size, ret; | ||
1847 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 69 times.
|
69 | int64_t duration_max_read_size = ic->duration_probesize ? ic->duration_probesize >> DURATION_MAX_RETRY : DURATION_DEFAULT_MAX_READ_SIZE; |
1848 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 69 times.
|
69 | int duration_max_retry = ic->duration_probesize ? DURATION_MAX_RETRY : DURATION_DEFAULT_MAX_RETRY; |
1849 | 69 | int found_duration = 0; | |
1850 | int is_end; | ||
1851 | int64_t filesize, offset, duration; | ||
1852 | 69 | int retry = 0; | |
1853 | |||
1854 | /* flush packet queue */ | ||
1855 | 69 | ff_flush_packet_queue(ic); | |
1856 | |||
1857 |
2/2✓ Branch 0 taken 143 times.
✓ Branch 1 taken 69 times.
|
212 | for (unsigned i = 0; i < ic->nb_streams; i++) { |
1858 | 143 | AVStream *const st = ic->streams[i]; | |
1859 | 143 | FFStream *const sti = ffstream(st); | |
1860 | |||
1861 |
2/2✓ Branch 0 taken 22 times.
✓ Branch 1 taken 121 times.
|
143 | if (st->start_time == AV_NOPTS_VALUE && |
1862 |
1/2✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
|
22 | sti->first_dts == AV_NOPTS_VALUE && |
1863 |
1/2✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
|
22 | st->codecpar->codec_type != AVMEDIA_TYPE_UNKNOWN) |
1864 | 22 | av_log(ic, AV_LOG_WARNING, | |
1865 | "start time for stream %d is not set in estimate_timings_from_pts\n", i); | ||
1866 | |||
1867 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 119 times.
|
143 | if (sti->parser) { |
1868 | 24 | av_parser_close(sti->parser); | |
1869 | 24 | sti->parser = NULL; | |
1870 | } | ||
1871 | } | ||
1872 | |||
1873 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 69 times.
|
69 | if (ic->skip_estimate_duration_from_pts) { |
1874 | ✗ | av_log(ic, AV_LOG_INFO, "Skipping duration calculation in estimate_timings_from_pts\n"); | |
1875 | ✗ | goto skip_duration_calc; | |
1876 | } | ||
1877 | |||
1878 | 69 | av_opt_set_int(ic, "skip_changes", 1, AV_OPT_SEARCH_CHILDREN); | |
1879 | /* estimate the end time (duration) */ | ||
1880 | /* XXX: may need to support wrapping */ | ||
1881 |
1/2✓ Branch 0 taken 69 times.
✗ Branch 1 not taken.
|
69 | filesize = ic->pb ? avio_size(ic->pb) : 0; |
1882 | do { | ||
1883 | 76 | is_end = found_duration; | |
1884 | 76 | offset = filesize - (duration_max_read_size << retry); | |
1885 |
2/2✓ Branch 0 taken 26 times.
✓ Branch 1 taken 50 times.
|
76 | if (offset < 0) |
1886 | 26 | offset = 0; | |
1887 | |||
1888 | 76 | avio_seek(ic->pb, offset, SEEK_SET); | |
1889 | 76 | read_size = 0; | |
1890 | 6554 | for (;;) { | |
1891 | AVStream *st; | ||
1892 | FFStream *sti; | ||
1893 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 6623 times.
|
6630 | if (read_size >= duration_max_read_size << (FFMAX(retry - 1, 0))) |
1894 | 7 | break; | |
1895 | |||
1896 | do { | ||
1897 | 6623 | ret = ff_read_packet(ic, pkt); | |
1898 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6623 times.
|
6623 | } while (ret == AVERROR(EAGAIN)); |
1899 |
2/2✓ Branch 0 taken 69 times.
✓ Branch 1 taken 6554 times.
|
6623 | if (ret != 0) |
1900 | 69 | break; | |
1901 | 6554 | read_size += pkt->size; | |
1902 | 6554 | st = ic->streams[pkt->stream_index]; | |
1903 | 6554 | sti = ffstream(st); | |
1904 |
2/2✓ Branch 0 taken 2805 times.
✓ Branch 1 taken 3749 times.
|
6554 | if (pkt->pts != AV_NOPTS_VALUE && |
1905 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2805 times.
|
2805 | (st->start_time != AV_NOPTS_VALUE || |
1906 | ✗ | sti->first_dts != AV_NOPTS_VALUE)) { | |
1907 |
1/2✓ Branch 0 taken 2805 times.
✗ Branch 1 not taken.
|
2805 | if (pkt->duration == 0) { |
1908 | 2805 | compute_frame_duration(ic, &num, &den, st, sti->parser, pkt); | |
1909 |
3/4✓ Branch 0 taken 2345 times.
✓ Branch 1 taken 460 times.
✓ Branch 2 taken 2345 times.
✗ Branch 3 not taken.
|
2805 | if (den && num) { |
1910 | 2345 | pkt->duration = av_rescale_rnd(1, | |
1911 | 2345 | num * (int64_t) st->time_base.den, | |
1912 | 2345 | den * (int64_t) st->time_base.num, | |
1913 | AV_ROUND_DOWN); | ||
1914 | } | ||
1915 | } | ||
1916 | 2805 | duration = pkt->pts + pkt->duration; | |
1917 | 2805 | found_duration = 1; | |
1918 |
1/2✓ Branch 0 taken 2805 times.
✗ Branch 1 not taken.
|
2805 | if (st->start_time != AV_NOPTS_VALUE) |
1919 | 2805 | duration -= st->start_time; | |
1920 | else | ||
1921 | ✗ | duration -= sti->first_dts; | |
1922 |
2/2✓ Branch 0 taken 2796 times.
✓ Branch 1 taken 9 times.
|
2805 | if (duration > 0) { |
1923 |
3/4✓ Branch 0 taken 2685 times.
✓ Branch 1 taken 111 times.
✓ Branch 2 taken 2685 times.
✗ Branch 3 not taken.
|
2796 | if (st->duration == AV_NOPTS_VALUE || sti->info->last_duration<= 0 || |
1924 |
3/4✓ Branch 0 taken 2431 times.
✓ Branch 1 taken 254 times.
✓ Branch 2 taken 2431 times.
✗ Branch 3 not taken.
|
2685 | (st->duration < duration && FFABS(duration - sti->info->last_duration) < 60LL*st->time_base.den / st->time_base.num)) |
1925 | 2542 | st->duration = duration; | |
1926 | 2796 | sti->info->last_duration = duration; | |
1927 | } | ||
1928 | } | ||
1929 | 6554 | av_packet_unref(pkt); | |
1930 | } | ||
1931 | |||
1932 | /* check if all audio/video streams have valid duration */ | ||
1933 |
2/2✓ Branch 0 taken 69 times.
✓ Branch 1 taken 7 times.
|
76 | if (!is_end) { |
1934 | 69 | is_end = 1; | |
1935 |
2/2✓ Branch 0 taken 143 times.
✓ Branch 1 taken 69 times.
|
212 | for (unsigned i = 0; i < ic->nb_streams; i++) { |
1936 | 143 | const AVStream *const st = ic->streams[i]; | |
1937 |
2/2✓ Branch 0 taken 125 times.
✓ Branch 1 taken 18 times.
|
143 | switch (st->codecpar->codec_type) { |
1938 | 125 | case AVMEDIA_TYPE_VIDEO: | |
1939 | case AVMEDIA_TYPE_AUDIO: | ||
1940 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 101 times.
|
125 | if (st->duration == AV_NOPTS_VALUE) |
1941 | 24 | is_end = 0; | |
1942 | } | ||
1943 | } | ||
1944 | } | ||
1945 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 2 times.
|
9 | } while (!is_end && |
1946 |
3/4✓ Branch 0 taken 9 times.
✓ Branch 1 taken 67 times.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
|
83 | offset && |
1947 | ++retry <= duration_max_retry); | ||
1948 | |||
1949 | 69 | av_opt_set_int(ic, "skip_changes", 0, AV_OPT_SEARCH_CHILDREN); | |
1950 | |||
1951 | /* warn about audio/video streams which duration could not be estimated */ | ||
1952 |
2/2✓ Branch 0 taken 143 times.
✓ Branch 1 taken 69 times.
|
212 | for (unsigned i = 0; i < ic->nb_streams; i++) { |
1953 | 143 | const AVStream *const st = ic->streams[i]; | |
1954 | 143 | const FFStream *const sti = cffstream(st); | |
1955 | |||
1956 |
2/2✓ Branch 0 taken 32 times.
✓ Branch 1 taken 111 times.
|
143 | if (st->duration == AV_NOPTS_VALUE) { |
1957 |
2/2✓ Branch 0 taken 17 times.
✓ Branch 1 taken 15 times.
|
32 | switch (st->codecpar->codec_type) { |
1958 | 17 | case AVMEDIA_TYPE_VIDEO: | |
1959 | case AVMEDIA_TYPE_AUDIO: | ||
1960 |
3/4✓ Branch 0 taken 10 times.
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 10 times.
|
17 | if (st->start_time != AV_NOPTS_VALUE || sti->first_dts != AV_NOPTS_VALUE) { |
1961 | 7 | av_log(ic, AV_LOG_WARNING, "stream %d : no PTS found at end of file, duration not set\n", i); | |
1962 | } else | ||
1963 | 10 | av_log(ic, AV_LOG_WARNING, "stream %d : no TS found at start of file, duration not set\n", i); | |
1964 | } | ||
1965 | } | ||
1966 | } | ||
1967 | 69 | skip_duration_calc: | |
1968 | 69 | fill_all_stream_timings(ic); | |
1969 | |||
1970 | 69 | avio_seek(ic->pb, old_offset, SEEK_SET); | |
1971 |
2/2✓ Branch 0 taken 143 times.
✓ Branch 1 taken 69 times.
|
212 | for (unsigned i = 0; i < ic->nb_streams; i++) { |
1972 | 143 | AVStream *const st = ic->streams[i]; | |
1973 | 143 | FFStream *const sti = ffstream(st); | |
1974 | |||
1975 | 143 | sti->cur_dts = sti->first_dts; | |
1976 | 143 | sti->last_IP_pts = AV_NOPTS_VALUE; | |
1977 | 143 | sti->last_dts_for_order_check = AV_NOPTS_VALUE; | |
1978 |
2/2✓ Branch 0 taken 2431 times.
✓ Branch 1 taken 143 times.
|
2574 | for (int j = 0; j < MAX_REORDER_DELAY + 1; j++) |
1979 | 2431 | sti->pts_buffer[j] = AV_NOPTS_VALUE; | |
1980 | } | ||
1981 | 69 | } | |
1982 | |||
1983 | /* 1:1 map to AVDurationEstimationMethod */ | ||
1984 | static const char *const duration_name[] = { | ||
1985 | [AVFMT_DURATION_FROM_PTS] = "pts", | ||
1986 | [AVFMT_DURATION_FROM_STREAM] = "stream", | ||
1987 | [AVFMT_DURATION_FROM_BITRATE] = "bit rate", | ||
1988 | }; | ||
1989 | |||
1990 | 7535 | static const char *duration_estimate_name(enum AVDurationEstimationMethod method) | |
1991 | { | ||
1992 | 7535 | return duration_name[method]; | |
1993 | } | ||
1994 | |||
1995 | 7535 | static void estimate_timings(AVFormatContext *ic, int64_t old_offset) | |
1996 | { | ||
1997 | int64_t file_size; | ||
1998 | |||
1999 | /* get the file size, if possible */ | ||
2000 |
2/2✓ Branch 0 taken 3061 times.
✓ Branch 1 taken 4474 times.
|
7535 | if (ic->iformat->flags & AVFMT_NOFILE) { |
2001 | 3061 | file_size = 0; | |
2002 | } else { | ||
2003 | 4474 | file_size = avio_size(ic->pb); | |
2004 | 4474 | file_size = FFMAX(0, file_size); | |
2005 | } | ||
2006 | |||
2007 |
2/2✓ Branch 0 taken 7510 times.
✓ Branch 1 taken 25 times.
|
7535 | if ((!strcmp(ic->iformat->name, "mpeg") || |
2008 |
3/4✓ Branch 0 taken 44 times.
✓ Branch 1 taken 7466 times.
✓ Branch 2 taken 69 times.
✗ Branch 3 not taken.
|
7535 | !strcmp(ic->iformat->name, "mpegts")) && |
2009 |
1/2✓ Branch 0 taken 69 times.
✗ Branch 1 not taken.
|
69 | file_size && (ic->pb->seekable & AVIO_SEEKABLE_NORMAL)) { |
2010 | /* get accurate estimate from the PTSes */ | ||
2011 | 69 | estimate_timings_from_pts(ic, old_offset); | |
2012 | 69 | ic->duration_estimation_method = AVFMT_DURATION_FROM_PTS; | |
2013 |
2/2✓ Branch 1 taken 5488 times.
✓ Branch 2 taken 1978 times.
|
7466 | } else if (has_duration(ic)) { |
2014 | /* at least one component has timings - we use them for all | ||
2015 | * the components */ | ||
2016 | 5488 | fill_all_stream_timings(ic); | |
2017 | /* nut demuxer estimate the duration from PTS */ | ||
2018 |
2/2✓ Branch 0 taken 30 times.
✓ Branch 1 taken 5458 times.
|
5488 | if (!strcmp(ic->iformat->name, "nut")) |
2019 | 30 | ic->duration_estimation_method = AVFMT_DURATION_FROM_PTS; | |
2020 | else | ||
2021 | 5458 | ic->duration_estimation_method = AVFMT_DURATION_FROM_STREAM; | |
2022 | } else { | ||
2023 | /* less precise: use bitrate info */ | ||
2024 | 1978 | estimate_timings_from_bit_rate(ic); | |
2025 | 1978 | ic->duration_estimation_method = AVFMT_DURATION_FROM_BITRATE; | |
2026 | } | ||
2027 | 7535 | update_stream_timings(ic); | |
2028 | |||
2029 |
2/2✓ Branch 0 taken 8344 times.
✓ Branch 1 taken 7535 times.
|
15879 | for (unsigned i = 0; i < ic->nb_streams; i++) { |
2030 | 8344 | AVStream *const st = ic->streams[i]; | |
2031 |
1/2✓ Branch 0 taken 8344 times.
✗ Branch 1 not taken.
|
8344 | if (st->time_base.den) |
2032 | 8344 | av_log(ic, AV_LOG_TRACE, "stream %u: start_time: %s duration: %s\n", i, | |
2033 | 8344 | av_ts2timestr(st->start_time, &st->time_base), | |
2034 | 8344 | av_ts2timestr(st->duration, &st->time_base)); | |
2035 | } | ||
2036 | 7535 | av_log(ic, AV_LOG_TRACE, | |
2037 | "format: start_time: %s duration: %s (estimate from %s) bitrate=%"PRId64" kb/s\n", | ||
2038 | 7535 | av_ts2timestr(ic->start_time, &AV_TIME_BASE_Q), | |
2039 | 7535 | av_ts2timestr(ic->duration, &AV_TIME_BASE_Q), | |
2040 | duration_estimate_name(ic->duration_estimation_method), | ||
2041 | 7535 | (int64_t)ic->bit_rate / 1000); | |
2042 | 7535 | } | |
2043 | |||
2044 | 106265 | static int determinable_frame_size(const AVCodecContext *avctx) | |
2045 | { | ||
2046 |
2/2✓ Branch 0 taken 2187 times.
✓ Branch 1 taken 104078 times.
|
106265 | switch(avctx->codec_id) { |
2047 | 2187 | case AV_CODEC_ID_MP1: | |
2048 | case AV_CODEC_ID_MP2: | ||
2049 | case AV_CODEC_ID_MP3: | ||
2050 | case AV_CODEC_ID_CODEC2: | ||
2051 | 2187 | return 1; | |
2052 | } | ||
2053 | |||
2054 | 104078 | return 0; | |
2055 | } | ||
2056 | |||
2057 | 422123 | static int has_codec_parameters(const AVStream *st, const char **errmsg_ptr) | |
2058 | { | ||
2059 | 422123 | const FFStream *const sti = cffstream(st); | |
2060 | 422123 | const AVCodecContext *const avctx = sti->avctx; | |
2061 | |||
2062 | #define FAIL(errmsg) do { \ | ||
2063 | if (errmsg_ptr) \ | ||
2064 | *errmsg_ptr = errmsg; \ | ||
2065 | return 0; \ | ||
2066 | } while (0) | ||
2067 | |||
2068 |
2/2✓ Branch 0 taken 644 times.
✓ Branch 1 taken 421479 times.
|
422123 | if ( avctx->codec_id == AV_CODEC_ID_NONE |
2069 |
2/2✓ Branch 0 taken 419 times.
✓ Branch 1 taken 225 times.
|
644 | && avctx->codec_type != AVMEDIA_TYPE_DATA) |
2070 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 414 times.
|
419 | FAIL("unknown codec"); |
2071 |
4/5✓ Branch 0 taken 121624 times.
✓ Branch 1 taken 298538 times.
✓ Branch 2 taken 629 times.
✓ Branch 3 taken 913 times.
✗ Branch 4 not taken.
|
421704 | switch (avctx->codec_type) { |
2072 | 121624 | case AVMEDIA_TYPE_AUDIO: | |
2073 |
4/4✓ Branch 0 taken 106265 times.
✓ Branch 1 taken 15359 times.
✓ Branch 3 taken 2187 times.
✓ Branch 4 taken 104078 times.
|
121624 | if (!avctx->frame_size && determinable_frame_size(avctx)) |
2074 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 2175 times.
|
2187 | FAIL("unspecified frame size"); |
2075 |
1/2✓ Branch 0 taken 119437 times.
✗ Branch 1 not taken.
|
119437 | if (sti->info->found_decoder >= 0 && |
2076 |
2/2✓ Branch 0 taken 2432 times.
✓ Branch 1 taken 117005 times.
|
119437 | avctx->sample_fmt == AV_SAMPLE_FMT_NONE) |
2077 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2432 times.
|
2432 | FAIL("unspecified sample format"); |
2078 |
2/2✓ Branch 0 taken 137 times.
✓ Branch 1 taken 116868 times.
|
117005 | if (!avctx->sample_rate) |
2079 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 131 times.
|
137 | FAIL("unspecified sample rate"); |
2080 |
2/2✓ Branch 0 taken 17 times.
✓ Branch 1 taken 116851 times.
|
116868 | if (!avctx->ch_layout.nb_channels) |
2081 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
|
17 | FAIL("unspecified number of channels"); |
2082 |
4/6✓ Branch 0 taken 116851 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 80775 times.
✓ Branch 3 taken 36076 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 80775 times.
|
116851 | if (sti->info->found_decoder >= 0 && !sti->nb_decoded_frames && avctx->codec_id == AV_CODEC_ID_DTS) |
2083 | ✗ | FAIL("no decodable DTS frames"); | |
2084 | 116851 | break; | |
2085 | 298538 | case AVMEDIA_TYPE_VIDEO: | |
2086 |
2/2✓ Branch 0 taken 12725 times.
✓ Branch 1 taken 285813 times.
|
298538 | if (!avctx->width) |
2087 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 12715 times.
|
12725 | FAIL("unspecified size"); |
2088 |
4/4✓ Branch 0 taken 285758 times.
✓ Branch 1 taken 55 times.
✓ Branch 2 taken 4222 times.
✓ Branch 3 taken 281536 times.
|
285813 | if (sti->info->found_decoder >= 0 && avctx->pix_fmt == AV_PIX_FMT_NONE) |
2089 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 4219 times.
|
4222 | FAIL("unspecified pixel format"); |
2090 |
4/4✓ Branch 0 taken 281491 times.
✓ Branch 1 taken 100 times.
✓ Branch 2 taken 86 times.
✓ Branch 3 taken 281405 times.
|
281591 | if (st->codecpar->codec_id == AV_CODEC_ID_RV30 || st->codecpar->codec_id == AV_CODEC_ID_RV40) |
2091 |
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) |
2092 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 67 times.
|
67 | FAIL("no frame in rv30/40 and no sar"); |
2093 | 281524 | break; | |
2094 | 629 | case AVMEDIA_TYPE_SUBTITLE: | |
2095 |
4/4✓ Branch 0 taken 35 times.
✓ Branch 1 taken 594 times.
✓ Branch 2 taken 21 times.
✓ Branch 3 taken 14 times.
|
629 | if (avctx->codec_id == AV_CODEC_ID_HDMV_PGS_SUBTITLE && !avctx->width) |
2096 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
|
21 | FAIL("unspecified size"); |
2097 | 608 | break; | |
2098 | 913 | case AVMEDIA_TYPE_DATA: | |
2099 |
2/2✓ Branch 0 taken 225 times.
✓ Branch 1 taken 688 times.
|
913 | if (avctx->codec_id == AV_CODEC_ID_NONE) return 1; |
2100 | } | ||
2101 | |||
2102 | 399671 | return 1; | |
2103 | } | ||
2104 | |||
2105 | /* returns 1 or 0 if or if not decoded data was returned, or a negative error */ | ||
2106 | 197283 | static int try_decode_frame(AVFormatContext *s, AVStream *st, | |
2107 | const AVPacket *pkt, AVDictionary **options) | ||
2108 | { | ||
2109 | 197283 | FFStream *const sti = ffstream(st); | |
2110 | 197283 | AVCodecContext *const avctx = sti->avctx; | |
2111 | const AVCodec *codec; | ||
2112 | 197283 | int got_picture = 1, ret = 0; | |
2113 | 197283 | AVFrame *frame = av_frame_alloc(); | |
2114 | AVSubtitle subtitle; | ||
2115 | 197283 | int do_skip_frame = 0; | |
2116 | enum AVDiscard skip_frame; | ||
2117 | 197283 | int pkt_to_send = pkt->size > 0; | |
2118 | |||
2119 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 197283 times.
|
197283 | if (!frame) |
2120 | ✗ | return AVERROR(ENOMEM); | |
2121 | |||
2122 |
2/2✓ Branch 1 taken 3392 times.
✓ Branch 2 taken 193891 times.
|
197283 | if (!avcodec_is_open(avctx) && |
2123 |
1/2✓ Branch 0 taken 3392 times.
✗ Branch 1 not taken.
|
3392 | sti->info->found_decoder <= 0 && |
2124 |
4/4✓ Branch 0 taken 2024 times.
✓ Branch 1 taken 1368 times.
✓ Branch 2 taken 63 times.
✓ Branch 3 taken 1961 times.
|
3392 | (st->codecpar->codec_id != -sti->info->found_decoder || !st->codecpar->codec_id)) { |
2125 | 1431 | AVDictionary *thread_opt = NULL; | |
2126 | |||
2127 | 1431 | codec = find_probe_decoder(s, st, st->codecpar->codec_id); | |
2128 | |||
2129 |
2/2✓ Branch 0 taken 76 times.
✓ Branch 1 taken 1355 times.
|
1431 | if (!codec) { |
2130 | 76 | sti->info->found_decoder = -st->codecpar->codec_id; | |
2131 | 76 | ret = -1; | |
2132 | 88 | goto fail; | |
2133 | } | ||
2134 | |||
2135 | /* Force thread count to 1 since the H.264 decoder will not extract | ||
2136 | * SPS and PPS to extradata during multi-threaded decoding. */ | ||
2137 |
2/2✓ Branch 0 taken 161 times.
✓ Branch 1 taken 1194 times.
|
1355 | av_dict_set(options ? options : &thread_opt, "threads", "1", 0); |
2138 | /* Force lowres to 0. The decoder might reduce the video size by the | ||
2139 | * lowres factor, and we don't want that propagated to the stream's | ||
2140 | * codecpar */ | ||
2141 |
2/2✓ Branch 0 taken 161 times.
✓ Branch 1 taken 1194 times.
|
1355 | av_dict_set(options ? options : &thread_opt, "lowres", "0", 0); |
2142 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1355 times.
|
1355 | if (s->codec_whitelist) |
2143 | ✗ | av_dict_set(options ? options : &thread_opt, "codec_whitelist", s->codec_whitelist, 0); | |
2144 |
2/2✓ Branch 0 taken 161 times.
✓ Branch 1 taken 1194 times.
|
1355 | ret = avcodec_open2(avctx, codec, options ? options : &thread_opt); |
2145 |
2/2✓ Branch 0 taken 161 times.
✓ Branch 1 taken 1194 times.
|
1355 | if (!options) |
2146 | 161 | av_dict_free(&thread_opt); | |
2147 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 1343 times.
|
1355 | if (ret < 0) { |
2148 | 12 | sti->info->found_decoder = -avctx->codec_id; | |
2149 | 12 | goto fail; | |
2150 | } | ||
2151 | 1343 | sti->info->found_decoder = 1; | |
2152 |
2/2✓ Branch 0 taken 6711 times.
✓ Branch 1 taken 189141 times.
|
195852 | } else if (!sti->info->found_decoder) |
2153 | 6711 | sti->info->found_decoder = 1; | |
2154 | |||
2155 |
2/2✓ Branch 0 taken 1961 times.
✓ Branch 1 taken 195234 times.
|
197195 | if (sti->info->found_decoder < 0) { |
2156 | 1961 | ret = -1; | |
2157 | 1961 | goto fail; | |
2158 | } | ||
2159 | |||
2160 |
2/2✓ Branch 1 taken 107473 times.
✓ Branch 2 taken 87761 times.
|
195234 | if (avpriv_codec_get_cap_skip_frame_fill_param(avctx->codec)) { |
2161 | 107473 | do_skip_frame = 1; | |
2162 | 107473 | skip_frame = avctx->skip_frame; | |
2163 | 107473 | avctx->skip_frame = AVDISCARD_ALL; | |
2164 | } | ||
2165 | |||
2166 |
5/6✓ Branch 0 taken 7960 times.
✓ Branch 1 taken 4548 times.
✓ Branch 2 taken 26 times.
✓ Branch 3 taken 4522 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 195242 times.
|
398462 | while ((pkt_to_send || (!pkt->data && got_picture)) && |
2167 |
4/4✓ Branch 0 taken 12508 times.
✓ Branch 1 taken 190720 times.
✓ Branch 2 taken 5536 times.
✓ Branch 3 taken 189706 times.
|
398470 | ret >= 0 && |
2168 |
2/2✓ Branch 2 taken 2241 times.
✓ Branch 3 taken 187465 times.
|
384948 | (!has_codec_parameters(st, NULL) || !has_decode_delay_been_guessed(st) || |
2169 |
2/2✓ Branch 0 taken 184660 times.
✓ Branch 1 taken 2805 times.
|
187465 | (!sti->codec_info_nb_frames && |
2170 |
2/2✓ Branch 0 taken 457 times.
✓ Branch 1 taken 2348 times.
|
2805 | (avctx->codec->capabilities & AV_CODEC_CAP_CHANNEL_CONF)))) { |
2171 | 8234 | got_picture = 0; | |
2172 |
2/2✓ Branch 0 taken 730 times.
✓ Branch 1 taken 7504 times.
|
8234 | if (avctx->codec_type == AVMEDIA_TYPE_VIDEO || |
2173 |
2/2✓ Branch 0 taken 723 times.
✓ Branch 1 taken 7 times.
|
730 | avctx->codec_type == AVMEDIA_TYPE_AUDIO) { |
2174 | 8227 | ret = avcodec_send_packet(avctx, pkt); | |
2175 |
5/6✓ Branch 0 taken 249 times.
✓ Branch 1 taken 7978 times.
✓ Branch 2 taken 249 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 240 times.
✓ Branch 5 taken 9 times.
|
8227 | if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF) |
2176 | 240 | break; | |
2177 |
2/2✓ Branch 0 taken 7978 times.
✓ Branch 1 taken 9 times.
|
7987 | if (ret >= 0) |
2178 | 7978 | pkt_to_send = 0; | |
2179 | 7987 | ret = avcodec_receive_frame(avctx, frame); | |
2180 |
2/2✓ Branch 0 taken 3196 times.
✓ Branch 1 taken 4791 times.
|
7987 | if (ret >= 0) |
2181 | 3196 | got_picture = 1; | |
2182 |
4/4✓ Branch 0 taken 3222 times.
✓ Branch 1 taken 4765 times.
✓ Branch 2 taken 26 times.
✓ Branch 3 taken 3196 times.
|
7987 | if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) |
2183 | 4791 | ret = 0; | |
2184 |
1/2✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
|
7 | } else if (avctx->codec_type == AVMEDIA_TYPE_SUBTITLE) { |
2185 | 7 | ret = avcodec_decode_subtitle2(avctx, &subtitle, | |
2186 | &got_picture, pkt); | ||
2187 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 5 times.
|
7 | if (got_picture) |
2188 | 2 | avsubtitle_free(&subtitle); | |
2189 |
1/2✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
|
7 | if (ret >= 0) |
2190 | 7 | pkt_to_send = 0; | |
2191 | } | ||
2192 |
1/2✓ Branch 0 taken 7994 times.
✗ Branch 1 not taken.
|
7994 | if (ret >= 0) { |
2193 |
2/2✓ Branch 0 taken 3198 times.
✓ Branch 1 taken 4796 times.
|
7994 | if (got_picture) |
2194 | 3198 | sti->nb_decoded_frames++; | |
2195 | 7994 | ret = got_picture; | |
2196 | } | ||
2197 | } | ||
2198 | |||
2199 | 194994 | fail: | |
2200 |
2/2✓ Branch 0 taken 107473 times.
✓ Branch 1 taken 89810 times.
|
197283 | if (do_skip_frame) { |
2201 | 107473 | avctx->skip_frame = skip_frame; | |
2202 | } | ||
2203 | |||
2204 | 197283 | av_frame_free(&frame); | |
2205 | 197283 | return ret; | |
2206 | } | ||
2207 | |||
2208 | 57 | static int chapter_start_cmp(const void *p1, const void *p2) | |
2209 | { | ||
2210 | 57 | const AVChapter *const ch1 = *(AVChapter**)p1; | |
2211 | 57 | const AVChapter *const ch2 = *(AVChapter**)p2; | |
2212 | 57 | int delta = av_compare_ts(ch1->start, ch1->time_base, ch2->start, ch2->time_base); | |
2213 |
1/2✓ Branch 0 taken 57 times.
✗ Branch 1 not taken.
|
57 | if (delta) |
2214 | 57 | return delta; | |
2215 | ✗ | return FFDIFFSIGN(ch1->id, ch2->id); | |
2216 | } | ||
2217 | |||
2218 | 7535 | static int compute_chapters_end(AVFormatContext *s) | |
2219 | { | ||
2220 | 7535 | int64_t max_time = 0; | |
2221 | AVChapter **timetable; | ||
2222 | |||
2223 |
2/2✓ Branch 0 taken 7509 times.
✓ Branch 1 taken 26 times.
|
7535 | if (!s->nb_chapters) |
2224 | 7509 | return 0; | |
2225 | |||
2226 |
2/4✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 26 times.
✗ Branch 3 not taken.
|
26 | if (s->duration > 0 && s->start_time < INT64_MAX - s->duration) |
2227 | 26 | max_time = s->duration + | |
2228 |
2/2✓ Branch 0 taken 23 times.
✓ Branch 1 taken 3 times.
|
26 | ((s->start_time == AV_NOPTS_VALUE) ? 0 : s->start_time); |
2229 | |||
2230 | 26 | timetable = av_memdup(s->chapters, s->nb_chapters * sizeof(*timetable)); | |
2231 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
|
26 | if (!timetable) |
2232 | ✗ | return AVERROR(ENOMEM); | |
2233 | 26 | qsort(timetable, s->nb_chapters, sizeof(*timetable), chapter_start_cmp); | |
2234 | |||
2235 |
2/2✓ Branch 0 taken 71 times.
✓ Branch 1 taken 26 times.
|
97 | for (unsigned i = 0; i < s->nb_chapters; i++) |
2236 |
2/2✓ Branch 0 taken 40 times.
✓ Branch 1 taken 31 times.
|
71 | if (timetable[i]->end == AV_NOPTS_VALUE) { |
2237 | 40 | AVChapter *const ch = timetable[i]; | |
2238 | 40 | int64_t end = max_time ? av_rescale_q(max_time, AV_TIME_BASE_Q, | |
2239 | ch->time_base) | ||
2240 |
1/2✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
|
40 | : INT64_MAX; |
2241 | |||
2242 |
2/2✓ Branch 0 taken 21 times.
✓ Branch 1 taken 19 times.
|
40 | if (i + 1 < s->nb_chapters) { |
2243 | 21 | const AVChapter *const ch1 = timetable[i + 1]; | |
2244 | 21 | int64_t next_start = av_rescale_q(ch1->start, ch1->time_base, | |
2245 | ch->time_base); | ||
2246 |
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) |
2247 | 21 | end = next_start; | |
2248 | } | ||
2249 |
2/4✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 40 times.
|
40 | ch->end = (end == INT64_MAX || end < ch->start) ? ch->start : end; |
2250 | } | ||
2251 | 26 | av_free(timetable); | |
2252 | 26 | return 0; | |
2253 | } | ||
2254 | |||
2255 | 18701498 | static int get_std_framerate(int i) | |
2256 | { | ||
2257 |
2/2✓ Branch 0 taken 16990597 times.
✓ Branch 1 taken 1710901 times.
|
18701498 | if (i < 30*12) |
2258 | 16990597 | return (i + 1) * 1001; | |
2259 | 1710901 | i -= 30*12; | |
2260 | |||
2261 |
2/2✓ Branch 0 taken 1301099 times.
✓ Branch 1 taken 409802 times.
|
1710901 | if (i < 30) |
2262 | 1301099 | return (i + 31) * 1001 * 12; | |
2263 | 409802 | i -= 30; | |
2264 | |||
2265 |
2/2✓ Branch 0 taken 130623 times.
✓ Branch 1 taken 279179 times.
|
409802 | if (i < 3) |
2266 | 130623 | return ((const int[]) { 80, 120, 240})[i] * 1001 * 12; | |
2267 | |||
2268 | 279179 | i -= 3; | |
2269 | |||
2270 | 279179 | return ((const int[]) { 24, 30, 60, 12, 15, 48 })[i] * 1000 * 12; | |
2271 | } | ||
2272 | |||
2273 | /* Is the time base unreliable? | ||
2274 | * This is a heuristic to balance between quick acceptance of the values in | ||
2275 | * the headers vs. some extra checks. | ||
2276 | * Old DivX and Xvid often have nonsense timebases like 1fps or 2fps. | ||
2277 | * MPEG-2 commonly misuses field repeat flags to store different framerates. | ||
2278 | * And there are "variable" fps files this needs to detect as well. */ | ||
2279 | 206592 | static int tb_unreliable(AVFormatContext *ic, AVStream *st) | |
2280 | { | ||
2281 | 206592 | FFStream *const sti = ffstream(st); | |
2282 | 206592 | const AVCodecDescriptor *desc = sti->codec_desc; | |
2283 | 206592 | AVCodecContext *c = sti->avctx; | |
2284 |
4/4✓ Branch 0 taken 206140 times.
✓ Branch 1 taken 452 times.
✓ Branch 2 taken 21029 times.
✓ Branch 3 taken 185111 times.
|
206592 | AVRational mul = (AVRational){ desc && (desc->props & AV_CODEC_PROP_FIELDS) ? 2 : 1, 1 }; |
2285 | 206592 | AVRational time_base = c->framerate.num ? av_inv_q(av_mul_q(c->framerate, mul)) | |
2286 | /* NOHEADER check added to not break existing behavior */ | ||
2287 |
2/2✓ Branch 0 taken 17678 times.
✓ Branch 1 taken 188914 times.
|
206592 | : (((ic->ctx_flags & AVFMTCTX_NOHEADER) || |
2288 |
2/2✓ Branch 0 taken 44400 times.
✓ Branch 1 taken 30359 times.
|
74759 | st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) ? (AVRational){0, 1} |
2289 |
2/2✓ Branch 0 taken 74759 times.
✓ Branch 1 taken 114155 times.
|
188914 | : st->time_base); |
2290 | |||
2291 |
2/2✓ Branch 0 taken 23956 times.
✓ Branch 1 taken 182636 times.
|
206592 | if (time_base.den >= 101LL * time_base.num || |
2292 |
2/2✓ Branch 0 taken 23548 times.
✓ Branch 1 taken 408 times.
|
23956 | time_base.den < 5LL * time_base.num || |
2293 | // c->codec_tag == AV_RL32("DIVX") || | ||
2294 | // c->codec_tag == AV_RL32("XVID") || | ||
2295 |
2/2✓ Branch 0 taken 23449 times.
✓ Branch 1 taken 99 times.
|
23548 | c->codec_tag == AV_RL32("mp4v") || |
2296 |
2/2✓ Branch 0 taken 14620 times.
✓ Branch 1 taken 8829 times.
|
23449 | c->codec_id == AV_CODEC_ID_MPEG2VIDEO || |
2297 |
2/2✓ Branch 0 taken 14090 times.
✓ Branch 1 taken 530 times.
|
14620 | c->codec_id == AV_CODEC_ID_GIF || |
2298 |
2/2✓ Branch 0 taken 13385 times.
✓ Branch 1 taken 705 times.
|
14090 | c->codec_id == AV_CODEC_ID_HEVC || |
2299 |
2/2✓ Branch 0 taken 3483 times.
✓ Branch 1 taken 9902 times.
|
13385 | c->codec_id == AV_CODEC_ID_H264) |
2300 | 196690 | return 1; | |
2301 | 9902 | return 0; | |
2302 | } | ||
2303 | |||
2304 | 141409 | int ff_rfps_add_frame(AVFormatContext *ic, AVStream *st, int64_t ts) | |
2305 | { | ||
2306 | 141409 | FFStream *const sti = ffstream(st); | |
2307 | 141409 | FFStreamInfo *info = sti->info; | |
2308 | 141409 | int64_t last = info->last_dts; | |
2309 | |||
2310 |
6/6✓ Branch 0 taken 125315 times.
✓ Branch 1 taken 16094 times.
✓ Branch 2 taken 119345 times.
✓ Branch 3 taken 5970 times.
✓ Branch 4 taken 119317 times.
✓ Branch 5 taken 28 times.
|
141409 | if ( ts != AV_NOPTS_VALUE && last != AV_NOPTS_VALUE && ts > last |
2311 |
1/2✓ Branch 0 taken 119317 times.
✗ Branch 1 not taken.
|
119317 | && ts - (uint64_t)last < INT64_MAX) { |
2312 |
2/2✓ Branch 1 taken 6511 times.
✓ Branch 2 taken 112806 times.
|
119317 | double dts = (is_relative(ts) ? ts - RELATIVE_TS_BASE : ts) * av_q2d(st->time_base); |
2313 | 119317 | int64_t duration = ts - last; | |
2314 | |||
2315 |
2/2✓ Branch 0 taken 3889 times.
✓ Branch 1 taken 115428 times.
|
119317 | if (!info->duration_error) |
2316 | 3889 | info->duration_error = av_mallocz(sizeof(info->duration_error[0])*2); | |
2317 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 119317 times.
|
119317 | if (!info->duration_error) |
2318 | ✗ | return AVERROR(ENOMEM); | |
2319 | |||
2320 | // if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) | ||
2321 | // av_log(NULL, AV_LOG_ERROR, "%f\n", dts); | ||
2322 |
2/2✓ Branch 0 taken 47607483 times.
✓ Branch 1 taken 119317 times.
|
47726800 | for (int i = 0; i < MAX_STD_TIMEBASES; i++) { |
2323 |
2/2✓ Branch 0 taken 18334822 times.
✓ Branch 1 taken 29272661 times.
|
47607483 | if (info->duration_error[0][1][i] < 1e10) { |
2324 | 18334822 | int framerate = get_std_framerate(i); | |
2325 | 18334822 | double sdts = dts*framerate/(1001*12); | |
2326 |
2/2✓ Branch 0 taken 36669644 times.
✓ Branch 1 taken 18334822 times.
|
55004466 | for (int j = 0; j < 2; j++) { |
2327 | 36669644 | int64_t ticks = llrint(sdts+j*0.5); | |
2328 | 36669644 | double error = sdts - ticks + j*0.5; | |
2329 | 36669644 | info->duration_error[j][0][i] += error; | |
2330 | 36669644 | info->duration_error[j][1][i] += error*error; | |
2331 | } | ||
2332 | } | ||
2333 | } | ||
2334 |
1/2✓ Branch 0 taken 119317 times.
✗ Branch 1 not taken.
|
119317 | if (info->rfps_duration_sum <= INT64_MAX - duration) { |
2335 | 119317 | info->duration_count++; | |
2336 | 119317 | info->rfps_duration_sum += duration; | |
2337 | } | ||
2338 | |||
2339 |
2/2✓ Branch 0 taken 10857 times.
✓ Branch 1 taken 108460 times.
|
119317 | if (info->duration_count % 10 == 0) { |
2340 | 10857 | int n = info->duration_count; | |
2341 |
2/2✓ Branch 0 taken 4331943 times.
✓ Branch 1 taken 10857 times.
|
4342800 | for (int i = 0; i < MAX_STD_TIMEBASES; i++) { |
2342 |
2/2✓ Branch 0 taken 1755913 times.
✓ Branch 1 taken 2576030 times.
|
4331943 | if (info->duration_error[0][1][i] < 1e10) { |
2343 | 1755913 | double a0 = info->duration_error[0][0][i] / n; | |
2344 | 1755913 | double error0 = info->duration_error[0][1][i] / n - a0*a0; | |
2345 | 1755913 | double a1 = info->duration_error[1][0][i] / n; | |
2346 | 1755913 | double error1 = info->duration_error[1][1][i] / n - a1*a1; | |
2347 |
4/4✓ Branch 0 taken 1437955 times.
✓ Branch 1 taken 317958 times.
✓ Branch 2 taken 1342058 times.
✓ Branch 3 taken 95897 times.
|
1755913 | if (error0 > 0.04 && error1 > 0.04) { |
2348 | 1342058 | info->duration_error[0][1][i] = 2e10; | |
2349 | 1342058 | info->duration_error[1][1][i] = 2e10; | |
2350 | } | ||
2351 | } | ||
2352 | } | ||
2353 | } | ||
2354 | |||
2355 | // ignore the first 4 values, they might have some random jitter | ||
2356 |
3/4✓ Branch 0 taken 107736 times.
✓ Branch 1 taken 11581 times.
✓ Branch 4 taken 107736 times.
✗ Branch 5 not taken.
|
119317 | if (info->duration_count > 3 && is_relative(ts) == is_relative(last)) |
2357 | 107736 | info->duration_gcd = av_gcd(info->duration_gcd, duration); | |
2358 | } | ||
2359 |
2/2✓ Branch 0 taken 125315 times.
✓ Branch 1 taken 16094 times.
|
141409 | if (ts != AV_NOPTS_VALUE) |
2360 | 125315 | info->last_dts = ts; | |
2361 | |||
2362 | 141409 | return 0; | |
2363 | } | ||
2364 | |||
2365 | 8028 | void ff_rfps_calculate(AVFormatContext *ic) | |
2366 | { | ||
2367 |
2/2✓ Branch 0 taken 9015 times.
✓ Branch 1 taken 8028 times.
|
17043 | for (unsigned i = 0; i < ic->nb_streams; i++) { |
2368 | 9015 | AVStream *const st = ic->streams[i]; | |
2369 | 9015 | FFStream *const sti = ffstream(st); | |
2370 | |||
2371 |
2/2✓ Branch 0 taken 2460 times.
✓ Branch 1 taken 6555 times.
|
9015 | if (st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO) |
2372 | 2460 | continue; | |
2373 | // the check for tb_unreliable() is not completely correct, since this is not about handling | ||
2374 | // an unreliable/inexact time base, but a time base that is finer than necessary, as e.g. | ||
2375 | // ipmovie.c produces. | ||
2376 |
8/8✓ Branch 1 taken 5180 times.
✓ Branch 2 taken 1375 times.
✓ Branch 3 taken 3365 times.
✓ Branch 4 taken 1815 times.
✓ Branch 5 taken 295 times.
✓ Branch 6 taken 3070 times.
✓ Branch 7 taken 144 times.
✓ Branch 8 taken 151 times.
|
6555 | 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 && |
2377 |
1/2✓ Branch 0 taken 144 times.
✗ Branch 1 not taken.
|
144 | sti->info->duration_gcd < INT64_MAX / st->time_base.num) |
2378 | 144 | av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den, st->time_base.den, st->time_base.num * sti->info->duration_gcd, INT_MAX); | |
2379 |
4/4✓ Branch 0 taken 3852 times.
✓ Branch 1 taken 2703 times.
✓ Branch 2 taken 407 times.
✓ Branch 3 taken 3445 times.
|
6555 | if (sti->info->duration_count > 1 && !st->r_frame_rate.num |
2380 |
2/2✓ Branch 1 taken 303 times.
✓ Branch 2 taken 104 times.
|
407 | && tb_unreliable(ic, st)) { |
2381 | 303 | int num = 0; | |
2382 | 303 | double best_error = 0.01; | |
2383 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 303 times.
|
303 | AVRational ref_rate = st->r_frame_rate.num ? st->r_frame_rate : av_inv_q(st->time_base); |
2384 | |||
2385 |
2/2✓ Branch 0 taken 120897 times.
✓ Branch 1 taken 303 times.
|
121200 | for (int j = 0; j < MAX_STD_TIMEBASES; j++) { |
2386 |
2/2✓ Branch 0 taken 89775 times.
✓ Branch 1 taken 31122 times.
|
120897 | if (sti->info->codec_info_duration && |
2387 |
2/2✓ Branch 2 taken 9919 times.
✓ Branch 3 taken 79856 times.
|
89775 | sti->info->codec_info_duration*av_q2d(st->time_base) < (1001*11.5)/get_std_framerate(j)) |
2388 | 9919 | continue; | |
2389 |
4/4✓ Branch 0 taken 31122 times.
✓ Branch 1 taken 79856 times.
✓ Branch 3 taken 858 times.
✓ Branch 4 taken 30264 times.
|
110978 | if (!sti->info->codec_info_duration && get_std_framerate(j) < 1001*12) |
2390 | 858 | continue; | |
2391 | |||
2392 |
2/2✓ Branch 2 taken 51873 times.
✓ Branch 3 taken 58247 times.
|
110120 | if (av_q2d(st->time_base) * sti->info->rfps_duration_sum / sti->info->duration_count < (1001*12.0 * 0.8)/get_std_framerate(j)) |
2393 | 51873 | continue; | |
2394 | |||
2395 |
2/2✓ Branch 0 taken 116494 times.
✓ Branch 1 taken 58247 times.
|
174741 | for (int k = 0; k < 2; k++) { |
2396 | 116494 | int n = sti->info->duration_count; | |
2397 | 116494 | double a = sti->info->duration_error[k][0][j] / n; | |
2398 | 116494 | double error = sti->info->duration_error[k][1][j]/n - a*a; | |
2399 | |||
2400 |
4/4✓ Branch 0 taken 4505 times.
✓ Branch 1 taken 111989 times.
✓ Branch 2 taken 4434 times.
✓ Branch 3 taken 71 times.
|
116494 | if (error < best_error && best_error> 0.000000001) { |
2401 | 4434 | best_error= error; | |
2402 | 4434 | num = get_std_framerate(j); | |
2403 | } | ||
2404 |
2/2✓ Branch 0 taken 23894 times.
✓ Branch 1 taken 92600 times.
|
116494 | if (error < 0.02) |
2405 | 23894 | av_log(ic, AV_LOG_DEBUG, "rfps: %f %f\n", get_std_framerate(j) / 12.0/1001, error); | |
2406 | } | ||
2407 | } | ||
2408 | // do not increase frame rate by more than 1 % in order to match a standard rate. | ||
2409 |
5/6✓ Branch 0 taken 297 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 297 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 295 times.
✓ Branch 6 taken 2 times.
|
303 | if (num && (!ref_rate.num || (double)num/(12*1001) < 1.01 * av_q2d(ref_rate))) |
2410 | 295 | av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den, num, 12*1001, INT_MAX); | |
2411 | } | ||
2412 |
2/2✓ Branch 0 taken 1090 times.
✓ Branch 1 taken 5465 times.
|
6555 | if ( !st->avg_frame_rate.num |
2413 |
4/4✓ Branch 0 taken 284 times.
✓ Branch 1 taken 806 times.
✓ Branch 2 taken 280 times.
✓ Branch 3 taken 4 times.
|
1090 | && st->r_frame_rate.num && sti->info->rfps_duration_sum |
2414 |
2/2✓ Branch 0 taken 73 times.
✓ Branch 1 taken 207 times.
|
280 | && sti->info->codec_info_duration <= 0 |
2415 |
2/2✓ Branch 0 taken 70 times.
✓ Branch 1 taken 3 times.
|
73 | && sti->info->duration_count > 2 |
2416 |
2/2✓ Branch 2 taken 55 times.
✓ Branch 3 taken 15 times.
|
70 | && 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 |
2417 | ) { | ||
2418 | 55 | av_log(ic, AV_LOG_DEBUG, "Setting avg frame rate based on r frame rate\n"); | |
2419 | 55 | st->avg_frame_rate = st->r_frame_rate; | |
2420 | } | ||
2421 | |||
2422 | 6555 | av_freep(&sti->info->duration_error); | |
2423 | 6555 | sti->info->last_dts = AV_NOPTS_VALUE; | |
2424 | 6555 | sti->info->duration_count = 0; | |
2425 | 6555 | sti->info->rfps_duration_sum = 0; | |
2426 | } | ||
2427 | 8028 | } | |
2428 | |||
2429 | 8971 | static int extract_extradata_check(AVStream *st) | |
2430 | { | ||
2431 | 8971 | const AVBitStreamFilter *const f = av_bsf_get_by_name("extract_extradata"); | |
2432 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8971 times.
|
8971 | if (!f) |
2433 | ✗ | return 0; | |
2434 | |||
2435 |
1/2✓ Branch 0 taken 8971 times.
✗ Branch 1 not taken.
|
8971 | if (f->codec_ids) { |
2436 | const enum AVCodecID *ids; | ||
2437 |
2/2✓ Branch 0 taken 95041 times.
✓ Branch 1 taken 8081 times.
|
103122 | for (ids = f->codec_ids; *ids != AV_CODEC_ID_NONE; ids++) |
2438 |
2/2✓ Branch 0 taken 890 times.
✓ Branch 1 taken 94151 times.
|
95041 | if (*ids == st->codecpar->codec_id) |
2439 | 890 | return 1; | |
2440 | } | ||
2441 | |||
2442 | 8081 | return 0; | |
2443 | } | ||
2444 | |||
2445 | 7064 | static int extract_extradata_init(AVStream *st) | |
2446 | { | ||
2447 | 7064 | FFStream *const sti = ffstream(st); | |
2448 | const AVBitStreamFilter *f; | ||
2449 | int ret; | ||
2450 | |||
2451 | 7064 | f = av_bsf_get_by_name("extract_extradata"); | |
2452 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7064 times.
|
7064 | if (!f) |
2453 | ✗ | goto finish; | |
2454 | |||
2455 | /* check that the codec id is supported */ | ||
2456 | 7064 | ret = extract_extradata_check(st); | |
2457 |
2/2✓ Branch 0 taken 6284 times.
✓ Branch 1 taken 780 times.
|
7064 | if (!ret) |
2458 | 6284 | goto finish; | |
2459 | |||
2460 | 780 | av_bsf_free(&sti->extract_extradata.bsf); | |
2461 | 780 | ret = av_bsf_alloc(f, &sti->extract_extradata.bsf); | |
2462 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 780 times.
|
780 | if (ret < 0) |
2463 | ✗ | return ret; | |
2464 | |||
2465 | 780 | ret = avcodec_parameters_copy(sti->extract_extradata.bsf->par_in, | |
2466 | 780 | st->codecpar); | |
2467 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 780 times.
|
780 | if (ret < 0) |
2468 | ✗ | goto fail; | |
2469 | |||
2470 | 780 | sti->extract_extradata.bsf->time_base_in = st->time_base; | |
2471 | |||
2472 | 780 | ret = av_bsf_init(sti->extract_extradata.bsf); | |
2473 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 780 times.
|
780 | if (ret < 0) |
2474 | ✗ | goto fail; | |
2475 | |||
2476 | 780 | finish: | |
2477 | 7064 | sti->extract_extradata.inited = 1; | |
2478 | |||
2479 | 7064 | return 0; | |
2480 | ✗ | fail: | |
2481 | ✗ | av_bsf_free(&sti->extract_extradata.bsf); | |
2482 | ✗ | return ret; | |
2483 | } | ||
2484 | |||
2485 | 162097 | static int extract_extradata(FFFormatContext *si, AVStream *st, const AVPacket *pkt) | |
2486 | { | ||
2487 | 162097 | FFStream *const sti = ffstream(st); | |
2488 | 162097 | AVPacket *const pkt_ref = si->parse_pkt; | |
2489 | int ret; | ||
2490 | |||
2491 |
2/2✓ Branch 0 taken 7064 times.
✓ Branch 1 taken 155033 times.
|
162097 | if (!sti->extract_extradata.inited) { |
2492 | 7064 | ret = extract_extradata_init(st); | |
2493 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7064 times.
|
7064 | if (ret < 0) |
2494 | ✗ | return ret; | |
2495 | } | ||
2496 | |||
2497 |
3/4✓ Branch 0 taken 162097 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 160932 times.
✓ Branch 3 taken 1165 times.
|
162097 | if (sti->extract_extradata.inited && !sti->extract_extradata.bsf) |
2498 | 160932 | return 0; | |
2499 | |||
2500 | 1165 | ret = av_packet_ref(pkt_ref, pkt); | |
2501 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1165 times.
|
1165 | if (ret < 0) |
2502 | ✗ | return ret; | |
2503 | |||
2504 | 1165 | ret = av_bsf_send_packet(sti->extract_extradata.bsf, pkt_ref); | |
2505 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1165 times.
|
1165 | if (ret < 0) { |
2506 | ✗ | av_packet_unref(pkt_ref); | |
2507 | ✗ | return ret; | |
2508 | } | ||
2509 | |||
2510 |
4/4✓ Branch 0 taken 2330 times.
✓ Branch 1 taken 384 times.
✓ Branch 2 taken 1549 times.
✓ Branch 3 taken 781 times.
|
2714 | while (ret >= 0 && !sti->avctx->extradata) { |
2511 | 1549 | ret = av_bsf_receive_packet(sti->extract_extradata.bsf, pkt_ref); | |
2512 |
2/2✓ Branch 0 taken 384 times.
✓ Branch 1 taken 1165 times.
|
1549 | if (ret < 0) { |
2513 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 384 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
384 | if (ret != AVERROR(EAGAIN) && ret != AVERROR_EOF) |
2514 | ✗ | return ret; | |
2515 | 384 | continue; | |
2516 | } | ||
2517 | |||
2518 |
2/2✓ Branch 0 taken 1027 times.
✓ Branch 1 taken 384 times.
|
1411 | for (int i = 0; i < pkt_ref->side_data_elems; i++) { |
2519 | 1027 | AVPacketSideData *const side_data = &pkt_ref->side_data[i]; | |
2520 |
2/2✓ Branch 0 taken 781 times.
✓ Branch 1 taken 246 times.
|
1027 | if (side_data->type == AV_PKT_DATA_NEW_EXTRADATA) { |
2521 | 781 | sti->avctx->extradata = side_data->data; | |
2522 | 781 | sti->avctx->extradata_size = side_data->size; | |
2523 | 781 | side_data->data = NULL; | |
2524 | 781 | side_data->size = 0; | |
2525 | 781 | break; | |
2526 | } | ||
2527 | } | ||
2528 | 1165 | av_packet_unref(pkt_ref); | |
2529 | } | ||
2530 | |||
2531 | 1165 | return 0; | |
2532 | } | ||
2533 | |||
2534 | 7535 | int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options) | |
2535 | { | ||
2536 | 7535 | FFFormatContext *const si = ffformatcontext(ic); | |
2537 | 7535 | int count = 0, ret = 0, err; | |
2538 | int64_t read_size; | ||
2539 | 7535 | AVPacket *pkt1 = si->pkt; | |
2540 | 7535 | int64_t old_offset = avio_tell(ic->pb); | |
2541 | // new streams might appear, no options for those | ||
2542 | 7535 | int orig_nb_streams = ic->nb_streams; | |
2543 | int flush_codecs; | ||
2544 | 7535 | int64_t max_analyze_duration = ic->max_analyze_duration; | |
2545 | int64_t max_stream_analyze_duration; | ||
2546 | int64_t max_subtitle_analyze_duration; | ||
2547 | 7535 | int64_t probesize = ic->probesize; | |
2548 | 7535 | int eof_reached = 0; | |
2549 | |||
2550 | 7535 | flush_codecs = probesize > 0; | |
2551 | |||
2552 | 7535 | av_opt_set_int(ic, "skip_clear", 1, AV_OPT_SEARCH_CHILDREN); | |
2553 | |||
2554 | 7535 | max_stream_analyze_duration = max_analyze_duration; | |
2555 | 7535 | max_subtitle_analyze_duration = max_analyze_duration; | |
2556 |
2/2✓ Branch 0 taken 7534 times.
✓ Branch 1 taken 1 times.
|
7535 | if (!max_analyze_duration) { |
2557 | 7534 | max_stream_analyze_duration = | |
2558 | 7534 | max_analyze_duration = 5*AV_TIME_BASE; | |
2559 | 7534 | max_subtitle_analyze_duration = 30*AV_TIME_BASE; | |
2560 |
2/2✓ Branch 0 taken 36 times.
✓ Branch 1 taken 7498 times.
|
7534 | if (!strcmp(ic->iformat->name, "flv")) |
2561 | 36 | max_stream_analyze_duration = 90*AV_TIME_BASE; | |
2562 |
4/4✓ Branch 0 taken 7509 times.
✓ Branch 1 taken 25 times.
✓ Branch 2 taken 44 times.
✓ Branch 3 taken 7465 times.
|
7534 | if (!strcmp(ic->iformat->name, "mpeg") || !strcmp(ic->iformat->name, "mpegts")) |
2563 | 69 | max_stream_analyze_duration = 7*AV_TIME_BASE; | |
2564 | } | ||
2565 | |||
2566 |
2/2✓ Branch 0 taken 4499 times.
✓ Branch 1 taken 3036 times.
|
7535 | if (ic->pb) { |
2567 | 4499 | FFIOContext *const ctx = ffiocontext(ic->pb); | |
2568 | 4499 | av_log(ic, AV_LOG_DEBUG, "Before avformat_find_stream_info() pos: %"PRId64" bytes read:%"PRId64" seeks:%d nb_streams:%d\n", | |
2569 | avio_tell(ic->pb), ctx->bytes_read, ctx->seek_count, ic->nb_streams); | ||
2570 | } | ||
2571 | |||
2572 |
2/2✓ Branch 0 taken 8161 times.
✓ Branch 1 taken 7535 times.
|
15696 | for (unsigned i = 0; i < ic->nb_streams; i++) { |
2573 | const AVCodec *codec; | ||
2574 | 8161 | AVDictionary *thread_opt = NULL; | |
2575 | 8161 | AVStream *const st = ic->streams[i]; | |
2576 | 8161 | FFStream *const sti = ffstream(st); | |
2577 | 8161 | AVCodecContext *const avctx = sti->avctx; | |
2578 | |||
2579 | /* check if the caller has overridden the codec id */ | ||
2580 | // only for the split stuff | ||
2581 |
4/6✓ Branch 0 taken 8161 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8161 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 7657 times.
✓ Branch 5 taken 504 times.
|
8161 | if (!sti->parser && !(ic->flags & AVFMT_FLAG_NOPARSE) && sti->request_probe <= 0) { |
2582 | 7657 | sti->parser = av_parser_init(st->codecpar->codec_id); | |
2583 |
2/2✓ Branch 0 taken 5335 times.
✓ Branch 1 taken 2322 times.
|
7657 | if (sti->parser) { |
2584 |
2/2✓ Branch 0 taken 751 times.
✓ Branch 1 taken 4584 times.
|
5335 | if (sti->need_parsing == AVSTREAM_PARSE_HEADERS) { |
2585 | 751 | sti->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES; | |
2586 |
2/2✓ Branch 0 taken 816 times.
✓ Branch 1 taken 3768 times.
|
4584 | } else if (sti->need_parsing == AVSTREAM_PARSE_FULL_RAW) { |
2587 | 816 | sti->parser->flags |= PARSER_FLAG_USE_CODEC_TS; | |
2588 | } | ||
2589 |
2/2✓ Branch 0 taken 648 times.
✓ Branch 1 taken 1674 times.
|
2322 | } else if (sti->need_parsing) { |
2590 | 648 | av_log(ic, AV_LOG_VERBOSE, "parser not found for codec " | |
2591 | "%s, packets or times may be invalid.\n", | ||
2592 | 648 | avcodec_get_name(st->codecpar->codec_id)); | |
2593 | } | ||
2594 | } | ||
2595 | |||
2596 | 8161 | ret = avcodec_parameters_to_context(avctx, st->codecpar); | |
2597 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8161 times.
|
8161 | if (ret < 0) |
2598 | ✗ | goto find_stream_info_err; | |
2599 |
2/2✓ Branch 0 taken 7657 times.
✓ Branch 1 taken 504 times.
|
8161 | if (sti->request_probe <= 0) |
2600 | 7657 | sti->avctx_inited = 1; | |
2601 | |||
2602 | 8161 | codec = find_probe_decoder(ic, st, st->codecpar->codec_id); | |
2603 | |||
2604 | /* Force thread count to 1 since the H.264 decoder will not extract | ||
2605 | * SPS and PPS to extradata during multi-threaded decoding. */ | ||
2606 |
2/2✓ Branch 0 taken 7877 times.
✓ Branch 1 taken 284 times.
|
8161 | av_dict_set(options ? &options[i] : &thread_opt, "threads", "1", 0); |
2607 | /* Force lowres to 0. The decoder might reduce the video size by the | ||
2608 | * lowres factor, and we don't want that propagated to the stream's | ||
2609 | * codecpar */ | ||
2610 |
2/2✓ Branch 0 taken 7877 times.
✓ Branch 1 taken 284 times.
|
8161 | av_dict_set(options ? &options[i] : &thread_opt, "lowres", "0", 0); |
2611 | |||
2612 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8161 times.
|
8161 | if (ic->codec_whitelist) |
2613 | ✗ | av_dict_set(options ? &options[i] : &thread_opt, "codec_whitelist", ic->codec_whitelist, 0); | |
2614 | |||
2615 | // Try to just open decoders, in case this is enough to get parameters. | ||
2616 | // Also ensure that subtitle_header is properly set. | ||
2617 |
4/4✓ Branch 1 taken 7348 times.
✓ Branch 2 taken 813 times.
✓ Branch 3 taken 503 times.
✓ Branch 4 taken 6845 times.
|
8161 | if (!has_codec_parameters(st, NULL) && sti->request_probe <= 0 || |
2618 |
2/2✓ Branch 0 taken 78 times.
✓ Branch 1 taken 1238 times.
|
1316 | st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) { |
2619 |
3/4✓ Branch 0 taken 6904 times.
✓ Branch 1 taken 19 times.
✓ Branch 2 taken 6904 times.
✗ Branch 3 not taken.
|
6923 | if (codec && !avctx->codec) |
2620 |
4/4✓ Branch 0 taken 6624 times.
✓ Branch 1 taken 280 times.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 6898 times.
|
6904 | if (avcodec_open2(avctx, codec, options ? &options[i] : &thread_opt) < 0) |
2621 | 6 | av_log(ic, AV_LOG_WARNING, | |
2622 | "Failed to open codec in %s\n", __func__); | ||
2623 | } | ||
2624 |
2/2✓ Branch 0 taken 284 times.
✓ Branch 1 taken 7877 times.
|
8161 | if (!options) |
2625 | 284 | av_dict_free(&thread_opt); | |
2626 | } | ||
2627 | |||
2628 | 7535 | read_size = 0; | |
2629 | 192770 | for (;;) { | |
2630 | const AVPacket *pkt; | ||
2631 | AVStream *st; | ||
2632 | FFStream *sti; | ||
2633 | AVCodecContext *avctx; | ||
2634 | int analyzed_all_streams; | ||
2635 | unsigned i; | ||
2636 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 200305 times.
|
200305 | if (ff_check_interrupt(&ic->interrupt_callback)) { |
2637 | ✗ | ret = AVERROR_EXIT; | |
2638 | ✗ | av_log(ic, AV_LOG_DEBUG, "interrupted\n"); | |
2639 | ✗ | break; | |
2640 | } | ||
2641 | |||
2642 | /* check if one codec still needs to be handled */ | ||
2643 |
2/2✓ Branch 0 taken 208908 times.
✓ Branch 1 taken 109674 times.
|
318582 | for (i = 0; i < ic->nb_streams; i++) { |
2644 | 208908 | AVStream *const st = ic->streams[i]; | |
2645 | 208908 | FFStream *const sti = ffstream(st); | |
2646 | 208908 | int fps_analyze_framecount = 20; | |
2647 | int count; | ||
2648 | |||
2649 |
2/2✓ Branch 1 taken 9278 times.
✓ Branch 2 taken 199630 times.
|
208908 | if (!has_codec_parameters(st, NULL)) |
2650 | 9278 | break; | |
2651 | /* If the timebase is coarse (like the usual millisecond precision | ||
2652 | * of mkv), we need to analyze more frames to reliably arrive at | ||
2653 | * the correct fps. */ | ||
2654 |
2/2✓ Branch 1 taken 120345 times.
✓ Branch 2 taken 79285 times.
|
199630 | if (av_q2d(st->time_base) > 0.0005) |
2655 | 120345 | fps_analyze_framecount *= 2; | |
2656 |
2/2✓ Branch 1 taken 8423 times.
✓ Branch 2 taken 191207 times.
|
199630 | if (!tb_unreliable(ic, st)) |
2657 | 8423 | fps_analyze_framecount = 0; | |
2658 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 199630 times.
|
199630 | if (ic->fps_probe_size >= 0) |
2659 | ✗ | fps_analyze_framecount = ic->fps_probe_size; | |
2660 |
2/2✓ Branch 0 taken 44 times.
✓ Branch 1 taken 199586 times.
|
199630 | if (st->disposition & AV_DISPOSITION_ATTACHED_PIC) |
2661 | 44 | fps_analyze_framecount = 0; | |
2662 | /* variable fps and no guess at the real fps */ | ||
2663 |
2/2✓ Branch 0 taken 19296 times.
✓ Branch 1 taken 180334 times.
|
199630 | count = (ic->iformat->flags & AVFMT_NOTIMESTAMPS) ? |
2664 | 19296 | sti->info->codec_info_duration_fields/2 : | |
2665 | 180334 | sti->info->duration_count; | |
2666 |
4/4✓ Branch 0 taken 101135 times.
✓ Branch 1 taken 98495 times.
✓ Branch 2 taken 223 times.
✓ Branch 3 taken 100912 times.
|
199630 | if (!(st->r_frame_rate.num && st->avg_frame_rate.num) && |
2667 |
2/2✓ Branch 0 taken 42900 times.
✓ Branch 1 taken 55818 times.
|
98718 | st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { |
2668 |
2/2✓ Branch 0 taken 24878 times.
✓ Branch 1 taken 18022 times.
|
42900 | if (count < fps_analyze_framecount) |
2669 | 24878 | break; | |
2670 | } | ||
2671 | // Look at the first 3 frames if there is evidence of frame delay | ||
2672 | // but the decoder delay is not set. | ||
2673 |
6/6✓ Branch 0 taken 2898 times.
✓ Branch 1 taken 171854 times.
✓ Branch 2 taken 130 times.
✓ Branch 3 taken 2768 times.
✓ Branch 4 taken 12 times.
✓ Branch 5 taken 118 times.
|
174752 | if (sti->info->frame_delay_evidence && count < 2 && sti->avctx->has_b_frames == 0) |
2674 | 12 | break; | |
2675 |
2/2✓ Branch 0 taken 154572 times.
✓ Branch 1 taken 20168 times.
|
174740 | if (!sti->avctx->extradata && |
2676 |
6/6✓ Branch 0 taken 152775 times.
✓ Branch 1 taken 1797 times.
✓ Branch 2 taken 110 times.
✓ Branch 3 taken 152665 times.
✓ Branch 4 taken 110 times.
✓ Branch 5 taken 1797 times.
|
156479 | (!sti->extract_extradata.inited || sti->extract_extradata.bsf) && |
2677 | 1907 | extract_extradata_check(st)) | |
2678 | 110 | break; | |
2679 |
2/2✓ Branch 0 taken 59505 times.
✓ Branch 1 taken 115125 times.
|
174630 | if (sti->first_dts == AV_NOPTS_VALUE && |
2680 |
6/6✓ Branch 0 taken 9372 times.
✓ Branch 1 taken 50133 times.
✓ Branch 2 taken 9291 times.
✓ Branch 3 taken 81 times.
✓ Branch 4 taken 57136 times.
✓ Branch 5 taken 2288 times.
|
118929 | (!(ic->iformat->flags & AVFMT_NOTIMESTAMPS) || sti->need_parsing == AVSTREAM_PARSE_FULL_RAW) && |
2681 |
2/2✓ Branch 0 taken 59380 times.
✓ Branch 1 taken 44 times.
|
59424 | sti->codec_info_nb_frames < ((st->disposition & AV_DISPOSITION_ATTACHED_PIC) ? 1 : ic->max_ts_probe) && |
2682 |
2/2✓ Branch 0 taken 41396 times.
✓ Branch 1 taken 15740 times.
|
57136 | (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO || |
2683 |
2/2✓ Branch 0 taken 783 times.
✓ Branch 1 taken 40613 times.
|
41396 | st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)) |
2684 | break; | ||
2685 | } | ||
2686 | 200305 | analyzed_all_streams = 0; | |
2687 |
4/4✓ Branch 0 taken 109674 times.
✓ Branch 1 taken 90631 times.
✓ Branch 2 taken 109637 times.
✓ Branch 3 taken 37 times.
|
200305 | if (i == ic->nb_streams && !si->missing_streams) { |
2688 | 109637 | analyzed_all_streams = 1; | |
2689 | /* NOTE: If the format has no header, then we need to read some | ||
2690 | * packets to get most of the streams, so we cannot stop here. */ | ||
2691 |
2/2✓ Branch 0 taken 3287 times.
✓ Branch 1 taken 106350 times.
|
109637 | if (!(ic->ctx_flags & AVFMTCTX_NOHEADER)) { |
2692 | /* If we found the info for all the codecs, we can stop. */ | ||
2693 | 3287 | ret = count; | |
2694 | 3287 | av_log(ic, AV_LOG_DEBUG, "All info found\n"); | |
2695 | 3287 | flush_codecs = 0; | |
2696 | 3287 | break; | |
2697 | } | ||
2698 | } | ||
2699 | /* We did not get all the codec info, but we read too much data. */ | ||
2700 |
2/2✓ Branch 0 taken 2967 times.
✓ Branch 1 taken 194051 times.
|
197018 | if (read_size >= probesize) { |
2701 | 2967 | ret = count; | |
2702 | 2967 | av_log(ic, AV_LOG_DEBUG, | |
2703 | "Probe buffer size limit of %"PRId64" bytes reached\n", probesize); | ||
2704 |
2/2✓ Branch 0 taken 2981 times.
✓ Branch 1 taken 2967 times.
|
5948 | for (unsigned i = 0; i < ic->nb_streams; i++) { |
2705 | 2981 | AVStream *const st = ic->streams[i]; | |
2706 | 2981 | FFStream *const sti = ffstream(st); | |
2707 |
2/2✓ Branch 0 taken 58 times.
✓ Branch 1 taken 2923 times.
|
2981 | if (!st->r_frame_rate.num && |
2708 |
2/2✓ Branch 0 taken 15 times.
✓ Branch 1 taken 43 times.
|
58 | sti->info->duration_count <= 1 && |
2709 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 14 times.
|
15 | st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && |
2710 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | strcmp(ic->iformat->name, "image2")) |
2711 | 1 | av_log(ic, AV_LOG_WARNING, | |
2712 | "Stream #%d: not enough frames to estimate rate; " | ||
2713 | "consider increasing probesize\n", i); | ||
2714 | } | ||
2715 | 2967 | break; | |
2716 | } | ||
2717 | |||
2718 | /* NOTE: A new stream can be added there if no header in file | ||
2719 | * (AVFMTCTX_NOHEADER). */ | ||
2720 | 194051 | ret = read_frame_internal(ic, pkt1); | |
2721 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 194051 times.
|
194051 | if (ret == AVERROR(EAGAIN)) |
2722 | ✗ | continue; | |
2723 | |||
2724 |
2/2✓ Branch 0 taken 1178 times.
✓ Branch 1 taken 192873 times.
|
194051 | if (ret < 0) { |
2725 | /* EOF or error*/ | ||
2726 | 1178 | eof_reached = 1; | |
2727 | 1178 | break; | |
2728 | } | ||
2729 | |||
2730 |
1/2✓ Branch 0 taken 192873 times.
✗ Branch 1 not taken.
|
192873 | if (!(ic->flags & AVFMT_FLAG_NOBUFFER)) { |
2731 | 192873 | ret = avpriv_packet_list_put(&si->packet_buffer, | |
2732 | pkt1, NULL, 0); | ||
2733 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 192873 times.
|
192873 | if (ret < 0) |
2734 | ✗ | goto unref_then_goto_end; | |
2735 | |||
2736 | 192873 | pkt = &si->packet_buffer.tail->pkt; | |
2737 | } else { | ||
2738 | ✗ | pkt = pkt1; | |
2739 | } | ||
2740 | |||
2741 | 192873 | st = ic->streams[pkt->stream_index]; | |
2742 | 192873 | sti = ffstream(st); | |
2743 |
2/2✓ Branch 0 taken 192817 times.
✓ Branch 1 taken 56 times.
|
192873 | if (!(st->disposition & AV_DISPOSITION_ATTACHED_PIC)) |
2744 | 192817 | read_size += pkt->size; | |
2745 | |||
2746 | 192873 | avctx = sti->avctx; | |
2747 |
2/2✓ Branch 0 taken 674 times.
✓ Branch 1 taken 192199 times.
|
192873 | if (!sti->avctx_inited) { |
2748 | 674 | ret = avcodec_parameters_to_context(avctx, st->codecpar); | |
2749 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 674 times.
|
674 | if (ret < 0) |
2750 | ✗ | goto unref_then_goto_end; | |
2751 | 674 | sti->avctx_inited = 1; | |
2752 | } | ||
2753 | |||
2754 |
4/4✓ Branch 0 taken 176543 times.
✓ Branch 1 taken 16330 times.
✓ Branch 2 taken 164208 times.
✓ Branch 3 taken 12335 times.
|
192873 | if (pkt->dts != AV_NOPTS_VALUE && sti->codec_info_nb_frames > 1) { |
2755 | /* check for non-increasing dts */ | ||
2756 |
2/2✓ Branch 0 taken 159348 times.
✓ Branch 1 taken 4860 times.
|
164208 | if (sti->info->fps_last_dts != AV_NOPTS_VALUE && |
2757 |
2/2✓ Branch 0 taken 23 times.
✓ Branch 1 taken 159325 times.
|
159348 | sti->info->fps_last_dts >= pkt->dts) { |
2758 | 23 | av_log(ic, AV_LOG_DEBUG, | |
2759 | "Non-increasing DTS in stream %d: packet %d with DTS " | ||
2760 | "%"PRId64", packet %d with DTS %"PRId64"\n", | ||
2761 | 23 | st->index, sti->info->fps_last_dts_idx, | |
2762 | 23 | sti->info->fps_last_dts, sti->codec_info_nb_frames, | |
2763 | 23 | pkt->dts); | |
2764 | 23 | sti->info->fps_first_dts = | |
2765 | 23 | sti->info->fps_last_dts = AV_NOPTS_VALUE; | |
2766 | } | ||
2767 | /* Check for a discontinuity in dts. If the difference in dts | ||
2768 | * is more than 1000 times the average packet duration in the | ||
2769 | * sequence, we treat it as a discontinuity. */ | ||
2770 |
2/2✓ Branch 0 taken 159325 times.
✓ Branch 1 taken 4883 times.
|
164208 | if (sti->info->fps_last_dts != AV_NOPTS_VALUE && |
2771 |
2/2✓ Branch 0 taken 154494 times.
✓ Branch 1 taken 4831 times.
|
159325 | sti->info->fps_last_dts_idx > sti->info->fps_first_dts_idx && |
2772 | 154494 | (pkt->dts - (uint64_t)sti->info->fps_last_dts) / 1000 > | |
2773 | 154494 | (sti->info->fps_last_dts - (uint64_t)sti->info->fps_first_dts) / | |
2774 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 154494 times.
|
154494 | (sti->info->fps_last_dts_idx - sti->info->fps_first_dts_idx)) { |
2775 | ✗ | av_log(ic, AV_LOG_WARNING, | |
2776 | "DTS discontinuity in stream %d: packet %d with DTS " | ||
2777 | "%"PRId64", packet %d with DTS %"PRId64"\n", | ||
2778 | ✗ | st->index, sti->info->fps_last_dts_idx, | |
2779 | ✗ | sti->info->fps_last_dts, sti->codec_info_nb_frames, | |
2780 | ✗ | pkt->dts); | |
2781 | ✗ | sti->info->fps_first_dts = | |
2782 | ✗ | sti->info->fps_last_dts = AV_NOPTS_VALUE; | |
2783 | } | ||
2784 | |||
2785 | /* update stored dts values */ | ||
2786 |
2/2✓ Branch 0 taken 4883 times.
✓ Branch 1 taken 159325 times.
|
164208 | if (sti->info->fps_first_dts == AV_NOPTS_VALUE) { |
2787 | 4883 | sti->info->fps_first_dts = pkt->dts; | |
2788 | 4883 | sti->info->fps_first_dts_idx = sti->codec_info_nb_frames; | |
2789 | } | ||
2790 | 164208 | sti->info->fps_last_dts = pkt->dts; | |
2791 | 164208 | sti->info->fps_last_dts_idx = sti->codec_info_nb_frames; | |
2792 | } | ||
2793 |
2/2✓ Branch 0 taken 179370 times.
✓ Branch 1 taken 13503 times.
|
192873 | if (sti->codec_info_nb_frames > 1) { |
2794 | 179370 | int64_t t = 0; | |
2795 | int64_t limit; | ||
2796 | |||
2797 |
1/2✓ Branch 0 taken 179370 times.
✗ Branch 1 not taken.
|
179370 | if (st->time_base.den > 0) |
2798 | 179370 | t = av_rescale_q(sti->info->codec_info_duration, st->time_base, AV_TIME_BASE_Q); | |
2799 |
2/2✓ Branch 0 taken 112504 times.
✓ Branch 1 taken 66866 times.
|
179370 | if (st->avg_frame_rate.num > 0) |
2800 |
2/2✓ Branch 1 taken 111977 times.
✓ Branch 2 taken 527 times.
|
112504 | t = FFMAX(t, av_rescale_q(sti->codec_info_nb_frames, av_inv_q(st->avg_frame_rate), AV_TIME_BASE_Q)); |
2801 | |||
2802 |
2/2✓ Branch 0 taken 4115 times.
✓ Branch 1 taken 175255 times.
|
179370 | if ( t == 0 |
2803 |
2/2✓ Branch 0 taken 535 times.
✓ Branch 1 taken 3580 times.
|
4115 | && sti->codec_info_nb_frames > 30 |
2804 |
2/2✓ Branch 0 taken 497 times.
✓ Branch 1 taken 38 times.
|
535 | && sti->info->fps_first_dts != AV_NOPTS_VALUE |
2805 |
1/2✓ Branch 0 taken 497 times.
✗ Branch 1 not taken.
|
497 | && sti->info->fps_last_dts != AV_NOPTS_VALUE) { |
2806 | 497 | int64_t dur = av_sat_sub64(sti->info->fps_last_dts, sti->info->fps_first_dts); | |
2807 |
1/2✓ Branch 0 taken 497 times.
✗ Branch 1 not taken.
|
497 | t = FFMAX(t, av_rescale_q(dur, st->time_base, AV_TIME_BASE_Q)); |
2808 | } | ||
2809 | |||
2810 |
2/2✓ Branch 0 taken 100152 times.
✓ Branch 1 taken 79218 times.
|
179370 | if (analyzed_all_streams) limit = max_analyze_duration; |
2811 |
2/2✓ Branch 0 taken 91 times.
✓ Branch 1 taken 79127 times.
|
79218 | else if (avctx->codec_type == AVMEDIA_TYPE_SUBTITLE) limit = max_subtitle_analyze_duration; |
2812 | 79127 | else limit = max_stream_analyze_duration; | |
2813 | |||
2814 |
2/2✓ Branch 0 taken 103 times.
✓ Branch 1 taken 179267 times.
|
179370 | if (t >= limit) { |
2815 | 103 | av_log(ic, AV_LOG_VERBOSE, "max_analyze_duration %"PRId64" reached at %"PRId64" microseconds st:%d\n", | |
2816 | limit, | ||
2817 | 103 | t, pkt->stream_index); | |
2818 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 103 times.
|
103 | if (ic->flags & AVFMT_FLAG_NOBUFFER) |
2819 | ✗ | av_packet_unref(pkt1); | |
2820 | 103 | break; | |
2821 | } | ||
2822 |
3/4✓ Branch 0 taken 175621 times.
✓ Branch 1 taken 3646 times.
✓ Branch 2 taken 175621 times.
✗ Branch 3 not taken.
|
179267 | if (pkt->duration > 0 && pkt->duration < INT64_MAX - sti->info->codec_info_duration) { |
2823 |
4/4✓ Branch 0 taken 175577 times.
✓ Branch 1 taken 44 times.
✓ Branch 2 taken 14236 times.
✓ Branch 3 taken 161341 times.
|
175621 | const int fields = sti->codec_desc && (sti->codec_desc->props & AV_CODEC_PROP_FIELDS); |
2824 |
1/8✗ Branch 0 not taken.
✓ Branch 1 taken 175621 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.
|
175621 | if (avctx->codec_type == AVMEDIA_TYPE_SUBTITLE && pkt->pts != AV_NOPTS_VALUE && st->start_time != AV_NOPTS_VALUE && pkt->pts >= st->start_time |
2825 | ✗ | && (uint64_t)pkt->pts - st->start_time < INT64_MAX | |
2826 | ) { | ||
2827 | ✗ | sti->info->codec_info_duration = FFMIN(pkt->pts - st->start_time, sti->info->codec_info_duration + pkt->duration); | |
2828 | } else | ||
2829 | 175621 | sti->info->codec_info_duration += pkt->duration; | |
2830 |
4/4✓ Branch 0 taken 35590 times.
✓ Branch 1 taken 91738 times.
✓ Branch 2 taken 13864 times.
✓ Branch 3 taken 21726 times.
|
302949 | sti->info->codec_info_duration_fields += sti->parser && sti->need_parsing && fields |
2831 |
2/2✓ Branch 0 taken 127328 times.
✓ Branch 1 taken 48293 times.
|
302949 | ? sti->parser->repeat_pict + 1 : 2; |
2832 | } | ||
2833 | } | ||
2834 |
2/2✓ Branch 0 taken 131050 times.
✓ Branch 1 taken 61720 times.
|
192770 | if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { |
2835 | #if FF_API_R_FRAME_RATE | ||
2836 | 131050 | ff_rfps_add_frame(ic, st, pkt->dts); | |
2837 | #endif | ||
2838 |
6/6✓ Branch 0 taken 4230 times.
✓ Branch 1 taken 126820 times.
✓ Branch 2 taken 3991 times.
✓ Branch 3 taken 239 times.
✓ Branch 4 taken 1965 times.
✓ Branch 5 taken 2026 times.
|
131050 | if (pkt->dts != pkt->pts && pkt->dts != AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE) |
2839 | 1965 | sti->info->frame_delay_evidence = 1; | |
2840 | } | ||
2841 |
2/2✓ Branch 0 taken 161948 times.
✓ Branch 1 taken 30822 times.
|
192770 | if (!sti->avctx->extradata) { |
2842 | 161948 | ret = extract_extradata(si, st, pkt); | |
2843 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 161948 times.
|
161948 | if (ret < 0) |
2844 | ✗ | goto unref_then_goto_end; | |
2845 | } | ||
2846 | |||
2847 | /* If still no information, we try to open the codec and to | ||
2848 | * decompress the frame. We try to avoid that in most cases as | ||
2849 | * it takes longer and uses more memory. For MPEG-4, we need to | ||
2850 | * decompress for QuickTime. | ||
2851 | * | ||
2852 | * If AV_CODEC_CAP_CHANNEL_CONF is set this will force decoding of at | ||
2853 | * least one frame of codec data, this makes sure the codec initializes | ||
2854 | * the channel configuration and does not only trust the values from | ||
2855 | * the container. */ | ||
2856 |
2/2✓ Branch 0 taken 175263 times.
✓ Branch 1 taken 17507 times.
|
368033 | try_decode_frame(ic, st, pkt, |
2857 |
2/2✓ Branch 0 taken 79436 times.
✓ Branch 1 taken 95827 times.
|
175263 | (options && i < orig_nb_streams) ? &options[i] : NULL); |
2858 | |||
2859 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 192770 times.
|
192770 | if (ic->flags & AVFMT_FLAG_NOBUFFER) |
2860 | ✗ | av_packet_unref(pkt1); | |
2861 | |||
2862 | 192770 | sti->codec_info_nb_frames++; | |
2863 | 192770 | count++; | |
2864 | } | ||
2865 | |||
2866 |
2/2✓ Branch 0 taken 1178 times.
✓ Branch 1 taken 6357 times.
|
7535 | if (eof_reached) { |
2867 |
2/2✓ Branch 0 taken 1468 times.
✓ Branch 1 taken 1178 times.
|
2646 | for (unsigned stream_index = 0; stream_index < ic->nb_streams; stream_index++) { |
2868 | 1468 | AVStream *const st = ic->streams[stream_index]; | |
2869 | 1468 | AVCodecContext *const avctx = ffstream(st)->avctx; | |
2870 |
2/2✓ Branch 1 taken 29 times.
✓ Branch 2 taken 1439 times.
|
1468 | if (!has_codec_parameters(st, NULL)) { |
2871 | 29 | const AVCodec *codec = find_probe_decoder(ic, st, st->codecpar->codec_id); | |
2872 |
4/4✓ Branch 0 taken 22 times.
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 14 times.
|
29 | if (codec && !avctx->codec) { |
2873 | 8 | AVDictionary *opts = NULL; | |
2874 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
|
8 | if (ic->codec_whitelist) |
2875 | ✗ | av_dict_set(&opts, "codec_whitelist", ic->codec_whitelist, 0); | |
2876 |
4/6✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 6 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 8 times.
|
8 | if (avcodec_open2(avctx, codec, (options && stream_index < orig_nb_streams) ? &options[stream_index] : &opts) < 0) |
2877 | ✗ | av_log(ic, AV_LOG_WARNING, | |
2878 | "Failed to open codec in %s\n", __func__); | ||
2879 | 8 | av_dict_free(&opts); | |
2880 | } | ||
2881 | } | ||
2882 | |||
2883 | // EOF already reached while reading the stream above. | ||
2884 | // So continue with reoordering DTS with whatever delay we have. | ||
2885 |
4/4✓ Branch 0 taken 1450 times.
✓ Branch 1 taken 18 times.
✓ Branch 3 taken 18 times.
✓ Branch 4 taken 1432 times.
|
1468 | if (si->packet_buffer.head && !has_decode_delay_been_guessed(st)) { |
2886 | 18 | update_dts_from_pts(ic, stream_index, si->packet_buffer.head); | |
2887 | } | ||
2888 | } | ||
2889 | } | ||
2890 | |||
2891 |
2/2✓ Branch 0 taken 4248 times.
✓ Branch 1 taken 3287 times.
|
7535 | if (flush_codecs) { |
2892 | 4248 | AVPacket *empty_pkt = si->pkt; | |
2893 | 4248 | int err = 0; | |
2894 | 4248 | av_packet_unref(empty_pkt); | |
2895 | |||
2896 |
2/2✓ Branch 0 taken 4591 times.
✓ Branch 1 taken 4248 times.
|
8839 | for (unsigned i = 0; i < ic->nb_streams; i++) { |
2897 | 4591 | AVStream *const st = ic->streams[i]; | |
2898 | 4591 | FFStream *const sti = ffstream(st); | |
2899 | |||
2900 | /* flush the decoders */ | ||
2901 |
2/2✓ Branch 0 taken 4513 times.
✓ Branch 1 taken 78 times.
|
4591 | if (sti->info->found_decoder == 1) { |
2902 |
2/2✓ Branch 0 taken 4198 times.
✓ Branch 1 taken 315 times.
|
8711 | err = try_decode_frame(ic, st, empty_pkt, |
2903 |
2/2✓ Branch 0 taken 4182 times.
✓ Branch 1 taken 16 times.
|
4198 | (options && i < orig_nb_streams) |
2904 | 4182 | ? &options[i] : NULL); | |
2905 | |||
2906 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4513 times.
|
4513 | if (err < 0) { |
2907 | ✗ | av_log(ic, AV_LOG_INFO, | |
2908 | "decoding for stream %d failed\n", st->index); | ||
2909 | } | ||
2910 | } | ||
2911 | } | ||
2912 | } | ||
2913 | |||
2914 | 7535 | ff_rfps_calculate(ic); | |
2915 | |||
2916 |
2/2✓ Branch 0 taken 8344 times.
✓ Branch 1 taken 7535 times.
|
15879 | for (unsigned i = 0; i < ic->nb_streams; i++) { |
2917 | 8344 | AVStream *const st = ic->streams[i]; | |
2918 | 8344 | FFStream *const sti = ffstream(st); | |
2919 | 8344 | AVCodecContext *const avctx = sti->avctx; | |
2920 | |||
2921 |
2/2✓ Branch 0 taken 6216 times.
✓ Branch 1 taken 2128 times.
|
8344 | if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) { |
2922 |
6/6✓ Branch 0 taken 634 times.
✓ Branch 1 taken 5582 times.
✓ Branch 2 taken 585 times.
✓ Branch 3 taken 49 times.
✓ Branch 4 taken 569 times.
✓ Branch 5 taken 16 times.
|
6216 | if (avctx->codec_id == AV_CODEC_ID_RAWVIDEO && !avctx->codec_tag && !avctx->bits_per_coded_sample) { |
2923 | 569 | uint32_t tag= avcodec_pix_fmt_to_codec_tag(avctx->pix_fmt); | |
2924 |
2/2✓ Branch 1 taken 562 times.
✓ Branch 2 taken 7 times.
|
569 | if (avpriv_pix_fmt_find(PIX_FMT_LIST_RAW, tag) == avctx->pix_fmt) |
2925 | 562 | avctx->codec_tag= tag; | |
2926 | } | ||
2927 | |||
2928 | /* estimate average framerate if not set by demuxer */ | ||
2929 |
2/2✓ Branch 0 taken 3989 times.
✓ Branch 1 taken 2227 times.
|
6216 | if (sti->info->codec_info_duration_fields && |
2930 |
2/2✓ Branch 0 taken 269 times.
✓ Branch 1 taken 3720 times.
|
3989 | !st->avg_frame_rate.num && |
2931 |
1/2✓ Branch 0 taken 269 times.
✗ Branch 1 not taken.
|
269 | sti->info->codec_info_duration) { |
2932 | 269 | int best_fps = 0; | |
2933 | 269 | double best_error = 0.01; | |
2934 | 269 | AVRational codec_frame_rate = avctx->framerate; | |
2935 | |||
2936 |
1/2✓ Branch 0 taken 269 times.
✗ Branch 1 not taken.
|
269 | if (sti->info->codec_info_duration >= INT64_MAX / st->time_base.num / 2|| |
2937 |
1/2✓ Branch 0 taken 269 times.
✗ Branch 1 not taken.
|
269 | sti->info->codec_info_duration_fields >= INT64_MAX / st->time_base.den || |
2938 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 269 times.
|
269 | sti->info->codec_info_duration < 0) |
2939 | ✗ | continue; | |
2940 | 269 | av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den, | |
2941 | 269 | sti->info->codec_info_duration_fields * (int64_t) st->time_base.den, | |
2942 | 269 | sti->info->codec_info_duration * 2 * (int64_t) st->time_base.num, 60000); | |
2943 | |||
2944 | /* Round guessed framerate to a "standard" framerate if it's | ||
2945 | * within 1% of the original estimate. */ | ||
2946 |
2/2✓ Branch 0 taken 107331 times.
✓ Branch 1 taken 269 times.
|
107600 | for (int j = 0; j < MAX_STD_TIMEBASES; j++) { |
2947 | 107331 | AVRational std_fps = { get_std_framerate(j), 12 * 1001 }; | |
2948 | 107331 | double error = fabs(av_q2d(st->avg_frame_rate) / | |
2949 | 107331 | av_q2d(std_fps) - 1); | |
2950 | |||
2951 |
2/2✓ Branch 0 taken 736 times.
✓ Branch 1 taken 106595 times.
|
107331 | if (error < best_error) { |
2952 | 736 | best_error = error; | |
2953 | 736 | best_fps = std_fps.num; | |
2954 | } | ||
2955 | |||
2956 |
2/2✓ Branch 1 taken 12768 times.
✓ Branch 2 taken 94563 times.
|
107331 | if ((ffifmt(ic->iformat)->flags_internal & FF_INFMT_FLAG_PREFER_CODEC_FRAMERATE) && |
2957 |
2/4✓ Branch 0 taken 12768 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12768 times.
✗ Branch 3 not taken.
|
12768 | codec_frame_rate.num > 0 && codec_frame_rate.den > 0) { |
2958 | 12768 | error = fabs(av_q2d(codec_frame_rate) / | |
2959 | 12768 | av_q2d(std_fps) - 1); | |
2960 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 12758 times.
|
12768 | if (error < best_error) { |
2961 | 10 | best_error = error; | |
2962 | 10 | best_fps = std_fps.num; | |
2963 | } | ||
2964 | } | ||
2965 | } | ||
2966 |
2/2✓ Branch 0 taken 267 times.
✓ Branch 1 taken 2 times.
|
269 | if (best_fps) |
2967 | 267 | av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den, | |
2968 | best_fps, 12 * 1001, INT_MAX); | ||
2969 | } | ||
2970 |
2/2✓ Branch 0 taken 1834 times.
✓ Branch 1 taken 4382 times.
|
6216 | if (!st->r_frame_rate.num) { |
2971 | 1834 | const AVCodecDescriptor *desc = sti->codec_desc; | |
2972 |
3/4✓ Branch 0 taken 1834 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 246 times.
✓ Branch 3 taken 1588 times.
|
1834 | AVRational mul = (AVRational){ desc && (desc->props & AV_CODEC_PROP_FIELDS) ? 2 : 1, 1 }; |
2973 | 1834 | AVRational fr = av_mul_q(avctx->framerate, mul); | |
2974 | |||
2975 |
5/6✓ Branch 0 taken 189 times.
✓ Branch 1 taken 1645 times.
✓ Branch 2 taken 189 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 175 times.
✓ Branch 7 taken 14 times.
|
1834 | if (fr.num && fr.den && av_cmp_q(st->time_base, av_inv_q(fr)) <= 0) { |
2976 | 175 | st->r_frame_rate = fr; | |
2977 | } else { | ||
2978 | 1659 | st->r_frame_rate.num = st->time_base.den; | |
2979 | 1659 | st->r_frame_rate.den = st->time_base.num; | |
2980 | } | ||
2981 | } | ||
2982 | 6216 | st->codecpar->framerate = avctx->framerate; | |
2983 |
3/4✓ Branch 0 taken 116 times.
✓ Branch 1 taken 6100 times.
✓ Branch 2 taken 116 times.
✗ Branch 3 not taken.
|
6216 | if (sti->display_aspect_ratio.num && sti->display_aspect_ratio.den) { |
2984 | 116 | AVRational hw_ratio = { avctx->height, avctx->width }; | |
2985 | 116 | st->sample_aspect_ratio = av_mul_q(sti->display_aspect_ratio, | |
2986 | hw_ratio); | ||
2987 | } | ||
2988 |
2/2✓ Branch 0 taken 1959 times.
✓ Branch 1 taken 169 times.
|
2128 | } else if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) { |
2989 |
2/2✓ Branch 0 taken 642 times.
✓ Branch 1 taken 1317 times.
|
1959 | if (!avctx->bits_per_coded_sample) |
2990 | 642 | avctx->bits_per_coded_sample = | |
2991 | 642 | av_get_bits_per_sample(avctx->codec_id); | |
2992 | // set stream disposition based on audio service type | ||
2993 |
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 1959 times.
|
1959 | switch (avctx->audio_service_type) { |
2994 | ✗ | case AV_AUDIO_SERVICE_TYPE_EFFECTS: | |
2995 | ✗ | st->disposition = AV_DISPOSITION_CLEAN_EFFECTS; | |
2996 | ✗ | break; | |
2997 | ✗ | case AV_AUDIO_SERVICE_TYPE_VISUALLY_IMPAIRED: | |
2998 | ✗ | st->disposition = AV_DISPOSITION_VISUAL_IMPAIRED; | |
2999 | ✗ | break; | |
3000 | ✗ | case AV_AUDIO_SERVICE_TYPE_HEARING_IMPAIRED: | |
3001 | ✗ | st->disposition = AV_DISPOSITION_HEARING_IMPAIRED; | |
3002 | ✗ | break; | |
3003 | ✗ | case AV_AUDIO_SERVICE_TYPE_COMMENTARY: | |
3004 | ✗ | st->disposition = AV_DISPOSITION_COMMENT; | |
3005 | ✗ | break; | |
3006 | ✗ | case AV_AUDIO_SERVICE_TYPE_KARAOKE: | |
3007 | ✗ | st->disposition = AV_DISPOSITION_KARAOKE; | |
3008 | ✗ | break; | |
3009 | } | ||
3010 | } | ||
3011 | } | ||
3012 | |||
3013 |
1/2✓ Branch 0 taken 7535 times.
✗ Branch 1 not taken.
|
7535 | if (probesize) |
3014 | 7535 | estimate_timings(ic, old_offset); | |
3015 | |||
3016 | 7535 | av_opt_set_int(ic, "skip_clear", 0, AV_OPT_SEARCH_CHILDREN); | |
3017 | |||
3018 |
3/4✓ Branch 0 taken 6357 times.
✓ Branch 1 taken 1178 times.
✓ Branch 2 taken 6357 times.
✗ Branch 3 not taken.
|
7535 | if (ret >= 0 && ic->nb_streams) |
3019 | /* We could not have all the codec parameters before EOF. */ | ||
3020 | 6357 | ret = -1; | |
3021 |
2/2✓ Branch 0 taken 8344 times.
✓ Branch 1 taken 7535 times.
|
15879 | for (unsigned i = 0; i < ic->nb_streams; i++) { |
3022 | 8344 | AVStream *const st = ic->streams[i]; | |
3023 | 8344 | FFStream *const sti = ffstream(st); | |
3024 | const char *errmsg; | ||
3025 | |||
3026 | /* if no packet was ever seen, update context now for has_codec_parameters */ | ||
3027 |
2/2✓ Branch 0 taken 13 times.
✓ Branch 1 taken 8331 times.
|
8344 | if (!sti->avctx_inited) { |
3028 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 5 times.
|
13 | if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && |
3029 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | st->codecpar->format == AV_SAMPLE_FMT_NONE) |
3030 | 8 | st->codecpar->format = sti->avctx->sample_fmt; | |
3031 | 13 | ret = avcodec_parameters_to_context(sti->avctx, st->codecpar); | |
3032 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
|
13 | if (ret < 0) |
3033 | ✗ | goto find_stream_info_err; | |
3034 | } | ||
3035 |
2/2✓ Branch 1 taken 36 times.
✓ Branch 2 taken 8308 times.
|
8344 | if (!has_codec_parameters(st, &errmsg)) { |
3036 | char buf[256]; | ||
3037 | 36 | avcodec_string(buf, sizeof(buf), sti->avctx, 0); | |
3038 | 36 | av_log(ic, AV_LOG_WARNING, | |
3039 | "Could not find codec parameters for stream %d (%s): %s\n" | ||
3040 | "Consider increasing the value for the 'analyzeduration' (%"PRId64") and 'probesize' (%"PRId64") options\n", | ||
3041 | i, buf, errmsg, ic->max_analyze_duration, ic->probesize); | ||
3042 | } else { | ||
3043 | 8308 | ret = 0; | |
3044 | } | ||
3045 | } | ||
3046 | |||
3047 | 7535 | err = compute_chapters_end(ic); | |
3048 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7535 times.
|
7535 | if (err < 0) { |
3049 | ✗ | ret = err; | |
3050 | ✗ | goto find_stream_info_err; | |
3051 | } | ||
3052 | |||
3053 | /* update the stream parameters from the internal codec contexts */ | ||
3054 |
2/2✓ Branch 0 taken 8344 times.
✓ Branch 1 taken 7535 times.
|
15879 | for (unsigned i = 0; i < ic->nb_streams; i++) { |
3055 | 8344 | AVStream *const st = ic->streams[i]; | |
3056 | 8344 | FFStream *const sti = ffstream(st); | |
3057 | |||
3058 |
2/2✓ Branch 0 taken 8331 times.
✓ Branch 1 taken 13 times.
|
8344 | if (sti->avctx_inited) { |
3059 | 8331 | ret = avcodec_parameters_from_context(st->codecpar, sti->avctx); | |
3060 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8331 times.
|
8331 | if (ret < 0) |
3061 | ✗ | goto find_stream_info_err; | |
3062 | |||
3063 |
3/4✓ Branch 0 taken 8147 times.
✓ Branch 1 taken 184 times.
✓ Branch 2 taken 8147 times.
✗ Branch 3 not taken.
|
8331 | if (sti->avctx->rc_buffer_size > 0 || sti->avctx->rc_max_rate > 0 || |
3064 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8147 times.
|
8147 | sti->avctx->rc_min_rate) { |
3065 | size_t cpb_size; | ||
3066 | 184 | AVCPBProperties *props = av_cpb_properties_alloc(&cpb_size); | |
3067 |
1/2✓ Branch 0 taken 184 times.
✗ Branch 1 not taken.
|
184 | if (props) { |
3068 |
1/2✓ Branch 0 taken 184 times.
✗ Branch 1 not taken.
|
184 | if (sti->avctx->rc_buffer_size > 0) |
3069 | 184 | props->buffer_size = sti->avctx->rc_buffer_size; | |
3070 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 184 times.
|
184 | if (sti->avctx->rc_min_rate > 0) |
3071 | ✗ | props->min_bitrate = sti->avctx->rc_min_rate; | |
3072 |
2/2✓ Branch 0 taken 77 times.
✓ Branch 1 taken 107 times.
|
184 | if (sti->avctx->rc_max_rate > 0) |
3073 | 77 | props->max_bitrate = sti->avctx->rc_max_rate; | |
3074 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 184 times.
|
184 | if (!av_packet_side_data_add(&st->codecpar->coded_side_data, |
3075 | 184 | &st->codecpar->nb_coded_side_data, | |
3076 | AV_PKT_DATA_CPB_PROPERTIES, | ||
3077 | (uint8_t *)props, cpb_size, 0)) | ||
3078 | ✗ | av_free(props); | |
3079 | } | ||
3080 | } | ||
3081 | } | ||
3082 | |||
3083 | 8344 | sti->avctx_inited = 0; | |
3084 | #if FF_API_AVSTREAM_SIDE_DATA | ||
3085 | FF_DISABLE_DEPRECATION_WARNINGS | ||
3086 |
2/2✓ Branch 0 taken 478 times.
✓ Branch 1 taken 7866 times.
|
8344 | if (st->codecpar->nb_coded_side_data > 0) { |
3087 |
2/4✓ Branch 0 taken 478 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 478 times.
|
478 | av_assert0(!st->side_data && !st->nb_side_data); |
3088 | 478 | st->side_data = av_calloc(st->codecpar->nb_coded_side_data, sizeof(*st->side_data)); | |
3089 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 478 times.
|
478 | if (!st->side_data) { |
3090 | ✗ | ret = AVERROR(ENOMEM); | |
3091 | ✗ | goto find_stream_info_err; | |
3092 | } | ||
3093 | |||
3094 |
2/2✓ Branch 0 taken 509 times.
✓ Branch 1 taken 478 times.
|
987 | for (int j = 0; j < st->codecpar->nb_coded_side_data; j++) { |
3095 | 509 | uint8_t *data = av_memdup(st->codecpar->coded_side_data[j].data, | |
3096 | 509 | st->codecpar->coded_side_data[j].size); | |
3097 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 509 times.
|
509 | if (!data) { |
3098 | ✗ | ret = AVERROR(ENOMEM); | |
3099 | ✗ | goto find_stream_info_err; | |
3100 | } | ||
3101 | 509 | st->side_data[j].type = st->codecpar->coded_side_data[j].type; | |
3102 | 509 | st->side_data[j].size = st->codecpar->coded_side_data[j].size; | |
3103 | 509 | st->side_data[j].data = data; | |
3104 | 509 | st->nb_side_data++; | |
3105 | } | ||
3106 | } | ||
3107 | FF_ENABLE_DEPRECATION_WARNINGS | ||
3108 | #endif | ||
3109 | } | ||
3110 | |||
3111 | 7535 | find_stream_info_err: | |
3112 |
2/2✓ Branch 0 taken 8344 times.
✓ Branch 1 taken 7535 times.
|
15879 | for (unsigned i = 0; i < ic->nb_streams; i++) { |
3113 | 8344 | AVStream *const st = ic->streams[i]; | |
3114 | 8344 | FFStream *const sti = ffstream(st); | |
3115 | int err; | ||
3116 | |||
3117 |
1/2✓ Branch 0 taken 8344 times.
✗ Branch 1 not taken.
|
8344 | if (sti->info) { |
3118 | 8344 | av_freep(&sti->info->duration_error); | |
3119 | 8344 | av_freep(&sti->info); | |
3120 | } | ||
3121 | |||
3122 |
2/2✓ Branch 1 taken 8236 times.
✓ Branch 2 taken 108 times.
|
8344 | if (avcodec_is_open(sti->avctx)) { |
3123 | 8236 | err = codec_close(sti); | |
3124 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 8236 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
8236 | if (err < 0 && ret >= 0) |
3125 | ✗ | ret = err; | |
3126 | } | ||
3127 | |||
3128 | 8344 | av_bsf_free(&sti->extract_extradata.bsf); | |
3129 | } | ||
3130 |
2/2✓ Branch 0 taken 4499 times.
✓ Branch 1 taken 3036 times.
|
7535 | if (ic->pb) { |
3131 | 4499 | FFIOContext *const ctx = ffiocontext(ic->pb); | |
3132 | 4499 | av_log(ic, AV_LOG_DEBUG, "After avformat_find_stream_info() pos: %"PRId64" bytes read:%"PRId64" seeks:%d frames:%d\n", | |
3133 | avio_tell(ic->pb), ctx->bytes_read, ctx->seek_count, count); | ||
3134 | } | ||
3135 | 7535 | return ret; | |
3136 | |||
3137 | ✗ | unref_then_goto_end: | |
3138 | ✗ | av_packet_unref(pkt1); | |
3139 | ✗ | goto find_stream_info_err; | |
3140 | } | ||
3141 |