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/hwcontext.h"
44 #include "libavutil/pixfmt.h"
45 #include "libavutil/rational.h"
48 #define VSYNC_PASSTHROUGH 0
62 typedef struct HWAccel {
64 int (*init)(AVCodecContext *s);
66 enum AVPixelFormat pix_fmt;
67 enum AVHWDeviceType device_type;
70 typedef struct HWDevice {
72 enum AVHWDeviceType type;
73 AVBufferRef *device_ref;
76 /* select an input stream for an output stream */
77 typedef struct StreamMap {
78 int disabled; /* 1 is this mapping is disabled by a negative map */
82 int sync_stream_index;
83 char *linklabel; /* name of an output link, for mapping lavfi outputs */
86 /* select an input file for an output file */
87 typedef struct MetadataMap {
88 int file; // file index
89 char type; // type of metadata to copy -- (g)lobal, (s)tream, (c)hapter or (p)rogram
90 int index; // stream/chapter/program number
93 typedef struct OptionsContext {
96 /* input/output options */
100 SpecifierOpt *codec_names;
102 SpecifierOpt *audio_channels;
103 int nb_audio_channels;
104 SpecifierOpt *audio_sample_rate;
105 int nb_audio_sample_rate;
106 SpecifierOpt *frame_rates;
108 SpecifierOpt *frame_sizes;
110 SpecifierOpt *frame_pix_fmts;
111 int nb_frame_pix_fmts;
114 int64_t input_ts_offset;
119 SpecifierOpt *ts_scale;
121 SpecifierOpt *dump_attachment;
122 int nb_dump_attachment;
123 SpecifierOpt *hwaccels;
125 SpecifierOpt *hwaccel_devices;
126 int nb_hwaccel_devices;
127 SpecifierOpt *hwaccel_output_formats;
128 int nb_hwaccel_output_formats;
129 SpecifierOpt *autorotate;
133 StreamMap *stream_maps;
135 /* first item specifies output metadata, second is input */
136 MetadataMap (*meta_data_maps)[2];
137 int nb_meta_data_maps;
138 int metadata_global_manual;
139 int metadata_streams_manual;
140 int metadata_chapters_manual;
141 const char **attachments;
144 int chapters_input_file;
146 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 *bitrates;
175 SpecifierOpt *forced_key_frames;
176 int nb_forced_key_frames;
177 SpecifierOpt *force_fps;
179 SpecifierOpt *frame_aspect_ratios;
180 int nb_frame_aspect_ratios;
181 SpecifierOpt *rc_overrides;
183 SpecifierOpt *intra_matrices;
184 int nb_intra_matrices;
185 SpecifierOpt *inter_matrices;
186 int nb_inter_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 *filters;
197 SpecifierOpt *filter_scripts;
198 int nb_filter_scripts;
201 SpecifierOpt *passlogfiles;
203 SpecifierOpt *max_muxing_queue_size;
204 int nb_max_muxing_queue_size;
207 typedef struct InputFilter {
208 AVFilterContext *filter;
209 struct InputStream *ist;
210 struct FilterGraph *graph;
213 AVFifoBuffer *frame_queue;
215 // parameters configured for this input
219 AVRational sample_aspect_ratio;
222 uint64_t channel_layout;
224 AVBufferRef *hw_frames_ctx;
229 typedef struct OutputFilter {
230 AVFilterContext *filter;
231 struct OutputStream *ost;
232 struct FilterGraph *graph;
235 /* temporary storage until stream maps are processed */
236 AVFilterInOut *out_tmp;
237 enum AVMediaType type;
239 /* desired output stream properties */
241 AVRational frame_rate;
244 uint64_t channel_layout;
246 // those are only set if no format is specified and the encoder gives us multiple options
248 uint64_t *channel_layouts;
252 typedef struct FilterGraph {
254 const char *graph_desc;
256 AVFilterGraph *graph;
258 InputFilter **inputs;
260 OutputFilter **outputs;
264 typedef struct InputStream {
267 int discard; /* true if stream data should be discarded */
268 int decoding_needed; /* true if the packets must be decoded in 'raw_fifo' */
269 AVCodecContext *dec_ctx;
271 AVFrame *decoded_frame;
272 AVFrame *filter_frame; /* a ref of decoded_frame, to be sent to filters */
274 int64_t start; /* time when read started */
275 /* predicted dts of the next packet read for this stream or (when there are
276 * several frames in a packet) of the next frame in current packet */
278 /* dts of the last packet read for this stream */
280 int64_t min_pts; /* pts with the smallest value in a current stream */
281 int64_t max_pts; /* pts with the higher value in a current stream */
283 // when forcing constant input framerate through -r,
284 // this contains the pts that will be given to the next decoded frame
285 int64_t cfr_next_pts;
287 int64_t nb_samples; /* number of samples in the last decoded audio frame before looping */
288 PtsCorrectionContext pts_ctx;
290 AVDictionary *decoder_opts;
291 AVRational framerate; /* framerate forced with -r */
295 /* decoded data from this stream goes into all those filters
296 * currently video and audio only */
297 InputFilter **filters;
300 /* hwaccel options */
301 enum HWAccelID hwaccel_id;
302 char *hwaccel_device;
303 enum AVPixelFormat hwaccel_output_format;
305 /* hwaccel context */
306 enum HWAccelID active_hwaccel_id;
308 void (*hwaccel_uninit)(AVCodecContext *s);
309 int (*hwaccel_get_buffer)(AVCodecContext *s, AVFrame *frame, int flags);
310 int (*hwaccel_retrieve_data)(AVCodecContext *s, AVFrame *frame);
311 enum AVPixelFormat hwaccel_pix_fmt;
312 enum AVPixelFormat hwaccel_retrieved_pix_fmt;
313 AVBufferRef *hw_frames_ctx;
316 // combined size of all the packets read
318 /* number of packets successfully read for this stream */
320 // number of frames/samples retrieved from the decoder
321 uint64_t frames_decoded;
322 uint64_t samples_decoded;
325 typedef struct InputFile {
326 AVFormatContext *ctx;
327 int eof_reached; /* true if eof reached */
328 int eagain; /* true if last read attempt returned EAGAIN */
329 int ist_index; /* index of first stream in ist_table */
330 int loop; /* set number of times input stream should be looped */
331 int64_t duration; /* actual duration of the longest stream in a file
332 at the moment when looping happens */
333 AVRational time_base; /* time base of the duration */
335 int64_t start_time; /* user-specified start time in AV_TIME_BASE or AV_NOPTS_VALUE */
336 int64_t recording_time;
337 int nb_streams; /* number of stream that avconv is aware of; may be different
338 from ctx.nb_streams if new streams appear during av_read_frame() */
343 pthread_t thread; /* thread reading from this file */
344 int finished; /* the thread has exited */
345 int joined; /* the thread has been joined */
346 pthread_mutex_t fifo_lock; /* lock for access to fifo */
347 pthread_cond_t fifo_cond; /* the main thread will signal on this cond after reading from fifo */
348 AVFifoBuffer *fifo; /* demuxed packets are stored here; freed by the main thread */
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 // double sync_ipts; /* dts from the AVPacket of the demuxer in second units */
362 struct InputStream *sync_ist; /* input stream to sync against */
363 int64_t sync_opts; /* output frame counter, could be changed to some true timestamp */ // FIXME look at frame_number
364 /* pts of the first frame encoded for this stream, used for limiting
367 /* dts of the last packet sent to the muxer */
368 int64_t last_mux_dts;
369 // the timebase of the packets sent to the muxer
370 AVRational mux_timebase;
372 int nb_bitstream_filters;
373 AVBSFContext **bsf_ctx;
375 AVCodecContext *enc_ctx;
378 AVFrame *filtered_frame;
383 AVRational frame_rate;
387 float frame_aspect_ratio;
389 /* forced key frames */
390 int64_t *forced_kf_pts;
393 char *forced_keyframes;
395 // the bitrate to send to the muxer for streamcopy
396 int bitrate_override;
398 char *logfile_prefix;
401 OutputFilter *filter;
405 AVDictionary *encoder_opts;
406 AVDictionary *resample_opts;
407 int finished; /* no more packets should be written for this stream */
410 // init_output_stream() has been called for this stream
411 // The encoder and the bistream filters have been initialized and the stream
412 // parameters are set in the AVStream.
415 const char *attachment_filename;
416 int copy_initial_nonkeyframes;
418 enum AVPixelFormat pix_fmts[2];
420 AVCodecParserContext *parser;
421 AVCodecContext *parser_avctx;
424 // combined size of all the packets written
426 // number of packets send to the muxer
427 uint64_t packets_written;
428 // number of frames/samples sent to the encoder
429 uint64_t frames_encoded;
430 uint64_t samples_encoded;
432 /* packet quality factor */
435 int max_muxing_queue_size;
437 /* the packets are buffered here until the muxer is ready to be initialized */
438 AVFifoBuffer *muxing_queue;
441 typedef struct OutputFile {
442 AVFormatContext *ctx;
444 int ost_index; /* index of the first stream in output_streams */
445 int64_t recording_time; /* desired length of the resulting file in microseconds */
446 int64_t start_time; /* start time in microseconds */
447 uint64_t limit_filesize;
454 extern InputStream **input_streams;
455 extern int nb_input_streams;
456 extern InputFile **input_files;
457 extern int nb_input_files;
459 extern OutputStream **output_streams;
460 extern int nb_output_streams;
461 extern OutputFile **output_files;
462 extern int nb_output_files;
464 extern FilterGraph **filtergraphs;
465 extern int nb_filtergraphs;
467 extern char *vstats_filename;
469 extern float audio_drift_threshold;
470 extern float dts_delta_threshold;
472 extern int audio_volume;
473 extern int audio_sync_method;
474 extern int video_sync_method;
475 extern int do_benchmark;
476 extern int do_deinterlace;
477 extern int do_hex_dump;
478 extern int do_pkt_dump;
481 extern int exit_on_error;
482 extern int print_stats;
485 extern const AVIOInterruptCB int_cb;
487 extern const OptionDef options[];
489 extern const HWAccel hwaccels[];
490 extern int hwaccel_lax_profile_check;
491 extern AVBufferRef *hw_device_ctx;
493 void reset_options(OptionsContext *o);
494 void show_usage(void);
496 void opt_output_file(void *optctx, const char *filename);
498 void assert_avoptions(AVDictionary *m);
500 int guess_input_channel_layout(InputStream *ist);
502 int configure_filtergraph(FilterGraph *fg);
503 int configure_output_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out);
504 int ist_in_filtergraph(FilterGraph *fg, InputStream *ist);
505 int filtergraph_is_simple(FilterGraph *fg);
506 int init_simple_filtergraph(InputStream *ist, OutputStream *ost);
507 int init_complex_filtergraph(FilterGraph *fg);
509 int ifilter_parameters_from_frame(InputFilter *ifilter, const AVFrame *frame);
511 int avconv_parse_options(int argc, char **argv);
513 int dxva2_init(AVCodecContext *s);
514 int vda_init(AVCodecContext *s);
515 int qsv_init(AVCodecContext *s);
516 int qsv_transcode_init(OutputStream *ost);
518 HWDevice *hw_device_get_by_name(const char *name);
519 int hw_device_init_from_string(const char *arg, HWDevice **dev);
520 void hw_device_free_all(void);
522 int hw_device_setup_for_decode(InputStream *ist);
523 int hw_device_setup_for_encode(OutputStream *ost);
525 int hwaccel_decode_init(AVCodecContext *avctx);
527 #endif /* AVCONV_H */