{"id":16898,"url":"https://patchwork.libcamera.org/api/1.1/patches/16898/?format=json","web_url":"https://patchwork.libcamera.org/patch/16898/","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":"<20220802100955.1546-5-laurent.pinchart@ideasonboard.com>","date":"2022-08-02T10:09:55","name":"[libcamera-devel,RFC,4/4] ipa: ipu3: Add YAML tuning file support","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"5e71705db2043368df89381d8898ebcf5c825563","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/1.1/people/2/?format=json","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/16898/mbox/","series":[{"id":3356,"url":"https://patchwork.libcamera.org/api/1.1/series/3356/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=3356","date":"2022-08-02T10:09:51","name":"Add tuning data file support to the IPU3 IPA module","version":1,"mbox":"https://patchwork.libcamera.org/series/3356/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/16898/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/16898/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 0099DC3275\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue,  2 Aug 2022 10:10:07 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id AD8BF63316;\n\tTue,  2 Aug 2022 12:10:07 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 57EBA63319\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue,  2 Aug 2022 12:10:05 +0200 (CEST)","from pendragon.ideasonboard.com (62-78-145-57.bb.dnainternet.fi\n\t[62.78.145.57])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 04FFA593\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue,  2 Aug 2022 12:10:04 +0200 (CEST)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1659435007;\n\tbh=OWPsTPLMftQoLOkE/haPFu6iSNWIM5Ejt0Oi70izDRc=;\n\th=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe:\n\tList-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:\n\tFrom;\n\tb=KE4sLItaYHJia+c/HEC8amHQ6IRHG6BD7YKBCTVHT3zIye58DVl+nUgTT4MhXHf8+\n\tOcvBfn9KghSbalUOP5bFXMgcC/QltuM1TzRdKGVORWbFbZOAiiPkh2Koql5cH0pyDF\n\tiJJ7gL7tbBCNqn3ysByjkxhwOKsNUNW+96luYHf5zwO+HXxAphWxWKY5I21VmLQsJk\n\tn2pra33fJzuDd66JvA4J0jiNIdi1bXm841uPHaUTG0IIi5FqGZ7AdW6AVpqegRUd1m\n\tqX6fWY15P4aw89zU6ZpU0NsghloV0MAIabnZbjLLLUgCDYgrKK2TZfEQZmlh1rOzas\n\tYSIaUzl+0skeg==","v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1659435005;\n\tbh=OWPsTPLMftQoLOkE/haPFu6iSNWIM5Ejt0Oi70izDRc=;\n\th=From:To:Subject:Date:In-Reply-To:References:From;\n\tb=gmoSfx+q39GuIvOKKPH1FQdZrhY7WH9btSYsksXrgbFb7B5IaueWvXsvnCG6Zndne\n\teA4VirFGecjCC47QH4nRPAVVqZV2HlyAxDiwDSSchjzxU7BVTrrJb7JIdNrWTNWZsK\n\t8/DwBDFVm2dCWZxVVFZbwLkg26nTBdXDA0Z9FC3Q="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"gmoSfx+q\"; dkim-atps=neutral","To":"libcamera-devel@lists.libcamera.org","Date":"Tue,  2 Aug 2022 13:09:55 +0300","Message-Id":"<20220802100955.1546-5-laurent.pinchart@ideasonboard.com>","X-Mailer":"git-send-email 2.35.1","In-Reply-To":"<20220802100955.1546-1-laurent.pinchart@ideasonboard.com>","References":"<20220802100955.1546-1-laurent.pinchart@ideasonboard.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [RFC PATCH 4/4] ipa: ipu3: Add YAML tuning file\n\tsupport","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>","From":"Laurent Pinchart via libcamera-devel\n\t<libcamera-devel@lists.libcamera.org>","Reply-To":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"Replace the manual instantiation of algorithms with an automatic\nmechanism based on a tuning data file, provided by the Module base\nclass. This brings the IPU3 IPA module in line with the RkISP1 IPA\nmodule.\n\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\n src/ipa/ipu3/ipu3.cpp | 70 ++++++++++++++++++++++++++++++++-----------\n 1 file changed, 52 insertions(+), 18 deletions(-)","diff":"diff --git a/src/ipa/ipu3/ipu3.cpp b/src/ipa/ipu3/ipu3.cpp\nindex 2f6bb672f7bb..e37b2fa0f30e 100644\n--- a/src/ipa/ipu3/ipu3.cpp\n+++ b/src/ipa/ipu3/ipu3.cpp\n@@ -18,6 +18,7 @@\n #include <linux/intel-ipu3.h>\n #include <linux/v4l2-controls.h>\n \n+#include <libcamera/base/file.h>\n #include <libcamera/base/log.h>\n #include <libcamera/base/utils.h>\n \n@@ -29,6 +30,7 @@\n #include <libcamera/request.h>\n \n #include \"libcamera/internal/mapped_framebuffer.h\"\n+#include \"libcamera/internal/yaml_parser.h\"\n \n #include \"algorithms/af.h\"\n #include \"algorithms/agc.h\"\n@@ -71,7 +73,7 @@ namespace ipa::ipu3 {\n  *\n  * At initialisation time, a CameraSensorHelper is instantiated to support\n  * camera-specific calculations, while the default controls are computed, and\n- * the algorithms are constructed and placed in an ordered list.\n+ * the algorithms are instantiated from the tuning data file.\n  *\n  * The IPU3 ImgU operates with a grid layout to divide the overall frame into\n  * rectangular cells of pixels. When the IPA is configured, we determine the\n@@ -92,12 +94,14 @@ namespace ipa::ipu3 {\n  * fillParamsBuffer() call.\n  *\n  * The individual algorithms are split into modular components that are called\n- * iteratively to allow them to process statistics from the ImgU in a defined\n- * order.\n+ * iteratively to allow them to process statistics from the ImgU in the order\n+ * defined in the tuning data file.\n  *\n- * The current implementation supports three core algorithms:\n- * - Automatic white balance (AWB)\n+ * The current implementation supports five core algorithms:\n+ *\n+ * - Auto focus (AF)\n  * - Automatic gain and exposure control (AGC)\n+ * - Automatic white balance (AWB)\n  * - Black level correction (BLC)\n  * - Tone mapping (Gamma)\n  *\n@@ -128,7 +132,7 @@ namespace ipa::ipu3 {\n  * sensor-specific tuning to adapt for Black Level compensation (BLC), Lens\n  * shading correction (SHD) and Color correction (CCM).\n  */\n-class IPAIPU3 : public IPAIPU3Interface\n+class IPAIPU3 : public IPAIPU3Interface, public Module\n {\n public:\n \tint init(const IPASettings &settings,\n@@ -150,6 +154,10 @@ public:\n \tvoid processStatsBuffer(const uint32_t frame, const int64_t frameTimestamp,\n \t\t\t\tconst uint32_t bufferId,\n \t\t\t\tconst ControlList &sensorControls) override;\n+\n+protected:\n+\tstd::string logPrefix() const override;\n+\n private:\n \tvoid updateControls(const IPACameraSensorInfo &sensorInfo,\n \t\t\t    const ControlInfoMap &sensorControls,\n@@ -171,13 +179,15 @@ private:\n \t/* Interface to the Camera Helper */\n \tstd::unique_ptr<CameraSensorHelper> camHelper_;\n \n-\t/* Maintain the algorithms used by the IPA */\n-\tstd::list<std::unique_ptr<ipa::ipu3::Algorithm>> algorithms_;\n-\n \t/* Local parameter storage */\n \tstruct IPAContext context_;\n };\n \n+std::string IPAIPU3::logPrefix() const\n+{\n+\treturn \"ipu3\";\n+}\n+\n /**\n  * \\brief Compute IPASessionConfiguration using the sensor information and the\n  * sensor V4L2 controls\n@@ -316,12 +326,36 @@ int IPAIPU3::init(const IPASettings &settings,\n \tcontext_.configuration = {};\n \tcontext_.configuration.sensor.lineDuration = sensorInfo.lineLength * 1.0s / sensorInfo.pixelRate;\n \n-\t/* Construct our Algorithms */\n-\talgorithms_.push_back(std::make_unique<algorithms::Af>());\n-\talgorithms_.push_back(std::make_unique<algorithms::Agc>());\n-\talgorithms_.push_back(std::make_unique<algorithms::Awb>());\n-\talgorithms_.push_back(std::make_unique<algorithms::BlackLevelCorrection>());\n-\talgorithms_.push_back(std::make_unique<algorithms::ToneMapping>());\n+\t/* Load the tuning data file. */\n+\tFile file(settings.configurationFile.c_str());\n+\tif (!file.open(File::OpenModeFlag::ReadOnly)) {\n+\t\tint ret = file.error();\n+\t\tLOG(IPAIPU3, Error)\n+\t\t\t<< \"Failed to open configuration file \"\n+\t\t\t<< settings.configurationFile << \": \" << strerror(-ret);\n+\t\treturn ret;\n+\t}\n+\n+\tstd::unique_ptr<libcamera::YamlObject> data = YamlParser::parse(file);\n+\tif (!data)\n+\t\treturn -EINVAL;\n+\n+\tunsigned int version = (*data)[\"version\"].get<uint32_t>(0);\n+\tif (version != 1) {\n+\t\tLOG(IPAIPU3, Error)\n+\t\t\t<< \"Invalid tuning file version \" << version;\n+\t\treturn -EINVAL;\n+\t}\n+\n+\tif (!data->contains(\"algorithms\")) {\n+\t\tLOG(IPAIPU3, Error)\n+\t\t\t<< \"Tuning file doesn't contain any algorithm\";\n+\t\treturn -EINVAL;\n+\t}\n+\n+\tint ret = createAlgorithms(context_, (*data)[\"algorithms\"]);\n+\tif (ret)\n+\t\treturn ret;\n \n \t/* Initialize controls. */\n \tupdateControls(sensorInfo, sensorControls, ipaControls);\n@@ -470,7 +504,7 @@ int IPAIPU3::configure(const IPAConfigInfo &configInfo,\n \t/* Update the IPASessionConfiguration using the sensor settings. */\n \tupdateSessionConfiguration(sensorCtrls_);\n \n-\tfor (auto const &algo : algorithms_) {\n+\tfor (auto const &algo : algorithms()) {\n \t\tint ret = algo->configure(context_, configInfo);\n \t\tif (ret)\n \t\t\treturn ret;\n@@ -538,7 +572,7 @@ void IPAIPU3::fillParamsBuffer(const uint32_t frame, const uint32_t bufferId)\n \t */\n \tparams->use = {};\n \n-\tfor (auto const &algo : algorithms_)\n+\tfor (auto const &algo : algorithms())\n \t\talgo->prepare(context_, params);\n \n \tparamsBufferReady.emit(frame);\n@@ -581,7 +615,7 @@ void IPAIPU3::processStatsBuffer(const uint32_t frame,\n \tint32_t vBlank = context_.configuration.sensor.defVBlank;\n \tControlList ctrls(controls::controls);\n \n-\tfor (auto const &algo : algorithms_)\n+\tfor (auto const &algo : algorithms())\n \t\talgo->process(context_, &frameContext, stats);\n \n \tsetControls(frame);\n","prefixes":["libcamera-devel","RFC","4/4"]}