ffmpeg.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955
  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 <stdatomic.h>
  22. #include <stdint.h>
  23. #include <stdio.h>
  24. #include <signal.h>
  25. #include "cmdutils.h"
  26. #include "sync_queue.h"
  27. #include "libavformat/avformat.h"
  28. #include "libavformat/avio.h"
  29. #include "libavcodec/avcodec.h"
  30. #include "libavcodec/bsf.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/thread.h"
  40. #include "libavutil/threadmessage.h"
  41. #include "libswresample/swresample.h"
  42. // deprecated features
  43. #define FFMPEG_OPT_PSNR 1
  44. #define FFMPEG_OPT_MAP_CHANNEL 1
  45. #define FFMPEG_OPT_MAP_SYNC 1
  46. #define FFMPEG_ROTATION_METADATA 1
  47. #define FFMPEG_OPT_QPHIST 1
  48. #define FFMPEG_OPT_ADRIFT_THRESHOLD 1
  49. #define FFMPEG_OPT_ENC_TIME_BASE_NUM 1
  50. #define FFMPEG_OPT_TOP 1
  51. #define FFMPEG_OPT_FORCE_KF_SOURCE_NO_DROP 1
  52. enum VideoSyncMethod {
  53. VSYNC_AUTO = -1,
  54. VSYNC_PASSTHROUGH,
  55. VSYNC_CFR,
  56. VSYNC_VFR,
  57. VSYNC_VSCFR,
  58. VSYNC_DROP,
  59. };
  60. enum EncTimeBase {
  61. ENC_TIME_BASE_DEMUX = -1,
  62. ENC_TIME_BASE_FILTER = -2,
  63. };
  64. enum HWAccelID {
  65. HWACCEL_NONE = 0,
  66. HWACCEL_AUTO,
  67. HWACCEL_GENERIC,
  68. };
  69. typedef struct HWDevice {
  70. const char *name;
  71. enum AVHWDeviceType type;
  72. AVBufferRef *device_ref;
  73. } HWDevice;
  74. /* select an input stream for an output stream */
  75. typedef struct StreamMap {
  76. int disabled; /* 1 is this mapping is disabled by a negative map */
  77. int file_index;
  78. int stream_index;
  79. char *linklabel; /* name of an output link, for mapping lavfi outputs */
  80. } StreamMap;
  81. #if FFMPEG_OPT_MAP_CHANNEL
  82. typedef struct {
  83. int file_idx, stream_idx, channel_idx; // input
  84. int ofile_idx, ostream_idx; // output
  85. } AudioChannelMap;
  86. #endif
  87. typedef struct DemuxPktData {
  88. // estimated dts in AV_TIME_BASE_Q,
  89. // to be used when real dts is missing
  90. int64_t dts_est;
  91. } DemuxPktData;
  92. typedef struct OptionsContext {
  93. OptionGroup *g;
  94. /* input/output options */
  95. int64_t start_time;
  96. int64_t start_time_eof;
  97. int seek_timestamp;
  98. const char *format;
  99. SpecifierOpt *codec_names;
  100. int nb_codec_names;
  101. SpecifierOpt *audio_ch_layouts;
  102. int nb_audio_ch_layouts;
  103. SpecifierOpt *audio_channels;
  104. int nb_audio_channels;
  105. SpecifierOpt *audio_sample_rate;
  106. int nb_audio_sample_rate;
  107. SpecifierOpt *frame_rates;
  108. int nb_frame_rates;
  109. SpecifierOpt *max_frame_rates;
  110. int nb_max_frame_rates;
  111. SpecifierOpt *frame_sizes;
  112. int nb_frame_sizes;
  113. SpecifierOpt *frame_pix_fmts;
  114. int nb_frame_pix_fmts;
  115. /* input options */
  116. int64_t input_ts_offset;
  117. int loop;
  118. int rate_emu;
  119. float readrate;
  120. double readrate_initial_burst;
  121. int accurate_seek;
  122. int thread_queue_size;
  123. int input_sync_ref;
  124. int find_stream_info;
  125. SpecifierOpt *ts_scale;
  126. int nb_ts_scale;
  127. SpecifierOpt *dump_attachment;
  128. int nb_dump_attachment;
  129. SpecifierOpt *hwaccels;
  130. int nb_hwaccels;
  131. SpecifierOpt *hwaccel_devices;
  132. int nb_hwaccel_devices;
  133. SpecifierOpt *hwaccel_output_formats;
  134. int nb_hwaccel_output_formats;
  135. SpecifierOpt *autorotate;
  136. int nb_autorotate;
  137. /* output options */
  138. StreamMap *stream_maps;
  139. int nb_stream_maps;
  140. #if FFMPEG_OPT_MAP_CHANNEL
  141. AudioChannelMap *audio_channel_maps; /* one info entry per -map_channel */
  142. int nb_audio_channel_maps; /* number of (valid) -map_channel settings */
  143. #endif
  144. const char **attachments;
  145. int nb_attachments;
  146. int chapters_input_file;
  147. int64_t recording_time;
  148. int64_t stop_time;
  149. int64_t limit_filesize;
  150. float mux_preload;
  151. float mux_max_delay;
  152. float shortest_buf_duration;
  153. int shortest;
  154. int bitexact;
  155. int video_disable;
  156. int audio_disable;
  157. int subtitle_disable;
  158. int data_disable;
  159. // keys are stream indices
  160. AVDictionary *streamid;
  161. SpecifierOpt *metadata;
  162. int nb_metadata;
  163. SpecifierOpt *max_frames;
  164. int nb_max_frames;
  165. SpecifierOpt *bitstream_filters;
  166. int nb_bitstream_filters;
  167. SpecifierOpt *codec_tags;
  168. int nb_codec_tags;
  169. SpecifierOpt *sample_fmts;
  170. int nb_sample_fmts;
  171. SpecifierOpt *qscale;
  172. int nb_qscale;
  173. SpecifierOpt *forced_key_frames;
  174. int nb_forced_key_frames;
  175. SpecifierOpt *fps_mode;
  176. int nb_fps_mode;
  177. SpecifierOpt *force_fps;
  178. int nb_force_fps;
  179. SpecifierOpt *frame_aspect_ratios;
  180. int nb_frame_aspect_ratios;
  181. SpecifierOpt *display_rotations;
  182. int nb_display_rotations;
  183. SpecifierOpt *display_hflips;
  184. int nb_display_hflips;
  185. SpecifierOpt *display_vflips;
  186. int nb_display_vflips;
  187. SpecifierOpt *rc_overrides;
  188. int nb_rc_overrides;
  189. SpecifierOpt *intra_matrices;
  190. int nb_intra_matrices;
  191. SpecifierOpt *inter_matrices;
  192. int nb_inter_matrices;
  193. SpecifierOpt *chroma_intra_matrices;
  194. int nb_chroma_intra_matrices;
  195. #if FFMPEG_OPT_TOP
  196. SpecifierOpt *top_field_first;
  197. int nb_top_field_first;
  198. #endif
  199. SpecifierOpt *metadata_map;
  200. int nb_metadata_map;
  201. SpecifierOpt *presets;
  202. int nb_presets;
  203. SpecifierOpt *copy_initial_nonkeyframes;
  204. int nb_copy_initial_nonkeyframes;
  205. SpecifierOpt *copy_prior_start;
  206. int nb_copy_prior_start;
  207. SpecifierOpt *filters;
  208. int nb_filters;
  209. SpecifierOpt *filter_scripts;
  210. int nb_filter_scripts;
  211. SpecifierOpt *reinit_filters;
  212. int nb_reinit_filters;
  213. SpecifierOpt *fix_sub_duration;
  214. int nb_fix_sub_duration;
  215. SpecifierOpt *fix_sub_duration_heartbeat;
  216. int nb_fix_sub_duration_heartbeat;
  217. SpecifierOpt *canvas_sizes;
  218. int nb_canvas_sizes;
  219. SpecifierOpt *pass;
  220. int nb_pass;
  221. SpecifierOpt *passlogfiles;
  222. int nb_passlogfiles;
  223. SpecifierOpt *max_muxing_queue_size;
  224. int nb_max_muxing_queue_size;
  225. SpecifierOpt *muxing_queue_data_threshold;
  226. int nb_muxing_queue_data_threshold;
  227. SpecifierOpt *guess_layout_max;
  228. int nb_guess_layout_max;
  229. SpecifierOpt *apad;
  230. int nb_apad;
  231. SpecifierOpt *discard;
  232. int nb_discard;
  233. SpecifierOpt *disposition;
  234. int nb_disposition;
  235. SpecifierOpt *program;
  236. int nb_program;
  237. SpecifierOpt *time_bases;
  238. int nb_time_bases;
  239. SpecifierOpt *enc_time_bases;
  240. int nb_enc_time_bases;
  241. SpecifierOpt *autoscale;
  242. int nb_autoscale;
  243. SpecifierOpt *bits_per_raw_sample;
  244. int nb_bits_per_raw_sample;
  245. SpecifierOpt *enc_stats_pre;
  246. int nb_enc_stats_pre;
  247. SpecifierOpt *enc_stats_post;
  248. int nb_enc_stats_post;
  249. SpecifierOpt *mux_stats;
  250. int nb_mux_stats;
  251. SpecifierOpt *enc_stats_pre_fmt;
  252. int nb_enc_stats_pre_fmt;
  253. SpecifierOpt *enc_stats_post_fmt;
  254. int nb_enc_stats_post_fmt;
  255. SpecifierOpt *mux_stats_fmt;
  256. int nb_mux_stats_fmt;
  257. } OptionsContext;
  258. typedef struct InputFilter {
  259. struct FilterGraph *graph;
  260. uint8_t *name;
  261. } InputFilter;
  262. typedef struct OutputFilter {
  263. struct OutputStream *ost;
  264. struct FilterGraph *graph;
  265. uint8_t *name;
  266. /* for filters that are not yet bound to an output stream,
  267. * this stores the output linklabel, if any */
  268. uint8_t *linklabel;
  269. enum AVMediaType type;
  270. /* pts of the last frame received from this filter, in AV_TIME_BASE_Q */
  271. int64_t last_pts;
  272. uint64_t nb_frames_dup;
  273. uint64_t nb_frames_drop;
  274. } OutputFilter;
  275. typedef struct FilterGraph {
  276. const AVClass *class;
  277. int index;
  278. AVFilterGraph *graph;
  279. InputFilter **inputs;
  280. int nb_inputs;
  281. OutputFilter **outputs;
  282. int nb_outputs;
  283. } FilterGraph;
  284. typedef struct Decoder Decoder;
  285. typedef struct InputStream {
  286. const AVClass *class;
  287. int file_index;
  288. int index;
  289. AVStream *st;
  290. int discard; /* true if stream data should be discarded */
  291. int user_set_discard;
  292. int decoding_needed; /* non zero if the packets must be decoded in 'raw_fifo', see DECODING_FOR_* */
  293. #define DECODING_FOR_OST 1
  294. #define DECODING_FOR_FILTER 2
  295. /**
  296. * Codec parameters - to be used by the decoding/streamcopy code.
  297. * st->codecpar should not be accessed, because it may be modified
  298. * concurrently by the demuxing thread.
  299. */
  300. AVCodecParameters *par;
  301. Decoder *decoder;
  302. AVCodecContext *dec_ctx;
  303. const AVCodec *dec;
  304. const AVCodecDescriptor *codec_desc;
  305. AVRational framerate_guessed;
  306. int64_t nb_samples; /* number of samples in the last decoded audio frame before looping */
  307. AVDictionary *decoder_opts;
  308. AVRational framerate; /* framerate forced with -r */
  309. #if FFMPEG_OPT_TOP
  310. int top_field_first;
  311. #endif
  312. int autorotate;
  313. int fix_sub_duration;
  314. struct sub2video {
  315. int w, h;
  316. } sub2video;
  317. /* decoded data from this stream goes into all those filters
  318. * currently video and audio only */
  319. InputFilter **filters;
  320. int nb_filters;
  321. /*
  322. * Output targets that do not go through lavfi, i.e. subtitles or
  323. * streamcopy. Those two cases are distinguished by the OutputStream
  324. * having an encoder or not.
  325. */
  326. struct OutputStream **outputs;
  327. int nb_outputs;
  328. int reinit_filters;
  329. /* hwaccel options */
  330. enum HWAccelID hwaccel_id;
  331. enum AVHWDeviceType hwaccel_device_type;
  332. char *hwaccel_device;
  333. enum AVPixelFormat hwaccel_output_format;
  334. /* stats */
  335. // number of frames/samples retrieved from the decoder
  336. uint64_t frames_decoded;
  337. uint64_t samples_decoded;
  338. uint64_t decode_errors;
  339. } InputStream;
  340. typedef struct LastFrameDuration {
  341. int stream_idx;
  342. int64_t duration;
  343. } LastFrameDuration;
  344. typedef struct InputFile {
  345. const AVClass *class;
  346. int index;
  347. // input format has no timestamps
  348. int format_nots;
  349. AVFormatContext *ctx;
  350. int eof_reached; /* true if eof reached */
  351. int eagain; /* true if last read attempt returned EAGAIN */
  352. int64_t input_ts_offset;
  353. int input_sync_ref;
  354. /**
  355. * Effective format start time based on enabled streams.
  356. */
  357. int64_t start_time_effective;
  358. int64_t ts_offset;
  359. int64_t start_time; /* user-specified start time in AV_TIME_BASE or AV_NOPTS_VALUE */
  360. int64_t recording_time;
  361. /* streams that ffmpeg is aware of;
  362. * there may be extra streams in ctx that are not mapped to an InputStream
  363. * if new streams appear dynamically during demuxing */
  364. InputStream **streams;
  365. int nb_streams;
  366. float readrate;
  367. int accurate_seek;
  368. /* when looping the input file, this queue is used by decoders to report
  369. * the last frame duration back to the demuxer thread */
  370. AVThreadMessageQueue *audio_duration_queue;
  371. int audio_duration_queue_size;
  372. } InputFile;
  373. enum forced_keyframes_const {
  374. FKF_N,
  375. FKF_N_FORCED,
  376. FKF_PREV_FORCED_N,
  377. FKF_PREV_FORCED_T,
  378. FKF_T,
  379. FKF_NB
  380. };
  381. #define ABORT_ON_FLAG_EMPTY_OUTPUT (1 << 0)
  382. #define ABORT_ON_FLAG_EMPTY_OUTPUT_STREAM (1 << 1)
  383. enum EncStatsType {
  384. ENC_STATS_LITERAL = 0,
  385. ENC_STATS_FILE_IDX,
  386. ENC_STATS_STREAM_IDX,
  387. ENC_STATS_FRAME_NUM,
  388. ENC_STATS_FRAME_NUM_IN,
  389. ENC_STATS_TIMEBASE,
  390. ENC_STATS_TIMEBASE_IN,
  391. ENC_STATS_PTS,
  392. ENC_STATS_PTS_TIME,
  393. ENC_STATS_PTS_IN,
  394. ENC_STATS_PTS_TIME_IN,
  395. ENC_STATS_DTS,
  396. ENC_STATS_DTS_TIME,
  397. ENC_STATS_SAMPLE_NUM,
  398. ENC_STATS_NB_SAMPLES,
  399. ENC_STATS_PKT_SIZE,
  400. ENC_STATS_BITRATE,
  401. ENC_STATS_AVG_BITRATE,
  402. };
  403. typedef struct EncStatsComponent {
  404. enum EncStatsType type;
  405. uint8_t *str;
  406. size_t str_len;
  407. } EncStatsComponent;
  408. typedef struct EncStats {
  409. EncStatsComponent *components;
  410. int nb_components;
  411. AVIOContext *io;
  412. } EncStats;
  413. extern const char *const forced_keyframes_const_names[];
  414. typedef enum {
  415. ENCODER_FINISHED = 1,
  416. MUXER_FINISHED = 2,
  417. } OSTFinished ;
  418. enum {
  419. KF_FORCE_SOURCE = 1,
  420. #if FFMPEG_OPT_FORCE_KF_SOURCE_NO_DROP
  421. KF_FORCE_SOURCE_NO_DROP = 2,
  422. #endif
  423. };
  424. typedef struct KeyframeForceCtx {
  425. int type;
  426. int64_t ref_pts;
  427. // timestamps of the forced keyframes, in AV_TIME_BASE_Q
  428. int64_t *pts;
  429. int nb_pts;
  430. int index;
  431. AVExpr *pexpr;
  432. double expr_const_values[FKF_NB];
  433. int dropped_keyframe;
  434. } KeyframeForceCtx;
  435. typedef struct Encoder Encoder;
  436. typedef struct OutputStream {
  437. const AVClass *class;
  438. enum AVMediaType type;
  439. int file_index; /* file index */
  440. int index; /* stream index in the output file */
  441. /**
  442. * Codec parameters for packets submitted to the muxer (i.e. before
  443. * bitstream filtering, if any).
  444. */
  445. AVCodecParameters *par_in;
  446. /* input stream that is the source for this output stream;
  447. * may be NULL for streams with no well-defined source, e.g.
  448. * attachments or outputs from complex filtergraphs */
  449. InputStream *ist;
  450. AVStream *st; /* stream in the output file */
  451. /* dts of the last packet sent to the muxing queue, in AV_TIME_BASE_Q */
  452. int64_t last_mux_dts;
  453. AVRational enc_timebase;
  454. Encoder *enc;
  455. AVCodecContext *enc_ctx;
  456. /* video only */
  457. AVRational frame_rate;
  458. AVRational max_frame_rate;
  459. enum VideoSyncMethod vsync_method;
  460. int is_cfr;
  461. int force_fps;
  462. #if FFMPEG_OPT_TOP
  463. int top_field_first;
  464. #endif
  465. #if FFMPEG_ROTATION_METADATA
  466. int rotate_overridden;
  467. #endif
  468. int autoscale;
  469. int bitexact;
  470. int bits_per_raw_sample;
  471. #if FFMPEG_ROTATION_METADATA
  472. double rotate_override_value;
  473. #endif
  474. AVRational frame_aspect_ratio;
  475. KeyframeForceCtx kf;
  476. /* audio only */
  477. #if FFMPEG_OPT_MAP_CHANNEL
  478. int *audio_channels_map; /* list of the channels id to pick from the source stream */
  479. int audio_channels_mapped; /* number of channels in audio_channels_map */
  480. #endif
  481. char *logfile_prefix;
  482. FILE *logfile;
  483. OutputFilter *filter;
  484. AVDictionary *encoder_opts;
  485. AVDictionary *sws_dict;
  486. AVDictionary *swr_opts;
  487. char *apad;
  488. OSTFinished finished; /* no more packets should be written for this stream */
  489. int unavailable; /* true if the steram is unavailable (possibly temporarily) */
  490. // init_output_stream() has been called for this stream
  491. // The encoder and the bitstream filters have been initialized and the stream
  492. // parameters are set in the AVStream.
  493. int initialized;
  494. const char *attachment_filename;
  495. int keep_pix_fmt;
  496. /* stats */
  497. // number of packets send to the muxer
  498. atomic_uint_least64_t packets_written;
  499. // number of frames/samples sent to the encoder
  500. uint64_t frames_encoded;
  501. uint64_t samples_encoded;
  502. /* packet quality factor */
  503. int quality;
  504. int sq_idx_encode;
  505. int sq_idx_mux;
  506. EncStats enc_stats_pre;
  507. EncStats enc_stats_post;
  508. /*
  509. * bool on whether this stream should be utilized for splitting
  510. * subtitles utilizing fix_sub_duration at random access points.
  511. */
  512. unsigned int fix_sub_duration_heartbeat;
  513. } OutputStream;
  514. typedef struct OutputFile {
  515. const AVClass *class;
  516. int index;
  517. const AVOutputFormat *format;
  518. const char *url;
  519. OutputStream **streams;
  520. int nb_streams;
  521. SyncQueue *sq_encode;
  522. int64_t recording_time; ///< desired length of the resulting file in microseconds == AV_TIME_BASE units
  523. int64_t start_time; ///< start time in microseconds == AV_TIME_BASE units
  524. int shortest;
  525. int bitexact;
  526. } OutputFile;
  527. // optionally attached as opaque_ref to decoded AVFrames
  528. typedef struct FrameData {
  529. // properties that come from the decoder
  530. struct {
  531. uint64_t frame_num;
  532. int64_t pts;
  533. AVRational tb;
  534. } dec;
  535. AVRational frame_rate_filter;
  536. int bits_per_raw_sample;
  537. } FrameData;
  538. extern InputFile **input_files;
  539. extern int nb_input_files;
  540. extern OutputFile **output_files;
  541. extern int nb_output_files;
  542. extern FilterGraph **filtergraphs;
  543. extern int nb_filtergraphs;
  544. extern char *vstats_filename;
  545. extern char *sdp_filename;
  546. extern float dts_delta_threshold;
  547. extern float dts_error_threshold;
  548. extern enum VideoSyncMethod video_sync_method;
  549. extern float frame_drop_threshold;
  550. extern int do_benchmark;
  551. extern int do_benchmark_all;
  552. extern int do_hex_dump;
  553. extern int do_pkt_dump;
  554. extern int copy_ts;
  555. extern int start_at_zero;
  556. extern int copy_tb;
  557. extern int debug_ts;
  558. extern int exit_on_error;
  559. extern int abort_on_flags;
  560. extern int print_stats;
  561. extern int64_t stats_period;
  562. extern int stdin_interaction;
  563. extern AVIOContext *progress_avio;
  564. extern float max_error_rate;
  565. extern char *filter_nbthreads;
  566. extern int filter_complex_nbthreads;
  567. extern int vstats_version;
  568. extern int auto_conversion_filters;
  569. extern const AVIOInterruptCB int_cb;
  570. extern const OptionDef options[];
  571. extern HWDevice *filter_hw_device;
  572. extern unsigned nb_output_dumped;
  573. extern int ignore_unknown_streams;
  574. extern int copy_unknown_streams;
  575. extern int recast_media;
  576. extern FILE *vstats_file;
  577. #if FFMPEG_OPT_PSNR
  578. extern int do_psnr;
  579. #endif
  580. void term_init(void);
  581. void term_exit(void);
  582. void show_usage(void);
  583. void remove_avoptions(AVDictionary **a, AVDictionary *b);
  584. int check_avoptions(AVDictionary *m);
  585. int assert_file_overwrite(const char *filename);
  586. char *file_read(const char *filename);
  587. AVDictionary *strip_specifiers(const AVDictionary *dict);
  588. int find_codec(void *logctx, const char *name,
  589. enum AVMediaType type, int encoder, const AVCodec **codec);
  590. int parse_and_set_vsync(const char *arg, int *vsync_var, int file_idx, int st_idx, int is_global);
  591. int check_filter_outputs(void);
  592. int filtergraph_is_simple(const FilterGraph *fg);
  593. int init_simple_filtergraph(InputStream *ist, OutputStream *ost,
  594. char *graph_desc);
  595. int init_complex_filtergraph(FilterGraph *fg);
  596. int copy_av_subtitle(AVSubtitle *dst, const AVSubtitle *src);
  597. int subtitle_wrap_frame(AVFrame *frame, AVSubtitle *subtitle, int copy);
  598. /**
  599. * Get our axiliary frame data attached to the frame, allocating it
  600. * if needed.
  601. */
  602. FrameData *frame_data(AVFrame *frame);
  603. int ifilter_send_frame(InputFilter *ifilter, AVFrame *frame, int keep_reference);
  604. int ifilter_send_eof(InputFilter *ifilter, int64_t pts, AVRational tb);
  605. int ifilter_sub2video(InputFilter *ifilter, const AVFrame *frame);
  606. void ifilter_sub2video_heartbeat(InputFilter *ifilter, int64_t pts, AVRational tb);
  607. /**
  608. * Set up fallback filtering parameters from a decoder context. They will only
  609. * be used if no frames are ever sent on this input, otherwise the actual
  610. * parameters are taken from the frame.
  611. */
  612. int ifilter_parameters_from_dec(InputFilter *ifilter, const AVCodecContext *dec);
  613. int ofilter_bind_ost(OutputFilter *ofilter, OutputStream *ost);
  614. /**
  615. * Create a new filtergraph in the global filtergraph list.
  616. *
  617. * @param graph_desc Graph description; an av_malloc()ed string, filtergraph
  618. * takes ownership of it.
  619. */
  620. int fg_create(FilterGraph **pfg, char *graph_desc);
  621. void fg_free(FilterGraph **pfg);
  622. /**
  623. * Perform a step of transcoding for the specified filter graph.
  624. *
  625. * @param[in] graph filter graph to consider
  626. * @param[out] best_ist input stream where a frame would allow to continue
  627. * @return 0 for success, <0 for error
  628. */
  629. int fg_transcode_step(FilterGraph *graph, InputStream **best_ist);
  630. void fg_send_command(FilterGraph *fg, double time, const char *target,
  631. const char *command, const char *arg, int all_filters);
  632. /**
  633. * Get and encode new output from specified filtergraph, without causing
  634. * activity.
  635. *
  636. * @return 0 for success, <0 for severe errors
  637. */
  638. int reap_filters(FilterGraph *fg, int flush);
  639. int ffmpeg_parse_options(int argc, char **argv);
  640. void enc_stats_write(OutputStream *ost, EncStats *es,
  641. const AVFrame *frame, const AVPacket *pkt,
  642. uint64_t frame_num);
  643. HWDevice *hw_device_get_by_name(const char *name);
  644. HWDevice *hw_device_get_by_type(enum AVHWDeviceType type);
  645. int hw_device_init_from_string(const char *arg, HWDevice **dev);
  646. int hw_device_init_from_type(enum AVHWDeviceType type,
  647. const char *device,
  648. HWDevice **dev_out);
  649. void hw_device_free_all(void);
  650. /**
  651. * Get a hardware device to be used with this filtergraph.
  652. * The returned reference is owned by the callee, the caller
  653. * must ref it explicitly for long-term use.
  654. */
  655. AVBufferRef *hw_device_for_filter(void);
  656. int hwaccel_retrieve_data(AVCodecContext *avctx, AVFrame *input);
  657. int dec_open(InputStream *ist);
  658. void dec_free(Decoder **pdec);
  659. /**
  660. * Submit a packet for decoding
  661. *
  662. * When pkt==NULL and no_eof=0, there will be no more input. Flush decoders and
  663. * mark all downstreams as finished.
  664. *
  665. * When pkt==NULL and no_eof=1, the stream was reset (e.g. after a seek). Flush
  666. * decoders and await further input.
  667. */
  668. int dec_packet(InputStream *ist, const AVPacket *pkt, int no_eof);
  669. int enc_alloc(Encoder **penc, const AVCodec *codec);
  670. void enc_free(Encoder **penc);
  671. int enc_open(OutputStream *ost, const AVFrame *frame);
  672. int enc_subtitle(OutputFile *of, OutputStream *ost, const AVSubtitle *sub);
  673. int enc_frame(OutputStream *ost, AVFrame *frame);
  674. int enc_flush(void);
  675. /*
  676. * Initialize muxing state for the given stream, should be called
  677. * after the codec/streamcopy setup has been done.
  678. *
  679. * Open the muxer once all the streams have been initialized.
  680. */
  681. int of_stream_init(OutputFile *of, OutputStream *ost);
  682. int of_write_trailer(OutputFile *of);
  683. int of_open(const OptionsContext *o, const char *filename);
  684. void of_free(OutputFile **pof);
  685. void of_enc_stats_close(void);
  686. int of_output_packet(OutputFile *of, OutputStream *ost, AVPacket *pkt);
  687. /**
  688. * @param dts predicted packet dts in AV_TIME_BASE_Q
  689. */
  690. int of_streamcopy(OutputStream *ost, const AVPacket *pkt, int64_t dts);
  691. int64_t of_filesize(OutputFile *of);
  692. int ifile_open(const OptionsContext *o, const char *filename);
  693. void ifile_close(InputFile **f);
  694. /**
  695. * Get next input packet from the demuxer.
  696. *
  697. * @param pkt the packet is written here when this function returns 0
  698. * @return
  699. * - 0 when a packet has been read successfully
  700. * - 1 when stream end was reached, but the stream is looped;
  701. * caller should flush decoders and read from this demuxer again
  702. * - a negative error code on failure
  703. */
  704. int ifile_get_packet(InputFile *f, AVPacket **pkt);
  705. int ist_output_add(InputStream *ist, OutputStream *ost);
  706. int ist_filter_add(InputStream *ist, InputFilter *ifilter, int is_simple);
  707. /**
  708. * Find an unused input stream of given type.
  709. */
  710. InputStream *ist_find_unused(enum AVMediaType type);
  711. /* iterate over all input streams in all input files;
  712. * pass NULL to start iteration */
  713. InputStream *ist_iter(InputStream *prev);
  714. /* iterate over all output streams in all output files;
  715. * pass NULL to start iteration */
  716. OutputStream *ost_iter(OutputStream *prev);
  717. void close_output_stream(OutputStream *ost);
  718. int trigger_fix_sub_duration_heartbeat(OutputStream *ost, const AVPacket *pkt);
  719. int fix_sub_duration_heartbeat(InputStream *ist, int64_t signal_pts);
  720. void update_benchmark(const char *fmt, ...);
  721. /**
  722. * Merge two return codes - return one of the error codes if at least one of
  723. * them was negative, 0 otherwise.
  724. * Currently just picks the first one, eventually we might want to do something
  725. * more sophisticated, like sorting them by priority.
  726. */
  727. static inline int err_merge(int err0, int err1)
  728. {
  729. return (err0 < 0) ? err0 : FFMIN(err1, 0);
  730. }
  731. #define SPECIFIER_OPT_FMT_str "%s"
  732. #define SPECIFIER_OPT_FMT_i "%i"
  733. #define SPECIFIER_OPT_FMT_i64 "%"PRId64
  734. #define SPECIFIER_OPT_FMT_ui64 "%"PRIu64
  735. #define SPECIFIER_OPT_FMT_f "%f"
  736. #define SPECIFIER_OPT_FMT_dbl "%lf"
  737. #define WARN_MULTIPLE_OPT_USAGE(name, type, so, st)\
  738. {\
  739. char namestr[128] = "";\
  740. const char *spec = so->specifier && so->specifier[0] ? so->specifier : "";\
  741. for (int _i = 0; opt_name_##name[_i]; _i++)\
  742. av_strlcatf(namestr, sizeof(namestr), "-%s%s", opt_name_##name[_i], opt_name_##name[_i+1] ? (opt_name_##name[_i+2] ? ", " : " or ") : "");\
  743. av_log(NULL, AV_LOG_WARNING, "Multiple %s options specified for stream %d, only the last option '-%s%s%s "SPECIFIER_OPT_FMT_##type"' will be used.\n",\
  744. namestr, st->index, opt_name_##name[0], spec[0] ? ":" : "", spec, so->u.type);\
  745. }
  746. #define MATCH_PER_STREAM_OPT(name, type, outvar, fmtctx, st)\
  747. {\
  748. int _ret, _matches = 0;\
  749. SpecifierOpt *so;\
  750. for (int _i = 0; _i < o->nb_ ## name; _i++) {\
  751. char *spec = o->name[_i].specifier;\
  752. if ((_ret = check_stream_specifier(fmtctx, st, spec)) > 0) {\
  753. outvar = o->name[_i].u.type;\
  754. so = &o->name[_i];\
  755. _matches++;\
  756. } else if (_ret < 0)\
  757. return _ret;\
  758. }\
  759. if (_matches > 1)\
  760. WARN_MULTIPLE_OPT_USAGE(name, type, so, st);\
  761. }
  762. #define MATCH_PER_TYPE_OPT(name, type, outvar, fmtctx, mediatype)\
  763. {\
  764. int i;\
  765. for (i = 0; i < o->nb_ ## name; i++) {\
  766. char *spec = o->name[i].specifier;\
  767. if (!strcmp(spec, mediatype))\
  768. outvar = o->name[i].u.type;\
  769. }\
  770. }
  771. extern const char * const opt_name_codec_names[];
  772. extern const char * const opt_name_codec_tags[];
  773. extern const char * const opt_name_frame_rates[];
  774. #if FFMPEG_OPT_TOP
  775. extern const char * const opt_name_top_field_first[];
  776. #endif
  777. static inline void pkt_move(void *dst, void *src)
  778. {
  779. av_packet_move_ref(dst, src);
  780. }
  781. static inline void frame_move(void *dst, void *src)
  782. {
  783. av_frame_move_ref(dst, src);
  784. }
  785. #endif /* FFTOOLS_FFMPEG_H */