ffmpeg.h 15 KB

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