Allow code to pass data to filters it creates.
[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
1653c11f
VS
49 ref->pic = pic;
50 ref->w = link->w;
51 ref->h = link->h;
4dbbcdee
VS
52 ref->perms = perms;
53
54 pic->refcount = 1;
1653c11f
VS
55 pic->format = link->format;
56 pic->free = avfilter_default_free_video_buffer;
4dbbcdee
VS
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{
3628fbe0
VS
67 AVFilterLink *out = link->dst->outputs[0];
68
4dbbcdee 69 link->cur_pic = picref;
3628fbe0
VS
70
71 if(out) {
1653c11f 72 out->outpic = avfilter_get_video_buffer(out, AV_PERM_WRITE);
af838d4c 73 out->outpic->pts = picref->pts;
3628fbe0
VS
74 avfilter_start_frame(out, avfilter_ref_pic(out->outpic, ~0));
75 }
4dbbcdee
VS
76}
77
78void avfilter_default_end_frame(AVFilterLink *link)
79{
3628fbe0
VS
80 AVFilterLink *out = link->dst->outputs[0];
81
4dbbcdee
VS
82 avfilter_unref_pic(link->cur_pic);
83 link->cur_pic = NULL;
3628fbe0
VS
84
85 if(out) {
86 avfilter_unref_pic(out->outpic);
87 out->outpic = NULL;
88 avfilter_end_frame(out);
89 }
4dbbcdee
VS
90}
91
efb36bfc 92AVFilterPicRef *avfilter_ref_pic(AVFilterPicRef *ref, int pmask)
4dbbcdee
VS
93{
94 AVFilterPicRef *ret = av_malloc(sizeof(AVFilterPicRef));
95 memcpy(ret, ref, sizeof(AVFilterPicRef));
efb36bfc 96 ret->perms &= pmask;
4dbbcdee
VS
97 ret->pic->refcount ++;
98 return ret;
99}
100
101void avfilter_unref_pic(AVFilterPicRef *ref)
102{
103 if(-- ref->pic->refcount == 0)
104 ref->pic->free(ref->pic);
105 av_free(ref);
106}
107
102fb0e3
VS
108/**
109 * default config_link() implementation for output video links to simplify
110 * the implementation of one input one output video filters */
111static int default_config_output_link(AVFilterLink *link)
112{
113 link->w = link->src->inputs[0]->w;
114 link->h = link->src->inputs[0]->h;
115
116 return 0;
117}
118
119/**
120 * default query_formats() implementation for output video links to simplify
121 * the implementation of one input one output video filters */
122static int *default_query_output_formats(AVFilterLink *link)
123{
124 return avfilter_make_format_list(1, link->src->inputs[0]->format);
125}
126
4dbbcdee
VS
127int avfilter_link(AVFilterContext *src, unsigned srcpad,
128 AVFilterContext *dst, unsigned dstpad)
129{
130 AVFilterLink *link;
e0752603 131 int *fmts[2], i, j;
4dbbcdee
VS
132
133 if(src->outputs[srcpad] || dst->inputs[dstpad])
134 return -1;
135
136 src->outputs[srcpad] =
137 dst->inputs[dstpad] = link = av_malloc(sizeof(AVFilterLink));
138
1653c11f
VS
139 link->src = src;
140 link->dst = dst;
141 link->srcpad = srcpad;
142 link->dstpad = dstpad;
4dbbcdee
VS
143 link->cur_pic = NULL;
144
e0752603 145 /* find a format both filters support - TODO: auto-insert conversion filter */
998a7aa3 146 link->format = -1;
102fb0e3
VS
147 if(src->filter->outputs[srcpad].query_formats)
148 fmts[0] = src->filter->outputs[srcpad].query_formats(link);
149 else
150 fmts[0] = default_query_output_formats(link);
ba6b9035 151 fmts[1] = dst->filter-> inputs[dstpad].query_formats(link);
e0752603
VS
152 for(i = 0; fmts[0][i] != -1; i ++)
153 for(j = 0; fmts[1][j] != -1; j ++)
154 if(fmts[0][i] == fmts[1][j]) {
155 link->format = fmts[0][i];
156 goto format_done;
157 }
158
159format_done:
160 av_free(fmts[0]);
161 av_free(fmts[1]);
162 if(link->format == -1) {
163 /* failed to find a format. fail at creating the link */
164 av_free(link);
165 src->outputs[srcpad] = NULL;
166 dst->inputs[dstpad] = NULL;
167 return -1;
168 }
169
102fb0e3
VS
170 if (src->filter->outputs[srcpad].config_props)
171 src->filter->outputs[srcpad].config_props(link);
172 else
173 default_config_output_link(link);
174
175 if (dst->filter->inputs[dstpad].config_props)
176 dst->filter->inputs[dstpad].config_props(link);
177
4dbbcdee
VS
178 return 0;
179}
180
181AVFilterPicRef *avfilter_get_video_buffer(AVFilterLink *link, int perms)
182{
183 AVFilterPicRef *ret = NULL;
184
185 if(link->dst->filter->inputs[link->dstpad].get_video_buffer)
186 ret = link->dst->filter->inputs[link->dstpad].get_video_buffer(link, perms);
187
188 if(!ret)
189 ret = avfilter_default_get_video_buffer(link, perms);
190
191 return ret;
192}
193
194void avfilter_request_frame(AVFilterLink *link)
195{
9586ba3a
VS
196 const AVFilterPad *pad = &link->src->filter->outputs[link->srcpad];
197
198 if(pad->request_frame)
199 pad->request_frame(link);
200 else if(link->src->inputs[0])
201 avfilter_request_frame(link->src->inputs[0]);
4dbbcdee
VS
202}
203
204/* XXX: should we do the duplicating of the picture ref here, instead of
205 * forcing the source filter to do it? */
206void avfilter_start_frame(AVFilterLink *link, AVFilterPicRef *picref)
207{
208 void (*start_frame)(AVFilterLink *, AVFilterPicRef *);
209
210 start_frame = link->dst->filter->inputs[link->dstpad].start_frame;
211 if(!start_frame)
212 start_frame = avfilter_default_start_frame;
213
214 start_frame(link, picref);
215}
216
217void avfilter_end_frame(AVFilterLink *link)
218{
219 void (*end_frame)(AVFilterLink *);
220
221 end_frame = link->dst->filter->inputs[link->dstpad].end_frame;
222 if(!end_frame)
223 end_frame = avfilter_default_end_frame;
224
225 end_frame(link);
226}
227
228void avfilter_draw_slice(AVFilterLink *link, uint8_t *data[4], int y, int h)
229{
230 link->dst->filter->inputs[link->dstpad].draw_slice(link, data, y, h);
231}
232
233static int filter_cmp(const void *aa, const void *bb)
234{
235 const AVFilter *a = *(const AVFilter **)aa, *b = *(const AVFilter **)bb;
236 return strcmp(a->name, b->name);
237}
238
239AVFilter *avfilter_get_by_name(char *name)
240{
241 AVFilter key = { .name = name, };
242 AVFilter *key2 = &key;
243 AVFilter **ret;
244
245 ret = bsearch(&key2, filters, filter_count, sizeof(AVFilter **), filter_cmp);
246 if(ret)
247 return *ret;
248 return NULL;
249}
250
251/* FIXME: insert in order, rather than insert at end + resort */
252void avfilter_register(AVFilter *filter)
253{
254 filters = av_realloc(filters, sizeof(AVFilter*) * (filter_count+1));
255 filters[filter_count] = filter;
256 qsort(filters, ++filter_count, sizeof(AVFilter **), filter_cmp);
257}
258
259void avfilter_init(void)
260{
d72a138e 261 avfilter_register(&vsrc_dummy);
eaf7eb66 262 avfilter_register(&vsrc_ppm);
d72a138e
VS
263 avfilter_register(&vf_crop);
264 avfilter_register(&vf_passthrough);
72e3037a 265 avfilter_register(&vf_rgb2bgr);
ce356b09 266 avfilter_register(&vf_slicify);
d72a138e 267 avfilter_register(&vo_sdl);
4dbbcdee
VS
268}
269
270void avfilter_uninit(void)
271{
272 av_freep(&filters);
273 filter_count = 0;
274}
275
276static int pad_count(const AVFilterPad *pads)
277{
278 AVFilterPad *p = (AVFilterPad *) pads;
279 int count;
280
281 for(count = 0; p->name; count ++) p ++;
282 return count;
283}
284
285static const char *filter_name(void *p)
286{
287 AVFilterContext *filter = p;
288 return filter->filter->name;
289}
290
6ae82d1e 291AVFilterContext *avfilter_create(AVFilter *filter, char *inst_name)
4dbbcdee
VS
292{
293 AVFilterContext *ret = av_malloc(sizeof(AVFilterContext));
294
295 ret->av_class = av_mallocz(sizeof(AVClass));
296 ret->av_class->item_name = filter_name;
297 ret->filter = filter;
2350e69c 298 ret->name = inst_name ? av_strdup(inst_name) : NULL;
4dbbcdee
VS
299 ret->inputs = av_mallocz(sizeof(AVFilterLink*) * pad_count(filter->inputs));
300 ret->outputs = av_mallocz(sizeof(AVFilterLink*) * pad_count(filter->outputs));
301 ret->priv = av_mallocz(filter->priv_size);
302
303 return ret;
304}
305
306void avfilter_destroy(AVFilterContext *filter)
307{
308 int i;
309
310 if(filter->filter->uninit)
311 filter->filter->uninit(filter);
312
313 for(i = 0; i < pad_count(filter->filter->inputs); i ++) {
314 if(filter->inputs[i])
315 filter->inputs[i]->src->outputs[filter->inputs[i]->srcpad] = NULL;
316 av_free(filter->inputs[i]);
317 }
318 for(i = 0; i < pad_count(filter->filter->outputs); i ++) {
319 if(filter->outputs[i])
320 filter->outputs[i]->dst->inputs[filter->outputs[i]->dstpad] = NULL;
321 av_free(filter->outputs[i]);
322 }
323
2350e69c 324 av_free(filter->name);
4dbbcdee
VS
325 av_free(filter->inputs);
326 av_free(filter->outputs);
327 av_free(filter->priv);
328 av_free(filter->av_class);
329 av_free(filter);
330}
331
6ae82d1e 332AVFilterContext *avfilter_create_by_name(char *name, char *inst_name)
4dbbcdee
VS
333{
334 AVFilter *filt;
335
336 if(!(filt = avfilter_get_by_name(name))) return NULL;
6ae82d1e 337 return avfilter_create(filt, inst_name);
4dbbcdee
VS
338}
339
a360f71e 340int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
4dbbcdee
VS
341{
342 int ret, i;
343
344 if(filter->filter->init)
a360f71e 345 if((ret = filter->filter->init(filter, args, opaque))) return ret;
4dbbcdee
VS
346 return 0;
347}
348
e0752603
VS
349int *avfilter_make_format_list(int len, ...)
350{
351 int *ret, i;
352 va_list vl;
353
354 ret = av_malloc(sizeof(int) * (len + 1));
355 va_start(vl, len);
356 for(i = 0; i < len; i ++)
357 ret[i] = va_arg(vl, int);
358 va_end(vl);
359 ret[len] = -1;
360
361 return ret;
362}
363