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