From patchwork Thu Jan 23 11:40:57 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Klug X-Patchwork-Id: 22622 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 6E630BDE6B for ; Thu, 23 Jan 2025 11:42:35 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id B538E6856F; Thu, 23 Jan 2025 12:42:34 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="VOo6mSz6"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 09E406856B for ; Thu, 23 Jan 2025 12:42:32 +0100 (CET) Received: from ideasonboard.com (unknown [IPv6:2a00:6020:448c:6c00:c0a:33cd:b453:5d3f]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 86095465; Thu, 23 Jan 2025 12:41:28 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1737632488; bh=81yB7+lofl9u90S3yCYFx/74zgR/+ZjsCRx9OIQ1eDI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=VOo6mSz651CyHRBc/tUVd1C3Peq/4VnUdmsvFG5+pYUqG0pTk3lRmWoxeYO5q+Fqs zB9KIBFSKy031iiGB6Srhk4iMXJyfBF/BM7tfIIbuqepeWe8JS9LrLOUBQrNUW6xuF EBeNWd9O8ZBbkW97oYpZt4PmrNqrkrxir80HTV/o= From: Stefan Klug To: libcamera-devel@lists.libcamera.org Cc: Stefan Klug , Paul Elder , Daniel Scally Subject: [PATCH v2 07/17] ipa: rkisp1: Use grey world algorithm from libipa Date: Thu, 23 Jan 2025 12:40:57 +0100 Message-ID: <20250123114204.79321-8-stefan.klug@ideasonboard.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250123114204.79321-1-stefan.klug@ideasonboard.com> References: <20250123114204.79321-1-stefan.klug@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" Now that libipa contains a grey world algorithm, use that. Signed-off-by: Stefan Klug Reviewed-by: Paul Elder Reviewed-by: Daniel Scally --- Changes in v2: - Collected tags - Replaced r variable with awbResult --- src/ipa/rkisp1/algorithms/awb.cpp | 73 ++++++++++++++++++++----------- src/ipa/rkisp1/algorithms/awb.h | 4 +- 2 files changed, 50 insertions(+), 27 deletions(-) diff --git a/src/ipa/rkisp1/algorithms/awb.cpp b/src/ipa/rkisp1/algorithms/awb.cpp index f6449565834b..b21d0d4c03bb 100644 --- a/src/ipa/rkisp1/algorithms/awb.cpp +++ b/src/ipa/rkisp1/algorithms/awb.cpp @@ -16,6 +16,7 @@ #include +#include "libipa/awb_grey.h" #include "libipa/colours.h" /** @@ -40,6 +41,28 @@ constexpr int32_t kDefaultColourTemperature = 5000; /* Minimum mean value below which AWB can't operate. */ constexpr double kMeanMinThreshold = 2.0; +class RkISP1AwbStats : public AwbStats +{ +public: + RkISP1AwbStats(const RGB &rgbMeans) + : rgbMeans_(rgbMeans) {} + + double computeColourError([[maybe_unused]] const RGB &gains) const override + { + LOG(RkISP1Awb, Error) + << "RkISP1AwbStats::computeColourError is not implemented"; + return 0.0; + } + + RGB getRGBMeans() const override + { + return rgbMeans_; + }; + +private: + RGB rgbMeans_; +}; + Awb::Awb() : rgbMode_(false) { @@ -55,15 +78,12 @@ int Awb::init(IPAContext &context, const YamlObject &tuningData) kMaxColourTemperature, kDefaultColourTemperature); - Interpolator> gainCurve; - int ret = gainCurve.readYaml(tuningData["colourGains"], "ct", "gains"); - if (ret < 0) - LOG(RkISP1Awb, Warning) - << "Failed to parse 'colourGains' " - << "parameter from tuning file; " - << "manual colour temperature will not work properly"; - else - colourGainCurve_ = gainCurve; + awbAlgo_ = std::make_unique(); + int ret = awbAlgo_->init(tuningData); + if (ret) { + LOG(RkISP1Awb, Error) << "Failed to init awb algorithm"; + return ret; + } return 0; } @@ -128,10 +148,10 @@ void Awb::queueRequest(IPAContext &context, * This will be fixed with the bayes AWB algorithm. */ update = true; - } else if (colourTemperature && colourGainCurve_) { - const auto &gains = colourGainCurve_->getInterpolated(*colourTemperature); - awb.gains.manual.r() = gains[0]; - awb.gains.manual.b() = gains[1]; + } else if (colourTemperature) { + const auto &gains = awbAlgo_->gainsFromColourTemperature(*colourTemperature); + awb.gains.manual.r() = gains.r(); + awb.gains.manual.b() = gains.b(); awb.temperatureK = *colourTemperature; update = true; } @@ -251,33 +271,34 @@ void Awb::process(IPAContext &context, rgbMeans.b() < kMeanMinThreshold) return; - activeState.awb.temperatureK = estimateCCT(rgbMeans); + /* + * \Todo: Hardcode lux to a fixed value, until an estimation is + * implemented. + */ + int lux = 1000; + + RkISP1AwbStats awbStats{ rgbMeans }; + AwbResult awbResult = awbAlgo_->calculateAwb(awbStats, lux); + + activeState.awb.temperatureK = awbResult.colourTemperature; /* Metadata shall contain the up to date measurement */ metadata.set(controls::ColourTemperature, activeState.awb.temperatureK); - /* - * Estimate the red and blue gains to apply in a grey world. The green - * gain is hardcoded to 1.0. Avoid divisions by zero by clamping the - * divisor to a minimum value of 1.0. - */ - RGB gains({ rgbMeans.g() / std::max(rgbMeans.r(), 1.0), - 1.0, - rgbMeans.g() / std::max(rgbMeans.b(), 1.0) }); - /* * Clamp the gain values to the hardware, which expresses gains as Q2.8 * unsigned integer values. Set the minimum just above zero to avoid * divisions by zero when computing the raw means in subsequent * iterations. */ - gains = gains.max(1.0 / 256).min(1023.0 / 256); + awbResult.gains = awbResult.gains.max(1.0 / 256).min(1023.0 / 256); /* Filter the values to avoid oscillations. */ double speed = 0.2; - gains = gains * speed + activeState.awb.gains.automatic * (1 - speed); + awbResult.gains = awbResult.gains * speed + + activeState.awb.gains.automatic * (1 - speed); - activeState.awb.gains.automatic = gains; + activeState.awb.gains.automatic = awbResult.gains; LOG(RkISP1Awb, Debug) << std::showpoint diff --git a/src/ipa/rkisp1/algorithms/awb.h b/src/ipa/rkisp1/algorithms/awb.h index 2a64cb60d5f4..3382b2178567 100644 --- a/src/ipa/rkisp1/algorithms/awb.h +++ b/src/ipa/rkisp1/algorithms/awb.h @@ -9,6 +9,7 @@ #include +#include "libipa/awb.h" #include "libipa/interpolator.h" #include "libipa/vector.h" @@ -41,7 +42,8 @@ private: RGB calculateRgbMeans(const IPAFrameContext &frameContext, const rkisp1_cif_isp_awb_stat *awb) const; - std::optional>> colourGainCurve_; + std::unique_ptr awbAlgo_; + bool rgbMode_; };