avconv: init filtergraphs only after we have a frame on each input
[libav.git] / avconv.h
1 /*
2 * This file is part of Libav.
3 *
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.
8 *
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.
13 *
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
17 */
18
19 #ifndef AVCONV_H
20 #define AVCONV_H
21
22 #include "config.h"
23
24 #include <stdint.h>
25 #include <stdio.h>
26
27 #if HAVE_PTHREADS
28 #include <pthread.h>
29 #endif
30
31 #include "cmdutils.h"
32
33 #include "libavformat/avformat.h"
34 #include "libavformat/avio.h"
35
36 #include "libavcodec/avcodec.h"
37
38 #include "libavfilter/avfilter.h"
39
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"
45
46 #define VSYNC_AUTO -1
47 #define VSYNC_PASSTHROUGH 0
48 #define VSYNC_CFR 1
49 #define VSYNC_VFR 2
50
51 enum HWAccelID {
52 HWACCEL_NONE = 0,
53 HWACCEL_AUTO,
54 HWACCEL_VDPAU,
55 HWACCEL_DXVA2,
56 HWACCEL_VDA,
57 HWACCEL_QSV,
58 HWACCEL_VAAPI,
59 };
60
61 typedef struct HWAccel {
62 const char *name;
63 int (*init)(AVCodecContext *s);
64 enum HWAccelID id;
65 enum AVPixelFormat pix_fmt;
66 } HWAccel;
67
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 */
71 int file_index;
72 int stream_index;
73 int sync_file_index;
74 int sync_stream_index;
75 char *linklabel; /* name of an output link, for mapping lavfi outputs */
76 } StreamMap;
77
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
83 } MetadataMap;
84
85 typedef struct OptionsContext {
86 OptionGroup *g;
87
88 /* input/output options */
89 int64_t start_time;
90 const char *format;
91
92 SpecifierOpt *codec_names;
93 int nb_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;
99 int nb_frame_rates;
100 SpecifierOpt *frame_sizes;
101 int nb_frame_sizes;
102 SpecifierOpt *frame_pix_fmts;
103 int nb_frame_pix_fmts;
104
105 /* input options */
106 int64_t input_ts_offset;
107 int loop;
108 int rate_emu;
109 int accurate_seek;
110
111 SpecifierOpt *ts_scale;
112 int nb_ts_scale;
113 SpecifierOpt *dump_attachment;
114 int nb_dump_attachment;
115 SpecifierOpt *hwaccels;
116 int nb_hwaccels;
117 SpecifierOpt *hwaccel_devices;
118 int nb_hwaccel_devices;
119 SpecifierOpt *hwaccel_output_formats;
120 int nb_hwaccel_output_formats;
121 SpecifierOpt *autorotate;
122 int nb_autorotate;
123
124 /* output options */
125 StreamMap *stream_maps;
126 int nb_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;
134 int nb_attachments;
135
136 int chapters_input_file;
137
138 int64_t recording_time;
139 uint64_t limit_filesize;
140 float mux_preload;
141 float mux_max_delay;
142 int shortest;
143
144 int video_disable;
145 int audio_disable;
146 int subtitle_disable;
147 int data_disable;
148
149 /* indexed by output file stream index */
150 int *streamid_map;
151 int nb_streamid_map;
152
153 SpecifierOpt *metadata;
154 int nb_metadata;
155 SpecifierOpt *max_frames;
156 int nb_max_frames;
157 SpecifierOpt *bitstream_filters;
158 int nb_bitstream_filters;
159 SpecifierOpt *codec_tags;
160 int nb_codec_tags;
161 SpecifierOpt *sample_fmts;
162 int nb_sample_fmts;
163 SpecifierOpt *qscale;
164 int nb_qscale;
165 SpecifierOpt *forced_key_frames;
166 int nb_forced_key_frames;
167 SpecifierOpt *force_fps;
168 int nb_force_fps;
169 SpecifierOpt *frame_aspect_ratios;
170 int nb_frame_aspect_ratios;
171 SpecifierOpt *rc_overrides;
172 int nb_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;
180 int nb_metadata_map;
181 SpecifierOpt *presets;
182 int nb_presets;
183 SpecifierOpt *copy_initial_nonkeyframes;
184 int nb_copy_initial_nonkeyframes;
185 SpecifierOpt *filters;
186 int nb_filters;
187 SpecifierOpt *filter_scripts;
188 int nb_filter_scripts;
189 SpecifierOpt *pass;
190 int nb_pass;
191 SpecifierOpt *passlogfiles;
192 int nb_passlogfiles;
193 SpecifierOpt *max_muxing_queue_size;
194 int nb_max_muxing_queue_size;
195 } OptionsContext;
196
197 typedef struct InputFilter {
198 AVFilterContext *filter;
199 struct InputStream *ist;
200 struct FilterGraph *graph;
201 uint8_t *name;
202
203 AVFifoBuffer *frame_queue;
204
205 // parameters configured for this input
206 int format;
207
208 int width, height;
209 AVRational sample_aspect_ratio;
210
211 int sample_rate;
212 uint64_t channel_layout;
213
214 AVBufferRef *hw_frames_ctx;
215 } InputFilter;
216
217 typedef struct OutputFilter {
218 AVFilterContext *filter;
219 struct OutputStream *ost;
220 struct FilterGraph *graph;
221 uint8_t *name;
222
223 /* temporary storage until stream maps are processed */
224 AVFilterInOut *out_tmp;
225 enum AVMediaType type;
226
227 /* desired output stream properties */
228 int width, height;
229 AVRational frame_rate;
230 int format;
231 int sample_rate;
232 uint64_t channel_layout;
233
234 // those are only set if no format is specified and the encoder gives us multiple options
235 int *formats;
236 uint64_t *channel_layouts;
237 int *sample_rates;
238 } OutputFilter;
239
240 typedef struct FilterGraph {
241 int index;
242 const char *graph_desc;
243
244 AVFilterGraph *graph;
245
246 InputFilter **inputs;
247 int nb_inputs;
248 OutputFilter **outputs;
249 int nb_outputs;
250 } FilterGraph;
251
252 typedef struct InputStream {
253 int file_index;
254 AVStream *st;
255 int discard; /* true if stream data should be discarded */
256 int decoding_needed; /* true if the packets must be decoded in 'raw_fifo' */
257 AVCodecContext *dec_ctx;
258 AVCodec *dec;
259 AVFrame *decoded_frame;
260 AVFrame *filter_frame; /* a ref of decoded_frame, to be sent to filters */
261
262 int64_t start; /* time when read started */
263 /* predicted dts of the next packet read for this stream or (when there are
264 * several frames in a packet) of the next frame in current packet */
265 int64_t next_dts;
266 /* dts of the last packet read for this stream */
267 int64_t last_dts;
268 int64_t min_pts; /* pts with the smallest value in a current stream */
269 int64_t max_pts; /* pts with the higher value in a current stream */
270 int64_t nb_samples; /* number of samples in the last decoded audio frame before looping */
271 PtsCorrectionContext pts_ctx;
272 double ts_scale;
273 AVDictionary *decoder_opts;
274 AVRational framerate; /* framerate forced with -r */
275
276 int autorotate;
277
278 /* decoded data from this stream goes into all those filters
279 * currently video and audio only */
280 InputFilter **filters;
281 int nb_filters;
282
283 /* hwaccel options */
284 enum HWAccelID hwaccel_id;
285 char *hwaccel_device;
286 enum AVPixelFormat hwaccel_output_format;
287
288 /* hwaccel context */
289 enum HWAccelID active_hwaccel_id;
290 void *hwaccel_ctx;
291 void (*hwaccel_uninit)(AVCodecContext *s);
292 int (*hwaccel_get_buffer)(AVCodecContext *s, AVFrame *frame, int flags);
293 int (*hwaccel_retrieve_data)(AVCodecContext *s, AVFrame *frame);
294 enum AVPixelFormat hwaccel_pix_fmt;
295 enum AVPixelFormat hwaccel_retrieved_pix_fmt;
296 AVBufferRef *hw_frames_ctx;
297
298 /* stats */
299 // combined size of all the packets read
300 uint64_t data_size;
301 /* number of packets successfully read for this stream */
302 uint64_t nb_packets;
303 // number of frames/samples retrieved from the decoder
304 uint64_t frames_decoded;
305 uint64_t samples_decoded;
306 } InputStream;
307
308 typedef struct InputFile {
309 AVFormatContext *ctx;
310 int eof_reached; /* true if eof reached */
311 int eagain; /* true if last read attempt returned EAGAIN */
312 int ist_index; /* index of first stream in ist_table */
313 int loop; /* set number of times input stream should be looped */
314 int64_t duration; /* actual duration of the longest stream in a file
315 at the moment when looping happens */
316 AVRational time_base; /* time base of the duration */
317 int64_t ts_offset;
318 int64_t start_time; /* user-specified start time in AV_TIME_BASE or AV_NOPTS_VALUE */
319 int64_t recording_time;
320 int nb_streams; /* number of stream that avconv is aware of; may be different
321 from ctx.nb_streams if new streams appear during av_read_frame() */
322 int rate_emu;
323 int accurate_seek;
324
325 #if HAVE_PTHREADS
326 pthread_t thread; /* thread reading from this file */
327 int finished; /* the thread has exited */
328 int joined; /* the thread has been joined */
329 pthread_mutex_t fifo_lock; /* lock for access to fifo */
330 pthread_cond_t fifo_cond; /* the main thread will signal on this cond after reading from fifo */
331 AVFifoBuffer *fifo; /* demuxed packets are stored here; freed by the main thread */
332 #endif
333 } InputFile;
334
335 typedef struct OutputStream {
336 int file_index; /* file index */
337 int index; /* stream index in the output file */
338 int source_index; /* InputStream index */
339 AVStream *st; /* stream in the output file */
340 int encoding_needed; /* true if encoding needed for this stream */
341 int frame_number;
342 /* input pts and corresponding output pts
343 for A/V sync */
344 // double sync_ipts; /* dts from the AVPacket of the demuxer in second units */
345 struct InputStream *sync_ist; /* input stream to sync against */
346 int64_t sync_opts; /* output frame counter, could be changed to some true timestamp */ // FIXME look at frame_number
347 /* pts of the first frame encoded for this stream, used for limiting
348 * recording time */
349 int64_t first_pts;
350 /* dts of the last packet sent to the muxer */
351 int64_t last_mux_dts;
352 // the timebase of the packets sent to the muxer
353 AVRational mux_timebase;
354
355 int nb_bitstream_filters;
356 const AVBitStreamFilter **bitstream_filters;
357 AVBSFContext **bsf_ctx;
358
359 AVCodecContext *enc_ctx;
360 AVCodec *enc;
361 int64_t max_frames;
362 AVFrame *filtered_frame;
363
364 void *hwaccel_ctx;
365
366 /* video only */
367 AVRational frame_rate;
368 int force_fps;
369 int top_field_first;
370
371 float frame_aspect_ratio;
372
373 /* forced key frames */
374 int64_t *forced_kf_pts;
375 int forced_kf_count;
376 int forced_kf_index;
377 char *forced_keyframes;
378
379 char *logfile_prefix;
380 FILE *logfile;
381
382 OutputFilter *filter;
383 char *avfilter;
384
385 int64_t sws_flags;
386 AVDictionary *encoder_opts;
387 AVDictionary *resample_opts;
388 int finished; /* no more packets should be written for this stream */
389 int stream_copy;
390
391 // init_output_stream() has been called for this stream
392 // The encoder and the bistream filters have been initialized and the stream
393 // parameters are set in the AVStream.
394 int initialized;
395
396 const char *attachment_filename;
397 int copy_initial_nonkeyframes;
398
399 enum AVPixelFormat pix_fmts[2];
400
401 AVCodecParserContext *parser;
402 AVCodecContext *parser_avctx;
403
404 /* stats */
405 // combined size of all the packets written
406 uint64_t data_size;
407 // number of packets send to the muxer
408 uint64_t packets_written;
409 // number of frames/samples sent to the encoder
410 uint64_t frames_encoded;
411 uint64_t samples_encoded;
412
413 /* packet quality factor */
414 int quality;
415
416 int max_muxing_queue_size;
417
418 /* the packets are buffered here until the muxer is ready to be initialized */
419 AVFifoBuffer *muxing_queue;
420 } OutputStream;
421
422 typedef struct OutputFile {
423 AVFormatContext *ctx;
424 AVDictionary *opts;
425 int ost_index; /* index of the first stream in output_streams */
426 int64_t recording_time; /* desired length of the resulting file in microseconds */
427 int64_t start_time; /* start time in microseconds */
428 uint64_t limit_filesize;
429
430 int shortest;
431
432 int header_written;
433 } OutputFile;
434
435 extern InputStream **input_streams;
436 extern int nb_input_streams;
437 extern InputFile **input_files;
438 extern int nb_input_files;
439
440 extern OutputStream **output_streams;
441 extern int nb_output_streams;
442 extern OutputFile **output_files;
443 extern int nb_output_files;
444
445 extern FilterGraph **filtergraphs;
446 extern int nb_filtergraphs;
447
448 extern char *vstats_filename;
449
450 extern float audio_drift_threshold;
451 extern float dts_delta_threshold;
452
453 extern int audio_volume;
454 extern int audio_sync_method;
455 extern int video_sync_method;
456 extern int do_benchmark;
457 extern int do_deinterlace;
458 extern int do_hex_dump;
459 extern int do_pkt_dump;
460 extern int copy_ts;
461 extern int copy_tb;
462 extern int exit_on_error;
463 extern int print_stats;
464 extern int qp_hist;
465
466 extern const AVIOInterruptCB int_cb;
467
468 extern const OptionDef options[];
469
470 extern const HWAccel hwaccels[];
471 extern int hwaccel_lax_profile_check;
472 extern AVBufferRef *hw_device_ctx;
473
474 void reset_options(OptionsContext *o);
475 void show_usage(void);
476
477 void opt_output_file(void *optctx, const char *filename);
478
479 void assert_avoptions(AVDictionary *m);
480
481 int guess_input_channel_layout(InputStream *ist);
482
483 int configure_filtergraph(FilterGraph *fg);
484 int configure_output_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out);
485 int ist_in_filtergraph(FilterGraph *fg, InputStream *ist);
486 int filtergraph_is_simple(FilterGraph *fg);
487 int init_simple_filtergraph(InputStream *ist, OutputStream *ost);
488 int init_complex_filtergraph(FilterGraph *fg);
489
490 int ifilter_parameters_from_frame(InputFilter *ifilter, const AVFrame *frame);
491
492 int avconv_parse_options(int argc, char **argv);
493
494 int vdpau_init(AVCodecContext *s);
495 int dxva2_init(AVCodecContext *s);
496 int vda_init(AVCodecContext *s);
497 int qsv_init(AVCodecContext *s);
498 int qsv_transcode_init(OutputStream *ost);
499 int vaapi_decode_init(AVCodecContext *avctx);
500 int vaapi_device_init(const char *device);
501
502 #endif /* AVCONV_H */