From 7cbd05dc01eb031d82e7e2db3008eb0146908abb Mon Sep 17 00:00:00 2001
From: Milan Zamazal <mzamazal@redhat.com>
Date: Fri, 18 Oct 2024 16:12:37 +0200
Subject: libcamera: software_isp: Black level from tuning file

This patch allows obtaining a black level from a tuning file in addition
to the camera sensor helper.  If both of them define a black level, the
one from the tuning file takes precedence.

The use cases are:

- A user wants to use a different black level, for whatever reason.

- There is a sensor without known gains but with a known black level.
  Because a camera sensor helper cannot be defined without specifying
  gains, the only way to specify the black level is using the tuning
  file.  Software ISP uses its fallback gain handling in such a case.

Signed-off-by: Milan Zamazal <mzamazal@redhat.com>
Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
---
 src/ipa/simple/algorithms/blc.cpp | 13 +++++++++++++
 src/ipa/simple/algorithms/blc.h   |  1 +
 2 files changed, 14 insertions(+)

(limited to 'src/ipa/simple/algorithms')

diff --git a/src/ipa/simple/algorithms/blc.cpp b/src/ipa/simple/algorithms/blc.cpp
index a7af2e12..b4e32fe1 100644
--- a/src/ipa/simple/algorithms/blc.cpp
+++ b/src/ipa/simple/algorithms/blc.cpp
@@ -21,6 +21,19 @@ BlackLevel::BlackLevel()
 {
 }
 
+int BlackLevel::init(IPAContext &context, const YamlObject &tuningData)
+{
+	auto blackLevel = tuningData["blackLevel"].get<int16_t>();
+	if (blackLevel.has_value()) {
+		/*
+		 * Convert 16 bit values from the tuning file to 8 bit black
+		 * level for the SoftISP.
+		 */
+		context.configuration.black.level = blackLevel.value() >> 8;
+	}
+	return 0;
+}
+
 int BlackLevel::configure(IPAContext &context,
 			  [[maybe_unused]] const IPAConfigInfo &configInfo)
 {
diff --git a/src/ipa/simple/algorithms/blc.h b/src/ipa/simple/algorithms/blc.h
index 828ad8b1..2cf2a877 100644
--- a/src/ipa/simple/algorithms/blc.h
+++ b/src/ipa/simple/algorithms/blc.h
@@ -19,6 +19,7 @@ public:
 	BlackLevel();
 	~BlackLevel() = default;
 
+	int init(IPAContext &context, const YamlObject &tuningData) override;
 	int configure(IPAContext &context, const IPAConfigInfo &configInfo) override;
 	void process(IPAContext &context, const uint32_t frame,
 		     IPAFrameContext &frameContext,
-- 
cgit v1.2.1