[{"id":33830,"web_url":"https://patchwork.libcamera.org/comment/33830/","msgid":"<20250401012131.GA22217@pendragon.ideasonboard.com>","date":"2025-04-01T01:21:31","subject":"Re: [PATCH v2 17/17] ipa: rkisp1: awb: Avoid division by zero","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Stefan,\n\nThank you for the patch.\n\nOn Wed, Mar 19, 2025 at 05:11:22PM +0100, Stefan Klug wrote:\n> As the gains can also be specified manually, the regulation can run into\n> numeric instabilities by dividing by near zero. Mitigate that by\n> applying a small minium value.\n> \n> Signed-off-by: Stefan Klug <stefan.klug@ideasonboard.com>\n> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n> \n> ---\n> \n> Changes in v2:\n> - Collected tag\n> ---\n>  src/ipa/rkisp1/algorithms/awb.cpp | 4 ++--\n>  1 file changed, 2 insertions(+), 2 deletions(-)\n> \n> diff --git a/src/ipa/rkisp1/algorithms/awb.cpp b/src/ipa/rkisp1/algorithms/awb.cpp\n> index e8b04d03748d..eb4545439ae3 100644\n> --- a/src/ipa/rkisp1/algorithms/awb.cpp\n> +++ b/src/ipa/rkisp1/algorithms/awb.cpp\n> @@ -421,9 +421,9 @@ RGB<double> Awb::calculateRgbMeans(const IPAFrameContext &frameContext, const rk\n>  \t/*\n>  \t * The ISP computes the AWB means after applying the colour gains,\n>  \t * divide by the gains that were used to get the raw means from the\n> -\t * sensor.\n> +\t * sensor. Apply a minimum value to avoid divisions by near-zero.\n\nShouldn't we instead prevent the user from setting zero (or near-zero)\ngains ? \n\n>  \t */\n> -\trgbMeans /= frameContext.awb.gains;\n> +\trgbMeans /= frameContext.awb.gains.max(0.01);\n>  \n>  \treturn rgbMeans;\n>  }","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 C6E12C323E\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue,  1 Apr 2025 01:21:58 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 2B0286897A;\n\tTue,  1 Apr 2025 03:21:58 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id A880062C66\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue,  1 Apr 2025 03:21:56 +0200 (CEST)","from pendragon.ideasonboard.com (85-76-147-224-nat.elisa-mobile.fi\n\t[85.76.147.224])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id A86EE6F9;\n\tTue,  1 Apr 2025 03:20:04 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"ML2vX0wp\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1743470404;\n\tbh=SvTYW5B/R3T3HUA+eDItJB4x94GKdQUyzVXSLr1IP2A=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=ML2vX0wpocWW/iFftSYYabiExFhXm95NHI/WJfQobSgzBCCryY5eqNs8lb8Bjlt0n\n\t9EFnaJhdBHhjkyjIyHc/wxPui1ZWUvMmczElABlvIFjCwU2axGhuax+4IE8liQaCmm\n\tyYL3BfP6+LCLjO1pZWWeJ8tIbgVgxt+s98s76zR8=","Date":"Tue, 1 Apr 2025 04:21:31 +0300","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Stefan Klug <stefan.klug@ideasonboard.com>","Cc":"libcamera-devel@lists.libcamera.org,\n\tKieran Bingham <kieran.bingham@ideasonboard.com>","Subject":"Re: [PATCH v2 17/17] ipa: rkisp1: awb: Avoid division by zero","Message-ID":"<20250401012131.GA22217@pendragon.ideasonboard.com>","References":"<20250319161152.63625-1-stefan.klug@ideasonboard.com>\n\t<20250319161152.63625-18-stefan.klug@ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20250319161152.63625-18-stefan.klug@ideasonboard.com>","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>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}},{"id":33894,"web_url":"https://patchwork.libcamera.org/comment/33894/","msgid":"<lqt2ywdqbt4inbjk7h4suedpugli5nr73hbpxtz5q22rvmvifo@2eyqfqzqk5eo>","date":"2025-04-02T15:26:55","subject":"Re: [PATCH v2 17/17] ipa: rkisp1: awb: Avoid division by zero","submitter":{"id":184,"url":"https://patchwork.libcamera.org/api/people/184/","name":"Stefan Klug","email":"stefan.klug@ideasonboard.com"},"content":"Hi Laurent,\n\nThank you for the review. \n\nOn Tue, Apr 01, 2025 at 04:21:31AM +0300, Laurent Pinchart wrote:\n> Hi Stefan,\n> \n> Thank you for the patch.\n> \n> On Wed, Mar 19, 2025 at 05:11:22PM +0100, Stefan Klug wrote:\n> > As the gains can also be specified manually, the regulation can run into\n> > numeric instabilities by dividing by near zero. Mitigate that by\n> > applying a small minium value.\n> > \n> > Signed-off-by: Stefan Klug <stefan.klug@ideasonboard.com>\n> > Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n> > \n> > ---\n> > \n> > Changes in v2:\n> > - Collected tag\n> > ---\n> >  src/ipa/rkisp1/algorithms/awb.cpp | 4 ++--\n> >  1 file changed, 2 insertions(+), 2 deletions(-)\n> > \n> > diff --git a/src/ipa/rkisp1/algorithms/awb.cpp b/src/ipa/rkisp1/algorithms/awb.cpp\n> > index e8b04d03748d..eb4545439ae3 100644\n> > --- a/src/ipa/rkisp1/algorithms/awb.cpp\n> > +++ b/src/ipa/rkisp1/algorithms/awb.cpp\n> > @@ -421,9 +421,9 @@ RGB<double> Awb::calculateRgbMeans(const IPAFrameContext &frameContext, const rk\n> >  \t/*\n> >  \t * The ISP computes the AWB means after applying the colour gains,\n> >  \t * divide by the gains that were used to get the raw means from the\n> > -\t * sensor.\n> > +\t * sensor. Apply a minimum value to avoid divisions by near-zero.\n> \n> Shouldn't we instead prevent the user from setting zero (or near-zero)\n> gains ? \n\nThat would also work, but afaik we have no mechanism to check if\ncontrols adhere to the range provided in the ControlInfo. And there are\nmultiple code paths feeding into framContext.awb.gains. So I think it\nis easier to move the check to where it might do harm instead of\nensuring every code path has similar checks.\n\nBest regards,\nStefan\n\n> \n> >  \t */\n> > -\trgbMeans /= frameContext.awb.gains;\n> > +\trgbMeans /= frameContext.awb.gains.max(0.01);\n> >  \n> >  \treturn rgbMeans;\n> >  }\n> \n> -- \n> Regards,\n> \n> Laurent Pinchart","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 BB827C3213\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed,  2 Apr 2025 15:27:01 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id EC95C6898E;\n\tWed,  2 Apr 2025 17:27:00 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 3D6D768979\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed,  2 Apr 2025 17:26:59 +0200 (CEST)","from ideasonboard.com (unknown\n\t[IPv6:2a00:6020:448c:6c00:3e5a:57a6:9731:f378])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 3281C415;\n\tWed,  2 Apr 2025 17:25:06 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"XzKwe3Py\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1743607506;\n\tbh=TgSszkJC1lH6E54Q1cHpO+RT3X+tYGy1B0LTrIHaGxw=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=XzKwe3PyNeO+hteBbMlluVifPV5rX1fBfJZzrJ+WpXZAUq8dxbp2DlNAEVJ4u8ilz\n\t5YPleJwzIdAXH9Fpwciyym7kPtrO49w07JUa1wb9zeZBqO/Zt6sHSdxNIO9Et4RSaI\n\tkvzE9TzFJPldvsETdEDLrfWmG5fSz570GDiJo4vg=","Date":"Wed, 2 Apr 2025 17:26:55 +0200","From":"Stefan Klug <stefan.klug@ideasonboard.com>","To":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","Cc":"libcamera-devel@lists.libcamera.org, \n\tKieran Bingham <kieran.bingham@ideasonboard.com>","Subject":"Re: [PATCH v2 17/17] ipa: rkisp1: awb: Avoid division by zero","Message-ID":"<lqt2ywdqbt4inbjk7h4suedpugli5nr73hbpxtz5q22rvmvifo@2eyqfqzqk5eo>","References":"<20250319161152.63625-1-stefan.klug@ideasonboard.com>\n\t<20250319161152.63625-18-stefan.klug@ideasonboard.com>\n\t<20250401012131.GA22217@pendragon.ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20250401012131.GA22217@pendragon.ideasonboard.com>","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>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}},{"id":33895,"web_url":"https://patchwork.libcamera.org/comment/33895/","msgid":"<20250402155202.GG13762@pendragon.ideasonboard.com>","date":"2025-04-02T15:52:02","subject":"Re: [PATCH v2 17/17] ipa: rkisp1: awb: Avoid division by zero","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"On Wed, Apr 02, 2025 at 05:26:55PM +0200, Stefan Klug wrote:\n> On Tue, Apr 01, 2025 at 04:21:31AM +0300, Laurent Pinchart wrote:\n> > On Wed, Mar 19, 2025 at 05:11:22PM +0100, Stefan Klug wrote:\n> > > As the gains can also be specified manually, the regulation can run into\n> > > numeric instabilities by dividing by near zero. Mitigate that by\n> > > applying a small minium value.\n> > > \n> > > Signed-off-by: Stefan Klug <stefan.klug@ideasonboard.com>\n> > > Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n> > > \n> > > ---\n> > > \n> > > Changes in v2:\n> > > - Collected tag\n> > > ---\n> > >  src/ipa/rkisp1/algorithms/awb.cpp | 4 ++--\n> > >  1 file changed, 2 insertions(+), 2 deletions(-)\n> > > \n> > > diff --git a/src/ipa/rkisp1/algorithms/awb.cpp b/src/ipa/rkisp1/algorithms/awb.cpp\n> > > index e8b04d03748d..eb4545439ae3 100644\n> > > --- a/src/ipa/rkisp1/algorithms/awb.cpp\n> > > +++ b/src/ipa/rkisp1/algorithms/awb.cpp\n> > > @@ -421,9 +421,9 @@ RGB<double> Awb::calculateRgbMeans(const IPAFrameContext &frameContext, const rk\n> > >  \t/*\n> > >  \t * The ISP computes the AWB means after applying the colour gains,\n> > >  \t * divide by the gains that were used to get the raw means from the\n> > > -\t * sensor.\n> > > +\t * sensor. Apply a minimum value to avoid divisions by near-zero.\n> > \n> > Shouldn't we instead prevent the user from setting zero (or near-zero)\n> > gains ? \n> \n> That would also work, but afaik we have no mechanism to check if\n> controls adhere to the range provided in the ControlInfo. And there are\n> multiple code paths feeding into framContext.awb.gains. So I think it\n> is easier to move the check to where it might do harm instead of\n> ensuring every code path has similar checks.\n\nAs a short term fix that's fine with me, but I think we should still\naddress the overall issue, and clamp/adjust control values at queue\nrequest time. That's the entry point for controls, if we handle it\nthere, it should be safe everywhere else in the IPA module.\n\n> > >  \t */\n> > > -\trgbMeans /= frameContext.awb.gains;\n> > > +\trgbMeans /= frameContext.awb.gains.max(0.01);\n> > >  \n> > >  \treturn rgbMeans;\n> > >  }","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 0FE1FC3213\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed,  2 Apr 2025 15:52:28 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 450986898E;\n\tWed,  2 Apr 2025 17:52:27 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 439E168979\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed,  2 Apr 2025 17:52:26 +0200 (CEST)","from pendragon.ideasonboard.com (81-175-209-231.bb.dnainternet.fi\n\t[81.175.209.231])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 3BB7C63B;\n\tWed,  2 Apr 2025 17:50:33 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"vdTo8+/+\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1743609033;\n\tbh=CMAQ5lczV9XKrPIPzc9og8wrfl+6QhoG3HYweOeCmTw=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=vdTo8+/+Jv5wsOGNkGmK7ykVtvthhJHDoV1mVqMXkhC9dGQM1NekskQMBVDHVyrXm\n\tb2NWck3Kut4AnrDmUy2EYbueG/O3XvmR0Msq8dsGzgdVEeaHs7+hfA1IlenTbGuQBl\n\tt+jKtpXstu6eVLcsm8g0pEPWO33LbZc6gR2IEPsc=","Date":"Wed, 2 Apr 2025 18:52:02 +0300","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Stefan Klug <stefan.klug@ideasonboard.com>","Cc":"libcamera-devel@lists.libcamera.org,\n\tKieran Bingham <kieran.bingham@ideasonboard.com>","Subject":"Re: [PATCH v2 17/17] ipa: rkisp1: awb: Avoid division by zero","Message-ID":"<20250402155202.GG13762@pendragon.ideasonboard.com>","References":"<20250319161152.63625-1-stefan.klug@ideasonboard.com>\n\t<20250319161152.63625-18-stefan.klug@ideasonboard.com>\n\t<20250401012131.GA22217@pendragon.ideasonboard.com>\n\t<lqt2ywdqbt4inbjk7h4suedpugli5nr73hbpxtz5q22rvmvifo@2eyqfqzqk5eo>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<lqt2ywdqbt4inbjk7h4suedpugli5nr73hbpxtz5q22rvmvifo@2eyqfqzqk5eo>","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>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]