[06/10] libipa: agc: Implement DigitalGainMode support
diff mbox series

Message ID 20261117-sklug-digital-gain-support-v1-v1-6-f0aa342ca877@ideasonboard.com
State New
Headers show
Series
  • libipa: agc: Add digital gain support
Related show

Commit Message

Stefan Klug Nov. 17, 2026, 8:47 a.m. UTC
Implement DigitalGainMode support for the agc algorithm.
By default DigitalGainMode is set to Auto. For digital gain to be
enabled the configuration passed to AgcAlgorithm::configure() must
contain a maxDigitalGain value > 1 and the tuning file must also contain
a maxDigitalGain value > 1.

Signed-off-by: Stefan Klug <stefan.klug@ideasonboard.com>
---
 src/ipa/libipa/agc.cpp | 91 ++++++++++++++++++++++++++++++++++++++++++++++++--
 src/ipa/libipa/agc.h   |  7 ++++
 2 files changed, 96 insertions(+), 2 deletions(-)

Comments

Barnabás Pőcze Nov. 17, 2026, 11:11 a.m. UTC | #1
2026. 11. 17. 9:47 keltezéssel, Stefan Klug írta:
> Implement DigitalGainMode support for the agc algorithm.
> By default DigitalGainMode is set to Auto. For digital gain to be
> enabled the configuration passed to AgcAlgorithm::configure() must
> contain a maxDigitalGain value > 1 and the tuning file must also contain
> a maxDigitalGain value > 1.
> 
> Signed-off-by: Stefan Klug <stefan.klug@ideasonboard.com>
> ---
>   src/ipa/libipa/agc.cpp | 91 ++++++++++++++++++++++++++++++++++++++++++++++++--
>   src/ipa/libipa/agc.h   |  7 ++++
>   2 files changed, 96 insertions(+), 2 deletions(-)
> 
> diff --git a/src/ipa/libipa/agc.cpp b/src/ipa/libipa/agc.cpp
> index c8924f3c3fbb..f680bb93f8ed 100644
> --- a/src/ipa/libipa/agc.cpp
> +++ b/src/ipa/libipa/agc.cpp
> @@ -380,6 +380,7 @@ int AgcAlgorithm::configure(agc::Session &session, agc::ActiveState &state,
>   	session.autoAllowed = config.autoAllowed;
>   	session.lineDuration = lineLength * 1.0s / config.sensorInfo.pixelRate;
>   	session.sensor.outputSize = config.sensorInfo.outputSize;
> +	session.maxDigitalGain = config.maxDigitalGain;
>   
>   	auto exposureMargin = sensor_ ? sensor_->exposureMargin() : std::nullopt;
>   	session.sensor.exposureMargin = exposureMargin.value_or(4);
> @@ -455,8 +456,10 @@ int AgcAlgorithm::configure(agc::Session &session, agc::ActiveState &state,
>   	state.automatic.digitalGain = 1;
>   	state.manual.gain = state.automatic.gain;
>   	state.manual.exposure = state.automatic.exposure;
> +	state.manual.digitalGain = 1.0;
>   	state.autoExposureEnabled = session.autoAllowed;
>   	state.autoGainEnabled = session.autoAllowed;
> +	state.autoDigitalGainEnabled = session.autoAllowed;
>   	state.exposureValue = 0;
>   	state.minFrameDuration = session.minFrameDuration;
>   	state.maxFrameDuration = session.maxFrameDuration;
> @@ -504,6 +507,14 @@ int AgcAlgorithm::configure(agc::Session &session, agc::ActiveState &state,
>   				return;
>   
>   			config.ctrlMap[&controls::ExposureValue] = ControlInfo(-8.0f, 8.0f, 0.0f);
> +			if (session.maxDigitalGain > 1.0) {
> +				config.ctrlMap[&controls::DigitalGain] =
> +					ControlInfo(1.0f, static_cast<float>(session.maxDigitalGain), 1.0f);
> +				config.ctrlMap[&controls::DigitalGainMode] =
> +					ControlInfo({ { ControlValue(controls::DigitalGainModeAuto),
> +							ControlValue(controls::DigitalGainModeManual) } },
> +							ControlValue(controls::DigitalGainModeAuto));
> +			}
>   
>   			std::vector<ControlValue> options;
>   			for (const auto &[id, _] : impl.constraintModes())
> @@ -634,6 +645,23 @@ void AgcAlgorithm::queueRequest(const agc::Session &session, agc::ActiveState &s
>   			if (!state.autoGainEnabled && !controls.get(controls::AnalogueGain))
>   				frameContext.autoGainModeChange = true;
>   		}
> +
> +		const auto &dgMode = controls.get(controls::DigitalGainMode);
> +		if (dgMode &&
> +		    (*dgMode == controls::DigitalGainModeAuto) != state.autoDigitalGainEnabled) {
> +			state.autoDigitalGainEnabled = (*dgMode == controls::AnalogueGainModeAuto);
> +
> +			LOG(Agc, Debug)
> +				<< (state.autoDigitalGainEnabled ? "Enabling" : "Disabling")
> +				<< " AGC (digital gain)";
> +			/*
> +			 * If we go from auto -> manual with no manual control
> +			 * set, use the last computed value, which we don't
> +			 * know until prepare() so save this information.
> +			 */
> +			if (!state.autoDigitalGainEnabled && !controls.get(controls::DigitalGain))
> +				frameContext.autoDigitalGainModeChange = true;
> +		}
>   	}
>   
>   	const auto &frameDurationLimits = controls.get(controls::FrameDurationLimits);
> @@ -664,14 +692,31 @@ void AgcAlgorithm::queueRequest(const agc::Session &session, agc::ActiveState &s
>   		LOG(Agc, Debug) << "Set gain to " << state.manual.gain;
>   	}
>   
> +	const auto &digitalGain = controls.get(controls::DigitalGain);
> +	if (digitalGain && !state.autoDigitalGainEnabled) {
> +		if (session.maxDigitalGain > 1.0) {
> +			state.manual.digitalGain = *digitalGain;

Should it be clamped to [1; session.maxDigitalGain] ? Wouldn't it be sufficient to just do

   state.manual.digitalGain = std::clamp<double>(*digitalGain, 1.0, session.maxDigitalGain);

unconditionally?


> +			LOG(Agc, Debug) << "Set digital gain to " << state.manual.digitalGain;
> +		} else {
> +			LOG(Agc, Warning) << "DigitalGain is not supported";

This shouldn't be possible, so I'd drop it.


> +		}
> +	}
> +
>   	frameContext.autoExposureEnabled = state.autoExposureEnabled;
>   	frameContext.autoGainEnabled = state.autoGainEnabled;
> +	frameContext.autoDigitalGainEnabled = state.autoDigitalGainEnabled;
>   
>   	if (!frameContext.autoExposureEnabled)
>   		frameContext.exposure = clampExposure(session, frameContext, state.manual.exposure);
> +
>   	if (!frameContext.autoGainEnabled)
>   		frameContext.gain = state.manual.gain;
>   
> +	if (frameContext.autoDigitalGainEnabled)
> +		frameContext.digitalGain = state.automatic.digitalGain;

Shouldn't this be only done in `prepare()` to keep the existing pattern?
I imagine this is related to the PFC rework? But I still it would be preferable
to keep the same pattern for now.


> +	else
> +		frameContext.digitalGain = state.manual.digitalGain;
> +
>   	if (!frameContext.autoExposureEnabled && !frameContext.autoGainEnabled)
>   		frameContext.quantizationGain = 1.0;
>   
> [...]
> @@ -842,9 +910,18 @@ void AgcAlgorithm::process(const agc::Session &session, agc::ActiveState &state,
>   			utils::Duration effectiveExposureValue =
>   				lineDuration * params->exposure * params->gain;
>   
> +			/*
> +			 * Include the quantization gain if it was applied. Do not use
> +			 * compress.gain because it will include gains that shall not be
> +			 * reported to the user when HDR is implemented.

This comment seems unrelated? Or at least rkisp1 specific?


> +			 */
> +			if (session.maxDigitalGain > 1.0)
> +				effectiveExposureValue *= frameContext.quantizationGain
> +							* frameContext.digitalGain;

My understanding is that the effective exposure should reflect the value that was in
effect when the luminance histogram is sampled. So doing this means additional requirements:

   - `frameContext.quantizationGain * frameContext.digitalGain` must be applied as "digital gain"
   - the digital gain must apply "immediately"
   - the luminance histogram must be sampled after this digital gain has been applied

And with that the documentation of `ProcessParams::gain` should be adjusted to say "analogue gain"
instead of just "gain". When I created that originally, I was envisioning that it would be up to
the platform layer to collect all the gains and do the multiplication.


> +
>   			impl.setLimits(minExposureTime, maxExposureTime,
>   				       minAnalogueGain, maxAnalogueGain,
> -				       1.0, 1.0,
> +				       minDigitalGain, maxDigitalGain,
>   				       std::move(params->additionalConstraints));
>   
>   			const auto &newEv = impl.calculateNewEv({
> @@ -932,6 +1009,16 @@ void AgcAlgorithm::fillMetadata(const agc::Session &session,
>   		metadata.set(controls::AeConstraintMode, frameContext.constraintMode);
>   		metadata.set(controls::ExposureValue, frameContext.exposureValue);
>   	}
> +
> +	/*
> +	 * Add the quantization gain to the digital gain metadata, so that
> +	 * we don't apply any hidden gains.
> +	 * \todo This might be unexpected for applications trying to match a
> +	 * digital gain control with the metadata, as it will always be off by
> +	 * the quantization gain.
> +	 */
> +	metadata.set(controls::DigitalGain,
> +		     frameContext.quantizationGain * frameContext.digitalGain);
>   }
>   
>   } /* namespace ipa */
> diff --git a/src/ipa/libipa/agc.h b/src/ipa/libipa/agc.h
> index 9b4bcf0bed1c..c4e0cb6be8a4 100644
> --- a/src/ipa/libipa/agc.h
> +++ b/src/ipa/libipa/agc.h
> @@ -41,6 +41,7 @@ struct Session {
> [...]

Patch
diff mbox series

diff --git a/src/ipa/libipa/agc.cpp b/src/ipa/libipa/agc.cpp
index c8924f3c3fbb..f680bb93f8ed 100644
--- a/src/ipa/libipa/agc.cpp
+++ b/src/ipa/libipa/agc.cpp
@@ -380,6 +380,7 @@  int AgcAlgorithm::configure(agc::Session &session, agc::ActiveState &state,
 	session.autoAllowed = config.autoAllowed;
 	session.lineDuration = lineLength * 1.0s / config.sensorInfo.pixelRate;
 	session.sensor.outputSize = config.sensorInfo.outputSize;
+	session.maxDigitalGain = config.maxDigitalGain;
 
 	auto exposureMargin = sensor_ ? sensor_->exposureMargin() : std::nullopt;
 	session.sensor.exposureMargin = exposureMargin.value_or(4);
@@ -455,8 +456,10 @@  int AgcAlgorithm::configure(agc::Session &session, agc::ActiveState &state,
 	state.automatic.digitalGain = 1;
 	state.manual.gain = state.automatic.gain;
 	state.manual.exposure = state.automatic.exposure;
+	state.manual.digitalGain = 1.0;
 	state.autoExposureEnabled = session.autoAllowed;
 	state.autoGainEnabled = session.autoAllowed;
+	state.autoDigitalGainEnabled = session.autoAllowed;
 	state.exposureValue = 0;
 	state.minFrameDuration = session.minFrameDuration;
 	state.maxFrameDuration = session.maxFrameDuration;
@@ -504,6 +507,14 @@  int AgcAlgorithm::configure(agc::Session &session, agc::ActiveState &state,
 				return;
 
 			config.ctrlMap[&controls::ExposureValue] = ControlInfo(-8.0f, 8.0f, 0.0f);
+			if (session.maxDigitalGain > 1.0) {
+				config.ctrlMap[&controls::DigitalGain] =
+					ControlInfo(1.0f, static_cast<float>(session.maxDigitalGain), 1.0f);
+				config.ctrlMap[&controls::DigitalGainMode] =
+					ControlInfo({ { ControlValue(controls::DigitalGainModeAuto),
+							ControlValue(controls::DigitalGainModeManual) } },
+							ControlValue(controls::DigitalGainModeAuto));
+			}
 
 			std::vector<ControlValue> options;
 			for (const auto &[id, _] : impl.constraintModes())
@@ -634,6 +645,23 @@  void AgcAlgorithm::queueRequest(const agc::Session &session, agc::ActiveState &s
 			if (!state.autoGainEnabled && !controls.get(controls::AnalogueGain))
 				frameContext.autoGainModeChange = true;
 		}
+
+		const auto &dgMode = controls.get(controls::DigitalGainMode);
+		if (dgMode &&
+		    (*dgMode == controls::DigitalGainModeAuto) != state.autoDigitalGainEnabled) {
+			state.autoDigitalGainEnabled = (*dgMode == controls::AnalogueGainModeAuto);
+
+			LOG(Agc, Debug)
+				<< (state.autoDigitalGainEnabled ? "Enabling" : "Disabling")
+				<< " AGC (digital gain)";
+			/*
+			 * If we go from auto -> manual with no manual control
+			 * set, use the last computed value, which we don't
+			 * know until prepare() so save this information.
+			 */
+			if (!state.autoDigitalGainEnabled && !controls.get(controls::DigitalGain))
+				frameContext.autoDigitalGainModeChange = true;
+		}
 	}
 
 	const auto &frameDurationLimits = controls.get(controls::FrameDurationLimits);
@@ -664,14 +692,31 @@  void AgcAlgorithm::queueRequest(const agc::Session &session, agc::ActiveState &s
 		LOG(Agc, Debug) << "Set gain to " << state.manual.gain;
 	}
 
+	const auto &digitalGain = controls.get(controls::DigitalGain);
+	if (digitalGain && !state.autoDigitalGainEnabled) {
+		if (session.maxDigitalGain > 1.0) {
+			state.manual.digitalGain = *digitalGain;
+			LOG(Agc, Debug) << "Set digital gain to " << state.manual.digitalGain;
+		} else {
+			LOG(Agc, Warning) << "DigitalGain is not supported";
+		}
+	}
+
 	frameContext.autoExposureEnabled = state.autoExposureEnabled;
 	frameContext.autoGainEnabled = state.autoGainEnabled;
+	frameContext.autoDigitalGainEnabled = state.autoDigitalGainEnabled;
 
 	if (!frameContext.autoExposureEnabled)
 		frameContext.exposure = clampExposure(session, frameContext, state.manual.exposure);
+
 	if (!frameContext.autoGainEnabled)
 		frameContext.gain = state.manual.gain;
 
+	if (frameContext.autoDigitalGainEnabled)
+		frameContext.digitalGain = state.automatic.digitalGain;
+	else
+		frameContext.digitalGain = state.manual.digitalGain;
+
 	if (!frameContext.autoExposureEnabled && !frameContext.autoGainEnabled)
 		frameContext.quantizationGain = 1.0;
 
@@ -714,7 +759,13 @@  void AgcAlgorithm::prepare([[maybe_unused]] const agc::Session &session, agc::Ac
 {
 	uint32_t activeAutoExposure = state.automatic.exposure;
 	double activeAutoGain = state.automatic.gain;
-	double activeAutoQGain = state.automatic.quantizationGain;
+	double activeAutoQGain = 1.0;
+	double activeAutoDigitalGain = 1.0;
+
+	if (session.maxDigitalGain > 1.0) {
+		activeAutoQGain = state.automatic.quantizationGain;
+		activeAutoDigitalGain = state.automatic.digitalGain;
+	}
 
 	/* Populate exposure and gain in auto mode */
 	if (frameContext.autoExposureEnabled) {
@@ -725,6 +776,8 @@  void AgcAlgorithm::prepare([[maybe_unused]] const agc::Session &session, agc::Ac
 		frameContext.gain = activeAutoGain;
 		frameContext.quantizationGain = activeAutoQGain;
 	}
+	if (frameContext.autoDigitalGainEnabled)
+		frameContext.digitalGain = activeAutoDigitalGain;
 
 	/*
 	 * Populate manual exposure and gain from the active auto values when
@@ -733,12 +786,17 @@  void AgcAlgorithm::prepare([[maybe_unused]] const agc::Session &session, agc::Ac
 	if (!frameContext.autoExposureEnabled && frameContext.autoExposureModeChange) {
 		state.manual.exposure = activeAutoExposure;
 		frameContext.exposure = activeAutoExposure;
+		frameContext.quantizationGain = activeAutoQGain;
 	}
 	if (!frameContext.autoGainEnabled && frameContext.autoGainModeChange) {
 		state.manual.gain = activeAutoGain;
 		frameContext.gain = activeAutoGain;
 		frameContext.quantizationGain = activeAutoQGain;
 	}
+	if (!frameContext.autoDigitalGainEnabled && frameContext.autoDigitalGainModeChange) {
+		state.manual.gain = activeAutoDigitalGain;
+		frameContext.digitalGain = activeAutoDigitalGain;
+	}
 
 	frameContext.yTarget = state.automatic.yTarget;
 }
@@ -791,6 +849,8 @@  void AgcAlgorithm::process(const agc::Session &session, agc::ActiveState &state,
 					  session.sensor.exposureMargin * lineDuration;
 	double minAnalogueGain;
 	double maxAnalogueGain;
+	double minDigitalGain;
+	double maxDigitalGain;
 
 	if (!frameContext.autoExposureEnabled) {
 		minExposureTime = std::clamp<utils::Duration>(
@@ -807,6 +867,14 @@  void AgcAlgorithm::process(const agc::Session &session, agc::ActiveState &state,
 		maxAnalogueGain = frameContext.gain;
 	}
 
+	if (frameContext.autoDigitalGainEnabled) {
+		minDigitalGain = 1.0;
+		maxDigitalGain = session.maxDigitalGain;
+	} else {
+		minDigitalGain = frameContext.digitalGain;
+		maxDigitalGain = frameContext.digitalGain;
+	}
+
 	std::visit(utils::overloaded{
 		[&](AgcMSV& impl) {
 			impl.setLimits({
@@ -842,9 +910,18 @@  void AgcAlgorithm::process(const agc::Session &session, agc::ActiveState &state,
 			utils::Duration effectiveExposureValue =
 				lineDuration * params->exposure * params->gain;
 
+			/*
+			 * Include the quantization gain if it was applied. Do not use
+			 * compress.gain because it will include gains that shall not be
+			 * reported to the user when HDR is implemented.
+			 */
+			if (session.maxDigitalGain > 1.0)
+				effectiveExposureValue *= frameContext.quantizationGain
+							* frameContext.digitalGain;
+
 			impl.setLimits(minExposureTime, maxExposureTime,
 				       minAnalogueGain, maxAnalogueGain,
-				       1.0, 1.0,
+				       minDigitalGain, maxDigitalGain,
 				       std::move(params->additionalConstraints));
 
 			const auto &newEv = impl.calculateNewEv({
@@ -932,6 +1009,16 @@  void AgcAlgorithm::fillMetadata(const agc::Session &session,
 		metadata.set(controls::AeConstraintMode, frameContext.constraintMode);
 		metadata.set(controls::ExposureValue, frameContext.exposureValue);
 	}
+
+	/*
+	 * Add the quantization gain to the digital gain metadata, so that
+	 * we don't apply any hidden gains.
+	 * \todo This might be unexpected for applications trying to match a
+	 * digital gain control with the metadata, as it will always be off by
+	 * the quantization gain.
+	 */
+	metadata.set(controls::DigitalGain,
+		     frameContext.quantizationGain * frameContext.digitalGain);
 }
 
 } /* namespace ipa */
diff --git a/src/ipa/libipa/agc.h b/src/ipa/libipa/agc.h
index 9b4bcf0bed1c..c4e0cb6be8a4 100644
--- a/src/ipa/libipa/agc.h
+++ b/src/ipa/libipa/agc.h
@@ -41,6 +41,7 @@  struct Session {
 	double minAnalogueGain;
 	double maxAnalogueGain;
 	double defAnalogueGain;
+	double maxDigitalGain;
 	utils::Duration minFrameDuration;
 	utils::Duration maxFrameDuration;
 	utils::Duration lineDuration;
@@ -57,6 +58,7 @@  struct ActiveState {
 	struct {
 		uint32_t exposure;
 		double gain;
+		double digitalGain;
 	} manual;
 	struct {
 		uint32_t exposure;
@@ -68,6 +70,7 @@  struct ActiveState {
 
 	bool autoExposureEnabled;
 	bool autoGainEnabled;
+	bool autoDigitalGainEnabled;
 	double exposureValue;
 	controls::AeConstraintModeEnum constraintMode;
 	controls::AeExposureModeEnum exposureMode;
@@ -79,11 +82,13 @@  struct FrameContext {
 	uint32_t exposure;
 	double gain;
 	double quantizationGain;
+	double digitalGain;
 	double exposureValue;
 	double yTarget;
 	uint32_t vblank;
 	bool autoExposureEnabled;
 	bool autoGainEnabled;
+	bool autoDigitalGainEnabled;
 	controls::AeConstraintModeEnum constraintMode;
 	controls::AeExposureModeEnum exposureMode;
 	utils::Duration minFrameDuration;
@@ -91,6 +96,7 @@  struct FrameContext {
 	utils::Duration frameDuration;
 	bool autoExposureModeChange;
 	bool autoGainModeChange;
+	bool autoDigitalGainModeChange;
 };
 
 [[nodiscard]]
@@ -128,6 +134,7 @@  public:
 		const ControlInfoMap &sensorControls;
 		ControlInfoMap::Map &ctrlMap;
 		bool autoAllowed = true;
+		double maxDigitalGain = 1.0;
 	};
 
 	struct ProcessParams {