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