FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/fftools/ffmpeg_demux.c
Date: 2024-10-04 17:46:48
Exec Total Coverage
Lines: 809 1057 76.5%
Functions: 33 34 97.1%
Branches: 503 774 65.0%

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 1847864 static DemuxStream *ds_from_ist(InputStream *ist)
148 {
149 1847864 return (DemuxStream*)ist;
150 }
151
152 20454 static Demuxer *demuxer_from_ifile(InputFile *f)
153 {
154 20454 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 9757 static void report_new_stream(Demuxer *d, const AVPacket *pkt)
169 {
170 9757 const AVStream *st = d->f.ctx->streams[pkt->stream_index];
171
172
1/2
✓ Branch 0 taken 9757 times.
✗ Branch 1 not taken.
9757 if (pkt->stream_index < d->nb_streams_warn)
173 9757 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 414756 static void ts_discontinuity_detect(Demuxer *d, InputStream *ist,
209 AVPacket *pkt)
210 {
211 414756 InputFile *ifile = &d->f;
212 414756 DemuxStream *ds = ds_from_ist(ist);
213 414756 const int fmt_is_discont = ifile->ctx->iformat->flags & AVFMT_TS_DISCONT;
214 414756 int disable_discontinuity_correction = copy_ts;
215 414756 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 414562 times.
✓ Branch 2 taken 188 times.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 73 times.
✓ Branch 5 taken 115 times.
414756 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 408317 times.
✓ Branch 1 taken 6439 times.
✓ Branch 2 taken 408129 times.
✓ Branch 3 taken 188 times.
822885 if (ds->next_dts != AV_NOPTS_VALUE && !disable_discontinuity_correction) {
228 408129 int64_t delta = pkt_dts - ds->next_dts;
229
2/2
✓ Branch 0 taken 22492 times.
✓ Branch 1 taken 385637 times.
408129 if (fmt_is_discont) {
230
4/4
✓ Branch 0 taken 22436 times.
✓ Branch 1 taken 56 times.
✓ Branch 2 taken 22409 times.
✓ Branch 3 taken 83 times.
22492 if (FFABS(delta) > 1LL * dts_delta_threshold * AV_TIME_BASE ||
231
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 22401 times.
22409 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 382164 times.
✓ Branch 1 taken 3473 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 385637 times.
385637 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 383573 times.
✓ Branch 1 taken 2064 times.
385637 if (pkt->pts != AV_NOPTS_VALUE){
249 383573 int64_t pkt_pts = av_rescale_q(pkt->pts, pkt->time_base, AV_TIME_BASE_Q);
250 383573 delta = pkt_pts - ds->next_dts;
251
3/4
✓ Branch 0 taken 380408 times.
✓ Branch 1 taken 3165 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 383573 times.
383573 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 6439 times.
✓ Branch 1 taken 188 times.
✓ Branch 2 taken 6433 times.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 115 times.
✓ Branch 5 taken 6318 times.
6627 } else if (ds->next_dts == AV_NOPTS_VALUE && !copy_ts &&
260
1/2
✓ Branch 0 taken 115 times.
✗ Branch 1 not taken.
115 fmt_is_discont && d->last_ts != AV_NOPTS_VALUE) {
261 115 int64_t delta = pkt_dts - d->last_ts;
262
3/4
✓ Branch 0 taken 55 times.
✓ Branch 1 taken 60 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 115 times.
115 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 414756 d->last_ts = av_rescale_q(pkt->dts, pkt->time_base, AV_TIME_BASE_Q);
274 414756 }
275
276 462052 static void ts_discontinuity_process(Demuxer *d, InputStream *ist,
277 AVPacket *pkt)
278 {
279 462052 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 417461 times.
✓ Branch 1 taken 44591 times.
462052 if (pkt->dts != AV_NOPTS_VALUE)
285 417461 pkt->dts += offset;
286
2/2
✓ Branch 0 taken 415357 times.
✓ Branch 1 taken 46695 times.
462052 if (pkt->pts != AV_NOPTS_VALUE)
287 415357 pkt->pts += offset;
288
289 // detect timestamp discontinuities for audio/video
290
2/2
✓ Branch 0 taken 281726 times.
✓ Branch 1 taken 180326 times.
462052 if ((ist->par->codec_type == AVMEDIA_TYPE_VIDEO ||
291
2/2
✓ Branch 0 taken 279017 times.
✓ Branch 1 taken 2709 times.
281726 ist->par->codec_type == AVMEDIA_TYPE_AUDIO) &&
292
2/2
✓ Branch 0 taken 414756 times.
✓ Branch 1 taken 44587 times.
459343 pkt->dts != AV_NOPTS_VALUE)
293 414756 ts_discontinuity_detect(d, ist, pkt);
294 462052 }
295
296 462052 static int ist_dts_update(DemuxStream *ds, AVPacket *pkt, FrameData *fd)
297 {
298 462052 InputStream *ist = &ds->ist;
299 462052 const AVCodecParameters *par = ist->par;
300
301
2/2
✓ Branch 0 taken 7051 times.
✓ Branch 1 taken 455001 times.
462052 if (!ds->saw_first_ts) {
302 7051 ds->first_dts =
303
2/2
✓ Branch 0 taken 4809 times.
✓ Branch 1 taken 2242 times.
7051 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 6460 times.
✓ Branch 1 taken 591 times.
7051 if (pkt->pts != AV_NOPTS_VALUE) {
305 6460 ds->first_dts =
306 6460 ds->dts += av_rescale_q(pkt->pts, pkt->time_base, AV_TIME_BASE_Q);
307 }
308 7051 ds->saw_first_ts = 1;
309 }
310
311
2/2
✓ Branch 0 taken 7051 times.
✓ Branch 1 taken 455001 times.
462052 if (ds->next_dts == AV_NOPTS_VALUE)
312 7051 ds->next_dts = ds->dts;
313
314
2/2
✓ Branch 0 taken 417461 times.
✓ Branch 1 taken 44591 times.
462052 if (pkt->dts != AV_NOPTS_VALUE)
315 417461 ds->next_dts = ds->dts = av_rescale_q(pkt->dts, pkt->time_base, AV_TIME_BASE_Q);
316
317 462052 ds->dts = ds->next_dts;
318
3/3
✓ Branch 0 taken 279017 times.
✓ Branch 1 taken 180326 times.
✓ Branch 2 taken 2709 times.
462052 switch (par->codec_type) {
319 279017 case AVMEDIA_TYPE_AUDIO:
320 av_assert1(pkt->duration >= 0);
321
1/2
✓ Branch 0 taken 279017 times.
✗ Branch 1 not taken.
279017 if (par->sample_rate) {
322 279017 ds->next_dts += ((int64_t)AV_TIME_BASE * par->frame_size) /
323 279017 par->sample_rate;
324 } else {
325 ds->next_dts += av_rescale_q(pkt->duration, pkt->time_base, AV_TIME_BASE_Q);
326 }
327 279017 break;
328 180326 case AVMEDIA_TYPE_VIDEO:
329
2/2
✓ Branch 0 taken 604 times.
✓ Branch 1 taken 179722 times.
180326 if (ist->framerate.num) {
330 // TODO: Remove work-around for c99-to-c89 issue 7
331 604 AVRational time_base_q = AV_TIME_BASE_Q;
332 604 int64_t next_dts = av_rescale_q(ds->next_dts, time_base_q, av_inv_q(ist->framerate));
333 604 ds->next_dts = av_rescale_q(next_dts + 1, av_inv_q(ist->framerate), time_base_q);
334
2/2
✓ Branch 0 taken 177584 times.
✓ Branch 1 taken 2138 times.
179722 } else if (pkt->duration) {
335 177584 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 2068 times.
2138 } 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 180326 break;
349 }
350
351 462052 fd->dts_est = ds->dts;
352
353 462052 return 0;
354 }
355
356 462052 static int ts_fixup(Demuxer *d, AVPacket *pkt, FrameData *fd)
357 {
358 462052 InputFile *ifile = &d->f;
359 462052 InputStream *ist = ifile->streams[pkt->stream_index];
360 462052 DemuxStream *ds = ds_from_ist(ist);
361 462052 const int64_t start_time = ifile->start_time_effective;
362 int64_t duration;
363 int ret;
364
365 462052 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 462052 times.
462052 SHOW_TS_DEBUG("demuxer");
379
380
4/4
✓ Branch 0 taken 414474 times.
✓ Branch 1 taken 47578 times.
✓ Branch 2 taken 318340 times.
✓ Branch 3 taken 96134 times.
462052 if (!ds->wrap_correction_done && start_time != AV_NOPTS_VALUE &&
381
2/2
✓ Branch 0 taken 355 times.
✓ Branch 1 taken 317985 times.
318340 ist->st->pts_wrap_bits < 64) {
382 int64_t stime, stime2;
383
384 355 stime = av_rescale_q(start_time, AV_TIME_BASE_Q, pkt->time_base);
385 355 stime2= stime + (1ULL<<ist->st->pts_wrap_bits);
386 355 ds->wrap_correction_done = 1;
387
388
5/6
✓ Branch 0 taken 325 times.
✓ Branch 1 taken 30 times.
✓ Branch 2 taken 291 times.
✓ Branch 3 taken 34 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 291 times.
355 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 325 times.
✓ Branch 1 taken 30 times.
✓ Branch 2 taken 285 times.
✓ Branch 3 taken 40 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 285 times.
355 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 417461 times.
✓ Branch 1 taken 44591 times.
462052 if (pkt->dts != AV_NOPTS_VALUE)
399 417461 pkt->dts += av_rescale_q(ifile->ts_offset, AV_TIME_BASE_Q, pkt->time_base);
400
2/2
✓ Branch 0 taken 415357 times.
✓ Branch 1 taken 46695 times.
462052 if (pkt->pts != AV_NOPTS_VALUE)
401 415357 pkt->pts += av_rescale_q(ifile->ts_offset, AV_TIME_BASE_Q, pkt->time_base);
402
403
2/2
✓ Branch 0 taken 415357 times.
✓ Branch 1 taken 46695 times.
462052 if (pkt->pts != AV_NOPTS_VALUE)
404 415357 pkt->pts *= ds->ts_scale;
405
2/2
✓ Branch 0 taken 417461 times.
✓ Branch 1 taken 44591 times.
462052 if (pkt->dts != AV_NOPTS_VALUE)
406 417461 pkt->dts *= ds->ts_scale;
407
408 462052 duration = av_rescale_q(d->duration.ts, d->duration.tb, pkt->time_base);
409
2/2
✓ Branch 0 taken 415357 times.
✓ Branch 1 taken 46695 times.
462052 if (pkt->pts != AV_NOPTS_VALUE) {
410 // audio decoders take precedence for estimating total file duration
411
2/2
✓ Branch 0 taken 184700 times.
✓ Branch 1 taken 230657 times.
415357 int64_t pkt_duration = d->have_audio_dec ? 0 : pkt->duration;
412
413 415357 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 409054 times.
✓ Branch 1 taken 6303 times.
✓ Branch 2 taken 396814 times.
✓ Branch 3 taken 12240 times.
824411 if (d->max_pts.ts == AV_NOPTS_VALUE ||
418 409054 av_compare_ts(d->max_pts.ts, d->max_pts.tb,
419 409054 pkt->pts + pkt_duration, pkt->time_base) < 0) {
420 403117 d->max_pts = (Timestamp){ .ts = pkt->pts + pkt_duration,
421 403117 .tb = pkt->time_base };
422 }
423
4/4
✓ Branch 0 taken 409054 times.
✓ Branch 1 taken 6303 times.
✓ Branch 2 taken 23 times.
✓ Branch 3 taken 409031 times.
824411 if (d->min_pts.ts == AV_NOPTS_VALUE ||
424 409054 av_compare_ts(d->min_pts.ts, d->min_pts.tb,
425 pkt->pts, pkt->time_base) > 0) {
426 6326 d->min_pts = (Timestamp){ .ts = pkt->pts,
427 6326 .tb = pkt->time_base };
428 }
429 }
430
431
2/2
✓ Branch 0 taken 417461 times.
✓ Branch 1 taken 44591 times.
462052 if (pkt->dts != AV_NOPTS_VALUE)
432 417461 pkt->dts += duration;
433
434
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 462052 times.
462052 SHOW_TS_DEBUG("demuxer+tsfixup");
435
436 // detect and try to correct for timestamp discontinuities
437 462052 ts_discontinuity_process(d, ist, pkt);
438
439 // update estimated/predicted dts
440 462052 ret = ist_dts_update(ds, pkt, fd);
441
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 462052 times.
462052 if (ret < 0)
442 return ret;
443
444 462052 return 0;
445 }
446
447 462052 static int input_packet_process(Demuxer *d, AVPacket *pkt, unsigned *send_flags)
448 {
449 462052 InputFile *f = &d->f;
450 462052 InputStream *ist = f->streams[pkt->stream_index];
451 462052 DemuxStream *ds = ds_from_ist(ist);
452 FrameData *fd;
453 462052 int ret = 0;
454
455 462052 fd = packet_data(pkt);
456
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 462052 times.
462052 if (!fd)
457 return AVERROR(ENOMEM);
458
459 462052 ret = ts_fixup(d, pkt, fd);
460
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 462052 times.
462052 if (ret < 0)
461 return ret;
462
463
2/2
✓ Branch 0 taken 229 times.
✓ Branch 1 taken 461823 times.
462052 if (d->recording_time != INT64_MAX) {
464 229 int64_t start_time = 0;
465
2/2
✓ Branch 0 taken 74 times.
✓ Branch 1 taken 155 times.
229 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 33 times.
✓ Branch 1 taken 196 times.
229 if (ds->dts >= d->recording_time + start_time)
470 33 *send_flags |= DEMUX_SEND_STREAMCOPY_EOF;
471 }
472
473 462052 ds->data_size += pkt->size;
474 462052 ds->nb_packets++;
475
476 462052 fd->wallclock[LATENCY_PROBE_DEMUX] = av_gettime_relative();
477
478
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 462052 times.
462052 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 462052 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 191 times.
✓ Branch 1 taken 25 times.
216 if (pts - burst_until > now)
507 191 av_usleep(pts - burst_until - now);
508 }
509 216 }
510
511 463000 static int do_send(Demuxer *d, DemuxStream *ds, AVPacket *pkt, unsigned flags,
512 const char *pkt_desc)
513 {
514 int ret;
515
516 463000 pkt->stream_index = ds->sch_idx_stream;
517
518 463000 ret = sch_demux_send(d->sch, d->f.index, pkt, flags);
519
2/2
✓ Branch 0 taken 3117 times.
✓ Branch 1 taken 459883 times.
463000 if (ret == AVERROR_EOF) {
520 3117 av_packet_unref(pkt);
521
522 3117 av_log(ds, AV_LOG_VERBOSE, "All consumers of this stream are done\n");
523 3117 ds->finished = 1;
524
525
2/2
✓ Branch 0 taken 3093 times.
✓ Branch 1 taken 24 times.
3117 if (++d->nb_streams_finished == d->nb_streams_used) {
526 3093 av_log(d, AV_LOG_VERBOSE, "All consumers are done\n");
527 3093 return AVERROR_EOF;
528 }
529
2/2
✓ Branch 0 taken 57 times.
✓ Branch 1 taken 459826 times.
459883 } else if (ret < 0) {
530
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 57 times.
57 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 57 return ret;
535 }
536
537 459850 return 0;
538 }
539
540 462055 static int demux_send(Demuxer *d, DemuxThreadContext *dt, DemuxStream *ds,
541 AVPacket *pkt, unsigned flags)
542 {
543 462055 InputFile *f = &d->f;
544 int ret;
545
546 // pkt can be NULL only when flushing BSFs
547
3/4
✓ Branch 0 taken 462040 times.
✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 462040 times.
462055 av_assert0(ds->bsf || pkt);
548
549 // send heartbeat for sub2video streams
550
4/6
✓ Branch 0 taken 948 times.
✓ Branch 1 taken 461107 times.
✓ Branch 2 taken 948 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 948 times.
✗ Branch 5 not taken.
462055 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 462040 times.
462055 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 462040 ret = do_send(d, ds, pkt, flags, "demuxed");
602
2/2
✓ Branch 0 taken 3150 times.
✓ Branch 1 taken 458890 times.
462040 if (ret < 0)
603 3150 return ret;
604 }
605
606 458890 return 0;
607 }
608
609 3693 static int demux_bsf_flush(Demuxer *d, DemuxThreadContext *dt)
610 {
611 3693 InputFile *f = &d->f;
612 int ret;
613
614
2/2
✓ Branch 0 taken 4167 times.
✓ Branch 1 taken 3693 times.
7860 for (unsigned i = 0; i < f->nb_streams; i++) {
615 4167 DemuxStream *ds = ds_from_ist(f->streams[i]);
616
617
2/2
✓ Branch 0 taken 4164 times.
✓ Branch 1 taken 3 times.
4167 if (!ds->bsf)
618 4164 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 3693 return 0;
632 }
633
634 6832 static void discard_unused_programs(InputFile *ifile)
635 {
636
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 6832 times.
6874 for (int j = 0; j < ifile->ctx->nb_programs; j++) {
637 42 AVProgram *p = ifile->ctx->programs[j];
638 42 int discard = AVDISCARD_ALL;
639
640
1/2
✓ Branch 0 taken 57 times.
✗ Branch 1 not taken.
57 for (int k = 0; k < p->nb_stream_indexes; k++) {
641 57 DemuxStream *ds = ds_from_ist(ifile->streams[p->stream_index[k]]);
642
643
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 15 times.
57 if (!ds->discard) {
644 42 discard = AVDISCARD_DEFAULT;
645 42 break;
646 }
647 }
648 42 p->discard = discard;
649 }
650 6832 }
651
652 6832 static void thread_set_name(InputFile *f)
653 {
654 char name[16];
655 6832 snprintf(name, sizeof(name), "dmx%d:%s", f->index, f->ctx->iformat->name);
656 6832 ff_thread_setname(name);
657 6832 }
658
659 6832 static void demux_thread_uninit(DemuxThreadContext *dt)
660 {
661 6832 av_packet_free(&dt->pkt_demux);
662 6832 av_packet_free(&dt->pkt_bsf);
663
664 6832 memset(dt, 0, sizeof(*dt));
665 6832 }
666
667 6832 static int demux_thread_init(DemuxThreadContext *dt)
668 {
669 6832 memset(dt, 0, sizeof(*dt));
670
671 6832 dt->pkt_demux = av_packet_alloc();
672
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6832 times.
6832 if (!dt->pkt_demux)
673 return AVERROR(ENOMEM);
674
675 6832 dt->pkt_bsf = av_packet_alloc();
676
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6832 times.
6832 if (!dt->pkt_bsf)
677 return AVERROR(ENOMEM);
678
679 6832 return 0;
680 }
681
682 6832 static int input_thread(void *arg)
683 {
684 6832 Demuxer *d = arg;
685 6832 InputFile *f = &d->f;
686
687 DemuxThreadContext dt;
688
689 6832 int ret = 0;
690
691 6832 ret = demux_thread_init(&dt);
692
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6832 times.
6832 if (ret < 0)
693 goto finish;
694
695 6832 thread_set_name(f);
696
697 6832 discard_unused_programs(f);
698
699 6832 d->read_started = 1;
700 6832 d->wallclock_start = av_gettime_relative();
701
702 468671 while (1) {
703 DemuxStream *ds;
704 475503 unsigned send_flags = 0;
705
706 475503 ret = av_read_frame(f->ctx, dt.pkt_demux);
707
708
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 475502 times.
475503 if (ret == AVERROR(EAGAIN)) {
709 1 av_usleep(10000);
710 9769 continue;
711 }
712
2/2
✓ Branch 0 taken 3693 times.
✓ Branch 1 taken 471809 times.
475502 if (ret < 0) {
713 int ret_bsf;
714
715
2/2
✓ Branch 0 taken 3637 times.
✓ Branch 1 taken 56 times.
3693 if (ret == AVERROR_EOF)
716 3637 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 3693 ret_bsf = demux_bsf_flush(d, &dt);
724
2/2
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 3637 times.
3693 ret = err_merge(ret == AVERROR_EOF ? 0 : ret, ret_bsf);
725
726
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 3682 times.
3693 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 3682 break;
740 }
741
742
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 471809 times.
471809 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 943618 ds = dt.pkt_demux->stream_index < f->nb_streams ?
750
1/2
✓ Branch 0 taken 471809 times.
✗ Branch 1 not taken.
471809 ds_from_ist(f->streams[dt.pkt_demux->stream_index]) : NULL;
751
5/6
✓ Branch 0 taken 471809 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 463675 times.
✓ Branch 3 taken 8134 times.
✓ Branch 4 taken 1623 times.
✓ Branch 5 taken 462052 times.
471809 if (!ds || ds->discard || ds->finished) {
752 9757 report_new_stream(d, dt.pkt_demux);
753 9757 av_packet_unref(dt.pkt_demux);
754 9757 continue;
755 }
756
757
2/2
✓ Branch 0 taken 99 times.
✓ Branch 1 taken 461953 times.
462052 if (dt.pkt_demux->flags & AV_PKT_FLAG_CORRUPT) {
758 99 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 99 times.
99 dt.pkt_demux->stream_index);
761
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 99 times.
99 if (exit_on_error) {
762 av_packet_unref(dt.pkt_demux);
763 ret = AVERROR_INVALIDDATA;
764 break;
765 }
766 }
767
768 462052 ret = input_packet_process(d, dt.pkt_demux, &send_flags);
769
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 462052 times.
462052 if (ret < 0)
770 break;
771
772
2/2
✓ Branch 0 taken 216 times.
✓ Branch 1 taken 461836 times.
462052 if (d->readrate)
773 216 readrate_sleep(d);
774
775 462052 ret = demux_send(d, &dt, ds, dt.pkt_demux, send_flags);
776
2/2
✓ Branch 0 taken 3150 times.
✓ Branch 1 taken 458902 times.
462052 if (ret < 0)
777 3150 break;
778 }
779
780 // EOF/EXIT is normal termination
781
4/4
✓ Branch 0 taken 3739 times.
✓ Branch 1 taken 3093 times.
✓ Branch 2 taken 3682 times.
✓ Branch 3 taken 57 times.
6832 if (ret == AVERROR_EOF || ret == AVERROR_EXIT)
782 3150 ret = 0;
783
784 3682 finish:
785 6832 demux_thread_uninit(&dt);
786
787 6832 return ret;
788 }
789
790 6832 static void demux_final_stats(Demuxer *d)
791 {
792 6832 InputFile *f = &d->f;
793 6832 uint64_t total_packets = 0, total_size = 0;
794
795 6832 av_log(f, AV_LOG_VERBOSE, "Input file #%d (%s):\n",
796 6832 f->index, f->ctx->url);
797
798
2/2
✓ Branch 0 taken 7363 times.
✓ Branch 1 taken 6832 times.
14195 for (int j = 0; j < f->nb_streams; j++) {
799 7363 InputStream *ist = f->streams[j];
800 7363 DemuxStream *ds = ds_from_ist(ist);
801 7363 enum AVMediaType type = ist->par->codec_type;
802
803
3/4
✓ Branch 0 taken 7072 times.
✓ Branch 1 taken 291 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 7072 times.
7363 if (ds->discard || type == AVMEDIA_TYPE_ATTACHMENT)
804 291 continue;
805
806 7072 total_size += ds->data_size;
807 7072 total_packets += ds->nb_packets;
808
809 7072 av_log(f, AV_LOG_VERBOSE, " Input stream #%d:%d (%s): ",
810 f->index, j, av_get_media_type_string(type));
811 7072 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 6435 times.
✓ Branch 1 taken 637 times.
7072 if (ds->decoding_needed) {
815 6435 av_log(f, AV_LOG_VERBOSE,
816 "%"PRIu64" frames decoded; %"PRIu64" decode errors",
817 6435 ist->decoder->frames_decoded, ist->decoder->decode_errors);
818
2/2
✓ Branch 0 taken 1206 times.
✓ Branch 1 taken 5229 times.
6435 if (type == AVMEDIA_TYPE_AUDIO)
819 1206 av_log(f, AV_LOG_VERBOSE, " (%"PRIu64" samples)", ist->decoder->samples_decoded);
820 6435 av_log(f, AV_LOG_VERBOSE, "; ");
821 }
822
823 7072 av_log(f, AV_LOG_VERBOSE, "\n");
824 }
825
826 6832 av_log(f, AV_LOG_VERBOSE, " Total: %"PRIu64" packets (%"PRIu64" bytes) demuxed\n",
827 total_packets, total_size);
828 6832 }
829
830 7378 static void ist_free(InputStream **pist)
831 {
832 7378 InputStream *ist = *pist;
833 DemuxStream *ds;
834
835
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7378 times.
7378 if (!ist)
836 return;
837 7378 ds = ds_from_ist(ist);
838
839 7378 dec_free(&ist->decoder);
840
841 7378 av_dict_free(&ds->decoder_opts);
842 7378 av_freep(&ist->filters);
843 7378 av_freep(&ds->dec_opts.hwaccel_device);
844
845 7378 avcodec_parameters_free(&ist->par);
846
847 7378 av_frame_free(&ds->decoded_params);
848
849 7378 av_bsf_free(&ds->bsf);
850
851 7378 av_freep(pist);
852 }
853
854 6846 void ifile_close(InputFile **pf)
855 {
856 6846 InputFile *f = *pf;
857 6846 Demuxer *d = demuxer_from_ifile(f);
858
859
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6846 times.
6846 if (!f)
860 return;
861
862
2/2
✓ Branch 0 taken 6832 times.
✓ Branch 1 taken 14 times.
6846 if (d->read_started)
863 6832 demux_final_stats(d);
864
865
2/2
✓ Branch 0 taken 7378 times.
✓ Branch 1 taken 6846 times.
14224 for (int i = 0; i < f->nb_streams; i++)
866 7378 ist_free(&f->streams[i]);
867 6846 av_freep(&f->streams);
868
869 6846 avformat_close_input(&f->ctx);
870
871 6846 av_packet_free(&d->pkt_heartbeat);
872
873 6846 av_freep(pf);
874 }
875
876 7153 int ist_use(InputStream *ist, int decoding_needed,
877 const ViewSpecifier *vs, SchedulerNode *src)
878 {
879 7153 Demuxer *d = demuxer_from_ifile(ist->file);
880 7153 DemuxStream *ds = ds_from_ist(ist);
881 int ret;
882
883
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7153 times.
7153 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 6493 times.
✓ Branch 1 taken 660 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6493 times.
7153 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 7072 times.
✓ Branch 1 taken 81 times.
7153 if (ds->sch_idx_stream < 0) {
897 7072 ret = sch_add_demux_stream(d->sch, d->f.index);
898
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7072 times.
7072 if (ret < 0)
899 return ret;
900 7072 ds->sch_idx_stream = ret;
901 }
902
903
2/2
✓ Branch 0 taken 7072 times.
✓ Branch 1 taken 81 times.
7153 if (ds->discard) {
904 7072 ds->discard = 0;
905 7072 d->nb_streams_used++;
906 }
907
908 7153 ist->st->discard = ist->user_set_discard;
909 7153 ds->decoding_needed |= decoding_needed;
910 7153 ds->streamcopy_needed |= !decoding_needed;
911
912
4/4
✓ Branch 0 taken 6493 times.
✓ Branch 1 taken 660 times.
✓ Branch 2 taken 6435 times.
✓ Branch 3 taken 58 times.
7153 if (decoding_needed && ds->sch_idx_dec < 0) {
913 6435 int is_audio = ist->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO;
914
915 12870 ds->dec_opts.flags |= (!!ist->fix_sub_duration * DECODER_FLAG_FIX_SUB_DURATION) |
916 12870 (!!(d->f.ctx->iformat->flags & AVFMT_NOTIMESTAMPS) * DECODER_FLAG_TS_UNRELIABLE) |
917
4/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6433 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
6435 (!!(d->loop && is_audio) * DECODER_FLAG_SEND_END_TS)
918 #if FFMPEG_OPT_TOP
919
2/2
✓ Branch 0 taken 1246 times.
✓ Branch 1 taken 5189 times.
6435 | ((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 6410 times.
6435 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 6410 ds->dec_opts.framerate = ist->st->avg_frame_rate;
928
929
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6434 times.
6435 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 6435 snprintf(ds->dec_name, sizeof(ds->dec_name), "%d:%d", ist->file->index, ist->index);
939 6435 ds->dec_opts.name = ds->dec_name;
940
941 6435 ds->dec_opts.codec = ist->dec;
942 6435 ds->dec_opts.par = ist->par;
943
944 6435 ds->dec_opts.log_parent = ist;
945
946 6435 ds->decoded_params = av_frame_alloc();
947
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6435 times.
6435 if (!ds->decoded_params)
948 return AVERROR(ENOMEM);
949
950 6435 ret = dec_init(&ist->decoder, d->sch,
951 6435 &ds->decoder_opts, &ds->dec_opts, ds->decoded_params);
952
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6435 times.
6435 if (ret < 0)
953 return ret;
954 6435 ds->sch_idx_dec = ret;
955
956 6435 ret = sch_connect(d->sch, SCH_DSTREAM(d->f.index, ds->sch_idx_stream),
957 6435 SCH_DEC_IN(ds->sch_idx_dec));
958
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6435 times.
6435 if (ret < 0)
959 return ret;
960
961 6435 d->have_audio_dec |= is_audio;
962 }
963
964
4/4
✓ Branch 0 taken 6493 times.
✓ Branch 1 taken 660 times.
✓ Branch 2 taken 5199 times.
✓ Branch 3 taken 1294 times.
7153 if (decoding_needed && ist->par->codec_type == AVMEDIA_TYPE_VIDEO) {
965 5199 ret = dec_request_view(ist->decoder, vs, src);
966
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5199 times.
5199 if (ret < 0)
967 return ret;
968 } else {
969 1954 *src = decoding_needed ?
970
2/2
✓ Branch 0 taken 1294 times.
✓ Branch 1 taken 660 times.
1954 SCH_DEC_OUT(ds->sch_idx_dec, 0) :
971 660 SCH_DSTREAM(d->f.index, ds->sch_idx_stream);
972 }
973
974 7153 return 0;
975 }
976
977 6455 int ist_filter_add(InputStream *ist, InputFilter *ifilter, int is_simple,
978 const ViewSpecifier *vs, InputFilterOptions *opts,
979 SchedulerNode *src)
980 {
981 6455 Demuxer *d = demuxer_from_ifile(ist->file);
982 6455 DemuxStream *ds = ds_from_ist(ist);
983 6455 int64_t tsoffset = 0;
984 int ret;
985
986
2/2
✓ Branch 0 taken 6317 times.
✓ Branch 1 taken 138 times.
6455 ret = ist_use(ist, is_simple ? DECODING_FOR_OST : DECODING_FOR_FILTER,
987 vs, src);
988
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6455 times.
6455 if (ret < 0)
989 return ret;
990
991 6455 ret = GROW_ARRAY(ist->filters, ist->nb_filters);
992
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6455 times.
6455 if (ret < 0)
993 return ret;
994
995 6455 ist->filters[ist->nb_filters - 1] = ifilter;
996
997
2/2
✓ Branch 0 taken 5199 times.
✓ Branch 1 taken 1256 times.
6455 if (ist->par->codec_type == AVMEDIA_TYPE_VIDEO) {
998 5199 const AVPacketSideData *sd = av_packet_side_data_get(ist->par->coded_side_data,
999 5199 ist->par->nb_coded_side_data,
1000 AV_PKT_DATA_FRAME_CROPPING);
1001
3/4
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 5174 times.
✓ Branch 2 taken 25 times.
✗ Branch 3 not taken.
5199 if (ist->framerate.num > 0 && ist->framerate.den > 0) {
1002 25 opts->framerate = ist->framerate;
1003 25 opts->flags |= IFILTER_FLAG_CFR;
1004 } else
1005 5174 opts->framerate = av_guess_frame_rate(d->f.ctx, ist->st, NULL);
1006
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 5195 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
5199 if (sd && sd->size >= sizeof(uint32_t) * 4) {
1007 4 opts->crop_top = AV_RL32(sd->data + 0);
1008 4 opts->crop_bottom = AV_RL32(sd->data + 4);
1009 4 opts->crop_left = AV_RL32(sd->data + 8);
1010 4 opts->crop_right = AV_RL32(sd->data + 12);
1011
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 &&
1012
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 (opts->crop_top | opts->crop_bottom | opts->crop_left | opts->crop_right))
1013 4 opts->flags |= IFILTER_FLAG_CROP;
1014 }
1015
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1252 times.
1256 } else if (ist->par->codec_type == AVMEDIA_TYPE_SUBTITLE) {
1016 /* Compute the size of the canvas for the subtitles stream.
1017 If the subtitles codecpar has set a size, use it. Otherwise use the
1018 maximum dimensions of the video streams in the same file. */
1019 4 opts->sub2video_width = ist->par->width;
1020 4 opts->sub2video_height = ist->par->height;
1021
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)) {
1022 for (int j = 0; j < d->f.nb_streams; j++) {
1023 AVCodecParameters *par1 = d->f.streams[j]->par;
1024 if (par1->codec_type == AVMEDIA_TYPE_VIDEO) {
1025 opts->sub2video_width = FFMAX(opts->sub2video_width, par1->width);
1026 opts->sub2video_height = FFMAX(opts->sub2video_height, par1->height);
1027 }
1028 }
1029 }
1030
1031
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)) {
1032 opts->sub2video_width = FFMAX(opts->sub2video_width, 720);
1033 opts->sub2video_height = FFMAX(opts->sub2video_height, 576);
1034 }
1035
1036
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (!d->pkt_heartbeat) {
1037 4 d->pkt_heartbeat = av_packet_alloc();
1038
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!d->pkt_heartbeat)
1039 return AVERROR(ENOMEM);
1040 }
1041 4 ds->have_sub2video = 1;
1042 }
1043
1044 6455 ret = av_frame_copy_props(opts->fallback, ds->decoded_params);
1045
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6455 times.
6455 if (ret < 0)
1046 return ret;
1047 6455 opts->fallback->format = ds->decoded_params->format;
1048 6455 opts->fallback->width = ds->decoded_params->width;
1049 6455 opts->fallback->height = ds->decoded_params->height;
1050
1051 6455 ret = av_channel_layout_copy(&opts->fallback->ch_layout, &ds->decoded_params->ch_layout);
1052
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6455 times.
6455 if (ret < 0)
1053 return ret;
1054
1055
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 6452 times.
6455 if (copy_ts) {
1056
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;
1057
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)
1058 2 tsoffset += d->f.ctx->start_time;
1059 }
1060
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) ?
1061
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 6439 times.
6471 AV_NOPTS_VALUE : tsoffset;
1062 6455 opts->trim_end_us = d->recording_time;
1063
1064 6455 opts->name = av_strdup(ds->dec_name);
1065
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6455 times.
6455 if (!opts->name)
1066 return AVERROR(ENOMEM);
1067
1068 12910 opts->flags |= IFILTER_FLAG_AUTOROTATE * !!(ds->autorotate) |
1069
1/2
✓ Branch 0 taken 6455 times.
✗ Branch 1 not taken.
6455 IFILTER_FLAG_REINIT * !!(ds->reinit_filters);
1070
1071 6455 return 0;
1072 }
1073
1074 14626 static int choose_decoder(const OptionsContext *o, void *logctx,
1075 AVFormatContext *s, AVStream *st,
1076 enum HWAccelID hwaccel_id, enum AVHWDeviceType hwaccel_device_type,
1077 const AVCodec **pcodec)
1078
1079 {
1080 14626 const char *codec_name = NULL;
1081
1082 14626 opt_match_per_stream_str(logctx, &o->codec_names, s, st, &codec_name);
1083
2/2
✓ Branch 0 taken 5523 times.
✓ Branch 1 taken 9103 times.
14626 if (codec_name) {
1084 5523 int ret = find_codec(NULL, codec_name, st->codecpar->codec_type, 0, pcodec);
1085
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5523 times.
5523 if (ret < 0)
1086 return ret;
1087 5523 st->codecpar->codec_id = (*pcodec)->id;
1088
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)
1089 st->codecpar->codec_type = (*pcodec)->type;
1090 5523 return 0;
1091 } else {
1092
3/4
✓ Branch 0 taken 5648 times.
✓ Branch 1 taken 3455 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 5648 times.
9103 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
1093 hwaccel_id == HWACCEL_GENERIC &&
1094 hwaccel_device_type != AV_HWDEVICE_TYPE_NONE) {
1095 const AVCodec *c;
1096 void *i = NULL;
1097
1098 while ((c = av_codec_iterate(&i))) {
1099 const AVCodecHWConfig *config;
1100
1101 if (c->id != st->codecpar->codec_id ||
1102 !av_codec_is_decoder(c))
1103 continue;
1104
1105 for (int j = 0; config = avcodec_get_hw_config(c, j); j++) {
1106 if (config->device_type == hwaccel_device_type) {
1107 av_log(NULL, AV_LOG_VERBOSE, "Selecting decoder '%s' because of requested hwaccel method %s\n",
1108 c->name, av_hwdevice_get_type_name(hwaccel_device_type));
1109 *pcodec = c;
1110 return 0;
1111 }
1112 }
1113 }
1114 }
1115
1116 9103 *pcodec = avcodec_find_decoder(st->codecpar->codec_id);
1117 9103 return 0;
1118 }
1119 }
1120
1121 1654 static int guess_input_channel_layout(InputStream *ist, AVCodecParameters *par,
1122 int guess_layout_max)
1123 {
1124
2/2
✓ Branch 0 taken 768 times.
✓ Branch 1 taken 886 times.
1654 if (par->ch_layout.order == AV_CHANNEL_ORDER_UNSPEC) {
1125 char layout_name[256];
1126
1127
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 764 times.
768 if (par->ch_layout.nb_channels > guess_layout_max)
1128 14 return 0;
1129 764 av_channel_layout_default(&par->ch_layout, par->ch_layout.nb_channels);
1130
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 754 times.
764 if (par->ch_layout.order == AV_CHANNEL_ORDER_UNSPEC)
1131 10 return 0;
1132 754 av_channel_layout_describe(&par->ch_layout, layout_name, sizeof(layout_name));
1133 754 av_log(ist, AV_LOG_WARNING, "Guessed Channel Layout: %s\n", layout_name);
1134 }
1135 1640 return 1;
1136 }
1137
1138 5577 static int add_display_matrix_to_stream(const OptionsContext *o,
1139 AVFormatContext *ctx, InputStream *ist)
1140 {
1141 5577 AVStream *st = ist->st;
1142 AVPacketSideData *sd;
1143 5577 double rotation = DBL_MAX;
1144 5577 int hflip = -1, vflip = -1;
1145 5577 int hflip_set = 0, vflip_set = 0, rotation_set = 0;
1146 int32_t *buf;
1147
1148 5577 opt_match_per_stream_dbl(ist, &o->display_rotations, ctx, st, &rotation);
1149 5577 opt_match_per_stream_int(ist, &o->display_hflips, ctx, st, &hflip);
1150 5577 opt_match_per_stream_int(ist, &o->display_vflips, ctx, st, &vflip);
1151
1152 5577 rotation_set = rotation != DBL_MAX;
1153 5577 hflip_set = hflip != -1;
1154 5577 vflip_set = vflip != -1;
1155
1156
4/6
✓ Branch 0 taken 5576 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 5576 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 5576 times.
✗ Branch 5 not taken.
5577 if (!rotation_set && !hflip_set && !vflip_set)
1157 5576 return 0;
1158
1159 1 sd = av_packet_side_data_new(&st->codecpar->coded_side_data,
1160 1 &st->codecpar->nb_coded_side_data,
1161 AV_PKT_DATA_DISPLAYMATRIX,
1162 sizeof(int32_t) * 9, 0);
1163
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!sd) {
1164 av_log(ist, AV_LOG_FATAL, "Failed to generate a display matrix!\n");
1165 return AVERROR(ENOMEM);
1166 }
1167
1168 1 buf = (int32_t *)sd->data;
1169
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
2 av_display_rotation_set(buf,
1170 1 rotation_set ? -(rotation) : -0.0f);
1171
1172
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,
1173 hflip_set ? hflip : 0,
1174 vflip_set ? vflip : 0);
1175
1176 1 return 0;
1177 }
1178
1179 1380 static const char *input_stream_item_name(void *obj)
1180 {
1181 1380 const DemuxStream *ds = obj;
1182
1183 1380 return ds->log_name;
1184 }
1185
1186 static const AVClass input_stream_class = {
1187 .class_name = "InputStream",
1188 .version = LIBAVUTIL_VERSION_INT,
1189 .item_name = input_stream_item_name,
1190 .category = AV_CLASS_CATEGORY_DEMUXER,
1191 };
1192
1193 7378 static DemuxStream *demux_stream_alloc(Demuxer *d, AVStream *st)
1194 {
1195 7378 const char *type_str = av_get_media_type_string(st->codecpar->codec_type);
1196 7378 InputFile *f = &d->f;
1197 DemuxStream *ds;
1198
1199 7378 ds = allocate_array_elem(&f->streams, sizeof(*ds), &f->nb_streams);
1200
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7378 times.
7378 if (!ds)
1201 return NULL;
1202
1203 7378 ds->sch_idx_stream = -1;
1204 7378 ds->sch_idx_dec = -1;
1205
1206 7378 ds->ist.st = st;
1207 7378 ds->ist.file = f;
1208 7378 ds->ist.index = st->index;
1209 7378 ds->ist.class = &input_stream_class;
1210
1211
1/2
✓ Branch 0 taken 7378 times.
✗ Branch 1 not taken.
7378 snprintf(ds->log_name, sizeof(ds->log_name), "%cist#%d:%d/%s",
1212 7378 type_str ? *type_str : '?', d->f.index, st->index,
1213 7378 avcodec_get_name(st->codecpar->codec_id));
1214
1215 7378 return ds;
1216 }
1217
1218 7378 static int ist_add(const OptionsContext *o, Demuxer *d, AVStream *st, AVDictionary **opts_used)
1219 {
1220 7378 AVFormatContext *ic = d->f.ctx;
1221 7378 AVCodecParameters *par = st->codecpar;
1222 DemuxStream *ds;
1223 InputStream *ist;
1224 7378 const char *framerate = NULL, *hwaccel_device = NULL;
1225 7378 const char *hwaccel = NULL;
1226 7378 const char *apply_cropping = NULL;
1227 7378 const char *hwaccel_output_format = NULL;
1228 7378 const char *codec_tag = NULL;
1229 7378 const char *bsfs = NULL;
1230 char *next;
1231 7378 const char *discard_str = NULL;
1232 int ret;
1233
1234 7378 ds = demux_stream_alloc(d, st);
1235
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7378 times.
7378 if (!ds)
1236 return AVERROR(ENOMEM);
1237
1238 7378 ist = &ds->ist;
1239
1240 7378 ds->discard = 1;
1241 7378 st->discard = AVDISCARD_ALL;
1242 7378 ds->first_dts = AV_NOPTS_VALUE;
1243 7378 ds->next_dts = AV_NOPTS_VALUE;
1244
1245 7378 ds->dec_opts.time_base = st->time_base;
1246
1247 7378 ds->ts_scale = 1.0;
1248 7378 opt_match_per_stream_dbl(ist, &o->ts_scale, ic, st, &ds->ts_scale);
1249
1250 7378 ds->autorotate = 1;
1251 7378 opt_match_per_stream_int(ist, &o->autorotate, ic, st, &ds->autorotate);
1252
1253 7378 ds->apply_cropping = CROP_ALL;
1254 7378 opt_match_per_stream_str(ist, &o->apply_cropping, ic, st, &apply_cropping);
1255
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7378 times.
7378 if (apply_cropping) {
1256 const AVOption opts[] = {
1257 { "apply_cropping", NULL, 0, AV_OPT_TYPE_INT,
1258 { .i64 = CROP_ALL }, CROP_DISABLED, CROP_CONTAINER, AV_OPT_FLAG_DECODING_PARAM, .unit = "apply_cropping" },
1259 { "none", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CROP_DISABLED }, .unit = "apply_cropping" },
1260 { "all", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CROP_ALL }, .unit = "apply_cropping" },
1261 { "codec", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CROP_CODEC }, .unit = "apply_cropping" },
1262 { "container", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CROP_CONTAINER }, .unit = "apply_cropping" },
1263 { NULL },
1264 };
1265 const AVClass class = {
1266 .class_name = "apply_cropping",
1267 .item_name = av_default_item_name,
1268 .option = opts,
1269 .version = LIBAVUTIL_VERSION_INT,
1270 };
1271 const AVClass *pclass = &class;
1272
1273 ret = av_opt_eval_int(&pclass, opts, apply_cropping, &ds->apply_cropping);
1274 if (ret < 0) {
1275 av_log(ist, AV_LOG_ERROR, "Invalid apply_cropping value '%s'.\n", apply_cropping);
1276 return ret;
1277 }
1278 }
1279
1280 7378 opt_match_per_stream_str(ist, &o->codec_tags, ic, st, &codec_tag);
1281
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 7376 times.
7378 if (codec_tag) {
1282 2 uint32_t tag = strtol(codec_tag, &next, 0);
1283
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (*next) {
1284 2 uint8_t buf[4] = { 0 };
1285
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 memcpy(buf, codec_tag, FFMIN(sizeof(buf), strlen(codec_tag)));
1286 2 tag = AV_RL32(buf);
1287 }
1288
1289 2 st->codecpar->codec_tag = tag;
1290 }
1291
1292
2/2
✓ Branch 0 taken 5577 times.
✓ Branch 1 taken 1801 times.
7378 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
1293 5577 ret = add_display_matrix_to_stream(o, ic, ist);
1294
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5577 times.
5577 if (ret < 0)
1295 return ret;
1296
1297 5577 opt_match_per_stream_str(ist, &o->hwaccels, ic, st, &hwaccel);
1298 5577 opt_match_per_stream_str(ist, &o->hwaccel_output_formats, ic, st,
1299 &hwaccel_output_format);
1300
4/6
✓ Branch 0 taken 5577 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5281 times.
✓ Branch 3 taken 296 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 5281 times.
5577 if (!hwaccel_output_format && hwaccel && !strcmp(hwaccel, "cuvid")) {
1301 av_log(ist, AV_LOG_WARNING,
1302 "WARNING: defaulting hwaccel_output_format to cuda for compatibility "
1303 "with old commandlines. This behaviour is DEPRECATED and will be removed "
1304 "in the future. Please explicitly set \"-hwaccel_output_format cuda\".\n");
1305 ds->dec_opts.hwaccel_output_format = AV_PIX_FMT_CUDA;
1306
4/6
✓ Branch 0 taken 5577 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5281 times.
✓ Branch 3 taken 296 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 5281 times.
5577 } else if (!hwaccel_output_format && hwaccel && !strcmp(hwaccel, "qsv")) {
1307 av_log(ist, AV_LOG_WARNING,
1308 "WARNING: defaulting hwaccel_output_format to qsv for compatibility "
1309 "with old commandlines. This behaviour is DEPRECATED and will be removed "
1310 "in the future. Please explicitly set \"-hwaccel_output_format qsv\".\n");
1311 ds->dec_opts.hwaccel_output_format = AV_PIX_FMT_QSV;
1312
4/6
✓ Branch 0 taken 5577 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5281 times.
✓ Branch 3 taken 296 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 5281 times.
5577 } else if (!hwaccel_output_format && hwaccel && !strcmp(hwaccel, "mediacodec")) {
1313 // There is no real AVHWFrameContext implementation. Set
1314 // hwaccel_output_format to avoid av_hwframe_transfer_data error.
1315 ds->dec_opts.hwaccel_output_format = AV_PIX_FMT_MEDIACODEC;
1316
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5577 times.
5577 } else if (hwaccel_output_format) {
1317 ds->dec_opts.hwaccel_output_format = av_get_pix_fmt(hwaccel_output_format);
1318 if (ds->dec_opts.hwaccel_output_format == AV_PIX_FMT_NONE) {
1319 av_log(ist, AV_LOG_FATAL, "Unrecognised hwaccel output "
1320 "format: %s", hwaccel_output_format);
1321 }
1322 } else {
1323 5577 ds->dec_opts.hwaccel_output_format = AV_PIX_FMT_NONE;
1324 }
1325
1326
2/2
✓ Branch 0 taken 5281 times.
✓ Branch 1 taken 296 times.
5577 if (hwaccel) {
1327 // The NVDEC hwaccels use a CUDA device, so remap the name here.
1328
2/4
✓ Branch 0 taken 5281 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5281 times.
5281 if (!strcmp(hwaccel, "nvdec") || !strcmp(hwaccel, "cuvid"))
1329 hwaccel = "cuda";
1330
1331
1/2
✓ Branch 0 taken 5281 times.
✗ Branch 1 not taken.
5281 if (!strcmp(hwaccel, "none"))
1332 5281 ds->dec_opts.hwaccel_id = HWACCEL_NONE;
1333 else if (!strcmp(hwaccel, "auto"))
1334 ds->dec_opts.hwaccel_id = HWACCEL_AUTO;
1335 else {
1336 enum AVHWDeviceType type = av_hwdevice_find_type_by_name(hwaccel);
1337 if (type != AV_HWDEVICE_TYPE_NONE) {
1338 ds->dec_opts.hwaccel_id = HWACCEL_GENERIC;
1339 ds->dec_opts.hwaccel_device_type = type;
1340 }
1341
1342 if (!ds->dec_opts.hwaccel_id) {
1343 av_log(ist, AV_LOG_FATAL, "Unrecognized hwaccel: %s.\n",
1344 hwaccel);
1345 av_log(ist, AV_LOG_FATAL, "Supported hwaccels: ");
1346 type = AV_HWDEVICE_TYPE_NONE;
1347 while ((type = av_hwdevice_iterate_types(type)) !=
1348 AV_HWDEVICE_TYPE_NONE)
1349 av_log(ist, AV_LOG_FATAL, "%s ",
1350 av_hwdevice_get_type_name(type));
1351 av_log(ist, AV_LOG_FATAL, "\n");
1352 return AVERROR(EINVAL);
1353 }
1354 }
1355 }
1356
1357 5577 opt_match_per_stream_str(ist, &o->hwaccel_devices, ic, st, &hwaccel_device);
1358
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5577 times.
5577 if (hwaccel_device) {
1359 ds->dec_opts.hwaccel_device = av_strdup(hwaccel_device);
1360 if (!ds->dec_opts.hwaccel_device)
1361 return AVERROR(ENOMEM);
1362 }
1363 }
1364
1365 7378 ret = choose_decoder(o, ist, ic, st, ds->dec_opts.hwaccel_id,
1366 ds->dec_opts.hwaccel_device_type, &ist->dec);
1367
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7378 times.
7378 if (ret < 0)
1368 return ret;
1369
1370
2/2
✓ Branch 0 taken 7302 times.
✓ Branch 1 taken 76 times.
7378 if (ist->dec) {
1371 7302 ret = filter_codec_opts(o->g->codec_opts, ist->st->codecpar->codec_id,
1372 ic, st, ist->dec, &ds->decoder_opts, opts_used);
1373
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7302 times.
7302 if (ret < 0)
1374 return ret;
1375 }
1376
1377 7378 ds->reinit_filters = -1;
1378 7378 opt_match_per_stream_int(ist, &o->reinit_filters, ic, st, &ds->reinit_filters);
1379
1380 7378 ist->user_set_discard = AVDISCARD_NONE;
1381
1382
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 7378 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
7378 if ((o->video_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) ||
1383
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7377 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
7378 (o->audio_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) ||
1384
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 7377 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
7377 (o->subtitle_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) ||
1385
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 7377 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
7377 (o->data_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_DATA))
1386 1 ist->user_set_discard = AVDISCARD_ALL;
1387
1388 7378 opt_match_per_stream_str(ist, &o->discard, ic, st, &discard_str);
1389
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7378 times.
7378 if (discard_str) {
1390 ret = av_opt_set(ist->st, "discard", discard_str, 0);
1391 if (ret < 0) {
1392 av_log(ist, AV_LOG_ERROR, "Error parsing discard %s.\n", discard_str);
1393 return ret;
1394 }
1395 ist->user_set_discard = ist->st->discard;
1396 }
1397
1398
2/2
✓ Branch 0 taken 96 times.
✓ Branch 1 taken 7282 times.
7378 ds->dec_opts.flags |= DECODER_FLAG_BITEXACT * !!o->bitexact;
1399
1400 7378 av_dict_set_int(&ds->decoder_opts, "apply_cropping",
1401
2/4
✓ Branch 0 taken 7378 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7378 times.
✗ Branch 3 not taken.
7378 ds->apply_cropping && ds->apply_cropping != CROP_CONTAINER, 0);
1402
1403 /* Attached pics are sparse, therefore we would not want to delay their decoding
1404 * till EOF. */
1405
2/2
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 7341 times.
7378 if (ist->st->disposition & AV_DISPOSITION_ATTACHED_PIC)
1406 37 av_dict_set(&ds->decoder_opts, "thread_type", "-frame", 0);
1407
1408
4/5
✓ Branch 0 taken 5577 times.
✓ Branch 1 taken 1655 times.
✓ Branch 2 taken 144 times.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
7378 switch (par->codec_type) {
1409 5577 case AVMEDIA_TYPE_VIDEO:
1410 5577 opt_match_per_stream_str(ist, &o->frame_rates, ic, st, &framerate);
1411
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 5551 times.
5577 if (framerate) {
1412 26 ret = av_parse_video_rate(&ist->framerate, framerate);
1413
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
26 if (ret < 0) {
1414 av_log(ist, AV_LOG_ERROR, "Error parsing framerate %s.\n",
1415 framerate);
1416 return ret;
1417 }
1418 }
1419
1420 #if FFMPEG_OPT_TOP
1421 5577 ist->top_field_first = -1;
1422 5577 opt_match_per_stream_int(ist, &o->top_field_first, ic, st, &ist->top_field_first);
1423 #endif
1424
1425 5577 break;
1426 1655 case AVMEDIA_TYPE_AUDIO: {
1427 1655 const char *ch_layout_str = NULL;
1428
1429 1655 opt_match_per_stream_str(ist, &o->audio_ch_layouts, ic, st, &ch_layout_str);
1430
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1654 times.
1655 if (ch_layout_str) {
1431 AVChannelLayout ch_layout;
1432 1 ret = av_channel_layout_from_string(&ch_layout, ch_layout_str);
1433
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0) {
1434 av_log(ist, AV_LOG_ERROR, "Error parsing channel layout %s.\n", ch_layout_str);
1435 return ret;
1436 }
1437
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) {
1438 1 av_channel_layout_uninit(&par->ch_layout);
1439 1 par->ch_layout = ch_layout;
1440 } else {
1441 av_log(ist, AV_LOG_ERROR,
1442 "Specified channel layout '%s' has %d channels, but input has %d channels.\n",
1443 ch_layout_str, ch_layout.nb_channels, par->ch_layout.nb_channels);
1444 av_channel_layout_uninit(&ch_layout);
1445 return AVERROR(EINVAL);
1446 }
1447 } else {
1448 1654 int guess_layout_max = INT_MAX;
1449 1654 opt_match_per_stream_int(ist, &o->guess_layout_max, ic, st, &guess_layout_max);
1450 1654 guess_input_channel_layout(ist, par, guess_layout_max);
1451 }
1452 1655 break;
1453 }
1454 144 case AVMEDIA_TYPE_DATA:
1455 case AVMEDIA_TYPE_SUBTITLE: {
1456 144 const char *canvas_size = NULL;
1457
1458 144 opt_match_per_stream_int(ist, &o->fix_sub_duration, ic, st, &ist->fix_sub_duration);
1459 144 opt_match_per_stream_str(ist, &o->canvas_sizes, ic, st, &canvas_size);
1460
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 144 times.
144 if (canvas_size) {
1461 ret = av_parse_video_size(&par->width, &par->height,
1462 canvas_size);
1463 if (ret < 0) {
1464 av_log(ist, AV_LOG_FATAL, "Invalid canvas size: %s.\n", canvas_size);
1465 return ret;
1466 }
1467 }
1468 144 break;
1469 }
1470 2 case AVMEDIA_TYPE_ATTACHMENT:
1471 case AVMEDIA_TYPE_UNKNOWN:
1472 2 break;
1473 default: av_assert0(0);
1474 }
1475
1476 7378 ist->par = avcodec_parameters_alloc();
1477
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7378 times.
7378 if (!ist->par)
1478 return AVERROR(ENOMEM);
1479
1480 7378 ret = avcodec_parameters_copy(ist->par, par);
1481
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7378 times.
7378 if (ret < 0) {
1482 av_log(ist, AV_LOG_ERROR, "Error exporting stream parameters.\n");
1483 return ret;
1484 }
1485
1486
2/2
✓ Branch 0 taken 428 times.
✓ Branch 1 taken 6950 times.
7378 if (ist->st->sample_aspect_ratio.num)
1487 428 ist->par->sample_aspect_ratio = ist->st->sample_aspect_ratio;
1488
1489 7378 opt_match_per_stream_str(ist, &o->bitstream_filters, ic, st, &bsfs);
1490
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7377 times.
7378 if (bsfs) {
1491 1 ret = av_bsf_list_parse_str(bsfs, &ds->bsf);
1492
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0) {
1493 av_log(ist, AV_LOG_ERROR,
1494 "Error parsing bitstream filter sequence '%s': %s\n",
1495 bsfs, av_err2str(ret));
1496 return ret;
1497 }
1498
1499 1 ret = avcodec_parameters_copy(ds->bsf->par_in, ist->par);
1500
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
1501 return ret;
1502 1 ds->bsf->time_base_in = ist->st->time_base;
1503
1504 1 ret = av_bsf_init(ds->bsf);
1505
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0) {
1506 av_log(ist, AV_LOG_ERROR, "Error initializing bitstream filters: %s\n",
1507 av_err2str(ret));
1508 return ret;
1509 }
1510
1511 1 ret = avcodec_parameters_copy(ist->par, ds->bsf->par_out);
1512
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
1513 return ret;
1514 }
1515
1516 7378 ds->codec_desc = avcodec_descriptor_get(ist->par->codec_id);
1517
1518 7378 return 0;
1519 }
1520
1521 static int dump_attachment(InputStream *ist, const char *filename)
1522 {
1523 AVStream *st = ist->st;
1524 int ret;
1525 AVIOContext *out = NULL;
1526 const AVDictionaryEntry *e;
1527
1528 if (!st->codecpar->extradata_size) {
1529 av_log(ist, AV_LOG_WARNING, "No extradata to dump.\n");
1530 return 0;
1531 }
1532 if (!*filename && (e = av_dict_get(st->metadata, "filename", NULL, 0)))
1533 filename = e->value;
1534 if (!*filename) {
1535 av_log(ist, AV_LOG_FATAL, "No filename specified and no 'filename' tag");
1536 return AVERROR(EINVAL);
1537 }
1538
1539 ret = assert_file_overwrite(filename);
1540 if (ret < 0)
1541 return ret;
1542
1543 if ((ret = avio_open2(&out, filename, AVIO_FLAG_WRITE, &int_cb, NULL)) < 0) {
1544 av_log(ist, AV_LOG_FATAL, "Could not open file %s for writing.\n",
1545 filename);
1546 return ret;
1547 }
1548
1549 avio_write(out, st->codecpar->extradata, st->codecpar->extradata_size);
1550 ret = avio_close(out);
1551
1552 if (ret >= 0)
1553 av_log(ist, AV_LOG_INFO, "Wrote attachment (%d bytes) to '%s'\n",
1554 st->codecpar->extradata_size, filename);
1555
1556 return ret;
1557 }
1558
1559 623 static const char *input_file_item_name(void *obj)
1560 {
1561 623 const Demuxer *d = obj;
1562
1563 623 return d->log_name;
1564 }
1565
1566 static const AVClass input_file_class = {
1567 .class_name = "InputFile",
1568 .version = LIBAVUTIL_VERSION_INT,
1569 .item_name = input_file_item_name,
1570 .category = AV_CLASS_CATEGORY_DEMUXER,
1571 };
1572
1573 6846 static Demuxer *demux_alloc(void)
1574 {
1575 6846 Demuxer *d = allocate_array_elem(&input_files, sizeof(*d), &nb_input_files);
1576
1577
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6846 times.
6846 if (!d)
1578 return NULL;
1579
1580 6846 d->f.class = &input_file_class;
1581 6846 d->f.index = nb_input_files - 1;
1582
1583 6846 snprintf(d->log_name, sizeof(d->log_name), "in#%d", d->f.index);
1584
1585 6846 return d;
1586 }
1587
1588 6846 int ifile_open(const OptionsContext *o, const char *filename, Scheduler *sch)
1589 {
1590 Demuxer *d;
1591 InputFile *f;
1592 AVFormatContext *ic;
1593 6846 const AVInputFormat *file_iformat = NULL;
1594 6846 int err, ret = 0;
1595 int64_t timestamp;
1596 6846 AVDictionary *opts_used = NULL;
1597 6846 const char* video_codec_name = NULL;
1598 6846 const char* audio_codec_name = NULL;
1599 6846 const char* subtitle_codec_name = NULL;
1600 6846 const char* data_codec_name = NULL;
1601 6846 int scan_all_pmts_set = 0;
1602
1603 6846 int64_t start_time = o->start_time;
1604 6846 int64_t start_time_eof = o->start_time_eof;
1605 6846 int64_t stop_time = o->stop_time;
1606 6846 int64_t recording_time = o->recording_time;
1607
1608 6846 d = demux_alloc();
1609
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6846 times.
6846 if (!d)
1610 return AVERROR(ENOMEM);
1611
1612 6846 f = &d->f;
1613
1614 6846 ret = sch_add_demux(sch, input_thread, d);
1615
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6846 times.
6846 if (ret < 0)
1616 return ret;
1617 6846 d->sch = sch;
1618
1619
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 6846 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
6846 if (stop_time != INT64_MAX && recording_time != INT64_MAX) {
1620 stop_time = INT64_MAX;
1621 av_log(d, AV_LOG_WARNING, "-t and -to cannot be used together; using -t.\n");
1622 }
1623
1624
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 6846 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
6846 if (stop_time != INT64_MAX && recording_time == INT64_MAX) {
1625 int64_t start = start_time == AV_NOPTS_VALUE ? 0 : start_time;
1626 if (stop_time <= start) {
1627 av_log(d, AV_LOG_ERROR, "-to value smaller than -ss; aborting.\n");
1628 return AVERROR(EINVAL);
1629 } else {
1630 recording_time = stop_time - start;
1631 }
1632 }
1633
1634
2/2
✓ Branch 0 taken 3364 times.
✓ Branch 1 taken 3482 times.
6846 if (o->format) {
1635
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3364 times.
3364 if (!(file_iformat = av_find_input_format(o->format))) {
1636 av_log(d, AV_LOG_FATAL, "Unknown input format: '%s'\n", o->format);
1637 return AVERROR(EINVAL);
1638 }
1639 }
1640
1641
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6846 times.
6846 if (!strcmp(filename, "-"))
1642 filename = "fd:";
1643
1644 20538 stdin_interaction &= strncmp(filename, "pipe:", 5) &&
1645
2/4
✓ Branch 0 taken 6846 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6846 times.
✗ Branch 3 not taken.
13692 strcmp(filename, "fd:") &&
1646
1/2
✓ Branch 0 taken 6846 times.
✗ Branch 1 not taken.
6846 strcmp(filename, "/dev/stdin");
1647
1648 /* get default parameters from command line */
1649 6846 ic = avformat_alloc_context();
1650
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6846 times.
6846 if (!ic)
1651 return AVERROR(ENOMEM);
1652
2/2
✓ Branch 0 taken 67 times.
✓ Branch 1 taken 6779 times.
6846 if (o->audio_sample_rate.nb_opt) {
1653 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);
1654 }
1655
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 6837 times.
6846 if (o->audio_channels.nb_opt) {
1656 const AVClass *priv_class;
1657
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) &&
1658 9 av_opt_find(&priv_class, "ch_layout", NULL, 0,
1659 AV_OPT_SEARCH_FAKE_OBJ)) {
1660 char buf[32];
1661 9 snprintf(buf, sizeof(buf), "%dC", o->audio_channels.opt[o->audio_channels.nb_opt - 1].u.i);
1662 9 av_dict_set(&o->g->format_opts, "ch_layout", buf, 0);
1663 }
1664 }
1665
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6845 times.
6846 if (o->audio_ch_layouts.nb_opt) {
1666 const AVClass *priv_class;
1667
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) &&
1668 1 av_opt_find(&priv_class, "ch_layout", NULL, 0,
1669 AV_OPT_SEARCH_FAKE_OBJ)) {
1670 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);
1671 }
1672 }
1673
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 6820 times.
6846 if (o->frame_rates.nb_opt) {
1674 const AVClass *priv_class;
1675 /* set the format-level framerate option;
1676 * this is important for video grabbers, e.g. x11 */
1677
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) &&
1678 25 av_opt_find(&priv_class, "framerate", NULL, 0,
1679 AV_OPT_SEARCH_FAKE_OBJ)) {
1680 25 av_dict_set(&o->g->format_opts, "framerate",
1681 25 o->frame_rates.opt[o->frame_rates.nb_opt - 1].u.str, 0);
1682 }
1683 }
1684
2/2
✓ Branch 0 taken 552 times.
✓ Branch 1 taken 6294 times.
6846 if (o->frame_sizes.nb_opt) {
1685 552 av_dict_set(&o->g->format_opts, "video_size", o->frame_sizes.opt[o->frame_sizes.nb_opt - 1].u.str, 0);
1686 }
1687
2/2
✓ Branch 0 taken 561 times.
✓ Branch 1 taken 6285 times.
6846 if (o->frame_pix_fmts.nb_opt)
1688 561 av_dict_set(&o->g->format_opts, "pixel_format", o->frame_pix_fmts.opt[o->frame_pix_fmts.nb_opt - 1].u.str, 0);
1689
1690 6846 video_codec_name = opt_match_per_type_str(&o->codec_names, 'v');
1691 6846 audio_codec_name = opt_match_per_type_str(&o->codec_names, 'a');
1692 6846 subtitle_codec_name = opt_match_per_type_str(&o->codec_names, 's');
1693 6846 data_codec_name = opt_match_per_type_str(&o->codec_names, 'd');
1694
1695
2/2
✓ Branch 0 taken 2727 times.
✓ Branch 1 taken 4119 times.
6846 if (video_codec_name)
1696 2727 ret = err_merge(ret, find_codec(NULL, video_codec_name , AVMEDIA_TYPE_VIDEO , 0,
1697 2727 &ic->video_codec));
1698
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 6828 times.
6846 if (audio_codec_name)
1699 18 ret = err_merge(ret, find_codec(NULL, audio_codec_name , AVMEDIA_TYPE_AUDIO , 0,
1700 18 &ic->audio_codec));
1701
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6846 times.
6846 if (subtitle_codec_name)
1702 ret = err_merge(ret, find_codec(NULL, subtitle_codec_name, AVMEDIA_TYPE_SUBTITLE, 0,
1703 &ic->subtitle_codec));
1704
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6846 times.
6846 if (data_codec_name)
1705 ret = err_merge(ret, find_codec(NULL, data_codec_name , AVMEDIA_TYPE_DATA, 0,
1706 &ic->data_codec));
1707
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6846 times.
6846 if (ret < 0) {
1708 avformat_free_context(ic);
1709 return ret;
1710 }
1711
1712
2/2
✓ Branch 0 taken 2727 times.
✓ Branch 1 taken 4119 times.
6846 ic->video_codec_id = video_codec_name ? ic->video_codec->id : AV_CODEC_ID_NONE;
1713
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 6828 times.
6846 ic->audio_codec_id = audio_codec_name ? ic->audio_codec->id : AV_CODEC_ID_NONE;
1714
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6846 times.
6846 ic->subtitle_codec_id = subtitle_codec_name ? ic->subtitle_codec->id : AV_CODEC_ID_NONE;
1715
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6846 times.
6846 ic->data_codec_id = data_codec_name ? ic->data_codec->id : AV_CODEC_ID_NONE;
1716
1717 6846 ic->flags |= AVFMT_FLAG_NONBLOCK;
1718
2/2
✓ Branch 0 taken 85 times.
✓ Branch 1 taken 6761 times.
6846 if (o->bitexact)
1719 85 ic->flags |= AVFMT_FLAG_BITEXACT;
1720 6846 ic->interrupt_callback = int_cb;
1721
1722
1/2
✓ Branch 1 taken 6846 times.
✗ Branch 2 not taken.
6846 if (!av_dict_get(o->g->format_opts, "scan_all_pmts", NULL, AV_DICT_MATCH_CASE)) {
1723 6846 av_dict_set(&o->g->format_opts, "scan_all_pmts", "1", AV_DICT_DONT_OVERWRITE);
1724 6846 scan_all_pmts_set = 1;
1725 }
1726 /* open the input file with generic avformat function */
1727 6846 err = avformat_open_input(&ic, filename, file_iformat, &o->g->format_opts);
1728
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6846 times.
6846 if (err < 0) {
1729 av_log(d, AV_LOG_ERROR,
1730 "Error opening input: %s\n", av_err2str(err));
1731 if (err == AVERROR_PROTOCOL_NOT_FOUND)
1732 av_log(d, AV_LOG_ERROR, "Did you mean file:%s?\n", filename);
1733 return err;
1734 }
1735 6846 f->ctx = ic;
1736
1737 6846 av_strlcat(d->log_name, "/", sizeof(d->log_name));
1738 6846 av_strlcat(d->log_name, ic->iformat->name, sizeof(d->log_name));
1739
1740
1/2
✓ Branch 0 taken 6846 times.
✗ Branch 1 not taken.
6846 if (scan_all_pmts_set)
1741 6846 av_dict_set(&o->g->format_opts, "scan_all_pmts", NULL, AV_DICT_MATCH_CASE);
1742 6846 remove_avoptions(&o->g->format_opts, o->g->codec_opts);
1743
1744 6846 ret = check_avoptions(o->g->format_opts);
1745
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6846 times.
6846 if (ret < 0)
1746 return ret;
1747
1748 /* apply forced codec ids */
1749
2/2
✓ Branch 0 taken 7248 times.
✓ Branch 1 taken 6846 times.
14094 for (int i = 0; i < ic->nb_streams; i++) {
1750 const AVCodec *dummy;
1751 7248 ret = choose_decoder(o, f, ic, ic->streams[i], HWACCEL_NONE, AV_HWDEVICE_TYPE_NONE,
1752 &dummy);
1753
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7248 times.
7248 if (ret < 0)
1754 return ret;
1755 }
1756
1757
2/2
✓ Branch 0 taken 6844 times.
✓ Branch 1 taken 2 times.
6846 if (o->find_stream_info) {
1758 AVDictionary **opts;
1759 6844 int orig_nb_streams = ic->nb_streams;
1760
1761 6844 ret = setup_find_stream_info_opts(ic, o->g->codec_opts, &opts);
1762
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6844 times.
6844 if (ret < 0)
1763 return ret;
1764
1765 /* If not enough info to get the stream parameters, we decode the
1766 first frames to get it. (used in mpeg case for example) */
1767 6844 ret = avformat_find_stream_info(ic, opts);
1768
1769
2/2
✓ Branch 0 taken 7246 times.
✓ Branch 1 taken 6844 times.
14090 for (int i = 0; i < orig_nb_streams; i++)
1770 7246 av_dict_free(&opts[i]);
1771 6844 av_freep(&opts);
1772
1773
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6844 times.
6844 if (ret < 0) {
1774 av_log(d, AV_LOG_FATAL, "could not find codec parameters\n");
1775 if (ic->nb_streams == 0)
1776 return ret;
1777 }
1778 }
1779
1780
3/4
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 6825 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 21 times.
6846 if (start_time != AV_NOPTS_VALUE && start_time_eof != AV_NOPTS_VALUE) {
1781 av_log(d, AV_LOG_WARNING, "Cannot use -ss and -sseof both, using -ss\n");
1782 start_time_eof = AV_NOPTS_VALUE;
1783 }
1784
1785
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6846 times.
6846 if (start_time_eof != AV_NOPTS_VALUE) {
1786 if (start_time_eof >= 0) {
1787 av_log(d, AV_LOG_ERROR, "-sseof value must be negative; aborting\n");
1788 return AVERROR(EINVAL);
1789 }
1790 if (ic->duration > 0) {
1791 start_time = start_time_eof + ic->duration;
1792 if (start_time < 0) {
1793 av_log(d, AV_LOG_WARNING, "-sseof value seeks to before start of file; ignored\n");
1794 start_time = AV_NOPTS_VALUE;
1795 }
1796 } else
1797 av_log(d, AV_LOG_WARNING, "Cannot use -sseof, file duration not known\n");
1798 }
1799
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 6825 times.
6846 timestamp = (start_time == AV_NOPTS_VALUE) ? 0 : start_time;
1800 /* add the stream start time */
1801
4/4
✓ Branch 0 taken 6843 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 5134 times.
✓ Branch 3 taken 1709 times.
6846 if (!o->seek_timestamp && ic->start_time != AV_NOPTS_VALUE)
1802 5134 timestamp += ic->start_time;
1803
1804 /* if seeking requested, we execute it */
1805
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 6825 times.
6846 if (start_time != AV_NOPTS_VALUE) {
1806 21 int64_t seek_timestamp = timestamp;
1807
1808
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
21 if (!(ic->iformat->flags & AVFMT_SEEK_TO_PTS)) {
1809 21 int dts_heuristic = 0;
1810
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 21 times.
42 for (int i = 0; i < ic->nb_streams; i++) {
1811 21 const AVCodecParameters *par = ic->streams[i]->codecpar;
1812
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (par->video_delay) {
1813 dts_heuristic = 1;
1814 break;
1815 }
1816 }
1817
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (dts_heuristic) {
1818 seek_timestamp -= 3*AV_TIME_BASE / 23;
1819 }
1820 }
1821 21 ret = avformat_seek_file(ic, -1, INT64_MIN, seek_timestamp, seek_timestamp, 0);
1822
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 20 times.
21 if (ret < 0) {
1823 1 av_log(d, AV_LOG_WARNING, "could not seek to position %0.3f\n",
1824 1 (double)timestamp / AV_TIME_BASE);
1825 }
1826 }
1827
1828 6846 f->start_time = start_time;
1829 6846 d->recording_time = recording_time;
1830 6846 f->input_sync_ref = o->input_sync_ref;
1831 6846 f->input_ts_offset = o->input_ts_offset;
1832
3/6
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 6833 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 13 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
6846 f->ts_offset = o->input_ts_offset - (copy_ts ? (start_at_zero && ic->start_time != AV_NOPTS_VALUE ? ic->start_time : 0) : timestamp);
1833 6846 d->accurate_seek = o->accurate_seek;
1834 6846 d->loop = o->loop;
1835 6846 d->nb_streams_warn = ic->nb_streams;
1836
1837 6846 d->duration = (Timestamp){ .ts = 0, .tb = (AVRational){ 1, 1 } };
1838 6846 d->min_pts = (Timestamp){ .ts = AV_NOPTS_VALUE, .tb = (AVRational){ 1, 1 } };
1839 6846 d->max_pts = (Timestamp){ .ts = AV_NOPTS_VALUE, .tb = (AVRational){ 1, 1 } };
1840
1841 6846 d->readrate = o->readrate ? o->readrate : 0.0;
1842
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6846 times.
6846 if (d->readrate < 0.0f) {
1843 av_log(d, AV_LOG_ERROR, "Option -readrate is %0.3f; it must be non-negative.\n", d->readrate);
1844 return AVERROR(EINVAL);
1845 }
1846
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6845 times.
6846 if (o->rate_emu) {
1847
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (d->readrate) {
1848 av_log(d, AV_LOG_WARNING, "Both -readrate and -re set. Using -readrate %0.3f.\n", d->readrate);
1849 } else
1850 1 d->readrate = 1.0f;
1851 }
1852
1853
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6845 times.
6846 if (d->readrate) {
1854
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;
1855
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (d->readrate_initial_burst < 0.0) {
1856 av_log(d, AV_LOG_ERROR,
1857 "Option -readrate_initial_burst is %0.3f; it must be non-negative.\n",
1858 d->readrate_initial_burst);
1859 return AVERROR(EINVAL);
1860 }
1861
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6845 times.
6845 } else if (o->readrate_initial_burst) {
1862 av_log(d, AV_LOG_WARNING, "Option -readrate_initial_burst ignored "
1863 "since neither -readrate nor -re were given\n");
1864 }
1865
1866 /* Add all the streams from the given input file to the demuxer */
1867
2/2
✓ Branch 0 taken 7378 times.
✓ Branch 1 taken 6846 times.
14224 for (int i = 0; i < ic->nb_streams; i++) {
1868 7378 ret = ist_add(o, d, ic->streams[i], &opts_used);
1869
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7378 times.
7378 if (ret < 0) {
1870 av_dict_free(&opts_used);
1871 return ret;
1872 }
1873 }
1874
1875 /* dump the file content */
1876 6846 av_dump_format(ic, f->index, filename, 0);
1877
1878 /* check if all codec options have been used */
1879 6846 ret = check_avoptions_used(o->g->codec_opts, opts_used, d, 1);
1880 6846 av_dict_free(&opts_used);
1881
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6846 times.
6846 if (ret < 0)
1882 return ret;
1883
1884
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6846 times.
6846 for (int i = 0; i < o->dump_attachment.nb_opt; i++) {
1885 for (int j = 0; j < f->nb_streams; j++) {
1886 InputStream *ist = f->streams[j];
1887
1888 if (check_stream_specifier(ic, ist->st, o->dump_attachment.opt[i].specifier) == 1) {
1889 ret = dump_attachment(ist, o->dump_attachment.opt[i].u.str);
1890 if (ret < 0)
1891 return ret;
1892 }
1893 }
1894 }
1895
1896 6846 return 0;
1897 }
1898