3 * Copyright (c) 2007 Bobby Bingham
5 * This file is part of Libav.
7 * Libav 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 * Libav 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 Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "libavutil/avstring.h"
25 #include "libavutil/channel_layout.h"
26 #include "libavutil/common.h"
27 #include "libavutil/imgutils.h"
28 #include "libavutil/opt.h"
29 #include "libavutil/pixdesc.h"
30 #include "libavutil/rational.h"
31 #include "libavutil/samplefmt.h"
39 unsigned avfilter_version(void) {
40 return LIBAVFILTER_VERSION_INT;
43 const char *avfilter_configuration(void)
45 return LIBAV_CONFIGURATION;
48 const char *avfilter_license(void)
50 #define LICENSE_PREFIX "libavfilter license: "
51 return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
54 void ff_insert_pad(unsigned idx, unsigned *count, size_t padidx_off,
55 AVFilterPad **pads, AVFilterLink ***links,
60 idx = FFMIN(idx, *count);
62 *pads = av_realloc(*pads, sizeof(AVFilterPad) * (*count + 1));
63 *links = av_realloc(*links, sizeof(AVFilterLink*) * (*count + 1));
64 memmove(*pads +idx+1, *pads +idx, sizeof(AVFilterPad) * (*count-idx));
65 memmove(*links+idx+1, *links+idx, sizeof(AVFilterLink*) * (*count-idx));
66 memcpy(*pads+idx, newpad, sizeof(AVFilterPad));
70 for (i = idx+1; i < *count; i++)
72 (*(unsigned *)((uint8_t *) *links[i] + padidx_off))++;
75 int avfilter_link(AVFilterContext *src, unsigned srcpad,
76 AVFilterContext *dst, unsigned dstpad)
80 if (src->nb_outputs <= srcpad || dst->nb_inputs <= dstpad ||
81 src->outputs[srcpad] || dst->inputs[dstpad])
84 if (src->output_pads[srcpad].type != dst->input_pads[dstpad].type) {
85 av_log(src, AV_LOG_ERROR,
86 "Media type mismatch between the '%s' filter output pad %d and the '%s' filter input pad %d\n",
87 src->name, srcpad, dst->name, dstpad);
88 return AVERROR(EINVAL);
91 src->outputs[srcpad] =
92 dst-> inputs[dstpad] = link = av_mallocz(sizeof(AVFilterLink));
96 link->srcpad = &src->output_pads[srcpad];
97 link->dstpad = &dst->input_pads[dstpad];
98 link->type = src->output_pads[srcpad].type;
99 assert(AV_PIX_FMT_NONE == -1 && AV_SAMPLE_FMT_NONE == -1);
105 int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt,
106 unsigned filt_srcpad_idx, unsigned filt_dstpad_idx)
109 unsigned dstpad_idx = link->dstpad - link->dst->input_pads;
111 av_log(link->dst, AV_LOG_VERBOSE, "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[dstpad_idx] = NULL;
116 if ((ret = avfilter_link(filt, filt_dstpad_idx, link->dst, dstpad_idx)) < 0) {
117 /* failed to link output filter to new filter */
118 link->dst->inputs[dstpad_idx] = link;
122 /* re-hookup the link to the new destination filter we inserted */
124 link->dstpad = &filt->input_pads[filt_srcpad_idx];
125 filt->inputs[filt_srcpad_idx] = 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 ff_formats_changeref(&link->out_formats,
131 &filt->outputs[filt_dstpad_idx]->out_formats);
132 if (link->out_samplerates)
133 ff_formats_changeref(&link->out_samplerates,
134 &filt->outputs[filt_dstpad_idx]->out_samplerates);
135 if (link->out_channel_layouts)
136 ff_channel_layouts_changeref(&link->out_channel_layouts,
137 &filt->outputs[filt_dstpad_idx]->out_channel_layouts);
142 int avfilter_config_links(AVFilterContext *filter)
144 int (*config_link)(AVFilterLink *);
148 for (i = 0; i < filter->nb_inputs; i ++) {
149 AVFilterLink *link = filter->inputs[i];
153 switch (link->init_state) {
156 case AVLINK_STARTINIT:
157 av_log(filter, AV_LOG_INFO, "circular filter chain detected\n");
160 link->init_state = AVLINK_STARTINIT;
162 if ((ret = avfilter_config_links(link->src)) < 0)
165 if (!(config_link = link->srcpad->config_props)) {
166 if (link->src->nb_inputs != 1) {
167 av_log(link->src, AV_LOG_ERROR, "Source filters and filters "
168 "with more than one input "
169 "must set config_props() "
170 "callbacks on all outputs\n");
171 return AVERROR(EINVAL);
173 } else if ((ret = config_link(link)) < 0) {
174 av_log(link->src, AV_LOG_ERROR,
175 "Failed to configure output pad on %s\n",
180 if (link->time_base.num == 0 && link->time_base.den == 0)
181 link->time_base = link->src && link->src->nb_inputs ?
182 link->src->inputs[0]->time_base : AV_TIME_BASE_Q;
184 if (link->type == AVMEDIA_TYPE_VIDEO) {
185 if (!link->sample_aspect_ratio.num && !link->sample_aspect_ratio.den)
186 link->sample_aspect_ratio = link->src->nb_inputs ?
187 link->src->inputs[0]->sample_aspect_ratio : (AVRational){1,1};
189 if (link->src->nb_inputs) {
191 link->w = link->src->inputs[0]->w;
193 link->h = link->src->inputs[0]->h;
194 } else if (!link->w || !link->h) {
195 av_log(link->src, AV_LOG_ERROR,
196 "Video source filters must set their output link's "
197 "width and height\n");
198 return AVERROR(EINVAL);
202 if ((config_link = link->dstpad->config_props))
203 if ((ret = config_link(link)) < 0) {
204 av_log(link->src, AV_LOG_ERROR,
205 "Failed to configure input pad on %s\n",
210 link->init_state = AVLINK_INIT;
217 void ff_dlog_link(void *ctx, AVFilterLink *link, int end)
219 if (link->type == AVMEDIA_TYPE_VIDEO) {
221 "link[%p s:%dx%d fmt:%-16s %-16s->%-16s]%s",
222 link, link->w, link->h,
223 av_get_pix_fmt_name(link->format),
224 link->src ? link->src->filter->name : "",
225 link->dst ? link->dst->filter->name : "",
229 av_get_channel_layout_string(buf, sizeof(buf), -1, link->channel_layout);
232 "link[%p r:%d cl:%s fmt:%-16s %-16s->%-16s]%s",
233 link, link->sample_rate, buf,
234 av_get_sample_fmt_name(link->format),
235 link->src ? link->src->filter->name : "",
236 link->dst ? link->dst->filter->name : "",
241 int ff_request_frame(AVFilterLink *link)
243 FF_DPRINTF_START(NULL, request_frame); ff_dlog_link(NULL, link, 1);
245 if (link->srcpad->request_frame)
246 return link->srcpad->request_frame(link);
247 else if (link->src->inputs[0])
248 return ff_request_frame(link->src->inputs[0]);
252 int ff_poll_frame(AVFilterLink *link)
254 int i, min = INT_MAX;
256 if (link->srcpad->poll_frame)
257 return link->srcpad->poll_frame(link);
259 for (i = 0; i < link->src->nb_inputs; i++) {
261 if (!link->src->inputs[i])
263 val = ff_poll_frame(link->src->inputs[i]);
264 min = FFMIN(min, val);
270 #define MAX_REGISTERED_AVFILTERS_NB 64
272 static AVFilter *registered_avfilters[MAX_REGISTERED_AVFILTERS_NB + 1];
274 static int next_registered_avfilter_idx = 0;
276 AVFilter *avfilter_get_by_name(const char *name)
280 for (i = 0; registered_avfilters[i]; i++)
281 if (!strcmp(registered_avfilters[i]->name, name))
282 return registered_avfilters[i];
287 int avfilter_register(AVFilter *filter)
289 if (next_registered_avfilter_idx == MAX_REGISTERED_AVFILTERS_NB)
292 registered_avfilters[next_registered_avfilter_idx++] = filter;
296 AVFilter **av_filter_next(AVFilter **filter)
298 return filter ? ++filter : ®istered_avfilters[0];
301 void avfilter_uninit(void)
303 memset(registered_avfilters, 0, sizeof(registered_avfilters));
304 next_registered_avfilter_idx = 0;
307 static int pad_count(const AVFilterPad *pads)
314 for(count = 0; pads->name; count ++) pads ++;
318 static const char *filter_name(void *p)
320 AVFilterContext *filter = p;
321 return filter->filter->name;
324 static void *filter_child_next(void *obj, void *prev)
326 AVFilterContext *ctx = obj;
327 if (!prev && ctx->filter && ctx->filter->priv_class && ctx->priv)
332 static const AVClass *filter_child_class_next(const AVClass *prev)
336 while (prev && *(f = av_filter_next(f)))
337 if ((*f)->priv_class == prev)
340 while (*(f = av_filter_next(f)))
341 if ((*f)->priv_class)
342 return (*f)->priv_class;
347 static const AVClass avfilter_class = {
348 .class_name = "AVFilter",
349 .item_name = filter_name,
350 .version = LIBAVUTIL_VERSION_INT,
351 .child_next = filter_child_next,
352 .child_class_next = filter_child_class_next,
355 AVFilterContext *ff_filter_alloc(const AVFilter *filter, const char *inst_name)
357 AVFilterContext *ret;
362 ret = av_mallocz(sizeof(AVFilterContext));
366 ret->av_class = &avfilter_class;
367 ret->filter = filter;
368 ret->name = inst_name ? av_strdup(inst_name) : NULL;
369 if (filter->priv_size) {
370 ret->priv = av_mallocz(filter->priv_size);
375 if (filter->priv_class) {
376 *(const AVClass**)ret->priv = filter->priv_class;
377 av_opt_set_defaults(ret->priv);
380 ret->nb_inputs = pad_count(filter->inputs);
381 if (ret->nb_inputs ) {
382 ret->input_pads = av_malloc(sizeof(AVFilterPad) * ret->nb_inputs);
383 if (!ret->input_pads)
385 memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad) * ret->nb_inputs);
386 ret->inputs = av_mallocz(sizeof(AVFilterLink*) * ret->nb_inputs);
391 ret->nb_outputs = pad_count(filter->outputs);
392 if (ret->nb_outputs) {
393 ret->output_pads = av_malloc(sizeof(AVFilterPad) * ret->nb_outputs);
394 if (!ret->output_pads)
396 memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad) * ret->nb_outputs);
397 ret->outputs = av_mallocz(sizeof(AVFilterLink*) * ret->nb_outputs);
402 ret->output_count = ret->nb_outputs;
403 ret->input_count = ret->nb_inputs;
409 av_freep(&ret->inputs);
410 av_freep(&ret->input_pads);
412 av_freep(&ret->outputs);
413 av_freep(&ret->output_pads);
415 av_freep(&ret->priv);
420 #if FF_API_AVFILTER_OPEN
421 int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name)
423 *filter_ctx = ff_filter_alloc(filter, inst_name);
424 return *filter_ctx ? 0 : AVERROR(ENOMEM);
428 void avfilter_free(AVFilterContext *filter)
434 ff_filter_graph_remove_filter(filter->graph, filter);
436 if (filter->filter->uninit)
437 filter->filter->uninit(filter);
439 for (i = 0; i < filter->nb_inputs; i++) {
440 if ((link = filter->inputs[i])) {
442 link->src->outputs[link->srcpad - link->src->output_pads] = NULL;
443 ff_formats_unref(&link->in_formats);
444 ff_formats_unref(&link->out_formats);
445 ff_formats_unref(&link->in_samplerates);
446 ff_formats_unref(&link->out_samplerates);
447 ff_channel_layouts_unref(&link->in_channel_layouts);
448 ff_channel_layouts_unref(&link->out_channel_layouts);
452 for (i = 0; i < filter->nb_outputs; i++) {
453 if ((link = filter->outputs[i])) {
455 link->dst->inputs[link->dstpad - link->dst->input_pads] = NULL;
456 ff_formats_unref(&link->in_formats);
457 ff_formats_unref(&link->out_formats);
458 ff_formats_unref(&link->in_samplerates);
459 ff_formats_unref(&link->out_samplerates);
460 ff_channel_layouts_unref(&link->in_channel_layouts);
461 ff_channel_layouts_unref(&link->out_channel_layouts);
466 if (filter->filter->priv_class)
467 av_opt_free(filter->priv);
469 av_freep(&filter->name);
470 av_freep(&filter->input_pads);
471 av_freep(&filter->output_pads);
472 av_freep(&filter->inputs);
473 av_freep(&filter->outputs);
474 av_freep(&filter->priv);
478 /* process a list of value1:value2:..., each value corresponding
479 * to subsequent AVOption, in the order they are declared */
480 static int process_unnamed_options(AVFilterContext *ctx, AVDictionary **options,
483 const AVOption *o = NULL;
484 const char *p = args;
488 o = av_opt_next(ctx->priv, o);
490 av_log(ctx, AV_LOG_ERROR, "More options provided than "
491 "this filter supports.\n");
492 return AVERROR(EINVAL);
494 if (o->type == AV_OPT_TYPE_CONST)
497 val = av_get_token(&p, ":");
499 return AVERROR(ENOMEM);
501 av_dict_set(options, o->name, val, 0);
511 int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
513 AVDictionary *options = NULL;
514 AVDictionaryEntry *e;
518 if (!filter->filter->priv_class) {
519 av_log(filter, AV_LOG_ERROR, "This filter does not take any "
520 "options, but options were provided: %s.\n", args);
521 return AVERROR(EINVAL);
524 #if FF_API_OLD_FILTER_OPTS
525 if (!strcmp(filter->filter->name, "scale") &&
526 strchr(args, ':') < strchr(args, '=')) {
527 /* old w:h:flags=<flags> syntax */
528 char *copy = av_strdup(args);
531 av_log(filter, AV_LOG_WARNING, "The <w>:<h>:flags=<flags> option "
532 "syntax is deprecated. Use either <w>:<h>:<flags> or "
533 "w=<w>:h=<h>:flags=<flags>.\n");
536 ret = AVERROR(ENOMEM);
540 p = strrchr(copy, ':');
543 ret = av_dict_parse_string(&options, p, "=", ":", 0);
546 ret = process_unnamed_options(filter, &options, copy);
554 if (strchr(args, '=')) {
555 /* assume a list of key1=value1:key2=value2:... */
556 ret = av_dict_parse_string(&options, args, "=", ":", 0);
559 #if FF_API_OLD_FILTER_OPTS
560 } else if (!strcmp(filter->filter->name, "format") ||
561 !strcmp(filter->filter->name, "noformat") ||
562 !strcmp(filter->filter->name, "frei0r") ||
563 !strcmp(filter->filter->name, "frei0r_src") ||
564 !strcmp(filter->filter->name, "ocv")) {
565 /* a hack for compatibility with the old syntax
566 * replace colons with |s */
567 char *copy = av_strdup(args);
569 int nb_leading = 0; // number of leading colons to skip
572 ret = AVERROR(ENOMEM);
576 if (!strcmp(filter->filter->name, "frei0r") ||
577 !strcmp(filter->filter->name, "ocv"))
579 else if (!strcmp(filter->filter->name, "frei0r_src"))
582 while (nb_leading--) {
585 p = copy + strlen(copy);
591 if (strchr(p, ':')) {
592 av_log(filter, AV_LOG_WARNING, "This syntax is deprecated. Use "
593 "'|' to separate the list items.\n");
596 while ((p = strchr(p, ':')))
599 ret = process_unnamed_options(filter, &options, copy);
606 ret = process_unnamed_options(filter, &options, args);
612 if (filter->filter->priv_class) {
613 ret = av_opt_set_dict(filter->priv, &options);
615 av_log(filter, AV_LOG_ERROR, "Error applying options to the filter.\n");
620 if (filter->filter->init)
621 ret = filter->filter->init(filter);
622 else if (filter->filter->init_dict)
623 ret = filter->filter->init_dict(filter, &options);
627 if ((e = av_dict_get(options, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
628 av_log(filter, AV_LOG_ERROR, "No such option: %s.\n", e->key);
629 ret = AVERROR_OPTION_NOT_FOUND;
634 av_dict_free(&options);
639 const char *avfilter_pad_get_name(const AVFilterPad *pads, int pad_idx)
641 return pads[pad_idx].name;
644 enum AVMediaType avfilter_pad_get_type(const AVFilterPad *pads, int pad_idx)
646 return pads[pad_idx].type;
649 static int default_filter_frame(AVFilterLink *link, AVFrame *frame)
651 return ff_filter_frame(link->dst->outputs[0], frame);
654 int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
656 int (*filter_frame)(AVFilterLink *, AVFrame *);
657 AVFilterPad *dst = link->dstpad;
660 FF_DPRINTF_START(NULL, filter_frame);
661 ff_dlog_link(NULL, link, 1);
663 if (!(filter_frame = dst->filter_frame))
664 filter_frame = default_filter_frame;
666 /* copy the frame if needed */
667 if (dst->needs_writable && !av_frame_is_writable(frame)) {
668 av_log(link->dst, AV_LOG_DEBUG, "Copying data in avfilter.\n");
670 switch (link->type) {
671 case AVMEDIA_TYPE_VIDEO:
672 out = ff_get_video_buffer(link, link->w, link->h);
674 case AVMEDIA_TYPE_AUDIO:
675 out = ff_get_audio_buffer(link, frame->nb_samples);
677 default: return AVERROR(EINVAL);
680 av_frame_free(&frame);
681 return AVERROR(ENOMEM);
683 av_frame_copy_props(out, frame);
685 switch (link->type) {
686 case AVMEDIA_TYPE_VIDEO:
687 av_image_copy(out->data, out->linesize, frame->data, frame->linesize,
688 frame->format, frame->width, frame->height);
690 case AVMEDIA_TYPE_AUDIO:
691 av_samples_copy(out->extended_data, frame->extended_data,
692 0, 0, frame->nb_samples,
693 av_get_channel_layout_nb_channels(frame->channel_layout),
696 default: return AVERROR(EINVAL);
699 av_frame_free(&frame);
703 return filter_frame(link, out);
706 const AVClass *avfilter_get_class(void)
708 return &avfilter_class;