OSDN Git Service

avcodec/scpr: improve motion vectors checking for out of buffer write
[android-x86/external-ffmpeg.git] / 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 *max_muxing_queue_size;
216     int        nb_max_muxing_queue_size;
217     SpecifierOpt *guess_layout_max;
218     int        nb_guess_layout_max;
219     SpecifierOpt *apad;
220     int        nb_apad;
221     SpecifierOpt *discard;
222     int        nb_discard;
223     SpecifierOpt *disposition;
224     int        nb_disposition;
225     SpecifierOpt *program;
226     int        nb_program;
227     SpecifierOpt *time_bases;
228     int        nb_time_bases;
229 } OptionsContext;
230
231 typedef struct InputFilter {
232     AVFilterContext    *filter;
233     struct InputStream *ist;
234     struct FilterGraph *graph;
235     uint8_t            *name;
236
237     // parameters configured for this input
238     int format;
239
240     int width, height;
241     AVRational sample_aspect_ratio;
242
243     int sample_rate;
244     int channels;
245     uint64_t channel_layout;
246
247     AVBufferRef *hw_frames_ctx;
248 } InputFilter;
249
250 typedef struct OutputFilter {
251     AVFilterContext     *filter;
252     struct OutputStream *ost;
253     struct FilterGraph  *graph;
254     uint8_t             *name;
255
256     /* temporary storage until stream maps are processed */
257     AVFilterInOut       *out_tmp;
258     enum AVMediaType     type;
259
260     /* desired output stream properties */
261     int width, height;
262     AVRational frame_rate;
263     int format;
264     int sample_rate;
265     uint64_t channel_layout;
266
267     // those are only set if no format is specified and the encoder gives us multiple options
268     int *formats;
269     uint64_t *channel_layouts;
270     int *sample_rates;
271 } OutputFilter;
272
273 typedef struct FilterGraph {
274     int            index;
275     const char    *graph_desc;
276
277     AVFilterGraph *graph;
278     int reconfiguration;
279
280     InputFilter   **inputs;
281     int          nb_inputs;
282     OutputFilter **outputs;
283     int         nb_outputs;
284 } FilterGraph;
285
286 typedef struct InputStream {
287     int file_index;
288     AVStream *st;
289     int discard;             /* true if stream data should be discarded */
290     int user_set_discard;
291     int decoding_needed;     /* non zero if the packets must be decoded in 'raw_fifo', see DECODING_FOR_* */
292 #define DECODING_FOR_OST    1
293 #define DECODING_FOR_FILTER 2
294
295     AVCodecContext *dec_ctx;
296     AVCodec *dec;
297     AVFrame *decoded_frame;
298     AVFrame *filter_frame; /* a ref of decoded_frame, to be sent to filters */
299
300     int64_t       start;     /* time when read started */
301     /* predicted dts of the next packet read for this stream or (when there are
302      * several frames in a packet) of the next frame in current packet (in AV_TIME_BASE units) */
303     int64_t       next_dts;
304     int64_t       dts;       ///< dts of the last packet read for this stream (in AV_TIME_BASE units)
305
306     int64_t       next_pts;  ///< synthetic pts for the next decode frame (in AV_TIME_BASE units)
307     int64_t       pts;       ///< current pts of the decoded frame  (in AV_TIME_BASE units)
308     int           wrap_correction_done;
309
310     int64_t filter_in_rescale_delta_last;
311
312     int64_t min_pts; /* pts with the smallest value in a current stream */
313     int64_t max_pts; /* pts with the higher value in a current stream */
314     int64_t nb_samples; /* number of samples in the last decoded audio frame before looping */
315
316     double ts_scale;
317     int saw_first_ts;
318     AVDictionary *decoder_opts;
319     AVRational framerate;               /* framerate forced with -r */
320     int top_field_first;
321     int guess_layout_max;
322
323     int autorotate;
324     int resample_height;
325     int resample_width;
326     int resample_pix_fmt;
327
328     int      resample_sample_fmt;
329     int      resample_sample_rate;
330     int      resample_channels;
331     uint64_t resample_channel_layout;
332
333     int fix_sub_duration;
334     struct { /* previous decoded subtitle and related variables */
335         int got_output;
336         int ret;
337         AVSubtitle subtitle;
338     } prev_sub;
339
340     struct sub2video {
341         int64_t last_pts;
342         int64_t end_pts;
343         AVFrame *frame;
344         int w, h;
345     } sub2video;
346
347     int dr1;
348
349     /* decoded data from this stream goes into all those filters
350      * currently video and audio only */
351     InputFilter **filters;
352     int        nb_filters;
353
354     int reinit_filters;
355
356     /* hwaccel options */
357     enum HWAccelID hwaccel_id;
358     char  *hwaccel_device;
359     enum AVPixelFormat hwaccel_output_format;
360
361     /* hwaccel context */
362     enum HWAccelID active_hwaccel_id;
363     void  *hwaccel_ctx;
364     void (*hwaccel_uninit)(AVCodecContext *s);
365     int  (*hwaccel_get_buffer)(AVCodecContext *s, AVFrame *frame, int flags);
366     int  (*hwaccel_retrieve_data)(AVCodecContext *s, AVFrame *frame);
367     enum AVPixelFormat hwaccel_pix_fmt;
368     enum AVPixelFormat hwaccel_retrieved_pix_fmt;
369     AVBufferRef *hw_frames_ctx;
370
371     /* stats */
372     // combined size of all the packets read
373     uint64_t data_size;
374     /* number of packets successfully read for this stream */
375     uint64_t nb_packets;
376     // number of frames/samples retrieved from the decoder
377     uint64_t frames_decoded;
378     uint64_t samples_decoded;
379
380     int64_t *dts_buffer;
381     int nb_dts_buffer;
382 } InputStream;
383
384 typedef struct InputFile {
385     AVFormatContext *ctx;
386     int eof_reached;      /* true if eof reached */
387     int eagain;           /* true if last read attempt returned EAGAIN */
388     int ist_index;        /* index of first stream in input_streams */
389     int loop;             /* set number of times input stream should be looped */
390     int64_t duration;     /* actual duration of the longest stream in a file
391                              at the moment when looping happens */
392     AVRational time_base; /* time base of the duration */
393     int64_t input_ts_offset;
394
395     int64_t ts_offset;
396     int64_t last_ts;
397     int64_t start_time;   /* user-specified start time in AV_TIME_BASE or AV_NOPTS_VALUE */
398     int seek_timestamp;
399     int64_t recording_time;
400     int nb_streams;       /* number of stream that ffmpeg is aware of; may be different
401                              from ctx.nb_streams if new streams appear during av_read_frame() */
402     int nb_streams_warn;  /* number of streams that the user was warned of */
403     int rate_emu;
404     int accurate_seek;
405
406 #if HAVE_PTHREADS
407     AVThreadMessageQueue *in_thread_queue;
408     pthread_t thread;           /* thread reading from this file */
409     int non_blocking;           /* reading packets from the thread should not block */
410     int joined;                 /* the thread has been joined */
411     int thread_queue_size;      /* maximum number of queued packets */
412 #endif
413 } InputFile;
414
415 enum forced_keyframes_const {
416     FKF_N,
417     FKF_N_FORCED,
418     FKF_PREV_FORCED_N,
419     FKF_PREV_FORCED_T,
420     FKF_T,
421     FKF_NB
422 };
423
424 #define ABORT_ON_FLAG_EMPTY_OUTPUT (1 <<  0)
425
426 extern const char *const forced_keyframes_const_names[];
427
428 typedef enum {
429     ENCODER_FINISHED = 1,
430     MUXER_FINISHED = 2,
431 } OSTFinished ;
432
433 typedef struct OutputStream {
434     int file_index;          /* file index */
435     int index;               /* stream index in the output file */
436     int source_index;        /* InputStream index */
437     AVStream *st;            /* stream in the output file */
438     int encoding_needed;     /* true if encoding needed for this stream */
439     int frame_number;
440     /* input pts and corresponding output pts
441        for A/V sync */
442     struct InputStream *sync_ist; /* input stream to sync against */
443     int64_t sync_opts;       /* output frame counter, could be changed to some true timestamp */ // FIXME look at frame_number
444     /* pts of the first frame encoded for this stream, used for limiting
445      * recording time */
446     int64_t first_pts;
447     /* dts of the last packet sent to the muxer */
448     int64_t last_mux_dts;
449
450     int                    nb_bitstream_filters;
451     uint8_t                  *bsf_extradata_updated;
452     AVBSFContext            **bsf_ctx;
453
454     AVCodecContext *enc_ctx;
455     AVCodecParameters *ref_par; /* associated input codec parameters with encoders options applied */
456     AVCodec *enc;
457     int64_t max_frames;
458     AVFrame *filtered_frame;
459     AVFrame *last_frame;
460     int last_dropped;
461     int last_nb0_frames[3];
462
463     void  *hwaccel_ctx;
464
465     /* video only */
466     AVRational frame_rate;
467     int is_cfr;
468     int force_fps;
469     int top_field_first;
470     int rotate_overridden;
471
472     AVRational frame_aspect_ratio;
473
474     /* forced key frames */
475     int64_t *forced_kf_pts;
476     int forced_kf_count;
477     int forced_kf_index;
478     char *forced_keyframes;
479     AVExpr *forced_keyframes_pexpr;
480     double forced_keyframes_expr_const_values[FKF_NB];
481
482     /* audio only */
483     int *audio_channels_map;             /* list of the channels id to pick from the source stream */
484     int audio_channels_mapped;           /* number of channels in audio_channels_map */
485
486     char *logfile_prefix;
487     FILE *logfile;
488
489     OutputFilter *filter;
490     char *avfilter;
491     char *filters;         ///< filtergraph associated to the -filter option
492     char *filters_script;  ///< filtergraph script associated to the -filter_script option
493
494     AVDictionary *encoder_opts;
495     AVDictionary *sws_dict;
496     AVDictionary *swr_opts;
497     AVDictionary *resample_opts;
498     char *apad;
499     OSTFinished finished;        /* no more packets should be written for this stream */
500     int unavailable;                     /* true if the steram is unavailable (possibly temporarily) */
501     int stream_copy;
502
503     // init_output_stream() has been called for this stream
504     // The encoder and the bitstream filters have been initialized and the stream
505     // parameters are set in the AVStream.
506     int initialized;
507
508     const char *attachment_filename;
509     int copy_initial_nonkeyframes;
510     int copy_prior_start;
511     char *disposition;
512
513     int keep_pix_fmt;
514
515     AVCodecParserContext *parser;
516     AVCodecContext       *parser_avctx;
517
518     /* stats */
519     // combined size of all the packets written
520     uint64_t data_size;
521     // number of packets send to the muxer
522     uint64_t packets_written;
523     // number of frames/samples sent to the encoder
524     uint64_t frames_encoded;
525     uint64_t samples_encoded;
526
527     /* packet quality factor */
528     int quality;
529
530     int max_muxing_queue_size;
531
532     /* the packets are buffered here until the muxer is ready to be initialized */
533     AVFifoBuffer *muxing_queue;
534
535     /* packet picture type */
536     int pict_type;
537
538     /* frame encode sum of squared error values */
539     int64_t error[4];
540 } OutputStream;
541
542 typedef struct OutputFile {
543     AVFormatContext *ctx;
544     AVDictionary *opts;
545     int ost_index;       /* index of the first stream in output_streams */
546     int64_t recording_time;  ///< desired length of the resulting file in microseconds == AV_TIME_BASE units
547     int64_t start_time;      ///< start time in microseconds == AV_TIME_BASE units
548     uint64_t limit_filesize; /* filesize limit expressed in bytes */
549
550     int shortest;
551
552     int header_written;
553 } OutputFile;
554
555 extern InputStream **input_streams;
556 extern int        nb_input_streams;
557 extern InputFile   **input_files;
558 extern int        nb_input_files;
559
560 extern OutputStream **output_streams;
561 extern int         nb_output_streams;
562 extern OutputFile   **output_files;
563 extern int         nb_output_files;
564
565 extern FilterGraph **filtergraphs;
566 extern int        nb_filtergraphs;
567
568 extern char *vstats_filename;
569 extern char *sdp_filename;
570
571 extern float audio_drift_threshold;
572 extern float dts_delta_threshold;
573 extern float dts_error_threshold;
574
575 extern int audio_volume;
576 extern int audio_sync_method;
577 extern int video_sync_method;
578 extern float frame_drop_threshold;
579 extern int do_benchmark;
580 extern int do_benchmark_all;
581 extern int do_deinterlace;
582 extern int do_hex_dump;
583 extern int do_pkt_dump;
584 extern int copy_ts;
585 extern int start_at_zero;
586 extern int copy_tb;
587 extern int debug_ts;
588 extern int exit_on_error;
589 extern int abort_on_flags;
590 extern int print_stats;
591 extern int qp_hist;
592 extern int stdin_interaction;
593 extern int frame_bits_per_raw_sample;
594 extern AVIOContext *progress_avio;
595 extern float max_error_rate;
596 extern char *videotoolbox_pixfmt;
597
598 extern int filter_nbthreads;
599 extern int filter_complex_nbthreads;
600 extern int vstats_version;
601
602 extern const AVIOInterruptCB int_cb;
603
604 extern const OptionDef options[];
605 extern const HWAccel hwaccels[];
606 extern int hwaccel_lax_profile_check;
607 extern AVBufferRef *hw_device_ctx;
608 #if CONFIG_QSV
609 extern char *qsv_device;
610 #endif
611
612
613 void term_init(void);
614 void term_exit(void);
615
616 void reset_options(OptionsContext *o, int is_input);
617 void show_usage(void);
618
619 void opt_output_file(void *optctx, const char *filename);
620
621 void remove_avoptions(AVDictionary **a, AVDictionary *b);
622 void assert_avoptions(AVDictionary *m);
623
624 int guess_input_channel_layout(InputStream *ist);
625
626 enum AVPixelFormat choose_pixel_fmt(AVStream *st, AVCodecContext *avctx, AVCodec *codec, enum AVPixelFormat target);
627 void choose_sample_fmt(AVStream *st, AVCodec *codec);
628
629 int configure_filtergraph(FilterGraph *fg);
630 int configure_output_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out);
631 int ist_in_filtergraph(FilterGraph *fg, InputStream *ist);
632 int filtergraph_is_simple(FilterGraph *fg);
633 int init_simple_filtergraph(InputStream *ist, OutputStream *ost);
634 int init_complex_filtergraph(FilterGraph *fg);
635
636 int ifilter_parameters_from_frame(InputFilter *ifilter, const AVFrame *frame);
637 int ifilter_parameters_from_decoder(InputFilter *ifilter, const AVCodecContext *avctx);
638
639 int ffmpeg_parse_options(int argc, char **argv);
640
641 int vdpau_init(AVCodecContext *s);
642 int dxva2_init(AVCodecContext *s);
643 int vda_init(AVCodecContext *s);
644 int videotoolbox_init(AVCodecContext *s);
645 int qsv_init(AVCodecContext *s);
646 int qsv_transcode_init(OutputStream *ost);
647 int vaapi_decode_init(AVCodecContext *avctx);
648 int vaapi_device_init(const char *device);
649 int cuvid_init(AVCodecContext *s);
650 int cuvid_transcode_init(OutputStream *ost);
651
652 #endif /* FFMPEG_H */