FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/fftools/ffmpeg_demux.c
Date: 2024-11-20 23:03:26
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 1878468 static DemuxStream *ds_from_ist(InputStream *ist)
148 {
149 1878468 return (DemuxStream*)ist;
150 }
151
152 21408 static Demuxer *demuxer_from_ifile(InputFile *f)
153 {
154 21408 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 9890 static void report_new_stream(Demuxer *d, const AVPacket *pkt)
169 {
170 9890 const AVStream *st = d->f.ctx->streams[pkt->stream_index];
171
172
1/2
✓ Branch 0 taken 9890 times.
✗ Branch 1 not taken.
9890 if (pkt->stream_index < d->nb_streams_warn)
173 9890 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 422031 static void ts_discontinuity_detect(Demuxer *d, InputStream *ist,
209 AVPacket *pkt)
210 {
211 422031 InputFile *ifile = &d->f;
212 422031 DemuxStream *ds = ds_from_ist(ist);
213 422031 const int fmt_is_discont = ifile->ctx->iformat->flags & AVFMT_TS_DISCONT;
214 422031 int disable_discontinuity_correction = copy_ts;
215 422031 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 421837 times.
✓ Branch 2 taken 188 times.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 73 times.
✓ Branch 5 taken 115 times.
422031 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 415275 times.
✓ Branch 1 taken 6756 times.
✓ Branch 2 taken 415087 times.
✓ Branch 3 taken 188 times.
837118 if (ds->next_dts != AV_NOPTS_VALUE && !disable_discontinuity_correction) {
228 415087 int64_t delta = pkt_dts - ds->next_dts;
229
2/2
✓ Branch 0 taken 22516 times.
✓ Branch 1 taken 392571 times.
415087 if (fmt_is_discont) {
230
4/4
✓ Branch 0 taken 22460 times.
✓ Branch 1 taken 56 times.
✓ Branch 2 taken 22433 times.
✓ Branch 3 taken 83 times.
22516 if (FFABS(delta) > 1LL * dts_delta_threshold * AV_TIME_BASE ||
231
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 22425 times.
22433 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 389034 times.
✓ Branch 1 taken 3537 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 392571 times.
392571 if (FFABS(delta) > 1LL * dts_error_threshold * AV_TIME_BASE) {
243 av_log(ist, 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 390510 times.
✓ Branch 1 taken 2061 times.
392571 if (pkt->pts != AV_NOPTS_VALUE){
249 390510 int64_t pkt_pts = av_rescale_q(pkt->pts, pkt->time_base, AV_TIME_BASE_Q);
250 390510 delta = pkt_pts - ds->next_dts;
251
3/4
✓ Branch 0 taken 387278 times.
✓ Branch 1 taken 3232 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 390510 times.
390510 if (FFABS(delta) > 1LL * dts_error_threshold * AV_TIME_BASE) {
252 av_log(ist, 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 6756 times.
✓ Branch 1 taken 188 times.
✓ Branch 2 taken 6750 times.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 115 times.
✓ Branch 5 taken 6635 times.
6944 } 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(ist, 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 422031 d->last_ts = av_rescale_q(pkt->dts, pkt->time_base, AV_TIME_BASE_Q);
274 422031 }
275
276 469353 static void ts_discontinuity_process(Demuxer *d, InputStream *ist,
277 AVPacket *pkt)
278 {
279 469353 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 424734 times.
✓ Branch 1 taken 44619 times.
469353 if (pkt->dts != AV_NOPTS_VALUE)
285 424734 pkt->dts += offset;
286
2/2
✓ Branch 0 taken 422633 times.
✓ Branch 1 taken 46720 times.
469353 if (pkt->pts != AV_NOPTS_VALUE)
287 422633 pkt->pts += offset;
288
289 // detect timestamp discontinuities for audio/video
290
2/2
✓ Branch 0 taken 281752 times.
✓ Branch 1 taken 187601 times.
469353 if ((ist->par->codec_type == AVMEDIA_TYPE_VIDEO ||
291
2/2
✓ Branch 0 taken 279045 times.
✓ Branch 1 taken 2707 times.
281752 ist->par->codec_type == AVMEDIA_TYPE_AUDIO) &&
292
2/2
✓ Branch 0 taken 422031 times.
✓ Branch 1 taken 44615 times.
466646 pkt->dts != AV_NOPTS_VALUE)
293 422031 ts_discontinuity_detect(d, ist, pkt);
294 469353 }
295
296 469353 static int ist_dts_update(DemuxStream *ds, AVPacket *pkt, FrameData *fd)
297 {
298 469353 InputStream *ist = &ds->ist;
299 469353 const AVCodecParameters *par = ist->par;
300
301
2/2
✓ Branch 0 taken 7369 times.
✓ Branch 1 taken 461984 times.
469353 if (!ds->saw_first_ts) {
302 7369 ds->first_dts =
303
2/2
✓ Branch 0 taken 5115 times.
✓ Branch 1 taken 2254 times.
7369 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 6777 times.
✓ Branch 1 taken 592 times.
7369 if (pkt->pts != AV_NOPTS_VALUE) {
305 6777 ds->first_dts =
306 6777 ds->dts += av_rescale_q(pkt->pts, pkt->time_base, AV_TIME_BASE_Q);
307 }
308 7369 ds->saw_first_ts = 1;
309 }
310
311
2/2
✓ Branch 0 taken 7369 times.
✓ Branch 1 taken 461984 times.
469353 if (ds->next_dts == AV_NOPTS_VALUE)
312 7369 ds->next_dts = ds->dts;
313
314
2/2
✓ Branch 0 taken 424734 times.
✓ Branch 1 taken 44619 times.
469353 if (pkt->dts != AV_NOPTS_VALUE)
315 424734 ds->next_dts = ds->dts = av_rescale_q(pkt->dts, pkt->time_base, AV_TIME_BASE_Q);
316
317 469353 ds->dts = ds->next_dts;
318
3/3
✓ Branch 0 taken 279045 times.
✓ Branch 1 taken 187601 times.
✓ Branch 2 taken 2707 times.
469353 switch (par->codec_type) {
319 279045 case AVMEDIA_TYPE_AUDIO:
320 av_assert1(pkt->duration >= 0);
321
1/2
✓ Branch 0 taken 279045 times.
✗ Branch 1 not taken.
279045 if (par->sample_rate) {
322 279045 ds->next_dts += ((int64_t)AV_TIME_BASE * par->frame_size) /
323 279045 par->sample_rate;
324 } else {
325 ds->next_dts += av_rescale_q(pkt->duration, pkt->time_base, AV_TIME_BASE_Q);
326 }
327 279045 break;
328 187601 case AVMEDIA_TYPE_VIDEO:
329
2/2
✓ Branch 0 taken 744 times.
✓ Branch 1 taken 186857 times.
187601 if (ist->framerate.num) {
330 // TODO: Remove work-around for c99-to-c89 issue 7
331 744 AVRational time_base_q = AV_TIME_BASE_Q;
332 744 int64_t next_dts = av_rescale_q(ds->next_dts, time_base_q, av_inv_q(ist->framerate));
333 744 ds->next_dts = av_rescale_q(next_dts + 1, av_inv_q(ist->framerate), time_base_q);
334
2/2
✓ Branch 0 taken 184686 times.
✓ Branch 1 taken 2171 times.
186857 } else if (pkt->duration) {
335 184686 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 2101 times.
2171 } 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 187601 break;
349 }
350
351 469353 fd->dts_est = ds->dts;
352
353 469353 return 0;
354 }
355
356 469353 static int ts_fixup(Demuxer *d, AVPacket *pkt, FrameData *fd)
357 {
358 469353 InputFile *ifile = &d->f;
359 469353 InputStream *ist = ifile->streams[pkt->stream_index];
360 469353 DemuxStream *ds = ds_from_ist(ist);
361 469353 const int64_t start_time = ifile->start_time_effective;
362 int64_t duration;
363 int ret;
364
365 469353 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 469353 times.
469353 SHOW_TS_DEBUG("demuxer");
379
380
4/4
✓ Branch 0 taken 421745 times.
✓ Branch 1 taken 47608 times.
✓ Branch 2 taken 325612 times.
✓ Branch 3 taken 96133 times.
469353 if (!ds->wrap_correction_done && start_time != AV_NOPTS_VALUE &&
381
2/2
✓ Branch 0 taken 355 times.
✓ Branch 1 taken 325257 times.
325612 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 424734 times.
✓ Branch 1 taken 44619 times.
469353 if (pkt->dts != AV_NOPTS_VALUE)
399 424734 pkt->dts += av_rescale_q(ifile->ts_offset, AV_TIME_BASE_Q, pkt->time_base);
400
2/2
✓ Branch 0 taken 422633 times.
✓ Branch 1 taken 46720 times.
469353 if (pkt->pts != AV_NOPTS_VALUE)
401 422633 pkt->pts += av_rescale_q(ifile->ts_offset, AV_TIME_BASE_Q, pkt->time_base);
402
403
2/2
✓ Branch 0 taken 422633 times.
✓ Branch 1 taken 46720 times.
469353 if (pkt->pts != AV_NOPTS_VALUE)
404 422633 pkt->pts *= ds->ts_scale;
405
2/2
✓ Branch 0 taken 424734 times.
✓ Branch 1 taken 44619 times.
469353 if (pkt->dts != AV_NOPTS_VALUE)
406 424734 pkt->dts *= ds->ts_scale;
407
408 469353 duration = av_rescale_q(d->duration.ts, d->duration.tb, pkt->time_base);
409
2/2
✓ Branch 0 taken 422633 times.
✓ Branch 1 taken 46720 times.
469353 if (pkt->pts != AV_NOPTS_VALUE) {
410 // audio decoders take precedence for estimating total file duration
411
2/2
✓ Branch 0 taken 191973 times.
✓ Branch 1 taken 230660 times.
422633 int64_t pkt_duration = d->have_audio_dec ? 0 : pkt->duration;
412
413 422633 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 416013 times.
✓ Branch 1 taken 6620 times.
✓ Branch 2 taken 403771 times.
✓ Branch 3 taken 12242 times.
838646 if (d->max_pts.ts == AV_NOPTS_VALUE ||
418 416013 av_compare_ts(d->max_pts.ts, d->max_pts.tb,
419 416013 pkt->pts + pkt_duration, pkt->time_base) < 0) {
420 410391 d->max_pts = (Timestamp){ .ts = pkt->pts + pkt_duration,
421 410391 .tb = pkt->time_base };
422 }
423
4/4
✓ Branch 0 taken 416013 times.
✓ Branch 1 taken 6620 times.
✓ Branch 2 taken 23 times.
✓ Branch 3 taken 415990 times.
838646 if (d->min_pts.ts == AV_NOPTS_VALUE ||
424 416013 av_compare_ts(d->min_pts.ts, d->min_pts.tb,
425 pkt->pts, pkt->time_base) > 0) {
426 6643 d->min_pts = (Timestamp){ .ts = pkt->pts,
427 6643 .tb = pkt->time_base };
428 }
429 }
430
431
2/2
✓ Branch 0 taken 424734 times.
✓ Branch 1 taken 44619 times.
469353 if (pkt->dts != AV_NOPTS_VALUE)
432 424734 pkt->dts += duration;
433
434
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 469353 times.
469353 SHOW_TS_DEBUG("demuxer+tsfixup");
435
436 // detect and try to correct for timestamp discontinuities
437 469353 ts_discontinuity_process(d, ist, pkt);
438
439 // update estimated/predicted dts
440 469353 ret = ist_dts_update(ds, pkt, fd);
441
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 469353 times.
469353 if (ret < 0)
442 return ret;
443
444 469353 return 0;
445 }
446
447 469353 static int input_packet_process(Demuxer *d, AVPacket *pkt, unsigned *send_flags)
448 {
449 469353 InputFile *f = &d->f;
450 469353 InputStream *ist = f->streams[pkt->stream_index];
451 469353 DemuxStream *ds = ds_from_ist(ist);
452 FrameData *fd;
453 469353 int ret = 0;
454
455 469353 fd = packet_data(pkt);
456
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 469353 times.
469353 if (!fd)
457 return AVERROR(ENOMEM);
458
459 469353 ret = ts_fixup(d, pkt, fd);
460
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 469353 times.
469353 if (ret < 0)
461 return ret;
462
463
2/2
✓ Branch 0 taken 227 times.
✓ Branch 1 taken 469126 times.
469353 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 469353 ds->data_size += pkt->size;
474 469353 ds->nb_packets++;
475
476 469353 fd->wallclock[LATENCY_PROBE_DEMUX] = av_gettime_relative();
477
478
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 469353 times.
469353 if (debug_ts) {
479 av_log(ist, AV_LOG_INFO, "demuxer+ffmpeg -> ist_index:%d:%d type:%s pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s duration:%s duration_time:%s off:%s off_time:%s\n",
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 469353 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 470301 static int do_send(Demuxer *d, DemuxStream *ds, AVPacket *pkt, unsigned flags,
512 const char *pkt_desc)
513 {
514 int ret;
515
516 470301 pkt->stream_index = ds->sch_idx_stream;
517
518 470301 ret = sch_demux_send(d->sch, d->f.index, pkt, flags);
519
2/2
✓ Branch 0 taken 3432 times.
✓ Branch 1 taken 466869 times.
470301 if (ret == AVERROR_EOF) {
520 3432 av_packet_unref(pkt);
521
522 3432 av_log(ds, AV_LOG_VERBOSE, "All consumers of this stream are done\n");
523 3432 ds->finished = 1;
524
525
2/2
✓ Branch 0 taken 3403 times.
✓ Branch 1 taken 29 times.
3432 if (++d->nb_streams_finished == d->nb_streams_used) {
526 3403 av_log(d, AV_LOG_VERBOSE, "All consumers are done\n");
527 3403 return AVERROR_EOF;
528 }
529
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 466825 times.
466869 } else if (ret < 0) {
530
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 44 times.
44 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 44 return ret;
535 }
536
537 466854 return 0;
538 }
539
540 469356 static int demux_send(Demuxer *d, DemuxThreadContext *dt, DemuxStream *ds,
541 AVPacket *pkt, unsigned flags)
542 {
543 469356 InputFile *f = &d->f;
544 int ret;
545
546 // pkt can be NULL only when flushing BSFs
547
3/4
✓ Branch 0 taken 469341 times.
✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 469341 times.
469356 av_assert0(ds->bsf || pkt);
548
549 // send heartbeat for sub2video streams
550
4/6
✓ Branch 0 taken 948 times.
✓ Branch 1 taken 468408 times.
✓ Branch 2 taken 948 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 948 times.
✗ Branch 5 not taken.
469356 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 469341 times.
469356 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 469341 ret = do_send(d, ds, pkt, flags, "demuxed");
602
2/2
✓ Branch 0 taken 3447 times.
✓ Branch 1 taken 465894 times.
469341 if (ret < 0)
603 3447 return ret;
604 }
605
606 465894 return 0;
607 }
608
609 3714 static int demux_bsf_flush(Demuxer *d, DemuxThreadContext *dt)
610 {
611 3714 InputFile *f = &d->f;
612 int ret;
613
614
2/2
✓ Branch 0 taken 4188 times.
✓ Branch 1 taken 3714 times.
7902 for (unsigned i = 0; i < f->nb_streams; i++) {
615 4188 DemuxStream *ds = ds_from_ist(f->streams[i]);
616
617
2/2
✓ Branch 0 taken 4185 times.
✓ Branch 1 taken 3 times.
4188 if (!ds->bsf)
618 4185 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 3714 return 0;
632 }
633
634 7150 static void discard_unused_programs(InputFile *ifile)
635 {
636
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 7150 times.
7192 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 7150 }
651
652 7150 static void thread_set_name(InputFile *f)
653 {
654 char name[16];
655 7150 snprintf(name, sizeof(name), "dmx%d:%s", f->index, f->ctx->iformat->name);
656 7150 ff_thread_setname(name);
657 7150 }
658
659 7150 static void demux_thread_uninit(DemuxThreadContext *dt)
660 {
661 7150 av_packet_free(&dt->pkt_demux);
662 7150 av_packet_free(&dt->pkt_bsf);
663
664 7150 memset(dt, 0, sizeof(*dt));
665 7150 }
666
667 7150 static int demux_thread_init(DemuxThreadContext *dt)
668 {
669 7150 memset(dt, 0, sizeof(*dt));
670
671 7150 dt->pkt_demux = av_packet_alloc();
672
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7150 times.
7150 if (!dt->pkt_demux)
673 return AVERROR(ENOMEM);
674
675 7150 dt->pkt_bsf = av_packet_alloc();
676
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7150 times.
7150 if (!dt->pkt_bsf)
677 return AVERROR(ENOMEM);
678
679 7150 return 0;
680 }
681
682 7150 static int input_thread(void *arg)
683 {
684 7150 Demuxer *d = arg;
685 7150 InputFile *f = &d->f;
686
687 DemuxThreadContext dt;
688
689 7150 int ret = 0;
690
691 7150 ret = demux_thread_init(&dt);
692
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7150 times.
7150 if (ret < 0)
693 goto finish;
694
695 7150 thread_set_name(f);
696
697 7150 discard_unused_programs(f);
698
699 7150 d->read_started = 1;
700 7150 d->wallclock_start = av_gettime_relative();
701
702 475808 while (1) {
703 DemuxStream *ds;
704 482958 unsigned send_flags = 0;
705
706 482958 ret = av_read_frame(f->ctx, dt.pkt_demux);
707
708
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 482957 times.
482958 if (ret == AVERROR(EAGAIN)) {
709 1 av_usleep(10000);
710 9902 continue;
711 }
712
2/2
✓ Branch 0 taken 3714 times.
✓ Branch 1 taken 479243 times.
482957 if (ret < 0) {
713 int ret_bsf;
714
715
2/2
✓ Branch 0 taken 3656 times.
✓ Branch 1 taken 58 times.
3714 if (ret == AVERROR_EOF)
716 3656 av_log(d, AV_LOG_VERBOSE, "EOF while reading input\n");
717 else {
718 58 av_log(d, AV_LOG_ERROR, "Error during demuxing: %s\n",
719 58 av_err2str(ret));
720
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
58 ret = exit_on_error ? ret : 0;
721 }
722
723 3714 ret_bsf = demux_bsf_flush(d, &dt);
724
2/2
✓ Branch 0 taken 58 times.
✓ Branch 1 taken 3656 times.
3714 ret = err_merge(ret == AVERROR_EOF ? 0 : ret, ret_bsf);
725
726
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 3703 times.
3714 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 3703 break;
740 }
741
742
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 479243 times.
479243 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 958486 ds = dt.pkt_demux->stream_index < f->nb_streams ?
750
1/2
✓ Branch 0 taken 479243 times.
✗ Branch 1 not taken.
479243 ds_from_ist(f->streams[dt.pkt_demux->stream_index]) : NULL;
751
5/6
✓ Branch 0 taken 479243 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 471119 times.
✓ Branch 3 taken 8124 times.
✓ Branch 4 taken 1766 times.
✓ Branch 5 taken 469353 times.
479243 if (!ds || ds->discard || ds->finished) {
752 9890 report_new_stream(d, dt.pkt_demux);
753 9890 av_packet_unref(dt.pkt_demux);
754 9890 continue;
755 }
756
757
2/2
✓ Branch 0 taken 99 times.
✓ Branch 1 taken 469254 times.
469353 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 469353 ret = input_packet_process(d, dt.pkt_demux, &send_flags);
769
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 469353 times.
469353 if (ret < 0)
770 break;
771
772
2/2
✓ Branch 0 taken 216 times.
✓ Branch 1 taken 469137 times.
469353 if (d->readrate)
773 216 readrate_sleep(d);
774
775 469353 ret = demux_send(d, &dt, ds, dt.pkt_demux, send_flags);
776
2/2
✓ Branch 0 taken 3447 times.
✓ Branch 1 taken 465906 times.
469353 if (ret < 0)
777 3447 break;
778 }
779
780 // EOF/EXIT is normal termination
781
4/4
✓ Branch 0 taken 3747 times.
✓ Branch 1 taken 3403 times.
✓ Branch 2 taken 3703 times.
✓ Branch 3 taken 44 times.
7150 if (ret == AVERROR_EOF || ret == AVERROR_EXIT)
782 3447 ret = 0;
783
784 3703 finish:
785 7150 demux_thread_uninit(&dt);
786
787 7150 return ret;
788 }
789
790 7150 static void demux_final_stats(Demuxer *d)
791 {
792 7150 InputFile *f = &d->f;
793 7150 uint64_t total_packets = 0, total_size = 0;
794
795 7150 av_log(f, AV_LOG_VERBOSE, "Input file #%d (%s):\n",
796 7150 f->index, f->ctx->url);
797
798
2/2
✓ Branch 0 taken 7681 times.
✓ Branch 1 taken 7150 times.
14831 for (int j = 0; j < f->nb_streams; j++) {
799 7681 InputStream *ist = f->streams[j];
800 7681 DemuxStream *ds = ds_from_ist(ist);
801 7681 enum AVMediaType type = ist->par->codec_type;
802
803
3/4
✓ Branch 0 taken 7390 times.
✓ Branch 1 taken 291 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 7390 times.
7681 if (ds->discard || type == AVMEDIA_TYPE_ATTACHMENT)
804 291 continue;
805
806 7390 total_size += ds->data_size;
807 7390 total_packets += ds->nb_packets;
808
809 7390 av_log(f, AV_LOG_VERBOSE, " Input stream #%d:%d (%s): ",
810 f->index, j, av_get_media_type_string(type));
811 7390 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 6753 times.
✓ Branch 1 taken 637 times.
7390 if (ds->decoding_needed) {
815 6753 av_log(f, AV_LOG_VERBOSE,
816 "%"PRIu64" frames decoded; %"PRIu64" decode errors",
817 6753 ist->decoder->frames_decoded, ist->decoder->decode_errors);
818
2/2
✓ Branch 0 taken 1206 times.
✓ Branch 1 taken 5547 times.
6753 if (type == AVMEDIA_TYPE_AUDIO)
819 1206 av_log(f, AV_LOG_VERBOSE, " (%"PRIu64" samples)", ist->decoder->samples_decoded);
820 6753 av_log(f, AV_LOG_VERBOSE, "; ");
821 }
822
823 7390 av_log(f, AV_LOG_VERBOSE, "\n");
824 }
825
826 7150 av_log(f, AV_LOG_VERBOSE, " Total: %"PRIu64" packets (%"PRIu64" bytes) demuxed\n",
827 total_packets, total_size);
828 7150 }
829
830 7696 static void ist_free(InputStream **pist)
831 {
832 7696 InputStream *ist = *pist;
833 DemuxStream *ds;
834
835
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7696 times.
7696 if (!ist)
836 return;
837 7696 ds = ds_from_ist(ist);
838
839 7696 dec_free(&ist->decoder);
840
841 7696 av_dict_free(&ds->decoder_opts);
842 7696 av_freep(&ist->filters);
843 7696 av_freep(&ds->dec_opts.hwaccel_device);
844
845 7696 avcodec_parameters_free(&ist->par);
846
847 7696 av_frame_free(&ds->decoded_params);
848
849 7696 av_bsf_free(&ds->bsf);
850
851 7696 av_freep(pist);
852 }
853
854 7164 void ifile_close(InputFile **pf)
855 {
856 7164 InputFile *f = *pf;
857 7164 Demuxer *d = demuxer_from_ifile(f);
858
859
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7164 times.
7164 if (!f)
860 return;
861
862
2/2
✓ Branch 0 taken 7150 times.
✓ Branch 1 taken 14 times.
7164 if (d->read_started)
863 7150 demux_final_stats(d);
864
865
2/2
✓ Branch 0 taken 7696 times.
✓ Branch 1 taken 7164 times.
14860 for (int i = 0; i < f->nb_streams; i++)
866 7696 ist_free(&f->streams[i]);
867 7164 av_freep(&f->streams);
868
869 7164 avformat_close_input(&f->ctx);
870
871 7164 av_packet_free(&d->pkt_heartbeat);
872
873 7164 av_freep(pf);
874 }
875
876 7471 int ist_use(InputStream *ist, int decoding_needed,
877 const ViewSpecifier *vs, SchedulerNode *src)
878 {
879 7471 Demuxer *d = demuxer_from_ifile(ist->file);
880 7471 DemuxStream *ds = ds_from_ist(ist);
881 int ret;
882
883
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7471 times.
7471 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 6811 times.
✓ Branch 1 taken 660 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6811 times.
7471 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 7390 times.
✓ Branch 1 taken 81 times.
7471 if (ds->sch_idx_stream < 0) {
897 7390 ret = sch_add_demux_stream(d->sch, d->f.index);
898
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7390 times.
7390 if (ret < 0)
899 return ret;
900 7390 ds->sch_idx_stream = ret;
901 }
902
903
2/2
✓ Branch 0 taken 7390 times.
✓ Branch 1 taken 81 times.
7471 if (ds->discard) {
904 7390 ds->discard = 0;
905 7390 d->nb_streams_used++;
906 }
907
908 7471 ist->st->discard = ist->user_set_discard;
909 7471 ds->decoding_needed |= decoding_needed;
910 7471 ds->streamcopy_needed |= !decoding_needed;
911
912
4/4
✓ Branch 0 taken 6811 times.
✓ Branch 1 taken 660 times.
✓ Branch 2 taken 6753 times.
✓ Branch 3 taken 58 times.
7471 if (decoding_needed && ds->sch_idx_dec < 0) {
913 6753 int is_audio = ist->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO;
914
915 13506 ds->dec_opts.flags |= (!!ist->fix_sub_duration * DECODER_FLAG_FIX_SUB_DURATION) |
916 13506 (!!(d->f.ctx->iformat->flags & AVFMT_NOTIMESTAMPS) * DECODER_FLAG_TS_UNRELIABLE) |
917
4/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6751 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
6753 (!!(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 5507 times.
6753 | ((ist->top_field_first >= 0) * DECODER_FLAG_TOP_FIELD_FIRST)
920 #endif
921 ;
922
923
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 6721 times.
6753 if (ist->framerate.num) {
924 32 ds->dec_opts.flags |= DECODER_FLAG_FRAMERATE_FORCED;
925 32 ds->dec_opts.framerate = ist->framerate;
926 } else
927 6721 ds->dec_opts.framerate = ist->st->avg_frame_rate;
928
929
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6752 times.
6753 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 6753 snprintf(ds->dec_name, sizeof(ds->dec_name), "%d:%d", ist->file->index, ist->index);
939 6753 ds->dec_opts.name = ds->dec_name;
940
941 6753 ds->dec_opts.codec = ist->dec;
942 6753 ds->dec_opts.par = ist->par;
943
944 6753 ds->dec_opts.log_parent = ist;
945
946 6753 ds->decoded_params = av_frame_alloc();
947
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6753 times.
6753 if (!ds->decoded_params)
948 return AVERROR(ENOMEM);
949
950 6753 ret = dec_init(&ist->decoder, d->sch,
951 6753 &ds->decoder_opts, &ds->dec_opts, ds->decoded_params);
952
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6753 times.
6753 if (ret < 0)
953 return ret;
954 6753 ds->sch_idx_dec = ret;
955
956 6753 ret = sch_connect(d->sch, SCH_DSTREAM(d->f.index, ds->sch_idx_stream),
957 6753 SCH_DEC_IN(ds->sch_idx_dec));
958
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6753 times.
6753 if (ret < 0)
959 return ret;
960
961 6753 d->have_audio_dec |= is_audio;
962 }
963
964
4/4
✓ Branch 0 taken 6811 times.
✓ Branch 1 taken 660 times.
✓ Branch 2 taken 5517 times.
✓ Branch 3 taken 1294 times.
7471 if (decoding_needed && ist->par->codec_type == AVMEDIA_TYPE_VIDEO) {
965 5517 ret = dec_request_view(ist->decoder, vs, src);
966
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5517 times.
5517 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 7471 return 0;
975 }
976
977 6773 int ist_filter_add(InputStream *ist, InputFilter *ifilter, int is_simple,
978 const ViewSpecifier *vs, InputFilterOptions *opts,
979 SchedulerNode *src)
980 {
981 6773 Demuxer *d = demuxer_from_ifile(ist->file);
982 6773 DemuxStream *ds = ds_from_ist(ist);
983 6773 int64_t tsoffset = 0;
984 int ret;
985
986
2/2
✓ Branch 0 taken 6635 times.
✓ Branch 1 taken 138 times.
6773 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 6773 times.
6773 if (ret < 0)
989 return ret;
990
991 6773 ret = GROW_ARRAY(ist->filters, ist->nb_filters);
992
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6773 times.
6773 if (ret < 0)
993 return ret;
994
995 6773 ist->filters[ist->nb_filters - 1] = ifilter;
996
997
2/2
✓ Branch 0 taken 5517 times.
✓ Branch 1 taken 1256 times.
6773 if (ist->par->codec_type == AVMEDIA_TYPE_VIDEO) {
998 5517 const AVPacketSideData *sd = av_packet_side_data_get(ist->par->coded_side_data,
999 5517 ist->par->nb_coded_side_data,
1000 AV_PKT_DATA_FRAME_CROPPING);
1001
3/4
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 5485 times.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
5517 if (ist->framerate.num > 0 && ist->framerate.den > 0) {
1002 32 opts->framerate = ist->framerate;
1003 32 opts->flags |= IFILTER_FLAG_CFR;
1004 } else
1005 5485 opts->framerate = av_guess_frame_rate(d->f.ctx, ist->st, NULL);
1006
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 5513 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
5517 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 6773 ret = av_frame_copy_props(opts->fallback, ds->decoded_params);
1045
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6773 times.
6773 if (ret < 0)
1046 return ret;
1047 6773 opts->fallback->format = ds->decoded_params->format;
1048 6773 opts->fallback->width = ds->decoded_params->width;
1049 6773 opts->fallback->height = ds->decoded_params->height;
1050
1051 6773 ret = av_channel_layout_copy(&opts->fallback->ch_layout, &ds->decoded_params->ch_layout);
1052
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6773 times.
6773 if (ret < 0)
1053 return ret;
1054
1055
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 6770 times.
6773 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 6757 times.
6789 AV_NOPTS_VALUE : tsoffset;
1062 6773 opts->trim_end_us = d->recording_time;
1063
1064 6773 opts->name = av_strdup(ds->dec_name);
1065
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6773 times.
6773 if (!opts->name)
1066 return AVERROR(ENOMEM);
1067
1068 13546 opts->flags |= IFILTER_FLAG_AUTOROTATE * !!(ds->autorotate) |
1069
1/2
✓ Branch 0 taken 6773 times.
✗ Branch 1 not taken.
6773 IFILTER_FLAG_REINIT * !!(ds->reinit_filters);
1070
1071 6773 return 0;
1072 }
1073
1074 15262 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 15262 const char *codec_name = NULL;
1081
1082 15262 opt_match_per_stream_str(logctx, &o->codec_names, s, st, &codec_name);
1083
2/2
✓ Branch 0 taken 6121 times.
✓ Branch 1 taken 9141 times.
15262 if (codec_name) {
1084 6121 int ret = find_codec(NULL, codec_name, st->codecpar->codec_type, 0, pcodec);
1085
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6121 times.
6121 if (ret < 0)
1086 return ret;
1087 6121 st->codecpar->codec_id = (*pcodec)->id;
1088
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 6121 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
6121 if (recast_media && st->codecpar->codec_type != (*pcodec)->type)
1089 st->codecpar->codec_type = (*pcodec)->type;
1090 6121 return 0;
1091 } else {
1092
3/4
✓ Branch 0 taken 5686 times.
✓ Branch 1 taken 3455 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 5686 times.
9141 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(logctx, 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 9141 *pcodec = avcodec_find_decoder(st->codecpar->codec_id);
1117 9141 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 5895 static int add_display_matrix_to_stream(const OptionsContext *o,
1139 AVFormatContext *ctx, InputStream *ist)
1140 {
1141 5895 AVStream *st = ist->st;
1142 AVPacketSideData *sd;
1143 5895 double rotation = DBL_MAX;
1144 5895 int hflip = -1, vflip = -1;
1145 5895 int hflip_set = 0, vflip_set = 0, rotation_set = 0;
1146 int32_t *buf;
1147
1148 5895 opt_match_per_stream_dbl(ist, &o->display_rotations, ctx, st, &rotation);
1149 5895 opt_match_per_stream_int(ist, &o->display_hflips, ctx, st, &hflip);
1150 5895 opt_match_per_stream_int(ist, &o->display_vflips, ctx, st, &vflip);
1151
1152 5895 rotation_set = rotation != DBL_MAX;
1153 5895 hflip_set = hflip != -1;
1154 5895 vflip_set = vflip != -1;
1155
1156
4/6
✓ Branch 0 taken 5894 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 5894 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 5894 times.
✗ Branch 5 not taken.
5895 if (!rotation_set && !hflip_set && !vflip_set)
1157 5894 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 1379 static const char *input_stream_item_name(void *obj)
1180 {
1181 1379 const DemuxStream *ds = obj;
1182
1183 1379 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 7696 static DemuxStream *demux_stream_alloc(Demuxer *d, AVStream *st)
1194 {
1195 7696 const char *type_str = av_get_media_type_string(st->codecpar->codec_type);
1196 7696 InputFile *f = &d->f;
1197 DemuxStream *ds;
1198
1199 7696 ds = allocate_array_elem(&f->streams, sizeof(*ds), &f->nb_streams);
1200
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7696 times.
7696 if (!ds)
1201 return NULL;
1202
1203 7696 ds->sch_idx_stream = -1;
1204 7696 ds->sch_idx_dec = -1;
1205
1206 7696 ds->ist.st = st;
1207 7696 ds->ist.file = f;
1208 7696 ds->ist.index = st->index;
1209 7696 ds->ist.class = &input_stream_class;
1210
1211
1/2
✓ Branch 0 taken 7696 times.
✗ Branch 1 not taken.
7696 snprintf(ds->log_name, sizeof(ds->log_name), "%cist#%d:%d/%s",
1212 7696 type_str ? *type_str : '?', d->f.index, st->index,
1213 7696 avcodec_get_name(st->codecpar->codec_id));
1214
1215 7696 return ds;
1216 }
1217
1218 7696 static int ist_add(const OptionsContext *o, Demuxer *d, AVStream *st, AVDictionary **opts_used)
1219 {
1220 7696 AVFormatContext *ic = d->f.ctx;
1221 7696 AVCodecParameters *par = st->codecpar;
1222 DemuxStream *ds;
1223 InputStream *ist;
1224 7696 const char *framerate = NULL, *hwaccel_device = NULL;
1225 7696 const char *hwaccel = NULL;
1226 7696 const char *apply_cropping = NULL;
1227 7696 const char *hwaccel_output_format = NULL;
1228 7696 const char *codec_tag = NULL;
1229 7696 const char *bsfs = NULL;
1230 char *next;
1231 7696 const char *discard_str = NULL;
1232 int ret;
1233
1234 7696 ds = demux_stream_alloc(d, st);
1235
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7696 times.
7696 if (!ds)
1236 return AVERROR(ENOMEM);
1237
1238 7696 ist = &ds->ist;
1239
1240 7696 ds->discard = 1;
1241 7696 st->discard = AVDISCARD_ALL;
1242 7696 ds->first_dts = AV_NOPTS_VALUE;
1243 7696 ds->next_dts = AV_NOPTS_VALUE;
1244
1245 7696 ds->dec_opts.time_base = st->time_base;
1246
1247 7696 ds->ts_scale = 1.0;
1248 7696 opt_match_per_stream_dbl(ist, &o->ts_scale, ic, st, &ds->ts_scale);
1249
1250 7696 ds->autorotate = 1;
1251 7696 opt_match_per_stream_int(ist, &o->autorotate, ic, st, &ds->autorotate);
1252
1253 7696 ds->apply_cropping = CROP_ALL;
1254 7696 opt_match_per_stream_str(ist, &o->apply_cropping, ic, st, &apply_cropping);
1255
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7696 times.
7696 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 7696 opt_match_per_stream_str(ist, &o->codec_tags, ic, st, &codec_tag);
1281
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 7694 times.
7696 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 5895 times.
✓ Branch 1 taken 1801 times.
7696 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
1293 5895 ret = add_display_matrix_to_stream(o, ic, ist);
1294
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5895 times.
5895 if (ret < 0)
1295 return ret;
1296
1297 5895 opt_match_per_stream_str(ist, &o->hwaccels, ic, st, &hwaccel);
1298 5895 opt_match_per_stream_str(ist, &o->hwaccel_output_formats, ic, st,
1299 &hwaccel_output_format);
1300
4/6
✓ Branch 0 taken 5895 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5585 times.
✓ Branch 3 taken 310 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 5585 times.
5895 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 5895 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5585 times.
✓ Branch 3 taken 310 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 5585 times.
5895 } 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 5895 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5585 times.
✓ Branch 3 taken 310 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 5585 times.
5895 } 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 5895 times.
5895 } 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 5895 ds->dec_opts.hwaccel_output_format = AV_PIX_FMT_NONE;
1324 }
1325
1326
2/2
✓ Branch 0 taken 5585 times.
✓ Branch 1 taken 310 times.
5895 if (hwaccel) {
1327 // The NVDEC hwaccels use a CUDA device, so remap the name here.
1328
2/4
✓ Branch 0 taken 5585 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5585 times.
5585 if (!strcmp(hwaccel, "nvdec") || !strcmp(hwaccel, "cuvid"))
1329 hwaccel = "cuda";
1330
1331
1/2
✓ Branch 0 taken 5585 times.
✗ Branch 1 not taken.
5585 if (!strcmp(hwaccel, "none"))
1332 5585 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 5895 opt_match_per_stream_str(ist, &o->hwaccel_devices, ic, st, &hwaccel_device);
1358
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5895 times.
5895 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 7696 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 7696 times.
7696 if (ret < 0)
1368 return ret;
1369
1370
2/2
✓ Branch 0 taken 7620 times.
✓ Branch 1 taken 76 times.
7696 if (ist->dec) {
1371 7620 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 7620 times.
7620 if (ret < 0)
1374 return ret;
1375 }
1376
1377 7696 ds->reinit_filters = -1;
1378 7696 opt_match_per_stream_int(ist, &o->reinit_filters, ic, st, &ds->reinit_filters);
1379
1380 7696 ist->user_set_discard = AVDISCARD_NONE;
1381
1382
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 7696 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
7696 if ((o->video_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) ||
1383
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7695 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
7696 (o->audio_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) ||
1384
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 7695 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
7695 (o->subtitle_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) ||
1385
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 7695 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
7695 (o->data_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_DATA))
1386 1 ist->user_set_discard = AVDISCARD_ALL;
1387
1388 7696 opt_match_per_stream_str(ist, &o->discard, ic, st, &discard_str);
1389
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7696 times.
7696 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 7600 times.
7696 ds->dec_opts.flags |= DECODER_FLAG_BITEXACT * !!o->bitexact;
1399
1400 7696 av_dict_set_int(&ds->decoder_opts, "apply_cropping",
1401
2/4
✓ Branch 0 taken 7696 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7696 times.
✗ Branch 3 not taken.
7696 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 7659 times.
7696 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 5895 times.
✓ Branch 1 taken 1655 times.
✓ Branch 2 taken 144 times.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
7696 switch (par->codec_type) {
1409 5895 case AVMEDIA_TYPE_VIDEO:
1410 5895 opt_match_per_stream_str(ist, &o->frame_rates, ic, st, &framerate);
1411
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 5862 times.
5895 if (framerate) {
1412 33 ret = av_parse_video_rate(&ist->framerate, framerate);
1413
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 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 5895 ist->top_field_first = -1;
1422 5895 opt_match_per_stream_int(ist, &o->top_field_first, ic, st, &ist->top_field_first);
1423 #endif
1424
1425 5895 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 7696 ist->par = avcodec_parameters_alloc();
1477
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7696 times.
7696 if (!ist->par)
1478 return AVERROR(ENOMEM);
1479
1480 7696 ret = avcodec_parameters_copy(ist->par, par);
1481
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7696 times.
7696 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 7268 times.
7696 if (ist->st->sample_aspect_ratio.num)
1487 428 ist->par->sample_aspect_ratio = ist->st->sample_aspect_ratio;
1488
1489 7696 opt_match_per_stream_str(ist, &o->bitstream_filters, ic, st, &bsfs);
1490
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7695 times.
7696 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 7696 ds->codec_desc = avcodec_descriptor_get(ist->par->codec_id);
1517
1518 7696 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 625 static const char *input_file_item_name(void *obj)
1560 {
1561 625 const Demuxer *d = obj;
1562
1563 625 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 7164 static Demuxer *demux_alloc(void)
1574 {
1575 7164 Demuxer *d = allocate_array_elem(&input_files, sizeof(*d), &nb_input_files);
1576
1577
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7164 times.
7164 if (!d)
1578 return NULL;
1579
1580 7164 d->f.class = &input_file_class;
1581 7164 d->f.index = nb_input_files - 1;
1582
1583 7164 snprintf(d->log_name, sizeof(d->log_name), "in#%d", d->f.index);
1584
1585 7164 return d;
1586 }
1587
1588 7164 int ifile_open(const OptionsContext *o, const char *filename, Scheduler *sch)
1589 {
1590 Demuxer *d;
1591 InputFile *f;
1592 AVFormatContext *ic;
1593 7164 const AVInputFormat *file_iformat = NULL;
1594 7164 int err, ret = 0;
1595 int64_t timestamp;
1596 7164 AVDictionary *opts_used = NULL;
1597 7164 const char* video_codec_name = NULL;
1598 7164 const char* audio_codec_name = NULL;
1599 7164 const char* subtitle_codec_name = NULL;
1600 7164 const char* data_codec_name = NULL;
1601 7164 int scan_all_pmts_set = 0;
1602
1603 7164 int64_t start_time = o->start_time;
1604 7164 int64_t start_time_eof = o->start_time_eof;
1605 7164 int64_t stop_time = o->stop_time;
1606 7164 int64_t recording_time = o->recording_time;
1607
1608 7164 d = demux_alloc();
1609
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7164 times.
7164 if (!d)
1610 return AVERROR(ENOMEM);
1611
1612 7164 f = &d->f;
1613
1614 7164 ret = sch_add_demux(sch, input_thread, d);
1615
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7164 times.
7164 if (ret < 0)
1616 return ret;
1617 7164 d->sch = sch;
1618
1619
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 7164 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
7164 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 7164 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
7164 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 3673 times.
✓ Branch 1 taken 3491 times.
7164 if (o->format) {
1635
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3673 times.
3673 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 7164 times.
7164 if (!strcmp(filename, "-"))
1642 filename = "fd:";
1643
1644 21492 stdin_interaction &= strncmp(filename, "pipe:", 5) &&
1645
2/4
✓ Branch 0 taken 7164 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7164 times.
✗ Branch 3 not taken.
14328 strcmp(filename, "fd:") &&
1646
1/2
✓ Branch 0 taken 7164 times.
✗ Branch 1 not taken.
7164 strcmp(filename, "/dev/stdin");
1647
1648 /* get default parameters from command line */
1649 7164 ic = avformat_alloc_context();
1650
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7164 times.
7164 if (!ic)
1651 return AVERROR(ENOMEM);
1652
2/2
✓ Branch 0 taken 67 times.
✓ Branch 1 taken 7097 times.
7164 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 7155 times.
7164 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 7163 times.
7164 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 33 times.
✓ Branch 1 taken 7131 times.
7164 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 32 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 32 times.
✗ Branch 5 not taken.
65 if (file_iformat && (priv_class = file_iformat->priv_class) &&
1678 32 av_opt_find(&priv_class, "framerate", NULL, 0,
1679 AV_OPT_SEARCH_FAKE_OBJ)) {
1680 32 av_dict_set(&o->g->format_opts, "framerate",
1681 32 o->frame_rates.opt[o->frame_rates.nb_opt - 1].u.str, 0);
1682 }
1683 }
1684
2/2
✓ Branch 0 taken 563 times.
✓ Branch 1 taken 6601 times.
7164 if (o->frame_sizes.nb_opt) {
1685 563 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 572 times.
✓ Branch 1 taken 6592 times.
7164 if (o->frame_pix_fmts.nb_opt)
1688 572 av_dict_set(&o->g->format_opts, "pixel_format", o->frame_pix_fmts.opt[o->frame_pix_fmts.nb_opt - 1].u.str, 0);
1689
1690 7164 video_codec_name = opt_match_per_type_str(&o->codec_names, 'v');
1691 7164 audio_codec_name = opt_match_per_type_str(&o->codec_names, 'a');
1692 7164 subtitle_codec_name = opt_match_per_type_str(&o->codec_names, 's');
1693 7164 data_codec_name = opt_match_per_type_str(&o->codec_names, 'd');
1694
1695
2/2
✓ Branch 0 taken 3026 times.
✓ Branch 1 taken 4138 times.
7164 if (video_codec_name)
1696 3026 ret = err_merge(ret, find_codec(NULL, video_codec_name , AVMEDIA_TYPE_VIDEO , 0,
1697 3026 &ic->video_codec));
1698
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 7146 times.
7164 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 7164 times.
7164 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 7164 times.
7164 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 7164 times.
7164 if (ret < 0) {
1708 avformat_free_context(ic);
1709 return ret;
1710 }
1711
1712
2/2
✓ Branch 0 taken 3026 times.
✓ Branch 1 taken 4138 times.
7164 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 7146 times.
7164 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 7164 times.
7164 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 7164 times.
7164 ic->data_codec_id = data_codec_name ? ic->data_codec->id : AV_CODEC_ID_NONE;
1716
1717 7164 ic->flags |= AVFMT_FLAG_NONBLOCK;
1718
2/2
✓ Branch 0 taken 85 times.
✓ Branch 1 taken 7079 times.
7164 if (o->bitexact)
1719 85 ic->flags |= AVFMT_FLAG_BITEXACT;
1720 7164 ic->interrupt_callback = int_cb;
1721
1722
1/2
✓ Branch 1 taken 7164 times.
✗ Branch 2 not taken.
7164 if (!av_dict_get(o->g->format_opts, "scan_all_pmts", NULL, AV_DICT_MATCH_CASE)) {
1723 7164 av_dict_set(&o->g->format_opts, "scan_all_pmts", "1", AV_DICT_DONT_OVERWRITE);
1724 7164 scan_all_pmts_set = 1;
1725 }
1726 /* open the input file with generic avformat function */
1727 7164 err = avformat_open_input(&ic, filename, file_iformat, &o->g->format_opts);
1728
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7164 times.
7164 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 7164 f->ctx = ic;
1736
1737 7164 av_strlcat(d->log_name, "/", sizeof(d->log_name));
1738 7164 av_strlcat(d->log_name, ic->iformat->name, sizeof(d->log_name));
1739
1740
1/2
✓ Branch 0 taken 7164 times.
✗ Branch 1 not taken.
7164 if (scan_all_pmts_set)
1741 7164 av_dict_set(&o->g->format_opts, "scan_all_pmts", NULL, AV_DICT_MATCH_CASE);
1742 7164 remove_avoptions(&o->g->format_opts, o->g->codec_opts);
1743
1744 7164 ret = check_avoptions(o->g->format_opts);
1745
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7164 times.
7164 if (ret < 0)
1746 return ret;
1747
1748 /* apply forced codec ids */
1749
2/2
✓ Branch 0 taken 7566 times.
✓ Branch 1 taken 7164 times.
14730 for (int i = 0; i < ic->nb_streams; i++) {
1750 const AVCodec *dummy;
1751 7566 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 7566 times.
7566 if (ret < 0)
1754 return ret;
1755 }
1756
1757
2/2
✓ Branch 0 taken 7162 times.
✓ Branch 1 taken 2 times.
7164 if (o->find_stream_info) {
1758 AVDictionary **opts;
1759 7162 int orig_nb_streams = ic->nb_streams;
1760
1761 7162 ret = setup_find_stream_info_opts(ic, o->g->codec_opts, &opts);
1762
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7162 times.
7162 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 7162 ret = avformat_find_stream_info(ic, opts);
1768
1769
2/2
✓ Branch 0 taken 7564 times.
✓ Branch 1 taken 7162 times.
14726 for (int i = 0; i < orig_nb_streams; i++)
1770 7564 av_dict_free(&opts[i]);
1771 7162 av_freep(&opts);
1772
1773
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7162 times.
7162 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 7143 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 21 times.
7164 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 7164 times.
7164 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 7143 times.
7164 timestamp = (start_time == AV_NOPTS_VALUE) ? 0 : start_time;
1800 /* add the stream start time */
1801
4/4
✓ Branch 0 taken 7161 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 5451 times.
✓ Branch 3 taken 1710 times.
7164 if (!o->seek_timestamp && ic->start_time != AV_NOPTS_VALUE)
1802 5451 timestamp += ic->start_time;
1803
1804 /* if seeking requested, we execute it */
1805
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 7143 times.
7164 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 7164 f->start_time = start_time;
1829 7164 d->recording_time = recording_time;
1830 7164 f->input_sync_ref = o->input_sync_ref;
1831 7164 f->input_ts_offset = o->input_ts_offset;
1832
3/6
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 7151 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 13 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
7164 f->ts_offset = o->input_ts_offset - (copy_ts ? (start_at_zero && ic->start_time != AV_NOPTS_VALUE ? ic->start_time : 0) : timestamp);
1833 7164 d->accurate_seek = o->accurate_seek;
1834 7164 d->loop = o->loop;
1835 7164 d->nb_streams_warn = ic->nb_streams;
1836
1837 7164 d->duration = (Timestamp){ .ts = 0, .tb = (AVRational){ 1, 1 } };
1838 7164 d->min_pts = (Timestamp){ .ts = AV_NOPTS_VALUE, .tb = (AVRational){ 1, 1 } };
1839 7164 d->max_pts = (Timestamp){ .ts = AV_NOPTS_VALUE, .tb = (AVRational){ 1, 1 } };
1840
1841 7164 d->readrate = o->readrate ? o->readrate : 0.0;
1842
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7164 times.
7164 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 7163 times.
7164 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 7163 times.
7164 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 7163 times.
7163 } 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 7696 times.
✓ Branch 1 taken 7164 times.
14860 for (int i = 0; i < ic->nb_streams; i++) {
1868 7696 ret = ist_add(o, d, ic->streams[i], &opts_used);
1869
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7696 times.
7696 if (ret < 0) {
1870 av_dict_free(&opts_used);
1871 return ret;
1872 }
1873 }
1874
1875 /* dump the file content */
1876 7164 av_dump_format(ic, f->index, filename, 0);
1877
1878 /* check if all codec options have been used */
1879 7164 ret = check_avoptions_used(o->g->codec_opts, opts_used, d, 1);
1880 7164 av_dict_free(&opts_used);
1881
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7164 times.
7164 if (ret < 0)
1882 return ret;
1883
1884
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7164 times.
7164 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 7164 return 0;
1897 }
1898