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