]> git.sesse.net Git - ffmpeg/blob - fftools/ffmpeg.h
avcodec/packet: change side data related public function and struct size types to...
[ffmpeg] / fftools / 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 FFTOOLS_FFMPEG_H
20 #define FFTOOLS_FFMPEG_H
21
22 #include "config.h"
23
24 #include <stdint.h>
25 #include <stdio.h>
26 #include <signal.h>
27
28 #include "cmdutils.h"
29
30 #include "libavformat/avformat.h"
31 #include "libavformat/avio.h"
32
33 #include "libavcodec/avcodec.h"
34
35 #include "libavfilter/avfilter.h"
36
37 #include "libavutil/avutil.h"
38 #include "libavutil/dict.h"
39 #include "libavutil/eval.h"
40 #include "libavutil/fifo.h"
41 #include "libavutil/hwcontext.h"
42 #include "libavutil/pixfmt.h"
43 #include "libavutil/rational.h"
44 #include "libavutil/thread.h"
45 #include "libavutil/threadmessage.h"
46
47 #include "libswresample/swresample.h"
48
49 #define VSYNC_AUTO       -1
50 #define VSYNC_PASSTHROUGH 0
51 #define VSYNC_CFR         1
52 #define VSYNC_VFR         2
53 #define VSYNC_VSCFR       0xfe
54 #define VSYNC_DROP        0xff
55
56 #define MAX_STREAMS 1024    /* arbitrary sanity check value */
57
58 enum HWAccelID {
59     HWACCEL_NONE = 0,
60     HWACCEL_AUTO,
61     HWACCEL_GENERIC,
62     HWACCEL_VIDEOTOOLBOX,
63     HWACCEL_QSV,
64 };
65
66 typedef struct HWAccel {
67     const char *name;
68     int (*init)(AVCodecContext *s);
69     enum HWAccelID id;
70     enum AVPixelFormat pix_fmt;
71 } HWAccel;
72
73 typedef struct HWDevice {
74     const char *name;
75     enum AVHWDeviceType type;
76     AVBufferRef *device_ref;
77 } HWDevice;
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 *max_frame_rates;
112     int        nb_max_frame_rates;
113     SpecifierOpt *frame_sizes;
114     int        nb_frame_sizes;
115     SpecifierOpt *frame_pix_fmts;
116     int        nb_frame_pix_fmts;
117
118     /* input options */
119     int64_t input_ts_offset;
120     int loop;
121     int rate_emu;
122     int accurate_seek;
123     int thread_queue_size;
124
125     SpecifierOpt *ts_scale;
126     int        nb_ts_scale;
127     SpecifierOpt *dump_attachment;
128     int        nb_dump_attachment;
129     SpecifierOpt *hwaccels;
130     int        nb_hwaccels;
131     SpecifierOpt *hwaccel_devices;
132     int        nb_hwaccel_devices;
133     SpecifierOpt *hwaccel_output_formats;
134     int        nb_hwaccel_output_formats;
135     SpecifierOpt *autorotate;
136     int        nb_autorotate;
137
138     /* output options */
139     StreamMap *stream_maps;
140     int     nb_stream_maps;
141     AudioChannelMap *audio_channel_maps; /* one info entry per -map_channel */
142     int           nb_audio_channel_maps; /* number of (valid) -map_channel settings */
143     int metadata_global_manual;
144     int metadata_streams_manual;
145     int metadata_chapters_manual;
146     const char **attachments;
147     int       nb_attachments;
148
149     int chapters_input_file;
150
151     int64_t recording_time;
152     int64_t stop_time;
153     uint64_t limit_filesize;
154     float mux_preload;
155     float mux_max_delay;
156     int shortest;
157     int bitexact;
158
159     int video_disable;
160     int audio_disable;
161     int subtitle_disable;
162     int data_disable;
163
164     /* indexed by output file stream index */
165     int   *streamid_map;
166     int nb_streamid_map;
167
168     SpecifierOpt *metadata;
169     int        nb_metadata;
170     SpecifierOpt *max_frames;
171     int        nb_max_frames;
172     SpecifierOpt *bitstream_filters;
173     int        nb_bitstream_filters;
174     SpecifierOpt *codec_tags;
175     int        nb_codec_tags;
176     SpecifierOpt *sample_fmts;
177     int        nb_sample_fmts;
178     SpecifierOpt *qscale;
179     int        nb_qscale;
180     SpecifierOpt *forced_key_frames;
181     int        nb_forced_key_frames;
182     SpecifierOpt *force_fps;
183     int        nb_force_fps;
184     SpecifierOpt *frame_aspect_ratios;
185     int        nb_frame_aspect_ratios;
186     SpecifierOpt *rc_overrides;
187     int        nb_rc_overrides;
188     SpecifierOpt *intra_matrices;
189     int        nb_intra_matrices;
190     SpecifierOpt *inter_matrices;
191     int        nb_inter_matrices;
192     SpecifierOpt *chroma_intra_matrices;
193     int        nb_chroma_intra_matrices;
194     SpecifierOpt *top_field_first;
195     int        nb_top_field_first;
196     SpecifierOpt *metadata_map;
197     int        nb_metadata_map;
198     SpecifierOpt *presets;
199     int        nb_presets;
200     SpecifierOpt *copy_initial_nonkeyframes;
201     int        nb_copy_initial_nonkeyframes;
202     SpecifierOpt *copy_prior_start;
203     int        nb_copy_prior_start;
204     SpecifierOpt *filters;
205     int        nb_filters;
206     SpecifierOpt *filter_scripts;
207     int        nb_filter_scripts;
208     SpecifierOpt *reinit_filters;
209     int        nb_reinit_filters;
210     SpecifierOpt *fix_sub_duration;
211     int        nb_fix_sub_duration;
212     SpecifierOpt *canvas_sizes;
213     int        nb_canvas_sizes;
214     SpecifierOpt *pass;
215     int        nb_pass;
216     SpecifierOpt *passlogfiles;
217     int        nb_passlogfiles;
218     SpecifierOpt *max_muxing_queue_size;
219     int        nb_max_muxing_queue_size;
220     SpecifierOpt *muxing_queue_data_threshold;
221     int        nb_muxing_queue_data_threshold;
222     SpecifierOpt *guess_layout_max;
223     int        nb_guess_layout_max;
224     SpecifierOpt *apad;
225     int        nb_apad;
226     SpecifierOpt *discard;
227     int        nb_discard;
228     SpecifierOpt *disposition;
229     int        nb_disposition;
230     SpecifierOpt *program;
231     int        nb_program;
232     SpecifierOpt *time_bases;
233     int        nb_time_bases;
234     SpecifierOpt *enc_time_bases;
235     int        nb_enc_time_bases;
236     SpecifierOpt *autoscale;
237     int        nb_autoscale;
238 } OptionsContext;
239
240 typedef struct InputFilter {
241     AVFilterContext    *filter;
242     struct InputStream *ist;
243     struct FilterGraph *graph;
244     uint8_t            *name;
245     enum AVMediaType    type;   // AVMEDIA_TYPE_SUBTITLE for sub2video
246
247     AVFifoBuffer *frame_queue;
248
249     // parameters configured for this input
250     int format;
251
252     int width, height;
253     AVRational sample_aspect_ratio;
254
255     int sample_rate;
256     int channels;
257     uint64_t channel_layout;
258
259     AVBufferRef *hw_frames_ctx;
260
261     int eof;
262 } InputFilter;
263
264 typedef struct OutputFilter {
265     AVFilterContext     *filter;
266     struct OutputStream *ost;
267     struct FilterGraph  *graph;
268     uint8_t             *name;
269
270     /* temporary storage until stream maps are processed */
271     AVFilterInOut       *out_tmp;
272     enum AVMediaType     type;
273
274     /* desired output stream properties */
275     int width, height;
276     AVRational frame_rate;
277     int format;
278     int sample_rate;
279     uint64_t channel_layout;
280
281     // those are only set if no format is specified and the encoder gives us multiple options
282     int *formats;
283     uint64_t *channel_layouts;
284     int *sample_rates;
285 } OutputFilter;
286
287 typedef struct FilterGraph {
288     int            index;
289     const char    *graph_desc;
290
291     AVFilterGraph *graph;
292     int reconfiguration;
293
294     InputFilter   **inputs;
295     int          nb_inputs;
296     OutputFilter **outputs;
297     int         nb_outputs;
298 } FilterGraph;
299
300 typedef struct InputStream {
301     int file_index;
302     AVStream *st;
303     int discard;             /* true if stream data should be discarded */
304     int user_set_discard;
305     int decoding_needed;     /* non zero if the packets must be decoded in 'raw_fifo', see DECODING_FOR_* */
306 #define DECODING_FOR_OST    1
307 #define DECODING_FOR_FILTER 2
308
309     AVCodecContext *dec_ctx;
310     const AVCodec *dec;
311     AVFrame *decoded_frame;
312     AVFrame *filter_frame; /* a ref of decoded_frame, to be sent to filters */
313
314     int64_t       start;     /* time when read started */
315     /* predicted dts of the next packet read for this stream or (when there are
316      * several frames in a packet) of the next frame in current packet (in AV_TIME_BASE units) */
317     int64_t       next_dts;
318     int64_t       dts;       ///< dts of the last packet read for this stream (in AV_TIME_BASE units)
319
320     int64_t       next_pts;  ///< synthetic pts for the next decode frame (in AV_TIME_BASE units)
321     int64_t       pts;       ///< current pts of the decoded frame  (in AV_TIME_BASE units)
322     int           wrap_correction_done;
323
324     int64_t filter_in_rescale_delta_last;
325
326     int64_t min_pts; /* pts with the smallest value in a current stream */
327     int64_t max_pts; /* pts with the higher value in a current stream */
328
329     // when forcing constant input framerate through -r,
330     // this contains the pts that will be given to the next decoded frame
331     int64_t cfr_next_pts;
332
333     int64_t nb_samples; /* number of samples in the last decoded audio frame before looping */
334
335     double ts_scale;
336     int saw_first_ts;
337     AVDictionary *decoder_opts;
338     AVRational framerate;               /* framerate forced with -r */
339     int top_field_first;
340     int guess_layout_max;
341
342     int autorotate;
343
344     int fix_sub_duration;
345     struct { /* previous decoded subtitle and related variables */
346         int got_output;
347         int ret;
348         AVSubtitle subtitle;
349     } prev_sub;
350
351     struct sub2video {
352         int64_t last_pts;
353         int64_t end_pts;
354         AVFifoBuffer *sub_queue;    ///< queue of AVSubtitle* before filter init
355         AVFrame *frame;
356         int w, h;
357         unsigned int initialize; ///< marks if sub2video_update should force an initialization
358     } sub2video;
359
360     int dr1;
361
362     /* decoded data from this stream goes into all those filters
363      * currently video and audio only */
364     InputFilter **filters;
365     int        nb_filters;
366
367     int reinit_filters;
368
369     /* hwaccel options */
370     enum HWAccelID hwaccel_id;
371     enum AVHWDeviceType hwaccel_device_type;
372     char  *hwaccel_device;
373     enum AVPixelFormat hwaccel_output_format;
374
375     /* hwaccel context */
376     void  *hwaccel_ctx;
377     void (*hwaccel_uninit)(AVCodecContext *s);
378     int  (*hwaccel_get_buffer)(AVCodecContext *s, AVFrame *frame, int flags);
379     int  (*hwaccel_retrieve_data)(AVCodecContext *s, AVFrame *frame);
380     enum AVPixelFormat hwaccel_pix_fmt;
381     enum AVPixelFormat hwaccel_retrieved_pix_fmt;
382     AVBufferRef *hw_frames_ctx;
383
384     /* stats */
385     // combined size of all the packets read
386     uint64_t data_size;
387     /* number of packets successfully read for this stream */
388     uint64_t nb_packets;
389     // number of frames/samples retrieved from the decoder
390     uint64_t frames_decoded;
391     uint64_t samples_decoded;
392
393     int64_t *dts_buffer;
394     int nb_dts_buffer;
395
396     int got_output;
397 } InputStream;
398
399 typedef struct InputFile {
400     AVFormatContext *ctx;
401     int eof_reached;      /* true if eof reached */
402     int eagain;           /* true if last read attempt returned EAGAIN */
403     int ist_index;        /* index of first stream in input_streams */
404     int loop;             /* set number of times input stream should be looped */
405     int64_t duration;     /* actual duration of the longest stream in a file
406                              at the moment when looping happens */
407     AVRational time_base; /* time base of the duration */
408     int64_t input_ts_offset;
409
410     int64_t ts_offset;
411     int64_t last_ts;
412     int64_t start_time;   /* user-specified start time in AV_TIME_BASE or AV_NOPTS_VALUE */
413     int seek_timestamp;
414     int64_t recording_time;
415     int nb_streams;       /* number of stream that ffmpeg is aware of; may be different
416                              from ctx.nb_streams if new streams appear during av_read_frame() */
417     int nb_streams_warn;  /* number of streams that the user was warned of */
418     int rate_emu;
419     int accurate_seek;
420
421 #if HAVE_THREADS
422     AVThreadMessageQueue *in_thread_queue;
423     pthread_t thread;           /* thread reading from this file */
424     int non_blocking;           /* reading packets from the thread should not block */
425     int joined;                 /* the thread has been joined */
426     int thread_queue_size;      /* maximum number of queued packets */
427 #endif
428 } InputFile;
429
430 enum forced_keyframes_const {
431     FKF_N,
432     FKF_N_FORCED,
433     FKF_PREV_FORCED_N,
434     FKF_PREV_FORCED_T,
435     FKF_T,
436     FKF_NB
437 };
438
439 #define ABORT_ON_FLAG_EMPTY_OUTPUT        (1 <<  0)
440 #define ABORT_ON_FLAG_EMPTY_OUTPUT_STREAM (1 <<  1)
441
442 extern const char *const forced_keyframes_const_names[];
443
444 typedef enum {
445     ENCODER_FINISHED = 1,
446     MUXER_FINISHED = 2,
447 } OSTFinished ;
448
449 typedef struct OutputStream {
450     int file_index;          /* file index */
451     int index;               /* stream index in the output file */
452     int source_index;        /* InputStream index */
453     AVStream *st;            /* stream in the output file */
454     int encoding_needed;     /* true if encoding needed for this stream */
455     int frame_number;
456     /* input pts and corresponding output pts
457        for A/V sync */
458     struct InputStream *sync_ist; /* input stream to sync against */
459     int64_t sync_opts;       /* output frame counter, could be changed to some true timestamp */ // FIXME look at frame_number
460     /* pts of the first frame encoded for this stream, used for limiting
461      * recording time */
462     int64_t first_pts;
463     /* dts of the last packet sent to the muxer */
464     int64_t last_mux_dts;
465     // the timebase of the packets sent to the muxer
466     AVRational mux_timebase;
467     AVRational enc_timebase;
468
469     AVBSFContext            *bsf_ctx;
470
471     AVCodecContext *enc_ctx;
472     AVCodecParameters *ref_par; /* associated input codec parameters with encoders options applied */
473     const AVCodec *enc;
474     int64_t max_frames;
475     AVFrame *filtered_frame;
476     AVFrame *last_frame;
477     int last_dropped;
478     int last_nb0_frames[3];
479
480     void  *hwaccel_ctx;
481
482     /* video only */
483     AVRational frame_rate;
484     AVRational max_frame_rate;
485     int is_cfr;
486     int force_fps;
487     int top_field_first;
488     int rotate_overridden;
489     int autoscale;
490     double rotate_override_value;
491
492     AVRational frame_aspect_ratio;
493
494     /* forced key frames */
495     int64_t forced_kf_ref_pts;
496     int64_t *forced_kf_pts;
497     int forced_kf_count;
498     int forced_kf_index;
499     char *forced_keyframes;
500     AVExpr *forced_keyframes_pexpr;
501     double forced_keyframes_expr_const_values[FKF_NB];
502
503     /* audio only */
504     int *audio_channels_map;             /* list of the channels id to pick from the source stream */
505     int audio_channels_mapped;           /* number of channels in audio_channels_map */
506
507     char *logfile_prefix;
508     FILE *logfile;
509
510     OutputFilter *filter;
511     char *avfilter;
512     char *filters;         ///< filtergraph associated to the -filter option
513     char *filters_script;  ///< filtergraph script associated to the -filter_script option
514
515     AVDictionary *encoder_opts;
516     AVDictionary *sws_dict;
517     AVDictionary *swr_opts;
518     AVDictionary *resample_opts;
519     char *apad;
520     OSTFinished finished;        /* no more packets should be written for this stream */
521     int unavailable;                     /* true if the steram is unavailable (possibly temporarily) */
522     int stream_copy;
523
524     // init_output_stream() has been called for this stream
525     // The encoder and the bitstream filters have been initialized and the stream
526     // parameters are set in the AVStream.
527     int initialized;
528
529     int inputs_done;
530
531     const char *attachment_filename;
532     int copy_initial_nonkeyframes;
533     int copy_prior_start;
534     char *disposition;
535
536     int keep_pix_fmt;
537
538     /* stats */
539     // combined size of all the packets written
540     uint64_t data_size;
541     // number of packets send to the muxer
542     uint64_t packets_written;
543     // number of frames/samples sent to the encoder
544     uint64_t frames_encoded;
545     uint64_t samples_encoded;
546
547     /* packet quality factor */
548     int quality;
549
550     int max_muxing_queue_size;
551
552     /* the packets are buffered here until the muxer is ready to be initialized */
553     AVFifoBuffer *muxing_queue;
554
555     /*
556      * The size of the AVPackets' buffers in queue.
557      * Updated when a packet is either pushed or pulled from the queue.
558      */
559     size_t muxing_queue_data_size;
560
561     /* Threshold after which max_muxing_queue_size will be in effect */
562     size_t muxing_queue_data_threshold;
563
564     /* packet picture type */
565     int pict_type;
566
567     /* frame encode sum of squared error values */
568     int64_t error[4];
569 } OutputStream;
570
571 typedef struct OutputFile {
572     AVFormatContext *ctx;
573     AVDictionary *opts;
574     int ost_index;       /* index of the first stream in output_streams */
575     int64_t recording_time;  ///< desired length of the resulting file in microseconds == AV_TIME_BASE units
576     int64_t start_time;      ///< start time in microseconds == AV_TIME_BASE units
577     uint64_t limit_filesize; /* filesize limit expressed in bytes */
578
579     int shortest;
580
581     int header_written;
582 } OutputFile;
583
584 extern InputStream **input_streams;
585 extern int        nb_input_streams;
586 extern InputFile   **input_files;
587 extern int        nb_input_files;
588
589 extern OutputStream **output_streams;
590 extern int         nb_output_streams;
591 extern OutputFile   **output_files;
592 extern int         nb_output_files;
593
594 extern FilterGraph **filtergraphs;
595 extern int        nb_filtergraphs;
596
597 extern char *vstats_filename;
598 extern char *sdp_filename;
599
600 extern float audio_drift_threshold;
601 extern float dts_delta_threshold;
602 extern float dts_error_threshold;
603
604 extern int audio_volume;
605 extern int audio_sync_method;
606 extern int video_sync_method;
607 extern float frame_drop_threshold;
608 extern int do_benchmark;
609 extern int do_benchmark_all;
610 extern int do_deinterlace;
611 extern int do_hex_dump;
612 extern int do_pkt_dump;
613 extern int copy_ts;
614 extern int start_at_zero;
615 extern int copy_tb;
616 extern int debug_ts;
617 extern int exit_on_error;
618 extern int abort_on_flags;
619 extern int print_stats;
620 extern int64_t stats_period;
621 extern int qp_hist;
622 extern int stdin_interaction;
623 extern int frame_bits_per_raw_sample;
624 extern AVIOContext *progress_avio;
625 extern float max_error_rate;
626 extern char *videotoolbox_pixfmt;
627
628 extern int filter_nbthreads;
629 extern int filter_complex_nbthreads;
630 extern int vstats_version;
631 extern int auto_conversion_filters;
632
633 extern const AVIOInterruptCB int_cb;
634
635 extern const OptionDef options[];
636 extern const HWAccel hwaccels[];
637 #if CONFIG_QSV
638 extern char *qsv_device;
639 #endif
640 extern HWDevice *filter_hw_device;
641
642
643 void term_init(void);
644 void term_exit(void);
645
646 void show_usage(void);
647
648 void remove_avoptions(AVDictionary **a, AVDictionary *b);
649 void assert_avoptions(AVDictionary *m);
650
651 int guess_input_channel_layout(InputStream *ist);
652
653 int configure_filtergraph(FilterGraph *fg);
654 void check_filter_outputs(void);
655 int filtergraph_is_simple(FilterGraph *fg);
656 int init_simple_filtergraph(InputStream *ist, OutputStream *ost);
657 int init_complex_filtergraph(FilterGraph *fg);
658
659 void sub2video_update(InputStream *ist, int64_t heartbeat_pts, AVSubtitle *sub);
660
661 int ifilter_parameters_from_frame(InputFilter *ifilter, const AVFrame *frame);
662
663 int ffmpeg_parse_options(int argc, char **argv);
664
665 int videotoolbox_init(AVCodecContext *s);
666 int qsv_init(AVCodecContext *s);
667
668 HWDevice *hw_device_get_by_name(const char *name);
669 int hw_device_init_from_string(const char *arg, HWDevice **dev);
670 void hw_device_free_all(void);
671
672 int hw_device_setup_for_decode(InputStream *ist);
673 int hw_device_setup_for_encode(OutputStream *ost);
674 int hw_device_setup_for_filter(FilterGraph *fg);
675
676 int hwaccel_decode_init(AVCodecContext *avctx);
677
678 #endif /* FFTOOLS_FFMPEG_H */