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.

538 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. 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. SpecifierOpt *disposition;
  183. int nb_disposition;
  184. } OptionsContext;
  185. typedef struct InputFilter {
  186. AVFilterContext *filter;
  187. struct InputStream *ist;
  188. struct FilterGraph *graph;
  189. uint8_t *name;
  190. } InputFilter;
  191. typedef struct OutputFilter {
  192. AVFilterContext *filter;
  193. struct OutputStream *ost;
  194. struct FilterGraph *graph;
  195. uint8_t *name;
  196. /* temporary storage until stream maps are processed */
  197. AVFilterInOut *out_tmp;
  198. } OutputFilter;
  199. typedef struct FilterGraph {
  200. int index;
  201. const char *graph_desc;
  202. AVFilterGraph *graph;
  203. int reconfiguration;
  204. InputFilter **inputs;
  205. int nb_inputs;
  206. OutputFilter **outputs;
  207. int nb_outputs;
  208. } FilterGraph;
  209. typedef struct InputStream {
  210. int file_index;
  211. AVStream *st;
  212. int discard; /* true if stream data should be discarded */
  213. int user_set_discard;
  214. int decoding_needed; /* non zero if the packets must be decoded in 'raw_fifo', see DECODING_FOR_* */
  215. #define DECODING_FOR_OST 1
  216. #define DECODING_FOR_FILTER 2
  217. AVCodecContext *dec_ctx;
  218. AVCodec *dec;
  219. AVFrame *decoded_frame;
  220. AVFrame *filter_frame; /* a ref of decoded_frame, to be sent to filters */
  221. int64_t start; /* time when read started */
  222. /* predicted dts of the next packet read for this stream or (when there are
  223. * several frames in a packet) of the next frame in current packet (in AV_TIME_BASE units) */
  224. int64_t next_dts;
  225. int64_t dts; ///< dts of the last packet read for this stream (in AV_TIME_BASE units)
  226. int64_t next_pts; ///< synthetic pts for the next decode frame (in AV_TIME_BASE units)
  227. int64_t pts; ///< current pts of the decoded frame (in AV_TIME_BASE units)
  228. int wrap_correction_done;
  229. int64_t filter_in_rescale_delta_last;
  230. double ts_scale;
  231. int saw_first_ts;
  232. int showed_multi_packet_warning;
  233. AVDictionary *decoder_opts;
  234. AVRational framerate; /* framerate forced with -r */
  235. int top_field_first;
  236. int guess_layout_max;
  237. int resample_height;
  238. int resample_width;
  239. int resample_pix_fmt;
  240. int resample_sample_fmt;
  241. int resample_sample_rate;
  242. int resample_channels;
  243. uint64_t resample_channel_layout;
  244. int fix_sub_duration;
  245. struct { /* previous decoded subtitle and related variables */
  246. int got_output;
  247. int ret;
  248. AVSubtitle subtitle;
  249. } prev_sub;
  250. struct sub2video {
  251. int64_t last_pts;
  252. int64_t end_pts;
  253. AVFrame *frame;
  254. int w, h;
  255. } sub2video;
  256. int dr1;
  257. /* decoded data from this stream goes into all those filters
  258. * currently video and audio only */
  259. InputFilter **filters;
  260. int nb_filters;
  261. int reinit_filters;
  262. /* hwaccel options */
  263. enum HWAccelID hwaccel_id;
  264. char *hwaccel_device;
  265. /* hwaccel context */
  266. enum HWAccelID active_hwaccel_id;
  267. void *hwaccel_ctx;
  268. void (*hwaccel_uninit)(AVCodecContext *s);
  269. int (*hwaccel_get_buffer)(AVCodecContext *s, AVFrame *frame, int flags);
  270. int (*hwaccel_retrieve_data)(AVCodecContext *s, AVFrame *frame);
  271. enum AVPixelFormat hwaccel_pix_fmt;
  272. enum AVPixelFormat hwaccel_retrieved_pix_fmt;
  273. /* stats */
  274. // combined size of all the packets read
  275. uint64_t data_size;
  276. /* number of packets successfully read for this stream */
  277. uint64_t nb_packets;
  278. // number of frames/samples retrieved from the decoder
  279. uint64_t frames_decoded;
  280. uint64_t samples_decoded;
  281. } InputStream;
  282. typedef struct InputFile {
  283. AVFormatContext *ctx;
  284. int eof_reached; /* true if eof reached */
  285. int eagain; /* true if last read attempt returned EAGAIN */
  286. int ist_index; /* index of first stream in input_streams */
  287. int64_t input_ts_offset;
  288. int64_t ts_offset;
  289. int64_t last_ts;
  290. int64_t start_time; /* user-specified start time in AV_TIME_BASE or AV_NOPTS_VALUE */
  291. int64_t recording_time;
  292. int nb_streams; /* number of stream that ffmpeg is aware of; may be different
  293. from ctx.nb_streams if new streams appear during av_read_frame() */
  294. int nb_streams_warn; /* number of streams that the user was warned of */
  295. int rate_emu;
  296. int accurate_seek;
  297. #if HAVE_PTHREADS
  298. AVThreadMessageQueue *in_thread_queue;
  299. pthread_t thread; /* thread reading from this file */
  300. int non_blocking; /* reading packets from the thread should not block */
  301. int joined; /* the thread has been joined */
  302. #endif
  303. } InputFile;
  304. enum forced_keyframes_const {
  305. FKF_N,
  306. FKF_N_FORCED,
  307. FKF_PREV_FORCED_N,
  308. FKF_PREV_FORCED_T,
  309. FKF_T,
  310. FKF_NB
  311. };
  312. extern const char *const forced_keyframes_const_names[];
  313. typedef enum {
  314. ENCODER_FINISHED = 1,
  315. MUXER_FINISHED = 2,
  316. } OSTFinished ;
  317. typedef struct OutputStream {
  318. int file_index; /* file index */
  319. int index; /* stream index in the output file */
  320. int source_index; /* InputStream index */
  321. AVStream *st; /* stream in the output file */
  322. int encoding_needed; /* true if encoding needed for this stream */
  323. int frame_number;
  324. /* input pts and corresponding output pts
  325. for A/V sync */
  326. struct InputStream *sync_ist; /* input stream to sync against */
  327. int64_t sync_opts; /* output frame counter, could be changed to some true timestamp */ // FIXME look at frame_number
  328. /* pts of the first frame encoded for this stream, used for limiting
  329. * recording time */
  330. int64_t first_pts;
  331. /* dts of the last packet sent to the muxer */
  332. int64_t last_mux_dts;
  333. AVBitStreamFilterContext *bitstream_filters;
  334. AVCodecContext *enc_ctx;
  335. AVCodec *enc;
  336. int64_t max_frames;
  337. AVFrame *filtered_frame;
  338. AVFrame *last_frame;
  339. int last_droped;
  340. /* video only */
  341. AVRational frame_rate;
  342. int force_fps;
  343. int top_field_first;
  344. AVRational frame_aspect_ratio;
  345. /* forced key frames */
  346. int64_t *forced_kf_pts;
  347. int forced_kf_count;
  348. int forced_kf_index;
  349. char *forced_keyframes;
  350. AVExpr *forced_keyframes_pexpr;
  351. double forced_keyframes_expr_const_values[FKF_NB];
  352. /* audio only */
  353. int *audio_channels_map; /* list of the channels id to pick from the source stream */
  354. int audio_channels_mapped; /* number of channels in audio_channels_map */
  355. char *logfile_prefix;
  356. FILE *logfile;
  357. OutputFilter *filter;
  358. char *avfilter;
  359. char *filters; ///< filtergraph associated to the -filter option
  360. char *filters_script; ///< filtergraph script associated to the -filter_script option
  361. int64_t sws_flags;
  362. AVDictionary *encoder_opts;
  363. AVDictionary *swr_opts;
  364. AVDictionary *resample_opts;
  365. AVDictionary *bsf_args;
  366. char *apad;
  367. OSTFinished finished; /* no more packets should be written for this stream */
  368. int unavailable; /* true if the steram is unavailable (possibly temporarily) */
  369. int stream_copy;
  370. const char *attachment_filename;
  371. int copy_initial_nonkeyframes;
  372. int copy_prior_start;
  373. char *disposition;
  374. int keep_pix_fmt;
  375. AVCodecParserContext *parser;
  376. /* stats */
  377. // combined size of all the packets written
  378. uint64_t data_size;
  379. // number of packets send to the muxer
  380. uint64_t packets_written;
  381. // number of frames/samples sent to the encoder
  382. uint64_t frames_encoded;
  383. uint64_t samples_encoded;
  384. } OutputStream;
  385. typedef struct OutputFile {
  386. AVFormatContext *ctx;
  387. AVDictionary *opts;
  388. int ost_index; /* index of the first stream in output_streams */
  389. int64_t recording_time; ///< desired length of the resulting file in microseconds == AV_TIME_BASE units
  390. int64_t start_time; ///< start time in microseconds == AV_TIME_BASE units
  391. uint64_t limit_filesize; /* filesize limit expressed in bytes */
  392. int shortest;
  393. } OutputFile;
  394. extern InputStream **input_streams;
  395. extern int nb_input_streams;
  396. extern InputFile **input_files;
  397. extern int nb_input_files;
  398. extern OutputStream **output_streams;
  399. extern int nb_output_streams;
  400. extern OutputFile **output_files;
  401. extern int nb_output_files;
  402. extern FilterGraph **filtergraphs;
  403. extern int nb_filtergraphs;
  404. extern char *vstats_filename;
  405. extern char *sdp_filename;
  406. extern float audio_drift_threshold;
  407. extern float dts_delta_threshold;
  408. extern float dts_error_threshold;
  409. extern int audio_volume;
  410. extern int audio_sync_method;
  411. extern int video_sync_method;
  412. extern float frame_drop_threshold;
  413. extern int do_benchmark;
  414. extern int do_benchmark_all;
  415. extern int do_deinterlace;
  416. extern int do_hex_dump;
  417. extern int do_pkt_dump;
  418. extern int copy_ts;
  419. extern int start_at_zero;
  420. extern int copy_tb;
  421. extern int debug_ts;
  422. extern int exit_on_error;
  423. extern int print_stats;
  424. extern int qp_hist;
  425. extern int stdin_interaction;
  426. extern int frame_bits_per_raw_sample;
  427. extern AVIOContext *progress_avio;
  428. extern float max_error_rate;
  429. extern int vdpau_api_ver;
  430. extern const AVIOInterruptCB int_cb;
  431. extern const OptionDef options[];
  432. extern const HWAccel hwaccels[];
  433. void term_init(void);
  434. void term_exit(void);
  435. void reset_options(OptionsContext *o, int is_input);
  436. void show_usage(void);
  437. void opt_output_file(void *optctx, const char *filename);
  438. void remove_avoptions(AVDictionary **a, AVDictionary *b);
  439. void assert_avoptions(AVDictionary *m);
  440. int guess_input_channel_layout(InputStream *ist);
  441. enum AVPixelFormat choose_pixel_fmt(AVStream *st, AVCodecContext *avctx, AVCodec *codec, enum AVPixelFormat target);
  442. void choose_sample_fmt(AVStream *st, AVCodec *codec);
  443. int configure_filtergraph(FilterGraph *fg);
  444. int configure_output_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out);
  445. int ist_in_filtergraph(FilterGraph *fg, InputStream *ist);
  446. FilterGraph *init_simple_filtergraph(InputStream *ist, OutputStream *ost);
  447. int ffmpeg_parse_options(int argc, char **argv);
  448. int vdpau_init(AVCodecContext *s);
  449. int dxva2_init(AVCodecContext *s);
  450. int vda_init(AVCodecContext *s);
  451. #endif /* FFMPEG_H */