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.

653 lines
19KB

  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 "libavutil/avutil.h"
  33. #include "libavutil/dict.h"
  34. #include "libavutil/eval.h"
  35. #include "libavutil/fifo.h"
  36. #include "libavutil/pixfmt.h"
  37. #include "libavutil/rational.h"
  38. #include "libavutil/threadmessage.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_VSCFR 0xfe
  45. #define VSYNC_DROP 0xff
  46. #define MAX_STREAMS 1024 /* arbitrary sanity check value */
  47. enum HWAccelID {
  48. HWACCEL_NONE = 0,
  49. HWACCEL_AUTO,
  50. HWACCEL_VDPAU,
  51. HWACCEL_DXVA2,
  52. HWACCEL_VDA,
  53. HWACCEL_VIDEOTOOLBOX,
  54. HWACCEL_QSV,
  55. HWACCEL_VAAPI,
  56. HWACCEL_CUVID,
  57. };
  58. typedef struct HWAccel {
  59. const char *name;
  60. int (*init)(AVCodecContext *s);
  61. enum HWAccelID id;
  62. enum AVPixelFormat pix_fmt;
  63. } HWAccel;
  64. /* select an input stream for an output stream */
  65. typedef struct StreamMap {
  66. int disabled; /* 1 is this mapping is disabled by a negative map */
  67. int file_index;
  68. int stream_index;
  69. int sync_file_index;
  70. int sync_stream_index;
  71. char *linklabel; /* name of an output link, for mapping lavfi outputs */
  72. } StreamMap;
  73. typedef struct {
  74. int file_idx, stream_idx, channel_idx; // input
  75. int ofile_idx, ostream_idx; // output
  76. } AudioChannelMap;
  77. typedef struct OptionsContext {
  78. OptionGroup *g;
  79. /* input/output options */
  80. int64_t start_time;
  81. int64_t start_time_eof;
  82. int seek_timestamp;
  83. const char *format;
  84. SpecifierOpt *codec_names;
  85. int nb_codec_names;
  86. SpecifierOpt *audio_channels;
  87. int nb_audio_channels;
  88. SpecifierOpt *audio_sample_rate;
  89. int nb_audio_sample_rate;
  90. SpecifierOpt *frame_rates;
  91. int nb_frame_rates;
  92. SpecifierOpt *frame_sizes;
  93. int nb_frame_sizes;
  94. SpecifierOpt *frame_pix_fmts;
  95. int nb_frame_pix_fmts;
  96. /* input options */
  97. int64_t input_ts_offset;
  98. int loop;
  99. int rate_emu;
  100. int accurate_seek;
  101. int thread_queue_size;
  102. SpecifierOpt *ts_scale;
  103. int nb_ts_scale;
  104. SpecifierOpt *dump_attachment;
  105. int nb_dump_attachment;
  106. SpecifierOpt *hwaccels;
  107. int nb_hwaccels;
  108. SpecifierOpt *hwaccel_devices;
  109. int nb_hwaccel_devices;
  110. SpecifierOpt *hwaccel_output_formats;
  111. int nb_hwaccel_output_formats;
  112. SpecifierOpt *autorotate;
  113. int nb_autorotate;
  114. /* output options */
  115. StreamMap *stream_maps;
  116. int nb_stream_maps;
  117. AudioChannelMap *audio_channel_maps; /* one info entry per -map_channel */
  118. int nb_audio_channel_maps; /* number of (valid) -map_channel settings */
  119. int metadata_global_manual;
  120. int metadata_streams_manual;
  121. int metadata_chapters_manual;
  122. const char **attachments;
  123. int nb_attachments;
  124. int chapters_input_file;
  125. int64_t recording_time;
  126. int64_t stop_time;
  127. uint64_t limit_filesize;
  128. float mux_preload;
  129. float mux_max_delay;
  130. int shortest;
  131. int video_disable;
  132. int audio_disable;
  133. int subtitle_disable;
  134. int data_disable;
  135. /* indexed by output file stream index */
  136. int *streamid_map;
  137. int nb_streamid_map;
  138. SpecifierOpt *metadata;
  139. int nb_metadata;
  140. SpecifierOpt *max_frames;
  141. int nb_max_frames;
  142. SpecifierOpt *bitstream_filters;
  143. int nb_bitstream_filters;
  144. SpecifierOpt *codec_tags;
  145. int nb_codec_tags;
  146. SpecifierOpt *sample_fmts;
  147. int nb_sample_fmts;
  148. SpecifierOpt *qscale;
  149. int nb_qscale;
  150. SpecifierOpt *forced_key_frames;
  151. int nb_forced_key_frames;
  152. SpecifierOpt *force_fps;
  153. int nb_force_fps;
  154. SpecifierOpt *frame_aspect_ratios;
  155. int nb_frame_aspect_ratios;
  156. SpecifierOpt *rc_overrides;
  157. int nb_rc_overrides;
  158. SpecifierOpt *intra_matrices;
  159. int nb_intra_matrices;
  160. SpecifierOpt *inter_matrices;
  161. int nb_inter_matrices;
  162. SpecifierOpt *chroma_intra_matrices;
  163. int nb_chroma_intra_matrices;
  164. SpecifierOpt *top_field_first;
  165. int nb_top_field_first;
  166. SpecifierOpt *metadata_map;
  167. int nb_metadata_map;
  168. SpecifierOpt *presets;
  169. int nb_presets;
  170. SpecifierOpt *copy_initial_nonkeyframes;
  171. int nb_copy_initial_nonkeyframes;
  172. SpecifierOpt *copy_prior_start;
  173. int nb_copy_prior_start;
  174. SpecifierOpt *filters;
  175. int nb_filters;
  176. SpecifierOpt *filter_scripts;
  177. int nb_filter_scripts;
  178. SpecifierOpt *reinit_filters;
  179. int nb_reinit_filters;
  180. SpecifierOpt *fix_sub_duration;
  181. int nb_fix_sub_duration;
  182. SpecifierOpt *canvas_sizes;
  183. int nb_canvas_sizes;
  184. SpecifierOpt *pass;
  185. int nb_pass;
  186. SpecifierOpt *passlogfiles;
  187. int nb_passlogfiles;
  188. SpecifierOpt *max_muxing_queue_size;
  189. int nb_max_muxing_queue_size;
  190. SpecifierOpt *guess_layout_max;
  191. int nb_guess_layout_max;
  192. SpecifierOpt *apad;
  193. int nb_apad;
  194. SpecifierOpt *discard;
  195. int nb_discard;
  196. SpecifierOpt *disposition;
  197. int nb_disposition;
  198. SpecifierOpt *program;
  199. int nb_program;
  200. SpecifierOpt *time_bases;
  201. int nb_time_bases;
  202. } OptionsContext;
  203. typedef struct InputFilter {
  204. AVFilterContext *filter;
  205. struct InputStream *ist;
  206. struct FilterGraph *graph;
  207. uint8_t *name;
  208. // parameters configured for this input
  209. int format;
  210. int width, height;
  211. AVRational sample_aspect_ratio;
  212. int sample_rate;
  213. int channels;
  214. uint64_t channel_layout;
  215. AVBufferRef *hw_frames_ctx;
  216. } InputFilter;
  217. typedef struct OutputFilter {
  218. AVFilterContext *filter;
  219. struct OutputStream *ost;
  220. struct FilterGraph *graph;
  221. uint8_t *name;
  222. /* temporary storage until stream maps are processed */
  223. AVFilterInOut *out_tmp;
  224. enum AVMediaType type;
  225. /* desired output stream properties */
  226. int width, height;
  227. AVRational frame_rate;
  228. int format;
  229. int sample_rate;
  230. uint64_t channel_layout;
  231. // those are only set if no format is specified and the encoder gives us multiple options
  232. int *formats;
  233. uint64_t *channel_layouts;
  234. int *sample_rates;
  235. } OutputFilter;
  236. typedef struct FilterGraph {
  237. int index;
  238. const char *graph_desc;
  239. AVFilterGraph *graph;
  240. int reconfiguration;
  241. InputFilter **inputs;
  242. int nb_inputs;
  243. OutputFilter **outputs;
  244. int nb_outputs;
  245. } FilterGraph;
  246. typedef struct InputStream {
  247. int file_index;
  248. AVStream *st;
  249. int discard; /* true if stream data should be discarded */
  250. int user_set_discard;
  251. int decoding_needed; /* non zero if the packets must be decoded in 'raw_fifo', see DECODING_FOR_* */
  252. #define DECODING_FOR_OST 1
  253. #define DECODING_FOR_FILTER 2
  254. AVCodecContext *dec_ctx;
  255. AVCodec *dec;
  256. AVFrame *decoded_frame;
  257. AVFrame *filter_frame; /* a ref of decoded_frame, to be sent to filters */
  258. int64_t start; /* time when read started */
  259. /* predicted dts of the next packet read for this stream or (when there are
  260. * several frames in a packet) of the next frame in current packet (in AV_TIME_BASE units) */
  261. int64_t next_dts;
  262. int64_t dts; ///< dts of the last packet read for this stream (in AV_TIME_BASE units)
  263. int64_t next_pts; ///< synthetic pts for the next decode frame (in AV_TIME_BASE units)
  264. int64_t pts; ///< current pts of the decoded frame (in AV_TIME_BASE units)
  265. int wrap_correction_done;
  266. int64_t filter_in_rescale_delta_last;
  267. int64_t min_pts; /* pts with the smallest value in a current stream */
  268. int64_t max_pts; /* pts with the higher value in a current stream */
  269. int64_t nb_samples; /* number of samples in the last decoded audio frame before looping */
  270. double ts_scale;
  271. int saw_first_ts;
  272. AVDictionary *decoder_opts;
  273. AVRational framerate; /* framerate forced with -r */
  274. int top_field_first;
  275. int guess_layout_max;
  276. int autorotate;
  277. int resample_height;
  278. int resample_width;
  279. int resample_pix_fmt;
  280. int resample_sample_fmt;
  281. int resample_sample_rate;
  282. int resample_channels;
  283. uint64_t resample_channel_layout;
  284. int fix_sub_duration;
  285. struct { /* previous decoded subtitle and related variables */
  286. int got_output;
  287. int ret;
  288. AVSubtitle subtitle;
  289. } prev_sub;
  290. struct sub2video {
  291. int64_t last_pts;
  292. int64_t end_pts;
  293. AVFrame *frame;
  294. int w, h;
  295. } sub2video;
  296. int dr1;
  297. /* decoded data from this stream goes into all those filters
  298. * currently video and audio only */
  299. InputFilter **filters;
  300. int nb_filters;
  301. int reinit_filters;
  302. /* hwaccel options */
  303. enum HWAccelID hwaccel_id;
  304. char *hwaccel_device;
  305. enum AVPixelFormat hwaccel_output_format;
  306. /* hwaccel context */
  307. enum HWAccelID active_hwaccel_id;
  308. void *hwaccel_ctx;
  309. void (*hwaccel_uninit)(AVCodecContext *s);
  310. int (*hwaccel_get_buffer)(AVCodecContext *s, AVFrame *frame, int flags);
  311. int (*hwaccel_retrieve_data)(AVCodecContext *s, AVFrame *frame);
  312. enum AVPixelFormat hwaccel_pix_fmt;
  313. enum AVPixelFormat hwaccel_retrieved_pix_fmt;
  314. AVBufferRef *hw_frames_ctx;
  315. /* stats */
  316. // combined size of all the packets read
  317. uint64_t data_size;
  318. /* number of packets successfully read for this stream */
  319. uint64_t nb_packets;
  320. // number of frames/samples retrieved from the decoder
  321. uint64_t frames_decoded;
  322. uint64_t samples_decoded;
  323. int64_t *dts_buffer;
  324. int nb_dts_buffer;
  325. } InputStream;
  326. typedef struct InputFile {
  327. AVFormatContext *ctx;
  328. int eof_reached; /* true if eof reached */
  329. int eagain; /* true if last read attempt returned EAGAIN */
  330. int ist_index; /* index of first stream in input_streams */
  331. int loop; /* set number of times input stream should be looped */
  332. int64_t duration; /* actual duration of the longest stream in a file
  333. at the moment when looping happens */
  334. AVRational time_base; /* time base of the duration */
  335. int64_t input_ts_offset;
  336. int64_t ts_offset;
  337. int64_t last_ts;
  338. int64_t start_time; /* user-specified start time in AV_TIME_BASE or AV_NOPTS_VALUE */
  339. int seek_timestamp;
  340. int64_t recording_time;
  341. int nb_streams; /* number of stream that ffmpeg is aware of; may be different
  342. from ctx.nb_streams if new streams appear during av_read_frame() */
  343. int nb_streams_warn; /* number of streams that the user was warned of */
  344. int rate_emu;
  345. int accurate_seek;
  346. #if HAVE_PTHREADS
  347. AVThreadMessageQueue *in_thread_queue;
  348. pthread_t thread; /* thread reading from this file */
  349. int non_blocking; /* reading packets from the thread should not block */
  350. int joined; /* the thread has been joined */
  351. int thread_queue_size; /* maximum number of queued packets */
  352. #endif
  353. } InputFile;
  354. enum forced_keyframes_const {
  355. FKF_N,
  356. FKF_N_FORCED,
  357. FKF_PREV_FORCED_N,
  358. FKF_PREV_FORCED_T,
  359. FKF_T,
  360. FKF_NB
  361. };
  362. #define ABORT_ON_FLAG_EMPTY_OUTPUT (1 << 0)
  363. extern const char *const forced_keyframes_const_names[];
  364. typedef enum {
  365. ENCODER_FINISHED = 1,
  366. MUXER_FINISHED = 2,
  367. } OSTFinished ;
  368. typedef struct OutputStream {
  369. int file_index; /* file index */
  370. int index; /* stream index in the output file */
  371. int source_index; /* InputStream index */
  372. AVStream *st; /* stream in the output file */
  373. int encoding_needed; /* true if encoding needed for this stream */
  374. int frame_number;
  375. /* input pts and corresponding output pts
  376. for A/V sync */
  377. struct InputStream *sync_ist; /* input stream to sync against */
  378. int64_t sync_opts; /* output frame counter, could be changed to some true timestamp */ // FIXME look at frame_number
  379. /* pts of the first frame encoded for this stream, used for limiting
  380. * recording time */
  381. int64_t first_pts;
  382. /* dts of the last packet sent to the muxer */
  383. int64_t last_mux_dts;
  384. int nb_bitstream_filters;
  385. uint8_t *bsf_extradata_updated;
  386. AVBSFContext **bsf_ctx;
  387. AVCodecContext *enc_ctx;
  388. AVCodecParameters *ref_par; /* associated input codec parameters with encoders options applied */
  389. AVCodec *enc;
  390. int64_t max_frames;
  391. AVFrame *filtered_frame;
  392. AVFrame *last_frame;
  393. int last_dropped;
  394. int last_nb0_frames[3];
  395. void *hwaccel_ctx;
  396. /* video only */
  397. AVRational frame_rate;
  398. int is_cfr;
  399. int force_fps;
  400. int top_field_first;
  401. int rotate_overridden;
  402. AVRational frame_aspect_ratio;
  403. /* forced key frames */
  404. int64_t *forced_kf_pts;
  405. int forced_kf_count;
  406. int forced_kf_index;
  407. char *forced_keyframes;
  408. AVExpr *forced_keyframes_pexpr;
  409. double forced_keyframes_expr_const_values[FKF_NB];
  410. /* audio only */
  411. int *audio_channels_map; /* list of the channels id to pick from the source stream */
  412. int audio_channels_mapped; /* number of channels in audio_channels_map */
  413. char *logfile_prefix;
  414. FILE *logfile;
  415. OutputFilter *filter;
  416. char *avfilter;
  417. char *filters; ///< filtergraph associated to the -filter option
  418. char *filters_script; ///< filtergraph script associated to the -filter_script option
  419. AVDictionary *encoder_opts;
  420. AVDictionary *sws_dict;
  421. AVDictionary *swr_opts;
  422. AVDictionary *resample_opts;
  423. char *apad;
  424. OSTFinished finished; /* no more packets should be written for this stream */
  425. int unavailable; /* true if the steram is unavailable (possibly temporarily) */
  426. int stream_copy;
  427. // init_output_stream() has been called for this stream
  428. // The encoder and the bitstream filters have been initialized and the stream
  429. // parameters are set in the AVStream.
  430. int initialized;
  431. const char *attachment_filename;
  432. int copy_initial_nonkeyframes;
  433. int copy_prior_start;
  434. char *disposition;
  435. int keep_pix_fmt;
  436. AVCodecParserContext *parser;
  437. AVCodecContext *parser_avctx;
  438. /* stats */
  439. // combined size of all the packets written
  440. uint64_t data_size;
  441. // number of packets send to the muxer
  442. uint64_t packets_written;
  443. // number of frames/samples sent to the encoder
  444. uint64_t frames_encoded;
  445. uint64_t samples_encoded;
  446. /* packet quality factor */
  447. int quality;
  448. int max_muxing_queue_size;
  449. /* the packets are buffered here until the muxer is ready to be initialized */
  450. AVFifoBuffer *muxing_queue;
  451. /* packet picture type */
  452. int pict_type;
  453. /* frame encode sum of squared error values */
  454. int64_t error[4];
  455. } OutputStream;
  456. typedef struct OutputFile {
  457. AVFormatContext *ctx;
  458. AVDictionary *opts;
  459. int ost_index; /* index of the first stream in output_streams */
  460. int64_t recording_time; ///< desired length of the resulting file in microseconds == AV_TIME_BASE units
  461. int64_t start_time; ///< start time in microseconds == AV_TIME_BASE units
  462. uint64_t limit_filesize; /* filesize limit expressed in bytes */
  463. int shortest;
  464. int header_written;
  465. } OutputFile;
  466. extern InputStream **input_streams;
  467. extern int nb_input_streams;
  468. extern InputFile **input_files;
  469. extern int nb_input_files;
  470. extern OutputStream **output_streams;
  471. extern int nb_output_streams;
  472. extern OutputFile **output_files;
  473. extern int nb_output_files;
  474. extern FilterGraph **filtergraphs;
  475. extern int nb_filtergraphs;
  476. extern char *vstats_filename;
  477. extern char *sdp_filename;
  478. extern float audio_drift_threshold;
  479. extern float dts_delta_threshold;
  480. extern float dts_error_threshold;
  481. extern int audio_volume;
  482. extern int audio_sync_method;
  483. extern int video_sync_method;
  484. extern float frame_drop_threshold;
  485. extern int do_benchmark;
  486. extern int do_benchmark_all;
  487. extern int do_deinterlace;
  488. extern int do_hex_dump;
  489. extern int do_pkt_dump;
  490. extern int copy_ts;
  491. extern int start_at_zero;
  492. extern int copy_tb;
  493. extern int debug_ts;
  494. extern int exit_on_error;
  495. extern int abort_on_flags;
  496. extern int print_stats;
  497. extern int qp_hist;
  498. extern int stdin_interaction;
  499. extern int frame_bits_per_raw_sample;
  500. extern AVIOContext *progress_avio;
  501. extern float max_error_rate;
  502. extern char *videotoolbox_pixfmt;
  503. extern int filter_nbthreads;
  504. extern int filter_complex_nbthreads;
  505. extern int vstats_version;
  506. extern const AVIOInterruptCB int_cb;
  507. extern const OptionDef options[];
  508. extern const HWAccel hwaccels[];
  509. extern int hwaccel_lax_profile_check;
  510. extern AVBufferRef *hw_device_ctx;
  511. #if CONFIG_QSV
  512. extern char *qsv_device;
  513. #endif
  514. void term_init(void);
  515. void term_exit(void);
  516. void reset_options(OptionsContext *o, int is_input);
  517. void show_usage(void);
  518. void opt_output_file(void *optctx, const char *filename);
  519. void remove_avoptions(AVDictionary **a, AVDictionary *b);
  520. void assert_avoptions(AVDictionary *m);
  521. int guess_input_channel_layout(InputStream *ist);
  522. enum AVPixelFormat choose_pixel_fmt(AVStream *st, AVCodecContext *avctx, AVCodec *codec, enum AVPixelFormat target);
  523. void choose_sample_fmt(AVStream *st, AVCodec *codec);
  524. int configure_filtergraph(FilterGraph *fg);
  525. int configure_output_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out);
  526. int ist_in_filtergraph(FilterGraph *fg, InputStream *ist);
  527. int filtergraph_is_simple(FilterGraph *fg);
  528. int init_simple_filtergraph(InputStream *ist, OutputStream *ost);
  529. int init_complex_filtergraph(FilterGraph *fg);
  530. int ifilter_parameters_from_frame(InputFilter *ifilter, const AVFrame *frame);
  531. int ifilter_parameters_from_decoder(InputFilter *ifilter, const AVCodecContext *avctx);
  532. int ffmpeg_parse_options(int argc, char **argv);
  533. int vdpau_init(AVCodecContext *s);
  534. int dxva2_init(AVCodecContext *s);
  535. int vda_init(AVCodecContext *s);
  536. int videotoolbox_init(AVCodecContext *s);
  537. int qsv_init(AVCodecContext *s);
  538. int qsv_transcode_init(OutputStream *ost);
  539. int vaapi_decode_init(AVCodecContext *avctx);
  540. int vaapi_device_init(const char *device);
  541. int cuvid_init(AVCodecContext *s);
  542. int cuvid_transcode_init(OutputStream *ost);
  543. #endif /* FFMPEG_H */