FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/fftools/ffmpeg_demux.c
Date: 2025-04-25 22:50:00
Exec Total Coverage
Lines: 830 1090 76.1%
Functions: 33 34 97.1%
Branches: 516 804 64.2%

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 1887161 static DemuxStream *ds_from_ist(InputStream *ist)
156 {
157 1887161 return (DemuxStream*)ist;
158 }
159
160 21574 static Demuxer *demuxer_from_ifile(InputFile *f)
161 {
162 21574 return (Demuxer*)f;
163 }
164
165 55 InputStream *ist_find_unused(enum AVMediaType type)
166 {
167
1/2
✓ Branch 2 taken 74 times.
✗ Branch 3 not taken.
74 for (InputStream *ist = ist_iter(NULL); ist; ist = ist_iter(ist)) {
168 74 DemuxStream *ds = ds_from_ist(ist);
169
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 &&
170
1/2
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
55 ist->user_set_discard != AVDISCARD_ALL)
171 55 return ist;
172 }
173 return NULL;
174 }
175
176 9768 static void report_new_stream(Demuxer *d, const AVPacket *pkt)
177 {
178 9768 const AVStream *st = d->f.ctx->streams[pkt->stream_index];
179
180
1/2
✓ Branch 0 taken 9768 times.
✗ Branch 1 not taken.
9768 if (pkt->stream_index < d->nb_streams_warn)
181 9768 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 424940 static void ts_discontinuity_detect(Demuxer *d, InputStream *ist,
217 AVPacket *pkt)
218 {
219 424940 InputFile *ifile = &d->f;
220 424940 DemuxStream *ds = ds_from_ist(ist);
221 424940 const int fmt_is_discont = ifile->ctx->iformat->flags & AVFMT_TS_DISCONT;
222 424940 int disable_discontinuity_correction = copy_ts;
223 424940 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 424746 times.
✓ Branch 2 taken 188 times.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 73 times.
✓ Branch 5 taken 115 times.
424940 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 418117 times.
✓ Branch 1 taken 6823 times.
✓ Branch 2 taken 417929 times.
✓ Branch 3 taken 188 times.
842869 if (ds->next_dts != AV_NOPTS_VALUE && !disable_discontinuity_correction) {
236 417929 int64_t delta = pkt_dts - ds->next_dts;
237
2/2
✓ Branch 0 taken 22665 times.
✓ Branch 1 taken 395264 times.
417929 if (fmt_is_discont) {
238
4/4
✓ Branch 0 taken 22609 times.
✓ Branch 1 taken 56 times.
✓ Branch 2 taken 22582 times.
✓ Branch 3 taken 83 times.
22665 if (FFABS(delta) > 1LL * dts_delta_threshold * AV_TIME_BASE ||
239
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 22574 times.
22582 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 391457 times.
✓ Branch 1 taken 3807 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 395264 times.
395264 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 393149 times.
✓ Branch 1 taken 2115 times.
395264 if (pkt->pts != AV_NOPTS_VALUE){
257 393149 int64_t pkt_pts = av_rescale_q(pkt->pts, pkt->time_base, AV_TIME_BASE_Q);
258 393149 delta = pkt_pts - ds->next_dts;
259
3/4
✓ Branch 0 taken 389708 times.
✓ Branch 1 taken 3441 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 393149 times.
393149 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 6823 times.
✓ Branch 1 taken 188 times.
✓ Branch 2 taken 6817 times.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 116 times.
✓ Branch 5 taken 6701 times.
7011 } else if (ds->next_dts == AV_NOPTS_VALUE && !copy_ts &&
268
1/2
✓ Branch 0 taken 116 times.
✗ Branch 1 not taken.
116 fmt_is_discont && d->last_ts != AV_NOPTS_VALUE) {
269 116 int64_t delta = pkt_dts - d->last_ts;
270
3/4
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 60 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 116 times.
116 if (FFABS(delta) > 1LL * dts_delta_threshold * AV_TIME_BASE) {
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 424940 d->last_ts = av_rescale_q(pkt->dts, pkt->time_base, AV_TIME_BASE_Q);
282 424940 }
283
284 471219 static void ts_discontinuity_process(Demuxer *d, InputStream *ist,
285 AVPacket *pkt)
286 {
287 471219 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 427644 times.
✓ Branch 1 taken 43575 times.
471219 if (pkt->dts != AV_NOPTS_VALUE)
293 427644 pkt->dts += offset;
294
2/2
✓ Branch 0 taken 425490 times.
✓ Branch 1 taken 45729 times.
471219 if (pkt->pts != AV_NOPTS_VALUE)
295 425490 pkt->pts += offset;
296
297 // detect timestamp discontinuities for audio/video
298
2/2
✓ Branch 0 taken 282034 times.
✓ Branch 1 taken 189185 times.
471219 if ((ist->par->codec_type == AVMEDIA_TYPE_VIDEO ||
299
2/2
✓ Branch 0 taken 279326 times.
✓ Branch 1 taken 2708 times.
282034 ist->par->codec_type == AVMEDIA_TYPE_AUDIO) &&
300
2/2
✓ Branch 0 taken 424940 times.
✓ Branch 1 taken 43571 times.
468511 pkt->dts != AV_NOPTS_VALUE)
301 424940 ts_discontinuity_detect(d, ist, pkt);
302 471219 }
303
304 471219 static int ist_dts_update(DemuxStream *ds, AVPacket *pkt, FrameData *fd)
305 {
306 471219 InputStream *ist = &ds->ist;
307 471219 const AVCodecParameters *par = ist->par;
308
309
2/2
✓ Branch 0 taken 7439 times.
✓ Branch 1 taken 463780 times.
471219 if (!ds->saw_first_ts) {
310 7439 ds->first_dts =
311
2/2
✓ Branch 0 taken 5161 times.
✓ Branch 1 taken 2278 times.
7439 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 6844 times.
✓ Branch 1 taken 595 times.
7439 if (pkt->pts != AV_NOPTS_VALUE) {
313 6844 ds->first_dts =
314 6844 ds->dts += av_rescale_q(pkt->pts, pkt->time_base, AV_TIME_BASE_Q);
315 }
316 7439 ds->saw_first_ts = 1;
317 }
318
319
2/2
✓ Branch 0 taken 7439 times.
✓ Branch 1 taken 463780 times.
471219 if (ds->next_dts == AV_NOPTS_VALUE)
320 7439 ds->next_dts = ds->dts;
321
322
2/2
✓ Branch 0 taken 427644 times.
✓ Branch 1 taken 43575 times.
471219 if (pkt->dts != AV_NOPTS_VALUE)
323 427644 ds->next_dts = ds->dts = av_rescale_q(pkt->dts, pkt->time_base, AV_TIME_BASE_Q);
324
325 471219 ds->dts = ds->next_dts;
326
3/3
✓ Branch 0 taken 279326 times.
✓ Branch 1 taken 189185 times.
✓ Branch 2 taken 2708 times.
471219 switch (par->codec_type) {
327 279326 case AVMEDIA_TYPE_AUDIO:
328 av_assert1(pkt->duration >= 0);
329
1/2
✓ Branch 0 taken 279326 times.
✗ Branch 1 not taken.
279326 if (par->sample_rate) {
330 279326 ds->next_dts += ((int64_t)AV_TIME_BASE * par->frame_size) /
331 279326 par->sample_rate;
332 } else {
333 ds->next_dts += av_rescale_q(pkt->duration, pkt->time_base, AV_TIME_BASE_Q);
334 }
335 279326 break;
336 189185 case AVMEDIA_TYPE_VIDEO:
337
2/2
✓ Branch 0 taken 743 times.
✓ Branch 1 taken 188442 times.
189185 if (ist->framerate.num) {
338 // TODO: Remove work-around for c99-to-c89 issue 7
339 743 AVRational time_base_q = AV_TIME_BASE_Q;
340 743 int64_t next_dts = av_rescale_q(ds->next_dts, time_base_q, av_inv_q(ist->framerate));
341 743 ds->next_dts = av_rescale_q(next_dts + 1, av_inv_q(ist->framerate), time_base_q);
342
2/2
✓ Branch 0 taken 186154 times.
✓ Branch 1 taken 2288 times.
188442 } else if (pkt->duration) {
343 186154 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 2239 times.
2288 } 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 189185 break;
357 }
358
359 471219 fd->dts_est = ds->dts;
360
361 471219 return 0;
362 }
363
364 471219 static int ts_fixup(Demuxer *d, AVPacket *pkt, FrameData *fd)
365 {
366 471219 InputFile *ifile = &d->f;
367 471219 InputStream *ist = ifile->streams[pkt->stream_index];
368 471219 DemuxStream *ds = ds_from_ist(ist);
369 471219 const int64_t start_time = ifile->start_time_effective;
370 int64_t duration;
371 int ret;
372
373 471219 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 471219 times.
471219 SHOW_TS_DEBUG("demuxer");
387
388
4/4
✓ Branch 0 taken 422714 times.
✓ Branch 1 taken 48505 times.
✓ Branch 2 taken 326673 times.
✓ Branch 3 taken 96041 times.
471219 if (!ds->wrap_correction_done && start_time != AV_NOPTS_VALUE &&
389
2/2
✓ Branch 0 taken 376 times.
✓ Branch 1 taken 326297 times.
326673 ist->st->pts_wrap_bits < 64) {
390 int64_t stime, stime2;
391
392 376 stime = av_rescale_q(start_time, AV_TIME_BASE_Q, pkt->time_base);
393 376 stime2= stime + (1ULL<<ist->st->pts_wrap_bits);
394 376 ds->wrap_correction_done = 1;
395
396
5/6
✓ Branch 0 taken 346 times.
✓ Branch 1 taken 30 times.
✓ Branch 2 taken 312 times.
✓ Branch 3 taken 34 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 312 times.
376 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 346 times.
✓ Branch 1 taken 30 times.
✓ Branch 2 taken 306 times.
✓ Branch 3 taken 40 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 306 times.
376 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 427644 times.
✓ Branch 1 taken 43575 times.
471219 if (pkt->dts != AV_NOPTS_VALUE)
407 427644 pkt->dts += av_rescale_q(ifile->ts_offset, AV_TIME_BASE_Q, pkt->time_base);
408
2/2
✓ Branch 0 taken 425490 times.
✓ Branch 1 taken 45729 times.
471219 if (pkt->pts != AV_NOPTS_VALUE)
409 425490 pkt->pts += av_rescale_q(ifile->ts_offset, AV_TIME_BASE_Q, pkt->time_base);
410
411
2/2
✓ Branch 0 taken 425490 times.
✓ Branch 1 taken 45729 times.
471219 if (pkt->pts != AV_NOPTS_VALUE)
412 425490 pkt->pts *= ds->ts_scale;
413
2/2
✓ Branch 0 taken 427644 times.
✓ Branch 1 taken 43575 times.
471219 if (pkt->dts != AV_NOPTS_VALUE)
414 427644 pkt->dts *= ds->ts_scale;
415
416 471219 duration = av_rescale_q(d->duration.ts, d->duration.tb, pkt->time_base);
417
2/2
✓ Branch 0 taken 425490 times.
✓ Branch 1 taken 45729 times.
471219 if (pkt->pts != AV_NOPTS_VALUE) {
418 // audio decoders take precedence for estimating total file duration
419
2/2
✓ Branch 0 taken 195238 times.
✓ Branch 1 taken 230252 times.
425490 int64_t pkt_duration = d->have_audio_dec ? 0 : pkt->duration;
420
421 425490 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 418830 times.
✓ Branch 1 taken 6660 times.
✓ Branch 2 taken 405594 times.
✓ Branch 3 taken 13236 times.
844320 if (d->max_pts.ts == AV_NOPTS_VALUE ||
426 418830 av_compare_ts(d->max_pts.ts, d->max_pts.tb,
427 418830 pkt->pts + pkt_duration, pkt->time_base) < 0) {
428 412254 d->max_pts = (Timestamp){ .ts = pkt->pts + pkt_duration,
429 412254 .tb = pkt->time_base };
430 }
431
4/4
✓ Branch 0 taken 418830 times.
✓ Branch 1 taken 6660 times.
✓ Branch 2 taken 25 times.
✓ Branch 3 taken 418805 times.
844320 if (d->min_pts.ts == AV_NOPTS_VALUE ||
432 418830 av_compare_ts(d->min_pts.ts, d->min_pts.tb,
433 pkt->pts, pkt->time_base) > 0) {
434 6685 d->min_pts = (Timestamp){ .ts = pkt->pts,
435 6685 .tb = pkt->time_base };
436 }
437 }
438
439
2/2
✓ Branch 0 taken 427644 times.
✓ Branch 1 taken 43575 times.
471219 if (pkt->dts != AV_NOPTS_VALUE)
440 427644 pkt->dts += duration;
441
442
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 471219 times.
471219 SHOW_TS_DEBUG("demuxer+tsfixup");
443
444 // detect and try to correct for timestamp discontinuities
445 471219 ts_discontinuity_process(d, ist, pkt);
446
447 // update estimated/predicted dts
448 471219 ret = ist_dts_update(ds, pkt, fd);
449
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 471219 times.
471219 if (ret < 0)
450 return ret;
451
452 471219 return 0;
453 }
454
455 471219 static int input_packet_process(Demuxer *d, AVPacket *pkt, unsigned *send_flags)
456 {
457 471219 InputFile *f = &d->f;
458 471219 InputStream *ist = f->streams[pkt->stream_index];
459 471219 DemuxStream *ds = ds_from_ist(ist);
460 FrameData *fd;
461 471219 int ret = 0;
462
463 471219 fd = packet_data(pkt);
464
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 471219 times.
471219 if (!fd)
465 return AVERROR(ENOMEM);
466
467 471219 ret = ts_fixup(d, pkt, fd);
468
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 471219 times.
471219 if (ret < 0)
469 return ret;
470
471
2/2
✓ Branch 0 taken 228 times.
✓ Branch 1 taken 470991 times.
471219 if (d->recording_time != INT64_MAX) {
472 228 int64_t start_time = 0;
473
2/2
✓ Branch 0 taken 74 times.
✓ Branch 1 taken 154 times.
228 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 32 times.
✓ Branch 1 taken 196 times.
228 if (ds->dts >= d->recording_time + start_time)
478 32 *send_flags |= DEMUX_SEND_STREAMCOPY_EOF;
479 }
480
481 471219 ds->data_size += pkt->size;
482 471219 ds->nb_packets++;
483
484 471219 fd->wallclock[LATENCY_PROBE_DEMUX] = av_gettime_relative();
485
486
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 471219 times.
471219 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 471219 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 int resume_warn;
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 472167 static int do_send(Demuxer *d, DemuxStream *ds, AVPacket *pkt, unsigned flags,
546 const char *pkt_desc)
547 {
548 int ret;
549
550 472167 pkt->stream_index = ds->sch_idx_stream;
551
552 472167 ret = sch_demux_send(d->sch, d->f.index, pkt, flags);
553
2/2
✓ Branch 0 taken 3455 times.
✓ Branch 1 taken 468712 times.
472167 if (ret == AVERROR_EOF) {
554 3455 av_packet_unref(pkt);
555
556 3455 av_log(ds, AV_LOG_VERBOSE, "All consumers of this stream are done\n");
557 3455 ds->finished = 1;
558
559
2/2
✓ Branch 0 taken 3423 times.
✓ Branch 1 taken 32 times.
3455 if (++d->nb_streams_finished == d->nb_streams_used) {
560 3423 av_log(d, AV_LOG_VERBOSE, "All consumers are done\n");
561 3423 return AVERROR_EOF;
562 }
563
2/2
✓ Branch 0 taken 49 times.
✓ Branch 1 taken 468663 times.
468712 } else if (ret < 0) {
564
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49 times.
49 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 49 return ret;
569 }
570
571 468695 return 0;
572 }
573
574 471222 static int demux_send(Demuxer *d, DemuxThreadContext *dt, DemuxStream *ds,
575 AVPacket *pkt, unsigned flags)
576 {
577 471222 InputFile *f = &d->f;
578 int ret;
579
580 // pkt can be NULL only when flushing BSFs
581
3/4
✓ Branch 0 taken 471207 times.
✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 471207 times.
471222 av_assert0(ds->bsf || pkt);
582
583 // send heartbeat for sub2video streams
584
4/6
✓ Branch 0 taken 948 times.
✓ Branch 1 taken 470274 times.
✓ Branch 2 taken 948 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 948 times.
✗ Branch 5 not taken.
471222 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 15 times.
✓ Branch 1 taken 471207 times.
471222 if (ds->bsf) {
602
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 3 times.
15 if (pkt)
603 12 av_packet_rescale_ts(pkt, pkt->time_base, ds->bsf->time_base_in);
604
605 15 ret = av_bsf_send_packet(ds->bsf, pkt);
606
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
15 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 27 ret = av_bsf_receive_packet(ds->bsf, dt->pkt_bsf);
616
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 15 times.
27 if (ret == AVERROR(EAGAIN))
617 12 return 0;
618
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 12 times.
15 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 12 dt->pkt_bsf->time_base = ds->bsf->time_base_out;
627
628 12 ret = do_send(d, ds, dt->pkt_bsf, 0, "filtered");
629
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (ret < 0) {
630 av_packet_unref(dt->pkt_bsf);
631 return ret;
632 }
633 }
634 } else {
635 471207 ret = do_send(d, ds, pkt, flags, "demuxed");
636
2/2
✓ Branch 0 taken 3472 times.
✓ Branch 1 taken 467735 times.
471207 if (ret < 0)
637 3472 return ret;
638 }
639
640 467735 return 0;
641 }
642
643 3732 static int demux_bsf_flush(Demuxer *d, DemuxThreadContext *dt)
644 {
645 3732 InputFile *f = &d->f;
646 int ret;
647
648
2/2
✓ Branch 0 taken 4233 times.
✓ Branch 1 taken 3732 times.
7965 for (unsigned i = 0; i < f->nb_streams; i++) {
649 4233 DemuxStream *ds = ds_from_ist(f->streams[i]);
650
651
2/2
✓ Branch 0 taken 4230 times.
✓ Branch 1 taken 3 times.
4233 if (!ds->bsf)
652 4230 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 3732 return 0;
666 }
667
668 7193 static void discard_unused_programs(InputFile *ifile)
669 {
670
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 7193 times.
7235 for (int j = 0; j < ifile->ctx->nb_programs; j++) {
671 42 AVProgram *p = ifile->ctx->programs[j];
672 42 int discard = AVDISCARD_ALL;
673
674
1/2
✓ Branch 0 taken 57 times.
✗ Branch 1 not taken.
57 for (int k = 0; k < p->nb_stream_indexes; k++) {
675 57 DemuxStream *ds = ds_from_ist(ifile->streams[p->stream_index[k]]);
676
677
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 15 times.
57 if (!ds->discard) {
678 42 discard = AVDISCARD_DEFAULT;
679 42 break;
680 }
681 }
682 42 p->discard = discard;
683 }
684 7193 }
685
686 7193 static void thread_set_name(InputFile *f)
687 {
688 char name[16];
689 7193 snprintf(name, sizeof(name), "dmx%d:%s", f->index, f->ctx->iformat->name);
690 7193 ff_thread_setname(name);
691 7193 }
692
693 7193 static void demux_thread_uninit(DemuxThreadContext *dt)
694 {
695 7193 av_packet_free(&dt->pkt_demux);
696 7193 av_packet_free(&dt->pkt_bsf);
697
698 7193 memset(dt, 0, sizeof(*dt));
699 7193 }
700
701 7193 static int demux_thread_init(DemuxThreadContext *dt)
702 {
703 7193 memset(dt, 0, sizeof(*dt));
704
705 7193 dt->pkt_demux = av_packet_alloc();
706
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7193 times.
7193 if (!dt->pkt_demux)
707 return AVERROR(ENOMEM);
708
709 7193 dt->pkt_bsf = av_packet_alloc();
710
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7193 times.
7193 if (!dt->pkt_bsf)
711 return AVERROR(ENOMEM);
712
713 7193 return 0;
714 }
715
716 7193 static int input_thread(void *arg)
717 {
718 7193 Demuxer *d = arg;
719 7193 InputFile *f = &d->f;
720
721 DemuxThreadContext dt;
722
723 7193 int ret = 0;
724
725 7193 ret = demux_thread_init(&dt);
726
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7193 times.
7193 if (ret < 0)
727 goto finish;
728
729 7193 thread_set_name(f);
730
731 7193 discard_unused_programs(f);
732
733 7193 d->read_started = 1;
734 7193 d->wallclock_start = av_gettime_relative();
735
736 477527 while (1) {
737 DemuxStream *ds;
738 484720 unsigned send_flags = 0;
739
740 484720 ret = av_read_frame(f->ctx, dt.pkt_demux);
741
742
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 484719 times.
484720 if (ret == AVERROR(EAGAIN)) {
743 1 av_usleep(10000);
744 9780 continue;
745 }
746
2/2
✓ Branch 0 taken 3732 times.
✓ Branch 1 taken 480987 times.
484719 if (ret < 0) {
747 int ret_bsf;
748
749
2/2
✓ Branch 0 taken 3674 times.
✓ Branch 1 taken 58 times.
3732 if (ret == AVERROR_EOF)
750 3674 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 3732 ret_bsf = demux_bsf_flush(d, &dt);
758
2/2
✓ Branch 0 taken 58 times.
✓ Branch 1 taken 3674 times.
3732 ret = err_merge(ret == AVERROR_EOF ? 0 : ret, ret_bsf);
759
760
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 3721 times.
3732 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 3721 break;
774 }
775
776
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 480987 times.
480987 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 961974 ds = dt.pkt_demux->stream_index < f->nb_streams ?
784
1/2
✓ Branch 0 taken 480987 times.
✗ Branch 1 not taken.
480987 ds_from_ist(f->streams[dt.pkt_demux->stream_index]) : NULL;
785
5/6
✓ Branch 0 taken 480987 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 472866 times.
✓ Branch 3 taken 8121 times.
✓ Branch 4 taken 1647 times.
✓ Branch 5 taken 471219 times.
480987 if (!ds || ds->discard || ds->finished) {
786 9768 report_new_stream(d, dt.pkt_demux);
787 9768 av_packet_unref(dt.pkt_demux);
788 9768 continue;
789 }
790
791
2/2
✓ Branch 0 taken 99 times.
✓ Branch 1 taken 471120 times.
471219 if (dt.pkt_demux->flags & AV_PKT_FLAG_CORRUPT) {
792 99 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 99 times.
99 dt.pkt_demux->stream_index);
795
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 99 times.
99 if (exit_on_error) {
796 av_packet_unref(dt.pkt_demux);
797 ret = AVERROR_INVALIDDATA;
798 break;
799 }
800 }
801
802 471219 ret = input_packet_process(d, dt.pkt_demux, &send_flags);
803
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 471219 times.
471219 if (ret < 0)
804 break;
805
806
2/2
✓ Branch 0 taken 216 times.
✓ Branch 1 taken 471003 times.
471219 if (d->readrate)
807 216 readrate_sleep(d);
808
809 471219 ret = demux_send(d, &dt, ds, dt.pkt_demux, send_flags);
810
2/2
✓ Branch 0 taken 3472 times.
✓ Branch 1 taken 467747 times.
471219 if (ret < 0)
811 3472 break;
812 }
813
814 // EOF/EXIT is normal termination
815
4/4
✓ Branch 0 taken 3770 times.
✓ Branch 1 taken 3423 times.
✓ Branch 2 taken 3721 times.
✓ Branch 3 taken 49 times.
7193 if (ret == AVERROR_EOF || ret == AVERROR_EXIT)
816 3472 ret = 0;
817
818 3721 finish:
819 7193 demux_thread_uninit(&dt);
820
821 7193 return ret;
822 }
823
824 7193 static void demux_final_stats(Demuxer *d)
825 {
826 7193 InputFile *f = &d->f;
827 7193 uint64_t total_packets = 0, total_size = 0;
828
829 7193 av_log(f, AV_LOG_VERBOSE, "Input file #%d (%s):\n",
830 7193 f->index, f->ctx->url);
831
832
2/2
✓ Branch 0 taken 7751 times.
✓ Branch 1 taken 7193 times.
14944 for (int j = 0; j < f->nb_streams; j++) {
833 7751 InputStream *ist = f->streams[j];
834 7751 DemuxStream *ds = ds_from_ist(ist);
835 7751 enum AVMediaType type = ist->par->codec_type;
836
837
3/4
✓ Branch 0 taken 7460 times.
✓ Branch 1 taken 291 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 7460 times.
7751 if (ds->discard || type == AVMEDIA_TYPE_ATTACHMENT)
838 291 continue;
839
840 7460 total_size += ds->data_size;
841 7460 total_packets += ds->nb_packets;
842
843 7460 av_log(f, AV_LOG_VERBOSE, " Input stream #%d:%d (%s): ",
844 f->index, j, av_get_media_type_string(type));
845 7460 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 6788 times.
✓ Branch 1 taken 672 times.
7460 if (ds->decoding_needed) {
849 6788 av_log(f, AV_LOG_VERBOSE,
850 "%"PRIu64" frames decoded; %"PRIu64" decode errors",
851 6788 ist->decoder->frames_decoded, ist->decoder->decode_errors);
852
2/2
✓ Branch 0 taken 1206 times.
✓ Branch 1 taken 5582 times.
6788 if (type == AVMEDIA_TYPE_AUDIO)
853 1206 av_log(f, AV_LOG_VERBOSE, " (%"PRIu64" samples)", ist->decoder->samples_decoded);
854 6788 av_log(f, AV_LOG_VERBOSE, "; ");
855 }
856
857 7460 av_log(f, AV_LOG_VERBOSE, "\n");
858 }
859
860 7193 av_log(f, AV_LOG_VERBOSE, " Total: %"PRIu64" packets (%"PRIu64" bytes) demuxed\n",
861 total_packets, total_size);
862 7193 }
863
864 7766 static void ist_free(InputStream **pist)
865 {
866 7766 InputStream *ist = *pist;
867 DemuxStream *ds;
868
869
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7766 times.
7766 if (!ist)
870 return;
871 7766 ds = ds_from_ist(ist);
872
873 7766 dec_free(&ist->decoder);
874
875 7766 av_dict_free(&ds->decoder_opts);
876 7766 av_freep(&ist->filters);
877 7766 av_freep(&ds->dec_opts.hwaccel_device);
878
879 7766 avcodec_parameters_free(&ist->par);
880
881 7766 av_frame_free(&ds->decoded_params);
882
883 7766 av_bsf_free(&ds->bsf);
884
885 7766 av_freep(pist);
886 }
887
888 7207 void ifile_close(InputFile **pf)
889 {
890 7207 InputFile *f = *pf;
891 7207 Demuxer *d = demuxer_from_ifile(f);
892
893
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7207 times.
7207 if (!f)
894 return;
895
896
2/2
✓ Branch 0 taken 7193 times.
✓ Branch 1 taken 14 times.
7207 if (d->read_started)
897 7193 demux_final_stats(d);
898
899
2/2
✓ Branch 0 taken 7766 times.
✓ Branch 1 taken 7207 times.
14973 for (int i = 0; i < f->nb_streams; i++)
900 7766 ist_free(&f->streams[i]);
901 7207 av_freep(&f->streams);
902
903 7207 avformat_close_input(&f->ctx);
904
905 7207 av_packet_free(&d->pkt_heartbeat);
906
907 7207 av_freep(pf);
908 }
909
910 7550 int ist_use(InputStream *ist, int decoding_needed,
911 const ViewSpecifier *vs, SchedulerNode *src)
912 {
913 7550 Demuxer *d = demuxer_from_ifile(ist->file);
914 7550 DemuxStream *ds = ds_from_ist(ist);
915 int ret;
916
917
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7550 times.
7550 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 6855 times.
✓ Branch 1 taken 695 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6855 times.
7550 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 7460 times.
✓ Branch 1 taken 90 times.
7550 if (ds->sch_idx_stream < 0) {
931 7460 ret = sch_add_demux_stream(d->sch, d->f.index);
932
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7460 times.
7460 if (ret < 0)
933 return ret;
934 7460 ds->sch_idx_stream = ret;
935 }
936
937
2/2
✓ Branch 0 taken 7460 times.
✓ Branch 1 taken 90 times.
7550 if (ds->discard) {
938 7460 ds->discard = 0;
939 7460 d->nb_streams_used++;
940 }
941
942 7550 ist->st->discard = ist->user_set_discard;
943 7550 ds->decoding_needed |= decoding_needed;
944 7550 ds->streamcopy_needed |= !decoding_needed;
945
946
4/4
✓ Branch 0 taken 6855 times.
✓ Branch 1 taken 695 times.
✓ Branch 2 taken 6788 times.
✓ Branch 3 taken 67 times.
7550 if (decoding_needed && ds->sch_idx_dec < 0) {
947 6788 int is_audio = ist->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO;
948
949 13576 ds->dec_opts.flags |= (!!ist->fix_sub_duration * DECODER_FLAG_FIX_SUB_DURATION) |
950 13576 (!!(d->f.ctx->iformat->flags & AVFMT_NOTIMESTAMPS) * DECODER_FLAG_TS_UNRELIABLE) |
951
4/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6786 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
6788 (!!(d->loop && is_audio) * DECODER_FLAG_SEND_END_TS)
952 #if FFMPEG_OPT_TOP
953
2/2
✓ Branch 0 taken 1246 times.
✓ Branch 1 taken 5542 times.
6788 | ((ist->top_field_first >= 0) * DECODER_FLAG_TOP_FIELD_FIRST)
954 #endif
955 ;
956
957
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 6756 times.
6788 if (ist->framerate.num) {
958 32 ds->dec_opts.flags |= DECODER_FLAG_FRAMERATE_FORCED;
959 32 ds->dec_opts.framerate = ist->framerate;
960 } else
961 6756 ds->dec_opts.framerate = ist->st->avg_frame_rate;
962
963
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6787 times.
6788 if (ist->dec->id == AV_CODEC_ID_DVB_SUBTITLE &&
964
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 (ds->decoding_needed & DECODING_FOR_OST)) {
965 1 av_dict_set(&ds->decoder_opts, "compute_edt", "1", AV_DICT_DONT_OVERWRITE);
966
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ds->decoding_needed & DECODING_FOR_FILTER)
967 av_log(ist, AV_LOG_WARNING,
968 "Warning using DVB subtitles for filtering and output at the "
969 "same time is not fully supported, also see -compute_edt [0|1]\n");
970 }
971
972 6788 snprintf(ds->dec_name, sizeof(ds->dec_name), "%d:%d", ist->file->index, ist->index);
973 6788 ds->dec_opts.name = ds->dec_name;
974
975 6788 ds->dec_opts.codec = ist->dec;
976 6788 ds->dec_opts.par = ist->par;
977
978 6788 ds->dec_opts.log_parent = ist;
979
980 6788 ds->decoded_params = av_frame_alloc();
981
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6788 times.
6788 if (!ds->decoded_params)
982 return AVERROR(ENOMEM);
983
984 6788 ret = dec_init(&ist->decoder, d->sch,
985 6788 &ds->decoder_opts, &ds->dec_opts, ds->decoded_params);
986
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6788 times.
6788 if (ret < 0)
987 return ret;
988 6788 ds->sch_idx_dec = ret;
989
990 6788 ret = sch_connect(d->sch, SCH_DSTREAM(d->f.index, ds->sch_idx_stream),
991 6788 SCH_DEC_IN(ds->sch_idx_dec));
992
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6788 times.
6788 if (ret < 0)
993 return ret;
994
995 6788 d->have_audio_dec |= is_audio;
996 }
997
998
4/4
✓ Branch 0 taken 6855 times.
✓ Branch 1 taken 695 times.
✓ Branch 2 taken 5552 times.
✓ Branch 3 taken 1303 times.
7550 if (decoding_needed && ist->par->codec_type == AVMEDIA_TYPE_VIDEO) {
999 5552 ret = dec_request_view(ist->decoder, vs, src);
1000
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5552 times.
5552 if (ret < 0)
1001 return ret;
1002 } else {
1003 1998 *src = decoding_needed ?
1004
2/2
✓ Branch 0 taken 1303 times.
✓ Branch 1 taken 695 times.
1998 SCH_DEC_OUT(ds->sch_idx_dec, 0) :
1005 695 SCH_DSTREAM(d->f.index, ds->sch_idx_stream);
1006 }
1007
1008 7550 return 0;
1009 }
1010
1011 6817 int ist_filter_add(InputStream *ist, InputFilter *ifilter, int is_simple,
1012 const ViewSpecifier *vs, InputFilterOptions *opts,
1013 SchedulerNode *src)
1014 {
1015 6817 Demuxer *d = demuxer_from_ifile(ist->file);
1016 6817 DemuxStream *ds = ds_from_ist(ist);
1017 6817 int64_t tsoffset = 0;
1018 int ret;
1019
1020
2/2
✓ Branch 0 taken 6668 times.
✓ Branch 1 taken 149 times.
6817 ret = ist_use(ist, is_simple ? DECODING_FOR_OST : DECODING_FOR_FILTER,
1021 vs, src);
1022
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6817 times.
6817 if (ret < 0)
1023 return ret;
1024
1025 6817 ret = GROW_ARRAY(ist->filters, ist->nb_filters);
1026
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6817 times.
6817 if (ret < 0)
1027 return ret;
1028
1029 6817 ist->filters[ist->nb_filters - 1] = ifilter;
1030
1031
2/2
✓ Branch 0 taken 5552 times.
✓ Branch 1 taken 1265 times.
6817 if (ist->par->codec_type == AVMEDIA_TYPE_VIDEO) {
1032 5552 const AVPacketSideData *sd = av_packet_side_data_get(ist->par->coded_side_data,
1033 5552 ist->par->nb_coded_side_data,
1034 AV_PKT_DATA_FRAME_CROPPING);
1035
3/4
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 5520 times.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
5552 if (ist->framerate.num > 0 && ist->framerate.den > 0) {
1036 32 opts->framerate = ist->framerate;
1037 32 opts->flags |= IFILTER_FLAG_CFR;
1038 } else
1039 5520 opts->framerate = av_guess_frame_rate(d->f.ctx, ist->st, NULL);
1040
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 5548 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
5552 if (sd && sd->size >= sizeof(uint32_t) * 4) {
1041 4 opts->crop_top = AV_RL32(sd->data + 0);
1042 4 opts->crop_bottom = AV_RL32(sd->data + 4);
1043 4 opts->crop_left = AV_RL32(sd->data + 8);
1044 4 opts->crop_right = AV_RL32(sd->data + 12);
1045
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 &&
1046
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 (opts->crop_top | opts->crop_bottom | opts->crop_left | opts->crop_right))
1047 4 opts->flags |= IFILTER_FLAG_CROP;
1048 }
1049
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1261 times.
1265 } else if (ist->par->codec_type == AVMEDIA_TYPE_SUBTITLE) {
1050 /* Compute the size of the canvas for the subtitles stream.
1051 If the subtitles codecpar has set a size, use it. Otherwise use the
1052 maximum dimensions of the video streams in the same file. */
1053 4 opts->sub2video_width = ist->par->width;
1054 4 opts->sub2video_height = ist->par->height;
1055
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)) {
1056 for (int j = 0; j < d->f.nb_streams; j++) {
1057 AVCodecParameters *par1 = d->f.streams[j]->par;
1058 if (par1->codec_type == AVMEDIA_TYPE_VIDEO) {
1059 opts->sub2video_width = FFMAX(opts->sub2video_width, par1->width);
1060 opts->sub2video_height = FFMAX(opts->sub2video_height, par1->height);
1061 }
1062 }
1063 }
1064
1065
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)) {
1066 opts->sub2video_width = FFMAX(opts->sub2video_width, 720);
1067 opts->sub2video_height = FFMAX(opts->sub2video_height, 576);
1068 }
1069
1070
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (!d->pkt_heartbeat) {
1071 4 d->pkt_heartbeat = av_packet_alloc();
1072
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!d->pkt_heartbeat)
1073 return AVERROR(ENOMEM);
1074 }
1075 4 ds->have_sub2video = 1;
1076 }
1077
1078 6817 ret = av_frame_copy_props(opts->fallback, ds->decoded_params);
1079
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6817 times.
6817 if (ret < 0)
1080 return ret;
1081 6817 opts->fallback->format = ds->decoded_params->format;
1082 6817 opts->fallback->width = ds->decoded_params->width;
1083 6817 opts->fallback->height = ds->decoded_params->height;
1084
1085 6817 ret = av_channel_layout_copy(&opts->fallback->ch_layout, &ds->decoded_params->ch_layout);
1086
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6817 times.
6817 if (ret < 0)
1087 return ret;
1088
1089
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 6814 times.
6817 if (copy_ts) {
1090
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;
1091
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)
1092 2 tsoffset += d->f.ctx->start_time;
1093 }
1094
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) ?
1095
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 6801 times.
6833 AV_NOPTS_VALUE : tsoffset;
1096 6817 opts->trim_end_us = d->recording_time;
1097
1098 6817 opts->name = av_strdup(ds->dec_name);
1099
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6817 times.
6817 if (!opts->name)
1100 return AVERROR(ENOMEM);
1101
1102 20451 opts->flags |= IFILTER_FLAG_AUTOROTATE * !!(ds->autorotate) |
1103
1/2
✓ Branch 0 taken 6817 times.
✗ Branch 1 not taken.
6817 IFILTER_FLAG_REINIT * !!(ds->reinit_filters) |
1104
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6817 times.
6817 IFILTER_FLAG_DROPCHANGED* !!(ds->drop_changed);
1105
1106 6817 return 0;
1107 }
1108
1109 15381 static int choose_decoder(const OptionsContext *o, void *logctx,
1110 AVFormatContext *s, AVStream *st,
1111 enum HWAccelID hwaccel_id, enum AVHWDeviceType hwaccel_device_type,
1112 const AVCodec **pcodec)
1113
1114 {
1115 15381 const char *codec_name = NULL;
1116
1117 15381 opt_match_per_stream_str(logctx, &o->codec_names, s, st, &codec_name);
1118
2/2
✓ Branch 0 taken 6174 times.
✓ Branch 1 taken 9207 times.
15381 if (codec_name) {
1119 6174 int ret = find_codec(NULL, codec_name, st->codecpar->codec_type, 0, pcodec);
1120
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6174 times.
6174 if (ret < 0)
1121 return ret;
1122 6174 st->codecpar->codec_id = (*pcodec)->id;
1123
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 6174 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
6174 if (recast_media && st->codecpar->codec_type != (*pcodec)->type)
1124 st->codecpar->codec_type = (*pcodec)->type;
1125 6174 return 0;
1126 } else {
1127
3/4
✓ Branch 0 taken 5722 times.
✓ Branch 1 taken 3485 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 5722 times.
9207 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
1128 hwaccel_id == HWACCEL_GENERIC &&
1129 hwaccel_device_type != AV_HWDEVICE_TYPE_NONE) {
1130 const AVCodec *c;
1131 void *i = NULL;
1132
1133 while ((c = av_codec_iterate(&i))) {
1134 const AVCodecHWConfig *config;
1135
1136 if (c->id != st->codecpar->codec_id ||
1137 !av_codec_is_decoder(c))
1138 continue;
1139
1140 for (int j = 0; config = avcodec_get_hw_config(c, j); j++) {
1141 if (config->device_type == hwaccel_device_type) {
1142 av_log(logctx, AV_LOG_VERBOSE, "Selecting decoder '%s' because of requested hwaccel method %s\n",
1143 c->name, av_hwdevice_get_type_name(hwaccel_device_type));
1144 *pcodec = c;
1145 return 0;
1146 }
1147 }
1148 }
1149 }
1150
1151 9207 *pcodec = avcodec_find_decoder(st->codecpar->codec_id);
1152 9207 return 0;
1153 }
1154 }
1155
1156 1674 static int guess_input_channel_layout(InputStream *ist, AVCodecParameters *par,
1157 int guess_layout_max)
1158 {
1159
2/2
✓ Branch 0 taken 773 times.
✓ Branch 1 taken 901 times.
1674 if (par->ch_layout.order == AV_CHANNEL_ORDER_UNSPEC) {
1160 char layout_name[256];
1161
1162
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 769 times.
773 if (par->ch_layout.nb_channels > guess_layout_max)
1163 14 return 0;
1164 769 av_channel_layout_default(&par->ch_layout, par->ch_layout.nb_channels);
1165
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 759 times.
769 if (par->ch_layout.order == AV_CHANNEL_ORDER_UNSPEC)
1166 10 return 0;
1167 759 av_channel_layout_describe(&par->ch_layout, layout_name, sizeof(layout_name));
1168 759 av_log(ist, AV_LOG_WARNING, "Guessed Channel Layout: %s\n", layout_name);
1169 }
1170 1660 return 1;
1171 }
1172
1173 5945 static int add_display_matrix_to_stream(const OptionsContext *o,
1174 AVFormatContext *ctx, InputStream *ist)
1175 {
1176 5945 AVStream *st = ist->st;
1177 AVPacketSideData *sd;
1178 5945 double rotation = DBL_MAX;
1179 5945 int hflip = -1, vflip = -1;
1180 5945 int hflip_set = 0, vflip_set = 0, rotation_set = 0;
1181 int32_t *buf;
1182
1183 5945 opt_match_per_stream_dbl(ist, &o->display_rotations, ctx, st, &rotation);
1184 5945 opt_match_per_stream_int(ist, &o->display_hflips, ctx, st, &hflip);
1185 5945 opt_match_per_stream_int(ist, &o->display_vflips, ctx, st, &vflip);
1186
1187 5945 rotation_set = rotation != DBL_MAX;
1188 5945 hflip_set = hflip != -1;
1189 5945 vflip_set = vflip != -1;
1190
1191
4/6
✓ Branch 0 taken 5944 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 5944 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 5944 times.
✗ Branch 5 not taken.
5945 if (!rotation_set && !hflip_set && !vflip_set)
1192 5944 return 0;
1193
1194 1 sd = av_packet_side_data_new(&st->codecpar->coded_side_data,
1195 1 &st->codecpar->nb_coded_side_data,
1196 AV_PKT_DATA_DISPLAYMATRIX,
1197 sizeof(int32_t) * 9, 0);
1198
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!sd) {
1199 av_log(ist, AV_LOG_FATAL, "Failed to generate a display matrix!\n");
1200 return AVERROR(ENOMEM);
1201 }
1202
1203 1 buf = (int32_t *)sd->data;
1204
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
2 av_display_rotation_set(buf,
1205 1 rotation_set ? -(rotation) : -0.0f);
1206
1207
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,
1208 hflip_set ? hflip : 0,
1209 vflip_set ? vflip : 0);
1210
1211 1 return 0;
1212 }
1213
1214 1436 static const char *input_stream_item_name(void *obj)
1215 {
1216 1436 const DemuxStream *ds = obj;
1217
1218 1436 return ds->log_name;
1219 }
1220
1221 static const AVClass input_stream_class = {
1222 .class_name = "InputStream",
1223 .version = LIBAVUTIL_VERSION_INT,
1224 .item_name = input_stream_item_name,
1225 .category = AV_CLASS_CATEGORY_DEMUXER,
1226 };
1227
1228 7766 static DemuxStream *demux_stream_alloc(Demuxer *d, AVStream *st)
1229 {
1230 7766 const char *type_str = av_get_media_type_string(st->codecpar->codec_type);
1231 7766 InputFile *f = &d->f;
1232 DemuxStream *ds;
1233
1234 7766 ds = allocate_array_elem(&f->streams, sizeof(*ds), &f->nb_streams);
1235
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7766 times.
7766 if (!ds)
1236 return NULL;
1237
1238 7766 ds->sch_idx_stream = -1;
1239 7766 ds->sch_idx_dec = -1;
1240
1241 7766 ds->ist.st = st;
1242 7766 ds->ist.file = f;
1243 7766 ds->ist.index = st->index;
1244 7766 ds->ist.class = &input_stream_class;
1245
1246
1/2
✓ Branch 0 taken 7766 times.
✗ Branch 1 not taken.
7766 snprintf(ds->log_name, sizeof(ds->log_name), "%cist#%d:%d/%s",
1247 7766 type_str ? *type_str : '?', d->f.index, st->index,
1248 7766 avcodec_get_name(st->codecpar->codec_id));
1249
1250 7766 return ds;
1251 }
1252
1253 7766 static int ist_add(const OptionsContext *o, Demuxer *d, AVStream *st, AVDictionary **opts_used)
1254 {
1255 7766 AVFormatContext *ic = d->f.ctx;
1256 7766 AVCodecParameters *par = st->codecpar;
1257 DemuxStream *ds;
1258 InputStream *ist;
1259 7766 const char *framerate = NULL, *hwaccel_device = NULL;
1260 7766 const char *hwaccel = NULL;
1261 7766 const char *apply_cropping = NULL;
1262 7766 const char *hwaccel_output_format = NULL;
1263 7766 const char *codec_tag = NULL;
1264 7766 const char *bsfs = NULL;
1265 char *next;
1266 7766 const char *discard_str = NULL;
1267 int ret;
1268
1269 7766 ds = demux_stream_alloc(d, st);
1270
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7766 times.
7766 if (!ds)
1271 return AVERROR(ENOMEM);
1272
1273 7766 ist = &ds->ist;
1274
1275 7766 ds->discard = 1;
1276 7766 st->discard = AVDISCARD_ALL;
1277 7766 ds->first_dts = AV_NOPTS_VALUE;
1278 7766 ds->next_dts = AV_NOPTS_VALUE;
1279
1280 7766 ds->dec_opts.time_base = st->time_base;
1281
1282 7766 ds->ts_scale = 1.0;
1283 7766 opt_match_per_stream_dbl(ist, &o->ts_scale, ic, st, &ds->ts_scale);
1284
1285 7766 ds->autorotate = 1;
1286 7766 opt_match_per_stream_int(ist, &o->autorotate, ic, st, &ds->autorotate);
1287
1288 7766 ds->apply_cropping = CROP_ALL;
1289 7766 opt_match_per_stream_str(ist, &o->apply_cropping, ic, st, &apply_cropping);
1290
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7766 times.
7766 if (apply_cropping) {
1291 const AVOption opts[] = {
1292 { "apply_cropping", NULL, 0, AV_OPT_TYPE_INT,
1293 { .i64 = CROP_ALL }, CROP_DISABLED, CROP_CONTAINER, AV_OPT_FLAG_DECODING_PARAM, .unit = "apply_cropping" },
1294 { "none", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CROP_DISABLED }, .unit = "apply_cropping" },
1295 { "all", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CROP_ALL }, .unit = "apply_cropping" },
1296 { "codec", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CROP_CODEC }, .unit = "apply_cropping" },
1297 { "container", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CROP_CONTAINER }, .unit = "apply_cropping" },
1298 { NULL },
1299 };
1300 const AVClass class = {
1301 .class_name = "apply_cropping",
1302 .item_name = av_default_item_name,
1303 .option = opts,
1304 .version = LIBAVUTIL_VERSION_INT,
1305 };
1306 const AVClass *pclass = &class;
1307
1308 ret = av_opt_eval_int(&pclass, opts, apply_cropping, &ds->apply_cropping);
1309 if (ret < 0) {
1310 av_log(ist, AV_LOG_ERROR, "Invalid apply_cropping value '%s'.\n", apply_cropping);
1311 return ret;
1312 }
1313 }
1314
1315 7766 opt_match_per_stream_str(ist, &o->codec_tags, ic, st, &codec_tag);
1316
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 7764 times.
7766 if (codec_tag) {
1317 2 uint32_t tag = strtol(codec_tag, &next, 0);
1318
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (*next) {
1319 2 uint8_t buf[4] = { 0 };
1320
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 memcpy(buf, codec_tag, FFMIN(sizeof(buf), strlen(codec_tag)));
1321 2 tag = AV_RL32(buf);
1322 }
1323
1324 2 st->codecpar->codec_tag = tag;
1325 }
1326
1327
2/2
✓ Branch 0 taken 5945 times.
✓ Branch 1 taken 1821 times.
7766 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
1328 5945 ret = add_display_matrix_to_stream(o, ic, ist);
1329
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5945 times.
5945 if (ret < 0)
1330 return ret;
1331
1332 5945 opt_match_per_stream_str(ist, &o->hwaccels, ic, st, &hwaccel);
1333 5945 opt_match_per_stream_str(ist, &o->hwaccel_output_formats, ic, st,
1334 &hwaccel_output_format);
1335
4/6
✓ Branch 0 taken 5945 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5633 times.
✓ Branch 3 taken 312 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 5633 times.
5945 if (!hwaccel_output_format && hwaccel && !strcmp(hwaccel, "cuvid")) {
1336 av_log(ist, AV_LOG_WARNING,
1337 "WARNING: defaulting hwaccel_output_format to cuda for compatibility "
1338 "with old commandlines. This behaviour is DEPRECATED and will be removed "
1339 "in the future. Please explicitly set \"-hwaccel_output_format cuda\".\n");
1340 ds->dec_opts.hwaccel_output_format = AV_PIX_FMT_CUDA;
1341
4/6
✓ Branch 0 taken 5945 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5633 times.
✓ Branch 3 taken 312 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 5633 times.
5945 } else if (!hwaccel_output_format && hwaccel && !strcmp(hwaccel, "qsv")) {
1342 av_log(ist, AV_LOG_WARNING,
1343 "WARNING: defaulting hwaccel_output_format to qsv for compatibility "
1344 "with old commandlines. This behaviour is DEPRECATED and will be removed "
1345 "in the future. Please explicitly set \"-hwaccel_output_format qsv\".\n");
1346 ds->dec_opts.hwaccel_output_format = AV_PIX_FMT_QSV;
1347
4/6
✓ Branch 0 taken 5945 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5633 times.
✓ Branch 3 taken 312 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 5633 times.
5945 } else if (!hwaccel_output_format && hwaccel && !strcmp(hwaccel, "mediacodec")) {
1348 // There is no real AVHWFrameContext implementation. Set
1349 // hwaccel_output_format to avoid av_hwframe_transfer_data error.
1350 ds->dec_opts.hwaccel_output_format = AV_PIX_FMT_MEDIACODEC;
1351
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5945 times.
5945 } else if (hwaccel_output_format) {
1352 ds->dec_opts.hwaccel_output_format = av_get_pix_fmt(hwaccel_output_format);
1353 if (ds->dec_opts.hwaccel_output_format == AV_PIX_FMT_NONE) {
1354 av_log(ist, AV_LOG_FATAL, "Unrecognised hwaccel output "
1355 "format: %s", hwaccel_output_format);
1356 }
1357 } else {
1358 5945 ds->dec_opts.hwaccel_output_format = AV_PIX_FMT_NONE;
1359 }
1360
1361
2/2
✓ Branch 0 taken 5633 times.
✓ Branch 1 taken 312 times.
5945 if (hwaccel) {
1362 // The NVDEC hwaccels use a CUDA device, so remap the name here.
1363
2/4
✓ Branch 0 taken 5633 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5633 times.
5633 if (!strcmp(hwaccel, "nvdec") || !strcmp(hwaccel, "cuvid"))
1364 hwaccel = "cuda";
1365
1366
1/2
✓ Branch 0 taken 5633 times.
✗ Branch 1 not taken.
5633 if (!strcmp(hwaccel, "none"))
1367 5633 ds->dec_opts.hwaccel_id = HWACCEL_NONE;
1368 else if (!strcmp(hwaccel, "auto"))
1369 ds->dec_opts.hwaccel_id = HWACCEL_AUTO;
1370 else {
1371 enum AVHWDeviceType type = av_hwdevice_find_type_by_name(hwaccel);
1372 if (type != AV_HWDEVICE_TYPE_NONE) {
1373 ds->dec_opts.hwaccel_id = HWACCEL_GENERIC;
1374 ds->dec_opts.hwaccel_device_type = type;
1375 }
1376
1377 if (!ds->dec_opts.hwaccel_id) {
1378 av_log(ist, AV_LOG_FATAL, "Unrecognized hwaccel: %s.\n",
1379 hwaccel);
1380 av_log(ist, AV_LOG_FATAL, "Supported hwaccels: ");
1381 type = AV_HWDEVICE_TYPE_NONE;
1382 while ((type = av_hwdevice_iterate_types(type)) !=
1383 AV_HWDEVICE_TYPE_NONE)
1384 av_log(ist, AV_LOG_FATAL, "%s ",
1385 av_hwdevice_get_type_name(type));
1386 av_log(ist, AV_LOG_FATAL, "\n");
1387 return AVERROR(EINVAL);
1388 }
1389 }
1390 }
1391
1392 5945 opt_match_per_stream_str(ist, &o->hwaccel_devices, ic, st, &hwaccel_device);
1393
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5945 times.
5945 if (hwaccel_device) {
1394 ds->dec_opts.hwaccel_device = av_strdup(hwaccel_device);
1395 if (!ds->dec_opts.hwaccel_device)
1396 return AVERROR(ENOMEM);
1397 }
1398 }
1399
1400 7766 ret = choose_decoder(o, ist, ic, st, ds->dec_opts.hwaccel_id,
1401 ds->dec_opts.hwaccel_device_type, &ist->dec);
1402
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7766 times.
7766 if (ret < 0)
1403 return ret;
1404
1405
2/2
✓ Branch 0 taken 7690 times.
✓ Branch 1 taken 76 times.
7766 if (ist->dec) {
1406 7690 ret = filter_codec_opts(o->g->codec_opts, ist->st->codecpar->codec_id,
1407 ic, st, ist->dec, &ds->decoder_opts, opts_used);
1408
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7690 times.
7690 if (ret < 0)
1409 return ret;
1410 }
1411
1412 7766 ds->reinit_filters = -1;
1413 7766 opt_match_per_stream_int(ist, &o->reinit_filters, ic, st, &ds->reinit_filters);
1414
1415 7766 ds->drop_changed = 0;
1416 7766 opt_match_per_stream_int(ist, &o->drop_changed, ic, st, &ds->drop_changed);
1417
1418
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 7766 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
7766 if (ds->drop_changed && ds->reinit_filters) {
1419 if (ds->reinit_filters > 0) {
1420 av_log(ist, AV_LOG_ERROR, "drop_changed and reinit_filters both enabled. These are mutually exclusive.\n");
1421 return AVERROR(EINVAL);
1422 }
1423 ds->reinit_filters = 0;
1424 }
1425
1426 7766 ist->user_set_discard = AVDISCARD_NONE;
1427
1428
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 7766 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
7766 if ((o->video_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) ||
1429
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7765 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
7766 (o->audio_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) ||
1430
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 7765 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
7765 (o->subtitle_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) ||
1431
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 7765 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
7765 (o->data_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_DATA))
1432 1 ist->user_set_discard = AVDISCARD_ALL;
1433
1434 7766 opt_match_per_stream_str(ist, &o->discard, ic, st, &discard_str);
1435
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7766 times.
7766 if (discard_str) {
1436 ret = av_opt_set(ist->st, "discard", discard_str, 0);
1437 if (ret < 0) {
1438 av_log(ist, AV_LOG_ERROR, "Error parsing discard %s.\n", discard_str);
1439 return ret;
1440 }
1441 ist->user_set_discard = ist->st->discard;
1442 }
1443
1444
2/2
✓ Branch 0 taken 95 times.
✓ Branch 1 taken 7671 times.
7766 ds->dec_opts.flags |= DECODER_FLAG_BITEXACT * !!o->bitexact;
1445
1446 7766 av_dict_set_int(&ds->decoder_opts, "apply_cropping",
1447
2/4
✓ Branch 0 taken 7766 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7766 times.
✗ Branch 3 not taken.
7766 ds->apply_cropping && ds->apply_cropping != CROP_CONTAINER, 0);
1448
1449 /* Attached pics are sparse, therefore we would not want to delay their decoding
1450 * till EOF. */
1451
2/2
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 7729 times.
7766 if (ist->st->disposition & AV_DISPOSITION_ATTACHED_PIC)
1452 37 av_dict_set(&ds->decoder_opts, "thread_type", "-frame", 0);
1453
1454
4/5
✓ Branch 0 taken 5945 times.
✓ Branch 1 taken 1675 times.
✓ Branch 2 taken 144 times.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
7766 switch (par->codec_type) {
1455 5945 case AVMEDIA_TYPE_VIDEO:
1456 5945 opt_match_per_stream_str(ist, &o->frame_rates, ic, st, &framerate);
1457
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 5912 times.
5945 if (framerate) {
1458 33 ret = av_parse_video_rate(&ist->framerate, framerate);
1459
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 if (ret < 0) {
1460 av_log(ist, AV_LOG_ERROR, "Error parsing framerate %s.\n",
1461 framerate);
1462 return ret;
1463 }
1464 }
1465
1466 #if FFMPEG_OPT_TOP
1467 5945 ist->top_field_first = -1;
1468 5945 opt_match_per_stream_int(ist, &o->top_field_first, ic, st, &ist->top_field_first);
1469 #endif
1470
1471 5945 break;
1472 1675 case AVMEDIA_TYPE_AUDIO: {
1473 1675 const char *ch_layout_str = NULL;
1474
1475 1675 opt_match_per_stream_str(ist, &o->audio_ch_layouts, ic, st, &ch_layout_str);
1476
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1674 times.
1675 if (ch_layout_str) {
1477 AVChannelLayout ch_layout;
1478 1 ret = av_channel_layout_from_string(&ch_layout, ch_layout_str);
1479
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0) {
1480 av_log(ist, AV_LOG_ERROR, "Error parsing channel layout %s.\n", ch_layout_str);
1481 return ret;
1482 }
1483
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) {
1484 1 av_channel_layout_uninit(&par->ch_layout);
1485 1 par->ch_layout = ch_layout;
1486 } else {
1487 av_log(ist, AV_LOG_ERROR,
1488 "Specified channel layout '%s' has %d channels, but input has %d channels.\n",
1489 ch_layout_str, ch_layout.nb_channels, par->ch_layout.nb_channels);
1490 av_channel_layout_uninit(&ch_layout);
1491 return AVERROR(EINVAL);
1492 }
1493 } else {
1494 1674 int guess_layout_max = INT_MAX;
1495 1674 opt_match_per_stream_int(ist, &o->guess_layout_max, ic, st, &guess_layout_max);
1496 1674 guess_input_channel_layout(ist, par, guess_layout_max);
1497 }
1498 1675 break;
1499 }
1500 144 case AVMEDIA_TYPE_DATA:
1501 case AVMEDIA_TYPE_SUBTITLE: {
1502 144 const char *canvas_size = NULL;
1503
1504 144 opt_match_per_stream_int(ist, &o->fix_sub_duration, ic, st, &ist->fix_sub_duration);
1505 144 opt_match_per_stream_str(ist, &o->canvas_sizes, ic, st, &canvas_size);
1506
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 144 times.
144 if (canvas_size) {
1507 ret = av_parse_video_size(&par->width, &par->height,
1508 canvas_size);
1509 if (ret < 0) {
1510 av_log(ist, AV_LOG_FATAL, "Invalid canvas size: %s.\n", canvas_size);
1511 return ret;
1512 }
1513 }
1514 144 break;
1515 }
1516 2 case AVMEDIA_TYPE_ATTACHMENT:
1517 case AVMEDIA_TYPE_UNKNOWN:
1518 2 break;
1519 default: av_assert0(0);
1520 }
1521
1522 7766 ist->par = avcodec_parameters_alloc();
1523
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7766 times.
7766 if (!ist->par)
1524 return AVERROR(ENOMEM);
1525
1526 7766 ret = avcodec_parameters_copy(ist->par, par);
1527
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7766 times.
7766 if (ret < 0) {
1528 av_log(ist, AV_LOG_ERROR, "Error exporting stream parameters.\n");
1529 return ret;
1530 }
1531
1532
2/2
✓ Branch 0 taken 430 times.
✓ Branch 1 taken 7336 times.
7766 if (ist->st->sample_aspect_ratio.num)
1533 430 ist->par->sample_aspect_ratio = ist->st->sample_aspect_ratio;
1534
1535 7766 opt_match_per_stream_str(ist, &o->bitstream_filters, ic, st, &bsfs);
1536
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7765 times.
7766 if (bsfs) {
1537 1 ret = av_bsf_list_parse_str(bsfs, &ds->bsf);
1538
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0) {
1539 av_log(ist, AV_LOG_ERROR,
1540 "Error parsing bitstream filter sequence '%s': %s\n",
1541 bsfs, av_err2str(ret));
1542 return ret;
1543 }
1544
1545 1 ret = avcodec_parameters_copy(ds->bsf->par_in, ist->par);
1546
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
1547 return ret;
1548 1 ds->bsf->time_base_in = ist->st->time_base;
1549
1550 1 ret = av_bsf_init(ds->bsf);
1551
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0) {
1552 av_log(ist, AV_LOG_ERROR, "Error initializing bitstream filters: %s\n",
1553 av_err2str(ret));
1554 return ret;
1555 }
1556
1557 1 ret = avcodec_parameters_copy(ist->par, ds->bsf->par_out);
1558
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
1559 return ret;
1560 }
1561
1562 7766 ds->codec_desc = avcodec_descriptor_get(ist->par->codec_id);
1563
1564 7766 return 0;
1565 }
1566
1567 static int dump_attachment(InputStream *ist, const char *filename)
1568 {
1569 AVStream *st = ist->st;
1570 int ret;
1571 AVIOContext *out = NULL;
1572 const AVDictionaryEntry *e;
1573
1574 if (!st->codecpar->extradata_size) {
1575 av_log(ist, AV_LOG_WARNING, "No extradata to dump.\n");
1576 return 0;
1577 }
1578 if (!*filename && (e = av_dict_get(st->metadata, "filename", NULL, 0)))
1579 filename = e->value;
1580 if (!*filename) {
1581 av_log(ist, AV_LOG_FATAL, "No filename specified and no 'filename' tag");
1582 return AVERROR(EINVAL);
1583 }
1584
1585 ret = assert_file_overwrite(filename);
1586 if (ret < 0)
1587 return ret;
1588
1589 if ((ret = avio_open2(&out, filename, AVIO_FLAG_WRITE, &int_cb, NULL)) < 0) {
1590 av_log(ist, AV_LOG_FATAL, "Could not open file %s for writing.\n",
1591 filename);
1592 return ret;
1593 }
1594
1595 avio_write(out, st->codecpar->extradata, st->codecpar->extradata_size);
1596 ret = avio_close(out);
1597
1598 if (ret >= 0)
1599 av_log(ist, AV_LOG_INFO, "Wrote attachment (%d bytes) to '%s'\n",
1600 st->codecpar->extradata_size, filename);
1601
1602 return ret;
1603 }
1604
1605 627 static const char *input_file_item_name(void *obj)
1606 {
1607 627 const Demuxer *d = obj;
1608
1609 627 return d->log_name;
1610 }
1611
1612 static const AVClass input_file_class = {
1613 .class_name = "InputFile",
1614 .version = LIBAVUTIL_VERSION_INT,
1615 .item_name = input_file_item_name,
1616 .category = AV_CLASS_CATEGORY_DEMUXER,
1617 };
1618
1619 7207 static Demuxer *demux_alloc(void)
1620 {
1621 7207 Demuxer *d = allocate_array_elem(&input_files, sizeof(*d), &nb_input_files);
1622
1623
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7207 times.
7207 if (!d)
1624 return NULL;
1625
1626 7207 d->f.class = &input_file_class;
1627 7207 d->f.index = nb_input_files - 1;
1628
1629 7207 snprintf(d->log_name, sizeof(d->log_name), "in#%d", d->f.index);
1630
1631 7207 return d;
1632 }
1633
1634 7207 int ifile_open(const OptionsContext *o, const char *filename, Scheduler *sch)
1635 {
1636 Demuxer *d;
1637 InputFile *f;
1638 AVFormatContext *ic;
1639 7207 const AVInputFormat *file_iformat = NULL;
1640 7207 int err, ret = 0;
1641 int64_t timestamp;
1642 7207 AVDictionary *opts_used = NULL;
1643 7207 const char* video_codec_name = NULL;
1644 7207 const char* audio_codec_name = NULL;
1645 7207 const char* subtitle_codec_name = NULL;
1646 7207 const char* data_codec_name = NULL;
1647 7207 int scan_all_pmts_set = 0;
1648
1649 7207 int64_t start_time = o->start_time;
1650 7207 int64_t start_time_eof = o->start_time_eof;
1651 7207 int64_t stop_time = o->stop_time;
1652 7207 int64_t recording_time = o->recording_time;
1653
1654 7207 d = demux_alloc();
1655
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7207 times.
7207 if (!d)
1656 return AVERROR(ENOMEM);
1657
1658 7207 f = &d->f;
1659
1660 7207 ret = sch_add_demux(sch, input_thread, d);
1661
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7207 times.
7207 if (ret < 0)
1662 return ret;
1663 7207 d->sch = sch;
1664
1665
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 7207 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
7207 if (stop_time != INT64_MAX && recording_time != INT64_MAX) {
1666 stop_time = INT64_MAX;
1667 av_log(d, AV_LOG_WARNING, "-t and -to cannot be used together; using -t.\n");
1668 }
1669
1670
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 7207 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
7207 if (stop_time != INT64_MAX && recording_time == INT64_MAX) {
1671 int64_t start = start_time == AV_NOPTS_VALUE ? 0 : start_time;
1672 if (stop_time <= start) {
1673 av_log(d, AV_LOG_ERROR, "-to value smaller than -ss; aborting.\n");
1674 return AVERROR(EINVAL);
1675 } else {
1676 recording_time = stop_time - start;
1677 }
1678 }
1679
1680
2/2
✓ Branch 0 taken 3703 times.
✓ Branch 1 taken 3504 times.
7207 if (o->format) {
1681
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3703 times.
3703 if (!(file_iformat = av_find_input_format(o->format))) {
1682 av_log(d, AV_LOG_FATAL, "Unknown input format: '%s'\n", o->format);
1683 return AVERROR(EINVAL);
1684 }
1685 }
1686
1687
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7207 times.
7207 if (!strcmp(filename, "-"))
1688 filename = "fd:";
1689
1690 21621 stdin_interaction &= strncmp(filename, "pipe:", 5) &&
1691
2/4
✓ Branch 0 taken 7207 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7207 times.
✗ Branch 3 not taken.
14414 strcmp(filename, "fd:") &&
1692
1/2
✓ Branch 0 taken 7207 times.
✗ Branch 1 not taken.
7207 strcmp(filename, "/dev/stdin");
1693
1694 /* get default parameters from command line */
1695 7207 ic = avformat_alloc_context();
1696
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7207 times.
7207 if (!ic)
1697 return AVERROR(ENOMEM);
1698
2/2
✓ Branch 0 taken 67 times.
✓ Branch 1 taken 7140 times.
7207 if (o->audio_sample_rate.nb_opt) {
1699 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);
1700 }
1701
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 7198 times.
7207 if (o->audio_channels.nb_opt) {
1702 const AVClass *priv_class;
1703
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) &&
1704 9 av_opt_find(&priv_class, "ch_layout", NULL, 0,
1705 AV_OPT_SEARCH_FAKE_OBJ)) {
1706 char buf[32];
1707 9 snprintf(buf, sizeof(buf), "%dC", o->audio_channels.opt[o->audio_channels.nb_opt - 1].u.i);
1708 9 av_dict_set(&o->g->format_opts, "ch_layout", buf, 0);
1709 }
1710 }
1711
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7206 times.
7207 if (o->audio_ch_layouts.nb_opt) {
1712 const AVClass *priv_class;
1713
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) &&
1714 1 av_opt_find(&priv_class, "ch_layout", NULL, 0,
1715 AV_OPT_SEARCH_FAKE_OBJ)) {
1716 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);
1717 }
1718 }
1719
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 7174 times.
7207 if (o->frame_rates.nb_opt) {
1720 const AVClass *priv_class;
1721 /* set the format-level framerate option;
1722 * this is important for video grabbers, e.g. x11 */
1723
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) &&
1724 32 av_opt_find(&priv_class, "framerate", NULL, 0,
1725 AV_OPT_SEARCH_FAKE_OBJ)) {
1726 32 av_dict_set(&o->g->format_opts, "framerate",
1727 32 o->frame_rates.opt[o->frame_rates.nb_opt - 1].u.str, 0);
1728 }
1729 }
1730
2/2
✓ Branch 0 taken 567 times.
✓ Branch 1 taken 6640 times.
7207 if (o->frame_sizes.nb_opt) {
1731 567 av_dict_set(&o->g->format_opts, "video_size", o->frame_sizes.opt[o->frame_sizes.nb_opt - 1].u.str, 0);
1732 }
1733
2/2
✓ Branch 0 taken 576 times.
✓ Branch 1 taken 6631 times.
7207 if (o->frame_pix_fmts.nb_opt)
1734 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);
1735
1736 7207 video_codec_name = opt_match_per_type_str(&o->codec_names, 'v');
1737 7207 audio_codec_name = opt_match_per_type_str(&o->codec_names, 'a');
1738 7207 subtitle_codec_name = opt_match_per_type_str(&o->codec_names, 's');
1739 7207 data_codec_name = opt_match_per_type_str(&o->codec_names, 'd');
1740
1741
2/2
✓ Branch 0 taken 3053 times.
✓ Branch 1 taken 4154 times.
7207 if (video_codec_name)
1742 3053 ret = err_merge(ret, find_codec(NULL, video_codec_name , AVMEDIA_TYPE_VIDEO , 0,
1743 3053 &ic->video_codec));
1744
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 7189 times.
7207 if (audio_codec_name)
1745 18 ret = err_merge(ret, find_codec(NULL, audio_codec_name , AVMEDIA_TYPE_AUDIO , 0,
1746 18 &ic->audio_codec));
1747
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7207 times.
7207 if (subtitle_codec_name)
1748 ret = err_merge(ret, find_codec(NULL, subtitle_codec_name, AVMEDIA_TYPE_SUBTITLE, 0,
1749 &ic->subtitle_codec));
1750
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7207 times.
7207 if (data_codec_name)
1751 ret = err_merge(ret, find_codec(NULL, data_codec_name , AVMEDIA_TYPE_DATA, 0,
1752 &ic->data_codec));
1753
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7207 times.
7207 if (ret < 0) {
1754 avformat_free_context(ic);
1755 return ret;
1756 }
1757
1758
2/2
✓ Branch 0 taken 3053 times.
✓ Branch 1 taken 4154 times.
7207 ic->video_codec_id = video_codec_name ? ic->video_codec->id : AV_CODEC_ID_NONE;
1759
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 7189 times.
7207 ic->audio_codec_id = audio_codec_name ? ic->audio_codec->id : AV_CODEC_ID_NONE;
1760
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7207 times.
7207 ic->subtitle_codec_id = subtitle_codec_name ? ic->subtitle_codec->id : AV_CODEC_ID_NONE;
1761
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7207 times.
7207 ic->data_codec_id = data_codec_name ? ic->data_codec->id : AV_CODEC_ID_NONE;
1762
1763 7207 ic->flags |= AVFMT_FLAG_NONBLOCK;
1764
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 7123 times.
7207 if (o->bitexact)
1765 84 ic->flags |= AVFMT_FLAG_BITEXACT;
1766 7207 ic->interrupt_callback = int_cb;
1767
1768
1/2
✓ Branch 1 taken 7207 times.
✗ Branch 2 not taken.
7207 if (!av_dict_get(o->g->format_opts, "scan_all_pmts", NULL, AV_DICT_MATCH_CASE)) {
1769 7207 av_dict_set(&o->g->format_opts, "scan_all_pmts", "1", AV_DICT_DONT_OVERWRITE);
1770 7207 scan_all_pmts_set = 1;
1771 }
1772 /* open the input file with generic avformat function */
1773 7207 err = avformat_open_input(&ic, filename, file_iformat, &o->g->format_opts);
1774
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7207 times.
7207 if (err < 0) {
1775 if (err != AVERROR_EXIT)
1776 av_log(d, AV_LOG_ERROR,
1777 "Error opening input: %s\n", av_err2str(err));
1778 if (err == AVERROR_PROTOCOL_NOT_FOUND)
1779 av_log(d, AV_LOG_ERROR, "Did you mean file:%s?\n", filename);
1780 return err;
1781 }
1782 7207 f->ctx = ic;
1783
1784 7207 av_strlcat(d->log_name, "/", sizeof(d->log_name));
1785 7207 av_strlcat(d->log_name, ic->iformat->name, sizeof(d->log_name));
1786
1787
1/2
✓ Branch 0 taken 7207 times.
✗ Branch 1 not taken.
7207 if (scan_all_pmts_set)
1788 7207 av_dict_set(&o->g->format_opts, "scan_all_pmts", NULL, AV_DICT_MATCH_CASE);
1789 7207 remove_avoptions(&o->g->format_opts, o->g->codec_opts);
1790
1791 7207 ret = check_avoptions(o->g->format_opts);
1792
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7207 times.
7207 if (ret < 0)
1793 return ret;
1794
1795 /* apply forced codec ids */
1796
2/2
✓ Branch 0 taken 7615 times.
✓ Branch 1 taken 7207 times.
14822 for (int i = 0; i < ic->nb_streams; i++) {
1797 const AVCodec *dummy;
1798 7615 ret = choose_decoder(o, f, ic, ic->streams[i], HWACCEL_NONE, AV_HWDEVICE_TYPE_NONE,
1799 &dummy);
1800
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7615 times.
7615 if (ret < 0)
1801 return ret;
1802 }
1803
1804
2/2
✓ Branch 0 taken 7205 times.
✓ Branch 1 taken 2 times.
7207 if (o->find_stream_info) {
1805 AVDictionary **opts;
1806 7205 int orig_nb_streams = ic->nb_streams;
1807
1808 7205 ret = setup_find_stream_info_opts(ic, o->g->codec_opts, &opts);
1809
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7205 times.
7205 if (ret < 0)
1810 return ret;
1811
1812 /* If not enough info to get the stream parameters, we decode the
1813 first frames to get it. (used in mpeg case for example) */
1814 7205 ret = avformat_find_stream_info(ic, opts);
1815
1816
2/2
✓ Branch 0 taken 7613 times.
✓ Branch 1 taken 7205 times.
14818 for (int i = 0; i < orig_nb_streams; i++)
1817 7613 av_dict_free(&opts[i]);
1818 7205 av_freep(&opts);
1819
1820
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7205 times.
7205 if (ret < 0) {
1821 av_log(d, AV_LOG_FATAL, "could not find codec parameters\n");
1822 if (ic->nb_streams == 0)
1823 return ret;
1824 }
1825 }
1826
1827
3/4
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 7186 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 21 times.
7207 if (start_time != AV_NOPTS_VALUE && start_time_eof != AV_NOPTS_VALUE) {
1828 av_log(d, AV_LOG_WARNING, "Cannot use -ss and -sseof both, using -ss\n");
1829 start_time_eof = AV_NOPTS_VALUE;
1830 }
1831
1832
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7207 times.
7207 if (start_time_eof != AV_NOPTS_VALUE) {
1833 if (start_time_eof >= 0) {
1834 av_log(d, AV_LOG_ERROR, "-sseof value must be negative; aborting\n");
1835 return AVERROR(EINVAL);
1836 }
1837 if (ic->duration > 0) {
1838 start_time = start_time_eof + ic->duration;
1839 if (start_time < 0) {
1840 av_log(d, AV_LOG_WARNING, "-sseof value seeks to before start of file; ignored\n");
1841 start_time = AV_NOPTS_VALUE;
1842 }
1843 } else
1844 av_log(d, AV_LOG_WARNING, "Cannot use -sseof, file duration not known\n");
1845 }
1846
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 7186 times.
7207 timestamp = (start_time == AV_NOPTS_VALUE) ? 0 : start_time;
1847 /* add the stream start time */
1848
4/4
✓ Branch 0 taken 7204 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 5487 times.
✓ Branch 3 taken 1717 times.
7207 if (!o->seek_timestamp && ic->start_time != AV_NOPTS_VALUE)
1849 5487 timestamp += ic->start_time;
1850
1851 /* if seeking requested, we execute it */
1852
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 7186 times.
7207 if (start_time != AV_NOPTS_VALUE) {
1853 21 int64_t seek_timestamp = timestamp;
1854
1855
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
21 if (!(ic->iformat->flags & AVFMT_SEEK_TO_PTS)) {
1856 21 int dts_heuristic = 0;
1857
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 21 times.
42 for (int i = 0; i < ic->nb_streams; i++) {
1858 21 const AVCodecParameters *par = ic->streams[i]->codecpar;
1859
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (par->video_delay) {
1860 dts_heuristic = 1;
1861 break;
1862 }
1863 }
1864
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (dts_heuristic) {
1865 seek_timestamp -= 3*AV_TIME_BASE / 23;
1866 }
1867 }
1868 21 ret = avformat_seek_file(ic, -1, INT64_MIN, seek_timestamp, seek_timestamp, 0);
1869
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 20 times.
21 if (ret < 0) {
1870 1 av_log(d, AV_LOG_WARNING, "could not seek to position %0.3f\n",
1871 1 (double)timestamp / AV_TIME_BASE);
1872 }
1873 }
1874
1875 7207 f->start_time = start_time;
1876 7207 d->recording_time = recording_time;
1877 7207 f->input_sync_ref = o->input_sync_ref;
1878 7207 f->input_ts_offset = o->input_ts_offset;
1879
3/6
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 7194 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 13 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
7207 f->ts_offset = o->input_ts_offset - (copy_ts ? (start_at_zero && ic->start_time != AV_NOPTS_VALUE ? ic->start_time : 0) : timestamp);
1880 7207 d->accurate_seek = o->accurate_seek;
1881 7207 d->loop = o->loop;
1882 7207 d->nb_streams_warn = ic->nb_streams;
1883
1884 7207 d->duration = (Timestamp){ .ts = 0, .tb = (AVRational){ 1, 1 } };
1885 7207 d->min_pts = (Timestamp){ .ts = AV_NOPTS_VALUE, .tb = (AVRational){ 1, 1 } };
1886 7207 d->max_pts = (Timestamp){ .ts = AV_NOPTS_VALUE, .tb = (AVRational){ 1, 1 } };
1887
1888 7207 d->readrate = o->readrate ? o->readrate : 0.0;
1889
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7207 times.
7207 if (d->readrate < 0.0f) {
1890 av_log(d, AV_LOG_ERROR, "Option -readrate is %0.3f; it must be non-negative.\n", d->readrate);
1891 return AVERROR(EINVAL);
1892 }
1893
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7206 times.
7207 if (o->rate_emu) {
1894
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (d->readrate) {
1895 av_log(d, AV_LOG_WARNING, "Both -readrate and -re set. Using -readrate %0.3f.\n", d->readrate);
1896 } else
1897 1 d->readrate = 1.0f;
1898 }
1899
1900
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7206 times.
7207 if (d->readrate) {
1901
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;
1902
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (d->readrate_initial_burst < 0.0) {
1903 av_log(d, AV_LOG_ERROR,
1904 "Option -readrate_initial_burst is %0.3f; it must be non-negative.\n",
1905 d->readrate_initial_burst);
1906 return AVERROR(EINVAL);
1907 }
1908
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;
1909
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (d->readrate_catchup < d->readrate) {
1910 av_log(d, AV_LOG_ERROR,
1911 "Option -readrate_catchup is %0.3f; it must be at least equal to %0.3f.\n",
1912 d->readrate_catchup, d->readrate);
1913 return AVERROR(EINVAL);
1914 }
1915 } else {
1916
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7206 times.
7206 if (o->readrate_initial_burst) {
1917 av_log(d, AV_LOG_WARNING, "Option -readrate_initial_burst ignored "
1918 "since neither -readrate nor -re were given\n");
1919 }
1920
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7206 times.
7206 if (o->readrate_catchup) {
1921 av_log(d, AV_LOG_WARNING, "Option -readrate_catchup ignored "
1922 "since neither -readrate nor -re were given\n");
1923 }
1924 }
1925
1926 /* Add all the streams from the given input file to the demuxer */
1927
2/2
✓ Branch 0 taken 7766 times.
✓ Branch 1 taken 7207 times.
14973 for (int i = 0; i < ic->nb_streams; i++) {
1928 7766 ret = ist_add(o, d, ic->streams[i], &opts_used);
1929
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7766 times.
7766 if (ret < 0) {
1930 av_dict_free(&opts_used);
1931 return ret;
1932 }
1933 }
1934
1935 /* dump the file content */
1936 7207 av_dump_format(ic, f->index, filename, 0);
1937
1938 /* check if all codec options have been used */
1939 7207 ret = check_avoptions_used(o->g->codec_opts, opts_used, d, 1);
1940 7207 av_dict_free(&opts_used);
1941
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7207 times.
7207 if (ret < 0)
1942 return ret;
1943
1944
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7207 times.
7207 for (int i = 0; i < o->dump_attachment.nb_opt; i++) {
1945 for (int j = 0; j < f->nb_streams; j++) {
1946 InputStream *ist = f->streams[j];
1947
1948 if (check_stream_specifier(ic, ist->st, o->dump_attachment.opt[i].specifier) == 1) {
1949 ret = dump_attachment(ist, o->dump_attachment.opt[i].u.str);
1950 if (ret < 0)
1951 return ret;
1952 }
1953 }
1954 }
1955
1956 7207 return 0;
1957 }
1958