[{"id":20366,"web_url":"https://patchwork.libcamera.org/comment/20366/","msgid":"<163485051772.4046931.13551428497699286330@Monstersaurus>","date":"2021-10-21T21:08:37","subject":"Re: [libcamera-devel] [PATCH v3 09/14] ipa: ipu3: agc: Simplify\n\tdivision of exposure/gain","submitter":{"id":4,"url":"https://patchwork.libcamera.org/api/people/4/","name":"Kieran Bingham","email":"kieran.bingham@ideasonboard.com"},"content":"Quoting Jean-Michel Hautbois (2021-10-21 17:43:56)\n> Until now, the algorithm makes complex assumptions when dividing the\n> exposure and analogue gain values. Instead, use a simpler clamping of\n> the shutter speed first, and then of the analogue gain, based on the\n> limits configured.\n> \n> Signed-off-by: Jean-Michel Hautbois <jeanmichel.hautbois@ideasonboard.com>\n> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n> ---\n>  src/ipa/ipu3/algorithms/agc.cpp | 36 +++++++++++++++++----------------\n>  1 file changed, 19 insertions(+), 17 deletions(-)\n> \n> diff --git a/src/ipa/ipu3/algorithms/agc.cpp b/src/ipa/ipu3/algorithms/agc.cpp\n> index f5bb3328..6f5b6a45 100644\n> --- a/src/ipa/ipu3/algorithms/agc.cpp\n> +++ b/src/ipa/ipu3/algorithms/agc.cpp\n> @@ -147,7 +147,9 @@ void Agc::lockExposureGain(uint32_t &exposure, double &gain)\n>                                     << \" Gain \" << gain;\n>  \n>                 currentExposure_ = currentExposureNoDg_ * newGain;\n> +               utils::Duration minShutterSpeed = minExposureLines_ * lineDuration_;\n>                 utils::Duration maxShutterSpeed = maxExposureLines_ * lineDuration_;\n> +\n>                 utils::Duration maxTotalExposure = maxShutterSpeed * kMaxGain;\n>                 currentExposure_ = std::min(currentExposure_, maxTotalExposure);\n>                 LOG(IPU3Agc, Debug) << \"Target total exposure \" << currentExposure_\n> @@ -156,23 +158,23 @@ void Agc::lockExposureGain(uint32_t &exposure, double &gain)\n>                 /* \\todo: estimate if we need to desaturate */\n>                 filterExposure();\n>  \n> -               utils::Duration newExposure = 0.0s;\n> -               if (currentShutter < maxShutterSpeed) {\n> -                       exposure = std::clamp<uint32_t>(exposure * filteredExposure_ / currentExposureNoDg_,\n> -                                                       minExposureLines_,\n> -                                                       maxExposureLines_);\n> -                       newExposure = filteredExposure_ / exposure;\n> -                       gain = std::clamp(gain * filteredExposure_ / newExposure,\n> -                                         kMinGain, kMaxGain);\n> -               } else {\n> -                       gain = std::clamp(gain * filteredExposure_ / currentExposureNoDg_,\n> -                                         kMinGain, kMaxGain);\n> -                       newExposure = filteredExposure_ / gain;\n> -                       exposure = std::clamp<uint32_t>(exposure * filteredExposure_ / newExposure,\n\nMakes the previous patch a bit redundant, but at least we removed code\nthat was more 'correct' ;-)\n\n> -                                                       minExposureLines_,\n> -                                                       maxExposureLines_);\n> -               }\n> -               LOG(IPU3Agc, Debug) << \"Adjust exposure \" << exposure * lineDuration_ << \" and gain \" << gain;\n> +               utils::Duration exposureValue = filteredExposure_;\n> +               utils::Duration shutterTime = minShutterSpeed;\n> +\n> +               /*\n> +                * Push the shutter time up to the maximum first, and only then\n> +                * increase the gain.\n> +                */\n> +               shutterTime = std::clamp<utils::Duration>(exposureValue / kMinGain,\n> +                                                         minShutterSpeed, maxShutterSpeed);\n> +               double stepGain = std::clamp(exposureValue / shutterTime,\n> +                                            kMinGain, kMaxGain);\n> +               LOG(IPU3Agc, Debug) << \"Divided up shutter and gain are \"\n> +                                   << shutterTime << \" and \"\n> +                                   << stepGain;\n> +\n> +               exposure = shutterTime / lineDuration_;\n> +               gain = stepGain;\n\nThis looks a lot simpler indeed. I haven't gone through and fully\nvalidated all the equations, but as I believe I've seen this working,\nlocally on my device, I think I can trust that it's functional.\n\nReviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n\n>         }\n>         lastFrame_ = frameCount_;\n>  }\n> -- \n> 2.32.0\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 2C5CEBF415\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu, 21 Oct 2021 21:08:43 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 6D38868F5B;\n\tThu, 21 Oct 2021 23:08:42 +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 10C58604FE\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 21 Oct 2021 23:08:41 +0200 (CEST)","from pendragon.ideasonboard.com\n\t(cpc89244-aztw30-2-0-cust3082.18-1.cable.virginm.net [86.31.172.11])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 9A3ED8B6;\n\tThu, 21 Oct 2021 23:08:40 +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=\"acP6LlEp\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1634850520;\n\tbh=F/+dWZy/MWlPsL+t/nzW3nlh7zA3SPW+xtOshlcuzuI=;\n\th=In-Reply-To:References:Subject:From:To:Date:From;\n\tb=acP6LlEpSQwX4ioN+juXDECVG36SgCEOAueu2pzvkom9NTp/HpkIJL5VJpudEUmM1\n\t7rwUI4oFnR4Lkg112F18lmO1Dp2zfP7sCRAtVKpulHrorPo0vZDY707qnvzAoHGz4O\n\tf4aFEYLgh1qJ1bLht8RURrv2inEGU+QkO+SE5O7s=","Content-Type":"text/plain; charset=\"utf-8\"","MIME-Version":"1.0","Content-Transfer-Encoding":"quoted-printable","In-Reply-To":"<20211021164401.110033-10-jeanmichel.hautbois@ideasonboard.com>","References":"<20211021164401.110033-1-jeanmichel.hautbois@ideasonboard.com>\n\t<20211021164401.110033-10-jeanmichel.hautbois@ideasonboard.com>","From":"Kieran Bingham <kieran.bingham@ideasonboard.com>","To":"Jean-Michel Hautbois <jeanmichel.hautbois@ideasonboard.com>,\n\tlibcamera-devel@lists.libcamera.org","Date":"Thu, 21 Oct 2021 22:08:37 +0100","Message-ID":"<163485051772.4046931.13551428497699286330@Monstersaurus>","User-Agent":"alot/0.9.1","Subject":"Re: [libcamera-devel] [PATCH v3 09/14] ipa: ipu3: agc: Simplify\n\tdivision of exposure/gain","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":20371,"web_url":"https://patchwork.libcamera.org/comment/20371/","msgid":"<YXI5IeMuoB9M9QS5@pendragon.ideasonboard.com>","date":"2021-10-22T04:08:01","subject":"Re: [libcamera-devel] [PATCH v3 09/14] ipa: ipu3: agc: Simplify\n\tdivision of exposure/gain","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"On Thu, Oct 21, 2021 at 10:08:37PM +0100, Kieran Bingham wrote:\n> Quoting Jean-Michel Hautbois (2021-10-21 17:43:56)\n> > Until now, the algorithm makes complex assumptions when dividing the\n> > exposure and analogue gain values. Instead, use a simpler clamping of\n> > the shutter speed first, and then of the analogue gain, based on the\n> > limits configured.\n> > \n> > Signed-off-by: Jean-Michel Hautbois <jeanmichel.hautbois@ideasonboard.com>\n> > Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n> > ---\n> >  src/ipa/ipu3/algorithms/agc.cpp | 36 +++++++++++++++++----------------\n> >  1 file changed, 19 insertions(+), 17 deletions(-)\n> > \n> > diff --git a/src/ipa/ipu3/algorithms/agc.cpp b/src/ipa/ipu3/algorithms/agc.cpp\n> > index f5bb3328..6f5b6a45 100644\n> > --- a/src/ipa/ipu3/algorithms/agc.cpp\n> > +++ b/src/ipa/ipu3/algorithms/agc.cpp\n> > @@ -147,7 +147,9 @@ void Agc::lockExposureGain(uint32_t &exposure, double &gain)\n> >                                     << \" Gain \" << gain;\n> >  \n> >                 currentExposure_ = currentExposureNoDg_ * newGain;\n> > +               utils::Duration minShutterSpeed = minExposureLines_ * lineDuration_;\n> >                 utils::Duration maxShutterSpeed = maxExposureLines_ * lineDuration_;\n> > +\n> >                 utils::Duration maxTotalExposure = maxShutterSpeed * kMaxGain;\n> >                 currentExposure_ = std::min(currentExposure_, maxTotalExposure);\n> >                 LOG(IPU3Agc, Debug) << \"Target total exposure \" << currentExposure_\n> > @@ -156,23 +158,23 @@ void Agc::lockExposureGain(uint32_t &exposure, double &gain)\n> >                 /* \\todo: estimate if we need to desaturate */\n> >                 filterExposure();\n> >  \n> > -               utils::Duration newExposure = 0.0s;\n> > -               if (currentShutter < maxShutterSpeed) {\n> > -                       exposure = std::clamp<uint32_t>(exposure * filteredExposure_ / currentExposureNoDg_,\n> > -                                                       minExposureLines_,\n> > -                                                       maxExposureLines_);\n> > -                       newExposure = filteredExposure_ / exposure;\n> > -                       gain = std::clamp(gain * filteredExposure_ / newExposure,\n> > -                                         kMinGain, kMaxGain);\n> > -               } else {\n> > -                       gain = std::clamp(gain * filteredExposure_ / currentExposureNoDg_,\n> > -                                         kMinGain, kMaxGain);\n> > -                       newExposure = filteredExposure_ / gain;\n> > -                       exposure = std::clamp<uint32_t>(exposure * filteredExposure_ / newExposure,\n> \n> Makes the previous patch a bit redundant, but at least we removed code\n> that was more 'correct' ;-)\n\nThe two used to be combined in a single patch, and I've pointed out\ntwice that they were separate changes :-) The switch to\nfilteredExposure_ was easy to overlook in previous versions.\n\n> > -                                                       minExposureLines_,\n> > -                                                       maxExposureLines_);\n> > -               }\n> > -               LOG(IPU3Agc, Debug) << \"Adjust exposure \" << exposure * lineDuration_ << \" and gain \" << gain;\n> > +               utils::Duration exposureValue = filteredExposure_;\n> > +               utils::Duration shutterTime = minShutterSpeed;\n> > +\n> > +               /*\n> > +                * Push the shutter time up to the maximum first, and only then\n> > +                * increase the gain.\n> > +                */\n> > +               shutterTime = std::clamp<utils::Duration>(exposureValue / kMinGain,\n> > +                                                         minShutterSpeed, maxShutterSpeed);\n> > +               double stepGain = std::clamp(exposureValue / shutterTime,\n> > +                                            kMinGain, kMaxGain);\n> > +               LOG(IPU3Agc, Debug) << \"Divided up shutter and gain are \"\n> > +                                   << shutterTime << \" and \"\n> > +                                   << stepGain;\n> > +\n> > +               exposure = shutterTime / lineDuration_;\n> > +               gain = stepGain;\n> \n> This looks a lot simpler indeed. I haven't gone through and fully\n> validated all the equations, but as I believe I've seen this working,\n> locally on my device, I think I can trust that it's functional.\n> \n> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n> \n> >         }\n> >         lastFrame_ = frameCount_;\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 C1B8ABDB1C\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri, 22 Oct 2021 04:08:28 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 0E94168F5B;\n\tFri, 22 Oct 2021 06:08:28 +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 CB1BC60123\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 22 Oct 2021 06:08:26 +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 4799251D;\n\tFri, 22 Oct 2021 06:08:26 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"Bcj2iYhU\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1634875706;\n\tbh=sO8hH8LMhMhu73TWq8ppgT0nV7s/3jmiU+tSqll8q0s=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=Bcj2iYhUIoYvpfj8j27+MflR0HxWG9YGqv+iDIsksbXox4fCkBvYJcVpDIKBJHU+J\n\tzmNtTc6esHYW/gJVzkDsttwEwn/KzCBiN5wzTftmVbm6Z67QfeFc2yfe1XahdUIz2d\n\tIr4zAP9UxblGzvOs8ldRfu5bhm+GhhZsSn0ARMjc=","Date":"Fri, 22 Oct 2021 07:08:01 +0300","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Kieran Bingham <kieran.bingham@ideasonboard.com>","Message-ID":"<YXI5IeMuoB9M9QS5@pendragon.ideasonboard.com>","References":"<20211021164401.110033-1-jeanmichel.hautbois@ideasonboard.com>\n\t<20211021164401.110033-10-jeanmichel.hautbois@ideasonboard.com>\n\t<163485051772.4046931.13551428497699286330@Monstersaurus>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<163485051772.4046931.13551428497699286330@Monstersaurus>","Subject":"Re: [libcamera-devel] [PATCH v3 09/14] ipa: ipu3: agc: Simplify\n\tdivision of exposure/gain","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@lists.libcamera.org","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]