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"
25 #include "libavutil/pixdesc.h"
29 unsigned avfilter_version(void) {
30 return LIBAVFILTER_VERSION_INT;
33 const char *avfilter_configuration(void)
35 return FFMPEG_CONFIGURATION;
38 const char *avfilter_license(void)
40 #define LICENSE_PREFIX "libavfilter license: "
41 return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
44 /** helper macros to get the in/out pad on the dst/src filter */
45 #define link_dpad(link) link->dst-> input_pads[link->dstpad]
46 #define link_spad(link) link->src->output_pads[link->srcpad]
48 AVFilterBufferRef *avfilter_ref_buffer(AVFilterBufferRef *ref, int pmask)
50 AVFilterBufferRef *ret = av_malloc(sizeof(AVFilterBufferRef));
53 ret->buf->refcount ++;
57 void avfilter_unref_buffer(AVFilterBufferRef *ref)
59 if(!(--ref->buf->refcount))
60 ref->buf->free(ref->buf);
64 void avfilter_insert_pad(unsigned idx, unsigned *count, size_t padidx_off,
65 AVFilterPad **pads, AVFilterLink ***links,
70 idx = FFMIN(idx, *count);
72 *pads = av_realloc(*pads, sizeof(AVFilterPad) * (*count + 1));
73 *links = av_realloc(*links, sizeof(AVFilterLink*) * (*count + 1));
74 memmove(*pads +idx+1, *pads +idx, sizeof(AVFilterPad) * (*count-idx));
75 memmove(*links+idx+1, *links+idx, sizeof(AVFilterLink*) * (*count-idx));
76 memcpy(*pads+idx, newpad, sizeof(AVFilterPad));
80 for(i = idx+1; i < *count; i ++)
82 (*(unsigned *)((uint8_t *) *links[i] + padidx_off)) ++;
85 int avfilter_link(AVFilterContext *src, unsigned srcpad,
86 AVFilterContext *dst, unsigned dstpad)
90 if(src->output_count <= srcpad || dst->input_count <= dstpad ||
91 src->outputs[srcpad] || dst->inputs[dstpad])
94 src->outputs[srcpad] =
95 dst-> inputs[dstpad] = link = av_mallocz(sizeof(AVFilterLink));
99 link->srcpad = srcpad;
100 link->dstpad = dstpad;
101 link->type = src->output_pads[srcpad].type;
102 assert(PIX_FMT_NONE == -1 && SAMPLE_FMT_NONE == -1);
108 int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt,
109 unsigned in, unsigned out)
111 av_log(link->dst, AV_LOG_INFO, "auto-inserting filter '%s' "
112 "between the filter '%s' and the filter '%s'\n",
113 filt->name, link->src->name, link->dst->name);
115 link->dst->inputs[link->dstpad] = NULL;
116 if(avfilter_link(filt, out, link->dst, link->dstpad)) {
117 /* failed to link output filter to new filter */
118 link->dst->inputs[link->dstpad] = link;
122 /* re-hookup the link to the new destination filter we inserted */
125 filt->inputs[in] = link;
127 /* if any information on supported media formats already exists on the
128 * link, we need to preserve that */
129 if(link->out_formats)
130 avfilter_formats_changeref(&link->out_formats,
131 &filt->outputs[out]->out_formats);
136 int avfilter_config_links(AVFilterContext *filter)
138 int (*config_link)(AVFilterLink *);
141 for(i = 0; i < filter->input_count; i ++) {
142 AVFilterLink *link = filter->inputs[i];
146 switch(link->init_state) {
149 case AVLINK_STARTINIT:
150 av_log(filter, AV_LOG_INFO, "circular filter chain detected\n");
153 link->init_state = AVLINK_STARTINIT;
155 if(avfilter_config_links(link->src))
158 if(!(config_link = link_spad(link).config_props))
159 config_link = avfilter_default_config_output_link;
160 if(config_link(link))
163 if((config_link = link_dpad(link).config_props))
164 if(config_link(link))
167 link->init_state = AVLINK_INIT;
174 void ff_dprintf_picref(void *ctx, AVFilterBufferRef *picref, int end)
177 "picref[%p data[%p, %p, %p, %p] linesize[%d, %d, %d, %d] pts:%"PRId64" pos:%"PRId64" a:%d/%d s:%dx%d]%s",
179 picref->data [0], picref->data [1], picref->data [2], picref->data [3],
180 picref->linesize[0], picref->linesize[1], picref->linesize[2], picref->linesize[3],
181 picref->pts, picref->pos,
182 picref->pixel_aspect.num, picref->pixel_aspect.den, picref->w, picref->h,
186 void ff_dprintf_link(void *ctx, AVFilterLink *link, int end)
189 "link[%p s:%dx%d fmt:%-16s %-16s->%-16s]%s",
190 link, link->w, link->h,
191 av_pix_fmt_descriptors[link->format].name,
192 link->src ? link->src->filter->name : "",
193 link->dst ? link->dst->filter->name : "",
197 AVFilterBufferRef *avfilter_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
199 AVFilterBufferRef *ret = NULL;
201 FF_DPRINTF_START(NULL, get_video_buffer); ff_dprintf_link(NULL, link, 0); dprintf(NULL, " perms:%d w:%d h:%d\n", perms, w, h);
203 if(link_dpad(link).get_video_buffer)
204 ret = link_dpad(link).get_video_buffer(link, perms, w, h);
207 ret = avfilter_default_get_video_buffer(link, perms, w, h);
209 FF_DPRINTF_START(NULL, get_video_buffer); ff_dprintf_link(NULL, link, 0); dprintf(NULL, " returning "); ff_dprintf_picref(NULL, ret, 1);
214 int avfilter_request_frame(AVFilterLink *link)
216 FF_DPRINTF_START(NULL, request_frame); ff_dprintf_link(NULL, link, 1);
218 if(link_spad(link).request_frame)
219 return link_spad(link).request_frame(link);
220 else if(link->src->inputs[0])
221 return avfilter_request_frame(link->src->inputs[0]);
225 int avfilter_poll_frame(AVFilterLink *link)
229 if(link_spad(link).poll_frame)
230 return link_spad(link).poll_frame(link);
232 for (i=0; i<link->src->input_count; i++) {
234 if(!link->src->inputs[i])
236 val = avfilter_poll_frame(link->src->inputs[i]);
237 min = FFMIN(min, val);
243 /* XXX: should we do the duplicating of the picture ref here, instead of
244 * forcing the source filter to do it? */
245 void avfilter_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
247 void (*start_frame)(AVFilterLink *, AVFilterBufferRef *);
248 AVFilterPad *dst = &link_dpad(link);
250 FF_DPRINTF_START(NULL, start_frame); ff_dprintf_link(NULL, link, 0); dprintf(NULL, " "); ff_dprintf_picref(NULL, picref, 1);
252 if(!(start_frame = dst->start_frame))
253 start_frame = avfilter_default_start_frame;
255 /* prepare to copy the picture if it has insufficient permissions */
256 if((dst->min_perms & picref->perms) != dst->min_perms ||
257 dst->rej_perms & picref->perms) {
259 av_log(link->dst, AV_LOG_INFO,
260 "frame copy needed (have perms %x, need %x, reject %x)\n",
262 link_dpad(link).min_perms, link_dpad(link).rej_perms);
265 link->cur_pic = avfilter_default_get_video_buffer(link, dst->min_perms, link->w, link->h);
266 link->srcpic = picref;
267 avfilter_copy_buffer_ref_props(link->cur_pic, link->srcpic);
270 link->cur_pic = picref;
272 start_frame(link, link->cur_pic);
275 void avfilter_end_frame(AVFilterLink *link)
277 void (*end_frame)(AVFilterLink *);
279 if(!(end_frame = link_dpad(link).end_frame))
280 end_frame = avfilter_default_end_frame;
284 /* unreference the source picture if we're feeding the destination filter
285 * a copied version dues to permission issues */
287 avfilter_unref_buffer(link->srcpic);
293 void avfilter_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
295 uint8_t *src[4], *dst[4];
297 void (*draw_slice)(AVFilterLink *, int, int, int);
299 FF_DPRINTF_START(NULL, draw_slice); ff_dprintf_link(NULL, link, 0); dprintf(NULL, " y:%d h:%d dir:%d\n", y, h, slice_dir);
301 /* copy the slice if needed for permission reasons */
303 vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
305 for(i = 0; i < 4; i ++) {
306 if(link->srcpic->data[i]) {
307 src[i] = link->srcpic-> data[i] +
308 (y >> (i==0 ? 0 : vsub)) * link->srcpic-> linesize[i];
309 dst[i] = link->cur_pic->data[i] +
310 (y >> (i==0 ? 0 : vsub)) * link->cur_pic->linesize[i];
312 src[i] = dst[i] = NULL;
315 for(i = 0; i < 4; i ++) {
317 ff_get_plane_bytewidth(link->format, link->cur_pic->w, i);
319 if(!src[i]) continue;
321 for(j = 0; j < h >> (i==0 ? 0 : vsub); j ++) {
322 memcpy(dst[i], src[i], planew);
323 src[i] += link->srcpic ->linesize[i];
324 dst[i] += link->cur_pic->linesize[i];
329 if(!(draw_slice = link_dpad(link).draw_slice))
330 draw_slice = avfilter_default_draw_slice;
331 draw_slice(link, y, h, slice_dir);
334 #define MAX_REGISTERED_AVFILTERS_NB 64
336 static AVFilter *registered_avfilters[MAX_REGISTERED_AVFILTERS_NB + 1];
338 static int next_registered_avfilter_idx = 0;
340 AVFilter *avfilter_get_by_name(const char *name)
344 for (i = 0; registered_avfilters[i]; i++)
345 if (!strcmp(registered_avfilters[i]->name, name))
346 return registered_avfilters[i];
351 int avfilter_register(AVFilter *filter)
353 if (next_registered_avfilter_idx == MAX_REGISTERED_AVFILTERS_NB)
356 registered_avfilters[next_registered_avfilter_idx++] = filter;
360 AVFilter **av_filter_next(AVFilter **filter)
362 return filter ? ++filter : ®istered_avfilters[0];
365 void avfilter_uninit(void)
367 memset(registered_avfilters, 0, sizeof(registered_avfilters));
368 next_registered_avfilter_idx = 0;
371 static int pad_count(const AVFilterPad *pads)
375 for(count = 0; pads->name; count ++) pads ++;
379 static const char *filter_name(void *p)
381 AVFilterContext *filter = p;
382 return filter->filter->name;
385 static const AVClass avfilter_class = {
389 LIBAVUTIL_VERSION_INT,
392 AVFilterContext *avfilter_open(AVFilter *filter, const char *inst_name)
394 AVFilterContext *ret;
399 ret = av_mallocz(sizeof(AVFilterContext));
401 ret->av_class = &avfilter_class;
402 ret->filter = filter;
403 ret->name = inst_name ? av_strdup(inst_name) : NULL;
404 ret->priv = av_mallocz(filter->priv_size);
406 ret->input_count = pad_count(filter->inputs);
407 if (ret->input_count) {
408 ret->input_pads = av_malloc(sizeof(AVFilterPad) * ret->input_count);
409 memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad) * ret->input_count);
410 ret->inputs = av_mallocz(sizeof(AVFilterLink*) * ret->input_count);
413 ret->output_count = pad_count(filter->outputs);
414 if (ret->output_count) {
415 ret->output_pads = av_malloc(sizeof(AVFilterPad) * ret->output_count);
416 memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad) * ret->output_count);
417 ret->outputs = av_mallocz(sizeof(AVFilterLink*) * ret->output_count);
423 void avfilter_destroy(AVFilterContext *filter)
427 if(filter->filter->uninit)
428 filter->filter->uninit(filter);
430 for(i = 0; i < filter->input_count; i ++) {
431 if(filter->inputs[i]) {
432 if (filter->inputs[i]->src)
433 filter->inputs[i]->src->outputs[filter->inputs[i]->srcpad] = NULL;
434 avfilter_formats_unref(&filter->inputs[i]->in_formats);
435 avfilter_formats_unref(&filter->inputs[i]->out_formats);
437 av_freep(&filter->inputs[i]);
439 for(i = 0; i < filter->output_count; i ++) {
440 if(filter->outputs[i]) {
441 if (filter->outputs[i]->dst)
442 filter->outputs[i]->dst->inputs[filter->outputs[i]->dstpad] = NULL;
443 avfilter_formats_unref(&filter->outputs[i]->in_formats);
444 avfilter_formats_unref(&filter->outputs[i]->out_formats);
446 av_freep(&filter->outputs[i]);
449 av_freep(&filter->name);
450 av_freep(&filter->input_pads);
451 av_freep(&filter->output_pads);
452 av_freep(&filter->inputs);
453 av_freep(&filter->outputs);
454 av_freep(&filter->priv);
458 int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
462 if(filter->filter->init)
463 ret = filter->filter->init(filter, args, opaque);