{"id":27235,"url":"https://patchwork.libcamera.org/api/patches/27235/?format=json","web_url":"https://patchwork.libcamera.org/patch/27235/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/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":"<20260706-libipa-algorithms-v3-35-968757b038bb@ideasonboard.com>","date":"2026-07-06T08:01:55","name":"[v3,35/35] ipa: mali-c55: Port to use LscAlgorithm","commit_ref":null,"pull_url":null,"state":"new","archived":false,"hash":"2747420da53acd93bf55cd8b6a7abfe0d1190e57","submitter":{"id":143,"url":"https://patchwork.libcamera.org/api/people/143/?format=json","name":"Jacopo Mondi","email":"jacopo.mondi@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/27235/mbox/","series":[{"id":6038,"url":"https://patchwork.libcamera.org/api/series/6038/?format=json","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/27235/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/27235/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 4B86FBF415\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon,  6 Jul 2026 08:02:42 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id D8D2066060;\n\tMon,  6 Jul 2026 10:02:41 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 905AE6604D\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon,  6 Jul 2026 10:02:21 +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 C79843F8B;\n\tMon,  6 Jul 2026 10:01:32 +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=\"ut2Zd32r\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1783324893;\n\tbh=f1UqyIzI2T7DnX6z/Cnua71GzNArb08yYpmFTSX6oFQ=;\n\th=From:Date:Subject:References:In-Reply-To:To:Cc:From;\n\tb=ut2Zd32rhTolXBX6+LTURPCqg4dVWI4PEpajNJ5UCxja4tQqUJkT2czM74ay6MZDE\n\t0f9kCNhHEmDAbwlkY9pCYB7aUyjtVOjM7jjbCUjqLKsKJ1Ncd1gPcOnKuG6D6vruch\n\tzLx18KNqK6ocIIx5h+i+m76WuFCX4euZlVDIlkxw=","From":"Jacopo Mondi <jacopo.mondi@ideasonboard.com>","Date":"Mon, 06 Jul 2026 10:01:55 +0200","Subject":"[PATCH v3 35/35] ipa: mali-c55: Port to use LscAlgorithm","MIME-Version":"1.0","Content-Type":"text/plain; charset=\"utf-8\"","Content-Transfer-Encoding":"7bit","Message-Id":"<20260706-libipa-algorithms-v3-35-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=10843;\n\ti=jacopo.mondi@ideasonboard.com; h=from:subject:message-id;\n\tbh=f1UqyIzI2T7DnX6z/Cnua71GzNArb08yYpmFTSX6oFQ=;\n\tb=owEBbQKS/ZANAwAKAXI0Bo8WoVY8AcsmYgBqS2DzfuiMSsbV7ENzOuoA36F8YD4KLI81QsPxI\n\tvpbBCmEU0WJAjMEAAEKAB0WIQS1xD1IgJogio9YOMByNAaPFqFWPAUCaktg8wAKCRByNAaPFqFW\n\tPHf2D/0ZYaHc1ycNYddqGCHdSelqzMT5HYCkOHZWzKROBvZOD68Mucwk15STELVLSkKz3CJfY/u\n\tYUdO0vljZhp/FgzzLmD/P3hfNp0kwU6mNcRV6L3172FQ7Bb+pnTdg2sJ0G0KPY+2JCrsxGwO2TQ\n\twtapzvR5TF5YkBiZMVFdLzQ/X9C2NrUR95mMhaOnN75K5MSs7qQsjCR/TK24MJL4QdJQ2F3ki8R\n\tOiQaHTtpdWN7biKY9Rrs5JrAbsOMAAvVP+I0HVGXd2FvftdDXllXsq/2RKWXq4U2viNEWbpxSJf\n\tInf7CLpIus02eMyezhSAkFrBgMXi+96KL9MQWIeNY8uNp8Vdby+WwpeKtneOMnSLH1l/3XRa19e\n\tnIMX51xEh5BVSHoE2JSFDyC5jSzwYDvjIkhSd4Q0ms5/5dQKdVK+ynyVa8EcpP8IXbNWiAFcCV0\n\twZrYpnnxqHXRzj0Yna2qSgvtwSFcaJ/ngPVIitldu5J0zaW/S6y6SV3AKVd9cxhNmzQZSfTItF1\n\tiLKtI1p1iK38AM6BseAA2JVk1I703qKQ4ifmTaXKleAcaOKgPm1Z32/OqmqAqleL4JSs/xBGiPK\n\t/CPV84sqWHDAqul0cLkCSE4OTyIPYFFdXvIRBBXu15+GB/YM7VWkPj7bG6I8e9BAxAWROnxZwe2\n\tJSM8x8GGtLn3w5w==","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":"Port the Mali-C55 LSC algorithm to use libIPA LscAlgorithm.\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/mali-c55/algorithms/lsc.cpp | 156 ++++++++++++++++++++++--------------\n src/ipa/mali-c55/algorithms/lsc.h   |  32 ++++++--\n src/ipa/mali-c55/ipa_context.h      |   3 +\n 3 files changed, 122 insertions(+), 69 deletions(-)","diff":"diff --git a/src/ipa/mali-c55/algorithms/lsc.cpp b/src/ipa/mali-c55/algorithms/lsc.cpp\nindex 097239dcef3e..36b422939e43 100644\n--- a/src/ipa/mali-c55/algorithms/lsc.cpp\n+++ b/src/ipa/mali-c55/algorithms/lsc.cpp\n@@ -9,38 +9,74 @@\n \n #include <libcamera/base/utils.h>\n \n-#include \"libcamera/internal/value_node.h\"\n-\n namespace libcamera {\n \n namespace ipa::mali_c55::algorithms {\n \n LOG_DEFINE_CATEGORY(MaliC55Lsc)\n \n-int Lsc::init([[maybe_unused]] IPAContext &context, const ValueNode &tuningData)\n+/* Gain values in [1, 5] range */\n+static constexpr unsigned int kMeshScale = 6;\n+\n+/* Mali-C55 hw supports configurable mesh sizes; we fix it to 32. */\n+static constexpr unsigned int kMeshSize = 32;\n+static constexpr unsigned int kGridSize = kMeshSize * kMeshSize;\n+\n+/* Per-colour component page offsets in the mesh table. */\n+static constexpr unsigned int kRedOffset = 0;\n+static constexpr unsigned int kGreenOffset = 1024;\n+static constexpr unsigned int kBlueOffset = 2048;\n+\n+/*\n+ * \\todo Clarify if Mali-C55 can support up to 4 colour temperatures.\n+ *\n+ * The uAPI only expose MALI_C55_NUM_MESH_SHADING_ELEMENTS (3072) gain elements,\n+ * which correspond to three pages of 1024 (32x32) entries.\n+ */\n+static constexpr unsigned int kMaxColourTemperatures = 3;\n+\n+/*\n+ * The LSC algorithm implementation only supports 32x32 grids. Create a list of\n+ * positions from the grid size.\n+ */\n+std::vector<double> Lsc::segmentsToPosition() const\n {\n-\tif (!tuningData.contains(\"meshScale\")) {\n-\t\tLOG(MaliC55Lsc, Error) << \"meshScale missing from tuningData\";\n-\t\treturn -EINVAL;\n-\t}\n+\tstd::vector<double> positions(kMeshSize);\n+\tfor (double i = 0.0; i < kMeshSize; ++i)\n+\t\tpositions[i] = i / (kMeshSize - 1);\n \n-\tmeshScale_ = tuningData[\"meshScale\"].get<uint32_t>(0);\n+\treturn positions;\n+}\n \n-\tconst ValueNode &sets = tuningData[\"sets\"];\n-\tif (!sets.isList()) {\n-\t\tLOG(MaliC55Lsc, Error) << \"LSC tables missing or invalid\";\n-\t\treturn -EINVAL;\n-\t}\n+int Lsc::init(IPAContext &context, const ValueNode &tuningData)\n+{\n+\tgridPos_ = segmentsToPosition();\n+\n+\treturn lscAlgo_.init(tuningData, context.ctrlMap, {\n+\t\t\t\t.keys = { \"r\", \"g\", \"b\" },\n+\t\t\t\t.numHCells = kMeshSize,\n+\t\t\t\t.numVCells = kMeshSize,\n+\t\t\t\t.sensorSize = context.sensorInfo.activeAreaSize\n+\t\t\t     });\n+}\n \n-\tsize_t tableSize = 0;\n-\tfor (const auto &set : sets.asList()) {\n-\t\tuint32_t ct = set[\"ct\"].get<uint32_t>(0);\n+int Lsc::configure(IPAContext &context, const IPACameraSensorInfo &configInfo)\n+{\n+\tint ret = lscAlgo_.configure(context.activeState.lsc, configInfo.analogCrop,\n+\t\t\t\t     gridPos_, gridPos_);\n+\tif (ret)\n+\t\treturn ret;\n \n-\t\tif (!ct) {\n-\t\t\tLOG(MaliC55Lsc, Error) << \"Invalid colour temperature\";\n-\t\t\treturn -EINVAL;\n-\t\t}\n+\t/* Re-initialize the mesh tables and reserve space for enough entries. */\n+\tmesh_ = std::vector<uint32_t>(kGridSize * kMaxColourTemperatures);\n+\tcolourTemperatures_.clear();\n \n+\t/*\n+\t * Get the lsc tables per colour components and populate mesh_ with\n+\t * their content.\n+\t */\n+\tconst auto &components = lscAlgo_.getComponents();\n+\tfor (auto const &[ct, component] : components) {\n \t\tif (std::count(colourTemperatures_.begin(),\n \t\t\t       colourTemperatures_.end(), ct)) {\n \t\t\tLOG(MaliC55Lsc, Error)\n@@ -48,44 +84,29 @@ int Lsc::init([[maybe_unused]] IPAContext &context, const ValueNode &tuningData)\n \t\t\treturn -EINVAL;\n \t\t}\n \n-\t\tstd::vector<uint8_t> rTable =\n-\t\t\tset[\"r\"].get<std::vector<uint8_t>>().value_or(utils::defopt);\n-\t\tstd::vector<uint8_t> gTable =\n-\t\t\tset[\"g\"].get<std::vector<uint8_t>>().value_or(utils::defopt);\n-\t\tstd::vector<uint8_t> bTable =\n-\t\t\tset[\"b\"].get<std::vector<uint8_t>>().value_or(utils::defopt);\n-\n-\t\t/*\n-\t\t * Some validation to do; only 16x16 and 32x32 tables of\n-\t\t * coefficients are acceptable, and all tables across all of the\n-\t\t * sets must be the same size. The first time we encounter a\n-\t\t * table we check that it is an acceptable size and if so make\n-\t\t * sure all other tables are of equal size.\n-\t\t */\n-\t\tif (!tableSize) {\n-\t\t\tif (rTable.size() != 256 && rTable.size() != 1024) {\n-\t\t\t\tLOG(MaliC55Lsc, Error)\n-\t\t\t\t\t<< \"Invalid table size for colour temperature \" << ct;\n-\t\t\t\treturn -EINVAL;\n-\t\t\t}\n-\t\t\ttableSize = rTable.size();\n-\t\t}\n+\t\tstd::vector<uint8_t> rTable = component.at(\"r\");\n+\t\tstd::vector<uint8_t> gTable = component.at(\"g\");\n+\t\tstd::vector<uint8_t> bTable = component.at(\"b\");\n \n-\t\tif (rTable.size() != tableSize ||\n-\t\t    gTable.size() != tableSize ||\n-\t\t    bTable.size() != tableSize) {\n+\t\t/* Only 32x32 tables of coefficients are accepted. */\n+\t\tif (rTable.size() != kGridSize || gTable.size() != kGridSize ||\n+\t\t    bTable.size() != kGridSize) {\n \t\t\tLOG(MaliC55Lsc, Error)\n-\t\t\t\t<< \"Invalid or mismatched table size for colour temperature \" << ct;\n+\t\t\t\t<< \"Invalid table size for colour temperature \" << ct;\n \t\t\treturn -EINVAL;\n \t\t}\n \n-\t\tif (colourTemperatures_.size() >= 3) {\n+\t\tif (colourTemperatures_.size() >= kMaxColourTemperatures) {\n \t\t\tLOG(MaliC55Lsc, Error)\n \t\t\t\t<< \"A maximum of 3 colour temperatures are supported\";\n \t\t\treturn -EINVAL;\n \t\t}\n \n-\t\tfor (unsigned int i = 0; i < tableSize; i++) {\n+\t\t/*\n+\t\t * Create the mesh table entries by assembling up to 3 gains per\n+\t\t * colour temperature in a u32 word.\n+\t\t */\n+\t\tfor (unsigned int i = 0; i < kGridSize; i++) {\n \t\t\tmesh_[kRedOffset + i] |=\n \t\t\t\t(rTable[i] << (colourTemperatures_.size() * 8));\n \t\t\tmesh_[kGreenOffset + i] |=\n@@ -97,35 +118,36 @@ int Lsc::init([[maybe_unused]] IPAContext &context, const ValueNode &tuningData)\n \t\tcolourTemperatures_.push_back(ct);\n \t}\n \n-\t/*\n-\t * The mesh has either 16x16 or 32x32 nodes, we tell the driver which it\n-\t * is based on the number of values in the tuning data's table.\n-\t */\n-\tif (tableSize == 256)\n-\t\tmeshSize_ = 15;\n-\telse\n-\t\tmeshSize_ = 31;\n-\n \treturn 0;\n }\n \n+/**\n+ * \\copydoc libcamera::ipa::Algorithm::queueRequest\n+ */\n+void Lsc::queueRequest(IPAContext &context, [[maybe_unused]] const uint32_t frame,\n+\t\t       IPAFrameContext &frameContext, const ControlList &controls)\n+{\n+\tlscAlgo_.queueRequest(context.activeState.lsc, frameContext.lsc,\n+\t\t\t      controls);\n+}\n+\n void Lsc::fillConfigParamsBlock(MaliC55Params *params) const\n {\n \tauto block = params->block<MaliC55Blocks::MeshShadingConfig>();\n \n \tblock->mesh_show = false;\n-\tblock->mesh_scale = meshScale_;\n+\tblock->mesh_scale = kMeshScale;\n \tblock->mesh_page_r = 0;\n \tblock->mesh_page_g = 1;\n \tblock->mesh_page_b = 2;\n-\tblock->mesh_width = meshSize_;\n-\tblock->mesh_height = meshSize_;\n+\tblock->mesh_width = kMeshSize - 1;\n+\tblock->mesh_height = kMeshSize - 1;\n \n \tstd::copy(mesh_.begin(), mesh_.end(), block->mesh);\n }\n \n void Lsc::fillSelectionParamsBlock(MaliC55Params *params, uint8_t bank,\n-\t\t\t\t     uint8_t alpha) const\n+\t\t\t\t   uint8_t alpha) const\n {\n \tauto block = params->block<MaliC55Blocks::MeshShadingSel>();\n \n@@ -198,6 +220,18 @@ void Lsc::prepare(IPAContext &context, [[maybe_unused]] const uint32_t frame,\n \tfillConfigParamsBlock(params);\n }\n \n+/**\n+ * \\copydoc libcamera::ipa::Algorithm::process\n+ */\n+void Lsc::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 mali_c55_stats_buffer *stats,\n+\t\t  ControlList &metadata)\n+{\n+\tlscAlgo_.process(frameContext.lsc, metadata);\n+}\n+\n REGISTER_IPA_ALGORITHM(Lsc, \"Lsc\")\n \n } /* namespace ipa::mali_c55::algorithms */\ndiff --git a/src/ipa/mali-c55/algorithms/lsc.h b/src/ipa/mali-c55/algorithms/lsc.h\nindex 3d35fd72bfa8..5f752eebaef5 100644\n--- a/src/ipa/mali-c55/algorithms/lsc.h\n+++ b/src/ipa/mali-c55/algorithms/lsc.h\n@@ -5,10 +5,19 @@\n  * Mali-C55 Lens shading correction algorithm\n  */\n \n-#include <map>\n+#include <vector>\n #include <tuple>\n \n+#include <linux/media/arm/mali-c55-config.h>\n+\n+#include \"libcamera/internal/value_node.h\"\n+\n+#include \"libipa/fixedpoint.h\"\n+#include \"libipa/lsc.h\"\n+\n #include \"algorithm.h\"\n+#include \"ipa_context.h\"\n+#include \"params.h\"\n \n namespace libcamera {\n \n@@ -21,23 +30,30 @@ public:\n \t~Lsc() = default;\n \n \tint init(IPAContext &context, const ValueNode &tuningData) override;\n+\tint configure(IPAContext &context, const IPACameraSensorInfo &configInfo) override;\n+\tvoid queueRequest(IPAContext &context, const uint32_t frame,\n+\t\t\t  IPAFrameContext &frameContext,\n+\t\t\t  const ControlList &controls) override;\n \tvoid prepare(IPAContext &context, const uint32_t frame,\n \t\t     IPAFrameContext &frameContext,\n \t\t     MaliC55Params *params) override;\n+\tvoid process(IPAContext &context, const uint32_t frame,\n+\t\t     IPAFrameContext &frameContext,\n+\t\t     const mali_c55_stats_buffer *stats,\n+\t\t     ControlList &metadata) override;\n private:\n-\tstatic constexpr unsigned int kRedOffset = 0;\n-\tstatic constexpr unsigned int kGreenOffset = 1024;\n-\tstatic constexpr unsigned int kBlueOffset = 2048;\n-\n+\tstd::vector<double> segmentsToPosition() const;\n \tvoid fillConfigParamsBlock(MaliC55Params *params) const;\n \tvoid fillSelectionParamsBlock(MaliC55Params *params,\n \t\t\t\t      uint8_t bank, uint8_t alpha) const;\n \tstd::tuple<uint8_t, uint8_t> findBankAndAlpha(uint32_t ct) const;\n \n-\tstd::vector<uint32_t> mesh_ = std::vector<uint32_t>(3072);\n \tstd::vector<uint32_t> colourTemperatures_;\n-\tuint32_t meshScale_;\n-\tuint32_t meshSize_;\n+\tstd::vector<uint32_t> mesh_;\n+\n+\tstd::vector<double> gridPos_;\n+\n+\tLscAlgorithm<UQ<2, 6>> lscAlgo_;\n };\n \n } /* namespace ipa::mali_c55::algorithms */\ndiff --git a/src/ipa/mali-c55/ipa_context.h b/src/ipa/mali-c55/ipa_context.h\nindex 0daa5f8023a2..d924fb7ae7c0 100644\n--- a/src/ipa/mali-c55/ipa_context.h\n+++ b/src/ipa/mali-c55/ipa_context.h\n@@ -20,6 +20,7 @@\n #include \"libipa/awb.h\"\n #include \"libipa/ccm.h\"\n #include \"libipa/fixedpoint.h\"\n+#include \"libipa/lsc.h\"\n \n namespace libcamera {\n \n@@ -61,6 +62,7 @@ struct IPAActiveState {\n \n \tipa::awb::ActiveState awb;\n \tipa::ccm::ActiveState ccm;\n+\tipa::lsc::ActiveState lsc;\n };\n \n struct IPAFrameContext : public FrameContext {\n@@ -72,6 +74,7 @@ struct IPAFrameContext : public FrameContext {\n \n \tipa::awb::FrameContext awb;\n \tipa::ccm::FrameContext ccm;\n+\tipa::lsc::FrameContext lsc;\n };\n \n struct IPAContext {\n","prefixes":["v3","35/35"]}