{"id":27269,"url":"https://patchwork.libcamera.org/api/1.1/patches/27269/?format=json","web_url":"https://patchwork.libcamera.org/patch/27269/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20260708-libipa-algorithms-v4-4-f0e99f035294@ideasonboard.com>","date":"2026-07-08T09:24:51","name":"[v4,04/35] ipa: libipa: Add CcmAlgorithm to libipa","commit_ref":null,"pull_url":null,"state":"new","archived":false,"hash":"2ce79f7e1865b96bcc74c5d2354698872eaef0cd","submitter":{"id":143,"url":"https://patchwork.libcamera.org/api/1.1/people/143/?format=json","name":"Jacopo Mondi","email":"jacopo.mondi@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/27269/mbox/","series":[{"id":6046,"url":"https://patchwork.libcamera.org/api/1.1/series/6046/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=6046","date":"2026-07-08T09:24:47","name":"ipa: libipa: Introduce libipa algorithms","version":4,"mbox":"https://patchwork.libcamera.org/series/6046/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/27269/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/27269/checks/","tags":{},"headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 7F05FC3301\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed,  8 Jul 2026 09:25:20 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 6384F66093;\n\tWed,  8 Jul 2026 11:25:11 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 5F84A65FEF\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed,  8 Jul 2026 11:25:06 +0200 (CEST)","from [192.168.1.182] (93-46-82-201.ip106.fastwebnet.it\n\t[93.46.82.201])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id A63781C2A;\n\tWed,  8 Jul 2026 11:24:16 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"YNOdW4x2\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1783502656;\n\tbh=f9+XfBVeEJ4VSD1bfCxQGr3LUyOFZa7nG5xmDQhXjKc=;\n\th=From:Date:Subject:References:In-Reply-To:To:Cc:From;\n\tb=YNOdW4x2n/Hn2tY0ySSIf5MvpKpkpXWcfUwT+oYnioSsmUOeWndOwqIWRovA50dn5\n\tLUTWpFf2nOh29A4mDCriL5OFu8mrdug3cN7ce6v669W/hDypFv4iGX9oQJRtIX1+LX\n\ti/9LtBMZOusBQSRlRjPMEKJWinJOPbRQ8ZyAM51A=","From":"Jacopo Mondi <jacopo.mondi@ideasonboard.com>","Date":"Wed, 08 Jul 2026 11:24:51 +0200","Subject":"[PATCH v4 04/35] ipa: libipa: Add CcmAlgorithm to libipa","MIME-Version":"1.0","Content-Type":"text/plain; charset=\"utf-8\"","Content-Transfer-Encoding":"7bit","Message-Id":"<20260708-libipa-algorithms-v4-4-f0e99f035294@ideasonboard.com>","References":"<20260708-libipa-algorithms-v4-0-f0e99f035294@ideasonboard.com>","In-Reply-To":"<20260708-libipa-algorithms-v4-0-f0e99f035294@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org, \n\tStefan Klug <stefan.klug@ideasonboard.com>","Cc":"Jacopo Mondi <jacopo.mondi@ideasonboard.com>, \n\tKieran Bingham <kieran.bingham@ideasonboard.com>, \n\tDaniel Scally <dan.scally@ideasonboard.com>, \n\tRobert Mader <robert.mader@collabora.com>","X-Mailer":"b4 0.14.3","X-Developer-Signature":"v=1; a=openpgp-sha256; l=12018;\n\ti=jacopo.mondi@ideasonboard.com; h=from:subject:message-id;\n\tbh=f9+XfBVeEJ4VSD1bfCxQGr3LUyOFZa7nG5xmDQhXjKc=;\n\tb=owEBbQKS/ZANAwAKAXI0Bo8WoVY8AcsmYgBqThdsxOoiKpbo7+Zyfl31TqJA66UM8u3cSM9I9\n\tn+Ol9+aKemJAjMEAAEKAB0WIQS1xD1IgJogio9YOMByNAaPFqFWPAUCak4XbAAKCRByNAaPFqFW\n\tPOfED/46qVa6RGCPqy/zP9QlUhhixB1tD2z2+2U1dES5/PsvifUU/HCgQVlhIMlzVn4ipgQiq4l\n\tjTWyAZys5HFX8KjZEMGrzPgdSNd+dcLElrf0kfKSTlpaMpXa5r8o8c1hYmfu44lc43+ltZ2Cjrf\n\ts9DPFAdWdSBJ9ZWjcALadNr2XdjO2Tw0JvdvavsTN6L8Q23SPBQr4625wFfFMT/eLxBSADr3IMB\n\tqcZUZJUeodPHqk2Q+ozYdu/PMHQmVLI5cEGl7yfJMMEMWROkJ4L20gcaCxsKw+hKorEyVT8z1HQ\n\ttlvO36X0L2GfR9DIFLqxMMcd1kboD4PT5ocQD9euvolj5Q54PPUB5FBEJaz7eakSf7zXQphnuVh\n\tVZN0B5m0stanSbXFZu5f6SvRMY87ukcVcajHHxLMMTwhAEG4VHqCFhFY95yaHL7aMbXnCVNOZPH\n\th7A7/jk3sNcrvwKimwlyrMlWBnQEvl5N7kA35Wczj+uwOcZJkAPHDjwMz0B5n1eYNeAeWqQSPnO\n\tKZ4ZVQJgrc2ljyHMSYFodm/TAL93iA1kJFixd9cUjDrsL05JhRzsLDsoy9F6Lkl5HnBTm6prA9D\n\tsSUP5kLjuDd61j6rRwsmYB7FalSai/sFPxDU79ZGsRscLe7o1oroayvkza2zkJYtAJzZQSGy9uT\n\t8hCTobjH6lHQPuA==","X-Developer-Key":"i=jacopo.mondi@ideasonboard.com; a=openpgp;\n\tfpr=72392EDC88144A65C701EA9BA5826A2587AD026B","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"Add a CcmAlgorithm to libipa.\n\nThe CcmAlgorithm performs interpolation of the colour correction\nmatrices as loaded from tuning file on a colour temperature.\n\nThe implementation is based on the existing RkISP1 CCM algorihtm.\n\nSigned-off-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>\nReviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\nReviewed-by: Daniel Scally <dan.scally@ideasonboard.com>\nTested-by: Robert Mader <robert.mader@collabora.com>\n---\n src/ipa/libipa/ccm.cpp     | 245 +++++++++++++++++++++++++++++++++++++++++++++\n src/ipa/libipa/ccm.h       |  77 ++++++++++++++\n src/ipa/libipa/meson.build |   2 +\n 3 files changed, 324 insertions(+)","diff":"diff --git a/src/ipa/libipa/ccm.cpp b/src/ipa/libipa/ccm.cpp\nnew file mode 100644\nindex 000000000000..8f3652f7e32b\n--- /dev/null\n+++ b/src/ipa/libipa/ccm.cpp\n@@ -0,0 +1,245 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2026 Ideas on Board Oy\n+ *\n+ * libIPA CCM algorithm\n+ */\n+\n+#include \"ccm.h\"\n+\n+/**\n+ * \\file ccm.h\n+ * \\brief libipa CCM (Colour Correction Matrix) algorithm\n+ */\n+\n+namespace libcamera {\n+\n+namespace ipa {\n+\n+LOG_DEFINE_CATEGORY(Ccm)\n+\n+namespace ccm {\n+\n+/**\n+ * \\struct ActiveState\n+ * \\brief Active CCM state\n+ *\n+ * \\var ActiveState::manual\n+ * \\brief The most recent manually requested CCM state\n+ *\n+ * \\var ActiveState::automatic\n+ * \\brief The most recent automatically calculated CCM state\n+ */\n+\n+/**\n+ * \\struct CcmContext\n+ * \\brief CCM coefficients and offsets\n+ *\n+ * \\var CcmContext::ccm\n+ * \\brief Matrix of 3x3 CCM coefficients\n+ *\n+ * \\var CcmContext::offsets\n+ * \\brief Vector of RGB CCM offsets\n+ */\n+\n+/**\n+ * \\typedef FrameContext\n+ * \\brief Per-frame CCM state\n+ */\n+\n+} /* namespace ccm */\n+\n+/**\n+ * \\class CcmAlgorithmBase\n+ * \\brief Base class for CcmAlgorithm for non-templated functions implementation\n+ *\n+ * Base class for CcmAlgorithm where non-templated functions are implemented.\n+ * IPA implementations shall use CcmAlgorithm and not this class.\n+ */\n+\n+/**\n+ * \\brief Initialize the algorithm with the given tuning data\n+ * \\param[in] tuningData The tuning data to use for the algorithm\n+ *\n+ * Parse \\a tuningData to initialize the CCM algorithm and register controls.\n+ * IPA modules are expected to call this function as part of their\n+ * implementation of Algorithm::init().\n+ *\n+ * \\return 0 on success, a negative error code otherwise\n+ */\n+int CcmAlgorithmBase::init(const ValueNode &tuningData)\n+{\n+\tint ret = ccm_.readYaml(tuningData[\"ccms\"], \"ct\", \"ccm\");\n+\tif (ret < 0) {\n+\t\tLOG(Ccm, Warning)\n+\t\t\t<< \"Failed to parse 'ccm' \"\n+\t\t\t<< \"parameter from tuning file; falling back to unit matrix\";\n+\t\tccm_.setData({ { 0, Matrix<float, 3, 3>::identity() } });\n+\t}\n+\n+\tret = offsets_.readYaml(tuningData[\"ccms\"], \"ct\", \"offsets\");\n+\tif (ret < 0) {\n+\t\tLOG(Ccm, Warning)\n+\t\t\t<< \"Failed to parse 'offsets' \"\n+\t\t\t<< \"parameter from tuning file; falling back to zero offsets\";\n+\n+\t\toffsets_.setData({ { 0, Matrix<int16_t, 3, 1>({ 0, 0, 0 }) } });\n+\t}\n+\n+\treturn 0;\n+}\n+\n+/**\n+ * \\brief Configure the CCM algorithm\n+ * \\param[in] state The CCM active state\n+ * \\param[in] temperatureK The colour temperature in Kelvin\n+ *\n+ * Configure the CCM algorithm by initializing the manual and automatic\n+ * states in \\a state by interpolating the default colour correction matrix\n+ * with the given colour temperature \\a temperatureK.\n+ *\n+ * \\return 0 if successful, an error code otherwise\n+ */\n+int CcmAlgorithmBase::configure(ccm::ActiveState &state, unsigned int temperatureK)\n+{\n+\tstate.manual.ccm = ccm_.getInterpolated(temperatureK);\n+\tstate.manual.offsets = offsets_.getInterpolated(temperatureK);\n+\tstate.automatic.ccm = ccm_.getInterpolated(temperatureK);\n+\tstate.automatic.offsets = offsets_.getInterpolated(temperatureK);\n+\n+\treturn 0;\n+}\n+\n+/**\n+ * \\brief Queue a Request to the CCM algorithm\n+ * \\param[in] state The CCM active state\n+ * \\param[in] context The CCM frame context\n+ * \\param[in] controls The list of controls associated with the Request\n+ *\n+ * Queue a new Request to the CCM algorithm and store the manual colour\n+ * correction matrix and temperature in \\a frameContext.\n+ *\n+ * The currently handled controls are:\n+ * - controls::ColourTemperature\n+ * - controls::ColourCorrectionMatrix\n+ *\n+ * When controls::ColourCorrectionMatrix is passed in the supplied matrix is\n+ * stored in \\a state and \\a context.\n+ *\n+ * When controls::ColourTemperature is passed in, the matrices loaded from\n+ * configuration file are interpolated with the given temperature and the result\n+ * is stored in \\a state and \\a context.\n+ *\n+ * If the IPA is running in manual mode, the IPA CCM algorithm implementations\n+ * can use the matrix coefficients and offsets directly from \\a context after\n+ * calling this function to program the HW CCM engine, without calling prepare().\n+ */\n+void CcmAlgorithmBase::queueRequest(ccm::ActiveState &state,\n+\t\t\t\t    ccm::FrameContext &context,\n+\t\t\t\t    const ControlList &controls)\n+{\n+\tconst auto &colourTemperature = controls.get(controls::ColourTemperature);\n+\tconst auto &ccmMatrix = controls.get(controls::ColourCorrectionMatrix);\n+\tif (ccmMatrix) {\n+\t\tstate.manual.ccm = Matrix<float, 3, 3>(*ccmMatrix);\n+\t\tLOG(Ccm, Debug) << \"Setting manual CCM from CCM control to \"\n+\t\t\t\t<< state.manual.ccm;\n+\t} else if (colourTemperature) {\n+\t\tstate.manual.ccm = ccm_.getInterpolated(*colourTemperature);\n+\t\tLOG(Ccm, Debug) << \"Setting manual CCM from CT control to \"\n+\t\t\t\t<< state.manual.ccm;\n+\t}\n+\n+\tcontext = state.manual;\n+}\n+\n+/**\n+ * \\brief Calculate the matrix coefficients for a colour temperature\n+ * \\param[in] state The CCM active state\n+ * \\param[in] context The CCM frame context\n+ * \\param[in] frame The frame number\n+ * \\param[in] temperatureK The colour temperature in Kelvin\n+ *\n+ * Interpolate the colour correction matrices as loaded from configuration file\n+ * for colour temperature \\a temperatureK.\n+ *\n+ * The function shall only be called if the IPA algorithm is running in auto\n+ * mode. If running in manual mode the application supplied correction matrix is\n+ * stored in \\a frameContext at queueRequest() time.\n+ */\n+void CcmAlgorithmBase::prepare(ccm::ActiveState &state,\n+\t\t\t       ccm::FrameContext &context,\n+\t\t\t       unsigned int frame, unsigned int temperatureK)\n+{\n+\tif (frame > 0 && temperatureK == ct_) {\n+\t\tcontext = state.automatic;\n+\t\treturn;\n+\t}\n+\n+\tct_ = temperatureK;\n+\tcontext.ccm = ccm_.getInterpolated(ct_);\n+\tcontext.offsets = offsets_.getInterpolated(ct_);\n+\n+\tstate.automatic = context;\n+}\n+\n+/**\n+ * \\brief Populate metadata with the latest correction matrix coefficients\n+ * \\param[in] context The CCM frame context\n+ * \\param[out] metadata The metadata list\n+ */\n+void CcmAlgorithmBase::process(ccm::FrameContext &context, ControlList &metadata)\n+{\n+\tmetadata.set(controls::ColourCorrectionMatrix, context.ccm.data());\n+}\n+\n+/**\n+ * \\class CcmAlgorithm\n+ * \\brief The libipa CCM algorithm\n+ * \\tparam Q The fixedpoint register representation of the colour correction\n+ * coefficients\n+ *\n+ * Implement the CCM algorithm for libipa.\n+ *\n+ * The CcmAlgorithm class implements an interface similar in spirit to the one\n+ * of the Algorithm class. IPA modules are expected to store an instance of\n+ * CcmAlgorithm as class member, template it with the CCM coefficients register\n+ * representation and call its functions in their implementations of the\n+ * Algorithm interface.\n+ *\n+ * The CcmAlgorithm class provides an init() function where tuning data is\n+ * parsed and the per-colour temperature correction matrices are loaded from\n+ * the tuning file.\n+ *\n+ * CcmAlgorithm supports both automatic and manual colour correction operations,\n+ * but doesn't offer a way to select one of them. Enabling or disabling\n+ * automatic CCM operations usually goes through the Awb algorithm\n+ * enable/disable as the two algorithms should work with the same mode.\n+ *\n+ * When the IPA module runs in manual mode a custom colour correction matrix\n+ * or a custom colour temperature can be supplied to the CCM algorithm at\n+ * queueRequest() time. If the Request contains a color correction matrix\n+ * (controls::ColourCorrectionMatrix) then the matrix coefficients gets saved in\n+ * the FrameContext and the IPA module can immediately use them and doesn't need\n+ * to call prepare(). If a custom colour temperature is provided\n+ * (controls::ColourTemperature) then the matrices loaded from configuration are\n+ * interpolated with it and the result is saved in the FrameContext. In this\n+ * case as well IPA modules can use the result immediately and should avoid\n+ * calling prepare().\n+ *\n+ * When the IPA module runs in automatic mode instead, it estimates the scene\n+ * colour temperature. The estimated colour temperature shall be passed to\n+ * prepare(), where it is used to interpolate the matrices loaded from the\n+ * tuning file. The resulting coefficients are stored in the FrameContext for\n+ * the IPA algorithm to use them to program their CCM engine registers.\n+ */\n+\n+/**\n+ * \\fn CcmAlgorithm::init()\n+ * \\param[in] controls The info map of the IPA controls\n+ * \\copydoc CcmAlgorithmBase::init()\n+ */\n+\n+} /* namespace ipa */\n+\n+} /* namespace libcamera */\ndiff --git a/src/ipa/libipa/ccm.h b/src/ipa/libipa/ccm.h\nnew file mode 100644\nindex 000000000000..b72b25e2a3b3\n--- /dev/null\n+++ b/src/ipa/libipa/ccm.h\n@@ -0,0 +1,77 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2026 Ideas on Board Oy\n+ *\n+ * libIPA CCM algorithm\n+ */\n+\n+#pragma once\n+\n+#include <libcamera/control_ids.h>\n+#include <libcamera/controls.h>\n+\n+#include \"libcamera/internal/matrix.h\"\n+\n+#include \"fixedpoint.h\"\n+#include \"interpolator.h\"\n+\n+namespace libcamera {\n+\n+namespace ipa {\n+\n+namespace ccm {\n+\n+struct CcmContext {\n+\tMatrix<float, 3, 3> ccm;\n+\tMatrix<int16_t, 3, 1> offsets;\n+};\n+\n+struct ActiveState {\n+\tstruct CcmContext manual;\n+\tstruct CcmContext automatic;\n+};\n+\n+using FrameContext = CcmContext;\n+\n+} /* namespace ccm */\n+\n+class CcmAlgorithmBase\n+{\n+public:\n+\tint init(const ValueNode &tuningData);\n+\tint configure(ccm::ActiveState &state, unsigned int temperatureK);\n+\tvoid queueRequest(ccm::ActiveState &state, ccm::FrameContext &context,\n+\t\t\t  const ControlList &controls);\n+\n+\tvoid prepare(ccm::ActiveState &state, ccm::FrameContext &context,\n+\t\t     unsigned int frame, unsigned int temperatureK);\n+\tvoid process(ccm::FrameContext &context, ControlList &metadata);\n+\n+private:\n+\tunsigned int ct_;\n+\tInterpolator<Matrix<float, 3, 3>> ccm_;\n+\tInterpolator<Matrix<int16_t, 3, 1>> offsets_;\n+};\n+\n+template<typename Q>\n+class CcmAlgorithm : public CcmAlgorithmBase\n+{\n+public:\n+\tint init(const ValueNode &tuningData, ControlInfoMap::Map &controls)\n+\t{\n+\t\tint ret = CcmAlgorithmBase::init(tuningData);\n+\t\tif (ret)\n+\t\t\treturn ret;\n+\n+\t\tcontrols[&controls::ColourCorrectionMatrix] =\n+\t\t\tControlInfo(ControlValue(Q::TraitsType::min),\n+\t\t\t\t    ControlValue(Q::TraitsType::max),\n+\t\t\t\t    ControlValue(Matrix<float, 3, 3>::identity().data()));\n+\n+\t\treturn 0;\n+\t}\n+};\n+\n+} /* namespace ipa */\n+\n+} /* namespace libcamera */\ndiff --git a/src/ipa/libipa/meson.build b/src/ipa/libipa/meson.build\nindex 963c5ee73063..edf8eabd8b78 100644\n--- a/src/ipa/libipa/meson.build\n+++ b/src/ipa/libipa/meson.build\n@@ -7,6 +7,7 @@ libipa_headers = files([\n     'awb_grey.h',\n     'awb.h',\n     'camera_sensor_helper.h',\n+    'ccm.h',\n     'colours.h',\n     'exposure_mode_helper.h',\n     'fc_queue.h',\n@@ -28,6 +29,7 @@ libipa_sources = files([\n     'awb_grey.cpp',\n     'awb.cpp',\n     'camera_sensor_helper.cpp',\n+    'ccm.cpp',\n     'colours.cpp',\n     'exposure_mode_helper.cpp',\n     'fc_queue.cpp',\n","prefixes":["v4","04/35"]}