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
60 typedef struct HWAccel {
62 int (*init)(AVCodecContext *s);
64 enum AVPixelFormat pix_fmt;
67 /* select an input stream for an output stream */
68 typedef struct StreamMap {
69 int disabled; /* 1 is this mapping is disabled by a negative map */
73 int sync_stream_index;
74 char *linklabel; /* name of an output link, for mapping lavfi outputs */
77 /* select an input file for an output file */
78 typedef struct MetadataMap {
79 int file; // file index
80 char type; // type of metadata to copy -- (g)lobal, (s)tream, (c)hapter or (p)rogram
81 int index; // stream/chapter/program number
84 typedef struct OptionsContext {
87 /* input/output options */
91 SpecifierOpt *codec_names;
93 SpecifierOpt *audio_channels;
94 int nb_audio_channels;
95 SpecifierOpt *audio_sample_rate;
96 int nb_audio_sample_rate;
97 SpecifierOpt *frame_rates;
99 SpecifierOpt *frame_sizes;
101 SpecifierOpt *frame_pix_fmts;
102 int nb_frame_pix_fmts;
105 int64_t input_ts_offset;
110 SpecifierOpt *ts_scale;
112 SpecifierOpt *dump_attachment;
113 int nb_dump_attachment;
114 SpecifierOpt *hwaccels;
116 SpecifierOpt *hwaccel_devices;
117 int nb_hwaccel_devices;
118 SpecifierOpt *autorotate;
122 StreamMap *stream_maps;
124 /* first item specifies output metadata, second is input */
125 MetadataMap (*meta_data_maps)[2];
126 int nb_meta_data_maps;
127 int metadata_global_manual;
128 int metadata_streams_manual;
129 int metadata_chapters_manual;
130 const char **attachments;
133 int chapters_input_file;
135 int64_t recording_time;
136 uint64_t limit_filesize;
143 int subtitle_disable;
146 /* indexed by output file stream index */
150 SpecifierOpt *metadata;
152 SpecifierOpt *max_frames;
154 SpecifierOpt *bitstream_filters;
155 int nb_bitstream_filters;
156 SpecifierOpt *codec_tags;
158 SpecifierOpt *sample_fmts;
160 SpecifierOpt *qscale;
162 SpecifierOpt *forced_key_frames;
163 int nb_forced_key_frames;
164 SpecifierOpt *force_fps;
166 SpecifierOpt *frame_aspect_ratios;
167 int nb_frame_aspect_ratios;
168 SpecifierOpt *rc_overrides;
170 SpecifierOpt *intra_matrices;
171 int nb_intra_matrices;
172 SpecifierOpt *inter_matrices;
173 int nb_inter_matrices;
174 SpecifierOpt *top_field_first;
175 int nb_top_field_first;
176 SpecifierOpt *metadata_map;
178 SpecifierOpt *presets;
180 SpecifierOpt *copy_initial_nonkeyframes;
181 int nb_copy_initial_nonkeyframes;
182 SpecifierOpt *filters;
184 SpecifierOpt *filter_scripts;
185 int nb_filter_scripts;
188 SpecifierOpt *passlogfiles;
192 typedef struct InputFilter {
193 AVFilterContext *filter;
194 struct InputStream *ist;
195 struct FilterGraph *graph;
199 typedef struct OutputFilter {
200 AVFilterContext *filter;
201 struct OutputStream *ost;
202 struct FilterGraph *graph;
205 /* temporary storage until stream maps are processed */
206 AVFilterInOut *out_tmp;
207 enum AVMediaType type;
210 typedef struct FilterGraph {
212 const char *graph_desc;
214 AVFilterGraph *graph;
216 InputFilter **inputs;
218 OutputFilter **outputs;
222 typedef struct InputStream {
225 int discard; /* true if stream data should be discarded */
226 int decoding_needed; /* true if the packets must be decoded in 'raw_fifo' */
227 AVCodecContext *dec_ctx;
229 AVFrame *decoded_frame;
230 AVFrame *filter_frame; /* a ref of decoded_frame, to be sent to filters */
232 int64_t start; /* time when read started */
233 /* predicted dts of the next packet read for this stream or (when there are
234 * several frames in a packet) of the next frame in current packet */
236 /* dts of the last packet read for this stream */
238 int64_t min_pts; /* pts with the smallest value in a current stream */
239 int64_t max_pts; /* pts with the higher value in a current stream */
240 int64_t nb_samples; /* number of samples in the last decoded audio frame before looping */
241 PtsCorrectionContext pts_ctx;
243 int showed_multi_packet_warning;
244 AVDictionary *decoder_opts;
245 AVRational framerate; /* framerate forced with -r */
250 int resample_pix_fmt;
252 int resample_sample_fmt;
253 int resample_sample_rate;
254 int resample_channels;
255 uint64_t resample_channel_layout;
257 /* decoded data from this stream goes into all those filters
258 * currently video and audio only */
259 InputFilter **filters;
262 /* hwaccel options */
263 enum HWAccelID hwaccel_id;
264 char *hwaccel_device;
266 /* hwaccel context */
267 enum HWAccelID active_hwaccel_id;
269 void (*hwaccel_uninit)(AVCodecContext *s);
270 int (*hwaccel_get_buffer)(AVCodecContext *s, AVFrame *frame, int flags);
271 int (*hwaccel_retrieve_data)(AVCodecContext *s, AVFrame *frame);
272 enum AVPixelFormat hwaccel_pix_fmt;
273 enum AVPixelFormat hwaccel_retrieved_pix_fmt;
276 // combined size of all the packets read
278 /* number of packets successfully read for this stream */
280 // number of frames/samples retrieved from the decoder
281 uint64_t frames_decoded;
282 uint64_t samples_decoded;
285 typedef struct InputFile {
286 AVFormatContext *ctx;
287 int eof_reached; /* true if eof reached */
288 int eagain; /* true if last read attempt returned EAGAIN */
289 int ist_index; /* index of first stream in ist_table */
290 int loop; /* set number of times input stream should be looped */
291 int64_t duration; /* actual duration of the longest stream in a file
292 at the moment when looping happens */
293 AVRational time_base; /* time base of the duration */
295 int64_t start_time; /* user-specified start time in AV_TIME_BASE or AV_NOPTS_VALUE */
296 int64_t recording_time;
297 int nb_streams; /* number of stream that avconv is aware of; may be different
298 from ctx.nb_streams if new streams appear during av_read_frame() */
303 pthread_t thread; /* thread reading from this file */
304 int finished; /* the thread has exited */
305 int joined; /* the thread has been joined */
306 pthread_mutex_t fifo_lock; /* lock for access to fifo */
307 pthread_cond_t fifo_cond; /* the main thread will signal on this cond after reading from fifo */
308 AVFifoBuffer *fifo; /* demuxed packets are stored here; freed by the main thread */
312 typedef struct OutputStream {
313 int file_index; /* file index */
314 int index; /* stream index in the output file */
315 int source_index; /* InputStream index */
316 AVStream *st; /* stream in the output file */
317 int encoding_needed; /* true if encoding needed for this stream */
319 /* input pts and corresponding output pts
321 // double sync_ipts; /* dts from the AVPacket of the demuxer in second units */
322 struct InputStream *sync_ist; /* input stream to sync against */
323 int64_t sync_opts; /* output frame counter, could be changed to some true timestamp */ // FIXME look at frame_number
324 /* pts of the first frame encoded for this stream, used for limiting
327 /* dts of the last packet sent to the muxer */
328 int64_t last_mux_dts;
329 AVBitStreamFilterContext *bitstream_filters;
330 AVCodecContext *enc_ctx;
333 AVFrame *filtered_frame;
338 AVRational frame_rate;
342 float frame_aspect_ratio;
344 /* forced key frames */
345 int64_t *forced_kf_pts;
348 char *forced_keyframes;
350 char *logfile_prefix;
353 OutputFilter *filter;
357 AVDictionary *encoder_opts;
358 AVDictionary *resample_opts;
359 int finished; /* no more packets should be written for this stream */
361 const char *attachment_filename;
362 int copy_initial_nonkeyframes;
364 enum AVPixelFormat pix_fmts[2];
366 AVCodecParserContext *parser;
369 // combined size of all the packets written
371 // number of packets send to the muxer
372 uint64_t packets_written;
373 // number of frames/samples sent to the encoder
374 uint64_t frames_encoded;
375 uint64_t samples_encoded;
377 /* packet quality factor */
381 typedef struct OutputFile {
382 AVFormatContext *ctx;
384 int ost_index; /* index of the first stream in output_streams */
385 int64_t recording_time; /* desired length of the resulting file in microseconds */
386 int64_t start_time; /* start time in microseconds */
387 uint64_t limit_filesize;
392 extern InputStream **input_streams;
393 extern int nb_input_streams;
394 extern InputFile **input_files;
395 extern int nb_input_files;
397 extern OutputStream **output_streams;
398 extern int nb_output_streams;
399 extern OutputFile **output_files;
400 extern int nb_output_files;
402 extern FilterGraph **filtergraphs;
403 extern int nb_filtergraphs;
405 extern char *vstats_filename;
407 extern float audio_drift_threshold;
408 extern float dts_delta_threshold;
410 extern int audio_volume;
411 extern int audio_sync_method;
412 extern int video_sync_method;
413 extern int do_benchmark;
414 extern int do_deinterlace;
415 extern int do_hex_dump;
416 extern int do_pkt_dump;
419 extern int exit_on_error;
420 extern int print_stats;
423 extern const AVIOInterruptCB int_cb;
425 extern const OptionDef options[];
427 extern const HWAccel hwaccels[];
429 void reset_options(OptionsContext *o);
430 void show_usage(void);
432 void opt_output_file(void *optctx, const char *filename);
434 void assert_avoptions(AVDictionary *m);
436 int guess_input_channel_layout(InputStream *ist);
438 int configure_filtergraph(FilterGraph *fg);
439 int configure_output_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out);
440 int ist_in_filtergraph(FilterGraph *fg, InputStream *ist);
441 FilterGraph *init_simple_filtergraph(InputStream *ist, OutputStream *ost);
442 int init_complex_filtergraph(FilterGraph *fg);
444 int avconv_parse_options(int argc, char **argv);
446 int vdpau_init(AVCodecContext *s);
447 int dxva2_init(AVCodecContext *s);
448 int vda_init(AVCodecContext *s);
449 int qsv_init(AVCodecContext *s);
450 int qsv_transcode_init(OutputStream *ost);
452 #endif /* AVCONV_H */