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.

1270 lines
45KB

  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. * external API header
  27. */
  28. /**
  29. * @defgroup lavfi Libavfilter
  30. * @{
  31. */
  32. #include <stddef.h>
  33. #include "libavutil/avutil.h"
  34. #include "libavutil/dict.h"
  35. #include "libavutil/frame.h"
  36. #include "libavutil/log.h"
  37. #include "libavutil/samplefmt.h"
  38. #include "libavutil/pixfmt.h"
  39. #include "libavutil/rational.h"
  40. #include "libavfilter/version.h"
  41. /**
  42. * Return the LIBAVFILTER_VERSION_INT constant.
  43. */
  44. unsigned avfilter_version(void);
  45. /**
  46. * Return the libavfilter build-time configuration.
  47. */
  48. const char *avfilter_configuration(void);
  49. /**
  50. * Return the libavfilter license.
  51. */
  52. const char *avfilter_license(void);
  53. typedef struct AVFilterContext AVFilterContext;
  54. typedef struct AVFilterLink AVFilterLink;
  55. typedef struct AVFilterPad AVFilterPad;
  56. typedef struct AVFilterFormats AVFilterFormats;
  57. #if FF_API_AVFILTERBUFFER
  58. /**
  59. * A reference-counted buffer data type used by the filter system. Filters
  60. * should not store pointers to this structure directly, but instead use the
  61. * AVFilterBufferRef structure below.
  62. */
  63. typedef struct AVFilterBuffer {
  64. uint8_t *data[8]; ///< buffer data for each plane/channel
  65. /**
  66. * pointers to the data planes/channels.
  67. *
  68. * For video, this should simply point to data[].
  69. *
  70. * For planar audio, each channel has a separate data pointer, and
  71. * linesize[0] contains the size of each channel buffer.
  72. * For packed audio, there is just one data pointer, and linesize[0]
  73. * contains the total size of the buffer for all channels.
  74. *
  75. * Note: Both data and extended_data will always be set, but for planar
  76. * audio with more channels that can fit in data, extended_data must be used
  77. * in order to access all channels.
  78. */
  79. uint8_t **extended_data;
  80. int linesize[8]; ///< number of bytes per line
  81. /** private data to be used by a custom free function */
  82. void *priv;
  83. /**
  84. * A pointer to the function to deallocate this buffer if the default
  85. * function is not sufficient. This could, for example, add the memory
  86. * back into a memory pool to be reused later without the overhead of
  87. * reallocating it from scratch.
  88. */
  89. void (*free)(struct AVFilterBuffer *buf);
  90. int format; ///< media format
  91. int w, h; ///< width and height of the allocated buffer
  92. unsigned refcount; ///< number of references to this buffer
  93. } AVFilterBuffer;
  94. #define AV_PERM_READ 0x01 ///< can read from the buffer
  95. #define AV_PERM_WRITE 0x02 ///< can write to the buffer
  96. #define AV_PERM_PRESERVE 0x04 ///< nobody else can overwrite the buffer
  97. #define AV_PERM_REUSE 0x08 ///< can output the buffer multiple times, with the same contents each time
  98. #define AV_PERM_REUSE2 0x10 ///< can output the buffer multiple times, modified each time
  99. #define AV_PERM_NEG_LINESIZES 0x20 ///< the buffer requested can have negative linesizes
  100. #define AV_PERM_ALIGN 0x40 ///< the buffer must be aligned
  101. #define AVFILTER_ALIGN 16 //not part of ABI
  102. /**
  103. * Audio specific properties in a reference to an AVFilterBuffer. Since
  104. * AVFilterBufferRef is common to different media formats, audio specific
  105. * per reference properties must be separated out.
  106. */
  107. typedef struct AVFilterBufferRefAudioProps {
  108. uint64_t channel_layout; ///< channel layout of audio buffer
  109. int nb_samples; ///< number of audio samples per channel
  110. int sample_rate; ///< audio buffer sample rate
  111. int channels; ///< number of channels (do not access directly)
  112. } AVFilterBufferRefAudioProps;
  113. /**
  114. * Video specific properties in a reference to an AVFilterBuffer. Since
  115. * AVFilterBufferRef is common to different media formats, video specific
  116. * per reference properties must be separated out.
  117. */
  118. typedef struct AVFilterBufferRefVideoProps {
  119. int w; ///< image width
  120. int h; ///< image height
  121. AVRational sample_aspect_ratio; ///< sample aspect ratio
  122. int interlaced; ///< is frame interlaced
  123. int top_field_first; ///< field order
  124. enum AVPictureType pict_type; ///< picture type of the frame
  125. int key_frame; ///< 1 -> keyframe, 0-> not
  126. int qp_table_linesize; ///< qp_table stride
  127. int qp_table_size; ///< qp_table size
  128. int8_t *qp_table; ///< array of Quantization Parameters
  129. } AVFilterBufferRefVideoProps;
  130. /**
  131. * A reference to an AVFilterBuffer. Since filters can manipulate the origin of
  132. * a buffer to, for example, crop image without any memcpy, the buffer origin
  133. * and dimensions are per-reference properties. Linesize is also useful for
  134. * image flipping, frame to field filters, etc, and so is also per-reference.
  135. *
  136. * TODO: add anything necessary for frame reordering
  137. */
  138. typedef struct AVFilterBufferRef {
  139. AVFilterBuffer *buf; ///< the buffer that this is a reference to
  140. uint8_t *data[8]; ///< picture/audio data for each plane
  141. /**
  142. * pointers to the data planes/channels.
  143. *
  144. * For video, this should simply point to data[].
  145. *
  146. * For planar audio, each channel has a separate data pointer, and
  147. * linesize[0] contains the size of each channel buffer.
  148. * For packed audio, there is just one data pointer, and linesize[0]
  149. * contains the total size of the buffer for all channels.
  150. *
  151. * Note: Both data and extended_data will always be set, but for planar
  152. * audio with more channels that can fit in data, extended_data must be used
  153. * in order to access all channels.
  154. */
  155. uint8_t **extended_data;
  156. int linesize[8]; ///< number of bytes per line
  157. AVFilterBufferRefVideoProps *video; ///< video buffer specific properties
  158. AVFilterBufferRefAudioProps *audio; ///< audio buffer specific properties
  159. /**
  160. * presentation timestamp. The time unit may change during
  161. * filtering, as it is specified in the link and the filter code
  162. * may need to rescale the PTS accordingly.
  163. */
  164. int64_t pts;
  165. int64_t pos; ///< byte position in stream, -1 if unknown
  166. int format; ///< media format
  167. int perms; ///< permissions, see the AV_PERM_* flags
  168. enum AVMediaType type; ///< media type of buffer data
  169. AVDictionary *metadata; ///< dictionary containing metadata key=value tags
  170. } AVFilterBufferRef;
  171. /**
  172. * Copy properties of src to dst, without copying the actual data
  173. */
  174. attribute_deprecated
  175. void avfilter_copy_buffer_ref_props(AVFilterBufferRef *dst, AVFilterBufferRef *src);
  176. /**
  177. * Add a new reference to a buffer.
  178. *
  179. * @param ref an existing reference to the buffer
  180. * @param pmask a bitmask containing the allowable permissions in the new
  181. * reference
  182. * @return a new reference to the buffer with the same properties as the
  183. * old, excluding any permissions denied by pmask
  184. */
  185. attribute_deprecated
  186. AVFilterBufferRef *avfilter_ref_buffer(AVFilterBufferRef *ref, int pmask);
  187. /**
  188. * Remove a reference to a buffer. If this is the last reference to the
  189. * buffer, the buffer itself is also automatically freed.
  190. *
  191. * @param ref reference to the buffer, may be NULL
  192. *
  193. * @note it is recommended to use avfilter_unref_bufferp() instead of this
  194. * function
  195. */
  196. attribute_deprecated
  197. void avfilter_unref_buffer(AVFilterBufferRef *ref);
  198. /**
  199. * Remove a reference to a buffer and set the pointer to NULL.
  200. * If this is the last reference to the buffer, the buffer itself
  201. * is also automatically freed.
  202. *
  203. * @param ref pointer to the buffer reference
  204. */
  205. attribute_deprecated
  206. void avfilter_unref_bufferp(AVFilterBufferRef **ref);
  207. #endif
  208. /**
  209. * Get the number of channels of a buffer reference.
  210. */
  211. attribute_deprecated
  212. int avfilter_ref_get_channels(AVFilterBufferRef *ref);
  213. #if FF_API_AVFILTERPAD_PUBLIC
  214. /**
  215. * A filter pad used for either input or output.
  216. *
  217. * See doc/filter_design.txt for details on how to implement the methods.
  218. *
  219. * @warning this struct might be removed from public API.
  220. * users should call avfilter_pad_get_name() and avfilter_pad_get_type()
  221. * to access the name and type fields; there should be no need to access
  222. * any other fields from outside of libavfilter.
  223. */
  224. struct AVFilterPad {
  225. /**
  226. * Pad name. The name is unique among inputs and among outputs, but an
  227. * input may have the same name as an output. This may be NULL if this
  228. * pad has no need to ever be referenced by name.
  229. */
  230. const char *name;
  231. /**
  232. * AVFilterPad type.
  233. */
  234. enum AVMediaType type;
  235. /**
  236. * Input pads:
  237. * Minimum required permissions on incoming buffers. Any buffer with
  238. * insufficient permissions will be automatically copied by the filter
  239. * system to a new buffer which provides the needed access permissions.
  240. *
  241. * Output pads:
  242. * Guaranteed permissions on outgoing buffers. Any buffer pushed on the
  243. * link must have at least these permissions; this fact is checked by
  244. * asserts. It can be used to optimize buffer allocation.
  245. */
  246. attribute_deprecated int min_perms;
  247. /**
  248. * Input pads:
  249. * Permissions which are not accepted on incoming buffers. Any buffer
  250. * which has any of these permissions set will be automatically copied
  251. * by the filter system to a new buffer which does not have those
  252. * permissions. This can be used to easily disallow buffers with
  253. * AV_PERM_REUSE.
  254. *
  255. * Output pads:
  256. * Permissions which are automatically removed on outgoing buffers. It
  257. * can be used to optimize buffer allocation.
  258. */
  259. attribute_deprecated int rej_perms;
  260. /**
  261. * @deprecated unused
  262. */
  263. int (*start_frame)(AVFilterLink *link, AVFilterBufferRef *picref);
  264. /**
  265. * Callback function to get a video buffer. If NULL, the filter system will
  266. * use ff_default_get_video_buffer().
  267. *
  268. * Input video pads only.
  269. */
  270. AVFrame *(*get_video_buffer)(AVFilterLink *link, int w, int h);
  271. /**
  272. * Callback function to get an audio buffer. If NULL, the filter system will
  273. * use ff_default_get_audio_buffer().
  274. *
  275. * Input audio pads only.
  276. */
  277. AVFrame *(*get_audio_buffer)(AVFilterLink *link, int nb_samples);
  278. /**
  279. * @deprecated unused
  280. */
  281. int (*end_frame)(AVFilterLink *link);
  282. /**
  283. * @deprecated unused
  284. */
  285. int (*draw_slice)(AVFilterLink *link, int y, int height, int slice_dir);
  286. /**
  287. * Filtering callback. This is where a filter receives a frame with
  288. * audio/video data and should do its processing.
  289. *
  290. * Input pads only.
  291. *
  292. * @return >= 0 on success, a negative AVERROR on error. This function
  293. * must ensure that frame is properly unreferenced on error if it
  294. * hasn't been passed on to another filter.
  295. */
  296. int (*filter_frame)(AVFilterLink *link, AVFrame *frame);
  297. /**
  298. * Frame poll callback. This returns the number of immediately available
  299. * samples. It should return a positive value if the next request_frame()
  300. * is guaranteed to return one frame (with no delay).
  301. *
  302. * Defaults to just calling the source poll_frame() method.
  303. *
  304. * Output pads only.
  305. */
  306. int (*poll_frame)(AVFilterLink *link);
  307. /**
  308. * Frame request callback. A call to this should result in at least one
  309. * frame being output over the given link. This should return zero on
  310. * success, and another value on error.
  311. * See ff_request_frame() for the error codes with a specific
  312. * meaning.
  313. *
  314. * Output pads only.
  315. */
  316. int (*request_frame)(AVFilterLink *link);
  317. /**
  318. * Link configuration callback.
  319. *
  320. * For output pads, this should set the following link properties:
  321. * video: width, height, sample_aspect_ratio, time_base
  322. * audio: sample_rate.
  323. *
  324. * This should NOT set properties such as format, channel_layout, etc which
  325. * are negotiated between filters by the filter system using the
  326. * query_formats() callback before this function is called.
  327. *
  328. * For input pads, this should check the properties of the link, and update
  329. * the filter's internal state as necessary.
  330. *
  331. * For both input and output pads, this should return zero on success,
  332. * and another value on error.
  333. */
  334. int (*config_props)(AVFilterLink *link);
  335. /**
  336. * The filter expects a fifo to be inserted on its input link,
  337. * typically because it has a delay.
  338. *
  339. * input pads only.
  340. */
  341. int needs_fifo;
  342. int needs_writable;
  343. };
  344. #endif
  345. /**
  346. * Get the number of elements in a NULL-terminated array of AVFilterPads (e.g.
  347. * AVFilter.inputs/outputs).
  348. */
  349. int avfilter_pad_count(const AVFilterPad *pads);
  350. /**
  351. * Get the name of an AVFilterPad.
  352. *
  353. * @param pads an array of AVFilterPads
  354. * @param pad_idx index of the pad in the array it; is the caller's
  355. * responsibility to ensure the index is valid
  356. *
  357. * @return name of the pad_idx'th pad in pads
  358. */
  359. const char *avfilter_pad_get_name(const AVFilterPad *pads, int pad_idx);
  360. /**
  361. * Get the type of an AVFilterPad.
  362. *
  363. * @param pads an array of AVFilterPads
  364. * @param pad_idx index of the pad in the array; it is the caller's
  365. * responsibility to ensure the index is valid
  366. *
  367. * @return type of the pad_idx'th pad in pads
  368. */
  369. enum AVMediaType avfilter_pad_get_type(const AVFilterPad *pads, int pad_idx);
  370. /**
  371. * The number of the filter inputs is not determined just by AVFilter.inputs.
  372. * The filter might add additional inputs during initialization depending on the
  373. * options supplied to it.
  374. */
  375. #define AVFILTER_FLAG_DYNAMIC_INPUTS (1 << 0)
  376. /**
  377. * The number of the filter outputs is not determined just by AVFilter.outputs.
  378. * The filter might add additional outputs during initialization depending on
  379. * the options supplied to it.
  380. */
  381. #define AVFILTER_FLAG_DYNAMIC_OUTPUTS (1 << 1)
  382. /**
  383. * Filter definition. This defines the pads a filter contains, and all the
  384. * callback functions used to interact with the filter.
  385. */
  386. typedef struct AVFilter {
  387. const char *name; ///< filter name
  388. /**
  389. * A description for the filter. You should use the
  390. * NULL_IF_CONFIG_SMALL() macro to define it.
  391. */
  392. const char *description;
  393. const AVFilterPad *inputs; ///< NULL terminated list of inputs. NULL if none
  394. const AVFilterPad *outputs; ///< NULL terminated list of outputs. NULL if none
  395. /**
  396. * A class for the private data, used to access filter private
  397. * AVOptions.
  398. */
  399. const AVClass *priv_class;
  400. /**
  401. * A combination of AVFILTER_FLAG_*
  402. */
  403. int flags;
  404. /*****************************************************************
  405. * All fields below this line are not part of the public API. They
  406. * may not be used outside of libavfilter and can be changed and
  407. * removed at will.
  408. * New public fields should be added right above.
  409. *****************************************************************
  410. */
  411. /**
  412. * Filter initialization function. Called when all the options have been
  413. * set.
  414. */
  415. int (*init)(AVFilterContext *ctx);
  416. /**
  417. * Should be set instead of init by the filters that want to pass a
  418. * dictionary of AVOptions to nested contexts that are allocated in
  419. * init.
  420. */
  421. int (*init_dict)(AVFilterContext *ctx, AVDictionary **options);
  422. /**
  423. * Filter uninitialization function. Should deallocate any memory held
  424. * by the filter, release any buffer references, etc. This does not need
  425. * to deallocate the AVFilterContext->priv memory itself.
  426. */
  427. void (*uninit)(AVFilterContext *ctx);
  428. /**
  429. * Queries formats/layouts supported by the filter and its pads, and sets
  430. * the in_formats/in_chlayouts for links connected to its output pads,
  431. * and out_formats/out_chlayouts for links connected to its input pads.
  432. *
  433. * @return zero on success, a negative value corresponding to an
  434. * AVERROR code otherwise
  435. */
  436. int (*query_formats)(AVFilterContext *);
  437. int priv_size; ///< size of private data to allocate for the filter
  438. struct AVFilter *next;
  439. /**
  440. * Make the filter instance process a command.
  441. *
  442. * @param cmd the command to process, for handling simplicity all commands must be alphanumeric only
  443. * @param arg the argument for the command
  444. * @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.
  445. * @param flags if AVFILTER_CMD_FLAG_FAST is set and the command would be
  446. * time consuming then a filter should treat it like an unsupported command
  447. *
  448. * @returns >=0 on success otherwise an error code.
  449. * AVERROR(ENOSYS) on unsupported commands
  450. */
  451. int (*process_command)(AVFilterContext *, const char *cmd, const char *arg, char *res, int res_len, int flags);
  452. /**
  453. * Filter initialization function, alternative to the init()
  454. * callback. Args contains the user-supplied parameters, opaque is
  455. * used for providing binary data.
  456. */
  457. int (*init_opaque)(AVFilterContext *ctx, void *opaque);
  458. } AVFilter;
  459. /** An instance of a filter */
  460. struct AVFilterContext {
  461. const AVClass *av_class; ///< needed for av_log()
  462. const AVFilter *filter; ///< the AVFilter of which this is an instance
  463. char *name; ///< name of this filter instance
  464. AVFilterPad *input_pads; ///< array of input pads
  465. AVFilterLink **inputs; ///< array of pointers to input links
  466. #if FF_API_FOO_COUNT
  467. unsigned input_count; ///< @deprecated use nb_inputs
  468. #endif
  469. unsigned nb_inputs; ///< number of input pads
  470. AVFilterPad *output_pads; ///< array of output pads
  471. AVFilterLink **outputs; ///< array of pointers to output links
  472. #if FF_API_FOO_COUNT
  473. unsigned output_count; ///< @deprecated use nb_outputs
  474. #endif
  475. unsigned nb_outputs; ///< number of output pads
  476. void *priv; ///< private data for use by the filter
  477. struct AVFilterGraph *graph; ///< filtergraph this filter belongs to
  478. struct AVFilterCommand *command_queue;
  479. };
  480. /**
  481. * A link between two filters. This contains pointers to the source and
  482. * destination filters between which this link exists, and the indexes of
  483. * the pads involved. In addition, this link also contains the parameters
  484. * which have been negotiated and agreed upon between the filter, such as
  485. * image dimensions, format, etc.
  486. */
  487. struct AVFilterLink {
  488. AVFilterContext *src; ///< source filter
  489. AVFilterPad *srcpad; ///< output pad on the source filter
  490. AVFilterContext *dst; ///< dest filter
  491. AVFilterPad *dstpad; ///< input pad on the dest filter
  492. enum AVMediaType type; ///< filter media type
  493. /* These parameters apply only to video */
  494. int w; ///< agreed upon image width
  495. int h; ///< agreed upon image height
  496. AVRational sample_aspect_ratio; ///< agreed upon sample aspect ratio
  497. /* These parameters apply only to audio */
  498. uint64_t channel_layout; ///< channel layout of current buffer (see libavutil/channel_layout.h)
  499. int sample_rate; ///< samples per second
  500. int format; ///< agreed upon media format
  501. /**
  502. * Define the time base used by the PTS of the frames/samples
  503. * which will pass through this link.
  504. * During the configuration stage, each filter is supposed to
  505. * change only the output timebase, while the timebase of the
  506. * input link is assumed to be an unchangeable property.
  507. */
  508. AVRational time_base;
  509. /*****************************************************************
  510. * All fields below this line are not part of the public API. They
  511. * may not be used outside of libavfilter and can be changed and
  512. * removed at will.
  513. * New public fields should be added right above.
  514. *****************************************************************
  515. */
  516. /**
  517. * Lists of formats and channel layouts supported by the input and output
  518. * filters respectively. These lists are used for negotiating the format
  519. * to actually be used, which will be loaded into the format and
  520. * channel_layout members, above, when chosen.
  521. *
  522. */
  523. AVFilterFormats *in_formats;
  524. AVFilterFormats *out_formats;
  525. /**
  526. * Lists of channel layouts and sample rates used for automatic
  527. * negotiation.
  528. */
  529. AVFilterFormats *in_samplerates;
  530. AVFilterFormats *out_samplerates;
  531. struct AVFilterChannelLayouts *in_channel_layouts;
  532. struct AVFilterChannelLayouts *out_channel_layouts;
  533. /**
  534. * Audio only, the destination filter sets this to a non-zero value to
  535. * request that buffers with the given number of samples should be sent to
  536. * it. AVFilterPad.needs_fifo must also be set on the corresponding input
  537. * pad.
  538. * Last buffer before EOF will be padded with silence.
  539. */
  540. int request_samples;
  541. /** stage of the initialization of the link properties (dimensions, etc) */
  542. enum {
  543. AVLINK_UNINIT = 0, ///< not started
  544. AVLINK_STARTINIT, ///< started, but incomplete
  545. AVLINK_INIT ///< complete
  546. } init_state;
  547. struct AVFilterPool *pool;
  548. /**
  549. * Graph the filter belongs to.
  550. */
  551. struct AVFilterGraph *graph;
  552. /**
  553. * Current timestamp of the link, as defined by the most recent
  554. * frame(s), in AV_TIME_BASE units.
  555. */
  556. int64_t current_pts;
  557. /**
  558. * Index in the age array.
  559. */
  560. int age_index;
  561. /**
  562. * Frame rate of the stream on the link, or 1/0 if unknown;
  563. * if left to 0/0, will be automatically be copied from the first input
  564. * of the source filter if it exists.
  565. *
  566. * Sources should set it to the best estimation of the real frame rate.
  567. * Filters should update it if necessary depending on their function.
  568. * Sinks can use it to set a default output frame rate.
  569. * It is similar to the r_frame_rate field in AVStream.
  570. */
  571. AVRational frame_rate;
  572. /**
  573. * Buffer partially filled with samples to achieve a fixed/minimum size.
  574. */
  575. AVFrame *partial_buf;
  576. /**
  577. * Size of the partial buffer to allocate.
  578. * Must be between min_samples and max_samples.
  579. */
  580. int partial_buf_size;
  581. /**
  582. * Minimum number of samples to filter at once. If filter_frame() is
  583. * called with fewer samples, it will accumulate them in partial_buf.
  584. * This field and the related ones must not be changed after filtering
  585. * has started.
  586. * If 0, all related fields are ignored.
  587. */
  588. int min_samples;
  589. /**
  590. * Maximum number of samples to filter at once. If filter_frame() is
  591. * called with more samples, it will split them.
  592. */
  593. int max_samples;
  594. /**
  595. * The buffer reference currently being received across the link by the
  596. * destination filter. This is used internally by the filter system to
  597. * allow automatic copying of buffers which do not have sufficient
  598. * permissions for the destination. This should not be accessed directly
  599. * by the filters.
  600. */
  601. AVFilterBufferRef *cur_buf_copy;
  602. /**
  603. * True if the link is closed.
  604. * If set, all attemps of start_frame, filter_frame or request_frame
  605. * will fail with AVERROR_EOF, and if necessary the reference will be
  606. * destroyed.
  607. * If request_frame returns AVERROR_EOF, this flag is set on the
  608. * corresponding link.
  609. * It can be set also be set by either the source or the destination
  610. * filter.
  611. */
  612. int closed;
  613. /**
  614. * Number of channels.
  615. */
  616. int channels;
  617. /**
  618. * True if a frame is being requested on the link.
  619. * Used internally by the framework.
  620. */
  621. unsigned frame_requested;
  622. /**
  623. * Link processing flags.
  624. */
  625. unsigned flags;
  626. };
  627. /**
  628. * Link two filters together.
  629. *
  630. * @param src the source filter
  631. * @param srcpad index of the output pad on the source filter
  632. * @param dst the destination filter
  633. * @param dstpad index of the input pad on the destination filter
  634. * @return zero on success
  635. */
  636. int avfilter_link(AVFilterContext *src, unsigned srcpad,
  637. AVFilterContext *dst, unsigned dstpad);
  638. /**
  639. * Free the link in *link, and set its pointer to NULL.
  640. */
  641. void avfilter_link_free(AVFilterLink **link);
  642. /**
  643. * Get the number of channels of a link.
  644. */
  645. int avfilter_link_get_channels(AVFilterLink *link);
  646. /**
  647. * Set the closed field of a link.
  648. */
  649. void avfilter_link_set_closed(AVFilterLink *link, int closed);
  650. /**
  651. * Negotiate the media format, dimensions, etc of all inputs to a filter.
  652. *
  653. * @param filter the filter to negotiate the properties for its inputs
  654. * @return zero on successful negotiation
  655. */
  656. int avfilter_config_links(AVFilterContext *filter);
  657. #if FF_API_AVFILTERBUFFER
  658. /**
  659. * Create a buffer reference wrapped around an already allocated image
  660. * buffer.
  661. *
  662. * @param data pointers to the planes of the image to reference
  663. * @param linesize linesizes for the planes of the image to reference
  664. * @param perms the required access permissions
  665. * @param w the width of the image specified by the data and linesize arrays
  666. * @param h the height of the image specified by the data and linesize arrays
  667. * @param format the pixel format of the image specified by the data and linesize arrays
  668. */
  669. attribute_deprecated
  670. AVFilterBufferRef *
  671. avfilter_get_video_buffer_ref_from_arrays(uint8_t * const data[4], const int linesize[4], int perms,
  672. int w, int h, enum AVPixelFormat format);
  673. /**
  674. * Create an audio buffer reference wrapped around an already
  675. * allocated samples buffer.
  676. *
  677. * See avfilter_get_audio_buffer_ref_from_arrays_channels() for a version
  678. * that can handle unknown channel layouts.
  679. *
  680. * @param data pointers to the samples plane buffers
  681. * @param linesize linesize for the samples plane buffers
  682. * @param perms the required access permissions
  683. * @param nb_samples number of samples per channel
  684. * @param sample_fmt the format of each sample in the buffer to allocate
  685. * @param channel_layout the channel layout of the buffer
  686. */
  687. attribute_deprecated
  688. AVFilterBufferRef *avfilter_get_audio_buffer_ref_from_arrays(uint8_t **data,
  689. int linesize,
  690. int perms,
  691. int nb_samples,
  692. enum AVSampleFormat sample_fmt,
  693. uint64_t channel_layout);
  694. /**
  695. * Create an audio buffer reference wrapped around an already
  696. * allocated samples buffer.
  697. *
  698. * @param data pointers to the samples plane buffers
  699. * @param linesize linesize for the samples plane buffers
  700. * @param perms the required access permissions
  701. * @param nb_samples number of samples per channel
  702. * @param sample_fmt the format of each sample in the buffer to allocate
  703. * @param channels the number of channels of the buffer
  704. * @param channel_layout the channel layout of the buffer,
  705. * must be either 0 or consistent with channels
  706. */
  707. attribute_deprecated
  708. AVFilterBufferRef *avfilter_get_audio_buffer_ref_from_arrays_channels(uint8_t **data,
  709. int linesize,
  710. int perms,
  711. int nb_samples,
  712. enum AVSampleFormat sample_fmt,
  713. int channels,
  714. uint64_t channel_layout);
  715. #endif
  716. #define AVFILTER_CMD_FLAG_ONE 1 ///< Stop once a filter understood the command (for target=all for example), fast filters are favored automatically
  717. #define AVFILTER_CMD_FLAG_FAST 2 ///< Only execute command when its fast (like a video out that supports contrast adjustment in hw)
  718. /**
  719. * Make the filter instance process a command.
  720. * It is recommended to use avfilter_graph_send_command().
  721. */
  722. int avfilter_process_command(AVFilterContext *filter, const char *cmd, const char *arg, char *res, int res_len, int flags);
  723. /** Initialize the filter system. Register all builtin filters. */
  724. void avfilter_register_all(void);
  725. #if FF_API_OLD_FILTER_REGISTER
  726. /** Uninitialize the filter system. Unregister all filters. */
  727. attribute_deprecated
  728. void avfilter_uninit(void);
  729. #endif
  730. /**
  731. * Register a filter. This is only needed if you plan to use
  732. * avfilter_get_by_name later to lookup the AVFilter structure by name. A
  733. * filter can still by instantiated with avfilter_graph_alloc_filter even if it
  734. * is not registered.
  735. *
  736. * @param filter the filter to register
  737. * @return 0 if the registration was successful, a negative value
  738. * otherwise
  739. */
  740. int avfilter_register(AVFilter *filter);
  741. /**
  742. * Get a filter definition matching the given name.
  743. *
  744. * @param name the filter name to find
  745. * @return the filter definition, if any matching one is registered.
  746. * NULL if none found.
  747. */
  748. AVFilter *avfilter_get_by_name(const char *name);
  749. /**
  750. * Iterate over all registered filters.
  751. * @return If prev is non-NULL, next registered filter after prev or NULL if
  752. * prev is the last filter. If prev is NULL, return the first registered filter.
  753. */
  754. const AVFilter *avfilter_next(const AVFilter *prev);
  755. #if FF_API_OLD_FILTER_REGISTER
  756. /**
  757. * If filter is NULL, returns a pointer to the first registered filter pointer,
  758. * if filter is non-NULL, returns the next pointer after filter.
  759. * If the returned pointer points to NULL, the last registered filter
  760. * was already reached.
  761. * @deprecated use avfilter_next()
  762. */
  763. attribute_deprecated
  764. AVFilter **av_filter_next(AVFilter **filter);
  765. #endif
  766. #if FF_API_AVFILTER_OPEN
  767. /**
  768. * Create a filter instance.
  769. *
  770. * @param filter_ctx put here a pointer to the created filter context
  771. * on success, NULL on failure
  772. * @param filter the filter to create an instance of
  773. * @param inst_name Name to give to the new instance. Can be NULL for none.
  774. * @return >= 0 in case of success, a negative error code otherwise
  775. * @deprecated use avfilter_graph_alloc_filter() instead
  776. */
  777. attribute_deprecated
  778. int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name);
  779. #endif
  780. #if FF_API_AVFILTER_INIT_FILTER
  781. /**
  782. * Initialize a filter.
  783. *
  784. * @param filter the filter to initialize
  785. * @param args A string of parameters to use when initializing the filter.
  786. * The format and meaning of this string varies by filter.
  787. * @param opaque Any extra non-string data needed by the filter. The meaning
  788. * of this parameter varies by filter.
  789. * @return zero on success
  790. */
  791. attribute_deprecated
  792. int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque);
  793. #endif
  794. /**
  795. * Initialize a filter with the supplied parameters.
  796. *
  797. * @param ctx uninitialized filter context to initialize
  798. * @param args Options to initialize the filter with. This must be a
  799. * ':'-separated list of options in the 'key=value' form.
  800. * May be NULL if the options have been set directly using the
  801. * AVOptions API or there are no options that need to be set.
  802. * @return 0 on success, a negative AVERROR on failure
  803. */
  804. int avfilter_init_str(AVFilterContext *ctx, const char *args);
  805. /**
  806. * Initialize a filter with the supplied dictionary of options.
  807. *
  808. * @param ctx uninitialized filter context to initialize
  809. * @param options An AVDictionary filled with options for this filter. On
  810. * return this parameter will be destroyed and replaced with
  811. * a dict containing options that were not found. This dictionary
  812. * must be freed by the caller.
  813. * May be NULL, then this function is equivalent to
  814. * avfilter_init_str() with the second parameter set to NULL.
  815. * @return 0 on success, a negative AVERROR on failure
  816. *
  817. * @note This function and avfilter_init_str() do essentially the same thing,
  818. * the difference is in manner in which the options are passed. It is up to the
  819. * calling code to choose whichever is more preferable. The two functions also
  820. * behave differently when some of the provided options are not declared as
  821. * supported by the filter. In such a case, avfilter_init_str() will fail, but
  822. * this function will leave those extra options in the options AVDictionary and
  823. * continue as usual.
  824. */
  825. int avfilter_init_dict(AVFilterContext *ctx, AVDictionary **options);
  826. /**
  827. * Free a filter context. This will also remove the filter from its
  828. * filtergraph's list of filters.
  829. *
  830. * @param filter the filter to free
  831. */
  832. void avfilter_free(AVFilterContext *filter);
  833. /**
  834. * Insert a filter in the middle of an existing link.
  835. *
  836. * @param link the link into which the filter should be inserted
  837. * @param filt the filter to be inserted
  838. * @param filt_srcpad_idx the input pad on the filter to connect
  839. * @param filt_dstpad_idx the output pad on the filter to connect
  840. * @return zero on success
  841. */
  842. int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt,
  843. unsigned filt_srcpad_idx, unsigned filt_dstpad_idx);
  844. #if FF_API_AVFILTERBUFFER
  845. /**
  846. * Copy the frame properties of src to dst, without copying the actual
  847. * image data.
  848. *
  849. * @return 0 on success, a negative number on error.
  850. */
  851. attribute_deprecated
  852. int avfilter_copy_frame_props(AVFilterBufferRef *dst, const AVFrame *src);
  853. /**
  854. * Copy the frame properties and data pointers of src to dst, without copying
  855. * the actual data.
  856. *
  857. * @return 0 on success, a negative number on error.
  858. */
  859. attribute_deprecated
  860. int avfilter_copy_buf_props(AVFrame *dst, const AVFilterBufferRef *src);
  861. #endif
  862. /**
  863. * @return AVClass for AVFilterContext.
  864. *
  865. * @see av_opt_find().
  866. */
  867. const AVClass *avfilter_get_class(void);
  868. typedef struct AVFilterGraph {
  869. const AVClass *av_class;
  870. #if FF_API_FOO_COUNT
  871. attribute_deprecated
  872. unsigned filter_count_unused;
  873. #endif
  874. AVFilterContext **filters;
  875. #if !FF_API_FOO_COUNT
  876. unsigned nb_filters;
  877. #endif
  878. char *scale_sws_opts; ///< sws options to use for the auto-inserted scale filters
  879. char *resample_lavr_opts; ///< libavresample options to use for the auto-inserted resample filters
  880. #if FF_API_FOO_COUNT
  881. unsigned nb_filters;
  882. #endif
  883. char *aresample_swr_opts; ///< swr options to use for the auto-inserted aresample filters, Access ONLY through AVOptions
  884. /**
  885. * Private fields
  886. *
  887. * The following fields are for internal use only.
  888. * Their type, offset, number and semantic can change without notice.
  889. */
  890. AVFilterLink **sink_links;
  891. int sink_links_count;
  892. unsigned disable_auto_convert;
  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, AVFilter *filt,
  949. const char *name, const char *args, void *opaque,
  950. AVFilterGraph *graph_ctx);
  951. /**
  952. * Enable or disable automatic format conversion inside the graph.
  953. *
  954. * Note that format conversion can still happen inside explicitly inserted
  955. * scale and aresample filters.
  956. *
  957. * @param flags any of the AVFILTER_AUTO_CONVERT_* constants
  958. */
  959. void avfilter_graph_set_auto_convert(AVFilterGraph *graph, unsigned flags);
  960. enum {
  961. AVFILTER_AUTO_CONVERT_ALL = 0, /**< all automatic conversions enabled */
  962. AVFILTER_AUTO_CONVERT_NONE = -1, /**< all automatic conversions disabled */
  963. };
  964. /**
  965. * Check validity and configure all the links and formats in the graph.
  966. *
  967. * @param graphctx the filter graph
  968. * @param log_ctx context used for logging
  969. * @return 0 in case of success, a negative AVERROR code otherwise
  970. */
  971. int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx);
  972. /**
  973. * Free a graph, destroy its links, and set *graph to NULL.
  974. * If *graph is NULL, do nothing.
  975. */
  976. void avfilter_graph_free(AVFilterGraph **graph);
  977. /**
  978. * A linked-list of the inputs/outputs of the filter chain.
  979. *
  980. * This is mainly useful for avfilter_graph_parse() / avfilter_graph_parse2(),
  981. * where it is used to communicate open (unlinked) inputs and outputs from and
  982. * to the caller.
  983. * This struct specifies, per each not connected pad contained in the graph, the
  984. * filter context and the pad index required for establishing a link.
  985. */
  986. typedef struct AVFilterInOut {
  987. /** unique name for this input/output in the list */
  988. char *name;
  989. /** filter context associated to this input/output */
  990. AVFilterContext *filter_ctx;
  991. /** index of the filt_ctx pad to use for linking */
  992. int pad_idx;
  993. /** next input/input in the list, NULL if this is the last */
  994. struct AVFilterInOut *next;
  995. } AVFilterInOut;
  996. /**
  997. * Allocate a single AVFilterInOut entry.
  998. * Must be freed with avfilter_inout_free().
  999. * @return allocated AVFilterInOut on success, NULL on failure.
  1000. */
  1001. AVFilterInOut *avfilter_inout_alloc(void);
  1002. /**
  1003. * Free the supplied list of AVFilterInOut and set *inout to NULL.
  1004. * If *inout is NULL, do nothing.
  1005. */
  1006. void avfilter_inout_free(AVFilterInOut **inout);
  1007. /**
  1008. * Add a graph described by a string to a graph.
  1009. *
  1010. * @param graph the filter graph where to link the parsed graph context
  1011. * @param filters string to be parsed
  1012. * @param inputs pointer to a linked list to the inputs of the graph, may be NULL.
  1013. * If non-NULL, *inputs is updated to contain the list of open inputs
  1014. * after the parsing, should be freed with avfilter_inout_free().
  1015. * @param outputs pointer to a linked list to the outputs of the graph, may be NULL.
  1016. * If non-NULL, *outputs is updated to contain the list of open outputs
  1017. * after the parsing, should be freed with avfilter_inout_free().
  1018. * @return non negative on success, a negative AVERROR code on error
  1019. */
  1020. int avfilter_graph_parse(AVFilterGraph *graph, const char *filters,
  1021. AVFilterInOut **inputs, AVFilterInOut **outputs,
  1022. void *log_ctx);
  1023. /**
  1024. * Add a graph described by a string to a graph.
  1025. *
  1026. * @param[in] graph the filter graph where to link the parsed graph context
  1027. * @param[in] filters string to be parsed
  1028. * @param[out] inputs a linked list of all free (unlinked) inputs of the
  1029. * parsed graph will be returned here. It is to be freed
  1030. * by the caller using avfilter_inout_free().
  1031. * @param[out] outputs a linked list of all free (unlinked) outputs of the
  1032. * parsed graph will be returned here. It is to be freed by the
  1033. * caller using avfilter_inout_free().
  1034. * @return zero on success, a negative AVERROR code on error
  1035. *
  1036. * @note the difference between avfilter_graph_parse2() and
  1037. * avfilter_graph_parse() is that in avfilter_graph_parse(), the caller provides
  1038. * the lists of inputs and outputs, which therefore must be known before calling
  1039. * the function. On the other hand, avfilter_graph_parse2() \em returns the
  1040. * inputs and outputs that are left unlinked after parsing the graph and the
  1041. * caller then deals with them. Another difference is that in
  1042. * avfilter_graph_parse(), the inputs parameter describes inputs of the
  1043. * <em>already existing</em> part of the graph; i.e. from the point of view of
  1044. * the newly created part, they are outputs. Similarly the outputs parameter
  1045. * describes outputs of the already existing filters, which are provided as
  1046. * inputs to the parsed filters.
  1047. * avfilter_graph_parse2() takes the opposite approach -- it makes no reference
  1048. * whatsoever to already existing parts of the graph and the inputs parameter
  1049. * will on return contain inputs of the newly parsed part of the graph.
  1050. * Analogously the outputs parameter will contain outputs of the newly created
  1051. * filters.
  1052. */
  1053. int avfilter_graph_parse2(AVFilterGraph *graph, const char *filters,
  1054. AVFilterInOut **inputs,
  1055. AVFilterInOut **outputs);
  1056. /**
  1057. * Send a command to one or more filter instances.
  1058. *
  1059. * @param graph the filter graph
  1060. * @param target the filter(s) to which the command should be sent
  1061. * "all" sends to all filters
  1062. * otherwise it can be a filter or filter instance name
  1063. * which will send the command to all matching filters.
  1064. * @param cmd the command to sent, for handling simplicity all commands must be alphanumeric only
  1065. * @param arg the argument for the command
  1066. * @param res a buffer with size res_size where the filter(s) can return a response.
  1067. *
  1068. * @returns >=0 on success otherwise an error code.
  1069. * AVERROR(ENOSYS) on unsupported commands
  1070. */
  1071. int avfilter_graph_send_command(AVFilterGraph *graph, const char *target, const char *cmd, const char *arg, char *res, int res_len, int flags);
  1072. /**
  1073. * Queue a command for one or more filter instances.
  1074. *
  1075. * @param graph the filter graph
  1076. * @param target the filter(s) to which the command should be sent
  1077. * "all" sends to all filters
  1078. * otherwise it can be a filter or filter instance name
  1079. * which will send the command to all matching filters.
  1080. * @param cmd the command to sent, for handling simplicity all commands must be alphanummeric only
  1081. * @param arg the argument for the command
  1082. * @param ts time at which the command should be sent to the filter
  1083. *
  1084. * @note As this executes commands after this function returns, no return code
  1085. * from the filter is provided, also AVFILTER_CMD_FLAG_ONE is not supported.
  1086. */
  1087. int avfilter_graph_queue_command(AVFilterGraph *graph, const char *target, const char *cmd, const char *arg, int flags, double ts);
  1088. /**
  1089. * Dump a graph into a human-readable string representation.
  1090. *
  1091. * @param graph the graph to dump
  1092. * @param options formatting options; currently ignored
  1093. * @return a string, or NULL in case of memory allocation failure;
  1094. * the string must be freed using av_free
  1095. */
  1096. char *avfilter_graph_dump(AVFilterGraph *graph, const char *options);
  1097. /**
  1098. * Request a frame on the oldest sink link.
  1099. *
  1100. * If the request returns AVERROR_EOF, try the next.
  1101. *
  1102. * Note that this function is not meant to be the sole scheduling mechanism
  1103. * of a filtergraph, only a convenience function to help drain a filtergraph
  1104. * in a balanced way under normal circumstances.
  1105. *
  1106. * Also note that AVERROR_EOF does not mean that frames did not arrive on
  1107. * some of the sinks during the process.
  1108. * When there are multiple sink links, in case the requested link
  1109. * returns an EOF, this may cause a filter to flush pending frames
  1110. * which are sent to another sink link, although unrequested.
  1111. *
  1112. * @return the return value of ff_request_frame(),
  1113. * or AVERROR_EOF if all links returned AVERROR_EOF
  1114. */
  1115. int avfilter_graph_request_oldest(AVFilterGraph *graph);
  1116. /**
  1117. * @}
  1118. */
  1119. #endif /* AVFILTER_AVFILTER_H */