From patchwork Tue Nov 17 08:47:11 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Klug X-Patchwork-Id: 28938 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 4317ABDE17 for ; Tue, 17 Nov 2026 08:47:20 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id E21ED6A86C; Tue, 17 Nov 2026 09:47:19 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="KfRqIQ4p"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 0EAD06A861 for ; Tue, 17 Nov 2026 09:47:19 +0100 (CET) Received: from ideasonboard.com (unknown [IPv6:2a00:6020:448c:6c00:27fa:cb4b:1d83:9b64]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 97947929; Tue, 17 Nov 2026 09:44:51 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1794905091; bh=DVnLqXnRiwAXkl/F+urIfO/w29xSYoipHTYetx3LM54=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=KfRqIQ4psptw9OHs35grB2DgfWDy4TLzLOR9Vz2oZG/CdTyXpVPtvzbT+SVwcrmxj CeGPt2nT4yUPRWkg2F8GsehzUM6kjvySaBUZ841SbRwm6xZu42CYbp7iJSh+qmaC74 XrO0GIhi46LTWRlx2Lu6D+H7jRsK3VOqCywisLog= From: Stefan Klug Date: Tue, 17 Nov 2026 09:47:11 +0100 Subject: [PATCH 01/10] libipa: exposure_mode_helper: Add digital gain support MIME-Version: 1.0 Message-Id: <20261117-sklug-digital-gain-support-v1-v1-1-f0aa342ca877@ideasonboard.com> References: <20261117-sklug-digital-gain-support-v1-v1-0-f0aa342ca877@ideasonboard.com> In-Reply-To: <20261117-sklug-digital-gain-support-v1-v1-0-f0aa342ca877@ideasonboard.com> To: libcamera-devel@lists.libcamera.org Cc: Stefan Klug X-Mailer: b4 0.14.3 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" The exposure mode helper splits a requested exposure into exposure time and gain according to the configured stages. Extend this to support digital gain. Per stage the specified exposure time, gain and then digital gain get maxed out until the algorithm goes to the next stage. Extend the setLimits function, so that digital gain can be constrained by external limits the same way as exposure time and gain. AgcMeanLuminance hardcodes a fixed digital gain limit of 1 for now and will be extended in upcoming patches. Signed-off-by: Stefan Klug --- src/ipa/libipa/agc_mean_luminance.cpp | 5 +- src/ipa/libipa/exposure_mode_helper.cpp | 90 +++++++++++++++++++++++---------- src/ipa/libipa/exposure_mode_helper.h | 8 ++- 3 files changed, 73 insertions(+), 30 deletions(-) diff --git a/src/ipa/libipa/agc_mean_luminance.cpp b/src/ipa/libipa/agc_mean_luminance.cpp index 0c5ad469ae0e..33d938cc6a87 100644 --- a/src/ipa/libipa/agc_mean_luminance.cpp +++ b/src/ipa/libipa/agc_mean_luminance.cpp @@ -370,6 +370,7 @@ int parseExposureModes(AgcMeanLuminance::Configuration &config, const ValueNode stages.push_back({ .exposureTime = std::chrono::microseconds(exposureTimes[i]), .gain = gains[i], + .digitalGain = 1.0, }); } @@ -501,6 +502,7 @@ AgcMeanLuminance::AgcMeanLuminance(Configuration &&config) { .exposureTime = utils::Duration::max(), .gain = std::numeric_limits::max(), + .digitalGain = 1.0, }, }); } @@ -543,7 +545,8 @@ void AgcMeanLuminance::setLimits(utils::Duration minExposureTime, std::vector constraints) { for (auto &[id, helper] : exposureModeHelpers_) - helper.setLimits(minExposureTime, maxExposureTime, minGain, maxGain); + helper.setLimits(minExposureTime, maxExposureTime, minGain, + maxGain, 1.0, 1.0); additionalConstraints_ = std::move(constraints); } diff --git a/src/ipa/libipa/exposure_mode_helper.cpp b/src/ipa/libipa/exposure_mode_helper.cpp index 5c4242519bc1..e3a7d096aea6 100644 --- a/src/ipa/libipa/exposure_mode_helper.cpp +++ b/src/ipa/libipa/exposure_mode_helper.cpp @@ -82,7 +82,8 @@ namespace ipa { */ ExposureModeHelper::ExposureModeHelper(std::vector &&stages) : stages_(std::move(stages)), lineDuration_(1us), minExposureTime_(0us), - maxExposureTime_(0us), minGain_(0), maxGain_(0), sensorHelper_(nullptr) + maxExposureTime_(0us), minGain_(0), maxGain_(0), minDigitalGain_(1.0), + maxDigitalGain_(1.0), sensorHelper_(nullptr) { ASSERT(!stages_.empty()); } @@ -115,24 +116,29 @@ void ExposureModeHelper::configure(utils::Duration lineDuration, * \param[in] maxExposureTime The maximum exposure time supported * \param[in] minGain The minimum analogue gain supported * \param[in] maxGain The maximum analogue gain supported + * \param[in] minDigitalGain The minimum digital gain supported + * \param[in] maxDigitalGain The maximum digital gain supported * - * This function configures the exposure time and analogue gain limits that need - * to be adhered to as the helper divides up exposure. Note that this function - * *must* be called whenever those limits change and before splitExposure() is - * used. + * This function configures the exposure time, analogue gain and digital gain + * limits that need to be adhered to as the helper divides up exposure. Note + * that this function *must* be called whenever those limits change and before + * splitExposure() is used. * - * If the algorithm using the helpers needs to indicate that either exposure time - * or analogue gain or both should be fixed it can do so by setting both the - * minima and maxima to the same value. + * If the algorithm using the helpers needs to indicate that exposure time, + * analogue gain or digital gain should be fixed it can do so by setting the + * minimum and maximum to the same value. */ void ExposureModeHelper::setLimits(utils::Duration minExposureTime, utils::Duration maxExposureTime, - double minGain, double maxGain) + double minGain, double maxGain, + double minDigitalGain, double maxDigitalGain) { minExposureTime_ = minExposureTime; maxExposureTime_ = maxExposureTime; minGain_ = minGain; maxGain_ = maxGain; + minDigitalGain_ = minDigitalGain; + maxDigitalGain_ = maxDigitalGain; } utils::Duration ExposureModeHelper::clampExposureTime(utils::Duration exposureTime, @@ -161,6 +167,11 @@ double ExposureModeHelper::clampGain(double gain, double *quantizationGain) cons return clamped; } +double ExposureModeHelper::clampDigitalGain(double gain) const +{ + return std::clamp(gain, minDigitalGain_, maxDigitalGain_); +} + /** * \struct ExposureModeHelper::Result * \brief Result of splitExposure() @@ -226,29 +237,32 @@ ExposureModeHelper::splitExposure(utils::Duration exposure) const double gain; double quantGain; double quantGain2; - bool gainFixed = minGain_ == maxGain_; - bool exposureTimeFixed = minExposureTime_ == maxExposureTime_; + double digitalGain; /* - * There's no point entering the loop if we cannot change either gain - * nor exposure time anyway. + * There's no point entering the loop if we cannot change anything. */ - if (exposureTimeFixed && gainFixed) { + if (minExposureTime_ == maxExposureTime_ && + minGain_ == maxGain_ && + minDigitalGain_ == maxDigitalGain_) { exposureTime = clampExposureTime(minExposureTime_, &quantGain); gain = clampGain(minGain_, &quantGain2); quantGain *= quantGain2; + digitalGain = clampDigitalGain(minDigitalGain_); - return { exposureTime, gain, quantGain, - exposure / (exposureTime * gain * quantGain) }; + return { exposureTime, gain, quantGain, digitalGain }; } utils::Duration stageExposureTime = {}; double lastStageGain = clampGain(1.0); + double lastStageDigitalGain = clampDigitalGain(1.0); double stageGain = {}; + double stageDigitalGain = {}; for (const auto &stage : stages_) { stageExposureTime = clampExposureTime(stage.exposureTime, &quantGain); stageGain = clampGain(stage.gain, &quantGain2); + stageDigitalGain = clampDigitalGain(stage.digitalGain); /* * We perform the clamping on both exposure time and gain in @@ -259,26 +273,48 @@ ExposureModeHelper::splitExposure(utils::Duration exposure) const */ /* Clamp the gain to lastStageGain and regulate exposureTime. */ - if (stageExposureTime * lastStageGain >= exposure) { - exposureTime = clampExposureTime(exposure / lastStageGain, &quantGain); - gain = clampGain(exposure / exposureTime, &quantGain2); + if (stageExposureTime * lastStageGain * lastStageDigitalGain >= exposure) { + auto targetExposure = exposure / (lastStageGain * lastStageDigitalGain); + exposureTime = clampExposureTime(targetExposure, &quantGain); + + auto targetGain = exposure / (exposureTime * lastStageDigitalGain); + gain = clampGain(targetGain, &quantGain2); quantGain *= quantGain2; - return { exposureTime, gain, quantGain, - exposure / (exposureTime * gain * quantGain) }; + auto targetDigitalGain = exposure / (exposureTime * gain * quantGain); + digitalGain = clampDigitalGain(targetDigitalGain); + + return { exposureTime, gain, quantGain, digitalGain }; } /* Clamp the exposureTime to stageExposureTime and regulate gain. */ - if (stageExposureTime * stageGain >= exposure) { + if (stageExposureTime * stageGain * lastStageDigitalGain >= exposure) { exposureTime = stageExposureTime; - gain = clampGain(exposure / exposureTime, &quantGain2); + + auto targetGain = exposure / (exposureTime * lastStageDigitalGain); + gain = clampGain(targetGain, &quantGain2); quantGain *= quantGain2; - return { exposureTime, gain, quantGain, - exposure / (exposureTime * gain * quantGain) }; + auto targetDigitalGain = exposure / (exposureTime * gain * quantGain); + digitalGain = clampDigitalGain(targetDigitalGain); + + return { exposureTime, gain, quantGain, digitalGain }; + } + + /* Clamp the exposureTime to stageExposureTime and regulate gain. */ + if (stageExposureTime * stageGain * stageDigitalGain >= exposure) { + exposureTime = stageExposureTime; + gain = stageGain; + quantGain *= quantGain2; + + auto targetDigitalGain = exposure / (exposureTime * gain * quantGain); + digitalGain = clampDigitalGain(targetDigitalGain); + + return { exposureTime, gain, quantGain, targetDigitalGain }; } lastStageGain = stageGain; + lastStageDigitalGain = stageDigitalGain; } /* @@ -288,9 +324,9 @@ ExposureModeHelper::splitExposure(utils::Duration exposure) const exposureTime = stageExposureTime; gain = stageGain; quantGain *= quantGain2; + digitalGain = stageDigitalGain; - return { exposureTime, gain, quantGain, - exposure / (exposureTime * gain * quantGain) }; + return { exposureTime, gain, quantGain, digitalGain }; } } /* namespace ipa */ diff --git a/src/ipa/libipa/exposure_mode_helper.h b/src/ipa/libipa/exposure_mode_helper.h index d33a0fd822e4..aeae5a9cb700 100644 --- a/src/ipa/libipa/exposure_mode_helper.h +++ b/src/ipa/libipa/exposure_mode_helper.h @@ -23,14 +23,15 @@ public: struct Stage { utils::Duration exposureTime; double gain; + double digitalGain; }; ExposureModeHelper(std::vector &&stages); void configure(utils::Duration lineLength, const CameraSensorHelper *sensorHelper); void setLimits(utils::Duration minExposureTime, utils::Duration maxExposureTime, - double minGain, double maxGain); - + double minGain, double maxGain, double minDigitalGain, + double maxDigitalGain); struct Result { utils::Duration exposureTime; double analogueGain; @@ -44,6 +45,7 @@ private: utils::Duration clampExposureTime(utils::Duration exposureTime, double *quantizationGain = nullptr) const; double clampGain(double gain, double *quantizationGain = nullptr) const; + double clampDigitalGain(double gain) const; std::vector stages_; @@ -52,6 +54,8 @@ private: utils::Duration maxExposureTime_; double minGain_; double maxGain_; + double minDigitalGain_; + double maxDigitalGain_; const CameraSensorHelper *sensorHelper_; };