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.

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