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.

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