[{"id":26700,"web_url":"https://patchwork.libcamera.org/comment/26700/","msgid":"<20230321144905.xdwv4fij4faori4a@uno.localdomain>","date":"2023-03-21T14:49:05","subject":"Re: [libcamera-devel] [PATCH v4 06/10] ipa: rkisp1: Add AF\n\talgorithm basing on AfHillClimbing","submitter":{"id":143,"url":"https://patchwork.libcamera.org/api/people/143/","name":"Jacopo Mondi","email":"jacopo.mondi@ideasonboard.com"},"content":"Hi Daniel\n\nOn Tue, Mar 14, 2023 at 03:48:30PM +0100, Daniel Semkowicz wrote:\n> Add contrast based auto focus implementation for rkisp1 platform using\n> the common AfHillClimbing algorithm.\n>\n> Rockchip ISP AF block allows calculation of sharpness and luminance\n> in up to three user defined windows. If no windows are set, there are\n> some default settings applied for the first window and exposed through\n> the driver. For each frame, use the sharpness value calculated for this\n> default window and feed the hill climbing algorithm with them. Then set\n> the lens position to value calculated by the algorithm.\n>\n> Signed-off-by: Daniel Semkowicz <dse@thaumatec.com>\n> ---\n>  src/ipa/rkisp1/algorithms/af.cpp      | 107 ++++++++++++++++++++++++++\n>  src/ipa/rkisp1/algorithms/af.h        |  39 ++++++++++\n>  src/ipa/rkisp1/algorithms/meson.build |   1 +\n>  src/ipa/rkisp1/ipa_context.cpp        |  13 ++++\n>  src/ipa/rkisp1/ipa_context.h          |   5 ++\n>  5 files changed, 165 insertions(+)\n>  create mode 100644 src/ipa/rkisp1/algorithms/af.cpp\n>  create mode 100644 src/ipa/rkisp1/algorithms/af.h\n>\n> diff --git a/src/ipa/rkisp1/algorithms/af.cpp b/src/ipa/rkisp1/algorithms/af.cpp\n> new file mode 100644\n> index 00000000..abf5d91f\n> --- /dev/null\n> +++ b/src/ipa/rkisp1/algorithms/af.cpp\n> @@ -0,0 +1,107 @@\n> +/* SPDX-License-Identifier: LGPL-2.1-or-later */\n> +/*\n> + * Copyright (C) 2023, Theobroma Systems\n> + *\n> + * af.cpp - RkISP1 AF hill climbing based control algorithm\n> + */\n> +\n> +#include \"af.h\"\n> +\n> +/**\n> + * \\file af.h\n> + */\n> +\n> +namespace libcamera::ipa::rkisp1::algorithms {\n> +\n> +/**\n> + * \\class Af\n> + * \\brief AF contrast based hill climbing control algorithm for RkISP platforms\n> + *\n> + * Auto focus algorithm for RkISP platforms, basing on the common contrast based\n\ns/basing/based\ns/common constrast based/common\n\n> + * hill climbing auto focus implementation\n> + * (libcamera::ipa::common::algorithms::AfHillClimbing).\n> + *\n> + * This is the platform layer of the algorithm.\n> + *\n> + * Tuning file parameters:\n> + * - **wait-frames-lens:** Number of frames that should be skipped when lens\n> + *   position is changed. Lens movement takes some time and statistics measured\n> + *   during the lens movement are unstable. Currently there is no way to know\n> + *   when lens movement finished and this is a workaround for this. Wait a fixed\n> + *   amount of time on each movement. This parameter should be set according\n> + *   to the worst case  - the number of frames it takes to move lens between\n> + *   limit positions.\n\nI would add a\n\n        \\todo Model the lens delay as number of frames required for\n        the lens position to stabilize in the CameraLens class\n\n> + */\n> +\n> +LOG_DEFINE_CATEGORY(RkISP1Af)\n> +\n> +/**\n> + * \\copydoc libcamera::ipa::Algorithm::init\n> + */\n> +int Af::init([[maybe_unused]] IPAContext &context, const YamlObject &tuningData)\n> +{\n> +\twaitFramesLens_ = tuningData[\"wait-frames-lens\"].get<uint32_t>(1);\n> +\n> +\tLOG(RkISP1Af, Debug) << \"waitFramesLens_: \" << waitFramesLens_;\n> +\n> +\treturn af.init(tuningData);\n> +}\n> +\n> +/**\n> + * \\copydoc libcamera::ipa::Algorithm::configure\n> + */\n> +int Af::configure(IPAContext &context, const IPACameraSensorInfo &configInfo)\n> +{\n> +\tconst auto &lensConfig = context.configuration.lens;\n> +\n> +\taf.configure(lensConfig.minFocusPosition, lensConfig.maxFocusPosition,\n> +\t\t     configInfo.outputSize);\n> +\n> +\t/*\n> +\t* Lens position is unknown at the startup, so initialize\n> +\t* the current position to something out of range.\n> +\t*/\n\nCheck comments along the series\n\n> +\tcontext.activeState.af.lensPosition = lensConfig.maxFocusPosition + 1;\n> +\n> +\treturn 0;\n> +}\n> +\n> +/**\n> + * \\copydoc libcamera::ipa::Algorithm::queueRequest\n> + */\n> +void Af::queueRequest([[maybe_unused]] IPAContext &context,\n> +\t\t      [[maybe_unused]] const uint32_t frame,\n> +\t\t      [[maybe_unused]] IPAFrameContext &frameContext,\n> +\t\t      const ControlList &controls)\n> +{\n> +\taf.queueRequest(controls);\n> +}\n> +\n> +/**\n> + * \\copydoc libcamera::ipa::Algorithm::process\n> + */\n> +void Af::process(IPAContext &context, [[maybe_unused]] const uint32_t frame,\n> +\t\t [[maybe_unused]] IPAFrameContext &frameContext,\n> +\t\t const rkisp1_stat_buffer *stats,\n> +\t\t [[maybe_unused]] ControlList &metadata)\n> +{\n> +\tuint32_t sharpness = stats->params.af.window[0].sum;\n> +\tuint32_t luminance = stats->params.af.window[0].lum;\n> +\n> +\tLOG(RkISP1Af, Debug)\n> +\t\t<< \"lensPosition: \" << context.activeState.af.lensPosition\n> +\t\t<< \", Sharpness: \" << sharpness\n> +\t\t<< \", Luminance: \" << luminance;\n> +\n> +\tint32_t newLensPosition = af.process(sharpness);\n> +\n> +\tif (newLensPosition != context.activeState.af.lensPosition) {\n> +\t\tcontext.activeState.af.lensPosition = newLensPosition;\n> +\t\tcontext.activeState.af.applyLensCtrls = true;\n> +\t\taf.setFramesToSkip(waitFramesLens_);\n> +\t}\n> +}\n> +\n> +REGISTER_IPA_ALGORITHM(Af, \"Af\")\n> +\n> +} /* namespace libcamera::ipa::rkisp1::algorithms */\n> diff --git a/src/ipa/rkisp1/algorithms/af.h b/src/ipa/rkisp1/algorithms/af.h\n> new file mode 100644\n> index 00000000..85de0a64\n> --- /dev/null\n> +++ b/src/ipa/rkisp1/algorithms/af.h\n> @@ -0,0 +1,39 @@\n> +/* SPDX-License-Identifier: LGPL-2.1-or-later */\n> +/*\n> + * Copyright (C) 2023, Theobroma Systems\n> + *\n> + * af.h - RkISP1 AF hill climbing based control algorithm\n> + */\n> +\n> +#pragma once\n> +\n> +#include <linux/rkisp1-config.h>\n> +\n> +#include \"libipa/algorithms/af_hill_climbing.h\"\n> +\n> +#include \"algorithm.h\"\n> +\n> +namespace libcamera::ipa::rkisp1::algorithms {\n> +\n> +class Af : public Algorithm\n> +{\n> +public:\n> +\tint init(IPAContext &context, const YamlObject &tuningData) override;\n> +\tint configure(IPAContext &context,\n> +\t\t      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 process(IPAContext &context, const uint32_t frame,\n> +\t\t     IPAFrameContext &frameContext,\n> +\t\t     const rkisp1_stat_buffer *stats,\n> +\t\t     ControlList &metadata) override;\n> +\n> +private:\n> +\tipa::common::algorithms::AfHillClimbing af;\n> +\n> +\t/* Wait number of frames after changing lens position */\n> +\tuint32_t waitFramesLens_;\n> +};\n> +\n> +} /* namespace libcamera::ipa::rkisp1::algorithms */\n> diff --git a/src/ipa/rkisp1/algorithms/meson.build b/src/ipa/rkisp1/algorithms/meson.build\n> index 93a48329..ab7e44f3 100644\n> --- a/src/ipa/rkisp1/algorithms/meson.build\n> +++ b/src/ipa/rkisp1/algorithms/meson.build\n> @@ -1,6 +1,7 @@\n>  # SPDX-License-Identifier: CC0-1.0\n>\n>  rkisp1_ipa_algorithms = files([\n> +    'af.cpp',\n>      'agc.cpp',\n>      'awb.cpp',\n>      'blc.cpp',\n> diff --git a/src/ipa/rkisp1/ipa_context.cpp b/src/ipa/rkisp1/ipa_context.cpp\n> index 401c098f..5c5f80c7 100644\n> --- a/src/ipa/rkisp1/ipa_context.cpp\n> +++ b/src/ipa/rkisp1/ipa_context.cpp\n> @@ -134,6 +134,19 @@ namespace libcamera::ipa::rkisp1 {\n>   * member may be read by any algorithm, but shall only be written by its owner.\n>   */\n>\n> +/**\n> + * \\var IPAActiveState::af\n> + * \\brief State for the Automatic Focus Control algorithm\n> + *\n> + * \\var IPAActiveState::af.lensPosition\n> + * \\brief Lens position calculated by the AF algorithm\n> + *\n> + * \\var IPAActiveState::af.applyLensCtrls\n> + * \\brief Whether the lens position should be applied\n> + *\n> + * If true, IPA should send new controls to the PH to set new lens position.\n> + */\n> +\n>  /**\n>   * \\var IPAActiveState::agc\n>   * \\brief State for the Automatic Gain Control algorithm\n> diff --git a/src/ipa/rkisp1/ipa_context.h b/src/ipa/rkisp1/ipa_context.h\n> index bfb6e1b7..2c2eec3b 100644\n> --- a/src/ipa/rkisp1/ipa_context.h\n> +++ b/src/ipa/rkisp1/ipa_context.h\n> @@ -58,6 +58,11 @@ struct IPASessionConfiguration {\n>  };\n>\n>  struct IPAActiveState {\n> +\tstruct {\n> +\t\tint32_t lensPosition;\n> +\t\tbool applyLensCtrls;\n> +\t} af;\n> +\n>  \tstruct {\n>  \t\tstruct {\n>  \t\t\tuint32_t exposure;\n> --\n> 2.39.2\n>","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 2CBB9C0F1B\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue, 21 Mar 2023 14:49:12 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 7C590626E3;\n\tTue, 21 Mar 2023 15:49:11 +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 F169C61ED3\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 21 Mar 2023 15:49:09 +0100 (CET)","from ideasonboard.com (host-87-18-61-243.retail.telecomitalia.it\n\t[87.18.61.243])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 2984110B;\n\tTue, 21 Mar 2023 15:49:09 +0100 (CET)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1679410151;\n\tbh=sAb6i1luYzA1bp6oKjErGeCDh/SbohHMgmAYCp7BV2Y=;\n\th=Date:To:References:In-Reply-To:Subject:List-Id:List-Unsubscribe:\n\tList-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc:\n\tFrom;\n\tb=nfK4m5Pp0uP0Qck8It2E3ZHZrJRCcQuF7J2gowNKhmafVxhiTlakXEx5u9i1La87a\n\tffpe8DI2uoi0BYne5JGf2Mx162FpAyqAFBhHb9mg0yHo1Qi8SZFZlEdfWJm124a821\n\tJzHDUV5OvyIXB9xZs4dkKtTjm0BX6e7zJxvKqx3MmziQjc8U+iDw9c/sZV9is4qc9T\n\tbVls2ly/hDZ0up/Nh9BxEVo5UVI5NI51diEXYq0nJlnqzWcgzKu/GY3RP5nYMj9HYc\n\t+PQfbl3U3vbdio9IxNhEzYHbtQorHj6ihSUnnM2a4DL2IduGLp4XsM7kLPX6hCygy0\n\tCOh/15gdhrRww==","v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1679410149;\n\tbh=sAb6i1luYzA1bp6oKjErGeCDh/SbohHMgmAYCp7BV2Y=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=X9qD1B5j5jSJEjs2+kfcfQcp8pidS+Gb3/0I88/+QIJJJ4JVUI8AJ8PKcHvWdZt9H\n\tCoxRrtKhjqLqGCLxi+Jg1/F83mdsYNUd1LO3q7dtSPfibV3KimV6AKx6zDtNofwHTp\n\tMtMgJU0HZDZFuqy9+HMgYftFdlbSpmakH3cZCpoA="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"X9qD1B5j\"; dkim-atps=neutral","Date":"Tue, 21 Mar 2023 15:49:05 +0100","To":"Daniel Semkowicz <dse@thaumatec.com>","Message-ID":"<20230321144905.xdwv4fij4faori4a@uno.localdomain>","References":"<20230314144834.85193-1-dse@thaumatec.com>\n\t<20230314144834.85193-7-dse@thaumatec.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20230314144834.85193-7-dse@thaumatec.com>","Subject":"Re: [libcamera-devel] [PATCH v4 06/10] ipa: rkisp1: Add AF\n\talgorithm basing on AfHillClimbing","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":"Jacopo Mondi via libcamera-devel <libcamera-devel@lists.libcamera.org>","Reply-To":"Jacopo Mondi <jacopo.mondi@ideasonboard.com>","Cc":"jacopo.mondi@ideasonboard.com, libcamera-devel@lists.libcamera.org","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]