Simplify
[libav.git] / libavfilter / avfilter.c
CommitLineData
4dbbcdee 1/*
16790dc3 2 * filter layer
4dbbcdee
VS
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
4dbbcdee 22#include "avfilter.h"
d72a138e 23#include "allfilters.h"
4dbbcdee 24
6d8c67a7
VS
25/** list of registered filters */
26struct FilterList
27{
28 AVFilter *filter;
29 struct FilterList *next;
30} *filters = NULL;
4dbbcdee 31
6fb0648d
VS
32/** helper macros to get the in/out pad on the dst/src filter */
33#define link_dpad(link) link->dst-> input_pads[link->dstpad]
34#define link_spad(link) link->src->output_pads[link->srcpad]
35
269b9847 36AVFilterPicRef *avfilter_ref_pic(AVFilterPicRef *ref, int pmask)
4dbbcdee
VS
37{
38 AVFilterPicRef *ret = av_malloc(sizeof(AVFilterPicRef));
cdf2a332 39 *ret = *ref;
efb36bfc 40 ret->perms &= pmask;
4dbbcdee
VS
41 ret->pic->refcount ++;
42 return ret;
43}
44
45void avfilter_unref_pic(AVFilterPicRef *ref)
46{
e079d22e 47 if(!(--ref->pic->refcount))
4dbbcdee
VS
48 ref->pic->free(ref->pic);
49 av_free(ref);
50}
51
24618441
VS
52void avfilter_insert_pad(unsigned idx, unsigned *count, size_t padidx_off,
53 AVFilterPad **pads, AVFilterLink ***links,
54 AVFilterPad *newpad)
55{
56 unsigned i;
57
58 idx = FFMIN(idx, *count);
59
60 *pads = av_realloc(*pads, sizeof(AVFilterPad) * (*count + 1));
61 *links = av_realloc(*links, sizeof(AVFilterLink*) * (*count + 1));
62 memmove(*pads +idx+1, *pads +idx, sizeof(AVFilterPad) * (*count-idx));
63 memmove(*links+idx+1, *links+idx, sizeof(AVFilterLink*) * (*count-idx));
64 memcpy(*pads+idx, newpad, sizeof(AVFilterPad));
65 (*links)[idx] = NULL;
66
67 (*count) ++;
68 for(i = idx+1; i < *count; i ++)
69 if(*links[i])
d9b575d3 70 (*(unsigned *)((uint8_t *) *links[i] + padidx_off)) ++;
24618441
VS
71}
72
4dbbcdee
VS
73int avfilter_link(AVFilterContext *src, unsigned srcpad,
74 AVFilterContext *dst, unsigned dstpad)
75{
76 AVFilterLink *link;
77
7d0e1392
VS
78 if(src->output_count <= srcpad || dst->input_count <= dstpad ||
79 src->outputs[srcpad] || dst->inputs[dstpad])
4dbbcdee
VS
80 return -1;
81
82 src->outputs[srcpad] =
b3431ecd 83 dst-> inputs[dstpad] = link = av_mallocz(sizeof(AVFilterLink));
4dbbcdee 84
1653c11f
VS
85 link->src = src;
86 link->dst = dst;
87 link->srcpad = srcpad;
88 link->dstpad = dstpad;
01942f1d
VS
89 link->format = -1;
90
91 return 0;
92}
93
52362e9d
VS
94int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt,
95 unsigned in, unsigned out)
96{
2c5741b8 97 av_log(link->dst, AV_LOG_INFO, "auto-inserting filter '%s'\n",
52362e9d
VS
98 filt->filter->name);
99
100 link->dst->inputs[link->dstpad] = NULL;
101 if(avfilter_link(filt, out, link->dst, link->dstpad)) {
102 /* failed to link output filter to new filter */
103 link->dst->inputs[link->dstpad] = link;
104 return -1;
105 }
106
107 /* re-hookup the link to the new destination filter we inserted */
108 link->dst = filt;
109 link->dstpad = in;
110 filt->inputs[in] = link;
111
07d0bba5
VS
112 /* if any information on supported colorspaces already exists on the
113 * link, we need to preserve that */
5b7c4033 114 if(link->out_formats)
e3065c8b
VS
115 avfilter_formats_changeref(&link->out_formats,
116 &filt->outputs[out]->out_formats);
07d0bba5 117
52362e9d
VS
118 return 0;
119}
120
69818105 121int avfilter_config_links(AVFilterContext *filter)
01942f1d 122{
01942f1d 123 int (*config_link)(AVFilterLink *);
69818105
VS
124 unsigned i;
125
126 for(i = 0; i < filter->input_count; i ++) {
e079d22e 127 AVFilterLink *link = filter->inputs[i];
d21cbbff 128
e079d22e 129 if(!link) continue;
69818105
VS
130
131 switch(link->init_state) {
132 case AVLINK_INIT:
133 continue;
134 case AVLINK_STARTINIT:
7a9fd2a0
VS
135 av_log(filter, AV_LOG_INFO, "circular filter chain detected\n");
136 return 0;
69818105
VS
137 case AVLINK_UNINIT:
138 link->init_state = AVLINK_STARTINIT;
139
140 if(avfilter_config_links(link->src))
141 return -1;
4dbbcdee 142
2fd46877
VS
143 if(!(config_link = link_spad(link).config_props))
144 config_link = avfilter_default_config_output_link;
145 if(config_link(link))
146 return -1;
102fb0e3 147
2fd46877 148 if((config_link = link_dpad(link).config_props))
a5b5d95a
VS
149 if(config_link(link))
150 return -1;
102fb0e3 151
69818105
VS
152 link->init_state = AVLINK_INIT;
153 }
154 }
155
4dbbcdee
VS
156 return 0;
157}
158
159AVFilterPicRef *avfilter_get_video_buffer(AVFilterLink *link, int perms)
160{
161 AVFilterPicRef *ret = NULL;
162
6fb0648d
VS
163 if(link_dpad(link).get_video_buffer)
164 ret = link_dpad(link).get_video_buffer(link, perms);
4dbbcdee
VS
165
166 if(!ret)
167 ret = avfilter_default_get_video_buffer(link, perms);
168
169 return ret;
170}
171
3306f8bd 172int avfilter_request_frame(AVFilterLink *link)
4dbbcdee 173{
6fb0648d
VS
174 if(link_spad(link).request_frame)
175 return link_spad(link).request_frame(link);
9586ba3a 176 else if(link->src->inputs[0])
3306f8bd
VS
177 return avfilter_request_frame(link->src->inputs[0]);
178 else return -1;
4dbbcdee
VS
179}
180
7b02c484
VS
181int avfilter_poll_frame(AVFilterLink *link)
182{
183 int i, min=INT_MAX;
184
185 if(link_spad(link).poll_frame)
186 return link_spad(link).poll_frame(link);
c245ddf2
VS
187
188 for (i=0; i<link->src->input_count; i++) {
189 if(!link->src->inputs[i])
190 return -1;
191 min = FFMIN(min, avfilter_poll_frame(link->src->inputs[i]));
192 }
7b02c484
VS
193
194 return min;
195}
196
4dbbcdee
VS
197/* XXX: should we do the duplicating of the picture ref here, instead of
198 * forcing the source filter to do it? */
199void avfilter_start_frame(AVFilterLink *link, AVFilterPicRef *picref)
200{
201 void (*start_frame)(AVFilterLink *, AVFilterPicRef *);
ac8a116e 202 AVFilterPad *dst = &link_dpad(link);
4dbbcdee 203
ac8a116e 204 if(!(start_frame = dst->start_frame))
4dbbcdee
VS
205 start_frame = avfilter_default_start_frame;
206
c949d867 207 /* prepare to copy the picture if it has insufficient permissions */
ac8a116e
VS
208 if((dst->min_perms & picref->perms) != dst->min_perms ||
209 dst->rej_perms & picref->perms) {
35db407e 210 /*
c949d867
VS
211 av_log(link->dst, AV_LOG_INFO,
212 "frame copy needed (have perms %x, need %x, reject %x)\n",
269b9847 213 picref->perms,
c949d867 214 link_dpad(link).min_perms, link_dpad(link).rej_perms);
35db407e 215 */
c949d867 216
ac8a116e 217 link->cur_pic = avfilter_default_get_video_buffer(link, dst->min_perms);
c949d867 218 link->srcpic = picref;
be36bc09 219 link->cur_pic->pts = link->srcpic->pts;
c949d867
VS
220 }
221 else
222 link->cur_pic = picref;
223
224 start_frame(link, link->cur_pic);
4dbbcdee
VS
225}
226
227void avfilter_end_frame(AVFilterLink *link)
228{
229 void (*end_frame)(AVFilterLink *);
230
35db407e
VS
231 if(!(end_frame = link_dpad(link).end_frame))
232 end_frame = avfilter_default_end_frame;
233
234 end_frame(link);
235
c949d867
VS
236 /* unreference the source picture if we're feeding the destination filter
237 * a copied version dues to permission issues */
238 if(link->srcpic) {
239 avfilter_unref_pic(link->srcpic);
240 link->srcpic = NULL;
241 }
242
4dbbcdee
VS
243}
244
ef35d13a 245void avfilter_draw_slice(AVFilterLink *link, int y, int h)
4dbbcdee 246{
c949d867
VS
247 uint8_t *src[4], *dst[4];
248 int i, j, hsub, vsub;
249
250 /* copy the slice if needed for permission reasons */
251 if(link->srcpic) {
252 avcodec_get_chroma_sub_sample(link->format, &hsub, &vsub);
253
254 src[0] = link->srcpic-> data[0] + y * link->srcpic-> linesize[0];
255 dst[0] = link->cur_pic->data[0] + y * link->cur_pic->linesize[0];
256 for(i = 1; i < 4; i ++) {
257 if(link->srcpic->data[i]) {
258 src[i] = link->srcpic-> data[i] + (y >> vsub) * link->srcpic-> linesize[i];
259 dst[i] = link->cur_pic->data[i] + (y >> vsub) * link->cur_pic->linesize[i];
260 } else
261 src[i] = dst[i] = NULL;
262 }
19dc7104
VS
263
264 for(i = 0; i < 4; i ++) {
c949d867
VS
265 if(!src[i]) continue;
266
19dc7104 267 for(j = 0; j < h >> (i==0 ? 0 : vsub); j ++) {
c949d867
VS
268 memcpy(dst[i], src[i], link->cur_pic->linesize[i]);
269 src[i] += link->srcpic ->linesize[i];
270 dst[i] += link->cur_pic->linesize[i];
271 }
272 }
273 }
274
ce657d80
VS
275 if(link_dpad(link).draw_slice)
276 link_dpad(link).draw_slice(link, y, h);
4dbbcdee
VS
277}
278
05decb00 279AVFilter *avfilter_get_by_name(const char *name)
4dbbcdee 280{
6d8c67a7
VS
281 struct FilterList *filt;
282
283 for(filt = filters; filt; filt = filt->next)
284 if(!strcmp(filt->filter->name, name))
285 return filt->filter;
4dbbcdee 286
4dbbcdee
VS
287 return NULL;
288}
289
4dbbcdee
VS
290void avfilter_register(AVFilter *filter)
291{
6d8c67a7
VS
292 struct FilterList *newfilt = av_malloc(sizeof(struct FilterList));
293
294 newfilt->filter = filter;
295 newfilt->next = filters;
296 filters = newfilt;
4dbbcdee
VS
297}
298
299void avfilter_init(void)
300{
7bce27a3
VS
301 avfilter_register(&avfilter_vf_crop);
302 avfilter_register(&avfilter_vf_fifo);
e946c25a 303 avfilter_register(&avfilter_vf_format);
7bce27a3
VS
304 avfilter_register(&avfilter_vf_fps);
305 avfilter_register(&avfilter_vf_graph);
306 avfilter_register(&avfilter_vf_graphdesc);
307 avfilter_register(&avfilter_vf_graphfile);
c35a0473 308 avfilter_register(&avfilter_vf_hflip);
f9033975 309 avfilter_register(&avfilter_vf_negate);
cc0bb5a1 310 avfilter_register(&avfilter_vf_noformat);
7bce27a3 311 avfilter_register(&avfilter_vf_overlay);
0970ec6a 312 avfilter_register(&avfilter_vf_rotate);
2a553ef5 313 avfilter_register(&avfilter_vf_scale);
0970ec6a 314 avfilter_register(&avfilter_vf_setpts);
7bce27a3
VS
315 avfilter_register(&avfilter_vf_slicify);
316 avfilter_register(&avfilter_vf_split);
be696990 317 avfilter_register(&avfilter_vf_transpose);
7bce27a3 318 avfilter_register(&avfilter_vf_vflip);
b62966c0
VS
319#ifdef CONFIG_AVFILTER_LAVF
320 avfilter_register(&avfilter_vsrc_movie);
321#endif //CONFIG_AVFILTER_LAVF
4dbbcdee
VS
322}
323
324void avfilter_uninit(void)
325{
6d8c67a7
VS
326 struct FilterList *tmp;
327
328 for(; filters; filters = tmp) {
329 tmp = filters->next;
330 av_free(filters);
331 }
4dbbcdee
VS
332}
333
334static int pad_count(const AVFilterPad *pads)
335{
4dbbcdee
VS
336 int count;
337
05decb00 338 for(count = 0; pads->name; count ++) pads ++;
4dbbcdee
VS
339 return count;
340}
341
342static const char *filter_name(void *p)
343{
344 AVFilterContext *filter = p;
345 return filter->filter->name;
346}
347
05decb00 348AVFilterContext *avfilter_open(AVFilter *filter, const char *inst_name)
4dbbcdee 349{
8f618f4c
VS
350 AVFilterContext *ret;
351
352 if (!filter)
353 return 0;
354
355 ret = av_malloc(sizeof(AVFilterContext));
4dbbcdee
VS
356
357 ret->av_class = av_mallocz(sizeof(AVClass));
358 ret->av_class->item_name = filter_name;
359 ret->filter = filter;
2350e69c 360 ret->name = inst_name ? av_strdup(inst_name) : NULL;
4dbbcdee
VS
361 ret->priv = av_mallocz(filter->priv_size);
362
7d0e1392 363 ret->input_count = pad_count(filter->inputs);
c5ef7d7b
VS
364 ret->input_pads = av_malloc(sizeof(AVFilterPad) * ret->input_count);
365 memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad)*ret->input_count);
7d0e1392 366 ret->inputs = av_mallocz(sizeof(AVFilterLink*) * ret->input_count);
c5ef7d7b 367
7d0e1392 368 ret->output_count = pad_count(filter->outputs);
c5ef7d7b
VS
369 ret->output_pads = av_malloc(sizeof(AVFilterPad) * ret->output_count);
370 memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad)*ret->output_count);
7d0e1392
VS
371 ret->outputs = av_mallocz(sizeof(AVFilterLink*) * ret->output_count);
372
4dbbcdee
VS
373 return ret;
374}
375
376void avfilter_destroy(AVFilterContext *filter)
377{
378 int i;
379
380 if(filter->filter->uninit)
381 filter->filter->uninit(filter);
382
c5ef7d7b 383 for(i = 0; i < filter->input_count; i ++) {
4dbbcdee
VS
384 if(filter->inputs[i])
385 filter->inputs[i]->src->outputs[filter->inputs[i]->srcpad] = NULL;
f4cb4462 386 av_freep(&filter->inputs[i]);
4dbbcdee 387 }
c5ef7d7b 388 for(i = 0; i < filter->output_count; i ++) {
4dbbcdee
VS
389 if(filter->outputs[i])
390 filter->outputs[i]->dst->inputs[filter->outputs[i]->dstpad] = NULL;
f4cb4462 391 av_freep(&filter->outputs[i]);
4dbbcdee
VS
392 }
393
f4cb4462
VS
394 av_freep(&filter->name);
395 av_freep(&filter->input_pads);
396 av_freep(&filter->output_pads);
397 av_freep(&filter->inputs);
398 av_freep(&filter->outputs);
399 av_freep(&filter->priv);
400 av_freep(&filter->av_class);
4dbbcdee
VS
401 av_free(filter);
402}
403
a360f71e 404int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
4dbbcdee 405{
e079d22e 406 int ret=0;
4dbbcdee
VS
407
408 if(filter->filter->init)
e079d22e
VS
409 ret = filter->filter->init(filter, args, opaque);
410 return ret;
4dbbcdee
VS
411}
412