All filters are described by an AVFilter structure. This structure gives information needed to initialize the filter, and information on the entry points into the filter code. This structure is declared in libavfilter/avfilter.h:
濾鏡的數(shù)據(jù)結(jié)構(gòu)體(包括需要初始化濾鏡變量,濾鏡的函數(shù)入口點(diǎn)等)定義在libavfilter/avfilter.h中:
The query_formats function sets the in_formats member of connected output links, and the out_formats member of connected input links, described below under AVFilterLink.
query_formats 函數(shù)設(shè)置濾鏡輸入,輸出圖像的格式,如YUV420,YUV422等。
Let's take a quick look at the AVFilterPad structure, which is used to describe the inputs and outputs of the filter. This is also defined in libavfilter/avfilter.h:
AVFilterPad結(jié)構(gòu)體用于描述濾鏡的輸入和輸出。
The actual definition in the header file has doxygen comments describing each entry point, its purpose, and what type of pads it is relevant for. These fields are relevant for all pads:
Field | Description |
---|---|
name | Name of the pad. No two inputs should have the same name, and no two outputs should have the same name. (pad的名稱,當(dāng)有多于一個(gè)的輸入或輸出時(shí),不要重名) |
type | Only AV_PAD_VIDEO currently. |
config_props | Handles configuration of the link connected to the pad |
Fields only relevant to input pads are:
Field | Description |
---|---|
min_perms | Minimum permissions required to a picture received as input. (本濾鏡要求的輸入圖像的最小授權(quán)) |
rej_perms | Permissions not accepted on pictures received as input. |
start_frame | Called when a frame is about to be given as input. |
draw_slice | Called when a slice of frame data has been given as input. (主要的濾鏡處理函數(shù)) |
end_frame | Called when the input frame has been completely sent. |
get_video_buffer | Called by the previous filter to request memory for a picture. |
Fields only relevant to output pads are:
Field | Description |
---|---|
request_frame | Requests that the filter output a frame. |
All pictures in the filter system are reference counted. This means that there is a picture buffer with memory allocated for the image data, and various filters can own a reference to the buffer. When a reference is no longer needed, its owner frees the reference. When the last reference to a picture buffer is freed, the filter system automatically frees the picture buffer.
濾鏡系統(tǒng)中所有的圖像都是有計(jì)數(shù)的引用。意即,有一個(gè)分配了存儲(chǔ)空間的圖像buffer,而每個(gè)濾鏡可以有自己的引用指向這個(gè)圖像buffer。當(dāng)這個(gè)引用不再需要時(shí),擁有這個(gè)引用的濾鏡將釋放這個(gè)引用。當(dāng)圖像buffer的最后一個(gè)引用也被釋放時(shí),濾鏡系統(tǒng)將自動(dòng)釋放這個(gè)圖像buffer。
The upshot of multiple filters having references to a single picture is that they will all want some level of access to the image data. It should be obvious that if one filter expects to be able to read the image data without it changing that no other filter should write to the image data. The permissions system handles this.
使用組合濾鏡的結(jié)果就是會(huì)有多個(gè)引用指向同一個(gè)圖像buffer,而且它們都希望對(duì)buffer中的圖像有相同的訪問權(quán)限。這時(shí)就會(huì)產(chǎn)生沖突啦,例如,當(dāng)一個(gè)濾鏡有讀取buffer中圖像數(shù)據(jù)的權(quán)限時(shí),肯定不希望有別的濾鏡同時(shí)在改定buffer中的圖像數(shù)據(jù)。解決這種沖突就需要授權(quán)系統(tǒng)來處理。
In most cases, when a filter prepares to output a frame, it will request a buffer from the filter to which it will be outputting. It specifies the minimum permissions it needs to the buffer, though it may be given a buffer with more permissions than the minimum it requested.
通常,當(dāng)濾鏡準(zhǔn)備輸出一幀數(shù)據(jù)時(shí),它會(huì)向?yàn)V鏡系統(tǒng)請(qǐng)求一個(gè)輸出buffer,這時(shí),濾鏡系統(tǒng)會(huì)返回一個(gè)buffer結(jié)該濾鏡并將這個(gè)buffer授予最小的權(quán)限。
When it wants to pass this buffer to another filter as output, it creates a new reference to the picture, possibly with a reduced set of permissions. This new reference will be owned by the filter receiving it.
當(dāng)當(dāng)前濾鏡要將圖像buffer輸出給下一個(gè)濾鏡時(shí),該濾鏡會(huì)創(chuàng)建一個(gè)指向圖像buffer的新引用,這個(gè)buffer的授權(quán)可能會(huì)減小。而這個(gè)新的引用會(huì)被下一個(gè)濾鏡接收。
So, for example, for a filter which drops frames if they are similar to the last frame it output, it would want to keep its own reference to a picture after outputting it, and make sure that no other filter modified the buffer either. It would do this by requesting the permissions AV_PERM_READ|AV_PERM_WRITE|AV_PERM_PRESERVE for itself, and removing the AV_PERM_WRITE permission from any references it gave to other filters.
這段沒搞明白什么意思
The available permissions are:
Permission | Description |
---|---|
AV_PERM_READ | Can read the image data. |
AV_PERM_WRITE | Can write to the image data. |
AV_PERM_PRESERVE | Can assume that the image data will not be modified by other filters. This means that no other filters should have the AV_PERM_WRITE permission. |
AV_PERM_REUSE | The filter may output the same buffer multiple times, but the image data may not be changed for the different outputs. |
AV_PERM_REUSE2 | The filter may output the same buffer multiple times, and may modify the image data between outputs. |
A filter's inputs and outputs are connected to those of another filter through the AVFilterLink structure:
The src and dst members indicate the filters at the source and destination ends of the link, respectively. The srcpad indicates the index of the output pad on the source filter to which the link is connected. Likewise, the dstpad indicates the index of the input pad on the destination filter.
The in_formats member points to a list of formats supported by the source filter, while the out_formats member points to a list of formats supported by the destination filter. The AVFilterFormats structure used to store the lists is reference counted, and in fact tracks its references (see the comments for the AVFilterFormats structure in libavfilter/avfilter.h for more information on how the colorspace negotiation is works and why this is necessary). The upshot is that if a filter provides pointers to the same list on multiple input/output links, it means that those links will be forced to use the same format as each other.
When two filters are connected, they need to agree upon the dimensions of the image data they'll be working with, and the format that data is in. Once this has been agreed upon, these parameters are stored in the link structure.
The srcpic member is used internally by the filter system, and should not be accessed directly.
The cur_pic member is for the use of the destination filter. When a frame is currently being sent over the link (ie. starting from the call to start_frame() and ending with the call to end_frame()), this contains the reference to the frame which is owned by the destination filter.
The outpic member is described in the following tutorial on writing a simple filter.
Because the majority of filters that will probably be written will take exactly one input, and produce exactly one output, and output one frame for every frame received as input, the filter system provides a number default entry points to ease the development of such filters.
Entry point | Actions taken by the default implementation |
---|---|
request_frame() | Request a frame from the previous filter in the chain. |
query_formats() | Sets the list of supported formats on all input pads such that all links must use the same format, from a default list of formats containing most YUV and RGB/BGR formats. |
start_frame() | Request a buffer to store the output frame in. A reference to this buffer is stored in the outpic member of the link hooked to the filter's output. The next filter's start_frame() callback is called and given a reference to this buffer. |
end_frame() | Calls the next filter's end_frame() callback. Frees the reference to the outpic member of the output link, if it was set by (ie. if the default start_frame() is used). Frees the cur_pic reference in the input link. |
get_video_buffer() | Returns a buffer with the AV_PERM_READ permission in addition to all the requested permissions. |
config_props() on output pad | Sets the image dimensions for the output link to the same as on the filter's input. |
Having looked at the data structures and callback functions involved, let's take a look at an actual filter. The vf_negate filter inverts the colors in a video. It has one input, and one output, and outputs exactly one frame for every input frame. In this way, it's fairly typical, and can take advantage of many of the default callback implementations offered by the filter system.
First, let's take a look at the AVFilter structure at the bottom of the libavfilter/vf_negate.c file:
Here, you can see that the filter is named "negate," and it needs sizeof(NegContext) bytes of data to store its context. In the list of inputs and outputs, a pad whose name is set to NULL indicates the end of the list, so this filter has exactly one input and one output. If you look closely at the pad definitions, you will see that fairly few callback functions are actually specified. Because of the simplicity of the filter, the defaults can do most of the work for us.
Let us take a look at the callback function it does define.
This calls avfilter_make_format_list(). This function takes as its first parameter the number of formats which will follow as the remaining parameters. The return value is an AVFilterFormats structure containing the given formats. The avfilter_set_common_formats() function which this structure is passed to sets all connected links to use this same list of formats, which causes all the filters to use the same format after negotiation is complete. As you can see, this filter supports a number of planar YUV colorspaces, including JPEG YUV colorspaces (the ones with a 'J' in the names).
The config_props() on an input pad is responsible for verifying that the properties of the input pad are supported by the filter, and to make any updates to the filter's context which are necessary for the link's properties.
TODO: quick explanation of YUV colorspaces, chroma subsampling, difference in range of YUV and JPEG YUV.
Let's take a look at the way in which this filter stores its context:
That's right. The priv_size member of the AVFilter structure tells the filter system how many bytes to reserve for this structure. The hsub and vsub members are used for chroma subsampling, and the offY and offUV members are used for handling the difference in range between YUV and JPEG YUV. Let's see how these are set in the input pad's config_props:
This simply calls avcodec_get_chroma_sub_sample() to get the chroma subsampling shift factors, and stores those in the context. It then stores a set of offsets for compensating for different luma/chroma value ranges for JPEG YUV, and a different set of offsets for other YUV colorspaces. It returns zero to indicate success, because there are no possible input cases which this filter cannot handle.
Finally, the function which actually does the processing for the filter, draw_slice():
The y parameter indicates the top of the current slice, and the h parameter the slice's height. Areas of the image outside this slice should not be assumed to be meaningful (though a method to allow this assumption in order to simplify boundary cases for some filters is coming in the future).
This sets inrow to point to the beginning of the first row of the slice in the input, and outrow similarly for the output. Then, for each row, it loops through all the pixels, subtracting them from 255, and adding the offset which was determined in config_props() to account for different value ranges.
It then does the same thing for the chroma planes. Note how the width and height are shifted right to account for the chroma subsampling.
Once the drawing is completed, the slice is sent to the next filter by calling avfilter_draw_slice().
Chapter 2:
將濾鏡添加進(jìn)ffmpeg中編譯和運(yùn)行
1. configure中聲明使用的協(xié)議
2. libavfilter/allfilter.c中注冊自定義濾鏡
3. libavfilter/Makfile中添加濾鏡的鏈接
4. configure設(shè)置
5. 編譯與運(yùn)行
chapter 3:
反相濾鏡源碼
聯(lián)系客服