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 */
97 SpecifierOpt *codec_names;
99 SpecifierOpt *audio_channels;
100 int nb_audio_channels;
101 SpecifierOpt *audio_sample_rate;
102 int nb_audio_sample_rate;
103 SpecifierOpt *frame_rates;
105 SpecifierOpt *frame_sizes;
107 SpecifierOpt *frame_pix_fmts;
108 int nb_frame_pix_fmts;
111 int64_t input_ts_offset;
115 SpecifierOpt *ts_scale;
117 SpecifierOpt *dump_attachment;
118 int nb_dump_attachment;
119 SpecifierOpt *hwaccels;
121 SpecifierOpt *hwaccel_devices;
122 int nb_hwaccel_devices;
125 StreamMap *stream_maps;
127 AudioChannelMap *audio_channel_maps; /* one info entry per -map_channel */
128 int nb_audio_channel_maps; /* number of (valid) -map_channel settings */
129 int metadata_global_manual;
130 int metadata_streams_manual;
131 int metadata_chapters_manual;
132 const char **attachments;
135 int chapters_input_file;
137 int64_t recording_time;
139 uint64_t limit_filesize;
146 int subtitle_disable;
149 /* indexed by output file stream index */
153 SpecifierOpt *metadata;
155 SpecifierOpt *max_frames;
157 SpecifierOpt *bitstream_filters;
158 int nb_bitstream_filters;
159 SpecifierOpt *codec_tags;
161 SpecifierOpt *sample_fmts;
163 SpecifierOpt *qscale;
165 SpecifierOpt *forced_key_frames;
166 int nb_forced_key_frames;
167 SpecifierOpt *force_fps;
169 SpecifierOpt *frame_aspect_ratios;
170 int nb_frame_aspect_ratios;
171 SpecifierOpt *rc_overrides;
173 SpecifierOpt *intra_matrices;
174 int nb_intra_matrices;
175 SpecifierOpt *inter_matrices;
176 int nb_inter_matrices;
177 SpecifierOpt *chroma_intra_matrices;
178 int nb_chroma_intra_matrices;
179 SpecifierOpt *top_field_first;
180 int nb_top_field_first;
181 SpecifierOpt *metadata_map;
183 SpecifierOpt *presets;
185 SpecifierOpt *copy_initial_nonkeyframes;
186 int nb_copy_initial_nonkeyframes;
187 SpecifierOpt *copy_prior_start;
188 int nb_copy_prior_start;
189 SpecifierOpt *filters;
191 SpecifierOpt *filter_scripts;
192 int nb_filter_scripts;
193 SpecifierOpt *reinit_filters;
194 int nb_reinit_filters;
195 SpecifierOpt *fix_sub_duration;
196 int nb_fix_sub_duration;
197 SpecifierOpt *canvas_sizes;
201 SpecifierOpt *passlogfiles;
203 SpecifierOpt *guess_layout_max;
204 int nb_guess_layout_max;
207 SpecifierOpt *discard;
209 SpecifierOpt *disposition;
213 typedef struct InputFilter {
214 AVFilterContext *filter;
215 struct InputStream *ist;
216 struct FilterGraph *graph;
220 typedef struct OutputFilter {
221 AVFilterContext *filter;
222 struct OutputStream *ost;
223 struct FilterGraph *graph;
226 /* temporary storage until stream maps are processed */
227 AVFilterInOut *out_tmp;
230 typedef struct FilterGraph {
232 const char *graph_desc;
234 AVFilterGraph *graph;
237 InputFilter **inputs;
239 OutputFilter **outputs;
243 typedef struct InputStream {
246 int discard; /* true if stream data should be discarded */
247 int user_set_discard;
248 int decoding_needed; /* non zero if the packets must be decoded in 'raw_fifo', see DECODING_FOR_* */
249 #define DECODING_FOR_OST 1
250 #define DECODING_FOR_FILTER 2
252 AVCodecContext *dec_ctx;
254 AVFrame *decoded_frame;
255 AVFrame *filter_frame; /* a ref of decoded_frame, to be sent to filters */
257 int64_t start; /* time when read started */
258 /* predicted dts of the next packet read for this stream or (when there are
259 * several frames in a packet) of the next frame in current packet (in AV_TIME_BASE units) */
261 int64_t dts; ///< dts of the last packet read for this stream (in AV_TIME_BASE units)
263 int64_t next_pts; ///< synthetic pts for the next decode frame (in AV_TIME_BASE units)
264 int64_t pts; ///< current pts of the decoded frame (in AV_TIME_BASE units)
265 int wrap_correction_done;
267 int64_t filter_in_rescale_delta_last;
271 int showed_multi_packet_warning;
272 AVDictionary *decoder_opts;
273 AVRational framerate; /* framerate forced with -r */
275 int guess_layout_max;
279 int resample_pix_fmt;
281 int resample_sample_fmt;
282 int resample_sample_rate;
283 int resample_channels;
284 uint64_t resample_channel_layout;
286 int fix_sub_duration;
287 struct { /* previous decoded subtitle and related variables */
302 /* decoded data from this stream goes into all those filters
303 * currently video and audio only */
304 InputFilter **filters;
309 /* hwaccel options */
310 enum HWAccelID hwaccel_id;
311 char *hwaccel_device;
313 /* hwaccel context */
314 enum HWAccelID active_hwaccel_id;
316 void (*hwaccel_uninit)(AVCodecContext *s);
317 int (*hwaccel_get_buffer)(AVCodecContext *s, AVFrame *frame, int flags);
318 int (*hwaccel_retrieve_data)(AVCodecContext *s, AVFrame *frame);
319 enum AVPixelFormat hwaccel_pix_fmt;
320 enum AVPixelFormat hwaccel_retrieved_pix_fmt;
323 // combined size of all the packets read
325 /* number of packets successfully read for this stream */
327 // number of frames/samples retrieved from the decoder
328 uint64_t frames_decoded;
329 uint64_t samples_decoded;
332 typedef struct InputFile {
333 AVFormatContext *ctx;
334 int eof_reached; /* true if eof reached */
335 int eagain; /* true if last read attempt returned EAGAIN */
336 int ist_index; /* index of first stream in input_streams */
337 int64_t input_ts_offset;
340 int64_t start_time; /* user-specified start time in AV_TIME_BASE or AV_NOPTS_VALUE */
341 int64_t recording_time;
342 int nb_streams; /* number of stream that ffmpeg is aware of; may be different
343 from ctx.nb_streams if new streams appear during av_read_frame() */
344 int nb_streams_warn; /* number of streams that the user was warned of */
349 AVThreadMessageQueue *in_thread_queue;
350 pthread_t thread; /* thread reading from this file */
351 int non_blocking; /* reading packets from the thread should not block */
352 int joined; /* the thread has been joined */
356 enum forced_keyframes_const {
365 extern const char *const forced_keyframes_const_names[];
368 ENCODER_FINISHED = 1,
372 typedef struct OutputStream {
373 int file_index; /* file index */
374 int index; /* stream index in the output file */
375 int source_index; /* InputStream index */
376 AVStream *st; /* stream in the output file */
377 int encoding_needed; /* true if encoding needed for this stream */
379 /* input pts and corresponding output pts
381 struct InputStream *sync_ist; /* input stream to sync against */
382 int64_t sync_opts; /* output frame counter, could be changed to some true timestamp */ // FIXME look at frame_number
383 /* pts of the first frame encoded for this stream, used for limiting
386 /* dts of the last packet sent to the muxer */
387 int64_t last_mux_dts;
388 AVBitStreamFilterContext *bitstream_filters;
389 AVCodecContext *enc_ctx;
392 AVFrame *filtered_frame;
397 AVRational frame_rate;
401 AVRational frame_aspect_ratio;
403 /* forced key frames */
404 int64_t *forced_kf_pts;
407 char *forced_keyframes;
408 AVExpr *forced_keyframes_pexpr;
409 double forced_keyframes_expr_const_values[FKF_NB];
412 int *audio_channels_map; /* list of the channels id to pick from the source stream */
413 int audio_channels_mapped; /* number of channels in audio_channels_map */
415 char *logfile_prefix;
418 OutputFilter *filter;
420 char *filters; ///< filtergraph associated to the -filter option
421 char *filters_script; ///< filtergraph script associated to the -filter_script option
424 AVDictionary *encoder_opts;
425 AVDictionary *swr_opts;
426 AVDictionary *resample_opts;
427 AVDictionary *bsf_args;
429 OSTFinished finished; /* no more packets should be written for this stream */
430 int unavailable; /* true if the steram is unavailable (possibly temporarily) */
432 const char *attachment_filename;
433 int copy_initial_nonkeyframes;
434 int copy_prior_start;
439 AVCodecParserContext *parser;
442 // combined size of all the packets written
444 // number of packets send to the muxer
445 uint64_t packets_written;
446 // number of frames/samples sent to the encoder
447 uint64_t frames_encoded;
448 uint64_t samples_encoded;
451 typedef struct OutputFile {
452 AVFormatContext *ctx;
454 int ost_index; /* index of the first stream in output_streams */
455 int64_t recording_time; ///< desired length of the resulting file in microseconds == AV_TIME_BASE units
456 int64_t start_time; ///< start time in microseconds == AV_TIME_BASE units
457 uint64_t limit_filesize; /* filesize limit expressed in bytes */
462 extern InputStream **input_streams;
463 extern int nb_input_streams;
464 extern InputFile **input_files;
465 extern int nb_input_files;
467 extern OutputStream **output_streams;
468 extern int nb_output_streams;
469 extern OutputFile **output_files;
470 extern int nb_output_files;
472 extern FilterGraph **filtergraphs;
473 extern int nb_filtergraphs;
475 extern char *vstats_filename;
476 extern char *sdp_filename;
478 extern float audio_drift_threshold;
479 extern float dts_delta_threshold;
480 extern float dts_error_threshold;
482 extern int audio_volume;
483 extern int audio_sync_method;
484 extern int video_sync_method;
485 extern float frame_drop_threshold;
486 extern int do_benchmark;
487 extern int do_benchmark_all;
488 extern int do_deinterlace;
489 extern int do_hex_dump;
490 extern int do_pkt_dump;
492 extern int start_at_zero;
495 extern int exit_on_error;
496 extern int print_stats;
498 extern int stdin_interaction;
499 extern int frame_bits_per_raw_sample;
500 extern AVIOContext *progress_avio;
501 extern float max_error_rate;
502 extern int vdpau_api_ver;
504 extern const AVIOInterruptCB int_cb;
506 extern const OptionDef options[];
507 extern const HWAccel hwaccels[];
510 void term_init(void);
511 void term_exit(void);
513 void reset_options(OptionsContext *o, int is_input);
514 void show_usage(void);
516 void opt_output_file(void *optctx, const char *filename);
518 void remove_avoptions(AVDictionary **a, AVDictionary *b);
519 void assert_avoptions(AVDictionary *m);
521 int guess_input_channel_layout(InputStream *ist);
523 enum AVPixelFormat choose_pixel_fmt(AVStream *st, AVCodecContext *avctx, AVCodec *codec, enum AVPixelFormat target);
524 void choose_sample_fmt(AVStream *st, AVCodec *codec);
526 int configure_filtergraph(FilterGraph *fg);
527 int configure_output_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out);
528 int ist_in_filtergraph(FilterGraph *fg, InputStream *ist);
529 FilterGraph *init_simple_filtergraph(InputStream *ist, OutputStream *ost);
531 int ffmpeg_parse_options(int argc, char **argv);
533 int vdpau_init(AVCodecContext *s);
534 int dxva2_init(AVCodecContext *s);
535 int vda_init(AVCodecContext *s);
537 #endif /* FFMPEG_H */