Separate the process of creating links between filters from that of configuring
[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
efb36bfc 34AVFilterPicRef *avfilter_ref_pic(AVFilterPicRef *ref, int pmask)
4dbbcdee
VS
35{
36 AVFilterPicRef *ret = av_malloc(sizeof(AVFilterPicRef));
37 memcpy(ret, ref, sizeof(AVFilterPicRef));
efb36bfc 38 ret->perms &= pmask;
4dbbcdee
VS
39 ret->pic->refcount ++;
40 return ret;
41}
42
43void avfilter_unref_pic(AVFilterPicRef *ref)
44{
45 if(-- ref->pic->refcount == 0)
46 ref->pic->free(ref->pic);
47 av_free(ref);
48}
49
50int avfilter_link(AVFilterContext *src, unsigned srcpad,
51 AVFilterContext *dst, unsigned dstpad)
52{
53 AVFilterLink *link;
54
7d0e1392
VS
55 if(src->output_count <= srcpad || dst->input_count <= dstpad ||
56 src->outputs[srcpad] || dst->inputs[dstpad])
4dbbcdee
VS
57 return -1;
58
59 src->outputs[srcpad] =
60 dst->inputs[dstpad] = link = av_malloc(sizeof(AVFilterLink));
61
1653c11f
VS
62 link->src = src;
63 link->dst = dst;
64 link->srcpad = srcpad;
65 link->dstpad = dstpad;
4dbbcdee 66 link->cur_pic = NULL;
01942f1d
VS
67 link->format = -1;
68
69 return 0;
70}
71
72int avfilter_config_link(AVFilterLink *link)
73{
74 int *fmts[2], i, j;
75 int (*config_link)(AVFilterLink *);
76
77 if(!link)
78 return 0;
4dbbcdee 79
e0752603 80 /* find a format both filters support - TODO: auto-insert conversion filter */
998a7aa3 81 link->format = -1;
01942f1d
VS
82 if(link->src->output_pads[link->srcpad].query_formats)
83 fmts[0] = link->src->output_pads[link->srcpad].query_formats(link);
102fb0e3 84 else
78b0c0bb 85 fmts[0] = avfilter_default_query_output_formats(link);
01942f1d 86 fmts[1] = link->dst->input_pads[link->dstpad].query_formats(link);
e0752603
VS
87 for(i = 0; fmts[0][i] != -1; i ++)
88 for(j = 0; fmts[1][j] != -1; j ++)
89 if(fmts[0][i] == fmts[1][j]) {
90 link->format = fmts[0][i];
91 goto format_done;
92 }
93
94format_done:
95 av_free(fmts[0]);
96 av_free(fmts[1]);
01942f1d 97 if(link->format == -1)
e0752603 98 return -1;
e0752603 99
01942f1d
VS
100 if(!(config_link = link->src->output_pads[link->srcpad].config_props))
101 config_link = avfilter_default_config_output_link;
102 if(config_link(link))
103 return -1;
102fb0e3 104
01942f1d
VS
105 if(!(config_link = link->dst->input_pads[link->dstpad].config_props))
106 config_link = avfilter_default_config_input_link;
107 if(config_link(link))
108 return -1;
102fb0e3 109
4dbbcdee
VS
110 return 0;
111}
112
113AVFilterPicRef *avfilter_get_video_buffer(AVFilterLink *link, int perms)
114{
115 AVFilterPicRef *ret = NULL;
116
c5ef7d7b
VS
117 if(link->dst->input_pads[link->dstpad].get_video_buffer)
118 ret = link->dst->input_pads[link->dstpad].get_video_buffer(link, perms);
4dbbcdee
VS
119
120 if(!ret)
121 ret = avfilter_default_get_video_buffer(link, perms);
122
123 return ret;
124}
125
126void avfilter_request_frame(AVFilterLink *link)
127{
c5ef7d7b 128 const AVFilterPad *pad = &link->src->output_pads[link->srcpad];
9586ba3a
VS
129
130 if(pad->request_frame)
131 pad->request_frame(link);
132 else if(link->src->inputs[0])
133 avfilter_request_frame(link->src->inputs[0]);
4dbbcdee
VS
134}
135
136/* XXX: should we do the duplicating of the picture ref here, instead of
137 * forcing the source filter to do it? */
138void avfilter_start_frame(AVFilterLink *link, AVFilterPicRef *picref)
139{
140 void (*start_frame)(AVFilterLink *, AVFilterPicRef *);
141
c5ef7d7b 142 start_frame = link->dst->input_pads[link->dstpad].start_frame;
4dbbcdee
VS
143 if(!start_frame)
144 start_frame = avfilter_default_start_frame;
145
146 start_frame(link, picref);
147}
148
149void avfilter_end_frame(AVFilterLink *link)
150{
151 void (*end_frame)(AVFilterLink *);
152
c5ef7d7b 153 end_frame = link->dst->input_pads[link->dstpad].end_frame;
4dbbcdee
VS
154 if(!end_frame)
155 end_frame = avfilter_default_end_frame;
156
157 end_frame(link);
158}
159
160void avfilter_draw_slice(AVFilterLink *link, uint8_t *data[4], int y, int h)
161{
c5ef7d7b 162 if(!link->dst->input_pads[link->dstpad].draw_slice)
7d0e1392
VS
163 return;
164
c5ef7d7b 165 link->dst->input_pads[link->dstpad].draw_slice(link, data, y, h);
4dbbcdee
VS
166}
167
168static int filter_cmp(const void *aa, const void *bb)
169{
170 const AVFilter *a = *(const AVFilter **)aa, *b = *(const AVFilter **)bb;
171 return strcmp(a->name, b->name);
172}
173
174AVFilter *avfilter_get_by_name(char *name)
175{
176 AVFilter key = { .name = name, };
177 AVFilter *key2 = &key;
178 AVFilter **ret;
179
180 ret = bsearch(&key2, filters, filter_count, sizeof(AVFilter **), filter_cmp);
181 if(ret)
182 return *ret;
183 return NULL;
184}
185
186/* FIXME: insert in order, rather than insert at end + resort */
187void avfilter_register(AVFilter *filter)
188{
189 filters = av_realloc(filters, sizeof(AVFilter*) * (filter_count+1));
190 filters[filter_count] = filter;
191 qsort(filters, ++filter_count, sizeof(AVFilter **), filter_cmp);
192}
193
194void avfilter_init(void)
195{
d72a138e 196 avfilter_register(&vsrc_dummy);
eaf7eb66 197 avfilter_register(&vsrc_ppm);
d72a138e 198 avfilter_register(&vf_crop);
d4169dd3 199 avfilter_register(&vf_graph);
d72a138e 200 avfilter_register(&vf_passthrough);
72e3037a 201 avfilter_register(&vf_rgb2bgr);
ce356b09 202 avfilter_register(&vf_slicify);
d72a138e 203 avfilter_register(&vo_sdl);
4dbbcdee
VS
204}
205
206void avfilter_uninit(void)
207{
208 av_freep(&filters);
209 filter_count = 0;
210}
211
212static int pad_count(const AVFilterPad *pads)
213{
214 AVFilterPad *p = (AVFilterPad *) pads;
215 int count;
216
217 for(count = 0; p->name; count ++) p ++;
218 return count;
219}
220
221static const char *filter_name(void *p)
222{
223 AVFilterContext *filter = p;
224 return filter->filter->name;
225}
226
6ae82d1e 227AVFilterContext *avfilter_create(AVFilter *filter, char *inst_name)
4dbbcdee
VS
228{
229 AVFilterContext *ret = av_malloc(sizeof(AVFilterContext));
230
231 ret->av_class = av_mallocz(sizeof(AVClass));
232 ret->av_class->item_name = filter_name;
233 ret->filter = filter;
2350e69c 234 ret->name = inst_name ? av_strdup(inst_name) : NULL;
4dbbcdee
VS
235 ret->priv = av_mallocz(filter->priv_size);
236
7d0e1392 237 ret->input_count = pad_count(filter->inputs);
c5ef7d7b
VS
238 ret->input_pads = av_malloc(sizeof(AVFilterPad) * ret->input_count);
239 memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad)*ret->input_count);
7d0e1392 240 ret->inputs = av_mallocz(sizeof(AVFilterLink*) * ret->input_count);
c5ef7d7b 241
7d0e1392 242 ret->output_count = pad_count(filter->outputs);
c5ef7d7b
VS
243 ret->output_pads = av_malloc(sizeof(AVFilterPad) * ret->output_count);
244 memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad)*ret->output_count);
7d0e1392
VS
245 ret->outputs = av_mallocz(sizeof(AVFilterLink*) * ret->output_count);
246
4dbbcdee
VS
247 return ret;
248}
249
250void avfilter_destroy(AVFilterContext *filter)
251{
252 int i;
253
254 if(filter->filter->uninit)
255 filter->filter->uninit(filter);
256
c5ef7d7b 257 for(i = 0; i < filter->input_count; i ++) {
4dbbcdee
VS
258 if(filter->inputs[i])
259 filter->inputs[i]->src->outputs[filter->inputs[i]->srcpad] = NULL;
260 av_free(filter->inputs[i]);
261 }
c5ef7d7b 262 for(i = 0; i < filter->output_count; i ++) {
4dbbcdee
VS
263 if(filter->outputs[i])
264 filter->outputs[i]->dst->inputs[filter->outputs[i]->dstpad] = NULL;
265 av_free(filter->outputs[i]);
266 }
267
2350e69c 268 av_free(filter->name);
c5ef7d7b
VS
269 av_free(filter->input_pads);
270 av_free(filter->output_pads);
4dbbcdee
VS
271 av_free(filter->inputs);
272 av_free(filter->outputs);
273 av_free(filter->priv);
274 av_free(filter->av_class);
275 av_free(filter);
276}
277
6ae82d1e 278AVFilterContext *avfilter_create_by_name(char *name, char *inst_name)
4dbbcdee
VS
279{
280 AVFilter *filt;
281
282 if(!(filt = avfilter_get_by_name(name))) return NULL;
6ae82d1e 283 return avfilter_create(filt, inst_name);
4dbbcdee
VS
284}
285
a360f71e 286int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
4dbbcdee 287{
c6b0aa68 288 int ret;
4dbbcdee
VS
289
290 if(filter->filter->init)
a360f71e 291 if((ret = filter->filter->init(filter, args, opaque))) return ret;
4dbbcdee
VS
292 return 0;
293}
294
e0752603
VS
295int *avfilter_make_format_list(int len, ...)
296{
297 int *ret, i;
298 va_list vl;
299
300 ret = av_malloc(sizeof(int) * (len + 1));
301 va_start(vl, len);
302 for(i = 0; i < len; i ++)
303 ret[i] = va_arg(vl, int);
304 va_end(vl);
305 ret[len] = -1;
306
307 return ret;
308}
309