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