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.

453 lines
13KB

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