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.

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