[FFmpeg-devel] [PATCH] lavfi: add erosion & dilation filter

Paul B Mahol onemda at gmail.com
Wed Feb 25 15:55:58 CET 2015


Signed-off-by: Paul B Mahol <onemda at gmail.com>
---
 doc/filters.texi          |  34 ++++++
 libavfilter/Makefile      |   2 +
 libavfilter/allfilters.c  |   2 +
 libavfilter/vf_neighbor.c | 289 ++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 327 insertions(+)
 create mode 100644 libavfilter/vf_neighbor.c

diff --git a/doc/filters.texi b/doc/filters.texi
index baef346..13ba797 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -3728,6 +3728,23 @@ FFmpeg was configured with @code{--enable-opencl}. Default value is 0.
 
 @end table
 
+ at section dilation
+
+Apply dilation effect to the video.
+
+This filter replaces the pixel by the local(3x3) maximum.
+
+It accepts the following parameters:
+
+ at table @option
+ at item threshold
+Allows to limit the maximum change, default is 65535.
+
+ at item coordinates
+Flag which specifies the pixel to refer to. Default is 255 ie. all eight
+pixels are used.
+ at end table
+
 @section drawbox
 
 Draw a colored box on the input image.
@@ -4437,6 +4454,23 @@ value.
 
 @end table
 
+ at section erosion
+
+Apply erosion effect to the video.
+
+This filter replaces the pixel by the local(3x3) minimum.
+
+It accepts the following parameters:
+
+ at table @option
+ at item threshold
+Allows to limit the maximum change, default is 65535.
+
+ at item coordinates
+Flag which specifies the pixel to refer to. Default is 255 ie. all eight
+pixels are used.
+ at end table
+
 @section extractplanes
 
 Extract color channel components from input video stream into
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 289c63b..97a38a8 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -112,12 +112,14 @@ OBJS-$(CONFIG_DECIMATE_FILTER)               += vf_decimate.o
 OBJS-$(CONFIG_DEJUDDER_FILTER)               += vf_dejudder.o
 OBJS-$(CONFIG_DELOGO_FILTER)                 += vf_delogo.o
 OBJS-$(CONFIG_DESHAKE_FILTER)                += vf_deshake.o
+OBJS-$(CONFIG_DILATION_FILTER)               += vf_neighbor.o
 OBJS-$(CONFIG_DRAWBOX_FILTER)                += vf_drawbox.o
 OBJS-$(CONFIG_DRAWGRID_FILTER)               += vf_drawbox.o
 OBJS-$(CONFIG_DRAWTEXT_FILTER)               += vf_drawtext.o
 OBJS-$(CONFIG_ELBG_FILTER)                   += vf_elbg.o
 OBJS-$(CONFIG_EDGEDETECT_FILTER)             += vf_edgedetect.o
 OBJS-$(CONFIG_EQ_FILTER)                     += vf_eq.o
+OBJS-$(CONFIG_EROSION_FILTER)                += vf_neighbor.o
 OBJS-$(CONFIG_EXTRACTPLANES_FILTER)          += vf_extractplanes.o
 OBJS-$(CONFIG_FADE_FILTER)                   += vf_fade.o
 OBJS-$(CONFIG_FIELD_FILTER)                  += vf_field.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index 55de154..060744a 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -128,12 +128,14 @@ void avfilter_register_all(void)
     REGISTER_FILTER(DEJUDDER,       dejudder,       vf);
     REGISTER_FILTER(DELOGO,         delogo,         vf);
     REGISTER_FILTER(DESHAKE,        deshake,        vf);
+    REGISTER_FILTER(DILATION,       dilation,       vf);
     REGISTER_FILTER(DRAWBOX,        drawbox,        vf);
     REGISTER_FILTER(DRAWGRID,       drawgrid,       vf);
     REGISTER_FILTER(DRAWTEXT,       drawtext,       vf);
     REGISTER_FILTER(EDGEDETECT,     edgedetect,     vf);
     REGISTER_FILTER(ELBG,           elbg,           vf);
     REGISTER_FILTER(EQ,             eq,             vf);
+    REGISTER_FILTER(EROSION,        erosion,        vf);
     REGISTER_FILTER(EXTRACTPLANES,  extractplanes,  vf);
     REGISTER_FILTER(FADE,           fade,           vf);
     REGISTER_FILTER(FIELD,          field,          vf);
diff --git a/libavfilter/vf_neighbor.c b/libavfilter/vf_neighbor.c
new file mode 100644
index 0000000..1a923dd
--- /dev/null
+++ b/libavfilter/vf_neighbor.c
@@ -0,0 +1,289 @@
+/*
+ * Copyright (c) 2012-2013 Oka Motofumi (chikuzen.mo at gmail dot com)
+ * Copyright (c) 2015 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/imgutils.h"
+#include "libavutil/pixdesc.h"
+#include "libavutil/opt.h"
+#include "avfilter.h"
+#include "formats.h"
+#include "internal.h"
+#include "video.h"
+
+typedef struct EDContext {
+    const AVClass *class;
+    int planeheight[4];
+    int linesize[4];
+    int nb_planes;
+    int threshold;
+    int coordinates;
+    uint8_t *buffer;
+} EDContext;
+
+#define OFFSET(x) offsetof(EDContext, x)
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
+
+static const AVOption erosion_options[] = {
+    { "threshold",   "set threshold",   OFFSET(threshold),   AV_OPT_TYPE_INT, {.i64=65535}, 0, 65535, FLAGS },
+    { "coordinates", "set coordinates", OFFSET(coordinates), AV_OPT_TYPE_INT, {.i64=255}, 0, 255, FLAGS },
+    { NULL }
+};
+
+AVFILTER_DEFINE_CLASS(erosion);
+
+static int query_formats(AVFilterContext *ctx)
+{
+    static const enum AVPixelFormat pix_fmts[] = {
+        AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA420P,
+        AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ422P,AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ411P,
+        AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
+        AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP, AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE
+    };
+
+    ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
+    return 0;
+}
+
+static int config_input(AVFilterLink *inlink)
+{
+    EDContext *s = inlink->dst->priv;
+    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
+    int ret;
+
+    if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
+        return ret;
+
+    s->planeheight[1] = s->planeheight[2] = FF_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
+    s->planeheight[0] = s->planeheight[3] = inlink->h;
+
+    s->nb_planes = av_pix_fmt_count_planes(inlink->format);
+    s->buffer = av_malloc(3 * (s->linesize[0] + 32));
+    if (!s->buffer)
+        return AVERROR(ENOMEM);
+
+    return 0;
+}
+
+static inline void line_copy8(uint8_t *line, const uint8_t *srcp, int width, int mergin)
+{
+    memcpy(line, srcp, width);
+
+    for (int i = mergin; i > 0; i--) {
+        line[-i] = line[i];
+        line[width - 1 + i] = line[width - 1 - i];
+    }
+}
+
+static int erosion_filter_frame(AVFilterLink *inlink, AVFrame *in)
+{
+    AVFilterContext *ctx = inlink->dst;
+    AVFilterLink *outlink = ctx->outputs[0];
+    EDContext *s = ctx->priv;
+    AVFrame *out;
+    int plane, y, x, i;
+
+    out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
+    if (!out) {
+        av_frame_free(&in);
+        return AVERROR(ENOMEM);
+    }
+    av_frame_copy_props(out, in);
+
+    for (plane = 0; plane < s->nb_planes; plane++) {
+        const uint8_t *src = in->data[plane];
+        uint8_t *dst = out->data[plane];
+        int stride = in->linesize[plane];
+        int height = s->planeheight[plane];
+        int width  = s->linesize[plane];
+        uint8_t *p0 = s->buffer + 16;
+        uint8_t *p1 = p0 + s->linesize[0];
+        uint8_t *p2 = p1 + s->linesize[0];
+        uint8_t *orig = p0, *end = p2;
+
+        line_copy8(p0, src + stride, width, 1);
+        line_copy8(p1, src, width, 1);
+
+        for (y = 0; y < height; y++) {
+            uint8_t *coordinates[] = { p0 - 1, p0, p0 + 1,
+                                       p1 - 1,     p1 + 1,
+                                       p2 - 1, p2, p2 + 1};
+            src += stride * (y < height - 1 ? 1 : -1);
+            line_copy8(p2, src, width, 1);
+
+            for (x = 0; x < width; x++) {
+                int min = p1[x];
+                int limit = FFMAX(min - s->threshold, 0);
+
+                for (i = 0; i < 8; i++) {
+                    if (s->coordinates & (1 << i)) {
+                        min = FFMIN(min, *(coordinates[i] + x));
+                    }
+                    min = FFMAX(min, limit);
+                }
+
+                dst[x] = min;
+            }
+
+            p0 = p1;
+            p1 = p2;
+            p2 = (p2 == end) ? orig: p2 + s->linesize[0];
+            dst += out->linesize[plane];
+        }
+    }
+
+    av_frame_free(&in);
+    return ff_filter_frame(outlink, out);
+}
+
+static av_cold void uninit(AVFilterContext *ctx)
+{
+    EDContext *s = ctx->priv;
+
+    av_freep(&s->buffer);
+}
+
+static const AVFilterPad erosion_inputs[] = {
+    {
+        .name         = "default",
+        .type         = AVMEDIA_TYPE_VIDEO,
+        .filter_frame = erosion_filter_frame,
+        .config_props = config_input,
+    },
+    { NULL }
+};
+
+static const AVFilterPad erosion_outputs[] = {
+    {
+        .name = "default",
+        .type = AVMEDIA_TYPE_VIDEO,
+    },
+    { NULL }
+};
+
+AVFilter ff_vf_erosion = {
+    .name          = "erosion",
+    .description   = NULL_IF_CONFIG_SMALL("Apply erosion effect."),
+    .priv_size     = sizeof(EDContext),
+    .priv_class    = &erosion_class,
+    .uninit        = uninit,
+    .query_formats = query_formats,
+    .inputs        = erosion_inputs,
+    .outputs       = erosion_outputs,
+    .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
+};
+
+static const AVOption dilation_options[] = {
+    { "threshold",   "set threshold",   OFFSET(threshold),   AV_OPT_TYPE_INT, {.i64=65535}, 0, 65535, FLAGS },
+    { "coordinates", "set coordinates", OFFSET(coordinates), AV_OPT_TYPE_INT, {.i64=255}, 0, 255, FLAGS },
+    { NULL }
+};
+
+AVFILTER_DEFINE_CLASS(dilation);
+
+static int dilation_filter_frame(AVFilterLink *inlink, AVFrame *in)
+{
+    AVFilterContext *ctx = inlink->dst;
+    AVFilterLink *outlink = ctx->outputs[0];
+    EDContext *s = ctx->priv;
+    AVFrame *out;
+    int plane, y, x, i;
+
+    out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
+    if (!out) {
+        av_frame_free(&in);
+        return AVERROR(ENOMEM);
+    }
+    av_frame_copy_props(out, in);
+
+    for (plane = 0; plane < s->nb_planes; plane++) {
+        const uint8_t *src = in->data[plane];
+        uint8_t *dst = out->data[plane];
+        int stride = in->linesize[plane];
+        int height = s->planeheight[plane];
+        int width  = s->linesize[plane];
+        uint8_t *p0 = s->buffer + 16;
+        uint8_t *p1 = p0 + s->linesize[0];
+        uint8_t *p2 = p1 + s->linesize[0];
+        uint8_t *orig = p0, *end = p2;
+
+        line_copy8(p0, src + stride, width, 1);
+        line_copy8(p1, src, width, 1);
+
+        for (y = 0; y < height; y++) {
+            uint8_t *coordinates[] = { p0 - 1, p0, p0 + 1,
+                                       p1 - 1,     p1 + 1,
+                                       p2 - 1, p2, p2 + 1};
+            src += stride * (y < height - 1 ? 1 : -1);
+            line_copy8(p2, src, width, 1);
+
+            for (x = 0; x < width; x++) {
+                int max = p1[x];
+                int limit = FFMIN(max + s->threshold, 255);
+
+                for (i = 0; i < 8; i++) {
+                    if (s->coordinates & (1 << i)) {
+                        max = FFMAX(max, *(coordinates[i] + x));
+                    }
+                    max = FFMIN(max, limit);
+                }
+
+                dst[x] = max;
+            }
+
+            p0 = p1;
+            p1 = p2;
+            p2 = (p2 == end) ? orig: p2 + s->linesize[0];
+            dst += out->linesize[plane];
+        }
+    }
+
+    av_frame_free(&in);
+    return ff_filter_frame(outlink, out);
+}
+
+static const AVFilterPad dilation_inputs[] = {
+    {
+        .name         = "default",
+        .type         = AVMEDIA_TYPE_VIDEO,
+        .filter_frame = dilation_filter_frame,
+        .config_props = config_input,
+    },
+    { NULL }
+};
+
+static const AVFilterPad dilation_outputs[] = {
+    {
+        .name = "default",
+        .type = AVMEDIA_TYPE_VIDEO,
+    },
+    { NULL }
+};
+
+AVFilter ff_vf_dilation = {
+    .name          = "dilation",
+    .description   = NULL_IF_CONFIG_SMALL("Apply dilation effect."),
+    .priv_size     = sizeof(EDContext),
+    .priv_class    = &dilation_class,
+    .uninit        = uninit,
+    .query_formats = query_formats,
+    .inputs        = dilation_inputs,
+    .outputs       = dilation_outputs,
+    .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
+};
-- 
1.7.11.2



More information about the ffmpeg-devel mailing list