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_DROP 0xff
56 #define MAX_STREAMS 1024 /* arbitrary sanity check value */
58 /* select an input stream for an output stream */
59 typedef struct StreamMap {
60 int disabled; /* 1 is this mapping is disabled by a negative map */
64 int sync_stream_index;
65 char *linklabel; /* name of an output link, for mapping lavfi outputs */
69 int file_idx, stream_idx, channel_idx; // input
70 int ofile_idx, ostream_idx; // output
73 typedef struct OptionsContext {
76 /* input/output options */
80 SpecifierOpt *codec_names;
82 SpecifierOpt *audio_channels;
83 int nb_audio_channels;
84 SpecifierOpt *audio_sample_rate;
85 int nb_audio_sample_rate;
86 SpecifierOpt *frame_rates;
88 SpecifierOpt *frame_sizes;
90 SpecifierOpt *frame_pix_fmts;
91 int nb_frame_pix_fmts;
94 int64_t input_ts_offset;
97 SpecifierOpt *ts_scale;
99 SpecifierOpt *dump_attachment;
100 int nb_dump_attachment;
103 StreamMap *stream_maps;
105 AudioChannelMap *audio_channel_maps; /* one info entry per -map_channel */
106 int nb_audio_channel_maps; /* number of (valid) -map_channel settings */
107 int metadata_global_manual;
108 int metadata_streams_manual;
109 int metadata_chapters_manual;
110 const char **attachments;
113 int chapters_input_file;
115 int64_t recording_time;
117 uint64_t limit_filesize;
124 int subtitle_disable;
127 /* indexed by output file stream index */
131 SpecifierOpt *metadata;
133 SpecifierOpt *max_frames;
135 SpecifierOpt *bitstream_filters;
136 int nb_bitstream_filters;
137 SpecifierOpt *codec_tags;
139 SpecifierOpt *sample_fmts;
141 SpecifierOpt *qscale;
143 SpecifierOpt *forced_key_frames;
144 int nb_forced_key_frames;
145 SpecifierOpt *force_fps;
147 SpecifierOpt *frame_aspect_ratios;
148 int nb_frame_aspect_ratios;
149 SpecifierOpt *rc_overrides;
151 SpecifierOpt *intra_matrices;
152 int nb_intra_matrices;
153 SpecifierOpt *inter_matrices;
154 int nb_inter_matrices;
155 SpecifierOpt *top_field_first;
156 int nb_top_field_first;
157 SpecifierOpt *metadata_map;
159 SpecifierOpt *presets;
161 SpecifierOpt *copy_initial_nonkeyframes;
162 int nb_copy_initial_nonkeyframes;
163 SpecifierOpt *copy_prior_start;
164 int nb_copy_prior_start;
165 SpecifierOpt *filters;
167 SpecifierOpt *filter_scripts;
168 int nb_filter_scripts;
169 SpecifierOpt *reinit_filters;
170 int nb_reinit_filters;
171 SpecifierOpt *fix_sub_duration;
172 int nb_fix_sub_duration;
173 SpecifierOpt *canvas_sizes;
177 SpecifierOpt *passlogfiles;
179 SpecifierOpt *guess_layout_max;
180 int nb_guess_layout_max;
183 typedef struct InputFilter {
184 AVFilterContext *filter;
185 struct InputStream *ist;
186 struct FilterGraph *graph;
190 typedef struct OutputFilter {
191 AVFilterContext *filter;
192 struct OutputStream *ost;
193 struct FilterGraph *graph;
196 /* temporary storage until stream maps are processed */
197 AVFilterInOut *out_tmp;
200 typedef struct FilterGraph {
202 const char *graph_desc;
204 AVFilterGraph *graph;
207 InputFilter **inputs;
209 OutputFilter **outputs;
213 typedef struct InputStream {
216 int discard; /* true if stream data should be discarded */
217 int decoding_needed; /* true if the packets must be decoded in 'raw_fifo' */
219 AVFrame *decoded_frame;
220 AVFrame *filter_frame; /* a ref of decoded_frame, to be sent to filters */
222 int64_t start; /* time when read started */
223 /* predicted dts of the next packet read for this stream or (when there are
224 * several frames in a packet) of the next frame in current packet (in AV_TIME_BASE units) */
226 int64_t dts; ///< dts of the last packet read for this stream (in AV_TIME_BASE units)
228 int64_t next_pts; ///< synthetic pts for the next decode frame (in AV_TIME_BASE units)
229 int64_t pts; ///< current pts of the decoded frame (in AV_TIME_BASE units)
230 int wrap_correction_done;
232 int64_t filter_in_rescale_delta_last;
235 int is_start; /* is 1 at the start and after a discontinuity */
237 int showed_multi_packet_warning;
239 AVRational framerate; /* framerate forced with -r */
241 int guess_layout_max;
245 int resample_pix_fmt;
247 int resample_sample_fmt;
248 int resample_sample_rate;
249 int resample_channels;
250 uint64_t resample_channel_layout;
252 int fix_sub_duration;
253 struct { /* previous decoded subtitle and related variables */
268 /* decoded data from this stream goes into all those filters
269 * currently video and audio only */
270 InputFilter **filters;
276 typedef struct InputFile {
277 AVFormatContext *ctx;
278 int eof_reached; /* true if eof reached */
279 int eagain; /* true if last read attempt returned EAGAIN */
280 int ist_index; /* index of first stream in input_streams */
283 int nb_streams; /* number of stream that ffmpeg is aware of; may be different
284 from ctx.nb_streams if new streams appear during av_read_frame() */
285 int nb_streams_warn; /* number of streams that the user was warned of */
289 pthread_t thread; /* thread reading from this file */
290 int finished; /* the thread has exited */
291 int joined; /* the thread has been joined */
292 pthread_mutex_t fifo_lock; /* lock for access to fifo */
293 pthread_cond_t fifo_cond; /* the main thread will signal on this cond after reading from fifo */
294 AVFifoBuffer *fifo; /* demuxed packets are stored here; freed by the main thread */
298 enum forced_keyframes_const {
307 extern const char *const forced_keyframes_const_names[];
309 typedef struct OutputStream {
310 int file_index; /* file index */
311 int index; /* stream index in the output file */
312 int source_index; /* InputStream index */
313 AVStream *st; /* stream in the output file */
314 int encoding_needed; /* true if encoding needed for this stream */
316 /* input pts and corresponding output pts
318 struct InputStream *sync_ist; /* input stream to sync against */
319 int64_t sync_opts; /* output frame counter, could be changed to some true timestamp */ // FIXME look at frame_number
320 /* pts of the first frame encoded for this stream, used for limiting
323 AVBitStreamFilterContext *bitstream_filters;
326 AVFrame *filtered_frame;
329 AVRational frame_rate;
333 AVRational frame_aspect_ratio;
335 /* forced key frames */
336 int64_t *forced_kf_pts;
339 char *forced_keyframes;
340 AVExpr *forced_keyframes_pexpr;
341 double forced_keyframes_expr_const_values[FKF_NB];
344 int audio_channels_map[SWR_CH_MAX]; /* list of the channels id to pick from the source stream */
345 int audio_channels_mapped; /* number of channels in audio_channels_map */
347 char *logfile_prefix;
350 OutputFilter *filter;
355 AVDictionary *swr_opts;
356 AVDictionary *resample_opts;
357 int finished; /* no more packets should be written for this stream */
358 int unavailable; /* true if the steram is unavailable (possibly temporarily) */
360 const char *attachment_filename;
361 int copy_initial_nonkeyframes;
362 int copy_prior_start;
367 typedef struct OutputFile {
368 AVFormatContext *ctx;
370 int ost_index; /* index of the first stream in output_streams */
371 int64_t recording_time; ///< desired length of the resulting file in microseconds == AV_TIME_BASE units
372 int64_t start_time; ///< start time in microseconds == AV_TIME_BASE units
373 uint64_t limit_filesize; /* filesize limit expressed in bytes */
378 extern InputStream **input_streams;
379 extern int nb_input_streams;
380 extern InputFile **input_files;
381 extern int nb_input_files;
383 extern OutputStream **output_streams;
384 extern int nb_output_streams;
385 extern OutputFile **output_files;
386 extern int nb_output_files;
388 extern FilterGraph **filtergraphs;
389 extern int nb_filtergraphs;
391 extern char *vstats_filename;
393 extern float audio_drift_threshold;
394 extern float dts_delta_threshold;
395 extern float dts_error_threshold;
397 extern int audio_volume;
398 extern int audio_sync_method;
399 extern int video_sync_method;
400 extern int do_benchmark;
401 extern int do_benchmark_all;
402 extern int do_deinterlace;
403 extern int do_hex_dump;
404 extern int do_pkt_dump;
408 extern int exit_on_error;
409 extern int print_stats;
411 extern int stdin_interaction;
412 extern int frame_bits_per_raw_sample;
413 extern AVIOContext *progress_avio;
415 extern const AVIOInterruptCB int_cb;
417 extern const OptionDef options[];
419 void term_init(void);
420 void term_exit(void);
422 void reset_options(OptionsContext *o, int is_input);
423 void show_usage(void);
425 void opt_output_file(void *optctx, const char *filename);
427 void assert_avoptions(AVDictionary *m);
429 int guess_input_channel_layout(InputStream *ist);
431 enum AVPixelFormat choose_pixel_fmt(AVStream *st, AVCodec *codec, enum AVPixelFormat target);
432 void choose_sample_fmt(AVStream *st, AVCodec *codec);
434 int configure_filtergraph(FilterGraph *fg);
435 int configure_output_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out);
436 int ist_in_filtergraph(FilterGraph *fg, InputStream *ist);
437 FilterGraph *init_simple_filtergraph(InputStream *ist, OutputStream *ost);
439 int ffmpeg_parse_options(int argc, char **argv);
441 #endif /* FFMPEG_H */