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.

363 lines
11KB

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