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
22 #include "libavutil/avstring.h"
23 #include "libavutil/channel_layout.h"
24 #include "libavutil/common.h"
25 #include "libavutil/imgutils.h"
26 #include "libavutil/opt.h"
27 #include "libavutil/pixdesc.h"
28 #include "libavutil/rational.h"
29 #include "libavutil/samplefmt.h"
37 unsigned avfilter_version(void)
39 return LIBAVFILTER_VERSION_INT;
42 const char *avfilter_configuration(void)
44 return LIBAV_CONFIGURATION;
47 const char *avfilter_license(void)
49 #define LICENSE_PREFIX "libavfilter license: "
50 return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
53 void ff_insert_pad(unsigned idx, unsigned *count, size_t padidx_off,
54 AVFilterPad **pads, AVFilterLink ***links,
59 idx = FFMIN(idx, *count);
61 *pads = av_realloc(*pads, sizeof(AVFilterPad) * (*count + 1));
62 *links = av_realloc(*links, sizeof(AVFilterLink*) * (*count + 1));
63 memmove(*pads + idx + 1, *pads + idx, sizeof(AVFilterPad) * (*count - idx));
64 memmove(*links + idx + 1, *links + idx, sizeof(AVFilterLink*) * (*count - idx));
65 memcpy(*pads + idx, newpad, sizeof(AVFilterPad));
69 for (i = idx + 1; i < *count; i++)
71 (*(unsigned *)((uint8_t *) *links[i] + padidx_off))++;
74 int avfilter_link(AVFilterContext *src, unsigned srcpad,
75 AVFilterContext *dst, unsigned dstpad)
79 if (src->nb_outputs <= srcpad || dst->nb_inputs <= dstpad ||
80 src->outputs[srcpad] || dst->inputs[dstpad])
83 if (src->output_pads[srcpad].type != dst->input_pads[dstpad].type) {
84 av_log(src, AV_LOG_ERROR,
85 "Media type mismatch between the '%s' filter output pad %d and the '%s' filter input pad %d\n",
86 src->name, srcpad, dst->name, dstpad);
87 return AVERROR(EINVAL);
90 src->outputs[srcpad] =
91 dst-> inputs[dstpad] = link = av_mallocz(sizeof(AVFilterLink));
95 link->srcpad = &src->output_pads[srcpad];
96 link->dstpad = &dst->input_pads[dstpad];
97 link->type = src->output_pads[srcpad].type;
98 assert(AV_PIX_FMT_NONE == -1 && AV_SAMPLE_FMT_NONE == -1);
104 int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt,
105 unsigned filt_srcpad_idx, unsigned filt_dstpad_idx)
108 unsigned dstpad_idx = link->dstpad - link->dst->input_pads;
110 av_log(link->dst, AV_LOG_VERBOSE, "auto-inserting filter '%s' "
111 "between the filter '%s' and the filter '%s'\n",
112 filt->name, link->src->name, link->dst->name);
114 link->dst->inputs[dstpad_idx] = NULL;
115 if ((ret = avfilter_link(filt, filt_dstpad_idx, link->dst, dstpad_idx)) < 0) {
116 /* failed to link output filter to new filter */
117 link->dst->inputs[dstpad_idx] = link;
121 /* re-hookup the link to the new destination filter we inserted */
123 link->dstpad = &filt->input_pads[filt_srcpad_idx];
124 filt->inputs[filt_srcpad_idx] = link;
126 /* if any information on supported media formats already exists on the
127 * link, we need to preserve that */
128 if (link->out_formats)
129 ff_formats_changeref(&link->out_formats,
130 &filt->outputs[filt_dstpad_idx]->out_formats);
131 if (link->out_samplerates)
132 ff_formats_changeref(&link->out_samplerates,
133 &filt->outputs[filt_dstpad_idx]->out_samplerates);
134 if (link->out_channel_layouts)
135 ff_channel_layouts_changeref(&link->out_channel_layouts,
136 &filt->outputs[filt_dstpad_idx]->out_channel_layouts);
141 int avfilter_config_links(AVFilterContext *filter)
143 int (*config_link)(AVFilterLink *);
147 for (i = 0; i < filter->nb_inputs; i ++) {
148 AVFilterLink *link = filter->inputs[i];
152 switch (link->init_state) {
155 case AVLINK_STARTINIT:
156 av_log(filter, AV_LOG_INFO, "circular filter chain detected\n");
159 link->init_state = AVLINK_STARTINIT;
161 if ((ret = avfilter_config_links(link->src)) < 0)
164 if (!(config_link = link->srcpad->config_props)) {
165 if (link->src->nb_inputs != 1) {
166 av_log(link->src, AV_LOG_ERROR, "Source filters and filters "
167 "with more than one input "
168 "must set config_props() "
169 "callbacks on all outputs\n");
170 return AVERROR(EINVAL);
172 } else if ((ret = config_link(link)) < 0) {
173 av_log(link->src, AV_LOG_ERROR,
174 "Failed to configure output pad on %s\n",
179 if (link->time_base.num == 0 && link->time_base.den == 0)
180 link->time_base = link->src && link->src->nb_inputs ?
181 link->src->inputs[0]->time_base : AV_TIME_BASE_Q;
183 if (link->type == AVMEDIA_TYPE_VIDEO) {
184 if (!link->sample_aspect_ratio.num && !link->sample_aspect_ratio.den)
185 link->sample_aspect_ratio = link->src->nb_inputs ?
186 link->src->inputs[0]->sample_aspect_ratio : (AVRational){1,1};
188 if (link->src->nb_inputs) {
190 link->w = link->src->inputs[0]->w;
192 link->h = link->src->inputs[0]->h;
193 } else if (!link->w || !link->h) {
194 av_log(link->src, AV_LOG_ERROR,
195 "Video source filters must set their output link's "
196 "width and height\n");
197 return AVERROR(EINVAL);
201 if ((config_link = link->dstpad->config_props))
202 if ((ret = config_link(link)) < 0) {
203 av_log(link->src, AV_LOG_ERROR,
204 "Failed to configure input pad on %s\n",
209 link->init_state = AVLINK_INIT;
216 void ff_dlog_link(void *ctx, AVFilterLink *link, int end)
218 if (link->type == AVMEDIA_TYPE_VIDEO) {
220 "link[%p s:%dx%d fmt:%-16s %-16s->%-16s]%s",
221 link, link->w, link->h,
222 av_get_pix_fmt_name(link->format),
223 link->src ? link->src->filter->name : "",
224 link->dst ? link->dst->filter->name : "",
228 av_get_channel_layout_string(buf, sizeof(buf), -1, link->channel_layout);
231 "link[%p r:%d cl:%s fmt:%-16s %-16s->%-16s]%s",
232 link, link->sample_rate, buf,
233 av_get_sample_fmt_name(link->format),
234 link->src ? link->src->filter->name : "",
235 link->dst ? link->dst->filter->name : "",
240 int ff_request_frame(AVFilterLink *link)
242 FF_DPRINTF_START(NULL, request_frame); ff_dlog_link(NULL, link, 1);
244 if (link->srcpad->request_frame)
245 return link->srcpad->request_frame(link);
246 else if (link->src->inputs[0])
247 return ff_request_frame(link->src->inputs[0]);
251 int ff_poll_frame(AVFilterLink *link)
253 int i, min = INT_MAX;
255 if (link->srcpad->poll_frame)
256 return link->srcpad->poll_frame(link);
258 for (i = 0; i < link->src->nb_inputs; i++) {
260 if (!link->src->inputs[i])
262 val = ff_poll_frame(link->src->inputs[i]);
263 min = FFMIN(min, val);
269 static AVFilter *first_filter;
271 AVFilter *avfilter_get_by_name(const char *name)
278 while ((f = avfilter_next(f)))
279 if (!strcmp(f->name, name))
285 int avfilter_register(AVFilter *filter)
287 AVFilter **f = &first_filter;
295 const AVFilter *avfilter_next(const AVFilter *prev)
297 return prev ? prev->next : first_filter;
300 #if FF_API_OLD_FILTER_REGISTER
301 AVFilter **av_filter_next(AVFilter **filter)
303 return filter ? &(*filter)->next : &first_filter;
306 void avfilter_uninit(void)
311 int avfilter_pad_count(const AVFilterPad *pads)
318 for (count = 0; pads->name; count++)
323 static const char *filter_name(void *p)
325 AVFilterContext *filter = p;
326 return filter->filter->name;
329 static void *filter_child_next(void *obj, void *prev)
331 AVFilterContext *ctx = obj;
332 if (!prev && ctx->filter && ctx->filter->priv_class && ctx->priv)
337 static const AVClass *filter_child_class_next(const AVClass *prev)
341 while (prev && (f = avfilter_next(f)))
342 if (f->priv_class == prev)
345 while ((f = avfilter_next(f)))
347 return f->priv_class;
352 static const AVClass avfilter_class = {
353 .class_name = "AVFilter",
354 .item_name = filter_name,
355 .version = LIBAVUTIL_VERSION_INT,
356 .child_next = filter_child_next,
357 .child_class_next = filter_child_class_next,
360 AVFilterContext *ff_filter_alloc(const AVFilter *filter, const char *inst_name)
362 AVFilterContext *ret;
367 ret = av_mallocz(sizeof(AVFilterContext));
371 ret->av_class = &avfilter_class;
372 ret->filter = filter;
373 ret->name = inst_name ? av_strdup(inst_name) : NULL;
374 if (filter->priv_size) {
375 ret->priv = av_mallocz(filter->priv_size);
380 if (filter->priv_class) {
381 *(const AVClass**)ret->priv = filter->priv_class;
382 av_opt_set_defaults(ret->priv);
385 ret->nb_inputs = avfilter_pad_count(filter->inputs);
386 if (ret->nb_inputs ) {
387 ret->input_pads = av_malloc(sizeof(AVFilterPad) * ret->nb_inputs);
388 if (!ret->input_pads)
390 memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad) * ret->nb_inputs);
391 ret->inputs = av_mallocz(sizeof(AVFilterLink*) * ret->nb_inputs);
396 ret->nb_outputs = avfilter_pad_count(filter->outputs);
397 if (ret->nb_outputs) {
398 ret->output_pads = av_malloc(sizeof(AVFilterPad) * ret->nb_outputs);
399 if (!ret->output_pads)
401 memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad) * ret->nb_outputs);
402 ret->outputs = av_mallocz(sizeof(AVFilterLink*) * ret->nb_outputs);
407 ret->output_count = ret->nb_outputs;
408 ret->input_count = ret->nb_inputs;
414 av_freep(&ret->inputs);
415 av_freep(&ret->input_pads);
417 av_freep(&ret->outputs);
418 av_freep(&ret->output_pads);
420 av_freep(&ret->priv);
425 #if FF_API_AVFILTER_OPEN
426 int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name)
428 *filter_ctx = ff_filter_alloc(filter, inst_name);
429 return *filter_ctx ? 0 : AVERROR(ENOMEM);
433 static void free_link(AVFilterLink *link)
439 link->src->outputs[link->srcpad - link->src->output_pads] = NULL;
441 link->dst->inputs[link->dstpad - link->dst->input_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 void avfilter_free(AVFilterContext *filter)
457 ff_filter_graph_remove_filter(filter->graph, filter);
459 if (filter->filter->uninit)
460 filter->filter->uninit(filter);
462 for (i = 0; i < filter->nb_inputs; i++) {
463 free_link(filter->inputs[i]);
465 for (i = 0; i < filter->nb_outputs; i++) {
466 free_link(filter->outputs[i]);
469 if (filter->filter->priv_class)
470 av_opt_free(filter->priv);
472 av_freep(&filter->name);
473 av_freep(&filter->input_pads);
474 av_freep(&filter->output_pads);
475 av_freep(&filter->inputs);
476 av_freep(&filter->outputs);
477 av_freep(&filter->priv);
481 /* process a list of value1:value2:..., each value corresponding
482 * to subsequent AVOption, in the order they are declared */
483 static int process_unnamed_options(AVFilterContext *ctx, AVDictionary **options,
486 const AVOption *o = NULL;
487 const char *p = args;
491 o = av_opt_next(ctx->priv, o);
493 av_log(ctx, AV_LOG_ERROR, "More options provided than "
494 "this filter supports.\n");
495 return AVERROR(EINVAL);
497 if (o->type == AV_OPT_TYPE_CONST)
500 val = av_get_token(&p, ":");
502 return AVERROR(ENOMEM);
504 av_dict_set(options, o->name, val, 0);
514 #if FF_API_AVFILTER_INIT_FILTER
515 int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
517 return avfilter_init_str(filter, args);
521 int avfilter_init_dict(AVFilterContext *ctx, AVDictionary **options)
525 if (ctx->filter->priv_class) {
526 ret = av_opt_set_dict(ctx->priv, options);
528 av_log(ctx, AV_LOG_ERROR, "Error applying options to the filter.\n");
533 if (ctx->filter->init)
534 ret = ctx->filter->init(ctx);
535 else if (ctx->filter->init_dict)
536 ret = ctx->filter->init_dict(ctx, options);
541 int avfilter_init_str(AVFilterContext *filter, const char *args)
543 AVDictionary *options = NULL;
544 AVDictionaryEntry *e;
548 if (!filter->filter->priv_class) {
549 av_log(filter, AV_LOG_ERROR, "This filter does not take any "
550 "options, but options were provided: %s.\n", args);
551 return AVERROR(EINVAL);
554 #if FF_API_OLD_FILTER_OPTS
555 if (!strcmp(filter->filter->name, "scale") &&
556 strchr(args, ':') && strchr(args, ':') < strchr(args, '=')) {
557 /* old w:h:flags=<flags> syntax */
558 char *copy = av_strdup(args);
561 av_log(filter, AV_LOG_WARNING, "The <w>:<h>:flags=<flags> option "
562 "syntax is deprecated. Use either <w>:<h>:<flags> or "
563 "w=<w>:h=<h>:flags=<flags>.\n");
566 ret = AVERROR(ENOMEM);
570 p = strrchr(copy, ':');
573 ret = av_dict_parse_string(&options, p, "=", ":", 0);
576 ret = process_unnamed_options(filter, &options, copy);
584 if (strchr(args, '=')) {
585 /* assume a list of key1=value1:key2=value2:... */
586 ret = av_dict_parse_string(&options, args, "=", ":", 0);
589 #if FF_API_OLD_FILTER_OPTS
590 } else if (!strcmp(filter->filter->name, "format") ||
591 !strcmp(filter->filter->name, "noformat") ||
592 !strcmp(filter->filter->name, "frei0r") ||
593 !strcmp(filter->filter->name, "frei0r_src") ||
594 !strcmp(filter->filter->name, "ocv")) {
595 /* a hack for compatibility with the old syntax
596 * replace colons with |s */
597 char *copy = av_strdup(args);
599 int nb_leading = 0; // number of leading colons to skip
602 ret = AVERROR(ENOMEM);
606 if (!strcmp(filter->filter->name, "frei0r") ||
607 !strcmp(filter->filter->name, "ocv"))
609 else if (!strcmp(filter->filter->name, "frei0r_src"))
612 while (nb_leading--) {
615 p = copy + strlen(copy);
621 if (strchr(p, ':')) {
622 av_log(filter, AV_LOG_WARNING, "This syntax is deprecated. Use "
623 "'|' to separate the list items.\n");
626 while ((p = strchr(p, ':')))
629 ret = process_unnamed_options(filter, &options, copy);
636 ret = process_unnamed_options(filter, &options, args);
642 ret = avfilter_init_dict(filter, &options);
646 if ((e = av_dict_get(options, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
647 av_log(filter, AV_LOG_ERROR, "No such option: %s.\n", e->key);
648 ret = AVERROR_OPTION_NOT_FOUND;
653 av_dict_free(&options);
658 const char *avfilter_pad_get_name(const AVFilterPad *pads, int pad_idx)
660 return pads[pad_idx].name;
663 enum AVMediaType avfilter_pad_get_type(const AVFilterPad *pads, int pad_idx)
665 return pads[pad_idx].type;
668 static int default_filter_frame(AVFilterLink *link, AVFrame *frame)
670 return ff_filter_frame(link->dst->outputs[0], frame);
673 int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
675 int (*filter_frame)(AVFilterLink *, AVFrame *);
676 AVFilterPad *dst = link->dstpad;
679 FF_DPRINTF_START(NULL, filter_frame);
680 ff_dlog_link(NULL, link, 1);
682 if (!(filter_frame = dst->filter_frame))
683 filter_frame = default_filter_frame;
685 /* copy the frame if needed */
686 if (dst->needs_writable && !av_frame_is_writable(frame)) {
687 av_log(link->dst, AV_LOG_DEBUG, "Copying data in avfilter.\n");
689 switch (link->type) {
690 case AVMEDIA_TYPE_VIDEO:
691 out = ff_get_video_buffer(link, link->w, link->h);
693 case AVMEDIA_TYPE_AUDIO:
694 out = ff_get_audio_buffer(link, frame->nb_samples);
696 default: return AVERROR(EINVAL);
699 av_frame_free(&frame);
700 return AVERROR(ENOMEM);
702 av_frame_copy_props(out, frame);
704 switch (link->type) {
705 case AVMEDIA_TYPE_VIDEO:
706 av_image_copy(out->data, out->linesize, frame->data, frame->linesize,
707 frame->format, frame->width, frame->height);
709 case AVMEDIA_TYPE_AUDIO:
710 av_samples_copy(out->extended_data, frame->extended_data,
711 0, 0, frame->nb_samples,
712 av_get_channel_layout_nb_channels(frame->channel_layout),
715 default: return AVERROR(EINVAL);
718 av_frame_free(&frame);
722 return filter_frame(link, out);
725 const AVClass *avfilter_get_class(void)
727 return &avfilter_class;