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