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