h264dec: add a CUVID hwaccel
[libav.git] / avtools / 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"
d2e6dd32 43#include "libavutil/hwcontext.h"
f5e66827
AK
44#include "libavutil/pixfmt.h"
45#include "libavutil/rational.h"
46
47#define VSYNC_AUTO -1
48#define VSYNC_PASSTHROUGH 0
49#define VSYNC_CFR 1
50#define VSYNC_VFR 2
51
07fd0a22
AK
52enum HWAccelID {
53 HWACCEL_NONE = 0,
54 HWACCEL_AUTO,
7671dd7c 55 HWACCEL_VDPAU,
35177ba7 56 HWACCEL_DXVA2,
1839fafa 57 HWACCEL_VDA,
fb472e1a 58 HWACCEL_QSV,
5d273d3e 59 HWACCEL_VAAPI,
f9e7a2f9 60 HWACCEL_D3D11VA,
b9129ec4 61 HWACCEL_CUVID,
07fd0a22
AK
62};
63
64typedef struct HWAccel {
65 const char *name;
66 int (*init)(AVCodecContext *s);
67 enum HWAccelID id;
68 enum AVPixelFormat pix_fmt;
d2e6dd32 69 enum AVHWDeviceType device_type;
07fd0a22
AK
70} HWAccel;
71
d2e6dd32
MT
72typedef struct HWDevice {
73 char *name;
74 enum AVHWDeviceType type;
75 AVBufferRef *device_ref;
76} HWDevice;
77
f5e66827
AK
78/* select an input stream for an output stream */
79typedef struct StreamMap {
c1ef30a6 80 int disabled; /* 1 is this mapping is disabled by a negative map */
f5e66827
AK
81 int file_index;
82 int stream_index;
83 int sync_file_index;
84 int sync_stream_index;
c1ef30a6 85 char *linklabel; /* name of an output link, for mapping lavfi outputs */
f5e66827
AK
86} StreamMap;
87
c1ef30a6 88/* select an input file for an output file */
f5e66827 89typedef struct MetadataMap {
c1ef30a6
DB
90 int file; // file index
91 char type; // type of metadata to copy -- (g)lobal, (s)tream, (c)hapter or (p)rogram
92 int index; // stream/chapter/program number
f5e66827
AK
93} MetadataMap;
94
95typedef struct OptionsContext {
77bd1bc7
AK
96 OptionGroup *g;
97
f5e66827
AK
98 /* input/output options */
99 int64_t start_time;
100 const char *format;
101
102 SpecifierOpt *codec_names;
103 int nb_codec_names;
104 SpecifierOpt *audio_channels;
105 int nb_audio_channels;
106 SpecifierOpt *audio_sample_rate;
107 int nb_audio_sample_rate;
108 SpecifierOpt *frame_rates;
109 int nb_frame_rates;
110 SpecifierOpt *frame_sizes;
111 int nb_frame_sizes;
112 SpecifierOpt *frame_pix_fmts;
113 int nb_frame_pix_fmts;
114
115 /* input options */
116 int64_t input_ts_offset;
16b0c929 117 int loop;
f5e66827 118 int rate_emu;
811bd078 119 int accurate_seek;
f5e66827
AK
120
121 SpecifierOpt *ts_scale;
122 int nb_ts_scale;
123 SpecifierOpt *dump_attachment;
124 int nb_dump_attachment;
07fd0a22
AK
125 SpecifierOpt *hwaccels;
126 int nb_hwaccels;
127 SpecifierOpt *hwaccel_devices;
128 int nb_hwaccel_devices;
5d273d3e
MT
129 SpecifierOpt *hwaccel_output_formats;
130 int nb_hwaccel_output_formats;
16302246
MS
131 SpecifierOpt *autorotate;
132 int nb_autorotate;
f5e66827
AK
133
134 /* output options */
135 StreamMap *stream_maps;
136 int nb_stream_maps;
137 /* first item specifies output metadata, second is input */
138 MetadataMap (*meta_data_maps)[2];
139 int nb_meta_data_maps;
140 int metadata_global_manual;
141 int metadata_streams_manual;
142 int metadata_chapters_manual;
143 const char **attachments;
144 int nb_attachments;
145
146 int chapters_input_file;
147
148 int64_t recording_time;
149 uint64_t limit_filesize;
150 float mux_preload;
151 float mux_max_delay;
3c0df905 152 int shortest;
f5e66827
AK
153
154 int video_disable;
155 int audio_disable;
156 int subtitle_disable;
157 int data_disable;
158
159 /* indexed by output file stream index */
160 int *streamid_map;
161 int nb_streamid_map;
162
163 SpecifierOpt *metadata;
164 int nb_metadata;
165 SpecifierOpt *max_frames;
166 int nb_max_frames;
167 SpecifierOpt *bitstream_filters;
168 int nb_bitstream_filters;
169 SpecifierOpt *codec_tags;
170 int nb_codec_tags;
171 SpecifierOpt *sample_fmts;
172 int nb_sample_fmts;
173 SpecifierOpt *qscale;
174 int nb_qscale;
b420a27e
AK
175 SpecifierOpt *bitrates;
176 int nb_bitrates;
f5e66827
AK
177 SpecifierOpt *forced_key_frames;
178 int nb_forced_key_frames;
179 SpecifierOpt *force_fps;
180 int nb_force_fps;
181 SpecifierOpt *frame_aspect_ratios;
182 int nb_frame_aspect_ratios;
183 SpecifierOpt *rc_overrides;
184 int nb_rc_overrides;
185 SpecifierOpt *intra_matrices;
186 int nb_intra_matrices;
187 SpecifierOpt *inter_matrices;
188 int nb_inter_matrices;
189 SpecifierOpt *top_field_first;
190 int nb_top_field_first;
191 SpecifierOpt *metadata_map;
192 int nb_metadata_map;
193 SpecifierOpt *presets;
194 int nb_presets;
195 SpecifierOpt *copy_initial_nonkeyframes;
196 int nb_copy_initial_nonkeyframes;
197 SpecifierOpt *filters;
198 int nb_filters;
a4208b9b
AK
199 SpecifierOpt *filter_scripts;
200 int nb_filter_scripts;
038c0b1e
AK
201 SpecifierOpt *pass;
202 int nb_pass;
bbcedade
AK
203 SpecifierOpt *passlogfiles;
204 int nb_passlogfiles;
398f015f
AK
205 SpecifierOpt *max_muxing_queue_size;
206 int nb_max_muxing_queue_size;
f5e66827
AK
207} OptionsContext;
208
209typedef struct InputFilter {
210 AVFilterContext *filter;
211 struct InputStream *ist;
212 struct FilterGraph *graph;
213 uint8_t *name;
722ec3eb 214
a3a0230a
AK
215 AVFifoBuffer *frame_queue;
216
722ec3eb
AK
217 // parameters configured for this input
218 int format;
219
220 int width, height;
221 AVRational sample_aspect_ratio;
222
223 int sample_rate;
224 uint64_t channel_layout;
225
226 AVBufferRef *hw_frames_ctx;
94ebf556
AK
227
228 int eof;
f5e66827
AK
229} InputFilter;
230
231typedef struct OutputFilter {
232 AVFilterContext *filter;
233 struct OutputStream *ost;
234 struct FilterGraph *graph;
235 uint8_t *name;
236
237 /* temporary storage until stream maps are processed */
238 AVFilterInOut *out_tmp;
6d592fbd 239 enum AVMediaType type;
50722b4f
AK
240
241 /* desired output stream properties */
242 int width, height;
243 AVRational frame_rate;
244 int format;
245 int sample_rate;
246 uint64_t channel_layout;
247
248 // those are only set if no format is specified and the encoder gives us multiple options
249 int *formats;
250 uint64_t *channel_layouts;
251 int *sample_rates;
f5e66827
AK
252} OutputFilter;
253
254typedef struct FilterGraph {
255 int index;
256 const char *graph_desc;
257
258 AVFilterGraph *graph;
259
260 InputFilter **inputs;
261 int nb_inputs;
262 OutputFilter **outputs;
263 int nb_outputs;
264} FilterGraph;
265
266typedef struct InputStream {
267 int file_index;
268 AVStream *st;
269 int discard; /* true if stream data should be discarded */
270 int decoding_needed; /* true if the packets must be decoded in 'raw_fifo' */
41776ba9 271 AVCodecContext *dec_ctx;
f5e66827
AK
272 AVCodec *dec;
273 AVFrame *decoded_frame;
9b2dc295 274 AVFrame *filter_frame; /* a ref of decoded_frame, to be sent to filters */
f5e66827
AK
275
276 int64_t start; /* time when read started */
277 /* predicted dts of the next packet read for this stream or (when there are
278 * several frames in a packet) of the next frame in current packet */
279 int64_t next_dts;
280 /* dts of the last packet read for this stream */
281 int64_t last_dts;
16b0c929
AH
282 int64_t min_pts; /* pts with the smallest value in a current stream */
283 int64_t max_pts; /* pts with the higher value in a current stream */
b0f36a00
AK
284
285 // when forcing constant input framerate through -r,
286 // this contains the pts that will be given to the next decoded frame
287 int64_t cfr_next_pts;
288
16b0c929 289 int64_t nb_samples; /* number of samples in the last decoded audio frame before looping */
f5e66827
AK
290 PtsCorrectionContext pts_ctx;
291 double ts_scale;
f3a6ad22 292 AVDictionary *decoder_opts;
f5e66827
AK
293 AVRational framerate; /* framerate forced with -r */
294
16302246 295 int autorotate;
f5e66827 296
f5e66827
AK
297 /* decoded data from this stream goes into all those filters
298 * currently video and audio only */
299 InputFilter **filters;
300 int nb_filters;
07fd0a22
AK
301
302 /* hwaccel options */
303 enum HWAccelID hwaccel_id;
304 char *hwaccel_device;
5d273d3e 305 enum AVPixelFormat hwaccel_output_format;
07fd0a22
AK
306
307 /* hwaccel context */
308 enum HWAccelID active_hwaccel_id;
309 void *hwaccel_ctx;
310 void (*hwaccel_uninit)(AVCodecContext *s);
311 int (*hwaccel_get_buffer)(AVCodecContext *s, AVFrame *frame, int flags);
312 int (*hwaccel_retrieve_data)(AVCodecContext *s, AVFrame *frame);
313 enum AVPixelFormat hwaccel_pix_fmt;
314 enum AVPixelFormat hwaccel_retrieved_pix_fmt;
5d273d3e 315 AVBufferRef *hw_frames_ctx;
47543450
AK
316
317 /* stats */
318 // combined size of all the packets read
319 uint64_t data_size;
320 /* number of packets successfully read for this stream */
321 uint64_t nb_packets;
322 // number of frames/samples retrieved from the decoder
323 uint64_t frames_decoded;
324 uint64_t samples_decoded;
f5e66827
AK
325} InputStream;
326
327typedef struct InputFile {
328 AVFormatContext *ctx;
329 int eof_reached; /* true if eof reached */
0b26ef42 330 int eagain; /* true if last read attempt returned EAGAIN */
f5e66827 331 int ist_index; /* index of first stream in ist_table */
16b0c929
AH
332 int loop; /* set number of times input stream should be looped */
333 int64_t duration; /* actual duration of the longest stream in a file
334 at the moment when looping happens */
335 AVRational time_base; /* time base of the duration */
f5e66827 336 int64_t ts_offset;
811bd078 337 int64_t start_time; /* user-specified start time in AV_TIME_BASE or AV_NOPTS_VALUE */
488a0fa6 338 int64_t recording_time;
f5e66827
AK
339 int nb_streams; /* number of stream that avconv is aware of; may be different
340 from ctx.nb_streams if new streams appear during av_read_frame() */
341 int rate_emu;
811bd078 342 int accurate_seek;
f5e66827
AK
343
344#if HAVE_PTHREADS
345 pthread_t thread; /* thread reading from this file */
346 int finished; /* the thread has exited */
347 int joined; /* the thread has been joined */
348 pthread_mutex_t fifo_lock; /* lock for access to fifo */
349 pthread_cond_t fifo_cond; /* the main thread will signal on this cond after reading from fifo */
350 AVFifoBuffer *fifo; /* demuxed packets are stored here; freed by the main thread */
351#endif
352} InputFile;
353
354typedef struct OutputStream {
355 int file_index; /* file index */
356 int index; /* stream index in the output file */
357 int source_index; /* InputStream index */
358 AVStream *st; /* stream in the output file */
359 int encoding_needed; /* true if encoding needed for this stream */
360 int frame_number;
361 /* input pts and corresponding output pts
362 for A/V sync */
363 // double sync_ipts; /* dts from the AVPacket of the demuxer in second units */
364 struct InputStream *sync_ist; /* input stream to sync against */
365 int64_t sync_opts; /* output frame counter, could be changed to some true timestamp */ // FIXME look at frame_number
366 /* pts of the first frame encoded for this stream, used for limiting
367 * recording time */
368 int64_t first_pts;
76d23f40
AK
369 /* dts of the last packet sent to the muxer */
370 int64_t last_mux_dts;
3e265ca5
AK
371 // the timebase of the packets sent to the muxer
372 AVRational mux_timebase;
4426540f
AK
373
374 int nb_bitstream_filters;
4426540f
AK
375 AVBSFContext **bsf_ctx;
376
38313626 377 AVCodecContext *enc_ctx;
f5e66827
AK
378 AVCodec *enc;
379 int64_t max_frames;
380 AVFrame *filtered_frame;
381
fb472e1a
AK
382 void *hwaccel_ctx;
383
f5e66827
AK
384 /* video only */
385 AVRational frame_rate;
386 int force_fps;
387 int top_field_first;
388
389 float frame_aspect_ratio;
f5e66827
AK
390
391 /* forced key frames */
392 int64_t *forced_kf_pts;
393 int forced_kf_count;
394 int forced_kf_index;
395 char *forced_keyframes;
396
b420a27e
AK
397 // the bitrate to send to the muxer for streamcopy
398 int bitrate_override;
399
bbcedade 400 char *logfile_prefix;
f5e66827
AK
401 FILE *logfile;
402
403 OutputFilter *filter;
404 char *avfilter;
405
406 int64_t sws_flags;
9580818c 407 AVDictionary *encoder_opts;
5c7db097 408 AVDictionary *resample_opts;
57d24225 409 int finished; /* no more packets should be written for this stream */
f5e66827 410 int stream_copy;
1c169782
AK
411
412 // init_output_stream() has been called for this stream
413 // The encoder and the bistream filters have been initialized and the stream
414 // parameters are set in the AVStream.
415 int initialized;
416
f5e66827
AK
417 const char *attachment_filename;
418 int copy_initial_nonkeyframes;
419
716d413c 420 enum AVPixelFormat pix_fmts[2];
c872d310
AK
421
422 AVCodecParserContext *parser;
35c85806 423 AVCodecContext *parser_avctx;
baeb59d8
AK
424
425 /* stats */
426 // combined size of all the packets written
427 uint64_t data_size;
47543450
AK
428 // number of packets send to the muxer
429 uint64_t packets_written;
430 // number of frames/samples sent to the encoder
431 uint64_t frames_encoded;
432 uint64_t samples_encoded;
5d3addb9
VG
433
434 /* packet quality factor */
435 int quality;
398f015f
AK
436
437 int max_muxing_queue_size;
438
439 /* the packets are buffered here until the muxer is ready to be initialized */
440 AVFifoBuffer *muxing_queue;
f5e66827
AK
441} OutputStream;
442
443typedef struct OutputFile {
444 AVFormatContext *ctx;
445 AVDictionary *opts;
446 int ost_index; /* index of the first stream in output_streams */
447 int64_t recording_time; /* desired length of the resulting file in microseconds */
448 int64_t start_time; /* start time in microseconds */
449 uint64_t limit_filesize;
3c0df905
AK
450
451 int shortest;
1c169782
AK
452
453 int header_written;
f5e66827
AK
454} OutputFile;
455
456extern InputStream **input_streams;
457extern int nb_input_streams;
458extern InputFile **input_files;
459extern int nb_input_files;
460
461extern OutputStream **output_streams;
462extern int nb_output_streams;
463extern OutputFile **output_files;
464extern int nb_output_files;
465
466extern FilterGraph **filtergraphs;
467extern int nb_filtergraphs;
468
f5e66827
AK
469extern char *vstats_filename;
470
471extern float audio_drift_threshold;
472extern float dts_delta_threshold;
473
474extern int audio_volume;
475extern int audio_sync_method;
476extern int video_sync_method;
477extern int do_benchmark;
478extern int do_deinterlace;
479extern int do_hex_dump;
480extern int do_pkt_dump;
481extern int copy_ts;
482extern int copy_tb;
f5e66827
AK
483extern int exit_on_error;
484extern int print_stats;
485extern int qp_hist;
f5e66827
AK
486
487extern const AVIOInterruptCB int_cb;
488
489extern const OptionDef options[];
490
07fd0a22 491extern const HWAccel hwaccels[];
5d273d3e
MT
492extern int hwaccel_lax_profile_check;
493extern AVBufferRef *hw_device_ctx;
e669db76 494extern HWDevice *filter_hw_device;
07fd0a22 495
f5e66827
AK
496void reset_options(OptionsContext *o);
497void show_usage(void);
498
f5e66827
AK
499void opt_output_file(void *optctx, const char *filename);
500
501void assert_avoptions(AVDictionary *m);
502
503int guess_input_channel_layout(InputStream *ist);
504
505int configure_filtergraph(FilterGraph *fg);
506int configure_output_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out);
fe2147e9 507int ist_in_filtergraph(FilterGraph *fg, InputStream *ist);
49670e42 508int filtergraph_is_simple(FilterGraph *fg);
73c6ec6d 509int init_simple_filtergraph(InputStream *ist, OutputStream *ost);
6d592fbd 510int init_complex_filtergraph(FilterGraph *fg);
f5e66827 511
722ec3eb 512int ifilter_parameters_from_frame(InputFilter *ifilter, const AVFrame *frame);
722ec3eb 513
77bd1bc7
AK
514int avconv_parse_options(int argc, char **argv);
515
35177ba7 516int dxva2_init(AVCodecContext *s);
1839fafa 517int vda_init(AVCodecContext *s);
fb472e1a
AK
518int qsv_init(AVCodecContext *s);
519int qsv_transcode_init(OutputStream *ost);
7671dd7c 520
d2e6dd32
MT
521HWDevice *hw_device_get_by_name(const char *name);
522int hw_device_init_from_string(const char *arg, HWDevice **dev);
523void hw_device_free_all(void);
524
525int hw_device_setup_for_decode(InputStream *ist);
526int hw_device_setup_for_encode(OutputStream *ost);
527
528int hwaccel_decode_init(AVCodecContext *avctx);
529
f5e66827 530#endif /* AVCONV_H */