2 * This file is part of Libav.
4 * Libav 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 * Libav 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 Libav; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
33 #include "libavformat/avformat.h"
34 #include "libavformat/avio.h"
36 #include "libavcodec/avcodec.h"
38 #include "libavfilter/avfilter.h"
40 #include "libavutil/avutil.h"
41 #include "libavutil/dict.h"
42 #include "libavutil/fifo.h"
43 #include "libavutil/hwcontext.h"
44 #include "libavutil/pixfmt.h"
45 #include "libavutil/rational.h"
48 #define VSYNC_PASSTHROUGH 0
60 typedef struct HWAccel
{
62 int (*init
)(AVCodecContext
*s
);
64 enum AVPixelFormat pix_fmt
;
67 typedef struct HWDevice
{
69 enum AVHWDeviceType type
;
70 AVBufferRef
*device_ref
;
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 */
79 int sync_stream_index
;
80 char *linklabel
; /* name of an output link, for mapping lavfi outputs */
83 /* select an input file for an output file */
84 typedef struct MetadataMap
{
85 int file
; // file index
86 char type
; // type of metadata to copy -- (g)lobal, (s)tream, (c)hapter or (p)rogram
87 int index
; // stream/chapter/program number
90 typedef struct OptionsContext
{
93 /* input/output options */
97 SpecifierOpt
*codec_names
;
99 SpecifierOpt
*audio_channels
;
100 int nb_audio_channels
;
101 SpecifierOpt
*audio_sample_rate
;
102 int nb_audio_sample_rate
;
103 SpecifierOpt
*frame_rates
;
105 SpecifierOpt
*frame_sizes
;
107 SpecifierOpt
*frame_pix_fmts
;
108 int nb_frame_pix_fmts
;
111 int64_t input_ts_offset
;
116 SpecifierOpt
*ts_scale
;
118 SpecifierOpt
*dump_attachment
;
119 int nb_dump_attachment
;
120 SpecifierOpt
*hwaccels
;
122 SpecifierOpt
*hwaccel_devices
;
123 int nb_hwaccel_devices
;
124 SpecifierOpt
*hwaccel_output_formats
;
125 int nb_hwaccel_output_formats
;
126 SpecifierOpt
*autorotate
;
130 StreamMap
*stream_maps
;
132 /* first item specifies output metadata, second is input */
133 MetadataMap (*meta_data_maps
)[2];
134 int nb_meta_data_maps
;
135 int metadata_global_manual
;
136 int metadata_streams_manual
;
137 int metadata_chapters_manual
;
138 const char **attachments
;
141 int chapters_input_file
;
143 int64_t recording_time
;
144 uint64_t limit_filesize
;
151 int subtitle_disable
;
154 /* indexed by output file stream index */
158 SpecifierOpt
*metadata
;
160 SpecifierOpt
*max_frames
;
162 SpecifierOpt
*bitstream_filters
;
163 int nb_bitstream_filters
;
164 SpecifierOpt
*codec_tags
;
166 SpecifierOpt
*sample_fmts
;
168 SpecifierOpt
*qscale
;
170 SpecifierOpt
*bitrates
;
172 SpecifierOpt
*forced_key_frames
;
173 int nb_forced_key_frames
;
174 SpecifierOpt
*force_fps
;
176 SpecifierOpt
*frame_aspect_ratios
;
177 int nb_frame_aspect_ratios
;
178 SpecifierOpt
*rc_overrides
;
180 SpecifierOpt
*intra_matrices
;
181 int nb_intra_matrices
;
182 SpecifierOpt
*inter_matrices
;
183 int nb_inter_matrices
;
184 SpecifierOpt
*top_field_first
;
185 int nb_top_field_first
;
186 SpecifierOpt
*metadata_map
;
188 SpecifierOpt
*presets
;
190 SpecifierOpt
*copy_initial_nonkeyframes
;
191 int nb_copy_initial_nonkeyframes
;
192 SpecifierOpt
*filters
;
194 SpecifierOpt
*filter_scripts
;
195 int nb_filter_scripts
;
198 SpecifierOpt
*passlogfiles
;
200 SpecifierOpt
*max_muxing_queue_size
;
201 int nb_max_muxing_queue_size
;
204 typedef struct InputFilter
{
205 AVFilterContext
*filter
;
206 struct InputStream
*ist
;
207 struct FilterGraph
*graph
;
210 AVFifoBuffer
*frame_queue
;
212 // parameters configured for this input
216 AVRational sample_aspect_ratio
;
219 uint64_t channel_layout
;
221 AVBufferRef
*hw_frames_ctx
;
226 typedef struct OutputFilter
{
227 AVFilterContext
*filter
;
228 struct OutputStream
*ost
;
229 struct FilterGraph
*graph
;
232 /* temporary storage until stream maps are processed */
233 AVFilterInOut
*out_tmp
;
234 enum AVMediaType type
;
236 /* desired output stream properties */
238 AVRational frame_rate
;
241 uint64_t channel_layout
;
243 // those are only set if no format is specified and the encoder gives us multiple options
245 uint64_t *channel_layouts
;
249 typedef struct FilterGraph
{
251 const char *graph_desc
;
253 AVFilterGraph
*graph
;
255 InputFilter
**inputs
;
257 OutputFilter
**outputs
;
261 typedef struct InputStream
{
264 int discard
; /* true if stream data should be discarded */
265 int decoding_needed
; /* true if the packets must be decoded in 'raw_fifo' */
266 AVCodecContext
*dec_ctx
;
268 AVFrame
*decoded_frame
;
269 AVFrame
*filter_frame
; /* a ref of decoded_frame, to be sent to filters */
271 int64_t start
; /* time when read started */
272 /* predicted dts of the next packet read for this stream or (when there are
273 * several frames in a packet) of the next frame in current packet */
275 /* dts of the last packet read for this stream */
277 int64_t min_pts
; /* pts with the smallest value in a current stream */
278 int64_t max_pts
; /* pts with the higher value in a current stream */
280 // when forcing constant input framerate through -r,
281 // this contains the pts that will be given to the next decoded frame
282 int64_t cfr_next_pts
;
284 int64_t nb_samples
; /* number of samples in the last decoded audio frame before looping */
285 PtsCorrectionContext pts_ctx
;
287 AVDictionary
*decoder_opts
;
288 AVRational framerate
; /* framerate forced with -r */
292 /* decoded data from this stream goes into all those filters
293 * currently video and audio only */
294 InputFilter
**filters
;
297 /* hwaccel options */
298 enum HWAccelID hwaccel_id
;
299 enum AVHWDeviceType hwaccel_device_type
;
300 char *hwaccel_device
;
301 enum AVPixelFormat hwaccel_output_format
;
303 /* hwaccel context */
305 void (*hwaccel_uninit
)(AVCodecContext
*s
);
306 int (*hwaccel_get_buffer
)(AVCodecContext
*s
, AVFrame
*frame
, int flags
);
307 int (*hwaccel_retrieve_data
)(AVCodecContext
*s
, AVFrame
*frame
);
308 enum AVPixelFormat hwaccel_pix_fmt
;
309 enum AVPixelFormat hwaccel_retrieved_pix_fmt
;
310 AVBufferRef
*hw_frames_ctx
;
313 // combined size of all the packets read
315 /* number of packets successfully read for this stream */
317 // number of frames/samples retrieved from the decoder
318 uint64_t frames_decoded
;
319 uint64_t samples_decoded
;
322 typedef struct InputFile
{
323 AVFormatContext
*ctx
;
324 int eof_reached
; /* true if eof reached */
325 int eagain
; /* true if last read attempt returned EAGAIN */
326 int ist_index
; /* index of first stream in ist_table */
327 int loop
; /* set number of times input stream should be looped */
328 int64_t duration
; /* actual duration of the longest stream in a file
329 at the moment when looping happens */
330 AVRational time_base
; /* time base of the duration */
332 int64_t start_time
; /* user-specified start time in AV_TIME_BASE or AV_NOPTS_VALUE */
333 int64_t recording_time
;
334 int nb_streams
; /* number of stream that avconv is aware of; may be different
335 from ctx.nb_streams if new streams appear during av_read_frame() */
340 pthread_t thread
; /* thread reading from this file */
341 int finished
; /* the thread has exited */
342 int joined
; /* the thread has been joined */
343 pthread_mutex_t fifo_lock
; /* lock for access to fifo */
344 pthread_cond_t fifo_cond
; /* the main thread will signal on this cond after reading from fifo */
345 AVFifoBuffer
*fifo
; /* demuxed packets are stored here; freed by the main thread */
349 typedef struct OutputStream
{
350 int file_index
; /* file index */
351 int index
; /* stream index in the output file */
352 int source_index
; /* InputStream index */
353 AVStream
*st
; /* stream in the output file */
354 int encoding_needed
; /* true if encoding needed for this stream */
356 /* input pts and corresponding output pts
358 // double sync_ipts; /* dts from the AVPacket of the demuxer in second units */
359 struct InputStream
*sync_ist
; /* input stream to sync against */
360 int64_t sync_opts
; /* output frame counter, could be changed to some true timestamp */ // FIXME look at frame_number
361 /* pts of the first frame encoded for this stream, used for limiting
364 /* dts of the last packet sent to the muxer */
365 int64_t last_mux_dts
;
366 // the timebase of the packets sent to the muxer
367 AVRational mux_timebase
;
369 int nb_bitstream_filters
;
370 AVBSFContext
**bsf_ctx
;
372 AVCodecContext
*enc_ctx
;
375 AVFrame
*filtered_frame
;
380 AVRational frame_rate
;
384 float frame_aspect_ratio
;
386 /* forced key frames */
387 int64_t *forced_kf_pts
;
390 char *forced_keyframes
;
392 // the bitrate to send to the muxer for streamcopy
393 int bitrate_override
;
395 char *logfile_prefix
;
398 OutputFilter
*filter
;
402 AVDictionary
*encoder_opts
;
403 AVDictionary
*resample_opts
;
404 int finished
; /* no more packets should be written for this stream */
407 // init_output_stream() has been called for this stream
408 // The encoder and the bistream filters have been initialized and the stream
409 // parameters are set in the AVStream.
412 const char *attachment_filename
;
413 int copy_initial_nonkeyframes
;
415 enum AVPixelFormat pix_fmts
[2];
417 AVCodecParserContext
*parser
;
418 AVCodecContext
*parser_avctx
;
421 // combined size of all the packets written
423 // number of packets send to the muxer
424 uint64_t packets_written
;
425 // number of frames/samples sent to the encoder
426 uint64_t frames_encoded
;
427 uint64_t samples_encoded
;
429 /* packet quality factor */
432 int max_muxing_queue_size
;
434 /* the packets are buffered here until the muxer is ready to be initialized */
435 AVFifoBuffer
*muxing_queue
;
438 typedef struct OutputFile
{
439 AVFormatContext
*ctx
;
441 int ost_index
; /* index of the first stream in output_streams */
442 int64_t recording_time
; /* desired length of the resulting file in microseconds */
443 int64_t start_time
; /* start time in microseconds */
444 uint64_t limit_filesize
;
451 extern InputStream
**input_streams
;
452 extern int nb_input_streams
;
453 extern InputFile
**input_files
;
454 extern int nb_input_files
;
456 extern OutputStream
**output_streams
;
457 extern int nb_output_streams
;
458 extern OutputFile
**output_files
;
459 extern int nb_output_files
;
461 extern FilterGraph
**filtergraphs
;
462 extern int nb_filtergraphs
;
464 extern char *vstats_filename
;
466 extern float audio_drift_threshold
;
467 extern float dts_delta_threshold
;
469 extern int audio_volume
;
470 extern int audio_sync_method
;
471 extern int video_sync_method
;
472 extern int do_benchmark
;
473 extern int do_deinterlace
;
474 extern int do_hex_dump
;
475 extern int do_pkt_dump
;
478 extern int exit_on_error
;
479 extern int print_stats
;
482 extern const AVIOInterruptCB int_cb
;
484 extern const OptionDef options
[];
486 extern const HWAccel hwaccels
[];
487 extern int hwaccel_lax_profile_check
;
488 extern AVBufferRef
*hw_device_ctx
;
489 extern HWDevice
*filter_hw_device
;
491 void reset_options(OptionsContext
*o
);
492 void show_usage(void);
494 void opt_output_file(void *optctx
, const char *filename
);
496 void assert_avoptions(AVDictionary
*m
);
498 int guess_input_channel_layout(InputStream
*ist
);
500 int configure_filtergraph(FilterGraph
*fg
);
501 int configure_output_filter(FilterGraph
*fg
, OutputFilter
*ofilter
, AVFilterInOut
*out
);
502 int ist_in_filtergraph(FilterGraph
*fg
, InputStream
*ist
);
503 int filtergraph_is_simple(FilterGraph
*fg
);
504 int init_simple_filtergraph(InputStream
*ist
, OutputStream
*ost
);
505 int init_complex_filtergraph(FilterGraph
*fg
);
507 int ifilter_parameters_from_frame(InputFilter
*ifilter
, const AVFrame
*frame
);
509 int avconv_parse_options(int argc
, char **argv
);
511 int dxva2_init(AVCodecContext
*s
);
512 int vda_init(AVCodecContext
*s
);
513 int qsv_init(AVCodecContext
*s
);
514 int qsv_transcode_init(OutputStream
*ost
);
516 HWDevice
*hw_device_get_by_name(const char *name
);
517 int hw_device_init_from_string(const char *arg
, HWDevice
**dev
);
518 void hw_device_free_all(void);
520 int hw_device_setup_for_decode(InputStream
*ist
);
521 int hw_device_setup_for_encode(OutputStream
*ost
);
523 int hwaccel_decode_init(AVCodecContext
*avctx
);
525 #endif /* AVCONV_H */