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
27 #include "libavutil/avutil.h"
28 #include "libavutil/log.h"
29 #include "libavutil/samplefmt.h"
30 #include "libavutil/pixfmt.h"
31 #include "libavutil/rational.h"
33 #include "libavfilter/version.h"
36 * Return the LIBAVFILTER_VERSION_INT constant.
38 unsigned avfilter_version(void);
41 * Return the libavfilter build-time configuration.
43 const char *avfilter_configuration(void);
46 * Return the libavfilter license.
48 const char *avfilter_license(void);
51 typedef struct AVFilterContext AVFilterContext;
52 typedef struct AVFilterLink AVFilterLink;
53 typedef struct AVFilterPad AVFilterPad;
54 typedef struct AVFilterFormats AVFilterFormats;
57 * A reference-counted buffer data type used by the filter system. Filters
58 * should not store pointers to this structure directly, but instead use the
59 * AVFilterBufferRef structure below.
61 typedef struct AVFilterBuffer {
62 uint8_t *data[8]; ///< buffer data for each plane/channel
65 * pointers to the data planes/channels.
67 * For video, this should simply point to data[].
69 * For planar audio, each channel has a separate data pointer, and
70 * linesize[0] contains the size of each channel buffer.
71 * For packed audio, there is just one data pointer, and linesize[0]
72 * contains the total size of the buffer for all channels.
74 * Note: Both data and extended_data will always be set, but for planar
75 * audio with more channels that can fit in data, extended_data must be used
76 * in order to access all channels.
78 uint8_t **extended_data;
79 int linesize[8]; ///< number of bytes per line
81 /** private data to be used by a custom free function */
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.
89 void (*free)(struct AVFilterBuffer *buf);
91 int format; ///< media format
92 int w, h; ///< width and height of the allocated buffer
93 unsigned refcount; ///< number of references to this buffer
96 #define AV_PERM_READ 0x01 ///< can read from the buffer
97 #define AV_PERM_WRITE 0x02 ///< can write to the buffer
98 #define AV_PERM_PRESERVE 0x04 ///< nobody else can overwrite the buffer
99 #define AV_PERM_REUSE 0x08 ///< can output the buffer multiple times, with the same contents each time
100 #define AV_PERM_REUSE2 0x10 ///< can output the buffer multiple times, modified each time
101 #define AV_PERM_NEG_LINESIZES 0x20 ///< the buffer requested can have negative linesizes
102 #define AV_PERM_ALIGN 0x40 ///< the buffer must be aligned
104 #define AVFILTER_ALIGN 16 //not part of ABI
107 * Audio specific properties in a reference to an AVFilterBuffer. Since
108 * AVFilterBufferRef is common to different media formats, audio specific
109 * per reference properties must be separated out.
111 typedef struct AVFilterBufferRefAudioProps {
112 uint64_t channel_layout; ///< channel layout of audio buffer
113 int nb_samples; ///< number of audio samples per channel
114 int sample_rate; ///< audio buffer sample rate
115 } AVFilterBufferRefAudioProps;
118 * Video specific properties in a reference to an AVFilterBuffer. Since
119 * AVFilterBufferRef is common to different media formats, video specific
120 * per reference properties must be separated out.
122 typedef struct AVFilterBufferRefVideoProps {
123 int w; ///< image width
124 int h; ///< image height
125 AVRational sample_aspect_ratio; ///< sample aspect ratio
126 int interlaced; ///< is frame interlaced
127 int top_field_first; ///< field order
128 enum AVPictureType pict_type; ///< picture type of the frame
129 int key_frame; ///< 1 -> keyframe, 0-> not
130 } AVFilterBufferRefVideoProps;
133 * A reference to an AVFilterBuffer. Since filters can manipulate the origin of
134 * a buffer to, for example, crop image without any memcpy, the buffer origin
135 * and dimensions are per-reference properties. Linesize is also useful for
136 * image flipping, frame to field filters, etc, and so is also per-reference.
138 * TODO: add anything necessary for frame reordering
140 typedef struct AVFilterBufferRef {
141 AVFilterBuffer *buf; ///< the buffer that this is a reference to
142 uint8_t *data[8]; ///< picture/audio data for each plane
144 * pointers to the data planes/channels.
146 * For video, this should simply point to data[].
148 * For planar audio, each channel has a separate data pointer, and
149 * linesize[0] contains the size of each channel buffer.
150 * For packed audio, there is just one data pointer, and linesize[0]
151 * contains the total size of the buffer for all channels.
153 * Note: Both data and extended_data will always be set, but for planar
154 * audio with more channels that can fit in data, extended_data must be used
155 * in order to access all channels.
157 uint8_t **extended_data;
158 int linesize[8]; ///< number of bytes per line
160 AVFilterBufferRefVideoProps *video; ///< video buffer specific properties
161 AVFilterBufferRefAudioProps *audio; ///< audio buffer specific properties
164 * presentation timestamp. The time unit may change during
165 * filtering, as it is specified in the link and the filter code
166 * may need to rescale the PTS accordingly.
169 int64_t pos; ///< byte position in stream, -1 if unknown
171 int format; ///< media format
173 int perms; ///< permissions, see the AV_PERM_* flags
175 enum AVMediaType type; ///< media type of buffer data
179 * Copy properties of src to dst, without copying the actual data
181 void avfilter_copy_buffer_ref_props(AVFilterBufferRef *dst, AVFilterBufferRef *src);
184 * Add a new reference to a buffer.
186 * @param ref an existing reference to the buffer
187 * @param pmask a bitmask containing the allowable permissions in the new
189 * @return a new reference to the buffer with the same properties as the
190 * old, excluding any permissions denied by pmask
192 AVFilterBufferRef *avfilter_ref_buffer(AVFilterBufferRef *ref, int pmask);
195 * Remove a reference to a buffer. If this is the last reference to the
196 * buffer, the buffer itself is also automatically freed.
198 * @param ref reference to the buffer, may be NULL
200 void avfilter_unref_buffer(AVFilterBufferRef *ref);
203 * Remove a reference to a buffer and set the pointer to NULL.
204 * If this is the last reference to the buffer, the buffer itself
205 * is also automatically freed.
207 * @param ref pointer to the buffer reference
209 void avfilter_unref_bufferp(AVFilterBufferRef **ref);
212 #if FF_API_AVFILTERPAD_PUBLIC
214 * A filter pad used for either input or output.
216 * See doc/filter_design.txt for details on how to implement the methods.
218 * @warning this struct might be removed from public API.
219 * users should call avfilter_pad_get_name() and avfilter_pad_get_type()
220 * to access the name and type fields; there should be no need to access
221 * any other fields from outside of libavfilter.
225 * Pad name. The name is unique among inputs and among outputs, but an
226 * input may have the same name as an output. This may be NULL if this
227 * pad has no need to ever be referenced by name.
234 enum AVMediaType type;
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.
246 * Permissions which are not accepted on incoming buffers. Any buffer
247 * which has any of these permissions set will be automatically copied
248 * by the filter system to a new buffer which does not have those
249 * permissions. This can be used to easily disallow buffers with
257 * Callback called before passing the first slice of a new frame. If
258 * NULL, the filter layer will default to storing a reference to the
259 * picture inside the link structure.
261 * Input video pads only.
263 void (*start_frame)(AVFilterLink *link, AVFilterBufferRef *picref);
266 * Callback function to get a video buffer. If NULL, the filter system will
267 * use avfilter_default_get_video_buffer().
269 * Input video pads only.
271 AVFilterBufferRef *(*get_video_buffer)(AVFilterLink *link, int perms, int w, int h);
274 * Callback function to get an audio buffer. If NULL, the filter system will
275 * use avfilter_default_get_audio_buffer().
277 * Input audio pads only.
279 AVFilterBufferRef *(*get_audio_buffer)(AVFilterLink *link, int perms,
283 * Callback called after the slices of a frame are completely sent. If
284 * NULL, the filter layer will default to releasing the reference stored
285 * in the link structure during start_frame().
287 * Input video pads only.
289 void (*end_frame)(AVFilterLink *link);
292 * Slice drawing callback. This is where a filter receives video data
293 * and should do its processing.
295 * Input video pads only.
297 void (*draw_slice)(AVFilterLink *link, int y, int height, int slice_dir);
300 * Samples filtering callback. This is where a filter receives audio data
301 * and should do its processing.
303 * Input audio pads only.
305 void (*filter_samples)(AVFilterLink *link, AVFilterBufferRef *samplesref);
308 * Frame poll callback. This returns the number of immediately available
309 * samples. It should return a positive value if the next request_frame()
310 * is guaranteed to return one frame (with no delay).
312 * Defaults to just calling the source poll_frame() method.
316 int (*poll_frame)(AVFilterLink *link);
319 * Frame request callback. A call to this should result in at least one
320 * frame being output over the given link. This should return zero on
321 * success, and another value on error.
322 * See ff_request_frame() for the error codes with a specific
327 int (*request_frame)(AVFilterLink *link);
330 * Link configuration callback.
332 * For output pads, this should set the following link properties:
333 * video: width, height, sample_aspect_ratio, time_base
334 * audio: sample_rate.
336 * This should NOT set properties such as format, channel_layout, etc which
337 * are negotiated between filters by the filter system using the
338 * query_formats() callback before this function is called.
340 * For input pads, this should check the properties of the link, and update
341 * the filter's internal state as necessary.
343 * For both input and output pads, this should return zero on success,
344 * and another value on error.
346 int (*config_props)(AVFilterLink *link);
349 * The filter expects a fifo to be inserted on its input link,
350 * typically because it has a delay.
359 * Get the name of an AVFilterPad.
361 * @param pads an array of AVFilterPads
362 * @param pad_idx index of the pad in the array it; is the caller's
363 * responsibility to ensure the index is valid
365 * @return name of the pad_idx'th pad in pads
367 const char *avfilter_pad_get_name(AVFilterPad *pads, int pad_idx);
370 * Get the type of an AVFilterPad.
372 * @param pads an array of AVFilterPads
373 * @param pad_idx index of the pad in the array; it is the caller's
374 * responsibility to ensure the index is valid
376 * @return type of the pad_idx'th pad in pads
378 enum AVMediaType avfilter_pad_get_type(AVFilterPad *pads, int pad_idx);
380 /** default handler for end_frame() for video inputs */
382 void avfilter_default_end_frame(AVFilterLink *link);
385 * Filter definition. This defines the pads a filter contains, and all the
386 * callback functions used to interact with the filter.
388 typedef struct AVFilter {
389 const char *name; ///< filter name
392 * A description for the filter. You should use the
393 * NULL_IF_CONFIG_SMALL() macro to define it.
395 const char *description;
397 const AVFilterPad *inputs; ///< NULL terminated list of inputs. NULL if none
398 const AVFilterPad *outputs; ///< NULL terminated list of outputs. NULL if none
400 /*****************************************************************
401 * All fields below this line are not part of the public API. They
402 * may not be used outside of libavfilter and can be changed and
404 * New public fields should be added right above.
405 *****************************************************************
409 * Filter initialization function. Args contains the user-supplied
410 * parameters. FIXME: maybe an AVOption-based system would be better?
412 int (*init)(AVFilterContext *ctx, const char *args);
415 * Filter uninitialization function. Should deallocate any memory held
416 * by the filter, release any buffer references, etc. This does not need
417 * to deallocate the AVFilterContext->priv memory itself.
419 void (*uninit)(AVFilterContext *ctx);
422 * Queries formats/layouts supported by the filter and its pads, and sets
423 * the in_formats/in_chlayouts for links connected to its output pads,
424 * and out_formats/out_chlayouts for links connected to its input pads.
426 * @return zero on success, a negative value corresponding to an
427 * AVERROR code otherwise
429 int (*query_formats)(AVFilterContext *);
431 int priv_size; ///< size of private data to allocate for the filter
434 * Make the filter instance process a command.
436 * @param cmd the command to process, for handling simplicity all commands must be alphanumeric only
437 * @param arg the argument for the command
438 * @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.
439 * @param flags if AVFILTER_CMD_FLAG_FAST is set and the command would be
440 * time consuming then a filter should treat it like an unsupported command
442 * @returns >=0 on success otherwise an error code.
443 * AVERROR(ENOSYS) on unsupported commands
445 int (*process_command)(AVFilterContext *, const char *cmd, const char *arg, char *res, int res_len, int flags);
448 /** An instance of a filter */
449 struct AVFilterContext {
450 const AVClass *av_class; ///< needed for av_log()
452 AVFilter *filter; ///< the AVFilter of which this is an instance
454 char *name; ///< name of this filter instance
456 AVFilterPad *input_pads; ///< array of input pads
457 AVFilterLink **inputs; ///< array of pointers to input links
459 unsigned input_count; ///< @deprecated use nb_inputs
461 unsigned nb_inputs; ///< number of input pads
463 AVFilterPad *output_pads; ///< array of output pads
464 AVFilterLink **outputs; ///< array of pointers to output links
466 unsigned output_count; ///< @deprecated use nb_outputs
468 unsigned nb_outputs; ///< number of output pads
470 void *priv; ///< private data for use by the filter
472 struct AVFilterCommand *command_queue;
476 * A link between two filters. This contains pointers to the source and
477 * destination filters between which this link exists, and the indexes of
478 * the pads involved. In addition, this link also contains the parameters
479 * which have been negotiated and agreed upon between the filter, such as
480 * image dimensions, format, etc.
482 struct AVFilterLink {
483 AVFilterContext *src; ///< source filter
484 AVFilterPad *srcpad; ///< output pad on the source filter
486 AVFilterContext *dst; ///< dest filter
487 AVFilterPad *dstpad; ///< input pad on the dest filter
489 enum AVMediaType type; ///< filter media type
491 /* These parameters apply only to video */
492 int w; ///< agreed upon image width
493 int h; ///< agreed upon image height
494 AVRational sample_aspect_ratio; ///< agreed upon sample aspect ratio
495 /* These parameters apply only to audio */
496 uint64_t channel_layout; ///< channel layout of current buffer (see libavutil/audioconvert.h)
497 int sample_rate; ///< samples per second
499 int format; ///< agreed upon media format
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.
508 AVRational time_base;
510 /*****************************************************************
511 * All fields below this line are not part of the public API. They
512 * may not be used outside of libavfilter and can be changed and
514 * New public fields should be added right above.
515 *****************************************************************
518 * Lists of formats and channel layouts supported by the input and output
519 * filters respectively. These lists are used for negotiating the format
520 * to actually be used, which will be loaded into the format and
521 * channel_layout members, above, when chosen.
524 AVFilterFormats *in_formats;
525 AVFilterFormats *out_formats;
528 * Lists of channel layouts and sample rates used for automatic
531 AVFilterFormats *in_samplerates;
532 AVFilterFormats *out_samplerates;
533 struct AVFilterChannelLayouts *in_channel_layouts;
534 struct AVFilterChannelLayouts *out_channel_layouts;
537 * Audio only, the destination filter sets this to a non-zero value to
538 * request that buffers with the given number of samples should be sent to
539 * it. AVFilterPad.needs_fifo must also be set on the corresponding input
541 * Last buffer before EOF will be padded with silence.
545 /** stage of the initialization of the link properties (dimensions, etc) */
547 AVLINK_UNINIT = 0, ///< not started
548 AVLINK_STARTINIT, ///< started, but incomplete
549 AVLINK_INIT ///< complete
553 * The buffer reference currently being sent across the link by the source
554 * filter. This is used internally by the filter system to allow
555 * automatic copying of buffers which do not have sufficient permissions
556 * for the destination. This should not be accessed directly by the
559 AVFilterBufferRef *src_buf;
561 AVFilterBufferRef *cur_buf;
562 AVFilterBufferRef *out_buf;
564 struct AVFilterPool *pool;
567 * Graph the filter belongs to.
569 struct AVFilterGraph *graph;
572 * Current timestamp of the link, as defined by the most recent
573 * frame(s), in AV_TIME_BASE units.
578 * Index in the age array.
583 * Frame rate of the stream on the link, or 1/0 if unknown;
584 * if left to 0/0, will be automatically be copied from the first input
585 * of the source filter if it exists.
587 * Sources should set it to the best estimation of the real frame rate.
588 * Filters should update it if necessary depending on their function.
589 * Sinks can use it to set a default output frame rate.
590 * It is similar to the r_frae_rate field in AVStream.
592 AVRational frame_rate;
595 * Buffer partially filled with samples to achieve a fixed/minimum size.
597 AVFilterBufferRef *partial_buf;
600 * Size of the partial buffer to allocate.
601 * Must be between min_samples and max_samples.
603 int partial_buf_size;
606 * Minimum number of samples to filter at once. If filter_samples() is
607 * called with fewer samples, it will accumulate them in partial_buf.
608 * This field and the related ones must not be changed after filtering
610 * If 0, all related fields are ignored.
615 * Maximum number of samples to filter at once. If filter_samples() is
616 * called with more samples, it will split them.
622 * Link two filters together.
624 * @param src the source filter
625 * @param srcpad index of the output pad on the source filter
626 * @param dst the destination filter
627 * @param dstpad index of the input pad on the destination filter
628 * @return zero on success
630 int avfilter_link(AVFilterContext *src, unsigned srcpad,
631 AVFilterContext *dst, unsigned dstpad);
634 * Free the link in *link, and set its pointer to NULL.
636 void avfilter_link_free(AVFilterLink **link);
639 * Negotiate the media format, dimensions, etc of all inputs to a filter.
641 * @param filter the filter to negotiate the properties for its inputs
642 * @return zero on successful negotiation
644 int avfilter_config_links(AVFilterContext *filter);
647 * Create a buffer reference wrapped around an already allocated image
650 * @param data pointers to the planes of the image to reference
651 * @param linesize linesizes for the planes of the image to reference
652 * @param perms the required access permissions
653 * @param w the width of the image specified by the data and linesize arrays
654 * @param h the height of the image specified by the data and linesize arrays
655 * @param format the pixel format of the image specified by the data and linesize arrays
658 avfilter_get_video_buffer_ref_from_arrays(uint8_t * const data[4], const int linesize[4], int perms,
659 int w, int h, enum PixelFormat format);
662 * Create an audio buffer reference wrapped around an already
663 * allocated samples buffer.
665 * @param data pointers to the samples plane buffers
666 * @param linesize linesize for the samples plane buffers
667 * @param perms the required access permissions
668 * @param nb_samples number of samples per channel
669 * @param sample_fmt the format of each sample in the buffer to allocate
670 * @param channel_layout the channel layout of the buffer
672 AVFilterBufferRef *avfilter_get_audio_buffer_ref_from_arrays(uint8_t **data,
676 enum AVSampleFormat sample_fmt,
677 uint64_t channel_layout);
680 #define AVFILTER_CMD_FLAG_ONE 1 ///< Stop once a filter understood the command (for target=all for example), fast filters are favored automatically
681 #define AVFILTER_CMD_FLAG_FAST 2 ///< Only execute command when its fast (like a video out that supports contrast adjustment in hw)
684 * Make the filter instance process a command.
685 * It is recommended to use avfilter_graph_send_command().
687 int avfilter_process_command(AVFilterContext *filter, const char *cmd, const char *arg, char *res, int res_len, int flags);
689 /** Initialize the filter system. Register all builtin filters. */
690 void avfilter_register_all(void);
692 /** Uninitialize the filter system. Unregister all filters. */
693 void avfilter_uninit(void);
696 * Register a filter. This is only needed if you plan to use
697 * avfilter_get_by_name later to lookup the AVFilter structure by name. A
698 * filter can still by instantiated with avfilter_open even if it is not
701 * @param filter the filter to register
702 * @return 0 if the registration was successful, a negative value
705 int avfilter_register(AVFilter *filter);
708 * Get a filter definition matching the given name.
710 * @param name the filter name to find
711 * @return the filter definition, if any matching one is registered.
712 * NULL if none found.
714 AVFilter *avfilter_get_by_name(const char *name);
717 * If filter is NULL, returns a pointer to the first registered filter pointer,
718 * if filter is non-NULL, returns the next pointer after filter.
719 * If the returned pointer points to NULL, the last registered filter
720 * was already reached.
722 AVFilter **av_filter_next(AVFilter **filter);
725 * Create a filter instance.
727 * @param filter_ctx put here a pointer to the created filter context
728 * on success, NULL on failure
729 * @param filter the filter to create an instance of
730 * @param inst_name Name to give to the new instance. Can be NULL for none.
731 * @return >= 0 in case of success, a negative error code otherwise
733 int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name);
736 * Initialize a filter.
738 * @param filter the filter to initialize
739 * @param args A string of parameters to use when initializing the filter.
740 * The format and meaning of this string varies by filter.
741 * @param opaque Any extra non-string data needed by the filter. The meaning
742 * of this parameter varies by filter.
743 * @return zero on success
745 int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque);
748 * Free a filter context.
750 * @param filter the filter to free
752 void avfilter_free(AVFilterContext *filter);
755 * Insert a filter in the middle of an existing link.
757 * @param link the link into which the filter should be inserted
758 * @param filt the filter to be inserted
759 * @param filt_srcpad_idx the input pad on the filter to connect
760 * @param filt_dstpad_idx the output pad on the filter to connect
761 * @return zero on success
763 int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt,
764 unsigned filt_srcpad_idx, unsigned filt_dstpad_idx);
766 #endif /* AVFILTER_AVFILTER_H */