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.

406 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. };
  44. typedef struct HWAccel {
  45. const char *name;
  46. int (*init)(AVCodecContext *s);
  47. enum HWAccelID id;
  48. enum AVPixelFormat pix_fmt;
  49. } HWAccel;
  50. /* select an input stream for an output stream */
  51. typedef struct StreamMap {
  52. int disabled; /* 1 is this mapping is disabled by a negative map */
  53. int file_index;
  54. int stream_index;
  55. int sync_file_index;
  56. int sync_stream_index;
  57. char *linklabel; /* name of an output link, for mapping lavfi outputs */
  58. } StreamMap;
  59. /* select an input file for an output file */
  60. typedef struct MetadataMap {
  61. int file; // file index
  62. char type; // type of metadata to copy -- (g)lobal, (s)tream, (c)hapter or (p)rogram
  63. int index; // stream/chapter/program number
  64. } MetadataMap;
  65. typedef struct OptionsContext {
  66. OptionGroup *g;
  67. /* input/output options */
  68. int64_t start_time;
  69. const char *format;
  70. SpecifierOpt *codec_names;
  71. int nb_codec_names;
  72. SpecifierOpt *audio_channels;
  73. int nb_audio_channels;
  74. SpecifierOpt *audio_sample_rate;
  75. int nb_audio_sample_rate;
  76. SpecifierOpt *frame_rates;
  77. int nb_frame_rates;
  78. SpecifierOpt *frame_sizes;
  79. int nb_frame_sizes;
  80. SpecifierOpt *frame_pix_fmts;
  81. int nb_frame_pix_fmts;
  82. /* input options */
  83. int64_t input_ts_offset;
  84. int rate_emu;
  85. int accurate_seek;
  86. SpecifierOpt *ts_scale;
  87. int nb_ts_scale;
  88. SpecifierOpt *dump_attachment;
  89. int nb_dump_attachment;
  90. SpecifierOpt *hwaccels;
  91. int nb_hwaccels;
  92. SpecifierOpt *hwaccel_devices;
  93. int nb_hwaccel_devices;
  94. /* output options */
  95. StreamMap *stream_maps;
  96. int nb_stream_maps;
  97. /* first item specifies output metadata, second is input */
  98. MetadataMap (*meta_data_maps)[2];
  99. int nb_meta_data_maps;
  100. int metadata_global_manual;
  101. int metadata_streams_manual;
  102. int metadata_chapters_manual;
  103. const char **attachments;
  104. int nb_attachments;
  105. int chapters_input_file;
  106. int64_t recording_time;
  107. uint64_t limit_filesize;
  108. float mux_preload;
  109. float mux_max_delay;
  110. int shortest;
  111. int video_disable;
  112. int audio_disable;
  113. int subtitle_disable;
  114. int data_disable;
  115. /* indexed by output file stream index */
  116. int *streamid_map;
  117. int nb_streamid_map;
  118. SpecifierOpt *metadata;
  119. int nb_metadata;
  120. SpecifierOpt *max_frames;
  121. int nb_max_frames;
  122. SpecifierOpt *bitstream_filters;
  123. int nb_bitstream_filters;
  124. SpecifierOpt *codec_tags;
  125. int nb_codec_tags;
  126. SpecifierOpt *sample_fmts;
  127. int nb_sample_fmts;
  128. SpecifierOpt *qscale;
  129. int nb_qscale;
  130. SpecifierOpt *forced_key_frames;
  131. int nb_forced_key_frames;
  132. SpecifierOpt *force_fps;
  133. int nb_force_fps;
  134. SpecifierOpt *frame_aspect_ratios;
  135. int nb_frame_aspect_ratios;
  136. SpecifierOpt *rc_overrides;
  137. int nb_rc_overrides;
  138. SpecifierOpt *intra_matrices;
  139. int nb_intra_matrices;
  140. SpecifierOpt *inter_matrices;
  141. int nb_inter_matrices;
  142. SpecifierOpt *top_field_first;
  143. int nb_top_field_first;
  144. SpecifierOpt *metadata_map;
  145. int nb_metadata_map;
  146. SpecifierOpt *presets;
  147. int nb_presets;
  148. SpecifierOpt *copy_initial_nonkeyframes;
  149. int nb_copy_initial_nonkeyframes;
  150. SpecifierOpt *filters;
  151. int nb_filters;
  152. SpecifierOpt *filter_scripts;
  153. int nb_filter_scripts;
  154. SpecifierOpt *pass;
  155. int nb_pass;
  156. SpecifierOpt *passlogfiles;
  157. int nb_passlogfiles;
  158. } OptionsContext;
  159. typedef struct InputFilter {
  160. AVFilterContext *filter;
  161. struct InputStream *ist;
  162. struct FilterGraph *graph;
  163. uint8_t *name;
  164. } InputFilter;
  165. typedef struct OutputFilter {
  166. AVFilterContext *filter;
  167. struct OutputStream *ost;
  168. struct FilterGraph *graph;
  169. uint8_t *name;
  170. /* temporary storage until stream maps are processed */
  171. AVFilterInOut *out_tmp;
  172. } OutputFilter;
  173. typedef struct FilterGraph {
  174. int index;
  175. const char *graph_desc;
  176. AVFilterGraph *graph;
  177. InputFilter **inputs;
  178. int nb_inputs;
  179. OutputFilter **outputs;
  180. int nb_outputs;
  181. } FilterGraph;
  182. typedef struct InputStream {
  183. int file_index;
  184. AVStream *st;
  185. int discard; /* true if stream data should be discarded */
  186. int decoding_needed; /* true if the packets must be decoded in 'raw_fifo' */
  187. AVCodec *dec;
  188. AVFrame *decoded_frame;
  189. AVFrame *filter_frame; /* a ref of decoded_frame, to be sent to filters */
  190. int64_t start; /* time when read started */
  191. /* predicted dts of the next packet read for this stream or (when there are
  192. * several frames in a packet) of the next frame in current packet */
  193. int64_t next_dts;
  194. /* dts of the last packet read for this stream */
  195. int64_t last_dts;
  196. PtsCorrectionContext pts_ctx;
  197. double ts_scale;
  198. int is_start; /* is 1 at the start and after a discontinuity */
  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. #endif /* AVCONV_H */