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