LCOV - code coverage report
Current view: top level - libavfilter - vf_fftdnoiz.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 366 0.0 %
Date: 2018-05-20 11:54:08 Functions: 0 15 0.0 %

          Line data    Source code
       1             : /*
       2             :  * This file is part of FFmpeg.
       3             :  *
       4             :  * FFmpeg is free software; you can redistribute it and/or
       5             :  * modify it under the terms of the GNU Lesser General Public
       6             :  * License as published by the Free Software Foundation; either
       7             :  * version 2.1 of the License, or (at your option) any later version.
       8             :  *
       9             :  * FFmpeg is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      12             :  * Lesser General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU Lesser General Public
      15             :  * License along with FFmpeg; if not, write to the Free Software
      16             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      17             :  */
      18             : 
      19             : #include <float.h>
      20             : 
      21             : #include "libavutil/avassert.h"
      22             : #include "libavutil/common.h"
      23             : #include "libavutil/imgutils.h"
      24             : #include "libavutil/opt.h"
      25             : #include "libavutil/pixdesc.h"
      26             : #include "internal.h"
      27             : #include "libavcodec/avfft.h"
      28             : 
      29             : enum BufferTypes {
      30             :     CURRENT,
      31             :     PREV,
      32             :     NEXT,
      33             :     BSIZE
      34             : };
      35             : 
      36             : typedef struct PlaneContext {
      37             :     int planewidth, planeheight;
      38             :     int nox, noy;
      39             :     int b;
      40             :     int o;
      41             :     float n;
      42             : 
      43             :     float *buffer[BSIZE];
      44             :     FFTComplex *hdata, *vdata;
      45             :     int data_linesize;
      46             :     int buffer_linesize;
      47             : 
      48             :     FFTContext *fft, *ifft;
      49             : } PlaneContext;
      50             : 
      51             : typedef struct FFTdnoizContext {
      52             :     const AVClass *class;
      53             : 
      54             :     float sigma;
      55             :     float amount;
      56             :     int   block_bits;
      57             :     float overlap;
      58             :     int   nb_prev;
      59             :     int   nb_next;
      60             :     int   planesf;
      61             : 
      62             :     AVFrame *prev, *cur, *next;
      63             : 
      64             :     int depth;
      65             :     int nb_planes;
      66             :     PlaneContext planes[4];
      67             : 
      68             :     void (*import_row)(FFTComplex *dst, uint8_t *src, int rw);
      69             :     void (*export_row)(FFTComplex *src, uint8_t *dst, int rw, float scale, int depth);
      70             : } FFTdnoizContext;
      71             : 
      72             : #define OFFSET(x) offsetof(FFTdnoizContext, x)
      73             : #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
      74             : static const AVOption fftdnoiz_options[] = {
      75             :     { "sigma",   "set denoise strength",
      76             :         OFFSET(sigma),      AV_OPT_TYPE_FLOAT, {.dbl=1},        0,  30, .flags = FLAGS },
      77             :     { "amount",  "set amount of denoising",
      78             :         OFFSET(amount),     AV_OPT_TYPE_FLOAT, {.dbl=1},     0.01,   1, .flags = FLAGS },
      79             :     { "block",   "set block log2(size)",
      80             :         OFFSET(block_bits), AV_OPT_TYPE_INT,   {.i64=4},        3,   6, .flags = FLAGS },
      81             :     { "overlap", "set block overlap",
      82             :         OFFSET(overlap),    AV_OPT_TYPE_FLOAT, {.dbl=0.5},    0.2, 0.8, .flags = FLAGS },
      83             :     { "prev",    "set number of previous frames for temporal denoising",
      84             :         OFFSET(nb_prev),    AV_OPT_TYPE_INT,   {.i64=0},        0,   1, .flags = FLAGS },
      85             :     { "next",    "set number of next frames for temporal denoising",
      86             :         OFFSET(nb_next),    AV_OPT_TYPE_INT,   {.i64=0},        0,   1, .flags = FLAGS },
      87             :     { "planes",  "set planes to filter",
      88             :         OFFSET(planesf),    AV_OPT_TYPE_INT,   {.i64=7},        0,  15, .flags = FLAGS },
      89             :     { NULL }
      90             : };
      91             : 
      92             : AVFILTER_DEFINE_CLASS(fftdnoiz);
      93             : 
      94           0 : static av_cold int init(AVFilterContext *ctx)
      95             : {
      96           0 :     FFTdnoizContext *s = ctx->priv;
      97             :     int i;
      98             : 
      99           0 :     for (i = 0; i < 4; i++) {
     100           0 :         PlaneContext *p = &s->planes[i];
     101             : 
     102           0 :         p->fft  = av_fft_init(s->block_bits, 0);
     103           0 :         p->ifft = av_fft_init(s->block_bits, 1);
     104           0 :         if (!p->fft || !p->ifft)
     105           0 :             return AVERROR(ENOMEM);
     106             :     }
     107             : 
     108           0 :     return 0;
     109             : }
     110             : 
     111           0 : static int query_formats(AVFilterContext *ctx)
     112             : {
     113             :     static const enum AVPixelFormat pix_fmts[] = {
     114             :         AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9,
     115             :         AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12,
     116             :         AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
     117             :         AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
     118             :         AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
     119             :         AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
     120             :         AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
     121             :         AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P,
     122             :         AV_PIX_FMT_YUVJ411P,
     123             :         AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
     124             :         AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
     125             :         AV_PIX_FMT_YUV440P10,
     126             :         AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12,
     127             :         AV_PIX_FMT_YUV440P12,
     128             :         AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV420P14,
     129             :         AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
     130             :         AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
     131             :         AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
     132             :         AV_PIX_FMT_NONE
     133             :     };
     134           0 :     AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
     135           0 :     if (!fmts_list)
     136           0 :         return AVERROR(ENOMEM);
     137           0 :     return ff_set_common_formats(ctx, fmts_list);
     138             : }
     139             : 
     140             : typedef struct ThreadData {
     141             :     float *src, *dst;
     142             : } ThreadData;
     143             : 
     144           0 : static void import_row8(FFTComplex *dst, uint8_t *src, int rw)
     145             : {
     146             :     int j;
     147             : 
     148           0 :     for (j = 0; j < rw; j++) {
     149           0 :         dst[j].re = src[j];
     150           0 :         dst[j].im = 0;
     151             :     }
     152           0 : }
     153             : 
     154           0 : static void export_row8(FFTComplex *src, uint8_t *dst, int rw, float scale, int depth)
     155             : {
     156             :     int j;
     157             : 
     158           0 :     for (j = 0; j < rw; j++)
     159           0 :         dst[j] = av_clip_uint8(src[j].re * scale);
     160           0 : }
     161             : 
     162           0 : static void import_row16(FFTComplex *dst, uint8_t *srcp, int rw)
     163             : {
     164           0 :     uint16_t *src = (uint16_t *)srcp;
     165             :     int j;
     166             : 
     167           0 :     for (j = 0; j < rw; j++) {
     168           0 :         dst[j].re = src[j];
     169           0 :         dst[j].im = 0;
     170             :     }
     171           0 : }
     172             : 
     173           0 : static void export_row16(FFTComplex *src, uint8_t *dstp, int rw, float scale, int depth)
     174             : {
     175           0 :     uint16_t *dst = (uint16_t *)dstp;
     176             :     int j;
     177             : 
     178           0 :     for (j = 0; j < rw; j++)
     179           0 :         dst[j] = av_clip_uintp2(src[j].re * scale, depth);
     180           0 : }
     181             : 
     182           0 : static int config_input(AVFilterLink *inlink)
     183             : {
     184           0 :     AVFilterContext *ctx = inlink->dst;
     185             :     const AVPixFmtDescriptor *desc;
     186           0 :     FFTdnoizContext *s = ctx->priv;
     187             :     int i;
     188             : 
     189           0 :     desc = av_pix_fmt_desc_get(inlink->format);
     190           0 :     s->depth = desc->comp[0].depth;
     191             : 
     192           0 :     if (s->depth <= 8) {
     193           0 :         s->import_row = import_row8;
     194           0 :         s->export_row = export_row8;
     195             :     } else {
     196           0 :         s->import_row = import_row16;
     197           0 :         s->export_row = export_row16;
     198           0 :         s->sigma *= 1 << (s->depth - 8) * (1 + s->nb_prev + s->nb_next);
     199             :     }
     200             : 
     201           0 :     s->planes[1].planewidth = s->planes[2].planewidth = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
     202           0 :     s->planes[0].planewidth = s->planes[3].planewidth = inlink->w;
     203           0 :     s->planes[1].planeheight = s->planes[2].planeheight = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
     204           0 :     s->planes[0].planeheight = s->planes[3].planeheight = inlink->h;
     205             : 
     206           0 :     s->nb_planes = av_pix_fmt_count_planes(inlink->format);
     207             : 
     208           0 :     for (i = 0; i < s->nb_planes; i++) {
     209           0 :         PlaneContext *p = &s->planes[i];
     210             :         int size;
     211             : 
     212           0 :         p->b = 1 << s->block_bits;
     213           0 :         p->n = 1.f / (p->b * p->b);
     214           0 :         p->o = p->b * s->overlap;
     215           0 :         size = p->b - p->o;
     216           0 :         p->nox = (p->planewidth  + (size - 1)) / size;
     217           0 :         p->noy = (p->planeheight + (size - 1)) / size;
     218             : 
     219           0 :         av_log(ctx, AV_LOG_DEBUG, "nox:%d noy:%d size:%d\n", p->nox, p->noy, size);
     220             : 
     221           0 :         p->buffer_linesize = p->b * p->nox * sizeof(FFTComplex);
     222           0 :         p->buffer[CURRENT] = av_calloc(p->b * p->noy, p->buffer_linesize);
     223           0 :         if (!p->buffer[CURRENT])
     224           0 :             return AVERROR(ENOMEM);
     225           0 :         if (s->nb_prev > 0) {
     226           0 :             p->buffer[PREV] = av_calloc(p->b * p->noy, p->buffer_linesize);
     227           0 :             if (!p->buffer[PREV])
     228           0 :                 return AVERROR(ENOMEM);
     229             :         }
     230           0 :         if (s->nb_next > 0) {
     231           0 :             p->buffer[NEXT] = av_calloc(p->b * p->noy, p->buffer_linesize);
     232           0 :             if (!p->buffer[NEXT])
     233           0 :                 return AVERROR(ENOMEM);
     234             :         }
     235           0 :         p->data_linesize = 2 * p->b * sizeof(float);
     236           0 :         p->hdata = av_calloc(p->b, p->data_linesize);
     237           0 :         p->vdata = av_calloc(p->b, p->data_linesize);
     238           0 :         if (!p->hdata || !p->vdata)
     239           0 :             return AVERROR(ENOMEM);
     240             :     }
     241             : 
     242           0 :     return 0;
     243             : }
     244             : 
     245           0 : static void import_plane(FFTdnoizContext *s,
     246             :                          uint8_t *srcp, int src_linesize,
     247             :                          float *buffer, int buffer_linesize, int plane)
     248             : {
     249           0 :     PlaneContext *p = &s->planes[plane];
     250           0 :     const int width = p->planewidth;
     251           0 :     const int height = p->planeheight;
     252           0 :     const int block = p->b;
     253           0 :     const int overlap = p->o;
     254           0 :     const int size = block - overlap;
     255           0 :     const int nox = p->nox;
     256           0 :     const int noy = p->noy;
     257           0 :     const int bpp = (s->depth + 7) / 8;
     258           0 :     const int data_linesize = p->data_linesize / sizeof(FFTComplex);
     259           0 :     FFTComplex *hdata = p->hdata;
     260           0 :     FFTComplex *vdata = p->vdata;
     261             :     int x, y, i, j;
     262             : 
     263           0 :     buffer_linesize /= sizeof(float);
     264           0 :     for (y = 0; y < noy; y++) {
     265           0 :         for (x = 0; x < nox; x++) {
     266           0 :             const int rh = FFMIN(block, height - y * size);
     267           0 :             const int rw = FFMIN(block, width  - x * size);
     268           0 :             uint8_t *src = srcp + src_linesize * y * size + x * size * bpp;
     269           0 :             float *bdst = buffer + buffer_linesize * y * block + x * block * 2;
     270           0 :             FFTComplex *ssrc, *dst = hdata;
     271             : 
     272           0 :             for (i = 0; i < rh; i++) {
     273           0 :                 s->import_row(dst, src, rw);
     274           0 :                 for (j = rw; j < block; j++) {
     275           0 :                     dst[j].re = dst[block - j - 1].re;
     276           0 :                     dst[j].im = 0;
     277             :                 }
     278           0 :                 av_fft_permute(p->fft, dst);
     279           0 :                 av_fft_calc(p->fft, dst);
     280             : 
     281           0 :                 src += src_linesize;
     282           0 :                 dst += data_linesize;
     283             :             }
     284             : 
     285           0 :             dst = hdata;
     286           0 :             for (; i < block; i++) {
     287           0 :                 for (j = 0; j < block; j++) {
     288           0 :                     dst[j].re = dst[(block - i - 1) * data_linesize + j].re;
     289           0 :                     dst[j].im = dst[(block - i - 1) * data_linesize + j].im;
     290             :                 }
     291             :             }
     292             : 
     293           0 :             ssrc = hdata;
     294           0 :             dst = vdata;
     295           0 :             for (i = 0; i < block; i++) {
     296           0 :                 for (j = 0; j < block; j++)
     297           0 :                     dst[j] = ssrc[j * data_linesize + i];
     298           0 :                 av_fft_permute(p->fft, dst);
     299           0 :                 av_fft_calc(p->fft, dst);
     300           0 :                 memcpy(bdst, dst, block * sizeof(FFTComplex));
     301             : 
     302           0 :                 dst += data_linesize;
     303           0 :                 bdst += buffer_linesize;
     304             :             }
     305             :         }
     306             :     }
     307           0 : }
     308             : 
     309           0 : static void export_plane(FFTdnoizContext *s,
     310             :                          uint8_t *dstp, int dst_linesize,
     311             :                          float *buffer, int buffer_linesize, int plane)
     312             : {
     313           0 :     PlaneContext *p = &s->planes[plane];
     314           0 :     const int depth = s->depth;
     315           0 :     const int bpp = (depth + 7) / 8;
     316           0 :     const int width = p->planewidth;
     317           0 :     const int height = p->planeheight;
     318           0 :     const int block = p->b;
     319           0 :     const int overlap = p->o;
     320           0 :     const int hoverlap = overlap / 2;
     321           0 :     const int size = block - overlap;
     322           0 :     const int nox = p->nox;
     323           0 :     const int noy = p->noy;
     324           0 :     const int data_linesize = p->data_linesize / sizeof(FFTComplex);
     325           0 :     const float scale = 1.f / (block * block);
     326           0 :     FFTComplex *hdata = p->hdata;
     327           0 :     FFTComplex *vdata = p->vdata;
     328             :     int x, y, i, j;
     329             : 
     330           0 :     buffer_linesize /= sizeof(float);
     331           0 :     for (y = 0; y < noy; y++) {
     332           0 :         for (x = 0; x < nox; x++) {
     333           0 :             const int woff = x == 0 ? 0 : hoverlap;
     334           0 :             const int hoff = y == 0 ? 0 : hoverlap;
     335           0 :             const int rw = x == 0 ? block : FFMIN(size, width  - x * size - woff);
     336           0 :             const int rh = y == 0 ? block : FFMIN(size, height - y * size - hoff);
     337           0 :             float *bsrc = buffer + buffer_linesize * y * block + x * block * 2;
     338           0 :             uint8_t *dst = dstp + dst_linesize * (y * size + hoff) + (x * size + woff) * bpp;
     339           0 :             FFTComplex *hdst, *ddst = vdata;
     340             : 
     341           0 :             hdst = hdata;
     342           0 :             for (i = 0; i < block; i++) {
     343           0 :                 memcpy(ddst, bsrc, block * sizeof(FFTComplex));
     344           0 :                 av_fft_permute(p->ifft, ddst);
     345           0 :                 av_fft_calc(p->ifft, ddst);
     346           0 :                 for (j = 0; j < block; j++) {
     347           0 :                     hdst[j * data_linesize + i] = ddst[j];
     348             :                 }
     349             : 
     350           0 :                 ddst += data_linesize;
     351           0 :                 bsrc += buffer_linesize;
     352             :             }
     353             : 
     354           0 :             hdst = hdata + hoff * data_linesize;
     355           0 :             for (i = 0; i < rh; i++) {
     356           0 :                 av_fft_permute(p->ifft, hdst);
     357           0 :                 av_fft_calc(p->ifft, hdst);
     358           0 :                 s->export_row(hdst + woff, dst, rw, scale, depth);
     359             : 
     360           0 :                 hdst += data_linesize;
     361           0 :                 dst += dst_linesize;
     362             :             }
     363             :         }
     364             :     }
     365           0 : }
     366             : 
     367           0 : static void filter_plane3d2(FFTdnoizContext *s, int plane, float *pbuffer, float *nbuffer)
     368             : {
     369           0 :     PlaneContext *p = &s->planes[plane];
     370           0 :     const int block = p->b;
     371           0 :     const int nox = p->nox;
     372           0 :     const int noy = p->noy;
     373           0 :     const int buffer_linesize = p->buffer_linesize / sizeof(float);
     374           0 :     const float sigma = s->sigma * s->sigma * block * block;
     375           0 :     const float limit = 1.f - s->amount;
     376           0 :     float *cbuffer = p->buffer[CURRENT];
     377           0 :     const float cfactor = sqrtf(3.f) * 0.5f;
     378           0 :     const float scale = 1.f / 3.f;
     379             :     int y, x, i, j;
     380             : 
     381           0 :     for (y = 0; y < noy; y++) {
     382           0 :         for (x = 0; x < nox; x++) {
     383           0 :             float *cbuff = cbuffer + buffer_linesize * y * block + x * block * 2;
     384           0 :             float *pbuff = pbuffer + buffer_linesize * y * block + x * block * 2;
     385           0 :             float *nbuff = nbuffer + buffer_linesize * y * block + x * block * 2;
     386             : 
     387           0 :             for (i = 0; i < block; i++) {
     388           0 :                 for (j = 0; j < block; j++) {
     389             :                     float sumr, sumi, difr, difi, mpr, mpi, mnr, mni;
     390             :                     float factor, power, sumpnr, sumpni;
     391             : 
     392           0 :                     sumpnr = pbuff[2 * j    ] + nbuff[2 * j    ];
     393           0 :                     sumpni = pbuff[2 * j + 1] + nbuff[2 * j + 1];
     394           0 :                     sumr = cbuff[2 * j    ] + sumpnr;
     395           0 :                     sumi = cbuff[2 * j + 1] + sumpni;
     396           0 :                     difr = cfactor * (nbuff[2 * j    ] - pbuff[2 * j    ]);
     397           0 :                     difi = cfactor * (pbuff[2 * j + 1] - nbuff[2 * j + 1]);
     398           0 :                     mpr = cbuff[2 * j    ] - 0.5f * sumpnr + difi;
     399           0 :                     mnr = mpr - difi - difi;
     400           0 :                     mpi = cbuff[2 * j + 1] - 0.5f * sumpni + difr;
     401           0 :                     mni = mpi - difr - difr;
     402           0 :                     power = sumr * sumr + sumi * sumi + 1e-15f;
     403           0 :                     factor = FFMAX((power - sigma) / power, limit);
     404           0 :                     sumr *= factor;
     405           0 :                     sumi *= factor;
     406           0 :                     power = mpr * mpr + mpi * mpi + 1e-15f;
     407           0 :                     factor = FFMAX((power - sigma) / power, limit);
     408           0 :                     mpr *= factor;
     409           0 :                     mpi *= factor;
     410           0 :                     power = mnr * mnr + mni * mni + 1e-15f;
     411           0 :                     factor = FFMAX((power - sigma) / power, limit);
     412           0 :                     mnr *= factor;
     413           0 :                     mni *= factor;
     414           0 :                     cbuff[2 * j    ] = (sumr + mpr + mnr) * scale;
     415           0 :                     cbuff[2 * j + 1] = (sumi + mpi + mni) * scale;
     416             : 
     417             :                 }
     418             : 
     419           0 :                 cbuff += buffer_linesize;
     420           0 :                 pbuff += buffer_linesize;
     421           0 :                 nbuff += buffer_linesize;
     422             :             }
     423             :         }
     424             :     }
     425           0 : }
     426             : 
     427           0 : static void filter_plane3d1(FFTdnoizContext *s, int plane, float *pbuffer)
     428             : {
     429           0 :     PlaneContext *p = &s->planes[plane];
     430           0 :     const int block = p->b;
     431           0 :     const int nox = p->nox;
     432           0 :     const int noy = p->noy;
     433           0 :     const int buffer_linesize = p->buffer_linesize / sizeof(float);
     434           0 :     const float sigma = s->sigma * s->sigma * block * block;
     435           0 :     const float limit = 1.f - s->amount;
     436           0 :     float *cbuffer = p->buffer[CURRENT];
     437             :     int y, x, i, j;
     438             : 
     439           0 :     for (y = 0; y < noy; y++) {
     440           0 :         for (x = 0; x < nox; x++) {
     441           0 :             float *cbuff = cbuffer + buffer_linesize * y * block + x * block * 2;
     442           0 :             float *pbuff = pbuffer + buffer_linesize * y * block + x * block * 2;
     443             : 
     444           0 :             for (i = 0; i < block; i++) {
     445           0 :                 for (j = 0; j < block; j++) {
     446             :                     float factor, power, re, im, pre, pim;
     447             :                     float sumr, sumi, difr, difi;
     448             : 
     449           0 :                     re = cbuff[j * 2    ];
     450           0 :                     pre = pbuff[j * 2    ];
     451           0 :                     im = cbuff[j * 2 + 1];
     452           0 :                     pim = pbuff[j * 2 + 1];
     453             : 
     454           0 :                     sumr = re + pre;
     455           0 :                     sumi = im + pim;
     456           0 :                     difr = re - pre;
     457           0 :                     difi = im - pim;
     458             : 
     459           0 :                     power = sumr * sumr + sumi * sumi + 1e-15f;
     460           0 :                     factor = FFMAX(limit, (power - sigma) / power);
     461           0 :                     sumr *= factor;
     462           0 :                     sumi *= factor;
     463           0 :                     power = difr * difr + difi * difi + 1e-15f;
     464           0 :                     factor = FFMAX(limit, (power - sigma) / power);
     465           0 :                     difr *= factor;
     466           0 :                     difi *= factor;
     467             : 
     468           0 :                     cbuff[j * 2    ] = (sumr + difr) * 0.5f;
     469           0 :                     cbuff[j * 2 + 1] = (sumi + difi) * 0.5f;
     470             :                 }
     471             : 
     472           0 :                 cbuff += buffer_linesize;
     473           0 :                 pbuff += buffer_linesize;
     474             :             }
     475             :         }
     476             :     }
     477           0 : }
     478             : 
     479           0 : static void filter_plane2d(FFTdnoizContext *s, int plane)
     480             : {
     481           0 :     PlaneContext *p = &s->planes[plane];
     482           0 :     const int block = p->b;
     483           0 :     const int nox = p->nox;
     484           0 :     const int noy = p->noy;
     485           0 :     const int buffer_linesize = p->buffer_linesize / 4;
     486           0 :     const float sigma = s->sigma * s->sigma * block * block;
     487           0 :     const float limit = 1.f - s->amount;
     488           0 :     float *buffer = p->buffer[CURRENT];
     489             :     int y, x, i, j;
     490             : 
     491           0 :     for (y = 0; y < noy; y++) {
     492           0 :         for (x = 0; x < nox; x++) {
     493           0 :             float *buff = buffer + buffer_linesize * y * block + x * block * 2;
     494             : 
     495           0 :             for (i = 0; i < block; i++) {
     496           0 :                 for (j = 0; j < block; j++) {
     497             :                     float factor, power, re, im;
     498             : 
     499           0 :                     re = buff[j * 2    ];
     500           0 :                     im = buff[j * 2 + 1];
     501           0 :                     power = re * re + im * im + 1e-15f;
     502           0 :                     factor = FFMAX(limit, (power - sigma) / power);
     503           0 :                     buff[j * 2    ] *= factor;
     504           0 :                     buff[j * 2 + 1] *= factor;
     505             :                 }
     506             : 
     507           0 :                 buff += buffer_linesize;
     508             :             }
     509             :         }
     510             :     }
     511           0 : }
     512             : 
     513           0 : static int filter_frame(AVFilterLink *inlink, AVFrame *in)
     514             : {
     515           0 :     AVFilterContext *ctx = inlink->dst;
     516           0 :     FFTdnoizContext *s = ctx->priv;
     517           0 :     AVFilterLink *outlink = ctx->outputs[0];
     518             :     int direct, plane;
     519             :     AVFrame *out;
     520             : 
     521           0 :     if (s->nb_next > 0 && s->nb_prev > 0) {
     522           0 :         av_frame_free(&s->prev);
     523           0 :         s->prev = s->cur;
     524           0 :         s->cur = s->next;
     525           0 :         s->next = in;
     526             : 
     527           0 :         if (!s->prev && s->cur) {
     528           0 :             s->prev = av_frame_clone(s->cur);
     529           0 :             if (!s->prev)
     530           0 :                 return AVERROR(ENOMEM);
     531             :         }
     532           0 :         if (!s->cur)
     533           0 :             return 0;
     534           0 :     } else if (s->nb_next > 0) {
     535           0 :         av_frame_free(&s->cur);
     536           0 :         s->cur = s->next;
     537           0 :         s->next = in;
     538             : 
     539           0 :         if (!s->cur)
     540           0 :             return 0;
     541           0 :     } else if (s->nb_prev > 0) {
     542           0 :         av_frame_free(&s->prev);
     543           0 :         s->prev = s->cur;
     544           0 :         s->cur = in;
     545             : 
     546           0 :         if (!s->prev)
     547           0 :             s->prev = av_frame_clone(s->cur);
     548           0 :         if (!s->prev)
     549           0 :             return AVERROR(ENOMEM);
     550             :     } else {
     551           0 :         s->cur = in;
     552             :     }
     553             : 
     554           0 :     if (av_frame_is_writable(in) && s->nb_next == 0 && s->nb_prev == 0) {
     555           0 :         direct = 1;
     556           0 :         out = in;
     557             :     } else {
     558           0 :         direct = 0;
     559           0 :         out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
     560           0 :         if (!out)
     561           0 :             return AVERROR(ENOMEM);
     562           0 :         av_frame_copy_props(out, s->cur);
     563             :     }
     564             : 
     565           0 :     for (plane = 0; plane < s->nb_planes; plane++) {
     566           0 :         PlaneContext *p = &s->planes[plane];
     567             : 
     568           0 :         if (!((1 << plane) & s->planesf) || ctx->is_disabled) {
     569           0 :             if (!direct)
     570           0 :                 av_image_copy_plane(out->data[plane], out->linesize[plane],
     571           0 :                                     s->cur->data[plane], s->cur->linesize[plane],
     572             :                                     p->planewidth, p->planeheight);
     573           0 :             continue;
     574             :         }
     575             : 
     576           0 :         if (s->next) {
     577           0 :             import_plane(s, s->next->data[plane], s->next->linesize[plane],
     578             :                          p->buffer[NEXT], p->buffer_linesize, plane);
     579             :         }
     580             : 
     581           0 :         if (s->prev) {
     582           0 :             import_plane(s, s->prev->data[plane], s->prev->linesize[plane],
     583             :                          p->buffer[PREV], p->buffer_linesize, plane);
     584             :         }
     585             : 
     586           0 :         import_plane(s, s->cur->data[plane], s->cur->linesize[plane],
     587             :                      p->buffer[CURRENT], p->buffer_linesize, plane);
     588             : 
     589           0 :         if (s->next && s->prev) {
     590           0 :             filter_plane3d2(s, plane, p->buffer[PREV], p->buffer[NEXT]);
     591           0 :         } else if (s->next) {
     592           0 :             filter_plane3d1(s, plane, p->buffer[NEXT]);
     593           0 :         } else  if (s->prev) {
     594           0 :             filter_plane3d1(s, plane, p->buffer[PREV]);
     595             :         } else {
     596           0 :             filter_plane2d(s, plane);
     597             :         }
     598             : 
     599           0 :         export_plane(s, out->data[plane], out->linesize[plane],
     600             :                      p->buffer[CURRENT], p->buffer_linesize, plane);
     601             :     }
     602             : 
     603           0 :     if (s->nb_next == 0 && s->nb_prev == 0) {
     604           0 :         if (direct) {
     605           0 :             s->cur = NULL;
     606             :         } else {
     607           0 :             av_frame_free(&s->cur);
     608             :         }
     609             :     }
     610           0 :     return ff_filter_frame(outlink, out);
     611             : }
     612             : 
     613           0 : static int request_frame(AVFilterLink *outlink)
     614             : {
     615           0 :     AVFilterContext *ctx = outlink->src;
     616           0 :     FFTdnoizContext *s = ctx->priv;
     617           0 :     int ret = 0;
     618             : 
     619           0 :     ret = ff_request_frame(ctx->inputs[0]);
     620             : 
     621           0 :     if (ret == AVERROR_EOF && (s->nb_next > 0)) {
     622             :         AVFrame *buf;
     623             : 
     624           0 :         if (s->next && s->nb_next > 0)
     625           0 :             buf = av_frame_clone(s->next);
     626           0 :         else if (s->cur)
     627           0 :             buf = av_frame_clone(s->cur);
     628             :         else
     629           0 :             buf = av_frame_clone(s->prev);
     630           0 :         if (!buf)
     631           0 :             return AVERROR(ENOMEM);
     632             : 
     633           0 :         ret = filter_frame(ctx->inputs[0], buf);
     634           0 :         if (ret < 0)
     635           0 :             return ret;
     636           0 :         ret = AVERROR_EOF;
     637             :     }
     638             : 
     639           0 :     return ret;
     640             : }
     641             : 
     642           0 : static av_cold void uninit(AVFilterContext *ctx)
     643             : {
     644           0 :     FFTdnoizContext *s = ctx->priv;
     645             :     int i;
     646             : 
     647           0 :     for (i = 0; i < 4; i++) {
     648           0 :         PlaneContext *p = &s->planes[i];
     649             : 
     650           0 :         av_freep(&p->hdata);
     651           0 :         av_freep(&p->vdata);
     652           0 :         av_freep(&p->buffer[PREV]);
     653           0 :         av_freep(&p->buffer[CURRENT]);
     654           0 :         av_freep(&p->buffer[NEXT]);
     655           0 :         av_fft_end(p->fft);
     656           0 :         av_fft_end(p->ifft);
     657             :     }
     658             : 
     659           0 :     av_frame_free(&s->prev);
     660           0 :     av_frame_free(&s->cur);
     661           0 :     av_frame_free(&s->next);
     662           0 : }
     663             : 
     664             : static const AVFilterPad fftdnoiz_inputs[] = {
     665             :     {
     666             :         .name         = "default",
     667             :         .type         = AVMEDIA_TYPE_VIDEO,
     668             :         .filter_frame = filter_frame,
     669             :         .config_props = config_input,
     670             :     },
     671             :     { NULL }
     672             : };
     673             : 
     674             : static const AVFilterPad fftdnoiz_outputs[] = {
     675             :     {
     676             :         .name          = "default",
     677             :         .type          = AVMEDIA_TYPE_VIDEO,
     678             :         .request_frame = request_frame,
     679             :     },
     680             :     { NULL }
     681             : };
     682             : 
     683             : AVFilter ff_vf_fftdnoiz = {
     684             :     .name          = "fftdnoiz",
     685             :     .description   = NULL_IF_CONFIG_SMALL("Denoise frames using 3D FFT."),
     686             :     .priv_size     = sizeof(FFTdnoizContext),
     687             :     .init          = init,
     688             :     .uninit        = uninit,
     689             :     .query_formats = query_formats,
     690             :     .inputs        = fftdnoiz_inputs,
     691             :     .outputs       = fftdnoiz_outputs,
     692             :     .priv_class    = &fftdnoiz_class,
     693             :     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
     694             : };

Generated by: LCOV version 1.13