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