[2/2] ipa: rpi: Get decompand curve from camera helper
diff mbox series

Message ID 20261120114233.86270-3-danius@kurokesu.com
State New
Headers show
Series
  • Raspberry Pi: Sensor HDR and decompand curve from camera helper
Related show

Commit Message

Danius Kalvaitis Nov. 20, 2026, 11:42 a.m. UTC
Decompand curve is a sensor property and can depend on sensor mode,
for example with sensor HDR enabled. The tuning file curve is currently
selected by bit depth only and cannot follow a mode change that keeps
bit depth.

Add CamHelper::decompandCurve() to return the curve of a camera mode.
It takes precedence over the tuning file one, which remains as fallback.
rpi.decompand block is still needed to enable the algorithm but its
decompand_curve entry is now optional.

Signed-off-by: Danius Kalvaitis <danius@kurokesu.com>
---
Tested on a Raspberry Pi CM5 with imx585 in 12-bit Clear HDR mode and
an out-of-tree camera helper that returns the curve.

Without a decompand curve AGC reads the scene about 24 times brighter
than in linear mode. With the helper curve it meters within 4% of
linear mode.

A tuning file curve is still used when the helper returns none. Linear
mode is unchanged.

 src/ipa/rpi/cam_helper/cam_helper.cpp    |  5 +++++
 src/ipa/rpi/cam_helper/cam_helper.h      |  5 +++++
 src/ipa/rpi/cam_helper/meson.build       |  1 +
 src/ipa/rpi/common/ipa_base.cpp          |  1 +
 src/ipa/rpi/common/meson.build           |  1 +
 src/ipa/rpi/controller/camera_mode.h     |  4 ++++
 src/ipa/rpi/controller/rpi/decompand.cpp | 27 ++++++++++++++++--------
 src/ipa/rpi/controller/rpi/decompand.h   |  2 +-
 8 files changed, 36 insertions(+), 10 deletions(-)

Patch
diff mbox series

diff --git a/src/ipa/rpi/cam_helper/cam_helper.cpp b/src/ipa/rpi/cam_helper/cam_helper.cpp
index a480de004..12fbd05a0 100644
--- a/src/ipa/rpi/cam_helper/cam_helper.cpp
+++ b/src/ipa/rpi/cam_helper/cam_helper.cpp
@@ -177,6 +177,11 @@  double CamHelper::getModeSensitivity([[maybe_unused]] const CameraMode &mode) co
 	return 1.0;
 }
 
+ipa::Pwl CamHelper::decompandCurve([[maybe_unused]] const CameraMode &mode) const
+{
+	return {};
+}
+
 unsigned int CamHelper::hideFramesStartup() const
 {
 	/*
diff --git a/src/ipa/rpi/cam_helper/cam_helper.h b/src/ipa/rpi/cam_helper/cam_helper.h
index bbaf57a1f..78bf501ef 100644
--- a/src/ipa/rpi/cam_helper/cam_helper.h
+++ b/src/ipa/rpi/cam_helper/cam_helper.h
@@ -13,6 +13,8 @@ 
 
 #include <libcamera/base/utils.h>
 
+#include <libipa/pwl.h>
+
 #include "controller/camera_mode.h"
 #include "controller/controller.h"
 #include "controller/metadata.h"
@@ -40,6 +42,8 @@  namespace RPiController {
  *
  * A function to return the sensitivity of a given camera mode.
  *
+ * A function to return the decompand curve of a given camera mode.
+ *
  * A parser to parse the embedded data buffers provided by some sensors (for
  * example, the imx219 does; the ov5647 doesn't). This allows us to know for
  * sure the exposure and gain of the frame we're looking at. CamHelper
@@ -94,6 +98,7 @@  public:
 	virtual double gain(uint32_t gainCode) const = 0;
 	virtual bool sensorEmbeddedDataPresent() const;
 	virtual double getModeSensitivity(const CameraMode &mode) const;
+	virtual libcamera::ipa::Pwl decompandCurve(const CameraMode &mode) const;
 	virtual unsigned int hideFramesStartup() const;
 	virtual unsigned int hideFramesModeSwitch() const;
 	virtual unsigned int mistrustMetadataStartup() const;
diff --git a/src/ipa/rpi/cam_helper/meson.build b/src/ipa/rpi/cam_helper/meson.build
index 6935cecf8..6c0c87bdb 100644
--- a/src/ipa/rpi/cam_helper/meson.build
+++ b/src/ipa/rpi/cam_helper/meson.build
@@ -24,6 +24,7 @@  rpi_ipa_cam_helper_sources = files([
 
 rpi_ipa_cam_helper_includes = [
     include_directories('..'),
+    libipa_includes,
 ]
 
 rpi_ipa_cam_helper_deps = [
diff --git a/src/ipa/rpi/common/ipa_base.cpp b/src/ipa/rpi/common/ipa_base.cpp
index 319d6bf44..c23c04fb4 100644
--- a/src/ipa/rpi/common/ipa_base.cpp
+++ b/src/ipa/rpi/common/ipa_base.cpp
@@ -216,6 +216,7 @@  int32_t IpaBase::configure(const IPACameraSensorInfo &sensorInfo, const ConfigPa
 
 	mode_.transform = static_cast<libcamera::Transform>(params.transform);
 	mode_.sensorHdr = params.sensorHdr;
+	mode_.decompandCurve = helper_->decompandCurve(mode_);
 
 	/* Pass the camera mode to the CamHelper to setup algorithms. */
 	helper_->setCameraMode(mode_);
diff --git a/src/ipa/rpi/common/meson.build b/src/ipa/rpi/common/meson.build
index 73d2ee732..e4f804d70 100644
--- a/src/ipa/rpi/common/meson.build
+++ b/src/ipa/rpi/common/meson.build
@@ -6,6 +6,7 @@  rpi_ipa_common_sources = files([
 
 rpi_ipa_common_includes = [
     include_directories('..'),
+    libipa_includes,
 ]
 
 rpi_ipa_common_deps = [
diff --git a/src/ipa/rpi/controller/camera_mode.h b/src/ipa/rpi/controller/camera_mode.h
index 5eb05705e..9c908ac16 100644
--- a/src/ipa/rpi/controller/camera_mode.h
+++ b/src/ipa/rpi/controller/camera_mode.h
@@ -10,6 +10,8 @@ 
 
 #include <libcamera/base/utils.h>
 
+#include <libipa/pwl.h>
+
 /*
  * Description of a "camera mode", holding enough information for control
  * algorithms to adapt their behaviour to the different modes of the camera,
@@ -58,4 +60,6 @@  struct CameraMode {
 	double maxAnalogueGain;
 	/* sensor HDR state */
 	bool sensorHdr;
+	/* decompand curve of this mode */
+	libcamera::ipa::Pwl decompandCurve;
 };
diff --git a/src/ipa/rpi/controller/rpi/decompand.cpp b/src/ipa/rpi/controller/rpi/decompand.cpp
index 185e06f91..ccf7ec0d9 100644
--- a/src/ipa/rpi/controller/rpi/decompand.cpp
+++ b/src/ipa/rpi/controller/rpi/decompand.cpp
@@ -27,8 +27,14 @@  char const *Decompand::name() const
 int Decompand::read(const libcamera::ValueNode &params)
 {
 	config_.bitdepth = params["bitdepth"].get<uint32_t>(0);
-	config_.decompandCurve = params["decompand_curve"].get<ipa::Pwl>(ipa::Pwl{});
-	return config_.decompandCurve.empty() ? -EINVAL : 0;
+
+	if (params.contains("decompand_curve")) {
+		config_.decompandCurve = params["decompand_curve"].get<ipa::Pwl>(ipa::Pwl{});
+		if (config_.decompandCurve.empty())
+			return -EINVAL;
+	}
+
+	return 0;
 }
 
 void Decompand::initialise()
@@ -38,23 +44,26 @@  void Decompand::initialise()
 void Decompand::switchMode(CameraMode const &cameraMode,
 			   [[maybe_unused]] Metadata *metadata)
 {
-	mode_ = cameraMode;
+	/* Camera helper curve takes precedence over the tuning file. */
+	if (!cameraMode.decompandCurve.empty())
+		curve_ = cameraMode.decompandCurve;
+	else if (config_.bitdepth == 0 || cameraMode.bitdepth == config_.bitdepth)
+		curve_ = config_.decompandCurve;
+	else
+		curve_ = {};
 }
 
 void Decompand::initialValues(libcamera::ipa::Pwl &decompandCurve)
 {
-	if (config_.bitdepth == 0 || mode_.bitdepth == config_.bitdepth) {
-		decompandCurve = config_.decompandCurve;
-	} else
-		decompandCurve = {};
+	decompandCurve = curve_;
 }
 
 void Decompand::prepare(Metadata *imageMetadata)
 {
 	DecompandStatus decompandStatus;
 
-	if (config_.bitdepth == 0 || mode_.bitdepth == config_.bitdepth) {
-		decompandStatus.decompandCurve = config_.decompandCurve;
+	if (!curve_.empty()) {
+		decompandStatus.decompandCurve = curve_;
 		imageMetadata->set("decompand.status", decompandStatus);
 	}
 }
diff --git a/src/ipa/rpi/controller/rpi/decompand.h b/src/ipa/rpi/controller/rpi/decompand.h
index 1a53de245..8e58f3ad7 100644
--- a/src/ipa/rpi/controller/rpi/decompand.h
+++ b/src/ipa/rpi/controller/rpi/decompand.h
@@ -25,8 +25,8 @@  public:
 	void prepare(Metadata *imageMetadata) override;
 
 private:
-	CameraMode mode_;
 	DecompandConfig config_;
+	libcamera::ipa::Pwl curve_;
 };
 
 } /* namespace RPiController */