{"id":15363,"url":"https://patchwork.libcamera.org/api/1.1/patches/15363/?format=json","web_url":"https://patchwork.libcamera.org/patch/15363/","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":"<20220214095136.25425-1-hpa@redhat.com>","date":"2022-02-14T09:51:36","name":"[libcamera-devel,v8] ipa: ipu3: af: Auto focus for dw9719 Surface Go2 VCM","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"d486480bf1ea9d3c4703a33d3966552f6c4e3620","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/15363/mbox/","series":[{"id":2930,"url":"https://patchwork.libcamera.org/api/1.1/series/2930/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=2930","date":"2022-02-14T09:51:36","name":"[libcamera-devel,v8] ipa: ipu3: af: Auto focus for dw9719 Surface Go2 VCM","version":8,"mbox":"https://patchwork.libcamera.org/series/2930/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/15363/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/15363/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 14C03BE08A\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon, 14 Feb 2022 09:51:58 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 0A97461122;\n\tMon, 14 Feb 2022 10:51:57 +0100 (CET)","from us-smtp-delivery-124.mimecast.com\n\t(us-smtp-delivery-124.mimecast.com [170.10.129.124])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id DBB0C610AD\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 14 Feb 2022 10:51:53 +0100 (CET)","from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com\n\t[209.132.183.4]) 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-493-2VYqGtuQO8OzLo6mvulqJA-1; Mon, 14 Feb 2022 04:51:48 -0500","from smtp.corp.redhat.com\n\t(int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22])\n\t(using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits))\n\t(No client certificate requested)\n\tby mimecast-mx01.redhat.com (Postfix) with ESMTPS id A63D4814245;\n\tMon, 14 Feb 2022 09:51:47 +0000 (UTC)","from fedora.redhat.com (unknown [10.39.193.253])\n\tby smtp.corp.redhat.com (Postfix) with ESMTP id 39EA61077CB7;\n\tMon, 14 Feb 2022 09:51:43 +0000 (UTC)"],"Authentication-Results":["lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (1024-bit key;\n\tunprotected) header.d=redhat.com header.i=@redhat.com\n\theader.b=\"O/dZV9sp\"; dkim-atps=neutral","relay.mimecast.com;\n\tauth=pass smtp.auth=CUSA124A263 smtp.mailfrom=hpa@redhat.com"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com;\n\ts=mimecast20190719; t=1644832312;\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\tbh=B/bzg0rKnPRmZxSPqH683ETVcZS1n5if/sYDxytOcK4=;\n\tb=O/dZV9spVmV/6/hZF8VdC050mDhxzkXXpDBDrph6kHCdD6wScEQREssSJtBRDT3IiKaZo9\n\tFaX9TWavGwpEDEdKdzrYSU7rdEdq8KEvAT5Au9offxZuM4jVy43GCkC9pdzRFyF/BU6BYv\n\t2pLaG/1XwxGzwPRqCe8qWkJs11oRCGA=","X-MC-Unique":"2VYqGtuQO8OzLo6mvulqJA-1","From":"Kate Hsuan <hpa@redhat.com>","To":"libcamera devel <libcamera-devel@lists.libcamera.org>","Date":"Mon, 14 Feb 2022 17:51:36 +0800","Message-Id":"<20220214095136.25425-1-hpa@redhat.com>","MIME-Version":"1.0","X-Scanned-By":"MIMEDefang 2.84 on 10.5.11.22","X-Mimecast-Spam-Score":"0","X-Mimecast-Originator":"redhat.com","Content-Transfer-Encoding":"8bit","Content-Type":"text/plain; charset=\"US-ASCII\"","Subject":"[libcamera-devel] [PATCH v8] ipa: ipu3: af: Auto focus for dw9719\n\tSurface Go2 VCM","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":"Since VCM for surface Go 2 (dw9719) had been successfully\ndriven, this Af module can be used to control the VCM and\ndetermine the focus value based on the IPU3 AF state.\n\nBased on the values from the IPU3 AF buffer, the variance\nof each focus step is determined and a greedy approach is\nused to find the maximum variance of the AF state and an\nappropriate focus value.\n\nThe grid configuration is implemented as a context. Also,\nthe grid parameter- AF_MIN_BLOCK_WIDTH is set to 4 (default\nis 3) since if the default value is used, x_start\n(x_start > 640) will be at an incorrect location of the\nimage (rightmost of the sensor).\n\nSigned-off-by: Kate Hsuan <hpa@redhat.com>\nTested-by: Jean-Michel Hautbois <jeanmichel.hautbois@ideasonboard.com>\n---\nChanges in v8:\n1. Revised and improved the comments.\n2. Simplified the algorithm.\n---\n src/ipa/ipu3/algorithms/af.cpp      | 435 ++++++++++++++++++++++++++++\n src/ipa/ipu3/algorithms/af.h        |  79 +++++\n src/ipa/ipu3/algorithms/meson.build |   1 +\n src/ipa/ipu3/ipa_context.cpp        |  23 ++\n src/ipa/ipu3/ipa_context.h          |  10 +\n src/ipa/ipu3/ipu3.cpp               |   2 +\n 6 files changed, 550 insertions(+)\n create mode 100644 src/ipa/ipu3/algorithms/af.cpp\n create mode 100644 src/ipa/ipu3/algorithms/af.h","diff":"diff --git a/src/ipa/ipu3/algorithms/af.cpp b/src/ipa/ipu3/algorithms/af.cpp\nnew file mode 100644\nindex 00000000..17055c04\n--- /dev/null\n+++ b/src/ipa/ipu3/algorithms/af.cpp\n@@ -0,0 +1,435 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2021, Red Hat\n+ *\n+ * af.cpp - IPU3 auto focus algorithm\n+ */\n+\n+#include \"af.h\"\n+\n+#include <algorithm>\n+#include <chrono>\n+#include <cmath>\n+#include <fcntl.h>\n+#include <numeric>\n+#include <sys/ioctl.h>\n+#include <sys/stat.h>\n+#include <sys/types.h>\n+#include <unistd.h>\n+\n+#include <linux/videodev2.h>\n+\n+#include <libcamera/base/log.h>\n+\n+#include <libcamera/ipa/core_ipa_interface.h>\n+\n+#include \"libipa/histogram.h\"\n+\n+/**\n+ * \\file af.h\n+ */\n+\n+/**\n+ * \\var kAfMinGridWidth\n+ * \\brief the minimum width of AF grid.\n+ * The minimum grid horizontal dimensions.\n+*/\n+\n+/**\n+ * \\var kAfMinGridHeight\n+ * \\brief the minimum height of AF grid.\n+ * The minimum grid vertical dimensions.\n+*/\n+\n+/**\n+ * \\var kAfMaxGridWidth\n+ * \\brief the maximum width of AF grid.\n+ * The maximum grid horizontal dimensions.\n+*/\n+\n+/**\n+ * \\var kAfMaxGridHeight\n+ * \\brief The maximum height of AF grid.\n+ * The maximum grid vertical dimensions.\n+*/\n+\n+/**\n+ * \\var kAfMinGridBlockWidth\n+ * \\brief The minimum block size of the width.\n+ * The minimum value of Log2 of the width of the grid cell.\n+ */\n+\n+/**\n+ * \\var kAfMinGridBlockHeight\n+ * \\brief The minimum block size of the height.\n+ * The minimum value of Log2 of the height of the grid cell.\n+ */\n+\n+/**\n+ * \\def kAfMaxGridBlockWidth\n+ * \\brief The maximum block size of the width.\n+ * The maximum value of Log2 of the width of the grid cell.\n+ */\n+\n+/**\n+ * \\var kAfMaxGridBlockHeight\n+ * \\brief The maximum block size of the height.\n+ * The maximum value of Log2 of the height of the grid cell.\n+ */\n+\n+/**\n+ * \\var kAfDefaultHeightPerSlice\n+ * \\brief The default number of blocks in vertical axis per slice.\n+ * The number of blocks in vertical axis per slice.\n+ */\n+\n+namespace libcamera {\n+\n+using namespace std::literals::chrono_literals;\n+\n+namespace ipa::ipu3::algorithms {\n+\n+LOG_DEFINE_CATEGORY(IPU3Af)\n+\n+/**\n+ * Maximum focus steps of the VCM control\n+ * \\todo should be obtained from the VCM driver\n+ */\n+static constexpr uint32_t kMaxFocusSteps = 1023;\n+\n+/* Minimum focus step for searching appropriate focus */\n+static constexpr uint32_t kCoarseSearchStep = 30;\n+static constexpr uint32_t kFineSearchStep = 1;\n+\n+/* Max ratio of variance change, 0.0 < kMaxChange < 1.0 */\n+static constexpr double kMaxChange = 0.5;\n+\n+/* The numbers of frame to be ignored, before performing focus scan. */\n+static constexpr uint32_t kIgnoreFrame = 10;\n+\n+/* Fine scan range 0 < kFineRange < 1 */\n+static constexpr double kFineRange = 0.05;\n+\n+/* Settings for IPU3 AF filter */\n+static struct ipu3_uapi_af_filter_config afFilterConfigDefault = {\n+\t.y1_coeff_0 = { 0, 1, 3, 7 },\n+\t.y1_coeff_1 = { 11, 13, 1, 2 },\n+\t.y1_coeff_2 = { 8, 19, 34, 242 },\n+\t.y1_sign_vec = 0x7fdffbfe,\n+\t.y2_coeff_0 = { 0, 1, 6, 6 },\n+\t.y2_coeff_1 = { 13, 25, 3, 0 },\n+\t.y2_coeff_2 = { 25, 3, 177, 254 },\n+\t.y2_sign_vec = 0x4e53ca72,\n+\t.y_calc = { 8, 8, 8, 8 },\n+\t.nf = { 0, 9, 0, 9, 0 },\n+};\n+\n+/**\n+ * \\class Af\n+ * \\brief An auto-focus algorithm based on IPU3 statistics\n+ * This algorithm is used to determine the position of the lens to make a\n+ * focused image. The IPU3 AF processing block computes the statistics that\n+ * are composed by two types of filtered value and stores in a AF buffer.\n+ * Typically, for a clear image, it has a relatively higher contrast than a\n+ * blurred one. Therefore, if an image with the highest contrast can be\n+ * found through the scan, the position of the len indicates to a clearest\n+ * image.\n+ */\n+Af::Af()\n+\t: focus_(0), bestFocus_(0), currentVariance_(0.0), previousVariance_(0.0),\n+\t  coarseCompleted_(false), fineCompleted_(false)\n+{\n+}\n+\n+/**\n+ * \\copydoc libcamera::ipa::Algorithm::prepare\n+ */\n+void Af::prepare(IPAContext &context, ipu3_uapi_params *params)\n+{\n+\tconst struct ipu3_uapi_grid_config &grid = context.configuration.af.afGrid;\n+\tparams->acc_param.af.grid_cfg = grid;\n+\tparams->acc_param.af.filter_config = afFilterConfigDefault;\n+\n+\t/* Enable AF processing block */\n+\tparams->use.acc_af = 1;\n+}\n+\n+/**\n+ * \\brief Configure the Af given a configInfo\n+ * \\param[in] context The shared IPA context\n+ * \\param[in] configInfo The IPA configuration data\n+ * \\return 0\n+ */\n+int Af::configure(IPAContext &context, const IPAConfigInfo &configInfo)\n+{\n+\tstruct ipu3_uapi_grid_config &grid = context.configuration.af.afGrid;\n+\tgrid.width = kAfMinGridWidth;\n+\tgrid.height = kAfMinGridHeight;\n+\tgrid.block_width_log2 = kAfMinGridBlockWidth;\n+\tgrid.block_height_log2 = kAfMinGridBlockHeight;\n+\tgrid.height_per_slice = kAfDefaultHeightPerSlice;\n+\n+\t/* x_start and y start are default to BDS center */\n+\tgrid.x_start = (configInfo.bdsOutputSize.width / 2) -\n+\t\t       (((grid.width << grid.block_width_log2) / 2));\n+\tgrid.y_start = (configInfo.bdsOutputSize.height / 2) -\n+\t\t       (((grid.height << grid.block_height_log2) / 2));\n+\n+\t/* x_start and y_start should be even */\n+\tgrid.x_start = (grid.x_start / 2) * 2;\n+\tgrid.y_start = (grid.y_start / 2) * 2;\n+\tgrid.y_start = grid.y_start | IPU3_UAPI_GRID_Y_START_EN;\n+\n+\t/* Initial max focus step */\n+\tmaxStep_ = kMaxFocusSteps;\n+\n+\t/* Initial focus value */\n+\tcontext.frameContext.af.focus = 0;\n+\t/* Maximum variance of the AF statistics */\n+\tcontext.frameContext.af.maxVariance = 0;\n+\t/* The stable AF value flag. if it is true, the AF should be in a stable state. */\n+\tcontext.frameContext.af.stable = false;\n+\n+\treturn 0;\n+}\n+\n+/**\n+ * \\brief AF coarse scan\n+ * Find a near focused image using a coarse step. The step is determined by coarseSearchStep.\n+ * \\param[in] context The shared IPA context\n+ */\n+void Af::afCoarseScan(IPAContext &context)\n+{\n+\tif (coarseCompleted_)\n+\t\treturn;\n+\n+\tif (afNeedIgnoreFrame())\n+\t\treturn;\n+\n+\tif (afScan(context, kCoarseSearchStep)) {\n+\t\tcoarseCompleted_ = true;\n+\t\tcontext.frameContext.af.maxVariance = 0;\n+\t\tfocus_ = context.frameContext.af.focus -\n+\t\t\t (context.frameContext.af.focus * kFineRange);\n+\t\tcontext.frameContext.af.focus = focus_;\n+\t\tpreviousVariance_ = 0;\n+\t\tmaxStep_ = std::clamp(focus_ + static_cast<uint32_t>((focus_ * kFineRange)),\n+\t\t\t\t      0U, kMaxFocusSteps);\n+\t}\n+}\n+\n+/**\n+ * \\brief AF fine scan\n+ * Find an optimum lens position with moving 1 step for each search.\n+ * \\param[in] context The shared IPA context\n+ */\n+void Af::afFineScan(IPAContext &context)\n+{\n+\tif (!coarseCompleted_)\n+\t\treturn;\n+\n+\tif (afNeedIgnoreFrame())\n+\t\treturn;\n+\n+\tif (afScan(context, kFineSearchStep)) {\n+\t\tcontext.frameContext.af.stable = true;\n+\t\tfineCompleted_ = true;\n+\t}\n+}\n+\n+/**\n+ * \\brief AF reset\n+ * Reset all the parameters to start over the AF process.\n+ * \\param[in] context The shared IPA context\n+ */\n+void Af::afReset(IPAContext &context)\n+{\n+\tif (afNeedIgnoreFrame())\n+\t\treturn;\n+\n+\tcontext.frameContext.af.maxVariance = 0;\n+\tcontext.frameContext.af.focus = 0;\n+\tfocus_ = 0;\n+\tcontext.frameContext.af.stable = false;\n+\tignoreCounter_ = kIgnoreFrame;\n+\tpreviousVariance_ = 0.0;\n+\tcoarseCompleted_ = false;\n+\tfineCompleted_ = false;\n+\tmaxStep_ = kMaxFocusSteps;\n+}\n+\n+/**\n+ * \\brief AF variance comparison.\n+ * It always picks the largest variance to replace the previous one. The image\n+ * with a larger variance also indicates it is a clearer image than previous\n+ * one. If it finds the negative sign of derivative, it returns immediately.\n+ * \\param[in] context The IPA context\n+ * \\param min_step The VCM movement step.\n+ * \\return True, if it finds a AF value.\n+ */\n+bool Af::afScan(IPAContext &context, int min_step)\n+{\n+\tif (focus_ > maxStep_) {\n+\t\t/* If reach the max step, move lens to the position. */\n+\t\tcontext.frameContext.af.focus = bestFocus_;\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\n+\t\t * passed a maximum one step before.\n+\t\t*/\n+\t\tif ((currentVariance_ - context.frameContext.af.maxVariance) >=\n+\t\t    -(context.frameContext.af.maxVariance * 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 variance\n+\t\t\t * and previous focus value are updated.\n+\t\t\t */\n+\t\t\tbestFocus_ = focus_;\n+\t\t\tfocus_ += min_step;\n+\t\t\tcontext.frameContext.af.focus = focus_;\n+\t\t\tcontext.frameContext.af.maxVariance = currentVariance_;\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\tcontext.frameContext.af.focus = bestFocus_;\n+\t\t\treturn true;\n+\t\t}\n+\t}\n+\n+\tpreviousVariance_ = currentVariance_;\n+\tLOG(IPU3Af, Debug) << \" Previous step is \"\n+\t\t\t   << bestFocus_\n+\t\t\t   << \" Current step is \"\n+\t\t\t   << focus_;\n+\treturn false;\n+}\n+\n+/**\n+ * \\brief Determine the frame to be ignored.\n+ * \\return Return true the frame is ignored.\n+ * \\return Return false the frame should be processed.\n+ */\n+bool Af::afNeedIgnoreFrame()\n+{\n+\tif (ignoreCounter_ == 0)\n+\t\treturn false;\n+\telse\n+\t\tignoreCounter_--;\n+\treturn true;\n+}\n+\n+/**\n+ * \\brief Reset frame ignore counter.\n+ */\n+void Af::afIgnoreFrameReset()\n+{\n+\tignoreCounter_ = kIgnoreFrame;\n+}\n+\n+/**\n+ * \\brief Estemate variance\n+ */\n+double Af::afEstemateVariance(y_table_item_t *y_item, uint32_t len,\n+\t\t\t      bool isY1)\n+{\n+\tuint32_t z = 0;\n+\tuint32_t total = 0;\n+\tdouble mean;\n+\tdouble var_sum = 0;\n+\n+\tfor (z = 0; z < len; z++) {\n+\t\tif (isY1)\n+\t\t\ttotal += y_item[z].y1_avg;\n+\t\telse\n+\t\t\ttotal += y_item[z].y2_avg;\n+\t}\n+\tmean = total / len;\n+\tfor (z = 0; z < len; z++) {\n+\t\tif (isY1)\n+\t\t\tvar_sum += pow((y_item[z].y1_avg - mean), 2);\n+\t\telse\n+\t\t\tvar_sum += pow((y_item[z].y2_avg - mean), 2);\n+\t}\n+\n+\treturn var_sum / static_cast<double>(len);\n+}\n+\n+/**\n+ * \\brief Determine out-of-focus situation.\n+ * Out-of-focus means that the variance change rate for a focused and a new\n+ * variance is greater than a threshold.\n+ * \\param context The IPA context.\n+ * \\return If it is out-of-focus, return true.\n+ * \\return If is is focused, return false.\n+ */\n+bool Af::afIsOutOfFocus(IPAContext context)\n+{\n+\tconst uint32_t diff_var = std::abs(currentVariance_ -\n+\t\t\t\t\t   context.frameContext.af.maxVariance);\n+\tconst double var_ratio = diff_var / context.frameContext.af.maxVariance;\n+\tLOG(IPU3Af, Debug) << \"Variance change rate: \"\n+\t\t\t   << var_ratio\n+\t\t\t   << \" Current VCM step: \"\n+\t\t\t   << context.frameContext.af.focus;\n+\tif (var_ratio > kMaxChange)\n+\t\treturn true;\n+\telse\n+\t\treturn false;\n+}\n+\n+/**\n+ * \\brief Determine the max contrast image and lens position.\n+ * Ideally, a clear image also has a raletively higher contrast. So, every\n+ * images for each focus step should be tested to find a optimal focus step.\n+ * The Hill Climbing Algorithm[1] is used to find the maximum variance of the\n+ * AF statistic which is the AF output of IPU3. The focus step is increased\n+ * then the variance of the AF statistic is estimated. If it finds the negative\n+ * derivative which means we just passed the peak, the best focus is found.\n+ *\n+ * [1] Hill Climbing Algorithm, https://en.wikipedia.org/wiki/Hill_climbing\n+ * \\param[in] context The IPA context.\n+ * \\param[in] stats The statistic buffer of IPU3.\n+ */\n+void Af::process(IPAContext &context, const ipu3_uapi_stats_3a *stats)\n+{\n+\ty_table_item_t y_item[IPU3_UAPI_AF_Y_TABLE_MAX_SIZE / sizeof(y_table_item_t)];\n+\tuint32_t afRawBufferLen;\n+\n+\t/* Evaluate the AF buffer length */\n+\tafRawBufferLen = context.configuration.af.afGrid.width *\n+\t\t\t context.configuration.af.afGrid.height;\n+\n+\tmemcpy(y_item, stats->af_raw_buffer.y_table,\n+\t       afRawBufferLen * sizeof(y_table_item_t));\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+\tif (coarseCompleted_)\n+\t\tcurrentVariance_ = afEstemateVariance(y_item, afRawBufferLen, false);\n+\telse\n+\t\tcurrentVariance_ = afEstemateVariance(y_item, afRawBufferLen, true);\n+\n+\tif (!context.frameContext.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}\n+}\n+\n+} /* namespace ipa::ipu3::algorithms */\n+\n+} /* namespace libcamera */\ndiff --git a/src/ipa/ipu3/algorithms/af.h b/src/ipa/ipu3/algorithms/af.h\nnew file mode 100644\nindex 00000000..2ca78c84\n--- /dev/null\n+++ b/src/ipa/ipu3/algorithms/af.h\n@@ -0,0 +1,79 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2021, Red Hat\n+ *\n+ * af.h - IPU3 Af algorithm\n+ */\n+\n+#pragma once\n+\n+#include <linux/intel-ipu3.h>\n+\n+#include <libcamera/base/utils.h>\n+\n+#include <libcamera/geometry.h>\n+\n+#include \"algorithm.h\"\n+\n+/* Static variables from repo of chromium */\n+static constexpr uint8_t kAfMinGridWidth = 16;\n+static constexpr uint8_t kAfMinGridHeight = 16;\n+static constexpr uint8_t kAfMaxGridWidth = 32;\n+static constexpr uint8_t kAfMaxGridHeight = 24;\n+static constexpr uint16_t kAfMinGridBlockWidth = 4;\n+static constexpr uint16_t kAfMinGridBlockHeight = 3;\n+static constexpr uint16_t kAfMaxGridBlockWidth = 6;\n+static constexpr uint16_t kAfMaxGridBlockHeight = 6;\n+static constexpr uint16_t kAfDefaultHeightPerSlice = 2;\n+\n+namespace libcamera {\n+\n+namespace ipa::ipu3::algorithms {\n+\n+class Af : public Algorithm\n+{\n+\t/* The format of y_table. From ipu3-ipa repo */\n+\ttypedef struct __attribute__((packed)) y_table_item {\n+\t\tuint16_t y1_avg;\n+\t\tuint16_t y2_avg;\n+\t} y_table_item_t;\n+public:\n+\tAf();\n+\t~Af() = default;\n+\n+\tvoid prepare(IPAContext &context, ipu3_uapi_params *params) override;\n+\tint configure(IPAContext &context, const IPAConfigInfo &configInfo) override;\n+\tvoid process(IPAContext &context, const ipu3_uapi_stats_3a *stats) override;\n+\n+private:\n+\tvoid afCoarseScan(IPAContext &context);\n+\tvoid afFineScan(IPAContext &context);\n+\tbool afScan(IPAContext &context, int min_step);\n+\tvoid afReset(IPAContext &context);\n+\tbool afNeedIgnoreFrame();\n+\tvoid afIgnoreFrameReset();\n+\tdouble afEstemateVariance(y_table_item_t *y_item, uint32_t len,\n+\t\t\t\t  bool isY1);\n+\tbool afIsOutOfFocus(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+\tuint32_t bestFocus_;\n+\t/* Current AF statistic variance. */\n+\tdouble currentVariance_;\n+\t/* The frames are ignore before starting measuring. */\n+\tuint32_t ignoreCounter_;\n+\t/* It is used to determine the derivative during scanning */\n+\tdouble previousVariance_;\n+\t/* The designated maximum range of focus scanning. */\n+\tuint32_t maxStep_;\n+\t/* If the coarse scan completes, it is set to true. */\n+\tbool coarseCompleted_;\n+\t/* If the fine scan completes, it is set to true. */\n+\tbool fineCompleted_;\n+};\n+\n+} /* namespace ipa::ipu3::algorithms */\n+\n+} /* namespace libcamera */\ndiff --git a/src/ipa/ipu3/algorithms/meson.build b/src/ipa/ipu3/algorithms/meson.build\nindex 4db6ae1d..b70a551c 100644\n--- a/src/ipa/ipu3/algorithms/meson.build\n+++ b/src/ipa/ipu3/algorithms/meson.build\n@@ -1,6 +1,7 @@\n # SPDX-License-Identifier: CC0-1.0\n \n ipu3_ipa_algorithms = files([\n+    'af.cpp',\n     'agc.cpp',\n     'awb.cpp',\n     'blc.cpp',\ndiff --git a/src/ipa/ipu3/ipa_context.cpp b/src/ipa/ipu3/ipa_context.cpp\nindex 86794ac1..e8f7367c 100644\n--- a/src/ipa/ipu3/ipa_context.cpp\n+++ b/src/ipa/ipu3/ipa_context.cpp\n@@ -69,6 +69,29 @@ namespace libcamera::ipa::ipu3 {\n  * \\brief Number of cells on one line including the ImgU padding\n  */\n \n+/**\n+ * \\var IPASessionConfiguration::af\n+ * \\brief AF grid configuration of the IPA\n+ *\n+ * \\var IPASessionConfiguration::af.afGrid\n+ * \\brief AF scene grid configuration.\n+ */\n+\n+/**\n+ * \\var IPAFrameContext::af\n+ * \\brief Context for the Automatic Focus algorithm\n+ *\n+ * \\struct  IPAFrameContext::af\n+ * \\var IPAFrameContext::af.focus\n+ * \\brief Current position of the lens\n+ *\n+ * \\var IPAFrameContext::af.maxVariance\n+ * \\brief The maximum variance of the current image.\n+ *\n+ * \\var IPAFrameContext::af.stable\n+ * \\brief It is set to true, if the best focus is found.\n+ */\n+\n /**\n  * \\var IPASessionConfiguration::agc\n  * \\brief AGC parameters configuration of the IPA\ndiff --git a/src/ipa/ipu3/ipa_context.h b/src/ipa/ipu3/ipa_context.h\nindex c6dc0814..60ad3194 100644\n--- a/src/ipa/ipu3/ipa_context.h\n+++ b/src/ipa/ipu3/ipa_context.h\n@@ -25,6 +25,10 @@ struct IPASessionConfiguration {\n \t\tuint32_t stride;\n \t} grid;\n \n+\tstruct {\n+\t\tipu3_uapi_grid_config afGrid;\n+\t} af;\n+\n \tstruct {\n \t\tutils::Duration minShutterSpeed;\n \t\tutils::Duration maxShutterSpeed;\n@@ -34,6 +38,12 @@ struct IPASessionConfiguration {\n };\n \n struct IPAFrameContext {\n+\tstruct {\n+\t\tuint32_t focus;\n+\t\tdouble maxVariance;\n+\t\tbool stable;\n+\t} af;\n+\n \tstruct {\n \t\tuint32_t exposure;\n \t\tdouble gain;\ndiff --git a/src/ipa/ipu3/ipu3.cpp b/src/ipa/ipu3/ipu3.cpp\nindex e44a31bb..417e0562 100644\n--- a/src/ipa/ipu3/ipu3.cpp\n+++ b/src/ipa/ipu3/ipu3.cpp\n@@ -30,6 +30,7 @@\n \n #include \"libcamera/internal/mapped_framebuffer.h\"\n \n+#include \"algorithms/af.h\"\n #include \"algorithms/agc.h\"\n #include \"algorithms/algorithm.h\"\n #include \"algorithms/awb.h\"\n@@ -295,6 +296,7 @@ int IPAIPU3::init(const IPASettings &settings,\n \t}\n \n \t/* Construct our Algorithms */\n+\talgorithms_.push_back(std::make_unique<algorithms::Af>());\n \talgorithms_.push_back(std::make_unique<algorithms::Agc>());\n \talgorithms_.push_back(std::make_unique<algorithms::Awb>());\n \talgorithms_.push_back(std::make_unique<algorithms::BlackLevelCorrection>());\n","prefixes":["libcamera-devel","v8"]}