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