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.

679 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_VIDEOTOOLBOX,
  54. HWACCEL_QSV,
  55. HWACCEL_VAAPI,
  56. HWACCEL_CUVID,
  57. HWACCEL_D3D11VA,
  58. HWACCEL_NVDEC,
  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 bitexact;
  140. int video_disable;
  141. int audio_disable;
  142. int subtitle_disable;
  143. int data_disable;
  144. /* indexed by output file stream index */
  145. int *streamid_map;
  146. int nb_streamid_map;
  147. SpecifierOpt *metadata;
  148. int nb_metadata;
  149. SpecifierOpt *max_frames;
  150. int nb_max_frames;
  151. SpecifierOpt *bitstream_filters;
  152. int nb_bitstream_filters;
  153. SpecifierOpt *codec_tags;
  154. int nb_codec_tags;
  155. SpecifierOpt *sample_fmts;
  156. int nb_sample_fmts;
  157. SpecifierOpt *qscale;
  158. int nb_qscale;
  159. SpecifierOpt *forced_key_frames;
  160. int nb_forced_key_frames;
  161. SpecifierOpt *force_fps;
  162. int nb_force_fps;
  163. SpecifierOpt *frame_aspect_ratios;
  164. int nb_frame_aspect_ratios;
  165. SpecifierOpt *rc_overrides;
  166. int nb_rc_overrides;
  167. SpecifierOpt *intra_matrices;
  168. int nb_intra_matrices;
  169. SpecifierOpt *inter_matrices;
  170. int nb_inter_matrices;
  171. SpecifierOpt *chroma_intra_matrices;
  172. int nb_chroma_intra_matrices;
  173. SpecifierOpt *top_field_first;
  174. int nb_top_field_first;
  175. SpecifierOpt *metadata_map;
  176. int nb_metadata_map;
  177. SpecifierOpt *presets;
  178. int nb_presets;
  179. SpecifierOpt *copy_initial_nonkeyframes;
  180. int nb_copy_initial_nonkeyframes;
  181. SpecifierOpt *copy_prior_start;
  182. int nb_copy_prior_start;
  183. SpecifierOpt *filters;
  184. int nb_filters;
  185. SpecifierOpt *filter_scripts;
  186. int nb_filter_scripts;
  187. SpecifierOpt *reinit_filters;
  188. int nb_reinit_filters;
  189. SpecifierOpt *fix_sub_duration;
  190. int nb_fix_sub_duration;
  191. SpecifierOpt *canvas_sizes;
  192. int nb_canvas_sizes;
  193. SpecifierOpt *pass;
  194. int nb_pass;
  195. SpecifierOpt *passlogfiles;
  196. int nb_passlogfiles;
  197. SpecifierOpt *max_muxing_queue_size;
  198. int nb_max_muxing_queue_size;
  199. SpecifierOpt *guess_layout_max;
  200. int nb_guess_layout_max;
  201. SpecifierOpt *apad;
  202. int nb_apad;
  203. SpecifierOpt *discard;
  204. int nb_discard;
  205. SpecifierOpt *disposition;
  206. int nb_disposition;
  207. SpecifierOpt *program;
  208. int nb_program;
  209. SpecifierOpt *time_bases;
  210. int nb_time_bases;
  211. SpecifierOpt *enc_time_bases;
  212. int nb_enc_time_bases;
  213. } OptionsContext;
  214. typedef struct InputFilter {
  215. AVFilterContext *filter;
  216. struct InputStream *ist;
  217. struct FilterGraph *graph;
  218. uint8_t *name;
  219. enum AVMediaType type; // AVMEDIA_TYPE_SUBTITLE for sub2video
  220. AVFifoBuffer *frame_queue;
  221. // parameters configured for this input
  222. int format;
  223. int width, height;
  224. AVRational sample_aspect_ratio;
  225. int sample_rate;
  226. int channels;
  227. uint64_t channel_layout;
  228. AVBufferRef *hw_frames_ctx;
  229. int eof;
  230. } InputFilter;
  231. typedef struct OutputFilter {
  232. AVFilterContext *filter;
  233. struct OutputStream *ost;
  234. struct FilterGraph *graph;
  235. uint8_t *name;
  236. /* temporary storage until stream maps are processed */
  237. AVFilterInOut *out_tmp;
  238. enum AVMediaType type;
  239. /* desired output stream properties */
  240. int width, height;
  241. AVRational frame_rate;
  242. int format;
  243. int sample_rate;
  244. uint64_t channel_layout;
  245. // those are only set if no format is specified and the encoder gives us multiple options
  246. int *formats;
  247. uint64_t *channel_layouts;
  248. int *sample_rates;
  249. } OutputFilter;
  250. typedef struct FilterGraph {
  251. int index;
  252. const char *graph_desc;
  253. AVFilterGraph *graph;
  254. int reconfiguration;
  255. InputFilter **inputs;
  256. int nb_inputs;
  257. OutputFilter **outputs;
  258. int nb_outputs;
  259. } FilterGraph;
  260. typedef struct InputStream {
  261. int file_index;
  262. AVStream *st;
  263. int discard; /* true if stream data should be discarded */
  264. int user_set_discard;
  265. int decoding_needed; /* non zero if the packets must be decoded in 'raw_fifo', see DECODING_FOR_* */
  266. #define DECODING_FOR_OST 1
  267. #define DECODING_FOR_FILTER 2
  268. AVCodecContext *dec_ctx;
  269. AVCodec *dec;
  270. AVFrame *decoded_frame;
  271. AVFrame *filter_frame; /* a ref of decoded_frame, to be sent to filters */
  272. int64_t start; /* time when read started */
  273. /* predicted dts of the next packet read for this stream or (when there are
  274. * several frames in a packet) of the next frame in current packet (in AV_TIME_BASE units) */
  275. int64_t next_dts;
  276. int64_t dts; ///< dts of the last packet read for this stream (in AV_TIME_BASE units)
  277. int64_t next_pts; ///< synthetic pts for the next decode frame (in AV_TIME_BASE units)
  278. int64_t pts; ///< current pts of the decoded frame (in AV_TIME_BASE units)
  279. int wrap_correction_done;
  280. int64_t filter_in_rescale_delta_last;
  281. int64_t min_pts; /* pts with the smallest value in a current stream */
  282. int64_t max_pts; /* pts with the higher value in a current stream */
  283. // when forcing constant input framerate through -r,
  284. // this contains the pts that will be given to the next decoded frame
  285. int64_t cfr_next_pts;
  286. int64_t nb_samples; /* number of samples in the last decoded audio frame before looping */
  287. double ts_scale;
  288. int saw_first_ts;
  289. AVDictionary *decoder_opts;
  290. AVRational framerate; /* framerate forced with -r */
  291. int top_field_first;
  292. int guess_layout_max;
  293. int autorotate;
  294. int fix_sub_duration;
  295. struct { /* previous decoded subtitle and related variables */
  296. int got_output;
  297. int ret;
  298. AVSubtitle subtitle;
  299. } prev_sub;
  300. struct sub2video {
  301. int64_t last_pts;
  302. int64_t end_pts;
  303. AVFifoBuffer *sub_queue; ///< queue of AVSubtitle* before filter init
  304. AVFrame *frame;
  305. int w, h;
  306. } sub2video;
  307. int dr1;
  308. /* decoded data from this stream goes into all those filters
  309. * currently video and audio only */
  310. InputFilter **filters;
  311. int nb_filters;
  312. int reinit_filters;
  313. /* hwaccel options */
  314. enum HWAccelID hwaccel_id;
  315. char *hwaccel_device;
  316. enum AVPixelFormat hwaccel_output_format;
  317. /* hwaccel context */
  318. enum HWAccelID active_hwaccel_id;
  319. void *hwaccel_ctx;
  320. void (*hwaccel_uninit)(AVCodecContext *s);
  321. int (*hwaccel_get_buffer)(AVCodecContext *s, AVFrame *frame, int flags);
  322. int (*hwaccel_retrieve_data)(AVCodecContext *s, AVFrame *frame);
  323. enum AVPixelFormat hwaccel_pix_fmt;
  324. enum AVPixelFormat hwaccel_retrieved_pix_fmt;
  325. AVBufferRef *hw_frames_ctx;
  326. /* stats */
  327. // combined size of all the packets read
  328. uint64_t data_size;
  329. /* number of packets successfully read for this stream */
  330. uint64_t nb_packets;
  331. // number of frames/samples retrieved from the decoder
  332. uint64_t frames_decoded;
  333. uint64_t samples_decoded;
  334. int64_t *dts_buffer;
  335. int nb_dts_buffer;
  336. int got_output;
  337. } InputStream;
  338. typedef struct InputFile {
  339. AVFormatContext *ctx;
  340. int eof_reached; /* true if eof reached */
  341. int eagain; /* true if last read attempt returned EAGAIN */
  342. int ist_index; /* index of first stream in input_streams */
  343. int loop; /* set number of times input stream should be looped */
  344. int64_t duration; /* actual duration of the longest stream in a file
  345. at the moment when looping happens */
  346. AVRational time_base; /* time base of the duration */
  347. int64_t input_ts_offset;
  348. int64_t ts_offset;
  349. int64_t last_ts;
  350. int64_t start_time; /* user-specified start time in AV_TIME_BASE or AV_NOPTS_VALUE */
  351. int seek_timestamp;
  352. int64_t recording_time;
  353. int nb_streams; /* number of stream that ffmpeg is aware of; may be different
  354. from ctx.nb_streams if new streams appear during av_read_frame() */
  355. int nb_streams_warn; /* number of streams that the user was warned of */
  356. int rate_emu;
  357. int accurate_seek;
  358. #if HAVE_PTHREADS
  359. AVThreadMessageQueue *in_thread_queue;
  360. pthread_t thread; /* thread reading from this file */
  361. int non_blocking; /* reading packets from the thread should not block */
  362. int joined; /* the thread has been joined */
  363. int thread_queue_size; /* maximum number of queued packets */
  364. #endif
  365. } InputFile;
  366. enum forced_keyframes_const {
  367. FKF_N,
  368. FKF_N_FORCED,
  369. FKF_PREV_FORCED_N,
  370. FKF_PREV_FORCED_T,
  371. FKF_T,
  372. FKF_NB
  373. };
  374. #define ABORT_ON_FLAG_EMPTY_OUTPUT (1 << 0)
  375. extern const char *const forced_keyframes_const_names[];
  376. typedef enum {
  377. ENCODER_FINISHED = 1,
  378. MUXER_FINISHED = 2,
  379. } OSTFinished ;
  380. typedef struct OutputStream {
  381. int file_index; /* file index */
  382. int index; /* stream index in the output file */
  383. int source_index; /* InputStream index */
  384. AVStream *st; /* stream in the output file */
  385. int encoding_needed; /* true if encoding needed for this stream */
  386. int frame_number;
  387. /* input pts and corresponding output pts
  388. for A/V sync */
  389. struct InputStream *sync_ist; /* input stream to sync against */
  390. int64_t sync_opts; /* output frame counter, could be changed to some true timestamp */ // FIXME look at frame_number
  391. /* pts of the first frame encoded for this stream, used for limiting
  392. * recording time */
  393. int64_t first_pts;
  394. /* dts of the last packet sent to the muxer */
  395. int64_t last_mux_dts;
  396. // the timebase of the packets sent to the muxer
  397. AVRational mux_timebase;
  398. AVRational enc_timebase;
  399. int nb_bitstream_filters;
  400. AVBSFContext **bsf_ctx;
  401. AVCodecContext *enc_ctx;
  402. AVCodecParameters *ref_par; /* associated input codec parameters with encoders options applied */
  403. AVCodec *enc;
  404. int64_t max_frames;
  405. AVFrame *filtered_frame;
  406. AVFrame *last_frame;
  407. int last_dropped;
  408. int last_nb0_frames[3];
  409. void *hwaccel_ctx;
  410. /* video only */
  411. AVRational frame_rate;
  412. int is_cfr;
  413. int force_fps;
  414. int top_field_first;
  415. int rotate_overridden;
  416. double rotate_override_value;
  417. AVRational frame_aspect_ratio;
  418. /* forced key frames */
  419. int64_t *forced_kf_pts;
  420. int forced_kf_count;
  421. int forced_kf_index;
  422. char *forced_keyframes;
  423. AVExpr *forced_keyframes_pexpr;
  424. double forced_keyframes_expr_const_values[FKF_NB];
  425. /* audio only */
  426. int *audio_channels_map; /* list of the channels id to pick from the source stream */
  427. int audio_channels_mapped; /* number of channels in audio_channels_map */
  428. char *logfile_prefix;
  429. FILE *logfile;
  430. OutputFilter *filter;
  431. char *avfilter;
  432. char *filters; ///< filtergraph associated to the -filter option
  433. char *filters_script; ///< filtergraph script associated to the -filter_script option
  434. AVDictionary *encoder_opts;
  435. AVDictionary *sws_dict;
  436. AVDictionary *swr_opts;
  437. AVDictionary *resample_opts;
  438. char *apad;
  439. OSTFinished finished; /* no more packets should be written for this stream */
  440. int unavailable; /* true if the steram is unavailable (possibly temporarily) */
  441. int stream_copy;
  442. // init_output_stream() has been called for this stream
  443. // The encoder and the bitstream filters have been initialized and the stream
  444. // parameters are set in the AVStream.
  445. int initialized;
  446. int inputs_done;
  447. const char *attachment_filename;
  448. int copy_initial_nonkeyframes;
  449. int copy_prior_start;
  450. char *disposition;
  451. int keep_pix_fmt;
  452. AVCodecParserContext *parser;
  453. AVCodecContext *parser_avctx;
  454. /* stats */
  455. // combined size of all the packets written
  456. uint64_t data_size;
  457. // number of packets send to the muxer
  458. uint64_t packets_written;
  459. // number of frames/samples sent to the encoder
  460. uint64_t frames_encoded;
  461. uint64_t samples_encoded;
  462. /* packet quality factor */
  463. int quality;
  464. int max_muxing_queue_size;
  465. /* the packets are buffered here until the muxer is ready to be initialized */
  466. AVFifoBuffer *muxing_queue;
  467. /* packet picture type */
  468. int pict_type;
  469. /* frame encode sum of squared error values */
  470. int64_t error[4];
  471. } OutputStream;
  472. typedef struct OutputFile {
  473. AVFormatContext *ctx;
  474. AVDictionary *opts;
  475. int ost_index; /* index of the first stream in output_streams */
  476. int64_t recording_time; ///< desired length of the resulting file in microseconds == AV_TIME_BASE units
  477. int64_t start_time; ///< start time in microseconds == AV_TIME_BASE units
  478. uint64_t limit_filesize; /* filesize limit expressed in bytes */
  479. int shortest;
  480. int header_written;
  481. } OutputFile;
  482. extern InputStream **input_streams;
  483. extern int nb_input_streams;
  484. extern InputFile **input_files;
  485. extern int nb_input_files;
  486. extern OutputStream **output_streams;
  487. extern int nb_output_streams;
  488. extern OutputFile **output_files;
  489. extern int nb_output_files;
  490. extern FilterGraph **filtergraphs;
  491. extern int nb_filtergraphs;
  492. extern char *vstats_filename;
  493. extern char *sdp_filename;
  494. extern float audio_drift_threshold;
  495. extern float dts_delta_threshold;
  496. extern float dts_error_threshold;
  497. extern int audio_volume;
  498. extern int audio_sync_method;
  499. extern int video_sync_method;
  500. extern float frame_drop_threshold;
  501. extern int do_benchmark;
  502. extern int do_benchmark_all;
  503. extern int do_deinterlace;
  504. extern int do_hex_dump;
  505. extern int do_pkt_dump;
  506. extern int copy_ts;
  507. extern int start_at_zero;
  508. extern int copy_tb;
  509. extern int debug_ts;
  510. extern int exit_on_error;
  511. extern int abort_on_flags;
  512. extern int print_stats;
  513. extern int qp_hist;
  514. extern int stdin_interaction;
  515. extern int frame_bits_per_raw_sample;
  516. extern AVIOContext *progress_avio;
  517. extern float max_error_rate;
  518. extern char *videotoolbox_pixfmt;
  519. extern int filter_nbthreads;
  520. extern int filter_complex_nbthreads;
  521. extern int vstats_version;
  522. extern const AVIOInterruptCB int_cb;
  523. extern const OptionDef options[];
  524. extern const HWAccel hwaccels[];
  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 videotoolbox_init(AVCodecContext *s);
  551. int qsv_init(AVCodecContext *s);
  552. int cuvid_init(AVCodecContext *s);
  553. HWDevice *hw_device_get_by_name(const char *name);
  554. int hw_device_init_from_string(const char *arg, HWDevice **dev);
  555. void hw_device_free_all(void);
  556. int hw_device_setup_for_decode(InputStream *ist);
  557. int hw_device_setup_for_encode(OutputStream *ost);
  558. int hwaccel_decode_init(AVCodecContext *avctx);
  559. #endif /* FFTOOLS_FFMPEG_H */