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
24 #include "libavcodec/imgconvert.h"
27 unsigned avfilter_version(void) {
28 return LIBAVFILTER_VERSION_INT;
31 const char *avfilter_configuration(void)
33 return FFMPEG_CONFIGURATION;
36 const char *avfilter_license(void)
38 #define LICENSE_PREFIX "libavfilter license: "
39 return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
42 /** helper macros to get the in/out pad on the dst/src filter */
43 #define link_dpad(link) link->dst-> input_pads[link->dstpad]
44 #define link_spad(link) link->src->output_pads[link->srcpad]
46 AVFilterPicRef *avfilter_ref_pic(AVFilterPicRef *ref, int pmask)
48 AVFilterPicRef *ret = av_malloc(sizeof(AVFilterPicRef));
51 ret->pic->refcount ++;
55 void avfilter_unref_pic(AVFilterPicRef *ref)
57 if(!(--ref->pic->refcount))
58 ref->pic->free(ref->pic);
62 void avfilter_insert_pad(unsigned idx, unsigned *count, size_t padidx_off,
63 AVFilterPad **pads, AVFilterLink ***links,
68 idx = FFMIN(idx, *count);
70 *pads = av_realloc(*pads, sizeof(AVFilterPad) * (*count + 1));
71 *links = av_realloc(*links, sizeof(AVFilterLink*) * (*count + 1));
72 memmove(*pads +idx+1, *pads +idx, sizeof(AVFilterPad) * (*count-idx));
73 memmove(*links+idx+1, *links+idx, sizeof(AVFilterLink*) * (*count-idx));
74 memcpy(*pads+idx, newpad, sizeof(AVFilterPad));
78 for(i = idx+1; i < *count; i ++)
80 (*(unsigned *)((uint8_t *) *links[i] + padidx_off)) ++;
83 int avfilter_link(AVFilterContext *src, unsigned srcpad,
84 AVFilterContext *dst, unsigned dstpad)
88 if(src->output_count <= srcpad || dst->input_count <= dstpad ||
89 src->outputs[srcpad] || dst->inputs[dstpad])
92 src->outputs[srcpad] =
93 dst-> inputs[dstpad] = link = av_mallocz(sizeof(AVFilterLink));
97 link->srcpad = srcpad;
98 link->dstpad = dstpad;
99 link->format = PIX_FMT_NONE;
104 int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt,
105 unsigned in, unsigned out)
107 av_log(link->dst, AV_LOG_INFO, "auto-inserting filter '%s' "
108 "between the filter '%s' and the filter '%s'\n",
109 filt->name, link->src->name, link->dst->name);
111 link->dst->inputs[link->dstpad] = NULL;
112 if(avfilter_link(filt, out, link->dst, link->dstpad)) {
113 /* failed to link output filter to new filter */
114 link->dst->inputs[link->dstpad] = link;
118 /* re-hookup the link to the new destination filter we inserted */
121 filt->inputs[in] = link;
123 /* if any information on supported colorspaces already exists on the
124 * link, we need to preserve that */
125 if(link->out_formats)
126 avfilter_formats_changeref(&link->out_formats,
127 &filt->outputs[out]->out_formats);
132 int avfilter_config_links(AVFilterContext *filter)
134 int (*config_link)(AVFilterLink *);
137 for(i = 0; i < filter->input_count; i ++) {
138 AVFilterLink *link = filter->inputs[i];
142 switch(link->init_state) {
145 case AVLINK_STARTINIT:
146 av_log(filter, AV_LOG_INFO, "circular filter chain detected\n");
149 link->init_state = AVLINK_STARTINIT;
151 if(avfilter_config_links(link->src))
154 if(!(config_link = link_spad(link).config_props))
155 config_link = avfilter_default_config_output_link;
156 if(config_link(link))
159 if((config_link = link_dpad(link).config_props))
160 if(config_link(link))
163 link->init_state = AVLINK_INIT;
170 static void dprintf_picref(void *ctx, AVFilterPicRef *picref, int end)
173 "picref[%p data[%p, %p, %p, %p] linesize[%d, %d, %d, %d] pts:%"PRId64" s:%dx%d]%s",
175 picref->data [0], picref->data [1], picref->data [2], picref->data [3],
176 picref->linesize[0], picref->linesize[1], picref->linesize[2], picref->linesize[3],
177 picref->pts, picref->w, picref->h,
181 static void dprintf_link(void *ctx, AVFilterLink *link, int end)
184 "link[%p s:%dx%d fmt:%-16s %-16s->%-16s]%s",
185 link, link->w, link->h,
186 avcodec_get_pix_fmt_name(link->format),
187 link->src ? link->src->filter->name : "",
188 link->dst ? link->dst->filter->name : "",
192 #define DPRINTF_START(ctx, func) dprintf(NULL, "%-16s: ", #func)
194 AVFilterPicRef *avfilter_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
196 AVFilterPicRef *ret = NULL;
198 DPRINTF_START(NULL, get_video_buffer); dprintf_link(NULL, link, 0); dprintf(NULL, " perms:%d w:%d h:%d\n", perms, w, h);
200 if(link_dpad(link).get_video_buffer)
201 ret = link_dpad(link).get_video_buffer(link, perms, w, h);
204 ret = avfilter_default_get_video_buffer(link, perms, w, h);
206 DPRINTF_START(NULL, get_video_buffer); dprintf_link(NULL, link, 0); dprintf(NULL, " returning "); dprintf_picref(NULL, ret, 1);
211 int avfilter_request_frame(AVFilterLink *link)
213 DPRINTF_START(NULL, request_frame); dprintf_link(NULL, link, 1);
215 if(link_spad(link).request_frame)
216 return link_spad(link).request_frame(link);
217 else if(link->src->inputs[0])
218 return avfilter_request_frame(link->src->inputs[0]);
222 int avfilter_poll_frame(AVFilterLink *link)
226 if(link_spad(link).poll_frame)
227 return link_spad(link).poll_frame(link);
229 for (i=0; i<link->src->input_count; i++) {
231 if(!link->src->inputs[i])
233 val = avfilter_poll_frame(link->src->inputs[i]);
234 min = FFMIN(min, val);
240 /* XXX: should we do the duplicating of the picture ref here, instead of
241 * forcing the source filter to do it? */
242 void avfilter_start_frame(AVFilterLink *link, AVFilterPicRef *picref)
244 void (*start_frame)(AVFilterLink *, AVFilterPicRef *);
245 AVFilterPad *dst = &link_dpad(link);
247 DPRINTF_START(NULL, start_frame); dprintf_link(NULL, link, 0); dprintf(NULL, " "); dprintf_picref(NULL, picref, 1);
249 if(!(start_frame = dst->start_frame))
250 start_frame = avfilter_default_start_frame;
252 /* prepare to copy the picture if it has insufficient permissions */
253 if((dst->min_perms & picref->perms) != dst->min_perms ||
254 dst->rej_perms & picref->perms) {
256 av_log(link->dst, AV_LOG_INFO,
257 "frame copy needed (have perms %x, need %x, reject %x)\n",
259 link_dpad(link).min_perms, link_dpad(link).rej_perms);
262 link->cur_pic = avfilter_default_get_video_buffer(link, dst->min_perms, link->w, link->h);
263 link->srcpic = picref;
264 link->cur_pic->pts = link->srcpic->pts;
265 link->cur_pic->pixel_aspect = link->srcpic->pixel_aspect;
268 link->cur_pic = picref;
270 start_frame(link, link->cur_pic);
273 void avfilter_end_frame(AVFilterLink *link)
275 void (*end_frame)(AVFilterLink *);
277 if(!(end_frame = link_dpad(link).end_frame))
278 end_frame = avfilter_default_end_frame;
282 /* unreference the source picture if we're feeding the destination filter
283 * a copied version dues to permission issues */
285 avfilter_unref_pic(link->srcpic);
291 void avfilter_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
293 uint8_t *src[4], *dst[4];
294 int i, j, hsub, vsub;
295 void (*draw_slice)(AVFilterLink *, int, int, int);
297 DPRINTF_START(NULL, draw_slice); dprintf_link(NULL, link, 0); dprintf(NULL, " y:%d h:%d dir:%d\n", y, h, slice_dir);
299 /* copy the slice if needed for permission reasons */
301 avcodec_get_chroma_sub_sample(link->format, &hsub, &vsub);
303 for(i = 0; i < 4; i ++) {
304 if(link->srcpic->data[i]) {
305 src[i] = link->srcpic-> data[i] +
306 (y >> (i==0 ? 0 : vsub)) * link->srcpic-> linesize[i];
307 dst[i] = link->cur_pic->data[i] +
308 (y >> (i==0 ? 0 : vsub)) * link->cur_pic->linesize[i];
310 src[i] = dst[i] = NULL;
313 for(i = 0; i < 4; i ++) {
315 ff_get_plane_bytewidth(link->format, link->cur_pic->w, i);
317 if(!src[i]) continue;
319 for(j = 0; j < h >> (i==0 ? 0 : vsub); j ++) {
320 memcpy(dst[i], src[i], planew);
321 src[i] += link->srcpic ->linesize[i];
322 dst[i] += link->cur_pic->linesize[i];
327 if(!(draw_slice = link_dpad(link).draw_slice))
328 draw_slice = avfilter_default_draw_slice;
329 draw_slice(link, y, h, slice_dir);
332 #define MAX_REGISTERED_AVFILTERS_NB 64
334 static AVFilter *registered_avfilters[MAX_REGISTERED_AVFILTERS_NB + 1];
336 static int next_registered_avfilter_idx = 0;
338 AVFilter *avfilter_get_by_name(const char *name)
342 for (i = 0; registered_avfilters[i]; i++)
343 if (!strcmp(registered_avfilters[i]->name, name))
344 return registered_avfilters[i];
349 int avfilter_register(AVFilter *filter)
351 if (next_registered_avfilter_idx == MAX_REGISTERED_AVFILTERS_NB)
354 registered_avfilters[next_registered_avfilter_idx++] = filter;
358 AVFilter **av_filter_next(AVFilter **filter)
360 return filter ? ++filter : ®istered_avfilters[0];
363 void avfilter_uninit(void)
365 memset(registered_avfilters, 0, sizeof(registered_avfilters));
366 next_registered_avfilter_idx = 0;
369 static int pad_count(const AVFilterPad *pads)
373 for(count = 0; pads->name; count ++) pads ++;
377 static const char *filter_name(void *p)
379 AVFilterContext *filter = p;
380 return filter->filter->name;
383 static const AVClass avfilter_class = {
388 AVFilterContext *avfilter_open(AVFilter *filter, const char *inst_name)
390 AVFilterContext *ret;
395 ret = av_mallocz(sizeof(AVFilterContext));
397 ret->av_class = &avfilter_class;
398 ret->filter = filter;
399 ret->name = inst_name ? av_strdup(inst_name) : NULL;
400 ret->priv = av_mallocz(filter->priv_size);
402 ret->input_count = pad_count(filter->inputs);
403 if (ret->input_count) {
404 ret->input_pads = av_malloc(sizeof(AVFilterPad) * ret->input_count);
405 memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad) * ret->input_count);
406 ret->inputs = av_mallocz(sizeof(AVFilterLink*) * ret->input_count);
409 ret->output_count = pad_count(filter->outputs);
410 if (ret->output_count) {
411 ret->output_pads = av_malloc(sizeof(AVFilterPad) * ret->output_count);
412 memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad) * ret->output_count);
413 ret->outputs = av_mallocz(sizeof(AVFilterLink*) * ret->output_count);
419 void avfilter_destroy(AVFilterContext *filter)
423 if(filter->filter->uninit)
424 filter->filter->uninit(filter);
426 for(i = 0; i < filter->input_count; i ++) {
427 if(filter->inputs[i])
428 filter->inputs[i]->src->outputs[filter->inputs[i]->srcpad] = NULL;
429 av_freep(&filter->inputs[i]);
431 for(i = 0; i < filter->output_count; i ++) {
432 if(filter->outputs[i])
433 filter->outputs[i]->dst->inputs[filter->outputs[i]->dstpad] = NULL;
434 av_freep(&filter->outputs[i]);
437 av_freep(&filter->name);
438 av_freep(&filter->input_pads);
439 av_freep(&filter->output_pads);
440 av_freep(&filter->inputs);
441 av_freep(&filter->outputs);
442 av_freep(&filter->priv);
446 int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
450 if(filter->filter->init)
451 ret = filter->filter->init(filter, args, opaque);