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.

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