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.

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