Patch Detail
Show a patch.
GET /api/1.1/patches/18447/?format=api
{ "id": 18447, "url": "https://patchwork.libcamera.org/api/1.1/patches/18447/?format=api", "web_url": "https://patchwork.libcamera.org/patch/18447/", "project": { "id": 1, "url": "https://patchwork.libcamera.org/api/1.1/projects/1/?format=api", "name": "libcamera", "link_name": "libcamera", "list_id": "libcamera_core", "list_email": "libcamera-devel@lists.libcamera.org", "web_url": "", "scm_url": "", "webscm_url": "" }, "msgid": "<20230324142908.64224-5-dse@thaumatec.com>", "date": "2023-03-24T14:29:02", "name": "[libcamera-devel,v5,04/10] ipa: Add common contrast based AF implementation", "commit_ref": null, "pull_url": null, "state": "superseded", "archived": false, "hash": "4f2f903be9db6f294999ef9780af0b4c569aee08", "submitter": { "id": 126, "url": "https://patchwork.libcamera.org/api/1.1/people/126/?format=api", "name": "Daniel Semkowicz", "email": "dse@thaumatec.com" }, "delegate": null, "mbox": "https://patchwork.libcamera.org/patch/18447/mbox/", "series": [ { "id": 3813, "url": "https://patchwork.libcamera.org/api/1.1/series/3813/?format=api", "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=3813", "date": "2023-03-24T14:28:58", "name": "ipa: rkisp1: Add autofocus algorithm", "version": 5, "mbox": "https://patchwork.libcamera.org/series/3813/mbox/" } ], "comments": "https://patchwork.libcamera.org/api/patches/18447/comments/", "check": "pending", "checks": "https://patchwork.libcamera.org/api/patches/18447/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 500F3C329C\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri, 24 Mar 2023 14:29:34 +0000 (UTC)", "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id E58736273D;\n\tFri, 24 Mar 2023 15:29:33 +0100 (CET)", "from mail-lf1-x12b.google.com (mail-lf1-x12b.google.com\n\t[IPv6:2a00:1450:4864:20::12b])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 2733A62715\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 24 Mar 2023 15:29:27 +0100 (CET)", "by mail-lf1-x12b.google.com with SMTP id y20so2497437lfj.2\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 24 Mar 2023 07:29:27 -0700 (PDT)", "from localhost.localdomain ([91.90.166.178])\n\tby smtp.gmail.com with ESMTPSA id\n\tj21-20020a2e8515000000b002a10b2ea089sm981714lji.16.2023.03.24.07.29.25\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tFri, 24 Mar 2023 07:29:26 -0700 (PDT)" ], "DKIM-Signature": [ "v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1679668174;\n\tbh=71gherLPKH0KVRevos7fn6CtT5f5q1Lq0vDofBzbnSs=;\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:Cc:\n\tFrom;\n\tb=dIQx955IRStyrIA3fp1kdV91iNaRUw/ekdPNaBC2S0KTbe4Q/3wCajDFz5cIK60DU\n\tyrd6LxcMm/Oi5CbnSH7fswyKyWCq+v3AZR4HecWYfVbRoTpxn7hICAvJX9DR6IvTUz\n\t4mKlpgnvAfXcD/fipZ9oVZLs2rXM5ZOlpNq2TBJ8p2J2yJRO6kcCdsxKluOHhmaBcl\n\tOLzXXbvfl5Aw+BUDCkqeNl85n//B0OCWZvctL400mWE1/E+wgedadgaOhLX+juBLbP\n\te2r5YUtHWYHwIZuAEogTCX8TUsfX1KSbNjOmGKpFhRX1iMBhd0cTHuRoFlw6ygGRsq\n\t8WFGgkM8nSZCQ==", "v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=thaumatec-com.20210112.gappssmtp.com; s=20210112; t=1679668166;\n\th=content-transfer-encoding:mime-version:references:in-reply-to\n\t:message-id:date:subject:cc:to:from:from:to:cc:subject:date\n\t:message-id:reply-to;\n\tbh=aAPkIGXrDpXgztSRfv8F1aKgcAkKp15YtqP6MCql3D8=;\n\tb=UPOVy/zScj27iG6qExLy0bMfsvMoEIa/i2WfcSYYh40Srl9ahuMruHHdZ3KYfWB51g\n\td408uincP0JrEjW9MbnpGqPRxgs4SH/gGISqHBzHq32Cemda9CfmH5xKg705q7kjibbM\n\toxCobMApswA5mRjTNZOUPBeTqB8lrf0omidrrYmfrM0EscgzFCrqQ0GXtpaislnWzX+m\n\tC8yoZ0hMOG4ueafBNOCw2OJ5w/bXnmyoUmJvg9ziAghyCuxDZpuLIEO2G82Q2Pxi83SU\n\tjxmjP3n9YmdLFeXNEj0HtA6Esdf3NB9jb5rYjCQi5Ng4eSlTvpiASCm0aJnvB3UVpyMs\n\tYSkg==" ], "Authentication-Results": "lancelot.ideasonboard.com; dkim=pass (2048-bit key; \n\tunprotected)\n\theader.d=thaumatec-com.20210112.gappssmtp.com\n\theader.i=@thaumatec-com.20210112.gappssmtp.com header.b=\"UPOVy/zS\"; \n\tdkim-atps=neutral", "X-Google-DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20210112; t=1679668166;\n\th=content-transfer-encoding:mime-version:references:in-reply-to\n\t:message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc\n\t:subject:date:message-id:reply-to;\n\tbh=aAPkIGXrDpXgztSRfv8F1aKgcAkKp15YtqP6MCql3D8=;\n\tb=EZZPl8IREVgty4dWV+VU22NopTYJRlsp/MHFC9Te613PgsIvuXpdQyp32SDXuoj/58\n\tyFGzDcTBy+9y0UH98iO7sqoXLtCzAx/NIZB4mKgQvI0mJbgv3jzO4VqRHIQaT4VReLnr\n\tj3ICXfTCvIxm8mxNatIgD5tSRQkP3TiiZQxH8aMOInTJoIFPXZ20/dkyR+3HiuaoFPiz\n\tP+YkXsbTbbP7YAM7T4fudLLaFrJtwtuEtelItz7KhZRNUep1aEvL9Mkl2oSQxXNxiPi1\n\tH8Spp6dYxpMyLTyGWyK/Uh73vQvHr7SyiyciTX1sV9bynOymMEUJbkBhribD0f6iiOzm\n\tc7MQ==", "X-Gm-Message-State": "AAQBX9dzTtcr1bwWqQM/5vzRLUjXVUUOp7rrofU57HOElQS4Ej7Q4xFv\n\t1AJpQqf7V12ykCu7Vg8tCCPYLqR3A30o7s2THWw=", "X-Google-Smtp-Source": "AKy350b0Nc8GFl/2hLxzUKFPMlhu0/zAefTQPyw9K5IpOpIRGXQIyJ9k17uxfxjcx4FETCRl1njmng==", "X-Received": "by 2002:ac2:442a:0:b0:4ea:e68c:91bc with SMTP id\n\tw10-20020ac2442a000000b004eae68c91bcmr820049lfl.18.1679668166323; \n\tFri, 24 Mar 2023 07:29:26 -0700 (PDT)", "To": "libcamera-devel@lists.libcamera.org", "Date": "Fri, 24 Mar 2023 15:29:02 +0100", "Message-Id": "<20230324142908.64224-5-dse@thaumatec.com>", "X-Mailer": "git-send-email 2.39.2", "In-Reply-To": "<20230324142908.64224-1-dse@thaumatec.com>", "References": "<20230324142908.64224-1-dse@thaumatec.com>", "MIME-Version": "1.0", "Content-Transfer-Encoding": "8bit", "Subject": "[libcamera-devel] [PATCH v5 04/10] ipa: Add common contrast based\n\tAF 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": "Daniel Semkowicz via libcamera-devel\n\t<libcamera-devel@lists.libcamera.org>", "Reply-To": "Daniel Semkowicz <dse@thaumatec.com>", "Cc": "jacopo.mondi@ideasonboard.com", "Errors-To": "libcamera-devel-bounces@lists.libcamera.org", "Sender": "\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>" }, "content": "Create a new class with contrast based Auto Focus implementation\nusing hill climbing algorithm. This common implementation is independent\nof platform specific code. This way, each platform can just implement\ncontrast calculation and run the AF control loop basing on this class.\nThis implementation is based on the code that was common for IPU3\nand RPi AF algorithms.\n\nSigned-off-by: Daniel Semkowicz <dse@thaumatec.com>\n---\n .../libipa/algorithms/af_hill_climbing.cpp | 379 ++++++++++++++++++\n src/ipa/libipa/algorithms/af_hill_climbing.h | 91 +++++\n src/ipa/libipa/algorithms/meson.build | 2 +\n 3 files changed, 472 insertions(+)\n create mode 100644 src/ipa/libipa/algorithms/af_hill_climbing.cpp\n create mode 100644 src/ipa/libipa/algorithms/af_hill_climbing.h", "diff": "diff --git a/src/ipa/libipa/algorithms/af_hill_climbing.cpp b/src/ipa/libipa/algorithms/af_hill_climbing.cpp\nnew file mode 100644\nindex 00000000..6ee090eb\n--- /dev/null\n+++ b/src/ipa/libipa/algorithms/af_hill_climbing.cpp\n@@ -0,0 +1,379 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2021, Red Hat\n+ * Copyright (C) 2022, Ideas On Board\n+ * Copyright (C) 2023, Theobroma Systems\n+ *\n+ * af_hill_climbing.cpp - AF contrast based hill climbing common algorithm\n+ */\n+\n+#include \"af_hill_climbing.h\"\n+\n+#include \"libcamera/internal/yaml_parser.h\"\n+\n+/**\n+ * \\file af_hill_climbing.h\n+ * \\brief AF contrast based hill climbing common algorithm\n+ */\n+\n+namespace libcamera {\n+\n+namespace ipa::algorithms {\n+\n+LOG_DEFINE_CATEGORY(Af)\n+\n+/**\n+ * \\class AfHillClimbing\n+ * \\brief Contrast based hill climbing auto focus control algorithm\n+ * implementation\n+ *\n+ * Control part of the auto focus algorithm. It calculates the lens position\n+ * based on the contrast measure supplied by the platform-specific\n+ * implementation. This way it is independent from the platform.\n+ *\n+ * Platform layer that use this algorithm should call process() function\n+ * for each each frame and set the lens to the calculated position.\n+ *\n+ * Focus search is divided into two phases:\n+ * 1. coarse search,\n+ * 2. fine search.\n+ *\n+ * In the first phase, the lens is moved with bigger steps to quickly find\n+ * a rough position for the best focus. Then, based on the outcome of coarse\n+ * search, the second phase is executed. Lens is moved with smaller steps\n+ * in a limited range within the rough position to find the exact position\n+ * for best focus.\n+ *\n+ * Tuning file parameters:\n+ * - **coarse-search-step:** The value by which the lens position will change\n+ * in one step in the *coarse search* phase. Unit is lens specific.\n+ * - **fine-search-step:** The value by which the lens position will change\n+ * in one step in the *fine search* phase. Unit is lens specific.\n+ * - **fine-search-range:** Search range in the *fine search* phase, expressed\n+ * as a percentage of the coarse search result. Valid values are\n+ * in the [0, 100] interval. Value 5 means 5%. If coarse search stopped\n+ * at value 200, the fine search range will be [190, 210].\n+ * - **max-variance-change:** ratio of contrast variance change in the\n+ * *continuous mode* needed for triggering the focus change. When the variance\n+ * change exceeds this value, focus search will be triggered. Valid values are\n+ * in the [0.0, 1.0] interval.\n+ * .\n+ *\n+ * \\todo Search range in the *fine search* phase should depend on the lens\n+ * movement range rather than coarse search result.\n+ * \\todo Implement setRange.\n+ * \\todo Implement setSpeed.\n+ * \\todo Implement setMeteringMode.\n+ * \\todo Implement setWindows.\n+ * \\todo Implement the AfPauseDeferred mode.\n+ */\n+\n+/**\n+ * \\brief Initialize the AfHillClimbing with tuning data\n+ * \\param[in] tuningData The tuning data for the algorithm\n+ *\n+ * This method should be called in the libcamera::ipa::Algorithm::init()\n+ * method of the platform layer.\n+ *\n+ * \\return 0 if successful, an error code otherwise\n+ */\n+int AfHillClimbing::init(const YamlObject &tuningData)\n+{\n+\tcoarseSearchStep_ = tuningData[\"coarse-search-step\"].get<uint32_t>(30);\n+\tfineSearchStep_ = tuningData[\"fine-search-step\"].get<uint32_t>(1);\n+\tfineRange_ = tuningData[\"fine-search-range\"].get<uint32_t>(5);\n+\tfineRange_ /= 100;\n+\tmaxChange_ = tuningData[\"max-variance-change\"].get<double>(0.5);\n+\n+\tLOG(Af, Debug) << \"coarseSearchStep_: \" << coarseSearchStep_\n+\t\t << \", fineSearchStep_: \" << fineSearchStep_\n+\t\t << \", fineRange_: \" << fineRange_\n+\t\t << \", maxChange_: \" << maxChange_;\n+\n+\treturn 0;\n+}\n+\n+/**\n+ * \\brief Configure the AfHillClimbing with sensor and lens information\n+ * \\param[in] minFocusPosition Minimum position supported by camera lens\n+ * \\param[in] maxFocusPosition Maximum position supported by camera lens\n+ *\n+ * This method should be called in the libcamera::ipa::Algorithm::configure()\n+ * method of the platform layer.\n+ */\n+void AfHillClimbing::configure(int32_t minFocusPosition,\n+\t\t\t int32_t maxFocusPosition)\n+{\n+\tminLensPosition_ = minFocusPosition;\n+\tmaxLensPosition_ = maxFocusPosition;\n+}\n+\n+/**\n+ * \\brief Run the auto focus algorithm loop\n+ * \\param[in] currentContrast New value of contrast measured for current frame\n+ *\n+ * This method should be called in the libcamera::ipa::Algorithm::process()\n+ * method of the platform layer for each frame.\n+ *\n+ * Contrast value supplied in the \\p currentContrast parameter can be platform\n+ * specific. The only requirement is the contrast value must increase with\n+ * the increasing image focus. Contrast value must be highest when image is in\n+ * focus.\n+ *\n+ * \\return New lens position calculated by the AF algorithm\n+ */\n+int32_t AfHillClimbing::process(double currentContrast)\n+{\n+\tcurrentContrast_ = currentContrast;\n+\n+\tif (shouldSkipFrame())\n+\t\treturn lensPosition_;\n+\n+\tswitch (mode_) {\n+\tcase controls::AfModeManual:\n+\t\t/* Nothing to process. */\n+\t\tbreak;\n+\tcase controls::AfModeAuto:\n+\t\tprocessAutoMode();\n+\t\tbreak;\n+\tcase controls::AfModeContinuous:\n+\t\tprocessContinuousMode();\n+\t\tbreak;\n+\t}\n+\n+\treturn lensPosition_;\n+}\n+\n+void AfHillClimbing::processAutoMode()\n+{\n+\tif (state_ == controls::AfStateScanning) {\n+\t\tafCoarseScan();\n+\t\tafFineScan();\n+\t}\n+}\n+\n+void AfHillClimbing::processContinuousMode()\n+{\n+\t/* If we are in a paused state, we won't process the stats. */\n+\tif (pauseState_ == controls::AfPauseStatePaused)\n+\t\treturn;\n+\n+\tif (state_ == controls::AfStateScanning) {\n+\t\tafCoarseScan();\n+\t\tafFineScan();\n+\t\treturn;\n+\t}\n+\n+\t/*\n+\t * AF scan can be started at any moment in AfModeContinuous,\n+\t * except when the state is already AfStateScanning.\n+\t */\n+\tif (afIsOutOfFocus())\n+\t\tafReset();\n+}\n+\n+/**\n+ * \\brief Request AF to skip n frames\n+ * \\param[in] n Number of frames to be skipped\n+ *\n+ * For the requested number of frames, the AF calculation will be skipped\n+ * and lens position will not change. The platform layer still needs to\n+ * call process() function for each frame during this time.\n+ * This function can be used by the platform layer if the hardware needs more\n+ * time for some operations.\n+ *\n+ * The number of the requested frames (\\p n) will be applied only if \\p n has\n+ * higher value than the number of frames already requested to be skipped.\n+ * For example, if *setFramesToSkip(5)* was already called for the current\n+ * frame, then calling *setFramesToSkip(3)* will not override the previous\n+ * request and 5 frames will be skipped.\n+ */\n+void AfHillClimbing::setFramesToSkip(uint32_t n)\n+{\n+\tif (n > framesToSkip_)\n+\t\tframesToSkip_ = n;\n+}\n+\n+void AfHillClimbing::setMode(controls::AfModeEnum mode)\n+{\n+\tif (mode == mode_)\n+\t\treturn;\n+\n+\tLOG(Af, Debug) << \"Switched AF mode from \" << mode_ << \" to \" << mode;\n+\tmode_ = mode;\n+\n+\tstate_ = controls::AfStateIdle;\n+\tpauseState_ = controls::AfPauseStateRunning;\n+\n+\tif (mode_ == controls::AfModeContinuous)\n+\t\tafReset();\n+}\n+\n+void AfHillClimbing::setRange([[maybe_unused]] controls::AfRangeEnum range)\n+{\n+\tLOG(Af, Error) << __FUNCTION__ << \" not implemented!\";\n+}\n+\n+void AfHillClimbing::setSpeed([[maybe_unused]] controls::AfSpeedEnum speed)\n+{\n+\tLOG(Af, Error) << __FUNCTION__ << \" not implemented!\";\n+}\n+\n+void AfHillClimbing::setMeteringMode([[maybe_unused]] controls::AfMeteringEnum metering)\n+{\n+\tLOG(Af, Error) << __FUNCTION__ << \" not implemented!\";\n+}\n+\n+void AfHillClimbing::setWindows([[maybe_unused]] Span<const Rectangle> windows)\n+{\n+\tLOG(Af, Error) << __FUNCTION__ << \" not implemented!\";\n+}\n+\n+void AfHillClimbing::setTrigger(controls::AfTriggerEnum trigger)\n+{\n+\tif (mode_ != controls::AfModeAuto) {\n+\t\tLOG(Af, Warning)\n+\t\t\t<< __FUNCTION__ << \" not valid in mode \" << mode_;\n+\t\treturn;\n+\t}\n+\n+\tLOG(Af, Debug) << \"Trigger called with \" << trigger;\n+\n+\tif (trigger == controls::AfTriggerStart)\n+\t\tafReset();\n+\telse\n+\t\tstate_ = controls::AfStateIdle;\n+}\n+\n+void AfHillClimbing::setPause(controls::AfPauseEnum pause)\n+{\n+\tif (mode_ != controls::AfModeContinuous) {\n+\t\tLOG(Af, Warning)\n+\t\t\t<< __FUNCTION__ << \" not valid in mode \" << mode_;\n+\t\treturn;\n+\t}\n+\n+\tswitch (pause) {\n+\tcase controls::AfPauseImmediate:\n+\t\tpauseState_ = controls::AfPauseStatePaused;\n+\t\tbreak;\n+\tcase controls::AfPauseDeferred:\n+\t\tLOG(Af, Warning) << \"AfPauseDeferred is not supported!\";\n+\t\tbreak;\n+\tcase controls::AfPauseResume:\n+\t\tpauseState_ = controls::AfPauseStateRunning;\n+\t\tbreak;\n+\t}\n+}\n+\n+void AfHillClimbing::setLensPosition(float lensPosition)\n+{\n+\tif (mode_ != controls::AfModeManual) {\n+\t\tLOG(Af, Warning)\n+\t\t\t<< __FUNCTION__ << \" not valid in mode \" << mode_;\n+\t\treturn;\n+\t}\n+\n+\tlensPosition_ = static_cast<int32_t>(lensPosition);\n+\n+\tLOG(Af, Debug) << \"Requesting lens position \" << lensPosition_;\n+}\n+\n+void AfHillClimbing::afCoarseScan()\n+{\n+\tif (coarseCompleted_)\n+\t\treturn;\n+\n+\tif (afScan(coarseSearchStep_)) {\n+\t\tcoarseCompleted_ = true;\n+\t\tmaxContrast_ = 0;\n+\t\tconst auto diff = static_cast<int32_t>(\n+\t\t\tstd::abs(lensPosition_) * fineRange_);\n+\t\tlensPosition_ = std::max(lensPosition_ - diff, minLensPosition_);\n+\t\tmaxStep_ = std::min(lensPosition_ + diff, maxLensPosition_);\n+\t}\n+}\n+\n+void AfHillClimbing::afFineScan()\n+{\n+\tif (!coarseCompleted_)\n+\t\treturn;\n+\n+\tif (afScan(fineSearchStep_)) {\n+\t\tLOG(Af, Debug) << \"AF found the best focus position!\";\n+\t\tstate_ = controls::AfStateFocused;\n+\t}\n+}\n+\n+bool AfHillClimbing::afScan(uint32_t steps)\n+{\n+\tif (lensPosition_ + static_cast<int32_t>(steps) > maxStep_) {\n+\t\t/* If the max step is reached, move lens to the position. */\n+\t\tlensPosition_ = bestPosition_;\n+\t\treturn true;\n+\t} else {\n+\t\t/*\n+\t\t * Find the maximum of the variance by estimating its\n+\t\t * derivative. If the direction changes, it means we have passed\n+\t\t * a maximum one step before.\n+\t\t */\n+\t\tif ((currentContrast_ - maxContrast_) >= -(maxContrast_ * 0.1)) {\n+\t\t\t/*\n+\t\t\t * Positive and zero derivative:\n+\t\t\t * The variance is still increasing. The focus could be\n+\t\t\t * increased for the next comparison. Also, the max\n+\t\t\t * variance and previous focus value are updated.\n+\t\t\t */\n+\t\t\tbestPosition_ = lensPosition_;\n+\t\t\tlensPosition_ += static_cast<int32_t>(steps);\n+\t\t\tmaxContrast_ = currentContrast_;\n+\t\t} else {\n+\t\t\t/*\n+\t\t\t * Negative derivative:\n+\t\t\t * The variance starts to decrease which means the maximum\n+\t\t\t * variance is found. Set focus step to previous good one\n+\t\t\t * then return immediately.\n+\t\t\t */\n+\t\t\tlensPosition_ = bestPosition_;\n+\t\t\treturn true;\n+\t\t}\n+\t}\n+\n+\tLOG(Af, Debug) << \"Previous step is \" << bestPosition_\n+\t\t << \", Current step is \" << lensPosition_;\n+\treturn false;\n+}\n+\n+void AfHillClimbing::afReset()\n+{\n+\tLOG(Af, Debug) << \"Reset AF parameters\";\n+\tlensPosition_ = minLensPosition_;\n+\tmaxStep_ = maxLensPosition_;\n+\tstate_ = controls::AfStateScanning;\n+\tcoarseCompleted_ = false;\n+\tmaxContrast_ = 0.0;\n+\tsetFramesToSkip(1);\n+}\n+\n+bool AfHillClimbing::afIsOutOfFocus() const\n+{\n+\tconst double diff_var = std::abs(currentContrast_ - maxContrast_);\n+\tconst double var_ratio = diff_var / maxContrast_;\n+\tLOG(Af, Debug) << \"Variance change rate: \" << var_ratio\n+\t\t << \", Current lens step: \" << lensPosition_;\n+\treturn var_ratio > maxChange_;\n+}\n+\n+bool AfHillClimbing::shouldSkipFrame()\n+{\n+\tif (framesToSkip_ > 0) {\n+\t\tframesToSkip_--;\n+\t\treturn true;\n+\t}\n+\n+\treturn false;\n+}\n+\n+} /* namespace ipa::algorithms */\n+\n+} /* namespace libcamera */\ndiff --git a/src/ipa/libipa/algorithms/af_hill_climbing.h b/src/ipa/libipa/algorithms/af_hill_climbing.h\nnew file mode 100644\nindex 00000000..47d2bbec\n--- /dev/null\n+++ b/src/ipa/libipa/algorithms/af_hill_climbing.h\n@@ -0,0 +1,91 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2021, Red Hat\n+ * Copyright (C) 2022, Ideas On Board\n+ * Copyright (C) 2023, Theobroma Systems\n+ *\n+ * af_hill_climbing.h - AF contrast based hill climbing common algorithm\n+ */\n+\n+#pragma once\n+\n+#include <libcamera/base/log.h>\n+\n+#include \"af.h\"\n+\n+namespace libcamera {\n+\n+class YamlObject;\n+\n+namespace ipa::algorithms {\n+\n+LOG_DECLARE_CATEGORY(Af)\n+\n+class AfHillClimbing : public Af\n+{\n+public:\n+\tint init(const YamlObject &tuningData);\n+\tvoid configure(int32_t minFocusPosition, int32_t maxFocusPosition);\n+\tint32_t process(double currentContrast);\n+\tvoid setFramesToSkip(uint32_t n);\n+\n+\tcontrols::AfStateEnum state() final { return state_; }\n+\tcontrols::AfPauseStateEnum pauseState() final { return pauseState_; }\n+\n+private:\n+\tvoid setMode(controls::AfModeEnum mode) final;\n+\tvoid setRange(controls::AfRangeEnum range) final;\n+\tvoid setSpeed(controls::AfSpeedEnum speed) final;\n+\tvoid setMeteringMode(controls::AfMeteringEnum metering) final;\n+\tvoid setWindows(Span<const Rectangle> windows) final;\n+\tvoid setTrigger(controls::AfTriggerEnum trigger) final;\n+\tvoid setPause(controls::AfPauseEnum pause) final;\n+\tvoid setLensPosition(float lensPosition) final;\n+\n+\tvoid processAutoMode();\n+\tvoid processContinuousMode();\n+\tvoid afCoarseScan();\n+\tvoid afFineScan();\n+\tbool afScan(uint32_t steps);\n+\tvoid afReset();\n+\t[[nodiscard]] bool afIsOutOfFocus() const;\n+\tbool shouldSkipFrame();\n+\n+\tcontrols::AfModeEnum mode_ = controls::AfModeManual;\n+\tcontrols::AfStateEnum state_ = controls::AfStateIdle;\n+\tcontrols::AfPauseStateEnum pauseState_ = controls::AfPauseStateRunning;\n+\n+\t/* Current focus lens position. */\n+\tint32_t lensPosition_ = 0;\n+\t/* Local optimum focus lens position during scanning. */\n+\tint32_t bestPosition_ = 0;\n+\n+\t/* Current AF statistic contrast. */\n+\tdouble currentContrast_ = 0;\n+\t/* It is used to determine the derivative during scanning */\n+\tdouble maxContrast_ = 0;\n+\t/* The designated maximum range of focus scanning. */\n+\tint32_t maxStep_ = 0;\n+\t/* If the coarse scan completes, it is set to true. */\n+\tbool coarseCompleted_ = false;\n+\n+\tuint32_t framesToSkip_ = 0;\n+\n+\t/* Position limits of the focus lens. */\n+\tint32_t minLensPosition_;\n+\tint32_t maxLensPosition_;\n+\n+\t/* Minimum position step for searching appropriate focus. */\n+\tuint32_t coarseSearchStep_;\n+\tuint32_t fineSearchStep_;\n+\n+\t/* Fine scan range 0 < fineRange_ < 1. */\n+\tdouble fineRange_;\n+\n+\t/* Max ratio of variance change, 0.0 < maxChange_ < 1.0. */\n+\tdouble maxChange_;\n+};\n+\n+} /* namespace ipa::algorithms */\n+\n+} /* namespace libcamera */\ndiff --git a/src/ipa/libipa/algorithms/meson.build b/src/ipa/libipa/algorithms/meson.build\nindex 3df4798f..20e437fc 100644\n--- a/src/ipa/libipa/algorithms/meson.build\n+++ b/src/ipa/libipa/algorithms/meson.build\n@@ -2,8 +2,10 @@\n \n libipa_algorithms_headers = files([\n 'af.h',\n+ 'af_hill_climbing.h',\n ])\n \n libipa_algorithms_sources = files([\n 'af.cpp',\n+ 'af_hill_climbing.cpp',\n ])\n", "prefixes": [ "libcamera-devel", "v5", "04/10" ] }