FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/fftools/ffmpeg_demux.c
Date: 2023-06-04 16:45:34
Exec Total Coverage
Lines: 702 905 77.6%
Functions: 33 34 97.1%
Branches: 476 988 48.2%

Line Branch Exec Source
1 /*
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include <float.h>
20 #include <stdint.h>
21
22 #include "ffmpeg.h"
23
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 1322489 static DemuxStream *ds_from_ist(InputStream *ist)
126 {
127 1322489 return (DemuxStream*)ist;
128 }
129
130 453097 static Demuxer *demuxer_from_ifile(InputFile *f)
131 {
132 453097 return (Demuxer*)f;
133 }
134
135 50 InputStream *ist_find_unused(enum AVMediaType type)
136 {
137
1/2
✓ Branch 2 taken 69 times.
✗ Branch 3 not taken.
69 for (InputStream *ist = ist_iter(NULL); ist; ist = ist_iter(ist)) {
138
4/4
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 50 times.
✓ Branch 3 taken 18 times.
69 if (ist->par->codec_type == type && ist->discard &&
139
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 ist->user_set_discard != AVDISCARD_ALL)
140 50 return ist;
141 }
142 return NULL;
143 }
144
145 8197 static void report_new_stream(Demuxer *d, const AVPacket *pkt)
146 {
147 8197 AVStream *st = d->f.ctx->streams[pkt->stream_index];
148
149
1/2
✓ Branch 0 taken 8197 times.
✗ Branch 1 not taken.
8197 if (pkt->stream_index < d->nb_streams_warn)
150 8197 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 6 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 6 times.
✗ Branch 1 not taken.
6 if (ds->max_pts > ds->min_pts &&
164
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 ds->max_pts - (uint64_t)ds->min_pts < INT64_MAX - last_duration)
165 6 last_duration += ds->max_pts - ds->min_pts;
166
167
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
10 if (!d->duration ||
168 4 av_compare_ts(d->duration, d->time_base,
169 4 last_duration, ds->ist.st->time_base) < 0) {
170 6 d->duration = last_duration;
171 6 d->time_base = ds->ist.st->time_base;
172 }
173 6 }
174
175 6 static int seek_to_start(Demuxer *d)
176 {
177 6 InputFile *ifile = &d->f;
178 6 AVFormatContext *is = ifile->ctx;
179 int ret;
180
181 6 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 6 times.
6 if (ret < 0)
183 return ret;
184
185
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 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 int got_durations = 0;
190
191 while (got_durations < ifile->audio_duration_queue_size) {
192 DemuxStream *ds;
193 LastFrameDuration dur;
194 ret = av_thread_message_queue_recv(ifile->audio_duration_queue, &dur, 0);
195 if (ret < 0)
196 return ret;
197 got_durations++;
198
199 ds = ds_from_ist(ifile->streams[dur.stream_idx]);
200 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 6 times.
✗ Branch 1 not taken.
6 if (d->loop > 0)
221 6 d->loop--;
222
223 6 return ret;
224 }
225
226 408773 static void ts_discontinuity_detect(Demuxer *d, InputStream *ist,
227 AVPacket *pkt)
228 {
229 408773 InputFile *ifile = &d->f;
230 408773 DemuxStream *ds = ds_from_ist(ist);
231 408773 const int fmt_is_discont = ifile->ctx->iformat->flags & AVFMT_TS_DISCONT;
232 408773 int disable_discontinuity_correction = copy_ts;
233 408773 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 408593 times.
✓ Branch 2 taken 175 times.
✓ Branch 3 taken 5 times.
✓ Branch 4 taken 75 times.
✓ Branch 5 taken 100 times.
408773 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 402567 times.
✓ Branch 1 taken 6206 times.
✓ Branch 2 taken 402392 times.
✓ Branch 3 taken 175 times.
811165 if (ds->next_dts != AV_NOPTS_VALUE && !disable_discontinuity_correction) {
246 402392 int64_t delta = pkt_dts - ds->next_dts;
247
2/2
✓ Branch 0 taken 22594 times.
✓ Branch 1 taken 379798 times.
402392 if (fmt_is_discont) {
248
4/4
✓ Branch 0 taken 22554 times.
✓ Branch 1 taken 40 times.
✓ Branch 2 taken 22593 times.
✓ Branch 3 taken 1 times.
22594 if (FFABS(delta) > 1LL * dts_delta_threshold * AV_TIME_BASE ||
249
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 22585 times.
22593 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 376177 times.
✓ Branch 1 taken 3621 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 379798 times.
379798 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 377754 times.
✓ Branch 1 taken 2044 times.
379798 if (pkt->pts != AV_NOPTS_VALUE){
267 377754 int64_t pkt_pts = av_rescale_q(pkt->pts, pkt->time_base, AV_TIME_BASE_Q);
268 377754 delta = pkt_pts - ds->next_dts;
269
3/4
✓ Branch 0 taken 374450 times.
✓ Branch 1 taken 3304 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 377754 times.
377754 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 6206 times.
✓ Branch 1 taken 175 times.
✓ Branch 2 taken 6201 times.
✓ Branch 3 taken 5 times.
✓ Branch 4 taken 113 times.
✓ Branch 5 taken 6088 times.
6381 } else if (ds->next_dts == AV_NOPTS_VALUE && !copy_ts &&
278
1/2
✓ Branch 0 taken 113 times.
✗ Branch 1 not taken.
113 fmt_is_discont && d->last_ts != AV_NOPTS_VALUE) {
279 113 int64_t delta = pkt_dts - d->last_ts;
280
3/4
✓ Branch 0 taken 53 times.
✓ Branch 1 taken 60 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 113 times.
113 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 408773 d->last_ts = av_rescale_q(pkt->dts, pkt->time_base, AV_TIME_BASE_Q);
292 408773 }
293
294 449833 static void ts_discontinuity_process(Demuxer *d, InputStream *ist,
295 AVPacket *pkt)
296 {
297 449833 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 411357 times.
✓ Branch 1 taken 38476 times.
449833 if (pkt->dts != AV_NOPTS_VALUE)
303 411357 pkt->dts += offset;
304
2/2
✓ Branch 0 taken 409265 times.
✓ Branch 1 taken 40568 times.
449833 if (pkt->pts != AV_NOPTS_VALUE)
305 409265 pkt->pts += offset;
306
307 // detect timestamp discontinuities for audio/video
308
2/2
✓ Branch 0 taken 321935 times.
✓ Branch 1 taken 127898 times.
449833 if ((ist->par->codec_type == AVMEDIA_TYPE_VIDEO ||
309
2/2
✓ Branch 0 taken 319347 times.
✓ Branch 1 taken 2588 times.
321935 ist->par->codec_type == AVMEDIA_TYPE_AUDIO) &&
310
2/2
✓ Branch 0 taken 408773 times.
✓ Branch 1 taken 38472 times.
447245 pkt->dts != AV_NOPTS_VALUE)
311 408773 ts_discontinuity_detect(d, ist, pkt);
312 449833 }
313
314 449833 static int ist_dts_update(DemuxStream *ds, AVPacket *pkt)
315 {
316 449833 InputStream *ist = &ds->ist;
317 449833 const AVCodecParameters *par = ist->par;
318
319
2/2
✓ Branch 0 taken 6740 times.
✓ Branch 1 taken 443093 times.
449833 if (!ds->saw_first_ts) {
320 6740 ds->first_dts =
321
2/2
✓ Branch 0 taken 4639 times.
✓ Branch 1 taken 2101 times.
6740 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 6223 times.
✓ Branch 1 taken 517 times.
6740 if (pkt->pts != AV_NOPTS_VALUE) {
323 6223 ds->first_dts =
324 6223 ds->dts += av_rescale_q(pkt->pts, pkt->time_base, AV_TIME_BASE_Q);
325 }
326 6740 ds->saw_first_ts = 1;
327 }
328
329
2/2
✓ Branch 0 taken 6740 times.
✓ Branch 1 taken 443093 times.
449833 if (ds->next_dts == AV_NOPTS_VALUE)
330 6740 ds->next_dts = ds->dts;
331
332
2/2
✓ Branch 0 taken 411357 times.
✓ Branch 1 taken 38476 times.
449833 if (pkt->dts != AV_NOPTS_VALUE)
333 411357 ds->next_dts = ds->dts = av_rescale_q(pkt->dts, pkt->time_base, AV_TIME_BASE_Q);
334
335 449833 ds->dts = ds->next_dts;
336
3/3
✓ Branch 0 taken 319347 times.
✓ Branch 1 taken 127898 times.
✓ Branch 2 taken 2588 times.
449833 switch (par->codec_type) {
337 319347 case AVMEDIA_TYPE_AUDIO:
338 av_assert1(pkt->duration >= 0);
339
1/2
✓ Branch 0 taken 319347 times.
✗ Branch 1 not taken.
319347 if (par->sample_rate) {
340 319347 ds->next_dts += ((int64_t)AV_TIME_BASE * par->frame_size) /
341 319347 par->sample_rate;
342 } else {
343 ds->next_dts += av_rescale_q(pkt->duration, pkt->time_base, AV_TIME_BASE_Q);
344 }
345 319347 break;
346 127898 case AVMEDIA_TYPE_VIDEO:
347
2/2
✓ Branch 0 taken 237 times.
✓ Branch 1 taken 127661 times.
127898 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 125544 times.
✓ Branch 1 taken 2117 times.
127661 } else if (pkt->duration) {
353 125544 ds->next_dts += av_rescale_q(pkt->duration, pkt->time_base, AV_TIME_BASE_Q);
354
2/2
✓ Branch 0 taken 1566 times.
✓ Branch 1 taken 551 times.
2117 } else if (ist->par->framerate.num != 0) {
355 1566 AVRational field_rate = av_mul_q(ist->par->framerate,
356 1566 (AVRational){ 2, 1 });
357 1566 int fields = 2;
358
359
1/2
✓ Branch 0 taken 1566 times.
✗ Branch 1 not taken.
1566 if (ist->codec_desc &&
360
4/4
✓ Branch 0 taken 181 times.
✓ Branch 1 taken 1385 times.
✓ Branch 2 taken 171 times.
✓ Branch 3 taken 10 times.
1747 (ist->codec_desc->props & AV_CODEC_PROP_FIELDS) &&
361 181 av_stream_get_parser(ist->st))
362 171 fields = 1 + av_stream_get_parser(ist->st)->repeat_pict;
363
364 1566 ds->next_dts += av_rescale_q(fields, av_inv_q(field_rate), AV_TIME_BASE_Q);
365 }
366 127898 break;
367 }
368
369
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 449833 times.
449833 av_assert0(!pkt->opaque_ref);
370
2/2
✓ Branch 0 taken 67379 times.
✓ Branch 1 taken 382454 times.
449833 if (ds->streamcopy_needed) {
371 DemuxPktData *pd;
372
373 67379 pkt->opaque_ref = av_buffer_allocz(sizeof(*pd));
374
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 67379 times.
67379 if (!pkt->opaque_ref)
375 return AVERROR(ENOMEM);
376 67379 pd = (DemuxPktData*)pkt->opaque_ref->data;
377
378 67379 pd->dts_est = ds->dts;
379 }
380
381 449833 return 0;
382 }
383
384 449833 static int ts_fixup(Demuxer *d, AVPacket *pkt)
385 {
386 449833 InputFile *ifile = &d->f;
387 449833 InputStream *ist = ifile->streams[pkt->stream_index];
388 449833 DemuxStream *ds = ds_from_ist(ist);
389 449833 const int64_t start_time = ifile->start_time_effective;
390 int64_t duration;
391 int ret;
392
393 449833 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 449833 times.
449833 SHOW_TS_DEBUG("demuxer");
407
408
4/4
✓ Branch 0 taken 400675 times.
✓ Branch 1 taken 49158 times.
✓ Branch 2 taken 267629 times.
✓ Branch 3 taken 133046 times.
449833 if (!ds->wrap_correction_done && start_time != AV_NOPTS_VALUE &&
409
2/2
✓ Branch 0 taken 314 times.
✓ Branch 1 taken 267315 times.
267629 ist->st->pts_wrap_bits < 64) {
410 int64_t stime, stime2;
411
412 314 stime = av_rescale_q(start_time, AV_TIME_BASE_Q, pkt->time_base);
413 314 stime2= stime + (1ULL<<ist->st->pts_wrap_bits);
414 314 ds->wrap_correction_done = 1;
415
416
5/6
✓ Branch 0 taken 285 times.
✓ Branch 1 taken 29 times.
✓ Branch 2 taken 251 times.
✓ Branch 3 taken 34 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 251 times.
314 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 285 times.
✓ Branch 1 taken 29 times.
✓ Branch 2 taken 245 times.
✓ Branch 3 taken 40 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 245 times.
314 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 411357 times.
✓ Branch 1 taken 38476 times.
449833 if (pkt->dts != AV_NOPTS_VALUE)
427 411357 pkt->dts += av_rescale_q(ifile->ts_offset, AV_TIME_BASE_Q, pkt->time_base);
428
2/2
✓ Branch 0 taken 409265 times.
✓ Branch 1 taken 40568 times.
449833 if (pkt->pts != AV_NOPTS_VALUE)
429 409265 pkt->pts += av_rescale_q(ifile->ts_offset, AV_TIME_BASE_Q, pkt->time_base);
430
431
2/2
✓ Branch 0 taken 409265 times.
✓ Branch 1 taken 40568 times.
449833 if (pkt->pts != AV_NOPTS_VALUE)
432 409265 pkt->pts *= ds->ts_scale;
433
2/2
✓ Branch 0 taken 411357 times.
✓ Branch 1 taken 38476 times.
449833 if (pkt->dts != AV_NOPTS_VALUE)
434 411357 pkt->dts *= ds->ts_scale;
435
436 449833 duration = av_rescale_q(d->duration, d->time_base, pkt->time_base);
437
2/2
✓ Branch 0 taken 409265 times.
✓ Branch 1 taken 40568 times.
449833 if (pkt->pts != AV_NOPTS_VALUE) {
438 409265 pkt->pts += duration;
439 409265 ds->max_pts = FFMAX(pkt->pts, ds->max_pts);
440 409265 ds->min_pts = FFMIN(pkt->pts, ds->min_pts);
441 }
442
443
2/2
✓ Branch 0 taken 411357 times.
✓ Branch 1 taken 38476 times.
449833 if (pkt->dts != AV_NOPTS_VALUE)
444 411357 pkt->dts += duration;
445
446
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 449833 times.
449833 SHOW_TS_DEBUG("demuxer+tsfixup");
447
448 // detect and try to correct for timestamp discontinuities
449 449833 ts_discontinuity_process(d, ist, pkt);
450
451 // update estimated/predicted dts
452 449833 ret = ist_dts_update(ds, pkt);
453
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 449833 times.
449833 if (ret < 0)
454 return ret;
455
456 449833 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 449833 static int input_packet_process(Demuxer *d, DemuxMsg *msg, AVPacket *src)
462 {
463 449833 InputFile *f = &d->f;
464 449833 InputStream *ist = f->streams[src->stream_index];
465 449833 DemuxStream *ds = ds_from_ist(ist);
466 AVPacket *pkt;
467 449833 int ret = 0;
468
469 449833 pkt = av_packet_alloc();
470
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 449833 times.
449833 if (!pkt) {
471 av_packet_unref(src);
472 return AVERROR(ENOMEM);
473 }
474 449833 av_packet_move_ref(pkt, src);
475
476 449833 ret = ts_fixup(d, pkt);
477
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 449833 times.
449833 if (ret < 0)
478 goto fail;
479
480 449833 ds->data_size += pkt->size;
481 449833 ds->nb_packets++;
482
483 /* add the stream-global side data to the first packet */
484
2/2
✓ Branch 0 taken 6740 times.
✓ Branch 1 taken 443093 times.
449833 if (ds->nb_packets == 1) {
485
2/2
✓ Branch 0 taken 322 times.
✓ Branch 1 taken 6740 times.
7062 for (int i = 0; i < ist->st->nb_side_data; i++) {
486 322 AVPacketSideData *src_sd = &ist->st->side_data[i];
487 uint8_t *dst_data;
488
489
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 319 times.
322 if (src_sd->type == AV_PKT_DATA_DISPLAYMATRIX)
490 3 continue;
491
492
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 319 times.
319 if (av_packet_get_side_data(pkt, src_sd->type, NULL))
493 continue;
494
495 319 dst_data = av_packet_new_side_data(pkt, src_sd->type, src_sd->size);
496
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 319 times.
319 if (!dst_data) {
497 ret = AVERROR(ENOMEM);
498 goto fail;
499 }
500
501 319 memcpy(dst_data, src_sd->data, src_sd->size);
502 }
503 }
504
505
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 449833 times.
449833 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 449833 msg->pkt = pkt;
517 449833 pkt = NULL;
518
519 449833 fail:
520 449833 av_packet_free(&pkt);
521
522 449833 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 189 times.
✓ Branch 1 taken 27 times.
216 if (pts - burst_until > now)
541 189 av_usleep(pts - burst_until - now);
542 }
543 216 }
544
545 6584 static void discard_unused_programs(InputFile *ifile)
546 {
547
2/2
✓ Branch 0 taken 41 times.
✓ Branch 1 taken 6584 times.
6625 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 6584 }
559
560 6584 static void thread_set_name(InputFile *f)
561 {
562 char name[16];
563 6584 snprintf(name, sizeof(name), "dmx%d:%s", f->index, f->ctx->iformat->name);
564 6584 ff_thread_setname(name);
565 6584 }
566
567 6584 static void *input_thread(void *arg)
568 {
569 6584 Demuxer *d = arg;
570 6584 InputFile *f = &d->f;
571 AVPacket *pkt;
572 6584 unsigned flags = d->non_blocking ? AV_THREAD_MESSAGE_NONBLOCK : 0;
573 6584 int ret = 0;
574
575 6584 pkt = av_packet_alloc();
576
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6584 times.
6584 if (!pkt) {
577 ret = AVERROR(ENOMEM);
578 goto finish;
579 }
580
581 6584 thread_set_name(f);
582
583 6584 discard_unused_programs(f);
584
585 6584 d->wallclock_start = av_gettime_relative();
586
587 454919 while (1) {
588 461503 DemuxMsg msg = { NULL };
589
590 461503 ret = av_read_frame(f->ctx, pkt);
591
592
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 461502 times.
461503 if (ret == AVERROR(EAGAIN)) {
593 1 av_usleep(10000);
594 8204 continue;
595 }
596
2/2
✓ Branch 0 taken 3472 times.
✓ Branch 1 taken 458030 times.
461502 if (ret < 0) {
597
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 3466 times.
3472 if (d->loop) {
598 /* signal looping to the consumer thread */
599 6 msg.looping = 1;
600 6 ret = av_thread_message_queue_send(d->in_thread_queue, &msg, 0);
601
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (ret >= 0)
602 6 ret = seek_to_start(d);
603
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (ret >= 0)
604 6 continue;
605
606 /* fallthrough to the error path */
607 }
608
609
2/2
✓ Branch 0 taken 3408 times.
✓ Branch 1 taken 58 times.
3466 if (ret == AVERROR_EOF)
610 3408 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 6584 break;
616 }
617
618
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 458030 times.
458030 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 458030 times.
✗ Branch 1 not taken.
458030 if (pkt->stream_index >= f->nb_streams ||
626
2/2
✓ Branch 0 taken 8197 times.
✓ Branch 1 taken 449833 times.
458030 f->streams[pkt->stream_index]->discard) {
627 8197 report_new_stream(d, pkt);
628 8197 av_packet_unref(pkt);
629 8197 continue;
630 }
631
632
2/2
✓ Branch 0 taken 96 times.
✓ Branch 1 taken 449737 times.
449833 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 449833 ret = input_packet_process(d, &msg, pkt);
644
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 449833 times.
449833 if (ret < 0)
645 break;
646
647
2/2
✓ Branch 0 taken 216 times.
✓ Branch 1 taken 449617 times.
449833 if (f->readrate)
648 216 readrate_sleep(d);
649
650 449833 ret = av_thread_message_queue_send(d->in_thread_queue, &msg, flags);
651
4/4
✓ Branch 0 taken 513 times.
✓ Branch 1 taken 449320 times.
✓ Branch 2 taken 57 times.
✓ Branch 3 taken 456 times.
449833 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 3118 times.
✓ Branch 1 taken 446715 times.
449833 if (ret < 0) {
660
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3118 times.
3118 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 3118 av_packet_free(&msg.pkt);
665 3118 break;
666 }
667 }
668
669 6584 finish:
670
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6584 times.
6584 av_assert0(ret < 0);
671 6584 av_thread_message_queue_set_err_recv(d->in_thread_queue, ret);
672
673 6584 av_packet_free(&pkt);
674
675 6584 av_log(d, AV_LOG_VERBOSE, "Terminating demuxer thread\n");
676
677 6584 return NULL;
678 }
679
680 6598 static void thread_stop(Demuxer *d)
681 {
682 6598 InputFile *f = &d->f;
683 DemuxMsg msg;
684
685
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 6584 times.
6598 if (!d->in_thread_queue)
686 14 return;
687 6584 av_thread_message_queue_set_err_send(d->in_thread_queue, AVERROR_EOF);
688
2/2
✓ Branch 1 taken 3717 times.
✓ Branch 2 taken 6584 times.
10301 while (av_thread_message_queue_recv(d->in_thread_queue, &msg, 0) >= 0)
689 3717 av_packet_free(&msg.pkt);
690
691 6584 pthread_join(d->thread, NULL);
692 6584 av_thread_message_queue_free(&d->in_thread_queue);
693 6584 av_thread_message_queue_free(&f->audio_duration_queue);
694 }
695
696 6584 static int thread_start(Demuxer *d)
697 {
698 int ret;
699 6584 InputFile *f = &d->f;
700
701
1/2
✓ Branch 0 taken 6584 times.
✗ Branch 1 not taken.
6584 if (d->thread_queue_size <= 0)
702
2/2
✓ Branch 0 taken 149 times.
✓ Branch 1 taken 6435 times.
6584 d->thread_queue_size = (nb_input_files > 1 ? 8 : 1);
703
704
4/4
✓ Branch 0 taken 149 times.
✓ Branch 1 taken 6435 times.
✓ Branch 2 taken 57 times.
✓ Branch 3 taken 92 times.
6733 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 6584 ret = av_thread_message_queue_alloc(&d->in_thread_queue,
709 6584 d->thread_queue_size, sizeof(DemuxMsg));
710
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6584 times.
6584 if (ret < 0)
711 return ret;
712
713
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6582 times.
6584 if (d->loop) {
714 2 int nb_audio_dec = 0;
715
716
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 for (int i = 0; i < f->nb_streams; i++) {
717 2 InputStream *ist = f->streams[i];
718
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 nb_audio_dec += !!(ist->decoding_needed &&
719 ist->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO);
720 }
721
722
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (nb_audio_dec) {
723 ret = av_thread_message_queue_alloc(&f->audio_duration_queue,
724 nb_audio_dec, sizeof(LastFrameDuration));
725 if (ret < 0)
726 goto fail;
727 f->audio_duration_queue_size = nb_audio_dec;
728 }
729 }
730
731
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6584 times.
6584 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 6584 d->read_started = 1;
738
739 6584 return 0;
740 fail:
741 av_thread_message_queue_free(&d->in_thread_queue);
742 return ret;
743 }
744
745 446499 int ifile_get_packet(InputFile *f, AVPacket **pkt)
746 {
747 446499 Demuxer *d = demuxer_from_ifile(f);
748 DemuxMsg msg;
749 int ret;
750
751
2/2
✓ Branch 0 taken 6584 times.
✓ Branch 1 taken 439915 times.
446499 if (!d->in_thread_queue) {
752 6584 ret = thread_start(d);
753
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6584 times.
6584 if (ret < 0)
754 return ret;
755 }
756
757 446499 ret = av_thread_message_queue_recv(d->in_thread_queue, &msg,
758 446499 d->non_blocking ?
759 AV_THREAD_MESSAGE_NONBLOCK : 0);
760
2/2
✓ Branch 0 taken 3495 times.
✓ Branch 1 taken 443004 times.
446499 if (ret < 0)
761 3495 return ret;
762
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 442998 times.
443004 if (msg.looping)
763 6 return 1;
764
765 442998 *pkt = msg.pkt;
766 442998 return 0;
767 }
768
769 6584 static void demux_final_stats(Demuxer *d)
770 {
771 6584 InputFile *f = &d->f;
772 6584 uint64_t total_packets = 0, total_size = 0;
773
774 6584 av_log(f, AV_LOG_VERBOSE, "Input file #%d (%s):\n",
775 6584 f->index, f->ctx->url);
776
777
2/2
✓ Branch 0 taken 7032 times.
✓ Branch 1 taken 6584 times.
13616 for (int j = 0; j < f->nb_streams; j++) {
778 7032 InputStream *ist = f->streams[j];
779 7032 DemuxStream *ds = ds_from_ist(ist);
780 7032 enum AVMediaType type = ist->par->codec_type;
781
782
3/4
✓ Branch 0 taken 6761 times.
✓ Branch 1 taken 271 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6761 times.
7032 if (ist->discard || type == AVMEDIA_TYPE_ATTACHMENT)
783 271 continue;
784
785 6761 total_size += ds->data_size;
786 6761 total_packets += ds->nb_packets;
787
788 6761 av_log(f, AV_LOG_VERBOSE, " Input stream #%d:%d (%s): ",
789 f->index, j, av_get_media_type_string(type));
790 6761 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 6283 times.
✓ Branch 1 taken 478 times.
6761 if (ist->decoding_needed) {
794 6283 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 1182 times.
✓ Branch 1 taken 5101 times.
6283 if (type == AVMEDIA_TYPE_AUDIO)
798 1182 av_log(f, AV_LOG_VERBOSE, " (%"PRIu64" samples)", ist->samples_decoded);
799 6283 av_log(f, AV_LOG_VERBOSE, "; ");
800 }
801
802 6761 av_log(f, AV_LOG_VERBOSE, "\n");
803 }
804
805 6584 av_log(f, AV_LOG_VERBOSE, " Total: %"PRIu64" packets (%"PRIu64" bytes) demuxed\n",
806 total_packets, total_size);
807 6584 }
808
809 7047 static void ist_free(InputStream **pist)
810 {
811 7047 InputStream *ist = *pist;
812
813
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7047 times.
7047 if (!ist)
814 return;
815
816 7047 dec_free(&ist->decoder);
817
818 7047 av_dict_free(&ist->decoder_opts);
819 7047 avsubtitle_free(&ist->prev_sub.subtitle);
820 7047 av_freep(&ist->filters);
821 7047 av_freep(&ist->outputs);
822 7047 av_freep(&ist->hwaccel_device);
823
824 7047 avcodec_free_context(&ist->dec_ctx);
825 7047 avcodec_parameters_free(&ist->par);
826
827 7047 av_freep(pist);
828 }
829
830 6598 void ifile_close(InputFile **pf)
831 {
832 6598 InputFile *f = *pf;
833 6598 Demuxer *d = demuxer_from_ifile(f);
834
835
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6598 times.
6598 if (!f)
836 return;
837
838 6598 thread_stop(d);
839
840
2/2
✓ Branch 0 taken 6584 times.
✓ Branch 1 taken 14 times.
6598 if (d->read_started)
841 6584 demux_final_stats(d);
842
843
2/2
✓ Branch 0 taken 7047 times.
✓ Branch 1 taken 6598 times.
13645 for (int i = 0; i < f->nb_streams; i++)
844 7047 ist_free(&f->streams[i]);
845 6598 av_freep(&f->streams);
846
847 6598 avformat_close_input(&f->ctx);
848
849 6598 av_freep(pf);
850 }
851
852 6796 static int ist_use(InputStream *ist, int decoding_needed)
853 {
854 6796 DemuxStream *ds = ds_from_ist(ist);
855
856
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6796 times.
6796 if (ist->user_set_discard == AVDISCARD_ALL) {
857 av_log(ist, AV_LOG_ERROR, "Cannot %s a disabled input stream\n",
858 decoding_needed ? "decode" : "streamcopy");
859 return AVERROR(EINVAL);
860 }
861
862 6796 ist->discard = 0;
863 6796 ist->st->discard = ist->user_set_discard;
864 6796 ist->decoding_needed |= decoding_needed;
865 6796 ds->streamcopy_needed |= !decoding_needed;
866
867
4/4
✓ Branch 0 taken 6301 times.
✓ Branch 1 taken 495 times.
✓ Branch 3 taken 6283 times.
✓ Branch 4 taken 18 times.
6796 if (decoding_needed && !avcodec_is_open(ist->dec_ctx)) {
868 6283 int ret = dec_open(ist);
869
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6283 times.
6283 if (ret < 0)
870 return ret;
871 }
872
873 6796 return 0;
874 }
875
876 533 int ist_output_add(InputStream *ist, OutputStream *ost)
877 {
878 int ret;
879
880 533 ret = ist_use(ist, ost->enc ? DECODING_FOR_OST : 0);
881
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 533 times.
533 if (ret < 0)
882 return ret;
883
884 533 GROW_ARRAY(ist->outputs, ist->nb_outputs);
885 533 ist->outputs[ist->nb_outputs - 1] = ost;
886
887 533 return 0;
888 }
889
890 6263 int ist_filter_add(InputStream *ist, InputFilter *ifilter, int is_simple)
891 {
892 int ret;
893
894
2/2
✓ Branch 0 taken 6174 times.
✓ Branch 1 taken 89 times.
6263 ret = ist_use(ist, is_simple ? DECODING_FOR_OST : DECODING_FOR_FILTER);
895
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6263 times.
6263 if (ret < 0)
896 return ret;
897
898 6263 GROW_ARRAY(ist->filters, ist->nb_filters);
899 6263 ist->filters[ist->nb_filters - 1] = ifilter;
900
901 // initialize fallback parameters for filtering
902 6263 ret = ifilter_parameters_from_dec(ifilter, ist->dec_ctx);
903
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6263 times.
6263 if (ret < 0)
904 return ret;
905
906 6263 return 0;
907 }
908
909 13974 static const AVCodec *choose_decoder(const OptionsContext *o, AVFormatContext *s, AVStream *st,
910 enum HWAccelID hwaccel_id, enum AVHWDeviceType hwaccel_device_type)
911
912 {
913 13974 char *codec_name = NULL;
914
915
6/20
✓ Branch 1 taken 5394 times.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8 times.
✓ Branch 6 taken 5402 times.
✓ Branch 7 taken 13974 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 13974 times.
✗ 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 17 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
19376 MATCH_PER_STREAM_OPT(codec_names, str, codec_name, s, st);
916
2/2
✓ Branch 0 taken 5394 times.
✓ Branch 1 taken 8580 times.
13974 if (codec_name) {
917 5394 const AVCodec *codec = find_codec_or_die(NULL, codec_name, st->codecpar->codec_type, 0);
918 5394 st->codecpar->codec_id = codec->id;
919
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 5394 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
5394 if (recast_media && st->codecpar->codec_type != codec->type)
920 st->codecpar->codec_type = codec->type;
921 5394 return codec;
922 } else {
923
3/4
✓ Branch 0 taken 5334 times.
✓ Branch 1 taken 3246 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 5334 times.
8580 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
924 hwaccel_id == HWACCEL_GENERIC &&
925 hwaccel_device_type != AV_HWDEVICE_TYPE_NONE) {
926 const AVCodec *c;
927 void *i = NULL;
928
929 while ((c = av_codec_iterate(&i))) {
930 const AVCodecHWConfig *config;
931
932 if (c->id != st->codecpar->codec_id ||
933 !av_codec_is_decoder(c))
934 continue;
935
936 for (int j = 0; config = avcodec_get_hw_config(c, j); j++) {
937 if (config->device_type == hwaccel_device_type) {
938 av_log(NULL, AV_LOG_VERBOSE, "Selecting decoder '%s' because of requested hwaccel method %s\n",
939 c->name, av_hwdevice_get_type_name(hwaccel_device_type));
940 return c;
941 }
942 }
943 }
944 }
945
946 8580 return avcodec_find_decoder(st->codecpar->codec_id);
947 }
948 }
949
950 1560 static int guess_input_channel_layout(InputStream *ist, int guess_layout_max)
951 {
952 1560 AVCodecContext *dec = ist->dec_ctx;
953
954
2/2
✓ Branch 0 taken 806 times.
✓ Branch 1 taken 754 times.
1560 if (dec->ch_layout.order == AV_CHANNEL_ORDER_UNSPEC) {
955 char layout_name[256];
956
957
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 802 times.
806 if (dec->ch_layout.nb_channels > guess_layout_max)
958 12 return 0;
959 802 av_channel_layout_default(&dec->ch_layout, dec->ch_layout.nb_channels);
960
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 794 times.
802 if (dec->ch_layout.order == AV_CHANNEL_ORDER_UNSPEC)
961 8 return 0;
962 794 av_channel_layout_describe(&dec->ch_layout, layout_name, sizeof(layout_name));
963 794 av_log(ist, AV_LOG_WARNING, "Guessed Channel Layout: %s\n", layout_name);
964 }
965 1548 return 1;
966 }
967
968 5354 static void add_display_matrix_to_stream(const OptionsContext *o,
969 AVFormatContext *ctx, InputStream *ist)
970 {
971 5354 AVStream *st = ist->st;
972 5354 double rotation = DBL_MAX;
973 5354 int hflip = -1, vflip = -1;
974 5354 int hflip_set = 0, vflip_set = 0, rotation_set = 0;
975 int32_t *buf;
976
977
4/20
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 6 taken 1 times.
✓ Branch 7 taken 5354 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 5354 times.
✗ 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 17 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
5355 MATCH_PER_STREAM_OPT(display_rotations, dbl, rotation, ctx, st);
978
2/20
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 5354 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 5354 times.
✗ 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 17 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
5354 MATCH_PER_STREAM_OPT(display_hflips, i, hflip, ctx, st);
979
2/20
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 5354 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 5354 times.
✗ 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 17 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
5354 MATCH_PER_STREAM_OPT(display_vflips, i, vflip, ctx, st);
980
981 5354 rotation_set = rotation != DBL_MAX;
982 5354 hflip_set = hflip != -1;
983 5354 vflip_set = vflip != -1;
984
985
4/6
✓ Branch 0 taken 5353 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 5353 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 5353 times.
✗ Branch 5 not taken.
5354 if (!rotation_set && !hflip_set && !vflip_set)
986 5353 return;
987
988 1 buf = (int32_t *)av_stream_new_side_data(st, AV_PKT_DATA_DISPLAYMATRIX, sizeof(int32_t) * 9);
989
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!buf) {
990 av_log(ist, AV_LOG_FATAL, "Failed to generate a display matrix!\n");
991 exit_program(1);
992 }
993
994
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 av_display_rotation_set(buf,
995 rotation_set ? -(rotation) : -0.0f);
996
997
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,
998 hflip_set ? hflip : 0,
999 vflip_set ? vflip : 0);
1000 }
1001
1002 1305 static const char *input_stream_item_name(void *obj)
1003 {
1004 1305 const DemuxStream *ds = obj;
1005
1006 1305 return ds->log_name;
1007 }
1008
1009 static const AVClass input_stream_class = {
1010 .class_name = "InputStream",
1011 .version = LIBAVUTIL_VERSION_INT,
1012 .item_name = input_stream_item_name,
1013 .category = AV_CLASS_CATEGORY_DEMUXER,
1014 };
1015
1016 7047 static DemuxStream *demux_stream_alloc(Demuxer *d, AVStream *st)
1017 {
1018 7047 const char *type_str = av_get_media_type_string(st->codecpar->codec_type);
1019 7047 InputFile *f = &d->f;
1020 7047 DemuxStream *ds = allocate_array_elem(&f->streams, sizeof(*ds),
1021 &f->nb_streams);
1022
1023 7047 ds->ist.st = st;
1024 7047 ds->ist.file_index = f->index;
1025 7047 ds->ist.index = st->index;
1026 7047 ds->ist.class = &input_stream_class;
1027
1028
1/2
✓ Branch 0 taken 7047 times.
✗ Branch 1 not taken.
7047 snprintf(ds->log_name, sizeof(ds->log_name), "%cist#%d:%d/%s",
1029 7047 type_str ? *type_str : '?', d->f.index, st->index,
1030 7047 avcodec_get_name(st->codecpar->codec_id));
1031
1032 7047 return ds;
1033 }
1034
1035 /* Add all the streams from the given input file to the demuxer */
1036 6598 static void add_input_streams(const OptionsContext *o, Demuxer *d)
1037 {
1038 6598 InputFile *f = &d->f;
1039 6598 AVFormatContext *ic = f->ctx;
1040 int i, ret;
1041
1042
2/2
✓ Branch 0 taken 7047 times.
✓ Branch 1 taken 6598 times.
13645 for (i = 0; i < ic->nb_streams; i++) {
1043 7047 AVStream *st = ic->streams[i];
1044 7047 AVCodecParameters *par = st->codecpar;
1045 DemuxStream *ds;
1046 InputStream *ist;
1047 7047 char *framerate = NULL, *hwaccel_device = NULL;
1048 7047 const char *hwaccel = NULL;
1049 7047 char *hwaccel_output_format = NULL;
1050 7047 char *codec_tag = NULL;
1051 char *next;
1052 7047 char *discard_str = NULL;
1053 7047 const AVClass *cc = avcodec_get_class();
1054 7047 const AVOption *discard_opt = av_opt_find(&cc, "skip_frame", NULL,
1055 0, AV_OPT_SEARCH_FAKE_OBJ);
1056
1057 7047 ds = demux_stream_alloc(d, st);
1058 7047 ist = &ds->ist;
1059
1060 7047 ist->discard = 1;
1061 7047 st->discard = AVDISCARD_ALL;
1062 7047 ist->nb_samples = 0;
1063 7047 ds->first_dts = AV_NOPTS_VALUE;
1064 7047 ds->next_dts = AV_NOPTS_VALUE;
1065
1066 7047 ds->min_pts = INT64_MAX;
1067 7047 ds->max_pts = INT64_MIN;
1068
1069 7047 ds->ts_scale = 1.0;
1070
2/20
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 7047 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 7047 times.
✗ 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 17 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
7047 MATCH_PER_STREAM_OPT(ts_scale, dbl, ds->ts_scale, ic, st);
1071
1072 7047 ist->autorotate = 1;
1073
2/20
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 7047 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 7047 times.
✗ 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 17 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
7047 MATCH_PER_STREAM_OPT(autorotate, i, ist->autorotate, ic, st);
1074
1075
4/20
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 7047 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 7047 times.
✗ 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 17 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
7049 MATCH_PER_STREAM_OPT(codec_tags, str, codec_tag, ic, st);
1076
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 7045 times.
7047 if (codec_tag) {
1077 2 uint32_t tag = strtol(codec_tag, &next, 0);
1078
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (*next) {
1079 2 uint8_t buf[4] = { 0 };
1080
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 memcpy(buf, codec_tag, FFMIN(sizeof(buf), strlen(codec_tag)));
1081 2 tag = AV_RL32(buf);
1082 }
1083
1084 2 st->codecpar->codec_tag = tag;
1085 }
1086
1087
2/2
✓ Branch 0 taken 5354 times.
✓ Branch 1 taken 1693 times.
7047 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
1088 5354 add_display_matrix_to_stream(o, ic, ist);
1089
1090
4/20
✓ Branch 1 taken 5070 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 6 taken 5070 times.
✓ Branch 7 taken 5354 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 5354 times.
✗ 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 17 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
10424 MATCH_PER_STREAM_OPT(hwaccels, str, hwaccel, ic, st);
1091
2/20
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 5354 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 5354 times.
✗ 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 17 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
5354 MATCH_PER_STREAM_OPT(hwaccel_output_formats, str,
1092 hwaccel_output_format, ic, st);
1093
1094
4/6
✓ Branch 0 taken 5354 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5070 times.
✓ Branch 3 taken 284 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 5070 times.
5354 if (!hwaccel_output_format && hwaccel && !strcmp(hwaccel, "cuvid")) {
1095 av_log(ist, AV_LOG_WARNING,
1096 "WARNING: defaulting hwaccel_output_format to cuda for compatibility "
1097 "with old commandlines. This behaviour is DEPRECATED and will be removed "
1098 "in the future. Please explicitly set \"-hwaccel_output_format cuda\".\n");
1099 ist->hwaccel_output_format = AV_PIX_FMT_CUDA;
1100
4/6
✓ Branch 0 taken 5354 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5070 times.
✓ Branch 3 taken 284 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 5070 times.
5354 } else if (!hwaccel_output_format && hwaccel && !strcmp(hwaccel, "qsv")) {
1101 av_log(ist, AV_LOG_WARNING,
1102 "WARNING: defaulting hwaccel_output_format to qsv for compatibility "
1103 "with old commandlines. This behaviour is DEPRECATED and will be removed "
1104 "in the future. Please explicitly set \"-hwaccel_output_format qsv\".\n");
1105 ist->hwaccel_output_format = AV_PIX_FMT_QSV;
1106
4/6
✓ Branch 0 taken 5354 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5070 times.
✓ Branch 3 taken 284 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 5070 times.
5354 } else if (!hwaccel_output_format && hwaccel && !strcmp(hwaccel, "mediacodec")) {
1107 // There is no real AVHWFrameContext implementation. Set
1108 // hwaccel_output_format to avoid av_hwframe_transfer_data error.
1109 ist->hwaccel_output_format = AV_PIX_FMT_MEDIACODEC;
1110
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5354 times.
5354 } else if (hwaccel_output_format) {
1111 ist->hwaccel_output_format = av_get_pix_fmt(hwaccel_output_format);
1112 if (ist->hwaccel_output_format == AV_PIX_FMT_NONE) {
1113 av_log(ist, AV_LOG_FATAL, "Unrecognised hwaccel output "
1114 "format: %s", hwaccel_output_format);
1115 }
1116 } else {
1117 5354 ist->hwaccel_output_format = AV_PIX_FMT_NONE;
1118 }
1119
1120
2/2
✓ Branch 0 taken 5070 times.
✓ Branch 1 taken 284 times.
5354 if (hwaccel) {
1121 // The NVDEC hwaccels use a CUDA device, so remap the name here.
1122
2/4
✓ Branch 0 taken 5070 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5070 times.
5070 if (!strcmp(hwaccel, "nvdec") || !strcmp(hwaccel, "cuvid"))
1123 hwaccel = "cuda";
1124
1125
1/2
✓ Branch 0 taken 5070 times.
✗ Branch 1 not taken.
5070 if (!strcmp(hwaccel, "none"))
1126 5070 ist->hwaccel_id = HWACCEL_NONE;
1127 else if (!strcmp(hwaccel, "auto"))
1128 ist->hwaccel_id = HWACCEL_AUTO;
1129 else {
1130 enum AVHWDeviceType type = av_hwdevice_find_type_by_name(hwaccel);
1131 if (type != AV_HWDEVICE_TYPE_NONE) {
1132 ist->hwaccel_id = HWACCEL_GENERIC;
1133 ist->hwaccel_device_type = type;
1134 }
1135
1136 if (!ist->hwaccel_id) {
1137 av_log(ist, AV_LOG_FATAL, "Unrecognized hwaccel: %s.\n",
1138 hwaccel);
1139 av_log(ist, AV_LOG_FATAL, "Supported hwaccels: ");
1140 type = AV_HWDEVICE_TYPE_NONE;
1141 while ((type = av_hwdevice_iterate_types(type)) !=
1142 AV_HWDEVICE_TYPE_NONE)
1143 av_log(ist, AV_LOG_FATAL, "%s ",
1144 av_hwdevice_get_type_name(type));
1145 av_log(ist, AV_LOG_FATAL, "\n");
1146 exit_program(1);
1147 }
1148 }
1149 }
1150
1151
2/20
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 5354 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 5354 times.
✗ 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 17 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
5354 MATCH_PER_STREAM_OPT(hwaccel_devices, str, hwaccel_device, ic, st);
1152
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5354 times.
5354 if (hwaccel_device) {
1153 ist->hwaccel_device = av_strdup(hwaccel_device);
1154 if (!ist->hwaccel_device)
1155 report_and_exit(AVERROR(ENOMEM));
1156 }
1157
1158 5354 ist->hwaccel_pix_fmt = AV_PIX_FMT_NONE;
1159 }
1160
1161 7047 ist->dec = choose_decoder(o, ic, st, ist->hwaccel_id, ist->hwaccel_device_type);
1162 7047 ist->decoder_opts = filter_codec_opts(o->g->codec_opts, ist->st->codecpar->codec_id, ic, st, ist->dec);
1163
1164 7047 ist->reinit_filters = -1;
1165
2/20
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 7047 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 7047 times.
✗ 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 17 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
7047 MATCH_PER_STREAM_OPT(reinit_filters, i, ist->reinit_filters, ic, st);
1166
1167
2/20
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 7047 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 7047 times.
✗ 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 17 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
7047 MATCH_PER_STREAM_OPT(discard, str, discard_str, ic, st);
1168 7047 ist->user_set_discard = AVDISCARD_NONE;
1169
1170
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 7047 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
7047 if ((o->video_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) ||
1171
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7046 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
7047 (o->audio_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) ||
1172
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 7046 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
7046 (o->subtitle_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) ||
1173
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 7046 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
7046 (o->data_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_DATA))
1174 1 ist->user_set_discard = AVDISCARD_ALL;
1175
1176
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 7047 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
7047 if (discard_str && av_opt_eval_int(&cc, discard_opt, discard_str, &ist->user_set_discard) < 0) {
1177 av_log(ist, AV_LOG_ERROR, "Error parsing discard %s.\n",
1178 discard_str);
1179 exit_program(1);
1180 }
1181
1182 7047 ist->dec_ctx = avcodec_alloc_context3(ist->dec);
1183
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7047 times.
7047 if (!ist->dec_ctx)
1184 report_and_exit(AVERROR(ENOMEM));
1185
1186 7047 ret = avcodec_parameters_to_context(ist->dec_ctx, par);
1187
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7047 times.
7047 if (ret < 0) {
1188 av_log(ist, AV_LOG_ERROR, "Error initializing the decoder context.\n");
1189 exit_program(1);
1190 }
1191
1192
2/2
✓ Branch 0 taken 93 times.
✓ Branch 1 taken 6954 times.
7047 if (o->bitexact)
1193 93 ist->dec_ctx->flags |= AV_CODEC_FLAG_BITEXACT;
1194
1195
4/5
✓ Branch 0 taken 5354 times.
✓ Branch 1 taken 1560 times.
✓ Branch 2 taken 132 times.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
7047 switch (par->codec_type) {
1196 5354 case AVMEDIA_TYPE_VIDEO:
1197 // avformat_find_stream_info() doesn't set this for us anymore.
1198 5354 ist->dec_ctx->framerate = st->avg_frame_rate;
1199
1200
4/20
✓ Branch 1 taken 26 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 6 taken 26 times.
✓ Branch 7 taken 5354 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 5354 times.
✗ 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 17 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
5380 MATCH_PER_STREAM_OPT(frame_rates, str, framerate, ic, st);
1201
3/4
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 5328 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 26 times.
5354 if (framerate && av_parse_video_rate(&ist->framerate,
1202 framerate) < 0) {
1203 av_log(ist, AV_LOG_ERROR, "Error parsing framerate %s.\n",
1204 framerate);
1205 exit_program(1);
1206 }
1207
1208 5354 ist->top_field_first = -1;
1209
2/20
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 5354 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 5354 times.
✗ 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 17 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
5354 MATCH_PER_STREAM_OPT(top_field_first, i, ist->top_field_first, ic, st);
1210
1211 5354 ist->framerate_guessed = av_guess_frame_rate(ic, st, NULL);
1212
1213 5354 break;
1214 1560 case AVMEDIA_TYPE_AUDIO: {
1215 1560 int guess_layout_max = INT_MAX;
1216
4/20
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 6 taken 4 times.
✓ Branch 7 taken 1560 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 1560 times.
✗ 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 17 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
1564 MATCH_PER_STREAM_OPT(guess_layout_max, i, guess_layout_max, ic, st);
1217 1560 guess_input_channel_layout(ist, guess_layout_max);
1218 1560 break;
1219 }
1220 132 case AVMEDIA_TYPE_DATA:
1221 case AVMEDIA_TYPE_SUBTITLE: {
1222 132 char *canvas_size = NULL;
1223
4/20
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 132 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 132 times.
✗ 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 17 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
134 MATCH_PER_STREAM_OPT(fix_sub_duration, i, ist->fix_sub_duration, ic, st);
1224
2/20
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 132 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 132 times.
✗ 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 17 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
132 MATCH_PER_STREAM_OPT(canvas_sizes, str, canvas_size, ic, st);
1225
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 132 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
132 if (canvas_size &&
1226 av_parse_video_size(&ist->dec_ctx->width, &ist->dec_ctx->height, canvas_size) < 0) {
1227 av_log(ist, AV_LOG_FATAL, "Invalid canvas size: %s.\n", canvas_size);
1228 exit_program(1);
1229 }
1230
1231 /* Compute the size of the canvas for the subtitles stream.
1232 If the subtitles codecpar has set a size, use it. Otherwise use the
1233 maximum dimensions of the video streams in the same file. */
1234 132 ist->sub2video.w = ist->dec_ctx->width;
1235 132 ist->sub2video.h = ist->dec_ctx->height;
1236
3/4
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 113 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 19 times.
132 if (!(ist->sub2video.w && ist->sub2video.h)) {
1237
2/2
✓ Branch 0 taken 308 times.
✓ Branch 1 taken 113 times.
421 for (int j = 0; j < ic->nb_streams; j++) {
1238 308 AVCodecParameters *par1 = ic->streams[j]->codecpar;
1239
2/2
✓ Branch 0 taken 63 times.
✓ Branch 1 taken 245 times.
308 if (par1->codec_type == AVMEDIA_TYPE_VIDEO) {
1240 63 ist->sub2video.w = FFMAX(ist->sub2video.w, par1->width);
1241 63 ist->sub2video.h = FFMAX(ist->sub2video.h, par1->height);
1242 }
1243 }
1244 }
1245
1246
3/4
✓ Branch 0 taken 71 times.
✓ Branch 1 taken 61 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 71 times.
132 if (!(ist->sub2video.w && ist->sub2video.h)) {
1247 61 ist->sub2video.w = FFMAX(ist->sub2video.w, 720);
1248 61 ist->sub2video.h = FFMAX(ist->sub2video.h, 576);
1249 }
1250
1251 132 break;
1252 }
1253 1 case AVMEDIA_TYPE_ATTACHMENT:
1254 case AVMEDIA_TYPE_UNKNOWN:
1255 1 break;
1256 default:
1257 abort();
1258 }
1259
1260 7047 ist->par = avcodec_parameters_alloc();
1261
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7047 times.
7047 if (!ist->par)
1262 report_and_exit(AVERROR(ENOMEM));
1263
1264 7047 ret = avcodec_parameters_from_context(ist->par, ist->dec_ctx);
1265
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7047 times.
7047 if (ret < 0) {
1266 av_log(ist, AV_LOG_ERROR, "Error initializing the decoder context.\n");
1267 exit_program(1);
1268 }
1269
1270 7047 ist->codec_desc = avcodec_descriptor_get(ist->par->codec_id);
1271 }
1272 6598 }
1273
1274 static void dump_attachment(InputStream *ist, const char *filename)
1275 {
1276 AVStream *st = ist->st;
1277 int ret;
1278 AVIOContext *out = NULL;
1279 const AVDictionaryEntry *e;
1280
1281 if (!st->codecpar->extradata_size) {
1282 av_log(ist, AV_LOG_WARNING, "No extradata to dump.\n");
1283 return;
1284 }
1285 if (!*filename && (e = av_dict_get(st->metadata, "filename", NULL, 0)))
1286 filename = e->value;
1287 if (!*filename) {
1288 av_log(ist, AV_LOG_FATAL, "No filename specified and no 'filename' tag");
1289 exit_program(1);
1290 }
1291
1292 assert_file_overwrite(filename);
1293
1294 if ((ret = avio_open2(&out, filename, AVIO_FLAG_WRITE, &int_cb, NULL)) < 0) {
1295 av_log(ist, AV_LOG_FATAL, "Could not open file %s for writing.\n",
1296 filename);
1297 exit_program(1);
1298 }
1299
1300 avio_write(out, st->codecpar->extradata, st->codecpar->extradata_size);
1301 avio_flush(out);
1302 avio_close(out);
1303 }
1304
1305 681 static const char *input_file_item_name(void *obj)
1306 {
1307 681 const Demuxer *d = obj;
1308
1309 681 return d->log_name;
1310 }
1311
1312 static const AVClass input_file_class = {
1313 .class_name = "InputFile",
1314 .version = LIBAVUTIL_VERSION_INT,
1315 .item_name = input_file_item_name,
1316 .category = AV_CLASS_CATEGORY_DEMUXER,
1317 };
1318
1319 6598 static Demuxer *demux_alloc(void)
1320 {
1321 6598 Demuxer *d = allocate_array_elem(&input_files, sizeof(*d), &nb_input_files);
1322
1323 6598 d->f.class = &input_file_class;
1324 6598 d->f.index = nb_input_files - 1;
1325
1326 6598 snprintf(d->log_name, sizeof(d->log_name), "in#%d", d->f.index);
1327
1328 6598 return d;
1329 }
1330
1331 6598 int ifile_open(const OptionsContext *o, const char *filename)
1332 {
1333 Demuxer *d;
1334 InputFile *f;
1335 AVFormatContext *ic;
1336 6598 const AVInputFormat *file_iformat = NULL;
1337 int err, i, ret;
1338 int64_t timestamp;
1339 6598 AVDictionary *unused_opts = NULL;
1340 6598 const AVDictionaryEntry *e = NULL;
1341 6598 char * video_codec_name = NULL;
1342 6598 char * audio_codec_name = NULL;
1343 6598 char *subtitle_codec_name = NULL;
1344 6598 char * data_codec_name = NULL;
1345 6598 int scan_all_pmts_set = 0;
1346
1347 6598 int64_t start_time = o->start_time;
1348 6598 int64_t start_time_eof = o->start_time_eof;
1349 6598 int64_t stop_time = o->stop_time;
1350 6598 int64_t recording_time = o->recording_time;
1351
1352 6598 d = demux_alloc();
1353 6598 f = &d->f;
1354
1355
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 6598 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
6598 if (stop_time != INT64_MAX && recording_time != INT64_MAX) {
1356 stop_time = INT64_MAX;
1357 av_log(d, AV_LOG_WARNING, "-t and -to cannot be used together; using -t.\n");
1358 }
1359
1360
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 6598 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
6598 if (stop_time != INT64_MAX && recording_time == INT64_MAX) {
1361 int64_t start = start_time == AV_NOPTS_VALUE ? 0 : start_time;
1362 if (stop_time <= start) {
1363 av_log(d, AV_LOG_ERROR, "-to value smaller than -ss; aborting.\n");
1364 exit_program(1);
1365 } else {
1366 recording_time = stop_time - start;
1367 }
1368 }
1369
1370
2/2
✓ Branch 0 taken 3277 times.
✓ Branch 1 taken 3321 times.
6598 if (o->format) {
1371
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3277 times.
3277 if (!(file_iformat = av_find_input_format(o->format))) {
1372 av_log(d, AV_LOG_FATAL, "Unknown input format: '%s'\n", o->format);
1373 exit_program(1);
1374 }
1375 }
1376
1377
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6598 times.
6598 if (!strcmp(filename, "-"))
1378 filename = "fd:";
1379
1380 19794 stdin_interaction &= strncmp(filename, "pipe:", 5) &&
1381
2/4
✓ Branch 0 taken 6598 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6598 times.
✗ Branch 3 not taken.
13196 strcmp(filename, "fd:") &&
1382
1/2
✓ Branch 0 taken 6598 times.
✗ Branch 1 not taken.
6598 strcmp(filename, "/dev/stdin");
1383
1384 /* get default parameters from command line */
1385 6598 ic = avformat_alloc_context();
1386
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6598 times.
6598 if (!ic)
1387 report_and_exit(AVERROR(ENOMEM));
1388
2/2
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 6532 times.
6598 if (o->nb_audio_sample_rate) {
1389 66 av_dict_set_int(&o->g->format_opts, "sample_rate", o->audio_sample_rate[o->nb_audio_sample_rate - 1].u.i, 0);
1390 }
1391
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 6589 times.
6598 if (o->nb_audio_channels) {
1392 const AVClass *priv_class;
1393
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) &&
1394 9 av_opt_find(&priv_class, "ch_layout", NULL, 0,
1395 AV_OPT_SEARCH_FAKE_OBJ)) {
1396 char buf[32];
1397 9 snprintf(buf, sizeof(buf), "%dC", o->audio_channels[o->nb_audio_channels - 1].u.i);
1398 9 av_dict_set(&o->g->format_opts, "ch_layout", buf, 0);
1399 }
1400 }
1401
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6597 times.
6598 if (o->nb_audio_ch_layouts) {
1402 const AVClass *priv_class;
1403
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) &&
1404 1 av_opt_find(&priv_class, "ch_layout", NULL, 0,
1405 AV_OPT_SEARCH_FAKE_OBJ)) {
1406 1 av_dict_set(&o->g->format_opts, "ch_layout", o->audio_ch_layouts[o->nb_audio_ch_layouts - 1].u.str, 0);
1407 }
1408 }
1409
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 6572 times.
6598 if (o->nb_frame_rates) {
1410 const AVClass *priv_class;
1411 /* set the format-level framerate option;
1412 * this is important for video grabbers, e.g. x11 */
1413
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) &&
1414 25 av_opt_find(&priv_class, "framerate", NULL, 0,
1415 AV_OPT_SEARCH_FAKE_OBJ)) {
1416 25 av_dict_set(&o->g->format_opts, "framerate",
1417 25 o->frame_rates[o->nb_frame_rates - 1].u.str, 0);
1418 }
1419 }
1420
2/2
✓ Branch 0 taken 522 times.
✓ Branch 1 taken 6076 times.
6598 if (o->nb_frame_sizes) {
1421 522 av_dict_set(&o->g->format_opts, "video_size", o->frame_sizes[o->nb_frame_sizes - 1].u.str, 0);
1422 }
1423
2/2
✓ Branch 0 taken 531 times.
✓ Branch 1 taken 6067 times.
6598 if (o->nb_frame_pix_fmts)
1424 531 av_dict_set(&o->g->format_opts, "pixel_format", o->frame_pix_fmts[o->nb_frame_pix_fmts - 1].u.str, 0);
1425
1426
4/4
✓ Branch 0 taken 2663 times.
✓ Branch 1 taken 34 times.
✓ Branch 2 taken 2697 times.
✓ Branch 3 taken 6598 times.
9295 MATCH_PER_TYPE_OPT(codec_names, str, video_codec_name, ic, "v");
1427
4/4
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 2680 times.
✓ Branch 2 taken 2697 times.
✓ Branch 3 taken 6598 times.
9295 MATCH_PER_TYPE_OPT(codec_names, str, audio_codec_name, ic, "a");
1428
3/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2697 times.
✓ Branch 2 taken 2697 times.
✓ Branch 3 taken 6598 times.
9295 MATCH_PER_TYPE_OPT(codec_names, str, subtitle_codec_name, ic, "s");
1429
3/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2697 times.
✓ Branch 2 taken 2697 times.
✓ Branch 3 taken 6598 times.
9295 MATCH_PER_TYPE_OPT(codec_names, str, data_codec_name, ic, "d");
1430
1431
2/2
✓ Branch 0 taken 2663 times.
✓ Branch 1 taken 3935 times.
6598 if (video_codec_name)
1432 2663 ic->video_codec = find_codec_or_die(NULL, video_codec_name , AVMEDIA_TYPE_VIDEO , 0);
1433
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 6581 times.
6598 if (audio_codec_name)
1434 17 ic->audio_codec = find_codec_or_die(NULL, audio_codec_name , AVMEDIA_TYPE_AUDIO , 0);
1435
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6598 times.
6598 if (subtitle_codec_name)
1436 ic->subtitle_codec = find_codec_or_die(NULL, subtitle_codec_name, AVMEDIA_TYPE_SUBTITLE, 0);
1437
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6598 times.
6598 if (data_codec_name)
1438 ic->data_codec = find_codec_or_die(NULL, data_codec_name , AVMEDIA_TYPE_DATA , 0);
1439
1440
2/2
✓ Branch 0 taken 2663 times.
✓ Branch 1 taken 3935 times.
6598 ic->video_codec_id = video_codec_name ? ic->video_codec->id : AV_CODEC_ID_NONE;
1441
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 6581 times.
6598 ic->audio_codec_id = audio_codec_name ? ic->audio_codec->id : AV_CODEC_ID_NONE;
1442
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6598 times.
6598 ic->subtitle_codec_id = subtitle_codec_name ? ic->subtitle_codec->id : AV_CODEC_ID_NONE;
1443
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6598 times.
6598 ic->data_codec_id = data_codec_name ? ic->data_codec->id : AV_CODEC_ID_NONE;
1444
1445 6598 ic->flags |= AVFMT_FLAG_NONBLOCK;
1446
2/2
✓ Branch 0 taken 82 times.
✓ Branch 1 taken 6516 times.
6598 if (o->bitexact)
1447 82 ic->flags |= AVFMT_FLAG_BITEXACT;
1448 6598 ic->interrupt_callback = int_cb;
1449
1450
1/2
✓ Branch 1 taken 6598 times.
✗ Branch 2 not taken.
6598 if (!av_dict_get(o->g->format_opts, "scan_all_pmts", NULL, AV_DICT_MATCH_CASE)) {
1451 6598 av_dict_set(&o->g->format_opts, "scan_all_pmts", "1", AV_DICT_DONT_OVERWRITE);
1452 6598 scan_all_pmts_set = 1;
1453 }
1454 /* open the input file with generic avformat function */
1455 6598 err = avformat_open_input(&ic, filename, file_iformat, &o->g->format_opts);
1456
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6598 times.
6598 if (err < 0) {
1457 av_log(d, AV_LOG_ERROR,
1458 "Error opening input: %s\n", av_err2str(err));
1459 if (err == AVERROR_PROTOCOL_NOT_FOUND)
1460 av_log(d, AV_LOG_ERROR, "Did you mean file:%s?\n", filename);
1461 exit_program(1);
1462 }
1463
1464 6598 av_strlcat(d->log_name, "/", sizeof(d->log_name));
1465 6598 av_strlcat(d->log_name, ic->iformat->name, sizeof(d->log_name));
1466
1467
1/2
✓ Branch 0 taken 6598 times.
✗ Branch 1 not taken.
6598 if (scan_all_pmts_set)
1468 6598 av_dict_set(&o->g->format_opts, "scan_all_pmts", NULL, AV_DICT_MATCH_CASE);
1469 6598 remove_avoptions(&o->g->format_opts, o->g->codec_opts);
1470 6598 assert_avoptions(o->g->format_opts);
1471
1472 /* apply forced codec ids */
1473
2/2
✓ Branch 0 taken 6927 times.
✓ Branch 1 taken 6598 times.
13525 for (i = 0; i < ic->nb_streams; i++)
1474 6927 choose_decoder(o, ic, ic->streams[i], HWACCEL_NONE, AV_HWDEVICE_TYPE_NONE);
1475
1476
2/2
✓ Branch 0 taken 6596 times.
✓ Branch 1 taken 2 times.
6598 if (o->find_stream_info) {
1477 6596 AVDictionary **opts = setup_find_stream_info_opts(ic, o->g->codec_opts);
1478 6596 int orig_nb_streams = ic->nb_streams;
1479
1480 /* If not enough info to get the stream parameters, we decode the
1481 first frames to get it. (used in mpeg case for example) */
1482 6596 ret = avformat_find_stream_info(ic, opts);
1483
1484
2/2
✓ Branch 0 taken 6925 times.
✓ Branch 1 taken 6596 times.
13521 for (i = 0; i < orig_nb_streams; i++)
1485 6925 av_dict_free(&opts[i]);
1486 6596 av_freep(&opts);
1487
1488
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6596 times.
6596 if (ret < 0) {
1489 av_log(d, AV_LOG_FATAL, "could not find codec parameters\n");
1490 if (ic->nb_streams == 0) {
1491 avformat_close_input(&ic);
1492 exit_program(1);
1493 }
1494 }
1495 }
1496
1497
3/4
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 6577 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 21 times.
6598 if (start_time != AV_NOPTS_VALUE && start_time_eof != AV_NOPTS_VALUE) {
1498 av_log(d, AV_LOG_WARNING, "Cannot use -ss and -sseof both, using -ss\n");
1499 start_time_eof = AV_NOPTS_VALUE;
1500 }
1501
1502
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6598 times.
6598 if (start_time_eof != AV_NOPTS_VALUE) {
1503 if (start_time_eof >= 0) {
1504 av_log(d, AV_LOG_ERROR, "-sseof value must be negative; aborting\n");
1505 exit_program(1);
1506 }
1507 if (ic->duration > 0) {
1508 start_time = start_time_eof + ic->duration;
1509 if (start_time < 0) {
1510 av_log(d, AV_LOG_WARNING, "-sseof value seeks to before start of file; ignored\n");
1511 start_time = AV_NOPTS_VALUE;
1512 }
1513 } else
1514 av_log(d, AV_LOG_WARNING, "Cannot use -sseof, file duration not known\n");
1515 }
1516
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 6577 times.
6598 timestamp = (start_time == AV_NOPTS_VALUE) ? 0 : start_time;
1517 /* add the stream start time */
1518
4/4
✓ Branch 0 taken 6595 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 4991 times.
✓ Branch 3 taken 1604 times.
6598 if (!o->seek_timestamp && ic->start_time != AV_NOPTS_VALUE)
1519 4991 timestamp += ic->start_time;
1520
1521 /* if seeking requested, we execute it */
1522
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 6577 times.
6598 if (start_time != AV_NOPTS_VALUE) {
1523 21 int64_t seek_timestamp = timestamp;
1524
1525
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
21 if (!(ic->iformat->flags & AVFMT_SEEK_TO_PTS)) {
1526 21 int dts_heuristic = 0;
1527
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 21 times.
42 for (i=0; i<ic->nb_streams; i++) {
1528 21 const AVCodecParameters *par = ic->streams[i]->codecpar;
1529
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (par->video_delay) {
1530 dts_heuristic = 1;
1531 break;
1532 }
1533 }
1534
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (dts_heuristic) {
1535 seek_timestamp -= 3*AV_TIME_BASE / 23;
1536 }
1537 }
1538 21 ret = avformat_seek_file(ic, -1, INT64_MIN, seek_timestamp, seek_timestamp, 0);
1539
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 20 times.
21 if (ret < 0) {
1540 1 av_log(d, AV_LOG_WARNING, "could not seek to position %0.3f\n",
1541 1 (double)timestamp / AV_TIME_BASE);
1542 }
1543 }
1544
1545 6598 f->ctx = ic;
1546 6598 f->start_time = start_time;
1547 6598 f->recording_time = recording_time;
1548 6598 f->input_sync_ref = o->input_sync_ref;
1549 6598 f->input_ts_offset = o->input_ts_offset;
1550
3/6
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 6586 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 12 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
6598 f->ts_offset = o->input_ts_offset - (copy_ts ? (start_at_zero && ic->start_time != AV_NOPTS_VALUE ? ic->start_time : 0) : timestamp);
1551 6598 f->accurate_seek = o->accurate_seek;
1552 6598 d->loop = o->loop;
1553 6598 d->duration = 0;
1554 6598 d->time_base = (AVRational){ 1, 1 };
1555 6598 d->nb_streams_warn = ic->nb_streams;
1556
1557 6598 f->format_nots = !!(ic->iformat->flags & AVFMT_NOTIMESTAMPS);
1558
1559 6598 f->readrate = o->readrate ? o->readrate : 0.0;
1560
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6598 times.
6598 if (f->readrate < 0.0f) {
1561 av_log(d, AV_LOG_ERROR, "Option -readrate is %0.3f; it must be non-negative.\n", f->readrate);
1562 exit_program(1);
1563 }
1564
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6597 times.
6598 if (o->rate_emu) {
1565
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (f->readrate) {
1566 av_log(d, AV_LOG_WARNING, "Both -readrate and -re set. Using -readrate %0.3f.\n", f->readrate);
1567 } else
1568 1 f->readrate = 1.0f;
1569 }
1570
1571
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6597 times.
6598 if (f->readrate) {
1572
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;
1573
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (d->readrate_initial_burst < 0.0) {
1574 av_log(d, AV_LOG_ERROR,
1575 "Option -readrate_initial_burst is %0.3f; it must be non-negative.\n",
1576 d->readrate_initial_burst);
1577 exit_program(1);
1578 }
1579
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6597 times.
6597 } else if (o->readrate_initial_burst) {
1580 av_log(d, AV_LOG_WARNING, "Option -readrate_initial_burst ignored "
1581 "since neither -readrate nor -re were given\n");
1582 }
1583
1584 6598 d->thread_queue_size = o->thread_queue_size;
1585
1586 6598 add_input_streams(o, d);
1587
1588 /* dump the file content */
1589 6598 av_dump_format(ic, f->index, filename, 0);
1590
1591 /* check if all codec options have been used */
1592 6598 unused_opts = strip_specifiers(o->g->codec_opts);
1593
2/2
✓ Branch 0 taken 7047 times.
✓ Branch 1 taken 6598 times.
13645 for (i = 0; i < f->nb_streams; i++) {
1594 7047 e = NULL;
1595
2/2
✓ Branch 1 taken 21885 times.
✓ Branch 2 taken 7047 times.
28932 while ((e = av_dict_iterate(f->streams[i]->decoder_opts, e)))
1596 21885 av_dict_set(&unused_opts, e->key, NULL, 0);
1597 }
1598
1599 6598 e = NULL;
1600
2/2
✓ Branch 1 taken 401 times.
✓ Branch 2 taken 6598 times.
6999 while ((e = av_dict_iterate(unused_opts, e))) {
1601 401 const AVClass *class = avcodec_get_class();
1602 401 const AVOption *option = av_opt_find(&class, e->key, NULL, 0,
1603 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ);
1604 401 const AVClass *fclass = avformat_get_class();
1605 401 const AVOption *foption = av_opt_find(&fclass, e->key, NULL, 0,
1606 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ);
1607
2/4
✓ Branch 0 taken 401 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 401 times.
401 if (!option || foption)
1608 continue;
1609
1610
1611
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 401 times.
401 if (!(option->flags & AV_OPT_FLAG_DECODING_PARAM)) {
1612 av_log(d, AV_LOG_ERROR, "Codec AVOption %s (%s) is not a decoding "
1613 "option.\n", e->key, option->help ? option->help : "");
1614 exit_program(1);
1615 }
1616
1617 401 av_log(d, AV_LOG_WARNING, "Codec AVOption %s (%s) has not been used "
1618 "for any stream. The most likely reason is either wrong type "
1619 "(e.g. a video option with no video streams) or that it is a "
1620 "private option of some decoder which was not actually used "
1621
1/2
✓ Branch 0 taken 401 times.
✗ Branch 1 not taken.
802 "for any stream.\n", e->key, option->help ? option->help : "");
1622 }
1623 6598 av_dict_free(&unused_opts);
1624
1625
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6598 times.
6598 for (i = 0; i < o->nb_dump_attachment; i++) {
1626 int j;
1627
1628 for (j = 0; j < f->nb_streams; j++) {
1629 InputStream *ist = f->streams[j];
1630
1631 if (check_stream_specifier(ic, ist->st, o->dump_attachment[i].specifier) == 1)
1632 dump_attachment(ist, o->dump_attachment[i].u.str);
1633 }
1634 }
1635
1636 6598 return 0;
1637 }
1638