ffmpeg.h 15 KB

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