avconv.h 14 KB

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