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