[FFmpeg-devel] [PATCH] lavfi: port boxblur filter from libmpcodecs

Stefano Sabatini stefano.sabatini-lala at poste.it
Sat Jul 9 18:41:44 CEST 2011


---
 configure                |    1 +
 doc/filters.texi         |   18 ++++
 libavfilter/Makefile     |    1 +
 libavfilter/allfilters.c |    1 +
 libavfilter/vf_boxblur.c |  241 ++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 262 insertions(+), 0 deletions(-)
 create mode 100644 libavfilter/vf_boxblur.c

diff --git a/configure b/configure
index 58b6abd..055e40e 100755
--- a/configure
+++ b/configure
@@ -1497,6 +1497,7 @@ udp_protocol_deps="network"
 
 # filters
 blackframe_filter_deps="gpl"
+boxblur_filter_deps="gpl"
 cropdetect_filter_deps="gpl"
 drawtext_filter_deps="libfreetype"
 frei0r_filter_deps="frei0r dlopen strtok_r"
diff --git a/doc/filters.texi b/doc/filters.texi
index 9fde89c..236696a 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -183,6 +183,24 @@ threshold, and defaults to 98.
 @var{threshold} is the threshold below which a pixel value is
 considered black, and defaults to 32.
 
+ at section boxblur
+
+Apply boxblur algorithm to the input video.
+
+This filter accepts the parameters:
+ at var{luma_power}:@var{luma_radius}:@var{chroma_radius}:@var{chroma_power}
+
+ at var{chroma_radius} and @var{chroma_power} are optional, if not
+specified they default to the values set to @var{luma_radius} and
+ at var{luma_power}.
+
+ at var{luma_radius} and @var{chroma_radius} represent the blur filter
+strenght (applied respectively to the luma and chroma planes), must be a
+non-negative number. If set to 0 the filter acts as a null filter.
+
+ at var{luma_power} and @var{chroma_power} represent the number of filter
+applications (applied respectively to the luma and chroma planes).
+
 @section copy
 
 Copy the input source unchanged to the output. Mainly useful for
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 3755630..645169b 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -25,6 +25,7 @@ OBJS-$(CONFIG_ANULLSRC_FILTER)               += asrc_anullsrc.o
 OBJS-$(CONFIG_ANULLSINK_FILTER)              += asink_anullsink.o
 
 OBJS-$(CONFIG_BLACKFRAME_FILTER)             += vf_blackframe.o
+OBJS-$(CONFIG_BOXBLUR_FILTER)                += vf_boxblur.o
 OBJS-$(CONFIG_COPY_FILTER)                   += vf_copy.o
 OBJS-$(CONFIG_CROP_FILTER)                   += vf_crop.o
 OBJS-$(CONFIG_CROPDETECT_FILTER)             += vf_cropdetect.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index feae239..8d80957 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -41,6 +41,7 @@ void avfilter_register_all(void)
     REGISTER_FILTER (ANULLSINK,   anullsink,   asink);
 
     REGISTER_FILTER (BLACKFRAME,  blackframe,  vf);
+    REGISTER_FILTER (BOXBLUR,     boxblur,     vf);
     REGISTER_FILTER (COPY,        copy,        vf);
     REGISTER_FILTER (CROP,        crop,        vf);
     REGISTER_FILTER (CROPDETECT,  cropdetect,  vf);
diff --git a/libavfilter/vf_boxblur.c b/libavfilter/vf_boxblur.c
new file mode 100644
index 0000000..f0cf2eb
--- /dev/null
+++ b/libavfilter/vf_boxblur.c
@@ -0,0 +1,241 @@
+/*
+ * Copyright (c) 2002 Michael Niedermayer <michaelni at gmx.at>
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU 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.
+ */
+
+/**
+ * @file
+ * Apply a boxblur filter to the input video.
+ * Ported from MPlayer libmpcodecs/vf_boxblur.c.
+ */
+
+#include "libavutil/pixdesc.h"
+#include "avfilter.h"
+
+typedef struct {
+    int radius;
+    int power;
+} FilterParam;
+
+typedef struct {
+    FilterParam luma_param;
+    FilterParam chroma_param;
+    int hsub, vsub;
+    int radius[4];
+    int power[4];
+} BoxBlurContext;
+
+static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
+{
+    BoxBlurContext *boxblur = ctx->priv;
+    int e;
+
+    if (!args) {
+        av_log(ctx, AV_LOG_ERROR,
+               "Filter expects 2 or 4 arguments, none provided\n");
+        return AVERROR(EINVAL);
+    }
+
+    e = sscanf(args, "%d:%d:%d:%d",
+               &boxblur->luma_param.radius,
+               &boxblur->luma_param.power,
+               &boxblur->chroma_param.radius,
+               &boxblur->chroma_param.power);
+    if (e != 2 && e != 4) {
+        av_log(ctx, AV_LOG_ERROR,
+               "Filter expects 2 or 4 params, provided %d\n", e);
+        return AVERROR(EINVAL);
+    }
+
+    if (e == 2) {
+        /* set chroma == luma params */
+        boxblur->chroma_param.radius = boxblur->luma_param.radius;
+        boxblur->chroma_param.power  = boxblur->luma_param.power;
+    }
+
+    av_log(ctx, AV_LOG_INFO,
+           "luma_radius:%d luma_power:%d chroma_radius:%d chroma_power:%d\n",
+           boxblur->luma_param.radius, boxblur->luma_param.power,
+           boxblur->chroma_param.radius, boxblur->chroma_param.power);
+
+    if (boxblur->luma_param.radius < 0 || boxblur->chroma_param.radius < 0) {
+        av_log(ctx, AV_LOG_ERROR,
+               "Invalid negative value for luma or chroma radius\n");
+        return AVERROR(EINVAL);
+    }
+
+    boxblur->radius[0] = boxblur->luma_param.radius;
+    boxblur->radius[1] = boxblur->radius[2] = boxblur->chroma_param.radius;
+    boxblur->power [0]  = boxblur->luma_param.power;
+    boxblur->power [1]  = boxblur->power[2] = boxblur->chroma_param.power;
+
+    return 0;
+}
+
+static int query_formats(AVFilterContext *ctx)
+{
+    enum PixelFormat pix_fmts[] = {
+        PIX_FMT_YUV444P,  PIX_FMT_YUV422P,  PIX_FMT_YUV420P,
+        PIX_FMT_YUV411P,  PIX_FMT_YUV410P,
+        PIX_FMT_YUVJ444P, PIX_FMT_YUVJ422P, PIX_FMT_YUVJ420P,
+        PIX_FMT_YUV440P,  PIX_FMT_YUVJ440P,
+        PIX_FMT_NONE
+    };
+
+    avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
+    return 0;
+}
+
+static int config_input(AVFilterLink *inlink)
+{
+    AVFilterContext *ctx = inlink->dst;
+    BoxBlurContext *boxblur = ctx->priv;
+    const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[inlink->format];
+
+    boxblur->hsub = desc->log2_chroma_w;
+    boxblur->vsub = desc->log2_chroma_h;
+
+    return 0;
+}
+
+static inline void blur(uint8_t *dst, int dst_linesize, uint8_t *src, int src_linesize,
+                        int w, int radius)
+{
+    int x, sum = 0;
+    const int length = radius*2 + 1;
+    const int inv = ((1<<16) + length/2)/length;
+
+    for (x = 0; x < radius; x++)
+        sum += src[x*src_linesize]<<1;
+    sum += src[radius*src_linesize];
+
+    for (x = 0; x <= radius; x++) {
+        sum += src[(radius+x)*src_linesize] - src[(radius-x)*src_linesize];
+        dst[x*dst_linesize] = (sum*inv + (1<<15))>>16;
+    }
+
+    for (; x < w-radius; x++) {
+        sum += src[(radius+x)*src_linesize] - src[(x-radius-1)*src_linesize];
+        dst[x*dst_linesize] = (sum*inv + (1<<15))>>16;
+    }
+
+    for (; x < w; x++) {
+        sum += src[(2*w-radius-x-1)*src_linesize] - src[(x-radius-1)*src_linesize];
+        dst[x*dst_linesize] = (sum*inv + (1<<15))>>16;
+    }
+}
+
+static inline void blur2(uint8_t *dst, int dst_linesize, uint8_t *src, int src_linesize,
+                         int w, int radius, int power)
+{
+    uint8_t temp[2][4096];
+    uint8_t *a = temp[0], *b = temp[1];
+
+    if (radius && power) {
+        blur(a, 1, src, src_linesize, w, radius);
+        for (; power > 2; power--) {
+            uint8_t *c;
+            blur(b, 1, a, 1, w, radius);
+            c = a; a = b; b = c;
+        }
+        if (power > 1) {
+            blur(dst, dst_linesize, a, 1, w, radius);
+        } else {
+            int i;
+            for (i = 0; i < w; i++)
+                dst[i*dst_linesize] = a[i];
+        }
+    } else {
+        int i;
+        for (i = 0; i < w; i++)
+            dst[i*dst_linesize] = src[i*src_linesize];
+    }
+}
+
+static void hblur(uint8_t *dst, int dstStride, uint8_t *src, int srcStride,
+                 int w, int h, int radius, int power)
+{
+    int y;
+
+    if (radius == 0 && dst == src)
+        return;
+
+    for (y = 0; y < h; y++)
+        blur2(dst + y*dstStride, 1, src + y*srcStride, 1, w, radius, power);
+}
+
+static void vblur(uint8_t *dst, int dstStride, uint8_t *src, int srcStride,
+                 int w, int h, int radius, int power)
+{
+    int x;
+
+    if (radius == 0 && dst == src)
+        return;
+
+    for (x = 0; x < w; x++)
+        blur2(dst + x, dstStride, src + x, srcStride, h, radius, power);
+}
+
+static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { }
+
+static void end_frame(AVFilterLink *inlink)
+{
+    AVFilterContext *ctx = inlink->dst;
+    BoxBlurContext *boxblur = ctx->priv;
+    AVFilterLink *outlink = inlink->dst->outputs[0];
+    AVFilterBufferRef *inpicref  = inlink ->cur_buf;
+    AVFilterBufferRef *outpicref = outlink->out_buf;
+    int plane;
+    int cw = inlink->w >> boxblur->hsub, ch = inlink->h >> boxblur->vsub;
+    int w[3] = { inlink->w, cw, cw };
+    int h[3] = { inlink->h, ch, ch };
+
+    for (plane = 0; inpicref->data[plane] && plane < 3; plane++)
+        hblur(outpicref->data[plane], outpicref->linesize[plane],
+              inpicref ->data[plane], inpicref ->linesize[plane],
+              w[plane], h[plane], boxblur->radius[plane], boxblur->power[plane]);
+
+    for (plane = 0; inpicref->data[plane] && plane < 3; plane++)
+        vblur(outpicref->data[plane], outpicref->linesize[plane],
+              outpicref->data[plane], outpicref->linesize[plane],
+              w[plane], h[plane], boxblur->radius[plane], boxblur->power[plane]);
+
+    avfilter_unref_buffer(inpicref);
+    avfilter_draw_slice(outlink, 0, outlink->h, 1);
+    avfilter_end_frame(outlink);
+    avfilter_unref_buffer(outpicref);
+}
+
+AVFilter avfilter_vf_boxblur = {
+    .name      = "boxblur",
+    .description = NULL_IF_CONFIG_SMALL("Blur the input."),
+    .priv_size = sizeof(BoxBlurContext),
+    .init      = init,
+
+    .query_formats   = query_formats,
+    .inputs    = (AVFilterPad[]) {{ .name             = "default",
+                                    .type             = AVMEDIA_TYPE_VIDEO,
+                                    .config_props     = config_input,
+                                    .draw_slice       = null_draw_slice,
+                                    .end_frame        = end_frame,
+                                    .min_perms        = AV_PERM_READ },
+                                  { .name = NULL}},
+    .outputs   = (AVFilterPad[]) {{ .name             = "default",
+                                    .type             = AVMEDIA_TYPE_VIDEO, },
+                                  { .name = NULL}},
+};
-- 
1.7.2.5



More information about the ffmpeg-devel mailing list