On Tue, Feb 24, 2015 at 4:12 PM, Michael Niedermayer <michae...@gmx.at>
wrote:

> On Tue, Feb 24, 2015 at 02:27:01PM +0530, arwa arif wrote:
> > Hello,
> >
> > I have written a very primitive code for porting FFT domain filter. It
> > accepts only gray8 format images. The output should be a grayscale image,
> > but the ouput image is coming out to be a black and white image. Also, I
> am
> > getting confused when to do the vertical pass. After taking irdft of the
> > horizontal pass or before it?
>
> you can do both rdft first and then the 2 irdft passes, this should
> give more possibilities in filtering but it could be done the other
> way around too
>
>
> >
> > I have attached the patch.
>
> >  Makefile     |    1
> >  allfilters.c |    1
> >  vf_fftfilt.c |  139
> +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
> >  3 files changed, 141 insertions(+)
> > d4b25d6a204534a66400f52c1f5312652e8208af
> 0001-Port-FFT-domain-filter.patch
> > From 455a261d7e2b3afba767aac2e73448aeee02d159 Mon Sep 17 00:00:00 2001
> > From: Arwa Arif <arwaarif1...@gmail.com>
> > Date: Tue, 24 Feb 2015 12:17:30 +0530
> > Subject: [PATCH] Port FFT domain filter.
> >
> > ---
> >  libavfilter/Makefile     |    1 +
> >  libavfilter/allfilters.c |    1 +
> >  libavfilter/vf_fftfilt.c |  139
> ++++++++++++++++++++++++++++++++++++++++++++++
> >  3 files changed, 141 insertions(+)
> >  create mode 100644 libavfilter/vf_fftfilt.c
> >
> > diff --git a/libavfilter/Makefile b/libavfilter/Makefile
> > index 289c63b..b184f07 100644
> > --- a/libavfilter/Makefile
> > +++ b/libavfilter/Makefile
> > @@ -120,6 +120,7 @@ OBJS-$(CONFIG_EDGEDETECT_FILTER)             +=
> vf_edgedetect.o
> >  OBJS-$(CONFIG_EQ_FILTER)                     += vf_eq.o
> >  OBJS-$(CONFIG_EXTRACTPLANES_FILTER)          += vf_extractplanes.o
> >  OBJS-$(CONFIG_FADE_FILTER)                   += vf_fade.o
> > +OBJS-$(CONFIG_FFTFILT_FILTER)                += vf_fftfilt.o
> >  OBJS-$(CONFIG_FIELD_FILTER)                  += vf_field.o
> >  OBJS-$(CONFIG_FIELDMATCH_FILTER)             += vf_fieldmatch.o
> >  OBJS-$(CONFIG_FIELDORDER_FILTER)             += vf_fieldorder.o
> > diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
> > index 55de154..043ac56 100644
> > --- a/libavfilter/allfilters.c
> > +++ b/libavfilter/allfilters.c
> > @@ -136,6 +136,7 @@ void avfilter_register_all(void)
> >      REGISTER_FILTER(EQ,             eq,             vf);
> >      REGISTER_FILTER(EXTRACTPLANES,  extractplanes,  vf);
> >      REGISTER_FILTER(FADE,           fade,           vf);
> > +    REGISTER_FILTER(FFTFILT,        fftfilt,        vf);
> >      REGISTER_FILTER(FIELD,          field,          vf);
> >      REGISTER_FILTER(FIELDMATCH,     fieldmatch,     vf);
> >      REGISTER_FILTER(FIELDORDER,     fieldorder,     vf);
> > diff --git a/libavfilter/vf_fftfilt.c b/libavfilter/vf_fftfilt.c
> > new file mode 100644
> > index 0000000..753bc8e
> > --- /dev/null
> > +++ b/libavfilter/vf_fftfilt.c
> > @@ -0,0 +1,139 @@
> > +/*
> > + * Copyright (c) 2015 Arwa Arif <arwaarif1...@gmail.com>
> > + *
> > + * 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
> > + * FFT domain filtering.
> > + */
> > +
> > +#include "libavfilter/internal.h"
> > +#include "libavutil/common.h"
> > +#include "libavutil/imgutils.h"
> > +#include "libavutil/opt.h"
> > +#include "libavutil/pixdesc.h"
> > +#include "libavcodec/avfft.h"
> > +
> > +typedef struct {
> > +    const AVClass *class;
> > +
> > +    RDFTContext *rdft;
> > +    int rdft_bits;
> > +    FFTSample *rdft_data;
> > +
> > +} FFTFILTContext;
> > +
> > +static int filter_frame(AVFilterLink *inlink, AVFrame *in)
> > +{
> > +    AVFilterContext *ctx = inlink->dst;
> > +    AVFilterLink *outlink = inlink->dst->outputs[0];
> > +    FFTFILTContext *fftfilt = ctx->priv;
> > +    AVFrame *out;
> > +    int i, j, k, rdft_bits;
> > +    size_t rdft_len, w, h;
> > +
> > +    w = inlink->w;
> > +    h = inlink->h;
> > +
> > +    /* RDFT window size (precision) according to the requested output
> frame height */
> > +    for (rdft_bits = 1; 1 << rdft_bits < 2 * w; rdft_bits++);
> > +    rdft_len = 1 << rdft_bits;
> > +    fftfilt->rdft_data = av_malloc_array(h, rdft_len *
> sizeof(FFTSample));
> > +    memset(fftfilt->rdft_data, 0, rdft_len * h * sizeof(FFTSample));
> > +
> > +    out = ff_get_video_buffer(outlink, inlink->w, inlink->h);
> > +    if (!out)
> > +        return AVERROR(ENOMEM);
> > +
> > +    av_frame_copy_props(out, in);
> > +
> > +    /*Horizontal pass - RDFT*/
> > +    fftfilt->rdft = av_rdft_init(rdft_bits, DFT_R2C);
>
> > +    k = 0;
> > +    for (i = 0; i < h; i++)
> > +        for (j = 0; j < w; j++)
> > +        {
> > +            fftfilt->rdft_data[k] = *(in->data[0] + in->linesize[0] * i
> + j);
> > +            k++;
> > +        }
>
> the copied data must line up with the rdft applied to them
> also its better to only clear the space between than the whole, as its
> faster, so the memset above is not needed if the memset below is used
>     for (i = 0; i < h; i++) {
>         for (j = 0; j < w; j++)
>             fftfilt->rdft_data[i*rdft_len + j] = *(in->data[0] +
> in->linesize[0] * i + j);
>         memset(fftfilt->rdft_data + i*rdft_len + j, 0,
> (rdft_len-w)*sizeof(*fftfilt->rdft_data));
>     }
>
>
> > +
> > +    for (i = 0; i < h; i++)
> > +        av_rdft_calc(fftfilt->rdft, fftfilt->rdft_data + i * rdft_len);
> > +
> > +    av_rdft_end(fftfilt->rdft);
> > +
> > +    /*Horizontal pass - IRDFT*/
> > +    fftfilt->rdft = av_rdft_init(rdft_bits, IDFT_C2R);
> > +
> > +    for (i = 0; i < h; i++)
> > +        av_rdft_calc(fftfilt->rdft, fftfilt->rdft_data + i * rdft_len);
> > +
> > +    k = 0;
>
> > +    for (i = 0; i < h; i++)
> > +        for (j = 0; j < w; j++)
> > +        {
> > +            *(out->data[0] + out->linesize[0] * i + j) =
> fftfilt->rdft_data[k];
> > +            k++;
> > +        }
>
> this needs cliping and scaling
>     for (i = 0; i < h; i++)
>         for (j = 0; j < w; j++)
>             *(out->data[0] + out->linesize[0] * i + j) =
> av_clip(fftfilt->rdft_data[i*rdft_len + j] * 2 / rdft_len, 0, 255);
>
>
>
Thanks a lot. It is working for horizontal passes. I tried to introduce
vertical pass. But, for that I am not getting the exact same image. Should
the array sizes of vertical pass and horizontal passes be same? Also, on
what basis did you decide the scaling factor?

I have attached the patch.


>
>
> [...]
>
>
>
> --
> Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB
>
> While the State exists there can be no freedom; when there is freedom there
> will be no State. -- Vladimir Leninam
>
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
>
From 11629e4c279e5bc0dc331017fe9c8db0b7571519 Mon Sep 17 00:00:00 2001
From: Arwa Arif <arwaarif1...@gmail.com>
Date: Tue, 24 Feb 2015 12:17:30 +0530
Subject: [PATCH] Port FFT domain filter.

---
 libavfilter/Makefile     |    1 +
 libavfilter/allfilters.c |    1 +
 libavfilter/vf_fftfilt.c |  168 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 170 insertions(+)
 create mode 100644 libavfilter/vf_fftfilt.c

diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 289c63b..b184f07 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -120,6 +120,7 @@ OBJS-$(CONFIG_EDGEDETECT_FILTER)             += vf_edgedetect.o
 OBJS-$(CONFIG_EQ_FILTER)                     += vf_eq.o
 OBJS-$(CONFIG_EXTRACTPLANES_FILTER)          += vf_extractplanes.o
 OBJS-$(CONFIG_FADE_FILTER)                   += vf_fade.o
+OBJS-$(CONFIG_FFTFILT_FILTER)                += vf_fftfilt.o
 OBJS-$(CONFIG_FIELD_FILTER)                  += vf_field.o
 OBJS-$(CONFIG_FIELDMATCH_FILTER)             += vf_fieldmatch.o
 OBJS-$(CONFIG_FIELDORDER_FILTER)             += vf_fieldorder.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index 55de154..043ac56 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -136,6 +136,7 @@ void avfilter_register_all(void)
     REGISTER_FILTER(EQ,             eq,             vf);
     REGISTER_FILTER(EXTRACTPLANES,  extractplanes,  vf);
     REGISTER_FILTER(FADE,           fade,           vf);
+    REGISTER_FILTER(FFTFILT,        fftfilt,        vf);
     REGISTER_FILTER(FIELD,          field,          vf);
     REGISTER_FILTER(FIELDMATCH,     fieldmatch,     vf);
     REGISTER_FILTER(FIELDORDER,     fieldorder,     vf);
diff --git a/libavfilter/vf_fftfilt.c b/libavfilter/vf_fftfilt.c
new file mode 100644
index 0000000..88c3fe5
--- /dev/null
+++ b/libavfilter/vf_fftfilt.c
@@ -0,0 +1,168 @@
+/*
+ * Copyright (c) 2015 Arwa Arif <arwaarif1...@gmail.com>
+ *
+ * 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
+ * FFT domain filtering.
+ */
+
+#include "libavfilter/internal.h"
+#include "libavutil/common.h"
+#include "libavutil/imgutils.h"
+#include "libavutil/opt.h"
+#include "libavutil/pixdesc.h"
+#include "libavcodec/avfft.h"
+
+typedef struct {
+    const AVClass *class;
+
+    RDFTContext *rdft;
+    int rdft_hbits;
+    int rdft_vbits;
+    FFTSample *rdft_hdata;
+    FFTSample *rdft_vdata;
+
+} FFTFILTContext;
+
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
+{
+    AVFilterContext *ctx = inlink->dst;
+    AVFilterLink *outlink = inlink->dst->outputs[0];
+    FFTFILTContext *fftfilt = ctx->priv;
+    AVFrame *out;
+    int i, j, rdft_hbits, rdft_vbits;
+    size_t rdft_hlen, rdft_vlen, w, h;
+
+    w = inlink->w;
+    h = inlink->h;
+
+    /* RDFT - Array initialization for Horizontal pass*/
+    for (rdft_hbits = 1; 1 << rdft_hbits < 2 * w; rdft_hbits++);
+    rdft_hlen = 1 << rdft_hbits;
+    fftfilt->rdft_hdata = av_malloc_array(h, rdft_hlen * sizeof(FFTSample));
+
+    /* RDFT - Array initialization for Vertical pass*/
+    for (rdft_vbits = 1; 1 << rdft_vbits < 2 * h; rdft_vbits++);
+    rdft_vlen = 1 << rdft_vbits;
+    fftfilt->rdft_vdata = av_malloc_array(w, rdft_vlen * sizeof(FFTSample));
+
+    out = ff_get_video_buffer(outlink, inlink->w, inlink->h);
+    if (!out)
+        return AVERROR(ENOMEM);
+
+    av_frame_copy_props(out, in);
+
+    /*Horizontal pass - RDFT*/
+    fftfilt->rdft = av_rdft_init(rdft_hbits, DFT_R2C);
+    for (i = 0; i < h; i++)
+    {
+        memset(fftfilt->rdft_hdata + i * rdft_hlen, 0, rdft_hlen * sizeof(*fftfilt->rdft_hdata));
+        for (j = 0; j < w; j++)
+            fftfilt->rdft_hdata[i * rdft_hlen + j] = *(in->data[0] + in->linesize[0] * i + j);
+    }
+
+    for (i = 0; i < h; i++)
+        av_rdft_calc(fftfilt->rdft, fftfilt->rdft_hdata + i * rdft_hlen);
+    
+    av_rdft_end(fftfilt->rdft);
+
+    /*Vertical pass - RDFT*/
+    fftfilt->rdft = av_rdft_init(rdft_vbits, DFT_R2C);
+    for (i = 0; i < w; i++)
+    {
+        memset(fftfilt->rdft_vdata + i * rdft_vlen, 0, rdft_vlen * sizeof(*fftfilt->rdft_vdata));
+        for (j = 0; j < h; j++)
+            fftfilt->rdft_vdata[i * rdft_vlen + j] = av_clip(fftfilt->rdft_hdata[j * rdft_hlen + i] * 2 / rdft_hlen, 0, 255);
+    }
+
+    for (i = 0; i < w; i++)
+        av_rdft_calc(fftfilt->rdft, fftfilt->rdft_vdata + i * rdft_vlen);
+    
+    av_rdft_end(fftfilt->rdft);
+
+    /*Vertical pass - IRDFT*/
+    fftfilt->rdft = av_rdft_init(rdft_vbits, IDFT_C2R);
+
+    for (i = 0; i < w; i++)
+        av_rdft_calc(fftfilt->rdft, fftfilt->rdft_vdata + i * rdft_vlen);
+
+    for (i = 0; i < w; i++)
+        for (j = 0; j < h; j++)
+            fftfilt->rdft_hdata[j * rdft_hlen + i] = av_clip(fftfilt->rdft_vdata[i * rdft_vlen + j] * 2 / rdft_vlen , 0, 255);
+    
+    av_rdft_end(fftfilt->rdft);
+
+    /*Horizontal pass - IRDFT*/
+    fftfilt->rdft = av_rdft_init(rdft_hbits, IDFT_C2R);
+
+    for (i = 0; i < h; i++)
+        av_rdft_calc(fftfilt->rdft, fftfilt->rdft_hdata + i * rdft_hlen);
+
+    for (i = 0; i < h; i++)
+        for (j = 0; j < w; j++)
+            *(out->data[0] + out->linesize[0] * i + j) = av_clip(fftfilt->rdft_hdata[i * rdft_hlen + j] * 2 / rdft_hlen, 0, 255);
+    
+    av_rdft_end(fftfilt->rdft);
+
+    av_free(fftfilt->rdft_hdata);
+    av_free(fftfilt->rdft_vdata);
+
+    av_frame_free(&in);
+
+    return ff_filter_frame(outlink, out);
+}
+
+static int query_formats(AVFilterContext *ctx)
+{
+    static const enum AVPixelFormat pixel_fmts_fftfilt[] = {
+        AV_PIX_FMT_GRAY8,
+        AV_PIX_FMT_NONE
+    };
+
+    ff_set_common_formats(ctx, ff_make_format_list(pixel_fmts_fftfilt));
+
+    return 0;
+}
+
+static const AVFilterPad fftfilt_inputs[] = {
+    {
+        .name = "default",
+        .type = AVMEDIA_TYPE_VIDEO,
+        .filter_frame = filter_frame,
+    },
+    { NULL }
+};
+
+static const AVFilterPad fftfilt_outputs[] = {
+    {
+        .name = "default",
+        .type = AVMEDIA_TYPE_VIDEO,
+    },
+    { NULL }
+};
+
+AVFilter ff_vf_fftfilt = {
+    .name            = "fftfilt",
+    .description     = NULL_IF_CONFIG_SMALL("Adjust gain."),
+    .priv_size       = sizeof(FFTFILTContext),
+    .inputs          = fftfilt_inputs,
+    .outputs         = fftfilt_outputs,
+    .query_formats   = query_formats,
+};
-- 
1.7.9.5

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

Reply via email to