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.

441 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. int64_t start; /* time when read started */
  188. /* predicted dts of the next packet read for this stream or (when there are
  189. * several frames in a packet) of the next frame in current packet (in AV_TIME_BASE units) */
  190. int64_t next_dts;
  191. int64_t dts; ///< dts of the last packet read for this stream (in AV_TIME_BASE units)
  192. int64_t next_pts; ///< synthetic pts for the next decode frame (in AV_TIME_BASE units)
  193. int64_t pts; ///< current pts of the decoded frame (in AV_TIME_BASE units)
  194. int wrap_correction_done;
  195. int64_t filter_in_rescale_delta_last;
  196. double ts_scale;
  197. int is_start; /* is 1 at the start and after a discontinuity */
  198. int saw_first_ts;
  199. int showed_multi_packet_warning;
  200. AVDictionary *opts;
  201. AVRational framerate; /* framerate forced with -r */
  202. int top_field_first;
  203. int guess_layout_max;
  204. int resample_height;
  205. int resample_width;
  206. int resample_pix_fmt;
  207. int resample_sample_fmt;
  208. int resample_sample_rate;
  209. int resample_channels;
  210. uint64_t resample_channel_layout;
  211. int fix_sub_duration;
  212. struct { /* previous decoded subtitle and related variables */
  213. int got_output;
  214. int ret;
  215. AVSubtitle subtitle;
  216. } prev_sub;
  217. struct sub2video {
  218. int64_t last_pts;
  219. int64_t end_pts;
  220. AVFrame *frame;
  221. int w, h;
  222. } sub2video;
  223. /* a pool of free buffers for decoded data */
  224. FrameBuffer *buffer_pool;
  225. int dr1;
  226. /* decoded data from this stream goes into all those filters
  227. * currently video and audio only */
  228. InputFilter **filters;
  229. int nb_filters;
  230. int reinit_filters;
  231. } InputStream;
  232. typedef struct InputFile {
  233. AVFormatContext *ctx;
  234. int eof_reached; /* true if eof reached */
  235. int eagain; /* true if last read attempt returned EAGAIN */
  236. int ist_index; /* index of first stream in input_streams */
  237. int64_t ts_offset;
  238. int nb_streams; /* number of stream that ffmpeg is aware of; may be different
  239. from ctx.nb_streams if new streams appear during av_read_frame() */
  240. int nb_streams_warn; /* number of streams that the user was warned of */
  241. int rate_emu;
  242. #if HAVE_PTHREADS
  243. pthread_t thread; /* thread reading from this file */
  244. int finished; /* the thread has exited */
  245. int joined; /* the thread has been joined */
  246. pthread_mutex_t fifo_lock; /* lock for access to fifo */
  247. pthread_cond_t fifo_cond; /* the main thread will signal on this cond after reading from fifo */
  248. AVFifoBuffer *fifo; /* demuxed packets are stored here; freed by the main thread */
  249. #endif
  250. } InputFile;
  251. enum forced_keyframes_const {
  252. FKF_N,
  253. FKF_N_FORCED,
  254. FKF_PREV_FORCED_N,
  255. FKF_PREV_FORCED_T,
  256. FKF_T,
  257. FKF_NB
  258. };
  259. extern const char *const forced_keyframes_const_names[];
  260. typedef struct OutputStream {
  261. int file_index; /* file index */
  262. int index; /* stream index in the output file */
  263. int source_index; /* InputStream index */
  264. AVStream *st; /* stream in the output file */
  265. int encoding_needed; /* true if encoding needed for this stream */
  266. int frame_number;
  267. /* input pts and corresponding output pts
  268. for A/V sync */
  269. struct InputStream *sync_ist; /* input stream to sync against */
  270. int64_t sync_opts; /* output frame counter, could be changed to some true timestamp */ // FIXME look at frame_number
  271. /* pts of the first frame encoded for this stream, used for limiting
  272. * recording time */
  273. int64_t first_pts;
  274. AVBitStreamFilterContext *bitstream_filters;
  275. AVCodec *enc;
  276. int64_t max_frames;
  277. AVFrame *filtered_frame;
  278. /* video only */
  279. AVRational frame_rate;
  280. int force_fps;
  281. int top_field_first;
  282. float frame_aspect_ratio;
  283. /* forced key frames */
  284. int64_t *forced_kf_pts;
  285. int forced_kf_count;
  286. int forced_kf_index;
  287. char *forced_keyframes;
  288. AVExpr *forced_keyframes_pexpr;
  289. double forced_keyframes_expr_const_values[FKF_NB];
  290. /* audio only */
  291. int audio_channels_map[SWR_CH_MAX]; /* list of the channels id to pick from the source stream */
  292. int audio_channels_mapped; /* number of channels in audio_channels_map */
  293. char *logfile_prefix;
  294. FILE *logfile;
  295. OutputFilter *filter;
  296. char *avfilter;
  297. int64_t sws_flags;
  298. AVDictionary *opts;
  299. AVDictionary *swr_opts;
  300. AVDictionary *resample_opts;
  301. int finished; /* no more packets should be written for this stream */
  302. int unavailable; /* true if the steram is unavailable (possibly temporarily) */
  303. int stream_copy;
  304. const char *attachment_filename;
  305. int copy_initial_nonkeyframes;
  306. int copy_prior_start;
  307. int keep_pix_fmt;
  308. } OutputStream;
  309. typedef struct OutputFile {
  310. AVFormatContext *ctx;
  311. AVDictionary *opts;
  312. int ost_index; /* index of the first stream in output_streams */
  313. int64_t recording_time; ///< desired length of the resulting file in microseconds == AV_TIME_BASE units
  314. int64_t start_time; ///< start time in microseconds == AV_TIME_BASE units
  315. uint64_t limit_filesize; /* filesize limit expressed in bytes */
  316. int shortest;
  317. } OutputFile;
  318. extern InputStream **input_streams;
  319. extern int nb_input_streams;
  320. extern InputFile **input_files;
  321. extern int nb_input_files;
  322. extern OutputStream **output_streams;
  323. extern int nb_output_streams;
  324. extern OutputFile **output_files;
  325. extern int nb_output_files;
  326. extern FilterGraph **filtergraphs;
  327. extern int nb_filtergraphs;
  328. extern char *vstats_filename;
  329. extern float audio_drift_threshold;
  330. extern float dts_delta_threshold;
  331. extern float dts_error_threshold;
  332. extern int audio_volume;
  333. extern int audio_sync_method;
  334. extern int video_sync_method;
  335. extern int do_benchmark;
  336. extern int do_benchmark_all;
  337. extern int do_deinterlace;
  338. extern int do_hex_dump;
  339. extern int do_pkt_dump;
  340. extern int copy_ts;
  341. extern int copy_tb;
  342. extern int debug_ts;
  343. extern int exit_on_error;
  344. extern int print_stats;
  345. extern int qp_hist;
  346. extern int stdin_interaction;
  347. extern int frame_bits_per_raw_sample;
  348. extern AVIOContext *progress_avio;
  349. extern const AVIOInterruptCB int_cb;
  350. extern const OptionDef options[];
  351. void term_init(void);
  352. void term_exit(void);
  353. void reset_options(OptionsContext *o, int is_input);
  354. void show_usage(void);
  355. void opt_output_file(void *optctx, const char *filename);
  356. void assert_avoptions(AVDictionary *m);
  357. int guess_input_channel_layout(InputStream *ist);
  358. enum AVPixelFormat choose_pixel_fmt(AVStream *st, AVCodec *codec, enum AVPixelFormat target);
  359. void choose_sample_fmt(AVStream *st, AVCodec *codec);
  360. int configure_filtergraph(FilterGraph *fg);
  361. int configure_output_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out);
  362. int ist_in_filtergraph(FilterGraph *fg, InputStream *ist);
  363. FilterGraph *init_simple_filtergraph(InputStream *ist, OutputStream *ost);
  364. int ffmpeg_parse_options(int argc, char **argv);
  365. #endif /* FFMPEG_H */