3 * Copyright (c) 2007 Bobby Bingham
5 * This file is part of FFmpeg.
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.
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.
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
22 #ifndef AVFILTER_AVFILTER_H
23 #define AVFILTER_AVFILTER_H
28 * Main libavfilter public API header
32 * @defgroup lavfi Libavfilter - graph-based frame editing library
38 #include "libavutil/attributes.h"
39 #include "libavutil/avutil.h"
40 #include "libavutil/dict.h"
41 #include "libavutil/frame.h"
42 #include "libavutil/log.h"
43 #include "libavutil/samplefmt.h"
44 #include "libavutil/pixfmt.h"
45 #include "libavutil/rational.h"
47 #include "libavfilter/version.h"
50 * Return the LIBAVFILTER_VERSION_INT constant.
52 unsigned avfilter_version(void);
55 * Return the libavfilter build-time configuration.
57 const char *avfilter_configuration(void);
60 * Return the libavfilter license.
62 const char *avfilter_license(void);
64 typedef struct AVFilterContext AVFilterContext;
65 typedef struct AVFilterLink AVFilterLink;
66 typedef struct AVFilterPad AVFilterPad;
67 typedef struct AVFilterFormats AVFilterFormats;
69 #if FF_API_AVFILTERBUFFER
71 * A reference-counted buffer data type used by the filter system. Filters
72 * should not store pointers to this structure directly, but instead use the
73 * AVFilterBufferRef structure below.
75 typedef struct AVFilterBuffer {
76 uint8_t *data[8]; ///< buffer data for each plane/channel
79 * pointers to the data planes/channels.
81 * For video, this should simply point to data[].
83 * For planar audio, each channel has a separate data pointer, and
84 * linesize[0] contains the size of each channel buffer.
85 * For packed audio, there is just one data pointer, and linesize[0]
86 * contains the total size of the buffer for all channels.
88 * Note: Both data and extended_data will always be set, but for planar
89 * audio with more channels that can fit in data, extended_data must be used
90 * in order to access all channels.
92 uint8_t **extended_data;
93 int linesize[8]; ///< number of bytes per line
95 /** private data to be used by a custom free function */
98 * A pointer to the function to deallocate this buffer if the default
99 * function is not sufficient. This could, for example, add the memory
100 * back into a memory pool to be reused later without the overhead of
101 * reallocating it from scratch.
103 void (*free)(struct AVFilterBuffer *buf);
105 int format; ///< media format
106 int w, h; ///< width and height of the allocated buffer
107 unsigned refcount; ///< number of references to this buffer
110 #define AV_PERM_READ 0x01 ///< can read from the buffer
111 #define AV_PERM_WRITE 0x02 ///< can write to the buffer
112 #define AV_PERM_PRESERVE 0x04 ///< nobody else can overwrite the buffer
113 #define AV_PERM_REUSE 0x08 ///< can output the buffer multiple times, with the same contents each time
114 #define AV_PERM_REUSE2 0x10 ///< can output the buffer multiple times, modified each time
115 #define AV_PERM_NEG_LINESIZES 0x20 ///< the buffer requested can have negative linesizes
116 #define AV_PERM_ALIGN 0x40 ///< the buffer must be aligned
118 #define AVFILTER_ALIGN 16 //not part of ABI
121 * Audio specific properties in a reference to an AVFilterBuffer. Since
122 * AVFilterBufferRef is common to different media formats, audio specific
123 * per reference properties must be separated out.
125 typedef struct AVFilterBufferRefAudioProps {
126 uint64_t channel_layout; ///< channel layout of audio buffer
127 int nb_samples; ///< number of audio samples per channel
128 int sample_rate; ///< audio buffer sample rate
129 int channels; ///< number of channels (do not access directly)
130 } AVFilterBufferRefAudioProps;
133 * Video specific properties in a reference to an AVFilterBuffer. Since
134 * AVFilterBufferRef is common to different media formats, video specific
135 * per reference properties must be separated out.
137 typedef struct AVFilterBufferRefVideoProps {
138 int w; ///< image width
139 int h; ///< image height
140 AVRational sample_aspect_ratio; ///< sample aspect ratio
141 int interlaced; ///< is frame interlaced
142 int top_field_first; ///< field order
143 enum AVPictureType pict_type; ///< picture type of the frame
144 int key_frame; ///< 1 -> keyframe, 0-> not
145 int qp_table_linesize; ///< qp_table stride
146 int qp_table_size; ///< qp_table size
147 int8_t *qp_table; ///< array of Quantization Parameters
148 } AVFilterBufferRefVideoProps;
151 * A reference to an AVFilterBuffer. Since filters can manipulate the origin of
152 * a buffer to, for example, crop image without any memcpy, the buffer origin
153 * and dimensions are per-reference properties. Linesize is also useful for
154 * image flipping, frame to field filters, etc, and so is also per-reference.
156 * TODO: add anything necessary for frame reordering
158 typedef struct AVFilterBufferRef {
159 AVFilterBuffer *buf; ///< the buffer that this is a reference to
160 uint8_t *data[8]; ///< picture/audio data for each plane
162 * pointers to the data planes/channels.
164 * For video, this should simply point to data[].
166 * For planar audio, each channel has a separate data pointer, and
167 * linesize[0] contains the size of each channel buffer.
168 * For packed audio, there is just one data pointer, and linesize[0]
169 * contains the total size of the buffer for all channels.
171 * Note: Both data and extended_data will always be set, but for planar
172 * audio with more channels that can fit in data, extended_data must be used
173 * in order to access all channels.
175 uint8_t **extended_data;
176 int linesize[8]; ///< number of bytes per line
178 AVFilterBufferRefVideoProps *video; ///< video buffer specific properties
179 AVFilterBufferRefAudioProps *audio; ///< audio buffer specific properties
182 * presentation timestamp. The time unit may change during
183 * filtering, as it is specified in the link and the filter code
184 * may need to rescale the PTS accordingly.
187 int64_t pos; ///< byte position in stream, -1 if unknown
189 int format; ///< media format
191 int perms; ///< permissions, see the AV_PERM_* flags
193 enum AVMediaType type; ///< media type of buffer data
195 AVDictionary *metadata; ///< dictionary containing metadata key=value tags
199 * Copy properties of src to dst, without copying the actual data
202 void avfilter_copy_buffer_ref_props(AVFilterBufferRef *dst, AVFilterBufferRef *src);
205 * Add a new reference to a buffer.
207 * @param ref an existing reference to the buffer
208 * @param pmask a bitmask containing the allowable permissions in the new
210 * @return a new reference to the buffer with the same properties as the
211 * old, excluding any permissions denied by pmask
214 AVFilterBufferRef *avfilter_ref_buffer(AVFilterBufferRef *ref, int pmask);
217 * Remove a reference to a buffer. If this is the last reference to the
218 * buffer, the buffer itself is also automatically freed.
220 * @param ref reference to the buffer, may be NULL
222 * @note it is recommended to use avfilter_unref_bufferp() instead of this
226 void avfilter_unref_buffer(AVFilterBufferRef *ref);
229 * Remove a reference to a buffer and set the pointer to NULL.
230 * If this is the last reference to the buffer, the buffer itself
231 * is also automatically freed.
233 * @param ref pointer to the buffer reference
236 void avfilter_unref_bufferp(AVFilterBufferRef **ref);
240 * Get the number of channels of a buffer reference.
243 int avfilter_ref_get_channels(AVFilterBufferRef *ref);
245 #if FF_API_AVFILTERPAD_PUBLIC
247 * A filter pad used for either input or output.
249 * See doc/filter_design.txt for details on how to implement the methods.
251 * @warning this struct might be removed from public API.
252 * users should call avfilter_pad_get_name() and avfilter_pad_get_type()
253 * to access the name and type fields; there should be no need to access
254 * any other fields from outside of libavfilter.
258 * Pad name. The name is unique among inputs and among outputs, but an
259 * input may have the same name as an output. This may be NULL if this
260 * pad has no need to ever be referenced by name.
267 enum AVMediaType type;
271 * Minimum required permissions on incoming buffers. Any buffer with
272 * insufficient permissions will be automatically copied by the filter
273 * system to a new buffer which provides the needed access permissions.
276 * Guaranteed permissions on outgoing buffers. Any buffer pushed on the
277 * link must have at least these permissions; this fact is checked by
278 * asserts. It can be used to optimize buffer allocation.
280 attribute_deprecated int min_perms;
284 * Permissions which are not accepted on incoming buffers. Any buffer
285 * which has any of these permissions set will be automatically copied
286 * by the filter system to a new buffer which does not have those
287 * permissions. This can be used to easily disallow buffers with
291 * Permissions which are automatically removed on outgoing buffers. It
292 * can be used to optimize buffer allocation.
294 attribute_deprecated int rej_perms;
299 int (*start_frame)(AVFilterLink *link, AVFilterBufferRef *picref);
302 * Callback function to get a video buffer. If NULL, the filter system will
303 * use ff_default_get_video_buffer().
305 * Input video pads only.
307 AVFrame *(*get_video_buffer)(AVFilterLink *link, int w, int h);
310 * Callback function to get an audio buffer. If NULL, the filter system will
311 * use ff_default_get_audio_buffer().
313 * Input audio pads only.
315 AVFrame *(*get_audio_buffer)(AVFilterLink *link, int nb_samples);
320 int (*end_frame)(AVFilterLink *link);
325 int (*draw_slice)(AVFilterLink *link, int y, int height, int slice_dir);
328 * Filtering callback. This is where a filter receives a frame with
329 * audio/video data and should do its processing.
333 * @return >= 0 on success, a negative AVERROR on error. This function
334 * must ensure that frame is properly unreferenced on error if it
335 * hasn't been passed on to another filter.
337 int (*filter_frame)(AVFilterLink *link, AVFrame *frame);
340 * Frame poll callback. This returns the number of immediately available
341 * samples. It should return a positive value if the next request_frame()
342 * is guaranteed to return one frame (with no delay).
344 * Defaults to just calling the source poll_frame() method.
348 int (*poll_frame)(AVFilterLink *link);
351 * Frame request callback. A call to this should result in at least one
352 * frame being output over the given link. This should return zero on
353 * success, and another value on error.
354 * See ff_request_frame() for the error codes with a specific
359 int (*request_frame)(AVFilterLink *link);
362 * Link configuration callback.
364 * For output pads, this should set the following link properties:
365 * video: width, height, sample_aspect_ratio, time_base
366 * audio: sample_rate.
368 * This should NOT set properties such as format, channel_layout, etc which
369 * are negotiated between filters by the filter system using the
370 * query_formats() callback before this function is called.
372 * For input pads, this should check the properties of the link, and update
373 * the filter's internal state as necessary.
375 * For both input and output pads, this should return zero on success,
376 * and another value on error.
378 int (*config_props)(AVFilterLink *link);
381 * The filter expects a fifo to be inserted on its input link,
382 * typically because it has a delay.
393 * Get the number of elements in a NULL-terminated array of AVFilterPads (e.g.
394 * AVFilter.inputs/outputs).
396 int avfilter_pad_count(const AVFilterPad *pads);
399 * Get the name of an AVFilterPad.
401 * @param pads an array of AVFilterPads
402 * @param pad_idx index of the pad in the array it; is the caller's
403 * responsibility to ensure the index is valid
405 * @return name of the pad_idx'th pad in pads
407 const char *avfilter_pad_get_name(const AVFilterPad *pads, int pad_idx);
410 * Get the type of an AVFilterPad.
412 * @param pads an array of AVFilterPads
413 * @param pad_idx index of the pad in the array; it is the caller's
414 * responsibility to ensure the index is valid
416 * @return type of the pad_idx'th pad in pads
418 enum AVMediaType avfilter_pad_get_type(const AVFilterPad *pads, int pad_idx);
421 * The number of the filter inputs is not determined just by AVFilter.inputs.
422 * The filter might add additional inputs during initialization depending on the
423 * options supplied to it.
425 #define AVFILTER_FLAG_DYNAMIC_INPUTS (1 << 0)
427 * The number of the filter outputs is not determined just by AVFilter.outputs.
428 * The filter might add additional outputs during initialization depending on
429 * the options supplied to it.
431 #define AVFILTER_FLAG_DYNAMIC_OUTPUTS (1 << 1)
433 * The filter supports multithreading by splitting frames into multiple parts
434 * and processing them concurrently.
436 #define AVFILTER_FLAG_SLICE_THREADS (1 << 2)
438 * Some filters support a generic "enable" expression option that can be used
439 * to enable or disable a filter in the timeline. Filters supporting this
440 * option have this flag set. When the enable expression is false, the default
441 * no-op filter_frame() function is called in place of the filter_frame()
442 * callback defined on each input pad, thus the frame is passed unchanged to
445 #define AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC (1 << 16)
447 * Same as AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC, except that the filter will
448 * have its filter_frame() callback(s) called as usual even when the enable
449 * expression is false. The filter will disable filtering within the
450 * filter_frame() callback(s) itself, for example executing code depending on
451 * the AVFilterContext->is_disabled value.
453 #define AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL (1 << 17)
455 * Handy mask to test whether the filter supports or no the timeline feature
456 * (internally or generically).
458 #define AVFILTER_FLAG_SUPPORT_TIMELINE (AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL)
461 * Filter definition. This defines the pads a filter contains, and all the
462 * callback functions used to interact with the filter.
464 typedef struct AVFilter {
466 * Filter name. Must be non-NULL and unique among filters.
471 * A description of the filter. May be NULL.
473 * You should use the NULL_IF_CONFIG_SMALL() macro to define it.
475 const char *description;
478 * List of inputs, terminated by a zeroed element.
480 * NULL if there are no (static) inputs. Instances of filters with
481 * AVFILTER_FLAG_DYNAMIC_INPUTS set may have more inputs than present in
484 const AVFilterPad *inputs;
486 * List of outputs, terminated by a zeroed element.
488 * NULL if there are no (static) outputs. Instances of filters with
489 * AVFILTER_FLAG_DYNAMIC_OUTPUTS set may have more outputs than present in
492 const AVFilterPad *outputs;
495 * A class for the private data, used to declare filter private AVOptions.
496 * This field is NULL for filters that do not declare any options.
498 * If this field is non-NULL, the first member of the filter private data
499 * must be a pointer to AVClass, which will be set by libavfilter generic
500 * code to this class.
502 const AVClass *priv_class;
505 * A combination of AVFILTER_FLAG_*
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
513 * New public fields should be added right above.
514 *****************************************************************
518 * Filter initialization function.
520 * This callback will be called only once during the filter lifetime, after
521 * all the options have been set, but before links between filters are
522 * established and format negotiation is done.
524 * Basic filter initialization should be done here. Filters with dynamic
525 * inputs and/or outputs should create those inputs/outputs here based on
526 * provided options. No more changes to this filter's inputs/outputs can be
527 * done after this callback.
529 * This callback must not assume that the filter links exist or frame
530 * parameters are known.
532 * @ref AVFilter.uninit "uninit" is guaranteed to be called even if
533 * initialization fails, so this callback does not have to clean up on
536 * @return 0 on success, a negative AVERROR on failure
538 int (*init)(AVFilterContext *ctx);
541 * Should be set instead of @ref AVFilter.init "init" by the filters that
542 * want to pass a dictionary of AVOptions to nested contexts that are
543 * allocated during init.
545 * On return, the options dict should be freed and replaced with one that
546 * contains all the options which could not be processed by this filter (or
547 * with NULL if all the options were processed).
549 * Otherwise the semantics is the same as for @ref AVFilter.init "init".
551 int (*init_dict)(AVFilterContext *ctx, AVDictionary **options);
554 * Filter uninitialization function.
556 * Called only once right before the filter is freed. Should deallocate any
557 * memory held by the filter, release any buffer references, etc. It does
558 * not need to deallocate the AVFilterContext.priv memory itself.
560 * This callback may be called even if @ref AVFilter.init "init" was not
561 * called or failed, so it must be prepared to handle such a situation.
563 void (*uninit)(AVFilterContext *ctx);
566 * Query formats supported by the filter on its inputs and outputs.
568 * This callback is called after the filter is initialized (so the inputs
569 * and outputs are fixed), shortly before the format negotiation. This
570 * callback may be called more than once.
572 * This callback must set AVFilterLink.out_formats on every input link and
573 * AVFilterLink.in_formats on every output link to a list of pixel/sample
574 * formats that the filter supports on that link. For audio links, this
575 * filter must also set @ref AVFilterLink.in_samplerates "in_samplerates" /
576 * @ref AVFilterLink.out_samplerates "out_samplerates" and
577 * @ref AVFilterLink.in_channel_layouts "in_channel_layouts" /
578 * @ref AVFilterLink.out_channel_layouts "out_channel_layouts" analogously.
580 * This callback may be NULL for filters with one input, in which case
581 * libavfilter assumes that it supports all input formats and preserves
584 * @return zero on success, a negative value corresponding to an
585 * AVERROR code otherwise
587 int (*query_formats)(AVFilterContext *);
589 int priv_size; ///< size of private data to allocate for the filter
592 * Used by the filter registration system. Must not be touched by any other
595 struct AVFilter *next;
598 * Make the filter instance process a command.
600 * @param cmd the command to process, for handling simplicity all commands must be alphanumeric only
601 * @param arg the argument for the command
602 * @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.
603 * @param flags if AVFILTER_CMD_FLAG_FAST is set and the command would be
604 * time consuming then a filter should treat it like an unsupported command
606 * @returns >=0 on success otherwise an error code.
607 * AVERROR(ENOSYS) on unsupported commands
609 int (*process_command)(AVFilterContext *, const char *cmd, const char *arg, char *res, int res_len, int flags);
612 * Filter initialization function, alternative to the init()
613 * callback. Args contains the user-supplied parameters, opaque is
614 * used for providing binary data.
616 int (*init_opaque)(AVFilterContext *ctx, void *opaque);
620 * Process multiple parts of the frame concurrently.
622 #define AVFILTER_THREAD_SLICE (1 << 0)
624 typedef struct AVFilterInternal AVFilterInternal;
626 /** An instance of a filter */
627 struct AVFilterContext {
628 const AVClass *av_class; ///< needed for av_log() and filters common options
630 const AVFilter *filter; ///< the AVFilter of which this is an instance
632 char *name; ///< name of this filter instance
634 AVFilterPad *input_pads; ///< array of input pads
635 AVFilterLink **inputs; ///< array of pointers to input links
637 attribute_deprecated unsigned input_count; ///< @deprecated use nb_inputs
639 unsigned nb_inputs; ///< number of input pads
641 AVFilterPad *output_pads; ///< array of output pads
642 AVFilterLink **outputs; ///< array of pointers to output links
644 attribute_deprecated unsigned output_count; ///< @deprecated use nb_outputs
646 unsigned nb_outputs; ///< number of output pads
648 void *priv; ///< private data for use by the filter
650 struct AVFilterGraph *graph; ///< filtergraph this filter belongs to
653 * Type of multithreading being allowed/used. A combination of
654 * AVFILTER_THREAD_* flags.
656 * May be set by the caller before initializing the filter to forbid some
657 * or all kinds of multithreading for this filter. The default is allowing
660 * When the filter is initialized, this field is combined using bit AND with
661 * AVFilterGraph.thread_type to get the final mask used for determining
662 * allowed threading types. I.e. a threading type needs to be set in both
665 * After the filter is initialzed, libavfilter sets this field to the
666 * threading type that is actually used (0 for no multithreading).
671 * An opaque struct for libavfilter internal use.
673 AVFilterInternal *internal;
675 struct AVFilterCommand *command_queue;
677 char *enable_str; ///< enable expression string
678 void *enable; ///< parsed expression (AVExpr*)
679 double *var_values; ///< variable values for the enable expression
680 int is_disabled; ///< the enabled state from the last expression evaluation
684 * A link between two filters. This contains pointers to the source and
685 * destination filters between which this link exists, and the indexes of
686 * the pads involved. In addition, this link also contains the parameters
687 * which have been negotiated and agreed upon between the filter, such as
688 * image dimensions, format, etc.
690 struct AVFilterLink {
691 AVFilterContext *src; ///< source filter
692 AVFilterPad *srcpad; ///< output pad on the source filter
694 AVFilterContext *dst; ///< dest filter
695 AVFilterPad *dstpad; ///< input pad on the dest filter
697 enum AVMediaType type; ///< filter media type
699 /* These parameters apply only to video */
700 int w; ///< agreed upon image width
701 int h; ///< agreed upon image height
702 AVRational sample_aspect_ratio; ///< agreed upon sample aspect ratio
703 /* These parameters apply only to audio */
704 uint64_t channel_layout; ///< channel layout of current buffer (see libavutil/channel_layout.h)
705 int sample_rate; ///< samples per second
707 int format; ///< agreed upon media format
710 * Define the time base used by the PTS of the frames/samples
711 * which will pass through this link.
712 * During the configuration stage, each filter is supposed to
713 * change only the output timebase, while the timebase of the
714 * input link is assumed to be an unchangeable property.
716 AVRational time_base;
718 /*****************************************************************
719 * All fields below this line are not part of the public API. They
720 * may not be used outside of libavfilter and can be changed and
722 * New public fields should be added right above.
723 *****************************************************************
726 * Lists of formats and channel layouts supported by the input and output
727 * filters respectively. These lists are used for negotiating the format
728 * to actually be used, which will be loaded into the format and
729 * channel_layout members, above, when chosen.
732 AVFilterFormats *in_formats;
733 AVFilterFormats *out_formats;
736 * Lists of channel layouts and sample rates used for automatic
739 AVFilterFormats *in_samplerates;
740 AVFilterFormats *out_samplerates;
741 struct AVFilterChannelLayouts *in_channel_layouts;
742 struct AVFilterChannelLayouts *out_channel_layouts;
745 * Audio only, the destination filter sets this to a non-zero value to
746 * request that buffers with the given number of samples should be sent to
747 * it. AVFilterPad.needs_fifo must also be set on the corresponding input
749 * Last buffer before EOF will be padded with silence.
753 /** stage of the initialization of the link properties (dimensions, etc) */
755 AVLINK_UNINIT = 0, ///< not started
756 AVLINK_STARTINIT, ///< started, but incomplete
757 AVLINK_INIT ///< complete
760 struct AVFilterPool *pool;
763 * Graph the filter belongs to.
765 struct AVFilterGraph *graph;
768 * Current timestamp of the link, as defined by the most recent
769 * frame(s), in AV_TIME_BASE units.
774 * Index in the age array.
779 * Frame rate of the stream on the link, or 1/0 if unknown;
780 * if left to 0/0, will be automatically be copied from the first input
781 * of the source filter if it exists.
783 * Sources should set it to the best estimation of the real frame rate.
784 * Filters should update it if necessary depending on their function.
785 * Sinks can use it to set a default output frame rate.
786 * It is similar to the r_frame_rate field in AVStream.
788 AVRational frame_rate;
791 * Buffer partially filled with samples to achieve a fixed/minimum size.
793 AVFrame *partial_buf;
796 * Size of the partial buffer to allocate.
797 * Must be between min_samples and max_samples.
799 int partial_buf_size;
802 * Minimum number of samples to filter at once. If filter_frame() is
803 * called with fewer samples, it will accumulate them in partial_buf.
804 * This field and the related ones must not be changed after filtering
806 * If 0, all related fields are ignored.
811 * Maximum number of samples to filter at once. If filter_frame() is
812 * called with more samples, it will split them.
817 * The buffer reference currently being received across the link by the
818 * destination filter. This is used internally by the filter system to
819 * allow automatic copying of buffers which do not have sufficient
820 * permissions for the destination. This should not be accessed directly
823 AVFilterBufferRef *cur_buf_copy;
826 * True if the link is closed.
827 * If set, all attemps of start_frame, filter_frame or request_frame
828 * will fail with AVERROR_EOF, and if necessary the reference will be
830 * If request_frame returns AVERROR_EOF, this flag is set on the
831 * corresponding link.
832 * It can be set also be set by either the source or the destination
838 * Number of channels.
843 * True if a frame is being requested on the link.
844 * Used internally by the framework.
846 unsigned frame_requested;
849 * Link processing flags.
854 * Number of past frames sent through the link.
860 * Link two filters together.
862 * @param src the source filter
863 * @param srcpad index of the output pad on the source filter
864 * @param dst the destination filter
865 * @param dstpad index of the input pad on the destination filter
866 * @return zero on success
868 int avfilter_link(AVFilterContext *src, unsigned srcpad,
869 AVFilterContext *dst, unsigned dstpad);
872 * Free the link in *link, and set its pointer to NULL.
874 void avfilter_link_free(AVFilterLink **link);
877 * Get the number of channels of a link.
879 int avfilter_link_get_channels(AVFilterLink *link);
882 * Set the closed field of a link.
884 void avfilter_link_set_closed(AVFilterLink *link, int closed);
887 * Negotiate the media format, dimensions, etc of all inputs to a filter.
889 * @param filter the filter to negotiate the properties for its inputs
890 * @return zero on successful negotiation
892 int avfilter_config_links(AVFilterContext *filter);
894 #if FF_API_AVFILTERBUFFER
896 * Create a buffer reference wrapped around an already allocated image
899 * @param data pointers to the planes of the image to reference
900 * @param linesize linesizes for the planes of the image to reference
901 * @param perms the required access permissions
902 * @param w the width of the image specified by the data and linesize arrays
903 * @param h the height of the image specified by the data and linesize arrays
904 * @param format the pixel format of the image specified by the data and linesize arrays
908 avfilter_get_video_buffer_ref_from_arrays(uint8_t * const data[4], const int linesize[4], int perms,
909 int w, int h, enum AVPixelFormat format);
912 * Create an audio buffer reference wrapped around an already
913 * allocated samples buffer.
915 * See avfilter_get_audio_buffer_ref_from_arrays_channels() for a version
916 * that can handle unknown channel layouts.
918 * @param data pointers to the samples plane buffers
919 * @param linesize linesize for the samples plane buffers
920 * @param perms the required access permissions
921 * @param nb_samples number of samples per channel
922 * @param sample_fmt the format of each sample in the buffer to allocate
923 * @param channel_layout the channel layout of the buffer
926 AVFilterBufferRef *avfilter_get_audio_buffer_ref_from_arrays(uint8_t **data,
930 enum AVSampleFormat sample_fmt,
931 uint64_t channel_layout);
933 * Create an audio buffer reference wrapped around an already
934 * allocated samples buffer.
936 * @param data pointers to the samples plane buffers
937 * @param linesize linesize for the samples plane buffers
938 * @param perms the required access permissions
939 * @param nb_samples number of samples per channel
940 * @param sample_fmt the format of each sample in the buffer to allocate
941 * @param channels the number of channels of the buffer
942 * @param channel_layout the channel layout of the buffer,
943 * must be either 0 or consistent with channels
946 AVFilterBufferRef *avfilter_get_audio_buffer_ref_from_arrays_channels(uint8_t **data,
950 enum AVSampleFormat sample_fmt,
952 uint64_t channel_layout);
957 #define AVFILTER_CMD_FLAG_ONE 1 ///< Stop once a filter understood the command (for target=all for example), fast filters are favored automatically
958 #define AVFILTER_CMD_FLAG_FAST 2 ///< Only execute command when its fast (like a video out that supports contrast adjustment in hw)
961 * Make the filter instance process a command.
962 * It is recommended to use avfilter_graph_send_command().
964 int avfilter_process_command(AVFilterContext *filter, const char *cmd, const char *arg, char *res, int res_len, int flags);
966 /** Initialize the filter system. Register all builtin filters. */
967 void avfilter_register_all(void);
969 #if FF_API_OLD_FILTER_REGISTER
970 /** Uninitialize the filter system. Unregister all filters. */
972 void avfilter_uninit(void);
976 * Register a filter. This is only needed if you plan to use
977 * avfilter_get_by_name later to lookup the AVFilter structure by name. A
978 * filter can still by instantiated with avfilter_graph_alloc_filter even if it
981 * @param filter the filter to register
982 * @return 0 if the registration was successful, a negative value
985 int avfilter_register(AVFilter *filter);
988 * Get a filter definition matching the given name.
990 * @param name the filter name to find
991 * @return the filter definition, if any matching one is registered.
992 * NULL if none found.
994 AVFilter *avfilter_get_by_name(const char *name);
997 * Iterate over all registered filters.
998 * @return If prev is non-NULL, next registered filter after prev or NULL if
999 * prev is the last filter. If prev is NULL, return the first registered filter.
1001 const AVFilter *avfilter_next(const AVFilter *prev);
1003 #if FF_API_OLD_FILTER_REGISTER
1005 * If filter is NULL, returns a pointer to the first registered filter pointer,
1006 * if filter is non-NULL, returns the next pointer after filter.
1007 * If the returned pointer points to NULL, the last registered filter
1008 * was already reached.
1009 * @deprecated use avfilter_next()
1011 attribute_deprecated
1012 AVFilter **av_filter_next(AVFilter **filter);
1015 #if FF_API_AVFILTER_OPEN
1017 * Create a filter instance.
1019 * @param filter_ctx put here a pointer to the created filter context
1020 * on success, NULL on failure
1021 * @param filter the filter to create an instance of
1022 * @param inst_name Name to give to the new instance. Can be NULL for none.
1023 * @return >= 0 in case of success, a negative error code otherwise
1024 * @deprecated use avfilter_graph_alloc_filter() instead
1026 attribute_deprecated
1027 int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name);
1031 #if FF_API_AVFILTER_INIT_FILTER
1033 * Initialize a filter.
1035 * @param filter the filter to initialize
1036 * @param args A string of parameters to use when initializing the filter.
1037 * The format and meaning of this string varies by filter.
1038 * @param opaque Any extra non-string data needed by the filter. The meaning
1039 * of this parameter varies by filter.
1040 * @return zero on success
1042 attribute_deprecated
1043 int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque);
1047 * Initialize a filter with the supplied parameters.
1049 * @param ctx uninitialized filter context to initialize
1050 * @param args Options to initialize the filter with. This must be a
1051 * ':'-separated list of options in the 'key=value' form.
1052 * May be NULL if the options have been set directly using the
1053 * AVOptions API or there are no options that need to be set.
1054 * @return 0 on success, a negative AVERROR on failure
1056 int avfilter_init_str(AVFilterContext *ctx, const char *args);
1059 * Initialize a filter with the supplied dictionary of options.
1061 * @param ctx uninitialized filter context to initialize
1062 * @param options An AVDictionary filled with options for this filter. On
1063 * return this parameter will be destroyed and replaced with
1064 * a dict containing options that were not found. This dictionary
1065 * must be freed by the caller.
1066 * May be NULL, then this function is equivalent to
1067 * avfilter_init_str() with the second parameter set to NULL.
1068 * @return 0 on success, a negative AVERROR on failure
1070 * @note This function and avfilter_init_str() do essentially the same thing,
1071 * the difference is in manner in which the options are passed. It is up to the
1072 * calling code to choose whichever is more preferable. The two functions also
1073 * behave differently when some of the provided options are not declared as
1074 * supported by the filter. In such a case, avfilter_init_str() will fail, but
1075 * this function will leave those extra options in the options AVDictionary and
1076 * continue as usual.
1078 int avfilter_init_dict(AVFilterContext *ctx, AVDictionary **options);
1081 * Free a filter context. This will also remove the filter from its
1082 * filtergraph's list of filters.
1084 * @param filter the filter to free
1086 void avfilter_free(AVFilterContext *filter);
1089 * Insert a filter in the middle of an existing link.
1091 * @param link the link into which the filter should be inserted
1092 * @param filt the filter to be inserted
1093 * @param filt_srcpad_idx the input pad on the filter to connect
1094 * @param filt_dstpad_idx the output pad on the filter to connect
1095 * @return zero on success
1097 int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt,
1098 unsigned filt_srcpad_idx, unsigned filt_dstpad_idx);
1100 #if FF_API_AVFILTERBUFFER
1102 * Copy the frame properties of src to dst, without copying the actual
1105 * @return 0 on success, a negative number on error.
1107 attribute_deprecated
1108 int avfilter_copy_frame_props(AVFilterBufferRef *dst, const AVFrame *src);
1111 * Copy the frame properties and data pointers of src to dst, without copying
1114 * @return 0 on success, a negative number on error.
1116 attribute_deprecated
1117 int avfilter_copy_buf_props(AVFrame *dst, const AVFilterBufferRef *src);
1121 * @return AVClass for AVFilterContext.
1123 * @see av_opt_find().
1125 const AVClass *avfilter_get_class(void);
1127 typedef struct AVFilterGraphInternal AVFilterGraphInternal;
1129 typedef struct AVFilterGraph {
1130 const AVClass *av_class;
1131 #if FF_API_FOO_COUNT
1132 attribute_deprecated
1133 unsigned filter_count_unused;
1135 AVFilterContext **filters;
1136 #if !FF_API_FOO_COUNT
1137 unsigned nb_filters;
1140 char *scale_sws_opts; ///< sws options to use for the auto-inserted scale filters
1141 char *resample_lavr_opts; ///< libavresample options to use for the auto-inserted resample filters
1142 #if FF_API_FOO_COUNT
1143 unsigned nb_filters;
1147 * Type of multithreading allowed for filters in this graph. A combination
1148 * of AVFILTER_THREAD_* flags.
1150 * May be set by the caller at any point, the setting will apply to all
1151 * filters initialized after that. The default is allowing everything.
1153 * When a filter in this graph is initialized, this field is combined using
1154 * bit AND with AVFilterContext.thread_type to get the final mask used for
1155 * determining allowed threading types. I.e. a threading type needs to be
1156 * set in both to be allowed.
1161 * Maximum number of threads used by filters in this graph. May be set by
1162 * the caller before adding any filters to the filtergraph. Zero (the
1163 * default) means that the number of threads is determined automatically.
1168 * Opaque object for libavfilter internal use.
1170 AVFilterGraphInternal *internal;
1172 char *aresample_swr_opts; ///< swr options to use for the auto-inserted aresample filters, Access ONLY through AVOptions
1177 * The following fields are for internal use only.
1178 * Their type, offset, number and semantic can change without notice.
1181 AVFilterLink **sink_links;
1182 int sink_links_count;
1184 unsigned disable_auto_convert;
1188 * Allocate a filter graph.
1190 AVFilterGraph *avfilter_graph_alloc(void);
1193 * Create a new filter instance in a filter graph.
1195 * @param graph graph in which the new filter will be used
1196 * @param filter the filter to create an instance of
1197 * @param name Name to give to the new instance (will be copied to
1198 * AVFilterContext.name). This may be used by the caller to identify
1199 * different filters, libavfilter itself assigns no semantics to
1200 * this parameter. May be NULL.
1202 * @return the context of the newly created filter instance (note that it is
1203 * also retrievable directly through AVFilterGraph.filters or with
1204 * avfilter_graph_get_filter()) on success or NULL or failure.
1206 AVFilterContext *avfilter_graph_alloc_filter(AVFilterGraph *graph,
1207 const AVFilter *filter,
1211 * Get a filter instance with name name from graph.
1213 * @return the pointer to the found filter instance or NULL if it
1216 AVFilterContext *avfilter_graph_get_filter(AVFilterGraph *graph, char *name);
1218 #if FF_API_AVFILTER_OPEN
1220 * Add an existing filter instance to a filter graph.
1222 * @param graphctx the filter graph
1223 * @param filter the filter to be added
1225 * @deprecated use avfilter_graph_alloc_filter() to allocate a filter in a
1228 attribute_deprecated
1229 int avfilter_graph_add_filter(AVFilterGraph *graphctx, AVFilterContext *filter);
1233 * Create and add a filter instance into an existing graph.
1234 * The filter instance is created from the filter filt and inited
1235 * with the parameters args and opaque.
1237 * In case of success put in *filt_ctx the pointer to the created
1238 * filter instance, otherwise set *filt_ctx to NULL.
1240 * @param name the instance name to give to the created filter instance
1241 * @param graph_ctx the filter graph
1242 * @return a negative AVERROR error code in case of failure, a non
1243 * negative value otherwise
1245 int avfilter_graph_create_filter(AVFilterContext **filt_ctx, AVFilter *filt,
1246 const char *name, const char *args, void *opaque,
1247 AVFilterGraph *graph_ctx);
1250 * Enable or disable automatic format conversion inside the graph.
1252 * Note that format conversion can still happen inside explicitly inserted
1253 * scale and aresample filters.
1255 * @param flags any of the AVFILTER_AUTO_CONVERT_* constants
1257 void avfilter_graph_set_auto_convert(AVFilterGraph *graph, unsigned flags);
1260 AVFILTER_AUTO_CONVERT_ALL = 0, /**< all automatic conversions enabled */
1261 AVFILTER_AUTO_CONVERT_NONE = -1, /**< all automatic conversions disabled */
1265 * Check validity and configure all the links and formats in the graph.
1267 * @param graphctx the filter graph
1268 * @param log_ctx context used for logging
1269 * @return 0 in case of success, a negative AVERROR code otherwise
1271 int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx);
1274 * Free a graph, destroy its links, and set *graph to NULL.
1275 * If *graph is NULL, do nothing.
1277 void avfilter_graph_free(AVFilterGraph **graph);
1280 * A linked-list of the inputs/outputs of the filter chain.
1282 * This is mainly useful for avfilter_graph_parse() / avfilter_graph_parse2(),
1283 * where it is used to communicate open (unlinked) inputs and outputs from and
1285 * This struct specifies, per each not connected pad contained in the graph, the
1286 * filter context and the pad index required for establishing a link.
1288 typedef struct AVFilterInOut {
1289 /** unique name for this input/output in the list */
1292 /** filter context associated to this input/output */
1293 AVFilterContext *filter_ctx;
1295 /** index of the filt_ctx pad to use for linking */
1298 /** next input/input in the list, NULL if this is the last */
1299 struct AVFilterInOut *next;
1303 * Allocate a single AVFilterInOut entry.
1304 * Must be freed with avfilter_inout_free().
1305 * @return allocated AVFilterInOut on success, NULL on failure.
1307 AVFilterInOut *avfilter_inout_alloc(void);
1310 * Free the supplied list of AVFilterInOut and set *inout to NULL.
1311 * If *inout is NULL, do nothing.
1313 void avfilter_inout_free(AVFilterInOut **inout);
1315 #if AV_HAVE_INCOMPATIBLE_LIBAV_ABI || !FF_API_OLD_GRAPH_PARSE
1317 * Add a graph described by a string to a graph.
1319 * @note The caller must provide the lists of inputs and outputs,
1320 * which therefore must be known before calling the function.
1322 * @note The inputs parameter describes inputs of the already existing
1323 * part of the graph; i.e. from the point of view of the newly created
1324 * part, they are outputs. Similarly the outputs parameter describes
1325 * outputs of the already existing filters, which are provided as
1326 * inputs to the parsed filters.
1328 * @param graph the filter graph where to link the parsed grap context
1329 * @param filters string to be parsed
1330 * @param inputs linked list to the inputs of the graph
1331 * @param outputs linked list to the outputs of the graph
1332 * @return zero on success, a negative AVERROR code on error
1334 int avfilter_graph_parse(AVFilterGraph *graph, const char *filters,
1335 AVFilterInOut *inputs, AVFilterInOut *outputs,
1339 * Add a graph described by a string to a graph.
1341 * @param graph the filter graph where to link the parsed graph context
1342 * @param filters string to be parsed
1343 * @param inputs pointer to a linked list to the inputs of the graph, may be NULL.
1344 * If non-NULL, *inputs is updated to contain the list of open inputs
1345 * after the parsing, should be freed with avfilter_inout_free().
1346 * @param outputs pointer to a linked list to the outputs of the graph, may be NULL.
1347 * If non-NULL, *outputs is updated to contain the list of open outputs
1348 * after the parsing, should be freed with avfilter_inout_free().
1349 * @return non negative on success, a negative AVERROR code on error
1350 * @deprecated Use avfilter_graph_parse_ptr() instead.
1352 attribute_deprecated
1353 int avfilter_graph_parse(AVFilterGraph *graph, const char *filters,
1354 AVFilterInOut **inputs, AVFilterInOut **outputs,
1359 * Add a graph described by a string to a graph.
1361 * @param graph the filter graph where to link the parsed graph context
1362 * @param filters string to be parsed
1363 * @param inputs pointer to a linked list to the inputs of the graph, may be NULL.
1364 * If non-NULL, *inputs is updated to contain the list of open inputs
1365 * after the parsing, should be freed with avfilter_inout_free().
1366 * @param outputs pointer to a linked list to the outputs of the graph, may be NULL.
1367 * If non-NULL, *outputs is updated to contain the list of open outputs
1368 * after the parsing, should be freed with avfilter_inout_free().
1369 * @return non negative on success, a negative AVERROR code on error
1371 int avfilter_graph_parse_ptr(AVFilterGraph *graph, const char *filters,
1372 AVFilterInOut **inputs, AVFilterInOut **outputs,
1376 * Add a graph described by a string to a graph.
1378 * @param[in] graph the filter graph where to link the parsed graph context
1379 * @param[in] filters string to be parsed
1380 * @param[out] inputs a linked list of all free (unlinked) inputs of the
1381 * parsed graph will be returned here. It is to be freed
1382 * by the caller using avfilter_inout_free().
1383 * @param[out] outputs a linked list of all free (unlinked) outputs of the
1384 * parsed graph will be returned here. It is to be freed by the
1385 * caller using avfilter_inout_free().
1386 * @return zero on success, a negative AVERROR code on error
1388 * @note This function returns the inputs and outputs that are left
1389 * unlinked after parsing the graph and the caller then deals with
1391 * @note This function makes no reference whatsoever to already
1392 * existing parts of the graph and the inputs parameter will on return
1393 * contain inputs of the newly parsed part of the graph. Analogously
1394 * the outputs parameter will contain outputs of the newly created
1397 int avfilter_graph_parse2(AVFilterGraph *graph, const char *filters,
1398 AVFilterInOut **inputs,
1399 AVFilterInOut **outputs);
1402 * Send a command to one or more filter instances.
1404 * @param graph the filter graph
1405 * @param target the filter(s) to which the command should be sent
1406 * "all" sends to all filters
1407 * otherwise it can be a filter or filter instance name
1408 * which will send the command to all matching filters.
1409 * @param cmd the command to send, for handling simplicity all commands must be alphanumeric only
1410 * @param arg the argument for the command
1411 * @param res a buffer with size res_size where the filter(s) can return a response.
1413 * @returns >=0 on success otherwise an error code.
1414 * AVERROR(ENOSYS) on unsupported commands
1416 int avfilter_graph_send_command(AVFilterGraph *graph, const char *target, const char *cmd, const char *arg, char *res, int res_len, int flags);
1419 * Queue a command for one or more filter instances.
1421 * @param graph the filter graph
1422 * @param target the filter(s) to which the command should be sent
1423 * "all" sends to all filters
1424 * otherwise it can be a filter or filter instance name
1425 * which will send the command to all matching filters.
1426 * @param cmd the command to sent, for handling simplicity all commands must be alphanummeric only
1427 * @param arg the argument for the command
1428 * @param ts time at which the command should be sent to the filter
1430 * @note As this executes commands after this function returns, no return code
1431 * from the filter is provided, also AVFILTER_CMD_FLAG_ONE is not supported.
1433 int avfilter_graph_queue_command(AVFilterGraph *graph, const char *target, const char *cmd, const char *arg, int flags, double ts);
1437 * Dump a graph into a human-readable string representation.
1439 * @param graph the graph to dump
1440 * @param options formatting options; currently ignored
1441 * @return a string, or NULL in case of memory allocation failure;
1442 * the string must be freed using av_free
1444 char *avfilter_graph_dump(AVFilterGraph *graph, const char *options);
1447 * Request a frame on the oldest sink link.
1449 * If the request returns AVERROR_EOF, try the next.
1451 * Note that this function is not meant to be the sole scheduling mechanism
1452 * of a filtergraph, only a convenience function to help drain a filtergraph
1453 * in a balanced way under normal circumstances.
1455 * Also note that AVERROR_EOF does not mean that frames did not arrive on
1456 * some of the sinks during the process.
1457 * When there are multiple sink links, in case the requested link
1458 * returns an EOF, this may cause a filter to flush pending frames
1459 * which are sent to another sink link, although unrequested.
1461 * @return the return value of ff_request_frame(),
1462 * or AVERROR_EOF if all links returned AVERROR_EOF
1464 int avfilter_graph_request_oldest(AVFilterGraph *graph);
1470 #endif /* AVFILTER_AVFILTER_H */