[FFmpeg-devel] [PATCH] colorkeymask filter

Paul B Mahol onemda at gmail.com
Wed May 8 16:21:54 CEST 2013


Signed-off-by: Paul B Mahol <onemda at gmail.com>
---
 libavfilter/Makefile          |   1 +
 libavfilter/allfilters.c      |   1 +
 libavfilter/vf_colorkeymask.c | 161 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 163 insertions(+)
 create mode 100644 libavfilter/vf_colorkeymask.c

diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index f0e703e..04f9646 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -110,6 +110,7 @@ OBJS-$(CONFIG_BLEND_FILTER)                  += vf_blend.o
 OBJS-$(CONFIG_BOXBLUR_FILTER)                += vf_boxblur.o
 OBJS-$(CONFIG_COLORBALANCE_FILTER)           += vf_colorbalance.o
 OBJS-$(CONFIG_COLORCHANNELMIXER_FILTER)      += vf_colorchannelmixer.o
+OBJS-$(CONFIG_COLORKEYMASK_FILTER)           += vf_colorkeymask.o
 OBJS-$(CONFIG_COLORMATRIX_FILTER)            += vf_colormatrix.o
 OBJS-$(CONFIG_COPY_FILTER)                   += vf_copy.o
 OBJS-$(CONFIG_CROP_FILTER)                   += vf_crop.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index b8f273d..8d6e2fa 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -108,6 +108,7 @@ void avfilter_register_all(void)
     REGISTER_FILTER(BOXBLUR,        boxblur,        vf);
     REGISTER_FILTER(COLORBALANCE,   colorbalance,   vf);
     REGISTER_FILTER(COLORCHANNELMIXER, colorchannelmixer, vf);
+    REGISTER_FILTER(COLORKEYMASK,   colorkeymask,   vf);
     REGISTER_FILTER(COLORMATRIX,    colormatrix,    vf);
     REGISTER_FILTER(COPY,           copy,           vf);
     REGISTER_FILTER(CROP,           crop,           vf);
diff --git a/libavfilter/vf_colorkeymask.c b/libavfilter/vf_colorkeymask.c
new file mode 100644
index 0000000..7efec0f
--- /dev/null
+++ b/libavfilter/vf_colorkeymask.c
@@ -0,0 +1,161 @@
+/*
+ * Copyright (c) 2013 Paul B Mahol
+ *
+ * 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 "libavutil/parseutils.h"
+#include "avfilter.h"
+#include "drawutils.h"
+#include "formats.h"
+#include "internal.h"
+#include "video.h"
+
+#define R 0
+#define G 1
+#define B 2
+#define A 3
+
+typedef struct {
+    const AVClass *class;
+    char *color_str;
+    int rt, gt, bt;
+    uint8_t lut[3][256];
+    uint8_t rgba_map[4];
+} ColorKeyMaskContext;
+
+#define OFFSET(x) offsetof(ColorKeyMaskContext, x)
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
+static const AVOption colorkeymask_options[] = {
+    { "color", "set color",   OFFSET(color_str), AV_OPT_TYPE_STRING, {.str="black"}, 0, 0, FLAGS },
+    { "r", "set red tolerance",   OFFSET(rt), AV_OPT_TYPE_INT, {.i64=10}, 0, 255, FLAGS },
+    { "g", "set green tolerance", OFFSET(gt), AV_OPT_TYPE_INT, {.i64=10}, 0, 255, FLAGS },
+    { "b", "set blue tolerance",  OFFSET(bt), AV_OPT_TYPE_INT, {.i64=10}, 0, 255, FLAGS },
+    { NULL }
+};
+
+AVFILTER_DEFINE_CLASS(colorkeymask);
+
+static av_cold int init(AVFilterContext *ctx)
+{
+    ColorKeyMaskContext *s = ctx->priv;
+    uint8_t rmin, gmin, bmin;
+    uint8_t rmax, gmax, bmax;
+    uint8_t rgba[4];
+    int i, ret;
+
+    ret = av_parse_color(rgba, s->color_str, -1, ctx);
+    if (ret < 0)
+        return ret;
+
+    rmin = av_clip_uint8(rgba[R] - s->rt);
+    gmin = av_clip_uint8(rgba[G] - s->gt);
+    bmin = av_clip_uint8(rgba[B] - s->bt);
+    rmax = av_clip_uint8(rgba[R] + s->rt);
+    gmax = av_clip_uint8(rgba[G] + s->gt);
+    bmax = av_clip_uint8(rgba[B] + s->bt);
+
+    for (i = 0; i < 256; i++) {
+        if (i >= rmin && i <= rmax)
+            s->lut[R][i] = 1;
+        if (i >= gmin && i <= gmax)
+            s->lut[G][i] = 1;
+        if (i >= bmin && i <= bmax)
+            s->lut[B][i] = 1;
+    }
+
+    return 0;
+}
+
+static int query_formats(AVFilterContext *ctx)
+{
+    static const enum AVPixelFormat pix_fmts[] = {
+        AV_PIX_FMT_RGBA,  AV_PIX_FMT_BGRA,
+        AV_PIX_FMT_ABGR,  AV_PIX_FMT_ARGB,
+        AV_PIX_FMT_NONE
+    };
+
+    ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
+    return 0;
+}
+
+static int config_input(AVFilterLink *inlink)
+{
+    ColorKeyMaskContext *s = inlink->dst->priv;
+    ff_fill_rgba_map(s->rgba_map, inlink->format);
+    return 0;
+}
+
+static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
+{
+    AVFilterContext *ctx = inlink->dst;
+    ColorKeyMaskContext *s = ctx->priv;
+    AVFilterLink *outlink = ctx->outputs[0];
+    const uint8_t r = s->rgba_map[R];
+    const uint8_t g = s->rgba_map[G];
+    const uint8_t b = s->rgba_map[B];
+    const uint8_t a = s->rgba_map[A];
+    const int bytewidth = inlink->w * 4;
+    uint8_t *dst;
+    int y, x;
+
+    dst = frame->data[0];
+
+    for (y = 0; y < inlink->h; y++) {
+        for (x = 0; x < bytewidth; x += 4) {
+            if (s->lut[R][dst[x + r]] &&
+                s->lut[G][dst[x + g]] &&
+                s->lut[B][dst[x + b]])
+                dst[x + a] = 0;
+        }
+        dst += frame->linesize[0];
+    }
+
+    return ff_filter_frame(outlink, frame);
+}
+
+static const AVFilterPad colorkeymask_inputs[] = {
+    {
+        .name           = "default",
+        .type           = AVMEDIA_TYPE_VIDEO,
+        .filter_frame   = filter_frame,
+        .config_props   = config_input,
+        .needs_writable = 1,
+    },
+    { NULL }
+};
+
+static const AVFilterPad colorkeymask_outputs[] = {
+    {
+        .name           = "default",
+        .type           = AVMEDIA_TYPE_VIDEO,
+    },
+    { NULL }
+};
+
+AVFilter avfilter_vf_colorkeymask = {
+    .name          = "colorkeymask",
+    .description   = NULL_IF_CONFIG_SMALL("Clear alpha channel by comparing non-alpha channels."),
+    .priv_size     = sizeof(ColorKeyMaskContext),
+    .priv_class    = &colorkeymask_class,
+    .init          = init,
+    .query_formats = query_formats,
+    .inputs        = colorkeymask_inputs,
+    .outputs       = colorkeymask_outputs,
+    .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE,
+};
-- 
1.7.11.2



More information about the ffmpeg-devel mailing list