Patch Detail
Show a patch.
GET /api/patches/27204/?format=api
{ "id": 27204, "url": "https://patchwork.libcamera.org/api/patches/27204/?format=api", "web_url": "https://patchwork.libcamera.org/patch/27204/", "project": { "id": 1, "url": "https://patchwork.libcamera.org/api/projects/1/?format=api", "name": "libcamera", "link_name": "libcamera", "list_id": "libcamera_core", "list_email": "libcamera-devel@lists.libcamera.org", "web_url": "", "scm_url": "", "webscm_url": "" }, "msgid": "<20260706-libipa-algorithms-v3-4-968757b038bb@ideasonboard.com>", "date": "2026-07-06T08:01:24", "name": "[v3,04/35] ipa: libipa: Add CcmAlgorithm to libipa", "commit_ref": null, "pull_url": null, "state": "new", "archived": false, "hash": "30341356eb467671f333f92120e587fc7ef8d4c9", "submitter": { "id": 143, "url": "https://patchwork.libcamera.org/api/people/143/?format=api", "name": "Jacopo Mondi", "email": "jacopo.mondi@ideasonboard.com" }, "delegate": null, "mbox": "https://patchwork.libcamera.org/patch/27204/mbox/", "series": [ { "id": 6038, "url": "https://patchwork.libcamera.org/api/series/6038/?format=api", "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=6038", "date": "2026-07-06T08:01:20", "name": "ipa: libipa: Introduce libipa algorithms", "version": 3, "mbox": "https://patchwork.libcamera.org/series/6038/mbox/" } ], "comments": "https://patchwork.libcamera.org/api/patches/27204/comments/", "check": "pending", "checks": "https://patchwork.libcamera.org/api/patches/27204/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 4E39CC3308\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon, 6 Jul 2026 08:02:07 +0000 (UTC)", "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 689DF6600F;\n\tMon, 6 Jul 2026 10:02:05 +0200 (CEST)", "from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 9647565FF1\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 6 Jul 2026 10:01:59 +0200 (CEST)", "from [192.168.125.177] (mob-109-113-60-81.net.vodafone.it\n\t[109.113.60.81])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id DF1AC1B9D;\n\tMon, 6 Jul 2026 10:01:10 +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=\"frvy343H\"; dkim-atps=neutral", "DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1783324871;\n\tbh=6ScMKQJSBq7qeDItlcImfE7q4AwSHnLvxMUZRmbYI0o=;\n\th=From:Date:Subject:References:In-Reply-To:To:Cc:From;\n\tb=frvy343H6p7b2Kpmy2fEVjexjcUdmjwXKG05FQFeW/C8x6eOmhfngQCv0mvb4eRuA\n\tOqbrfNiKDuhRGjqCr+58HxMV5YDZrVDYi4H3NasExQGTjK6PDey5R2oEP4rwb324XU\n\tfYXlyT/Me2/U/YgUzLroOgCTMBtP3dL/STs4vWJM=", "From": "Jacopo Mondi <jacopo.mondi@ideasonboard.com>", "Date": "Mon, 06 Jul 2026 10:01:24 +0200", "Subject": "[PATCH v3 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": "<20260706-libipa-algorithms-v3-4-968757b038bb@ideasonboard.com>", "References": "<20260706-libipa-algorithms-v3-0-968757b038bb@ideasonboard.com>", "In-Reply-To": "<20260706-libipa-algorithms-v3-0-968757b038bb@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>", "X-Mailer": "b4 0.14.3", "X-Developer-Signature": "v=1; a=openpgp-sha256; l=11964;\n\ti=jacopo.mondi@ideasonboard.com; h=from:subject:message-id;\n\tbh=6ScMKQJSBq7qeDItlcImfE7q4AwSHnLvxMUZRmbYI0o=;\n\tb=owEBbQKS/ZANAwAKAXI0Bo8WoVY8AcsmYgBqS2DvzP/jmW4SbfFef8nxBy5awCD6q5t6M9i9Q\n\teWPtyFlcRKJAjMEAAEKAB0WIQS1xD1IgJogio9YOMByNAaPFqFWPAUCaktg7wAKCRByNAaPFqFW\n\tPO6sEACN837jgr2FZaI9wFJ5FMS7k2+nNZcghwT/5xodh4V64rM5PT6VxfhRp/mIikuD+2BGEaB\n\tCcaa31+XQOQ8vTXEGEwPoBmAmFcgrx/S8TP7dqY3sNgtypwRq2iueoXMBES2c8Eon6OITIe/yXa\n\tgM0dRPj0gO6rfxVOLYcl1UlGEh4FGG4Gb0xYHBP9LJ/X6KiMMgjuGNfS2e/pYBBuwB4RTVhL0C/\n\txZJrMcEAdwtRXYXRU6/2MkuqRHBbgNv+PnApjtX9QdyVTWVB7oZwOwKcwL48TWIl+RKc45Tpfh3\n\te3mSVxaB9IvbCaQv/CaonA1ZAp47AGou8ePMXIXxTRVppLf566w7yjVyZKvj6WtaYa1y1Z75IWG\n\tDQgNJ9henUixcuHBPhYENaYu7uWYW7iQch9BQUD4GfSCWMoFl8+zwvDFJ/3wjYjxmMI8vP9Y974\n\tCYZYwrKY5ST4/C3uJeWPhxiKX5QhBdiO3EQZwBdKzLTDNVV2V0RprS9eGblQF9H1s94P3/O1bn3\n\tZwZlG+iv3haQuguBIAC+kHHoXDY7Jvgk6dKmPhCXpyyLqRM0tfLydEAT0NgDeCchIAn3plwyIuD\n\ti+uBKVueo0NP1p9GqMLyp6UQ7BJjdHHvMJdXmuyv/FsXL0kd/iyBEy/a71AuF00UGjSitFItc++\n\t3PK+G8NxMgMKoZg==", "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>\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..ce556303f4f4\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": [ "v3", "04/35" ] }