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/audioconvert.c"
25 #include "libavutil/pixdesc.h"
26 #include "libavcore/imgutils.h"
30 unsigned avfilter_version(void) {
31 return LIBAVFILTER_VERSION_INT;
34 const char *avfilter_configuration(void)
36 return FFMPEG_CONFIGURATION;
39 const char *avfilter_license(void)
41 #define LICENSE_PREFIX "libavfilter license: "
42 return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
45 /** helper macros to get the in/out pad on the dst/src filter */
46 #define link_dpad(link) link->dst-> input_pads[link->dstpad]
47 #define link_spad(link) link->src->output_pads[link->srcpad]
49 AVFilterBufferRef *avfilter_ref_buffer(AVFilterBufferRef *ref, int pmask)
51 AVFilterBufferRef *ret = av_malloc(sizeof(AVFilterBufferRef));
55 if (ref->type == AVMEDIA_TYPE_VIDEO) {
56 ret->video = av_malloc(sizeof(AVFilterBufferRefVideoProps));
61 *ret->video = *ref->video;
62 } else if (ref->type == AVMEDIA_TYPE_AUDIO) {
63 ret->audio = av_malloc(sizeof(AVFilterBufferRefAudioProps));
68 *ret->audio = *ref->audio;
71 ret->buf->refcount ++;
75 void avfilter_unref_buffer(AVFilterBufferRef *ref)
77 if (!(--ref->buf->refcount))
78 ref->buf->free(ref->buf);
84 void avfilter_insert_pad(unsigned idx, unsigned *count, size_t padidx_off,
85 AVFilterPad **pads, AVFilterLink ***links,
90 idx = FFMIN(idx, *count);
92 *pads = av_realloc(*pads, sizeof(AVFilterPad) * (*count + 1));
93 *links = av_realloc(*links, sizeof(AVFilterLink*) * (*count + 1));
94 memmove(*pads +idx+1, *pads +idx, sizeof(AVFilterPad) * (*count-idx));
95 memmove(*links+idx+1, *links+idx, sizeof(AVFilterLink*) * (*count-idx));
96 memcpy(*pads+idx, newpad, sizeof(AVFilterPad));
100 for (i = idx+1; i < *count; i++)
102 (*(unsigned *)((uint8_t *) *links[i] + padidx_off))++;
105 int avfilter_link(AVFilterContext *src, unsigned srcpad,
106 AVFilterContext *dst, unsigned dstpad)
110 if (src->output_count <= srcpad || dst->input_count <= dstpad ||
111 src->outputs[srcpad] || dst->inputs[dstpad])
114 src->outputs[srcpad] =
115 dst-> inputs[dstpad] = link = av_mallocz(sizeof(AVFilterLink));
119 link->srcpad = srcpad;
120 link->dstpad = dstpad;
121 link->type = src->output_pads[srcpad].type;
122 assert(PIX_FMT_NONE == -1 && SAMPLE_FMT_NONE == -1);
128 int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt,
129 unsigned in, unsigned out)
131 av_log(link->dst, AV_LOG_INFO, "auto-inserting filter '%s' "
132 "between the filter '%s' and the filter '%s'\n",
133 filt->name, link->src->name, link->dst->name);
135 link->dst->inputs[link->dstpad] = NULL;
136 if (avfilter_link(filt, out, link->dst, link->dstpad)) {
137 /* failed to link output filter to new filter */
138 link->dst->inputs[link->dstpad] = link;
142 /* re-hookup the link to the new destination filter we inserted */
145 filt->inputs[in] = link;
147 /* if any information on supported media formats already exists on the
148 * link, we need to preserve that */
149 if (link->out_formats)
150 avfilter_formats_changeref(&link->out_formats,
151 &filt->outputs[out]->out_formats);
156 int avfilter_config_links(AVFilterContext *filter)
158 int (*config_link)(AVFilterLink *);
161 for (i = 0; i < filter->input_count; i ++) {
162 AVFilterLink *link = filter->inputs[i];
166 switch (link->init_state) {
169 case AVLINK_STARTINIT:
170 av_log(filter, AV_LOG_INFO, "circular filter chain detected\n");
173 link->init_state = AVLINK_STARTINIT;
175 if (avfilter_config_links(link->src))
178 if (!(config_link = link_spad(link).config_props))
179 config_link = avfilter_default_config_output_link;
180 if (config_link(link))
183 if ((config_link = link_dpad(link).config_props))
184 if (config_link(link))
187 link->init_state = AVLINK_INIT;
194 char *ff_get_ref_perms_string(char *buf, size_t buf_size, int perms)
196 snprintf(buf, buf_size, "%s%s%s%s%s",
197 perms & AV_PERM_READ ? "r" : "",
198 perms & AV_PERM_WRITE ? "w" : "",
199 perms & AV_PERM_PRESERVE ? "p" : "",
200 perms & AV_PERM_REUSE ? "r" : "",
201 perms & AV_PERM_REUSE2 ? "R" : "");
205 void ff_dprintf_ref(void *ctx, AVFilterBufferRef *ref, int end)
207 av_unused char buf[16];
209 "ref[%p buf:%p refcount:%d perms:%s data:%p linesize[%d, %d, %d, %d] pts:%"PRId64" pos:%"PRId64,
210 ref, ref->buf, ref->buf->refcount, ff_get_ref_perms_string(buf, sizeof(buf), ref->perms), ref->data[0],
211 ref->linesize[0], ref->linesize[1], ref->linesize[2], ref->linesize[3],
215 dprintf(ctx, " a:%d/%d s:%dx%d i:%c",
216 ref->video->pixel_aspect.num, ref->video->pixel_aspect.den,
217 ref->video->w, ref->video->h,
218 !ref->video->interlaced ? 'P' : /* Progressive */
219 ref->video->top_field_first ? 'T' : 'B'); /* Top / Bottom */
222 dprintf(ctx, " cl:%"PRId64"d sn:%d s:%d sr:%d p:%d",
223 ref->audio->channel_layout,
224 ref->audio->samples_nb,
226 ref->audio->sample_rate,
230 dprintf(ctx, "]%s", end ? "\n" : "");
233 void ff_dprintf_link(void *ctx, AVFilterLink *link, int end)
236 "link[%p s:%dx%d fmt:%-16s %-16s->%-16s]%s",
237 link, link->w, link->h,
238 av_pix_fmt_descriptors[link->format].name,
239 link->src ? link->src->filter->name : "",
240 link->dst ? link->dst->filter->name : "",
244 AVFilterBufferRef *avfilter_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
246 AVFilterBufferRef *ret = NULL;
248 av_unused char buf[16];
249 FF_DPRINTF_START(NULL, get_video_buffer); ff_dprintf_link(NULL, link, 0);
250 dprintf(NULL, " perms:%s w:%d h:%d\n", ff_get_ref_perms_string(buf, sizeof(buf), perms), w, h);
252 if (link_dpad(link).get_video_buffer)
253 ret = link_dpad(link).get_video_buffer(link, perms, w, h);
256 ret = avfilter_default_get_video_buffer(link, perms, w, h);
259 ret->type = AVMEDIA_TYPE_VIDEO;
261 FF_DPRINTF_START(NULL, get_video_buffer); ff_dprintf_link(NULL, link, 0); dprintf(NULL, " returning "); ff_dprintf_ref(NULL, ret, 1);
266 AVFilterBufferRef *avfilter_get_audio_buffer(AVFilterLink *link, int perms,
267 enum SampleFormat sample_fmt, int size,
268 int64_t channel_layout, int planar)
270 AVFilterBufferRef *ret = NULL;
272 if (link_dpad(link).get_audio_buffer)
273 ret = link_dpad(link).get_audio_buffer(link, perms, sample_fmt, size, channel_layout, planar);
276 ret = avfilter_default_get_audio_buffer(link, perms, sample_fmt, size, channel_layout, planar);
279 ret->type = AVMEDIA_TYPE_AUDIO;
284 int avfilter_request_frame(AVFilterLink *link)
286 FF_DPRINTF_START(NULL, request_frame); ff_dprintf_link(NULL, link, 1);
288 if (link_spad(link).request_frame)
289 return link_spad(link).request_frame(link);
290 else if (link->src->inputs[0])
291 return avfilter_request_frame(link->src->inputs[0]);
295 int avfilter_poll_frame(AVFilterLink *link)
297 int i, min = INT_MAX;
299 if (link_spad(link).poll_frame)
300 return link_spad(link).poll_frame(link);
302 for (i = 0; i < link->src->input_count; i++) {
304 if (!link->src->inputs[i])
306 val = avfilter_poll_frame(link->src->inputs[i]);
307 min = FFMIN(min, val);
313 /* XXX: should we do the duplicating of the picture ref here, instead of
314 * forcing the source filter to do it? */
315 void avfilter_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
317 void (*start_frame)(AVFilterLink *, AVFilterBufferRef *);
318 AVFilterPad *dst = &link_dpad(link);
320 FF_DPRINTF_START(NULL, start_frame); ff_dprintf_link(NULL, link, 0); dprintf(NULL, " "); ff_dprintf_ref(NULL, picref, 1);
322 if (!(start_frame = dst->start_frame))
323 start_frame = avfilter_default_start_frame;
325 /* prepare to copy the picture if it has insufficient permissions */
326 if ((dst->min_perms & picref->perms) != dst->min_perms ||
327 dst->rej_perms & picref->perms) {
328 av_log(link->dst, AV_LOG_DEBUG,
329 "frame copy needed (have perms %x, need %x, reject %x)\n",
331 link_dpad(link).min_perms, link_dpad(link).rej_perms);
333 link->cur_buf = avfilter_get_video_buffer(link, dst->min_perms, link->w, link->h);
334 link->src_buf = picref;
335 avfilter_copy_buffer_ref_props(link->cur_buf, link->src_buf);
338 link->cur_buf = picref;
340 start_frame(link, link->cur_buf);
343 void avfilter_end_frame(AVFilterLink *link)
345 void (*end_frame)(AVFilterLink *);
347 if (!(end_frame = link_dpad(link).end_frame))
348 end_frame = avfilter_default_end_frame;
352 /* unreference the source picture if we're feeding the destination filter
353 * a copied version dues to permission issues */
355 avfilter_unref_buffer(link->src_buf);
356 link->src_buf = NULL;
360 void avfilter_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
362 uint8_t *src[4], *dst[4];
364 void (*draw_slice)(AVFilterLink *, int, int, int);
366 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);
368 /* copy the slice if needed for permission reasons */
370 vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
372 for (i = 0; i < 4; i++) {
373 if (link->src_buf->data[i]) {
374 src[i] = link->src_buf-> data[i] +
375 (y >> (i==0 ? 0 : vsub)) * link->src_buf-> linesize[i];
376 dst[i] = link->cur_buf->data[i] +
377 (y >> (i==0 ? 0 : vsub)) * link->cur_buf->linesize[i];
379 src[i] = dst[i] = NULL;
382 for (i = 0; i < 4; i++) {
384 av_get_image_linesize(link->format, link->cur_buf->video->w, i);
386 if (!src[i]) continue;
388 for (j = 0; j < h >> (i==0 ? 0 : vsub); j++) {
389 memcpy(dst[i], src[i], planew);
390 src[i] += link->src_buf->linesize[i];
391 dst[i] += link->cur_buf->linesize[i];
396 if (!(draw_slice = link_dpad(link).draw_slice))
397 draw_slice = avfilter_default_draw_slice;
398 draw_slice(link, y, h, slice_dir);
401 void avfilter_filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
403 void (*filter_samples)(AVFilterLink *, AVFilterBufferRef *);
404 AVFilterPad *dst = &link_dpad(link);
406 if (!(filter_samples = dst->filter_samples))
407 filter_samples = avfilter_default_filter_samples;
409 /* prepare to copy the samples if the buffer has insufficient permissions */
410 if ((dst->min_perms & samplesref->perms) != dst->min_perms ||
411 dst->rej_perms & samplesref->perms) {
413 av_log(link->dst, AV_LOG_DEBUG,
414 "Copying audio data in avfilter (have perms %x, need %x, reject %x)\n",
415 samplesref->perms, link_dpad(link).min_perms, link_dpad(link).rej_perms);
417 link->cur_buf = avfilter_default_get_audio_buffer(link, dst->min_perms,
419 samplesref->audio->size,
420 samplesref->audio->channel_layout,
421 samplesref->audio->planar);
422 link->cur_buf->pts = samplesref->pts;
423 link->cur_buf->audio->sample_rate = samplesref->audio->sample_rate;
425 /* Copy actual data into new samples buffer */
426 memcpy(link->cur_buf->data[0], samplesref->data[0], samplesref->audio->size);
428 avfilter_unref_buffer(samplesref);
430 link->cur_buf = samplesref;
432 filter_samples(link, link->cur_buf);
435 #define MAX_REGISTERED_AVFILTERS_NB 64
437 static AVFilter *registered_avfilters[MAX_REGISTERED_AVFILTERS_NB + 1];
439 static int next_registered_avfilter_idx = 0;
441 AVFilter *avfilter_get_by_name(const char *name)
445 for (i = 0; registered_avfilters[i]; i++)
446 if (!strcmp(registered_avfilters[i]->name, name))
447 return registered_avfilters[i];
452 int avfilter_register(AVFilter *filter)
454 if (next_registered_avfilter_idx == MAX_REGISTERED_AVFILTERS_NB)
457 registered_avfilters[next_registered_avfilter_idx++] = filter;
461 AVFilter **av_filter_next(AVFilter **filter)
463 return filter ? ++filter : ®istered_avfilters[0];
466 void avfilter_uninit(void)
468 memset(registered_avfilters, 0, sizeof(registered_avfilters));
469 next_registered_avfilter_idx = 0;
472 static int pad_count(const AVFilterPad *pads)
476 for(count = 0; pads->name; count ++) pads ++;
480 static const char *filter_name(void *p)
482 AVFilterContext *filter = p;
483 return filter->filter->name;
486 static const AVClass avfilter_class = {
490 LIBAVUTIL_VERSION_INT,
493 int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name)
495 AVFilterContext *ret;
499 return AVERROR(EINVAL);
501 ret = av_mallocz(sizeof(AVFilterContext));
503 ret->av_class = &avfilter_class;
504 ret->filter = filter;
505 ret->name = inst_name ? av_strdup(inst_name) : NULL;
506 ret->priv = av_mallocz(filter->priv_size);
508 ret->input_count = pad_count(filter->inputs);
509 if (ret->input_count) {
510 ret->input_pads = av_malloc(sizeof(AVFilterPad) * ret->input_count);
511 memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad) * ret->input_count);
512 ret->inputs = av_mallocz(sizeof(AVFilterLink*) * ret->input_count);
515 ret->output_count = pad_count(filter->outputs);
516 if (ret->output_count) {
517 ret->output_pads = av_malloc(sizeof(AVFilterPad) * ret->output_count);
518 memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad) * ret->output_count);
519 ret->outputs = av_mallocz(sizeof(AVFilterLink*) * ret->output_count);
526 void avfilter_destroy(AVFilterContext *filter)
530 if (filter->filter->uninit)
531 filter->filter->uninit(filter);
533 for (i = 0; i < filter->input_count; i++) {
534 if (filter->inputs[i]) {
535 if (filter->inputs[i]->src)
536 filter->inputs[i]->src->outputs[filter->inputs[i]->srcpad] = NULL;
537 avfilter_formats_unref(&filter->inputs[i]->in_formats);
538 avfilter_formats_unref(&filter->inputs[i]->out_formats);
540 av_freep(&filter->inputs[i]);
542 for (i = 0; i < filter->output_count; i++) {
543 if (filter->outputs[i]) {
544 if (filter->outputs[i]->dst)
545 filter->outputs[i]->dst->inputs[filter->outputs[i]->dstpad] = NULL;
546 avfilter_formats_unref(&filter->outputs[i]->in_formats);
547 avfilter_formats_unref(&filter->outputs[i]->out_formats);
549 av_freep(&filter->outputs[i]);
552 av_freep(&filter->name);
553 av_freep(&filter->input_pads);
554 av_freep(&filter->output_pads);
555 av_freep(&filter->inputs);
556 av_freep(&filter->outputs);
557 av_freep(&filter->priv);
561 int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
565 if (filter->filter->init)
566 ret = filter->filter->init(filter, args, opaque);