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.

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