Message ID | 20191119223715.7159-1-onemda@gmail.com |
---|---|
State | Superseded |
Headers | show |
On Wed, Nov 20, 2019 at 6:43 AM Paul B Mahol <onemda@gmail.com> wrote: > > Signed-off-by: Paul B Mahol <onemda@gmail.com> > --- > libavfilter/aarch64/vf_nlmeans_init.c | 4 +- > libavfilter/nlmeans_template.c | 366 ++++++++++++++++++ > libavfilter/vf_nlmeans.c | 526 ++++++-------------------- > libavfilter/vf_nlmeans.h | 44 ++- > 4 files changed, 520 insertions(+), 420 deletions(-) > create mode 100644 libavfilter/nlmeans_template.c > > diff --git a/libavfilter/aarch64/vf_nlmeans_init.c b/libavfilter/aarch64/vf_nlmeans_init.c > index a1edefb144..ee87c88dd6 100644 > --- a/libavfilter/aarch64/vf_nlmeans_init.c > +++ b/libavfilter/aarch64/vf_nlmeans_init.c > @@ -24,10 +24,10 @@ void ff_compute_safe_ssd_integral_image_neon(uint32_t *dst, ptrdiff_t dst_linesi > const uint8_t *s2, ptrdiff_t linesize2, > int w, int h); > > -av_cold void ff_nlmeans_init_aarch64(NLMeansDSPContext *dsp) > +av_cold void ff_nlmeans_init_aarch64(NLMeansDSPContext *dsp, int depth) > { > int cpu_flags = av_get_cpu_flags(); > > - if (have_neon(cpu_flags)) > + if (have_neon(cpu_flags) && depth == 8) > dsp->compute_safe_ssd_integral_image = ff_compute_safe_ssd_integral_image_neon; > } > diff --git a/libavfilter/nlmeans_template.c b/libavfilter/nlmeans_template.c > new file mode 100644 > index 0000000000..64ac2e9e9b > --- /dev/null > +++ b/libavfilter/nlmeans_template.c > @@ -0,0 +1,366 @@ > +/* > + * Copyright (c) 2016 Clément Bœsch <u pkh me> > + * > + * This file is part of FFmpeg. > + * > + * FFmpeg is free software; you can redistribute it and/or > + * modify it under the terms of the GNU Lesser General Public > + * License as published by the Free Software Foundation; either > + * version 2.1 of the License, or (at your option) any later version. > + * > + * FFmpeg is distributed in the hope that it will be useful, > + * but WITHOUT ANY WARRANTY; without even the implied warranty of > + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > + * Lesser General Public License for more details. > + * > + * You should have received a copy of the GNU Lesser General Public > + * License along with FFmpeg; if not, write to the Free Software > + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA > + */ > + > +#include "libavutil/avassert.h" > +#include "avfilter.h" > +#include "formats.h" > +#include "internal.h" > +#include "vf_nlmeans.h" > +#include "video.h" > + > +#undef pixel > +#undef integral > +#if DEPTH <= 8 > +#define pixel uint8_t > +#define integral uint32_t > +#else > +#define pixel uint16_t > +#define integral uint64_t > +#endif > + > +#define fn3(a,b) a##b > +#define fn2(a,b) fn3(a,b) > +#define fn(a) fn2(a, DEPTH) > + > +/** > + * Compute squared difference of the safe area (the zone where s1 and s2 > + * overlap). It is likely the largest integral zone, so it is interesting to do > + * as little checks as possible; contrary to the unsafe version of this > + * function, we do not need any clipping here. > + * > + * The line above dst and the column to its left are always readable. > + */ > +static void fn(compute_safe_ssd_integral_image_c)(uint32_t *ddst, ptrdiff_t dst_linesize_32, > + const uint8_t *ss1, ptrdiff_t linesize1, > + const uint8_t *ss2, ptrdiff_t linesize2, > + int w, int h) > +{ > + const pixel *s1 = (const pixel *)ss1; > + const pixel *s2 = (const pixel *)ss2; > + int x, y; > + integral *dst = (integral *)ddst; > + const integral *dst_top = dst - dst_linesize_32; > + > + linesize1 /= sizeof(pixel); > + linesize2 /= sizeof(pixel); > + > + /* SIMD-friendly assumptions allowed here */ > + av_assert2(!(w & 0xf) && w >= 16 && h >= 1); > + > + for (y = 0; y < h; y++) { > + for (x = 0; x < w; x += 4) { > + const int d0 = s1[x ] - s2[x ]; > + const int d1 = s1[x + 1] - s2[x + 1]; > + const int d2 = s1[x + 2] - s2[x + 2]; > + const int d3 = s1[x + 3] - s2[x + 3]; > + > + dst[x ] = dst_top[x ] - dst_top[x - 1] + d0*d0; > + dst[x + 1] = dst_top[x + 1] - dst_top[x ] + d1*d1; > + dst[x + 2] = dst_top[x + 2] - dst_top[x + 1] + d2*d2; > + dst[x + 3] = dst_top[x + 3] - dst_top[x + 2] + d3*d3; > + > + dst[x ] += dst[x - 1]; > + dst[x + 1] += dst[x ]; > + dst[x + 2] += dst[x + 1]; > + dst[x + 3] += dst[x + 2]; > + } > + s1 += linesize1; > + s2 += linesize2; > + dst += dst_linesize_32; > + dst_top += dst_linesize_32; > + } > +} > + > +/** > + * Compute squared difference of an unsafe area (the zone nor s1 nor s2 could > + * be readable). > + * > + * On the other hand, the line above dst and the column to its left are always > + * readable. > + * > + * There is little point in having this function SIMDified as it is likely too > + * complex and only handle small portions of the image. > + * > + * @param dst integral image > + * @param dst_linesize_32 integral image linesize (in 32-bit integers unit) > + * @param startx integral starting x position > + * @param starty integral starting y position > + * @param ssrc source plane buffer > + * @param linesize source plane linesize > + * @param offx source offsetting in x > + * @param offy source offsetting in y > + * @paran r absolute maximum source offsetting > + * @param sw source width > + * @param sh source height > + * @param w width to compute > + * @param h height to compute > + */ > +static inline void fn(compute_unsafe_ssd_integral_image)(uint32_t *ddst, ptrdiff_t dst_linesize_32, > + int startx, int starty, > + const uint8_t *ssrc, ptrdiff_t linesize, > + int offx, int offy, int r, int sw, int sh, > + int w, int h) > +{ > + integral *dst = (integral *)ddst; > + const pixel *src = (const pixel *)ssrc; > + int x, y; > + > + linesize /= sizeof(pixel); > + > + for (y = starty; y < starty + h; y++) { > + integral acc = dst[y*dst_linesize_32 + startx - 1] - dst[(y-1)*dst_linesize_32 + startx - 1]; > + const int s1y = av_clip(y - r, 0, sh - 1); > + const int s2y = av_clip(y - (r + offy), 0, sh - 1); > + > + for (x = startx; x < startx + w; x++) { > + const int s1x = av_clip(x - r, 0, sw - 1); > + const int s2x = av_clip(x - (r + offx), 0, sw - 1); > + const pixel v1 = src[s1y*linesize + s1x]; > + const pixel v2 = src[s2y*linesize + s2x]; > + const int d = v1 - v2; > + acc += d * d; > + dst[y*dst_linesize_32 + x] = dst[(y-1)*dst_linesize_32 + x] + acc; > + } > + } > +} > + > +/* > + * Compute the sum of squared difference integral image > + * http://www.ipol.im/pub/art/2014/57/ > + * Integral Images for Block Matching - Gabriele Facciolo, Nicolas Limare, Enric Meinhardt-Llopis > + * > + * @param ii integral image of dimension (w+e*2) x (h+e*2) with > + * an additional zeroed top line and column already > + * "applied" to the pointer value > + * @param ii_linesize_32 integral image linesize (in 32-bit integers unit) > + * @param src source plane buffer > + * @param linesize source plane linesize > + * @param offx x-offsetting ranging in [-e;e] > + * @param offy y-offsetting ranging in [-e;e] > + * @param w source width > + * @param h source height > + * @param e research padding edge > + */ > +static void fn(compute_ssd_integral_image)(const NLMeansDSPContext *dsp, > + integral *ii, ptrdiff_t ii_linesize_32, > + const uint8_t *src, ptrdiff_t linesize, int offx, int offy, > + int e, int w, int h) > +{ > + // ii has a surrounding padding of thickness "e" > + const int ii_w = w + e*2; > + const int ii_h = h + e*2; > + > + // we center the first source > + const int s1x = e; > + const int s1y = e; > + > + // 2nd source is the frame with offsetting > + const int s2x = e + offx; > + const int s2y = e + offy; > + > + // get the dimension of the overlapping rectangle where it is always safe > + // to compare the 2 sources pixels > + const int startx_safe = FFMAX(s1x, s2x); > + const int starty_safe = FFMAX(s1y, s2y); > + const int u_endx_safe = FFMIN(s1x + w, s2x + w); // unaligned > + const int endy_safe = FFMIN(s1y + h, s2y + h); > + > + // deduce the safe area width and height > + const int safe_pw = (u_endx_safe - startx_safe) & ~0xf; > + const int safe_ph = endy_safe - starty_safe; > + > + // adjusted end x position of the safe area after width of the safe area gets aligned > + const int endx_safe = startx_safe + safe_pw; > + > + // top part where only one of s1 and s2 is still readable, or none at all > + fn(compute_unsafe_ssd_integral_image)((uint32_t *)(ii), ii_linesize_32, > + 0, 0, > + src, linesize, > + offx, offy, e, w, h, > + ii_w, starty_safe); > + > + // fill the left column integral required to compute the central > + // overlapping one > + fn(compute_unsafe_ssd_integral_image)((uint32_t *)(ii), ii_linesize_32, > + 0, starty_safe, > + src, linesize, > + offx, offy, e, w, h, > + startx_safe, safe_ph); > + > + // main and safe part of the integral > + av_assert1(startx_safe - s1x >= 0); av_assert1(startx_safe - s1x < w); > + av_assert1(starty_safe - s1y >= 0); av_assert1(starty_safe - s1y < h); > + av_assert1(startx_safe - s2x >= 0); av_assert1(startx_safe - s2x < w); > + av_assert1(starty_safe - s2y >= 0); av_assert1(starty_safe - s2y < h); > + if (safe_pw && safe_ph) > + dsp->compute_safe_ssd_integral_image((uint32_t *)(ii + starty_safe*ii_linesize_32 + startx_safe), ii_linesize_32, > + src + (starty_safe - s1y) * linesize + (startx_safe - s1x) * sizeof(pixel), linesize, > + src + (starty_safe - s2y) * linesize + (startx_safe - s2x) * sizeof(pixel), linesize, > + safe_pw, safe_ph); > + > + // right part of the integral > + fn(compute_unsafe_ssd_integral_image)((uint32_t *)(ii), ii_linesize_32, > + endx_safe, starty_safe, > + src, linesize, > + offx, offy, e, w, h, > + ii_w - endx_safe, safe_ph); > + > + // bottom part where only one of s1 and s2 is still readable, or none at all > + fn(compute_unsafe_ssd_integral_image)((uint32_t *)(ii), ii_linesize_32, > + 0, endy_safe, > + src, linesize, > + offx, offy, e, w, h, > + ii_w, ii_h - endy_safe); > +} > + > +static int fn(nlmeans_slice)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) > +{ > + int x, y; > + NLMeansContext *s = ctx->priv; > + const struct thread_data *td = arg; > + const ptrdiff_t src_linesize = td->src_linesize; > + const int process_h = td->endy - td->starty; > + const int slice_start = (process_h * jobnr ) / nb_jobs; > + const int slice_end = (process_h * (jobnr+1)) / nb_jobs; > + const int starty = td->starty + slice_start; > + const int endy = td->starty + slice_end; > + const int p = td->p; > + const integral *ii = (const integral *)td->ii_start + (starty - p - 1) * s->ii_lz_32 - p - 1; > + const int dist_b = 2*p + 1; > + const int dist_d = dist_b * s->ii_lz_32; > + const int dist_e = dist_d + dist_b; > + > + for (y = starty; y < endy; y++) { > + const pixel *src = (const pixel *)(td->src + y*src_linesize); > + struct weighted_avg *wa = s->wa + y*s->wa_linesize; > + for (x = td->startx; x < td->endx; x++) { > + /* > + * M is a discrete map where every entry contains the sum of all the entries > + * in the rectangle from the top-left origin of M to its coordinate. In the > + * following schema, "i" contains the sum of the whole map: > + * > + * M = +----------+-----------------+----+ > + * | | | | > + * | | | | > + * | a| b| c| > + * +----------+-----------------+----+ > + * | | | | > + * | | | | > + * | | X | | > + * | | | | > + * | d| e| f| > + * +----------+-----------------+----+ > + * | | | | > + * | g| h| i| > + * +----------+-----------------+----+ > + * > + * The sum of the X box can be calculated with: > + * X = e-d-b+a > + * > + * See https://en.wikipedia.org/wiki/Summed_area_table > + * > + * The compute*_ssd functions compute the integral image M where every entry > + * contains the sum of the squared difference of every corresponding pixels of > + * two input planes of the same size as M. > + */ > + const integral a = ii[x]; > + const integral b = ii[x + dist_b]; > + const integral d = ii[x + dist_d]; > + const integral e = ii[x + dist_e]; > + const integral patch_diff_sq = e + a - d - b; > + > + if (patch_diff_sq < s->max_meaningful_diff) { > + const float weight = s->weight_lut[patch_diff_sq]; // exp(-patch_diff_sq * s->pdiff_scale) > + wa[x].total_weight += weight; > + wa[x].sum += weight * src[x]; > + } > + } > + ii += s->ii_lz_32; > + } > + return 0; > +} > + > +static void fn(weight_averages)(uint8_t *ddst, ptrdiff_t dst_linesize, > + const uint8_t *ssrc, ptrdiff_t src_linesize, > + struct weighted_avg *wa, ptrdiff_t wa_linesize, > + int w, int h) > +{ > + const pixel *src = (const pixel *)ssrc; > + pixel *dst = (pixel *)ddst; > + int x, y; > + > + src_linesize /= sizeof(pixel); > + dst_linesize /= sizeof(pixel); > + > + for (y = 0; y < h; y++) { > + for (x = 0; x < w; x++) { > + // Also weight the centered pixel > + wa[x].total_weight += 1.f; > + wa[x].sum += 1.f * src[x]; > + dst[x] = av_clip_uintp2(wa[x].sum / wa[x].total_weight + 0.5f, DEPTH); > + } > + dst += dst_linesize; > + src += src_linesize; > + wa += wa_linesize; > + } > +} > + > +static int fn(nlmeans_plane)(AVFilterContext *ctx, int w, int h, int p, int r, > + uint8_t *dst, ptrdiff_t dst_linesize, > + const uint8_t *src, ptrdiff_t src_linesize) > +{ > + int offx, offy; > + NLMeansContext *s = ctx->priv; > + /* patches center points cover the whole research window so the patches > + * themselves overflow the research window */ > + const int e = r + p; > + /* focus an integral pointer on the centered image (s1) */ > + const integral *centered_ii = (const integral *)s->ii + e*s->ii_lz_32 + e; > + > + memset(s->wa, 0, s->wa_linesize * h * sizeof(*s->wa)); > + > + for (offy = -r; offy <= r; offy++) { > + for (offx = -r; offx <= r; offx++) { > + if (offx || offy) { > + struct thread_data td = { > + .src = src + offy*src_linesize + offx * (int)(sizeof(pixel)), > + .src_linesize = src_linesize, > + .startx = FFMAX(0, -offx), > + .starty = FFMAX(0, -offy), > + .endx = FFMIN(w, w - offx), > + .endy = FFMIN(h, h - offy), > + .ii_start = (uint32_t *)(centered_ii + offy*s->ii_lz_32 + offx), > + .p = p, > + }; > + > + fn(compute_ssd_integral_image)(&s->dsp, (integral *)s->ii, s->ii_lz_32, > + src, src_linesize, > + offx, offy, e, w, h); > + ctx->internal->execute(ctx, fn(nlmeans_slice), &td, NULL, > + FFMIN(td.endy - td.starty, ff_filter_get_nb_threads(ctx))); > + } > + } > + } > + > + fn(weight_averages)(dst, dst_linesize, src, src_linesize, > + s->wa, s->wa_linesize, w, h); > + > + return 0; > +} > diff --git a/libavfilter/vf_nlmeans.c b/libavfilter/vf_nlmeans.c > index 06233b0dd4..49edc1f8c7 100644 > --- a/libavfilter/vf_nlmeans.c > +++ b/libavfilter/vf_nlmeans.c > @@ -38,31 +38,28 @@ > #include "vf_nlmeans.h" > #include "video.h" > > -struct weighted_avg { > - float total_weight; > - float sum; > -}; > +#define DEPTH 8 > +#include "nlmeans_template.c" > + > +#undef DEPTH > +#define DEPTH 9 > +#include "nlmeans_template.c" > + > +#undef DEPTH > +#define DEPTH 10 > +#include "nlmeans_template.c" > + > +#undef DEPTH > +#define DEPTH 12 > +#include "nlmeans_template.c" > + > +#undef DEPTH > +#define DEPTH 14 > +#include "nlmeans_template.c" > > -typedef struct NLMeansContext { > - const AVClass *class; > - int nb_planes; > - int chroma_w, chroma_h; > - double pdiff_scale; // invert of the filtering parameter (sigma*10) squared > - double sigma; // denoising strength > - int patch_size, patch_hsize; // patch size and half size > - int patch_size_uv, patch_hsize_uv; // patch size and half size for chroma planes > - int research_size, research_hsize; // research size and half size > - int research_size_uv, research_hsize_uv; // research size and half size for chroma planes > - uint32_t *ii_orig; // integral image > - uint32_t *ii; // integral image starting after the 0-line and 0-column > - int ii_w, ii_h; // width and height of the integral image > - ptrdiff_t ii_lz_32; // linesize in 32-bit units of the integral image > - struct weighted_avg *wa; // weighted average of every pixel > - ptrdiff_t wa_linesize; // linesize for wa in struct size unit > - float *weight_lut; // lookup table mapping (scaled) patch differences to their associated weights > - uint32_t max_meaningful_diff; // maximum difference considered (if the patch difference is too high we ignore the pixel) > - NLMeansDSPContext dsp; > -} NLMeansContext; > +#undef DEPTH > +#define DEPTH 16 > +#include "nlmeans_template.c" > > #define OFFSET(x) offsetof(NLMeansContext, x) > #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM > @@ -87,6 +84,13 @@ static int query_formats(AVFilterContext *ctx) > AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P, > AV_PIX_FMT_YUVJ411P, > AV_PIX_FMT_GRAY8, AV_PIX_FMT_GBRP, > + AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9, > + AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10, > + AV_PIX_FMT_YUV440P10, > + AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12, > + AV_PIX_FMT_YUV440P12, > + AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV420P14, > + AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16, > AV_PIX_FMT_NONE > }; > > @@ -96,380 +100,6 @@ static int query_formats(AVFilterContext *ctx) > return ff_set_common_formats(ctx, fmts_list); > } > > -/** > - * Compute squared difference of the safe area (the zone where s1 and s2 > - * overlap). It is likely the largest integral zone, so it is interesting to do > - * as little checks as possible; contrary to the unsafe version of this > - * function, we do not need any clipping here. > - * > - * The line above dst and the column to its left are always readable. > - */ > -static void compute_safe_ssd_integral_image_c(uint32_t *dst, ptrdiff_t dst_linesize_32, > - const uint8_t *s1, ptrdiff_t linesize1, > - const uint8_t *s2, ptrdiff_t linesize2, > - int w, int h) > -{ > - int x, y; > - const uint32_t *dst_top = dst - dst_linesize_32; > - > - /* SIMD-friendly assumptions allowed here */ > - av_assert2(!(w & 0xf) && w >= 16 && h >= 1); > - > - for (y = 0; y < h; y++) { > - for (x = 0; x < w; x += 4) { > - const int d0 = s1[x ] - s2[x ]; > - const int d1 = s1[x + 1] - s2[x + 1]; > - const int d2 = s1[x + 2] - s2[x + 2]; > - const int d3 = s1[x + 3] - s2[x + 3]; > - > - dst[x ] = dst_top[x ] - dst_top[x - 1] + d0*d0; > - dst[x + 1] = dst_top[x + 1] - dst_top[x ] + d1*d1; > - dst[x + 2] = dst_top[x + 2] - dst_top[x + 1] + d2*d2; > - dst[x + 3] = dst_top[x + 3] - dst_top[x + 2] + d3*d3; > - > - dst[x ] += dst[x - 1]; > - dst[x + 1] += dst[x ]; > - dst[x + 2] += dst[x + 1]; > - dst[x + 3] += dst[x + 2]; > - } > - s1 += linesize1; > - s2 += linesize2; > - dst += dst_linesize_32; > - dst_top += dst_linesize_32; > - } > -} > - > -/** > - * Compute squared difference of an unsafe area (the zone nor s1 nor s2 could > - * be readable). > - * > - * On the other hand, the line above dst and the column to its left are always > - * readable. > - * > - * There is little point in having this function SIMDified as it is likely too > - * complex and only handle small portions of the image. > - * > - * @param dst integral image > - * @param dst_linesize_32 integral image linesize (in 32-bit integers unit) > - * @param startx integral starting x position > - * @param starty integral starting y position > - * @param src source plane buffer > - * @param linesize source plane linesize > - * @param offx source offsetting in x > - * @param offy source offsetting in y > - * @paran r absolute maximum source offsetting > - * @param sw source width > - * @param sh source height > - * @param w width to compute > - * @param h height to compute > - */ > -static inline void compute_unsafe_ssd_integral_image(uint32_t *dst, ptrdiff_t dst_linesize_32, > - int startx, int starty, > - const uint8_t *src, ptrdiff_t linesize, > - int offx, int offy, int r, int sw, int sh, > - int w, int h) > -{ > - int x, y; > - > - for (y = starty; y < starty + h; y++) { > - uint32_t acc = dst[y*dst_linesize_32 + startx - 1] - dst[(y-1)*dst_linesize_32 + startx - 1]; > - const int s1y = av_clip(y - r, 0, sh - 1); > - const int s2y = av_clip(y - (r + offy), 0, sh - 1); > - > - for (x = startx; x < startx + w; x++) { > - const int s1x = av_clip(x - r, 0, sw - 1); > - const int s2x = av_clip(x - (r + offx), 0, sw - 1); > - const uint8_t v1 = src[s1y*linesize + s1x]; > - const uint8_t v2 = src[s2y*linesize + s2x]; > - const int d = v1 - v2; > - acc += d * d; > - dst[y*dst_linesize_32 + x] = dst[(y-1)*dst_linesize_32 + x] + acc; > - } > - } > -} > - > -/* > - * Compute the sum of squared difference integral image > - * http://www.ipol.im/pub/art/2014/57/ > - * Integral Images for Block Matching - Gabriele Facciolo, Nicolas Limare, Enric Meinhardt-Llopis > - * > - * @param ii integral image of dimension (w+e*2) x (h+e*2) with > - * an additional zeroed top line and column already > - * "applied" to the pointer value > - * @param ii_linesize_32 integral image linesize (in 32-bit integers unit) > - * @param src source plane buffer > - * @param linesize source plane linesize > - * @param offx x-offsetting ranging in [-e;e] > - * @param offy y-offsetting ranging in [-e;e] > - * @param w source width > - * @param h source height > - * @param e research padding edge > - */ > -static void compute_ssd_integral_image(const NLMeansDSPContext *dsp, > - uint32_t *ii, ptrdiff_t ii_linesize_32, > - const uint8_t *src, ptrdiff_t linesize, int offx, int offy, > - int e, int w, int h) > -{ > - // ii has a surrounding padding of thickness "e" > - const int ii_w = w + e*2; > - const int ii_h = h + e*2; > - > - // we center the first source > - const int s1x = e; > - const int s1y = e; > - > - // 2nd source is the frame with offsetting > - const int s2x = e + offx; > - const int s2y = e + offy; > - > - // get the dimension of the overlapping rectangle where it is always safe > - // to compare the 2 sources pixels > - const int startx_safe = FFMAX(s1x, s2x); > - const int starty_safe = FFMAX(s1y, s2y); > - const int u_endx_safe = FFMIN(s1x + w, s2x + w); // unaligned > - const int endy_safe = FFMIN(s1y + h, s2y + h); > - > - // deduce the safe area width and height > - const int safe_pw = (u_endx_safe - startx_safe) & ~0xf; > - const int safe_ph = endy_safe - starty_safe; > - > - // adjusted end x position of the safe area after width of the safe area gets aligned > - const int endx_safe = startx_safe + safe_pw; > - > - // top part where only one of s1 and s2 is still readable, or none at all > - compute_unsafe_ssd_integral_image(ii, ii_linesize_32, > - 0, 0, > - src, linesize, > - offx, offy, e, w, h, > - ii_w, starty_safe); > - > - // fill the left column integral required to compute the central > - // overlapping one > - compute_unsafe_ssd_integral_image(ii, ii_linesize_32, > - 0, starty_safe, > - src, linesize, > - offx, offy, e, w, h, > - startx_safe, safe_ph); > - > - // main and safe part of the integral > - av_assert1(startx_safe - s1x >= 0); av_assert1(startx_safe - s1x < w); > - av_assert1(starty_safe - s1y >= 0); av_assert1(starty_safe - s1y < h); > - av_assert1(startx_safe - s2x >= 0); av_assert1(startx_safe - s2x < w); > - av_assert1(starty_safe - s2y >= 0); av_assert1(starty_safe - s2y < h); > - if (safe_pw && safe_ph) > - dsp->compute_safe_ssd_integral_image(ii + starty_safe*ii_linesize_32 + startx_safe, ii_linesize_32, > - src + (starty_safe - s1y) * linesize + (startx_safe - s1x), linesize, > - src + (starty_safe - s2y) * linesize + (startx_safe - s2x), linesize, > - safe_pw, safe_ph); > - > - // right part of the integral > - compute_unsafe_ssd_integral_image(ii, ii_linesize_32, > - endx_safe, starty_safe, > - src, linesize, > - offx, offy, e, w, h, > - ii_w - endx_safe, safe_ph); > - > - // bottom part where only one of s1 and s2 is still readable, or none at all > - compute_unsafe_ssd_integral_image(ii, ii_linesize_32, > - 0, endy_safe, > - src, linesize, > - offx, offy, e, w, h, > - ii_w, ii_h - endy_safe); > -} > - > -static int config_input(AVFilterLink *inlink) > -{ > - AVFilterContext *ctx = inlink->dst; > - NLMeansContext *s = ctx->priv; > - const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format); > - const int e = FFMAX(s->research_hsize, s->research_hsize_uv) > - + FFMAX(s->patch_hsize, s->patch_hsize_uv); > - > - s->chroma_w = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w); > - s->chroma_h = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h); > - s->nb_planes = av_pix_fmt_count_planes(inlink->format); > - > - /* Allocate the integral image with extra edges of thickness "e" > - * > - * +_+-------------------------------+ > - * |0|0000000000000000000000000000000| > - * +-x-------------------------------+ > - * |0|\ ^ | > - * |0| ii | e | > - * |0| v | > - * |0| +-----------------------+ | > - * |0| | | | > - * |0|<->| | | > - * |0| e | | | > - * |0| | | | > - * |0| +-----------------------+ | > - * |0| | > - * |0| | > - * |0| | > - * +-+-------------------------------+ > - */ > - s->ii_w = inlink->w + e*2; > - s->ii_h = inlink->h + e*2; > - > - // align to 4 the linesize, "+1" is for the space of the left 0-column > - s->ii_lz_32 = FFALIGN(s->ii_w + 1, 4); > - > - // "+1" is for the space of the top 0-line > - s->ii_orig = av_mallocz_array(s->ii_h + 1, s->ii_lz_32 * sizeof(*s->ii_orig)); > - if (!s->ii_orig) > - return AVERROR(ENOMEM); > - > - // skip top 0-line and left 0-column > - s->ii = s->ii_orig + s->ii_lz_32 + 1; > - > - // allocate weighted average for every pixel > - s->wa_linesize = inlink->w; > - s->wa = av_malloc_array(s->wa_linesize, inlink->h * sizeof(*s->wa)); > - if (!s->wa) > - return AVERROR(ENOMEM); > - > - return 0; > -} > - > -struct thread_data { > - const uint8_t *src; > - ptrdiff_t src_linesize; > - int startx, starty; > - int endx, endy; > - const uint32_t *ii_start; > - int p; > -}; > - > -static int nlmeans_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) > -{ > - int x, y; > - NLMeansContext *s = ctx->priv; > - const struct thread_data *td = arg; > - const ptrdiff_t src_linesize = td->src_linesize; > - const int process_h = td->endy - td->starty; > - const int slice_start = (process_h * jobnr ) / nb_jobs; > - const int slice_end = (process_h * (jobnr+1)) / nb_jobs; > - const int starty = td->starty + slice_start; > - const int endy = td->starty + slice_end; > - const int p = td->p; > - const uint32_t *ii = td->ii_start + (starty - p - 1) * s->ii_lz_32 - p - 1; > - const int dist_b = 2*p + 1; > - const int dist_d = dist_b * s->ii_lz_32; > - const int dist_e = dist_d + dist_b; > - > - for (y = starty; y < endy; y++) { > - const uint8_t *src = td->src + y*src_linesize; > - struct weighted_avg *wa = s->wa + y*s->wa_linesize; > - for (x = td->startx; x < td->endx; x++) { > - /* > - * M is a discrete map where every entry contains the sum of all the entries > - * in the rectangle from the top-left origin of M to its coordinate. In the > - * following schema, "i" contains the sum of the whole map: > - * > - * M = +----------+-----------------+----+ > - * | | | | > - * | | | | > - * | a| b| c| > - * +----------+-----------------+----+ > - * | | | | > - * | | | | > - * | | X | | > - * | | | | > - * | d| e| f| > - * +----------+-----------------+----+ > - * | | | | > - * | g| h| i| > - * +----------+-----------------+----+ > - * > - * The sum of the X box can be calculated with: > - * X = e-d-b+a > - * > - * See https://en.wikipedia.org/wiki/Summed_area_table > - * > - * The compute*_ssd functions compute the integral image M where every entry > - * contains the sum of the squared difference of every corresponding pixels of > - * two input planes of the same size as M. > - */ > - const uint32_t a = ii[x]; > - const uint32_t b = ii[x + dist_b]; > - const uint32_t d = ii[x + dist_d]; > - const uint32_t e = ii[x + dist_e]; > - const uint32_t patch_diff_sq = e - d - b + a; > - > - if (patch_diff_sq < s->max_meaningful_diff) { > - const float weight = s->weight_lut[patch_diff_sq]; // exp(-patch_diff_sq * s->pdiff_scale) > - wa[x].total_weight += weight; > - wa[x].sum += weight * src[x]; > - } > - } > - ii += s->ii_lz_32; > - } > - return 0; > -} > - > -static void weight_averages(uint8_t *dst, ptrdiff_t dst_linesize, > - const uint8_t *src, ptrdiff_t src_linesize, > - struct weighted_avg *wa, ptrdiff_t wa_linesize, > - int w, int h) > -{ > - int x, y; > - > - for (y = 0; y < h; y++) { > - for (x = 0; x < w; x++) { > - // Also weight the centered pixel > - wa[x].total_weight += 1.f; > - wa[x].sum += 1.f * src[x]; > - dst[x] = av_clip_uint8(wa[x].sum / wa[x].total_weight + 0.5f); > - } > - dst += dst_linesize; > - src += src_linesize; > - wa += wa_linesize; > - } > -} > - > -static int nlmeans_plane(AVFilterContext *ctx, int w, int h, int p, int r, > - uint8_t *dst, ptrdiff_t dst_linesize, > - const uint8_t *src, ptrdiff_t src_linesize) > -{ > - int offx, offy; > - NLMeansContext *s = ctx->priv; > - /* patches center points cover the whole research window so the patches > - * themselves overflow the research window */ > - const int e = r + p; > - /* focus an integral pointer on the centered image (s1) */ > - const uint32_t *centered_ii = s->ii + e*s->ii_lz_32 + e; > - > - memset(s->wa, 0, s->wa_linesize * h * sizeof(*s->wa)); > - > - for (offy = -r; offy <= r; offy++) { > - for (offx = -r; offx <= r; offx++) { > - if (offx || offy) { > - struct thread_data td = { > - .src = src + offy*src_linesize + offx, > - .src_linesize = src_linesize, > - .startx = FFMAX(0, -offx), > - .starty = FFMAX(0, -offy), > - .endx = FFMIN(w, w - offx), > - .endy = FFMIN(h, h - offy), > - .ii_start = centered_ii + offy*s->ii_lz_32 + offx, > - .p = p, > - }; > - > - compute_ssd_integral_image(&s->dsp, s->ii, s->ii_lz_32, > - src, src_linesize, > - offx, offy, e, w, h); > - ctx->internal->execute(ctx, nlmeans_slice, &td, NULL, > - FFMIN(td.endy - td.starty, ff_filter_get_nb_threads(ctx))); > - } > - } > - } > - > - weight_averages(dst, dst_linesize, src, src_linesize, > - s->wa, s->wa_linesize, w, h); > - > - return 0; > -} > - > static int filter_frame(AVFilterLink *inlink, AVFrame *in) > { > int i; > @@ -489,15 +119,30 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) > const int h = i ? s->chroma_h : inlink->h; > const int p = i ? s->patch_hsize_uv : s->patch_hsize; > const int r = i ? s->research_hsize_uv : s->research_hsize; > - nlmeans_plane(ctx, w, h, p, r, > - out->data[i], out->linesize[i], > - in->data[i], in->linesize[i]); > + s->nlmeans_plane(ctx, w, h, p, r, > + out->data[i], out->linesize[i], > + in->data[i], in->linesize[i]); > } > > av_frame_free(&in); > return ff_filter_frame(outlink, out); > } > > +void ff_nlmeans_init(NLMeansDSPContext *dsp, int depth) > +{ > + switch (depth) { > + case 8: dsp->compute_safe_ssd_integral_image = compute_safe_ssd_integral_image_c8; break; > + case 9: dsp->compute_safe_ssd_integral_image = compute_safe_ssd_integral_image_c9; break; > + case 10: dsp->compute_safe_ssd_integral_image = compute_safe_ssd_integral_image_c10; break; > + case 12: dsp->compute_safe_ssd_integral_image = compute_safe_ssd_integral_image_c12; break; > + case 14: dsp->compute_safe_ssd_integral_image = compute_safe_ssd_integral_image_c14; break; > + case 16: dsp->compute_safe_ssd_integral_image = compute_safe_ssd_integral_image_c16; break; > + } > + > + if (ARCH_AARCH64) > + ff_nlmeans_init_aarch64(dsp, depth); > +} > + > #define CHECK_ODD_FIELD(field, name) do { \ > if (!(s->field & 1)) { \ > s->field |= 1; \ > @@ -506,26 +151,22 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) > } \ > } while (0) > > -void ff_nlmeans_init(NLMeansDSPContext *dsp) > -{ > - dsp->compute_safe_ssd_integral_image = compute_safe_ssd_integral_image_c; > - > - if (ARCH_AARCH64) > - ff_nlmeans_init_aarch64(dsp); > -} > - > -static av_cold int init(AVFilterContext *ctx) > +static int config_input(AVFilterLink *inlink) > { > - int i; > + AVFilterContext *ctx = inlink->dst; > NLMeansContext *s = ctx->priv; > - const double h = s->sigma * 10.; > + const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format); > + double h; > + int e; > > + s->depth = desc->comp[0].depth; > + h = s->sigma * 10. * (1 << (s->depth - 8)); > s->pdiff_scale = 1. / (h * h); > - s->max_meaningful_diff = log(255.) / s->pdiff_scale; > + s->max_meaningful_diff = FFMIN(log(255.) / s->pdiff_scale, INT32_MAX / 8); > s->weight_lut = av_calloc(s->max_meaningful_diff, sizeof(*s->weight_lut)); > if (!s->weight_lut) > return AVERROR(ENOMEM); > - for (i = 0; i < s->max_meaningful_diff; i++) > + for (int i = 0; i < s->max_meaningful_diff; i++) > s->weight_lut[i] = exp(-i * s->pdiff_scale); > > CHECK_ODD_FIELD(research_size, "Luma research window"); > @@ -542,11 +183,65 @@ static av_cold int init(AVFilterContext *ctx) > s->patch_hsize = s->patch_size / 2; > s->patch_hsize_uv = s->patch_size_uv / 2; > > + e = FFMAX(s->research_hsize, s->research_hsize_uv) + > + FFMAX(s->patch_hsize, s->patch_hsize_uv); > av_log(ctx, AV_LOG_INFO, "Research window: %dx%d / %dx%d, patch size: %dx%d / %dx%d\n", > s->research_size, s->research_size, s->research_size_uv, s->research_size_uv, > s->patch_size, s->patch_size, s->patch_size_uv, s->patch_size_uv); > > - ff_nlmeans_init(&s->dsp); > + ff_nlmeans_init(&s->dsp, s->depth); > + > + s->chroma_w = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w); > + s->chroma_h = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h); > + s->nb_planes = av_pix_fmt_count_planes(inlink->format); > + > + /* Allocate the integral image with extra edges of thickness "e" > + * > + * +_+-------------------------------+ > + * |0|0000000000000000000000000000000| > + * +-x-------------------------------+ > + * |0|\ ^ | > + * |0| ii | e | > + * |0| v | > + * |0| +-----------------------+ | > + * |0| | | | > + * |0|<->| | | > + * |0| e | | | > + * |0| | | | > + * |0| +-----------------------+ | > + * |0| | > + * |0| | > + * |0| | > + * +-+-------------------------------+ > + */ > + s->ii_w = inlink->w + e*2; > + s->ii_h = inlink->h + e*2; > + > + // align to 4 the linesize, "+1" is for the space of the left 0-column > + s->ii_lz_32 = FFALIGN(s->ii_w + 1, 4); > + > + // "+1" is for the space of the top 0-line > + s->ii_orig = av_mallocz_array(s->ii_h + 1, (1 + (s->depth > 8)) * s->ii_lz_32 * sizeof(*s->ii_orig)); > + if (!s->ii_orig) > + return AVERROR(ENOMEM); > + > + // skip top 0-line and left 0-column > + s->ii = s->ii_orig + (s->ii_lz_32 + 1) * (1 + (s->depth > 8)); > + > + // allocate weighted average for every pixel > + s->wa_linesize = inlink->w; > + s->wa = av_malloc_array(s->wa_linesize, inlink->h * sizeof(*s->wa)); > + if (!s->wa) > + return AVERROR(ENOMEM); > + > + switch (s->depth) { > + case 8: s->nlmeans_plane = nlmeans_plane8; break; > + case 9: s->nlmeans_plane = nlmeans_plane9; break; > + case 10: s->nlmeans_plane = nlmeans_plane10; break; > + case 12: s->nlmeans_plane = nlmeans_plane12; break; > + case 14: s->nlmeans_plane = nlmeans_plane14; break; > + case 16: s->nlmeans_plane = nlmeans_plane16; break; > + } > > return 0; > } > @@ -581,7 +276,6 @@ AVFilter ff_vf_nlmeans = { > .name = "nlmeans", > .description = NULL_IF_CONFIG_SMALL("Non-local means denoiser."), > .priv_size = sizeof(NLMeansContext), > - .init = init, > .uninit = uninit, > .query_formats = query_formats, > .inputs = nlmeans_inputs, > diff --git a/libavfilter/vf_nlmeans.h b/libavfilter/vf_nlmeans.h > index 0a9aab2928..d8fb5588c6 100644 > --- a/libavfilter/vf_nlmeans.h > +++ b/libavfilter/vf_nlmeans.h > @@ -29,7 +29,47 @@ typedef struct NLMeansDSPContext { > int w, int h); > } NLMeansDSPContext; > > -void ff_nlmeans_init(NLMeansDSPContext *dsp); > -void ff_nlmeans_init_aarch64(NLMeansDSPContext *dsp); > +struct weighted_avg { > + float total_weight; > + float sum; > +}; > + > +struct thread_data { > + const uint8_t *src; > + ptrdiff_t src_linesize; > + int startx, starty; > + int endx, endy; > + const uint32_t *ii_start; > + int p; > +}; > + typedef struct ThreadData like other filters with slice thread support. > +typedef struct NLMeansContext { > + const AVClass *class; > + int nb_planes; > + int depth; > + int chroma_w, chroma_h; > + double pdiff_scale; // invert of the filtering parameter (sigma*10) squared > + double sigma; // denoising strength > + int patch_size, patch_hsize; // patch size and half size > + int patch_size_uv, patch_hsize_uv; // patch size and half size for chroma planes > + int research_size, research_hsize; // research size and half size > + int research_size_uv, research_hsize_uv; // research size and half size for chroma planes > + uint32_t *ii_orig; // integral image > + uint32_t *ii; // integral image starting after the 0-line and 0-column I don't think keep the integral image in uint32 is good idea, more infor you can refer to the link: https://patchwork.ffmpeg.org/patch/12242 > + int ii_w, ii_h; // width and height of the integral image > + ptrdiff_t ii_lz_32; // linesize in 32-bit units of the integral image > + struct weighted_avg *wa; // weighted average of every pixel > + ptrdiff_t wa_linesize; // linesize for wa in struct size unit > + float *weight_lut; // lookup table mapping (scaled) patch differences to their associated weights > + uint32_t max_meaningful_diff; // maximum difference considered (if the patch difference is too high we ignore the pixel) > + NLMeansDSPContext dsp; > + > + int (*nlmeans_plane)(AVFilterContext *ctx, int w, int h, int p, int r, > + uint8_t *dst, ptrdiff_t dst_linesize, > + const uint8_t *src, ptrdiff_t src_linesize); > +} NLMeansContext; > + > +void ff_nlmeans_init(NLMeansDSPContext *dsp, int depth); > +void ff_nlmeans_init_aarch64(NLMeansDSPContext *dsp, int depth); > > #endif /* AVFILTER_NLMEANS_H */ > -- > 2.17.1 >
On 11/20/19, mypopy@gmail.com <mypopy@gmail.com> wrote: > On Wed, Nov 20, 2019 at 6:43 AM Paul B Mahol <onemda@gmail.com> wrote: >> >> Signed-off-by: Paul B Mahol <onemda@gmail.com> >> --- >> libavfilter/aarch64/vf_nlmeans_init.c | 4 +- >> libavfilter/nlmeans_template.c | 366 ++++++++++++++++++ >> libavfilter/vf_nlmeans.c | 526 ++++++-------------------- >> libavfilter/vf_nlmeans.h | 44 ++- >> 4 files changed, 520 insertions(+), 420 deletions(-) >> create mode 100644 libavfilter/nlmeans_template.c >> >> diff --git a/libavfilter/aarch64/vf_nlmeans_init.c >> b/libavfilter/aarch64/vf_nlmeans_init.c >> index a1edefb144..ee87c88dd6 100644 >> --- a/libavfilter/aarch64/vf_nlmeans_init.c >> +++ b/libavfilter/aarch64/vf_nlmeans_init.c >> @@ -24,10 +24,10 @@ void ff_compute_safe_ssd_integral_image_neon(uint32_t >> *dst, ptrdiff_t dst_linesi >> const uint8_t *s2, ptrdiff_t >> linesize2, >> int w, int h); >> >> -av_cold void ff_nlmeans_init_aarch64(NLMeansDSPContext *dsp) >> +av_cold void ff_nlmeans_init_aarch64(NLMeansDSPContext *dsp, int depth) >> { >> int cpu_flags = av_get_cpu_flags(); >> >> - if (have_neon(cpu_flags)) >> + if (have_neon(cpu_flags) && depth == 8) >> dsp->compute_safe_ssd_integral_image = >> ff_compute_safe_ssd_integral_image_neon; >> } >> diff --git a/libavfilter/nlmeans_template.c >> b/libavfilter/nlmeans_template.c >> new file mode 100644 >> index 0000000000..64ac2e9e9b >> --- /dev/null >> +++ b/libavfilter/nlmeans_template.c >> @@ -0,0 +1,366 @@ >> +/* >> + * Copyright (c) 2016 Clément Bœsch <u pkh me> >> + * >> + * This file is part of FFmpeg. >> + * >> + * FFmpeg is free software; you can redistribute it and/or >> + * modify it under the terms of the GNU Lesser General Public >> + * License as published by the Free Software Foundation; either >> + * version 2.1 of the License, or (at your option) any later version. >> + * >> + * FFmpeg is distributed in the hope that it will be useful, >> + * but WITHOUT ANY WARRANTY; without even the implied warranty of >> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU >> + * Lesser General Public License for more details. >> + * >> + * You should have received a copy of the GNU Lesser General Public >> + * License along with FFmpeg; if not, write to the Free Software >> + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA >> 02110-1301 USA >> + */ >> + >> +#include "libavutil/avassert.h" >> +#include "avfilter.h" >> +#include "formats.h" >> +#include "internal.h" >> +#include "vf_nlmeans.h" >> +#include "video.h" >> + >> +#undef pixel >> +#undef integral >> +#if DEPTH <= 8 >> +#define pixel uint8_t >> +#define integral uint32_t >> +#else >> +#define pixel uint16_t >> +#define integral uint64_t >> +#endif >> + >> +#define fn3(a,b) a##b >> +#define fn2(a,b) fn3(a,b) >> +#define fn(a) fn2(a, DEPTH) >> + >> +/** >> + * Compute squared difference of the safe area (the zone where s1 and s2 >> + * overlap). It is likely the largest integral zone, so it is interesting >> to do >> + * as little checks as possible; contrary to the unsafe version of this >> + * function, we do not need any clipping here. >> + * >> + * The line above dst and the column to its left are always readable. >> + */ >> +static void fn(compute_safe_ssd_integral_image_c)(uint32_t *ddst, >> ptrdiff_t dst_linesize_32, >> + const uint8_t *ss1, >> ptrdiff_t linesize1, >> + const uint8_t *ss2, >> ptrdiff_t linesize2, >> + int w, int h) >> +{ >> + const pixel *s1 = (const pixel *)ss1; >> + const pixel *s2 = (const pixel *)ss2; >> + int x, y; >> + integral *dst = (integral *)ddst; >> + const integral *dst_top = dst - dst_linesize_32; >> + >> + linesize1 /= sizeof(pixel); >> + linesize2 /= sizeof(pixel); >> + >> + /* SIMD-friendly assumptions allowed here */ >> + av_assert2(!(w & 0xf) && w >= 16 && h >= 1); >> + >> + for (y = 0; y < h; y++) { >> + for (x = 0; x < w; x += 4) { >> + const int d0 = s1[x ] - s2[x ]; >> + const int d1 = s1[x + 1] - s2[x + 1]; >> + const int d2 = s1[x + 2] - s2[x + 2]; >> + const int d3 = s1[x + 3] - s2[x + 3]; >> + >> + dst[x ] = dst_top[x ] - dst_top[x - 1] + d0*d0; >> + dst[x + 1] = dst_top[x + 1] - dst_top[x ] + d1*d1; >> + dst[x + 2] = dst_top[x + 2] - dst_top[x + 1] + d2*d2; >> + dst[x + 3] = dst_top[x + 3] - dst_top[x + 2] + d3*d3; >> + >> + dst[x ] += dst[x - 1]; >> + dst[x + 1] += dst[x ]; >> + dst[x + 2] += dst[x + 1]; >> + dst[x + 3] += dst[x + 2]; >> + } >> + s1 += linesize1; >> + s2 += linesize2; >> + dst += dst_linesize_32; >> + dst_top += dst_linesize_32; >> + } >> +} >> + >> +/** >> + * Compute squared difference of an unsafe area (the zone nor s1 nor s2 >> could >> + * be readable). >> + * >> + * On the other hand, the line above dst and the column to its left are >> always >> + * readable. >> + * >> + * There is little point in having this function SIMDified as it is >> likely too >> + * complex and only handle small portions of the image. >> + * >> + * @param dst integral image >> + * @param dst_linesize_32 integral image linesize (in 32-bit integers >> unit) >> + * @param startx integral starting x position >> + * @param starty integral starting y position >> + * @param ssrc source plane buffer >> + * @param linesize source plane linesize >> + * @param offx source offsetting in x >> + * @param offy source offsetting in y >> + * @paran r absolute maximum source offsetting >> + * @param sw source width >> + * @param sh source height >> + * @param w width to compute >> + * @param h height to compute >> + */ >> +static inline void fn(compute_unsafe_ssd_integral_image)(uint32_t *ddst, >> ptrdiff_t dst_linesize_32, >> + int startx, int >> starty, >> + const uint8_t >> *ssrc, ptrdiff_t linesize, >> + int offx, int >> offy, int r, int sw, int sh, >> + int w, int h) >> +{ >> + integral *dst = (integral *)ddst; >> + const pixel *src = (const pixel *)ssrc; >> + int x, y; >> + >> + linesize /= sizeof(pixel); >> + >> + for (y = starty; y < starty + h; y++) { >> + integral acc = dst[y*dst_linesize_32 + startx - 1] - >> dst[(y-1)*dst_linesize_32 + startx - 1]; >> + const int s1y = av_clip(y - r, 0, sh - 1); >> + const int s2y = av_clip(y - (r + offy), 0, sh - 1); >> + >> + for (x = startx; x < startx + w; x++) { >> + const int s1x = av_clip(x - r, 0, sw - 1); >> + const int s2x = av_clip(x - (r + offx), 0, sw - 1); >> + const pixel v1 = src[s1y*linesize + s1x]; >> + const pixel v2 = src[s2y*linesize + s2x]; >> + const int d = v1 - v2; >> + acc += d * d; >> + dst[y*dst_linesize_32 + x] = dst[(y-1)*dst_linesize_32 + x] + >> acc; >> + } >> + } >> +} >> + >> +/* >> + * Compute the sum of squared difference integral image >> + * http://www.ipol.im/pub/art/2014/57/ >> + * Integral Images for Block Matching - Gabriele Facciolo, Nicolas >> Limare, Enric Meinhardt-Llopis >> + * >> + * @param ii integral image of dimension (w+e*2) x (h+e*2) >> with >> + * an additional zeroed top line and column >> already >> + * "applied" to the pointer value >> + * @param ii_linesize_32 integral image linesize (in 32-bit integers >> unit) >> + * @param src source plane buffer >> + * @param linesize source plane linesize >> + * @param offx x-offsetting ranging in [-e;e] >> + * @param offy y-offsetting ranging in [-e;e] >> + * @param w source width >> + * @param h source height >> + * @param e research padding edge >> + */ >> +static void fn(compute_ssd_integral_image)(const NLMeansDSPContext *dsp, >> + integral *ii, ptrdiff_t >> ii_linesize_32, >> + const uint8_t *src, ptrdiff_t >> linesize, int offx, int offy, >> + int e, int w, int h) >> +{ >> + // ii has a surrounding padding of thickness "e" >> + const int ii_w = w + e*2; >> + const int ii_h = h + e*2; >> + >> + // we center the first source >> + const int s1x = e; >> + const int s1y = e; >> + >> + // 2nd source is the frame with offsetting >> + const int s2x = e + offx; >> + const int s2y = e + offy; >> + >> + // get the dimension of the overlapping rectangle where it is always >> safe >> + // to compare the 2 sources pixels >> + const int startx_safe = FFMAX(s1x, s2x); >> + const int starty_safe = FFMAX(s1y, s2y); >> + const int u_endx_safe = FFMIN(s1x + w, s2x + w); // unaligned >> + const int endy_safe = FFMIN(s1y + h, s2y + h); >> + >> + // deduce the safe area width and height >> + const int safe_pw = (u_endx_safe - startx_safe) & ~0xf; >> + const int safe_ph = endy_safe - starty_safe; >> + >> + // adjusted end x position of the safe area after width of the safe >> area gets aligned >> + const int endx_safe = startx_safe + safe_pw; >> + >> + // top part where only one of s1 and s2 is still readable, or none at >> all >> + fn(compute_unsafe_ssd_integral_image)((uint32_t *)(ii), >> ii_linesize_32, >> + 0, 0, >> + src, linesize, >> + offx, offy, e, w, h, >> + ii_w, starty_safe); >> + >> + // fill the left column integral required to compute the central >> + // overlapping one >> + fn(compute_unsafe_ssd_integral_image)((uint32_t *)(ii), >> ii_linesize_32, >> + 0, starty_safe, >> + src, linesize, >> + offx, offy, e, w, h, >> + startx_safe, safe_ph); >> + >> + // main and safe part of the integral >> + av_assert1(startx_safe - s1x >= 0); av_assert1(startx_safe - s1x < >> w); >> + av_assert1(starty_safe - s1y >= 0); av_assert1(starty_safe - s1y < >> h); >> + av_assert1(startx_safe - s2x >= 0); av_assert1(startx_safe - s2x < >> w); >> + av_assert1(starty_safe - s2y >= 0); av_assert1(starty_safe - s2y < >> h); >> + if (safe_pw && safe_ph) >> + dsp->compute_safe_ssd_integral_image((uint32_t *)(ii + >> starty_safe*ii_linesize_32 + startx_safe), ii_linesize_32, >> + src + (starty_safe - s1y) * >> linesize + (startx_safe - s1x) * sizeof(pixel), linesize, >> + src + (starty_safe - s2y) * >> linesize + (startx_safe - s2x) * sizeof(pixel), linesize, >> + safe_pw, safe_ph); >> + >> + // right part of the integral >> + fn(compute_unsafe_ssd_integral_image)((uint32_t *)(ii), >> ii_linesize_32, >> + endx_safe, starty_safe, >> + src, linesize, >> + offx, offy, e, w, h, >> + ii_w - endx_safe, safe_ph); >> + >> + // bottom part where only one of s1 and s2 is still readable, or none >> at all >> + fn(compute_unsafe_ssd_integral_image)((uint32_t *)(ii), >> ii_linesize_32, >> + 0, endy_safe, >> + src, linesize, >> + offx, offy, e, w, h, >> + ii_w, ii_h - endy_safe); >> +} >> + >> +static int fn(nlmeans_slice)(AVFilterContext *ctx, void *arg, int jobnr, >> int nb_jobs) >> +{ >> + int x, y; >> + NLMeansContext *s = ctx->priv; >> + const struct thread_data *td = arg; >> + const ptrdiff_t src_linesize = td->src_linesize; >> + const int process_h = td->endy - td->starty; >> + const int slice_start = (process_h * jobnr ) / nb_jobs; >> + const int slice_end = (process_h * (jobnr+1)) / nb_jobs; >> + const int starty = td->starty + slice_start; >> + const int endy = td->starty + slice_end; >> + const int p = td->p; >> + const integral *ii = (const integral *)td->ii_start + (starty - p - >> 1) * s->ii_lz_32 - p - 1; >> + const int dist_b = 2*p + 1; >> + const int dist_d = dist_b * s->ii_lz_32; >> + const int dist_e = dist_d + dist_b; >> + >> + for (y = starty; y < endy; y++) { >> + const pixel *src = (const pixel *)(td->src + y*src_linesize); >> + struct weighted_avg *wa = s->wa + y*s->wa_linesize; >> + for (x = td->startx; x < td->endx; x++) { >> + /* >> + * M is a discrete map where every entry contains the sum of >> all the entries >> + * in the rectangle from the top-left origin of M to its >> coordinate. In the >> + * following schema, "i" contains the sum of the whole map: >> + * >> + * M = +----------+-----------------+----+ >> + * | | | | >> + * | | | | >> + * | a| b| c| >> + * +----------+-----------------+----+ >> + * | | | | >> + * | | | | >> + * | | X | | >> + * | | | | >> + * | d| e| f| >> + * +----------+-----------------+----+ >> + * | | | | >> + * | g| h| i| >> + * +----------+-----------------+----+ >> + * >> + * The sum of the X box can be calculated with: >> + * X = e-d-b+a >> + * >> + * See https://en.wikipedia.org/wiki/Summed_area_table >> + * >> + * The compute*_ssd functions compute the integral image M >> where every entry >> + * contains the sum of the squared difference of every >> corresponding pixels of >> + * two input planes of the same size as M. >> + */ >> + const integral a = ii[x]; >> + const integral b = ii[x + dist_b]; >> + const integral d = ii[x + dist_d]; >> + const integral e = ii[x + dist_e]; >> + const integral patch_diff_sq = e + a - d - b; >> + >> + if (patch_diff_sq < s->max_meaningful_diff) { >> + const float weight = s->weight_lut[patch_diff_sq]; // >> exp(-patch_diff_sq * s->pdiff_scale) >> + wa[x].total_weight += weight; >> + wa[x].sum += weight * src[x]; >> + } >> + } >> + ii += s->ii_lz_32; >> + } >> + return 0; >> +} >> + >> +static void fn(weight_averages)(uint8_t *ddst, ptrdiff_t dst_linesize, >> + const uint8_t *ssrc, ptrdiff_t >> src_linesize, >> + struct weighted_avg *wa, ptrdiff_t >> wa_linesize, >> + int w, int h) >> +{ >> + const pixel *src = (const pixel *)ssrc; >> + pixel *dst = (pixel *)ddst; >> + int x, y; >> + >> + src_linesize /= sizeof(pixel); >> + dst_linesize /= sizeof(pixel); >> + >> + for (y = 0; y < h; y++) { >> + for (x = 0; x < w; x++) { >> + // Also weight the centered pixel >> + wa[x].total_weight += 1.f; >> + wa[x].sum += 1.f * src[x]; >> + dst[x] = av_clip_uintp2(wa[x].sum / wa[x].total_weight + >> 0.5f, DEPTH); >> + } >> + dst += dst_linesize; >> + src += src_linesize; >> + wa += wa_linesize; >> + } >> +} >> + >> +static int fn(nlmeans_plane)(AVFilterContext *ctx, int w, int h, int p, >> int r, >> + uint8_t *dst, ptrdiff_t dst_linesize, >> + const uint8_t *src, ptrdiff_t src_linesize) >> +{ >> + int offx, offy; >> + NLMeansContext *s = ctx->priv; >> + /* patches center points cover the whole research window so the >> patches >> + * themselves overflow the research window */ >> + const int e = r + p; >> + /* focus an integral pointer on the centered image (s1) */ >> + const integral *centered_ii = (const integral *)s->ii + e*s->ii_lz_32 >> + e; >> + >> + memset(s->wa, 0, s->wa_linesize * h * sizeof(*s->wa)); >> + >> + for (offy = -r; offy <= r; offy++) { >> + for (offx = -r; offx <= r; offx++) { >> + if (offx || offy) { >> + struct thread_data td = { >> + .src = src + offy*src_linesize + offx * >> (int)(sizeof(pixel)), >> + .src_linesize = src_linesize, >> + .startx = FFMAX(0, -offx), >> + .starty = FFMAX(0, -offy), >> + .endx = FFMIN(w, w - offx), >> + .endy = FFMIN(h, h - offy), >> + .ii_start = (uint32_t *)(centered_ii + >> offy*s->ii_lz_32 + offx), >> + .p = p, >> + }; >> + >> + fn(compute_ssd_integral_image)(&s->dsp, (integral >> *)s->ii, s->ii_lz_32, >> + src, src_linesize, >> + offx, offy, e, w, h); >> + ctx->internal->execute(ctx, fn(nlmeans_slice), &td, NULL, >> + FFMIN(td.endy - td.starty, >> ff_filter_get_nb_threads(ctx))); >> + } >> + } >> + } >> + >> + fn(weight_averages)(dst, dst_linesize, src, src_linesize, >> + s->wa, s->wa_linesize, w, h); >> + >> + return 0; >> +} >> diff --git a/libavfilter/vf_nlmeans.c b/libavfilter/vf_nlmeans.c >> index 06233b0dd4..49edc1f8c7 100644 >> --- a/libavfilter/vf_nlmeans.c >> +++ b/libavfilter/vf_nlmeans.c >> @@ -38,31 +38,28 @@ >> #include "vf_nlmeans.h" >> #include "video.h" >> >> -struct weighted_avg { >> - float total_weight; >> - float sum; >> -}; >> +#define DEPTH 8 >> +#include "nlmeans_template.c" >> + >> +#undef DEPTH >> +#define DEPTH 9 >> +#include "nlmeans_template.c" >> + >> +#undef DEPTH >> +#define DEPTH 10 >> +#include "nlmeans_template.c" >> + >> +#undef DEPTH >> +#define DEPTH 12 >> +#include "nlmeans_template.c" >> + >> +#undef DEPTH >> +#define DEPTH 14 >> +#include "nlmeans_template.c" >> >> -typedef struct NLMeansContext { >> - const AVClass *class; >> - int nb_planes; >> - int chroma_w, chroma_h; >> - double pdiff_scale; // invert of the >> filtering parameter (sigma*10) squared >> - double sigma; // denoising strength >> - int patch_size, patch_hsize; // patch size and half >> size >> - int patch_size_uv, patch_hsize_uv; // patch size and half >> size for chroma planes >> - int research_size, research_hsize; // research size and half >> size >> - int research_size_uv, research_hsize_uv; // research size and half >> size for chroma planes >> - uint32_t *ii_orig; // integral image >> - uint32_t *ii; // integral image >> starting after the 0-line and 0-column >> - int ii_w, ii_h; // width and height of >> the integral image >> - ptrdiff_t ii_lz_32; // linesize in 32-bit >> units of the integral image >> - struct weighted_avg *wa; // weighted average of >> every pixel >> - ptrdiff_t wa_linesize; // linesize for wa in >> struct size unit >> - float *weight_lut; // lookup table mapping >> (scaled) patch differences to their associated weights >> - uint32_t max_meaningful_diff; // maximum difference >> considered (if the patch difference is too high we ignore the pixel) >> - NLMeansDSPContext dsp; >> -} NLMeansContext; >> +#undef DEPTH >> +#define DEPTH 16 >> +#include "nlmeans_template.c" >> >> #define OFFSET(x) offsetof(NLMeansContext, x) >> #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM >> @@ -87,6 +84,13 @@ static int query_formats(AVFilterContext *ctx) >> AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P, >> AV_PIX_FMT_YUVJ411P, >> AV_PIX_FMT_GRAY8, AV_PIX_FMT_GBRP, >> + AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9, >> + AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10, >> + AV_PIX_FMT_YUV440P10, >> + AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12, >> + AV_PIX_FMT_YUV440P12, >> + AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV420P14, >> + AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16, >> AV_PIX_FMT_NONE >> }; >> >> @@ -96,380 +100,6 @@ static int query_formats(AVFilterContext *ctx) >> return ff_set_common_formats(ctx, fmts_list); >> } >> >> -/** >> - * Compute squared difference of the safe area (the zone where s1 and s2 >> - * overlap). It is likely the largest integral zone, so it is interesting >> to do >> - * as little checks as possible; contrary to the unsafe version of this >> - * function, we do not need any clipping here. >> - * >> - * The line above dst and the column to its left are always readable. >> - */ >> -static void compute_safe_ssd_integral_image_c(uint32_t *dst, ptrdiff_t >> dst_linesize_32, >> - const uint8_t *s1, >> ptrdiff_t linesize1, >> - const uint8_t *s2, >> ptrdiff_t linesize2, >> - int w, int h) >> -{ >> - int x, y; >> - const uint32_t *dst_top = dst - dst_linesize_32; >> - >> - /* SIMD-friendly assumptions allowed here */ >> - av_assert2(!(w & 0xf) && w >= 16 && h >= 1); >> - >> - for (y = 0; y < h; y++) { >> - for (x = 0; x < w; x += 4) { >> - const int d0 = s1[x ] - s2[x ]; >> - const int d1 = s1[x + 1] - s2[x + 1]; >> - const int d2 = s1[x + 2] - s2[x + 2]; >> - const int d3 = s1[x + 3] - s2[x + 3]; >> - >> - dst[x ] = dst_top[x ] - dst_top[x - 1] + d0*d0; >> - dst[x + 1] = dst_top[x + 1] - dst_top[x ] + d1*d1; >> - dst[x + 2] = dst_top[x + 2] - dst_top[x + 1] + d2*d2; >> - dst[x + 3] = dst_top[x + 3] - dst_top[x + 2] + d3*d3; >> - >> - dst[x ] += dst[x - 1]; >> - dst[x + 1] += dst[x ]; >> - dst[x + 2] += dst[x + 1]; >> - dst[x + 3] += dst[x + 2]; >> - } >> - s1 += linesize1; >> - s2 += linesize2; >> - dst += dst_linesize_32; >> - dst_top += dst_linesize_32; >> - } >> -} >> - >> -/** >> - * Compute squared difference of an unsafe area (the zone nor s1 nor s2 >> could >> - * be readable). >> - * >> - * On the other hand, the line above dst and the column to its left are >> always >> - * readable. >> - * >> - * There is little point in having this function SIMDified as it is >> likely too >> - * complex and only handle small portions of the image. >> - * >> - * @param dst integral image >> - * @param dst_linesize_32 integral image linesize (in 32-bit integers >> unit) >> - * @param startx integral starting x position >> - * @param starty integral starting y position >> - * @param src source plane buffer >> - * @param linesize source plane linesize >> - * @param offx source offsetting in x >> - * @param offy source offsetting in y >> - * @paran r absolute maximum source offsetting >> - * @param sw source width >> - * @param sh source height >> - * @param w width to compute >> - * @param h height to compute >> - */ >> -static inline void compute_unsafe_ssd_integral_image(uint32_t *dst, >> ptrdiff_t dst_linesize_32, >> - int startx, int >> starty, >> - const uint8_t *src, >> ptrdiff_t linesize, >> - int offx, int offy, >> int r, int sw, int sh, >> - int w, int h) >> -{ >> - int x, y; >> - >> - for (y = starty; y < starty + h; y++) { >> - uint32_t acc = dst[y*dst_linesize_32 + startx - 1] - >> dst[(y-1)*dst_linesize_32 + startx - 1]; >> - const int s1y = av_clip(y - r, 0, sh - 1); >> - const int s2y = av_clip(y - (r + offy), 0, sh - 1); >> - >> - for (x = startx; x < startx + w; x++) { >> - const int s1x = av_clip(x - r, 0, sw - 1); >> - const int s2x = av_clip(x - (r + offx), 0, sw - 1); >> - const uint8_t v1 = src[s1y*linesize + s1x]; >> - const uint8_t v2 = src[s2y*linesize + s2x]; >> - const int d = v1 - v2; >> - acc += d * d; >> - dst[y*dst_linesize_32 + x] = dst[(y-1)*dst_linesize_32 + x] + >> acc; >> - } >> - } >> -} >> - >> -/* >> - * Compute the sum of squared difference integral image >> - * http://www.ipol.im/pub/art/2014/57/ >> - * Integral Images for Block Matching - Gabriele Facciolo, Nicolas >> Limare, Enric Meinhardt-Llopis >> - * >> - * @param ii integral image of dimension (w+e*2) x (h+e*2) >> with >> - * an additional zeroed top line and column >> already >> - * "applied" to the pointer value >> - * @param ii_linesize_32 integral image linesize (in 32-bit integers >> unit) >> - * @param src source plane buffer >> - * @param linesize source plane linesize >> - * @param offx x-offsetting ranging in [-e;e] >> - * @param offy y-offsetting ranging in [-e;e] >> - * @param w source width >> - * @param h source height >> - * @param e research padding edge >> - */ >> -static void compute_ssd_integral_image(const NLMeansDSPContext *dsp, >> - uint32_t *ii, ptrdiff_t >> ii_linesize_32, >> - const uint8_t *src, ptrdiff_t >> linesize, int offx, int offy, >> - int e, int w, int h) >> -{ >> - // ii has a surrounding padding of thickness "e" >> - const int ii_w = w + e*2; >> - const int ii_h = h + e*2; >> - >> - // we center the first source >> - const int s1x = e; >> - const int s1y = e; >> - >> - // 2nd source is the frame with offsetting >> - const int s2x = e + offx; >> - const int s2y = e + offy; >> - >> - // get the dimension of the overlapping rectangle where it is always >> safe >> - // to compare the 2 sources pixels >> - const int startx_safe = FFMAX(s1x, s2x); >> - const int starty_safe = FFMAX(s1y, s2y); >> - const int u_endx_safe = FFMIN(s1x + w, s2x + w); // unaligned >> - const int endy_safe = FFMIN(s1y + h, s2y + h); >> - >> - // deduce the safe area width and height >> - const int safe_pw = (u_endx_safe - startx_safe) & ~0xf; >> - const int safe_ph = endy_safe - starty_safe; >> - >> - // adjusted end x position of the safe area after width of the safe >> area gets aligned >> - const int endx_safe = startx_safe + safe_pw; >> - >> - // top part where only one of s1 and s2 is still readable, or none at >> all >> - compute_unsafe_ssd_integral_image(ii, ii_linesize_32, >> - 0, 0, >> - src, linesize, >> - offx, offy, e, w, h, >> - ii_w, starty_safe); >> - >> - // fill the left column integral required to compute the central >> - // overlapping one >> - compute_unsafe_ssd_integral_image(ii, ii_linesize_32, >> - 0, starty_safe, >> - src, linesize, >> - offx, offy, e, w, h, >> - startx_safe, safe_ph); >> - >> - // main and safe part of the integral >> - av_assert1(startx_safe - s1x >= 0); av_assert1(startx_safe - s1x < >> w); >> - av_assert1(starty_safe - s1y >= 0); av_assert1(starty_safe - s1y < >> h); >> - av_assert1(startx_safe - s2x >= 0); av_assert1(startx_safe - s2x < >> w); >> - av_assert1(starty_safe - s2y >= 0); av_assert1(starty_safe - s2y < >> h); >> - if (safe_pw && safe_ph) >> - dsp->compute_safe_ssd_integral_image(ii + >> starty_safe*ii_linesize_32 + startx_safe, ii_linesize_32, >> - src + (starty_safe - s1y) * >> linesize + (startx_safe - s1x), linesize, >> - src + (starty_safe - s2y) * >> linesize + (startx_safe - s2x), linesize, >> - safe_pw, safe_ph); >> - >> - // right part of the integral >> - compute_unsafe_ssd_integral_image(ii, ii_linesize_32, >> - endx_safe, starty_safe, >> - src, linesize, >> - offx, offy, e, w, h, >> - ii_w - endx_safe, safe_ph); >> - >> - // bottom part where only one of s1 and s2 is still readable, or none >> at all >> - compute_unsafe_ssd_integral_image(ii, ii_linesize_32, >> - 0, endy_safe, >> - src, linesize, >> - offx, offy, e, w, h, >> - ii_w, ii_h - endy_safe); >> -} >> - >> -static int config_input(AVFilterLink *inlink) >> -{ >> - AVFilterContext *ctx = inlink->dst; >> - NLMeansContext *s = ctx->priv; >> - const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format); >> - const int e = FFMAX(s->research_hsize, s->research_hsize_uv) >> - + FFMAX(s->patch_hsize, s->patch_hsize_uv); >> - >> - s->chroma_w = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w); >> - s->chroma_h = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h); >> - s->nb_planes = av_pix_fmt_count_planes(inlink->format); >> - >> - /* Allocate the integral image with extra edges of thickness "e" >> - * >> - * +_+-------------------------------+ >> - * |0|0000000000000000000000000000000| >> - * +-x-------------------------------+ >> - * |0|\ ^ | >> - * |0| ii | e | >> - * |0| v | >> - * |0| +-----------------------+ | >> - * |0| | | | >> - * |0|<->| | | >> - * |0| e | | | >> - * |0| | | | >> - * |0| +-----------------------+ | >> - * |0| | >> - * |0| | >> - * |0| | >> - * +-+-------------------------------+ >> - */ >> - s->ii_w = inlink->w + e*2; >> - s->ii_h = inlink->h + e*2; >> - >> - // align to 4 the linesize, "+1" is for the space of the left >> 0-column >> - s->ii_lz_32 = FFALIGN(s->ii_w + 1, 4); >> - >> - // "+1" is for the space of the top 0-line >> - s->ii_orig = av_mallocz_array(s->ii_h + 1, s->ii_lz_32 * >> sizeof(*s->ii_orig)); >> - if (!s->ii_orig) >> - return AVERROR(ENOMEM); >> - >> - // skip top 0-line and left 0-column >> - s->ii = s->ii_orig + s->ii_lz_32 + 1; >> - >> - // allocate weighted average for every pixel >> - s->wa_linesize = inlink->w; >> - s->wa = av_malloc_array(s->wa_linesize, inlink->h * sizeof(*s->wa)); >> - if (!s->wa) >> - return AVERROR(ENOMEM); >> - >> - return 0; >> -} >> - >> -struct thread_data { >> - const uint8_t *src; >> - ptrdiff_t src_linesize; >> - int startx, starty; >> - int endx, endy; >> - const uint32_t *ii_start; >> - int p; >> -}; >> - >> -static int nlmeans_slice(AVFilterContext *ctx, void *arg, int jobnr, int >> nb_jobs) >> -{ >> - int x, y; >> - NLMeansContext *s = ctx->priv; >> - const struct thread_data *td = arg; >> - const ptrdiff_t src_linesize = td->src_linesize; >> - const int process_h = td->endy - td->starty; >> - const int slice_start = (process_h * jobnr ) / nb_jobs; >> - const int slice_end = (process_h * (jobnr+1)) / nb_jobs; >> - const int starty = td->starty + slice_start; >> - const int endy = td->starty + slice_end; >> - const int p = td->p; >> - const uint32_t *ii = td->ii_start + (starty - p - 1) * s->ii_lz_32 - >> p - 1; >> - const int dist_b = 2*p + 1; >> - const int dist_d = dist_b * s->ii_lz_32; >> - const int dist_e = dist_d + dist_b; >> - >> - for (y = starty; y < endy; y++) { >> - const uint8_t *src = td->src + y*src_linesize; >> - struct weighted_avg *wa = s->wa + y*s->wa_linesize; >> - for (x = td->startx; x < td->endx; x++) { >> - /* >> - * M is a discrete map where every entry contains the sum of >> all the entries >> - * in the rectangle from the top-left origin of M to its >> coordinate. In the >> - * following schema, "i" contains the sum of the whole map: >> - * >> - * M = +----------+-----------------+----+ >> - * | | | | >> - * | | | | >> - * | a| b| c| >> - * +----------+-----------------+----+ >> - * | | | | >> - * | | | | >> - * | | X | | >> - * | | | | >> - * | d| e| f| >> - * +----------+-----------------+----+ >> - * | | | | >> - * | g| h| i| >> - * +----------+-----------------+----+ >> - * >> - * The sum of the X box can be calculated with: >> - * X = e-d-b+a >> - * >> - * See https://en.wikipedia.org/wiki/Summed_area_table >> - * >> - * The compute*_ssd functions compute the integral image M >> where every entry >> - * contains the sum of the squared difference of every >> corresponding pixels of >> - * two input planes of the same size as M. >> - */ >> - const uint32_t a = ii[x]; >> - const uint32_t b = ii[x + dist_b]; >> - const uint32_t d = ii[x + dist_d]; >> - const uint32_t e = ii[x + dist_e]; >> - const uint32_t patch_diff_sq = e - d - b + a; >> - >> - if (patch_diff_sq < s->max_meaningful_diff) { >> - const float weight = s->weight_lut[patch_diff_sq]; // >> exp(-patch_diff_sq * s->pdiff_scale) >> - wa[x].total_weight += weight; >> - wa[x].sum += weight * src[x]; >> - } >> - } >> - ii += s->ii_lz_32; >> - } >> - return 0; >> -} >> - >> -static void weight_averages(uint8_t *dst, ptrdiff_t dst_linesize, >> - const uint8_t *src, ptrdiff_t src_linesize, >> - struct weighted_avg *wa, ptrdiff_t >> wa_linesize, >> - int w, int h) >> -{ >> - int x, y; >> - >> - for (y = 0; y < h; y++) { >> - for (x = 0; x < w; x++) { >> - // Also weight the centered pixel >> - wa[x].total_weight += 1.f; >> - wa[x].sum += 1.f * src[x]; >> - dst[x] = av_clip_uint8(wa[x].sum / wa[x].total_weight + >> 0.5f); >> - } >> - dst += dst_linesize; >> - src += src_linesize; >> - wa += wa_linesize; >> - } >> -} >> - >> -static int nlmeans_plane(AVFilterContext *ctx, int w, int h, int p, int >> r, >> - uint8_t *dst, ptrdiff_t dst_linesize, >> - const uint8_t *src, ptrdiff_t src_linesize) >> -{ >> - int offx, offy; >> - NLMeansContext *s = ctx->priv; >> - /* patches center points cover the whole research window so the >> patches >> - * themselves overflow the research window */ >> - const int e = r + p; >> - /* focus an integral pointer on the centered image (s1) */ >> - const uint32_t *centered_ii = s->ii + e*s->ii_lz_32 + e; >> - >> - memset(s->wa, 0, s->wa_linesize * h * sizeof(*s->wa)); >> - >> - for (offy = -r; offy <= r; offy++) { >> - for (offx = -r; offx <= r; offx++) { >> - if (offx || offy) { >> - struct thread_data td = { >> - .src = src + offy*src_linesize + offx, >> - .src_linesize = src_linesize, >> - .startx = FFMAX(0, -offx), >> - .starty = FFMAX(0, -offy), >> - .endx = FFMIN(w, w - offx), >> - .endy = FFMIN(h, h - offy), >> - .ii_start = centered_ii + offy*s->ii_lz_32 + >> offx, >> - .p = p, >> - }; >> - >> - compute_ssd_integral_image(&s->dsp, s->ii, s->ii_lz_32, >> - src, src_linesize, >> - offx, offy, e, w, h); >> - ctx->internal->execute(ctx, nlmeans_slice, &td, NULL, >> - FFMIN(td.endy - td.starty, >> ff_filter_get_nb_threads(ctx))); >> - } >> - } >> - } >> - >> - weight_averages(dst, dst_linesize, src, src_linesize, >> - s->wa, s->wa_linesize, w, h); >> - >> - return 0; >> -} >> - >> static int filter_frame(AVFilterLink *inlink, AVFrame *in) >> { >> int i; >> @@ -489,15 +119,30 @@ static int filter_frame(AVFilterLink *inlink, >> AVFrame *in) >> const int h = i ? s->chroma_h : inlink->h; >> const int p = i ? s->patch_hsize_uv : s->patch_hsize; >> const int r = i ? s->research_hsize_uv : s->research_hsize; >> - nlmeans_plane(ctx, w, h, p, r, >> - out->data[i], out->linesize[i], >> - in->data[i], in->linesize[i]); >> + s->nlmeans_plane(ctx, w, h, p, r, >> + out->data[i], out->linesize[i], >> + in->data[i], in->linesize[i]); >> } >> >> av_frame_free(&in); >> return ff_filter_frame(outlink, out); >> } >> >> +void ff_nlmeans_init(NLMeansDSPContext *dsp, int depth) >> +{ >> + switch (depth) { >> + case 8: dsp->compute_safe_ssd_integral_image = >> compute_safe_ssd_integral_image_c8; break; >> + case 9: dsp->compute_safe_ssd_integral_image = >> compute_safe_ssd_integral_image_c9; break; >> + case 10: dsp->compute_safe_ssd_integral_image = >> compute_safe_ssd_integral_image_c10; break; >> + case 12: dsp->compute_safe_ssd_integral_image = >> compute_safe_ssd_integral_image_c12; break; >> + case 14: dsp->compute_safe_ssd_integral_image = >> compute_safe_ssd_integral_image_c14; break; >> + case 16: dsp->compute_safe_ssd_integral_image = >> compute_safe_ssd_integral_image_c16; break; >> + } >> + >> + if (ARCH_AARCH64) >> + ff_nlmeans_init_aarch64(dsp, depth); >> +} >> + >> #define CHECK_ODD_FIELD(field, name) do { \ >> if (!(s->field & 1)) { \ >> s->field |= 1; \ >> @@ -506,26 +151,22 @@ static int filter_frame(AVFilterLink *inlink, >> AVFrame *in) >> } \ >> } while (0) >> >> -void ff_nlmeans_init(NLMeansDSPContext *dsp) >> -{ >> - dsp->compute_safe_ssd_integral_image = >> compute_safe_ssd_integral_image_c; >> - >> - if (ARCH_AARCH64) >> - ff_nlmeans_init_aarch64(dsp); >> -} >> - >> -static av_cold int init(AVFilterContext *ctx) >> +static int config_input(AVFilterLink *inlink) >> { >> - int i; >> + AVFilterContext *ctx = inlink->dst; >> NLMeansContext *s = ctx->priv; >> - const double h = s->sigma * 10.; >> + const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format); >> + double h; >> + int e; >> >> + s->depth = desc->comp[0].depth; >> + h = s->sigma * 10. * (1 << (s->depth - 8)); >> s->pdiff_scale = 1. / (h * h); >> - s->max_meaningful_diff = log(255.) / s->pdiff_scale; >> + s->max_meaningful_diff = FFMIN(log(255.) / s->pdiff_scale, INT32_MAX >> / 8); >> s->weight_lut = av_calloc(s->max_meaningful_diff, >> sizeof(*s->weight_lut)); >> if (!s->weight_lut) >> return AVERROR(ENOMEM); >> - for (i = 0; i < s->max_meaningful_diff; i++) >> + for (int i = 0; i < s->max_meaningful_diff; i++) >> s->weight_lut[i] = exp(-i * s->pdiff_scale); >> >> CHECK_ODD_FIELD(research_size, "Luma research window"); >> @@ -542,11 +183,65 @@ static av_cold int init(AVFilterContext *ctx) >> s->patch_hsize = s->patch_size / 2; >> s->patch_hsize_uv = s->patch_size_uv / 2; >> >> + e = FFMAX(s->research_hsize, s->research_hsize_uv) + >> + FFMAX(s->patch_hsize, s->patch_hsize_uv); >> av_log(ctx, AV_LOG_INFO, "Research window: %dx%d / %dx%d, patch size: >> %dx%d / %dx%d\n", >> s->research_size, s->research_size, s->research_size_uv, >> s->research_size_uv, >> s->patch_size, s->patch_size, s->patch_size_uv, >> s->patch_size_uv); >> >> - ff_nlmeans_init(&s->dsp); >> + ff_nlmeans_init(&s->dsp, s->depth); >> + >> + s->chroma_w = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w); >> + s->chroma_h = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h); >> + s->nb_planes = av_pix_fmt_count_planes(inlink->format); >> + >> + /* Allocate the integral image with extra edges of thickness "e" >> + * >> + * +_+-------------------------------+ >> + * |0|0000000000000000000000000000000| >> + * +-x-------------------------------+ >> + * |0|\ ^ | >> + * |0| ii | e | >> + * |0| v | >> + * |0| +-----------------------+ | >> + * |0| | | | >> + * |0|<->| | | >> + * |0| e | | | >> + * |0| | | | >> + * |0| +-----------------------+ | >> + * |0| | >> + * |0| | >> + * |0| | >> + * +-+-------------------------------+ >> + */ >> + s->ii_w = inlink->w + e*2; >> + s->ii_h = inlink->h + e*2; >> + >> + // align to 4 the linesize, "+1" is for the space of the left >> 0-column >> + s->ii_lz_32 = FFALIGN(s->ii_w + 1, 4); >> + >> + // "+1" is for the space of the top 0-line >> + s->ii_orig = av_mallocz_array(s->ii_h + 1, (1 + (s->depth > 8)) * >> s->ii_lz_32 * sizeof(*s->ii_orig)); >> + if (!s->ii_orig) >> + return AVERROR(ENOMEM); >> + >> + // skip top 0-line and left 0-column >> + s->ii = s->ii_orig + (s->ii_lz_32 + 1) * (1 + (s->depth > 8)); >> + >> + // allocate weighted average for every pixel >> + s->wa_linesize = inlink->w; >> + s->wa = av_malloc_array(s->wa_linesize, inlink->h * sizeof(*s->wa)); >> + if (!s->wa) >> + return AVERROR(ENOMEM); >> + >> + switch (s->depth) { >> + case 8: s->nlmeans_plane = nlmeans_plane8; break; >> + case 9: s->nlmeans_plane = nlmeans_plane9; break; >> + case 10: s->nlmeans_plane = nlmeans_plane10; break; >> + case 12: s->nlmeans_plane = nlmeans_plane12; break; >> + case 14: s->nlmeans_plane = nlmeans_plane14; break; >> + case 16: s->nlmeans_plane = nlmeans_plane16; break; >> + } >> >> return 0; >> } >> @@ -581,7 +276,6 @@ AVFilter ff_vf_nlmeans = { >> .name = "nlmeans", >> .description = NULL_IF_CONFIG_SMALL("Non-local means denoiser."), >> .priv_size = sizeof(NLMeansContext), >> - .init = init, >> .uninit = uninit, >> .query_formats = query_formats, >> .inputs = nlmeans_inputs, >> diff --git a/libavfilter/vf_nlmeans.h b/libavfilter/vf_nlmeans.h >> index 0a9aab2928..d8fb5588c6 100644 >> --- a/libavfilter/vf_nlmeans.h >> +++ b/libavfilter/vf_nlmeans.h >> @@ -29,7 +29,47 @@ typedef struct NLMeansDSPContext { >> int w, int h); >> } NLMeansDSPContext; >> >> -void ff_nlmeans_init(NLMeansDSPContext *dsp); >> -void ff_nlmeans_init_aarch64(NLMeansDSPContext *dsp); >> +struct weighted_avg { >> + float total_weight; >> + float sum; >> +}; >> + >> +struct thread_data { >> + const uint8_t *src; >> + ptrdiff_t src_linesize; >> + int startx, starty; >> + int endx, endy; >> + const uint32_t *ii_start; >> + int p; >> +}; >> + > typedef struct ThreadData like other filters with slice thread support. >> +typedef struct NLMeansContext { >> + const AVClass *class; >> + int nb_planes; >> + int depth; >> + int chroma_w, chroma_h; >> + double pdiff_scale; // invert of the >> filtering parameter (sigma*10) squared >> + double sigma; // denoising strength >> + int patch_size, patch_hsize; // patch size and half >> size >> + int patch_size_uv, patch_hsize_uv; // patch size and half >> size for chroma planes >> + int research_size, research_hsize; // research size and half >> size >> + int research_size_uv, research_hsize_uv; // research size and half >> size for chroma planes >> + uint32_t *ii_orig; // integral image >> + uint32_t *ii; // integral image >> starting after the 0-line and 0-column > I don't think keep the integral image in uint32 is good idea, more > infor you can refer to the link: > https://patchwork.ffmpeg.org/patch/12242 Better look more carefully patch again. >> + int ii_w, ii_h; // width and height of >> the integral image >> + ptrdiff_t ii_lz_32; // linesize in 32-bit >> units of the integral image >> + struct weighted_avg *wa; // weighted average of >> every pixel >> + ptrdiff_t wa_linesize; // linesize for wa in >> struct size unit >> + float *weight_lut; // lookup table mapping >> (scaled) patch differences to their associated weights >> + uint32_t max_meaningful_diff; // maximum difference >> considered (if the patch difference is too high we ignore the pixel) >> + NLMeansDSPContext dsp; >> + >> + int (*nlmeans_plane)(AVFilterContext *ctx, int w, int h, int p, int >> r, >> + uint8_t *dst, ptrdiff_t dst_linesize, >> + const uint8_t *src, ptrdiff_t src_linesize); >> +} NLMeansContext; >> + >> +void ff_nlmeans_init(NLMeansDSPContext *dsp, int depth); >> +void ff_nlmeans_init_aarch64(NLMeansDSPContext *dsp, int depth); >> >> #endif /* AVFILTER_NLMEANS_H */ >> -- >> 2.17.1 >> > _______________________________________________ > ffmpeg-devel mailing list > ffmpeg-devel@ffmpeg.org > https://ffmpeg.org/mailman/listinfo/ffmpeg-devel > > To unsubscribe, visit link above, or email > ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
diff --git a/libavfilter/aarch64/vf_nlmeans_init.c b/libavfilter/aarch64/vf_nlmeans_init.c index a1edefb144..ee87c88dd6 100644 --- a/libavfilter/aarch64/vf_nlmeans_init.c +++ b/libavfilter/aarch64/vf_nlmeans_init.c @@ -24,10 +24,10 @@ void ff_compute_safe_ssd_integral_image_neon(uint32_t *dst, ptrdiff_t dst_linesi const uint8_t *s2, ptrdiff_t linesize2, int w, int h); -av_cold void ff_nlmeans_init_aarch64(NLMeansDSPContext *dsp) +av_cold void ff_nlmeans_init_aarch64(NLMeansDSPContext *dsp, int depth) { int cpu_flags = av_get_cpu_flags(); - if (have_neon(cpu_flags)) + if (have_neon(cpu_flags) && depth == 8) dsp->compute_safe_ssd_integral_image = ff_compute_safe_ssd_integral_image_neon; } diff --git a/libavfilter/nlmeans_template.c b/libavfilter/nlmeans_template.c new file mode 100644 index 0000000000..64ac2e9e9b --- /dev/null +++ b/libavfilter/nlmeans_template.c @@ -0,0 +1,366 @@ +/* + * Copyright (c) 2016 Clément Bœsch <u pkh me> + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "libavutil/avassert.h" +#include "avfilter.h" +#include "formats.h" +#include "internal.h" +#include "vf_nlmeans.h" +#include "video.h" + +#undef pixel +#undef integral +#if DEPTH <= 8 +#define pixel uint8_t +#define integral uint32_t +#else +#define pixel uint16_t +#define integral uint64_t +#endif + +#define fn3(a,b) a##b +#define fn2(a,b) fn3(a,b) +#define fn(a) fn2(a, DEPTH) + +/** + * Compute squared difference of the safe area (the zone where s1 and s2 + * overlap). It is likely the largest integral zone, so it is interesting to do + * as little checks as possible; contrary to the unsafe version of this + * function, we do not need any clipping here. + * + * The line above dst and the column to its left are always readable. + */ +static void fn(compute_safe_ssd_integral_image_c)(uint32_t *ddst, ptrdiff_t dst_linesize_32, + const uint8_t *ss1, ptrdiff_t linesize1, + const uint8_t *ss2, ptrdiff_t linesize2, + int w, int h) +{ + const pixel *s1 = (const pixel *)ss1; + const pixel *s2 = (const pixel *)ss2; + int x, y; + integral *dst = (integral *)ddst; + const integral *dst_top = dst - dst_linesize_32; + + linesize1 /= sizeof(pixel); + linesize2 /= sizeof(pixel); + + /* SIMD-friendly assumptions allowed here */ + av_assert2(!(w & 0xf) && w >= 16 && h >= 1); + + for (y = 0; y < h; y++) { + for (x = 0; x < w; x += 4) { + const int d0 = s1[x ] - s2[x ]; + const int d1 = s1[x + 1] - s2[x + 1]; + const int d2 = s1[x + 2] - s2[x + 2]; + const int d3 = s1[x + 3] - s2[x + 3]; + + dst[x ] = dst_top[x ] - dst_top[x - 1] + d0*d0; + dst[x + 1] = dst_top[x + 1] - dst_top[x ] + d1*d1; + dst[x + 2] = dst_top[x + 2] - dst_top[x + 1] + d2*d2; + dst[x + 3] = dst_top[x + 3] - dst_top[x + 2] + d3*d3; + + dst[x ] += dst[x - 1]; + dst[x + 1] += dst[x ]; + dst[x + 2] += dst[x + 1]; + dst[x + 3] += dst[x + 2]; + } + s1 += linesize1; + s2 += linesize2; + dst += dst_linesize_32; + dst_top += dst_linesize_32; + } +} + +/** + * Compute squared difference of an unsafe area (the zone nor s1 nor s2 could + * be readable). + * + * On the other hand, the line above dst and the column to its left are always + * readable. + * + * There is little point in having this function SIMDified as it is likely too + * complex and only handle small portions of the image. + * + * @param dst integral image + * @param dst_linesize_32 integral image linesize (in 32-bit integers unit) + * @param startx integral starting x position + * @param starty integral starting y position + * @param ssrc source plane buffer + * @param linesize source plane linesize + * @param offx source offsetting in x + * @param offy source offsetting in y + * @paran r absolute maximum source offsetting + * @param sw source width + * @param sh source height + * @param w width to compute + * @param h height to compute + */ +static inline void fn(compute_unsafe_ssd_integral_image)(uint32_t *ddst, ptrdiff_t dst_linesize_32, + int startx, int starty, + const uint8_t *ssrc, ptrdiff_t linesize, + int offx, int offy, int r, int sw, int sh, + int w, int h) +{ + integral *dst = (integral *)ddst; + const pixel *src = (const pixel *)ssrc; + int x, y; + + linesize /= sizeof(pixel); + + for (y = starty; y < starty + h; y++) { + integral acc = dst[y*dst_linesize_32 + startx - 1] - dst[(y-1)*dst_linesize_32 + startx - 1]; + const int s1y = av_clip(y - r, 0, sh - 1); + const int s2y = av_clip(y - (r + offy), 0, sh - 1); + + for (x = startx; x < startx + w; x++) { + const int s1x = av_clip(x - r, 0, sw - 1); + const int s2x = av_clip(x - (r + offx), 0, sw - 1); + const pixel v1 = src[s1y*linesize + s1x]; + const pixel v2 = src[s2y*linesize + s2x]; + const int d = v1 - v2; + acc += d * d; + dst[y*dst_linesize_32 + x] = dst[(y-1)*dst_linesize_32 + x] + acc; + } + } +} + +/* + * Compute the sum of squared difference integral image + * http://www.ipol.im/pub/art/2014/57/ + * Integral Images for Block Matching - Gabriele Facciolo, Nicolas Limare, Enric Meinhardt-Llopis + * + * @param ii integral image of dimension (w+e*2) x (h+e*2) with + * an additional zeroed top line and column already + * "applied" to the pointer value + * @param ii_linesize_32 integral image linesize (in 32-bit integers unit) + * @param src source plane buffer + * @param linesize source plane linesize + * @param offx x-offsetting ranging in [-e;e] + * @param offy y-offsetting ranging in [-e;e] + * @param w source width + * @param h source height + * @param e research padding edge + */ +static void fn(compute_ssd_integral_image)(const NLMeansDSPContext *dsp, + integral *ii, ptrdiff_t ii_linesize_32, + const uint8_t *src, ptrdiff_t linesize, int offx, int offy, + int e, int w, int h) +{ + // ii has a surrounding padding of thickness "e" + const int ii_w = w + e*2; + const int ii_h = h + e*2; + + // we center the first source + const int s1x = e; + const int s1y = e; + + // 2nd source is the frame with offsetting + const int s2x = e + offx; + const int s2y = e + offy; + + // get the dimension of the overlapping rectangle where it is always safe + // to compare the 2 sources pixels + const int startx_safe = FFMAX(s1x, s2x); + const int starty_safe = FFMAX(s1y, s2y); + const int u_endx_safe = FFMIN(s1x + w, s2x + w); // unaligned + const int endy_safe = FFMIN(s1y + h, s2y + h); + + // deduce the safe area width and height + const int safe_pw = (u_endx_safe - startx_safe) & ~0xf; + const int safe_ph = endy_safe - starty_safe; + + // adjusted end x position of the safe area after width of the safe area gets aligned + const int endx_safe = startx_safe + safe_pw; + + // top part where only one of s1 and s2 is still readable, or none at all + fn(compute_unsafe_ssd_integral_image)((uint32_t *)(ii), ii_linesize_32, + 0, 0, + src, linesize, + offx, offy, e, w, h, + ii_w, starty_safe); + + // fill the left column integral required to compute the central + // overlapping one + fn(compute_unsafe_ssd_integral_image)((uint32_t *)(ii), ii_linesize_32, + 0, starty_safe, + src, linesize, + offx, offy, e, w, h, + startx_safe, safe_ph); + + // main and safe part of the integral + av_assert1(startx_safe - s1x >= 0); av_assert1(startx_safe - s1x < w); + av_assert1(starty_safe - s1y >= 0); av_assert1(starty_safe - s1y < h); + av_assert1(startx_safe - s2x >= 0); av_assert1(startx_safe - s2x < w); + av_assert1(starty_safe - s2y >= 0); av_assert1(starty_safe - s2y < h); + if (safe_pw && safe_ph) + dsp->compute_safe_ssd_integral_image((uint32_t *)(ii + starty_safe*ii_linesize_32 + startx_safe), ii_linesize_32, + src + (starty_safe - s1y) * linesize + (startx_safe - s1x) * sizeof(pixel), linesize, + src + (starty_safe - s2y) * linesize + (startx_safe - s2x) * sizeof(pixel), linesize, + safe_pw, safe_ph); + + // right part of the integral + fn(compute_unsafe_ssd_integral_image)((uint32_t *)(ii), ii_linesize_32, + endx_safe, starty_safe, + src, linesize, + offx, offy, e, w, h, + ii_w - endx_safe, safe_ph); + + // bottom part where only one of s1 and s2 is still readable, or none at all + fn(compute_unsafe_ssd_integral_image)((uint32_t *)(ii), ii_linesize_32, + 0, endy_safe, + src, linesize, + offx, offy, e, w, h, + ii_w, ii_h - endy_safe); +} + +static int fn(nlmeans_slice)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) +{ + int x, y; + NLMeansContext *s = ctx->priv; + const struct thread_data *td = arg; + const ptrdiff_t src_linesize = td->src_linesize; + const int process_h = td->endy - td->starty; + const int slice_start = (process_h * jobnr ) / nb_jobs; + const int slice_end = (process_h * (jobnr+1)) / nb_jobs; + const int starty = td->starty + slice_start; + const int endy = td->starty + slice_end; + const int p = td->p; + const integral *ii = (const integral *)td->ii_start + (starty - p - 1) * s->ii_lz_32 - p - 1; + const int dist_b = 2*p + 1; + const int dist_d = dist_b * s->ii_lz_32; + const int dist_e = dist_d + dist_b; + + for (y = starty; y < endy; y++) { + const pixel *src = (const pixel *)(td->src + y*src_linesize); + struct weighted_avg *wa = s->wa + y*s->wa_linesize; + for (x = td->startx; x < td->endx; x++) { + /* + * M is a discrete map where every entry contains the sum of all the entries + * in the rectangle from the top-left origin of M to its coordinate. In the + * following schema, "i" contains the sum of the whole map: + * + * M = +----------+-----------------+----+ + * | | | | + * | | | | + * | a| b| c| + * +----------+-----------------+----+ + * | | | | + * | | | | + * | | X | | + * | | | | + * | d| e| f| + * +----------+-----------------+----+ + * | | | | + * | g| h| i| + * +----------+-----------------+----+ + * + * The sum of the X box can be calculated with: + * X = e-d-b+a + * + * See https://en.wikipedia.org/wiki/Summed_area_table + * + * The compute*_ssd functions compute the integral image M where every entry + * contains the sum of the squared difference of every corresponding pixels of + * two input planes of the same size as M. + */ + const integral a = ii[x]; + const integral b = ii[x + dist_b]; + const integral d = ii[x + dist_d]; + const integral e = ii[x + dist_e]; + const integral patch_diff_sq = e + a - d - b; + + if (patch_diff_sq < s->max_meaningful_diff) { + const float weight = s->weight_lut[patch_diff_sq]; // exp(-patch_diff_sq * s->pdiff_scale) + wa[x].total_weight += weight; + wa[x].sum += weight * src[x]; + } + } + ii += s->ii_lz_32; + } + return 0; +} + +static void fn(weight_averages)(uint8_t *ddst, ptrdiff_t dst_linesize, + const uint8_t *ssrc, ptrdiff_t src_linesize, + struct weighted_avg *wa, ptrdiff_t wa_linesize, + int w, int h) +{ + const pixel *src = (const pixel *)ssrc; + pixel *dst = (pixel *)ddst; + int x, y; + + src_linesize /= sizeof(pixel); + dst_linesize /= sizeof(pixel); + + for (y = 0; y < h; y++) { + for (x = 0; x < w; x++) { + // Also weight the centered pixel + wa[x].total_weight += 1.f; + wa[x].sum += 1.f * src[x]; + dst[x] = av_clip_uintp2(wa[x].sum / wa[x].total_weight + 0.5f, DEPTH); + } + dst += dst_linesize; + src += src_linesize; + wa += wa_linesize; + } +} + +static int fn(nlmeans_plane)(AVFilterContext *ctx, int w, int h, int p, int r, + uint8_t *dst, ptrdiff_t dst_linesize, + const uint8_t *src, ptrdiff_t src_linesize) +{ + int offx, offy; + NLMeansContext *s = ctx->priv; + /* patches center points cover the whole research window so the patches + * themselves overflow the research window */ + const int e = r + p; + /* focus an integral pointer on the centered image (s1) */ + const integral *centered_ii = (const integral *)s->ii + e*s->ii_lz_32 + e; + + memset(s->wa, 0, s->wa_linesize * h * sizeof(*s->wa)); + + for (offy = -r; offy <= r; offy++) { + for (offx = -r; offx <= r; offx++) { + if (offx || offy) { + struct thread_data td = { + .src = src + offy*src_linesize + offx * (int)(sizeof(pixel)), + .src_linesize = src_linesize, + .startx = FFMAX(0, -offx), + .starty = FFMAX(0, -offy), + .endx = FFMIN(w, w - offx), + .endy = FFMIN(h, h - offy), + .ii_start = (uint32_t *)(centered_ii + offy*s->ii_lz_32 + offx), + .p = p, + }; + + fn(compute_ssd_integral_image)(&s->dsp, (integral *)s->ii, s->ii_lz_32, + src, src_linesize, + offx, offy, e, w, h); + ctx->internal->execute(ctx, fn(nlmeans_slice), &td, NULL, + FFMIN(td.endy - td.starty, ff_filter_get_nb_threads(ctx))); + } + } + } + + fn(weight_averages)(dst, dst_linesize, src, src_linesize, + s->wa, s->wa_linesize, w, h); + + return 0; +} diff --git a/libavfilter/vf_nlmeans.c b/libavfilter/vf_nlmeans.c index 06233b0dd4..49edc1f8c7 100644 --- a/libavfilter/vf_nlmeans.c +++ b/libavfilter/vf_nlmeans.c @@ -38,31 +38,28 @@ #include "vf_nlmeans.h" #include "video.h" -struct weighted_avg { - float total_weight; - float sum; -}; +#define DEPTH 8 +#include "nlmeans_template.c" + +#undef DEPTH +#define DEPTH 9 +#include "nlmeans_template.c" + +#undef DEPTH +#define DEPTH 10 +#include "nlmeans_template.c" + +#undef DEPTH +#define DEPTH 12 +#include "nlmeans_template.c" + +#undef DEPTH +#define DEPTH 14 +#include "nlmeans_template.c" -typedef struct NLMeansContext { - const AVClass *class; - int nb_planes; - int chroma_w, chroma_h; - double pdiff_scale; // invert of the filtering parameter (sigma*10) squared - double sigma; // denoising strength - int patch_size, patch_hsize; // patch size and half size - int patch_size_uv, patch_hsize_uv; // patch size and half size for chroma planes - int research_size, research_hsize; // research size and half size - int research_size_uv, research_hsize_uv; // research size and half size for chroma planes - uint32_t *ii_orig; // integral image - uint32_t *ii; // integral image starting after the 0-line and 0-column - int ii_w, ii_h; // width and height of the integral image - ptrdiff_t ii_lz_32; // linesize in 32-bit units of the integral image - struct weighted_avg *wa; // weighted average of every pixel - ptrdiff_t wa_linesize; // linesize for wa in struct size unit - float *weight_lut; // lookup table mapping (scaled) patch differences to their associated weights - uint32_t max_meaningful_diff; // maximum difference considered (if the patch difference is too high we ignore the pixel) - NLMeansDSPContext dsp; -} NLMeansContext; +#undef DEPTH +#define DEPTH 16 +#include "nlmeans_template.c" #define OFFSET(x) offsetof(NLMeansContext, x) #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM @@ -87,6 +84,13 @@ static int query_formats(AVFilterContext *ctx) AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_GRAY8, AV_PIX_FMT_GBRP, + AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9, + AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10, + AV_PIX_FMT_YUV440P10, + AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12, + AV_PIX_FMT_YUV440P12, + AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV420P14, + AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16, AV_PIX_FMT_NONE }; @@ -96,380 +100,6 @@ static int query_formats(AVFilterContext *ctx) return ff_set_common_formats(ctx, fmts_list); } -/** - * Compute squared difference of the safe area (the zone where s1 and s2 - * overlap). It is likely the largest integral zone, so it is interesting to do - * as little checks as possible; contrary to the unsafe version of this - * function, we do not need any clipping here. - * - * The line above dst and the column to its left are always readable. - */ -static void compute_safe_ssd_integral_image_c(uint32_t *dst, ptrdiff_t dst_linesize_32, - const uint8_t *s1, ptrdiff_t linesize1, - const uint8_t *s2, ptrdiff_t linesize2, - int w, int h) -{ - int x, y; - const uint32_t *dst_top = dst - dst_linesize_32; - - /* SIMD-friendly assumptions allowed here */ - av_assert2(!(w & 0xf) && w >= 16 && h >= 1); - - for (y = 0; y < h; y++) { - for (x = 0; x < w; x += 4) { - const int d0 = s1[x ] - s2[x ]; - const int d1 = s1[x + 1] - s2[x + 1]; - const int d2 = s1[x + 2] - s2[x + 2]; - const int d3 = s1[x + 3] - s2[x + 3]; - - dst[x ] = dst_top[x ] - dst_top[x - 1] + d0*d0; - dst[x + 1] = dst_top[x + 1] - dst_top[x ] + d1*d1; - dst[x + 2] = dst_top[x + 2] - dst_top[x + 1] + d2*d2; - dst[x + 3] = dst_top[x + 3] - dst_top[x + 2] + d3*d3; - - dst[x ] += dst[x - 1]; - dst[x + 1] += dst[x ]; - dst[x + 2] += dst[x + 1]; - dst[x + 3] += dst[x + 2]; - } - s1 += linesize1; - s2 += linesize2; - dst += dst_linesize_32; - dst_top += dst_linesize_32; - } -} - -/** - * Compute squared difference of an unsafe area (the zone nor s1 nor s2 could - * be readable). - * - * On the other hand, the line above dst and the column to its left are always - * readable. - * - * There is little point in having this function SIMDified as it is likely too - * complex and only handle small portions of the image. - * - * @param dst integral image - * @param dst_linesize_32 integral image linesize (in 32-bit integers unit) - * @param startx integral starting x position - * @param starty integral starting y position - * @param src source plane buffer - * @param linesize source plane linesize - * @param offx source offsetting in x - * @param offy source offsetting in y - * @paran r absolute maximum source offsetting - * @param sw source width - * @param sh source height - * @param w width to compute - * @param h height to compute - */ -static inline void compute_unsafe_ssd_integral_image(uint32_t *dst, ptrdiff_t dst_linesize_32, - int startx, int starty, - const uint8_t *src, ptrdiff_t linesize, - int offx, int offy, int r, int sw, int sh, - int w, int h) -{ - int x, y; - - for (y = starty; y < starty + h; y++) { - uint32_t acc = dst[y*dst_linesize_32 + startx - 1] - dst[(y-1)*dst_linesize_32 + startx - 1]; - const int s1y = av_clip(y - r, 0, sh - 1); - const int s2y = av_clip(y - (r + offy), 0, sh - 1); - - for (x = startx; x < startx + w; x++) { - const int s1x = av_clip(x - r, 0, sw - 1); - const int s2x = av_clip(x - (r + offx), 0, sw - 1); - const uint8_t v1 = src[s1y*linesize + s1x]; - const uint8_t v2 = src[s2y*linesize + s2x]; - const int d = v1 - v2; - acc += d * d; - dst[y*dst_linesize_32 + x] = dst[(y-1)*dst_linesize_32 + x] + acc; - } - } -} - -/* - * Compute the sum of squared difference integral image - * http://www.ipol.im/pub/art/2014/57/ - * Integral Images for Block Matching - Gabriele Facciolo, Nicolas Limare, Enric Meinhardt-Llopis - * - * @param ii integral image of dimension (w+e*2) x (h+e*2) with - * an additional zeroed top line and column already - * "applied" to the pointer value - * @param ii_linesize_32 integral image linesize (in 32-bit integers unit) - * @param src source plane buffer - * @param linesize source plane linesize - * @param offx x-offsetting ranging in [-e;e] - * @param offy y-offsetting ranging in [-e;e] - * @param w source width - * @param h source height - * @param e research padding edge - */ -static void compute_ssd_integral_image(const NLMeansDSPContext *dsp, - uint32_t *ii, ptrdiff_t ii_linesize_32, - const uint8_t *src, ptrdiff_t linesize, int offx, int offy, - int e, int w, int h) -{ - // ii has a surrounding padding of thickness "e" - const int ii_w = w + e*2; - const int ii_h = h + e*2; - - // we center the first source - const int s1x = e; - const int s1y = e; - - // 2nd source is the frame with offsetting - const int s2x = e + offx; - const int s2y = e + offy; - - // get the dimension of the overlapping rectangle where it is always safe - // to compare the 2 sources pixels - const int startx_safe = FFMAX(s1x, s2x); - const int starty_safe = FFMAX(s1y, s2y); - const int u_endx_safe = FFMIN(s1x + w, s2x + w); // unaligned - const int endy_safe = FFMIN(s1y + h, s2y + h); - - // deduce the safe area width and height - const int safe_pw = (u_endx_safe - startx_safe) & ~0xf; - const int safe_ph = endy_safe - starty_safe; - - // adjusted end x position of the safe area after width of the safe area gets aligned - const int endx_safe = startx_safe + safe_pw; - - // top part where only one of s1 and s2 is still readable, or none at all - compute_unsafe_ssd_integral_image(ii, ii_linesize_32, - 0, 0, - src, linesize, - offx, offy, e, w, h, - ii_w, starty_safe); - - // fill the left column integral required to compute the central - // overlapping one - compute_unsafe_ssd_integral_image(ii, ii_linesize_32, - 0, starty_safe, - src, linesize, - offx, offy, e, w, h, - startx_safe, safe_ph); - - // main and safe part of the integral - av_assert1(startx_safe - s1x >= 0); av_assert1(startx_safe - s1x < w); - av_assert1(starty_safe - s1y >= 0); av_assert1(starty_safe - s1y < h); - av_assert1(startx_safe - s2x >= 0); av_assert1(startx_safe - s2x < w); - av_assert1(starty_safe - s2y >= 0); av_assert1(starty_safe - s2y < h); - if (safe_pw && safe_ph) - dsp->compute_safe_ssd_integral_image(ii + starty_safe*ii_linesize_32 + startx_safe, ii_linesize_32, - src + (starty_safe - s1y) * linesize + (startx_safe - s1x), linesize, - src + (starty_safe - s2y) * linesize + (startx_safe - s2x), linesize, - safe_pw, safe_ph); - - // right part of the integral - compute_unsafe_ssd_integral_image(ii, ii_linesize_32, - endx_safe, starty_safe, - src, linesize, - offx, offy, e, w, h, - ii_w - endx_safe, safe_ph); - - // bottom part where only one of s1 and s2 is still readable, or none at all - compute_unsafe_ssd_integral_image(ii, ii_linesize_32, - 0, endy_safe, - src, linesize, - offx, offy, e, w, h, - ii_w, ii_h - endy_safe); -} - -static int config_input(AVFilterLink *inlink) -{ - AVFilterContext *ctx = inlink->dst; - NLMeansContext *s = ctx->priv; - const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format); - const int e = FFMAX(s->research_hsize, s->research_hsize_uv) - + FFMAX(s->patch_hsize, s->patch_hsize_uv); - - s->chroma_w = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w); - s->chroma_h = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h); - s->nb_planes = av_pix_fmt_count_planes(inlink->format); - - /* Allocate the integral image with extra edges of thickness "e" - * - * +_+-------------------------------+ - * |0|0000000000000000000000000000000| - * +-x-------------------------------+ - * |0|\ ^ | - * |0| ii | e | - * |0| v | - * |0| +-----------------------+ | - * |0| | | | - * |0|<->| | | - * |0| e | | | - * |0| | | | - * |0| +-----------------------+ | - * |0| | - * |0| | - * |0| | - * +-+-------------------------------+ - */ - s->ii_w = inlink->w + e*2; - s->ii_h = inlink->h + e*2; - - // align to 4 the linesize, "+1" is for the space of the left 0-column - s->ii_lz_32 = FFALIGN(s->ii_w + 1, 4); - - // "+1" is for the space of the top 0-line - s->ii_orig = av_mallocz_array(s->ii_h + 1, s->ii_lz_32 * sizeof(*s->ii_orig)); - if (!s->ii_orig) - return AVERROR(ENOMEM); - - // skip top 0-line and left 0-column - s->ii = s->ii_orig + s->ii_lz_32 + 1; - - // allocate weighted average for every pixel - s->wa_linesize = inlink->w; - s->wa = av_malloc_array(s->wa_linesize, inlink->h * sizeof(*s->wa)); - if (!s->wa) - return AVERROR(ENOMEM); - - return 0; -} - -struct thread_data { - const uint8_t *src; - ptrdiff_t src_linesize; - int startx, starty; - int endx, endy; - const uint32_t *ii_start; - int p; -}; - -static int nlmeans_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) -{ - int x, y; - NLMeansContext *s = ctx->priv; - const struct thread_data *td = arg; - const ptrdiff_t src_linesize = td->src_linesize; - const int process_h = td->endy - td->starty; - const int slice_start = (process_h * jobnr ) / nb_jobs; - const int slice_end = (process_h * (jobnr+1)) / nb_jobs; - const int starty = td->starty + slice_start; - const int endy = td->starty + slice_end; - const int p = td->p; - const uint32_t *ii = td->ii_start + (starty - p - 1) * s->ii_lz_32 - p - 1; - const int dist_b = 2*p + 1; - const int dist_d = dist_b * s->ii_lz_32; - const int dist_e = dist_d + dist_b; - - for (y = starty; y < endy; y++) { - const uint8_t *src = td->src + y*src_linesize; - struct weighted_avg *wa = s->wa + y*s->wa_linesize; - for (x = td->startx; x < td->endx; x++) { - /* - * M is a discrete map where every entry contains the sum of all the entries - * in the rectangle from the top-left origin of M to its coordinate. In the - * following schema, "i" contains the sum of the whole map: - * - * M = +----------+-----------------+----+ - * | | | | - * | | | | - * | a| b| c| - * +----------+-----------------+----+ - * | | | | - * | | | | - * | | X | | - * | | | | - * | d| e| f| - * +----------+-----------------+----+ - * | | | | - * | g| h| i| - * +----------+-----------------+----+ - * - * The sum of the X box can be calculated with: - * X = e-d-b+a - * - * See https://en.wikipedia.org/wiki/Summed_area_table - * - * The compute*_ssd functions compute the integral image M where every entry - * contains the sum of the squared difference of every corresponding pixels of - * two input planes of the same size as M. - */ - const uint32_t a = ii[x]; - const uint32_t b = ii[x + dist_b]; - const uint32_t d = ii[x + dist_d]; - const uint32_t e = ii[x + dist_e]; - const uint32_t patch_diff_sq = e - d - b + a; - - if (patch_diff_sq < s->max_meaningful_diff) { - const float weight = s->weight_lut[patch_diff_sq]; // exp(-patch_diff_sq * s->pdiff_scale) - wa[x].total_weight += weight; - wa[x].sum += weight * src[x]; - } - } - ii += s->ii_lz_32; - } - return 0; -} - -static void weight_averages(uint8_t *dst, ptrdiff_t dst_linesize, - const uint8_t *src, ptrdiff_t src_linesize, - struct weighted_avg *wa, ptrdiff_t wa_linesize, - int w, int h) -{ - int x, y; - - for (y = 0; y < h; y++) { - for (x = 0; x < w; x++) { - // Also weight the centered pixel - wa[x].total_weight += 1.f; - wa[x].sum += 1.f * src[x]; - dst[x] = av_clip_uint8(wa[x].sum / wa[x].total_weight + 0.5f); - } - dst += dst_linesize; - src += src_linesize; - wa += wa_linesize; - } -} - -static int nlmeans_plane(AVFilterContext *ctx, int w, int h, int p, int r, - uint8_t *dst, ptrdiff_t dst_linesize, - const uint8_t *src, ptrdiff_t src_linesize) -{ - int offx, offy; - NLMeansContext *s = ctx->priv; - /* patches center points cover the whole research window so the patches - * themselves overflow the research window */ - const int e = r + p; - /* focus an integral pointer on the centered image (s1) */ - const uint32_t *centered_ii = s->ii + e*s->ii_lz_32 + e; - - memset(s->wa, 0, s->wa_linesize * h * sizeof(*s->wa)); - - for (offy = -r; offy <= r; offy++) { - for (offx = -r; offx <= r; offx++) { - if (offx || offy) { - struct thread_data td = { - .src = src + offy*src_linesize + offx, - .src_linesize = src_linesize, - .startx = FFMAX(0, -offx), - .starty = FFMAX(0, -offy), - .endx = FFMIN(w, w - offx), - .endy = FFMIN(h, h - offy), - .ii_start = centered_ii + offy*s->ii_lz_32 + offx, - .p = p, - }; - - compute_ssd_integral_image(&s->dsp, s->ii, s->ii_lz_32, - src, src_linesize, - offx, offy, e, w, h); - ctx->internal->execute(ctx, nlmeans_slice, &td, NULL, - FFMIN(td.endy - td.starty, ff_filter_get_nb_threads(ctx))); - } - } - } - - weight_averages(dst, dst_linesize, src, src_linesize, - s->wa, s->wa_linesize, w, h); - - return 0; -} - static int filter_frame(AVFilterLink *inlink, AVFrame *in) { int i; @@ -489,15 +119,30 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) const int h = i ? s->chroma_h : inlink->h; const int p = i ? s->patch_hsize_uv : s->patch_hsize; const int r = i ? s->research_hsize_uv : s->research_hsize; - nlmeans_plane(ctx, w, h, p, r, - out->data[i], out->linesize[i], - in->data[i], in->linesize[i]); + s->nlmeans_plane(ctx, w, h, p, r, + out->data[i], out->linesize[i], + in->data[i], in->linesize[i]); } av_frame_free(&in); return ff_filter_frame(outlink, out); } +void ff_nlmeans_init(NLMeansDSPContext *dsp, int depth) +{ + switch (depth) { + case 8: dsp->compute_safe_ssd_integral_image = compute_safe_ssd_integral_image_c8; break; + case 9: dsp->compute_safe_ssd_integral_image = compute_safe_ssd_integral_image_c9; break; + case 10: dsp->compute_safe_ssd_integral_image = compute_safe_ssd_integral_image_c10; break; + case 12: dsp->compute_safe_ssd_integral_image = compute_safe_ssd_integral_image_c12; break; + case 14: dsp->compute_safe_ssd_integral_image = compute_safe_ssd_integral_image_c14; break; + case 16: dsp->compute_safe_ssd_integral_image = compute_safe_ssd_integral_image_c16; break; + } + + if (ARCH_AARCH64) + ff_nlmeans_init_aarch64(dsp, depth); +} + #define CHECK_ODD_FIELD(field, name) do { \ if (!(s->field & 1)) { \ s->field |= 1; \ @@ -506,26 +151,22 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) } \ } while (0) -void ff_nlmeans_init(NLMeansDSPContext *dsp) -{ - dsp->compute_safe_ssd_integral_image = compute_safe_ssd_integral_image_c; - - if (ARCH_AARCH64) - ff_nlmeans_init_aarch64(dsp); -} - -static av_cold int init(AVFilterContext *ctx) +static int config_input(AVFilterLink *inlink) { - int i; + AVFilterContext *ctx = inlink->dst; NLMeansContext *s = ctx->priv; - const double h = s->sigma * 10.; + const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format); + double h; + int e; + s->depth = desc->comp[0].depth; + h = s->sigma * 10. * (1 << (s->depth - 8)); s->pdiff_scale = 1. / (h * h); - s->max_meaningful_diff = log(255.) / s->pdiff_scale; + s->max_meaningful_diff = FFMIN(log(255.) / s->pdiff_scale, INT32_MAX / 8); s->weight_lut = av_calloc(s->max_meaningful_diff, sizeof(*s->weight_lut)); if (!s->weight_lut) return AVERROR(ENOMEM); - for (i = 0; i < s->max_meaningful_diff; i++) + for (int i = 0; i < s->max_meaningful_diff; i++) s->weight_lut[i] = exp(-i * s->pdiff_scale); CHECK_ODD_FIELD(research_size, "Luma research window"); @@ -542,11 +183,65 @@ static av_cold int init(AVFilterContext *ctx) s->patch_hsize = s->patch_size / 2; s->patch_hsize_uv = s->patch_size_uv / 2; + e = FFMAX(s->research_hsize, s->research_hsize_uv) + + FFMAX(s->patch_hsize, s->patch_hsize_uv); av_log(ctx, AV_LOG_INFO, "Research window: %dx%d / %dx%d, patch size: %dx%d / %dx%d\n", s->research_size, s->research_size, s->research_size_uv, s->research_size_uv, s->patch_size, s->patch_size, s->patch_size_uv, s->patch_size_uv); - ff_nlmeans_init(&s->dsp); + ff_nlmeans_init(&s->dsp, s->depth); + + s->chroma_w = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w); + s->chroma_h = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h); + s->nb_planes = av_pix_fmt_count_planes(inlink->format); + + /* Allocate the integral image with extra edges of thickness "e" + * + * +_+-------------------------------+ + * |0|0000000000000000000000000000000| + * +-x-------------------------------+ + * |0|\ ^ | + * |0| ii | e | + * |0| v | + * |0| +-----------------------+ | + * |0| | | | + * |0|<->| | | + * |0| e | | | + * |0| | | | + * |0| +-----------------------+ | + * |0| | + * |0| | + * |0| | + * +-+-------------------------------+ + */ + s->ii_w = inlink->w + e*2; + s->ii_h = inlink->h + e*2; + + // align to 4 the linesize, "+1" is for the space of the left 0-column + s->ii_lz_32 = FFALIGN(s->ii_w + 1, 4); + + // "+1" is for the space of the top 0-line + s->ii_orig = av_mallocz_array(s->ii_h + 1, (1 + (s->depth > 8)) * s->ii_lz_32 * sizeof(*s->ii_orig)); + if (!s->ii_orig) + return AVERROR(ENOMEM); + + // skip top 0-line and left 0-column + s->ii = s->ii_orig + (s->ii_lz_32 + 1) * (1 + (s->depth > 8)); + + // allocate weighted average for every pixel + s->wa_linesize = inlink->w; + s->wa = av_malloc_array(s->wa_linesize, inlink->h * sizeof(*s->wa)); + if (!s->wa) + return AVERROR(ENOMEM); + + switch (s->depth) { + case 8: s->nlmeans_plane = nlmeans_plane8; break; + case 9: s->nlmeans_plane = nlmeans_plane9; break; + case 10: s->nlmeans_plane = nlmeans_plane10; break; + case 12: s->nlmeans_plane = nlmeans_plane12; break; + case 14: s->nlmeans_plane = nlmeans_plane14; break; + case 16: s->nlmeans_plane = nlmeans_plane16; break; + } return 0; } @@ -581,7 +276,6 @@ AVFilter ff_vf_nlmeans = { .name = "nlmeans", .description = NULL_IF_CONFIG_SMALL("Non-local means denoiser."), .priv_size = sizeof(NLMeansContext), - .init = init, .uninit = uninit, .query_formats = query_formats, .inputs = nlmeans_inputs, diff --git a/libavfilter/vf_nlmeans.h b/libavfilter/vf_nlmeans.h index 0a9aab2928..d8fb5588c6 100644 --- a/libavfilter/vf_nlmeans.h +++ b/libavfilter/vf_nlmeans.h @@ -29,7 +29,47 @@ typedef struct NLMeansDSPContext { int w, int h); } NLMeansDSPContext; -void ff_nlmeans_init(NLMeansDSPContext *dsp); -void ff_nlmeans_init_aarch64(NLMeansDSPContext *dsp); +struct weighted_avg { + float total_weight; + float sum; +}; + +struct thread_data { + const uint8_t *src; + ptrdiff_t src_linesize; + int startx, starty; + int endx, endy; + const uint32_t *ii_start; + int p; +}; + +typedef struct NLMeansContext { + const AVClass *class; + int nb_planes; + int depth; + int chroma_w, chroma_h; + double pdiff_scale; // invert of the filtering parameter (sigma*10) squared + double sigma; // denoising strength + int patch_size, patch_hsize; // patch size and half size + int patch_size_uv, patch_hsize_uv; // patch size and half size for chroma planes + int research_size, research_hsize; // research size and half size + int research_size_uv, research_hsize_uv; // research size and half size for chroma planes + uint32_t *ii_orig; // integral image + uint32_t *ii; // integral image starting after the 0-line and 0-column + int ii_w, ii_h; // width and height of the integral image + ptrdiff_t ii_lz_32; // linesize in 32-bit units of the integral image + struct weighted_avg *wa; // weighted average of every pixel + ptrdiff_t wa_linesize; // linesize for wa in struct size unit + float *weight_lut; // lookup table mapping (scaled) patch differences to their associated weights + uint32_t max_meaningful_diff; // maximum difference considered (if the patch difference is too high we ignore the pixel) + NLMeansDSPContext dsp; + + int (*nlmeans_plane)(AVFilterContext *ctx, int w, int h, int p, int r, + uint8_t *dst, ptrdiff_t dst_linesize, + const uint8_t *src, ptrdiff_t src_linesize); +} NLMeansContext; + +void ff_nlmeans_init(NLMeansDSPContext *dsp, int depth); +void ff_nlmeans_init_aarch64(NLMeansDSPContext *dsp, int depth); #endif /* AVFILTER_NLMEANS_H */
Signed-off-by: Paul B Mahol <onemda@gmail.com> --- libavfilter/aarch64/vf_nlmeans_init.c | 4 +- libavfilter/nlmeans_template.c | 366 ++++++++++++++++++ libavfilter/vf_nlmeans.c | 526 ++++++-------------------- libavfilter/vf_nlmeans.h | 44 ++- 4 files changed, 520 insertions(+), 420 deletions(-) create mode 100644 libavfilter/nlmeans_template.c