From 302f285b3b2e2c9b84694e882f301331097bbdbb Mon Sep 17 00:00:00 2001 From: Stefan Klug Date: Tue, 25 Nov 2025 17:28:37 +0100 Subject: [PATCH] pipeline: rkisp1: Load dewarp parameters from tuning file Load the dewarp parameters from the tuning file. Signed-off-by: Stefan Klug Reviewed-by: Kieran Bingham --- .../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 5219a0dd..d70c8fe7 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 createModule(DeviceEnumerator *enumerator); + int init(const YamlObject ¶ms); + int configure(const StreamConfiguration &inputCfg, const std::vector> &outputCfg); @@ -67,12 +69,18 @@ private: int applyControls(const Stream *stream, const V4L2Request *request); void reinitRequest(V4L2Request *request); + struct DewarpParms { + Matrix cm; + std::vector coeffs; + }; + struct VertexMapInfo { Dw100VertexMap map; bool update; }; std::map vertexMaps_; + std::optional 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 c6a37507..16bbc417 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 ¶ms) +{ + 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>(); + 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(); + 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 e9652905..5fd11026 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 ¶ms = 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;