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.

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