{"id":17377,"url":"https://patchwork.libcamera.org/api/1.1/patches/17377/?format=json","web_url":"https://patchwork.libcamera.org/patch/17377/","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":"<20220916103713.21132-4-hpa@redhat.com>","date":"2022-09-16T10:37:13","name":"[libcamera-devel,3/3] ipa: ipu3: af: AfMode and LensPosition control implementation","commit_ref":null,"pull_url":null,"state":"new","archived":false,"hash":"8e810a3c1023c73da9e5287c782ac0af32000051","submitter":{"id":105,"url":"https://patchwork.libcamera.org/api/1.1/people/105/?format=json","name":"Kate Hsuan","email":"hpa@redhat.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/17377/mbox/","series":[{"id":3489,"url":"https://patchwork.libcamera.org/api/1.1/series/3489/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=3489","date":"2022-09-16T10:37:10","name":"Basic AF control for IPU3","version":1,"mbox":"https://patchwork.libcamera.org/series/3489/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/17377/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/17377/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 49D9FC3272\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri, 16 Sep 2022 10:37:49 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id E8CAC6208F;\n\tFri, 16 Sep 2022 12:37:48 +0200 (CEST)","from us-smtp-delivery-124.mimecast.com\n\t(us-smtp-delivery-124.mimecast.com [170.10.133.124])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 60F2761FB7\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 16 Sep 2022 12:37:47 +0200 (CEST)","from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com\n\t[66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS\n\t(version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id\n\tus-mta-312-Z8fWpUbWPeudzNX4Rnhdww-1; Fri, 16 Sep 2022 06:37:45 -0400","from smtp.corp.redhat.com\n\t(int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1])\n\t(using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits))\n\t(No client certificate requested)\n\tby mimecast-mx02.redhat.com (Postfix) with ESMTPS id 497DD101A528\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 16 Sep 2022 10:37:45 +0000 (UTC)","from fedora.redhat.com (unknown [10.39.193.148])\n\tby smtp.corp.redhat.com (Postfix) with ESMTP id 35BA040C83EB;\n\tFri, 16 Sep 2022 10:37:42 +0000 (UTC)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1663324668;\n\tbh=EzhGxGjctKdsR7YJHD/qd9HhfmpwnUT20OnjG62q8UE=;\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=PSV/xpMtFslgr7Nnjrmuxet3eyVk4xhs7sgfpgrKs4bQtIte/lA4dLDmPo4XbqTlP\n\tWTSFMvD9/hfDLVq07bS4DXF666pJJprI+U5gGfINyVUq8NfSZSbF3f/4deAneyzbyx\n\toeVVrUL1hHUcM3DyHxPKhKBu7Vu0qT0JY0/G5gayqk8f7SytNGzeYFS806/zcLEKih\n\tdWn6hvX9THrIB4a47MfAOzYtjhOfac3xG/tH4F4aT+wQ2kcedyyUQduXrAfKFt3lY/\n\tLWGQovGQQ2AUMBWpcnrU+3U/Adj4e49ANHsJ0unw+ggRy6hi9BIj9QWpVE+HQwObNf\n\tgKzgy6so/86YQ==","v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com;\n\ts=mimecast20190719; t=1663324666;\n\th=from:from:reply-to:subject:subject:date:date:message-id:message-id:\n\tto:to:cc:cc:mime-version:mime-version:content-type:content-type:\n\tcontent-transfer-encoding:content-transfer-encoding:\n\tin-reply-to:in-reply-to:references:references;\n\tbh=LHGod8qOBDpSdbsgZjzmaChqt6EtIlH8wVkl3t9neO0=;\n\tb=RaPxFG2XzLcwVnuev1ZrWSSP6UQ9S/MqzsMs7u5w/uGN8JmjRHPwlWycxKwPqHa5EtD+tN\n\tQQfmR570oIM5fYxdCqq4PDTXQA8ikDno79rDN0XEbPv5PeMEQIJXheLT2oOfTaArgz0PVi\n\t02Cc2mnnxQUplKlTzo8TFxhNSIV52AQ="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=redhat.com\n\theader.i=@redhat.com header.b=\"RaPxFG2X\"; \n\tdkim-atps=neutral","X-MC-Unique":"Z8fWpUbWPeudzNX4Rnhdww-1","To":"libcamera devel <libcamera-devel@lists.libcamera.org>,\n\tHans De Goede <hdegoede@redhat.com>","Date":"Fri, 16 Sep 2022 18:37:13 +0800","Message-Id":"<20220916103713.21132-4-hpa@redhat.com>","In-Reply-To":"<20220916103713.21132-1-hpa@redhat.com>","References":"<20220916103713.21132-1-hpa@redhat.com>","MIME-Version":"1.0","X-Scanned-By":"MIMEDefang 3.1 on 10.11.54.1","X-Mimecast-Spam-Score":"0","X-Mimecast-Originator":"redhat.com","Content-Transfer-Encoding":"8bit","Content-Type":"text/plain; charset=\"US-ASCII\"; x-default=true","Subject":"[libcamera-devel] [PATCH 3/3] ipa: ipu3: af: AfMode and\n\tLensPosition control implementation","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":"Kate Hsuan via libcamera-devel <libcamera-devel@lists.libcamera.org>","Reply-To":"Kate Hsuan <hpa@redhat.com>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"AfMode controls include manual, auto, and continuous modes. For auto\nand continuous modes, both of the algorithms try to keep the image\nfocused. The difference between them is that continuous mode has higher\nsensitivity to the change of image variance than auto mode. So, the AF\nis frequently triggered in continuous mode. The user-defined lens\nposition can only be set in manual mode.\n\nSigned-off-by: Kate Hsuan <hpa@redhat.com>\n---\n src/ipa/ipu3/algorithms/af.cpp | 106 ++++++++++++++++++++++++++++-----\n src/ipa/ipu3/algorithms/af.h   |  15 +++++\n 2 files changed, 105 insertions(+), 16 deletions(-)","diff":"diff --git a/src/ipa/ipu3/algorithms/af.cpp b/src/ipa/ipu3/algorithms/af.cpp\nindex 4835a034..c57d3e18 100644\n--- a/src/ipa/ipu3/algorithms/af.cpp\n+++ b/src/ipa/ipu3/algorithms/af.cpp\n@@ -21,6 +21,8 @@\n \n #include <libcamera/base/log.h>\n \n+#include <libcamera/control_ids.h>\n+\n #include <libcamera/ipa/core_ipa_interface.h>\n \n #include \"libipa/histogram.h\"\n@@ -109,7 +111,8 @@ static struct ipu3_uapi_af_filter_config afFilterConfigDefault = {\n  */\n Af::Af()\n \t: focus_(0), bestFocus_(0), currentVariance_(0.0), previousVariance_(0.0),\n-\t  coarseCompleted_(false), fineCompleted_(false)\n+\t  coarseCompleted_(false), fineCompleted_(false), maxChange_(kMaxChange),\n+\t  afMode_(controls::AfModeAuto)\n {\n }\n \n@@ -194,6 +197,69 @@ int Af::configure(IPAContext &context, const IPAConfigInfo &configInfo)\n \treturn 0;\n }\n \n+/**\n+ * \\brief AF controls handler\n+ *\n+ * Put the control parameter to the corresponding variables when receiving the controls\n+ * from the user.\n+ * \\param[in] context The shared IPA context\n+ * \\param[in] frame Frame number\n+ * \\param[in] controls control list of the request\n+ */\n+void Af::queueRequest([[maybe_unused]] IPAContext &context,\n+\t\t      [[maybe_unused]] const uint32_t frame,\n+\t\t      const ControlList &controls)\n+{\n+\tfor (auto const &ctrl : controls) {\n+\t\tunsigned int ctrlEnum = ctrl.first;\n+\t\tconst ControlValue &ctrlValue = ctrl.second;\n+\n+\t\tswitch (ctrlEnum) {\n+\t\tcase controls::AF_MODE:\n+\t\t\tafModeSet(ctrlValue.get<int32_t>());\n+\t\t\tbreak;\n+\t\tcase controls::LENS_POSITION:\n+\t\t\tlensPosition_ = ctrlValue.get<float>();\n+\t\t}\n+\t}\n+}\n+\n+/**\n+ * \\brief AF Mode set\n+ *\n+ * Set AF mode, including manual, auto, and continuous.\n+ *\n+ * \\param[in] AF operation mode 0, 1, 2 are manual, auto, and continuous, respectively.\n+ */\n+\n+void Af::afModeSet(uint32_t mode)\n+{\n+\tswitch (mode) {\n+\tcase controls::AfModeManual:\n+\tcase controls::AfModeAuto:\n+\t\tafMode_ = mode;\n+\t\tbreak;\n+\tcase controls::AfModeContinuous:\n+\t\tafMode_ = mode;\n+\t\tmaxChange_ = 0.05;\n+\t\tbreak;\n+\tdefault:\n+\t\tafMode_ = controls::AfModeAuto;\n+\t}\n+\n+\tLOG(IPU3Af, Debug) << \"AfMode set \" << mode;\n+}\n+\n+/**\n+ * \\brief Set lens position\n+ *\n+ * Set user-defined lens position to the focus steps.\n+ */\n+void Af::afLensPositionSet(IPAContext &context)\n+{\n+\tcontext.activeState.af.focus = kMaxFocusSteps * lensPosition_;\n+}\n+\n /**\n  * \\brief AF coarse scan\n  *\n@@ -397,7 +463,7 @@ bool Af::afIsOutOfFocus(IPAContext context)\n \t\t\t   << \" Current VCM step: \"\n \t\t\t   << context.activeState.af.focus;\n \n-\tif (var_ratio > kMaxChange)\n+\tif (var_ratio > maxChange_)\n \t\treturn true;\n \telse\n \t\treturn false;\n@@ -432,21 +498,29 @@ void Af::process(IPAContext &context, [[maybe_unused]] IPAFrameContext *frameCon\n \tSpan<const y_table_item_t> y_items(reinterpret_cast<const y_table_item_t *>(&stats->af_raw_buffer.y_table),\n \t\t\t\t\t   afRawBufferLen);\n \n-\t/*\n-\t * Calculate the mean and the variance of AF statistics for a given grid.\n-\t * For coarse: y1 are used.\n-\t * For fine: y2 results are used.\n-\t */\n-\tcurrentVariance_ = afEstimateVariance(y_items, !coarseCompleted_);\n+\tswitch (afMode_) {\n+\tcase controls::AfModeManual:\n+\t\tafLensPositionSet(context);\n+\t\tbreak;\n+\tcase controls::AfModeContinuous:\n+\tcase controls::AfModeAuto:\n+\tdefault:\n+\t\t/*\n+\t\t * Calculate the mean and the variance of AF statistics for a given grid.\n+\t\t * For coarse: y1 are used.\n+\t\t * For fine: y2 results are used.\n+\t\t */\n+\t\tcurrentVariance_ = afEstimateVariance(y_items, !coarseCompleted_);\n \n-\tif (!context.activeState.af.stable) {\n-\t\tafCoarseScan(context);\n-\t\tafFineScan(context);\n-\t} else {\n-\t\tif (afIsOutOfFocus(context))\n-\t\t\tafReset(context);\n-\t\telse\n-\t\t\tafIgnoreFrameReset();\n+\t\tif (!context.activeState.af.stable) {\n+\t\t\tafCoarseScan(context);\n+\t\t\tafFineScan(context);\n+\t\t} else {\n+\t\t\tif (afIsOutOfFocus(context))\n+\t\t\t\tafReset(context);\n+\t\t\telse\n+\t\t\t\tafIgnoreFrameReset();\n+\t\t}\n \t}\n }\n \ndiff --git a/src/ipa/ipu3/algorithms/af.h b/src/ipa/ipu3/algorithms/af.h\nindex ccf015f3..77bab2a7 100644\n--- a/src/ipa/ipu3/algorithms/af.h\n+++ b/src/ipa/ipu3/algorithms/af.h\n@@ -26,6 +26,7 @@ class Af : public Algorithm\n \t\tuint16_t y1_avg;\n \t\tuint16_t y2_avg;\n \t} y_table_item_t;\n+\n public:\n \tAf();\n \t~Af() = default;\n@@ -34,6 +35,9 @@ public:\n \tint configure(IPAContext &context, const IPAConfigInfo &configInfo) override;\n \tvoid process(IPAContext &context, IPAFrameContext *frameContext,\n \t\t     const ipu3_uapi_stats_3a *stats) override;\n+\tvoid queueRequest([[maybe_unused]] IPAContext &context,\n+\t\t\t  [[maybe_unused]] const uint32_t frame,\n+\t\t\t  [[maybe_unused]] const ControlList &controls) override;\n \n private:\n \tvoid afCoarseScan(IPAContext &context);\n@@ -46,6 +50,11 @@ private:\n \n \tbool afIsOutOfFocus(IPAContext context);\n \n+\tvoid afModeSet(uint32_t mode);\n+\tvoid afModeGet();\n+\n+\tvoid afLensPositionSet(IPAContext &context);\n+\n \t/* VCM step configuration. It is the current setting of the VCM step. */\n \tuint32_t focus_;\n \t/* The best VCM step. It is a local optimum VCM step during scanning. */\n@@ -62,6 +71,12 @@ private:\n \tbool coarseCompleted_;\n \t/* If the fine scan completes, it is set to true. */\n \tbool fineCompleted_;\n+\t/* Max focus change ratio to determine */\n+\tdouble maxChange_;\n+\t/* Relative lens position in percentage. */\n+\tdouble lensPosition_;\n+\t/* Af operation mode. */\n+\tuint32_t afMode_;\n };\n \n } /* namespace ipa::ipu3::algorithms */\n","prefixes":["libcamera-devel","3/3"]}