[libcamera-devel,v1,09/10] ipa: raspberrypi: Generalise the agc algorithm
diff mbox series

Message ID 20230322130612.5208-10-naush@raspberrypi.com
State Superseded
Headers show
Series
  • Raspberry Pi: Generalised algorithms
Related show

Commit Message

Naushir Patuck March 22, 2023, 1:06 p.m. UTC
Remove any hard-coded assumptions about the target hardware platform
from the AGC algorithm. Instead, use the "target" string provided by
the camera tuning config and generalised statistics structures to
determing parameters such as grid and region sizes.

This change replaces all hard-coded arrays with equivalent std::vector
types.

Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
---
 src/ipa/raspberrypi/controller/rpi/agc.cpp | 19 ++++++++++++-------
 src/ipa/raspberrypi/controller/rpi/agc.h   |  9 +--------
 2 files changed, 13 insertions(+), 15 deletions(-)

Comments

David Plowman March 22, 2023, 2:41 p.m. UTC | #1
Hi Naush

Thanks for the patch!

On Wed, 22 Mar 2023 at 13:06, Naushir Patuck via libcamera-devel
<libcamera-devel@lists.libcamera.org> wrote:
>
> Remove any hard-coded assumptions about the target hardware platform
> from the AGC algorithm. Instead, use the "target" string provided by
> the camera tuning config and generalised statistics structures to
> determing parameters such as grid and region sizes.

I couldn't see that we did use the "target" string, so maybe perhaps
just remove the reference to it in the commit message?

>
> This change replaces all hard-coded arrays with equivalent std::vector
> types.
>
> Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
> ---
>  src/ipa/raspberrypi/controller/rpi/agc.cpp | 19 ++++++++++++-------
>  src/ipa/raspberrypi/controller/rpi/agc.h   |  9 +--------
>  2 files changed, 13 insertions(+), 15 deletions(-)
>
> diff --git a/src/ipa/raspberrypi/controller/rpi/agc.cpp b/src/ipa/raspberrypi/controller/rpi/agc.cpp
> index 4ea0dd41e66c..3e4a8149b9ae 100644
> --- a/src/ipa/raspberrypi/controller/rpi/agc.cpp
> +++ b/src/ipa/raspberrypi/controller/rpi/agc.cpp
> @@ -31,17 +31,12 @@ LOG_DEFINE_CATEGORY(RPiAgc)
>  int AgcMeteringMode::read(const libcamera::YamlObject &params)
>  {
>         const YamlObject &yamlWeights = params["weights"];
> -       if (yamlWeights.size() != AgcStatsSize) {
> -               LOG(RPiAgc, Error) << "AgcMeteringMode: Incorrect number of weights";
> -               return -EINVAL;
> -       }
>
> -       unsigned int num = 0;
>         for (const auto &p : yamlWeights.asList()) {
>                 auto value = p.get<double>();
>                 if (!value)
>                         return -EINVAL;
> -               weights[num++] = *value;
> +               weights.push_back(*value);
>         }
>
>         return 0;
> @@ -249,6 +244,14 @@ int Agc::read(const libcamera::YamlObject &params)
>         if (ret)
>                 return ret;
>
> +       const Size &size = getHardwareConfig().agcZoneWeights;
> +       for (auto const &modes : config_.meteringModes) {
> +               if (modes.second.weights.size() != size.width * size.height) {
> +                       LOG(RPiAgc, Error) << "AgcMeteringMode: Incorrect number of weights";
> +                       return -EINVAL;
> +               }
> +       }
> +
>         /*
>          * Set the config's defaults (which are the first ones it read) as our
>          * current modes, until someone changes them.  (they're all known to
> @@ -582,9 +585,11 @@ void Agc::fetchAwbStatus(Metadata *imageMetadata)
>  }
>
>  static double computeInitialY(StatisticsPtr &stats, AwbStatus const &awb,
> -                             double weights[], double gain)
> +                             std::vector<double> &weights, double gain)

I'm guessing the weights could be const? (of course that would have
been true in the original code too, which would be my fault) Don't
know if it's worth the trouble though.

Other than that:

Reviewed-by: David Plowman <david.plowman@raspberrypi.com>

Thanks!
David


>  {
>         constexpr uint64_t maxVal = 1 << Statistics::NormalisationFactorPow2;
> +
> +       ASSERT(weights.size() == stats->agcRegions.numRegions());
>         /*
>          * Note how the calculation below means that equal weights give you
>          * "average" metering (i.e. all pixels equally important).
> diff --git a/src/ipa/raspberrypi/controller/rpi/agc.h b/src/ipa/raspberrypi/controller/rpi/agc.h
> index f04896ca25ad..d11a49c9cd85 100644
> --- a/src/ipa/raspberrypi/controller/rpi/agc.h
> +++ b/src/ipa/raspberrypi/controller/rpi/agc.h
> @@ -17,17 +17,10 @@
>
>  /* This is our implementation of AGC. */
>
> -/*
> - * This is the number actually set up by the firmware, not the maximum possible
> - * number (which is 16).
> - */
> -
> -constexpr unsigned int AgcStatsSize = 15;
> -
>  namespace RPiController {
>
>  struct AgcMeteringMode {
> -       double weights[AgcStatsSize];
> +       std::vector<double> weights;
>         int read(const libcamera::YamlObject &params);
>  };
>
> --
> 2.34.1
>
Jacopo Mondi March 24, 2023, 9:37 a.m. UTC | #2
Hi Naush

On Wed, Mar 22, 2023 at 01:06:11PM +0000, Naushir Patuck via libcamera-devel wrote:
> Remove any hard-coded assumptions about the target hardware platform
> from the AGC algorithm. Instead, use the "target" string provided by
> the camera tuning config and generalised statistics structures to
> determing parameters such as grid and region sizes.
>
> This change replaces all hard-coded arrays with equivalent std::vector
> types.
>
> Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
> ---
>  src/ipa/raspberrypi/controller/rpi/agc.cpp | 19 ++++++++++++-------
>  src/ipa/raspberrypi/controller/rpi/agc.h   |  9 +--------
>  2 files changed, 13 insertions(+), 15 deletions(-)
>
> diff --git a/src/ipa/raspberrypi/controller/rpi/agc.cpp b/src/ipa/raspberrypi/controller/rpi/agc.cpp
> index 4ea0dd41e66c..3e4a8149b9ae 100644
> --- a/src/ipa/raspberrypi/controller/rpi/agc.cpp
> +++ b/src/ipa/raspberrypi/controller/rpi/agc.cpp
> @@ -31,17 +31,12 @@ LOG_DEFINE_CATEGORY(RPiAgc)
>  int AgcMeteringMode::read(const libcamera::YamlObject &params)
>  {
>  	const YamlObject &yamlWeights = params["weights"];
> -	if (yamlWeights.size() != AgcStatsSize) {
> -		LOG(RPiAgc, Error) << "AgcMeteringMode: Incorrect number of weights";
> -		return -EINVAL;
> -	}
>
> -	unsigned int num = 0;
>  	for (const auto &p : yamlWeights.asList()) {
>  		auto value = p.get<double>();
>  		if (!value)
>  			return -EINVAL;
> -		weights[num++] = *value;
> +		weights.push_back(*value);


>  	}
>
>  	return 0;
> @@ -249,6 +244,14 @@ int Agc::read(const libcamera::YamlObject &params)
>  	if (ret)
>  		return ret;
>
> +	const Size &size = getHardwareConfig().agcZoneWeights;
> +	for (auto const &modes : config_.meteringModes) {
> +		if (modes.second.weights.size() != size.width * size.height) {
> +			LOG(RPiAgc, Error) << "AgcMeteringMode: Incorrect number of weights";
> +			return -EINVAL;
> +		}
> +	}
> +
>  	/*
>  	 * Set the config's defaults (which are the first ones it read) as our
>  	 * current modes, until someone changes them.  (they're all known to
> @@ -582,9 +585,11 @@ void Agc::fetchAwbStatus(Metadata *imageMetadata)
>  }
>
>  static double computeInitialY(StatisticsPtr &stats, AwbStatus const &awb,
> -			      double weights[], double gain)
> +			      std::vector<double> &weights, double gain)
>  {
>  	constexpr uint64_t maxVal = 1 << Statistics::NormalisationFactorPow2;
> +
> +	ASSERT(weights.size() == stats->agcRegions.numRegions());

Maybe an empty line, but just if you have to resend

Reviewed-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>

>  	/*
>  	 * Note how the calculation below means that equal weights give you
>  	 * "average" metering (i.e. all pixels equally important).
> diff --git a/src/ipa/raspberrypi/controller/rpi/agc.h b/src/ipa/raspberrypi/controller/rpi/agc.h
> index f04896ca25ad..d11a49c9cd85 100644
> --- a/src/ipa/raspberrypi/controller/rpi/agc.h
> +++ b/src/ipa/raspberrypi/controller/rpi/agc.h
> @@ -17,17 +17,10 @@
>
>  /* This is our implementation of AGC. */
>
> -/*
> - * This is the number actually set up by the firmware, not the maximum possible
> - * number (which is 16).
> - */
> -
> -constexpr unsigned int AgcStatsSize = 15;
> -
>  namespace RPiController {
>
>  struct AgcMeteringMode {
> -	double weights[AgcStatsSize];
> +	std::vector<double> weights;
>  	int read(const libcamera::YamlObject &params);
>  };
>
> --
> 2.34.1
>

Patch
diff mbox series

diff --git a/src/ipa/raspberrypi/controller/rpi/agc.cpp b/src/ipa/raspberrypi/controller/rpi/agc.cpp
index 4ea0dd41e66c..3e4a8149b9ae 100644
--- a/src/ipa/raspberrypi/controller/rpi/agc.cpp
+++ b/src/ipa/raspberrypi/controller/rpi/agc.cpp
@@ -31,17 +31,12 @@  LOG_DEFINE_CATEGORY(RPiAgc)
 int AgcMeteringMode::read(const libcamera::YamlObject &params)
 {
 	const YamlObject &yamlWeights = params["weights"];
-	if (yamlWeights.size() != AgcStatsSize) {
-		LOG(RPiAgc, Error) << "AgcMeteringMode: Incorrect number of weights";
-		return -EINVAL;
-	}
 
-	unsigned int num = 0;
 	for (const auto &p : yamlWeights.asList()) {
 		auto value = p.get<double>();
 		if (!value)
 			return -EINVAL;
-		weights[num++] = *value;
+		weights.push_back(*value);
 	}
 
 	return 0;
@@ -249,6 +244,14 @@  int Agc::read(const libcamera::YamlObject &params)
 	if (ret)
 		return ret;
 
+	const Size &size = getHardwareConfig().agcZoneWeights;
+	for (auto const &modes : config_.meteringModes) {
+		if (modes.second.weights.size() != size.width * size.height) {
+			LOG(RPiAgc, Error) << "AgcMeteringMode: Incorrect number of weights";
+			return -EINVAL;
+		}
+	}
+
 	/*
 	 * Set the config's defaults (which are the first ones it read) as our
 	 * current modes, until someone changes them.  (they're all known to
@@ -582,9 +585,11 @@  void Agc::fetchAwbStatus(Metadata *imageMetadata)
 }
 
 static double computeInitialY(StatisticsPtr &stats, AwbStatus const &awb,
-			      double weights[], double gain)
+			      std::vector<double> &weights, double gain)
 {
 	constexpr uint64_t maxVal = 1 << Statistics::NormalisationFactorPow2;
+
+	ASSERT(weights.size() == stats->agcRegions.numRegions());
 	/*
 	 * Note how the calculation below means that equal weights give you
 	 * "average" metering (i.e. all pixels equally important).
diff --git a/src/ipa/raspberrypi/controller/rpi/agc.h b/src/ipa/raspberrypi/controller/rpi/agc.h
index f04896ca25ad..d11a49c9cd85 100644
--- a/src/ipa/raspberrypi/controller/rpi/agc.h
+++ b/src/ipa/raspberrypi/controller/rpi/agc.h
@@ -17,17 +17,10 @@ 
 
 /* This is our implementation of AGC. */
 
-/*
- * This is the number actually set up by the firmware, not the maximum possible
- * number (which is 16).
- */
-
-constexpr unsigned int AgcStatsSize = 15;
-
 namespace RPiController {
 
 struct AgcMeteringMode {
-	double weights[AgcStatsSize];
+	std::vector<double> weights;
 	int read(const libcamera::YamlObject &params);
 };