Updated the patch.
From 4e73a3876d0e33af33ea3af27d38ce4e5755ad25 Mon Sep 17 00:00:00 2001
From: Arwa Arif <arwaarif1...@gmail.com>
Date: Tue, 24 Feb 2015 12:17:30 +0530
Subject: [PATCH] Add FFT domain filter.

---
 doc/filters.texi         |   38 +++++
 libavfilter/Makefile     |    1 +
 libavfilter/allfilters.c |    1 +
 libavfilter/vf_fftfilt.c |  344 ++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 384 insertions(+)
 create mode 100644 libavfilter/vf_fftfilt.c

diff --git a/doc/filters.texi b/doc/filters.texi
index 485efcb..10034e5 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -4591,6 +4591,44 @@ fade=t=in:st=5.5:d=0.5
 
 @end itemize
 
+@section fftfilt
+Adjust gain and luminance. It uses frequency domain filtering.
+
+@table @option
+@item dc_Y
+Adjust the dc value (gain) of the luma plane of the image. The filter
+accepts an integer value in range @code{0} to @code{1000}. The default
+value is set to @code{0}.
+
+@item dc_U
+Adjust the dc value (gain) of the 1st chroma plane of the image. The
+filter accepts an integer value in range @code{0} to @code{1000}. The
+default value is set to @code{0}.
+
+@item dc_V
+Adjust the dc value (gain) of the 2nd chroma plane of the image. The
+filter accepts an integer value in range @code{0} to @code{1000}. The 
+default value is set to @code{0}.
+
+@item lum_Y
+Set the luminance expression for the luma plane. 
+
+@item lum_U
+Set the luminance expression for the 1st chroma plane.
+
+@item lum_V
+Set the luminance expression for the 2nd chroma plane.
+
+The filter accepts the following variables:
+@item X
+@item Y
+The coordinates of the current sample.
+
+@item W
+@item H
+The width and height of the image.
+@end table
+
 @section field
 
 Extract a single field from an interlaced image using stride
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..dfed875
--- /dev/null
+++ b/libavfilter/vf_fftfilt.c
@@ -0,0 +1,344 @@
+/*
+ * 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 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
+ */
+
+/**
+ * @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"
+#include "libavutil/eval.h"
+
+typedef struct {
+    const AVClass *class;
+
+    RDFTContext *rdft;
+    int rdft_hbits[3];
+    int rdft_vbits[3];
+    size_t rdft_hlen[3];
+    size_t rdft_vlen[3];
+    FFTSample *rdft_hdata[3];
+    FFTSample *rdft_vdata[3];
+
+    int dc[3];
+    char *lum_str[3];
+    AVExpr *lum_expr[3];
+    double *lum_data[3];
+
+} FFTFILTContext;
+
+static const char *const var_names[] = {   "X",   "Y",   "W",   "H",     NULL    };
+enum                                   { VAR_X, VAR_Y, VAR_W, VAR_H, VAR_VARS_NB };
+
+enum { Y = 0, U, V };
+
+#define OFFSET(x) offsetof(FFTFILTContext, x)
+#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
+
+static const AVOption fftfilt_options[] = {
+    { "dc_Y",  "adjust gain in Y plane",              OFFSET(dc[Y]),      AV_OPT_TYPE_INT,    {.i64 = 0},      0,     1000,     FLAGS },
+    { "dc_U",  "adjust gain in U plane",              OFFSET(dc[U]),      AV_OPT_TYPE_INT,    {.i64 = 0},      0,     1000,     FLAGS },
+    { "dc_V",  "adjust gain in V plane",              OFFSET(dc[V]),      AV_OPT_TYPE_INT,    {.i64 = 0},      0,     1000,     FLAGS },
+    { "lum_Y", "set luminance expression in Y plane", OFFSET(lum_str[Y]), AV_OPT_TYPE_STRING, {.str = "1"}, CHAR_MIN, CHAR_MAX, FLAGS },
+    { "lum_U", "set luminance expression in U plane", OFFSET(lum_str[U]), AV_OPT_TYPE_STRING, {.str = "1"}, CHAR_MIN, CHAR_MAX, FLAGS },
+    { "lum_V", "set luminance expression in V plane", OFFSET(lum_str[V]), AV_OPT_TYPE_STRING, {.str = "1"}, CHAR_MIN, CHAR_MAX, FLAGS },
+    {NULL},
+};
+
+AVFILTER_DEFINE_CLASS(fftfilt);
+
+static inline double lum(void *priv, double x, double y, int plane)
+{
+    FFTFILTContext *fftfilt = priv;
+    return fftfilt->rdft_vdata[plane][(int)x * fftfilt->rdft_vlen[plane] + (int)y];
+}
+
+static double lum_Y(void *priv, double x, double y) { return lum(priv, x, y, Y); }
+static double lum_U(void *priv, double x, double y) { return lum(priv, x, y, U); }
+static double lum_V(void *priv, double x, double y) { return lum(priv, x, y, V); }
+
+static void copy_rev (FFTSample *dest[3], int start, size_t n, int plane)
+{
+    size_t i;
+    for (i = start * n; i < (start + 1) * n; i++)
+        dest[plane][i] = dest[plane][(start - 1) * n - 1 - i];
+}
+
+/*Horizontal pass - RDFT*/
+static void rdft_horizontal(FFTFILTContext *fftfilt, AVFrame *in, int w, int h, int plane)
+{
+    int i, j;
+    fftfilt->rdft = av_rdft_init(fftfilt->rdft_hbits[plane], DFT_R2C);
+    for (i = 0; i < h; i++) {
+        if(!i)
+            memset(fftfilt->rdft_hdata[plane], 0, fftfilt->rdft_hlen[plane] * sizeof(*fftfilt->rdft_hdata[plane]));
+        else
+            copy_rev(fftfilt->rdft_hdata, i, fftfilt->rdft_hlen[plane], plane);
+
+        for (j = 0; j < w; j++)
+            fftfilt->rdft_hdata[plane][i * fftfilt->rdft_hlen[plane] + j] = *(in->data[plane] + in->linesize[plane] * i + j);
+    }
+
+    for (i = 0; i < h; i++)
+        av_rdft_calc(fftfilt->rdft, fftfilt->rdft_hdata[plane] + i * fftfilt->rdft_hlen[plane]);
+    
+    av_rdft_end(fftfilt->rdft);
+}
+
+/*Vertical pass - RDFT*/
+static void rdft_vertical(FFTFILTContext *fftfilt, int h, int plane)
+{
+    int i, j;
+    fftfilt->rdft = av_rdft_init(fftfilt->rdft_vbits[plane], DFT_R2C);
+    for (i = 0; i < fftfilt->rdft_hlen[plane]; i++) {
+        if(!i)
+            memset(fftfilt->rdft_vdata[plane], 0, fftfilt->rdft_vlen[plane] * sizeof(*fftfilt->rdft_vdata[plane]));
+        else
+            copy_rev(fftfilt->rdft_vdata, i, fftfilt->rdft_vlen[plane], plane);
+
+        for (j = 0; j < h; j++)
+            fftfilt->rdft_vdata[plane][i * fftfilt->rdft_vlen[plane] + j] =
+            fftfilt->rdft_hdata[plane][j * fftfilt->rdft_hlen[plane] + i];
+    }
+
+    for (i = 0; i < fftfilt->rdft_hlen[plane]; i++)
+        av_rdft_calc(fftfilt->rdft, fftfilt->rdft_vdata[plane] + i * fftfilt->rdft_vlen[plane]);
+
+    av_rdft_end(fftfilt->rdft);
+}
+/*Vertical pass - IRDFT*/
+static void irdft_vertical(FFTFILTContext *fftfilt, int h, int plane)
+{
+    int i, j;
+    fftfilt->rdft = av_rdft_init(fftfilt->rdft_vbits[plane], IDFT_C2R);
+    for (i = 0; i < fftfilt->rdft_hlen[plane]; i++)
+        av_rdft_calc(fftfilt->rdft, fftfilt->rdft_vdata[plane] + i * fftfilt->rdft_vlen[plane]);
+
+    for (i = 0; i < fftfilt->rdft_hlen[plane]; i++)
+        for (j = 0; j < h; j++)
+            fftfilt->rdft_hdata[plane][j * fftfilt->rdft_hlen[plane] + i] =
+            fftfilt->rdft_vdata[plane][i * fftfilt->rdft_vlen[plane] + j];
+    
+    av_rdft_end(fftfilt->rdft);
+}
+
+/*Horizontal pass - IRDFT*/
+static void irdft_horizontal(FFTFILTContext *fftfilt, AVFrame *out, int w, int h, int plane)
+{
+    int i, j;
+    fftfilt->rdft = av_rdft_init(fftfilt->rdft_hbits[plane], IDFT_C2R);
+    for (i = 0; i < h; i++)
+        av_rdft_calc(fftfilt->rdft, fftfilt->rdft_hdata[plane] + i * fftfilt->rdft_hlen[plane]);
+
+    for (i = 0; i < h; i++)
+        for (j = 0; j < w; j++)
+            *(out->data[plane] + out->linesize[plane] * i + j) = av_clip(fftfilt->rdft_hdata[plane][i
+                                                                         *fftfilt->rdft_hlen[plane] + j] * 4 /
+                                                                         (fftfilt->rdft_hlen[plane] *
+                                                                          fftfilt->rdft_vlen[plane]), 0, 255);
+    
+    av_rdft_end(fftfilt->rdft);
+}
+
+static av_cold int initialize(AVFilterContext *ctx)
+{
+    FFTFILTContext *fftfilt = ctx->priv;
+    int ret = 0, plane;
+
+    if (!fftfilt->dc[U] && !fftfilt->dc[V]) {
+        fftfilt->dc[U] = fftfilt->dc[Y];
+        fftfilt->dc[V] = fftfilt->dc[Y];
+    } else {
+        if (!fftfilt->dc[U]) fftfilt->dc[U] = fftfilt->dc[V];
+        if (!fftfilt->dc[V]) fftfilt->dc[V] = fftfilt->dc[U];
+    }
+
+    if (!fftfilt->lum_str[U] && !fftfilt->lum_str[V]) {
+        fftfilt->lum_str[U] = av_strdup(fftfilt->lum_str[Y]);
+        fftfilt->lum_str[V] = av_strdup(fftfilt->lum_str[Y]);
+    } else {
+        if (!fftfilt->lum_str[U]) fftfilt->lum_str[U] = av_strdup(fftfilt->lum_str[V]);
+        if (!fftfilt->lum_str[V]) fftfilt->lum_str[V] = av_strdup(fftfilt->lum_str[U]);
+    }
+
+    for (plane = 0; plane < 3; plane++) {
+        static double (*p[])(void *, double, double) = { lum_Y, lum_U, lum_V };
+        const char *const func2_names[] = {"lum_Y", "lum_U", "lum_V", NULL };
+        double (*func2[])(void *, double, double) = { lum_Y, lum_U, lum_V, p[plane], NULL };
+
+        ret = av_expr_parse(&fftfilt->lum_expr[plane], fftfilt->lum_str[plane], var_names,
+                            NULL, NULL, func2_names, func2, 0, ctx);
+        if (ret < 0)
+            break;
+    }
+    return ret;
+}
+
+static int config_props(AVFilterLink *inlink)
+{
+    FFTFILTContext *fftfilt = inlink->dst->priv;
+    const AVPixFmtDescriptor *desc;
+    int rdft_hbits, rdft_vbits, i, j, plane;
+    double values[VAR_VARS_NB];
+
+    desc = av_pix_fmt_desc_get(inlink->format);
+    for (i = 0; i < desc->nb_components; i++) {
+        int w = inlink->w;
+        int h = inlink->h;
+
+        /* RDFT - Array initialization for Horizontal pass*/
+        for (rdft_hbits = 1; 1 << rdft_hbits < w; rdft_hbits++);
+        fftfilt->rdft_hbits[i] = rdft_hbits;
+        fftfilt->rdft_hlen[i] = 1 << rdft_hbits;
+        if (!(fftfilt->rdft_hdata[i] = av_malloc_array(h, fftfilt->rdft_hlen[i] * sizeof(FFTSample))))
+            return AVERROR(ENOMEM);
+
+        /* RDFT - Array initialization for Vertical pass*/
+        for (rdft_vbits = 1; 1 << rdft_vbits < h; rdft_vbits++);
+        fftfilt->rdft_vbits[i] = rdft_vbits;
+        fftfilt->rdft_vlen[i] = 1 << rdft_vbits;
+        if (!(fftfilt->rdft_vdata[i] = av_malloc_array(fftfilt->rdft_hlen[i], fftfilt->rdft_vlen[i] * sizeof(FFTSample))))
+            return AVERROR(ENOMEM);
+    }
+
+    /*Luminance value - Array initialization*/
+    values[VAR_W] = inlink->w;
+    values[VAR_H] = inlink->h;
+    for (plane = 0; plane < 3; plane++)
+    {
+        if(!(fftfilt->lum_data[plane] = av_malloc_array(fftfilt->rdft_hlen[plane], fftfilt->rdft_vlen[plane] * sizeof(double))))
+            return AVERROR(ENOMEM);
+        for (i = 0; i < fftfilt->rdft_hlen[plane]; i++)
+        {
+            values[VAR_X] = i;
+            for (j = 0; j < fftfilt->rdft_vlen[plane]; j++)
+            {
+                values[VAR_Y] = j;
+                fftfilt->lum_data[plane][i * fftfilt->rdft_vlen[plane] + j] =
+                av_expr_eval(fftfilt->lum_expr[plane], values, fftfilt);
+            }
+        }
+    }
+    return 0;
+}
+
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
+{
+    AVFilterContext *ctx = inlink->dst;
+    AVFilterLink *outlink = inlink->dst->outputs[0];
+    const AVPixFmtDescriptor *desc;
+    FFTFILTContext *fftfilt = ctx->priv;
+    AVFrame *out;
+    int i, j, plane;
+
+    out = ff_get_video_buffer(outlink, inlink->w, inlink->h);
+    if (!out)
+        return AVERROR(ENOMEM);
+
+    av_frame_copy_props(out, in);
+
+    desc = av_pix_fmt_desc_get(inlink->format);
+    for (plane = 0; plane < desc->nb_components; plane++) {
+        int w = inlink->w;
+        int h = inlink->h;
+
+        if (plane == 1 || plane == 2) {
+            w = FF_CEIL_RSHIFT(w, desc->log2_chroma_w);
+            h = FF_CEIL_RSHIFT(h, desc->log2_chroma_h);
+        }
+
+        rdft_horizontal(fftfilt, in, w, h, plane);
+        rdft_vertical(fftfilt, h, plane);
+        
+        /*Change user defined parameters*/
+        for (i = 0; i < fftfilt->rdft_hlen[plane]; i++)
+            for (j = 0; j < fftfilt->rdft_vlen[plane]; j++)
+                fftfilt->rdft_vdata[plane][i * fftfilt->rdft_vlen[plane] + j] *=
+                  fftfilt->lum_data[plane][i * fftfilt->rdft_vlen[plane] + j];
+        
+        fftfilt->rdft_vdata[plane][0] += fftfilt->rdft_hlen[plane] * fftfilt->rdft_vlen[plane] * fftfilt->dc[plane];
+
+        irdft_vertical(fftfilt, h, plane);
+        irdft_horizontal(fftfilt, out, w, h, plane);
+    }
+
+    av_frame_free(&in);
+    return ff_filter_frame(outlink, out);
+}
+
+static av_cold void uninit(AVFilterContext *ctx)
+{
+    FFTFILTContext *fftfilt = ctx->priv;
+    int i;
+    for (i = 0; i < 3; i++) {
+        av_free(fftfilt->rdft_hdata[i]);
+        av_free(fftfilt->rdft_vdata[i]);
+        av_expr_free(fftfilt->lum_expr[i]);
+        av_free(fftfilt->lum_data[i]);
+    }
+}
+
+static int query_formats(AVFilterContext *ctx)
+{
+    static const enum AVPixelFormat pixel_fmts_fftfilt[] = {
+        AV_PIX_FMT_GRAY8,
+        AV_PIX_FMT_YUV444P,
+        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,
+        .config_props = config_props,
+        .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("Apply arbitrary expressions to samples in frequency domain"),
+    .priv_size       = sizeof(FFTFILTContext),
+    .priv_class      = &fftfilt_class,
+    .inputs          = fftfilt_inputs,
+    .outputs         = fftfilt_outputs,
+    .query_formats   = query_formats,
+    .init            = initialize,
+    .uninit          = uninit,
+};
-- 
1.7.9.5

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

Reply via email to