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 */
95 int64_t start_time_eof;
99 SpecifierOpt *codec_names;
101 SpecifierOpt *audio_channels;
102 int nb_audio_channels;
103 SpecifierOpt *audio_sample_rate;
104 int nb_audio_sample_rate;
105 SpecifierOpt *frame_rates;
107 SpecifierOpt *frame_sizes;
109 SpecifierOpt *frame_pix_fmts;
110 int nb_frame_pix_fmts;
113 int64_t input_ts_offset;
116 int thread_queue_size;
118 SpecifierOpt *ts_scale;
120 SpecifierOpt *dump_attachment;
121 int nb_dump_attachment;
122 SpecifierOpt *hwaccels;
124 SpecifierOpt *hwaccel_devices;
125 int nb_hwaccel_devices;
126 SpecifierOpt *autorotate;
130 StreamMap *stream_maps;
132 AudioChannelMap *audio_channel_maps; /* one info entry per -map_channel */
133 int nb_audio_channel_maps; /* number of (valid) -map_channel settings */
134 int metadata_global_manual;
135 int metadata_streams_manual;
136 int metadata_chapters_manual;
137 const char **attachments;
140 int chapters_input_file;
142 int64_t recording_time;
144 uint64_t limit_filesize;
151 int subtitle_disable;
154 /* indexed by output file stream index */
158 SpecifierOpt *metadata;
160 SpecifierOpt *max_frames;
162 SpecifierOpt *bitstream_filters;
163 int nb_bitstream_filters;
164 SpecifierOpt *codec_tags;
166 SpecifierOpt *sample_fmts;
168 SpecifierOpt *qscale;
170 SpecifierOpt *forced_key_frames;
171 int nb_forced_key_frames;
172 SpecifierOpt *force_fps;
174 SpecifierOpt *frame_aspect_ratios;
175 int nb_frame_aspect_ratios;
176 SpecifierOpt *rc_overrides;
178 SpecifierOpt *intra_matrices;
179 int nb_intra_matrices;
180 SpecifierOpt *inter_matrices;
181 int nb_inter_matrices;
182 SpecifierOpt *chroma_intra_matrices;
183 int nb_chroma_intra_matrices;
184 SpecifierOpt *top_field_first;
185 int nb_top_field_first;
186 SpecifierOpt *metadata_map;
188 SpecifierOpt *presets;
190 SpecifierOpt *copy_initial_nonkeyframes;
191 int nb_copy_initial_nonkeyframes;
192 SpecifierOpt *copy_prior_start;
193 int nb_copy_prior_start;
194 SpecifierOpt *filters;
196 SpecifierOpt *filter_scripts;
197 int nb_filter_scripts;
198 SpecifierOpt *reinit_filters;
199 int nb_reinit_filters;
200 SpecifierOpt *fix_sub_duration;
201 int nb_fix_sub_duration;
202 SpecifierOpt *canvas_sizes;
206 SpecifierOpt *passlogfiles;
208 SpecifierOpt *guess_layout_max;
209 int nb_guess_layout_max;
212 SpecifierOpt *discard;
214 SpecifierOpt *disposition;
218 typedef struct InputFilter {
219 AVFilterContext *filter;
220 struct InputStream *ist;
221 struct FilterGraph *graph;
225 typedef struct OutputFilter {
226 AVFilterContext *filter;
227 struct OutputStream *ost;
228 struct FilterGraph *graph;
231 /* temporary storage until stream maps are processed */
232 AVFilterInOut *out_tmp;
233 enum AVMediaType type;
236 typedef struct FilterGraph {
238 const char *graph_desc;
240 AVFilterGraph *graph;
243 InputFilter **inputs;
245 OutputFilter **outputs;
249 typedef struct InputStream {
252 int discard; /* true if stream data should be discarded */
253 int user_set_discard;
254 int decoding_needed; /* non zero if the packets must be decoded in 'raw_fifo', see DECODING_FOR_* */
255 #define DECODING_FOR_OST 1
256 #define DECODING_FOR_FILTER 2
258 AVCodecContext *dec_ctx;
260 AVFrame *decoded_frame;
261 AVFrame *filter_frame; /* a ref of decoded_frame, to be sent to filters */
263 int64_t start; /* time when read started */
264 /* predicted dts of the next packet read for this stream or (when there are
265 * several frames in a packet) of the next frame in current packet (in AV_TIME_BASE units) */
267 int64_t dts; ///< dts of the last packet read for this stream (in AV_TIME_BASE units)
269 int64_t next_pts; ///< synthetic pts for the next decode frame (in AV_TIME_BASE units)
270 int64_t pts; ///< current pts of the decoded frame (in AV_TIME_BASE units)
271 int wrap_correction_done;
273 int64_t filter_in_rescale_delta_last;
277 int showed_multi_packet_warning;
278 AVDictionary *decoder_opts;
279 AVRational framerate; /* framerate forced with -r */
281 int guess_layout_max;
286 int resample_pix_fmt;
288 int resample_sample_fmt;
289 int resample_sample_rate;
290 int resample_channels;
291 uint64_t resample_channel_layout;
293 int fix_sub_duration;
294 struct { /* previous decoded subtitle and related variables */
309 /* decoded data from this stream goes into all those filters
310 * currently video and audio only */
311 InputFilter **filters;
316 /* hwaccel options */
317 enum HWAccelID hwaccel_id;
318 char *hwaccel_device;
320 /* hwaccel context */
321 enum HWAccelID active_hwaccel_id;
323 void (*hwaccel_uninit)(AVCodecContext *s);
324 int (*hwaccel_get_buffer)(AVCodecContext *s, AVFrame *frame, int flags);
325 int (*hwaccel_retrieve_data)(AVCodecContext *s, AVFrame *frame);
326 enum AVPixelFormat hwaccel_pix_fmt;
327 enum AVPixelFormat hwaccel_retrieved_pix_fmt;
330 // combined size of all the packets read
332 /* number of packets successfully read for this stream */
334 // number of frames/samples retrieved from the decoder
335 uint64_t frames_decoded;
336 uint64_t samples_decoded;
339 typedef struct InputFile {
340 AVFormatContext *ctx;
341 int eof_reached; /* true if eof reached */
342 int eagain; /* true if last read attempt returned EAGAIN */
343 int ist_index; /* index of first stream in input_streams */
344 int64_t input_ts_offset;
347 int64_t start_time; /* user-specified start time in AV_TIME_BASE or AV_NOPTS_VALUE */
349 int64_t recording_time;
350 int nb_streams; /* number of stream that ffmpeg is aware of; may be different
351 from ctx.nb_streams if new streams appear during av_read_frame() */
352 int nb_streams_warn; /* number of streams that the user was warned of */
357 AVThreadMessageQueue *in_thread_queue;
358 pthread_t thread; /* thread reading from this file */
359 int non_blocking; /* reading packets from the thread should not block */
360 int joined; /* the thread has been joined */
361 int thread_queue_size; /* maximum number of queued packets */
365 enum forced_keyframes_const {
374 extern const char *const forced_keyframes_const_names[];
377 ENCODER_FINISHED = 1,
381 typedef struct OutputStream {
382 int file_index; /* file index */
383 int index; /* stream index in the output file */
384 int source_index; /* InputStream index */
385 AVStream *st; /* stream in the output file */
386 int encoding_needed; /* true if encoding needed for this stream */
388 /* input pts and corresponding output pts
390 struct InputStream *sync_ist; /* input stream to sync against */
391 int64_t sync_opts; /* output frame counter, could be changed to some true timestamp */ // FIXME look at frame_number
392 /* pts of the first frame encoded for this stream, used for limiting
395 /* dts of the last packet sent to the muxer */
396 int64_t last_mux_dts;
397 AVBitStreamFilterContext *bitstream_filters;
398 AVCodecContext *enc_ctx;
401 AVFrame *filtered_frame;
404 int last_nb0_frames[3];
407 AVRational frame_rate;
410 int rotate_overridden;
412 AVRational frame_aspect_ratio;
414 /* forced key frames */
415 int64_t *forced_kf_pts;
418 char *forced_keyframes;
419 AVExpr *forced_keyframes_pexpr;
420 double forced_keyframes_expr_const_values[FKF_NB];
423 int *audio_channels_map; /* list of the channels id to pick from the source stream */
424 int audio_channels_mapped; /* number of channels in audio_channels_map */
426 char *logfile_prefix;
429 OutputFilter *filter;
431 char *filters; ///< filtergraph associated to the -filter option
432 char *filters_script; ///< filtergraph script associated to the -filter_script option
435 AVDictionary *encoder_opts;
436 AVDictionary *swr_opts;
437 AVDictionary *resample_opts;
438 AVDictionary *bsf_args;
440 OSTFinished finished; /* no more packets should be written for this stream */
441 int unavailable; /* true if the steram is unavailable (possibly temporarily) */
443 const char *attachment_filename;
444 int copy_initial_nonkeyframes;
445 int copy_prior_start;
450 AVCodecParserContext *parser;
453 // combined size of all the packets written
455 // number of packets send to the muxer
456 uint64_t packets_written;
457 // number of frames/samples sent to the encoder
458 uint64_t frames_encoded;
459 uint64_t samples_encoded;
461 /* packet quality factor */
464 /* packet picture type */
467 /* frame encode sum of squared error values */
471 typedef struct OutputFile {
472 AVFormatContext *ctx;
474 int ost_index; /* index of the first stream in output_streams */
475 int64_t recording_time; ///< desired length of the resulting file in microseconds == AV_TIME_BASE units
476 int64_t start_time; ///< start time in microseconds == AV_TIME_BASE units
477 uint64_t limit_filesize; /* filesize limit expressed in bytes */
482 extern InputStream **input_streams;
483 extern int nb_input_streams;
484 extern InputFile **input_files;
485 extern int nb_input_files;
487 extern OutputStream **output_streams;
488 extern int nb_output_streams;
489 extern OutputFile **output_files;
490 extern int nb_output_files;
492 extern FilterGraph **filtergraphs;
493 extern int nb_filtergraphs;
495 extern char *vstats_filename;
496 extern char *sdp_filename;
498 extern float audio_drift_threshold;
499 extern float dts_delta_threshold;
500 extern float dts_error_threshold;
502 extern int audio_volume;
503 extern int audio_sync_method;
504 extern int video_sync_method;
505 extern float frame_drop_threshold;
506 extern int do_benchmark;
507 extern int do_benchmark_all;
508 extern int do_deinterlace;
509 extern int do_hex_dump;
510 extern int do_pkt_dump;
512 extern int start_at_zero;
515 extern int exit_on_error;
516 extern int print_stats;
518 extern int stdin_interaction;
519 extern int frame_bits_per_raw_sample;
520 extern AVIOContext *progress_avio;
521 extern float max_error_rate;
522 extern int vdpau_api_ver;
524 extern const AVIOInterruptCB int_cb;
526 extern const OptionDef options[];
527 extern const HWAccel hwaccels[];
530 void term_init(void);
531 void term_exit(void);
533 void reset_options(OptionsContext *o, int is_input);
534 void show_usage(void);
536 void opt_output_file(void *optctx, const char *filename);
538 void remove_avoptions(AVDictionary **a, AVDictionary *b);
539 void assert_avoptions(AVDictionary *m);
541 int guess_input_channel_layout(InputStream *ist);
543 enum AVPixelFormat choose_pixel_fmt(AVStream *st, AVCodecContext *avctx, AVCodec *codec, enum AVPixelFormat target);
544 void choose_sample_fmt(AVStream *st, AVCodec *codec);
546 int configure_filtergraph(FilterGraph *fg);
547 int configure_output_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out);
548 int ist_in_filtergraph(FilterGraph *fg, InputStream *ist);
549 FilterGraph *init_simple_filtergraph(InputStream *ist, OutputStream *ost);
550 int init_complex_filtergraph(FilterGraph *fg);
552 int ffmpeg_parse_options(int argc, char **argv);
554 int vdpau_init(AVCodecContext *s);
555 int dxva2_init(AVCodecContext *s);
556 int vda_init(AVCodecContext *s);
558 #endif /* FFMPEG_H */