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.

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