[{"id":33747,"web_url":"https://patchwork.libcamera.org/comment/33747/","msgid":"<174306475057.1178509.10856128097109127489@ping.linuxembedded.co.uk>","date":"2025-03-27T08:39:10","subject":"Re: [PATCH v2 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":"Hi Paul,\n\nQuoting Paul Elder (2025-03-26 08:00:33)\n> From: Kieran Bingham <kieran.bingham@ideasonboard.com>\n> \n> The implementation of gainCode for both Exponential and Linear gain\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> Correct the rounding error and ensure that gainCode translation\n> produces accurate bi-directional conversions from the perspective\n> of the gainCode.\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> which use the Linear gain model helpers.\n> \n\nThanks for reviving this.\n\nI think this one needs to be thought about carefully though - I recall\nsome discussion with David, where the key/important information I missed\nwhen I worked on this last was that we shouldn't ever round up in these\nequations...\n\nCould you review https://patchwork.libcamera.org/patch/20099/ please?\n\nWhen I started this work - I mis-interpreted the underlying issue as a\nrounding issue - but it's not. And using round() causes different\nproblems.\n\nI made a playground for graphing all of this at \n https://git.uk.ideasonboard.com/kbingham/octave-gains\n\nBut this should be re-implemented based on the feedback from David.\n--\nKieran\n\n\n> Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n> Signed-off-by: Paul Elder <paul.elder@ideasonboard.com>\n> \n> ---\n> Changes in v2:\n> - recover from bitrot\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 7c66cd57d685..5645dd14fda3 100644\n> --- a/src/ipa/libipa/camera_sensor_helper.cpp\n> +++ b/src/ipa/libipa/camera_sensor_helper.cpp\n> @@ -90,12 +90,12 @@ uint32_t CameraSensorHelper::gainCode(double gain) const\n>         if (auto *l = std::get_if<AnalogueGainLinear>(&gain_)) {\n>                 ASSERT(l->m0 == 0 || l->m1 == 0);\n>  \n> -               return (l->c0 - l->c1 * gain) /\n> -                      (l->m1 * gain - l->m0);\n> +               return std::round((l->c0 - l->c1 * gain) /\n> +                                 (l->m1 * gain - l->m0));\n>         } else if (auto *e = std::get_if<AnalogueGainExp>(&gain_)) {\n>                 ASSERT(e->a != 0 && e->m != 0);\n>  \n> -               return std::log2(gain / e->a) / e->m;\n> +               return std::round(std::log2(gain / e->a) / e->m);\n>         } else {\n>                 ASSERT(false);\n>                 return 0;\n> -- \n> 2.47.2\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 319B7C3213\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu, 27 Mar 2025 08:39:16 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 52BC868972;\n\tThu, 27 Mar 2025 09:39:15 +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 4B701614E9\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 27 Mar 2025 09:39:13 +0100 (CET)","from pendragon.ideasonboard.com\n\t(cpc89244-aztw30-2-0-cust6594.18-1.cable.virginm.net [86.31.185.195])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id A4584666;\n\tThu, 27 Mar 2025 09:37:24 +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=\"uaPDA81x\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1743064644;\n\tbh=1SaamxW/SYn6sGgGfwL41thzmLpQzsBK2gFwhtcvFTU=;\n\th=In-Reply-To:References:Subject:From:Cc:To:Date:From;\n\tb=uaPDA81xPMOurUbbBKEnj9miNpz8LgsuywqKaoF1pAEfoeSHUCA7fRzV9/4n+LLwr\n\tY/pkIIENMjHrUoB/1uA4100iGWzV7lGcDhRpJiedlHgH5r0tDDAuNzVEhwilmUDBgN\n\tqCPn8XanWJgFvsCTtuoFOW7XAxD1IOPoaI0fKNus=","Content-Type":"text/plain; charset=\"utf-8\"","MIME-Version":"1.0","Content-Transfer-Encoding":"quoted-printable","In-Reply-To":"<20250326080034.1733385-4-paul.elder@ideasonboard.com>","References":"<20250326080034.1733385-1-paul.elder@ideasonboard.com>\n\t<20250326080034.1733385-4-paul.elder@ideasonboard.com>","Subject":"Re: [PATCH v2 3/3] libipa: camera_sensor_helper: Fix rounding of\n\tgainCode","From":"Kieran Bingham <kieran.bingham@ideasonboard.com>","Cc":"Paul Elder <paul.elder@ideasonboard.com>","To":"Paul Elder <paul.elder@ideasonboard.com>,\n\tlibcamera-devel@lists.libcamera.org","Date":"Thu, 27 Mar 2025 08:39:10 +0000","Message-ID":"<174306475057.1178509.10856128097109127489@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>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}},{"id":33754,"web_url":"https://patchwork.libcamera.org/comment/33754/","msgid":"<Z-Yw-7XTsAQCcQXq@pyrite.rasen.tech>","date":"2025-03-28T05:17:47","subject":"Re: [PATCH v2 3/3] libipa: camera_sensor_helper: Fix rounding of\n\tgainCode","submitter":{"id":17,"url":"https://patchwork.libcamera.org/api/people/17/","name":"Paul Elder","email":"paul.elder@ideasonboard.com"},"content":"Hi Kieran,\n\nOn Thu, Mar 27, 2025 at 08:39:10AM +0000, Kieran Bingham wrote:\n> Hi Paul,\n> \n> Quoting Paul Elder (2025-03-26 08:00:33)\n> > From: Kieran Bingham <kieran.bingham@ideasonboard.com>\n> > \n> > The implementation of gainCode for both Exponential and Linear gain\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> > Correct the rounding error and ensure that gainCode translation\n> > produces accurate bi-directional conversions from the perspective\n> > of the gainCode.\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> > which use the Linear gain model helpers.\n> > \n> \n> Thanks for reviving this.\n> \n> I think this one needs to be thought about carefully though - I recall\n> some discussion with David, where the key/important information I missed\n> when I worked on this last was that we shouldn't ever round up in these\n> equations...\n> \n> Could you review https://patchwork.libcamera.org/patch/20099/ please?\n\nAh, I didn't notice that. I'll take a look.\n\n> \n> When I started this work - I mis-interpreted the underlying issue as a\n> rounding issue - but it's not. And using round() causes different\n> problems.\n> \n> I made a playground for graphing all of this at \n>  https://git.uk.ideasonboard.com/kbingham/octave-gains\n\nOh neat, thanks,\n\n> \n> But this should be re-implemented based on the feedback from David.\n\nack\n\n\nPaul\n\n> \n> \n> > Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n> > Signed-off-by: Paul Elder <paul.elder@ideasonboard.com>\n> > \n> > ---\n> > Changes in v2:\n> > - recover from bitrot\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 7c66cd57d685..5645dd14fda3 100644\n> > --- a/src/ipa/libipa/camera_sensor_helper.cpp\n> > +++ b/src/ipa/libipa/camera_sensor_helper.cpp\n> > @@ -90,12 +90,12 @@ uint32_t CameraSensorHelper::gainCode(double gain) const\n> >         if (auto *l = std::get_if<AnalogueGainLinear>(&gain_)) {\n> >                 ASSERT(l->m0 == 0 || l->m1 == 0);\n> >  \n> > -               return (l->c0 - l->c1 * gain) /\n> > -                      (l->m1 * gain - l->m0);\n> > +               return std::round((l->c0 - l->c1 * gain) /\n> > +                                 (l->m1 * gain - l->m0));\n> >         } else if (auto *e = std::get_if<AnalogueGainExp>(&gain_)) {\n> >                 ASSERT(e->a != 0 && e->m != 0);\n> >  \n> > -               return std::log2(gain / e->a) / e->m;\n> > +               return std::round(std::log2(gain / e->a) / e->m);\n> >         } else {\n> >                 ASSERT(false);\n> >                 return 0;\n> > -- \n> > 2.47.2\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 E7990C3213\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri, 28 Mar 2025 05:17:59 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id F0C3C6897C;\n\tFri, 28 Mar 2025 06:17:58 +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 180146894F\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 28 Mar 2025 06:17:57 +0100 (CET)","from pyrite.rasen.tech (unknown\n\t[IPv6:2404:7a81:160:2100:7586:ac75:4e3b:2836])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id D84A6594;\n\tFri, 28 Mar 2025 06:16:06 +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=\"H5wE8V2T\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1743138967;\n\tbh=uaEL2kI+JtyyKMz2j6Jvyhine08SDYz/4y2n6Rssxdg=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=H5wE8V2TWUSZLlXz2sPlPGhwqj7eX38OzG2XjOb36N8zInQt5sctm5LmDncz3hDXm\n\tg/73wyM4ldLctJdUNCiKwUgT/dZOKmdtPgeRFNOcbsTjmb0bKJ2KhQJCIyup7I/Jn3\n\tnHaF0spE/jfg6OCaAVSoQIDHTSXEDk13zRSeP6kc=","Date":"Fri, 28 Mar 2025 14:17:47 +0900","From":"Paul Elder <paul.elder@ideasonboard.com>","To":"Kieran Bingham <kieran.bingham@ideasonboard.com>","Cc":"libcamera-devel@lists.libcamera.org","Subject":"Re: [PATCH v2 3/3] libipa: camera_sensor_helper: Fix rounding of\n\tgainCode","Message-ID":"<Z-Yw-7XTsAQCcQXq@pyrite.rasen.tech>","References":"<20250326080034.1733385-1-paul.elder@ideasonboard.com>\n\t<20250326080034.1733385-4-paul.elder@ideasonboard.com>\n\t<174306475057.1178509.10856128097109127489@ping.linuxembedded.co.uk>","MIME-Version":"1.0","Content-Type":"text/plain; charset=us-ascii","Content-Disposition":"inline","In-Reply-To":"<174306475057.1178509.10856128097109127489@ping.linuxembedded.co.uk>","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>"}}]