2 * This file is part of FFmpeg.
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.
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.
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
23 #include "ffmpeg_sched.h"
24 #include "ffmpeg_utils.h"
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"
38 #include "libavcodec/bsf.h"
39 #include "libavcodec/packet.h"
41 #include "libavformat/avformat.h"
43 typedef struct DemuxStream
{
46 // name used for logging
54 /* non zero if the packets must be decoded in 'raw_fifo', see DECODING_FOR_* */
56 #define DECODING_FOR_OST 1
57 #define DECODING_FOR_FILTER 2
59 /* true if stream data should be discarded */
62 // scheduler returned EOF for this stream
65 int streamcopy_needed
;
72 int wrap_correction_done
;
74 ///< dts of the first packet read for this stream (in AV_TIME_BASE units)
77 /* predicted dts of the next packet read for this stream or (when there are
78 * several frames in a packet) of the next frame in current packet (in AV_TIME_BASE units) */
80 ///< dts of the last packet read for this stream (in AV_TIME_BASE units)
83 const AVCodecDescriptor
*codec_desc
;
85 AVDictionary
*decoder_opts
;
88 // decoded media properties, as estimated by opening the decoder
89 AVFrame
*decoded_params
;
93 /* number of packets successfully read for this stream */
95 // combined size of all the packets read
99 typedef struct Demuxer
{
102 // name used for logging
105 int64_t wallclock_start
;
108 * Extra timestamp offset added by discontinuity handling.
110 int64_t ts_offset_discont
;
113 int64_t recording_time
;
116 /* number of times input stream should be looped */
119 /* duration of the looped segment of the input file */
121 /* pts with the smallest/largest values ever seen */
125 /* number of streams that the user was warned of */
129 double readrate_initial_burst
;
133 AVPacket
*pkt_heartbeat
;
137 int nb_streams_finished
;
140 typedef struct DemuxThreadContext
{
141 // packet used for reading from the demuxer
143 // packet for reading from BSFs
145 } DemuxThreadContext
;
147 static DemuxStream
*ds_from_ist(InputStream
*ist
)
149 return (DemuxStream
*)ist
;
152 static Demuxer
*demuxer_from_ifile(InputFile
*f
)
157 InputStream
*ist_find_unused(enum AVMediaType type
)
159 for (InputStream
*ist
= ist_iter(NULL
); ist
; ist
= ist_iter(ist
)) {
160 DemuxStream
*ds
= ds_from_ist(ist
);
161 if (ist
->par
->codec_type
== type
&& ds
->discard
&&
162 ist
->user_set_discard
!= AVDISCARD_ALL
)
168 static void report_new_stream(Demuxer
*d
, const AVPacket
*pkt
)
170 const AVStream
*st
= d
->f
.ctx
->streams
[pkt
->stream_index
];
172 if (pkt
->stream_index
< d
->nb_streams_warn
)
174 av_log(d
, AV_LOG_WARNING
,
175 "New %s stream with index %d at pos:%"PRId64
" and DTS:%ss\n",
176 av_get_media_type_string(st
->codecpar
->codec_type
),
177 pkt
->stream_index
, pkt
->pos
, av_ts2timestr(pkt
->dts
, &st
->time_base
));
178 d
->nb_streams_warn
= pkt
->stream_index
+ 1;
181 static int seek_to_start(Demuxer
*d
, Timestamp end_pts
)
183 InputFile
*ifile
= &d
->f
;
184 AVFormatContext
*is
= ifile
->ctx
;
187 ret
= avformat_seek_file(is
, -1, INT64_MIN
, is
->start_time
, is
->start_time
, 0);
191 if (end_pts
.ts
!= AV_NOPTS_VALUE
&&
192 (d
->max_pts
.ts
== AV_NOPTS_VALUE
||
193 av_compare_ts(d
->max_pts
.ts
, d
->max_pts
.tb
, end_pts
.ts
, end_pts
.tb
) < 0))
194 d
->max_pts
= end_pts
;
196 if (d
->max_pts
.ts
!= AV_NOPTS_VALUE
) {
197 int64_t min_pts
= d
->min_pts
.ts
== AV_NOPTS_VALUE
? 0 : d
->min_pts
.ts
;
198 d
->duration
.ts
= d
->max_pts
.ts
- av_rescale_q(min_pts
, d
->min_pts
.tb
, d
->max_pts
.tb
);
200 d
->duration
.tb
= d
->max_pts
.tb
;
208 static void ts_discontinuity_detect(Demuxer
*d
, InputStream
*ist
,
211 InputFile
*ifile
= &d
->f
;
212 DemuxStream
*ds
= ds_from_ist(ist
);
213 const int fmt_is_discont
= ifile
->ctx
->iformat
->flags
& AVFMT_TS_DISCONT
;
214 int disable_discontinuity_correction
= copy_ts
;
215 int64_t pkt_dts
= av_rescale_q_rnd(pkt
->dts
, pkt
->time_base
, AV_TIME_BASE_Q
,
216 AV_ROUND_NEAR_INF
| AV_ROUND_PASS_MINMAX
);
218 if (copy_ts
&& ds
->next_dts
!= AV_NOPTS_VALUE
&&
219 fmt_is_discont
&& ist
->st
->pts_wrap_bits
< 60) {
220 int64_t wrap_dts
= av_rescale_q_rnd(pkt
->dts
+ (1LL<<ist
->st
->pts_wrap_bits
),
221 pkt
->time_base
, AV_TIME_BASE_Q
,
222 AV_ROUND_NEAR_INF
|AV_ROUND_PASS_MINMAX
);
223 if (FFABS(wrap_dts
- ds
->next_dts
) < FFABS(pkt_dts
- ds
->next_dts
)/10)
224 disable_discontinuity_correction
= 0;
227 if (ds
->next_dts
!= AV_NOPTS_VALUE
&& !disable_discontinuity_correction
) {
228 int64_t delta
= pkt_dts
- ds
->next_dts
;
229 if (fmt_is_discont
) {
230 if (FFABS(delta
) > 1LL * dts_delta_threshold
* AV_TIME_BASE
||
231 pkt_dts
+ AV_TIME_BASE
/10 < ds
->dts
) {
232 d
->ts_offset_discont
-= delta
;
233 av_log(ist
, AV_LOG_WARNING
,
234 "timestamp discontinuity "
235 "(stream id=%d): %"PRId64
", new offset= %"PRId64
"\n",
236 ist
->st
->id
, delta
, d
->ts_offset_discont
);
237 pkt
->dts
-= av_rescale_q(delta
, AV_TIME_BASE_Q
, pkt
->time_base
);
238 if (pkt
->pts
!= AV_NOPTS_VALUE
)
239 pkt
->pts
-= av_rescale_q(delta
, AV_TIME_BASE_Q
, pkt
->time_base
);
242 if (FFABS(delta
) > 1LL * dts_error_threshold
* AV_TIME_BASE
) {
243 av_log(ist
, AV_LOG_WARNING
,
244 "DTS %"PRId64
", next:%"PRId64
" st:%d invalid dropping\n",
245 pkt
->dts
, ds
->next_dts
, pkt
->stream_index
);
246 pkt
->dts
= AV_NOPTS_VALUE
;
248 if (pkt
->pts
!= AV_NOPTS_VALUE
){
249 int64_t pkt_pts
= av_rescale_q(pkt
->pts
, pkt
->time_base
, AV_TIME_BASE_Q
);
250 delta
= pkt_pts
- ds
->next_dts
;
251 if (FFABS(delta
) > 1LL * dts_error_threshold
* AV_TIME_BASE
) {
252 av_log(ist
, AV_LOG_WARNING
,
253 "PTS %"PRId64
", next:%"PRId64
" invalid dropping st:%d\n",
254 pkt
->pts
, ds
->next_dts
, pkt
->stream_index
);
255 pkt
->pts
= AV_NOPTS_VALUE
;
259 } else if (ds
->next_dts
== AV_NOPTS_VALUE
&& !copy_ts
&&
260 fmt_is_discont
&& d
->last_ts
!= AV_NOPTS_VALUE
) {
261 int64_t delta
= pkt_dts
- d
->last_ts
;
262 if (FFABS(delta
) > 1LL * dts_delta_threshold
* AV_TIME_BASE
) {
263 d
->ts_offset_discont
-= delta
;
264 av_log(ist
, AV_LOG_DEBUG
,
265 "Inter stream timestamp discontinuity %"PRId64
", new offset= %"PRId64
"\n",
266 delta
, d
->ts_offset_discont
);
267 pkt
->dts
-= av_rescale_q(delta
, AV_TIME_BASE_Q
, pkt
->time_base
);
268 if (pkt
->pts
!= AV_NOPTS_VALUE
)
269 pkt
->pts
-= av_rescale_q(delta
, AV_TIME_BASE_Q
, pkt
->time_base
);
273 d
->last_ts
= av_rescale_q(pkt
->dts
, pkt
->time_base
, AV_TIME_BASE_Q
);
276 static void ts_discontinuity_process(Demuxer
*d
, InputStream
*ist
,
279 int64_t offset
= av_rescale_q(d
->ts_offset_discont
, AV_TIME_BASE_Q
,
282 // apply previously-detected timestamp-discontinuity offset
283 // (to all streams, not just audio/video)
284 if (pkt
->dts
!= AV_NOPTS_VALUE
)
286 if (pkt
->pts
!= AV_NOPTS_VALUE
)
289 // detect timestamp discontinuities for audio/video
290 if ((ist
->par
->codec_type
== AVMEDIA_TYPE_VIDEO
||
291 ist
->par
->codec_type
== AVMEDIA_TYPE_AUDIO
) &&
292 pkt
->dts
!= AV_NOPTS_VALUE
)
293 ts_discontinuity_detect(d
, ist
, pkt
);
296 static int ist_dts_update(DemuxStream
*ds
, AVPacket
*pkt
, FrameData
*fd
)
298 InputStream
*ist
= &ds
->ist
;
299 const AVCodecParameters
*par
= ist
->par
;
301 if (!ds
->saw_first_ts
) {
303 ds
->dts
= ist
->st
->avg_frame_rate
.num
? - ist
->par
->video_delay
* AV_TIME_BASE
/ av_q2d(ist
->st
->avg_frame_rate
) : 0;
304 if (pkt
->pts
!= AV_NOPTS_VALUE
) {
306 ds
->dts
+= av_rescale_q(pkt
->pts
, pkt
->time_base
, AV_TIME_BASE_Q
);
308 ds
->saw_first_ts
= 1;
311 if (ds
->next_dts
== AV_NOPTS_VALUE
)
312 ds
->next_dts
= ds
->dts
;
314 if (pkt
->dts
!= AV_NOPTS_VALUE
)
315 ds
->next_dts
= ds
->dts
= av_rescale_q(pkt
->dts
, pkt
->time_base
, AV_TIME_BASE_Q
);
317 ds
->dts
= ds
->next_dts
;
318 switch (par
->codec_type
) {
319 case AVMEDIA_TYPE_AUDIO
:
320 av_assert1(pkt
->duration
>= 0);
321 if (par
->sample_rate
) {
322 ds
->next_dts
+= ((int64_t)AV_TIME_BASE
* par
->frame_size
) /
325 ds
->next_dts
+= av_rescale_q(pkt
->duration
, pkt
->time_base
, AV_TIME_BASE_Q
);
328 case AVMEDIA_TYPE_VIDEO
:
329 if (ist
->framerate
.num
) {
330 // TODO: Remove work-around for c99-to-c89 issue 7
331 AVRational time_base_q
= AV_TIME_BASE_Q
;
332 int64_t next_dts
= av_rescale_q(ds
->next_dts
, time_base_q
, av_inv_q(ist
->framerate
));
333 ds
->next_dts
= av_rescale_q(next_dts
+ 1, av_inv_q(ist
->framerate
), time_base_q
);
334 } else if (pkt
->duration
) {
335 ds
->next_dts
+= av_rescale_q(pkt
->duration
, pkt
->time_base
, AV_TIME_BASE_Q
);
336 } else if (ist
->par
->framerate
.num
!= 0) {
337 AVRational field_rate
= av_mul_q(ist
->par
->framerate
,
338 (AVRational
){ 2, 1 });
341 if (ds
->codec_desc
&&
342 (ds
->codec_desc
->props
& AV_CODEC_PROP_FIELDS
) &&
343 av_stream_get_parser(ist
->st
))
344 fields
= 1 + av_stream_get_parser(ist
->st
)->repeat_pict
;
346 ds
->next_dts
+= av_rescale_q(fields
, av_inv_q(field_rate
), AV_TIME_BASE_Q
);
351 fd
->dts_est
= ds
->dts
;
356 static int ts_fixup(Demuxer
*d
, AVPacket
*pkt
, FrameData
*fd
)
358 InputFile
*ifile
= &d
->f
;
359 InputStream
*ist
= ifile
->streams
[pkt
->stream_index
];
360 DemuxStream
*ds
= ds_from_ist(ist
);
361 const int64_t start_time
= ifile
->start_time_effective
;
365 pkt
->time_base
= ist
->st
->time_base
;
367 #define SHOW_TS_DEBUG(tag_) \
369 av_log(ist, AV_LOG_INFO, "%s -> ist_index:%d:%d type:%s " \
370 "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s duration:%s duration_time:%s\n", \
371 tag_, ifile->index, pkt->stream_index, \
372 av_get_media_type_string(ist->st->codecpar->codec_type), \
373 av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &pkt->time_base), \
374 av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &pkt->time_base), \
375 av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &pkt->time_base)); \
378 SHOW_TS_DEBUG("demuxer");
380 if (!ds
->wrap_correction_done
&& start_time
!= AV_NOPTS_VALUE
&&
381 ist
->st
->pts_wrap_bits
< 64) {
382 int64_t stime
, stime2
;
384 stime
= av_rescale_q(start_time
, AV_TIME_BASE_Q
, pkt
->time_base
);
385 stime2
= stime
+ (1ULL<<ist
->st
->pts_wrap_bits
);
386 ds
->wrap_correction_done
= 1;
388 if(stime2
> stime
&& pkt
->dts
!= AV_NOPTS_VALUE
&& pkt
->dts
> stime
+ (1LL<<(ist
->st
->pts_wrap_bits
-1))) {
389 pkt
->dts
-= 1ULL<<ist
->st
->pts_wrap_bits
;
390 ds
->wrap_correction_done
= 0;
392 if(stime2
> stime
&& pkt
->pts
!= AV_NOPTS_VALUE
&& pkt
->pts
> stime
+ (1LL<<(ist
->st
->pts_wrap_bits
-1))) {
393 pkt
->pts
-= 1ULL<<ist
->st
->pts_wrap_bits
;
394 ds
->wrap_correction_done
= 0;
398 if (pkt
->dts
!= AV_NOPTS_VALUE
)
399 pkt
->dts
+= av_rescale_q(ifile
->ts_offset
, AV_TIME_BASE_Q
, pkt
->time_base
);
400 if (pkt
->pts
!= AV_NOPTS_VALUE
)
401 pkt
->pts
+= av_rescale_q(ifile
->ts_offset
, AV_TIME_BASE_Q
, pkt
->time_base
);
403 if (pkt
->pts
!= AV_NOPTS_VALUE
)
404 pkt
->pts
*= ds
->ts_scale
;
405 if (pkt
->dts
!= AV_NOPTS_VALUE
)
406 pkt
->dts
*= ds
->ts_scale
;
408 duration
= av_rescale_q(d
->duration
.ts
, d
->duration
.tb
, pkt
->time_base
);
409 if (pkt
->pts
!= AV_NOPTS_VALUE
) {
410 // audio decoders take precedence for estimating total file duration
411 int64_t pkt_duration
= d
->have_audio_dec
? 0 : pkt
->duration
;
413 pkt
->pts
+= duration
;
415 // update max/min pts that will be used to compute total file duration
416 // when using -stream_loop
417 if (d
->max_pts
.ts
== AV_NOPTS_VALUE
||
418 av_compare_ts(d
->max_pts
.ts
, d
->max_pts
.tb
,
419 pkt
->pts
+ pkt_duration
, pkt
->time_base
) < 0) {
420 d
->max_pts
= (Timestamp
){ .ts
= pkt
->pts
+ pkt_duration
,
421 .tb
= pkt
->time_base
};
423 if (d
->min_pts
.ts
== AV_NOPTS_VALUE
||
424 av_compare_ts(d
->min_pts
.ts
, d
->min_pts
.tb
,
425 pkt
->pts
, pkt
->time_base
) > 0) {
426 d
->min_pts
= (Timestamp
){ .ts
= pkt
->pts
,
427 .tb
= pkt
->time_base
};
431 if (pkt
->dts
!= AV_NOPTS_VALUE
)
432 pkt
->dts
+= duration
;
434 SHOW_TS_DEBUG("demuxer+tsfixup");
436 // detect and try to correct for timestamp discontinuities
437 ts_discontinuity_process(d
, ist
, pkt
);
439 // update estimated/predicted dts
440 ret
= ist_dts_update(ds
, pkt
, fd
);
447 static int input_packet_process(Demuxer
*d
, AVPacket
*pkt
, unsigned *send_flags
)
449 InputFile
*f
= &d
->f
;
450 InputStream
*ist
= f
->streams
[pkt
->stream_index
];
451 DemuxStream
*ds
= ds_from_ist(ist
);
455 fd
= packet_data(pkt
);
457 return AVERROR(ENOMEM
);
459 ret
= ts_fixup(d
, pkt
, fd
);
463 if (d
->recording_time
!= INT64_MAX
) {
464 int64_t start_time
= 0;
466 start_time
+= f
->start_time
!= AV_NOPTS_VALUE
? f
->start_time
: 0;
467 start_time
+= start_at_zero
? 0 : f
->start_time_effective
;
469 if (ds
->dts
>= d
->recording_time
+ start_time
)
470 *send_flags
|= DEMUX_SEND_STREAMCOPY_EOF
;
473 ds
->data_size
+= pkt
->size
;
476 fd
->wallclock
[LATENCY_PROBE_DEMUX
] = av_gettime_relative();
479 av_log(ist
, AV_LOG_INFO
, "demuxer+ffmpeg -> ist_index:%d:%d type:%s pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s duration:%s duration_time:%s off:%s off_time:%s\n",
480 f
->index
, pkt
->stream_index
,
481 av_get_media_type_string(ist
->par
->codec_type
),
482 av_ts2str(pkt
->pts
), av_ts2timestr(pkt
->pts
, &pkt
->time_base
),
483 av_ts2str(pkt
->dts
), av_ts2timestr(pkt
->dts
, &pkt
->time_base
),
484 av_ts2str(pkt
->duration
), av_ts2timestr(pkt
->duration
, &pkt
->time_base
),
485 av_ts2str(f
->ts_offset
), av_ts2timestr(f
->ts_offset
, &AV_TIME_BASE_Q
));
491 static void readrate_sleep(Demuxer
*d
)
493 InputFile
*f
= &d
->f
;
494 int64_t file_start
= copy_ts
* (
495 (f
->start_time_effective
!= AV_NOPTS_VALUE
? f
->start_time_effective
* !start_at_zero
: 0) +
496 (f
->start_time
!= AV_NOPTS_VALUE
? f
->start_time
: 0)
498 int64_t burst_until
= AV_TIME_BASE
* d
->readrate_initial_burst
;
499 for (int i
= 0; i
< f
->nb_streams
; i
++) {
500 InputStream
*ist
= f
->streams
[i
];
501 DemuxStream
*ds
= ds_from_ist(ist
);
502 int64_t stream_ts_offset
, pts
, now
;
503 stream_ts_offset
= FFMAX(ds
->first_dts
!= AV_NOPTS_VALUE
? ds
->first_dts
: 0, file_start
);
504 pts
= av_rescale(ds
->dts
, 1000000, AV_TIME_BASE
);
505 now
= (av_gettime_relative() - d
->wallclock_start
) * d
->readrate
+ stream_ts_offset
;
506 if (pts
- burst_until
> now
)
507 av_usleep(pts
- burst_until
- now
);
511 static int do_send(Demuxer
*d
, DemuxStream
*ds
, AVPacket
*pkt
, unsigned flags
,
512 const char *pkt_desc
)
516 pkt
->stream_index
= ds
->sch_idx_stream
;
518 ret
= sch_demux_send(d
->sch
, d
->f
.index
, pkt
, flags
);
519 if (ret
== AVERROR_EOF
) {
520 av_packet_unref(pkt
);
522 av_log(ds
, AV_LOG_VERBOSE
, "All consumers of this stream are done\n");
525 if (++d
->nb_streams_finished
== d
->nb_streams_used
) {
526 av_log(d
, AV_LOG_VERBOSE
, "All consumers are done\n");
529 } else if (ret
< 0) {
530 if (ret
!= AVERROR_EXIT
)
531 av_log(d
, AV_LOG_ERROR
,
532 "Unable to send %s packet to consumers: %s\n",
533 pkt_desc
, av_err2str(ret
));
540 static int demux_send(Demuxer
*d
, DemuxThreadContext
*dt
, DemuxStream
*ds
,
541 AVPacket
*pkt
, unsigned flags
)
543 InputFile
*f
= &d
->f
;
546 // pkt can be NULL only when flushing BSFs
547 av_assert0(ds
->bsf
|| pkt
);
549 // send heartbeat for sub2video streams
550 if (d
->pkt_heartbeat
&& pkt
&& pkt
->pts
!= AV_NOPTS_VALUE
) {
551 for (int i
= 0; i
< f
->nb_streams
; i
++) {
552 DemuxStream
*ds1
= ds_from_ist(f
->streams
[i
]);
554 if (ds1
->finished
|| !ds1
->have_sub2video
)
557 d
->pkt_heartbeat
->pts
= pkt
->pts
;
558 d
->pkt_heartbeat
->time_base
= pkt
->time_base
;
559 d
->pkt_heartbeat
->opaque
= (void*)(intptr_t)PKT_OPAQUE_SUB_HEARTBEAT
;
561 ret
= do_send(d
, ds1
, d
->pkt_heartbeat
, 0, "heartbeat");
569 av_packet_rescale_ts(pkt
, pkt
->time_base
, ds
->bsf
->time_base_in
);
571 ret
= av_bsf_send_packet(ds
->bsf
, pkt
);
574 av_packet_unref(pkt
);
575 av_log(ds
, AV_LOG_ERROR
, "Error submitting a packet for filtering: %s\n",
581 ret
= av_bsf_receive_packet(ds
->bsf
, dt
->pkt_bsf
);
582 if (ret
== AVERROR(EAGAIN
))
585 if (ret
!= AVERROR_EOF
)
586 av_log(ds
, AV_LOG_ERROR
,
587 "Error applying bitstream filters to a packet: %s\n",
592 dt
->pkt_bsf
->time_base
= ds
->bsf
->time_base_out
;
594 ret
= do_send(d
, ds
, dt
->pkt_bsf
, 0, "filtered");
596 av_packet_unref(dt
->pkt_bsf
);
601 ret
= do_send(d
, ds
, pkt
, flags
, "demuxed");
609 static int demux_bsf_flush(Demuxer
*d
, DemuxThreadContext
*dt
)
611 InputFile
*f
= &d
->f
;
614 for (unsigned i
= 0; i
< f
->nb_streams
; i
++) {
615 DemuxStream
*ds
= ds_from_ist(f
->streams
[i
]);
620 ret
= demux_send(d
, dt
, ds
, NULL
, 0);
621 ret
= (ret
== AVERROR_EOF
) ? 0 : (ret
< 0) ? ret
: AVERROR_BUG
;
623 av_log(ds
, AV_LOG_ERROR
, "Error flushing BSFs: %s\n",
628 av_bsf_flush(ds
->bsf
);
634 static void discard_unused_programs(InputFile
*ifile
)
636 for (int j
= 0; j
< ifile
->ctx
->nb_programs
; j
++) {
637 AVProgram
*p
= ifile
->ctx
->programs
[j
];
638 int discard
= AVDISCARD_ALL
;
640 for (int k
= 0; k
< p
->nb_stream_indexes
; k
++) {
641 DemuxStream
*ds
= ds_from_ist(ifile
->streams
[p
->stream_index
[k
]]);
644 discard
= AVDISCARD_DEFAULT
;
648 p
->discard
= discard
;
652 static void thread_set_name(InputFile
*f
)
655 snprintf(name
, sizeof(name
), "dmx%d:%s", f
->index
, f
->ctx
->iformat
->name
);
656 ff_thread_setname(name
);
659 static void demux_thread_uninit(DemuxThreadContext
*dt
)
661 av_packet_free(&dt
->pkt_demux
);
662 av_packet_free(&dt
->pkt_bsf
);
664 memset(dt
, 0, sizeof(*dt
));
667 static int demux_thread_init(DemuxThreadContext
*dt
)
669 memset(dt
, 0, sizeof(*dt
));
671 dt
->pkt_demux
= av_packet_alloc();
673 return AVERROR(ENOMEM
);
675 dt
->pkt_bsf
= av_packet_alloc();
677 return AVERROR(ENOMEM
);
682 static int input_thread(void *arg
)
685 InputFile
*f
= &d
->f
;
687 DemuxThreadContext dt
;
691 ret
= demux_thread_init(&dt
);
697 discard_unused_programs(f
);
700 d
->wallclock_start
= av_gettime_relative();
704 unsigned send_flags
= 0;
706 ret
= av_read_frame(f
->ctx
, dt
.pkt_demux
);
708 if (ret
== AVERROR(EAGAIN
)) {
715 if (ret
== AVERROR_EOF
)
716 av_log(d
, AV_LOG_VERBOSE
, "EOF while reading input\n");
718 av_log(d
, AV_LOG_ERROR
, "Error during demuxing: %s\n",
720 ret
= exit_on_error
? ret
: 0;
723 ret_bsf
= demux_bsf_flush(d
, &dt
);
724 ret
= err_merge(ret
== AVERROR_EOF
? 0 : ret
, ret_bsf
);
727 /* signal looping to our consumers */
728 dt
.pkt_demux
->stream_index
= -1;
729 ret
= sch_demux_send(d
->sch
, f
->index
, dt
.pkt_demux
, 0);
731 ret
= seek_to_start(d
, (Timestamp
){ .ts
= dt
.pkt_demux
->pts
,
732 .tb
= dt
.pkt_demux
->time_base
});
736 /* fallthrough to the error path */
743 av_pkt_dump_log2(NULL
, AV_LOG_INFO
, dt
.pkt_demux
, do_hex_dump
,
744 f
->ctx
->streams
[dt
.pkt_demux
->stream_index
]);
747 /* the following test is needed in case new streams appear
748 dynamically in stream : we ignore them */
749 ds
= dt
.pkt_demux
->stream_index
< f
->nb_streams
?
750 ds_from_ist(f
->streams
[dt
.pkt_demux
->stream_index
]) : NULL
;
751 if (!ds
|| ds
->discard
|| ds
->finished
) {
752 report_new_stream(d
, dt
.pkt_demux
);
753 av_packet_unref(dt
.pkt_demux
);
757 if (dt
.pkt_demux
->flags
& AV_PKT_FLAG_CORRUPT
) {
758 av_log(d
, exit_on_error
? AV_LOG_FATAL
: AV_LOG_WARNING
,
759 "corrupt input packet in stream %d\n",
760 dt
.pkt_demux
->stream_index
);
762 av_packet_unref(dt
.pkt_demux
);
763 ret
= AVERROR_INVALIDDATA
;
768 ret
= input_packet_process(d
, dt
.pkt_demux
, &send_flags
);
775 ret
= demux_send(d
, &dt
, ds
, dt
.pkt_demux
, send_flags
);
780 // EOF/EXIT is normal termination
781 if (ret
== AVERROR_EOF
|| ret
== AVERROR_EXIT
)
785 demux_thread_uninit(&dt
);
790 static void demux_final_stats(Demuxer
*d
)
792 InputFile
*f
= &d
->f
;
793 uint64_t total_packets
= 0, total_size
= 0;
795 av_log(f
, AV_LOG_VERBOSE
, "Input file #%d (%s):\n",
796 f
->index
, f
->ctx
->url
);
798 for (int j
= 0; j
< f
->nb_streams
; j
++) {
799 InputStream
*ist
= f
->streams
[j
];
800 DemuxStream
*ds
= ds_from_ist(ist
);
801 enum AVMediaType type
= ist
->par
->codec_type
;
803 if (ds
->discard
|| type
== AVMEDIA_TYPE_ATTACHMENT
)
806 total_size
+= ds
->data_size
;
807 total_packets
+= ds
->nb_packets
;
809 av_log(f
, AV_LOG_VERBOSE
, " Input stream #%d:%d (%s): ",
810 f
->index
, j
, av_get_media_type_string(type
));
811 av_log(f
, AV_LOG_VERBOSE
, "%"PRIu64
" packets read (%"PRIu64
" bytes); ",
812 ds
->nb_packets
, ds
->data_size
);
814 if (ds
->decoding_needed
) {
815 av_log(f
, AV_LOG_VERBOSE
,
816 "%"PRIu64
" frames decoded; %"PRIu64
" decode errors",
817 ist
->decoder
->frames_decoded
, ist
->decoder
->decode_errors
);
818 if (type
== AVMEDIA_TYPE_AUDIO
)
819 av_log(f
, AV_LOG_VERBOSE
, " (%"PRIu64
" samples)", ist
->decoder
->samples_decoded
);
820 av_log(f
, AV_LOG_VERBOSE
, "; ");
823 av_log(f
, AV_LOG_VERBOSE
, "\n");
826 av_log(f
, AV_LOG_VERBOSE
, " Total: %"PRIu64
" packets (%"PRIu64
" bytes) demuxed\n",
827 total_packets
, total_size
);
830 static void ist_free(InputStream
**pist
)
832 InputStream
*ist
= *pist
;
837 ds
= ds_from_ist(ist
);
839 dec_free(&ist
->decoder
);
841 av_dict_free(&ds
->decoder_opts
);
842 av_freep(&ist
->filters
);
843 av_freep(&ds
->dec_opts
.hwaccel_device
);
845 avcodec_parameters_free(&ist
->par
);
847 av_frame_free(&ds
->decoded_params
);
849 av_bsf_free(&ds
->bsf
);
854 void ifile_close(InputFile
**pf
)
857 Demuxer
*d
= demuxer_from_ifile(f
);
863 demux_final_stats(d
);
865 for (int i
= 0; i
< f
->nb_streams
; i
++)
866 ist_free(&f
->streams
[i
]);
867 av_freep(&f
->streams
);
869 avformat_close_input(&f
->ctx
);
871 av_packet_free(&d
->pkt_heartbeat
);
876 int ist_use(InputStream
*ist
, int decoding_needed
,
877 const ViewSpecifier
*vs
, SchedulerNode
*src
)
879 Demuxer
*d
= demuxer_from_ifile(ist
->file
);
880 DemuxStream
*ds
= ds_from_ist(ist
);
883 if (ist
->user_set_discard
== AVDISCARD_ALL
) {
884 av_log(ist
, AV_LOG_ERROR
, "Cannot %s a disabled input stream\n",
885 decoding_needed
? "decode" : "streamcopy");
886 return AVERROR(EINVAL
);
889 if (decoding_needed
&& !ist
->dec
) {
890 av_log(ist
, AV_LOG_ERROR
,
891 "Decoding requested, but no decoder found for: %s\n",
892 avcodec_get_name(ist
->par
->codec_id
));
893 return AVERROR(EINVAL
);
896 if (ds
->sch_idx_stream
< 0) {
897 ret
= sch_add_demux_stream(d
->sch
, d
->f
.index
);
900 ds
->sch_idx_stream
= ret
;
905 d
->nb_streams_used
++;
908 ist
->st
->discard
= ist
->user_set_discard
;
909 ds
->decoding_needed
|= decoding_needed
;
910 ds
->streamcopy_needed
|= !decoding_needed
;
912 if (decoding_needed
&& ds
->sch_idx_dec
< 0) {
913 int is_audio
= ist
->st
->codecpar
->codec_type
== AVMEDIA_TYPE_AUDIO
;
915 ds
->dec_opts
.flags
|= (!!ist
->fix_sub_duration
* DECODER_FLAG_FIX_SUB_DURATION
) |
916 (!!(d
->f
.ctx
->iformat
->flags
& AVFMT_NOTIMESTAMPS
) * DECODER_FLAG_TS_UNRELIABLE
) |
917 (!!(d
->loop
&& is_audio
) * DECODER_FLAG_SEND_END_TS
)
919 | ((ist
->top_field_first
>= 0) * DECODER_FLAG_TOP_FIELD_FIRST
)
923 if (ist
->framerate
.num
) {
924 ds
->dec_opts
.flags
|= DECODER_FLAG_FRAMERATE_FORCED
;
925 ds
->dec_opts
.framerate
= ist
->framerate
;
927 ds
->dec_opts
.framerate
= ist
->st
->avg_frame_rate
;
929 if (ist
->dec
->id
== AV_CODEC_ID_DVB_SUBTITLE
&&
930 (ds
->decoding_needed
& DECODING_FOR_OST
)) {
931 av_dict_set(&ds
->decoder_opts
, "compute_edt", "1", AV_DICT_DONT_OVERWRITE
);
932 if (ds
->decoding_needed
& DECODING_FOR_FILTER
)
933 av_log(ist
, AV_LOG_WARNING
,
934 "Warning using DVB subtitles for filtering and output at the "
935 "same time is not fully supported, also see -compute_edt [0|1]\n");
938 snprintf(ds
->dec_name
, sizeof(ds
->dec_name
), "%d:%d", ist
->file
->index
, ist
->index
);
939 ds
->dec_opts
.name
= ds
->dec_name
;
941 ds
->dec_opts
.codec
= ist
->dec
;
942 ds
->dec_opts
.par
= ist
->par
;
944 ds
->dec_opts
.log_parent
= ist
;
946 ds
->decoded_params
= av_frame_alloc();
947 if (!ds
->decoded_params
)
948 return AVERROR(ENOMEM
);
950 ret
= dec_init(&ist
->decoder
, d
->sch
,
951 &ds
->decoder_opts
, &ds
->dec_opts
, ds
->decoded_params
);
954 ds
->sch_idx_dec
= ret
;
956 ret
= sch_connect(d
->sch
, SCH_DSTREAM(d
->f
.index
, ds
->sch_idx_stream
),
957 SCH_DEC_IN(ds
->sch_idx_dec
));
961 d
->have_audio_dec
|= is_audio
;
964 if (decoding_needed
&& ist
->par
->codec_type
== AVMEDIA_TYPE_VIDEO
) {
965 ret
= dec_request_view(ist
->decoder
, vs
, src
);
969 *src
= decoding_needed
?
970 SCH_DEC_OUT(ds
->sch_idx_dec
, 0) :
971 SCH_DSTREAM(d
->f
.index
, ds
->sch_idx_stream
);
977 int ist_filter_add(InputStream
*ist
, InputFilter
*ifilter
, int is_simple
,
978 const ViewSpecifier
*vs
, InputFilterOptions
*opts
,
981 Demuxer
*d
= demuxer_from_ifile(ist
->file
);
982 DemuxStream
*ds
= ds_from_ist(ist
);
983 int64_t tsoffset
= 0;
986 ret
= ist_use(ist
, is_simple
? DECODING_FOR_OST
: DECODING_FOR_FILTER
,
991 ret
= GROW_ARRAY(ist
->filters
, ist
->nb_filters
);
995 ist
->filters
[ist
->nb_filters
- 1] = ifilter
;
997 if (ist
->par
->codec_type
== AVMEDIA_TYPE_VIDEO
) {
998 const AVPacketSideData
*sd
= av_packet_side_data_get(ist
->par
->coded_side_data
,
999 ist
->par
->nb_coded_side_data
,
1000 AV_PKT_DATA_FRAME_CROPPING
);
1001 if (ist
->framerate
.num
> 0 && ist
->framerate
.den
> 0) {
1002 opts
->framerate
= ist
->framerate
;
1003 opts
->flags
|= IFILTER_FLAG_CFR
;
1005 opts
->framerate
= av_guess_frame_rate(d
->f
.ctx
, ist
->st
, NULL
);
1006 if (sd
&& sd
->size
>= sizeof(uint32_t) * 4) {
1007 opts
->crop_top
= AV_RL32(sd
->data
+ 0);
1008 opts
->crop_bottom
= AV_RL32(sd
->data
+ 4);
1009 opts
->crop_left
= AV_RL32(sd
->data
+ 8);
1010 opts
->crop_right
= AV_RL32(sd
->data
+ 12);
1011 if (ds
->apply_cropping
&& ds
->apply_cropping
!= CROP_CODEC
&&
1012 (opts
->crop_top
| opts
->crop_bottom
| opts
->crop_left
| opts
->crop_right
))
1013 opts
->flags
|= IFILTER_FLAG_CROP
;
1015 } else if (ist
->par
->codec_type
== AVMEDIA_TYPE_SUBTITLE
) {
1016 /* Compute the size of the canvas for the subtitles stream.
1017 If the subtitles codecpar has set a size, use it. Otherwise use the
1018 maximum dimensions of the video streams in the same file. */
1019 opts
->sub2video_width
= ist
->par
->width
;
1020 opts
->sub2video_height
= ist
->par
->height
;
1021 if (!(opts
->sub2video_width
&& opts
->sub2video_height
)) {
1022 for (int j
= 0; j
< d
->f
.nb_streams
; j
++) {
1023 AVCodecParameters
*par1
= d
->f
.streams
[j
]->par
;
1024 if (par1
->codec_type
== AVMEDIA_TYPE_VIDEO
) {
1025 opts
->sub2video_width
= FFMAX(opts
->sub2video_width
, par1
->width
);
1026 opts
->sub2video_height
= FFMAX(opts
->sub2video_height
, par1
->height
);
1031 if (!(opts
->sub2video_width
&& opts
->sub2video_height
)) {
1032 opts
->sub2video_width
= FFMAX(opts
->sub2video_width
, 720);
1033 opts
->sub2video_height
= FFMAX(opts
->sub2video_height
, 576);
1036 if (!d
->pkt_heartbeat
) {
1037 d
->pkt_heartbeat
= av_packet_alloc();
1038 if (!d
->pkt_heartbeat
)
1039 return AVERROR(ENOMEM
);
1041 ds
->have_sub2video
= 1;
1044 ret
= av_frame_copy_props(opts
->fallback
, ds
->decoded_params
);
1047 opts
->fallback
->format
= ds
->decoded_params
->format
;
1048 opts
->fallback
->width
= ds
->decoded_params
->width
;
1049 opts
->fallback
->height
= ds
->decoded_params
->height
;
1051 ret
= av_channel_layout_copy(&opts
->fallback
->ch_layout
, &ds
->decoded_params
->ch_layout
);
1056 tsoffset
= d
->f
.start_time
== AV_NOPTS_VALUE
? 0 : d
->f
.start_time
;
1057 if (!start_at_zero
&& d
->f
.ctx
->start_time
!= AV_NOPTS_VALUE
)
1058 tsoffset
+= d
->f
.ctx
->start_time
;
1060 opts
->trim_start_us
= ((d
->f
.start_time
== AV_NOPTS_VALUE
) || !d
->accurate_seek
) ?
1061 AV_NOPTS_VALUE
: tsoffset
;
1062 opts
->trim_end_us
= d
->recording_time
;
1064 opts
->name
= av_strdup(ds
->dec_name
);
1066 return AVERROR(ENOMEM
);
1068 opts
->flags
|= IFILTER_FLAG_AUTOROTATE
* !!(ds
->autorotate
) |
1069 IFILTER_FLAG_REINIT
* !!(ds
->reinit_filters
);
1074 static int choose_decoder(const OptionsContext
*o
, void *logctx
,
1075 AVFormatContext
*s
, AVStream
*st
,
1076 enum HWAccelID hwaccel_id
, enum AVHWDeviceType hwaccel_device_type
,
1077 const AVCodec
**pcodec
)
1080 const char *codec_name
= NULL
;
1082 opt_match_per_stream_str(logctx
, &o
->codec_names
, s
, st
, &codec_name
);
1084 int ret
= find_codec(NULL
, codec_name
, st
->codecpar
->codec_type
, 0, pcodec
);
1087 st
->codecpar
->codec_id
= (*pcodec
)->id
;
1088 if (recast_media
&& st
->codecpar
->codec_type
!= (*pcodec
)->type
)
1089 st
->codecpar
->codec_type
= (*pcodec
)->type
;
1092 if (st
->codecpar
->codec_type
== AVMEDIA_TYPE_VIDEO
&&
1093 hwaccel_id
== HWACCEL_GENERIC
&&
1094 hwaccel_device_type
!= AV_HWDEVICE_TYPE_NONE
) {
1098 while ((c
= av_codec_iterate(&i
))) {
1099 const AVCodecHWConfig
*config
;
1101 if (c
->id
!= st
->codecpar
->codec_id
||
1102 !av_codec_is_decoder(c
))
1105 for (int j
= 0; config
= avcodec_get_hw_config(c
, j
); j
++) {
1106 if (config
->device_type
== hwaccel_device_type
) {
1107 av_log(logctx
, AV_LOG_VERBOSE
, "Selecting decoder '%s' because of requested hwaccel method %s\n",
1108 c
->name
, av_hwdevice_get_type_name(hwaccel_device_type
));
1116 *pcodec
= avcodec_find_decoder(st
->codecpar
->codec_id
);
1121 static int guess_input_channel_layout(InputStream
*ist
, AVCodecParameters
*par
,
1122 int guess_layout_max
)
1124 if (par
->ch_layout
.order
== AV_CHANNEL_ORDER_UNSPEC
) {
1125 char layout_name
[256];
1127 if (par
->ch_layout
.nb_channels
> guess_layout_max
)
1129 av_channel_layout_default(&par
->ch_layout
, par
->ch_layout
.nb_channels
);
1130 if (par
->ch_layout
.order
== AV_CHANNEL_ORDER_UNSPEC
)
1132 av_channel_layout_describe(&par
->ch_layout
, layout_name
, sizeof(layout_name
));
1133 av_log(ist
, AV_LOG_WARNING
, "Guessed Channel Layout: %s\n", layout_name
);
1138 static int add_display_matrix_to_stream(const OptionsContext
*o
,
1139 AVFormatContext
*ctx
, InputStream
*ist
)
1141 AVStream
*st
= ist
->st
;
1142 AVPacketSideData
*sd
;
1143 double rotation
= DBL_MAX
;
1144 int hflip
= -1, vflip
= -1;
1145 int hflip_set
= 0, vflip_set
= 0, rotation_set
= 0;
1148 opt_match_per_stream_dbl(ist
, &o
->display_rotations
, ctx
, st
, &rotation
);
1149 opt_match_per_stream_int(ist
, &o
->display_hflips
, ctx
, st
, &hflip
);
1150 opt_match_per_stream_int(ist
, &o
->display_vflips
, ctx
, st
, &vflip
);
1152 rotation_set
= rotation
!= DBL_MAX
;
1153 hflip_set
= hflip
!= -1;
1154 vflip_set
= vflip
!= -1;
1156 if (!rotation_set
&& !hflip_set
&& !vflip_set
)
1159 sd
= av_packet_side_data_new(&st
->codecpar
->coded_side_data
,
1160 &st
->codecpar
->nb_coded_side_data
,
1161 AV_PKT_DATA_DISPLAYMATRIX
,
1162 sizeof(int32_t) * 9, 0);
1164 av_log(ist
, AV_LOG_FATAL
, "Failed to generate a display matrix!\n");
1165 return AVERROR(ENOMEM
);
1168 buf
= (int32_t *)sd
->data
;
1169 av_display_rotation_set(buf
,
1170 rotation_set
? -(rotation
) : -0.0f
);
1172 av_display_matrix_flip(buf
,
1173 hflip_set
? hflip
: 0,
1174 vflip_set
? vflip
: 0);
1179 static const char *input_stream_item_name(void *obj
)
1181 const DemuxStream
*ds
= obj
;
1183 return ds
->log_name
;
1186 static const AVClass input_stream_class
= {
1187 .class_name
= "InputStream",
1188 .version
= LIBAVUTIL_VERSION_INT
,
1189 .item_name
= input_stream_item_name
,
1190 .category
= AV_CLASS_CATEGORY_DEMUXER
,
1193 static DemuxStream
*demux_stream_alloc(Demuxer
*d
, AVStream
*st
)
1195 const char *type_str
= av_get_media_type_string(st
->codecpar
->codec_type
);
1196 InputFile
*f
= &d
->f
;
1199 ds
= allocate_array_elem(&f
->streams
, sizeof(*ds
), &f
->nb_streams
);
1203 ds
->sch_idx_stream
= -1;
1204 ds
->sch_idx_dec
= -1;
1208 ds
->ist
.index
= st
->index
;
1209 ds
->ist
.class = &input_stream_class
;
1211 snprintf(ds
->log_name
, sizeof(ds
->log_name
), "%cist#%d:%d/%s",
1212 type_str
? *type_str
: '?', d
->f
.index
, st
->index
,
1213 avcodec_get_name(st
->codecpar
->codec_id
));
1218 static int ist_add(const OptionsContext
*o
, Demuxer
*d
, AVStream
*st
, AVDictionary
**opts_used
)
1220 AVFormatContext
*ic
= d
->f
.ctx
;
1221 AVCodecParameters
*par
= st
->codecpar
;
1224 const char *framerate
= NULL
, *hwaccel_device
= NULL
;
1225 const char *hwaccel
= NULL
;
1226 const char *apply_cropping
= NULL
;
1227 const char *hwaccel_output_format
= NULL
;
1228 const char *codec_tag
= NULL
;
1229 const char *bsfs
= NULL
;
1231 const char *discard_str
= NULL
;
1234 ds
= demux_stream_alloc(d
, st
);
1236 return AVERROR(ENOMEM
);
1241 st
->discard
= AVDISCARD_ALL
;
1242 ds
->first_dts
= AV_NOPTS_VALUE
;
1243 ds
->next_dts
= AV_NOPTS_VALUE
;
1245 ds
->dec_opts
.time_base
= st
->time_base
;
1248 opt_match_per_stream_dbl(ist
, &o
->ts_scale
, ic
, st
, &ds
->ts_scale
);
1251 opt_match_per_stream_int(ist
, &o
->autorotate
, ic
, st
, &ds
->autorotate
);
1253 ds
->apply_cropping
= CROP_ALL
;
1254 opt_match_per_stream_str(ist
, &o
->apply_cropping
, ic
, st
, &apply_cropping
);
1255 if (apply_cropping
) {
1256 const AVOption opts
[] = {
1257 { "apply_cropping", NULL
, 0, AV_OPT_TYPE_INT
,
1258 { .i64
= CROP_ALL
}, CROP_DISABLED
, CROP_CONTAINER
, AV_OPT_FLAG_DECODING_PARAM
, .unit
= "apply_cropping" },
1259 { "none", NULL
, 0, AV_OPT_TYPE_CONST
, { .i64
= CROP_DISABLED
}, .unit
= "apply_cropping" },
1260 { "all", NULL
, 0, AV_OPT_TYPE_CONST
, { .i64
= CROP_ALL
}, .unit
= "apply_cropping" },
1261 { "codec", NULL
, 0, AV_OPT_TYPE_CONST
, { .i64
= CROP_CODEC
}, .unit
= "apply_cropping" },
1262 { "container", NULL
, 0, AV_OPT_TYPE_CONST
, { .i64
= CROP_CONTAINER
}, .unit
= "apply_cropping" },
1265 const AVClass
class = {
1266 .class_name
= "apply_cropping",
1267 .item_name
= av_default_item_name
,
1269 .version
= LIBAVUTIL_VERSION_INT
,
1271 const AVClass
*pclass
= &class;
1273 ret
= av_opt_eval_int(&pclass
, opts
, apply_cropping
, &ds
->apply_cropping
);
1275 av_log(ist
, AV_LOG_ERROR
, "Invalid apply_cropping value '%s'.\n", apply_cropping
);
1280 opt_match_per_stream_str(ist
, &o
->codec_tags
, ic
, st
, &codec_tag
);
1282 uint32_t tag
= strtol(codec_tag
, &next
, 0);
1284 uint8_t buf
[4] = { 0 };
1285 memcpy(buf
, codec_tag
, FFMIN(sizeof(buf
), strlen(codec_tag
)));
1289 st
->codecpar
->codec_tag
= tag
;
1292 if (st
->codecpar
->codec_type
== AVMEDIA_TYPE_VIDEO
) {
1293 ret
= add_display_matrix_to_stream(o
, ic
, ist
);
1297 opt_match_per_stream_str(ist
, &o
->hwaccels
, ic
, st
, &hwaccel
);
1298 opt_match_per_stream_str(ist
, &o
->hwaccel_output_formats
, ic
, st
,
1299 &hwaccel_output_format
);
1300 if (!hwaccel_output_format
&& hwaccel
&& !strcmp(hwaccel
, "cuvid")) {
1301 av_log(ist
, AV_LOG_WARNING
,
1302 "WARNING: defaulting hwaccel_output_format to cuda for compatibility "
1303 "with old commandlines. This behaviour is DEPRECATED and will be removed "
1304 "in the future. Please explicitly set \"-hwaccel_output_format cuda\".\n");
1305 ds
->dec_opts
.hwaccel_output_format
= AV_PIX_FMT_CUDA
;
1306 } else if (!hwaccel_output_format
&& hwaccel
&& !strcmp(hwaccel
, "qsv")) {
1307 av_log(ist
, AV_LOG_WARNING
,
1308 "WARNING: defaulting hwaccel_output_format to qsv for compatibility "
1309 "with old commandlines. This behaviour is DEPRECATED and will be removed "
1310 "in the future. Please explicitly set \"-hwaccel_output_format qsv\".\n");
1311 ds
->dec_opts
.hwaccel_output_format
= AV_PIX_FMT_QSV
;
1312 } else if (!hwaccel_output_format
&& hwaccel
&& !strcmp(hwaccel
, "mediacodec")) {
1313 // There is no real AVHWFrameContext implementation. Set
1314 // hwaccel_output_format to avoid av_hwframe_transfer_data error.
1315 ds
->dec_opts
.hwaccel_output_format
= AV_PIX_FMT_MEDIACODEC
;
1316 } else if (hwaccel_output_format
) {
1317 ds
->dec_opts
.hwaccel_output_format
= av_get_pix_fmt(hwaccel_output_format
);
1318 if (ds
->dec_opts
.hwaccel_output_format
== AV_PIX_FMT_NONE
) {
1319 av_log(ist
, AV_LOG_FATAL
, "Unrecognised hwaccel output "
1320 "format: %s", hwaccel_output_format
);
1323 ds
->dec_opts
.hwaccel_output_format
= AV_PIX_FMT_NONE
;
1327 // The NVDEC hwaccels use a CUDA device, so remap the name here.
1328 if (!strcmp(hwaccel
, "nvdec") || !strcmp(hwaccel
, "cuvid"))
1331 if (!strcmp(hwaccel
, "none"))
1332 ds
->dec_opts
.hwaccel_id
= HWACCEL_NONE
;
1333 else if (!strcmp(hwaccel
, "auto"))
1334 ds
->dec_opts
.hwaccel_id
= HWACCEL_AUTO
;
1336 enum AVHWDeviceType type
= av_hwdevice_find_type_by_name(hwaccel
);
1337 if (type
!= AV_HWDEVICE_TYPE_NONE
) {
1338 ds
->dec_opts
.hwaccel_id
= HWACCEL_GENERIC
;
1339 ds
->dec_opts
.hwaccel_device_type
= type
;
1342 if (!ds
->dec_opts
.hwaccel_id
) {
1343 av_log(ist
, AV_LOG_FATAL
, "Unrecognized hwaccel: %s.\n",
1345 av_log(ist
, AV_LOG_FATAL
, "Supported hwaccels: ");
1346 type
= AV_HWDEVICE_TYPE_NONE
;
1347 while ((type
= av_hwdevice_iterate_types(type
)) !=
1348 AV_HWDEVICE_TYPE_NONE
)
1349 av_log(ist
, AV_LOG_FATAL
, "%s ",
1350 av_hwdevice_get_type_name(type
));
1351 av_log(ist
, AV_LOG_FATAL
, "\n");
1352 return AVERROR(EINVAL
);
1357 opt_match_per_stream_str(ist
, &o
->hwaccel_devices
, ic
, st
, &hwaccel_device
);
1358 if (hwaccel_device
) {
1359 ds
->dec_opts
.hwaccel_device
= av_strdup(hwaccel_device
);
1360 if (!ds
->dec_opts
.hwaccel_device
)
1361 return AVERROR(ENOMEM
);
1365 ret
= choose_decoder(o
, ist
, ic
, st
, ds
->dec_opts
.hwaccel_id
,
1366 ds
->dec_opts
.hwaccel_device_type
, &ist
->dec
);
1371 ret
= filter_codec_opts(o
->g
->codec_opts
, ist
->st
->codecpar
->codec_id
,
1372 ic
, st
, ist
->dec
, &ds
->decoder_opts
, opts_used
);
1377 ds
->reinit_filters
= -1;
1378 opt_match_per_stream_int(ist
, &o
->reinit_filters
, ic
, st
, &ds
->reinit_filters
);
1380 ist
->user_set_discard
= AVDISCARD_NONE
;
1382 if ((o
->video_disable
&& ist
->st
->codecpar
->codec_type
== AVMEDIA_TYPE_VIDEO
) ||
1383 (o
->audio_disable
&& ist
->st
->codecpar
->codec_type
== AVMEDIA_TYPE_AUDIO
) ||
1384 (o
->subtitle_disable
&& ist
->st
->codecpar
->codec_type
== AVMEDIA_TYPE_SUBTITLE
) ||
1385 (o
->data_disable
&& ist
->st
->codecpar
->codec_type
== AVMEDIA_TYPE_DATA
))
1386 ist
->user_set_discard
= AVDISCARD_ALL
;
1388 opt_match_per_stream_str(ist
, &o
->discard
, ic
, st
, &discard_str
);
1390 ret
= av_opt_set(ist
->st
, "discard", discard_str
, 0);
1392 av_log(ist
, AV_LOG_ERROR
, "Error parsing discard %s.\n", discard_str
);
1395 ist
->user_set_discard
= ist
->st
->discard
;
1398 ds
->dec_opts
.flags
|= DECODER_FLAG_BITEXACT
* !!o
->bitexact
;
1400 av_dict_set_int(&ds
->decoder_opts
, "apply_cropping",
1401 ds
->apply_cropping
&& ds
->apply_cropping
!= CROP_CONTAINER
, 0);
1403 /* Attached pics are sparse, therefore we would not want to delay their decoding
1405 if (ist
->st
->disposition
& AV_DISPOSITION_ATTACHED_PIC
)
1406 av_dict_set(&ds
->decoder_opts
, "thread_type", "-frame", 0);
1408 switch (par
->codec_type
) {
1409 case AVMEDIA_TYPE_VIDEO
:
1410 opt_match_per_stream_str(ist
, &o
->frame_rates
, ic
, st
, &framerate
);
1412 ret
= av_parse_video_rate(&ist
->framerate
, framerate
);
1414 av_log(ist
, AV_LOG_ERROR
, "Error parsing framerate %s.\n",
1421 ist
->top_field_first
= -1;
1422 opt_match_per_stream_int(ist
, &o
->top_field_first
, ic
, st
, &ist
->top_field_first
);
1426 case AVMEDIA_TYPE_AUDIO
: {
1427 const char *ch_layout_str
= NULL
;
1429 opt_match_per_stream_str(ist
, &o
->audio_ch_layouts
, ic
, st
, &ch_layout_str
);
1430 if (ch_layout_str
) {
1431 AVChannelLayout ch_layout
;
1432 ret
= av_channel_layout_from_string(&ch_layout
, ch_layout_str
);
1434 av_log(ist
, AV_LOG_ERROR
, "Error parsing channel layout %s.\n", ch_layout_str
);
1437 if (par
->ch_layout
.nb_channels
<= 0 || par
->ch_layout
.nb_channels
== ch_layout
.nb_channels
) {
1438 av_channel_layout_uninit(&par
->ch_layout
);
1439 par
->ch_layout
= ch_layout
;
1441 av_log(ist
, AV_LOG_ERROR
,
1442 "Specified channel layout '%s' has %d channels, but input has %d channels.\n",
1443 ch_layout_str
, ch_layout
.nb_channels
, par
->ch_layout
.nb_channels
);
1444 av_channel_layout_uninit(&ch_layout
);
1445 return AVERROR(EINVAL
);
1448 int guess_layout_max
= INT_MAX
;
1449 opt_match_per_stream_int(ist
, &o
->guess_layout_max
, ic
, st
, &guess_layout_max
);
1450 guess_input_channel_layout(ist
, par
, guess_layout_max
);
1454 case AVMEDIA_TYPE_DATA
:
1455 case AVMEDIA_TYPE_SUBTITLE
: {
1456 const char *canvas_size
= NULL
;
1458 opt_match_per_stream_int(ist
, &o
->fix_sub_duration
, ic
, st
, &ist
->fix_sub_duration
);
1459 opt_match_per_stream_str(ist
, &o
->canvas_sizes
, ic
, st
, &canvas_size
);
1461 ret
= av_parse_video_size(&par
->width
, &par
->height
,
1464 av_log(ist
, AV_LOG_FATAL
, "Invalid canvas size: %s.\n", canvas_size
);
1470 case AVMEDIA_TYPE_ATTACHMENT
:
1471 case AVMEDIA_TYPE_UNKNOWN
:
1473 default: av_assert0(0);
1476 ist
->par
= avcodec_parameters_alloc();
1478 return AVERROR(ENOMEM
);
1480 ret
= avcodec_parameters_copy(ist
->par
, par
);
1482 av_log(ist
, AV_LOG_ERROR
, "Error exporting stream parameters.\n");
1486 if (ist
->st
->sample_aspect_ratio
.num
)
1487 ist
->par
->sample_aspect_ratio
= ist
->st
->sample_aspect_ratio
;
1489 opt_match_per_stream_str(ist
, &o
->bitstream_filters
, ic
, st
, &bsfs
);
1491 ret
= av_bsf_list_parse_str(bsfs
, &ds
->bsf
);
1493 av_log(ist
, AV_LOG_ERROR
,
1494 "Error parsing bitstream filter sequence '%s': %s\n",
1495 bsfs
, av_err2str(ret
));
1499 ret
= avcodec_parameters_copy(ds
->bsf
->par_in
, ist
->par
);
1502 ds
->bsf
->time_base_in
= ist
->st
->time_base
;
1504 ret
= av_bsf_init(ds
->bsf
);
1506 av_log(ist
, AV_LOG_ERROR
, "Error initializing bitstream filters: %s\n",
1511 ret
= avcodec_parameters_copy(ist
->par
, ds
->bsf
->par_out
);
1516 ds
->codec_desc
= avcodec_descriptor_get(ist
->par
->codec_id
);
1521 static int dump_attachment(InputStream
*ist
, const char *filename
)
1523 AVStream
*st
= ist
->st
;
1525 AVIOContext
*out
= NULL
;
1526 const AVDictionaryEntry
*e
;
1528 if (!st
->codecpar
->extradata_size
) {
1529 av_log(ist
, AV_LOG_WARNING
, "No extradata to dump.\n");
1532 if (!*filename
&& (e
= av_dict_get(st
->metadata
, "filename", NULL
, 0)))
1533 filename
= e
->value
;
1535 av_log(ist
, AV_LOG_FATAL
, "No filename specified and no 'filename' tag");
1536 return AVERROR(EINVAL
);
1539 ret
= assert_file_overwrite(filename
);
1543 if ((ret
= avio_open2(&out
, filename
, AVIO_FLAG_WRITE
, &int_cb
, NULL
)) < 0) {
1544 av_log(ist
, AV_LOG_FATAL
, "Could not open file %s for writing.\n",
1549 avio_write(out
, st
->codecpar
->extradata
, st
->codecpar
->extradata_size
);
1550 ret
= avio_close(out
);
1553 av_log(ist
, AV_LOG_INFO
, "Wrote attachment (%d bytes) to '%s'\n",
1554 st
->codecpar
->extradata_size
, filename
);
1559 static const char *input_file_item_name(void *obj
)
1561 const Demuxer
*d
= obj
;
1566 static const AVClass input_file_class
= {
1567 .class_name
= "InputFile",
1568 .version
= LIBAVUTIL_VERSION_INT
,
1569 .item_name
= input_file_item_name
,
1570 .category
= AV_CLASS_CATEGORY_DEMUXER
,
1573 static Demuxer
*demux_alloc(void)
1575 Demuxer
*d
= allocate_array_elem(&input_files
, sizeof(*d
), &nb_input_files
);
1580 d
->f
.class = &input_file_class
;
1581 d
->f
.index
= nb_input_files
- 1;
1583 snprintf(d
->log_name
, sizeof(d
->log_name
), "in#%d", d
->f
.index
);
1588 int ifile_open(const OptionsContext
*o
, const char *filename
, Scheduler
*sch
)
1592 AVFormatContext
*ic
;
1593 const AVInputFormat
*file_iformat
= NULL
;
1596 AVDictionary
*opts_used
= NULL
;
1597 const char* video_codec_name
= NULL
;
1598 const char* audio_codec_name
= NULL
;
1599 const char* subtitle_codec_name
= NULL
;
1600 const char* data_codec_name
= NULL
;
1601 int scan_all_pmts_set
= 0;
1603 int64_t start_time
= o
->start_time
;
1604 int64_t start_time_eof
= o
->start_time_eof
;
1605 int64_t stop_time
= o
->stop_time
;
1606 int64_t recording_time
= o
->recording_time
;
1610 return AVERROR(ENOMEM
);
1614 ret
= sch_add_demux(sch
, input_thread
, d
);
1619 if (stop_time
!= INT64_MAX
&& recording_time
!= INT64_MAX
) {
1620 stop_time
= INT64_MAX
;
1621 av_log(d
, AV_LOG_WARNING
, "-t and -to cannot be used together; using -t.\n");
1624 if (stop_time
!= INT64_MAX
&& recording_time
== INT64_MAX
) {
1625 int64_t start
= start_time
== AV_NOPTS_VALUE
? 0 : start_time
;
1626 if (stop_time
<= start
) {
1627 av_log(d
, AV_LOG_ERROR
, "-to value smaller than -ss; aborting.\n");
1628 return AVERROR(EINVAL
);
1630 recording_time
= stop_time
- start
;
1635 if (!(file_iformat
= av_find_input_format(o
->format
))) {
1636 av_log(d
, AV_LOG_FATAL
, "Unknown input format: '%s'\n", o
->format
);
1637 return AVERROR(EINVAL
);
1641 if (!strcmp(filename
, "-"))
1644 stdin_interaction
&= strncmp(filename
, "pipe:", 5) &&
1645 strcmp(filename
, "fd:") &&
1646 strcmp(filename
, "/dev/stdin");
1648 /* get default parameters from command line */
1649 ic
= avformat_alloc_context();
1651 return AVERROR(ENOMEM
);
1652 if (o
->audio_sample_rate
.nb_opt
) {
1653 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);
1655 if (o
->audio_channels
.nb_opt
) {
1656 const AVClass
*priv_class
;
1657 if (file_iformat
&& (priv_class
= file_iformat
->priv_class
) &&
1658 av_opt_find(&priv_class
, "ch_layout", NULL
, 0,
1659 AV_OPT_SEARCH_FAKE_OBJ
)) {
1661 snprintf(buf
, sizeof(buf
), "%dC", o
->audio_channels
.opt
[o
->audio_channels
.nb_opt
- 1].u
.i
);
1662 av_dict_set(&o
->g
->format_opts
, "ch_layout", buf
, 0);
1665 if (o
->audio_ch_layouts
.nb_opt
) {
1666 const AVClass
*priv_class
;
1667 if (file_iformat
&& (priv_class
= file_iformat
->priv_class
) &&
1668 av_opt_find(&priv_class
, "ch_layout", NULL
, 0,
1669 AV_OPT_SEARCH_FAKE_OBJ
)) {
1670 av_dict_set(&o
->g
->format_opts
, "ch_layout", o
->audio_ch_layouts
.opt
[o
->audio_ch_layouts
.nb_opt
- 1].u
.str
, 0);
1673 if (o
->frame_rates
.nb_opt
) {
1674 const AVClass
*priv_class
;
1675 /* set the format-level framerate option;
1676 * this is important for video grabbers, e.g. x11 */
1677 if (file_iformat
&& (priv_class
= file_iformat
->priv_class
) &&
1678 av_opt_find(&priv_class
, "framerate", NULL
, 0,
1679 AV_OPT_SEARCH_FAKE_OBJ
)) {
1680 av_dict_set(&o
->g
->format_opts
, "framerate",
1681 o
->frame_rates
.opt
[o
->frame_rates
.nb_opt
- 1].u
.str
, 0);
1684 if (o
->frame_sizes
.nb_opt
) {
1685 av_dict_set(&o
->g
->format_opts
, "video_size", o
->frame_sizes
.opt
[o
->frame_sizes
.nb_opt
- 1].u
.str
, 0);
1687 if (o
->frame_pix_fmts
.nb_opt
)
1688 av_dict_set(&o
->g
->format_opts
, "pixel_format", o
->frame_pix_fmts
.opt
[o
->frame_pix_fmts
.nb_opt
- 1].u
.str
, 0);
1690 video_codec_name
= opt_match_per_type_str(&o
->codec_names
, 'v');
1691 audio_codec_name
= opt_match_per_type_str(&o
->codec_names
, 'a');
1692 subtitle_codec_name
= opt_match_per_type_str(&o
->codec_names
, 's');
1693 data_codec_name
= opt_match_per_type_str(&o
->codec_names
, 'd');
1695 if (video_codec_name
)
1696 ret
= err_merge(ret
, find_codec(NULL
, video_codec_name
, AVMEDIA_TYPE_VIDEO
, 0,
1698 if (audio_codec_name
)
1699 ret
= err_merge(ret
, find_codec(NULL
, audio_codec_name
, AVMEDIA_TYPE_AUDIO
, 0,
1701 if (subtitle_codec_name
)
1702 ret
= err_merge(ret
, find_codec(NULL
, subtitle_codec_name
, AVMEDIA_TYPE_SUBTITLE
, 0,
1703 &ic
->subtitle_codec
));
1704 if (data_codec_name
)
1705 ret
= err_merge(ret
, find_codec(NULL
, data_codec_name
, AVMEDIA_TYPE_DATA
, 0,
1708 avformat_free_context(ic
);
1712 ic
->video_codec_id
= video_codec_name
? ic
->video_codec
->id
: AV_CODEC_ID_NONE
;
1713 ic
->audio_codec_id
= audio_codec_name
? ic
->audio_codec
->id
: AV_CODEC_ID_NONE
;
1714 ic
->subtitle_codec_id
= subtitle_codec_name
? ic
->subtitle_codec
->id
: AV_CODEC_ID_NONE
;
1715 ic
->data_codec_id
= data_codec_name
? ic
->data_codec
->id
: AV_CODEC_ID_NONE
;
1717 ic
->flags
|= AVFMT_FLAG_NONBLOCK
;
1719 ic
->flags
|= AVFMT_FLAG_BITEXACT
;
1720 ic
->interrupt_callback
= int_cb
;
1722 if (!av_dict_get(o
->g
->format_opts
, "scan_all_pmts", NULL
, AV_DICT_MATCH_CASE
)) {
1723 av_dict_set(&o
->g
->format_opts
, "scan_all_pmts", "1", AV_DICT_DONT_OVERWRITE
);
1724 scan_all_pmts_set
= 1;
1726 /* open the input file with generic avformat function */
1727 err
= avformat_open_input(&ic
, filename
, file_iformat
, &o
->g
->format_opts
);
1729 av_log(d
, AV_LOG_ERROR
,
1730 "Error opening input: %s\n", av_err2str(err
));
1731 if (err
== AVERROR_PROTOCOL_NOT_FOUND
)
1732 av_log(d
, AV_LOG_ERROR
, "Did you mean file:%s?\n", filename
);
1737 av_strlcat(d
->log_name
, "/", sizeof(d
->log_name
));
1738 av_strlcat(d
->log_name
, ic
->iformat
->name
, sizeof(d
->log_name
));
1740 if (scan_all_pmts_set
)
1741 av_dict_set(&o
->g
->format_opts
, "scan_all_pmts", NULL
, AV_DICT_MATCH_CASE
);
1742 remove_avoptions(&o
->g
->format_opts
, o
->g
->codec_opts
);
1744 ret
= check_avoptions(o
->g
->format_opts
);
1748 /* apply forced codec ids */
1749 for (int i
= 0; i
< ic
->nb_streams
; i
++) {
1750 const AVCodec
*dummy
;
1751 ret
= choose_decoder(o
, f
, ic
, ic
->streams
[i
], HWACCEL_NONE
, AV_HWDEVICE_TYPE_NONE
,
1757 if (o
->find_stream_info
) {
1758 AVDictionary
**opts
;
1759 int orig_nb_streams
= ic
->nb_streams
;
1761 ret
= setup_find_stream_info_opts(ic
, o
->g
->codec_opts
, &opts
);
1765 /* If not enough info to get the stream parameters, we decode the
1766 first frames to get it. (used in mpeg case for example) */
1767 ret
= avformat_find_stream_info(ic
, opts
);
1769 for (int i
= 0; i
< orig_nb_streams
; i
++)
1770 av_dict_free(&opts
[i
]);
1774 av_log(d
, AV_LOG_FATAL
, "could not find codec parameters\n");
1775 if (ic
->nb_streams
== 0)
1780 if (start_time
!= AV_NOPTS_VALUE
&& start_time_eof
!= AV_NOPTS_VALUE
) {
1781 av_log(d
, AV_LOG_WARNING
, "Cannot use -ss and -sseof both, using -ss\n");
1782 start_time_eof
= AV_NOPTS_VALUE
;
1785 if (start_time_eof
!= AV_NOPTS_VALUE
) {
1786 if (start_time_eof
>= 0) {
1787 av_log(d
, AV_LOG_ERROR
, "-sseof value must be negative; aborting\n");
1788 return AVERROR(EINVAL
);
1790 if (ic
->duration
> 0) {
1791 start_time
= start_time_eof
+ ic
->duration
;
1792 if (start_time
< 0) {
1793 av_log(d
, AV_LOG_WARNING
, "-sseof value seeks to before start of file; ignored\n");
1794 start_time
= AV_NOPTS_VALUE
;
1797 av_log(d
, AV_LOG_WARNING
, "Cannot use -sseof, file duration not known\n");
1799 timestamp
= (start_time
== AV_NOPTS_VALUE
) ? 0 : start_time
;
1800 /* add the stream start time */
1801 if (!o
->seek_timestamp
&& ic
->start_time
!= AV_NOPTS_VALUE
)
1802 timestamp
+= ic
->start_time
;
1804 /* if seeking requested, we execute it */
1805 if (start_time
!= AV_NOPTS_VALUE
) {
1806 int64_t seek_timestamp
= timestamp
;
1808 if (!(ic
->iformat
->flags
& AVFMT_SEEK_TO_PTS
)) {
1809 int dts_heuristic
= 0;
1810 for (int i
= 0; i
< ic
->nb_streams
; i
++) {
1811 const AVCodecParameters
*par
= ic
->streams
[i
]->codecpar
;
1812 if (par
->video_delay
) {
1817 if (dts_heuristic
) {
1818 seek_timestamp
-= 3*AV_TIME_BASE
/ 23;
1821 ret
= avformat_seek_file(ic
, -1, INT64_MIN
, seek_timestamp
, seek_timestamp
, 0);
1823 av_log(d
, AV_LOG_WARNING
, "could not seek to position %0.3f\n",
1824 (double)timestamp
/ AV_TIME_BASE
);
1828 f
->start_time
= start_time
;
1829 d
->recording_time
= recording_time
;
1830 f
->input_sync_ref
= o
->input_sync_ref
;
1831 f
->input_ts_offset
= o
->input_ts_offset
;
1832 f
->ts_offset
= o
->input_ts_offset
- (copy_ts
? (start_at_zero
&& ic
->start_time
!= AV_NOPTS_VALUE
? ic
->start_time
: 0) : timestamp
);
1833 d
->accurate_seek
= o
->accurate_seek
;
1835 d
->nb_streams_warn
= ic
->nb_streams
;
1837 d
->duration
= (Timestamp
){ .ts
= 0, .tb
= (AVRational
){ 1, 1 } };
1838 d
->min_pts
= (Timestamp
){ .ts
= AV_NOPTS_VALUE
, .tb
= (AVRational
){ 1, 1 } };
1839 d
->max_pts
= (Timestamp
){ .ts
= AV_NOPTS_VALUE
, .tb
= (AVRational
){ 1, 1 } };
1841 d
->readrate
= o
->readrate
? o
->readrate
: 0.0;
1842 if (d
->readrate
< 0.0f
) {
1843 av_log(d
, AV_LOG_ERROR
, "Option -readrate is %0.3f; it must be non-negative.\n", d
->readrate
);
1844 return AVERROR(EINVAL
);
1848 av_log(d
, AV_LOG_WARNING
, "Both -readrate and -re set. Using -readrate %0.3f.\n", d
->readrate
);
1854 d
->readrate_initial_burst
= o
->readrate_initial_burst
? o
->readrate_initial_burst
: 0.5;
1855 if (d
->readrate_initial_burst
< 0.0) {
1856 av_log(d
, AV_LOG_ERROR
,
1857 "Option -readrate_initial_burst is %0.3f; it must be non-negative.\n",
1858 d
->readrate_initial_burst
);
1859 return AVERROR(EINVAL
);
1861 } else if (o
->readrate_initial_burst
) {
1862 av_log(d
, AV_LOG_WARNING
, "Option -readrate_initial_burst ignored "
1863 "since neither -readrate nor -re were given\n");
1866 /* Add all the streams from the given input file to the demuxer */
1867 for (int i
= 0; i
< ic
->nb_streams
; i
++) {
1868 ret
= ist_add(o
, d
, ic
->streams
[i
], &opts_used
);
1870 av_dict_free(&opts_used
);
1875 /* dump the file content */
1876 av_dump_format(ic
, f
->index
, filename
, 0);
1878 /* check if all codec options have been used */
1879 ret
= check_avoptions_used(o
->g
->codec_opts
, opts_used
, d
, 1);
1880 av_dict_free(&opts_used
);
1884 for (int i
= 0; i
< o
->dump_attachment
.nb_opt
; i
++) {
1885 for (int j
= 0; j
< f
->nb_streams
; j
++) {
1886 InputStream
*ist
= f
->streams
[j
];
1888 if (check_stream_specifier(ic
, ist
->st
, o
->dump_attachment
.opt
[i
].specifier
) == 1) {
1889 ret
= dump_attachment(ist
, o
->dump_attachment
.opt
[i
].u
.str
);