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.

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