ffmpeg.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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 "libavutil/avutil.h"
  33. #include "libavutil/dict.h"
  34. #include "libavutil/eval.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_VSCFR 0xfe
  44. #define VSYNC_DROP 0xff
  45. #define MAX_STREAMS 1024 /* arbitrary sanity check value */
  46. /* select an input stream for an output stream */
  47. typedef struct StreamMap {
  48. int disabled; /* 1 is this mapping is disabled by a negative map */
  49. int file_index;
  50. int stream_index;
  51. int sync_file_index;
  52. int sync_stream_index;
  53. char *linklabel; /* name of an output link, for mapping lavfi outputs */
  54. } StreamMap;
  55. typedef struct {
  56. int file_idx, stream_idx, channel_idx; // input
  57. int ofile_idx, ostream_idx; // output
  58. } AudioChannelMap;
  59. typedef struct OptionsContext {
  60. OptionGroup *g;
  61. /* input/output options */
  62. int64_t start_time;
  63. const char *format;
  64. SpecifierOpt *codec_names;
  65. int nb_codec_names;
  66. SpecifierOpt *audio_channels;
  67. int nb_audio_channels;
  68. SpecifierOpt *audio_sample_rate;
  69. int nb_audio_sample_rate;
  70. SpecifierOpt *frame_rates;
  71. int nb_frame_rates;
  72. SpecifierOpt *frame_sizes;
  73. int nb_frame_sizes;
  74. SpecifierOpt *frame_pix_fmts;
  75. int nb_frame_pix_fmts;
  76. /* input options */
  77. int64_t input_ts_offset;
  78. int rate_emu;
  79. int accurate_seek;
  80. SpecifierOpt *ts_scale;
  81. int nb_ts_scale;
  82. SpecifierOpt *dump_attachment;
  83. int nb_dump_attachment;
  84. /* output options */
  85. StreamMap *stream_maps;
  86. int nb_stream_maps;
  87. AudioChannelMap *audio_channel_maps; /* one info entry per -map_channel */
  88. int nb_audio_channel_maps; /* number of (valid) -map_channel settings */
  89. int metadata_global_manual;
  90. int metadata_streams_manual;
  91. int metadata_chapters_manual;
  92. const char **attachments;
  93. int nb_attachments;
  94. int chapters_input_file;
  95. int64_t recording_time;
  96. int64_t stop_time;
  97. uint64_t limit_filesize;
  98. float mux_preload;
  99. float mux_max_delay;
  100. int shortest;
  101. int video_disable;
  102. int audio_disable;
  103. int subtitle_disable;
  104. int data_disable;
  105. /* indexed by output file stream index */
  106. int *streamid_map;
  107. int nb_streamid_map;
  108. SpecifierOpt *metadata;
  109. int nb_metadata;
  110. SpecifierOpt *max_frames;
  111. int nb_max_frames;
  112. SpecifierOpt *bitstream_filters;
  113. int nb_bitstream_filters;
  114. SpecifierOpt *codec_tags;
  115. int nb_codec_tags;
  116. SpecifierOpt *sample_fmts;
  117. int nb_sample_fmts;
  118. SpecifierOpt *qscale;
  119. int nb_qscale;
  120. SpecifierOpt *forced_key_frames;
  121. int nb_forced_key_frames;
  122. SpecifierOpt *force_fps;
  123. int nb_force_fps;
  124. SpecifierOpt *frame_aspect_ratios;
  125. int nb_frame_aspect_ratios;
  126. SpecifierOpt *rc_overrides;
  127. int nb_rc_overrides;
  128. SpecifierOpt *intra_matrices;
  129. int nb_intra_matrices;
  130. SpecifierOpt *inter_matrices;
  131. int nb_inter_matrices;
  132. SpecifierOpt *top_field_first;
  133. int nb_top_field_first;
  134. SpecifierOpt *metadata_map;
  135. int nb_metadata_map;
  136. SpecifierOpt *presets;
  137. int nb_presets;
  138. SpecifierOpt *copy_initial_nonkeyframes;
  139. int nb_copy_initial_nonkeyframes;
  140. SpecifierOpt *copy_prior_start;
  141. int nb_copy_prior_start;
  142. SpecifierOpt *filters;
  143. int nb_filters;
  144. SpecifierOpt *filter_scripts;
  145. int nb_filter_scripts;
  146. SpecifierOpt *reinit_filters;
  147. int nb_reinit_filters;
  148. SpecifierOpt *fix_sub_duration;
  149. int nb_fix_sub_duration;
  150. SpecifierOpt *canvas_sizes;
  151. int nb_canvas_sizes;
  152. SpecifierOpt *pass;
  153. int nb_pass;
  154. SpecifierOpt *passlogfiles;
  155. int nb_passlogfiles;
  156. SpecifierOpt *guess_layout_max;
  157. int nb_guess_layout_max;
  158. SpecifierOpt *apad;
  159. int nb_apad;
  160. } OptionsContext;
  161. typedef struct InputFilter {
  162. AVFilterContext *filter;
  163. struct InputStream *ist;
  164. struct FilterGraph *graph;
  165. uint8_t *name;
  166. } InputFilter;
  167. typedef struct OutputFilter {
  168. AVFilterContext *filter;
  169. struct OutputStream *ost;
  170. struct FilterGraph *graph;
  171. uint8_t *name;
  172. /* temporary storage until stream maps are processed */
  173. AVFilterInOut *out_tmp;
  174. } OutputFilter;
  175. typedef struct FilterGraph {
  176. int index;
  177. const char *graph_desc;
  178. AVFilterGraph *graph;
  179. int reconfiguration;
  180. InputFilter **inputs;
  181. int nb_inputs;
  182. OutputFilter **outputs;
  183. int nb_outputs;
  184. } FilterGraph;
  185. typedef struct InputStream {
  186. int file_index;
  187. AVStream *st;
  188. int discard; /* true if stream data should be discarded */
  189. int decoding_needed; /* true if the packets must be decoded in 'raw_fifo' */
  190. AVCodec *dec;
  191. AVFrame *decoded_frame;
  192. AVFrame *filter_frame; /* a ref of decoded_frame, to be sent to filters */
  193. int64_t start; /* time when read started */
  194. /* predicted dts of the next packet read for this stream or (when there are
  195. * several frames in a packet) of the next frame in current packet (in AV_TIME_BASE units) */
  196. int64_t next_dts;
  197. int64_t dts; ///< dts of the last packet read for this stream (in AV_TIME_BASE units)
  198. int64_t next_pts; ///< synthetic pts for the next decode frame (in AV_TIME_BASE units)
  199. int64_t pts; ///< current pts of the decoded frame (in AV_TIME_BASE units)
  200. int wrap_correction_done;
  201. int64_t filter_in_rescale_delta_last;
  202. double ts_scale;
  203. int is_start; /* is 1 at the start and after a discontinuity */
  204. int saw_first_ts;
  205. int showed_multi_packet_warning;
  206. AVDictionary *opts;
  207. AVRational framerate; /* framerate forced with -r */
  208. int top_field_first;
  209. int guess_layout_max;
  210. int resample_height;
  211. int resample_width;
  212. int resample_pix_fmt;
  213. int resample_sample_fmt;
  214. int resample_sample_rate;
  215. int resample_channels;
  216. uint64_t resample_channel_layout;
  217. int fix_sub_duration;
  218. struct { /* previous decoded subtitle and related variables */
  219. int got_output;
  220. int ret;
  221. AVSubtitle subtitle;
  222. } prev_sub;
  223. struct sub2video {
  224. int64_t last_pts;
  225. int64_t end_pts;
  226. AVFrame *frame;
  227. int w, h;
  228. } sub2video;
  229. int dr1;
  230. /* decoded data from this stream goes into all those filters
  231. * currently video and audio only */
  232. InputFilter **filters;
  233. int nb_filters;
  234. int reinit_filters;
  235. } InputStream;
  236. typedef struct InputFile {
  237. AVFormatContext *ctx;
  238. int eof_reached; /* true if eof reached */
  239. int eagain; /* true if last read attempt returned EAGAIN */
  240. int ist_index; /* index of first stream in input_streams */
  241. int64_t input_ts_offset;
  242. int64_t ts_offset;
  243. int64_t last_ts;
  244. int64_t start_time; /* user-specified start time in AV_TIME_BASE or AV_NOPTS_VALUE */
  245. int64_t recording_time;
  246. int nb_streams; /* number of stream that ffmpeg is aware of; may be different
  247. from ctx.nb_streams if new streams appear during av_read_frame() */
  248. int nb_streams_warn; /* number of streams that the user was warned of */
  249. int rate_emu;
  250. int accurate_seek;
  251. #if HAVE_PTHREADS
  252. pthread_t thread; /* thread reading from this file */
  253. int finished; /* the thread has exited */
  254. int joined; /* the thread has been joined */
  255. pthread_mutex_t fifo_lock; /* lock for access to fifo */
  256. pthread_cond_t fifo_cond; /* the main thread will signal on this cond after reading from fifo */
  257. AVFifoBuffer *fifo; /* demuxed packets are stored here; freed by the main thread */
  258. #endif
  259. } InputFile;
  260. enum forced_keyframes_const {
  261. FKF_N,
  262. FKF_N_FORCED,
  263. FKF_PREV_FORCED_N,
  264. FKF_PREV_FORCED_T,
  265. FKF_T,
  266. FKF_NB
  267. };
  268. extern const char *const forced_keyframes_const_names[];
  269. typedef struct OutputStream {
  270. int file_index; /* file index */
  271. int index; /* stream index in the output file */
  272. int source_index; /* InputStream index */
  273. AVStream *st; /* stream in the output file */
  274. int encoding_needed; /* true if encoding needed for this stream */
  275. int frame_number;
  276. /* input pts and corresponding output pts
  277. for A/V sync */
  278. struct InputStream *sync_ist; /* input stream to sync against */
  279. int64_t sync_opts; /* output frame counter, could be changed to some true timestamp */ // FIXME look at frame_number
  280. /* pts of the first frame encoded for this stream, used for limiting
  281. * recording time */
  282. int64_t first_pts;
  283. /* dts of the last packet sent to the muxer */
  284. int64_t last_mux_dts;
  285. AVBitStreamFilterContext *bitstream_filters;
  286. AVCodec *enc;
  287. int64_t max_frames;
  288. AVFrame *filtered_frame;
  289. /* video only */
  290. AVRational frame_rate;
  291. int force_fps;
  292. int top_field_first;
  293. AVRational frame_aspect_ratio;
  294. /* forced key frames */
  295. int64_t *forced_kf_pts;
  296. int forced_kf_count;
  297. int forced_kf_index;
  298. char *forced_keyframes;
  299. AVExpr *forced_keyframes_pexpr;
  300. double forced_keyframes_expr_const_values[FKF_NB];
  301. /* audio only */
  302. int audio_channels_map[SWR_CH_MAX]; /* list of the channels id to pick from the source stream */
  303. int audio_channels_mapped; /* number of channels in audio_channels_map */
  304. char *logfile_prefix;
  305. FILE *logfile;
  306. OutputFilter *filter;
  307. char *avfilter;
  308. int64_t sws_flags;
  309. AVDictionary *opts;
  310. AVDictionary *swr_opts;
  311. AVDictionary *resample_opts;
  312. char *apad;
  313. int finished; /* no more packets should be written for this stream */
  314. int unavailable; /* true if the steram is unavailable (possibly temporarily) */
  315. int stream_copy;
  316. const char *attachment_filename;
  317. int copy_initial_nonkeyframes;
  318. int copy_prior_start;
  319. int keep_pix_fmt;
  320. } OutputStream;
  321. typedef struct OutputFile {
  322. AVFormatContext *ctx;
  323. AVDictionary *opts;
  324. int ost_index; /* index of the first stream in output_streams */
  325. int64_t recording_time; ///< desired length of the resulting file in microseconds == AV_TIME_BASE units
  326. int64_t start_time; ///< start time in microseconds == AV_TIME_BASE units
  327. uint64_t limit_filesize; /* filesize limit expressed in bytes */
  328. int shortest;
  329. } OutputFile;
  330. extern InputStream **input_streams;
  331. extern int nb_input_streams;
  332. extern InputFile **input_files;
  333. extern int nb_input_files;
  334. extern OutputStream **output_streams;
  335. extern int nb_output_streams;
  336. extern OutputFile **output_files;
  337. extern int nb_output_files;
  338. extern FilterGraph **filtergraphs;
  339. extern int nb_filtergraphs;
  340. extern char *vstats_filename;
  341. extern float audio_drift_threshold;
  342. extern float dts_delta_threshold;
  343. extern float dts_error_threshold;
  344. extern int audio_volume;
  345. extern int audio_sync_method;
  346. extern int video_sync_method;
  347. extern int do_benchmark;
  348. extern int do_benchmark_all;
  349. extern int do_deinterlace;
  350. extern int do_hex_dump;
  351. extern int do_pkt_dump;
  352. extern int copy_ts;
  353. extern int copy_tb;
  354. extern int debug_ts;
  355. extern int exit_on_error;
  356. extern int print_stats;
  357. extern int qp_hist;
  358. extern int stdin_interaction;
  359. extern int frame_bits_per_raw_sample;
  360. extern AVIOContext *progress_avio;
  361. extern float max_error_rate;
  362. extern const AVIOInterruptCB int_cb;
  363. extern const OptionDef options[];
  364. void term_init(void);
  365. void term_exit(void);
  366. void reset_options(OptionsContext *o, int is_input);
  367. void show_usage(void);
  368. void opt_output_file(void *optctx, const char *filename);
  369. void assert_avoptions(AVDictionary *m);
  370. int guess_input_channel_layout(InputStream *ist);
  371. enum AVPixelFormat choose_pixel_fmt(AVStream *st, AVCodec *codec, enum AVPixelFormat target);
  372. void choose_sample_fmt(AVStream *st, AVCodec *codec);
  373. int configure_filtergraph(FilterGraph *fg);
  374. int configure_output_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out);
  375. int ist_in_filtergraph(FilterGraph *fg, InputStream *ist);
  376. FilterGraph *init_simple_filtergraph(InputStream *ist, OutputStream *ost);
  377. int ffmpeg_parse_options(int argc, char **argv);
  378. #endif /* FFMPEG_H */