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