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
58 typedef struct HWAccel {
60 int (*init)(AVCodecContext *s);
62 enum AVPixelFormat pix_fmt;
65 /* select an input stream for an output stream */
66 typedef struct StreamMap {
67 int disabled; /* 1 is this mapping is disabled by a negative map */
71 int sync_stream_index;
72 char *linklabel; /* name of an output link, for mapping lavfi outputs */
75 /* select an input file for an output file */
76 typedef struct MetadataMap {
77 int file; // file index
78 char type; // type of metadata to copy -- (g)lobal, (s)tream, (c)hapter or (p)rogram
79 int index; // stream/chapter/program number
82 typedef struct OptionsContext {
85 /* input/output options */
89 SpecifierOpt *codec_names;
91 SpecifierOpt *audio_channels;
92 int nb_audio_channels;
93 SpecifierOpt *audio_sample_rate;
94 int nb_audio_sample_rate;
95 SpecifierOpt *frame_rates;
97 SpecifierOpt *frame_sizes;
99 SpecifierOpt *frame_pix_fmts;
100 int nb_frame_pix_fmts;
103 int64_t input_ts_offset;
107 SpecifierOpt *ts_scale;
109 SpecifierOpt *dump_attachment;
110 int nb_dump_attachment;
111 SpecifierOpt *hwaccels;
113 SpecifierOpt *hwaccel_devices;
114 int nb_hwaccel_devices;
117 StreamMap *stream_maps;
119 /* first item specifies output metadata, second is input */
120 MetadataMap (*meta_data_maps)[2];
121 int nb_meta_data_maps;
122 int metadata_global_manual;
123 int metadata_streams_manual;
124 int metadata_chapters_manual;
125 const char **attachments;
128 int chapters_input_file;
130 int64_t recording_time;
131 uint64_t limit_filesize;
138 int subtitle_disable;
141 /* indexed by output file stream index */
145 SpecifierOpt *metadata;
147 SpecifierOpt *max_frames;
149 SpecifierOpt *bitstream_filters;
150 int nb_bitstream_filters;
151 SpecifierOpt *codec_tags;
153 SpecifierOpt *sample_fmts;
155 SpecifierOpt *qscale;
157 SpecifierOpt *forced_key_frames;
158 int nb_forced_key_frames;
159 SpecifierOpt *force_fps;
161 SpecifierOpt *frame_aspect_ratios;
162 int nb_frame_aspect_ratios;
163 SpecifierOpt *rc_overrides;
165 SpecifierOpt *intra_matrices;
166 int nb_intra_matrices;
167 SpecifierOpt *inter_matrices;
168 int nb_inter_matrices;
169 SpecifierOpt *top_field_first;
170 int nb_top_field_first;
171 SpecifierOpt *metadata_map;
173 SpecifierOpt *presets;
175 SpecifierOpt *copy_initial_nonkeyframes;
176 int nb_copy_initial_nonkeyframes;
177 SpecifierOpt *filters;
179 SpecifierOpt *filter_scripts;
180 int nb_filter_scripts;
183 SpecifierOpt *passlogfiles;
187 typedef struct InputFilter {
188 AVFilterContext *filter;
189 struct InputStream *ist;
190 struct FilterGraph *graph;
194 typedef struct OutputFilter {
195 AVFilterContext *filter;
196 struct OutputStream *ost;
197 struct FilterGraph *graph;
200 /* temporary storage until stream maps are processed */
201 AVFilterInOut *out_tmp;
204 typedef struct FilterGraph {
206 const char *graph_desc;
208 AVFilterGraph *graph;
210 InputFilter **inputs;
212 OutputFilter **outputs;
216 typedef struct InputStream {
219 int discard; /* true if stream data should be discarded */
220 int decoding_needed; /* true if the packets must be decoded in 'raw_fifo' */
222 AVFrame *decoded_frame;
223 AVFrame *filter_frame; /* a ref of decoded_frame, to be sent to filters */
225 int64_t start; /* time when read started */
226 /* predicted dts of the next packet read for this stream or (when there are
227 * several frames in a packet) of the next frame in current packet */
229 /* dts of the last packet read for this stream */
231 PtsCorrectionContext pts_ctx;
233 int showed_multi_packet_warning;
235 AVRational framerate; /* framerate forced with -r */
239 int resample_pix_fmt;
241 int resample_sample_fmt;
242 int resample_sample_rate;
243 int resample_channels;
244 uint64_t resample_channel_layout;
246 /* decoded data from this stream goes into all those filters
247 * currently video and audio only */
248 InputFilter **filters;
251 /* hwaccel options */
252 enum HWAccelID hwaccel_id;
253 char *hwaccel_device;
255 /* hwaccel context */
256 enum HWAccelID active_hwaccel_id;
258 void (*hwaccel_uninit)(AVCodecContext *s);
259 int (*hwaccel_get_buffer)(AVCodecContext *s, AVFrame *frame, int flags);
260 int (*hwaccel_retrieve_data)(AVCodecContext *s, AVFrame *frame);
261 enum AVPixelFormat hwaccel_pix_fmt;
262 enum AVPixelFormat hwaccel_retrieved_pix_fmt;
265 // combined size of all the packets read
267 /* number of packets successfully read for this stream */
269 // number of frames/samples retrieved from the decoder
270 uint64_t frames_decoded;
271 uint64_t samples_decoded;
274 typedef struct InputFile {
275 AVFormatContext *ctx;
276 int eof_reached; /* true if eof reached */
277 int eagain; /* true if last read attempt returned EAGAIN */
278 int ist_index; /* index of first stream in ist_table */
280 int64_t start_time; /* user-specified start time in AV_TIME_BASE or AV_NOPTS_VALUE */
281 int64_t recording_time;
282 int nb_streams; /* number of stream that avconv is aware of; may be different
283 from ctx.nb_streams if new streams appear during av_read_frame() */
288 pthread_t thread; /* thread reading from this file */
289 int finished; /* the thread has exited */
290 int joined; /* the thread has been joined */
291 pthread_mutex_t fifo_lock; /* lock for access to fifo */
292 pthread_cond_t fifo_cond; /* the main thread will signal on this cond after reading from fifo */
293 AVFifoBuffer *fifo; /* demuxed packets are stored here; freed by the main thread */
297 typedef struct OutputStream {
298 int file_index; /* file index */
299 int index; /* stream index in the output file */
300 int source_index; /* InputStream index */
301 AVStream *st; /* stream in the output file */
302 int encoding_needed; /* true if encoding needed for this stream */
304 /* input pts and corresponding output pts
306 // double sync_ipts; /* dts from the AVPacket of the demuxer in second units */
307 struct InputStream *sync_ist; /* input stream to sync against */
308 int64_t sync_opts; /* output frame counter, could be changed to some true timestamp */ // FIXME look at frame_number
309 /* pts of the first frame encoded for this stream, used for limiting
312 /* dts of the last packet sent to the muxer */
313 int64_t last_mux_dts;
314 AVBitStreamFilterContext *bitstream_filters;
317 AVFrame *filtered_frame;
320 AVRational frame_rate;
324 float frame_aspect_ratio;
326 /* forced key frames */
327 int64_t *forced_kf_pts;
330 char *forced_keyframes;
332 char *logfile_prefix;
335 OutputFilter *filter;
340 AVDictionary *resample_opts;
341 int finished; /* no more packets should be written for this stream */
343 const char *attachment_filename;
344 int copy_initial_nonkeyframes;
346 enum AVPixelFormat pix_fmts[2];
348 AVCodecParserContext *parser;
351 // combined size of all the packets written
353 // number of packets send to the muxer
354 uint64_t packets_written;
355 // number of frames/samples sent to the encoder
356 uint64_t frames_encoded;
357 uint64_t samples_encoded;
360 typedef struct OutputFile {
361 AVFormatContext *ctx;
363 int ost_index; /* index of the first stream in output_streams */
364 int64_t recording_time; /* desired length of the resulting file in microseconds */
365 int64_t start_time; /* start time in microseconds */
366 uint64_t limit_filesize;
371 extern InputStream **input_streams;
372 extern int nb_input_streams;
373 extern InputFile **input_files;
374 extern int nb_input_files;
376 extern OutputStream **output_streams;
377 extern int nb_output_streams;
378 extern OutputFile **output_files;
379 extern int nb_output_files;
381 extern FilterGraph **filtergraphs;
382 extern int nb_filtergraphs;
384 extern char *vstats_filename;
386 extern float audio_drift_threshold;
387 extern float dts_delta_threshold;
389 extern int audio_volume;
390 extern int audio_sync_method;
391 extern int video_sync_method;
392 extern int do_benchmark;
393 extern int do_deinterlace;
394 extern int do_hex_dump;
395 extern int do_pkt_dump;
398 extern int exit_on_error;
399 extern int print_stats;
402 extern const AVIOInterruptCB int_cb;
404 extern const OptionDef options[];
406 extern const HWAccel hwaccels[];
408 void reset_options(OptionsContext *o);
409 void show_usage(void);
411 void opt_output_file(void *optctx, const char *filename);
413 void assert_avoptions(AVDictionary *m);
415 int guess_input_channel_layout(InputStream *ist);
417 int configure_filtergraph(FilterGraph *fg);
418 int configure_output_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out);
419 int ist_in_filtergraph(FilterGraph *fg, InputStream *ist);
420 FilterGraph *init_simple_filtergraph(InputStream *ist, OutputStream *ost);
422 int avconv_parse_options(int argc, char **argv);
424 int vdpau_init(AVCodecContext *s);
425 int dxva2_init(AVCodecContext *s);
427 #endif /* AVCONV_H */