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.

680 lines
20KB

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