You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

526 lines
15KB

  1. /*
  2. * This file is part of Libav.
  3. *
  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.
  8. *
  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.
  13. *
  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
  17. */
  18. #ifndef AVCONV_H
  19. #define AVCONV_H
  20. #include "config.h"
  21. #include <stdint.h>
  22. #include <stdio.h>
  23. #if HAVE_PTHREADS
  24. #include <pthread.h>
  25. #endif
  26. #include "cmdutils.h"
  27. #include "libavformat/avformat.h"
  28. #include "libavformat/avio.h"
  29. #include "libavcodec/avcodec.h"
  30. #include "libavfilter/avfilter.h"
  31. #include "libavutil/avutil.h"
  32. #include "libavutil/dict.h"
  33. #include "libavutil/fifo.h"
  34. #include "libavutil/hwcontext.h"
  35. #include "libavutil/pixfmt.h"
  36. #include "libavutil/rational.h"
  37. #define VSYNC_AUTO -1
  38. #define VSYNC_PASSTHROUGH 0
  39. #define VSYNC_CFR 1
  40. #define VSYNC_VFR 2
  41. enum HWAccelID {
  42. HWACCEL_NONE = 0,
  43. HWACCEL_AUTO,
  44. HWACCEL_GENERIC,
  45. HWACCEL_VDA,
  46. HWACCEL_QSV,
  47. };
  48. typedef struct HWAccel {
  49. const char *name;
  50. int (*init)(AVCodecContext *s);
  51. enum HWAccelID id;
  52. enum AVPixelFormat pix_fmt;
  53. } HWAccel;
  54. typedef struct HWDevice {
  55. char *name;
  56. enum AVHWDeviceType type;
  57. AVBufferRef *device_ref;
  58. } HWDevice;
  59. /* select an input stream for an output stream */
  60. typedef struct StreamMap {
  61. int disabled; /* 1 is this mapping is disabled by a negative map */
  62. int file_index;
  63. int stream_index;
  64. int sync_file_index;
  65. int sync_stream_index;
  66. char *linklabel; /* name of an output link, for mapping lavfi outputs */
  67. } StreamMap;
  68. /* select an input file for an output file */
  69. typedef struct MetadataMap {
  70. int file; // file index
  71. char type; // type of metadata to copy -- (g)lobal, (s)tream, (c)hapter or (p)rogram
  72. int index; // stream/chapter/program number
  73. } MetadataMap;
  74. typedef struct OptionsContext {
  75. OptionGroup *g;
  76. /* input/output options */
  77. int64_t start_time;
  78. const char *format;
  79. SpecifierOpt *codec_names;
  80. int nb_codec_names;
  81. SpecifierOpt *audio_channels;
  82. int nb_audio_channels;
  83. SpecifierOpt *audio_sample_rate;
  84. int nb_audio_sample_rate;
  85. SpecifierOpt *frame_rates;
  86. int nb_frame_rates;
  87. SpecifierOpt *frame_sizes;
  88. int nb_frame_sizes;
  89. SpecifierOpt *frame_pix_fmts;
  90. int nb_frame_pix_fmts;
  91. /* input options */
  92. int64_t input_ts_offset;
  93. int loop;
  94. int rate_emu;
  95. int accurate_seek;
  96. SpecifierOpt *ts_scale;
  97. int nb_ts_scale;
  98. SpecifierOpt *dump_attachment;
  99. int nb_dump_attachment;
  100. SpecifierOpt *hwaccels;
  101. int nb_hwaccels;
  102. SpecifierOpt *hwaccel_devices;
  103. int nb_hwaccel_devices;
  104. SpecifierOpt *hwaccel_output_formats;
  105. int nb_hwaccel_output_formats;
  106. SpecifierOpt *autorotate;
  107. int nb_autorotate;
  108. /* output options */
  109. StreamMap *stream_maps;
  110. int nb_stream_maps;
  111. /* first item specifies output metadata, second is input */
  112. MetadataMap (*meta_data_maps)[2];
  113. int nb_meta_data_maps;
  114. int metadata_global_manual;
  115. int metadata_streams_manual;
  116. int metadata_chapters_manual;
  117. const char **attachments;
  118. int nb_attachments;
  119. int chapters_input_file;
  120. int64_t recording_time;
  121. uint64_t limit_filesize;
  122. float mux_preload;
  123. float mux_max_delay;
  124. int shortest;
  125. int video_disable;
  126. int audio_disable;
  127. int subtitle_disable;
  128. int data_disable;
  129. /* indexed by output file stream index */
  130. int *streamid_map;
  131. int nb_streamid_map;
  132. SpecifierOpt *metadata;
  133. int nb_metadata;
  134. SpecifierOpt *max_frames;
  135. int nb_max_frames;
  136. SpecifierOpt *bitstream_filters;
  137. int nb_bitstream_filters;
  138. SpecifierOpt *codec_tags;
  139. int nb_codec_tags;
  140. SpecifierOpt *sample_fmts;
  141. int nb_sample_fmts;
  142. SpecifierOpt *qscale;
  143. int nb_qscale;
  144. SpecifierOpt *bitrates;
  145. int nb_bitrates;
  146. SpecifierOpt *forced_key_frames;
  147. int nb_forced_key_frames;
  148. SpecifierOpt *force_fps;
  149. int nb_force_fps;
  150. SpecifierOpt *frame_aspect_ratios;
  151. int nb_frame_aspect_ratios;
  152. SpecifierOpt *rc_overrides;
  153. int nb_rc_overrides;
  154. SpecifierOpt *intra_matrices;
  155. int nb_intra_matrices;
  156. SpecifierOpt *inter_matrices;
  157. int nb_inter_matrices;
  158. SpecifierOpt *top_field_first;
  159. int nb_top_field_first;
  160. SpecifierOpt *metadata_map;
  161. int nb_metadata_map;
  162. SpecifierOpt *presets;
  163. int nb_presets;
  164. SpecifierOpt *copy_initial_nonkeyframes;
  165. int nb_copy_initial_nonkeyframes;
  166. SpecifierOpt *filters;
  167. int nb_filters;
  168. SpecifierOpt *filter_scripts;
  169. int nb_filter_scripts;
  170. SpecifierOpt *pass;
  171. int nb_pass;
  172. SpecifierOpt *passlogfiles;
  173. int nb_passlogfiles;
  174. SpecifierOpt *max_muxing_queue_size;
  175. int nb_max_muxing_queue_size;
  176. } OptionsContext;
  177. typedef struct InputFilter {
  178. AVFilterContext *filter;
  179. struct InputStream *ist;
  180. struct FilterGraph *graph;
  181. uint8_t *name;
  182. AVFifoBuffer *frame_queue;
  183. // parameters configured for this input
  184. int format;
  185. int width, height;
  186. AVRational sample_aspect_ratio;
  187. int sample_rate;
  188. uint64_t channel_layout;
  189. AVBufferRef *hw_frames_ctx;
  190. int eof;
  191. } InputFilter;
  192. typedef struct OutputFilter {
  193. AVFilterContext *filter;
  194. struct OutputStream *ost;
  195. struct FilterGraph *graph;
  196. uint8_t *name;
  197. /* temporary storage until stream maps are processed */
  198. AVFilterInOut *out_tmp;
  199. enum AVMediaType type;
  200. /* desired output stream properties */
  201. int width, height;
  202. AVRational frame_rate;
  203. int format;
  204. int sample_rate;
  205. uint64_t channel_layout;
  206. // those are only set if no format is specified and the encoder gives us multiple options
  207. int *formats;
  208. uint64_t *channel_layouts;
  209. int *sample_rates;
  210. } OutputFilter;
  211. typedef struct FilterGraph {
  212. int index;
  213. const char *graph_desc;
  214. AVFilterGraph *graph;
  215. InputFilter **inputs;
  216. int nb_inputs;
  217. OutputFilter **outputs;
  218. int nb_outputs;
  219. } FilterGraph;
  220. typedef struct InputStream {
  221. int file_index;
  222. AVStream *st;
  223. int discard; /* true if stream data should be discarded */
  224. int decoding_needed; /* true if the packets must be decoded in 'raw_fifo' */
  225. AVCodecContext *dec_ctx;
  226. AVCodec *dec;
  227. AVFrame *decoded_frame;
  228. AVFrame *filter_frame; /* a ref of decoded_frame, to be sent to filters */
  229. int64_t start; /* time when read started */
  230. /* predicted dts of the next packet read for this stream or (when there are
  231. * several frames in a packet) of the next frame in current packet */
  232. int64_t next_dts;
  233. /* dts of the last packet read for this stream */
  234. int64_t last_dts;
  235. int64_t min_pts; /* pts with the smallest value in a current stream */
  236. int64_t max_pts; /* pts with the higher value in a current stream */
  237. // when forcing constant input framerate through -r,
  238. // this contains the pts that will be given to the next decoded frame
  239. int64_t cfr_next_pts;
  240. int64_t nb_samples; /* number of samples in the last decoded audio frame before looping */
  241. PtsCorrectionContext pts_ctx;
  242. double ts_scale;
  243. AVDictionary *decoder_opts;
  244. AVRational framerate; /* framerate forced with -r */
  245. int autorotate;
  246. /* decoded data from this stream goes into all those filters
  247. * currently video and audio only */
  248. InputFilter **filters;
  249. int nb_filters;
  250. /* hwaccel options */
  251. enum HWAccelID hwaccel_id;
  252. enum AVHWDeviceType hwaccel_device_type;
  253. char *hwaccel_device;
  254. enum AVPixelFormat hwaccel_output_format;
  255. /* hwaccel context */
  256. void *hwaccel_ctx;
  257. void (*hwaccel_uninit)(AVCodecContext *s);
  258. int (*hwaccel_get_buffer)(AVCodecContext *s, AVFrame *frame, int flags);
  259. int (*hwaccel_retrieve_data)(AVCodecContext *s, AVFrame *frame);
  260. enum AVPixelFormat hwaccel_pix_fmt;
  261. enum AVPixelFormat hwaccel_retrieved_pix_fmt;
  262. AVBufferRef *hw_frames_ctx;
  263. /* stats */
  264. // combined size of all the packets read
  265. uint64_t data_size;
  266. /* number of packets successfully read for this stream */
  267. uint64_t nb_packets;
  268. // number of frames/samples retrieved from the decoder
  269. uint64_t frames_decoded;
  270. uint64_t samples_decoded;
  271. } InputStream;
  272. typedef struct InputFile {
  273. AVFormatContext *ctx;
  274. int eof_reached; /* true if eof reached */
  275. int eagain; /* true if last read attempt returned EAGAIN */
  276. int ist_index; /* index of first stream in ist_table */
  277. int loop; /* set number of times input stream should be looped */
  278. int64_t duration; /* actual duration of the longest stream in a file
  279. at the moment when looping happens */
  280. AVRational time_base; /* time base of the duration */
  281. int64_t ts_offset;
  282. int64_t start_time; /* user-specified start time in AV_TIME_BASE or AV_NOPTS_VALUE */
  283. int64_t recording_time;
  284. int nb_streams; /* number of stream that avconv is aware of; may be different
  285. from ctx.nb_streams if new streams appear during av_read_frame() */
  286. int rate_emu;
  287. int accurate_seek;
  288. #if HAVE_PTHREADS
  289. pthread_t thread; /* thread reading from this file */
  290. int finished; /* the thread has exited */
  291. int joined; /* the thread has been joined */
  292. pthread_mutex_t fifo_lock; /* lock for access to fifo */
  293. pthread_cond_t fifo_cond; /* the main thread will signal on this cond after reading from fifo */
  294. AVFifoBuffer *fifo; /* demuxed packets are stored here; freed by the main thread */
  295. #endif
  296. } InputFile;
  297. typedef struct OutputStream {
  298. int file_index; /* file index */
  299. int index; /* stream index in the output file */
  300. int source_index; /* InputStream index */
  301. AVStream *st; /* stream in the output file */
  302. int encoding_needed; /* true if encoding needed for this stream */
  303. int frame_number;
  304. /* input pts and corresponding output pts
  305. for A/V sync */
  306. // double sync_ipts; /* dts from the AVPacket of the demuxer in second units */
  307. struct InputStream *sync_ist; /* input stream to sync against */
  308. int64_t sync_opts; /* output frame counter, could be changed to some true timestamp */ // FIXME look at frame_number
  309. /* pts of the first frame encoded for this stream, used for limiting
  310. * recording time */
  311. int64_t first_pts;
  312. /* dts of the last packet sent to the muxer */
  313. int64_t last_mux_dts;
  314. // the timebase of the packets sent to the muxer
  315. AVRational mux_timebase;
  316. int nb_bitstream_filters;
  317. AVBSFContext **bsf_ctx;
  318. AVCodecContext *enc_ctx;
  319. AVCodec *enc;
  320. int64_t max_frames;
  321. AVFrame *filtered_frame;
  322. void *hwaccel_ctx;
  323. /* video only */
  324. AVRational frame_rate;
  325. int force_fps;
  326. int top_field_first;
  327. float frame_aspect_ratio;
  328. /* forced key frames */
  329. int64_t *forced_kf_pts;
  330. int forced_kf_count;
  331. int forced_kf_index;
  332. char *forced_keyframes;
  333. // the bitrate to send to the muxer for streamcopy
  334. int bitrate_override;
  335. char *logfile_prefix;
  336. FILE *logfile;
  337. OutputFilter *filter;
  338. char *avfilter;
  339. int64_t sws_flags;
  340. AVDictionary *encoder_opts;
  341. AVDictionary *resample_opts;
  342. int finished; /* no more packets should be written for this stream */
  343. int stream_copy;
  344. // init_output_stream() has been called for this stream
  345. // The encoder and the bistream filters have been initialized and the stream
  346. // parameters are set in the AVStream.
  347. int initialized;
  348. const char *attachment_filename;
  349. int copy_initial_nonkeyframes;
  350. enum AVPixelFormat pix_fmts[2];
  351. AVCodecParserContext *parser;
  352. AVCodecContext *parser_avctx;
  353. /* stats */
  354. // combined size of all the packets written
  355. uint64_t data_size;
  356. // number of packets send to the muxer
  357. uint64_t packets_written;
  358. // number of frames/samples sent to the encoder
  359. uint64_t frames_encoded;
  360. uint64_t samples_encoded;
  361. /* packet quality factor */
  362. int quality;
  363. int max_muxing_queue_size;
  364. /* the packets are buffered here until the muxer is ready to be initialized */
  365. AVFifoBuffer *muxing_queue;
  366. } OutputStream;
  367. typedef struct OutputFile {
  368. AVFormatContext *ctx;
  369. AVDictionary *opts;
  370. int ost_index; /* index of the first stream in output_streams */
  371. int64_t recording_time; /* desired length of the resulting file in microseconds */
  372. int64_t start_time; /* start time in microseconds */
  373. uint64_t limit_filesize;
  374. int shortest;
  375. int header_written;
  376. } OutputFile;
  377. extern InputStream **input_streams;
  378. extern int nb_input_streams;
  379. extern InputFile **input_files;
  380. extern int nb_input_files;
  381. extern OutputStream **output_streams;
  382. extern int nb_output_streams;
  383. extern OutputFile **output_files;
  384. extern int nb_output_files;
  385. extern FilterGraph **filtergraphs;
  386. extern int nb_filtergraphs;
  387. extern char *vstats_filename;
  388. extern float audio_drift_threshold;
  389. extern float dts_delta_threshold;
  390. extern int audio_volume;
  391. extern int audio_sync_method;
  392. extern int video_sync_method;
  393. extern int do_benchmark;
  394. extern int do_deinterlace;
  395. extern int do_hex_dump;
  396. extern int do_pkt_dump;
  397. extern int copy_ts;
  398. extern int copy_tb;
  399. extern int exit_on_error;
  400. extern int print_stats;
  401. extern int qp_hist;
  402. extern const AVIOInterruptCB int_cb;
  403. extern const OptionDef options[];
  404. extern const HWAccel hwaccels[];
  405. extern int hwaccel_lax_profile_check;
  406. extern AVBufferRef *hw_device_ctx;
  407. extern HWDevice *filter_hw_device;
  408. void reset_options(OptionsContext *o);
  409. void show_usage(void);
  410. void opt_output_file(void *optctx, const char *filename);
  411. void assert_avoptions(AVDictionary *m);
  412. int guess_input_channel_layout(InputStream *ist);
  413. int configure_filtergraph(FilterGraph *fg);
  414. int configure_output_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out);
  415. int ist_in_filtergraph(FilterGraph *fg, InputStream *ist);
  416. int filtergraph_is_simple(FilterGraph *fg);
  417. int init_simple_filtergraph(InputStream *ist, OutputStream *ost);
  418. int init_complex_filtergraph(FilterGraph *fg);
  419. int ifilter_parameters_from_frame(InputFilter *ifilter, const AVFrame *frame);
  420. int avconv_parse_options(int argc, char **argv);
  421. int dxva2_init(AVCodecContext *s);
  422. int vda_init(AVCodecContext *s);
  423. int qsv_init(AVCodecContext *s);
  424. int qsv_transcode_init(OutputStream *ost);
  425. HWDevice *hw_device_get_by_name(const char *name);
  426. int hw_device_init_from_string(const char *arg, HWDevice **dev);
  427. void hw_device_free_all(void);
  428. int hw_device_setup_for_decode(InputStream *ist);
  429. int hw_device_setup_for_encode(OutputStream *ost);
  430. int hwaccel_decode_init(AVCodecContext *avctx);
  431. #endif /* AVCONV_H */