[FFmpeg-devel] [PATCH] avfilter/vf_colorkey: Add colorkey video filter
Timo Rothenpieler
timo at rothenpieler.org
Sat Apr 18 22:28:47 CEST 2015
---
Changelog | 1 +
MAINTAINERS | 1 +
libavfilter/Makefile | 1 +
libavfilter/allfilters.c | 1 +
libavfilter/vf_colorkey.c | 226 ++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 230 insertions(+)
create mode 100644 libavfilter/vf_colorkey.c
diff --git a/Changelog b/Changelog
index 2e30b24..e99d1e9 100644
--- a/Changelog
+++ b/Changelog
@@ -17,6 +17,7 @@ version <next>:
- WebM Live Chunk Muxer
- nvenc level and tier options
- chorus filter
+- colorkey video filter
version 2.6:
diff --git a/MAINTAINERS b/MAINTAINERS
index 598c0b3..18f6ba3 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -352,6 +352,7 @@ Filters:
avf_showcqt.c Muhammad Faiz
vf_blend.c Paul B Mahol
vf_colorbalance.c Paul B Mahol
+ vf_colorkey.c Timo Rothenpieler
vf_dejudder.c Nicholas Robbins
vf_delogo.c Jean Delvare (CC <khali at linux-fr.org>)
vf_drawbox.c/drawgrid Andrey Utkin
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 48cee50..60e6b52 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -102,6 +102,7 @@ OBJS-$(CONFIG_BOXBLUR_FILTER) += vf_boxblur.o
OBJS-$(CONFIG_CODECVIEW_FILTER) += vf_codecview.o
OBJS-$(CONFIG_COLORBALANCE_FILTER) += vf_colorbalance.o
OBJS-$(CONFIG_COLORCHANNELMIXER_FILTER) += vf_colorchannelmixer.o
+OBJS-$(CONFIG_COLORKEY_FILTER) += vf_colorkey.o
OBJS-$(CONFIG_COLORLEVELS_FILTER) += vf_colorlevels.o
OBJS-$(CONFIG_COLORMATRIX_FILTER) += vf_colormatrix.o
OBJS-$(CONFIG_COPY_FILTER) += vf_copy.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index 7961dca..2eeec7f 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -118,6 +118,7 @@ void avfilter_register_all(void)
REGISTER_FILTER(CODECVIEW, codecview, vf);
REGISTER_FILTER(COLORBALANCE, colorbalance, vf);
REGISTER_FILTER(COLORCHANNELMIXER, colorchannelmixer, vf);
+ REGISTER_FILTER(COLORKEY, colorkey, vf);
REGISTER_FILTER(COLORLEVELS, colorlevels, vf);
REGISTER_FILTER(COLORMATRIX, colormatrix, vf);
REGISTER_FILTER(COPY, copy, vf);
diff --git a/libavfilter/vf_colorkey.c b/libavfilter/vf_colorkey.c
new file mode 100644
index 0000000..b5180cf
--- /dev/null
+++ b/libavfilter/vf_colorkey.c
@@ -0,0 +1,226 @@
+/*
+ * Copyright (c) 2015 Timo Rothenpieler <timo at rothenpieler.org>
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "libavutil/opt.h"
+#include "avfilter.h"
+#include "formats.h"
+#include "internal.h"
+#include "video.h"
+
+typedef struct ColorkeyContext {
+ const AVClass *class;
+
+ uint8_t colorkey_rgba[4];
+ float cf[3];
+
+ float similarity;
+ float blend;
+} ColorkeyContext;
+
+static inline int convert_format(int fmt)
+{
+ switch(fmt) {
+ case AV_PIX_FMT_0RGB:
+ return AV_PIX_FMT_ARGB;
+ case AV_PIX_FMT_0BGR:
+ return AV_PIX_FMT_ABGR;
+ case AV_PIX_FMT_BGR0:
+ return AV_PIX_FMT_BGRA;
+ case AV_PIX_FMT_RGB0:
+ return AV_PIX_FMT_RGBA;
+ default:
+ return fmt;
+ }
+}
+
+static inline int offset_r(int fmt)
+{
+ switch(fmt) {
+ case AV_PIX_FMT_ARGB:
+ return 1;
+ case AV_PIX_FMT_ABGR:
+ return 3;
+ case AV_PIX_FMT_BGRA:
+ return 2;
+ case AV_PIX_FMT_RGBA:
+ default:
+ return 0;
+ }
+}
+
+static inline int offset_g(int fmt)
+{
+ switch(fmt) {
+ case AV_PIX_FMT_ARGB:
+ return 2;
+ case AV_PIX_FMT_ABGR:
+ return 2;
+ case AV_PIX_FMT_BGRA:
+ return 1;
+ case AV_PIX_FMT_RGBA:
+ default:
+ return 1;
+ }
+}
+
+static inline int offset_b(int fmt)
+{
+ switch(fmt) {
+ case AV_PIX_FMT_ARGB:
+ return 3;
+ case AV_PIX_FMT_ABGR:
+ return 1;
+ case AV_PIX_FMT_BGRA:
+ return 0;
+ case AV_PIX_FMT_RGBA:
+ default:
+ return 2;
+ }
+}
+
+static inline int offset_a(int fmt)
+{
+ switch(fmt) {
+ case AV_PIX_FMT_ARGB:
+ return 0;
+ case AV_PIX_FMT_ABGR:
+ return 0;
+ case AV_PIX_FMT_BGRA:
+ return 3;
+ case AV_PIX_FMT_RGBA:
+ default:
+ return 3;
+ }
+}
+
+#define SATURATE(val) (FFMAX(FFMIN((val), 1.0), 0.0))
+
+static uint8_t do_colorkey_pixel(ColorkeyContext *ctx, uint8_t r, uint8_t g, uint8_t b)
+{
+ float diff_r = r / 255.0 - ctx->cf[0];
+ float diff_g = g / 255.0 - ctx->cf[1];
+ float diff_b = b / 255.0 - ctx->cf[2];
+
+ float diff = sqrt(diff_r * diff_r + diff_g * diff_g + diff_b * diff_b);
+
+ return SATURATE(FFMAX(diff - ctx->similarity, 0.0) / ctx->blend) * 255.0;
+}
+
+static void do_colorkey(ColorkeyContext *ctx, AVFrame *frame)
+{
+ int o_r, o_g, o_b, o_a;
+ int o, x, y;
+
+ frame->format = convert_format(frame->format);
+
+ o_r = offset_r(frame->format);
+ o_g = offset_g(frame->format);
+ o_b = offset_b(frame->format);
+ o_a = offset_a(frame->format);
+
+ for (x = 0; x < 3; ++x)
+ ctx->cf[x] = ctx->colorkey_rgba[x] / 255.0;
+
+ for (y = 0; y < frame->height; ++y) {
+ for (x = 0; x < frame->width; ++x) {
+ o = frame->linesize[0] * y + x * 4;
+
+ frame->data[0][o + o_a] =
+ do_colorkey_pixel(ctx,
+ frame->data[0][o + o_r],
+ frame->data[0][o + o_g],
+ frame->data[0][o + o_b]);
+ }
+ }
+}
+
+static AVFrame *get_video_buffer(AVFilterLink *link, int w, int h)
+{
+ AVFrame *picref = ff_default_get_video_buffer(link, w, h);
+ do_colorkey(link->dst->priv, picref);
+ return picref;
+}
+
+static int filter_frame(AVFilterLink *link, AVFrame *inpicref)
+{
+ do_colorkey(link->dst->priv, inpicref);
+ return ff_filter_frame(link->dst->outputs[0], inpicref);
+}
+
+static int query_formats(AVFilterContext *ctx)
+{
+ static const enum AVPixelFormat pixel_fmts_eq[] = {
+ AV_PIX_FMT_0RGB,
+ AV_PIX_FMT_ARGB,
+ AV_PIX_FMT_RGB0,
+ AV_PIX_FMT_RGBA,
+ AV_PIX_FMT_0BGR,
+ AV_PIX_FMT_ABGR,
+ AV_PIX_FMT_BGR0,
+ AV_PIX_FMT_BGRA,
+ AV_PIX_FMT_NONE
+ };
+
+ AVFilterFormats *fmts_list = ff_make_format_list(pixel_fmts_eq);
+ if (!fmts_list)
+ return AVERROR(ENOMEM);
+
+ return ff_set_common_formats(ctx, fmts_list);
+}
+
+static const AVFilterPad colorkey_inputs[] = {
+ {
+ .name = "default",
+ .type = AVMEDIA_TYPE_VIDEO,
+ .get_video_buffer = get_video_buffer,
+ .filter_frame = filter_frame,
+ },
+ { NULL }
+};
+
+static const AVFilterPad colorkey_outputs[] = {
+ {
+ .name = "default",
+ .type = AVMEDIA_TYPE_VIDEO,
+ },
+ { NULL }
+};
+
+#define OFFSET(x) offsetof(ColorkeyContext, x)
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
+
+static const AVOption colorkey_options[] = {
+ { "color", "set the colorkey key color", OFFSET(colorkey_rgba), AV_OPT_TYPE_COLOR, { .str = "black" }, CHAR_MIN, CHAR_MAX, FLAGS },
+ { "similarity", "set the colorkey similarity value", OFFSET(similarity), AV_OPT_TYPE_FLOAT, { .dbl = 0.01 }, 0.01, 1.0, FLAGS },
+ { "blend", "set the colorkey key blend value", OFFSET(blend), AV_OPT_TYPE_FLOAT, { .dbl = 0.0 }, 0.0, 1.0, FLAGS },
+ { NULL }
+};
+
+AVFILTER_DEFINE_CLASS(colorkey);
+
+AVFilter ff_vf_colorkey = {
+ .name = "colorkey",
+ .description = NULL_IF_CONFIG_SMALL("colorkey filter"),
+ .priv_size = sizeof(ColorkeyContext),
+ .priv_class = &colorkey_class,
+ .query_formats = query_formats,
+ .inputs = colorkey_inputs,
+ .outputs = colorkey_outputs,
+};
--
2.3.5
More information about the ffmpeg-devel
mailing list