[v4,3/4] ipa: softisp: Support any LSC table sizes
diff mbox series

Message ID 20261202151305.132644-4-mzamazal@redhat.com
State New
Headers show
Series
  • softisp tuning script for LSC
Related show

Commit Message

Milan Zamazal Dec. 2, 2026, 3:13 p.m. UTC
This adds support of any lens shading correction table size to the
software ISP and its IPA.  The tuning file can specify the table grid
size using a newly introduced `gridSize' parameter, for example:

  - Lsc:
      gridSize:
        width: 32
        height: 32
      sets:
        ...

If unspecified, the old size of 17x17 is used for backward
compatibility.

While debayering uses a gridSize_ std::optional variable for the
purpose, it cannot be used here because Mojo apparently doesn't support
optional's.  So separate "size" and "enabled" variables are used when
talking to the IPA.

Signed-off-by: Milan Zamazal <mzamazal@redhat.com>
---
 .../internal/software_isp/debayer_params.h    | 21 +++-----
 .../internal/software_isp/software_isp.h      |  1 -
 include/libcamera/ipa/softisp.mojom           |  3 +-
 src/ipa/softisp/algorithms/lsc.cpp            | 53 ++++++++++++++-----
 src/ipa/softisp/algorithms/lsc.h              |  6 ++-
 src/ipa/softisp/ipa_context.h                 |  1 +
 src/ipa/softisp/softisp.cpp                   | 10 ++--
 src/libcamera/software_isp/debayer.cpp        | 13 -----
 src/libcamera/software_isp/software_isp.cpp   |  9 ++--
 9 files changed, 67 insertions(+), 50 deletions(-)

Patch
diff mbox series

diff --git a/include/libcamera/internal/software_isp/debayer_params.h b/include/libcamera/internal/software_isp/debayer_params.h
index 300c3f2d6..0e3c3995c 100644
--- a/include/libcamera/internal/software_isp/debayer_params.h
+++ b/include/libcamera/internal/software_isp/debayer_params.h
@@ -10,8 +10,10 @@ 
 
 #pragma once
 
-#include <array>
 #include <stdint.h>
+#include <vector>
+
+#include <libcamera/geometry.h>
 
 #include "libcamera/internal/matrix.h"
 #include "libcamera/internal/vector.h"
@@ -32,23 +34,12 @@  struct DebayerParams {
 	 * must be a multiple of 4 (the initial value of GL_UNPACK_ALIGNMENT)
 	 * and lscLut is also aligned to 4 bytes.
 	 **/
-	static constexpr unsigned int kLscGridSize = 17;
 	static constexpr unsigned int kLscValuesPerCell = 4;
 	static constexpr unsigned int kLscBytesPerCell =
 		kLscValuesPerCell * sizeof(uint8_t);
-	using LscLookupTable =
-		std::array<uint8_t, kLscGridSize * kLscGridSize * kLscValuesPerCell>;
-	static constexpr auto identityLscLut = [] {
-		LscLookupTable lut = {};
-		/* lut.fill(64) could be used, but it fails with older gcc versions */
-		for (size_t i = 0; kLscValuesPerCell * i < lut.size(); i++) {
-			lut[i * kLscValuesPerCell + 0] = 64; /* == UQ<2, 6>(1.0f).quantized() */
-			lut[i * kLscValuesPerCell + 1] = 64;
-			lut[i * kLscValuesPerCell + 2] = 64;
-		}
-		return lut;
-	}();
-	alignas(4) LscLookupTable lscLut = identityLscLut;
+	using LscLookupTable = std::vector<uint8_t>;
+	/* Minimum allowed alignment for the vector is 8. */
+	alignas(8) LscLookupTable lscLut{};
 	uint64_t lscLutVersion = 0;
 };
 
diff --git a/include/libcamera/internal/software_isp/software_isp.h b/include/libcamera/internal/software_isp/software_isp.h
index c8ed91949..9958f6966 100644
--- a/include/libcamera/internal/software_isp/software_isp.h
+++ b/include/libcamera/internal/software_isp/software_isp.h
@@ -101,7 +101,6 @@  private:
 	DebayerParams debayerParams_;
 	DmaBufAllocator dmaHeap_;
 	bool ccmEnabled_;
-	bool lscEnabled_;
 	std::optional<Size> lscGridSize_;
 
 	std::unique_ptr<ipa::softisp::IPAProxySoftIsp> ipa_;
diff --git a/include/libcamera/ipa/softisp.mojom b/include/libcamera/ipa/softisp.mojom
index e42742ed7..e10819e93 100644
--- a/include/libcamera/ipa/softisp.mojom
+++ b/include/libcamera/ipa/softisp.mojom
@@ -20,7 +20,8 @@  interface IPASoftIspInterface {
 	     libcamera.ControlInfoMap sensorControls)
 		=> (int32 ret,
 		    libcamera.ControlInfoMap ipaControls,
-		    bool ccmEnabled, bool lscEnabled);
+		    bool ccmEnabled, bool lscEnabled,
+                    libcamera.Size lscGridSize);
 	start() => (int32 ret);
 	stop();
 	configure(IPAConfigInfo configInfo)
diff --git a/src/ipa/softisp/algorithms/lsc.cpp b/src/ipa/softisp/algorithms/lsc.cpp
index 308939f19..d49ab58ab 100644
--- a/src/ipa/softisp/algorithms/lsc.cpp
+++ b/src/ipa/softisp/algorithms/lsc.cpp
@@ -13,22 +13,45 @@  namespace ipa::softisp::algorithms {
 
 LOG_DEFINE_CATEGORY(IPASoftIspLsc)
 
-int Lsc::init(IPAContext &context, const ValueNode &tuningData)
+DebayerParams::LscLookupTable identityLscLut(const Size &size)
 {
-	static constexpr unsigned int kGridSize = DebayerParams::kLscGridSize;
+	size_t lutSize = DebayerParams::kLscValuesPerCell * size.width * size.height;
+	/* 64 == UQ<2, 6>(1.0f).quantized() */
+	return DebayerParams::LscLookupTable(lutSize, 64);
+}
 
-	for (unsigned int i = 0; i < kGridSize; i++)
-		gridPos_.push_back(static_cast<double>(i) / (kGridSize - 1));
+int Lsc::init(IPAContext &context, const ValueNode &tuningData)
+{
+	auto &tuningGridSize = tuningData["gridSize"];
+	if (tuningGridSize.isEmpty()) {
+		/* Just for backward compatibility, should be erroneous otherwise. */
+		gridSize_ = defaultGridSize_;
+	} else {
+		if (!tuningGridSize.isDictionary()) {
+			LOG(IPASoftIspLsc, Error)
+				<< "Invalid LSC grid size specification in the tuning file";
+			return -1;
+		}
+		auto gridWidth = tuningGridSize["width"].get<unsigned int>();
+		auto gridHeight = tuningGridSize["height"].get<unsigned int>();
+		if (!gridWidth || !gridHeight || gridWidth <= 1 || gridHeight <= 1) {
+			LOG(IPASoftIspLsc, Error)
+				<< "Invalid LSC grid values in the tuning file";
+			return -1;
+		}
+		gridSize_ = Size(gridWidth.value(), gridHeight.value());
+	}
 
 	int ret = lscAlgo_.init(tuningData, context.ctrlMap,
-			     { .keys = { "r", "g", "b" },
-			       .numHSamples = kGridSize,
-			       .numVSamples = kGridSize,
-			       .sensorSize = context.sensorInfo.activeArea.size() });
+				{ .keys = { "r", "g", "b" },
+				  .numHSamples = gridSize_.width,
+				  .numVSamples = gridSize_.height,
+				  .sensorSize = context.sensorInfo.activeArea.size() });
 	if (ret)
 		return ret;
 
 	context.lscEnabled = true;
+	context.lscGridSize = gridSize_;
 
 	return 0;
 }
@@ -36,9 +59,15 @@  int Lsc::init(IPAContext &context, const ValueNode &tuningData)
 int Lsc::configure(IPAContext &context,
 		   [[maybe_unused]] const IPAConfigInfo &configInfo)
 {
+	std::vector<double> gridPosX, gridPosY;
+	for (unsigned int i = 0; i < gridSize_.width; i++)
+		gridPosX.push_back(static_cast<double>(i) / (gridSize_.width - 1));
+	for (unsigned int i = 0; i < gridSize_.height; i++)
+		gridPosY.push_back(static_cast<double>(i) / (gridSize_.height - 1));
+
 	return lscAlgo_.configure(context.activeState.lsc,
 				  context.sensorInfo.analogCrop,
-				  gridPos_, gridPos_);
+				  gridPosX, gridPosY);
 }
 
 void Lsc::prepare([[maybe_unused]] IPAContext &context,
@@ -51,7 +80,7 @@  void Lsc::prepare([[maybe_unused]] IPAContext &context,
 
 	if (!frameContext.lsc.enabled) {
 		if (lastAppliedCt_ != 0) {
-			params->lscLut = DebayerParams::identityLscLut;
+			params->lscLut = identityLscLut(gridSize_);
 			params->lscLutVersion++;
 			lastAppliedCt_ = 0;
 
@@ -71,8 +100,8 @@  void Lsc::prepare([[maybe_unused]] IPAContext &context,
 	const auto &blue = set.at("b");
 
 	DebayerParams::LscLookupTable &lut = params->lscLut;
-	constexpr unsigned int gridSize = DebayerParams::kLscGridSize;
-	for (unsigned int i = 0, j = 0; i < gridSize * gridSize; i++) {
+	lut.resize(DebayerParams::kLscValuesPerCell * gridSize_.width * gridSize_.height);
+	for (unsigned int i = 0, j = 0; i < gridSize_.width * gridSize_.height; i++) {
 		lut[j++] = red[i];
 		lut[j++] = green[i];
 		lut[j++] = blue[i];
diff --git a/src/ipa/softisp/algorithms/lsc.h b/src/ipa/softisp/algorithms/lsc.h
index 83ec5aec2..eebbd0e68 100644
--- a/src/ipa/softisp/algorithms/lsc.h
+++ b/src/ipa/softisp/algorithms/lsc.h
@@ -17,6 +17,8 @@  namespace libcamera {
 
 namespace ipa::softisp::algorithms {
 
+DebayerParams::LscLookupTable identityLscLut(const Size &size);
+
 class Lsc : public Algorithm
 {
 public:
@@ -41,8 +43,8 @@  public:
 private:
 	LscAlgorithm<UQ<2, 6>> lscAlgo_;
 
-	std::vector<double> gridPos_;
-
+	static constexpr Size defaultGridSize_ = Size(17, 17);
+	Size gridSize_ = defaultGridSize_;
 	unsigned int lastAppliedCt_ = 0;
 };
 
diff --git a/src/ipa/softisp/ipa_context.h b/src/ipa/softisp/ipa_context.h
index 62d1ae84b..9a4ae726b 100644
--- a/src/ipa/softisp/ipa_context.h
+++ b/src/ipa/softisp/ipa_context.h
@@ -91,6 +91,7 @@  struct IPAContext {
 	ControlInfoMap::Map ctrlMap;
 	bool ccmEnabled = false;
 	bool lscEnabled = false;
+	Size lscGridSize;
 };
 
 } /* namespace ipa::softisp */
diff --git a/src/ipa/softisp/softisp.cpp b/src/ipa/softisp/softisp.cpp
index e35f07737..9926715f8 100644
--- a/src/ipa/softisp/softisp.cpp
+++ b/src/ipa/softisp/softisp.cpp
@@ -27,6 +27,7 @@ 
 #include "libcamera/internal/yaml_parser.h"
 
 #include "algorithms/adjust.h"
+#include "algorithms/lsc.h"
 #include "libipa/agc.h"
 #include "libipa/camera_sensor_helper.h"
 
@@ -59,7 +60,8 @@  public:
 		 const ControlInfoMap &sensorControls,
 		 ControlInfoMap *ipaControls,
 		 bool *ccmEnabled,
-		 bool *lscEnabled) override;
+		 bool *lscEnabled,
+		 Size *lscGridSize) override;
 	int configure(const IPAConfigInfo &configInfo,
 		      ControlInfoMap *ipaControls) override;
 
@@ -99,7 +101,8 @@  int IPASoftIsp::init(const IPASettings &settings,
 		     const ControlInfoMap &sensorControls,
 		     ControlInfoMap *ipaControls,
 		     bool *ccmEnabled,
-		     bool *lscEnabled)
+		     bool *lscEnabled,
+		     Size *lscGridSize)
 {
 	context_.camHelper = CameraSensorHelperFactoryBase::create(settings.sensorModel);
 	if (!context_.camHelper) {
@@ -140,6 +143,7 @@  int IPASoftIsp::init(const IPASettings &settings,
 
 	*ccmEnabled = context_.ccmEnabled;
 	*lscEnabled = context_.lscEnabled;
+	*lscGridSize = context_.lscGridSize;
 
 	params_ = nullptr;
 	stats_ = nullptr;
@@ -168,7 +172,7 @@  int IPASoftIsp::init(const IPASettings &settings,
 		params_->contrastExp = 1.0;
 		params_->gains = { { 1.0, 1.0, 1.0 } };
 		params_->lscLutVersion = 0;
-		params_->lscLut = DebayerParams::identityLscLut;
+		params_->lscLut = DebayerParams::LscLookupTable();
 		/* combinedMatrix is reset for each frame. */
 	}
 
diff --git a/src/libcamera/software_isp/debayer.cpp b/src/libcamera/software_isp/debayer.cpp
index 65820eabd..32bd6e432 100644
--- a/src/libcamera/software_isp/debayer.cpp
+++ b/src/libcamera/software_isp/debayer.cpp
@@ -43,11 +43,6 @@  namespace libcamera {
  * \brief Contrast value to be used as an exponent
  */
 
-/**
- * \var DebayerParams::kLscGridSize
- * \brief Number of lens shading grid areas in one direction
- */
-
 /**
  * \var DebayerParams::kLscValuesPerCell
  * \brief Number of pixel values per each of the lens shading grid areas
@@ -71,14 +66,6 @@  namespace libcamera {
  * \brief Lens shading lookup table
  */
 
-/**
- * \var DebayerParams::identityLscLut
- * \brief Lens shading lookup table of identity mapping
- *
- * This table can be used to initialise the lens shading lookup table or to
- * not apply any real lens shading correction.
- */
-
 /**
  * \var DebayerParams::lscLutVersion
  * \brief Incremented on each \a lscLut change
diff --git a/src/libcamera/software_isp/software_isp.cpp b/src/libcamera/software_isp/software_isp.cpp
index 88349598a..4b86af7b5 100644
--- a/src/libcamera/software_isp/software_isp.cpp
+++ b/src/libcamera/software_isp/software_isp.cpp
@@ -157,6 +157,8 @@  SoftwareIsp::SoftwareIsp(PipelineHandler *pipe, const CameraSensor *sensor,
 		return;
 	}
 
+	bool lscEnabled;
+	Size gridSize;
 	ret = ipa_->init(IPASettings{ ipaTuningFile, sensor->model() },
 			 debayer_->getStatsFD(),
 			 sharedParams_.fd(),
@@ -164,15 +166,16 @@  SoftwareIsp::SoftwareIsp(PipelineHandler *pipe, const CameraSensor *sensor,
 			 sensor->controls(),
 			 ipaControls,
 			 &ccmEnabled_,
-			 &lscEnabled_);
+			 &lscEnabled,
+			 &gridSize);
 	if (ret) {
 		LOG(SoftwareIsp, Error) << "IPA init failed";
 		debayer_.reset();
 		return;
 	}
 
-	if (lscEnabled_)
-		lscGridSize_ = Size(DebayerParams::kLscGridSize, DebayerParams::kLscGridSize);
+	if (lscEnabled)
+		lscGridSize_ = gridSize;
 	else
 		lscGridSize_.reset();