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 */
69 typedef struct HWAccel {
71 int (*init)(AVCodecContext *s);
73 enum AVPixelFormat pix_fmt;
76 /* select an input stream for an output stream */
77 typedef struct StreamMap {
78 int disabled; /* 1 is this mapping is disabled by a negative map */
82 int sync_stream_index;
83 char *linklabel; /* name of an output link, for mapping lavfi outputs */
87 int file_idx, stream_idx, channel_idx; // input
88 int ofile_idx, ostream_idx; // output
91 typedef struct OptionsContext {
94 /* input/output options */
96 int64_t start_time_eof;
100 SpecifierOpt *codec_names;
102 SpecifierOpt *audio_channels;
103 int nb_audio_channels;
104 SpecifierOpt *audio_sample_rate;
105 int nb_audio_sample_rate;
106 SpecifierOpt *frame_rates;
108 SpecifierOpt *frame_sizes;
110 SpecifierOpt *frame_pix_fmts;
111 int nb_frame_pix_fmts;
114 int64_t input_ts_offset;
117 int thread_queue_size;
119 SpecifierOpt *ts_scale;
121 SpecifierOpt *dump_attachment;
122 int nb_dump_attachment;
123 SpecifierOpt *hwaccels;
125 SpecifierOpt *hwaccel_devices;
126 int nb_hwaccel_devices;
127 SpecifierOpt *autorotate;
131 StreamMap *stream_maps;
133 AudioChannelMap *audio_channel_maps; /* one info entry per -map_channel */
134 int nb_audio_channel_maps; /* number of (valid) -map_channel settings */
135 int metadata_global_manual;
136 int metadata_streams_manual;
137 int metadata_chapters_manual;
138 const char **attachments;
141 int chapters_input_file;
143 int64_t recording_time;
145 uint64_t limit_filesize;
152 int subtitle_disable;
155 /* indexed by output file stream index */
159 SpecifierOpt *metadata;
161 SpecifierOpt *max_frames;
163 SpecifierOpt *bitstream_filters;
164 int nb_bitstream_filters;
165 SpecifierOpt *codec_tags;
167 SpecifierOpt *sample_fmts;
169 SpecifierOpt *qscale;
171 SpecifierOpt *forced_key_frames;
172 int nb_forced_key_frames;
173 SpecifierOpt *force_fps;
175 SpecifierOpt *frame_aspect_ratios;
176 int nb_frame_aspect_ratios;
177 SpecifierOpt *rc_overrides;
179 SpecifierOpt *intra_matrices;
180 int nb_intra_matrices;
181 SpecifierOpt *inter_matrices;
182 int nb_inter_matrices;
183 SpecifierOpt *chroma_intra_matrices;
184 int nb_chroma_intra_matrices;
185 SpecifierOpt *top_field_first;
186 int nb_top_field_first;
187 SpecifierOpt *metadata_map;
189 SpecifierOpt *presets;
191 SpecifierOpt *copy_initial_nonkeyframes;
192 int nb_copy_initial_nonkeyframes;
193 SpecifierOpt *copy_prior_start;
194 int nb_copy_prior_start;
195 SpecifierOpt *filters;
197 SpecifierOpt *filter_scripts;
198 int nb_filter_scripts;
199 SpecifierOpt *reinit_filters;
200 int nb_reinit_filters;
201 SpecifierOpt *fix_sub_duration;
202 int nb_fix_sub_duration;
203 SpecifierOpt *canvas_sizes;
207 SpecifierOpt *passlogfiles;
209 SpecifierOpt *guess_layout_max;
210 int nb_guess_layout_max;
213 SpecifierOpt *discard;
215 SpecifierOpt *disposition;
219 typedef struct InputFilter {
220 AVFilterContext *filter;
221 struct InputStream *ist;
222 struct FilterGraph *graph;
226 typedef struct OutputFilter {
227 AVFilterContext *filter;
228 struct OutputStream *ost;
229 struct FilterGraph *graph;
232 /* temporary storage until stream maps are processed */
233 AVFilterInOut *out_tmp;
234 enum AVMediaType type;
237 typedef struct FilterGraph {
239 const char *graph_desc;
241 AVFilterGraph *graph;
244 InputFilter **inputs;
246 OutputFilter **outputs;
250 typedef struct InputStream {
253 int discard; /* true if stream data should be discarded */
254 int user_set_discard;
255 int decoding_needed; /* non zero if the packets must be decoded in 'raw_fifo', see DECODING_FOR_* */
256 #define DECODING_FOR_OST 1
257 #define DECODING_FOR_FILTER 2
259 AVCodecContext *dec_ctx;
261 AVFrame *decoded_frame;
262 AVFrame *filter_frame; /* a ref of decoded_frame, to be sent to filters */
264 int64_t start; /* time when read started */
265 /* predicted dts of the next packet read for this stream or (when there are
266 * several frames in a packet) of the next frame in current packet (in AV_TIME_BASE units) */
268 int64_t dts; ///< dts of the last packet read for this stream (in AV_TIME_BASE units)
270 int64_t next_pts; ///< synthetic pts for the next decode frame (in AV_TIME_BASE units)
271 int64_t pts; ///< current pts of the decoded frame (in AV_TIME_BASE units)
272 int wrap_correction_done;
274 int64_t filter_in_rescale_delta_last;
278 int showed_multi_packet_warning;
279 AVDictionary *decoder_opts;
280 AVRational framerate; /* framerate forced with -r */
282 int guess_layout_max;
287 int resample_pix_fmt;
289 int resample_sample_fmt;
290 int resample_sample_rate;
291 int resample_channels;
292 uint64_t resample_channel_layout;
294 int fix_sub_duration;
295 struct { /* previous decoded subtitle and related variables */
310 /* decoded data from this stream goes into all those filters
311 * currently video and audio only */
312 InputFilter **filters;
317 /* hwaccel options */
318 enum HWAccelID hwaccel_id;
319 char *hwaccel_device;
321 /* hwaccel context */
322 enum HWAccelID active_hwaccel_id;
324 void (*hwaccel_uninit)(AVCodecContext *s);
325 int (*hwaccel_get_buffer)(AVCodecContext *s, AVFrame *frame, int flags);
326 int (*hwaccel_retrieve_data)(AVCodecContext *s, AVFrame *frame);
327 enum AVPixelFormat hwaccel_pix_fmt;
328 enum AVPixelFormat hwaccel_retrieved_pix_fmt;
331 // combined size of all the packets read
333 /* number of packets successfully read for this stream */
335 // number of frames/samples retrieved from the decoder
336 uint64_t frames_decoded;
337 uint64_t samples_decoded;
340 typedef struct InputFile {
341 AVFormatContext *ctx;
342 int eof_reached; /* true if eof reached */
343 int eagain; /* true if last read attempt returned EAGAIN */
344 int ist_index; /* index of first stream in input_streams */
345 int64_t input_ts_offset;
348 int64_t start_time; /* user-specified start time in AV_TIME_BASE or AV_NOPTS_VALUE */
350 int64_t recording_time;
351 int nb_streams; /* number of stream that ffmpeg is aware of; may be different
352 from ctx.nb_streams if new streams appear during av_read_frame() */
353 int nb_streams_warn; /* number of streams that the user was warned of */
358 AVThreadMessageQueue *in_thread_queue;
359 pthread_t thread; /* thread reading from this file */
360 int non_blocking; /* reading packets from the thread should not block */
361 int joined; /* the thread has been joined */
362 int thread_queue_size; /* maximum number of queued packets */
366 enum forced_keyframes_const {
375 extern const char *const forced_keyframes_const_names[];
378 ENCODER_FINISHED = 1,
382 typedef struct OutputStream {
383 int file_index; /* file index */
384 int index; /* stream index in the output file */
385 int source_index; /* InputStream index */
386 AVStream *st; /* stream in the output file */
387 int encoding_needed; /* true if encoding needed for this stream */
389 /* input pts and corresponding output pts
391 struct InputStream *sync_ist; /* input stream to sync against */
392 int64_t sync_opts; /* output frame counter, could be changed to some true timestamp */ // FIXME look at frame_number
393 /* pts of the first frame encoded for this stream, used for limiting
396 /* dts of the last packet sent to the muxer */
397 int64_t last_mux_dts;
398 AVBitStreamFilterContext *bitstream_filters;
399 AVCodecContext *enc_ctx;
402 AVFrame *filtered_frame;
405 int last_nb0_frames[3];
408 AVRational frame_rate;
411 int rotate_overridden;
413 AVRational frame_aspect_ratio;
415 /* forced key frames */
416 int64_t *forced_kf_pts;
419 char *forced_keyframes;
420 AVExpr *forced_keyframes_pexpr;
421 double forced_keyframes_expr_const_values[FKF_NB];
424 int *audio_channels_map; /* list of the channels id to pick from the source stream */
425 int audio_channels_mapped; /* number of channels in audio_channels_map */
427 char *logfile_prefix;
430 OutputFilter *filter;
432 char *filters; ///< filtergraph associated to the -filter option
433 char *filters_script; ///< filtergraph script associated to the -filter_script option
435 AVDictionary *encoder_opts;
436 AVDictionary *sws_dict;
437 AVDictionary *swr_opts;
438 AVDictionary *resample_opts;
439 AVDictionary *bsf_args;
441 OSTFinished finished; /* no more packets should be written for this stream */
442 int unavailable; /* true if the steram is unavailable (possibly temporarily) */
444 const char *attachment_filename;
445 int copy_initial_nonkeyframes;
446 int copy_prior_start;
451 AVCodecParserContext *parser;
454 // combined size of all the packets written
456 // number of packets send to the muxer
457 uint64_t packets_written;
458 // number of frames/samples sent to the encoder
459 uint64_t frames_encoded;
460 uint64_t samples_encoded;
462 /* packet quality factor */
465 /* packet picture type */
468 /* frame encode sum of squared error values */
472 typedef struct OutputFile {
473 AVFormatContext *ctx;
475 int ost_index; /* index of the first stream in output_streams */
476 int64_t recording_time; ///< desired length of the resulting file in microseconds == AV_TIME_BASE units
477 int64_t start_time; ///< start time in microseconds == AV_TIME_BASE units
478 uint64_t limit_filesize; /* filesize limit expressed in bytes */
483 extern InputStream **input_streams;
484 extern int nb_input_streams;
485 extern InputFile **input_files;
486 extern int nb_input_files;
488 extern OutputStream **output_streams;
489 extern int nb_output_streams;
490 extern OutputFile **output_files;
491 extern int nb_output_files;
493 extern FilterGraph **filtergraphs;
494 extern int nb_filtergraphs;
496 extern char *vstats_filename;
497 extern char *sdp_filename;
499 extern float audio_drift_threshold;
500 extern float dts_delta_threshold;
501 extern float dts_error_threshold;
503 extern int audio_volume;
504 extern int audio_sync_method;
505 extern int video_sync_method;
506 extern float frame_drop_threshold;
507 extern int do_benchmark;
508 extern int do_benchmark_all;
509 extern int do_deinterlace;
510 extern int do_hex_dump;
511 extern int do_pkt_dump;
513 extern int start_at_zero;
516 extern int exit_on_error;
517 extern int print_stats;
519 extern int stdin_interaction;
520 extern int frame_bits_per_raw_sample;
521 extern AVIOContext *progress_avio;
522 extern float max_error_rate;
523 extern int vdpau_api_ver;
524 extern char *videotoolbox_pixfmt;
526 extern const AVIOInterruptCB int_cb;
528 extern const OptionDef options[];
529 extern const HWAccel hwaccels[];
532 void term_init(void);
533 void term_exit(void);
535 void reset_options(OptionsContext *o, int is_input);
536 void show_usage(void);
538 void opt_output_file(void *optctx, const char *filename);
540 void remove_avoptions(AVDictionary **a, AVDictionary *b);
541 void assert_avoptions(AVDictionary *m);
543 int guess_input_channel_layout(InputStream *ist);
545 enum AVPixelFormat choose_pixel_fmt(AVStream *st, AVCodecContext *avctx, AVCodec *codec, enum AVPixelFormat target);
546 void choose_sample_fmt(AVStream *st, AVCodec *codec);
548 int configure_filtergraph(FilterGraph *fg);
549 int configure_output_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out);
550 int ist_in_filtergraph(FilterGraph *fg, InputStream *ist);
551 FilterGraph *init_simple_filtergraph(InputStream *ist, OutputStream *ost);
552 int init_complex_filtergraph(FilterGraph *fg);
554 int ffmpeg_parse_options(int argc, char **argv);
556 int vdpau_init(AVCodecContext *s);
557 int dxva2_init(AVCodecContext *s);
558 int vda_init(AVCodecContext *s);
559 int videotoolbox_init(AVCodecContext *s);
561 #endif /* FFMPEG_H */