You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1161 lines
41KB

  1. /*
  2. * filter layer
  3. * Copyright (c) 2007 Bobby Bingham
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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 - graph-based frame editing library
  30. * @{
  31. */
  32. #include "libavutil/attributes.h"
  33. #include "libavutil/avutil.h"
  34. #include "libavutil/frame.h"
  35. #include "libavutil/log.h"
  36. #include "libavutil/samplefmt.h"
  37. #include "libavutil/pixfmt.h"
  38. #include "libavutil/rational.h"
  39. #include "libavcodec/avcodec.h"
  40. #include <stddef.h>
  41. #include "libavfilter/version.h"
  42. /**
  43. * Return the LIBAVFILTER_VERSION_INT constant.
  44. */
  45. unsigned avfilter_version(void);
  46. /**
  47. * Return the libavfilter build-time configuration.
  48. */
  49. const char *avfilter_configuration(void);
  50. /**
  51. * Return the libavfilter license.
  52. */
  53. const char *avfilter_license(void);
  54. typedef struct AVFilterContext AVFilterContext;
  55. typedef struct AVFilterLink AVFilterLink;
  56. typedef struct AVFilterPad AVFilterPad;
  57. typedef struct AVFilterFormats AVFilterFormats;
  58. #if FF_API_AVFILTERBUFFER
  59. /**
  60. * A reference-counted buffer data type used by the filter system. Filters
  61. * should not store pointers to this structure directly, but instead use the
  62. * AVFilterBufferRef structure below.
  63. */
  64. typedef struct AVFilterBuffer {
  65. uint8_t *data[8]; ///< buffer data for each plane/channel
  66. /**
  67. * pointers to the data planes/channels.
  68. *
  69. * For video, this should simply point to data[].
  70. *
  71. * For planar audio, each channel has a separate data pointer, and
  72. * linesize[0] contains the size of each channel buffer.
  73. * For packed audio, there is just one data pointer, and linesize[0]
  74. * contains the total size of the buffer for all channels.
  75. *
  76. * Note: Both data and extended_data will always be set, but for planar
  77. * audio with more channels that can fit in data, extended_data must be used
  78. * in order to access all channels.
  79. */
  80. uint8_t **extended_data;
  81. int linesize[8]; ///< number of bytes per line
  82. /** private data to be used by a custom free function */
  83. void *priv;
  84. /**
  85. * A pointer to the function to deallocate this buffer if the default
  86. * function is not sufficient. This could, for example, add the memory
  87. * back into a memory pool to be reused later without the overhead of
  88. * reallocating it from scratch.
  89. */
  90. void (*free)(struct AVFilterBuffer *buf);
  91. int format; ///< media format
  92. int w, h; ///< width and height of the allocated buffer
  93. unsigned refcount; ///< number of references to this buffer
  94. } AVFilterBuffer;
  95. #define AV_PERM_READ 0x01 ///< can read from the buffer
  96. #define AV_PERM_WRITE 0x02 ///< can write to the buffer
  97. #define AV_PERM_PRESERVE 0x04 ///< nobody else can overwrite the buffer
  98. #define AV_PERM_REUSE 0x08 ///< can output the buffer multiple times, with the same contents each time
  99. #define AV_PERM_REUSE2 0x10 ///< can output the buffer multiple times, modified each time
  100. #define AV_PERM_NEG_LINESIZES 0x20 ///< the buffer requested can have negative linesizes
  101. /**
  102. * Audio specific properties in a reference to an AVFilterBuffer. Since
  103. * AVFilterBufferRef is common to different media formats, audio specific
  104. * per reference properties must be separated out.
  105. */
  106. typedef struct AVFilterBufferRefAudioProps {
  107. uint64_t channel_layout; ///< channel layout of audio buffer
  108. int nb_samples; ///< number of audio samples
  109. int sample_rate; ///< audio buffer sample rate
  110. int planar; ///< audio buffer - planar or packed
  111. } AVFilterBufferRefAudioProps;
  112. /**
  113. * Video specific properties in a reference to an AVFilterBuffer. Since
  114. * AVFilterBufferRef is common to different media formats, video specific
  115. * per reference properties must be separated out.
  116. */
  117. typedef struct AVFilterBufferRefVideoProps {
  118. int w; ///< image width
  119. int h; ///< image height
  120. AVRational pixel_aspect; ///< pixel aspect ratio
  121. int interlaced; ///< is frame interlaced
  122. int top_field_first; ///< field order
  123. enum AVPictureType pict_type; ///< picture type of the frame
  124. int key_frame; ///< 1 -> keyframe, 0-> not
  125. } AVFilterBufferRefVideoProps;
  126. /**
  127. * A reference to an AVFilterBuffer. Since filters can manipulate the origin of
  128. * a buffer to, for example, crop image without any memcpy, the buffer origin
  129. * and dimensions are per-reference properties. Linesize is also useful for
  130. * image flipping, frame to field filters, etc, and so is also per-reference.
  131. *
  132. * TODO: add anything necessary for frame reordering
  133. */
  134. typedef struct AVFilterBufferRef {
  135. AVFilterBuffer *buf; ///< the buffer that this is a reference to
  136. uint8_t *data[8]; ///< picture/audio data for each plane
  137. /**
  138. * pointers to the data planes/channels.
  139. *
  140. * For video, this should simply point to data[].
  141. *
  142. * For planar audio, each channel has a separate data pointer, and
  143. * linesize[0] contains the size of each channel buffer.
  144. * For packed audio, there is just one data pointer, and linesize[0]
  145. * contains the total size of the buffer for all channels.
  146. *
  147. * Note: Both data and extended_data will always be set, but for planar
  148. * audio with more channels that can fit in data, extended_data must be used
  149. * in order to access all channels.
  150. */
  151. uint8_t **extended_data;
  152. int linesize[8]; ///< number of bytes per line
  153. AVFilterBufferRefVideoProps *video; ///< video buffer specific properties
  154. AVFilterBufferRefAudioProps *audio; ///< audio buffer specific properties
  155. /**
  156. * presentation timestamp. The time unit may change during
  157. * filtering, as it is specified in the link and the filter code
  158. * may need to rescale the PTS accordingly.
  159. */
  160. int64_t pts;
  161. int64_t pos; ///< byte position in stream, -1 if unknown
  162. int format; ///< media format
  163. int perms; ///< permissions, see the AV_PERM_* flags
  164. enum AVMediaType type; ///< media type of buffer data
  165. } AVFilterBufferRef;
  166. /**
  167. * Copy properties of src to dst, without copying the actual data
  168. */
  169. attribute_deprecated
  170. void avfilter_copy_buffer_ref_props(AVFilterBufferRef *dst, AVFilterBufferRef *src);
  171. /**
  172. * Add a new reference to a buffer.
  173. *
  174. * @param ref an existing reference to the buffer
  175. * @param pmask a bitmask containing the allowable permissions in the new
  176. * reference
  177. * @return a new reference to the buffer with the same properties as the
  178. * old, excluding any permissions denied by pmask
  179. */
  180. attribute_deprecated
  181. AVFilterBufferRef *avfilter_ref_buffer(AVFilterBufferRef *ref, int pmask);
  182. /**
  183. * Remove a reference to a buffer. If this is the last reference to the
  184. * buffer, the buffer itself is also automatically freed.
  185. *
  186. * @param ref reference to the buffer, may be NULL
  187. *
  188. * @note it is recommended to use avfilter_unref_bufferp() instead of this
  189. * function
  190. */
  191. attribute_deprecated
  192. void avfilter_unref_buffer(AVFilterBufferRef *ref);
  193. /**
  194. * Remove a reference to a buffer and set the pointer to NULL.
  195. * If this is the last reference to the buffer, the buffer itself
  196. * is also automatically freed.
  197. *
  198. * @param ref pointer to the buffer reference
  199. */
  200. attribute_deprecated
  201. void avfilter_unref_bufferp(AVFilterBufferRef **ref);
  202. #endif
  203. #if FF_API_AVFILTERPAD_PUBLIC
  204. /**
  205. * A filter pad used for either input or output.
  206. *
  207. * @warning this struct will be removed from public API.
  208. * users should call avfilter_pad_get_name() and avfilter_pad_get_type()
  209. * to access the name and type fields; there should be no need to access
  210. * any other fields from outside of libavfilter.
  211. */
  212. struct AVFilterPad {
  213. /**
  214. * Pad name. The name is unique among inputs and among outputs, but an
  215. * input may have the same name as an output. This may be NULL if this
  216. * pad has no need to ever be referenced by name.
  217. */
  218. const char *name;
  219. /**
  220. * AVFilterPad type.
  221. */
  222. enum AVMediaType type;
  223. /**
  224. * Minimum required permissions on incoming buffers. Any buffer with
  225. * insufficient permissions will be automatically copied by the filter
  226. * system to a new buffer which provides the needed access permissions.
  227. *
  228. * Input pads only.
  229. */
  230. attribute_deprecated int min_perms;
  231. /**
  232. * Permissions which are not accepted on incoming buffers. Any buffer
  233. * which has any of these permissions set will be automatically copied
  234. * by the filter system to a new buffer which does not have those
  235. * permissions. This can be used to easily disallow buffers with
  236. * AV_PERM_REUSE.
  237. *
  238. * Input pads only.
  239. */
  240. attribute_deprecated int rej_perms;
  241. /**
  242. * @deprecated unused
  243. */
  244. int (*start_frame)(AVFilterLink *link, AVFilterBufferRef *picref);
  245. /**
  246. * Callback function to get a video buffer. If NULL, the filter system will
  247. * use avfilter_default_get_video_buffer().
  248. *
  249. * Input video pads only.
  250. */
  251. AVFrame *(*get_video_buffer)(AVFilterLink *link, int w, int h);
  252. /**
  253. * Callback function to get an audio buffer. If NULL, the filter system will
  254. * use avfilter_default_get_audio_buffer().
  255. *
  256. * Input audio pads only.
  257. */
  258. AVFrame *(*get_audio_buffer)(AVFilterLink *link, int nb_samples);
  259. /**
  260. * @deprecated unused
  261. */
  262. int (*end_frame)(AVFilterLink *link);
  263. /**
  264. * @deprecated unused
  265. */
  266. int (*draw_slice)(AVFilterLink *link, int y, int height, int slice_dir);
  267. /**
  268. * Filtering callback. This is where a filter receives a frame with
  269. * audio/video data and should do its processing.
  270. *
  271. * Input pads only.
  272. *
  273. * @return >= 0 on success, a negative AVERROR on error. This function
  274. * must ensure that samplesref is properly unreferenced on error if it
  275. * hasn't been passed on to another filter.
  276. */
  277. int (*filter_frame)(AVFilterLink *link, AVFrame *frame);
  278. /**
  279. * Frame poll callback. This returns the number of immediately available
  280. * samples. It should return a positive value if the next request_frame()
  281. * is guaranteed to return one frame (with no delay).
  282. *
  283. * Defaults to just calling the source poll_frame() method.
  284. *
  285. * Output pads only.
  286. */
  287. int (*poll_frame)(AVFilterLink *link);
  288. /**
  289. * Frame request callback. A call to this should result in at least one
  290. * frame being output over the given link. This should return zero on
  291. * success, and another value on error.
  292. *
  293. * Output pads only.
  294. */
  295. int (*request_frame)(AVFilterLink *link);
  296. /**
  297. * Link configuration callback.
  298. *
  299. * For output pads, this should set the link properties such as
  300. * width/height. This should NOT set the format property - that is
  301. * negotiated between filters by the filter system using the
  302. * query_formats() callback before this function is called.
  303. *
  304. * For input pads, this should check the properties of the link, and update
  305. * the filter's internal state as necessary.
  306. *
  307. * For both input and output filters, this should return zero on success,
  308. * and another value on error.
  309. */
  310. int (*config_props)(AVFilterLink *link);
  311. /**
  312. * The filter expects a fifo to be inserted on its input link,
  313. * typically because it has a delay.
  314. *
  315. * input pads only.
  316. */
  317. int needs_fifo;
  318. int needs_writable;
  319. };
  320. #endif
  321. /**
  322. * Get the number of elements in a NULL-terminated array of AVFilterPads (e.g.
  323. * AVFilter.inputs/outputs).
  324. */
  325. int avfilter_pad_count(const AVFilterPad *pads);
  326. /**
  327. * Get the name of an AVFilterPad.
  328. *
  329. * @param pads an array of AVFilterPads
  330. * @param pad_idx index of the pad in the array it; is the caller's
  331. * responsibility to ensure the index is valid
  332. *
  333. * @return name of the pad_idx'th pad in pads
  334. */
  335. const char *avfilter_pad_get_name(const AVFilterPad *pads, int pad_idx);
  336. /**
  337. * Get the type of an AVFilterPad.
  338. *
  339. * @param pads an array of AVFilterPads
  340. * @param pad_idx index of the pad in the array; it is the caller's
  341. * responsibility to ensure the index is valid
  342. *
  343. * @return type of the pad_idx'th pad in pads
  344. */
  345. enum AVMediaType avfilter_pad_get_type(const AVFilterPad *pads, int pad_idx);
  346. /**
  347. * The number of the filter inputs is not determined just by AVFilter.inputs.
  348. * The filter might add additional inputs during initialization depending on the
  349. * options supplied to it.
  350. */
  351. #define AVFILTER_FLAG_DYNAMIC_INPUTS (1 << 0)
  352. /**
  353. * The number of the filter outputs is not determined just by AVFilter.outputs.
  354. * The filter might add additional outputs during initialization depending on
  355. * the options supplied to it.
  356. */
  357. #define AVFILTER_FLAG_DYNAMIC_OUTPUTS (1 << 1)
  358. /**
  359. * The filter supports multithreading by splitting frames into multiple parts
  360. * and processing them concurrently.
  361. */
  362. #define AVFILTER_FLAG_SLICE_THREADS (1 << 2)
  363. /**
  364. * Filter definition. This defines the pads a filter contains, and all the
  365. * callback functions used to interact with the filter.
  366. */
  367. typedef struct AVFilter {
  368. /**
  369. * Filter name. Must be non-NULL and unique among filters.
  370. */
  371. const char *name;
  372. /**
  373. * A description of the filter. May be NULL.
  374. *
  375. * You should use the NULL_IF_CONFIG_SMALL() macro to define it.
  376. */
  377. const char *description;
  378. /**
  379. * List of inputs, terminated by a zeroed element.
  380. *
  381. * NULL if there are no (static) inputs. Instances of filters with
  382. * AVFILTER_FLAG_DYNAMIC_INPUTS set may have more inputs than present in
  383. * this list.
  384. */
  385. const AVFilterPad *inputs;
  386. /**
  387. * List of outputs, terminated by a zeroed element.
  388. *
  389. * NULL if there are no (static) outputs. Instances of filters with
  390. * AVFILTER_FLAG_DYNAMIC_OUTPUTS set may have more outputs than present in
  391. * this list.
  392. */
  393. const AVFilterPad *outputs;
  394. /**
  395. * A class for the private data, used to declare filter private AVOptions.
  396. * This field is NULL for filters that do not declare any options.
  397. *
  398. * If this field is non-NULL, the first member of the filter private data
  399. * must be a pointer to AVClass, which will be set by libavfilter generic
  400. * code to this class.
  401. */
  402. const AVClass *priv_class;
  403. /**
  404. * A combination of AVFILTER_FLAG_*
  405. */
  406. int flags;
  407. /*****************************************************************
  408. * All fields below this line are not part of the public API. They
  409. * may not be used outside of libavfilter and can be changed and
  410. * removed at will.
  411. * New public fields should be added right above.
  412. *****************************************************************
  413. */
  414. /**
  415. * Filter initialization function.
  416. *
  417. * This callback will be called only once during the filter lifetime, after
  418. * all the options have been set, but before links between filters are
  419. * established and format negotiation is done.
  420. *
  421. * Basic filter initialization should be done here. Filters with dynamic
  422. * inputs and/or outputs should create those inputs/outputs here based on
  423. * provided options. No more changes to this filter's inputs/outputs can be
  424. * done after this callback.
  425. *
  426. * This callback must not assume that the filter links exist or frame
  427. * parameters are known.
  428. *
  429. * @ref AVFilter.uninit "uninit" is guaranteed to be called even if
  430. * initialization fails, so this callback does not have to clean up on
  431. * failure.
  432. *
  433. * @return 0 on success, a negative AVERROR on failure
  434. */
  435. int (*init)(AVFilterContext *ctx);
  436. /**
  437. * Should be set instead of @ref AVFilter.init "init" by the filters that
  438. * want to pass a dictionary of AVOptions to nested contexts that are
  439. * allocated during init.
  440. *
  441. * On return, the options dict should be freed and replaced with one that
  442. * contains all the options which could not be processed by this filter (or
  443. * with NULL if all the options were processed).
  444. *
  445. * Otherwise the semantics is the same as for @ref AVFilter.init "init".
  446. */
  447. int (*init_dict)(AVFilterContext *ctx, AVDictionary **options);
  448. /**
  449. * Filter uninitialization function.
  450. *
  451. * Called only once right before the filter is freed. Should deallocate any
  452. * memory held by the filter, release any buffer references, etc. It does
  453. * not need to deallocate the AVFilterContext.priv memory itself.
  454. *
  455. * This callback may be called even if @ref AVFilter.init "init" was not
  456. * called or failed, so it must be prepared to handle such a situation.
  457. */
  458. void (*uninit)(AVFilterContext *ctx);
  459. /**
  460. * Query formats supported by the filter on its inputs and outputs.
  461. *
  462. * This callback is called after the filter is initialized (so the inputs
  463. * and outputs are fixed), shortly before the format negotiation. This
  464. * callback may be called more than once.
  465. *
  466. * This callback must set AVFilterLink.out_formats on every input link and
  467. * AVFilterLink.in_formats on every output link to a list of pixel/sample
  468. * formats that the filter supports on that link. For audio links, this
  469. * filter must also set @ref AVFilterLink.in_samplerates "in_samplerates" /
  470. * @ref AVFilterLink.out_samplerates "out_samplerates" and
  471. * @ref AVFilterLink.in_channel_layouts "in_channel_layouts" /
  472. * @ref AVFilterLink.out_channel_layouts "out_channel_layouts" analogously.
  473. *
  474. * This callback may be NULL for filters with one input, in which case
  475. * libavfilter assumes that it supports all input formats and preserves
  476. * them on output.
  477. *
  478. * @return zero on success, a negative value corresponding to an
  479. * AVERROR code otherwise
  480. */
  481. int (*query_formats)(AVFilterContext *);
  482. int priv_size; ///< size of private data to allocate for the filter
  483. /**
  484. * Used by the filter registration system. Must not be touched by any other
  485. * code.
  486. */
  487. struct AVFilter *next;
  488. } AVFilter;
  489. /**
  490. * Process multiple parts of the frame concurrently.
  491. */
  492. #define AVFILTER_THREAD_SLICE (1 << 0)
  493. typedef struct AVFilterInternal AVFilterInternal;
  494. /** An instance of a filter */
  495. struct AVFilterContext {
  496. const AVClass *av_class; ///< needed for av_log()
  497. const AVFilter *filter; ///< the AVFilter of which this is an instance
  498. char *name; ///< name of this filter instance
  499. AVFilterPad *input_pads; ///< array of input pads
  500. AVFilterLink **inputs; ///< array of pointers to input links
  501. #if FF_API_FOO_COUNT
  502. attribute_deprecated unsigned input_count; ///< @deprecated use nb_inputs
  503. #endif
  504. unsigned nb_inputs; ///< number of input pads
  505. AVFilterPad *output_pads; ///< array of output pads
  506. AVFilterLink **outputs; ///< array of pointers to output links
  507. #if FF_API_FOO_COUNT
  508. attribute_deprecated unsigned output_count; ///< @deprecated use nb_outputs
  509. #endif
  510. unsigned nb_outputs; ///< number of output pads
  511. void *priv; ///< private data for use by the filter
  512. struct AVFilterGraph *graph; ///< filtergraph this filter belongs to
  513. /**
  514. * Type of multithreading being allowed/used. A combination of
  515. * AVFILTER_THREAD_* flags.
  516. *
  517. * May be set by the caller before initializing the filter to forbid some
  518. * or all kinds of multithreading for this filter. The default is allowing
  519. * everything.
  520. *
  521. * When the filter is initialized, this field is combined using bit AND with
  522. * AVFilterGraph.thread_type to get the final mask used for determining
  523. * allowed threading types. I.e. a threading type needs to be set in both
  524. * to be allowed.
  525. *
  526. * After the filter is initialzed, libavfilter sets this field to the
  527. * threading type that is actually used (0 for no multithreading).
  528. */
  529. int thread_type;
  530. /**
  531. * An opaque struct for libavfilter internal use.
  532. */
  533. AVFilterInternal *internal;
  534. };
  535. /**
  536. * A link between two filters. This contains pointers to the source and
  537. * destination filters between which this link exists, and the indexes of
  538. * the pads involved. In addition, this link also contains the parameters
  539. * which have been negotiated and agreed upon between the filter, such as
  540. * image dimensions, format, etc.
  541. */
  542. struct AVFilterLink {
  543. AVFilterContext *src; ///< source filter
  544. AVFilterPad *srcpad; ///< output pad on the source filter
  545. AVFilterContext *dst; ///< dest filter
  546. AVFilterPad *dstpad; ///< input pad on the dest filter
  547. enum AVMediaType type; ///< filter media type
  548. /* These parameters apply only to video */
  549. int w; ///< agreed upon image width
  550. int h; ///< agreed upon image height
  551. AVRational sample_aspect_ratio; ///< agreed upon sample aspect ratio
  552. /* These two parameters apply only to audio */
  553. uint64_t channel_layout; ///< channel layout of current buffer (see libavutil/channel_layout.h)
  554. int sample_rate; ///< samples per second
  555. int format; ///< agreed upon media format
  556. /**
  557. * Define the time base used by the PTS of the frames/samples
  558. * which will pass through this link.
  559. * During the configuration stage, each filter is supposed to
  560. * change only the output timebase, while the timebase of the
  561. * input link is assumed to be an unchangeable property.
  562. */
  563. AVRational time_base;
  564. /*****************************************************************
  565. * All fields below this line are not part of the public API. They
  566. * may not be used outside of libavfilter and can be changed and
  567. * removed at will.
  568. * New public fields should be added right above.
  569. *****************************************************************
  570. */
  571. /**
  572. * Lists of formats supported by the input and output filters respectively.
  573. * These lists are used for negotiating the format to actually be used,
  574. * which will be loaded into the format member, above, when chosen.
  575. */
  576. AVFilterFormats *in_formats;
  577. AVFilterFormats *out_formats;
  578. /**
  579. * Lists of channel layouts and sample rates used for automatic
  580. * negotiation.
  581. */
  582. AVFilterFormats *in_samplerates;
  583. AVFilterFormats *out_samplerates;
  584. struct AVFilterChannelLayouts *in_channel_layouts;
  585. struct AVFilterChannelLayouts *out_channel_layouts;
  586. /**
  587. * Audio only, the destination filter sets this to a non-zero value to
  588. * request that buffers with the given number of samples should be sent to
  589. * it. AVFilterPad.needs_fifo must also be set on the corresponding input
  590. * pad.
  591. * Last buffer before EOF will be padded with silence.
  592. */
  593. int request_samples;
  594. /** stage of the initialization of the link properties (dimensions, etc) */
  595. enum {
  596. AVLINK_UNINIT = 0, ///< not started
  597. AVLINK_STARTINIT, ///< started, but incomplete
  598. AVLINK_INIT ///< complete
  599. } init_state;
  600. };
  601. /**
  602. * Link two filters together.
  603. *
  604. * @param src the source filter
  605. * @param srcpad index of the output pad on the source filter
  606. * @param dst the destination filter
  607. * @param dstpad index of the input pad on the destination filter
  608. * @return zero on success
  609. */
  610. int avfilter_link(AVFilterContext *src, unsigned srcpad,
  611. AVFilterContext *dst, unsigned dstpad);
  612. /**
  613. * Negotiate the media format, dimensions, etc of all inputs to a filter.
  614. *
  615. * @param filter the filter to negotiate the properties for its inputs
  616. * @return zero on successful negotiation
  617. */
  618. int avfilter_config_links(AVFilterContext *filter);
  619. #if FF_API_AVFILTERBUFFER
  620. /**
  621. * Create a buffer reference wrapped around an already allocated image
  622. * buffer.
  623. *
  624. * @param data pointers to the planes of the image to reference
  625. * @param linesize linesizes for the planes of the image to reference
  626. * @param perms the required access permissions
  627. * @param w the width of the image specified by the data and linesize arrays
  628. * @param h the height of the image specified by the data and linesize arrays
  629. * @param format the pixel format of the image specified by the data and linesize arrays
  630. */
  631. attribute_deprecated
  632. AVFilterBufferRef *
  633. avfilter_get_video_buffer_ref_from_arrays(uint8_t *data[4], int linesize[4], int perms,
  634. int w, int h, enum AVPixelFormat format);
  635. /**
  636. * Create an audio buffer reference wrapped around an already
  637. * allocated samples buffer.
  638. *
  639. * @param data pointers to the samples plane buffers
  640. * @param linesize linesize for the samples plane buffers
  641. * @param perms the required access permissions
  642. * @param nb_samples number of samples per channel
  643. * @param sample_fmt the format of each sample in the buffer to allocate
  644. * @param channel_layout the channel layout of the buffer
  645. */
  646. attribute_deprecated
  647. AVFilterBufferRef *avfilter_get_audio_buffer_ref_from_arrays(uint8_t **data,
  648. int linesize,
  649. int perms,
  650. int nb_samples,
  651. enum AVSampleFormat sample_fmt,
  652. uint64_t channel_layout);
  653. #endif
  654. /** Initialize the filter system. Register all builtin filters. */
  655. void avfilter_register_all(void);
  656. #if FF_API_OLD_FILTER_REGISTER
  657. /** Uninitialize the filter system. Unregister all filters. */
  658. attribute_deprecated
  659. void avfilter_uninit(void);
  660. #endif
  661. /**
  662. * Register a filter. This is only needed if you plan to use
  663. * avfilter_get_by_name later to lookup the AVFilter structure by name. A
  664. * filter can still by instantiated with avfilter_graph_alloc_filter even if it
  665. * is not registered.
  666. *
  667. * @param filter the filter to register
  668. * @return 0 if the registration was succesfull, a negative value
  669. * otherwise
  670. */
  671. int avfilter_register(AVFilter *filter);
  672. /**
  673. * Get a filter definition matching the given name.
  674. *
  675. * @param name the filter name to find
  676. * @return the filter definition, if any matching one is registered.
  677. * NULL if none found.
  678. */
  679. AVFilter *avfilter_get_by_name(const char *name);
  680. /**
  681. * Iterate over all registered filters.
  682. * @return If prev is non-NULL, next registered filter after prev or NULL if
  683. * prev is the last filter. If prev is NULL, return the first registered filter.
  684. */
  685. const AVFilter *avfilter_next(const AVFilter *prev);
  686. #if FF_API_OLD_FILTER_REGISTER
  687. /**
  688. * If filter is NULL, returns a pointer to the first registered filter pointer,
  689. * if filter is non-NULL, returns the next pointer after filter.
  690. * If the returned pointer points to NULL, the last registered filter
  691. * was already reached.
  692. * @deprecated use avfilter_next()
  693. */
  694. attribute_deprecated
  695. AVFilter **av_filter_next(AVFilter **filter);
  696. #endif
  697. #if FF_API_AVFILTER_OPEN
  698. /**
  699. * Create a filter instance.
  700. *
  701. * @param filter_ctx put here a pointer to the created filter context
  702. * on success, NULL on failure
  703. * @param filter the filter to create an instance of
  704. * @param inst_name Name to give to the new instance. Can be NULL for none.
  705. * @return >= 0 in case of success, a negative error code otherwise
  706. * @deprecated use avfilter_graph_alloc_filter() instead
  707. */
  708. attribute_deprecated
  709. int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name);
  710. #endif
  711. #if FF_API_AVFILTER_INIT_FILTER
  712. /**
  713. * Initialize a filter.
  714. *
  715. * @param filter the filter to initialize
  716. * @param args A string of parameters to use when initializing the filter.
  717. * The format and meaning of this string varies by filter.
  718. * @param opaque Any extra non-string data needed by the filter. The meaning
  719. * of this parameter varies by filter.
  720. * @return zero on success
  721. */
  722. attribute_deprecated
  723. int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque);
  724. #endif
  725. /**
  726. * Initialize a filter with the supplied parameters.
  727. *
  728. * @param ctx uninitialized filter context to initialize
  729. * @param args Options to initialize the filter with. This must be a
  730. * ':'-separated list of options in the 'key=value' form.
  731. * May be NULL if the options have been set directly using the
  732. * AVOptions API or there are no options that need to be set.
  733. * @return 0 on success, a negative AVERROR on failure
  734. */
  735. int avfilter_init_str(AVFilterContext *ctx, const char *args);
  736. /**
  737. * Initialize a filter with the supplied dictionary of options.
  738. *
  739. * @param ctx uninitialized filter context to initialize
  740. * @param options An AVDictionary filled with options for this filter. On
  741. * return this parameter will be destroyed and replaced with
  742. * a dict containing options that were not found. This dictionary
  743. * must be freed by the caller.
  744. * May be NULL, then this function is equivalent to
  745. * avfilter_init_str() with the second parameter set to NULL.
  746. * @return 0 on success, a negative AVERROR on failure
  747. *
  748. * @note This function and avfilter_init_str() do essentially the same thing,
  749. * the difference is in manner in which the options are passed. It is up to the
  750. * calling code to choose whichever is more preferable. The two functions also
  751. * behave differently when some of the provided options are not declared as
  752. * supported by the filter. In such a case, avfilter_init_str() will fail, but
  753. * this function will leave those extra options in the options AVDictionary and
  754. * continue as usual.
  755. */
  756. int avfilter_init_dict(AVFilterContext *ctx, AVDictionary **options);
  757. /**
  758. * Free a filter context. This will also remove the filter from its
  759. * filtergraph's list of filters.
  760. *
  761. * @param filter the filter to free
  762. */
  763. void avfilter_free(AVFilterContext *filter);
  764. /**
  765. * Insert a filter in the middle of an existing link.
  766. *
  767. * @param link the link into which the filter should be inserted
  768. * @param filt the filter to be inserted
  769. * @param filt_srcpad_idx the input pad on the filter to connect
  770. * @param filt_dstpad_idx the output pad on the filter to connect
  771. * @return zero on success
  772. */
  773. int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt,
  774. unsigned filt_srcpad_idx, unsigned filt_dstpad_idx);
  775. #if FF_API_AVFILTERBUFFER
  776. /**
  777. * Copy the frame properties of src to dst, without copying the actual
  778. * image data.
  779. *
  780. * @return 0 on success, a negative number on error.
  781. */
  782. attribute_deprecated
  783. int avfilter_copy_frame_props(AVFilterBufferRef *dst, const AVFrame *src);
  784. /**
  785. * Copy the frame properties and data pointers of src to dst, without copying
  786. * the actual data.
  787. *
  788. * @return 0 on success, a negative number on error.
  789. */
  790. attribute_deprecated
  791. int avfilter_copy_buf_props(AVFrame *dst, const AVFilterBufferRef *src);
  792. #endif
  793. /**
  794. * @return AVClass for AVFilterContext.
  795. *
  796. * @see av_opt_find().
  797. */
  798. const AVClass *avfilter_get_class(void);
  799. typedef struct AVFilterGraphInternal AVFilterGraphInternal;
  800. /**
  801. * A function pointer passed to the @ref AVFilterGraph.execute callback to be
  802. * executed multiple times, possibly in parallel.
  803. *
  804. * @param ctx the filter context the job belongs to
  805. * @param arg an opaque parameter passed through from @ref
  806. * AVFilterGraph.execute
  807. * @param jobnr the index of the job being executed
  808. * @param nb_jobs the total number of jobs
  809. *
  810. * @return 0 on success, a negative AVERROR on error
  811. */
  812. typedef int (avfilter_action_func)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
  813. /**
  814. * A function executing multiple jobs, possibly in parallel.
  815. *
  816. * @param ctx the filter context to which the jobs belong
  817. * @param func the function to be called multiple times
  818. * @param arg the argument to be passed to func
  819. * @param ret a nb_jobs-sized array to be filled with return values from each
  820. * invocation of func
  821. * @param nb_jobs the number of jobs to execute
  822. *
  823. * @return 0 on success, a negative AVERROR on error
  824. */
  825. typedef int (avfilter_execute_func)(AVFilterContext *ctx, avfilter_action_func *func,
  826. void *arg, int *ret, int nb_jobs);
  827. typedef struct AVFilterGraph {
  828. const AVClass *av_class;
  829. #if FF_API_FOO_COUNT
  830. attribute_deprecated
  831. unsigned filter_count;
  832. #endif
  833. AVFilterContext **filters;
  834. #if !FF_API_FOO_COUNT
  835. unsigned nb_filters;
  836. #endif
  837. char *scale_sws_opts; ///< sws options to use for the auto-inserted scale filters
  838. char *resample_lavr_opts; ///< libavresample options to use for the auto-inserted resample filters
  839. #if FF_API_FOO_COUNT
  840. unsigned nb_filters;
  841. #endif
  842. /**
  843. * Type of multithreading allowed for filters in this graph. A combination
  844. * of AVFILTER_THREAD_* flags.
  845. *
  846. * May be set by the caller at any point, the setting will apply to all
  847. * filters initialized after that. The default is allowing everything.
  848. *
  849. * When a filter in this graph is initialized, this field is combined using
  850. * bit AND with AVFilterContext.thread_type to get the final mask used for
  851. * determining allowed threading types. I.e. a threading type needs to be
  852. * set in both to be allowed.
  853. */
  854. int thread_type;
  855. /**
  856. * Maximum number of threads used by filters in this graph. May be set by
  857. * the caller before adding any filters to the filtergraph. Zero (the
  858. * default) means that the number of threads is determined automatically.
  859. */
  860. int nb_threads;
  861. /**
  862. * Opaque object for libavfilter internal use.
  863. */
  864. AVFilterGraphInternal *internal;
  865. /**
  866. * Opaque user data. May be set by the caller to an arbitrary value, e.g. to
  867. * be used from callbacks like @ref AVFilterGraph.execute.
  868. * Libavfilter will not touch this field in any way.
  869. */
  870. void *opaque;
  871. /**
  872. * This callback may be set by the caller immediately after allocating the
  873. * graph and before adding any filters to it, to provide a custom
  874. * multithreading implementation.
  875. *
  876. * If set, filters with slice threading capability will call this callback
  877. * to execute multiple jobs in parallel.
  878. *
  879. * If this field is left unset, libavfilter will use its internal
  880. * implementation, which may or may not be multithreaded depending on the
  881. * platform and build options.
  882. */
  883. avfilter_execute_func *execute;
  884. } AVFilterGraph;
  885. /**
  886. * Allocate a filter graph.
  887. */
  888. AVFilterGraph *avfilter_graph_alloc(void);
  889. /**
  890. * Create a new filter instance in a filter graph.
  891. *
  892. * @param graph graph in which the new filter will be used
  893. * @param filter the filter to create an instance of
  894. * @param name Name to give to the new instance (will be copied to
  895. * AVFilterContext.name). This may be used by the caller to identify
  896. * different filters, libavfilter itself assigns no semantics to
  897. * this parameter. May be NULL.
  898. *
  899. * @return the context of the newly created filter instance (note that it is
  900. * also retrievable directly through AVFilterGraph.filters or with
  901. * avfilter_graph_get_filter()) on success or NULL or failure.
  902. */
  903. AVFilterContext *avfilter_graph_alloc_filter(AVFilterGraph *graph,
  904. const AVFilter *filter,
  905. const char *name);
  906. /**
  907. * Get a filter instance with name name from graph.
  908. *
  909. * @return the pointer to the found filter instance or NULL if it
  910. * cannot be found.
  911. */
  912. AVFilterContext *avfilter_graph_get_filter(AVFilterGraph *graph, char *name);
  913. #if FF_API_AVFILTER_OPEN
  914. /**
  915. * Add an existing filter instance to a filter graph.
  916. *
  917. * @param graphctx the filter graph
  918. * @param filter the filter to be added
  919. *
  920. * @deprecated use avfilter_graph_alloc_filter() to allocate a filter in a
  921. * filter graph
  922. */
  923. attribute_deprecated
  924. int avfilter_graph_add_filter(AVFilterGraph *graphctx, AVFilterContext *filter);
  925. #endif
  926. /**
  927. * Create and add a filter instance into an existing graph.
  928. * The filter instance is created from the filter filt and inited
  929. * with the parameters args and opaque.
  930. *
  931. * In case of success put in *filt_ctx the pointer to the created
  932. * filter instance, otherwise set *filt_ctx to NULL.
  933. *
  934. * @param name the instance name to give to the created filter instance
  935. * @param graph_ctx the filter graph
  936. * @return a negative AVERROR error code in case of failure, a non
  937. * negative value otherwise
  938. */
  939. int avfilter_graph_create_filter(AVFilterContext **filt_ctx, const AVFilter *filt,
  940. const char *name, const char *args, void *opaque,
  941. AVFilterGraph *graph_ctx);
  942. /**
  943. * Check validity and configure all the links and formats in the graph.
  944. *
  945. * @param graphctx the filter graph
  946. * @param log_ctx context used for logging
  947. * @return 0 in case of success, a negative AVERROR code otherwise
  948. */
  949. int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx);
  950. /**
  951. * Free a graph, destroy its links, and set *graph to NULL.
  952. * If *graph is NULL, do nothing.
  953. */
  954. void avfilter_graph_free(AVFilterGraph **graph);
  955. /**
  956. * A linked-list of the inputs/outputs of the filter chain.
  957. *
  958. * This is mainly useful for avfilter_graph_parse() / avfilter_graph_parse2(),
  959. * where it is used to communicate open (unlinked) inputs and outputs from and
  960. * to the caller.
  961. * This struct specifies, per each not connected pad contained in the graph, the
  962. * filter context and the pad index required for establishing a link.
  963. */
  964. typedef struct AVFilterInOut {
  965. /** unique name for this input/output in the list */
  966. char *name;
  967. /** filter context associated to this input/output */
  968. AVFilterContext *filter_ctx;
  969. /** index of the filt_ctx pad to use for linking */
  970. int pad_idx;
  971. /** next input/input in the list, NULL if this is the last */
  972. struct AVFilterInOut *next;
  973. } AVFilterInOut;
  974. /**
  975. * Allocate a single AVFilterInOut entry.
  976. * Must be freed with avfilter_inout_free().
  977. * @return allocated AVFilterInOut on success, NULL on failure.
  978. */
  979. AVFilterInOut *avfilter_inout_alloc(void);
  980. /**
  981. * Free the supplied list of AVFilterInOut and set *inout to NULL.
  982. * If *inout is NULL, do nothing.
  983. */
  984. void avfilter_inout_free(AVFilterInOut **inout);
  985. /**
  986. * Add a graph described by a string to a graph.
  987. *
  988. * @param graph the filter graph where to link the parsed graph context
  989. * @param filters string to be parsed
  990. * @param inputs linked list to the inputs of the graph
  991. * @param outputs linked list to the outputs of the graph
  992. * @return zero on success, a negative AVERROR code on error
  993. */
  994. int avfilter_graph_parse(AVFilterGraph *graph, const char *filters,
  995. AVFilterInOut *inputs, AVFilterInOut *outputs,
  996. void *log_ctx);
  997. /**
  998. * Add a graph described by a string to a graph.
  999. *
  1000. * @param[in] graph the filter graph where to link the parsed graph context
  1001. * @param[in] filters string to be parsed
  1002. * @param[out] inputs a linked list of all free (unlinked) inputs of the
  1003. * parsed graph will be returned here. It is to be freed
  1004. * by the caller using avfilter_inout_free().
  1005. * @param[out] outputs a linked list of all free (unlinked) outputs of the
  1006. * parsed graph will be returned here. It is to be freed by the
  1007. * caller using avfilter_inout_free().
  1008. * @return zero on success, a negative AVERROR code on error
  1009. *
  1010. * @note the difference between avfilter_graph_parse2() and
  1011. * avfilter_graph_parse() is that in avfilter_graph_parse(), the caller provides
  1012. * the lists of inputs and outputs, which therefore must be known before calling
  1013. * the function. On the other hand, avfilter_graph_parse2() \em returns the
  1014. * inputs and outputs that are left unlinked after parsing the graph and the
  1015. * caller then deals with them. Another difference is that in
  1016. * avfilter_graph_parse(), the inputs parameter describes inputs of the
  1017. * <em>already existing</em> part of the graph; i.e. from the point of view of
  1018. * the newly created part, they are outputs. Similarly the outputs parameter
  1019. * describes outputs of the already existing filters, which are provided as
  1020. * inputs to the parsed filters.
  1021. * avfilter_graph_parse2() takes the opposite approach -- it makes no reference
  1022. * whatsoever to already existing parts of the graph and the inputs parameter
  1023. * will on return contain inputs of the newly parsed part of the graph.
  1024. * Analogously the outputs parameter will contain outputs of the newly created
  1025. * filters.
  1026. */
  1027. int avfilter_graph_parse2(AVFilterGraph *graph, const char *filters,
  1028. AVFilterInOut **inputs,
  1029. AVFilterInOut **outputs);
  1030. /**
  1031. * @}
  1032. */
  1033. #endif /* AVFILTER_AVFILTER_H */