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