{"id":17442,"url":"https://patchwork.libcamera.org/api/patches/17442/?format=json","web_url":"https://patchwork.libcamera.org/patch/17442/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20220927023642.12341-32-laurent.pinchart@ideasonboard.com>","date":"2022-09-27T02:36:40","name":"[libcamera-devel,v5,31/33] ipa: rkisp1: awb: Clamp gains to prevent divisions by zero","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"8b1ea912607f583254a9be7b0a9f9c8badf9b2ee","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/?format=json","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/17442/mbox/","series":[{"id":3506,"url":"https://patchwork.libcamera.org/api/series/3506/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=3506","date":"2022-09-27T02:36:09","name":"ipa: Frame context queue, IPU3 & RkISP consolidation, and RkISP1 improvements","version":5,"mbox":"https://patchwork.libcamera.org/series/3506/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/17442/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/17442/checks/","tags":{},"headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 57BEAC327E\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue, 27 Sep 2022 02:37:50 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 1A17C62378;\n\tTue, 27 Sep 2022 04:37:50 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 0D7CE62295\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 27 Sep 2022 04:37:48 +0200 (CEST)","from pendragon.ideasonboard.com (62-78-145-57.bb.dnainternet.fi\n\t[62.78.145.57])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 85A42E5\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 27 Sep 2022 04:37:47 +0200 (CEST)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1664246270;\n\tbh=fRiz4PGXGBvt4bzwxnX/+k7/Gm/HEywmgBG7nxHurcQ=;\n\th=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe:\n\tList-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:\n\tFrom;\n\tb=XQoslnuQd53mmVMQo3z2wWu8Pyh5AVhcMj2nZT46Vf43PJh8XSpKIfzSYHcRQ5/2g\n\tCSm7XSsWKjomozdxLRjywOjgbOEV/VO6Ao3qjojcj0GzKV0pDslUOyq0wFzTyAmwGK\n\t6vYzHMxquSx0dCA+NZPRSzWilEwqEExGE9zqA7F6Xqyl0Taz/0kuoFZ7Eob4OP+7EL\n\tbY/KoJ7f/SNZlgW+/V8dtOvzh+q2Lp+WKMQiVRO9i+Bzu9rg7p7RQsR/qvwLWAM1Xh\n\tIOdDpu7EN7VQh1H8URVtT40md4ZKPfpE6VsXXnDDuufIgjyYFfBjxCJOugtc1x+JQc\n\tzL9MdHfDUsPzg==","v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1664246267;\n\tbh=fRiz4PGXGBvt4bzwxnX/+k7/Gm/HEywmgBG7nxHurcQ=;\n\th=From:To:Subject:Date:In-Reply-To:References:From;\n\tb=G+jS83vt+79paWYUyP4UxddnXA+P6yDBONwajEyPM0/GBbYHGBzKWcF5/OOESkNUX\n\tlWEzb1hNgLsPZJyEgOMYAUWRQIVxNCB5SQbmzuJnkGy4X6vqW3fQ3JbEiLYtiaMbqO\n\t8Xh/UZHWysFBr1jJqalBo6qX4fR3r5GxE4/AQ3f0="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"G+jS83vt\"; dkim-atps=neutral","To":"libcamera-devel@lists.libcamera.org","Date":"Tue, 27 Sep 2022 05:36:40 +0300","Message-Id":"<20220927023642.12341-32-laurent.pinchart@ideasonboard.com>","X-Mailer":"git-send-email 2.35.1","In-Reply-To":"<20220927023642.12341-1-laurent.pinchart@ideasonboard.com>","References":"<20220927023642.12341-1-laurent.pinchart@ideasonboard.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH v5 31/33] ipa: rkisp1: awb: Clamp gains to\n\tprevent divisions by zero","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","From":"Laurent Pinchart via libcamera-devel\n\t<libcamera-devel@lists.libcamera.org>","Reply-To":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"The gain values are currently clamped to the range [0.0, 3.996] used by\nthe hardware. A zero value makes little sense, as it would completely\nremove the contribution of the corresponding color channel from the AWB\naccumulators, but worse, would lead to divisions by zero when\ncalculating the raw means in subsequent iterations. Prevent this by\nsetting the minimum gain value to 1/256.\n\nWhile at it, clamp the gain values before filtering them, to improve the\nstability of the control loop.\n\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\nReviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\nReviewed-by: Jacopo Mondi <jacopo@jmondi.org>\n---\nChanges since v4:\n\n- Fix typo in comment\n---\n src/ipa/rkisp1/algorithms/awb.cpp | 22 +++++++++++++++-------\n 1 file changed, 15 insertions(+), 7 deletions(-)","diff":"diff --git a/src/ipa/rkisp1/algorithms/awb.cpp b/src/ipa/rkisp1/algorithms/awb.cpp\nindex 59664d09b84c..a3066fbb1994 100644\n--- a/src/ipa/rkisp1/algorithms/awb.cpp\n+++ b/src/ipa/rkisp1/algorithms/awb.cpp\n@@ -265,21 +265,29 @@ void Awb::process(IPAContext &context,\n \n \tframeContext.awb.temperatureK = estimateCCT(redMean, greenMean, blueMean);\n \n-\t/* Estimate the red and blue gains to apply in a grey world. */\n+\t/*\n+\t * Estimate the red and blue gains to apply in a grey world. The green\n+\t * gain is hardcoded to 1.0.\n+\t */\n \tdouble redGain = greenMean / (redMean + 1);\n \tdouble blueGain = greenMean / (blueMean + 1);\n \n+\t/*\n+\t * Clamp the gain values to the hardware, which expresses gains as Q2.8\n+\t * unsigned integer values. Set the minimum just above zero to avoid\n+\t * divisions by zero when computing the raw means in subsequent\n+\t * iterations.\n+\t */\n+\tredGain = std::clamp(redGain, 1.0 / 256, 1023.0 / 256);\n+\tblueGain = std::clamp(blueGain, 1.0 / 256, 1023.0 / 256);\n+\n \t/* Filter the values to avoid oscillations. */\n \tdouble speed = 0.2;\n \tredGain = speed * redGain + (1 - speed) * activeState.awb.gains.automatic.red;\n \tblueGain = speed * blueGain + (1 - speed) * activeState.awb.gains.automatic.blue;\n \n-\t/*\n-\t * Gain values are unsigned integer value, range 0 to 4 with 8 bit\n-\t * fractional part. Hardcode the green gain to 1.0.\n-\t */\n-\tactiveState.awb.gains.automatic.red = std::clamp(redGain, 0.0, 1023.0 / 256);\n-\tactiveState.awb.gains.automatic.blue = std::clamp(blueGain, 0.0, 1023.0 / 256);\n+\tactiveState.awb.gains.automatic.red = redGain;\n+\tactiveState.awb.gains.automatic.blue = blueGain;\n \tactiveState.awb.gains.automatic.green = 1.0;\n \n \tLOG(RkISP1Awb, Debug) << std::showpoint\n","prefixes":["libcamera-devel","v5","31/33"]}