{"id":26211,"url":"https://patchwork.libcamera.org/api/patches/26211/?format=json","web_url":"https://patchwork.libcamera.org/patch/26211/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20260219-kbingham-quantizers-v8-13-2b6ff68ead26@ideasonboard.com>","date":"2026-02-19T15:05:14","name":"[v8,13/15] ipa: mali-c55: agc: Quantise the ISP Digital Gain","commit_ref":"8896ca13f1dee770dde2d9e8adfcf4ec00f65f5b","pull_url":null,"state":"accepted","archived":false,"hash":"c69df568ab05801d49713dabba7a7b2e1ccf4bb1","submitter":{"id":4,"url":"https://patchwork.libcamera.org/api/people/4/?format=json","name":"Kieran Bingham","email":"kieran.bingham@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/26211/mbox/","series":[{"id":5802,"url":"https://patchwork.libcamera.org/api/series/5802/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=5802","date":"2026-02-19T15:05:01","name":"libipa: Introduce a Quantized type","version":8,"mbox":"https://patchwork.libcamera.org/series/5802/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/26211/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/26211/checks/","tags":{},"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 8F27CC32F3\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu, 19 Feb 2026 15:05:28 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 3CE196229B;\n\tThu, 19 Feb 2026 16:05:26 +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 4CD1862270\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 19 Feb 2026 16:05:12 +0100 (CET)","from ping.linuxembedded.co.uk\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 27E6727F7;\n\tThu, 19 Feb 2026 16:04:19 +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=\"ZE4HGxcM\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1771513459;\n\tbh=8mR7Mod1Kgrw0Q8pG6KfQsCxjvvIGVE6ohbpnqr2JS4=;\n\th=From:Date:Subject:References:In-Reply-To:To:Cc:From;\n\tb=ZE4HGxcM5n+M78FT3H0Mx7+E+3dvgapTyizu202gJ1PMe1dmLMTYKSWbnvyWKDmw4\n\ta97R5uy/HYaFFvRbnUb1VcTu9V2dkdtttKJVc1Gm1hdcJYcAxIL6VVo63v8WrurKmP\n\tj04lKvxAd7DdFtGEd+4bSGqtQ1+NllLmxc4UujuU=","From":"Kieran Bingham <kieran.bingham@ideasonboard.com>","Date":"Thu, 19 Feb 2026 15:05:14 +0000","Subject":"[PATCH v8 13/15] ipa: mali-c55: agc: Quantise the ISP Digital Gain","MIME-Version":"1.0","Content-Type":"text/plain; charset=\"utf-8\"","Content-Transfer-Encoding":"7bit","Message-Id":"<20260219-kbingham-quantizers-v8-13-2b6ff68ead26@ideasonboard.com>","References":"<20260219-kbingham-quantizers-v8-0-2b6ff68ead26@ideasonboard.com>","In-Reply-To":"<20260219-kbingham-quantizers-v8-0-2b6ff68ead26@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Cc":"Kieran Bingham <kieran.bingham@ideasonboard.com>, \n\tIsaac Scott <isaac.scott@ideasonboard.com>, \n\tStefan Klug <stefan.klug@ideasonboard.com>","X-Mailer":"b4 0.14.3","X-Developer-Signature":"v=1; a=ed25519-sha256; t=1771513507; l=4981;\n\ti=kieran.bingham@ideasonboard.com; s=20260207;\n\th=from:subject:message-id; \n\tbh=8mR7Mod1Kgrw0Q8pG6KfQsCxjvvIGVE6ohbpnqr2JS4=;\n\tb=6xOklpOspfW3Nkx7kpqKymQ9Ig+/8Pim+p6Yyl/kYpXiL4hQajCO0pm+lh1zj44IvAITWfFED\n\t7oOy9qUcTIQCGceJNm+L1lf9yMOYJLUYhR2lKnyrUKBSt9jUTPI0g9D","X-Developer-Key":"i=kieran.bingham@ideasonboard.com; a=ed25519;\n\tpk=FVXKN7YuwHc6UtbRUeTMAmranfsQomA+vnilfglWdaY=","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>"},"content":"The Mali-C55 ISP has a digital gain block which allows setting gain in Q5.8\nformat, a range of 0.0 to (very nearly) 32.0.\n\nConvert usage to the new UQ<5, 8> FixedPoint Quantised type which will\nsupport the conversion, clamping and quantisation so that the metadata\nand debug prints can now report the effective gain applied instead of\nthe potentially inaccurate float.\n\nReviewed-by: Isaac Scott <isaac.scott@ideasonboard.com>\nReviewed-by: Stefan Klug <stefan.klug@ideasonboard.com>\nSigned-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n\n---\n\nv8:\n - Keep the gain clamps\n---\n src/ipa/mali-c55/algorithms/agc.cpp | 25 ++++++++++++-------------\n src/ipa/mali-c55/ipa_context.h      |  6 +++---\n 2 files changed, 15 insertions(+), 16 deletions(-)","diff":"diff --git a/src/ipa/mali-c55/algorithms/agc.cpp b/src/ipa/mali-c55/algorithms/agc.cpp\nindex 014fd12452ac..34d676677efa 100644\n--- a/src/ipa/mali-c55/algorithms/agc.cpp\n+++ b/src/ipa/mali-c55/algorithms/agc.cpp\n@@ -38,8 +38,8 @@ static constexpr unsigned int kNumHistogramBins = 256;\n  * format, a range of 0.0 to (very nearly) 32.0. We clamp from 1.0 to the actual\n  * max value which is 8191 * 2^-8.\n  */\n-static constexpr double kMinDigitalGain = 1.0;\n-static constexpr double kMaxDigitalGain = 31.99609375;\n+static constexpr float kMinDigitalGain = 1.0;\n+static constexpr float kMaxDigitalGain = UQ<5, 8>::TraitsType::max;\n \n uint32_t AgcStatistics::decodeBinValue(uint16_t binVal)\n {\n@@ -139,10 +139,7 @@ int Agc::init(IPAContext &context, const YamlObject &tuningData)\n \n \tcontext.ctrlMap[&controls::AeEnable] = ControlInfo(false, true);\n \tcontext.ctrlMap[&controls::DigitalGain] = ControlInfo(\n-\t\tstatic_cast<float>(kMinDigitalGain),\n-\t\tstatic_cast<float>(kMaxDigitalGain),\n-\t\tstatic_cast<float>(kMinDigitalGain)\n-\t);\n+\t\tkMinDigitalGain, kMaxDigitalGain, kMinDigitalGain);\n \tcontext.ctrlMap.merge(controls());\n \n \treturn 0;\n@@ -245,7 +242,7 @@ void Agc::fillGainParamBlock(IPAContext &context, IPAFrameContext &frameContext,\n \t\t\t     MaliC55Params *params)\n {\n \tIPAActiveState &activeState = context.activeState;\n-\tdouble gain;\n+\tUQ<5, 8> gain;\n \n \tif (activeState.agc.autoEnabled)\n \t\tgain = activeState.agc.automatic.ispGain;\n@@ -253,7 +250,7 @@ void Agc::fillGainParamBlock(IPAContext &context, IPAFrameContext &frameContext,\n \t\tgain = activeState.agc.manual.ispGain;\n \n \tauto block = params->block<MaliC55Blocks::Dgain>();\n-\tblock->gain = floatingToFixedPoint<5, 8, uint16_t, double>(gain);\n+\tblock->gain = gain.quantized();\n \n \tframeContext.agc.ispGain = gain;\n }\n@@ -358,7 +355,7 @@ void Agc::process(IPAContext &context,\n \t */\n \tuint32_t exposure = frameContext.agc.exposure;\n \tdouble analogueGain = frameContext.agc.sensorGain;\n-\tdouble digitalGain = frameContext.agc.ispGain;\n+\tdouble digitalGain = frameContext.agc.ispGain.value();\n \tdouble totalGain = analogueGain * digitalGain;\n \tutils::Duration currentShutter = exposure * configuration.sensor.lineDuration;\n \tutils::Duration effectiveExposureValue = currentShutter * totalGain;\n@@ -370,19 +367,21 @@ void Agc::process(IPAContext &context,\n \t\t\t       activeState.agc.exposureMode, statistics_.yHist,\n \t\t\t       effectiveExposureValue);\n \n-\tdGain = std::clamp(dGain, kMinDigitalGain, kMaxDigitalGain);\n+\tUQ<5, 8> dGainQ = std::clamp(static_cast<float>(dGain),\n+\t\t\t\t     kMinDigitalGain,\n+\t\t\t\t     kMaxDigitalGain);\n \n \tLOG(MaliC55Agc, Debug)\n \t\t<< \"Divided up shutter, analogue gain and digital gain are \"\n-\t\t<< shutterTime << \", \" << aGain << \" and \" << dGain;\n+\t\t<< shutterTime << \", \" << aGain << \" and \" << dGainQ;\n \n \tactiveState.agc.automatic.exposure = shutterTime / configuration.sensor.lineDuration;\n \tactiveState.agc.automatic.sensorGain = aGain;\n-\tactiveState.agc.automatic.ispGain = dGain;\n+\tactiveState.agc.automatic.ispGain = dGainQ;\n \n \tmetadata.set(controls::ExposureTime, currentShutter.get<std::micro>());\n \tmetadata.set(controls::AnalogueGain, frameContext.agc.sensorGain);\n-\tmetadata.set(controls::DigitalGain, frameContext.agc.ispGain);\n+\tmetadata.set(controls::DigitalGain, frameContext.agc.ispGain.value());\n \tmetadata.set(controls::ColourTemperature, context.activeState.agc.temperatureK);\n }\n \ndiff --git a/src/ipa/mali-c55/ipa_context.h b/src/ipa/mali-c55/ipa_context.h\nindex 08f78e4f74ce..ac4b83773803 100644\n--- a/src/ipa/mali-c55/ipa_context.h\n+++ b/src/ipa/mali-c55/ipa_context.h\n@@ -41,12 +41,12 @@ struct IPAActiveState {\n \t\tstruct {\n \t\t\tuint32_t exposure;\n \t\t\tdouble sensorGain;\n-\t\t\tdouble ispGain;\n+\t\t\tUQ<5, 8> ispGain;\n \t\t} automatic;\n \t\tstruct {\n \t\t\tuint32_t exposure;\n \t\t\tdouble sensorGain;\n-\t\t\tdouble ispGain;\n+\t\t\tUQ<5, 8> ispGain;\n \t\t} manual;\n \t\tbool autoEnabled;\n \t\tuint32_t constraintMode;\n@@ -64,7 +64,7 @@ struct IPAFrameContext : public FrameContext {\n \tstruct {\n \t\tuint32_t exposure;\n \t\tdouble sensorGain;\n-\t\tdouble ispGain;\n+\t\tUQ<5, 8> ispGain;\n \t} agc;\n \n \tstruct {\n","prefixes":["v8","13/15"]}