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;
118 int thread_queue_size;
120 SpecifierOpt *ts_scale;
122 SpecifierOpt *dump_attachment;
123 int nb_dump_attachment;
124 SpecifierOpt *hwaccels;
126 SpecifierOpt *hwaccel_devices;
127 int nb_hwaccel_devices;
128 SpecifierOpt *autorotate;
132 StreamMap *stream_maps;
134 AudioChannelMap *audio_channel_maps; /* one info entry per -map_channel */
135 int nb_audio_channel_maps; /* number of (valid) -map_channel settings */
136 int metadata_global_manual;
137 int metadata_streams_manual;
138 int metadata_chapters_manual;
139 const char **attachments;
142 int chapters_input_file;
144 int64_t recording_time;
146 uint64_t limit_filesize;
153 int subtitle_disable;
156 /* indexed by output file stream index */
160 SpecifierOpt *metadata;
162 SpecifierOpt *max_frames;
164 SpecifierOpt *bitstream_filters;
165 int nb_bitstream_filters;
166 SpecifierOpt *codec_tags;
168 SpecifierOpt *sample_fmts;
170 SpecifierOpt *qscale;
172 SpecifierOpt *forced_key_frames;
173 int nb_forced_key_frames;
174 SpecifierOpt *force_fps;
176 SpecifierOpt *frame_aspect_ratios;
177 int nb_frame_aspect_ratios;
178 SpecifierOpt *rc_overrides;
180 SpecifierOpt *intra_matrices;
181 int nb_intra_matrices;
182 SpecifierOpt *inter_matrices;
183 int nb_inter_matrices;
184 SpecifierOpt *chroma_intra_matrices;
185 int nb_chroma_intra_matrices;
186 SpecifierOpt *top_field_first;
187 int nb_top_field_first;
188 SpecifierOpt *metadata_map;
190 SpecifierOpt *presets;
192 SpecifierOpt *copy_initial_nonkeyframes;
193 int nb_copy_initial_nonkeyframes;
194 SpecifierOpt *copy_prior_start;
195 int nb_copy_prior_start;
196 SpecifierOpt *filters;
198 SpecifierOpt *filter_scripts;
199 int nb_filter_scripts;
200 SpecifierOpt *reinit_filters;
201 int nb_reinit_filters;
202 SpecifierOpt *fix_sub_duration;
203 int nb_fix_sub_duration;
204 SpecifierOpt *canvas_sizes;
208 SpecifierOpt *passlogfiles;
210 SpecifierOpt *guess_layout_max;
211 int nb_guess_layout_max;
214 SpecifierOpt *discard;
216 SpecifierOpt *disposition;
220 typedef struct InputFilter {
221 AVFilterContext *filter;
222 struct InputStream *ist;
223 struct FilterGraph *graph;
227 typedef struct OutputFilter {
228 AVFilterContext *filter;
229 struct OutputStream *ost;
230 struct FilterGraph *graph;
233 /* temporary storage until stream maps are processed */
234 AVFilterInOut *out_tmp;
235 enum AVMediaType type;
238 typedef struct FilterGraph {
240 const char *graph_desc;
242 AVFilterGraph *graph;
245 InputFilter **inputs;
247 OutputFilter **outputs;
251 typedef struct InputStream {
254 int discard; /* true if stream data should be discarded */
255 int user_set_discard;
256 int decoding_needed; /* non zero if the packets must be decoded in 'raw_fifo', see DECODING_FOR_* */
257 #define DECODING_FOR_OST 1
258 #define DECODING_FOR_FILTER 2
260 AVCodecContext *dec_ctx;
262 AVFrame *decoded_frame;
263 AVFrame *filter_frame; /* a ref of decoded_frame, to be sent to filters */
265 int64_t start; /* time when read started */
266 /* predicted dts of the next packet read for this stream or (when there are
267 * several frames in a packet) of the next frame in current packet (in AV_TIME_BASE units) */
269 int64_t dts; ///< dts of the last packet read for this stream (in AV_TIME_BASE units)
271 int64_t next_pts; ///< synthetic pts for the next decode frame (in AV_TIME_BASE units)
272 int64_t pts; ///< current pts of the decoded frame (in AV_TIME_BASE units)
273 int wrap_correction_done;
275 int64_t filter_in_rescale_delta_last;
277 int64_t min_pts; /* pts with the smallest value in a current stream */
278 int64_t max_pts; /* pts with the higher value in a current stream */
279 int64_t nb_samples; /* number of samples in the last decoded audio frame before looping */
283 int showed_multi_packet_warning;
284 AVDictionary *decoder_opts;
285 AVRational framerate; /* framerate forced with -r */
287 int guess_layout_max;
292 int resample_pix_fmt;
294 int resample_sample_fmt;
295 int resample_sample_rate;
296 int resample_channels;
297 uint64_t resample_channel_layout;
299 int fix_sub_duration;
300 struct { /* previous decoded subtitle and related variables */
315 /* decoded data from this stream goes into all those filters
316 * currently video and audio only */
317 InputFilter **filters;
322 /* hwaccel options */
323 enum HWAccelID hwaccel_id;
324 char *hwaccel_device;
326 /* hwaccel context */
327 enum HWAccelID active_hwaccel_id;
329 void (*hwaccel_uninit)(AVCodecContext *s);
330 int (*hwaccel_get_buffer)(AVCodecContext *s, AVFrame *frame, int flags);
331 int (*hwaccel_retrieve_data)(AVCodecContext *s, AVFrame *frame);
332 enum AVPixelFormat hwaccel_pix_fmt;
333 enum AVPixelFormat hwaccel_retrieved_pix_fmt;
336 // combined size of all the packets read
338 /* number of packets successfully read for this stream */
340 // number of frames/samples retrieved from the decoder
341 uint64_t frames_decoded;
342 uint64_t samples_decoded;
345 typedef struct InputFile {
346 AVFormatContext *ctx;
347 int eof_reached; /* true if eof reached */
348 int eagain; /* true if last read attempt returned EAGAIN */
349 int ist_index; /* index of first stream in input_streams */
350 int loop; /* set number of times input stream should be looped */
351 int64_t duration; /* actual duration of the longest stream in a file
352 at the moment when looping happens */
353 AVRational time_base; /* time base of the duration */
354 int64_t input_ts_offset;
358 int64_t start_time; /* user-specified start time in AV_TIME_BASE or AV_NOPTS_VALUE */
360 int64_t recording_time;
361 int nb_streams; /* number of stream that ffmpeg is aware of; may be different
362 from ctx.nb_streams if new streams appear during av_read_frame() */
363 int nb_streams_warn; /* number of streams that the user was warned of */
368 AVThreadMessageQueue *in_thread_queue;
369 pthread_t thread; /* thread reading from this file */
370 int non_blocking; /* reading packets from the thread should not block */
371 int joined; /* the thread has been joined */
372 int thread_queue_size; /* maximum number of queued packets */
376 enum forced_keyframes_const {
385 extern const char *const forced_keyframes_const_names[];
388 ENCODER_FINISHED = 1,
392 typedef struct OutputStream {
393 int file_index; /* file index */
394 int index; /* stream index in the output file */
395 int source_index; /* InputStream index */
396 AVStream *st; /* stream in the output file */
397 int encoding_needed; /* true if encoding needed for this stream */
399 /* input pts and corresponding output pts
401 struct InputStream *sync_ist; /* input stream to sync against */
402 int64_t sync_opts; /* output frame counter, could be changed to some true timestamp */ // FIXME look at frame_number
403 /* pts of the first frame encoded for this stream, used for limiting
406 /* dts of the last packet sent to the muxer */
407 int64_t last_mux_dts;
408 AVBitStreamFilterContext *bitstream_filters;
409 AVCodecContext *enc_ctx;
412 AVFrame *filtered_frame;
415 int last_nb0_frames[3];
418 AVRational frame_rate;
421 int rotate_overridden;
423 AVRational frame_aspect_ratio;
425 /* forced key frames */
426 int64_t *forced_kf_pts;
429 char *forced_keyframes;
430 AVExpr *forced_keyframes_pexpr;
431 double forced_keyframes_expr_const_values[FKF_NB];
434 int *audio_channels_map; /* list of the channels id to pick from the source stream */
435 int audio_channels_mapped; /* number of channels in audio_channels_map */
437 char *logfile_prefix;
440 OutputFilter *filter;
442 char *filters; ///< filtergraph associated to the -filter option
443 char *filters_script; ///< filtergraph script associated to the -filter_script option
445 AVDictionary *encoder_opts;
446 AVDictionary *sws_dict;
447 AVDictionary *swr_opts;
448 AVDictionary *resample_opts;
449 AVDictionary *bsf_args;
451 OSTFinished finished; /* no more packets should be written for this stream */
452 int unavailable; /* true if the steram is unavailable (possibly temporarily) */
454 const char *attachment_filename;
455 int copy_initial_nonkeyframes;
456 int copy_prior_start;
461 AVCodecParserContext *parser;
464 // combined size of all the packets written
466 // number of packets send to the muxer
467 uint64_t packets_written;
468 // number of frames/samples sent to the encoder
469 uint64_t frames_encoded;
470 uint64_t samples_encoded;
472 /* packet quality factor */
475 /* packet picture type */
478 /* frame encode sum of squared error values */
482 typedef struct OutputFile {
483 AVFormatContext *ctx;
485 int ost_index; /* index of the first stream in output_streams */
486 int64_t recording_time; ///< desired length of the resulting file in microseconds == AV_TIME_BASE units
487 int64_t start_time; ///< start time in microseconds == AV_TIME_BASE units
488 uint64_t limit_filesize; /* filesize limit expressed in bytes */
493 extern InputStream **input_streams;
494 extern int nb_input_streams;
495 extern InputFile **input_files;
496 extern int nb_input_files;
498 extern OutputStream **output_streams;
499 extern int nb_output_streams;
500 extern OutputFile **output_files;
501 extern int nb_output_files;
503 extern FilterGraph **filtergraphs;
504 extern int nb_filtergraphs;
506 extern char *vstats_filename;
507 extern char *sdp_filename;
509 extern float audio_drift_threshold;
510 extern float dts_delta_threshold;
511 extern float dts_error_threshold;
513 extern int audio_volume;
514 extern int audio_sync_method;
515 extern int video_sync_method;
516 extern float frame_drop_threshold;
517 extern int do_benchmark;
518 extern int do_benchmark_all;
519 extern int do_deinterlace;
520 extern int do_hex_dump;
521 extern int do_pkt_dump;
523 extern int start_at_zero;
526 extern int exit_on_error;
527 extern int print_stats;
529 extern int stdin_interaction;
530 extern int frame_bits_per_raw_sample;
531 extern AVIOContext *progress_avio;
532 extern float max_error_rate;
533 extern int vdpau_api_ver;
534 extern char *videotoolbox_pixfmt;
536 extern const AVIOInterruptCB int_cb;
538 extern const OptionDef options[];
539 extern const HWAccel hwaccels[];
542 void term_init(void);
543 void term_exit(void);
545 void reset_options(OptionsContext *o, int is_input);
546 void show_usage(void);
548 void opt_output_file(void *optctx, const char *filename);
550 void remove_avoptions(AVDictionary **a, AVDictionary *b);
551 void assert_avoptions(AVDictionary *m);
553 int guess_input_channel_layout(InputStream *ist);
555 enum AVPixelFormat choose_pixel_fmt(AVStream *st, AVCodecContext *avctx, AVCodec *codec, enum AVPixelFormat target);
556 void choose_sample_fmt(AVStream *st, AVCodec *codec);
558 int configure_filtergraph(FilterGraph *fg);
559 int configure_output_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out);
560 int ist_in_filtergraph(FilterGraph *fg, InputStream *ist);
561 FilterGraph *init_simple_filtergraph(InputStream *ist, OutputStream *ost);
562 int init_complex_filtergraph(FilterGraph *fg);
564 int ffmpeg_parse_options(int argc, char **argv);
566 int vdpau_init(AVCodecContext *s);
567 int dxva2_init(AVCodecContext *s);
568 int vda_init(AVCodecContext *s);
569 int videotoolbox_init(AVCodecContext *s);
571 #endif /* FFMPEG_H */