ffmpeg.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  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. typedef struct InputFilter {
  229. struct FilterGraph *graph;
  230. uint8_t *name;
  231. } InputFilter;
  232. typedef struct OutputFilter {
  233. struct OutputStream *ost;
  234. struct FilterGraph *graph;
  235. uint8_t *name;
  236. /* for filters that are not yet bound to an output stream,
  237. * this stores the output linklabel, if any */
  238. uint8_t *linklabel;
  239. enum AVMediaType type;
  240. atomic_uint_least64_t nb_frames_dup;
  241. atomic_uint_least64_t nb_frames_drop;
  242. } OutputFilter;
  243. typedef struct FilterGraph {
  244. const AVClass *class;
  245. int index;
  246. InputFilter **inputs;
  247. int nb_inputs;
  248. OutputFilter **outputs;
  249. int nb_outputs;
  250. } FilterGraph;
  251. enum DecoderFlags {
  252. DECODER_FLAG_FIX_SUB_DURATION = (1 << 0),
  253. // input timestamps are unreliable (guessed by demuxer)
  254. DECODER_FLAG_TS_UNRELIABLE = (1 << 1),
  255. // decoder should override timestamps by fixed framerate
  256. // from DecoderOpts.framerate
  257. DECODER_FLAG_FRAMERATE_FORCED = (1 << 2),
  258. #if FFMPEG_OPT_TOP
  259. DECODER_FLAG_TOP_FIELD_FIRST = (1 << 3),
  260. #endif
  261. DECODER_FLAG_SEND_END_TS = (1 << 4),
  262. };
  263. typedef struct DecoderOpts {
  264. int flags;
  265. char *name;
  266. void *log_parent;
  267. const AVCodec *codec;
  268. const AVCodecParameters *par;
  269. /* hwaccel options */
  270. enum HWAccelID hwaccel_id;
  271. enum AVHWDeviceType hwaccel_device_type;
  272. char *hwaccel_device;
  273. enum AVPixelFormat hwaccel_output_format;
  274. AVRational time_base;
  275. // Either forced (when DECODER_FLAG_FRAMERATE_FORCED is set) or
  276. // estimated (otherwise) video framerate.
  277. AVRational framerate;
  278. } DecoderOpts;
  279. typedef struct Decoder {
  280. const AVClass *class;
  281. enum AVMediaType type;
  282. const uint8_t *subtitle_header;
  283. int subtitle_header_size;
  284. // number of frames/samples retrieved from the decoder
  285. uint64_t frames_decoded;
  286. uint64_t samples_decoded;
  287. uint64_t decode_errors;
  288. } Decoder;
  289. typedef struct InputStream {
  290. const AVClass *class;
  291. /* parent source */
  292. struct InputFile *file;
  293. int index;
  294. AVStream *st;
  295. int user_set_discard;
  296. /**
  297. * Codec parameters - to be used by the decoding/streamcopy code.
  298. * st->codecpar should not be accessed, because it may be modified
  299. * concurrently by the demuxing thread.
  300. */
  301. AVCodecParameters *par;
  302. Decoder *decoder;
  303. const AVCodec *dec;
  304. /* framerate forced with -r */
  305. AVRational framerate;
  306. #if FFMPEG_OPT_TOP
  307. int top_field_first;
  308. #endif
  309. int autorotate;
  310. int fix_sub_duration;
  311. /* decoded data from this stream goes into all those filters
  312. * currently video and audio only */
  313. InputFilter **filters;
  314. int nb_filters;
  315. /*
  316. * Output targets that do not go through lavfi, i.e. subtitles or
  317. * streamcopy. Those two cases are distinguished by the OutputStream
  318. * having an encoder or not.
  319. */
  320. struct OutputStream **outputs;
  321. int nb_outputs;
  322. } InputStream;
  323. typedef struct InputFile {
  324. const AVClass *class;
  325. int index;
  326. AVFormatContext *ctx;
  327. int64_t input_ts_offset;
  328. int input_sync_ref;
  329. /**
  330. * Effective format start time based on enabled streams.
  331. */
  332. int64_t start_time_effective;
  333. int64_t ts_offset;
  334. /* user-specified start time in AV_TIME_BASE or AV_NOPTS_VALUE */
  335. int64_t start_time;
  336. /* streams that ffmpeg is aware of;
  337. * there may be extra streams in ctx that are not mapped to an InputStream
  338. * if new streams appear dynamically during demuxing */
  339. InputStream **streams;
  340. int nb_streams;
  341. } InputFile;
  342. enum forced_keyframes_const {
  343. FKF_N,
  344. FKF_N_FORCED,
  345. FKF_PREV_FORCED_N,
  346. FKF_PREV_FORCED_T,
  347. FKF_T,
  348. FKF_NB
  349. };
  350. #define ABORT_ON_FLAG_EMPTY_OUTPUT (1 << 0)
  351. #define ABORT_ON_FLAG_EMPTY_OUTPUT_STREAM (1 << 1)
  352. enum EncStatsType {
  353. ENC_STATS_LITERAL = 0,
  354. ENC_STATS_FILE_IDX,
  355. ENC_STATS_STREAM_IDX,
  356. ENC_STATS_FRAME_NUM,
  357. ENC_STATS_FRAME_NUM_IN,
  358. ENC_STATS_TIMEBASE,
  359. ENC_STATS_TIMEBASE_IN,
  360. ENC_STATS_PTS,
  361. ENC_STATS_PTS_TIME,
  362. ENC_STATS_PTS_IN,
  363. ENC_STATS_PTS_TIME_IN,
  364. ENC_STATS_DTS,
  365. ENC_STATS_DTS_TIME,
  366. ENC_STATS_SAMPLE_NUM,
  367. ENC_STATS_NB_SAMPLES,
  368. ENC_STATS_PKT_SIZE,
  369. ENC_STATS_BITRATE,
  370. ENC_STATS_AVG_BITRATE,
  371. ENC_STATS_KEYFRAME,
  372. };
  373. typedef struct EncStatsComponent {
  374. enum EncStatsType type;
  375. uint8_t *str;
  376. size_t str_len;
  377. } EncStatsComponent;
  378. typedef struct EncStats {
  379. EncStatsComponent *components;
  380. int nb_components;
  381. AVIOContext *io;
  382. pthread_mutex_t lock;
  383. int lock_initialized;
  384. } EncStats;
  385. extern const char *const forced_keyframes_const_names[];
  386. typedef enum {
  387. ENCODER_FINISHED = 1,
  388. MUXER_FINISHED = 2,
  389. } OSTFinished ;
  390. enum {
  391. KF_FORCE_SOURCE = 1,
  392. #if FFMPEG_OPT_FORCE_KF_SOURCE_NO_DROP
  393. KF_FORCE_SOURCE_NO_DROP = 2,
  394. #endif
  395. };
  396. typedef struct KeyframeForceCtx {
  397. int type;
  398. int64_t ref_pts;
  399. // timestamps of the forced keyframes, in AV_TIME_BASE_Q
  400. int64_t *pts;
  401. int nb_pts;
  402. int index;
  403. AVExpr *pexpr;
  404. double expr_const_values[FKF_NB];
  405. int dropped_keyframe;
  406. } KeyframeForceCtx;
  407. typedef struct Encoder Encoder;
  408. typedef struct OutputStream {
  409. const AVClass *class;
  410. enum AVMediaType type;
  411. /* parent muxer */
  412. struct OutputFile *file;
  413. int index; /* stream index in the output file */
  414. /**
  415. * Codec parameters for packets submitted to the muxer (i.e. before
  416. * bitstream filtering, if any).
  417. */
  418. AVCodecParameters *par_in;
  419. /* input stream that is the source for this output stream;
  420. * may be NULL for streams with no well-defined source, e.g.
  421. * attachments or outputs from complex filtergraphs */
  422. InputStream *ist;
  423. AVStream *st; /* stream in the output file */
  424. AVRational enc_timebase;
  425. Encoder *enc;
  426. AVCodecContext *enc_ctx;
  427. /* video only */
  428. AVRational frame_rate;
  429. AVRational max_frame_rate;
  430. enum VideoSyncMethod vsync_method;
  431. int is_cfr;
  432. int force_fps;
  433. #if FFMPEG_OPT_TOP
  434. int top_field_first;
  435. #endif
  436. int autoscale;
  437. int bitexact;
  438. int bits_per_raw_sample;
  439. AVRational frame_aspect_ratio;
  440. KeyframeForceCtx kf;
  441. char *logfile_prefix;
  442. FILE *logfile;
  443. OutputFilter *filter;
  444. AVDictionary *encoder_opts;
  445. AVDictionary *sws_dict;
  446. AVDictionary *swr_opts;
  447. char *apad;
  448. char *attachment_filename;
  449. int keep_pix_fmt;
  450. /* stats */
  451. // number of packets send to the muxer
  452. atomic_uint_least64_t packets_written;
  453. // number of frames/samples sent to the encoder
  454. uint64_t frames_encoded;
  455. uint64_t samples_encoded;
  456. /* packet quality factor */
  457. atomic_int quality;
  458. EncStats enc_stats_pre;
  459. EncStats enc_stats_post;
  460. /*
  461. * bool on whether this stream should be utilized for splitting
  462. * subtitles utilizing fix_sub_duration at random access points.
  463. */
  464. unsigned int fix_sub_duration_heartbeat;
  465. } OutputStream;
  466. typedef struct OutputFile {
  467. const AVClass *class;
  468. int index;
  469. const AVOutputFormat *format;
  470. const char *url;
  471. OutputStream **streams;
  472. int nb_streams;
  473. int64_t recording_time; ///< desired length of the resulting file in microseconds == AV_TIME_BASE units
  474. int64_t start_time; ///< start time in microseconds == AV_TIME_BASE units
  475. int shortest;
  476. int bitexact;
  477. } OutputFile;
  478. // optionally attached as opaque_ref to decoded AVFrames
  479. typedef struct FrameData {
  480. // demuxer-estimated dts in AV_TIME_BASE_Q,
  481. // to be used when real dts is missing
  482. int64_t dts_est;
  483. // properties that come from the decoder
  484. struct {
  485. uint64_t frame_num;
  486. int64_t pts;
  487. AVRational tb;
  488. } dec;
  489. AVRational frame_rate_filter;
  490. int bits_per_raw_sample;
  491. int64_t wallclock[LATENCY_PROBE_NB];
  492. AVCodecParameters *par_enc;
  493. } FrameData;
  494. extern InputFile **input_files;
  495. extern int nb_input_files;
  496. extern OutputFile **output_files;
  497. extern int nb_output_files;
  498. extern FilterGraph **filtergraphs;
  499. extern int nb_filtergraphs;
  500. // standalone decoders (not tied to demuxed streams)
  501. extern Decoder **decoders;
  502. extern int nb_decoders;
  503. extern char *vstats_filename;
  504. extern float dts_delta_threshold;
  505. extern float dts_error_threshold;
  506. extern enum VideoSyncMethod video_sync_method;
  507. extern float frame_drop_threshold;
  508. extern int do_benchmark;
  509. extern int do_benchmark_all;
  510. extern int do_hex_dump;
  511. extern int do_pkt_dump;
  512. extern int copy_ts;
  513. extern int start_at_zero;
  514. extern int copy_tb;
  515. extern int debug_ts;
  516. extern int exit_on_error;
  517. extern int abort_on_flags;
  518. extern int print_stats;
  519. extern int64_t stats_period;
  520. extern int stdin_interaction;
  521. extern AVIOContext *progress_avio;
  522. extern float max_error_rate;
  523. extern char *filter_nbthreads;
  524. extern int filter_complex_nbthreads;
  525. extern int vstats_version;
  526. extern int auto_conversion_filters;
  527. extern const AVIOInterruptCB int_cb;
  528. extern const OptionDef options[];
  529. extern HWDevice *filter_hw_device;
  530. extern atomic_uint nb_output_dumped;
  531. extern int ignore_unknown_streams;
  532. extern int copy_unknown_streams;
  533. extern int recast_media;
  534. extern FILE *vstats_file;
  535. void term_init(void);
  536. void term_exit(void);
  537. void show_usage(void);
  538. void remove_avoptions(AVDictionary **a, AVDictionary *b);
  539. int check_avoptions(AVDictionary *m);
  540. int assert_file_overwrite(const char *filename);
  541. AVDictionary *strip_specifiers(const AVDictionary *dict);
  542. int find_codec(void *logctx, const char *name,
  543. enum AVMediaType type, int encoder, const AVCodec **codec);
  544. int parse_and_set_vsync(const char *arg, int *vsync_var, int file_idx, int st_idx, int is_global);
  545. int filtergraph_is_simple(const FilterGraph *fg);
  546. int init_simple_filtergraph(InputStream *ist, OutputStream *ost,
  547. char *graph_desc,
  548. Scheduler *sch, unsigned sch_idx_enc);
  549. int fg_finalise_bindings(FilterGraph *fg);
  550. /**
  551. * Get our axiliary frame data attached to the frame, allocating it
  552. * if needed.
  553. */
  554. FrameData *frame_data(AVFrame *frame);
  555. const FrameData *frame_data_c(AVFrame *frame);
  556. FrameData *packet_data (AVPacket *pkt);
  557. const FrameData *packet_data_c(AVPacket *pkt);
  558. int ofilter_bind_ost(OutputFilter *ofilter, OutputStream *ost,
  559. unsigned sched_idx_enc);
  560. /**
  561. * Create a new filtergraph in the global filtergraph list.
  562. *
  563. * @param graph_desc Graph description; an av_malloc()ed string, filtergraph
  564. * takes ownership of it.
  565. */
  566. int fg_create(FilterGraph **pfg, char *graph_desc, Scheduler *sch);
  567. void fg_free(FilterGraph **pfg);
  568. void fg_send_command(FilterGraph *fg, double time, const char *target,
  569. const char *command, const char *arg, int all_filters);
  570. int ffmpeg_parse_options(int argc, char **argv, Scheduler *sch);
  571. void enc_stats_write(OutputStream *ost, EncStats *es,
  572. const AVFrame *frame, const AVPacket *pkt,
  573. uint64_t frame_num);
  574. HWDevice *hw_device_get_by_name(const char *name);
  575. HWDevice *hw_device_get_by_type(enum AVHWDeviceType type);
  576. int hw_device_init_from_string(const char *arg, HWDevice **dev);
  577. int hw_device_init_from_type(enum AVHWDeviceType type,
  578. const char *device,
  579. HWDevice **dev_out);
  580. void hw_device_free_all(void);
  581. /**
  582. * Get a hardware device to be used with this filtergraph.
  583. * The returned reference is owned by the callee, the caller
  584. * must ref it explicitly for long-term use.
  585. */
  586. AVBufferRef *hw_device_for_filter(void);
  587. /**
  588. * Create a standalone decoder.
  589. */
  590. int dec_create(const OptionsContext *o, const char *arg, Scheduler *sch);
  591. /**
  592. * @param dec_opts Dictionary filled with decoder options. Its ownership
  593. * is transferred to the decoder.
  594. * @param param_out If non-NULL, media properties after opening the decoder
  595. * are written here.
  596. *
  597. * @retval ">=0" non-negative scheduler index on success
  598. * @retval "<0" an error code on failure
  599. */
  600. int dec_init(Decoder **pdec, Scheduler *sch,
  601. AVDictionary **dec_opts, const DecoderOpts *o,
  602. AVFrame *param_out);
  603. void dec_free(Decoder **pdec);
  604. /*
  605. * Called by filters to connect decoder's output to given filtergraph input.
  606. *
  607. * @param opts filtergraph input options, to be filled by this function
  608. */
  609. int dec_filter_add(Decoder *dec, InputFilter *ifilter, InputFilterOptions *opts);
  610. int enc_alloc(Encoder **penc, const AVCodec *codec,
  611. Scheduler *sch, unsigned sch_idx);
  612. void enc_free(Encoder **penc);
  613. int enc_open(void *opaque, const AVFrame *frame);
  614. int enc_loopback(Encoder *enc);
  615. /*
  616. * Initialize muxing state for the given stream, should be called
  617. * after the codec/streamcopy setup has been done.
  618. *
  619. * Open the muxer once all the streams have been initialized.
  620. */
  621. int of_stream_init(OutputFile *of, OutputStream *ost);
  622. int of_write_trailer(OutputFile *of);
  623. int of_open(const OptionsContext *o, const char *filename, Scheduler *sch);
  624. void of_free(OutputFile **pof);
  625. void of_enc_stats_close(void);
  626. int64_t of_filesize(OutputFile *of);
  627. int ifile_open(const OptionsContext *o, const char *filename, Scheduler *sch);
  628. void ifile_close(InputFile **f);
  629. int ist_output_add(InputStream *ist, OutputStream *ost);
  630. int ist_filter_add(InputStream *ist, InputFilter *ifilter, int is_simple,
  631. InputFilterOptions *opts);
  632. /**
  633. * Find an unused input stream of given type.
  634. */
  635. InputStream *ist_find_unused(enum AVMediaType type);
  636. /* iterate over all input streams in all input files;
  637. * pass NULL to start iteration */
  638. InputStream *ist_iter(InputStream *prev);
  639. /* iterate over all output streams in all output files;
  640. * pass NULL to start iteration */
  641. OutputStream *ost_iter(OutputStream *prev);
  642. void update_benchmark(const char *fmt, ...);
  643. #define SPECIFIER_OPT_FMT_str "%s"
  644. #define SPECIFIER_OPT_FMT_i "%i"
  645. #define SPECIFIER_OPT_FMT_i64 "%"PRId64
  646. #define SPECIFIER_OPT_FMT_ui64 "%"PRIu64
  647. #define SPECIFIER_OPT_FMT_f "%f"
  648. #define SPECIFIER_OPT_FMT_dbl "%lf"
  649. #define WARN_MULTIPLE_OPT_USAGE(optname, type, idx, st)\
  650. {\
  651. char namestr[128] = "";\
  652. const SpecifierOpt *so = &o->optname.opt[idx];\
  653. const char *spec = so->specifier && so->specifier[0] ? so->specifier : "";\
  654. snprintf(namestr, sizeof(namestr), "-%s", o->optname.opt_canon->name);\
  655. if (o->optname.opt_canon->flags & OPT_HAS_ALT) {\
  656. const char * const *names_alt = o->optname.opt_canon->u1.names_alt;\
  657. for (int _i = 0; names_alt[_i]; _i++)\
  658. av_strlcatf(namestr, sizeof(namestr), "/-%s", names_alt[_i]);\
  659. }\
  660. 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",\
  661. namestr, st->index, o->optname.opt_canon->name, spec[0] ? ":" : "", spec, so->u.type);\
  662. }
  663. #define MATCH_PER_STREAM_OPT(name, type, outvar, fmtctx, st)\
  664. {\
  665. int _ret, _matches = 0, _match_idx;\
  666. for (int _i = 0; _i < o->name.nb_opt; _i++) {\
  667. char *spec = o->name.opt[_i].specifier;\
  668. if ((_ret = check_stream_specifier(fmtctx, st, spec)) > 0) {\
  669. outvar = o->name.opt[_i].u.type;\
  670. _match_idx = _i;\
  671. _matches++;\
  672. } else if (_ret < 0)\
  673. return _ret;\
  674. }\
  675. if (_matches > 1 && o->name.opt_canon)\
  676. WARN_MULTIPLE_OPT_USAGE(name, type, _match_idx, st);\
  677. }
  678. const char *opt_match_per_type_str(const SpecifierOptList *sol,
  679. char mediatype);
  680. int muxer_thread(void *arg);
  681. int encoder_thread(void *arg);
  682. #endif /* FFTOOLS_FFMPEG_H */