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

Message ID 20230322130612.5208-7-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
Generalise the contrast algorithm code by removing any hard-coded
assumptions about the target hardware platform. Instead, the algorithm
code creates a generic Pwl that gets returned to the IPA, where it gets
converted to the bcm2835 hardware specific lookup table.

As a drive-by, remove an unused mutex.

Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
Reviewed-by: David Plowman <david.plowman@raspberrypi.com>
---
 .../raspberrypi/controller/contrast_status.h  | 11 ++-----
 .../raspberrypi/controller/rpi/contrast.cpp   | 30 ++++---------------
 src/ipa/raspberrypi/controller/rpi/contrast.h |  1 -
 src/ipa/raspberrypi/raspberrypi.cpp           | 15 +++++++---
 4 files changed, 20 insertions(+), 37 deletions(-)

Comments

Jacopo Mondi March 24, 2023, 9:09 a.m. UTC | #1
Hi Naush

On Wed, Mar 22, 2023 at 01:06:08PM +0000, Naushir Patuck via libcamera-devel wrote:
> Generalise the contrast algorithm code by removing any hard-coded
> assumptions about the target hardware platform. Instead, the algorithm
> code creates a generic Pwl that gets returned to the IPA, where it gets
> converted to the bcm2835 hardware specific lookup table.
>
> As a drive-by, remove an unused mutex.
>
> Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
> Reviewed-by: David Plowman <david.plowman@raspberrypi.com>

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

Thanks
  j

> ---
>  .../raspberrypi/controller/contrast_status.h  | 11 ++-----
>  .../raspberrypi/controller/rpi/contrast.cpp   | 30 ++++---------------
>  src/ipa/raspberrypi/controller/rpi/contrast.h |  1 -
>  src/ipa/raspberrypi/raspberrypi.cpp           | 15 +++++++---
>  4 files changed, 20 insertions(+), 37 deletions(-)
>
> diff --git a/src/ipa/raspberrypi/controller/contrast_status.h b/src/ipa/raspberrypi/controller/contrast_status.h
> index ef2a7c680fc2..fb9fe4bace71 100644
> --- a/src/ipa/raspberrypi/controller/contrast_status.h
> +++ b/src/ipa/raspberrypi/controller/contrast_status.h
> @@ -6,20 +6,15 @@
>   */
>  #pragma once
>
> +#include "pwl.h"
> +
>  /*
>   * The "contrast" algorithm creates a gamma curve, optionally doing a little bit
>   * of contrast stretching based on the AGC histogram.
>   */
>
> -constexpr unsigned int ContrastNumPoints = 33;
> -
> -struct ContrastPoint {
> -	uint16_t x;
> -	uint16_t y;
> -};
> -
>  struct ContrastStatus {
> -	struct ContrastPoint points[ContrastNumPoints];
> +	RPiController::Pwl gammaCurve;
>  	double brightness;
>  	double contrast;
>  };
> diff --git a/src/ipa/raspberrypi/controller/rpi/contrast.cpp b/src/ipa/raspberrypi/controller/rpi/contrast.cpp
> index a4f8c4f04fc4..bee1eadd3657 100644
> --- a/src/ipa/raspberrypi/controller/rpi/contrast.cpp
> +++ b/src/ipa/raspberrypi/controller/rpi/contrast.cpp
> @@ -65,34 +65,19 @@ void Contrast::setContrast(double contrast)
>  	contrast_ = contrast;
>  }
>
> -static void fillInStatus(ContrastStatus &status, double brightness,
> -			 double contrast, Pwl &gammaCurve)
> -{
> -	status.brightness = brightness;
> -	status.contrast = contrast;
> -	for (unsigned int i = 0; i < ContrastNumPoints - 1; i++) {
> -		int x = i < 16 ? i * 1024
> -			       : (i < 24 ? (i - 16) * 2048 + 16384
> -					 : (i - 24) * 4096 + 32768);
> -		status.points[i].x = x;
> -		status.points[i].y = std::min(65535.0, gammaCurve.eval(x));
> -	}
> -	status.points[ContrastNumPoints - 1].x = 65535;
> -	status.points[ContrastNumPoints - 1].y = 65535;
> -}
> -
>  void Contrast::initialise()
>  {
>  	/*
>  	 * Fill in some default values as Prepare will run before Process gets
>  	 * called.
>  	 */
> -	fillInStatus(status_, brightness_, contrast_, config_.gammaCurve);
> +	status_.brightness = brightness_;
> +	status_.contrast = contrast_;
> +	status_.gammaCurve = config_.gammaCurve;
>  }
>
>  void Contrast::prepare(Metadata *imageMetadata)
>  {
> -	std::unique_lock<std::mutex> lock(mutex_);
>  	imageMetadata->set("contrast.status", status_);
>  }
>
> @@ -183,12 +168,9 @@ void Contrast::process(StatisticsPtr &stats,
>  	 * And fill in the status for output. Use more points towards the bottom
>  	 * of the curve.
>  	 */
> -	ContrastStatus status;
> -	fillInStatus(status, brightness_, contrast_, gammaCurve);
> -	{
> -		std::unique_lock<std::mutex> lock(mutex_);
> -		status_ = status;
> -	}
> +	status_.brightness = brightness_;
> +	status_.contrast = contrast_;
> +	status_.gammaCurve = std::move(gammaCurve);
>  }
>
>  /* Register algorithm with the system. */
> diff --git a/src/ipa/raspberrypi/controller/rpi/contrast.h b/src/ipa/raspberrypi/controller/rpi/contrast.h
> index c68adbc9e463..9c81277a0450 100644
> --- a/src/ipa/raspberrypi/controller/rpi/contrast.h
> +++ b/src/ipa/raspberrypi/controller/rpi/contrast.h
> @@ -46,7 +46,6 @@ private:
>  	double brightness_;
>  	double contrast_;
>  	ContrastStatus status_;
> -	std::mutex mutex_;
>  };
>
>  } /* namespace RPiController */
> diff --git a/src/ipa/raspberrypi/raspberrypi.cpp b/src/ipa/raspberrypi/raspberrypi.cpp
> index 0fa79bb4af41..b6e5465ca32a 100644
> --- a/src/ipa/raspberrypi/raspberrypi.cpp
> +++ b/src/ipa/raspberrypi/raspberrypi.cpp
> @@ -1592,14 +1592,21 @@ void IPARPi::applyCCM(const struct CcmStatus *ccmStatus, ControlList &ctrls)
>
>  void IPARPi::applyGamma(const struct ContrastStatus *contrastStatus, ControlList &ctrls)
>  {
> +	const unsigned int numGammaPoints = controller_.getHardwareConfig().numGammaPoints;
>  	struct bcm2835_isp_gamma gamma;
>
> -	gamma.enabled = 1;
> -	for (unsigned int i = 0; i < ContrastNumPoints; i++) {
> -		gamma.x[i] = contrastStatus->points[i].x;
> -		gamma.y[i] = contrastStatus->points[i].y;
> +	for (unsigned int i = 0; i < numGammaPoints - 1; i++) {
> +		int x = i < 16 ? i * 1024
> +			       : (i < 24 ? (i - 16) * 2048 + 16384
> +					 : (i - 24) * 4096 + 32768);
> +		gamma.x[i] = x;
> +		gamma.y[i] = std::min<uint16_t>(65535, contrastStatus->gammaCurve.eval(x));
>  	}
>
> +	gamma.x[numGammaPoints - 1] = 65535;
> +	gamma.y[numGammaPoints - 1] = 65535;
> +	gamma.enabled = 1;
> +
>  	ControlValue c(Span<const uint8_t>{ reinterpret_cast<uint8_t *>(&gamma),
>  					    sizeof(gamma) });
>  	ctrls.set(V4L2_CID_USER_BCM2835_ISP_GAMMA, c);
> --
> 2.34.1
>

Patch
diff mbox series

diff --git a/src/ipa/raspberrypi/controller/contrast_status.h b/src/ipa/raspberrypi/controller/contrast_status.h
index ef2a7c680fc2..fb9fe4bace71 100644
--- a/src/ipa/raspberrypi/controller/contrast_status.h
+++ b/src/ipa/raspberrypi/controller/contrast_status.h
@@ -6,20 +6,15 @@ 
  */
 #pragma once
 
+#include "pwl.h"
+
 /*
  * The "contrast" algorithm creates a gamma curve, optionally doing a little bit
  * of contrast stretching based on the AGC histogram.
  */
 
-constexpr unsigned int ContrastNumPoints = 33;
-
-struct ContrastPoint {
-	uint16_t x;
-	uint16_t y;
-};
-
 struct ContrastStatus {
-	struct ContrastPoint points[ContrastNumPoints];
+	RPiController::Pwl gammaCurve;
 	double brightness;
 	double contrast;
 };
diff --git a/src/ipa/raspberrypi/controller/rpi/contrast.cpp b/src/ipa/raspberrypi/controller/rpi/contrast.cpp
index a4f8c4f04fc4..bee1eadd3657 100644
--- a/src/ipa/raspberrypi/controller/rpi/contrast.cpp
+++ b/src/ipa/raspberrypi/controller/rpi/contrast.cpp
@@ -65,34 +65,19 @@  void Contrast::setContrast(double contrast)
 	contrast_ = contrast;
 }
 
-static void fillInStatus(ContrastStatus &status, double brightness,
-			 double contrast, Pwl &gammaCurve)
-{
-	status.brightness = brightness;
-	status.contrast = contrast;
-	for (unsigned int i = 0; i < ContrastNumPoints - 1; i++) {
-		int x = i < 16 ? i * 1024
-			       : (i < 24 ? (i - 16) * 2048 + 16384
-					 : (i - 24) * 4096 + 32768);
-		status.points[i].x = x;
-		status.points[i].y = std::min(65535.0, gammaCurve.eval(x));
-	}
-	status.points[ContrastNumPoints - 1].x = 65535;
-	status.points[ContrastNumPoints - 1].y = 65535;
-}
-
 void Contrast::initialise()
 {
 	/*
 	 * Fill in some default values as Prepare will run before Process gets
 	 * called.
 	 */
-	fillInStatus(status_, brightness_, contrast_, config_.gammaCurve);
+	status_.brightness = brightness_;
+	status_.contrast = contrast_;
+	status_.gammaCurve = config_.gammaCurve;
 }
 
 void Contrast::prepare(Metadata *imageMetadata)
 {
-	std::unique_lock<std::mutex> lock(mutex_);
 	imageMetadata->set("contrast.status", status_);
 }
 
@@ -183,12 +168,9 @@  void Contrast::process(StatisticsPtr &stats,
 	 * And fill in the status for output. Use more points towards the bottom
 	 * of the curve.
 	 */
-	ContrastStatus status;
-	fillInStatus(status, brightness_, contrast_, gammaCurve);
-	{
-		std::unique_lock<std::mutex> lock(mutex_);
-		status_ = status;
-	}
+	status_.brightness = brightness_;
+	status_.contrast = contrast_;
+	status_.gammaCurve = std::move(gammaCurve);
 }
 
 /* Register algorithm with the system. */
diff --git a/src/ipa/raspberrypi/controller/rpi/contrast.h b/src/ipa/raspberrypi/controller/rpi/contrast.h
index c68adbc9e463..9c81277a0450 100644
--- a/src/ipa/raspberrypi/controller/rpi/contrast.h
+++ b/src/ipa/raspberrypi/controller/rpi/contrast.h
@@ -46,7 +46,6 @@  private:
 	double brightness_;
 	double contrast_;
 	ContrastStatus status_;
-	std::mutex mutex_;
 };
 
 } /* namespace RPiController */
diff --git a/src/ipa/raspberrypi/raspberrypi.cpp b/src/ipa/raspberrypi/raspberrypi.cpp
index 0fa79bb4af41..b6e5465ca32a 100644
--- a/src/ipa/raspberrypi/raspberrypi.cpp
+++ b/src/ipa/raspberrypi/raspberrypi.cpp
@@ -1592,14 +1592,21 @@  void IPARPi::applyCCM(const struct CcmStatus *ccmStatus, ControlList &ctrls)
 
 void IPARPi::applyGamma(const struct ContrastStatus *contrastStatus, ControlList &ctrls)
 {
+	const unsigned int numGammaPoints = controller_.getHardwareConfig().numGammaPoints;
 	struct bcm2835_isp_gamma gamma;
 
-	gamma.enabled = 1;
-	for (unsigned int i = 0; i < ContrastNumPoints; i++) {
-		gamma.x[i] = contrastStatus->points[i].x;
-		gamma.y[i] = contrastStatus->points[i].y;
+	for (unsigned int i = 0; i < numGammaPoints - 1; i++) {
+		int x = i < 16 ? i * 1024
+			       : (i < 24 ? (i - 16) * 2048 + 16384
+					 : (i - 24) * 4096 + 32768);
+		gamma.x[i] = x;
+		gamma.y[i] = std::min<uint16_t>(65535, contrastStatus->gammaCurve.eval(x));
 	}
 
+	gamma.x[numGammaPoints - 1] = 65535;
+	gamma.y[numGammaPoints - 1] = 65535;
+	gamma.enabled = 1;
+
 	ControlValue c(Span<const uint8_t>{ reinterpret_cast<uint8_t *>(&gamma),
 					    sizeof(gamma) });
 	ctrls.set(V4L2_CID_USER_BCM2835_ISP_GAMMA, c);