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.

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