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
34 #include "libavformat/avformat.h"
35 #include "libavformat/avio.h"
37 #include "libavcodec/avcodec.h"
39 #include "libavfilter/avfilter.h"
41 #include "libavutil/avutil.h"
42 #include "libavutil/dict.h"
43 #include "libavutil/eval.h"
44 #include "libavutil/fifo.h"
45 #include "libavutil/pixfmt.h"
46 #include "libavutil/rational.h"
47 #include "libavutil/threadmessage.h"
49 #include "libswresample/swresample.h"
52 #define VSYNC_PASSTHROUGH 0
55 #define VSYNC_VSCFR 0xfe
56 #define VSYNC_DROP 0xff
58 #define MAX_STREAMS 1024 /* arbitrary sanity check value */
72 typedef struct HWAccel {
74 int (*init)(AVCodecContext *s);
76 enum AVPixelFormat pix_fmt;
79 /* select an input stream for an output stream */
80 typedef struct StreamMap {
81 int disabled; /* 1 is this mapping is disabled by a negative map */
85 int sync_stream_index;
86 char *linklabel; /* name of an output link, for mapping lavfi outputs */
90 int file_idx, stream_idx, channel_idx; // input
91 int ofile_idx, ostream_idx; // output
94 typedef struct OptionsContext {
97 /* input/output options */
99 int64_t start_time_eof;
103 SpecifierOpt *codec_names;
105 SpecifierOpt *audio_channels;
106 int nb_audio_channels;
107 SpecifierOpt *audio_sample_rate;
108 int nb_audio_sample_rate;
109 SpecifierOpt *frame_rates;
111 SpecifierOpt *frame_sizes;
113 SpecifierOpt *frame_pix_fmts;
114 int nb_frame_pix_fmts;
117 int64_t input_ts_offset;
121 int thread_queue_size;
123 SpecifierOpt *ts_scale;
125 SpecifierOpt *dump_attachment;
126 int nb_dump_attachment;
127 SpecifierOpt *hwaccels;
129 SpecifierOpt *hwaccel_devices;
130 int nb_hwaccel_devices;
131 SpecifierOpt *hwaccel_output_formats;
132 int nb_hwaccel_output_formats;
133 SpecifierOpt *autorotate;
137 StreamMap *stream_maps;
139 AudioChannelMap *audio_channel_maps; /* one info entry per -map_channel */
140 int nb_audio_channel_maps; /* number of (valid) -map_channel settings */
141 int metadata_global_manual;
142 int metadata_streams_manual;
143 int metadata_chapters_manual;
144 const char **attachments;
147 int chapters_input_file;
149 int64_t recording_time;
151 uint64_t limit_filesize;
158 int subtitle_disable;
161 /* indexed by output file stream index */
165 SpecifierOpt *metadata;
167 SpecifierOpt *max_frames;
169 SpecifierOpt *bitstream_filters;
170 int nb_bitstream_filters;
171 SpecifierOpt *codec_tags;
173 SpecifierOpt *sample_fmts;
175 SpecifierOpt *qscale;
177 SpecifierOpt *forced_key_frames;
178 int nb_forced_key_frames;
179 SpecifierOpt *force_fps;
181 SpecifierOpt *frame_aspect_ratios;
182 int nb_frame_aspect_ratios;
183 SpecifierOpt *rc_overrides;
185 SpecifierOpt *intra_matrices;
186 int nb_intra_matrices;
187 SpecifierOpt *inter_matrices;
188 int nb_inter_matrices;
189 SpecifierOpt *chroma_intra_matrices;
190 int nb_chroma_intra_matrices;
191 SpecifierOpt *top_field_first;
192 int nb_top_field_first;
193 SpecifierOpt *metadata_map;
195 SpecifierOpt *presets;
197 SpecifierOpt *copy_initial_nonkeyframes;
198 int nb_copy_initial_nonkeyframes;
199 SpecifierOpt *copy_prior_start;
200 int nb_copy_prior_start;
201 SpecifierOpt *filters;
203 SpecifierOpt *filter_scripts;
204 int nb_filter_scripts;
205 SpecifierOpt *reinit_filters;
206 int nb_reinit_filters;
207 SpecifierOpt *fix_sub_duration;
208 int nb_fix_sub_duration;
209 SpecifierOpt *canvas_sizes;
213 SpecifierOpt *passlogfiles;
215 SpecifierOpt *max_muxing_queue_size;
216 int nb_max_muxing_queue_size;
217 SpecifierOpt *guess_layout_max;
218 int nb_guess_layout_max;
221 SpecifierOpt *discard;
223 SpecifierOpt *disposition;
225 SpecifierOpt *program;
229 typedef struct InputFilter {
230 AVFilterContext *filter;
231 struct InputStream *ist;
232 struct FilterGraph *graph;
235 // parameters configured for this input
239 AVRational sample_aspect_ratio;
243 uint64_t channel_layout;
245 AVBufferRef *hw_frames_ctx;
248 typedef struct OutputFilter {
249 AVFilterContext *filter;
250 struct OutputStream *ost;
251 struct FilterGraph *graph;
254 /* temporary storage until stream maps are processed */
255 AVFilterInOut *out_tmp;
256 enum AVMediaType type;
258 /* desired output stream properties */
260 AVRational frame_rate;
263 uint64_t channel_layout;
265 // those are only set if no format is specified and the encoder gives us multiple options
267 uint64_t *channel_layouts;
271 typedef struct FilterGraph {
273 const char *graph_desc;
275 AVFilterGraph *graph;
278 InputFilter **inputs;
280 OutputFilter **outputs;
284 typedef struct InputStream {
287 int discard; /* true if stream data should be discarded */
288 int user_set_discard;
289 int decoding_needed; /* non zero if the packets must be decoded in 'raw_fifo', see DECODING_FOR_* */
290 #define DECODING_FOR_OST 1
291 #define DECODING_FOR_FILTER 2
293 AVCodecContext *dec_ctx;
295 AVFrame *decoded_frame;
296 AVFrame *filter_frame; /* a ref of decoded_frame, to be sent to filters */
298 int64_t start; /* time when read started */
299 /* predicted dts of the next packet read for this stream or (when there are
300 * several frames in a packet) of the next frame in current packet (in AV_TIME_BASE units) */
302 int64_t dts; ///< dts of the last packet read for this stream (in AV_TIME_BASE units)
304 int64_t next_pts; ///< synthetic pts for the next decode frame (in AV_TIME_BASE units)
305 int64_t pts; ///< current pts of the decoded frame (in AV_TIME_BASE units)
306 int wrap_correction_done;
308 int64_t filter_in_rescale_delta_last;
310 int64_t min_pts; /* pts with the smallest value in a current stream */
311 int64_t max_pts; /* pts with the higher value in a current stream */
312 int64_t nb_samples; /* number of samples in the last decoded audio frame before looping */
316 AVDictionary *decoder_opts;
317 AVRational framerate; /* framerate forced with -r */
319 int guess_layout_max;
324 int resample_pix_fmt;
326 int resample_sample_fmt;
327 int resample_sample_rate;
328 int resample_channels;
329 uint64_t resample_channel_layout;
331 int fix_sub_duration;
332 struct { /* previous decoded subtitle and related variables */
347 /* decoded data from this stream goes into all those filters
348 * currently video and audio only */
349 InputFilter **filters;
354 /* hwaccel options */
355 enum HWAccelID hwaccel_id;
356 char *hwaccel_device;
357 enum AVPixelFormat hwaccel_output_format;
359 /* hwaccel context */
360 enum HWAccelID active_hwaccel_id;
362 void (*hwaccel_uninit)(AVCodecContext *s);
363 int (*hwaccel_get_buffer)(AVCodecContext *s, AVFrame *frame, int flags);
364 int (*hwaccel_retrieve_data)(AVCodecContext *s, AVFrame *frame);
365 enum AVPixelFormat hwaccel_pix_fmt;
366 enum AVPixelFormat hwaccel_retrieved_pix_fmt;
367 AVBufferRef *hw_frames_ctx;
370 // combined size of all the packets read
372 /* number of packets successfully read for this stream */
374 // number of frames/samples retrieved from the decoder
375 uint64_t frames_decoded;
376 uint64_t samples_decoded;
382 typedef struct InputFile {
383 AVFormatContext *ctx;
384 int eof_reached; /* true if eof reached */
385 int eagain; /* true if last read attempt returned EAGAIN */
386 int ist_index; /* index of first stream in input_streams */
387 int loop; /* set number of times input stream should be looped */
388 int64_t duration; /* actual duration of the longest stream in a file
389 at the moment when looping happens */
390 AVRational time_base; /* time base of the duration */
391 int64_t input_ts_offset;
395 int64_t start_time; /* user-specified start time in AV_TIME_BASE or AV_NOPTS_VALUE */
397 int64_t recording_time;
398 int nb_streams; /* number of stream that ffmpeg is aware of; may be different
399 from ctx.nb_streams if new streams appear during av_read_frame() */
400 int nb_streams_warn; /* number of streams that the user was warned of */
405 AVThreadMessageQueue *in_thread_queue;
406 pthread_t thread; /* thread reading from this file */
407 int non_blocking; /* reading packets from the thread should not block */
408 int joined; /* the thread has been joined */
409 int thread_queue_size; /* maximum number of queued packets */
413 enum forced_keyframes_const {
422 #define ABORT_ON_FLAG_EMPTY_OUTPUT (1 << 0)
424 extern const char *const forced_keyframes_const_names[];
427 ENCODER_FINISHED = 1,
431 typedef struct OutputStream {
432 int file_index; /* file index */
433 int index; /* stream index in the output file */
434 int source_index; /* InputStream index */
435 AVStream *st; /* stream in the output file */
436 int encoding_needed; /* true if encoding needed for this stream */
438 /* input pts and corresponding output pts
440 struct InputStream *sync_ist; /* input stream to sync against */
441 int64_t sync_opts; /* output frame counter, could be changed to some true timestamp */ // FIXME look at frame_number
442 /* pts of the first frame encoded for this stream, used for limiting
445 /* dts of the last packet sent to the muxer */
446 int64_t last_mux_dts;
448 int nb_bitstream_filters;
449 uint8_t *bsf_extradata_updated;
450 AVBSFContext **bsf_ctx;
452 AVCodecContext *enc_ctx;
453 AVCodecParameters *ref_par; /* associated input codec parameters with encoders options applied */
456 AVFrame *filtered_frame;
459 int last_nb0_frames[3];
464 AVRational frame_rate;
468 int rotate_overridden;
470 AVRational frame_aspect_ratio;
472 /* forced key frames */
473 int64_t *forced_kf_pts;
476 char *forced_keyframes;
477 AVExpr *forced_keyframes_pexpr;
478 double forced_keyframes_expr_const_values[FKF_NB];
481 int *audio_channels_map; /* list of the channels id to pick from the source stream */
482 int audio_channels_mapped; /* number of channels in audio_channels_map */
484 char *logfile_prefix;
487 OutputFilter *filter;
489 char *filters; ///< filtergraph associated to the -filter option
490 char *filters_script; ///< filtergraph script associated to the -filter_script option
492 AVDictionary *encoder_opts;
493 AVDictionary *sws_dict;
494 AVDictionary *swr_opts;
495 AVDictionary *resample_opts;
497 OSTFinished finished; /* no more packets should be written for this stream */
498 int unavailable; /* true if the steram is unavailable (possibly temporarily) */
501 // init_output_stream() has been called for this stream
502 // The encoder and the bitstream filters have been initialized and the stream
503 // parameters are set in the AVStream.
506 const char *attachment_filename;
507 int copy_initial_nonkeyframes;
508 int copy_prior_start;
513 AVCodecParserContext *parser;
514 AVCodecContext *parser_avctx;
517 // combined size of all the packets written
519 // number of packets send to the muxer
520 uint64_t packets_written;
521 // number of frames/samples sent to the encoder
522 uint64_t frames_encoded;
523 uint64_t samples_encoded;
525 /* packet quality factor */
528 int max_muxing_queue_size;
530 /* the packets are buffered here until the muxer is ready to be initialized */
531 AVFifoBuffer *muxing_queue;
533 /* packet picture type */
536 /* frame encode sum of squared error values */
540 typedef struct OutputFile {
541 AVFormatContext *ctx;
543 int ost_index; /* index of the first stream in output_streams */
544 int64_t recording_time; ///< desired length of the resulting file in microseconds == AV_TIME_BASE units
545 int64_t start_time; ///< start time in microseconds == AV_TIME_BASE units
546 uint64_t limit_filesize; /* filesize limit expressed in bytes */
553 extern InputStream **input_streams;
554 extern int nb_input_streams;
555 extern InputFile **input_files;
556 extern int nb_input_files;
558 extern OutputStream **output_streams;
559 extern int nb_output_streams;
560 extern OutputFile **output_files;
561 extern int nb_output_files;
563 extern FilterGraph **filtergraphs;
564 extern int nb_filtergraphs;
566 extern char *vstats_filename;
567 extern char *sdp_filename;
569 extern float audio_drift_threshold;
570 extern float dts_delta_threshold;
571 extern float dts_error_threshold;
573 extern int audio_volume;
574 extern int audio_sync_method;
575 extern int video_sync_method;
576 extern float frame_drop_threshold;
577 extern int do_benchmark;
578 extern int do_benchmark_all;
579 extern int do_deinterlace;
580 extern int do_hex_dump;
581 extern int do_pkt_dump;
583 extern int start_at_zero;
586 extern int exit_on_error;
587 extern int abort_on_flags;
588 extern int print_stats;
590 extern int stdin_interaction;
591 extern int frame_bits_per_raw_sample;
592 extern AVIOContext *progress_avio;
593 extern float max_error_rate;
594 extern char *videotoolbox_pixfmt;
596 extern int filter_nbthreads;
597 extern int filter_complex_nbthreads;
599 extern const AVIOInterruptCB int_cb;
601 extern const OptionDef options[];
602 extern const HWAccel hwaccels[];
603 extern int hwaccel_lax_profile_check;
604 extern AVBufferRef *hw_device_ctx;
607 void term_init(void);
608 void term_exit(void);
610 void reset_options(OptionsContext *o, int is_input);
611 void show_usage(void);
613 void opt_output_file(void *optctx, const char *filename);
615 void remove_avoptions(AVDictionary **a, AVDictionary *b);
616 void assert_avoptions(AVDictionary *m);
618 int guess_input_channel_layout(InputStream *ist);
620 enum AVPixelFormat choose_pixel_fmt(AVStream *st, AVCodecContext *avctx, AVCodec *codec, enum AVPixelFormat target);
621 void choose_sample_fmt(AVStream *st, AVCodec *codec);
623 int configure_filtergraph(FilterGraph *fg);
624 int configure_output_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out);
625 int ist_in_filtergraph(FilterGraph *fg, InputStream *ist);
626 int filtergraph_is_simple(FilterGraph *fg);
627 int init_simple_filtergraph(InputStream *ist, OutputStream *ost);
628 int init_complex_filtergraph(FilterGraph *fg);
630 int ifilter_parameters_from_frame(InputFilter *ifilter, const AVFrame *frame);
631 int ifilter_parameters_from_decoder(InputFilter *ifilter, const AVCodecContext *avctx);
633 int ffmpeg_parse_options(int argc, char **argv);
635 int vdpau_init(AVCodecContext *s);
636 int dxva2_init(AVCodecContext *s);
637 int vda_init(AVCodecContext *s);
638 int videotoolbox_init(AVCodecContext *s);
639 int qsv_init(AVCodecContext *s);
640 int qsv_transcode_init(OutputStream *ost);
641 int vaapi_decode_init(AVCodecContext *avctx);
642 int vaapi_device_init(const char *device);
643 int cuvid_init(AVCodecContext *s);
644 int cuvid_transcode_init(OutputStream *ost);
646 #endif /* FFMPEG_H */