From patchwork Thu Jul 23 15:43:05 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= X-Patchwork-Id: 27472 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 AF2BFC3308 for ; Thu, 23 Jul 2026 15:44:07 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 468C867F1D; Thu, 23 Jul 2026 17:44:07 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="aLtw0KBf"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id EEB0967EC5 for ; Thu, 23 Jul 2026 17:43:35 +0200 (CEST) Received: from pb-laptop.local (185.182.215.156.nat.pool.zt.hu [185.182.215.156]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 19DDC1C37 for ; Thu, 23 Jul 2026 17:42:35 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1784821355; bh=r7fms7akk/zwjiuHiG6GIK0QnaIjwx2y4+nqZtRnBa0=; h=From:To:Subject:Date:In-Reply-To:References:From; b=aLtw0KBfP2ruDa9VtCeEq4d5CQDtK04I2K0TWomdy9Y4VBCUFqxSnhnqLVvlEOnuK j4pCnd4ycj2qQ0EbgVIuB7rQoK+/waRpmbrSvc9DxDbWibEx+ubdtGJUHY06eMo+Lk 4A6eF6hZvmDXvpP3HGb1HQ8ZWTQGBefSvFsl2TlQ= From: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= To: libcamera-devel@lists.libcamera.org Subject: [RFC PATCH v2 22/43] ipa: libipa: agc: Clamp exposure value Date: Thu, 23 Jul 2026 17:43:05 +0200 Message-ID: <20260723154327.1357866-23-barnabas.pocze@ideasonboard.com> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20260723154327.1357866-1-barnabas.pocze@ideasonboard.com> References: <20260723154327.1357866-1-barnabas.pocze@ideasonboard.com> MIME-Version: 1.0 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" It is possible that the minimum exposure time in microseconds is not an integer. In that case the minimum `ExposureTime` in the `ControlInfo` will therefore be actually lower than the real minimum. This can cause issues: if the minimum published exposure time is set, the division when setting `agc.manual.exposure` might produce a value in [0;1), leading to the manual exposure being set to 0, leading to an assertion failure in `ExposureModeHelper::splitExposure()`. Note that this is theoretically possible even when the exposure time is determined automatically by `AgcMeanLuminance`. Store the true integer limits of the exposure as well, and ensure that the exposure (time) -> exposure (line) conversions always clamp. Signed-off-by: Barnabás Pőcze --- src/ipa/libipa/agc.cpp | 27 ++++++++++++++++++++++++--- src/ipa/libipa/agc.h | 2 ++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/ipa/libipa/agc.cpp b/src/ipa/libipa/agc.cpp index e16a02fdde..8a2525ec98 100644 --- a/src/ipa/libipa/agc.cpp +++ b/src/ipa/libipa/agc.cpp @@ -38,6 +38,12 @@ LOG_DEFINE_CATEGORY(Agc) * \struct agc::Session * \brief Session configuration for AgcAlgorithm * + * \var agc::Session::minExposure + * \brief Minimum exposure (in lines) supported with the configured sensor + * + * \var agc::Session::maxExposure + * \brief Maximum exposure (in lines) supported with the configured sensor + * * \var agc::Session::minExposureTime * \brief Minimum exposure time supported with the configured sensor * @@ -223,6 +229,19 @@ LOG_DEFINE_CATEGORY(Agc) * \brief Effective lux value of the frame */ +namespace { + +[[nodiscard]] uint32_t clampExposure(utils::Duration exposureTime, const agc::Session &session) +{ + return std::clamp( + exposureTime / session.lineDuration, + session.minExposure, + session.maxExposure + ); +} + +} /* namespace */ + /** * \brief Load tuning data */ @@ -317,6 +336,8 @@ int AgcAlgorithm::configure(agc::Session &session, agc::ActiveState &state, cons * * \todo take VBLANK into account for maximum exposure time */ + session.minExposure = minExposure; + session.maxExposure = maxExposure; session.minExposureTime = minExposure * session.lineDuration; session.maxExposureTime = maxExposure * session.lineDuration; session.minAnalogueGain = minGain; @@ -331,7 +352,7 @@ int AgcAlgorithm::configure(agc::Session &session, agc::ActiveState &state, cons /* Configure the default exposure and gain. */ state = {}; state.automatic.gain = session.minAnalogueGain; - state.automatic.exposure = 10ms / session.lineDuration; + state.automatic.exposure = clampExposure(10ms, session); state.automatic.quantizationGain = 1; state.automatic.yTarget = impl_.effectiveYTarget(0, 1); state.manual.gain = state.automatic.gain; @@ -437,7 +458,7 @@ void AgcAlgorithm::queueRequest(const agc::Session &session, agc::ActiveState &s const auto &exposure = controls.get(controls::ExposureTime); if (exposure && !state.autoExposureEnabled) { - state.manual.exposure = *exposure * 1.0us / session.lineDuration; + state.manual.exposure = clampExposure(*exposure * 1us, session); LOG(Agc, Debug) << "Set exposure to " << state.manual.exposure; @@ -599,7 +620,7 @@ void AgcAlgorithm::process(const agc::Session &session, agc::ActiveState &state, << ", " << newEv.quantizationGain << " and " << newEv.digitalGain; /* Update the estimated exposure and gain. */ - state.automatic.exposure = newEv.exposureTime / lineDuration; + state.automatic.exposure = clampExposure(newEv.exposureTime, session); state.automatic.gain = newEv.analogueGain; state.automatic.quantizationGain = newEv.quantizationGain; state.automatic.yTarget = newEv.yTarget; diff --git a/src/ipa/libipa/agc.h b/src/ipa/libipa/agc.h index 1eace12908..c11dbf80cd 100644 --- a/src/ipa/libipa/agc.h +++ b/src/ipa/libipa/agc.h @@ -49,6 +49,8 @@ prepareControls(ControlList &controls, const CameraSensorHelper *sensor, } struct Session { + uint32_t minExposure; + uint32_t maxExposure; utils::Duration minExposureTime; utils::Duration maxExposureTime; double minAnalogueGain;