You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

562 lines
16KB

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