ffmpeg.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  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 "ffmpeg_sched.h"
  27. #include "sync_queue.h"
  28. #include "libavformat/avformat.h"
  29. #include "libavformat/avio.h"
  30. #include "libavcodec/avcodec.h"
  31. #include "libavcodec/bsf.h"
  32. #include "libavfilter/avfilter.h"
  33. #include "libavutil/avutil.h"
  34. #include "libavutil/dict.h"
  35. #include "libavutil/eval.h"
  36. #include "libavutil/fifo.h"
  37. #include "libavutil/hwcontext.h"
  38. #include "libavutil/pixfmt.h"
  39. #include "libavutil/rational.h"
  40. #include "libavutil/thread.h"
  41. #include "libavutil/threadmessage.h"
  42. #include "libswresample/swresample.h"
  43. // deprecated features
  44. #define FFMPEG_OPT_QPHIST 1
  45. #define FFMPEG_OPT_ADRIFT_THRESHOLD 1
  46. #define FFMPEG_OPT_ENC_TIME_BASE_NUM 1
  47. #define FFMPEG_OPT_TOP 1
  48. #define FFMPEG_OPT_FORCE_KF_SOURCE_NO_DROP 1
  49. #define FFMPEG_OPT_VSYNC_DROP 1
  50. #define FFMPEG_OPT_VSYNC 1
  51. #define FFMPEG_OPT_FILTER_SCRIPT 1
  52. #define FFMPEG_ERROR_RATE_EXCEEDED FFERRTAG('E', 'R', 'E', 'D')
  53. enum VideoSyncMethod {
  54. VSYNC_AUTO = -1,
  55. VSYNC_PASSTHROUGH,
  56. VSYNC_CFR,
  57. VSYNC_VFR,
  58. VSYNC_VSCFR,
  59. #if FFMPEG_OPT_VSYNC_DROP
  60. VSYNC_DROP,
  61. #endif
  62. };
  63. enum EncTimeBase {
  64. ENC_TIME_BASE_DEMUX = -1,
  65. ENC_TIME_BASE_FILTER = -2,
  66. };
  67. enum HWAccelID {
  68. HWACCEL_NONE = 0,
  69. HWACCEL_AUTO,
  70. HWACCEL_GENERIC,
  71. };
  72. enum FrameOpaque {
  73. FRAME_OPAQUE_SUB_HEARTBEAT = 1,
  74. FRAME_OPAQUE_EOF,
  75. FRAME_OPAQUE_SEND_COMMAND,
  76. };
  77. enum PacketOpaque {
  78. PKT_OPAQUE_SUB_HEARTBEAT = 1,
  79. PKT_OPAQUE_FIX_SUB_DURATION,
  80. };
  81. enum LatencyProbe {
  82. LATENCY_PROBE_DEMUX,
  83. LATENCY_PROBE_DEC_PRE,
  84. LATENCY_PROBE_DEC_POST,
  85. LATENCY_PROBE_FILTER_PRE,
  86. LATENCY_PROBE_FILTER_POST,
  87. LATENCY_PROBE_ENC_PRE,
  88. LATENCY_PROBE_ENC_POST,
  89. LATENCY_PROBE_NB,
  90. };
  91. typedef struct HWDevice {
  92. const char *name;
  93. enum AVHWDeviceType type;
  94. AVBufferRef *device_ref;
  95. } HWDevice;
  96. /* select an input stream for an output stream */
  97. typedef struct StreamMap {
  98. int disabled; /* 1 is this mapping is disabled by a negative map */
  99. int file_index;
  100. int stream_index;
  101. char *linklabel; /* name of an output link, for mapping lavfi outputs */
  102. } StreamMap;
  103. typedef struct OptionsContext {
  104. OptionGroup *g;
  105. /* input/output options */
  106. int64_t start_time;
  107. int64_t start_time_eof;
  108. int seek_timestamp;
  109. const char *format;
  110. SpecifierOptList codec_names;
  111. SpecifierOptList audio_ch_layouts;
  112. SpecifierOptList audio_channels;
  113. SpecifierOptList audio_sample_rate;
  114. SpecifierOptList frame_rates;
  115. SpecifierOptList max_frame_rates;
  116. SpecifierOptList frame_sizes;
  117. SpecifierOptList frame_pix_fmts;
  118. /* input options */
  119. int64_t input_ts_offset;
  120. int loop;
  121. int rate_emu;
  122. float readrate;
  123. double readrate_initial_burst;
  124. int accurate_seek;
  125. int thread_queue_size;
  126. int input_sync_ref;
  127. int find_stream_info;
  128. SpecifierOptList ts_scale;
  129. SpecifierOptList dump_attachment;
  130. SpecifierOptList hwaccels;
  131. SpecifierOptList hwaccel_devices;
  132. SpecifierOptList hwaccel_output_formats;
  133. SpecifierOptList autorotate;
  134. /* output options */
  135. StreamMap *stream_maps;
  136. int nb_stream_maps;
  137. const char **attachments;
  138. int nb_attachments;
  139. int chapters_input_file;
  140. int64_t recording_time;
  141. int64_t stop_time;
  142. int64_t limit_filesize;
  143. float mux_preload;
  144. float mux_max_delay;
  145. float shortest_buf_duration;
  146. int shortest;
  147. int bitexact;
  148. int video_disable;
  149. int audio_disable;
  150. int subtitle_disable;
  151. int data_disable;
  152. // keys are stream indices
  153. AVDictionary *streamid;
  154. SpecifierOptList metadata;
  155. SpecifierOptList max_frames;
  156. SpecifierOptList bitstream_filters;
  157. SpecifierOptList codec_tags;
  158. SpecifierOptList sample_fmts;
  159. SpecifierOptList qscale;
  160. SpecifierOptList forced_key_frames;
  161. SpecifierOptList fps_mode;
  162. SpecifierOptList force_fps;
  163. SpecifierOptList frame_aspect_ratios;
  164. SpecifierOptList display_rotations;
  165. SpecifierOptList display_hflips;
  166. SpecifierOptList display_vflips;
  167. SpecifierOptList rc_overrides;
  168. SpecifierOptList intra_matrices;
  169. SpecifierOptList inter_matrices;
  170. SpecifierOptList chroma_intra_matrices;
  171. #if FFMPEG_OPT_TOP
  172. SpecifierOptList top_field_first;
  173. #endif
  174. SpecifierOptList metadata_map;
  175. SpecifierOptList presets;
  176. SpecifierOptList copy_initial_nonkeyframes;
  177. SpecifierOptList copy_prior_start;
  178. SpecifierOptList filters;
  179. #if FFMPEG_OPT_FILTER_SCRIPT
  180. SpecifierOptList filter_scripts;
  181. #endif
  182. SpecifierOptList reinit_filters;
  183. SpecifierOptList fix_sub_duration;
  184. SpecifierOptList fix_sub_duration_heartbeat;
  185. SpecifierOptList canvas_sizes;
  186. SpecifierOptList pass;
  187. SpecifierOptList passlogfiles;
  188. SpecifierOptList max_muxing_queue_size;
  189. SpecifierOptList muxing_queue_data_threshold;
  190. SpecifierOptList guess_layout_max;
  191. SpecifierOptList apad;
  192. SpecifierOptList discard;
  193. SpecifierOptList disposition;
  194. SpecifierOptList program;
  195. SpecifierOptList stream_groups;
  196. SpecifierOptList time_bases;
  197. SpecifierOptList enc_time_bases;
  198. SpecifierOptList autoscale;
  199. SpecifierOptList bits_per_raw_sample;
  200. SpecifierOptList enc_stats_pre;
  201. SpecifierOptList enc_stats_post;
  202. SpecifierOptList mux_stats;
  203. SpecifierOptList enc_stats_pre_fmt;
  204. SpecifierOptList enc_stats_post_fmt;
  205. SpecifierOptList mux_stats_fmt;
  206. } OptionsContext;
  207. enum IFilterFlags {
  208. IFILTER_FLAG_AUTOROTATE = (1 << 0),
  209. IFILTER_FLAG_REINIT = (1 << 1),
  210. IFILTER_FLAG_CFR = (1 << 2),
  211. };
  212. typedef struct InputFilterOptions {
  213. int64_t trim_start_us;
  214. int64_t trim_end_us;
  215. uint8_t *name;
  216. /* When IFILTER_FLAG_CFR is set, the stream is guaranteed to be CFR with
  217. * this framerate.
  218. *
  219. * Otherwise, this is an estimate that should not be relied upon to be
  220. * accurate */
  221. AVRational framerate;
  222. int sub2video_width;
  223. int sub2video_height;
  224. // a combination of IFILTER_FLAG_*
  225. unsigned flags;
  226. AVFrame *fallback;
  227. } InputFilterOptions;
  228. enum OFilterFlags {
  229. OFILTER_FLAG_DISABLE_CONVERT = (1 << 0),
  230. // produce 24-bit audio
  231. OFILTER_FLAG_AUDIO_24BIT = (1 << 1),
  232. OFILTER_FLAG_AUTOSCALE = (1 << 2),
  233. };
  234. typedef struct OutputFilterOptions {
  235. // Caller-provided name for this output
  236. char *name;
  237. // Codec used for encoding, may be NULL
  238. const AVCodec *enc;
  239. // Overrides encoder pixel formats when set.
  240. const enum AVPixelFormat *pix_fmts;
  241. int64_t trim_start_us;
  242. int64_t trim_duration_us;
  243. int64_t ts_offset;
  244. /* Desired output timebase.
  245. * Numerator can be one of EncTimeBase values, or 0 when no preference.
  246. */
  247. AVRational output_tb;
  248. AVDictionary *sws_opts;
  249. AVDictionary *swr_opts;
  250. const char *nb_threads;
  251. // A combination of OFilterFlags.
  252. unsigned flags;
  253. int format;
  254. int width;
  255. int height;
  256. enum VideoSyncMethod vsync_method;
  257. int sample_rate;
  258. AVChannelLayout ch_layout;
  259. } OutputFilterOptions;
  260. typedef struct InputFilter {
  261. struct FilterGraph *graph;
  262. uint8_t *name;
  263. } InputFilter;
  264. typedef struct OutputFilter {
  265. const AVClass *class;
  266. struct FilterGraph *graph;
  267. uint8_t *name;
  268. /* for filters that are not yet bound to an output stream,
  269. * this stores the output linklabel, if any */
  270. int bound;
  271. uint8_t *linklabel;
  272. char *apad;
  273. enum AVMediaType type;
  274. atomic_uint_least64_t nb_frames_dup;
  275. atomic_uint_least64_t nb_frames_drop;
  276. } OutputFilter;
  277. typedef struct FilterGraph {
  278. const AVClass *class;
  279. int index;
  280. InputFilter **inputs;
  281. int nb_inputs;
  282. OutputFilter **outputs;
  283. int nb_outputs;
  284. } FilterGraph;
  285. enum DecoderFlags {
  286. DECODER_FLAG_FIX_SUB_DURATION = (1 << 0),
  287. // input timestamps are unreliable (guessed by demuxer)
  288. DECODER_FLAG_TS_UNRELIABLE = (1 << 1),
  289. // decoder should override timestamps by fixed framerate
  290. // from DecoderOpts.framerate
  291. DECODER_FLAG_FRAMERATE_FORCED = (1 << 2),
  292. #if FFMPEG_OPT_TOP
  293. DECODER_FLAG_TOP_FIELD_FIRST = (1 << 3),
  294. #endif
  295. DECODER_FLAG_SEND_END_TS = (1 << 4),
  296. // force bitexact decoding
  297. DECODER_FLAG_BITEXACT = (1 << 5),
  298. };
  299. typedef struct DecoderOpts {
  300. int flags;
  301. char *name;
  302. void *log_parent;
  303. const AVCodec *codec;
  304. const AVCodecParameters *par;
  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. AVRational time_base;
  311. // Either forced (when DECODER_FLAG_FRAMERATE_FORCED is set) or
  312. // estimated (otherwise) video framerate.
  313. AVRational framerate;
  314. } DecoderOpts;
  315. typedef struct Decoder {
  316. const AVClass *class;
  317. enum AVMediaType type;
  318. const uint8_t *subtitle_header;
  319. int subtitle_header_size;
  320. // number of frames/samples retrieved from the decoder
  321. uint64_t frames_decoded;
  322. uint64_t samples_decoded;
  323. uint64_t decode_errors;
  324. } Decoder;
  325. typedef struct InputStream {
  326. const AVClass *class;
  327. /* parent source */
  328. struct InputFile *file;
  329. int index;
  330. AVStream *st;
  331. int user_set_discard;
  332. /**
  333. * Codec parameters - to be used by the decoding/streamcopy code.
  334. * st->codecpar should not be accessed, because it may be modified
  335. * concurrently by the demuxing thread.
  336. */
  337. AVCodecParameters *par;
  338. Decoder *decoder;
  339. const AVCodec *dec;
  340. /* framerate forced with -r */
  341. AVRational framerate;
  342. #if FFMPEG_OPT_TOP
  343. int top_field_first;
  344. #endif
  345. int fix_sub_duration;
  346. /* decoded data from this stream goes into all those filters
  347. * currently video and audio only */
  348. InputFilter **filters;
  349. int nb_filters;
  350. /*
  351. * Output targets that do not go through lavfi, i.e. subtitles or
  352. * streamcopy. Those two cases are distinguished by the OutputStream
  353. * having an encoder or not.
  354. */
  355. struct OutputStream **outputs;
  356. int nb_outputs;
  357. } InputStream;
  358. typedef struct InputFile {
  359. const AVClass *class;
  360. int index;
  361. AVFormatContext *ctx;
  362. int64_t input_ts_offset;
  363. int input_sync_ref;
  364. /**
  365. * Effective format start time based on enabled streams.
  366. */
  367. int64_t start_time_effective;
  368. int64_t ts_offset;
  369. /* user-specified start time in AV_TIME_BASE or AV_NOPTS_VALUE */
  370. int64_t start_time;
  371. /* streams that ffmpeg is aware of;
  372. * there may be extra streams in ctx that are not mapped to an InputStream
  373. * if new streams appear dynamically during demuxing */
  374. InputStream **streams;
  375. int nb_streams;
  376. } InputFile;
  377. enum forced_keyframes_const {
  378. FKF_N,
  379. FKF_N_FORCED,
  380. FKF_PREV_FORCED_N,
  381. FKF_PREV_FORCED_T,
  382. FKF_T,
  383. FKF_NB
  384. };
  385. #define ABORT_ON_FLAG_EMPTY_OUTPUT (1 << 0)
  386. #define ABORT_ON_FLAG_EMPTY_OUTPUT_STREAM (1 << 1)
  387. enum EncStatsType {
  388. ENC_STATS_LITERAL = 0,
  389. ENC_STATS_FILE_IDX,
  390. ENC_STATS_STREAM_IDX,
  391. ENC_STATS_FRAME_NUM,
  392. ENC_STATS_FRAME_NUM_IN,
  393. ENC_STATS_TIMEBASE,
  394. ENC_STATS_TIMEBASE_IN,
  395. ENC_STATS_PTS,
  396. ENC_STATS_PTS_TIME,
  397. ENC_STATS_PTS_IN,
  398. ENC_STATS_PTS_TIME_IN,
  399. ENC_STATS_DTS,
  400. ENC_STATS_DTS_TIME,
  401. ENC_STATS_SAMPLE_NUM,
  402. ENC_STATS_NB_SAMPLES,
  403. ENC_STATS_PKT_SIZE,
  404. ENC_STATS_BITRATE,
  405. ENC_STATS_AVG_BITRATE,
  406. ENC_STATS_KEYFRAME,
  407. };
  408. typedef struct EncStatsComponent {
  409. enum EncStatsType type;
  410. uint8_t *str;
  411. size_t str_len;
  412. } EncStatsComponent;
  413. typedef struct EncStats {
  414. EncStatsComponent *components;
  415. int nb_components;
  416. AVIOContext *io;
  417. pthread_mutex_t lock;
  418. int lock_initialized;
  419. } EncStats;
  420. extern const char *const forced_keyframes_const_names[];
  421. typedef enum {
  422. ENCODER_FINISHED = 1,
  423. MUXER_FINISHED = 2,
  424. } OSTFinished ;
  425. enum {
  426. KF_FORCE_SOURCE = 1,
  427. #if FFMPEG_OPT_FORCE_KF_SOURCE_NO_DROP
  428. KF_FORCE_SOURCE_NO_DROP = 2,
  429. #endif
  430. };
  431. typedef struct KeyframeForceCtx {
  432. int type;
  433. int64_t ref_pts;
  434. // timestamps of the forced keyframes, in AV_TIME_BASE_Q
  435. int64_t *pts;
  436. int nb_pts;
  437. int index;
  438. AVExpr *pexpr;
  439. double expr_const_values[FKF_NB];
  440. int dropped_keyframe;
  441. } KeyframeForceCtx;
  442. typedef struct Encoder Encoder;
  443. typedef struct OutputStream {
  444. const AVClass *class;
  445. enum AVMediaType type;
  446. /* parent muxer */
  447. struct OutputFile *file;
  448. int index; /* stream index in the output file */
  449. /**
  450. * Codec parameters for packets submitted to the muxer (i.e. before
  451. * bitstream filtering, if any).
  452. */
  453. AVCodecParameters *par_in;
  454. /* input stream that is the source for this output stream;
  455. * may be NULL for streams with no well-defined source, e.g.
  456. * attachments or outputs from complex filtergraphs */
  457. InputStream *ist;
  458. AVStream *st; /* stream in the output file */
  459. Encoder *enc;
  460. AVCodecContext *enc_ctx;
  461. /* video only */
  462. AVRational frame_rate;
  463. AVRational max_frame_rate;
  464. int force_fps;
  465. #if FFMPEG_OPT_TOP
  466. int top_field_first;
  467. #endif
  468. int bitexact;
  469. int bits_per_raw_sample;
  470. AVRational frame_aspect_ratio;
  471. KeyframeForceCtx kf;
  472. char *logfile_prefix;
  473. FILE *logfile;
  474. // simple filtergraph feeding this stream, if any
  475. FilterGraph *fg_simple;
  476. OutputFilter *filter;
  477. AVDictionary *encoder_opts;
  478. char *attachment_filename;
  479. /* stats */
  480. // number of packets send to the muxer
  481. atomic_uint_least64_t packets_written;
  482. // number of frames/samples sent to the encoder
  483. uint64_t frames_encoded;
  484. uint64_t samples_encoded;
  485. /* packet quality factor */
  486. atomic_int quality;
  487. EncStats enc_stats_pre;
  488. EncStats enc_stats_post;
  489. /*
  490. * bool on whether this stream should be utilized for splitting
  491. * subtitles utilizing fix_sub_duration at random access points.
  492. */
  493. unsigned int fix_sub_duration_heartbeat;
  494. } OutputStream;
  495. typedef struct OutputFile {
  496. const AVClass *class;
  497. int index;
  498. const char *url;
  499. OutputStream **streams;
  500. int nb_streams;
  501. int64_t recording_time; ///< desired length of the resulting file in microseconds == AV_TIME_BASE units
  502. int64_t start_time; ///< start time in microseconds == AV_TIME_BASE units
  503. int bitexact;
  504. } OutputFile;
  505. // optionally attached as opaque_ref to decoded AVFrames
  506. typedef struct FrameData {
  507. // demuxer-estimated dts in AV_TIME_BASE_Q,
  508. // to be used when real dts is missing
  509. int64_t dts_est;
  510. // properties that come from the decoder
  511. struct {
  512. uint64_t frame_num;
  513. int64_t pts;
  514. AVRational tb;
  515. } dec;
  516. AVRational frame_rate_filter;
  517. int bits_per_raw_sample;
  518. int64_t wallclock[LATENCY_PROBE_NB];
  519. AVCodecParameters *par_enc;
  520. } FrameData;
  521. extern InputFile **input_files;
  522. extern int nb_input_files;
  523. extern OutputFile **output_files;
  524. extern int nb_output_files;
  525. // complex filtergraphs
  526. extern FilterGraph **filtergraphs;
  527. extern int nb_filtergraphs;
  528. // standalone decoders (not tied to demuxed streams)
  529. extern Decoder **decoders;
  530. extern int nb_decoders;
  531. extern char *vstats_filename;
  532. extern float dts_delta_threshold;
  533. extern float dts_error_threshold;
  534. extern enum VideoSyncMethod video_sync_method;
  535. extern float frame_drop_threshold;
  536. extern int do_benchmark;
  537. extern int do_benchmark_all;
  538. extern int do_hex_dump;
  539. extern int do_pkt_dump;
  540. extern int copy_ts;
  541. extern int start_at_zero;
  542. extern int copy_tb;
  543. extern int debug_ts;
  544. extern int exit_on_error;
  545. extern int abort_on_flags;
  546. extern int print_stats;
  547. extern int64_t stats_period;
  548. extern int stdin_interaction;
  549. extern AVIOContext *progress_avio;
  550. extern float max_error_rate;
  551. extern char *filter_nbthreads;
  552. extern int filter_complex_nbthreads;
  553. extern int vstats_version;
  554. extern int auto_conversion_filters;
  555. extern const AVIOInterruptCB int_cb;
  556. extern const OptionDef options[];
  557. extern HWDevice *filter_hw_device;
  558. extern atomic_uint nb_output_dumped;
  559. extern int ignore_unknown_streams;
  560. extern int copy_unknown_streams;
  561. extern int recast_media;
  562. extern FILE *vstats_file;
  563. void term_init(void);
  564. void term_exit(void);
  565. void show_usage(void);
  566. int assert_file_overwrite(const char *filename);
  567. AVDictionary *strip_specifiers(const AVDictionary *dict);
  568. int find_codec(void *logctx, const char *name,
  569. enum AVMediaType type, int encoder, const AVCodec **codec);
  570. int parse_and_set_vsync(const char *arg, int *vsync_var, int file_idx, int st_idx, int is_global);
  571. int filtergraph_is_simple(const FilterGraph *fg);
  572. int init_simple_filtergraph(InputStream *ist, OutputStream *ost,
  573. char *graph_desc,
  574. Scheduler *sch, unsigned sch_idx_enc,
  575. const OutputFilterOptions *opts);
  576. int fg_finalise_bindings(void);
  577. /**
  578. * Get our axiliary frame data attached to the frame, allocating it
  579. * if needed.
  580. */
  581. FrameData *frame_data(AVFrame *frame);
  582. const FrameData *frame_data_c(AVFrame *frame);
  583. FrameData *packet_data (AVPacket *pkt);
  584. const FrameData *packet_data_c(AVPacket *pkt);
  585. int ofilter_bind_ost(OutputFilter *ofilter, OutputStream *ost,
  586. unsigned sched_idx_enc,
  587. const OutputFilterOptions *opts);
  588. /**
  589. * Create a new filtergraph in the global filtergraph list.
  590. *
  591. * @param graph_desc Graph description; an av_malloc()ed string, filtergraph
  592. * takes ownership of it.
  593. */
  594. int fg_create(FilterGraph **pfg, char *graph_desc, Scheduler *sch);
  595. void fg_free(FilterGraph **pfg);
  596. void fg_send_command(FilterGraph *fg, double time, const char *target,
  597. const char *command, const char *arg, int all_filters);
  598. int ffmpeg_parse_options(int argc, char **argv, Scheduler *sch);
  599. void enc_stats_write(OutputStream *ost, EncStats *es,
  600. const AVFrame *frame, const AVPacket *pkt,
  601. uint64_t frame_num);
  602. HWDevice *hw_device_get_by_name(const char *name);
  603. HWDevice *hw_device_get_by_type(enum AVHWDeviceType type);
  604. int hw_device_init_from_string(const char *arg, HWDevice **dev);
  605. int hw_device_init_from_type(enum AVHWDeviceType type,
  606. const char *device,
  607. HWDevice **dev_out);
  608. void hw_device_free_all(void);
  609. /**
  610. * Get a hardware device to be used with this filtergraph.
  611. * The returned reference is owned by the callee, the caller
  612. * must ref it explicitly for long-term use.
  613. */
  614. AVBufferRef *hw_device_for_filter(void);
  615. /**
  616. * Create a standalone decoder.
  617. */
  618. int dec_create(const OptionsContext *o, const char *arg, Scheduler *sch);
  619. /**
  620. * @param dec_opts Dictionary filled with decoder options. Its ownership
  621. * is transferred to the decoder.
  622. * @param param_out If non-NULL, media properties after opening the decoder
  623. * are written here.
  624. *
  625. * @retval ">=0" non-negative scheduler index on success
  626. * @retval "<0" an error code on failure
  627. */
  628. int dec_init(Decoder **pdec, Scheduler *sch,
  629. AVDictionary **dec_opts, const DecoderOpts *o,
  630. AVFrame *param_out);
  631. void dec_free(Decoder **pdec);
  632. /*
  633. * Called by filters to connect decoder's output to given filtergraph input.
  634. *
  635. * @param opts filtergraph input options, to be filled by this function
  636. */
  637. int dec_filter_add(Decoder *dec, InputFilter *ifilter, InputFilterOptions *opts);
  638. int enc_alloc(Encoder **penc, const AVCodec *codec,
  639. Scheduler *sch, unsigned sch_idx);
  640. void enc_free(Encoder **penc);
  641. int enc_open(void *opaque, const AVFrame *frame);
  642. int enc_loopback(Encoder *enc);
  643. /*
  644. * Initialize muxing state for the given stream, should be called
  645. * after the codec/streamcopy setup has been done.
  646. *
  647. * Open the muxer once all the streams have been initialized.
  648. */
  649. int of_stream_init(OutputFile *of, OutputStream *ost);
  650. int of_write_trailer(OutputFile *of);
  651. int of_open(const OptionsContext *o, const char *filename, Scheduler *sch);
  652. void of_free(OutputFile **pof);
  653. void of_enc_stats_close(void);
  654. int64_t of_filesize(OutputFile *of);
  655. int ifile_open(const OptionsContext *o, const char *filename, Scheduler *sch);
  656. void ifile_close(InputFile **f);
  657. int ist_output_add(InputStream *ist, OutputStream *ost);
  658. int ist_filter_add(InputStream *ist, InputFilter *ifilter, int is_simple,
  659. InputFilterOptions *opts);
  660. /**
  661. * Find an unused input stream of given type.
  662. */
  663. InputStream *ist_find_unused(enum AVMediaType type);
  664. /* iterate over all input streams in all input files;
  665. * pass NULL to start iteration */
  666. InputStream *ist_iter(InputStream *prev);
  667. /* iterate over all output streams in all output files;
  668. * pass NULL to start iteration */
  669. OutputStream *ost_iter(OutputStream *prev);
  670. void update_benchmark(const char *fmt, ...);
  671. #define SPECIFIER_OPT_FMT_str "%s"
  672. #define SPECIFIER_OPT_FMT_i "%i"
  673. #define SPECIFIER_OPT_FMT_i64 "%"PRId64
  674. #define SPECIFIER_OPT_FMT_ui64 "%"PRIu64
  675. #define SPECIFIER_OPT_FMT_f "%f"
  676. #define SPECIFIER_OPT_FMT_dbl "%lf"
  677. #define WARN_MULTIPLE_OPT_USAGE(optname, type, idx, st)\
  678. {\
  679. char namestr[128] = "";\
  680. const SpecifierOpt *so = &o->optname.opt[idx];\
  681. const char *spec = so->specifier && so->specifier[0] ? so->specifier : "";\
  682. snprintf(namestr, sizeof(namestr), "-%s", o->optname.opt_canon->name);\
  683. if (o->optname.opt_canon->flags & OPT_HAS_ALT) {\
  684. const char * const *names_alt = o->optname.opt_canon->u1.names_alt;\
  685. for (int _i = 0; names_alt[_i]; _i++)\
  686. av_strlcatf(namestr, sizeof(namestr), "/-%s", names_alt[_i]);\
  687. }\
  688. 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",\
  689. namestr, st->index, o->optname.opt_canon->name, spec[0] ? ":" : "", spec, so->u.type);\
  690. }
  691. #define MATCH_PER_STREAM_OPT_CLEAN(name, type, outvar, fmtctx, st, clean)\
  692. {\
  693. int _ret, _matches = 0, _match_idx;\
  694. for (int _i = 0; _i < o->name.nb_opt; _i++) {\
  695. char *spec = o->name.opt[_i].specifier;\
  696. if ((_ret = check_stream_specifier(fmtctx, st, spec)) > 0) {\
  697. outvar = o->name.opt[_i].u.type;\
  698. _match_idx = _i;\
  699. _matches++;\
  700. } else if (_ret < 0)\
  701. clean;\
  702. }\
  703. if (_matches > 1 && o->name.opt_canon)\
  704. WARN_MULTIPLE_OPT_USAGE(name, type, _match_idx, st);\
  705. }
  706. #define MATCH_PER_STREAM_OPT(name, type, outvar, fmtctx, st)\
  707. {\
  708. MATCH_PER_STREAM_OPT_CLEAN(name, type, outvar, fmtctx, st, return _ret)\
  709. }
  710. const char *opt_match_per_type_str(const SpecifierOptList *sol,
  711. char mediatype);
  712. int muxer_thread(void *arg);
  713. int encoder_thread(void *arg);
  714. #endif /* FFTOOLS_FFMPEG_H */