{"id":23615,"url":"https://patchwork.libcamera.org/api/patches/23615/?format=json","web_url":"https://patchwork.libcamera.org/patch/23615/","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":"<20250620124452.557855-8-naush@raspberrypi.com>","date":"2025-06-20T12:42:28","name":"[v1,7/8] ipa: rpi: controller: AutoFocus bidirectional scanning","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"84eb716f63d5e8bd2b5b9def45e7b4204463941d","submitter":{"id":34,"url":"https://patchwork.libcamera.org/api/people/34/?format=json","name":"Naushir Patuck","email":"naush@raspberrypi.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/23615/mbox/","series":[{"id":5235,"url":"https://patchwork.libcamera.org/api/series/5235/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=5235","date":"2025-06-20T12:42:21","name":"Raspberry Pi: AF improvements","version":1,"mbox":"https://patchwork.libcamera.org/series/5235/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/23615/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/23615/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 D02F9BDE6B\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri, 20 Jun 2025 12:45:17 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id BF30D68E01;\n\tFri, 20 Jun 2025 14:45:16 +0200 (CEST)","from mail-wr1-x432.google.com (mail-wr1-x432.google.com\n\t[IPv6:2a00:1450:4864:20::432])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 4985C68DF0\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 20 Jun 2025 14:45:02 +0200 (CEST)","by mail-wr1-x432.google.com with SMTP id\n\tffacd0b85a97d-3a577f164c8so288300f8f.2\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 20 Jun 2025 05:45:02 -0700 (PDT)","from NAUSH-P-DELL.pitowers.org ([93.93.133.154])\n\tby smtp.gmail.com with ESMTPSA id\n\t5b1f17b1804b1-45361461375sm41561525e9.14.2025.06.20.05.45.00\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tFri, 20 Jun 2025 05:45:01 -0700 (PDT)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (2048-bit key;\n\tunprotected) header.d=raspberrypi.com header.i=@raspberrypi.com\n\theader.b=\"CIVsR3pT\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=raspberrypi.com; s=google; t=1750423502; x=1751028302;\n\tdarn=lists.libcamera.org; \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=KWje5sBJ8QU0mBeCf3VYQD9sRSW4DAtdZgufgu9drJU=;\n\tb=CIVsR3pTTggLjpouEUuzmNwgjZGWZ6AJ3/6idIHYQ4x8Avj0KBW/lqTlz4tiO1f1hH\n\tunfnCl5hTj56uazE077J6O5pvPMnSeNrxSepWIGyAeTDd7PxmVvcbO/sd3fXwAhpuULy\n\tBuMQR8Iku5b5Th2L6XX6IH5xBBi7TWtppbYO0GiGsi0hZnt388eqY1AY+6RndmVPU6Y7\n\tmGlZ37WJhMHPhD7cr5IsGxDfmBPPeFQVctZ38yt007OVXKctiTr3r+shMWFltW1s409f\n\tup6ImGot5FyPfm58pDONIa751pt9AHPcMs9uYp5tFrwV9KS4Lp3GWTY2IWO906SObg8Q\n\tT20g==","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20230601; t=1750423502; x=1751028302;\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=KWje5sBJ8QU0mBeCf3VYQD9sRSW4DAtdZgufgu9drJU=;\n\tb=bMBHi08bZTAJYelDgbOE9MPwg6j4t/AXWLtHPA7UYIgDiPn5/7+Cyplmv0dDWDl0dl\n\tIaF3CvQFvbwdKWQR5riV17xHzTH4lrcqqi5Zth+5kivqioe+Rz1ZKv4FktUphgTo0p+R\n\t3zs8W/HTk9beWT7wRkMfGJdMCXQLcY7/yNbipZRun2N5X70m8elogJrkgoP542A8tmYa\n\t78wWMzy3x6RFkZohUUVAGCGpgPbSVOHFOhqMUwkJZUgeaAVtFMCx+/k5Z79q1DEHizEn\n\tTEKgyS4p+hxKdZ/YMnelpFKtXiqhskU3DFh0hyHXoCVE4+ooAGCNRvw4oyj2YY7xVu6U\n\taOJQ==","X-Gm-Message-State":"AOJu0Yx2NX+yyZiGAiyEMPrhuih0lYNGoTKJr2wzqi1IM4wENYPtw9ge\n\tnKGvjvSPiytrNRUh1zj8Mon36WWu4GzCr30Cn6n0mzsWgyIoEGU9wweJbZDZwDGr0p9gOdlU0sw\n\tcTsbI","X-Gm-Gg":"ASbGncuT6/t83MRBVILeCvrWvOmcP1q/CtJntU7C5dzFctt7mqSKa70ii3llYEmato5\n\t+jzNb2x5jXA0ijk6tFpOBN6YCibfjHo7DBuAUQO0hToVl1h37aNv8GlzDi9/R374YQ7lxs7Uc/d\n\tcyJzVde8khZFr9gD5gGXhso25Nl5SQH9SUuwCN1i1t2SXpQ/PXauWVij7rW2+sMfpxFsPC44yW5\n\tsPsyDVapID+knRG0JFZ1pw07UKnB9ixerS2teqByyObOu7vOPl4a72hLtPi47e5IjYjLBEFhY0o\n\tYQvbs36ZWu84tW4iew4Cm2wnikt4pK0ln1mv21djPgUX8m/rpv6EAv4oOKF0Flb0cUmi0YCpvTT\n\t39I5fBA==","X-Google-Smtp-Source":"AGHT+IEVkEHsgfVFjJkozYT/AL6UaYdLLn8b7+QJL8QWOK+kgxzXG1a0CGqxRAXLtMHmklxlvlw8EA==","X-Received":"by 2002:a05:600c:1d02:b0:450:d4b4:92d0 with SMTP id\n\t5b1f17b1804b1-453653c105emr9273005e9.3.1750423501409; \n\tFri, 20 Jun 2025 05:45:01 -0700 (PDT)","From":"Naushir Patuck <naush@raspberrypi.com>","To":"libcamera-devel@lists.libcamera.org","Cc":"Nick Hollinghurst <nick.hollinghurst@raspberrypi.com>,\n\tNaushir Patuck <naush@raspberrypi.com>","Subject":"[PATCH v1 7/8] ipa: rpi: controller: AutoFocus bidirectional\n\tscanning","Date":"Fri, 20 Jun 2025 13:42:28 +0100","Message-ID":"<20250620124452.557855-8-naush@raspberrypi.com>","X-Mailer":"git-send-email 2.43.0","In-Reply-To":"<20250620124452.557855-1-naush@raspberrypi.com>","References":"<20250620124452.557855-1-naush@raspberrypi.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":"From: Nick Hollinghurst <nick.hollinghurst@raspberrypi.com>\n\nTo reduce unnecessary lens movements, allow the CDAF-based\nsearch procedure to start from either end of the range;\nor if not near an end, from the current lens position.\n\nThis sometimes requires a second coarse scan, if the first\none started in the middle and did not find peak contrast.\n\nShorten the fine scan from 5 steps to 3 steps; allow fine scan\nto be omitted altogether when \"step_fine\": 0 in the tuning file.\n\nMove updateLensPosition() out of startProgrammedScan() to avoid\ncalling it more than once per iteration.\n\nSigned-off-by: Nick Hollinghurst <nick.hollinghurst@raspberrypi.com>\nSigned-off-by: Naushir Patuck <naush@raspberrypi.com>\nReviewed-by: Naushir Patuck <naush@raspberrypi.com>\n---\n src/ipa/rpi/controller/rpi/af.cpp | 97 ++++++++++++++++++++-----------\n src/ipa/rpi/controller/rpi/af.h   |  9 +--\n 2 files changed, 67 insertions(+), 39 deletions(-)","diff":"diff --git a/src/ipa/rpi/controller/rpi/af.cpp b/src/ipa/rpi/controller/rpi/af.cpp\nindex 4396420a0277..eaaca4bc9d7a 100644\n--- a/src/ipa/rpi/controller/rpi/af.cpp\n+++ b/src/ipa/rpi/controller/rpi/af.cpp\n@@ -200,6 +200,7 @@ Af::Af(Controller *controller)\n \t  sceneChangeCount_(0),\n \t  scanMaxContrast_(0.0),\n \t  scanMinContrast_(1.0e9),\n+\t  scanStep_(0.0),\n \t  scanData_(),\n \t  reportState_(AfState::Idle)\n {\n@@ -251,13 +252,14 @@ void Af::switchMode(CameraMode const &cameraMode, [[maybe_unused]] Metadata *met\n \t\t\t  << statsRegion_.height;\n \tinvalidateWeights();\n \n-\tif (scanState_ >= ScanState::Coarse && scanState_ < ScanState::Settle) {\n+\tif (scanState_ >= ScanState::Coarse1 && scanState_ < ScanState::Settle) {\n \t\t/*\n \t\t * If a scan was in progress, re-start it, as CDAF statistics\n \t\t * may have changed. Though if the application is just about\n \t\t * to take a still picture, this will not help...\n \t\t */\n \t\tstartProgrammedScan();\n+\t\tupdateLensPosition();\n \t}\n \tskipCount_ = cfg_.skipFrames;\n }\n@@ -543,31 +545,42 @@ void Af::doScan(double contrast, double phase, double conf)\n \t\tscanMinContrast_ = contrast;\n \tscanData_.emplace_back(ScanRecord{ ftarget_, contrast, phase, conf });\n \n-\tif (scanState_ == ScanState::Coarse) {\n-\t\tif (ftarget_ >= cfg_.ranges[range_].focusMax ||\n-\t\t    contrast < cfg_.speeds[speed_].contrastRatio * scanMaxContrast_) {\n-\t\t\t/*\n-\t\t\t * Finished course scan, or termination based on contrast.\n-\t\t\t * Jump to just after max contrast and start fine scan.\n-\t\t\t */\n-\t\t\tftarget_ = std::min(ftarget_, findPeak(scanMaxIndex_) +\n-\t\t\t\t\t2.0 * cfg_.speeds[speed_].stepFine);\n-\t\t\tscanState_ = ScanState::Fine;\n-\t\t\tscanData_.clear();\n-\t\t} else\n-\t\t\tftarget_ += cfg_.speeds[speed_].stepCoarse;\n-\t} else { /* ScanState::Fine */\n-\t\tif (ftarget_ <= cfg_.ranges[range_].focusMin || scanData_.size() >= 5 ||\n-\t\t    contrast < cfg_.speeds[speed_].contrastRatio * scanMaxContrast_) {\n-\t\t\t/*\n-\t\t\t * Finished fine scan, or termination based on contrast.\n-\t\t\t * Use quadratic peak-finding to find best contrast position.\n-\t\t\t */\n-\t\t\tftarget_ = findPeak(scanMaxIndex_);\n+\tif ((scanStep_ >= 0.0 && ftarget_ >= cfg_.ranges[range_].focusMax) ||\n+\t    (scanStep_ <= 0.0 && ftarget_ <= cfg_.ranges[range_].focusMin) ||\n+\t    (scanState_ == ScanState::Fine && scanData_.size() >= 3) ||\n+\t    contrast < cfg_.speeds[speed_].contrastRatio * scanMaxContrast_) {\n+\t\tdouble pk = findPeak(scanMaxIndex_);\n+\t\t/*\n+\t\t * Finished a scan, by hitting a limit or due to constrast dropping off.\n+\t\t * If this is a first coarse scan and we didn't bracket the peak, reverse!\n+\t\t * If this is a fine scan, or no fine step was defined, we've finished.\n+\t\t * Otherwise, start fine scan in opposite direction.\n+\t\t */\n+\t\tif (scanState_ == ScanState::Coarse1 &&\n+\t\t    scanData_[0].contrast >= cfg_.speeds[speed_].contrastRatio * scanMaxContrast_) {\n+\t\t\tscanStep_ = -scanStep_;\n+\t\t\tscanState_ = ScanState::Coarse2;\n+\t\t} else if (scanState_ == ScanState::Fine || cfg_.speeds[speed_].stepFine <= 0.0) {\n+\t\t\tftarget_ = pk;\n \t\t\tscanState_ = ScanState::Settle;\n-\t\t} else\n-\t\t\tftarget_ -= cfg_.speeds[speed_].stepFine;\n-\t}\n+\t\t} else if (scanState_ == ScanState::Coarse1 &&\n+\t\t\t   scanData_[0].contrast >= cfg_.speeds[speed_].contrastRatio * scanMaxContrast_) {\n+\t\t\tscanStep_ = -scanStep_;\n+\t\t\tscanState_ = ScanState::Coarse2;\n+\t\t} else if (scanStep_ >= 0.0) {\n+\t\t\tftarget_ = std::min(pk + cfg_.speeds[speed_].stepFine,\n+\t\t\t\t\t    cfg_.ranges[range_].focusMax);\n+\t\t\tscanStep_ = -cfg_.speeds[speed_].stepFine;\n+\t\t\tscanState_ = ScanState::Fine;\n+\t\t} else {\n+\t\t\tftarget_ = std::max(pk - cfg_.speeds[speed_].stepFine,\n+\t\t\t\t\t    cfg_.ranges[range_].focusMin);\n+\t\t\tscanStep_ = cfg_.speeds[speed_].stepFine;\n+\t\t\tscanState_ = ScanState::Fine;\n+\t\t}\n+\t\tscanData_.clear();\n+\t} else\n+\t\tftarget_ += scanStep_;\n \n \tstepCount_ = (ftarget_ == fsmooth_) ? 0 : cfg_.speeds[speed_].stepFrames;\n }\n@@ -622,7 +635,7 @@ void Af::doAF(double contrast, double phase, double conf)\n \t\t\t/* else fall through to waiting for a scene change */\n \t\t}\n \t}\n-\tif (scanState_ < ScanState::Coarse && mode_ == AfModeContinuous) {\n+\tif (scanState_ < ScanState::Coarse1 && mode_ == AfModeContinuous) {\n \t\t/*\n \t\t * In CAF mode, not in a scan, and PDAF is unavailable.\n \t\t * Wait for a scene change, followed by stability.\n@@ -642,7 +655,7 @@ void Af::doAF(double contrast, double phase, double conf)\n \t\t\tsceneChangeCount_++;\n \t\tif (sceneChangeCount_ >= cfg_.speeds[speed_].retriggerDelay)\n \t\t\tstartProgrammedScan();\n-\t} else if (scanState_ >= ScanState::Coarse && fsmooth_ == ftarget_) {\n+\t} else if (scanState_ >= ScanState::Coarse1 && fsmooth_ == ftarget_) {\n \t\t/*\n \t\t * CDAF-based scanning sequence.\n \t\t * Allow a delay between steps for CDAF FoM statistics to be\n@@ -714,15 +727,27 @@ void Af::startAF()\n \t\toldSceneContrast_ = 0.0;\n \t\tsceneChangeCount_ = 0;\n \t\treportState_ = AfState::Scanning;\n-\t} else\n+\t} else {\n \t\tstartProgrammedScan();\n+\t\tupdateLensPosition();\n+\t}\n }\n \n void Af::startProgrammedScan()\n {\n-\tftarget_ = cfg_.ranges[range_].focusMin;\n-\tupdateLensPosition();\n-\tscanState_ = ScanState::Coarse;\n+\tif (!initted_ || mode_ != AfModeContinuous ||\n+\t    fsmooth_ <= cfg_.ranges[range_].focusMin + 2.0 * cfg_.speeds[speed_].stepCoarse) {\n+\t\tftarget_ = cfg_.ranges[range_].focusMin;\n+\t\tscanStep_ = cfg_.speeds[speed_].stepCoarse;\n+\t\tscanState_ = ScanState::Coarse2;\n+\t} else if (fsmooth_ >= cfg_.ranges[range_].focusMax - 2.0 * cfg_.speeds[speed_].stepCoarse) {\n+\t\tftarget_ = cfg_.ranges[range_].focusMax;\n+\t\tscanStep_ = -cfg_.speeds[speed_].stepCoarse;\n+\t\tscanState_ = ScanState::Coarse2;\n+\t} else {\n+\t\tscanStep_ = -cfg_.speeds[speed_].stepCoarse;\n+\t\tscanState_ = ScanState::Coarse1;\n+\t}\n \tscanMaxContrast_ = 0.0;\n \tscanMinContrast_ = 1.0e9;\n \tscanMaxIndex_ = 0;\n@@ -785,7 +810,9 @@ void Af::prepare(Metadata *imageMetadata)\n \telse\n \t\tstatus.pauseState = AfPauseState::Running;\n \n-\tif (mode_ == AfModeAuto && scanState_ != ScanState::Idle)\n+\tif (scanState_ == ScanState::Idle)\n+\t\tstatus.state = AfState::Idle;\n+\telse if (mode_ == AfModeAuto)\n \t\tstatus.state = AfState::Scanning;\n \telse\n \t\tstatus.state = reportState_;\n@@ -907,7 +934,7 @@ void Af::setMode(AfAlgorithm::AfMode mode)\n \t\tpauseFlag_ = false;\n \t\tif (mode == AfModeContinuous)\n \t\t\tscanState_ = ScanState::Trigger;\n-\t\telse if (mode != AfModeAuto || scanState_ < ScanState::Coarse)\n+\t\telse if (mode != AfModeAuto || scanState_ < ScanState::Coarse1)\n \t\t\tgoIdle();\n \t}\n }\n@@ -923,11 +950,11 @@ void Af::pause(AfAlgorithm::AfPause pause)\n \tif (mode_ == AfModeContinuous) {\n \t\tif (pause == AfPauseResume && pauseFlag_) {\n \t\t\tpauseFlag_ = false;\n-\t\t\tif (scanState_ < ScanState::Coarse)\n+\t\t\tif (scanState_ < ScanState::Coarse1)\n \t\t\t\tscanState_ = ScanState::Trigger;\n \t\t} else if (pause != AfPauseResume && !pauseFlag_) {\n \t\t\tpauseFlag_ = true;\n-\t\t\tif (pause == AfPauseImmediate || scanState_ < ScanState::Coarse)\n+\t\t\tif (pause == AfPauseImmediate || scanState_ < ScanState::Coarse1)\n \t\t\t\tgoIdle();\n \t\t}\n \t}\ndiff --git a/src/ipa/rpi/controller/rpi/af.h b/src/ipa/rpi/controller/rpi/af.h\nindex e1700f998f29..d35a39d12049 100644\n--- a/src/ipa/rpi/controller/rpi/af.h\n+++ b/src/ipa/rpi/controller/rpi/af.h\n@@ -75,7 +75,8 @@ private:\n \t\tIdle = 0,\n \t\tTrigger,\n \t\tPdaf,\n-\t\tCoarse,\n+\t\tCoarse1,\n+\t\tCoarse2,\n \t\tFine,\n \t\tSettle\n \t};\n@@ -90,8 +91,8 @@ private:\n \t};\n \n \tstruct SpeedDependentParams {\n-\t\tdouble stepCoarse;\t\t/* used for scans */\n-\t\tdouble stepFine;\t\t/* used for scans */\n+\t\tdouble stepCoarse;\t\t/* in dioptres; used for scans */\n+\t\tdouble stepFine;\t\t/* in dioptres; used for scans */\n \t\tdouble contrastRatio;\t\t/* used for scan termination and reporting */\n \t\tdouble retriggerRatio;          /* contrast and RGB ratio for re-triggering */\n \t\tuint32_t retriggerDelay;        /* frames of stability before re-triggering */\n@@ -177,7 +178,7 @@ private:\n \tunsigned sameSignCount_;\n \tunsigned sceneChangeCount_;\n \tunsigned scanMaxIndex_;\n-\tdouble scanMaxContrast_, scanMinContrast_;\n+\tdouble scanMaxContrast_, scanMinContrast_, scanStep_;\n \tstd::vector<ScanRecord> scanData_;\n \tAfState reportState_;\n };\n","prefixes":["v1","7/8"]}