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
23 #include "allfilters.h"
25 /** list of registered filters */
29 struct FilterList *next;
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]
36 AVFilterPicRef *avfilter_ref_pic(AVFilterPicRef *ref, int pmask)
38 AVFilterPicRef *ret = av_malloc(sizeof(AVFilterPicRef));
41 ret->pic->refcount ++;
45 void avfilter_unref_pic(AVFilterPicRef *ref)
47 if(!(--ref->pic->refcount))
48 ref->pic->free(ref->pic);
52 void avfilter_insert_pad(unsigned idx, unsigned *count, size_t padidx_off,
53 AVFilterPad **pads, AVFilterLink ***links,
58 idx = FFMIN(idx, *count);
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));
68 for(i = idx+1; i < *count; i ++)
70 (*(unsigned *)((uint8_t *) *links[i] + padidx_off)) ++;
73 int avfilter_link(AVFilterContext *src, unsigned srcpad,
74 AVFilterContext *dst, unsigned dstpad)
78 if(src->output_count <= srcpad || dst->input_count <= dstpad ||
79 src->outputs[srcpad] || dst->inputs[dstpad])
82 src->outputs[srcpad] =
83 dst-> inputs[dstpad] = link = av_mallocz(sizeof(AVFilterLink));
87 link->srcpad = srcpad;
88 link->dstpad = dstpad;
94 int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt,
95 unsigned in, unsigned out)
97 av_log(link->dst, AV_LOG_INFO, "auto-inserting filter '%s'\n",
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;
107 /* re-hookup the link to the new destination filter we inserted */
110 filt->inputs[in] = link;
112 /* if any information on supported colorspaces already exists on the
113 * link, we need to preserve that */
114 if(link->out_formats)
115 avfilter_formats_changeref(&link->out_formats,
116 &filt->outputs[out]->out_formats);
121 int avfilter_config_links(AVFilterContext *filter)
123 int (*config_link)(AVFilterLink *);
126 for(i = 0; i < filter->input_count; i ++) {
127 AVFilterLink *link = filter->inputs[i];
131 switch(link->init_state) {
134 case AVLINK_STARTINIT:
135 av_log(filter, AV_LOG_INFO, "circular filter chain detected\n");
138 link->init_state = AVLINK_STARTINIT;
140 if(avfilter_config_links(link->src))
143 if(!(config_link = link_spad(link).config_props))
144 config_link = avfilter_default_config_output_link;
145 if(config_link(link))
148 if((config_link = link_dpad(link).config_props))
149 if(config_link(link))
152 link->init_state = AVLINK_INIT;
159 AVFilterPicRef *avfilter_get_video_buffer(AVFilterLink *link, int perms)
161 AVFilterPicRef *ret = NULL;
163 if(link_dpad(link).get_video_buffer)
164 ret = link_dpad(link).get_video_buffer(link, perms);
167 ret = avfilter_default_get_video_buffer(link, perms);
172 int avfilter_request_frame(AVFilterLink *link)
174 if(link_spad(link).request_frame)
175 return link_spad(link).request_frame(link);
176 else if(link->src->inputs[0])
177 return avfilter_request_frame(link->src->inputs[0]);
181 int avfilter_poll_frame(AVFilterLink *link)
185 if(link_spad(link).poll_frame)
186 return link_spad(link).poll_frame(link);
187 for (i=0; i<link->src->input_count; i++) {
188 if(!link->src->inputs[i])
190 min = FFMIN(min, avfilter_poll_frame(link->src->inputs[i]));
196 /* XXX: should we do the duplicating of the picture ref here, instead of
197 * forcing the source filter to do it? */
198 void avfilter_start_frame(AVFilterLink *link, AVFilterPicRef *picref)
200 void (*start_frame)(AVFilterLink *, AVFilterPicRef *);
201 AVFilterPad *dst = &link_dpad(link);
203 if(!(start_frame = dst->start_frame))
204 start_frame = avfilter_default_start_frame;
206 /* prepare to copy the picture if it has insufficient permissions */
207 if((dst->min_perms & picref->perms) != dst->min_perms ||
208 dst->rej_perms & picref->perms) {
210 av_log(link->dst, AV_LOG_INFO,
211 "frame copy needed (have perms %x, need %x, reject %x)\n",
213 link_dpad(link).min_perms, link_dpad(link).rej_perms);
216 link->cur_pic = avfilter_default_get_video_buffer(link, dst->min_perms);
217 link->srcpic = picref;
218 link->cur_pic->pts = link->srcpic->pts;
221 link->cur_pic = picref;
223 start_frame(link, link->cur_pic);
226 void avfilter_end_frame(AVFilterLink *link)
228 void (*end_frame)(AVFilterLink *);
230 if(!(end_frame = link_dpad(link).end_frame))
231 end_frame = avfilter_default_end_frame;
235 /* unreference the source picture if we're feeding the destination filter
236 * a copied version dues to permission issues */
238 avfilter_unref_pic(link->srcpic);
244 void avfilter_draw_slice(AVFilterLink *link, int y, int h)
246 uint8_t *src[4], *dst[4];
247 int i, j, hsub, vsub;
249 /* copy the slice if needed for permission reasons */
251 avcodec_get_chroma_sub_sample(link->format, &hsub, &vsub);
253 src[0] = link->srcpic-> data[0] + y * link->srcpic-> linesize[0];
254 dst[0] = link->cur_pic->data[0] + y * link->cur_pic->linesize[0];
255 for(i = 1; i < 4; i ++) {
256 if(link->srcpic->data[i]) {
257 src[i] = link->srcpic-> data[i] + (y >> vsub) * link->srcpic-> linesize[i];
258 dst[i] = link->cur_pic->data[i] + (y >> vsub) * link->cur_pic->linesize[i];
260 src[i] = dst[i] = NULL;
263 for(i = 0; i < 4; i ++) {
264 if(!src[i]) continue;
266 for(j = 0; j < h >> (i==0 ? 0 : vsub); j ++) {
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];
274 if(!link_dpad(link).draw_slice)
277 link_dpad(link).draw_slice(link, y, h);
280 AVFilter *avfilter_get_by_name(const char *name)
282 struct FilterList *filt;
284 for(filt = filters; filt; filt = filt->next)
285 if(!strcmp(filt->filter->name, name))
291 void avfilter_register(AVFilter *filter)
293 struct FilterList *newfilt = av_malloc(sizeof(struct FilterList));
295 newfilt->filter = filter;
296 newfilt->next = filters;
300 void avfilter_init(void)
302 avfilter_register(&avfilter_vf_crop);
303 avfilter_register(&avfilter_vf_fifo);
304 avfilter_register(&avfilter_vf_format);
305 avfilter_register(&avfilter_vf_fps);
306 avfilter_register(&avfilter_vf_graph);
307 avfilter_register(&avfilter_vf_graphdesc);
308 avfilter_register(&avfilter_vf_graphfile);
309 avfilter_register(&avfilter_vf_hflip);
310 avfilter_register(&avfilter_vf_negate);
311 avfilter_register(&avfilter_vf_noformat);
312 avfilter_register(&avfilter_vf_overlay);
313 avfilter_register(&avfilter_vf_rotate);
314 avfilter_register(&avfilter_vf_scale);
315 avfilter_register(&avfilter_vf_setpts);
316 avfilter_register(&avfilter_vf_slicify);
317 avfilter_register(&avfilter_vf_split);
318 avfilter_register(&avfilter_vf_transpose);
319 avfilter_register(&avfilter_vf_vflip);
320 #ifdef CONFIG_AVFILTER_LAVF
321 avfilter_register(&avfilter_vsrc_movie);
322 #endif //CONFIG_AVFILTER_LAVF
325 void avfilter_uninit(void)
327 struct FilterList *tmp;
329 for(; filters; filters = tmp) {
335 static int pad_count(const AVFilterPad *pads)
339 for(count = 0; pads->name; count ++) pads ++;
343 static const char *filter_name(void *p)
345 AVFilterContext *filter = p;
346 return filter->filter->name;
349 AVFilterContext *avfilter_open(AVFilter *filter, const char *inst_name)
351 AVFilterContext *ret;
356 ret = av_malloc(sizeof(AVFilterContext));
358 ret->av_class = av_mallocz(sizeof(AVClass));
359 ret->av_class->item_name = filter_name;
360 ret->filter = filter;
361 ret->name = inst_name ? av_strdup(inst_name) : NULL;
362 ret->priv = av_mallocz(filter->priv_size);
364 ret->input_count = pad_count(filter->inputs);
365 ret->input_pads = av_malloc(sizeof(AVFilterPad) * ret->input_count);
366 memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad)*ret->input_count);
367 ret->inputs = av_mallocz(sizeof(AVFilterLink*) * ret->input_count);
369 ret->output_count = pad_count(filter->outputs);
370 ret->output_pads = av_malloc(sizeof(AVFilterPad) * ret->output_count);
371 memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad)*ret->output_count);
372 ret->outputs = av_mallocz(sizeof(AVFilterLink*) * ret->output_count);
377 void avfilter_destroy(AVFilterContext *filter)
381 if(filter->filter->uninit)
382 filter->filter->uninit(filter);
384 for(i = 0; i < filter->input_count; i ++) {
385 if(filter->inputs[i])
386 filter->inputs[i]->src->outputs[filter->inputs[i]->srcpad] = NULL;
387 av_freep(&filter->inputs[i]);
389 for(i = 0; i < filter->output_count; i ++) {
390 if(filter->outputs[i])
391 filter->outputs[i]->dst->inputs[filter->outputs[i]->dstpad] = NULL;
392 av_freep(&filter->outputs[i]);
395 av_freep(&filter->name);
396 av_freep(&filter->input_pads);
397 av_freep(&filter->output_pads);
398 av_freep(&filter->inputs);
399 av_freep(&filter->outputs);
400 av_freep(&filter->priv);
401 av_freep(&filter->av_class);
405 int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
409 if(filter->filter->init)
410 ret = filter->filter->init(filter, args, opaque);