[2/6] ipa: ipu3: lsc: Subtract the base gain from the shading LUT values
diff mbox series

Message ID 20261118164621.15513-3-guyliandejong@proton.me
State New
Headers show
Series
  • ipu3: Lens shading and black level fixes, OV9734 tuning
Related show

Commit Message

GuyliandeJong Nov. 18, 2026, 4:46 p.m. UTC
The ImgU adds a base gain of 1.0 to the shading LUT values, as the
comment in prepare() already assumes. The gains produced by
LscAlgorithm are total gains in UQ<2, 10> format, where 1024 is a gain
of 1.0, and are written to the LUT as is. Every gain is thus applied
with an extra 1.0: a table of unity gains doubles the image brightness
instead of leaving it unchanged, and the relative correction towards
the corners is weakened.

Measured on an OV9734 with a fixed exposure and a polynomial table of
unity gains, enabling shading correction made the image about 1.7
times brighter. With this change it leaves the image unchanged, and a
radial table leaves the centre of the image unchanged while brightening
the corners.

Subtract the base gain of 1.0 when filling the LUT, clamping gains
lower than 1.0 as the hardware can't apply them.

Fixes: 279d355ef8f7 ("ipa: ipu3: Add Lens Shading Correction algorithm")
Signed-off-by: GuyliandeJong <guyliandejong@proton.me>
---
 src/ipa/ipu3/algorithms/lsc.cpp | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)

Patch
diff mbox series

diff --git a/src/ipa/ipu3/algorithms/lsc.cpp b/src/ipa/ipu3/algorithms/lsc.cpp
index 729a23ff..1991d6ee 100644
--- a/src/ipa/ipu3/algorithms/lsc.cpp
+++ b/src/ipa/ipu3/algorithms/lsc.cpp
@@ -32,6 +32,9 @@  static constexpr unsigned int kMaxNumHCells = 73;
 static constexpr unsigned int kMaxNumVCells = 56;
 static constexpr int kColourTemperatureQuantization = 10;
 
+/* Gain of 1.0 in the UQ<2, 10> format of the LscAlgorithm gains */
+static constexpr uint16_t kUnityGain = 1 << 10;
+
 /**
  * \copydoc libcamera::ipa::Algorithm::init
  */
@@ -238,6 +241,11 @@  void Lsc::prepare([[maybe_unused]] IPAContext &context, [[maybe_unused]] const u
 	 * And that a base gain of 1.0 is added to those configured values. As a
 	 * gain of more than 5.0 is fairly unlikely, let's fix gain_factor to 0
 	 * for now and revisit if needed.
+	 *
+	 * Measurements on an OV9734 match this: a LUT value of 0 leaves the
+	 * image unchanged, and 1024 doubles it. As LscAlgorithm produces the
+	 * total gain, the base gain of 1.0 is subtracted when filling the LUT
+	 * below. Gains lower than 1.0 can't be represented and are clamped.
 	 */
 	config->general.gain_factor = 0;
 
@@ -260,12 +268,16 @@  void Lsc::prepare([[maybe_unused]] IPAContext &context, [[maybe_unused]] const u
 			       config->grid.grid_height_per_slice;
 	unsigned int i = 0;
 
+	const auto lutValue = [](uint16_t gain) -> uint16_t {
+		return gain > kUnityGain ? gain - kUnityGain : 0;
+	};
+
 	for (unsigned int s = 0; s < numSets; s++) {
 		for (unsigned int c = 0; c < cellsPerSet && i < totalCells; c++, i++) {
-			lut->sets[s].r_and_gr[c].r = set.at("r")[i];
-			lut->sets[s].r_and_gr[c].gr = set.at("gr")[i];
-			lut->sets[s].gb_and_b[c].gb = set.at("gb")[i];
-			lut->sets[s].gb_and_b[c].b = set.at("b")[i];
+			lut->sets[s].r_and_gr[c].r = lutValue(set.at("r")[i]);
+			lut->sets[s].r_and_gr[c].gr = lutValue(set.at("gr")[i]);
+			lut->sets[s].gb_and_b[c].gb = lutValue(set.at("gb")[i]);
+			lut->sets[s].gb_and_b[c].b = lutValue(set.at("b")[i]);
 		}
 	}