{"id":20263,"url":"https://patchwork.libcamera.org/api/1.1/patches/20263/?format=json","web_url":"https://patchwork.libcamera.org/patch/20263/","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":"<20240611140207.520083-4-paul.elder@ideasonboard.com>","date":"2024-06-11T14:02:07","name":"[v7,3/3] ipa: rkisp1: algorithms: Add crosstalk algorithm","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"f139d083ca8d6f4d982be2a8eb45ce02ad1c0949","submitter":{"id":17,"url":"https://patchwork.libcamera.org/api/1.1/people/17/?format=json","name":"Paul Elder","email":"paul.elder@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/20263/mbox/","series":[{"id":4383,"url":"https://patchwork.libcamera.org/api/1.1/series/4383/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=4383","date":"2024-06-11T14:02:04","name":"ipa: rkisp1: Add crosstalk algorithm","version":7,"mbox":"https://patchwork.libcamera.org/series/4383/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/20263/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/20263/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 23D77C3292\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue, 11 Jun 2024 14:02:38 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id D8CFD65463;\n\tTue, 11 Jun 2024 16:02:36 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 9DCB661A26\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 11 Jun 2024 16:02:31 +0200 (CEST)","from neptunite.hamster-moth.ts.net\n\t(h175-177-049-156.catv02.itscom.jp [175.177.49.156])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id C3B0329A;\n\tTue, 11 Jun 2024 16:02:15 +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=\"qZJVTOQO\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1718114539;\n\tbh=1OOlzXMAZ/zuHGpZMsthYsCwiyfM/2we0+4WD2qJNn4=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=qZJVTOQO6V8oH68vNNYGypr4nRv3MWxxKcWkR85vrpwzz3QBZkZiFTmwP/qjfvK9f\n\t4E/kJ3JaxqJp0omwJlL+JMgrn0KW+rsBpgpolR9sDaSIe3GACqi+XMAnJeb7c6iyUA\n\tN2vYKXYpg4uGz+HgOKYRN5fH+kumY4yFFKe1iOIM=","From":"Paul Elder <paul.elder@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Cc":"Paul Elder <paul.elder@ideasonboard.com>,\n\tStefan Klug <stefan.klug@ideasonboard.com>","Subject":"[PATCH v7 3/3] ipa: rkisp1: algorithms: Add crosstalk algorithm","Date":"Tue, 11 Jun 2024 23:02:07 +0900","Message-Id":"<20240611140207.520083-4-paul.elder@ideasonboard.com>","X-Mailer":"git-send-email 2.39.2","In-Reply-To":"<20240611140207.520083-1-paul.elder@ideasonboard.com>","References":"<20240611140207.520083-1-paul.elder@ideasonboard.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","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 an algorithm module to the rkisp1 IPA for crosstalk correction.\n\nSigned-off-by: Paul Elder <paul.elder@ideasonboard.com>\nReviewed-by: Stefan Klug <stefan.klug@ideasonboard.com>\n\n---\nChanges in v7:\n- make offsets_ default to zero-matrices as opposed to identity matrices\n- checkstyle\n- populate metadata\n  - add ccm to IPAFrameContext\n- don't update the ccm if the color temperature didn't change\n\nNo change in v6\n\nChanges in v5:\n- clean up documentation\n- coalesce parseYaml into init\n\nChanges in v4:\n- remove stray semicolons\n- use the new matrix interpolator readYaml\n- use the new matrix operator[] getter\n\nChanges in v3:\n- read ccm offsets from tuning data, and write these offsets to the\n  parameters buffer\n- make parseYaml return void, as it should fill in default data if\n  unable to read, thus never failing\n\nChanges in v2:\n- rename ctk to ccm\n- reset the matrix interpolator to identity matrix if failed to read\n  from tuning file\n---\n src/ipa/rkisp1/algorithms/ccm.cpp     | 140 ++++++++++++++++++++++++++\n src/ipa/rkisp1/algorithms/ccm.h       |  50 +++++++++\n src/ipa/rkisp1/algorithms/meson.build |   1 +\n src/ipa/rkisp1/ipa_context.h          |   5 +\n 4 files changed, 196 insertions(+)\n create mode 100644 src/ipa/rkisp1/algorithms/ccm.cpp\n create mode 100644 src/ipa/rkisp1/algorithms/ccm.h","diff":"diff --git a/src/ipa/rkisp1/algorithms/ccm.cpp b/src/ipa/rkisp1/algorithms/ccm.cpp\nnew file mode 100644\nindex 000000000000..09fe4b2aa1bc\n--- /dev/null\n+++ b/src/ipa/rkisp1/algorithms/ccm.cpp\n@@ -0,0 +1,140 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2024, Ideas On Board\n+ *\n+ * RkISP1 Color Correction Matrix control algorithm\n+ */\n+\n+#include \"ccm.h\"\n+\n+#include <algorithm>\n+#include <chrono>\n+#include <cmath>\n+#include <tuple>\n+#include <vector>\n+\n+#include <libcamera/base/log.h>\n+#include <libcamera/base/utils.h>\n+\n+#include <libcamera/control_ids.h>\n+\n+#include <libcamera/ipa/core_ipa_interface.h>\n+\n+#include \"libcamera/internal/yaml_parser.h\"\n+\n+#include \"../utils.h\"\n+#include \"libipa/matrix_interpolator.h\"\n+\n+/**\n+ * \\file ccm.h\n+ */\n+\n+namespace libcamera {\n+\n+namespace ipa::rkisp1::algorithms {\n+\n+/**\n+ * \\class Ccm\n+ * \\brief A color correction matrix algorithm\n+ */\n+\n+LOG_DEFINE_CATEGORY(RkISP1Ccm)\n+\n+/**\n+ * \\copydoc libcamera::ipa::Algorithm::init\n+ */\n+int Ccm::init([[maybe_unused]] IPAContext &context, const YamlObject &tuningData)\n+{\n+\tint ret = ccm_.readYaml(tuningData[\"ccms\"], \"ct\", \"ccm\");\n+\tif (ret < 0) {\n+\t\tLOG(RkISP1Ccm, 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_.reset();\n+\t}\n+\n+\tret = offsets_.readYaml(tuningData[\"ccms\"], \"ct\", \"offsets\");\n+\tif (ret < 0) {\n+\t\tLOG(RkISP1Ccm, Warning)\n+\t\t\t<< \"Failed to parse 'offsets' \"\n+\t\t\t<< \"parameter from tuning file; falling back to zero offsets\";\n+\t\t/*\n+\t\t * MatrixInterpolator::reset() resets to identity matrices\n+\t\t * while here we need zero matrices so we need to construct it\n+\t\t * ourselves.\n+\t\t */\n+\t\tMatrix<int16_t, 3, 1> m({ 0, 0, 0 });\n+\t\tstd::map<unsigned int, Matrix<int16_t, 3, 1>> matrices = { { 0, m } };\n+\t\toffsets_ = MatrixInterpolator<int16_t, 3, 1>(matrices);\n+\t}\n+\n+\treturn 0;\n+}\n+\n+void Ccm::setParameters(rkisp1_params_cfg *params,\n+\t\t\tconst Matrix<double, 3, 3> &matrix,\n+\t\t\tconst Matrix<int16_t, 3, 1> &offsets)\n+{\n+\tstruct rkisp1_cif_isp_ctk_config &config = params->others.ctk_config;\n+\n+\t/*\n+\t * 4 bit integer and 7 bit fractional, ranging from -8 (0x400) to\n+\t * +7.992 (0x3ff)\n+\t */\n+\tfor (unsigned int i = 0; i < 3; i++)\n+\t\tfor (unsigned int j = 0; j < 3; j++)\n+\t\t\tconfig.coeff[i][j] =\n+\t\t\t\tutils::floatingToFixedPoint<4, 7, uint16_t, double>(matrix[i][j]);\n+\n+\tfor (unsigned int i = 0; i < 3; i++)\n+\t\tconfig.ct_offset[i] = offsets[i][0] & 0xfff;\n+\n+\tLOG(RkISP1Ccm, Debug) << \"Setting matrix \" << matrix;\n+\tLOG(RkISP1Ccm, Debug) << \"Setting offsets \" << offsets;\n+\n+\tparams->module_en_update |= RKISP1_CIF_ISP_MODULE_CTK;\n+\tparams->module_ens |= RKISP1_CIF_ISP_MODULE_CTK;\n+\tparams->module_cfg_update |= RKISP1_CIF_ISP_MODULE_CTK;\n+}\n+\n+/**\n+ * \\copydoc libcamera::ipa::Algorithm::prepare\n+ */\n+void Ccm::prepare(IPAContext &context, [[maybe_unused]] const uint32_t frame,\n+\t\t  IPAFrameContext &frameContext,\n+\t\t  rkisp1_params_cfg *params)\n+{\n+\tuint32_t ct = context.activeState.awb.temperatureK;\n+\tif (ct == ct_)\n+\t\treturn;\n+\n+\tct_ = ct;\n+\tMatrix<double, 3, 3> ccm = ccm_.get(ct);\n+\tMatrix<int16_t, 3, 1> offsets = offsets_.get(ct);\n+\n+\tframeContext.ccm.ccm = ccm;\n+\n+\tsetParameters(params, ccm, offsets);\n+}\n+\n+/**\n+ * \\copydoc libcamera::ipa::Algorithm::process\n+ */\n+void process([[maybe_unused]] IPAContext &context,\n+\t     [[maybe_unused]] const uint32_t frame,\n+\t     IPAFrameContext &frameContext,\n+\t     [[maybe_unused]] const rkisp1_stat_buffer *stats,\n+\t     ControlList &metadata)\n+{\n+\tfloat m[9];\n+\tfor (unsigned int i = 0; i < 3; i++)\n+\t\tfor (unsigned int j = 0; j < 3; j++)\n+\t\t\tm[i] = frameContext.ccm.ccm[i][j];\n+\tmetadata.set(controls::ColourCorrectionMatrix, m);\n+}\n+\n+REGISTER_IPA_ALGORITHM(Ccm, \"Ccm\")\n+\n+} /* namespace ipa::rkisp1::algorithms */\n+\n+} /* namespace libcamera */\ndiff --git a/src/ipa/rkisp1/algorithms/ccm.h b/src/ipa/rkisp1/algorithms/ccm.h\nnew file mode 100644\nindex 000000000000..09a6801626b4\n--- /dev/null\n+++ b/src/ipa/rkisp1/algorithms/ccm.h\n@@ -0,0 +1,50 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2024, Ideas On Board\n+ *\n+ * RkISP1 Color Correction Matrix control algorithm\n+ */\n+\n+#pragma once\n+\n+#include <linux/rkisp1-config.h>\n+\n+#include \"libipa/matrix.h\"\n+#include \"libipa/matrix_interpolator.h\"\n+\n+#include \"algorithm.h\"\n+\n+namespace libcamera {\n+\n+namespace ipa::rkisp1::algorithms {\n+\n+class Ccm : public Algorithm\n+{\n+public:\n+\tCcm() {}\n+\t~Ccm() = default;\n+\n+\tint init(IPAContext &context, const YamlObject &tuningData) override;\n+\tvoid prepare(IPAContext &context, const uint32_t frame,\n+\t\t     IPAFrameContext &frameContext,\n+\t\t     rkisp1_params_cfg *params) override;\n+\tvoid process([[maybe_unused]] IPAContext &context,\n+\t\t     [[maybe_unused]] const uint32_t frame,\n+\t\t     IPAFrameContext &frameContext,\n+\t\t     [[maybe_unused]] const rkisp1_stat_buffer *stats,\n+\t\t     ControlList &metadata) override;\n+\n+private:\n+\tvoid parseYaml(const YamlObject &tuningData);\n+\tvoid setParameters(rkisp1_params_cfg *params,\n+\t\t\t   const Matrix<double, 3, 3> &matrix,\n+\t\t\t   const Matrix<int16_t, 3, 1> &offsets);\n+\n+\tunsigned int ct_;\n+\tMatrixInterpolator<double, 3, 3> ccm_;\n+\tMatrixInterpolator<int16_t, 3, 1> offsets_;\n+};\n+\n+} /* namespace ipa::rkisp1::algorithms */\n+\n+} /* namespace libcamera */\ndiff --git a/src/ipa/rkisp1/algorithms/meson.build b/src/ipa/rkisp1/algorithms/meson.build\nindex 6ee71a9b5da3..1734a6675f78 100644\n--- a/src/ipa/rkisp1/algorithms/meson.build\n+++ b/src/ipa/rkisp1/algorithms/meson.build\n@@ -4,6 +4,7 @@ rkisp1_ipa_algorithms = files([\n     'agc.cpp',\n     'awb.cpp',\n     'blc.cpp',\n+    'ccm.cpp',\n     'cproc.cpp',\n     'dpcc.cpp',\n     'dpf.cpp',\ndiff --git a/src/ipa/rkisp1/ipa_context.h b/src/ipa/rkisp1/ipa_context.h\nindex 2a994d81ae41..cfb1f9770870 100644\n--- a/src/ipa/rkisp1/ipa_context.h\n+++ b/src/ipa/rkisp1/ipa_context.h\n@@ -16,6 +16,7 @@\n #include <libcamera/geometry.h>\n \n #include <libipa/fc_queue.h>\n+#include <libipa/matrix.h>\n \n namespace libcamera {\n \n@@ -155,6 +156,10 @@ struct IPAFrameContext : public FrameContext {\n \t\tuint32_t exposure;\n \t\tdouble gain;\n \t} sensor;\n+\n+\tstruct {\n+\t\tMatrix<double, 3, 3> ccm;\n+\t} ccm;\n };\n \n struct IPAContext {\n","prefixes":["v7","3/3"]}