[FFmpeg-devel] [PATCH] avfilter: add fftdnoiz filter
Paul B Mahol
onemda at gmail.com
Mon May 7 16:59:29 EEST 2018
Signed-off-by: Paul B Mahol <onemda at gmail.com>
---
libavfilter/Makefile | 1 +
libavfilter/allfilters.c | 1 +
libavfilter/vf_fftdnoiz.c | 393 ++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 395 insertions(+)
create mode 100644 libavfilter/vf_fftdnoiz.c
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index a416fa1fae..92f526a275 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -201,6 +201,7 @@ 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_FFTDNOIZ_FILTER) += vf_fftdnoiz.o
OBJS-$(CONFIG_FFTFILT_FILTER) += vf_fftfilt.o
OBJS-$(CONFIG_FIELD_FILTER) += vf_field.o
OBJS-$(CONFIG_FIELDHINT_FILTER) += vf_fieldhint.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index 42f956b3bf..f3a76c4652 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -193,6 +193,7 @@ extern AVFilter ff_vf_eq;
extern AVFilter ff_vf_erosion;
extern AVFilter ff_vf_extractplanes;
extern AVFilter ff_vf_fade;
+extern AVFilter ff_vf_fftdnoiz;
extern AVFilter ff_vf_fftfilt;
extern AVFilter ff_vf_field;
extern AVFilter ff_vf_fieldhint;
diff --git a/libavfilter/vf_fftdnoiz.c b/libavfilter/vf_fftdnoiz.c
new file mode 100644
index 0000000000..eac3f2642a
--- /dev/null
+++ b/libavfilter/vf_fftdnoiz.c
@@ -0,0 +1,393 @@
+/*
+ * 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 <float.h>
+
+#include "libavutil/avassert.h"
+#include "libavutil/common.h"
+#include "libavutil/imgutils.h"
+#include "libavutil/opt.h"
+#include "libavutil/pixdesc.h"
+#include "internal.h"
+#include "libavcodec/avfft.h"
+
+typedef struct PlaneContext {
+ int planewidth, planeheight;
+ int nox, noy;
+ int b;
+ int o;
+ float n;
+
+ float *buffer;
+ FFTComplex *hdata, *vdata;
+ int data_linesize;
+ int buffer_linesize;
+
+ FFTContext *fft, *ifft;
+} PlaneContext;
+
+typedef struct FFTdnoizContext {
+ const AVClass *class;
+
+ float sigma;
+ float amount;
+ int block_bits;
+ float overlap;
+ int planesf;
+
+ int nb_planes;
+ PlaneContext planes[4];
+} FFTdnoizContext;
+
+#define OFFSET(x) offsetof(FFTdnoizContext, x)
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
+static const AVOption fftdnoiz_options[] = {
+ { "sigma", "set noise sigma", OFFSET(sigma), AV_OPT_TYPE_FLOAT, {.dbl=2}, 0, 7, .flags = FLAGS },
+ { "amount", "set amount of denoising", OFFSET(amount), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0.01, 1, .flags = FLAGS },
+ { "block", "set block log2(size)", OFFSET(block_bits), AV_OPT_TYPE_INT, {.i64=5}, 4, 6, .flags = FLAGS },
+ { "overlap", "set block overlap", OFFSET(overlap), AV_OPT_TYPE_FLOAT, {.dbl=0.3333}, 0.2, 0.8, .flags = FLAGS },
+ { "planes", "set planes to filter", OFFSET(planesf), AV_OPT_TYPE_INT, {.i64=7}, 0, 15, .flags = FLAGS },
+ { NULL }
+};
+
+AVFILTER_DEFINE_CLASS(fftdnoiz);
+
+static int config_input(AVFilterLink *inlink)
+{
+ AVFilterContext *ctx = inlink->dst;
+ const AVPixFmtDescriptor *desc;
+ FFTdnoizContext *s = ctx->priv;
+ int i;
+
+ desc = av_pix_fmt_desc_get(inlink->format);
+
+ s->planes[1].planewidth = s->planes[2].planewidth = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
+ s->planes[0].planewidth = s->planes[3].planewidth = inlink->w;
+ s->planes[1].planeheight = s->planes[2].planeheight = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
+ s->planes[0].planeheight = s->planes[3].planeheight = inlink->h;
+
+ s->nb_planes = av_pix_fmt_count_planes(inlink->format);
+
+ for (i = 0; i < s->nb_planes; i++) {
+ PlaneContext *p = &s->planes[i];
+ int size;
+
+ p->b = 1 << s->block_bits;
+ p->n = 1.f / (p->b * p->b);
+ p->o = p->b * s->overlap;
+ size = p->b - p->o;
+ p->nox = (p->planewidth + (size - 1)) / size;
+ p->noy = (p->planeheight + (size - 1)) / size;
+
+ av_log(ctx, AV_LOG_DEBUG, "nox:%d noy:%d size:%d\n", p->nox, p->noy, size);
+
+ p->buffer_linesize = 2 * p->b * p->nox * sizeof(float);
+ p->buffer = av_calloc(p->nox * p->noy, 2 * p->b * p->b * sizeof(float));
+ p->data_linesize = 2 * p->b * sizeof(float);
+ p->hdata = av_calloc(p->b, p->data_linesize);
+ p->vdata = av_calloc(p->b, p->data_linesize);
+ if (!p->buffer || !p->hdata || !p->vdata)
+ return AVERROR(ENOMEM);
+ }
+
+ return 0;
+}
+
+static av_cold int init(AVFilterContext *ctx)
+{
+ FFTdnoizContext *s = ctx->priv;
+ int i;
+
+ for (i = 0; i < 4; i++) {
+ PlaneContext *p = &s->planes[i];
+
+ p->fft = av_fft_init(s->block_bits, 0);
+ p->ifft = av_fft_init(s->block_bits, 1);
+ if (!p->fft || !p->ifft)
+ return AVERROR(ENOMEM);
+ }
+
+ return 0;
+}
+
+static int query_formats(AVFilterContext *ctx)
+{
+ static const enum AVPixelFormat pix_fmts[] = {
+ AV_PIX_FMT_YUV420P,
+ AV_PIX_FMT_YUV422P,
+ AV_PIX_FMT_YUV444P,
+ AV_PIX_FMT_NONE
+ };
+ AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
+ if (!fmts_list)
+ return AVERROR(ENOMEM);
+ return ff_set_common_formats(ctx, fmts_list);
+}
+
+typedef struct ThreadData {
+ float *src, *dst;
+} ThreadData;
+
+static void import_plane(FFTdnoizContext *s,
+ uint8_t *srcp, int src_linesize,
+ float *buffer, int buffer_linesize, int plane)
+{
+ PlaneContext *p = &s->planes[plane];
+ const int width = p->planewidth;
+ const int height = p->planeheight;
+ const int block = p->b;
+ const int overlap = p->o;
+ const int size = block - overlap;
+ const int nox = p->nox;
+ const int noy = p->noy;
+ const int data_linesize = p->data_linesize / sizeof(FFTComplex);
+ FFTComplex *hdata = p->hdata;
+ FFTComplex *vdata = p->vdata;
+ int x, y, i, j;
+
+ buffer_linesize /= 4;
+ for (y = 0; y < noy; y++) {
+ for (x = 0; x < nox; x++) {
+ const int rh = FFMIN(block, height - y * size);
+ const int rw = FFMIN(block, width - x * size);
+ uint8_t *src = srcp + src_linesize * y * size + x * size;
+ float *bdst = buffer + buffer_linesize * y * block + x * block * 2;
+ FFTComplex *ssrc, *dst = hdata;
+
+ for (i = 0; i < rh; i++) {
+ for (j = 0; j < rw; j++) {
+ dst[j].re = src[j];
+ dst[j].im = 0;
+ }
+ for (; j < block; j++) {
+ dst[j].re = src[block - j - 1];
+ dst[j].im = 0;
+ }
+ av_fft_permute(p->fft, dst);
+ av_fft_calc(p->fft, dst);
+
+ src += src_linesize;
+ dst += data_linesize;
+ }
+
+ dst = hdata;
+ for (; i < block; i++) {
+ for (j = 0; j < block; j++) {
+ dst[j].re = dst[(block - i - 1) * data_linesize + j].re;
+ dst[j].im = dst[(block - i - 1) * data_linesize + j].im;
+ }
+ }
+
+ ssrc = hdata;
+ dst = vdata;
+ for (i = 0; i < block; i++) {
+ for (j = 0; j < block; j++)
+ dst[j] = ssrc[j * data_linesize + i];
+ av_fft_permute(p->fft, dst);
+ av_fft_calc(p->fft, dst);
+ memcpy(bdst, dst, block * sizeof(FFTComplex));
+
+ dst += data_linesize;
+ bdst += buffer_linesize;
+ }
+ }
+ }
+}
+
+static void export_plane(FFTdnoizContext *s,
+ uint8_t *dstp, int dst_linesize,
+ float *buffer, int buffer_linesize, int plane)
+{
+ PlaneContext *p = &s->planes[plane];
+ const int width = p->planewidth;
+ const int height = p->planeheight;
+ const int block = p->b;
+ const int overlap = p->o;
+ const int hoverlap = overlap / 2;
+ const int size = block - overlap;
+ const int nox = p->nox;
+ const int noy = p->noy;
+ const int data_linesize = p->data_linesize / sizeof(FFTComplex);
+ const float scale = 1.f / (block * block);
+ FFTComplex *hdata = p->hdata;
+ FFTComplex *vdata = p->vdata;
+ int x, y, i, j;
+
+ buffer_linesize /= 4;
+ for (y = 0; y < noy; y++) {
+ for (x = 0; x < nox; x++) {
+ const int woff = x == 0 ? 0 : hoverlap;
+ const int hoff = y == 0 ? 0 : hoverlap;
+ const int rw = x == 0 ? block : FFMIN(size, width - x * size - woff);
+ const int rh = y == 0 ? block : FFMIN(size, height - y * size - hoff);
+ float *bsrc = buffer + buffer_linesize * y * block + x * block * 2;
+ uint8_t *dst = dstp + dst_linesize * (y * size + hoff) + x * size + woff;
+ FFTComplex *hdst, *ddst = vdata;
+
+ hdst = hdata;
+ for (i = 0; i < block; i++) {
+ memcpy(ddst, bsrc, block * sizeof(FFTComplex));
+ av_fft_permute(p->ifft, ddst);
+ av_fft_calc(p->ifft, ddst);
+ for (j = 0; j < block; j++) {
+ hdst[j * data_linesize + i] = ddst[j];
+ }
+
+ ddst += data_linesize;
+ bsrc += buffer_linesize;
+ }
+
+ hdst = hdata + hoff * data_linesize;
+ for (i = 0; i < rh; i++) {
+ av_fft_permute(p->ifft, hdst);
+ av_fft_calc(p->ifft, hdst);
+ for (j = 0; j < rw; j++)
+ dst[j] = av_clip_uint8(hdst[j + woff].re * scale);
+
+ hdst += data_linesize;
+ dst += dst_linesize;
+ }
+ }
+ }
+}
+
+static void filter_plane(FFTdnoizContext *s, int plane)
+{
+ PlaneContext *p = &s->planes[plane];
+ const int block = p->b;
+ const int nox = p->nox;
+ const int noy = p->noy;
+ const int buffer_linesize = p->buffer_linesize / 4;
+ float *buffer = p->buffer;
+ const float sigma = s->sigma * s->sigma * block * block;
+ const float limit = 1.f - s->amount;
+ int y, x, i, j;
+
+ for (y = 0; y < noy; y++) {
+ for (x = 0; x < nox; x++) {
+ float *buff = buffer + buffer_linesize * y * block + x * block * 2;
+
+ for (i = 0; i < block; i++) {
+ for (j = 0; j < block; j++) {
+ float factor, power, re, im;
+
+ re = buff[j * 2 ];
+ im = buff[j * 2 + 1];
+ power = re * re + im * im + 1e-15f;
+ factor = FFMAX(limit, (power - sigma) / power);
+ buff[j * 2 ] *= factor;
+ buff[j * 2 + 1] *= factor;
+ }
+
+ buff += buffer_linesize;
+ }
+ }
+ }
+}
+
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
+{
+ AVFilterContext *ctx = inlink->dst;
+ FFTdnoizContext *s = ctx->priv;
+ AVFilterLink *outlink = ctx->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);
+ }
+
+ for (plane = 0; plane < s->nb_planes; plane++) {
+ PlaneContext *p = &s->planes[plane];
+
+ if (!((1 << plane) & s->planesf)) {
+ av_image_copy_plane(out->data[plane], out->linesize[plane],
+ in->data[plane], in->linesize[plane],
+ p->planewidth, p->planeheight);
+ continue;
+ }
+
+ import_plane(s, in->data[plane], in->linesize[plane],
+ p->buffer, p->buffer_linesize, plane);
+
+ filter_plane(s, plane);
+
+ export_plane(s, out->data[plane], out->linesize[plane],
+ p->buffer, p->buffer_linesize, plane);
+ }
+
+ if (!direct)
+ av_frame_free(&in);
+ return ff_filter_frame(outlink, out);
+}
+
+static av_cold void uninit(AVFilterContext *ctx)
+{
+ FFTdnoizContext *s = ctx->priv;
+ int i;
+
+ for (i = 0; i < 4; i++) {
+ PlaneContext *p = &s->planes[i];
+
+ av_freep(&p->hdata);
+ av_freep(&p->vdata);
+ av_freep(&p->buffer);
+ av_fft_end(p->fft);
+ av_fft_end(p->ifft);
+ }
+}
+
+static const AVFilterPad fftdnoiz_inputs[] = {
+ {
+ .name = "default",
+ .type = AVMEDIA_TYPE_VIDEO,
+ .filter_frame = filter_frame,
+ .config_props = config_input,
+ },
+ { NULL }
+};
+
+static const AVFilterPad fftdnoiz_outputs[] = {
+ {
+ .name = "default",
+ .type = AVMEDIA_TYPE_VIDEO,
+ },
+ { NULL }
+};
+
+AVFilter ff_vf_fftdnoiz = {
+ .name = "fftdnoiz",
+ .description = NULL_IF_CONFIG_SMALL("Denoise frames using 2D FFT."),
+ .priv_size = sizeof(FFTdnoizContext),
+ .init = init,
+ .uninit = uninit,
+ .query_formats = query_formats,
+ .inputs = fftdnoiz_inputs,
+ .outputs = fftdnoiz_outputs,
+ .priv_class = &fftdnoiz_class,
+ .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
+};
--
2.11.0
More information about the ffmpeg-devel
mailing list