[libcamera-devel,v3,4/5] ipa: ipu3: af: Use Span for y_table_item_t
diff mbox series

Message ID 20220325092555.1799897-5-kieran.bingham@ideasonboard.com
State Accepted
Headers show
Series
  • ipa: ipu3: af: Small improvements
Related show

Commit Message

Kieran Bingham March 25, 2022, 9:25 a.m. UTC
Convert the y_table_item_t to a Span and use that for iteration when
estimating variance of the table.

Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

---
v3:
 - Remove parentheses and casting in afEstimateVariance

 src/ipa/ipu3/algorithms/af.cpp | 34 +++++++++++++++++-----------------
 src/ipa/ipu3/algorithms/af.h   |  4 ++--
 2 files changed, 19 insertions(+), 19 deletions(-)

Comments

Jean-Michel Hautbois March 29, 2022, 5:57 a.m. UTC | #1
Hi Kieran,

Thanks for the patch !

On 25/03/2022 10:25, Kieran Bingham wrote:
> Convert the y_table_item_t to a Span and use that for iteration when
> estimating variance of the table.
> 
> Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

Reviewed-by: Jean-Michel Hautbois <jeanmichel.hautbois@ideasonboard.com>

> 
> ---
> v3:
>   - Remove parentheses and casting in afEstimateVariance
> 
>   src/ipa/ipu3/algorithms/af.cpp | 34 +++++++++++++++++-----------------
>   src/ipa/ipu3/algorithms/af.h   |  4 ++--
>   2 files changed, 19 insertions(+), 19 deletions(-)
> 
> diff --git a/src/ipa/ipu3/algorithms/af.cpp b/src/ipa/ipu3/algorithms/af.cpp
> index 6893a15fdc07..c0f2d34368cb 100644
> --- a/src/ipa/ipu3/algorithms/af.cpp
> +++ b/src/ipa/ipu3/algorithms/af.cpp
> @@ -335,29 +335,29 @@ void Af::afIgnoreFrameReset()
>    *
>    * \return The variance of the values in the data set \a y_item selected by \a isY1
>    */
> -double Af::afEstimateVariance(const y_table_item_t *y_item, uint32_t len,
> -			      bool isY1)
> +double Af::afEstimateVariance(Span<const y_table_item_t> y_items, bool isY1)
>   {
> -	uint32_t z = 0;
>   	uint32_t total = 0;
>   	double mean;
>   	double var_sum = 0;
>   
> -	for (z = 0; z < len; z++) {
> +	for (auto y : y_items) {
>   		if (isY1)
> -			total += y_item[z].y1_avg;
> +			total += y.y1_avg;
>   		else
> -			total += y_item[z].y2_avg;
> +			total += y.y2_avg;
>   	}
> -	mean = total / len;
> -	for (z = 0; z < len; z++) {
> +
> +	mean = total / y_items.size();
> +
> +	for (auto y : y_items) {
>   		if (isY1)
> -			var_sum += pow((y_item[z].y1_avg - mean), 2);
> +			var_sum += pow(y.y1_avg - mean, 2);
>   		else
> -			var_sum += pow((y_item[z].y2_avg - mean), 2);
> +			var_sum += pow(y.y2_avg - mean, 2);
>   	}
>   
> -	return var_sum / static_cast<double>(len);
> +	return var_sum / y_items.size();
>   }
>   
>   /**
> @@ -405,21 +405,21 @@ bool Af::afIsOutOfFocus(IPAContext context)
>    */
>   void Af::process(IPAContext &context, const ipu3_uapi_stats_3a *stats)
>   {
> -	const y_table_item_t *y_item = reinterpret_cast<const y_table_item_t *>(&stats->af_raw_buffer.y_table);
> -	uint32_t afRawBufferLen;
> -
>   	/* Evaluate the AF buffer length */
> -	afRawBufferLen = context.configuration.af.afGrid.width *
> -			 context.configuration.af.afGrid.height;
> +	uint32_t afRawBufferLen = context.configuration.af.afGrid.width *
> +				  context.configuration.af.afGrid.height;
>   
>   	ASSERT(afRawBufferLen < IPU3_UAPI_AF_Y_TABLE_MAX_SIZE);
>   
> +	Span<const y_table_item_t> y_items(reinterpret_cast<const y_table_item_t *>(&stats->af_raw_buffer.y_table),
> +					   afRawBufferLen);
> +
>   	/*
>   	 * Calculate the mean and the variance of AF statistics for a given grid.
>   	 * For coarse: y1 are used.
>   	 * For fine: y2 results are used.
>   	 */
> -	currentVariance_ = afEstimateVariance(y_item, afRawBufferLen, !coarseCompleted_);
> +	currentVariance_ = afEstimateVariance(y_items, !coarseCompleted_);
>   
>   	if (!context.frameContext.af.stable) {
>   		afCoarseScan(context);
> diff --git a/src/ipa/ipu3/algorithms/af.h b/src/ipa/ipu3/algorithms/af.h
> index 3b5758e868ea..b85cf94163ff 100644
> --- a/src/ipa/ipu3/algorithms/af.h
> +++ b/src/ipa/ipu3/algorithms/af.h
> @@ -41,8 +41,8 @@ private:
>   	void afReset(IPAContext &context);
>   	bool afNeedIgnoreFrame();
>   	void afIgnoreFrameReset();
> -	double afEstimateVariance(const y_table_item_t *y_item, uint32_t len,
> -				  bool isY1);
> +	double afEstimateVariance(Span<const y_table_item_t> y_items, bool isY1);
> +
>   	bool afIsOutOfFocus(IPAContext context);
>   
>   	/* VCM step configuration. It is the current setting of the VCM step. */

Patch
diff mbox series

diff --git a/src/ipa/ipu3/algorithms/af.cpp b/src/ipa/ipu3/algorithms/af.cpp
index 6893a15fdc07..c0f2d34368cb 100644
--- a/src/ipa/ipu3/algorithms/af.cpp
+++ b/src/ipa/ipu3/algorithms/af.cpp
@@ -335,29 +335,29 @@  void Af::afIgnoreFrameReset()
  *
  * \return The variance of the values in the data set \a y_item selected by \a isY1
  */
-double Af::afEstimateVariance(const y_table_item_t *y_item, uint32_t len,
-			      bool isY1)
+double Af::afEstimateVariance(Span<const y_table_item_t> y_items, bool isY1)
 {
-	uint32_t z = 0;
 	uint32_t total = 0;
 	double mean;
 	double var_sum = 0;
 
-	for (z = 0; z < len; z++) {
+	for (auto y : y_items) {
 		if (isY1)
-			total += y_item[z].y1_avg;
+			total += y.y1_avg;
 		else
-			total += y_item[z].y2_avg;
+			total += y.y2_avg;
 	}
-	mean = total / len;
-	for (z = 0; z < len; z++) {
+
+	mean = total / y_items.size();
+
+	for (auto y : y_items) {
 		if (isY1)
-			var_sum += pow((y_item[z].y1_avg - mean), 2);
+			var_sum += pow(y.y1_avg - mean, 2);
 		else
-			var_sum += pow((y_item[z].y2_avg - mean), 2);
+			var_sum += pow(y.y2_avg - mean, 2);
 	}
 
-	return var_sum / static_cast<double>(len);
+	return var_sum / y_items.size();
 }
 
 /**
@@ -405,21 +405,21 @@  bool Af::afIsOutOfFocus(IPAContext context)
  */
 void Af::process(IPAContext &context, const ipu3_uapi_stats_3a *stats)
 {
-	const y_table_item_t *y_item = reinterpret_cast<const y_table_item_t *>(&stats->af_raw_buffer.y_table);
-	uint32_t afRawBufferLen;
-
 	/* Evaluate the AF buffer length */
-	afRawBufferLen = context.configuration.af.afGrid.width *
-			 context.configuration.af.afGrid.height;
+	uint32_t afRawBufferLen = context.configuration.af.afGrid.width *
+				  context.configuration.af.afGrid.height;
 
 	ASSERT(afRawBufferLen < IPU3_UAPI_AF_Y_TABLE_MAX_SIZE);
 
+	Span<const y_table_item_t> y_items(reinterpret_cast<const y_table_item_t *>(&stats->af_raw_buffer.y_table),
+					   afRawBufferLen);
+
 	/*
 	 * Calculate the mean and the variance of AF statistics for a given grid.
 	 * For coarse: y1 are used.
 	 * For fine: y2 results are used.
 	 */
-	currentVariance_ = afEstimateVariance(y_item, afRawBufferLen, !coarseCompleted_);
+	currentVariance_ = afEstimateVariance(y_items, !coarseCompleted_);
 
 	if (!context.frameContext.af.stable) {
 		afCoarseScan(context);
diff --git a/src/ipa/ipu3/algorithms/af.h b/src/ipa/ipu3/algorithms/af.h
index 3b5758e868ea..b85cf94163ff 100644
--- a/src/ipa/ipu3/algorithms/af.h
+++ b/src/ipa/ipu3/algorithms/af.h
@@ -41,8 +41,8 @@  private:
 	void afReset(IPAContext &context);
 	bool afNeedIgnoreFrame();
 	void afIgnoreFrameReset();
-	double afEstimateVariance(const y_table_item_t *y_item, uint32_t len,
-				  bool isY1);
+	double afEstimateVariance(Span<const y_table_item_t> y_items, bool isY1);
+
 	bool afIsOutOfFocus(IPAContext context);
 
 	/* VCM step configuration. It is the current setting of the VCM step. */