]> git.sesse.net Git - ffmpeg/blob - ffmpeg.h
Merge commit '93afb6c98df876b15e3d911a9450ad55f92080ce'
[ffmpeg] / ffmpeg.h
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg 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  * FFmpeg 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 FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18
19 #ifndef FFMPEG_H
20 #define FFMPEG_H
21
22 #include "config.h"
23
24 #include <stdint.h>
25 #include <stdio.h>
26 #include <signal.h>
27
28 #if HAVE_PTHREADS
29 #include <pthread.h>
30 #endif
31
32 #include "cmdutils.h"
33
34 #include "libavformat/avformat.h"
35 #include "libavformat/avio.h"
36
37 #include "libavcodec/avcodec.h"
38
39 #include "libavfilter/avfilter.h"
40
41 #include "libavutil/avutil.h"
42 #include "libavutil/dict.h"
43 #include "libavutil/eval.h"
44 #include "libavutil/fifo.h"
45 #include "libavutil/pixfmt.h"
46 #include "libavutil/rational.h"
47
48 #include "libswresample/swresample.h"
49
50 #define VSYNC_AUTO       -1
51 #define VSYNC_PASSTHROUGH 0
52 #define VSYNC_CFR         1
53 #define VSYNC_VFR         2
54 #define VSYNC_VSCFR       0xfe
55 #define VSYNC_DROP        0xff
56
57 #define MAX_STREAMS 1024    /* arbitrary sanity check value */
58
59 enum HWAccelID {
60     HWACCEL_NONE = 0,
61     HWACCEL_AUTO,
62     HWACCEL_VDPAU,
63     HWACCEL_DXVA2,
64     HWACCEL_VDA,
65 };
66
67 typedef struct HWAccel {
68     const char *name;
69     int (*init)(AVCodecContext *s);
70     enum HWAccelID id;
71     enum AVPixelFormat pix_fmt;
72 } HWAccel;
73
74 /* select an input stream for an output stream */
75 typedef struct StreamMap {
76     int disabled;           /* 1 is this mapping is disabled by a negative map */
77     int file_index;
78     int stream_index;
79     int sync_file_index;
80     int sync_stream_index;
81     char *linklabel;       /* name of an output link, for mapping lavfi outputs */
82 } StreamMap;
83
84 typedef struct {
85     int  file_idx,  stream_idx,  channel_idx; // input
86     int ofile_idx, ostream_idx;               // output
87 } AudioChannelMap;
88
89 typedef struct OptionsContext {
90     OptionGroup *g;
91
92     /* input/output options */
93     int64_t start_time;
94     const char *format;
95
96     SpecifierOpt *codec_names;
97     int        nb_codec_names;
98     SpecifierOpt *audio_channels;
99     int        nb_audio_channels;
100     SpecifierOpt *audio_sample_rate;
101     int        nb_audio_sample_rate;
102     SpecifierOpt *frame_rates;
103     int        nb_frame_rates;
104     SpecifierOpt *frame_sizes;
105     int        nb_frame_sizes;
106     SpecifierOpt *frame_pix_fmts;
107     int        nb_frame_pix_fmts;
108
109     /* input options */
110     int64_t input_ts_offset;
111     int rate_emu;
112     int accurate_seek;
113
114     SpecifierOpt *ts_scale;
115     int        nb_ts_scale;
116     SpecifierOpt *dump_attachment;
117     int        nb_dump_attachment;
118     SpecifierOpt *hwaccels;
119     int        nb_hwaccels;
120     SpecifierOpt *hwaccel_devices;
121     int        nb_hwaccel_devices;
122
123     /* output options */
124     StreamMap *stream_maps;
125     int     nb_stream_maps;
126     AudioChannelMap *audio_channel_maps; /* one info entry per -map_channel */
127     int           nb_audio_channel_maps; /* number of (valid) -map_channel settings */
128     int metadata_global_manual;
129     int metadata_streams_manual;
130     int metadata_chapters_manual;
131     const char **attachments;
132     int       nb_attachments;
133
134     int chapters_input_file;
135
136     int64_t recording_time;
137     int64_t stop_time;
138     uint64_t limit_filesize;
139     float mux_preload;
140     float mux_max_delay;
141     int shortest;
142
143     int video_disable;
144     int audio_disable;
145     int subtitle_disable;
146     int data_disable;
147
148     /* indexed by output file stream index */
149     int   *streamid_map;
150     int nb_streamid_map;
151
152     SpecifierOpt *metadata;
153     int        nb_metadata;
154     SpecifierOpt *max_frames;
155     int        nb_max_frames;
156     SpecifierOpt *bitstream_filters;
157     int        nb_bitstream_filters;
158     SpecifierOpt *codec_tags;
159     int        nb_codec_tags;
160     SpecifierOpt *sample_fmts;
161     int        nb_sample_fmts;
162     SpecifierOpt *qscale;
163     int        nb_qscale;
164     SpecifierOpt *forced_key_frames;
165     int        nb_forced_key_frames;
166     SpecifierOpt *force_fps;
167     int        nb_force_fps;
168     SpecifierOpt *frame_aspect_ratios;
169     int        nb_frame_aspect_ratios;
170     SpecifierOpt *rc_overrides;
171     int        nb_rc_overrides;
172     SpecifierOpt *intra_matrices;
173     int        nb_intra_matrices;
174     SpecifierOpt *inter_matrices;
175     int        nb_inter_matrices;
176     SpecifierOpt *chroma_intra_matrices;
177     int        nb_chroma_intra_matrices;
178     SpecifierOpt *top_field_first;
179     int        nb_top_field_first;
180     SpecifierOpt *metadata_map;
181     int        nb_metadata_map;
182     SpecifierOpt *presets;
183     int        nb_presets;
184     SpecifierOpt *copy_initial_nonkeyframes;
185     int        nb_copy_initial_nonkeyframes;
186     SpecifierOpt *copy_prior_start;
187     int        nb_copy_prior_start;
188     SpecifierOpt *filters;
189     int        nb_filters;
190     SpecifierOpt *filter_scripts;
191     int        nb_filter_scripts;
192     SpecifierOpt *reinit_filters;
193     int        nb_reinit_filters;
194     SpecifierOpt *fix_sub_duration;
195     int        nb_fix_sub_duration;
196     SpecifierOpt *canvas_sizes;
197     int        nb_canvas_sizes;
198     SpecifierOpt *pass;
199     int        nb_pass;
200     SpecifierOpt *passlogfiles;
201     int        nb_passlogfiles;
202     SpecifierOpt *guess_layout_max;
203     int        nb_guess_layout_max;
204     SpecifierOpt *apad;
205     int        nb_apad;
206 } OptionsContext;
207
208 typedef struct InputFilter {
209     AVFilterContext    *filter;
210     struct InputStream *ist;
211     struct FilterGraph *graph;
212     uint8_t            *name;
213 } InputFilter;
214
215 typedef 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;
223 } OutputFilter;
224
225 typedef struct FilterGraph {
226     int            index;
227     const char    *graph_desc;
228
229     AVFilterGraph *graph;
230     int reconfiguration;
231
232     InputFilter   **inputs;
233     int          nb_inputs;
234     OutputFilter **outputs;
235     int         nb_outputs;
236 } FilterGraph;
237
238 typedef 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' */
243     AVCodec *dec;
244     AVFrame *decoded_frame;
245     AVFrame *filter_frame; /* a ref of decoded_frame, to be sent to filters */
246
247     int64_t       start;     /* time when read started */
248     /* predicted dts of the next packet read for this stream or (when there are
249      * several frames in a packet) of the next frame in current packet (in AV_TIME_BASE units) */
250     int64_t       next_dts;
251     int64_t       dts;       ///< dts of the last packet read for this stream (in AV_TIME_BASE units)
252
253     int64_t       next_pts;  ///< synthetic pts for the next decode frame (in AV_TIME_BASE units)
254     int64_t       pts;       ///< current pts of the decoded frame  (in AV_TIME_BASE units)
255     int           wrap_correction_done;
256
257     int64_t filter_in_rescale_delta_last;
258
259     double ts_scale;
260     int saw_first_ts;
261     int showed_multi_packet_warning;
262     AVDictionary *decoder_opts;
263     AVRational framerate;               /* framerate forced with -r */
264     int top_field_first;
265     int guess_layout_max;
266
267     int resample_height;
268     int resample_width;
269     int resample_pix_fmt;
270
271     int      resample_sample_fmt;
272     int      resample_sample_rate;
273     int      resample_channels;
274     uint64_t resample_channel_layout;
275
276     int fix_sub_duration;
277     struct { /* previous decoded subtitle and related variables */
278         int got_output;
279         int ret;
280         AVSubtitle subtitle;
281     } prev_sub;
282
283     struct sub2video {
284         int64_t last_pts;
285         int64_t end_pts;
286         AVFrame *frame;
287         int w, h;
288     } sub2video;
289
290     int dr1;
291
292     /* decoded data from this stream goes into all those filters
293      * currently video and audio only */
294     InputFilter **filters;
295     int        nb_filters;
296
297     int reinit_filters;
298
299     /* hwaccel options */
300     enum HWAccelID hwaccel_id;
301     char  *hwaccel_device;
302
303     /* hwaccel context */
304     enum HWAccelID active_hwaccel_id;
305     void  *hwaccel_ctx;
306     void (*hwaccel_uninit)(AVCodecContext *s);
307     int  (*hwaccel_get_buffer)(AVCodecContext *s, AVFrame *frame, int flags);
308     int  (*hwaccel_retrieve_data)(AVCodecContext *s, AVFrame *frame);
309     enum AVPixelFormat hwaccel_pix_fmt;
310     enum AVPixelFormat hwaccel_retrieved_pix_fmt;
311
312     /* stats */
313     // combined size of all the packets read
314     uint64_t data_size;
315     /* number of packets successfully read for this stream */
316     uint64_t nb_packets;
317     // number of frames/samples retrieved from the decoder
318     uint64_t frames_decoded;
319     uint64_t samples_decoded;
320 } InputStream;
321
322 typedef struct InputFile {
323     AVFormatContext *ctx;
324     int eof_reached;      /* true if eof reached */
325     int eagain;           /* true if last read attempt returned EAGAIN */
326     int ist_index;        /* index of first stream in input_streams */
327     int64_t input_ts_offset;
328     int64_t ts_offset;
329     int64_t last_ts;
330     int64_t start_time;   /* user-specified start time in AV_TIME_BASE or AV_NOPTS_VALUE */
331     int64_t recording_time;
332     int nb_streams;       /* number of stream that ffmpeg is aware of; may be different
333                              from ctx.nb_streams if new streams appear during av_read_frame() */
334     int nb_streams_warn;  /* number of streams that the user was warned of */
335     int rate_emu;
336     int accurate_seek;
337
338 #if HAVE_PTHREADS
339     pthread_t thread;           /* thread reading from this file */
340     int non_blocking;           /* reading packets from the thread should not block */
341     int finished;               /* the thread has exited */
342     int joined;                 /* the thread has been joined */
343     pthread_mutex_t fifo_lock;  /* lock for access to fifo */
344     pthread_cond_t  fifo_cond;  /* the main thread will signal on this cond after reading from fifo */
345     AVFifoBuffer *fifo;         /* demuxed packets are stored here; freed by the main thread */
346 #endif
347 } InputFile;
348
349 enum forced_keyframes_const {
350     FKF_N,
351     FKF_N_FORCED,
352     FKF_PREV_FORCED_N,
353     FKF_PREV_FORCED_T,
354     FKF_T,
355     FKF_NB
356 };
357
358 extern const char *const forced_keyframes_const_names[];
359
360 typedef enum {
361     ENCODER_FINISHED = 1,
362     MUXER_FINISHED = 2,
363 } OSTFinished ;
364
365 typedef struct OutputStream {
366     int file_index;          /* file index */
367     int index;               /* stream index in the output file */
368     int source_index;        /* InputStream index */
369     AVStream *st;            /* stream in the output file */
370     int encoding_needed;     /* true if encoding needed for this stream */
371     int frame_number;
372     /* input pts and corresponding output pts
373        for A/V sync */
374     struct InputStream *sync_ist; /* input stream to sync against */
375     int64_t sync_opts;       /* output frame counter, could be changed to some true timestamp */ // FIXME look at frame_number
376     /* pts of the first frame encoded for this stream, used for limiting
377      * recording time */
378     int64_t first_pts;
379     /* dts of the last packet sent to the muxer */
380     int64_t last_mux_dts;
381     AVBitStreamFilterContext *bitstream_filters;
382     AVCodec *enc;
383     int64_t max_frames;
384     AVFrame *filtered_frame;
385
386     /* video only */
387     AVRational frame_rate;
388     int force_fps;
389     int top_field_first;
390
391     AVRational frame_aspect_ratio;
392
393     /* forced key frames */
394     int64_t *forced_kf_pts;
395     int forced_kf_count;
396     int forced_kf_index;
397     char *forced_keyframes;
398     AVExpr *forced_keyframes_pexpr;
399     double forced_keyframes_expr_const_values[FKF_NB];
400
401     /* audio only */
402     int audio_channels_map[SWR_CH_MAX];  /* list of the channels id to pick from the source stream */
403     int audio_channels_mapped;           /* number of channels in audio_channels_map */
404
405     char *logfile_prefix;
406     FILE *logfile;
407
408     OutputFilter *filter;
409     char *avfilter;
410     char *filters;         ///< filtergraph associated to the -filter option
411     char *filters_script;  ///< filtergraph script associated to the -filter_script option
412
413     int64_t sws_flags;
414     AVDictionary *encoder_opts;
415     AVDictionary *swr_opts;
416     AVDictionary *resample_opts;
417     char *apad;
418     OSTFinished finished;        /* no more packets should be written for this stream */
419     int unavailable;                     /* true if the steram is unavailable (possibly temporarily) */
420     int stream_copy;
421     const char *attachment_filename;
422     int copy_initial_nonkeyframes;
423     int copy_prior_start;
424
425     int keep_pix_fmt;
426
427     AVCodecParserContext *parser;
428
429     /* stats */
430     // combined size of all the packets written
431     uint64_t data_size;
432     // number of packets send to the muxer
433     uint64_t packets_written;
434     // number of frames/samples sent to the encoder
435     uint64_t frames_encoded;
436     uint64_t samples_encoded;
437 } OutputStream;
438
439 typedef struct OutputFile {
440     AVFormatContext *ctx;
441     AVDictionary *opts;
442     int ost_index;       /* index of the first stream in output_streams */
443     int64_t recording_time;  ///< desired length of the resulting file in microseconds == AV_TIME_BASE units
444     int64_t start_time;      ///< start time in microseconds == AV_TIME_BASE units
445     uint64_t limit_filesize; /* filesize limit expressed in bytes */
446
447     int shortest;
448 } OutputFile;
449
450 extern InputStream **input_streams;
451 extern int        nb_input_streams;
452 extern InputFile   **input_files;
453 extern int        nb_input_files;
454
455 extern OutputStream **output_streams;
456 extern int         nb_output_streams;
457 extern OutputFile   **output_files;
458 extern int         nb_output_files;
459
460 extern FilterGraph **filtergraphs;
461 extern int        nb_filtergraphs;
462
463 extern char *vstats_filename;
464
465 extern float audio_drift_threshold;
466 extern float dts_delta_threshold;
467 extern float dts_error_threshold;
468
469 extern int audio_volume;
470 extern int audio_sync_method;
471 extern int video_sync_method;
472 extern int do_benchmark;
473 extern int do_benchmark_all;
474 extern int do_deinterlace;
475 extern int do_hex_dump;
476 extern int do_pkt_dump;
477 extern int copy_ts;
478 extern int copy_tb;
479 extern int debug_ts;
480 extern int exit_on_error;
481 extern int print_stats;
482 extern int qp_hist;
483 extern int stdin_interaction;
484 extern int frame_bits_per_raw_sample;
485 extern AVIOContext *progress_avio;
486 extern float max_error_rate;
487
488 extern const AVIOInterruptCB int_cb;
489
490 extern const OptionDef options[];
491 extern const HWAccel hwaccels[];
492
493
494 void term_init(void);
495 void term_exit(void);
496
497 void reset_options(OptionsContext *o, int is_input);
498 void show_usage(void);
499
500 void opt_output_file(void *optctx, const char *filename);
501
502 void assert_avoptions(AVDictionary *m);
503
504 int guess_input_channel_layout(InputStream *ist);
505
506 enum AVPixelFormat choose_pixel_fmt(AVStream *st, AVCodec *codec, enum AVPixelFormat target);
507 void choose_sample_fmt(AVStream *st, AVCodec *codec);
508
509 int configure_filtergraph(FilterGraph *fg);
510 int configure_output_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out);
511 int ist_in_filtergraph(FilterGraph *fg, InputStream *ist);
512 FilterGraph *init_simple_filtergraph(InputStream *ist, OutputStream *ost);
513
514 int ffmpeg_parse_options(int argc, char **argv);
515
516 int vdpau_init(AVCodecContext *s);
517 int dxva2_init(AVCodecContext *s);
518 int vda_init(AVCodecContext *s);
519
520 #endif /* FFMPEG_H */