From patchwork Sun Oct 24 20:25:01 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul B Mahol X-Patchwork-Id: 31225 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6602:2084:0:0:0:0 with SMTP id a4csp4158712ioa; Sun, 24 Oct 2021 13:26:05 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzbxGVGOrLx7+Q7qZJ8LGD1UX/z2LAglnbhPyO9pmZRShtoU3+rqqc5niF0lBsW37qrKbqQ X-Received: by 2002:a05:6402:350b:: with SMTP id b11mr20345561edd.341.1635107165251; Sun, 24 Oct 2021 13:26:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1635107165; cv=none; d=google.com; s=arc-20160816; b=KYVBQjgANscKUljy02r+KMgOL6olRo2qKnPBzskEhK1K6yLDFKzduDFe/hPMTqVt8q MyD4nas7h6yhyHcgxm5GECfPIVRetLJA25d/d4pchqWEhBsbqIUbxcUbruaVn07PjTwj DzyqVm3Y6cM7OSiaXo+8x31oRt/FwGcdirsHsWsgW3adXA7RBg5ispYj6aO4Nv2v3S1+ h9NJ9HzgLLFEVRfJEVGt2Y+0t9EJbAkFYxQ9LbI73IQKEsCylMZbvzoiefYOW3IQQh8k pqeR72NAyUS1hPrwftJ0WWGAe7AgsC5mFRlpcIMlfvHW+JnAtNVNz94fNEIwR0X1ITjQ 8yUA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:dkim-signature:delivered-to; bh=onNoMF3w33ayqVup7pdMpuT1Gq3EYblyeJnFiEgXdlw=; b=owj0rF3g7aXrqQo4q9gxTL/J0ywCuzhsCtJraZ4tyyR4T0iVMAANXzVdpUzM5NHN9N svojtXPPsdiIBXuzhKhIFOEFemZUZUZmYsdLOx5i7VgUF4klzm7nOWPZTkR3t6M4lMy7 qcxOLbgGO4ONOapIiJv0EkiRMNR7BreJE9or1MKuDIfT8JZ5yrVOIRRbAROFVxLphKkm 8mDhR/4J7WS4OY55x3HKNAe2+IprmMI6pas5UbP8vyjTySskEGB8YmZyLN656FbyFf+h 5ZpHTbM+Lh1kQfCdn5nsc7/eNqrs/s6ZtQlBti4hh9LM/qRm9yU5ZgMRSOSIUhhRyVch NjIA== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=NpemVP02; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id j12si34840084edw.239.2021.10.24.13.26.04; Sun, 24 Oct 2021 13:26:05 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=NpemVP02; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 5F140689F94; Sun, 24 Oct 2021 23:25:12 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ed1-f42.google.com (mail-ed1-f42.google.com [209.85.208.42]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 1C66068A7CB for ; Sun, 24 Oct 2021 23:25:04 +0300 (EEST) Received: by mail-ed1-f42.google.com with SMTP id j10so2695822eds.12 for ; Sun, 24 Oct 2021 13:25:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=2oWxsKAZZGtNg2BX2RBCAuqdAuiJ7qws54vrPe4yKZg=; b=NpemVP02wvyh5ScTDRr9f+c1LIRfe6XqtsDiyQhl48q35a6NvUc2Wx+ts6JdB/qI3O g8K1ZlpEKs3daqS3PGZoMHajyuZAokTfZYFLcv8XtHBRxs7yrv2aiNtEjZdz7deRUTQ6 4hA9fKap0uo8InexFbZZDjv0lywrix/iegiOhS1rZ/Epr8omlTL+QChVr6zmfrFXK235 O3OSBtnX0ac6zhv9EPE+ounjrHq8ZZEnXqPBHWCjArAlQ89HbfpPZY62M8QHxM+msj4U MLdkzMjADYsbF2m57f5pcaqn0wAQm7TjIddvRKxVpdZgYhB6p1T/52GTXN+5YEEzwN1g imiw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=2oWxsKAZZGtNg2BX2RBCAuqdAuiJ7qws54vrPe4yKZg=; b=Hd1rFhA8jRcbrtfVluL7H0tP7Z5tyQjnx6weKbbEUyBQRpVMGWFkuAnE/YrXnW0zYz 620qApehw9BbETUFG8QWQW8o6JDfLUHYzAS8jzJ/Fdmr3n5/vRKKM7K7CT+7GT0ehlIg TNYyoVIzp1ZVyJbVRfMxNQEYdyA16RF/01nW/HwqU/7K9YdP8tKmW0Zc/gI5hOJXvlEz v7WFO26lhSlR0xAGQrl2QRIzLsFaH8g796t1o8hYlXL6Sbe4Q6++6CD5J21iBiZJnVs7 4xShetpfSweySMwcGSDK25lbxIu9YtnBimBHk21b2o/EJXsAXxTkrchSXoyi/SGeMc4v vNgQ== X-Gm-Message-State: AOAM531pYC9XPFdySOg7zDPogXW8fnWNmpYZAvFi9UBCBsEMOKJwsSJP WUT3+UwaCtfrhBxHXrQvWlpuEn7x4PA= X-Received: by 2002:a05:6402:4243:: with SMTP id g3mr20137284edb.381.1635107103642; Sun, 24 Oct 2021 13:25:03 -0700 (PDT) Received: from localhost.localdomain ([95.168.118.28]) by smtp.gmail.com with ESMTPSA id ga42sm6416697ejc.105.2021.10.24.13.25.02 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 24 Oct 2021 13:25:03 -0700 (PDT) From: Paul B Mahol To: ffmpeg-devel@ffmpeg.org Date: Sun, 24 Oct 2021 22:25:01 +0200 Message-Id: <20211024202502.945133-5-onemda@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20211024202502.945133-1-onemda@gmail.com> References: <20211024202502.945133-1-onemda@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 5/6] avfilter/vf_nlmeans: refactor line processing in preparation for x86 SIMD assembly X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: UDLwydx2fEbw Signed-off-by: Paul B Mahol --- libavfilter/vf_nlmeans.c | 109 ++++++++++++++++++++++----------------- libavfilter/vf_nlmeans.h | 14 +++++ 2 files changed, 77 insertions(+), 46 deletions(-) diff --git a/libavfilter/vf_nlmeans.c b/libavfilter/vf_nlmeans.c index af165c861c..93a14bcf19 100644 --- a/libavfilter/vf_nlmeans.c +++ b/libavfilter/vf_nlmeans.c @@ -38,11 +38,6 @@ #include "vf_nlmeans.h" #include "video.h" -struct weighted_avg { - float total_weight; - float sum; -}; - typedef struct NLMeansContext { const AVClass *class; int nb_planes; @@ -329,6 +324,58 @@ struct thread_data { int p; }; +static void compute_weights_line_c(const uint32_t *const iia, + const uint32_t *const iib, + const uint32_t *const iid, + const uint32_t *const iie, + const uint8_t *const src, + struct weighted_avg *wa, + const float *const weight_lut, + int max_meaningful_diff, + int startx, int endx) +{ + for (int x = startx; x < 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 = iia[x]; + const uint32_t b = iib[x]; + const uint32_t d = iid[x]; + const uint32_t e = iie[x]; + const uint32_t patch_diff_sq = FFMIN(e - d - b + a, max_meaningful_diff); + const float weight = weight_lut[patch_diff_sq]; // exp(-patch_diff_sq * s->pdiff_scale) + + wa[x].total_weight += weight; + wa[x].sum += weight * src[x]; + } +} + static int nlmeans_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) { NLMeansContext *s = ctx->priv; @@ -346,50 +393,19 @@ static int nlmeans_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs const int dist_d = dist_b * s->ii_lz_32; const int dist_e = dist_d + dist_b; const float *const weight_lut = s->weight_lut; + NLMeansDSPContext *dsp = &s->dsp; for (int y = starty; y < endy; y++) { - const uint8_t *src = td->src + y*src_linesize; + const uint8_t *const src = td->src + y*src_linesize; struct weighted_avg *wa = s->wa + y*s->wa_linesize; - for (int 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 = FFMIN(e - d - b + a, max_meaningful_diff); - const float weight = weight_lut[patch_diff_sq]; // exp(-patch_diff_sq * s->pdiff_scale) - - wa[x].total_weight += weight; - wa[x].sum += weight * src[x]; - } + const uint32_t *const iia = ii; + const uint32_t *const iib = ii + dist_b; + const uint32_t *const iid = ii + dist_d; + const uint32_t *const iie = ii + dist_e; + + dsp->compute_weights_line(iia, iib, iid, iie, src, wa, + weight_lut, max_meaningful_diff, + td->startx, td->endx); ii += s->ii_lz_32; } return 0; @@ -493,6 +509,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) void ff_nlmeans_init(NLMeansDSPContext *dsp) { dsp->compute_safe_ssd_integral_image = compute_safe_ssd_integral_image_c; + dsp->compute_weights_line = compute_weights_line_c; if (ARCH_AARCH64) ff_nlmeans_init_aarch64(dsp); diff --git a/libavfilter/vf_nlmeans.h b/libavfilter/vf_nlmeans.h index 0a9aab2928..d0d0056163 100644 --- a/libavfilter/vf_nlmeans.h +++ b/libavfilter/vf_nlmeans.h @@ -22,11 +22,25 @@ #include #include +struct weighted_avg { + float total_weight; + float sum; +}; + typedef struct NLMeansDSPContext { void (*compute_safe_ssd_integral_image)(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); + void (*compute_weights_line)(const uint32_t *const iia, + const uint32_t *const iib, + const uint32_t *const iid, + const uint32_t *const iie, + const uint8_t *const src, + struct weighted_avg *wa, + const float *const weight_lut, + int max_meaningful_diff, + int startx, int endx); } NLMeansDSPContext; void ff_nlmeans_init(NLMeansDSPContext *dsp);