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.

426 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. /* stats */
  225. // combined size of all the packets read
  226. uint64_t data_size;
  227. /* number of packets successfully read for this stream */
  228. uint64_t nb_packets;
  229. // number of frames/samples retrieved from the decoder
  230. uint64_t frames_decoded;
  231. uint64_t samples_decoded;
  232. } InputStream;
  233. typedef struct InputFile {
  234. AVFormatContext *ctx;
  235. int eof_reached; /* true if eof reached */
  236. int eagain; /* true if last read attempt returned EAGAIN */
  237. int ist_index; /* index of first stream in ist_table */
  238. int64_t ts_offset;
  239. int64_t start_time; /* user-specified start time in AV_TIME_BASE or AV_NOPTS_VALUE */
  240. int64_t recording_time;
  241. int nb_streams; /* number of stream that avconv is aware of; may be different
  242. from ctx.nb_streams if new streams appear during av_read_frame() */
  243. int rate_emu;
  244. int accurate_seek;
  245. #if HAVE_PTHREADS
  246. pthread_t thread; /* thread reading from this file */
  247. int finished; /* the thread has exited */
  248. int joined; /* the thread has been joined */
  249. pthread_mutex_t fifo_lock; /* lock for access to fifo */
  250. pthread_cond_t fifo_cond; /* the main thread will signal on this cond after reading from fifo */
  251. AVFifoBuffer *fifo; /* demuxed packets are stored here; freed by the main thread */
  252. #endif
  253. } InputFile;
  254. typedef struct OutputStream {
  255. int file_index; /* file index */
  256. int index; /* stream index in the output file */
  257. int source_index; /* InputStream index */
  258. AVStream *st; /* stream in the output file */
  259. int encoding_needed; /* true if encoding needed for this stream */
  260. int frame_number;
  261. /* input pts and corresponding output pts
  262. for A/V sync */
  263. // double sync_ipts; /* dts from the AVPacket of the demuxer in second units */
  264. struct InputStream *sync_ist; /* input stream to sync against */
  265. int64_t sync_opts; /* output frame counter, could be changed to some true timestamp */ // FIXME look at frame_number
  266. /* pts of the first frame encoded for this stream, used for limiting
  267. * recording time */
  268. int64_t first_pts;
  269. /* dts of the last packet sent to the muxer */
  270. int64_t last_mux_dts;
  271. AVBitStreamFilterContext *bitstream_filters;
  272. AVCodec *enc;
  273. int64_t max_frames;
  274. AVFrame *filtered_frame;
  275. /* video only */
  276. AVRational frame_rate;
  277. int force_fps;
  278. int top_field_first;
  279. float frame_aspect_ratio;
  280. /* forced key frames */
  281. int64_t *forced_kf_pts;
  282. int forced_kf_count;
  283. int forced_kf_index;
  284. char *forced_keyframes;
  285. char *logfile_prefix;
  286. FILE *logfile;
  287. OutputFilter *filter;
  288. char *avfilter;
  289. int64_t sws_flags;
  290. AVDictionary *opts;
  291. AVDictionary *resample_opts;
  292. int finished; /* no more packets should be written for this stream */
  293. int stream_copy;
  294. const char *attachment_filename;
  295. int copy_initial_nonkeyframes;
  296. enum AVPixelFormat pix_fmts[2];
  297. AVCodecParserContext *parser;
  298. /* stats */
  299. // combined size of all the packets written
  300. uint64_t data_size;
  301. // number of packets send to the muxer
  302. uint64_t packets_written;
  303. // number of frames/samples sent to the encoder
  304. uint64_t frames_encoded;
  305. uint64_t samples_encoded;
  306. } OutputStream;
  307. typedef struct OutputFile {
  308. AVFormatContext *ctx;
  309. AVDictionary *opts;
  310. int ost_index; /* index of the first stream in output_streams */
  311. int64_t recording_time; /* desired length of the resulting file in microseconds */
  312. int64_t start_time; /* start time in microseconds */
  313. uint64_t limit_filesize;
  314. int shortest;
  315. } OutputFile;
  316. extern InputStream **input_streams;
  317. extern int nb_input_streams;
  318. extern InputFile **input_files;
  319. extern int nb_input_files;
  320. extern OutputStream **output_streams;
  321. extern int nb_output_streams;
  322. extern OutputFile **output_files;
  323. extern int nb_output_files;
  324. extern FilterGraph **filtergraphs;
  325. extern int nb_filtergraphs;
  326. extern char *vstats_filename;
  327. extern float audio_drift_threshold;
  328. extern float dts_delta_threshold;
  329. extern int audio_volume;
  330. extern int audio_sync_method;
  331. extern int video_sync_method;
  332. extern int do_benchmark;
  333. extern int do_deinterlace;
  334. extern int do_hex_dump;
  335. extern int do_pkt_dump;
  336. extern int copy_ts;
  337. extern int copy_tb;
  338. extern int exit_on_error;
  339. extern int print_stats;
  340. extern int qp_hist;
  341. extern const AVIOInterruptCB int_cb;
  342. extern const OptionDef options[];
  343. extern const HWAccel hwaccels[];
  344. void reset_options(OptionsContext *o);
  345. void show_usage(void);
  346. void opt_output_file(void *optctx, const char *filename);
  347. void assert_avoptions(AVDictionary *m);
  348. int guess_input_channel_layout(InputStream *ist);
  349. int configure_filtergraph(FilterGraph *fg);
  350. int configure_output_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out);
  351. int ist_in_filtergraph(FilterGraph *fg, InputStream *ist);
  352. FilterGraph *init_simple_filtergraph(InputStream *ist, OutputStream *ost);
  353. int avconv_parse_options(int argc, char **argv);
  354. int vdpau_init(AVCodecContext *s);
  355. #endif /* AVCONV_H */