]> git.sesse.net Git - ffmpeg/blob - avconv.h
avconv: Support VDA hwaccel
[ffmpeg] / 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 };
58
59 typedef struct HWAccel {
60     const char *name;
61     int (*init)(AVCodecContext *s);
62     enum HWAccelID id;
63     enum AVPixelFormat pix_fmt;
64 } HWAccel;
65
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 */
69     int file_index;
70     int stream_index;
71     int sync_file_index;
72     int sync_stream_index;
73     char *linklabel;       /* name of an output link, for mapping lavfi outputs */
74 } StreamMap;
75
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
81 } MetadataMap;
82
83 typedef struct OptionsContext {
84     OptionGroup *g;
85
86     /* input/output options */
87     int64_t start_time;
88     const char *format;
89
90     SpecifierOpt *codec_names;
91     int        nb_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;
97     int        nb_frame_rates;
98     SpecifierOpt *frame_sizes;
99     int        nb_frame_sizes;
100     SpecifierOpt *frame_pix_fmts;
101     int        nb_frame_pix_fmts;
102
103     /* input options */
104     int64_t input_ts_offset;
105     int rate_emu;
106     int accurate_seek;
107
108     SpecifierOpt *ts_scale;
109     int        nb_ts_scale;
110     SpecifierOpt *dump_attachment;
111     int        nb_dump_attachment;
112     SpecifierOpt *hwaccels;
113     int        nb_hwaccels;
114     SpecifierOpt *hwaccel_devices;
115     int        nb_hwaccel_devices;
116
117     /* output options */
118     StreamMap *stream_maps;
119     int     nb_stream_maps;
120     /* first item specifies output metadata, second is input */
121     MetadataMap (*meta_data_maps)[2];
122     int nb_meta_data_maps;
123     int metadata_global_manual;
124     int metadata_streams_manual;
125     int metadata_chapters_manual;
126     const char **attachments;
127     int       nb_attachments;
128
129     int chapters_input_file;
130
131     int64_t recording_time;
132     uint64_t limit_filesize;
133     float mux_preload;
134     float mux_max_delay;
135     int shortest;
136
137     int video_disable;
138     int audio_disable;
139     int subtitle_disable;
140     int data_disable;
141
142     /* indexed by output file stream index */
143     int   *streamid_map;
144     int nb_streamid_map;
145
146     SpecifierOpt *metadata;
147     int        nb_metadata;
148     SpecifierOpt *max_frames;
149     int        nb_max_frames;
150     SpecifierOpt *bitstream_filters;
151     int        nb_bitstream_filters;
152     SpecifierOpt *codec_tags;
153     int        nb_codec_tags;
154     SpecifierOpt *sample_fmts;
155     int        nb_sample_fmts;
156     SpecifierOpt *qscale;
157     int        nb_qscale;
158     SpecifierOpt *forced_key_frames;
159     int        nb_forced_key_frames;
160     SpecifierOpt *force_fps;
161     int        nb_force_fps;
162     SpecifierOpt *frame_aspect_ratios;
163     int        nb_frame_aspect_ratios;
164     SpecifierOpt *rc_overrides;
165     int        nb_rc_overrides;
166     SpecifierOpt *intra_matrices;
167     int        nb_intra_matrices;
168     SpecifierOpt *inter_matrices;
169     int        nb_inter_matrices;
170     SpecifierOpt *top_field_first;
171     int        nb_top_field_first;
172     SpecifierOpt *metadata_map;
173     int        nb_metadata_map;
174     SpecifierOpt *presets;
175     int        nb_presets;
176     SpecifierOpt *copy_initial_nonkeyframes;
177     int        nb_copy_initial_nonkeyframes;
178     SpecifierOpt *filters;
179     int        nb_filters;
180     SpecifierOpt *filter_scripts;
181     int        nb_filter_scripts;
182     SpecifierOpt *pass;
183     int        nb_pass;
184     SpecifierOpt *passlogfiles;
185     int        nb_passlogfiles;
186 } OptionsContext;
187
188 typedef struct InputFilter {
189     AVFilterContext    *filter;
190     struct InputStream *ist;
191     struct FilterGraph *graph;
192     uint8_t            *name;
193 } InputFilter;
194
195 typedef struct OutputFilter {
196     AVFilterContext     *filter;
197     struct OutputStream *ost;
198     struct FilterGraph  *graph;
199     uint8_t             *name;
200
201     /* temporary storage until stream maps are processed */
202     AVFilterInOut       *out_tmp;
203 } OutputFilter;
204
205 typedef struct FilterGraph {
206     int            index;
207     const char    *graph_desc;
208
209     AVFilterGraph *graph;
210
211     InputFilter   **inputs;
212     int          nb_inputs;
213     OutputFilter **outputs;
214     int         nb_outputs;
215 } FilterGraph;
216
217 typedef struct InputStream {
218     int file_index;
219     AVStream *st;
220     int discard;             /* true if stream data should be discarded */
221     int decoding_needed;     /* true if the packets must be decoded in 'raw_fifo' */
222     AVCodec *dec;
223     AVFrame *decoded_frame;
224     AVFrame *filter_frame; /* a ref of decoded_frame, to be sent to filters */
225
226     int64_t       start;     /* time when read started */
227     /* predicted dts of the next packet read for this stream or (when there are
228      * several frames in a packet) of the next frame in current packet */
229     int64_t       next_dts;
230     /* dts of the last packet read for this stream */
231     int64_t       last_dts;
232     PtsCorrectionContext pts_ctx;
233     double ts_scale;
234     int showed_multi_packet_warning;
235     AVDictionary *decoder_opts;
236     AVRational framerate;               /* framerate forced with -r */
237
238     int resample_height;
239     int resample_width;
240     int resample_pix_fmt;
241
242     int      resample_sample_fmt;
243     int      resample_sample_rate;
244     int      resample_channels;
245     uint64_t resample_channel_layout;
246
247     /* decoded data from this stream goes into all those filters
248      * currently video and audio only */
249     InputFilter **filters;
250     int        nb_filters;
251
252     /* hwaccel options */
253     enum HWAccelID hwaccel_id;
254     char  *hwaccel_device;
255
256     /* hwaccel context */
257     enum HWAccelID active_hwaccel_id;
258     void  *hwaccel_ctx;
259     void (*hwaccel_uninit)(AVCodecContext *s);
260     int  (*hwaccel_get_buffer)(AVCodecContext *s, AVFrame *frame, int flags);
261     int  (*hwaccel_retrieve_data)(AVCodecContext *s, AVFrame *frame);
262     enum AVPixelFormat hwaccel_pix_fmt;
263     enum AVPixelFormat hwaccel_retrieved_pix_fmt;
264
265     /* stats */
266     // combined size of all the packets read
267     uint64_t data_size;
268     /* number of packets successfully read for this stream */
269     uint64_t nb_packets;
270     // number of frames/samples retrieved from the decoder
271     uint64_t frames_decoded;
272     uint64_t samples_decoded;
273 } InputStream;
274
275 typedef struct InputFile {
276     AVFormatContext *ctx;
277     int eof_reached;      /* true if eof reached */
278     int eagain;           /* true if last read attempt returned EAGAIN */
279     int ist_index;        /* index of first stream in ist_table */
280     int64_t ts_offset;
281     int64_t start_time;   /* user-specified start time in AV_TIME_BASE or AV_NOPTS_VALUE */
282     int64_t recording_time;
283     int nb_streams;       /* number of stream that avconv is aware of; may be different
284                              from ctx.nb_streams if new streams appear during av_read_frame() */
285     int rate_emu;
286     int accurate_seek;
287
288 #if HAVE_PTHREADS
289     pthread_t thread;           /* thread reading from this file */
290     int finished;               /* the thread has exited */
291     int joined;                 /* the thread has been joined */
292     pthread_mutex_t fifo_lock;  /* lock for access to fifo */
293     pthread_cond_t  fifo_cond;  /* the main thread will signal on this cond after reading from fifo */
294     AVFifoBuffer *fifo;         /* demuxed packets are stored here; freed by the main thread */
295 #endif
296 } InputFile;
297
298 typedef struct OutputStream {
299     int file_index;          /* file index */
300     int index;               /* stream index in the output file */
301     int source_index;        /* InputStream index */
302     AVStream *st;            /* stream in the output file */
303     int encoding_needed;     /* true if encoding needed for this stream */
304     int frame_number;
305     /* input pts and corresponding output pts
306        for A/V sync */
307     // double sync_ipts;        /* dts from the AVPacket of the demuxer in second units */
308     struct InputStream *sync_ist; /* input stream to sync against */
309     int64_t sync_opts;       /* output frame counter, could be changed to some true timestamp */ // FIXME look at frame_number
310     /* pts of the first frame encoded for this stream, used for limiting
311      * recording time */
312     int64_t first_pts;
313     /* dts of the last packet sent to the muxer */
314     int64_t last_mux_dts;
315     AVBitStreamFilterContext *bitstream_filters;
316     AVCodec *enc;
317     int64_t max_frames;
318     AVFrame *filtered_frame;
319
320     /* video only */
321     AVRational frame_rate;
322     int force_fps;
323     int top_field_first;
324
325     float frame_aspect_ratio;
326
327     /* forced key frames */
328     int64_t *forced_kf_pts;
329     int forced_kf_count;
330     int forced_kf_index;
331     char *forced_keyframes;
332
333     char *logfile_prefix;
334     FILE *logfile;
335
336     OutputFilter *filter;
337     char *avfilter;
338
339     int64_t sws_flags;
340     AVDictionary *encoder_opts;
341     AVDictionary *resample_opts;
342     int finished;        /* no more packets should be written for this stream */
343     int stream_copy;
344     const char *attachment_filename;
345     int copy_initial_nonkeyframes;
346
347     enum AVPixelFormat pix_fmts[2];
348
349     AVCodecParserContext *parser;
350
351     /* stats */
352     // combined size of all the packets written
353     uint64_t data_size;
354     // number of packets send to the muxer
355     uint64_t packets_written;
356     // number of frames/samples sent to the encoder
357     uint64_t frames_encoded;
358     uint64_t samples_encoded;
359 } OutputStream;
360
361 typedef struct OutputFile {
362     AVFormatContext *ctx;
363     AVDictionary *opts;
364     int ost_index;       /* index of the first stream in output_streams */
365     int64_t recording_time; /* desired length of the resulting file in microseconds */
366     int64_t start_time;     /* start time in microseconds */
367     uint64_t limit_filesize;
368
369     int shortest;
370 } OutputFile;
371
372 extern InputStream **input_streams;
373 extern int        nb_input_streams;
374 extern InputFile   **input_files;
375 extern int        nb_input_files;
376
377 extern OutputStream **output_streams;
378 extern int         nb_output_streams;
379 extern OutputFile   **output_files;
380 extern int         nb_output_files;
381
382 extern FilterGraph **filtergraphs;
383 extern int        nb_filtergraphs;
384
385 extern char *vstats_filename;
386
387 extern float audio_drift_threshold;
388 extern float dts_delta_threshold;
389
390 extern int audio_volume;
391 extern int audio_sync_method;
392 extern int video_sync_method;
393 extern int do_benchmark;
394 extern int do_deinterlace;
395 extern int do_hex_dump;
396 extern int do_pkt_dump;
397 extern int copy_ts;
398 extern int copy_tb;
399 extern int exit_on_error;
400 extern int print_stats;
401 extern int qp_hist;
402
403 extern const AVIOInterruptCB int_cb;
404
405 extern const OptionDef options[];
406
407 extern const HWAccel hwaccels[];
408
409 void reset_options(OptionsContext *o);
410 void show_usage(void);
411
412 void opt_output_file(void *optctx, const char *filename);
413
414 void assert_avoptions(AVDictionary *m);
415
416 int guess_input_channel_layout(InputStream *ist);
417
418 int configure_filtergraph(FilterGraph *fg);
419 int configure_output_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out);
420 int ist_in_filtergraph(FilterGraph *fg, InputStream *ist);
421 FilterGraph *init_simple_filtergraph(InputStream *ist, OutputStream *ost);
422
423 int avconv_parse_options(int argc, char **argv);
424
425 int vdpau_init(AVCodecContext *s);
426 int dxva2_init(AVCodecContext *s);
427 int vda_init(AVCodecContext *s);
428
429 #endif /* AVCONV_H */