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