Missing semicolon typo
[libav.git] / libavfilter / avfilter.c
CommitLineData
4dbbcdee
VS
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
e0752603 22#include <stdarg.h>
4dbbcdee
VS
23#include <stdlib.h>
24#include <string.h>
25#include <stdio.h>
26
27#include "avfilter.h"
d72a138e 28#include "allfilters.h"
4dbbcdee
VS
29
30/** list of registered filters, sorted by name */
31static int filter_count = 0;
32static AVFilter **filters = NULL;
33
34/* TODO: buffer pool. see comment for avfilter_default_get_video_buffer() */
35void avfilter_default_free_video_buffer(AVFilterPic *pic)
36{
37 avpicture_free((AVPicture *) pic);
38 av_free(pic);
39}
40
41/* TODO: set the buffer's priv member to a context structure for the whole
42 * filter chain. This will allow for a buffer pool instead of the constant
43 * alloc & free cycle currently implemented. */
44AVFilterPicRef *avfilter_default_get_video_buffer(AVFilterLink *link, int perms)
45{
46 AVFilterPic *pic = av_mallocz(sizeof(AVFilterPic));
47 AVFilterPicRef *ref = av_mallocz(sizeof(AVFilterPicRef));
48
49 ref->pic = pic;
50 ref->w = link->w;
51 ref->h = link->h;
52 ref->perms = perms;
53
54 pic->refcount = 1;
55 pic->format = link->format;
56 pic->free = avfilter_default_free_video_buffer;
57 avpicture_alloc((AVPicture *)pic, pic->format, ref->w, ref->h);
58
92beffdc 59 memcpy(ref->data, pic->data, sizeof(pic->data));
a4ca7389 60 memcpy(ref->linesize, pic->linesize, sizeof(pic->linesize));
4dbbcdee
VS
61
62 return ref;
63}
64
65void avfilter_default_start_frame(AVFilterLink *link, AVFilterPicRef *picref)
66{
67 link->cur_pic = picref;
68}
69
70void avfilter_default_end_frame(AVFilterLink *link)
71{
72 avfilter_unref_pic(link->cur_pic);
73 link->cur_pic = NULL;
74}
75
efb36bfc 76AVFilterPicRef *avfilter_ref_pic(AVFilterPicRef *ref, int pmask)
4dbbcdee
VS
77{
78 AVFilterPicRef *ret = av_malloc(sizeof(AVFilterPicRef));
79 memcpy(ret, ref, sizeof(AVFilterPicRef));
efb36bfc 80 ret->perms &= pmask;
4dbbcdee
VS
81 ret->pic->refcount ++;
82 return ret;
83}
84
85void avfilter_unref_pic(AVFilterPicRef *ref)
86{
87 if(-- ref->pic->refcount == 0)
88 ref->pic->free(ref->pic);
89 av_free(ref);
90}
91
102fb0e3
VS
92/**
93 * default config_link() implementation for output video links to simplify
94 * the implementation of one input one output video filters */
95static int default_config_output_link(AVFilterLink *link)
96{
97 link->w = link->src->inputs[0]->w;
98 link->h = link->src->inputs[0]->h;
99
100 return 0;
101}
102
103/**
104 * default query_formats() implementation for output video links to simplify
105 * the implementation of one input one output video filters */
106static int *default_query_output_formats(AVFilterLink *link)
107{
108 return avfilter_make_format_list(1, link->src->inputs[0]->format);
109}
110
4dbbcdee
VS
111int avfilter_link(AVFilterContext *src, unsigned srcpad,
112 AVFilterContext *dst, unsigned dstpad)
113{
114 AVFilterLink *link;
e0752603 115 int *fmts[2], i, j;
4dbbcdee
VS
116
117 if(src->outputs[srcpad] || dst->inputs[dstpad])
118 return -1;
119
120 src->outputs[srcpad] =
121 dst->inputs[dstpad] = link = av_malloc(sizeof(AVFilterLink));
122
123 link->src = src;
124 link->dst = dst;
125 link->srcpad = srcpad;
126 link->dstpad = dstpad;
127 link->cur_pic = NULL;
128
e0752603 129 /* find a format both filters support - TODO: auto-insert conversion filter */
102fb0e3
VS
130 if(src->filter->outputs[srcpad].query_formats)
131 fmts[0] = src->filter->outputs[srcpad].query_formats(link);
132 else
133 fmts[0] = default_query_output_formats(link);
ba6b9035 134 fmts[1] = dst->filter-> inputs[dstpad].query_formats(link);
e0752603
VS
135 for(i = 0; fmts[0][i] != -1; i ++)
136 for(j = 0; fmts[1][j] != -1; j ++)
137 if(fmts[0][i] == fmts[1][j]) {
138 link->format = fmts[0][i];
139 goto format_done;
140 }
141
142format_done:
143 av_free(fmts[0]);
144 av_free(fmts[1]);
145 if(link->format == -1) {
146 /* failed to find a format. fail at creating the link */
147 av_free(link);
148 src->outputs[srcpad] = NULL;
149 dst->inputs[dstpad] = NULL;
150 return -1;
151 }
152
102fb0e3
VS
153 if (src->filter->outputs[srcpad].config_props)
154 src->filter->outputs[srcpad].config_props(link);
155 else
156 default_config_output_link(link);
157
158 if (dst->filter->inputs[dstpad].config_props)
159 dst->filter->inputs[dstpad].config_props(link);
160
4dbbcdee
VS
161 return 0;
162}
163
164AVFilterPicRef *avfilter_get_video_buffer(AVFilterLink *link, int perms)
165{
166 AVFilterPicRef *ret = NULL;
167
168 if(link->dst->filter->inputs[link->dstpad].get_video_buffer)
169 ret = link->dst->filter->inputs[link->dstpad].get_video_buffer(link, perms);
170
171 if(!ret)
172 ret = avfilter_default_get_video_buffer(link, perms);
173
174 return ret;
175}
176
177void avfilter_request_frame(AVFilterLink *link)
178{
179 link->src->filter->outputs[link->srcpad].request_frame(link);
180}
181
182/* XXX: should we do the duplicating of the picture ref here, instead of
183 * forcing the source filter to do it? */
184void avfilter_start_frame(AVFilterLink *link, AVFilterPicRef *picref)
185{
186 void (*start_frame)(AVFilterLink *, AVFilterPicRef *);
187
188 start_frame = link->dst->filter->inputs[link->dstpad].start_frame;
189 if(!start_frame)
190 start_frame = avfilter_default_start_frame;
191
192 start_frame(link, picref);
193}
194
195void avfilter_end_frame(AVFilterLink *link)
196{
197 void (*end_frame)(AVFilterLink *);
198
199 end_frame = link->dst->filter->inputs[link->dstpad].end_frame;
200 if(!end_frame)
201 end_frame = avfilter_default_end_frame;
202
203 end_frame(link);
204}
205
206void avfilter_draw_slice(AVFilterLink *link, uint8_t *data[4], int y, int h)
207{
208 link->dst->filter->inputs[link->dstpad].draw_slice(link, data, y, h);
209}
210
211static int filter_cmp(const void *aa, const void *bb)
212{
213 const AVFilter *a = *(const AVFilter **)aa, *b = *(const AVFilter **)bb;
214 return strcmp(a->name, b->name);
215}
216
217AVFilter *avfilter_get_by_name(char *name)
218{
219 AVFilter key = { .name = name, };
220 AVFilter *key2 = &key;
221 AVFilter **ret;
222
223 ret = bsearch(&key2, filters, filter_count, sizeof(AVFilter **), filter_cmp);
224 if(ret)
225 return *ret;
226 return NULL;
227}
228
229/* FIXME: insert in order, rather than insert at end + resort */
230void avfilter_register(AVFilter *filter)
231{
232 filters = av_realloc(filters, sizeof(AVFilter*) * (filter_count+1));
233 filters[filter_count] = filter;
234 qsort(filters, ++filter_count, sizeof(AVFilter **), filter_cmp);
235}
236
237void avfilter_init(void)
238{
d72a138e 239 avfilter_register(&vsrc_dummy);
eaf7eb66 240 avfilter_register(&vsrc_ppm);
d72a138e
VS
241 avfilter_register(&vf_crop);
242 avfilter_register(&vf_passthrough);
72e3037a 243 avfilter_register(&vf_rgb2bgr);
ce356b09 244 avfilter_register(&vf_slicify);
d72a138e 245 avfilter_register(&vo_sdl);
4dbbcdee
VS
246}
247
248void avfilter_uninit(void)
249{
250 av_freep(&filters);
251 filter_count = 0;
252}
253
254static int pad_count(const AVFilterPad *pads)
255{
256 AVFilterPad *p = (AVFilterPad *) pads;
257 int count;
258
259 for(count = 0; p->name; count ++) p ++;
260 return count;
261}
262
263static const char *filter_name(void *p)
264{
265 AVFilterContext *filter = p;
266 return filter->filter->name;
267}
268
269AVFilterContext *avfilter_create(AVFilter *filter)
270{
271 AVFilterContext *ret = av_malloc(sizeof(AVFilterContext));
272
273 ret->av_class = av_mallocz(sizeof(AVClass));
274 ret->av_class->item_name = filter_name;
275 ret->filter = filter;
276 ret->inputs = av_mallocz(sizeof(AVFilterLink*) * pad_count(filter->inputs));
277 ret->outputs = av_mallocz(sizeof(AVFilterLink*) * pad_count(filter->outputs));
278 ret->priv = av_mallocz(filter->priv_size);
279
280 return ret;
281}
282
283void avfilter_destroy(AVFilterContext *filter)
284{
285 int i;
286
287 if(filter->filter->uninit)
288 filter->filter->uninit(filter);
289
290 for(i = 0; i < pad_count(filter->filter->inputs); i ++) {
291 if(filter->inputs[i])
292 filter->inputs[i]->src->outputs[filter->inputs[i]->srcpad] = NULL;
293 av_free(filter->inputs[i]);
294 }
295 for(i = 0; i < pad_count(filter->filter->outputs); i ++) {
296 if(filter->outputs[i])
297 filter->outputs[i]->dst->inputs[filter->outputs[i]->dstpad] = NULL;
298 av_free(filter->outputs[i]);
299 }
300
301 av_free(filter->inputs);
302 av_free(filter->outputs);
303 av_free(filter->priv);
304 av_free(filter->av_class);
305 av_free(filter);
306}
307
308AVFilterContext *avfilter_create_by_name(char *name)
309{
310 AVFilter *filt;
311
312 if(!(filt = avfilter_get_by_name(name))) return NULL;
313 return avfilter_create(filt);
314}
315
89e64908 316int avfilter_init_filter(AVFilterContext *filter, const char *args)
4dbbcdee
VS
317{
318 int ret, i;
319
320 if(filter->filter->init)
89e64908 321 if((ret = filter->filter->init(filter, args))) return ret;
4dbbcdee
VS
322 return 0;
323}
324
e0752603
VS
325int *avfilter_make_format_list(int len, ...)
326{
327 int *ret, i;
328 va_list vl;
329
330 ret = av_malloc(sizeof(int) * (len + 1));
331 va_start(vl, len);
332 for(i = 0; i < len; i ++)
333 ret[i] = va_arg(vl, int);
334 va_end(vl);
335 ret[len] = -1;
336
337 return ret;
338}
339