[libcamera-devel] [PATCH v3 12/14] ipa: ipu3: agc: Refactor condition on exposure correction

Jean-Michel Hautbois jeanmichel.hautbois at ideasonboard.com
Thu Oct 21 18:43:59 CEST 2021


Until now, we can't know when the exposure and gains applied in the
IPAIPU3::setControls() are really applied (it can be several frames). We
don't want to use the values calculated as if they are already applied,
and this is done by testing the frame number.

When the exposure is estimated, we verify if it changed enough for
exposure and gain to be updated.

Keep the indentation level removal and simply return when the change is
so small there is no need to apply the newly calculated value.

Signed-off-by: Jean-Michel Hautbois <jeanmichel.hautbois at ideasonboard.com>
Reviewed-by: Kieran Bingham <kieran.bingham at ideasonboard.com>
Reviewed-by: Laurent Pinchart <laurent.pinchart at ideasonboard.com>
---
 src/ipa/ipu3/algorithms/agc.cpp | 91 ++++++++++++++++-----------------
 1 file changed, 45 insertions(+), 46 deletions(-)

diff --git a/src/ipa/ipu3/algorithms/agc.cpp b/src/ipa/ipu3/algorithms/agc.cpp
index 43ef89df..7ff3029b 100644
--- a/src/ipa/ipu3/algorithms/agc.cpp
+++ b/src/ipa/ipu3/algorithms/agc.cpp
@@ -138,60 +138,59 @@ void Agc::lockExposureGain(uint32_t &exposure, double &analogueGain)
 	if ((frameCount_ < kInitialFrameMinAECount) || (frameCount_ - lastFrame_ < kFrameSkipCount))
 		return;
 
-	/* Are we correctly exposed ? */
-	if (std::abs(iqMean_ - kEvGainTarget * knumHistogramBins) <= 1) {
-		LOG(IPU3Agc, Debug) << "!!! Good exposure with iqMean = " << iqMean_;
-	} else {
-		double evGain = kEvGainTarget * knumHistogramBins / iqMean_;
+	if (std::abs(iqMean_ - kEvGainTarget * knumHistogramBins) <= 1)
+		return;
 
-		/* extracted from Rpi::Agc::computeTargetExposure */
-		utils::Duration currentShutter = exposure * lineDuration_;
-		currentExposureNoDg_ = currentShutter * analogueGain;
-		LOG(IPU3Agc, Debug) << "Actual total exposure " << currentExposureNoDg_
-				    << " Shutter speed " << currentShutter
-				    << " Gain " << analogueGain
-				    << " Needed ev gain " << evGain;
+	double evGain = kEvGainTarget * knumHistogramBins / iqMean_;
 
-		currentExposure_ = prevExposureValue_ * evGain;
-		utils::Duration minShutterSpeed = minExposureLines_ * lineDuration_;
-		utils::Duration maxShutterSpeed = maxExposureLines_ * lineDuration_;
+	/* extracted from Rpi::Agc::computeTargetExposure */
+	utils::Duration currentShutter = exposure * lineDuration_;
+	currentExposureNoDg_ = currentShutter * analogueGain;
+	LOG(IPU3Agc, Debug) << "Actual total exposure " << currentExposureNoDg_
+			    << " Shutter speed " << currentShutter
+			    << " Gain " << analogueGain
+			    << " Needed ev gain " << evGain;
 
-		utils::Duration maxTotalExposure = maxShutterSpeed * kMaxGain;
-		currentExposure_ = std::min(currentExposure_, maxTotalExposure);
-		LOG(IPU3Agc, Debug) << "Target total exposure " << currentExposure_
-				    << ", maximum is " << maxTotalExposure;
+	currentExposure_ = prevExposureValue_ * evGain;
+	utils::Duration minShutterSpeed = minExposureLines_ * lineDuration_;
+	utils::Duration maxShutterSpeed = maxExposureLines_ * lineDuration_;
 
-		/* \todo: estimate if we need to desaturate */
-		filterExposure();
+	utils::Duration maxTotalExposure = maxShutterSpeed * kMaxGain;
+	currentExposure_ = std::min(currentExposure_, maxTotalExposure);
+	LOG(IPU3Agc, Debug) << "Target total exposure " << currentExposure_
+			    << ", maximum is " << maxTotalExposure;
 
-		utils::Duration exposureValue = filteredExposure_;
-		utils::Duration shutterTime = minShutterSpeed;
+	/* \todo: estimate if we need to desaturate */
+	filterExposure();
 
-		/*
-		 * Push the shutter time up to the maximum first, and only then
-		 * increase the gain.
-		 */
-		shutterTime = std::clamp<utils::Duration>(exposureValue / kMinGain,
-							  minShutterSpeed, maxShutterSpeed);
-		double stepGain = std::clamp(exposureValue / shutterTime,
-					     kMinGain, kMaxGain);
-		LOG(IPU3Agc, Debug) << "Divided up shutter and gain are "
-				    << shutterTime << " and "
-				    << stepGain;
+	utils::Duration exposureValue = filteredExposure_;
+	utils::Duration shutterTime = minShutterSpeed;
 
-		exposure = shutterTime / lineDuration_;
-		analogueGain = stepGain;
+	/*
+	* Push the shutter time up to the maximum first, and only then
+	* increase the gain.
+	*/
+	shutterTime = std::clamp<utils::Duration>(exposureValue / kMinGain,
+						  minShutterSpeed, maxShutterSpeed);
+	double stepGain = std::clamp(exposureValue / shutterTime,
+				     kMinGain, kMaxGain);
+	LOG(IPU3Agc, Debug) << "Divided up shutter and gain are "
+			    << shutterTime << " and "
+			    << stepGain;
+
+	exposure = shutterTime / lineDuration_;
+	analogueGain = stepGain;
+
+	/*
+	 * Update the exposure value for the next process call.
+	 *
+	 * \todo Obtain the values of the exposure time and analog gain
+	 * that were actually used by the sensor, either from embedded
+	 * data when available, or from the delayed controls
+	 * infrastructure in case a slow down caused a mismatch.
+	 */
+	prevExposureValue_ = shutterTime * analogueGain;
 
-		/*
-		 * Update the exposure value for the next process call.
-		 *
-		 * \todo Obtain the values of the exposure time and analog gain
-		 * that were actually used by the sensor, either from embedded
-		 * data when available, or from the delayed controls
-		 * infrastructure in case a slow down caused a mismatch.
-		 */
-		prevExposureValue_ = shutterTime * analogueGain;
-	}
 	lastFrame_ = frameCount_;
 }
 
-- 
2.32.0



More information about the libcamera-devel mailing list