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 | 2472151 | static int64_t wrap_timestamp(const AVStream *st, int64_t timestamp) | |
54 | { | ||
55 | 2472151 | const FFStream *const sti = cffstream(st); | |
56 |
4/4✓ Branch 0 taken 150401 times.
✓ Branch 1 taken 2321750 times.
✓ Branch 2 taken 145811 times.
✓ Branch 3 taken 4590 times.
|
2472151 | if (sti->pts_wrap_behavior != AV_PTS_WRAP_IGNORE && st->pts_wrap_bits < 64 && |
57 |
3/4✓ Branch 0 taken 145811 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 88217 times.
✓ Branch 3 taken 57594 times.
|
145811 | sti->pts_wrap_reference != AV_NOPTS_VALUE && timestamp != AV_NOPTS_VALUE) { |
58 |
2/2✓ Branch 0 taken 88013 times.
✓ Branch 1 taken 204 times.
|
88217 | if (sti->pts_wrap_behavior == AV_PTS_WRAP_ADD_OFFSET && |
59 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 88013 times.
|
88013 | timestamp < sti->pts_wrap_reference) |
60 | ✗ | return timestamp + (1ULL << st->pts_wrap_bits); | |
61 |
2/2✓ Branch 0 taken 204 times.
✓ Branch 1 taken 88013 times.
|
88217 | 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 | 2472101 | return timestamp; | |
66 | } | ||
67 | |||
68 | 303733 | int64_t ff_wrap_timestamp(const AVStream *st, int64_t timestamp) | |
69 | { | ||
70 | 303733 | return wrap_timestamp(st, timestamp); | |
71 | } | ||
72 | |||
73 | 9620 | 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 9285 times.
|
9620 | if (codec_id == AV_CODEC_ID_H264) |
81 | 335 | return avcodec_find_decoder_by_name("h264"); | |
82 | #endif | ||
83 | |||
84 | 9285 | codec = ff_find_decoder(s, st, codec_id); | |
85 |
2/2✓ Branch 0 taken 171 times.
✓ Branch 1 taken 9114 times.
|
9285 | if (!codec) |
86 | 171 | return NULL; | |
87 | |||
88 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9114 times.
|
9114 | 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 | 9114 | 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 | { "dts", AV_CODEC_ID_DTS, AVMEDIA_TYPE_AUDIO }, | ||
115 | { "dvbsub", AV_CODEC_ID_DVB_SUBTITLE, AVMEDIA_TYPE_SUBTITLE }, | ||
116 | { "dvbtxt", AV_CODEC_ID_DVB_TELETEXT, AVMEDIA_TYPE_SUBTITLE }, | ||
117 | { "eac3", AV_CODEC_ID_EAC3, AVMEDIA_TYPE_AUDIO }, | ||
118 | { "h264", AV_CODEC_ID_H264, AVMEDIA_TYPE_VIDEO }, | ||
119 | { "hevc", AV_CODEC_ID_HEVC, AVMEDIA_TYPE_VIDEO }, | ||
120 | { "loas", AV_CODEC_ID_AAC_LATM, AVMEDIA_TYPE_AUDIO }, | ||
121 | { "m4v", AV_CODEC_ID_MPEG4, AVMEDIA_TYPE_VIDEO }, | ||
122 | { "mjpeg_2000", AV_CODEC_ID_JPEG2000, AVMEDIA_TYPE_VIDEO }, | ||
123 | { "mp3", AV_CODEC_ID_MP3, AVMEDIA_TYPE_AUDIO }, | ||
124 | { "mpegvideo", AV_CODEC_ID_MPEG2VIDEO, AVMEDIA_TYPE_VIDEO }, | ||
125 | { "truehd", AV_CODEC_ID_TRUEHD, AVMEDIA_TYPE_AUDIO }, | ||
126 | { "evc", AV_CODEC_ID_EVC, AVMEDIA_TYPE_VIDEO }, | ||
127 | { "vvc", AV_CODEC_ID_VVC, AVMEDIA_TYPE_VIDEO }, | ||
128 | { 0 } | ||
129 | }; | ||
130 | int score; | ||
131 | 3025 | const AVInputFormat *fmt = av_probe_input_format3(pd, 1, &score); | |
132 | 3025 | FFStream *const sti = ffstream(st); | |
133 | |||
134 |
2/2✓ Branch 0 taken 946 times.
✓ Branch 1 taken 2079 times.
|
3025 | if (fmt) { |
135 | 946 | av_log(s, AV_LOG_DEBUG, | |
136 | "Probe with size=%d, packets=%d detected %s with score=%d\n", | ||
137 | 946 | pd->buf_size, s->max_probe_packets - sti->probe_packets, | |
138 | 946 | fmt->name, score); | |
139 |
2/2✓ Branch 0 taken 15937 times.
✓ Branch 1 taken 924 times.
|
16861 | for (int i = 0; fmt_id_type[i].name; i++) { |
140 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 15913 times.
|
15937 | if (!strcmp(fmt->name, fmt_id_type[i].name)) { |
141 |
2/2✓ Branch 0 taken 11 times.
✓ Branch 1 taken 13 times.
|
24 | if (fmt_id_type[i].type != AVMEDIA_TYPE_AUDIO && |
142 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
|
11 | st->codecpar->sample_rate) |
143 | ✗ | continue; | |
144 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 18 times.
|
24 | if (sti->request_probe > score && |
145 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
|
6 | st->codecpar->codec_id != fmt_id_type[i].id) |
146 | 2 | continue; | |
147 | 22 | st->codecpar->codec_id = fmt_id_type[i].id; | |
148 | 22 | st->codecpar->codec_type = fmt_id_type[i].type; | |
149 | 22 | sti->need_context_update = 1; | |
150 | 22 | return score; | |
151 | } | ||
152 | } | ||
153 | } | ||
154 | 3003 | return 0; | |
155 | } | ||
156 | |||
157 | 7555 | static int init_input(AVFormatContext *s, const char *filename, | |
158 | AVDictionary **options) | ||
159 | { | ||
160 | int ret; | ||
161 | 7555 | AVProbeData pd = { filename, NULL, 0 }; | |
162 | 7555 | int score = AVPROBE_SCORE_RETRY; | |
163 | |||
164 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 7543 times.
|
7555 | if (s->pb) { |
165 | 12 | s->flags |= AVFMT_FLAG_CUSTOM_IO; | |
166 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | if (!s->iformat) |
167 | ✗ | return av_probe_input_buffer2(s->pb, &s->iformat, filename, | |
168 | ✗ | s, 0, s->format_probesize); | |
169 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | else if (s->iformat->flags & AVFMT_NOFILE) |
170 | ✗ | av_log(s, AV_LOG_WARNING, "Custom AVIOContext makes no sense and " | |
171 | "will be ignored with AVFMT_NOFILE format.\n"); | ||
172 | 12 | return 0; | |
173 | } | ||
174 | |||
175 |
4/4✓ Branch 0 taken 3692 times.
✓ Branch 1 taken 3851 times.
✓ Branch 2 taken 849 times.
✓ Branch 3 taken 2843 times.
|
7543 | if ((s->iformat && s->iformat->flags & AVFMT_NOFILE) || |
176 |
4/4✓ Branch 0 taken 3851 times.
✓ Branch 1 taken 849 times.
✓ Branch 3 taken 193 times.
✓ Branch 4 taken 3658 times.
|
4700 | (!s->iformat && (s->iformat = av_probe_input_format2(&pd, 0, &score)))) |
177 | 3036 | return score; | |
178 | |||
179 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 4507 times.
|
4507 | if ((ret = s->io_open(s, &s->pb, filename, AVIO_FLAG_READ | s->avio_flags, options)) < 0) |
180 | ✗ | return ret; | |
181 | |||
182 |
2/2✓ Branch 0 taken 849 times.
✓ Branch 1 taken 3658 times.
|
4507 | if (s->iformat) |
183 | 849 | return 0; | |
184 | 3658 | return av_probe_input_buffer2(s->pb, &s->iformat, filename, | |
185 | 3658 | s, 0, s->format_probesize); | |
186 | } | ||
187 | |||
188 | 7555 | static int update_stream_avctx(AVFormatContext *s) | |
189 | { | ||
190 | int ret; | ||
191 |
2/2✓ Branch 0 taken 8178 times.
✓ Branch 1 taken 7555 times.
|
15733 | for (unsigned i = 0; i < s->nb_streams; i++) { |
192 | 8178 | AVStream *const st = s->streams[i]; | |
193 | 8178 | FFStream *const sti = ffstream(st); | |
194 | |||
195 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8178 times.
|
8178 | if (!sti->need_context_update) |
196 | ✗ | continue; | |
197 | |||
198 | /* close parser, because it depends on the codec */ | ||
199 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 8178 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
8178 | if (sti->parser && sti->avctx->codec_id != st->codecpar->codec_id) { |
200 | ✗ | av_parser_close(sti->parser); | |
201 | ✗ | sti->parser = NULL; | |
202 | } | ||
203 | |||
204 | /* update internal codec context, for the parser */ | ||
205 | 8178 | ret = avcodec_parameters_to_context(sti->avctx, st->codecpar); | |
206 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8178 times.
|
8178 | if (ret < 0) |
207 | ✗ | return ret; | |
208 | |||
209 | 8178 | sti->codec_desc = avcodec_descriptor_get(sti->avctx->codec_id); | |
210 | |||
211 | 8178 | sti->need_context_update = 0; | |
212 | } | ||
213 | 7555 | return 0; | |
214 | } | ||
215 | |||
216 | 7555 | int avformat_open_input(AVFormatContext **ps, const char *filename, | |
217 | const AVInputFormat *fmt, AVDictionary **options) | ||
218 | { | ||
219 | FormatContextInternal *fci; | ||
220 | 7555 | AVFormatContext *s = *ps; | |
221 | FFFormatContext *si; | ||
222 | 7555 | AVDictionary *tmp = NULL; | |
223 | 7555 | ID3v2ExtraMeta *id3v2_extra_meta = NULL; | |
224 | 7555 | int ret = 0; | |
225 | |||
226 |
3/4✓ Branch 0 taken 22 times.
✓ Branch 1 taken 7533 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 22 times.
|
7555 | if (!s && !(s = avformat_alloc_context())) |
227 | ✗ | return AVERROR(ENOMEM); | |
228 | 7555 | fci = ff_fc_internal(s); | |
229 | 7555 | si = &fci->fc; | |
230 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7555 times.
|
7555 | if (!s->av_class) { |
231 | ✗ | av_log(NULL, AV_LOG_ERROR, "Input context has not been properly allocated by avformat_alloc_context() and is not NULL either\n"); | |
232 | ✗ | return AVERROR(EINVAL); | |
233 | } | ||
234 |
2/2✓ Branch 0 taken 3704 times.
✓ Branch 1 taken 3851 times.
|
7555 | if (fmt) |
235 | 3704 | s->iformat = fmt; | |
236 | |||
237 |
2/2✓ Branch 0 taken 7545 times.
✓ Branch 1 taken 10 times.
|
7555 | if (options) |
238 | 7545 | av_dict_copy(&tmp, *options, 0); | |
239 | |||
240 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 7543 times.
|
7555 | if (s->pb) // must be before any goto fail |
241 | 12 | s->flags |= AVFMT_FLAG_CUSTOM_IO; | |
242 | |||
243 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 7555 times.
|
7555 | if ((ret = av_opt_set_dict(s, &tmp)) < 0) |
244 | ✗ | goto fail; | |
245 | |||
246 |
2/4✓ Branch 0 taken 7555 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 7555 times.
|
7555 | if (!(s->url = av_strdup(filename ? filename : ""))) { |
247 | ✗ | ret = AVERROR(ENOMEM); | |
248 | ✗ | goto fail; | |
249 | } | ||
250 | |||
251 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 7555 times.
|
7555 | if ((ret = init_input(s, filename, &tmp)) < 0) |
252 | ✗ | goto fail; | |
253 | 7555 | s->probe_score = ret; | |
254 | |||
255 |
6/6✓ Branch 0 taken 7472 times.
✓ Branch 1 taken 83 times.
✓ Branch 2 taken 4436 times.
✓ Branch 3 taken 3036 times.
✓ Branch 4 taken 4435 times.
✓ Branch 5 taken 1 times.
|
7555 | if (!s->protocol_whitelist && s->pb && s->pb->protocol_whitelist) { |
256 | 4435 | s->protocol_whitelist = av_strdup(s->pb->protocol_whitelist); | |
257 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4435 times.
|
4435 | if (!s->protocol_whitelist) { |
258 | ✗ | ret = AVERROR(ENOMEM); | |
259 | ✗ | goto fail; | |
260 | } | ||
261 | } | ||
262 | |||
263 |
4/6✓ Branch 0 taken 7555 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4519 times.
✓ Branch 3 taken 3036 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 4519 times.
|
7555 | if (!s->protocol_blacklist && s->pb && s->pb->protocol_blacklist) { |
264 | ✗ | s->protocol_blacklist = av_strdup(s->pb->protocol_blacklist); | |
265 | ✗ | if (!s->protocol_blacklist) { | |
266 | ✗ | ret = AVERROR(ENOMEM); | |
267 | ✗ | goto fail; | |
268 | } | ||
269 | } | ||
270 | |||
271 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 7555 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
7555 | if (s->format_whitelist && av_match_list(s->iformat->name, s->format_whitelist, ',') <= 0) { |
272 | ✗ | av_log(s, AV_LOG_ERROR, "Format not on whitelist \'%s\'\n", s->format_whitelist); | |
273 | ✗ | ret = AVERROR(EINVAL); | |
274 | ✗ | goto fail; | |
275 | } | ||
276 | |||
277 | 7555 | avio_skip(s->pb, s->skip_initial_bytes); | |
278 | |||
279 | /* Check filename in case an image number is expected. */ | ||
280 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7555 times.
|
7555 | if (s->iformat->flags & AVFMT_NEEDNUMBER) { |
281 | ✗ | if (!av_filename_number_test(filename)) { | |
282 | ✗ | ret = AVERROR(EINVAL); | |
283 | ✗ | goto fail; | |
284 | } | ||
285 | } | ||
286 | |||
287 | 7555 | s->duration = s->start_time = AV_NOPTS_VALUE; | |
288 | |||
289 | /* Allocate private data. */ | ||
290 |
2/2✓ Branch 1 taken 7397 times.
✓ Branch 2 taken 158 times.
|
7555 | if (ffifmt(s->iformat)->priv_data_size > 0) { |
291 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 7397 times.
|
7397 | if (!(s->priv_data = av_mallocz(ffifmt(s->iformat)->priv_data_size))) { |
292 | ✗ | ret = AVERROR(ENOMEM); | |
293 | ✗ | goto fail; | |
294 | } | ||
295 |
2/2✓ Branch 0 taken 6569 times.
✓ Branch 1 taken 828 times.
|
7397 | if (s->iformat->priv_class) { |
296 | 6569 | *(const AVClass **) s->priv_data = s->iformat->priv_class; | |
297 | 6569 | av_opt_set_defaults(s->priv_data); | |
298 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 6569 times.
|
6569 | if ((ret = av_opt_set_dict(s->priv_data, &tmp)) < 0) |
299 | ✗ | goto fail; | |
300 | } | ||
301 | } | ||
302 | |||
303 | /* e.g. AVFMT_NOFILE formats will not have an AVIOContext */ | ||
304 |
2/2✓ Branch 0 taken 4519 times.
✓ Branch 1 taken 3036 times.
|
7555 | if (s->pb) |
305 | 4519 | ff_id3v2_read_dict(s->pb, &si->id3v2_meta, ID3v2_DEFAULT_MAGIC, &id3v2_extra_meta); | |
306 | |||
307 |
1/2✓ Branch 1 taken 7555 times.
✗ Branch 2 not taken.
|
7555 | if (ffifmt(s->iformat)->read_header) |
308 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 7555 times.
|
7555 | if ((ret = ffifmt(s->iformat)->read_header(s)) < 0) { |
309 | ✗ | if (ffifmt(s->iformat)->flags_internal & FF_INFMT_FLAG_INIT_CLEANUP) | |
310 | ✗ | goto close; | |
311 | ✗ | goto fail; | |
312 | } | ||
313 | |||
314 |
2/2✓ Branch 0 taken 6281 times.
✓ Branch 1 taken 1274 times.
|
7555 | if (!s->metadata) { |
315 | 6281 | s->metadata = si->id3v2_meta; | |
316 | 6281 | si->id3v2_meta = NULL; | |
317 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1274 times.
|
1274 | } else if (si->id3v2_meta) { |
318 | ✗ | av_log(s, AV_LOG_WARNING, "Discarding ID3 tags because more suitable tags were found.\n"); | |
319 | ✗ | av_dict_free(&si->id3v2_meta); | |
320 | } | ||
321 | |||
322 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 7545 times.
|
7555 | if (id3v2_extra_meta) { |
323 |
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") || |
324 | ✗ | !strcmp(s->iformat->name, "tta") || !strcmp(s->iformat->name, "wav")) { | |
325 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
|
10 | if ((ret = ff_id3v2_parse_apic(s, id3v2_extra_meta)) < 0) |
326 | ✗ | goto close; | |
327 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
|
10 | if ((ret = ff_id3v2_parse_chapters(s, id3v2_extra_meta)) < 0) |
328 | ✗ | goto close; | |
329 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
|
10 | if ((ret = ff_id3v2_parse_priv(s, id3v2_extra_meta)) < 0) |
330 | ✗ | goto close; | |
331 | } else | ||
332 | ✗ | av_log(s, AV_LOG_DEBUG, "demuxer does not support additional id3 data, skipping\n"); | |
333 | 10 | ff_id3v2_free_extra_meta(&id3v2_extra_meta); | |
334 | } | ||
335 | |||
336 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 7555 times.
|
7555 | if ((ret = avformat_queue_attached_pictures(s)) < 0) |
337 | ✗ | goto close; | |
338 | |||
339 |
4/4✓ Branch 0 taken 4519 times.
✓ Branch 1 taken 3036 times.
✓ Branch 2 taken 4083 times.
✓ Branch 3 taken 436 times.
|
7555 | if (s->pb && !si->data_offset) |
340 | 4083 | si->data_offset = avio_tell(s->pb); | |
341 | |||
342 | 7555 | fci->raw_packet_buffer_size = 0; | |
343 | |||
344 | 7555 | update_stream_avctx(s); | |
345 | |||
346 |
2/2✓ Branch 0 taken 7545 times.
✓ Branch 1 taken 10 times.
|
7555 | if (options) { |
347 | 7545 | av_dict_free(options); | |
348 | 7545 | *options = tmp; | |
349 | } | ||
350 | 7555 | *ps = s; | |
351 | 7555 | return 0; | |
352 | |||
353 | ✗ | close: | |
354 | ✗ | if (ffifmt(s->iformat)->read_close) | |
355 | ✗ | ffifmt(s->iformat)->read_close(s); | |
356 | ✗ | fail: | |
357 | ✗ | ff_id3v2_free_extra_meta(&id3v2_extra_meta); | |
358 | ✗ | av_dict_free(&tmp); | |
359 | ✗ | if (s->pb && !(s->flags & AVFMT_FLAG_CUSTOM_IO)) | |
360 | ✗ | avio_closep(&s->pb); | |
361 | ✗ | avformat_free_context(s); | |
362 | ✗ | *ps = NULL; | |
363 | ✗ | return ret; | |
364 | } | ||
365 | |||
366 | 7555 | void avformat_close_input(AVFormatContext **ps) | |
367 | { | ||
368 | AVFormatContext *s; | ||
369 | AVIOContext *pb; | ||
370 | |||
371 |
2/4✓ Branch 0 taken 7555 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 7555 times.
|
7555 | if (!ps || !*ps) |
372 | ✗ | return; | |
373 | |||
374 | 7555 | s = *ps; | |
375 | 7555 | pb = s->pb; | |
376 | |||
377 |
5/6✓ Branch 0 taken 7555 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4537 times.
✓ Branch 3 taken 3018 times.
✓ Branch 4 taken 4494 times.
✓ Branch 5 taken 43 times.
|
7555 | if ((s->iformat && strcmp(s->iformat->name, "image2") && s->iformat->flags & AVFMT_NOFILE) || |
378 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 7500 times.
|
7512 | (s->flags & AVFMT_FLAG_CUSTOM_IO)) |
379 | 55 | pb = NULL; | |
380 | |||
381 |
1/2✓ Branch 0 taken 7555 times.
✗ Branch 1 not taken.
|
7555 | if (s->iformat) |
382 |
2/2✓ Branch 1 taken 4900 times.
✓ Branch 2 taken 2655 times.
|
7555 | if (ffifmt(s->iformat)->read_close) |
383 | 4900 | ffifmt(s->iformat)->read_close(s); | |
384 | |||
385 | 7555 | avformat_free_context(s); | |
386 | |||
387 | 7555 | *ps = NULL; | |
388 | |||
389 | 7555 | avio_close(pb); | |
390 | } | ||
391 | |||
392 | 1087233 | static void force_codec_ids(AVFormatContext *s, AVStream *st) | |
393 | { | ||
394 |
5/5✓ Branch 0 taken 737242 times.
✓ Branch 1 taken 346893 times.
✓ Branch 2 taken 2968 times.
✓ Branch 3 taken 128 times.
✓ Branch 4 taken 2 times.
|
1087233 | switch (st->codecpar->codec_type) { |
395 | 737242 | case AVMEDIA_TYPE_VIDEO: | |
396 |
2/2✓ Branch 0 taken 114558 times.
✓ Branch 1 taken 622684 times.
|
737242 | if (s->video_codec_id) |
397 | 114558 | st->codecpar->codec_id = s->video_codec_id; | |
398 | 737242 | break; | |
399 | 346893 | case AVMEDIA_TYPE_AUDIO: | |
400 |
2/2✓ Branch 0 taken 2558 times.
✓ Branch 1 taken 344335 times.
|
346893 | if (s->audio_codec_id) |
401 | 2558 | st->codecpar->codec_id = s->audio_codec_id; | |
402 | 346893 | break; | |
403 | 2968 | case AVMEDIA_TYPE_SUBTITLE: | |
404 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2968 times.
|
2968 | if (s->subtitle_codec_id) |
405 | ✗ | st->codecpar->codec_id = s->subtitle_codec_id; | |
406 | 2968 | break; | |
407 | 128 | case AVMEDIA_TYPE_DATA: | |
408 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 128 times.
|
128 | if (s->data_codec_id) |
409 | ✗ | st->codecpar->codec_id = s->data_codec_id; | |
410 | 128 | break; | |
411 | } | ||
412 | 1087233 | } | |
413 | |||
414 | 22107 | static int probe_codec(AVFormatContext *s, AVStream *st, const AVPacket *pkt) | |
415 | { | ||
416 | 22107 | FormatContextInternal *const fci = ff_fc_internal(s); | |
417 | 22107 | FFStream *const sti = ffstream(st); | |
418 | |||
419 |
2/2✓ Branch 0 taken 15942 times.
✓ Branch 1 taken 6165 times.
|
22107 | if (sti->request_probe > 0) { |
420 | 15942 | AVProbeData *const pd = &sti->probe_data; | |
421 | int end; | ||
422 | 15942 | av_log(s, AV_LOG_DEBUG, "probing stream %d pp:%d\n", st->index, sti->probe_packets); | |
423 | 15942 | --sti->probe_packets; | |
424 | |||
425 |
2/2✓ Branch 0 taken 15931 times.
✓ Branch 1 taken 11 times.
|
15942 | if (pkt) { |
426 | 15931 | uint8_t *new_buf = av_realloc(pd->buf, pd->buf_size+pkt->size+AVPROBE_PADDING_SIZE); | |
427 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 15931 times.
|
15931 | if (!new_buf) { |
428 | ✗ | av_log(s, AV_LOG_WARNING, | |
429 | "Failed to reallocate probe buffer for stream %d\n", | ||
430 | st->index); | ||
431 | ✗ | goto no_packet; | |
432 | } | ||
433 | 15931 | pd->buf = new_buf; | |
434 | 15931 | memcpy(pd->buf + pd->buf_size, pkt->data, pkt->size); | |
435 | 15931 | pd->buf_size += pkt->size; | |
436 | 15931 | memset(pd->buf + pd->buf_size, 0, AVPROBE_PADDING_SIZE); | |
437 | } else { | ||
438 | 11 | no_packet: | |
439 | 11 | sti->probe_packets = 0; | |
440 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
|
11 | if (!pd->buf_size) { |
441 | ✗ | av_log(s, AV_LOG_WARNING, | |
442 | "nothing to probe for stream %d\n", st->index); | ||
443 | } | ||
444 | } | ||
445 | |||
446 |
1/2✓ Branch 0 taken 15942 times.
✗ Branch 1 not taken.
|
31884 | end = fci->raw_packet_buffer_size >= s->probesize || |
447 |
2/2✓ Branch 0 taken 497 times.
✓ Branch 1 taken 15445 times.
|
15942 | sti->probe_packets <= 0; |
448 | |||
449 |
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)) { |
450 | 3025 | int score = set_codec_from_probe_data(s, st, pd); | |
451 |
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) |
452 |
2/2✓ Branch 0 taken 497 times.
✓ Branch 1 taken 2510 times.
|
3007 | || end) { |
453 | 515 | pd->buf_size = 0; | |
454 | 515 | av_freep(&pd->buf); | |
455 | 515 | sti->request_probe = -1; | |
456 |
1/2✓ Branch 0 taken 515 times.
✗ Branch 1 not taken.
|
515 | if (st->codecpar->codec_id != AV_CODEC_ID_NONE) { |
457 | 515 | av_log(s, AV_LOG_DEBUG, "probed stream %d\n", st->index); | |
458 | } else | ||
459 | ✗ | av_log(s, AV_LOG_WARNING, "probed stream %d failed\n", st->index); | |
460 | } | ||
461 | 3025 | force_codec_ids(s, st); | |
462 | } | ||
463 | } | ||
464 | 22107 | return 0; | |
465 | } | ||
466 | |||
467 | 1084208 | static int update_wrap_reference(AVFormatContext *s, AVStream *st, int stream_index, AVPacket *pkt) | |
468 | { | ||
469 | 1084208 | FFStream *const sti = ffstream(st); | |
470 | 1084208 | int64_t ref = pkt->dts; | |
471 | int pts_wrap_behavior; | ||
472 | int64_t pts_wrap_reference; | ||
473 | AVProgram *first_program; | ||
474 | |||
475 |
2/2✓ Branch 0 taken 882729 times.
✓ Branch 1 taken 201479 times.
|
1084208 | if (ref == AV_NOPTS_VALUE) |
476 | 882729 | ref = pkt->pts; | |
477 |
7/8✓ Branch 0 taken 1016632 times.
✓ Branch 1 taken 67576 times.
✓ Branch 2 taken 48570 times.
✓ Branch 3 taken 968062 times.
✓ Branch 4 taken 300 times.
✓ Branch 5 taken 48270 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 300 times.
|
1084208 | if (sti->pts_wrap_reference != AV_NOPTS_VALUE || st->pts_wrap_bits >= 63 || ref == AV_NOPTS_VALUE || !s->correct_ts_overflow) |
478 | 1083908 | return 0; | |
479 | 300 | ref &= (1LL << st->pts_wrap_bits)-1; | |
480 | |||
481 | // reference time stamp should be 60 s before first time stamp | ||
482 | 300 | pts_wrap_reference = ref - av_rescale(60, st->time_base.den, st->time_base.num); | |
483 | // if first time stamp is not more than 1/8 and 60s before the wrap point, subtract rather than add wrap offset | ||
484 | 601 | pts_wrap_behavior = (ref < (1LL << st->pts_wrap_bits) - (1LL << st->pts_wrap_bits-3)) || | |
485 |
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)) ? |
486 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 299 times.
|
301 | AV_PTS_WRAP_ADD_OFFSET : AV_PTS_WRAP_SUB_OFFSET; |
487 | |||
488 | 300 | first_program = av_find_program_from_stream(s, NULL, stream_index); | |
489 | |||
490 |
2/2✓ Branch 0 taken 194 times.
✓ Branch 1 taken 106 times.
|
300 | if (!first_program) { |
491 | 194 | int default_stream_index = av_find_default_stream_index(s); | |
492 | 194 | FFStream *const default_sti = ffstream(s->streams[default_stream_index]); | |
493 |
2/2✓ Branch 0 taken 183 times.
✓ Branch 1 taken 11 times.
|
194 | if (default_sti->pts_wrap_reference == AV_NOPTS_VALUE) { |
494 |
2/2✓ Branch 0 taken 343 times.
✓ Branch 1 taken 183 times.
|
526 | for (unsigned i = 0; i < s->nb_streams; i++) { |
495 | 343 | FFStream *const sti = ffstream(s->streams[i]); | |
496 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 343 times.
|
343 | if (av_find_program_from_stream(s, NULL, i)) |
497 | ✗ | continue; | |
498 | 343 | sti->pts_wrap_reference = pts_wrap_reference; | |
499 | 343 | sti->pts_wrap_behavior = pts_wrap_behavior; | |
500 | } | ||
501 | } else { | ||
502 | 11 | sti->pts_wrap_reference = default_sti->pts_wrap_reference; | |
503 | 11 | sti->pts_wrap_behavior = default_sti->pts_wrap_behavior; | |
504 | } | ||
505 | } else { | ||
506 | 106 | AVProgram *program = first_program; | |
507 |
2/2✓ Branch 0 taken 106 times.
✓ Branch 1 taken 66 times.
|
172 | while (program) { |
508 |
2/2✓ Branch 0 taken 40 times.
✓ Branch 1 taken 66 times.
|
106 | if (program->pts_wrap_reference != AV_NOPTS_VALUE) { |
509 | 40 | pts_wrap_reference = program->pts_wrap_reference; | |
510 | 40 | pts_wrap_behavior = program->pts_wrap_behavior; | |
511 | 40 | break; | |
512 | } | ||
513 | 66 | program = av_find_program_from_stream(s, program, stream_index); | |
514 | } | ||
515 | |||
516 | // update every program with differing pts_wrap_reference | ||
517 | 106 | program = first_program; | |
518 |
2/2✓ Branch 0 taken 106 times.
✓ Branch 1 taken 106 times.
|
212 | while (program) { |
519 |
2/2✓ Branch 0 taken 66 times.
✓ Branch 1 taken 40 times.
|
106 | if (program->pts_wrap_reference != pts_wrap_reference) { |
520 |
2/2✓ Branch 0 taken 108 times.
✓ Branch 1 taken 66 times.
|
174 | for (unsigned i = 0; i < program->nb_stream_indexes; i++) { |
521 | 108 | FFStream *const sti = ffstream(s->streams[program->stream_index[i]]); | |
522 | 108 | sti->pts_wrap_reference = pts_wrap_reference; | |
523 | 108 | sti->pts_wrap_behavior = pts_wrap_behavior; | |
524 | } | ||
525 | |||
526 | 66 | program->pts_wrap_reference = pts_wrap_reference; | |
527 | 66 | program->pts_wrap_behavior = pts_wrap_behavior; | |
528 | } | ||
529 | 106 | program = av_find_program_from_stream(s, program, stream_index); | |
530 | } | ||
531 | } | ||
532 | 300 | return 1; | |
533 | } | ||
534 | |||
535 | 1084208 | static void update_timestamps(AVFormatContext *s, AVStream *st, AVPacket *pkt) | |
536 | { | ||
537 | 1084208 | FFStream *const sti = ffstream(st); | |
538 | |||
539 |
4/4✓ Branch 1 taken 300 times.
✓ Branch 2 taken 1083908 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 299 times.
|
1084208 | if (update_wrap_reference(s, st, pkt->stream_index, pkt) && sti->pts_wrap_behavior == AV_PTS_WRAP_SUB_OFFSET) { |
540 | // correct first time stamps to negative values | ||
541 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | if (!is_relative(sti->first_dts)) |
542 | 1 | sti->first_dts = wrap_timestamp(st, sti->first_dts); | |
543 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | if (!is_relative(st->start_time)) |
544 | 1 | st->start_time = wrap_timestamp(st, st->start_time); | |
545 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if (!is_relative(sti->cur_dts)) |
546 | ✗ | sti->cur_dts = wrap_timestamp(st, sti->cur_dts); | |
547 | } | ||
548 | |||
549 | 1084208 | pkt->dts = wrap_timestamp(st, pkt->dts); | |
550 | 1084208 | pkt->pts = wrap_timestamp(st, pkt->pts); | |
551 | |||
552 | 1084208 | force_codec_ids(s, st); | |
553 | |||
554 | /* TODO: audio: time filter; video: frame reordering (pts != dts) */ | ||
555 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1084208 times.
|
1084208 | if (s->use_wallclock_as_timestamps) |
556 | ✗ | pkt->dts = pkt->pts = av_rescale_q(av_gettime(), AV_TIME_BASE_Q, st->time_base); | |
557 | 1084208 | } | |
558 | |||
559 | /** | ||
560 | * Handle a new packet and either return it directly if possible and | ||
561 | * allow_passthrough is true or queue the packet (or drop the packet | ||
562 | * if corrupt). | ||
563 | * | ||
564 | * @return < 0 on error, 0 if the packet was passed through, | ||
565 | * 1 if it was queued or dropped | ||
566 | */ | ||
567 | 1084208 | static int handle_new_packet(AVFormatContext *s, AVPacket *pkt, int allow_passthrough) | |
568 | { | ||
569 | 1084208 | FormatContextInternal *const fci = ff_fc_internal(s); | |
570 | AVStream *st; | ||
571 | FFStream *sti; | ||
572 | int err; | ||
573 | |||
574 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1084208 times.
|
1084208 | av_assert0(pkt->stream_index < (unsigned)s->nb_streams && |
575 | "Invalid stream index.\n"); | ||
576 | |||
577 |
2/2✓ Branch 0 taken 200 times.
✓ Branch 1 taken 1084008 times.
|
1084208 | if (pkt->flags & AV_PKT_FLAG_CORRUPT) { |
578 | 400 | av_log(s, AV_LOG_WARNING, | |
579 | "Packet corrupt (stream = %d, dts = %s)%s.\n", | ||
580 | 200 | pkt->stream_index, av_ts2str(pkt->dts), | |
581 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 200 times.
|
200 | s->flags & AVFMT_FLAG_DISCARD_CORRUPT ? ", dropping it" : ""); |
582 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 200 times.
|
200 | if (s->flags & AVFMT_FLAG_DISCARD_CORRUPT) { |
583 | ✗ | av_packet_unref(pkt); | |
584 | ✗ | return 1; | |
585 | } | ||
586 | } | ||
587 | |||
588 | 1084208 | st = s->streams[pkt->stream_index]; | |
589 | 1084208 | sti = ffstream(st); | |
590 | |||
591 | 1084208 | update_timestamps(s, st, pkt); | |
592 | |||
593 |
6/6✓ Branch 0 taken 1068277 times.
✓ Branch 1 taken 15931 times.
✓ Branch 2 taken 1062154 times.
✓ Branch 3 taken 6123 times.
✓ Branch 4 taken 1062112 times.
✓ Branch 5 taken 42 times.
|
1084208 | if (sti->request_probe <= 0 && allow_passthrough && !fci->raw_packet_buffer.head) |
594 | 1062112 | return 0; | |
595 | |||
596 | 22096 | err = avpriv_packet_list_put(&fci->raw_packet_buffer, pkt, NULL, 0); | |
597 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 22096 times.
|
22096 | if (err < 0) { |
598 | ✗ | av_packet_unref(pkt); | |
599 | ✗ | return err; | |
600 | } | ||
601 | |||
602 | 22096 | pkt = &fci->raw_packet_buffer.tail->pkt; | |
603 | 22096 | fci->raw_packet_buffer_size += pkt->size; | |
604 | |||
605 | 22096 | err = probe_codec(s, st, pkt); | |
606 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 22096 times.
|
22096 | if (err < 0) |
607 | ✗ | return err; | |
608 | |||
609 | 22096 | return 1; | |
610 | } | ||
611 | |||
612 | 6123 | int ff_buffer_packet(AVFormatContext *s, AVPacket *pkt) | |
613 | { | ||
614 | 6123 | int err = handle_new_packet(s, pkt, 0); | |
615 | |||
616 | 6123 | return err < 0 ? err : 0; | |
617 | } | ||
618 | |||
619 | 1090712 | int ff_read_packet(AVFormatContext *s, AVPacket *pkt) | |
620 | { | ||
621 | 1090712 | FormatContextInternal *const fci = ff_fc_internal(s); | |
622 | int err; | ||
623 | |||
624 | #if FF_API_INIT_PACKET | ||
625 | FF_DISABLE_DEPRECATION_WARNINGS | ||
626 | 1090712 | pkt->data = NULL; | |
627 | 1090712 | pkt->size = 0; | |
628 | 1090712 | av_init_packet(pkt); | |
629 | FF_ENABLE_DEPRECATION_WARNINGS | ||
630 | #else | ||
631 | av_packet_unref(pkt); | ||
632 | #endif | ||
633 | |||
634 | 22378 | for (;;) { | |
635 | 1113090 | PacketListEntry *pktl = fci->raw_packet_buffer.head; | |
636 | |||
637 |
2/2✓ Branch 0 taken 37612 times.
✓ Branch 1 taken 1075478 times.
|
1113090 | if (pktl) { |
638 | 37612 | AVStream *const st = s->streams[pktl->pkt.stream_index]; | |
639 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 37612 times.
|
37612 | if (fci->raw_packet_buffer_size >= s->probesize) |
640 | ✗ | if ((err = probe_codec(s, st, NULL)) < 0) | |
641 | ✗ | return err; | |
642 |
2/2✓ Branch 1 taken 22143 times.
✓ Branch 2 taken 15469 times.
|
37612 | if (ffstream(st)->request_probe <= 0) { |
643 | 22143 | avpriv_packet_list_get(&fci->raw_packet_buffer, pkt); | |
644 | 22143 | fci->raw_packet_buffer_size -= pkt->size; | |
645 | 22143 | return 0; | |
646 | } | ||
647 | } | ||
648 | |||
649 | 1090947 | err = ffifmt(s->iformat)->read_packet(s, pkt); | |
650 |
2/2✓ Branch 0 taken 12862 times.
✓ Branch 1 taken 1078085 times.
|
1090947 | if (err < 0) { |
651 | 12862 | av_packet_unref(pkt); | |
652 | |||
653 | /* Some demuxers return FFERROR_REDO when they consume | ||
654 | data and discard it (ignored streams, junk, extradata). | ||
655 | We must re-call the demuxer to get the real packet. */ | ||
656 |
2/2✓ Branch 0 taken 6394 times.
✓ Branch 1 taken 6468 times.
|
12862 | if (err == FFERROR_REDO) |
657 | 6394 | continue; | |
658 |
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)) |
659 | 6457 | return err; | |
660 |
2/2✓ Branch 0 taken 11 times.
✓ Branch 1 taken 11 times.
|
22 | for (unsigned i = 0; i < s->nb_streams; i++) { |
661 | 11 | AVStream *const st = s->streams[i]; | |
662 | 11 | FFStream *const sti = ffstream(st); | |
663 |
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) |
664 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
|
11 | if ((err = probe_codec(s, st, NULL)) < 0) |
665 | ✗ | return err; | |
666 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
|
11 | av_assert0(sti->request_probe <= 0); |
667 | } | ||
668 | 11 | continue; | |
669 | } | ||
670 | |||
671 | 1078085 | err = av_packet_make_refcounted(pkt); | |
672 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1078085 times.
|
1078085 | if (err < 0) { |
673 | ✗ | av_packet_unref(pkt); | |
674 | ✗ | return err; | |
675 | } | ||
676 | |||
677 | 1078085 | err = handle_new_packet(s, pkt, 1); | |
678 |
2/2✓ Branch 0 taken 1062112 times.
✓ Branch 1 taken 15973 times.
|
1078085 | if (err <= 0) /* Error or passthrough */ |
679 | 1062112 | return err; | |
680 | } | ||
681 | } | ||
682 | |||
683 | /** | ||
684 | * Return the frame duration in seconds. Return 0 if not available. | ||
685 | */ | ||
686 | 320852 | static void compute_frame_duration(AVFormatContext *s, int *pnum, int *pden, | |
687 | AVStream *st, AVCodecParserContext *pc, | ||
688 | AVPacket *pkt) | ||
689 | { | ||
690 | 320852 | FFStream *const sti = ffstream(st); | |
691 | 320852 | AVRational codec_framerate = sti->avctx->framerate; | |
692 | int frame_size, sample_rate; | ||
693 | |||
694 | 320852 | *pnum = 0; | |
695 | 320852 | *pden = 0; | |
696 |
3/3✓ Branch 0 taken 218737 times.
✓ Branch 1 taken 100734 times.
✓ Branch 2 taken 1381 times.
|
320852 | switch (st->codecpar->codec_type) { |
697 | 218737 | case AVMEDIA_TYPE_VIDEO: | |
698 |
6/6✓ Branch 0 taken 188989 times.
✓ Branch 1 taken 29748 times.
✓ Branch 2 taken 40119 times.
✓ Branch 3 taken 148870 times.
✓ Branch 4 taken 28819 times.
✓ Branch 5 taken 11300 times.
|
218737 | if (st->r_frame_rate.num && (!pc || !codec_framerate.num)) { |
699 | 177689 | *pnum = st->r_frame_rate.den; | |
700 | 177689 | *pden = st->r_frame_rate.num; | |
701 |
2/2✓ Branch 0 taken 23635 times.
✓ Branch 1 taken 17413 times.
|
41048 | } else if ((s->iformat->flags & AVFMT_NOTIMESTAMPS) && |
702 |
2/2✓ Branch 0 taken 14673 times.
✓ Branch 1 taken 8962 times.
|
23635 | !codec_framerate.num && |
703 |
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) { |
704 | 14671 | *pnum = st->avg_frame_rate.den; | |
705 | 14671 | *pden = st->avg_frame_rate.num; | |
706 |
2/2✓ Branch 0 taken 9006 times.
✓ Branch 1 taken 17371 times.
|
26377 | } else if (st->time_base.num * 1000LL > st->time_base.den) { |
707 | 9006 | *pnum = st->time_base.num; | |
708 | 9006 | *pden = st->time_base.den; | |
709 |
2/2✓ Branch 0 taken 17346 times.
✓ Branch 1 taken 25 times.
|
17371 | } else if (codec_framerate.den * 1000LL > codec_framerate.num) { |
710 | 52038 | int ticks_per_frame = (sti->codec_desc && | |
711 |
3/4✓ Branch 0 taken 17346 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12081 times.
✓ Branch 3 taken 5265 times.
|
17346 | (sti->codec_desc->props & AV_CODEC_PROP_FIELDS)) ? 2 : 1; |
712 | 17346 | av_reduce(pnum, pden, | |
713 | 17346 | codec_framerate.den, | |
714 | 17346 | codec_framerate.num * (int64_t)ticks_per_frame, | |
715 | INT_MAX); | ||
716 | |||
717 |
4/4✓ Branch 0 taken 15520 times.
✓ Branch 1 taken 1826 times.
✓ Branch 2 taken 11452 times.
✓ Branch 3 taken 4068 times.
|
17346 | if (pc && pc->repeat_pict) { |
718 | 11452 | av_reduce(pnum, pden, | |
719 | 11452 | (*pnum) * (1LL + pc->repeat_pict), | |
720 | 11452 | (*pden), | |
721 | INT_MAX); | ||
722 | } | ||
723 | /* If this codec can be interlaced or progressive then we need | ||
724 | * a parser to compute duration of a packet. Thus if we have | ||
725 | * no parser in such case leave duration undefined. */ | ||
726 |
1/2✓ Branch 0 taken 17346 times.
✗ Branch 1 not taken.
|
17346 | if (sti->codec_desc && |
727 |
3/4✓ Branch 0 taken 12081 times.
✓ Branch 1 taken 5265 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 12081 times.
|
17346 | (sti->codec_desc->props & AV_CODEC_PROP_FIELDS) && !pc) |
728 | ✗ | *pnum = *pden = 0; | |
729 | } | ||
730 | 218737 | break; | |
731 | 100734 | case AVMEDIA_TYPE_AUDIO: | |
732 |
2/2✓ Branch 0 taken 45149 times.
✓ Branch 1 taken 55585 times.
|
100734 | if (sti->avctx_inited) { |
733 | 45149 | frame_size = av_get_audio_frame_duration(sti->avctx, pkt->size); | |
734 | 45149 | sample_rate = sti->avctx->sample_rate; | |
735 | } else { | ||
736 | 55585 | frame_size = av_get_audio_frame_duration2(st->codecpar, pkt->size); | |
737 | 55585 | sample_rate = st->codecpar->sample_rate; | |
738 | } | ||
739 |
4/4✓ Branch 0 taken 95469 times.
✓ Branch 1 taken 5265 times.
✓ Branch 2 taken 95464 times.
✓ Branch 3 taken 5 times.
|
100734 | if (frame_size <= 0 || sample_rate <= 0) |
740 | break; | ||
741 | 95464 | *pnum = frame_size; | |
742 | 95464 | *pden = sample_rate; | |
743 | 95464 | break; | |
744 | 1381 | default: | |
745 | 1381 | break; | |
746 | } | ||
747 | 320852 | } | |
748 | |||
749 | 719282 | static int has_decode_delay_been_guessed(AVStream *st) | |
750 | { | ||
751 | 719282 | FFStream *const sti = ffstream(st); | |
752 |
2/2✓ Branch 0 taken 702323 times.
✓ Branch 1 taken 16959 times.
|
719282 | if (st->codecpar->codec_id != AV_CODEC_ID_H264) return 1; |
753 |
2/2✓ Branch 0 taken 6052 times.
✓ Branch 1 taken 10907 times.
|
16959 | if (!sti->info) // if we have left find_stream_info then nb_decoded_frames won't increase anymore for stream copy |
754 | 6052 | return 1; | |
755 | #if CONFIG_H264_DECODER | ||
756 |
2/2✓ Branch 0 taken 6687 times.
✓ Branch 1 taken 4220 times.
|
10907 | if (sti->avctx->has_b_frames && |
757 |
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) |
758 | 1416 | return 1; | |
759 | #endif | ||
760 |
2/2✓ Branch 0 taken 9303 times.
✓ Branch 1 taken 188 times.
|
9491 | if (sti->avctx->has_b_frames < 3) |
761 | 9303 | return sti->nb_decoded_frames >= 7; | |
762 |
2/2✓ Branch 0 taken 36 times.
✓ Branch 1 taken 152 times.
|
188 | else if (sti->avctx->has_b_frames < 4) |
763 | 36 | return sti->nb_decoded_frames >= 18; | |
764 | else | ||
765 | 152 | return sti->nb_decoded_frames >= 20; | |
766 | } | ||
767 | |||
768 | 36690 | static PacketListEntry *get_next_pkt(AVFormatContext *s, AVStream *st, | |
769 | PacketListEntry *pktl) | ||
770 | { | ||
771 | 36690 | FormatContextInternal *const fci = ff_fc_internal(s); | |
772 | 36690 | FFFormatContext *const si = &fci->fc; | |
773 |
2/2✓ Branch 0 taken 34772 times.
✓ Branch 1 taken 1918 times.
|
36690 | if (pktl->next) |
774 | 34772 | return pktl->next; | |
775 |
2/2✓ Branch 0 taken 1888 times.
✓ Branch 1 taken 30 times.
|
1918 | if (pktl == si->packet_buffer.tail) |
776 | 1888 | return fci->parse_queue.head; | |
777 | 30 | return NULL; | |
778 | } | ||
779 | |||
780 | 521861 | static int64_t select_from_pts_buffer(AVStream *st, int64_t *pts_buffer, int64_t dts) | |
781 | { | ||
782 | 521861 | FFStream *const sti = ffstream(st); | |
783 | 1558340 | int onein_oneout = st->codecpar->codec_id != AV_CODEC_ID_H264 && | |
784 |
4/4✓ Branch 0 taken 514618 times.
✓ Branch 1 taken 7243 times.
✓ Branch 2 taken 513702 times.
✓ Branch 3 taken 916 times.
|
1035563 | st->codecpar->codec_id != AV_CODEC_ID_HEVC && |
785 |
2/2✓ Branch 0 taken 513683 times.
✓ Branch 1 taken 19 times.
|
513702 | st->codecpar->codec_id != AV_CODEC_ID_VVC; |
786 | |||
787 |
2/2✓ Branch 0 taken 8178 times.
✓ Branch 1 taken 513683 times.
|
521861 | if (!onein_oneout) { |
788 | 8178 | int delay = sti->avctx->has_b_frames; | |
789 | |||
790 |
2/2✓ Branch 0 taken 1215 times.
✓ Branch 1 taken 6963 times.
|
8178 | if (dts == AV_NOPTS_VALUE) { |
791 | 1215 | int64_t best_score = INT64_MAX; | |
792 |
2/2✓ Branch 0 taken 1391 times.
✓ Branch 1 taken 1215 times.
|
2606 | for (int i = 0; i < delay; i++) { |
793 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1391 times.
|
1391 | if (sti->pts_reorder_error_count[i]) { |
794 | ✗ | int64_t score = sti->pts_reorder_error[i] / sti->pts_reorder_error_count[i]; | |
795 | ✗ | if (score < best_score) { | |
796 | ✗ | best_score = score; | |
797 | ✗ | dts = pts_buffer[i]; | |
798 | } | ||
799 | } | ||
800 | } | ||
801 | } else { | ||
802 |
2/2✓ Branch 0 taken 11895 times.
✓ Branch 1 taken 6963 times.
|
18858 | for (int i = 0; i < delay; i++) { |
803 |
2/2✓ Branch 0 taken 11098 times.
✓ Branch 1 taken 797 times.
|
11895 | if (pts_buffer[i] != AV_NOPTS_VALUE) { |
804 | 11098 | int64_t diff = FFABS(pts_buffer[i] - dts) | |
805 | 11098 | + (uint64_t)sti->pts_reorder_error[i]; | |
806 | 11098 | diff = FFMAX(diff, sti->pts_reorder_error[i]); | |
807 | 11098 | sti->pts_reorder_error[i] = diff; | |
808 | 11098 | sti->pts_reorder_error_count[i]++; | |
809 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 11095 times.
|
11098 | if (sti->pts_reorder_error_count[i] > 250) { |
810 | 3 | sti->pts_reorder_error[i] >>= 1; | |
811 | 3 | sti->pts_reorder_error_count[i] >>= 1; | |
812 | } | ||
813 | } | ||
814 | } | ||
815 | } | ||
816 | } | ||
817 | |||
818 |
2/2✓ Branch 0 taken 1390 times.
✓ Branch 1 taken 520471 times.
|
521861 | if (dts == AV_NOPTS_VALUE) |
819 | 1390 | dts = pts_buffer[0]; | |
820 | |||
821 | 521861 | return dts; | |
822 | } | ||
823 | |||
824 | /** | ||
825 | * Updates the dts of packets of a stream in pkt_buffer, by re-ordering the pts | ||
826 | * of the packets in a window. | ||
827 | */ | ||
828 | 6087 | static void update_dts_from_pts(AVFormatContext *s, int stream_index, | |
829 | PacketListEntry *pkt_buffer) | ||
830 | { | ||
831 | 6087 | AVStream *const st = s->streams[stream_index]; | |
832 | 6087 | int delay = ffstream(st)->avctx->has_b_frames; | |
833 | |||
834 | int64_t pts_buffer[MAX_REORDER_DELAY+1]; | ||
835 | |||
836 |
2/2✓ Branch 0 taken 103479 times.
✓ Branch 1 taken 6087 times.
|
109566 | for (int i = 0; i < MAX_REORDER_DELAY + 1; i++) |
837 | 103479 | pts_buffer[i] = AV_NOPTS_VALUE; | |
838 | |||
839 |
2/2✓ Branch 1 taken 5506 times.
✓ Branch 2 taken 6087 times.
|
11593 | for (; pkt_buffer; pkt_buffer = get_next_pkt(s, st, pkt_buffer)) { |
840 |
2/2✓ Branch 0 taken 4848 times.
✓ Branch 1 taken 658 times.
|
5506 | if (pkt_buffer->pkt.stream_index != stream_index) |
841 | 4848 | continue; | |
842 | |||
843 |
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) { |
844 | 561 | pts_buffer[0] = pkt_buffer->pkt.pts; | |
845 |
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++) |
846 | 212 | FFSWAP(int64_t, pts_buffer[i], pts_buffer[i + 1]); | |
847 | |||
848 | 561 | pkt_buffer->pkt.dts = select_from_pts_buffer(st, pts_buffer, pkt_buffer->pkt.dts); | |
849 | } | ||
850 | } | ||
851 | 6087 | } | |
852 | |||
853 | 563186 | static void update_initial_timestamps(AVFormatContext *s, int stream_index, | |
854 | int64_t dts, int64_t pts, AVPacket *pkt) | ||
855 | { | ||
856 | 563186 | FormatContextInternal *const fci = ff_fc_internal(s); | |
857 | 563186 | FFFormatContext *const si = &fci->fc; | |
858 | 563186 | AVStream *const st = s->streams[stream_index]; | |
859 | 563186 | FFStream *const sti = ffstream(st); | |
860 |
2/2✓ Branch 0 taken 183552 times.
✓ Branch 1 taken 379634 times.
|
563186 | PacketListEntry *pktl = si->packet_buffer.head ? si->packet_buffer.head : fci->parse_queue.head; |
861 | |||
862 | uint64_t shift; | ||
863 | |||
864 |
4/4✓ Branch 0 taken 186109 times.
✓ Branch 1 taken 377077 times.
✓ Branch 2 taken 6150 times.
✓ Branch 3 taken 179959 times.
|
563186 | if (sti->first_dts != AV_NOPTS_VALUE || |
865 | 6150 | dts == AV_NOPTS_VALUE || | |
866 |
1/2✓ Branch 0 taken 6150 times.
✗ Branch 1 not taken.
|
6150 | sti->cur_dts == AV_NOPTS_VALUE || |
867 |
1/2✓ Branch 0 taken 6150 times.
✗ Branch 1 not taken.
|
6150 | sti->cur_dts < INT_MIN + RELATIVE_TS_BASE || |
868 |
2/4✓ Branch 0 taken 6150 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6150 times.
|
12300 | dts < INT_MIN + (sti->cur_dts - RELATIVE_TS_BASE) || |
869 | 6150 | is_relative(dts)) | |
870 | 557036 | return; | |
871 | |||
872 | 6150 | sti->first_dts = dts - (sti->cur_dts - RELATIVE_TS_BASE); | |
873 | 6150 | sti->cur_dts = dts; | |
874 | 6150 | shift = (uint64_t)sti->first_dts - RELATIVE_TS_BASE; | |
875 | |||
876 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 6150 times.
|
6150 | if (is_relative(pts)) |
877 | ✗ | pts += shift; | |
878 | |||
879 |
2/2✓ Branch 1 taken 5247 times.
✓ Branch 2 taken 6150 times.
|
11397 | for (PacketListEntry *pktl_it = pktl; pktl_it; pktl_it = get_next_pkt(s, st, pktl_it)) { |
880 |
2/2✓ Branch 0 taken 4777 times.
✓ Branch 1 taken 470 times.
|
5247 | if (pktl_it->pkt.stream_index != stream_index) |
881 | 4777 | continue; | |
882 |
2/2✓ Branch 1 taken 205 times.
✓ Branch 2 taken 265 times.
|
470 | if (is_relative(pktl_it->pkt.pts)) |
883 | 205 | pktl_it->pkt.pts += shift; | |
884 | |||
885 |
2/2✓ Branch 1 taken 233 times.
✓ Branch 2 taken 237 times.
|
470 | if (is_relative(pktl_it->pkt.dts)) |
886 | 233 | pktl_it->pkt.dts += shift; | |
887 | |||
888 |
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) { |
889 | 41 | st->start_time = pktl_it->pkt.pts; | |
890 |
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) |
891 | 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)); | |
892 | } | ||
893 | } | ||
894 | |||
895 |
2/2✓ Branch 1 taken 6069 times.
✓ Branch 2 taken 81 times.
|
6150 | if (has_decode_delay_been_guessed(st)) |
896 | 6069 | update_dts_from_pts(s, stream_index, pktl); | |
897 | |||
898 |
2/2✓ Branch 0 taken 1700 times.
✓ Branch 1 taken 4450 times.
|
6150 | if (st->start_time == AV_NOPTS_VALUE) { |
899 |
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)) { |
900 | 1700 | st->start_time = pts; | |
901 | } | ||
902 |
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) |
903 | 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)); | |
904 | } | ||
905 | } | ||
906 | |||
907 | 227369 | static void update_initial_durations(AVFormatContext *s, AVStream *st, | |
908 | int stream_index, int64_t duration) | ||
909 | { | ||
910 | 227369 | FormatContextInternal *const fci = ff_fc_internal(s); | |
911 | 227369 | FFFormatContext *const si = &fci->fc; | |
912 | 227369 | FFStream *const sti = ffstream(st); | |
913 |
2/2✓ Branch 0 taken 180244 times.
✓ Branch 1 taken 47125 times.
|
227369 | PacketListEntry *pktl = si->packet_buffer.head ? si->packet_buffer.head : fci->parse_queue.head; |
914 | 227369 | int64_t cur_dts = RELATIVE_TS_BASE; | |
915 | |||
916 |
2/2✓ Branch 0 taken 122532 times.
✓ Branch 1 taken 104837 times.
|
227369 | if (sti->first_dts != AV_NOPTS_VALUE) { |
917 |
2/2✓ Branch 0 taken 118915 times.
✓ Branch 1 taken 3617 times.
|
122532 | if (sti->update_initial_durations_done) |
918 | 118915 | return; | |
919 | 3617 | sti->update_initial_durations_done = 1; | |
920 | 3617 | cur_dts = sti->first_dts; | |
921 |
1/2✓ Branch 1 taken 5433 times.
✗ Branch 2 not taken.
|
5433 | for (; pktl; pktl = get_next_pkt(s, st, pktl)) { |
922 |
2/2✓ Branch 0 taken 3632 times.
✓ Branch 1 taken 1801 times.
|
5433 | if (pktl->pkt.stream_index == stream_index) { |
923 |
2/2✓ Branch 0 taken 3521 times.
✓ Branch 1 taken 111 times.
|
3632 | if (pktl->pkt.pts != pktl->pkt.dts || |
924 |
2/2✓ Branch 0 taken 15 times.
✓ Branch 1 taken 3506 times.
|
3521 | pktl->pkt.dts != AV_NOPTS_VALUE || |
925 |
1/2✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
|
15 | pktl->pkt.duration) |
926 | break; | ||
927 | 15 | cur_dts -= duration; | |
928 | } | ||
929 | } | ||
930 |
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) { |
931 | 33 | av_log(s, AV_LOG_DEBUG, "first_dts %s not matching first dts %s (pts %s, duration %"PRId64") in the queue\n", | |
932 | 33 | av_ts2str(sti->first_dts), av_ts2str(pktl->pkt.dts), av_ts2str(pktl->pkt.pts), pktl->pkt.duration); | |
933 | 33 | return; | |
934 | } | ||
935 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3584 times.
|
3584 | if (!pktl) { |
936 | ✗ | av_log(s, AV_LOG_DEBUG, "first_dts %s but no packet with dts in the queue\n", av_ts2str(sti->first_dts)); | |
937 | ✗ | return; | |
938 | } | ||
939 |
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; |
940 | 3584 | sti->first_dts = cur_dts; | |
941 |
2/2✓ Branch 0 taken 80343 times.
✓ Branch 1 taken 24494 times.
|
104837 | } else if (sti->cur_dts != RELATIVE_TS_BASE) |
942 | 80343 | return; | |
943 | |||
944 |
2/2✓ Branch 1 taken 51555 times.
✓ Branch 2 taken 644 times.
|
52199 | for (; pktl; pktl = get_next_pkt(s, st, pktl)) { |
945 |
2/2✓ Branch 0 taken 23965 times.
✓ Branch 1 taken 27590 times.
|
51555 | if (pktl->pkt.stream_index != stream_index) |
946 | 23965 | continue; | |
947 |
2/2✓ Branch 0 taken 567 times.
✓ Branch 1 taken 27023 times.
|
27590 | if ((pktl->pkt.pts == pktl->pkt.dts || |
948 |
2/2✓ Branch 0 taken 14 times.
✓ Branch 1 taken 553 times.
|
567 | pktl->pkt.pts == AV_NOPTS_VALUE) && |
949 |
2/2✓ Branch 0 taken 3653 times.
✓ Branch 1 taken 23384 times.
|
27037 | (pktl->pkt.dts == AV_NOPTS_VALUE || |
950 |
2/2✓ Branch 0 taken 178 times.
✓ Branch 1 taken 3475 times.
|
3653 | pktl->pkt.dts == sti->first_dts || |
951 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 176 times.
|
178 | pktl->pkt.dts == RELATIVE_TS_BASE) && |
952 |
2/2✓ Branch 0 taken 156 times.
✓ Branch 1 taken 26705 times.
|
26861 | !pktl->pkt.duration && |
953 |
1/2✓ Branch 1 taken 156 times.
✗ Branch 2 not taken.
|
156 | av_sat_add64(cur_dts, duration) == cur_dts + (uint64_t)duration |
954 | ) { | ||
955 | 156 | pktl->pkt.dts = cur_dts; | |
956 |
2/2✓ Branch 0 taken 140 times.
✓ Branch 1 taken 16 times.
|
156 | if (!sti->avctx->has_b_frames) |
957 | 140 | pktl->pkt.pts = cur_dts; | |
958 | 156 | pktl->pkt.duration = duration; | |
959 | } else | ||
960 | break; | ||
961 | 156 | cur_dts = pktl->pkt.dts + pktl->pkt.duration; | |
962 | } | ||
963 |
2/2✓ Branch 0 taken 644 times.
✓ Branch 1 taken 27434 times.
|
28078 | if (!pktl) |
964 | 644 | sti->cur_dts = cur_dts; | |
965 | } | ||
966 | |||
967 | 569011 | static void compute_pkt_fields(AVFormatContext *s, AVStream *st, | |
968 | AVCodecParserContext *pc, AVPacket *pkt, | ||
969 | int64_t next_dts, int64_t next_pts) | ||
970 | { | ||
971 | 569011 | FormatContextInternal *const fci = ff_fc_internal(s); | |
972 | 569011 | FFFormatContext *const si = &fci->fc; | |
973 | 569011 | FFStream *const sti = ffstream(st); | |
974 | int num, den, presentation_delayed, delay; | ||
975 | int64_t offset; | ||
976 | AVRational duration; | ||
977 | 1673114 | int onein_oneout = st->codecpar->codec_id != AV_CODEC_ID_H264 && | |
978 |
4/4✓ Branch 0 taken 535092 times.
✓ Branch 1 taken 33919 times.
✓ Branch 2 taken 522135 times.
✓ Branch 3 taken 12957 times.
|
1091146 | st->codecpar->codec_id != AV_CODEC_ID_HEVC && |
979 |
2/2✓ Branch 0 taken 519089 times.
✓ Branch 1 taken 3046 times.
|
522135 | st->codecpar->codec_id != AV_CODEC_ID_VVC; |
980 | |||
981 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 569011 times.
|
569011 | if (s->flags & AVFMT_FLAG_NOFILLIN) |
982 | ✗ | return; | |
983 | |||
984 |
4/4✓ Branch 0 taken 242719 times.
✓ Branch 1 taken 326292 times.
✓ Branch 2 taken 73143 times.
✓ Branch 3 taken 169576 times.
|
569011 | if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && pkt->dts != AV_NOPTS_VALUE) { |
985 |
4/4✓ Branch 0 taken 43070 times.
✓ Branch 1 taken 30073 times.
✓ Branch 2 taken 41909 times.
✓ Branch 3 taken 1161 times.
|
73143 | if (pkt->dts == pkt->pts && sti->last_dts_for_order_check != AV_NOPTS_VALUE) { |
986 |
2/2✓ Branch 0 taken 41900 times.
✓ Branch 1 taken 9 times.
|
41909 | if (sti->last_dts_for_order_check <= pkt->dts) { |
987 | 41900 | sti->dts_ordered++; | |
988 | } else { | ||
989 |
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, |
990 | "DTS %"PRIi64" < %"PRIi64" out of order\n", | ||
991 | pkt->dts, | ||
992 | sti->last_dts_for_order_check); | ||
993 | 9 | sti->dts_misordered++; | |
994 | } | ||
995 |
2/2✓ Branch 0 taken 29 times.
✓ Branch 1 taken 41880 times.
|
41909 | if (sti->dts_ordered + sti->dts_misordered > 250) { |
996 | 29 | sti->dts_ordered >>= 1; | |
997 | 29 | sti->dts_misordered >>= 1; | |
998 | } | ||
999 | } | ||
1000 | |||
1001 | 73143 | sti->last_dts_for_order_check = pkt->dts; | |
1002 |
4/4✓ Branch 0 taken 37 times.
✓ Branch 1 taken 73106 times.
✓ Branch 2 taken 20 times.
✓ Branch 3 taken 17 times.
|
73143 | if (sti->dts_ordered < 8 * sti->dts_misordered && pkt->dts == pkt->pts) |
1003 | 20 | pkt->dts = AV_NOPTS_VALUE; | |
1004 | } | ||
1005 | |||
1006 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 569011 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
569011 | if ((s->flags & AVFMT_FLAG_IGNDTS) && pkt->pts != AV_NOPTS_VALUE) |
1007 | ✗ | pkt->dts = AV_NOPTS_VALUE; | |
1008 | |||
1009 |
4/4✓ Branch 0 taken 185107 times.
✓ Branch 1 taken 383904 times.
✓ Branch 2 taken 27723 times.
✓ Branch 3 taken 157384 times.
|
569011 | if (pc && pc->pict_type == AV_PICTURE_TYPE_B |
1010 |
2/2✓ Branch 0 taken 81 times.
✓ Branch 1 taken 27642 times.
|
27723 | && !sti->avctx->has_b_frames) |
1011 | //FIXME Set low_delay = 0 when has_b_frames = 1 | ||
1012 | 81 | sti->avctx->has_b_frames = 1; | |
1013 | |||
1014 | /* do we have a video B-frame ? */ | ||
1015 | 569011 | delay = sti->avctx->has_b_frames; | |
1016 | 569011 | presentation_delayed = 0; | |
1017 | |||
1018 | /* XXX: need has_b_frame, but cannot get it if the codec is | ||
1019 | * not initialized */ | ||
1020 |
4/4✓ Branch 0 taken 46539 times.
✓ Branch 1 taken 522472 times.
✓ Branch 2 taken 42509 times.
✓ Branch 3 taken 4030 times.
|
569011 | if (delay && |
1021 |
2/2✓ Branch 0 taken 14786 times.
✓ Branch 1 taken 27723 times.
|
42509 | pc && pc->pict_type != AV_PICTURE_TYPE_B) |
1022 | 14786 | presentation_delayed = 1; | |
1023 | |||
1024 |
4/4✓ Branch 0 taken 339246 times.
✓ Branch 1 taken 229765 times.
✓ Branch 2 taken 167442 times.
✓ Branch 3 taken 171804 times.
|
569011 | if (pkt->pts != AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE && |
1025 |
3/4✓ Branch 0 taken 27440 times.
✓ Branch 1 taken 140002 times.
✓ Branch 2 taken 27440 times.
✗ Branch 3 not taken.
|
167442 | st->pts_wrap_bits < 63 && pkt->dts > INT64_MIN + (1LL << st->pts_wrap_bits) && |
1026 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 27440 times.
|
27440 | pkt->dts - (1LL << (st->pts_wrap_bits - 1)) > pkt->pts) { |
1027 | ✗ | if (is_relative(sti->cur_dts) || pkt->dts - (1LL<<(st->pts_wrap_bits - 1)) > sti->cur_dts) { | |
1028 | ✗ | pkt->dts -= 1LL << st->pts_wrap_bits; | |
1029 | } else | ||
1030 | ✗ | pkt->pts += 1LL << st->pts_wrap_bits; | |
1031 | } | ||
1032 | |||
1033 | /* Some MPEG-2 in MPEG-PS lack dts (issue #171 / input_file.mpg). | ||
1034 | * We take the conservative approach and discard both. | ||
1035 | * Note: If this is misbehaving for an H.264 file, then possibly | ||
1036 | * presentation_delayed is not set correctly. */ | ||
1037 |
4/4✓ Branch 0 taken 28564 times.
✓ Branch 1 taken 540447 times.
✓ Branch 2 taken 23406 times.
✓ Branch 3 taken 5158 times.
|
569011 | if (delay == 1 && pkt->dts == pkt->pts && |
1038 |
4/4✓ Branch 0 taken 6134 times.
✓ Branch 1 taken 17272 times.
✓ Branch 2 taken 628 times.
✓ Branch 3 taken 5506 times.
|
23406 | pkt->dts != AV_NOPTS_VALUE && presentation_delayed) { |
1039 | 628 | av_log(s, AV_LOG_DEBUG, "invalid dts/pts combination %"PRIi64"\n", pkt->dts); | |
1040 |
2/2✓ Branch 0 taken 293 times.
✓ Branch 1 taken 335 times.
|
628 | if ( strcmp(s->iformat->name, "mov,mp4,m4a,3gp,3g2,mj2") |
1041 |
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 |
1042 | 22 | pkt->dts = AV_NOPTS_VALUE; | |
1043 | } | ||
1044 | |||
1045 | 569011 | duration = av_mul_q((AVRational) {pkt->duration, 1}, st->time_base); | |
1046 |
2/2✓ Branch 0 taken 318047 times.
✓ Branch 1 taken 250964 times.
|
569011 | if (pkt->duration <= 0) { |
1047 | 318047 | compute_frame_duration(s, &num, &den, st, pc, pkt); | |
1048 |
3/4✓ Branch 0 taken 308762 times.
✓ Branch 1 taken 9285 times.
✓ Branch 2 taken 308762 times.
✗ Branch 3 not taken.
|
318047 | if (den && num) { |
1049 | 308762 | duration = (AVRational) {num, den}; | |
1050 | 308762 | pkt->duration = av_rescale_rnd(1, | |
1051 | 308762 | num * (int64_t) st->time_base.den, | |
1052 | 308762 | den * (int64_t) st->time_base.num, | |
1053 | AV_ROUND_DOWN); | ||
1054 | } | ||
1055 | } | ||
1056 | |||
1057 |
6/6✓ Branch 0 taken 556904 times.
✓ Branch 1 taken 12107 times.
✓ Branch 2 taken 376660 times.
✓ Branch 3 taken 180244 times.
✓ Branch 4 taken 47125 times.
✓ Branch 5 taken 329535 times.
|
569011 | if (pkt->duration > 0 && (si->packet_buffer.head || fci->parse_queue.head)) |
1058 | 227369 | update_initial_durations(s, st, pkt->stream_index, pkt->duration); | |
1059 | |||
1060 | /* Correct timestamps with byte offset if demuxers only have timestamps | ||
1061 | * on packet boundaries */ | ||
1062 |
5/6✓ Branch 0 taken 185107 times.
✓ Branch 1 taken 383904 times.
✓ Branch 2 taken 960 times.
✓ Branch 3 taken 184147 times.
✓ Branch 4 taken 960 times.
✗ Branch 5 not taken.
|
569011 | if (pc && sti->need_parsing == AVSTREAM_PARSE_TIMESTAMPS && pkt->size) { |
1063 | /* this will estimate bitrate based on this frame's duration and size */ | ||
1064 | 960 | offset = av_rescale(pc->offset, pkt->duration, pkt->size); | |
1065 |
2/2✓ Branch 0 taken 575 times.
✓ Branch 1 taken 385 times.
|
960 | if (pkt->pts != AV_NOPTS_VALUE) |
1066 | 575 | pkt->pts += offset; | |
1067 |
2/2✓ Branch 0 taken 121 times.
✓ Branch 1 taken 839 times.
|
960 | if (pkt->dts != AV_NOPTS_VALUE) |
1068 | 121 | pkt->dts += offset; | |
1069 | } | ||
1070 | |||
1071 | /* This may be redundant, but it should not hurt. */ | ||
1072 |
2/2✓ Branch 0 taken 197625 times.
✓ Branch 1 taken 371386 times.
|
569011 | if (pkt->dts != AV_NOPTS_VALUE && |
1073 |
2/2✓ Branch 0 taken 167420 times.
✓ Branch 1 taken 30205 times.
|
197625 | pkt->pts != AV_NOPTS_VALUE && |
1074 |
2/2✓ Branch 0 taken 5158 times.
✓ Branch 1 taken 162262 times.
|
167420 | pkt->pts > pkt->dts) |
1075 | 5158 | presentation_delayed = 1; | |
1076 | |||
1077 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 569011 times.
|
569011 | if (s->debug & FF_FDEBUG_TS) |
1078 | ✗ | av_log(s, AV_LOG_DEBUG, | |
1079 | "IN delayed:%d pts:%s, dts:%s cur_dts:%s st:%d pc:%p duration:%"PRId64" delay:%d onein_oneout:%d\n", | ||
1080 | ✗ | presentation_delayed, av_ts2str(pkt->pts), av_ts2str(pkt->dts), av_ts2str(sti->cur_dts), | |
1081 | pkt->stream_index, pc, pkt->duration, delay, onein_oneout); | ||
1082 | |||
1083 | /* Interpolate PTS and DTS if they are not present. We skip H264 | ||
1084 | * currently because delay and has_b_frames are not reliably set. */ | ||
1085 |
8/8✓ Branch 0 taken 46539 times.
✓ Branch 1 taken 522472 times.
✓ Branch 2 taken 28564 times.
✓ Branch 3 taken 17975 times.
✓ Branch 4 taken 25111 times.
✓ Branch 5 taken 3453 times.
✓ Branch 6 taken 515649 times.
✓ Branch 7 taken 31934 times.
|
569011 | if ((delay == 0 || (delay == 1 && pc)) && |
1086 | onein_oneout) { | ||
1087 |
2/2✓ Branch 0 taken 5459 times.
✓ Branch 1 taken 510190 times.
|
515649 | if (presentation_delayed) { |
1088 | /* DTS = decompression timestamp */ | ||
1089 | /* PTS = presentation timestamp */ | ||
1090 |
2/2✓ Branch 0 taken 2846 times.
✓ Branch 1 taken 2613 times.
|
5459 | if (pkt->dts == AV_NOPTS_VALUE) |
1091 | 2846 | pkt->dts = sti->last_IP_pts; | |
1092 | 5459 | update_initial_timestamps(s, pkt->stream_index, pkt->dts, pkt->pts, pkt); | |
1093 |
2/2✓ Branch 0 taken 2507 times.
✓ Branch 1 taken 2952 times.
|
5459 | if (pkt->dts == AV_NOPTS_VALUE) |
1094 | 2507 | pkt->dts = sti->cur_dts; | |
1095 | |||
1096 | /* This is tricky: the dts must be incremented by the duration | ||
1097 | * of the frame we are displaying, i.e. the last I- or P-frame. */ | ||
1098 |
3/4✓ Branch 0 taken 238 times.
✓ Branch 1 taken 5221 times.
✓ Branch 2 taken 238 times.
✗ Branch 3 not taken.
|
5459 | if (sti->last_IP_duration == 0 && (uint64_t)pkt->duration <= INT32_MAX) |
1099 | 238 | sti->last_IP_duration = pkt->duration; | |
1100 |
1/2✓ Branch 0 taken 5459 times.
✗ Branch 1 not taken.
|
5459 | if (pkt->dts != AV_NOPTS_VALUE) |
1101 | 5459 | sti->cur_dts = av_sat_add64(pkt->dts, sti->last_IP_duration); | |
1102 |
1/2✓ Branch 0 taken 5459 times.
✗ Branch 1 not taken.
|
5459 | if (pkt->dts != AV_NOPTS_VALUE && |
1103 |
2/2✓ Branch 0 taken 3228 times.
✓ Branch 1 taken 2231 times.
|
5459 | pkt->pts == AV_NOPTS_VALUE && |
1104 |
2/2✓ Branch 0 taken 3226 times.
✓ Branch 1 taken 2 times.
|
3228 | sti->last_IP_duration > 0 && |
1105 |
4/4✓ Branch 0 taken 738 times.
✓ Branch 1 taken 2488 times.
✓ Branch 2 taken 732 times.
✓ Branch 3 taken 6 times.
|
3226 | ((uint64_t)sti->cur_dts - (uint64_t)next_dts + 1) <= 2 && |
1106 |
2/2✓ Branch 0 taken 11 times.
✓ Branch 1 taken 721 times.
|
732 | next_dts != next_pts && |
1107 | next_pts != AV_NOPTS_VALUE) | ||
1108 | 11 | pkt->pts = next_dts; | |
1109 | |||
1110 |
1/2✓ Branch 0 taken 5459 times.
✗ Branch 1 not taken.
|
5459 | if ((uint64_t)pkt->duration <= INT32_MAX) |
1111 | 5459 | sti->last_IP_duration = pkt->duration; | |
1112 | 5459 | sti->last_IP_pts = pkt->pts; | |
1113 | /* Cannot compute PTS if not present (we can compute it only | ||
1114 | * by knowing the future. */ | ||
1115 |
2/2✓ Branch 0 taken 185057 times.
✓ Branch 1 taken 325133 times.
|
510190 | } else if (pkt->pts != AV_NOPTS_VALUE || |
1116 |
2/2✓ Branch 0 taken 155652 times.
✓ Branch 1 taken 29405 times.
|
185057 | pkt->dts != AV_NOPTS_VALUE || |
1117 |
2/2✓ Branch 0 taken 153267 times.
✓ Branch 1 taken 2385 times.
|
155652 | pkt->duration > 0 ) { |
1118 | |||
1119 | /* presentation is not delayed : PTS and DTS are the same */ | ||
1120 |
2/2✓ Branch 0 taken 182672 times.
✓ Branch 1 taken 325133 times.
|
507805 | if (pkt->pts == AV_NOPTS_VALUE) |
1121 | 182672 | pkt->pts = pkt->dts; | |
1122 | 507805 | update_initial_timestamps(s, pkt->stream_index, pkt->pts, | |
1123 | pkt->pts, pkt); | ||
1124 |
2/2✓ Branch 0 taken 153267 times.
✓ Branch 1 taken 354538 times.
|
507805 | if (pkt->pts == AV_NOPTS_VALUE) |
1125 | 153267 | pkt->pts = sti->cur_dts; | |
1126 | 507805 | pkt->dts = pkt->pts; | |
1127 |
3/4✓ Branch 0 taken 507805 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 507797 times.
✓ Branch 3 taken 8 times.
|
507805 | if (pkt->pts != AV_NOPTS_VALUE && duration.num >= 0) |
1128 | 507797 | sti->cur_dts = av_add_stable(st->time_base, pkt->pts, duration, 1); | |
1129 | } | ||
1130 | } | ||
1131 | |||
1132 |
3/4✓ Branch 0 taken 521929 times.
✓ Branch 1 taken 47082 times.
✓ Branch 2 taken 521929 times.
✗ Branch 3 not taken.
|
569011 | if (pkt->pts != AV_NOPTS_VALUE && delay <= MAX_REORDER_DELAY) { |
1133 | 521929 | sti->pts_buffer[0] = pkt->pts; | |
1134 |
4/4✓ Branch 0 taken 22152 times.
✓ Branch 1 taken 516632 times.
✓ Branch 2 taken 16855 times.
✓ Branch 3 taken 5297 times.
|
538784 | for (int i = 0; i < delay && sti->pts_buffer[i] > sti->pts_buffer[i + 1]; i++) |
1135 | 16855 | FFSWAP(int64_t, sti->pts_buffer[i], sti->pts_buffer[i + 1]); | |
1136 | |||
1137 |
2/2✓ Branch 1 taken 521300 times.
✓ Branch 2 taken 629 times.
|
521929 | if (has_decode_delay_been_guessed(st)) |
1138 | 521300 | pkt->dts = select_from_pts_buffer(st, sti->pts_buffer, pkt->dts); | |
1139 | } | ||
1140 | // We skipped it above so we try here. | ||
1141 |
2/2✓ Branch 0 taken 49922 times.
✓ Branch 1 taken 519089 times.
|
569011 | if (!onein_oneout) |
1142 | // This should happen on the first packet | ||
1143 | 49922 | update_initial_timestamps(s, pkt->stream_index, pkt->dts, pkt->pts, pkt); | |
1144 |
2/2✓ Branch 0 taken 7967 times.
✓ Branch 1 taken 561044 times.
|
569011 | if (pkt->dts > sti->cur_dts) |
1145 | 7967 | sti->cur_dts = pkt->dts; | |
1146 | |||
1147 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 569011 times.
|
569011 | if (s->debug & FF_FDEBUG_TS) |
1148 | ✗ | av_log(s, AV_LOG_DEBUG, "OUTdelayed:%d/%d pts:%s, dts:%s cur_dts:%s st:%d (%d)\n", | |
1149 | ✗ | presentation_delayed, delay, av_ts2str(pkt->pts), av_ts2str(pkt->dts), av_ts2str(sti->cur_dts), st->index, st->id); | |
1150 | |||
1151 | /* update flags */ | ||
1152 |
4/4✓ Branch 0 taken 568926 times.
✓ Branch 1 taken 85 times.
✓ Branch 3 taken 372848 times.
✓ Branch 4 taken 196078 times.
|
569011 | if (st->codecpar->codec_type == AVMEDIA_TYPE_DATA || ff_is_intra_only(st->codecpar->codec_id)) |
1153 | 372933 | pkt->flags |= AV_PKT_FLAG_KEY; | |
1154 | } | ||
1155 | |||
1156 | /** | ||
1157 | * Parse a packet, add all split parts to parse_queue. | ||
1158 | * | ||
1159 | * @param pkt Packet to parse; must not be NULL. | ||
1160 | * @param flush Indicates whether to flush. If set, pkt must be blank. | ||
1161 | */ | ||
1162 | 695692 | static int parse_packet(AVFormatContext *s, AVPacket *pkt, | |
1163 | int stream_index, int flush) | ||
1164 | { | ||
1165 | 695692 | FormatContextInternal *const fci = ff_fc_internal(s); | |
1166 | 695692 | FFFormatContext *const si = &fci->fc; | |
1167 | 695692 | AVPacket *out_pkt = si->parse_pkt; | |
1168 | 695692 | AVStream *st = s->streams[stream_index]; | |
1169 | 695692 | FFStream *const sti = ffstream(st); | |
1170 | 695692 | const uint8_t *data = pkt->data; | |
1171 | 695692 | int size = pkt->size; | |
1172 | 695692 | int ret = 0, got_output = flush; | |
1173 | |||
1174 |
5/6✓ Branch 0 taken 2042 times.
✓ Branch 1 taken 693650 times.
✓ Branch 2 taken 16 times.
✓ Branch 3 taken 2026 times.
✓ Branch 4 taken 16 times.
✗ Branch 5 not taken.
|
695692 | if (!size && !flush && sti->parser->flags & PARSER_FLAG_COMPLETE_FRAMES) { |
1175 | // preserve 0-size sync packets | ||
1176 | 16 | compute_pkt_fields(s, st, sti->parser, pkt, AV_NOPTS_VALUE, AV_NOPTS_VALUE); | |
1177 | } | ||
1178 | |||
1179 |
6/6✓ Branch 0 taken 809709 times.
✓ Branch 1 taken 698879 times.
✓ Branch 2 taken 5213 times.
✓ Branch 3 taken 693666 times.
✓ Branch 4 taken 3187 times.
✓ Branch 5 taken 2026 times.
|
1508588 | while (size > 0 || (flush && got_output)) { |
1180 | 812896 | int64_t next_pts = pkt->pts; | |
1181 | 812896 | int64_t next_dts = pkt->dts; | |
1182 | int len; | ||
1183 | |||
1184 | 812896 | len = av_parser_parse2(sti->parser, sti->avctx, | |
1185 | &out_pkt->data, &out_pkt->size, data, size, | ||
1186 | pkt->pts, pkt->dts, pkt->pos); | ||
1187 | |||
1188 | 812896 | pkt->pts = pkt->dts = AV_NOPTS_VALUE; | |
1189 | 812896 | pkt->pos = -1; | |
1190 | /* increment read pointer */ | ||
1191 | av_assert1(data || !len); | ||
1192 |
2/2✓ Branch 0 taken 800045 times.
✓ Branch 1 taken 12851 times.
|
812896 | data = len ? data + len : data; |
1193 | 812896 | size -= len; | |
1194 | |||
1195 | 812896 | got_output = !!out_pkt->size; | |
1196 | |||
1197 |
2/2✓ Branch 0 taken 627805 times.
✓ Branch 1 taken 185091 times.
|
812896 | if (!out_pkt->size) |
1198 | 627805 | continue; | |
1199 | |||
1200 |
4/4✓ Branch 0 taken 183930 times.
✓ Branch 1 taken 1161 times.
✓ Branch 2 taken 67519 times.
✓ Branch 3 taken 116411 times.
|
185091 | if (pkt->buf && out_pkt->data == pkt->data) { |
1201 | /* reference pkt->buf only when out_pkt->data is guaranteed to point | ||
1202 | * to data in it and not in the parser's internal buffer. */ | ||
1203 | /* XXX: Ensure this is the case with all parsers when sti->parser->flags | ||
1204 | * is PARSER_FLAG_COMPLETE_FRAMES and check for that instead? */ | ||
1205 | 67519 | out_pkt->buf = av_buffer_ref(pkt->buf); | |
1206 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 67519 times.
|
67519 | if (!out_pkt->buf) { |
1207 | ✗ | ret = AVERROR(ENOMEM); | |
1208 | ✗ | goto fail; | |
1209 | } | ||
1210 | } else { | ||
1211 | 117572 | ret = av_packet_make_refcounted(out_pkt); | |
1212 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 117572 times.
|
117572 | if (ret < 0) |
1213 | ✗ | goto fail; | |
1214 | } | ||
1215 | |||
1216 |
2/2✓ Branch 0 taken 9162 times.
✓ Branch 1 taken 175929 times.
|
185091 | if (pkt->side_data) { |
1217 | 9162 | out_pkt->side_data = pkt->side_data; | |
1218 | 9162 | out_pkt->side_data_elems = pkt->side_data_elems; | |
1219 | 9162 | pkt->side_data = NULL; | |
1220 | 9162 | pkt->side_data_elems = 0; | |
1221 | } | ||
1222 | |||
1223 | /* set the duration */ | ||
1224 |
2/2✓ Branch 0 taken 47310 times.
✓ Branch 1 taken 137781 times.
|
185091 | out_pkt->duration = (sti->parser->flags & PARSER_FLAG_COMPLETE_FRAMES) ? pkt->duration : 0; |
1225 |
2/2✓ Branch 0 taken 109258 times.
✓ Branch 1 taken 75833 times.
|
185091 | if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) { |
1226 |
2/2✓ Branch 0 taken 109151 times.
✓ Branch 1 taken 107 times.
|
109258 | if (sti->avctx->sample_rate > 0) { |
1227 | 109151 | out_pkt->duration = | |
1228 | 109151 | av_rescale_q_rnd(sti->parser->duration, | |
1229 | 109151 | (AVRational) { 1, sti->avctx->sample_rate }, | |
1230 | st->time_base, | ||
1231 | AV_ROUND_DOWN); | ||
1232 | } | ||
1233 |
2/2✓ Branch 0 taken 1822 times.
✓ Branch 1 taken 74011 times.
|
75833 | } else if (st->codecpar->codec_id == AV_CODEC_ID_GIF) { |
1234 |
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 && |
1235 |
1/2✓ Branch 0 taken 1822 times.
✗ Branch 1 not taken.
|
1822 | sti->parser->duration) { |
1236 | 1822 | out_pkt->duration = sti->parser->duration; | |
1237 | } | ||
1238 | } | ||
1239 | |||
1240 | 185091 | out_pkt->stream_index = st->index; | |
1241 | 185091 | out_pkt->pts = sti->parser->pts; | |
1242 | 185091 | out_pkt->dts = sti->parser->dts; | |
1243 | 185091 | out_pkt->pos = sti->parser->pos; | |
1244 | 185091 | out_pkt->flags |= pkt->flags & (AV_PKT_FLAG_DISCARD | AV_PKT_FLAG_CORRUPT); | |
1245 | |||
1246 |
2/2✓ Branch 0 taken 114943 times.
✓ Branch 1 taken 70148 times.
|
185091 | if (sti->need_parsing == AVSTREAM_PARSE_FULL_RAW) |
1247 | 114943 | out_pkt->pos = sti->parser->frame_offset; | |
1248 | |||
1249 |
2/2✓ Branch 0 taken 173140 times.
✓ Branch 1 taken 11951 times.
|
185091 | if (sti->parser->key_frame == 1 || |
1250 |
2/2✓ Branch 0 taken 93666 times.
✓ Branch 1 taken 79474 times.
|
173140 | (sti->parser->key_frame == -1 && |
1251 |
2/2✓ Branch 0 taken 81774 times.
✓ Branch 1 taken 11892 times.
|
93666 | sti->parser->pict_type == AV_PICTURE_TYPE_I)) |
1252 | 93725 | out_pkt->flags |= AV_PKT_FLAG_KEY; | |
1253 | |||
1254 |
6/6✓ Branch 0 taken 93666 times.
✓ Branch 1 taken 91425 times.
✓ Branch 2 taken 317 times.
✓ Branch 3 taken 93349 times.
✓ Branch 4 taken 288 times.
✓ Branch 5 taken 29 times.
|
185091 | if (sti->parser->key_frame == -1 && sti->parser->pict_type ==AV_PICTURE_TYPE_NONE && (pkt->flags&AV_PKT_FLAG_KEY)) |
1255 | 288 | out_pkt->flags |= AV_PKT_FLAG_KEY; | |
1256 | |||
1257 | 185091 | compute_pkt_fields(s, st, sti->parser, out_pkt, next_dts, next_pts); | |
1258 | |||
1259 | 185091 | ret = avpriv_packet_list_put(&fci->parse_queue, | |
1260 | out_pkt, NULL, 0); | ||
1261 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 185091 times.
|
185091 | if (ret < 0) |
1262 | ✗ | goto fail; | |
1263 | } | ||
1264 | |||
1265 | /* end of the stream => close and free the parser */ | ||
1266 |
2/2✓ Branch 0 taken 693666 times.
✓ Branch 1 taken 2026 times.
|
695692 | if (flush) { |
1267 | 2026 | av_parser_close(sti->parser); | |
1268 | 2026 | sti->parser = NULL; | |
1269 | } | ||
1270 | |||
1271 | 693666 | fail: | |
1272 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 695692 times.
|
695692 | if (ret < 0) |
1273 | ✗ | av_packet_unref(out_pkt); | |
1274 | 695692 | av_packet_unref(pkt); | |
1275 | 695692 | return ret; | |
1276 | } | ||
1277 | |||
1278 | 8774 | static int64_t ts_to_samples(AVStream *st, int64_t ts) | |
1279 | { | ||
1280 | 8774 | return av_rescale(ts, st->time_base.num * st->codecpar->sample_rate, st->time_base.den); | |
1281 | } | ||
1282 | |||
1283 | 8248 | static int codec_close(FFStream *sti) | |
1284 | { | ||
1285 | 8248 | AVCodecContext *avctx_new = NULL; | |
1286 | 8248 | AVCodecParameters *par_tmp = NULL; | |
1287 | int ret; | ||
1288 | |||
1289 | 8248 | avctx_new = avcodec_alloc_context3(sti->avctx->codec); | |
1290 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8248 times.
|
8248 | if (!avctx_new) { |
1291 | ✗ | ret = AVERROR(ENOMEM); | |
1292 | ✗ | goto fail; | |
1293 | } | ||
1294 | |||
1295 | 8248 | par_tmp = avcodec_parameters_alloc(); | |
1296 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8248 times.
|
8248 | if (!par_tmp) { |
1297 | ✗ | ret = AVERROR(ENOMEM); | |
1298 | ✗ | goto fail; | |
1299 | } | ||
1300 | |||
1301 | 8248 | ret = avcodec_parameters_from_context(par_tmp, sti->avctx); | |
1302 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8248 times.
|
8248 | if (ret < 0) |
1303 | ✗ | goto fail; | |
1304 | |||
1305 | 8248 | ret = avcodec_parameters_to_context(avctx_new, par_tmp); | |
1306 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8248 times.
|
8248 | if (ret < 0) |
1307 | ✗ | goto fail; | |
1308 | |||
1309 | 8248 | avctx_new->pkt_timebase = sti->avctx->pkt_timebase; | |
1310 | |||
1311 | #if FF_API_TICKS_PER_FRAME | ||
1312 | FF_DISABLE_DEPRECATION_WARNINGS | ||
1313 | 8248 | avctx_new->ticks_per_frame = sti->avctx->ticks_per_frame; | |
1314 | FF_ENABLE_DEPRECATION_WARNINGS | ||
1315 | #endif | ||
1316 | |||
1317 | 8248 | avcodec_free_context(&sti->avctx); | |
1318 | 8248 | sti->avctx = avctx_new; | |
1319 | |||
1320 | 8248 | avctx_new = NULL; | |
1321 | 8248 | ret = 0; | |
1322 | |||
1323 | 8248 | fail: | |
1324 | 8248 | avcodec_free_context(&avctx_new); | |
1325 | 8248 | avcodec_parameters_free(&par_tmp); | |
1326 | |||
1327 | 8248 | return ret; | |
1328 | } | ||
1329 | |||
1330 | static int extract_extradata(FFFormatContext *si, AVStream *st, const AVPacket *pkt); | ||
1331 | |||
1332 | 573415 | static int read_frame_internal(AVFormatContext *s, AVPacket *pkt) | |
1333 | { | ||
1334 | 573415 | FormatContextInternal *const fci = ff_fc_internal(s); | |
1335 | 573415 | FFFormatContext *const si = &fci->fc; | |
1336 | 573415 | int ret, got_packet = 0; | |
1337 | 573415 | AVDictionary *metadata = NULL; | |
1338 | |||
1339 |
4/4✓ Branch 0 taken 1267212 times.
✓ Branch 1 taken 383904 times.
✓ Branch 2 taken 1084089 times.
✓ Branch 3 taken 183123 times.
|
1651116 | while (!got_packet && !fci->parse_queue.head) { |
1340 | AVStream *st; | ||
1341 | FFStream *sti; | ||
1342 | |||
1343 | /* read next packet */ | ||
1344 | 1084089 | ret = ff_read_packet(s, pkt); | |
1345 |
2/2✓ Branch 0 taken 6388 times.
✓ Branch 1 taken 1077701 times.
|
1084089 | if (ret < 0) { |
1346 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6387 times.
|
6388 | if (ret == AVERROR(EAGAIN)) |
1347 | 1 | return ret; | |
1348 | /* flush the parsers */ | ||
1349 |
2/2✓ Branch 0 taken 7430 times.
✓ Branch 1 taken 6387 times.
|
13817 | for (unsigned i = 0; i < s->nb_streams; i++) { |
1350 | 7430 | AVStream *const st = s->streams[i]; | |
1351 | 7430 | FFStream *const sti = ffstream(st); | |
1352 |
4/4✓ Branch 0 taken 2679 times.
✓ Branch 1 taken 4751 times.
✓ Branch 2 taken 2026 times.
✓ Branch 3 taken 653 times.
|
7430 | if (sti->parser && sti->need_parsing) |
1353 | 2026 | parse_packet(s, pkt, st->index, 1); | |
1354 | } | ||
1355 | /* all remaining packets are now in parse_queue => | ||
1356 | * really terminate parsing */ | ||
1357 | 6387 | break; | |
1358 | } | ||
1359 | 1077701 | ret = 0; | |
1360 | 1077701 | st = s->streams[pkt->stream_index]; | |
1361 | 1077701 | sti = ffstream(st); | |
1362 | |||
1363 | 1077701 | st->event_flags |= AVSTREAM_EVENT_FLAG_NEW_PACKETS; | |
1364 | |||
1365 | /* update context if required */ | ||
1366 |
2/2✓ Branch 0 taken 196 times.
✓ Branch 1 taken 1077505 times.
|
1077701 | if (sti->need_context_update) { |
1367 |
2/2✓ Branch 1 taken 13 times.
✓ Branch 2 taken 183 times.
|
196 | if (avcodec_is_open(sti->avctx)) { |
1368 | 13 | av_log(s, AV_LOG_DEBUG, "Demuxer context update while decoder is open, closing and trying to re-open\n"); | |
1369 | 13 | ret = codec_close(sti); | |
1370 | 13 | sti->info->found_decoder = 0; | |
1371 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
|
13 | if (ret < 0) |
1372 | ✗ | return ret; | |
1373 | } | ||
1374 | |||
1375 | /* close parser, because it depends on the codec */ | ||
1376 |
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) { |
1377 | 10 | av_parser_close(sti->parser); | |
1378 | 10 | sti->parser = NULL; | |
1379 | } | ||
1380 | |||
1381 | 196 | ret = avcodec_parameters_to_context(sti->avctx, st->codecpar); | |
1382 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 196 times.
|
196 | if (ret < 0) { |
1383 | ✗ | av_packet_unref(pkt); | |
1384 | ✗ | return ret; | |
1385 | } | ||
1386 | |||
1387 |
2/2✓ Branch 0 taken 149 times.
✓ Branch 1 taken 47 times.
|
196 | if (!sti->avctx->extradata) { |
1388 | 149 | sti->extract_extradata.inited = 0; | |
1389 | |||
1390 | 149 | ret = extract_extradata(si, st, pkt); | |
1391 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 149 times.
|
149 | if (ret < 0) { |
1392 | ✗ | av_packet_unref(pkt); | |
1393 | ✗ | return ret; | |
1394 | } | ||
1395 | } | ||
1396 | |||
1397 | 196 | sti->codec_desc = avcodec_descriptor_get(sti->avctx->codec_id); | |
1398 | |||
1399 | 196 | sti->need_context_update = 0; | |
1400 | } | ||
1401 | |||
1402 |
2/2✓ Branch 0 taken 336646 times.
✓ Branch 1 taken 741055 times.
|
1077701 | if (pkt->pts != AV_NOPTS_VALUE && |
1403 |
2/2✓ Branch 0 taken 168460 times.
✓ Branch 1 taken 168186 times.
|
336646 | pkt->dts != AV_NOPTS_VALUE && |
1404 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 168460 times.
|
168460 | pkt->pts < pkt->dts) { |
1405 | ✗ | av_log(s, AV_LOG_WARNING, | |
1406 | "Invalid timestamps stream=%d, pts=%s, dts=%s, size=%d\n", | ||
1407 | pkt->stream_index, | ||
1408 | ✗ | av_ts2str(pkt->pts), | |
1409 | ✗ | av_ts2str(pkt->dts), | |
1410 | pkt->size); | ||
1411 | } | ||
1412 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1077701 times.
|
1077701 | if (s->debug & FF_FDEBUG_TS) |
1413 | ✗ | av_log(s, AV_LOG_DEBUG, | |
1414 | "ff_read_packet stream=%d, pts=%s, dts=%s, size=%d, duration=%"PRId64", flags=%d\n", | ||
1415 | pkt->stream_index, | ||
1416 | ✗ | av_ts2str(pkt->pts), | |
1417 | ✗ | av_ts2str(pkt->dts), | |
1418 | pkt->size, pkt->duration, pkt->flags); | ||
1419 | |||
1420 |
5/6✓ Branch 0 taken 694994 times.
✓ Branch 1 taken 382707 times.
✓ Branch 2 taken 2786 times.
✓ Branch 3 taken 692208 times.
✓ Branch 4 taken 2786 times.
✗ Branch 5 not taken.
|
1077701 | if (sti->need_parsing && !sti->parser && !(s->flags & AVFMT_FLAG_NOPARSE)) { |
1421 | 2786 | sti->parser = av_parser_init(st->codecpar->codec_id); | |
1422 |
2/2✓ Branch 0 taken 1197 times.
✓ Branch 1 taken 1589 times.
|
2786 | if (!sti->parser) { |
1423 | 1197 | av_log(s, AV_LOG_VERBOSE, "parser not found for codec " | |
1424 | "%s, packets or times may be invalid.\n", | ||
1425 | 1197 | avcodec_get_name(st->codecpar->codec_id)); | |
1426 | /* no parser available: just output the raw packets */ | ||
1427 | 1197 | sti->need_parsing = AVSTREAM_PARSE_NONE; | |
1428 |
2/2✓ Branch 0 taken 694 times.
✓ Branch 1 taken 895 times.
|
1589 | } else if (sti->need_parsing == AVSTREAM_PARSE_HEADERS) |
1429 | 694 | sti->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES; | |
1430 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 895 times.
|
895 | else if (sti->need_parsing == AVSTREAM_PARSE_FULL_ONCE) |
1431 | ✗ | sti->parser->flags |= PARSER_FLAG_ONCE; | |
1432 |
2/2✓ Branch 0 taken 471 times.
✓ Branch 1 taken 424 times.
|
895 | else if (sti->need_parsing == AVSTREAM_PARSE_FULL_RAW) |
1433 | 471 | sti->parser->flags |= PARSER_FLAG_USE_CODEC_TS; | |
1434 | } | ||
1435 | |||
1436 |
3/4✓ Branch 0 taken 693797 times.
✓ Branch 1 taken 383904 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 693797 times.
|
1077701 | if (!sti->need_parsing || !sti->parser) { |
1437 | /* no parsing needed: we just output the packet as is */ | ||
1438 | 383904 | compute_pkt_fields(s, st, NULL, pkt, AV_NOPTS_VALUE, AV_NOPTS_VALUE); | |
1439 |
2/2✓ Branch 0 taken 92981 times.
✓ Branch 1 taken 290923 times.
|
383904 | if ((s->iformat->flags & AVFMT_GENERIC_INDEX) && |
1440 |
4/4✓ Branch 0 taken 92245 times.
✓ Branch 1 taken 736 times.
✓ Branch 2 taken 92100 times.
✓ Branch 3 taken 145 times.
|
92981 | (pkt->flags & AV_PKT_FLAG_KEY) && pkt->dts != AV_NOPTS_VALUE) { |
1441 | 92100 | ff_reduce_index(s, st->index); | |
1442 | 92100 | av_add_index_entry(st, pkt->pos, pkt->dts, | |
1443 | 0, 0, AVINDEX_KEYFRAME); | ||
1444 | } | ||
1445 | 383904 | got_packet = 1; | |
1446 |
2/2✓ Branch 0 taken 693666 times.
✓ Branch 1 taken 131 times.
|
693797 | } else if (st->discard < AVDISCARD_ALL) { |
1447 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 693666 times.
|
693666 | if ((ret = parse_packet(s, pkt, pkt->stream_index, 0)) < 0) |
1448 | ✗ | return ret; | |
1449 | 693666 | st->codecpar->sample_rate = sti->avctx->sample_rate; | |
1450 | 693666 | st->codecpar->bit_rate = sti->avctx->bit_rate; | |
1451 | 693666 | ret = av_channel_layout_copy(&st->codecpar->ch_layout, &sti->avctx->ch_layout); | |
1452 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 693666 times.
|
693666 | if (ret < 0) |
1453 | ✗ | return ret; | |
1454 | 693666 | st->codecpar->codec_id = sti->avctx->codec_id; | |
1455 | } else { | ||
1456 | /* free packet */ | ||
1457 | 131 | av_packet_unref(pkt); | |
1458 | } | ||
1459 |
2/2✓ Branch 0 taken 358701 times.
✓ Branch 1 taken 719000 times.
|
1077701 | if (pkt->flags & AV_PKT_FLAG_KEY) |
1460 | 358701 | sti->skip_to_keyframe = 0; | |
1461 |
2/2✓ Branch 0 taken 85 times.
✓ Branch 1 taken 1077616 times.
|
1077701 | if (sti->skip_to_keyframe) { |
1462 | 85 | av_packet_unref(pkt); | |
1463 | 85 | got_packet = 0; | |
1464 | } | ||
1465 | } | ||
1466 | |||
1467 |
4/4✓ Branch 0 taken 189510 times.
✓ Branch 1 taken 383904 times.
✓ Branch 2 taken 184027 times.
✓ Branch 3 taken 5483 times.
|
573414 | if (!got_packet && fci->parse_queue.head) |
1468 | 184027 | ret = avpriv_packet_list_get(&fci->parse_queue, pkt); | |
1469 | |||
1470 |
2/2✓ Branch 0 taken 567931 times.
✓ Branch 1 taken 5483 times.
|
573414 | if (ret >= 0) { |
1471 | 567931 | AVStream *const st = s->streams[pkt->stream_index]; | |
1472 | 567931 | FFStream *const sti = ffstream(st); | |
1473 | 567931 | int discard_padding = 0; | |
1474 |
3/4✓ Branch 0 taken 4387 times.
✓ Branch 1 taken 563544 times.
✓ Branch 2 taken 4387 times.
✗ Branch 3 not taken.
|
567931 | if (sti->first_discard_sample && pkt->pts != AV_NOPTS_VALUE) { |
1475 |
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); |
1476 | 4387 | int64_t sample = ts_to_samples(st, pts); | |
1477 | 4387 | int64_t duration = ts_to_samples(st, pkt->duration); | |
1478 | 4387 | int64_t end_sample = sample + duration; | |
1479 |
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 && |
1480 |
1/2✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
|
14 | sample < sti->last_discard_sample) |
1481 | 14 | discard_padding = FFMIN(end_sample - sti->first_discard_sample, duration); | |
1482 | } | ||
1483 |
6/6✓ Branch 0 taken 4387 times.
✓ Branch 1 taken 563544 times.
✓ Branch 2 taken 4371 times.
✓ Branch 3 taken 16 times.
✓ Branch 4 taken 20 times.
✓ Branch 5 taken 4351 times.
|
567931 | if (sti->start_skip_samples && (pkt->pts == 0 || pkt->pts == RELATIVE_TS_BASE)) |
1484 | 36 | sti->skip_samples = sti->start_skip_samples; | |
1485 | 567931 | sti->skip_samples = FFMAX(0, sti->skip_samples); | |
1486 |
4/4✓ Branch 0 taken 567843 times.
✓ Branch 1 taken 88 times.
✓ Branch 2 taken 14 times.
✓ Branch 3 taken 567829 times.
|
567931 | if (sti->skip_samples || discard_padding) { |
1487 | 102 | uint8_t *p = av_packet_new_side_data(pkt, AV_PKT_DATA_SKIP_SAMPLES, 10); | |
1488 |
1/2✓ Branch 0 taken 102 times.
✗ Branch 1 not taken.
|
102 | if (p) { |
1489 | 102 | AV_WL32(p, sti->skip_samples); | |
1490 | 102 | AV_WL32(p + 4, discard_padding); | |
1491 | 102 | av_log(s, AV_LOG_DEBUG, "demuxer injecting skip %u / discard %u\n", | |
1492 | 102 | (unsigned)sti->skip_samples, (unsigned)discard_padding); | |
1493 | } | ||
1494 | 102 | sti->skip_samples = 0; | |
1495 | } | ||
1496 | |||
1497 | #if FF_API_AVSTREAM_SIDE_DATA | ||
1498 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 567931 times.
|
567931 | if (sti->inject_global_side_data) { |
1499 | ✗ | for (int i = 0; i < st->codecpar->nb_coded_side_data; i++) { | |
1500 | ✗ | const AVPacketSideData *const src_sd = &st->codecpar->coded_side_data[i]; | |
1501 | uint8_t *dst_data; | ||
1502 | |||
1503 | ✗ | if (av_packet_get_side_data(pkt, src_sd->type, NULL)) | |
1504 | ✗ | continue; | |
1505 | |||
1506 | ✗ | dst_data = av_packet_new_side_data(pkt, src_sd->type, src_sd->size); | |
1507 | ✗ | if (!dst_data) { | |
1508 | ✗ | av_log(s, AV_LOG_WARNING, "Could not inject global side data\n"); | |
1509 | ✗ | continue; | |
1510 | } | ||
1511 | |||
1512 | ✗ | memcpy(dst_data, src_sd->data, src_sd->size); | |
1513 | } | ||
1514 | ✗ | sti->inject_global_side_data = 0; | |
1515 | } | ||
1516 | #endif | ||
1517 | } | ||
1518 | |||
1519 |
2/2✓ Branch 0 taken 7542 times.
✓ Branch 1 taken 565872 times.
|
573414 | if (!fci->metafree) { |
1520 | 7542 | int metaret = av_opt_get_dict_val(s, "metadata", AV_OPT_SEARCH_CHILDREN, &metadata); | |
1521 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7542 times.
|
7542 | if (metadata) { |
1522 | ✗ | s->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED; | |
1523 | ✗ | av_dict_copy(&s->metadata, metadata, 0); | |
1524 | ✗ | av_dict_free(&metadata); | |
1525 | ✗ | av_opt_set_dict_val(s, "metadata", NULL, AV_OPT_SEARCH_CHILDREN); | |
1526 | } | ||
1527 | 7542 | fci->metafree = metaret == AVERROR_OPTION_NOT_FOUND; | |
1528 | } | ||
1529 | |||
1530 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 573414 times.
|
573414 | if (s->debug & FF_FDEBUG_TS) |
1531 | ✗ | av_log(s, AV_LOG_DEBUG, | |
1532 | "read_frame_internal stream=%d, pts=%s, dts=%s, " | ||
1533 | "size=%d, duration=%"PRId64", flags=%d\n", | ||
1534 | pkt->stream_index, | ||
1535 | ✗ | av_ts2str(pkt->pts), | |
1536 | ✗ | av_ts2str(pkt->dts), | |
1537 | pkt->size, pkt->duration, pkt->flags); | ||
1538 | |||
1539 | /* A demuxer might have returned EOF because of an IO error, let's | ||
1540 | * propagate this back to the user. */ | ||
1541 |
5/8✓ Branch 0 taken 5371 times.
✓ Branch 1 taken 568043 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.
|
573414 | if (ret == AVERROR_EOF && s->pb && s->pb->error < 0 && s->pb->error != AVERROR(EAGAIN)) |
1542 | ✗ | ret = s->pb->error; | |
1543 | |||
1544 | 573414 | return ret; | |
1545 | } | ||
1546 | |||
1547 | 511599 | int av_read_frame(AVFormatContext *s, AVPacket *pkt) | |
1548 | { | ||
1549 | 511599 | FFFormatContext *const si = ffformatcontext(s); | |
1550 | 511599 | const int genpts = s->flags & AVFMT_FLAG_GENPTS; | |
1551 | 511599 | int eof = 0; | |
1552 | int ret; | ||
1553 | AVStream *st; | ||
1554 | |||
1555 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 511599 times.
|
511599 | if (!genpts) { |
1556 | 1023198 | ret = si->packet_buffer.head | |
1557 | 132283 | ? avpriv_packet_list_get(&si->packet_buffer, pkt) | |
1558 |
2/2✓ Branch 0 taken 132283 times.
✓ Branch 1 taken 379316 times.
|
511599 | : read_frame_internal(s, pkt); |
1559 |
2/2✓ Branch 0 taken 4306 times.
✓ Branch 1 taken 507293 times.
|
511599 | if (ret < 0) |
1560 | 4306 | return ret; | |
1561 | 507293 | goto return_packet; | |
1562 | } | ||
1563 | |||
1564 | ✗ | for (;;) { | |
1565 | ✗ | PacketListEntry *pktl = si->packet_buffer.head; | |
1566 | |||
1567 | ✗ | if (pktl) { | |
1568 | ✗ | AVPacket *next_pkt = &pktl->pkt; | |
1569 | |||
1570 | ✗ | if (next_pkt->dts != AV_NOPTS_VALUE) { | |
1571 | ✗ | int wrap_bits = s->streams[next_pkt->stream_index]->pts_wrap_bits; | |
1572 | // last dts seen for this stream. if any of packets following | ||
1573 | // current one had no dts, we will set this to AV_NOPTS_VALUE. | ||
1574 | ✗ | int64_t last_dts = next_pkt->dts; | |
1575 | av_assert2(wrap_bits <= 64); | ||
1576 | ✗ | while (pktl && next_pkt->pts == AV_NOPTS_VALUE) { | |
1577 | ✗ | if (pktl->pkt.stream_index == next_pkt->stream_index && | |
1578 | ✗ | av_compare_mod(next_pkt->dts, pktl->pkt.dts, 2ULL << (wrap_bits - 1)) < 0) { | |
1579 | ✗ | if (av_compare_mod(pktl->pkt.pts, pktl->pkt.dts, 2ULL << (wrap_bits - 1))) { | |
1580 | // not B-frame | ||
1581 | ✗ | next_pkt->pts = pktl->pkt.dts; | |
1582 | } | ||
1583 | ✗ | if (last_dts != AV_NOPTS_VALUE) { | |
1584 | // Once last dts was set to AV_NOPTS_VALUE, we don't change it. | ||
1585 | ✗ | last_dts = pktl->pkt.dts; | |
1586 | } | ||
1587 | } | ||
1588 | ✗ | pktl = pktl->next; | |
1589 | } | ||
1590 | ✗ | if (eof && next_pkt->pts == AV_NOPTS_VALUE && last_dts != AV_NOPTS_VALUE) { | |
1591 | // Fixing the last reference frame had none pts issue (For MXF etc). | ||
1592 | // We only do this when | ||
1593 | // 1. eof. | ||
1594 | // 2. we are not able to resolve a pts value for current packet. | ||
1595 | // 3. the packets for this stream at the end of the files had valid dts. | ||
1596 | ✗ | next_pkt->pts = last_dts + next_pkt->duration; | |
1597 | } | ||
1598 | ✗ | pktl = si->packet_buffer.head; | |
1599 | } | ||
1600 | |||
1601 | /* read packet from packet buffer, if there is data */ | ||
1602 | ✗ | st = s->streams[next_pkt->stream_index]; | |
1603 | ✗ | if (!(next_pkt->pts == AV_NOPTS_VALUE && st->discard < AVDISCARD_ALL && | |
1604 | ✗ | next_pkt->dts != AV_NOPTS_VALUE && !eof)) { | |
1605 | ✗ | ret = avpriv_packet_list_get(&si->packet_buffer, pkt); | |
1606 | ✗ | goto return_packet; | |
1607 | } | ||
1608 | } | ||
1609 | |||
1610 | ✗ | ret = read_frame_internal(s, pkt); | |
1611 | ✗ | if (ret < 0) { | |
1612 | ✗ | if (pktl && ret != AVERROR(EAGAIN)) { | |
1613 | ✗ | eof = 1; | |
1614 | ✗ | continue; | |
1615 | } else | ||
1616 | ✗ | return ret; | |
1617 | } | ||
1618 | |||
1619 | ✗ | ret = avpriv_packet_list_put(&si->packet_buffer, | |
1620 | pkt, NULL, 0); | ||
1621 | ✗ | if (ret < 0) { | |
1622 | ✗ | av_packet_unref(pkt); | |
1623 | ✗ | return ret; | |
1624 | } | ||
1625 | } | ||
1626 | |||
1627 | 507293 | return_packet: | |
1628 | 507293 | st = s->streams[pkt->stream_index]; | |
1629 |
4/4✓ Branch 0 taken 194400 times.
✓ Branch 1 taken 312893 times.
✓ Branch 2 taken 120883 times.
✓ Branch 3 taken 73517 times.
|
507293 | if ((s->iformat->flags & AVFMT_GENERIC_INDEX) && pkt->flags & AV_PKT_FLAG_KEY) { |
1630 | 120883 | ff_reduce_index(s, st->index); | |
1631 | 120883 | av_add_index_entry(st, pkt->pos, pkt->dts, 0, 0, AVINDEX_KEYFRAME); | |
1632 | } | ||
1633 | |||
1634 |
2/2✓ Branch 1 taken 120654 times.
✓ Branch 2 taken 386639 times.
|
507293 | if (is_relative(pkt->dts)) |
1635 | 120654 | pkt->dts -= RELATIVE_TS_BASE; | |
1636 |
2/2✓ Branch 1 taken 118960 times.
✓ Branch 2 taken 388333 times.
|
507293 | if (is_relative(pkt->pts)) |
1637 | 118960 | pkt->pts -= RELATIVE_TS_BASE; | |
1638 | |||
1639 | 507293 | return ret; | |
1640 | } | ||
1641 | |||
1642 | /** | ||
1643 | * Return TRUE if the stream has accurate duration in any stream. | ||
1644 | * | ||
1645 | * @return TRUE if the stream has accurate duration for at least one component. | ||
1646 | */ | ||
1647 | 7465 | static int has_duration(AVFormatContext *ic) | |
1648 | { | ||
1649 |
2/2✓ Branch 0 taken 7756 times.
✓ Branch 1 taken 2416 times.
|
10172 | for (unsigned i = 0; i < ic->nb_streams; i++) { |
1650 | 7756 | const AVStream *const st = ic->streams[i]; | |
1651 |
2/2✓ Branch 0 taken 5049 times.
✓ Branch 1 taken 2707 times.
|
7756 | if (st->duration != AV_NOPTS_VALUE) |
1652 | 5049 | return 1; | |
1653 | } | ||
1654 |
2/2✓ Branch 0 taken 437 times.
✓ Branch 1 taken 1979 times.
|
2416 | if (ic->duration != AV_NOPTS_VALUE) |
1655 | 437 | return 1; | |
1656 | 1979 | return 0; | |
1657 | } | ||
1658 | |||
1659 | /** | ||
1660 | * Estimate the stream timings from the one of each components. | ||
1661 | * | ||
1662 | * Also computes the global bitrate if possible. | ||
1663 | */ | ||
1664 | 13089 | static void update_stream_timings(AVFormatContext *ic) | |
1665 | { | ||
1666 | int64_t start_time, start_time1, start_time_text, end_time, end_time1, end_time_text; | ||
1667 | int64_t duration, duration1, duration_text, filesize; | ||
1668 | |||
1669 | 13089 | start_time = INT64_MAX; | |
1670 | 13089 | start_time_text = INT64_MAX; | |
1671 | 13089 | end_time = INT64_MIN; | |
1672 | 13089 | end_time_text = INT64_MIN; | |
1673 | 13089 | duration = INT64_MIN; | |
1674 | 13089 | duration_text = INT64_MIN; | |
1675 | |||
1676 |
2/2✓ Branch 0 taken 14561 times.
✓ Branch 1 taken 13089 times.
|
27650 | for (unsigned i = 0; i < ic->nb_streams; i++) { |
1677 | 14561 | AVStream *const st = ic->streams[i]; | |
1678 |
2/2✓ Branch 0 taken 14436 times.
✓ Branch 1 taken 125 times.
|
28997 | int is_text = st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE || |
1679 |
2/2✓ Branch 0 taken 151 times.
✓ Branch 1 taken 14285 times.
|
14436 | st->codecpar->codec_type == AVMEDIA_TYPE_DATA; |
1680 | |||
1681 |
3/4✓ Branch 0 taken 11910 times.
✓ Branch 1 taken 2651 times.
✓ Branch 2 taken 11910 times.
✗ Branch 3 not taken.
|
14561 | if (st->start_time != AV_NOPTS_VALUE && st->time_base.den) { |
1682 | 11910 | start_time1 = av_rescale_q(st->start_time, st->time_base, | |
1683 | 11910 | AV_TIME_BASE_Q); | |
1684 |
2/2✓ Branch 0 taken 158 times.
✓ Branch 1 taken 11752 times.
|
11910 | if (is_text) |
1685 | 158 | start_time_text = FFMIN(start_time_text, start_time1); | |
1686 | else | ||
1687 | 11752 | start_time = FFMIN(start_time, start_time1); | |
1688 | 11910 | end_time1 = av_rescale_q_rnd(st->duration, st->time_base, | |
1689 | 11910 | AV_TIME_BASE_Q, | |
1690 | AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX); | ||
1691 |
5/6✓ Branch 0 taken 10526 times.
✓ Branch 1 taken 1384 times.
✓ Branch 2 taken 10497 times.
✓ Branch 3 taken 29 times.
✓ Branch 4 taken 10526 times.
✗ Branch 5 not taken.
|
11910 | if (end_time1 != AV_NOPTS_VALUE && (end_time1 > 0 ? start_time1 <= INT64_MAX - end_time1 : start_time1 >= INT64_MIN - end_time1)) { |
1692 | 10526 | end_time1 += start_time1; | |
1693 |
2/2✓ Branch 0 taken 143 times.
✓ Branch 1 taken 10383 times.
|
10526 | if (is_text) |
1694 | 143 | end_time_text = FFMAX(end_time_text, end_time1); | |
1695 | else | ||
1696 | 10383 | end_time = FFMAX(end_time, end_time1); | |
1697 | } | ||
1698 |
2/2✓ Branch 1 taken 208 times.
✓ Branch 2 taken 11910 times.
|
12118 | for (AVProgram *p = NULL; (p = av_find_program_from_stream(ic, p, i)); ) { |
1699 |
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) |
1700 | 81 | p->start_time = start_time1; | |
1701 |
2/2✓ Branch 0 taken 52 times.
✓ Branch 1 taken 156 times.
|
208 | if (p->end_time < end_time1) |
1702 | 52 | p->end_time = end_time1; | |
1703 | } | ||
1704 | } | ||
1705 |
2/2✓ Branch 0 taken 11984 times.
✓ Branch 1 taken 2577 times.
|
14561 | if (st->duration != AV_NOPTS_VALUE) { |
1706 | 11984 | duration1 = av_rescale_q(st->duration, st->time_base, | |
1707 | 11984 | AV_TIME_BASE_Q); | |
1708 |
2/2✓ Branch 0 taken 167 times.
✓ Branch 1 taken 11817 times.
|
11984 | if (is_text) |
1709 | 167 | duration_text = FFMAX(duration_text, duration1); | |
1710 | else | ||
1711 | 11817 | duration = FFMAX(duration, duration1); | |
1712 | } | ||
1713 | } | ||
1714 |
3/6✓ Branch 0 taken 10652 times.
✓ Branch 1 taken 2437 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 10652 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
13089 | if (start_time == INT64_MAX || (start_time > start_time_text && start_time - (uint64_t)start_time_text < AV_TIME_BASE)) |
1715 | 2437 | start_time = start_time_text; | |
1716 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10652 times.
|
10652 | else if (start_time > start_time_text) |
1717 | ✗ | av_log(ic, AV_LOG_VERBOSE, "Ignoring outlier non primary stream starttime %f\n", start_time_text / (float)AV_TIME_BASE); | |
1718 | |||
1719 |
5/6✓ Branch 0 taken 9678 times.
✓ Branch 1 taken 3411 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 9676 times.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
|
13089 | if (end_time == INT64_MIN || (end_time < end_time_text && end_time_text - (uint64_t)end_time < AV_TIME_BASE)) |
1720 | 3413 | end_time = end_time_text; | |
1721 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9676 times.
|
9676 | else if (end_time < end_time_text) |
1722 | ✗ | av_log(ic, AV_LOG_VERBOSE, "Ignoring outlier non primary stream endtime %f\n", end_time_text / (float)AV_TIME_BASE); | |
1723 | |||
1724 |
5/6✓ Branch 0 taken 11097 times.
✓ Branch 1 taken 1992 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 11091 times.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
|
13089 | if (duration == INT64_MIN || (duration < duration_text && (uint64_t)duration_text - duration < AV_TIME_BASE)) |
1725 | 1998 | duration = duration_text; | |
1726 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 11091 times.
|
11091 | else if (duration < duration_text) |
1727 | ✗ | av_log(ic, AV_LOG_VERBOSE, "Ignoring outlier non primary stream duration %f\n", duration_text / (float)AV_TIME_BASE); | |
1728 | |||
1729 |
2/2✓ Branch 0 taken 10673 times.
✓ Branch 1 taken 2416 times.
|
13089 | if (start_time != INT64_MAX) { |
1730 | 10673 | ic->start_time = start_time; | |
1731 |
2/2✓ Branch 0 taken 9695 times.
✓ Branch 1 taken 978 times.
|
10673 | if (end_time != INT64_MIN) { |
1732 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9695 times.
|
9695 | if (ic->nb_programs > 1) { |
1733 | ✗ | for (unsigned i = 0; i < ic->nb_programs; i++) { | |
1734 | ✗ | AVProgram *const p = ic->programs[i]; | |
1735 | |||
1736 | ✗ | if (p->start_time != AV_NOPTS_VALUE && | |
1737 | ✗ | p->end_time > p->start_time && | |
1738 | ✗ | p->end_time - (uint64_t)p->start_time <= INT64_MAX) | |
1739 | ✗ | duration = FFMAX(duration, p->end_time - p->start_time); | |
1740 | } | ||
1741 |
2/4✓ Branch 0 taken 9695 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9695 times.
✗ Branch 3 not taken.
|
9695 | } else if (end_time >= start_time && end_time - (uint64_t)start_time <= INT64_MAX) { |
1742 | 9695 | duration = FFMAX(duration, end_time - start_time); | |
1743 | } | ||
1744 | } | ||
1745 | } | ||
1746 |
6/6✓ Branch 0 taken 11127 times.
✓ Branch 1 taken 1962 times.
✓ Branch 2 taken 11091 times.
✓ Branch 3 taken 36 times.
✓ Branch 4 taken 5960 times.
✓ Branch 5 taken 5131 times.
|
13089 | if (duration != INT64_MIN && duration > 0 && ic->duration == AV_NOPTS_VALUE) { |
1747 | 5960 | ic->duration = duration; | |
1748 | } | ||
1749 |
5/6✓ Branch 0 taken 7060 times.
✓ Branch 1 taken 6029 times.
✓ Branch 3 taken 7060 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 5948 times.
✓ Branch 6 taken 1112 times.
|
13089 | if (ic->pb && (filesize = avio_size(ic->pb)) > 0 && ic->duration > 0) { |
1750 | /* compute the bitrate */ | ||
1751 | 5948 | double bitrate = (double) filesize * 8.0 * AV_TIME_BASE / | |
1752 | 5948 | (double) ic->duration; | |
1753 |
2/4✓ Branch 0 taken 5948 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5948 times.
✗ Branch 3 not taken.
|
5948 | if (bitrate >= 0 && bitrate <= INT64_MAX) |
1754 | 5948 | ic->bit_rate = bitrate; | |
1755 | } | ||
1756 | 13089 | } | |
1757 | |||
1758 | 5555 | static void fill_all_stream_timings(AVFormatContext *ic) | |
1759 | { | ||
1760 | 5555 | update_stream_timings(ic); | |
1761 |
2/2✓ Branch 0 taken 6218 times.
✓ Branch 1 taken 5555 times.
|
11773 | for (unsigned i = 0; i < ic->nb_streams; i++) { |
1762 | 6218 | AVStream *const st = ic->streams[i]; | |
1763 | |||
1764 |
2/2✓ Branch 0 taken 772 times.
✓ Branch 1 taken 5446 times.
|
6218 | if (st->start_time == AV_NOPTS_VALUE) { |
1765 |
2/2✓ Branch 0 taken 129 times.
✓ Branch 1 taken 643 times.
|
772 | if (ic->start_time != AV_NOPTS_VALUE) |
1766 | 129 | st->start_time = av_rescale_q(ic->start_time, AV_TIME_BASE_Q, | |
1767 | st->time_base); | ||
1768 |
2/2✓ Branch 0 taken 757 times.
✓ Branch 1 taken 15 times.
|
772 | if (ic->duration != AV_NOPTS_VALUE) |
1769 | 757 | st->duration = av_rescale_q(ic->duration, AV_TIME_BASE_Q, | |
1770 | st->time_base); | ||
1771 | } | ||
1772 | } | ||
1773 | 5555 | } | |
1774 | |||
1775 | 1979 | static void estimate_timings_from_bit_rate(AVFormatContext *ic) | |
1776 | { | ||
1777 | 1979 | FFFormatContext *const si = ffformatcontext(ic); | |
1778 | 1979 | int show_warning = 0; | |
1779 | |||
1780 | /* if bit_rate is already set, we believe it */ | ||
1781 |
2/2✓ Branch 0 taken 1944 times.
✓ Branch 1 taken 35 times.
|
1979 | if (ic->bit_rate <= 0) { |
1782 | 1944 | int64_t bit_rate = 0; | |
1783 |
2/2✓ Branch 0 taken 2068 times.
✓ Branch 1 taken 1297 times.
|
3365 | for (unsigned i = 0; i < ic->nb_streams; i++) { |
1784 | 2068 | const AVStream *const st = ic->streams[i]; | |
1785 | 2068 | const FFStream *const sti = cffstream(st); | |
1786 |
4/4✓ Branch 0 taken 1295 times.
✓ Branch 1 taken 773 times.
✓ Branch 2 taken 110 times.
✓ Branch 3 taken 1185 times.
|
2068 | if (st->codecpar->bit_rate <= 0 && sti->avctx->bit_rate > 0) |
1787 | 110 | st->codecpar->bit_rate = sti->avctx->bit_rate; | |
1788 |
2/2✓ Branch 0 taken 883 times.
✓ Branch 1 taken 1185 times.
|
2068 | if (st->codecpar->bit_rate > 0) { |
1789 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 883 times.
|
883 | if (INT64_MAX - st->codecpar->bit_rate < bit_rate) { |
1790 | ✗ | bit_rate = 0; | |
1791 | ✗ | break; | |
1792 | } | ||
1793 | 883 | bit_rate += st->codecpar->bit_rate; | |
1794 |
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) { |
1795 | // If we have a videostream with packets but without a bitrate | ||
1796 | // then consider the sum not known | ||
1797 | 647 | bit_rate = 0; | |
1798 | 647 | break; | |
1799 | } | ||
1800 | } | ||
1801 | 1944 | ic->bit_rate = bit_rate; | |
1802 | } | ||
1803 | |||
1804 | /* if duration is already set, we believe it */ | ||
1805 |
1/2✓ Branch 0 taken 1979 times.
✗ Branch 1 not taken.
|
1979 | if (ic->duration == AV_NOPTS_VALUE && |
1806 |
2/2✓ Branch 0 taken 890 times.
✓ Branch 1 taken 1089 times.
|
1979 | ic->bit_rate != 0) { |
1807 |
2/2✓ Branch 0 taken 866 times.
✓ Branch 1 taken 24 times.
|
890 | int64_t filesize = ic->pb ? avio_size(ic->pb) : 0; |
1808 |
2/2✓ Branch 0 taken 860 times.
✓ Branch 1 taken 30 times.
|
890 | if (filesize > si->data_offset) { |
1809 | 860 | filesize -= si->data_offset; | |
1810 |
2/2✓ Branch 0 taken 897 times.
✓ Branch 1 taken 860 times.
|
1757 | for (unsigned i = 0; i < ic->nb_streams; i++) { |
1811 | 897 | AVStream *const st = ic->streams[i]; | |
1812 | |||
1813 |
1/2✓ Branch 0 taken 897 times.
✗ Branch 1 not taken.
|
897 | if ( st->time_base.num <= INT64_MAX / ic->bit_rate |
1814 |
1/2✓ Branch 0 taken 897 times.
✗ Branch 1 not taken.
|
897 | && st->duration == AV_NOPTS_VALUE) { |
1815 | 897 | st->duration = av_rescale(filesize, 8LL * st->time_base.den, | |
1816 | 897 | ic->bit_rate * | |
1817 | 897 | (int64_t) st->time_base.num); | |
1818 | 897 | show_warning = 1; | |
1819 | } | ||
1820 | } | ||
1821 | } | ||
1822 | } | ||
1823 |
2/2✓ Branch 0 taken 860 times.
✓ Branch 1 taken 1119 times.
|
1979 | if (show_warning) |
1824 | 860 | av_log(ic, AV_LOG_WARNING, | |
1825 | "Estimating duration from bitrate, this may be inaccurate\n"); | ||
1826 | 1979 | } | |
1827 | |||
1828 | #define DURATION_DEFAULT_MAX_READ_SIZE 250000LL | ||
1829 | #define DURATION_DEFAULT_MAX_RETRY 6 | ||
1830 | #define DURATION_MAX_RETRY 1 | ||
1831 | |||
1832 | /* only usable for MPEG-PS streams */ | ||
1833 | 69 | static void estimate_timings_from_pts(AVFormatContext *ic, int64_t old_offset) | |
1834 | { | ||
1835 | 69 | FFFormatContext *const si = ffformatcontext(ic); | |
1836 | 69 | AVPacket *const pkt = si->pkt; | |
1837 | int num, den, read_size, ret; | ||
1838 |
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; |
1839 |
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; |
1840 | 69 | int found_duration = 0; | |
1841 | int is_end; | ||
1842 | int64_t filesize, offset, duration; | ||
1843 | 69 | int retry = 0; | |
1844 | |||
1845 | /* flush packet queue */ | ||
1846 | 69 | ff_flush_packet_queue(ic); | |
1847 | |||
1848 |
2/2✓ Branch 0 taken 143 times.
✓ Branch 1 taken 69 times.
|
212 | for (unsigned i = 0; i < ic->nb_streams; i++) { |
1849 | 143 | AVStream *const st = ic->streams[i]; | |
1850 | 143 | FFStream *const sti = ffstream(st); | |
1851 | |||
1852 |
2/2✓ Branch 0 taken 22 times.
✓ Branch 1 taken 121 times.
|
143 | if (st->start_time == AV_NOPTS_VALUE && |
1853 |
1/2✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
|
22 | sti->first_dts == AV_NOPTS_VALUE && |
1854 |
1/2✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
|
22 | st->codecpar->codec_type != AVMEDIA_TYPE_UNKNOWN) |
1855 | 22 | av_log(ic, AV_LOG_WARNING, | |
1856 | "start time for stream %d is not set in estimate_timings_from_pts\n", i); | ||
1857 | |||
1858 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 119 times.
|
143 | if (sti->parser) { |
1859 | 24 | av_parser_close(sti->parser); | |
1860 | 24 | sti->parser = NULL; | |
1861 | } | ||
1862 | } | ||
1863 | |||
1864 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 69 times.
|
69 | if (ic->skip_estimate_duration_from_pts) { |
1865 | ✗ | av_log(ic, AV_LOG_INFO, "Skipping duration calculation in estimate_timings_from_pts\n"); | |
1866 | ✗ | goto skip_duration_calc; | |
1867 | } | ||
1868 | |||
1869 | 69 | av_opt_set_int(ic, "skip_changes", 1, AV_OPT_SEARCH_CHILDREN); | |
1870 | /* estimate the end time (duration) */ | ||
1871 | /* XXX: may need to support wrapping */ | ||
1872 |
1/2✓ Branch 0 taken 69 times.
✗ Branch 1 not taken.
|
69 | filesize = ic->pb ? avio_size(ic->pb) : 0; |
1873 | do { | ||
1874 | 76 | is_end = found_duration; | |
1875 | 76 | offset = filesize - (duration_max_read_size << retry); | |
1876 |
2/2✓ Branch 0 taken 26 times.
✓ Branch 1 taken 50 times.
|
76 | if (offset < 0) |
1877 | 26 | offset = 0; | |
1878 | |||
1879 | 76 | avio_seek(ic->pb, offset, SEEK_SET); | |
1880 | 76 | read_size = 0; | |
1881 | 6554 | for (;;) { | |
1882 | AVStream *st; | ||
1883 | FFStream *sti; | ||
1884 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 6623 times.
|
6630 | if (read_size >= duration_max_read_size << (FFMAX(retry - 1, 0))) |
1885 | 7 | break; | |
1886 | |||
1887 | do { | ||
1888 | 6623 | ret = ff_read_packet(ic, pkt); | |
1889 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6623 times.
|
6623 | } while (ret == AVERROR(EAGAIN)); |
1890 |
2/2✓ Branch 0 taken 69 times.
✓ Branch 1 taken 6554 times.
|
6623 | if (ret != 0) |
1891 | 69 | break; | |
1892 | 6554 | read_size += pkt->size; | |
1893 | 6554 | st = ic->streams[pkt->stream_index]; | |
1894 | 6554 | sti = ffstream(st); | |
1895 |
2/2✓ Branch 0 taken 2805 times.
✓ Branch 1 taken 3749 times.
|
6554 | if (pkt->pts != AV_NOPTS_VALUE && |
1896 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2805 times.
|
2805 | (st->start_time != AV_NOPTS_VALUE || |
1897 | ✗ | sti->first_dts != AV_NOPTS_VALUE)) { | |
1898 |
1/2✓ Branch 0 taken 2805 times.
✗ Branch 1 not taken.
|
2805 | if (pkt->duration == 0) { |
1899 | 2805 | compute_frame_duration(ic, &num, &den, st, sti->parser, pkt); | |
1900 |
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) { |
1901 | 2345 | pkt->duration = av_rescale_rnd(1, | |
1902 | 2345 | num * (int64_t) st->time_base.den, | |
1903 | 2345 | den * (int64_t) st->time_base.num, | |
1904 | AV_ROUND_DOWN); | ||
1905 | } | ||
1906 | } | ||
1907 | 2805 | duration = pkt->pts + pkt->duration; | |
1908 | 2805 | found_duration = 1; | |
1909 |
1/2✓ Branch 0 taken 2805 times.
✗ Branch 1 not taken.
|
2805 | if (st->start_time != AV_NOPTS_VALUE) |
1910 | 2805 | duration -= st->start_time; | |
1911 | else | ||
1912 | ✗ | duration -= sti->first_dts; | |
1913 |
2/2✓ Branch 0 taken 2796 times.
✓ Branch 1 taken 9 times.
|
2805 | if (duration > 0) { |
1914 |
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 || |
1915 |
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)) |
1916 | 2542 | st->duration = duration; | |
1917 | 2796 | sti->info->last_duration = duration; | |
1918 | } | ||
1919 | } | ||
1920 | 6554 | av_packet_unref(pkt); | |
1921 | } | ||
1922 | |||
1923 | /* check if all audio/video streams have valid duration */ | ||
1924 |
2/2✓ Branch 0 taken 69 times.
✓ Branch 1 taken 7 times.
|
76 | if (!is_end) { |
1925 | 69 | is_end = 1; | |
1926 |
2/2✓ Branch 0 taken 143 times.
✓ Branch 1 taken 69 times.
|
212 | for (unsigned i = 0; i < ic->nb_streams; i++) { |
1927 | 143 | const AVStream *const st = ic->streams[i]; | |
1928 |
2/2✓ Branch 0 taken 125 times.
✓ Branch 1 taken 18 times.
|
143 | switch (st->codecpar->codec_type) { |
1929 | 125 | case AVMEDIA_TYPE_VIDEO: | |
1930 | case AVMEDIA_TYPE_AUDIO: | ||
1931 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 101 times.
|
125 | if (st->duration == AV_NOPTS_VALUE) |
1932 | 24 | is_end = 0; | |
1933 | } | ||
1934 | } | ||
1935 | } | ||
1936 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 2 times.
|
9 | } while (!is_end && |
1937 |
3/4✓ Branch 0 taken 9 times.
✓ Branch 1 taken 67 times.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
|
83 | offset && |
1938 | ++retry <= duration_max_retry); | ||
1939 | |||
1940 | 69 | av_opt_set_int(ic, "skip_changes", 0, AV_OPT_SEARCH_CHILDREN); | |
1941 | |||
1942 | /* warn about audio/video streams which duration could not be estimated */ | ||
1943 |
2/2✓ Branch 0 taken 143 times.
✓ Branch 1 taken 69 times.
|
212 | for (unsigned i = 0; i < ic->nb_streams; i++) { |
1944 | 143 | const AVStream *const st = ic->streams[i]; | |
1945 | 143 | const FFStream *const sti = cffstream(st); | |
1946 | |||
1947 |
2/2✓ Branch 0 taken 32 times.
✓ Branch 1 taken 111 times.
|
143 | if (st->duration == AV_NOPTS_VALUE) { |
1948 |
2/2✓ Branch 0 taken 17 times.
✓ Branch 1 taken 15 times.
|
32 | switch (st->codecpar->codec_type) { |
1949 | 17 | case AVMEDIA_TYPE_VIDEO: | |
1950 | case AVMEDIA_TYPE_AUDIO: | ||
1951 |
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) { |
1952 | 7 | av_log(ic, AV_LOG_WARNING, "stream %d : no PTS found at end of file, duration not set\n", i); | |
1953 | } else | ||
1954 | 10 | av_log(ic, AV_LOG_WARNING, "stream %d : no TS found at start of file, duration not set\n", i); | |
1955 | } | ||
1956 | } | ||
1957 | } | ||
1958 | 69 | skip_duration_calc: | |
1959 | 69 | fill_all_stream_timings(ic); | |
1960 | |||
1961 | 69 | avio_seek(ic->pb, old_offset, SEEK_SET); | |
1962 |
2/2✓ Branch 0 taken 143 times.
✓ Branch 1 taken 69 times.
|
212 | for (unsigned i = 0; i < ic->nb_streams; i++) { |
1963 | 143 | AVStream *const st = ic->streams[i]; | |
1964 | 143 | FFStream *const sti = ffstream(st); | |
1965 | |||
1966 | 143 | sti->cur_dts = sti->first_dts; | |
1967 | 143 | sti->last_IP_pts = AV_NOPTS_VALUE; | |
1968 | 143 | sti->last_dts_for_order_check = AV_NOPTS_VALUE; | |
1969 |
2/2✓ Branch 0 taken 2431 times.
✓ Branch 1 taken 143 times.
|
2574 | for (int j = 0; j < MAX_REORDER_DELAY + 1; j++) |
1970 | 2431 | sti->pts_buffer[j] = AV_NOPTS_VALUE; | |
1971 | } | ||
1972 | 69 | } | |
1973 | |||
1974 | /* 1:1 map to AVDurationEstimationMethod */ | ||
1975 | static const char *const duration_name[] = { | ||
1976 | [AVFMT_DURATION_FROM_PTS] = "pts", | ||
1977 | [AVFMT_DURATION_FROM_STREAM] = "stream", | ||
1978 | [AVFMT_DURATION_FROM_BITRATE] = "bit rate", | ||
1979 | }; | ||
1980 | |||
1981 | 7534 | static const char *duration_estimate_name(enum AVDurationEstimationMethod method) | |
1982 | { | ||
1983 | 7534 | return duration_name[method]; | |
1984 | } | ||
1985 | |||
1986 | 7534 | static void estimate_timings(AVFormatContext *ic, int64_t old_offset) | |
1987 | { | ||
1988 | int64_t file_size; | ||
1989 | |||
1990 | /* get the file size, if possible */ | ||
1991 |
2/2✓ Branch 0 taken 3061 times.
✓ Branch 1 taken 4473 times.
|
7534 | if (ic->iformat->flags & AVFMT_NOFILE) { |
1992 | 3061 | file_size = 0; | |
1993 | } else { | ||
1994 | 4473 | file_size = avio_size(ic->pb); | |
1995 | 4473 | file_size = FFMAX(0, file_size); | |
1996 | } | ||
1997 | |||
1998 |
2/2✓ Branch 0 taken 7509 times.
✓ Branch 1 taken 25 times.
|
7534 | if ((!strcmp(ic->iformat->name, "mpeg") || |
1999 |
3/4✓ Branch 0 taken 44 times.
✓ Branch 1 taken 7465 times.
✓ Branch 2 taken 69 times.
✗ Branch 3 not taken.
|
7534 | !strcmp(ic->iformat->name, "mpegts")) && |
2000 |
1/2✓ Branch 0 taken 69 times.
✗ Branch 1 not taken.
|
69 | file_size && (ic->pb->seekable & AVIO_SEEKABLE_NORMAL)) { |
2001 | /* get accurate estimate from the PTSes */ | ||
2002 | 69 | estimate_timings_from_pts(ic, old_offset); | |
2003 | 69 | ic->duration_estimation_method = AVFMT_DURATION_FROM_PTS; | |
2004 |
2/2✓ Branch 1 taken 5486 times.
✓ Branch 2 taken 1979 times.
|
7465 | } else if (has_duration(ic)) { |
2005 | /* at least one component has timings - we use them for all | ||
2006 | * the components */ | ||
2007 | 5486 | fill_all_stream_timings(ic); | |
2008 | /* nut demuxer estimate the duration from PTS */ | ||
2009 |
2/2✓ Branch 0 taken 30 times.
✓ Branch 1 taken 5456 times.
|
5486 | if (!strcmp(ic->iformat->name, "nut")) |
2010 | 30 | ic->duration_estimation_method = AVFMT_DURATION_FROM_PTS; | |
2011 | else | ||
2012 | 5456 | ic->duration_estimation_method = AVFMT_DURATION_FROM_STREAM; | |
2013 | } else { | ||
2014 | /* less precise: use bitrate info */ | ||
2015 | 1979 | estimate_timings_from_bit_rate(ic); | |
2016 | 1979 | ic->duration_estimation_method = AVFMT_DURATION_FROM_BITRATE; | |
2017 | } | ||
2018 | 7534 | update_stream_timings(ic); | |
2019 | |||
2020 |
2/2✓ Branch 0 taken 8343 times.
✓ Branch 1 taken 7534 times.
|
15877 | for (unsigned i = 0; i < ic->nb_streams; i++) { |
2021 | 8343 | AVStream *const st = ic->streams[i]; | |
2022 |
1/2✓ Branch 0 taken 8343 times.
✗ Branch 1 not taken.
|
8343 | if (st->time_base.den) |
2023 | 8343 | av_log(ic, AV_LOG_TRACE, "stream %u: start_time: %s duration: %s\n", i, | |
2024 | 8343 | av_ts2timestr(st->start_time, &st->time_base), | |
2025 | 8343 | av_ts2timestr(st->duration, &st->time_base)); | |
2026 | } | ||
2027 | 7534 | av_log(ic, AV_LOG_TRACE, | |
2028 | "format: start_time: %s duration: %s (estimate from %s) bitrate=%"PRId64" kb/s\n", | ||
2029 | 7534 | av_ts2timestr(ic->start_time, &AV_TIME_BASE_Q), | |
2030 | 7534 | av_ts2timestr(ic->duration, &AV_TIME_BASE_Q), | |
2031 | duration_estimate_name(ic->duration_estimation_method), | ||
2032 | 7534 | (int64_t)ic->bit_rate / 1000); | |
2033 | 7534 | } | |
2034 | |||
2035 | 106268 | static int determinable_frame_size(const AVCodecContext *avctx) | |
2036 | { | ||
2037 |
2/2✓ Branch 0 taken 2187 times.
✓ Branch 1 taken 104081 times.
|
106268 | switch(avctx->codec_id) { |
2038 | 2187 | case AV_CODEC_ID_MP1: | |
2039 | case AV_CODEC_ID_MP2: | ||
2040 | case AV_CODEC_ID_MP3: | ||
2041 | case AV_CODEC_ID_CODEC2: | ||
2042 | 2187 | return 1; | |
2043 | } | ||
2044 | |||
2045 | 104081 | return 0; | |
2046 | } | ||
2047 | |||
2048 | 422216 | static int has_codec_parameters(const AVStream *st, const char **errmsg_ptr) | |
2049 | { | ||
2050 | 422216 | const FFStream *const sti = cffstream(st); | |
2051 | 422216 | const AVCodecContext *const avctx = sti->avctx; | |
2052 | |||
2053 | #define FAIL(errmsg) do { \ | ||
2054 | if (errmsg_ptr) \ | ||
2055 | *errmsg_ptr = errmsg; \ | ||
2056 | return 0; \ | ||
2057 | } while (0) | ||
2058 | |||
2059 |
2/2✓ Branch 0 taken 644 times.
✓ Branch 1 taken 421572 times.
|
422216 | if ( avctx->codec_id == AV_CODEC_ID_NONE |
2060 |
2/2✓ Branch 0 taken 419 times.
✓ Branch 1 taken 225 times.
|
644 | && avctx->codec_type != AVMEDIA_TYPE_DATA) |
2061 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 414 times.
|
419 | FAIL("unknown codec"); |
2062 |
4/5✓ Branch 0 taken 121727 times.
✓ Branch 1 taken 298528 times.
✓ Branch 2 taken 629 times.
✓ Branch 3 taken 913 times.
✗ Branch 4 not taken.
|
421797 | switch (avctx->codec_type) { |
2063 | 121727 | case AVMEDIA_TYPE_AUDIO: | |
2064 |
4/4✓ Branch 0 taken 106268 times.
✓ Branch 1 taken 15459 times.
✓ Branch 3 taken 2187 times.
✓ Branch 4 taken 104081 times.
|
121727 | if (!avctx->frame_size && determinable_frame_size(avctx)) |
2065 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 2175 times.
|
2187 | FAIL("unspecified frame size"); |
2066 |
1/2✓ Branch 0 taken 119540 times.
✗ Branch 1 not taken.
|
119540 | if (sti->info->found_decoder >= 0 && |
2067 |
2/2✓ Branch 0 taken 2433 times.
✓ Branch 1 taken 117107 times.
|
119540 | avctx->sample_fmt == AV_SAMPLE_FMT_NONE) |
2068 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2433 times.
|
2433 | FAIL("unspecified sample format"); |
2069 |
2/2✓ Branch 0 taken 139 times.
✓ Branch 1 taken 116968 times.
|
117107 | if (!avctx->sample_rate) |
2070 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 133 times.
|
139 | FAIL("unspecified sample rate"); |
2071 |
2/2✓ Branch 0 taken 17 times.
✓ Branch 1 taken 116951 times.
|
116968 | if (!avctx->ch_layout.nb_channels) |
2072 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
|
17 | FAIL("unspecified number of channels"); |
2073 |
4/6✓ Branch 0 taken 116951 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 80775 times.
✓ Branch 3 taken 36176 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 80775 times.
|
116951 | if (sti->info->found_decoder >= 0 && !sti->nb_decoded_frames && avctx->codec_id == AV_CODEC_ID_DTS) |
2074 | ✗ | FAIL("no decodable DTS frames"); | |
2075 | 116951 | break; | |
2076 | 298528 | case AVMEDIA_TYPE_VIDEO: | |
2077 |
2/2✓ Branch 0 taken 12725 times.
✓ Branch 1 taken 285803 times.
|
298528 | if (!avctx->width) |
2078 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 12715 times.
|
12725 | FAIL("unspecified size"); |
2079 |
4/4✓ Branch 0 taken 285748 times.
✓ Branch 1 taken 55 times.
✓ Branch 2 taken 4220 times.
✓ Branch 3 taken 281528 times.
|
285803 | if (sti->info->found_decoder >= 0 && avctx->pix_fmt == AV_PIX_FMT_NONE) |
2080 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 4217 times.
|
4220 | FAIL("unspecified pixel format"); |
2081 |
4/4✓ Branch 0 taken 281483 times.
✓ Branch 1 taken 100 times.
✓ Branch 2 taken 86 times.
✓ Branch 3 taken 281397 times.
|
281583 | if (st->codecpar->codec_id == AV_CODEC_ID_RV30 || st->codecpar->codec_id == AV_CODEC_ID_RV40) |
2082 |
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) |
2083 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 67 times.
|
67 | FAIL("no frame in rv30/40 and no sar"); |
2084 | 281516 | break; | |
2085 | 629 | case AVMEDIA_TYPE_SUBTITLE: | |
2086 |
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) |
2087 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
|
21 | FAIL("unspecified size"); |
2088 | 608 | break; | |
2089 | 913 | case AVMEDIA_TYPE_DATA: | |
2090 |
2/2✓ Branch 0 taken 225 times.
✓ Branch 1 taken 688 times.
|
913 | if (avctx->codec_id == AV_CODEC_ID_NONE) return 1; |
2091 | } | ||
2092 | |||
2093 | 399763 | return 1; | |
2094 | } | ||
2095 | |||
2096 | /* returns 1 or 0 if or if not decoded data was returned, or a negative error */ | ||
2097 | 197331 | static int try_decode_frame(AVFormatContext *s, AVStream *st, | |
2098 | const AVPacket *pkt, AVDictionary **options) | ||
2099 | { | ||
2100 | 197331 | FFStream *const sti = ffstream(st); | |
2101 | 197331 | AVCodecContext *const avctx = sti->avctx; | |
2102 | const AVCodec *codec; | ||
2103 | 197331 | int got_picture = 1, ret = 0; | |
2104 | 197331 | AVFrame *frame = av_frame_alloc(); | |
2105 | AVSubtitle subtitle; | ||
2106 | 197331 | int do_skip_frame = 0; | |
2107 | enum AVDiscard skip_frame; | ||
2108 | 197331 | int pkt_to_send = pkt->size > 0; | |
2109 | |||
2110 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 197331 times.
|
197331 | if (!frame) |
2111 | ✗ | return AVERROR(ENOMEM); | |
2112 | |||
2113 |
2/2✓ Branch 1 taken 3392 times.
✓ Branch 2 taken 193939 times.
|
197331 | if (!avcodec_is_open(avctx) && |
2114 |
1/2✓ Branch 0 taken 3392 times.
✗ Branch 1 not taken.
|
3392 | sti->info->found_decoder <= 0 && |
2115 |
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)) { |
2116 | 1431 | AVDictionary *thread_opt = NULL; | |
2117 | |||
2118 | 1431 | codec = find_probe_decoder(s, st, st->codecpar->codec_id); | |
2119 | |||
2120 |
2/2✓ Branch 0 taken 76 times.
✓ Branch 1 taken 1355 times.
|
1431 | if (!codec) { |
2121 | 76 | sti->info->found_decoder = -st->codecpar->codec_id; | |
2122 | 76 | ret = -1; | |
2123 | 88 | goto fail; | |
2124 | } | ||
2125 | |||
2126 | /* Force thread count to 1 since the H.264 decoder will not extract | ||
2127 | * SPS and PPS to extradata during multi-threaded decoding. */ | ||
2128 |
2/2✓ Branch 0 taken 161 times.
✓ Branch 1 taken 1194 times.
|
1355 | av_dict_set(options ? options : &thread_opt, "threads", "1", 0); |
2129 | /* Force lowres to 0. The decoder might reduce the video size by the | ||
2130 | * lowres factor, and we don't want that propagated to the stream's | ||
2131 | * codecpar */ | ||
2132 |
2/2✓ Branch 0 taken 161 times.
✓ Branch 1 taken 1194 times.
|
1355 | av_dict_set(options ? options : &thread_opt, "lowres", "0", 0); |
2133 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1355 times.
|
1355 | if (s->codec_whitelist) |
2134 | ✗ | av_dict_set(options ? options : &thread_opt, "codec_whitelist", s->codec_whitelist, 0); | |
2135 |
2/2✓ Branch 0 taken 161 times.
✓ Branch 1 taken 1194 times.
|
1355 | ret = avcodec_open2(avctx, codec, options ? options : &thread_opt); |
2136 |
2/2✓ Branch 0 taken 161 times.
✓ Branch 1 taken 1194 times.
|
1355 | if (!options) |
2137 | 161 | av_dict_free(&thread_opt); | |
2138 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 1343 times.
|
1355 | if (ret < 0) { |
2139 | 12 | sti->info->found_decoder = -avctx->codec_id; | |
2140 | 12 | goto fail; | |
2141 | } | ||
2142 | 1343 | sti->info->found_decoder = 1; | |
2143 |
2/2✓ Branch 0 taken 6710 times.
✓ Branch 1 taken 189190 times.
|
195900 | } else if (!sti->info->found_decoder) |
2144 | 6710 | sti->info->found_decoder = 1; | |
2145 | |||
2146 |
2/2✓ Branch 0 taken 1961 times.
✓ Branch 1 taken 195282 times.
|
197243 | if (sti->info->found_decoder < 0) { |
2147 | 1961 | ret = -1; | |
2148 | 1961 | goto fail; | |
2149 | } | ||
2150 | |||
2151 |
2/2✓ Branch 1 taken 107473 times.
✓ Branch 2 taken 87809 times.
|
195282 | if (avpriv_codec_get_cap_skip_frame_fill_param(avctx->codec)) { |
2152 | 107473 | do_skip_frame = 1; | |
2153 | 107473 | skip_frame = avctx->skip_frame; | |
2154 | 107473 | avctx->skip_frame = AVDISCARD_ALL; | |
2155 | } | ||
2156 | |||
2157 |
5/6✓ Branch 0 taken 7961 times.
✓ Branch 1 taken 4548 times.
✓ Branch 2 taken 26 times.
✓ Branch 3 taken 4522 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 195290 times.
|
398559 | while ((pkt_to_send || (!pkt->data && got_picture)) && |
2158 |
4/4✓ Branch 0 taken 12509 times.
✓ Branch 1 taken 190768 times.
✓ Branch 2 taken 5537 times.
✓ Branch 3 taken 189753 times.
|
398567 | ret >= 0 && |
2159 |
2/2✓ Branch 2 taken 2241 times.
✓ Branch 3 taken 187512 times.
|
385043 | (!has_codec_parameters(st, NULL) || !has_decode_delay_been_guessed(st) || |
2160 |
2/2✓ Branch 0 taken 184709 times.
✓ Branch 1 taken 2803 times.
|
187512 | (!sti->codec_info_nb_frames && |
2161 |
2/2✓ Branch 0 taken 457 times.
✓ Branch 1 taken 2346 times.
|
2803 | (avctx->codec->capabilities & AV_CODEC_CAP_CHANNEL_CONF)))) { |
2162 | 8235 | got_picture = 0; | |
2163 |
2/2✓ Branch 0 taken 731 times.
✓ Branch 1 taken 7504 times.
|
8235 | if (avctx->codec_type == AVMEDIA_TYPE_VIDEO || |
2164 |
2/2✓ Branch 0 taken 724 times.
✓ Branch 1 taken 7 times.
|
731 | avctx->codec_type == AVMEDIA_TYPE_AUDIO) { |
2165 | 8228 | ret = avcodec_send_packet(avctx, pkt); | |
2166 |
5/6✓ Branch 0 taken 249 times.
✓ Branch 1 taken 7979 times.
✓ Branch 2 taken 249 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 240 times.
✓ Branch 5 taken 9 times.
|
8228 | if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF) |
2167 | 240 | break; | |
2168 |
2/2✓ Branch 0 taken 7979 times.
✓ Branch 1 taken 9 times.
|
7988 | if (ret >= 0) |
2169 | 7979 | pkt_to_send = 0; | |
2170 | 7988 | ret = avcodec_receive_frame(avctx, frame); | |
2171 |
2/2✓ Branch 0 taken 3197 times.
✓ Branch 1 taken 4791 times.
|
7988 | if (ret >= 0) |
2172 | 3197 | got_picture = 1; | |
2173 |
4/4✓ Branch 0 taken 3223 times.
✓ Branch 1 taken 4765 times.
✓ Branch 2 taken 26 times.
✓ Branch 3 taken 3197 times.
|
7988 | if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) |
2174 | 4791 | ret = 0; | |
2175 |
1/2✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
|
7 | } else if (avctx->codec_type == AVMEDIA_TYPE_SUBTITLE) { |
2176 | 7 | ret = avcodec_decode_subtitle2(avctx, &subtitle, | |
2177 | &got_picture, pkt); | ||
2178 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 5 times.
|
7 | if (got_picture) |
2179 | 2 | avsubtitle_free(&subtitle); | |
2180 |
1/2✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
|
7 | if (ret >= 0) |
2181 | 7 | pkt_to_send = 0; | |
2182 | } | ||
2183 |
1/2✓ Branch 0 taken 7995 times.
✗ Branch 1 not taken.
|
7995 | if (ret >= 0) { |
2184 |
2/2✓ Branch 0 taken 3199 times.
✓ Branch 1 taken 4796 times.
|
7995 | if (got_picture) |
2185 | 3199 | sti->nb_decoded_frames++; | |
2186 | 7995 | ret = got_picture; | |
2187 | } | ||
2188 | } | ||
2189 | |||
2190 | 195042 | fail: | |
2191 |
2/2✓ Branch 0 taken 107473 times.
✓ Branch 1 taken 89858 times.
|
197331 | if (do_skip_frame) { |
2192 | 107473 | avctx->skip_frame = skip_frame; | |
2193 | } | ||
2194 | |||
2195 | 197331 | av_frame_free(&frame); | |
2196 | 197331 | return ret; | |
2197 | } | ||
2198 | |||
2199 | 57 | static int chapter_start_cmp(const void *p1, const void *p2) | |
2200 | { | ||
2201 | 57 | const AVChapter *const ch1 = *(AVChapter**)p1; | |
2202 | 57 | const AVChapter *const ch2 = *(AVChapter**)p2; | |
2203 | 57 | int delta = av_compare_ts(ch1->start, ch1->time_base, ch2->start, ch2->time_base); | |
2204 |
1/2✓ Branch 0 taken 57 times.
✗ Branch 1 not taken.
|
57 | if (delta) |
2205 | 57 | return delta; | |
2206 | ✗ | return FFDIFFSIGN(ch1->id, ch2->id); | |
2207 | } | ||
2208 | |||
2209 | 7534 | static int compute_chapters_end(AVFormatContext *s) | |
2210 | { | ||
2211 | 7534 | int64_t max_time = 0; | |
2212 | AVChapter **timetable; | ||
2213 | |||
2214 |
2/2✓ Branch 0 taken 7508 times.
✓ Branch 1 taken 26 times.
|
7534 | if (!s->nb_chapters) |
2215 | 7508 | return 0; | |
2216 | |||
2217 |
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) |
2218 | 26 | max_time = s->duration + | |
2219 |
2/2✓ Branch 0 taken 23 times.
✓ Branch 1 taken 3 times.
|
26 | ((s->start_time == AV_NOPTS_VALUE) ? 0 : s->start_time); |
2220 | |||
2221 | 26 | timetable = av_memdup(s->chapters, s->nb_chapters * sizeof(*timetable)); | |
2222 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
|
26 | if (!timetable) |
2223 | ✗ | return AVERROR(ENOMEM); | |
2224 | 26 | qsort(timetable, s->nb_chapters, sizeof(*timetable), chapter_start_cmp); | |
2225 | |||
2226 |
2/2✓ Branch 0 taken 71 times.
✓ Branch 1 taken 26 times.
|
97 | for (unsigned i = 0; i < s->nb_chapters; i++) |
2227 |
2/2✓ Branch 0 taken 40 times.
✓ Branch 1 taken 31 times.
|
71 | if (timetable[i]->end == AV_NOPTS_VALUE) { |
2228 | 40 | AVChapter *const ch = timetable[i]; | |
2229 | 40 | int64_t end = max_time ? av_rescale_q(max_time, AV_TIME_BASE_Q, | |
2230 | ch->time_base) | ||
2231 |
1/2✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
|
40 | : INT64_MAX; |
2232 | |||
2233 |
2/2✓ Branch 0 taken 21 times.
✓ Branch 1 taken 19 times.
|
40 | if (i + 1 < s->nb_chapters) { |
2234 | 21 | const AVChapter *const ch1 = timetable[i + 1]; | |
2235 | 21 | int64_t next_start = av_rescale_q(ch1->start, ch1->time_base, | |
2236 | ch->time_base); | ||
2237 |
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) |
2238 | 21 | end = next_start; | |
2239 | } | ||
2240 |
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; |
2241 | } | ||
2242 | 26 | av_free(timetable); | |
2243 | 26 | return 0; | |
2244 | } | ||
2245 | |||
2246 | 18696185 | static int get_std_framerate(int i) | |
2247 | { | ||
2248 |
2/2✓ Branch 0 taken 16985743 times.
✓ Branch 1 taken 1710442 times.
|
18696185 | if (i < 30*12) |
2249 | 16985743 | return (i + 1) * 1001; | |
2250 | 1710442 | i -= 30*12; | |
2251 | |||
2252 |
2/2✓ Branch 0 taken 1300740 times.
✓ Branch 1 taken 409702 times.
|
1710442 | if (i < 30) |
2253 | 1300740 | return (i + 31) * 1001 * 12; | |
2254 | 409702 | i -= 30; | |
2255 | |||
2256 |
2/2✓ Branch 0 taken 130593 times.
✓ Branch 1 taken 279109 times.
|
409702 | if (i < 3) |
2257 | 130593 | return ((const int[]) { 80, 120, 240})[i] * 1001 * 12; | |
2258 | |||
2259 | 279109 | i -= 3; | |
2260 | |||
2261 | 279109 | return ((const int[]) { 24, 30, 60, 12, 15, 48 })[i] * 1000 * 12; | |
2262 | } | ||
2263 | |||
2264 | /* Is the time base unreliable? | ||
2265 | * This is a heuristic to balance between quick acceptance of the values in | ||
2266 | * the headers vs. some extra checks. | ||
2267 | * Old DivX and Xvid often have nonsense timebases like 1fps or 2fps. | ||
2268 | * MPEG-2 commonly misuses field repeat flags to store different framerates. | ||
2269 | * And there are "variable" fps files this needs to detect as well. */ | ||
2270 | 206635 | static int tb_unreliable(AVFormatContext *ic, AVStream *st) | |
2271 | { | ||
2272 | 206635 | FFStream *const sti = ffstream(st); | |
2273 | 206635 | const AVCodecDescriptor *desc = sti->codec_desc; | |
2274 | 206635 | AVCodecContext *c = sti->avctx; | |
2275 |
4/4✓ Branch 0 taken 206184 times.
✓ Branch 1 taken 451 times.
✓ Branch 2 taken 21029 times.
✓ Branch 3 taken 185155 times.
|
206635 | AVRational mul = (AVRational){ desc && (desc->props & AV_CODEC_PROP_FIELDS) ? 2 : 1, 1 }; |
2276 | 206635 | AVRational time_base = c->framerate.num ? av_inv_q(av_mul_q(c->framerate, mul)) | |
2277 | /* NOHEADER check added to not break existing behavior */ | ||
2278 |
2/2✓ Branch 0 taken 17675 times.
✓ Branch 1 taken 188960 times.
|
206635 | : (((ic->ctx_flags & AVFMTCTX_NOHEADER) || |
2279 |
2/2✓ Branch 0 taken 44450 times.
✓ Branch 1 taken 30355 times.
|
74805 | st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) ? (AVRational){0, 1} |
2280 |
2/2✓ Branch 0 taken 74805 times.
✓ Branch 1 taken 114155 times.
|
188960 | : st->time_base); |
2281 | |||
2282 |
2/2✓ Branch 0 taken 23953 times.
✓ Branch 1 taken 182682 times.
|
206635 | if (time_base.den >= 101LL * time_base.num || |
2283 |
2/2✓ Branch 0 taken 23545 times.
✓ Branch 1 taken 408 times.
|
23953 | time_base.den < 5LL * time_base.num || |
2284 | // c->codec_tag == AV_RL32("DIVX") || | ||
2285 | // c->codec_tag == AV_RL32("XVID") || | ||
2286 |
2/2✓ Branch 0 taken 23446 times.
✓ Branch 1 taken 99 times.
|
23545 | c->codec_tag == AV_RL32("mp4v") || |
2287 |
2/2✓ Branch 0 taken 14617 times.
✓ Branch 1 taken 8829 times.
|
23446 | c->codec_id == AV_CODEC_ID_MPEG2VIDEO || |
2288 |
2/2✓ Branch 0 taken 14087 times.
✓ Branch 1 taken 530 times.
|
14617 | c->codec_id == AV_CODEC_ID_GIF || |
2289 |
2/2✓ Branch 0 taken 13382 times.
✓ Branch 1 taken 705 times.
|
14087 | c->codec_id == AV_CODEC_ID_HEVC || |
2290 |
2/2✓ Branch 0 taken 3483 times.
✓ Branch 1 taken 9899 times.
|
13382 | c->codec_id == AV_CODEC_ID_H264) |
2291 | 196736 | return 1; | |
2292 | 9899 | return 0; | |
2293 | } | ||
2294 | |||
2295 | 141357 | int ff_rfps_add_frame(AVFormatContext *ic, AVStream *st, int64_t ts) | |
2296 | { | ||
2297 | 141357 | FFStream *const sti = ffstream(st); | |
2298 | 141357 | FFStreamInfo *info = sti->info; | |
2299 | 141357 | int64_t last = info->last_dts; | |
2300 | |||
2301 |
6/6✓ Branch 0 taken 125263 times.
✓ Branch 1 taken 16094 times.
✓ Branch 2 taken 119296 times.
✓ Branch 3 taken 5967 times.
✓ Branch 4 taken 119268 times.
✓ Branch 5 taken 28 times.
|
141357 | if ( ts != AV_NOPTS_VALUE && last != AV_NOPTS_VALUE && ts > last |
2302 |
1/2✓ Branch 0 taken 119268 times.
✗ Branch 1 not taken.
|
119268 | && ts - (uint64_t)last < INT64_MAX) { |
2303 |
2/2✓ Branch 1 taken 6511 times.
✓ Branch 2 taken 112757 times.
|
119268 | double dts = (is_relative(ts) ? ts - RELATIVE_TS_BASE : ts) * av_q2d(st->time_base); |
2304 | 119268 | int64_t duration = ts - last; | |
2305 | |||
2306 |
2/2✓ Branch 0 taken 3888 times.
✓ Branch 1 taken 115380 times.
|
119268 | if (!info->duration_error) |
2307 | 3888 | info->duration_error = av_mallocz(sizeof(info->duration_error[0])*2); | |
2308 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 119268 times.
|
119268 | if (!info->duration_error) |
2309 | ✗ | return AVERROR(ENOMEM); | |
2310 | |||
2311 | // if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) | ||
2312 | // av_log(NULL, AV_LOG_ERROR, "%f\n", dts); | ||
2313 |
2/2✓ Branch 0 taken 47587932 times.
✓ Branch 1 taken 119268 times.
|
47707200 | for (int i = 0; i < MAX_STD_TIMEBASES; i++) { |
2314 |
2/2✓ Branch 0 taken 18329509 times.
✓ Branch 1 taken 29258423 times.
|
47587932 | if (info->duration_error[0][1][i] < 1e10) { |
2315 | 18329509 | int framerate = get_std_framerate(i); | |
2316 | 18329509 | double sdts = dts*framerate/(1001*12); | |
2317 |
2/2✓ Branch 0 taken 36659018 times.
✓ Branch 1 taken 18329509 times.
|
54988527 | for (int j = 0; j < 2; j++) { |
2318 | 36659018 | int64_t ticks = llrint(sdts+j*0.5); | |
2319 | 36659018 | double error = sdts - ticks + j*0.5; | |
2320 | 36659018 | info->duration_error[j][0][i] += error; | |
2321 | 36659018 | info->duration_error[j][1][i] += error*error; | |
2322 | } | ||
2323 | } | ||
2324 | } | ||
2325 |
1/2✓ Branch 0 taken 119268 times.
✗ Branch 1 not taken.
|
119268 | if (info->rfps_duration_sum <= INT64_MAX - duration) { |
2326 | 119268 | info->duration_count++; | |
2327 | 119268 | info->rfps_duration_sum += duration; | |
2328 | } | ||
2329 | |||
2330 |
2/2✓ Branch 0 taken 10853 times.
✓ Branch 1 taken 108415 times.
|
119268 | if (info->duration_count % 10 == 0) { |
2331 | 10853 | int n = info->duration_count; | |
2332 |
2/2✓ Branch 0 taken 4330347 times.
✓ Branch 1 taken 10853 times.
|
4341200 | for (int i = 0; i < MAX_STD_TIMEBASES; i++) { |
2333 |
2/2✓ Branch 0 taken 1755397 times.
✓ Branch 1 taken 2574950 times.
|
4330347 | if (info->duration_error[0][1][i] < 1e10) { |
2334 | 1755397 | double a0 = info->duration_error[0][0][i] / n; | |
2335 | 1755397 | double error0 = info->duration_error[0][1][i] / n - a0*a0; | |
2336 | 1755397 | double a1 = info->duration_error[1][0][i] / n; | |
2337 | 1755397 | double error1 = info->duration_error[1][1][i] / n - a1*a1; | |
2338 |
4/4✓ Branch 0 taken 1437540 times.
✓ Branch 1 taken 317857 times.
✓ Branch 2 taken 1341676 times.
✓ Branch 3 taken 95864 times.
|
1755397 | if (error0 > 0.04 && error1 > 0.04) { |
2339 | 1341676 | info->duration_error[0][1][i] = 2e10; | |
2340 | 1341676 | info->duration_error[1][1][i] = 2e10; | |
2341 | } | ||
2342 | } | ||
2343 | } | ||
2344 | } | ||
2345 | |||
2346 | // ignore the first 4 values, they might have some random jitter | ||
2347 |
3/4✓ Branch 0 taken 107690 times.
✓ Branch 1 taken 11578 times.
✓ Branch 4 taken 107690 times.
✗ Branch 5 not taken.
|
119268 | if (info->duration_count > 3 && is_relative(ts) == is_relative(last)) |
2348 | 107690 | info->duration_gcd = av_gcd(info->duration_gcd, duration); | |
2349 | } | ||
2350 |
2/2✓ Branch 0 taken 125263 times.
✓ Branch 1 taken 16094 times.
|
141357 | if (ts != AV_NOPTS_VALUE) |
2351 | 125263 | info->last_dts = ts; | |
2352 | |||
2353 | 141357 | return 0; | |
2354 | } | ||
2355 | |||
2356 | 8026 | void ff_rfps_calculate(AVFormatContext *ic) | |
2357 | { | ||
2358 |
2/2✓ Branch 0 taken 9013 times.
✓ Branch 1 taken 8026 times.
|
17039 | for (unsigned i = 0; i < ic->nb_streams; i++) { |
2359 | 9013 | AVStream *const st = ic->streams[i]; | |
2360 | 9013 | FFStream *const sti = ffstream(st); | |
2361 | |||
2362 |
2/2✓ Branch 0 taken 2461 times.
✓ Branch 1 taken 6552 times.
|
9013 | if (st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO) |
2363 | 2461 | continue; | |
2364 | // the check for tb_unreliable() is not completely correct, since this is not about handling | ||
2365 | // an unreliable/inexact time base, but a time base that is finer than necessary, as e.g. | ||
2366 | // ipmovie.c produces. | ||
2367 |
8/8✓ Branch 1 taken 5178 times.
✓ Branch 2 taken 1374 times.
✓ Branch 3 taken 3364 times.
✓ Branch 4 taken 1814 times.
✓ Branch 5 taken 294 times.
✓ Branch 6 taken 3070 times.
✓ Branch 7 taken 144 times.
✓ Branch 8 taken 150 times.
|
6552 | 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 && |
2368 |
1/2✓ Branch 0 taken 144 times.
✗ Branch 1 not taken.
|
144 | sti->info->duration_gcd < INT64_MAX / st->time_base.num) |
2369 | 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); | |
2370 |
4/4✓ Branch 0 taken 3851 times.
✓ Branch 1 taken 2701 times.
✓ Branch 2 taken 407 times.
✓ Branch 3 taken 3444 times.
|
6552 | if (sti->info->duration_count > 1 && !st->r_frame_rate.num |
2371 |
2/2✓ Branch 1 taken 303 times.
✓ Branch 2 taken 104 times.
|
407 | && tb_unreliable(ic, st)) { |
2372 | 303 | int num = 0; | |
2373 | 303 | double best_error = 0.01; | |
2374 |
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); |
2375 | |||
2376 |
2/2✓ Branch 0 taken 120897 times.
✓ Branch 1 taken 303 times.
|
121200 | for (int j = 0; j < MAX_STD_TIMEBASES; j++) { |
2377 |
2/2✓ Branch 0 taken 89775 times.
✓ Branch 1 taken 31122 times.
|
120897 | if (sti->info->codec_info_duration && |
2378 |
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)) |
2379 | 9919 | continue; | |
2380 |
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) |
2381 | 858 | continue; | |
2382 | |||
2383 |
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)) |
2384 | 51873 | continue; | |
2385 | |||
2386 |
2/2✓ Branch 0 taken 116494 times.
✓ Branch 1 taken 58247 times.
|
174741 | for (int k = 0; k < 2; k++) { |
2387 | 116494 | int n = sti->info->duration_count; | |
2388 | 116494 | double a = sti->info->duration_error[k][0][j] / n; | |
2389 | 116494 | double error = sti->info->duration_error[k][1][j]/n - a*a; | |
2390 | |||
2391 |
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) { |
2392 | 4434 | best_error= error; | |
2393 | 4434 | num = get_std_framerate(j); | |
2394 | } | ||
2395 |
2/2✓ Branch 0 taken 23894 times.
✓ Branch 1 taken 92600 times.
|
116494 | if (error < 0.02) |
2396 | 23894 | av_log(ic, AV_LOG_DEBUG, "rfps: %f %f\n", get_std_framerate(j) / 12.0/1001, error); | |
2397 | } | ||
2398 | } | ||
2399 | // do not increase frame rate by more than 1 % in order to match a standard rate. | ||
2400 |
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))) |
2401 | 295 | av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den, num, 12*1001, INT_MAX); | |
2402 | } | ||
2403 |
2/2✓ Branch 0 taken 1089 times.
✓ Branch 1 taken 5463 times.
|
6552 | if ( !st->avg_frame_rate.num |
2404 |
4/4✓ Branch 0 taken 284 times.
✓ Branch 1 taken 805 times.
✓ Branch 2 taken 280 times.
✓ Branch 3 taken 4 times.
|
1089 | && st->r_frame_rate.num && sti->info->rfps_duration_sum |
2405 |
2/2✓ Branch 0 taken 73 times.
✓ Branch 1 taken 207 times.
|
280 | && sti->info->codec_info_duration <= 0 |
2406 |
2/2✓ Branch 0 taken 70 times.
✓ Branch 1 taken 3 times.
|
73 | && sti->info->duration_count > 2 |
2407 |
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 |
2408 | ) { | ||
2409 | 55 | av_log(ic, AV_LOG_DEBUG, "Setting avg frame rate based on r frame rate\n"); | |
2410 | 55 | st->avg_frame_rate = st->r_frame_rate; | |
2411 | } | ||
2412 | |||
2413 | 6552 | av_freep(&sti->info->duration_error); | |
2414 | 6552 | sti->info->last_dts = AV_NOPTS_VALUE; | |
2415 | 6552 | sti->info->duration_count = 0; | |
2416 | 6552 | sti->info->rfps_duration_sum = 0; | |
2417 | } | ||
2418 | 8026 | } | |
2419 | |||
2420 | 8972 | static int extract_extradata_check(AVStream *st) | |
2421 | { | ||
2422 | 8972 | const AVBitStreamFilter *const f = av_bsf_get_by_name("extract_extradata"); | |
2423 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8972 times.
|
8972 | if (!f) |
2424 | ✗ | return 0; | |
2425 | |||
2426 |
1/2✓ Branch 0 taken 8972 times.
✗ Branch 1 not taken.
|
8972 | if (f->codec_ids) { |
2427 | const enum AVCodecID *ids; | ||
2428 |
2/2✓ Branch 0 taken 95052 times.
✓ Branch 1 taken 8082 times.
|
103134 | for (ids = f->codec_ids; *ids != AV_CODEC_ID_NONE; ids++) |
2429 |
2/2✓ Branch 0 taken 890 times.
✓ Branch 1 taken 94162 times.
|
95052 | if (*ids == st->codecpar->codec_id) |
2430 | 890 | return 1; | |
2431 | } | ||
2432 | |||
2433 | 8082 | return 0; | |
2434 | } | ||
2435 | |||
2436 | 7065 | static int extract_extradata_init(AVStream *st) | |
2437 | { | ||
2438 | 7065 | FFStream *const sti = ffstream(st); | |
2439 | const AVBitStreamFilter *f; | ||
2440 | int ret; | ||
2441 | |||
2442 | 7065 | f = av_bsf_get_by_name("extract_extradata"); | |
2443 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7065 times.
|
7065 | if (!f) |
2444 | ✗ | goto finish; | |
2445 | |||
2446 | /* check that the codec id is supported */ | ||
2447 | 7065 | ret = extract_extradata_check(st); | |
2448 |
2/2✓ Branch 0 taken 6285 times.
✓ Branch 1 taken 780 times.
|
7065 | if (!ret) |
2449 | 6285 | goto finish; | |
2450 | |||
2451 | 780 | av_bsf_free(&sti->extract_extradata.bsf); | |
2452 | 780 | ret = av_bsf_alloc(f, &sti->extract_extradata.bsf); | |
2453 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 780 times.
|
780 | if (ret < 0) |
2454 | ✗ | return ret; | |
2455 | |||
2456 | 780 | ret = avcodec_parameters_copy(sti->extract_extradata.bsf->par_in, | |
2457 | 780 | st->codecpar); | |
2458 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 780 times.
|
780 | if (ret < 0) |
2459 | ✗ | goto fail; | |
2460 | |||
2461 | 780 | sti->extract_extradata.bsf->time_base_in = st->time_base; | |
2462 | |||
2463 | 780 | ret = av_bsf_init(sti->extract_extradata.bsf); | |
2464 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 780 times.
|
780 | if (ret < 0) |
2465 | ✗ | goto fail; | |
2466 | |||
2467 | 780 | finish: | |
2468 | 7065 | sti->extract_extradata.inited = 1; | |
2469 | |||
2470 | 7065 | return 0; | |
2471 | ✗ | fail: | |
2472 | ✗ | av_bsf_free(&sti->extract_extradata.bsf); | |
2473 | ✗ | return ret; | |
2474 | } | ||
2475 | |||
2476 | 162147 | static int extract_extradata(FFFormatContext *si, AVStream *st, const AVPacket *pkt) | |
2477 | { | ||
2478 | 162147 | FFStream *const sti = ffstream(st); | |
2479 | 162147 | AVPacket *const pkt_ref = si->parse_pkt; | |
2480 | int ret; | ||
2481 | |||
2482 |
2/2✓ Branch 0 taken 7065 times.
✓ Branch 1 taken 155082 times.
|
162147 | if (!sti->extract_extradata.inited) { |
2483 | 7065 | ret = extract_extradata_init(st); | |
2484 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7065 times.
|
7065 | if (ret < 0) |
2485 | ✗ | return ret; | |
2486 | } | ||
2487 | |||
2488 |
3/4✓ Branch 0 taken 162147 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 160982 times.
✓ Branch 3 taken 1165 times.
|
162147 | if (sti->extract_extradata.inited && !sti->extract_extradata.bsf) |
2489 | 160982 | return 0; | |
2490 | |||
2491 | 1165 | ret = av_packet_ref(pkt_ref, pkt); | |
2492 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1165 times.
|
1165 | if (ret < 0) |
2493 | ✗ | return ret; | |
2494 | |||
2495 | 1165 | ret = av_bsf_send_packet(sti->extract_extradata.bsf, pkt_ref); | |
2496 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1165 times.
|
1165 | if (ret < 0) { |
2497 | ✗ | av_packet_unref(pkt_ref); | |
2498 | ✗ | return ret; | |
2499 | } | ||
2500 | |||
2501 |
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) { |
2502 | 1549 | ret = av_bsf_receive_packet(sti->extract_extradata.bsf, pkt_ref); | |
2503 |
2/2✓ Branch 0 taken 384 times.
✓ Branch 1 taken 1165 times.
|
1549 | if (ret < 0) { |
2504 |
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) |
2505 | ✗ | return ret; | |
2506 | 384 | continue; | |
2507 | } | ||
2508 | |||
2509 |
2/2✓ Branch 0 taken 1027 times.
✓ Branch 1 taken 384 times.
|
1411 | for (int i = 0; i < pkt_ref->side_data_elems; i++) { |
2510 | 1027 | AVPacketSideData *const side_data = &pkt_ref->side_data[i]; | |
2511 |
2/2✓ Branch 0 taken 781 times.
✓ Branch 1 taken 246 times.
|
1027 | if (side_data->type == AV_PKT_DATA_NEW_EXTRADATA) { |
2512 | 781 | sti->avctx->extradata = side_data->data; | |
2513 | 781 | sti->avctx->extradata_size = side_data->size; | |
2514 | 781 | side_data->data = NULL; | |
2515 | 781 | side_data->size = 0; | |
2516 | 781 | break; | |
2517 | } | ||
2518 | } | ||
2519 | 1165 | av_packet_unref(pkt_ref); | |
2520 | } | ||
2521 | |||
2522 | 1165 | return 0; | |
2523 | } | ||
2524 | |||
2525 | 7534 | int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options) | |
2526 | { | ||
2527 | 7534 | FFFormatContext *const si = ffformatcontext(ic); | |
2528 | 7534 | int count = 0, ret = 0, err; | |
2529 | int64_t read_size; | ||
2530 | 7534 | AVPacket *pkt1 = si->pkt; | |
2531 | 7534 | int64_t old_offset = avio_tell(ic->pb); | |
2532 | // new streams might appear, no options for those | ||
2533 | 7534 | int orig_nb_streams = ic->nb_streams; | |
2534 | int flush_codecs; | ||
2535 | 7534 | int64_t max_analyze_duration = ic->max_analyze_duration; | |
2536 | int64_t max_stream_analyze_duration; | ||
2537 | int64_t max_subtitle_analyze_duration; | ||
2538 | 7534 | int64_t probesize = ic->probesize; | |
2539 | 7534 | int eof_reached = 0; | |
2540 | |||
2541 | 7534 | flush_codecs = probesize > 0; | |
2542 | |||
2543 | 7534 | av_opt_set_int(ic, "skip_clear", 1, AV_OPT_SEARCH_CHILDREN); | |
2544 | |||
2545 | 7534 | max_stream_analyze_duration = max_analyze_duration; | |
2546 | 7534 | max_subtitle_analyze_duration = max_analyze_duration; | |
2547 |
2/2✓ Branch 0 taken 7533 times.
✓ Branch 1 taken 1 times.
|
7534 | if (!max_analyze_duration) { |
2548 | 7533 | max_stream_analyze_duration = | |
2549 | 7533 | max_analyze_duration = 5*AV_TIME_BASE; | |
2550 | 7533 | max_subtitle_analyze_duration = 30*AV_TIME_BASE; | |
2551 |
2/2✓ Branch 0 taken 36 times.
✓ Branch 1 taken 7497 times.
|
7533 | if (!strcmp(ic->iformat->name, "flv")) |
2552 | 36 | max_stream_analyze_duration = 90*AV_TIME_BASE; | |
2553 |
4/4✓ Branch 0 taken 7508 times.
✓ Branch 1 taken 25 times.
✓ Branch 2 taken 44 times.
✓ Branch 3 taken 7464 times.
|
7533 | if (!strcmp(ic->iformat->name, "mpeg") || !strcmp(ic->iformat->name, "mpegts")) |
2554 | 69 | max_stream_analyze_duration = 7*AV_TIME_BASE; | |
2555 | } | ||
2556 | |||
2557 |
2/2✓ Branch 0 taken 4498 times.
✓ Branch 1 taken 3036 times.
|
7534 | if (ic->pb) { |
2558 | 4498 | FFIOContext *const ctx = ffiocontext(ic->pb); | |
2559 | 4498 | av_log(ic, AV_LOG_DEBUG, "Before avformat_find_stream_info() pos: %"PRId64" bytes read:%"PRId64" seeks:%d nb_streams:%d\n", | |
2560 | avio_tell(ic->pb), ctx->bytes_read, ctx->seek_count, ic->nb_streams); | ||
2561 | } | ||
2562 | |||
2563 |
2/2✓ Branch 0 taken 8160 times.
✓ Branch 1 taken 7534 times.
|
15694 | for (unsigned i = 0; i < ic->nb_streams; i++) { |
2564 | const AVCodec *codec; | ||
2565 | 8160 | AVDictionary *thread_opt = NULL; | |
2566 | 8160 | AVStream *const st = ic->streams[i]; | |
2567 | 8160 | FFStream *const sti = ffstream(st); | |
2568 | 8160 | AVCodecContext *const avctx = sti->avctx; | |
2569 | |||
2570 | /* check if the caller has overridden the codec id */ | ||
2571 | // only for the split stuff | ||
2572 |
4/6✓ Branch 0 taken 8160 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8160 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 7656 times.
✓ Branch 5 taken 504 times.
|
8160 | if (!sti->parser && !(ic->flags & AVFMT_FLAG_NOPARSE) && sti->request_probe <= 0) { |
2573 | 7656 | sti->parser = av_parser_init(st->codecpar->codec_id); | |
2574 |
2/2✓ Branch 0 taken 5289 times.
✓ Branch 1 taken 2367 times.
|
7656 | if (sti->parser) { |
2575 |
2/2✓ Branch 0 taken 706 times.
✓ Branch 1 taken 4583 times.
|
5289 | if (sti->need_parsing == AVSTREAM_PARSE_HEADERS) { |
2576 | 706 | sti->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES; | |
2577 |
2/2✓ Branch 0 taken 817 times.
✓ Branch 1 taken 3766 times.
|
4583 | } else if (sti->need_parsing == AVSTREAM_PARSE_FULL_RAW) { |
2578 | 817 | sti->parser->flags |= PARSER_FLAG_USE_CODEC_TS; | |
2579 | } | ||
2580 |
2/2✓ Branch 0 taken 692 times.
✓ Branch 1 taken 1675 times.
|
2367 | } else if (sti->need_parsing) { |
2581 | 692 | av_log(ic, AV_LOG_VERBOSE, "parser not found for codec " | |
2582 | "%s, packets or times may be invalid.\n", | ||
2583 | 692 | avcodec_get_name(st->codecpar->codec_id)); | |
2584 | } | ||
2585 | } | ||
2586 | |||
2587 | 8160 | ret = avcodec_parameters_to_context(avctx, st->codecpar); | |
2588 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8160 times.
|
8160 | if (ret < 0) |
2589 | ✗ | goto find_stream_info_err; | |
2590 |
2/2✓ Branch 0 taken 7656 times.
✓ Branch 1 taken 504 times.
|
8160 | if (sti->request_probe <= 0) |
2591 | 7656 | sti->avctx_inited = 1; | |
2592 | |||
2593 | 8160 | codec = find_probe_decoder(ic, st, st->codecpar->codec_id); | |
2594 | |||
2595 | /* Force thread count to 1 since the H.264 decoder will not extract | ||
2596 | * SPS and PPS to extradata during multi-threaded decoding. */ | ||
2597 |
2/2✓ Branch 0 taken 7876 times.
✓ Branch 1 taken 284 times.
|
8160 | av_dict_set(options ? &options[i] : &thread_opt, "threads", "1", 0); |
2598 | /* Force lowres to 0. The decoder might reduce the video size by the | ||
2599 | * lowres factor, and we don't want that propagated to the stream's | ||
2600 | * codecpar */ | ||
2601 |
2/2✓ Branch 0 taken 7876 times.
✓ Branch 1 taken 284 times.
|
8160 | av_dict_set(options ? &options[i] : &thread_opt, "lowres", "0", 0); |
2602 | |||
2603 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8160 times.
|
8160 | if (ic->codec_whitelist) |
2604 | ✗ | av_dict_set(options ? &options[i] : &thread_opt, "codec_whitelist", ic->codec_whitelist, 0); | |
2605 | |||
2606 | // Try to just open decoders, in case this is enough to get parameters. | ||
2607 | // Also ensure that subtitle_header is properly set. | ||
2608 |
4/4✓ Branch 1 taken 7347 times.
✓ Branch 2 taken 813 times.
✓ Branch 3 taken 503 times.
✓ Branch 4 taken 6844 times.
|
8160 | if (!has_codec_parameters(st, NULL) && sti->request_probe <= 0 || |
2609 |
2/2✓ Branch 0 taken 78 times.
✓ Branch 1 taken 1238 times.
|
1316 | st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) { |
2610 |
3/4✓ Branch 0 taken 6903 times.
✓ Branch 1 taken 19 times.
✓ Branch 2 taken 6903 times.
✗ Branch 3 not taken.
|
6922 | if (codec && !avctx->codec) |
2611 |
4/4✓ Branch 0 taken 6623 times.
✓ Branch 1 taken 280 times.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 6897 times.
|
6903 | if (avcodec_open2(avctx, codec, options ? &options[i] : &thread_opt) < 0) |
2612 | 6 | av_log(ic, AV_LOG_WARNING, | |
2613 | "Failed to open codec in %s\n", __func__); | ||
2614 | } | ||
2615 |
2/2✓ Branch 0 taken 284 times.
✓ Branch 1 taken 7876 times.
|
8160 | if (!options) |
2616 | 284 | av_dict_free(&thread_opt); | |
2617 | } | ||
2618 | |||
2619 | 7534 | read_size = 0; | |
2620 | 192818 | for (;;) { | |
2621 | const AVPacket *pkt; | ||
2622 | AVStream *st; | ||
2623 | FFStream *sti; | ||
2624 | AVCodecContext *avctx; | ||
2625 | int analyzed_all_streams; | ||
2626 | unsigned i; | ||
2627 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 200352 times.
|
200352 | if (ff_check_interrupt(&ic->interrupt_callback)) { |
2628 | ✗ | ret = AVERROR_EXIT; | |
2629 | ✗ | av_log(ic, AV_LOG_DEBUG, "interrupted\n"); | |
2630 | ✗ | break; | |
2631 | } | ||
2632 | |||
2633 | /* check if one codec still needs to be handled */ | ||
2634 |
2/2✓ Branch 0 taken 208955 times.
✓ Branch 1 taken 109673 times.
|
318628 | for (i = 0; i < ic->nb_streams; i++) { |
2635 | 208955 | AVStream *const st = ic->streams[i]; | |
2636 | 208955 | FFStream *const sti = ffstream(st); | |
2637 | 208955 | int fps_analyze_framecount = 20; | |
2638 | int count; | ||
2639 | |||
2640 |
2/2✓ Branch 1 taken 9279 times.
✓ Branch 2 taken 199676 times.
|
208955 | if (!has_codec_parameters(st, NULL)) |
2641 | 9279 | break; | |
2642 | /* If the timebase is coarse (like the usual millisecond precision | ||
2643 | * of mkv), we need to analyze more frames to reliably arrive at | ||
2644 | * the correct fps. */ | ||
2645 |
2/2✓ Branch 1 taken 120343 times.
✓ Branch 2 taken 79333 times.
|
199676 | if (av_q2d(st->time_base) > 0.0005) |
2646 | 120343 | fps_analyze_framecount *= 2; | |
2647 |
2/2✓ Branch 1 taken 8421 times.
✓ Branch 2 taken 191255 times.
|
199676 | if (!tb_unreliable(ic, st)) |
2648 | 8421 | fps_analyze_framecount = 0; | |
2649 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 199676 times.
|
199676 | if (ic->fps_probe_size >= 0) |
2650 | ✗ | fps_analyze_framecount = ic->fps_probe_size; | |
2651 |
2/2✓ Branch 0 taken 44 times.
✓ Branch 1 taken 199632 times.
|
199676 | if (st->disposition & AV_DISPOSITION_ATTACHED_PIC) |
2652 | 44 | fps_analyze_framecount = 0; | |
2653 | /* variable fps and no guess at the real fps */ | ||
2654 |
2/2✓ Branch 0 taken 19296 times.
✓ Branch 1 taken 180380 times.
|
199676 | count = (ic->iformat->flags & AVFMT_NOTIMESTAMPS) ? |
2655 | 19296 | sti->info->codec_info_duration_fields/2 : | |
2656 | 180380 | sti->info->duration_count; | |
2657 |
4/4✓ Branch 0 taken 101133 times.
✓ Branch 1 taken 98543 times.
✓ Branch 2 taken 223 times.
✓ Branch 3 taken 100910 times.
|
199676 | if (!(st->r_frame_rate.num && st->avg_frame_rate.num) && |
2658 |
2/2✓ Branch 0 taken 42898 times.
✓ Branch 1 taken 55868 times.
|
98766 | st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { |
2659 |
2/2✓ Branch 0 taken 24878 times.
✓ Branch 1 taken 18020 times.
|
42898 | if (count < fps_analyze_framecount) |
2660 | 24878 | break; | |
2661 | } | ||
2662 | // Look at the first 3 frames if there is evidence of frame delay | ||
2663 | // but the decoder delay is not set. | ||
2664 |
6/6✓ Branch 0 taken 2898 times.
✓ Branch 1 taken 171900 times.
✓ Branch 2 taken 130 times.
✓ Branch 3 taken 2768 times.
✓ Branch 4 taken 12 times.
✓ Branch 5 taken 118 times.
|
174798 | if (sti->info->frame_delay_evidence && count < 2 && sti->avctx->has_b_frames == 0) |
2665 | 12 | break; | |
2666 |
2/2✓ Branch 0 taken 154622 times.
✓ Branch 1 taken 20164 times.
|
174786 | if (!sti->avctx->extradata && |
2667 |
6/6✓ Branch 0 taken 152825 times.
✓ Branch 1 taken 1797 times.
✓ Branch 2 taken 110 times.
✓ Branch 3 taken 152715 times.
✓ Branch 4 taken 110 times.
✓ Branch 5 taken 1797 times.
|
156529 | (!sti->extract_extradata.inited || sti->extract_extradata.bsf) && |
2668 | 1907 | extract_extradata_check(st)) | |
2669 | 110 | break; | |
2670 |
2/2✓ Branch 0 taken 59553 times.
✓ Branch 1 taken 115123 times.
|
174676 | if (sti->first_dts == AV_NOPTS_VALUE && |
2671 |
6/6✓ Branch 0 taken 9372 times.
✓ Branch 1 taken 50181 times.
✓ Branch 2 taken 9291 times.
✓ Branch 3 taken 81 times.
✓ Branch 4 taken 57183 times.
✓ Branch 5 taken 2289 times.
|
119025 | (!(ic->iformat->flags & AVFMT_NOTIMESTAMPS) || sti->need_parsing == AVSTREAM_PARSE_FULL_RAW) && |
2672 |
2/2✓ Branch 0 taken 59428 times.
✓ Branch 1 taken 44 times.
|
59472 | sti->codec_info_nb_frames < ((st->disposition & AV_DISPOSITION_ATTACHED_PIC) ? 1 : ic->max_ts_probe) && |
2673 |
2/2✓ Branch 0 taken 41445 times.
✓ Branch 1 taken 15738 times.
|
57183 | (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO || |
2674 |
2/2✓ Branch 0 taken 783 times.
✓ Branch 1 taken 40662 times.
|
41445 | st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)) |
2675 | break; | ||
2676 | } | ||
2677 | 200352 | analyzed_all_streams = 0; | |
2678 |
4/4✓ Branch 0 taken 109673 times.
✓ Branch 1 taken 90679 times.
✓ Branch 2 taken 109636 times.
✓ Branch 3 taken 37 times.
|
200352 | if (i == ic->nb_streams && !si->missing_streams) { |
2679 | 109636 | analyzed_all_streams = 1; | |
2680 | /* NOTE: If the format has no header, then we need to read some | ||
2681 | * packets to get most of the streams, so we cannot stop here. */ | ||
2682 |
2/2✓ Branch 0 taken 3286 times.
✓ Branch 1 taken 106350 times.
|
109636 | if (!(ic->ctx_flags & AVFMTCTX_NOHEADER)) { |
2683 | /* If we found the info for all the codecs, we can stop. */ | ||
2684 | 3286 | ret = count; | |
2685 | 3286 | av_log(ic, AV_LOG_DEBUG, "All info found\n"); | |
2686 | 3286 | flush_codecs = 0; | |
2687 | 3286 | break; | |
2688 | } | ||
2689 | } | ||
2690 | /* We did not get all the codec info, but we read too much data. */ | ||
2691 |
2/2✓ Branch 0 taken 2967 times.
✓ Branch 1 taken 194099 times.
|
197066 | if (read_size >= probesize) { |
2692 | 2967 | ret = count; | |
2693 | 2967 | av_log(ic, AV_LOG_DEBUG, | |
2694 | "Probe buffer size limit of %"PRId64" bytes reached\n", probesize); | ||
2695 |
2/2✓ Branch 0 taken 2981 times.
✓ Branch 1 taken 2967 times.
|
5948 | for (unsigned i = 0; i < ic->nb_streams; i++) { |
2696 | 2981 | AVStream *const st = ic->streams[i]; | |
2697 | 2981 | FFStream *const sti = ffstream(st); | |
2698 |
2/2✓ Branch 0 taken 58 times.
✓ Branch 1 taken 2923 times.
|
2981 | if (!st->r_frame_rate.num && |
2699 |
2/2✓ Branch 0 taken 15 times.
✓ Branch 1 taken 43 times.
|
58 | sti->info->duration_count <= 1 && |
2700 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 14 times.
|
15 | st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && |
2701 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | strcmp(ic->iformat->name, "image2")) |
2702 | 1 | av_log(ic, AV_LOG_WARNING, | |
2703 | "Stream #%d: not enough frames to estimate rate; " | ||
2704 | "consider increasing probesize\n", i); | ||
2705 | } | ||
2706 | 2967 | break; | |
2707 | } | ||
2708 | |||
2709 | /* NOTE: A new stream can be added there if no header in file | ||
2710 | * (AVFMTCTX_NOHEADER). */ | ||
2711 | 194099 | ret = read_frame_internal(ic, pkt1); | |
2712 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 194099 times.
|
194099 | if (ret == AVERROR(EAGAIN)) |
2713 | ✗ | continue; | |
2714 | |||
2715 |
2/2✓ Branch 0 taken 1178 times.
✓ Branch 1 taken 192921 times.
|
194099 | if (ret < 0) { |
2716 | /* EOF or error*/ | ||
2717 | 1178 | eof_reached = 1; | |
2718 | 1178 | break; | |
2719 | } | ||
2720 | |||
2721 |
1/2✓ Branch 0 taken 192921 times.
✗ Branch 1 not taken.
|
192921 | if (!(ic->flags & AVFMT_FLAG_NOBUFFER)) { |
2722 | 192921 | ret = avpriv_packet_list_put(&si->packet_buffer, | |
2723 | pkt1, NULL, 0); | ||
2724 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 192921 times.
|
192921 | if (ret < 0) |
2725 | ✗ | goto unref_then_goto_end; | |
2726 | |||
2727 | 192921 | pkt = &si->packet_buffer.tail->pkt; | |
2728 | } else { | ||
2729 | ✗ | pkt = pkt1; | |
2730 | } | ||
2731 | |||
2732 | 192921 | st = ic->streams[pkt->stream_index]; | |
2733 | 192921 | sti = ffstream(st); | |
2734 |
2/2✓ Branch 0 taken 192865 times.
✓ Branch 1 taken 56 times.
|
192921 | if (!(st->disposition & AV_DISPOSITION_ATTACHED_PIC)) |
2735 | 192865 | read_size += pkt->size; | |
2736 | |||
2737 | 192921 | avctx = sti->avctx; | |
2738 |
2/2✓ Branch 0 taken 674 times.
✓ Branch 1 taken 192247 times.
|
192921 | if (!sti->avctx_inited) { |
2739 | 674 | ret = avcodec_parameters_to_context(avctx, st->codecpar); | |
2740 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 674 times.
|
674 | if (ret < 0) |
2741 | ✗ | goto unref_then_goto_end; | |
2742 | 674 | sti->avctx_inited = 1; | |
2743 | } | ||
2744 | |||
2745 |
4/4✓ Branch 0 taken 176590 times.
✓ Branch 1 taken 16331 times.
✓ Branch 2 taken 164256 times.
✓ Branch 3 taken 12334 times.
|
192921 | if (pkt->dts != AV_NOPTS_VALUE && sti->codec_info_nb_frames > 1) { |
2746 | /* check for non-increasing dts */ | ||
2747 |
2/2✓ Branch 0 taken 159395 times.
✓ Branch 1 taken 4861 times.
|
164256 | if (sti->info->fps_last_dts != AV_NOPTS_VALUE && |
2748 |
2/2✓ Branch 0 taken 23 times.
✓ Branch 1 taken 159372 times.
|
159395 | sti->info->fps_last_dts >= pkt->dts) { |
2749 | 23 | av_log(ic, AV_LOG_DEBUG, | |
2750 | "Non-increasing DTS in stream %d: packet %d with DTS " | ||
2751 | "%"PRId64", packet %d with DTS %"PRId64"\n", | ||
2752 | 23 | st->index, sti->info->fps_last_dts_idx, | |
2753 | 23 | sti->info->fps_last_dts, sti->codec_info_nb_frames, | |
2754 | 23 | pkt->dts); | |
2755 | 23 | sti->info->fps_first_dts = | |
2756 | 23 | sti->info->fps_last_dts = AV_NOPTS_VALUE; | |
2757 | } | ||
2758 | /* Check for a discontinuity in dts. If the difference in dts | ||
2759 | * is more than 1000 times the average packet duration in the | ||
2760 | * sequence, we treat it as a discontinuity. */ | ||
2761 |
2/2✓ Branch 0 taken 159372 times.
✓ Branch 1 taken 4884 times.
|
164256 | if (sti->info->fps_last_dts != AV_NOPTS_VALUE && |
2762 |
2/2✓ Branch 0 taken 154540 times.
✓ Branch 1 taken 4832 times.
|
159372 | sti->info->fps_last_dts_idx > sti->info->fps_first_dts_idx && |
2763 | 154540 | (pkt->dts - (uint64_t)sti->info->fps_last_dts) / 1000 > | |
2764 | 154540 | (sti->info->fps_last_dts - (uint64_t)sti->info->fps_first_dts) / | |
2765 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 154540 times.
|
154540 | (sti->info->fps_last_dts_idx - sti->info->fps_first_dts_idx)) { |
2766 | ✗ | av_log(ic, AV_LOG_WARNING, | |
2767 | "DTS discontinuity in stream %d: packet %d with DTS " | ||
2768 | "%"PRId64", packet %d with DTS %"PRId64"\n", | ||
2769 | ✗ | st->index, sti->info->fps_last_dts_idx, | |
2770 | ✗ | sti->info->fps_last_dts, sti->codec_info_nb_frames, | |
2771 | ✗ | pkt->dts); | |
2772 | ✗ | sti->info->fps_first_dts = | |
2773 | ✗ | sti->info->fps_last_dts = AV_NOPTS_VALUE; | |
2774 | } | ||
2775 | |||
2776 | /* update stored dts values */ | ||
2777 |
2/2✓ Branch 0 taken 4884 times.
✓ Branch 1 taken 159372 times.
|
164256 | if (sti->info->fps_first_dts == AV_NOPTS_VALUE) { |
2778 | 4884 | sti->info->fps_first_dts = pkt->dts; | |
2779 | 4884 | sti->info->fps_first_dts_idx = sti->codec_info_nb_frames; | |
2780 | } | ||
2781 | 164256 | sti->info->fps_last_dts = pkt->dts; | |
2782 | 164256 | sti->info->fps_last_dts_idx = sti->codec_info_nb_frames; | |
2783 | } | ||
2784 |
2/2✓ Branch 0 taken 179418 times.
✓ Branch 1 taken 13503 times.
|
192921 | if (sti->codec_info_nb_frames > 1) { |
2785 | 179418 | int64_t t = 0; | |
2786 | int64_t limit; | ||
2787 | |||
2788 |
1/2✓ Branch 0 taken 179418 times.
✗ Branch 1 not taken.
|
179418 | if (st->time_base.den > 0) |
2789 | 179418 | t = av_rescale_q(sti->info->codec_info_duration, st->time_base, AV_TIME_BASE_Q); | |
2790 |
2/2✓ Branch 0 taken 112504 times.
✓ Branch 1 taken 66914 times.
|
179418 | if (st->avg_frame_rate.num > 0) |
2791 |
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)); |
2792 | |||
2793 |
2/2✓ Branch 0 taken 4116 times.
✓ Branch 1 taken 175302 times.
|
179418 | if ( t == 0 |
2794 |
2/2✓ Branch 0 taken 535 times.
✓ Branch 1 taken 3581 times.
|
4116 | && sti->codec_info_nb_frames > 30 |
2795 |
2/2✓ Branch 0 taken 497 times.
✓ Branch 1 taken 38 times.
|
535 | && sti->info->fps_first_dts != AV_NOPTS_VALUE |
2796 |
1/2✓ Branch 0 taken 497 times.
✗ Branch 1 not taken.
|
497 | && sti->info->fps_last_dts != AV_NOPTS_VALUE) { |
2797 | 497 | int64_t dur = av_sat_sub64(sti->info->fps_last_dts, sti->info->fps_first_dts); | |
2798 |
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)); |
2799 | } | ||
2800 | |||
2801 |
2/2✓ Branch 0 taken 100152 times.
✓ Branch 1 taken 79266 times.
|
179418 | if (analyzed_all_streams) limit = max_analyze_duration; |
2802 |
2/2✓ Branch 0 taken 91 times.
✓ Branch 1 taken 79175 times.
|
79266 | else if (avctx->codec_type == AVMEDIA_TYPE_SUBTITLE) limit = max_subtitle_analyze_duration; |
2803 | 79175 | else limit = max_stream_analyze_duration; | |
2804 | |||
2805 |
2/2✓ Branch 0 taken 103 times.
✓ Branch 1 taken 179315 times.
|
179418 | if (t >= limit) { |
2806 | 103 | av_log(ic, AV_LOG_VERBOSE, "max_analyze_duration %"PRId64" reached at %"PRId64" microseconds st:%d\n", | |
2807 | limit, | ||
2808 | 103 | t, pkt->stream_index); | |
2809 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 103 times.
|
103 | if (ic->flags & AVFMT_FLAG_NOBUFFER) |
2810 | ✗ | av_packet_unref(pkt1); | |
2811 | 103 | break; | |
2812 | } | ||
2813 |
3/4✓ Branch 0 taken 175669 times.
✓ Branch 1 taken 3646 times.
✓ Branch 2 taken 175669 times.
✗ Branch 3 not taken.
|
179315 | if (pkt->duration > 0 && pkt->duration < INT64_MAX - sti->info->codec_info_duration) { |
2814 |
4/4✓ Branch 0 taken 175625 times.
✓ Branch 1 taken 44 times.
✓ Branch 2 taken 14236 times.
✓ Branch 3 taken 161389 times.
|
175669 | const int fields = sti->codec_desc && (sti->codec_desc->props & AV_CODEC_PROP_FIELDS); |
2815 |
1/8✗ Branch 0 not taken.
✓ Branch 1 taken 175669 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.
|
175669 | if (avctx->codec_type == AVMEDIA_TYPE_SUBTITLE && pkt->pts != AV_NOPTS_VALUE && st->start_time != AV_NOPTS_VALUE && pkt->pts >= st->start_time |
2816 | ✗ | && (uint64_t)pkt->pts - st->start_time < INT64_MAX | |
2817 | ) { | ||
2818 | ✗ | sti->info->codec_info_duration = FFMIN(pkt->pts - st->start_time, sti->info->codec_info_duration + pkt->duration); | |
2819 | } else | ||
2820 | 175669 | sti->info->codec_info_duration += pkt->duration; | |
2821 |
4/4✓ Branch 0 taken 35638 times.
✓ Branch 1 taken 91738 times.
✓ Branch 2 taken 13864 times.
✓ Branch 3 taken 21774 times.
|
303045 | sti->info->codec_info_duration_fields += sti->parser && sti->need_parsing && fields |
2822 |
2/2✓ Branch 0 taken 127376 times.
✓ Branch 1 taken 48293 times.
|
303045 | ? sti->parser->repeat_pict + 1 : 2; |
2823 | } | ||
2824 | } | ||
2825 |
2/2✓ Branch 0 taken 131048 times.
✓ Branch 1 taken 61770 times.
|
192818 | if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { |
2826 | #if FF_API_R_FRAME_RATE | ||
2827 | 131048 | ff_rfps_add_frame(ic, st, pkt->dts); | |
2828 | #endif | ||
2829 |
6/6✓ Branch 0 taken 4230 times.
✓ Branch 1 taken 126818 times.
✓ Branch 2 taken 3991 times.
✓ Branch 3 taken 239 times.
✓ Branch 4 taken 1965 times.
✓ Branch 5 taken 2026 times.
|
131048 | if (pkt->dts != pkt->pts && pkt->dts != AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE) |
2830 | 1965 | sti->info->frame_delay_evidence = 1; | |
2831 | } | ||
2832 |
2/2✓ Branch 0 taken 161998 times.
✓ Branch 1 taken 30820 times.
|
192818 | if (!sti->avctx->extradata) { |
2833 | 161998 | ret = extract_extradata(si, st, pkt); | |
2834 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 161998 times.
|
161998 | if (ret < 0) |
2835 | ✗ | goto unref_then_goto_end; | |
2836 | } | ||
2837 | |||
2838 | /* If still no information, we try to open the codec and to | ||
2839 | * decompress the frame. We try to avoid that in most cases as | ||
2840 | * it takes longer and uses more memory. For MPEG-4, we need to | ||
2841 | * decompress for QuickTime. | ||
2842 | * | ||
2843 | * If AV_CODEC_CAP_CHANNEL_CONF is set this will force decoding of at | ||
2844 | * least one frame of codec data, this makes sure the codec initializes | ||
2845 | * the channel configuration and does not only trust the values from | ||
2846 | * the container. */ | ||
2847 |
2/2✓ Branch 0 taken 175311 times.
✓ Branch 1 taken 17507 times.
|
368129 | try_decode_frame(ic, st, pkt, |
2848 |
2/2✓ Branch 0 taken 79484 times.
✓ Branch 1 taken 95827 times.
|
175311 | (options && i < orig_nb_streams) ? &options[i] : NULL); |
2849 | |||
2850 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 192818 times.
|
192818 | if (ic->flags & AVFMT_FLAG_NOBUFFER) |
2851 | ✗ | av_packet_unref(pkt1); | |
2852 | |||
2853 | 192818 | sti->codec_info_nb_frames++; | |
2854 | 192818 | count++; | |
2855 | } | ||
2856 | |||
2857 |
2/2✓ Branch 0 taken 1178 times.
✓ Branch 1 taken 6356 times.
|
7534 | if (eof_reached) { |
2858 |
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++) { |
2859 | 1468 | AVStream *const st = ic->streams[stream_index]; | |
2860 | 1468 | AVCodecContext *const avctx = ffstream(st)->avctx; | |
2861 |
2/2✓ Branch 1 taken 29 times.
✓ Branch 2 taken 1439 times.
|
1468 | if (!has_codec_parameters(st, NULL)) { |
2862 | 29 | const AVCodec *codec = find_probe_decoder(ic, st, st->codecpar->codec_id); | |
2863 |
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) { |
2864 | 8 | AVDictionary *opts = NULL; | |
2865 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
|
8 | if (ic->codec_whitelist) |
2866 | ✗ | av_dict_set(&opts, "codec_whitelist", ic->codec_whitelist, 0); | |
2867 |
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) |
2868 | ✗ | av_log(ic, AV_LOG_WARNING, | |
2869 | "Failed to open codec in %s\n", __func__); | ||
2870 | 8 | av_dict_free(&opts); | |
2871 | } | ||
2872 | } | ||
2873 | |||
2874 | // EOF already reached while reading the stream above. | ||
2875 | // So continue with reoordering DTS with whatever delay we have. | ||
2876 |
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)) { |
2877 | 18 | update_dts_from_pts(ic, stream_index, si->packet_buffer.head); | |
2878 | } | ||
2879 | } | ||
2880 | } | ||
2881 | |||
2882 |
2/2✓ Branch 0 taken 4248 times.
✓ Branch 1 taken 3286 times.
|
7534 | if (flush_codecs) { |
2883 | 4248 | AVPacket *empty_pkt = si->pkt; | |
2884 | 4248 | int err = 0; | |
2885 | 4248 | av_packet_unref(empty_pkt); | |
2886 | |||
2887 |
2/2✓ Branch 0 taken 4591 times.
✓ Branch 1 taken 4248 times.
|
8839 | for (unsigned i = 0; i < ic->nb_streams; i++) { |
2888 | 4591 | AVStream *const st = ic->streams[i]; | |
2889 | 4591 | FFStream *const sti = ffstream(st); | |
2890 | |||
2891 | /* flush the decoders */ | ||
2892 |
2/2✓ Branch 0 taken 4513 times.
✓ Branch 1 taken 78 times.
|
4591 | if (sti->info->found_decoder == 1) { |
2893 |
2/2✓ Branch 0 taken 4198 times.
✓ Branch 1 taken 315 times.
|
8711 | err = try_decode_frame(ic, st, empty_pkt, |
2894 |
2/2✓ Branch 0 taken 4182 times.
✓ Branch 1 taken 16 times.
|
4198 | (options && i < orig_nb_streams) |
2895 | 4182 | ? &options[i] : NULL); | |
2896 | |||
2897 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4513 times.
|
4513 | if (err < 0) { |
2898 | ✗ | av_log(ic, AV_LOG_INFO, | |
2899 | "decoding for stream %d failed\n", st->index); | ||
2900 | } | ||
2901 | } | ||
2902 | } | ||
2903 | } | ||
2904 | |||
2905 | 7534 | ff_rfps_calculate(ic); | |
2906 | |||
2907 |
2/2✓ Branch 0 taken 8343 times.
✓ Branch 1 taken 7534 times.
|
15877 | for (unsigned i = 0; i < ic->nb_streams; i++) { |
2908 | 8343 | AVStream *const st = ic->streams[i]; | |
2909 | 8343 | FFStream *const sti = ffstream(st); | |
2910 | 8343 | AVCodecContext *const avctx = sti->avctx; | |
2911 | |||
2912 |
2/2✓ Branch 0 taken 6214 times.
✓ Branch 1 taken 2129 times.
|
8343 | if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) { |
2913 |
6/6✓ Branch 0 taken 634 times.
✓ Branch 1 taken 5580 times.
✓ Branch 2 taken 585 times.
✓ Branch 3 taken 49 times.
✓ Branch 4 taken 569 times.
✓ Branch 5 taken 16 times.
|
6214 | if (avctx->codec_id == AV_CODEC_ID_RAWVIDEO && !avctx->codec_tag && !avctx->bits_per_coded_sample) { |
2914 | 569 | uint32_t tag= avcodec_pix_fmt_to_codec_tag(avctx->pix_fmt); | |
2915 |
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) |
2916 | 562 | avctx->codec_tag= tag; | |
2917 | } | ||
2918 | |||
2919 | /* estimate average framerate if not set by demuxer */ | ||
2920 |
2/2✓ Branch 0 taken 3989 times.
✓ Branch 1 taken 2225 times.
|
6214 | if (sti->info->codec_info_duration_fields && |
2921 |
2/2✓ Branch 0 taken 269 times.
✓ Branch 1 taken 3720 times.
|
3989 | !st->avg_frame_rate.num && |
2922 |
1/2✓ Branch 0 taken 269 times.
✗ Branch 1 not taken.
|
269 | sti->info->codec_info_duration) { |
2923 | 269 | int best_fps = 0; | |
2924 | 269 | double best_error = 0.01; | |
2925 | 269 | AVRational codec_frame_rate = avctx->framerate; | |
2926 | |||
2927 |
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|| |
2928 |
1/2✓ Branch 0 taken 269 times.
✗ Branch 1 not taken.
|
269 | sti->info->codec_info_duration_fields >= INT64_MAX / st->time_base.den || |
2929 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 269 times.
|
269 | sti->info->codec_info_duration < 0) |
2930 | ✗ | continue; | |
2931 | 269 | av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den, | |
2932 | 269 | sti->info->codec_info_duration_fields * (int64_t) st->time_base.den, | |
2933 | 269 | sti->info->codec_info_duration * 2 * (int64_t) st->time_base.num, 60000); | |
2934 | |||
2935 | /* Round guessed framerate to a "standard" framerate if it's | ||
2936 | * within 1% of the original estimate. */ | ||
2937 |
2/2✓ Branch 0 taken 107331 times.
✓ Branch 1 taken 269 times.
|
107600 | for (int j = 0; j < MAX_STD_TIMEBASES; j++) { |
2938 | 107331 | AVRational std_fps = { get_std_framerate(j), 12 * 1001 }; | |
2939 | 107331 | double error = fabs(av_q2d(st->avg_frame_rate) / | |
2940 | 107331 | av_q2d(std_fps) - 1); | |
2941 | |||
2942 |
2/2✓ Branch 0 taken 736 times.
✓ Branch 1 taken 106595 times.
|
107331 | if (error < best_error) { |
2943 | 736 | best_error = error; | |
2944 | 736 | best_fps = std_fps.num; | |
2945 | } | ||
2946 | |||
2947 |
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) && |
2948 |
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) { |
2949 | 12768 | error = fabs(av_q2d(codec_frame_rate) / | |
2950 | 12768 | av_q2d(std_fps) - 1); | |
2951 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 12758 times.
|
12768 | if (error < best_error) { |
2952 | 10 | best_error = error; | |
2953 | 10 | best_fps = std_fps.num; | |
2954 | } | ||
2955 | } | ||
2956 | } | ||
2957 |
2/2✓ Branch 0 taken 267 times.
✓ Branch 1 taken 2 times.
|
269 | if (best_fps) |
2958 | 267 | av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den, | |
2959 | best_fps, 12 * 1001, INT_MAX); | ||
2960 | } | ||
2961 |
2/2✓ Branch 0 taken 1833 times.
✓ Branch 1 taken 4381 times.
|
6214 | if (!st->r_frame_rate.num) { |
2962 | 1833 | const AVCodecDescriptor *desc = sti->codec_desc; | |
2963 |
3/4✓ Branch 0 taken 1833 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 246 times.
✓ Branch 3 taken 1587 times.
|
1833 | AVRational mul = (AVRational){ desc && (desc->props & AV_CODEC_PROP_FIELDS) ? 2 : 1, 1 }; |
2964 | 1833 | AVRational fr = av_mul_q(avctx->framerate, mul); | |
2965 | |||
2966 |
5/6✓ Branch 0 taken 188 times.
✓ Branch 1 taken 1645 times.
✓ Branch 2 taken 188 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 174 times.
✓ Branch 7 taken 14 times.
|
1833 | if (fr.num && fr.den && av_cmp_q(st->time_base, av_inv_q(fr)) <= 0) { |
2967 | 174 | st->r_frame_rate = fr; | |
2968 | } else { | ||
2969 | 1659 | st->r_frame_rate.num = st->time_base.den; | |
2970 | 1659 | st->r_frame_rate.den = st->time_base.num; | |
2971 | } | ||
2972 | } | ||
2973 | 6214 | st->codecpar->framerate = avctx->framerate; | |
2974 |
3/4✓ Branch 0 taken 116 times.
✓ Branch 1 taken 6098 times.
✓ Branch 2 taken 116 times.
✗ Branch 3 not taken.
|
6214 | if (sti->display_aspect_ratio.num && sti->display_aspect_ratio.den) { |
2975 | 116 | AVRational hw_ratio = { avctx->height, avctx->width }; | |
2976 | 116 | st->sample_aspect_ratio = av_mul_q(sti->display_aspect_ratio, | |
2977 | hw_ratio); | ||
2978 | } | ||
2979 |
2/2✓ Branch 0 taken 1960 times.
✓ Branch 1 taken 169 times.
|
2129 | } else if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) { |
2980 |
2/2✓ Branch 0 taken 643 times.
✓ Branch 1 taken 1317 times.
|
1960 | if (!avctx->bits_per_coded_sample) |
2981 | 643 | avctx->bits_per_coded_sample = | |
2982 | 643 | av_get_bits_per_sample(avctx->codec_id); | |
2983 | // set stream disposition based on audio service type | ||
2984 |
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 1960 times.
|
1960 | switch (avctx->audio_service_type) { |
2985 | ✗ | case AV_AUDIO_SERVICE_TYPE_EFFECTS: | |
2986 | ✗ | st->disposition = AV_DISPOSITION_CLEAN_EFFECTS; | |
2987 | ✗ | break; | |
2988 | ✗ | case AV_AUDIO_SERVICE_TYPE_VISUALLY_IMPAIRED: | |
2989 | ✗ | st->disposition = AV_DISPOSITION_VISUAL_IMPAIRED; | |
2990 | ✗ | break; | |
2991 | ✗ | case AV_AUDIO_SERVICE_TYPE_HEARING_IMPAIRED: | |
2992 | ✗ | st->disposition = AV_DISPOSITION_HEARING_IMPAIRED; | |
2993 | ✗ | break; | |
2994 | ✗ | case AV_AUDIO_SERVICE_TYPE_COMMENTARY: | |
2995 | ✗ | st->disposition = AV_DISPOSITION_COMMENT; | |
2996 | ✗ | break; | |
2997 | ✗ | case AV_AUDIO_SERVICE_TYPE_KARAOKE: | |
2998 | ✗ | st->disposition = AV_DISPOSITION_KARAOKE; | |
2999 | ✗ | break; | |
3000 | } | ||
3001 | } | ||
3002 | } | ||
3003 | |||
3004 |
1/2✓ Branch 0 taken 7534 times.
✗ Branch 1 not taken.
|
7534 | if (probesize) |
3005 | 7534 | estimate_timings(ic, old_offset); | |
3006 | |||
3007 | 7534 | av_opt_set_int(ic, "skip_clear", 0, AV_OPT_SEARCH_CHILDREN); | |
3008 | |||
3009 |
3/4✓ Branch 0 taken 6356 times.
✓ Branch 1 taken 1178 times.
✓ Branch 2 taken 6356 times.
✗ Branch 3 not taken.
|
7534 | if (ret >= 0 && ic->nb_streams) |
3010 | /* We could not have all the codec parameters before EOF. */ | ||
3011 | 6356 | ret = -1; | |
3012 |
2/2✓ Branch 0 taken 8343 times.
✓ Branch 1 taken 7534 times.
|
15877 | for (unsigned i = 0; i < ic->nb_streams; i++) { |
3013 | 8343 | AVStream *const st = ic->streams[i]; | |
3014 | 8343 | FFStream *const sti = ffstream(st); | |
3015 | const char *errmsg; | ||
3016 | |||
3017 | /* if no packet was ever seen, update context now for has_codec_parameters */ | ||
3018 |
2/2✓ Branch 0 taken 13 times.
✓ Branch 1 taken 8330 times.
|
8343 | if (!sti->avctx_inited) { |
3019 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 5 times.
|
13 | if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && |
3020 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | st->codecpar->format == AV_SAMPLE_FMT_NONE) |
3021 | 8 | st->codecpar->format = sti->avctx->sample_fmt; | |
3022 | 13 | ret = avcodec_parameters_to_context(sti->avctx, st->codecpar); | |
3023 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
|
13 | if (ret < 0) |
3024 | ✗ | goto find_stream_info_err; | |
3025 | } | ||
3026 |
2/2✓ Branch 1 taken 36 times.
✓ Branch 2 taken 8307 times.
|
8343 | if (!has_codec_parameters(st, &errmsg)) { |
3027 | char buf[256]; | ||
3028 | 36 | avcodec_string(buf, sizeof(buf), sti->avctx, 0); | |
3029 | 36 | av_log(ic, AV_LOG_WARNING, | |
3030 | "Could not find codec parameters for stream %d (%s): %s\n" | ||
3031 | "Consider increasing the value for the 'analyzeduration' (%"PRId64") and 'probesize' (%"PRId64") options\n", | ||
3032 | i, buf, errmsg, ic->max_analyze_duration, ic->probesize); | ||
3033 | } else { | ||
3034 | 8307 | ret = 0; | |
3035 | } | ||
3036 | } | ||
3037 | |||
3038 | 7534 | err = compute_chapters_end(ic); | |
3039 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7534 times.
|
7534 | if (err < 0) { |
3040 | ✗ | ret = err; | |
3041 | ✗ | goto find_stream_info_err; | |
3042 | } | ||
3043 | |||
3044 | /* update the stream parameters from the internal codec contexts */ | ||
3045 |
2/2✓ Branch 0 taken 8343 times.
✓ Branch 1 taken 7534 times.
|
15877 | for (unsigned i = 0; i < ic->nb_streams; i++) { |
3046 | 8343 | AVStream *const st = ic->streams[i]; | |
3047 | 8343 | FFStream *const sti = ffstream(st); | |
3048 | |||
3049 |
2/2✓ Branch 0 taken 8330 times.
✓ Branch 1 taken 13 times.
|
8343 | if (sti->avctx_inited) { |
3050 | 8330 | ret = avcodec_parameters_from_context(st->codecpar, sti->avctx); | |
3051 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8330 times.
|
8330 | if (ret < 0) |
3052 | ✗ | goto find_stream_info_err; | |
3053 | |||
3054 |
3/4✓ Branch 0 taken 8146 times.
✓ Branch 1 taken 184 times.
✓ Branch 2 taken 8146 times.
✗ Branch 3 not taken.
|
8330 | if (sti->avctx->rc_buffer_size > 0 || sti->avctx->rc_max_rate > 0 || |
3055 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8146 times.
|
8146 | sti->avctx->rc_min_rate) { |
3056 | size_t cpb_size; | ||
3057 | 184 | AVCPBProperties *props = av_cpb_properties_alloc(&cpb_size); | |
3058 |
1/2✓ Branch 0 taken 184 times.
✗ Branch 1 not taken.
|
184 | if (props) { |
3059 |
1/2✓ Branch 0 taken 184 times.
✗ Branch 1 not taken.
|
184 | if (sti->avctx->rc_buffer_size > 0) |
3060 | 184 | props->buffer_size = sti->avctx->rc_buffer_size; | |
3061 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 184 times.
|
184 | if (sti->avctx->rc_min_rate > 0) |
3062 | ✗ | props->min_bitrate = sti->avctx->rc_min_rate; | |
3063 |
2/2✓ Branch 0 taken 77 times.
✓ Branch 1 taken 107 times.
|
184 | if (sti->avctx->rc_max_rate > 0) |
3064 | 77 | props->max_bitrate = sti->avctx->rc_max_rate; | |
3065 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 184 times.
|
184 | if (!av_packet_side_data_add(&st->codecpar->coded_side_data, |
3066 | 184 | &st->codecpar->nb_coded_side_data, | |
3067 | AV_PKT_DATA_CPB_PROPERTIES, | ||
3068 | (uint8_t *)props, cpb_size, 0)) | ||
3069 | ✗ | av_free(props); | |
3070 | } | ||
3071 | } | ||
3072 | } | ||
3073 | |||
3074 | 8343 | sti->avctx_inited = 0; | |
3075 | #if FF_API_AVSTREAM_SIDE_DATA | ||
3076 | FF_DISABLE_DEPRECATION_WARNINGS | ||
3077 |
2/2✓ Branch 0 taken 478 times.
✓ Branch 1 taken 7865 times.
|
8343 | if (st->codecpar->nb_coded_side_data > 0) { |
3078 |
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); |
3079 | 478 | st->side_data = av_calloc(st->codecpar->nb_coded_side_data, sizeof(*st->side_data)); | |
3080 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 478 times.
|
478 | if (!st->side_data) { |
3081 | ✗ | ret = AVERROR(ENOMEM); | |
3082 | ✗ | goto find_stream_info_err; | |
3083 | } | ||
3084 | |||
3085 |
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++) { |
3086 | 509 | uint8_t *data = av_memdup(st->codecpar->coded_side_data[j].data, | |
3087 | 509 | st->codecpar->coded_side_data[j].size); | |
3088 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 509 times.
|
509 | if (!data) { |
3089 | ✗ | ret = AVERROR(ENOMEM); | |
3090 | ✗ | goto find_stream_info_err; | |
3091 | } | ||
3092 | 509 | st->side_data[j].type = st->codecpar->coded_side_data[j].type; | |
3093 | 509 | st->side_data[j].size = st->codecpar->coded_side_data[j].size; | |
3094 | 509 | st->side_data[j].data = data; | |
3095 | 509 | st->nb_side_data++; | |
3096 | } | ||
3097 | } | ||
3098 | FF_ENABLE_DEPRECATION_WARNINGS | ||
3099 | #endif | ||
3100 | } | ||
3101 | |||
3102 | 7534 | find_stream_info_err: | |
3103 |
2/2✓ Branch 0 taken 8343 times.
✓ Branch 1 taken 7534 times.
|
15877 | for (unsigned i = 0; i < ic->nb_streams; i++) { |
3104 | 8343 | AVStream *const st = ic->streams[i]; | |
3105 | 8343 | FFStream *const sti = ffstream(st); | |
3106 | int err; | ||
3107 | |||
3108 |
1/2✓ Branch 0 taken 8343 times.
✗ Branch 1 not taken.
|
8343 | if (sti->info) { |
3109 | 8343 | av_freep(&sti->info->duration_error); | |
3110 | 8343 | av_freep(&sti->info); | |
3111 | } | ||
3112 | |||
3113 |
2/2✓ Branch 1 taken 8235 times.
✓ Branch 2 taken 108 times.
|
8343 | if (avcodec_is_open(sti->avctx)) { |
3114 | 8235 | err = codec_close(sti); | |
3115 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 8235 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
8235 | if (err < 0 && ret >= 0) |
3116 | ✗ | ret = err; | |
3117 | } | ||
3118 | |||
3119 | 8343 | av_bsf_free(&sti->extract_extradata.bsf); | |
3120 | } | ||
3121 |
2/2✓ Branch 0 taken 4498 times.
✓ Branch 1 taken 3036 times.
|
7534 | if (ic->pb) { |
3122 | 4498 | FFIOContext *const ctx = ffiocontext(ic->pb); | |
3123 | 4498 | av_log(ic, AV_LOG_DEBUG, "After avformat_find_stream_info() pos: %"PRId64" bytes read:%"PRId64" seeks:%d frames:%d\n", | |
3124 | avio_tell(ic->pb), ctx->bytes_read, ctx->seek_count, count); | ||
3125 | } | ||
3126 | 7534 | return ret; | |
3127 | |||
3128 | ✗ | unref_then_goto_end: | |
3129 | ✗ | av_packet_unref(pkt1); | |
3130 | ✗ | goto find_stream_info_err; | |
3131 | } | ||
3132 |