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.

440 lines
13KB

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