From 355503233baae4ebf4ad6fd64023e66d4b05cb7e Mon Sep 17 00:00:00 2001
From: Jean-Michel Hautbois <jeanmichel.hautbois@ideasonboard.com>
Date: Tue, 12 Oct 2021 16:41:20 +0200
Subject: ipa: ipu3: agc: Change exposure limits

We are using arbitrary constants for the exposure limit in a number of
lines.

Instead of using static constants for those, use the limits of the
sensor passed in IPASessionConfiguration and cache those.

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

(limited to 'src/ipa/ipu3/algorithms')

diff --git a/src/ipa/ipu3/algorithms/agc.cpp b/src/ipa/ipu3/algorithms/agc.cpp
index 62ea0916..58872410 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,15 @@ int Agc::configure(IPAContext &context, const IPAConfigInfo &configInfo)
 
 	lineDuration_ = configInfo.sensorInfo.lineLength * 1.0s
 		      / configInfo.sensorInfo.pixelRate;
-	maxExposureTime_ = context.configuration.agc.maxShutterSpeed;
+
+	/* \todo replace the exposure in lines storage with time based ones. */
+	minExposureLines_ = context.configuration.agc.minShutterSpeed / lineDuration_;
+	maxExposureLines_ = context.configuration.agc.maxShutterSpeed / lineDuration_;
 
 	/* 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_;
 
 	return 0;
 }
@@ -151,23 +149,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 {
 			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_;
-- 
cgit v1.2.1