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