2 * This file is part of FFmpeg.
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.
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.
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
19 #ifndef FFTOOLS_FFMPEG_H
20 #define FFTOOLS_FFMPEG_H
24 #include <stdatomic.h>
30 #include "ffmpeg_sched.h"
31 #include "sync_queue.h"
33 #include "libavformat/avformat.h"
34 #include "libavformat/avio.h"
36 #include "libavcodec/avcodec.h"
37 #include "libavcodec/bsf.h"
39 #include "libavfilter/avfilter.h"
41 #include "libavutil/avutil.h"
42 #include "libavutil/dict.h"
43 #include "libavutil/eval.h"
44 #include "libavutil/fifo.h"
45 #include "libavutil/hwcontext.h"
46 #include "libavutil/pixfmt.h"
47 #include "libavutil/rational.h"
48 #include "libavutil/thread.h"
49 #include "libavutil/threadmessage.h"
51 #include "libswresample/swresample.h"
53 // deprecated features
54 #define FFMPEG_OPT_QPHIST 1
55 #define FFMPEG_OPT_ADRIFT_THRESHOLD 1
56 #define FFMPEG_OPT_ENC_TIME_BASE_NUM 1
57 #define FFMPEG_OPT_TOP 1
58 #define FFMPEG_OPT_FORCE_KF_SOURCE_NO_DROP 1
59 #define FFMPEG_OPT_VSYNC_DROP 1
60 #define FFMPEG_OPT_VSYNC 1
61 #define FFMPEG_OPT_FILTER_SCRIPT 1
63 #define FFMPEG_ERROR_RATE_EXCEEDED FFERRTAG('E', 'R', 'E', 'D')
65 enum VideoSyncMethod
{
71 #if FFMPEG_OPT_VSYNC_DROP
77 ENC_TIME_BASE_DEMUX
= -1,
78 ENC_TIME_BASE_FILTER
= -2,
88 FRAME_OPAQUE_SUB_HEARTBEAT
= 1,
90 FRAME_OPAQUE_SEND_COMMAND
,
94 PKT_OPAQUE_SUB_HEARTBEAT
= 1,
95 PKT_OPAQUE_FIX_SUB_DURATION
,
100 LATENCY_PROBE_DEC_PRE
,
101 LATENCY_PROBE_DEC_POST
,
102 LATENCY_PROBE_FILTER_PRE
,
103 LATENCY_PROBE_FILTER_POST
,
104 LATENCY_PROBE_ENC_PRE
,
105 LATENCY_PROBE_ENC_POST
,
109 typedef struct HWDevice
{
111 enum AVHWDeviceType type
;
112 AVBufferRef
*device_ref
;
115 enum ViewSpecifierType
{
116 // no specifier given
117 VIEW_SPECIFIER_TYPE_NONE
= 0,
119 VIEW_SPECIFIER_TYPE_IDX
,
121 VIEW_SPECIFIER_TYPE_ID
,
122 // specify view by its position, val is AV_STEREO3D_VIEW_LEFT/RIGHT
123 VIEW_SPECIFIER_TYPE_POS
,
124 // use all views, val is ignored
125 VIEW_SPECIFIER_TYPE_ALL
,
128 typedef struct ViewSpecifier
{
129 enum ViewSpecifierType type
;
133 /* select an input stream for an output stream */
134 typedef struct StreamMap
{
135 int disabled
; /* 1 is this mapping is disabled by a negative map */
138 char *linklabel
; /* name of an output link, for mapping lavfi outputs */
143 typedef struct OptionsContext
{
146 /* input/output options */
148 int64_t start_time_eof
;
152 SpecifierOptList codec_names
;
153 SpecifierOptList audio_ch_layouts
;
154 SpecifierOptList audio_channels
;
155 SpecifierOptList audio_sample_rate
;
156 SpecifierOptList frame_rates
;
157 SpecifierOptList max_frame_rates
;
158 SpecifierOptList frame_sizes
;
159 SpecifierOptList frame_pix_fmts
;
162 int64_t input_ts_offset
;
166 double readrate_initial_burst
;
168 int thread_queue_size
;
170 int find_stream_info
;
172 SpecifierOptList ts_scale
;
173 SpecifierOptList dump_attachment
;
174 SpecifierOptList hwaccels
;
175 SpecifierOptList hwaccel_devices
;
176 SpecifierOptList hwaccel_output_formats
;
177 SpecifierOptList autorotate
;
178 SpecifierOptList apply_cropping
;
181 StreamMap
*stream_maps
;
183 const char **attachments
;
186 int chapters_input_file
;
188 int64_t recording_time
;
190 int64_t limit_filesize
;
193 float shortest_buf_duration
;
199 int subtitle_disable
;
202 // keys are stream indices
203 AVDictionary
*streamid
;
205 SpecifierOptList metadata
;
206 SpecifierOptList max_frames
;
207 SpecifierOptList bitstream_filters
;
208 SpecifierOptList codec_tags
;
209 SpecifierOptList sample_fmts
;
210 SpecifierOptList qscale
;
211 SpecifierOptList forced_key_frames
;
212 SpecifierOptList fps_mode
;
213 SpecifierOptList force_fps
;
214 SpecifierOptList frame_aspect_ratios
;
215 SpecifierOptList display_rotations
;
216 SpecifierOptList display_hflips
;
217 SpecifierOptList display_vflips
;
218 SpecifierOptList rc_overrides
;
219 SpecifierOptList intra_matrices
;
220 SpecifierOptList inter_matrices
;
221 SpecifierOptList chroma_intra_matrices
;
223 SpecifierOptList top_field_first
;
225 SpecifierOptList metadata_map
;
226 SpecifierOptList presets
;
227 SpecifierOptList copy_initial_nonkeyframes
;
228 SpecifierOptList copy_prior_start
;
229 SpecifierOptList filters
;
230 #if FFMPEG_OPT_FILTER_SCRIPT
231 SpecifierOptList filter_scripts
;
233 SpecifierOptList reinit_filters
;
234 SpecifierOptList fix_sub_duration
;
235 SpecifierOptList fix_sub_duration_heartbeat
;
236 SpecifierOptList canvas_sizes
;
237 SpecifierOptList pass
;
238 SpecifierOptList passlogfiles
;
239 SpecifierOptList max_muxing_queue_size
;
240 SpecifierOptList muxing_queue_data_threshold
;
241 SpecifierOptList guess_layout_max
;
242 SpecifierOptList apad
;
243 SpecifierOptList discard
;
244 SpecifierOptList disposition
;
245 SpecifierOptList program
;
246 SpecifierOptList stream_groups
;
247 SpecifierOptList time_bases
;
248 SpecifierOptList enc_time_bases
;
249 SpecifierOptList autoscale
;
250 SpecifierOptList bits_per_raw_sample
;
251 SpecifierOptList enc_stats_pre
;
252 SpecifierOptList enc_stats_post
;
253 SpecifierOptList mux_stats
;
254 SpecifierOptList enc_stats_pre_fmt
;
255 SpecifierOptList enc_stats_post_fmt
;
256 SpecifierOptList mux_stats_fmt
;
260 IFILTER_FLAG_AUTOROTATE
= (1 << 0),
261 IFILTER_FLAG_REINIT
= (1 << 1),
262 IFILTER_FLAG_CFR
= (1 << 2),
263 IFILTER_FLAG_CROP
= (1 << 3),
266 typedef struct InputFilterOptions
{
267 int64_t trim_start_us
;
272 /* When IFILTER_FLAG_CFR is set, the stream is guaranteed to be CFR with
275 * Otherwise, this is an estimate that should not be relied upon to be
277 AVRational framerate
;
280 unsigned crop_bottom
;
285 int sub2video_height
;
287 // a combination of IFILTER_FLAG_*
291 } InputFilterOptions
;
294 OFILTER_FLAG_DISABLE_CONVERT
= (1 << 0),
295 // produce 24-bit audio
296 OFILTER_FLAG_AUDIO_24BIT
= (1 << 1),
297 OFILTER_FLAG_AUTOSCALE
= (1 << 2),
300 typedef struct OutputFilterOptions
{
301 // Caller-provided name for this output
304 // Codec used for encoding, may be NULL
307 int64_t trim_start_us
;
308 int64_t trim_duration_us
;
311 /* Desired output timebase.
312 * Numerator can be one of EncTimeBase values, or 0 when no preference.
314 AVRational output_tb
;
316 AVDictionary
*sws_opts
;
317 AVDictionary
*swr_opts
;
319 const char *nb_threads
;
321 // A combination of OFilterFlags.
327 enum AVColorSpace color_space
;
328 enum AVColorRange color_range
;
330 enum VideoSyncMethod vsync_method
;
331 AVRational frame_rate
;
332 AVRational max_frame_rate
;
335 AVChannelLayout ch_layout
;
338 const int *sample_rates
;
339 const AVChannelLayout
*ch_layouts
;
340 const AVRational
*frame_rates
;
341 const enum AVColorSpace
*color_spaces
;
342 const enum AVColorRange
*color_ranges
;
344 // for simple filtergraphs only, view specifier passed
345 // along to the decoder
346 const ViewSpecifier
*vs
;
347 } OutputFilterOptions
;
349 typedef struct InputFilter
{
350 struct FilterGraph
*graph
;
354 typedef struct OutputFilter
{
355 const AVClass
*class;
357 struct FilterGraph
*graph
;
360 /* for filters that are not yet bound to an output stream,
361 * this stores the output linklabel, if any */
367 enum AVMediaType type
;
369 atomic_uint_least64_t nb_frames_dup
;
370 atomic_uint_least64_t nb_frames_drop
;
373 typedef struct FilterGraph
{
374 const AVClass
*class;
377 InputFilter
**inputs
;
379 OutputFilter
**outputs
;
384 DECODER_FLAG_FIX_SUB_DURATION
= (1 << 0),
385 // input timestamps are unreliable (guessed by demuxer)
386 DECODER_FLAG_TS_UNRELIABLE
= (1 << 1),
387 // decoder should override timestamps by fixed framerate
388 // from DecoderOpts.framerate
389 DECODER_FLAG_FRAMERATE_FORCED
= (1 << 2),
391 DECODER_FLAG_TOP_FIELD_FIRST
= (1 << 3),
393 DECODER_FLAG_SEND_END_TS
= (1 << 4),
394 // force bitexact decoding
395 DECODER_FLAG_BITEXACT
= (1 << 5),
398 typedef struct DecoderOpts
{
404 const AVCodec
*codec
;
405 const AVCodecParameters
*par
;
407 /* hwaccel options */
408 enum HWAccelID hwaccel_id
;
409 enum AVHWDeviceType hwaccel_device_type
;
410 char *hwaccel_device
;
411 enum AVPixelFormat hwaccel_output_format
;
413 AVRational time_base
;
415 // Either forced (when DECODER_FLAG_FRAMERATE_FORCED is set) or
416 // estimated (otherwise) video framerate.
417 AVRational framerate
;
420 typedef struct Decoder
{
421 const AVClass
*class;
423 enum AVMediaType type
;
425 const uint8_t *subtitle_header
;
426 int subtitle_header_size
;
428 // number of frames/samples retrieved from the decoder
429 uint64_t frames_decoded
;
430 uint64_t samples_decoded
;
431 uint64_t decode_errors
;
434 typedef struct InputStream
{
435 const AVClass
*class;
438 struct InputFile
*file
;
443 int user_set_discard
;
446 * Codec parameters - to be used by the decoding/streamcopy code.
447 * st->codecpar should not be accessed, because it may be modified
448 * concurrently by the demuxing thread.
450 AVCodecParameters
*par
;
454 /* framerate forced with -r */
455 AVRational framerate
;
460 int fix_sub_duration
;
462 /* decoded data from this stream goes into all those filters
463 * currently video and audio only */
464 InputFilter
**filters
;
468 typedef struct InputFile
{
469 const AVClass
*class;
473 AVFormatContext
*ctx
;
474 int64_t input_ts_offset
;
477 * Effective format start time based on enabled streams.
479 int64_t start_time_effective
;
481 /* user-specified start time in AV_TIME_BASE or AV_NOPTS_VALUE */
484 /* streams that ffmpeg is aware of;
485 * there may be extra streams in ctx that are not mapped to an InputStream
486 * if new streams appear dynamically during demuxing */
487 InputStream
**streams
;
491 enum forced_keyframes_const
{
500 #define ABORT_ON_FLAG_EMPTY_OUTPUT (1 << 0)
501 #define ABORT_ON_FLAG_EMPTY_OUTPUT_STREAM (1 << 1)
504 ENC_STATS_LITERAL
= 0,
506 ENC_STATS_STREAM_IDX
,
508 ENC_STATS_FRAME_NUM_IN
,
510 ENC_STATS_TIMEBASE_IN
,
514 ENC_STATS_PTS_TIME_IN
,
517 ENC_STATS_SAMPLE_NUM
,
518 ENC_STATS_NB_SAMPLES
,
521 ENC_STATS_AVG_BITRATE
,
525 typedef struct EncStatsComponent
{
526 enum EncStatsType type
;
532 typedef struct EncStats
{
533 EncStatsComponent
*components
;
538 pthread_mutex_t lock
;
539 int lock_initialized
;
544 #if FFMPEG_OPT_FORCE_KF_SOURCE_NO_DROP
545 KF_FORCE_SOURCE_NO_DROP
= 2,
549 typedef struct KeyframeForceCtx
{
554 // timestamps of the forced keyframes, in AV_TIME_BASE_Q
560 double expr_const_values
[FKF_NB
];
562 int dropped_keyframe
;
565 typedef struct Encoder
{
566 const AVClass
*class;
568 AVCodecContext
*enc_ctx
;
570 // number of frames/samples sent to the encoder
571 uint64_t frames_encoded
;
572 uint64_t samples_encoded
;
582 typedef struct OutputStream
{
583 const AVClass
*class;
585 enum AVMediaType type
;
588 struct OutputFile
*file
;
590 int index
; /* stream index in the output file */
592 /* input stream that is the source for this output stream;
593 * may be NULL for streams with no well-defined source, e.g.
594 * attachments or outputs from complex filtergraphs */
597 AVStream
*st
; /* stream in the output file */
606 int bits_per_raw_sample
;
608 AVRational frame_aspect_ratio
;
612 const char *logfile_prefix
;
615 // simple filtergraph feeding this stream, if any
616 FilterGraph
*fg_simple
;
617 OutputFilter
*filter
;
619 char *attachment_filename
;
622 // number of packets send to the muxer
623 atomic_uint_least64_t packets_written
;
625 /* packet quality factor */
628 EncStats enc_stats_pre
;
629 EncStats enc_stats_post
;
632 * bool on whether this stream should be utilized for splitting
633 * subtitles utilizing fix_sub_duration at random access points.
635 unsigned int fix_sub_duration_heartbeat
;
638 typedef struct OutputFile
{
639 const AVClass
*class;
645 OutputStream
**streams
;
648 int64_t recording_time
; ///< desired length of the resulting file in microseconds == AV_TIME_BASE units
649 int64_t start_time
; ///< start time in microseconds == AV_TIME_BASE units
654 // optionally attached as opaque_ref to decoded AVFrames
655 typedef struct FrameData
{
656 // demuxer-estimated dts in AV_TIME_BASE_Q,
657 // to be used when real dts is missing
660 // properties that come from the decoder
668 AVRational frame_rate_filter
;
670 int bits_per_raw_sample
;
672 int64_t wallclock
[LATENCY_PROBE_NB
];
674 AVCodecParameters
*par_enc
;
677 extern InputFile
**input_files
;
678 extern int nb_input_files
;
680 extern OutputFile
**output_files
;
681 extern int nb_output_files
;
683 // complex filtergraphs
684 extern FilterGraph
**filtergraphs
;
685 extern int nb_filtergraphs
;
687 // standalone decoders (not tied to demuxed streams)
688 extern Decoder
**decoders
;
689 extern int nb_decoders
;
691 extern char *vstats_filename
;
693 extern float dts_delta_threshold
;
694 extern float dts_error_threshold
;
696 extern enum VideoSyncMethod video_sync_method
;
697 extern float frame_drop_threshold
;
698 extern int do_benchmark
;
699 extern int do_benchmark_all
;
700 extern int do_hex_dump
;
701 extern int do_pkt_dump
;
703 extern int start_at_zero
;
706 extern int exit_on_error
;
707 extern int abort_on_flags
;
708 extern int print_stats
;
709 extern int64_t stats_period
;
710 extern int stdin_interaction
;
711 extern AVIOContext
*progress_avio
;
712 extern float max_error_rate
;
714 extern char *filter_nbthreads
;
715 extern int filter_complex_nbthreads
;
716 extern int vstats_version
;
717 extern int auto_conversion_filters
;
719 extern const AVIOInterruptCB int_cb
;
721 extern const OptionDef options
[];
722 extern HWDevice
*filter_hw_device
;
724 extern atomic_uint nb_output_dumped
;
726 extern int ignore_unknown_streams
;
727 extern int copy_unknown_streams
;
729 extern int recast_media
;
731 extern FILE *vstats_file
;
733 void term_init(void);
734 void term_exit(void);
736 void show_usage(void);
738 int check_avoptions_used(const AVDictionary
*opts
, const AVDictionary
*opts_used
,
739 void *logctx
, int decode
);
741 int assert_file_overwrite(const char *filename
);
742 int find_codec(void *logctx
, const char *name
,
743 enum AVMediaType type
, int encoder
, const AVCodec
**codec
);
744 int parse_and_set_vsync(const char *arg
, int *vsync_var
, int file_idx
, int st_idx
, int is_global
);
746 int filtergraph_is_simple(const FilterGraph
*fg
);
747 int fg_create_simple(FilterGraph
**pfg
,
750 Scheduler
*sch
, unsigned sched_idx_enc
,
751 const OutputFilterOptions
*opts
);
752 int fg_finalise_bindings(void);
755 * Get our axiliary frame data attached to the frame, allocating it
758 FrameData
*frame_data(AVFrame
*frame
);
760 const FrameData
*frame_data_c(AVFrame
*frame
);
762 FrameData
*packet_data (AVPacket
*pkt
);
763 const FrameData
*packet_data_c(AVPacket
*pkt
);
765 int ofilter_bind_enc(OutputFilter
*ofilter
,
766 unsigned sched_idx_enc
,
767 const OutputFilterOptions
*opts
);
770 * Create a new filtergraph in the global filtergraph list.
772 * @param graph_desc Graph description; an av_malloc()ed string, filtergraph
773 * takes ownership of it.
775 int fg_create(FilterGraph
**pfg
, char *graph_desc
, Scheduler
*sch
);
777 void fg_free(FilterGraph
**pfg
);
779 void fg_send_command(FilterGraph
*fg
, double time
, const char *target
,
780 const char *command
, const char *arg
, int all_filters
);
782 int ffmpeg_parse_options(int argc
, char **argv
, Scheduler
*sch
);
784 void enc_stats_write(OutputStream
*ost
, EncStats
*es
,
785 const AVFrame
*frame
, const AVPacket
*pkt
,
788 HWDevice
*hw_device_get_by_name(const char *name
);
789 HWDevice
*hw_device_get_by_type(enum AVHWDeviceType type
);
790 int hw_device_init_from_string(const char *arg
, HWDevice
**dev
);
791 int hw_device_init_from_type(enum AVHWDeviceType type
,
794 void hw_device_free_all(void);
797 * Get a hardware device to be used with this filtergraph.
798 * The returned reference is owned by the callee, the caller
799 * must ref it explicitly for long-term use.
801 AVBufferRef
*hw_device_for_filter(void);
804 * Create a standalone decoder.
806 int dec_create(const OptionsContext
*o
, const char *arg
, Scheduler
*sch
);
809 * @param dec_opts Dictionary filled with decoder options. Its ownership
810 * is transferred to the decoder.
811 * @param param_out If non-NULL, media properties after opening the decoder
814 * @retval ">=0" non-negative scheduler index on success
815 * @retval "<0" an error code on failure
817 int dec_init(Decoder
**pdec
, Scheduler
*sch
,
818 AVDictionary
**dec_opts
, const DecoderOpts
*o
,
820 void dec_free(Decoder
**pdec
);
823 * Called by filters to connect decoder's output to given filtergraph input.
825 * @param opts filtergraph input options, to be filled by this function
827 int dec_filter_add(Decoder
*dec
, InputFilter
*ifilter
, InputFilterOptions
*opts
,
828 const ViewSpecifier
*vs
, SchedulerNode
*src
);
831 * For multiview video, request output of the view(s) determined by vs.
832 * May be called multiple times.
834 * If this function is never called, only the base view is output. If it is
835 * called at least once, only the views requested are output.
837 * @param src scheduler node from which the frames corresponding vs
840 int dec_request_view(Decoder
*dec
, const ViewSpecifier
*vs
,
843 int enc_alloc(Encoder
**penc
, const AVCodec
*codec
,
844 Scheduler
*sch
, unsigned sch_idx
, void *log_parent
);
845 void enc_free(Encoder
**penc
);
847 int enc_open(void *opaque
, const AVFrame
*frame
);
849 int enc_loopback(Encoder
*enc
);
852 * Initialize muxing state for the given stream, should be called
853 * after the codec/streamcopy setup has been done.
855 * Open the muxer once all the streams have been initialized.
857 int of_stream_init(OutputFile
*of
, OutputStream
*ost
,
858 const AVCodecContext
*enc_ctx
);
859 int of_write_trailer(OutputFile
*of
);
860 int of_open(const OptionsContext
*o
, const char *filename
, Scheduler
*sch
);
861 void of_free(OutputFile
**pof
);
863 void of_enc_stats_close(void);
865 int64_t of_filesize(OutputFile
*of
);
867 int ifile_open(const OptionsContext
*o
, const char *filename
, Scheduler
*sch
);
868 void ifile_close(InputFile
**f
);
870 int ist_use(InputStream
*ist
, int decoding_needed
,
871 const ViewSpecifier
*vs
, SchedulerNode
*src
);
872 int ist_filter_add(InputStream
*ist
, InputFilter
*ifilter
, int is_simple
,
873 const ViewSpecifier
*vs
, InputFilterOptions
*opts
,
877 * Find an unused input stream of given type.
879 InputStream
*ist_find_unused(enum AVMediaType type
);
881 /* iterate over all input streams in all input files;
882 * pass NULL to start iteration */
883 InputStream
*ist_iter(InputStream
*prev
);
885 /* iterate over all output streams in all output files;
886 * pass NULL to start iteration */
887 OutputStream
*ost_iter(OutputStream
*prev
);
889 void update_benchmark(const char *fmt
, ...);
891 const char *opt_match_per_type_str(const SpecifierOptList
*sol
,
893 void opt_match_per_stream_str(void *logctx
, const SpecifierOptList
*sol
,
894 AVFormatContext
*fc
, AVStream
*st
, const char **out
);
895 void opt_match_per_stream_int(void *logctx
, const SpecifierOptList
*sol
,
896 AVFormatContext
*fc
, AVStream
*st
, int *out
);
897 void opt_match_per_stream_int64(void *logctx
, const SpecifierOptList
*sol
,
898 AVFormatContext
*fc
, AVStream
*st
, int64_t *out
);
899 void opt_match_per_stream_dbl(void *logctx
, const SpecifierOptList
*sol
,
900 AVFormatContext
*fc
, AVStream
*st
, double *out
);
902 int view_specifier_parse(const char **pspec
, ViewSpecifier
*vs
);
904 int muxer_thread(void *arg
);
905 int encoder_thread(void *arg
);
907 #endif /* FFTOOLS_FFMPEG_H */