FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/fftools/ffmpeg_demux.c
Date: 2025-08-19 23:55:23
Exec Total Coverage
Lines: 842 1097 76.8%
Functions: 33 34 97.1%
Branches: 525 810 64.8%

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