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 const AVClass avfilter_class = {
325 .class_name = "AVFilter",
326 .item_name = filter_name,
327 .version = LIBAVUTIL_VERSION_INT,
330 int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name)
332 AVFilterContext *ret;
336 return AVERROR(EINVAL);
338 ret = av_mallocz(sizeof(AVFilterContext));
340 return AVERROR(ENOMEM);
342 ret->av_class = &avfilter_class;
343 ret->filter = filter;
344 ret->name = inst_name ? av_strdup(inst_name) : NULL;
345 if (filter->priv_size) {
346 ret->priv = av_mallocz(filter->priv_size);
351 if (filter->priv_class) {
352 *(const AVClass**)ret->priv = filter->priv_class;
353 av_opt_set_defaults(ret->priv);
356 ret->nb_inputs = pad_count(filter->inputs);
357 if (ret->nb_inputs ) {
358 ret->input_pads = av_malloc(sizeof(AVFilterPad) * ret->nb_inputs);
359 if (!ret->input_pads)
361 memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad) * ret->nb_inputs);
362 ret->inputs = av_mallocz(sizeof(AVFilterLink*) * ret->nb_inputs);
367 ret->nb_outputs = pad_count(filter->outputs);
368 if (ret->nb_outputs) {
369 ret->output_pads = av_malloc(sizeof(AVFilterPad) * ret->nb_outputs);
370 if (!ret->output_pads)
372 memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad) * ret->nb_outputs);
373 ret->outputs = av_mallocz(sizeof(AVFilterLink*) * ret->nb_outputs);
378 ret->output_count = ret->nb_outputs;
379 ret->input_count = ret->nb_inputs;
386 av_freep(&ret->inputs);
387 av_freep(&ret->input_pads);
389 av_freep(&ret->outputs);
390 av_freep(&ret->output_pads);
392 av_freep(&ret->priv);
394 return AVERROR(ENOMEM);
397 void avfilter_free(AVFilterContext *filter)
402 if (filter->filter->uninit)
403 filter->filter->uninit(filter);
405 for (i = 0; i < filter->nb_inputs; i++) {
406 if ((link = filter->inputs[i])) {
408 link->src->outputs[link->srcpad - link->src->output_pads] = NULL;
409 ff_formats_unref(&link->in_formats);
410 ff_formats_unref(&link->out_formats);
411 ff_formats_unref(&link->in_samplerates);
412 ff_formats_unref(&link->out_samplerates);
413 ff_channel_layouts_unref(&link->in_channel_layouts);
414 ff_channel_layouts_unref(&link->out_channel_layouts);
418 for (i = 0; i < filter->nb_outputs; i++) {
419 if ((link = filter->outputs[i])) {
421 link->dst->inputs[link->dstpad - link->dst->input_pads] = NULL;
422 ff_formats_unref(&link->in_formats);
423 ff_formats_unref(&link->out_formats);
424 ff_formats_unref(&link->in_samplerates);
425 ff_formats_unref(&link->out_samplerates);
426 ff_channel_layouts_unref(&link->in_channel_layouts);
427 ff_channel_layouts_unref(&link->out_channel_layouts);
432 if (filter->filter->priv_class)
433 av_opt_free(filter->priv);
435 av_freep(&filter->name);
436 av_freep(&filter->input_pads);
437 av_freep(&filter->output_pads);
438 av_freep(&filter->inputs);
439 av_freep(&filter->outputs);
440 av_freep(&filter->priv);
444 /* process a list of value1:value2:..., each value corresponding
445 * to subsequent AVOption, in the order they are declared */
446 static int process_unnamed_options(AVFilterContext *ctx, AVDictionary **options,
449 const AVOption *o = NULL;
450 const char *p = args;
454 o = av_opt_next(ctx->priv, o);
456 av_log(ctx, AV_LOG_ERROR, "More options provided than "
457 "this filter supports.\n");
458 return AVERROR(EINVAL);
460 if (o->type == AV_OPT_TYPE_CONST)
463 val = av_get_token(&p, ":");
465 return AVERROR(ENOMEM);
467 av_dict_set(options, o->name, val, 0);
477 int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
479 AVDictionary *options = NULL;
480 AVDictionaryEntry *e;
483 if (args && *args && filter->filter->priv_class) {
484 #if FF_API_OLD_FILTER_OPTS
485 if (!strcmp(filter->filter->name, "scale") &&
486 strchr(args, ':') < strchr(args, '=')) {
487 /* old w:h:flags=<flags> syntax */
488 char *copy = av_strdup(args);
491 av_log(filter, AV_LOG_WARNING, "The <w>:<h>:flags=<flags> option "
492 "syntax is deprecated. Use either <w>:<h>:<flags> or "
493 "w=<w>:h=<h>:flags=<flags>.\n");
496 ret = AVERROR(ENOMEM);
500 p = strrchr(copy, ':');
503 ret = av_dict_parse_string(&options, p, "=", ":", 0);
506 ret = process_unnamed_options(filter, &options, copy);
514 if (strchr(args, '=')) {
515 /* assume a list of key1=value1:key2=value2:... */
516 ret = av_dict_parse_string(&options, args, "=", ":", 0);
519 #if FF_API_OLD_FILTER_OPTS
520 } else if (!strcmp(filter->filter->name, "format") ||
521 !strcmp(filter->filter->name, "noformat") ||
522 !strcmp(filter->filter->name, "frei0r") ||
523 !strcmp(filter->filter->name, "frei0r_src") ||
524 !strcmp(filter->filter->name, "ocv")) {
525 /* a hack for compatibility with the old syntax
526 * replace colons with |s */
527 char *copy = av_strdup(args);
529 int nb_leading = 0; // number of leading colons to skip
532 ret = AVERROR(ENOMEM);
536 if (!strcmp(filter->filter->name, "frei0r") ||
537 !strcmp(filter->filter->name, "ocv"))
539 else if (!strcmp(filter->filter->name, "frei0r_src"))
542 while (nb_leading--) {
545 p = copy + strlen(copy);
551 if (strchr(p, ':')) {
552 av_log(filter, AV_LOG_WARNING, "This syntax is deprecated. Use "
553 "'|' to separate the list items.\n");
556 while ((p = strchr(p, ':')))
559 ret = process_unnamed_options(filter, &options, copy);
566 ret = process_unnamed_options(filter, &options, args);
572 if (filter->filter->priv_class) {
573 ret = av_opt_set_dict(filter->priv, &options);
575 av_log(filter, AV_LOG_ERROR, "Error applying options to the filter.\n");
580 if (filter->filter->init)
581 ret = filter->filter->init(filter);
582 else if (filter->filter->init_dict)
583 ret = filter->filter->init_dict(filter, &options);
587 if ((e = av_dict_get(options, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
588 av_log(filter, AV_LOG_ERROR, "No such option: %s.\n", e->key);
589 ret = AVERROR_OPTION_NOT_FOUND;
594 av_dict_free(&options);
599 const char *avfilter_pad_get_name(AVFilterPad *pads, int pad_idx)
601 return pads[pad_idx].name;
604 enum AVMediaType avfilter_pad_get_type(AVFilterPad *pads, int pad_idx)
606 return pads[pad_idx].type;
609 static int default_filter_frame(AVFilterLink *link, AVFrame *frame)
611 return ff_filter_frame(link->dst->outputs[0], frame);
614 int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
616 int (*filter_frame)(AVFilterLink *, AVFrame *);
617 AVFilterPad *dst = link->dstpad;
620 FF_DPRINTF_START(NULL, filter_frame);
621 ff_dlog_link(NULL, link, 1);
623 if (!(filter_frame = dst->filter_frame))
624 filter_frame = default_filter_frame;
626 /* copy the frame if needed */
627 if (dst->needs_writable && !av_frame_is_writable(frame)) {
628 av_log(link->dst, AV_LOG_DEBUG, "Copying data in avfilter.\n");
630 switch (link->type) {
631 case AVMEDIA_TYPE_VIDEO:
632 out = ff_get_video_buffer(link, link->w, link->h);
634 case AVMEDIA_TYPE_AUDIO:
635 out = ff_get_audio_buffer(link, frame->nb_samples);
637 default: return AVERROR(EINVAL);
640 av_frame_free(&frame);
641 return AVERROR(ENOMEM);
643 av_frame_copy_props(out, frame);
645 switch (link->type) {
646 case AVMEDIA_TYPE_VIDEO:
647 av_image_copy(out->data, out->linesize, frame->data, frame->linesize,
648 frame->format, frame->width, frame->height);
650 case AVMEDIA_TYPE_AUDIO:
651 av_samples_copy(out->extended_data, frame->extended_data,
652 0, 0, frame->nb_samples,
653 av_get_channel_layout_nb_channels(frame->channel_layout),
656 default: return AVERROR(EINVAL);
659 av_frame_free(&frame);
663 return filter_frame(link, out);