Signed-off-by: Paul B Mahol <one...@gmail.com>
---
 Changelog                 |   1 +
 doc/filters.texi          |  91 +++++++
 libavfilter/Makefile      |   5 +
 libavfilter/allfilters.c  |   5 +
 libavfilter/vf_neighbor.c | 648 ++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 750 insertions(+)
 create mode 100644 libavfilter/vf_neighbor.c

diff --git a/Changelog b/Changelog
index de08065..37cffb4 100644
--- a/Changelog
+++ b/Changelog
@@ -35,6 +35,7 @@ version <next>:
 - Fix stsd atom corruption in DNxHD QuickTimes
 - Canopus HQX decoder
 - RTP depacketization of T.140 text (RFC 4103)
+- erosion, dilation, median, deflate and inflate filters
 
 
 version 2.5:
diff --git a/doc/filters.texi b/doc/filters.texi
index 0c72145..ce5bb33 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -3569,6 +3569,24 @@ Set whether or not chroma is considered in the metric 
calculations. Default is
 @code{1}.
 @end table
 
+@section deflate
+
+Apply deflate effect to the video.
+
+This filter replaces the pixel by the local(3x3) average by taking into account
+only values lower than the pixel.
+
+It accepts the following options:
+
+@table @option
+@item threshold
+Allows to limit the maximum change, default is 65535.
+
+@item planes
+Flag which specifies which planes to filter. Default is 15 i.e. all four
+planes.
+@end table
+
 @section dejudder
 
 Remove judder produced by partially interlaced telecined content.
@@ -3728,6 +3746,27 @@ FFmpeg was configured with @code{--enable-opencl}. 
Default value is 0.
 
 @end table
 
+@section dilation
+
+Apply dilation effect to the video.
+
+This filter replaces the pixel by the local(3x3) maximum.
+
+It accepts the following options:
+
+@table @option
+@item threshold
+Allows to limit the maximum change, default is 65535.
+
+@item coordinates
+Flag which specifies the pixel to refer to. Default is 255 i.e. all eight
+pixels are used.
+
+@item planes
+Flag which specifies which planes to filter. Default is 15 i.e. all four
+planes.
+@end table
+
 @section drawbox
 
 Draw a colored box on the input image.
@@ -4437,6 +4476,27 @@ value.
 
 @end table
 
+@section erosion
+
+Apply erosion effect to the video.
+
+This filter replaces the pixel by the local(3x3) minimum.
+
+It accepts the following options:
+
+@table @option
+@item threshold
+Allows to limit the maximum change, default is 65535.
+
+@item coordinates
+Flag which specifies the pixel to refer to. Default is 255 i.e. all eight
+pixels are used.
+
+@item planes
+Flag which specifies which planes to filter. Default is 15 i.e. all four
+planes.
+@end table
+
 @section extractplanes
 
 Extract color channel components from input video stream into
@@ -5908,6 +5968,24 @@ Default value is @code{none}.
 Swap luma/chroma/alpha fields. Exchange even & odd lines. Default value is 
@code{0}.
 @end table
 
+@section inflate
+
+Apply inflate effect to the video.
+
+This filter replaces the pixel by the local(3x3) average by taking into account
+only values higher than the pixel.
+
+It accepts the following options:
+
+@table @option
+@item threshold
+Allows to limit the maximum change, default is 65535.
+
+@item planes
+Flag which specifies which planes to filter. Default is 15 i.e. all four
+planes.
+@end table
+
 @section interlace
 
 Simple interlacing filter from progressive contents. This interleaves upper (or
@@ -6214,6 +6292,19 @@ lutyuv=y='bitand(val, 128+64+32)'
 @end example
 @end itemize
 
+@section median
+
+Apply median effect to the video.
+
+This filter replaces the pixel by the local(3x3) median.
+
+This filter accepts the following options:
+@table @option
+@item planes
+Flag which specifies which planes to filter. Default is 15 i.e. all four
+planes.
+@end table
+
 @section mergeplanes
 
 Merge color channel components from several video streams.
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 289c63b..24de476 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -109,15 +109,18 @@ OBJS-$(CONFIG_CROPDETECT_FILTER)             += 
vf_cropdetect.o
 OBJS-$(CONFIG_CURVES_FILTER)                 += vf_curves.o
 OBJS-$(CONFIG_DCTDNOIZ_FILTER)               += vf_dctdnoiz.o
 OBJS-$(CONFIG_DECIMATE_FILTER)               += vf_decimate.o
+OBJS-$(CONFIG_DEFLATE_FILTER)                += vf_neighbor.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
@@ -140,6 +143,7 @@ OBJS-$(CONFIG_HQX_FILTER)                    += vf_hqx.o
 OBJS-$(CONFIG_HUE_FILTER)                    += vf_hue.o
 OBJS-$(CONFIG_IDET_FILTER)                   += vf_idet.o
 OBJS-$(CONFIG_IL_FILTER)                     += vf_il.o
+OBJS-$(CONFIG_INFLATE_FILTER)                += vf_neighbor.o
 OBJS-$(CONFIG_INTERLACE_FILTER)              += vf_interlace.o
 OBJS-$(CONFIG_INTERLEAVE_FILTER)             += f_interleave.o
 OBJS-$(CONFIG_KERNDEINT_FILTER)              += vf_kerndeint.o
@@ -149,6 +153,7 @@ OBJS-$(CONFIG_LUT_FILTER)                    += vf_lut.o
 OBJS-$(CONFIG_LUTRGB_FILTER)                 += vf_lut.o
 OBJS-$(CONFIG_LUTYUV_FILTER)                 += vf_lut.o
 OBJS-$(CONFIG_MCDEINT_FILTER)                += vf_mcdeint.o
+OBJS-$(CONFIG_MEDIAN_FILTER)                 += vf_neighbor.o
 OBJS-$(CONFIG_MERGEPLANES_FILTER)            += vf_mergeplanes.o framesync.o
 OBJS-$(CONFIG_MPDECIMATE_FILTER)             += vf_mpdecimate.o
 OBJS-$(CONFIG_NEGATE_FILTER)                 += vf_lut.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index 55de154..f64228c 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -125,15 +125,18 @@ void avfilter_register_all(void)
     REGISTER_FILTER(CURVES,         curves,         vf);
     REGISTER_FILTER(DCTDNOIZ,       dctdnoiz,       vf);
     REGISTER_FILTER(DECIMATE,       decimate,       vf);
+    REGISTER_FILTER(DEFLATE,        deflate,        vf);
     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);
@@ -156,6 +159,7 @@ void avfilter_register_all(void)
     REGISTER_FILTER(HUE,            hue,            vf);
     REGISTER_FILTER(IDET,           idet,           vf);
     REGISTER_FILTER(IL,             il,             vf);
+    REGISTER_FILTER(INFLATE,        inflate,        vf);
     REGISTER_FILTER(INTERLACE,      interlace,      vf);
     REGISTER_FILTER(INTERLEAVE,     interleave,     vf);
     REGISTER_FILTER(KERNDEINT,      kerndeint,      vf);
@@ -165,6 +169,7 @@ void avfilter_register_all(void)
     REGISTER_FILTER(LUTRGB,         lutrgb,         vf);
     REGISTER_FILTER(LUTYUV,         lutyuv,         vf);
     REGISTER_FILTER(MCDEINT,        mcdeint,        vf);
+    REGISTER_FILTER(MEDIAN,         median,         vf);
     REGISTER_FILTER(MERGEPLANES,    mergeplanes,    vf);
     REGISTER_FILTER(MPDECIMATE,     mpdecimate,     vf);
     REGISTER_FILTER(NEGATE,         negate,         vf);
diff --git a/libavfilter/vf_neighbor.c b/libavfilter/vf_neighbor.c
new file mode 100644
index 0000000..68f670a
--- /dev/null
+++ b/libavfilter/vf_neighbor.c
@@ -0,0 +1,648 @@
+/*
+ * 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 planewidth[4];
+    int nb_planes;
+    int threshold;
+    int coordinates;
+    int planes;
+    uint8_t *buffer;
+} EDContext;
+
+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->planewidth, 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->planewidth[0] + 32));
+    if (!s->buffer)
+        return AVERROR(ENOMEM);
+
+    return 0;
+}
+
+static av_cold void uninit(AVFilterContext *ctx)
+{
+    EDContext *s = ctx->priv;
+
+    av_freep(&s->buffer);
+}
+
+static inline void line_copy8(uint8_t *line, const uint8_t *srcp, int width, 
int mergin)
+{
+    int i;
+
+    memcpy(line, srcp, width);
+
+    for (i = mergin; i > 0; i--) {
+        line[-i] = line[i];
+        line[width - 1 + i] = line[width - 1 - i];
+    }
+}
+
+#define OFFSET(x) offsetof(EDContext, x)
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
+
+#if CONFIG_EROSION_FILTER
+
+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 },
+    { "planes",      "set planes",      OFFSET(planes),      AV_OPT_TYPE_INT, 
{.i64=15},    0, 255,   FLAGS },
+    { NULL }
+};
+
+AVFILTER_DEFINE_CLASS(erosion);
+
+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++) {
+        if (s->planes & (1 << 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->planewidth[plane];
+            uint8_t *p0 = s->buffer + 16;
+            uint8_t *p1 = p0 + s->planewidth[0];
+            uint8_t *p2 = p1 + s->planewidth[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->planewidth[0];
+                dst += out->linesize[plane];
+            }
+        } else {
+            av_image_copy_plane(out->data[plane], out->linesize[plane],
+                                in->data[plane], in->linesize[plane],
+                                s->planewidth[plane], s->planeheight[plane]);
+        }
+    }
+
+    av_frame_free(&in);
+    return ff_filter_frame(outlink, out);
+}
+
+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,
+};
+
+#endif /* CONFIG_EROSION_FILTER */
+
+#if CONFIG_DILATION_FILTER
+
+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 },
+    { "planes",      "set planes",      OFFSET(planes),      AV_OPT_TYPE_INT, 
{.i64=15},    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++) {
+        if (s->planes & (1 << 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->planewidth[plane];
+            uint8_t *p0 = s->buffer + 16;
+            uint8_t *p1 = p0 + s->planewidth[0];
+            uint8_t *p2 = p1 + s->planewidth[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->planewidth[0];
+                dst += out->linesize[plane];
+            }
+        } else {
+            av_image_copy_plane(out->data[plane], out->linesize[plane],
+                                in->data[plane], in->linesize[plane],
+                                s->planewidth[plane], s->planeheight[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,
+};
+
+#endif /* CONFIG_DILATION_FILTER */
+
+#if CONFIG_MEDIAN_FILTER
+
+static const AVOption median_options[] = {
+    { "planes", "set planes", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=15}, 0, 
255, FLAGS },
+    { NULL }
+};
+
+AVFILTER_DEFINE_CLASS(median);
+
+#define LOWHIGH(X, Y) {      \
+    if (X < Y)               \
+        FFSWAP(int, X, Y);   \
+}
+
+static int median_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;
+
+    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++) {
+        if (s->planes & (1 << 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->planewidth[plane];
+            uint8_t *p0 = s->buffer + 16;
+            uint8_t *p1 = p0 + s->planewidth[0];
+            uint8_t *p2 = p1 + s->planewidth[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++) {
+                src += stride * (y < height - 1 ? 1 : -1);
+                line_copy8(p2, src, width, 1);
+
+                for (x = 0; x < width; x++) {
+                    int x0, x1, x2, x3, x4, x5, x6, x7;
+                    x0 = p0[x];
+                    x1 = p0[x + 1];
+                    LOWHIGH(x0, x1);
+                    x2 = p0[x - 1];
+                    LOWHIGH(x0, x2);
+                    LOWHIGH(x1, x2);
+                    x3 = p1[x];
+                    x4 = p1[x + 1];
+                    LOWHIGH(x3, x4);
+                    x5 = p1[x - 1];
+                    LOWHIGH(x3, x5);
+                    LOWHIGH(x4, x5);
+                    x0 = FFMAX(x0, x3);
+                    x3 = p2[x];
+                    x6 = p2[x + 1];
+                    LOWHIGH(x3, x6);
+                    x7 = p2[x - 1];
+                    LOWHIGH(x3, x7);
+                    LOWHIGH(x6, x7);
+                    x0 = FFMAX(x0, x3);
+                    LOWHIGH(x4, x6);
+                    x1 = FFMAX(x1, x4);
+                    x5 = FFMIN(x5, x7);
+                    x2 = FFMIN(x2, x5);
+                    x1 = FFMIN(x1, x6);
+                    LOWHIGH(x1, x2);
+                    x0 = FFMAX(x0, x1);
+                    x0 = FFMIN(x0, x2);
+                    dst[x] = x0;
+                }
+
+                p0 = p1;
+                p1 = p2;
+                p2 = (p2 == end) ? orig: p2 + s->planewidth[0];
+                dst += out->linesize[plane];
+            }
+        } else {
+            av_image_copy_plane(out->data[plane], out->linesize[plane],
+                                in->data[plane], in->linesize[plane],
+                                s->planewidth[plane], s->planeheight[plane]);
+        }
+    }
+
+    av_frame_free(&in);
+    return ff_filter_frame(outlink, out);
+}
+
+static const AVFilterPad median_inputs[] = {
+    {
+        .name         = "default",
+        .type         = AVMEDIA_TYPE_VIDEO,
+        .filter_frame = median_filter_frame,
+        .config_props = config_input,
+    },
+    { NULL }
+};
+
+static const AVFilterPad median_outputs[] = {
+    {
+        .name = "default",
+        .type = AVMEDIA_TYPE_VIDEO,
+    },
+    { NULL }
+};
+
+AVFilter ff_vf_median = {
+    .name          = "median",
+    .description   = NULL_IF_CONFIG_SMALL("Apply median effect."),
+    .priv_size     = sizeof(EDContext),
+    .priv_class    = &median_class,
+    .uninit        = uninit,
+    .query_formats = query_formats,
+    .inputs        = median_inputs,
+    .outputs       = median_outputs,
+    .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
+};
+
+#endif /* CONFIG_MEDIAN_FILTER */
+
+#if CONFIG_DEFLATE_FILTER
+
+static const AVOption deflate_options[] = {
+    { "threshold", "set threshold", OFFSET(threshold), AV_OPT_TYPE_INT, 
{.i64=65535}, 0, 65535, FLAGS },
+    { "planes",       "set planes", OFFSET(planes),    AV_OPT_TYPE_INT, 
{.i64=15}, 0, 255, FLAGS },
+    { NULL }
+};
+
+AVFILTER_DEFINE_CLASS(deflate);
+
+static int deflate_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++) {
+        if (s->planes & (1 << 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->planewidth[plane];
+            uint8_t *p0 = s->buffer + 16;
+            uint8_t *p1 = p0 + s->planewidth[0];
+            uint8_t *p2 = p1 + s->planewidth[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++) {
+                src += stride * (y < height - 1 ? 1 : -1);
+                line_copy8(p2, src, width, 1);
+                uint8_t *coordinates[] = {p0 - 1, p0, p0 + 1,
+                                          p1 - 1,     p1 + 1,
+                                          p2 - 1, p2, p2 + 1};
+
+                for (x = 0; x < width; x++) {
+                    int sum = 0;
+                    int limit = FFMAX(p1[x] - s->threshold, 0);
+                    for (i = 0; i < 8; sum += *(coordinates[i++] + x));
+                    dst[x] = FFMAX(FFMIN(sum / 8, p1[x]), limit);
+                }
+
+                p0 = p1;
+                p1 = p2;
+                p2 = (p2 == end) ? orig: p2 + s->planewidth[0];
+                dst += out->linesize[plane];
+            }
+        } else {
+            av_image_copy_plane(out->data[plane], out->linesize[plane],
+                                in->data[plane], in->linesize[plane],
+                                s->planewidth[plane], s->planeheight[plane]);
+        }
+    }
+
+    av_frame_free(&in);
+    return ff_filter_frame(outlink, out);
+}
+
+static const AVFilterPad deflate_inputs[] = {
+    {
+        .name         = "default",
+        .type         = AVMEDIA_TYPE_VIDEO,
+        .filter_frame = deflate_filter_frame,
+        .config_props = config_input,
+    },
+    { NULL }
+};
+
+static const AVFilterPad deflate_outputs[] = {
+    {
+        .name = "default",
+        .type = AVMEDIA_TYPE_VIDEO,
+    },
+    { NULL }
+};
+
+AVFilter ff_vf_deflate = {
+    .name          = "deflate",
+    .description   = NULL_IF_CONFIG_SMALL("Apply deflate effect."),
+    .priv_size     = sizeof(EDContext),
+    .priv_class    = &deflate_class,
+    .uninit        = uninit,
+    .query_formats = query_formats,
+    .inputs        = deflate_inputs,
+    .outputs       = deflate_outputs,
+    .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
+};
+
+#endif /* CONFIG_DEFLATE_FILTER */
+
+#if CONFIG_INFLATE_FILTER
+
+static const AVOption inflate_options[] = {
+    { "threshold", "set threshold", OFFSET(threshold), AV_OPT_TYPE_INT, 
{.i64=65535}, 0, 65535, FLAGS },
+    { "planes",       "set planes", OFFSET(planes),    AV_OPT_TYPE_INT, 
{.i64=15}, 0, 255, FLAGS },
+    { NULL }
+};
+
+AVFILTER_DEFINE_CLASS(inflate);
+
+static int inflate_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++) {
+        if (s->planes & (1 << 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->planewidth[plane];
+            uint8_t *p0 = s->buffer + 16;
+            uint8_t *p1 = p0 + s->planewidth[0];
+            uint8_t *p2 = p1 + s->planewidth[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++) {
+                src += stride * (y < height - 1 ? 1 : -1);
+                line_copy8(p2, src, width, 1);
+                uint8_t *coordinates[] = {p0 - 1, p0, p0 + 1,
+                                          p1 - 1,     p1 + 1,
+                                          p2 - 1, p2, p2 + 1};
+
+                for (x = 0; x < width; x++) {
+                    int sum = 0;
+                    int limit = FFMIN(p1[x] + s->threshold, 255);
+                    for (i = 0; i < 8; sum += *(coordinates[i++] + x));
+                    dst[x] = FFMIN(FFMAX(sum / 8, p1[x]), limit);
+                }
+
+                p0 = p1;
+                p1 = p2;
+                p2 = (p2 == end) ? orig: p2 + s->planewidth[0];
+                dst += out->linesize[plane];
+            }
+        } else {
+            av_image_copy_plane(out->data[plane], out->linesize[plane],
+                                in->data[plane], in->linesize[plane],
+                                s->planewidth[plane], s->planeheight[plane]);
+        }
+    }
+
+    av_frame_free(&in);
+    return ff_filter_frame(outlink, out);
+}
+
+static const AVFilterPad inflate_inputs[] = {
+    {
+        .name         = "default",
+        .type         = AVMEDIA_TYPE_VIDEO,
+        .filter_frame = inflate_filter_frame,
+        .config_props = config_input,
+    },
+    { NULL }
+};
+
+static const AVFilterPad inflate_outputs[] = {
+    {
+        .name = "default",
+        .type = AVMEDIA_TYPE_VIDEO,
+    },
+    { NULL }
+};
+
+AVFilter ff_vf_inflate = {
+    .name          = "inflate",
+    .description   = NULL_IF_CONFIG_SMALL("Apply inflate effect."),
+    .priv_size     = sizeof(EDContext),
+    .priv_class    = &inflate_class,
+    .uninit        = uninit,
+    .query_formats = query_formats,
+    .inputs        = inflate_inputs,
+    .outputs       = inflate_outputs,
+    .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
+};
+
+#endif /* CONFIG_INFLATE_FILTER */
-- 
1.7.11.2

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel

Reply via email to