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 */
68 typedef struct HWAccel {
70 int (*init)(AVCodecContext *s);
72 enum AVPixelFormat pix_fmt;
75 /* select an input stream for an output stream */
76 typedef struct StreamMap {
77 int disabled; /* 1 is this mapping is disabled by a negative map */
81 int sync_stream_index;
82 char *linklabel; /* name of an output link, for mapping lavfi outputs */
86 int file_idx, stream_idx, channel_idx; // input
87 int ofile_idx, ostream_idx; // output
90 typedef struct OptionsContext {
93 /* input/output options */
98 SpecifierOpt *codec_names;
100 SpecifierOpt *audio_channels;
101 int nb_audio_channels;
102 SpecifierOpt *audio_sample_rate;
103 int nb_audio_sample_rate;
104 SpecifierOpt *frame_rates;
106 SpecifierOpt *frame_sizes;
108 SpecifierOpt *frame_pix_fmts;
109 int nb_frame_pix_fmts;
112 int64_t input_ts_offset;
115 int thread_queue_size;
117 SpecifierOpt *ts_scale;
119 SpecifierOpt *dump_attachment;
120 int nb_dump_attachment;
121 SpecifierOpt *hwaccels;
123 SpecifierOpt *hwaccel_devices;
124 int nb_hwaccel_devices;
125 SpecifierOpt *autorotate;
129 StreamMap *stream_maps;
131 AudioChannelMap *audio_channel_maps; /* one info entry per -map_channel */
132 int nb_audio_channel_maps; /* number of (valid) -map_channel settings */
133 int metadata_global_manual;
134 int metadata_streams_manual;
135 int metadata_chapters_manual;
136 const char **attachments;
139 int chapters_input_file;
141 int64_t recording_time;
143 uint64_t limit_filesize;
150 int subtitle_disable;
153 /* indexed by output file stream index */
157 SpecifierOpt *metadata;
159 SpecifierOpt *max_frames;
161 SpecifierOpt *bitstream_filters;
162 int nb_bitstream_filters;
163 SpecifierOpt *codec_tags;
165 SpecifierOpt *sample_fmts;
167 SpecifierOpt *qscale;
169 SpecifierOpt *forced_key_frames;
170 int nb_forced_key_frames;
171 SpecifierOpt *force_fps;
173 SpecifierOpt *frame_aspect_ratios;
174 int nb_frame_aspect_ratios;
175 SpecifierOpt *rc_overrides;
177 SpecifierOpt *intra_matrices;
178 int nb_intra_matrices;
179 SpecifierOpt *inter_matrices;
180 int nb_inter_matrices;
181 SpecifierOpt *chroma_intra_matrices;
182 int nb_chroma_intra_matrices;
183 SpecifierOpt *top_field_first;
184 int nb_top_field_first;
185 SpecifierOpt *metadata_map;
187 SpecifierOpt *presets;
189 SpecifierOpt *copy_initial_nonkeyframes;
190 int nb_copy_initial_nonkeyframes;
191 SpecifierOpt *copy_prior_start;
192 int nb_copy_prior_start;
193 SpecifierOpt *filters;
195 SpecifierOpt *filter_scripts;
196 int nb_filter_scripts;
197 SpecifierOpt *reinit_filters;
198 int nb_reinit_filters;
199 SpecifierOpt *fix_sub_duration;
200 int nb_fix_sub_duration;
201 SpecifierOpt *canvas_sizes;
205 SpecifierOpt *passlogfiles;
207 SpecifierOpt *guess_layout_max;
208 int nb_guess_layout_max;
211 SpecifierOpt *discard;
213 SpecifierOpt *disposition;
217 typedef struct InputFilter {
218 AVFilterContext *filter;
219 struct InputStream *ist;
220 struct FilterGraph *graph;
224 typedef struct OutputFilter {
225 AVFilterContext *filter;
226 struct OutputStream *ost;
227 struct FilterGraph *graph;
230 /* temporary storage until stream maps are processed */
231 AVFilterInOut *out_tmp;
234 typedef struct FilterGraph {
236 const char *graph_desc;
238 AVFilterGraph *graph;
241 InputFilter **inputs;
243 OutputFilter **outputs;
247 typedef struct InputStream {
250 int discard; /* true if stream data should be discarded */
251 int user_set_discard;
252 int decoding_needed; /* non zero if the packets must be decoded in 'raw_fifo', see DECODING_FOR_* */
253 #define DECODING_FOR_OST 1
254 #define DECODING_FOR_FILTER 2
256 AVCodecContext *dec_ctx;
258 AVFrame *decoded_frame;
259 AVFrame *filter_frame; /* a ref of decoded_frame, to be sent to filters */
261 int64_t start; /* time when read started */
262 /* predicted dts of the next packet read for this stream or (when there are
263 * several frames in a packet) of the next frame in current packet (in AV_TIME_BASE units) */
265 int64_t dts; ///< dts of the last packet read for this stream (in AV_TIME_BASE units)
267 int64_t next_pts; ///< synthetic pts for the next decode frame (in AV_TIME_BASE units)
268 int64_t pts; ///< current pts of the decoded frame (in AV_TIME_BASE units)
269 int wrap_correction_done;
271 int64_t filter_in_rescale_delta_last;
275 int showed_multi_packet_warning;
276 AVDictionary *decoder_opts;
277 AVRational framerate; /* framerate forced with -r */
279 int guess_layout_max;
284 int resample_pix_fmt;
286 int resample_sample_fmt;
287 int resample_sample_rate;
288 int resample_channels;
289 uint64_t resample_channel_layout;
291 int fix_sub_duration;
292 struct { /* previous decoded subtitle and related variables */
307 /* decoded data from this stream goes into all those filters
308 * currently video and audio only */
309 InputFilter **filters;
314 /* hwaccel options */
315 enum HWAccelID hwaccel_id;
316 char *hwaccel_device;
318 /* hwaccel context */
319 enum HWAccelID active_hwaccel_id;
321 void (*hwaccel_uninit)(AVCodecContext *s);
322 int (*hwaccel_get_buffer)(AVCodecContext *s, AVFrame *frame, int flags);
323 int (*hwaccel_retrieve_data)(AVCodecContext *s, AVFrame *frame);
324 enum AVPixelFormat hwaccel_pix_fmt;
325 enum AVPixelFormat hwaccel_retrieved_pix_fmt;
328 // combined size of all the packets read
330 /* number of packets successfully read for this stream */
332 // number of frames/samples retrieved from the decoder
333 uint64_t frames_decoded;
334 uint64_t samples_decoded;
337 typedef struct InputFile {
338 AVFormatContext *ctx;
339 int eof_reached; /* true if eof reached */
340 int eagain; /* true if last read attempt returned EAGAIN */
341 int ist_index; /* index of first stream in input_streams */
342 int64_t input_ts_offset;
345 int64_t start_time; /* user-specified start time in AV_TIME_BASE or AV_NOPTS_VALUE */
347 int64_t recording_time;
348 int nb_streams; /* number of stream that ffmpeg is aware of; may be different
349 from ctx.nb_streams if new streams appear during av_read_frame() */
350 int nb_streams_warn; /* number of streams that the user was warned of */
355 AVThreadMessageQueue *in_thread_queue;
356 pthread_t thread; /* thread reading from this file */
357 int non_blocking; /* reading packets from the thread should not block */
358 int joined; /* the thread has been joined */
359 int thread_queue_size; /* maximum number of queued packets */
363 enum forced_keyframes_const {
372 extern const char *const forced_keyframes_const_names[];
375 ENCODER_FINISHED = 1,
379 typedef struct OutputStream {
380 int file_index; /* file index */
381 int index; /* stream index in the output file */
382 int source_index; /* InputStream index */
383 AVStream *st; /* stream in the output file */
384 int encoding_needed; /* true if encoding needed for this stream */
386 /* input pts and corresponding output pts
388 struct InputStream *sync_ist; /* input stream to sync against */
389 int64_t sync_opts; /* output frame counter, could be changed to some true timestamp */ // FIXME look at frame_number
390 /* pts of the first frame encoded for this stream, used for limiting
393 /* dts of the last packet sent to the muxer */
394 int64_t last_mux_dts;
395 AVBitStreamFilterContext *bitstream_filters;
396 AVCodecContext *enc_ctx;
399 AVFrame *filtered_frame;
402 int last_nb0_frames[3];
405 AVRational frame_rate;
408 int rotate_overridden;
410 AVRational frame_aspect_ratio;
412 /* forced key frames */
413 int64_t *forced_kf_pts;
416 char *forced_keyframes;
417 AVExpr *forced_keyframes_pexpr;
418 double forced_keyframes_expr_const_values[FKF_NB];
421 int *audio_channels_map; /* list of the channels id to pick from the source stream */
422 int audio_channels_mapped; /* number of channels in audio_channels_map */
424 char *logfile_prefix;
427 OutputFilter *filter;
429 char *filters; ///< filtergraph associated to the -filter option
430 char *filters_script; ///< filtergraph script associated to the -filter_script option
433 AVDictionary *encoder_opts;
434 AVDictionary *swr_opts;
435 AVDictionary *resample_opts;
436 AVDictionary *bsf_args;
438 OSTFinished finished; /* no more packets should be written for this stream */
439 int unavailable; /* true if the steram is unavailable (possibly temporarily) */
441 const char *attachment_filename;
442 int copy_initial_nonkeyframes;
443 int copy_prior_start;
448 AVCodecParserContext *parser;
451 // combined size of all the packets written
453 // number of packets send to the muxer
454 uint64_t packets_written;
455 // number of frames/samples sent to the encoder
456 uint64_t frames_encoded;
457 uint64_t samples_encoded;
460 typedef struct OutputFile {
461 AVFormatContext *ctx;
463 int ost_index; /* index of the first stream in output_streams */
464 int64_t recording_time; ///< desired length of the resulting file in microseconds == AV_TIME_BASE units
465 int64_t start_time; ///< start time in microseconds == AV_TIME_BASE units
466 uint64_t limit_filesize; /* filesize limit expressed in bytes */
471 extern InputStream **input_streams;
472 extern int nb_input_streams;
473 extern InputFile **input_files;
474 extern int nb_input_files;
476 extern OutputStream **output_streams;
477 extern int nb_output_streams;
478 extern OutputFile **output_files;
479 extern int nb_output_files;
481 extern FilterGraph **filtergraphs;
482 extern int nb_filtergraphs;
484 extern char *vstats_filename;
485 extern char *sdp_filename;
487 extern float audio_drift_threshold;
488 extern float dts_delta_threshold;
489 extern float dts_error_threshold;
491 extern int audio_volume;
492 extern int audio_sync_method;
493 extern int video_sync_method;
494 extern float frame_drop_threshold;
495 extern int do_benchmark;
496 extern int do_benchmark_all;
497 extern int do_deinterlace;
498 extern int do_hex_dump;
499 extern int do_pkt_dump;
501 extern int start_at_zero;
504 extern int exit_on_error;
505 extern int print_stats;
507 extern int stdin_interaction;
508 extern int frame_bits_per_raw_sample;
509 extern AVIOContext *progress_avio;
510 extern float max_error_rate;
511 extern int vdpau_api_ver;
513 extern const AVIOInterruptCB int_cb;
515 extern const OptionDef options[];
516 extern const HWAccel hwaccels[];
519 void term_init(void);
520 void term_exit(void);
522 void reset_options(OptionsContext *o, int is_input);
523 void show_usage(void);
525 void opt_output_file(void *optctx, const char *filename);
527 void remove_avoptions(AVDictionary **a, AVDictionary *b);
528 void assert_avoptions(AVDictionary *m);
530 int guess_input_channel_layout(InputStream *ist);
532 enum AVPixelFormat choose_pixel_fmt(AVStream *st, AVCodecContext *avctx, AVCodec *codec, enum AVPixelFormat target);
533 void choose_sample_fmt(AVStream *st, AVCodec *codec);
535 int configure_filtergraph(FilterGraph *fg);
536 int configure_output_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out);
537 int ist_in_filtergraph(FilterGraph *fg, InputStream *ist);
538 FilterGraph *init_simple_filtergraph(InputStream *ist, OutputStream *ost);
540 int ffmpeg_parse_options(int argc, char **argv);
542 int vdpau_init(AVCodecContext *s);
543 int dxva2_init(AVCodecContext *s);
544 int vda_init(AVCodecContext *s);
546 #endif /* FFMPEG_H */