ffmpeg.h 16 KB

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