{"id":27222,"url":"https://patchwork.libcamera.org/api/patches/27222/?format=json","web_url":"https://patchwork.libcamera.org/patch/27222/","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-22-968757b038bb@ideasonboard.com>","date":"2026-07-06T08:01:42","name":"[v3,22/35] ipa: libipa: lsc: Introduce LscAlgorithm","commit_ref":null,"pull_url":null,"state":"new","archived":false,"hash":"e59f19818814e30522fca49b57f2dd77d62c3065","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/27222/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/27222/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/27222/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 B3D3DC3308\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon,  6 Jul 2026 08:02:29 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 52E5E66047;\n\tMon,  6 Jul 2026 10:02:29 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id ACB386601C\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon,  6 Jul 2026 10:02:11 +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 663161CE7;\n\tMon,  6 Jul 2026 10:01:23 +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=\"cWo2d4pE\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1783324883;\n\tbh=e7UIzUv4LL94mHhFMVLNQ03e/6HSMMA854bYuJ+XUL0=;\n\th=From:Date:Subject:References:In-Reply-To:To:Cc:From;\n\tb=cWo2d4pENCpkqe7WLwvZooJ8Ca6WS+ApofcsN1eYPAMtuhwGQcrj0XvtzDPK4JGd0\n\t90SbtHt7fgl7AS0iSTN2NPtMyFO71oxInC8Bb+s8Fxwjyk4V1JjQSeCbU7WjTsrAMe\n\tFeo2qMpW/H2/E+WRIaznCy7wa4sorTiGDbytR/I0=","From":"Jacopo Mondi <jacopo.mondi@ideasonboard.com>","Date":"Mon, 06 Jul 2026 10:01:42 +0200","Subject":"[PATCH v3 22/35] ipa: libipa: lsc: Introduce LscAlgorithm","MIME-Version":"1.0","Content-Type":"text/plain; charset=\"utf-8\"","Content-Transfer-Encoding":"7bit","Message-Id":"<20260706-libipa-algorithms-v3-22-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>","X-Mailer":"b4 0.14.3","X-Developer-Signature":"v=1; a=openpgp-sha256; l=9923;\n\ti=jacopo.mondi@ideasonboard.com; h=from:subject:message-id;\n\tbh=e7UIzUv4LL94mHhFMVLNQ03e/6HSMMA854bYuJ+XUL0=;\n\tb=owEBbQKS/ZANAwAKAXI0Bo8WoVY8AcsmYgBqS2DxG8GCyITVP3LBLK9tJmZllOardmn52ZDcY\n\t+52EC2FGy2JAjMEAAEKAB0WIQS1xD1IgJogio9YOMByNAaPFqFWPAUCaktg8QAKCRByNAaPFqFW\n\tPL+2D/4tdPVUgsGA/JhkO4Q/LzPZo45nCKBAweVHNtYmf7uf5rx2eMTe3Mq5Q2lvD6UPk9k0VRV\n\tFZpTOs+yXd6kStx+lJkGvfxfD4va5uoi9IVhk82lvobMmldRIJ6XMJ/+z3dZLVEVfvmmYpo8T5U\n\t+jBr9ut/YrRTzwC91Pg7FQ97mAztu4paBcUDWubF5+sd8CkWOreN640fBGkYtDTIMDLaZZhdoGR\n\tgFCVrKeOtD/jSMJk/qypLH6u+/GuoUitTpa4eQiof+4UlD1jB3cljX5oNQ2NZMiDsSkwh1q4ndt\n\tLZXf/SqjQSkH6lhjpmSuspgM13KR0W80IJfDevETKvrNaK0uVcC94E93FXahw81wXL888OKO3wq\n\tGFKo3CuIba1X14LV2pZy+Q5dDxHTo7LA/cehNi+D1JqmEWseW2kK3pZ3OaZLCYe79V4MOdXAgyX\n\tdEotZqRZE7ZlBjSUt82kOC3FeO3VFkcxw9mfSN/j6FW7MED+VWH03P1FIx1MKGTM17/U6lyJtf1\n\tdpKZ/H1aMJ1fhGOyiiJPsC7epTvjUYhrcYZ/I6o1TGCRGte8tqnGXfiZlRckOdAhSCadOxr0XXI\n\tZ5+PUFQ49Aem/6+Lbs47NBDjp5FtLrMXENEET9EY//C59BK118iDB4xiryA3FmImFd/F0w5IvWV\n\tMrVNOWmfykrLSCw==","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":"Introduce the LscAlgorithm class that implements the LSC algorithm\nfor libipa.\n\nThe class uses two backends (LscPolynomial and LscTable) to perform\ntuning file parsing and re-scaling of the LSC gains.\n\nSigned-off-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>\n---\n src/ipa/libipa/lsc.cpp | 209 +++++++++++++++++++++++++++++++++++++++++++++++++\n src/ipa/libipa/lsc.h   |  42 ++++++++++\n 2 files changed, 251 insertions(+)","diff":"diff --git a/src/ipa/libipa/lsc.cpp b/src/ipa/libipa/lsc.cpp\nindex b789ae98529f..66d33a2dd796 100644\n--- a/src/ipa/libipa/lsc.cpp\n+++ b/src/ipa/libipa/lsc.cpp\n@@ -7,6 +7,13 @@\n \n #include \"lsc.h\"\n \n+#include <libcamera/base/log.h>\n+\n+#include <libcamera/control_ids.h>\n+\n+#include \"lsc_polynomial.h\"\n+#include \"lsc_table.h\"\n+\n /**\n  * \\file lsc.h\n  * \\brief libipa lsc algorithm\n@@ -14,6 +21,8 @@\n \n namespace libcamera {\n \n+LOG_DEFINE_CATEGORY(Lsc)\n+\n namespace ipa {\n \n namespace lsc {\n@@ -39,6 +48,206 @@ namespace lsc {\n \n } /* namespace lsc */\n \n+/**\n+ * \\class LscAlgorithm\n+ * \\brief libIPA lsc algorithm implementation\n+ *\n+ * Due to the optical characteristics of the lens, the light intensity received\n+ * by the sensor is not uniform. The Lens Shading Correction algorithm applies\n+ * multipliers to all pixels to compensate for the lens shading effect.\n+ *\n+ * The LscAlgorithm implements the libipa Lens Shading Correction algorithm\n+ * using an implementation of the LscImplementation interface.\n+ *\n+ * It provides support for parsing the tuning file content and generates tables\n+ * of per-colour temperature gains that IPA algorithms can use to program their\n+ * lsc engine.\n+ *\n+ * The init() function parses the tuning file and loads the gain tables either\n+ * in tabular form (LscTable) or as radial polynomials (LscPolynomial). The gain\n+ * tables are organized per-colour temperature with per-colour components gain\n+ * vectors or polynomial coefficients.\n+ *\n+ * At LscAlgorithm::configure() time the lsc tables are re-sampled on the\n+ * sensor's crop rectangle in use to adapt them to the configuration in use for\n+ * a streaming session. Polynomial lsc tables support re-sampling and can be\n+ * applied to any sensor configuration. Grid-based lsc tables cannot currently\n+ * be re-sampled and the configuration as parsed from the tuning file is used\n+ * for all sensor configurations providing best-effort results.\n+ *\n+ * \\todo: Implement grid based re-sampling\n+ *\n+ * When the IPA algorithms wants to get access to the (re-sampled) tables to\n+ * program its lsc engine, it uses LscAlgorithm::interpolateComponents() to get\n+ * an lsc table interpolated by the LscAlgorithm class for the specified colour\n+ * temperature. If the algorithm wants to access the non-interpolated tables it\n+ * can retrieve them using LscAlgorithm::getComponents().\n+ */\n+\n+/**\n+ * \\param[in] tuningData The tuning data\n+ * \\param[in] sensorSize The physical sensor size\n+ * \\param[in] controls The IPA list of supported controls\n+ *\n+ * Parse \\a tuningData according to the settings specified in \\a descriptor to\n+ * populate the lsc data and registers lsc controls in \\a controls.\n+ *\n+ * \\return 0 on success, a negative error code otherwise\n+ */\n+int LscAlgorithm::init(const ValueNode &tuningData, const Size &sensorSize,\n+\t\t       ControlInfoMap::Map &controls)\n+{\n+\tpolynomial_ = false;\n+\n+\tstd::string type = tuningData[\"type\"].get<std::string>(\"table\");\n+\tif (type == \"table\") {\n+\t\timpl_ = std::make_unique<LscTable>();\n+\t\tLOG(Lsc, Debug) << \"Using table-based Lsc\";\n+\t} else if (type == \"polynomial\") {\n+\t\t/*\n+\t\t * \\todo: Most likely the reference frame should be native_size.\n+\t\t * Let's wait how the internal discussions progress.\n+\t\t */\n+\t\timpl_ = std::make_unique<LscPolynomial>(sensorSize);\n+\t\tpolynomial_ = true;\n+\t\tLOG(Lsc, Debug) << \"Using polynomial Lsc\";\n+\t} else {\n+\t\tLOG(Lsc, Error) << \"Unsupported Lsc algorithm '\"\n+\t\t\t\t<< type << \"'\";\n+\t\treturn -EINVAL;\n+\t}\n+\n+\tconst ValueNode &yamlSets = tuningData[\"sets\"];\n+\tif (!yamlSets.isList()) {\n+\t\tLOG(Lsc, Error) << \"'sets' parameter not found in tuning file\";\n+\t\treturn -EINVAL;\n+\t}\n+\n+\tint ret = impl_->parseLscData(yamlSets);\n+\tif (ret)\n+\t\treturn ret;\n+\n+\tcontrols[&controls::LensShadingCorrectionEnable] =\n+\t\tControlInfo(false, true, true);\n+\n+\treturn 0;\n+}\n+\n+/**\n+ * \\param[in] state The lsc active state\n+ * \\param[in] analogCrop The current sensor analog crop rectangle\n+ * \\param[in] xPos List of horizontal positions of the LSC grid nodes\n+ * \\param[in] yPos List of vertical positions of the LSC grid nodes\n+ *\n+ * Re-sample the lsc data for an \\a analogCrop.\n+ *\n+ * Lsc tables are generated at tuning time using a known sensor configuration.\n+ * When a new streaming session is started, it might use a different sensor\n+ * configuration for which the lsc tables need to be adjusted to.\n+ *\n+ * This function re-generates the lsc tables to adapt them to a new sensor\n+ * configuration, specifically it re-samples the lsc data for a new \\a\n+ * analogCrop on a grid specified by \\a xPos and \\a yPos. Re-sampling of\n+ * lsc data is currently supported by polynomial-based lsc tables.\n+ *\n+ * \\sa LscImplementation::sampleForCrop\n+ *\n+ * \\return 0 on success, a negative error code otherwise\n+ */\n+int LscAlgorithm::configure(lsc::ActiveState &state, const Rectangle &analogCrop,\n+\t\t\t    const std::vector<double> &xPos,\n+\t\t\t    const std::vector<double> &yPos)\n+{\n+\tLOG(Lsc, Debug) << \"Sample Lsc data for \" << analogCrop;\n+\tlsc::ComponentsMap lscData =\n+\t\timpl_->sampleForCrop(analogCrop, xPos, yPos);\n+\n+\t/*\n+\t * Retain a copy of the components table.\n+\t *\n+\t * We could avoid a copy here if getComponents() could\n+\t * return sets_.data() but I wasn't able to work around the\n+\t * compiler refusing it.\n+\t */\n+\tlscData_ = lscData;\n+\n+\tsets_.setData(std::move(lscData));\n+\tstate.enabled = true;\n+\n+\treturn 0;\n+}\n+\n+/**\n+ * \\brief Queue a request to the lsc algorithm\n+ * \\param[in] state The lsc active state\n+ * \\param[in] context The lsc frame context\n+ * \\param[in] controls The list of controls associated with a Request\n+ *\n+ * Queue a new list of \\a controls to the lsc algorithm.\n+ * The only supported control is controls::LensShadingCorrectionEnable.\n+ */\n+void LscAlgorithm::queueRequest(lsc::ActiveState &state,\n+\t\t\t\tlsc::FrameContext &context,\n+\t\t\t\tconst ControlList &controls)\n+{\n+\tconst auto &lscEnable = controls.get(controls::LensShadingCorrectionEnable);\n+\tif (lscEnable && *lscEnable != state.enabled) {\n+\t\tstate.enabled = *lscEnable;\n+\n+\t\tLOG(Lsc, Debug)\n+\t\t\t<< (state.enabled ? \"Enabling\" : \"Disabling\") << \" Lsc\";\n+\n+\t\tcontext.update = true;\n+\t}\n+\n+\tcontext.enabled = state.enabled;\n+}\n+\n+/**\n+ * \\brief Populate the list of lsc metadata\n+ * \\param[in] context The lsc frame context\n+ * \\param[in] metadata The list of metadata\n+ *\n+ * Populates the list of \\a metadata with controls handled by the LscAlgorithm\n+ * class. The only supported metadata is controls::LensShadingCorrectionEnable.\n+ */\n+void LscAlgorithm::process(lsc::FrameContext &context, ControlList &metadata)\n+{\n+\tmetadata.set(controls::LensShadingCorrectionEnable, context.enabled);\n+}\n+\n+/**\n+ * \\fn LscAlgorithm::interpolateComponents\n+ * \\brief Interpolate the lsc tables for a given colour temperature\n+ * \\param[in] ct The colour temperature\n+ *\n+ * Lsc tables are generated using different colour temperatures during the\n+ * tuning phase.\n+ *\n+ * This function returns the interpolated lsc data for a given \\a ct\n+ * colour temperature.\n+ *\n+ * IPA algorithm can use this function to obtain a list of gains per-colour\n+ * component to program their lsc engines with every time a significant enough\n+ * change in colour temperature is detected.\n+ *\n+ * Calling this function is only valid after LscAlgorithm::configure() has been\n+ * called. An empty components list is returned otherwise.\n+ *\n+ * \\return The lsc gains table interpolated for temperature \\a ct\n+ */\n+\n+/**\n+ * \\fn LscAlgorithm::getComponents\n+ *\n+ * Return the map of lsc data per colour temperature.\n+ *\n+ * Calling this function is only valid after LscAlgorithm::configure() has been\n+ * called. An empty components list is returned otherwise.\n+ *\n+ * \\return The map of lsc gains tables per colour-temperature\n+ */\n+\n } /* namespace ipa */\n \n } /* namespace libcamera */\ndiff --git a/src/ipa/libipa/lsc.h b/src/ipa/libipa/lsc.h\nindex 73519564feff..9a51d32c3464 100644\n--- a/src/ipa/libipa/lsc.h\n+++ b/src/ipa/libipa/lsc.h\n@@ -7,6 +7,17 @@\n \n #pragma once\n \n+#include <memory>\n+#include <vector>\n+\n+#include <libcamera/controls.h>\n+#include <libcamera/geometry.h>\n+\n+#include \"libcamera/internal/value_node.h\"\n+\n+#include \"interpolator.h\"\n+#include \"lsc_base.h\"\n+\n namespace libcamera {\n \n namespace ipa {\n@@ -24,6 +35,37 @@ struct FrameContext {\n \n } /* namespace lsc */\n \n+class LscAlgorithm\n+{\n+public:\n+\tint init(const ValueNode &tuningData, const Size &sensorSize,\n+\t\t ControlInfoMap::Map &controls);\n+\n+\tint configure(lsc::ActiveState &state, const Rectangle &analogCrop,\n+\t\t      const std::vector<double> &xPos,\n+\t\t      const std::vector<double> &yPos);\n+\n+\tvoid queueRequest(lsc::ActiveState &state, lsc::FrameContext &context,\n+\t\t\t  const ControlList &controls);\n+\tvoid process(lsc::FrameContext &context, ControlList &metadata);\n+\n+\tconst lsc::Components interpolateComponents(unsigned int ct)\n+\t{\n+\t\treturn sets_.getInterpolated(ct);\n+\t}\n+\n+\tconst lsc::ComponentsMap getComponents()\n+\t{\n+\t\treturn lscData_;\n+\t}\n+\n+private:\n+\tstd::unique_ptr<LscImplementation> impl_;\n+\tInterpolator<lsc::Components> sets_;\n+\tlsc::ComponentsMap lscData_;\n+\tbool polynomial_;\n+};\n+\n } /* namespace ipa */\n \n } /* namespace libcamera */\n","prefixes":["v3","22/35"]}