4 This document explains guidelines that should be observed (or ignored with
5 good reason) when writing filters for libavfilter.
7 In this document, the word “frame” indicates either a video frame or a group
8 of audio samples, as stored in an AVFilterBuffer structure.
14 The query_formats method should set, for each input and each output links,
15 the list of supported formats.
17 For video links, that means pixel format. For audio links, that means
18 channel layout, sample format (the sample packing is implied by the sample
19 format) and sample rate.
21 The lists are not just lists, they are references to shared objects. When
22 the negotiation mechanism computes the intersection of the formats
23 supported at each end of a link, all references to both lists are replaced
24 with a reference to the intersection. And when a single format is
25 eventually chosen for a link amongst the remaining list, again, all
26 references to the list are updated.
28 That means that if a filter requires that its input and output have the
29 same format amongst a supported list, all it has to do is use a reference
30 to the same list of formats.
33 Buffer references ownership and permissions
34 ===========================================
39 Audio and video data are voluminous; the buffer and buffer reference
40 mechanism is intended to avoid, as much as possible, expensive copies of
41 that data while still allowing the filters to produce correct results.
43 The data is stored in buffers represented by AVFilterBuffer structures.
44 They must not be accessed directly, but through references stored in
45 AVFilterBufferRef structures. Several references can point to the
46 same buffer; the buffer is automatically deallocated once all
47 corresponding references have been destroyed.
49 The characteristics of the data (resolution, sample rate, etc.) are
50 stored in the reference; different references for the same buffer can
51 show different characteristics. In particular, a video reference can
52 point to only a part of a video buffer.
54 A reference is usually obtained as input to the start_frame or
55 filter_frame method or requested using the ff_get_video_buffer or
56 ff_get_audio_buffer functions. A new reference on an existing buffer can
57 be created with the avfilter_ref_buffer. A reference is destroyed using
58 the avfilter_unref_bufferp function.
63 At any time, a reference “belongs” to a particular piece of code,
64 usually a filter. With a few caveats that will be explained below, only
65 that piece of code is allowed to access it. It is also responsible for
66 destroying it, although this is sometimes done automatically (see the
67 section on link reference fields).
69 Here are the (fairly obvious) rules for reference ownership:
71 * A reference received by the filter_frame method (or its start_frame
72 deprecated version) belongs to the corresponding filter.
74 Special exception: for video references: the reference may be used
75 internally for automatic copying and must not be destroyed before
76 end_frame; it can be given away to ff_start_frame.
78 * A reference passed to ff_filter_frame (or the deprecated
79 ff_start_frame) is given away and must no longer be used.
81 * A reference created with avfilter_ref_buffer belongs to the code that
84 * A reference obtained with ff_get_video_buffer or ff_get_audio_buffer
85 belongs to the code that requested it.
87 * A reference given as return value by the get_video_buffer or
88 get_audio_buffer method is given away and must no longer be used.
93 The AVFilterLink structure has a few AVFilterBufferRef fields. The
94 cur_buf and out_buf were used with the deprecated
95 start_frame/draw_slice/end_frame API and should no longer be used.
96 src_buf, cur_buf_copy and partial_buf are used by libavfilter internally
97 and must not be accessed by filters.
100 ---------------------
102 The AVFilterBufferRef structure has a perms field that describes what
103 the code that owns the reference is allowed to do to the buffer data.
104 Different references for the same buffer can have different permissions.
106 For video filters that implement the deprecated
107 start_frame/draw_slice/end_frame API, the permissions only apply to the
108 parts of the buffer that have already been covered by the draw_slice
111 The value is a binary OR of the following constants:
113 * AV_PERM_READ: the owner can read the buffer data; this is essentially
114 always true and is there for self-documentation.
116 * AV_PERM_WRITE: the owner can modify the buffer data.
118 * AV_PERM_PRESERVE: the owner can rely on the fact that the buffer data
119 will not be modified by previous filters.
121 * AV_PERM_REUSE: the owner can output the buffer several times, without
122 modifying the data in between.
124 * AV_PERM_REUSE2: the owner can output the buffer several times and
125 modify the data in between (useless without the WRITE permissions).
127 * AV_PERM_ALIGN: the owner can access the data using fast operations
128 that require data alignment.
130 The READ, WRITE and PRESERVE permissions are about sharing the same
131 buffer between several filters to avoid expensive copies without them
132 doing conflicting changes on the data.
134 The REUSE and REUSE2 permissions are about special memory for direct
135 rendering. For example a buffer directly allocated in video memory must
136 not modified once it is displayed on screen, or it will cause tearing;
137 it will therefore not have the REUSE2 permission.
139 The ALIGN permission is about extracting part of the buffer, for
140 copy-less padding or cropping for example.
143 References received on input pads are guaranteed to have all the
144 permissions stated in the min_perms field and none of the permissions
145 stated in the rej_perms.
147 References obtained by ff_get_video_buffer and ff_get_audio_buffer are
148 guaranteed to have at least all the permissions requested as argument.
150 References created by avfilter_ref_buffer have the same permissions as
151 the original reference minus the ones explicitly masked; the mask is
152 usually ~0 to keep the same permissions.
154 Filters should remove permissions on reference they give to output
155 whenever necessary. It can be automatically done by setting the
156 rej_perms field on the output pad.
158 Here are a few guidelines corresponding to common situations:
160 * Filters that modify and forward their frame (like drawtext) need the
163 * Filters that read their input to produce a new frame on output (like
164 scale) need the READ permission on input and and must request a buffer
165 with the WRITE permission.
167 * Filters that intend to keep a reference after the filtering process
168 is finished (after filter_frame returns) must have the PRESERVE
169 permission on it and remove the WRITE permission if they create a new
170 reference to give it away.
172 * Filters that intend to modify a reference they have kept after the end
173 of the filtering process need the REUSE2 permission and must remove
174 the PRESERVE permission if they create a new reference to give it
181 The purpose of these rules is to ensure that frames flow in the filter
182 graph without getting stuck and accumulating somewhere.
184 Simple filters that output one frame for each input frame should not have
190 This method is called when a frame is pushed to the filter's input. It
191 can be called at any time except in a reentrant way.
193 If the input frame is enough to produce output, then the filter should
194 push the output frames on the output link immediately.
196 As an exception to the previous rule, if the input frame is enough to
197 produce several output frames, then the filter needs output only at
198 least one per link. The additional frames can be left buffered in the
199 filter; these buffered frames must be flushed immediately if a new input
202 (Example: framerate-doubling filter: filter_frame must (1) flush the
203 second copy of the previous frame, if it is still there, (2) push the
204 first copy of the incoming frame, (3) keep the second copy for later.)
206 If the input frame is not enough to produce output, the filter must not
207 call request_frame to get more. It must just process the frame or queue
208 it. The task of requesting more frames is left to the filter's
209 request_frame method or the application.
211 If a filter has several inputs, the filter must be ready for frames
212 arriving randomly on any input. Therefore, any filter with several inputs
213 will most likely require some kind of queuing mechanism. It is perfectly
214 acceptable to have a limited queue and to drop frames when the inputs
220 This method is called when a frame is wanted on an output.
222 For an input, it should directly call filter_frame on the corresponding
225 For a filter, if there are queued frames already ready, one of these
226 frames should be pushed. If not, the filter should request a frame on
227 one of its inputs, repeatedly until at least one frame has been pushed.
230 if request_frame could produce a frame, it should return 0;
231 if it could not for temporary reasons, it should return AVERROR(EAGAIN);
232 if it could not because there are no more frames, it should return
235 The typical implementation of request_frame for a filter with several
236 inputs will look like that:
242 while (!frame_pushed) {
243 input = input_where_a_frame_is_most_needed();
244 ret = ff_request_frame(input);
245 if (ret == AVERROR_EOF) {
246 process_eof_on_input();
247 } else if (ret < 0) {
253 Note that, except for filters that can have queued frames, request_frame
254 does not push frames: it requests them to its input, and as a reaction,
255 the filter_frame method will be called and do the work.
260 Until libavfilter 3.23, the filter_frame method was split:
262 - for video filters, it was made of start_frame, draw_slice (that could be
263 called several times on distinct parts of the frame) and end_frame;
265 - for audio filters, it was called filter_samples.