ffmpeg.h 16 KB

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