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.

404 lines
12KB

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