[v1,10/11] ipa: rkisp1: Add support for bayes AWB algorithm from libipa
diff mbox series

Message ID 20250109115412.356768-11-stefan.klug@ideasonboard.com
State New
Headers show
Series
  • Add Bayesian AWB algorithm to libipa and rkisp1
Related show

Commit Message

Stefan Klug Jan. 9, 2025, 11:54 a.m. UTC
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.

Signed-off-by: Stefan Klug <stefan.klug@ideasonboard.com>
---

Todo: The lux level is currently hardcoded to 1000. The result from the
lux estimation needs to be used here.
---
 src/ipa/rkisp1/algorithms/awb.cpp | 44 ++++++++++++++++++++++++++-----
 1 file changed, 38 insertions(+), 6 deletions(-)

Comments

Paul Elder Jan. 14, 2025, 3:05 a.m. UTC | #1
On Thu, Jan 09, 2025 at 12:54:01PM +0100, Stefan Klug wrote:
> 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.
> 
> Signed-off-by: Stefan Klug <stefan.klug@ideasonboard.com>

Reviewed-by: Paul Elder <paul.elder@ideasonboard.com>

> ---
> 
> Todo: The lux level is currently hardcoded to 1000. The result from the
> lux estimation needs to be used here.
> ---
>  src/ipa/rkisp1/algorithms/awb.cpp | 44 ++++++++++++++++++++++++++-----
>  1 file changed, 38 insertions(+), 6 deletions(-)
> 
> diff --git a/src/ipa/rkisp1/algorithms/awb.cpp b/src/ipa/rkisp1/algorithms/awb.cpp
> index 42a4784998bc..39a2c0589943 100644
> --- a/src/ipa/rkisp1/algorithms/awb.cpp
> +++ b/src/ipa/rkisp1/algorithms/awb.cpp
> @@ -16,6 +16,7 @@
>  
>  #include <libcamera/ipa/core_ipa_interface.h>
>  
> +#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<double> &rgbMeans)
> -		: rgbMeans_(rgbMeans) {}
> +		: rgbMeans_(rgbMeans)
> +	{
> +		rg_ = rgbMeans_.r() / rgbMeans_.g();
> +		bg_ = rgbMeans_.b() / rgbMeans_.g();
> +	}
>  
> -	double computeColourError([[maybe_unused]] const RGB<double> &gains) const override
> +	double computeColourError(const RGB<double> &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<double> getRGBMeans() const override
> @@ -61,6 +72,8 @@ public:
>  
>  private:
>  	RGB<double> 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<AwbGrey>();
> +	if (!tuningData.contains("algorithm"))
> +		LOG(RkISP1Awb, Info) << "No awb algorithm specified."
> +				     << " Default to grey world";
> +
> +	auto mode = tuningData["algorithm"].get<std::string>("grey");
> +	if (mode == "grey") {
> +		awbAlgo_ = std::make_unique<AwbGrey>();
> +	} else if (mode == "bayes") {
> +		awbAlgo_ = std::make_unique<AwbBayes>();
> +	} 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)
> -- 
> 2.43.0
>

Patch
diff mbox series

diff --git a/src/ipa/rkisp1/algorithms/awb.cpp b/src/ipa/rkisp1/algorithms/awb.cpp
index 42a4784998bc..39a2c0589943 100644
--- a/src/ipa/rkisp1/algorithms/awb.cpp
+++ b/src/ipa/rkisp1/algorithms/awb.cpp
@@ -16,6 +16,7 @@ 
 
 #include <libcamera/ipa/core_ipa_interface.h>
 
+#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<double> &rgbMeans)
-		: rgbMeans_(rgbMeans) {}
+		: rgbMeans_(rgbMeans)
+	{
+		rg_ = rgbMeans_.r() / rgbMeans_.g();
+		bg_ = rgbMeans_.b() / rgbMeans_.g();
+	}
 
-	double computeColourError([[maybe_unused]] const RGB<double> &gains) const override
+	double computeColourError(const RGB<double> &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<double> getRGBMeans() const override
@@ -61,6 +72,8 @@  public:
 
 private:
 	RGB<double> 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<AwbGrey>();
+	if (!tuningData.contains("algorithm"))
+		LOG(RkISP1Awb, Info) << "No awb algorithm specified."
+				     << " Default to grey world";
+
+	auto mode = tuningData["algorithm"].get<std::string>("grey");
+	if (mode == "grey") {
+		awbAlgo_ = std::make_unique<AwbGrey>();
+	} else if (mode == "bayes") {
+		awbAlgo_ = std::make_unique<AwbBayes>();
+	} 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)