[FFmpeg-devel] [PATCH] avfilter: add lumakey filter

Paul B Mahol onemda at gmail.com
Sun Apr 23 21:47:27 EEST 2017


Signed-off-by: Paul B Mahol <onemda at gmail.com>
---
 doc/filters.texi         |  16 +++++
 libavfilter/Makefile     |   1 +
 libavfilter/allfilters.c |   1 +
 libavfilter/vf_lumakey.c | 174 +++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 192 insertions(+)
 create mode 100644 libavfilter/vf_lumakey.c

diff --git a/doc/filters.texi b/doc/filters.texi
index 4c733f0..d7ac4cf 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -9278,6 +9278,22 @@ Interpolate values using a tetrahedron.
 @end table
 @end table
 
+ at section lumakey
+
+Turn certain luma values into transparency.
+
+The filter accepts the following options:
+
+ at table @option
+ at item threshold
+Set the luma which will be used as base for transparency.
+Default value is @code{0}.
+
+ at item tolerance
+Set the range of luma values to be keyed out.
+Default value is @code{0}.
+ at end table
+
 @section lut, lutrgb, lutyuv
 
 Compute a look-up table for binding each pixel component input value
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 50c5132..074c690 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -212,6 +212,7 @@ OBJS-$(CONFIG_INTERLEAVE_FILTER)             += f_interleave.o
 OBJS-$(CONFIG_KERNDEINT_FILTER)              += vf_kerndeint.o
 OBJS-$(CONFIG_LENSCORRECTION_FILTER)         += vf_lenscorrection.o
 OBJS-$(CONFIG_LOOP_FILTER)                   += f_loop.o
+OBJS-$(CONFIG_LUMAKEY_FILTER)                += vf_lumakey.o
 OBJS-$(CONFIG_LUT_FILTER)                    += vf_lut.o
 OBJS-$(CONFIG_LUT2_FILTER)                   += vf_lut2.o framesync.o
 OBJS-$(CONFIG_LUT3D_FILTER)                  += vf_lut3d.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index f482adb..c69f79e 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -223,6 +223,7 @@ static void register_all(void)
     REGISTER_FILTER(KERNDEINT,      kerndeint,      vf);
     REGISTER_FILTER(LENSCORRECTION, lenscorrection, vf);
     REGISTER_FILTER(LOOP,           loop,           vf);
+    REGISTER_FILTER(LUMAKEY,        lumakey,        vf);
     REGISTER_FILTER(LUT,            lut,            vf);
     REGISTER_FILTER(LUT2,           lut2,           vf);
     REGISTER_FILTER(LUT3D,          lut3d,          vf);
diff --git a/libavfilter/vf_lumakey.c b/libavfilter/vf_lumakey.c
new file mode 100644
index 0000000..4ab17a9
--- /dev/null
+++ b/libavfilter/vf_lumakey.c
@@ -0,0 +1,174 @@
+/*
+ * Copyright (c) 2017 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/imgutils.h"
+#include "avfilter.h"
+#include "formats.h"
+#include "internal.h"
+#include "video.h"
+
+typedef struct LumakeyContext {
+    const AVClass *class;
+
+    int threshold;
+    int tolerance;
+
+    int (*do_lumakey_slice)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
+} LumakeyContext;
+
+static int do_lumakey_slice8(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
+{
+    LumakeyContext *s = ctx->priv;
+    AVFrame *frame = arg;
+    const int slice_start = (frame->height * jobnr) / nb_jobs;
+    const int slice_end = (frame->height * (jobnr + 1)) / nb_jobs;
+    uint8_t *alpha = frame->data[3] + slice_start * frame->linesize[3];
+    const uint8_t *luma = frame->data[0] + slice_start * frame->linesize[0];
+    const int thr = s->threshold;
+    const int tol = s->tolerance;
+    int x, y;
+
+    for (y = slice_start; y < slice_end; ++y) {
+        for (x = 0; x < frame->width; ++x) {
+            if (luma[x] >= thr - tol && luma[x] <= thr + tol) {
+                alpha[x] = 0;
+            }
+        }
+        luma += frame->linesize[0];
+        alpha += frame->linesize[3];
+    }
+
+    return 0;
+}
+
+static int do_lumakey_slice16(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
+{
+    LumakeyContext *s = ctx->priv;
+    AVFrame *frame = arg;
+    const int slice_start = (frame->height * jobnr) / nb_jobs;
+    const int slice_end = (frame->height * (jobnr + 1)) / nb_jobs;
+    uint16_t *alpha = (uint16_t *)(frame->data[3] + slice_start * frame->linesize[3]);
+    const uint16_t *luma = (const uint16_t *)(frame->data[0] + slice_start * frame->linesize[0]);
+    const int thr = s->threshold;
+    const int tol = s->tolerance;
+    int x, y;
+
+    for (y = slice_start; y < slice_end; ++y) {
+        for (x = 0; x < frame->width; ++x) {
+            if (luma[x] >= thr - tol && luma[x] <= thr + tol) {
+                alpha[x] = 0;
+            }
+        }
+        luma += frame->linesize[0] / 2;
+        alpha += frame->linesize[3] / 2;
+    }
+
+    return 0;
+}
+
+static int config_input(AVFilterLink *inlink)
+{
+    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
+    AVFilterContext *ctx = inlink->dst;
+    LumakeyContext *s = ctx->priv;
+    int depth;
+
+    depth = desc->comp[0].depth;
+    if (depth == 8)
+        s->do_lumakey_slice = do_lumakey_slice8;
+    else
+        s->do_lumakey_slice = do_lumakey_slice16;
+
+    return 0;
+}
+
+static int filter_frame(AVFilterLink *link, AVFrame *frame)
+{
+    AVFilterContext *ctx = link->dst;
+    LumakeyContext *s = ctx->priv;
+    int ret;
+
+    if (ret = av_frame_make_writable(frame))
+        return ret;
+
+    if (ret = ctx->internal->execute(ctx, s->do_lumakey_slice, frame, NULL, FFMIN(frame->height, ff_filter_get_nb_threads(ctx))))
+        return ret;
+
+    return ff_filter_frame(ctx->outputs[0], frame);
+}
+
+static av_cold int query_formats(AVFilterContext *ctx)
+{
+    static const enum AVPixelFormat pixel_fmts[] = {
+        AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA420P,
+        AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA420P9,
+        AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA420P10,
+        AV_PIX_FMT_YUVA444P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA420P16,
+        AV_PIX_FMT_NONE
+    };
+    AVFilterFormats *formats;
+
+    formats = ff_make_format_list(pixel_fmts);
+    if (!formats)
+        return AVERROR(ENOMEM);
+
+    return ff_set_common_formats(ctx, formats);
+}
+
+static const AVFilterPad lumakey_inputs[] = {
+    {
+        .name         = "default",
+        .type         = AVMEDIA_TYPE_VIDEO,
+        .filter_frame = filter_frame,
+        .config_props = config_input,
+    },
+    { NULL }
+};
+
+static const AVFilterPad lumakey_outputs[] = {
+    {
+        .name          = "default",
+        .type          = AVMEDIA_TYPE_VIDEO,
+    },
+    { NULL }
+};
+
+#define OFFSET(x) offsetof(LumakeyContext, x)
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
+
+static const AVOption lumakey_options[] = {
+    { "threshold", "set the threshold value", OFFSET(threshold), AV_OPT_TYPE_INT, {.i64=0}, 0, UINT16_MAX, FLAGS },
+    { "tolerance", "set the tolerance value", OFFSET(tolerance), AV_OPT_TYPE_INT, {.i64=1}, 0, UINT16_MAX, FLAGS },
+    { NULL }
+};
+
+AVFILTER_DEFINE_CLASS(lumakey);
+
+AVFilter ff_vf_lumakey = {
+    .name          = "lumakey",
+    .description   = NULL_IF_CONFIG_SMALL("Turns a certain luma into transparency."),
+    .priv_size     = sizeof(LumakeyContext),
+    .priv_class    = &lumakey_class,
+    .query_formats = query_formats,
+    .inputs        = lumakey_inputs,
+    .outputs       = lumakey_outputs,
+    .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
+};
-- 
2.9.3



More information about the ffmpeg-devel mailing list