FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/fftools/ffmpeg_demux.c
Date: 2025-10-10 03:51:19
Exec Total Coverage
Lines: 849 1105 76.8%
Functions: 33 34 97.1%
Branches: 528 814 64.9%

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