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.

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