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.

392 lines
12KB

  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg 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. * FFmpeg 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 FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #ifndef FFMPEG_H
  19. #define FFMPEG_H
  20. #include "config.h"
  21. #include <stdint.h>
  22. #include <stdio.h>
  23. #include <signal.h>
  24. #if HAVE_PTHREADS
  25. #include <pthread.h>
  26. #endif
  27. #include "cmdutils.h"
  28. #include "libavformat/avformat.h"
  29. #include "libavformat/avio.h"
  30. #include "libavcodec/avcodec.h"
  31. #include "libavfilter/avfilter.h"
  32. #include "libavfilter/avfiltergraph.h"
  33. #include "libavutil/avutil.h"
  34. #include "libavutil/dict.h"
  35. #include "libavutil/fifo.h"
  36. #include "libavutil/pixfmt.h"
  37. #include "libavutil/rational.h"
  38. #include "libswresample/swresample.h"
  39. #define VSYNC_AUTO -1
  40. #define VSYNC_PASSTHROUGH 0
  41. #define VSYNC_CFR 1
  42. #define VSYNC_VFR 2
  43. #define VSYNC_DROP 0xff
  44. #define MAX_STREAMS 1024 /* arbitrary sanity check value */
  45. /* select an input stream for an output stream */
  46. typedef struct StreamMap {
  47. int disabled; /** 1 is this mapping is disabled by a negative map */
  48. int file_index;
  49. int stream_index;
  50. int sync_file_index;
  51. int sync_stream_index;
  52. char *linklabel; /** name of an output link, for mapping lavfi outputs */
  53. } StreamMap;
  54. typedef struct {
  55. int file_idx, stream_idx, channel_idx; // input
  56. int ofile_idx, ostream_idx; // output
  57. } AudioChannelMap;
  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. AudioChannelMap *audio_channel_maps; /* one info entry per -map_channel */
  85. int nb_audio_channel_maps; /* number of (valid) -map_channel settings */
  86. int metadata_global_manual;
  87. int metadata_streams_manual;
  88. int metadata_chapters_manual;
  89. const char **attachments;
  90. int nb_attachments;
  91. int chapters_input_file;
  92. int64_t recording_time;
  93. uint64_t limit_filesize;
  94. float mux_preload;
  95. float mux_max_delay;
  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. } OptionsContext;
  138. typedef struct InputFilter {
  139. AVFilterContext *filter;
  140. struct InputStream *ist;
  141. struct FilterGraph *graph;
  142. uint8_t *name;
  143. } InputFilter;
  144. typedef struct OutputFilter {
  145. AVFilterContext *filter;
  146. struct OutputStream *ost;
  147. struct FilterGraph *graph;
  148. uint8_t *name;
  149. /* temporary storage until stream maps are processed */
  150. AVFilterInOut *out_tmp;
  151. } OutputFilter;
  152. typedef struct FilterGraph {
  153. int index;
  154. const char *graph_desc;
  155. AVFilterGraph *graph;
  156. InputFilter **inputs;
  157. int nb_inputs;
  158. OutputFilter **outputs;
  159. int nb_outputs;
  160. } FilterGraph;
  161. typedef struct InputStream {
  162. int file_index;
  163. AVStream *st;
  164. int discard; /* true if stream data should be discarded */
  165. int decoding_needed; /* true if the packets must be decoded in 'raw_fifo' */
  166. AVCodec *dec;
  167. AVFrame *decoded_frame;
  168. int64_t start; /* time when read started */
  169. /* predicted dts of the next packet read for this stream or (when there are
  170. * several frames in a packet) of the next frame in current packet (in AV_TIME_BASE units) */
  171. int64_t next_dts;
  172. int64_t dts; ///< dts of the last packet read for this stream (in AV_TIME_BASE units)
  173. int64_t next_pts; ///< synthetic pts for the next decode frame (in AV_TIME_BASE units)
  174. int64_t pts; ///< current pts of the decoded frame (in AV_TIME_BASE units)
  175. int wrap_correction_done;
  176. double ts_scale;
  177. int is_start; /* is 1 at the start and after a discontinuity */
  178. int saw_first_ts;
  179. int showed_multi_packet_warning;
  180. AVDictionary *opts;
  181. AVRational framerate; /* framerate forced with -r */
  182. int top_field_first;
  183. int resample_height;
  184. int resample_width;
  185. int resample_pix_fmt;
  186. int resample_sample_fmt;
  187. int resample_sample_rate;
  188. int resample_channels;
  189. uint64_t resample_channel_layout;
  190. struct sub2video {
  191. int64_t last_pts;
  192. AVFilterBufferRef *ref;
  193. int w, h;
  194. } sub2video;
  195. /* a pool of free buffers for decoded data */
  196. FrameBuffer *buffer_pool;
  197. int dr1;
  198. /* decoded data from this stream goes into all those filters
  199. * currently video and audio only */
  200. InputFilter **filters;
  201. int nb_filters;
  202. } InputStream;
  203. typedef struct InputFile {
  204. AVFormatContext *ctx;
  205. int eof_reached; /* true if eof reached */
  206. int eagain; /* true if last read attempt returned EAGAIN */
  207. int ist_index; /* index of first stream in input_streams */
  208. int64_t ts_offset;
  209. int nb_streams; /* number of stream that ffmpeg is aware of; may be different
  210. from ctx.nb_streams if new streams appear during av_read_frame() */
  211. int nb_streams_warn; /* number of streams that the user was warned of */
  212. int rate_emu;
  213. #if HAVE_PTHREADS
  214. pthread_t thread; /* thread reading from this file */
  215. int finished; /* the thread has exited */
  216. int joined; /* the thread has been joined */
  217. pthread_mutex_t fifo_lock; /* lock for access to fifo */
  218. pthread_cond_t fifo_cond; /* the main thread will signal on this cond after reading from fifo */
  219. AVFifoBuffer *fifo; /* demuxed packets are stored here; freed by the main thread */
  220. #endif
  221. } InputFile;
  222. typedef struct OutputStream {
  223. int file_index; /* file index */
  224. int index; /* stream index in the output file */
  225. int source_index; /* InputStream index */
  226. AVStream *st; /* stream in the output file */
  227. int encoding_needed; /* true if encoding needed for this stream */
  228. int frame_number;
  229. /* input pts and corresponding output pts
  230. for A/V sync */
  231. struct InputStream *sync_ist; /* input stream to sync against */
  232. int64_t sync_opts; /* output frame counter, could be changed to some true timestamp */ // FIXME look at frame_number
  233. /* pts of the first frame encoded for this stream, used for limiting
  234. * recording time */
  235. int64_t first_pts;
  236. AVBitStreamFilterContext *bitstream_filters;
  237. AVCodec *enc;
  238. int64_t max_frames;
  239. AVFrame *filtered_frame;
  240. /* video only */
  241. AVRational frame_rate;
  242. int force_fps;
  243. int top_field_first;
  244. float frame_aspect_ratio;
  245. float last_quality;
  246. /* forced key frames */
  247. int64_t *forced_kf_pts;
  248. int forced_kf_count;
  249. int forced_kf_index;
  250. char *forced_keyframes;
  251. /* audio only */
  252. int audio_channels_map[SWR_CH_MAX]; /* list of the channels id to pick from the source stream */
  253. int audio_channels_mapped; /* number of channels in audio_channels_map */
  254. FILE *logfile;
  255. OutputFilter *filter;
  256. char *avfilter;
  257. int64_t sws_flags;
  258. int64_t swr_dither_method;
  259. double swr_dither_scale;
  260. AVDictionary *opts;
  261. int finished; /* no more packets should be written for this stream */
  262. int unavailable; /* true if the steram is unavailable (possibly temporarily) */
  263. int stream_copy;
  264. const char *attachment_filename;
  265. int copy_initial_nonkeyframes;
  266. int keep_pix_fmt;
  267. } OutputStream;
  268. typedef struct OutputFile {
  269. AVFormatContext *ctx;
  270. AVDictionary *opts;
  271. int ost_index; /* index of the first stream in output_streams */
  272. int64_t recording_time; ///< desired length of the resulting file in microseconds == AV_TIME_BASE units
  273. int64_t start_time; ///< start time in microseconds == AV_TIME_BASE units
  274. uint64_t limit_filesize; /* filesize limit expressed in bytes */
  275. } OutputFile;
  276. extern InputStream **input_streams;
  277. extern int nb_input_streams;
  278. extern InputFile **input_files;
  279. extern int nb_input_files;
  280. extern OutputStream **output_streams;
  281. extern int nb_output_streams;
  282. extern OutputFile **output_files;
  283. extern int nb_output_files;
  284. extern FilterGraph **filtergraphs;
  285. extern int nb_filtergraphs;
  286. extern const char *pass_logfilename_prefix;
  287. extern char *vstats_filename;
  288. extern float audio_drift_threshold;
  289. extern float dts_delta_threshold;
  290. extern float dts_error_threshold;
  291. extern int audio_volume;
  292. extern int audio_sync_method;
  293. extern int video_sync_method;
  294. extern int do_benchmark;
  295. extern int do_benchmark_all;
  296. extern int do_deinterlace;
  297. extern int do_hex_dump;
  298. extern int do_pkt_dump;
  299. extern int copy_ts;
  300. extern int copy_tb;
  301. extern int debug_ts;
  302. extern int opt_shortest;
  303. extern int exit_on_error;
  304. extern int print_stats;
  305. extern int qp_hist;
  306. extern int same_quant;
  307. extern int stdin_interaction;
  308. extern int frame_bits_per_raw_sample;
  309. extern AVIOContext *progress_avio;
  310. extern const AVIOInterruptCB int_cb;
  311. extern const OptionDef options[];
  312. void term_init(void);
  313. void term_exit(void);
  314. void reset_options(OptionsContext *o, int is_input);
  315. void show_usage(void);
  316. void opt_output_file(void *optctx, const char *filename);
  317. void assert_avoptions(AVDictionary *m);
  318. int guess_input_channel_layout(InputStream *ist);
  319. enum PixelFormat choose_pixel_fmt(AVStream *st, AVCodec *codec, enum PixelFormat target);
  320. void choose_sample_fmt(AVStream *st, AVCodec *codec);
  321. int configure_filtergraph(FilterGraph *fg);
  322. int configure_output_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out);
  323. int ist_in_filtergraph(FilterGraph *fg, InputStream *ist);
  324. FilterGraph *init_simple_filtergraph(InputStream *ist, OutputStream *ost);
  325. #endif /* FFMPEG_H */