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.

572 lines
17KB

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