• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "libavcodec/bsf.h"
35 
36 #include "libavfilter/avfilter.h"
37 
38 #include "libavutil/avutil.h"
39 #include "libavutil/dict.h"
40 #include "libavutil/eval.h"
41 #include "libavutil/fifo.h"
42 #include "libavutil/hwcontext.h"
43 #include "libavutil/pixfmt.h"
44 #include "libavutil/rational.h"
45 #include "libavutil/thread.h"
46 #include "libavutil/threadmessage.h"
47 
48 #include "libswresample/swresample.h"
49 
50 enum VideoSyncMethod {
51     VSYNC_AUTO = -1,
52     VSYNC_PASSTHROUGH,
53     VSYNC_CFR,
54     VSYNC_VFR,
55     VSYNC_VSCFR,
56     VSYNC_DROP,
57 };
58 
59 #define MAX_STREAMS 1024    /* arbitrary sanity check value */
60 
61 enum HWAccelID {
62     HWACCEL_NONE = 0,
63     HWACCEL_AUTO,
64     HWACCEL_GENERIC,
65 };
66 
67 typedef struct HWDevice {
68     const char *name;
69     enum AVHWDeviceType type;
70     AVBufferRef *device_ref;
71 } HWDevice;
72 
73 /* select an input stream for an output stream */
74 typedef struct StreamMap {
75     int disabled;           /* 1 is this mapping is disabled by a negative map */
76     int file_index;
77     int stream_index;
78     int sync_file_index;
79     int sync_stream_index;
80     char *linklabel;       /* name of an output link, for mapping lavfi outputs */
81 } StreamMap;
82 
83 typedef struct {
84     int  file_idx,  stream_idx,  channel_idx; // input
85     int ofile_idx, ostream_idx;               // output
86 } AudioChannelMap;
87 
88 typedef struct OptionsContext {
89     OptionGroup *g;
90 
91     /* input/output options */
92     int64_t start_time;
93     int64_t start_time_eof;
94     int seek_timestamp;
95     const char *format;
96 
97     SpecifierOpt *codec_names;
98     int        nb_codec_names;
99     SpecifierOpt *audio_ch_layouts;
100     int        nb_audio_ch_layouts;
101     SpecifierOpt *audio_channels;
102     int        nb_audio_channels;
103     SpecifierOpt *audio_sample_rate;
104     int        nb_audio_sample_rate;
105     SpecifierOpt *frame_rates;
106     int        nb_frame_rates;
107     SpecifierOpt *max_frame_rates;
108     int        nb_max_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     float readrate;
119     int accurate_seek;
120     int thread_queue_size;
121     int input_sync_ref;
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 *fps_mode;
181     int        nb_fps_mode;
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     SpecifierOpt *bits_per_raw_sample;
239     int        nb_bits_per_raw_sample;
240 } OptionsContext;
241 
242 typedef struct InputFilter {
243     AVFilterContext    *filter;
244     struct InputStream *ist;
245     struct FilterGraph *graph;
246     uint8_t            *name;
247     enum AVMediaType    type;   // AVMEDIA_TYPE_SUBTITLE for sub2video
248 
249     AVFifo *frame_queue;
250 
251     // parameters configured for this input
252     int format;
253 
254     int width, height;
255     AVRational sample_aspect_ratio;
256 
257     int sample_rate;
258     AVChannelLayout ch_layout;
259 
260     AVBufferRef *hw_frames_ctx;
261     int32_t *displaymatrix;
262 
263     int eof;
264 } InputFilter;
265 
266 typedef struct OutputFilter {
267     AVFilterContext     *filter;
268     struct OutputStream *ost;
269     struct FilterGraph  *graph;
270     uint8_t             *name;
271 
272     /* temporary storage until stream maps are processed */
273     AVFilterInOut       *out_tmp;
274     enum AVMediaType     type;
275 
276     /* desired output stream properties */
277     int width, height;
278     AVRational frame_rate;
279     int format;
280     int sample_rate;
281     AVChannelLayout ch_layout;
282 
283     // those are only set if no format is specified and the encoder gives us multiple options
284     // They point directly to the relevant lists of the encoder.
285     const int *formats;
286     const AVChannelLayout *ch_layouts;
287     const int *sample_rates;
288 } OutputFilter;
289 
290 typedef struct FilterGraph {
291     int            index;
292     const char    *graph_desc;
293 
294     AVFilterGraph *graph;
295     int reconfiguration;
296     // true when the filtergraph contains only meta filters
297     // that do not modify the frame data
298     int is_meta;
299 
300     InputFilter   **inputs;
301     int          nb_inputs;
302     OutputFilter **outputs;
303     int         nb_outputs;
304 } FilterGraph;
305 
306 typedef struct InputStream {
307     int file_index;
308     AVStream *st;
309     int discard;             /* true if stream data should be discarded */
310     int user_set_discard;
311     int decoding_needed;     /* non zero if the packets must be decoded in 'raw_fifo', see DECODING_FOR_* */
312 #define DECODING_FOR_OST    1
313 #define DECODING_FOR_FILTER 2
314     int processing_needed;   /* non zero if the packets must be processed */
315 
316     AVCodecContext *dec_ctx;
317     const AVCodec *dec;
318     AVFrame *decoded_frame;
319     AVPacket *pkt;
320 
321     int64_t       prev_pkt_pts;
322     int64_t       start;     /* time when read started */
323     /* predicted dts of the next packet read for this stream or (when there are
324      * several frames in a packet) of the next frame in current packet (in AV_TIME_BASE units) */
325     int64_t       next_dts;
326     int64_t first_dts;       ///< dts of the first packet read for this stream (in AV_TIME_BASE units)
327     int64_t       dts;       ///< dts of the last packet read for this stream (in AV_TIME_BASE units)
328 
329     int64_t       next_pts;  ///< synthetic pts for the next decode frame (in AV_TIME_BASE units)
330     int64_t       pts;       ///< current pts of the decoded frame  (in AV_TIME_BASE units)
331     int           wrap_correction_done;
332 
333     int64_t filter_in_rescale_delta_last;
334 
335     int64_t min_pts; /* pts with the smallest value in a current stream */
336     int64_t max_pts; /* pts with the higher value in a current stream */
337 
338     // when forcing constant input framerate through -r,
339     // this contains the pts that will be given to the next decoded frame
340     int64_t cfr_next_pts;
341 
342     int64_t nb_samples; /* number of samples in the last decoded audio frame before looping */
343 
344     double ts_scale;
345     int saw_first_ts;
346     AVDictionary *decoder_opts;
347     AVRational framerate;               /* framerate forced with -r */
348     int top_field_first;
349     int guess_layout_max;
350 
351     int autorotate;
352 
353     int fix_sub_duration;
354     struct { /* previous decoded subtitle and related variables */
355         int got_output;
356         int ret;
357         AVSubtitle subtitle;
358     } prev_sub;
359 
360     struct sub2video {
361         int64_t last_pts;
362         int64_t end_pts;
363         AVFifo *sub_queue;    ///< queue of AVSubtitle* before filter init
364         AVFrame *frame;
365         int w, h;
366         unsigned int initialize; ///< marks if sub2video_update should force an initialization
367     } sub2video;
368 
369     /* decoded data from this stream goes into all those filters
370      * currently video and audio only */
371     InputFilter **filters;
372     int        nb_filters;
373 
374     int reinit_filters;
375 
376     /* hwaccel options */
377     enum HWAccelID hwaccel_id;
378     enum AVHWDeviceType hwaccel_device_type;
379     char  *hwaccel_device;
380     enum AVPixelFormat hwaccel_output_format;
381 
382     /* hwaccel context */
383     void  *hwaccel_ctx;
384     void (*hwaccel_uninit)(AVCodecContext *s);
385     int  (*hwaccel_retrieve_data)(AVCodecContext *s, AVFrame *frame);
386     enum AVPixelFormat hwaccel_pix_fmt;
387     enum AVPixelFormat hwaccel_retrieved_pix_fmt;
388 
389     /* stats */
390     // combined size of all the packets read
391     uint64_t data_size;
392     /* number of packets successfully read for this stream */
393     uint64_t nb_packets;
394     // number of frames/samples retrieved from the decoder
395     uint64_t frames_decoded;
396     uint64_t samples_decoded;
397 
398     int64_t *dts_buffer;
399     int nb_dts_buffer;
400 
401     int got_output;
402 } InputStream;
403 
404 typedef struct InputFile {
405     AVFormatContext *ctx;
406     int eof_reached;      /* true if eof reached */
407     int eagain;           /* true if last read attempt returned EAGAIN */
408     int ist_index;        /* index of first stream in input_streams */
409     int loop;             /* set number of times input stream should be looped */
410     int64_t duration;     /* actual duration of the longest stream in a file
411                              at the moment when looping happens */
412     AVRational time_base; /* time base of the duration */
413     int64_t input_ts_offset;
414     int input_sync_ref;
415 
416     int64_t ts_offset;
417     int64_t last_ts;
418     int64_t start_time;   /* user-specified start time in AV_TIME_BASE or AV_NOPTS_VALUE */
419     int64_t recording_time;
420     int nb_streams;       /* number of stream that ffmpeg is aware of; may be different
421                              from ctx.nb_streams if new streams appear during av_read_frame() */
422     int nb_streams_warn;  /* number of streams that the user was warned of */
423     int rate_emu;
424     float readrate;
425     int accurate_seek;
426 
427     AVPacket *pkt;
428 
429 #if HAVE_THREADS
430     AVThreadMessageQueue *in_thread_queue;
431     pthread_t thread;           /* thread reading from this file */
432     int non_blocking;           /* reading packets from the thread should not block */
433     int joined;                 /* the thread has been joined */
434     int thread_queue_size;      /* maximum number of queued packets */
435 #endif
436 } InputFile;
437 
438 enum forced_keyframes_const {
439     FKF_N,
440     FKF_N_FORCED,
441     FKF_PREV_FORCED_N,
442     FKF_PREV_FORCED_T,
443     FKF_T,
444     FKF_NB
445 };
446 
447 #define ABORT_ON_FLAG_EMPTY_OUTPUT        (1 <<  0)
448 #define ABORT_ON_FLAG_EMPTY_OUTPUT_STREAM (1 <<  1)
449 
450 extern const char *const forced_keyframes_const_names[];
451 
452 typedef enum {
453     ENCODER_FINISHED = 1,
454     MUXER_FINISHED = 2,
455 } OSTFinished ;
456 
457 typedef struct OutputStream {
458     int file_index;          /* file index */
459     int index;               /* stream index in the output file */
460     int source_index;        /* InputStream index */
461     AVStream *st;            /* stream in the output file */
462     int encoding_needed;     /* true if encoding needed for this stream */
463     int64_t frame_number;
464     /* input pts and corresponding output pts
465        for A/V sync */
466     struct InputStream *sync_ist; /* input stream to sync against */
467     int64_t sync_opts;       /* output frame counter, could be changed to some true timestamp */ // FIXME look at frame_number
468     /* pts of the first frame encoded for this stream, used for limiting
469      * recording time */
470     int64_t first_pts;
471     /* dts of the last packet sent to the muxer */
472     int64_t last_mux_dts;
473     // the timebase of the packets sent to the muxer
474     AVRational mux_timebase;
475     AVRational enc_timebase;
476 
477     AVBSFContext            *bsf_ctx;
478 
479     AVCodecContext *enc_ctx;
480     AVCodecParameters *ref_par; /* associated input codec parameters with encoders options applied */
481     const AVCodec *enc;
482     int64_t max_frames;
483     AVFrame *filtered_frame;
484     AVFrame *last_frame;
485     AVPacket *pkt;
486     int64_t last_dropped;
487     int64_t last_nb0_frames[3];
488 
489     void  *hwaccel_ctx;
490 
491     /* video only */
492     AVRational frame_rate;
493     AVRational max_frame_rate;
494     enum VideoSyncMethod vsync_method;
495     int is_cfr;
496     const char *fps_mode;
497     int force_fps;
498     int top_field_first;
499     int rotate_overridden;
500     int autoscale;
501     int bits_per_raw_sample;
502     double rotate_override_value;
503 
504     AVRational frame_aspect_ratio;
505 
506     /* forced key frames */
507     int64_t forced_kf_ref_pts;
508     int64_t *forced_kf_pts;
509     int forced_kf_count;
510     int forced_kf_index;
511     char *forced_keyframes;
512     AVExpr *forced_keyframes_pexpr;
513     double forced_keyframes_expr_const_values[FKF_NB];
514     int dropped_keyframe;
515 
516     /* audio only */
517     int *audio_channels_map;             /* list of the channels id to pick from the source stream */
518     int audio_channels_mapped;           /* number of channels in audio_channels_map */
519 
520     char *logfile_prefix;
521     FILE *logfile;
522 
523     OutputFilter *filter;
524     char *avfilter;
525     char *filters;         ///< filtergraph associated to the -filter option
526     char *filters_script;  ///< filtergraph script associated to the -filter_script option
527 
528     AVDictionary *encoder_opts;
529     AVDictionary *sws_dict;
530     AVDictionary *swr_opts;
531     char *apad;
532     OSTFinished finished;        /* no more packets should be written for this stream */
533     int unavailable;                     /* true if the steram is unavailable (possibly temporarily) */
534     int stream_copy;
535 
536     // init_output_stream() has been called for this stream
537     // The encoder and the bitstream filters have been initialized and the stream
538     // parameters are set in the AVStream.
539     int initialized;
540 
541     int inputs_done;
542 
543     const char *attachment_filename;
544     int streamcopy_started;
545     int copy_initial_nonkeyframes;
546     int copy_prior_start;
547     char *disposition;
548 
549     int keep_pix_fmt;
550 
551     /* stats */
552     // combined size of all the packets written
553     uint64_t data_size;
554     // number of packets send to the muxer
555     uint64_t packets_written;
556     // number of frames/samples sent to the encoder
557     uint64_t frames_encoded;
558     uint64_t samples_encoded;
559     // number of packets received from the encoder
560     uint64_t packets_encoded;
561 
562     /* packet quality factor */
563     int quality;
564 
565     int max_muxing_queue_size;
566 
567     /* the packets are buffered here until the muxer is ready to be initialized */
568     AVFifo *muxing_queue;
569 
570     /*
571      * The size of the AVPackets' buffers in queue.
572      * Updated when a packet is either pushed or pulled from the queue.
573      */
574     size_t muxing_queue_data_size;
575 
576     /* Threshold after which max_muxing_queue_size will be in effect */
577     size_t muxing_queue_data_threshold;
578 
579     /* packet picture type */
580     int pict_type;
581 
582     /* frame encode sum of squared error values */
583     int64_t error[4];
584 } OutputStream;
585 
586 typedef struct OutputFile {
587     int index;
588 
589     const AVOutputFormat *format;
590 
591     AVFormatContext *ctx;
592     AVDictionary *opts;
593     int ost_index;       /* index of the first stream in output_streams */
594     int64_t recording_time;  ///< desired length of the resulting file in microseconds == AV_TIME_BASE units
595     int64_t start_time;      ///< start time in microseconds == AV_TIME_BASE units
596     uint64_t limit_filesize; /* filesize limit expressed in bytes */
597 
598     int shortest;
599 
600     int header_written;
601 } OutputFile;
602 
603 extern InputStream **input_streams;
604 extern int        nb_input_streams;
605 extern InputFile   **input_files;
606 extern int        nb_input_files;
607 
608 extern OutputStream **output_streams;
609 extern int         nb_output_streams;
610 extern OutputFile   **output_files;
611 extern int         nb_output_files;
612 
613 extern FilterGraph **filtergraphs;
614 extern int        nb_filtergraphs;
615 
616 extern char *vstats_filename;
617 extern char *sdp_filename;
618 
619 extern float audio_drift_threshold;
620 extern float dts_delta_threshold;
621 extern float dts_error_threshold;
622 
623 extern int audio_volume;
624 extern int audio_sync_method;
625 extern enum VideoSyncMethod video_sync_method;
626 extern float frame_drop_threshold;
627 extern int do_benchmark;
628 extern int do_benchmark_all;
629 extern int do_deinterlace;
630 extern int do_hex_dump;
631 extern int do_pkt_dump;
632 extern int copy_ts;
633 extern int start_at_zero;
634 extern int copy_tb;
635 extern int debug_ts;
636 extern int exit_on_error;
637 extern int abort_on_flags;
638 extern int print_stats;
639 extern int64_t stats_period;
640 extern int qp_hist;
641 extern int stdin_interaction;
642 extern int frame_bits_per_raw_sample;
643 extern AVIOContext *progress_avio;
644 extern float max_error_rate;
645 
646 extern char *filter_nbthreads;
647 extern int filter_complex_nbthreads;
648 extern int vstats_version;
649 extern int auto_conversion_filters;
650 
651 extern const AVIOInterruptCB int_cb;
652 
653 extern const OptionDef options[];
654 #if CONFIG_QSV
655 extern char *qsv_device;
656 #endif
657 extern HWDevice *filter_hw_device;
658 
659 extern int want_sdp;
660 extern unsigned nb_output_dumped;
661 extern int main_return_code;
662 
663 
664 void term_init(void);
665 void term_exit(void);
666 
667 void show_usage(void);
668 
669 void remove_avoptions(AVDictionary **a, AVDictionary *b);
670 void assert_avoptions(AVDictionary *m);
671 
672 int guess_input_channel_layout(InputStream *ist);
673 
674 int configure_filtergraph(FilterGraph *fg);
675 void check_filter_outputs(void);
676 int filtergraph_is_simple(FilterGraph *fg);
677 int init_simple_filtergraph(InputStream *ist, OutputStream *ost);
678 int init_complex_filtergraph(FilterGraph *fg);
679 
680 void sub2video_update(InputStream *ist, int64_t heartbeat_pts, AVSubtitle *sub);
681 
682 int ifilter_parameters_from_frame(InputFilter *ifilter, const AVFrame *frame);
683 
684 int ffmpeg_parse_options(int argc, char **argv);
685 
686 int videotoolbox_init(AVCodecContext *s);
687 int qsv_init(AVCodecContext *s);
688 
689 HWDevice *hw_device_get_by_name(const char *name);
690 int hw_device_init_from_string(const char *arg, HWDevice **dev);
691 void hw_device_free_all(void);
692 
693 int hw_device_setup_for_decode(InputStream *ist);
694 int hw_device_setup_for_encode(OutputStream *ost);
695 int hw_device_setup_for_filter(FilterGraph *fg);
696 
697 int hwaccel_decode_init(AVCodecContext *avctx);
698 
699 /* open the muxer when all the streams are initialized */
700 int of_check_init(OutputFile *of);
701 int of_write_trailer(OutputFile *of);
702 void of_close(OutputFile **pof);
703 
704 void of_write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost,
705                      int unqueue);
706 
707 #endif /* FFTOOLS_FFMPEG_H */
708