From patchwork Thu Sep 25 19:28:50 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Milan Zamazal X-Patchwork-Id: 24455 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 430A9C328C for ; Thu, 25 Sep 2025 19:29:15 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id C72176B5FB; Thu, 25 Sep 2025 21:29:14 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.b="acZ6Rs8a"; 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 E61AF69318 for ; Thu, 25 Sep 2025 21:29:10 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1758828549; 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=NwrFJpnHPVrzbjm6OuH8AKj+KCwc3Xtq1lsigQHKLgQ=; b=acZ6Rs8af4fKn10QR4Em5sEmG5gsFHN2Z3PHysIw6qhhaZ+3T7gVDFKbTVKpXaIxVcWOA9 DRjgZnia82QQDG1/9UX4Fcc1a/La/anP6EMeVIsxceL5DrRi9tBNGCWqHji8FgqIt7m++y x731pOj4MYTle1bkgXdghDY2+j2R6yg= Received: from mx-prod-mc-02.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-591-Qij5hSRKMP2DEeijs3TKLA-1; Thu, 25 Sep 2025 15:29:06 -0400 X-MC-Unique: Qij5hSRKMP2DEeijs3TKLA-1 X-Mimecast-MFC-AGG-ID: Qij5hSRKMP2DEeijs3TKLA_1758828545 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (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-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 2D4FE195609E; Thu, 25 Sep 2025 19:29:05 +0000 (UTC) Received: from mzamazal-thinkpadp1gen7.tpbc.com (unknown [10.44.32.62]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 878E91956095; Thu, 25 Sep 2025 19:29:03 +0000 (UTC) From: Milan Zamazal To: libcamera-devel@lists.libcamera.org Cc: Milan Zamazal , pobrn@protonmail.com, mail@maciej.szmigiero.name Subject: [PATCH v4 1/7] libcamera: software_isp: Fix width adjustment in SwStatsCpu::setWindow Date: Thu, 25 Sep 2025 21:28:50 +0200 Message-ID: <20250925192856.77881-2-mzamazal@redhat.com> In-Reply-To: <20250925192856.77881-1-mzamazal@redhat.com> References: <20250925192856.77881-1-mzamazal@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: Z4H8srSy5dJLyFkciTpfr8xkFKB7TxW0RA8RA5XRvPM_1758828545 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" SwStatsCpu::setWindow reduces the window width by the added x-offset, to prevent exceeding image bounds. But if the window width is smaller than the x-offset, we get unsigned integer underflow. Fix it by setting the window width to 0 in such a case. Signed-off-by: Milan Zamazal Reviewed-by: Barnabás Pőcze --- src/libcamera/software_isp/swstats_cpu.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libcamera/software_isp/swstats_cpu.cpp b/src/libcamera/software_isp/swstats_cpu.cpp index 4b77b3600..e8a1d52f2 100644 --- a/src/libcamera/software_isp/swstats_cpu.cpp +++ b/src/libcamera/software_isp/swstats_cpu.cpp @@ -426,7 +426,7 @@ void SwStatsCpu::setWindow(const Rectangle &window) window_.y &= ~(patternSize_.height - 1); /* width_ - xShift_ to make sure the window fits */ - window_.width -= xShift_; + window_.width = (window_.width > xShift_ ? window_.width - xShift_ : 0); window_.width &= ~(patternSize_.width - 1); window_.height &= ~(patternSize_.height - 1); } From patchwork Thu Sep 25 19:28:51 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Milan Zamazal X-Patchwork-Id: 24456 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 3CF3EC328C for ; Thu, 25 Sep 2025 19:29:20 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id D9A4A6B606; Thu, 25 Sep 2025 21:29:19 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.b="AOQmzjCw"; 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 1A5116B5FF for ; Thu, 25 Sep 2025 21:29:14 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1758828553; 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=JA1CQ4xqsSqvxKDRsYazb2IohN7xUAnAqdTkJPMvb7s=; b=AOQmzjCw/vqK0BvawXJX+83tG44pcdNoI5xNf6irKpLdIbHfiyBjs9e4RG/RLWZwQYH9KK qAa9b8X7yu4Gc83Y2eI9HMBpvtELKp4ZBxMBSD9UzZXkOMwMd8iYhO/r4S/OOwf6/tRRuY 8AYcSb5+YtxCjUohMOgOYRqQy59hJHY= 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-357-J4mEya7IN7yzGa94OZWzrA-1; Thu, 25 Sep 2025 15:29:08 -0400 X-MC-Unique: J4mEya7IN7yzGa94OZWzrA-1 X-Mimecast-MFC-AGG-ID: J4mEya7IN7yzGa94OZWzrA_1758828547 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (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 A74C7180028E; Thu, 25 Sep 2025 19:29:07 +0000 (UTC) Received: from mzamazal-thinkpadp1gen7.tpbc.com (unknown [10.44.32.62]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id C41DE1956095; Thu, 25 Sep 2025 19:29:05 +0000 (UTC) From: Milan Zamazal To: libcamera-devel@lists.libcamera.org Cc: Milan Zamazal , pobrn@protonmail.com, mail@maciej.szmigiero.name Subject: [PATCH v4 2/7] libcamera: software_isp: Clarify SwStatsCpu::setWindow use Date: Thu, 25 Sep 2025 21:28:51 +0200 Message-ID: <20250925192856.77881-3-mzamazal@redhat.com> In-Reply-To: <20250925192856.77881-1-mzamazal@redhat.com> References: <20250925192856.77881-1-mzamazal@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: 71lGiaOptKsqsQPqQZh0KbYgKgyWXqzn90EuyBOuPGM_1758828547 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. Reviewed-by: Maciej S. Szmigiero Signed-off-by: Milan Zamazal --- src/libcamera/software_isp/debayer_cpu.cpp | 6 +++++- src/libcamera/software_isp/swstats_cpu.cpp | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/libcamera/software_isp/debayer_cpu.cpp b/src/libcamera/software_isp/debayer_cpu.cpp index 2dc85e5e0..bcaaa5dee 100644 --- a/src/libcamera/software_isp/debayer_cpu.cpp +++ b/src/libcamera/software_isp/debayer_cpu.cpp @@ -554,7 +554,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 e8a1d52f2..c936ef1dc 100644 --- a/src/libcamera/software_isp/swstats_cpu.cpp +++ b/src/libcamera/software_isp/swstats_cpu.cpp @@ -416,6 +416,25 @@ 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. + * + * It is the responsibility of the callers to provide sensible \a window values, + * most notably not exceeding the original image boundaries. + * + * The method may adjust the window slightly if it is not aligned according to + * the bayer pattern determined in \a SwStatsCpu::configure(). If that happens, + * it is guaranteed that non-negative top-left point coordinates remain + * non-negative and that the window width and height don't get enlarged. */ void SwStatsCpu::setWindow(const Rectangle &window) { From patchwork Thu Sep 25 19:28:52 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Milan Zamazal X-Patchwork-Id: 24457 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 24A9DBDB1C for ; Thu, 25 Sep 2025 19:29:22 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id D0C1A6B60E; Thu, 25 Sep 2025 21:29:20 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.b="CtARagey"; 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 38FE569318 for ; Thu, 25 Sep 2025 21:29:16 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1758828554; 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=PtaZlGq26lh1o6lLWlr+mPPbwqfBa5VPofvYiEmcXMk=; b=CtARageyb6KYxVaNVdWFtv7M4mLFID0gkFOyJqMlIHtnNHes7dI1XpyRul0EYGKM7vzQEG Xw0BTWCENDugsWJfZLtdmawAQbrIxluvuNalpbo0arzmzef0bguFkHVnitwHHI2Ns0y68o oy+J9mf3DqRecUXDTveSbe6PSHa4rK8= Received: from mx-prod-mc-02.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-629-DB1HusEEMrKOOZmbfJ-x_A-1; Thu, 25 Sep 2025 15:29:11 -0400 X-MC-Unique: DB1HusEEMrKOOZmbfJ-x_A-1 X-Mimecast-MFC-AGG-ID: DB1HusEEMrKOOZmbfJ-x_A_1758828550 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (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-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 4FC861956096; Thu, 25 Sep 2025 19:29:10 +0000 (UTC) Received: from mzamazal-thinkpadp1gen7.tpbc.com (unknown [10.44.32.62]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 320A71956095; Thu, 25 Sep 2025 19:29:07 +0000 (UTC) From: Milan Zamazal To: libcamera-devel@lists.libcamera.org Cc: Milan Zamazal , pobrn@protonmail.com, mail@maciej.szmigiero.name Subject: [PATCH v4 3/7] libcamera: software_isp: Pass correct y-coordinate to stats Date: Thu, 25 Sep 2025 21:28:52 +0200 Message-ID: <20250925192856.77881-4-mzamazal@redhat.com> In-Reply-To: <20250925192856.77881-1-mzamazal@redhat.com> References: <20250925192856.77881-1-mzamazal@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: N2p8er34GQQ1qsX4vL-io8qY4b5-6LkI7amDmWR0ZOc_1758828550 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 Signed-off-by: Maciej S. Szmigiero Signed-off-by: Milan Zamazal --- 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 bcaaa5dee..5f3f22f07 100644 --- a/src/libcamera/software_isp/debayer_cpu.cpp +++ b/src/libcamera/software_isp/debayer_cpu.cpp @@ -661,7 +661,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]; @@ -677,12 +677,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); @@ -716,7 +716,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. @@ -734,7 +734,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 Sep 25 19:28:53 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Milan Zamazal X-Patchwork-Id: 24458 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 26FECC328C for ; Thu, 25 Sep 2025 19:29:24 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 19D046B601; Thu, 25 Sep 2025 21:29:23 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.b="EBAtDIXW"; 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 C1C7F6B5F3 for ; Thu, 25 Sep 2025 21:29:16 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1758828555; 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=kxWXAsfsXBoBanj0UP0moQFq7qNzO+r51HP0qQyCo4E=; b=EBAtDIXWdIRFPK7cS1HBRa7EhA2s15FVcRghzsRf744nCYp2WFIAnmlV0IHDEKhN8IkgK6 U+T05NKpex+GYhCAMFzUCIb3cFKgbByMP19+PXHvzUECLCAZBEdKZzOHgngnKipZK+cbpJ PIWNbj6VZyRBjlSsNeg+uiRkKBJOQJ8= Received: from mx-prod-mc-03.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-214-Lg0AhNAePEaHwAyWYPRCsQ-1; Thu, 25 Sep 2025 15:29:14 -0400 X-MC-Unique: Lg0AhNAePEaHwAyWYPRCsQ-1 X-Mimecast-MFC-AGG-ID: Lg0AhNAePEaHwAyWYPRCsQ_1758828553 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (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-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 127C119560B5; Thu, 25 Sep 2025 19:29:13 +0000 (UTC) Received: from mzamazal-thinkpadp1gen7.tpbc.com (unknown [10.44.32.62]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id A7D861956095; Thu, 25 Sep 2025 19:29:10 +0000 (UTC) From: Milan Zamazal To: libcamera-devel@lists.libcamera.org Cc: Milan Zamazal , pobrn@protonmail.com, mail@maciej.szmigiero.name, =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= Subject: [PATCH v4 4/7] libcamera: software_isp: Check processed window size alignment Date: Thu, 25 Sep 2025 21:28:53 +0200 Message-ID: <20250925192856.77881-5-mzamazal@redhat.com> In-Reply-To: <20250925192856.77881-1-mzamazal@redhat.com> References: <20250925192856.77881-1-mzamazal@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: yZ3ghKfvNbn_jAyBT2TtLw14LrPJAPX9udlhgzmAkJg_1758828553 X-Mimecast-Originator: redhat.com 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 a requirement of the current debayering implementation). 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. Reviewed-by: Barnabás Pőcze Reviewed-by: Maciej S. Szmigiero Signed-off-by: Milan Zamazal --- 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 5f3f22f07..b3b326d9c 100644 --- a/src/libcamera/software_isp/debayer_cpu.cpp +++ b/src/libcamera/software_isp/debayer_cpu.cpp @@ -552,7 +552,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 Sep 25 19:28:54 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Milan Zamazal X-Patchwork-Id: 24459 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 B3D44C3331 for ; Thu, 25 Sep 2025 19:29:27 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id D8ACE6B601; Thu, 25 Sep 2025 21:29:26 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.b="L7pQtkgy"; 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 296206B611 for ; Thu, 25 Sep 2025 21:29:23 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1758828562; 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=LyZsWx5DwXfv2ZPUtHNL2o3r3JWEFuo++LnL2EVHaVQ=; b=L7pQtkgyHkUZ6ifdgL8zCbsBYflbSfn/On0xwoMPlY9geuoMjagCvNiOMXRBAj1Buqtf4c NRsrXjTY76BO9CKYphj390oT09RielF0tHFzuEFNo+Gy7xrbhNzKE1DyTj/tBo/KMvDUat EN16AOMMgydSPzFkb605ttYlXL6ROEk= Received: from mx-prod-mc-02.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-151-xEPBYVQ4Oe257GFlL1vL5w-1; Thu, 25 Sep 2025 15:29:16 -0400 X-MC-Unique: xEPBYVQ4Oe257GFlL1vL5w-1 X-Mimecast-MFC-AGG-ID: xEPBYVQ4Oe257GFlL1vL5w_1758828555 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (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-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id A17E1195609F; Thu, 25 Sep 2025 19:29:15 +0000 (UTC) Received: from mzamazal-thinkpadp1gen7.tpbc.com (unknown [10.44.32.62]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id A01691956095; Thu, 25 Sep 2025 19:29:13 +0000 (UTC) From: Milan Zamazal To: libcamera-devel@lists.libcamera.org Cc: Milan Zamazal , pobrn@protonmail.com, mail@maciej.szmigiero.name Subject: [PATCH v4 5/7] libcamera: simple: Avoid incorrect arithmetic in AWB Date: Thu, 25 Sep 2025 21:28:54 +0200 Message-ID: <20250925192856.77881-6-mzamazal@redhat.com> In-Reply-To: <20250925192856.77881-1-mzamazal@redhat.com> References: <20250925192856.77881-1-mzamazal@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: 7U7vob3z-JDAdOG3_vO7NpaWFkydLLG-4Ip04IcvNIU_1758828555 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 | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/ipa/simple/algorithms/awb.cpp b/src/ipa/simple/algorithms/awb.cpp index cf567e894..8231a4968 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 @@ -68,10 +69,11 @@ 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 int64_t offset = blackLevel * nPixels; + const int64_t minValid = 1; + const uint64_t sumR = std::max(static_cast(stats->sumR_) - offset / 4, minValid); + const uint64_t sumG = std::max(static_cast(stats->sumG_) - offset / 2, minValid); + const uint64_t sumB = std::max(static_cast(stats->sumB_) - offset / 4, minValid); /* * Calculate red and blue gains for AWB. From patchwork Thu Sep 25 19:28:55 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Milan Zamazal X-Patchwork-Id: 24460 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 9D815C3332 for ; Thu, 25 Sep 2025 19:29:30 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 0DF1D6B61A; Thu, 25 Sep 2025 21:29:30 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.b="AdFLHNDP"; 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 C3EB26B5FB for ; Thu, 25 Sep 2025 21:29:25 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1758828564; 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=lmNWTOBg6UB3HS/T/mVXkajSkni9mVLeYsp5zO1MAq8=; b=AdFLHNDPZ3pWZYgd5DHAJfrmnnMch0F7rJdamHoLd1sNOFnehu+LQ/kYfSGz49EVjbf+yX 89Q9DfRhC87X2R4d+BsI8yHGeOyvUSXFg9Qc7lWR8rY6xg03hCui5wuJFdlJovkRknHNh2 wvsmVNxgWJo1dUmdXErgbMHuZkZ/DCU= 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-509-3BQj1s2UPZWff4VAFfoCFQ-1; Thu, 25 Sep 2025 15:29:20 -0400 X-MC-Unique: 3BQj1s2UPZWff4VAFfoCFQ-1 X-Mimecast-MFC-AGG-ID: 3BQj1s2UPZWff4VAFfoCFQ_1758828559 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (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 036BD18002D7; Thu, 25 Sep 2025 19:29:19 +0000 (UTC) Received: from mzamazal-thinkpadp1gen7.tpbc.com (unknown [10.44.32.62]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 355531956095; Thu, 25 Sep 2025 19:29:15 +0000 (UTC) From: Milan Zamazal To: libcamera-devel@lists.libcamera.org Cc: Milan Zamazal , pobrn@protonmail.com, mail@maciej.szmigiero.name, =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= Subject: [PATCH v4 6/7] ipa: simple: blc: Prevent division by zero in BLC Date: Thu, 25 Sep 2025 21:28:55 +0200 Message-ID: <20250925192856.77881-7-mzamazal@redhat.com> In-Reply-To: <20250925192856.77881-1-mzamazal@redhat.com> References: <20250925192856.77881-1-mzamazal@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: Rp0C7HP7_qC_bIDYG_DDrHNBqbGgLTglyEqJKE973rU_1758828559 X-Mimecast-Originator: redhat.com 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. Tested-by: Barnabás Pőcze Reviewed-by: Barnabás Pőcze Reviewed-by: Maciej S. Szmigiero Signed-off-by: Milan Zamazal --- 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..ea5356443 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 = From patchwork Thu Sep 25 19:28:56 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Milan Zamazal X-Patchwork-Id: 24461 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 BB839BDB1C for ; Thu, 25 Sep 2025 19:29:32 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 1EB736B60E; Thu, 25 Sep 2025 21:29:32 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.b="CjsEwzVk"; 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 F20EB6B614 for ; Thu, 25 Sep 2025 21:29:26 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1758828566; 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=8Ar7rPvF0560/BLcKfYhFP4NTCURP5c0LZstSfMrNDk=; b=CjsEwzVkp8EvYqe+4Z4qtB/OnLYK4QQXasAGwOeeiDw1F4ciZ90Zf6rxehs/vI48xoVA9/ GAdKghuyR8bEr4io3BZgmHnyO4GrhGL5us9g9guCWTXag2faQhkY2vPYY44iLwc3gz6Lcj WGbGNKCgSwN2bXNVcsY6iJAS7333aBw= 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-543-eFNHN-hPPoS2o9SEGshnEw-1; Thu, 25 Sep 2025 15:29:22 -0400 X-MC-Unique: eFNHN-hPPoS2o9SEGshnEw-1 X-Mimecast-MFC-AGG-ID: eFNHN-hPPoS2o9SEGshnEw_1758828562 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (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 A2A811800350; Thu, 25 Sep 2025 19:29:21 +0000 (UTC) Received: from mzamazal-thinkpadp1gen7.tpbc.com (unknown [10.44.32.62]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 9174F19540EB; Thu, 25 Sep 2025 19:29:19 +0000 (UTC) From: Milan Zamazal To: libcamera-devel@lists.libcamera.org Cc: Milan Zamazal , pobrn@protonmail.com, mail@maciej.szmigiero.name, =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= Subject: [PATCH v4 7/7] ipa: simple: agc: Prevent division by zero in AGC Date: Thu, 25 Sep 2025 21:28:56 +0200 Message-ID: <20250925192856.77881-8-mzamazal@redhat.com> In-Reply-To: <20250925192856.77881-1-mzamazal@redhat.com> References: <20250925192856.77881-1-mzamazal@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: UnSpxnjQ9oUGsPBJBei72wCXmGdHVG2t8FSX4PrLHpo_1758828562 X-Mimecast-Originator: redhat.com 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" If the histogram size is non-zero but lower than the number of bins, yHistValsPerBin is zero and then the AGC processing crashes on division by it. Let's check yHistValsPerBin for being zero and stop AGC processing in such a case. The condition also covers the cases where histogramSize or yHistValsPerBinMod are zero. Tested-by: Barnabás Pőcze Signed-off-by: Milan Zamazal --- src/ipa/simple/algorithms/agc.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/ipa/simple/algorithms/agc.cpp b/src/ipa/simple/algorithms/agc.cpp index c46bb0ebe..e47949ec4 100644 --- a/src/ipa/simple/algorithms/agc.cpp +++ b/src/ipa/simple/algorithms/agc.cpp @@ -124,6 +124,12 @@ void Agc::process(IPAContext &context, unsigned int denom = 0; unsigned int num = 0; + if (yHistValsPerBin == 0) { + LOG(IPASoftExposure, Debug) + << "Not adjusting exposure due to insufficient histogram data"; + return; + } + for (unsigned int i = 0; i < histogramSize; i++) { unsigned int idx = (i - (i / yHistValsPerBinMod)) / yHistValsPerBin; exposureBins[idx] += histogram[blackLevelHistIdx + i];