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 *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;
159 int subtitle_disable;
162 /* indexed by output file stream index */
166 SpecifierOpt *metadata;
168 SpecifierOpt *max_frames;
170 SpecifierOpt *bitstream_filters;
171 int nb_bitstream_filters;
172 SpecifierOpt *codec_tags;
174 SpecifierOpt *sample_fmts;
176 SpecifierOpt *qscale;
178 SpecifierOpt *forced_key_frames;
179 int nb_forced_key_frames;
180 SpecifierOpt *force_fps;
182 SpecifierOpt *frame_aspect_ratios;
183 int nb_frame_aspect_ratios;
184 SpecifierOpt *rc_overrides;
186 SpecifierOpt *intra_matrices;
187 int nb_intra_matrices;
188 SpecifierOpt *inter_matrices;
189 int nb_inter_matrices;
190 SpecifierOpt *chroma_intra_matrices;
191 int nb_chroma_intra_matrices;
192 SpecifierOpt *top_field_first;
193 int nb_top_field_first;
194 SpecifierOpt *metadata_map;
196 SpecifierOpt *presets;
198 SpecifierOpt *copy_initial_nonkeyframes;
199 int nb_copy_initial_nonkeyframes;
200 SpecifierOpt *copy_prior_start;
201 int nb_copy_prior_start;
202 SpecifierOpt *filters;
204 SpecifierOpt *filter_scripts;
205 int nb_filter_scripts;
206 SpecifierOpt *reinit_filters;
207 int nb_reinit_filters;
208 SpecifierOpt *fix_sub_duration;
209 int nb_fix_sub_duration;
210 SpecifierOpt *canvas_sizes;
214 SpecifierOpt *passlogfiles;
216 SpecifierOpt *max_muxing_queue_size;
217 int nb_max_muxing_queue_size;
218 SpecifierOpt *guess_layout_max;
219 int nb_guess_layout_max;
222 SpecifierOpt *discard;
224 SpecifierOpt *disposition;
226 SpecifierOpt *program;
228 SpecifierOpt *time_bases;
230 SpecifierOpt *enc_time_bases;
231 int nb_enc_time_bases;
232 SpecifierOpt *autoscale;
236 typedef struct InputFilter {
237 AVFilterContext *filter;
238 struct InputStream *ist;
239 struct FilterGraph *graph;
241 enum AVMediaType type; // AVMEDIA_TYPE_SUBTITLE for sub2video
243 AVFifoBuffer *frame_queue;
245 // parameters configured for this input
249 AVRational sample_aspect_ratio;
253 uint64_t channel_layout;
255 AVBufferRef *hw_frames_ctx;
260 typedef struct OutputFilter {
261 AVFilterContext *filter;
262 struct OutputStream *ost;
263 struct FilterGraph *graph;
266 /* temporary storage until stream maps are processed */
267 AVFilterInOut *out_tmp;
268 enum AVMediaType type;
270 /* desired output stream properties */
272 AVRational frame_rate;
275 uint64_t channel_layout;
277 // those are only set if no format is specified and the encoder gives us multiple options
279 uint64_t *channel_layouts;
283 typedef struct FilterGraph {
285 const char *graph_desc;
287 AVFilterGraph *graph;
290 InputFilter **inputs;
292 OutputFilter **outputs;
296 typedef struct InputStream {
299 int discard; /* true if stream data should be discarded */
300 int user_set_discard;
301 int decoding_needed; /* non zero if the packets must be decoded in 'raw_fifo', see DECODING_FOR_* */
302 #define DECODING_FOR_OST 1
303 #define DECODING_FOR_FILTER 2
305 AVCodecContext *dec_ctx;
307 AVFrame *decoded_frame;
308 AVFrame *filter_frame; /* a ref of decoded_frame, to be sent to filters */
310 int64_t start; /* time when read started */
311 /* predicted dts of the next packet read for this stream or (when there are
312 * several frames in a packet) of the next frame in current packet (in AV_TIME_BASE units) */
314 int64_t dts; ///< dts of the last packet read for this stream (in AV_TIME_BASE units)
316 int64_t next_pts; ///< synthetic pts for the next decode frame (in AV_TIME_BASE units)
317 int64_t pts; ///< current pts of the decoded frame (in AV_TIME_BASE units)
318 int wrap_correction_done;
320 int64_t filter_in_rescale_delta_last;
322 int64_t min_pts; /* pts with the smallest value in a current stream */
323 int64_t max_pts; /* pts with the higher value in a current stream */
325 // when forcing constant input framerate through -r,
326 // this contains the pts that will be given to the next decoded frame
327 int64_t cfr_next_pts;
329 int64_t nb_samples; /* number of samples in the last decoded audio frame before looping */
333 AVDictionary *decoder_opts;
334 AVRational framerate; /* framerate forced with -r */
336 int guess_layout_max;
340 int fix_sub_duration;
341 struct { /* previous decoded subtitle and related variables */
350 AVFifoBuffer *sub_queue; ///< queue of AVSubtitle* before filter init
353 unsigned int initialize; ///< marks if sub2video_update should force an initialization
358 /* decoded data from this stream goes into all those filters
359 * currently video and audio only */
360 InputFilter **filters;
365 /* hwaccel options */
366 enum HWAccelID hwaccel_id;
367 enum AVHWDeviceType hwaccel_device_type;
368 char *hwaccel_device;
369 enum AVPixelFormat hwaccel_output_format;
371 /* hwaccel context */
373 void (*hwaccel_uninit)(AVCodecContext *s);
374 int (*hwaccel_get_buffer)(AVCodecContext *s, AVFrame *frame, int flags);
375 int (*hwaccel_retrieve_data)(AVCodecContext *s, AVFrame *frame);
376 enum AVPixelFormat hwaccel_pix_fmt;
377 enum AVPixelFormat hwaccel_retrieved_pix_fmt;
378 AVBufferRef *hw_frames_ctx;
381 // combined size of all the packets read
383 /* number of packets successfully read for this stream */
385 // number of frames/samples retrieved from the decoder
386 uint64_t frames_decoded;
387 uint64_t samples_decoded;
395 typedef struct InputFile {
396 AVFormatContext *ctx;
397 int eof_reached; /* true if eof reached */
398 int eagain; /* true if last read attempt returned EAGAIN */
399 int ist_index; /* index of first stream in input_streams */
400 int loop; /* set number of times input stream should be looped */
401 int64_t duration; /* actual duration of the longest stream in a file
402 at the moment when looping happens */
403 AVRational time_base; /* time base of the duration */
404 int64_t input_ts_offset;
408 int64_t start_time; /* user-specified start time in AV_TIME_BASE or AV_NOPTS_VALUE */
410 int64_t recording_time;
411 int nb_streams; /* number of stream that ffmpeg is aware of; may be different
412 from ctx.nb_streams if new streams appear during av_read_frame() */
413 int nb_streams_warn; /* number of streams that the user was warned of */
418 AVThreadMessageQueue *in_thread_queue;
419 pthread_t thread; /* thread reading from this file */
420 int non_blocking; /* reading packets from the thread should not block */
421 int joined; /* the thread has been joined */
422 int thread_queue_size; /* maximum number of queued packets */
426 enum forced_keyframes_const {
435 #define ABORT_ON_FLAG_EMPTY_OUTPUT (1 << 0)
436 #define ABORT_ON_FLAG_EMPTY_OUTPUT_STREAM (1 << 1)
438 extern const char *const forced_keyframes_const_names[];
441 ENCODER_FINISHED = 1,
445 typedef struct OutputStream {
446 int file_index; /* file index */
447 int index; /* stream index in the output file */
448 int source_index; /* InputStream index */
449 AVStream *st; /* stream in the output file */
450 int encoding_needed; /* true if encoding needed for this stream */
452 /* input pts and corresponding output pts
454 struct InputStream *sync_ist; /* input stream to sync against */
455 int64_t sync_opts; /* output frame counter, could be changed to some true timestamp */ // FIXME look at frame_number
456 /* pts of the first frame encoded for this stream, used for limiting
459 /* dts of the last packet sent to the muxer */
460 int64_t last_mux_dts;
461 // the timebase of the packets sent to the muxer
462 AVRational mux_timebase;
463 AVRational enc_timebase;
465 AVBSFContext *bsf_ctx;
467 AVCodecContext *enc_ctx;
468 AVCodecParameters *ref_par; /* associated input codec parameters with encoders options applied */
471 AVFrame *filtered_frame;
474 int last_nb0_frames[3];
479 AVRational frame_rate;
483 int rotate_overridden;
485 double rotate_override_value;
487 AVRational frame_aspect_ratio;
489 /* forced key frames */
490 int64_t forced_kf_ref_pts;
491 int64_t *forced_kf_pts;
494 char *forced_keyframes;
495 AVExpr *forced_keyframes_pexpr;
496 double forced_keyframes_expr_const_values[FKF_NB];
499 int *audio_channels_map; /* list of the channels id to pick from the source stream */
500 int audio_channels_mapped; /* number of channels in audio_channels_map */
502 char *logfile_prefix;
505 OutputFilter *filter;
507 char *filters; ///< filtergraph associated to the -filter option
508 char *filters_script; ///< filtergraph script associated to the -filter_script option
510 AVDictionary *encoder_opts;
511 AVDictionary *sws_dict;
512 AVDictionary *swr_opts;
513 AVDictionary *resample_opts;
515 OSTFinished finished; /* no more packets should be written for this stream */
516 int unavailable; /* true if the steram is unavailable (possibly temporarily) */
519 // init_output_stream() has been called for this stream
520 // The encoder and the bitstream filters have been initialized and the stream
521 // parameters are set in the AVStream.
526 const char *attachment_filename;
527 int copy_initial_nonkeyframes;
528 int copy_prior_start;
534 // combined size of all the packets written
536 // number of packets send to the muxer
537 uint64_t packets_written;
538 // number of frames/samples sent to the encoder
539 uint64_t frames_encoded;
540 uint64_t samples_encoded;
542 /* packet quality factor */
545 int max_muxing_queue_size;
547 /* the packets are buffered here until the muxer is ready to be initialized */
548 AVFifoBuffer *muxing_queue;
550 /* packet picture type */
553 /* frame encode sum of squared error values */
557 typedef struct OutputFile {
558 AVFormatContext *ctx;
560 int ost_index; /* index of the first stream in output_streams */
561 int64_t recording_time; ///< desired length of the resulting file in microseconds == AV_TIME_BASE units
562 int64_t start_time; ///< start time in microseconds == AV_TIME_BASE units
563 uint64_t limit_filesize; /* filesize limit expressed in bytes */
570 extern InputStream **input_streams;
571 extern int nb_input_streams;
572 extern InputFile **input_files;
573 extern int nb_input_files;
575 extern OutputStream **output_streams;
576 extern int nb_output_streams;
577 extern OutputFile **output_files;
578 extern int nb_output_files;
580 extern FilterGraph **filtergraphs;
581 extern int nb_filtergraphs;
583 extern char *vstats_filename;
584 extern char *sdp_filename;
586 extern float audio_drift_threshold;
587 extern float dts_delta_threshold;
588 extern float dts_error_threshold;
590 extern int audio_volume;
591 extern int audio_sync_method;
592 extern int video_sync_method;
593 extern float frame_drop_threshold;
594 extern int do_benchmark;
595 extern int do_benchmark_all;
596 extern int do_deinterlace;
597 extern int do_hex_dump;
598 extern int do_pkt_dump;
600 extern int start_at_zero;
603 extern int exit_on_error;
604 extern int abort_on_flags;
605 extern int print_stats;
607 extern int stdin_interaction;
608 extern int frame_bits_per_raw_sample;
609 extern AVIOContext *progress_avio;
610 extern float max_error_rate;
611 extern char *videotoolbox_pixfmt;
613 extern int filter_nbthreads;
614 extern int filter_complex_nbthreads;
615 extern int vstats_version;
617 extern const AVIOInterruptCB int_cb;
619 extern const OptionDef options[];
620 extern const HWAccel hwaccels[];
622 extern char *qsv_device;
624 extern HWDevice *filter_hw_device;
627 void term_init(void);
628 void term_exit(void);
630 void reset_options(OptionsContext *o, int is_input);
631 void show_usage(void);
633 void opt_output_file(void *optctx, const char *filename);
635 void remove_avoptions(AVDictionary **a, AVDictionary *b);
636 void assert_avoptions(AVDictionary *m);
638 int guess_input_channel_layout(InputStream *ist);
640 enum AVPixelFormat choose_pixel_fmt(AVStream *st, AVCodecContext *avctx, AVCodec *codec, enum AVPixelFormat target);
641 void choose_sample_fmt(AVStream *st, AVCodec *codec);
643 int configure_filtergraph(FilterGraph *fg);
644 int configure_output_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out);
645 void check_filter_outputs(void);
646 int ist_in_filtergraph(FilterGraph *fg, InputStream *ist);
647 int filtergraph_is_simple(FilterGraph *fg);
648 int init_simple_filtergraph(InputStream *ist, OutputStream *ost);
649 int init_complex_filtergraph(FilterGraph *fg);
651 void sub2video_update(InputStream *ist, int64_t heartbeat_pts, AVSubtitle *sub);
653 int ifilter_parameters_from_frame(InputFilter *ifilter, const AVFrame *frame);
655 int ffmpeg_parse_options(int argc, char **argv);
657 int videotoolbox_init(AVCodecContext *s);
658 int qsv_init(AVCodecContext *s);
660 HWDevice *hw_device_get_by_name(const char *name);
661 int hw_device_init_from_string(const char *arg, HWDevice **dev);
662 void hw_device_free_all(void);
664 int hw_device_setup_for_decode(InputStream *ist);
665 int hw_device_setup_for_encode(OutputStream *ost);
666 int hw_device_setup_for_filter(FilterGraph *fg);
668 int hwaccel_decode_init(AVCodecContext *avctx);
670 #endif /* FFTOOLS_FFMPEG_H */