{"id":21778,"url":"https://patchwork.libcamera.org/api/patches/21778/?format=json","web_url":"https://patchwork.libcamera.org/patch/21778/","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":"<20241030164853.87586-3-jacopo.mondi@ideasonboard.com>","date":"2024-10-30T16:48:52","name":"[v2,2/2] ipa: rkisp1: Have algos initialize FrameContext","commit_ref":null,"pull_url":null,"state":"new","archived":false,"hash":"5049926f66020ba7f5757654e04294a64d25ada3","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/21778/mbox/","series":[{"id":4757,"url":"https://patchwork.libcamera.org/api/series/4757/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=4757","date":"2024-10-30T16:48:50","name":"libipa: Initialize FrameContext with ActiveState","version":2,"mbox":"https://patchwork.libcamera.org/series/4757/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/21778/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/21778/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 2D97EC330B\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed, 30 Oct 2024 16:49:09 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id AF469653B2;\n\tWed, 30 Oct 2024 17:49:05 +0100 (CET)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id D16FA65392\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 30 Oct 2024 17:49:02 +0100 (CET)","from ideasonboard.com (93-61-96-190.ip145.fastwebnet.it\n\t[93.61.96.190])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 5D0FCB7E;\n\tWed, 30 Oct 2024 17:48:59 +0100 (CET)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"dszoHZhc\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1730306939;\n\tbh=ENkjd2vo0r5Yb6A4HNbdTnWQsnT9Jf4U7wNdjEGompo=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=dszoHZhc88DRR1QhyKCMC/qidncj1Eliz/4g8YrxVHL7t2KGW8PO1S1tavmWBVaCb\n\tt9Y4WxEwY/ro77jqJS8FKS1r1ByVjY7A6RGbKG1vAVS7OlMooSpSpi1/AcGOHb636J\n\tqyjvqHnZ6eDqWbNvVuJ876RFZKjb4A4lNej0vdnA=","From":"Jacopo Mondi <jacopo.mondi@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Cc":"Jacopo Mondi <jacopo.mondi@ideasonboard.com>,\n\tStefan Klug <stefan.klug@ideasonboard.com>,\n\tDan Scally <dan.scally@ideasonboard.com>","Subject":"[PATCH v2 2/2] ipa: rkisp1: Have algos initialize FrameContext","Date":"Wed, 30 Oct 2024 17:48:52 +0100","Message-ID":"<20241030164853.87586-3-jacopo.mondi@ideasonboard.com>","X-Mailer":"git-send-email 2.47.0","In-Reply-To":"<20241030164853.87586-1-jacopo.mondi@ideasonboard.com>","References":"<20241030164853.87586-1-jacopo.mondi@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":"The RkISP1 AGC algorithms assumes the metering mode to be\n\"MeteringMatrix\" and pre-fill an array of weights associated\nwith it stored in meteringModes_[MeteringMatrix] when intializing\nthe algorithm in parseMeteringModes().\n\nIt laters fetches the weights when computing parameters using the\nFrameContext.agc.meteringMode as index of the meteringModes_ array.\n\nWhen a Request gets queued to the algorithm, the\nFrameContext.agc.meteringMode index is populated from the algorithm's\nactive state, and optionally updated if the application has specified\na different metering mode.\n\nIn case of Request underrun however, a non-initialized FrameContext\ngets provided to the algorithm, causing an (unhandled) exception when\naccessing the meteringModes_ array.\n\nTo handle the situation when a Request underrun occours and let\nalgorithms initialize a FrameContext to safe defaults:\n\n- Add an 'underrun' flag to FrameContext\n- Add an 'initFrameContext()' function to RkISP1 algorithms\n- If a FrameContext is get() before being allocated, pass it through\n  the algorithms' initFrameContext() to safely initialize it\n\nSigned-off-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>\n---\n src/ipa/libipa/fc_queue.cpp           | 6 ++++++\n src/ipa/libipa/fc_queue.h             | 5 +++++\n src/ipa/rkisp1/algorithms/agc.cpp     | 8 ++++++++\n src/ipa/rkisp1/algorithms/agc.h       | 4 ++++\n src/ipa/rkisp1/algorithms/algorithm.h | 5 +++++\n src/ipa/rkisp1/rkisp1.cpp             | 9 +++++++++\n 6 files changed, 37 insertions(+)","diff":"diff --git a/src/ipa/libipa/fc_queue.cpp b/src/ipa/libipa/fc_queue.cpp\nindex 0365e9197748..44f9d866ad1b 100644\n--- a/src/ipa/libipa/fc_queue.cpp\n+++ b/src/ipa/libipa/fc_queue.cpp\n@@ -36,6 +36,12 @@ namespace ipa {\n  *\n  * \\var FrameContext::frame\n  * \\brief The frame number\n+ *\n+ * \\var FrameContext::underrun\n+ * \\brief Boolean flag that reports if the FrameContext has been accessed with\n+ * a FCQeueu::get() call before being FCQueue::alloc()-ated. If the flag is set\n+ * to True then the frame context needs to be initialized by algorithms to safe\n+ * defaults as it won't be initialized with any non-user provided control.\n  */\n \n /**\ndiff --git a/src/ipa/libipa/fc_queue.h b/src/ipa/libipa/fc_queue.h\nindex a1d136521107..d70ca9601bd7 100644\n--- a/src/ipa/libipa/fc_queue.h\n+++ b/src/ipa/libipa/fc_queue.h\n@@ -22,6 +22,9 @@ template<typename FrameContext>\n class FCQueue;\n \n struct FrameContext {\n+public:\n+\tbool underrun = false;\n+\n private:\n \ttemplate<typename T> friend class FCQueue;\n \tuint32_t frame;\n@@ -97,6 +100,7 @@ public:\n \t\t\t * is called before alloc() by the IPA for frame#0.\n \t\t\t */\n \t\t\tinit(frameContext, frame);\n+\t\t\tframeContext.underrun = true;\n \n \t\t\treturn frameContext;\n \t\t}\n@@ -117,6 +121,7 @@ public:\n \t\t\t<< \"Obtained an uninitialised FrameContext for \" << frame;\n \n \t\tinit(frameContext, frame);\n+\t\tframeContext.underrun = true;\n \n \t\treturn frameContext;\n \t}\ndiff --git a/src/ipa/rkisp1/algorithms/agc.cpp b/src/ipa/rkisp1/algorithms/agc.cpp\nindex 301b7ec26508..4122f665b3ee 100644\n--- a/src/ipa/rkisp1/algorithms/agc.cpp\n+++ b/src/ipa/rkisp1/algorithms/agc.cpp\n@@ -204,6 +204,14 @@ int Agc::configure(IPAContext &context, const IPACameraSensorInfo &configInfo)\n \treturn 0;\n }\n \n+void Agc::initFrameContext(IPAContext &context,\n+\t\t\t   IPAFrameContext &frameContext)\n+{\n+\tauto &agc = context.activeState.agc;\n+\n+\tframeContext.agc.meteringMode = agc.meteringMode;\n+}\n+\n /**\n  * \\copydoc libcamera::ipa::Algorithm::queueRequest\n  */\ndiff --git a/src/ipa/rkisp1/algorithms/agc.h b/src/ipa/rkisp1/algorithms/agc.h\nindex aa86f2c5bc21..c1adf91bbc4e 100644\n--- a/src/ipa/rkisp1/algorithms/agc.h\n+++ b/src/ipa/rkisp1/algorithms/agc.h\n@@ -30,6 +30,10 @@ public:\n \n \tint init(IPAContext &context, const YamlObject &tuningData) override;\n \tint configure(IPAContext &context, const IPACameraSensorInfo &configInfo) override;\n+\n+\tvoid initFrameContext(IPAContext &context,\n+\t\t\t      IPAFrameContext &frameContext) override;\n+\n \tvoid queueRequest(IPAContext &context,\n \t\t\t  const uint32_t frame,\n \t\t\t  IPAFrameContext &frameContext,\ndiff --git a/src/ipa/rkisp1/algorithms/algorithm.h b/src/ipa/rkisp1/algorithms/algorithm.h\nindex 715cfcd8298b..82603b7b372d 100644\n--- a/src/ipa/rkisp1/algorithms/algorithm.h\n+++ b/src/ipa/rkisp1/algorithms/algorithm.h\n@@ -23,6 +23,11 @@ public:\n \t{\n \t}\n \n+\tvirtual void initFrameContext([[maybe_unused]] IPAContext &context,\n+\t\t\t\t      [[maybe_unused]] IPAFrameContext &frameContext)\n+\t{\n+\t}\n+\n \tbool disabled_;\n \tbool supportsRaw_;\n };\ndiff --git a/src/ipa/rkisp1/rkisp1.cpp b/src/ipa/rkisp1/rkisp1.cpp\nindex 9e161cabdea4..b743de9ff6af 100644\n--- a/src/ipa/rkisp1/rkisp1.cpp\n+++ b/src/ipa/rkisp1/rkisp1.cpp\n@@ -353,6 +353,15 @@ void IPARkISP1::processStatsBuffer(const uint32_t frame, const uint32_t bufferId\n {\n \tIPAFrameContext &frameContext = context_.frameContexts.get(frame);\n \n+\tif (frameContext.underrun) {\n+\t\tfor (auto const &a : algorithms()) {\n+\t\t\tAlgorithm *algo = static_cast<Algorithm *>(a.get());\n+\t\t\tif (algo->disabled_)\n+\t\t\t\tcontinue;\n+\t\t\talgo->initFrameContext(context_, frameContext);\n+\t\t}\n+\t}\n+\n \t/*\n \t * In raw capture mode, the ISP is bypassed and no statistics buffer is\n \t * provided.\n","prefixes":["v2","2/2"]}