From patchwork Thu Jan 23 11:41:00 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Klug X-Patchwork-Id: 22625 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 E5962C3317 for ; Thu, 23 Jan 2025 11:42:42 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 8523E68572; Thu, 23 Jan 2025 12:42:42 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="QbU1DQZu"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 6DE1F6856C for ; Thu, 23 Jan 2025 12:42:40 +0100 (CET) Received: from ideasonboard.com (unknown [IPv6:2a00:6020:448c:6c00:c0a:33cd:b453:5d3f]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 000AA134C; Thu, 23 Jan 2025 12:41:36 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1737632497; bh=OYOLbYo26r1XY6je1zy4ggkTl/orvQ0+x8C/m1NNU64=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=QbU1DQZuayIf6KDGc5dylwwUFXLzGfcwQESNC7ckhNuBazwc20U2PFxCoXdoWH72d ubpH+XwXI2gNbvYfCIPACUcGkR6nCwYYaMym/Kpu7NKOZBDVbyAeR0cT7/8qeMpZ82 XaJcBFciwETZf/13UpvNy0B6EKd4E5lC4p6XPaYI= From: Stefan Klug To: libcamera-devel@lists.libcamera.org Cc: Stefan Klug , Paul Elder , Daniel Scally Subject: [PATCH v2 10/17] ipa: rkisp1: Add support for bayes AWB algorithm from libipa Date: Thu, 23 Jan 2025 12:41:00 +0100 Message-ID: <20250123114204.79321-11-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 bayes AWB algorithm, add it as supported algorithm to the rkisp1 ipa. The decision between the grey world algorithm and the bayesian is done based on the "algorithm" property of the "Awb" algorithm in the tuning file. If the lux value in the frameContext is set by the Lux algorithm it is taken into account. If the lux value is 0 the prior likelihood estimation gets ignored in the AWB calculations. Signed-off-by: Stefan Klug Reviewed-by: Paul Elder Reviewed-by: Daniel Scally --- Changes in v2: - Collected tags --- src/ipa/rkisp1/algorithms/awb.cpp | 52 +++++++++++++++++++++++-------- 1 file changed, 39 insertions(+), 13 deletions(-) diff --git a/src/ipa/rkisp1/algorithms/awb.cpp b/src/ipa/rkisp1/algorithms/awb.cpp index b21d0d4c03bb..55e1b43c8cd2 100644 --- a/src/ipa/rkisp1/algorithms/awb.cpp +++ b/src/ipa/rkisp1/algorithms/awb.cpp @@ -16,6 +16,7 @@ #include +#include "libipa/awb_bayes.h" #include "libipa/awb_grey.h" #include "libipa/colours.h" @@ -45,13 +46,23 @@ class RkISP1AwbStats : public AwbStats { public: RkISP1AwbStats(const RGB &rgbMeans) - : rgbMeans_(rgbMeans) {} + : rgbMeans_(rgbMeans) + { + rg_ = rgbMeans_.r() / rgbMeans_.g(); + bg_ = rgbMeans_.b() / rgbMeans_.g(); + } - double computeColourError([[maybe_unused]] const RGB &gains) const override + double computeColourError(const RGB &gains) const override { - LOG(RkISP1Awb, Error) - << "RkISP1AwbStats::computeColourError is not implemented"; - return 0.0; + /* + * Compute the sum of the squared colour error (non-greyness) as it + * appears in the log likelihood equation. + */ + double deltaR = gains.r() * rg_ - 1.0; + double deltaB = gains.b() * bg_ - 1.0; + double delta2 = deltaR * deltaR + deltaB * deltaB; + + return delta2; } RGB getRGBMeans() const override @@ -61,6 +72,8 @@ public: private: RGB rgbMeans_; + double rg_; + double bg_; }; Awb::Awb() @@ -78,13 +91,30 @@ int Awb::init(IPAContext &context, const YamlObject &tuningData) kMaxColourTemperature, kDefaultColourTemperature); - awbAlgo_ = std::make_unique(); + if (!tuningData.contains("algorithm")) + LOG(RkISP1Awb, Info) << "No awb algorithm specified." + << " Default to grey world"; + + auto mode = tuningData["algorithm"].get("grey"); + if (mode == "grey") { + awbAlgo_ = std::make_unique(); + } else if (mode == "bayes") { + awbAlgo_ = std::make_unique(); + } else { + LOG(RkISP1Awb, Error) << "Unknown awb algorithm: " << mode; + return -EINVAL; + } + LOG(RkISP1Awb, Debug) << "Using awb algorithm: " << mode; + int ret = awbAlgo_->init(tuningData); if (ret) { LOG(RkISP1Awb, Error) << "Failed to init awb algorithm"; return ret; } + const auto &src = awbAlgo_->controls(); + cmap.insert(src.begin(), src.end()); + return 0; } @@ -131,6 +161,8 @@ void Awb::queueRequest(IPAContext &context, << (*awbEnable ? "Enabling" : "Disabling") << " AWB"; } + awbAlgo_->handleControls(controls); + frameContext.awb.autoEnabled = awb.autoEnabled; if (awb.autoEnabled) @@ -271,14 +303,8 @@ void Awb::process(IPAContext &context, rgbMeans.b() < kMeanMinThreshold) return; - /* - * \Todo: Hardcode lux to a fixed value, until an estimation is - * implemented. - */ - int lux = 1000; - RkISP1AwbStats awbStats{ rgbMeans }; - AwbResult awbResult = awbAlgo_->calculateAwb(awbStats, lux); + AwbResult awbResult = awbAlgo_->calculateAwb(awbStats, frameContext.lux.lux); activeState.awb.temperatureK = awbResult.colourTemperature;