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.

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