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.

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