[6/6] ipa: ipu3: blc: Use the black level from the camera sensor helper
diff mbox series

Message ID 20261118164621.15513-7-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:47 p.m. UTC
The BlackLevelCorrection algorithm programs a fixed obgrid value of 64
for all sensors. Use the black level from the camera sensor helper
instead, and keep 64 when the helper doesn't provide one.

The obgrid units are not documented. Measured on an OV9734 by sweeping
the obgrid value on frames with a fixed exposure and gain, at three
input levels read from the raw CIO2 stream (16.3, 31.9 and 48.4 on 10
bits), the value that brings the output to black was about 28, 60 and
96. The obgrid scale is thus twice the 10-bit scale, which corresponds
to the 16-bit helper value shifted right by 5.

For the OV9734, whose black level is 0x10 on 10 bits, this gives 32
instead of 64. With 64, an input 16 levels above the black level comes
out at 1.3 instead of 6.9 (on 8 bits, with a linear tone curve), so
the shadows are crushed.

This also changes the value for the other IPU3 sensors whose helper
provides a black level, from 64 to 128 for a black level of 0x40 on
10 bits, which was too low by the same measure.

Signed-off-by: GuyliandeJong <guyliandejong@proton.me>
---
 src/ipa/ipu3/algorithms/blc.cpp | 53 +++++++++++++++++++++++++++------
 src/ipa/ipu3/algorithms/blc.h   |  4 +++
 2 files changed, 48 insertions(+), 9 deletions(-)

Patch
diff mbox series

diff --git a/src/ipa/ipu3/algorithms/blc.cpp b/src/ipa/ipu3/algorithms/blc.cpp
index 35748fb2..491ed199 100644
--- a/src/ipa/ipu3/algorithms/blc.cpp
+++ b/src/ipa/ipu3/algorithms/blc.cpp
@@ -7,6 +7,10 @@ 
 
 #include "blc.h"
 
+#include <libcamera/base/log.h>
+
+#include "libipa/camera_sensor_helper.h"
+
 /**
  * \file blc.h
  * \brief IPU3 Black Level Correction control
@@ -30,8 +34,44 @@  namespace ipa::ipu3::algorithms {
  * isn't currently supported.
  */
 
+LOG_DEFINE_CATEGORY(IPU3Blc)
+
+/*
+ * Black level applied when the camera sensor helper doesn't provide one, in
+ * the units of the ImgU obgrid parameters.
+ */
+static constexpr uint16_t kDefaultBlackLevel = 64;
+
 BlackLevelCorrection::BlackLevelCorrection()
+	: blackLevel_(kDefaultBlackLevel)
+{
+}
+
+/**
+ * \copydoc libcamera::ipa::Algorithm::init
+ */
+int BlackLevelCorrection::init(IPAContext &context,
+			       [[maybe_unused]] const ValueNode &tuningData)
 {
+	std::optional<int16_t> blackLevel = context.camHelper->blackLevel();
+	if (!blackLevel) {
+		LOG(IPU3Blc, Warning)
+			<< "No black level provided by camera sensor helper, using "
+			<< kDefaultBlackLevel;
+		return 0;
+	}
+
+	/*
+	 * The camera sensor helper expresses the black level on 16 bits. The
+	 * obgrid parameters are on 11 bits for the 10-bit raw formats the CIO2
+	 * produces: measured with an OV9734, the obgrid value that brings a
+	 * raw input level to black is twice that level on 10 bits.
+	 */
+	blackLevel_ = static_cast<uint16_t>(*blackLevel) >> 5;
+
+	LOG(IPU3Blc, Debug) << "Black level: " << blackLevel_;
+
+	return 0;
 }
 
 /**
@@ -49,15 +89,10 @@  void BlackLevelCorrection::prepare([[maybe_unused]] IPAContext &context,
 				   [[maybe_unused]] IPAFrameContext &frameContext,
 				   ipu3_uapi_params *params)
 {
-	/*
-	 * The Optical Black Level correction values
-	 * \todo The correction values should come from sensor specific
-	 * tuning processes. This is a first rough approximation.
-	 */
-	params->obgrid_param.gr = 64;
-	params->obgrid_param.r = 64;
-	params->obgrid_param.b = 64;
-	params->obgrid_param.gb = 64;
+	params->obgrid_param.gr = blackLevel_;
+	params->obgrid_param.r = blackLevel_;
+	params->obgrid_param.b = blackLevel_;
+	params->obgrid_param.gb = blackLevel_;
 
 	/* Enable the custom black level correction processing */
 	params->use.obgrid = 1;
diff --git a/src/ipa/ipu3/algorithms/blc.h b/src/ipa/ipu3/algorithms/blc.h
index 62748045..9c63ee85 100644
--- a/src/ipa/ipu3/algorithms/blc.h
+++ b/src/ipa/ipu3/algorithms/blc.h
@@ -18,9 +18,13 @@  class BlackLevelCorrection : public Algorithm
 public:
 	BlackLevelCorrection();
 
+	int init(IPAContext &context, const ValueNode &tuningData) override;
 	void prepare(IPAContext &context, const uint32_t frame,
 		     IPAFrameContext &frameContext,
 		     ipu3_uapi_params *params) override;
+
+private:
+	uint16_t blackLevel_;
 };
 
 } /* namespace ipa::ipu3::algorithms */