]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_nlmeans.c
lavfi/nlmeans: simplify log() call
[ffmpeg] / libavfilter / vf_nlmeans.c
1 /*
2  * Copyright (c) 2016 Clément Bœsch <u pkh me>
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (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 GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 /**
22  * @todo
23  * - better automatic defaults? see "Parameters" @ http://www.ipol.im/pub/art/2011/bcm_nlm/
24  * - temporal support (probably doesn't need any displacement according to
25  *   "Denoising image sequences does not require motion estimation")
26  * - Bayer pixel format support for at least raw photos? (DNG support would be
27  *   handy here)
28  * - FATE test (probably needs visual threshold test mechanism due to the use
29  *   of floats)
30  */
31
32 #include "libavutil/avassert.h"
33 #include "libavutil/opt.h"
34 #include "libavutil/pixdesc.h"
35 #include "avfilter.h"
36 #include "formats.h"
37 #include "internal.h"
38 #include "vf_nlmeans.h"
39 #include "video.h"
40
41 struct weighted_avg {
42     float total_weight;
43     float sum;
44 };
45
46 /*
47  * Note: WEIGHT_LUT_SIZE must be larger than max_meaningful_diff
48  * (log(255)*max(h)^2, which is approximately 500000 with the current
49  * maximum sigma of 30).
50  */
51 #define WEIGHT_LUT_SIZE 500000
52
53 typedef struct NLMeansContext {
54     const AVClass *class;
55     int nb_planes;
56     int chroma_w, chroma_h;
57     double pdiff_scale;                         // invert of the filtering parameter (sigma*10) squared
58     double sigma;                               // denoising strength
59     int patch_size,    patch_hsize;             // patch size and half size
60     int patch_size_uv, patch_hsize_uv;          // patch size and half size for chroma planes
61     int research_size,    research_hsize;       // research size and half size
62     int research_size_uv, research_hsize_uv;    // research size and half size for chroma planes
63     uint32_t *ii_orig;                          // integral image
64     uint32_t *ii;                               // integral image starting after the 0-line and 0-column
65     int ii_w, ii_h;                             // width and height of the integral image
66     ptrdiff_t ii_lz_32;                         // linesize in 32-bit units of the integral image
67     struct weighted_avg *wa;                    // weighted average of every pixel
68     ptrdiff_t wa_linesize;                      // linesize for wa in struct size unit
69     float weight_lut[WEIGHT_LUT_SIZE];          // lookup table mapping (scaled) patch differences to their associated weights
70     uint32_t max_meaningful_diff;               // maximum difference considered (if the patch difference is too high we ignore the pixel)
71     NLMeansDSPContext dsp;
72 } NLMeansContext;
73
74 #define OFFSET(x) offsetof(NLMeansContext, x)
75 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
76 static const AVOption nlmeans_options[] = {
77     { "s",  "denoising strength", OFFSET(sigma), AV_OPT_TYPE_DOUBLE, { .dbl = 1.0 }, 1.0, 30.0, FLAGS },
78     { "p",  "patch size",                   OFFSET(patch_size),    AV_OPT_TYPE_INT, { .i64 = 3*2+1 }, 0, 99, FLAGS },
79     { "pc", "patch size for chroma planes", OFFSET(patch_size_uv), AV_OPT_TYPE_INT, { .i64 = 0 },     0, 99, FLAGS },
80     { "r",  "research window",                   OFFSET(research_size),    AV_OPT_TYPE_INT, { .i64 = 7*2+1 }, 0, 99, FLAGS },
81     { "rc", "research window for chroma planes", OFFSET(research_size_uv), AV_OPT_TYPE_INT, { .i64 = 0 },     0, 99, FLAGS },
82     { NULL }
83 };
84
85 AVFILTER_DEFINE_CLASS(nlmeans);
86
87 static int query_formats(AVFilterContext *ctx)
88 {
89     static const enum AVPixelFormat pix_fmts[] = {
90         AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
91         AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
92         AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
93         AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
94         AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
95         AV_PIX_FMT_YUVJ411P,
96         AV_PIX_FMT_GRAY8, AV_PIX_FMT_GBRP,
97         AV_PIX_FMT_NONE
98     };
99
100     AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
101     if (!fmts_list)
102         return AVERROR(ENOMEM);
103     return ff_set_common_formats(ctx, fmts_list);
104 }
105
106 /**
107  * Compute squared difference of the safe area (the zone where s1 and s2
108  * overlap). It is likely the largest integral zone, so it is interesting to do
109  * as little checks as possible; contrary to the unsafe version of this
110  * function, we do not need any clipping here.
111  *
112  * The line above dst and the column to its left are always readable.
113  */
114 static void compute_safe_ssd_integral_image_c(uint32_t *dst, ptrdiff_t dst_linesize_32,
115                                               const uint8_t *s1, ptrdiff_t linesize1,
116                                               const uint8_t *s2, ptrdiff_t linesize2,
117                                               int w, int h)
118 {
119     int x, y;
120     const uint32_t *dst_top = dst - dst_linesize_32;
121
122     /* SIMD-friendly assumptions allowed here */
123     av_assert2(!(w & 0xf) && w >= 16 && h >= 1);
124
125     for (y = 0; y < h; y++) {
126         for (x = 0; x < w; x += 4) {
127             const int d0 = s1[x    ] - s2[x    ];
128             const int d1 = s1[x + 1] - s2[x + 1];
129             const int d2 = s1[x + 2] - s2[x + 2];
130             const int d3 = s1[x + 3] - s2[x + 3];
131
132             dst[x    ] = dst_top[x    ] - dst_top[x - 1] + d0*d0;
133             dst[x + 1] = dst_top[x + 1] - dst_top[x    ] + d1*d1;
134             dst[x + 2] = dst_top[x + 2] - dst_top[x + 1] + d2*d2;
135             dst[x + 3] = dst_top[x + 3] - dst_top[x + 2] + d3*d3;
136
137             dst[x    ] += dst[x - 1];
138             dst[x + 1] += dst[x    ];
139             dst[x + 2] += dst[x + 1];
140             dst[x + 3] += dst[x + 2];
141         }
142         s1  += linesize1;
143         s2  += linesize2;
144         dst += dst_linesize_32;
145         dst_top += dst_linesize_32;
146     }
147 }
148
149 /**
150  * Compute squared difference of an unsafe area (the zone nor s1 nor s2 could
151  * be readable).
152  *
153  * On the other hand, the line above dst and the column to its left are always
154  * readable.
155  *
156  * There is little point in having this function SIMDified as it is likely too
157  * complex and only handle small portions of the image.
158  *
159  * @param dst               integral image
160  * @param dst_linesize_32   integral image linesize (in 32-bit integers unit)
161  * @param startx            integral starting x position
162  * @param starty            integral starting y position
163  * @param src               source plane buffer
164  * @param linesize          source plane linesize
165  * @param offx              source offsetting in x
166  * @param offy              source offsetting in y
167  * @paran r                 absolute maximum source offsetting
168  * @param sw                source width
169  * @param sh                source height
170  * @param w                 width to compute
171  * @param h                 height to compute
172  */
173 static inline void compute_unsafe_ssd_integral_image(uint32_t *dst, ptrdiff_t dst_linesize_32,
174                                                      int startx, int starty,
175                                                      const uint8_t *src, ptrdiff_t linesize,
176                                                      int offx, int offy, int r, int sw, int sh,
177                                                      int w, int h)
178 {
179     int x, y;
180
181     for (y = starty; y < starty + h; y++) {
182         uint32_t acc = dst[y*dst_linesize_32 + startx - 1] - dst[(y-1)*dst_linesize_32 + startx - 1];
183         const int s1y = av_clip(y -  r,         0, sh - 1);
184         const int s2y = av_clip(y - (r + offy), 0, sh - 1);
185
186         for (x = startx; x < startx + w; x++) {
187             const int s1x = av_clip(x -  r,         0, sw - 1);
188             const int s2x = av_clip(x - (r + offx), 0, sw - 1);
189             const uint8_t v1 = src[s1y*linesize + s1x];
190             const uint8_t v2 = src[s2y*linesize + s2x];
191             const int d = v1 - v2;
192             acc += d * d;
193             dst[y*dst_linesize_32 + x] = dst[(y-1)*dst_linesize_32 + x] + acc;
194         }
195     }
196 }
197
198 /*
199  * Compute the sum of squared difference integral image
200  * http://www.ipol.im/pub/art/2014/57/
201  * Integral Images for Block Matching - Gabriele Facciolo, Nicolas Limare, Enric Meinhardt-Llopis
202  *
203  * @param ii                integral image of dimension (w+e*2) x (h+e*2) with
204  *                          an additional zeroed top line and column already
205  *                          "applied" to the pointer value
206  * @param ii_linesize_32    integral image linesize (in 32-bit integers unit)
207  * @param src               source plane buffer
208  * @param linesize          source plane linesize
209  * @param offx              x-offsetting ranging in [-e;e]
210  * @param offy              y-offsetting ranging in [-e;e]
211  * @param w                 source width
212  * @param h                 source height
213  * @param e                 research padding edge
214  */
215 static void compute_ssd_integral_image(const NLMeansDSPContext *dsp,
216                                        uint32_t *ii, ptrdiff_t ii_linesize_32,
217                                        const uint8_t *src, ptrdiff_t linesize, int offx, int offy,
218                                        int e, int w, int h)
219 {
220     // ii has a surrounding padding of thickness "e"
221     const int ii_w = w + e*2;
222     const int ii_h = h + e*2;
223
224     // we center the first source
225     const int s1x = e;
226     const int s1y = e;
227
228     // 2nd source is the frame with offsetting
229     const int s2x = e + offx;
230     const int s2y = e + offy;
231
232     // get the dimension of the overlapping rectangle where it is always safe
233     // to compare the 2 sources pixels
234     const int startx_safe = FFMAX(s1x, s2x);
235     const int starty_safe = FFMAX(s1y, s2y);
236     const int u_endx_safe = FFMIN(s1x + w, s2x + w); // unaligned
237     const int endy_safe   = FFMIN(s1y + h, s2y + h);
238
239     // deduce the safe area width and height
240     const int safe_pw = (u_endx_safe - startx_safe) & ~0xf;
241     const int safe_ph = endy_safe - starty_safe;
242
243     // adjusted end x position of the safe area after width of the safe area gets aligned
244     const int endx_safe = startx_safe + safe_pw;
245
246     // top part where only one of s1 and s2 is still readable, or none at all
247     compute_unsafe_ssd_integral_image(ii, ii_linesize_32,
248                                       0, 0,
249                                       src, linesize,
250                                       offx, offy, e, w, h,
251                                       ii_w, starty_safe);
252
253     // fill the left column integral required to compute the central
254     // overlapping one
255     compute_unsafe_ssd_integral_image(ii, ii_linesize_32,
256                                       0, starty_safe,
257                                       src, linesize,
258                                       offx, offy, e, w, h,
259                                       startx_safe, safe_ph);
260
261     // main and safe part of the integral
262     av_assert1(startx_safe - s1x >= 0); av_assert1(startx_safe - s1x < w);
263     av_assert1(starty_safe - s1y >= 0); av_assert1(starty_safe - s1y < h);
264     av_assert1(startx_safe - s2x >= 0); av_assert1(startx_safe - s2x < w);
265     av_assert1(starty_safe - s2y >= 0); av_assert1(starty_safe - s2y < h);
266     if (safe_pw && safe_ph)
267         dsp->compute_safe_ssd_integral_image(ii + starty_safe*ii_linesize_32 + startx_safe, ii_linesize_32,
268                                              src + (starty_safe - s1y) * linesize + (startx_safe - s1x), linesize,
269                                              src + (starty_safe - s2y) * linesize + (startx_safe - s2x), linesize,
270                                              safe_pw, safe_ph);
271
272     // right part of the integral
273     compute_unsafe_ssd_integral_image(ii, ii_linesize_32,
274                                       endx_safe, starty_safe,
275                                       src, linesize,
276                                       offx, offy, e, w, h,
277                                       ii_w - endx_safe, safe_ph);
278
279     // bottom part where only one of s1 and s2 is still readable, or none at all
280     compute_unsafe_ssd_integral_image(ii, ii_linesize_32,
281                                       0, endy_safe,
282                                       src, linesize,
283                                       offx, offy, e, w, h,
284                                       ii_w, ii_h - endy_safe);
285 }
286
287 static int config_input(AVFilterLink *inlink)
288 {
289     AVFilterContext *ctx = inlink->dst;
290     NLMeansContext *s = ctx->priv;
291     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
292     const int e = FFMAX(s->research_hsize, s->research_hsize_uv)
293                 + FFMAX(s->patch_hsize,    s->patch_hsize_uv);
294
295     s->chroma_w = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
296     s->chroma_h = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
297     s->nb_planes = av_pix_fmt_count_planes(inlink->format);
298
299     /* Allocate the integral image with extra edges of thickness "e"
300      *
301      *   +_+-------------------------------+
302      *   |0|0000000000000000000000000000000|
303      *   +-x-------------------------------+
304      *   |0|\    ^                         |
305      *   |0| ii  | e                       |
306      *   |0|     v                         |
307      *   |0|   +-----------------------+   |
308      *   |0|   |                       |   |
309      *   |0|<->|                       |   |
310      *   |0| e |                       |   |
311      *   |0|   |                       |   |
312      *   |0|   +-----------------------+   |
313      *   |0|                               |
314      *   |0|                               |
315      *   |0|                               |
316      *   +-+-------------------------------+
317      */
318     s->ii_w = inlink->w + e*2;
319     s->ii_h = inlink->h + e*2;
320
321     // align to 4 the linesize, "+1" is for the space of the left 0-column
322     s->ii_lz_32 = FFALIGN(s->ii_w + 1, 4);
323
324     // "+1" is for the space of the top 0-line
325     s->ii_orig = av_mallocz_array(s->ii_h + 1, s->ii_lz_32 * sizeof(*s->ii_orig));
326     if (!s->ii_orig)
327         return AVERROR(ENOMEM);
328
329     // skip top 0-line and left 0-column
330     s->ii = s->ii_orig + s->ii_lz_32 + 1;
331
332     // allocate weighted average for every pixel
333     s->wa_linesize = inlink->w;
334     s->wa = av_malloc_array(s->wa_linesize, inlink->h * sizeof(*s->wa));
335     if (!s->wa)
336         return AVERROR(ENOMEM);
337
338     return 0;
339 }
340
341 struct thread_data {
342     const uint8_t *src;
343     ptrdiff_t src_linesize;
344     int startx, starty;
345     int endx, endy;
346     const uint32_t *ii_start;
347     int p;
348 };
349
350 static int nlmeans_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
351 {
352     int x, y;
353     NLMeansContext *s = ctx->priv;
354     const struct thread_data *td = arg;
355     const ptrdiff_t src_linesize = td->src_linesize;
356     const int process_h = td->endy - td->starty;
357     const int slice_start = (process_h *  jobnr   ) / nb_jobs;
358     const int slice_end   = (process_h * (jobnr+1)) / nb_jobs;
359     const int starty = td->starty + slice_start;
360     const int endy   = td->starty + slice_end;
361     const int p = td->p;
362     const uint32_t *ii = td->ii_start + (starty - p - 1) * s->ii_lz_32 - p - 1;
363     const int dist_b = 2*p + 1;
364     const int dist_d = dist_b * s->ii_lz_32;
365     const int dist_e = dist_d + dist_b;
366
367     for (y = starty; y < endy; y++) {
368         const uint8_t *src = td->src + y*src_linesize;
369         struct weighted_avg *wa = s->wa + y*s->wa_linesize;
370         for (x = td->startx; x < td->endx; x++) {
371             /*
372              * M is a discrete map where every entry contains the sum of all the entries
373              * in the rectangle from the top-left origin of M to its coordinate. In the
374              * following schema, "i" contains the sum of the whole map:
375              *
376              * M = +----------+-----------------+----+
377              *     |          |                 |    |
378              *     |          |                 |    |
379              *     |         a|                b|   c|
380              *     +----------+-----------------+----+
381              *     |          |                 |    |
382              *     |          |                 |    |
383              *     |          |        X        |    |
384              *     |          |                 |    |
385              *     |         d|                e|   f|
386              *     +----------+-----------------+----+
387              *     |          |                 |    |
388              *     |         g|                h|   i|
389              *     +----------+-----------------+----+
390              *
391              * The sum of the X box can be calculated with:
392              *    X = e-d-b+a
393              *
394              * See https://en.wikipedia.org/wiki/Summed_area_table
395              *
396              * The compute*_ssd functions compute the integral image M where every entry
397              * contains the sum of the squared difference of every corresponding pixels of
398              * two input planes of the same size as M.
399              */
400             const uint32_t a = ii[x];
401             const uint32_t b = ii[x + dist_b];
402             const uint32_t d = ii[x + dist_d];
403             const uint32_t e = ii[x + dist_e];
404             const uint32_t patch_diff_sq = e - d - b + a;
405
406             if (patch_diff_sq < s->max_meaningful_diff) {
407                 const float weight = s->weight_lut[patch_diff_sq]; // exp(-patch_diff_sq * s->pdiff_scale)
408                 wa[x].total_weight += weight;
409                 wa[x].sum += weight * src[x];
410             }
411         }
412         ii += s->ii_lz_32;
413     }
414     return 0;
415 }
416
417 static void weight_averages(uint8_t *dst, ptrdiff_t dst_linesize,
418                             const uint8_t *src, ptrdiff_t src_linesize,
419                             struct weighted_avg *wa, ptrdiff_t wa_linesize,
420                             int w, int h)
421 {
422     int x, y;
423
424     for (y = 0; y < h; y++) {
425         for (x = 0; x < w; x++) {
426             // Also weight the centered pixel
427             wa[x].total_weight += 1.f;
428             wa[x].sum += 1.f * src[x];
429             dst[x] = av_clip_uint8(wa[x].sum / wa[x].total_weight);
430         }
431         dst += dst_linesize;
432         src += src_linesize;
433         wa += wa_linesize;
434     }
435 }
436
437 static int nlmeans_plane(AVFilterContext *ctx, int w, int h, int p, int r,
438                          uint8_t *dst, ptrdiff_t dst_linesize,
439                          const uint8_t *src, ptrdiff_t src_linesize)
440 {
441     int offx, offy;
442     NLMeansContext *s = ctx->priv;
443     /* patches center points cover the whole research window so the patches
444      * themselves overflow the research window */
445     const int e = r + p;
446     /* focus an integral pointer on the centered image (s1) */
447     const uint32_t *centered_ii = s->ii + e*s->ii_lz_32 + e;
448
449     memset(s->wa, 0, s->wa_linesize * h * sizeof(*s->wa));
450
451     for (offy = -r; offy <= r; offy++) {
452         for (offx = -r; offx <= r; offx++) {
453             if (offx || offy) {
454                 struct thread_data td = {
455                     .src          = src + offy*src_linesize + offx,
456                     .src_linesize = src_linesize,
457                     .startx       = FFMAX(0, -offx),
458                     .starty       = FFMAX(0, -offy),
459                     .endx         = FFMIN(w, w - offx),
460                     .endy         = FFMIN(h, h - offy),
461                     .ii_start     = centered_ii + offy*s->ii_lz_32 + offx,
462                     .p            = p,
463                 };
464
465                 compute_ssd_integral_image(&s->dsp, s->ii, s->ii_lz_32,
466                                            src, src_linesize,
467                                            offx, offy, e, w, h);
468                 ctx->internal->execute(ctx, nlmeans_slice, &td, NULL,
469                                        FFMIN(td.endy - td.starty, ff_filter_get_nb_threads(ctx)));
470             }
471         }
472     }
473
474     weight_averages(dst, dst_linesize, src, src_linesize,
475                     s->wa, s->wa_linesize, w, h);
476
477     return 0;
478 }
479
480 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
481 {
482     int i;
483     AVFilterContext *ctx = inlink->dst;
484     NLMeansContext *s = ctx->priv;
485     AVFilterLink *outlink = ctx->outputs[0];
486
487     AVFrame *out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
488     if (!out) {
489         av_frame_free(&in);
490         return AVERROR(ENOMEM);
491     }
492     av_frame_copy_props(out, in);
493
494     for (i = 0; i < s->nb_planes; i++) {
495         const int w = i ? s->chroma_w          : inlink->w;
496         const int h = i ? s->chroma_h          : inlink->h;
497         const int p = i ? s->patch_hsize_uv    : s->patch_hsize;
498         const int r = i ? s->research_hsize_uv : s->research_hsize;
499         nlmeans_plane(ctx, w, h, p, r,
500                       out->data[i], out->linesize[i],
501                       in->data[i],  in->linesize[i]);
502     }
503
504     av_frame_free(&in);
505     return ff_filter_frame(outlink, out);
506 }
507
508 #define CHECK_ODD_FIELD(field, name) do {                       \
509     if (!(s->field & 1)) {                                      \
510         s->field |= 1;                                          \
511         av_log(ctx, AV_LOG_WARNING, name " size must be odd, "  \
512                "setting it to %d\n", s->field);                 \
513     }                                                           \
514 } while (0)
515
516 void ff_nlmeans_init(NLMeansDSPContext *dsp)
517 {
518     dsp->compute_safe_ssd_integral_image = compute_safe_ssd_integral_image_c;
519
520     if (ARCH_AARCH64)
521         ff_nlmeans_init_aarch64(dsp);
522 }
523
524 static av_cold int init(AVFilterContext *ctx)
525 {
526     int i;
527     NLMeansContext *s = ctx->priv;
528     const double h = s->sigma * 10.;
529
530     s->pdiff_scale = 1. / (h * h);
531     s->max_meaningful_diff = log(255.) / s->pdiff_scale;
532     av_assert0((s->max_meaningful_diff - 1) < FF_ARRAY_ELEMS(s->weight_lut));
533     for (i = 0; i < WEIGHT_LUT_SIZE; i++)
534         s->weight_lut[i] = exp(-i * s->pdiff_scale);
535
536     CHECK_ODD_FIELD(research_size,   "Luma research window");
537     CHECK_ODD_FIELD(patch_size,      "Luma patch");
538
539     if (!s->research_size_uv) s->research_size_uv = s->research_size;
540     if (!s->patch_size_uv)    s->patch_size_uv    = s->patch_size;
541
542     CHECK_ODD_FIELD(research_size_uv, "Chroma research window");
543     CHECK_ODD_FIELD(patch_size_uv,    "Chroma patch");
544
545     s->research_hsize    = s->research_size    / 2;
546     s->research_hsize_uv = s->research_size_uv / 2;
547     s->patch_hsize       = s->patch_size       / 2;
548     s->patch_hsize_uv    = s->patch_size_uv    / 2;
549
550     av_log(ctx, AV_LOG_INFO, "Research window: %dx%d / %dx%d, patch size: %dx%d / %dx%d\n",
551            s->research_size, s->research_size, s->research_size_uv, s->research_size_uv,
552            s->patch_size,    s->patch_size,    s->patch_size_uv,    s->patch_size_uv);
553
554     ff_nlmeans_init(&s->dsp);
555
556     return 0;
557 }
558
559 static av_cold void uninit(AVFilterContext *ctx)
560 {
561     NLMeansContext *s = ctx->priv;
562     av_freep(&s->ii_orig);
563     av_freep(&s->wa);
564 }
565
566 static const AVFilterPad nlmeans_inputs[] = {
567     {
568         .name         = "default",
569         .type         = AVMEDIA_TYPE_VIDEO,
570         .config_props = config_input,
571         .filter_frame = filter_frame,
572     },
573     { NULL }
574 };
575
576 static const AVFilterPad nlmeans_outputs[] = {
577     {
578         .name = "default",
579         .type = AVMEDIA_TYPE_VIDEO,
580     },
581     { NULL }
582 };
583
584 AVFilter ff_vf_nlmeans = {
585     .name          = "nlmeans",
586     .description   = NULL_IF_CONFIG_SMALL("Non-local means denoiser."),
587     .priv_size     = sizeof(NLMeansContext),
588     .init          = init,
589     .uninit        = uninit,
590     .query_formats = query_formats,
591     .inputs        = nlmeans_inputs,
592     .outputs       = nlmeans_outputs,
593     .priv_class    = &nlmeans_class,
594     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
595 };