FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/fftools/ffmpeg_demux.c
Date: 2026-04-23 02:20:26
Exec Total Coverage
Lines: 948 1316 72.0%
Functions: 39 43 90.7%
Branches: 567 928 61.1%

Line Branch Exec Source
1 /*
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include <float.h>
20 #include <stdint.h>
21
22 #include "ffmpeg.h"
23 #include "ffmpeg_sched.h"
24 #include "ffmpeg_utils.h"
25
26 #include "libavutil/avassert.h"
27 #include "libavutil/avstring.h"
28 #include "libavutil/display.h"
29 #include "libavutil/error.h"
30 #include "libavutil/intreadwrite.h"
31 #include "libavutil/mastering_display_metadata.h"
32 #include "libavutil/mem.h"
33 #include "libavutil/opt.h"
34 #include "libavutil/parseutils.h"
35 #include "libavutil/pixdesc.h"
36 #include "libavutil/time.h"
37 #include "libavutil/timestamp.h"
38
39 #include "libavcodec/bsf.h"
40 #include "libavcodec/packet.h"
41
42 #include "libavformat/avformat.h"
43
44 typedef struct DemuxStream {
45 InputStream ist;
46
47 // name used for logging
48 char log_name[32];
49
50 int sch_idx_stream;
51 int sch_idx_dec;
52
53 double ts_scale;
54
55 /* non zero if the packets must be decoded in 'raw_fifo', see DECODING_FOR_* */
56 int decoding_needed;
57 #define DECODING_FOR_OST 1
58 #define DECODING_FOR_FILTER 2
59
60 /* true if stream data should be discarded */
61 int discard;
62
63 // scheduler returned EOF for this stream
64 int finished;
65
66 int streamcopy_needed;
67 int have_sub2video;
68 int reinit_filters;
69 int autorotate;
70 int apply_cropping;
71 int force_display_matrix;
72 int force_mastering_display;
73 int force_content_light;
74 int drop_changed;
75
76
77 int wrap_correction_done;
78 int saw_first_ts;
79 /// dts of the first packet read for this stream (in AV_TIME_BASE units)
80 int64_t first_dts;
81
82 /* predicted dts of the next packet read for this stream or (when there are
83 * several frames in a packet) of the next frame in current packet (in AV_TIME_BASE units) */
84 int64_t next_dts;
85 /// dts of the last packet read for this stream (in AV_TIME_BASE units)
86 int64_t dts;
87
88 const AVCodecDescriptor *codec_desc;
89
90 AVDictionary *decoder_opts;
91 DecoderOpts dec_opts;
92 char dec_name[16];
93 // decoded media properties, as estimated by opening the decoder
94 AVFrame *decoded_params;
95
96 AVBSFContext *bsf;
97
98 /* number of packets successfully read for this stream */
99 uint64_t nb_packets;
100 // combined size of all the packets read
101 uint64_t data_size;
102 // latest wallclock time at which packet reading resumed after a stall - used for readrate
103 int64_t resume_wc;
104 // timestamp of first packet sent after the latest stall - used for readrate
105 int64_t resume_pts;
106 // measure of how far behind packet reading is against spceified readrate
107 int64_t lag;
108 } DemuxStream;
109
110 typedef struct DemuxStreamGroup {
111 InputStreamGroup istg;
112
113 // name used for logging
114 char log_name[32];
115 } DemuxStreamGroup;
116
117 typedef struct Demuxer {
118 InputFile f;
119
120 // name used for logging
121 char log_name[32];
122
123 int64_t wallclock_start;
124
125 /**
126 * Extra timestamp offset added by discontinuity handling.
127 */
128 int64_t ts_offset_discont;
129 int64_t last_ts;
130
131 int64_t recording_time;
132 int accurate_seek;
133
134 /* number of times input stream should be looped */
135 int loop;
136 int have_audio_dec;
137 /* duration of the looped segment of the input file */
138 Timestamp duration;
139 /* pts with the smallest/largest values ever seen */
140 Timestamp min_pts;
141 Timestamp max_pts;
142
143 /* number of streams that the user was warned of */
144 int nb_streams_warn;
145
146 float readrate;
147 double readrate_initial_burst;
148 float readrate_catchup;
149
150 Scheduler *sch;
151
152 AVPacket *pkt_heartbeat;
153
154 int read_started;
155 int nb_streams_used;
156 int nb_streams_finished;
157 } Demuxer;
158
159 typedef struct DemuxThreadContext {
160 // packet used for reading from the demuxer
161 AVPacket *pkt_demux;
162 // packet for reading from BSFs
163 AVPacket *pkt_bsf;
164 } DemuxThreadContext;
165
166 2041277 static DemuxStream *ds_from_ist(InputStream *ist)
167 {
168 2041277 return (DemuxStream*)ist;
169 }
170
171 22299 static Demuxer *demuxer_from_ifile(InputFile *f)
172 {
173 22299 return (Demuxer*)f;
174 }
175
176 114 InputStream *ist_find_unused(enum AVMediaType type)
177 {
178
1/2
✓ Branch 2 taken 136 times.
✗ Branch 3 not taken.
136 for (InputStream *ist = ist_iter(NULL); ist; ist = ist_iter(ist)) {
179 136 DemuxStream *ds = ds_from_ist(ist);
180
4/4
✓ Branch 0 taken 134 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 114 times.
✓ Branch 3 taken 20 times.
136 if (ist->par->codec_type == type && ds->discard &&
181
1/2
✓ Branch 0 taken 114 times.
✗ Branch 1 not taken.
114 ist->user_set_discard != AVDISCARD_ALL)
182 114 return ist;
183 }
184 return NULL;
185 }
186
187 10044 static void report_new_stream(Demuxer *d, const AVPacket *pkt)
188 {
189 10044 const AVStream *st = d->f.ctx->streams[pkt->stream_index];
190
191
1/2
✓ Branch 0 taken 10044 times.
✗ Branch 1 not taken.
10044 if (pkt->stream_index < d->nb_streams_warn)
192 10044 return;
193 av_log(d, AV_LOG_WARNING,
194 "New %s stream with index %d at pos:%"PRId64" and DTS:%ss\n",
195 av_get_media_type_string(st->codecpar->codec_type),
196 pkt->stream_index, pkt->pos, av_ts2timestr(pkt->dts, &st->time_base));
197 d->nb_streams_warn = pkt->stream_index + 1;
198 }
199
200 11 static int seek_to_start(Demuxer *d, Timestamp end_pts)
201 {
202 11 InputFile *ifile = &d->f;
203 11 AVFormatContext *is = ifile->ctx;
204 int ret;
205
206 11 ret = avformat_seek_file(is, -1, INT64_MIN, is->start_time, is->start_time, 0);
207
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 if (ret < 0)
208 return ret;
209
210
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 8 times.
11 if (end_pts.ts != AV_NOPTS_VALUE &&
211
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
6 (d->max_pts.ts == AV_NOPTS_VALUE ||
212 3 av_compare_ts(d->max_pts.ts, d->max_pts.tb, end_pts.ts, end_pts.tb) < 0))
213 3 d->max_pts = end_pts;
214
215
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 if (d->max_pts.ts != AV_NOPTS_VALUE) {
216
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 int64_t min_pts = d->min_pts.ts == AV_NOPTS_VALUE ? 0 : d->min_pts.ts;
217 11 d->duration.ts = d->max_pts.ts - av_rescale_q(min_pts, d->min_pts.tb, d->max_pts.tb);
218 }
219 11 d->duration.tb = d->max_pts.tb;
220
221
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 if (d->loop > 0)
222 11 d->loop--;
223
224 11 return ret;
225 }
226
227 457343 static void ts_discontinuity_detect(Demuxer *d, InputStream *ist,
228 AVPacket *pkt)
229 {
230 457343 InputFile *ifile = &d->f;
231 457343 DemuxStream *ds = ds_from_ist(ist);
232 457343 const int fmt_is_discont = ifile->ctx->iformat->flags & AVFMT_TS_DISCONT;
233 457343 int disable_discontinuity_correction = copy_ts;
234 457343 int64_t pkt_dts = av_rescale_q_rnd(pkt->dts, pkt->time_base, AV_TIME_BASE_Q,
235 AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX);
236
237
6/6
✓ Branch 0 taken 102 times.
✓ Branch 1 taken 457241 times.
✓ Branch 2 taken 99 times.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 73 times.
✓ Branch 5 taken 26 times.
457343 if (copy_ts && ds->next_dts != AV_NOPTS_VALUE &&
238
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 73 times.
73 fmt_is_discont && ist->st->pts_wrap_bits < 60) {
239 int64_t wrap_dts = av_rescale_q_rnd(pkt->dts + (1LL<<ist->st->pts_wrap_bits),
240 pkt->time_base, AV_TIME_BASE_Q,
241 AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
242 if (FFABS(wrap_dts - ds->next_dts) < FFABS(pkt_dts - ds->next_dts)/10)
243 disable_discontinuity_correction = 0;
244 }
245
246
4/4
✓ Branch 0 taken 450276 times.
✓ Branch 1 taken 7067 times.
✓ Branch 2 taken 450177 times.
✓ Branch 3 taken 99 times.
907520 if (ds->next_dts != AV_NOPTS_VALUE && !disable_discontinuity_correction) {
247 450177 int64_t delta = pkt_dts - ds->next_dts;
248
2/2
✓ Branch 0 taken 23637 times.
✓ Branch 1 taken 426540 times.
450177 if (fmt_is_discont) {
249
4/4
✓ Branch 0 taken 23581 times.
✓ Branch 1 taken 56 times.
✓ Branch 2 taken 23554 times.
✓ Branch 3 taken 83 times.
23637 if (FFABS(delta) > 1LL * dts_delta_threshold * AV_TIME_BASE ||
250
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 23546 times.
23554 pkt_dts + AV_TIME_BASE/10 < ds->dts) {
251 91 d->ts_offset_discont -= delta;
252 91 av_log(ist, AV_LOG_WARNING,
253 "timestamp discontinuity "
254 "(stream id=%d): %"PRId64", new offset= %"PRId64"\n",
255 91 ist->st->id, delta, d->ts_offset_discont);
256 91 pkt->dts -= av_rescale_q(delta, AV_TIME_BASE_Q, pkt->time_base);
257
1/2
✓ Branch 0 taken 91 times.
✗ Branch 1 not taken.
91 if (pkt->pts != AV_NOPTS_VALUE)
258 91 pkt->pts -= av_rescale_q(delta, AV_TIME_BASE_Q, pkt->time_base);
259 }
260 } else {
261
3/4
✓ Branch 0 taken 408145 times.
✓ Branch 1 taken 18395 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 426540 times.
426540 if (FFABS(delta) > 1LL * dts_error_threshold * AV_TIME_BASE) {
262 av_log(ist, AV_LOG_WARNING,
263 "DTS %"PRId64", next:%"PRId64" st:%d invalid dropping\n",
264 pkt->dts, ds->next_dts, pkt->stream_index);
265 pkt->dts = AV_NOPTS_VALUE;
266 }
267
2/2
✓ Branch 0 taken 424437 times.
✓ Branch 1 taken 2103 times.
426540 if (pkt->pts != AV_NOPTS_VALUE){
268 424437 int64_t pkt_pts = av_rescale_q(pkt->pts, pkt->time_base, AV_TIME_BASE_Q);
269 424437 delta = pkt_pts - ds->next_dts;
270
3/4
✓ Branch 0 taken 406444 times.
✓ Branch 1 taken 17993 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 424437 times.
424437 if (FFABS(delta) > 1LL * dts_error_threshold * AV_TIME_BASE) {
271 av_log(ist, AV_LOG_WARNING,
272 "PTS %"PRId64", next:%"PRId64" invalid dropping st:%d\n",
273 pkt->pts, ds->next_dts, pkt->stream_index);
274 pkt->pts = AV_NOPTS_VALUE;
275 }
276 }
277 }
278
6/6
✓ Branch 0 taken 7067 times.
✓ Branch 1 taken 99 times.
✓ Branch 2 taken 7064 times.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 128 times.
✓ Branch 5 taken 6936 times.
7166 } else if (ds->next_dts == AV_NOPTS_VALUE && !copy_ts &&
279
1/2
✓ Branch 0 taken 128 times.
✗ Branch 1 not taken.
128 fmt_is_discont && d->last_ts != AV_NOPTS_VALUE) {
280 128 int64_t delta = pkt_dts - d->last_ts;
281
3/4
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 62 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 128 times.
128 if (FFABS(delta) > 1LL * dts_delta_threshold * AV_TIME_BASE) {
282 d->ts_offset_discont -= delta;
283 av_log(ist, AV_LOG_DEBUG,
284 "Inter stream timestamp discontinuity %"PRId64", new offset= %"PRId64"\n",
285 delta, d->ts_offset_discont);
286 pkt->dts -= av_rescale_q(delta, AV_TIME_BASE_Q, pkt->time_base);
287 if (pkt->pts != AV_NOPTS_VALUE)
288 pkt->pts -= av_rescale_q(delta, AV_TIME_BASE_Q, pkt->time_base);
289 }
290 }
291
292 457343 d->last_ts = av_rescale_q(pkt->dts, pkt->time_base, AV_TIME_BASE_Q);
293 457343 }
294
295 505128 static void ts_discontinuity_process(Demuxer *d, InputStream *ist,
296 AVPacket *pkt)
297 {
298 505128 int64_t offset = av_rescale_q(d->ts_offset_discont, AV_TIME_BASE_Q,
299 pkt->time_base);
300
301 // apply previously-detected timestamp-discontinuity offset
302 // (to all streams, not just audio/video)
303
2/2
✓ Branch 0 taken 460563 times.
✓ Branch 1 taken 44565 times.
505128 if (pkt->dts != AV_NOPTS_VALUE)
304 460563 pkt->dts += offset;
305
2/2
✓ Branch 0 taken 458423 times.
✓ Branch 1 taken 46705 times.
505128 if (pkt->pts != AV_NOPTS_VALUE)
306 458423 pkt->pts += offset;
307
308 // detect timestamp discontinuities for audio/video
309
2/2
✓ Branch 0 taken 326282 times.
✓ Branch 1 taken 178846 times.
505128 if ((ist->par->codec_type == AVMEDIA_TYPE_VIDEO ||
310
2/2
✓ Branch 0 taken 323058 times.
✓ Branch 1 taken 3224 times.
326282 ist->par->codec_type == AVMEDIA_TYPE_AUDIO) &&
311
2/2
✓ Branch 0 taken 457343 times.
✓ Branch 1 taken 44561 times.
501904 pkt->dts != AV_NOPTS_VALUE)
312 457343 ts_discontinuity_detect(d, ist, pkt);
313 505128 }
314
315 505128 static int ist_dts_update(DemuxStream *ds, AVPacket *pkt, FrameData *fd)
316 {
317 505128 InputStream *ist = &ds->ist;
318 505128 const AVCodecParameters *par = ist->par;
319
320
2/2
✓ Branch 0 taken 7707 times.
✓ Branch 1 taken 497421 times.
505128 if (!ds->saw_first_ts) {
321 7707 ds->first_dts =
322
2/2
✓ Branch 0 taken 5346 times.
✓ Branch 1 taken 2361 times.
7707 ds->dts = ist->st->avg_frame_rate.num ? - ist->par->video_delay * AV_TIME_BASE / av_q2d(ist->st->avg_frame_rate) : 0;
323
2/2
✓ Branch 0 taken 7100 times.
✓ Branch 1 taken 607 times.
7707 if (pkt->pts != AV_NOPTS_VALUE) {
324 7100 ds->first_dts =
325 7100 ds->dts += av_rescale_q(pkt->pts, pkt->time_base, AV_TIME_BASE_Q);
326 }
327 7707 ds->saw_first_ts = 1;
328 }
329
330
2/2
✓ Branch 0 taken 7707 times.
✓ Branch 1 taken 497421 times.
505128 if (ds->next_dts == AV_NOPTS_VALUE)
331 7707 ds->next_dts = ds->dts;
332
333
2/2
✓ Branch 0 taken 460563 times.
✓ Branch 1 taken 44565 times.
505128 if (pkt->dts != AV_NOPTS_VALUE)
334 460563 ds->next_dts = ds->dts = av_rescale_q(pkt->dts, pkt->time_base, AV_TIME_BASE_Q);
335
336 505128 ds->dts = ds->next_dts;
337
3/3
✓ Branch 0 taken 323058 times.
✓ Branch 1 taken 178846 times.
✓ Branch 2 taken 3224 times.
505128 switch (par->codec_type) {
338 323058 case AVMEDIA_TYPE_AUDIO:
339 av_assert1(pkt->duration >= 0);
340
1/2
✓ Branch 0 taken 323058 times.
✗ Branch 1 not taken.
323058 if (par->sample_rate) {
341 323058 ds->next_dts += ((int64_t)AV_TIME_BASE * par->frame_size) /
342 323058 par->sample_rate;
343 } else {
344 ds->next_dts += av_rescale_q(pkt->duration, pkt->time_base, AV_TIME_BASE_Q);
345 }
346 323058 break;
347 178846 case AVMEDIA_TYPE_VIDEO:
348
2/2
✓ Branch 0 taken 567 times.
✓ Branch 1 taken 178279 times.
178846 if (ist->framerate.num) {
349 // TODO: Remove work-around for c99-to-c89 issue 7
350 567 AVRational time_base_q = AV_TIME_BASE_Q;
351 567 int64_t next_dts = av_rescale_q(ds->next_dts, time_base_q, av_inv_q(ist->framerate));
352 567 ds->next_dts = av_rescale_q(next_dts + 1, av_inv_q(ist->framerate), time_base_q);
353
2/2
✓ Branch 0 taken 175975 times.
✓ Branch 1 taken 2304 times.
178279 } else if (pkt->duration) {
354 175975 ds->next_dts += av_rescale_q(pkt->duration, pkt->time_base, AV_TIME_BASE_Q);
355
2/2
✓ Branch 0 taken 49 times.
✓ Branch 1 taken 2255 times.
2304 } else if (ist->par->framerate.num != 0) {
356 49 AVRational field_rate = av_mul_q(ist->par->framerate,
357 49 (AVRational){ 2, 1 });
358 49 int fields = 2;
359
360
1/2
✓ Branch 0 taken 49 times.
✗ Branch 1 not taken.
49 if (ds->codec_desc &&
361
4/4
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 25 times.
✓ Branch 2 taken 14 times.
✓ Branch 3 taken 10 times.
73 (ds->codec_desc->props & AV_CODEC_PROP_FIELDS) &&
362 24 av_stream_get_parser(ist->st))
363 14 fields = 1 + av_stream_get_parser(ist->st)->repeat_pict;
364
365 49 ds->next_dts += av_rescale_q(fields, av_inv_q(field_rate), AV_TIME_BASE_Q);
366 }
367 178846 break;
368 }
369
370 505128 fd->dts_est = ds->dts;
371
372 505128 return 0;
373 }
374
375 505128 static int ts_fixup(Demuxer *d, AVPacket *pkt, FrameData *fd)
376 {
377 505128 InputFile *ifile = &d->f;
378 505128 InputStream *ist = ifile->streams[pkt->stream_index];
379 505128 DemuxStream *ds = ds_from_ist(ist);
380 505128 const int64_t start_time = ifile->start_time_effective;
381 int64_t duration;
382 int ret;
383
384 505128 pkt->time_base = ist->st->time_base;
385
386 #define SHOW_TS_DEBUG(tag_) \
387 if (debug_ts) { \
388 av_log(ist, AV_LOG_INFO, "%s -> ist_index:%d:%d type:%s " \
389 "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s duration:%s duration_time:%s\n", \
390 tag_, ifile->index, pkt->stream_index, \
391 av_get_media_type_string(ist->st->codecpar->codec_type), \
392 av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &pkt->time_base), \
393 av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &pkt->time_base), \
394 av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &pkt->time_base)); \
395 }
396
397
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 505128 times.
505128 SHOW_TS_DEBUG("demuxer");
398
399
4/4
✓ Branch 0 taken 422112 times.
✓ Branch 1 taken 83016 times.
✓ Branch 2 taken 324914 times.
✓ Branch 3 taken 97198 times.
505128 if (!ds->wrap_correction_done && start_time != AV_NOPTS_VALUE &&
400
2/2
✓ Branch 0 taken 412 times.
✓ Branch 1 taken 324502 times.
324914 ist->st->pts_wrap_bits < 64) {
401 int64_t stime, stime2;
402
403 412 stime = av_rescale_q(start_time, AV_TIME_BASE_Q, pkt->time_base);
404 412 stime2= stime + (1ULL<<ist->st->pts_wrap_bits);
405 412 ds->wrap_correction_done = 1;
406
407
5/6
✓ Branch 0 taken 382 times.
✓ Branch 1 taken 30 times.
✓ Branch 2 taken 348 times.
✓ Branch 3 taken 34 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 348 times.
412 if(stime2 > stime && pkt->dts != AV_NOPTS_VALUE && pkt->dts > stime + (1LL<<(ist->st->pts_wrap_bits-1))) {
408 pkt->dts -= 1ULL<<ist->st->pts_wrap_bits;
409 ds->wrap_correction_done = 0;
410 }
411
5/6
✓ Branch 0 taken 382 times.
✓ Branch 1 taken 30 times.
✓ Branch 2 taken 342 times.
✓ Branch 3 taken 40 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 342 times.
412 if(stime2 > stime && pkt->pts != AV_NOPTS_VALUE && pkt->pts > stime + (1LL<<(ist->st->pts_wrap_bits-1))) {
412 pkt->pts -= 1ULL<<ist->st->pts_wrap_bits;
413 ds->wrap_correction_done = 0;
414 }
415 }
416
417
2/2
✓ Branch 0 taken 460563 times.
✓ Branch 1 taken 44565 times.
505128 if (pkt->dts != AV_NOPTS_VALUE)
418 460563 pkt->dts += av_rescale_q(ifile->ts_offset, AV_TIME_BASE_Q, pkt->time_base);
419
2/2
✓ Branch 0 taken 458423 times.
✓ Branch 1 taken 46705 times.
505128 if (pkt->pts != AV_NOPTS_VALUE)
420 458423 pkt->pts += av_rescale_q(ifile->ts_offset, AV_TIME_BASE_Q, pkt->time_base);
421
422
2/2
✓ Branch 0 taken 458423 times.
✓ Branch 1 taken 46705 times.
505128 if (pkt->pts != AV_NOPTS_VALUE)
423 458423 pkt->pts *= ds->ts_scale;
424
2/2
✓ Branch 0 taken 460563 times.
✓ Branch 1 taken 44565 times.
505128 if (pkt->dts != AV_NOPTS_VALUE)
425 460563 pkt->dts *= ds->ts_scale;
426
427 505128 duration = av_rescale_q(d->duration.ts, d->duration.tb, pkt->time_base);
428
2/2
✓ Branch 0 taken 458423 times.
✓ Branch 1 taken 46705 times.
505128 if (pkt->pts != AV_NOPTS_VALUE) {
429 // audio decoders take precedence for estimating total file duration
430
2/2
✓ Branch 0 taken 185949 times.
✓ Branch 1 taken 272474 times.
458423 int64_t pkt_duration = d->have_audio_dec ? 0 : pkt->duration;
431
432 458423 pkt->pts += duration;
433
434 // update max/min pts that will be used to compute total file duration
435 // when using -stream_loop
436
4/4
✓ Branch 0 taken 451536 times.
✓ Branch 1 taken 6887 times.
✓ Branch 2 taken 437073 times.
✓ Branch 3 taken 14463 times.
909959 if (d->max_pts.ts == AV_NOPTS_VALUE ||
437 451536 av_compare_ts(d->max_pts.ts, d->max_pts.tb,
438 451536 pkt->pts + pkt_duration, pkt->time_base) < 0) {
439 443960 d->max_pts = (Timestamp){ .ts = pkt->pts + pkt_duration,
440 443960 .tb = pkt->time_base };
441 }
442
4/4
✓ Branch 0 taken 451536 times.
✓ Branch 1 taken 6887 times.
✓ Branch 2 taken 28 times.
✓ Branch 3 taken 451508 times.
909959 if (d->min_pts.ts == AV_NOPTS_VALUE ||
443 451536 av_compare_ts(d->min_pts.ts, d->min_pts.tb,
444 pkt->pts, pkt->time_base) > 0) {
445 6915 d->min_pts = (Timestamp){ .ts = pkt->pts,
446 6915 .tb = pkt->time_base };
447 }
448 }
449
450
2/2
✓ Branch 0 taken 460563 times.
✓ Branch 1 taken 44565 times.
505128 if (pkt->dts != AV_NOPTS_VALUE)
451 460563 pkt->dts += duration;
452
453
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 505128 times.
505128 SHOW_TS_DEBUG("demuxer+tsfixup");
454
455 // detect and try to correct for timestamp discontinuities
456 505128 ts_discontinuity_process(d, ist, pkt);
457
458 // update estimated/predicted dts
459 505128 ret = ist_dts_update(ds, pkt, fd);
460
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 505128 times.
505128 if (ret < 0)
461 return ret;
462
463 505128 return 0;
464 }
465
466 505128 static int input_packet_process(Demuxer *d, AVPacket *pkt, unsigned *send_flags)
467 {
468 505128 InputFile *f = &d->f;
469 505128 InputStream *ist = f->streams[pkt->stream_index];
470 505128 DemuxStream *ds = ds_from_ist(ist);
471 FrameData *fd;
472 505128 int ret = 0;
473
474 505128 fd = packet_data(pkt);
475
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 505128 times.
505128 if (!fd)
476 return AVERROR(ENOMEM);
477
478 505128 ret = ts_fixup(d, pkt, fd);
479
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 505128 times.
505128 if (ret < 0)
480 return ret;
481
482
2/2
✓ Branch 0 taken 223 times.
✓ Branch 1 taken 504905 times.
505128 if (d->recording_time != INT64_MAX) {
483 223 int64_t start_time = 0;
484
2/2
✓ Branch 0 taken 74 times.
✓ Branch 1 taken 149 times.
223 if (copy_ts) {
485
1/2
✓ Branch 0 taken 74 times.
✗ Branch 1 not taken.
74 start_time += f->start_time != AV_NOPTS_VALUE ? f->start_time : 0;
486
1/2
✓ Branch 0 taken 74 times.
✗ Branch 1 not taken.
74 start_time += start_at_zero ? 0 : f->start_time_effective;
487 }
488
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 196 times.
223 if (ds->dts >= d->recording_time + start_time)
489 27 *send_flags |= DEMUX_SEND_STREAMCOPY_EOF;
490 }
491
492 505128 ds->data_size += pkt->size;
493 505128 ds->nb_packets++;
494
495 505128 fd->wallclock[LATENCY_PROBE_DEMUX] = av_gettime_relative();
496
497
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 505128 times.
505128 if (debug_ts) {
498 av_log(ist, AV_LOG_INFO, "demuxer+ffmpeg -> ist_index:%d:%d type:%s pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s duration:%s duration_time:%s off:%s off_time:%s\n",
499 f->index, pkt->stream_index,
500 av_get_media_type_string(ist->par->codec_type),
501 av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &pkt->time_base),
502 av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &pkt->time_base),
503 av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &pkt->time_base),
504 av_ts2str(f->ts_offset), av_ts2timestr(f->ts_offset, &AV_TIME_BASE_Q));
505 }
506
507 505128 return 0;
508 }
509
510 216 static void readrate_sleep(Demuxer *d)
511 {
512 216 InputFile *f = &d->f;
513 432 int64_t file_start = copy_ts * (
514
1/2
✓ Branch 0 taken 216 times.
✗ Branch 1 not taken.
216 (f->start_time_effective != AV_NOPTS_VALUE ? f->start_time_effective * !start_at_zero : 0) +
515
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 216 times.
216 (f->start_time != AV_NOPTS_VALUE ? f->start_time : 0)
516 );
517 216 int64_t initial_burst = AV_TIME_BASE * d->readrate_initial_burst;
518 216 int resume_warn = 0;
519
520
2/2
✓ Branch 0 taken 216 times.
✓ Branch 1 taken 216 times.
432 for (int i = 0; i < f->nb_streams; i++) {
521 216 InputStream *ist = f->streams[i];
522 216 DemuxStream *ds = ds_from_ist(ist);
523 int64_t stream_ts_offset, pts, now, wc_elapsed, elapsed, lag, max_pts, limit_pts;
524
525
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 216 times.
216 if (ds->discard) continue;
526
527
1/2
✓ Branch 0 taken 216 times.
✗ Branch 1 not taken.
216 stream_ts_offset = FFMAX(ds->first_dts != AV_NOPTS_VALUE ? ds->first_dts : 0, file_start);
528 216 pts = av_rescale(ds->dts, 1000000, AV_TIME_BASE);
529 216 now = av_gettime_relative();
530 216 wc_elapsed = now - d->wallclock_start;
531
532
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 194 times.
216 if (pts <= stream_ts_offset + initial_burst) continue;
533
534 194 max_pts = stream_ts_offset + initial_burst + (int64_t)(wc_elapsed * d->readrate);
535 194 lag = FFMAX(max_pts - pts, 0);
536
3/6
✓ Branch 0 taken 194 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 194 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 194 times.
194 if ( (!ds->lag && lag > 0.3 * AV_TIME_BASE) || ( lag > ds->lag + 0.3 * AV_TIME_BASE) ) {
537 ds->lag = lag;
538 ds->resume_wc = now;
539 ds->resume_pts = pts;
540 av_log_once(ds, AV_LOG_WARNING, AV_LOG_DEBUG, &resume_warn,
541 "Resumed reading at pts %0.3f with rate %0.3f after a lag of %0.3fs\n",
542 (float)pts/AV_TIME_BASE, d->readrate_catchup, (float)lag/AV_TIME_BASE);
543 }
544
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 194 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
194 if (ds->lag && !lag)
545 ds->lag = ds->resume_wc = ds->resume_pts = 0;
546
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 194 times.
194 if (ds->resume_wc) {
547 elapsed = now - ds->resume_wc;
548 limit_pts = ds->resume_pts + (int64_t)(elapsed * d->readrate_catchup);
549 } else {
550 194 elapsed = wc_elapsed;
551 194 limit_pts = max_pts;
552 }
553
554
2/2
✓ Branch 0 taken 192 times.
✓ Branch 1 taken 2 times.
194 if (pts > limit_pts)
555 192 av_usleep(pts - limit_pts);
556 }
557 216 }
558
559 506076 static int do_send(Demuxer *d, DemuxStream *ds, AVPacket *pkt, unsigned flags,
560 const char *pkt_desc)
561 {
562 int ret;
563
564 506076 pkt->stream_index = ds->sch_idx_stream;
565
566 506076 ret = sch_demux_send(d->sch, d->f.index, pkt, flags);
567
2/2
✓ Branch 0 taken 3223 times.
✓ Branch 1 taken 502853 times.
506076 if (ret == AVERROR_EOF) {
568 3223 av_packet_unref(pkt);
569
570 3223 av_log(ds, AV_LOG_VERBOSE, "All consumers of this stream are done\n");
571 3223 ds->finished = 1;
572
573
2/2
✓ Branch 0 taken 3186 times.
✓ Branch 1 taken 37 times.
3223 if (++d->nb_streams_finished == d->nb_streams_used) {
574 3186 av_log(d, AV_LOG_VERBOSE, "All consumers are done\n");
575 3186 return AVERROR_EOF;
576 }
577
2/2
✓ Branch 0 taken 401 times.
✓ Branch 1 taken 502452 times.
502853 } else if (ret < 0) {
578
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 401 times.
401 if (ret != AVERROR_EXIT)
579 av_log(d, AV_LOG_ERROR,
580 "Unable to send %s packet to consumers: %s\n",
581 pkt_desc, av_err2str(ret));
582 401 return ret;
583 }
584
585 502489 return 0;
586 }
587
588 505131 static int demux_send(Demuxer *d, DemuxThreadContext *dt, DemuxStream *ds,
589 AVPacket *pkt, unsigned flags)
590 {
591 505131 InputFile *f = &d->f;
592 int ret;
593
594 // pkt can be NULL only when flushing BSFs
595
3/4
✓ Branch 0 taken 505101 times.
✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 505101 times.
505131 av_assert0(ds->bsf || pkt);
596
597 // send heartbeat for sub2video streams
598
4/6
✓ Branch 0 taken 948 times.
✓ Branch 1 taken 504183 times.
✓ Branch 2 taken 948 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 948 times.
✗ Branch 5 not taken.
505131 if (d->pkt_heartbeat && pkt && pkt->pts != AV_NOPTS_VALUE) {
599
2/2
✓ Branch 0 taken 4332 times.
✓ Branch 1 taken 948 times.
5280 for (int i = 0; i < f->nb_streams; i++) {
600 4332 DemuxStream *ds1 = ds_from_ist(f->streams[i]);
601
602
3/4
✓ Branch 0 taken 4332 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3384 times.
✓ Branch 3 taken 948 times.
4332 if (ds1->finished || !ds1->have_sub2video)
603 3384 continue;
604
605 948 d->pkt_heartbeat->pts = pkt->pts;
606 948 d->pkt_heartbeat->time_base = pkt->time_base;
607 948 d->pkt_heartbeat->opaque = (void*)(intptr_t)PKT_OPAQUE_SUB_HEARTBEAT;
608
609 948 ret = do_send(d, ds1, d->pkt_heartbeat, 0, "heartbeat");
610
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 948 times.
948 if (ret < 0)
611 return ret;
612 }
613 }
614
615
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 505101 times.
505131 if (ds->bsf) {
616
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 3 times.
30 if (pkt)
617 27 av_packet_rescale_ts(pkt, pkt->time_base, ds->bsf->time_base_in);
618
619 30 ret = av_bsf_send_packet(ds->bsf, pkt);
620
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 if (ret < 0) {
621 if (pkt)
622 av_packet_unref(pkt);
623 av_log(ds, AV_LOG_ERROR, "Error submitting a packet for filtering: %s\n",
624 av_err2str(ret));
625 return ret;
626 }
627
628 while (1) {
629 56 ret = av_bsf_receive_packet(ds->bsf, dt->pkt_bsf);
630
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 30 times.
56 if (ret == AVERROR(EAGAIN))
631 26 return 0;
632
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 27 times.
30 else if (ret < 0) {
633
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret != AVERROR_EOF)
634 av_log(ds, AV_LOG_ERROR,
635 "Error applying bitstream filters to a packet: %s\n",
636 av_err2str(ret));
637 3 return ret;
638 }
639
640 27 dt->pkt_bsf->time_base = ds->bsf->time_base_out;
641
642 27 ret = do_send(d, ds, dt->pkt_bsf, 0, "filtered");
643
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 26 times.
27 if (ret < 0) {
644 1 av_packet_unref(dt->pkt_bsf);
645 1 return ret;
646 }
647 }
648 } else {
649 505101 ret = do_send(d, ds, pkt, flags, "demuxed");
650
2/2
✓ Branch 0 taken 3586 times.
✓ Branch 1 taken 501515 times.
505101 if (ret < 0)
651 3586 return ret;
652 }
653
654 501515 return 0;
655 }
656
657 3857 static int demux_bsf_flush(Demuxer *d, DemuxThreadContext *dt)
658 {
659 3857 InputFile *f = &d->f;
660 int ret;
661
662
2/2
✓ Branch 0 taken 4398 times.
✓ Branch 1 taken 3857 times.
8255 for (unsigned i = 0; i < f->nb_streams; i++) {
663 4398 DemuxStream *ds = ds_from_ist(f->streams[i]);
664
665
2/2
✓ Branch 0 taken 4395 times.
✓ Branch 1 taken 3 times.
4398 if (!ds->bsf)
666 4395 continue;
667
668 3 ret = demux_send(d, dt, ds, NULL, 0);
669
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
3 ret = (ret == AVERROR_EOF) ? 0 : (ret < 0) ? ret : AVERROR_BUG;
670
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0) {
671 av_log(ds, AV_LOG_ERROR, "Error flushing BSFs: %s\n",
672 av_err2str(ret));
673 return ret;
674 }
675
676 3 av_bsf_flush(ds->bsf);
677 }
678
679 3857 return 0;
680 }
681
682 7433 static void discard_unused_programs(InputFile *ifile)
683 {
684
2/2
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 7433 times.
7484 for (int j = 0; j < ifile->ctx->nb_programs; j++) {
685 51 AVProgram *p = ifile->ctx->programs[j];
686 51 int discard = AVDISCARD_ALL;
687
688
1/2
✓ Branch 0 taken 66 times.
✗ Branch 1 not taken.
66 for (int k = 0; k < p->nb_stream_indexes; k++) {
689 66 DemuxStream *ds = ds_from_ist(ifile->streams[p->stream_index[k]]);
690
691
2/2
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 15 times.
66 if (!ds->discard) {
692 51 discard = AVDISCARD_DEFAULT;
693 51 break;
694 }
695 }
696 51 p->discard = discard;
697 }
698 7433 }
699
700 7433 static void thread_set_name(InputFile *f)
701 {
702 char name[16];
703 7433 snprintf(name, sizeof(name), "dmx%d:%s", f->index, f->ctx->iformat->name);
704 7433 ff_thread_setname(name);
705 7433 }
706
707 7433 static void demux_thread_uninit(DemuxThreadContext *dt)
708 {
709 7433 av_packet_free(&dt->pkt_demux);
710 7433 av_packet_free(&dt->pkt_bsf);
711
712 7433 memset(dt, 0, sizeof(*dt));
713 7433 }
714
715 7433 static int demux_thread_init(DemuxThreadContext *dt)
716 {
717 7433 memset(dt, 0, sizeof(*dt));
718
719 7433 dt->pkt_demux = av_packet_alloc();
720
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7433 times.
7433 if (!dt->pkt_demux)
721 return AVERROR(ENOMEM);
722
723 7433 dt->pkt_bsf = av_packet_alloc();
724
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7433 times.
7433 if (!dt->pkt_bsf)
725 return AVERROR(ENOMEM);
726
727 7433 return 0;
728 }
729
730 7433 static int input_thread(void *arg)
731 {
732 7433 Demuxer *d = arg;
733 7433 InputFile *f = &d->f;
734
735 DemuxThreadContext dt;
736
737 7433 int ret = 0;
738
739 7433 ret = demux_thread_init(&dt);
740
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7433 times.
7433 if (ret < 0)
741 goto finish;
742
743 7433 thread_set_name(f);
744
745 7433 discard_unused_programs(f);
746
747 7433 d->read_started = 1;
748 7433 d->wallclock_start = av_gettime_relative();
749
750 511597 while (1) {
751 DemuxStream *ds;
752 519030 unsigned send_flags = 0;
753
754 519030 ret = av_read_frame(f->ctx, dt.pkt_demux);
755
756
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 519029 times.
519030 if (ret == AVERROR(EAGAIN)) {
757 1 av_usleep(10000);
758 10056 continue;
759 }
760
2/2
✓ Branch 0 taken 3857 times.
✓ Branch 1 taken 515172 times.
519029 if (ret < 0) {
761 int ret_bsf;
762
763
2/2
✓ Branch 0 taken 3802 times.
✓ Branch 1 taken 55 times.
3857 if (ret == AVERROR_EOF)
764 3802 av_log(d, AV_LOG_VERBOSE, "EOF while reading input\n");
765 else {
766 55 av_log(d, AV_LOG_ERROR, "Error during demuxing: %s\n",
767 55 av_err2str(ret));
768
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 55 times.
55 ret = exit_on_error ? ret : 0;
769 }
770
771 3857 ret_bsf = demux_bsf_flush(d, &dt);
772
2/2
✓ Branch 0 taken 55 times.
✓ Branch 1 taken 3802 times.
3857 ret = err_merge(ret == AVERROR_EOF ? 0 : ret, ret_bsf);
773
774
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 3846 times.
3857 if (d->loop) {
775 /* signal looping to our consumers */
776 11 dt.pkt_demux->stream_index = -1;
777 11 ret = sch_demux_send(d->sch, f->index, dt.pkt_demux, 0);
778
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 if (ret >= 0)
779 11 ret = seek_to_start(d, (Timestamp){ .ts = dt.pkt_demux->pts,
780 11 .tb = dt.pkt_demux->time_base });
781
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 if (ret >= 0)
782 11 continue;
783
784 /* fallthrough to the error path */
785 }
786
787 3846 break;
788 }
789
790
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 515172 times.
515172 if (do_pkt_dump) {
791 av_pkt_dump_log2(NULL, AV_LOG_INFO, dt.pkt_demux, do_hex_dump,
792 f->ctx->streams[dt.pkt_demux->stream_index]);
793 }
794
795 /* the following test is needed in case new streams appear
796 dynamically in stream : we ignore them */
797 1030344 ds = dt.pkt_demux->stream_index < f->nb_streams ?
798
1/2
✓ Branch 0 taken 515172 times.
✗ Branch 1 not taken.
515172 ds_from_ist(f->streams[dt.pkt_demux->stream_index]) : NULL;
799
5/6
✓ Branch 0 taken 515172 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 506832 times.
✓ Branch 3 taken 8340 times.
✓ Branch 4 taken 1704 times.
✓ Branch 5 taken 505128 times.
515172 if (!ds || ds->discard || ds->finished) {
800 10044 report_new_stream(d, dt.pkt_demux);
801 10044 av_packet_unref(dt.pkt_demux);
802 10044 continue;
803 }
804
805
2/2
✓ Branch 0 taken 101 times.
✓ Branch 1 taken 505027 times.
505128 if (dt.pkt_demux->flags & AV_PKT_FLAG_CORRUPT) {
806 101 av_log(d, exit_on_error ? AV_LOG_FATAL : AV_LOG_WARNING,
807 "corrupt input packet in stream %d\n",
808
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 101 times.
101 dt.pkt_demux->stream_index);
809
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 101 times.
101 if (exit_on_error) {
810 av_packet_unref(dt.pkt_demux);
811 ret = AVERROR_INVALIDDATA;
812 break;
813 }
814 }
815
816 505128 ret = input_packet_process(d, dt.pkt_demux, &send_flags);
817
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 505128 times.
505128 if (ret < 0)
818 break;
819
820
2/2
✓ Branch 0 taken 216 times.
✓ Branch 1 taken 504912 times.
505128 if (d->readrate)
821 216 readrate_sleep(d);
822
823 505128 ret = demux_send(d, &dt, ds, dt.pkt_demux, send_flags);
824
2/2
✓ Branch 0 taken 3587 times.
✓ Branch 1 taken 501541 times.
505128 if (ret < 0)
825 3587 break;
826 }
827
828 // EOF/EXIT is normal termination
829
4/4
✓ Branch 0 taken 4247 times.
✓ Branch 1 taken 3186 times.
✓ Branch 2 taken 3846 times.
✓ Branch 3 taken 401 times.
7433 if (ret == AVERROR_EOF || ret == AVERROR_EXIT)
830 3587 ret = 0;
831
832 3846 finish:
833 7433 demux_thread_uninit(&dt);
834
835 7433 return ret;
836 }
837
838 7433 static void demux_final_stats(Demuxer *d)
839 {
840 7433 InputFile *f = &d->f;
841 7433 uint64_t total_packets = 0, total_size = 0;
842
843 7433 av_log(f, AV_LOG_VERBOSE, "Input file #%d (%s):\n",
844 7433 f->index, f->ctx->url);
845
846
2/2
✓ Branch 0 taken 8039 times.
✓ Branch 1 taken 7433 times.
15472 for (int j = 0; j < f->nb_streams; j++) {
847 8039 InputStream *ist = f->streams[j];
848 8039 DemuxStream *ds = ds_from_ist(ist);
849 8039 enum AVMediaType type = ist->par->codec_type;
850
851
3/4
✓ Branch 0 taken 7728 times.
✓ Branch 1 taken 311 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 7728 times.
8039 if (ds->discard || type == AVMEDIA_TYPE_ATTACHMENT)
852 311 continue;
853
854 7728 total_size += ds->data_size;
855 7728 total_packets += ds->nb_packets;
856
857 7728 av_log(f, AV_LOG_VERBOSE, " Input stream #%d:%d (%s): ",
858 f->index, j, av_get_media_type_string(type));
859 7728 av_log(f, AV_LOG_VERBOSE, "%"PRIu64" packets read (%"PRIu64" bytes); ",
860 ds->nb_packets, ds->data_size);
861
862
2/2
✓ Branch 0 taken 6999 times.
✓ Branch 1 taken 729 times.
7728 if (ds->decoding_needed) {
863 6999 av_log(f, AV_LOG_VERBOSE,
864 "%"PRIu64" frames decoded; %"PRIu64" decode errors",
865 6999 ist->decoder->frames_decoded, ist->decoder->decode_errors);
866
2/2
✓ Branch 0 taken 1250 times.
✓ Branch 1 taken 5749 times.
6999 if (type == AVMEDIA_TYPE_AUDIO)
867 1250 av_log(f, AV_LOG_VERBOSE, " (%"PRIu64" samples)", ist->decoder->samples_decoded);
868 6999 av_log(f, AV_LOG_VERBOSE, "; ");
869 }
870
871 7728 av_log(f, AV_LOG_VERBOSE, "\n");
872 }
873
874 7433 av_log(f, AV_LOG_VERBOSE, " Total: %"PRIu64" packets (%"PRIu64" bytes) demuxed\n",
875 total_packets, total_size);
876 7433 }
877
878 8054 static void ist_free(InputStream **pist)
879 {
880 8054 InputStream *ist = *pist;
881 DemuxStream *ds;
882
883
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8054 times.
8054 if (!ist)
884 return;
885 8054 ds = ds_from_ist(ist);
886
887 8054 dec_free(&ist->decoder);
888
889 8054 av_dict_free(&ds->decoder_opts);
890 8054 av_freep(&ist->filters);
891 8054 av_freep(&ds->dec_opts.hwaccel_device);
892
893 8054 avcodec_parameters_free(&ist->par);
894
895 8054 av_frame_free(&ds->decoded_params);
896
897 8054 av_bsf_free(&ds->bsf);
898
899 8054 av_freep(pist);
900 }
901
902 47 static void istg_free(InputStreamGroup **pistg)
903 {
904 47 InputStreamGroup *istg = *pistg;
905
906
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 47 times.
47 if (!istg)
907 return;
908
909 47 av_freep(pistg);
910 }
911
912 7448 void ifile_close(InputFile **pf)
913 {
914 7448 InputFile *f = *pf;
915 7448 Demuxer *d = demuxer_from_ifile(f);
916
917
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7448 times.
7448 if (!f)
918 return;
919
920
2/2
✓ Branch 0 taken 7433 times.
✓ Branch 1 taken 15 times.
7448 if (d->read_started)
921 7433 demux_final_stats(d);
922
923
2/2
✓ Branch 0 taken 8054 times.
✓ Branch 1 taken 7448 times.
15502 for (int i = 0; i < f->nb_streams; i++)
924 8054 ist_free(&f->streams[i]);
925 7448 av_freep(&f->streams);
926
927
2/2
✓ Branch 0 taken 47 times.
✓ Branch 1 taken 7448 times.
7495 for (int i = 0; i < f->nb_stream_groups; i++)
928 47 istg_free(&f->stream_groups[i]);
929 7448 av_freep(&f->stream_groups);
930
931 7448 avformat_close_input(&f->ctx);
932
933 7448 av_packet_free(&d->pkt_heartbeat);
934
935 7448 av_freep(pf);
936 }
937
938 7824 int ist_use(InputStream *ist, int decoding_needed,
939 const ViewSpecifier *vs, SchedulerNode *src)
940 {
941 7824 Demuxer *d = demuxer_from_ifile(ist->file);
942 7824 DemuxStream *ds = ds_from_ist(ist);
943 int ret;
944
945
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7824 times.
7824 if (ist->user_set_discard == AVDISCARD_ALL) {
946 av_log(ist, AV_LOG_ERROR, "Cannot %s a disabled input stream\n",
947 decoding_needed ? "decode" : "streamcopy");
948 return AVERROR(EINVAL);
949 }
950
951
3/4
✓ Branch 0 taken 7069 times.
✓ Branch 1 taken 755 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 7069 times.
7824 if (decoding_needed && !ist->dec) {
952 av_log(ist, AV_LOG_ERROR,
953 "Decoding requested, but no decoder found for: %s\n",
954 avcodec_get_name(ist->par->codec_id));
955 return AVERROR(EINVAL);
956 }
957
958
2/2
✓ Branch 0 taken 7728 times.
✓ Branch 1 taken 96 times.
7824 if (ds->sch_idx_stream < 0) {
959 7728 ret = sch_add_demux_stream(d->sch, d->f.index);
960
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7728 times.
7728 if (ret < 0)
961 return ret;
962 7728 ds->sch_idx_stream = ret;
963 }
964
965
2/2
✓ Branch 0 taken 7728 times.
✓ Branch 1 taken 96 times.
7824 if (ds->discard) {
966 7728 ds->discard = 0;
967 7728 d->nb_streams_used++;
968 }
969
970 7824 ist->st->discard = ist->user_set_discard;
971 7824 ds->decoding_needed |= decoding_needed;
972 7824 ds->streamcopy_needed |= !decoding_needed;
973
974
4/4
✓ Branch 0 taken 7069 times.
✓ Branch 1 taken 755 times.
✓ Branch 2 taken 6999 times.
✓ Branch 3 taken 70 times.
7824 if (decoding_needed && ds->sch_idx_dec < 0) {
975 6999 int is_audio = ist->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO;
976 6999 int is_unreliable = !!(d->f.ctx->iformat->flags & AVFMT_NOTIMESTAMPS);
977 int64_t use_wallclock_as_timestamps;
978
979 6999 ret = av_opt_get_int(d->f.ctx, "use_wallclock_as_timestamps", 0, &use_wallclock_as_timestamps);
980
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6999 times.
6999 if (ret < 0)
981 return ret;
982
983
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6999 times.
6999 if (use_wallclock_as_timestamps)
984 is_unreliable = 0;
985
986 20997 ds->dec_opts.flags |= (!!ist->fix_sub_duration * DECODER_FLAG_FIX_SUB_DURATION) |
987
2/2
✓ Branch 0 taken 596 times.
✓ Branch 1 taken 6403 times.
6999 (!!is_unreliable * DECODER_FLAG_TS_UNRELIABLE) |
988
4/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6997 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
6999 (!!(d->loop && is_audio) * DECODER_FLAG_SEND_END_TS)
989 #if FFMPEG_OPT_TOP
990
2/2
✓ Branch 0 taken 1294 times.
✓ Branch 1 taken 5705 times.
6999 | ((ist->top_field_first >= 0) * DECODER_FLAG_TOP_FIELD_FIRST)
991 #endif
992 ;
993
994
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 6967 times.
6999 if (ist->framerate.num) {
995 32 ds->dec_opts.flags |= DECODER_FLAG_FRAMERATE_FORCED;
996 32 ds->dec_opts.framerate = ist->framerate;
997 } else
998 6967 ds->dec_opts.framerate = ist->st->avg_frame_rate;
999
1000
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6998 times.
6999 if (ist->dec->id == AV_CODEC_ID_DVB_SUBTITLE &&
1001
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 (ds->decoding_needed & DECODING_FOR_OST)) {
1002 1 av_dict_set(&ds->decoder_opts, "compute_edt", "1", AV_DICT_DONT_OVERWRITE);
1003
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ds->decoding_needed & DECODING_FOR_FILTER)
1004 av_log(ist, AV_LOG_WARNING,
1005 "Warning using DVB subtitles for filtering and output at the "
1006 "same time is not fully supported, also see -compute_edt [0|1]\n");
1007 }
1008
1009 6999 snprintf(ds->dec_name, sizeof(ds->dec_name), "%d:%d", ist->file->index, ist->index);
1010 6999 ds->dec_opts.name = ds->dec_name;
1011
1012 6999 ds->dec_opts.codec = ist->dec;
1013 6999 ds->dec_opts.par = ist->par;
1014
1015 6999 ds->dec_opts.log_parent = ist;
1016
1017 6999 ds->decoded_params = av_frame_alloc();
1018
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6999 times.
6999 if (!ds->decoded_params)
1019 return AVERROR(ENOMEM);
1020
1021 6999 ret = dec_init(&ist->decoder, d->sch,
1022 6999 &ds->decoder_opts, &ds->dec_opts, ds->decoded_params);
1023
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6999 times.
6999 if (ret < 0)
1024 return ret;
1025 6999 ds->sch_idx_dec = ret;
1026
1027 6999 ret = sch_connect(d->sch, SCH_DSTREAM(d->f.index, ds->sch_idx_stream),
1028 6999 SCH_DEC_IN(ds->sch_idx_dec));
1029
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6999 times.
6999 if (ret < 0)
1030 return ret;
1031
1032 6999 d->have_audio_dec |= is_audio;
1033 }
1034
1035
4/4
✓ Branch 0 taken 7069 times.
✓ Branch 1 taken 755 times.
✓ Branch 2 taken 5715 times.
✓ Branch 3 taken 1354 times.
7824 if (decoding_needed && ist->par->codec_type == AVMEDIA_TYPE_VIDEO) {
1036 5715 ret = dec_request_view(ist->decoder, vs, src);
1037
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5715 times.
5715 if (ret < 0)
1038 return ret;
1039 } else {
1040 2109 *src = decoding_needed ?
1041
2/2
✓ Branch 0 taken 1354 times.
✓ Branch 1 taken 755 times.
2109 SCH_DEC_OUT(ds->sch_idx_dec, 0) :
1042 755 SCH_DSTREAM(d->f.index, ds->sch_idx_stream);
1043 }
1044
1045 7824 return 0;
1046 }
1047
1048 7027 int ist_filter_add(InputStream *ist, InputFilter *ifilter, int is_simple,
1049 const ViewSpecifier *vs, InputFilterOptions *opts,
1050 SchedulerNode *src)
1051 {
1052 7027 Demuxer *d = demuxer_from_ifile(ist->file);
1053 7027 DemuxStream *ds = ds_from_ist(ist);
1054 7027 int64_t tsoffset = 0;
1055 int ret;
1056
1057
2/2
✓ Branch 0 taken 6854 times.
✓ Branch 1 taken 173 times.
7027 ret = ist_use(ist, is_simple ? DECODING_FOR_OST : DECODING_FOR_FILTER,
1058 vs, src);
1059
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7027 times.
7027 if (ret < 0)
1060 return ret;
1061
1062 7027 ret = GROW_ARRAY(ist->filters, ist->nb_filters);
1063
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7027 times.
7027 if (ret < 0)
1064 return ret;
1065
1066 7027 ist->filters[ist->nb_filters - 1] = ifilter;
1067
1068
2/2
✓ Branch 0 taken 5715 times.
✓ Branch 1 taken 1312 times.
7027 if (ist->par->codec_type == AVMEDIA_TYPE_VIDEO) {
1069 5715 const AVPacketSideData *sd = av_packet_side_data_get(ist->par->coded_side_data,
1070 5715 ist->par->nb_coded_side_data,
1071 AV_PKT_DATA_FRAME_CROPPING);
1072
3/4
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 5683 times.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
5715 if (ist->framerate.num > 0 && ist->framerate.den > 0) {
1073 32 opts->framerate = ist->framerate;
1074 32 opts->flags |= IFILTER_FLAG_CFR;
1075 } else
1076 5683 opts->framerate = av_guess_frame_rate(d->f.ctx, ist->st, NULL);
1077
3/4
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 5708 times.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
5715 if (sd && sd->size >= sizeof(uint32_t) * 4) {
1078 7 opts->crop_top = AV_RL32(sd->data + 0);
1079 7 opts->crop_bottom = AV_RL32(sd->data + 4);
1080 7 opts->crop_left = AV_RL32(sd->data + 8);
1081 7 opts->crop_right = AV_RL32(sd->data + 12);
1082
3/4
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
7 if (ds->apply_cropping && ds->apply_cropping != CROP_CODEC &&
1083
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 (opts->crop_top | opts->crop_bottom | opts->crop_left | opts->crop_right))
1084 6 opts->flags |= IFILTER_FLAG_CROP;
1085 }
1086
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1308 times.
1312 } else if (ist->par->codec_type == AVMEDIA_TYPE_SUBTITLE) {
1087 /* Compute the size of the canvas for the subtitles stream.
1088 If the subtitles codecpar has set a size, use it. Otherwise use the
1089 maximum dimensions of the video streams in the same file. */
1090 4 opts->sub2video_width = ist->par->width;
1091 4 opts->sub2video_height = ist->par->height;
1092
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
4 if (!(opts->sub2video_width && opts->sub2video_height)) {
1093 for (int j = 0; j < d->f.nb_streams; j++) {
1094 AVCodecParameters *par1 = d->f.streams[j]->par;
1095 if (par1->codec_type == AVMEDIA_TYPE_VIDEO) {
1096 opts->sub2video_width = FFMAX(opts->sub2video_width, par1->width);
1097 opts->sub2video_height = FFMAX(opts->sub2video_height, par1->height);
1098 }
1099 }
1100 }
1101
1102
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
4 if (!(opts->sub2video_width && opts->sub2video_height)) {
1103 opts->sub2video_width = FFMAX(opts->sub2video_width, 720);
1104 opts->sub2video_height = FFMAX(opts->sub2video_height, 576);
1105 }
1106
1107
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (!d->pkt_heartbeat) {
1108 4 d->pkt_heartbeat = av_packet_alloc();
1109
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!d->pkt_heartbeat)
1110 return AVERROR(ENOMEM);
1111 }
1112 4 ds->have_sub2video = 1;
1113 }
1114
1115 7027 ret = av_frame_copy_props(opts->fallback, ds->decoded_params);
1116
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7027 times.
7027 if (ret < 0)
1117 return ret;
1118 7027 opts->fallback->format = ds->decoded_params->format;
1119 7027 opts->fallback->width = ds->decoded_params->width;
1120 7027 opts->fallback->height = ds->decoded_params->height;
1121
1122 7027 ret = av_channel_layout_copy(&opts->fallback->ch_layout, &ds->decoded_params->ch_layout);
1123
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7027 times.
7027 if (ret < 0)
1124 return ret;
1125
1126
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 7023 times.
7027 if (copy_ts) {
1127
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
4 tsoffset = d->f.start_time == AV_NOPTS_VALUE ? 0 : d->f.start_time;
1128
3/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 1 times.
4 if (!start_at_zero && d->f.ctx->start_time != AV_NOPTS_VALUE)
1129 3 tsoffset += d->f.ctx->start_time;
1130 }
1131
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 1 times.
23 opts->trim_start_us = ((d->f.start_time == AV_NOPTS_VALUE) || !d->accurate_seek) ?
1132
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 7004 times.
7050 AV_NOPTS_VALUE : tsoffset;
1133 7027 opts->trim_end_us = d->recording_time;
1134
1135 7027 opts->name = av_strdup(ds->dec_name);
1136
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7027 times.
7027 if (!opts->name)
1137 return AVERROR(ENOMEM);
1138
1139 21081 opts->flags |= IFILTER_FLAG_AUTOROTATE * !!(ds->autorotate) |
1140
1/2
✓ Branch 0 taken 7027 times.
✗ Branch 1 not taken.
7027 IFILTER_FLAG_REINIT * !!(ds->reinit_filters) |
1141
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7027 times.
7027 IFILTER_FLAG_DROPCHANGED* !!(ds->drop_changed);
1142
1143 7027 return 0;
1144 }
1145
1146 15956 static int choose_decoder(const OptionsContext *o, void *logctx,
1147 AVFormatContext *s, AVStream *st,
1148 enum HWAccelID hwaccel_id, enum AVHWDeviceType hwaccel_device_type,
1149 const AVCodec **pcodec)
1150
1151 {
1152 15956 const char *codec_name = NULL;
1153
1154 15956 opt_match_per_stream_str(logctx, &o->codec_names, s, st, &codec_name);
1155
2/2
✓ Branch 0 taken 6382 times.
✓ Branch 1 taken 9574 times.
15956 if (codec_name) {
1156 6382 int ret = find_codec(NULL, codec_name, st->codecpar->codec_type, 0, pcodec);
1157
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6382 times.
6382 if (ret < 0)
1158 return ret;
1159 6382 st->codecpar->codec_id = (*pcodec)->id;
1160
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 6382 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
6382 if (recast_media && st->codecpar->codec_type != (*pcodec)->type)
1161 st->codecpar->codec_type = (*pcodec)->type;
1162 6382 return 0;
1163 } else {
1164
3/4
✓ Branch 0 taken 5899 times.
✓ Branch 1 taken 3675 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 5899 times.
9574 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
1165 hwaccel_id == HWACCEL_GENERIC &&
1166 hwaccel_device_type != AV_HWDEVICE_TYPE_NONE) {
1167 const AVCodec *c;
1168 void *i = NULL;
1169
1170 while ((c = av_codec_iterate(&i))) {
1171 const AVCodecHWConfig *config;
1172
1173 if (c->id != st->codecpar->codec_id ||
1174 !av_codec_is_decoder(c))
1175 continue;
1176
1177 for (int j = 0; config = avcodec_get_hw_config(c, j); j++) {
1178 if (config->device_type == hwaccel_device_type) {
1179 av_log(logctx, AV_LOG_VERBOSE, "Selecting decoder '%s' because of requested hwaccel method %s\n",
1180 c->name, av_hwdevice_get_type_name(hwaccel_device_type));
1181 *pcodec = c;
1182 return 0;
1183 }
1184 }
1185 }
1186 }
1187
1188 9574 *pcodec = avcodec_find_decoder(st->codecpar->codec_id);
1189 9574 return 0;
1190 }
1191 }
1192
1193 1756 static int guess_input_channel_layout(InputStream *ist, AVCodecParameters *par,
1194 int guess_layout_max)
1195 {
1196
2/2
✓ Branch 0 taken 805 times.
✓ Branch 1 taken 951 times.
1756 if (par->ch_layout.order == AV_CHANNEL_ORDER_UNSPEC) {
1197 char layout_name[256];
1198
1199
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 801 times.
805 if (par->ch_layout.nb_channels > guess_layout_max)
1200 14 return 0;
1201 801 av_channel_layout_default(&par->ch_layout, par->ch_layout.nb_channels);
1202
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 791 times.
801 if (par->ch_layout.order == AV_CHANNEL_ORDER_UNSPEC)
1203 10 return 0;
1204 791 av_channel_layout_describe(&par->ch_layout, layout_name, sizeof(layout_name));
1205 791 av_log(ist, AV_LOG_WARNING, "Guessed Channel Layout: %s\n", layout_name);
1206 }
1207 1742 return 1;
1208 }
1209
1210 6138 static int add_display_matrix_to_stream(const OptionsContext *o,
1211 AVFormatContext *ctx, InputStream *ist)
1212 {
1213 6138 AVStream *st = ist->st;
1214 6138 DemuxStream *ds = ds_from_ist(ist);
1215 AVPacketSideData *sd;
1216 6138 double rotation = DBL_MAX;
1217 6138 int hflip = -1, vflip = -1;
1218 6138 int hflip_set = 0, vflip_set = 0, rotation_set = 0;
1219 int32_t *buf;
1220
1221 6138 opt_match_per_stream_dbl(ist, &o->display_rotations, ctx, st, &rotation);
1222 6138 opt_match_per_stream_int(ist, &o->display_hflips, ctx, st, &hflip);
1223 6138 opt_match_per_stream_int(ist, &o->display_vflips, ctx, st, &vflip);
1224
1225 6138 rotation_set = rotation != DBL_MAX;
1226 6138 hflip_set = hflip != -1;
1227 6138 vflip_set = vflip != -1;
1228
1229
4/6
✓ Branch 0 taken 6136 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 6136 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6136 times.
✗ Branch 5 not taken.
6138 if (!rotation_set && !hflip_set && !vflip_set)
1230 6136 return 0;
1231
1232 2 sd = av_packet_side_data_new(&st->codecpar->coded_side_data,
1233 2 &st->codecpar->nb_coded_side_data,
1234 AV_PKT_DATA_DISPLAYMATRIX,
1235 sizeof(int32_t) * 9, 0);
1236
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!sd) {
1237 av_log(ist, AV_LOG_FATAL, "Failed to generate a display matrix!\n");
1238 return AVERROR(ENOMEM);
1239 }
1240
1241 2 buf = (int32_t *)sd->data;
1242
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
4 av_display_rotation_set(buf,
1243 2 rotation_set ? -(rotation) : -0.0f);
1244
1245
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 av_display_matrix_flip(buf,
1246 hflip_set ? hflip : 0,
1247 vflip_set ? vflip : 0);
1248
1249 2 ds->force_display_matrix = 1;
1250
1251 2 return 0;
1252 }
1253
1254 6138 static int add_mastering_display_to_stream(const OptionsContext *o,
1255 AVFormatContext *ctx, InputStream *ist)
1256 {
1257 6138 AVStream *st = ist->st;
1258 6138 DemuxStream *ds = ds_from_ist(ist);
1259 AVMasteringDisplayMetadata *master_display;
1260 AVPacketSideData *sd;
1261 6138 const char *p = NULL;
1262 6138 const int chroma_den = 50000;
1263 6138 const int luma_den = 10000;
1264 size_t size;
1265 int ret;
1266
1267 6138 opt_match_per_stream_str(ist, &o->mastering_displays, ctx, st, &p);
1268
1269
1/2
✓ Branch 0 taken 6138 times.
✗ Branch 1 not taken.
6138 if (!p)
1270 6138 return 0;
1271
1272 master_display = av_mastering_display_metadata_alloc_size(&size);
1273 if (!master_display)
1274 return AVERROR(ENOMEM);
1275
1276 ret = sscanf(p,
1277 "G(%u,%u)B(%u,%u)R(%u,%u)WP(%u,%u)L(%u,%u)",
1278 (unsigned*)&master_display->display_primaries[1][0].num,
1279 (unsigned*)&master_display->display_primaries[1][1].num,
1280 (unsigned*)&master_display->display_primaries[2][0].num,
1281 (unsigned*)&master_display->display_primaries[2][1].num,
1282 (unsigned*)&master_display->display_primaries[0][0].num,
1283 (unsigned*)&master_display->display_primaries[0][1].num,
1284 (unsigned*)&master_display->white_point[0].num,
1285 (unsigned*)&master_display->white_point[1].num,
1286 (unsigned*)&master_display->max_luminance.num,
1287 (unsigned*)&master_display->min_luminance.num);
1288
1289 if (ret != 10 ||
1290 (unsigned)(master_display->display_primaries[1][0].num | master_display->display_primaries[1][1].num |
1291 master_display->display_primaries[2][0].num | master_display->display_primaries[2][1].num |
1292 master_display->display_primaries[0][0].num | master_display->display_primaries[0][1].num |
1293 master_display->white_point[0].num | master_display->white_point[1].num) > UINT16_MAX ||
1294 (unsigned)(master_display->max_luminance.num | master_display->min_luminance.num) > INT_MAX ||
1295 master_display->min_luminance.num > master_display->max_luminance.num) {
1296 av_freep(&master_display);
1297 av_log(ist, AV_LOG_ERROR, "Failed to parse mastering display option\n");
1298 return AVERROR(EINVAL);
1299 }
1300
1301 master_display->display_primaries[1][0].den = chroma_den;
1302 master_display->display_primaries[1][1].den = chroma_den;
1303 master_display->display_primaries[2][0].den = chroma_den;
1304 master_display->display_primaries[2][1].den = chroma_den;
1305 master_display->display_primaries[0][0].den = chroma_den;
1306 master_display->display_primaries[0][1].den = chroma_den;
1307 master_display->white_point[0].den = chroma_den;
1308 master_display->white_point[1].den = chroma_den;
1309 master_display->max_luminance.den = luma_den;
1310 master_display->min_luminance.den = luma_den;
1311
1312 master_display->has_primaries = 1;
1313 master_display->has_luminance = 1;
1314
1315 sd = av_packet_side_data_add(&st->codecpar->coded_side_data,
1316 &st->codecpar->nb_coded_side_data,
1317 AV_PKT_DATA_MASTERING_DISPLAY_METADATA,
1318 (uint8_t *)master_display, size, 0);
1319 if (!sd) {
1320 av_freep(&master_display);
1321 return AVERROR(ENOMEM);
1322 }
1323
1324 ds->force_mastering_display = 1;
1325
1326 return 0;
1327 }
1328
1329 6138 static int add_content_light_to_stream(const OptionsContext *o,
1330 AVFormatContext *ctx, InputStream *ist)
1331 {
1332 6138 AVStream *st = ist->st;
1333 6138 DemuxStream *ds = ds_from_ist(ist);
1334 AVContentLightMetadata *cll;
1335 AVPacketSideData *sd;
1336 6138 const char *p = NULL;
1337 size_t size;
1338 int ret;
1339
1340 6138 opt_match_per_stream_str(ist, &o->content_lights, ctx, st, &p);
1341
1342
1/2
✓ Branch 0 taken 6138 times.
✗ Branch 1 not taken.
6138 if (!p)
1343 6138 return 0;
1344
1345 cll = av_content_light_metadata_alloc(&size);
1346 if (!cll)
1347 return AVERROR(ENOMEM);
1348
1349 ret = sscanf(p, "%u,%u",
1350 (unsigned*)&cll->MaxCLL,
1351 (unsigned*)&cll->MaxFALL);
1352
1353 if (ret != 2 || (unsigned)(cll->MaxCLL | cll->MaxFALL) > UINT16_MAX) {
1354 av_freep(&cll);
1355 av_log(ist, AV_LOG_ERROR, "Failed to parse content light option\n");
1356 return AVERROR(EINVAL);
1357 }
1358
1359 sd = av_packet_side_data_add(&st->codecpar->coded_side_data,
1360 &st->codecpar->nb_coded_side_data,
1361 AV_PKT_DATA_CONTENT_LIGHT_LEVEL,
1362 (uint8_t *)cll, size, 0);
1363 if (!sd) {
1364 av_freep(&cll);
1365 return AVERROR(ENOMEM);
1366 }
1367
1368 ds->force_content_light = 1;
1369
1370 return 0;
1371 }
1372
1373 1458 static const char *input_stream_item_name(void *obj)
1374 {
1375 1458 const DemuxStream *ds = obj;
1376
1377 1458 return ds->log_name;
1378 }
1379
1380 static const AVClass input_stream_class = {
1381 .class_name = "InputStream",
1382 .version = LIBAVUTIL_VERSION_INT,
1383 .item_name = input_stream_item_name,
1384 .category = AV_CLASS_CATEGORY_DEMUXER,
1385 };
1386
1387 8054 static DemuxStream *demux_stream_alloc(Demuxer *d, AVStream *st)
1388 {
1389 8054 const char *type_str = av_get_media_type_string(st->codecpar->codec_type);
1390 8054 InputFile *f = &d->f;
1391 DemuxStream *ds;
1392
1393 8054 ds = allocate_array_elem(&f->streams, sizeof(*ds), &f->nb_streams);
1394
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8054 times.
8054 if (!ds)
1395 return NULL;
1396
1397 8054 ds->sch_idx_stream = -1;
1398 8054 ds->sch_idx_dec = -1;
1399
1400 8054 ds->ist.st = st;
1401 8054 ds->ist.file = f;
1402 8054 ds->ist.index = st->index;
1403 8054 ds->ist.class = &input_stream_class;
1404
1405
1/2
✓ Branch 0 taken 8054 times.
✗ Branch 1 not taken.
8054 snprintf(ds->log_name, sizeof(ds->log_name), "%cist#%d:%d/%s",
1406 8054 type_str ? *type_str : '?', d->f.index, st->index,
1407 8054 avcodec_get_name(st->codecpar->codec_id));
1408
1409 8054 return ds;
1410 }
1411
1412 8054 static int ist_add(const OptionsContext *o, Demuxer *d, AVStream *st, AVDictionary **opts_used)
1413 {
1414 8054 AVFormatContext *ic = d->f.ctx;
1415 8054 AVCodecParameters *par = st->codecpar;
1416 DemuxStream *ds;
1417 InputStream *ist;
1418 8054 const char *framerate = NULL, *hwaccel_device = NULL;
1419 8054 const char *hwaccel = NULL;
1420 8054 const char *apply_cropping = NULL;
1421 8054 const char *hwaccel_output_format = NULL;
1422 8054 const char *codec_tag = NULL;
1423 8054 const char *bsfs = NULL;
1424 char *next;
1425 8054 const char *discard_str = NULL;
1426 AVBPrint bp;
1427 int ret;
1428
1429 8054 ds = demux_stream_alloc(d, st);
1430
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8054 times.
8054 if (!ds)
1431 return AVERROR(ENOMEM);
1432
1433 8054 ist = &ds->ist;
1434
1435 8054 ds->discard = 1;
1436 8054 st->discard = AVDISCARD_ALL;
1437 8054 ds->first_dts = AV_NOPTS_VALUE;
1438 8054 ds->next_dts = AV_NOPTS_VALUE;
1439
1440 8054 ds->dec_opts.time_base = st->time_base;
1441
1442 8054 ds->ts_scale = 1.0;
1443 8054 opt_match_per_stream_dbl(ist, &o->ts_scale, ic, st, &ds->ts_scale);
1444
1445 8054 ds->autorotate = 1;
1446 8054 opt_match_per_stream_int(ist, &o->autorotate, ic, st, &ds->autorotate);
1447
1448 8054 ds->apply_cropping = CROP_ALL;
1449 8054 opt_match_per_stream_str(ist, &o->apply_cropping, ic, st, &apply_cropping);
1450
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 8053 times.
8054 if (apply_cropping) {
1451 1 const AVOption opts[] = {
1452 { "apply_cropping", NULL, 0, AV_OPT_TYPE_INT,
1453 { .i64 = CROP_ALL }, CROP_DISABLED, CROP_CONTAINER, AV_OPT_FLAG_DECODING_PARAM, .unit = "apply_cropping" },
1454 { "none", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CROP_DISABLED }, .unit = "apply_cropping" },
1455 { "all", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CROP_ALL }, .unit = "apply_cropping" },
1456 { "codec", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CROP_CODEC }, .unit = "apply_cropping" },
1457 { "container", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CROP_CONTAINER }, .unit = "apply_cropping" },
1458 { NULL },
1459 };
1460 1 const AVClass class = {
1461 .class_name = "apply_cropping",
1462 .item_name = av_default_item_name,
1463 .option = opts,
1464 .version = LIBAVUTIL_VERSION_INT,
1465 };
1466 1 const AVClass *pclass = &class;
1467
1468 1 ret = av_opt_eval_int(&pclass, opts, apply_cropping, &ds->apply_cropping);
1469
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0) {
1470 av_log(ist, AV_LOG_ERROR, "Invalid apply_cropping value '%s'.\n", apply_cropping);
1471 return ret;
1472 }
1473 }
1474
1475 8054 opt_match_per_stream_str(ist, &o->codec_tags, ic, st, &codec_tag);
1476
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 8052 times.
8054 if (codec_tag) {
1477 2 uint32_t tag = strtol(codec_tag, &next, 0);
1478
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (*next) {
1479 2 uint8_t buf[4] = { 0 };
1480
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 memcpy(buf, codec_tag, FFMIN(sizeof(buf), strlen(codec_tag)));
1481 2 tag = AV_RL32(buf);
1482 }
1483
1484 2 st->codecpar->codec_tag = tag;
1485 }
1486
1487
2/2
✓ Branch 0 taken 6138 times.
✓ Branch 1 taken 1916 times.
8054 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
1488 6138 ret = add_display_matrix_to_stream(o, ic, ist);
1489
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6138 times.
6138 if (ret < 0)
1490 return ret;
1491
1492 6138 ret = add_mastering_display_to_stream(o, ic, ist);
1493
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6138 times.
6138 if (ret < 0)
1494 return ret;
1495
1496 6138 ret = add_content_light_to_stream(o, ic, ist);
1497
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6138 times.
6138 if (ret < 0)
1498 return ret;
1499
1500 6138 opt_match_per_stream_str(ist, &o->hwaccels, ic, st, &hwaccel);
1501 6138 opt_match_per_stream_str(ist, &o->hwaccel_output_formats, ic, st,
1502 &hwaccel_output_format);
1503
4/6
✓ Branch 0 taken 6138 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5804 times.
✓ Branch 3 taken 334 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 5804 times.
6138 if (!hwaccel_output_format && hwaccel && !strcmp(hwaccel, "cuvid")) {
1504 av_log(ist, AV_LOG_WARNING,
1505 "WARNING: defaulting hwaccel_output_format to cuda for compatibility "
1506 "with old commandlines. This behaviour is DEPRECATED and will be removed "
1507 "in the future. Please explicitly set \"-hwaccel_output_format cuda\".\n");
1508 ds->dec_opts.hwaccel_output_format = AV_PIX_FMT_CUDA;
1509
4/6
✓ Branch 0 taken 6138 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5804 times.
✓ Branch 3 taken 334 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 5804 times.
6138 } else if (!hwaccel_output_format && hwaccel && !strcmp(hwaccel, "qsv")) {
1510 av_log(ist, AV_LOG_WARNING,
1511 "WARNING: defaulting hwaccel_output_format to qsv for compatibility "
1512 "with old commandlines. This behaviour is DEPRECATED and will be removed "
1513 "in the future. Please explicitly set \"-hwaccel_output_format qsv\".\n");
1514 ds->dec_opts.hwaccel_output_format = AV_PIX_FMT_QSV;
1515
4/6
✓ Branch 0 taken 6138 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5804 times.
✓ Branch 3 taken 334 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 5804 times.
6138 } else if (!hwaccel_output_format && hwaccel && !strcmp(hwaccel, "mediacodec")) {
1516 // There is no real AVHWFrameContext implementation. Set
1517 // hwaccel_output_format to avoid av_hwframe_transfer_data error.
1518 ds->dec_opts.hwaccel_output_format = AV_PIX_FMT_MEDIACODEC;
1519
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6138 times.
6138 } else if (hwaccel_output_format) {
1520 ds->dec_opts.hwaccel_output_format = av_get_pix_fmt(hwaccel_output_format);
1521 if (ds->dec_opts.hwaccel_output_format == AV_PIX_FMT_NONE) {
1522 av_log(ist, AV_LOG_FATAL, "Unrecognised hwaccel output "
1523 "format: %s", hwaccel_output_format);
1524 }
1525 } else {
1526 6138 ds->dec_opts.hwaccel_output_format = AV_PIX_FMT_NONE;
1527 }
1528
1529
2/2
✓ Branch 0 taken 5804 times.
✓ Branch 1 taken 334 times.
6138 if (hwaccel) {
1530 // The NVDEC hwaccels use a CUDA device, so remap the name here.
1531
2/4
✓ Branch 0 taken 5804 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5804 times.
5804 if (!strcmp(hwaccel, "nvdec") || !strcmp(hwaccel, "cuvid"))
1532 hwaccel = "cuda";
1533
1534
1/2
✓ Branch 0 taken 5804 times.
✗ Branch 1 not taken.
5804 if (!strcmp(hwaccel, "none"))
1535 5804 ds->dec_opts.hwaccel_id = HWACCEL_NONE;
1536 else if (!strcmp(hwaccel, "auto"))
1537 ds->dec_opts.hwaccel_id = HWACCEL_AUTO;
1538 else {
1539 enum AVHWDeviceType type = av_hwdevice_find_type_by_name(hwaccel);
1540 if (type != AV_HWDEVICE_TYPE_NONE) {
1541 ds->dec_opts.hwaccel_id = HWACCEL_GENERIC;
1542 ds->dec_opts.hwaccel_device_type = type;
1543 }
1544
1545 if (!ds->dec_opts.hwaccel_id) {
1546 av_log(ist, AV_LOG_FATAL, "Unrecognized hwaccel: %s.\n",
1547 hwaccel);
1548 av_log(ist, AV_LOG_FATAL, "Supported hwaccels: ");
1549 type = AV_HWDEVICE_TYPE_NONE;
1550 while ((type = av_hwdevice_iterate_types(type)) !=
1551 AV_HWDEVICE_TYPE_NONE)
1552 av_log(ist, AV_LOG_FATAL, "%s ",
1553 av_hwdevice_get_type_name(type));
1554 av_log(ist, AV_LOG_FATAL, "\n");
1555 return AVERROR(EINVAL);
1556 }
1557 }
1558 }
1559
1560 6138 opt_match_per_stream_str(ist, &o->hwaccel_devices, ic, st, &hwaccel_device);
1561
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6138 times.
6138 if (hwaccel_device) {
1562 ds->dec_opts.hwaccel_device = av_strdup(hwaccel_device);
1563 if (!ds->dec_opts.hwaccel_device)
1564 return AVERROR(ENOMEM);
1565 }
1566 }
1567
1568 8054 ret = choose_decoder(o, ist, ic, st, ds->dec_opts.hwaccel_id,
1569 ds->dec_opts.hwaccel_device_type, &ist->dec);
1570
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8054 times.
8054 if (ret < 0)
1571 return ret;
1572
1573
2/2
✓ Branch 0 taken 7972 times.
✓ Branch 1 taken 82 times.
8054 if (ist->dec) {
1574 7972 ret = filter_codec_opts(o->g->codec_opts, ist->st->codecpar->codec_id,
1575 ic, st, ist->dec, &ds->decoder_opts, opts_used);
1576
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7972 times.
7972 if (ret < 0)
1577 return ret;
1578 }
1579
1580 8054 ds->reinit_filters = -1;
1581 8054 opt_match_per_stream_int(ist, &o->reinit_filters, ic, st, &ds->reinit_filters);
1582
1583 8054 ds->drop_changed = 0;
1584 8054 opt_match_per_stream_int(ist, &o->drop_changed, ic, st, &ds->drop_changed);
1585
1586
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 8054 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
8054 if (ds->drop_changed && ds->reinit_filters) {
1587 if (ds->reinit_filters > 0) {
1588 av_log(ist, AV_LOG_ERROR, "drop_changed and reinit_filters both enabled. These are mutually exclusive.\n");
1589 return AVERROR(EINVAL);
1590 }
1591 ds->reinit_filters = 0;
1592 }
1593
1594 8054 ist->user_set_discard = AVDISCARD_NONE;
1595
1596
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 8054 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
8054 if ((o->video_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) ||
1597
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 8053 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
8054 (o->audio_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) ||
1598
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 8053 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
8053 (o->subtitle_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) ||
1599
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 8053 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
8053 (o->data_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_DATA))
1600 1 ist->user_set_discard = AVDISCARD_ALL;
1601
1602 8054 opt_match_per_stream_str(ist, &o->discard, ic, st, &discard_str);
1603
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8054 times.
8054 if (discard_str) {
1604 ret = av_opt_set(ist->st, "discard", discard_str, 0);
1605 if (ret < 0) {
1606 av_log(ist, AV_LOG_ERROR, "Error parsing discard %s.\n", discard_str);
1607 return ret;
1608 }
1609 ist->user_set_discard = ist->st->discard;
1610 }
1611
1612
2/2
✓ Branch 0 taken 107 times.
✓ Branch 1 taken 7947 times.
8054 ds->dec_opts.flags |= DECODER_FLAG_BITEXACT * !!o->bitexact;
1613
1614 8054 av_dict_set_int(&ds->decoder_opts, "apply_cropping",
1615
3/4
✓ Branch 0 taken 8053 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 8053 times.
✗ Branch 3 not taken.
8054 ds->apply_cropping && ds->apply_cropping != CROP_CONTAINER, 0);
1616
1617 8054 av_bprint_init(&bp, 0, AV_BPRINT_SIZE_AUTOMATIC);
1618
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 8052 times.
8054 if (ds->force_display_matrix) {
1619
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (av_dict_get(ds->decoder_opts, "side_data_prefer_packet", NULL, 0))
1620 av_bprintf(&bp, ",");
1621 2 av_bprintf(&bp, "displaymatrix");
1622 }
1623
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8054 times.
8054 if (ds->force_mastering_display) {
1624 if (bp.len || av_dict_get(ds->decoder_opts, "side_data_prefer_packet", NULL, 0))
1625 av_bprintf(&bp, ",");
1626 av_bprintf(&bp, "mastering_display_metadata");
1627 }
1628
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8054 times.
8054 if (ds->force_content_light) {
1629 if (bp.len || av_dict_get(ds->decoder_opts, "side_data_prefer_packet", NULL, 0))
1630 av_bprintf(&bp, ",");
1631 av_bprintf(&bp, "content_light_level");
1632 }
1633
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 8052 times.
8054 if (bp.len)
1634 2 av_dict_set(&ds->decoder_opts, "side_data_prefer_packet", bp.str, AV_DICT_APPEND);
1635 8054 av_bprint_finalize(&bp, NULL);
1636
1637 /* Attached pics are sparse, therefore we would not want to delay their decoding
1638 * till EOF. */
1639
2/2
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 8017 times.
8054 if (ist->st->disposition & AV_DISPOSITION_ATTACHED_PIC)
1640 37 av_dict_set(&ds->decoder_opts, "thread_type", "-frame", 0);
1641
1642
4/5
✓ Branch 0 taken 6138 times.
✓ Branch 1 taken 1759 times.
✓ Branch 2 taken 155 times.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
8054 switch (par->codec_type) {
1643 6138 case AVMEDIA_TYPE_VIDEO:
1644 6138 opt_match_per_stream_str(ist, &o->frame_rates, ic, st, &framerate);
1645
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 6105 times.
6138 if (framerate) {
1646 33 ret = av_parse_video_rate(&ist->framerate, framerate);
1647
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 if (ret < 0) {
1648 av_log(ist, AV_LOG_ERROR, "Error parsing framerate %s.\n",
1649 framerate);
1650 return ret;
1651 }
1652 }
1653
1654 #if FFMPEG_OPT_TOP
1655 6138 ist->top_field_first = -1;
1656 6138 opt_match_per_stream_int(ist, &o->top_field_first, ic, st, &ist->top_field_first);
1657 #endif
1658
1659 6138 break;
1660 1759 case AVMEDIA_TYPE_AUDIO: {
1661 1759 const char *ch_layout_str = NULL;
1662
1663 1759 opt_match_per_stream_str(ist, &o->audio_ch_layouts, ic, st, &ch_layout_str);
1664
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1756 times.
1759 if (ch_layout_str) {
1665 AVChannelLayout ch_layout;
1666 3 ret = av_channel_layout_from_string(&ch_layout, ch_layout_str);
1667
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0) {
1668 av_log(ist, AV_LOG_ERROR, "Error parsing channel layout %s.\n", ch_layout_str);
1669 return ret;
1670 }
1671
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 if (par->ch_layout.nb_channels <= 0 || par->ch_layout.nb_channels == ch_layout.nb_channels) {
1672 3 av_channel_layout_uninit(&par->ch_layout);
1673 3 par->ch_layout = ch_layout;
1674 } else {
1675 av_log(ist, AV_LOG_ERROR,
1676 "Specified channel layout '%s' has %d channels, but input has %d channels.\n",
1677 ch_layout_str, ch_layout.nb_channels, par->ch_layout.nb_channels);
1678 av_channel_layout_uninit(&ch_layout);
1679 return AVERROR(EINVAL);
1680 }
1681 } else {
1682 1756 int guess_layout_max = INT_MAX;
1683 1756 opt_match_per_stream_int(ist, &o->guess_layout_max, ic, st, &guess_layout_max);
1684 1756 guess_input_channel_layout(ist, par, guess_layout_max);
1685 }
1686 1759 break;
1687 }
1688 155 case AVMEDIA_TYPE_DATA:
1689 case AVMEDIA_TYPE_SUBTITLE: {
1690 155 const char *canvas_size = NULL;
1691
1692 155 opt_match_per_stream_int(ist, &o->fix_sub_duration, ic, st, &ist->fix_sub_duration);
1693 155 opt_match_per_stream_str(ist, &o->canvas_sizes, ic, st, &canvas_size);
1694
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 155 times.
155 if (canvas_size) {
1695 ret = av_parse_video_size(&par->width, &par->height,
1696 canvas_size);
1697 if (ret < 0) {
1698 av_log(ist, AV_LOG_FATAL, "Invalid canvas size: %s.\n", canvas_size);
1699 return ret;
1700 }
1701 }
1702 155 break;
1703 }
1704 2 case AVMEDIA_TYPE_ATTACHMENT:
1705 case AVMEDIA_TYPE_UNKNOWN:
1706 2 break;
1707 default: av_assert0(0);
1708 }
1709
1710 8054 ist->par = avcodec_parameters_alloc();
1711
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8054 times.
8054 if (!ist->par)
1712 return AVERROR(ENOMEM);
1713
1714 8054 ret = avcodec_parameters_copy(ist->par, par);
1715
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8054 times.
8054 if (ret < 0) {
1716 av_log(ist, AV_LOG_ERROR, "Error exporting stream parameters.\n");
1717 return ret;
1718 }
1719
1720
2/2
✓ Branch 0 taken 451 times.
✓ Branch 1 taken 7603 times.
8054 if (ist->st->sample_aspect_ratio.num)
1721 451 ist->par->sample_aspect_ratio = ist->st->sample_aspect_ratio;
1722
1723 8054 opt_match_per_stream_str(ist, &o->bitstream_filters, ic, st, &bsfs);
1724
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 8052 times.
8054 if (bsfs) {
1725 2 ret = av_bsf_list_parse_str(bsfs, &ds->bsf);
1726
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret < 0) {
1727 av_log(ist, AV_LOG_ERROR,
1728 "Error parsing bitstream filter sequence '%s': %s\n",
1729 bsfs, av_err2str(ret));
1730 return ret;
1731 }
1732
1733 2 ret = avcodec_parameters_copy(ds->bsf->par_in, ist->par);
1734
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret < 0)
1735 return ret;
1736 2 ds->bsf->time_base_in = ist->st->time_base;
1737
1738 2 ret = av_bsf_init(ds->bsf);
1739
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret < 0) {
1740 av_log(ist, AV_LOG_ERROR, "Error initializing bitstream filters: %s\n",
1741 av_err2str(ret));
1742 return ret;
1743 }
1744
1745 2 ret = avcodec_parameters_copy(ist->par, ds->bsf->par_out);
1746
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret < 0)
1747 return ret;
1748 }
1749
1750 8054 ds->codec_desc = avcodec_descriptor_get(ist->par->codec_id);
1751
1752 8054 return 0;
1753 }
1754
1755 static const char *input_stream_group_item_name(void *obj)
1756 {
1757 const DemuxStreamGroup *dsg = obj;
1758
1759 return dsg->log_name;
1760 }
1761
1762 static const AVClass input_stream_group_class = {
1763 .class_name = "InputStreamGroup",
1764 .version = LIBAVUTIL_VERSION_INT,
1765 .item_name = input_stream_group_item_name,
1766 .category = AV_CLASS_CATEGORY_DEMUXER,
1767 };
1768
1769 47 static DemuxStreamGroup *demux_stream_group_alloc(Demuxer *d, AVStreamGroup *stg)
1770 {
1771 47 InputFile *f = &d->f;
1772 DemuxStreamGroup *dsg;
1773
1774 47 dsg = allocate_array_elem(&f->stream_groups, sizeof(*dsg), &f->nb_stream_groups);
1775
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 47 times.
47 if (!dsg)
1776 return NULL;
1777
1778 47 dsg->istg.stg = stg;
1779 47 dsg->istg.file = f;
1780 47 dsg->istg.index = stg->index;
1781 47 dsg->istg.class = &input_stream_group_class;
1782
1783 47 snprintf(dsg->log_name, sizeof(dsg->log_name), "istg#%d:%d/%s",
1784 d->f.index, stg->index, avformat_stream_group_name(stg->type));
1785
1786 47 return dsg;
1787 }
1788
1789 6 static int istg_parse_tile_grid(const OptionsContext *o, Demuxer *d, InputStreamGroup *istg)
1790 {
1791 6 InputFile *f = &d->f;
1792 6 AVFormatContext *ic = d->f.ctx;
1793 6 AVStreamGroup *stg = istg->stg;
1794 6 const AVStreamGroupTileGrid *tg = stg->params.tile_grid;
1795 OutputFilterOptions opts;
1796 AVBPrint bp;
1797 char *graph_str;
1798 6 int autorotate = 1;
1799 6 const char *apply_cropping = NULL;
1800 int ret;
1801
1802
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (tg->nb_tiles == 1)
1803 return 0;
1804
1805 6 memset(&opts, 0, sizeof(opts));
1806
1807 6 opt_match_per_stream_group_int(istg, &o->autorotate, ic, stg, &autorotate);
1808
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (autorotate)
1809 6 opts.flags |= OFILTER_FLAG_AUTOROTATE;
1810
1811 6 opts.flags |= OFILTER_FLAG_CROP;
1812 6 opt_match_per_stream_group_str(istg, &o->apply_cropping, ic, stg, &apply_cropping);
1813
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (apply_cropping) {
1814 char *p;
1815 int crop = strtol(apply_cropping, &p, 0);
1816 if (*p)
1817 return AVERROR(EINVAL);
1818 if (!crop)
1819 opts.flags &= ~OFILTER_FLAG_CROP;
1820 }
1821
1822 6 av_bprint_init(&bp, 0, AV_BPRINT_SIZE_UNLIMITED);
1823
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 6 times.
26 for (int i = 0; i < tg->nb_tiles; i++)
1824 20 av_bprintf(&bp, "[%d:g:%d:%d]", f->index, stg->index, tg->offsets[i].idx);
1825 6 av_bprintf(&bp, "xstack=inputs=%d:layout=", tg->nb_tiles);
1826
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 6 times.
20 for (int i = 0; i < tg->nb_tiles - 1; i++)
1827 14 av_bprintf(&bp, "%d_%d|", tg->offsets[i].horizontal,
1828 14 tg->offsets[i].vertical);
1829 6 av_bprintf(&bp, "%d_%d:fill=0x%02X%02X%02X@0x%02X", tg->offsets[tg->nb_tiles - 1].horizontal,
1830 6 tg->offsets[tg->nb_tiles - 1].vertical,
1831 6 tg->background[0], tg->background[1],
1832 6 tg->background[2], tg->background[3]);
1833 6 av_bprintf(&bp, "[%d:g:%d]", f->index, stg->index);
1834 6 ret = av_bprint_finalize(&bp, &graph_str);
1835
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (ret < 0)
1836 return ret;
1837
1838
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
6 if (tg->coded_width != tg->width || tg->coded_height != tg->height) {
1839 opts.crop_top = tg->vertical_offset;
1840 opts.crop_bottom = tg->coded_height - tg->height - tg->vertical_offset;
1841 opts.crop_left = tg->horizontal_offset;
1842 opts.crop_right = tg->coded_width - tg->width - tg->horizontal_offset;
1843 }
1844
1845
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 for (int i = 0; i < tg->nb_coded_side_data; i++) {
1846 const AVPacketSideData *sd = &tg->coded_side_data[i];
1847
1848 ret = av_packet_side_data_to_frame(&opts.side_data, &opts.nb_side_data, sd, 0);
1849 if (ret < 0 && ret != AVERROR(EINVAL))
1850 goto fail;
1851 }
1852
1853 6 ret = fg_create(NULL, &graph_str, d->sch, &opts);
1854
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (ret < 0)
1855 goto fail;
1856
1857 6 istg->fg = filtergraphs[nb_filtergraphs-1];
1858 6 istg->fg->is_internal = 1;
1859
1860 6 ret = 0;
1861 6 fail:
1862
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (ret < 0)
1863 av_freep(&graph_str);
1864
1865 6 return ret;
1866 }
1867
1868 47 static int istg_add(const OptionsContext *o, Demuxer *d, AVStreamGroup *stg)
1869 {
1870 DemuxStreamGroup *dsg;
1871 InputStreamGroup *istg;
1872 int ret;
1873
1874 47 dsg = demux_stream_group_alloc(d, stg);
1875
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 47 times.
47 if (!dsg)
1876 return AVERROR(ENOMEM);
1877
1878 47 istg = &dsg->istg;
1879
1880
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 41 times.
47 switch (stg->type) {
1881 6 case AV_STREAM_GROUP_PARAMS_TILE_GRID:
1882 6 ret = istg_parse_tile_grid(o, d, istg);
1883
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (ret < 0)
1884 return ret;
1885 6 break;
1886 41 default:
1887 41 break;
1888 }
1889
1890 47 return 0;
1891 }
1892
1893 static int is_windows_reserved_device_name(const char *f)
1894 {
1895 #if HAVE_DOS_PATHS
1896 for (const char *p = f; p && *p; ) {
1897 char stem[6], *s;
1898 av_strlcpy(stem, p, sizeof(stem));
1899 if ((s = strchr(stem, '.')))
1900 *s = 0;
1901 if ((s = strpbrk(stem, "123456789")))
1902 *s = '1';
1903
1904 if( !av_strcasecmp(stem, "AUX") ||
1905 !av_strcasecmp(stem, "CON") ||
1906 !av_strcasecmp(stem, "NUL") ||
1907 !av_strcasecmp(stem, "PRN") ||
1908 !av_strcasecmp(stem, "COM1") ||
1909 !av_strcasecmp(stem, "LPT1")
1910 )
1911 return 1;
1912
1913 p = strchr(p, '/');
1914 if (p)
1915 p++;
1916 }
1917 #endif
1918 return 0;
1919 }
1920
1921 static int safe_filename(const char *f, int allow_subdir)
1922 {
1923 const char *start = f;
1924
1925 if (!*f || is_windows_reserved_device_name(f))
1926 return 0;
1927
1928 for (; *f; f++) {
1929 /* A-Za-z0-9_- */
1930 if (!((unsigned)((*f | 32) - 'a') < 26 ||
1931 (unsigned)(*f - '0') < 10 || *f == '_' || *f == '-')) {
1932 if (f == start)
1933 return 0;
1934 else if (allow_subdir && *f == '/')
1935 start = f + 1;
1936 else if (*f != '.')
1937 return 0;
1938 }
1939 }
1940 return 1;
1941 }
1942
1943 static int dump_attachment(InputStream *ist, const char *filename)
1944 {
1945 AVStream *st = ist->st;
1946 int ret;
1947 AVIOContext *out = NULL;
1948 const AVDictionaryEntry *e;
1949
1950 if (!st->codecpar->extradata_size) {
1951 av_log(ist, AV_LOG_WARNING, "No extradata to dump.\n");
1952 return 0;
1953 }
1954 if (!*filename && (e = av_dict_get(st->metadata, "filename", NULL, 0))) {
1955 filename = e->value;
1956 if (!safe_filename(filename, 0)) {
1957 av_log(ist, AV_LOG_ERROR, "Filename %s is unsafe\n", filename);
1958 return AVERROR(EINVAL);
1959 }
1960 }
1961 if (!*filename) {
1962 av_log(ist, AV_LOG_FATAL, "No filename specified and no 'filename' tag");
1963 return AVERROR(EINVAL);
1964 }
1965
1966 ret = assert_file_overwrite(filename);
1967 if (ret < 0)
1968 return ret;
1969
1970 if ((ret = avio_open2(&out, filename, AVIO_FLAG_WRITE, &int_cb, NULL)) < 0) {
1971 av_log(ist, AV_LOG_FATAL, "Could not open file %s for writing.\n",
1972 filename);
1973 return ret;
1974 }
1975
1976 avio_write(out, st->codecpar->extradata, st->codecpar->extradata_size);
1977 ret = avio_close(out);
1978
1979 if (ret >= 0)
1980 av_log(ist, AV_LOG_INFO, "Wrote attachment (%d bytes) to '%s'\n",
1981 st->codecpar->extradata_size, filename);
1982
1983 return ret;
1984 }
1985
1986 697 static const char *input_file_item_name(void *obj)
1987 {
1988 697 const Demuxer *d = obj;
1989
1990 697 return d->log_name;
1991 }
1992
1993 static const AVClass input_file_class = {
1994 .class_name = "InputFile",
1995 .version = LIBAVUTIL_VERSION_INT,
1996 .item_name = input_file_item_name,
1997 .category = AV_CLASS_CATEGORY_DEMUXER,
1998 };
1999
2000 7448 static Demuxer *demux_alloc(void)
2001 {
2002 7448 Demuxer *d = allocate_array_elem(&input_files, sizeof(*d), &nb_input_files);
2003
2004
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7448 times.
7448 if (!d)
2005 return NULL;
2006
2007 7448 d->f.class = &input_file_class;
2008 7448 d->f.index = nb_input_files - 1;
2009
2010 7448 snprintf(d->log_name, sizeof(d->log_name), "in#%d", d->f.index);
2011
2012 7448 return d;
2013 }
2014
2015 7448 int ifile_open(const OptionsContext *o, const char *filename, Scheduler *sch)
2016 {
2017 Demuxer *d;
2018 InputFile *f;
2019 AVFormatContext *ic;
2020 7448 const AVInputFormat *file_iformat = NULL;
2021 7448 int err, ret = 0;
2022 int64_t timestamp;
2023 7448 AVDictionary *opts_used = NULL;
2024 7448 const char* video_codec_name = NULL;
2025 7448 const char* audio_codec_name = NULL;
2026 7448 const char* subtitle_codec_name = NULL;
2027 7448 const char* data_codec_name = NULL;
2028 7448 int scan_all_pmts_set = 0;
2029
2030 7448 int64_t start_time = o->start_time;
2031 7448 int64_t start_time_eof = o->start_time_eof;
2032 7448 int64_t stop_time = o->stop_time;
2033 7448 int64_t recording_time = o->recording_time;
2034
2035 7448 d = demux_alloc();
2036
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7448 times.
7448 if (!d)
2037 return AVERROR(ENOMEM);
2038
2039 7448 f = &d->f;
2040
2041 7448 ret = sch_add_demux(sch, input_thread, d);
2042
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7448 times.
7448 if (ret < 0)
2043 return ret;
2044 7448 d->sch = sch;
2045
2046
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 7448 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
7448 if (stop_time != INT64_MAX && recording_time != INT64_MAX) {
2047 stop_time = INT64_MAX;
2048 av_log(d, AV_LOG_WARNING, "-t and -to cannot be used together; using -t.\n");
2049 }
2050
2051
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 7448 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
7448 if (stop_time != INT64_MAX && recording_time == INT64_MAX) {
2052 int64_t start = start_time == AV_NOPTS_VALUE ? 0 : start_time;
2053 if (stop_time <= start) {
2054 av_log(d, AV_LOG_ERROR, "-to value smaller than -ss; aborting.\n");
2055 return AVERROR(EINVAL);
2056 } else {
2057 recording_time = stop_time - start;
2058 }
2059 }
2060
2061
2/2
✓ Branch 0 taken 3877 times.
✓ Branch 1 taken 3571 times.
7448 if (o->format) {
2062
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3877 times.
3877 if (!(file_iformat = av_find_input_format(o->format))) {
2063 av_log(d, AV_LOG_FATAL, "Unknown input format: '%s'\n", o->format);
2064 return AVERROR(EINVAL);
2065 }
2066 }
2067
2068
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7448 times.
7448 if (!strcmp(filename, "-"))
2069 filename = "fd:";
2070
2071 22344 stdin_interaction &= strncmp(filename, "pipe:", 5) &&
2072
2/4
✓ Branch 0 taken 7448 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7448 times.
✗ Branch 3 not taken.
14896 strcmp(filename, "fd:") &&
2073
1/2
✓ Branch 0 taken 7448 times.
✗ Branch 1 not taken.
7448 strcmp(filename, "/dev/stdin");
2074
2075 /* get default parameters from command line */
2076 7448 ic = avformat_alloc_context();
2077
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7448 times.
7448 if (!ic)
2078 return AVERROR(ENOMEM);
2079 7448 ic->name = av_strdup(d->log_name);
2080
2/2
✓ Branch 0 taken 67 times.
✓ Branch 1 taken 7381 times.
7448 if (o->audio_sample_rate.nb_opt) {
2081 67 av_dict_set_int(&o->g->format_opts, "sample_rate", o->audio_sample_rate.opt[o->audio_sample_rate.nb_opt - 1].u.i, 0);
2082 }
2083
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 7439 times.
7448 if (o->audio_channels.nb_opt) {
2084 const AVClass *priv_class;
2085
3/6
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 9 times.
✗ Branch 5 not taken.
18 if (file_iformat && (priv_class = file_iformat->priv_class) &&
2086 9 av_opt_find(&priv_class, "ch_layout", NULL, 0,
2087 AV_OPT_SEARCH_FAKE_OBJ)) {
2088 char buf[32];
2089 9 snprintf(buf, sizeof(buf), "%dC", o->audio_channels.opt[o->audio_channels.nb_opt - 1].u.i);
2090 9 av_dict_set(&o->g->format_opts, "ch_layout", buf, 0);
2091 }
2092 }
2093
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 7445 times.
7448 if (o->audio_ch_layouts.nb_opt) {
2094 const AVClass *priv_class;
2095
4/6
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
4 if (file_iformat && (priv_class = file_iformat->priv_class) &&
2096 1 av_opt_find(&priv_class, "ch_layout", NULL, 0,
2097 AV_OPT_SEARCH_FAKE_OBJ)) {
2098 1 av_dict_set(&o->g->format_opts, "ch_layout", o->audio_ch_layouts.opt[o->audio_ch_layouts.nb_opt - 1].u.str, 0);
2099 }
2100 }
2101
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 7415 times.
7448 if (o->frame_rates.nb_opt) {
2102 const AVClass *priv_class;
2103 /* set the format-level framerate option;
2104 * this is important for video grabbers, e.g. x11 */
2105
4/6
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 32 times.
✗ Branch 5 not taken.
65 if (file_iformat && (priv_class = file_iformat->priv_class) &&
2106 32 av_opt_find(&priv_class, "framerate", NULL, 0,
2107 AV_OPT_SEARCH_FAKE_OBJ)) {
2108 32 av_dict_set(&o->g->format_opts, "framerate",
2109 32 o->frame_rates.opt[o->frame_rates.nb_opt - 1].u.str, 0);
2110 }
2111 }
2112
2/2
✓ Branch 0 taken 567 times.
✓ Branch 1 taken 6881 times.
7448 if (o->frame_sizes.nb_opt) {
2113 567 av_dict_set(&o->g->format_opts, "video_size", o->frame_sizes.opt[o->frame_sizes.nb_opt - 1].u.str, 0);
2114 }
2115
2/2
✓ Branch 0 taken 576 times.
✓ Branch 1 taken 6872 times.
7448 if (o->frame_pix_fmts.nb_opt)
2116 576 av_dict_set(&o->g->format_opts, "pixel_format", o->frame_pix_fmts.opt[o->frame_pix_fmts.nb_opt - 1].u.str, 0);
2117
2118 7448 video_codec_name = opt_match_per_type_str(&o->codec_names, 'v');
2119 7448 audio_codec_name = opt_match_per_type_str(&o->codec_names, 'a');
2120 7448 subtitle_codec_name = opt_match_per_type_str(&o->codec_names, 's');
2121 7448 data_codec_name = opt_match_per_type_str(&o->codec_names, 'd');
2122
2123
2/2
✓ Branch 0 taken 3157 times.
✓ Branch 1 taken 4291 times.
7448 if (video_codec_name)
2124 3157 ret = err_merge(ret, find_codec(NULL, video_codec_name , AVMEDIA_TYPE_VIDEO , 0,
2125 3157 &ic->video_codec));
2126
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 7430 times.
7448 if (audio_codec_name)
2127 18 ret = err_merge(ret, find_codec(NULL, audio_codec_name , AVMEDIA_TYPE_AUDIO , 0,
2128 18 &ic->audio_codec));
2129
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7448 times.
7448 if (subtitle_codec_name)
2130 ret = err_merge(ret, find_codec(NULL, subtitle_codec_name, AVMEDIA_TYPE_SUBTITLE, 0,
2131 &ic->subtitle_codec));
2132
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7448 times.
7448 if (data_codec_name)
2133 ret = err_merge(ret, find_codec(NULL, data_codec_name , AVMEDIA_TYPE_DATA, 0,
2134 &ic->data_codec));
2135
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7448 times.
7448 if (ret < 0) {
2136 avformat_free_context(ic);
2137 return ret;
2138 }
2139
2140
2/2
✓ Branch 0 taken 3157 times.
✓ Branch 1 taken 4291 times.
7448 ic->video_codec_id = video_codec_name ? ic->video_codec->id : AV_CODEC_ID_NONE;
2141
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 7430 times.
7448 ic->audio_codec_id = audio_codec_name ? ic->audio_codec->id : AV_CODEC_ID_NONE;
2142
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7448 times.
7448 ic->subtitle_codec_id = subtitle_codec_name ? ic->subtitle_codec->id : AV_CODEC_ID_NONE;
2143
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7448 times.
7448 ic->data_codec_id = data_codec_name ? ic->data_codec->id : AV_CODEC_ID_NONE;
2144
2145 7448 ic->flags |= AVFMT_FLAG_NONBLOCK;
2146
2/2
✓ Branch 0 taken 96 times.
✓ Branch 1 taken 7352 times.
7448 if (o->bitexact)
2147 96 ic->flags |= AVFMT_FLAG_BITEXACT;
2148 7448 ic->interrupt_callback = int_cb;
2149
2150
1/2
✓ Branch 1 taken 7448 times.
✗ Branch 2 not taken.
7448 if (!av_dict_get(o->g->format_opts, "scan_all_pmts", NULL, AV_DICT_MATCH_CASE)) {
2151 7448 av_dict_set(&o->g->format_opts, "scan_all_pmts", "1", AV_DICT_DONT_OVERWRITE);
2152 7448 scan_all_pmts_set = 1;
2153 }
2154 /* open the input file with generic avformat function */
2155 7448 err = avformat_open_input(&ic, filename, file_iformat, &o->g->format_opts);
2156
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7447 times.
7448 if (err < 0) {
2157
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (err != AVERROR_EXIT)
2158 1 av_log(d, AV_LOG_ERROR,
2159 1 "Error opening input: %s\n", av_err2str(err));
2160
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (err == AVERROR_PROTOCOL_NOT_FOUND)
2161 av_log(d, AV_LOG_ERROR, "Did you mean file:%s?\n", filename);
2162 1 return err;
2163 }
2164 7447 f->ctx = ic;
2165
2166 7447 av_strlcat(d->log_name, "/", sizeof(d->log_name));
2167 7447 av_strlcat(d->log_name, ic->iformat->name, sizeof(d->log_name));
2168 7447 av_freep(&ic->name);
2169 7447 ic->name = av_strdup(d->log_name);
2170
2171
1/2
✓ Branch 0 taken 7447 times.
✗ Branch 1 not taken.
7447 if (scan_all_pmts_set)
2172 7447 av_dict_set(&o->g->format_opts, "scan_all_pmts", NULL, AV_DICT_MATCH_CASE);
2173 7447 remove_avoptions(&o->g->format_opts, o->g->codec_opts);
2174
2175 7447 ret = check_avoptions(o->g->format_opts);
2176
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7447 times.
7447 if (ret < 0)
2177 return ret;
2178
2179 /* apply forced codec ids */
2180
2/2
✓ Branch 0 taken 7902 times.
✓ Branch 1 taken 7447 times.
15349 for (int i = 0; i < ic->nb_streams; i++) {
2181 const AVCodec *dummy;
2182 7902 ret = choose_decoder(o, f, ic, ic->streams[i], HWACCEL_NONE, AV_HWDEVICE_TYPE_NONE,
2183 &dummy);
2184
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7902 times.
7902 if (ret < 0)
2185 return ret;
2186 }
2187
2188
2/2
✓ Branch 0 taken 7445 times.
✓ Branch 1 taken 2 times.
7447 if (o->find_stream_info) {
2189 AVDictionary **opts;
2190 7445 int orig_nb_streams = ic->nb_streams;
2191
2192 7445 ret = setup_find_stream_info_opts(ic, o->g->codec_opts, &opts);
2193
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7445 times.
7445 if (ret < 0)
2194 return ret;
2195
2196 /* If not enough info to get the stream parameters, we decode the
2197 first frames to get it. (used in mpeg case for example) */
2198 7445 ret = avformat_find_stream_info(ic, opts);
2199
2200
2/2
✓ Branch 0 taken 7900 times.
✓ Branch 1 taken 7445 times.
15345 for (int i = 0; i < orig_nb_streams; i++)
2201 7900 av_dict_free(&opts[i]);
2202 7445 av_freep(&opts);
2203
2204
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7445 times.
7445 if (ret < 0) {
2205 av_log(d, AV_LOG_FATAL, "could not find codec parameters\n");
2206 if (ic->nb_streams == 0)
2207 return ret;
2208 }
2209 }
2210
2211
3/4
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 7419 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 28 times.
7447 if (start_time != AV_NOPTS_VALUE && start_time_eof != AV_NOPTS_VALUE) {
2212 av_log(d, AV_LOG_WARNING, "Cannot use -ss and -sseof both, using -ss\n");
2213 start_time_eof = AV_NOPTS_VALUE;
2214 }
2215
2216
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7447 times.
7447 if (start_time_eof != AV_NOPTS_VALUE) {
2217 if (start_time_eof >= 0) {
2218 av_log(d, AV_LOG_ERROR, "-sseof value must be negative; aborting\n");
2219 return AVERROR(EINVAL);
2220 }
2221 if (ic->duration > 0) {
2222 start_time = start_time_eof + ic->duration;
2223 if (start_time < 0) {
2224 av_log(d, AV_LOG_WARNING, "-sseof value seeks to before start of file; ignored\n");
2225 start_time = AV_NOPTS_VALUE;
2226 }
2227 } else
2228 av_log(d, AV_LOG_WARNING, "Cannot use -sseof, file duration not known\n");
2229 }
2230
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 7419 times.
7447 timestamp = (start_time == AV_NOPTS_VALUE) ? 0 : start_time;
2231 /* add the stream start time */
2232
4/4
✓ Branch 0 taken 7444 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 5705 times.
✓ Branch 3 taken 1739 times.
7447 if (!o->seek_timestamp && ic->start_time != AV_NOPTS_VALUE)
2233 5705 timestamp += ic->start_time;
2234
2235 /* if seeking requested, we execute it */
2236
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 7419 times.
7447 if (start_time != AV_NOPTS_VALUE) {
2237 28 int64_t seek_timestamp = timestamp;
2238
2239
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 1 times.
28 if (!(ic->iformat->flags & AVFMT_SEEK_TO_PTS)) {
2240 27 int dts_heuristic = 0;
2241
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 27 times.
54 for (int i = 0; i < ic->nb_streams; i++) {
2242 27 const AVCodecParameters *par = ic->streams[i]->codecpar;
2243
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (par->video_delay) {
2244 dts_heuristic = 1;
2245 break;
2246 }
2247 }
2248
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (dts_heuristic) {
2249 seek_timestamp -= 3*AV_TIME_BASE / 23;
2250 }
2251 }
2252 28 ret = avformat_seek_file(ic, -1, INT64_MIN, seek_timestamp, seek_timestamp, 0);
2253
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 27 times.
28 if (ret < 0) {
2254 1 av_log(d, AV_LOG_WARNING, "could not seek to position %0.3f\n",
2255 1 (double)timestamp / AV_TIME_BASE);
2256 }
2257 }
2258
2259 7447 f->start_time = start_time;
2260 7447 d->recording_time = recording_time;
2261 7447 f->input_sync_ref = o->input_sync_ref;
2262 7447 f->input_ts_offset = o->input_ts_offset;
2263
3/6
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 7436 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 11 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
7447 f->ts_offset = o->input_ts_offset - (copy_ts ? (start_at_zero && ic->start_time != AV_NOPTS_VALUE ? ic->start_time : 0) : timestamp);
2264 7447 d->accurate_seek = o->accurate_seek;
2265 7447 d->loop = o->loop;
2266 7447 d->nb_streams_warn = ic->nb_streams;
2267
2268 7447 d->duration = (Timestamp){ .ts = 0, .tb = (AVRational){ 1, 1 } };
2269 7447 d->min_pts = (Timestamp){ .ts = AV_NOPTS_VALUE, .tb = (AVRational){ 1, 1 } };
2270 7447 d->max_pts = (Timestamp){ .ts = AV_NOPTS_VALUE, .tb = (AVRational){ 1, 1 } };
2271
2272 7447 d->readrate = o->readrate ? o->readrate : 0.0;
2273
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7447 times.
7447 if (d->readrate < 0.0f) {
2274 av_log(d, AV_LOG_ERROR, "Option -readrate is %0.3f; it must be non-negative.\n", d->readrate);
2275 return AVERROR(EINVAL);
2276 }
2277
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7446 times.
7447 if (o->rate_emu) {
2278
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (d->readrate) {
2279 av_log(d, AV_LOG_WARNING, "Both -readrate and -re set. Using -readrate %0.3f.\n", d->readrate);
2280 } else
2281 1 d->readrate = 1.0f;
2282 }
2283
2284
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7446 times.
7447 if (d->readrate) {
2285
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 d->readrate_initial_burst = o->readrate_initial_burst ? o->readrate_initial_burst : 0.5;
2286
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (d->readrate_initial_burst < 0.0) {
2287 av_log(d, AV_LOG_ERROR,
2288 "Option -readrate_initial_burst is %0.3f; it must be non-negative.\n",
2289 d->readrate_initial_burst);
2290 return AVERROR(EINVAL);
2291 }
2292
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 d->readrate_catchup = o->readrate_catchup ? o->readrate_catchup : d->readrate * 1.05;
2293
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (d->readrate_catchup < d->readrate) {
2294 av_log(d, AV_LOG_ERROR,
2295 "Option -readrate_catchup is %0.3f; it must be at least equal to %0.3f.\n",
2296 d->readrate_catchup, d->readrate);
2297 return AVERROR(EINVAL);
2298 }
2299 } else {
2300
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7446 times.
7446 if (o->readrate_initial_burst) {
2301 av_log(d, AV_LOG_WARNING, "Option -readrate_initial_burst ignored "
2302 "since neither -readrate nor -re were given\n");
2303 }
2304
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7446 times.
7446 if (o->readrate_catchup) {
2305 av_log(d, AV_LOG_WARNING, "Option -readrate_catchup ignored "
2306 "since neither -readrate nor -re were given\n");
2307 }
2308 }
2309
2310 /* Add all the streams from the given input file to the demuxer */
2311
2/2
✓ Branch 0 taken 8054 times.
✓ Branch 1 taken 7447 times.
15501 for (int i = 0; i < ic->nb_streams; i++) {
2312 8054 ret = ist_add(o, d, ic->streams[i], &opts_used);
2313
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8054 times.
8054 if (ret < 0) {
2314 av_dict_free(&opts_used);
2315 return ret;
2316 }
2317 }
2318
2319 /* Add all the stream groups from the given input file to the demuxer */
2320
2/2
✓ Branch 0 taken 47 times.
✓ Branch 1 taken 7447 times.
7494 for (int i = 0; i < ic->nb_stream_groups; i++) {
2321 47 ret = istg_add(o, d, ic->stream_groups[i]);
2322
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 47 times.
47 if (ret < 0)
2323 return ret;
2324 }
2325
2326 /* dump the file content */
2327 7447 av_dump_format(ic, f->index, filename, 0);
2328
2329 /* check if all codec options have been used */
2330 7447 ret = check_avoptions_used(o->g->codec_opts, opts_used, d, 1);
2331 7447 av_dict_free(&opts_used);
2332
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7447 times.
7447 if (ret < 0)
2333 return ret;
2334
2335
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7447 times.
7447 for (int i = 0; i < o->dump_attachment.nb_opt; i++) {
2336 for (int j = 0; j < f->nb_streams; j++) {
2337 InputStream *ist = f->streams[j];
2338
2339 if (check_stream_specifier(ic, ist->st, o->dump_attachment.opt[i].specifier) == 1) {
2340 ret = dump_attachment(ist, o->dump_attachment.opt[i].u.str);
2341 if (ret < 0)
2342 return ret;
2343 }
2344 }
2345 }
2346
2347 7447 return 0;
2348 }
2349