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"
48 #include "libswresample/swresample.h"
51 #define VSYNC_PASSTHROUGH 0
54 #define VSYNC_VSCFR 0xfe
55 #define VSYNC_DROP 0xff
57 #define MAX_STREAMS 1024 /* arbitrary sanity check value */
65 typedef struct HWAccel {
67 int (*init)(AVCodecContext *s);
69 enum AVPixelFormat pix_fmt;
72 /* select an input stream for an output stream */
73 typedef struct StreamMap {
74 int disabled; /* 1 is this mapping is disabled by a negative map */
78 int sync_stream_index;
79 char *linklabel; /* name of an output link, for mapping lavfi outputs */
83 int file_idx, stream_idx, channel_idx; // input
84 int ofile_idx, ostream_idx; // output
87 typedef struct OptionsContext {
90 /* input/output options */
94 SpecifierOpt *codec_names;
96 SpecifierOpt *audio_channels;
97 int nb_audio_channels;
98 SpecifierOpt *audio_sample_rate;
99 int nb_audio_sample_rate;
100 SpecifierOpt *frame_rates;
102 SpecifierOpt *frame_sizes;
104 SpecifierOpt *frame_pix_fmts;
105 int nb_frame_pix_fmts;
108 int64_t input_ts_offset;
112 SpecifierOpt *ts_scale;
114 SpecifierOpt *dump_attachment;
115 int nb_dump_attachment;
116 SpecifierOpt *hwaccels;
118 SpecifierOpt *hwaccel_devices;
119 int nb_hwaccel_devices;
122 StreamMap *stream_maps;
124 AudioChannelMap *audio_channel_maps; /* one info entry per -map_channel */
125 int nb_audio_channel_maps; /* number of (valid) -map_channel settings */
126 int metadata_global_manual;
127 int metadata_streams_manual;
128 int metadata_chapters_manual;
129 const char **attachments;
132 int chapters_input_file;
134 int64_t recording_time;
136 uint64_t limit_filesize;
143 int subtitle_disable;
146 /* indexed by output file stream index */
150 SpecifierOpt *metadata;
152 SpecifierOpt *max_frames;
154 SpecifierOpt *bitstream_filters;
155 int nb_bitstream_filters;
156 SpecifierOpt *codec_tags;
158 SpecifierOpt *sample_fmts;
160 SpecifierOpt *qscale;
162 SpecifierOpt *forced_key_frames;
163 int nb_forced_key_frames;
164 SpecifierOpt *force_fps;
166 SpecifierOpt *frame_aspect_ratios;
167 int nb_frame_aspect_ratios;
168 SpecifierOpt *rc_overrides;
170 SpecifierOpt *intra_matrices;
171 int nb_intra_matrices;
172 SpecifierOpt *inter_matrices;
173 int nb_inter_matrices;
174 SpecifierOpt *top_field_first;
175 int nb_top_field_first;
176 SpecifierOpt *metadata_map;
178 SpecifierOpt *presets;
180 SpecifierOpt *copy_initial_nonkeyframes;
181 int nb_copy_initial_nonkeyframes;
182 SpecifierOpt *copy_prior_start;
183 int nb_copy_prior_start;
184 SpecifierOpt *filters;
186 SpecifierOpt *filter_scripts;
187 int nb_filter_scripts;
188 SpecifierOpt *reinit_filters;
189 int nb_reinit_filters;
190 SpecifierOpt *fix_sub_duration;
191 int nb_fix_sub_duration;
192 SpecifierOpt *canvas_sizes;
196 SpecifierOpt *passlogfiles;
198 SpecifierOpt *guess_layout_max;
199 int nb_guess_layout_max;
204 typedef struct InputFilter {
205 AVFilterContext *filter;
206 struct InputStream *ist;
207 struct FilterGraph *graph;
211 typedef struct OutputFilter {
212 AVFilterContext *filter;
213 struct OutputStream *ost;
214 struct FilterGraph *graph;
217 /* temporary storage until stream maps are processed */
218 AVFilterInOut *out_tmp;
221 typedef struct FilterGraph {
223 const char *graph_desc;
225 AVFilterGraph *graph;
228 InputFilter **inputs;
230 OutputFilter **outputs;
234 typedef struct InputStream {
237 int discard; /* true if stream data should be discarded */
238 int decoding_needed; /* true if the packets must be decoded in 'raw_fifo' */
240 AVFrame *decoded_frame;
241 AVFrame *filter_frame; /* a ref of decoded_frame, to be sent to filters */
243 int64_t start; /* time when read started */
244 /* predicted dts of the next packet read for this stream or (when there are
245 * several frames in a packet) of the next frame in current packet (in AV_TIME_BASE units) */
247 int64_t dts; ///< dts of the last packet read for this stream (in AV_TIME_BASE units)
249 int64_t next_pts; ///< synthetic pts for the next decode frame (in AV_TIME_BASE units)
250 int64_t pts; ///< current pts of the decoded frame (in AV_TIME_BASE units)
251 int wrap_correction_done;
253 int64_t filter_in_rescale_delta_last;
256 int is_start; /* is 1 at the start and after a discontinuity */
258 int showed_multi_packet_warning;
260 AVRational framerate; /* framerate forced with -r */
262 int guess_layout_max;
266 int resample_pix_fmt;
268 int resample_sample_fmt;
269 int resample_sample_rate;
270 int resample_channels;
271 uint64_t resample_channel_layout;
273 int fix_sub_duration;
274 struct { /* previous decoded subtitle and related variables */
289 /* decoded data from this stream goes into all those filters
290 * currently video and audio only */
291 InputFilter **filters;
296 /* hwaccel options */
297 enum HWAccelID hwaccel_id;
298 char *hwaccel_device;
300 /* hwaccel context */
301 enum HWAccelID active_hwaccel_id;
303 void (*hwaccel_uninit)(AVCodecContext *s);
304 int (*hwaccel_get_buffer)(AVCodecContext *s, AVFrame *frame, int flags);
305 int (*hwaccel_retrieve_data)(AVCodecContext *s, AVFrame *frame);
306 enum AVPixelFormat hwaccel_pix_fmt;
307 enum AVPixelFormat hwaccel_retrieved_pix_fmt;
310 typedef struct InputFile {
311 AVFormatContext *ctx;
312 int eof_reached; /* true if eof reached */
313 int eagain; /* true if last read attempt returned EAGAIN */
314 int ist_index; /* index of first stream in input_streams */
315 int64_t input_ts_offset;
318 int64_t start_time; /* user-specified start time in AV_TIME_BASE or AV_NOPTS_VALUE */
319 int64_t recording_time;
320 int nb_streams; /* number of stream that ffmpeg is aware of; may be different
321 from ctx.nb_streams if new streams appear during av_read_frame() */
322 int nb_streams_warn; /* number of streams that the user was warned of */
327 pthread_t thread; /* thread reading from this file */
328 int finished; /* the thread has exited */
329 int joined; /* the thread has been joined */
330 pthread_mutex_t fifo_lock; /* lock for access to fifo */
331 pthread_cond_t fifo_cond; /* the main thread will signal on this cond after reading from fifo */
332 AVFifoBuffer *fifo; /* demuxed packets are stored here; freed by the main thread */
336 enum forced_keyframes_const {
345 extern const char *const forced_keyframes_const_names[];
348 ENCODER_FINISHED = 1,
352 typedef struct OutputStream {
353 int file_index; /* file index */
354 int index; /* stream index in the output file */
355 int source_index; /* InputStream index */
356 AVStream *st; /* stream in the output file */
357 int encoding_needed; /* true if encoding needed for this stream */
359 /* input pts and corresponding output pts
361 struct InputStream *sync_ist; /* input stream to sync against */
362 int64_t sync_opts; /* output frame counter, could be changed to some true timestamp */ // FIXME look at frame_number
363 /* pts of the first frame encoded for this stream, used for limiting
366 /* dts of the last packet sent to the muxer */
367 int64_t last_mux_dts;
368 AVBitStreamFilterContext *bitstream_filters;
371 AVFrame *filtered_frame;
374 AVRational frame_rate;
378 AVRational frame_aspect_ratio;
380 /* forced key frames */
381 int64_t *forced_kf_pts;
384 char *forced_keyframes;
385 AVExpr *forced_keyframes_pexpr;
386 double forced_keyframes_expr_const_values[FKF_NB];
389 int audio_channels_map[SWR_CH_MAX]; /* list of the channels id to pick from the source stream */
390 int audio_channels_mapped; /* number of channels in audio_channels_map */
392 char *logfile_prefix;
395 OutputFilter *filter;
397 char *filters; ///< filtergraph associated to the -filter option
398 char *filters_script; ///< filtergraph script associated to the -filter_script option
402 AVDictionary *swr_opts;
403 AVDictionary *resample_opts;
405 OSTFinished finished; /* no more packets should be written for this stream */
406 int unavailable; /* true if the steram is unavailable (possibly temporarily) */
408 const char *attachment_filename;
409 int copy_initial_nonkeyframes;
410 int copy_prior_start;
414 AVCodecParserContext *parser;
417 typedef struct OutputFile {
418 AVFormatContext *ctx;
420 int ost_index; /* index of the first stream in output_streams */
421 int64_t recording_time; ///< desired length of the resulting file in microseconds == AV_TIME_BASE units
422 int64_t start_time; ///< start time in microseconds == AV_TIME_BASE units
423 uint64_t limit_filesize; /* filesize limit expressed in bytes */
428 extern InputStream **input_streams;
429 extern int nb_input_streams;
430 extern InputFile **input_files;
431 extern int nb_input_files;
433 extern OutputStream **output_streams;
434 extern int nb_output_streams;
435 extern OutputFile **output_files;
436 extern int nb_output_files;
438 extern FilterGraph **filtergraphs;
439 extern int nb_filtergraphs;
441 extern char *vstats_filename;
443 extern float audio_drift_threshold;
444 extern float dts_delta_threshold;
445 extern float dts_error_threshold;
447 extern int audio_volume;
448 extern int audio_sync_method;
449 extern int video_sync_method;
450 extern int do_benchmark;
451 extern int do_benchmark_all;
452 extern int do_deinterlace;
453 extern int do_hex_dump;
454 extern int do_pkt_dump;
458 extern int exit_on_error;
459 extern int print_stats;
461 extern int stdin_interaction;
462 extern int frame_bits_per_raw_sample;
463 extern AVIOContext *progress_avio;
464 extern float max_error_rate;
466 extern const AVIOInterruptCB int_cb;
468 extern const OptionDef options[];
469 extern const HWAccel hwaccels[];
472 void term_init(void);
473 void term_exit(void);
475 void reset_options(OptionsContext *o, int is_input);
476 void show_usage(void);
478 void opt_output_file(void *optctx, const char *filename);
480 void assert_avoptions(AVDictionary *m);
482 int guess_input_channel_layout(InputStream *ist);
484 enum AVPixelFormat choose_pixel_fmt(AVStream *st, AVCodec *codec, enum AVPixelFormat target);
485 void choose_sample_fmt(AVStream *st, AVCodec *codec);
487 int configure_filtergraph(FilterGraph *fg);
488 int configure_output_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out);
489 int ist_in_filtergraph(FilterGraph *fg, InputStream *ist);
490 FilterGraph *init_simple_filtergraph(InputStream *ist, OutputStream *ost);
492 int ffmpeg_parse_options(int argc, char **argv);
494 int vdpau_init(AVCodecContext *s);
496 #endif /* FFMPEG_H */