From patchwork Mon Nov 9 16:12:32 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= X-Patchwork-Id: 28823 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 180B9C3304 for ; Mon, 9 Nov 2026 16:13:08 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 827A96A07F; Mon, 9 Nov 2026 17:12:58 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="GYqr/Y8T"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id D326F691E1 for ; Mon, 9 Nov 2026 17:12:50 +0100 (CET) Received: from pb-laptop.local (185.221.143.99.nat.pool.zt.hu [185.221.143.99]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 1F27518C7; Mon, 9 Nov 2026 17:10:29 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1794240629; bh=bMKR5OgPo5jCLyCEWsXU72bwzSmGaAsrqH9uxNFaFZY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=GYqr/Y8TIHyX+Q9W2drsKvATBAiIU8tUdpmlrO76krr/6yyTo7hcT88auq5Ddrnua OJedZRCOvZuenhdCqkstprTVlf1R2T3YOwmco0Rc8FNBftFJp1OF00rMdIIuHgZ6nO Xv/HWTC6EYT58bYCRRr63y090s/inD8qfUJtgmwQ= From: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= To: libcamera-devel@lists.libcamera.org Cc: Jacopo Mondi , Stefan Klug Subject: [PATCH v8 05/18] ipa: libipa: exposure_mode_helper: Consider last stage as hard-limit Date: Mon, 9 Nov 2026 17:12:32 +0100 Message-ID: <20261109161246.513264-6-barnabas.pocze@ideasonboard.com> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261109161246.513264-1-barnabas.pocze@ideasonboard.com> References: <20261109161246.513264-1-barnabas.pocze@ideasonboard.com> MIME-Version: 1.0 X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Currently `splitExposure()` will raise the exposure and analogue gain above what is specified in the last stage, if the desired exposure could not be reached. This behaviour is different from that of the raspberry pi ipa, and also could be surprising to the user / tuning file creator. So make sure that the exposure time and analogue gain is limited by the last stage, in addition to the runtime controlled upper bound. With this change, at least one stage is required, so assertions are added. And the general agc algorithm is adjusted to provide stages for the fallback mode. Note that the `stageGain` initialization has to be adjusted to retrieve the quantization gain, so that at the end of the loop * stageExposureTime * stageGain * quantGain * quantGain2 are all up to date for the last stage. Closes: https://gitlab.freedesktop.org/camera/libcamera/-/work_items/362 Signed-off-by: Barnabás Pőcze Reviewed-by: Jacopo Mondi Reviewed-by: Stefan Klug --- src/ipa/libipa/agc_mean_luminance.cpp | 11 ++++++++-- src/ipa/libipa/exposure_mode_helper.cpp | 27 ++++++++++++------------- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/src/ipa/libipa/agc_mean_luminance.cpp b/src/ipa/libipa/agc_mean_luminance.cpp index 1ade717c17..7acfebf52f 100644 --- a/src/ipa/libipa/agc_mean_luminance.cpp +++ b/src/ipa/libipa/agc_mean_luminance.cpp @@ -9,6 +9,7 @@ #include #include +#include #include #include @@ -488,8 +489,14 @@ AgcMeanLuminance::AgcMeanLuminance(Configuration &&config) * possible before touching gain. */ if (exposureModeHelpers_.empty()) - exposureModeHelpers_.try_emplace(controls::AeExposureModeNormal, - std::vector{}); + exposureModeHelpers_.try_emplace( + controls::AeExposureModeNormal, + std::vector{ + { + .exposureTime = utils::Duration::max(), + .gain = std::numeric_limits::max(), + }, + }); } AgcMeanLuminance::~AgcMeanLuminance() = default; diff --git a/src/ipa/libipa/exposure_mode_helper.cpp b/src/ipa/libipa/exposure_mode_helper.cpp index 4ec31f54c3..5c4242519b 100644 --- a/src/ipa/libipa/exposure_mode_helper.cpp +++ b/src/ipa/libipa/exposure_mode_helper.cpp @@ -84,6 +84,7 @@ ExposureModeHelper::ExposureModeHelper(std::vector &&stages) : stages_(std::move(stages)), lineDuration_(1us), minExposureTime_(0us), maxExposureTime_(0us), minGain_(0), maxGain_(0), sensorHelper_(nullptr) { + ASSERT(!stages_.empty()); } /** @@ -203,8 +204,8 @@ double ExposureModeHelper::clampGain(double gain, double *quantizationGain) cons * value. * * If no combination of exposure time and gain limits is found that meets the - * required exposure, the helper falls-back to simply maximising the exposure - * time first, followed by analogue gain, followed by digital gain. + * required exposure, then the exposure and gain of the last stage will be used, + * and the digital gain will be increased as necessary. * * During the calculations the gain missed due to quantization is recorded and * returned as quantization gain. The quantization gain is not included in the @@ -219,6 +220,7 @@ ExposureModeHelper::splitExposure(utils::Duration exposure) const { ASSERT(maxExposureTime_); ASSERT(maxGain_); + ASSERT(!stages_.empty()); utils::Duration exposureTime; double gain; @@ -240,13 +242,13 @@ ExposureModeHelper::splitExposure(utils::Duration exposure) const exposure / (exposureTime * gain * quantGain) }; } - double stageGain = clampGain(1.0); - double lastStageGain = stageGain; + utils::Duration stageExposureTime = {}; + double lastStageGain = clampGain(1.0); + double stageGain = {}; for (const auto &stage : stages_) { - utils::Duration stageExposureTime = clampExposureTime(stage.exposureTime, - &quantGain); - stageGain = clampGain(stage.gain); + stageExposureTime = clampExposureTime(stage.exposureTime, &quantGain); + stageGain = clampGain(stage.gain, &quantGain2); /* * We perform the clamping on both exposure time and gain in @@ -280,14 +282,11 @@ ExposureModeHelper::splitExposure(utils::Duration exposure) const } /* - * From here on all we can do is max out the exposure time, followed by - * the analogue gain. If we still haven't achieved the target we send - * the rest of the exposure time to digital gain. If we were given no - * stages to use then the default stageGain of 1.0 is used so that - * exposure time is maxed before gain is touched at all. + * If no stages could satisfy the desired exposure, then use the last + * stage's exposure time and gain, which is the maximum allowed. */ - exposureTime = clampExposureTime(exposure / stageGain, &quantGain); - gain = clampGain(exposure / exposureTime, &quantGain2); + exposureTime = stageExposureTime; + gain = stageGain; quantGain *= quantGain2; return { exposureTime, gain, quantGain,