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.

1170 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. /**
  319. * The filter expects writable frames from its input link,
  320. * duplicating data buffers if needed.
  321. *
  322. * input pads only.
  323. */
  324. int needs_writable;
  325. };
  326. #endif
  327. /**
  328. * Get the number of elements in a NULL-terminated array of AVFilterPads (e.g.
  329. * AVFilter.inputs/outputs).
  330. */
  331. int avfilter_pad_count(const AVFilterPad *pads);
  332. /**
  333. * Get the name of an AVFilterPad.
  334. *
  335. * @param pads an array of AVFilterPads
  336. * @param pad_idx index of the pad in the array it; is the caller's
  337. * responsibility to ensure the index is valid
  338. *
  339. * @return name of the pad_idx'th pad in pads
  340. */
  341. const char *avfilter_pad_get_name(const AVFilterPad *pads, int pad_idx);
  342. /**
  343. * Get the type of an AVFilterPad.
  344. *
  345. * @param pads an array of AVFilterPads
  346. * @param pad_idx index of the pad in the array; it is the caller's
  347. * responsibility to ensure the index is valid
  348. *
  349. * @return type of the pad_idx'th pad in pads
  350. */
  351. enum AVMediaType avfilter_pad_get_type(const AVFilterPad *pads, int pad_idx);
  352. /**
  353. * The number of the filter inputs is not determined just by AVFilter.inputs.
  354. * The filter might add additional inputs during initialization depending on the
  355. * options supplied to it.
  356. */
  357. #define AVFILTER_FLAG_DYNAMIC_INPUTS (1 << 0)
  358. /**
  359. * The number of the filter outputs is not determined just by AVFilter.outputs.
  360. * The filter might add additional outputs during initialization depending on
  361. * the options supplied to it.
  362. */
  363. #define AVFILTER_FLAG_DYNAMIC_OUTPUTS (1 << 1)
  364. /**
  365. * The filter supports multithreading by splitting frames into multiple parts
  366. * and processing them concurrently.
  367. */
  368. #define AVFILTER_FLAG_SLICE_THREADS (1 << 2)
  369. /**
  370. * Filter definition. This defines the pads a filter contains, and all the
  371. * callback functions used to interact with the filter.
  372. */
  373. typedef struct AVFilter {
  374. /**
  375. * Filter name. Must be non-NULL and unique among filters.
  376. */
  377. const char *name;
  378. /**
  379. * A description of the filter. May be NULL.
  380. *
  381. * You should use the NULL_IF_CONFIG_SMALL() macro to define it.
  382. */
  383. const char *description;
  384. /**
  385. * List of inputs, terminated by a zeroed element.
  386. *
  387. * NULL if there are no (static) inputs. Instances of filters with
  388. * AVFILTER_FLAG_DYNAMIC_INPUTS set may have more inputs than present in
  389. * this list.
  390. */
  391. const AVFilterPad *inputs;
  392. /**
  393. * List of outputs, terminated by a zeroed element.
  394. *
  395. * NULL if there are no (static) outputs. Instances of filters with
  396. * AVFILTER_FLAG_DYNAMIC_OUTPUTS set may have more outputs than present in
  397. * this list.
  398. */
  399. const AVFilterPad *outputs;
  400. /**
  401. * A class for the private data, used to declare filter private AVOptions.
  402. * This field is NULL for filters that do not declare any options.
  403. *
  404. * If this field is non-NULL, the first member of the filter private data
  405. * must be a pointer to AVClass, which will be set by libavfilter generic
  406. * code to this class.
  407. */
  408. const AVClass *priv_class;
  409. /**
  410. * A combination of AVFILTER_FLAG_*
  411. */
  412. int flags;
  413. /*****************************************************************
  414. * All fields below this line are not part of the public API. They
  415. * may not be used outside of libavfilter and can be changed and
  416. * removed at will.
  417. * New public fields should be added right above.
  418. *****************************************************************
  419. */
  420. /**
  421. * Filter initialization function.
  422. *
  423. * This callback will be called only once during the filter lifetime, after
  424. * all the options have been set, but before links between filters are
  425. * established and format negotiation is done.
  426. *
  427. * Basic filter initialization should be done here. Filters with dynamic
  428. * inputs and/or outputs should create those inputs/outputs here based on
  429. * provided options. No more changes to this filter's inputs/outputs can be
  430. * done after this callback.
  431. *
  432. * This callback must not assume that the filter links exist or frame
  433. * parameters are known.
  434. *
  435. * @ref AVFilter.uninit "uninit" is guaranteed to be called even if
  436. * initialization fails, so this callback does not have to clean up on
  437. * failure.
  438. *
  439. * @return 0 on success, a negative AVERROR on failure
  440. */
  441. int (*init)(AVFilterContext *ctx);
  442. /**
  443. * Should be set instead of @ref AVFilter.init "init" by the filters that
  444. * want to pass a dictionary of AVOptions to nested contexts that are
  445. * allocated during init.
  446. *
  447. * On return, the options dict should be freed and replaced with one that
  448. * contains all the options which could not be processed by this filter (or
  449. * with NULL if all the options were processed).
  450. *
  451. * Otherwise the semantics is the same as for @ref AVFilter.init "init".
  452. */
  453. int (*init_dict)(AVFilterContext *ctx, AVDictionary **options);
  454. /**
  455. * Filter uninitialization function.
  456. *
  457. * Called only once right before the filter is freed. Should deallocate any
  458. * memory held by the filter, release any buffer references, etc. It does
  459. * not need to deallocate the AVFilterContext.priv memory itself.
  460. *
  461. * This callback may be called even if @ref AVFilter.init "init" was not
  462. * called or failed, so it must be prepared to handle such a situation.
  463. */
  464. void (*uninit)(AVFilterContext *ctx);
  465. /**
  466. * Query formats supported by the filter on its inputs and outputs.
  467. *
  468. * This callback is called after the filter is initialized (so the inputs
  469. * and outputs are fixed), shortly before the format negotiation. This
  470. * callback may be called more than once.
  471. *
  472. * This callback must set AVFilterLink.out_formats on every input link and
  473. * AVFilterLink.in_formats on every output link to a list of pixel/sample
  474. * formats that the filter supports on that link. For audio links, this
  475. * filter must also set @ref AVFilterLink.in_samplerates "in_samplerates" /
  476. * @ref AVFilterLink.out_samplerates "out_samplerates" and
  477. * @ref AVFilterLink.in_channel_layouts "in_channel_layouts" /
  478. * @ref AVFilterLink.out_channel_layouts "out_channel_layouts" analogously.
  479. *
  480. * This callback may be NULL for filters with one input, in which case
  481. * libavfilter assumes that it supports all input formats and preserves
  482. * them on output.
  483. *
  484. * @return zero on success, a negative value corresponding to an
  485. * AVERROR code otherwise
  486. */
  487. int (*query_formats)(AVFilterContext *);
  488. int priv_size; ///< size of private data to allocate for the filter
  489. /**
  490. * Used by the filter registration system. Must not be touched by any other
  491. * code.
  492. */
  493. struct AVFilter *next;
  494. } AVFilter;
  495. /**
  496. * Process multiple parts of the frame concurrently.
  497. */
  498. #define AVFILTER_THREAD_SLICE (1 << 0)
  499. typedef struct AVFilterInternal AVFilterInternal;
  500. /** An instance of a filter */
  501. struct AVFilterContext {
  502. const AVClass *av_class; ///< needed for av_log()
  503. const AVFilter *filter; ///< the AVFilter of which this is an instance
  504. char *name; ///< name of this filter instance
  505. AVFilterPad *input_pads; ///< array of input pads
  506. AVFilterLink **inputs; ///< array of pointers to input links
  507. #if FF_API_FOO_COUNT
  508. attribute_deprecated unsigned input_count; ///< @deprecated use nb_inputs
  509. #endif
  510. unsigned nb_inputs; ///< number of input pads
  511. AVFilterPad *output_pads; ///< array of output pads
  512. AVFilterLink **outputs; ///< array of pointers to output links
  513. #if FF_API_FOO_COUNT
  514. attribute_deprecated unsigned output_count; ///< @deprecated use nb_outputs
  515. #endif
  516. unsigned nb_outputs; ///< number of output pads
  517. void *priv; ///< private data for use by the filter
  518. struct AVFilterGraph *graph; ///< filtergraph this filter belongs to
  519. /**
  520. * Type of multithreading being allowed/used. A combination of
  521. * AVFILTER_THREAD_* flags.
  522. *
  523. * May be set by the caller before initializing the filter to forbid some
  524. * or all kinds of multithreading for this filter. The default is allowing
  525. * everything.
  526. *
  527. * When the filter is initialized, this field is combined using bit AND with
  528. * AVFilterGraph.thread_type to get the final mask used for determining
  529. * allowed threading types. I.e. a threading type needs to be set in both
  530. * to be allowed.
  531. *
  532. * After the filter is initialzed, libavfilter sets this field to the
  533. * threading type that is actually used (0 for no multithreading).
  534. */
  535. int thread_type;
  536. /**
  537. * An opaque struct for libavfilter internal use.
  538. */
  539. AVFilterInternal *internal;
  540. };
  541. /**
  542. * A link between two filters. This contains pointers to the source and
  543. * destination filters between which this link exists, and the indexes of
  544. * the pads involved. In addition, this link also contains the parameters
  545. * which have been negotiated and agreed upon between the filter, such as
  546. * image dimensions, format, etc.
  547. */
  548. struct AVFilterLink {
  549. AVFilterContext *src; ///< source filter
  550. AVFilterPad *srcpad; ///< output pad on the source filter
  551. AVFilterContext *dst; ///< dest filter
  552. AVFilterPad *dstpad; ///< input pad on the dest filter
  553. enum AVMediaType type; ///< filter media type
  554. /* These parameters apply only to video */
  555. int w; ///< agreed upon image width
  556. int h; ///< agreed upon image height
  557. AVRational sample_aspect_ratio; ///< agreed upon sample aspect ratio
  558. /* These two parameters apply only to audio */
  559. uint64_t channel_layout; ///< channel layout of current buffer (see libavutil/channel_layout.h)
  560. int sample_rate; ///< samples per second
  561. int format; ///< agreed upon media format
  562. /**
  563. * Define the time base used by the PTS of the frames/samples
  564. * which will pass through this link.
  565. * During the configuration stage, each filter is supposed to
  566. * change only the output timebase, while the timebase of the
  567. * input link is assumed to be an unchangeable property.
  568. */
  569. AVRational time_base;
  570. /*****************************************************************
  571. * All fields below this line are not part of the public API. They
  572. * may not be used outside of libavfilter and can be changed and
  573. * removed at will.
  574. * New public fields should be added right above.
  575. *****************************************************************
  576. */
  577. /**
  578. * Lists of formats supported by the input and output filters respectively.
  579. * These lists are used for negotiating the format to actually be used,
  580. * which will be loaded into the format member, above, when chosen.
  581. */
  582. AVFilterFormats *in_formats;
  583. AVFilterFormats *out_formats;
  584. /**
  585. * Lists of channel layouts and sample rates used for automatic
  586. * negotiation.
  587. */
  588. AVFilterFormats *in_samplerates;
  589. AVFilterFormats *out_samplerates;
  590. struct AVFilterChannelLayouts *in_channel_layouts;
  591. struct AVFilterChannelLayouts *out_channel_layouts;
  592. /**
  593. * Audio only, the destination filter sets this to a non-zero value to
  594. * request that buffers with the given number of samples should be sent to
  595. * it. AVFilterPad.needs_fifo must also be set on the corresponding input
  596. * pad.
  597. * Last buffer before EOF will be padded with silence.
  598. */
  599. int request_samples;
  600. /** stage of the initialization of the link properties (dimensions, etc) */
  601. enum {
  602. AVLINK_UNINIT = 0, ///< not started
  603. AVLINK_STARTINIT, ///< started, but incomplete
  604. AVLINK_INIT ///< complete
  605. } init_state;
  606. };
  607. /**
  608. * Link two filters together.
  609. *
  610. * @param src the source filter
  611. * @param srcpad index of the output pad on the source filter
  612. * @param dst the destination filter
  613. * @param dstpad index of the input pad on the destination filter
  614. * @return zero on success
  615. */
  616. int avfilter_link(AVFilterContext *src, unsigned srcpad,
  617. AVFilterContext *dst, unsigned dstpad);
  618. /**
  619. * Negotiate the media format, dimensions, etc of all inputs to a filter.
  620. *
  621. * @param filter the filter to negotiate the properties for its inputs
  622. * @return zero on successful negotiation
  623. */
  624. int avfilter_config_links(AVFilterContext *filter);
  625. #if FF_API_AVFILTERBUFFER
  626. /**
  627. * Create a buffer reference wrapped around an already allocated image
  628. * buffer.
  629. *
  630. * @param data pointers to the planes of the image to reference
  631. * @param linesize linesizes for the planes of the image to reference
  632. * @param perms the required access permissions
  633. * @param w the width of the image specified by the data and linesize arrays
  634. * @param h the height of the image specified by the data and linesize arrays
  635. * @param format the pixel format of the image specified by the data and linesize arrays
  636. */
  637. attribute_deprecated
  638. AVFilterBufferRef *
  639. avfilter_get_video_buffer_ref_from_arrays(uint8_t *data[4], int linesize[4], int perms,
  640. int w, int h, enum AVPixelFormat format);
  641. /**
  642. * Create an audio buffer reference wrapped around an already
  643. * allocated samples buffer.
  644. *
  645. * @param data pointers to the samples plane buffers
  646. * @param linesize linesize for the samples plane buffers
  647. * @param perms the required access permissions
  648. * @param nb_samples number of samples per channel
  649. * @param sample_fmt the format of each sample in the buffer to allocate
  650. * @param channel_layout the channel layout of the buffer
  651. */
  652. attribute_deprecated
  653. AVFilterBufferRef *avfilter_get_audio_buffer_ref_from_arrays(uint8_t **data,
  654. int linesize,
  655. int perms,
  656. int nb_samples,
  657. enum AVSampleFormat sample_fmt,
  658. uint64_t channel_layout);
  659. #endif
  660. /** Initialize the filter system. Register all builtin filters. */
  661. void avfilter_register_all(void);
  662. #if FF_API_OLD_FILTER_REGISTER
  663. /** Uninitialize the filter system. Unregister all filters. */
  664. attribute_deprecated
  665. void avfilter_uninit(void);
  666. #endif
  667. /**
  668. * Register a filter. This is only needed if you plan to use
  669. * avfilter_get_by_name later to lookup the AVFilter structure by name. A
  670. * filter can still by instantiated with avfilter_graph_alloc_filter even if it
  671. * is not registered.
  672. *
  673. * @param filter the filter to register
  674. * @return 0 if the registration was succesfull, a negative value
  675. * otherwise
  676. */
  677. int avfilter_register(AVFilter *filter);
  678. /**
  679. * Get a filter definition matching the given name.
  680. *
  681. * @param name the filter name to find
  682. * @return the filter definition, if any matching one is registered.
  683. * NULL if none found.
  684. */
  685. #if !FF_API_NOCONST_GET_NAME
  686. const
  687. #endif
  688. AVFilter *avfilter_get_by_name(const char *name);
  689. /**
  690. * Iterate over all registered filters.
  691. * @return If prev is non-NULL, next registered filter after prev or NULL if
  692. * prev is the last filter. If prev is NULL, return the first registered filter.
  693. */
  694. const AVFilter *avfilter_next(const AVFilter *prev);
  695. #if FF_API_OLD_FILTER_REGISTER
  696. /**
  697. * If filter is NULL, returns a pointer to the first registered filter pointer,
  698. * if filter is non-NULL, returns the next pointer after filter.
  699. * If the returned pointer points to NULL, the last registered filter
  700. * was already reached.
  701. * @deprecated use avfilter_next()
  702. */
  703. attribute_deprecated
  704. AVFilter **av_filter_next(AVFilter **filter);
  705. #endif
  706. #if FF_API_AVFILTER_OPEN
  707. /**
  708. * Create a filter instance.
  709. *
  710. * @param filter_ctx put here a pointer to the created filter context
  711. * on success, NULL on failure
  712. * @param filter the filter to create an instance of
  713. * @param inst_name Name to give to the new instance. Can be NULL for none.
  714. * @return >= 0 in case of success, a negative error code otherwise
  715. * @deprecated use avfilter_graph_alloc_filter() instead
  716. */
  717. attribute_deprecated
  718. int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name);
  719. #endif
  720. #if FF_API_AVFILTER_INIT_FILTER
  721. /**
  722. * Initialize a filter.
  723. *
  724. * @param filter the filter to initialize
  725. * @param args A string of parameters to use when initializing the filter.
  726. * The format and meaning of this string varies by filter.
  727. * @param opaque Any extra non-string data needed by the filter. The meaning
  728. * of this parameter varies by filter.
  729. * @return zero on success
  730. */
  731. attribute_deprecated
  732. int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque);
  733. #endif
  734. /**
  735. * Initialize a filter with the supplied parameters.
  736. *
  737. * @param ctx uninitialized filter context to initialize
  738. * @param args Options to initialize the filter with. This must be a
  739. * ':'-separated list of options in the 'key=value' form.
  740. * May be NULL if the options have been set directly using the
  741. * AVOptions API or there are no options that need to be set.
  742. * @return 0 on success, a negative AVERROR on failure
  743. */
  744. int avfilter_init_str(AVFilterContext *ctx, const char *args);
  745. /**
  746. * Initialize a filter with the supplied dictionary of options.
  747. *
  748. * @param ctx uninitialized filter context to initialize
  749. * @param options An AVDictionary filled with options for this filter. On
  750. * return this parameter will be destroyed and replaced with
  751. * a dict containing options that were not found. This dictionary
  752. * must be freed by the caller.
  753. * May be NULL, then this function is equivalent to
  754. * avfilter_init_str() with the second parameter set to NULL.
  755. * @return 0 on success, a negative AVERROR on failure
  756. *
  757. * @note This function and avfilter_init_str() do essentially the same thing,
  758. * the difference is in manner in which the options are passed. It is up to the
  759. * calling code to choose whichever is more preferable. The two functions also
  760. * behave differently when some of the provided options are not declared as
  761. * supported by the filter. In such a case, avfilter_init_str() will fail, but
  762. * this function will leave those extra options in the options AVDictionary and
  763. * continue as usual.
  764. */
  765. int avfilter_init_dict(AVFilterContext *ctx, AVDictionary **options);
  766. /**
  767. * Free a filter context. This will also remove the filter from its
  768. * filtergraph's list of filters.
  769. *
  770. * @param filter the filter to free
  771. */
  772. void avfilter_free(AVFilterContext *filter);
  773. /**
  774. * Insert a filter in the middle of an existing link.
  775. *
  776. * @param link the link into which the filter should be inserted
  777. * @param filt the filter to be inserted
  778. * @param filt_srcpad_idx the input pad on the filter to connect
  779. * @param filt_dstpad_idx the output pad on the filter to connect
  780. * @return zero on success
  781. */
  782. int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt,
  783. unsigned filt_srcpad_idx, unsigned filt_dstpad_idx);
  784. #if FF_API_AVFILTERBUFFER
  785. /**
  786. * Copy the frame properties of src to dst, without copying the actual
  787. * image data.
  788. *
  789. * @return 0 on success, a negative number on error.
  790. */
  791. attribute_deprecated
  792. int avfilter_copy_frame_props(AVFilterBufferRef *dst, const AVFrame *src);
  793. /**
  794. * Copy the frame properties and data pointers of src to dst, without copying
  795. * the actual data.
  796. *
  797. * @return 0 on success, a negative number on error.
  798. */
  799. attribute_deprecated
  800. int avfilter_copy_buf_props(AVFrame *dst, const AVFilterBufferRef *src);
  801. #endif
  802. /**
  803. * @return AVClass for AVFilterContext.
  804. *
  805. * @see av_opt_find().
  806. */
  807. const AVClass *avfilter_get_class(void);
  808. typedef struct AVFilterGraphInternal AVFilterGraphInternal;
  809. /**
  810. * A function pointer passed to the @ref AVFilterGraph.execute callback to be
  811. * executed multiple times, possibly in parallel.
  812. *
  813. * @param ctx the filter context the job belongs to
  814. * @param arg an opaque parameter passed through from @ref
  815. * AVFilterGraph.execute
  816. * @param jobnr the index of the job being executed
  817. * @param nb_jobs the total number of jobs
  818. *
  819. * @return 0 on success, a negative AVERROR on error
  820. */
  821. typedef int (avfilter_action_func)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
  822. /**
  823. * A function executing multiple jobs, possibly in parallel.
  824. *
  825. * @param ctx the filter context to which the jobs belong
  826. * @param func the function to be called multiple times
  827. * @param arg the argument to be passed to func
  828. * @param ret a nb_jobs-sized array to be filled with return values from each
  829. * invocation of func
  830. * @param nb_jobs the number of jobs to execute
  831. *
  832. * @return 0 on success, a negative AVERROR on error
  833. */
  834. typedef int (avfilter_execute_func)(AVFilterContext *ctx, avfilter_action_func *func,
  835. void *arg, int *ret, int nb_jobs);
  836. typedef struct AVFilterGraph {
  837. const AVClass *av_class;
  838. #if FF_API_FOO_COUNT
  839. attribute_deprecated
  840. unsigned filter_count;
  841. #endif
  842. AVFilterContext **filters;
  843. #if !FF_API_FOO_COUNT
  844. unsigned nb_filters;
  845. #endif
  846. char *scale_sws_opts; ///< sws options to use for the auto-inserted scale filters
  847. char *resample_lavr_opts; ///< libavresample options to use for the auto-inserted resample filters
  848. #if FF_API_FOO_COUNT
  849. unsigned nb_filters;
  850. #endif
  851. /**
  852. * Type of multithreading allowed for filters in this graph. A combination
  853. * of AVFILTER_THREAD_* flags.
  854. *
  855. * May be set by the caller at any point, the setting will apply to all
  856. * filters initialized after that. The default is allowing everything.
  857. *
  858. * When a filter in this graph is initialized, this field is combined using
  859. * bit AND with AVFilterContext.thread_type to get the final mask used for
  860. * determining allowed threading types. I.e. a threading type needs to be
  861. * set in both to be allowed.
  862. */
  863. int thread_type;
  864. /**
  865. * Maximum number of threads used by filters in this graph. May be set by
  866. * the caller before adding any filters to the filtergraph. Zero (the
  867. * default) means that the number of threads is determined automatically.
  868. */
  869. int nb_threads;
  870. /**
  871. * Opaque object for libavfilter internal use.
  872. */
  873. AVFilterGraphInternal *internal;
  874. /**
  875. * Opaque user data. May be set by the caller to an arbitrary value, e.g. to
  876. * be used from callbacks like @ref AVFilterGraph.execute.
  877. * Libavfilter will not touch this field in any way.
  878. */
  879. void *opaque;
  880. /**
  881. * This callback may be set by the caller immediately after allocating the
  882. * graph and before adding any filters to it, to provide a custom
  883. * multithreading implementation.
  884. *
  885. * If set, filters with slice threading capability will call this callback
  886. * to execute multiple jobs in parallel.
  887. *
  888. * If this field is left unset, libavfilter will use its internal
  889. * implementation, which may or may not be multithreaded depending on the
  890. * platform and build options.
  891. */
  892. avfilter_execute_func *execute;
  893. } AVFilterGraph;
  894. /**
  895. * Allocate a filter graph.
  896. */
  897. AVFilterGraph *avfilter_graph_alloc(void);
  898. /**
  899. * Create a new filter instance in a filter graph.
  900. *
  901. * @param graph graph in which the new filter will be used
  902. * @param filter the filter to create an instance of
  903. * @param name Name to give to the new instance (will be copied to
  904. * AVFilterContext.name). This may be used by the caller to identify
  905. * different filters, libavfilter itself assigns no semantics to
  906. * this parameter. May be NULL.
  907. *
  908. * @return the context of the newly created filter instance (note that it is
  909. * also retrievable directly through AVFilterGraph.filters or with
  910. * avfilter_graph_get_filter()) on success or NULL or failure.
  911. */
  912. AVFilterContext *avfilter_graph_alloc_filter(AVFilterGraph *graph,
  913. const AVFilter *filter,
  914. const char *name);
  915. /**
  916. * Get a filter instance with name name from graph.
  917. *
  918. * @return the pointer to the found filter instance or NULL if it
  919. * cannot be found.
  920. */
  921. AVFilterContext *avfilter_graph_get_filter(AVFilterGraph *graph, char *name);
  922. #if FF_API_AVFILTER_OPEN
  923. /**
  924. * Add an existing filter instance to a filter graph.
  925. *
  926. * @param graphctx the filter graph
  927. * @param filter the filter to be added
  928. *
  929. * @deprecated use avfilter_graph_alloc_filter() to allocate a filter in a
  930. * filter graph
  931. */
  932. attribute_deprecated
  933. int avfilter_graph_add_filter(AVFilterGraph *graphctx, AVFilterContext *filter);
  934. #endif
  935. /**
  936. * Create and add a filter instance into an existing graph.
  937. * The filter instance is created from the filter filt and inited
  938. * with the parameters args and opaque.
  939. *
  940. * In case of success put in *filt_ctx the pointer to the created
  941. * filter instance, otherwise set *filt_ctx to NULL.
  942. *
  943. * @param name the instance name to give to the created filter instance
  944. * @param graph_ctx the filter graph
  945. * @return a negative AVERROR error code in case of failure, a non
  946. * negative value otherwise
  947. */
  948. int avfilter_graph_create_filter(AVFilterContext **filt_ctx, const AVFilter *filt,
  949. const char *name, const char *args, void *opaque,
  950. AVFilterGraph *graph_ctx);
  951. /**
  952. * Check validity and configure all the links and formats in the graph.
  953. *
  954. * @param graphctx the filter graph
  955. * @param log_ctx context used for logging
  956. * @return 0 in case of success, a negative AVERROR code otherwise
  957. */
  958. int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx);
  959. /**
  960. * Free a graph, destroy its links, and set *graph to NULL.
  961. * If *graph is NULL, do nothing.
  962. */
  963. void avfilter_graph_free(AVFilterGraph **graph);
  964. /**
  965. * A linked-list of the inputs/outputs of the filter chain.
  966. *
  967. * This is mainly useful for avfilter_graph_parse() / avfilter_graph_parse2(),
  968. * where it is used to communicate open (unlinked) inputs and outputs from and
  969. * to the caller.
  970. * This struct specifies, per each not connected pad contained in the graph, the
  971. * filter context and the pad index required for establishing a link.
  972. */
  973. typedef struct AVFilterInOut {
  974. /** unique name for this input/output in the list */
  975. char *name;
  976. /** filter context associated to this input/output */
  977. AVFilterContext *filter_ctx;
  978. /** index of the filt_ctx pad to use for linking */
  979. int pad_idx;
  980. /** next input/input in the list, NULL if this is the last */
  981. struct AVFilterInOut *next;
  982. } AVFilterInOut;
  983. /**
  984. * Allocate a single AVFilterInOut entry.
  985. * Must be freed with avfilter_inout_free().
  986. * @return allocated AVFilterInOut on success, NULL on failure.
  987. */
  988. AVFilterInOut *avfilter_inout_alloc(void);
  989. /**
  990. * Free the supplied list of AVFilterInOut and set *inout to NULL.
  991. * If *inout is NULL, do nothing.
  992. */
  993. void avfilter_inout_free(AVFilterInOut **inout);
  994. /**
  995. * Add a graph described by a string to a graph.
  996. *
  997. * @param graph the filter graph where to link the parsed graph context
  998. * @param filters string to be parsed
  999. * @param inputs linked list to the inputs of the graph
  1000. * @param outputs linked list to the outputs of the graph
  1001. * @return zero on success, a negative AVERROR code on error
  1002. */
  1003. int avfilter_graph_parse(AVFilterGraph *graph, const char *filters,
  1004. AVFilterInOut *inputs, AVFilterInOut *outputs,
  1005. void *log_ctx);
  1006. /**
  1007. * Add a graph described by a string to a graph.
  1008. *
  1009. * @param[in] graph the filter graph where to link the parsed graph context
  1010. * @param[in] filters string to be parsed
  1011. * @param[out] inputs a linked list of all free (unlinked) inputs of the
  1012. * parsed graph will be returned here. It is to be freed
  1013. * by the caller using avfilter_inout_free().
  1014. * @param[out] outputs a linked list of all free (unlinked) outputs of the
  1015. * parsed graph will be returned here. It is to be freed by the
  1016. * caller using avfilter_inout_free().
  1017. * @return zero on success, a negative AVERROR code on error
  1018. *
  1019. * @note the difference between avfilter_graph_parse2() and
  1020. * avfilter_graph_parse() is that in avfilter_graph_parse(), the caller provides
  1021. * the lists of inputs and outputs, which therefore must be known before calling
  1022. * the function. On the other hand, avfilter_graph_parse2() \em returns the
  1023. * inputs and outputs that are left unlinked after parsing the graph and the
  1024. * caller then deals with them. Another difference is that in
  1025. * avfilter_graph_parse(), the inputs parameter describes inputs of the
  1026. * <em>already existing</em> part of the graph; i.e. from the point of view of
  1027. * the newly created part, they are outputs. Similarly the outputs parameter
  1028. * describes outputs of the already existing filters, which are provided as
  1029. * inputs to the parsed filters.
  1030. * avfilter_graph_parse2() takes the opposite approach -- it makes no reference
  1031. * whatsoever to already existing parts of the graph and the inputs parameter
  1032. * will on return contain inputs of the newly parsed part of the graph.
  1033. * Analogously the outputs parameter will contain outputs of the newly created
  1034. * filters.
  1035. */
  1036. int avfilter_graph_parse2(AVFilterGraph *graph, const char *filters,
  1037. AVFilterInOut **inputs,
  1038. AVFilterInOut **outputs);
  1039. /**
  1040. * @}
  1041. */
  1042. #endif /* AVFILTER_AVFILTER_H */