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
;
70 int force_display_matrix
;
73 int wrap_correction_done
;
75 ///< dts of the first packet read for this stream (in AV_TIME_BASE units)
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) */
81 ///< dts of the last packet read for this stream (in AV_TIME_BASE units)
84 const AVCodecDescriptor
*codec_desc
;
86 AVDictionary
*decoder_opts
;
89 // decoded media properties, as estimated by opening the decoder
90 AVFrame
*decoded_params
;
94 /* number of packets successfully read for this stream */
96 // combined size of all the packets read
100 typedef struct Demuxer
{
103 // name used for logging
106 int64_t wallclock_start
;
109 * Extra timestamp offset added by discontinuity handling.
111 int64_t ts_offset_discont
;
114 int64_t recording_time
;
117 /* number of times input stream should be looped */
120 /* duration of the looped segment of the input file */
122 /* pts with the smallest/largest values ever seen */
126 /* number of streams that the user was warned of */
130 double readrate_initial_burst
;
134 AVPacket
*pkt_heartbeat
;
138 int nb_streams_finished
;
141 typedef struct DemuxThreadContext
{
142 // packet used for reading from the demuxer
144 // packet for reading from BSFs
146 } DemuxThreadContext
;
148 static DemuxStream
*ds_from_ist(InputStream
*ist
)
150 return (DemuxStream
*)ist
;
153 static Demuxer
*demuxer_from_ifile(InputFile
*f
)
158 InputStream
*ist_find_unused(enum AVMediaType type
)
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
)
169 static void report_new_stream(Demuxer
*d
, const AVPacket
*pkt
)
171 const AVStream
*st
= d
->f
.ctx
->streams
[pkt
->stream_index
];
173 if (pkt
->stream_index
< d
->nb_streams_warn
)
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;
182 static int seek_to_start(Demuxer
*d
, Timestamp end_pts
)
184 InputFile
*ifile
= &d
->f
;
185 AVFormatContext
*is
= ifile
->ctx
;
188 ret
= avformat_seek_file(is
, -1, INT64_MIN
, is
->start_time
, is
->start_time
, 0);
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
;
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
);
201 d
->duration
.tb
= d
->max_pts
.tb
;
209 static void ts_discontinuity_detect(Demuxer
*d
, InputStream
*ist
,
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
);
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;
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
);
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
;
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
;
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
);
274 d
->last_ts
= av_rescale_q(pkt
->dts
, pkt
->time_base
, AV_TIME_BASE_Q
);
277 static void ts_discontinuity_process(Demuxer
*d
, InputStream
*ist
,
280 int64_t offset
= av_rescale_q(d
->ts_offset_discont
, AV_TIME_BASE_Q
,
283 // apply previously-detected timestamp-discontinuity offset
284 // (to all streams, not just audio/video)
285 if (pkt
->dts
!= AV_NOPTS_VALUE
)
287 if (pkt
->pts
!= AV_NOPTS_VALUE
)
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
);
297 static int ist_dts_update(DemuxStream
*ds
, AVPacket
*pkt
, FrameData
*fd
)
299 InputStream
*ist
= &ds
->ist
;
300 const AVCodecParameters
*par
= ist
->par
;
302 if (!ds
->saw_first_ts
) {
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
) {
307 ds
->dts
+= av_rescale_q(pkt
->pts
, pkt
->time_base
, AV_TIME_BASE_Q
);
309 ds
->saw_first_ts
= 1;
312 if (ds
->next_dts
== AV_NOPTS_VALUE
)
313 ds
->next_dts
= ds
->dts
;
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
);
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
) /
326 ds
->next_dts
+= av_rescale_q(pkt
->duration
, pkt
->time_base
, AV_TIME_BASE_Q
);
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 });
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
;
347 ds
->next_dts
+= av_rescale_q(fields
, av_inv_q(field_rate
), AV_TIME_BASE_Q
);
352 fd
->dts_est
= ds
->dts
;
357 static int ts_fixup(Demuxer
*d
, AVPacket
*pkt
, FrameData
*fd
)
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
;
366 pkt
->time_base
= ist
->st
->time_base
;
368 #define SHOW_TS_DEBUG(tag_) \
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)); \
379 SHOW_TS_DEBUG("demuxer");
381 if (!ds
->wrap_correction_done
&& start_time
!= AV_NOPTS_VALUE
&&
382 ist
->st
->pts_wrap_bits
< 64) {
383 int64_t stime
, stime2
;
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;
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;
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;
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
);
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
;
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
;
414 pkt
->pts
+= duration
;
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
};
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
};
432 if (pkt
->dts
!= AV_NOPTS_VALUE
)
433 pkt
->dts
+= duration
;
435 SHOW_TS_DEBUG("demuxer+tsfixup");
437 // detect and try to correct for timestamp discontinuities
438 ts_discontinuity_process(d
, ist
, pkt
);
440 // update estimated/predicted dts
441 ret
= ist_dts_update(ds
, pkt
, fd
);
448 static int input_packet_process(Demuxer
*d
, AVPacket
*pkt
, unsigned *send_flags
)
450 InputFile
*f
= &d
->f
;
451 InputStream
*ist
= f
->streams
[pkt
->stream_index
];
452 DemuxStream
*ds
= ds_from_ist(ist
);
456 fd
= packet_data(pkt
);
458 return AVERROR(ENOMEM
);
460 ret
= ts_fixup(d
, pkt
, fd
);
464 if (d
->recording_time
!= INT64_MAX
) {
465 int64_t start_time
= 0;
467 start_time
+= f
->start_time
!= AV_NOPTS_VALUE
? f
->start_time
: 0;
468 start_time
+= start_at_zero
? 0 : f
->start_time_effective
;
470 if (ds
->dts
>= d
->recording_time
+ start_time
)
471 *send_flags
|= DEMUX_SEND_STREAMCOPY_EOF
;
474 ds
->data_size
+= pkt
->size
;
477 fd
->wallclock
[LATENCY_PROBE_DEMUX
] = av_gettime_relative();
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
));
492 static void readrate_sleep(Demuxer
*d
)
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)
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
);
512 static int do_send(Demuxer
*d
, DemuxStream
*ds
, AVPacket
*pkt
, unsigned flags
,
513 const char *pkt_desc
)
517 pkt
->stream_index
= ds
->sch_idx_stream
;
519 ret
= sch_demux_send(d
->sch
, d
->f
.index
, pkt
, flags
);
520 if (ret
== AVERROR_EOF
) {
521 av_packet_unref(pkt
);
523 av_log(ds
, AV_LOG_VERBOSE
, "All consumers of this stream are done\n");
526 if (++d
->nb_streams_finished
== d
->nb_streams_used
) {
527 av_log(d
, AV_LOG_VERBOSE
, "All consumers are done\n");
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
));
541 static int demux_send(Demuxer
*d
, DemuxThreadContext
*dt
, DemuxStream
*ds
,
542 AVPacket
*pkt
, unsigned flags
)
544 InputFile
*f
= &d
->f
;
547 // pkt can be NULL only when flushing BSFs
548 av_assert0(ds
->bsf
|| pkt
);
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
]);
555 if (ds1
->finished
|| !ds1
->have_sub2video
)
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
;
562 ret
= do_send(d
, ds1
, d
->pkt_heartbeat
, 0, "heartbeat");
570 av_packet_rescale_ts(pkt
, pkt
->time_base
, ds
->bsf
->time_base_in
);
572 ret
= av_bsf_send_packet(ds
->bsf
, pkt
);
575 av_packet_unref(pkt
);
576 av_log(ds
, AV_LOG_ERROR
, "Error submitting a packet for filtering: %s\n",
582 ret
= av_bsf_receive_packet(ds
->bsf
, dt
->pkt_bsf
);
583 if (ret
== AVERROR(EAGAIN
))
586 if (ret
!= AVERROR_EOF
)
587 av_log(ds
, AV_LOG_ERROR
,
588 "Error applying bitstream filters to a packet: %s\n",
593 dt
->pkt_bsf
->time_base
= ds
->bsf
->time_base_out
;
595 ret
= do_send(d
, ds
, dt
->pkt_bsf
, 0, "filtered");
597 av_packet_unref(dt
->pkt_bsf
);
602 ret
= do_send(d
, ds
, pkt
, flags
, "demuxed");
610 static int demux_bsf_flush(Demuxer
*d
, DemuxThreadContext
*dt
)
612 InputFile
*f
= &d
->f
;
615 for (unsigned i
= 0; i
< f
->nb_streams
; i
++) {
616 DemuxStream
*ds
= ds_from_ist(f
->streams
[i
]);
621 ret
= demux_send(d
, dt
, ds
, NULL
, 0);
622 ret
= (ret
== AVERROR_EOF
) ? 0 : (ret
< 0) ? ret
: AVERROR_BUG
;
624 av_log(ds
, AV_LOG_ERROR
, "Error flushing BSFs: %s\n",
629 av_bsf_flush(ds
->bsf
);
635 static void discard_unused_programs(InputFile
*ifile
)
637 for (int j
= 0; j
< ifile
->ctx
->nb_programs
; j
++) {
638 AVProgram
*p
= ifile
->ctx
->programs
[j
];
639 int discard
= AVDISCARD_ALL
;
641 for (int k
= 0; k
< p
->nb_stream_indexes
; k
++) {
642 DemuxStream
*ds
= ds_from_ist(ifile
->streams
[p
->stream_index
[k
]]);
645 discard
= AVDISCARD_DEFAULT
;
649 p
->discard
= discard
;
653 static void thread_set_name(InputFile
*f
)
656 snprintf(name
, sizeof(name
), "dmx%d:%s", f
->index
, f
->ctx
->iformat
->name
);
657 ff_thread_setname(name
);
660 static void demux_thread_uninit(DemuxThreadContext
*dt
)
662 av_packet_free(&dt
->pkt_demux
);
663 av_packet_free(&dt
->pkt_bsf
);
665 memset(dt
, 0, sizeof(*dt
));
668 static int demux_thread_init(DemuxThreadContext
*dt
)
670 memset(dt
, 0, sizeof(*dt
));
672 dt
->pkt_demux
= av_packet_alloc();
674 return AVERROR(ENOMEM
);
676 dt
->pkt_bsf
= av_packet_alloc();
678 return AVERROR(ENOMEM
);
683 static int input_thread(void *arg
)
686 InputFile
*f
= &d
->f
;
688 DemuxThreadContext dt
;
692 ret
= demux_thread_init(&dt
);
698 discard_unused_programs(f
);
701 d
->wallclock_start
= av_gettime_relative();
705 unsigned send_flags
= 0;
707 ret
= av_read_frame(f
->ctx
, dt
.pkt_demux
);
709 if (ret
== AVERROR(EAGAIN
)) {
716 if (ret
== AVERROR_EOF
)
717 av_log(d
, AV_LOG_VERBOSE
, "EOF while reading input\n");
719 av_log(d
, AV_LOG_ERROR
, "Error during demuxing: %s\n",
721 ret
= exit_on_error
? ret
: 0;
724 ret_bsf
= demux_bsf_flush(d
, &dt
);
725 ret
= err_merge(ret
== AVERROR_EOF
? 0 : ret
, ret_bsf
);
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);
732 ret
= seek_to_start(d
, (Timestamp
){ .ts
= dt
.pkt_demux
->pts
,
733 .tb
= dt
.pkt_demux
->time_base
});
737 /* fallthrough to the error path */
744 av_pkt_dump_log2(NULL
, AV_LOG_INFO
, dt
.pkt_demux
, do_hex_dump
,
745 f
->ctx
->streams
[dt
.pkt_demux
->stream_index
]);
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
);
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
);
763 av_packet_unref(dt
.pkt_demux
);
764 ret
= AVERROR_INVALIDDATA
;
769 ret
= input_packet_process(d
, dt
.pkt_demux
, &send_flags
);
776 ret
= demux_send(d
, &dt
, ds
, dt
.pkt_demux
, send_flags
);
781 // EOF/EXIT is normal termination
782 if (ret
== AVERROR_EOF
|| ret
== AVERROR_EXIT
)
786 demux_thread_uninit(&dt
);
791 static void demux_final_stats(Demuxer
*d
)
793 InputFile
*f
= &d
->f
;
794 uint64_t total_packets
= 0, total_size
= 0;
796 av_log(f
, AV_LOG_VERBOSE
, "Input file #%d (%s):\n",
797 f
->index
, f
->ctx
->url
);
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
;
804 if (ds
->discard
|| type
== AVMEDIA_TYPE_ATTACHMENT
)
807 total_size
+= ds
->data_size
;
808 total_packets
+= ds
->nb_packets
;
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
);
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
, "; ");
824 av_log(f
, AV_LOG_VERBOSE
, "\n");
827 av_log(f
, AV_LOG_VERBOSE
, " Total: %"PRIu64
" packets (%"PRIu64
" bytes) demuxed\n",
828 total_packets
, total_size
);
831 static void ist_free(InputStream
**pist
)
833 InputStream
*ist
= *pist
;
838 ds
= ds_from_ist(ist
);
840 dec_free(&ist
->decoder
);
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
);
847 avcodec_parameters_free(&ist
->par
);
849 av_frame_free(&ds
->decoded_params
);
851 av_bsf_free(&ds
->bsf
);
856 void ifile_close(InputFile
**pf
)
859 Demuxer
*d
= demuxer_from_ifile(f
);
865 demux_final_stats(d
);
867 for (int i
= 0; i
< f
->nb_streams
; i
++)
868 ist_free(&f
->streams
[i
]);
869 av_freep(&f
->streams
);
871 avformat_close_input(&f
->ctx
);
873 av_packet_free(&d
->pkt_heartbeat
);
878 static int ist_use(InputStream
*ist
, int decoding_needed
,
879 const ViewSpecifier
*vs
, SchedulerNode
*src
)
881 Demuxer
*d
= demuxer_from_ifile(ist
->file
);
882 DemuxStream
*ds
= ds_from_ist(ist
);
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
);
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
);
898 if (ds
->sch_idx_stream
< 0) {
899 ret
= sch_add_demux_stream(d
->sch
, d
->f
.index
);
902 ds
->sch_idx_stream
= ret
;
907 d
->nb_streams_used
++;
910 ist
->st
->discard
= ist
->user_set_discard
;
911 ds
->decoding_needed
|= decoding_needed
;
912 ds
->streamcopy_needed
|= !decoding_needed
;
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
;
919 ret
= av_opt_get_int(d
->f
.ctx
, "use_wallclock_as_timestamps", 0, &use_wallclock_as_timestamps
);
923 if (use_wallclock_as_timestamps
)
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
)
930 | ((ist
->top_field_first
>= 0) * DECODER_FLAG_TOP_FIELD_FIRST
)
934 if (ist
->framerate
.num
) {
935 ds
->dec_opts
.flags
|= DECODER_FLAG_FRAMERATE_FORCED
;
936 ds
->dec_opts
.framerate
= ist
->framerate
;
938 ds
->dec_opts
.framerate
= ist
->st
->avg_frame_rate
;
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");
949 snprintf(ds
->dec_name
, sizeof(ds
->dec_name
), "%d:%d", ist
->file
->index
, ist
->index
);
950 ds
->dec_opts
.name
= ds
->dec_name
;
952 ds
->dec_opts
.codec
= ist
->dec
;
953 ds
->dec_opts
.par
= ist
->par
;
955 ds
->dec_opts
.log_parent
= ist
;
957 ds
->decoded_params
= av_frame_alloc();
958 if (!ds
->decoded_params
)
959 return AVERROR(ENOMEM
);
961 ret
= dec_init(&ist
->decoder
, d
->sch
,
962 &ds
->decoder_opts
, &ds
->dec_opts
, ds
->decoded_params
);
965 ds
->sch_idx_dec
= ret
;
967 ret
= sch_connect(d
->sch
, SCH_DSTREAM(d
->f
.index
, ds
->sch_idx_stream
),
968 SCH_DEC_IN(ds
->sch_idx_dec
));
972 d
->have_audio_dec
|= is_audio
;
975 if (decoding_needed
&& ist
->par
->codec_type
== AVMEDIA_TYPE_VIDEO
) {
976 ret
= dec_request_view(ist
->decoder
, vs
, src
);
980 *src
= decoding_needed
?
981 SCH_DEC_OUT(ds
->sch_idx_dec
, 0) :
982 SCH_DSTREAM(d
->f
.index
, ds
->sch_idx_stream
);
988 int ist_output_add(InputStream
*ist
, OutputStream
*ost
)
990 DemuxStream
*ds
= ds_from_ist(ist
);
994 ret
= ist_use(ist
, ost
->enc
? DECODING_FOR_OST
: 0, NULL
, &src
);
998 ret
= GROW_ARRAY(ist
->outputs
, ist
->nb_outputs
);
1002 ist
->outputs
[ist
->nb_outputs
- 1] = ost
;
1004 return ost
->enc
? ds
->sch_idx_dec
: ds
->sch_idx_stream
;
1007 int ist_filter_add(InputStream
*ist
, InputFilter
*ifilter
, int is_simple
,
1008 const ViewSpecifier
*vs
, InputFilterOptions
*opts
,
1011 Demuxer
*d
= demuxer_from_ifile(ist
->file
);
1012 DemuxStream
*ds
= ds_from_ist(ist
);
1013 int64_t tsoffset
= 0;
1016 ret
= ist_use(ist
, is_simple
? DECODING_FOR_OST
: DECODING_FOR_FILTER
,
1021 ret
= GROW_ARRAY(ist
->filters
, ist
->nb_filters
);
1025 ist
->filters
[ist
->nb_filters
- 1] = ifilter
;
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
;
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
;
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
);
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);
1066 if (!d
->pkt_heartbeat
) {
1067 d
->pkt_heartbeat
= av_packet_alloc();
1068 if (!d
->pkt_heartbeat
)
1069 return AVERROR(ENOMEM
);
1071 ds
->have_sub2video
= 1;
1074 ret
= av_frame_copy_props(opts
->fallback
, ds
->decoded_params
);
1077 opts
->fallback
->format
= ds
->decoded_params
->format
;
1078 opts
->fallback
->width
= ds
->decoded_params
->width
;
1079 opts
->fallback
->height
= ds
->decoded_params
->height
;
1081 ret
= av_channel_layout_copy(&opts
->fallback
->ch_layout
, &ds
->decoded_params
->ch_layout
);
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
;
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
;
1094 opts
->name
= av_strdup(ds
->dec_name
);
1096 return AVERROR(ENOMEM
);
1098 opts
->flags
|= IFILTER_FLAG_AUTOROTATE
* !!(ds
->autorotate
) |
1099 IFILTER_FLAG_REINIT
* !!(ds
->reinit_filters
);
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
)
1110 const char *codec_name
= NULL
;
1112 opt_match_per_stream_str(logctx
, &o
->codec_names
, s
, st
, &codec_name
);
1114 int ret
= find_codec(NULL
, codec_name
, st
->codecpar
->codec_type
, 0, pcodec
);
1117 st
->codecpar
->codec_id
= (*pcodec
)->id
;
1118 if (recast_media
&& st
->codecpar
->codec_type
!= (*pcodec
)->type
)
1119 st
->codecpar
->codec_type
= (*pcodec
)->type
;
1122 if (st
->codecpar
->codec_type
== AVMEDIA_TYPE_VIDEO
&&
1123 hwaccel_id
== HWACCEL_GENERIC
&&
1124 hwaccel_device_type
!= AV_HWDEVICE_TYPE_NONE
) {
1128 while ((c
= av_codec_iterate(&i
))) {
1129 const AVCodecHWConfig
*config
;
1131 if (c
->id
!= st
->codecpar
->codec_id
||
1132 !av_codec_is_decoder(c
))
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
));
1146 *pcodec
= avcodec_find_decoder(st
->codecpar
->codec_id
);
1151 static int guess_input_channel_layout(InputStream
*ist
, AVCodecParameters
*par
,
1152 int guess_layout_max
)
1154 if (par
->ch_layout
.order
== AV_CHANNEL_ORDER_UNSPEC
) {
1155 char layout_name
[256];
1157 if (par
->ch_layout
.nb_channels
> guess_layout_max
)
1159 av_channel_layout_default(&par
->ch_layout
, par
->ch_layout
.nb_channels
);
1160 if (par
->ch_layout
.order
== AV_CHANNEL_ORDER_UNSPEC
)
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
);
1168 static int add_display_matrix_to_stream(const OptionsContext
*o
,
1169 AVFormatContext
*ctx
, InputStream
*ist
)
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;
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
);
1183 rotation_set
= rotation
!= DBL_MAX
;
1184 hflip_set
= hflip
!= -1;
1185 vflip_set
= vflip
!= -1;
1187 if (!rotation_set
&& !hflip_set
&& !vflip_set
)
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);
1195 av_log(ist
, AV_LOG_FATAL
, "Failed to generate a display matrix!\n");
1196 return AVERROR(ENOMEM
);
1199 buf
= (int32_t *)sd
->data
;
1200 av_display_rotation_set(buf
,
1201 rotation_set
? -(rotation
) : -0.0f
);
1203 av_display_matrix_flip(buf
,
1204 hflip_set
? hflip
: 0,
1205 vflip_set
? vflip
: 0);
1207 ds
->force_display_matrix
= 1;
1212 static const char *input_stream_item_name(void *obj
)
1214 const DemuxStream
*ds
= obj
;
1216 return ds
->log_name
;
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
,
1226 static DemuxStream
*demux_stream_alloc(Demuxer
*d
, AVStream
*st
)
1228 const char *type_str
= av_get_media_type_string(st
->codecpar
->codec_type
);
1229 InputFile
*f
= &d
->f
;
1232 ds
= allocate_array_elem(&f
->streams
, sizeof(*ds
), &f
->nb_streams
);
1236 ds
->sch_idx_stream
= -1;
1237 ds
->sch_idx_dec
= -1;
1241 ds
->ist
.index
= st
->index
;
1242 ds
->ist
.class = &input_stream_class
;
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
));
1251 static int ist_add(const OptionsContext
*o
, Demuxer
*d
, AVStream
*st
, AVDictionary
**opts_used
)
1253 AVFormatContext
*ic
= d
->f
.ctx
;
1254 AVCodecParameters
*par
= st
->codecpar
;
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
;
1264 const char *discard_str
= NULL
;
1267 ds
= demux_stream_alloc(d
, st
);
1269 return AVERROR(ENOMEM
);
1274 st
->discard
= AVDISCARD_ALL
;
1275 ds
->first_dts
= AV_NOPTS_VALUE
;
1276 ds
->next_dts
= AV_NOPTS_VALUE
;
1278 ds
->dec_opts
.time_base
= st
->time_base
;
1281 opt_match_per_stream_dbl(ist
, &o
->ts_scale
, ic
, st
, &ds
->ts_scale
);
1284 opt_match_per_stream_int(ist
, &o
->autorotate
, ic
, st
, &ds
->autorotate
);
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" },
1298 const AVClass
class = {
1299 .class_name
= "apply_cropping",
1300 .item_name
= av_default_item_name
,
1302 .version
= LIBAVUTIL_VERSION_INT
,
1304 const AVClass
*pclass
= &class;
1306 ret
= av_opt_eval_int(&pclass
, opts
, apply_cropping
, &ds
->apply_cropping
);
1308 av_log(ist
, AV_LOG_ERROR
, "Invalid apply_cropping value '%s'.\n", apply_cropping
);
1313 opt_match_per_stream_str(ist
, &o
->codec_tags
, ic
, st
, &codec_tag
);
1315 uint32_t tag
= strtol(codec_tag
, &next
, 0);
1317 uint8_t buf
[4] = { 0 };
1318 memcpy(buf
, codec_tag
, FFMIN(sizeof(buf
), strlen(codec_tag
)));
1322 st
->codecpar
->codec_tag
= tag
;
1325 if (st
->codecpar
->codec_type
== AVMEDIA_TYPE_VIDEO
) {
1326 ret
= add_display_matrix_to_stream(o
, ic
, ist
);
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
);
1356 ds
->dec_opts
.hwaccel_output_format
= AV_PIX_FMT_NONE
;
1360 // The NVDEC hwaccels use a CUDA device, so remap the name here.
1361 if (!strcmp(hwaccel
, "nvdec") || !strcmp(hwaccel
, "cuvid"))
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
;
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
;
1375 if (!ds
->dec_opts
.hwaccel_id
) {
1376 av_log(ist
, AV_LOG_FATAL
, "Unrecognized hwaccel: %s.\n",
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
);
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
);
1398 ret
= choose_decoder(o
, ist
, ic
, st
, ds
->dec_opts
.hwaccel_id
,
1399 ds
->dec_opts
.hwaccel_device_type
, &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
);
1410 ds
->reinit_filters
= -1;
1411 opt_match_per_stream_int(ist
, &o
->reinit_filters
, ic
, st
, &ds
->reinit_filters
);
1413 ist
->user_set_discard
= AVDISCARD_NONE
;
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
;
1421 opt_match_per_stream_str(ist
, &o
->discard
, ic
, st
, &discard_str
);
1423 ret
= av_opt_set(ist
->st
, "discard", discard_str
, 0);
1425 av_log(ist
, AV_LOG_ERROR
, "Error parsing discard %s.\n", discard_str
);
1428 ist
->user_set_discard
= ist
->st
->discard
;
1431 ds
->dec_opts
.flags
|= DECODER_FLAG_BITEXACT
* !!o
->bitexact
;
1433 av_dict_set_int(&ds
->decoder_opts
, "apply_cropping",
1434 ds
->apply_cropping
&& ds
->apply_cropping
!= CROP_CONTAINER
, 0);
1436 if (ds
->force_display_matrix
) {
1438 if (av_dict_get(ds
->decoder_opts
, "side_data_prefer_packet", NULL
, 0))
1442 av_strlcat(buf
, "displaymatrix", sizeof(buf
));
1443 av_dict_set(&ds
->decoder_opts
, "side_data_prefer_packet", buf
, AV_DICT_APPEND
);
1445 /* Attached pics are sparse, therefore we would not want to delay their decoding
1447 if (ist
->st
->disposition
& AV_DISPOSITION_ATTACHED_PIC
)
1448 av_dict_set(&ds
->decoder_opts
, "thread_type", "-frame", 0);
1450 switch (par
->codec_type
) {
1451 case AVMEDIA_TYPE_VIDEO
:
1452 opt_match_per_stream_str(ist
, &o
->frame_rates
, ic
, st
, &framerate
);
1454 ret
= av_parse_video_rate(&ist
->framerate
, framerate
);
1456 av_log(ist
, AV_LOG_ERROR
, "Error parsing framerate %s.\n",
1463 ist
->top_field_first
= -1;
1464 opt_match_per_stream_int(ist
, &o
->top_field_first
, ic
, st
, &ist
->top_field_first
);
1468 case AVMEDIA_TYPE_AUDIO
: {
1469 const char *ch_layout_str
= NULL
;
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
);
1476 av_log(ist
, AV_LOG_ERROR
, "Error parsing channel layout %s.\n", ch_layout_str
);
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
;
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
);
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
);
1496 case AVMEDIA_TYPE_DATA
:
1497 case AVMEDIA_TYPE_SUBTITLE
: {
1498 const char *canvas_size
= NULL
;
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
);
1503 ret
= av_parse_video_size(&par
->width
, &par
->height
,
1506 av_log(ist
, AV_LOG_FATAL
, "Invalid canvas size: %s.\n", canvas_size
);
1512 case AVMEDIA_TYPE_ATTACHMENT
:
1513 case AVMEDIA_TYPE_UNKNOWN
:
1515 default: av_assert0(0);
1518 ist
->par
= avcodec_parameters_alloc();
1520 return AVERROR(ENOMEM
);
1522 ret
= avcodec_parameters_copy(ist
->par
, par
);
1524 av_log(ist
, AV_LOG_ERROR
, "Error exporting stream parameters.\n");
1528 if (ist
->st
->sample_aspect_ratio
.num
)
1529 ist
->par
->sample_aspect_ratio
= ist
->st
->sample_aspect_ratio
;
1531 opt_match_per_stream_str(ist
, &o
->bitstream_filters
, ic
, st
, &bsfs
);
1533 ret
= av_bsf_list_parse_str(bsfs
, &ds
->bsf
);
1535 av_log(ist
, AV_LOG_ERROR
,
1536 "Error parsing bitstream filter sequence '%s': %s\n",
1537 bsfs
, av_err2str(ret
));
1541 ret
= avcodec_parameters_copy(ds
->bsf
->par_in
, ist
->par
);
1544 ds
->bsf
->time_base_in
= ist
->st
->time_base
;
1546 ret
= av_bsf_init(ds
->bsf
);
1548 av_log(ist
, AV_LOG_ERROR
, "Error initializing bitstream filters: %s\n",
1553 ret
= avcodec_parameters_copy(ist
->par
, ds
->bsf
->par_out
);
1558 ds
->codec_desc
= avcodec_descriptor_get(ist
->par
->codec_id
);
1563 static int dump_attachment(InputStream
*ist
, const char *filename
)
1565 AVStream
*st
= ist
->st
;
1567 AVIOContext
*out
= NULL
;
1568 const AVDictionaryEntry
*e
;
1570 if (!st
->codecpar
->extradata_size
) {
1571 av_log(ist
, AV_LOG_WARNING
, "No extradata to dump.\n");
1574 if (!*filename
&& (e
= av_dict_get(st
->metadata
, "filename", NULL
, 0)))
1575 filename
= e
->value
;
1577 av_log(ist
, AV_LOG_FATAL
, "No filename specified and no 'filename' tag");
1578 return AVERROR(EINVAL
);
1581 ret
= assert_file_overwrite(filename
);
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",
1591 avio_write(out
, st
->codecpar
->extradata
, st
->codecpar
->extradata_size
);
1592 ret
= avio_close(out
);
1595 av_log(ist
, AV_LOG_INFO
, "Wrote attachment (%d bytes) to '%s'\n",
1596 st
->codecpar
->extradata_size
, filename
);
1601 static const char *input_file_item_name(void *obj
)
1603 const Demuxer
*d
= obj
;
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
,
1615 static Demuxer
*demux_alloc(void)
1617 Demuxer
*d
= allocate_array_elem(&input_files
, sizeof(*d
), &nb_input_files
);
1622 d
->f
.class = &input_file_class
;
1623 d
->f
.index
= nb_input_files
- 1;
1625 snprintf(d
->log_name
, sizeof(d
->log_name
), "in#%d", d
->f
.index
);
1630 int ifile_open(const OptionsContext
*o
, const char *filename
, Scheduler
*sch
)
1634 AVFormatContext
*ic
;
1635 const AVInputFormat
*file_iformat
= NULL
;
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;
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
;
1652 return AVERROR(ENOMEM
);
1656 ret
= sch_add_demux(sch
, input_thread
, d
);
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");
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
);
1672 recording_time
= stop_time
- start
;
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
);
1683 if (!strcmp(filename
, "-"))
1686 stdin_interaction
&= strncmp(filename
, "pipe:", 5) &&
1687 strcmp(filename
, "fd:") &&
1688 strcmp(filename
, "/dev/stdin");
1690 /* get default parameters from command line */
1691 ic
= avformat_alloc_context();
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);
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
)) {
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);
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);
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);
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);
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);
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');
1737 if (video_codec_name
)
1738 ret
= err_merge(ret
, find_codec(NULL
, video_codec_name
, AVMEDIA_TYPE_VIDEO
, 0,
1740 if (audio_codec_name
)
1741 ret
= err_merge(ret
, find_codec(NULL
, audio_codec_name
, AVMEDIA_TYPE_AUDIO
, 0,
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,
1750 avformat_free_context(ic
);
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
;
1759 ic
->flags
|= AVFMT_FLAG_NONBLOCK
;
1761 ic
->flags
|= AVFMT_FLAG_BITEXACT
;
1762 ic
->interrupt_callback
= int_cb
;
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;
1768 /* open the input file with generic avformat function */
1769 err
= avformat_open_input(&ic
, filename
, file_iformat
, &o
->g
->format_opts
);
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
);
1779 av_strlcat(d
->log_name
, "/", sizeof(d
->log_name
));
1780 av_strlcat(d
->log_name
, ic
->iformat
->name
, sizeof(d
->log_name
));
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
);
1786 ret
= check_avoptions(o
->g
->format_opts
);
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
,
1799 if (o
->find_stream_info
) {
1800 AVDictionary
**opts
;
1801 int orig_nb_streams
= ic
->nb_streams
;
1803 ret
= setup_find_stream_info_opts(ic
, o
->g
->codec_opts
, &opts
);
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
);
1811 for (int i
= 0; i
< orig_nb_streams
; i
++)
1812 av_dict_free(&opts
[i
]);
1816 av_log(d
, AV_LOG_FATAL
, "could not find codec parameters\n");
1817 if (ic
->nb_streams
== 0)
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
;
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
);
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
;
1839 av_log(d
, AV_LOG_WARNING
, "Cannot use -sseof, file duration not known\n");
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
;
1846 /* if seeking requested, we execute it */
1847 if (start_time
!= AV_NOPTS_VALUE
) {
1848 int64_t seek_timestamp
= timestamp
;
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
) {
1859 if (dts_heuristic
) {
1860 seek_timestamp
-= 3*AV_TIME_BASE
/ 23;
1863 ret
= avformat_seek_file(ic
, -1, INT64_MIN
, seek_timestamp
, seek_timestamp
, 0);
1865 av_log(d
, AV_LOG_WARNING
, "could not seek to position %0.3f\n",
1866 (double)timestamp
/ AV_TIME_BASE
);
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
;
1877 d
->nb_streams_warn
= ic
->nb_streams
;
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 } };
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
);
1890 av_log(d
, AV_LOG_WARNING
, "Both -readrate and -re set. Using -readrate %0.3f.\n", 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
);
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");
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
);
1912 av_dict_free(&opts_used
);
1917 /* dump the file content */
1918 av_dump_format(ic
, f
->index
, filename
, 0);
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
);
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
];
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
);