[FFmpeg-devel] [PATCH] lavfi/RFC: add dctdnoiz filter

Clément Bœsch ubitux at gmail.com
Sat Apr 27 17:19:21 CEST 2013


Simple and extremely slow DCT denoiser.

TODO: doc, minor bump, Changelog

---
Basically a brain dead implementation of
www.ipol.im/pub/art/2011/ys-dct/gg

In addition there is an overlap parameter (which needs some adjustment
to deal properly with borders), and an optional expression allowing more
advanced coefficient modifications than a hard threshold like proposed
in the paper.

Equivalence between sigma and expression parameters:
  dctdnoiz=20 <=> dctdnoiz=e='gte(c,20*3)'

With the examples from the paper:
  noise 15: http://i.imgur.com/UBDaAlX.jpg
  noise 30: http://i.imgur.com/yEfwC21.jpg
---
 configure                 |   1 +
 libavfilter/Makefile      |   1 +
 libavfilter/allfilters.c  |   1 +
 libavfilter/vf_dctdnoiz.c | 389 ++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 392 insertions(+)
 create mode 100644 libavfilter/vf_dctdnoiz.c

diff --git a/configure b/configure
index 648db2f..5158646 100755
--- a/configure
+++ b/configure
@@ -2120,6 +2120,7 @@ blackframe_filter_deps="gpl"
 boxblur_filter_deps="gpl"
 colormatrix_filter_deps="gpl"
 cropdetect_filter_deps="gpl"
+dctdnoiz_filter_deps="avcodec"
 delogo_filter_deps="gpl"
 deshake_filter_deps="avcodec"
 deshake_filter_select="dsputil"
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 358a4b6..43b3eae 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -114,6 +114,7 @@ OBJS-$(CONFIG_COPY_FILTER)                   += vf_copy.o
 OBJS-$(CONFIG_CROP_FILTER)                   += vf_crop.o
 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_DELOGO_FILTER)                 += vf_delogo.o
 OBJS-$(CONFIG_DESHAKE_FILTER)                += vf_deshake.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index 97e558b..8f5dc63 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -112,6 +112,7 @@ void avfilter_register_all(void)
     REGISTER_FILTER(CROP,           crop,           vf);
     REGISTER_FILTER(CROPDETECT,     cropdetect,     vf);
     REGISTER_FILTER(CURVES,         curves,         vf);
+    REGISTER_FILTER(DCTDNOIZ,       dctdnoiz,       vf);
     REGISTER_FILTER(DECIMATE,       decimate,       vf);
     REGISTER_FILTER(DELOGO,         delogo,         vf);
     REGISTER_FILTER(DESHAKE,        deshake,        vf);
diff --git a/libavfilter/vf_dctdnoiz.c b/libavfilter/vf_dctdnoiz.c
new file mode 100644
index 0000000..5841e7c
--- /dev/null
+++ b/libavfilter/vf_dctdnoiz.c
@@ -0,0 +1,389 @@
+/*
+ * Copyright (c) 2013 Clément Bœsch
+ *
+ * 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
+ */
+
+/**
+ * A simple, relatively efficient and extremely slow DCT image denoiser.
+ * @see http://www.ipol.im/pub/art/2011/ys-dct/
+ */
+
+#include "libavcodec/avfft.h"
+#include "libavutil/eval.h"
+#include "libavutil/opt.h"
+#include "drawutils.h"
+#include "internal.h"
+
+#define NBITS 4
+#define BSIZE (1<<(NBITS))
+
+static const char *const var_names[] = { "c", NULL };
+enum { VAR_C, VAR_VARS_NB };
+
+typedef struct {
+    const AVClass *class;
+
+    /* coefficient factor expression */
+    char *expr_str;
+    AVExpr *expr;
+    double var_values[VAR_VARS_NB];
+
+    float sigma;                // used when no expression are st
+    float th;                   // threshold (3*sigma)
+    float color_dct[3][3];      // 3x3 DCT for color decorrelation
+    float *cbuf[2][3];          // two planar rgb color buffers
+    float *weights;             // dct coeff are cumulated with overlapping; these values are used for averaging
+    int p_linesize;             // line sizes for color and weights
+    int overlap;                // number of block overlapping pixels
+    int step;                   // block step increment (BSIZE - overlap)
+    DCTContext *dct, *idct;     // DCT and inverse DCT contexts
+    float *block, *tmp_block;   // two BSIZE x BSIZE block buffers
+} DCTdnoizContext;
+
+#define OFFSET(x) offsetof(DCTdnoizContext, x)
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
+static const AVOption dctdnoiz_options[] = {
+    { "s",       "set noise sigma constant",               OFFSET(sigma),    AV_OPT_TYPE_FLOAT,  {.dbl=0},            0, 999,          .flags = FLAGS },
+    { "overlap", "set number of block overlapping pixels", OFFSET(overlap),  AV_OPT_TYPE_INT,    {.i64=(1<<NBITS)-1}, 0, (1<<NBITS)-1, .flags = FLAGS },
+    { "e",       "set coefficient factor expression",      OFFSET(expr_str), AV_OPT_TYPE_STRING, {.str=NULL},                          .flags = FLAGS },
+    { NULL }
+};
+
+AVFILTER_DEFINE_CLASS(dctdnoiz);
+
+static float *dct_block(DCTdnoizContext *ctx, const float *src, int src_linesize)
+{
+    int x, y;
+    float *column;
+
+    for (y = 0; y < BSIZE; y++) {
+        float *line = ctx->block;
+
+        memcpy(line, src, BSIZE * sizeof(*line));
+        src += src_linesize;
+        av_dct_calc(ctx->dct, line);
+
+        column = ctx->tmp_block + y;
+        for (x = 0; x < BSIZE; x++) {
+            *line *= x == 0 ? 1. / sqrt(BSIZE) : sqrt(2. / BSIZE);
+            *column = *line++;
+            column += BSIZE;
+        }
+    }
+
+    column = ctx->tmp_block;
+    for (x = 0; x < BSIZE; x++) {
+        av_dct_calc(ctx->dct, column);
+        for (y = 0; y < BSIZE; y++)
+            column[y] *= y == 0 ? 1. / sqrt(BSIZE) : sqrt(2. / BSIZE);
+        column += BSIZE;
+    }
+
+    for (y = 0; y < BSIZE; y++)
+        for (x = 0; x < BSIZE; x++)
+            ctx->block[y*BSIZE + x] = ctx->tmp_block[x*BSIZE + y];
+
+    return ctx->block;
+}
+
+static void idct_block(DCTdnoizContext *ctx, float *dst, int dst_linesize)
+{
+    int x, y;
+    float *block = ctx->block;
+    float *tmp = ctx->tmp_block;
+
+    for (y = 0; y < BSIZE; y++) {
+        for (x = 0; x < BSIZE; x++)
+            block[x] *= x == 0 ? sqrt(BSIZE) : 1./sqrt(2. / BSIZE);
+        av_dct_calc(ctx->idct, block);
+        block += BSIZE;
+    }
+
+    block = ctx->block;
+    for (y = 0; y < BSIZE; y++) {
+        for (x = 0; x < BSIZE; x++) {
+            tmp[x] = block[x*BSIZE + y];
+            tmp[x] *= x == 0 ? sqrt(BSIZE) : 1./sqrt(2. / BSIZE);
+        }
+        av_dct_calc(ctx->idct, tmp);
+        for (x = 0; x < BSIZE; x++)
+            dst[x*dst_linesize + y] += tmp[x];
+    }
+}
+
+static int config_input(AVFilterLink *inlink)
+{
+    DCTdnoizContext *ddn = inlink->dst->priv;
+    const int linesize = FFALIGN(inlink->w, 16);
+    int i, x, y, bx, by, *iweights;
+    const float dct_3x3[3][3] = {
+        { 1./sqrt(3),  1./sqrt(3),  1./sqrt(3) },
+        { 1./sqrt(2),           0, -1./sqrt(2) },
+        { 1./sqrt(6), -2./sqrt(6),  1./sqrt(6) },
+    };
+    uint8_t rgba_map[4];
+
+    ff_fill_rgba_map(rgba_map, inlink->format);
+    for (y = 0; y < 3; y++)
+        for (x = 0; x < 3; x++)
+            ddn->color_dct[y][x] = dct_3x3[rgba_map[y]][rgba_map[x]];
+
+    ddn->p_linesize = linesize;
+    for (i = 0; i < 2; i++) {
+        ddn->cbuf[i][0] = av_malloc(linesize * inlink->h * sizeof(*ddn->cbuf[i][0]));
+        ddn->cbuf[i][1] = av_malloc(linesize * inlink->h * sizeof(*ddn->cbuf[i][1]));
+        ddn->cbuf[i][2] = av_malloc(linesize * inlink->h * sizeof(*ddn->cbuf[i][2]));
+        if (!ddn->cbuf[i][0] || !ddn->cbuf[i][1] || !ddn->cbuf[i][2])
+            return AVERROR(ENOMEM);
+    }
+
+    ddn->weights = av_malloc(inlink->h * linesize * sizeof(*ddn->weights));
+    if (!ddn->weights)
+        return AVERROR(ENOMEM);
+    iweights = av_calloc(inlink->h, linesize * sizeof(*iweights));
+    if (!iweights)
+        return AVERROR(ENOMEM);
+    for (y = 0; y < inlink->h - BSIZE + 1; y += ddn->step)
+        for (x = 0; x < inlink->w - BSIZE + 1; x += ddn->step)
+            for (by = 0; by < BSIZE; by++)
+                for (bx = 0; bx < BSIZE; bx++)
+                    iweights[(y + by)*linesize + x + bx]++;
+    for (y = 0; y < inlink->h; y++)
+        for (x = 0; x < inlink->w; x++)
+            ddn->weights[y*linesize + x] = 1. / iweights[y*linesize + x];
+    av_free(iweights);
+
+    return 0;
+}
+
+static av_cold int init(AVFilterContext *ctx)
+{
+    DCTdnoizContext *ddn = ctx->priv;
+
+    if (ddn->expr_str) {
+        int ret = av_expr_parse(&ddn->expr, ddn->expr_str, var_names,
+                                NULL, NULL, NULL, NULL, 0, ctx);
+        if (ret < 0)
+            return ret;
+    }
+
+    ddn->th   = ddn->sigma * 3.;
+    ddn->step = BSIZE - ddn->overlap;
+    ddn->dct  = av_dct_init(NBITS, DCT_II);
+    ddn->idct = av_dct_init(NBITS, DCT_III);
+    ddn->block     = av_malloc(BSIZE * BSIZE * sizeof(*ddn->block));
+    ddn->tmp_block = av_malloc(BSIZE * BSIZE * sizeof(*ddn->tmp_block));
+
+    if (!ddn->dct || !ddn->idct || !ddn->tmp_block || !ddn->block)
+        return AVERROR(ENOMEM);
+
+    return 0;
+}
+
+static int query_formats(AVFilterContext *ctx)
+{
+    static const enum AVPixelFormat pix_fmts[] = {
+        AV_PIX_FMT_BGR24, AV_PIX_FMT_RGB24,
+        AV_PIX_FMT_NONE
+    };
+    ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
+    return 0;
+}
+
+static void color_decorrelation(float dct3ch[3][3], float **dst, int dst_linesize,
+                                const uint8_t *src, int src_linesize, int w, int h)
+{
+    int x, y;
+    float *dstp_r = dst[0];
+    float *dstp_g = dst[1];
+    float *dstp_b = dst[2];
+
+    for (y = 0; y < h; y++) {
+        const uint8_t *srcp = src;
+
+        for (x = 0; x < w; x++) {
+            dstp_r[x] = srcp[0] * dct3ch[0][0] + srcp[1] * dct3ch[0][1] + srcp[2] * dct3ch[0][2];
+            dstp_g[x] = srcp[0] * dct3ch[1][0] + srcp[1] * dct3ch[1][1] + srcp[2] * dct3ch[1][2];
+            dstp_b[x] = srcp[0] * dct3ch[2][0] + srcp[1] * dct3ch[2][1] + srcp[2] * dct3ch[2][2];
+            srcp += 3;
+        }
+        src += src_linesize;
+        dstp_r += dst_linesize;
+        dstp_g += dst_linesize;
+        dstp_b += dst_linesize;
+    }
+}
+
+static void color_correlation(float dct3ch[3][3], uint8_t *dst, int dst_linesize,
+                              float **src, int src_linesize, int w, int h)
+{
+    int x, y;
+    const float *src_r = src[0];
+    const float *src_g = src[1];
+    const float *src_b = src[2];
+
+    for (y = 0; y < h; y++) {
+        uint8_t *dstp = dst;
+
+        for (x = 0; x < w; x++) {
+            dstp[0] = av_clip_uint8(src_r[x] * dct3ch[0][0] + src_g[x] * dct3ch[1][0] + src_b[x] * dct3ch[2][0]);
+            dstp[1] = av_clip_uint8(src_r[x] * dct3ch[0][1] + src_g[x] * dct3ch[1][1] + src_b[x] * dct3ch[2][1]);
+            dstp[2] = av_clip_uint8(src_r[x] * dct3ch[0][2] + src_g[x] * dct3ch[1][2] + src_b[x] * dct3ch[2][2]);
+            dstp += 3;
+        }
+        dst += dst_linesize;
+        src_r += src_linesize;
+        src_g += src_linesize;
+        src_b += src_linesize;
+    }
+}
+
+static void filter_plane(AVFilterContext *ctx,
+                         float *dst, int dst_linesize,
+                         const float *src, int src_linesize,
+                         int w, int h)
+{
+    int x, y, bx, by;
+    DCTdnoizContext *ddn = ctx->priv;
+    float *dst0 = dst;
+    const float *weights = ddn->weights;
+
+    // reset block sums
+    memset(dst, 0, h * dst_linesize * sizeof(*dst));
+
+    // block dct sums
+    for (y = 0; y < h - BSIZE + 1; y += ddn->step) {
+        for (x = 0; x < w - BSIZE + 1; x += ddn->step) {
+            float *ftb = dct_block(ddn, src + x, src_linesize);
+
+            if (ddn->expr) {
+                for (by = 0; by < BSIZE; by++) {
+                    for (bx = 0; bx < BSIZE; bx++) {
+                        ddn->var_values[VAR_C] = FFABS(*ftb);
+                        *ftb++ *= av_expr_eval(ddn->expr, ddn->var_values, ddn);
+                    }
+                }
+            } else {
+                for (by = 0; by < BSIZE; by++) {
+                    for (bx = 0; bx < BSIZE; bx++) {
+                        if (FFABS(*ftb) < ddn->th)
+                            *ftb = 0;
+                        ftb++;
+                    }
+                }
+            }
+            idct_block(ddn, dst + x, dst_linesize);
+        }
+        src += ddn->step * src_linesize;
+        dst += ddn->step * dst_linesize;
+    }
+
+    // average blocks
+    dst = dst0;
+    for (y = 0; y < h; y++) {
+        for (x = 0; x < w; x++)
+            dst[x] *= weights[x];
+        dst += dst_linesize;
+        weights += dst_linesize;
+    }
+}
+
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
+{
+    AVFilterContext *ctx = inlink->dst;
+    DCTdnoizContext *ddn = ctx->priv;
+    AVFilterLink *outlink = inlink->dst->outputs[0];
+    int direct, plane;
+    AVFrame *out;
+
+    if (av_frame_is_writable(in)) {
+        direct = 1;
+        out = in;
+    } else {
+        direct = 0;
+        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);
+    }
+
+    color_decorrelation(ddn->color_dct, ddn->cbuf[0], ddn->p_linesize,
+                        in->data[0], in->linesize[0], inlink->w, inlink->h);
+    for (plane = 0; plane < 3; plane++)
+        filter_plane(ctx, ddn->cbuf[1][plane], ddn->p_linesize,
+                          ddn->cbuf[0][plane], ddn->p_linesize,
+                          inlink->w, inlink->h);
+    color_correlation(ddn->color_dct, out->data[0], out->linesize[0],
+                      ddn->cbuf[1], ddn->p_linesize, inlink->w, inlink->h);
+
+    if (!direct)
+        av_frame_free(&in);
+
+    return ff_filter_frame(outlink, out);
+}
+
+static av_cold void uninit(AVFilterContext *ctx)
+{
+    int i;
+    DCTdnoizContext *ddn = ctx->priv;
+
+    av_dct_end(ddn->dct);
+    av_dct_end(ddn->idct);
+    av_free(ddn->block);
+    av_free(ddn->tmp_block);
+    av_free(ddn->weights);
+    for (i = 0; i < 2; i++) {
+        av_free(ddn->cbuf[i][0]);
+        av_free(ddn->cbuf[i][1]);
+        av_free(ddn->cbuf[i][2]);
+    }
+    av_expr_free(ddn->expr);
+}
+
+static const AVFilterPad dctdnoiz_inputs[] = {
+    {
+        .name         = "default",
+        .type         = AVMEDIA_TYPE_VIDEO,
+        .filter_frame = filter_frame,
+        .config_props = config_input,
+     },
+     { NULL }
+};
+
+static const AVFilterPad dctdnoiz_outputs[] = {
+     {
+         .name = "default",
+         .type = AVMEDIA_TYPE_VIDEO,
+     },
+     { NULL }
+};
+
+AVFilter avfilter_vf_dctdnoiz = {
+    .name          = "dctdnoiz",
+    .description   = NULL_IF_CONFIG_SMALL("Denoise frames using 2D DCT."),
+    .priv_size     = sizeof(DCTdnoizContext),
+    .init          = init,
+    .uninit        = uninit,
+    .query_formats = query_formats,
+    .inputs        = dctdnoiz_inputs,
+    .outputs       = dctdnoiz_outputs,
+    .priv_class    = &dctdnoiz_class,
+    .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE,
+};
-- 
1.8.2.1



More information about the ffmpeg-devel mailing list