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
27 #include "allfilters.h"
29 /** list of registered filters, sorted by name */
30 static int filter_count
= 0;
31 static AVFilter
**filters
= NULL
;
33 /* TODO: buffer pool. see comment for avfilter_default_get_video_buffer() */
34 void avfilter_default_free_video_buffer(AVFilterPic
*pic
)
36 avpicture_free((AVPicture
*) pic
);
40 /* TODO: set the buffer's priv member to a context structure for the whole
41 * filter chain. This will allow for a buffer pool instead of the constant
42 * alloc & free cycle currently implemented. */
43 AVFilterPicRef
*avfilter_default_get_video_buffer(AVFilterLink
*link
, int perms
)
45 AVFilterPic
*pic
= av_mallocz(sizeof(AVFilterPic
));
46 AVFilterPicRef
*ref
= av_mallocz(sizeof(AVFilterPicRef
));
54 pic
->format
= link
->format
;
55 pic
->free
= avfilter_default_free_video_buffer
;
56 avpicture_alloc((AVPicture
*)pic
, pic
->format
, ref
->w
, ref
->h
);
58 memcpy(ref
->data
, pic
->data
, sizeof(int *) * 4);
63 void avfilter_default_start_frame(AVFilterLink
*link
, AVFilterPicRef
*picref
)
65 link
->cur_pic
= picref
;
68 void avfilter_default_end_frame(AVFilterLink
*link
)
70 avfilter_unref_pic(link
->cur_pic
);
74 AVFilterPicRef
*avfilter_ref_pic(AVFilterPicRef
*ref
)
76 AVFilterPicRef
*ret
= av_malloc(sizeof(AVFilterPicRef
));
77 memcpy(ret
, ref
, sizeof(AVFilterPicRef
));
78 ret
->pic
->refcount
++;
82 void avfilter_unref_pic(AVFilterPicRef
*ref
)
84 if(-- ref
->pic
->refcount
== 0)
85 ref
->pic
->free(ref
->pic
);
89 int avfilter_link(AVFilterContext
*src
, unsigned srcpad
,
90 AVFilterContext
*dst
, unsigned dstpad
)
94 if(src
->outputs
[srcpad
] || dst
->inputs
[dstpad
])
97 src
->outputs
[srcpad
] =
98 dst
->inputs
[dstpad
] = link
= av_malloc(sizeof(AVFilterLink
));
102 link
->srcpad
= srcpad
;
103 link
->dstpad
= dstpad
;
104 link
->cur_pic
= NULL
;
106 src
->filter
->outputs
[dstpad
].set_video_props(link
);
110 AVFilterPicRef
*avfilter_get_video_buffer(AVFilterLink
*link
, int perms
)
112 AVFilterPicRef
*ret
= NULL
;
114 if(link
->dst
->filter
->inputs
[link
->dstpad
].get_video_buffer
)
115 ret
= link
->dst
->filter
->inputs
[link
->dstpad
].get_video_buffer(link
, perms
);
118 ret
= avfilter_default_get_video_buffer(link
, perms
);
123 void avfilter_request_frame(AVFilterLink
*link
)
125 link
->src
->filter
->outputs
[link
->srcpad
].request_frame(link
);
128 /* XXX: should we do the duplicating of the picture ref here, instead of
129 * forcing the source filter to do it? */
130 void avfilter_start_frame(AVFilterLink
*link
, AVFilterPicRef
*picref
)
132 void (*start_frame
)(AVFilterLink
*, AVFilterPicRef
*);
134 start_frame
= link
->dst
->filter
->inputs
[link
->dstpad
].start_frame
;
136 start_frame
= avfilter_default_start_frame
;
138 start_frame(link
, picref
);
141 void avfilter_end_frame(AVFilterLink
*link
)
143 void (*end_frame
)(AVFilterLink
*);
145 end_frame
= link
->dst
->filter
->inputs
[link
->dstpad
].end_frame
;
147 end_frame
= avfilter_default_end_frame
;
152 void avfilter_draw_slice(AVFilterLink
*link
, uint8_t *data
[4], int y
, int h
)
154 link
->dst
->filter
->inputs
[link
->dstpad
].draw_slice(link
, data
, y
, h
);
157 static int filter_cmp(const void *aa
, const void *bb
)
159 const AVFilter
*a
= *(const AVFilter
**)aa
, *b
= *(const AVFilter
**)bb
;
160 return strcmp(a
->name
, b
->name
);
163 AVFilter
*avfilter_get_by_name(char *name
)
165 AVFilter key
= { .name
= name
, };
166 AVFilter
*key2
= &key
;
169 ret
= bsearch(&key2
, filters
, filter_count
, sizeof(AVFilter
**), filter_cmp
);
175 /* FIXME: insert in order, rather than insert at end + resort */
176 void avfilter_register(AVFilter
*filter
)
178 filters
= av_realloc(filters
, sizeof(AVFilter
*) * (filter_count
+1));
179 filters
[filter_count
] = filter
;
180 qsort(filters
, ++filter_count
, sizeof(AVFilter
**), filter_cmp
);
183 void avfilter_init(void)
185 avfilter_register(&vsrc_dummy
);
186 avfilter_register(&vf_crop
);
187 avfilter_register(&vf_passthrough
);
188 avfilter_register(&vo_sdl
);
191 void avfilter_uninit(void)
197 static int pad_count(const AVFilterPad
*pads
)
199 AVFilterPad
*p
= (AVFilterPad
*) pads
;
202 for(count
= 0; p
->name
; count
++) p
++;
206 static const char *filter_name(void *p
)
208 AVFilterContext
*filter
= p
;
209 return filter
->filter
->name
;
212 AVFilterContext
*avfilter_create(AVFilter
*filter
)
214 AVFilterContext
*ret
= av_malloc(sizeof(AVFilterContext
));
216 ret
->av_class
= av_mallocz(sizeof(AVClass
));
217 ret
->av_class
->item_name
= filter_name
;
218 ret
->filter
= filter
;
219 ret
->inputs
= av_mallocz(sizeof(AVFilterLink
*) * pad_count(filter
->inputs
));
220 ret
->outputs
= av_mallocz(sizeof(AVFilterLink
*) * pad_count(filter
->outputs
));
221 ret
->priv
= av_mallocz(filter
->priv_size
);
226 void avfilter_destroy(AVFilterContext
*filter
)
230 if(filter
->filter
->uninit
)
231 filter
->filter
->uninit(filter
);
233 for(i
= 0; i
< pad_count(filter
->filter
->inputs
); i
++) {
234 if(filter
->inputs
[i
])
235 filter
->inputs
[i
]->src
->outputs
[filter
->inputs
[i
]->srcpad
] = NULL
;
236 av_free(filter
->inputs
[i
]);
238 for(i
= 0; i
< pad_count(filter
->filter
->outputs
); i
++) {
239 if(filter
->outputs
[i
])
240 filter
->outputs
[i
]->dst
->inputs
[filter
->outputs
[i
]->dstpad
] = NULL
;
241 av_free(filter
->outputs
[i
]);
244 av_free(filter
->inputs
);
245 av_free(filter
->outputs
);
246 av_free(filter
->priv
);
247 av_free(filter
->av_class
);
251 AVFilterContext
*avfilter_create_by_name(char *name
)
255 if(!(filt
= avfilter_get_by_name(name
))) return NULL
;
256 return avfilter_create(filt
);
259 int avfilter_init_filter(AVFilterContext
*filter
)
263 if(filter
->filter
->init
)
264 if((ret
= filter
->filter
->init(filter
))) return ret
;
265 for(i
= 0; i
< pad_count(filter
->filter
->outputs
); i
++)
266 if(filter
->outputs
[i
])
267 filter
->filter
->outputs
[i
].set_video_props(filter
->outputs
[i
]);