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 {