Some simple filters for testing
[libav.git] / libavfilter / avfilter.c
1 /*
2 * Filter layer
3 * copyright (c) 2007 Bobby Bingham
4 *
5 * This file is part of FFmpeg.
6 *
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.
11 *
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.
16 *
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
20 */
21
22 #include <stdlib.h>
23 #include <string.h>
24 #include <stdio.h>
25
26 #include "avfilter.h"
27 #include "allfilters.h"
28
29 /** list of registered filters, sorted by name */
30 static int filter_count = 0;
31 static AVFilter **filters = NULL;
32
33 /* TODO: buffer pool. see comment for avfilter_default_get_video_buffer() */
34 void avfilter_default_free_video_buffer(AVFilterPic *pic)
35 {
36 avpicture_free((AVPicture *) pic);
37 av_free(pic);
38 }
39
40 /* TODO: set the buffer's priv member to a context structure for the whole
41 * filter chain. This will allow for a buffer pool instead of the constant
42 * alloc & free cycle currently implemented. */
43 AVFilterPicRef *avfilter_default_get_video_buffer(AVFilterLink *link, int perms)
44 {
45 AVFilterPic *pic = av_mallocz(sizeof(AVFilterPic));
46 AVFilterPicRef *ref = av_mallocz(sizeof(AVFilterPicRef));
47
48 ref->pic = pic;
49 ref->w = link->w;
50 ref->h = link->h;
51 ref->perms = perms;
52
53 pic->refcount = 1;
54 pic->format = link->format;
55 pic->free = avfilter_default_free_video_buffer;
56 avpicture_alloc((AVPicture *)pic, pic->format, ref->w, ref->h);
57
58 ref->data[0] = pic->data[0];
59 ref->data[1] = pic->data[1];
60 ref->data[2] = pic->data[2];
61 ref->data[3] = pic->data[3];
62
63 return ref;
64 }
65
66 void avfilter_default_start_frame(AVFilterLink *link, AVFilterPicRef *picref)
67 {
68 link->cur_pic = picref;
69 }
70
71 void avfilter_default_end_frame(AVFilterLink *link)
72 {
73 avfilter_unref_pic(link->cur_pic);
74 link->cur_pic = NULL;
75 }
76
77 AVFilterPicRef *avfilter_ref_pic(AVFilterPicRef *ref)
78 {
79 AVFilterPicRef *ret = av_malloc(sizeof(AVFilterPicRef));
80 memcpy(ret, ref, sizeof(AVFilterPicRef));
81 ret->pic->refcount ++;
82 return ret;
83 }
84
85 void avfilter_unref_pic(AVFilterPicRef *ref)
86 {
87 if(-- ref->pic->refcount == 0)
88 ref->pic->free(ref->pic);
89 av_free(ref);
90 }
91
92 int avfilter_link(AVFilterContext *src, unsigned srcpad,
93 AVFilterContext *dst, unsigned dstpad)
94 {
95 AVFilterLink *link;
96
97 if(src->outputs[srcpad] || dst->inputs[dstpad])
98 return -1;
99
100 src->outputs[srcpad] =
101 dst->inputs[dstpad] = link = av_malloc(sizeof(AVFilterLink));
102
103 link->src = src;
104 link->dst = dst;
105 link->srcpad = srcpad;
106 link->dstpad = dstpad;
107 link->cur_pic = NULL;
108
109 src->filter->outputs[dstpad].set_video_props(link);
110 return 0;
111 }
112
113 AVFilterPicRef *avfilter_get_video_buffer(AVFilterLink *link, int perms)
114 {
115 AVFilterPicRef *ret = NULL;
116
117 if(link->dst->filter->inputs[link->dstpad].get_video_buffer)
118 ret = link->dst->filter->inputs[link->dstpad].get_video_buffer(link, perms);
119
120 if(!ret)
121 ret = avfilter_default_get_video_buffer(link, perms);
122
123 return ret;
124 }
125
126 void avfilter_request_frame(AVFilterLink *link)
127 {
128 link->src->filter->outputs[link->srcpad].request_frame(link);
129 }
130
131 /* XXX: should we do the duplicating of the picture ref here, instead of
132 * forcing the source filter to do it? */
133 void avfilter_start_frame(AVFilterLink *link, AVFilterPicRef *picref)
134 {
135 void (*start_frame)(AVFilterLink *, AVFilterPicRef *);
136
137 start_frame = link->dst->filter->inputs[link->dstpad].start_frame;
138 if(!start_frame)
139 start_frame = avfilter_default_start_frame;
140
141 start_frame(link, picref);
142 }
143
144 void avfilter_end_frame(AVFilterLink *link)
145 {
146 void (*end_frame)(AVFilterLink *);
147
148 end_frame = link->dst->filter->inputs[link->dstpad].end_frame;
149 if(!end_frame)
150 end_frame = avfilter_default_end_frame;
151
152 end_frame(link);
153 }
154
155 void avfilter_draw_slice(AVFilterLink *link, uint8_t *data[4], int y, int h)
156 {
157 link->dst->filter->inputs[link->dstpad].draw_slice(link, data, y, h);
158 }
159
160 static int filter_cmp(const void *aa, const void *bb)
161 {
162 const AVFilter *a = *(const AVFilter **)aa, *b = *(const AVFilter **)bb;
163 return strcmp(a->name, b->name);
164 }
165
166 AVFilter *avfilter_get_by_name(char *name)
167 {
168 AVFilter key = { .name = name, };
169 AVFilter *key2 = &key;
170 AVFilter **ret;
171
172 ret = bsearch(&key2, filters, filter_count, sizeof(AVFilter **), filter_cmp);
173 if(ret)
174 return *ret;
175 return NULL;
176 }
177
178 /* FIXME: insert in order, rather than insert at end + resort */
179 void avfilter_register(AVFilter *filter)
180 {
181 filters = av_realloc(filters, sizeof(AVFilter*) * (filter_count+1));
182 filters[filter_count] = filter;
183 qsort(filters, ++filter_count, sizeof(AVFilter **), filter_cmp);
184 }
185
186 void avfilter_init(void)
187 {
188 avfilter_register(&vsrc_dummy);
189 avfilter_register(&vf_crop);
190 avfilter_register(&vf_passthrough);
191 avfilter_register(&vo_sdl);
192 }
193
194 void avfilter_uninit(void)
195 {
196 av_freep(&filters);
197 filter_count = 0;
198 }
199
200 static int pad_count(const AVFilterPad *pads)
201 {
202 AVFilterPad *p = (AVFilterPad *) pads;
203 int count;
204
205 for(count = 0; p->name; count ++) p ++;
206 return count;
207 }
208
209 static const char *filter_name(void *p)
210 {
211 AVFilterContext *filter = p;
212 return filter->filter->name;
213 }
214
215 AVFilterContext *avfilter_create(AVFilter *filter)
216 {
217 AVFilterContext *ret = av_malloc(sizeof(AVFilterContext));
218
219 ret->av_class = av_mallocz(sizeof(AVClass));
220 ret->av_class->item_name = filter_name;
221 ret->filter = filter;
222 ret->inputs = av_mallocz(sizeof(AVFilterLink*) * pad_count(filter->inputs));
223 ret->outputs = av_mallocz(sizeof(AVFilterLink*) * pad_count(filter->outputs));
224 ret->priv = av_mallocz(filter->priv_size);
225
226 return ret;
227 }
228
229 void avfilter_destroy(AVFilterContext *filter)
230 {
231 int i;
232
233 if(filter->filter->uninit)
234 filter->filter->uninit(filter);
235
236 for(i = 0; i < pad_count(filter->filter->inputs); i ++) {
237 if(filter->inputs[i])
238 filter->inputs[i]->src->outputs[filter->inputs[i]->srcpad] = NULL;
239 av_free(filter->inputs[i]);
240 }
241 for(i = 0; i < pad_count(filter->filter->outputs); i ++) {
242 if(filter->outputs[i])
243 filter->outputs[i]->dst->inputs[filter->outputs[i]->dstpad] = NULL;
244 av_free(filter->outputs[i]);
245 }
246
247 av_free(filter->inputs);
248 av_free(filter->outputs);
249 av_free(filter->priv);
250 av_free(filter->av_class);
251 av_free(filter);
252 }
253
254 AVFilterContext *avfilter_create_by_name(char *name)
255 {
256 AVFilter *filt;
257
258 if(!(filt = avfilter_get_by_name(name))) return NULL;
259 return avfilter_create(filt);
260 }
261
262 int avfilter_init_filter(AVFilterContext *filter)
263 {
264 int ret, i;
265
266 if(filter->filter->init)
267 if((ret = filter->filter->init(filter))) return ret;
268 for(i = 0; i < pad_count(filter->filter->outputs); i ++)
269 if(filter->outputs[i])
270 filter->filter->outputs[i].set_video_props(filter->outputs[i]);
271 return 0;
272 }
273