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/buffer.h"
24 #include "libavutil/channel_layout.h"
25 #include "libavutil/common.h"
26 #include "libavutil/hwcontext.h"
27 #include "libavutil/imgutils.h"
28 #include "libavutil/internal.h"
29 #include "libavutil/opt.h"
30 #include "libavutil/pixdesc.h"
31 #include "libavutil/rational.h"
32 #include "libavutil/samplefmt.h"
40 unsigned avfilter_version(void)
42 return LIBAVFILTER_VERSION_INT;
45 const char *avfilter_configuration(void)
47 return LIBAV_CONFIGURATION;
50 const char *avfilter_license(void)
52 #define LICENSE_PREFIX "libavfilter license: "
53 return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
56 void ff_insert_pad(unsigned idx, unsigned *count, size_t padidx_off,
57 AVFilterPad **pads, AVFilterLink ***links,
62 idx = FFMIN(idx, *count);
64 *pads = av_realloc(*pads, sizeof(AVFilterPad) * (*count + 1));
65 *links = av_realloc(*links, sizeof(AVFilterLink*) * (*count + 1));
66 memmove(*pads + idx + 1, *pads + idx, sizeof(AVFilterPad) * (*count - idx));
67 memmove(*links + idx + 1, *links + idx, sizeof(AVFilterLink*) * (*count - idx));
68 memcpy(*pads + idx, newpad, sizeof(AVFilterPad));
72 for (i = idx + 1; i < *count; i++)
74 (*(unsigned *)((uint8_t *) *links[i] + padidx_off))++;
77 int avfilter_link(AVFilterContext *src, unsigned srcpad,
78 AVFilterContext *dst, unsigned dstpad)
82 if (src->nb_outputs <= srcpad || dst->nb_inputs <= dstpad ||
83 src->outputs[srcpad] || dst->inputs[dstpad])
84 return AVERROR(EINVAL);
86 if (src->output_pads[srcpad].type != dst->input_pads[dstpad].type) {
87 av_log(src, AV_LOG_ERROR,
88 "Media type mismatch between the '%s' filter output pad %d and the '%s' filter input pad %d\n",
89 src->name, srcpad, dst->name, dstpad);
90 return AVERROR(EINVAL);
93 link = av_mallocz(sizeof(*link));
95 return AVERROR(ENOMEM);
97 src->outputs[srcpad] = dst->inputs[dstpad] = link;
101 link->srcpad = &src->output_pads[srcpad];
102 link->dstpad = &dst->input_pads[dstpad];
103 link->type = src->output_pads[srcpad].type;
104 assert(AV_PIX_FMT_NONE == -1 && AV_SAMPLE_FMT_NONE == -1);
110 int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt,
111 unsigned filt_srcpad_idx, unsigned filt_dstpad_idx)
114 unsigned dstpad_idx = link->dstpad - link->dst->input_pads;
116 av_log(link->dst, AV_LOG_VERBOSE, "auto-inserting filter '%s' "
117 "between the filter '%s' and the filter '%s'\n",
118 filt->name, link->src->name, link->dst->name);
120 link->dst->inputs[dstpad_idx] = NULL;
121 if ((ret = avfilter_link(filt, filt_dstpad_idx, link->dst, dstpad_idx)) < 0) {
122 /* failed to link output filter to new filter */
123 link->dst->inputs[dstpad_idx] = link;
127 /* re-hookup the link to the new destination filter we inserted */
129 link->dstpad = &filt->input_pads[filt_srcpad_idx];
130 filt->inputs[filt_srcpad_idx] = link;
132 /* if any information on supported media formats already exists on the
133 * link, we need to preserve that */
134 if (link->out_formats)
135 ff_formats_changeref(&link->out_formats,
136 &filt->outputs[filt_dstpad_idx]->out_formats);
137 if (link->out_samplerates)
138 ff_formats_changeref(&link->out_samplerates,
139 &filt->outputs[filt_dstpad_idx]->out_samplerates);
140 if (link->out_channel_layouts)
141 ff_channel_layouts_changeref(&link->out_channel_layouts,
142 &filt->outputs[filt_dstpad_idx]->out_channel_layouts);
147 int avfilter_config_links(AVFilterContext *filter)
149 int (*config_link)(AVFilterLink *);
153 for (i = 0; i < filter->nb_inputs; i ++) {
154 AVFilterLink *link = filter->inputs[i];
157 if (!link->src || !link->dst) {
158 av_log(filter, AV_LOG_ERROR,
159 "Not all input and output are properly linked (%d).\n", i);
160 return AVERROR(EINVAL);
163 switch (link->init_state) {
166 case AVLINK_STARTINIT:
167 av_log(filter, AV_LOG_INFO, "circular filter chain detected\n");
170 link->init_state = AVLINK_STARTINIT;
172 if ((ret = avfilter_config_links(link->src)) < 0)
175 if (!(config_link = link->srcpad->config_props)) {
176 if (link->src->nb_inputs != 1) {
177 av_log(link->src, AV_LOG_ERROR, "Source filters and filters "
178 "with more than one input "
179 "must set config_props() "
180 "callbacks on all outputs\n");
181 return AVERROR(EINVAL);
183 } else if ((ret = config_link(link)) < 0) {
184 av_log(link->src, AV_LOG_ERROR,
185 "Failed to configure output pad on %s\n",
190 if (link->time_base.num == 0 && link->time_base.den == 0)
191 link->time_base = link->src->nb_inputs ?
192 link->src->inputs[0]->time_base : AV_TIME_BASE_Q;
194 if (link->type == AVMEDIA_TYPE_VIDEO) {
195 if (!link->sample_aspect_ratio.num && !link->sample_aspect_ratio.den)
196 link->sample_aspect_ratio = link->src->nb_inputs ?
197 link->src->inputs[0]->sample_aspect_ratio : (AVRational){1,1};
199 if (link->src->nb_inputs) {
200 if (!link->frame_rate.num && !link->frame_rate.den)
201 link->frame_rate = link->src->inputs[0]->frame_rate;
203 link->w = link->src->inputs[0]->w;
205 link->h = link->src->inputs[0]->h;
206 } else if (!link->w || !link->h) {
207 av_log(link->src, AV_LOG_ERROR,
208 "Video source filters must set their output link's "
209 "width and height\n");
210 return AVERROR(EINVAL);
214 if ((config_link = link->dstpad->config_props))
215 if ((ret = config_link(link)) < 0) {
216 av_log(link->dst, AV_LOG_ERROR,
217 "Failed to configure input pad on %s\n",
222 if (link->src->nb_inputs && link->src->inputs[0]->hw_frames_ctx &&
223 !link->hw_frames_ctx) {
224 AVHWFramesContext *input_ctx = (AVHWFramesContext*)link->src->inputs[0]->hw_frames_ctx->data;
226 if (input_ctx->format == link->format) {
227 link->hw_frames_ctx = av_buffer_ref(link->src->inputs[0]->hw_frames_ctx);
228 if (!link->hw_frames_ctx)
229 return AVERROR(ENOMEM);
233 link->init_state = AVLINK_INIT;
240 void ff_dlog_link(void *ctx, AVFilterLink *link, int end)
242 if (link->type == AVMEDIA_TYPE_VIDEO) {
243 av_log(ctx, AV_LOG_TRACE,
244 "link[%p s:%dx%d fmt:%-16s %-16s->%-16s]%s",
245 link, link->w, link->h,
246 av_get_pix_fmt_name(link->format),
247 link->src ? link->src->filter->name : "",
248 link->dst ? link->dst->filter->name : "",
252 av_get_channel_layout_string(buf, sizeof(buf), -1, link->channel_layout);
254 av_log(ctx, AV_LOG_TRACE,
255 "link[%p r:%d cl:%s fmt:%-16s %-16s->%-16s]%s",
256 link, link->sample_rate, buf,
257 av_get_sample_fmt_name(link->format),
258 link->src ? link->src->filter->name : "",
259 link->dst ? link->dst->filter->name : "",
264 int ff_request_frame(AVFilterLink *link)
266 FF_DPRINTF_START(NULL, request_frame); ff_dlog_link(NULL, link, 1);
268 if (link->srcpad->request_frame)
269 return link->srcpad->request_frame(link);
270 else if (link->src->inputs[0])
271 return ff_request_frame(link->src->inputs[0]);
273 return AVERROR(EINVAL);
276 int ff_poll_frame(AVFilterLink *link)
278 int i, min = INT_MAX;
280 if (link->srcpad->poll_frame)
281 return link->srcpad->poll_frame(link);
283 for (i = 0; i < link->src->nb_inputs; i++) {
285 if (!link->src->inputs[i])
286 return AVERROR(EINVAL);
287 val = ff_poll_frame(link->src->inputs[i]);
288 min = FFMIN(min, val);
294 static AVFilter *first_filter;
296 #if !FF_API_NOCONST_GET_NAME
299 AVFilter *avfilter_get_by_name(const char *name)
301 const AVFilter *f = NULL;
306 while ((f = avfilter_next(f)))
307 if (!strcmp(f->name, name))
313 int avfilter_register(AVFilter *filter)
315 AVFilter **f = &first_filter;
323 const AVFilter *avfilter_next(const AVFilter *prev)
325 return prev ? prev->next : first_filter;
328 #if FF_API_OLD_FILTER_REGISTER
329 AVFilter **av_filter_next(AVFilter **filter)
331 return filter ? &(*filter)->next : &first_filter;
334 void avfilter_uninit(void)
339 int avfilter_pad_count(const AVFilterPad *pads)
346 for (count = 0; pads->name; count++)
351 static const char *filter_name(void *p)
353 AVFilterContext *filter = p;
354 return filter->filter->name;
357 static void *filter_child_next(void *obj, void *prev)
359 AVFilterContext *ctx = obj;
360 if (!prev && ctx->filter && ctx->filter->priv_class && ctx->priv)
365 static const AVClass *filter_child_class_next(const AVClass *prev)
367 const AVFilter *f = NULL;
369 while (prev && (f = avfilter_next(f)))
370 if (f->priv_class == prev)
373 while ((f = avfilter_next(f)))
375 return f->priv_class;
380 #define OFFSET(x) offsetof(AVFilterContext, x)
381 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM
382 static const AVOption avfilter_options[] = {
383 { "thread_type", "Allowed thread types", OFFSET(thread_type), AV_OPT_TYPE_FLAGS,
384 { .i64 = AVFILTER_THREAD_SLICE }, 0, INT_MAX, FLAGS, "thread_type" },
385 { "slice", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AVFILTER_THREAD_SLICE }, .unit = "thread_type" },
389 static const AVClass avfilter_class = {
390 .class_name = "AVFilter",
391 .item_name = filter_name,
392 .version = LIBAVUTIL_VERSION_INT,
393 .child_next = filter_child_next,
394 .child_class_next = filter_child_class_next,
395 .option = avfilter_options,
398 static int default_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg,
399 int *ret, int nb_jobs)
403 for (i = 0; i < nb_jobs; i++) {
404 int r = func(ctx, arg, i, nb_jobs);
411 AVFilterContext *ff_filter_alloc(const AVFilter *filter, const char *inst_name)
413 AVFilterContext *ret;
418 ret = av_mallocz(sizeof(AVFilterContext));
422 ret->av_class = &avfilter_class;
423 ret->filter = filter;
424 ret->name = inst_name ? av_strdup(inst_name) : NULL;
425 if (filter->priv_size) {
426 ret->priv = av_mallocz(filter->priv_size);
431 av_opt_set_defaults(ret);
432 if (filter->priv_class) {
433 *(const AVClass**)ret->priv = filter->priv_class;
434 av_opt_set_defaults(ret->priv);
437 ret->internal = av_mallocz(sizeof(*ret->internal));
440 ret->internal->execute = default_execute;
442 ret->nb_inputs = avfilter_pad_count(filter->inputs);
443 if (ret->nb_inputs ) {
444 ret->input_pads = av_malloc(sizeof(AVFilterPad) * ret->nb_inputs);
445 if (!ret->input_pads)
447 memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad) * ret->nb_inputs);
448 ret->inputs = av_mallocz(sizeof(AVFilterLink*) * ret->nb_inputs);
453 ret->nb_outputs = avfilter_pad_count(filter->outputs);
454 if (ret->nb_outputs) {
455 ret->output_pads = av_malloc(sizeof(AVFilterPad) * ret->nb_outputs);
456 if (!ret->output_pads)
458 memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad) * ret->nb_outputs);
459 ret->outputs = av_mallocz(sizeof(AVFilterLink*) * ret->nb_outputs);
467 av_freep(&ret->inputs);
468 av_freep(&ret->input_pads);
470 av_freep(&ret->outputs);
471 av_freep(&ret->output_pads);
473 av_freep(&ret->priv);
474 av_freep(&ret->internal);
479 #if FF_API_AVFILTER_OPEN
480 int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name)
482 *filter_ctx = ff_filter_alloc(filter, inst_name);
483 return *filter_ctx ? 0 : AVERROR(ENOMEM);
487 static void free_link(AVFilterLink *link)
493 link->src->outputs[link->srcpad - link->src->output_pads] = NULL;
495 link->dst->inputs[link->dstpad - link->dst->input_pads] = NULL;
497 av_buffer_unref(&link->hw_frames_ctx);
499 ff_formats_unref(&link->in_formats);
500 ff_formats_unref(&link->out_formats);
501 ff_formats_unref(&link->in_samplerates);
502 ff_formats_unref(&link->out_samplerates);
503 ff_channel_layouts_unref(&link->in_channel_layouts);
504 ff_channel_layouts_unref(&link->out_channel_layouts);
508 void avfilter_free(AVFilterContext *filter)
513 ff_filter_graph_remove_filter(filter->graph, filter);
515 if (filter->filter->uninit)
516 filter->filter->uninit(filter);
518 for (i = 0; i < filter->nb_inputs; i++) {
519 free_link(filter->inputs[i]);
521 for (i = 0; i < filter->nb_outputs; i++) {
522 free_link(filter->outputs[i]);
525 if (filter->filter->priv_class)
526 av_opt_free(filter->priv);
528 av_buffer_unref(&filter->hw_device_ctx);
530 av_freep(&filter->name);
531 av_freep(&filter->input_pads);
532 av_freep(&filter->output_pads);
533 av_freep(&filter->inputs);
534 av_freep(&filter->outputs);
535 av_freep(&filter->priv);
536 av_freep(&filter->internal);
540 /* process a list of value1:value2:..., each value corresponding
541 * to subsequent AVOption, in the order they are declared */
542 static int process_unnamed_options(AVFilterContext *ctx, AVDictionary **options,
545 const AVOption *o = NULL;
546 const char *p = args;
550 o = av_opt_next(ctx->priv, o);
552 av_log(ctx, AV_LOG_ERROR, "More options provided than "
553 "this filter supports.\n");
554 return AVERROR(EINVAL);
556 if (o->type == AV_OPT_TYPE_CONST)
559 val = av_get_token(&p, ":");
561 return AVERROR(ENOMEM);
563 av_dict_set(options, o->name, val, 0);
573 #if FF_API_AVFILTER_INIT_FILTER
574 int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
576 return avfilter_init_str(filter, args);
580 int avfilter_init_dict(AVFilterContext *ctx, AVDictionary **options)
584 ret = av_opt_set_dict(ctx, options);
586 av_log(ctx, AV_LOG_ERROR, "Error applying generic filter options.\n");
590 if (ctx->filter->flags & AVFILTER_FLAG_SLICE_THREADS &&
591 ctx->thread_type & ctx->graph->thread_type & AVFILTER_THREAD_SLICE &&
592 ctx->graph->internal->thread_execute) {
593 ctx->thread_type = AVFILTER_THREAD_SLICE;
594 ctx->internal->execute = ctx->graph->internal->thread_execute;
596 ctx->thread_type = 0;
599 if (ctx->filter->priv_class) {
600 ret = av_opt_set_dict(ctx->priv, options);
602 av_log(ctx, AV_LOG_ERROR, "Error applying options to the filter.\n");
607 if (ctx->filter->init)
608 ret = ctx->filter->init(ctx);
609 else if (ctx->filter->init_dict)
610 ret = ctx->filter->init_dict(ctx, options);
615 int avfilter_init_str(AVFilterContext *filter, const char *args)
617 AVDictionary *options = NULL;
618 AVDictionaryEntry *e;
622 if (!filter->filter->priv_class) {
623 av_log(filter, AV_LOG_ERROR, "This filter does not take any "
624 "options, but options were provided: %s.\n", args);
625 return AVERROR(EINVAL);
628 #if FF_API_OLD_FILTER_OPTS
629 if (!strcmp(filter->filter->name, "scale") &&
630 strchr(args, ':') && strchr(args, ':') < strchr(args, '=')) {
631 /* old w:h:flags=<flags> syntax */
632 char *copy = av_strdup(args);
635 av_log(filter, AV_LOG_WARNING, "The <w>:<h>:flags=<flags> option "
636 "syntax is deprecated. Use either <w>:<h>:<flags> or "
637 "w=<w>:h=<h>:flags=<flags>.\n");
640 ret = AVERROR(ENOMEM);
644 p = strrchr(copy, ':');
647 ret = av_dict_parse_string(&options, p, "=", ":", 0);
650 ret = process_unnamed_options(filter, &options, copy);
658 if (strchr(args, '=')) {
659 /* assume a list of key1=value1:key2=value2:... */
660 ret = av_dict_parse_string(&options, args, "=", ":", 0);
663 #if FF_API_OLD_FILTER_OPTS
664 } else if (!strcmp(filter->filter->name, "format") ||
665 !strcmp(filter->filter->name, "noformat") ||
666 !strcmp(filter->filter->name, "frei0r") ||
667 !strcmp(filter->filter->name, "frei0r_src") ||
668 !strcmp(filter->filter->name, "ocv")) {
669 /* a hack for compatibility with the old syntax
670 * replace colons with |s */
671 char *copy = av_strdup(args);
673 int nb_leading = 0; // number of leading colons to skip
676 ret = AVERROR(ENOMEM);
680 if (!strcmp(filter->filter->name, "frei0r") ||
681 !strcmp(filter->filter->name, "ocv"))
683 else if (!strcmp(filter->filter->name, "frei0r_src"))
686 while (nb_leading--) {
689 p = copy + strlen(copy);
695 if (strchr(p, ':')) {
696 av_log(filter, AV_LOG_WARNING, "This syntax is deprecated. Use "
697 "'|' to separate the list items.\n");
700 while ((p = strchr(p, ':')))
703 ret = process_unnamed_options(filter, &options, copy);
710 ret = process_unnamed_options(filter, &options, args);
716 ret = avfilter_init_dict(filter, &options);
720 if ((e = av_dict_get(options, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
721 av_log(filter, AV_LOG_ERROR, "No such option: %s.\n", e->key);
722 ret = AVERROR_OPTION_NOT_FOUND;
727 av_dict_free(&options);
732 const char *avfilter_pad_get_name(const AVFilterPad *pads, int pad_idx)
734 return pads[pad_idx].name;
737 enum AVMediaType avfilter_pad_get_type(const AVFilterPad *pads, int pad_idx)
739 return pads[pad_idx].type;
742 static int default_filter_frame(AVFilterLink *link, AVFrame *frame)
744 return ff_filter_frame(link->dst->outputs[0], frame);
747 int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
749 int (*filter_frame)(AVFilterLink *, AVFrame *);
750 AVFilterPad *dst = link->dstpad;
754 FF_DPRINTF_START(NULL, filter_frame);
755 ff_dlog_link(NULL, link, 1);
757 if (!(filter_frame = dst->filter_frame))
758 filter_frame = default_filter_frame;
760 /* copy the frame if needed */
761 if (dst->needs_writable && !av_frame_is_writable(frame)) {
762 av_log(link->dst, AV_LOG_DEBUG, "Copying data in avfilter.\n");
764 switch (link->type) {
765 case AVMEDIA_TYPE_VIDEO:
766 out = ff_get_video_buffer(link, link->w, link->h);
768 case AVMEDIA_TYPE_AUDIO:
769 out = ff_get_audio_buffer(link, frame->nb_samples);
772 ret = AVERROR(EINVAL);
776 ret = AVERROR(ENOMEM);
780 ret = av_frame_copy_props(out, frame);
784 switch (link->type) {
785 case AVMEDIA_TYPE_VIDEO:
786 av_image_copy(out->data, out->linesize, frame->data, frame->linesize,
787 frame->format, frame->width, frame->height);
789 case AVMEDIA_TYPE_AUDIO:
790 av_samples_copy(out->extended_data, frame->extended_data,
791 0, 0, frame->nb_samples,
792 av_get_channel_layout_nb_channels(frame->channel_layout),
796 ret = AVERROR(EINVAL);
800 av_frame_free(&frame);
804 return filter_frame(link, out);
808 av_frame_free(&frame);
812 const AVClass *avfilter_get_class(void)
814 return &avfilter_class;