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
59 typedef struct HWAccel {
61 int (*init)(AVCodecContext *s);
63 enum AVPixelFormat pix_fmt;
66 /* select an input stream for an output stream */
67 typedef struct StreamMap {
68 int disabled; /* 1 is this mapping is disabled by a negative map */
72 int sync_stream_index;
73 char *linklabel; /* name of an output link, for mapping lavfi outputs */
76 /* select an input file for an output file */
77 typedef struct MetadataMap {
78 int file; // file index
79 char type; // type of metadata to copy -- (g)lobal, (s)tream, (c)hapter or (p)rogram
80 int index; // stream/chapter/program number
83 typedef struct OptionsContext {
86 /* input/output options */
90 SpecifierOpt *codec_names;
92 SpecifierOpt *audio_channels;
93 int nb_audio_channels;
94 SpecifierOpt *audio_sample_rate;
95 int nb_audio_sample_rate;
96 SpecifierOpt *frame_rates;
98 SpecifierOpt *frame_sizes;
100 SpecifierOpt *frame_pix_fmts;
101 int nb_frame_pix_fmts;
104 int64_t input_ts_offset;
109 SpecifierOpt *ts_scale;
111 SpecifierOpt *dump_attachment;
112 int nb_dump_attachment;
113 SpecifierOpt *hwaccels;
115 SpecifierOpt *hwaccel_devices;
116 int nb_hwaccel_devices;
117 SpecifierOpt *autorotate;
121 StreamMap *stream_maps;
123 /* first item specifies output metadata, second is input */
124 MetadataMap (*meta_data_maps)[2];
125 int nb_meta_data_maps;
126 int metadata_global_manual;
127 int metadata_streams_manual;
128 int metadata_chapters_manual;
129 const char **attachments;
132 int chapters_input_file;
134 int64_t recording_time;
135 uint64_t limit_filesize;
142 int subtitle_disable;
145 /* indexed by output file stream index */
149 SpecifierOpt *metadata;
151 SpecifierOpt *max_frames;
153 SpecifierOpt *bitstream_filters;
154 int nb_bitstream_filters;
155 SpecifierOpt *codec_tags;
157 SpecifierOpt *sample_fmts;
159 SpecifierOpt *qscale;
161 SpecifierOpt *forced_key_frames;
162 int nb_forced_key_frames;
163 SpecifierOpt *force_fps;
165 SpecifierOpt *frame_aspect_ratios;
166 int nb_frame_aspect_ratios;
167 SpecifierOpt *rc_overrides;
169 SpecifierOpt *intra_matrices;
170 int nb_intra_matrices;
171 SpecifierOpt *inter_matrices;
172 int nb_inter_matrices;
173 SpecifierOpt *top_field_first;
174 int nb_top_field_first;
175 SpecifierOpt *metadata_map;
177 SpecifierOpt *presets;
179 SpecifierOpt *copy_initial_nonkeyframes;
180 int nb_copy_initial_nonkeyframes;
181 SpecifierOpt *filters;
183 SpecifierOpt *filter_scripts;
184 int nb_filter_scripts;
187 SpecifierOpt *passlogfiles;
191 typedef struct InputFilter {
192 AVFilterContext *filter;
193 struct InputStream *ist;
194 struct FilterGraph *graph;
198 typedef struct OutputFilter {
199 AVFilterContext *filter;
200 struct OutputStream *ost;
201 struct FilterGraph *graph;
204 /* temporary storage until stream maps are processed */
205 AVFilterInOut *out_tmp;
206 enum AVMediaType type;
209 typedef struct FilterGraph {
211 const char *graph_desc;
213 AVFilterGraph *graph;
215 InputFilter **inputs;
217 OutputFilter **outputs;
221 typedef struct InputStream {
224 int discard; /* true if stream data should be discarded */
225 int decoding_needed; /* true if the packets must be decoded in 'raw_fifo' */
226 AVCodecContext *dec_ctx;
228 AVFrame *decoded_frame;
229 AVFrame *filter_frame; /* a ref of decoded_frame, to be sent to filters */
231 int64_t start; /* time when read started */
232 /* predicted dts of the next packet read for this stream or (when there are
233 * several frames in a packet) of the next frame in current packet */
235 /* dts of the last packet read for this stream */
237 int64_t min_pts; /* pts with the smallest value in a current stream */
238 int64_t max_pts; /* pts with the higher value in a current stream */
239 int64_t nb_samples; /* number of samples in the last decoded audio frame before looping */
240 PtsCorrectionContext pts_ctx;
242 int showed_multi_packet_warning;
243 AVDictionary *decoder_opts;
244 AVRational framerate; /* framerate forced with -r */
249 int resample_pix_fmt;
251 int resample_sample_fmt;
252 int resample_sample_rate;
253 int resample_channels;
254 uint64_t resample_channel_layout;
256 /* decoded data from this stream goes into all those filters
257 * currently video and audio only */
258 InputFilter **filters;
261 /* hwaccel options */
262 enum HWAccelID hwaccel_id;
263 char *hwaccel_device;
265 /* hwaccel context */
266 enum HWAccelID active_hwaccel_id;
268 void (*hwaccel_uninit)(AVCodecContext *s);
269 int (*hwaccel_get_buffer)(AVCodecContext *s, AVFrame *frame, int flags);
270 int (*hwaccel_retrieve_data)(AVCodecContext *s, AVFrame *frame);
271 enum AVPixelFormat hwaccel_pix_fmt;
272 enum AVPixelFormat hwaccel_retrieved_pix_fmt;
275 // combined size of all the packets read
277 /* number of packets successfully read for this stream */
279 // number of frames/samples retrieved from the decoder
280 uint64_t frames_decoded;
281 uint64_t samples_decoded;
284 typedef struct InputFile {
285 AVFormatContext *ctx;
286 int eof_reached; /* true if eof reached */
287 int eagain; /* true if last read attempt returned EAGAIN */
288 int ist_index; /* index of first stream in ist_table */
289 int loop; /* set number of times input stream should be looped */
290 int64_t duration; /* actual duration of the longest stream in a file
291 at the moment when looping happens */
292 AVRational time_base; /* time base of the duration */
294 int64_t start_time; /* user-specified start time in AV_TIME_BASE or AV_NOPTS_VALUE */
295 int64_t recording_time;
296 int nb_streams; /* number of stream that avconv is aware of; may be different
297 from ctx.nb_streams if new streams appear during av_read_frame() */
302 pthread_t thread; /* thread reading from this file */
303 int finished; /* the thread has exited */
304 int joined; /* the thread has been joined */
305 pthread_mutex_t fifo_lock; /* lock for access to fifo */
306 pthread_cond_t fifo_cond; /* the main thread will signal on this cond after reading from fifo */
307 AVFifoBuffer *fifo; /* demuxed packets are stored here; freed by the main thread */
311 typedef struct OutputStream {
312 int file_index; /* file index */
313 int index; /* stream index in the output file */
314 int source_index; /* InputStream index */
315 AVStream *st; /* stream in the output file */
316 int encoding_needed; /* true if encoding needed for this stream */
318 /* input pts and corresponding output pts
320 // double sync_ipts; /* dts from the AVPacket of the demuxer in second units */
321 struct InputStream *sync_ist; /* input stream to sync against */
322 int64_t sync_opts; /* output frame counter, could be changed to some true timestamp */ // FIXME look at frame_number
323 /* pts of the first frame encoded for this stream, used for limiting
326 /* dts of the last packet sent to the muxer */
327 int64_t last_mux_dts;
328 AVBitStreamFilterContext *bitstream_filters;
329 AVCodecContext *enc_ctx;
332 AVFrame *filtered_frame;
335 AVRational frame_rate;
339 float frame_aspect_ratio;
341 /* forced key frames */
342 int64_t *forced_kf_pts;
345 char *forced_keyframes;
347 char *logfile_prefix;
350 OutputFilter *filter;
354 AVDictionary *encoder_opts;
355 AVDictionary *resample_opts;
356 int finished; /* no more packets should be written for this stream */
358 const char *attachment_filename;
359 int copy_initial_nonkeyframes;
361 enum AVPixelFormat pix_fmts[2];
363 AVCodecParserContext *parser;
366 // combined size of all the packets written
368 // number of packets send to the muxer
369 uint64_t packets_written;
370 // number of frames/samples sent to the encoder
371 uint64_t frames_encoded;
372 uint64_t samples_encoded;
374 /* packet quality factor */
378 typedef struct OutputFile {
379 AVFormatContext *ctx;
381 int ost_index; /* index of the first stream in output_streams */
382 int64_t recording_time; /* desired length of the resulting file in microseconds */
383 int64_t start_time; /* start time in microseconds */
384 uint64_t limit_filesize;
389 extern InputStream **input_streams;
390 extern int nb_input_streams;
391 extern InputFile **input_files;
392 extern int nb_input_files;
394 extern OutputStream **output_streams;
395 extern int nb_output_streams;
396 extern OutputFile **output_files;
397 extern int nb_output_files;
399 extern FilterGraph **filtergraphs;
400 extern int nb_filtergraphs;
402 extern char *vstats_filename;
404 extern float audio_drift_threshold;
405 extern float dts_delta_threshold;
407 extern int audio_volume;
408 extern int audio_sync_method;
409 extern int video_sync_method;
410 extern int do_benchmark;
411 extern int do_deinterlace;
412 extern int do_hex_dump;
413 extern int do_pkt_dump;
416 extern int exit_on_error;
417 extern int print_stats;
420 extern const AVIOInterruptCB int_cb;
422 extern const OptionDef options[];
424 extern const HWAccel hwaccels[];
426 void reset_options(OptionsContext *o);
427 void show_usage(void);
429 void opt_output_file(void *optctx, const char *filename);
431 void assert_avoptions(AVDictionary *m);
433 int guess_input_channel_layout(InputStream *ist);
435 int configure_filtergraph(FilterGraph *fg);
436 int configure_output_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out);
437 int ist_in_filtergraph(FilterGraph *fg, InputStream *ist);
438 FilterGraph *init_simple_filtergraph(InputStream *ist, OutputStream *ost);
439 int init_complex_filtergraph(FilterGraph *fg);
441 int avconv_parse_options(int argc, char **argv);
443 int vdpau_init(AVCodecContext *s);
444 int dxva2_init(AVCodecContext *s);
445 int vda_init(AVCodecContext *s);
447 #endif /* AVCONV_H */