[libcamera-devel,v2,06/13] ipa: ipu3: agc: Change exposure limits
diff mbox series

Message ID 20211020154607.180161-7-jeanmichel.hautbois@ideasonboard.com
State Superseded
Headers show
Series
  • ipa: ipu3: Fix AGC bugs
Related show

Commit Message

Jean-Michel Hautbois Oct. 20, 2021, 3:46 p.m. UTC
The exposure limits are set for one sensor until now, in a number of
lines. We should not use those, but exposure limits in a time unit.

Introduce default limits which with a default minimum of 20ms. This
value should be given by the IPAIPU3. Use cached values expressed as a
number of lines in the configure() call.

Adapt the process() call accordingly.

Signed-off-by: Jean-Michel Hautbois <jeanmichel.hautbois@ideasonboard.com>
Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
---
 src/ipa/ipu3/algorithms/agc.cpp | 34 ++++++++++++++++++---------------
 src/ipa/ipu3/algorithms/agc.h   |  3 ++-
 2 files changed, 21 insertions(+), 16 deletions(-)

Comments

Laurent Pinchart Oct. 21, 2021, 2:23 a.m. UTC | #1
Hi Jean-Michel,

Thank you for the patch.

On Wed, Oct 20, 2021 at 05:46:00PM +0200, Jean-Michel Hautbois wrote:
> The exposure limits are set for one sensor until now, in a number of
> lines. We should not use those, but exposure limits in a time unit.
> 
> Introduce default limits which with a default minimum of 20ms. This
> value should be given by the IPAIPU3. Use cached values expressed as a
> number of lines in the configure() call.

The commit message doesn't match the patch anymore.

> Adapt the process() call accordingly.
> 
> Signed-off-by: Jean-Michel Hautbois <jeanmichel.hautbois@ideasonboard.com>
> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
> ---
>  src/ipa/ipu3/algorithms/agc.cpp | 34 ++++++++++++++++++---------------
>  src/ipa/ipu3/algorithms/agc.h   |  3 ++-
>  2 files changed, 21 insertions(+), 16 deletions(-)
> 
> diff --git a/src/ipa/ipu3/algorithms/agc.cpp b/src/ipa/ipu3/algorithms/agc.cpp
> index b1757b91..8e1b6d8c 100644
> --- a/src/ipa/ipu3/algorithms/agc.cpp
> +++ b/src/ipa/ipu3/algorithms/agc.cpp
> @@ -39,18 +39,14 @@ static constexpr uint32_t kMaxISO = 1500;
>  static constexpr uint32_t kMinGain = kMinISO / 100;
>  static constexpr uint32_t kMaxGain = kMaxISO / 100;
>  
> -/* \todo use calculated value based on sensor */
> -static constexpr uint32_t kMinExposure = 1;
> -static constexpr uint32_t kMaxExposure = 1976;
> -
>  /* Histogram constants */
>  static constexpr uint32_t knumHistogramBins = 256;
>  static constexpr double kEvGainTarget = 0.5;
>  
>  Agc::Agc()
>  	: frameCount_(0), lastFrame_(0), iqMean_(0.0), lineDuration_(0s),
> -	  maxExposureTime_(0s), filteredExposure_(0s), filteredExposureNoDg_(0s),
> -	  currentExposure_(0s), currentExposureNoDg_(0s)
> +	  minExposureLines_(0), maxExposureLines_(0), filteredExposure_(0s),
> +	  filteredExposureNoDg_(0s), currentExposure_(0s), currentExposureNoDg_(0s)
>  {
>  }
>  
> @@ -60,13 +56,14 @@ int Agc::configure(IPAContext &context, const IPAConfigInfo &configInfo)
>  
>  	lineDuration_ = configInfo.sensorInfo.lineLength * 1.0s
>  		      / configInfo.sensorInfo.pixelRate;
> -	maxExposureTime_ = context.configuration.agc.maxShutterSpeed;
>  
>  	/* Configure the default exposure and gain */
>  	context.frameContext.agc.gain =
>  		context.configuration.agc.minAnalogueGain;
> -	context.frameContext.agc.exposure =
> -		context.configuration.agc.minShutterSpeed / lineDuration_;
> +	context.frameContext.agc.exposure = minExposureLines_;

You use minExposureLines_ before calculating it below.

> +
> +	minExposureLines_ = context.configuration.agc.minShutterSpeed / lineDuration_;
> +	maxExposureLines_ = context.configuration.agc.maxShutterSpeed / lineDuration_;
>  
>  	return 0;
>  }
> @@ -151,23 +148,30 @@ void Agc::lockExposureGain(uint32_t &exposure, double &gain)
>  		LOG(IPU3Agc, Debug) << "Actual total exposure " << currentExposureNoDg_
>  				    << " Shutter speed " << currentShutter
>  				    << " Gain " << gain;
> +
>  		currentExposure_ = currentExposureNoDg_ * newGain;
> -		utils::Duration maxTotalExposure = maxExposureTime_ * kMaxGain;
> +		utils::Duration maxShutterSpeed = maxExposureLines_ * lineDuration_;
> +		utils::Duration maxTotalExposure = maxShutterSpeed * kMaxGain;
>  		currentExposure_ = std::min(currentExposure_, maxTotalExposure);
> -		LOG(IPU3Agc, Debug) << "Target total exposure " << currentExposure_;
> +		LOG(IPU3Agc, Debug) << "Target total exposure " << currentExposure_
> +				    << ", maximum is " << maxTotalExposure;
>  
>  		/* \todo: estimate if we need to desaturate */
>  		filterExposure();
>  
>  		utils::Duration newExposure = 0.0s;
> -		if (currentShutter < maxExposureTime_) {
> -			exposure = std::clamp(static_cast<uint32_t>(exposure * currentExposure_ / currentExposureNoDg_), kMinExposure, kMaxExposure);
> +		if (currentShutter < maxShutterSpeed) {
> +			exposure = std::clamp<uint32_t>(exposure * currentExposure_ / currentExposureNoDg_,
> +							minExposureLines_,
> +							maxExposureLines_);
>  			newExposure = currentExposure_ / exposure;
>  			gain = std::clamp(static_cast<uint32_t>(gain * currentExposure_ / newExposure), kMinGain, kMaxGain);
> -		} else if (currentShutter >= maxExposureTime_) {
> +		} else if (currentShutter >= maxShutterSpeed) {

You could simply write this as

		} else {

With these issues fixed,

Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

>  			gain = std::clamp(static_cast<uint32_t>(gain * currentExposure_ / currentExposureNoDg_), kMinGain, kMaxGain);
>  			newExposure = currentExposure_ / gain;
> -			exposure = std::clamp(static_cast<uint32_t>(exposure * currentExposure_ / newExposure), kMinExposure, kMaxExposure);
> +			exposure = std::clamp<uint32_t>(exposure * currentExposure_ / newExposure,
> +							minExposureLines_,
> +							maxExposureLines_);
>  		}
>  		LOG(IPU3Agc, Debug) << "Adjust exposure " << exposure * lineDuration_ << " and gain " << gain;
>  	}
> diff --git a/src/ipa/ipu3/algorithms/agc.h b/src/ipa/ipu3/algorithms/agc.h
> index 16b9fa0a..cd26d08c 100644
> --- a/src/ipa/ipu3/algorithms/agc.h
> +++ b/src/ipa/ipu3/algorithms/agc.h
> @@ -42,7 +42,8 @@ private:
>  	double iqMean_;
>  
>  	utils::Duration lineDuration_;
> -	utils::Duration maxExposureTime_;
> +	uint32_t minExposureLines_;
> +	uint32_t maxExposureLines_;
>  
>  	utils::Duration filteredExposure_;
>  	utils::Duration filteredExposureNoDg_;

Patch
diff mbox series

diff --git a/src/ipa/ipu3/algorithms/agc.cpp b/src/ipa/ipu3/algorithms/agc.cpp
index b1757b91..8e1b6d8c 100644
--- a/src/ipa/ipu3/algorithms/agc.cpp
+++ b/src/ipa/ipu3/algorithms/agc.cpp
@@ -39,18 +39,14 @@  static constexpr uint32_t kMaxISO = 1500;
 static constexpr uint32_t kMinGain = kMinISO / 100;
 static constexpr uint32_t kMaxGain = kMaxISO / 100;
 
-/* \todo use calculated value based on sensor */
-static constexpr uint32_t kMinExposure = 1;
-static constexpr uint32_t kMaxExposure = 1976;
-
 /* Histogram constants */
 static constexpr uint32_t knumHistogramBins = 256;
 static constexpr double kEvGainTarget = 0.5;
 
 Agc::Agc()
 	: frameCount_(0), lastFrame_(0), iqMean_(0.0), lineDuration_(0s),
-	  maxExposureTime_(0s), filteredExposure_(0s), filteredExposureNoDg_(0s),
-	  currentExposure_(0s), currentExposureNoDg_(0s)
+	  minExposureLines_(0), maxExposureLines_(0), filteredExposure_(0s),
+	  filteredExposureNoDg_(0s), currentExposure_(0s), currentExposureNoDg_(0s)
 {
 }
 
@@ -60,13 +56,14 @@  int Agc::configure(IPAContext &context, const IPAConfigInfo &configInfo)
 
 	lineDuration_ = configInfo.sensorInfo.lineLength * 1.0s
 		      / configInfo.sensorInfo.pixelRate;
-	maxExposureTime_ = context.configuration.agc.maxShutterSpeed;
 
 	/* Configure the default exposure and gain */
 	context.frameContext.agc.gain =
 		context.configuration.agc.minAnalogueGain;
-	context.frameContext.agc.exposure =
-		context.configuration.agc.minShutterSpeed / lineDuration_;
+	context.frameContext.agc.exposure = minExposureLines_;
+
+	minExposureLines_ = context.configuration.agc.minShutterSpeed / lineDuration_;
+	maxExposureLines_ = context.configuration.agc.maxShutterSpeed / lineDuration_;
 
 	return 0;
 }
@@ -151,23 +148,30 @@  void Agc::lockExposureGain(uint32_t &exposure, double &gain)
 		LOG(IPU3Agc, Debug) << "Actual total exposure " << currentExposureNoDg_
 				    << " Shutter speed " << currentShutter
 				    << " Gain " << gain;
+
 		currentExposure_ = currentExposureNoDg_ * newGain;
-		utils::Duration maxTotalExposure = maxExposureTime_ * kMaxGain;
+		utils::Duration maxShutterSpeed = maxExposureLines_ * lineDuration_;
+		utils::Duration maxTotalExposure = maxShutterSpeed * kMaxGain;
 		currentExposure_ = std::min(currentExposure_, maxTotalExposure);
-		LOG(IPU3Agc, Debug) << "Target total exposure " << currentExposure_;
+		LOG(IPU3Agc, Debug) << "Target total exposure " << currentExposure_
+				    << ", maximum is " << maxTotalExposure;
 
 		/* \todo: estimate if we need to desaturate */
 		filterExposure();
 
 		utils::Duration newExposure = 0.0s;
-		if (currentShutter < maxExposureTime_) {
-			exposure = std::clamp(static_cast<uint32_t>(exposure * currentExposure_ / currentExposureNoDg_), kMinExposure, kMaxExposure);
+		if (currentShutter < maxShutterSpeed) {
+			exposure = std::clamp<uint32_t>(exposure * currentExposure_ / currentExposureNoDg_,
+							minExposureLines_,
+							maxExposureLines_);
 			newExposure = currentExposure_ / exposure;
 			gain = std::clamp(static_cast<uint32_t>(gain * currentExposure_ / newExposure), kMinGain, kMaxGain);
-		} else if (currentShutter >= maxExposureTime_) {
+		} else if (currentShutter >= maxShutterSpeed) {
 			gain = std::clamp(static_cast<uint32_t>(gain * currentExposure_ / currentExposureNoDg_), kMinGain, kMaxGain);
 			newExposure = currentExposure_ / gain;
-			exposure = std::clamp(static_cast<uint32_t>(exposure * currentExposure_ / newExposure), kMinExposure, kMaxExposure);
+			exposure = std::clamp<uint32_t>(exposure * currentExposure_ / newExposure,
+							minExposureLines_,
+							maxExposureLines_);
 		}
 		LOG(IPU3Agc, Debug) << "Adjust exposure " << exposure * lineDuration_ << " and gain " << gain;
 	}
diff --git a/src/ipa/ipu3/algorithms/agc.h b/src/ipa/ipu3/algorithms/agc.h
index 16b9fa0a..cd26d08c 100644
--- a/src/ipa/ipu3/algorithms/agc.h
+++ b/src/ipa/ipu3/algorithms/agc.h
@@ -42,7 +42,8 @@  private:
 	double iqMean_;
 
 	utils::Duration lineDuration_;
-	utils::Duration maxExposureTime_;
+	uint32_t minExposureLines_;
+	uint32_t maxExposureLines_;
 
 	utils::Duration filteredExposure_;
 	utils::Duration filteredExposureNoDg_;