2 * This file is part of Libav.
4 * Libav 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 * Libav 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 Libav; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
33 #include "libavformat/avformat.h"
34 #include "libavformat/avio.h"
36 #include "libavcodec/avcodec.h"
38 #include "libavfilter/avfilter.h"
40 #include "libavutil/avutil.h"
41 #include "libavutil/dict.h"
42 #include "libavutil/fifo.h"
43 #include "libavutil/pixfmt.h"
44 #include "libavutil/rational.h"
47 #define VSYNC_PASSTHROUGH 0
61 typedef struct HWAccel {
63 int (*init)(AVCodecContext *s);
65 enum AVPixelFormat pix_fmt;
68 /* select an input stream for an output stream */
69 typedef struct StreamMap {
70 int disabled; /* 1 is this mapping is disabled by a negative map */
74 int sync_stream_index;
75 char *linklabel; /* name of an output link, for mapping lavfi outputs */
78 /* select an input file for an output file */
79 typedef struct MetadataMap {
80 int file; // file index
81 char type; // type of metadata to copy -- (g)lobal, (s)tream, (c)hapter or (p)rogram
82 int index; // stream/chapter/program number
85 typedef struct OptionsContext {
88 /* input/output options */
92 SpecifierOpt *codec_names;
94 SpecifierOpt *audio_channels;
95 int nb_audio_channels;
96 SpecifierOpt *audio_sample_rate;
97 int nb_audio_sample_rate;
98 SpecifierOpt *frame_rates;
100 SpecifierOpt *frame_sizes;
102 SpecifierOpt *frame_pix_fmts;
103 int nb_frame_pix_fmts;
106 int64_t input_ts_offset;
111 SpecifierOpt *ts_scale;
113 SpecifierOpt *dump_attachment;
114 int nb_dump_attachment;
115 SpecifierOpt *hwaccels;
117 SpecifierOpt *hwaccel_devices;
118 int nb_hwaccel_devices;
119 SpecifierOpt *hwaccel_output_formats;
120 int nb_hwaccel_output_formats;
121 SpecifierOpt *autorotate;
125 StreamMap *stream_maps;
127 /* first item specifies output metadata, second is input */
128 MetadataMap (*meta_data_maps)[2];
129 int nb_meta_data_maps;
130 int metadata_global_manual;
131 int metadata_streams_manual;
132 int metadata_chapters_manual;
133 const char **attachments;
136 int chapters_input_file;
138 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 *top_field_first;
178 int nb_top_field_first;
179 SpecifierOpt *metadata_map;
181 SpecifierOpt *presets;
183 SpecifierOpt *copy_initial_nonkeyframes;
184 int nb_copy_initial_nonkeyframes;
185 SpecifierOpt *filters;
187 SpecifierOpt *filter_scripts;
188 int nb_filter_scripts;
191 SpecifierOpt *passlogfiles;
193 SpecifierOpt *max_muxing_queue_size;
194 int nb_max_muxing_queue_size;
197 typedef struct InputFilter {
198 AVFilterContext *filter;
199 struct InputStream *ist;
200 struct FilterGraph *graph;
203 // parameters configured for this input
207 AVRational sample_aspect_ratio;
210 uint64_t channel_layout;
212 AVBufferRef *hw_frames_ctx;
215 typedef struct OutputFilter {
216 AVFilterContext *filter;
217 struct OutputStream *ost;
218 struct FilterGraph *graph;
221 /* temporary storage until stream maps are processed */
222 AVFilterInOut *out_tmp;
223 enum AVMediaType type;
225 /* desired output stream properties */
227 AVRational frame_rate;
230 uint64_t channel_layout;
232 // those are only set if no format is specified and the encoder gives us multiple options
234 uint64_t *channel_layouts;
238 typedef struct FilterGraph {
240 const char *graph_desc;
242 AVFilterGraph *graph;
244 InputFilter **inputs;
246 OutputFilter **outputs;
250 typedef struct InputStream {
253 int discard; /* true if stream data should be discarded */
254 int decoding_needed; /* true if the packets must be decoded in 'raw_fifo' */
255 AVCodecContext *dec_ctx;
257 AVFrame *decoded_frame;
258 AVFrame *filter_frame; /* a ref of decoded_frame, to be sent to filters */
260 int64_t start; /* time when read started */
261 /* predicted dts of the next packet read for this stream or (when there are
262 * several frames in a packet) of the next frame in current packet */
264 /* dts of the last packet read for this stream */
266 int64_t min_pts; /* pts with the smallest value in a current stream */
267 int64_t max_pts; /* pts with the higher value in a current stream */
268 int64_t nb_samples; /* number of samples in the last decoded audio frame before looping */
269 PtsCorrectionContext pts_ctx;
271 AVDictionary *decoder_opts;
272 AVRational framerate; /* framerate forced with -r */
277 int resample_pix_fmt;
279 int resample_sample_fmt;
280 int resample_sample_rate;
281 int resample_channels;
282 uint64_t resample_channel_layout;
284 /* decoded data from this stream goes into all those filters
285 * currently video and audio only */
286 InputFilter **filters;
289 /* hwaccel options */
290 enum HWAccelID hwaccel_id;
291 char *hwaccel_device;
292 enum AVPixelFormat hwaccel_output_format;
294 /* hwaccel context */
295 enum HWAccelID active_hwaccel_id;
297 void (*hwaccel_uninit)(AVCodecContext *s);
298 int (*hwaccel_get_buffer)(AVCodecContext *s, AVFrame *frame, int flags);
299 int (*hwaccel_retrieve_data)(AVCodecContext *s, AVFrame *frame);
300 enum AVPixelFormat hwaccel_pix_fmt;
301 enum AVPixelFormat hwaccel_retrieved_pix_fmt;
302 AVBufferRef *hw_frames_ctx;
305 // combined size of all the packets read
307 /* number of packets successfully read for this stream */
309 // number of frames/samples retrieved from the decoder
310 uint64_t frames_decoded;
311 uint64_t samples_decoded;
314 typedef struct InputFile {
315 AVFormatContext *ctx;
316 int eof_reached; /* true if eof reached */
317 int eagain; /* true if last read attempt returned EAGAIN */
318 int ist_index; /* index of first stream in ist_table */
319 int loop; /* set number of times input stream should be looped */
320 int64_t duration; /* actual duration of the longest stream in a file
321 at the moment when looping happens */
322 AVRational time_base; /* time base of the duration */
324 int64_t start_time; /* user-specified start time in AV_TIME_BASE or AV_NOPTS_VALUE */
325 int64_t recording_time;
326 int nb_streams; /* number of stream that avconv is aware of; may be different
327 from ctx.nb_streams if new streams appear during av_read_frame() */
332 pthread_t thread; /* thread reading from this file */
333 int finished; /* the thread has exited */
334 int joined; /* the thread has been joined */
335 pthread_mutex_t fifo_lock; /* lock for access to fifo */
336 pthread_cond_t fifo_cond; /* the main thread will signal on this cond after reading from fifo */
337 AVFifoBuffer *fifo; /* demuxed packets are stored here; freed by the main thread */
341 typedef struct OutputStream {
342 int file_index; /* file index */
343 int index; /* stream index in the output file */
344 int source_index; /* InputStream index */
345 AVStream *st; /* stream in the output file */
346 int encoding_needed; /* true if encoding needed for this stream */
348 /* input pts and corresponding output pts
350 // double sync_ipts; /* dts from the AVPacket of the demuxer in second units */
351 struct InputStream *sync_ist; /* input stream to sync against */
352 int64_t sync_opts; /* output frame counter, could be changed to some true timestamp */ // FIXME look at frame_number
353 /* pts of the first frame encoded for this stream, used for limiting
356 /* dts of the last packet sent to the muxer */
357 int64_t last_mux_dts;
359 int nb_bitstream_filters;
360 const AVBitStreamFilter **bitstream_filters;
361 AVBSFContext **bsf_ctx;
363 AVCodecContext *enc_ctx;
366 AVFrame *filtered_frame;
371 AVRational frame_rate;
375 float frame_aspect_ratio;
377 /* forced key frames */
378 int64_t *forced_kf_pts;
381 char *forced_keyframes;
383 char *logfile_prefix;
386 OutputFilter *filter;
390 AVDictionary *encoder_opts;
391 AVDictionary *resample_opts;
392 int finished; /* no more packets should be written for this stream */
395 // init_output_stream() has been called for this stream
396 // The encoder and the bistream filters have been initialized and the stream
397 // parameters are set in the AVStream.
400 const char *attachment_filename;
401 int copy_initial_nonkeyframes;
403 enum AVPixelFormat pix_fmts[2];
405 AVCodecParserContext *parser;
406 AVCodecContext *parser_avctx;
409 // combined size of all the packets written
411 // number of packets send to the muxer
412 uint64_t packets_written;
413 // number of frames/samples sent to the encoder
414 uint64_t frames_encoded;
415 uint64_t samples_encoded;
417 /* packet quality factor */
420 int max_muxing_queue_size;
422 /* the packets are buffered here until the muxer is ready to be initialized */
423 AVFifoBuffer *muxing_queue;
426 typedef struct OutputFile {
427 AVFormatContext *ctx;
429 int ost_index; /* index of the first stream in output_streams */
430 int64_t recording_time; /* desired length of the resulting file in microseconds */
431 int64_t start_time; /* start time in microseconds */
432 uint64_t limit_filesize;
439 extern InputStream **input_streams;
440 extern int nb_input_streams;
441 extern InputFile **input_files;
442 extern int nb_input_files;
444 extern OutputStream **output_streams;
445 extern int nb_output_streams;
446 extern OutputFile **output_files;
447 extern int nb_output_files;
449 extern FilterGraph **filtergraphs;
450 extern int nb_filtergraphs;
452 extern char *vstats_filename;
454 extern float audio_drift_threshold;
455 extern float dts_delta_threshold;
457 extern int audio_volume;
458 extern int audio_sync_method;
459 extern int video_sync_method;
460 extern int do_benchmark;
461 extern int do_deinterlace;
462 extern int do_hex_dump;
463 extern int do_pkt_dump;
466 extern int exit_on_error;
467 extern int print_stats;
470 extern const AVIOInterruptCB int_cb;
472 extern const OptionDef options[];
474 extern const HWAccel hwaccels[];
475 extern int hwaccel_lax_profile_check;
476 extern AVBufferRef *hw_device_ctx;
478 void reset_options(OptionsContext *o);
479 void show_usage(void);
481 void opt_output_file(void *optctx, const char *filename);
483 void assert_avoptions(AVDictionary *m);
485 int guess_input_channel_layout(InputStream *ist);
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 int filtergraph_is_simple(FilterGraph *fg);
491 int init_simple_filtergraph(InputStream *ist, OutputStream *ost);
492 int init_complex_filtergraph(FilterGraph *fg);
494 int ifilter_parameters_from_frame(InputFilter *ifilter, const AVFrame *frame);
495 int ifilter_parameters_from_decoder(InputFilter *ifilter, const AVCodecContext *avctx);
497 int avconv_parse_options(int argc, char **argv);
499 int vdpau_init(AVCodecContext *s);
500 int dxva2_init(AVCodecContext *s);
501 int vda_init(AVCodecContext *s);
502 int qsv_init(AVCodecContext *s);
503 int qsv_transcode_init(OutputStream *ost);
504 int vaapi_decode_init(AVCodecContext *avctx);
505 int vaapi_device_init(const char *device);
507 #endif /* AVCONV_H */