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.

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