[03/10] libipa: agc_mean_luminance: Add support for digital gain
diff mbox series

Message ID 20261117-sklug-digital-gain-support-v1-v1-3-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
Extend AgcMeanLuminance so that an exposure mode can carry a optional
digital gain array.

The corresponding tuning file entry now has the following format:
- Agc:
      AeExposureMode:
        Normal:
          exposureTime: [ 100, 10000, 30000, 60000, 120000 ]
          gain: [ 2.0, 4.0, 6.0, 8.0, 10.0 ]
          digitalGain: [ 1.0, 2.0, 3.0, 4.0, 6.0 ]

When the digitalGain key is not present, a digital gain of 1.0 is
assumed.

Limit the digitalGain to 1.0 in AgcAlgorithm for now. It will be enabled
in an upcoming patch.

Signed-off-by: Stefan Klug <stefan.klug@ideasonboard.com>
---
 src/ipa/libipa/agc.cpp                |  1 +
 src/ipa/libipa/agc_mean_luminance.cpp | 31 ++++++++++++++++++++++++++-----
 src/ipa/libipa/agc_mean_luminance.h   |  3 ++-
 3 files changed, 29 insertions(+), 6 deletions(-)

Patch
diff mbox series

diff --git a/src/ipa/libipa/agc.cpp b/src/ipa/libipa/agc.cpp
index 4b2e65f37718..6ec37756c88e 100644
--- a/src/ipa/libipa/agc.cpp
+++ b/src/ipa/libipa/agc.cpp
@@ -842,6 +842,7 @@  void AgcAlgorithm::process(const agc::Session &session, agc::ActiveState &state,
 
 			impl.setLimits(minExposureTime, maxExposureTime,
 				       minAnalogueGain, maxAnalogueGain,
+				       1.0, 1.0,
 				       std::move(params->additionalConstraints));
 
 			const auto &newEv = impl.calculateNewEv({
diff --git a/src/ipa/libipa/agc_mean_luminance.cpp b/src/ipa/libipa/agc_mean_luminance.cpp
index 33d938cc6a87..d453602f0dbb 100644
--- a/src/ipa/libipa/agc_mean_luminance.cpp
+++ b/src/ipa/libipa/agc_mean_luminance.cpp
@@ -352,6 +352,8 @@  int parseExposureModes(AgcMeanLuminance::Configuration &config, const ValueNode
 				modeValues["exposureTime"].get<std::vector<uint32_t>>().value_or(utils::defopt);
 			std::vector<double> gains =
 				modeValues["gain"].get<std::vector<double>>().value_or(utils::defopt);
+			std::vector<double> digitalGains =
+				modeValues["digitalGain"].get<std::vector<double>>().value_or(utils::defopt);
 
 			if (exposureTimes.size() != gains.size()) {
 				LOG(AgcMeanLuminance, Error)
@@ -365,12 +367,22 @@  int parseExposureModes(AgcMeanLuminance::Configuration &config, const ValueNode
 				return -EINVAL;
 			}
 
+			/* Digital gain is optional and defaults to 1.0 */
+			if (digitalGains.empty())
+				digitalGains = std::vector<double>(exposureTimes.size(), 1.0);
+
+			if (exposureTimes.size() != digitalGains.size()) {
+				LOG(AgcMeanLuminance, Error)
+					<< "Exposure time and digital gain array sizes unequal";
+				return -EINVAL;
+			}
+
 			std::vector<ExposureModeHelper::Stage> stages;
 			for (size_t i = 0; i < exposureTimes.size(); i++) {
 				stages.push_back({
 					.exposureTime = std::chrono::microseconds(exposureTimes[i]),
 					.gain = gains[i],
-					.digitalGain = 1.0,
+					.digitalGain = digitalGains[i],
 				});
 			}
 
@@ -422,7 +434,8 @@  int parseExposureModes(AgcMeanLuminance::Configuration &config, const ValueNode
  * AeExposureMode containing per-mode setting dictionaries with the key being a
  * value from \ref controls::AeExposureModeNameValueMap. Each mode dict should
  * contain an array of exposure times with the key "exposureTime" and an array
- * of gain values with the key "gain", in this format:
+ * of gain values with the key "gain" and an optional array of digital gain
+ * values with the key "digitalGain" in this format:
  *
  * \code{.unparsed}
  * algorithms:
@@ -434,9 +447,12 @@  int parseExposureModes(AgcMeanLuminance::Configuration &config, const ValueNode
  *         Short:
  *           exposureTime: [ 100, 10000, 30000, 60000, 120000 ]
  *           gain: [ 2.0, 4.0, 6.0, 8.0, 10.0 ]
+ *           digitalGain: [ 1.0, 2.0, 3.0, 4.0, 6.0 ]
  *
  * \endcode
  *
+ * If there is no entry for digital gain, the digital gain will be fixed to 1.0.
+ *
  * \return 0 on success or a negative error code
  */
 std::optional<AgcMeanLuminance::Configuration>
@@ -494,6 +510,8 @@  AgcMeanLuminance::AgcMeanLuminance(Configuration &&config)
 	 * ExposureModeHelper using an empty vector of stages. This will result
 	 * in the ExposureModeHelper simply driving the exposure time as high as
 	 * possible before touching gain.
+	 * In contrast to analog gain, digital gain is mostly unlimited. As that
+	 * results in very bad images, limit it to 16x by default.
 	 */
 	if (exposureModeHelpers_.empty())
 		exposureModeHelpers_.try_emplace(
@@ -502,7 +520,7 @@  AgcMeanLuminance::AgcMeanLuminance(Configuration &&config)
 				{
 					.exposureTime = utils::Duration::max(),
 					.gain = std::numeric_limits<double>::max(),
-					.digitalGain = 1.0,
+					.digitalGain = 16.0,
 				},
 			});
 }
@@ -531,9 +549,11 @@  void AgcMeanLuminance::configure(utils::Duration lineDuration,
 /**
  * \brief Set the ExposureModeHelper limits for this class
  * \param[in] minExposureTime Minimum exposure time to allow
- * \param[in] maxExposureTime Maximum ewposure time to allow
+ * \param[in] maxExposureTime Maximum exposure time to allow
  * \param[in] minGain Minimum gain to allow
  * \param[in] maxGain Maximum gain to allow
+ * \param[in] minDigitalGain Minimum digital gain to allow
+ * \param[in] maxDigitalGain Maximum digital gain to allow
  * \param[in] constraints Additional constraints to apply
  *
  * This function calls \ref ExposureModeHelper::setLimits() for each
@@ -542,11 +562,12 @@  void AgcMeanLuminance::configure(utils::Duration lineDuration,
 void AgcMeanLuminance::setLimits(utils::Duration minExposureTime,
 				 utils::Duration maxExposureTime,
 				 double minGain, double maxGain,
+				 double minDigitalGain, double maxDigitalGain,
 				 std::vector<AgcMeanLuminance::AgcConstraint> constraints)
 {
 	for (auto &[id, helper] : exposureModeHelpers_)
 		helper.setLimits(minExposureTime, maxExposureTime, minGain,
-				 maxGain, 1.0, 1.0);
+				 maxGain, minDigitalGain, maxDigitalGain);
 
 	additionalConstraints_ = std::move(constraints);
 }
diff --git a/src/ipa/libipa/agc_mean_luminance.h b/src/ipa/libipa/agc_mean_luminance.h
index ffd1f1e25a8d..52aaf44c2324 100644
--- a/src/ipa/libipa/agc_mean_luminance.h
+++ b/src/ipa/libipa/agc_mean_luminance.h
@@ -56,7 +56,8 @@  public:
 
 	void configure(utils::Duration lineDuration, const CameraSensorHelper *sensorHelper);
 	void setLimits(utils::Duration minExposureTime, utils::Duration maxExposureTime,
-		       double minGain, double maxGain, std::vector<AgcConstraint> constraints);
+		       double minGain, double maxGain, double minDigitalGain,
+		       double maxDigitalGain, std::vector<AgcConstraint> constraints);
 
 	const std::map<int32_t, std::vector<AgcConstraint>> &constraintModes() const
 	{