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