avfilter.h 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168
  1. /*
  2. * filter layer
  3. * Copyright (c) 2007 Bobby Bingham
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #ifndef AVFILTER_AVFILTER_H
  22. #define AVFILTER_AVFILTER_H
  23. /**
  24. * @file
  25. * @ingroup lavfi
  26. * Main libavfilter public API header
  27. */
  28. /**
  29. * @defgroup lavfi libavfilter
  30. * Graph-based frame editing library.
  31. *
  32. * @{
  33. */
  34. #include <stddef.h>
  35. #include "libavutil/attributes.h"
  36. #include "libavutil/avutil.h"
  37. #include "libavutil/buffer.h"
  38. #include "libavutil/dict.h"
  39. #include "libavutil/frame.h"
  40. #include "libavutil/log.h"
  41. #include "libavutil/samplefmt.h"
  42. #include "libavutil/pixfmt.h"
  43. #include "libavutil/rational.h"
  44. #include "libavfilter/version.h"
  45. /**
  46. * Return the LIBAVFILTER_VERSION_INT constant.
  47. */
  48. unsigned avfilter_version(void);
  49. /**
  50. * Return the libavfilter build-time configuration.
  51. */
  52. const char *avfilter_configuration(void);
  53. /**
  54. * Return the libavfilter license.
  55. */
  56. const char *avfilter_license(void);
  57. typedef struct AVFilterContext AVFilterContext;
  58. typedef struct AVFilterLink AVFilterLink;
  59. typedef struct AVFilterPad AVFilterPad;
  60. typedef struct AVFilterFormats AVFilterFormats;
  61. /**
  62. * Get the number of elements in a NULL-terminated array of AVFilterPads (e.g.
  63. * AVFilter.inputs/outputs).
  64. */
  65. int avfilter_pad_count(const AVFilterPad *pads);
  66. /**
  67. * Get the name of an AVFilterPad.
  68. *
  69. * @param pads an array of AVFilterPads
  70. * @param pad_idx index of the pad in the array it; is the caller's
  71. * responsibility to ensure the index is valid
  72. *
  73. * @return name of the pad_idx'th pad in pads
  74. */
  75. const char *avfilter_pad_get_name(const AVFilterPad *pads, int pad_idx);
  76. /**
  77. * Get the type of an AVFilterPad.
  78. *
  79. * @param pads an array of AVFilterPads
  80. * @param pad_idx index of the pad in the array; it is the caller's
  81. * responsibility to ensure the index is valid
  82. *
  83. * @return type of the pad_idx'th pad in pads
  84. */
  85. enum AVMediaType avfilter_pad_get_type(const AVFilterPad *pads, int pad_idx);
  86. /**
  87. * The number of the filter inputs is not determined just by AVFilter.inputs.
  88. * The filter might add additional inputs during initialization depending on the
  89. * options supplied to it.
  90. */
  91. #define AVFILTER_FLAG_DYNAMIC_INPUTS (1 << 0)
  92. /**
  93. * The number of the filter outputs is not determined just by AVFilter.outputs.
  94. * The filter might add additional outputs during initialization depending on
  95. * the options supplied to it.
  96. */
  97. #define AVFILTER_FLAG_DYNAMIC_OUTPUTS (1 << 1)
  98. /**
  99. * The filter supports multithreading by splitting frames into multiple parts
  100. * and processing them concurrently.
  101. */
  102. #define AVFILTER_FLAG_SLICE_THREADS (1 << 2)
  103. /**
  104. * Some filters support a generic "enable" expression option that can be used
  105. * to enable or disable a filter in the timeline. Filters supporting this
  106. * option have this flag set. When the enable expression is false, the default
  107. * no-op filter_frame() function is called in place of the filter_frame()
  108. * callback defined on each input pad, thus the frame is passed unchanged to
  109. * the next filters.
  110. */
  111. #define AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC (1 << 16)
  112. /**
  113. * Same as AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC, except that the filter will
  114. * have its filter_frame() callback(s) called as usual even when the enable
  115. * expression is false. The filter will disable filtering within the
  116. * filter_frame() callback(s) itself, for example executing code depending on
  117. * the AVFilterContext->is_disabled value.
  118. */
  119. #define AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL (1 << 17)
  120. /**
  121. * Handy mask to test whether the filter supports or no the timeline feature
  122. * (internally or generically).
  123. */
  124. #define AVFILTER_FLAG_SUPPORT_TIMELINE (AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL)
  125. /**
  126. * Filter definition. This defines the pads a filter contains, and all the
  127. * callback functions used to interact with the filter.
  128. */
  129. typedef struct AVFilter {
  130. /**
  131. * Filter name. Must be non-NULL and unique among filters.
  132. */
  133. const char *name;
  134. /**
  135. * A description of the filter. May be NULL.
  136. *
  137. * You should use the NULL_IF_CONFIG_SMALL() macro to define it.
  138. */
  139. const char *description;
  140. /**
  141. * List of inputs, terminated by a zeroed element.
  142. *
  143. * NULL if there are no (static) inputs. Instances of filters with
  144. * AVFILTER_FLAG_DYNAMIC_INPUTS set may have more inputs than present in
  145. * this list.
  146. */
  147. const AVFilterPad *inputs;
  148. /**
  149. * List of outputs, terminated by a zeroed element.
  150. *
  151. * NULL if there are no (static) outputs. Instances of filters with
  152. * AVFILTER_FLAG_DYNAMIC_OUTPUTS set may have more outputs than present in
  153. * this list.
  154. */
  155. const AVFilterPad *outputs;
  156. /**
  157. * A class for the private data, used to declare filter private AVOptions.
  158. * This field is NULL for filters that do not declare any options.
  159. *
  160. * If this field is non-NULL, the first member of the filter private data
  161. * must be a pointer to AVClass, which will be set by libavfilter generic
  162. * code to this class.
  163. */
  164. const AVClass *priv_class;
  165. /**
  166. * A combination of AVFILTER_FLAG_*
  167. */
  168. int flags;
  169. /*****************************************************************
  170. * All fields below this line are not part of the public API. They
  171. * may not be used outside of libavfilter and can be changed and
  172. * removed at will.
  173. * New public fields should be added right above.
  174. *****************************************************************
  175. */
  176. /**
  177. * Filter pre-initialization function
  178. *
  179. * This callback will be called immediately after the filter context is
  180. * allocated, to allow allocating and initing sub-objects.
  181. *
  182. * If this callback is not NULL, the uninit callback will be called on
  183. * allocation failure.
  184. *
  185. * @return 0 on success,
  186. * AVERROR code on failure (but the code will be
  187. * dropped and treated as ENOMEM by the calling code)
  188. */
  189. int (*preinit)(AVFilterContext *ctx);
  190. /**
  191. * Filter initialization function.
  192. *
  193. * This callback will be called only once during the filter lifetime, after
  194. * all the options have been set, but before links between filters are
  195. * established and format negotiation is done.
  196. *
  197. * Basic filter initialization should be done here. Filters with dynamic
  198. * inputs and/or outputs should create those inputs/outputs here based on
  199. * provided options. No more changes to this filter's inputs/outputs can be
  200. * done after this callback.
  201. *
  202. * This callback must not assume that the filter links exist or frame
  203. * parameters are known.
  204. *
  205. * @ref AVFilter.uninit "uninit" is guaranteed to be called even if
  206. * initialization fails, so this callback does not have to clean up on
  207. * failure.
  208. *
  209. * @return 0 on success, a negative AVERROR on failure
  210. */
  211. int (*init)(AVFilterContext *ctx);
  212. /**
  213. * Should be set instead of @ref AVFilter.init "init" by the filters that
  214. * want to pass a dictionary of AVOptions to nested contexts that are
  215. * allocated during init.
  216. *
  217. * On return, the options dict should be freed and replaced with one that
  218. * contains all the options which could not be processed by this filter (or
  219. * with NULL if all the options were processed).
  220. *
  221. * Otherwise the semantics is the same as for @ref AVFilter.init "init".
  222. */
  223. int (*init_dict)(AVFilterContext *ctx, AVDictionary **options);
  224. /**
  225. * Filter uninitialization function.
  226. *
  227. * Called only once right before the filter is freed. Should deallocate any
  228. * memory held by the filter, release any buffer references, etc. It does
  229. * not need to deallocate the AVFilterContext.priv memory itself.
  230. *
  231. * This callback may be called even if @ref AVFilter.init "init" was not
  232. * called or failed, so it must be prepared to handle such a situation.
  233. */
  234. void (*uninit)(AVFilterContext *ctx);
  235. /**
  236. * Query formats supported by the filter on its inputs and outputs.
  237. *
  238. * This callback is called after the filter is initialized (so the inputs
  239. * and outputs are fixed), shortly before the format negotiation. This
  240. * callback may be called more than once.
  241. *
  242. * This callback must set AVFilterLink.out_formats on every input link and
  243. * AVFilterLink.in_formats on every output link to a list of pixel/sample
  244. * formats that the filter supports on that link. For audio links, this
  245. * filter must also set @ref AVFilterLink.in_samplerates "in_samplerates" /
  246. * @ref AVFilterLink.out_samplerates "out_samplerates" and
  247. * @ref AVFilterLink.in_channel_layouts "in_channel_layouts" /
  248. * @ref AVFilterLink.out_channel_layouts "out_channel_layouts" analogously.
  249. *
  250. * This callback may be NULL for filters with one input, in which case
  251. * libavfilter assumes that it supports all input formats and preserves
  252. * them on output.
  253. *
  254. * @return zero on success, a negative value corresponding to an
  255. * AVERROR code otherwise
  256. */
  257. int (*query_formats)(AVFilterContext *);
  258. int priv_size; ///< size of private data to allocate for the filter
  259. int flags_internal; ///< Additional flags for avfilter internal use only.
  260. /**
  261. * Used by the filter registration system. Must not be touched by any other
  262. * code.
  263. */
  264. struct AVFilter *next;
  265. /**
  266. * Make the filter instance process a command.
  267. *
  268. * @param cmd the command to process, for handling simplicity all commands must be alphanumeric only
  269. * @param arg the argument for the command
  270. * @param res a buffer with size res_size where the filter(s) can return a response. This must not change when the command is not supported.
  271. * @param flags if AVFILTER_CMD_FLAG_FAST is set and the command would be
  272. * time consuming then a filter should treat it like an unsupported command
  273. *
  274. * @returns >=0 on success otherwise an error code.
  275. * AVERROR(ENOSYS) on unsupported commands
  276. */
  277. int (*process_command)(AVFilterContext *, const char *cmd, const char *arg, char *res, int res_len, int flags);
  278. /**
  279. * Filter initialization function, alternative to the init()
  280. * callback. Args contains the user-supplied parameters, opaque is
  281. * used for providing binary data.
  282. */
  283. int (*init_opaque)(AVFilterContext *ctx, void *opaque);
  284. /**
  285. * Filter activation function.
  286. *
  287. * Called when any processing is needed from the filter, instead of any
  288. * filter_frame and request_frame on pads.
  289. *
  290. * The function must examine inlinks and outlinks and perform a single
  291. * step of processing. If there is nothing to do, the function must do
  292. * nothing and not return an error. If more steps are or may be
  293. * possible, it must use ff_filter_set_ready() to schedule another
  294. * activation.
  295. */
  296. int (*activate)(AVFilterContext *ctx);
  297. } AVFilter;
  298. /**
  299. * Process multiple parts of the frame concurrently.
  300. */
  301. #define AVFILTER_THREAD_SLICE (1 << 0)
  302. typedef struct AVFilterInternal AVFilterInternal;
  303. /** An instance of a filter */
  304. struct AVFilterContext {
  305. const AVClass *av_class; ///< needed for av_log() and filters common options
  306. const AVFilter *filter; ///< the AVFilter of which this is an instance
  307. char *name; ///< name of this filter instance
  308. AVFilterPad *input_pads; ///< array of input pads
  309. AVFilterLink **inputs; ///< array of pointers to input links
  310. unsigned nb_inputs; ///< number of input pads
  311. AVFilterPad *output_pads; ///< array of output pads
  312. AVFilterLink **outputs; ///< array of pointers to output links
  313. unsigned nb_outputs; ///< number of output pads
  314. void *priv; ///< private data for use by the filter
  315. struct AVFilterGraph *graph; ///< filtergraph this filter belongs to
  316. /**
  317. * Type of multithreading being allowed/used. A combination of
  318. * AVFILTER_THREAD_* flags.
  319. *
  320. * May be set by the caller before initializing the filter to forbid some
  321. * or all kinds of multithreading for this filter. The default is allowing
  322. * everything.
  323. *
  324. * When the filter is initialized, this field is combined using bit AND with
  325. * AVFilterGraph.thread_type to get the final mask used for determining
  326. * allowed threading types. I.e. a threading type needs to be set in both
  327. * to be allowed.
  328. *
  329. * After the filter is initialized, libavfilter sets this field to the
  330. * threading type that is actually used (0 for no multithreading).
  331. */
  332. int thread_type;
  333. /**
  334. * An opaque struct for libavfilter internal use.
  335. */
  336. AVFilterInternal *internal;
  337. struct AVFilterCommand *command_queue;
  338. char *enable_str; ///< enable expression string
  339. void *enable; ///< parsed expression (AVExpr*)
  340. double *var_values; ///< variable values for the enable expression
  341. int is_disabled; ///< the enabled state from the last expression evaluation
  342. /**
  343. * For filters which will create hardware frames, sets the device the
  344. * filter should create them in. All other filters will ignore this field:
  345. * in particular, a filter which consumes or processes hardware frames will
  346. * instead use the hw_frames_ctx field in AVFilterLink to carry the
  347. * hardware context information.
  348. */
  349. AVBufferRef *hw_device_ctx;
  350. /**
  351. * Max number of threads allowed in this filter instance.
  352. * If <= 0, its value is ignored.
  353. * Overrides global number of threads set per filter graph.
  354. */
  355. int nb_threads;
  356. /**
  357. * Ready status of the filter.
  358. * A non-0 value means that the filter needs activating;
  359. * a higher value suggests a more urgent activation.
  360. */
  361. unsigned ready;
  362. /**
  363. * Sets the number of extra hardware frames which the filter will
  364. * allocate on its output links for use in following filters or by
  365. * the caller.
  366. *
  367. * Some hardware filters require all frames that they will use for
  368. * output to be defined in advance before filtering starts. For such
  369. * filters, any hardware frame pools used for output must therefore be
  370. * of fixed size. The extra frames set here are on top of any number
  371. * that the filter needs internally in order to operate normally.
  372. *
  373. * This field must be set before the graph containing this filter is
  374. * configured.
  375. */
  376. int extra_hw_frames;
  377. };
  378. /**
  379. * A link between two filters. This contains pointers to the source and
  380. * destination filters between which this link exists, and the indexes of
  381. * the pads involved. In addition, this link also contains the parameters
  382. * which have been negotiated and agreed upon between the filter, such as
  383. * image dimensions, format, etc.
  384. *
  385. * Applications must not normally access the link structure directly.
  386. * Use the buffersrc and buffersink API instead.
  387. * In the future, access to the header may be reserved for filters
  388. * implementation.
  389. */
  390. struct AVFilterLink {
  391. AVFilterContext *src; ///< source filter
  392. AVFilterPad *srcpad; ///< output pad on the source filter
  393. AVFilterContext *dst; ///< dest filter
  394. AVFilterPad *dstpad; ///< input pad on the dest filter
  395. enum AVMediaType type; ///< filter media type
  396. /* These parameters apply only to video */
  397. int w; ///< agreed upon image width
  398. int h; ///< agreed upon image height
  399. AVRational sample_aspect_ratio; ///< agreed upon sample aspect ratio
  400. /* These parameters apply only to audio */
  401. uint64_t channel_layout; ///< channel layout of current buffer (see libavutil/channel_layout.h)
  402. int sample_rate; ///< samples per second
  403. int format; ///< agreed upon media format
  404. /**
  405. * Define the time base used by the PTS of the frames/samples
  406. * which will pass through this link.
  407. * During the configuration stage, each filter is supposed to
  408. * change only the output timebase, while the timebase of the
  409. * input link is assumed to be an unchangeable property.
  410. */
  411. AVRational time_base;
  412. /*****************************************************************
  413. * All fields below this line are not part of the public API. They
  414. * may not be used outside of libavfilter and can be changed and
  415. * removed at will.
  416. * New public fields should be added right above.
  417. *****************************************************************
  418. */
  419. /**
  420. * Lists of formats and channel layouts supported by the input and output
  421. * filters respectively. These lists are used for negotiating the format
  422. * to actually be used, which will be loaded into the format and
  423. * channel_layout members, above, when chosen.
  424. *
  425. */
  426. AVFilterFormats *in_formats;
  427. AVFilterFormats *out_formats;
  428. /**
  429. * Lists of channel layouts and sample rates used for automatic
  430. * negotiation.
  431. */
  432. AVFilterFormats *in_samplerates;
  433. AVFilterFormats *out_samplerates;
  434. struct AVFilterChannelLayouts *in_channel_layouts;
  435. struct AVFilterChannelLayouts *out_channel_layouts;
  436. /**
  437. * Audio only, the destination filter sets this to a non-zero value to
  438. * request that buffers with the given number of samples should be sent to
  439. * it. AVFilterPad.needs_fifo must also be set on the corresponding input
  440. * pad.
  441. * Last buffer before EOF will be padded with silence.
  442. */
  443. int request_samples;
  444. /** stage of the initialization of the link properties (dimensions, etc) */
  445. enum {
  446. AVLINK_UNINIT = 0, ///< not started
  447. AVLINK_STARTINIT, ///< started, but incomplete
  448. AVLINK_INIT ///< complete
  449. } init_state;
  450. /**
  451. * Graph the filter belongs to.
  452. */
  453. struct AVFilterGraph *graph;
  454. /**
  455. * Current timestamp of the link, as defined by the most recent
  456. * frame(s), in link time_base units.
  457. */
  458. int64_t current_pts;
  459. /**
  460. * Current timestamp of the link, as defined by the most recent
  461. * frame(s), in AV_TIME_BASE units.
  462. */
  463. int64_t current_pts_us;
  464. /**
  465. * Index in the age array.
  466. */
  467. int age_index;
  468. /**
  469. * Frame rate of the stream on the link, or 1/0 if unknown or variable;
  470. * if left to 0/0, will be automatically copied from the first input
  471. * of the source filter if it exists.
  472. *
  473. * Sources should set it to the best estimation of the real frame rate.
  474. * If the source frame rate is unknown or variable, set this to 1/0.
  475. * Filters should update it if necessary depending on their function.
  476. * Sinks can use it to set a default output frame rate.
  477. * It is similar to the r_frame_rate field in AVStream.
  478. */
  479. AVRational frame_rate;
  480. /**
  481. * Buffer partially filled with samples to achieve a fixed/minimum size.
  482. */
  483. AVFrame *partial_buf;
  484. /**
  485. * Size of the partial buffer to allocate.
  486. * Must be between min_samples and max_samples.
  487. */
  488. int partial_buf_size;
  489. /**
  490. * Minimum number of samples to filter at once. If filter_frame() is
  491. * called with fewer samples, it will accumulate them in partial_buf.
  492. * This field and the related ones must not be changed after filtering
  493. * has started.
  494. * If 0, all related fields are ignored.
  495. */
  496. int min_samples;
  497. /**
  498. * Maximum number of samples to filter at once. If filter_frame() is
  499. * called with more samples, it will split them.
  500. */
  501. int max_samples;
  502. /**
  503. * Number of channels.
  504. */
  505. int channels;
  506. /**
  507. * Link processing flags.
  508. */
  509. unsigned flags;
  510. /**
  511. * Number of past frames sent through the link.
  512. */
  513. int64_t frame_count_in, frame_count_out;
  514. /**
  515. * A pointer to a FFFramePool struct.
  516. */
  517. void *frame_pool;
  518. /**
  519. * True if a frame is currently wanted on the output of this filter.
  520. * Set when ff_request_frame() is called by the output,
  521. * cleared when a frame is filtered.
  522. */
  523. int frame_wanted_out;
  524. /**
  525. * For hwaccel pixel formats, this should be a reference to the
  526. * AVHWFramesContext describing the frames.
  527. */
  528. AVBufferRef *hw_frames_ctx;
  529. #ifndef FF_INTERNAL_FIELDS
  530. /**
  531. * Internal structure members.
  532. * The fields below this limit are internal for libavfilter's use
  533. * and must in no way be accessed by applications.
  534. */
  535. char reserved[0xF000];
  536. #else /* FF_INTERNAL_FIELDS */
  537. /**
  538. * Queue of frames waiting to be filtered.
  539. */
  540. FFFrameQueue fifo;
  541. /**
  542. * If set, the source filter can not generate a frame as is.
  543. * The goal is to avoid repeatedly calling the request_frame() method on
  544. * the same link.
  545. */
  546. int frame_blocked_in;
  547. /**
  548. * Link input status.
  549. * If not zero, all attempts of filter_frame will fail with the
  550. * corresponding code.
  551. */
  552. int status_in;
  553. /**
  554. * Timestamp of the input status change.
  555. */
  556. int64_t status_in_pts;
  557. /**
  558. * Link output status.
  559. * If not zero, all attempts of request_frame will fail with the
  560. * corresponding code.
  561. */
  562. int status_out;
  563. #endif /* FF_INTERNAL_FIELDS */
  564. };
  565. /**
  566. * Link two filters together.
  567. *
  568. * @param src the source filter
  569. * @param srcpad index of the output pad on the source filter
  570. * @param dst the destination filter
  571. * @param dstpad index of the input pad on the destination filter
  572. * @return zero on success
  573. */
  574. int avfilter_link(AVFilterContext *src, unsigned srcpad,
  575. AVFilterContext *dst, unsigned dstpad);
  576. /**
  577. * Free the link in *link, and set its pointer to NULL.
  578. */
  579. void avfilter_link_free(AVFilterLink **link);
  580. #if FF_API_FILTER_GET_SET
  581. /**
  582. * Get the number of channels of a link.
  583. * @deprecated Use av_buffersink_get_channels()
  584. */
  585. attribute_deprecated
  586. int avfilter_link_get_channels(AVFilterLink *link);
  587. #endif
  588. /**
  589. * Set the closed field of a link.
  590. * @deprecated applications are not supposed to mess with links, they should
  591. * close the sinks.
  592. */
  593. attribute_deprecated
  594. void avfilter_link_set_closed(AVFilterLink *link, int closed);
  595. /**
  596. * Negotiate the media format, dimensions, etc of all inputs to a filter.
  597. *
  598. * @param filter the filter to negotiate the properties for its inputs
  599. * @return zero on successful negotiation
  600. */
  601. int avfilter_config_links(AVFilterContext *filter);
  602. #define AVFILTER_CMD_FLAG_ONE 1 ///< Stop once a filter understood the command (for target=all for example), fast filters are favored automatically
  603. #define AVFILTER_CMD_FLAG_FAST 2 ///< Only execute command when its fast (like a video out that supports contrast adjustment in hw)
  604. /**
  605. * Make the filter instance process a command.
  606. * It is recommended to use avfilter_graph_send_command().
  607. */
  608. int avfilter_process_command(AVFilterContext *filter, const char *cmd, const char *arg, char *res, int res_len, int flags);
  609. /**
  610. * Iterate over all registered filters.
  611. *
  612. * @param opaque a pointer where libavfilter will store the iteration state. Must
  613. * point to NULL to start the iteration.
  614. *
  615. * @return the next registered filter or NULL when the iteration is
  616. * finished
  617. */
  618. const AVFilter *av_filter_iterate(void **opaque);
  619. #if FF_API_NEXT
  620. /** Initialize the filter system. Register all builtin filters. */
  621. attribute_deprecated
  622. void avfilter_register_all(void);
  623. /**
  624. * Register a filter. This is only needed if you plan to use
  625. * avfilter_get_by_name later to lookup the AVFilter structure by name. A
  626. * filter can still by instantiated with avfilter_graph_alloc_filter even if it
  627. * is not registered.
  628. *
  629. * @param filter the filter to register
  630. * @return 0 if the registration was successful, a negative value
  631. * otherwise
  632. */
  633. attribute_deprecated
  634. int avfilter_register(AVFilter *filter);
  635. /**
  636. * Iterate over all registered filters.
  637. * @return If prev is non-NULL, next registered filter after prev or NULL if
  638. * prev is the last filter. If prev is NULL, return the first registered filter.
  639. */
  640. attribute_deprecated
  641. const AVFilter *avfilter_next(const AVFilter *prev);
  642. #endif
  643. /**
  644. * Get a filter definition matching the given name.
  645. *
  646. * @param name the filter name to find
  647. * @return the filter definition, if any matching one is registered.
  648. * NULL if none found.
  649. */
  650. const AVFilter *avfilter_get_by_name(const char *name);
  651. /**
  652. * Initialize a filter with the supplied parameters.
  653. *
  654. * @param ctx uninitialized filter context to initialize
  655. * @param args Options to initialize the filter with. This must be a
  656. * ':'-separated list of options in the 'key=value' form.
  657. * May be NULL if the options have been set directly using the
  658. * AVOptions API or there are no options that need to be set.
  659. * @return 0 on success, a negative AVERROR on failure
  660. */
  661. int avfilter_init_str(AVFilterContext *ctx, const char *args);
  662. /**
  663. * Initialize a filter with the supplied dictionary of options.
  664. *
  665. * @param ctx uninitialized filter context to initialize
  666. * @param options An AVDictionary filled with options for this filter. On
  667. * return this parameter will be destroyed and replaced with
  668. * a dict containing options that were not found. This dictionary
  669. * must be freed by the caller.
  670. * May be NULL, then this function is equivalent to
  671. * avfilter_init_str() with the second parameter set to NULL.
  672. * @return 0 on success, a negative AVERROR on failure
  673. *
  674. * @note This function and avfilter_init_str() do essentially the same thing,
  675. * the difference is in manner in which the options are passed. It is up to the
  676. * calling code to choose whichever is more preferable. The two functions also
  677. * behave differently when some of the provided options are not declared as
  678. * supported by the filter. In such a case, avfilter_init_str() will fail, but
  679. * this function will leave those extra options in the options AVDictionary and
  680. * continue as usual.
  681. */
  682. int avfilter_init_dict(AVFilterContext *ctx, AVDictionary **options);
  683. /**
  684. * Free a filter context. This will also remove the filter from its
  685. * filtergraph's list of filters.
  686. *
  687. * @param filter the filter to free
  688. */
  689. void avfilter_free(AVFilterContext *filter);
  690. /**
  691. * Insert a filter in the middle of an existing link.
  692. *
  693. * @param link the link into which the filter should be inserted
  694. * @param filt the filter to be inserted
  695. * @param filt_srcpad_idx the input pad on the filter to connect
  696. * @param filt_dstpad_idx the output pad on the filter to connect
  697. * @return zero on success
  698. */
  699. int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt,
  700. unsigned filt_srcpad_idx, unsigned filt_dstpad_idx);
  701. /**
  702. * @return AVClass for AVFilterContext.
  703. *
  704. * @see av_opt_find().
  705. */
  706. const AVClass *avfilter_get_class(void);
  707. typedef struct AVFilterGraphInternal AVFilterGraphInternal;
  708. /**
  709. * A function pointer passed to the @ref AVFilterGraph.execute callback to be
  710. * executed multiple times, possibly in parallel.
  711. *
  712. * @param ctx the filter context the job belongs to
  713. * @param arg an opaque parameter passed through from @ref
  714. * AVFilterGraph.execute
  715. * @param jobnr the index of the job being executed
  716. * @param nb_jobs the total number of jobs
  717. *
  718. * @return 0 on success, a negative AVERROR on error
  719. */
  720. typedef int (avfilter_action_func)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
  721. /**
  722. * A function executing multiple jobs, possibly in parallel.
  723. *
  724. * @param ctx the filter context to which the jobs belong
  725. * @param func the function to be called multiple times
  726. * @param arg the argument to be passed to func
  727. * @param ret a nb_jobs-sized array to be filled with return values from each
  728. * invocation of func
  729. * @param nb_jobs the number of jobs to execute
  730. *
  731. * @return 0 on success, a negative AVERROR on error
  732. */
  733. typedef int (avfilter_execute_func)(AVFilterContext *ctx, avfilter_action_func *func,
  734. void *arg, int *ret, int nb_jobs);
  735. typedef struct AVFilterGraph {
  736. const AVClass *av_class;
  737. AVFilterContext **filters;
  738. unsigned nb_filters;
  739. char *scale_sws_opts; ///< sws options to use for the auto-inserted scale filters
  740. #if FF_API_LAVR_OPTS
  741. attribute_deprecated char *resample_lavr_opts; ///< libavresample options to use for the auto-inserted resample filters
  742. #endif
  743. /**
  744. * Type of multithreading allowed for filters in this graph. A combination
  745. * of AVFILTER_THREAD_* flags.
  746. *
  747. * May be set by the caller at any point, the setting will apply to all
  748. * filters initialized after that. The default is allowing everything.
  749. *
  750. * When a filter in this graph is initialized, this field is combined using
  751. * bit AND with AVFilterContext.thread_type to get the final mask used for
  752. * determining allowed threading types. I.e. a threading type needs to be
  753. * set in both to be allowed.
  754. */
  755. int thread_type;
  756. /**
  757. * Maximum number of threads used by filters in this graph. May be set by
  758. * the caller before adding any filters to the filtergraph. Zero (the
  759. * default) means that the number of threads is determined automatically.
  760. */
  761. int nb_threads;
  762. /**
  763. * Opaque object for libavfilter internal use.
  764. */
  765. AVFilterGraphInternal *internal;
  766. /**
  767. * Opaque user data. May be set by the caller to an arbitrary value, e.g. to
  768. * be used from callbacks like @ref AVFilterGraph.execute.
  769. * Libavfilter will not touch this field in any way.
  770. */
  771. void *opaque;
  772. /**
  773. * This callback may be set by the caller immediately after allocating the
  774. * graph and before adding any filters to it, to provide a custom
  775. * multithreading implementation.
  776. *
  777. * If set, filters with slice threading capability will call this callback
  778. * to execute multiple jobs in parallel.
  779. *
  780. * If this field is left unset, libavfilter will use its internal
  781. * implementation, which may or may not be multithreaded depending on the
  782. * platform and build options.
  783. */
  784. avfilter_execute_func *execute;
  785. char *aresample_swr_opts; ///< swr options to use for the auto-inserted aresample filters, Access ONLY through AVOptions
  786. /**
  787. * Private fields
  788. *
  789. * The following fields are for internal use only.
  790. * Their type, offset, number and semantic can change without notice.
  791. */
  792. AVFilterLink **sink_links;
  793. int sink_links_count;
  794. unsigned disable_auto_convert;
  795. } AVFilterGraph;
  796. /**
  797. * Allocate a filter graph.
  798. *
  799. * @return the allocated filter graph on success or NULL.
  800. */
  801. AVFilterGraph *avfilter_graph_alloc(void);
  802. /**
  803. * Create a new filter instance in a filter graph.
  804. *
  805. * @param graph graph in which the new filter will be used
  806. * @param filter the filter to create an instance of
  807. * @param name Name to give to the new instance (will be copied to
  808. * AVFilterContext.name). This may be used by the caller to identify
  809. * different filters, libavfilter itself assigns no semantics to
  810. * this parameter. May be NULL.
  811. *
  812. * @return the context of the newly created filter instance (note that it is
  813. * also retrievable directly through AVFilterGraph.filters or with
  814. * avfilter_graph_get_filter()) on success or NULL on failure.
  815. */
  816. AVFilterContext *avfilter_graph_alloc_filter(AVFilterGraph *graph,
  817. const AVFilter *filter,
  818. const char *name);
  819. /**
  820. * Get a filter instance identified by instance name from graph.
  821. *
  822. * @param graph filter graph to search through.
  823. * @param name filter instance name (should be unique in the graph).
  824. * @return the pointer to the found filter instance or NULL if it
  825. * cannot be found.
  826. */
  827. AVFilterContext *avfilter_graph_get_filter(AVFilterGraph *graph, const char *name);
  828. /**
  829. * Create and add a filter instance into an existing graph.
  830. * The filter instance is created from the filter filt and inited
  831. * with the parameters args and opaque.
  832. *
  833. * In case of success put in *filt_ctx the pointer to the created
  834. * filter instance, otherwise set *filt_ctx to NULL.
  835. *
  836. * @param name the instance name to give to the created filter instance
  837. * @param graph_ctx the filter graph
  838. * @return a negative AVERROR error code in case of failure, a non
  839. * negative value otherwise
  840. */
  841. int avfilter_graph_create_filter(AVFilterContext **filt_ctx, const AVFilter *filt,
  842. const char *name, const char *args, void *opaque,
  843. AVFilterGraph *graph_ctx);
  844. /**
  845. * Enable or disable automatic format conversion inside the graph.
  846. *
  847. * Note that format conversion can still happen inside explicitly inserted
  848. * scale and aresample filters.
  849. *
  850. * @param flags any of the AVFILTER_AUTO_CONVERT_* constants
  851. */
  852. void avfilter_graph_set_auto_convert(AVFilterGraph *graph, unsigned flags);
  853. enum {
  854. AVFILTER_AUTO_CONVERT_ALL = 0, /**< all automatic conversions enabled */
  855. AVFILTER_AUTO_CONVERT_NONE = -1, /**< all automatic conversions disabled */
  856. };
  857. /**
  858. * Check validity and configure all the links and formats in the graph.
  859. *
  860. * @param graphctx the filter graph
  861. * @param log_ctx context used for logging
  862. * @return >= 0 in case of success, a negative AVERROR code otherwise
  863. */
  864. int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx);
  865. /**
  866. * Free a graph, destroy its links, and set *graph to NULL.
  867. * If *graph is NULL, do nothing.
  868. */
  869. void avfilter_graph_free(AVFilterGraph **graph);
  870. /**
  871. * A linked-list of the inputs/outputs of the filter chain.
  872. *
  873. * This is mainly useful for avfilter_graph_parse() / avfilter_graph_parse2(),
  874. * where it is used to communicate open (unlinked) inputs and outputs from and
  875. * to the caller.
  876. * This struct specifies, per each not connected pad contained in the graph, the
  877. * filter context and the pad index required for establishing a link.
  878. */
  879. typedef struct AVFilterInOut {
  880. /** unique name for this input/output in the list */
  881. char *name;
  882. /** filter context associated to this input/output */
  883. AVFilterContext *filter_ctx;
  884. /** index of the filt_ctx pad to use for linking */
  885. int pad_idx;
  886. /** next input/input in the list, NULL if this is the last */
  887. struct AVFilterInOut *next;
  888. } AVFilterInOut;
  889. /**
  890. * Allocate a single AVFilterInOut entry.
  891. * Must be freed with avfilter_inout_free().
  892. * @return allocated AVFilterInOut on success, NULL on failure.
  893. */
  894. AVFilterInOut *avfilter_inout_alloc(void);
  895. /**
  896. * Free the supplied list of AVFilterInOut and set *inout to NULL.
  897. * If *inout is NULL, do nothing.
  898. */
  899. void avfilter_inout_free(AVFilterInOut **inout);
  900. /**
  901. * Add a graph described by a string to a graph.
  902. *
  903. * @note The caller must provide the lists of inputs and outputs,
  904. * which therefore must be known before calling the function.
  905. *
  906. * @note The inputs parameter describes inputs of the already existing
  907. * part of the graph; i.e. from the point of view of the newly created
  908. * part, they are outputs. Similarly the outputs parameter describes
  909. * outputs of the already existing filters, which are provided as
  910. * inputs to the parsed filters.
  911. *
  912. * @param graph the filter graph where to link the parsed graph context
  913. * @param filters string to be parsed
  914. * @param inputs linked list to the inputs of the graph
  915. * @param outputs linked list to the outputs of the graph
  916. * @return zero on success, a negative AVERROR code on error
  917. */
  918. int avfilter_graph_parse(AVFilterGraph *graph, const char *filters,
  919. AVFilterInOut *inputs, AVFilterInOut *outputs,
  920. void *log_ctx);
  921. /**
  922. * Add a graph described by a string to a graph.
  923. *
  924. * In the graph filters description, if the input label of the first
  925. * filter is not specified, "in" is assumed; if the output label of
  926. * the last filter is not specified, "out" is assumed.
  927. *
  928. * @param graph the filter graph where to link the parsed graph context
  929. * @param filters string to be parsed
  930. * @param inputs pointer to a linked list to the inputs of the graph, may be NULL.
  931. * If non-NULL, *inputs is updated to contain the list of open inputs
  932. * after the parsing, should be freed with avfilter_inout_free().
  933. * @param outputs pointer to a linked list to the outputs of the graph, may be NULL.
  934. * If non-NULL, *outputs is updated to contain the list of open outputs
  935. * after the parsing, should be freed with avfilter_inout_free().
  936. * @return non negative on success, a negative AVERROR code on error
  937. */
  938. int avfilter_graph_parse_ptr(AVFilterGraph *graph, const char *filters,
  939. AVFilterInOut **inputs, AVFilterInOut **outputs,
  940. void *log_ctx);
  941. /**
  942. * Add a graph described by a string to a graph.
  943. *
  944. * @param[in] graph the filter graph where to link the parsed graph context
  945. * @param[in] filters string to be parsed
  946. * @param[out] inputs a linked list of all free (unlinked) inputs of the
  947. * parsed graph will be returned here. It is to be freed
  948. * by the caller using avfilter_inout_free().
  949. * @param[out] outputs a linked list of all free (unlinked) outputs of the
  950. * parsed graph will be returned here. It is to be freed by the
  951. * caller using avfilter_inout_free().
  952. * @return zero on success, a negative AVERROR code on error
  953. *
  954. * @note This function returns the inputs and outputs that are left
  955. * unlinked after parsing the graph and the caller then deals with
  956. * them.
  957. * @note This function makes no reference whatsoever to already
  958. * existing parts of the graph and the inputs parameter will on return
  959. * contain inputs of the newly parsed part of the graph. Analogously
  960. * the outputs parameter will contain outputs of the newly created
  961. * filters.
  962. */
  963. int avfilter_graph_parse2(AVFilterGraph *graph, const char *filters,
  964. AVFilterInOut **inputs,
  965. AVFilterInOut **outputs);
  966. /**
  967. * Send a command to one or more filter instances.
  968. *
  969. * @param graph the filter graph
  970. * @param target the filter(s) to which the command should be sent
  971. * "all" sends to all filters
  972. * otherwise it can be a filter or filter instance name
  973. * which will send the command to all matching filters.
  974. * @param cmd the command to send, for handling simplicity all commands must be alphanumeric only
  975. * @param arg the argument for the command
  976. * @param res a buffer with size res_size where the filter(s) can return a response.
  977. *
  978. * @returns >=0 on success otherwise an error code.
  979. * AVERROR(ENOSYS) on unsupported commands
  980. */
  981. int avfilter_graph_send_command(AVFilterGraph *graph, const char *target, const char *cmd, const char *arg, char *res, int res_len, int flags);
  982. /**
  983. * Queue a command for one or more filter instances.
  984. *
  985. * @param graph the filter graph
  986. * @param target the filter(s) to which the command should be sent
  987. * "all" sends to all filters
  988. * otherwise it can be a filter or filter instance name
  989. * which will send the command to all matching filters.
  990. * @param cmd the command to sent, for handling simplicity all commands must be alphanumeric only
  991. * @param arg the argument for the command
  992. * @param ts time at which the command should be sent to the filter
  993. *
  994. * @note As this executes commands after this function returns, no return code
  995. * from the filter is provided, also AVFILTER_CMD_FLAG_ONE is not supported.
  996. */
  997. int avfilter_graph_queue_command(AVFilterGraph *graph, const char *target, const char *cmd, const char *arg, int flags, double ts);
  998. /**
  999. * Dump a graph into a human-readable string representation.
  1000. *
  1001. * @param graph the graph to dump
  1002. * @param options formatting options; currently ignored
  1003. * @return a string, or NULL in case of memory allocation failure;
  1004. * the string must be freed using av_free
  1005. */
  1006. char *avfilter_graph_dump(AVFilterGraph *graph, const char *options);
  1007. /**
  1008. * Request a frame on the oldest sink link.
  1009. *
  1010. * If the request returns AVERROR_EOF, try the next.
  1011. *
  1012. * Note that this function is not meant to be the sole scheduling mechanism
  1013. * of a filtergraph, only a convenience function to help drain a filtergraph
  1014. * in a balanced way under normal circumstances.
  1015. *
  1016. * Also note that AVERROR_EOF does not mean that frames did not arrive on
  1017. * some of the sinks during the process.
  1018. * When there are multiple sink links, in case the requested link
  1019. * returns an EOF, this may cause a filter to flush pending frames
  1020. * which are sent to another sink link, although unrequested.
  1021. *
  1022. * @return the return value of ff_request_frame(),
  1023. * or AVERROR_EOF if all links returned AVERROR_EOF
  1024. */
  1025. int avfilter_graph_request_oldest(AVFilterGraph *graph);
  1026. /**
  1027. * @}
  1028. */
  1029. #endif /* AVFILTER_AVFILTER_H */