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.

428 lines
12KB

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