FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/fftools/ffmpeg_demux.c
Date: 2025-03-08 20:38:41
Exec Total Coverage
Lines: 826 1082 76.3%
Functions: 33 34 97.1%
Branches: 514 796 64.6%

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/mem.h"
32 #include "libavutil/opt.h"
33 #include "libavutil/parseutils.h"
34 #include "libavutil/pixdesc.h"
35 #include "libavutil/time.h"
36 #include "libavutil/timestamp.h"
37
38 #include "libavcodec/bsf.h"
39 #include "libavcodec/packet.h"
40
41 #include "libavformat/avformat.h"
42
43 typedef struct DemuxStream {
44 InputStream ist;
45
46 // name used for logging
47 char log_name[32];
48
49 int sch_idx_stream;
50 int sch_idx_dec;
51
52 double ts_scale;
53
54 /* non zero if the packets must be decoded in 'raw_fifo', see DECODING_FOR_* */
55 int decoding_needed;
56 #define DECODING_FOR_OST 1
57 #define DECODING_FOR_FILTER 2
58
59 /* true if stream data should be discarded */
60 int discard;
61
62 // scheduler returned EOF for this stream
63 int finished;
64
65 int streamcopy_needed;
66 int have_sub2video;
67 int reinit_filters;
68 int autorotate;
69 int apply_cropping;
70
71
72 int wrap_correction_done;
73 int saw_first_ts;
74 ///< dts of the first packet read for this stream (in AV_TIME_BASE units)
75 int64_t first_dts;
76
77 /* predicted dts of the next packet read for this stream or (when there are
78 * several frames in a packet) of the next frame in current packet (in AV_TIME_BASE units) */
79 int64_t next_dts;
80 ///< dts of the last packet read for this stream (in AV_TIME_BASE units)
81 int64_t dts;
82
83 const AVCodecDescriptor *codec_desc;
84
85 AVDictionary *decoder_opts;
86 DecoderOpts dec_opts;
87 char dec_name[16];
88 // decoded media properties, as estimated by opening the decoder
89 AVFrame *decoded_params;
90
91 AVBSFContext *bsf;
92
93 /* number of packets successfully read for this stream */
94 uint64_t nb_packets;
95 // combined size of all the packets read
96 uint64_t data_size;
97 // latest wallclock time at which packet reading resumed after a stall - used for readrate
98 int64_t resume_wc;
99 // timestamp of first packet sent after the latest stall - used for readrate
100 int64_t resume_pts;
101 // measure of how far behind packet reading is against spceified readrate
102 int64_t lag;
103 } DemuxStream;
104
105 typedef struct Demuxer {
106 InputFile f;
107
108 // name used for logging
109 char log_name[32];
110
111 int64_t wallclock_start;
112
113 /**
114 * Extra timestamp offset added by discontinuity handling.
115 */
116 int64_t ts_offset_discont;
117 int64_t last_ts;
118
119 int64_t recording_time;
120 int accurate_seek;
121
122 /* number of times input stream should be looped */
123 int loop;
124 int have_audio_dec;
125 /* duration of the looped segment of the input file */
126 Timestamp duration;
127 /* pts with the smallest/largest values ever seen */
128 Timestamp min_pts;
129 Timestamp max_pts;
130
131 /* number of streams that the user was warned of */
132 int nb_streams_warn;
133
134 float readrate;
135 double readrate_initial_burst;
136 float readrate_catchup;
137
138 Scheduler *sch;
139
140 AVPacket *pkt_heartbeat;
141
142 int read_started;
143 int nb_streams_used;
144 int nb_streams_finished;
145 } Demuxer;
146
147 typedef struct DemuxThreadContext {
148 // packet used for reading from the demuxer
149 AVPacket *pkt_demux;
150 // packet for reading from BSFs
151 AVPacket *pkt_bsf;
152 } DemuxThreadContext;
153
154 1883718 static DemuxStream *ds_from_ist(InputStream *ist)
155 {
156 1883718 return (DemuxStream*)ist;
157 }
158
159 21474 static Demuxer *demuxer_from_ifile(InputFile *f)
160 {
161 21474 return (Demuxer*)f;
162 }
163
164 55 InputStream *ist_find_unused(enum AVMediaType type)
165 {
166
1/2
✓ Branch 2 taken 74 times.
✗ Branch 3 not taken.
74 for (InputStream *ist = ist_iter(NULL); ist; ist = ist_iter(ist)) {
167 74 DemuxStream *ds = ds_from_ist(ist);
168
4/4
✓ Branch 0 taken 73 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 55 times.
✓ Branch 3 taken 18 times.
74 if (ist->par->codec_type == type && ds->discard &&
169
1/2
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
55 ist->user_set_discard != AVDISCARD_ALL)
170 55 return ist;
171 }
172 return NULL;
173 }
174
175 9770 static void report_new_stream(Demuxer *d, const AVPacket *pkt)
176 {
177 9770 const AVStream *st = d->f.ctx->streams[pkt->stream_index];
178
179
1/2
✓ Branch 0 taken 9770 times.
✗ Branch 1 not taken.
9770 if (pkt->stream_index < d->nb_streams_warn)
180 9770 return;
181 av_log(d, AV_LOG_WARNING,
182 "New %s stream with index %d at pos:%"PRId64" and DTS:%ss\n",
183 av_get_media_type_string(st->codecpar->codec_type),
184 pkt->stream_index, pkt->pos, av_ts2timestr(pkt->dts, &st->time_base));
185 d->nb_streams_warn = pkt->stream_index + 1;
186 }
187
188 11 static int seek_to_start(Demuxer *d, Timestamp end_pts)
189 {
190 11 InputFile *ifile = &d->f;
191 11 AVFormatContext *is = ifile->ctx;
192 int ret;
193
194 11 ret = avformat_seek_file(is, -1, INT64_MIN, is->start_time, is->start_time, 0);
195
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 if (ret < 0)
196 return ret;
197
198
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 8 times.
11 if (end_pts.ts != AV_NOPTS_VALUE &&
199
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 ||
200 3 av_compare_ts(d->max_pts.ts, d->max_pts.tb, end_pts.ts, end_pts.tb) < 0))
201 3 d->max_pts = end_pts;
202
203
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 if (d->max_pts.ts != AV_NOPTS_VALUE) {
204
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;
205 11 d->duration.ts = d->max_pts.ts - av_rescale_q(min_pts, d->min_pts.tb, d->max_pts.tb);
206 }
207 11 d->duration.tb = d->max_pts.tb;
208
209
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 if (d->loop > 0)
210 11 d->loop--;
211
212 11 return ret;
213 }
214
215 424118 static void ts_discontinuity_detect(Demuxer *d, InputStream *ist,
216 AVPacket *pkt)
217 {
218 424118 InputFile *ifile = &d->f;
219 424118 DemuxStream *ds = ds_from_ist(ist);
220 424118 const int fmt_is_discont = ifile->ctx->iformat->flags & AVFMT_TS_DISCONT;
221 424118 int disable_discontinuity_correction = copy_ts;
222 424118 int64_t pkt_dts = av_rescale_q_rnd(pkt->dts, pkt->time_base, AV_TIME_BASE_Q,
223 AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX);
224
225
6/6
✓ Branch 0 taken 194 times.
✓ Branch 1 taken 423924 times.
✓ Branch 2 taken 188 times.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 73 times.
✓ Branch 5 taken 115 times.
424118 if (copy_ts && ds->next_dts != AV_NOPTS_VALUE &&
226
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 73 times.
73 fmt_is_discont && ist->st->pts_wrap_bits < 60) {
227 int64_t wrap_dts = av_rescale_q_rnd(pkt->dts + (1LL<<ist->st->pts_wrap_bits),
228 pkt->time_base, AV_TIME_BASE_Q,
229 AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
230 if (FFABS(wrap_dts - ds->next_dts) < FFABS(pkt_dts - ds->next_dts)/10)
231 disable_discontinuity_correction = 0;
232 }
233
234
4/4
✓ Branch 0 taken 417327 times.
✓ Branch 1 taken 6791 times.
✓ Branch 2 taken 417139 times.
✓ Branch 3 taken 188 times.
841257 if (ds->next_dts != AV_NOPTS_VALUE && !disable_discontinuity_correction) {
235 417139 int64_t delta = pkt_dts - ds->next_dts;
236
2/2
✓ Branch 0 taken 22668 times.
✓ Branch 1 taken 394471 times.
417139 if (fmt_is_discont) {
237
4/4
✓ Branch 0 taken 22612 times.
✓ Branch 1 taken 56 times.
✓ Branch 2 taken 22585 times.
✓ Branch 3 taken 83 times.
22668 if (FFABS(delta) > 1LL * dts_delta_threshold * AV_TIME_BASE ||
238
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 22577 times.
22585 pkt_dts + AV_TIME_BASE/10 < ds->dts) {
239 91 d->ts_offset_discont -= delta;
240 91 av_log(ist, AV_LOG_WARNING,
241 "timestamp discontinuity "
242 "(stream id=%d): %"PRId64", new offset= %"PRId64"\n",
243 91 ist->st->id, delta, d->ts_offset_discont);
244 91 pkt->dts -= av_rescale_q(delta, AV_TIME_BASE_Q, pkt->time_base);
245
1/2
✓ Branch 0 taken 91 times.
✗ Branch 1 not taken.
91 if (pkt->pts != AV_NOPTS_VALUE)
246 91 pkt->pts -= av_rescale_q(delta, AV_TIME_BASE_Q, pkt->time_base);
247 }
248 } else {
249
3/4
✓ Branch 0 taken 390651 times.
✓ Branch 1 taken 3820 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 394471 times.
394471 if (FFABS(delta) > 1LL * dts_error_threshold * AV_TIME_BASE) {
250 av_log(ist, AV_LOG_WARNING,
251 "DTS %"PRId64", next:%"PRId64" st:%d invalid dropping\n",
252 pkt->dts, ds->next_dts, pkt->stream_index);
253 pkt->dts = AV_NOPTS_VALUE;
254 }
255
2/2
✓ Branch 0 taken 392407 times.
✓ Branch 1 taken 2064 times.
394471 if (pkt->pts != AV_NOPTS_VALUE){
256 392407 int64_t pkt_pts = av_rescale_q(pkt->pts, pkt->time_base, AV_TIME_BASE_Q);
257 392407 delta = pkt_pts - ds->next_dts;
258
3/4
✓ Branch 0 taken 388954 times.
✓ Branch 1 taken 3453 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 392407 times.
392407 if (FFABS(delta) > 1LL * dts_error_threshold * AV_TIME_BASE) {
259 av_log(ist, AV_LOG_WARNING,
260 "PTS %"PRId64", next:%"PRId64" invalid dropping st:%d\n",
261 pkt->pts, ds->next_dts, pkt->stream_index);
262 pkt->pts = AV_NOPTS_VALUE;
263 }
264 }
265 }
266
6/6
✓ Branch 0 taken 6791 times.
✓ Branch 1 taken 188 times.
✓ Branch 2 taken 6785 times.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 116 times.
✓ Branch 5 taken 6669 times.
6979 } else if (ds->next_dts == AV_NOPTS_VALUE && !copy_ts &&
267
1/2
✓ Branch 0 taken 116 times.
✗ Branch 1 not taken.
116 fmt_is_discont && d->last_ts != AV_NOPTS_VALUE) {
268 116 int64_t delta = pkt_dts - d->last_ts;
269
3/4
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 60 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 116 times.
116 if (FFABS(delta) > 1LL * dts_delta_threshold * AV_TIME_BASE) {
270 d->ts_offset_discont -= delta;
271 av_log(ist, AV_LOG_DEBUG,
272 "Inter stream timestamp discontinuity %"PRId64", new offset= %"PRId64"\n",
273 delta, d->ts_offset_discont);
274 pkt->dts -= av_rescale_q(delta, AV_TIME_BASE_Q, pkt->time_base);
275 if (pkt->pts != AV_NOPTS_VALUE)
276 pkt->pts -= av_rescale_q(delta, AV_TIME_BASE_Q, pkt->time_base);
277 }
278 }
279
280 424118 d->last_ts = av_rescale_q(pkt->dts, pkt->time_base, AV_TIME_BASE_Q);
281 424118 }
282
283 470393 static void ts_discontinuity_process(Demuxer *d, InputStream *ist,
284 AVPacket *pkt)
285 {
286 470393 int64_t offset = av_rescale_q(d->ts_offset_discont, AV_TIME_BASE_Q,
287 pkt->time_base);
288
289 // apply previously-detected timestamp-discontinuity offset
290 // (to all streams, not just audio/video)
291
2/2
✓ Branch 0 taken 426822 times.
✓ Branch 1 taken 43571 times.
470393 if (pkt->dts != AV_NOPTS_VALUE)
292 426822 pkt->dts += offset;
293
2/2
✓ Branch 0 taken 424719 times.
✓ Branch 1 taken 45674 times.
470393 if (pkt->pts != AV_NOPTS_VALUE)
294 424719 pkt->pts += offset;
295
296 // detect timestamp discontinuities for audio/video
297
2/2
✓ Branch 0 taken 282076 times.
✓ Branch 1 taken 188317 times.
470393 if ((ist->par->codec_type == AVMEDIA_TYPE_VIDEO ||
298
2/2
✓ Branch 0 taken 279368 times.
✓ Branch 1 taken 2708 times.
282076 ist->par->codec_type == AVMEDIA_TYPE_AUDIO) &&
299
2/2
✓ Branch 0 taken 424118 times.
✓ Branch 1 taken 43567 times.
467685 pkt->dts != AV_NOPTS_VALUE)
300 424118 ts_discontinuity_detect(d, ist, pkt);
301 470393 }
302
303 470393 static int ist_dts_update(DemuxStream *ds, AVPacket *pkt, FrameData *fd)
304 {
305 470393 InputStream *ist = &ds->ist;
306 470393 const AVCodecParameters *par = ist->par;
307
308
2/2
✓ Branch 0 taken 7405 times.
✓ Branch 1 taken 462988 times.
470393 if (!ds->saw_first_ts) {
309 7405 ds->first_dts =
310
2/2
✓ Branch 0 taken 5131 times.
✓ Branch 1 taken 2274 times.
7405 ds->dts = ist->st->avg_frame_rate.num ? - ist->par->video_delay * AV_TIME_BASE / av_q2d(ist->st->avg_frame_rate) : 0;
311
2/2
✓ Branch 0 taken 6812 times.
✓ Branch 1 taken 593 times.
7405 if (pkt->pts != AV_NOPTS_VALUE) {
312 6812 ds->first_dts =
313 6812 ds->dts += av_rescale_q(pkt->pts, pkt->time_base, AV_TIME_BASE_Q);
314 }
315 7405 ds->saw_first_ts = 1;
316 }
317
318
2/2
✓ Branch 0 taken 7405 times.
✓ Branch 1 taken 462988 times.
470393 if (ds->next_dts == AV_NOPTS_VALUE)
319 7405 ds->next_dts = ds->dts;
320
321
2/2
✓ Branch 0 taken 426822 times.
✓ Branch 1 taken 43571 times.
470393 if (pkt->dts != AV_NOPTS_VALUE)
322 426822 ds->next_dts = ds->dts = av_rescale_q(pkt->dts, pkt->time_base, AV_TIME_BASE_Q);
323
324 470393 ds->dts = ds->next_dts;
325
3/3
✓ Branch 0 taken 279368 times.
✓ Branch 1 taken 188317 times.
✓ Branch 2 taken 2708 times.
470393 switch (par->codec_type) {
326 279368 case AVMEDIA_TYPE_AUDIO:
327 av_assert1(pkt->duration >= 0);
328
1/2
✓ Branch 0 taken 279368 times.
✗ Branch 1 not taken.
279368 if (par->sample_rate) {
329 279368 ds->next_dts += ((int64_t)AV_TIME_BASE * par->frame_size) /
330 279368 par->sample_rate;
331 } else {
332 ds->next_dts += av_rescale_q(pkt->duration, pkt->time_base, AV_TIME_BASE_Q);
333 }
334 279368 break;
335 188317 case AVMEDIA_TYPE_VIDEO:
336
2/2
✓ Branch 0 taken 743 times.
✓ Branch 1 taken 187574 times.
188317 if (ist->framerate.num) {
337 // TODO: Remove work-around for c99-to-c89 issue 7
338 743 AVRational time_base_q = AV_TIME_BASE_Q;
339 743 int64_t next_dts = av_rescale_q(ds->next_dts, time_base_q, av_inv_q(ist->framerate));
340 743 ds->next_dts = av_rescale_q(next_dts + 1, av_inv_q(ist->framerate), time_base_q);
341
2/2
✓ Branch 0 taken 185282 times.
✓ Branch 1 taken 2292 times.
187574 } else if (pkt->duration) {
342 185282 ds->next_dts += av_rescale_q(pkt->duration, pkt->time_base, AV_TIME_BASE_Q);
343
2/2
✓ Branch 0 taken 49 times.
✓ Branch 1 taken 2243 times.
2292 } else if (ist->par->framerate.num != 0) {
344 49 AVRational field_rate = av_mul_q(ist->par->framerate,
345 49 (AVRational){ 2, 1 });
346 49 int fields = 2;
347
348
1/2
✓ Branch 0 taken 49 times.
✗ Branch 1 not taken.
49 if (ds->codec_desc &&
349
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) &&
350 24 av_stream_get_parser(ist->st))
351 14 fields = 1 + av_stream_get_parser(ist->st)->repeat_pict;
352
353 49 ds->next_dts += av_rescale_q(fields, av_inv_q(field_rate), AV_TIME_BASE_Q);
354 }
355 188317 break;
356 }
357
358 470393 fd->dts_est = ds->dts;
359
360 470393 return 0;
361 }
362
363 470393 static int ts_fixup(Demuxer *d, AVPacket *pkt, FrameData *fd)
364 {
365 470393 InputFile *ifile = &d->f;
366 470393 InputStream *ist = ifile->streams[pkt->stream_index];
367 470393 DemuxStream *ds = ds_from_ist(ist);
368 470393 const int64_t start_time = ifile->start_time_effective;
369 int64_t duration;
370 int ret;
371
372 470393 pkt->time_base = ist->st->time_base;
373
374 #define SHOW_TS_DEBUG(tag_) \
375 if (debug_ts) { \
376 av_log(ist, AV_LOG_INFO, "%s -> ist_index:%d:%d type:%s " \
377 "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s duration:%s duration_time:%s\n", \
378 tag_, ifile->index, pkt->stream_index, \
379 av_get_media_type_string(ist->st->codecpar->codec_type), \
380 av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &pkt->time_base), \
381 av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &pkt->time_base), \
382 av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &pkt->time_base)); \
383 }
384
385
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 470393 times.
470393 SHOW_TS_DEBUG("demuxer");
386
387
4/4
✓ Branch 0 taken 421878 times.
✓ Branch 1 taken 48515 times.
✓ Branch 2 taken 325826 times.
✓ Branch 3 taken 96052 times.
470393 if (!ds->wrap_correction_done && start_time != AV_NOPTS_VALUE &&
388
2/2
✓ Branch 0 taken 375 times.
✓ Branch 1 taken 325451 times.
325826 ist->st->pts_wrap_bits < 64) {
389 int64_t stime, stime2;
390
391 375 stime = av_rescale_q(start_time, AV_TIME_BASE_Q, pkt->time_base);
392 375 stime2= stime + (1ULL<<ist->st->pts_wrap_bits);
393 375 ds->wrap_correction_done = 1;
394
395
5/6
✓ Branch 0 taken 345 times.
✓ Branch 1 taken 30 times.
✓ Branch 2 taken 311 times.
✓ Branch 3 taken 34 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 311 times.
375 if(stime2 > stime && pkt->dts != AV_NOPTS_VALUE && pkt->dts > stime + (1LL<<(ist->st->pts_wrap_bits-1))) {
396 pkt->dts -= 1ULL<<ist->st->pts_wrap_bits;
397 ds->wrap_correction_done = 0;
398 }
399
5/6
✓ Branch 0 taken 345 times.
✓ Branch 1 taken 30 times.
✓ Branch 2 taken 305 times.
✓ Branch 3 taken 40 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 305 times.
375 if(stime2 > stime && pkt->pts != AV_NOPTS_VALUE && pkt->pts > stime + (1LL<<(ist->st->pts_wrap_bits-1))) {
400 pkt->pts -= 1ULL<<ist->st->pts_wrap_bits;
401 ds->wrap_correction_done = 0;
402 }
403 }
404
405
2/2
✓ Branch 0 taken 426822 times.
✓ Branch 1 taken 43571 times.
470393 if (pkt->dts != AV_NOPTS_VALUE)
406 426822 pkt->dts += av_rescale_q(ifile->ts_offset, AV_TIME_BASE_Q, pkt->time_base);
407
2/2
✓ Branch 0 taken 424719 times.
✓ Branch 1 taken 45674 times.
470393 if (pkt->pts != AV_NOPTS_VALUE)
408 424719 pkt->pts += av_rescale_q(ifile->ts_offset, AV_TIME_BASE_Q, pkt->time_base);
409
410
2/2
✓ Branch 0 taken 424719 times.
✓ Branch 1 taken 45674 times.
470393 if (pkt->pts != AV_NOPTS_VALUE)
411 424719 pkt->pts *= ds->ts_scale;
412
2/2
✓ Branch 0 taken 426822 times.
✓ Branch 1 taken 43571 times.
470393 if (pkt->dts != AV_NOPTS_VALUE)
413 426822 pkt->dts *= ds->ts_scale;
414
415 470393 duration = av_rescale_q(d->duration.ts, d->duration.tb, pkt->time_base);
416
2/2
✓ Branch 0 taken 424719 times.
✓ Branch 1 taken 45674 times.
470393 if (pkt->pts != AV_NOPTS_VALUE) {
417 // audio decoders take precedence for estimating total file duration
418
2/2
✓ Branch 0 taken 194424 times.
✓ Branch 1 taken 230295 times.
424719 int64_t pkt_duration = d->have_audio_dec ? 0 : pkt->duration;
419
420 424719 pkt->pts += duration;
421
422 // update max/min pts that will be used to compute total file duration
423 // when using -stream_loop
424
4/4
✓ Branch 0 taken 418091 times.
✓ Branch 1 taken 6628 times.
✓ Branch 2 taken 404856 times.
✓ Branch 3 taken 13235 times.
842810 if (d->max_pts.ts == AV_NOPTS_VALUE ||
425 418091 av_compare_ts(d->max_pts.ts, d->max_pts.tb,
426 418091 pkt->pts + pkt_duration, pkt->time_base) < 0) {
427 411484 d->max_pts = (Timestamp){ .ts = pkt->pts + pkt_duration,
428 411484 .tb = pkt->time_base };
429 }
430
4/4
✓ Branch 0 taken 418091 times.
✓ Branch 1 taken 6628 times.
✓ Branch 2 taken 25 times.
✓ Branch 3 taken 418066 times.
842810 if (d->min_pts.ts == AV_NOPTS_VALUE ||
431 418091 av_compare_ts(d->min_pts.ts, d->min_pts.tb,
432 pkt->pts, pkt->time_base) > 0) {
433 6653 d->min_pts = (Timestamp){ .ts = pkt->pts,
434 6653 .tb = pkt->time_base };
435 }
436 }
437
438
2/2
✓ Branch 0 taken 426822 times.
✓ Branch 1 taken 43571 times.
470393 if (pkt->dts != AV_NOPTS_VALUE)
439 426822 pkt->dts += duration;
440
441
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 470393 times.
470393 SHOW_TS_DEBUG("demuxer+tsfixup");
442
443 // detect and try to correct for timestamp discontinuities
444 470393 ts_discontinuity_process(d, ist, pkt);
445
446 // update estimated/predicted dts
447 470393 ret = ist_dts_update(ds, pkt, fd);
448
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 470393 times.
470393 if (ret < 0)
449 return ret;
450
451 470393 return 0;
452 }
453
454 470393 static int input_packet_process(Demuxer *d, AVPacket *pkt, unsigned *send_flags)
455 {
456 470393 InputFile *f = &d->f;
457 470393 InputStream *ist = f->streams[pkt->stream_index];
458 470393 DemuxStream *ds = ds_from_ist(ist);
459 FrameData *fd;
460 470393 int ret = 0;
461
462 470393 fd = packet_data(pkt);
463
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 470393 times.
470393 if (!fd)
464 return AVERROR(ENOMEM);
465
466 470393 ret = ts_fixup(d, pkt, fd);
467
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 470393 times.
470393 if (ret < 0)
468 return ret;
469
470
2/2
✓ Branch 0 taken 236 times.
✓ Branch 1 taken 470157 times.
470393 if (d->recording_time != INT64_MAX) {
471 236 int64_t start_time = 0;
472
2/2
✓ Branch 0 taken 74 times.
✓ Branch 1 taken 162 times.
236 if (copy_ts) {
473
1/2
✓ Branch 0 taken 74 times.
✗ Branch 1 not taken.
74 start_time += f->start_time != AV_NOPTS_VALUE ? f->start_time : 0;
474
1/2
✓ Branch 0 taken 74 times.
✗ Branch 1 not taken.
74 start_time += start_at_zero ? 0 : f->start_time_effective;
475 }
476
2/2
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 196 times.
236 if (ds->dts >= d->recording_time + start_time)
477 40 *send_flags |= DEMUX_SEND_STREAMCOPY_EOF;
478 }
479
480 470393 ds->data_size += pkt->size;
481 470393 ds->nb_packets++;
482
483 470393 fd->wallclock[LATENCY_PROBE_DEMUX] = av_gettime_relative();
484
485
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 470393 times.
470393 if (debug_ts) {
486 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",
487 f->index, pkt->stream_index,
488 av_get_media_type_string(ist->par->codec_type),
489 av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &pkt->time_base),
490 av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &pkt->time_base),
491 av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &pkt->time_base),
492 av_ts2str(f->ts_offset), av_ts2timestr(f->ts_offset, &AV_TIME_BASE_Q));
493 }
494
495 470393 return 0;
496 }
497
498 216 static void readrate_sleep(Demuxer *d)
499 {
500 216 InputFile *f = &d->f;
501 432 int64_t file_start = copy_ts * (
502
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) +
503
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 216 times.
216 (f->start_time != AV_NOPTS_VALUE ? f->start_time : 0)
504 );
505 216 int64_t initial_burst = AV_TIME_BASE * d->readrate_initial_burst;
506 int resume_warn;
507
508
2/2
✓ Branch 0 taken 216 times.
✓ Branch 1 taken 216 times.
432 for (int i = 0; i < f->nb_streams; i++) {
509 216 InputStream *ist = f->streams[i];
510 216 DemuxStream *ds = ds_from_ist(ist);
511 int64_t stream_ts_offset, pts, now, wc_elapsed, elapsed, lag, max_pts, limit_pts;
512
513
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 216 times.
216 if (ds->discard) continue;
514
515
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);
516 216 pts = av_rescale(ds->dts, 1000000, AV_TIME_BASE);
517 216 now = av_gettime_relative();
518 216 wc_elapsed = now - d->wallclock_start;
519 216 max_pts = stream_ts_offset + initial_burst + wc_elapsed * d->readrate;
520 216 lag = FFMAX(max_pts - pts, 0);
521
4/6
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 215 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 215 times.
216 if ( (!ds->lag && lag > 0.3 * AV_TIME_BASE) || ( lag > ds->lag + 0.3 * AV_TIME_BASE) ) {
522 1 ds->lag = lag;
523 1 ds->resume_wc = now;
524 1 ds->resume_pts = pts;
525 1 av_log_once(ds, AV_LOG_WARNING, AV_LOG_DEBUG, &resume_warn,
526 "Resumed reading at pts %0.3f with rate %0.3f after a lag of %0.3fs\n",
527 1 (float)pts/AV_TIME_BASE, d->readrate_catchup, (float)lag/AV_TIME_BASE);
528 }
529
2/4
✓ Branch 0 taken 216 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 216 times.
216 if (ds->lag && !lag)
530 ds->lag = ds->resume_wc = ds->resume_pts = 0;
531
1/2
✓ Branch 0 taken 216 times.
✗ Branch 1 not taken.
216 if (ds->resume_wc) {
532 216 elapsed = now - ds->resume_wc;
533 216 limit_pts = ds->resume_pts + elapsed * d->readrate_catchup;
534 } else {
535 elapsed = wc_elapsed;
536 limit_pts = max_pts;
537 }
538
539
2/2
✓ Branch 0 taken 215 times.
✓ Branch 1 taken 1 times.
216 if (pts > limit_pts)
540 215 av_usleep(pts - limit_pts);
541 }
542 216 }
543
544 471341 static int do_send(Demuxer *d, DemuxStream *ds, AVPacket *pkt, unsigned flags,
545 const char *pkt_desc)
546 {
547 int ret;
548
549 471341 pkt->stream_index = ds->sch_idx_stream;
550
551 471341 ret = sch_demux_send(d->sch, d->f.index, pkt, flags);
552
2/2
✓ Branch 0 taken 3432 times.
✓ Branch 1 taken 467909 times.
471341 if (ret == AVERROR_EOF) {
553 3432 av_packet_unref(pkt);
554
555 3432 av_log(ds, AV_LOG_VERBOSE, "All consumers of this stream are done\n");
556 3432 ds->finished = 1;
557
558
2/2
✓ Branch 0 taken 3403 times.
✓ Branch 1 taken 29 times.
3432 if (++d->nb_streams_finished == d->nb_streams_used) {
559 3403 av_log(d, AV_LOG_VERBOSE, "All consumers are done\n");
560 3403 return AVERROR_EOF;
561 }
562
2/2
✓ Branch 0 taken 46 times.
✓ Branch 1 taken 467863 times.
467909 } else if (ret < 0) {
563
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 46 times.
46 if (ret != AVERROR_EXIT)
564 av_log(d, AV_LOG_ERROR,
565 "Unable to send %s packet to consumers: %s\n",
566 pkt_desc, av_err2str(ret));
567 46 return ret;
568 }
569
570 467892 return 0;
571 }
572
573 470396 static int demux_send(Demuxer *d, DemuxThreadContext *dt, DemuxStream *ds,
574 AVPacket *pkt, unsigned flags)
575 {
576 470396 InputFile *f = &d->f;
577 int ret;
578
579 // pkt can be NULL only when flushing BSFs
580
3/4
✓ Branch 0 taken 470381 times.
✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 470381 times.
470396 av_assert0(ds->bsf || pkt);
581
582 // send heartbeat for sub2video streams
583
4/6
✓ Branch 0 taken 948 times.
✓ Branch 1 taken 469448 times.
✓ Branch 2 taken 948 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 948 times.
✗ Branch 5 not taken.
470396 if (d->pkt_heartbeat && pkt && pkt->pts != AV_NOPTS_VALUE) {
584
2/2
✓ Branch 0 taken 4332 times.
✓ Branch 1 taken 948 times.
5280 for (int i = 0; i < f->nb_streams; i++) {
585 4332 DemuxStream *ds1 = ds_from_ist(f->streams[i]);
586
587
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)
588 3384 continue;
589
590 948 d->pkt_heartbeat->pts = pkt->pts;
591 948 d->pkt_heartbeat->time_base = pkt->time_base;
592 948 d->pkt_heartbeat->opaque = (void*)(intptr_t)PKT_OPAQUE_SUB_HEARTBEAT;
593
594 948 ret = do_send(d, ds1, d->pkt_heartbeat, 0, "heartbeat");
595
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 948 times.
948 if (ret < 0)
596 return ret;
597 }
598 }
599
600
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 470381 times.
470396 if (ds->bsf) {
601
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 3 times.
15 if (pkt)
602 12 av_packet_rescale_ts(pkt, pkt->time_base, ds->bsf->time_base_in);
603
604 15 ret = av_bsf_send_packet(ds->bsf, pkt);
605
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
15 if (ret < 0) {
606 if (pkt)
607 av_packet_unref(pkt);
608 av_log(ds, AV_LOG_ERROR, "Error submitting a packet for filtering: %s\n",
609 av_err2str(ret));
610 return ret;
611 }
612
613 while (1) {
614 27 ret = av_bsf_receive_packet(ds->bsf, dt->pkt_bsf);
615
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 15 times.
27 if (ret == AVERROR(EAGAIN))
616 12 return 0;
617
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 12 times.
15 else if (ret < 0) {
618
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret != AVERROR_EOF)
619 av_log(ds, AV_LOG_ERROR,
620 "Error applying bitstream filters to a packet: %s\n",
621 av_err2str(ret));
622 3 return ret;
623 }
624
625 12 dt->pkt_bsf->time_base = ds->bsf->time_base_out;
626
627 12 ret = do_send(d, ds, dt->pkt_bsf, 0, "filtered");
628
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (ret < 0) {
629 av_packet_unref(dt->pkt_bsf);
630 return ret;
631 }
632 }
633 } else {
634 470381 ret = do_send(d, ds, pkt, flags, "demuxed");
635
2/2
✓ Branch 0 taken 3449 times.
✓ Branch 1 taken 466932 times.
470381 if (ret < 0)
636 3449 return ret;
637 }
638
639 466932 return 0;
640 }
641
642 3721 static int demux_bsf_flush(Demuxer *d, DemuxThreadContext *dt)
643 {
644 3721 InputFile *f = &d->f;
645 int ret;
646
647
2/2
✓ Branch 0 taken 4222 times.
✓ Branch 1 taken 3721 times.
7943 for (unsigned i = 0; i < f->nb_streams; i++) {
648 4222 DemuxStream *ds = ds_from_ist(f->streams[i]);
649
650
2/2
✓ Branch 0 taken 4219 times.
✓ Branch 1 taken 3 times.
4222 if (!ds->bsf)
651 4219 continue;
652
653 3 ret = demux_send(d, dt, ds, NULL, 0);
654
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;
655
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0) {
656 av_log(ds, AV_LOG_ERROR, "Error flushing BSFs: %s\n",
657 av_err2str(ret));
658 return ret;
659 }
660
661 3 av_bsf_flush(ds->bsf);
662 }
663
664 3721 return 0;
665 }
666
667 7159 static void discard_unused_programs(InputFile *ifile)
668 {
669
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 7159 times.
7201 for (int j = 0; j < ifile->ctx->nb_programs; j++) {
670 42 AVProgram *p = ifile->ctx->programs[j];
671 42 int discard = AVDISCARD_ALL;
672
673
1/2
✓ Branch 0 taken 57 times.
✗ Branch 1 not taken.
57 for (int k = 0; k < p->nb_stream_indexes; k++) {
674 57 DemuxStream *ds = ds_from_ist(ifile->streams[p->stream_index[k]]);
675
676
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 15 times.
57 if (!ds->discard) {
677 42 discard = AVDISCARD_DEFAULT;
678 42 break;
679 }
680 }
681 42 p->discard = discard;
682 }
683 7159 }
684
685 7159 static void thread_set_name(InputFile *f)
686 {
687 char name[16];
688 7159 snprintf(name, sizeof(name), "dmx%d:%s", f->index, f->ctx->iformat->name);
689 7159 ff_thread_setname(name);
690 7159 }
691
692 7159 static void demux_thread_uninit(DemuxThreadContext *dt)
693 {
694 7159 av_packet_free(&dt->pkt_demux);
695 7159 av_packet_free(&dt->pkt_bsf);
696
697 7159 memset(dt, 0, sizeof(*dt));
698 7159 }
699
700 7159 static int demux_thread_init(DemuxThreadContext *dt)
701 {
702 7159 memset(dt, 0, sizeof(*dt));
703
704 7159 dt->pkt_demux = av_packet_alloc();
705
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7159 times.
7159 if (!dt->pkt_demux)
706 return AVERROR(ENOMEM);
707
708 7159 dt->pkt_bsf = av_packet_alloc();
709
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7159 times.
7159 if (!dt->pkt_bsf)
710 return AVERROR(ENOMEM);
711
712 7159 return 0;
713 }
714
715 7159 static int input_thread(void *arg)
716 {
717 7159 Demuxer *d = arg;
718 7159 InputFile *f = &d->f;
719
720 DemuxThreadContext dt;
721
722 7159 int ret = 0;
723
724 7159 ret = demux_thread_init(&dt);
725
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7159 times.
7159 if (ret < 0)
726 goto finish;
727
728 7159 thread_set_name(f);
729
730 7159 discard_unused_programs(f);
731
732 7159 d->read_started = 1;
733 7159 d->wallclock_start = av_gettime_relative();
734
735 476726 while (1) {
736 DemuxStream *ds;
737 483885 unsigned send_flags = 0;
738
739 483885 ret = av_read_frame(f->ctx, dt.pkt_demux);
740
741
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 483884 times.
483885 if (ret == AVERROR(EAGAIN)) {
742 1 av_usleep(10000);
743 9782 continue;
744 }
745
2/2
✓ Branch 0 taken 3721 times.
✓ Branch 1 taken 480163 times.
483884 if (ret < 0) {
746 int ret_bsf;
747
748
2/2
✓ Branch 0 taken 3663 times.
✓ Branch 1 taken 58 times.
3721 if (ret == AVERROR_EOF)
749 3663 av_log(d, AV_LOG_VERBOSE, "EOF while reading input\n");
750 else {
751 58 av_log(d, AV_LOG_ERROR, "Error during demuxing: %s\n",
752 58 av_err2str(ret));
753
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
58 ret = exit_on_error ? ret : 0;
754 }
755
756 3721 ret_bsf = demux_bsf_flush(d, &dt);
757
2/2
✓ Branch 0 taken 58 times.
✓ Branch 1 taken 3663 times.
3721 ret = err_merge(ret == AVERROR_EOF ? 0 : ret, ret_bsf);
758
759
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 3710 times.
3721 if (d->loop) {
760 /* signal looping to our consumers */
761 11 dt.pkt_demux->stream_index = -1;
762 11 ret = sch_demux_send(d->sch, f->index, dt.pkt_demux, 0);
763
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 if (ret >= 0)
764 11 ret = seek_to_start(d, (Timestamp){ .ts = dt.pkt_demux->pts,
765 11 .tb = dt.pkt_demux->time_base });
766
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 if (ret >= 0)
767 11 continue;
768
769 /* fallthrough to the error path */
770 }
771
772 3710 break;
773 }
774
775
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 480163 times.
480163 if (do_pkt_dump) {
776 av_pkt_dump_log2(NULL, AV_LOG_INFO, dt.pkt_demux, do_hex_dump,
777 f->ctx->streams[dt.pkt_demux->stream_index]);
778 }
779
780 /* the following test is needed in case new streams appear
781 dynamically in stream : we ignore them */
782 960326 ds = dt.pkt_demux->stream_index < f->nb_streams ?
783
1/2
✓ Branch 0 taken 480163 times.
✗ Branch 1 not taken.
480163 ds_from_ist(f->streams[dt.pkt_demux->stream_index]) : NULL;
784
5/6
✓ Branch 0 taken 480163 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 472045 times.
✓ Branch 3 taken 8118 times.
✓ Branch 4 taken 1652 times.
✓ Branch 5 taken 470393 times.
480163 if (!ds || ds->discard || ds->finished) {
785 9770 report_new_stream(d, dt.pkt_demux);
786 9770 av_packet_unref(dt.pkt_demux);
787 9770 continue;
788 }
789
790
2/2
✓ Branch 0 taken 99 times.
✓ Branch 1 taken 470294 times.
470393 if (dt.pkt_demux->flags & AV_PKT_FLAG_CORRUPT) {
791 99 av_log(d, exit_on_error ? AV_LOG_FATAL : AV_LOG_WARNING,
792 "corrupt input packet in stream %d\n",
793
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 99 times.
99 dt.pkt_demux->stream_index);
794
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 99 times.
99 if (exit_on_error) {
795 av_packet_unref(dt.pkt_demux);
796 ret = AVERROR_INVALIDDATA;
797 break;
798 }
799 }
800
801 470393 ret = input_packet_process(d, dt.pkt_demux, &send_flags);
802
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 470393 times.
470393 if (ret < 0)
803 break;
804
805
2/2
✓ Branch 0 taken 216 times.
✓ Branch 1 taken 470177 times.
470393 if (d->readrate)
806 216 readrate_sleep(d);
807
808 470393 ret = demux_send(d, &dt, ds, dt.pkt_demux, send_flags);
809
2/2
✓ Branch 0 taken 3449 times.
✓ Branch 1 taken 466944 times.
470393 if (ret < 0)
810 3449 break;
811 }
812
813 // EOF/EXIT is normal termination
814
4/4
✓ Branch 0 taken 3756 times.
✓ Branch 1 taken 3403 times.
✓ Branch 2 taken 3710 times.
✓ Branch 3 taken 46 times.
7159 if (ret == AVERROR_EOF || ret == AVERROR_EXIT)
815 3449 ret = 0;
816
817 3710 finish:
818 7159 demux_thread_uninit(&dt);
819
820 7159 return ret;
821 }
822
823 7159 static void demux_final_stats(Demuxer *d)
824 {
825 7159 InputFile *f = &d->f;
826 7159 uint64_t total_packets = 0, total_size = 0;
827
828 7159 av_log(f, AV_LOG_VERBOSE, "Input file #%d (%s):\n",
829 7159 f->index, f->ctx->url);
830
831
2/2
✓ Branch 0 taken 7717 times.
✓ Branch 1 taken 7159 times.
14876 for (int j = 0; j < f->nb_streams; j++) {
832 7717 InputStream *ist = f->streams[j];
833 7717 DemuxStream *ds = ds_from_ist(ist);
834 7717 enum AVMediaType type = ist->par->codec_type;
835
836
3/4
✓ Branch 0 taken 7426 times.
✓ Branch 1 taken 291 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 7426 times.
7717 if (ds->discard || type == AVMEDIA_TYPE_ATTACHMENT)
837 291 continue;
838
839 7426 total_size += ds->data_size;
840 7426 total_packets += ds->nb_packets;
841
842 7426 av_log(f, AV_LOG_VERBOSE, " Input stream #%d:%d (%s): ",
843 f->index, j, av_get_media_type_string(type));
844 7426 av_log(f, AV_LOG_VERBOSE, "%"PRIu64" packets read (%"PRIu64" bytes); ",
845 ds->nb_packets, ds->data_size);
846
847
2/2
✓ Branch 0 taken 6756 times.
✓ Branch 1 taken 670 times.
7426 if (ds->decoding_needed) {
848 6756 av_log(f, AV_LOG_VERBOSE,
849 "%"PRIu64" frames decoded; %"PRIu64" decode errors",
850 6756 ist->decoder->frames_decoded, ist->decoder->decode_errors);
851
2/2
✓ Branch 0 taken 1206 times.
✓ Branch 1 taken 5550 times.
6756 if (type == AVMEDIA_TYPE_AUDIO)
852 1206 av_log(f, AV_LOG_VERBOSE, " (%"PRIu64" samples)", ist->decoder->samples_decoded);
853 6756 av_log(f, AV_LOG_VERBOSE, "; ");
854 }
855
856 7426 av_log(f, AV_LOG_VERBOSE, "\n");
857 }
858
859 7159 av_log(f, AV_LOG_VERBOSE, " Total: %"PRIu64" packets (%"PRIu64" bytes) demuxed\n",
860 total_packets, total_size);
861 7159 }
862
863 7732 static void ist_free(InputStream **pist)
864 {
865 7732 InputStream *ist = *pist;
866 DemuxStream *ds;
867
868
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7732 times.
7732 if (!ist)
869 return;
870 7732 ds = ds_from_ist(ist);
871
872 7732 dec_free(&ist->decoder);
873
874 7732 av_dict_free(&ds->decoder_opts);
875 7732 av_freep(&ist->filters);
876 7732 av_freep(&ds->dec_opts.hwaccel_device);
877
878 7732 avcodec_parameters_free(&ist->par);
879
880 7732 av_frame_free(&ds->decoded_params);
881
882 7732 av_bsf_free(&ds->bsf);
883
884 7732 av_freep(pist);
885 }
886
887 7173 void ifile_close(InputFile **pf)
888 {
889 7173 InputFile *f = *pf;
890 7173 Demuxer *d = demuxer_from_ifile(f);
891
892
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7173 times.
7173 if (!f)
893 return;
894
895
2/2
✓ Branch 0 taken 7159 times.
✓ Branch 1 taken 14 times.
7173 if (d->read_started)
896 7159 demux_final_stats(d);
897
898
2/2
✓ Branch 0 taken 7732 times.
✓ Branch 1 taken 7173 times.
14905 for (int i = 0; i < f->nb_streams; i++)
899 7732 ist_free(&f->streams[i]);
900 7173 av_freep(&f->streams);
901
902 7173 avformat_close_input(&f->ctx);
903
904 7173 av_packet_free(&d->pkt_heartbeat);
905
906 7173 av_freep(pf);
907 }
908
909 7516 int ist_use(InputStream *ist, int decoding_needed,
910 const ViewSpecifier *vs, SchedulerNode *src)
911 {
912 7516 Demuxer *d = demuxer_from_ifile(ist->file);
913 7516 DemuxStream *ds = ds_from_ist(ist);
914 int ret;
915
916
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7516 times.
7516 if (ist->user_set_discard == AVDISCARD_ALL) {
917 av_log(ist, AV_LOG_ERROR, "Cannot %s a disabled input stream\n",
918 decoding_needed ? "decode" : "streamcopy");
919 return AVERROR(EINVAL);
920 }
921
922
3/4
✓ Branch 0 taken 6823 times.
✓ Branch 1 taken 693 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6823 times.
7516 if (decoding_needed && !ist->dec) {
923 av_log(ist, AV_LOG_ERROR,
924 "Decoding requested, but no decoder found for: %s\n",
925 avcodec_get_name(ist->par->codec_id));
926 return AVERROR(EINVAL);
927 }
928
929
2/2
✓ Branch 0 taken 7426 times.
✓ Branch 1 taken 90 times.
7516 if (ds->sch_idx_stream < 0) {
930 7426 ret = sch_add_demux_stream(d->sch, d->f.index);
931
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7426 times.
7426 if (ret < 0)
932 return ret;
933 7426 ds->sch_idx_stream = ret;
934 }
935
936
2/2
✓ Branch 0 taken 7426 times.
✓ Branch 1 taken 90 times.
7516 if (ds->discard) {
937 7426 ds->discard = 0;
938 7426 d->nb_streams_used++;
939 }
940
941 7516 ist->st->discard = ist->user_set_discard;
942 7516 ds->decoding_needed |= decoding_needed;
943 7516 ds->streamcopy_needed |= !decoding_needed;
944
945
4/4
✓ Branch 0 taken 6823 times.
✓ Branch 1 taken 693 times.
✓ Branch 2 taken 6756 times.
✓ Branch 3 taken 67 times.
7516 if (decoding_needed && ds->sch_idx_dec < 0) {
946 6756 int is_audio = ist->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO;
947
948 13512 ds->dec_opts.flags |= (!!ist->fix_sub_duration * DECODER_FLAG_FIX_SUB_DURATION) |
949 13512 (!!(d->f.ctx->iformat->flags & AVFMT_NOTIMESTAMPS) * DECODER_FLAG_TS_UNRELIABLE) |
950
4/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6754 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
6756 (!!(d->loop && is_audio) * DECODER_FLAG_SEND_END_TS)
951 #if FFMPEG_OPT_TOP
952
2/2
✓ Branch 0 taken 1246 times.
✓ Branch 1 taken 5510 times.
6756 | ((ist->top_field_first >= 0) * DECODER_FLAG_TOP_FIELD_FIRST)
953 #endif
954 ;
955
956
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 6724 times.
6756 if (ist->framerate.num) {
957 32 ds->dec_opts.flags |= DECODER_FLAG_FRAMERATE_FORCED;
958 32 ds->dec_opts.framerate = ist->framerate;
959 } else
960 6724 ds->dec_opts.framerate = ist->st->avg_frame_rate;
961
962
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6755 times.
6756 if (ist->dec->id == AV_CODEC_ID_DVB_SUBTITLE &&
963
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 (ds->decoding_needed & DECODING_FOR_OST)) {
964 1 av_dict_set(&ds->decoder_opts, "compute_edt", "1", AV_DICT_DONT_OVERWRITE);
965
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ds->decoding_needed & DECODING_FOR_FILTER)
966 av_log(ist, AV_LOG_WARNING,
967 "Warning using DVB subtitles for filtering and output at the "
968 "same time is not fully supported, also see -compute_edt [0|1]\n");
969 }
970
971 6756 snprintf(ds->dec_name, sizeof(ds->dec_name), "%d:%d", ist->file->index, ist->index);
972 6756 ds->dec_opts.name = ds->dec_name;
973
974 6756 ds->dec_opts.codec = ist->dec;
975 6756 ds->dec_opts.par = ist->par;
976
977 6756 ds->dec_opts.log_parent = ist;
978
979 6756 ds->decoded_params = av_frame_alloc();
980
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6756 times.
6756 if (!ds->decoded_params)
981 return AVERROR(ENOMEM);
982
983 6756 ret = dec_init(&ist->decoder, d->sch,
984 6756 &ds->decoder_opts, &ds->dec_opts, ds->decoded_params);
985
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6756 times.
6756 if (ret < 0)
986 return ret;
987 6756 ds->sch_idx_dec = ret;
988
989 6756 ret = sch_connect(d->sch, SCH_DSTREAM(d->f.index, ds->sch_idx_stream),
990 6756 SCH_DEC_IN(ds->sch_idx_dec));
991
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6756 times.
6756 if (ret < 0)
992 return ret;
993
994 6756 d->have_audio_dec |= is_audio;
995 }
996
997
4/4
✓ Branch 0 taken 6823 times.
✓ Branch 1 taken 693 times.
✓ Branch 2 taken 5520 times.
✓ Branch 3 taken 1303 times.
7516 if (decoding_needed && ist->par->codec_type == AVMEDIA_TYPE_VIDEO) {
998 5520 ret = dec_request_view(ist->decoder, vs, src);
999
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5520 times.
5520 if (ret < 0)
1000 return ret;
1001 } else {
1002 1996 *src = decoding_needed ?
1003
2/2
✓ Branch 0 taken 1303 times.
✓ Branch 1 taken 693 times.
1996 SCH_DEC_OUT(ds->sch_idx_dec, 0) :
1004 693 SCH_DSTREAM(d->f.index, ds->sch_idx_stream);
1005 }
1006
1007 7516 return 0;
1008 }
1009
1010 6785 int ist_filter_add(InputStream *ist, InputFilter *ifilter, int is_simple,
1011 const ViewSpecifier *vs, InputFilterOptions *opts,
1012 SchedulerNode *src)
1013 {
1014 6785 Demuxer *d = demuxer_from_ifile(ist->file);
1015 6785 DemuxStream *ds = ds_from_ist(ist);
1016 6785 int64_t tsoffset = 0;
1017 int ret;
1018
1019
2/2
✓ Branch 0 taken 6636 times.
✓ Branch 1 taken 149 times.
6785 ret = ist_use(ist, is_simple ? DECODING_FOR_OST : DECODING_FOR_FILTER,
1020 vs, src);
1021
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6785 times.
6785 if (ret < 0)
1022 return ret;
1023
1024 6785 ret = GROW_ARRAY(ist->filters, ist->nb_filters);
1025
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6785 times.
6785 if (ret < 0)
1026 return ret;
1027
1028 6785 ist->filters[ist->nb_filters - 1] = ifilter;
1029
1030
2/2
✓ Branch 0 taken 5520 times.
✓ Branch 1 taken 1265 times.
6785 if (ist->par->codec_type == AVMEDIA_TYPE_VIDEO) {
1031 5520 const AVPacketSideData *sd = av_packet_side_data_get(ist->par->coded_side_data,
1032 5520 ist->par->nb_coded_side_data,
1033 AV_PKT_DATA_FRAME_CROPPING);
1034
3/4
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 5488 times.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
5520 if (ist->framerate.num > 0 && ist->framerate.den > 0) {
1035 32 opts->framerate = ist->framerate;
1036 32 opts->flags |= IFILTER_FLAG_CFR;
1037 } else
1038 5488 opts->framerate = av_guess_frame_rate(d->f.ctx, ist->st, NULL);
1039
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 5516 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
5520 if (sd && sd->size >= sizeof(uint32_t) * 4) {
1040 4 opts->crop_top = AV_RL32(sd->data + 0);
1041 4 opts->crop_bottom = AV_RL32(sd->data + 4);
1042 4 opts->crop_left = AV_RL32(sd->data + 8);
1043 4 opts->crop_right = AV_RL32(sd->data + 12);
1044
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 if (ds->apply_cropping && ds->apply_cropping != CROP_CODEC &&
1045
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 (opts->crop_top | opts->crop_bottom | opts->crop_left | opts->crop_right))
1046 4 opts->flags |= IFILTER_FLAG_CROP;
1047 }
1048
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1261 times.
1265 } else if (ist->par->codec_type == AVMEDIA_TYPE_SUBTITLE) {
1049 /* Compute the size of the canvas for the subtitles stream.
1050 If the subtitles codecpar has set a size, use it. Otherwise use the
1051 maximum dimensions of the video streams in the same file. */
1052 4 opts->sub2video_width = ist->par->width;
1053 4 opts->sub2video_height = ist->par->height;
1054
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)) {
1055 for (int j = 0; j < d->f.nb_streams; j++) {
1056 AVCodecParameters *par1 = d->f.streams[j]->par;
1057 if (par1->codec_type == AVMEDIA_TYPE_VIDEO) {
1058 opts->sub2video_width = FFMAX(opts->sub2video_width, par1->width);
1059 opts->sub2video_height = FFMAX(opts->sub2video_height, par1->height);
1060 }
1061 }
1062 }
1063
1064
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)) {
1065 opts->sub2video_width = FFMAX(opts->sub2video_width, 720);
1066 opts->sub2video_height = FFMAX(opts->sub2video_height, 576);
1067 }
1068
1069
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (!d->pkt_heartbeat) {
1070 4 d->pkt_heartbeat = av_packet_alloc();
1071
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!d->pkt_heartbeat)
1072 return AVERROR(ENOMEM);
1073 }
1074 4 ds->have_sub2video = 1;
1075 }
1076
1077 6785 ret = av_frame_copy_props(opts->fallback, ds->decoded_params);
1078
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6785 times.
6785 if (ret < 0)
1079 return ret;
1080 6785 opts->fallback->format = ds->decoded_params->format;
1081 6785 opts->fallback->width = ds->decoded_params->width;
1082 6785 opts->fallback->height = ds->decoded_params->height;
1083
1084 6785 ret = av_channel_layout_copy(&opts->fallback->ch_layout, &ds->decoded_params->ch_layout);
1085
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6785 times.
6785 if (ret < 0)
1086 return ret;
1087
1088
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 6782 times.
6785 if (copy_ts) {
1089
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 tsoffset = d->f.start_time == AV_NOPTS_VALUE ? 0 : d->f.start_time;
1090
3/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 1 times.
3 if (!start_at_zero && d->f.ctx->start_time != AV_NOPTS_VALUE)
1091 2 tsoffset += d->f.ctx->start_time;
1092 }
1093
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 opts->trim_start_us = ((d->f.start_time == AV_NOPTS_VALUE) || !d->accurate_seek) ?
1094
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 6769 times.
6801 AV_NOPTS_VALUE : tsoffset;
1095 6785 opts->trim_end_us = d->recording_time;
1096
1097 6785 opts->name = av_strdup(ds->dec_name);
1098
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6785 times.
6785 if (!opts->name)
1099 return AVERROR(ENOMEM);
1100
1101 13570 opts->flags |= IFILTER_FLAG_AUTOROTATE * !!(ds->autorotate) |
1102
1/2
✓ Branch 0 taken 6785 times.
✗ Branch 1 not taken.
6785 IFILTER_FLAG_REINIT * !!(ds->reinit_filters);
1103
1104 6785 return 0;
1105 }
1106
1107 15314 static int choose_decoder(const OptionsContext *o, void *logctx,
1108 AVFormatContext *s, AVStream *st,
1109 enum HWAccelID hwaccel_id, enum AVHWDeviceType hwaccel_device_type,
1110 const AVCodec **pcodec)
1111
1112 {
1113 15314 const char *codec_name = NULL;
1114
1115 15314 opt_match_per_stream_str(logctx, &o->codec_names, s, st, &codec_name);
1116
2/2
✓ Branch 0 taken 6123 times.
✓ Branch 1 taken 9191 times.
15314 if (codec_name) {
1117 6123 int ret = find_codec(NULL, codec_name, st->codecpar->codec_type, 0, pcodec);
1118
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6123 times.
6123 if (ret < 0)
1119 return ret;
1120 6123 st->codecpar->codec_id = (*pcodec)->id;
1121
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 6123 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
6123 if (recast_media && st->codecpar->codec_type != (*pcodec)->type)
1122 st->codecpar->codec_type = (*pcodec)->type;
1123 6123 return 0;
1124 } else {
1125
3/4
✓ Branch 0 taken 5706 times.
✓ Branch 1 taken 3485 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 5706 times.
9191 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
1126 hwaccel_id == HWACCEL_GENERIC &&
1127 hwaccel_device_type != AV_HWDEVICE_TYPE_NONE) {
1128 const AVCodec *c;
1129 void *i = NULL;
1130
1131 while ((c = av_codec_iterate(&i))) {
1132 const AVCodecHWConfig *config;
1133
1134 if (c->id != st->codecpar->codec_id ||
1135 !av_codec_is_decoder(c))
1136 continue;
1137
1138 for (int j = 0; config = avcodec_get_hw_config(c, j); j++) {
1139 if (config->device_type == hwaccel_device_type) {
1140 av_log(logctx, AV_LOG_VERBOSE, "Selecting decoder '%s' because of requested hwaccel method %s\n",
1141 c->name, av_hwdevice_get_type_name(hwaccel_device_type));
1142 *pcodec = c;
1143 return 0;
1144 }
1145 }
1146 }
1147 }
1148
1149 9191 *pcodec = avcodec_find_decoder(st->codecpar->codec_id);
1150 9191 return 0;
1151 }
1152 }
1153
1154 1674 static int guess_input_channel_layout(InputStream *ist, AVCodecParameters *par,
1155 int guess_layout_max)
1156 {
1157
2/2
✓ Branch 0 taken 773 times.
✓ Branch 1 taken 901 times.
1674 if (par->ch_layout.order == AV_CHANNEL_ORDER_UNSPEC) {
1158 char layout_name[256];
1159
1160
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 769 times.
773 if (par->ch_layout.nb_channels > guess_layout_max)
1161 14 return 0;
1162 769 av_channel_layout_default(&par->ch_layout, par->ch_layout.nb_channels);
1163
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 759 times.
769 if (par->ch_layout.order == AV_CHANNEL_ORDER_UNSPEC)
1164 10 return 0;
1165 759 av_channel_layout_describe(&par->ch_layout, layout_name, sizeof(layout_name));
1166 759 av_log(ist, AV_LOG_WARNING, "Guessed Channel Layout: %s\n", layout_name);
1167 }
1168 1660 return 1;
1169 }
1170
1171 5911 static int add_display_matrix_to_stream(const OptionsContext *o,
1172 AVFormatContext *ctx, InputStream *ist)
1173 {
1174 5911 AVStream *st = ist->st;
1175 AVPacketSideData *sd;
1176 5911 double rotation = DBL_MAX;
1177 5911 int hflip = -1, vflip = -1;
1178 5911 int hflip_set = 0, vflip_set = 0, rotation_set = 0;
1179 int32_t *buf;
1180
1181 5911 opt_match_per_stream_dbl(ist, &o->display_rotations, ctx, st, &rotation);
1182 5911 opt_match_per_stream_int(ist, &o->display_hflips, ctx, st, &hflip);
1183 5911 opt_match_per_stream_int(ist, &o->display_vflips, ctx, st, &vflip);
1184
1185 5911 rotation_set = rotation != DBL_MAX;
1186 5911 hflip_set = hflip != -1;
1187 5911 vflip_set = vflip != -1;
1188
1189
4/6
✓ Branch 0 taken 5910 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 5910 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 5910 times.
✗ Branch 5 not taken.
5911 if (!rotation_set && !hflip_set && !vflip_set)
1190 5910 return 0;
1191
1192 1 sd = av_packet_side_data_new(&st->codecpar->coded_side_data,
1193 1 &st->codecpar->nb_coded_side_data,
1194 AV_PKT_DATA_DISPLAYMATRIX,
1195 sizeof(int32_t) * 9, 0);
1196
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!sd) {
1197 av_log(ist, AV_LOG_FATAL, "Failed to generate a display matrix!\n");
1198 return AVERROR(ENOMEM);
1199 }
1200
1201 1 buf = (int32_t *)sd->data;
1202
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
2 av_display_rotation_set(buf,
1203 1 rotation_set ? -(rotation) : -0.0f);
1204
1205
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 av_display_matrix_flip(buf,
1206 hflip_set ? hflip : 0,
1207 vflip_set ? vflip : 0);
1208
1209 1 return 0;
1210 }
1211
1212 1436 static const char *input_stream_item_name(void *obj)
1213 {
1214 1436 const DemuxStream *ds = obj;
1215
1216 1436 return ds->log_name;
1217 }
1218
1219 static const AVClass input_stream_class = {
1220 .class_name = "InputStream",
1221 .version = LIBAVUTIL_VERSION_INT,
1222 .item_name = input_stream_item_name,
1223 .category = AV_CLASS_CATEGORY_DEMUXER,
1224 };
1225
1226 7732 static DemuxStream *demux_stream_alloc(Demuxer *d, AVStream *st)
1227 {
1228 7732 const char *type_str = av_get_media_type_string(st->codecpar->codec_type);
1229 7732 InputFile *f = &d->f;
1230 DemuxStream *ds;
1231
1232 7732 ds = allocate_array_elem(&f->streams, sizeof(*ds), &f->nb_streams);
1233
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7732 times.
7732 if (!ds)
1234 return NULL;
1235
1236 7732 ds->sch_idx_stream = -1;
1237 7732 ds->sch_idx_dec = -1;
1238
1239 7732 ds->ist.st = st;
1240 7732 ds->ist.file = f;
1241 7732 ds->ist.index = st->index;
1242 7732 ds->ist.class = &input_stream_class;
1243
1244
1/2
✓ Branch 0 taken 7732 times.
✗ Branch 1 not taken.
7732 snprintf(ds->log_name, sizeof(ds->log_name), "%cist#%d:%d/%s",
1245 7732 type_str ? *type_str : '?', d->f.index, st->index,
1246 7732 avcodec_get_name(st->codecpar->codec_id));
1247
1248 7732 return ds;
1249 }
1250
1251 7732 static int ist_add(const OptionsContext *o, Demuxer *d, AVStream *st, AVDictionary **opts_used)
1252 {
1253 7732 AVFormatContext *ic = d->f.ctx;
1254 7732 AVCodecParameters *par = st->codecpar;
1255 DemuxStream *ds;
1256 InputStream *ist;
1257 7732 const char *framerate = NULL, *hwaccel_device = NULL;
1258 7732 const char *hwaccel = NULL;
1259 7732 const char *apply_cropping = NULL;
1260 7732 const char *hwaccel_output_format = NULL;
1261 7732 const char *codec_tag = NULL;
1262 7732 const char *bsfs = NULL;
1263 char *next;
1264 7732 const char *discard_str = NULL;
1265 int ret;
1266
1267 7732 ds = demux_stream_alloc(d, st);
1268
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7732 times.
7732 if (!ds)
1269 return AVERROR(ENOMEM);
1270
1271 7732 ist = &ds->ist;
1272
1273 7732 ds->discard = 1;
1274 7732 st->discard = AVDISCARD_ALL;
1275 7732 ds->first_dts = AV_NOPTS_VALUE;
1276 7732 ds->next_dts = AV_NOPTS_VALUE;
1277
1278 7732 ds->dec_opts.time_base = st->time_base;
1279
1280 7732 ds->ts_scale = 1.0;
1281 7732 opt_match_per_stream_dbl(ist, &o->ts_scale, ic, st, &ds->ts_scale);
1282
1283 7732 ds->autorotate = 1;
1284 7732 opt_match_per_stream_int(ist, &o->autorotate, ic, st, &ds->autorotate);
1285
1286 7732 ds->apply_cropping = CROP_ALL;
1287 7732 opt_match_per_stream_str(ist, &o->apply_cropping, ic, st, &apply_cropping);
1288
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7732 times.
7732 if (apply_cropping) {
1289 const AVOption opts[] = {
1290 { "apply_cropping", NULL, 0, AV_OPT_TYPE_INT,
1291 { .i64 = CROP_ALL }, CROP_DISABLED, CROP_CONTAINER, AV_OPT_FLAG_DECODING_PARAM, .unit = "apply_cropping" },
1292 { "none", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CROP_DISABLED }, .unit = "apply_cropping" },
1293 { "all", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CROP_ALL }, .unit = "apply_cropping" },
1294 { "codec", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CROP_CODEC }, .unit = "apply_cropping" },
1295 { "container", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CROP_CONTAINER }, .unit = "apply_cropping" },
1296 { NULL },
1297 };
1298 const AVClass class = {
1299 .class_name = "apply_cropping",
1300 .item_name = av_default_item_name,
1301 .option = opts,
1302 .version = LIBAVUTIL_VERSION_INT,
1303 };
1304 const AVClass *pclass = &class;
1305
1306 ret = av_opt_eval_int(&pclass, opts, apply_cropping, &ds->apply_cropping);
1307 if (ret < 0) {
1308 av_log(ist, AV_LOG_ERROR, "Invalid apply_cropping value '%s'.\n", apply_cropping);
1309 return ret;
1310 }
1311 }
1312
1313 7732 opt_match_per_stream_str(ist, &o->codec_tags, ic, st, &codec_tag);
1314
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 7730 times.
7732 if (codec_tag) {
1315 2 uint32_t tag = strtol(codec_tag, &next, 0);
1316
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (*next) {
1317 2 uint8_t buf[4] = { 0 };
1318
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 memcpy(buf, codec_tag, FFMIN(sizeof(buf), strlen(codec_tag)));
1319 2 tag = AV_RL32(buf);
1320 }
1321
1322 2 st->codecpar->codec_tag = tag;
1323 }
1324
1325
2/2
✓ Branch 0 taken 5911 times.
✓ Branch 1 taken 1821 times.
7732 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
1326 5911 ret = add_display_matrix_to_stream(o, ic, ist);
1327
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5911 times.
5911 if (ret < 0)
1328 return ret;
1329
1330 5911 opt_match_per_stream_str(ist, &o->hwaccels, ic, st, &hwaccel);
1331 5911 opt_match_per_stream_str(ist, &o->hwaccel_output_formats, ic, st,
1332 &hwaccel_output_format);
1333
4/6
✓ Branch 0 taken 5911 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5599 times.
✓ Branch 3 taken 312 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 5599 times.
5911 if (!hwaccel_output_format && hwaccel && !strcmp(hwaccel, "cuvid")) {
1334 av_log(ist, AV_LOG_WARNING,
1335 "WARNING: defaulting hwaccel_output_format to cuda for compatibility "
1336 "with old commandlines. This behaviour is DEPRECATED and will be removed "
1337 "in the future. Please explicitly set \"-hwaccel_output_format cuda\".\n");
1338 ds->dec_opts.hwaccel_output_format = AV_PIX_FMT_CUDA;
1339
4/6
✓ Branch 0 taken 5911 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5599 times.
✓ Branch 3 taken 312 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 5599 times.
5911 } else if (!hwaccel_output_format && hwaccel && !strcmp(hwaccel, "qsv")) {
1340 av_log(ist, AV_LOG_WARNING,
1341 "WARNING: defaulting hwaccel_output_format to qsv for compatibility "
1342 "with old commandlines. This behaviour is DEPRECATED and will be removed "
1343 "in the future. Please explicitly set \"-hwaccel_output_format qsv\".\n");
1344 ds->dec_opts.hwaccel_output_format = AV_PIX_FMT_QSV;
1345
4/6
✓ Branch 0 taken 5911 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5599 times.
✓ Branch 3 taken 312 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 5599 times.
5911 } else if (!hwaccel_output_format && hwaccel && !strcmp(hwaccel, "mediacodec")) {
1346 // There is no real AVHWFrameContext implementation. Set
1347 // hwaccel_output_format to avoid av_hwframe_transfer_data error.
1348 ds->dec_opts.hwaccel_output_format = AV_PIX_FMT_MEDIACODEC;
1349
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5911 times.
5911 } else if (hwaccel_output_format) {
1350 ds->dec_opts.hwaccel_output_format = av_get_pix_fmt(hwaccel_output_format);
1351 if (ds->dec_opts.hwaccel_output_format == AV_PIX_FMT_NONE) {
1352 av_log(ist, AV_LOG_FATAL, "Unrecognised hwaccel output "
1353 "format: %s", hwaccel_output_format);
1354 }
1355 } else {
1356 5911 ds->dec_opts.hwaccel_output_format = AV_PIX_FMT_NONE;
1357 }
1358
1359
2/2
✓ Branch 0 taken 5599 times.
✓ Branch 1 taken 312 times.
5911 if (hwaccel) {
1360 // The NVDEC hwaccels use a CUDA device, so remap the name here.
1361
2/4
✓ Branch 0 taken 5599 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5599 times.
5599 if (!strcmp(hwaccel, "nvdec") || !strcmp(hwaccel, "cuvid"))
1362 hwaccel = "cuda";
1363
1364
1/2
✓ Branch 0 taken 5599 times.
✗ Branch 1 not taken.
5599 if (!strcmp(hwaccel, "none"))
1365 5599 ds->dec_opts.hwaccel_id = HWACCEL_NONE;
1366 else if (!strcmp(hwaccel, "auto"))
1367 ds->dec_opts.hwaccel_id = HWACCEL_AUTO;
1368 else {
1369 enum AVHWDeviceType type = av_hwdevice_find_type_by_name(hwaccel);
1370 if (type != AV_HWDEVICE_TYPE_NONE) {
1371 ds->dec_opts.hwaccel_id = HWACCEL_GENERIC;
1372 ds->dec_opts.hwaccel_device_type = type;
1373 }
1374
1375 if (!ds->dec_opts.hwaccel_id) {
1376 av_log(ist, AV_LOG_FATAL, "Unrecognized hwaccel: %s.\n",
1377 hwaccel);
1378 av_log(ist, AV_LOG_FATAL, "Supported hwaccels: ");
1379 type = AV_HWDEVICE_TYPE_NONE;
1380 while ((type = av_hwdevice_iterate_types(type)) !=
1381 AV_HWDEVICE_TYPE_NONE)
1382 av_log(ist, AV_LOG_FATAL, "%s ",
1383 av_hwdevice_get_type_name(type));
1384 av_log(ist, AV_LOG_FATAL, "\n");
1385 return AVERROR(EINVAL);
1386 }
1387 }
1388 }
1389
1390 5911 opt_match_per_stream_str(ist, &o->hwaccel_devices, ic, st, &hwaccel_device);
1391
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5911 times.
5911 if (hwaccel_device) {
1392 ds->dec_opts.hwaccel_device = av_strdup(hwaccel_device);
1393 if (!ds->dec_opts.hwaccel_device)
1394 return AVERROR(ENOMEM);
1395 }
1396 }
1397
1398 7732 ret = choose_decoder(o, ist, ic, st, ds->dec_opts.hwaccel_id,
1399 ds->dec_opts.hwaccel_device_type, &ist->dec);
1400
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7732 times.
7732 if (ret < 0)
1401 return ret;
1402
1403
2/2
✓ Branch 0 taken 7656 times.
✓ Branch 1 taken 76 times.
7732 if (ist->dec) {
1404 7656 ret = filter_codec_opts(o->g->codec_opts, ist->st->codecpar->codec_id,
1405 ic, st, ist->dec, &ds->decoder_opts, opts_used);
1406
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7656 times.
7656 if (ret < 0)
1407 return ret;
1408 }
1409
1410 7732 ds->reinit_filters = -1;
1411 7732 opt_match_per_stream_int(ist, &o->reinit_filters, ic, st, &ds->reinit_filters);
1412
1413 7732 ist->user_set_discard = AVDISCARD_NONE;
1414
1415
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 7732 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
7732 if ((o->video_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) ||
1416
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7731 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
7732 (o->audio_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) ||
1417
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 7731 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
7731 (o->subtitle_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) ||
1418
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 7731 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
7731 (o->data_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_DATA))
1419 1 ist->user_set_discard = AVDISCARD_ALL;
1420
1421 7732 opt_match_per_stream_str(ist, &o->discard, ic, st, &discard_str);
1422
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7732 times.
7732 if (discard_str) {
1423 ret = av_opt_set(ist->st, "discard", discard_str, 0);
1424 if (ret < 0) {
1425 av_log(ist, AV_LOG_ERROR, "Error parsing discard %s.\n", discard_str);
1426 return ret;
1427 }
1428 ist->user_set_discard = ist->st->discard;
1429 }
1430
1431
2/2
✓ Branch 0 taken 95 times.
✓ Branch 1 taken 7637 times.
7732 ds->dec_opts.flags |= DECODER_FLAG_BITEXACT * !!o->bitexact;
1432
1433 7732 av_dict_set_int(&ds->decoder_opts, "apply_cropping",
1434
2/4
✓ Branch 0 taken 7732 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7732 times.
✗ Branch 3 not taken.
7732 ds->apply_cropping && ds->apply_cropping != CROP_CONTAINER, 0);
1435
1436 /* Attached pics are sparse, therefore we would not want to delay their decoding
1437 * till EOF. */
1438
2/2
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 7695 times.
7732 if (ist->st->disposition & AV_DISPOSITION_ATTACHED_PIC)
1439 37 av_dict_set(&ds->decoder_opts, "thread_type", "-frame", 0);
1440
1441
4/5
✓ Branch 0 taken 5911 times.
✓ Branch 1 taken 1675 times.
✓ Branch 2 taken 144 times.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
7732 switch (par->codec_type) {
1442 5911 case AVMEDIA_TYPE_VIDEO:
1443 5911 opt_match_per_stream_str(ist, &o->frame_rates, ic, st, &framerate);
1444
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 5878 times.
5911 if (framerate) {
1445 33 ret = av_parse_video_rate(&ist->framerate, framerate);
1446
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 if (ret < 0) {
1447 av_log(ist, AV_LOG_ERROR, "Error parsing framerate %s.\n",
1448 framerate);
1449 return ret;
1450 }
1451 }
1452
1453 #if FFMPEG_OPT_TOP
1454 5911 ist->top_field_first = -1;
1455 5911 opt_match_per_stream_int(ist, &o->top_field_first, ic, st, &ist->top_field_first);
1456 #endif
1457
1458 5911 break;
1459 1675 case AVMEDIA_TYPE_AUDIO: {
1460 1675 const char *ch_layout_str = NULL;
1461
1462 1675 opt_match_per_stream_str(ist, &o->audio_ch_layouts, ic, st, &ch_layout_str);
1463
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1674 times.
1675 if (ch_layout_str) {
1464 AVChannelLayout ch_layout;
1465 1 ret = av_channel_layout_from_string(&ch_layout, ch_layout_str);
1466
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0) {
1467 av_log(ist, AV_LOG_ERROR, "Error parsing channel layout %s.\n", ch_layout_str);
1468 return ret;
1469 }
1470
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 if (par->ch_layout.nb_channels <= 0 || par->ch_layout.nb_channels == ch_layout.nb_channels) {
1471 1 av_channel_layout_uninit(&par->ch_layout);
1472 1 par->ch_layout = ch_layout;
1473 } else {
1474 av_log(ist, AV_LOG_ERROR,
1475 "Specified channel layout '%s' has %d channels, but input has %d channels.\n",
1476 ch_layout_str, ch_layout.nb_channels, par->ch_layout.nb_channels);
1477 av_channel_layout_uninit(&ch_layout);
1478 return AVERROR(EINVAL);
1479 }
1480 } else {
1481 1674 int guess_layout_max = INT_MAX;
1482 1674 opt_match_per_stream_int(ist, &o->guess_layout_max, ic, st, &guess_layout_max);
1483 1674 guess_input_channel_layout(ist, par, guess_layout_max);
1484 }
1485 1675 break;
1486 }
1487 144 case AVMEDIA_TYPE_DATA:
1488 case AVMEDIA_TYPE_SUBTITLE: {
1489 144 const char *canvas_size = NULL;
1490
1491 144 opt_match_per_stream_int(ist, &o->fix_sub_duration, ic, st, &ist->fix_sub_duration);
1492 144 opt_match_per_stream_str(ist, &o->canvas_sizes, ic, st, &canvas_size);
1493
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 144 times.
144 if (canvas_size) {
1494 ret = av_parse_video_size(&par->width, &par->height,
1495 canvas_size);
1496 if (ret < 0) {
1497 av_log(ist, AV_LOG_FATAL, "Invalid canvas size: %s.\n", canvas_size);
1498 return ret;
1499 }
1500 }
1501 144 break;
1502 }
1503 2 case AVMEDIA_TYPE_ATTACHMENT:
1504 case AVMEDIA_TYPE_UNKNOWN:
1505 2 break;
1506 default: av_assert0(0);
1507 }
1508
1509 7732 ist->par = avcodec_parameters_alloc();
1510
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7732 times.
7732 if (!ist->par)
1511 return AVERROR(ENOMEM);
1512
1513 7732 ret = avcodec_parameters_copy(ist->par, par);
1514
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7732 times.
7732 if (ret < 0) {
1515 av_log(ist, AV_LOG_ERROR, "Error exporting stream parameters.\n");
1516 return ret;
1517 }
1518
1519
2/2
✓ Branch 0 taken 430 times.
✓ Branch 1 taken 7302 times.
7732 if (ist->st->sample_aspect_ratio.num)
1520 430 ist->par->sample_aspect_ratio = ist->st->sample_aspect_ratio;
1521
1522 7732 opt_match_per_stream_str(ist, &o->bitstream_filters, ic, st, &bsfs);
1523
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7731 times.
7732 if (bsfs) {
1524 1 ret = av_bsf_list_parse_str(bsfs, &ds->bsf);
1525
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0) {
1526 av_log(ist, AV_LOG_ERROR,
1527 "Error parsing bitstream filter sequence '%s': %s\n",
1528 bsfs, av_err2str(ret));
1529 return ret;
1530 }
1531
1532 1 ret = avcodec_parameters_copy(ds->bsf->par_in, ist->par);
1533
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
1534 return ret;
1535 1 ds->bsf->time_base_in = ist->st->time_base;
1536
1537 1 ret = av_bsf_init(ds->bsf);
1538
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0) {
1539 av_log(ist, AV_LOG_ERROR, "Error initializing bitstream filters: %s\n",
1540 av_err2str(ret));
1541 return ret;
1542 }
1543
1544 1 ret = avcodec_parameters_copy(ist->par, ds->bsf->par_out);
1545
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
1546 return ret;
1547 }
1548
1549 7732 ds->codec_desc = avcodec_descriptor_get(ist->par->codec_id);
1550
1551 7732 return 0;
1552 }
1553
1554 static int dump_attachment(InputStream *ist, const char *filename)
1555 {
1556 AVStream *st = ist->st;
1557 int ret;
1558 AVIOContext *out = NULL;
1559 const AVDictionaryEntry *e;
1560
1561 if (!st->codecpar->extradata_size) {
1562 av_log(ist, AV_LOG_WARNING, "No extradata to dump.\n");
1563 return 0;
1564 }
1565 if (!*filename && (e = av_dict_get(st->metadata, "filename", NULL, 0)))
1566 filename = e->value;
1567 if (!*filename) {
1568 av_log(ist, AV_LOG_FATAL, "No filename specified and no 'filename' tag");
1569 return AVERROR(EINVAL);
1570 }
1571
1572 ret = assert_file_overwrite(filename);
1573 if (ret < 0)
1574 return ret;
1575
1576 if ((ret = avio_open2(&out, filename, AVIO_FLAG_WRITE, &int_cb, NULL)) < 0) {
1577 av_log(ist, AV_LOG_FATAL, "Could not open file %s for writing.\n",
1578 filename);
1579 return ret;
1580 }
1581
1582 avio_write(out, st->codecpar->extradata, st->codecpar->extradata_size);
1583 ret = avio_close(out);
1584
1585 if (ret >= 0)
1586 av_log(ist, AV_LOG_INFO, "Wrote attachment (%d bytes) to '%s'\n",
1587 st->codecpar->extradata_size, filename);
1588
1589 return ret;
1590 }
1591
1592 627 static const char *input_file_item_name(void *obj)
1593 {
1594 627 const Demuxer *d = obj;
1595
1596 627 return d->log_name;
1597 }
1598
1599 static const AVClass input_file_class = {
1600 .class_name = "InputFile",
1601 .version = LIBAVUTIL_VERSION_INT,
1602 .item_name = input_file_item_name,
1603 .category = AV_CLASS_CATEGORY_DEMUXER,
1604 };
1605
1606 7173 static Demuxer *demux_alloc(void)
1607 {
1608 7173 Demuxer *d = allocate_array_elem(&input_files, sizeof(*d), &nb_input_files);
1609
1610
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7173 times.
7173 if (!d)
1611 return NULL;
1612
1613 7173 d->f.class = &input_file_class;
1614 7173 d->f.index = nb_input_files - 1;
1615
1616 7173 snprintf(d->log_name, sizeof(d->log_name), "in#%d", d->f.index);
1617
1618 7173 return d;
1619 }
1620
1621 7173 int ifile_open(const OptionsContext *o, const char *filename, Scheduler *sch)
1622 {
1623 Demuxer *d;
1624 InputFile *f;
1625 AVFormatContext *ic;
1626 7173 const AVInputFormat *file_iformat = NULL;
1627 7173 int err, ret = 0;
1628 int64_t timestamp;
1629 7173 AVDictionary *opts_used = NULL;
1630 7173 const char* video_codec_name = NULL;
1631 7173 const char* audio_codec_name = NULL;
1632 7173 const char* subtitle_codec_name = NULL;
1633 7173 const char* data_codec_name = NULL;
1634 7173 int scan_all_pmts_set = 0;
1635
1636 7173 int64_t start_time = o->start_time;
1637 7173 int64_t start_time_eof = o->start_time_eof;
1638 7173 int64_t stop_time = o->stop_time;
1639 7173 int64_t recording_time = o->recording_time;
1640
1641 7173 d = demux_alloc();
1642
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7173 times.
7173 if (!d)
1643 return AVERROR(ENOMEM);
1644
1645 7173 f = &d->f;
1646
1647 7173 ret = sch_add_demux(sch, input_thread, d);
1648
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7173 times.
7173 if (ret < 0)
1649 return ret;
1650 7173 d->sch = sch;
1651
1652
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 7173 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
7173 if (stop_time != INT64_MAX && recording_time != INT64_MAX) {
1653 stop_time = INT64_MAX;
1654 av_log(d, AV_LOG_WARNING, "-t and -to cannot be used together; using -t.\n");
1655 }
1656
1657
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 7173 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
7173 if (stop_time != INT64_MAX && recording_time == INT64_MAX) {
1658 int64_t start = start_time == AV_NOPTS_VALUE ? 0 : start_time;
1659 if (stop_time <= start) {
1660 av_log(d, AV_LOG_ERROR, "-to value smaller than -ss; aborting.\n");
1661 return AVERROR(EINVAL);
1662 } else {
1663 recording_time = stop_time - start;
1664 }
1665 }
1666
1667
2/2
✓ Branch 0 taken 3675 times.
✓ Branch 1 taken 3498 times.
7173 if (o->format) {
1668
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3675 times.
3675 if (!(file_iformat = av_find_input_format(o->format))) {
1669 av_log(d, AV_LOG_FATAL, "Unknown input format: '%s'\n", o->format);
1670 return AVERROR(EINVAL);
1671 }
1672 }
1673
1674
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7173 times.
7173 if (!strcmp(filename, "-"))
1675 filename = "fd:";
1676
1677 21519 stdin_interaction &= strncmp(filename, "pipe:", 5) &&
1678
2/4
✓ Branch 0 taken 7173 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7173 times.
✗ Branch 3 not taken.
14346 strcmp(filename, "fd:") &&
1679
1/2
✓ Branch 0 taken 7173 times.
✗ Branch 1 not taken.
7173 strcmp(filename, "/dev/stdin");
1680
1681 /* get default parameters from command line */
1682 7173 ic = avformat_alloc_context();
1683
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7173 times.
7173 if (!ic)
1684 return AVERROR(ENOMEM);
1685
2/2
✓ Branch 0 taken 67 times.
✓ Branch 1 taken 7106 times.
7173 if (o->audio_sample_rate.nb_opt) {
1686 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);
1687 }
1688
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 7164 times.
7173 if (o->audio_channels.nb_opt) {
1689 const AVClass *priv_class;
1690
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) &&
1691 9 av_opt_find(&priv_class, "ch_layout", NULL, 0,
1692 AV_OPT_SEARCH_FAKE_OBJ)) {
1693 char buf[32];
1694 9 snprintf(buf, sizeof(buf), "%dC", o->audio_channels.opt[o->audio_channels.nb_opt - 1].u.i);
1695 9 av_dict_set(&o->g->format_opts, "ch_layout", buf, 0);
1696 }
1697 }
1698
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7172 times.
7173 if (o->audio_ch_layouts.nb_opt) {
1699 const AVClass *priv_class;
1700
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
2 if (file_iformat && (priv_class = file_iformat->priv_class) &&
1701 1 av_opt_find(&priv_class, "ch_layout", NULL, 0,
1702 AV_OPT_SEARCH_FAKE_OBJ)) {
1703 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);
1704 }
1705 }
1706
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 7140 times.
7173 if (o->frame_rates.nb_opt) {
1707 const AVClass *priv_class;
1708 /* set the format-level framerate option;
1709 * this is important for video grabbers, e.g. x11 */
1710
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) &&
1711 32 av_opt_find(&priv_class, "framerate", NULL, 0,
1712 AV_OPT_SEARCH_FAKE_OBJ)) {
1713 32 av_dict_set(&o->g->format_opts, "framerate",
1714 32 o->frame_rates.opt[o->frame_rates.nb_opt - 1].u.str, 0);
1715 }
1716 }
1717
2/2
✓ Branch 0 taken 563 times.
✓ Branch 1 taken 6610 times.
7173 if (o->frame_sizes.nb_opt) {
1718 563 av_dict_set(&o->g->format_opts, "video_size", o->frame_sizes.opt[o->frame_sizes.nb_opt - 1].u.str, 0);
1719 }
1720
2/2
✓ Branch 0 taken 572 times.
✓ Branch 1 taken 6601 times.
7173 if (o->frame_pix_fmts.nb_opt)
1721 572 av_dict_set(&o->g->format_opts, "pixel_format", o->frame_pix_fmts.opt[o->frame_pix_fmts.nb_opt - 1].u.str, 0);
1722
1723 7173 video_codec_name = opt_match_per_type_str(&o->codec_names, 'v');
1724 7173 audio_codec_name = opt_match_per_type_str(&o->codec_names, 'a');
1725 7173 subtitle_codec_name = opt_match_per_type_str(&o->codec_names, 's');
1726 7173 data_codec_name = opt_match_per_type_str(&o->codec_names, 'd');
1727
1728
2/2
✓ Branch 0 taken 3027 times.
✓ Branch 1 taken 4146 times.
7173 if (video_codec_name)
1729 3027 ret = err_merge(ret, find_codec(NULL, video_codec_name , AVMEDIA_TYPE_VIDEO , 0,
1730 3027 &ic->video_codec));
1731
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 7155 times.
7173 if (audio_codec_name)
1732 18 ret = err_merge(ret, find_codec(NULL, audio_codec_name , AVMEDIA_TYPE_AUDIO , 0,
1733 18 &ic->audio_codec));
1734
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7173 times.
7173 if (subtitle_codec_name)
1735 ret = err_merge(ret, find_codec(NULL, subtitle_codec_name, AVMEDIA_TYPE_SUBTITLE, 0,
1736 &ic->subtitle_codec));
1737
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7173 times.
7173 if (data_codec_name)
1738 ret = err_merge(ret, find_codec(NULL, data_codec_name , AVMEDIA_TYPE_DATA, 0,
1739 &ic->data_codec));
1740
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7173 times.
7173 if (ret < 0) {
1741 avformat_free_context(ic);
1742 return ret;
1743 }
1744
1745
2/2
✓ Branch 0 taken 3027 times.
✓ Branch 1 taken 4146 times.
7173 ic->video_codec_id = video_codec_name ? ic->video_codec->id : AV_CODEC_ID_NONE;
1746
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 7155 times.
7173 ic->audio_codec_id = audio_codec_name ? ic->audio_codec->id : AV_CODEC_ID_NONE;
1747
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7173 times.
7173 ic->subtitle_codec_id = subtitle_codec_name ? ic->subtitle_codec->id : AV_CODEC_ID_NONE;
1748
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7173 times.
7173 ic->data_codec_id = data_codec_name ? ic->data_codec->id : AV_CODEC_ID_NONE;
1749
1750 7173 ic->flags |= AVFMT_FLAG_NONBLOCK;
1751
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 7089 times.
7173 if (o->bitexact)
1752 84 ic->flags |= AVFMT_FLAG_BITEXACT;
1753 7173 ic->interrupt_callback = int_cb;
1754
1755
1/2
✓ Branch 1 taken 7173 times.
✗ Branch 2 not taken.
7173 if (!av_dict_get(o->g->format_opts, "scan_all_pmts", NULL, AV_DICT_MATCH_CASE)) {
1756 7173 av_dict_set(&o->g->format_opts, "scan_all_pmts", "1", AV_DICT_DONT_OVERWRITE);
1757 7173 scan_all_pmts_set = 1;
1758 }
1759 /* open the input file with generic avformat function */
1760 7173 err = avformat_open_input(&ic, filename, file_iformat, &o->g->format_opts);
1761
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7173 times.
7173 if (err < 0) {
1762 if (err != AVERROR_EXIT)
1763 av_log(d, AV_LOG_ERROR,
1764 "Error opening input: %s\n", av_err2str(err));
1765 if (err == AVERROR_PROTOCOL_NOT_FOUND)
1766 av_log(d, AV_LOG_ERROR, "Did you mean file:%s?\n", filename);
1767 return err;
1768 }
1769 7173 f->ctx = ic;
1770
1771 7173 av_strlcat(d->log_name, "/", sizeof(d->log_name));
1772 7173 av_strlcat(d->log_name, ic->iformat->name, sizeof(d->log_name));
1773
1774
1/2
✓ Branch 0 taken 7173 times.
✗ Branch 1 not taken.
7173 if (scan_all_pmts_set)
1775 7173 av_dict_set(&o->g->format_opts, "scan_all_pmts", NULL, AV_DICT_MATCH_CASE);
1776 7173 remove_avoptions(&o->g->format_opts, o->g->codec_opts);
1777
1778 7173 ret = check_avoptions(o->g->format_opts);
1779
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7173 times.
7173 if (ret < 0)
1780 return ret;
1781
1782 /* apply forced codec ids */
1783
2/2
✓ Branch 0 taken 7582 times.
✓ Branch 1 taken 7173 times.
14755 for (int i = 0; i < ic->nb_streams; i++) {
1784 const AVCodec *dummy;
1785 7582 ret = choose_decoder(o, f, ic, ic->streams[i], HWACCEL_NONE, AV_HWDEVICE_TYPE_NONE,
1786 &dummy);
1787
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7582 times.
7582 if (ret < 0)
1788 return ret;
1789 }
1790
1791
2/2
✓ Branch 0 taken 7171 times.
✓ Branch 1 taken 2 times.
7173 if (o->find_stream_info) {
1792 AVDictionary **opts;
1793 7171 int orig_nb_streams = ic->nb_streams;
1794
1795 7171 ret = setup_find_stream_info_opts(ic, o->g->codec_opts, &opts);
1796
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7171 times.
7171 if (ret < 0)
1797 return ret;
1798
1799 /* If not enough info to get the stream parameters, we decode the
1800 first frames to get it. (used in mpeg case for example) */
1801 7171 ret = avformat_find_stream_info(ic, opts);
1802
1803
2/2
✓ Branch 0 taken 7580 times.
✓ Branch 1 taken 7171 times.
14751 for (int i = 0; i < orig_nb_streams; i++)
1804 7580 av_dict_free(&opts[i]);
1805 7171 av_freep(&opts);
1806
1807
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7171 times.
7171 if (ret < 0) {
1808 av_log(d, AV_LOG_FATAL, "could not find codec parameters\n");
1809 if (ic->nb_streams == 0)
1810 return ret;
1811 }
1812 }
1813
1814
3/4
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 7152 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 21 times.
7173 if (start_time != AV_NOPTS_VALUE && start_time_eof != AV_NOPTS_VALUE) {
1815 av_log(d, AV_LOG_WARNING, "Cannot use -ss and -sseof both, using -ss\n");
1816 start_time_eof = AV_NOPTS_VALUE;
1817 }
1818
1819
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7173 times.
7173 if (start_time_eof != AV_NOPTS_VALUE) {
1820 if (start_time_eof >= 0) {
1821 av_log(d, AV_LOG_ERROR, "-sseof value must be negative; aborting\n");
1822 return AVERROR(EINVAL);
1823 }
1824 if (ic->duration > 0) {
1825 start_time = start_time_eof + ic->duration;
1826 if (start_time < 0) {
1827 av_log(d, AV_LOG_WARNING, "-sseof value seeks to before start of file; ignored\n");
1828 start_time = AV_NOPTS_VALUE;
1829 }
1830 } else
1831 av_log(d, AV_LOG_WARNING, "Cannot use -sseof, file duration not known\n");
1832 }
1833
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 7152 times.
7173 timestamp = (start_time == AV_NOPTS_VALUE) ? 0 : start_time;
1834 /* add the stream start time */
1835
4/4
✓ Branch 0 taken 7170 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 5455 times.
✓ Branch 3 taken 1715 times.
7173 if (!o->seek_timestamp && ic->start_time != AV_NOPTS_VALUE)
1836 5455 timestamp += ic->start_time;
1837
1838 /* if seeking requested, we execute it */
1839
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 7152 times.
7173 if (start_time != AV_NOPTS_VALUE) {
1840 21 int64_t seek_timestamp = timestamp;
1841
1842
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
21 if (!(ic->iformat->flags & AVFMT_SEEK_TO_PTS)) {
1843 21 int dts_heuristic = 0;
1844
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 21 times.
42 for (int i = 0; i < ic->nb_streams; i++) {
1845 21 const AVCodecParameters *par = ic->streams[i]->codecpar;
1846
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (par->video_delay) {
1847 dts_heuristic = 1;
1848 break;
1849 }
1850 }
1851
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (dts_heuristic) {
1852 seek_timestamp -= 3*AV_TIME_BASE / 23;
1853 }
1854 }
1855 21 ret = avformat_seek_file(ic, -1, INT64_MIN, seek_timestamp, seek_timestamp, 0);
1856
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 20 times.
21 if (ret < 0) {
1857 1 av_log(d, AV_LOG_WARNING, "could not seek to position %0.3f\n",
1858 1 (double)timestamp / AV_TIME_BASE);
1859 }
1860 }
1861
1862 7173 f->start_time = start_time;
1863 7173 d->recording_time = recording_time;
1864 7173 f->input_sync_ref = o->input_sync_ref;
1865 7173 f->input_ts_offset = o->input_ts_offset;
1866
3/6
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 7160 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 13 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
7173 f->ts_offset = o->input_ts_offset - (copy_ts ? (start_at_zero && ic->start_time != AV_NOPTS_VALUE ? ic->start_time : 0) : timestamp);
1867 7173 d->accurate_seek = o->accurate_seek;
1868 7173 d->loop = o->loop;
1869 7173 d->nb_streams_warn = ic->nb_streams;
1870
1871 7173 d->duration = (Timestamp){ .ts = 0, .tb = (AVRational){ 1, 1 } };
1872 7173 d->min_pts = (Timestamp){ .ts = AV_NOPTS_VALUE, .tb = (AVRational){ 1, 1 } };
1873 7173 d->max_pts = (Timestamp){ .ts = AV_NOPTS_VALUE, .tb = (AVRational){ 1, 1 } };
1874
1875 7173 d->readrate = o->readrate ? o->readrate : 0.0;
1876
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7173 times.
7173 if (d->readrate < 0.0f) {
1877 av_log(d, AV_LOG_ERROR, "Option -readrate is %0.3f; it must be non-negative.\n", d->readrate);
1878 return AVERROR(EINVAL);
1879 }
1880
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7172 times.
7173 if (o->rate_emu) {
1881
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (d->readrate) {
1882 av_log(d, AV_LOG_WARNING, "Both -readrate and -re set. Using -readrate %0.3f.\n", d->readrate);
1883 } else
1884 1 d->readrate = 1.0f;
1885 }
1886
1887
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7172 times.
7173 if (d->readrate) {
1888
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;
1889
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (d->readrate_initial_burst < 0.0) {
1890 av_log(d, AV_LOG_ERROR,
1891 "Option -readrate_initial_burst is %0.3f; it must be non-negative.\n",
1892 d->readrate_initial_burst);
1893 return AVERROR(EINVAL);
1894 }
1895
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 d->readrate_catchup = o->readrate_catchup ? o->readrate_catchup : d->readrate;
1896
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (d->readrate_catchup < d->readrate) {
1897 av_log(d, AV_LOG_ERROR,
1898 "Option -readrate_catchup is %0.3f; it must be at least equal to %0.3f.\n",
1899 d->readrate_catchup, d->readrate);
1900 return AVERROR(EINVAL);
1901 }
1902 } else {
1903
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7172 times.
7172 if (o->readrate_initial_burst) {
1904 av_log(d, AV_LOG_WARNING, "Option -readrate_initial_burst ignored "
1905 "since neither -readrate nor -re were given\n");
1906 }
1907
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7172 times.
7172 if (o->readrate_catchup) {
1908 av_log(d, AV_LOG_WARNING, "Option -readrate_catchup ignored "
1909 "since neither -readrate nor -re were given\n");
1910 }
1911 }
1912
1913 /* Add all the streams from the given input file to the demuxer */
1914
2/2
✓ Branch 0 taken 7732 times.
✓ Branch 1 taken 7173 times.
14905 for (int i = 0; i < ic->nb_streams; i++) {
1915 7732 ret = ist_add(o, d, ic->streams[i], &opts_used);
1916
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7732 times.
7732 if (ret < 0) {
1917 av_dict_free(&opts_used);
1918 return ret;
1919 }
1920 }
1921
1922 /* dump the file content */
1923 7173 av_dump_format(ic, f->index, filename, 0);
1924
1925 /* check if all codec options have been used */
1926 7173 ret = check_avoptions_used(o->g->codec_opts, opts_used, d, 1);
1927 7173 av_dict_free(&opts_used);
1928
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7173 times.
7173 if (ret < 0)
1929 return ret;
1930
1931
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7173 times.
7173 for (int i = 0; i < o->dump_attachment.nb_opt; i++) {
1932 for (int j = 0; j < f->nb_streams; j++) {
1933 InputStream *ist = f->streams[j];
1934
1935 if (check_stream_specifier(ic, ist->st, o->dump_attachment.opt[i].specifier) == 1) {
1936 ret = dump_attachment(ist, o->dump_attachment.opt[i].u.str);
1937 if (ret < 0)
1938 return ret;
1939 }
1940 }
1941 }
1942
1943 7173 return 0;
1944 }
1945