[{"id":20218,"web_url":"https://patchwork.libcamera.org/comment/20218/","msgid":"<YWjVUdQHWtizecVt@pendragon.ideasonboard.com>","date":"2021-10-15T01:11:45","subject":"Re: [libcamera-devel] [PATCH 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 13, 2021 at 05:41:20PM +0200, Jean-Michel Hautbois wrote:\n> We are not using the filtered result from filterExposure() and we make\n> complex assumptions when dividing the exposure and analogue gain values.\n> \n> Use the same mechanism as in RPiController::Agc::divideUpExposure() with\n> the fixed limits defined previously. This simplifies the understanding,\n> and corrects a bug in the analogue gain calculus.\n\nThat's two fixes in one patch.\n\n> Signed-off-by: Jean-Michel Hautbois <jeanmichel.hautbois@ideasonboard.com>\n> ---\n>  src/ipa/ipu3/algorithms/agc.cpp | 37 ++++++++++++++++++++++++---------\n>  1 file changed, 27 insertions(+), 10 deletions(-)\n> \n> diff --git a/src/ipa/ipu3/algorithms/agc.cpp b/src/ipa/ipu3/algorithms/agc.cpp\n> index e58a8a8d..3ec1c60c 100644\n> --- a/src/ipa/ipu3/algorithms/agc.cpp\n> +++ b/src/ipa/ipu3/algorithms/agc.cpp\n> @@ -153,17 +153,34 @@ void Agc::lockExposureGain(uint32_t &exposure, double &gain)\n>  \t\t/* \\todo: estimate if we need to desaturate */\n>  \t\tfilterExposure();\n>  \n> -\t\tDuration newExposure = 0.0s;\n> -\t\tif (currentShutter < kMaxShutterSpeed) {\n> -\t\t\texposure = std::clamp(static_cast<uint32_t>(exposure * currentExposure_ / currentExposureNoDg_), minExposureLines_, maxExposureLines_);\n> -\t\t\tnewExposure = currentExposure_ / exposure;\n> -\t\t\tgain = std::clamp(static_cast<double>(gain * currentExposure_ / newExposure), kMinGain, kMaxGain);\n> -\t\t} else if (currentShutter >= kMaxShutterSpeed) {\n> -\t\t\tgain = std::clamp(static_cast<double>(gain * currentExposure_ / currentExposureNoDg_), kMinGain, kMaxGain);\n> -\t\t\tnewExposure = currentExposure_ / gain;\n> -\t\t\texposure = std::clamp(static_cast<uint32_t>(exposure * currentExposure_ / newExposure), minExposureLines_, maxExposureLines_);\n> +\t\tDuration exposureValue = filteredExposure_;\n> +\t\tDuration shutterTime = kMinShutterSpeed;\n> +\t\tdouble stepGain = kMinGain;\n\nWhere does \"step\" come from ?\n\n> +\n> +\t\tif (shutterTime * stepGain < exposureValue) {\n> +\t\t\tDuration maxShutterMinGain = kMaxShutterSpeed * stepGain;\n> +\t\t\tif (maxShutterMinGain >= exposureValue) {\n> +\t\t\t\tLOG(IPU3Agc, Debug) << \"Setting shutterTime to \" << shutterTime;\n> +\t\t\t\tshutterTime = exposureValue / stepGain;\n> +\t\t\t} else {\n> +\t\t\t\tshutterTime = kMaxShutterSpeed;\n> +\t\t\t}\n> +\n> +\t\t\tDuration maxGainShutter = kMaxGain * shutterTime;\n> +\t\t\tif (maxGainShutter >= exposureValue) {\n> +\t\t\t\tstepGain = exposureValue / shutterTime;\n> +\t\t\t\tLOG(IPU3Agc, Debug) << \"Setting analogue gain to \" << stepGain;\n> +\t\t\t} else {\n> +\t\t\t\tstepGain = kMaxGain;\n> +\t\t\t}\n>  \t\t}\n\nIf I understand the code correctly, this is essentially\n\n\t\tshutterTime = std::clamp(exposureValue / kMinGain,\n\t\t\t\t\t kMinShutterSpeed, kMaxShutterSpeed);\n\t\tstepGain = std::clamp(exposureValue / shutterTime,\n\t\t\t\t      kMinGain, kMaxGain);\n\nIsn't it easier to read ? You could even add a comment before:\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\nThe mechanism is fairly different from what is implemented in the RPi\nIPA, as you don't take into account fixed exposure time or fixed gains,\nor stages. I thus wouldn't indicate in the commit message that you're\ndoing the same as RPi.\n\n> -\t\tLOG(IPU3Agc, Debug) << \"Adjust exposure \" << exposure * lineDuration_ << \" and gain \" << gain;\n> +\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 82E3CC323E\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri, 15 Oct 2021 01:12:03 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 073A668F50;\n\tFri, 15 Oct 2021 03:12:03 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id C5B7368F4D\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 15 Oct 2021 03:12:01 +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 4DC8F2E3;\n\tFri, 15 Oct 2021 03:12:01 +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=\"pZ+Zg4FM\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1634260321;\n\tbh=2iRnig2w5QR475s9fv0o3VEhqVAleaX2XMq68UvSLW8=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=pZ+Zg4FMqxnXvxsBEYmELmv0EeizSzJ/CTyU/Fwx9qlyfozStmhydt8tf1WElyYNG\n\tXgRLoQDRLBvNG9p8+Y45IBrcq9yU8+RjUfNDnQYKhzz6hxIEdFzFtH7272xTtY6q9p\n\tZQiI/NPhRPpXq7lgzWwZ3YCTT8NOdKj389Smf5x4=","Date":"Fri, 15 Oct 2021 04:11:45 +0300","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Jean-Michel Hautbois <jeanmichel.hautbois@ideasonboard.com>","Message-ID":"<YWjVUdQHWtizecVt@pendragon.ideasonboard.com>","References":"<20211013154125.133419-1-jeanmichel.hautbois@ideasonboard.com>\n\t<20211013154125.133419-9-jeanmichel.hautbois@ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20211013154125.133419-9-jeanmichel.hautbois@ideasonboard.com>","Subject":"Re: [libcamera-devel] [PATCH 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>"}}]