ffmpeg.h 17 KB

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