4arwa

fft-filtering

Feb 13th, 2015
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.57 KB | None | 0 0
  1. /*
  2.  * Copyright (c) 2015 Arwa Arif <[email protected]>
  3.  *
  4.  * This file is part of FFmpeg.
  5.  *
  6.  * FFmpeg is free software; you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation; either version 2 of the License, or
  9.  * (at your option) any later version.
  10.  *
  11.  * FFmpeg is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License along
  17.  * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
  18.  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19.  */
  20.  
  21. /**
  22.  * @file
  23.  * FFT domain filtering.
  24.  */
  25.  
  26. #include "libavfilter/internal.h"
  27. #include "libavutil/common.h"
  28. #include "libavutil/imgutils.h"
  29. #include "libavutil/opt.h"
  30. #include "libavutil/pixdesc.h"
  31. #include "libavcodec/avfft.h"
  32.  
  33. typedef struct {
  34.     const AVClass *class;
  35.  
  36.     RDFTContext *rdft;
  37.     int rdft_bits;
  38.     FFTSample *rdft_data;
  39.  
  40. } FFTFILTContext;
  41.  
  42. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  43. {
  44.     AVFilterContext *ctx = inlink->dst;
  45.     AVFilterLink *outlink = inlink->dst->outputs[0];
  46.     FFTFILTContext *fftfilt = ctx->priv;
  47.     AVFrame *out;
  48.     int i, rdft_bits;
  49.     size_t rdft_len, w, h;
  50.  
  51.     w = inlink->w;
  52.     h = inlink->h;
  53.  
  54.     /* RDFT window size (precision) according to the requested output frame height */
  55.     for (rdft_bits = 1; 1 << rdft_bits < 2 * w; rdft_bits++);
  56.     rdft_len = 1 << rdft_bits;
  57.  
  58.     fftfilt->rdft_data = av_malloc_array(h, rdft_len);
  59.  
  60.     for (i = 0; i < h; i++)
  61.         memset(fftfilt->rdft_data, 0, rdft_len * h);
  62.  
  63.     out = ff_get_video_buffer(outlink, inlink->w, inlink->h);
  64.     if (!out)
  65.         return AVERROR(ENOMEM);
  66.  
  67.     av_frame_copy_props(out, in);
  68.  
  69.     /*Horizontal pass - RDFT*/
  70.     for (i = 0; i < h; i++)
  71.         fftfilt->rdft_data[i] = *(in->data[0] + in->linesize[0] * i);
  72.  
  73.     for (i = 0; i < h; i++)
  74.         av_rdft_calc(fftfilt->rdft, fftfilt->rdft_data + i * rdft_len);
  75.    
  76.     av_rdft_end(fftfilt->rdft);
  77.  
  78.     /*Horizontal pass - IRDFT*/
  79.     fftfilt->rdft = av_rdft_init(rdft_bits, IDFT_C2R);
  80.  
  81.     for (i = 0; i < h; i++)
  82.         av_rdft_calc(fftfilt->rdft, fftfilt->rdft_data + i*rdft_len);
  83.  
  84.     for (i = 0; i < h; i++)
  85.         *(out->data[0] + out->linesize[0] * i) = fftfilt->rdft_data[i];
  86.    
  87.     av_rdft_end(fftfilt->rdft);
  88.     av_free(fftfilt->rdft_data);
  89.  
  90.     av_frame_free(&in);
  91.  
  92.     return ff_filter_frame(outlink, out);
  93. }
  94.  
  95. static int query_formats(AVFilterContext *ctx)
  96. {
  97.     static const enum AVPixelFormat pixel_fmts_fftfilt[] = {
  98.         AV_PIX_FMT_GRAY8,
  99.         AV_PIX_FMT_NONE
  100.     };
  101.  
  102.     ff_set_common_formats(ctx, ff_make_format_list(pixel_fmts_fftfilt));
  103.  
  104.     return 0;
  105. }
  106.  
  107. static const AVFilterPad fftfilt_inputs[] = {
  108.     {
  109.         .name = "default",
  110.         .type = AVMEDIA_TYPE_VIDEO,
  111.         .filter_frame = filter_frame,
  112.     },
  113.     { NULL }
  114. };
  115.  
  116. static const AVFilterPad fftfilt_outputs[] = {
  117.     {
  118.         .name = "default",
  119.         .type = AVMEDIA_TYPE_VIDEO,
  120.     },
  121.     { NULL }
  122. };
  123.  
  124. AVFilter ff_vf_fftfilt = {
  125.     .name            = "fftfilt",
  126.     .description     = NULL_IF_CONFIG_SMALL("Adjust gain."),
  127.     .priv_size       = sizeof(FFTFILTContext),
  128.   //  .priv_class      = &fftfilt_class,
  129.     .inputs          = fftfilt_inputs,
  130.     .outputs         = fftfilt_outputs,
  131.     .query_formats   = query_formats,
  132. };
Advertisement
Add Comment
Please, Sign In to add comment