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

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2003 LeFunGus, lefungus@altern.org
       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             : #include <float.h>
      22             : 
      23             : #include "libavutil/imgutils.h"
      24             : #include "libavutil/attributes.h"
      25             : #include "libavutil/common.h"
      26             : #include "libavutil/pixdesc.h"
      27             : #include "libavutil/intreadwrite.h"
      28             : #include "libavutil/opt.h"
      29             : 
      30             : #include "avfilter.h"
      31             : #include "formats.h"
      32             : #include "internal.h"
      33             : #include "video.h"
      34             : 
      35             : typedef struct VagueDenoiserContext {
      36             :     const AVClass *class;
      37             : 
      38             :     float threshold;
      39             :     float percent;
      40             :     int method;
      41             :     int nsteps;
      42             :     int planes;
      43             : 
      44             :     int depth;
      45             :     int bpc;
      46             :     int peak;
      47             :     int nb_planes;
      48             :     int planeheight[4];
      49             :     int planewidth[4];
      50             : 
      51             :     float *block;
      52             :     float *in;
      53             :     float *out;
      54             :     float *tmp;
      55             : 
      56             :     int hlowsize[4][32];
      57             :     int hhighsize[4][32];
      58             :     int vlowsize[4][32];
      59             :     int vhighsize[4][32];
      60             : 
      61             :     void (*thresholding)(float *block, const int width, const int height,
      62             :                          const int stride, const float threshold,
      63             :                          const float percent, const int nsteps);
      64             : } VagueDenoiserContext;
      65             : 
      66             : #define OFFSET(x) offsetof(VagueDenoiserContext, x)
      67             : #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
      68             : static const AVOption vaguedenoiser_options[] = {
      69             :     { "threshold", "set filtering strength",   OFFSET(threshold), AV_OPT_TYPE_FLOAT, {.dbl=2.},  0,DBL_MAX, FLAGS },
      70             :     { "method",    "set filtering method",     OFFSET(method),    AV_OPT_TYPE_INT,   {.i64=2 },  0, 2,      FLAGS, "method" },
      71             :         { "hard",   "hard thresholding",       0,                 AV_OPT_TYPE_CONST, {.i64=0},   0, 0,      FLAGS, "method" },
      72             :         { "soft",   "soft thresholding",       0,                 AV_OPT_TYPE_CONST, {.i64=1},   0, 0,      FLAGS, "method" },
      73             :         { "garrote", "garotte thresholding",   0,                 AV_OPT_TYPE_CONST, {.i64=2},   0, 0,      FLAGS, "method" },
      74             :     { "nsteps",    "set number of steps",      OFFSET(nsteps),    AV_OPT_TYPE_INT,   {.i64=6 },  1, 32,     FLAGS },
      75             :     { "percent", "set percent of full denoising", OFFSET(percent),AV_OPT_TYPE_FLOAT, {.dbl=85},  0,100,     FLAGS },
      76             :     { "planes",    "set planes to filter",     OFFSET(planes),    AV_OPT_TYPE_INT,   {.i64=15 }, 0, 15,     FLAGS },
      77             :     { NULL }
      78             : };
      79             : 
      80             : AVFILTER_DEFINE_CLASS(vaguedenoiser);
      81             : 
      82             : #define NPAD 10
      83             : 
      84             : static const float analysis_low[9] = {
      85             :     0.037828455506995f, -0.023849465019380f, -0.110624404418423f, 0.377402855612654f,
      86             :     0.852698679009403f, 0.377402855612654f, -0.110624404418423f, -0.023849465019380f, 0.037828455506995f
      87             : };
      88             : 
      89             : static const float analysis_high[7] = {
      90             :     -0.064538882628938f, 0.040689417609558f, 0.418092273222212f, -0.788485616405664f,
      91             :     0.418092273222212f, 0.040689417609558f, -0.064538882628938f
      92             : };
      93             : 
      94             : static const float synthesis_low[7] = {
      95             :     -0.064538882628938f, -0.040689417609558f, 0.418092273222212f, 0.788485616405664f,
      96             :     0.418092273222212f, -0.040689417609558f, -0.064538882628938f
      97             : };
      98             : 
      99             : static const float synthesis_high[9] = {
     100             :     -0.037828455506995f, -0.023849465019380f, 0.110624404418423f, 0.377402855612654f,
     101             :     -0.852698679009403f, 0.377402855612654f, 0.110624404418423f, -0.023849465019380f, -0.037828455506995f
     102             : };
     103             : 
     104           0 : static int query_formats(AVFilterContext *ctx)
     105             : {
     106             :     static const enum AVPixelFormat pix_fmts[] = {
     107             :         AV_PIX_FMT_GRAY8,
     108             :         AV_PIX_FMT_GRAY16,
     109             :         AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
     110             :         AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
     111             :         AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
     112             :         AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
     113             :         AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P,
     114             :         AV_PIX_FMT_YUVJ411P,
     115             :         AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
     116             :         AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
     117             :         AV_PIX_FMT_YUV440P10,
     118             :         AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12,
     119             :         AV_PIX_FMT_YUV440P12,
     120             :         AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV420P14,
     121             :         AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
     122             :         AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
     123             :         AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
     124             :         AV_PIX_FMT_NONE
     125             :     };
     126           0 :     AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
     127           0 :     if (!fmts_list)
     128           0 :         return AVERROR(ENOMEM);
     129           0 :     return ff_set_common_formats(ctx, fmts_list);
     130             : }
     131             : 
     132           0 : static int config_input(AVFilterLink *inlink)
     133             : {
     134           0 :     VagueDenoiserContext *s = inlink->dst->priv;
     135           0 :     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
     136             :     int p, i, nsteps_width, nsteps_height, nsteps_max;
     137             : 
     138           0 :     s->depth = desc->comp[0].depth;
     139           0 :     s->bpc = (s->depth + 7) / 8;
     140           0 :     s->nb_planes = desc->nb_components;
     141             : 
     142           0 :     s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
     143           0 :     s->planeheight[0] = s->planeheight[3] = inlink->h;
     144           0 :     s->planewidth[1]  = s->planewidth[2]  = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
     145           0 :     s->planewidth[0]  = s->planewidth[3]  = inlink->w;
     146             : 
     147           0 :     s->block = av_malloc_array(inlink->w * inlink->h, sizeof(*s->block));
     148           0 :     s->in    = av_malloc_array(32 + FFMAX(inlink->w, inlink->h), sizeof(*s->in));
     149           0 :     s->out   = av_malloc_array(32 + FFMAX(inlink->w, inlink->h), sizeof(*s->out));
     150           0 :     s->tmp   = av_malloc_array(32 + FFMAX(inlink->w, inlink->h), sizeof(*s->tmp));
     151             : 
     152           0 :     if (!s->block || !s->in || !s->out || !s->tmp)
     153           0 :         return AVERROR(ENOMEM);
     154             : 
     155           0 :     s->threshold *= 1 << (s->depth - 8);
     156           0 :     s->peak = (1 << s->depth) - 1;
     157             : 
     158           0 :     nsteps_width  = ((s->planes & 2 || s->planes & 4) && s->nb_planes > 1) ? s->planewidth[1] : s->planewidth[0];
     159           0 :     nsteps_height = ((s->planes & 2 || s->planes & 4) && s->nb_planes > 1) ? s->planeheight[1] : s->planeheight[0];
     160             : 
     161           0 :     for (nsteps_max = 1; nsteps_max < 15; nsteps_max++) {
     162           0 :         if (pow(2, nsteps_max) >= nsteps_width || pow(2, nsteps_max) >= nsteps_height)
     163             :             break;
     164             :     }
     165             : 
     166           0 :     s->nsteps = FFMIN(s->nsteps, nsteps_max - 2);
     167             : 
     168           0 :     for (p = 0; p < 4; p++) {
     169           0 :         s->hlowsize[p][0]  = (s->planewidth[p] + 1) >> 1;
     170           0 :         s->hhighsize[p][0] =  s->planewidth[p] >> 1;
     171           0 :         s->vlowsize[p][0]  = (s->planeheight[p] + 1) >> 1;
     172           0 :         s->vhighsize[p][0] =  s->planeheight[p] >> 1;
     173             : 
     174           0 :         for (i = 1; i < s->nsteps; i++) {
     175           0 :             s->hlowsize[p][i]  = (s->hlowsize[p][i - 1] + 1) >> 1;
     176           0 :             s->hhighsize[p][i] =  s->hlowsize[p][i - 1] >> 1;
     177           0 :             s->vlowsize[p][i]  = (s->vlowsize[p][i - 1] + 1) >> 1;
     178           0 :             s->vhighsize[p][i] =  s->vlowsize[p][i - 1] >> 1;
     179             :         }
     180             :     }
     181             : 
     182           0 :     return 0;
     183             : }
     184             : 
     185           0 : static inline void copy(const float *p1, float *p2, const int length)
     186             : {
     187           0 :     memcpy(p2, p1, length * sizeof(float));
     188           0 : }
     189             : 
     190           0 : static inline void copyv(const float *p1, const int stride1, float *p2, const int length)
     191             : {
     192             :     int i;
     193             : 
     194           0 :     for (i = 0; i < length; i++) {
     195           0 :         p2[i] = *p1;
     196           0 :         p1 += stride1;
     197             :     }
     198           0 : }
     199             : 
     200           0 : static inline void copyh(const float *p1, float *p2, const int stride2, const int length)
     201             : {
     202             :     int i;
     203             : 
     204           0 :     for (i = 0; i < length; i++) {
     205           0 :         *p2 = p1[i];
     206           0 :         p2 += stride2;
     207             :     }
     208           0 : }
     209             : 
     210             : // Do symmetric extension of data using prescribed symmetries
     211             : // Original values are in output[npad] through output[npad+size-1]
     212             : // New values will be placed in output[0] through output[npad] and in output[npad+size] through output[2*npad+size-1] (note: end values may not be filled in)
     213             : // extension at left bdry is ... 3 2 1 0 | 0 1 2 3 ...
     214             : // same for right boundary
     215             : // if right_ext=1 then ... 3 2 1 0 | 1 2 3
     216           0 : static void symmetric_extension(float *output, const int size, const int left_ext, const int right_ext)
     217             : {
     218           0 :     int first = NPAD;
     219           0 :     int last = NPAD - 1 + size;
     220           0 :     const int originalLast = last;
     221             :     int i, nextend, idx;
     222             : 
     223           0 :     if (left_ext == 2)
     224           0 :         output[--first] = output[NPAD];
     225           0 :     if (right_ext == 2)
     226           0 :         output[++last] = output[originalLast];
     227             : 
     228             :     // extend left end
     229           0 :     nextend = first;
     230           0 :     for (i = 0; i < nextend; i++)
     231           0 :         output[--first] = output[NPAD + 1 + i];
     232             : 
     233           0 :     idx = NPAD + NPAD - 1 + size;
     234             : 
     235             :     // extend right end
     236           0 :     nextend = idx - last;
     237           0 :     for (i = 0; i < nextend; i++)
     238           0 :         output[++last] = output[originalLast - 1 - i];
     239           0 : }
     240             : 
     241           0 : static void transform_step(float *input, float *output, const int size, const int low_size, VagueDenoiserContext *s)
     242             : {
     243             :     int i;
     244             : 
     245           0 :     symmetric_extension(input, size, 1, 1);
     246             : 
     247           0 :     for (i = NPAD; i < NPAD + low_size; i++) {
     248           0 :         const float a = input[2 * i - 14] * analysis_low[0];
     249           0 :         const float b = input[2 * i - 13] * analysis_low[1];
     250           0 :         const float c = input[2 * i - 12] * analysis_low[2];
     251           0 :         const float d = input[2 * i - 11] * analysis_low[3];
     252           0 :         const float e = input[2 * i - 10] * analysis_low[4];
     253           0 :         const float f = input[2 * i -  9] * analysis_low[3];
     254           0 :         const float g = input[2 * i -  8] * analysis_low[2];
     255           0 :         const float h = input[2 * i -  7] * analysis_low[1];
     256           0 :         const float k = input[2 * i -  6] * analysis_low[0];
     257             : 
     258           0 :         output[i] = a + b + c + d + e + f + g + h + k;
     259             :     }
     260             : 
     261           0 :     for (i = NPAD; i < NPAD + low_size; i++) {
     262           0 :         const float a = input[2 * i - 12] * analysis_high[0];
     263           0 :         const float b = input[2 * i - 11] * analysis_high[1];
     264           0 :         const float c = input[2 * i - 10] * analysis_high[2];
     265           0 :         const float d = input[2 * i -  9] * analysis_high[3];
     266           0 :         const float e = input[2 * i -  8] * analysis_high[2];
     267           0 :         const float f = input[2 * i -  7] * analysis_high[1];
     268           0 :         const float g = input[2 * i -  6] * analysis_high[0];
     269             : 
     270           0 :         output[i + low_size] = a + b + c + d + e + f + g;
     271             :     }
     272           0 : }
     273             : 
     274           0 : static void invert_step(const float *input, float *output, float *temp, const int size, VagueDenoiserContext *s)
     275             : {
     276           0 :     const int low_size = (size + 1) >> 1;
     277           0 :     const int high_size = size >> 1;
     278           0 :     int left_ext = 1, right_ext, i;
     279             :     int findex;
     280             : 
     281           0 :     memcpy(temp + NPAD, input + NPAD, low_size * sizeof(float));
     282             : 
     283           0 :     right_ext = (size % 2 == 0) ? 2 : 1;
     284           0 :     symmetric_extension(temp, low_size, left_ext, right_ext);
     285             : 
     286           0 :     memset(output, 0, (NPAD + NPAD + size) * sizeof(float));
     287           0 :     findex = (size + 2) >> 1;
     288             : 
     289           0 :     for (i = 9; i < findex + 11; i++) {
     290           0 :         const float a = temp[i] * synthesis_low[0];
     291           0 :         const float b = temp[i] * synthesis_low[1];
     292           0 :         const float c = temp[i] * synthesis_low[2];
     293           0 :         const float d = temp[i] * synthesis_low[3];
     294             : 
     295           0 :         output[2 * i - 13] += a;
     296           0 :         output[2 * i - 12] += b;
     297           0 :         output[2 * i - 11] += c;
     298           0 :         output[2 * i - 10] += d;
     299           0 :         output[2 * i -  9] += c;
     300           0 :         output[2 * i -  8] += b;
     301           0 :         output[2 * i -  7] += a;
     302             :     }
     303             : 
     304           0 :     memcpy(temp + NPAD, input + NPAD + low_size, high_size * sizeof(float));
     305             : 
     306           0 :     left_ext = 2;
     307           0 :     right_ext = (size % 2 == 0) ? 1 : 2;
     308           0 :     symmetric_extension(temp, high_size, left_ext, right_ext);
     309             : 
     310           0 :     for (i = 8; i < findex + 11; i++) {
     311           0 :         const float a = temp[i] * synthesis_high[0];
     312           0 :         const float b = temp[i] * synthesis_high[1];
     313           0 :         const float c = temp[i] * synthesis_high[2];
     314           0 :         const float d = temp[i] * synthesis_high[3];
     315           0 :         const float e = temp[i] * synthesis_high[4];
     316             : 
     317           0 :         output[2 * i - 13] += a;
     318           0 :         output[2 * i - 12] += b;
     319           0 :         output[2 * i - 11] += c;
     320           0 :         output[2 * i - 10] += d;
     321           0 :         output[2 * i -  9] += e;
     322           0 :         output[2 * i -  8] += d;
     323           0 :         output[2 * i -  7] += c;
     324           0 :         output[2 * i -  6] += b;
     325           0 :         output[2 * i -  5] += a;
     326             :     }
     327           0 : }
     328             : 
     329           0 : static void hard_thresholding(float *block, const int width, const int height,
     330             :                               const int stride, const float threshold,
     331             :                               const float percent, const int unused)
     332             : {
     333           0 :     const float frac = 1.f - percent * 0.01f;
     334             :     int y, x;
     335             : 
     336           0 :     for (y = 0; y < height; y++) {
     337           0 :         for (x = 0; x < width; x++) {
     338           0 :             if (FFABS(block[x]) <= threshold)
     339           0 :                 block[x] *= frac;
     340             :         }
     341           0 :         block += stride;
     342             :     }
     343           0 : }
     344             : 
     345           0 : static void soft_thresholding(float *block, const int width, const int height, const int stride,
     346             :                               const float threshold, const float percent, const int nsteps)
     347             : {
     348           0 :     const float frac = 1.f - percent * 0.01f;
     349           0 :     const float shift = threshold * 0.01f * percent;
     350           0 :     int w = width;
     351           0 :     int h = height;
     352             :     int y, x, l;
     353             : 
     354           0 :     for (l = 0; l < nsteps; l++) {
     355           0 :         w = (w + 1) >> 1;
     356           0 :         h = (h + 1) >> 1;
     357             :     }
     358             : 
     359           0 :     for (y = 0; y < height; y++) {
     360           0 :         const int x0 = (y < h) ? w : 0;
     361           0 :         for (x = x0; x < width; x++) {
     362           0 :             const float temp = FFABS(block[x]);
     363           0 :             if (temp <= threshold)
     364           0 :                 block[x] *= frac;
     365             :             else
     366           0 :                 block[x] = (block[x] < 0.f ? -1.f : (block[x] > 0.f ? 1.f : 0.f)) * (temp - shift);
     367             :         }
     368           0 :         block += stride;
     369             :     }
     370           0 : }
     371             : 
     372           0 : static void qian_thresholding(float *block, const int width, const int height,
     373             :                               const int stride, const float threshold,
     374             :                               const float percent, const int unused)
     375             : {
     376           0 :     const float percent01 = percent * 0.01f;
     377           0 :     const float tr2 = threshold * threshold * percent01;
     378           0 :     const float frac = 1.f - percent01;
     379             :     int y, x;
     380             : 
     381           0 :     for (y = 0; y < height; y++) {
     382           0 :         for (x = 0; x < width; x++) {
     383           0 :             const float temp = FFABS(block[x]);
     384           0 :             if (temp <= threshold) {
     385           0 :                 block[x] *= frac;
     386             :             } else {
     387           0 :                 const float tp2 = temp * temp;
     388           0 :                 block[x] *= (tp2 - tr2) / tp2;
     389             :             }
     390             :         }
     391           0 :         block += stride;
     392             :     }
     393           0 : }
     394             : 
     395           0 : static void filter(VagueDenoiserContext *s, AVFrame *in, AVFrame *out)
     396             : {
     397             :     int p, y, x, i, j;
     398             : 
     399           0 :     for (p = 0; p < s->nb_planes; p++) {
     400           0 :         const int height = s->planeheight[p];
     401           0 :         const int width = s->planewidth[p];
     402           0 :         const uint8_t *srcp8 = in->data[p];
     403           0 :         const uint16_t *srcp16 = (const uint16_t *)in->data[p];
     404           0 :         uint8_t *dstp8 = out->data[p];
     405           0 :         uint16_t *dstp16 = (uint16_t *)out->data[p];
     406           0 :         float *output = s->block;
     407           0 :         int h_low_size0 = width;
     408           0 :         int v_low_size0 = height;
     409           0 :         int nsteps_transform = s->nsteps;
     410           0 :         int nsteps_invert = s->nsteps;
     411           0 :         const float *input = s->block;
     412             : 
     413           0 :         if (!((1 << p) & s->planes)) {
     414           0 :             av_image_copy_plane(out->data[p], out->linesize[p], in->data[p], in->linesize[p],
     415           0 :                                 s->planewidth[p] * s->bpc, s->planeheight[p]);
     416           0 :             continue;
     417             :         }
     418             : 
     419           0 :         if (s->depth <= 8) {
     420           0 :             for (y = 0; y < height; y++) {
     421           0 :                 for (x = 0; x < width; x++)
     422           0 :                     output[x] = srcp8[x];
     423           0 :                 srcp8 += in->linesize[p];
     424           0 :                 output += width;
     425             :             }
     426             :         } else {
     427           0 :             for (y = 0; y < height; y++) {
     428           0 :                 for (x = 0; x < width; x++)
     429           0 :                     output[x] = srcp16[x];
     430           0 :                 srcp16 += in->linesize[p] / 2;
     431           0 :                 output += width;
     432             :             }
     433             :         }
     434             : 
     435           0 :         while (nsteps_transform--) {
     436           0 :             int low_size = (h_low_size0 + 1) >> 1;
     437           0 :             float *input = s->block;
     438           0 :             for (j = 0; j < v_low_size0; j++) {
     439           0 :                 copy(input, s->in + NPAD, h_low_size0);
     440           0 :                 transform_step(s->in, s->out, h_low_size0, low_size, s);
     441           0 :                 copy(s->out + NPAD, input, h_low_size0);
     442           0 :                 input += width;
     443             :             }
     444             : 
     445           0 :             low_size = (v_low_size0 + 1) >> 1;
     446           0 :             input = s->block;
     447           0 :             for (j = 0; j < h_low_size0; j++) {
     448           0 :                 copyv(input, width, s->in + NPAD, v_low_size0);
     449           0 :                 transform_step(s->in, s->out, v_low_size0, low_size, s);
     450           0 :                 copyh(s->out + NPAD, input, width, v_low_size0);
     451           0 :                 input++;
     452             :             }
     453             : 
     454           0 :             h_low_size0 = (h_low_size0 + 1) >> 1;
     455           0 :             v_low_size0 = (v_low_size0 + 1) >> 1;
     456             :         }
     457             : 
     458           0 :         s->thresholding(s->block, width, height, width, s->threshold, s->percent, s->nsteps);
     459             : 
     460           0 :         while (nsteps_invert--) {
     461           0 :             const int idx = s->vlowsize[p][nsteps_invert]  + s->vhighsize[p][nsteps_invert];
     462           0 :             const int idx2 = s->hlowsize[p][nsteps_invert] + s->hhighsize[p][nsteps_invert];
     463           0 :             float * idx3 = s->block;
     464           0 :             for (i = 0; i < idx2; i++) {
     465           0 :                 copyv(idx3, width, s->in + NPAD, idx);
     466           0 :                 invert_step(s->in, s->out, s->tmp, idx, s);
     467           0 :                 copyh(s->out + NPAD, idx3, width, idx);
     468           0 :                 idx3++;
     469             :             }
     470             : 
     471           0 :             idx3 = s->block;
     472           0 :             for (i = 0; i < idx; i++) {
     473           0 :                 copy(idx3, s->in + NPAD, idx2);
     474           0 :                 invert_step(s->in, s->out, s->tmp, idx2, s);
     475           0 :                 copy(s->out + NPAD, idx3, idx2);
     476           0 :                 idx3 += width;
     477             :             }
     478             :         }
     479             : 
     480           0 :         if (s->depth <= 8) {
     481           0 :             for (y = 0; y < height; y++) {
     482           0 :                 for (x = 0; x < width; x++)
     483           0 :                     dstp8[x] = av_clip_uint8(input[x] + 0.5f);
     484           0 :                 input += width;
     485           0 :                 dstp8 += out->linesize[p];
     486             :             }
     487             :         } else {
     488           0 :             for (y = 0; y < height; y++) {
     489           0 :                 for (x = 0; x < width; x++)
     490           0 :                     dstp16[x] = av_clip(input[x] + 0.5f, 0, s->peak);
     491           0 :                 input += width;
     492           0 :                 dstp16 += out->linesize[p] / 2;
     493             :             }
     494             :         }
     495             :     }
     496           0 : }
     497             : 
     498           0 : static int filter_frame(AVFilterLink *inlink, AVFrame *in)
     499             : {
     500           0 :     AVFilterContext *ctx  = inlink->dst;
     501           0 :     VagueDenoiserContext *s = ctx->priv;
     502           0 :     AVFilterLink *outlink = ctx->outputs[0];
     503             :     AVFrame *out;
     504           0 :     int direct = av_frame_is_writable(in);
     505             : 
     506           0 :     if (direct) {
     507           0 :         out = in;
     508             :     } else {
     509           0 :         out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
     510           0 :         if (!out) {
     511           0 :             av_frame_free(&in);
     512           0 :             return AVERROR(ENOMEM);
     513             :         }
     514             : 
     515           0 :         av_frame_copy_props(out, in);
     516             :     }
     517             : 
     518           0 :     filter(s, in, out);
     519             : 
     520           0 :     if (!direct)
     521           0 :         av_frame_free(&in);
     522             : 
     523           0 :     return ff_filter_frame(outlink, out);
     524             : }
     525             : 
     526           0 : static av_cold int init(AVFilterContext *ctx)
     527             : {
     528           0 :     VagueDenoiserContext *s = ctx->priv;
     529             : 
     530           0 :     switch (s->method) {
     531           0 :     case 0:
     532           0 :         s->thresholding = hard_thresholding;
     533           0 :         break;
     534           0 :     case 1:
     535           0 :         s->thresholding = soft_thresholding;
     536           0 :         break;
     537           0 :     case 2:
     538           0 :         s->thresholding = qian_thresholding;
     539           0 :         break;
     540             :     }
     541             : 
     542           0 :     return 0;
     543             : }
     544             : 
     545           0 : static av_cold void uninit(AVFilterContext *ctx)
     546             : {
     547           0 :     VagueDenoiserContext *s = ctx->priv;
     548             : 
     549           0 :     av_freep(&s->block);
     550           0 :     av_freep(&s->in);
     551           0 :     av_freep(&s->out);
     552           0 :     av_freep(&s->tmp);
     553           0 : }
     554             : 
     555             : static const AVFilterPad vaguedenoiser_inputs[] = {
     556             :     {
     557             :         .name         = "default",
     558             :         .type         = AVMEDIA_TYPE_VIDEO,
     559             :         .config_props = config_input,
     560             :         .filter_frame = filter_frame,
     561             :     },
     562             :     { NULL }
     563             : };
     564             : 
     565             : 
     566             : static const AVFilterPad vaguedenoiser_outputs[] = {
     567             :     {
     568             :         .name = "default",
     569             :         .type = AVMEDIA_TYPE_VIDEO
     570             :     },
     571             :     { NULL }
     572             : };
     573             : 
     574             : AVFilter ff_vf_vaguedenoiser = {
     575             :     .name          = "vaguedenoiser",
     576             :     .description   = NULL_IF_CONFIG_SMALL("Apply a Wavelet based Denoiser."),
     577             :     .priv_size     = sizeof(VagueDenoiserContext),
     578             :     .priv_class    = &vaguedenoiser_class,
     579             :     .init          = init,
     580             :     .uninit        = uninit,
     581             :     .query_formats = query_formats,
     582             :     .inputs        = vaguedenoiser_inputs,
     583             :     .outputs       = vaguedenoiser_outputs,
     584             :     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
     585             : };

Generated by: LCOV version 1.13