FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/fftools/ffmpeg_demux.c
Date: 2024-07-26 21:54:09
Exec Total Coverage
Lines: 811 1060 76.5%
Functions: 34 35 97.1%
Branches: 560 1172 47.8%

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