From patchwork Mon Dec 16 09:49:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Elder X-Patchwork-Id: 22341 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 6F24BC32F9 for ; Mon, 16 Dec 2024 09:49:48 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 2910267F4C; Mon, 16 Dec 2024 10:49:48 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="KyLxdlVi"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 043DE67ECA for ; Mon, 16 Dec 2024 10:49:46 +0100 (CET) Received: from neptunite.flets-east.jp (unknown [IPv6:2404:7a81:160:2100:c11a:e451:861f:3d32]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 1F96F22F; Mon, 16 Dec 2024 10:49:07 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1734342549; bh=BLV+dkjia2FTYj0w45GpsmVB1QlijaLBzBoYRUil/YU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=KyLxdlVidrZRmM1HNE8dGJN4ViBq+r2BefFj/zCcbs/nO4ylJalxYQiIcsvvHDQZd 3iHFLJt8UbHAmn6WNb0BcNtHFd0Kd8jWejU7Y8+Gh+kqRI9TCPDNkHsW3Y/G+AKjQX 6+9/gdOeS975m9nr20+LN5/g73nQhyAeEraCmzFQ= From: Paul Elder To: libcamera-devel@lists.libcamera.org, laurent.pinchart@ideasonboard.com, stefan.klug@ideasonboard.com Cc: Paul Elder Subject: [PATCH v2 1/2] ipa: libipa: Add Lux helper Date: Mon, 16 Dec 2024 18:49:32 +0900 Message-Id: <20241216094933.198027-2-paul.elder@ideasonboard.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20241216094933.198027-1-paul.elder@ideasonboard.com> References: <20241216094933.198027-1-paul.elder@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" Add a Lux helper to libipa that does the estimation of the lux level given gain, exposure, and luminance histogram. The helper also handles reading the reference values from the tuning file. These are expected to be common operations of lux algorithm modules in IPAs, and is modeled/copied from Raspberry Pi. Signed-off-by: Paul Elder Reviewed-by: Laurent Pinchart --- Changes in v2: - improve documentation - add binSize member variable and corresponding setter - remove aperture - split gain into analogue and digital - change tuning file names into camel case --- src/ipa/libipa/lux.cpp | 183 +++++++++++++++++++++++++++++++++++++ src/ipa/libipa/lux.h | 48 ++++++++++ src/ipa/libipa/meson.build | 2 + 3 files changed, 233 insertions(+) create mode 100644 src/ipa/libipa/lux.cpp create mode 100644 src/ipa/libipa/lux.h diff --git a/src/ipa/libipa/lux.cpp b/src/ipa/libipa/lux.cpp new file mode 100644 index 000000000000..55b771705ab7 --- /dev/null +++ b/src/ipa/libipa/lux.cpp @@ -0,0 +1,183 @@ +/* SPDX-License-Identifier: BSD-2-Clause */ +/* + * Copyright (C) 2019, Raspberry Pi Ltd + * Copyright (C) 2024, Paul Elder + * + * Helper class that implements lux estimation + */ +#include "lux.h" + +#include +#include + +#include + +#include "libcamera/internal/yaml_parser.h" + +#include "histogram.h" + +/** + * \file lux.h + * \brief Helper class that implements lux estimation + * + * As estimating the lux level of an image is expected to be a common + * operation, it is implemented in a helper in libipa. It can be used to adjust + * the target Y value in AGC or for Bayesian AWB estimation. + */ + +namespace libcamera { + +using namespace std::literals::chrono_literals; + +LOG_DEFINE_CATEGORY(Lux) + +namespace ipa { + +/** + * \class Lux + * \brief Class that implements lux estimation + * + * IPAs that wish to use lux esimation 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 Lux::binSize_ + * \brief The maximum count of each bin + * + * This must be populated via setBinSize() before estimateLux() is used. + */ + +/** + * \var Lux::referenceExposureTime_ + * \brief The exposure time of the reference image, in microseconds. + */ + +/** + * \var Lux::referenceAnalogueGain_ + * \brief The analogue gain of the reference image. + */ + +/** + * \var Lux::referenceDigitalGain_ + * \brief The analogue gain of the reference image. + */ + +/** + * \var Lux::referenceY_ + * \brief The measured luminance of the reference image, out of the bin size. + * + * \sa binSize_ + */ + +/** + * \var Lux::referenceLux_ + * \brief The estimated lux level of the reference image. + */ + +/** + * \brief Configure the bin size of the Lux helper module + * \param[in] binSize The maximum count of each bin + * + * This must be configured befure estimateLux() is called, as it is + * platform-specific. + */ +void Lux::setBinSize(unsigned int binSize) +{ + binSize_ = binSize; +} + +/** + * \brief Parse tuning data + * \param[in] tuningData The YamlObject representing the tuning data + * + * This function parses yaml tuning data for the common Lux module. It requires + * reference exposure time, analogue gain, digital gain, and lux values. + * + * \code{.unparsed} + * algorithms: + * - Lux: + * referenceExposureTime: 10000 + * referenceAnalogueGain: 4.0 + * referenceDigitalGain: 1.0 + * referenceY: 12000 + * \endcode + * + * \return 0 on success or a negative error code + */ +int Lux::readYaml(const YamlObject &tuningData) +{ + auto value = tuningData["referenceExposureTime"].get(); + if (!value) { + LOG(Lux, Error) << "Missing tuning parameter: 'referenceExposureTime'"; + return -EINVAL; + } + referenceExposureTime_ = *value * 1.0us; + + value = tuningData["referenceAnalogueGain"].get(); + if (!value) { + LOG(Lux, Error) << "Missing tuning parameter: 'referenceAnalogueGain'"; + return -EINVAL; + } + referenceAnalogueGain_ = *value; + + value = tuningData["referenceDigitalGain"].get(); + if (!value) { + LOG(Lux, Error) << "Missing tuning parameter: 'referenceDigitalGain'"; + return -EINVAL; + } + referenceDigitalGain_ = *value; + + value = tuningData["referenceY"].get(); + if (!value) { + LOG(Lux, Error) << "Missing tuning parameter: 'referenceY'"; + return -EINVAL; + } + referenceY_ = *value; + + value = tuningData["referenceLux"].get(); + if (!value) { + LOG(Lux, Error) << "Missing tuning parameter: 'referenceLux'"; + return -EINVAL; + } + referenceLux_ = *value; + + return 0; +} + +/** + * \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 + * + * Estimate the lux given the exposure time, gain, and histogram. + * + * The bin size must be configured via setBinSize() before this function can be + * called. + * + * \return Estimated lux value + */ +double Lux::estimateLux(utils::Duration exposureTime, + double aGain, double dGain, + const Histogram &yHist) const +{ + double currentY = yHist.interQuantileMean(0, 1); + double exposureTimeRatio = referenceExposureTime_ / exposureTime; + double aGainRatio = referenceAnalogueGain_ / aGain; + double dGainRatio = referenceDigitalGain_ / dGain; + double yRatio = currentY * (binSize_ / yHist.bins()) / referenceY_; + + double estimatedLux = exposureTimeRatio * aGainRatio * dGainRatio * + yRatio * referenceLux_; + + LOG(Lux, Debug) << "Estimated lux " << estimatedLux; + return estimatedLux; +} + +} /* namespace ipa */ + +} /* namespace libcamera */ diff --git a/src/ipa/libipa/lux.h b/src/ipa/libipa/lux.h new file mode 100644 index 000000000000..3e6dcc5c3413 --- /dev/null +++ b/src/ipa/libipa/lux.h @@ -0,0 +1,48 @@ +/* SPDX-License-Identifier: BSD-2-Clause */ +/* + * Copyright (C) 2019, Raspberry Pi Ltd + * Copyright (C) 2024, Paul Elder + * + * Helper class that implements lux estimation + */ + +#pragma once + +#include +#include +#include + +#include + +#include "libcamera/internal/yaml_parser.h" + +#include "histogram.h" + +namespace libcamera { + +namespace ipa { + +class Lux +{ +public: + Lux() = default; + ~Lux() = default; + + void setBinSize(unsigned int binSize); + int readYaml(const YamlObject &tuningData); + double estimateLux(utils::Duration exposureTime, + double aGain, double dGain, + const Histogram &yHist) const; + +private: + unsigned int binSize_; + utils::Duration referenceExposureTime_; + double referenceAnalogueGain_; + double referenceDigitalGain_; + double referenceY_; + double referenceLux_; +}; + +} /* namespace ipa */ + +} /* namespace libcamera */ diff --git a/src/ipa/libipa/meson.build b/src/ipa/libipa/meson.build index 93ae25da5e83..189ec0311bd3 100644 --- a/src/ipa/libipa/meson.build +++ b/src/ipa/libipa/meson.build @@ -10,6 +10,7 @@ libipa_headers = files([ 'histogram.h', 'interpolator.h', 'lsc_polynomial.h', + 'lux.h', 'module.h', 'pwl.h', 'vector.h', @@ -25,6 +26,7 @@ libipa_sources = files([ 'histogram.cpp', 'interpolator.cpp', 'lsc_polynomial.cpp', + 'lux.cpp', 'module.cpp', 'pwl.cpp', 'vector.cpp',