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 */
34 struct FilterList *next;
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]
41 AVFilterPicRef *avfilter_ref_pic(AVFilterPicRef *ref, int pmask)
43 AVFilterPicRef *ret = av_malloc(sizeof(AVFilterPicRef));
46 ret->pic->refcount ++;
50 void avfilter_unref_pic(AVFilterPicRef *ref)
52 if(-- ref->pic->refcount == 0)
53 ref->pic->free(ref->pic);
57 void avfilter_insert_pad(unsigned idx, unsigned *count, size_t padidx_off,
58 AVFilterPad **pads, AVFilterLink ***links,
63 idx = FFMIN(idx, *count);
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));
73 for(i = idx+1; i < *count; i ++)
75 (*(unsigned *)((uint8_t *)(*links[i]) + padidx_off)) ++;
78 int avfilter_link(AVFilterContext *src, unsigned srcpad,
79 AVFilterContext *dst, unsigned dstpad)
83 if(src->output_count <= srcpad || dst->input_count <= dstpad ||
84 src->outputs[srcpad] || dst->inputs[dstpad])
87 src->outputs[srcpad] =
88 dst->inputs[dstpad] = link = av_mallocz(sizeof(AVFilterLink));
92 link->srcpad = srcpad;
93 link->dstpad = dstpad;
99 int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt,
100 unsigned in, unsigned out)
102 AVFilterFormats *formats;
104 av_log(NULL, AV_LOG_INFO, "auto-inserting filter '%s'\n",
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;
114 /* re-hookup the link to the new destination filter we inserted */
117 filt->inputs[in] = link;
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);
128 int avfilter_config_links(AVFilterContext *filter)
130 int (*config_link)(AVFilterLink *);
133 for(i = 0; i < filter->input_count; i ++) {
136 if(!(link = filter->inputs[i])) continue;
138 switch(link->init_state) {
141 case AVLINK_STARTINIT:
142 av_log(filter, AV_LOG_ERROR, "circular filter chain detected\n");
145 link->init_state = AVLINK_STARTINIT;
147 if(avfilter_config_links(link->src))
150 if(!(config_link = link_spad(link).config_props))
151 config_link = avfilter_default_config_output_link;
152 if(config_link(link))
155 if((config_link = link_dpad(link).config_props))
156 if(config_link(link))
159 link->init_state = AVLINK_INIT;
166 AVFilterPicRef *avfilter_get_video_buffer(AVFilterLink *link, int perms)
168 AVFilterPicRef *ret = NULL;
170 if(link_dpad(link).get_video_buffer)
171 ret = link_dpad(link).get_video_buffer(link, perms);
174 ret = avfilter_default_get_video_buffer(link, perms);
179 int avfilter_request_frame(AVFilterLink *link)
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]);
188 int avfilter_poll_frame(AVFilterLink *link)
192 if(link_spad(link).poll_frame)
193 return link_spad(link).poll_frame(link);
195 for (i=0; i<link->src->input_count; i++) {
196 if(!link->src->inputs[i])
198 min = FFMIN(min, avfilter_poll_frame(link->src->inputs[i]));
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)
208 void (*start_frame)(AVFilterLink *, AVFilterPicRef *);
210 if(!(start_frame = link_dpad(link).start_frame))
211 start_frame = avfilter_default_start_frame;
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) {
217 av_log(link->dst, AV_LOG_INFO,
218 "frame copy needed (have perms %x, need %x, reject %x)\n",
220 link_dpad(link).min_perms, link_dpad(link).rej_perms);
223 link->cur_pic = avfilter_default_get_video_buffer(link, link_dpad(link).min_perms);
224 link->srcpic = picref;
227 link->cur_pic = picref;
229 start_frame(link, link->cur_pic);
232 void avfilter_end_frame(AVFilterLink *link)
234 void (*end_frame)(AVFilterLink *);
236 if(!(end_frame = link_dpad(link).end_frame))
237 end_frame = avfilter_default_end_frame;
241 /* unreference the source picture if we're feeding the destination filter
242 * a copied version dues to permission issues */
244 avfilter_unref_pic(link->srcpic);
250 void avfilter_draw_slice(AVFilterLink *link, int y, int h)
252 uint8_t *src[4], *dst[4];
253 int i, j, hsub, vsub;
255 /* copy the slice if needed for permission reasons */
257 avcodec_get_chroma_sub_sample(link->format, &hsub, &vsub);
259 link->cur_pic->pts = link->srcpic->pts;
260 src[0] = link->srcpic-> data[0] + y * link->srcpic-> linesize[0];
261 dst[0] = link->cur_pic->data[0] + y * link->cur_pic->linesize[0];
262 for(i = 1; i < 4; i ++) {
263 if(link->srcpic->data[i]) {
264 src[i] = link->srcpic-> data[i] + (y >> vsub) * link->srcpic-> linesize[i];
265 dst[i] = link->cur_pic->data[i] + (y >> vsub) * link->cur_pic->linesize[i];
267 src[i] = dst[i] = NULL;
269 for(j = 0; j < h; j ++) {
270 memcpy(dst[0], src[0], link->cur_pic->linesize[0]);
271 src[0] += link->srcpic ->linesize[0];
272 dst[0] += link->cur_pic->linesize[0];
274 for(i = 1; i < 4; i ++) {
275 if(!src[i]) continue;
277 for(j = 0; j < h >> vsub; j ++) {
278 memcpy(dst[i], src[i], link->cur_pic->linesize[i]);
279 src[i] += link->srcpic ->linesize[i];
280 dst[i] += link->cur_pic->linesize[i];
285 if(!link_dpad(link).draw_slice)
288 link_dpad(link).draw_slice(link, y, h);
291 AVFilter *avfilter_get_by_name(const char *name)
293 struct FilterList *filt;
295 for(filt = filters; filt; filt = filt->next)
296 if(!strcmp(filt->filter->name, name))
302 void avfilter_register(AVFilter *filter)
304 struct FilterList *newfilt = av_malloc(sizeof(struct FilterList));
306 newfilt->filter = filter;
307 newfilt->next = filters;
311 void avfilter_init(void)
313 avfilter_register(&avfilter_vf_crop);
314 avfilter_register(&avfilter_vf_fifo);
315 avfilter_register(&avfilter_vf_format);
316 avfilter_register(&avfilter_vf_fps);
317 avfilter_register(&avfilter_vf_graph);
318 avfilter_register(&avfilter_vf_graphdesc);
319 avfilter_register(&avfilter_vf_graphfile);
320 avfilter_register(&avfilter_vf_hflip);
321 avfilter_register(&avfilter_vf_negate);
322 avfilter_register(&avfilter_vf_noformat);
323 avfilter_register(&avfilter_vf_overlay);
324 avfilter_register(&avfilter_vf_rotate);
325 avfilter_register(&avfilter_vf_scale);
326 avfilter_register(&avfilter_vf_setpts);
327 avfilter_register(&avfilter_vf_slicify);
328 avfilter_register(&avfilter_vf_split);
329 avfilter_register(&avfilter_vf_transpose);
330 avfilter_register(&avfilter_vf_vflip);
331 #ifdef CONFIG_AVFILTER_LAVF
332 avfilter_register(&avfilter_vsrc_movie);
333 #endif //CONFIG_AVFILTER_LAVF
336 void avfilter_uninit(void)
338 struct FilterList *tmp;
340 for(; filters; filters = tmp) {
346 static int pad_count(const AVFilterPad *pads)
350 for(count = 0; pads->name; count ++) pads ++;
354 static const char *filter_name(void *p)
356 AVFilterContext *filter = p;
357 return filter->filter->name;
360 AVFilterContext *avfilter_open(AVFilter *filter, const char *inst_name)
362 AVFilterContext *ret;
367 ret = av_malloc(sizeof(AVFilterContext));
369 ret->av_class = av_mallocz(sizeof(AVClass));
370 ret->av_class->item_name = filter_name;
371 ret->filter = filter;
372 ret->name = inst_name ? av_strdup(inst_name) : NULL;
373 ret->priv = av_mallocz(filter->priv_size);
375 ret->input_count = pad_count(filter->inputs);
376 ret->input_pads = av_malloc(sizeof(AVFilterPad) * ret->input_count);
377 memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad)*ret->input_count);
378 ret->inputs = av_mallocz(sizeof(AVFilterLink*) * ret->input_count);
380 ret->output_count = pad_count(filter->outputs);
381 ret->output_pads = av_malloc(sizeof(AVFilterPad) * ret->output_count);
382 memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad)*ret->output_count);
383 ret->outputs = av_mallocz(sizeof(AVFilterLink*) * ret->output_count);
388 void avfilter_destroy(AVFilterContext *filter)
392 if(filter->filter->uninit)
393 filter->filter->uninit(filter);
395 for(i = 0; i < filter->input_count; i ++) {
396 if(filter->inputs[i])
397 filter->inputs[i]->src->outputs[filter->inputs[i]->srcpad] = NULL;
398 av_freep(&filter->inputs[i]);
400 for(i = 0; i < filter->output_count; i ++) {
401 if(filter->outputs[i])
402 filter->outputs[i]->dst->inputs[filter->outputs[i]->dstpad] = NULL;
403 av_freep(&filter->outputs[i]);
406 av_freep(&filter->name);
407 av_freep(&filter->input_pads);
408 av_freep(&filter->output_pads);
409 av_freep(&filter->inputs);
410 av_freep(&filter->outputs);
411 av_freep(&filter->priv);
412 av_freep(&filter->av_class);
416 int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
420 if(filter->filter->init)
421 if((ret = filter->filter->init(filter, args, opaque))) return ret;