ffmpeg.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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 shortest;
  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 *copy_prior_start;
  137. int nb_copy_prior_start;
  138. SpecifierOpt *filters;
  139. int nb_filters;
  140. SpecifierOpt *fix_sub_duration;
  141. int nb_fix_sub_duration;
  142. SpecifierOpt *pass;
  143. int nb_pass;
  144. SpecifierOpt *passlogfiles;
  145. int nb_passlogfiles;
  146. } OptionsContext;
  147. typedef struct InputFilter {
  148. AVFilterContext *filter;
  149. struct InputStream *ist;
  150. struct FilterGraph *graph;
  151. uint8_t *name;
  152. } InputFilter;
  153. typedef struct OutputFilter {
  154. AVFilterContext *filter;
  155. struct OutputStream *ost;
  156. struct FilterGraph *graph;
  157. uint8_t *name;
  158. /* temporary storage until stream maps are processed */
  159. AVFilterInOut *out_tmp;
  160. } OutputFilter;
  161. typedef struct FilterGraph {
  162. int index;
  163. const char *graph_desc;
  164. AVFilterGraph *graph;
  165. InputFilter **inputs;
  166. int nb_inputs;
  167. OutputFilter **outputs;
  168. int nb_outputs;
  169. } FilterGraph;
  170. typedef struct InputStream {
  171. int file_index;
  172. AVStream *st;
  173. int discard; /* true if stream data should be discarded */
  174. int decoding_needed; /* true if the packets must be decoded in 'raw_fifo' */
  175. AVCodec *dec;
  176. AVFrame *decoded_frame;
  177. int64_t start; /* time when read started */
  178. /* predicted dts of the next packet read for this stream or (when there are
  179. * several frames in a packet) of the next frame in current packet (in AV_TIME_BASE units) */
  180. int64_t next_dts;
  181. int64_t dts; ///< dts of the last packet read for this stream (in AV_TIME_BASE units)
  182. int64_t next_pts; ///< synthetic pts for the next decode frame (in AV_TIME_BASE units)
  183. int64_t pts; ///< current pts of the decoded frame (in AV_TIME_BASE units)
  184. int wrap_correction_done;
  185. double ts_scale;
  186. int is_start; /* is 1 at the start and after a discontinuity */
  187. int saw_first_ts;
  188. int showed_multi_packet_warning;
  189. AVDictionary *opts;
  190. AVRational framerate; /* framerate forced with -r */
  191. int top_field_first;
  192. int resample_height;
  193. int resample_width;
  194. int resample_pix_fmt;
  195. int resample_sample_fmt;
  196. int resample_sample_rate;
  197. int resample_channels;
  198. uint64_t resample_channel_layout;
  199. int fix_sub_duration;
  200. struct { /* previous decoded subtitle and related variables */
  201. int got_output;
  202. int ret;
  203. AVSubtitle subtitle;
  204. } prev_sub;
  205. struct sub2video {
  206. int64_t last_pts;
  207. AVFilterBufferRef *ref;
  208. int w, h;
  209. } sub2video;
  210. /* a pool of free buffers for decoded data */
  211. FrameBuffer *buffer_pool;
  212. int dr1;
  213. /* decoded data from this stream goes into all those filters
  214. * currently video and audio only */
  215. InputFilter **filters;
  216. int nb_filters;
  217. } InputStream;
  218. typedef struct InputFile {
  219. AVFormatContext *ctx;
  220. int eof_reached; /* true if eof reached */
  221. int eagain; /* true if last read attempt returned EAGAIN */
  222. int ist_index; /* index of first stream in input_streams */
  223. int64_t ts_offset;
  224. int nb_streams; /* number of stream that ffmpeg is aware of; may be different
  225. from ctx.nb_streams if new streams appear during av_read_frame() */
  226. int nb_streams_warn; /* number of streams that the user was warned of */
  227. int rate_emu;
  228. #if HAVE_PTHREADS
  229. pthread_t thread; /* thread reading from this file */
  230. int finished; /* the thread has exited */
  231. int joined; /* the thread has been joined */
  232. pthread_mutex_t fifo_lock; /* lock for access to fifo */
  233. pthread_cond_t fifo_cond; /* the main thread will signal on this cond after reading from fifo */
  234. AVFifoBuffer *fifo; /* demuxed packets are stored here; freed by the main thread */
  235. #endif
  236. } InputFile;
  237. typedef struct OutputStream {
  238. int file_index; /* file index */
  239. int index; /* stream index in the output file */
  240. int source_index; /* InputStream index */
  241. AVStream *st; /* stream in the output file */
  242. int encoding_needed; /* true if encoding needed for this stream */
  243. int frame_number;
  244. /* input pts and corresponding output pts
  245. for A/V sync */
  246. struct InputStream *sync_ist; /* input stream to sync against */
  247. int64_t sync_opts; /* output frame counter, could be changed to some true timestamp */ // FIXME look at frame_number
  248. /* pts of the first frame encoded for this stream, used for limiting
  249. * recording time */
  250. int64_t first_pts;
  251. AVBitStreamFilterContext *bitstream_filters;
  252. AVCodec *enc;
  253. int64_t max_frames;
  254. AVFrame *filtered_frame;
  255. /* video only */
  256. AVRational frame_rate;
  257. int force_fps;
  258. int top_field_first;
  259. float frame_aspect_ratio;
  260. float last_quality;
  261. /* forced key frames */
  262. int64_t *forced_kf_pts;
  263. int forced_kf_count;
  264. int forced_kf_index;
  265. char *forced_keyframes;
  266. /* audio only */
  267. int audio_channels_map[SWR_CH_MAX]; /* list of the channels id to pick from the source stream */
  268. int audio_channels_mapped; /* number of channels in audio_channels_map */
  269. char *logfile_prefix;
  270. FILE *logfile;
  271. OutputFilter *filter;
  272. char *avfilter;
  273. int64_t sws_flags;
  274. int64_t swr_dither_method;
  275. double swr_dither_scale;
  276. AVDictionary *opts;
  277. int finished; /* no more packets should be written for this stream */
  278. int unavailable; /* true if the steram is unavailable (possibly temporarily) */
  279. int stream_copy;
  280. const char *attachment_filename;
  281. int copy_initial_nonkeyframes;
  282. int copy_prior_start;
  283. int keep_pix_fmt;
  284. } OutputStream;
  285. typedef struct OutputFile {
  286. AVFormatContext *ctx;
  287. AVDictionary *opts;
  288. int ost_index; /* index of the first stream in output_streams */
  289. int64_t recording_time; ///< desired length of the resulting file in microseconds == AV_TIME_BASE units
  290. int64_t start_time; ///< start time in microseconds == AV_TIME_BASE units
  291. uint64_t limit_filesize; /* filesize limit expressed in bytes */
  292. int shortest;
  293. } OutputFile;
  294. extern InputStream **input_streams;
  295. extern int nb_input_streams;
  296. extern InputFile **input_files;
  297. extern int nb_input_files;
  298. extern OutputStream **output_streams;
  299. extern int nb_output_streams;
  300. extern OutputFile **output_files;
  301. extern int nb_output_files;
  302. extern FilterGraph **filtergraphs;
  303. extern int nb_filtergraphs;
  304. extern char *vstats_filename;
  305. extern float audio_drift_threshold;
  306. extern float dts_delta_threshold;
  307. extern float dts_error_threshold;
  308. extern int audio_volume;
  309. extern int audio_sync_method;
  310. extern int video_sync_method;
  311. extern int do_benchmark;
  312. extern int do_benchmark_all;
  313. extern int do_deinterlace;
  314. extern int do_hex_dump;
  315. extern int do_pkt_dump;
  316. extern int copy_ts;
  317. extern int copy_tb;
  318. extern int debug_ts;
  319. extern int exit_on_error;
  320. extern int print_stats;
  321. extern int qp_hist;
  322. extern int same_quant;
  323. extern int stdin_interaction;
  324. extern int frame_bits_per_raw_sample;
  325. extern AVIOContext *progress_avio;
  326. extern const AVIOInterruptCB int_cb;
  327. extern const OptionDef options[];
  328. void term_init(void);
  329. void term_exit(void);
  330. void reset_options(OptionsContext *o, int is_input);
  331. void show_usage(void);
  332. void opt_output_file(void *optctx, const char *filename);
  333. void assert_avoptions(AVDictionary *m);
  334. int guess_input_channel_layout(InputStream *ist);
  335. enum PixelFormat choose_pixel_fmt(AVStream *st, AVCodec *codec, enum PixelFormat target);
  336. void choose_sample_fmt(AVStream *st, AVCodec *codec);
  337. int configure_filtergraph(FilterGraph *fg);
  338. int configure_output_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out);
  339. int ist_in_filtergraph(FilterGraph *fg, InputStream *ist);
  340. FilterGraph *init_simple_filtergraph(InputStream *ist, OutputStream *ost);
  341. #endif /* FFMPEG_H */