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.

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