[v3,25/29] pipeline: rkisp1: Load dewarp parameters from tuning file
diff mbox series

Message ID 20251125162851.2301793-26-stefan.klug@ideasonboard.com
State Accepted
Headers show
Series
  • Full dewarper support on imx8mp
Related show

Commit Message

Stefan Klug Nov. 25, 2025, 4:28 p.m. UTC
Load the dewarp parameters from the tuning file.

Signed-off-by: Stefan Klug <stefan.klug@ideasonboard.com>

---

Changes in v3:
- Moved code into converter module
- Added more documentation

Changes in v2:
- Dropped unused variable
---
 .../internal/converter/converter_dw100.h      |  8 +++
 src/libcamera/converter/converter_dw100.cpp   | 71 +++++++++++++++++++
 src/libcamera/pipeline/rkisp1/rkisp1.cpp      | 16 +++--
 3 files changed, 91 insertions(+), 4 deletions(-)

Comments

Kieran Bingham Nov. 25, 2025, 5:50 p.m. UTC | #1
Quoting Stefan Klug (2025-11-25 16:28:37)
> Load the dewarp parameters from the tuning file.
> 
> Signed-off-by: Stefan Klug <stefan.klug@ideasonboard.com>
> 

Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>

> ---
> 
> Changes in v3:
> - Moved code into converter module
> - Added more documentation
> 
> Changes in v2:
> - Dropped unused variable
> ---
>  .../internal/converter/converter_dw100.h      |  8 +++
>  src/libcamera/converter/converter_dw100.cpp   | 71 +++++++++++++++++++
>  src/libcamera/pipeline/rkisp1/rkisp1.cpp      | 16 +++--
>  3 files changed, 91 insertions(+), 4 deletions(-)
> 
> diff --git a/include/libcamera/internal/converter/converter_dw100.h b/include/libcamera/internal/converter/converter_dw100.h
> index 8dd21a6228f1..f0c858689e27 100644
> --- a/include/libcamera/internal/converter/converter_dw100.h
> +++ b/include/libcamera/internal/converter/converter_dw100.h
> @@ -31,6 +31,8 @@ public:
>  
>         static std::unique_ptr<ConverterDW100Module> createModule(DeviceEnumerator *enumerator);
>  
> +       int init(const YamlObject &params);
> +
>         int configure(const StreamConfiguration &inputCfg,
>                       const std::vector<std::reference_wrapper<StreamConfiguration>>
>                               &outputCfg);
> @@ -67,12 +69,18 @@ private:
>         int applyControls(const Stream *stream, const V4L2Request *request);
>         void reinitRequest(V4L2Request *request);
>  
> +       struct DewarpParms {
> +               Matrix<double, 3, 3> cm;
> +               std::vector<double> coeffs;
> +       };
> +
>         struct VertexMapInfo {
>                 Dw100VertexMap map;
>                 bool update;
>         };
>  
>         std::map<const Stream *, VertexMapInfo> vertexMaps_;
> +       std::optional<DewarpParms> dewarpParams_;
>         unsigned int inputBufferCount_;
>         V4L2M2MConverter converter_;
>         Rectangle sensorCrop_;
> diff --git a/src/libcamera/converter/converter_dw100.cpp b/src/libcamera/converter/converter_dw100.cpp
> index cba7cc9f709b..03ffd939dc63 100644
> --- a/src/libcamera/converter/converter_dw100.cpp
> +++ b/src/libcamera/converter/converter_dw100.cpp
> @@ -70,6 +70,74 @@ ConverterDW100Module::createModule(DeviceEnumerator *enumerator)
>         return {};
>  }
>  
> +/**
> + * \brief Initialize the module with configuration data
> + * \param[in] params The config parameters
> + *
> + * This function shall be called from the pipeline handler to initialize the
> + * module with the provided parameters.
> + *
> + * A typical tuning file entry for the dewarper looks like this:
> + * \code{.unparsed}
> + * modules:
> + * - Dewarp:
> + *    cm: [
> + *      1.0, 0.0, 0.0,
> + *      0.0, 1.0, 0.0,
> + *      0.0, 0.0, 1.0,
> + *    ]
> + *    coefficients: [
> + *      0,0,0,0,0,
> + *    ]
> + * \endcode
> + *
> + * The \a cm and \a coefficients parameters are documented in
> + * Dw100VertexMap::setDewarpParams()
> + *
> + * \sa Dw100VertexMap::setDewarpParams()
> + * \return 0 if successful, an error code otherwise
> + */
> +int ConverterDW100Module::init(const YamlObject &params)
> +{
> +       DewarpParms dp;
> +
> +       auto &cm = params["cm"];
> +       auto &coefficients = params["coefficients"];
> +
> +       /* If nothing is provided, the dewarper is still functional */
> +       if (!cm && !coefficients)
> +               return 0;
> +
> +       if (!cm) {
> +               LOG(Converter, Error) << "Dewarp parameters are missing 'cm' value";
> +               return -EINVAL;
> +       }
> +
> +       auto matrix = cm.get<Matrix<double, 3, 3>>();
> +       if (!matrix) {
> +               LOG(Converter, Error) << "Failed to load 'cm' value";
> +               return -EINVAL;
> +       }
> +
> +       dp.cm = *matrix;
> +
> +       if (!coefficients) {
> +               LOG(Converter, Error) << "Dewarp parameters are missing 'coefficients' value";
> +               return -EINVAL;
> +       }
> +
> +       const auto coeffs = coefficients.getList<double>();
> +       if (!coeffs) {
> +               LOG(Converter, Error) << "Dewarp parameters 'coefficients' value is not a list";
> +               return -EINVAL;
> +       }
> +       dp.coeffs = std::move(*coeffs);
> +
> +       dewarpParams_ = dp;
> +
> +       return 0;
> +}
> +
>  /**
>   * \copydoc libcamera::V4L2M2MConverter::configure
>   */
> @@ -93,6 +161,9 @@ int ConverterDW100Module::configure(const StreamConfiguration &inputCfg,
>                 vertexMap.setInputSize(inputCfg.size);
>                 vertexMap.setOutputSize(outputCfg.size);
>                 vertexMap.setSensorCrop(sensorCrop_);
> +
> +               if (dewarpParams_)
> +                       vertexMap.setDewarpParams(dewarpParams_->cm, dewarpParams_->coeffs);
>                 info.update = true;
>         }
>  
> diff --git a/src/libcamera/pipeline/rkisp1/rkisp1.cpp b/src/libcamera/pipeline/rkisp1/rkisp1.cpp
> index 746eeffab207..adce1de35263 100644
> --- a/src/libcamera/pipeline/rkisp1/rkisp1.cpp
> +++ b/src/libcamera/pipeline/rkisp1/rkisp1.cpp
> @@ -453,10 +453,18 @@ int RkISP1CameraData::loadTuningFile(const std::string &path)
>  
>         const auto &modules = (*data)["modules"].asList();
>         for (const auto &module : modules) {
> -               if (module.contains("Dewarp")) {
> -                       canUseDewarper_ = true;
> -                       break;
> -               }
> +               const auto &params = module["Dewarp"];
> +               if (!params)
> +                       continue;
> +
> +               ret = pipe()->dewarper_->init(params);
> +               if (ret)
> +                       return ret;
> +
> +               LOG(RkISP1, Info) << "Dw100 dewarper initialized";
> +
> +               canUseDewarper_ = true;
> +               return 0;
>         }
>  
>         return 0;
> -- 
> 2.51.0
>

Patch
diff mbox series

diff --git a/include/libcamera/internal/converter/converter_dw100.h b/include/libcamera/internal/converter/converter_dw100.h
index 8dd21a6228f1..f0c858689e27 100644
--- a/include/libcamera/internal/converter/converter_dw100.h
+++ b/include/libcamera/internal/converter/converter_dw100.h
@@ -31,6 +31,8 @@  public:
 
 	static std::unique_ptr<ConverterDW100Module> createModule(DeviceEnumerator *enumerator);
 
+	int init(const YamlObject &params);
+
 	int configure(const StreamConfiguration &inputCfg,
 		      const std::vector<std::reference_wrapper<StreamConfiguration>>
 			      &outputCfg);
@@ -67,12 +69,18 @@  private:
 	int applyControls(const Stream *stream, const V4L2Request *request);
 	void reinitRequest(V4L2Request *request);
 
+	struct DewarpParms {
+		Matrix<double, 3, 3> cm;
+		std::vector<double> coeffs;
+	};
+
 	struct VertexMapInfo {
 		Dw100VertexMap map;
 		bool update;
 	};
 
 	std::map<const Stream *, VertexMapInfo> vertexMaps_;
+	std::optional<DewarpParms> dewarpParams_;
 	unsigned int inputBufferCount_;
 	V4L2M2MConverter converter_;
 	Rectangle sensorCrop_;
diff --git a/src/libcamera/converter/converter_dw100.cpp b/src/libcamera/converter/converter_dw100.cpp
index cba7cc9f709b..03ffd939dc63 100644
--- a/src/libcamera/converter/converter_dw100.cpp
+++ b/src/libcamera/converter/converter_dw100.cpp
@@ -70,6 +70,74 @@  ConverterDW100Module::createModule(DeviceEnumerator *enumerator)
 	return {};
 }
 
+/**
+ * \brief Initialize the module with configuration data
+ * \param[in] params The config parameters
+ *
+ * This function shall be called from the pipeline handler to initialize the
+ * module with the provided parameters.
+ *
+ * A typical tuning file entry for the dewarper looks like this:
+ * \code{.unparsed}
+ * modules:
+ * - Dewarp:
+ *    cm: [
+ *      1.0, 0.0, 0.0,
+ *      0.0, 1.0, 0.0,
+ *      0.0, 0.0, 1.0,
+ *    ]
+ *    coefficients: [
+ *      0,0,0,0,0,
+ *    ]
+ * \endcode
+ *
+ * The \a cm and \a coefficients parameters are documented in
+ * Dw100VertexMap::setDewarpParams()
+ *
+ * \sa Dw100VertexMap::setDewarpParams()
+ * \return 0 if successful, an error code otherwise
+ */
+int ConverterDW100Module::init(const YamlObject &params)
+{
+	DewarpParms dp;
+
+	auto &cm = params["cm"];
+	auto &coefficients = params["coefficients"];
+
+	/* If nothing is provided, the dewarper is still functional */
+	if (!cm && !coefficients)
+		return 0;
+
+	if (!cm) {
+		LOG(Converter, Error) << "Dewarp parameters are missing 'cm' value";
+		return -EINVAL;
+	}
+
+	auto matrix = cm.get<Matrix<double, 3, 3>>();
+	if (!matrix) {
+		LOG(Converter, Error) << "Failed to load 'cm' value";
+		return -EINVAL;
+	}
+
+	dp.cm = *matrix;
+
+	if (!coefficients) {
+		LOG(Converter, Error) << "Dewarp parameters are missing 'coefficients' value";
+		return -EINVAL;
+	}
+
+	const auto coeffs = coefficients.getList<double>();
+	if (!coeffs) {
+		LOG(Converter, Error) << "Dewarp parameters 'coefficients' value is not a list";
+		return -EINVAL;
+	}
+	dp.coeffs = std::move(*coeffs);
+
+	dewarpParams_ = dp;
+
+	return 0;
+}
+
 /**
  * \copydoc libcamera::V4L2M2MConverter::configure
  */
@@ -93,6 +161,9 @@  int ConverterDW100Module::configure(const StreamConfiguration &inputCfg,
 		vertexMap.setInputSize(inputCfg.size);
 		vertexMap.setOutputSize(outputCfg.size);
 		vertexMap.setSensorCrop(sensorCrop_);
+
+		if (dewarpParams_)
+			vertexMap.setDewarpParams(dewarpParams_->cm, dewarpParams_->coeffs);
 		info.update = true;
 	}
 
diff --git a/src/libcamera/pipeline/rkisp1/rkisp1.cpp b/src/libcamera/pipeline/rkisp1/rkisp1.cpp
index 746eeffab207..adce1de35263 100644
--- a/src/libcamera/pipeline/rkisp1/rkisp1.cpp
+++ b/src/libcamera/pipeline/rkisp1/rkisp1.cpp
@@ -453,10 +453,18 @@  int RkISP1CameraData::loadTuningFile(const std::string &path)
 
 	const auto &modules = (*data)["modules"].asList();
 	for (const auto &module : modules) {
-		if (module.contains("Dewarp")) {
-			canUseDewarper_ = true;
-			break;
-		}
+		const auto &params = module["Dewarp"];
+		if (!params)
+			continue;
+
+		ret = pipe()->dewarper_->init(params);
+		if (ret)
+			return ret;
+
+		LOG(RkISP1, Info) << "Dw100 dewarper initialized";
+
+		canUseDewarper_ = true;
+		return 0;
 	}
 
 	return 0;