]> git.sesse.net Git - ffmpeg/blob - avconv.h
aarch64: vp9itxfm: Fix incorrect vertical alignment
[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     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 *bitrates;
166     int        nb_bitrates;
167     SpecifierOpt *forced_key_frames;
168     int        nb_forced_key_frames;
169     SpecifierOpt *force_fps;
170     int        nb_force_fps;
171     SpecifierOpt *frame_aspect_ratios;
172     int        nb_frame_aspect_ratios;
173     SpecifierOpt *rc_overrides;
174     int        nb_rc_overrides;
175     SpecifierOpt *intra_matrices;
176     int        nb_intra_matrices;
177     SpecifierOpt *inter_matrices;
178     int        nb_inter_matrices;
179     SpecifierOpt *top_field_first;
180     int        nb_top_field_first;
181     SpecifierOpt *metadata_map;
182     int        nb_metadata_map;
183     SpecifierOpt *presets;
184     int        nb_presets;
185     SpecifierOpt *copy_initial_nonkeyframes;
186     int        nb_copy_initial_nonkeyframes;
187     SpecifierOpt *filters;
188     int        nb_filters;
189     SpecifierOpt *filter_scripts;
190     int        nb_filter_scripts;
191     SpecifierOpt *pass;
192     int        nb_pass;
193     SpecifierOpt *passlogfiles;
194     int        nb_passlogfiles;
195     SpecifierOpt *max_muxing_queue_size;
196     int        nb_max_muxing_queue_size;
197 } OptionsContext;
198
199 typedef struct InputFilter {
200     AVFilterContext    *filter;
201     struct InputStream *ist;
202     struct FilterGraph *graph;
203     uint8_t            *name;
204
205     AVFifoBuffer *frame_queue;
206
207     // parameters configured for this input
208     int format;
209
210     int width, height;
211     AVRational sample_aspect_ratio;
212
213     int sample_rate;
214     uint64_t channel_layout;
215
216     AVBufferRef *hw_frames_ctx;
217
218     int eof;
219 } InputFilter;
220
221 typedef struct OutputFilter {
222     AVFilterContext     *filter;
223     struct OutputStream *ost;
224     struct FilterGraph  *graph;
225     uint8_t             *name;
226
227     /* temporary storage until stream maps are processed */
228     AVFilterInOut       *out_tmp;
229     enum AVMediaType     type;
230
231     /* desired output stream properties */
232     int width, height;
233     AVRational frame_rate;
234     int format;
235     int sample_rate;
236     uint64_t channel_layout;
237
238     // those are only set if no format is specified and the encoder gives us multiple options
239     int *formats;
240     uint64_t *channel_layouts;
241     int *sample_rates;
242 } OutputFilter;
243
244 typedef struct FilterGraph {
245     int            index;
246     const char    *graph_desc;
247
248     AVFilterGraph *graph;
249
250     InputFilter   **inputs;
251     int          nb_inputs;
252     OutputFilter **outputs;
253     int         nb_outputs;
254 } FilterGraph;
255
256 typedef struct InputStream {
257     int file_index;
258     AVStream *st;
259     int discard;             /* true if stream data should be discarded */
260     int decoding_needed;     /* true if the packets must be decoded in 'raw_fifo' */
261     AVCodecContext *dec_ctx;
262     AVCodec *dec;
263     AVFrame *decoded_frame;
264     AVFrame *filter_frame; /* a ref of decoded_frame, to be sent to filters */
265
266     int64_t       start;     /* time when read started */
267     /* predicted dts of the next packet read for this stream or (when there are
268      * several frames in a packet) of the next frame in current packet */
269     int64_t       next_dts;
270     /* dts of the last packet read for this stream */
271     int64_t       last_dts;
272     int64_t min_pts; /* pts with the smallest value in a current stream */
273     int64_t max_pts; /* pts with the higher value in a current stream */
274
275     // when forcing constant input framerate through -r,
276     // this contains the pts that will be given to the next decoded frame
277     int64_t cfr_next_pts;
278
279     int64_t nb_samples; /* number of samples in the last decoded audio frame before looping */
280     PtsCorrectionContext pts_ctx;
281     double ts_scale;
282     AVDictionary *decoder_opts;
283     AVRational framerate;               /* framerate forced with -r */
284
285     int autorotate;
286
287     /* decoded data from this stream goes into all those filters
288      * currently video and audio only */
289     InputFilter **filters;
290     int        nb_filters;
291
292     /* hwaccel options */
293     enum HWAccelID hwaccel_id;
294     char  *hwaccel_device;
295     enum AVPixelFormat hwaccel_output_format;
296
297     /* hwaccel context */
298     enum HWAccelID active_hwaccel_id;
299     void  *hwaccel_ctx;
300     void (*hwaccel_uninit)(AVCodecContext *s);
301     int  (*hwaccel_get_buffer)(AVCodecContext *s, AVFrame *frame, int flags);
302     int  (*hwaccel_retrieve_data)(AVCodecContext *s, AVFrame *frame);
303     enum AVPixelFormat hwaccel_pix_fmt;
304     enum AVPixelFormat hwaccel_retrieved_pix_fmt;
305     AVBufferRef *hw_frames_ctx;
306
307     /* stats */
308     // combined size of all the packets read
309     uint64_t data_size;
310     /* number of packets successfully read for this stream */
311     uint64_t nb_packets;
312     // number of frames/samples retrieved from the decoder
313     uint64_t frames_decoded;
314     uint64_t samples_decoded;
315 } InputStream;
316
317 typedef struct InputFile {
318     AVFormatContext *ctx;
319     int eof_reached;      /* true if eof reached */
320     int eagain;           /* true if last read attempt returned EAGAIN */
321     int ist_index;        /* index of first stream in ist_table */
322     int loop;             /* set number of times input stream should be looped */
323     int64_t duration;     /* actual duration of the longest stream in a file
324                              at the moment when looping happens */
325     AVRational time_base; /* time base of the duration */
326     int64_t ts_offset;
327     int64_t start_time;   /* user-specified start time in AV_TIME_BASE or AV_NOPTS_VALUE */
328     int64_t recording_time;
329     int nb_streams;       /* number of stream that avconv is aware of; may be different
330                              from ctx.nb_streams if new streams appear during av_read_frame() */
331     int rate_emu;
332     int accurate_seek;
333
334 #if HAVE_PTHREADS
335     pthread_t thread;           /* thread reading from this file */
336     int finished;               /* the thread has exited */
337     int joined;                 /* the thread has been joined */
338     pthread_mutex_t fifo_lock;  /* lock for access to fifo */
339     pthread_cond_t  fifo_cond;  /* the main thread will signal on this cond after reading from fifo */
340     AVFifoBuffer *fifo;         /* demuxed packets are stored here; freed by the main thread */
341 #endif
342 } InputFile;
343
344 typedef struct OutputStream {
345     int file_index;          /* file index */
346     int index;               /* stream index in the output file */
347     int source_index;        /* InputStream index */
348     AVStream *st;            /* stream in the output file */
349     int encoding_needed;     /* true if encoding needed for this stream */
350     int frame_number;
351     /* input pts and corresponding output pts
352        for A/V sync */
353     // double sync_ipts;        /* dts from the AVPacket of the demuxer in second units */
354     struct InputStream *sync_ist; /* input stream to sync against */
355     int64_t sync_opts;       /* output frame counter, could be changed to some true timestamp */ // FIXME look at frame_number
356     /* pts of the first frame encoded for this stream, used for limiting
357      * recording time */
358     int64_t first_pts;
359     /* dts of the last packet sent to the muxer */
360     int64_t last_mux_dts;
361     // the timebase of the packets sent to the muxer
362     AVRational mux_timebase;
363
364     int                    nb_bitstream_filters;
365     AVBSFContext            **bsf_ctx;
366
367     AVCodecContext *enc_ctx;
368     AVCodec *enc;
369     int64_t max_frames;
370     AVFrame *filtered_frame;
371
372     void  *hwaccel_ctx;
373
374     /* video only */
375     AVRational frame_rate;
376     int force_fps;
377     int top_field_first;
378
379     float frame_aspect_ratio;
380
381     /* forced key frames */
382     int64_t *forced_kf_pts;
383     int forced_kf_count;
384     int forced_kf_index;
385     char *forced_keyframes;
386
387     // the bitrate to send to the muxer for streamcopy
388     int bitrate_override;
389
390     char *logfile_prefix;
391     FILE *logfile;
392
393     OutputFilter *filter;
394     char *avfilter;
395
396     int64_t sws_flags;
397     AVDictionary *encoder_opts;
398     AVDictionary *resample_opts;
399     int finished;        /* no more packets should be written for this stream */
400     int stream_copy;
401
402     // init_output_stream() has been called for this stream
403     // The encoder and the bistream filters have been initialized and the stream
404     // parameters are set in the AVStream.
405     int initialized;
406
407     const char *attachment_filename;
408     int copy_initial_nonkeyframes;
409
410     enum AVPixelFormat pix_fmts[2];
411
412     AVCodecParserContext *parser;
413     AVCodecContext       *parser_avctx;
414
415     /* stats */
416     // combined size of all the packets written
417     uint64_t data_size;
418     // number of packets send to the muxer
419     uint64_t packets_written;
420     // number of frames/samples sent to the encoder
421     uint64_t frames_encoded;
422     uint64_t samples_encoded;
423
424     /* packet quality factor */
425     int quality;
426
427     int max_muxing_queue_size;
428
429     /* the packets are buffered here until the muxer is ready to be initialized */
430     AVFifoBuffer *muxing_queue;
431 } OutputStream;
432
433 typedef struct OutputFile {
434     AVFormatContext *ctx;
435     AVDictionary *opts;
436     int ost_index;       /* index of the first stream in output_streams */
437     int64_t recording_time; /* desired length of the resulting file in microseconds */
438     int64_t start_time;     /* start time in microseconds */
439     uint64_t limit_filesize;
440
441     int shortest;
442
443     int header_written;
444 } OutputFile;
445
446 extern InputStream **input_streams;
447 extern int        nb_input_streams;
448 extern InputFile   **input_files;
449 extern int        nb_input_files;
450
451 extern OutputStream **output_streams;
452 extern int         nb_output_streams;
453 extern OutputFile   **output_files;
454 extern int         nb_output_files;
455
456 extern FilterGraph **filtergraphs;
457 extern int        nb_filtergraphs;
458
459 extern char *vstats_filename;
460
461 extern float audio_drift_threshold;
462 extern float dts_delta_threshold;
463
464 extern int audio_volume;
465 extern int audio_sync_method;
466 extern int video_sync_method;
467 extern int do_benchmark;
468 extern int do_deinterlace;
469 extern int do_hex_dump;
470 extern int do_pkt_dump;
471 extern int copy_ts;
472 extern int copy_tb;
473 extern int exit_on_error;
474 extern int print_stats;
475 extern int qp_hist;
476
477 extern const AVIOInterruptCB int_cb;
478
479 extern const OptionDef options[];
480
481 extern const HWAccel hwaccels[];
482 extern int hwaccel_lax_profile_check;
483 extern AVBufferRef *hw_device_ctx;
484
485 void reset_options(OptionsContext *o);
486 void show_usage(void);
487
488 void opt_output_file(void *optctx, const char *filename);
489
490 void assert_avoptions(AVDictionary *m);
491
492 int guess_input_channel_layout(InputStream *ist);
493
494 int configure_filtergraph(FilterGraph *fg);
495 int configure_output_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out);
496 int ist_in_filtergraph(FilterGraph *fg, InputStream *ist);
497 int filtergraph_is_simple(FilterGraph *fg);
498 int init_simple_filtergraph(InputStream *ist, OutputStream *ost);
499 int init_complex_filtergraph(FilterGraph *fg);
500
501 int ifilter_parameters_from_frame(InputFilter *ifilter, const AVFrame *frame);
502
503 int avconv_parse_options(int argc, char **argv);
504
505 int vdpau_init(AVCodecContext *s);
506 int dxva2_init(AVCodecContext *s);
507 int vda_init(AVCodecContext *s);
508 int qsv_init(AVCodecContext *s);
509 int qsv_transcode_init(OutputStream *ost);
510 int vaapi_decode_init(AVCodecContext *avctx);
511 int vaapi_device_init(const char *device);
512
513 #endif /* AVCONV_H */