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_; }; From patchwork Tue Nov 17 08:47:12 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Stefan Klug X-Patchwork-Id: 28939 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 552EEBDE17 for ; Tue, 17 Nov 2026 08:47:23 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 0D3D36A873; Tue, 17 Nov 2026 09:47:23 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="eW0dmOQA"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 8126D6A861 for ; Tue, 17 Nov 2026 09:47:21 +0100 (CET) Received: from ideasonboard.com (unknown [IPv6:2a00:6020:448c:6c00:27fa:cb4b:1d83:9b64]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 1C0F3FBE; Tue, 17 Nov 2026 09:44:54 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1794905094; bh=y8CFENPBZ6RagfwPMWxTaIQRfRiWEwjXf1UZF/LFZuc=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=eW0dmOQAefkOmKJOCQbykAI+KorvPkNzcppOxUssL2WUeBmiwQO3SHKZNGF9/4ZwA OyTlLSKyiqWWCrYsV58yyxpoH36DoSOg5fINlq3HMhrXFTAj4Wk9ZiCN0dkXVUTG5H Wa1n2ITpLpB+ywPLly9J3jAZ+DAATljgzPz4eVaM= From: Stefan Klug Date: Tue, 17 Nov 2026 09:47:12 +0100 Subject: [PATCH 02/10] libcamera: control_ids_core: Add DigitalGainMode enum MIME-Version: 1.0 Message-Id: <20261117-sklug-digital-gain-support-v1-v1-2-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" Add a DigitalGainMode enum that works the same way for digital gain as the AnalogGainMode works for analogue gain. Signed-off-by: Stefan Klug --- src/libcamera/control_ids_core.yaml | 44 +++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/libcamera/control_ids_core.yaml b/src/libcamera/control_ids_core.yaml index 6504f983faa6..394447b31d3e 100644 --- a/src/libcamera/control_ids_core.yaml +++ b/src/libcamera/control_ids_core.yaml @@ -1380,4 +1380,48 @@ controls: The nominal range is [-180, 180], where 0° leaves hues unchanged and the range wraps around continuously, with 180° == -180°. + - DigitalGainMode: + type: int32_t + direction: inout + description: | + Controls the source of the digital gain that is applied during processing. + + When set to Auto, the AEGC algorithm computes the digital gain and + configures the ISP accordingly. When set to Manual, the value + of the DigitalGain control is used. + + When transitioning from Auto to Manual mode and no DigitalGain control + is provided by the application, the last value computed by the AEGC + algorithm when the mode was Auto will be used. If the DigitalGainMode + was never set to Auto (either because the camera started in Manual mode, + or Auto is not supported by the camera), the camera should 1.0 as value. + + If Manual is supported, the DigitalGain control must + also be supported. + + \sa ExposureTimeMode + \sa AnalogueGain + enum: + - name: Auto + value: 0 + description: | + The digital gain will be calculated automatically and set by the + AEGC algorithm. + + If DigitalGain is set while this mode is active, it will be + ignored, and it will also not be retained. + + When transitioning from Manual to Auto mode, the AEGC should start + its adjustments based on the last set manual DigitalGain value. + - name: Manual + value: 1 + description: | + The digital gain will not be updated by the AEGC algorithm. + + When transitioning from Auto to Manual mode, the last computed + gain value is used until a new value is specified through the + DigitalGain control. If an DigitalGain value is specified in the + same request where the DigitalGainMode is changed from Auto to + Manual, the provided DigitalGain is applied immediately. + ... From patchwork Tue Nov 17 08:47:13 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Klug X-Patchwork-Id: 28940 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 4C457BDE17 for ; Tue, 17 Nov 2026 08:47:25 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id F3CD16A878; Tue, 17 Nov 2026 09:47:24 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="WVpqKN+M"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 2224E6A814 for ; Tue, 17 Nov 2026 09:47:24 +0100 (CET) Received: from ideasonboard.com (unknown [IPv6:2a00:6020:448c:6c00:27fa:cb4b:1d83:9b64]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 923F6929; Tue, 17 Nov 2026 09:44:56 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1794905096; bh=8Gh4xjzV5VzBXwCeCxmZdNXmvowkFwgpGPtRJWKK5gg=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=WVpqKN+MxDn5vk+45EWnD6UB5SHd+q1kPVUirlmrD/yanI5acpi4TuCZl+xjjmJ5R vPBkvQK3+YixeN1M75r7VMuB9ed830SQx/KZGXEJvYTiv/WdX8qIfk86nf0luZhjaL gXwaSJ9tGCRUNlqfs7TkJ4ARg1k3ACuFnIaKm9a0= From: Stefan Klug Date: Tue, 17 Nov 2026 09:47:13 +0100 Subject: [PATCH 03/10] libipa: agc_mean_luminance: Add support for digital gain MIME-Version: 1.0 Message-Id: <20261117-sklug-digital-gain-support-v1-v1-3-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" Extend AgcMeanLuminance so that an exposure mode can carry a optional digital gain array. The corresponding tuning file entry now has the following format: - Agc: AeExposureMode: Normal: exposureTime: [ 100, 10000, 30000, 60000, 120000 ] gain: [ 2.0, 4.0, 6.0, 8.0, 10.0 ] digitalGain: [ 1.0, 2.0, 3.0, 4.0, 6.0 ] When the digitalGain key is not present, a digital gain of 1.0 is assumed. Limit the digitalGain to 1.0 in AgcAlgorithm for now. It will be enabled in an upcoming patch. Signed-off-by: Stefan Klug --- src/ipa/libipa/agc.cpp | 1 + src/ipa/libipa/agc_mean_luminance.cpp | 31 ++++++++++++++++++++++++++----- src/ipa/libipa/agc_mean_luminance.h | 3 ++- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/src/ipa/libipa/agc.cpp b/src/ipa/libipa/agc.cpp index 4b2e65f37718..6ec37756c88e 100644 --- a/src/ipa/libipa/agc.cpp +++ b/src/ipa/libipa/agc.cpp @@ -842,6 +842,7 @@ void AgcAlgorithm::process(const agc::Session &session, agc::ActiveState &state, impl.setLimits(minExposureTime, maxExposureTime, minAnalogueGain, maxAnalogueGain, + 1.0, 1.0, std::move(params->additionalConstraints)); const auto &newEv = impl.calculateNewEv({ diff --git a/src/ipa/libipa/agc_mean_luminance.cpp b/src/ipa/libipa/agc_mean_luminance.cpp index 33d938cc6a87..d453602f0dbb 100644 --- a/src/ipa/libipa/agc_mean_luminance.cpp +++ b/src/ipa/libipa/agc_mean_luminance.cpp @@ -352,6 +352,8 @@ int parseExposureModes(AgcMeanLuminance::Configuration &config, const ValueNode modeValues["exposureTime"].get>().value_or(utils::defopt); std::vector gains = modeValues["gain"].get>().value_or(utils::defopt); + std::vector digitalGains = + modeValues["digitalGain"].get>().value_or(utils::defopt); if (exposureTimes.size() != gains.size()) { LOG(AgcMeanLuminance, Error) @@ -365,12 +367,22 @@ int parseExposureModes(AgcMeanLuminance::Configuration &config, const ValueNode return -EINVAL; } + /* Digital gain is optional and defaults to 1.0 */ + if (digitalGains.empty()) + digitalGains = std::vector(exposureTimes.size(), 1.0); + + if (exposureTimes.size() != digitalGains.size()) { + LOG(AgcMeanLuminance, Error) + << "Exposure time and digital gain array sizes unequal"; + return -EINVAL; + } + std::vector stages; for (size_t i = 0; i < exposureTimes.size(); i++) { stages.push_back({ .exposureTime = std::chrono::microseconds(exposureTimes[i]), .gain = gains[i], - .digitalGain = 1.0, + .digitalGain = digitalGains[i], }); } @@ -422,7 +434,8 @@ int parseExposureModes(AgcMeanLuminance::Configuration &config, const ValueNode * AeExposureMode containing per-mode setting dictionaries with the key being a * value from \ref controls::AeExposureModeNameValueMap. Each mode dict should * contain an array of exposure times with the key "exposureTime" and an array - * of gain values with the key "gain", in this format: + * of gain values with the key "gain" and an optional array of digital gain + * values with the key "digitalGain" in this format: * * \code{.unparsed} * algorithms: @@ -434,9 +447,12 @@ int parseExposureModes(AgcMeanLuminance::Configuration &config, const ValueNode * Short: * exposureTime: [ 100, 10000, 30000, 60000, 120000 ] * gain: [ 2.0, 4.0, 6.0, 8.0, 10.0 ] + * digitalGain: [ 1.0, 2.0, 3.0, 4.0, 6.0 ] * * \endcode * + * If there is no entry for digital gain, the digital gain will be fixed to 1.0. + * * \return 0 on success or a negative error code */ std::optional @@ -494,6 +510,8 @@ AgcMeanLuminance::AgcMeanLuminance(Configuration &&config) * ExposureModeHelper using an empty vector of stages. This will result * in the ExposureModeHelper simply driving the exposure time as high as * possible before touching gain. + * In contrast to analog gain, digital gain is mostly unlimited. As that + * results in very bad images, limit it to 16x by default. */ if (exposureModeHelpers_.empty()) exposureModeHelpers_.try_emplace( @@ -502,7 +520,7 @@ AgcMeanLuminance::AgcMeanLuminance(Configuration &&config) { .exposureTime = utils::Duration::max(), .gain = std::numeric_limits::max(), - .digitalGain = 1.0, + .digitalGain = 16.0, }, }); } @@ -531,9 +549,11 @@ void AgcMeanLuminance::configure(utils::Duration lineDuration, /** * \brief Set the ExposureModeHelper limits for this class * \param[in] minExposureTime Minimum exposure time to allow - * \param[in] maxExposureTime Maximum ewposure time to allow + * \param[in] maxExposureTime Maximum exposure time to allow * \param[in] minGain Minimum gain to allow * \param[in] maxGain Maximum gain to allow + * \param[in] minDigitalGain Minimum digital gain to allow + * \param[in] maxDigitalGain Maximum digital gain to allow * \param[in] constraints Additional constraints to apply * * This function calls \ref ExposureModeHelper::setLimits() for each @@ -542,11 +562,12 @@ void AgcMeanLuminance::configure(utils::Duration lineDuration, void AgcMeanLuminance::setLimits(utils::Duration minExposureTime, utils::Duration maxExposureTime, double minGain, double maxGain, + double minDigitalGain, double maxDigitalGain, std::vector constraints) { for (auto &[id, helper] : exposureModeHelpers_) helper.setLimits(minExposureTime, maxExposureTime, minGain, - maxGain, 1.0, 1.0); + maxGain, minDigitalGain, maxDigitalGain); additionalConstraints_ = std::move(constraints); } diff --git a/src/ipa/libipa/agc_mean_luminance.h b/src/ipa/libipa/agc_mean_luminance.h index ffd1f1e25a8d..52aaf44c2324 100644 --- a/src/ipa/libipa/agc_mean_luminance.h +++ b/src/ipa/libipa/agc_mean_luminance.h @@ -56,7 +56,8 @@ public: void configure(utils::Duration lineDuration, const CameraSensorHelper *sensorHelper); void setLimits(utils::Duration minExposureTime, utils::Duration maxExposureTime, - double minGain, double maxGain, std::vector constraints); + double minGain, double maxGain, double minDigitalGain, + double maxDigitalGain, std::vector constraints); const std::map> &constraintModes() const { From patchwork Tue Nov 17 08:47:14 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Klug X-Patchwork-Id: 28941 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 50B96BDE17 for ; Tue, 17 Nov 2026 08:47:28 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 025BA6A861; Tue, 17 Nov 2026 09:47:28 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="azktCdSE"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 50BEB6A814 for ; Tue, 17 Nov 2026 09:47:26 +0100 (CET) Received: from ideasonboard.com (unknown [IPv6:2a00:6020:448c:6c00:27fa:cb4b:1d83:9b64]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id E0005593; Tue, 17 Nov 2026 09:44:58 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1794905099; bh=wWsjj6+XJNcQJgloWGO1EBkuO/nud/E8KTF0sBG0YtY=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=azktCdSEzjCw8bPWRUVc5zv2T6F77L7IiX3C3UsRoE1ZeQBbZ9cJEMUMglBYNQrwF 6TO4dyv/px9Z+78Fd5JNdcjA9jFurytSiWfLOh9izQXOvp2K1Kzxrlt+753loDNapQ V5BDZHvUrqCGbckAZn8li0zDWk07bjOpRe65/G7s= From: Stefan Klug Date: Tue, 17 Nov 2026 09:47:14 +0100 Subject: [PATCH 04/10] libipa: agc: Rename agEnable to agMode MIME-Version: 1.0 Message-Id: <20261117-sklug-digital-gain-support-v1-v1-4-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 agEnable variable contains a mode. Name it accordingly for better readability. Signed-off-by: Stefan Klug --- src/ipa/libipa/agc.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/ipa/libipa/agc.cpp b/src/ipa/libipa/agc.cpp index 6ec37756c88e..b2c2feea5ed1 100644 --- a/src/ipa/libipa/agc.cpp +++ b/src/ipa/libipa/agc.cpp @@ -617,10 +617,10 @@ void AgcAlgorithm::queueRequest(const agc::Session &session, agc::ActiveState &s frameContext.autoExposureModeChange = true; } - const auto &agEnable = controls.get(controls::AnalogueGainMode); - if (agEnable && - (*agEnable == controls::AnalogueGainModeAuto) != state.autoGainEnabled) { - state.autoGainEnabled = (*agEnable == controls::AnalogueGainModeAuto); + const auto &agMode = controls.get(controls::AnalogueGainMode); + if (agMode && + (*agMode == controls::AnalogueGainModeAuto) != state.autoGainEnabled) { + state.autoGainEnabled = (*agMode == controls::AnalogueGainModeAuto); LOG(Agc, Debug) << (state.autoGainEnabled ? "Enabling" : "Disabling") From patchwork Tue Nov 17 08:47:15 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Klug X-Patchwork-Id: 28942 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 2CA27BDE17 for ; Tue, 17 Nov 2026 08:47:31 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id B4F506A887; Tue, 17 Nov 2026 09:47:30 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="SsnhAlmO"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id BB2456A871 for ; Tue, 17 Nov 2026 09:47:28 +0100 (CET) Received: from ideasonboard.com (unknown [IPv6:2a00:6020:448c:6c00:27fa:cb4b:1d83:9b64]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 52962593; Tue, 17 Nov 2026 09:45:01 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1794905101; bh=pGkUfcC16jPS73QqkAqKWT72vOTf6Tz8K2Js5CJbV0A=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=SsnhAlmOYXCc3qUlGiOfOyEXdkSrF8fjMmd08KrZEupYax6Zvh+Jk6e5DPZt3vtM7 fPf7hXW8/v9MyHty7Cgq2lvBSpW71u+MeGpxZt3S/w8z6508BP5nuKnP0/n90C7mVw p9RHJPggqlJKt4FZ4G6QPECbUZqB2nnrTpEQWC00= From: Stefan Klug Date: Tue, 17 Nov 2026 09:47:15 +0100 Subject: [PATCH 05/10] libipa: agc: Pass agc::Session to prepare() MIME-Version: 1.0 Message-Id: <20261117-sklug-digital-gain-support-v1-v1-5-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" In an upcoming change the prepare() function needs to access the session. Prepare for that by passing the session into AgcAlgorithm::prepare() without using it. Update the IPA implementations accordingly. Signed-off-by: Stefan Klug --- src/ipa/ipu3/algorithms/agc.cpp | 2 +- src/ipa/libipa/agc.cpp | 4 +++- src/ipa/libipa/agc.h | 3 ++- src/ipa/mali-c55/algorithms/agc.cpp | 2 +- src/ipa/rkisp1/algorithms/agc.cpp | 2 +- src/ipa/softisp/algorithms/agc.cpp | 2 +- 6 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/ipa/ipu3/algorithms/agc.cpp b/src/ipa/ipu3/algorithms/agc.cpp index ccf65da3174f..afe80dc06279 100644 --- a/src/ipa/ipu3/algorithms/agc.cpp +++ b/src/ipa/ipu3/algorithms/agc.cpp @@ -111,7 +111,7 @@ void Agc::prepare(IPAContext &context, [[maybe_unused]] const uint32_t frame, IPAFrameContext &frameContext, [[maybe_unused]] ipu3_uapi_params *params) { - agc_.prepare(context.activeState.agc, frameContext.agc); + agc_.prepare(context.configuration.agc, context.activeState.agc, frameContext.agc); } Histogram Agc::parseStatistics(const ipu3_uapi_stats_3a *stats, diff --git a/src/ipa/libipa/agc.cpp b/src/ipa/libipa/agc.cpp index b2c2feea5ed1..c8924f3c3fbb 100644 --- a/src/ipa/libipa/agc.cpp +++ b/src/ipa/libipa/agc.cpp @@ -695,6 +695,7 @@ void AgcAlgorithm::queueRequest(const agc::Session &session, agc::ActiveState &s /** * \brief Prepare a frame + * \param[in] session The agc session configuration * \param[in] state The agc active state * \param[in] frameContext The agc frame context * @@ -708,7 +709,8 @@ void AgcAlgorithm::queueRequest(const agc::Session &session, agc::ActiveState &s * * \sa Algorithm::prepare() */ -void AgcAlgorithm::prepare(agc::ActiveState &state, agc::FrameContext &frameContext) +void AgcAlgorithm::prepare([[maybe_unused]] const agc::Session &session, agc::ActiveState &state, + agc::FrameContext &frameContext) { uint32_t activeAutoExposure = state.automatic.exposure; double activeAutoGain = state.automatic.gain; diff --git a/src/ipa/libipa/agc.h b/src/ipa/libipa/agc.h index bcd42f621ee8..9b4bcf0bed1c 100644 --- a/src/ipa/libipa/agc.h +++ b/src/ipa/libipa/agc.h @@ -148,7 +148,8 @@ public: void queueRequest(const agc::Session &session, agc::ActiveState &state, agc::FrameContext &frameContext, const ControlList &controls); - void prepare(agc::ActiveState &state, agc::FrameContext &frameContext); + void prepare(const agc::Session &session, agc::ActiveState &state, + agc::FrameContext &frameContext); void process(const agc::Session &session, agc::ActiveState &state, agc::FrameContext &frameContext, std::optional &¶ms, diff --git a/src/ipa/mali-c55/algorithms/agc.cpp b/src/ipa/mali-c55/algorithms/agc.cpp index 2c747da89170..5684b4df954c 100644 --- a/src/ipa/mali-c55/algorithms/agc.cpp +++ b/src/ipa/mali-c55/algorithms/agc.cpp @@ -207,7 +207,7 @@ void Agc::fillWeightsArrayBuffer(MaliC55Params *params, const enum MaliC55Blocks void Agc::prepare(IPAContext &context, const uint32_t frame, IPAFrameContext &frameContext, MaliC55Params *params) { - agc_.prepare(context.activeState.agc, frameContext.agc); + agc_.prepare(context.configuration.agc, context.activeState.agc, frameContext.agc); if (frame > 0) return; diff --git a/src/ipa/rkisp1/algorithms/agc.cpp b/src/ipa/rkisp1/algorithms/agc.cpp index 1c323ede87bc..d82bb2ece24a 100644 --- a/src/ipa/rkisp1/algorithms/agc.cpp +++ b/src/ipa/rkisp1/algorithms/agc.cpp @@ -209,7 +209,7 @@ void Agc::queueRequest(IPAContext &context, void Agc::prepare(IPAContext &context, const uint32_t frame, IPAFrameContext &frameContext, RkISP1Params *params) { - agc_.prepare(context.activeState.agc, frameContext.agc); + agc_.prepare(context.configuration.agc, context.activeState.agc, frameContext.agc); if (context.configuration.compress.supported) { frameContext.compress.enable = true; diff --git a/src/ipa/softisp/algorithms/agc.cpp b/src/ipa/softisp/algorithms/agc.cpp index ea234bc2ae4b..7d2a53b38576 100644 --- a/src/ipa/softisp/algorithms/agc.cpp +++ b/src/ipa/softisp/algorithms/agc.cpp @@ -74,7 +74,7 @@ void Agc::queueRequest(IPAContext &context, [[maybe_unused]] const uint32_t fram void Agc::prepare(IPAContext &context, [[maybe_unused]] const uint32_t frame, IPAFrameContext &frameContext, [[maybe_unused]] DebayerParams *params) { - agc_.prepare(context.activeState.agc, frameContext.agc); + agc_.prepare(context.configuration.agc, context.activeState.agc, frameContext.agc); } void Agc::process(IPAContext &context, From patchwork Tue Nov 17 08:47:16 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Klug X-Patchwork-Id: 28943 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 17B39BDE17 for ; Tue, 17 Nov 2026 08:47:34 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id BA20F6A873; Tue, 17 Nov 2026 09:47:33 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="Mdu7+7jM"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id B626C6A873 for ; Tue, 17 Nov 2026 09:47:31 +0100 (CET) Received: from ideasonboard.com (unknown [IPv6:2a00:6020:448c:6c00:27fa:cb4b:1d83:9b64]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 11428593; Tue, 17 Nov 2026 09:45:04 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1794905104; bh=hcd8Du+gCW0AQNpP6suv2q4ty7AAqI6EyCQSWdSOc7g=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=Mdu7+7jM0SAQupTDC2w5etJLIV4pxQoIu+JP7rNGNnqGeJtGSFw9OZbbwI5plKCXK y1ptN6AeWwR1M2XGn+yR+I5Sk3DxfqxG5LX2tRipgjF/RAlYA8aghLYtk77lOaWGER Db7VFRPv4hx4qtRQsgnuzYP83ZjVxLFT7VvZ798A= From: Stefan Klug Date: Tue, 17 Nov 2026 09:47:16 +0100 Subject: [PATCH 06/10] libipa: agc: Implement DigitalGainMode support MIME-Version: 1.0 Message-Id: <20261117-sklug-digital-gain-support-v1-v1-6-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" Implement DigitalGainMode support for the agc algorithm. By default DigitalGainMode is set to Auto. For digital gain to be enabled the configuration passed to AgcAlgorithm::configure() must contain a maxDigitalGain value > 1 and the tuning file must also contain a maxDigitalGain value > 1. Signed-off-by: Stefan Klug --- src/ipa/libipa/agc.cpp | 91 ++++++++++++++++++++++++++++++++++++++++++++++++-- src/ipa/libipa/agc.h | 7 ++++ 2 files changed, 96 insertions(+), 2 deletions(-) diff --git a/src/ipa/libipa/agc.cpp b/src/ipa/libipa/agc.cpp index c8924f3c3fbb..f680bb93f8ed 100644 --- a/src/ipa/libipa/agc.cpp +++ b/src/ipa/libipa/agc.cpp @@ -380,6 +380,7 @@ int AgcAlgorithm::configure(agc::Session &session, agc::ActiveState &state, session.autoAllowed = config.autoAllowed; session.lineDuration = lineLength * 1.0s / config.sensorInfo.pixelRate; session.sensor.outputSize = config.sensorInfo.outputSize; + session.maxDigitalGain = config.maxDigitalGain; auto exposureMargin = sensor_ ? sensor_->exposureMargin() : std::nullopt; session.sensor.exposureMargin = exposureMargin.value_or(4); @@ -455,8 +456,10 @@ int AgcAlgorithm::configure(agc::Session &session, agc::ActiveState &state, state.automatic.digitalGain = 1; state.manual.gain = state.automatic.gain; state.manual.exposure = state.automatic.exposure; + state.manual.digitalGain = 1.0; state.autoExposureEnabled = session.autoAllowed; state.autoGainEnabled = session.autoAllowed; + state.autoDigitalGainEnabled = session.autoAllowed; state.exposureValue = 0; state.minFrameDuration = session.minFrameDuration; state.maxFrameDuration = session.maxFrameDuration; @@ -504,6 +507,14 @@ int AgcAlgorithm::configure(agc::Session &session, agc::ActiveState &state, return; config.ctrlMap[&controls::ExposureValue] = ControlInfo(-8.0f, 8.0f, 0.0f); + if (session.maxDigitalGain > 1.0) { + config.ctrlMap[&controls::DigitalGain] = + ControlInfo(1.0f, static_cast(session.maxDigitalGain), 1.0f); + config.ctrlMap[&controls::DigitalGainMode] = + ControlInfo({ { ControlValue(controls::DigitalGainModeAuto), + ControlValue(controls::DigitalGainModeManual) } }, + ControlValue(controls::DigitalGainModeAuto)); + } std::vector options; for (const auto &[id, _] : impl.constraintModes()) @@ -634,6 +645,23 @@ void AgcAlgorithm::queueRequest(const agc::Session &session, agc::ActiveState &s if (!state.autoGainEnabled && !controls.get(controls::AnalogueGain)) frameContext.autoGainModeChange = true; } + + const auto &dgMode = controls.get(controls::DigitalGainMode); + if (dgMode && + (*dgMode == controls::DigitalGainModeAuto) != state.autoDigitalGainEnabled) { + state.autoDigitalGainEnabled = (*dgMode == controls::AnalogueGainModeAuto); + + LOG(Agc, Debug) + << (state.autoDigitalGainEnabled ? "Enabling" : "Disabling") + << " AGC (digital gain)"; + /* + * If we go from auto -> manual with no manual control + * set, use the last computed value, which we don't + * know until prepare() so save this information. + */ + if (!state.autoDigitalGainEnabled && !controls.get(controls::DigitalGain)) + frameContext.autoDigitalGainModeChange = true; + } } const auto &frameDurationLimits = controls.get(controls::FrameDurationLimits); @@ -664,14 +692,31 @@ void AgcAlgorithm::queueRequest(const agc::Session &session, agc::ActiveState &s LOG(Agc, Debug) << "Set gain to " << state.manual.gain; } + const auto &digitalGain = controls.get(controls::DigitalGain); + if (digitalGain && !state.autoDigitalGainEnabled) { + if (session.maxDigitalGain > 1.0) { + state.manual.digitalGain = *digitalGain; + LOG(Agc, Debug) << "Set digital gain to " << state.manual.digitalGain; + } else { + LOG(Agc, Warning) << "DigitalGain is not supported"; + } + } + frameContext.autoExposureEnabled = state.autoExposureEnabled; frameContext.autoGainEnabled = state.autoGainEnabled; + frameContext.autoDigitalGainEnabled = state.autoDigitalGainEnabled; if (!frameContext.autoExposureEnabled) frameContext.exposure = clampExposure(session, frameContext, state.manual.exposure); + if (!frameContext.autoGainEnabled) frameContext.gain = state.manual.gain; + if (frameContext.autoDigitalGainEnabled) + frameContext.digitalGain = state.automatic.digitalGain; + else + frameContext.digitalGain = state.manual.digitalGain; + if (!frameContext.autoExposureEnabled && !frameContext.autoGainEnabled) frameContext.quantizationGain = 1.0; @@ -714,7 +759,13 @@ void AgcAlgorithm::prepare([[maybe_unused]] const agc::Session &session, agc::Ac { uint32_t activeAutoExposure = state.automatic.exposure; double activeAutoGain = state.automatic.gain; - double activeAutoQGain = state.automatic.quantizationGain; + double activeAutoQGain = 1.0; + double activeAutoDigitalGain = 1.0; + + if (session.maxDigitalGain > 1.0) { + activeAutoQGain = state.automatic.quantizationGain; + activeAutoDigitalGain = state.automatic.digitalGain; + } /* Populate exposure and gain in auto mode */ if (frameContext.autoExposureEnabled) { @@ -725,6 +776,8 @@ void AgcAlgorithm::prepare([[maybe_unused]] const agc::Session &session, agc::Ac frameContext.gain = activeAutoGain; frameContext.quantizationGain = activeAutoQGain; } + if (frameContext.autoDigitalGainEnabled) + frameContext.digitalGain = activeAutoDigitalGain; /* * Populate manual exposure and gain from the active auto values when @@ -733,12 +786,17 @@ void AgcAlgorithm::prepare([[maybe_unused]] const agc::Session &session, agc::Ac if (!frameContext.autoExposureEnabled && frameContext.autoExposureModeChange) { state.manual.exposure = activeAutoExposure; frameContext.exposure = activeAutoExposure; + frameContext.quantizationGain = activeAutoQGain; } if (!frameContext.autoGainEnabled && frameContext.autoGainModeChange) { state.manual.gain = activeAutoGain; frameContext.gain = activeAutoGain; frameContext.quantizationGain = activeAutoQGain; } + if (!frameContext.autoDigitalGainEnabled && frameContext.autoDigitalGainModeChange) { + state.manual.gain = activeAutoDigitalGain; + frameContext.digitalGain = activeAutoDigitalGain; + } frameContext.yTarget = state.automatic.yTarget; } @@ -791,6 +849,8 @@ void AgcAlgorithm::process(const agc::Session &session, agc::ActiveState &state, session.sensor.exposureMargin * lineDuration; double minAnalogueGain; double maxAnalogueGain; + double minDigitalGain; + double maxDigitalGain; if (!frameContext.autoExposureEnabled) { minExposureTime = std::clamp( @@ -807,6 +867,14 @@ void AgcAlgorithm::process(const agc::Session &session, agc::ActiveState &state, maxAnalogueGain = frameContext.gain; } + if (frameContext.autoDigitalGainEnabled) { + minDigitalGain = 1.0; + maxDigitalGain = session.maxDigitalGain; + } else { + minDigitalGain = frameContext.digitalGain; + maxDigitalGain = frameContext.digitalGain; + } + std::visit(utils::overloaded{ [&](AgcMSV& impl) { impl.setLimits({ @@ -842,9 +910,18 @@ void AgcAlgorithm::process(const agc::Session &session, agc::ActiveState &state, utils::Duration effectiveExposureValue = lineDuration * params->exposure * params->gain; + /* + * Include the quantization gain if it was applied. Do not use + * compress.gain because it will include gains that shall not be + * reported to the user when HDR is implemented. + */ + if (session.maxDigitalGain > 1.0) + effectiveExposureValue *= frameContext.quantizationGain + * frameContext.digitalGain; + impl.setLimits(minExposureTime, maxExposureTime, minAnalogueGain, maxAnalogueGain, - 1.0, 1.0, + minDigitalGain, maxDigitalGain, std::move(params->additionalConstraints)); const auto &newEv = impl.calculateNewEv({ @@ -932,6 +1009,16 @@ void AgcAlgorithm::fillMetadata(const agc::Session &session, metadata.set(controls::AeConstraintMode, frameContext.constraintMode); metadata.set(controls::ExposureValue, frameContext.exposureValue); } + + /* + * Add the quantization gain to the digital gain metadata, so that + * we don't apply any hidden gains. + * \todo This might be unexpected for applications trying to match a + * digital gain control with the metadata, as it will always be off by + * the quantization gain. + */ + metadata.set(controls::DigitalGain, + frameContext.quantizationGain * frameContext.digitalGain); } } /* namespace ipa */ diff --git a/src/ipa/libipa/agc.h b/src/ipa/libipa/agc.h index 9b4bcf0bed1c..c4e0cb6be8a4 100644 --- a/src/ipa/libipa/agc.h +++ b/src/ipa/libipa/agc.h @@ -41,6 +41,7 @@ struct Session { double minAnalogueGain; double maxAnalogueGain; double defAnalogueGain; + double maxDigitalGain; utils::Duration minFrameDuration; utils::Duration maxFrameDuration; utils::Duration lineDuration; @@ -57,6 +58,7 @@ struct ActiveState { struct { uint32_t exposure; double gain; + double digitalGain; } manual; struct { uint32_t exposure; @@ -68,6 +70,7 @@ struct ActiveState { bool autoExposureEnabled; bool autoGainEnabled; + bool autoDigitalGainEnabled; double exposureValue; controls::AeConstraintModeEnum constraintMode; controls::AeExposureModeEnum exposureMode; @@ -79,11 +82,13 @@ struct FrameContext { uint32_t exposure; double gain; double quantizationGain; + double digitalGain; double exposureValue; double yTarget; uint32_t vblank; bool autoExposureEnabled; bool autoGainEnabled; + bool autoDigitalGainEnabled; controls::AeConstraintModeEnum constraintMode; controls::AeExposureModeEnum exposureMode; utils::Duration minFrameDuration; @@ -91,6 +96,7 @@ struct FrameContext { utils::Duration frameDuration; bool autoExposureModeChange; bool autoGainModeChange; + bool autoDigitalGainModeChange; }; [[nodiscard]] @@ -128,6 +134,7 @@ public: const ControlInfoMap &sensorControls; ControlInfoMap::Map &ctrlMap; bool autoAllowed = true; + double maxDigitalGain = 1.0; }; struct ProcessParams { From patchwork Tue Nov 17 08:47:17 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Klug X-Patchwork-Id: 28944 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 B610FBDE17 for ; Tue, 17 Nov 2026 08:47:35 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 680026A886; Tue, 17 Nov 2026 09:47:35 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="kg7t7GoS"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id EBCC76A883 for ; Tue, 17 Nov 2026 09:47:33 +0100 (CET) Received: from ideasonboard.com (unknown [IPv6:2a00:6020:448c:6c00:27fa:cb4b:1d83:9b64]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 82D12593; Tue, 17 Nov 2026 09:45:06 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1794905106; bh=VqhEcmHADJXpPkEN5HfwcIxTQ7kSwlu3+4wBOLc7f4E=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=kg7t7GoSVOfK51vrvDmtC5Hs2eG7oZ0ie3l3Xtg9bXVt51c4CE9EgvI4V8DAvQLTj S8yKcfgd25rj3IWtYA9a4rtPcbzgiapw18+6PloN5DAM12nkDzy3spZzfeHf/AgKym 1qI7YshNGZnGji+C7tbZzfYW6SquPYUyDvCEl8Aw= From: Stefan Klug Date: Tue, 17 Nov 2026 09:47:17 +0100 Subject: [PATCH 07/10] ipa: rkisp1: agc: Add digital gain support MIME-Version: 1.0 Message-Id: <20261117-sklug-digital-gain-support-v1-v1-7-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" Add digital gain support by using the compress modules to apply gain. Note: the Compress module must be placed *after* the Agc module in the tuning file for this to work correctly. Eventually this constraint should be modeled in code. Signed-off-by: Stefan Klug --- src/ipa/rkisp1/algorithms/agc.cpp | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/ipa/rkisp1/algorithms/agc.cpp b/src/ipa/rkisp1/algorithms/agc.cpp index d82bb2ece24a..771128fc3d4c 100644 --- a/src/ipa/rkisp1/algorithms/agc.cpp +++ b/src/ipa/rkisp1/algorithms/agc.cpp @@ -39,6 +39,17 @@ LOG_DEFINE_CATEGORY(RkISP1Agc) * \brief A mean-based auto-exposure algorithm */ +namespace { + +/* + * The maximum digital gain that the compress algorithm can supply. This is + * a workaround as we don't have support for inter-algorithm dependencies. + * \todo Implement using proper dependencies + */ +constexpr double kAgcMaxDigitalGain = 64.0; + +} /* namespace */ + int Agc::parseMeteringModes(IPAContext &context, const ValueNode &tuningData) { if (!tuningData.isDictionary()) @@ -167,6 +178,16 @@ int Agc::configure(IPAContext &context, const IPACameraSensorInfo &configInfo) .sensorControls = context.sensorControls, .ctrlMap = context.ctrlMap, .autoAllowed = !context.configuration.raw, + /* + * \todo The Compress algo must come after Agc in the tuning + * file to pick up the digital gain in prepare(). But as it is + * after Agc we can't query + * context.configuration.compress.supported here as the Compress + * algo is not configured yet. Therefore enable digital gain + * unconditionally for now. It won't effectively be enabled if + * the tuning file doesn't contain a maxDigitalGain > 1. + */ + .maxDigitalGain = kAgcMaxDigitalGain }); if (ret) return ret; @@ -213,7 +234,7 @@ void Agc::prepare(IPAContext &context, const uint32_t frame, if (context.configuration.compress.supported) { frameContext.compress.enable = true; - frameContext.compress.gain = frameContext.agc.quantizationGain; + frameContext.compress.gain = frameContext.agc.quantizationGain * frameContext.agc.digitalGain; } if (frame > 0 && !frameContext.agc.updateMetering) From patchwork Tue Nov 17 08:47:18 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Klug X-Patchwork-Id: 28945 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 70EBEBDE17 for ; Tue, 17 Nov 2026 08:47:38 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 133FB6A883; Tue, 17 Nov 2026 09:47:38 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="hQQhaTDj"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 449CC6A883 for ; Tue, 17 Nov 2026 09:47:36 +0100 (CET) Received: from ideasonboard.com (unknown [IPv6:2a00:6020:448c:6c00:27fa:cb4b:1d83:9b64]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id C45E71CF6; Tue, 17 Nov 2026 09:45:08 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1794905108; bh=9S9h/8RtGiYrAI6teD4ctFpnxK1rhuoL/SbsomYm9zM=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=hQQhaTDj6Z8BmLVJ2GKV130JUKzlp5D9OOhHgeTzR+wx5qd3uBZdkwSsRjX6XWvz9 hU2mYu//RuXNY7y3zBmspjGIwiLn+Fs8ajUh+Ps5VoBgcGKQgoW2NZT4QEqIhs1NOf 8z6XrsXbDQIKhQPd4WuH187oT/+d0sXUAOBGsngQ= From: Stefan Klug Date: Tue, 17 Nov 2026 09:47:18 +0100 Subject: [PATCH 08/10] RFC ipa: libipa: Inject the registered name into an algorithm MIME-Version: 1.0 Message-Id: <20261117-sklug-digital-gain-support-v1-v1-8-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" When algorithms are handled it is helpful to be able to deduce the name an algorithm was registered with from the algorithm instance. Allow that by injecting the name into the instance after construction. ToDo: - We could as well force every algorithm to implement name() and could then drop this clunky logic. But maybe there are other reasons for this kind of registry. Signed-off-by: Stefan Klug --- src/ipa/libipa/algorithm.h | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/ipa/libipa/algorithm.h b/src/ipa/libipa/algorithm.h index 4ddb16ef3920..b47765700317 100644 --- a/src/ipa/libipa/algorithm.h +++ b/src/ipa/libipa/algorithm.h @@ -18,6 +18,9 @@ class ValueNode; namespace ipa { +template +class AlgorithmFactoryBase; + template class Algorithm { @@ -59,6 +62,15 @@ public: [[maybe_unused]] ControlList &metadata) { } + + const std::string &name() const + { + return name_; + } + +private: + std::string name_; + friend class AlgorithmFactoryBase<_Module>; }; template @@ -77,6 +89,11 @@ public: virtual std::unique_ptr> create() const = 0; + void injectName(Algorithm<_Module> *instance) const + { + instance->name_ = name(); + } + private: std::string name_; }; @@ -94,7 +111,9 @@ public: std::unique_ptr> create() const override { - return std::make_unique<_Algorithm>(); + auto instance = std::make_unique<_Algorithm>(); + AlgorithmFactoryBase::injectName(instance.get()); + return instance; } }; From patchwork Tue Nov 17 08:47:19 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Klug X-Patchwork-Id: 28946 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 22A27BDE17 for ; Tue, 17 Nov 2026 08:47:40 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id C59A06A897; Tue, 17 Nov 2026 09:47:39 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="pBypnewz"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 321FA6A88C for ; Tue, 17 Nov 2026 09:47:38 +0100 (CET) Received: from ideasonboard.com (unknown [IPv6:2a00:6020:448c:6c00:27fa:cb4b:1d83:9b64]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id B89EE1CF6; Tue, 17 Nov 2026 09:45:10 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1794905110; bh=gTwJcHfkj8iBMBUuWyEhhNk+v3a8Ar3HDUBfHmkHdzI=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=pBypnewz8taCBNGqK0qUpZpCUCCI0ncphhDFvivavUkd6OoFqhggF45AQKdCz0LBi 1sUcltyMRZQVDOAsu9xlEVuzZWW7w89h4ANAOG+FWjoMec9Kdt52yWl6VnskSwnS7p 23o2j5hWUcrEiA3Vab6GothDfCfV8yKmsg0xmKCo= From: Stefan Klug Date: Tue, 17 Nov 2026 09:47:19 +0100 Subject: [PATCH 09/10] RFC ipa: libipa: Implement algorithm dependencies MIME-Version: 1.0 Message-Id: <20261117-sklug-digital-gain-support-v1-v1-9-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" Some algorithms have ordering dependencies on each other. As an example on rkisp1 the Compress algorithm is used to implement digital gain. So during computeParams(), Agc::prepare() must be called before Compress::prepare(), so that the Agc algorithm can store the digital gain in the frame context for Compress to pick it up. To make matters worse, the required order during configure() time can be inverse. In the example above, Compress::configure() should be called first, so that Agc::configure() can check the session if Compress (and therefore digital gain) is available. This patch implements a simple, yet effective dependency scheme where an algorithm can specify a list of dependencies. A dependency consists of an algorithm step (Init, Configure, Queue, Prepare, Process), a target and the type which can either be "Exists" meaning that the corresponding algorithm must be in the tuning file and "Before" which means that the target must be run first. Each algorithm can provide a list of dependencies through the newly added dependencies() function. This patch only implements the necessary infrastructure for rkisp1. Necessary dependencies will be added in upcoming patches. Signed-off-by: Stefan Klug --- src/ipa/libipa/algorithm.cpp | 22 ++++++ src/ipa/libipa/algorithm.h | 48 ++++++++++++ src/ipa/libipa/module.h | 178 +++++++++++++++++++++++++++++++++++-------- src/ipa/rkisp1/rkisp1.cpp | 16 ++-- 4 files changed, 225 insertions(+), 39 deletions(-) diff --git a/src/ipa/libipa/algorithm.cpp b/src/ipa/libipa/algorithm.cpp index 757ce3519652..20ead51bf7e0 100644 --- a/src/ipa/libipa/algorithm.cpp +++ b/src/ipa/libipa/algorithm.cpp @@ -16,6 +16,28 @@ namespace libcamera { namespace ipa { +std::ostream &operator<<(std::basic_ostream &out, libcamera::ipa::AlgorithmStep step) +{ + switch (step) { + case AlgorithmStep::Init: + out << "Init"; + break; + case AlgorithmStep::Configure: + out << "Configure"; + break; + case AlgorithmStep::Queue: + out << "Queue"; + break; + case AlgorithmStep::Prepare: + out << "Prepare"; + break; + case AlgorithmStep::Process: + out << "Process"; + break; + } + return out; +} + /** * \class Algorithm * \brief The base class for all IPA algorithms diff --git a/src/ipa/libipa/algorithm.h b/src/ipa/libipa/algorithm.h index b47765700317..97d7130a789d 100644 --- a/src/ipa/libipa/algorithm.h +++ b/src/ipa/libipa/algorithm.h @@ -7,6 +7,7 @@ #pragma once #include +#include #include #include @@ -18,6 +19,48 @@ class ValueNode; namespace ipa { +enum class AlgorithmStep { + Init = 0, + Configure, + Queue, + Prepare, + Process +}; + +std::ostream &operator<<(std::basic_ostream &out, libcamera::ipa::AlgorithmStep step); + +const std::array kAlgorithmAllSteps = { + AlgorithmStep::Init, + AlgorithmStep::Configure, + AlgorithmStep::Queue, + AlgorithmStep::Prepare, + AlgorithmStep::Process +}; + +struct AlgorithmDependency { + enum Type { + /* Step on target needs to come first */ + Before = 0x01, + /* Target must exist */ + Exist = 0x02 + }; + + std::string target; + AlgorithmStep step; + unsigned type = 0; + + bool operator<(const AlgorithmDependency &other) const + { + return (target < other.target) || + ((target == other.target) && + ((step < other.step) || + ((step == other.step) && + (type < other.type)))); + } +}; + +const std::set kNoDependencies = {}; + template class AlgorithmFactoryBase; @@ -63,6 +106,11 @@ public: { } + virtual const std::set &dependencies() const + { + return kNoDependencies; + } + const std::string &name() const { return name_; diff --git a/src/ipa/libipa/module.h b/src/ipa/libipa/module.h index 5644458bdf3d..cb2176dc37a6 100644 --- a/src/ipa/libipa/module.h +++ b/src/ipa/libipa/module.h @@ -7,8 +7,10 @@ #pragma once +#include #include #include +#include #include #include @@ -35,6 +37,7 @@ public: using Config = _Config; using Params = _Params; using Stats = _Stats; + using AlgorithmT = Algorithm; virtual ~Module() {} @@ -43,25 +46,76 @@ public: return algorithms_; } + const std::vector *> &algorithmsOrderedForStep(AlgorithmStep step) const + { + return algorithmsOrderedForStep_.at(step); + } + int createAlgorithms(Context &context, const ValueNode &algorithms) { const auto &list = algorithms.asList(); + std::map dataMap; + + utils::ScopeExitActions actions; + + actions += [this]() { + clearAlgorithms(); + }; for (const auto &[i, algo] : utils::enumerate(list)) { if (!algo.isDictionary()) { LOG(IPAModuleAlgo, Error) << "Invalid tuning data syntax for algorithm " << i; - algorithms_.clear(); return -EINVAL; } - int ret = createAlgorithm(context, algo); + const auto &[name, algoData] = *algo.asDict().begin(); + /* + * Optionally, algorithms can be disabled via the tuning file + * by including enabled: false as a parameter within the + * algorithm tuning data. This is not an error, so we return 0. + */ + if (!algoData["enabled"].get(true)) { + LOG(IPAModuleAlgo, Info) + << "Algorithm '" << name << "' disabled via tuning file"; + return 0; + } + + std::unique_ptr> instance = createAlgorithm(name); + if (!instance) { + LOG(IPAModuleAlgo, Error) + << "Algorithm '" << name << "' not found"; + return -EINVAL; + } + + algorithms_.push_back(std::move(instance)); + + LOG(IPAModuleAlgo, Debug) + << "Instantiated algorithm '" << name << "'"; + + dataMap[algorithms_.back().get()] = &algoData; + } + + for (auto step : kAlgorithmAllSteps) { + std::vector algos; + int ret = createOrderedList(algos, step); + if (ret) + return ret; + + algorithmsOrderedForStep_[step] = std::move(algos); + } + + for (const auto &a : algorithmsOrderedForStep(AlgorithmStep::Init)) { + int ret = a->init(context, *dataMap[a]); if (ret) { - algorithms_.clear(); + LOG(IPAModuleAlgo, Error) + << "Algorithm failed to initialize"; return ret; } } + actions.release(); + return 0; } @@ -71,42 +125,101 @@ public: } private: - int createAlgorithm(Context &context, const ValueNode &data) + int createOrderedList(std::vector *> &orderedList, + AlgorithmStep step) { - const auto &[name, algoData] = *data.asDict().begin(); + using Algo = Algorithm; + std::set handledAlgorithms; - /* - * Optionally, algorithms can be disabled via the tuning file - * by including enabled: false as a parameter within the - * algorithm tuning data. This is not an error, so we return 0. - */ - if (!algoData["enabled"].get(true)) { - LOG(IPAModuleAlgo, Info) - << "Algorithm '" << name << "' disabled via tuning file"; - return 0; + orderedList.clear(); + + for (const auto &a : algorithms()) { + std::vector resolutionStack; + int ret = addToOrderedList(a.get(), orderedList, + resolutionStack, + step); + if (ret) + return ret; } - std::unique_ptr> algo = createAlgorithm(name); - if (!algo) { - LOG(IPAModuleAlgo, Error) - << "Algorithm '" << name << "' not found"; - return -EINVAL; - } - - int ret = algo->init(context, algoData); - if (ret) { - LOG(IPAModuleAlgo, Error) - << "Algorithm '" << name << "' failed to initialize"; - return ret; - } - - LOG(IPAModuleAlgo, Debug) - << "Instantiated algorithm '" << name << "'"; - - algorithms_.push_back(std::move(algo)); return 0; } + int addToOrderedList(Algorithm *algo, + std::vector *> &orderedList, + std::vector &resolutionStack, + AlgorithmStep step) + { + /* nothing to do if it was added already due to dependency resolution */ + if (std::find(orderedList.begin(), orderedList.end(), algo) != orderedList.end()) + return 0; + + auto &deps = algo->dependencies(); + + /* First add the dependent ones */ + for (auto &dep : deps) { + if (dep.step != step) + continue; + + if (!(dep.type & (AlgorithmDependency::Exist | AlgorithmDependency::Before))) { + LOG(IPAModuleAlgo, Error) + << "Unknown dependency flags '" + << dep.type + << "' on dependency '" << dep.target; + return -EINVAL; + } + + if (std::find(resolutionStack.begin(), resolutionStack.end(), dep.target) != resolutionStack.end()) { + LOG(IPAModuleAlgo, Info) + << "Circular dependency detected: " + << dep.target; + return -EINVAL; + } + + /* Find the target */ + auto iter = std::find_if(algorithms_.begin(), algorithms_.end(), + [&](auto &a) { return a->name() == dep.target; }); + if (iter == algorithms_.end()) { + if (dep.type & AlgorithmDependency::Exist) { + LOG(IPAModuleAlgo, Error) + << "Algorithm '" << dep.target + << "' is listed as dependency but not instantiated"; + return -EINVAL; + } + + if (dep.type & AlgorithmDependency::Before) { + LOG(IPAModuleAlgo, Debug) + << "Algorithm '" << dep.target + << "' is listed as optional dependency but not instantiated"; + continue; + } + + ASSERT(false); + } + + Algorithm *dependency = iter->get(); + if (std::find(orderedList.begin(), orderedList.end(), dependency) == orderedList.end()) { + resolutionStack.push_back(dep.target); + int ret = addToOrderedList(dependency, + orderedList, + resolutionStack, + step); + if (ret) + return ret; + } + } + + orderedList.push_back(algo); + LOG(IPAModuleAlgo, Debug) << "Step " << step << " add algo " << algo->name(); + return 0; + } + + void clearAlgorithms() + { + algorithmsOrderedForStep_.clear(); + algorithms_.clear(); + } + static std::unique_ptr> createAlgorithm(const std::string &name) { for (const AlgorithmFactoryBase *factory : factories()) { @@ -129,6 +242,7 @@ private: } std::list>> algorithms_; + std::map> algorithmsOrderedForStep_; }; } /* namespace ipa */ diff --git a/src/ipa/rkisp1/rkisp1.cpp b/src/ipa/rkisp1/rkisp1.cpp index cefaf40aee63..add2e45cfa36 100644 --- a/src/ipa/rkisp1/rkisp1.cpp +++ b/src/ipa/rkisp1/rkisp1.cpp @@ -30,6 +30,7 @@ #include "algorithms/algorithm.h" #include "libipa/agc.h" +#include "libipa/algorithm.h" #include "ipa_context.h" #include "params.h" @@ -237,8 +238,8 @@ int IPARkISP1::configure(const IPAConfigInfo &ipaConfig, return format.colourEncoding == PixelFormatInfo::ColourEncodingRAW; }); - for (const auto &a : algorithms()) { - Algorithm *algo = static_cast(a.get()); + for (const auto &a : algorithmsOrderedForStep(AlgorithmStep::Configure)) { + Algorithm *algo = static_cast(a); /* Disable algorithms that don't support raw formats. */ algo->disabled_ = context_.configuration.raw && !algo->supportsRaw_; @@ -290,8 +291,8 @@ void IPARkISP1::queueRequest(const uint32_t frame, const ControlList &controls) IPAFrameContext &frameContext = context_.frameContexts.alloc(frame); context_.debugMetadata.enableByControl(controls); - for (const auto &a : algorithms()) { - Algorithm *algo = static_cast(a.get()); + for (const auto &a : algorithmsOrderedForStep(AlgorithmStep::Queue)) { + Algorithm *algo = static_cast(a); if (algo->disabled_) continue; algo->queueRequest(context_, frame, frameContext, controls); @@ -305,8 +306,9 @@ void IPARkISP1::computeParams(const uint32_t frame, const uint32_t bufferId) RkISP1Params params(context_.configuration.paramFormat, mappedBuffers_.at(bufferId).planes()[0]); - for (const auto &algo : algorithms()) + for (const auto &algo : algorithmsOrderedForStep(AlgorithmStep::Prepare)) { algo->prepare(context_, frame, frameContext, ¶ms); + } paramsComputed.emit(frame, params.bytesused()); } @@ -330,8 +332,8 @@ void IPARkISP1::processStats(const uint32_t frame, const uint32_t bufferId, ControlList metadata(controls::controls); - for (const auto &a : algorithms()) { - Algorithm *algo = static_cast(a.get()); + for (const auto &a : algorithmsOrderedForStep(AlgorithmStep::Process)) { + Algorithm *algo = static_cast(a); if (algo->disabled_) continue; algo->process(context_, frame, frameContext, stats, metadata); From patchwork Tue Nov 17 08:47:20 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Klug X-Patchwork-Id: 28947 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 CD3E9BDE17 for ; Tue, 17 Nov 2026 08:47:41 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 8099D6A89A; Tue, 17 Nov 2026 09:47:41 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="C9KhC5RF"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 9F29E6A88C for ; Tue, 17 Nov 2026 09:47:40 +0100 (CET) Received: from ideasonboard.com (unknown [IPv6:2a00:6020:448c:6c00:27fa:cb4b:1d83:9b64]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 302C0593; Tue, 17 Nov 2026 09:45:13 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1794905113; bh=jYyqZEG5wQ0Dp5iyrlIqAlhDJa5DeWjcDCXY380JwIs=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=C9KhC5RF2b+e7QwTBZUtHjZEukPGEjrUsZfYylvLdZeNnx5fNgGF+LONyXDsE9Tai Ekhtz0+9Fke2Y+FZMEcz65IjNTJDmHdvuqKw/WZkd5QlrmZS9CfMOJpUu0balz0xkA 0tRYqjoFwmEdIWdBEPIPVy27LD0UkRp3WNbR47Pk= From: Stefan Klug Date: Tue, 17 Nov 2026 09:47:20 +0100 Subject: [PATCH 10/10] RFC rkisp1: Model dependencies between Compress and Agc MIME-Version: 1.0 Message-Id: <20261117-sklug-digital-gain-support-v1-v1-10-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" Compress::configure() must run before Agc::configure(), so that Agc can check if compress (and therefore digital gain) is available. Agc::prepare() must run before Compress::prepare(), so that Agc can store the correct digital gain for compress in the frame comntext. Model these dependency and remove the hardcoded enablement of digital gain. Signed-off-by: Stefan Klug --- src/ipa/rkisp1/algorithms/agc.cpp | 41 +++++++++++++++++++++------------- src/ipa/rkisp1/algorithms/agc.h | 2 ++ src/ipa/rkisp1/algorithms/compress.cpp | 17 ++++++++++++++ src/ipa/rkisp1/algorithms/compress.h | 2 ++ 4 files changed, 46 insertions(+), 16 deletions(-) diff --git a/src/ipa/rkisp1/algorithms/agc.cpp b/src/ipa/rkisp1/algorithms/agc.cpp index 771128fc3d4c..998e3dbfeaba 100644 --- a/src/ipa/rkisp1/algorithms/agc.cpp +++ b/src/ipa/rkisp1/algorithms/agc.cpp @@ -134,6 +134,18 @@ Agc::Agc() supportsRaw_ = true; } +const std::set &Agc::dependencies() const +{ + static const std::set dependencies = { + { + .target = "Compress", + .step = AlgorithmStep::Configure, + .type = AlgorithmDependency::Before, + } + }; + return dependencies; +} + /** * \brief Initialise the AGC algorithm from tuning files * \param[in] context The shared IPA context @@ -173,22 +185,19 @@ int Agc::init(IPAContext &context, const ValueNode &tuningData) */ int Agc::configure(IPAContext &context, const IPACameraSensorInfo &configInfo) { - int ret = agc_.configure(context.configuration.agc, context.activeState.agc, { - .sensorInfo = context.sensorInfo, - .sensorControls = context.sensorControls, - .ctrlMap = context.ctrlMap, - .autoAllowed = !context.configuration.raw, - /* - * \todo The Compress algo must come after Agc in the tuning - * file to pick up the digital gain in prepare(). But as it is - * after Agc we can't query - * context.configuration.compress.supported here as the Compress - * algo is not configured yet. Therefore enable digital gain - * unconditionally for now. It won't effectively be enabled if - * the tuning file doesn't contain a maxDigitalGain > 1. - */ - .maxDigitalGain = kAgcMaxDigitalGain - }); + int ret = agc_.configure( + context.configuration.agc, + context.activeState.agc, + { + .sensorInfo = context.sensorInfo, + .sensorControls = context.sensorControls, + .ctrlMap = context.ctrlMap, + .autoAllowed = !context.configuration.raw, + .maxDigitalGain = context.configuration.compress.supported + ? kAgcMaxDigitalGain + : 1.0, + }); + if (ret) return ret; diff --git a/src/ipa/rkisp1/algorithms/agc.h b/src/ipa/rkisp1/algorithms/agc.h index b8d59352e70d..9cee84bf6153 100644 --- a/src/ipa/rkisp1/algorithms/agc.h +++ b/src/ipa/rkisp1/algorithms/agc.h @@ -41,6 +41,8 @@ public: const rkisp1_stat_buffer *stats, ControlList &metadata) override; + virtual const std::set &dependencies() const override; + private: int parseMeteringModes(IPAContext &context, const ValueNode &tuningData); uint8_t computeHistogramPredivider(const Size &size, diff --git a/src/ipa/rkisp1/algorithms/compress.cpp b/src/ipa/rkisp1/algorithms/compress.cpp index 2afa449608b7..16077c29b21a 100644 --- a/src/ipa/rkisp1/algorithms/compress.cpp +++ b/src/ipa/rkisp1/algorithms/compress.cpp @@ -44,6 +44,23 @@ LOG_DEFINE_CATEGORY(RkISP1Compress) constexpr static int kRkISP1CompressInBits = 20; constexpr static int kRkISP1CompressOutBits = 12; +const std::set &Compress::dependencies() const +{ + static const std::set dependencies = { + { + .target = "Agc", + .step = AlgorithmStep::Prepare, + .type = AlgorithmDependency::Before, + }, + { + .target = "Agc", + .step = AlgorithmStep::Process, + .type = AlgorithmDependency::Before, + } + }; + return dependencies; +} + /** * \copydoc libcamera::ipa::Algorithm::configure */ diff --git a/src/ipa/rkisp1/algorithms/compress.h b/src/ipa/rkisp1/algorithms/compress.h index 87797b8ebcc5..31d516a31685 100644 --- a/src/ipa/rkisp1/algorithms/compress.h +++ b/src/ipa/rkisp1/algorithms/compress.h @@ -24,6 +24,8 @@ public: void prepare(IPAContext &context, const uint32_t frame, IPAFrameContext &frameContext, RkISP1Params *params) override; + + const std::set &dependencies() const override; }; } /* namespace ipa::rkisp1::algorithms */