From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTP id C21AE446F8 for ; Tue, 25 Oct 2022 07:18:19 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id EDC8068BD1A; Tue, 25 Oct 2022 10:18:15 +0300 (EEST) Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 6FBC268BB98 for ; Tue, 25 Oct 2022 10:18:08 +0300 (EEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1666682293; x=1698218293; h=from:to:subject:date:message-id:references:in-reply-to: content-transfer-encoding:mime-version; bh=v/+1RTt4QxKQnHzWvzkxXg3NalC3R+dovM4YC8g6pac=; b=aovpTFa89JWwH5hJ+3g/81J5do5X0/7fGXrcRJEGu/9+PiNpkFSUkDur brlIATxawK2LFU3INmcobbHaYeorCG8MBjU9l9pgr9KWaaziaXzYSpu1q CcJCVZ6aKBQXtt//je/JzZBzyrddT5pZUQc7S5M56e7Js8up5mU4m6NJR UZ9M0k3FJV83JlLuF189Ci++NohzXIjRDLgbRWPY2zeN9GzVkbnbfMClV SvT8xd69BF3v78ofWIgpqtmCw1EJXagZhAItS4AonsW3N9RvBnFyb4aLK SrHJZVIICyD8IqdNurIozuPNthy+ktNmOVtjs5LaoT19PtQepFYjAc0qx Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10510"; a="393923207" X-IronPort-AV: E=Sophos;i="5.95,211,1661842800"; d="scan'208";a="393923207" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Oct 2022 00:18:03 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10510"; a="774094707" X-IronPort-AV: E=Sophos;i="5.95,211,1661842800"; d="scan'208";a="774094707" Received: from orsmsx602.amr.corp.intel.com ([10.22.229.15]) by fmsmga001.fm.intel.com with ESMTP; 25 Oct 2022 00:17:59 -0700 Received: from orsmsx611.amr.corp.intel.com (10.22.229.24) by ORSMSX602.amr.corp.intel.com (10.22.229.15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Tue, 25 Oct 2022 00:17:59 -0700 Received: from orsmsx610.amr.corp.intel.com (10.22.229.23) by ORSMSX611.amr.corp.intel.com (10.22.229.24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Tue, 25 Oct 2022 00:17:59 -0700 Received: from ORSEDG601.ED.cps.intel.com (10.7.248.6) by orsmsx610.amr.corp.intel.com (10.22.229.23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31 via Frontend Transport; Tue, 25 Oct 2022 00:17:59 -0700 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (104.47.57.177) by edgegateway.intel.com (134.134.137.102) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2375.31; Tue, 25 Oct 2022 00:17:58 -0700 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Ti3TJbQnvhu3DB+R2YCw/OC8NAJl2YvxKYA8HxBVfHh0RTG1ayeIyxYS34eFuvYaNewp8nTorpsdDBRgXArp5J6m3InBnDDjPcqCGRImX6/ibOO/g7+dTPe333pBU4QJwLXxb2l9iR+PwLYig5WN58rCkUOX5/yjYC26hKX9zHqOSuh/bjo7mcyQzC3C3GKHWlmvQNJhmonVSd65jCvoIWqA2WkwzprN41sbzvcGq5BxuOo8s38FkheObdBQuYUWdYihbNBqp+wmDuoQUPWZD9yk/6jjPjX2DUg5IDeySBBjBaM2V9VmDw9d40tStIbiqGNsaqkizY40DlWHen00Sg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=Bts4Uwx31zEIXJx1QNGqAaP5vd3rB00p+/8DUkwU0uE=; b=Ij4VFVVqWED9lGFrfMtsP2K9t4I8I6V7tEMOxbHmbDEQ5bMj39/35M4dkyij5pFpAbrCDVp9NSnvdBJK6pa4SNim0PFl9S8bUkSLtm2lQ0VlWRGezdEZDAC6HGtv3Tyq2kcyK0JD+AwLtEMSgkp4Kc0oK7qMNv6MCr5oKBpp7KRarJBPTdBXWQsmunc/Z3K3yuay4gV1lppItK+3Epzgvh2S+gCstsgN0Ro06Ew5gBx17BW5R78RgbRDirmiG+01J8md7/omwZCdQGHZ6HtfnkwZwF3Z+VTBKzLT3l6iiyJAMYr2wnFaWOAi8ExmHiwx8LAKfU/7H60QNdSSi4Y4nA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=intel.com; dmarc=pass action=none header.from=intel.com; dkim=pass header.d=intel.com; arc=none Received: from BN6PR11MB1746.namprd11.prod.outlook.com (2603:10b6:404:fb::20) by MW4PR11MB5934.namprd11.prod.outlook.com (2603:10b6:303:189::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5723.29; Tue, 25 Oct 2022 07:17:56 +0000 Received: from BN6PR11MB1746.namprd11.prod.outlook.com ([fe80::1565:8f45:a36f:8704]) by BN6PR11MB1746.namprd11.prod.outlook.com ([fe80::1565:8f45:a36f:8704%9]) with mapi id 15.20.5746.028; Tue, 25 Oct 2022 07:17:56 +0000 From: "Wang, Bin" To: FFmpeg development discussions and patches Thread-Topic: [FFmpeg-devel] [PATCH v5] libavfilter/x86/vf_convolution: add sobel filter optimization and unit test with intel AVX512 VNNI Thread-Index: AQHYzN88ne06z6dSPEybI4Sa/c4/S63xcTLAgCD3RqCADIF34A== Date: Tue, 25 Oct 2022 07:17:56 +0000 Message-ID: References: <20220920103305.7902-1-bin.wang@intel.com> In-Reply-To: Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: authentication-results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=intel.com; x-ms-publictraffictype: Email x-ms-traffictypediagnostic: BN6PR11MB1746:EE_|MW4PR11MB5934:EE_ x-ms-office365-filtering-correlation-id: 0a28fda8-47eb-4337-d13a-08dab6590a0b x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: ettQqY6eOUMNKXOBgWSAldpISRY441E0Ze39LRi2iWH9UR302twXedLUNudoGLPAkdxQHLk8ktQeZXWYomn+wA2IwjkDHFhrQPlrefzhvomhrTnf9CASrqObaFXH+bywWjOFaNImIaUxNPMLNgFHphCzK8vq6EhNKULsLuE7JAyNyGgqt5gHte4htXHWbTCHtOU3E1Dxg22ilNi1tTW9dcDlcrXt9fprUKV7zTuClL8bcrLvTg1/9VXt3CrjKJyUGeSp+7iDG515WtToy4Lwby8bLXe+FeZqznpRzKDwOyF6LzsvLOLJOLctqkCeICHgQr9G7WyHYEBA95u24YJY7voNjQ1R+t9R9bnhmjqQoMiDdC40r/IENdQl4tBQS7kR2tB23JiBx/tti8S8EwaNGyK5DhmOlkcBLHYzco4XnNT37UUF8mEdM0m2kY5acS8cwmW/aAcnOpGueblEFq6HUBvU83Xc62JuP3ucmd+QY5tGmCo8n17J9c/3UNqrR1RwZqJtLVz7YZ8wFnXTVWkFDqDkiRwF6jg6AeKC5I4/XaHvN6u+8OLoEUw1sSFPjEkDGr4uNrNPtgM8hyFQXx0PMV7qwLF2u3KoYspsSZYxNyBq2jBCH2uhl10Xzm1OtO1JvhguVUuPX3PePutjhWxNo98WEqdytoBxfaLPI2vwmBFpdK3t9x+oc4ou/Bk/jgp/jbMcWK+mQYiwnRQm4MhglWkcMNH6QRhJbXiETu0vlk6K4rGyN1HNGJbP/78+DDU7Sw1kVeX5KjqDHhSa9gqAM1eLBNWpZ6rIEfUGQBso3Rt0NvRdSzm8ViNI4mieQ/WJ x-forefront-antispam-report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:BN6PR11MB1746.namprd11.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230022)(396003)(346002)(376002)(39860400002)(366004)(136003)(451199015)(122000001)(38070700005)(33656002)(66899015)(86362001)(82960400001)(2906002)(38100700002)(41300700001)(9686003)(7696005)(8936002)(6506007)(66556008)(5660300002)(53546011)(66446008)(55016003)(26005)(478600001)(83380400001)(8676002)(186003)(6916009)(66476007)(71200400001)(30864003)(64756008)(52536014)(76116006)(66946007)(316002)(966005)(2004002)(579004); DIR:OUT; SFP:1102; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?us-ascii?Q?vRHtPJsY2xwZ8mX0Q0fJbN9VoiMKrfQX4/60+HKoUB6QxSPY2K6F/ZyBJFdm?= =?us-ascii?Q?Bb7HGmclC8ypUuAFov0MfUJLTaQM/fTe4XaqapKYCm5Vy2EuVFvF8OHwCE0c?= =?us-ascii?Q?Y6gToOFbMq+4JmBQUNW/YogUF0y+fEoKoLyhxf8Ajiiu/dK/0lG6sUtSs/VJ?= =?us-ascii?Q?m8okESst3uBdlD2tCFrg7xw+++lXwZluqKkZih1JoLr5HudfuRv24nViU1/l?= =?us-ascii?Q?4J43kz/CVEdshPi3zaGcB34hofoc7ftW4KYrQtkwsHCnzhMAVTsr5cupBC69?= =?us-ascii?Q?aNpPoRWe3RyrZoWoi5CZmKRV4JuAuvEZsK/5X5f33p9AxI2WUVU1CGEs5D1G?= =?us-ascii?Q?ceXXsBvgORyEdnA42pvHJfkCLDWwccKKp/aGONiuEQxRJ5qdCh/wXRdclVoF?= =?us-ascii?Q?1TN8gMqJI8YA622/fkTMcwuqAjbfdCUIuSiwEYyNS2hXxZbP2kxM6a/LkfKh?= =?us-ascii?Q?+WFM0vKlctbd5Yxw0O3ug5E1EPCXwtT6jw7VbKHoZbGvhtMlWlcobhr1Bg9b?= =?us-ascii?Q?QMof4naHNLCwBrGBfdrTEkojM9ZjBqIAhoAb4xb06QcoKNI8qwfLOlpDYTO3?= =?us-ascii?Q?pfj7daqvhkUkj4r068qlzyNm9sY/5g2MF8MItikaIjnlTYnXyKKNhE4qN8oE?= =?us-ascii?Q?FFVWD7ji6xuVeW9RhTg16ArM0h3nea7vXHrMc2bfDnclAs2QRvC4sCpIGIln?= =?us-ascii?Q?MkqGwJbXFy6nwuTk7SJaZC2qNCi/n9z92yJfthJ7Tw+ZKqvTs2Duay0kOkJ0?= =?us-ascii?Q?dpCBsT9WfrM9TZ/PlUOuv4ZY5FZmLo3/TljIPd5QiXcBnMWHDHewAy8CMyWK?= =?us-ascii?Q?CNLGEugd8s7ilF4Jo4pY/cG3Z0kXh6Iq+LtoqvqSrdUlOtP3l/SN8z8whzGH?= =?us-ascii?Q?vaUaLZC7l8KMHNgSAP30DPOGi5R30z/kT+6U/YeU7F/V7f4w0fIy1+6QxGs0?= =?us-ascii?Q?F1vwEBfdTz77EJp+V2WNpeNPltCZ7/nitL9ILTIiB/XwHpg8x+lrMKqW/c4D?= =?us-ascii?Q?IumgEutXBlYN8iO+tkvkTOokHYJFhjYqlffMVXay9I9tPZg4PnuEX7g6FYq/?= =?us-ascii?Q?MSJXiTHwm4mkW23RobnvM9k30ND5aJov2h0QfzKpN8awBqQHlySN2ZSJJwRM?= =?us-ascii?Q?zD7NtwgiAltY+4xnF3LNqVaKzShednjgAxUnsbnndnl9q3X3Jfn0nhGRVbsG?= =?us-ascii?Q?dy8TMjdtiVKfEMJ1QRDys00r6+SXE28wmTfdjwRU8RL2B5hTdjwomI/Obh8d?= =?us-ascii?Q?0AOcz1loEPajFB+AgW17QEPv7RP4sBGNlfUlaDXT8vPnYCRFxY/QVt5uVpU4?= =?us-ascii?Q?Rvb3l8mChMShBLUttA2OaIcelDmp3nkd/hKBfZ+qYdFJuQWxnZRt6UCn+IYr?= =?us-ascii?Q?LFkEXab7N0TiwoQ9ucXVIl8Su7gkjAox4IHhHVwkrajfxplg9Iw5oRK6x9fy?= =?us-ascii?Q?STqyGG/JAzBGmLRsx3sSBEBkL7kQDRfoBhvNZqXxIPX8nlya+IpYnR4k3DTr?= =?us-ascii?Q?91UA+PUpWpYoIgsjSn4jUKNt/EA4CPpzMjzJrFTwXVNjLFm5YWLxlgNk3mZM?= =?us-ascii?Q?C9SfKnbxFttviXyP7pfF6C9/K8WHIeYAXieS8KzI?= MIME-Version: 1.0 X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: BN6PR11MB1746.namprd11.prod.outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 0a28fda8-47eb-4337-d13a-08dab6590a0b X-MS-Exchange-CrossTenant-originalarrivaltime: 25 Oct 2022 07:17:56.2768 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 46c98d88-e344-4ed4-8496-4ed7712e255d X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: RhA9b2XEloqGHE8uRMEUn9bAVR96NMKTFJKQe3JfYl4iXJlBCKBS+qSZOgePJ8hdp/HF1+jlkU4JOaWnTpyEeQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW4PR11MB5934 X-OriginatorOrg: intel.com Subject: Re: [FFmpeg-devel] [PATCH v5] libavfilter/x86/vf_convolution: add sobel filter optimization and unit test with intel AVX512 VNNI 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 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: -----Original Message----- From: Wang, Bin Sent: Tuesday, September 20, 2022 6:33 PM To: ffmpeg-devel@ffmpeg.org Cc: Wang, Bin Subject: [FFmpeg-devel] [PATCH v5] libavfilter/x86/vf_convolution: add sobel filter optimization and unit test with intel AVX512 VNNI From: bwang30 This commit enabled assembly code with intel AVX512 VNNI and added unit test for sobel filter sobel_c: 4537 sobel_avx512icl 2136 Signed-off-by: bwang30 --- libavfilter/convolution.h | 74 +++++++++++++ libavfilter/vf_convolution.c | 91 +++------------- libavfilter/x86/vf_convolution.asm | 147 ++++++++++++++++++++++++++ libavfilter/x86/vf_convolution_init.c | 18 ++++ tests/checkasm/Makefile | 1 + tests/checkasm/checkasm.c | 3 + tests/checkasm/checkasm.h | 1 + tests/checkasm/vf_convolution.c | 103 ++++++++++++++++++ 8 files changed, 360 insertions(+), 78 deletions(-) create mode 100644 tests/checkasm/vf_convolution.c diff --git a/libavfilter/convolution.h b/libavfilter/convolution.h index 88aabe9a20..e44bfb5da8 100644 --- a/libavfilter/convolution.h +++ b/libavfilter/convolution.h @@ -21,6 +21,7 @@ #ifndef AVFILTER_CONVOLUTION_H #define AVFILTER_CONVOLUTION_H #include "avfilter.h" +#include "libavutil/intreadwrite.h" enum MatrixMode { MATRIX_SQUARE, @@ -61,4 +62,77 @@ typedef struct ConvolutionContext { } ConvolutionContext; void ff_convolution_init_x86(ConvolutionContext *s); +void ff_sobel_init_x86(ConvolutionContext *s, int depth, int +nb_planes); + +static void setup_3x3(int radius, const uint8_t *c[], const uint8_t *src, int stride, + int x, int w, int y, int h, int bpc) { + int i; + + for (i = 0; i < 9; i++) { + int xoff = FFABS(x + ((i % 3) - 1)); + int yoff = FFABS(y + (i / 3) - 1); + + xoff = xoff >= w ? 2 * w - 1 - xoff : xoff; + yoff = yoff >= h ? 2 * h - 1 - yoff : yoff; + + c[i] = src + xoff * bpc + yoff * stride; + } +} + +static void filter_sobel(uint8_t *dst, int width, + float scale, float delta, const int *const matrix, + const uint8_t *c[], int peak, int radius, + int dstride, int stride, int size) { + const uint8_t *c0 = c[0], *c1 = c[1], *c2 = c[2]; + const uint8_t *c3 = c[3], *c5 = c[5]; + const uint8_t *c6 = c[6], *c7 = c[7], *c8 = c[8]; + int x; + + for (x = 0; x < width; x++) { + float suma = c0[x] * -1 + c1[x] * -2 + c2[x] * -1 + + c6[x] * 1 + c7[x] * 2 + c8[x] * 1; + float sumb = c0[x] * -1 + c2[x] * 1 + c3[x] * -2 + + c5[x] * 2 + c6[x] * -1 + c8[x] * 1; + + dst[x] = av_clip_uint8(sqrtf(suma*suma + sumb*sumb) * scale + delta); + } +} + +static void filter16_sobel(uint8_t *dstp, int width, + float scale, float delta, const int *const matrix, + const uint8_t *c[], int peak, int radius, + int dstride, int stride, int size) { + uint16_t *dst = (uint16_t *)dstp; + int x; + + for (x = 0; x < width; x++) { + float suma = AV_RN16A(&c[0][2 * x]) * -1 + AV_RN16A(&c[1][2 * x]) * -2 + AV_RN16A(&c[2][2 * x]) * -1 + + AV_RN16A(&c[6][2 * x]) * 1 + AV_RN16A(&c[7][2 * x]) * 2 + AV_RN16A(&c[8][2 * x]) * 1; + float sumb = AV_RN16A(&c[0][2 * x]) * -1 + AV_RN16A(&c[2][2 * x]) * 1 + AV_RN16A(&c[3][2 * x]) * -2 + + AV_RN16A(&c[5][2 * x]) * 2 + AV_RN16A(&c[6][2 * + x]) * -1 + AV_RN16A(&c[8][2 * x]) * 1; + + dst[x] = av_clip(sqrtf(suma*suma + sumb*sumb) * scale + delta, 0, peak); + } +} + +static av_unused void ff_sobel_init(ConvolutionContext *s, int depth, +int nb_planes) { + for (int i = 0; i < 4; i++) { + s->filter[i] = filter_sobel; + s->copy[i] = !((1 << i) & s->planes); + s->size[i] = 3; + s->setup[i] = setup_3x3; + s->rdiv[i] = s->scale; + s->bias[i] = s->delta; + } + if (s->depth > 8) + for (int i = 0; i < 4; i++) + s->filter[i] = filter16_sobel; #if ARCH_X86_64 + ff_sobel_init_x86(s, depth, nb_planes); #endif } #endif diff --git a/libavfilter/vf_convolution.c b/libavfilter/vf_convolution.c index 9a9c099e6d..7762fa2a05 100644 --- a/libavfilter/vf_convolution.c +++ b/libavfilter/vf_convolution.c @@ -139,24 +139,6 @@ static void filter16_roberts(uint8_t *dstp, int width, } } -static void filter16_sobel(uint8_t *dstp, int width, - float scale, float delta, const int *const matrix, - const uint8_t *c[], int peak, int radius, - int dstride, int stride, int size) -{ - uint16_t *dst = (uint16_t *)dstp; - int x; - - for (x = 0; x < width; x++) { - float suma = AV_RN16A(&c[0][2 * x]) * -1 + AV_RN16A(&c[1][2 * x]) * -2 + AV_RN16A(&c[2][2 * x]) * -1 + - AV_RN16A(&c[6][2 * x]) * 1 + AV_RN16A(&c[7][2 * x]) * 2 + AV_RN16A(&c[8][2 * x]) * 1; - float sumb = AV_RN16A(&c[0][2 * x]) * -1 + AV_RN16A(&c[2][2 * x]) * 1 + AV_RN16A(&c[3][2 * x]) * -2 + - AV_RN16A(&c[5][2 * x]) * 2 + AV_RN16A(&c[6][2 * x]) * -1 + AV_RN16A(&c[8][2 * x]) * 1; - - dst[x] = av_clip(sqrtf(suma*suma + sumb*sumb) * scale + delta, 0, peak); - } -} - static void filter16_scharr(uint8_t *dstp, int width, float scale, float delta, const int *const matrix, const uint8_t *c[], int peak, int radius, @@ -261,26 +243,6 @@ static void filter_roberts(uint8_t *dst, int width, } } -static void filter_sobel(uint8_t *dst, int width, - float scale, float delta, const int *const matrix, - const uint8_t *c[], int peak, int radius, - int dstride, int stride, int size) -{ - const uint8_t *c0 = c[0], *c1 = c[1], *c2 = c[2]; - const uint8_t *c3 = c[3], *c5 = c[5]; - const uint8_t *c6 = c[6], *c7 = c[7], *c8 = c[8]; - int x; - - for (x = 0; x < width; x++) { - float suma = c0[x] * -1 + c1[x] * -2 + c2[x] * -1 + - c6[x] * 1 + c7[x] * 2 + c8[x] * 1; - float sumb = c0[x] * -1 + c2[x] * 1 + c3[x] * -2 + - c5[x] * 2 + c6[x] * -1 + c8[x] * 1; - - dst[x] = av_clip_uint8(sqrtf(suma*suma + sumb*sumb) * scale + delta); - } -} - static void filter_scharr(uint8_t *dst, int width, float scale, float delta, const int *const matrix, const uint8_t *c[], int peak, int radius, @@ -552,22 +514,6 @@ static void filter_column(uint8_t *dst, int height, } } -static void setup_3x3(int radius, const uint8_t *c[], const uint8_t *src, int stride, - int x, int w, int y, int h, int bpc) -{ - int i; - - for (i = 0; i < 9; i++) { - int xoff = FFABS(x + ((i % 3) - 1)); - int yoff = FFABS(y + (i / 3) - 1); - - xoff = xoff >= w ? 2 * w - 1 - xoff : xoff; - yoff = yoff >= h ? 2 * h - 1 - yoff : yoff; - - c[i] = src + xoff * bpc + yoff * stride; - } -} - static void setup_5x5(int radius, const uint8_t *c[], const uint8_t *src, int stride, int x, int w, int y, int h, int bpc) { @@ -708,6 +654,18 @@ static int param_init(AVFilterContext *ctx) const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format); int p, i; + s->depth = desc->comp[0].depth; + s->max = (1 << s->depth) - 1; + + s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w); + s->planewidth[0] = s->planewidth[3] = inlink->w; + s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h); + s->planeheight[0] = s->planeheight[3] = inlink->h; + + s->nb_planes = av_pix_fmt_count_planes(inlink->format); + s->nb_threads = ff_filter_get_nb_threads(ctx); + s->bpc = (s->depth + 7) / 8; + if (!strcmp(ctx->filter->name, "convolution")) { for (i = 0; i < 4; i++) { int *matrix = (int *)s->matrix[i]; @@ -804,14 +762,7 @@ static int param_init(AVFilterContext *ctx) s->bias[i] = s->delta; } } else if (!strcmp(ctx->filter->name, "sobel")) { - for (i = 0; i < 4; i++) { - s->filter[i] = filter_sobel; - s->copy[i] = !((1 << i) & s->planes); - s->size[i] = 3; - s->setup[i] = setup_3x3; - s->rdiv[i] = s->scale; - s->bias[i] = s->delta; - } + ff_sobel_init(s, s->depth, s->nb_planes); } else if (!strcmp(ctx->filter->name, "kirsch")) { for (i = 0; i < 4; i++) { s->filter[i] = filter_kirsch; @@ -832,18 +783,6 @@ static int param_init(AVFilterContext *ctx) } } - s->depth = desc->comp[0].depth; - s->max = (1 << s->depth) - 1; - - s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w); - s->planewidth[0] = s->planewidth[3] = inlink->w; - s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h); - s->planeheight[0] = s->planeheight[3] = inlink->h; - - s->nb_planes = av_pix_fmt_count_planes(inlink->format); - s->nb_threads = ff_filter_get_nb_threads(ctx); - s->bpc = (s->depth + 7) / 8; - if (!strcmp(ctx->filter->name, "convolution")) { if (s->depth > 8) { for (p = 0; p < s->nb_planes; p++) { @@ -870,10 +809,6 @@ static int param_init(AVFilterContext *ctx) if (s->depth > 8) for (p = 0; p < s->nb_planes; p++) s->filter[p] = filter16_roberts; - } else if (!strcmp(ctx->filter->name, "sobel")) { - if (s->depth > 8) - for (p = 0; p < s->nb_planes; p++) - s->filter[p] = filter16_sobel; } else if (!strcmp(ctx->filter->name, "kirsch")) { if (s->depth > 8) for (p = 0; p < s->nb_planes; p++) diff --git a/libavfilter/x86/vf_convolution.asm b/libavfilter/x86/vf_convolution.asm index 754d4d1064..c912d56752 100644 --- a/libavfilter/x86/vf_convolution.asm +++ b/libavfilter/x86/vf_convolution.asm @@ -22,6 +22,18 @@ SECTION_RODATA half: dd 0.5 +data_p1: dd 1 +data_n1: dd -1 +data_p2: dd 2 +data_n2: dd -2 + +ALIGN 64 +sobel_perm: db 0, 16, 32, 48, 1, 17, 33, 49, 2, 18, 34, 50, 3, 19, 35, 51 + db 4, 20, 36, 52, 5, 21, 37, 53, 6, 22, 38, 54, 7, 23, 39, 55 + db 8, 24, 40, 56, 9, 25, 41, 57, 10, 26, 42, 58, 11, 27, 43, 59 + db 12, 28, 44, 60, 13, 29, 45, 61, 14, 30, 46, 62, 15, 31, +47, 63 +sobel_mulA: db -1, 1, -2, 2 +sobel_mulB: db 1, -1, 2, -2 SECTION .text @@ -154,3 +166,138 @@ cglobal filter_3x3, 4, 15, 7, dst, width, rdiv, bias, matrix, ptr, c0, c1, c2, c INIT_XMM sse4 FILTER_3X3 %endif + +%macro SOBEL_MUL 2 + movzx ptrd, byte [c%1q + xq] + imul ptrd, [%2] + add rd, ptrd +%endmacro + +%macro SOBEL_ADD 1 + movzx ptrd, byte [c%1q + xq] + add rd, ptrd +%endmacro + +; void filter_sobel_avx512(uint8_t *dst, int width, +; float scale, float delta, const int *const matrix, +; const uint8_t *c[], int peak, int radius, +; int dstride, int stride) +%macro FILTER_SOBEL 0 +%if UNIX64 +cglobal filter_sobel, 4, 15, 7, dst, width, matrix, ptr, c0, c1, c2, +c3, c4, c5, c6, c7, c8, r, x %else cglobal filter_sobel, 4, 15, 7, dst, +width, rdiv, bias, matrix, ptr, c0, c1, c2, c3, c4, c5, c6, c7, c8, r, +x %endif %if WIN64 + SWAP xmm0, xmm2 + SWAP xmm1, xmm3 + mov r2q, matrixmp + mov r3q, ptrmp + DEFINE_ARGS dst, width, matrix, ptr, c0, c1, c2, c3, c4, c5, c6, +c7, c8, r, x %endif + movsxdifnidn widthq, widthd + VBROADCASTSS m0, xmm0 + VBROADCASTSS m1, xmm1 + pxor m6, m6 + mov c0q, [ptrq + 0*gprsize] + mov c1q, [ptrq + 1*gprsize] + mov c2q, [ptrq + 2*gprsize] + mov c3q, [ptrq + 3*gprsize] + mov c4q, [ptrq + 4*gprsize] + mov c5q, [ptrq + 5*gprsize] + mov c6q, [ptrq + 6*gprsize] + mov c7q, [ptrq + 7*gprsize] + mov c8q, [ptrq + 8*gprsize] + + xor xq, xq + cmp widthq, mmsize/4 + jl .loop2 + + mov rq, widthq + and rq, mmsize/4-1 + sub widthq, rq + + mova m6, [sobel_perm] +.loop1: + movu xm3, [c2q + xq] + pmovzxbd m5, [c0q + xq] + vinserti32x4 ym3, [c6q + xq], 1 + pmovzxbd m4, [c8q + xq] + vinserti32x4 m2, m3, [c1q + xq], 2 + vinserti32x4 m3, [c5q + xq], 2 + vinserti32x4 m2, [c7q + xq], 3 + vinserti32x4 m3, [c3q + xq], 3 + vpermb m2, m6, m2 + psubd m4, m5 + vpermb m3, m6, m3 + mova m5, m4 + vpdpbusd m4, m2, [sobel_mulA] {1to16} + vpdpbusd m5, m3, [sobel_mulB] {1to16} + + cvtdq2ps m4, m4 + mulps m4, m4 + + cvtdq2ps m5, m5 + VFMADD231PS m4, m5, m5 + + sqrtps m4, m4 + fmaddps m4, m4, m0, m1 + cvttps2dq m4, m4 + vpmovusdb [dstq + xq], m4 + + add xq, mmsize/4 + cmp xq, widthq + jl .loop1 + + add widthq, rq + cmp xq, widthq + jge .end + +.loop2: + xor rd, rd + pxor m4, m4 + + ;Gx + SOBEL_MUL 0, data_n1 + SOBEL_MUL 1, data_n2 + SOBEL_MUL 2, data_n1 + SOBEL_ADD 6 + SOBEL_MUL 7, data_p2 + SOBEL_ADD 8 + + cvtsi2ss xmm4, rd + mulss xmm4, xmm4 + + xor rd, rd + ;Gy + SOBEL_MUL 0, data_n1 + SOBEL_ADD 2 + SOBEL_MUL 3, data_n2 + SOBEL_MUL 5, data_p2 + SOBEL_MUL 6, data_n1 + SOBEL_ADD 8 + + cvtsi2ss xmm5, rd + fmaddss xmm4, xmm5, xmm5, xmm4 + + sqrtps xmm4, xmm4 + fmaddss xmm4, xmm4, xmm0, xmm1 ;sum = sum * rdiv + bias + cvttps2dq xmm4, xmm4 ; trunc to integer + packssdw xmm4, xmm4 + packuswb xmm4, xmm4 + movd rd, xmm4 + mov [dstq + xq], rb + + add xq, 1 + cmp xq, widthq + jl .loop2 +.end: + RET +%endmacro + +%if ARCH_X86_64 +%if HAVE_AVX512ICL_EXTERNAL +INIT_ZMM avx512icl +FILTER_SOBEL +%endif +%endif diff --git a/libavfilter/x86/vf_convolution_init.c b/libavfilter/x86/vf_convolution_init.c index b78a47d02b..bff10ca1a4 100644 --- a/libavfilter/x86/vf_convolution_init.c +++ b/libavfilter/x86/vf_convolution_init.c @@ -29,6 +29,11 @@ void ff_filter_3x3_sse4(uint8_t *dst, int width, const uint8_t *c[], int peak, int radius, int dstride, int stride, int size); +void ff_filter_sobel_avx512icl(uint8_t *dst, int width, + float scale, float delta, const int *const matrix, + const uint8_t *c[], int peak, int radius, + int dstride, int stride, int size); + av_cold void ff_convolution_init_x86(ConvolutionContext *s) { #if ARCH_X86_64 @@ -44,3 +49,16 @@ av_cold void ff_convolution_init_x86(ConvolutionContext *s) } #endif } + +av_cold void ff_sobel_init_x86(ConvolutionContext *s, int depth, int +nb_planes) { #if ARCH_X86_64 + int cpu_flags = av_get_cpu_flags(); + for (int i = 0; i < nb_planes; i++) { + if (depth == 8) { + if (EXTERNAL_AVX512ICL(cpu_flags)) + s->filter[i] = ff_filter_sobel_avx512icl; + } + } +#endif +} diff --git a/tests/checkasm/Makefile b/tests/checkasm/Makefile index ac02670e64..fe7081f87f 100644 --- a/tests/checkasm/Makefile +++ b/tests/checkasm/Makefile @@ -45,6 +45,7 @@ AVFILTEROBJS-$(CONFIG_GBLUR_FILTER) += vf_gblur.o AVFILTEROBJS-$(CONFIG_HFLIP_FILTER) += vf_hflip.o AVFILTEROBJS-$(CONFIG_THRESHOLD_FILTER) += vf_threshold.o AVFILTEROBJS-$(CONFIG_NLMEANS_FILTER) += vf_nlmeans.o +AVFILTEROBJS-$(CONFIG_CONVOLUTION_FILTER) += vf_convolution.o CHECKASMOBJS-$(CONFIG_AVFILTER) += $(AVFILTEROBJS-yes) diff --git a/tests/checkasm/checkasm.c b/tests/checkasm/checkasm.c index 6b4a0f22b2..12bc0ecef6 100644 --- a/tests/checkasm/checkasm.c +++ b/tests/checkasm/checkasm.c @@ -194,6 +194,9 @@ static const struct { #if CONFIG_THRESHOLD_FILTER { "vf_threshold", checkasm_check_vf_threshold }, #endif + #if CONFIG_CONVOLUTION_FILTER + { "vf_convolution", checkasm_check_vf_convolution }, + #endif #endif #if CONFIG_SWSCALE { "sw_gbrp", checkasm_check_sw_gbrp }, diff --git a/tests/checkasm/checkasm.h b/tests/checkasm/checkasm.h index 171dd06b47..6a06cf020d 100644 --- a/tests/checkasm/checkasm.h +++ b/tests/checkasm/checkasm.h @@ -85,6 +85,7 @@ void checkasm_check_vf_eq(void); void checkasm_check_vf_gblur(void); void checkasm_check_vf_hflip(void); void checkasm_check_vf_threshold(void); +void checkasm_check_vf_convolution(void); void checkasm_check_vp8dsp(void); void checkasm_check_vp9dsp(void); void checkasm_check_videodsp(void); diff --git a/tests/checkasm/vf_convolution.c b/tests/checkasm/vf_convolution.c new file mode 100644 index 0000000000..86820b9a1b --- /dev/null +++ b/tests/checkasm/vf_convolution.c @@ -0,0 +1,103 @@ +/* + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 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 General Public License for more details. + * + * You should have received a copy of the GNU 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 +#include "checkasm.h" +#include "libavfilter/avfilter.h" +#include "libavfilter/convolution.h" +#include "libavutil/intreadwrite.h" +#include "libavutil/mem_internal.h" + +#define WIDTH 512 +#define HEIGHT 512 +#define SRC_STRIDE 512 +#define PIXELS (WIDTH * HEIGHT) + +#define randomize_buffers(buf, size) \ + do { \ + int j; \ + uint8_t *tmp_buf = (uint8_t *)buf;\ + for (j = 0; j< size; j++) \ + tmp_buf[j] = rnd() & 0xFF; \ + } while (0) + +static void check_sobel(const char * report_name) { + LOCAL_ALIGNED_32(uint8_t, src, [PIXELS]); + LOCAL_ALIGNED_32(uint8_t, dst_ref, [PIXELS]); + LOCAL_ALIGNED_32(uint8_t, dst_new, [PIXELS]); + const int height = WIDTH; + const int width = HEIGHT; + const int stride = SRC_STRIDE; + const int dstride = SRC_STRIDE; + int mode = 0; + const uint8_t *c[49]; + const int radius = 1; + const int bpc = 1; + const int step = mode == MATRIX_COLUMN ? 16 : 1; + const int slice_start = 0; + const int slice_end = height; + int y; + const int sizew = mode == MATRIX_COLUMN ? height : width; + float scale = 2; + float delta = 10; + + ConvolutionContext s; + + s.scale = scale; + s.delta = delta; + s.depth = 8; + s.nb_planes = 3; + ff_sobel_init(&s, s.depth, s.nb_planes); + + declare_func(void, uint8_t *dst, int width, float scale, float delta, const int *const matrix, + const uint8_t *c[], int peak, int radius, int dstride, + int stride, int size); + + memset(dst_ref, 0, PIXELS); + memset(dst_new, 0, PIXELS); + randomize_buffers(src, PIXELS); + + if (check_func(s.filter[0], "%s", report_name)) { + for (y = slice_start; y < slice_end; y += step) { + const int xoff = mode == MATRIX_COLUMN ? (y - slice_start) * bpc : radius * bpc; + const int yoff = mode == MATRIX_COLUMN ? radius * dstride : + 0; + + s.setup[0](radius, c, src, stride, radius, width, y, height, bpc); + call_ref(dst_ref + yoff + xoff, sizew - 2 * radius, + scale, delta, NULL, c, 0, radius, + dstride, stride, slice_end - step); + call_new(dst_new + yoff + xoff, sizew - 2 * radius, + scale, delta, NULL, c, 0, radius, + dstride, stride, slice_end - step); + if (memcmp(dst_ref + yoff + xoff, dst_new + yoff + xoff, slice_end - step)) + fail(); + bench_new(dst_new + yoff + xoff, sizew - 2 * radius, + scale, delta, NULL, c, 0, radius, + dstride, stride, slice_end - step); + if (mode != MATRIX_COLUMN) + dst_ref += dstride; + } + } + +} + +void checkasm_check_vf_convolution(void) +{ + check_sobel("sobel"); + report("convolution:sobel"); +} -- 2.27.0 Based on the comments in patch v4, this patch added those changes. Any comments? Changes: 1. Remove redundant zero-initializations in asm code 2. Pass depth and nb_planes as parameters to ff_sobel_init_x86() 3. Filter-agnostic initialization was moved to the beginning of param_init() 4. I also noticed the duplicate configurations. But I think it may not suitable to change other code path in a sobel patch, just change sobel code path. 5. In order to move sobel initialization to convolution.h, I have to move 3 functions to this header file. They are setup_3x3(), filter_sobel() and filter16_sobel() Hi Andreas, Any new comments on this patch for those changes? Ping!! _______________________________________________ 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". _______________________________________________ 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". _______________________________________________ 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".