FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/fftools/ffmpeg_demux.c
Date: 2023-09-22 12:20:07
Exec Total Coverage
Lines: 730 950 76.8%
Functions: 33 34 97.1%
Branches: 500 1024 48.8%

Line Branch Exec Source
1 /*
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include <float.h>
20 #include <stdint.h>
21
22 #include "ffmpeg.h"
23
24 #include "libavutil/avassert.h"
25 #include "libavutil/avstring.h"
26 #include "libavutil/display.h"
27 #include "libavutil/error.h"
28 #include "libavutil/intreadwrite.h"
29 #include "libavutil/opt.h"
30 #include "libavutil/parseutils.h"
31 #include "libavutil/pixdesc.h"
32 #include "libavutil/time.h"
33 #include "libavutil/timestamp.h"
34 #include "libavutil/thread.h"
35 #include "libavutil/threadmessage.h"
36
37 #include "libavcodec/packet.h"
38
39 #include "libavformat/avformat.h"
40
41 static const char *const opt_name_discard[] = {"discard", NULL};
42 static const char *const opt_name_reinit_filters[] = {"reinit_filter", NULL};
43 static const char *const opt_name_fix_sub_duration[] = {"fix_sub_duration", NULL};
44 static const char *const opt_name_canvas_sizes[] = {"canvas_size", NULL};
45 static const char *const opt_name_guess_layout_max[] = {"guess_layout_max", NULL};
46 static const char *const opt_name_ts_scale[] = {"itsscale", NULL};
47 static const char *const opt_name_hwaccels[] = {"hwaccel", NULL};
48 static const char *const opt_name_hwaccel_devices[] = {"hwaccel_device", NULL};
49 static const char *const opt_name_hwaccel_output_formats[] = {"hwaccel_output_format", NULL};
50 static const char *const opt_name_autorotate[] = {"autorotate", NULL};
51 static const char *const opt_name_display_rotations[] = {"display_rotation", NULL};
52 static const char *const opt_name_display_hflips[] = {"display_hflip", NULL};
53 static const char *const opt_name_display_vflips[] = {"display_vflip", NULL};
54
55 typedef struct DemuxStream {
56 InputStream ist;
57
58 // name used for logging
59 char log_name[32];
60
61 double ts_scale;
62
63 int streamcopy_needed;
64
65 int wrap_correction_done;
66 int saw_first_ts;
67 ///< dts of the first packet read for this stream (in AV_TIME_BASE units)
68 int64_t first_dts;
69
70 /* predicted dts of the next packet read for this stream or (when there are
71 * several frames in a packet) of the next frame in current packet (in AV_TIME_BASE units) */
72 int64_t next_dts;
73 ///< dts of the last packet read for this stream (in AV_TIME_BASE units)
74 int64_t dts;
75
76 int64_t min_pts; /* pts with the smallest value in a current stream */
77 int64_t max_pts; /* pts with the higher value in a current stream */
78
79 /* number of packets successfully read for this stream */
80 uint64_t nb_packets;
81 // combined size of all the packets read
82 uint64_t data_size;
83 } DemuxStream;
84
85 typedef struct Demuxer {
86 InputFile f;
87
88 // name used for logging
89 char log_name[32];
90
91 int64_t wallclock_start;
92
93 /**
94 * Extra timestamp offset added by discontinuity handling.
95 */
96 int64_t ts_offset_discont;
97 int64_t last_ts;
98
99 /* number of times input stream should be looped */
100 int loop;
101 /* actual duration of the longest stream in a file at the moment when
102 * looping happens */
103 int64_t duration;
104 /* time base of the duration */
105 AVRational time_base;
106
107 /* number of streams that the user was warned of */
108 int nb_streams_warn;
109
110 double readrate_initial_burst;
111
112 AVThreadMessageQueue *in_thread_queue;
113 int thread_queue_size;
114 pthread_t thread;
115 int non_blocking;
116
117 int read_started;
118 } Demuxer;
119
120 typedef struct DemuxMsg {
121 AVPacket *pkt;
122 int looping;
123 } DemuxMsg;
124
125 1340770 static DemuxStream *ds_from_ist(InputStream *ist)
126 {
127 1340770 return (DemuxStream*)ist;
128 }
129
130 460400 static Demuxer *demuxer_from_ifile(InputFile *f)
131 {
132 460400 return (Demuxer*)f;
133 }
134
135 51 InputStream *ist_find_unused(enum AVMediaType type)
136 {
137
1/2
✓ Branch 2 taken 70 times.
✗ Branch 3 not taken.
70 for (InputStream *ist = ist_iter(NULL); ist; ist = ist_iter(ist)) {
138
4/4
✓ Branch 0 taken 69 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 51 times.
✓ Branch 3 taken 18 times.
70 if (ist->par->codec_type == type && ist->discard &&
139
1/2
✓ Branch 0 taken 51 times.
✗ Branch 1 not taken.
51 ist->user_set_discard != AVDISCARD_ALL)
140 51 return ist;
141 }
142 return NULL;
143 }
144
145 8198 static void report_new_stream(Demuxer *d, const AVPacket *pkt)
146 {
147 8198 AVStream *st = d->f.ctx->streams[pkt->stream_index];
148
149
1/2
✓ Branch 0 taken 8198 times.
✗ Branch 1 not taken.
8198 if (pkt->stream_index < d->nb_streams_warn)
150 8198 return;
151 av_log(d, AV_LOG_WARNING,
152 "New %s stream with index %d at pos:%"PRId64" and DTS:%ss\n",
153 av_get_media_type_string(st->codecpar->codec_type),
154 pkt->stream_index, pkt->pos, av_ts2timestr(pkt->dts, &st->time_base));
155 d->nb_streams_warn = pkt->stream_index + 1;
156 }
157
158 9 static void ifile_duration_update(Demuxer *d, DemuxStream *ds,
159 int64_t last_duration)
160 {
161 /* the total duration of the stream, max_pts - min_pts is
162 * the duration of the stream without the last frame */
163
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 if (ds->max_pts > ds->min_pts &&
164
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 ds->max_pts - (uint64_t)ds->min_pts < INT64_MAX - last_duration)
165 9 last_duration += ds->max_pts - ds->min_pts;
166
167
3/4
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
15 if (!d->duration ||
168 6 av_compare_ts(d->duration, d->time_base,
169 6 last_duration, ds->ist.st->time_base) < 0) {
170 9 d->duration = last_duration;
171 9 d->time_base = ds->ist.st->time_base;
172 }
173 9 }
174
175 9 static int seek_to_start(Demuxer *d)
176 {
177 9 InputFile *ifile = &d->f;
178 9 AVFormatContext *is = ifile->ctx;
179 int ret;
180
181 9 ret = avformat_seek_file(is, -1, INT64_MIN, is->start_time, is->start_time, 0);
182
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (ret < 0)
183 return ret;
184
185
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 6 times.
9 if (ifile->audio_duration_queue_size) {
186 /* duration is the length of the last frame in a stream
187 * when audio stream is present we don't care about
188 * last video frame length because it's not defined exactly */
189 3 int got_durations = 0;
190
191
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 while (got_durations < ifile->audio_duration_queue_size) {
192 DemuxStream *ds;
193 LastFrameDuration dur;
194 3 ret = av_thread_message_queue_recv(ifile->audio_duration_queue, &dur, 0);
195
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0)
196 return ret;
197 3 got_durations++;
198
199 3 ds = ds_from_ist(ifile->streams[dur.stream_idx]);
200 3 ifile_duration_update(d, ds, dur.duration);
201 }
202 } else {
203
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
12 for (int i = 0; i < ifile->nb_streams; i++) {
204 6 int64_t duration = 0;
205 6 InputStream *ist = ifile->streams[i];
206 6 DemuxStream *ds = ds_from_ist(ist);
207
208
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (ist->framerate.num) {
209 duration = av_rescale_q(1, av_inv_q(ist->framerate), ist->st->time_base);
210
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
6 } else if (ist->st->avg_frame_rate.num) {
211 2 duration = av_rescale_q(1, av_inv_q(ist->st->avg_frame_rate), ist->st->time_base);
212 } else {
213 4 duration = 1;
214 }
215
216 6 ifile_duration_update(d, ds, duration);
217 }
218 }
219
220
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 if (d->loop > 0)
221 9 d->loop--;
222
223 9 return ret;
224 }
225
226 412577 static void ts_discontinuity_detect(Demuxer *d, InputStream *ist,
227 AVPacket *pkt)
228 {
229 412577 InputFile *ifile = &d->f;
230 412577 DemuxStream *ds = ds_from_ist(ist);
231 412577 const int fmt_is_discont = ifile->ctx->iformat->flags & AVFMT_TS_DISCONT;
232 412577 int disable_discontinuity_correction = copy_ts;
233 412577 int64_t pkt_dts = av_rescale_q_rnd(pkt->dts, pkt->time_base, AV_TIME_BASE_Q,
234 AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX);
235
236
6/6
✓ Branch 0 taken 180 times.
✓ Branch 1 taken 412397 times.
✓ Branch 2 taken 175 times.
✓ Branch 3 taken 5 times.
✓ Branch 4 taken 75 times.
✓ Branch 5 taken 100 times.
412577 if (copy_ts && ds->next_dts != AV_NOPTS_VALUE &&
237
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 75 times.
75 fmt_is_discont && ist->st->pts_wrap_bits < 60) {
238 int64_t wrap_dts = av_rescale_q_rnd(pkt->dts + (1LL<<ist->st->pts_wrap_bits),
239 pkt->time_base, AV_TIME_BASE_Q,
240 AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
241 if (FFABS(wrap_dts - ds->next_dts) < FFABS(pkt_dts - ds->next_dts)/10)
242 disable_discontinuity_correction = 0;
243 }
244
245
4/4
✓ Branch 0 taken 406311 times.
✓ Branch 1 taken 6266 times.
✓ Branch 2 taken 406136 times.
✓ Branch 3 taken 175 times.
818713 if (ds->next_dts != AV_NOPTS_VALUE && !disable_discontinuity_correction) {
246 406136 int64_t delta = pkt_dts - ds->next_dts;
247
2/2
✓ Branch 0 taken 22603 times.
✓ Branch 1 taken 383533 times.
406136 if (fmt_is_discont) {
248
4/4
✓ Branch 0 taken 22563 times.
✓ Branch 1 taken 40 times.
✓ Branch 2 taken 22602 times.
✓ Branch 3 taken 1 times.
22603 if (FFABS(delta) > 1LL * dts_delta_threshold * AV_TIME_BASE ||
249
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 22594 times.
22602 pkt_dts + AV_TIME_BASE/10 < ds->dts) {
250 9 d->ts_offset_discont -= delta;
251 9 av_log(ist, AV_LOG_WARNING,
252 "timestamp discontinuity "
253 "(stream id=%d): %"PRId64", new offset= %"PRId64"\n",
254 9 ist->st->id, delta, d->ts_offset_discont);
255 9 pkt->dts -= av_rescale_q(delta, AV_TIME_BASE_Q, pkt->time_base);
256
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 if (pkt->pts != AV_NOPTS_VALUE)
257 9 pkt->pts -= av_rescale_q(delta, AV_TIME_BASE_Q, pkt->time_base);
258 }
259 } else {
260
3/4
✓ Branch 0 taken 380068 times.
✓ Branch 1 taken 3465 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 383533 times.
383533 if (FFABS(delta) > 1LL * dts_error_threshold * AV_TIME_BASE) {
261 av_log(NULL, AV_LOG_WARNING,
262 "DTS %"PRId64", next:%"PRId64" st:%d invalid dropping\n",
263 pkt->dts, ds->next_dts, pkt->stream_index);
264 pkt->dts = AV_NOPTS_VALUE;
265 }
266
2/2
✓ Branch 0 taken 381489 times.
✓ Branch 1 taken 2044 times.
383533 if (pkt->pts != AV_NOPTS_VALUE){
267 381489 int64_t pkt_pts = av_rescale_q(pkt->pts, pkt->time_base, AV_TIME_BASE_Q);
268 381489 delta = pkt_pts - ds->next_dts;
269
3/4
✓ Branch 0 taken 378347 times.
✓ Branch 1 taken 3142 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 381489 times.
381489 if (FFABS(delta) > 1LL * dts_error_threshold * AV_TIME_BASE) {
270 av_log(NULL, AV_LOG_WARNING,
271 "PTS %"PRId64", next:%"PRId64" invalid dropping st:%d\n",
272 pkt->pts, ds->next_dts, pkt->stream_index);
273 pkt->pts = AV_NOPTS_VALUE;
274 }
275 }
276 }
277
6/6
✓ Branch 0 taken 6266 times.
✓ Branch 1 taken 175 times.
✓ Branch 2 taken 6261 times.
✓ Branch 3 taken 5 times.
✓ Branch 4 taken 114 times.
✓ Branch 5 taken 6147 times.
6441 } else if (ds->next_dts == AV_NOPTS_VALUE && !copy_ts &&
278
1/2
✓ Branch 0 taken 114 times.
✗ Branch 1 not taken.
114 fmt_is_discont && d->last_ts != AV_NOPTS_VALUE) {
279 114 int64_t delta = pkt_dts - d->last_ts;
280
3/4
✓ Branch 0 taken 54 times.
✓ Branch 1 taken 60 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 114 times.
114 if (FFABS(delta) > 1LL * dts_delta_threshold * AV_TIME_BASE) {
281 d->ts_offset_discont -= delta;
282 av_log(NULL, AV_LOG_DEBUG,
283 "Inter stream timestamp discontinuity %"PRId64", new offset= %"PRId64"\n",
284 delta, d->ts_offset_discont);
285 pkt->dts -= av_rescale_q(delta, AV_TIME_BASE_Q, pkt->time_base);
286 if (pkt->pts != AV_NOPTS_VALUE)
287 pkt->pts -= av_rescale_q(delta, AV_TIME_BASE_Q, pkt->time_base);
288 }
289 }
290
291 412577 d->last_ts = av_rescale_q(pkt->dts, pkt->time_base, AV_TIME_BASE_Q);
292 412577 }
293
294 456968 static void ts_discontinuity_process(Demuxer *d, InputStream *ist,
295 AVPacket *pkt)
296 {
297 456968 int64_t offset = av_rescale_q(d->ts_offset_discont, AV_TIME_BASE_Q,
298 pkt->time_base);
299
300 // apply previously-detected timestamp-discontinuity offset
301 // (to all streams, not just audio/video)
302
2/2
✓ Branch 0 taken 415161 times.
✓ Branch 1 taken 41807 times.
456968 if (pkt->dts != AV_NOPTS_VALUE)
303 415161 pkt->dts += offset;
304
2/2
✓ Branch 0 taken 413074 times.
✓ Branch 1 taken 43894 times.
456968 if (pkt->pts != AV_NOPTS_VALUE)
305 413074 pkt->pts += offset;
306
307 // detect timestamp discontinuities for audio/video
308
2/2
✓ Branch 0 taken 323754 times.
✓ Branch 1 taken 133214 times.
456968 if ((ist->par->codec_type == AVMEDIA_TYPE_VIDEO ||
309
2/2
✓ Branch 0 taken 321166 times.
✓ Branch 1 taken 2588 times.
323754 ist->par->codec_type == AVMEDIA_TYPE_AUDIO) &&
310
2/2
✓ Branch 0 taken 412577 times.
✓ Branch 1 taken 41803 times.
454380 pkt->dts != AV_NOPTS_VALUE)
311 412577 ts_discontinuity_detect(d, ist, pkt);
312 456968 }
313
314 456968 static int ist_dts_update(DemuxStream *ds, AVPacket *pkt)
315 {
316 456968 InputStream *ist = &ds->ist;
317 456968 const AVCodecParameters *par = ist->par;
318
319
2/2
✓ Branch 0 taken 6835 times.
✓ Branch 1 taken 450133 times.
456968 if (!ds->saw_first_ts) {
320 6835 ds->first_dts =
321
2/2
✓ Branch 0 taken 4708 times.
✓ Branch 1 taken 2127 times.
6835 ds->dts = ist->st->avg_frame_rate.num ? - ist->par->video_delay * AV_TIME_BASE / av_q2d(ist->st->avg_frame_rate) : 0;
322
2/2
✓ Branch 0 taken 6285 times.
✓ Branch 1 taken 550 times.
6835 if (pkt->pts != AV_NOPTS_VALUE) {
323 6285 ds->first_dts =
324 6285 ds->dts += av_rescale_q(pkt->pts, pkt->time_base, AV_TIME_BASE_Q);
325 }
326 6835 ds->saw_first_ts = 1;
327 }
328
329
2/2
✓ Branch 0 taken 6835 times.
✓ Branch 1 taken 450133 times.
456968 if (ds->next_dts == AV_NOPTS_VALUE)
330 6835 ds->next_dts = ds->dts;
331
332
2/2
✓ Branch 0 taken 415161 times.
✓ Branch 1 taken 41807 times.
456968 if (pkt->dts != AV_NOPTS_VALUE)
333 415161 ds->next_dts = ds->dts = av_rescale_q(pkt->dts, pkt->time_base, AV_TIME_BASE_Q);
334
335 456968 ds->dts = ds->next_dts;
336
3/3
✓ Branch 0 taken 321166 times.
✓ Branch 1 taken 133214 times.
✓ Branch 2 taken 2588 times.
456968 switch (par->codec_type) {
337 321166 case AVMEDIA_TYPE_AUDIO:
338 av_assert1(pkt->duration >= 0);
339
1/2
✓ Branch 0 taken 321166 times.
✗ Branch 1 not taken.
321166 if (par->sample_rate) {
340 321166 ds->next_dts += ((int64_t)AV_TIME_BASE * par->frame_size) /
341 321166 par->sample_rate;
342 } else {
343 ds->next_dts += av_rescale_q(pkt->duration, pkt->time_base, AV_TIME_BASE_Q);
344 }
345 321166 break;
346 133214 case AVMEDIA_TYPE_VIDEO:
347
2/2
✓ Branch 0 taken 237 times.
✓ Branch 1 taken 132977 times.
133214 if (ist->framerate.num) {
348 // TODO: Remove work-around for c99-to-c89 issue 7
349 237 AVRational time_base_q = AV_TIME_BASE_Q;
350 237 int64_t next_dts = av_rescale_q(ds->next_dts, time_base_q, av_inv_q(ist->framerate));
351 237 ds->next_dts = av_rescale_q(next_dts + 1, av_inv_q(ist->framerate), time_base_q);
352
2/2
✓ Branch 0 taken 130808 times.
✓ Branch 1 taken 2169 times.
132977 } else if (pkt->duration) {
353 130808 ds->next_dts += av_rescale_q(pkt->duration, pkt->time_base, AV_TIME_BASE_Q);
354
2/2
✓ Branch 0 taken 154 times.
✓ Branch 1 taken 2015 times.
2169 } else if (ist->par->framerate.num != 0) {
355 154 AVRational field_rate = av_mul_q(ist->par->framerate,
356 154 (AVRational){ 2, 1 });
357 154 int fields = 2;
358
359
1/2
✓ Branch 0 taken 154 times.
✗ Branch 1 not taken.
154 if (ist->codec_desc &&
360
4/4
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 130 times.
✓ Branch 2 taken 14 times.
✓ Branch 3 taken 10 times.
178 (ist->codec_desc->props & AV_CODEC_PROP_FIELDS) &&
361 24 av_stream_get_parser(ist->st))
362 14 fields = 1 + av_stream_get_parser(ist->st)->repeat_pict;
363
364 154 ds->next_dts += av_rescale_q(fields, av_inv_q(field_rate), AV_TIME_BASE_Q);
365 }
366 133214 break;
367 }
368
369
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 456968 times.
456968 av_assert0(!pkt->opaque_ref);
370
2/2
✓ Branch 0 taken 72119 times.
✓ Branch 1 taken 384849 times.
456968 if (ds->streamcopy_needed) {
371 DemuxPktData *pd;
372
373 72119 pkt->opaque_ref = av_buffer_allocz(sizeof(*pd));
374
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 72119 times.
72119 if (!pkt->opaque_ref)
375 return AVERROR(ENOMEM);
376 72119 pd = (DemuxPktData*)pkt->opaque_ref->data;
377
378 72119 pd->dts_est = ds->dts;
379 }
380
381 456968 return 0;
382 }
383
384 456968 static int ts_fixup(Demuxer *d, AVPacket *pkt)
385 {
386 456968 InputFile *ifile = &d->f;
387 456968 InputStream *ist = ifile->streams[pkt->stream_index];
388 456968 DemuxStream *ds = ds_from_ist(ist);
389 456968 const int64_t start_time = ifile->start_time_effective;
390 int64_t duration;
391 int ret;
392
393 456968 pkt->time_base = ist->st->time_base;
394
395 #define SHOW_TS_DEBUG(tag_) \
396 if (debug_ts) { \
397 av_log(ist, AV_LOG_INFO, "%s -> ist_index:%d:%d type:%s " \
398 "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s duration:%s duration_time:%s\n", \
399 tag_, ifile->index, pkt->stream_index, \
400 av_get_media_type_string(ist->st->codecpar->codec_type), \
401 av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &pkt->time_base), \
402 av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &pkt->time_base), \
403 av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &pkt->time_base)); \
404 }
405
406
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 456968 times.
456968 SHOW_TS_DEBUG("demuxer");
407
408
4/4
✓ Branch 0 taken 407497 times.
✓ Branch 1 taken 49471 times.
✓ Branch 2 taken 271717 times.
✓ Branch 3 taken 135780 times.
456968 if (!ds->wrap_correction_done && start_time != AV_NOPTS_VALUE &&
409
2/2
✓ Branch 0 taken 318 times.
✓ Branch 1 taken 271399 times.
271717 ist->st->pts_wrap_bits < 64) {
410 int64_t stime, stime2;
411
412 318 stime = av_rescale_q(start_time, AV_TIME_BASE_Q, pkt->time_base);
413 318 stime2= stime + (1ULL<<ist->st->pts_wrap_bits);
414 318 ds->wrap_correction_done = 1;
415
416
5/6
✓ Branch 0 taken 289 times.
✓ Branch 1 taken 29 times.
✓ Branch 2 taken 255 times.
✓ Branch 3 taken 34 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 255 times.
318 if(stime2 > stime && pkt->dts != AV_NOPTS_VALUE && pkt->dts > stime + (1LL<<(ist->st->pts_wrap_bits-1))) {
417 pkt->dts -= 1ULL<<ist->st->pts_wrap_bits;
418 ds->wrap_correction_done = 0;
419 }
420
5/6
✓ Branch 0 taken 289 times.
✓ Branch 1 taken 29 times.
✓ Branch 2 taken 249 times.
✓ Branch 3 taken 40 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 249 times.
318 if(stime2 > stime && pkt->pts != AV_NOPTS_VALUE && pkt->pts > stime + (1LL<<(ist->st->pts_wrap_bits-1))) {
421 pkt->pts -= 1ULL<<ist->st->pts_wrap_bits;
422 ds->wrap_correction_done = 0;
423 }
424 }
425
426
2/2
✓ Branch 0 taken 415161 times.
✓ Branch 1 taken 41807 times.
456968 if (pkt->dts != AV_NOPTS_VALUE)
427 415161 pkt->dts += av_rescale_q(ifile->ts_offset, AV_TIME_BASE_Q, pkt->time_base);
428
2/2
✓ Branch 0 taken 413074 times.
✓ Branch 1 taken 43894 times.
456968 if (pkt->pts != AV_NOPTS_VALUE)
429 413074 pkt->pts += av_rescale_q(ifile->ts_offset, AV_TIME_BASE_Q, pkt->time_base);
430
431
2/2
✓ Branch 0 taken 413074 times.
✓ Branch 1 taken 43894 times.
456968 if (pkt->pts != AV_NOPTS_VALUE)
432 413074 pkt->pts *= ds->ts_scale;
433
2/2
✓ Branch 0 taken 415161 times.
✓ Branch 1 taken 41807 times.
456968 if (pkt->dts != AV_NOPTS_VALUE)
434 415161 pkt->dts *= ds->ts_scale;
435
436 456968 duration = av_rescale_q(d->duration, d->time_base, pkt->time_base);
437
2/2
✓ Branch 0 taken 413074 times.
✓ Branch 1 taken 43894 times.
456968 if (pkt->pts != AV_NOPTS_VALUE) {
438 413074 pkt->pts += duration;
439 413074 ds->max_pts = FFMAX(pkt->pts, ds->max_pts);
440 413074 ds->min_pts = FFMIN(pkt->pts, ds->min_pts);
441 }
442
443
2/2
✓ Branch 0 taken 415161 times.
✓ Branch 1 taken 41807 times.
456968 if (pkt->dts != AV_NOPTS_VALUE)
444 415161 pkt->dts += duration;
445
446
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 456968 times.
456968 SHOW_TS_DEBUG("demuxer+tsfixup");
447
448 // detect and try to correct for timestamp discontinuities
449 456968 ts_discontinuity_process(d, ist, pkt);
450
451 // update estimated/predicted dts
452 456968 ret = ist_dts_update(ds, pkt);
453
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 456968 times.
456968 if (ret < 0)
454 return ret;
455
456 456968 return 0;
457 }
458
459 // process an input packet into a message to send to the consumer thread
460 // src is always cleared by this function
461 456968 static int input_packet_process(Demuxer *d, DemuxMsg *msg, AVPacket *src)
462 {
463 456968 InputFile *f = &d->f;
464 456968 InputStream *ist = f->streams[src->stream_index];
465 456968 DemuxStream *ds = ds_from_ist(ist);
466 AVPacket *pkt;
467 456968 int ret = 0;
468
469 456968 pkt = av_packet_alloc();
470
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 456968 times.
456968 if (!pkt) {
471 av_packet_unref(src);
472 return AVERROR(ENOMEM);
473 }
474 456968 av_packet_move_ref(pkt, src);
475
476 456968 ret = ts_fixup(d, pkt);
477
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 456968 times.
456968 if (ret < 0)
478 goto fail;
479
480 456968 ds->data_size += pkt->size;
481 456968 ds->nb_packets++;
482
483 /* add the stream-global side data to the first packet */
484
2/2
✓ Branch 0 taken 6835 times.
✓ Branch 1 taken 450133 times.
456968 if (ds->nb_packets == 1) {
485
2/2
✓ Branch 0 taken 341 times.
✓ Branch 1 taken 6835 times.
7176 for (int i = 0; i < ist->st->nb_side_data; i++) {
486 341 AVPacketSideData *src_sd = &ist->st->side_data[i];
487 uint8_t *dst_data;
488
489
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 335 times.
341 if (src_sd->type == AV_PKT_DATA_DISPLAYMATRIX)
490 6 continue;
491
492
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 335 times.
335 if (av_packet_get_side_data(pkt, src_sd->type, NULL))
493 continue;
494
495 335 dst_data = av_packet_new_side_data(pkt, src_sd->type, src_sd->size);
496
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 335 times.
335 if (!dst_data) {
497 ret = AVERROR(ENOMEM);
498 goto fail;
499 }
500
501 335 memcpy(dst_data, src_sd->data, src_sd->size);
502 }
503 }
504
505
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 456968 times.
456968 if (debug_ts) {
506 av_log(NULL, AV_LOG_INFO, "demuxer+ffmpeg -> ist_index:%d:%d type:%s pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s duration:%s duration_time:%s off:%s off_time:%s\n",
507 f->index, pkt->stream_index,
508 av_get_media_type_string(ist->par->codec_type),
509 av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &pkt->time_base),
510 av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &pkt->time_base),
511 av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &pkt->time_base),
512 av_ts2str(input_files[ist->file_index]->ts_offset),
513 av_ts2timestr(input_files[ist->file_index]->ts_offset, &AV_TIME_BASE_Q));
514 }
515
516 456968 msg->pkt = pkt;
517 456968 pkt = NULL;
518
519 456968 fail:
520 456968 av_packet_free(&pkt);
521
522 456968 return ret;
523 }
524
525 216 static void readrate_sleep(Demuxer *d)
526 {
527 216 InputFile *f = &d->f;
528 432 int64_t file_start = copy_ts * (
529
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) +
530
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 216 times.
216 (f->start_time != AV_NOPTS_VALUE ? f->start_time : 0)
531 );
532 216 int64_t burst_until = AV_TIME_BASE * d->readrate_initial_burst;
533
2/2
✓ Branch 0 taken 216 times.
✓ Branch 1 taken 216 times.
432 for (int i = 0; i < f->nb_streams; i++) {
534 216 InputStream *ist = f->streams[i];
535 216 DemuxStream *ds = ds_from_ist(ist);
536 int64_t stream_ts_offset, pts, now;
537
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);
538 216 pts = av_rescale(ds->dts, 1000000, AV_TIME_BASE);
539 216 now = (av_gettime_relative() - d->wallclock_start) * f->readrate + stream_ts_offset;
540
2/2
✓ Branch 0 taken 192 times.
✓ Branch 1 taken 24 times.
216 if (pts - burst_until > now)
541 192 av_usleep(pts - burst_until - now);
542 }
543 216 }
544
545 6668 static void discard_unused_programs(InputFile *ifile)
546 {
547
2/2
✓ Branch 0 taken 41 times.
✓ Branch 1 taken 6668 times.
6709 for (int j = 0; j < ifile->ctx->nb_programs; j++) {
548 41 AVProgram *p = ifile->ctx->programs[j];
549 41 int discard = AVDISCARD_ALL;
550
551
1/2
✓ Branch 0 taken 49 times.
✗ Branch 1 not taken.
49 for (int k = 0; k < p->nb_stream_indexes; k++)
552
2/2
✓ Branch 0 taken 41 times.
✓ Branch 1 taken 8 times.
49 if (!ifile->streams[p->stream_index[k]]->discard) {
553 41 discard = AVDISCARD_DEFAULT;
554 41 break;
555 }
556 41 p->discard = discard;
557 }
558 6668 }
559
560 6668 static void thread_set_name(InputFile *f)
561 {
562 char name[16];
563 6668 snprintf(name, sizeof(name), "dmx%d:%s", f->index, f->ctx->iformat->name);
564 6668 ff_thread_setname(name);
565 6668 }
566
567 6668 static void *input_thread(void *arg)
568 {
569 6668 Demuxer *d = arg;
570 6668 InputFile *f = &d->f;
571 AVPacket *pkt;
572 6668 unsigned flags = d->non_blocking ? AV_THREAD_MESSAGE_NONBLOCK : 0;
573 6668 int ret = 0;
574
575 6668 pkt = av_packet_alloc();
576
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6668 times.
6668 if (!pkt) {
577 ret = AVERROR(ENOMEM);
578 goto finish;
579 }
580
581 6668 thread_set_name(f);
582
583 6668 discard_unused_programs(f);
584
585 6668 d->wallclock_start = av_gettime_relative();
586
587 462056 while (1) {
588 468724 DemuxMsg msg = { NULL };
589
590 468724 ret = av_read_frame(f->ctx, pkt);
591
592
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 468723 times.
468724 if (ret == AVERROR(EAGAIN)) {
593 1 av_usleep(10000);
594 8208 continue;
595 }
596
2/2
✓ Branch 0 taken 3557 times.
✓ Branch 1 taken 465166 times.
468723 if (ret < 0) {
597
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 3548 times.
3557 if (d->loop) {
598 /* signal looping to the consumer thread */
599 9 msg.looping = 1;
600 9 ret = av_thread_message_queue_send(d->in_thread_queue, &msg, 0);
601
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 if (ret >= 0)
602 9 ret = seek_to_start(d);
603
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 if (ret >= 0)
604 9 continue;
605
606 /* fallthrough to the error path */
607 }
608
609
2/2
✓ Branch 0 taken 3490 times.
✓ Branch 1 taken 58 times.
3548 if (ret == AVERROR_EOF)
610 3490 av_log(d, AV_LOG_VERBOSE, "EOF while reading input\n");
611 else
612 58 av_log(d, AV_LOG_ERROR, "Error during demuxing: %s\n",
613 58 av_err2str(ret));
614
615 6668 break;
616 }
617
618
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 465166 times.
465166 if (do_pkt_dump) {
619 av_pkt_dump_log2(NULL, AV_LOG_INFO, pkt, do_hex_dump,
620 f->ctx->streams[pkt->stream_index]);
621 }
622
623 /* the following test is needed in case new streams appear
624 dynamically in stream : we ignore them */
625
1/2
✓ Branch 0 taken 465166 times.
✗ Branch 1 not taken.
465166 if (pkt->stream_index >= f->nb_streams ||
626
2/2
✓ Branch 0 taken 8198 times.
✓ Branch 1 taken 456968 times.
465166 f->streams[pkt->stream_index]->discard) {
627 8198 report_new_stream(d, pkt);
628 8198 av_packet_unref(pkt);
629 8198 continue;
630 }
631
632
2/2
✓ Branch 0 taken 96 times.
✓ Branch 1 taken 456872 times.
456968 if (pkt->flags & AV_PKT_FLAG_CORRUPT) {
633 96 av_log(d, exit_on_error ? AV_LOG_FATAL : AV_LOG_WARNING,
634 "corrupt input packet in stream %d\n",
635
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 96 times.
96 pkt->stream_index);
636
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 96 times.
96 if (exit_on_error) {
637 av_packet_unref(pkt);
638 ret = AVERROR_INVALIDDATA;
639 break;
640 }
641 }
642
643 456968 ret = input_packet_process(d, &msg, pkt);
644
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 456968 times.
456968 if (ret < 0)
645 break;
646
647
2/2
✓ Branch 0 taken 216 times.
✓ Branch 1 taken 456752 times.
456968 if (f->readrate)
648 216 readrate_sleep(d);
649
650 456968 ret = av_thread_message_queue_send(d->in_thread_queue, &msg, flags);
651
4/4
✓ Branch 0 taken 513 times.
✓ Branch 1 taken 456455 times.
✓ Branch 2 taken 57 times.
✓ Branch 3 taken 456 times.
456968 if (flags && ret == AVERROR(EAGAIN)) {
652 57 flags = 0;
653 57 ret = av_thread_message_queue_send(d->in_thread_queue, &msg, flags);
654 57 av_log(f, AV_LOG_WARNING,
655 "Thread message queue blocking; consider raising the "
656 "thread_queue_size option (current value: %d)\n",
657 d->thread_queue_size);
658 }
659
2/2
✓ Branch 0 taken 3120 times.
✓ Branch 1 taken 453848 times.
456968 if (ret < 0) {
660
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3120 times.
3120 if (ret != AVERROR_EOF)
661 av_log(f, AV_LOG_ERROR,
662 "Unable to send packet to main thread: %s\n",
663 av_err2str(ret));
664 3120 av_packet_free(&msg.pkt);
665 3120 break;
666 }
667 }
668
669 6668 finish:
670
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6668 times.
6668 av_assert0(ret < 0);
671 6668 av_thread_message_queue_set_err_recv(d->in_thread_queue, ret);
672
673 6668 av_packet_free(&pkt);
674
675 6668 av_log(d, AV_LOG_VERBOSE, "Terminating demuxer thread\n");
676
677 6668 return NULL;
678 }
679
680 6682 static void thread_stop(Demuxer *d)
681 {
682 6682 InputFile *f = &d->f;
683 DemuxMsg msg;
684
685
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 6668 times.
6682 if (!d->in_thread_queue)
686 14 return;
687 6668 av_thread_message_queue_set_err_send(d->in_thread_queue, AVERROR_EOF);
688
2/2
✓ Branch 1 taken 3720 times.
✓ Branch 2 taken 6668 times.
10388 while (av_thread_message_queue_recv(d->in_thread_queue, &msg, 0) >= 0)
689 3720 av_packet_free(&msg.pkt);
690
691 6668 pthread_join(d->thread, NULL);
692 6668 av_thread_message_queue_free(&d->in_thread_queue);
693 6668 av_thread_message_queue_free(&f->audio_duration_queue);
694 }
695
696 6668 static int thread_start(Demuxer *d)
697 {
698 int ret;
699 6668 InputFile *f = &d->f;
700
701
1/2
✓ Branch 0 taken 6668 times.
✗ Branch 1 not taken.
6668 if (d->thread_queue_size <= 0)
702
2/2
✓ Branch 0 taken 149 times.
✓ Branch 1 taken 6519 times.
6668 d->thread_queue_size = (nb_input_files > 1 ? 8 : 1);
703
704
4/4
✓ Branch 0 taken 149 times.
✓ Branch 1 taken 6519 times.
✓ Branch 2 taken 57 times.
✓ Branch 3 taken 92 times.
6817 if (nb_input_files > 1 &&
705
2/2
✓ Branch 0 taken 85 times.
✓ Branch 1 taken 64 times.
149 (f->ctx->pb ? !f->ctx->pb->seekable :
706 64 strcmp(f->ctx->iformat->name, "lavfi")))
707 57 d->non_blocking = 1;
708 6668 ret = av_thread_message_queue_alloc(&d->in_thread_queue,
709 6668 d->thread_queue_size, sizeof(DemuxMsg));
710
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6668 times.
6668 if (ret < 0)
711 return ret;
712
713
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 6665 times.
6668 if (d->loop) {
714 3 int nb_audio_dec = 0;
715
716
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 3 times.
7 for (int i = 0; i < f->nb_streams; i++) {
717 4 InputStream *ist = f->streams[i];
718
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
6 nb_audio_dec += !!(ist->decoding_needed &&
719
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 ist->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO);
720 }
721
722
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 if (nb_audio_dec) {
723 1 ret = av_thread_message_queue_alloc(&f->audio_duration_queue,
724 nb_audio_dec, sizeof(LastFrameDuration));
725
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
726 goto fail;
727 1 f->audio_duration_queue_size = nb_audio_dec;
728 }
729 }
730
731
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6668 times.
6668 if ((ret = pthread_create(&d->thread, NULL, input_thread, d))) {
732 av_log(d, AV_LOG_ERROR, "pthread_create failed: %s. Try to increase `ulimit -v` or decrease `ulimit -s`.\n", strerror(ret));
733 ret = AVERROR(ret);
734 goto fail;
735 }
736
737 6668 d->read_started = 1;
738
739 6668 return 0;
740 fail:
741 av_thread_message_queue_free(&d->in_thread_queue);
742 return ret;
743 }
744
745 453718 int ifile_get_packet(InputFile *f, AVPacket **pkt)
746 {
747 453718 Demuxer *d = demuxer_from_ifile(f);
748 DemuxMsg msg;
749 int ret;
750
751
2/2
✓ Branch 0 taken 6668 times.
✓ Branch 1 taken 447050 times.
453718 if (!d->in_thread_queue) {
752 6668 ret = thread_start(d);
753
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6668 times.
6668 if (ret < 0)
754 return ret;
755 }
756
757 453718 ret = av_thread_message_queue_recv(d->in_thread_queue, &msg,
758 453718 d->non_blocking ?
759 AV_THREAD_MESSAGE_NONBLOCK : 0);
760
2/2
✓ Branch 0 taken 3581 times.
✓ Branch 1 taken 450137 times.
453718 if (ret < 0)
761 3581 return ret;
762
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 450128 times.
450137 if (msg.looping)
763 9 return 1;
764
765 450128 *pkt = msg.pkt;
766 450128 return 0;
767 }
768
769 6668 static void demux_final_stats(Demuxer *d)
770 {
771 6668 InputFile *f = &d->f;
772 6668 uint64_t total_packets = 0, total_size = 0;
773
774 6668 av_log(f, AV_LOG_VERBOSE, "Input file #%d (%s):\n",
775 6668 f->index, f->ctx->url);
776
777
2/2
✓ Branch 0 taken 7135 times.
✓ Branch 1 taken 6668 times.
13803 for (int j = 0; j < f->nb_streams; j++) {
778 7135 InputStream *ist = f->streams[j];
779 7135 DemuxStream *ds = ds_from_ist(ist);
780 7135 enum AVMediaType type = ist->par->codec_type;
781
782
3/4
✓ Branch 0 taken 6856 times.
✓ Branch 1 taken 279 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6856 times.
7135 if (ist->discard || type == AVMEDIA_TYPE_ATTACHMENT)
783 279 continue;
784
785 6856 total_size += ds->data_size;
786 6856 total_packets += ds->nb_packets;
787
788 6856 av_log(f, AV_LOG_VERBOSE, " Input stream #%d:%d (%s): ",
789 f->index, j, av_get_media_type_string(type));
790 6856 av_log(f, AV_LOG_VERBOSE, "%"PRIu64" packets read (%"PRIu64" bytes); ",
791 ds->nb_packets, ds->data_size);
792
793
2/2
✓ Branch 0 taken 6315 times.
✓ Branch 1 taken 541 times.
6856 if (ist->decoding_needed) {
794 6315 av_log(f, AV_LOG_VERBOSE,
795 "%"PRIu64" frames decoded; %"PRIu64" decode errors",
796 ist->frames_decoded, ist->decode_errors);
797
2/2
✓ Branch 0 taken 1188 times.
✓ Branch 1 taken 5127 times.
6315 if (type == AVMEDIA_TYPE_AUDIO)
798 1188 av_log(f, AV_LOG_VERBOSE, " (%"PRIu64" samples)", ist->samples_decoded);
799 6315 av_log(f, AV_LOG_VERBOSE, "; ");
800 }
801
802 6856 av_log(f, AV_LOG_VERBOSE, "\n");
803 }
804
805 6668 av_log(f, AV_LOG_VERBOSE, " Total: %"PRIu64" packets (%"PRIu64" bytes) demuxed\n",
806 total_packets, total_size);
807 6668 }
808
809 7150 static void ist_free(InputStream **pist)
810 {
811 7150 InputStream *ist = *pist;
812
813
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7150 times.
7150 if (!ist)
814 return;
815
816 7150 dec_free(&ist->decoder);
817
818 7150 av_dict_free(&ist->decoder_opts);
819 7150 av_freep(&ist->filters);
820 7150 av_freep(&ist->outputs);
821 7150 av_freep(&ist->hwaccel_device);
822
823 7150 avcodec_free_context(&ist->dec_ctx);
824 7150 avcodec_parameters_free(&ist->par);
825
826 7150 av_freep(pist);
827 }
828
829 6682 void ifile_close(InputFile **pf)
830 {
831 6682 InputFile *f = *pf;
832 6682 Demuxer *d = demuxer_from_ifile(f);
833
834
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6682 times.
6682 if (!f)
835 return;
836
837 6682 thread_stop(d);
838
839
2/2
✓ Branch 0 taken 6668 times.
✓ Branch 1 taken 14 times.
6682 if (d->read_started)
840 6668 demux_final_stats(d);
841
842
2/2
✓ Branch 0 taken 7150 times.
✓ Branch 1 taken 6682 times.
13832 for (int i = 0; i < f->nb_streams; i++)
843 7150 ist_free(&f->streams[i]);
844 6682 av_freep(&f->streams);
845
846 6682 avformat_close_input(&f->ctx);
847
848 6682 av_freep(pf);
849 }
850
851 6897 static int ist_use(InputStream *ist, int decoding_needed)
852 {
853 6897 DemuxStream *ds = ds_from_ist(ist);
854
855
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6897 times.
6897 if (ist->user_set_discard == AVDISCARD_ALL) {
856 av_log(ist, AV_LOG_ERROR, "Cannot %s a disabled input stream\n",
857 decoding_needed ? "decode" : "streamcopy");
858 return AVERROR(EINVAL);
859 }
860
861 6897 ist->discard = 0;
862 6897 ist->st->discard = ist->user_set_discard;
863 6897 ist->decoding_needed |= decoding_needed;
864 6897 ds->streamcopy_needed |= !decoding_needed;
865
866
4/4
✓ Branch 0 taken 6333 times.
✓ Branch 1 taken 564 times.
✓ Branch 3 taken 6315 times.
✓ Branch 4 taken 18 times.
6897 if (decoding_needed && !avcodec_is_open(ist->dec_ctx)) {
867 6315 int ret = dec_open(ist);
868
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6315 times.
6315 if (ret < 0)
869 return ret;
870 }
871
872 6897 return 0;
873 }
874
875 602 int ist_output_add(InputStream *ist, OutputStream *ost)
876 {
877 int ret;
878
879 602 ret = ist_use(ist, ost->enc ? DECODING_FOR_OST : 0);
880
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 602 times.
602 if (ret < 0)
881 return ret;
882
883 602 ret = GROW_ARRAY(ist->outputs, ist->nb_outputs);
884
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 602 times.
602 if (ret < 0)
885 return ret;
886
887 602 ist->outputs[ist->nb_outputs - 1] = ost;
888
889 602 return 0;
890 }
891
892 6295 int ist_filter_add(InputStream *ist, InputFilter *ifilter, int is_simple)
893 {
894 int ret;
895
896
2/2
✓ Branch 0 taken 6205 times.
✓ Branch 1 taken 90 times.
6295 ret = ist_use(ist, is_simple ? DECODING_FOR_OST : DECODING_FOR_FILTER);
897
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6295 times.
6295 if (ret < 0)
898 return ret;
899
900 6295 ret = GROW_ARRAY(ist->filters, ist->nb_filters);
901
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6295 times.
6295 if (ret < 0)
902 return ret;
903
904 6295 ist->filters[ist->nb_filters - 1] = ifilter;
905
906 // initialize fallback parameters for filtering
907 6295 ret = ifilter_parameters_from_dec(ifilter, ist->dec_ctx);
908
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6295 times.
6295 if (ret < 0)
909 return ret;
910
911 6295 return 0;
912 }
913
914 14176 static int choose_decoder(const OptionsContext *o, AVFormatContext *s, AVStream *st,
915 enum HWAccelID hwaccel_id, enum AVHWDeviceType hwaccel_device_type,
916 const AVCodec **pcodec)
917
918 {
919 14176 char *codec_name = NULL;
920
921
6/20
✓ Branch 1 taken 5404 times.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 10 times.
✓ Branch 5 taken 5414 times.
✓ Branch 6 taken 14176 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 14176 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
19590 MATCH_PER_STREAM_OPT(codec_names, str, codec_name, s, st);
922
2/2
✓ Branch 0 taken 5404 times.
✓ Branch 1 taken 8772 times.
14176 if (codec_name) {
923 5404 int ret = find_codec(NULL, codec_name, st->codecpar->codec_type, 0, pcodec);
924
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5404 times.
5404 if (ret < 0)
925 return ret;
926 5404 st->codecpar->codec_id = (*pcodec)->id;
927
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 5404 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
5404 if (recast_media && st->codecpar->codec_type != (*pcodec)->type)
928 st->codecpar->codec_type = (*pcodec)->type;
929 5404 return 0;
930 } else {
931
3/4
✓ Branch 0 taken 5493 times.
✓ Branch 1 taken 3279 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 5493 times.
8772 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
932 hwaccel_id == HWACCEL_GENERIC &&
933 hwaccel_device_type != AV_HWDEVICE_TYPE_NONE) {
934 const AVCodec *c;
935 void *i = NULL;
936
937 while ((c = av_codec_iterate(&i))) {
938 const AVCodecHWConfig *config;
939
940 if (c->id != st->codecpar->codec_id ||
941 !av_codec_is_decoder(c))
942 continue;
943
944 for (int j = 0; config = avcodec_get_hw_config(c, j); j++) {
945 if (config->device_type == hwaccel_device_type) {
946 av_log(NULL, AV_LOG_VERBOSE, "Selecting decoder '%s' because of requested hwaccel method %s\n",
947 c->name, av_hwdevice_get_type_name(hwaccel_device_type));
948 *pcodec = c;
949 return 0;
950 }
951 }
952 }
953 }
954
955 8772 *pcodec = avcodec_find_decoder(st->codecpar->codec_id);
956 8772 return 0;
957 }
958 }
959
960 1571 static int guess_input_channel_layout(InputStream *ist, int guess_layout_max)
961 {
962 1571 AVCodecContext *dec = ist->dec_ctx;
963
964
2/2
✓ Branch 0 taken 809 times.
✓ Branch 1 taken 762 times.
1571 if (dec->ch_layout.order == AV_CHANNEL_ORDER_UNSPEC) {
965 char layout_name[256];
966
967
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 805 times.
809 if (dec->ch_layout.nb_channels > guess_layout_max)
968 12 return 0;
969 805 av_channel_layout_default(&dec->ch_layout, dec->ch_layout.nb_channels);
970
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 797 times.
805 if (dec->ch_layout.order == AV_CHANNEL_ORDER_UNSPEC)
971 8 return 0;
972 797 av_channel_layout_describe(&dec->ch_layout, layout_name, sizeof(layout_name));
973 797 av_log(ist, AV_LOG_WARNING, "Guessed Channel Layout: %s\n", layout_name);
974 }
975 1559 return 1;
976 }
977
978 5439 static int add_display_matrix_to_stream(const OptionsContext *o,
979 AVFormatContext *ctx, InputStream *ist)
980 {
981 5439 AVStream *st = ist->st;
982 5439 double rotation = DBL_MAX;
983 5439 int hflip = -1, vflip = -1;
984 5439 int hflip_set = 0, vflip_set = 0, rotation_set = 0;
985 int32_t *buf;
986
987
4/20
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 5439 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 5439 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
5440 MATCH_PER_STREAM_OPT(display_rotations, dbl, rotation, ctx, st);
988
2/20
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 5439 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 5439 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
5439 MATCH_PER_STREAM_OPT(display_hflips, i, hflip, ctx, st);
989
2/20
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 5439 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 5439 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
5439 MATCH_PER_STREAM_OPT(display_vflips, i, vflip, ctx, st);
990
991 5439 rotation_set = rotation != DBL_MAX;
992 5439 hflip_set = hflip != -1;
993 5439 vflip_set = vflip != -1;
994
995
4/6
✓ Branch 0 taken 5438 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 5438 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 5438 times.
✗ Branch 5 not taken.
5439 if (!rotation_set && !hflip_set && !vflip_set)
996 5438 return 0;
997
998 1 buf = (int32_t *)av_stream_new_side_data(st, AV_PKT_DATA_DISPLAYMATRIX, sizeof(int32_t) * 9);
999
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!buf) {
1000 av_log(ist, AV_LOG_FATAL, "Failed to generate a display matrix!\n");
1001 return AVERROR(ENOMEM);
1002 }
1003
1004
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 av_display_rotation_set(buf,
1005 rotation_set ? -(rotation) : -0.0f);
1006
1007
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,
1008 hflip_set ? hflip : 0,
1009 vflip_set ? vflip : 0);
1010
1011 1 return 0;
1012 }
1013
1014 1324 static const char *input_stream_item_name(void *obj)
1015 {
1016 1324 const DemuxStream *ds = obj;
1017
1018 1324 return ds->log_name;
1019 }
1020
1021 static const AVClass input_stream_class = {
1022 .class_name = "InputStream",
1023 .version = LIBAVUTIL_VERSION_INT,
1024 .item_name = input_stream_item_name,
1025 .category = AV_CLASS_CATEGORY_DEMUXER,
1026 };
1027
1028 7150 static DemuxStream *demux_stream_alloc(Demuxer *d, AVStream *st)
1029 {
1030 7150 const char *type_str = av_get_media_type_string(st->codecpar->codec_type);
1031 7150 InputFile *f = &d->f;
1032 DemuxStream *ds;
1033
1034 7150 ds = allocate_array_elem(&f->streams, sizeof(*ds), &f->nb_streams);
1035
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7150 times.
7150 if (!ds)
1036 return NULL;
1037
1038 7150 ds->ist.st = st;
1039 7150 ds->ist.file_index = f->index;
1040 7150 ds->ist.index = st->index;
1041 7150 ds->ist.class = &input_stream_class;
1042
1043
1/2
✓ Branch 0 taken 7150 times.
✗ Branch 1 not taken.
7150 snprintf(ds->log_name, sizeof(ds->log_name), "%cist#%d:%d/%s",
1044 7150 type_str ? *type_str : '?', d->f.index, st->index,
1045 7150 avcodec_get_name(st->codecpar->codec_id));
1046
1047 7150 return ds;
1048 }
1049
1050 7150 static int ist_add(const OptionsContext *o, Demuxer *d, AVStream *st)
1051 {
1052 7150 AVFormatContext *ic = d->f.ctx;
1053 7150 AVCodecParameters *par = st->codecpar;
1054 DemuxStream *ds;
1055 InputStream *ist;
1056 7150 char *framerate = NULL, *hwaccel_device = NULL;
1057 7150 const char *hwaccel = NULL;
1058 7150 char *hwaccel_output_format = NULL;
1059 7150 char *codec_tag = NULL;
1060 char *next;
1061 7150 char *discard_str = NULL;
1062 7150 const AVClass *cc = avcodec_get_class();
1063 7150 const AVOption *discard_opt = av_opt_find(&cc, "skip_frame", NULL,
1064 0, AV_OPT_SEARCH_FAKE_OBJ);
1065 int ret;
1066
1067 7150 ds = demux_stream_alloc(d, st);
1068
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7150 times.
7150 if (!ds)
1069 return AVERROR(ENOMEM);
1070
1071 7150 ist = &ds->ist;
1072
1073 7150 ist->discard = 1;
1074 7150 st->discard = AVDISCARD_ALL;
1075 7150 ist->nb_samples = 0;
1076 7150 ds->first_dts = AV_NOPTS_VALUE;
1077 7150 ds->next_dts = AV_NOPTS_VALUE;
1078
1079 7150 ds->min_pts = INT64_MAX;
1080 7150 ds->max_pts = INT64_MIN;
1081
1082 7150 ds->ts_scale = 1.0;
1083
2/20
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 7150 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 7150 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
7150 MATCH_PER_STREAM_OPT(ts_scale, dbl, ds->ts_scale, ic, st);
1084
1085 7150 ist->autorotate = 1;
1086
2/20
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 7150 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 7150 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
7150 MATCH_PER_STREAM_OPT(autorotate, i, ist->autorotate, ic, st);
1087
1088
4/20
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 7150 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 7150 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
7152 MATCH_PER_STREAM_OPT(codec_tags, str, codec_tag, ic, st);
1089
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 7148 times.
7150 if (codec_tag) {
1090 2 uint32_t tag = strtol(codec_tag, &next, 0);
1091
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (*next) {
1092 2 uint8_t buf[4] = { 0 };
1093
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 memcpy(buf, codec_tag, FFMIN(sizeof(buf), strlen(codec_tag)));
1094 2 tag = AV_RL32(buf);
1095 }
1096
1097 2 st->codecpar->codec_tag = tag;
1098 }
1099
1100
2/2
✓ Branch 0 taken 5439 times.
✓ Branch 1 taken 1711 times.
7150 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
1101 5439 ret = add_display_matrix_to_stream(o, ic, ist);
1102
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5439 times.
5439 if (ret < 0)
1103 return ret;
1104
1105
4/20
✓ Branch 1 taken 5153 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 5153 times.
✓ Branch 6 taken 5439 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 5439 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
10592 MATCH_PER_STREAM_OPT(hwaccels, str, hwaccel, ic, st);
1106
2/20
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 5439 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 5439 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
5439 MATCH_PER_STREAM_OPT(hwaccel_output_formats, str,
1107 hwaccel_output_format, ic, st);
1108
1109
4/6
✓ Branch 0 taken 5439 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5153 times.
✓ Branch 3 taken 286 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 5153 times.
5439 if (!hwaccel_output_format && hwaccel && !strcmp(hwaccel, "cuvid")) {
1110 av_log(ist, AV_LOG_WARNING,
1111 "WARNING: defaulting hwaccel_output_format to cuda for compatibility "
1112 "with old commandlines. This behaviour is DEPRECATED and will be removed "
1113 "in the future. Please explicitly set \"-hwaccel_output_format cuda\".\n");
1114 ist->hwaccel_output_format = AV_PIX_FMT_CUDA;
1115
4/6
✓ Branch 0 taken 5439 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5153 times.
✓ Branch 3 taken 286 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 5153 times.
5439 } else if (!hwaccel_output_format && hwaccel && !strcmp(hwaccel, "qsv")) {
1116 av_log(ist, AV_LOG_WARNING,
1117 "WARNING: defaulting hwaccel_output_format to qsv for compatibility "
1118 "with old commandlines. This behaviour is DEPRECATED and will be removed "
1119 "in the future. Please explicitly set \"-hwaccel_output_format qsv\".\n");
1120 ist->hwaccel_output_format = AV_PIX_FMT_QSV;
1121
4/6
✓ Branch 0 taken 5439 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5153 times.
✓ Branch 3 taken 286 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 5153 times.
5439 } else if (!hwaccel_output_format && hwaccel && !strcmp(hwaccel, "mediacodec")) {
1122 // There is no real AVHWFrameContext implementation. Set
1123 // hwaccel_output_format to avoid av_hwframe_transfer_data error.
1124 ist->hwaccel_output_format = AV_PIX_FMT_MEDIACODEC;
1125
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5439 times.
5439 } else if (hwaccel_output_format) {
1126 ist->hwaccel_output_format = av_get_pix_fmt(hwaccel_output_format);
1127 if (ist->hwaccel_output_format == AV_PIX_FMT_NONE) {
1128 av_log(ist, AV_LOG_FATAL, "Unrecognised hwaccel output "
1129 "format: %s", hwaccel_output_format);
1130 }
1131 } else {
1132 5439 ist->hwaccel_output_format = AV_PIX_FMT_NONE;
1133 }
1134
1135
2/2
✓ Branch 0 taken 5153 times.
✓ Branch 1 taken 286 times.
5439 if (hwaccel) {
1136 // The NVDEC hwaccels use a CUDA device, so remap the name here.
1137
2/4
✓ Branch 0 taken 5153 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5153 times.
5153 if (!strcmp(hwaccel, "nvdec") || !strcmp(hwaccel, "cuvid"))
1138 hwaccel = "cuda";
1139
1140
1/2
✓ Branch 0 taken 5153 times.
✗ Branch 1 not taken.
5153 if (!strcmp(hwaccel, "none"))
1141 5153 ist->hwaccel_id = HWACCEL_NONE;
1142 else if (!strcmp(hwaccel, "auto"))
1143 ist->hwaccel_id = HWACCEL_AUTO;
1144 else {
1145 enum AVHWDeviceType type = av_hwdevice_find_type_by_name(hwaccel);
1146 if (type != AV_HWDEVICE_TYPE_NONE) {
1147 ist->hwaccel_id = HWACCEL_GENERIC;
1148 ist->hwaccel_device_type = type;
1149 }
1150
1151 if (!ist->hwaccel_id) {
1152 av_log(ist, AV_LOG_FATAL, "Unrecognized hwaccel: %s.\n",
1153 hwaccel);
1154 av_log(ist, AV_LOG_FATAL, "Supported hwaccels: ");
1155 type = AV_HWDEVICE_TYPE_NONE;
1156 while ((type = av_hwdevice_iterate_types(type)) !=
1157 AV_HWDEVICE_TYPE_NONE)
1158 av_log(ist, AV_LOG_FATAL, "%s ",
1159 av_hwdevice_get_type_name(type));
1160 av_log(ist, AV_LOG_FATAL, "\n");
1161 return AVERROR(EINVAL);
1162 }
1163 }
1164 }
1165
1166
2/20
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 5439 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 5439 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
5439 MATCH_PER_STREAM_OPT(hwaccel_devices, str, hwaccel_device, ic, st);
1167
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5439 times.
5439 if (hwaccel_device) {
1168 ist->hwaccel_device = av_strdup(hwaccel_device);
1169 if (!ist->hwaccel_device)
1170 return AVERROR(ENOMEM);
1171 }
1172 }
1173
1174 7150 ret = choose_decoder(o, ic, st, ist->hwaccel_id, ist->hwaccel_device_type,
1175 &ist->dec);
1176
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7150 times.
7150 if (ret < 0)
1177 return ret;
1178
1179 7150 ret = filter_codec_opts(o->g->codec_opts, ist->st->codecpar->codec_id,
1180 ic, st, ist->dec, &ist->decoder_opts);
1181
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7150 times.
7150 if (ret < 0)
1182 return ret;
1183
1184 7150 ist->reinit_filters = -1;
1185
2/20
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 7150 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 7150 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
7150 MATCH_PER_STREAM_OPT(reinit_filters, i, ist->reinit_filters, ic, st);
1186
1187
2/20
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 7150 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 7150 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
7150 MATCH_PER_STREAM_OPT(discard, str, discard_str, ic, st);
1188 7150 ist->user_set_discard = AVDISCARD_NONE;
1189
1190
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 7150 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
7150 if ((o->video_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) ||
1191
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7149 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
7150 (o->audio_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) ||
1192
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 7149 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
7149 (o->subtitle_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) ||
1193
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 7149 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
7149 (o->data_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_DATA))
1194 1 ist->user_set_discard = AVDISCARD_ALL;
1195
1196
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7150 times.
7150 if (discard_str) {
1197 ret = av_opt_eval_int(&cc, discard_opt, discard_str, &ist->user_set_discard);
1198 if (ret < 0) {
1199 av_log(ist, AV_LOG_ERROR, "Error parsing discard %s.\n", discard_str);
1200 return ret;
1201 }
1202 }
1203
1204 7150 ist->dec_ctx = avcodec_alloc_context3(ist->dec);
1205
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7150 times.
7150 if (!ist->dec_ctx)
1206 return AVERROR(ENOMEM);
1207
1208 7150 ret = avcodec_parameters_to_context(ist->dec_ctx, par);
1209
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7150 times.
7150 if (ret < 0) {
1210 av_log(ist, AV_LOG_ERROR, "Error initializing the decoder context.\n");
1211 return ret;
1212 }
1213
1214
2/2
✓ Branch 0 taken 94 times.
✓ Branch 1 taken 7056 times.
7150 if (o->bitexact)
1215 94 ist->dec_ctx->flags |= AV_CODEC_FLAG_BITEXACT;
1216
1217
4/5
✓ Branch 0 taken 5439 times.
✓ Branch 1 taken 1571 times.
✓ Branch 2 taken 139 times.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
7150 switch (par->codec_type) {
1218 5439 case AVMEDIA_TYPE_VIDEO:
1219
4/20
✓ Branch 1 taken 26 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 26 times.
✓ Branch 6 taken 5439 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 5439 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
5465 MATCH_PER_STREAM_OPT(frame_rates, str, framerate, ic, st);
1220
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 5413 times.
5439 if (framerate) {
1221 26 ret = av_parse_video_rate(&ist->framerate, framerate);
1222
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
26 if (ret < 0) {
1223 av_log(ist, AV_LOG_ERROR, "Error parsing framerate %s.\n",
1224 framerate);
1225 return ret;
1226 }
1227 }
1228
1229 #if FFMPEG_OPT_TOP
1230 5439 ist->top_field_first = -1;
1231
2/20
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 5439 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 5439 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
5439 MATCH_PER_STREAM_OPT(top_field_first, i, ist->top_field_first, ic, st);
1232 #endif
1233
1234 5439 ist->framerate_guessed = av_guess_frame_rate(ic, st, NULL);
1235
1236 5439 break;
1237 1571 case AVMEDIA_TYPE_AUDIO: {
1238 1571 int guess_layout_max = INT_MAX;
1239
4/20
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 4 times.
✓ Branch 6 taken 1571 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 1571 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
1575 MATCH_PER_STREAM_OPT(guess_layout_max, i, guess_layout_max, ic, st);
1240 1571 guess_input_channel_layout(ist, guess_layout_max);
1241 1571 break;
1242 }
1243 139 case AVMEDIA_TYPE_DATA:
1244 case AVMEDIA_TYPE_SUBTITLE: {
1245 139 char *canvas_size = NULL;
1246
4/20
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 139 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 139 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
141 MATCH_PER_STREAM_OPT(fix_sub_duration, i, ist->fix_sub_duration, ic, st);
1247
2/20
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 139 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 139 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
139 MATCH_PER_STREAM_OPT(canvas_sizes, str, canvas_size, ic, st);
1248
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 139 times.
139 if (canvas_size) {
1249 ret = av_parse_video_size(&ist->dec_ctx->width, &ist->dec_ctx->height,
1250 canvas_size);
1251 if (ret < 0) {
1252 av_log(ist, AV_LOG_FATAL, "Invalid canvas size: %s.\n", canvas_size);
1253 return ret;
1254 }
1255 }
1256
1257 /* Compute the size of the canvas for the subtitles stream.
1258 If the subtitles codecpar has set a size, use it. Otherwise use the
1259 maximum dimensions of the video streams in the same file. */
1260 139 ist->sub2video.w = ist->dec_ctx->width;
1261 139 ist->sub2video.h = ist->dec_ctx->height;
1262
3/4
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 120 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 19 times.
139 if (!(ist->sub2video.w && ist->sub2video.h)) {
1263
2/2
✓ Branch 0 taken 339 times.
✓ Branch 1 taken 120 times.
459 for (int j = 0; j < ic->nb_streams; j++) {
1264 339 AVCodecParameters *par1 = ic->streams[j]->codecpar;
1265
2/2
✓ Branch 0 taken 70 times.
✓ Branch 1 taken 269 times.
339 if (par1->codec_type == AVMEDIA_TYPE_VIDEO) {
1266 70 ist->sub2video.w = FFMAX(ist->sub2video.w, par1->width);
1267 70 ist->sub2video.h = FFMAX(ist->sub2video.h, par1->height);
1268 }
1269 }
1270 }
1271
1272
3/4
✓ Branch 0 taken 78 times.
✓ Branch 1 taken 61 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 78 times.
139 if (!(ist->sub2video.w && ist->sub2video.h)) {
1273 61 ist->sub2video.w = FFMAX(ist->sub2video.w, 720);
1274 61 ist->sub2video.h = FFMAX(ist->sub2video.h, 576);
1275 }
1276
1277 139 break;
1278 }
1279 1 case AVMEDIA_TYPE_ATTACHMENT:
1280 case AVMEDIA_TYPE_UNKNOWN:
1281 1 break;
1282 default:
1283 abort();
1284 }
1285
1286 7150 ist->par = avcodec_parameters_alloc();
1287
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7150 times.
7150 if (!ist->par)
1288 return AVERROR(ENOMEM);
1289
1290 7150 ret = avcodec_parameters_from_context(ist->par, ist->dec_ctx);
1291
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7150 times.
7150 if (ret < 0) {
1292 av_log(ist, AV_LOG_ERROR, "Error initializing the decoder context.\n");
1293 return ret;
1294 }
1295
1296 7150 ist->codec_desc = avcodec_descriptor_get(ist->par->codec_id);
1297
1298 7150 return 0;
1299 }
1300
1301 static int dump_attachment(InputStream *ist, const char *filename)
1302 {
1303 AVStream *st = ist->st;
1304 int ret;
1305 AVIOContext *out = NULL;
1306 const AVDictionaryEntry *e;
1307
1308 if (!st->codecpar->extradata_size) {
1309 av_log(ist, AV_LOG_WARNING, "No extradata to dump.\n");
1310 return 0;
1311 }
1312 if (!*filename && (e = av_dict_get(st->metadata, "filename", NULL, 0)))
1313 filename = e->value;
1314 if (!*filename) {
1315 av_log(ist, AV_LOG_FATAL, "No filename specified and no 'filename' tag");
1316 return AVERROR(EINVAL);
1317 }
1318
1319 ret = assert_file_overwrite(filename);
1320 if (ret < 0)
1321 return ret;
1322
1323 if ((ret = avio_open2(&out, filename, AVIO_FLAG_WRITE, &int_cb, NULL)) < 0) {
1324 av_log(ist, AV_LOG_FATAL, "Could not open file %s for writing.\n",
1325 filename);
1326 return ret;
1327 }
1328
1329 avio_write(out, st->codecpar->extradata, st->codecpar->extradata_size);
1330 ret = avio_close(out);
1331
1332 if (ret >= 0)
1333 av_log(ist, AV_LOG_INFO, "Wrote attachment (%d bytes) to '%s'\n",
1334 st->codecpar->extradata_size, filename);
1335
1336 return ret;
1337 }
1338
1339 682 static const char *input_file_item_name(void *obj)
1340 {
1341 682 const Demuxer *d = obj;
1342
1343 682 return d->log_name;
1344 }
1345
1346 static const AVClass input_file_class = {
1347 .class_name = "InputFile",
1348 .version = LIBAVUTIL_VERSION_INT,
1349 .item_name = input_file_item_name,
1350 .category = AV_CLASS_CATEGORY_DEMUXER,
1351 };
1352
1353 6682 static Demuxer *demux_alloc(void)
1354 {
1355 6682 Demuxer *d = allocate_array_elem(&input_files, sizeof(*d), &nb_input_files);
1356
1357
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6682 times.
6682 if (!d)
1358 return NULL;
1359
1360 6682 d->f.class = &input_file_class;
1361 6682 d->f.index = nb_input_files - 1;
1362
1363 6682 snprintf(d->log_name, sizeof(d->log_name), "in#%d", d->f.index);
1364
1365 6682 return d;
1366 }
1367
1368 6682 int ifile_open(const OptionsContext *o, const char *filename)
1369 {
1370 Demuxer *d;
1371 InputFile *f;
1372 AVFormatContext *ic;
1373 6682 const AVInputFormat *file_iformat = NULL;
1374 6682 int err, i, ret = 0;
1375 int64_t timestamp;
1376 6682 AVDictionary *unused_opts = NULL;
1377 6682 const AVDictionaryEntry *e = NULL;
1378 6682 char * video_codec_name = NULL;
1379 6682 char * audio_codec_name = NULL;
1380 6682 char *subtitle_codec_name = NULL;
1381 6682 char * data_codec_name = NULL;
1382 6682 int scan_all_pmts_set = 0;
1383
1384 6682 int64_t start_time = o->start_time;
1385 6682 int64_t start_time_eof = o->start_time_eof;
1386 6682 int64_t stop_time = o->stop_time;
1387 6682 int64_t recording_time = o->recording_time;
1388
1389 6682 d = demux_alloc();
1390
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6682 times.
6682 if (!d)
1391 return AVERROR(ENOMEM);
1392
1393 6682 f = &d->f;
1394
1395
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 6682 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
6682 if (stop_time != INT64_MAX && recording_time != INT64_MAX) {
1396 stop_time = INT64_MAX;
1397 av_log(d, AV_LOG_WARNING, "-t and -to cannot be used together; using -t.\n");
1398 }
1399
1400
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 6682 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
6682 if (stop_time != INT64_MAX && recording_time == INT64_MAX) {
1401 int64_t start = start_time == AV_NOPTS_VALUE ? 0 : start_time;
1402 if (stop_time <= start) {
1403 av_log(d, AV_LOG_ERROR, "-to value smaller than -ss; aborting.\n");
1404 return AVERROR(EINVAL);
1405 } else {
1406 recording_time = stop_time - start;
1407 }
1408 }
1409
1410
2/2
✓ Branch 0 taken 3295 times.
✓ Branch 1 taken 3387 times.
6682 if (o->format) {
1411
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3295 times.
3295 if (!(file_iformat = av_find_input_format(o->format))) {
1412 av_log(d, AV_LOG_FATAL, "Unknown input format: '%s'\n", o->format);
1413 return AVERROR(EINVAL);
1414 }
1415 }
1416
1417
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6682 times.
6682 if (!strcmp(filename, "-"))
1418 filename = "fd:";
1419
1420 20046 stdin_interaction &= strncmp(filename, "pipe:", 5) &&
1421
2/4
✓ Branch 0 taken 6682 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6682 times.
✗ Branch 3 not taken.
13364 strcmp(filename, "fd:") &&
1422
1/2
✓ Branch 0 taken 6682 times.
✗ Branch 1 not taken.
6682 strcmp(filename, "/dev/stdin");
1423
1424 /* get default parameters from command line */
1425 6682 ic = avformat_alloc_context();
1426
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6682 times.
6682 if (!ic)
1427 return AVERROR(ENOMEM);
1428
2/2
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 6616 times.
6682 if (o->nb_audio_sample_rate) {
1429 66 av_dict_set_int(&o->g->format_opts, "sample_rate", o->audio_sample_rate[o->nb_audio_sample_rate - 1].u.i, 0);
1430 }
1431
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 6673 times.
6682 if (o->nb_audio_channels) {
1432 const AVClass *priv_class;
1433
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) &&
1434 9 av_opt_find(&priv_class, "ch_layout", NULL, 0,
1435 AV_OPT_SEARCH_FAKE_OBJ)) {
1436 char buf[32];
1437 9 snprintf(buf, sizeof(buf), "%dC", o->audio_channels[o->nb_audio_channels - 1].u.i);
1438 9 av_dict_set(&o->g->format_opts, "ch_layout", buf, 0);
1439 }
1440 }
1441
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6681 times.
6682 if (o->nb_audio_ch_layouts) {
1442 const AVClass *priv_class;
1443
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) &&
1444 1 av_opt_find(&priv_class, "ch_layout", NULL, 0,
1445 AV_OPT_SEARCH_FAKE_OBJ)) {
1446 1 av_dict_set(&o->g->format_opts, "ch_layout", o->audio_ch_layouts[o->nb_audio_ch_layouts - 1].u.str, 0);
1447 }
1448 }
1449
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 6656 times.
6682 if (o->nb_frame_rates) {
1450 const AVClass *priv_class;
1451 /* set the format-level framerate option;
1452 * this is important for video grabbers, e.g. x11 */
1453
4/6
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 25 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 25 times.
✗ Branch 5 not taken.
51 if (file_iformat && (priv_class = file_iformat->priv_class) &&
1454 25 av_opt_find(&priv_class, "framerate", NULL, 0,
1455 AV_OPT_SEARCH_FAKE_OBJ)) {
1456 25 av_dict_set(&o->g->format_opts, "framerate",
1457 25 o->frame_rates[o->nb_frame_rates - 1].u.str, 0);
1458 }
1459 }
1460
2/2
✓ Branch 0 taken 534 times.
✓ Branch 1 taken 6148 times.
6682 if (o->nb_frame_sizes) {
1461 534 av_dict_set(&o->g->format_opts, "video_size", o->frame_sizes[o->nb_frame_sizes - 1].u.str, 0);
1462 }
1463
2/2
✓ Branch 0 taken 543 times.
✓ Branch 1 taken 6139 times.
6682 if (o->nb_frame_pix_fmts)
1464 543 av_dict_set(&o->g->format_opts, "pixel_format", o->frame_pix_fmts[o->nb_frame_pix_fmts - 1].u.str, 0);
1465
1466
4/4
✓ Branch 0 taken 2667 times.
✓ Branch 1 taken 35 times.
✓ Branch 2 taken 2702 times.
✓ Branch 3 taken 6682 times.
9384 MATCH_PER_TYPE_OPT(codec_names, str, video_codec_name, ic, "v");
1467
4/4
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 2684 times.
✓ Branch 2 taken 2702 times.
✓ Branch 3 taken 6682 times.
9384 MATCH_PER_TYPE_OPT(codec_names, str, audio_codec_name, ic, "a");
1468
3/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2702 times.
✓ Branch 2 taken 2702 times.
✓ Branch 3 taken 6682 times.
9384 MATCH_PER_TYPE_OPT(codec_names, str, subtitle_codec_name, ic, "s");
1469
3/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2702 times.
✓ Branch 2 taken 2702 times.
✓ Branch 3 taken 6682 times.
9384 MATCH_PER_TYPE_OPT(codec_names, str, data_codec_name, ic, "d");
1470
1471
2/2
✓ Branch 0 taken 2667 times.
✓ Branch 1 taken 4015 times.
6682 if (video_codec_name)
1472 2667 ret = err_merge(ret, find_codec(NULL, video_codec_name , AVMEDIA_TYPE_VIDEO , 0,
1473 2667 &ic->video_codec));
1474
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 6664 times.
6682 if (audio_codec_name)
1475 18 ret = err_merge(ret, find_codec(NULL, audio_codec_name , AVMEDIA_TYPE_AUDIO , 0,
1476 18 &ic->audio_codec));
1477
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6682 times.
6682 if (subtitle_codec_name)
1478 ret = err_merge(ret, find_codec(NULL, subtitle_codec_name, AVMEDIA_TYPE_SUBTITLE, 0,
1479 &ic->subtitle_codec));
1480
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6682 times.
6682 if (data_codec_name)
1481 ret = err_merge(ret, find_codec(NULL, data_codec_name , AVMEDIA_TYPE_DATA, 0,
1482 &ic->data_codec));
1483
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6682 times.
6682 if (ret < 0)
1484 return ret;
1485
1486
2/2
✓ Branch 0 taken 2667 times.
✓ Branch 1 taken 4015 times.
6682 ic->video_codec_id = video_codec_name ? ic->video_codec->id : AV_CODEC_ID_NONE;
1487
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 6664 times.
6682 ic->audio_codec_id = audio_codec_name ? ic->audio_codec->id : AV_CODEC_ID_NONE;
1488
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6682 times.
6682 ic->subtitle_codec_id = subtitle_codec_name ? ic->subtitle_codec->id : AV_CODEC_ID_NONE;
1489
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6682 times.
6682 ic->data_codec_id = data_codec_name ? ic->data_codec->id : AV_CODEC_ID_NONE;
1490
1491 6682 ic->flags |= AVFMT_FLAG_NONBLOCK;
1492
2/2
✓ Branch 0 taken 83 times.
✓ Branch 1 taken 6599 times.
6682 if (o->bitexact)
1493 83 ic->flags |= AVFMT_FLAG_BITEXACT;
1494 6682 ic->interrupt_callback = int_cb;
1495
1496
1/2
✓ Branch 1 taken 6682 times.
✗ Branch 2 not taken.
6682 if (!av_dict_get(o->g->format_opts, "scan_all_pmts", NULL, AV_DICT_MATCH_CASE)) {
1497 6682 av_dict_set(&o->g->format_opts, "scan_all_pmts", "1", AV_DICT_DONT_OVERWRITE);
1498 6682 scan_all_pmts_set = 1;
1499 }
1500 /* open the input file with generic avformat function */
1501 6682 err = avformat_open_input(&ic, filename, file_iformat, &o->g->format_opts);
1502
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6682 times.
6682 if (err < 0) {
1503 av_log(d, AV_LOG_ERROR,
1504 "Error opening input: %s\n", av_err2str(err));
1505 if (err == AVERROR_PROTOCOL_NOT_FOUND)
1506 av_log(d, AV_LOG_ERROR, "Did you mean file:%s?\n", filename);
1507 return err;
1508 }
1509
1510 6682 av_strlcat(d->log_name, "/", sizeof(d->log_name));
1511 6682 av_strlcat(d->log_name, ic->iformat->name, sizeof(d->log_name));
1512
1513
1/2
✓ Branch 0 taken 6682 times.
✗ Branch 1 not taken.
6682 if (scan_all_pmts_set)
1514 6682 av_dict_set(&o->g->format_opts, "scan_all_pmts", NULL, AV_DICT_MATCH_CASE);
1515 6682 remove_avoptions(&o->g->format_opts, o->g->codec_opts);
1516
1517 6682 ret = check_avoptions(o->g->format_opts);
1518
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6682 times.
6682 if (ret < 0)
1519 return ret;
1520
1521 /* apply forced codec ids */
1522
2/2
✓ Branch 0 taken 7026 times.
✓ Branch 1 taken 6682 times.
13708 for (i = 0; i < ic->nb_streams; i++) {
1523 const AVCodec *dummy;
1524 7026 ret = choose_decoder(o, ic, ic->streams[i], HWACCEL_NONE, AV_HWDEVICE_TYPE_NONE,
1525 &dummy);
1526
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7026 times.
7026 if (ret < 0)
1527 return ret;
1528 }
1529
1530
2/2
✓ Branch 0 taken 6680 times.
✓ Branch 1 taken 2 times.
6682 if (o->find_stream_info) {
1531 AVDictionary **opts;
1532 6680 int orig_nb_streams = ic->nb_streams;
1533
1534 6680 ret = setup_find_stream_info_opts(ic, o->g->codec_opts, &opts);
1535
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6680 times.
6680 if (ret < 0)
1536 return ret;
1537
1538 /* If not enough info to get the stream parameters, we decode the
1539 first frames to get it. (used in mpeg case for example) */
1540 6680 ret = avformat_find_stream_info(ic, opts);
1541
1542
2/2
✓ Branch 0 taken 7024 times.
✓ Branch 1 taken 6680 times.
13704 for (i = 0; i < orig_nb_streams; i++)
1543 7024 av_dict_free(&opts[i]);
1544 6680 av_freep(&opts);
1545
1546
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6680 times.
6680 if (ret < 0) {
1547 av_log(d, AV_LOG_FATAL, "could not find codec parameters\n");
1548 if (ic->nb_streams == 0) {
1549 avformat_close_input(&ic);
1550 return ret;
1551 }
1552 }
1553 }
1554
1555
3/4
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 6661 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 21 times.
6682 if (start_time != AV_NOPTS_VALUE && start_time_eof != AV_NOPTS_VALUE) {
1556 av_log(d, AV_LOG_WARNING, "Cannot use -ss and -sseof both, using -ss\n");
1557 start_time_eof = AV_NOPTS_VALUE;
1558 }
1559
1560
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6682 times.
6682 if (start_time_eof != AV_NOPTS_VALUE) {
1561 if (start_time_eof >= 0) {
1562 av_log(d, AV_LOG_ERROR, "-sseof value must be negative; aborting\n");
1563 return AVERROR(EINVAL);
1564 }
1565 if (ic->duration > 0) {
1566 start_time = start_time_eof + ic->duration;
1567 if (start_time < 0) {
1568 av_log(d, AV_LOG_WARNING, "-sseof value seeks to before start of file; ignored\n");
1569 start_time = AV_NOPTS_VALUE;
1570 }
1571 } else
1572 av_log(d, AV_LOG_WARNING, "Cannot use -sseof, file duration not known\n");
1573 }
1574
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 6661 times.
6682 timestamp = (start_time == AV_NOPTS_VALUE) ? 0 : start_time;
1575 /* add the stream start time */
1576
4/4
✓ Branch 0 taken 6679 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 5038 times.
✓ Branch 3 taken 1641 times.
6682 if (!o->seek_timestamp && ic->start_time != AV_NOPTS_VALUE)
1577 5038 timestamp += ic->start_time;
1578
1579 /* if seeking requested, we execute it */
1580
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 6661 times.
6682 if (start_time != AV_NOPTS_VALUE) {
1581 21 int64_t seek_timestamp = timestamp;
1582
1583
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
21 if (!(ic->iformat->flags & AVFMT_SEEK_TO_PTS)) {
1584 21 int dts_heuristic = 0;
1585
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 21 times.
42 for (i=0; i<ic->nb_streams; i++) {
1586 21 const AVCodecParameters *par = ic->streams[i]->codecpar;
1587
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (par->video_delay) {
1588 dts_heuristic = 1;
1589 break;
1590 }
1591 }
1592
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (dts_heuristic) {
1593 seek_timestamp -= 3*AV_TIME_BASE / 23;
1594 }
1595 }
1596 21 ret = avformat_seek_file(ic, -1, INT64_MIN, seek_timestamp, seek_timestamp, 0);
1597
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 20 times.
21 if (ret < 0) {
1598 1 av_log(d, AV_LOG_WARNING, "could not seek to position %0.3f\n",
1599 1 (double)timestamp / AV_TIME_BASE);
1600 }
1601 }
1602
1603 6682 f->ctx = ic;
1604 6682 f->start_time = start_time;
1605 6682 f->recording_time = recording_time;
1606 6682 f->input_sync_ref = o->input_sync_ref;
1607 6682 f->input_ts_offset = o->input_ts_offset;
1608
3/6
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 6670 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 12 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
6682 f->ts_offset = o->input_ts_offset - (copy_ts ? (start_at_zero && ic->start_time != AV_NOPTS_VALUE ? ic->start_time : 0) : timestamp);
1609 6682 f->accurate_seek = o->accurate_seek;
1610 6682 d->loop = o->loop;
1611 6682 d->duration = 0;
1612 6682 d->time_base = (AVRational){ 1, 1 };
1613 6682 d->nb_streams_warn = ic->nb_streams;
1614
1615 6682 f->format_nots = !!(ic->iformat->flags & AVFMT_NOTIMESTAMPS);
1616
1617 6682 f->readrate = o->readrate ? o->readrate : 0.0;
1618
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6682 times.
6682 if (f->readrate < 0.0f) {
1619 av_log(d, AV_LOG_ERROR, "Option -readrate is %0.3f; it must be non-negative.\n", f->readrate);
1620 return AVERROR(EINVAL);
1621 }
1622
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6681 times.
6682 if (o->rate_emu) {
1623
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (f->readrate) {
1624 av_log(d, AV_LOG_WARNING, "Both -readrate and -re set. Using -readrate %0.3f.\n", f->readrate);
1625 } else
1626 1 f->readrate = 1.0f;
1627 }
1628
1629
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6681 times.
6682 if (f->readrate) {
1630
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;
1631
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (d->readrate_initial_burst < 0.0) {
1632 av_log(d, AV_LOG_ERROR,
1633 "Option -readrate_initial_burst is %0.3f; it must be non-negative.\n",
1634 d->readrate_initial_burst);
1635 return AVERROR(EINVAL);
1636 }
1637
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6681 times.
6681 } else if (o->readrate_initial_burst) {
1638 av_log(d, AV_LOG_WARNING, "Option -readrate_initial_burst ignored "
1639 "since neither -readrate nor -re were given\n");
1640 }
1641
1642 6682 d->thread_queue_size = o->thread_queue_size;
1643
1644 /* Add all the streams from the given input file to the demuxer */
1645
2/2
✓ Branch 0 taken 7150 times.
✓ Branch 1 taken 6682 times.
13832 for (int i = 0; i < ic->nb_streams; i++) {
1646 7150 ret = ist_add(o, d, ic->streams[i]);
1647
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7150 times.
7150 if (ret < 0)
1648 return ret;
1649 }
1650
1651 /* dump the file content */
1652 6682 av_dump_format(ic, f->index, filename, 0);
1653
1654 /* check if all codec options have been used */
1655 6682 unused_opts = strip_specifiers(o->g->codec_opts);
1656
2/2
✓ Branch 0 taken 7150 times.
✓ Branch 1 taken 6682 times.
13832 for (i = 0; i < f->nb_streams; i++) {
1657 7150 e = NULL;
1658
2/2
✓ Branch 1 taken 22175 times.
✓ Branch 2 taken 7150 times.
29325 while ((e = av_dict_iterate(f->streams[i]->decoder_opts, e)))
1659 22175 av_dict_set(&unused_opts, e->key, NULL, 0);
1660 }
1661
1662 6682 e = NULL;
1663
2/2
✓ Branch 1 taken 402 times.
✓ Branch 2 taken 6682 times.
7084 while ((e = av_dict_iterate(unused_opts, e))) {
1664 402 const AVClass *class = avcodec_get_class();
1665 402 const AVOption *option = av_opt_find(&class, e->key, NULL, 0,
1666 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ);
1667 402 const AVClass *fclass = avformat_get_class();
1668 402 const AVOption *foption = av_opt_find(&fclass, e->key, NULL, 0,
1669 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ);
1670
2/4
✓ Branch 0 taken 402 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 402 times.
402 if (!option || foption)
1671 continue;
1672
1673
1674
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 402 times.
402 if (!(option->flags & AV_OPT_FLAG_DECODING_PARAM)) {
1675 av_log(d, AV_LOG_ERROR, "Codec AVOption %s (%s) is not a decoding "
1676 "option.\n", e->key, option->help ? option->help : "");
1677 return AVERROR(EINVAL);
1678 }
1679
1680 402 av_log(d, AV_LOG_WARNING, "Codec AVOption %s (%s) has not been used "
1681 "for any stream. The most likely reason is either wrong type "
1682 "(e.g. a video option with no video streams) or that it is a "
1683 "private option of some decoder which was not actually used "
1684
1/2
✓ Branch 0 taken 402 times.
✗ Branch 1 not taken.
804 "for any stream.\n", e->key, option->help ? option->help : "");
1685 }
1686 6682 av_dict_free(&unused_opts);
1687
1688
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6682 times.
6682 for (i = 0; i < o->nb_dump_attachment; i++) {
1689 int j;
1690
1691 for (j = 0; j < f->nb_streams; j++) {
1692 InputStream *ist = f->streams[j];
1693
1694 if (check_stream_specifier(ic, ist->st, o->dump_attachment[i].specifier) == 1) {
1695 ret = dump_attachment(ist, o->dump_attachment[i].u.str);
1696 if (ret < 0)
1697 return ret;
1698 }
1699 }
1700 }
1701
1702 6682 return 0;
1703 }
1704