From patchwork Mon Nov 16 17:49:06 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: 28923 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 EDC61C3360 for ; Mon, 16 Nov 2026 17:49:47 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 236556A87C; Mon, 16 Nov 2026 18:49:40 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="ElksbwuU"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id D97D66A84A for ; Mon, 16 Nov 2026 18:49:27 +0100 (CET) Received: from pb-laptop.local (185.221.140.49.nat.pool.zt.hu [185.221.140.49]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id DFEFF63C for ; Mon, 16 Nov 2026 18:47:00 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1794851221; bh=mjaJdBA5zv+MQbmG/h7w4lc1+yWYwsHwfFDAgJe3O4w=; h=From:To:Subject:Date:In-Reply-To:References:From; b=ElksbwuU1wZvhHKU3FnLBrvcvLIlO9qrCIHj06MPA8zTJcXv9AKB2PukjBcIoFYTe loXc+UDczFjtfPluGr6a0cMDg486YBlwlxXLb8ExTEyiuKI6mgtQ9cNxjVKJr6x1mI i/n/QhZfx0D0embznPGVpdbKXwk1utHDlXgI6J4w= From: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= To: libcamera-devel@lists.libcamera.org Subject: [PATCH v5 10/22] ipa: libipa: lux: Convert to "algorithm" Date: Mon, 16 Nov 2026 18:49:06 +0100 Message-ID: <20261116174919.1056403-11-barnabas.pocze@ideasonboard.com> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261116174919.1056403-1-barnabas.pocze@ideasonboard.com> References: <20261116174919.1056403-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" Bring the "Lux" type in-line with other generic algorithm implementations like `CcmAlgorithm`, `GammaAlgorithm`, etc. Signed-off-by: Barnabás Pőcze Reviewed-by: Kieran Bingham Reviewed-by: Jacopo Mondi --- src/ipa/libipa/lux.cpp | 136 +++++++++++++++++++++--------- src/ipa/libipa/lux.h | 36 ++++++-- src/ipa/rkisp1/algorithms/lux.cpp | 34 +++----- src/ipa/rkisp1/algorithms/lux.h | 4 +- src/ipa/rkisp1/ipa_context.h | 9 +- 5 files changed, 142 insertions(+), 77 deletions(-) diff --git a/src/ipa/libipa/lux.cpp b/src/ipa/libipa/lux.cpp index 12ba073a11..2d8c621e5c 100644 --- a/src/ipa/libipa/lux.cpp +++ b/src/ipa/libipa/lux.cpp @@ -3,22 +3,22 @@ * Copyright (C) 2019, Raspberry Pi Ltd * Copyright (C) 2024, Paul Elder * - * Helper class that implements lux estimation + * libIPA lux estimation */ #include "lux.h" -#include -#include - #include +#include +#include + #include "libcamera/internal/value_node.h" #include "histogram.h" /** * \file lux.h - * \brief Helper class that implements lux estimation + * \brief libipa implementation of lux estimation * * Estimating the lux level of an image is a common operation that can for * instance be used to adjust the target Y value in AGC or for Bayesian AWB @@ -33,51 +33,82 @@ LOG_DEFINE_CATEGORY(Lux) namespace ipa { +namespace lux { + /** - * \class Lux - * \brief Class that implements lux estimation + * \class ActiveState + * \brief The active state for the algorithm * - * IPAs that wish to use lux estimation should create a Lux algorithm module - * that lightly wraps this module by providing the platform-specific luminance - * histogram. The Lux entry in the tuning file must then precede the algorithms - * that depend on the estimated lux value. + * \var ActiveState::lux + * \brief The latest lux estimation */ /** - * \var Lux::referenceExposureTime_ + * \class FrameContext + * \brief The frame context for the algorithm + * + * \var FrameContext::lux + * \brief The estimated lux value for the given frame + */ + +} /* namespace lux */ + +/** + * \class LuxAlgorithm + * \brief Lux estimation algorithm + * + * This algorithm provides lux estimation based on tuning data. IPAs that wish to + * use lux estimation should create a Lux algorithm module that lightly wraps this + * module by providing the platform-specific luminance histogram. The Lux entry in + * the tuning file must then precede the algorithms that depend on the estimated lux + * value. + */ + +/** + * \class LuxAlgorithm::ProcessParams + * \brief Parameters for LuxAlgorithm::process() + * + * \var LuxAlgorithm::ProcessParams::exposureTime + * \brief Effective exposure time of the frame + * + * \var LuxAlgorithm::ProcessParams::analogueGain + * \brief Effective analogue gain of the frame + * + * \var LuxAlgorithm::ProcessParams::digitalGain + * \brief Effective digital gain of the frame + * + * \var LuxAlgorithm::ProcessParams::yHist + * \brief Luminance histogram of the frame + */ + +/** + * \var LuxAlgorithm::referenceExposureTime_ * \brief The exposure time of the reference image, in microseconds */ /** - * \var Lux::referenceAnalogueGain_ + * \var LuxAlgorithm::referenceAnalogueGain_ * \brief The analogue gain of the reference image */ /** - * \var Lux::referenceDigitalGain_ + * \var LuxAlgorithm::referenceDigitalGain_ * \brief The analogue gain of the reference image */ /** - * \var Lux::referenceY_ + * \var LuxAlgorithm::referenceY_ * \brief The measured luminance of the reference image, normalized to 1 * */ /** - * \var Lux::referenceLux_ + * \var LuxAlgorithm::referenceLux_ * \brief The estimated lux level of the reference image */ /** - * \brief Construct the Lux helper module - */ -Lux::Lux() -{ -} - -/** - * \brief Parse tuning data + * \brief Initialize algorithm and parse tuning data * \param[in] tuningData The ValueNode representing the tuning data * * This function parses tuning data for the common Lux module. It requires @@ -95,7 +126,7 @@ Lux::Lux() * * \return 0 on success or a negative error code */ -int Lux::parseTuningData(const ValueNode &tuningData) +int LuxAlgorithm::init(const ValueNode &tuningData) { auto value = tuningData["referenceExposureTime"].get(); if (!value) { @@ -141,25 +172,50 @@ int Lux::parseTuningData(const ValueNode &tuningData) } /** - * \brief Estimate lux given runtime values - * \param[in] exposureTime Exposure time applied to the frame - * \param[in] aGain Analogue gain applied to the frame - * \param[in] dGain Digital gain applied to the frame - * \param[in] yHist Histogram from the ISP statistics + * \param[in] state The active state + * \param[in] frameContext The frame context * - * Estimate the lux given the exposure time, gain, and histogram. + * This function finalizes \ref lux::FrameContext::lux "frameContext.lux" + * based on the latest estimation. + */ +void LuxAlgorithm::prepare(const lux::ActiveState &state, lux::FrameContext &frameContext) +{ + frameContext.lux = state.lux; +} + +/** + * \param[in] state The active state + * \param[in] frameContext The frame context + * \param[in] params The processing parameters + * \param[in] metadata The metadata * - * \return Estimated lux value + * This function updates the lux estimation based on the luminance histogram + * and other parameters of a completed frame, and additionally it also updates + * \a metadata for the frame. */ -double Lux::estimateLux(utils::Duration exposureTime, - double aGain, double dGain, - const Histogram &yHist) const +void LuxAlgorithm::process(lux::ActiveState &state, const lux::FrameContext &frameContext, + ControlList &metadata, std::optional &¶ms) +{ + /* + * Report the lux level used by algorithms to prepare this frame + * not the lux level *of* this frame. + */ + metadata.set(controls::Lux, frameContext.lux); + + /* If no statistics are available, keep the latest estimation. */ + if (!params) + return; + + state.lux = estimateLux(*params); +} + +double LuxAlgorithm::estimateLux(const ProcessParams ¶ms) const { - double currentY = yHist.interQuantileMean(0, 1); - double exposureTimeRatio = referenceExposureTime_ / exposureTime; - double aGainRatio = referenceAnalogueGain_ / aGain; - double dGainRatio = referenceDigitalGain_ / dGain; - double yRatio = (currentY / yHist.bins()) / referenceY_; + double currentY = params.yHist.interQuantileMean(0, 1); + double exposureTimeRatio = referenceExposureTime_ / params.exposureTime; + double aGainRatio = referenceAnalogueGain_ / params.analogueGain; + double dGainRatio = referenceDigitalGain_ / params.digitalGain; + double yRatio = (currentY / params.yHist.bins()) / referenceY_; double estimatedLux = exposureTimeRatio * aGainRatio * dGainRatio * yRatio * referenceLux_; diff --git a/src/ipa/libipa/lux.h b/src/ipa/libipa/lux.h index b6837ab729..5bce7855ad 100644 --- a/src/ipa/libipa/lux.h +++ b/src/ipa/libipa/lux.h @@ -8,27 +8,49 @@ #pragma once +#include + #include namespace libcamera { +class ControlList; class ValueNode; namespace ipa { class Histogram; -class Lux +namespace lux { + +struct ActiveState { + double lux; +}; + +struct FrameContext { + double lux; +}; + +} /* namespace lux */ + +class LuxAlgorithm { public: - Lux(); - - int parseTuningData(const ValueNode &tuningData); - double estimateLux(utils::Duration exposureTime, - double aGain, double dGain, - const Histogram &yHist) const; + struct ProcessParams { + utils::Duration exposureTime; + double analogueGain; + double digitalGain; + const Histogram &yHist; + }; + + int init(const ValueNode &tuningData); + void prepare(const lux::ActiveState &state, lux::FrameContext &frameContext); + void process(lux::ActiveState &state, const lux::FrameContext &frameContext, + ControlList &metadata, std::optional &¶ms); private: + double estimateLux(const ProcessParams ¶ms) const; + utils::Duration referenceExposureTime_; double referenceAnalogueGain_; double referenceDigitalGain_; diff --git a/src/ipa/rkisp1/algorithms/lux.cpp b/src/ipa/rkisp1/algorithms/lux.cpp index ce6928a55d..68ad41d6cf 100644 --- a/src/ipa/rkisp1/algorithms/lux.cpp +++ b/src/ipa/rkisp1/algorithms/lux.cpp @@ -31,19 +31,12 @@ namespace ipa::rkisp1::algorithms { * other algorithms (such as AGC) to use. */ -/** - * \brief Construct an rkisp1 Lux algo module - */ -Lux::Lux() -{ -} - /** * \copydoc libcamera::ipa::Algorithm::init */ int Lux::init([[maybe_unused]] IPAContext &context, const ValueNode &tuningData) { - return lux_.parseTuningData(tuningData); + return lux_.init(tuningData); } /** @@ -53,7 +46,7 @@ void Lux::prepare(IPAContext &context, [[maybe_unused]] const uint32_t frame, IPAFrameContext &frameContext, [[maybe_unused]] RkISP1Params *params) { - frameContext.lux.lux = context.activeState.lux.lux; + lux_.prepare(context.activeState.lux, frameContext.lux); } /** @@ -65,25 +58,24 @@ void Lux::process(IPAContext &context, const rkisp1_stat_buffer *stats, ControlList &metadata) { - /* - * Report the lux level used by algorithms to prepare this frame - * not the lux level *of* this frame. - */ - metadata.set(controls::Lux, frameContext.lux.lux); - - if (!stats) + if (!stats) { + lux_.process(context.activeState.lux, frameContext.lux, metadata, {}); return; - - utils::Duration exposureTime = context.configuration.agc.lineDuration * - frameContext.sensor.exposure; - double gain = frameContext.sensor.gain; + } /* \todo Deduplicate the histogram calculation from AGC */ const rkisp1_cif_isp_stat *params = &stats->params; Histogram yHist({ params->hist.hist_bins, context.hw.numHistogramBins }, [](uint32_t x) { return x >> 4; }); - context.activeState.lux.lux = lux_.estimateLux(exposureTime, gain, 1.0, yHist); + lux_.process(context.activeState.lux, frameContext.lux, metadata, + { { + .exposureTime = context.configuration.agc.lineDuration * + frameContext.sensor.exposure, + .analogueGain = frameContext.sensor.gain, + .digitalGain = 1, + .yHist = yHist, + } }); } REGISTER_IPA_ALGORITHM(Lux, "Lux") diff --git a/src/ipa/rkisp1/algorithms/lux.h b/src/ipa/rkisp1/algorithms/lux.h index 8cb35cbae2..f7d2581e57 100644 --- a/src/ipa/rkisp1/algorithms/lux.h +++ b/src/ipa/rkisp1/algorithms/lux.h @@ -20,8 +20,6 @@ namespace ipa::rkisp1::algorithms { class Lux : public Algorithm { public: - Lux(); - int init(IPAContext &context, const ValueNode &tuningData) override; void prepare(IPAContext &context, const uint32_t frame, IPAFrameContext &frameContext, @@ -32,7 +30,7 @@ public: ControlList &metadata) override; private: - ipa::Lux lux_; + ipa::LuxAlgorithm lux_; }; } /* namespace ipa::rkisp1::algorithms */ diff --git a/src/ipa/rkisp1/ipa_context.h b/src/ipa/rkisp1/ipa_context.h index a6db004c16..b34ea53406 100644 --- a/src/ipa/rkisp1/ipa_context.h +++ b/src/ipa/rkisp1/ipa_context.h @@ -32,6 +32,7 @@ #include "libipa/fixedpoint.h" #include "libipa/gamma.h" #include "libipa/lsc.h" +#include "libipa/lux.h" namespace libcamera { @@ -105,9 +106,7 @@ struct IPAActiveState { ipa::gamma::ActiveState gamma; - struct { - double lux; - } lux; + ipa::lux::ActiveState lux; struct { controls::WdrModeEnum mode; @@ -162,9 +161,7 @@ struct IPAFrameContext : public FrameContext { ipa::ccm::FrameContext ccm; - struct { - double lux; - } lux; + ipa::lux::FrameContext lux; struct { controls::WdrModeEnum mode;