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.

522 lines
15KB

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