ffmpeg.h 18 KB

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