{"id":27487,"url":"https://patchwork.libcamera.org/api/patches/27487/?format=json","web_url":"https://patchwork.libcamera.org/patch/27487/","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":"<20260723154327.1357866-39-barnabas.pocze@ideasonboard.com>","date":"2026-07-23T15:43:21","name":"[RFC,v2,38/43] ipa: libipa: agc_msv: Ensure limits are always respected","commit_ref":null,"pull_url":null,"state":"new","archived":false,"hash":"1ddd19ac00602e268257ae9374f4c2381e7a67fa","submitter":{"id":216,"url":"https://patchwork.libcamera.org/api/people/216/?format=json","name":"Barnabás Pőcze","email":"barnabas.pocze@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/27487/mbox/","series":[{"id":6075,"url":"https://patchwork.libcamera.org/api/series/6075/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=6075","date":"2026-07-23T15:42:43","name":"ipa: libipa: agc rework","version":2,"mbox":"https://patchwork.libcamera.org/series/6075/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/27487/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/27487/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 4840CC3339\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu, 23 Jul 2026 15:44:25 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id A121F67F80;\n\tThu, 23 Jul 2026 17:44:24 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 301DB67EEA\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 23 Jul 2026 17:43:40 +0200 (CEST)","from pb-laptop.local (185.182.215.156.nat.pool.zt.hu\n\t[185.182.215.156])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 50D052C0B\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 23 Jul 2026 17:42:39 +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=\"DmgtWS0k\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1784821359;\n\tbh=tqxwRekX1pe5lTqv8KeMnWE5PYwZfpJxr4s+icm4tOo=;\n\th=From:To:Subject:Date:In-Reply-To:References:From;\n\tb=DmgtWS0kqAkD9LQYvcDA5P+55zvDgUtalhlP3gAUIkkTqR5r770bRc2MiPwECd86l\n\t5+xrgFIdf25+LgVBUwklfNrb9FJuS6U0RzB9j8rurmSdPGKyFJkPYjBuAF1unKJj8u\n\tGiiRXJ7mvnH5P+K4QizkrPGAW82f2xfrA/HxzILE=","From":"=?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= <barnabas.pocze@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Subject":"[RFC PATCH v2 38/43] ipa: libipa: agc_msv: Ensure limits are always\n\trespected","Date":"Thu, 23 Jul 2026 17:43:21 +0200","Message-ID":"<20260723154327.1357866-39-barnabas.pocze@ideasonboard.com>","X-Mailer":"git-send-email 2.55.0","In-Reply-To":"<20260723154327.1357866-1-barnabas.pocze@ideasonboard.com>","References":"<20260723154327.1357866-1-barnabas.pocze@ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=UTF-8","Content-Transfer-Encoding":"8bit","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":"Make sure that even in the presence of continous limit changes, the\nreturned exposure and gain values are always within bounds. This is\nneeded for integration into `AgcAlgorithm`, which might vary the limits\nfor every invocation.\n\nSigned-off-by: Barnabás Pőcze <barnabas.pocze@ideasonboard.com>\n---\n src/ipa/libipa/agc_msv.cpp | 84 ++++++++++++++++++++------------------\n 1 file changed, 45 insertions(+), 39 deletions(-)","diff":"diff --git a/src/ipa/libipa/agc_msv.cpp b/src/ipa/libipa/agc_msv.cpp\nindex 63580a0055..956551685c 100644\n--- a/src/ipa/libipa/agc_msv.cpp\n+++ b/src/ipa/libipa/agc_msv.cpp\n@@ -157,59 +157,65 @@ void AgcMSV::setLimits(const Limits &limits)\n  */\n AgcMSV::Result AgcMSV::calculateNewEv(const Params &params)\n {\n-\tauto exposureMSV = calculateMSV(params.yHist);\n-\tif (!exposureMSV) {\n+\tAgcMSV::Result result = { params.exposure, params.gain };\n+\n+\tif (auto exposureMSV = calculateMSV(params.yHist)) {\n+\t\tresult = updateExposure(params.exposure, params.gain, *exposureMSV);\n+\t} else {\n \t\tLOG(AgcMSV, Debug)\n \t\t\t<< \"Not adjusting exposure due to insufficient histogram data\";\n-\t\treturn { params.exposure, params.gain };\n \t}\n \n-\treturn updateExposure(params.exposure, params.gain, *exposureMSV);\n+\tresult.exposure = std::clamp(result.exposure, limits_.exposure[0], limits_.exposure[1]);\n+\tresult.analogueGain = std::clamp(result.analogueGain, limits_.gain[0], limits_.gain[1]);\n+\n+\tLOG(AgcMSV, Debug)\n+\t\t<< \"exposure:\" << result.exposure\n+\t\t<< \" analogue-gain:\" << result.analogueGain;\n+\n+\treturn result;\n }\n \n AgcMSV::Result AgcMSV::updateExposure(uint32_t exposure, double again, float exposureMSV)\n {\n \tfloat error = kExposureOptimal - exposureMSV;\n-\tif (std::abs(error) <= kExposureSatisfactory)\n-\t\treturn { exposure, again };\n \n-\t/*\n-\t * Compute a proportional correction factor. The sign of the error\n-\t * determines the direction: positive error means too dark (increase),\n-\t * negative means too bright (decrease).\n-\t */\n-\tfloat step = std::clamp(error * kExpProportionalGain,\n-\t\t\t\t-kExpMaxStep, kExpMaxStep);\n-\tfloat factor = 1.0f + step;\n-\n-\tif (factor > 1.0f) {\n-\t\t/* Scene too dark: increase exposure first, then gain. */\n-\t\tif (exposure < limits_.exposure[1]) {\n-\t\t\tuint32_t next = exposure * factor;\n-\t\t\texposure = std::max(next, exposure + 1);\n-\t\t} else {\n-\t\t\tdouble next = again * factor;\n-\t\t\tagain = std::max(next, again + limits_.gainMinStep);\n-\t\t}\n-\t} else {\n-\t\t/* Scene too bright: decrease gain first, then exposure. */\n-\t\tif (again > limits_.gain1) {\n-\t\t\tdouble next = again * factor;\n-\t\t\tagain = std::min(next, again - limits_.gainMinStep);\n+\tLOG(AgcMSV, Debug)\n+\t\t<< \"exposureMSV:\" << exposureMSV << \" error:\" << error;\n+\n+\tif (std::abs(error) > kExposureSatisfactory) {\n+\t\t/*\n+\t\t * Compute a proportional correction factor. The sign of the error\n+\t\t * determines the direction: positive error means too dark (increase),\n+\t\t * negative means too bright (decrease).\n+\t\t */\n+\t\tfloat step = std::clamp(error * kExpProportionalGain,\n+\t\t\t\t\t-kExpMaxStep, kExpMaxStep);\n+\t\tfloat factor = 1.0f + step;\n+\n+\t\tLOG(AgcMSV, Debug) << \"factor:\" << factor;\n+\n+\t\tif (factor > 1.0f) {\n+\t\t\t/* Scene too dark: increase exposure first, then gain. */\n+\t\t\tif (exposure < limits_.exposure[1]) {\n+\t\t\t\tuint32_t next = exposure * factor;\n+\t\t\t\texposure = std::max(next, exposure + 1);\n+\t\t\t} else {\n+\t\t\t\tdouble next = again * factor;\n+\t\t\t\tagain = std::max(next, again + limits_.gainMinStep);\n+\t\t\t}\n \t\t} else {\n-\t\t\tuint32_t next = exposure * factor;\n-\t\t\texposure = std::min(next, exposure - 1);\n+\t\t\t/* Scene too bright: decrease gain first, then exposure. */\n+\t\t\tif (again > limits_.gain1) {\n+\t\t\t\tdouble next = again * factor;\n+\t\t\t\tagain = std::min(next, again - limits_.gainMinStep);\n+\t\t\t} else {\n+\t\t\t\tuint32_t next = exposure * factor;\n+\t\t\t\texposure = std::min(next, exposure - 1);\n+\t\t\t}\n \t\t}\n \t}\n \n-\texposure = std::clamp(exposure, limits_.exposure[0], limits_.exposure[1]);\n-\tagain = std::clamp(again, limits_.gain[0], limits_.gain[1]);\n-\n-\tLOG(AgcMSV, Debug)\n-\t\t<< \"exposureMSV:\" << exposureMSV\n-\t\t<< \" error:\" << error << \" factor:\" << factor\n-\t\t<< \" exposure:\" << exposure << \" analogue-gain:\" << again;\n-\n \treturn { exposure, again };\n }\n \n","prefixes":["RFC","v2","38/43"]}