3 * copyright (c) 2007 Bobby Bingham
5 * This file is part of FFmpeg.
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.
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.
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
28 #include "allfilters.h"
30 /** list of registered filters, sorted by name */
31 static int filter_count = 0;
32 static AVFilter **filters = NULL;
34 /* TODO: buffer pool. see comment for avfilter_default_get_video_buffer() */
35 void avfilter_default_free_video_buffer(AVFilterPic *pic)
37 avpicture_free((AVPicture *) pic);
41 /* TODO: set the buffer's priv member to a context structure for the whole
42 * filter chain. This will allow for a buffer pool instead of the constant
43 * alloc & free cycle currently implemented. */
44 AVFilterPicRef *avfilter_default_get_video_buffer(AVFilterLink *link, int perms)
46 AVFilterPic *pic = av_mallocz(sizeof(AVFilterPic));
47 AVFilterPicRef *ref = av_mallocz(sizeof(AVFilterPicRef));
55 pic->format = link->format;
56 pic->free = avfilter_default_free_video_buffer;
57 avpicture_alloc((AVPicture *)pic, pic->format, ref->w, ref->h);
59 memcpy(ref->data, pic->data, sizeof(pic->data));
60 memcpy(ref->linesize, pic->linesize, sizeof(pic->linesize));
65 void avfilter_default_start_frame(AVFilterLink *link, AVFilterPicRef *picref)
67 AVFilterLink *out = NULL;
69 if(link->dst->output_count)
70 out = link->dst->outputs[0];
72 link->cur_pic = picref;
75 out->outpic = avfilter_get_video_buffer(out, AV_PERM_WRITE);
76 out->outpic->pts = picref->pts;
77 avfilter_start_frame(out, avfilter_ref_pic(out->outpic, ~0));
81 void avfilter_default_end_frame(AVFilterLink *link)
83 AVFilterLink *out = NULL;
85 if(link->dst->output_count)
86 out = link->dst->outputs[0];
88 avfilter_unref_pic(link->cur_pic);
92 avfilter_unref_pic(out->outpic);
94 avfilter_end_frame(out);
98 AVFilterPicRef *avfilter_ref_pic(AVFilterPicRef *ref, int pmask)
100 AVFilterPicRef *ret = av_malloc(sizeof(AVFilterPicRef));
101 memcpy(ret, ref, sizeof(AVFilterPicRef));
103 ret->pic->refcount ++;
107 void avfilter_unref_pic(AVFilterPicRef *ref)
109 if(-- ref->pic->refcount == 0)
110 ref->pic->free(ref->pic);
115 * default config_link() implementation for output video links to simplify
116 * the implementation of one input one output video filters */
117 static int default_config_output_link(AVFilterLink *link)
119 if(link->src->input_count && link->src->inputs[0]) {
120 link->w = link->src->inputs[0]->w;
121 link->h = link->src->inputs[0]->h;
123 /* XXX: any non-simple filter which would cause this branch to be taken
124 * really should implement its own config_props() for this link. */
133 * default query_formats() implementation for output video links to simplify
134 * the implementation of one input one output video filters */
135 static int *default_query_output_formats(AVFilterLink *link)
137 if(link->src->input_count && link->src->inputs[0])
138 return avfilter_make_format_list(1, link->src->inputs[0]->format);
140 /* XXX: any non-simple filter which would cause this branch to be taken
141 * really should implement its own query_formats() for this link */
142 return avfilter_make_format_list(0);
145 int avfilter_link(AVFilterContext *src, unsigned srcpad,
146 AVFilterContext *dst, unsigned dstpad)
151 if(src->output_count <= srcpad || dst->input_count <= dstpad ||
152 src->outputs[srcpad] || dst->inputs[dstpad])
155 src->outputs[srcpad] =
156 dst->inputs[dstpad] = link = av_malloc(sizeof(AVFilterLink));
160 link->srcpad = srcpad;
161 link->dstpad = dstpad;
162 link->cur_pic = NULL;
164 /* find a format both filters support - TODO: auto-insert conversion filter */
166 if(src->filter->outputs[srcpad].query_formats)
167 fmts[0] = src->filter->outputs[srcpad].query_formats(link);
169 fmts[0] = default_query_output_formats(link);
170 fmts[1] = dst->filter-> inputs[dstpad].query_formats(link);
171 for(i = 0; fmts[0][i] != -1; i ++)
172 for(j = 0; fmts[1][j] != -1; j ++)
173 if(fmts[0][i] == fmts[1][j]) {
174 link->format = fmts[0][i];
181 if(link->format == -1) {
182 /* failed to find a format. fail at creating the link */
184 src->outputs[srcpad] = NULL;
185 dst->inputs[dstpad] = NULL;
189 if (src->filter->outputs[srcpad].config_props)
190 src->filter->outputs[srcpad].config_props(link);
192 default_config_output_link(link);
194 if (dst->filter->inputs[dstpad].config_props)
195 dst->filter->inputs[dstpad].config_props(link);
200 AVFilterPicRef *avfilter_get_video_buffer(AVFilterLink *link, int perms)
202 AVFilterPicRef *ret = NULL;
204 if(link->dst->filter->inputs[link->dstpad].get_video_buffer)
205 ret = link->dst->filter->inputs[link->dstpad].get_video_buffer(link, perms);
208 ret = avfilter_default_get_video_buffer(link, perms);
213 void avfilter_request_frame(AVFilterLink *link)
215 const AVFilterPad *pad = &link->src->filter->outputs[link->srcpad];
217 if(pad->request_frame)
218 pad->request_frame(link);
219 else if(link->src->inputs[0])
220 avfilter_request_frame(link->src->inputs[0]);
223 /* XXX: should we do the duplicating of the picture ref here, instead of
224 * forcing the source filter to do it? */
225 void avfilter_start_frame(AVFilterLink *link, AVFilterPicRef *picref)
227 void (*start_frame)(AVFilterLink *, AVFilterPicRef *);
229 start_frame = link->dst->filter->inputs[link->dstpad].start_frame;
231 start_frame = avfilter_default_start_frame;
233 start_frame(link, picref);
236 void avfilter_end_frame(AVFilterLink *link)
238 void (*end_frame)(AVFilterLink *);
240 end_frame = link->dst->filter->inputs[link->dstpad].end_frame;
242 end_frame = avfilter_default_end_frame;
247 void avfilter_draw_slice(AVFilterLink *link, uint8_t *data[4], int y, int h)
249 if(!link->dst->filter->inputs[link->dstpad].draw_slice)
252 link->dst->filter->inputs[link->dstpad].draw_slice(link, data, y, h);
255 static int filter_cmp(const void *aa, const void *bb)
257 const AVFilter *a = *(const AVFilter **)aa, *b = *(const AVFilter **)bb;
258 return strcmp(a->name, b->name);
261 AVFilter *avfilter_get_by_name(char *name)
263 AVFilter key = { .name = name, };
264 AVFilter *key2 = &key;
267 ret = bsearch(&key2, filters, filter_count, sizeof(AVFilter **), filter_cmp);
273 /* FIXME: insert in order, rather than insert at end + resort */
274 void avfilter_register(AVFilter *filter)
276 filters = av_realloc(filters, sizeof(AVFilter*) * (filter_count+1));
277 filters[filter_count] = filter;
278 qsort(filters, ++filter_count, sizeof(AVFilter **), filter_cmp);
281 void avfilter_init(void)
283 avfilter_register(&vsrc_dummy);
284 avfilter_register(&vsrc_ppm);
285 avfilter_register(&vf_crop);
286 avfilter_register(&vf_passthrough);
287 avfilter_register(&vf_rgb2bgr);
288 avfilter_register(&vf_slicify);
289 avfilter_register(&vo_sdl);
292 void avfilter_uninit(void)
298 static int pad_count(const AVFilterPad *pads)
300 AVFilterPad *p = (AVFilterPad *) pads;
303 for(count = 0; p->name; count ++) p ++;
307 static const char *filter_name(void *p)
309 AVFilterContext *filter = p;
310 return filter->filter->name;
313 AVFilterContext *avfilter_create(AVFilter *filter, char *inst_name)
315 AVFilterContext *ret = av_malloc(sizeof(AVFilterContext));
317 ret->av_class = av_mallocz(sizeof(AVClass));
318 ret->av_class->item_name = filter_name;
319 ret->filter = filter;
320 ret->name = inst_name ? av_strdup(inst_name) : NULL;
321 ret->priv = av_mallocz(filter->priv_size);
323 ret->input_count = pad_count(filter->inputs);
324 ret->inputs = av_mallocz(sizeof(AVFilterLink*) * ret->input_count);
325 ret->output_count = pad_count(filter->outputs);
326 ret->outputs = av_mallocz(sizeof(AVFilterLink*) * ret->output_count);
331 void avfilter_destroy(AVFilterContext *filter)
335 if(filter->filter->uninit)
336 filter->filter->uninit(filter);
338 for(i = 0; i < pad_count(filter->filter->inputs); i ++) {
339 if(filter->inputs[i])
340 filter->inputs[i]->src->outputs[filter->inputs[i]->srcpad] = NULL;
341 av_free(filter->inputs[i]);
343 for(i = 0; i < pad_count(filter->filter->outputs); i ++) {
344 if(filter->outputs[i])
345 filter->outputs[i]->dst->inputs[filter->outputs[i]->dstpad] = NULL;
346 av_free(filter->outputs[i]);
349 av_free(filter->name);
350 av_free(filter->inputs);
351 av_free(filter->outputs);
352 av_free(filter->priv);
353 av_free(filter->av_class);
357 AVFilterContext *avfilter_create_by_name(char *name, char *inst_name)
361 if(!(filt = avfilter_get_by_name(name))) return NULL;
362 return avfilter_create(filt, inst_name);
365 int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
369 if(filter->filter->init)
370 if((ret = filter->filter->init(filter, args, opaque))) return ret;
374 int *avfilter_make_format_list(int len, ...)
379 ret = av_malloc(sizeof(int) * (len + 1));
381 for(i = 0; i < len; i ++)
382 ret[i] = va_arg(vl, int);