[libcamera-devel] [PATCH 06/13] ipa: ipu3: agc: Change exposure limits

Jean-Michel Hautbois jeanmichel.hautbois at ideasonboard.com
Wed Oct 13 17:41:18 CEST 2021


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 at ideasonboard.com>
---
 src/ipa/ipu3/algorithms/agc.cpp | 30 +++++++++++++++++-------------
 src/ipa/ipu3/algorithms/agc.h   |  3 ++-
 2 files changed, 19 insertions(+), 14 deletions(-)

diff --git a/src/ipa/ipu3/algorithms/agc.cpp b/src/ipa/ipu3/algorithms/agc.cpp
index 7c2d4201..2dd5c5c1 100644
--- a/src/ipa/ipu3/algorithms/agc.cpp
+++ b/src/ipa/ipu3/algorithms/agc.cpp
@@ -40,8 +40,8 @@ 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;
+static constexpr libcamera::utils::Duration kMinShutterSpeed = 100us;
+static constexpr libcamera::utils::Duration kMaxShutterSpeed = 20ms;
 
 /* Histogram constants */
 static constexpr uint32_t knumHistogramBins = 256;
@@ -49,8 +49,8 @@ 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,7 +60,9 @@ int Agc::configure(IPAContext &context, const IPAConfigInfo &configInfo)
 
 	lineDuration_ = configInfo.sensorInfo.lineLength * 1.0s
 		      / configInfo.sensorInfo.pixelRate;
-	maxExposureTime_ = kMaxExposure * lineDuration_;
+
+	minExposureLines_ = kMinShutterSpeed / lineDuration_;
+	maxExposureLines_ = kMaxShutterSpeed / lineDuration_;
 
 	return 0;
 }
@@ -140,28 +142,30 @@ void Agc::lockExposureGain(uint32_t &exposure, double &gain)
 		double newGain = kEvGainTarget * knumHistogramBins / iqMean_;
 
 		/* extracted from Rpi::Agc::computeTargetExposure */
-		libcamera::utils::Duration currentShutter = exposure * lineDuration_;
+		Duration currentShutter = exposure * lineDuration_;
 		currentExposureNoDg_ = currentShutter * gain;
 		LOG(IPU3Agc, Debug) << "Actual total exposure " << currentExposureNoDg_
 				    << " Shutter speed " << currentShutter
 				    << " Gain " << gain;
+
 		currentExposure_ = currentExposureNoDg_ * newGain;
-		libcamera::utils::Duration maxTotalExposure = maxExposureTime_ * kMaxGain;
+		Duration maxTotalExposure = kMaxShutterSpeed * 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();
 
-		libcamera::utils::Duration newExposure = 0.0s;
-		if (currentShutter < maxExposureTime_) {
-			exposure = std::clamp(static_cast<uint32_t>(exposure * currentExposure_ / currentExposureNoDg_), kMinExposure, kMaxExposure);
+		Duration newExposure = 0.0s;
+		if (currentShutter < kMaxShutterSpeed) {
+			exposure = std::clamp(static_cast<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 >= kMaxShutterSpeed) {
 			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(static_cast<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 cd4d4855..7605ab39 100644
--- a/src/ipa/ipu3/algorithms/agc.h
+++ b/src/ipa/ipu3/algorithms/agc.h
@@ -44,7 +44,8 @@ private:
 	double iqMean_;
 
 	Duration lineDuration_;
-	Duration maxExposureTime_;
+	uint32_t minExposureLines_;
+	uint32_t maxExposureLines_;
 
 	Duration filteredExposure_;
 	Duration filteredExposureNoDg_;
-- 
2.30.2



More information about the libcamera-devel mailing list