]> git.sesse.net Git - ffmpeg/blob - ffmpeg.h
Merge commit '07fd0a22192805d56c635eb294dc26b0a54ae325'
[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 };
63
64 typedef struct HWAccel {
65     const char *name;
66     int (*init)(AVCodecContext *s);
67     enum HWAccelID id;
68     enum AVPixelFormat pix_fmt;
69 } HWAccel;
70
71 /* select an input stream for an output stream */
72 typedef struct StreamMap {
73     int disabled;           /* 1 is this mapping is disabled by a negative map */
74     int file_index;
75     int stream_index;
76     int sync_file_index;
77     int sync_stream_index;
78     char *linklabel;       /* name of an output link, for mapping lavfi outputs */
79 } StreamMap;
80
81 typedef struct {
82     int  file_idx,  stream_idx,  channel_idx; // input
83     int ofile_idx, ostream_idx;               // output
84 } AudioChannelMap;
85
86 typedef struct OptionsContext {
87     OptionGroup *g;
88
89     /* input/output options */
90     int64_t start_time;
91     const char *format;
92
93     SpecifierOpt *codec_names;
94     int        nb_codec_names;
95     SpecifierOpt *audio_channels;
96     int        nb_audio_channels;
97     SpecifierOpt *audio_sample_rate;
98     int        nb_audio_sample_rate;
99     SpecifierOpt *frame_rates;
100     int        nb_frame_rates;
101     SpecifierOpt *frame_sizes;
102     int        nb_frame_sizes;
103     SpecifierOpt *frame_pix_fmts;
104     int        nb_frame_pix_fmts;
105
106     /* input options */
107     int64_t input_ts_offset;
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
120     /* output options */
121     StreamMap *stream_maps;
122     int     nb_stream_maps;
123     AudioChannelMap *audio_channel_maps; /* one info entry per -map_channel */
124     int           nb_audio_channel_maps; /* number of (valid) -map_channel settings */
125     int metadata_global_manual;
126     int metadata_streams_manual;
127     int metadata_chapters_manual;
128     const char **attachments;
129     int       nb_attachments;
130
131     int chapters_input_file;
132
133     int64_t recording_time;
134     int64_t stop_time;
135     uint64_t limit_filesize;
136     float mux_preload;
137     float mux_max_delay;
138     int shortest;
139
140     int video_disable;
141     int audio_disable;
142     int subtitle_disable;
143     int data_disable;
144
145     /* indexed by output file stream index */
146     int   *streamid_map;
147     int nb_streamid_map;
148
149     SpecifierOpt *metadata;
150     int        nb_metadata;
151     SpecifierOpt *max_frames;
152     int        nb_max_frames;
153     SpecifierOpt *bitstream_filters;
154     int        nb_bitstream_filters;
155     SpecifierOpt *codec_tags;
156     int        nb_codec_tags;
157     SpecifierOpt *sample_fmts;
158     int        nb_sample_fmts;
159     SpecifierOpt *qscale;
160     int        nb_qscale;
161     SpecifierOpt *forced_key_frames;
162     int        nb_forced_key_frames;
163     SpecifierOpt *force_fps;
164     int        nb_force_fps;
165     SpecifierOpt *frame_aspect_ratios;
166     int        nb_frame_aspect_ratios;
167     SpecifierOpt *rc_overrides;
168     int        nb_rc_overrides;
169     SpecifierOpt *intra_matrices;
170     int        nb_intra_matrices;
171     SpecifierOpt *inter_matrices;
172     int        nb_inter_matrices;
173     SpecifierOpt *top_field_first;
174     int        nb_top_field_first;
175     SpecifierOpt *metadata_map;
176     int        nb_metadata_map;
177     SpecifierOpt *presets;
178     int        nb_presets;
179     SpecifierOpt *copy_initial_nonkeyframes;
180     int        nb_copy_initial_nonkeyframes;
181     SpecifierOpt *copy_prior_start;
182     int        nb_copy_prior_start;
183     SpecifierOpt *filters;
184     int        nb_filters;
185     SpecifierOpt *filter_scripts;
186     int        nb_filter_scripts;
187     SpecifierOpt *reinit_filters;
188     int        nb_reinit_filters;
189     SpecifierOpt *fix_sub_duration;
190     int        nb_fix_sub_duration;
191     SpecifierOpt *canvas_sizes;
192     int        nb_canvas_sizes;
193     SpecifierOpt *pass;
194     int        nb_pass;
195     SpecifierOpt *passlogfiles;
196     int        nb_passlogfiles;
197     SpecifierOpt *guess_layout_max;
198     int        nb_guess_layout_max;
199     SpecifierOpt *apad;
200     int        nb_apad;
201 } OptionsContext;
202
203 typedef struct InputFilter {
204     AVFilterContext    *filter;
205     struct InputStream *ist;
206     struct FilterGraph *graph;
207     uint8_t            *name;
208 } InputFilter;
209
210 typedef struct OutputFilter {
211     AVFilterContext     *filter;
212     struct OutputStream *ost;
213     struct FilterGraph  *graph;
214     uint8_t             *name;
215
216     /* temporary storage until stream maps are processed */
217     AVFilterInOut       *out_tmp;
218 } OutputFilter;
219
220 typedef struct FilterGraph {
221     int            index;
222     const char    *graph_desc;
223
224     AVFilterGraph *graph;
225     int reconfiguration;
226
227     InputFilter   **inputs;
228     int          nb_inputs;
229     OutputFilter **outputs;
230     int         nb_outputs;
231 } FilterGraph;
232
233 typedef struct InputStream {
234     int file_index;
235     AVStream *st;
236     int discard;             /* true if stream data should be discarded */
237     int decoding_needed;     /* true if the packets must be decoded in 'raw_fifo' */
238     AVCodec *dec;
239     AVFrame *decoded_frame;
240     AVFrame *filter_frame; /* a ref of decoded_frame, to be sent to filters */
241
242     int64_t       start;     /* time when read started */
243     /* predicted dts of the next packet read for this stream or (when there are
244      * several frames in a packet) of the next frame in current packet (in AV_TIME_BASE units) */
245     int64_t       next_dts;
246     int64_t       dts;       ///< dts of the last packet read for this stream (in AV_TIME_BASE units)
247
248     int64_t       next_pts;  ///< synthetic pts for the next decode frame (in AV_TIME_BASE units)
249     int64_t       pts;       ///< current pts of the decoded frame  (in AV_TIME_BASE units)
250     int           wrap_correction_done;
251
252     int64_t filter_in_rescale_delta_last;
253
254     double ts_scale;
255     int is_start;            /* is 1 at the start and after a discontinuity */
256     int saw_first_ts;
257     int showed_multi_packet_warning;
258     AVDictionary *opts;
259     AVRational framerate;               /* framerate forced with -r */
260     int top_field_first;
261     int guess_layout_max;
262
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
272     int fix_sub_duration;
273     struct { /* previous decoded subtitle and related variables */
274         int got_output;
275         int ret;
276         AVSubtitle subtitle;
277     } prev_sub;
278
279     struct sub2video {
280         int64_t last_pts;
281         int64_t end_pts;
282         AVFrame *frame;
283         int w, h;
284     } sub2video;
285
286     int dr1;
287
288     /* decoded data from this stream goes into all those filters
289      * currently video and audio only */
290     InputFilter **filters;
291     int        nb_filters;
292
293     int reinit_filters;
294
295     /* hwaccel options */
296     enum HWAccelID hwaccel_id;
297     char  *hwaccel_device;
298
299     /* hwaccel context */
300     enum HWAccelID active_hwaccel_id;
301     void  *hwaccel_ctx;
302     void (*hwaccel_uninit)(AVCodecContext *s);
303     int  (*hwaccel_get_buffer)(AVCodecContext *s, AVFrame *frame, int flags);
304     int  (*hwaccel_retrieve_data)(AVCodecContext *s, AVFrame *frame);
305     enum AVPixelFormat hwaccel_pix_fmt;
306     enum AVPixelFormat hwaccel_retrieved_pix_fmt;
307 } InputStream;
308
309 typedef struct InputFile {
310     AVFormatContext *ctx;
311     int eof_reached;      /* true if eof reached */
312     int eagain;           /* true if last read attempt returned EAGAIN */
313     int ist_index;        /* index of first stream in input_streams */
314     int64_t input_ts_offset;
315     int64_t ts_offset;
316     int64_t last_ts;
317     int64_t start_time;   /* user-specified start time in AV_TIME_BASE or AV_NOPTS_VALUE */
318     int64_t recording_time;
319     int nb_streams;       /* number of stream that ffmpeg is aware of; may be different
320                              from ctx.nb_streams if new streams appear during av_read_frame() */
321     int nb_streams_warn;  /* number of streams that the user was warned of */
322     int rate_emu;
323     int accurate_seek;
324
325 #if HAVE_PTHREADS
326     pthread_t thread;           /* thread reading from this file */
327     int finished;               /* the thread has exited */
328     int joined;                 /* the thread has been joined */
329     pthread_mutex_t fifo_lock;  /* lock for access to fifo */
330     pthread_cond_t  fifo_cond;  /* the main thread will signal on this cond after reading from fifo */
331     AVFifoBuffer *fifo;         /* demuxed packets are stored here; freed by the main thread */
332 #endif
333 } InputFile;
334
335 enum forced_keyframes_const {
336     FKF_N,
337     FKF_N_FORCED,
338     FKF_PREV_FORCED_N,
339     FKF_PREV_FORCED_T,
340     FKF_T,
341     FKF_NB
342 };
343
344 extern const char *const forced_keyframes_const_names[];
345
346 typedef struct OutputStream {
347     int file_index;          /* file index */
348     int index;               /* stream index in the output file */
349     int source_index;        /* InputStream index */
350     AVStream *st;            /* stream in the output file */
351     int encoding_needed;     /* true if encoding needed for this stream */
352     int frame_number;
353     /* input pts and corresponding output pts
354        for A/V sync */
355     struct InputStream *sync_ist; /* input stream to sync against */
356     int64_t sync_opts;       /* output frame counter, could be changed to some true timestamp */ // FIXME look at frame_number
357     /* pts of the first frame encoded for this stream, used for limiting
358      * recording time */
359     int64_t first_pts;
360     /* dts of the last packet sent to the muxer */
361     int64_t last_mux_dts;
362     AVBitStreamFilterContext *bitstream_filters;
363     AVCodec *enc;
364     int64_t max_frames;
365     AVFrame *filtered_frame;
366
367     /* video only */
368     AVRational frame_rate;
369     int force_fps;
370     int top_field_first;
371
372     AVRational frame_aspect_ratio;
373
374     /* forced key frames */
375     int64_t *forced_kf_pts;
376     int forced_kf_count;
377     int forced_kf_index;
378     char *forced_keyframes;
379     AVExpr *forced_keyframes_pexpr;
380     double forced_keyframes_expr_const_values[FKF_NB];
381
382     /* audio only */
383     int audio_channels_map[SWR_CH_MAX];  /* list of the channels id to pick from the source stream */
384     int audio_channels_mapped;           /* number of channels in audio_channels_map */
385
386     char *logfile_prefix;
387     FILE *logfile;
388
389     OutputFilter *filter;
390     char *avfilter;
391     char *filters;         ///< filtergraph associated to the -filter option
392     char *filters_script;  ///< filtergraph script associated to the -filter_script option
393
394     int64_t sws_flags;
395     AVDictionary *opts;
396     AVDictionary *swr_opts;
397     AVDictionary *resample_opts;
398     char *apad;
399     int finished;        /* no more packets should be written for this stream */
400     int unavailable;                     /* true if the steram is unavailable (possibly temporarily) */
401     int stream_copy;
402     const char *attachment_filename;
403     int copy_initial_nonkeyframes;
404     int copy_prior_start;
405
406     int keep_pix_fmt;
407
408     AVCodecParserContext *parser;
409 } OutputStream;
410
411 typedef struct OutputFile {
412     AVFormatContext *ctx;
413     AVDictionary *opts;
414     int ost_index;       /* index of the first stream in output_streams */
415     int64_t recording_time;  ///< desired length of the resulting file in microseconds == AV_TIME_BASE units
416     int64_t start_time;      ///< start time in microseconds == AV_TIME_BASE units
417     uint64_t limit_filesize; /* filesize limit expressed in bytes */
418
419     int shortest;
420 } OutputFile;
421
422 extern InputStream **input_streams;
423 extern int        nb_input_streams;
424 extern InputFile   **input_files;
425 extern int        nb_input_files;
426
427 extern OutputStream **output_streams;
428 extern int         nb_output_streams;
429 extern OutputFile   **output_files;
430 extern int         nb_output_files;
431
432 extern FilterGraph **filtergraphs;
433 extern int        nb_filtergraphs;
434
435 extern char *vstats_filename;
436
437 extern float audio_drift_threshold;
438 extern float dts_delta_threshold;
439 extern float dts_error_threshold;
440
441 extern int audio_volume;
442 extern int audio_sync_method;
443 extern int video_sync_method;
444 extern int do_benchmark;
445 extern int do_benchmark_all;
446 extern int do_deinterlace;
447 extern int do_hex_dump;
448 extern int do_pkt_dump;
449 extern int copy_ts;
450 extern int copy_tb;
451 extern int debug_ts;
452 extern int exit_on_error;
453 extern int print_stats;
454 extern int qp_hist;
455 extern int stdin_interaction;
456 extern int frame_bits_per_raw_sample;
457 extern AVIOContext *progress_avio;
458 extern float max_error_rate;
459
460 extern const AVIOInterruptCB int_cb;
461
462 extern const OptionDef options[];
463 extern const HWAccel hwaccels[];
464
465
466 void term_init(void);
467 void term_exit(void);
468
469 void reset_options(OptionsContext *o, int is_input);
470 void show_usage(void);
471
472 void opt_output_file(void *optctx, const char *filename);
473
474 void assert_avoptions(AVDictionary *m);
475
476 int guess_input_channel_layout(InputStream *ist);
477
478 enum AVPixelFormat choose_pixel_fmt(AVStream *st, AVCodec *codec, enum AVPixelFormat target);
479 void choose_sample_fmt(AVStream *st, AVCodec *codec);
480
481 int configure_filtergraph(FilterGraph *fg);
482 int configure_output_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out);
483 int ist_in_filtergraph(FilterGraph *fg, InputStream *ist);
484 FilterGraph *init_simple_filtergraph(InputStream *ist, OutputStream *ost);
485
486 int ffmpeg_parse_options(int argc, char **argv);
487
488 #endif /* FFMPEG_H */