From patchwork Thu Aug 21 13:41:37 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Milan Zamazal X-Patchwork-Id: 24170 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id D0743BEFBE for ; Thu, 21 Aug 2025 13:41:59 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 8F2DC69257; Thu, 21 Aug 2025 15:41:59 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.b="XOhfTVYU"; dkim-atps=neutral Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 2CEE169257 for ; Thu, 21 Aug 2025 15:41:58 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1755783716; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=xx9p3ljb9Zif+QvCmMVIJBoYxOlBBIXv+s5a+W+i3+0=; b=XOhfTVYUlJiWdUhrHkZxivTvYIt7r/rHPedzoyMsk9AxY2PQYqn1oYe4E6aJ7Y722621D5 5+y72TT2LNY9c27N1Lk+Y0Zt1+BIXl2wFfs8tI+FfGC6wk1QS84NVCczH31uqx+WrT8qHN rd0VY7T0UQTrbwKLFWoa0qqeBIx+5kU= Received: from mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-395-fBAsVwGoPSa5C46pZitt1g-1; Thu, 21 Aug 2025 09:41:53 -0400 X-MC-Unique: fBAsVwGoPSa5C46pZitt1g-1 X-Mimecast-MFC-AGG-ID: fBAsVwGoPSa5C46pZitt1g_1755783712 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 837EA19560A2; Thu, 21 Aug 2025 13:41:52 +0000 (UTC) Received: from mzamazal-thinkpadp1gen7.tpbc.com (unknown [10.45.224.162]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 7488D180047F; Thu, 21 Aug 2025 13:41:50 +0000 (UTC) From: Milan Zamazal To: libcamera-devel@lists.libcamera.org Cc: Milan Zamazal , pobrn@protonmail.com, mail@maciej.szmigiero.name Subject: [PATCH 1/5] libcamera: software_isp: Clarify SwStatsCpu::setWindow use Date: Thu, 21 Aug 2025 15:41:37 +0200 Message-ID: <20250821134141.83236-2-mzamazal@redhat.com> In-Reply-To: <20250821134141.83236-1-mzamazal@redhat.com> References: <20250821134141.83236-1-mzamazal@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: F2NZZGIwHPRWNPHwh_FORN2_5DjwBVXqdM4BpLN38Z8_1755783712 X-Mimecast-Originator: redhat.com content-type: text/plain; charset="US-ASCII"; x-default=true X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" The window coordinates passed to SwStatsCpu::setWindow are confusing. Let's clarify what the coordinates should be. A source of confusion is that the specified window is relative to the processed area. Debayering adjusts line pointers for its processed area and this is what's also passed to stats processing. The window passed to SwStatsCpu::setWindow should either specify the size of the whole processed (not image) area, or its cropping in case the stats shouldn't be gathered over the whole processed area. This patch should clarify this in the code. Signed-off-by: Milan Zamazal --- src/libcamera/software_isp/debayer_cpu.cpp | 6 +++++- src/libcamera/software_isp/swstats_cpu.cpp | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/libcamera/software_isp/debayer_cpu.cpp b/src/libcamera/software_isp/debayer_cpu.cpp index 66f6038c1..bcc847ae6 100644 --- a/src/libcamera/software_isp/debayer_cpu.cpp +++ b/src/libcamera/software_isp/debayer_cpu.cpp @@ -541,7 +541,11 @@ int DebayerCpu::configure(const StreamConfiguration &inputCfg, window_.width = outputCfg.size.width; window_.height = outputCfg.size.height; - /* Don't pass x,y since process() already adjusts src before passing it */ + /* + * Set the stats window to the whole processed window. Its coordinates are + * relative to the debayered area since debayering passes only the part of + * data to be processed to the stats; see SwStatsCpu::setWindow. + */ stats_->setWindow(Rectangle(window_.size())); /* pad with patternSize.Width on both left and right side */ diff --git a/src/libcamera/software_isp/swstats_cpu.cpp b/src/libcamera/software_isp/swstats_cpu.cpp index 4b77b3600..72aa88b69 100644 --- a/src/libcamera/software_isp/swstats_cpu.cpp +++ b/src/libcamera/software_isp/swstats_cpu.cpp @@ -416,6 +416,22 @@ int SwStatsCpu::configure(const StreamConfiguration &inputCfg) /** * \brief Specify window coordinates over which to gather statistics * \param[in] window The window object. + * + * This method specifies the image area over which to gather the statistics. + * It must be called to set the area, otherwise the default zero-sized + * \a Rectangle is used and no statistics is gathered. + * + * The specified \a window is relative to what is passed to processLine* + * methods. Typically, this means processLine* methods get only data from the + * processed area and \a window is \a Rectangle with (0, 0) top-left point and + * of the same size as the processed area. But if statistics is gathered only + * from some part of the image, e.g. its centre, \a window should specify such a + * restriction accordingly. + * + * The method may adjust the window slightly if it is not aligned according to + * the bayer pattern determined in \a SwStatsCpu::configure(). Such an + * adjustment is guaranteed to not exceed the bounds of + * Rectangle(0, 0, window.width, window.height). */ void SwStatsCpu::setWindow(const Rectangle &window) { From patchwork Thu Aug 21 13:41:38 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Milan Zamazal X-Patchwork-Id: 24171 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id A7B80C32BB for ; Thu, 21 Aug 2025 13:42:02 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 30D6C692E9; Thu, 21 Aug 2025 15:42:02 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.b="NjYLAApA"; dkim-atps=neutral Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 4955D692E0 for ; Thu, 21 Aug 2025 15:42:00 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1755783719; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Wl/TK3kHRaw+6lpLzuqpLesDlPkIT1iVxCiZboXG6BA=; b=NjYLAApAzwEJUCVplDZK4Q8DW037xpnH2IRCkQKmTc1/hIrV8UGOTBnmoF0XdP1twmdzc3 A37h3baKvGxjUvQK/tulT8hTQyTxherJNcbs6yuNRLAKWM5hkqWIMZEbjCruD2nd15W2Te AGGCCMNGa2WKrZTmm2ueLi1bHRm5w7I= Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-264-Fa297o7iORi3XLbtTfdlCA-1; Thu, 21 Aug 2025 09:41:55 -0400 X-MC-Unique: Fa297o7iORi3XLbtTfdlCA-1 X-Mimecast-MFC-AGG-ID: Fa297o7iORi3XLbtTfdlCA_1755783714 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id A0E631800289; Thu, 21 Aug 2025 13:41:54 +0000 (UTC) Received: from mzamazal-thinkpadp1gen7.tpbc.com (unknown [10.45.224.162]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 17C37180047F; Thu, 21 Aug 2025 13:41:52 +0000 (UTC) From: Milan Zamazal To: libcamera-devel@lists.libcamera.org Cc: Milan Zamazal , pobrn@protonmail.com, mail@maciej.szmigiero.name Subject: [PATCH 2/5] libcamera: software_isp: Pass correct y-coordinate to stats Date: Thu, 21 Aug 2025 15:41:38 +0200 Message-ID: <20250821134141.83236-3-mzamazal@redhat.com> In-Reply-To: <20250821134141.83236-1-mzamazal@redhat.com> References: <20250821134141.83236-1-mzamazal@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: ocbqjK1QPtZ5YhI563zNfkQRzlb2reJybkLQY39U7BI_1755783714 X-Mimecast-Originator: redhat.com content-type: text/plain; charset="US-ASCII"; x-default=true X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" The window set by SwStatsCpu::setWindow is relative to the processed image area. But debayering passes the processed line y-coordinate to the stats relative to the whole image area. This can result in gathering stats from a wrong image area or in not gathering stats at all. Let's pass the correct y-coordinate to the stats processing methods. Bug: https://bugs.libcamera.org/show_bug.cgi?id=280 Co-developed-by: Maciej S. Szmigiero Signed-off-by: Milan Zamazal Signed-off-by: Maciej S. Szmigiero --- src/libcamera/software_isp/debayer_cpu.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/libcamera/software_isp/debayer_cpu.cpp b/src/libcamera/software_isp/debayer_cpu.cpp index bcc847ae6..185edd814 100644 --- a/src/libcamera/software_isp/debayer_cpu.cpp +++ b/src/libcamera/software_isp/debayer_cpu.cpp @@ -648,7 +648,7 @@ void DebayerCpu::memcpyNextLine(const uint8_t *linePointers[]) void DebayerCpu::process2(const uint8_t *src, uint8_t *dst) { - unsigned int yEnd = window_.y + window_.height; + unsigned int yEnd = window_.height; /* Holds [0] previous- [1] current- [2] next-line */ const uint8_t *linePointers[3]; @@ -664,12 +664,12 @@ void DebayerCpu::process2(const uint8_t *src, uint8_t *dst) linePointers[1] = src + inputConfig_.stride; linePointers[2] = src; /* Last 2 lines also need special handling */ - yEnd -= 2; + yEnd = (yEnd > 2 ? yEnd - 2 : 0); } setupInputMemcpy(linePointers); - for (unsigned int y = window_.y; y < yEnd; y += 2) { + for (unsigned int y = 0; y < yEnd; y += 2) { shiftLinePointers(linePointers, src); memcpyNextLine(linePointers); stats_->processLine0(y, linePointers); @@ -703,7 +703,7 @@ void DebayerCpu::process2(const uint8_t *src, uint8_t *dst) void DebayerCpu::process4(const uint8_t *src, uint8_t *dst) { - const unsigned int yEnd = window_.y + window_.height; + const unsigned int yEnd = window_.height; /* * This holds pointers to [0] 2-lines-up [1] 1-line-up [2] current-line * [3] 1-line-down [4] 2-lines-down. @@ -721,7 +721,7 @@ void DebayerCpu::process4(const uint8_t *src, uint8_t *dst) setupInputMemcpy(linePointers); - for (unsigned int y = window_.y; y < yEnd; y += 4) { + for (unsigned int y = 0; y < yEnd; y += 4) { shiftLinePointers(linePointers, src); memcpyNextLine(linePointers); stats_->processLine0(y, linePointers); From patchwork Thu Aug 21 13:41:39 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Milan Zamazal X-Patchwork-Id: 24172 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 05E73BEFBE for ; Thu, 21 Aug 2025 13:42:03 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 1BAE7692E7; Thu, 21 Aug 2025 15:42:03 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.b="MjdajqiJ"; dkim-atps=neutral Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id AF67E692E6 for ; Thu, 21 Aug 2025 15:42:00 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1755783719; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=wpChjG77ZHEUeywoW7E5UEvUmsBXgEoU1mM9xlPYWpk=; b=MjdajqiJNxthoNZDjfYApZDttRWiMpwLie9KB21OKZonhD2qYJeDO9XUHvHCn3iP6NsTa1 sNJlJxkwX8Hfshv8PfdHmUCKTvPEsElzS/n5G394dQh6wpzPbs5qKWhMpM911xDmOvGqAu szAZek+eLAWOd7zl91GW8kbSVqnRPWU= Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-388-KdcMey6uO7Oat09s8gmaFQ-1; Thu, 21 Aug 2025 09:41:57 -0400 X-MC-Unique: KdcMey6uO7Oat09s8gmaFQ-1 X-Mimecast-MFC-AGG-ID: KdcMey6uO7Oat09s8gmaFQ_1755783716 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id B47681800359; Thu, 21 Aug 2025 13:41:56 +0000 (UTC) Received: from mzamazal-thinkpadp1gen7.tpbc.com (unknown [10.45.224.162]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 1B332180047F; Thu, 21 Aug 2025 13:41:54 +0000 (UTC) From: Milan Zamazal To: libcamera-devel@lists.libcamera.org Cc: Milan Zamazal , pobrn@protonmail.com, mail@maciej.szmigiero.name Subject: [PATCH 3/5] libcamera: software_isp: Check processed window size alignment Date: Thu, 21 Aug 2025 15:41:39 +0200 Message-ID: <20250821134141.83236-4-mzamazal@redhat.com> In-Reply-To: <20250821134141.83236-1-mzamazal@redhat.com> References: <20250821134141.83236-1-mzamazal@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: iIzTYZ8ryxzHepz-cmEcsAkYyg_IK8wBv73GuM3OsS8_1755783716 X-Mimecast-Originator: redhat.com content-type: text/plain; charset="US-ASCII"; x-default=true X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" The window of the image that should be debayered must be aligned to the bayer pattern size. This is already ensured for the window corner coordinates but not for the window sizes. We can either make the window sizes aligned similarly to how the window corner coordinates are aligned or reject an unaligned configuration. This patches chooses the latter as using a different window size than the requested output size could lead to artefacts. Since such a situation is not expected to occur in correctly set up environments, the change should have no negative impact. Signed-off-by: Milan Zamazal Reviewed-by: Barnabás Pőcze --- src/libcamera/software_isp/debayer_cpu.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/libcamera/software_isp/debayer_cpu.cpp b/src/libcamera/software_isp/debayer_cpu.cpp index 185edd814..3200b0c53 100644 --- a/src/libcamera/software_isp/debayer_cpu.cpp +++ b/src/libcamera/software_isp/debayer_cpu.cpp @@ -539,7 +539,17 @@ int DebayerCpu::configure(const StreamConfiguration &inputCfg, window_.y = ((inputCfg.size.height - outputCfg.size.height) / 2) & ~(inputConfig_.patternSize.height - 1); window_.width = outputCfg.size.width; + if (window_.width % inputConfig_.patternSize.width != 0) { + LOG(Debayer, Error) + << "Output width is not a multiple of the bayer pattern width"; + return -EINVAL; + } window_.height = outputCfg.size.height; + if (window_.height % inputConfig_.patternSize.height != 0) { + LOG(Debayer, Error) + << "Output height is not a multiple of the bayer pattern height"; + return -EINVAL; + } /* * Set the stats window to the whole processed window. Its coordinates are From patchwork Thu Aug 21 13:41:40 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Milan Zamazal X-Patchwork-Id: 24173 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id CB1E2BEFBE for ; Thu, 21 Aug 2025 13:42:07 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 70DA2692E8; Thu, 21 Aug 2025 15:42:07 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.b="ZNlTBGTs"; dkim-atps=neutral Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 2CC8A69257 for ; Thu, 21 Aug 2025 15:42:05 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1755783724; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=C/tqtmpGB5h77lOnle5+kULZCqZyYam+O1srY69HoA0=; b=ZNlTBGTs31DgvQrQMOzquOrPpiBEqx4WHH/pmiWM7B2H/jtN52lSSGhr9/Na0uY2AfuqFq N31N5R/lkrb32WifBtLdHQAmpn2DSBGs2Z4HtPvWybV5QUqxkv5A2U5igLwjtXrQboSGVO /e1dp4lEcFlDxs4Xb5KwBDxyvccaFt0= Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-267-GeP0Tyi2Pg6vDbFqgxQ3-g-1; Thu, 21 Aug 2025 09:42:01 -0400 X-MC-Unique: GeP0Tyi2Pg6vDbFqgxQ3-g-1 X-Mimecast-MFC-AGG-ID: GeP0Tyi2Pg6vDbFqgxQ3-g_1755783720 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 5EEFF180034A; Thu, 21 Aug 2025 13:41:59 +0000 (UTC) Received: from mzamazal-thinkpadp1gen7.tpbc.com (unknown [10.45.224.162]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 2E1ED180047F; Thu, 21 Aug 2025 13:41:56 +0000 (UTC) From: Milan Zamazal To: libcamera-devel@lists.libcamera.org Cc: Milan Zamazal , pobrn@protonmail.com, mail@maciej.szmigiero.name Subject: [PATCH 4/5] libcamera: simple: Avoid incorrect arithmetic in AWB Date: Thu, 21 Aug 2025 15:41:40 +0200 Message-ID: <20250821134141.83236-5-mzamazal@redhat.com> In-Reply-To: <20250821134141.83236-1-mzamazal@redhat.com> References: <20250821134141.83236-1-mzamazal@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: BHUVTJA81oC0fBb3MRtcN9Y4w0LWcJhEspWGHmi-dnY_1755783720 X-Mimecast-Originator: redhat.com content-type: text/plain; charset="US-ASCII"; x-default=true X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" The R/G/B sums computed in AWB simple IPA may be zero or perhaps even negative. Let's make sure the sums are always positive, to prevent division by zero or completely nonsense results. Signed-off-by: Milan Zamazal --- src/ipa/simple/algorithms/awb.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/ipa/simple/algorithms/awb.cpp b/src/ipa/simple/algorithms/awb.cpp index cf567e894..9509129e9 100644 --- a/src/ipa/simple/algorithms/awb.cpp +++ b/src/ipa/simple/algorithms/awb.cpp @@ -7,6 +7,7 @@ #include "awb.h" +#include #include #include @@ -69,9 +70,10 @@ void Awb::process(IPAContext &context, const uint64_t nPixels = std::accumulate( histogram.begin(), histogram.end(), 0); const uint64_t offset = blackLevel * nPixels; - const uint64_t sumR = stats->sumR_ - offset / 4; - const uint64_t sumG = stats->sumG_ - offset / 2; - const uint64_t sumB = stats->sumB_ - offset / 4; + const uint64_t minValid = 1; + const uint64_t sumR = std::max(stats->sumR_ - offset / 4, minValid); + const uint64_t sumG = std::max(stats->sumG_ - offset / 2, minValid); + const uint64_t sumB = std::max(stats->sumB_ - offset / 4, minValid); /* * Calculate red and blue gains for AWB. From patchwork Thu Aug 21 13:41:41 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Milan Zamazal X-Patchwork-Id: 24174 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 9A231BEFBE for ; Thu, 21 Aug 2025 13:42:10 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 5A698692ED; Thu, 21 Aug 2025 15:42:10 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.b="LJqq8SIO"; dkim-atps=neutral Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 5C76669257 for ; Thu, 21 Aug 2025 15:42:09 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1755783728; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=KMRZfXmG+aUPlclYMk6hPyaAt+ixcRkQlW0Jn7FZTro=; b=LJqq8SIO3G8Sm8OLXpoD+82uAMsG2frNj0F1v1+a+H3hwAxXskIbeD26zAt2WuN9zpj54F f002UCV5R4DRKCzdnq3wP0pVZiXhgEAHzQnfQUz0ANzahqq2h0avo09R58lG+/M3iXKZlO 50BHMYRMD0ARjJbxyMlzf9MNY9wxou8= Received: from mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-375-mta7ESvZNnSo9hdAa00arA-1; Thu, 21 Aug 2025 09:42:02 -0400 X-MC-Unique: mta7ESvZNnSo9hdAa00arA-1 X-Mimecast-MFC-AGG-ID: mta7ESvZNnSo9hdAa00arA_1755783721 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 9B362195608A; Thu, 21 Aug 2025 13:42:01 +0000 (UTC) Received: from mzamazal-thinkpadp1gen7.tpbc.com (unknown [10.45.224.162]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id BF20A180029B; Thu, 21 Aug 2025 13:41:59 +0000 (UTC) From: Milan Zamazal To: libcamera-devel@lists.libcamera.org Cc: Milan Zamazal , pobrn@protonmail.com, mail@maciej.szmigiero.name Subject: [PATCH 5/5] libcamera: simple: Prevent division by zero in BLC Date: Thu, 21 Aug 2025 15:41:41 +0200 Message-ID: <20250821134141.83236-6-mzamazal@redhat.com> In-Reply-To: <20250821134141.83236-1-mzamazal@redhat.com> References: <20250821134141.83236-1-mzamazal@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: 1cZxlQJAJ0U8ypNAe53BvVcnbiOY7qaNjm85xmmK0LQ_1755783721 X-Mimecast-Originator: redhat.com content-type: text/plain; charset="US-ASCII"; x-default=true X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" When there are no values in the histogram, BLC simple IPA can crash on division by zero. We cannot get anything meaningful in such a case anyway, let's simply return from `process()' then. Signed-off-by: Milan Zamazal Tested-by: Barnabás Pőcze Reviewed-by: Barnabás Pőcze --- src/ipa/simple/algorithms/blc.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/ipa/simple/algorithms/blc.cpp b/src/ipa/simple/algorithms/blc.cpp index 8c1e9ed08..71731c6f6 100644 --- a/src/ipa/simple/algorithms/blc.cpp +++ b/src/ipa/simple/algorithms/blc.cpp @@ -77,6 +77,11 @@ void BlackLevel::process(IPAContext &context, constexpr float ignoredPercentage = 0.02; const unsigned int total = std::accumulate(begin(histogram), end(histogram), 0); + if (total == 0) { + LOG(IPASoftBL, Debug) << "Not guessing black level, histogram is empty"; + return; + }; + const unsigned int pixelThreshold = ignoredPercentage * total; const unsigned int histogramRatio = 256 / SwIspStats::kYHistogramSize; const unsigned int currentBlackIdx =