ffmpeg.h 13 KB

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