[FFmpeg-devel] [PATCHv2] avfilter/vf_chromakey: Add chromakey video filter

Timo Rothenpieler timo at rothenpieler.org
Fri Sep 18 15:02:59 CEST 2015


---
 Changelog                  |   1 +
 MAINTAINERS                |   1 +
 doc/filters.texi           |  45 ++++++++++
 libavfilter/Makefile       |   1 +
 libavfilter/allfilters.c   |   1 +
 libavfilter/version.h      |   2 +-
 libavfilter/vf_chromakey.c | 199 +++++++++++++++++++++++++++++++++++++++++++++
 7 files changed, 249 insertions(+), 1 deletion(-)
 create mode 100644 libavfilter/vf_chromakey.c

diff --git a/Changelog b/Changelog
index 62d3a7a..461d0d6 100644
--- a/Changelog
+++ b/Changelog
@@ -8,6 +8,7 @@ version <next>:
 - alimiter filter
 - stereowiden filter
 - stereotools filter
+- chromakey filter
 
 
 version 2.8:
diff --git a/MAINTAINERS b/MAINTAINERS
index 4a50430..12da0cd 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -359,6 +359,7 @@ Filters:
   avf_avectorscope.c                    Paul B Mahol
   avf_showcqt.c                         Muhammad Faiz
   vf_blend.c                            Paul B Mahol
+  vf_chromakey.c                        Timo Rothenpieler
   vf_colorchannelmixer.c                Paul B Mahol
   vf_colorbalance.c                     Paul B Mahol
   vf_colorkey.c                         Timo Rothenpieler
diff --git a/doc/filters.texi b/doc/filters.texi
index 4a55e59..0446204 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -3556,6 +3556,51 @@ boxblur=luma_radius=min(h\,w)/10:luma_power=1:chroma_radius=min(cw\,ch)/10:chrom
 @end example
 @end itemize
 
+ at section chromakey
+YUV colorspace color/chroma keying.
+
+The filter accepts the following options:
+
+ at table @option
+ at item color
+The color which will be replaced with transparency.
+
+ at item similarity
+Similarity percentage with the key color.
+
+0.01 matches only the exact key color, while 1.0 matches everything.
+
+ at item blend
+Blend percentage.
+
+0.0 makes pixels either fully transparent, or not transparent at all.
+
+Higher values result in semi-transparent pixels, with a higher transparency
+the more similar the pixels color is to the key color.
+
+ at item yuv
+Signals that the color passed is already in YUV instead of RGB.
+
+Litteral colors like "green" or "red" don't make sense with this enabled anymore.
+This can be used to pass exact YUV values as hexadecimal numbers.
+ at end table
+
+ at subsection Examples
+
+ at itemize
+ at item
+Make every green pixel in the input image transparent:
+ at example
+ffmpeg -i input.png -vf chromakey=green out.png
+ at end example
+
+ at item
+Overlay a greenscreen-video on top of a static black background.
+ at example
+ffmpeg -f lavfi -i color=c=black:s=1280x720 -i video.mp4 -shortest -filter_complex "[1:v]chromakey=0x70de77:0.1:0.2[ckout];[0:v][ckout]overlay[out]" -map "[out]" output.mkv
+ at end example
+ at end itemize
+
 @section codecview
 
 Visualize information exported by some codecs.
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 05effd6..2aa8603 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -102,6 +102,7 @@ OBJS-$(CONFIG_BLACKDETECT_FILTER)            += vf_blackdetect.o
 OBJS-$(CONFIG_BLACKFRAME_FILTER)             += vf_blackframe.o
 OBJS-$(CONFIG_BLEND_FILTER)                  += vf_blend.o dualinput.o framesync.o
 OBJS-$(CONFIG_BOXBLUR_FILTER)                += vf_boxblur.o
+OBJS-$(CONFIG_CHROMAKEY_FILTER)              += vf_chromakey.o
 OBJS-$(CONFIG_CODECVIEW_FILTER)              += vf_codecview.o
 OBJS-$(CONFIG_COLORBALANCE_FILTER)           += vf_colorbalance.o
 OBJS-$(CONFIG_COLORCHANNELMIXER_FILTER)      += vf_colorchannelmixer.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index cab4564..5075fca 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -124,6 +124,7 @@ void avfilter_register_all(void)
     REGISTER_FILTER(BLACKFRAME,     blackframe,     vf);
     REGISTER_FILTER(BLEND,          blend,          vf);
     REGISTER_FILTER(BOXBLUR,        boxblur,        vf);
+    REGISTER_FILTER(CHROMAKEY,      chromakey,      vf);
     REGISTER_FILTER(CODECVIEW,      codecview,      vf);
     REGISTER_FILTER(COLORBALANCE,   colorbalance,   vf);
     REGISTER_FILTER(COLORCHANNELMIXER, colorchannelmixer, vf);
diff --git a/libavfilter/version.h b/libavfilter/version.h
index e918184..9d44fd0 100644
--- a/libavfilter/version.h
+++ b/libavfilter/version.h
@@ -30,7 +30,7 @@
 #include "libavutil/version.h"
 
 #define LIBAVFILTER_VERSION_MAJOR   6
-#define LIBAVFILTER_VERSION_MINOR   5
+#define LIBAVFILTER_VERSION_MINOR   6
 #define LIBAVFILTER_VERSION_MICRO 100
 
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \
diff --git a/libavfilter/vf_chromakey.c b/libavfilter/vf_chromakey.c
new file mode 100644
index 0000000..3828ca9
--- /dev/null
+++ b/libavfilter/vf_chromakey.c
@@ -0,0 +1,199 @@
+/*
+ * 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 "libavutil/imgutils.h"
+#include "avfilter.h"
+#include "formats.h"
+#include "internal.h"
+#include "video.h"
+
+typedef struct ChromakeyContext {
+    const AVClass *class;
+
+    uint8_t chromakey_rgba[4];
+    uint8_t chromakey_uv[2];
+
+    float similarity;
+    float blend;
+
+    int is_yuv;
+} ChromakeyContext;
+
+static uint8_t do_chromakey_pixel(ChromakeyContext *ctx, uint8_t u[9], uint8_t v[9])
+{
+    double diff = 0.0;
+    int du, dv, i;
+
+    for (i = 0; i < 9; ++i) {
+        du = (int)u[i] - ctx->chromakey_uv[0];
+        dv = (int)v[i] - ctx->chromakey_uv[1];
+
+        diff += sqrt((du * du + dv * dv) / (255.0 * 255.0));
+    }
+
+    diff /= 9.0;
+
+    if (ctx->blend > 0.0001) {
+        return av_clipd((diff - ctx->similarity) / ctx->blend, 0.0, 1.0) * 255.0;
+    } else {
+        return (diff > ctx->similarity) ? 255 : 0;
+    }
+}
+
+static void get_pixel_uv(AVFrame *frame, int x, int y, uint8_t *u, uint8_t *v)
+{
+    if (x < 0 || x >= frame->width || y < 0 || y >= frame->height) {
+        *u = 0;
+        *v = 0;
+        return;
+    }
+
+    if (frame->format == AV_PIX_FMT_YUVA420P || frame->format == AV_PIX_FMT_YUVA422P) {
+        x /= 2;
+    }
+
+    if (frame->format == AV_PIX_FMT_YUVA420P) {
+        y /= 2;
+    }
+
+    *u = frame->data[1][frame->linesize[1] * y + x];
+    *v = frame->data[2][frame->linesize[2] * y + x];
+}
+
+static int do_chromakey_slice(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
+{
+    AVFrame *frame = arg;
+
+    const int slice_start = (frame->height * jobnr) / nb_jobs;
+    const int slice_end = (frame->height * (jobnr + 1)) / nb_jobs;
+
+    ChromakeyContext *ctx = avctx->priv;
+
+    int x, y, xo, yo;
+    uint8_t u[9], v[9];
+
+    for (y = slice_start; y < slice_end; ++y) {
+        for (x = 0; x < frame->width; ++x) {
+            for (yo = 0; yo < 3; ++yo) {
+                for (xo = 0; xo < 3; ++xo) {
+                    get_pixel_uv(frame, x + xo - 1, y + yo - 1, &u[yo * 3 + xo], &v[yo * 3 + xo]);
+                }
+            }
+
+            frame->data[3][frame->linesize[3] * y + x] = do_chromakey_pixel(ctx, u, v);
+        }
+    }
+
+    return 0;
+}
+
+static int filter_frame(AVFilterLink *link, AVFrame *frame)
+{
+    AVFilterContext *avctx = link->dst;
+    int res;
+
+    if (res = av_frame_make_writable(frame))
+        return res;
+
+    if (res = avctx->internal->execute(avctx, do_chromakey_slice, frame, NULL, FFMIN(frame->height, avctx->graph->nb_threads)))
+        return res;
+
+    return ff_filter_frame(avctx->outputs[0], frame);
+}
+
+#define FIXNUM(x) ((int) ((x) * (1 << 10) + 0.5))
+#define RGB_TO_U(rgb) (((- FIXNUM(0.16874) * rgb[0] - FIXNUM(0.33126) * rgb[1] + FIXNUM(0.50000) * rgb[2] + (1 << 9) - 1) >> 10) + 128)
+#define RGB_TO_V(rgb) (((  FIXNUM(0.50000) * rgb[0] - FIXNUM(0.41869) * rgb[1] - FIXNUM(0.08131) * rgb[2] + (1 << 9) - 1) >> 10) + 128)
+
+static av_cold int initialize_chromakey(AVFilterContext *avctx)
+{
+    ChromakeyContext *ctx = avctx->priv;
+
+    if (ctx->is_yuv) {
+        ctx->chromakey_uv[0] = ctx->chromakey_rgba[1];
+        ctx->chromakey_uv[1] = ctx->chromakey_rgba[2];
+    } else {
+        ctx->chromakey_uv[0] = RGB_TO_U(ctx->chromakey_rgba);
+        ctx->chromakey_uv[1] = RGB_TO_V(ctx->chromakey_rgba);
+    }
+
+    return 0;
+}
+
+static av_cold int query_formats(AVFilterContext *avctx)
+{
+    static const enum AVPixelFormat pixel_fmts[] = {
+        AV_PIX_FMT_YUVA420P,
+        AV_PIX_FMT_YUVA422P,
+        AV_PIX_FMT_YUVA444P,
+        AV_PIX_FMT_NONE
+    };
+
+    AVFilterFormats *formats = NULL;
+
+    formats = ff_make_format_list(pixel_fmts);
+    if (!formats)
+        return AVERROR(ENOMEM);
+
+    return ff_set_common_formats(avctx, formats);
+}
+
+static const AVFilterPad chromakey_inputs[] = {
+    {
+        .name         = "default",
+        .type         = AVMEDIA_TYPE_VIDEO,
+        .filter_frame = filter_frame,
+    },
+    { NULL }
+};
+
+static const AVFilterPad chromakey_outputs[] = {
+    {
+        .name         = "default",
+        .type         = AVMEDIA_TYPE_VIDEO,
+    },
+    { NULL }
+};
+
+#define OFFSET(x) offsetof(ChromakeyContext, x)
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
+
+static const AVOption chromakey_options[] = {
+    { "color", "set the chromakey key color", OFFSET(chromakey_rgba), AV_OPT_TYPE_COLOR, { .str = "black" }, CHAR_MIN, CHAR_MAX, FLAGS },
+    { "similarity", "set the chromakey similarity value", OFFSET(similarity), AV_OPT_TYPE_FLOAT, { .dbl = 0.01 }, 0.01, 1.0, FLAGS },
+    { "blend", "set the chromakey key blend value", OFFSET(blend), AV_OPT_TYPE_FLOAT, { .dbl = 0.0 }, 0.0, 1.0, FLAGS },
+    { "yuv", "color parameter is in yuv instead of rgb", OFFSET(is_yuv), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
+    { NULL }
+};
+
+AVFILTER_DEFINE_CLASS(chromakey);
+
+AVFilter ff_vf_chromakey = {
+    .name          = "chromakey",
+    .description   = NULL_IF_CONFIG_SMALL("Turns a certain color into transparency. Operates on YUV colors."),
+    .priv_size     = sizeof(ChromakeyContext),
+    .priv_class    = &chromakey_class,
+    .init          = initialize_chromakey,
+    .query_formats = query_formats,
+    .inputs        = chromakey_inputs,
+    .outputs       = chromakey_outputs,
+    .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
+};
-- 
2.5.0



More information about the ffmpeg-devel mailing list