avconv: factor out initializing stream parameters for encoding
[libav.git] / avconv.h
CommitLineData
f5e66827
AK
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"
f5e66827
AK
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
07fd0a22
AK
51enum HWAccelID {
52 HWACCEL_NONE = 0,
53 HWACCEL_AUTO,
7671dd7c 54 HWACCEL_VDPAU,
35177ba7 55 HWACCEL_DXVA2,
1839fafa 56 HWACCEL_VDA,
fb472e1a 57 HWACCEL_QSV,
5d273d3e 58 HWACCEL_VAAPI,
07fd0a22
AK
59};
60
61typedef struct HWAccel {
62 const char *name;
63 int (*init)(AVCodecContext *s);
64 enum HWAccelID id;
65 enum AVPixelFormat pix_fmt;
66} HWAccel;
67
f5e66827
AK
68/* select an input stream for an output stream */
69typedef struct StreamMap {
c1ef30a6 70 int disabled; /* 1 is this mapping is disabled by a negative map */
f5e66827
AK
71 int file_index;
72 int stream_index;
73 int sync_file_index;
74 int sync_stream_index;
c1ef30a6 75 char *linklabel; /* name of an output link, for mapping lavfi outputs */
f5e66827
AK
76} StreamMap;
77
c1ef30a6 78/* select an input file for an output file */
f5e66827 79typedef struct MetadataMap {
c1ef30a6
DB
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
f5e66827
AK
83} MetadataMap;
84
85typedef struct OptionsContext {
77bd1bc7
AK
86 OptionGroup *g;
87
f5e66827
AK
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;
16b0c929 107 int loop;
f5e66827 108 int rate_emu;
811bd078 109 int accurate_seek;
f5e66827
AK
110
111 SpecifierOpt *ts_scale;
112 int nb_ts_scale;
113 SpecifierOpt *dump_attachment;
114 int nb_dump_attachment;
07fd0a22
AK
115 SpecifierOpt *hwaccels;
116 int nb_hwaccels;
117 SpecifierOpt *hwaccel_devices;
118 int nb_hwaccel_devices;
5d273d3e
MT
119 SpecifierOpt *hwaccel_output_formats;
120 int nb_hwaccel_output_formats;
16302246
MS
121 SpecifierOpt *autorotate;
122 int nb_autorotate;
f5e66827
AK
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;
3c0df905 142 int shortest;
f5e66827
AK
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;
a4208b9b
AK
187 SpecifierOpt *filter_scripts;
188 int nb_filter_scripts;
038c0b1e
AK
189 SpecifierOpt *pass;
190 int nb_pass;
bbcedade
AK
191 SpecifierOpt *passlogfiles;
192 int nb_passlogfiles;
398f015f
AK
193 SpecifierOpt *max_muxing_queue_size;
194 int nb_max_muxing_queue_size;
f5e66827
AK
195} OptionsContext;
196
197typedef struct InputFilter {
198 AVFilterContext *filter;
199 struct InputStream *ist;
200 struct FilterGraph *graph;
201 uint8_t *name;
722ec3eb
AK
202
203 // parameters configured for this input
204 int format;
205
206 int width, height;
207 AVRational sample_aspect_ratio;
208
209 int sample_rate;
210 uint64_t channel_layout;
211
212 AVBufferRef *hw_frames_ctx;
f5e66827
AK
213} InputFilter;
214
215typedef struct OutputFilter {
216 AVFilterContext *filter;
217 struct OutputStream *ost;
218 struct FilterGraph *graph;
219 uint8_t *name;
220
221 /* temporary storage until stream maps are processed */
222 AVFilterInOut *out_tmp;
6d592fbd 223 enum AVMediaType type;
f5e66827
AK
224} OutputFilter;
225
226typedef struct FilterGraph {
227 int index;
228 const char *graph_desc;
229
230 AVFilterGraph *graph;
231
232 InputFilter **inputs;
233 int nb_inputs;
234 OutputFilter **outputs;
235 int nb_outputs;
236} FilterGraph;
237
238typedef struct InputStream {
239 int file_index;
240 AVStream *st;
241 int discard; /* true if stream data should be discarded */
242 int decoding_needed; /* true if the packets must be decoded in 'raw_fifo' */
41776ba9 243 AVCodecContext *dec_ctx;
f5e66827
AK
244 AVCodec *dec;
245 AVFrame *decoded_frame;
9b2dc295 246 AVFrame *filter_frame; /* a ref of decoded_frame, to be sent to filters */
f5e66827
AK
247
248 int64_t start; /* time when read started */
249 /* predicted dts of the next packet read for this stream or (when there are
250 * several frames in a packet) of the next frame in current packet */
251 int64_t next_dts;
252 /* dts of the last packet read for this stream */
253 int64_t last_dts;
16b0c929
AH
254 int64_t min_pts; /* pts with the smallest value in a current stream */
255 int64_t max_pts; /* pts with the higher value in a current stream */
256 int64_t nb_samples; /* number of samples in the last decoded audio frame before looping */
f5e66827
AK
257 PtsCorrectionContext pts_ctx;
258 double ts_scale;
f3a6ad22 259 AVDictionary *decoder_opts;
f5e66827
AK
260 AVRational framerate; /* framerate forced with -r */
261
16302246 262 int autorotate;
f5e66827
AK
263 int resample_height;
264 int resample_width;
265 int resample_pix_fmt;
266
267 int resample_sample_fmt;
268 int resample_sample_rate;
269 int resample_channels;
270 uint64_t resample_channel_layout;
271
f5e66827
AK
272 /* decoded data from this stream goes into all those filters
273 * currently video and audio only */
274 InputFilter **filters;
275 int nb_filters;
07fd0a22
AK
276
277 /* hwaccel options */
278 enum HWAccelID hwaccel_id;
279 char *hwaccel_device;
5d273d3e 280 enum AVPixelFormat hwaccel_output_format;
07fd0a22
AK
281
282 /* hwaccel context */
283 enum HWAccelID active_hwaccel_id;
284 void *hwaccel_ctx;
285 void (*hwaccel_uninit)(AVCodecContext *s);
286 int (*hwaccel_get_buffer)(AVCodecContext *s, AVFrame *frame, int flags);
287 int (*hwaccel_retrieve_data)(AVCodecContext *s, AVFrame *frame);
288 enum AVPixelFormat hwaccel_pix_fmt;
289 enum AVPixelFormat hwaccel_retrieved_pix_fmt;
5d273d3e 290 AVBufferRef *hw_frames_ctx;
47543450
AK
291
292 /* stats */
293 // combined size of all the packets read
294 uint64_t data_size;
295 /* number of packets successfully read for this stream */
296 uint64_t nb_packets;
297 // number of frames/samples retrieved from the decoder
298 uint64_t frames_decoded;
299 uint64_t samples_decoded;
f5e66827
AK
300} InputStream;
301
302typedef struct InputFile {
303 AVFormatContext *ctx;
304 int eof_reached; /* true if eof reached */
0b26ef42 305 int eagain; /* true if last read attempt returned EAGAIN */
f5e66827 306 int ist_index; /* index of first stream in ist_table */
16b0c929
AH
307 int loop; /* set number of times input stream should be looped */
308 int64_t duration; /* actual duration of the longest stream in a file
309 at the moment when looping happens */
310 AVRational time_base; /* time base of the duration */
f5e66827 311 int64_t ts_offset;
811bd078 312 int64_t start_time; /* user-specified start time in AV_TIME_BASE or AV_NOPTS_VALUE */
488a0fa6 313 int64_t recording_time;
f5e66827
AK
314 int nb_streams; /* number of stream that avconv is aware of; may be different
315 from ctx.nb_streams if new streams appear during av_read_frame() */
316 int rate_emu;
811bd078 317 int accurate_seek;
f5e66827
AK
318
319#if HAVE_PTHREADS
320 pthread_t thread; /* thread reading from this file */
321 int finished; /* the thread has exited */
322 int joined; /* the thread has been joined */
323 pthread_mutex_t fifo_lock; /* lock for access to fifo */
324 pthread_cond_t fifo_cond; /* the main thread will signal on this cond after reading from fifo */
325 AVFifoBuffer *fifo; /* demuxed packets are stored here; freed by the main thread */
326#endif
327} InputFile;
328
329typedef struct OutputStream {
330 int file_index; /* file index */
331 int index; /* stream index in the output file */
332 int source_index; /* InputStream index */
333 AVStream *st; /* stream in the output file */
334 int encoding_needed; /* true if encoding needed for this stream */
335 int frame_number;
336 /* input pts and corresponding output pts
337 for A/V sync */
338 // double sync_ipts; /* dts from the AVPacket of the demuxer in second units */
339 struct InputStream *sync_ist; /* input stream to sync against */
340 int64_t sync_opts; /* output frame counter, could be changed to some true timestamp */ // FIXME look at frame_number
341 /* pts of the first frame encoded for this stream, used for limiting
342 * recording time */
343 int64_t first_pts;
76d23f40
AK
344 /* dts of the last packet sent to the muxer */
345 int64_t last_mux_dts;
4426540f
AK
346
347 int nb_bitstream_filters;
348 const AVBitStreamFilter **bitstream_filters;
349 AVBSFContext **bsf_ctx;
350
38313626 351 AVCodecContext *enc_ctx;
f5e66827
AK
352 AVCodec *enc;
353 int64_t max_frames;
354 AVFrame *filtered_frame;
355
fb472e1a
AK
356 void *hwaccel_ctx;
357
f5e66827
AK
358 /* video only */
359 AVRational frame_rate;
360 int force_fps;
361 int top_field_first;
362
363 float frame_aspect_ratio;
f5e66827
AK
364
365 /* forced key frames */
366 int64_t *forced_kf_pts;
367 int forced_kf_count;
368 int forced_kf_index;
369 char *forced_keyframes;
370
bbcedade 371 char *logfile_prefix;
f5e66827
AK
372 FILE *logfile;
373
374 OutputFilter *filter;
375 char *avfilter;
376
377 int64_t sws_flags;
9580818c 378 AVDictionary *encoder_opts;
5c7db097 379 AVDictionary *resample_opts;
57d24225 380 int finished; /* no more packets should be written for this stream */
f5e66827 381 int stream_copy;
1c169782
AK
382
383 // init_output_stream() has been called for this stream
384 // The encoder and the bistream filters have been initialized and the stream
385 // parameters are set in the AVStream.
386 int initialized;
387
f5e66827
AK
388 const char *attachment_filename;
389 int copy_initial_nonkeyframes;
390
716d413c 391 enum AVPixelFormat pix_fmts[2];
c872d310
AK
392
393 AVCodecParserContext *parser;
35c85806 394 AVCodecContext *parser_avctx;
baeb59d8
AK
395
396 /* stats */
397 // combined size of all the packets written
398 uint64_t data_size;
47543450
AK
399 // number of packets send to the muxer
400 uint64_t packets_written;
401 // number of frames/samples sent to the encoder
402 uint64_t frames_encoded;
403 uint64_t samples_encoded;
5d3addb9
VG
404
405 /* packet quality factor */
406 int quality;
398f015f
AK
407
408 int max_muxing_queue_size;
409
410 /* the packets are buffered here until the muxer is ready to be initialized */
411 AVFifoBuffer *muxing_queue;
f5e66827
AK
412} OutputStream;
413
414typedef struct OutputFile {
415 AVFormatContext *ctx;
416 AVDictionary *opts;
417 int ost_index; /* index of the first stream in output_streams */
418 int64_t recording_time; /* desired length of the resulting file in microseconds */
419 int64_t start_time; /* start time in microseconds */
420 uint64_t limit_filesize;
3c0df905
AK
421
422 int shortest;
1c169782
AK
423
424 int header_written;
f5e66827
AK
425} OutputFile;
426
427extern InputStream **input_streams;
428extern int nb_input_streams;
429extern InputFile **input_files;
430extern int nb_input_files;
431
432extern OutputStream **output_streams;
433extern int nb_output_streams;
434extern OutputFile **output_files;
435extern int nb_output_files;
436
437extern FilterGraph **filtergraphs;
438extern int nb_filtergraphs;
439
f5e66827
AK
440extern char *vstats_filename;
441
442extern float audio_drift_threshold;
443extern float dts_delta_threshold;
444
445extern int audio_volume;
446extern int audio_sync_method;
447extern int video_sync_method;
448extern int do_benchmark;
449extern int do_deinterlace;
450extern int do_hex_dump;
451extern int do_pkt_dump;
452extern int copy_ts;
453extern int copy_tb;
f5e66827
AK
454extern int exit_on_error;
455extern int print_stats;
456extern int qp_hist;
f5e66827
AK
457
458extern const AVIOInterruptCB int_cb;
459
460extern const OptionDef options[];
461
07fd0a22 462extern const HWAccel hwaccels[];
5d273d3e
MT
463extern int hwaccel_lax_profile_check;
464extern AVBufferRef *hw_device_ctx;
07fd0a22 465
f5e66827
AK
466void reset_options(OptionsContext *o);
467void show_usage(void);
468
f5e66827
AK
469void opt_output_file(void *optctx, const char *filename);
470
471void assert_avoptions(AVDictionary *m);
472
473int guess_input_channel_layout(InputStream *ist);
474
475int configure_filtergraph(FilterGraph *fg);
476int configure_output_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out);
fe2147e9 477int ist_in_filtergraph(FilterGraph *fg, InputStream *ist);
49670e42 478int filtergraph_is_simple(FilterGraph *fg);
73c6ec6d 479int init_simple_filtergraph(InputStream *ist, OutputStream *ost);
6d592fbd 480int init_complex_filtergraph(FilterGraph *fg);
f5e66827 481
722ec3eb
AK
482int ifilter_parameters_from_frame(InputFilter *ifilter, const AVFrame *frame);
483int ifilter_parameters_from_decoder(InputFilter *ifilter, const AVCodecContext *avctx);
484
77bd1bc7
AK
485int avconv_parse_options(int argc, char **argv);
486
7671dd7c 487int vdpau_init(AVCodecContext *s);
35177ba7 488int dxva2_init(AVCodecContext *s);
1839fafa 489int vda_init(AVCodecContext *s);
fb472e1a
AK
490int qsv_init(AVCodecContext *s);
491int qsv_transcode_init(OutputStream *ost);
5d273d3e
MT
492int vaapi_decode_init(AVCodecContext *avctx);
493int vaapi_device_init(const char *device);
7671dd7c 494
f5e66827 495#endif /* AVCONV_H */