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.

489 lines
14KB

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