[{"id":20346,"web_url":"https://patchwork.libcamera.org/comment/20346/","msgid":"<YXDPyOvG/9DqpPbs@pendragon.ideasonboard.com>","date":"2021-10-21T02:26:16","subject":"Re: [libcamera-devel] [PATCH v2 08/13] 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":"Hi Jean-Michel,\n\nThank you for the patch.\n\nOn Wed, Oct 20, 2021 at 05:46:02PM +0200, Jean-Michel Hautbois wrote:\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> While at it, correct a bug which makes the algorithm not use the\n> filtered result from filterExposure().\n\nThis is still two changes in the same patch, I would have split it in\ntwo.\n\n> Signed-off-by: Jean-Michel Hautbois <jeanmichel.hautbois@ideasonboard.com>\n\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n\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 1bae1eb9..84e41834 100644\n> --- a/src/ipa/ipu3/algorithms/agc.cpp\n> +++ b/src/ipa/ipu3/algorithms/agc.cpp\n> @@ -146,7 +146,9 @@ void Agc::lockExposureGain(uint32_t &exposure, double &gain)\n>  \t\t\t\t    << \" Gain \" << gain;\n>  \n>  \t\tcurrentExposure_ = currentExposureNoDg_ * newGain;\n> +\t\tutils::Duration minShutterSpeed = minExposureLines_ * lineDuration_;\n>  \t\tutils::Duration maxShutterSpeed = maxExposureLines_ * lineDuration_;\n> +\n>  \t\tutils::Duration maxTotalExposure = maxShutterSpeed * kMaxGain;\n>  \t\tcurrentExposure_ = std::min(currentExposure_, maxTotalExposure);\n>  \t\tLOG(IPU3Agc, Debug) << \"Target total exposure \" << currentExposure_\n> @@ -155,23 +157,23 @@ void Agc::lockExposureGain(uint32_t &exposure, double &gain)\n>  \t\t/* \\todo: estimate if we need to desaturate */\n>  \t\tfilterExposure();\n>  \n> -\t\tutils::Duration newExposure = 0.0s;\n> -\t\tif (currentShutter < maxShutterSpeed) {\n> -\t\t\texposure = std::clamp<uint32_t>(exposure * currentExposure_ / currentExposureNoDg_,\n> -\t\t\t\t\t\t\tminExposureLines_,\n> -\t\t\t\t\t\t\tmaxExposureLines_);\n> -\t\t\tnewExposure = currentExposure_ / exposure;\n> -\t\t\tgain = std::clamp(gain * currentExposure_ / newExposure,\n> -\t\t\t\t\t  kMinGain, kMaxGain);\n> -\t\t} else if (currentShutter >= maxShutterSpeed) {\n> -\t\t\tgain = std::clamp(gain * currentExposure_ / currentExposureNoDg_,\n> -\t\t\t\t\t  kMinGain, kMaxGain);\n> -\t\t\tnewExposure = currentExposure_ / gain;\n> -\t\t\texposure = std::clamp<uint32_t>(exposure * currentExposure_ / newExposure,\n> -\t\t\t\t\t\t\tminExposureLines_,\n> -\t\t\t\t\t\t\tmaxExposureLines_);\n> -\t\t}\n> -\t\tLOG(IPU3Agc, Debug) << \"Adjust exposure \" << exposure * lineDuration_ << \" and gain \" << gain;\n> +\t\tutils::Duration exposureValue = filteredExposure_;\n> +\t\tutils::Duration shutterTime = minShutterSpeed;\n> +\n> +\t\t/*\n> +\t\t * Push the shutter time up to the maximum first, and only then\n> +\t\t * increase the gain.\n> +\t\t */\n> +\t\tshutterTime = std::clamp<utils::Duration>(exposureValue / kMinGain,\n> +\t\t\t\t\t\t\t  minShutterSpeed, maxShutterSpeed);\n> +\t\tdouble stepGain = std::clamp(exposureValue / shutterTime,\n> +\t\t\t\t\t     kMinGain, kMaxGain);\n> +\t\tLOG(IPU3Agc, Debug) << \"Divided up shutter and gain are \"\n> +\t\t\t\t    << shutterTime << \" and \"\n> +\t\t\t\t    << stepGain;\n> +\n> +\t\texposure = shutterTime / lineDuration_;\n> +\t\tgain = stepGain;\n>  \t}\n>  \tlastFrame_ = 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 09A12BDB1C\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu, 21 Oct 2021 02:26:38 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 5342068F58;\n\tThu, 21 Oct 2021 04:26:37 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 2DB2860124\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 21 Oct 2021 04:26:36 +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 A46DA2BA;\n\tThu, 21 Oct 2021 04:26:35 +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=\"OCsuR3/c\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1634783195;\n\tbh=JEhcbUweojJSclOFuWJYeJozTmH00GE2fPh7GrfoysM=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=OCsuR3/czwy7/4r54GPOHiIKnJ1AOK/4Gs+K3lax6wHQ7yFq5OUhJ0pzSBFcHlBuc\n\tPP6Dh+xCr/UftgtLGRG6As9JvqaVfJXcdYS0CzeV9yUQ7mFAsFsFamd6suloGmcsGb\n\thas++2NW9s1j1aQZ+abJ8gRXf30AlM8eyMbAptXo=","Date":"Thu, 21 Oct 2021 05:26:16 +0300","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Jean-Michel Hautbois <jeanmichel.hautbois@ideasonboard.com>","Message-ID":"<YXDPyOvG/9DqpPbs@pendragon.ideasonboard.com>","References":"<20211020154607.180161-1-jeanmichel.hautbois@ideasonboard.com>\n\t<20211020154607.180161-9-jeanmichel.hautbois@ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20211020154607.180161-9-jeanmichel.hautbois@ideasonboard.com>","Subject":"Re: [libcamera-devel] [PATCH v2 08/13] 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>"}}]