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