2012-05-09 20:27:00 +02:00
|
|
|
Filter design
|
|
|
|
=============
|
|
|
|
|
|
|
|
This document explains guidelines that should be observed (or ignored with
|
|
|
|
good reason) when writing filters for libavfilter.
|
|
|
|
|
|
|
|
In this document, the word “frame” indicates either a video frame or a group
|
|
|
|
of audio samples, as stored in an AVFilterBuffer structure.
|
|
|
|
|
|
|
|
|
2012-05-16 00:31:31 +02:00
|
|
|
Format negotiation
|
2012-05-09 20:27:00 +02:00
|
|
|
==================
|
|
|
|
|
|
|
|
The query_formats method should set, for each input and each output links,
|
2012-07-22 11:28:57 +02:00
|
|
|
the list of supported formats.
|
2012-05-09 20:27:00 +02:00
|
|
|
|
|
|
|
For video links, that means pixel format. For audio links, that means
|
2012-11-29 17:40:42 +01:00
|
|
|
channel layout, sample format (the sample packing is implied by the sample
|
|
|
|
format) and sample rate.
|
2012-05-09 20:27:00 +02:00
|
|
|
|
|
|
|
The lists are not just lists, they are references to shared objects. When
|
2012-05-16 00:31:31 +02:00
|
|
|
the negotiation mechanism computes the intersection of the formats
|
2012-11-29 17:40:42 +01:00
|
|
|
supported at each end of a link, all references to both lists are replaced
|
|
|
|
with a reference to the intersection. And when a single format is
|
2012-05-09 20:27:00 +02:00
|
|
|
eventually chosen for a link amongst the remaining list, again, all
|
|
|
|
references to the list are updated.
|
|
|
|
|
|
|
|
That means that if a filter requires that its input and output have the
|
2012-07-22 11:28:57 +02:00
|
|
|
same format amongst a supported list, all it has to do is use a reference
|
2012-05-09 20:27:00 +02:00
|
|
|
to the same list of formats.
|
|
|
|
|
2013-02-21 20:21:16 +01:00
|
|
|
query_formats can leave some formats unset and return AVERROR(EAGAIN) to
|
|
|
|
cause the negotiation mechanism to try again later. That can be used by
|
|
|
|
filters with complex requirements to use the format negotiated on one link
|
|
|
|
to set the formats supported on another.
|
|
|
|
|
2012-05-09 20:27:00 +02:00
|
|
|
|
|
|
|
Buffer references ownership and permissions
|
|
|
|
===========================================
|
|
|
|
|
2012-08-12 13:58:09 +02:00
|
|
|
Principle
|
|
|
|
---------
|
|
|
|
|
|
|
|
Audio and video data are voluminous; the buffer and buffer reference
|
|
|
|
mechanism is intended to avoid, as much as possible, expensive copies of
|
|
|
|
that data while still allowing the filters to produce correct results.
|
|
|
|
|
|
|
|
The data is stored in buffers represented by AVFilterBuffer structures.
|
|
|
|
They must not be accessed directly, but through references stored in
|
|
|
|
AVFilterBufferRef structures. Several references can point to the
|
|
|
|
same buffer; the buffer is automatically deallocated once all
|
|
|
|
corresponding references have been destroyed.
|
|
|
|
|
|
|
|
The characteristics of the data (resolution, sample rate, etc.) are
|
|
|
|
stored in the reference; different references for the same buffer can
|
|
|
|
show different characteristics. In particular, a video reference can
|
|
|
|
point to only a part of a video buffer.
|
|
|
|
|
|
|
|
A reference is usually obtained as input to the start_frame or
|
2012-11-28 13:53:48 +01:00
|
|
|
filter_frame method or requested using the ff_get_video_buffer or
|
2012-08-12 13:58:09 +02:00
|
|
|
ff_get_audio_buffer functions. A new reference on an existing buffer can
|
|
|
|
be created with the avfilter_ref_buffer. A reference is destroyed using
|
|
|
|
the avfilter_unref_bufferp function.
|
|
|
|
|
|
|
|
Reference ownership
|
|
|
|
-------------------
|
|
|
|
|
|
|
|
At any time, a reference “belongs” to a particular piece of code,
|
|
|
|
usually a filter. With a few caveats that will be explained below, only
|
|
|
|
that piece of code is allowed to access it. It is also responsible for
|
|
|
|
destroying it, although this is sometimes done automatically (see the
|
|
|
|
section on link reference fields).
|
|
|
|
|
|
|
|
Here are the (fairly obvious) rules for reference ownership:
|
|
|
|
|
2012-11-29 17:40:42 +01:00
|
|
|
* A reference received by the filter_frame method (or its start_frame
|
|
|
|
deprecated version) belongs to the corresponding filter.
|
2012-08-12 13:58:09 +02:00
|
|
|
|
|
|
|
Special exception: for video references: the reference may be used
|
|
|
|
internally for automatic copying and must not be destroyed before
|
|
|
|
end_frame; it can be given away to ff_start_frame.
|
|
|
|
|
2012-11-29 17:40:42 +01:00
|
|
|
* A reference passed to ff_filter_frame (or the deprecated
|
|
|
|
ff_start_frame) is given away and must no longer be used.
|
2012-08-12 13:58:09 +02:00
|
|
|
|
|
|
|
* A reference created with avfilter_ref_buffer belongs to the code that
|
|
|
|
created it.
|
|
|
|
|
2012-09-18 07:38:30 +02:00
|
|
|
* A reference obtained with ff_get_video_buffer or ff_get_audio_buffer
|
2012-08-12 13:58:09 +02:00
|
|
|
belongs to the code that requested it.
|
|
|
|
|
|
|
|
* A reference given as return value by the get_video_buffer or
|
|
|
|
get_audio_buffer method is given away and must no longer be used.
|
|
|
|
|
|
|
|
Link reference fields
|
|
|
|
---------------------
|
|
|
|
|
2012-11-29 17:40:42 +01:00
|
|
|
The AVFilterLink structure has a few AVFilterBufferRef fields. The
|
|
|
|
cur_buf and out_buf were used with the deprecated
|
|
|
|
start_frame/draw_slice/end_frame API and should no longer be used.
|
|
|
|
src_buf, cur_buf_copy and partial_buf are used by libavfilter internally
|
|
|
|
and must not be accessed by filters.
|
2012-08-12 13:58:09 +02:00
|
|
|
|
|
|
|
Reference permissions
|
|
|
|
---------------------
|
|
|
|
|
|
|
|
The AVFilterBufferRef structure has a perms field that describes what
|
|
|
|
the code that owns the reference is allowed to do to the buffer data.
|
|
|
|
Different references for the same buffer can have different permissions.
|
|
|
|
|
2012-11-29 17:40:42 +01:00
|
|
|
For video filters that implement the deprecated
|
|
|
|
start_frame/draw_slice/end_frame API, the permissions only apply to the
|
|
|
|
parts of the buffer that have already been covered by the draw_slice
|
|
|
|
method.
|
2012-08-12 13:58:09 +02:00
|
|
|
|
|
|
|
The value is a binary OR of the following constants:
|
|
|
|
|
|
|
|
* AV_PERM_READ: the owner can read the buffer data; this is essentially
|
|
|
|
always true and is there for self-documentation.
|
|
|
|
|
|
|
|
* AV_PERM_WRITE: the owner can modify the buffer data.
|
|
|
|
|
|
|
|
* AV_PERM_PRESERVE: the owner can rely on the fact that the buffer data
|
|
|
|
will not be modified by previous filters.
|
|
|
|
|
|
|
|
* AV_PERM_REUSE: the owner can output the buffer several times, without
|
|
|
|
modifying the data in between.
|
|
|
|
|
|
|
|
* AV_PERM_REUSE2: the owner can output the buffer several times and
|
|
|
|
modify the data in between (useless without the WRITE permissions).
|
|
|
|
|
|
|
|
* AV_PERM_ALIGN: the owner can access the data using fast operations
|
|
|
|
that require data alignment.
|
|
|
|
|
|
|
|
The READ, WRITE and PRESERVE permissions are about sharing the same
|
|
|
|
buffer between several filters to avoid expensive copies without them
|
|
|
|
doing conflicting changes on the data.
|
|
|
|
|
|
|
|
The REUSE and REUSE2 permissions are about special memory for direct
|
|
|
|
rendering. For example a buffer directly allocated in video memory must
|
|
|
|
not modified once it is displayed on screen, or it will cause tearing;
|
|
|
|
it will therefore not have the REUSE2 permission.
|
|
|
|
|
|
|
|
The ALIGN permission is about extracting part of the buffer, for
|
|
|
|
copy-less padding or cropping for example.
|
|
|
|
|
|
|
|
|
|
|
|
References received on input pads are guaranteed to have all the
|
|
|
|
permissions stated in the min_perms field and none of the permissions
|
|
|
|
stated in the rej_perms.
|
|
|
|
|
2012-09-18 07:38:30 +02:00
|
|
|
References obtained by ff_get_video_buffer and ff_get_audio_buffer are
|
2012-08-12 13:58:09 +02:00
|
|
|
guaranteed to have at least all the permissions requested as argument.
|
|
|
|
|
|
|
|
References created by avfilter_ref_buffer have the same permissions as
|
|
|
|
the original reference minus the ones explicitly masked; the mask is
|
|
|
|
usually ~0 to keep the same permissions.
|
|
|
|
|
|
|
|
Filters should remove permissions on reference they give to output
|
|
|
|
whenever necessary. It can be automatically done by setting the
|
|
|
|
rej_perms field on the output pad.
|
|
|
|
|
|
|
|
Here are a few guidelines corresponding to common situations:
|
|
|
|
|
|
|
|
* Filters that modify and forward their frame (like drawtext) need the
|
|
|
|
WRITE permission.
|
|
|
|
|
|
|
|
* Filters that read their input to produce a new frame on output (like
|
2013-08-12 19:40:29 +02:00
|
|
|
scale) need the READ permission on input and must request a buffer
|
2012-08-12 13:58:09 +02:00
|
|
|
with the WRITE permission.
|
|
|
|
|
|
|
|
* Filters that intend to keep a reference after the filtering process
|
2012-11-29 17:40:42 +01:00
|
|
|
is finished (after filter_frame returns) must have the PRESERVE
|
|
|
|
permission on it and remove the WRITE permission if they create a new
|
|
|
|
reference to give it away.
|
2012-08-12 13:58:09 +02:00
|
|
|
|
|
|
|
* Filters that intend to modify a reference they have kept after the end
|
|
|
|
of the filtering process need the REUSE2 permission and must remove
|
|
|
|
the PRESERVE permission if they create a new reference to give it
|
|
|
|
away.
|
2012-05-09 20:27:00 +02:00
|
|
|
|
|
|
|
|
|
|
|
Frame scheduling
|
|
|
|
================
|
|
|
|
|
|
|
|
The purpose of these rules is to ensure that frames flow in the filter
|
|
|
|
graph without getting stuck and accumulating somewhere.
|
|
|
|
|
|
|
|
Simple filters that output one frame for each input frame should not have
|
|
|
|
to worry about it.
|
|
|
|
|
2012-11-29 17:40:42 +01:00
|
|
|
filter_frame
|
|
|
|
------------
|
2012-05-09 20:27:00 +02:00
|
|
|
|
2012-11-29 17:40:42 +01:00
|
|
|
This method is called when a frame is pushed to the filter's input. It
|
|
|
|
can be called at any time except in a reentrant way.
|
2012-05-09 20:27:00 +02:00
|
|
|
|
|
|
|
If the input frame is enough to produce output, then the filter should
|
|
|
|
push the output frames on the output link immediately.
|
|
|
|
|
|
|
|
As an exception to the previous rule, if the input frame is enough to
|
|
|
|
produce several output frames, then the filter needs output only at
|
|
|
|
least one per link. The additional frames can be left buffered in the
|
|
|
|
filter; these buffered frames must be flushed immediately if a new input
|
|
|
|
produces new output.
|
|
|
|
|
2013-03-30 19:55:47 +01:00
|
|
|
(Example: frame rate-doubling filter: filter_frame must (1) flush the
|
2012-05-16 00:39:07 +02:00
|
|
|
second copy of the previous frame, if it is still there, (2) push the
|
|
|
|
first copy of the incoming frame, (3) keep the second copy for later.)
|
2012-05-09 20:27:00 +02:00
|
|
|
|
|
|
|
If the input frame is not enough to produce output, the filter must not
|
|
|
|
call request_frame to get more. It must just process the frame or queue
|
|
|
|
it. The task of requesting more frames is left to the filter's
|
|
|
|
request_frame method or the application.
|
|
|
|
|
|
|
|
If a filter has several inputs, the filter must be ready for frames
|
2012-07-22 11:28:57 +02:00
|
|
|
arriving randomly on any input. Therefore, any filter with several inputs
|
2012-05-09 20:27:00 +02:00
|
|
|
will most likely require some kind of queuing mechanism. It is perfectly
|
|
|
|
acceptable to have a limited queue and to drop frames when the inputs
|
|
|
|
are too unbalanced.
|
|
|
|
|
|
|
|
request_frame
|
|
|
|
-------------
|
|
|
|
|
|
|
|
This method is called when a frame is wanted on an output.
|
|
|
|
|
2012-11-29 17:40:42 +01:00
|
|
|
For an input, it should directly call filter_frame on the corresponding
|
|
|
|
output.
|
2012-05-09 20:27:00 +02:00
|
|
|
|
|
|
|
For a filter, if there are queued frames already ready, one of these
|
|
|
|
frames should be pushed. If not, the filter should request a frame on
|
2012-07-22 11:28:57 +02:00
|
|
|
one of its inputs, repeatedly until at least one frame has been pushed.
|
2012-05-09 20:27:00 +02:00
|
|
|
|
|
|
|
Return values:
|
|
|
|
if request_frame could produce a frame, it should return 0;
|
|
|
|
if it could not for temporary reasons, it should return AVERROR(EAGAIN);
|
2012-05-16 00:39:07 +02:00
|
|
|
if it could not because there are no more frames, it should return
|
2012-05-09 20:27:00 +02:00
|
|
|
AVERROR_EOF.
|
|
|
|
|
|
|
|
The typical implementation of request_frame for a filter with several
|
|
|
|
inputs will look like that:
|
|
|
|
|
|
|
|
if (frames_queued) {
|
|
|
|
push_one_frame();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
while (!frame_pushed) {
|
|
|
|
input = input_where_a_frame_is_most_needed();
|
2012-11-29 17:40:42 +01:00
|
|
|
ret = ff_request_frame(input);
|
2012-05-09 20:27:00 +02:00
|
|
|
if (ret == AVERROR_EOF) {
|
|
|
|
process_eof_on_input();
|
|
|
|
} else if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
Note that, except for filters that can have queued frames, request_frame
|
|
|
|
does not push frames: it requests them to its input, and as a reaction,
|
2012-11-29 17:40:42 +01:00
|
|
|
the filter_frame method will be called and do the work.
|
|
|
|
|
|
|
|
Legacy API
|
|
|
|
==========
|
|
|
|
|
|
|
|
Until libavfilter 3.23, the filter_frame method was split:
|
|
|
|
|
|
|
|
- for video filters, it was made of start_frame, draw_slice (that could be
|
|
|
|
called several times on distinct parts of the frame) and end_frame;
|
|
|
|
|
|
|
|
- for audio filters, it was called filter_samples.
|