[{"id":28763,"web_url":"https://patchwork.libcamera.org/comment/28763/","msgid":"<20240227145847.GA28713@pendragon.ideasonboard.com>","date":"2024-02-27T14:58:47","subject":"Re: [PATCH 3/3] libipa: camera_sensor_helper: Fix rounding of\n\tgainCode","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Kieran,\n\nThank you for the patch.\n\nOn Fri, Feb 23, 2024 at 03:59:54PM +0000, Kieran Bingham wrote:\n> The implementation of gainCode for both Exponential and Linear gain\n\ns/gainCode/gainCode()/\n\n> models does not generate a gainCode that matches the result of the\n> reverse operation.\n> \n> This can be seen by converting sequential gainCodes to a gain\n> and converting that back to the gainCode. The values do not\n> translate back due to rounding errors.\n\nIt's not due to rounding errors, but to the fact that we only round\ndown, isn't it ?\n\n> Correct the rounding error and ensure that gainCode translation\n> produces accurate bi-directional conversions from the perspective\n> of the gainCode.\n\nEven rounding to the closest integer (which I think is a good idea), are\nyou sure you'll guarantee that gainCode(gain(code)) will always be equal\nto code, in *all* cases ?\n\n> This fixes the IMX290, IMX296, IMX327 and IMX335 which use the\n> Exponential gain model helpers, as well as IMX219 IMX258 and IMX477\n\nMissing commas.\n\n> which use the Linear gain model helpers.\n> \n> Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n> ---\n>  src/ipa/libipa/camera_sensor_helper.cpp | 6 +++---\n>  1 file changed, 3 insertions(+), 3 deletions(-)\n> \n> diff --git a/src/ipa/libipa/camera_sensor_helper.cpp b/src/ipa/libipa/camera_sensor_helper.cpp\n> index a925b37b9f7c..39e10d86a2c7 100644\n> --- a/src/ipa/libipa/camera_sensor_helper.cpp\n> +++ b/src/ipa/libipa/camera_sensor_helper.cpp\n> @@ -64,13 +64,13 @@ uint32_t CameraSensorHelper::gainCode(double gain) const\n>  \tcase AnalogueGainLinear:\n>  \t\tASSERT(k.linear.m0 == 0 || k.linear.m1 == 0);\n>  \n> -\t\treturn (k.linear.c0 - k.linear.c1 * gain) /\n> -\t\t       (k.linear.m1 * gain - k.linear.m0);\n> +\t\treturn std::round((k.linear.c0 - k.linear.c1 * gain) /\n> +\t\t\t\t  (k.linear.m1 * gain - k.linear.m0));\n>  \n>  \tcase AnalogueGainExponential:\n>  \t\tASSERT(k.exp.a != 0 && k.exp.m != 0);\n>  \n> -\t\treturn std::log2(gain / k.exp.a) / k.exp.m;\n> +\t\treturn std::round(std::log2(gain / k.exp.a) / k.exp.m);\n\nThe code change seems fine, but the commit message may need to be\nreworked.\n\n>  \n>  \tdefault:\n>  \t\tASSERT(false);","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 ADE25BD80A\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue, 27 Feb 2024 14:58:47 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 1299B62867;\n\tTue, 27 Feb 2024 15:58:47 +0100 (CET)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id EA04C62806\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 27 Feb 2024 15:58:45 +0100 (CET)","from pendragon.ideasonboard.com (89-27-53-110.bb.dnainternet.fi\n\t[89.27.53.110])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 7BB598C;\n\tTue, 27 Feb 2024 15:58:33 +0100 (CET)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"mOeqJsSy\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1709045913;\n\tbh=+2I6xjTl/a5ymiB8OW6TWx/q8O6Jtlr0UT7O9peRXuQ=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=mOeqJsSyLXvwJW0PCKko9Z4QC4pCAR2352yubOd/nVYPv1tDqAxlTIfjIraa3tYZd\n\thmQW5mV9FNi6kx6PRstEcyqVwgQspWV2exJeqrpW/pHp/ImOvqxUsenmNV/T071ZC2\n\tHHwKewVQXb5A9OHU3IytGWvFIqZuCBmaZv3o3KnQ=","Date":"Tue, 27 Feb 2024 16:58:47 +0200","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Kieran Bingham <kieran.bingham@ideasonboard.com>","Subject":"Re: [PATCH 3/3] libipa: camera_sensor_helper: Fix rounding of\n\tgainCode","Message-ID":"<20240227145847.GA28713@pendragon.ideasonboard.com>","References":"<20240223155954.4139705-1-kieran.bingham@ideasonboard.com>\n\t<20240223155954.4139705-4-kieran.bingham@ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20240223155954.4139705-4-kieran.bingham@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>","Cc":"libcamera devel <libcamera-devel@lists.libcamera.org>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}},{"id":28770,"web_url":"https://patchwork.libcamera.org/comment/28770/","msgid":"<170905427190.1406778.15793521243452442799@ping.linuxembedded.co.uk>","date":"2024-02-27T17:17:51","subject":"Re: [PATCH 3/3] libipa: camera_sensor_helper: Fix rounding of\n\tgainCode","submitter":{"id":4,"url":"https://patchwork.libcamera.org/api/people/4/","name":"Kieran Bingham","email":"kieran.bingham@ideasonboard.com"},"content":"Quoting Laurent Pinchart (2024-02-27 14:58:47)\n> Hi Kieran,\n> \n> Thank you for the patch.\n> \n> On Fri, Feb 23, 2024 at 03:59:54PM +0000, Kieran Bingham wrote:\n> > The implementation of gainCode for both Exponential and Linear gain\n> \n> s/gainCode/gainCode()/\n> \n> > models does not generate a gainCode that matches the result of the\n> > reverse operation.\n> > \n> > This can be seen by converting sequential gainCodes to a gain\n> > and converting that back to the gainCode. The values do not\n> > translate back due to rounding errors.\n> \n> It's not due to rounding errors, but to the fact that we only round\n> down, isn't it ?\n\nIsn't that a rounding error? An error when rounding (to integer)? ... :-)\n\n> > Correct the rounding error and ensure that gainCode translation\n> > produces accurate bi-directional conversions from the perspective\n> > of the gainCode.\n> \n> Even rounding to the closest integer (which I think is a good idea), are\n> you sure you'll guarantee that gainCode(gain(code)) will always be equal\n> to code, in *all* cases ?\n\nRun the test with and without the fix...\n\nI haven't done a formal proof here no. But that's also why the test ...\ntests every CameraSensorHelper in more or less brute force.\n\n> > This fixes the IMX290, IMX296, IMX327 and IMX335 which use the\n> > Exponential gain model helpers, as well as IMX219 IMX258 and IMX477\n> \n> Missing commas.\n\nOk.\n\n> \n> > which use the Linear gain model helpers.\n> > \n> > Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n> > ---\n> >  src/ipa/libipa/camera_sensor_helper.cpp | 6 +++---\n> >  1 file changed, 3 insertions(+), 3 deletions(-)\n> > \n> > diff --git a/src/ipa/libipa/camera_sensor_helper.cpp b/src/ipa/libipa/camera_sensor_helper.cpp\n> > index a925b37b9f7c..39e10d86a2c7 100644\n> > --- a/src/ipa/libipa/camera_sensor_helper.cpp\n> > +++ b/src/ipa/libipa/camera_sensor_helper.cpp\n> > @@ -64,13 +64,13 @@ uint32_t CameraSensorHelper::gainCode(double gain) const\n> >       case AnalogueGainLinear:\n> >               ASSERT(k.linear.m0 == 0 || k.linear.m1 == 0);\n> >  \n> > -             return (k.linear.c0 - k.linear.c1 * gain) /\n> > -                    (k.linear.m1 * gain - k.linear.m0);\n> > +             return std::round((k.linear.c0 - k.linear.c1 * gain) /\n> > +                               (k.linear.m1 * gain - k.linear.m0));\n> >  \n> >       case AnalogueGainExponential:\n> >               ASSERT(k.exp.a != 0 && k.exp.m != 0);\n> >  \n> > -             return std::log2(gain / k.exp.a) / k.exp.m;\n> > +             return std::round(std::log2(gain / k.exp.a) / k.exp.m);\n> \n> The code change seems fine, but the commit message may need to be\n> reworked.\n> \n> >  \n> >       default:\n> >               ASSERT(false);\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 9B345BD80A\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue, 27 Feb 2024 17:17:56 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id E562362871;\n\tTue, 27 Feb 2024 18:17:55 +0100 (CET)","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 3078062806\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 27 Feb 2024 18:17:55 +0100 (CET)","from pendragon.ideasonboard.com\n\t(aztw-30-b2-v4wan-166917-cust845.vm26.cable.virginm.net\n\t[82.37.23.78])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id DB1BF8D4;\n\tTue, 27 Feb 2024 18:17:42 +0100 (CET)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"UlUIF/4D\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1709054262;\n\tbh=QMeiW1dSLnMTP1tYHUEdnFHmjUARUU6SIV6ARY6Zaf8=;\n\th=In-Reply-To:References:Subject:From:Cc:To:Date:From;\n\tb=UlUIF/4DwCO7L+r9vJpMDL3bX0iQh/4U0TpirFZQ4dvCmsFf6urCcD64Z13Rp7qnU\n\t5K9FOaW12hqnfgH90BR/45A6216Ju6TCtFnMuVqTCZ3M9uM7GB9LcV38RBYMq0V3FY\n\tPqdJZhaiLJxSYXVqe46Y75PLfMNZMnb3PF2b/1Fk=","Content-Type":"text/plain; charset=\"utf-8\"","MIME-Version":"1.0","Content-Transfer-Encoding":"quoted-printable","In-Reply-To":"<20240227145847.GA28713@pendragon.ideasonboard.com>","References":"<20240223155954.4139705-1-kieran.bingham@ideasonboard.com>\n\t<20240223155954.4139705-4-kieran.bingham@ideasonboard.com>\n\t<20240227145847.GA28713@pendragon.ideasonboard.com>","Subject":"Re: [PATCH 3/3] libipa: camera_sensor_helper: Fix rounding of\n\tgainCode","From":"Kieran Bingham <kieran.bingham@ideasonboard.com>","To":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","Date":"Tue, 27 Feb 2024 17:17:51 +0000","Message-ID":"<170905427190.1406778.15793521243452442799@ping.linuxembedded.co.uk>","User-Agent":"alot/0.10","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>","Cc":"libcamera devel <libcamera-devel@lists.libcamera.org>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]