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
19 #ifndef FFTOOLS_FFMPEG_H
20 #define FFTOOLS_FFMPEG_H
30 #include "libavformat/avformat.h"
31 #include "libavformat/avio.h"
33 #include "libavcodec/avcodec.h"
35 #include "libavfilter/avfilter.h"
37 #include "libavutil/avutil.h"
38 #include "libavutil/dict.h"
39 #include "libavutil/eval.h"
40 #include "libavutil/fifo.h"
41 #include "libavutil/hwcontext.h"
42 #include "libavutil/pixfmt.h"
43 #include "libavutil/rational.h"
44 #include "libavutil/thread.h"
45 #include "libavutil/threadmessage.h"
47 #include "libswresample/swresample.h"
50 #define VSYNC_PASSTHROUGH 0
53 #define VSYNC_VSCFR 0xfe
54 #define VSYNC_DROP 0xff
56 #define MAX_STREAMS 1024 /* arbitrary sanity check value */
66 typedef struct HWAccel {
68 int (*init)(AVCodecContext *s);
70 enum AVPixelFormat pix_fmt;
73 typedef struct HWDevice {
75 enum AVHWDeviceType type;
76 AVBufferRef *device_ref;
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 *max_frame_rates;
112 int nb_max_frame_rates;
113 SpecifierOpt *frame_sizes;
115 SpecifierOpt *frame_pix_fmts;
116 int nb_frame_pix_fmts;
119 int64_t input_ts_offset;
123 int thread_queue_size;
125 SpecifierOpt *ts_scale;
127 SpecifierOpt *dump_attachment;
128 int nb_dump_attachment;
129 SpecifierOpt *hwaccels;
131 SpecifierOpt *hwaccel_devices;
132 int nb_hwaccel_devices;
133 SpecifierOpt *hwaccel_output_formats;
134 int nb_hwaccel_output_formats;
135 SpecifierOpt *autorotate;
139 StreamMap *stream_maps;
141 AudioChannelMap *audio_channel_maps; /* one info entry per -map_channel */
142 int nb_audio_channel_maps; /* number of (valid) -map_channel settings */
143 int metadata_global_manual;
144 int metadata_streams_manual;
145 int metadata_chapters_manual;
146 const char **attachments;
149 int chapters_input_file;
151 int64_t recording_time;
153 uint64_t limit_filesize;
161 int subtitle_disable;
164 /* indexed by output file stream index */
168 SpecifierOpt *metadata;
170 SpecifierOpt *max_frames;
172 SpecifierOpt *bitstream_filters;
173 int nb_bitstream_filters;
174 SpecifierOpt *codec_tags;
176 SpecifierOpt *sample_fmts;
178 SpecifierOpt *qscale;
180 SpecifierOpt *forced_key_frames;
181 int nb_forced_key_frames;
182 SpecifierOpt *force_fps;
184 SpecifierOpt *frame_aspect_ratios;
185 int nb_frame_aspect_ratios;
186 SpecifierOpt *rc_overrides;
188 SpecifierOpt *intra_matrices;
189 int nb_intra_matrices;
190 SpecifierOpt *inter_matrices;
191 int nb_inter_matrices;
192 SpecifierOpt *chroma_intra_matrices;
193 int nb_chroma_intra_matrices;
194 SpecifierOpt *top_field_first;
195 int nb_top_field_first;
196 SpecifierOpt *metadata_map;
198 SpecifierOpt *presets;
200 SpecifierOpt *copy_initial_nonkeyframes;
201 int nb_copy_initial_nonkeyframes;
202 SpecifierOpt *copy_prior_start;
203 int nb_copy_prior_start;
204 SpecifierOpt *filters;
206 SpecifierOpt *filter_scripts;
207 int nb_filter_scripts;
208 SpecifierOpt *reinit_filters;
209 int nb_reinit_filters;
210 SpecifierOpt *fix_sub_duration;
211 int nb_fix_sub_duration;
212 SpecifierOpt *canvas_sizes;
216 SpecifierOpt *passlogfiles;
218 SpecifierOpt *max_muxing_queue_size;
219 int nb_max_muxing_queue_size;
220 SpecifierOpt *muxing_queue_data_threshold;
221 int nb_muxing_queue_data_threshold;
222 SpecifierOpt *guess_layout_max;
223 int nb_guess_layout_max;
226 SpecifierOpt *discard;
228 SpecifierOpt *disposition;
230 SpecifierOpt *program;
232 SpecifierOpt *time_bases;
234 SpecifierOpt *enc_time_bases;
235 int nb_enc_time_bases;
236 SpecifierOpt *autoscale;
240 typedef struct InputFilter {
241 AVFilterContext *filter;
242 struct InputStream *ist;
243 struct FilterGraph *graph;
245 enum AVMediaType type; // AVMEDIA_TYPE_SUBTITLE for sub2video
247 AVFifoBuffer *frame_queue;
249 // parameters configured for this input
253 AVRational sample_aspect_ratio;
257 uint64_t channel_layout;
259 AVBufferRef *hw_frames_ctx;
264 typedef struct OutputFilter {
265 AVFilterContext *filter;
266 struct OutputStream *ost;
267 struct FilterGraph *graph;
270 /* temporary storage until stream maps are processed */
271 AVFilterInOut *out_tmp;
272 enum AVMediaType type;
274 /* desired output stream properties */
276 AVRational frame_rate;
279 uint64_t channel_layout;
281 // those are only set if no format is specified and the encoder gives us multiple options
283 uint64_t *channel_layouts;
287 typedef struct FilterGraph {
289 const char *graph_desc;
291 AVFilterGraph *graph;
294 InputFilter **inputs;
296 OutputFilter **outputs;
300 typedef struct InputStream {
303 int discard; /* true if stream data should be discarded */
304 int user_set_discard;
305 int decoding_needed; /* non zero if the packets must be decoded in 'raw_fifo', see DECODING_FOR_* */
306 #define DECODING_FOR_OST 1
307 #define DECODING_FOR_FILTER 2
309 AVCodecContext *dec_ctx;
311 AVFrame *decoded_frame;
312 AVFrame *filter_frame; /* a ref of decoded_frame, to be sent to filters */
315 int64_t start; /* time when read started */
316 /* predicted dts of the next packet read for this stream or (when there are
317 * several frames in a packet) of the next frame in current packet (in AV_TIME_BASE units) */
319 int64_t dts; ///< dts of the last packet read for this stream (in AV_TIME_BASE units)
321 int64_t next_pts; ///< synthetic pts for the next decode frame (in AV_TIME_BASE units)
322 int64_t pts; ///< current pts of the decoded frame (in AV_TIME_BASE units)
323 int wrap_correction_done;
325 int64_t filter_in_rescale_delta_last;
327 int64_t min_pts; /* pts with the smallest value in a current stream */
328 int64_t max_pts; /* pts with the higher value in a current stream */
330 // when forcing constant input framerate through -r,
331 // this contains the pts that will be given to the next decoded frame
332 int64_t cfr_next_pts;
334 int64_t nb_samples; /* number of samples in the last decoded audio frame before looping */
338 AVDictionary *decoder_opts;
339 AVRational framerate; /* framerate forced with -r */
341 int guess_layout_max;
345 int fix_sub_duration;
346 struct { /* previous decoded subtitle and related variables */
355 AVFifoBuffer *sub_queue; ///< queue of AVSubtitle* before filter init
358 unsigned int initialize; ///< marks if sub2video_update should force an initialization
363 /* decoded data from this stream goes into all those filters
364 * currently video and audio only */
365 InputFilter **filters;
370 /* hwaccel options */
371 enum HWAccelID hwaccel_id;
372 enum AVHWDeviceType hwaccel_device_type;
373 char *hwaccel_device;
374 enum AVPixelFormat hwaccel_output_format;
376 /* hwaccel context */
378 void (*hwaccel_uninit)(AVCodecContext *s);
379 int (*hwaccel_get_buffer)(AVCodecContext *s, AVFrame *frame, int flags);
380 int (*hwaccel_retrieve_data)(AVCodecContext *s, AVFrame *frame);
381 enum AVPixelFormat hwaccel_pix_fmt;
382 enum AVPixelFormat hwaccel_retrieved_pix_fmt;
383 AVBufferRef *hw_frames_ctx;
386 // combined size of all the packets read
388 /* number of packets successfully read for this stream */
390 // number of frames/samples retrieved from the decoder
391 uint64_t frames_decoded;
392 uint64_t samples_decoded;
400 typedef struct InputFile {
401 AVFormatContext *ctx;
402 int eof_reached; /* true if eof reached */
403 int eagain; /* true if last read attempt returned EAGAIN */
404 int ist_index; /* index of first stream in input_streams */
405 int loop; /* set number of times input stream should be looped */
406 int64_t duration; /* actual duration of the longest stream in a file
407 at the moment when looping happens */
408 AVRational time_base; /* time base of the duration */
409 int64_t input_ts_offset;
413 int64_t start_time; /* user-specified start time in AV_TIME_BASE or AV_NOPTS_VALUE */
415 int64_t recording_time;
416 int nb_streams; /* number of stream that ffmpeg is aware of; may be different
417 from ctx.nb_streams if new streams appear during av_read_frame() */
418 int nb_streams_warn; /* number of streams that the user was warned of */
425 AVThreadMessageQueue *in_thread_queue;
426 pthread_t thread; /* thread reading from this file */
427 int non_blocking; /* reading packets from the thread should not block */
428 int joined; /* the thread has been joined */
429 int thread_queue_size; /* maximum number of queued packets */
433 enum forced_keyframes_const {
442 #define ABORT_ON_FLAG_EMPTY_OUTPUT (1 << 0)
443 #define ABORT_ON_FLAG_EMPTY_OUTPUT_STREAM (1 << 1)
445 extern const char *const forced_keyframes_const_names[];
448 ENCODER_FINISHED = 1,
452 typedef struct OutputStream {
453 int file_index; /* file index */
454 int index; /* stream index in the output file */
455 int source_index; /* InputStream index */
456 AVStream *st; /* stream in the output file */
457 int encoding_needed; /* true if encoding needed for this stream */
459 /* input pts and corresponding output pts
461 struct InputStream *sync_ist; /* input stream to sync against */
462 int64_t sync_opts; /* output frame counter, could be changed to some true timestamp */ // FIXME look at frame_number
463 /* pts of the first frame encoded for this stream, used for limiting
466 /* dts of the last packet sent to the muxer */
467 int64_t last_mux_dts;
468 // the timebase of the packets sent to the muxer
469 AVRational mux_timebase;
470 AVRational enc_timebase;
472 AVBSFContext *bsf_ctx;
474 AVCodecContext *enc_ctx;
475 AVCodecParameters *ref_par; /* associated input codec parameters with encoders options applied */
478 AVFrame *filtered_frame;
482 int last_nb0_frames[3];
487 AVRational frame_rate;
488 AVRational max_frame_rate;
492 int rotate_overridden;
494 double rotate_override_value;
496 AVRational frame_aspect_ratio;
498 /* forced key frames */
499 int64_t forced_kf_ref_pts;
500 int64_t *forced_kf_pts;
503 char *forced_keyframes;
504 AVExpr *forced_keyframes_pexpr;
505 double forced_keyframes_expr_const_values[FKF_NB];
508 int *audio_channels_map; /* list of the channels id to pick from the source stream */
509 int audio_channels_mapped; /* number of channels in audio_channels_map */
511 char *logfile_prefix;
514 OutputFilter *filter;
516 char *filters; ///< filtergraph associated to the -filter option
517 char *filters_script; ///< filtergraph script associated to the -filter_script option
519 AVDictionary *encoder_opts;
520 AVDictionary *sws_dict;
521 AVDictionary *swr_opts;
522 AVDictionary *resample_opts;
524 OSTFinished finished; /* no more packets should be written for this stream */
525 int unavailable; /* true if the steram is unavailable (possibly temporarily) */
528 // init_output_stream() has been called for this stream
529 // The encoder and the bitstream filters have been initialized and the stream
530 // parameters are set in the AVStream.
535 const char *attachment_filename;
536 int copy_initial_nonkeyframes;
537 int copy_prior_start;
543 // combined size of all the packets written
545 // number of packets send to the muxer
546 uint64_t packets_written;
547 // number of frames/samples sent to the encoder
548 uint64_t frames_encoded;
549 uint64_t samples_encoded;
551 /* packet quality factor */
554 int max_muxing_queue_size;
556 /* the packets are buffered here until the muxer is ready to be initialized */
557 AVFifoBuffer *muxing_queue;
560 * The size of the AVPackets' buffers in queue.
561 * Updated when a packet is either pushed or pulled from the queue.
563 size_t muxing_queue_data_size;
565 /* Threshold after which max_muxing_queue_size will be in effect */
566 size_t muxing_queue_data_threshold;
568 /* packet picture type */
571 /* frame encode sum of squared error values */
575 typedef struct OutputFile {
576 AVFormatContext *ctx;
578 int ost_index; /* index of the first stream in output_streams */
579 int64_t recording_time; ///< desired length of the resulting file in microseconds == AV_TIME_BASE units
580 int64_t start_time; ///< start time in microseconds == AV_TIME_BASE units
581 uint64_t limit_filesize; /* filesize limit expressed in bytes */
588 extern InputStream **input_streams;
589 extern int nb_input_streams;
590 extern InputFile **input_files;
591 extern int nb_input_files;
593 extern OutputStream **output_streams;
594 extern int nb_output_streams;
595 extern OutputFile **output_files;
596 extern int nb_output_files;
598 extern FilterGraph **filtergraphs;
599 extern int nb_filtergraphs;
601 extern char *vstats_filename;
602 extern char *sdp_filename;
604 extern float audio_drift_threshold;
605 extern float dts_delta_threshold;
606 extern float dts_error_threshold;
608 extern int audio_volume;
609 extern int audio_sync_method;
610 extern int video_sync_method;
611 extern float frame_drop_threshold;
612 extern int do_benchmark;
613 extern int do_benchmark_all;
614 extern int do_deinterlace;
615 extern int do_hex_dump;
616 extern int do_pkt_dump;
618 extern int start_at_zero;
621 extern int exit_on_error;
622 extern int abort_on_flags;
623 extern int print_stats;
624 extern int64_t stats_period;
626 extern int stdin_interaction;
627 extern int frame_bits_per_raw_sample;
628 extern AVIOContext *progress_avio;
629 extern float max_error_rate;
630 extern char *videotoolbox_pixfmt;
632 extern int filter_nbthreads;
633 extern int filter_complex_nbthreads;
634 extern int vstats_version;
635 extern int auto_conversion_filters;
637 extern const AVIOInterruptCB int_cb;
639 extern const OptionDef options[];
640 extern const HWAccel hwaccels[];
642 extern char *qsv_device;
644 extern HWDevice *filter_hw_device;
647 void term_init(void);
648 void term_exit(void);
650 void show_usage(void);
652 void remove_avoptions(AVDictionary **a, AVDictionary *b);
653 void assert_avoptions(AVDictionary *m);
655 int guess_input_channel_layout(InputStream *ist);
657 int configure_filtergraph(FilterGraph *fg);
658 void check_filter_outputs(void);
659 int filtergraph_is_simple(FilterGraph *fg);
660 int init_simple_filtergraph(InputStream *ist, OutputStream *ost);
661 int init_complex_filtergraph(FilterGraph *fg);
663 void sub2video_update(InputStream *ist, int64_t heartbeat_pts, AVSubtitle *sub);
665 int ifilter_parameters_from_frame(InputFilter *ifilter, const AVFrame *frame);
667 int ffmpeg_parse_options(int argc, char **argv);
669 int videotoolbox_init(AVCodecContext *s);
670 int qsv_init(AVCodecContext *s);
672 HWDevice *hw_device_get_by_name(const char *name);
673 int hw_device_init_from_string(const char *arg, HWDevice **dev);
674 void hw_device_free_all(void);
676 int hw_device_setup_for_decode(InputStream *ist);
677 int hw_device_setup_for_encode(OutputStream *ost);
678 int hw_device_setup_for_filter(FilterGraph *fg);
680 int hwaccel_decode_init(AVCodecContext *avctx);
682 #endif /* FFTOOLS_FFMPEG_H */