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.

514 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 *bitrates;
  141. int nb_bitrates;
  142. SpecifierOpt *forced_key_frames;
  143. int nb_forced_key_frames;
  144. SpecifierOpt *force_fps;
  145. int nb_force_fps;
  146. SpecifierOpt *frame_aspect_ratios;
  147. int nb_frame_aspect_ratios;
  148. SpecifierOpt *rc_overrides;
  149. int nb_rc_overrides;
  150. SpecifierOpt *intra_matrices;
  151. int nb_intra_matrices;
  152. SpecifierOpt *inter_matrices;
  153. int nb_inter_matrices;
  154. SpecifierOpt *top_field_first;
  155. int nb_top_field_first;
  156. SpecifierOpt *metadata_map;
  157. int nb_metadata_map;
  158. SpecifierOpt *presets;
  159. int nb_presets;
  160. SpecifierOpt *copy_initial_nonkeyframes;
  161. int nb_copy_initial_nonkeyframes;
  162. SpecifierOpt *filters;
  163. int nb_filters;
  164. SpecifierOpt *filter_scripts;
  165. int nb_filter_scripts;
  166. SpecifierOpt *pass;
  167. int nb_pass;
  168. SpecifierOpt *passlogfiles;
  169. int nb_passlogfiles;
  170. SpecifierOpt *max_muxing_queue_size;
  171. int nb_max_muxing_queue_size;
  172. } OptionsContext;
  173. typedef struct InputFilter {
  174. AVFilterContext *filter;
  175. struct InputStream *ist;
  176. struct FilterGraph *graph;
  177. uint8_t *name;
  178. AVFifoBuffer *frame_queue;
  179. // parameters configured for this input
  180. int format;
  181. int width, height;
  182. AVRational sample_aspect_ratio;
  183. int sample_rate;
  184. uint64_t channel_layout;
  185. AVBufferRef *hw_frames_ctx;
  186. int eof;
  187. } InputFilter;
  188. typedef struct OutputFilter {
  189. AVFilterContext *filter;
  190. struct OutputStream *ost;
  191. struct FilterGraph *graph;
  192. uint8_t *name;
  193. /* temporary storage until stream maps are processed */
  194. AVFilterInOut *out_tmp;
  195. enum AVMediaType type;
  196. /* desired output stream properties */
  197. int width, height;
  198. AVRational frame_rate;
  199. int format;
  200. int sample_rate;
  201. uint64_t channel_layout;
  202. // those are only set if no format is specified and the encoder gives us multiple options
  203. int *formats;
  204. uint64_t *channel_layouts;
  205. int *sample_rates;
  206. } OutputFilter;
  207. typedef struct FilterGraph {
  208. int index;
  209. const char *graph_desc;
  210. AVFilterGraph *graph;
  211. InputFilter **inputs;
  212. int nb_inputs;
  213. OutputFilter **outputs;
  214. int nb_outputs;
  215. } FilterGraph;
  216. typedef struct InputStream {
  217. int file_index;
  218. AVStream *st;
  219. int discard; /* true if stream data should be discarded */
  220. int decoding_needed; /* true if the packets must be decoded in 'raw_fifo' */
  221. AVCodecContext *dec_ctx;
  222. AVCodec *dec;
  223. AVFrame *decoded_frame;
  224. AVFrame *filter_frame; /* a ref of decoded_frame, to be sent to filters */
  225. int64_t start; /* time when read started */
  226. /* predicted dts of the next packet read for this stream or (when there are
  227. * several frames in a packet) of the next frame in current packet */
  228. int64_t next_dts;
  229. /* dts of the last packet read for this stream */
  230. int64_t last_dts;
  231. int64_t min_pts; /* pts with the smallest value in a current stream */
  232. int64_t max_pts; /* pts with the higher value in a current stream */
  233. // when forcing constant input framerate through -r,
  234. // this contains the pts that will be given to the next decoded frame
  235. int64_t cfr_next_pts;
  236. int64_t nb_samples; /* number of samples in the last decoded audio frame before looping */
  237. PtsCorrectionContext pts_ctx;
  238. double ts_scale;
  239. AVDictionary *decoder_opts;
  240. AVRational framerate; /* framerate forced with -r */
  241. int autorotate;
  242. /* decoded data from this stream goes into all those filters
  243. * currently video and audio only */
  244. InputFilter **filters;
  245. int nb_filters;
  246. /* hwaccel options */
  247. enum HWAccelID hwaccel_id;
  248. char *hwaccel_device;
  249. enum AVPixelFormat hwaccel_output_format;
  250. /* hwaccel context */
  251. enum HWAccelID active_hwaccel_id;
  252. void *hwaccel_ctx;
  253. void (*hwaccel_uninit)(AVCodecContext *s);
  254. int (*hwaccel_get_buffer)(AVCodecContext *s, AVFrame *frame, int flags);
  255. int (*hwaccel_retrieve_data)(AVCodecContext *s, AVFrame *frame);
  256. enum AVPixelFormat hwaccel_pix_fmt;
  257. enum AVPixelFormat hwaccel_retrieved_pix_fmt;
  258. AVBufferRef *hw_frames_ctx;
  259. /* stats */
  260. // combined size of all the packets read
  261. uint64_t data_size;
  262. /* number of packets successfully read for this stream */
  263. uint64_t nb_packets;
  264. // number of frames/samples retrieved from the decoder
  265. uint64_t frames_decoded;
  266. uint64_t samples_decoded;
  267. } InputStream;
  268. typedef struct InputFile {
  269. AVFormatContext *ctx;
  270. int eof_reached; /* true if eof reached */
  271. int eagain; /* true if last read attempt returned EAGAIN */
  272. int ist_index; /* index of first stream in ist_table */
  273. int loop; /* set number of times input stream should be looped */
  274. int64_t duration; /* actual duration of the longest stream in a file
  275. at the moment when looping happens */
  276. AVRational time_base; /* time base of the duration */
  277. int64_t ts_offset;
  278. int64_t start_time; /* user-specified start time in AV_TIME_BASE or AV_NOPTS_VALUE */
  279. int64_t recording_time;
  280. int nb_streams; /* number of stream that avconv is aware of; may be different
  281. from ctx.nb_streams if new streams appear during av_read_frame() */
  282. int rate_emu;
  283. int accurate_seek;
  284. #if HAVE_PTHREADS
  285. pthread_t thread; /* thread reading from this file */
  286. int finished; /* the thread has exited */
  287. int joined; /* the thread has been joined */
  288. pthread_mutex_t fifo_lock; /* lock for access to fifo */
  289. pthread_cond_t fifo_cond; /* the main thread will signal on this cond after reading from fifo */
  290. AVFifoBuffer *fifo; /* demuxed packets are stored here; freed by the main thread */
  291. #endif
  292. } InputFile;
  293. typedef struct OutputStream {
  294. int file_index; /* file index */
  295. int index; /* stream index in the output file */
  296. int source_index; /* InputStream index */
  297. AVStream *st; /* stream in the output file */
  298. int encoding_needed; /* true if encoding needed for this stream */
  299. int frame_number;
  300. /* input pts and corresponding output pts
  301. for A/V sync */
  302. // double sync_ipts; /* dts from the AVPacket of the demuxer in second units */
  303. struct InputStream *sync_ist; /* input stream to sync against */
  304. int64_t sync_opts; /* output frame counter, could be changed to some true timestamp */ // FIXME look at frame_number
  305. /* pts of the first frame encoded for this stream, used for limiting
  306. * recording time */
  307. int64_t first_pts;
  308. /* dts of the last packet sent to the muxer */
  309. int64_t last_mux_dts;
  310. // the timebase of the packets sent to the muxer
  311. AVRational mux_timebase;
  312. int nb_bitstream_filters;
  313. AVBSFContext **bsf_ctx;
  314. AVCodecContext *enc_ctx;
  315. AVCodec *enc;
  316. int64_t max_frames;
  317. AVFrame *filtered_frame;
  318. void *hwaccel_ctx;
  319. /* video only */
  320. AVRational frame_rate;
  321. int force_fps;
  322. int top_field_first;
  323. float frame_aspect_ratio;
  324. /* forced key frames */
  325. int64_t *forced_kf_pts;
  326. int forced_kf_count;
  327. int forced_kf_index;
  328. char *forced_keyframes;
  329. // the bitrate to send to the muxer for streamcopy
  330. int bitrate_override;
  331. char *logfile_prefix;
  332. FILE *logfile;
  333. OutputFilter *filter;
  334. char *avfilter;
  335. int64_t sws_flags;
  336. AVDictionary *encoder_opts;
  337. AVDictionary *resample_opts;
  338. int finished; /* no more packets should be written for this stream */
  339. int stream_copy;
  340. // init_output_stream() has been called for this stream
  341. // The encoder and the bistream filters have been initialized and the stream
  342. // parameters are set in the AVStream.
  343. int initialized;
  344. const char *attachment_filename;
  345. int copy_initial_nonkeyframes;
  346. enum AVPixelFormat pix_fmts[2];
  347. AVCodecParserContext *parser;
  348. AVCodecContext *parser_avctx;
  349. /* stats */
  350. // combined size of all the packets written
  351. uint64_t data_size;
  352. // number of packets send to the muxer
  353. uint64_t packets_written;
  354. // number of frames/samples sent to the encoder
  355. uint64_t frames_encoded;
  356. uint64_t samples_encoded;
  357. /* packet quality factor */
  358. int quality;
  359. int max_muxing_queue_size;
  360. /* the packets are buffered here until the muxer is ready to be initialized */
  361. AVFifoBuffer *muxing_queue;
  362. } OutputStream;
  363. typedef struct OutputFile {
  364. AVFormatContext *ctx;
  365. AVDictionary *opts;
  366. int ost_index; /* index of the first stream in output_streams */
  367. int64_t recording_time; /* desired length of the resulting file in microseconds */
  368. int64_t start_time; /* start time in microseconds */
  369. uint64_t limit_filesize;
  370. int shortest;
  371. int header_written;
  372. } OutputFile;
  373. extern InputStream **input_streams;
  374. extern int nb_input_streams;
  375. extern InputFile **input_files;
  376. extern int nb_input_files;
  377. extern OutputStream **output_streams;
  378. extern int nb_output_streams;
  379. extern OutputFile **output_files;
  380. extern int nb_output_files;
  381. extern FilterGraph **filtergraphs;
  382. extern int nb_filtergraphs;
  383. extern char *vstats_filename;
  384. extern float audio_drift_threshold;
  385. extern float dts_delta_threshold;
  386. extern int audio_volume;
  387. extern int audio_sync_method;
  388. extern int video_sync_method;
  389. extern int do_benchmark;
  390. extern int do_deinterlace;
  391. extern int do_hex_dump;
  392. extern int do_pkt_dump;
  393. extern int copy_ts;
  394. extern int copy_tb;
  395. extern int exit_on_error;
  396. extern int print_stats;
  397. extern int qp_hist;
  398. extern const AVIOInterruptCB int_cb;
  399. extern const OptionDef options[];
  400. extern const HWAccel hwaccels[];
  401. extern int hwaccel_lax_profile_check;
  402. extern AVBufferRef *hw_device_ctx;
  403. void reset_options(OptionsContext *o);
  404. void show_usage(void);
  405. void opt_output_file(void *optctx, const char *filename);
  406. void assert_avoptions(AVDictionary *m);
  407. int guess_input_channel_layout(InputStream *ist);
  408. int configure_filtergraph(FilterGraph *fg);
  409. int configure_output_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out);
  410. int ist_in_filtergraph(FilterGraph *fg, InputStream *ist);
  411. int filtergraph_is_simple(FilterGraph *fg);
  412. int init_simple_filtergraph(InputStream *ist, OutputStream *ost);
  413. int init_complex_filtergraph(FilterGraph *fg);
  414. int ifilter_parameters_from_frame(InputFilter *ifilter, const AVFrame *frame);
  415. int avconv_parse_options(int argc, char **argv);
  416. int vdpau_init(AVCodecContext *s);
  417. int dxva2_init(AVCodecContext *s);
  418. int vda_init(AVCodecContext *s);
  419. int qsv_init(AVCodecContext *s);
  420. int qsv_transcode_init(OutputStream *ost);
  421. int vaapi_decode_init(AVCodecContext *avctx);
  422. int vaapi_device_init(const char *device);
  423. #endif /* AVCONV_H */