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 */
70 typedef struct HWAccel {
72 int (*init)(AVCodecContext *s);
74 enum AVPixelFormat pix_fmt;
77 /* select an input stream for an output stream */
78 typedef struct StreamMap {
79 int disabled; /* 1 is this mapping is disabled by a negative map */
83 int sync_stream_index;
84 char *linklabel; /* name of an output link, for mapping lavfi outputs */
88 int file_idx, stream_idx, channel_idx; // input
89 int ofile_idx, ostream_idx; // output
92 typedef struct OptionsContext {
95 /* input/output options */
97 int64_t start_time_eof;
101 SpecifierOpt *codec_names;
103 SpecifierOpt *audio_channels;
104 int nb_audio_channels;
105 SpecifierOpt *audio_sample_rate;
106 int nb_audio_sample_rate;
107 SpecifierOpt *frame_rates;
109 SpecifierOpt *frame_sizes;
111 SpecifierOpt *frame_pix_fmts;
112 int nb_frame_pix_fmts;
115 int64_t input_ts_offset;
119 int thread_queue_size;
121 SpecifierOpt *ts_scale;
123 SpecifierOpt *dump_attachment;
124 int nb_dump_attachment;
125 SpecifierOpt *hwaccels;
127 SpecifierOpt *hwaccel_devices;
128 int nb_hwaccel_devices;
129 SpecifierOpt *autorotate;
133 StreamMap *stream_maps;
135 AudioChannelMap *audio_channel_maps; /* one info entry per -map_channel */
136 int nb_audio_channel_maps; /* number of (valid) -map_channel settings */
137 int metadata_global_manual;
138 int metadata_streams_manual;
139 int metadata_chapters_manual;
140 const char **attachments;
143 int chapters_input_file;
145 int64_t recording_time;
147 uint64_t limit_filesize;
154 int subtitle_disable;
157 /* indexed by output file stream index */
161 SpecifierOpt *metadata;
163 SpecifierOpt *max_frames;
165 SpecifierOpt *bitstream_filters;
166 int nb_bitstream_filters;
167 SpecifierOpt *codec_tags;
169 SpecifierOpt *sample_fmts;
171 SpecifierOpt *qscale;
173 SpecifierOpt *forced_key_frames;
174 int nb_forced_key_frames;
175 SpecifierOpt *force_fps;
177 SpecifierOpt *frame_aspect_ratios;
178 int nb_frame_aspect_ratios;
179 SpecifierOpt *rc_overrides;
181 SpecifierOpt *intra_matrices;
182 int nb_intra_matrices;
183 SpecifierOpt *inter_matrices;
184 int nb_inter_matrices;
185 SpecifierOpt *chroma_intra_matrices;
186 int nb_chroma_intra_matrices;
187 SpecifierOpt *top_field_first;
188 int nb_top_field_first;
189 SpecifierOpt *metadata_map;
191 SpecifierOpt *presets;
193 SpecifierOpt *copy_initial_nonkeyframes;
194 int nb_copy_initial_nonkeyframes;
195 SpecifierOpt *copy_prior_start;
196 int nb_copy_prior_start;
197 SpecifierOpt *filters;
199 SpecifierOpt *filter_scripts;
200 int nb_filter_scripts;
201 SpecifierOpt *reinit_filters;
202 int nb_reinit_filters;
203 SpecifierOpt *fix_sub_duration;
204 int nb_fix_sub_duration;
205 SpecifierOpt *canvas_sizes;
209 SpecifierOpt *passlogfiles;
211 SpecifierOpt *guess_layout_max;
212 int nb_guess_layout_max;
215 SpecifierOpt *discard;
217 SpecifierOpt *disposition;
221 typedef struct InputFilter {
222 AVFilterContext *filter;
223 struct InputStream *ist;
224 struct FilterGraph *graph;
228 typedef struct OutputFilter {
229 AVFilterContext *filter;
230 struct OutputStream *ost;
231 struct FilterGraph *graph;
234 /* temporary storage until stream maps are processed */
235 AVFilterInOut *out_tmp;
236 enum AVMediaType type;
239 typedef struct FilterGraph {
241 const char *graph_desc;
243 AVFilterGraph *graph;
246 InputFilter **inputs;
248 OutputFilter **outputs;
252 typedef struct InputStream {
255 int discard; /* true if stream data should be discarded */
256 int user_set_discard;
257 int decoding_needed; /* non zero if the packets must be decoded in 'raw_fifo', see DECODING_FOR_* */
258 #define DECODING_FOR_OST 1
259 #define DECODING_FOR_FILTER 2
261 AVCodecContext *dec_ctx;
263 AVFrame *decoded_frame;
264 AVFrame *filter_frame; /* a ref of decoded_frame, to be sent to filters */
266 int64_t start; /* time when read started */
267 /* predicted dts of the next packet read for this stream or (when there are
268 * several frames in a packet) of the next frame in current packet (in AV_TIME_BASE units) */
270 int64_t dts; ///< dts of the last packet read for this stream (in AV_TIME_BASE units)
272 int64_t next_pts; ///< synthetic pts for the next decode frame (in AV_TIME_BASE units)
273 int64_t pts; ///< current pts of the decoded frame (in AV_TIME_BASE units)
274 int wrap_correction_done;
276 int64_t filter_in_rescale_delta_last;
278 int64_t min_pts; /* pts with the smallest value in a current stream */
279 int64_t max_pts; /* pts with the higher value in a current stream */
280 int64_t nb_samples; /* number of samples in the last decoded audio frame before looping */
284 int showed_multi_packet_warning;
285 AVDictionary *decoder_opts;
286 AVRational framerate; /* framerate forced with -r */
288 int guess_layout_max;
293 int resample_pix_fmt;
295 int resample_sample_fmt;
296 int resample_sample_rate;
297 int resample_channels;
298 uint64_t resample_channel_layout;
300 int fix_sub_duration;
301 struct { /* previous decoded subtitle and related variables */
316 /* decoded data from this stream goes into all those filters
317 * currently video and audio only */
318 InputFilter **filters;
323 /* hwaccel options */
324 enum HWAccelID hwaccel_id;
325 char *hwaccel_device;
327 /* hwaccel context */
328 enum HWAccelID active_hwaccel_id;
330 void (*hwaccel_uninit)(AVCodecContext *s);
331 int (*hwaccel_get_buffer)(AVCodecContext *s, AVFrame *frame, int flags);
332 int (*hwaccel_retrieve_data)(AVCodecContext *s, AVFrame *frame);
333 enum AVPixelFormat hwaccel_pix_fmt;
334 enum AVPixelFormat hwaccel_retrieved_pix_fmt;
337 // combined size of all the packets read
339 /* number of packets successfully read for this stream */
341 // number of frames/samples retrieved from the decoder
342 uint64_t frames_decoded;
343 uint64_t samples_decoded;
346 typedef struct InputFile {
347 AVFormatContext *ctx;
348 int eof_reached; /* true if eof reached */
349 int eagain; /* true if last read attempt returned EAGAIN */
350 int ist_index; /* index of first stream in input_streams */
351 int loop; /* set number of times input stream should be looped */
352 int64_t duration; /* actual duration of the longest stream in a file
353 at the moment when looping happens */
354 AVRational time_base; /* time base of the duration */
355 int64_t input_ts_offset;
359 int64_t start_time; /* user-specified start time in AV_TIME_BASE or AV_NOPTS_VALUE */
361 int64_t recording_time;
362 int nb_streams; /* number of stream that ffmpeg is aware of; may be different
363 from ctx.nb_streams if new streams appear during av_read_frame() */
364 int nb_streams_warn; /* number of streams that the user was warned of */
369 AVThreadMessageQueue *in_thread_queue;
370 pthread_t thread; /* thread reading from this file */
371 int non_blocking; /* reading packets from the thread should not block */
372 int joined; /* the thread has been joined */
373 int thread_queue_size; /* maximum number of queued packets */
377 enum forced_keyframes_const {
386 #define ABORT_ON_FLAG_EMPTY_OUTPUT (1 << 0)
388 extern const char *const forced_keyframes_const_names[];
391 ENCODER_FINISHED = 1,
395 typedef struct OutputStream {
396 int file_index; /* file index */
397 int index; /* stream index in the output file */
398 int source_index; /* InputStream index */
399 AVStream *st; /* stream in the output file */
400 int encoding_needed; /* true if encoding needed for this stream */
402 /* input pts and corresponding output pts
404 struct InputStream *sync_ist; /* input stream to sync against */
405 int64_t sync_opts; /* output frame counter, could be changed to some true timestamp */ // FIXME look at frame_number
406 /* pts of the first frame encoded for this stream, used for limiting
409 /* dts of the last packet sent to the muxer */
410 int64_t last_mux_dts;
411 AVBitStreamFilterContext *bitstream_filters;
412 AVCodecContext *enc_ctx;
415 AVFrame *filtered_frame;
418 int last_nb0_frames[3];
423 AVRational frame_rate;
426 int rotate_overridden;
428 AVRational frame_aspect_ratio;
430 /* forced key frames */
431 int64_t *forced_kf_pts;
434 char *forced_keyframes;
435 AVExpr *forced_keyframes_pexpr;
436 double forced_keyframes_expr_const_values[FKF_NB];
439 int *audio_channels_map; /* list of the channels id to pick from the source stream */
440 int audio_channels_mapped; /* number of channels in audio_channels_map */
442 char *logfile_prefix;
445 OutputFilter *filter;
447 char *filters; ///< filtergraph associated to the -filter option
448 char *filters_script; ///< filtergraph script associated to the -filter_script option
450 AVDictionary *encoder_opts;
451 AVDictionary *sws_dict;
452 AVDictionary *swr_opts;
453 AVDictionary *resample_opts;
454 AVDictionary *bsf_args;
456 OSTFinished finished; /* no more packets should be written for this stream */
457 int unavailable; /* true if the steram is unavailable (possibly temporarily) */
459 const char *attachment_filename;
460 int copy_initial_nonkeyframes;
461 int copy_prior_start;
466 AVCodecParserContext *parser;
469 // combined size of all the packets written
471 // number of packets send to the muxer
472 uint64_t packets_written;
473 // number of frames/samples sent to the encoder
474 uint64_t frames_encoded;
475 uint64_t samples_encoded;
477 /* packet quality factor */
480 /* packet picture type */
483 /* frame encode sum of squared error values */
487 typedef struct OutputFile {
488 AVFormatContext *ctx;
490 int ost_index; /* index of the first stream in output_streams */
491 int64_t recording_time; ///< desired length of the resulting file in microseconds == AV_TIME_BASE units
492 int64_t start_time; ///< start time in microseconds == AV_TIME_BASE units
493 uint64_t limit_filesize; /* filesize limit expressed in bytes */
498 extern InputStream **input_streams;
499 extern int nb_input_streams;
500 extern InputFile **input_files;
501 extern int nb_input_files;
503 extern OutputStream **output_streams;
504 extern int nb_output_streams;
505 extern OutputFile **output_files;
506 extern int nb_output_files;
508 extern FilterGraph **filtergraphs;
509 extern int nb_filtergraphs;
511 extern char *vstats_filename;
512 extern char *sdp_filename;
514 extern float audio_drift_threshold;
515 extern float dts_delta_threshold;
516 extern float dts_error_threshold;
518 extern int audio_volume;
519 extern int audio_sync_method;
520 extern int video_sync_method;
521 extern float frame_drop_threshold;
522 extern int do_benchmark;
523 extern int do_benchmark_all;
524 extern int do_deinterlace;
525 extern int do_hex_dump;
526 extern int do_pkt_dump;
528 extern int start_at_zero;
531 extern int exit_on_error;
532 extern int abort_on_flags;
533 extern int print_stats;
535 extern int stdin_interaction;
536 extern int frame_bits_per_raw_sample;
537 extern AVIOContext *progress_avio;
538 extern float max_error_rate;
539 extern int vdpau_api_ver;
540 extern char *videotoolbox_pixfmt;
542 extern const AVIOInterruptCB int_cb;
544 extern const OptionDef options[];
545 extern const HWAccel hwaccels[];
548 void term_init(void);
549 void term_exit(void);
551 void reset_options(OptionsContext *o, int is_input);
552 void show_usage(void);
554 void opt_output_file(void *optctx, const char *filename);
556 void remove_avoptions(AVDictionary **a, AVDictionary *b);
557 void assert_avoptions(AVDictionary *m);
559 int guess_input_channel_layout(InputStream *ist);
561 enum AVPixelFormat choose_pixel_fmt(AVStream *st, AVCodecContext *avctx, AVCodec *codec, enum AVPixelFormat target);
562 void choose_sample_fmt(AVStream *st, AVCodec *codec);
564 int configure_filtergraph(FilterGraph *fg);
565 int configure_output_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out);
566 int ist_in_filtergraph(FilterGraph *fg, InputStream *ist);
567 FilterGraph *init_simple_filtergraph(InputStream *ist, OutputStream *ost);
568 int init_complex_filtergraph(FilterGraph *fg);
570 int ffmpeg_parse_options(int argc, char **argv);
572 int vdpau_init(AVCodecContext *s);
573 int dxva2_init(AVCodecContext *s);
574 int vda_init(AVCodecContext *s);
575 int videotoolbox_init(AVCodecContext *s);
576 int qsv_init(AVCodecContext *s);
577 int qsv_transcode_init(OutputStream *ost);
579 #endif /* FFMPEG_H */