Show a patch.

GET /api/patches/15355/?format=api
HTTP 200 OK
Allow: GET, PUT, PATCH, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

{
    "id": 15355,
    "url": "https://patchwork.libcamera.org/api/patches/15355/?format=api",
    "web_url": "https://patchwork.libcamera.org/patch/15355/",
    "project": {
        "id": 1,
        "url": "https://patchwork.libcamera.org/api/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": "<20220210085759.71419-1-hpa@redhat.com>",
    "date": "2022-02-10T08:57:59",
    "name": "[libcamera-devel,v7] ipa: ipu3: af: Auto focus for dw9719 Surface Go2 VCM",
    "commit_ref": null,
    "pull_url": null,
    "state": "accepted",
    "archived": false,
    "hash": "b438e88b516d4362ded0550ac39bc87446383959",
    "submitter": {
        "id": 105,
        "url": "https://patchwork.libcamera.org/api/people/105/?format=api",
        "name": "Kate Hsuan",
        "email": "hpa@redhat.com"
    },
    "delegate": null,
    "mbox": "https://patchwork.libcamera.org/patch/15355/mbox/",
    "series": [
        {
            "id": 2925,
            "url": "https://patchwork.libcamera.org/api/series/2925/?format=api",
            "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=2925",
            "date": "2022-02-10T08:57:59",
            "name": "[libcamera-devel,v7] ipa: ipu3: af: Auto focus for dw9719 Surface Go2 VCM",
            "version": 7,
            "mbox": "https://patchwork.libcamera.org/series/2925/mbox/"
        }
    ],
    "comments": "https://patchwork.libcamera.org/api/patches/15355/comments/",
    "check": "pending",
    "checks": "https://patchwork.libcamera.org/api/patches/15355/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 D91AABF415\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu, 10 Feb 2022 08:58:34 +0000 (UTC)",
            "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 290D4610B3;\n\tThu, 10 Feb 2022 09:58:34 +0100 (CET)",
            "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 5EE2B61098\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 10 Feb 2022 09:58:31 +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-584-KS-rTHPmNFi-sNd9GUp9KA-1; Thu, 10 Feb 2022 03:58:22 -0500",
            "from smtp.corp.redhat.com\n\t(int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11])\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 EFCCC1091DA1;\n\tThu, 10 Feb 2022 08:58:20 +0000 (UTC)",
            "from fedora.redhat.com (unknown [10.39.195.40])\n\tby smtp.corp.redhat.com (Postfix) with ESMTP id 9CFA966E10;\n\tThu, 10 Feb 2022 08:58:12 +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=\"DHuwuFiq\"; 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=1644483510;\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=UblV14donUqRJmP6aDgJyLK72B5znZ/XUQckVQzz0eE=;\n\tb=DHuwuFiqaFxMsfjErh2EgcU1izrk0g95LN/3BtMqm+FH6UpfTBBERf5mNjB9bYTWRUw6lw\n\tx17e4jQbdoEbyW8c+IslWfQ6fzdULV7vZI73L7tfyLB+JzR61o0xr2KtGq4BpoMjzUhaxd\n\tlk3UY/8a7eXIxobst2cGXtZIw6qE6Y8=",
        "X-MC-Unique": "KS-rTHPmNFi-sNd9GUp9KA-1",
        "From": "Kate Hsuan <hpa@redhat.com>",
        "To": "libcamera devel <libcamera-devel@lists.libcamera.org>",
        "Date": "Thu, 10 Feb 2022 16:57:59 +0800",
        "Message-Id": "<20220210085759.71419-1-hpa@redhat.com>",
        "MIME-Version": "1.0",
        "X-Scanned-By": "MIMEDefang 2.79 on 10.5.11.11",
        "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 v7] 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 statistics.\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 v7:\n1. Improved comments and fixed typo.\n2. Simplified AF algorithm. \"ignore frame\" had been moved into a\nfunction and remove a unnecessary \"if\" expression.\n3. afRawBufferLen_ keeped to be a local variable not a class member.\n4. Initial configurations are moved to configure().\n---\n src/ipa/ipu3/algorithms/af.cpp      | 426 ++++++++++++++++++++++++++++\n src/ipa/ipu3/algorithms/af.h        |  78 +++++\n src/ipa/ipu3/algorithms/meson.build |   3 +-\n src/ipa/ipu3/ipa_context.cpp        |  24 ++\n src/ipa/ipu3/ipa_context.h          |  10 +\n src/ipa/ipu3/ipu3.cpp               |   2 +\n 6 files changed, 542 insertions(+), 1 deletion(-)\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..b26aa018\n--- /dev/null\n+++ b/src/ipa/ipu3/algorithms/af.cpp\n@@ -0,0 +1,426 @@\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, in number of grid blocks(cells).\n+*/\n+\n+/**\n+ * \\var kAfMinGridHeight\n+ * \\brief the minimum height of AF grid.\n+ * The minimum grid vertical dimensions, in number of grid blocks(cells).\n+*/\n+\n+/**\n+ * \\var kAfMaxGridWidth\n+ * \\brief the maximum width of AF grid.\n+ * The maximum grid horizontal dimensions, in number of grid blocks(cells).\n+*/\n+\n+/**\n+ * \\var kAfMaxGridHeight\n+ * \\brief the maximum height of AF grid.\n+ * The maximum grid vertical dimensions, in number of grid blocks(cells).\n+*/\n+\n+/**\n+ * \\var kAfMinGridBlockWidth\n+ * \\brief the minimum block size of the width.\n+ */\n+\n+/**\n+ * \\var kAfMinGridBlockHeight\n+ * \\brief the minimum block size of the height.\n+ */\n+\n+/**\n+ * \\def kAfMaxGridBlockWidth\n+ * \\brief the maximum block size of the width.\n+ */\n+\n+/**\n+ * \\var kAfMaxGridBlockHeight\n+ * \\brief the maximum block size of the height.\n+ */\n+\n+/**\n+ * \\var kAfDefaultHeightPerSlice\n+ * \\brief The default 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+/**\n+ * \\class Af\n+ * \\brief An auto-focus algorithm based on IPU3 statistics\n+ *\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+ */\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+Af::Af()\n+\t: focus_(0), goodFocus_(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+ *\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/* inital max focus step */\n+\tmaxStep_ = kMaxFocusSteps;\n+\n+\t/* determined focus value i.e. current 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+ *\n+ * Find a near focused image using a coarse step. The step is determined by coarseSearchStep.\n+ *\n+ * \\param[in] context The shared IPA context\n+ *\n+ */\n+void Af::afCoarseScan(IPAContext &context)\n+{\n+\tif (coarseCompleted_ == true)\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 - (context.frameContext.af.focus * kFineRange);\n+\t\tcontext.frameContext.af.focus = focus_;\n+\t\tpreviousVariance_ = 0;\n+\t\tmaxStep_ = std::clamp(static_cast<uint32_t>(focus_ + (focus_ * kFineRange)), 0U, kMaxFocusSteps);\n+\t}\n+}\n+\n+/**\n+ * \\brief AF fine scan\n+ *\n+ * Find an optimum lens position with moving 1 step for each search.\n+ *\n+ * \\param[in] context The shared IPA context\n+ *\n+ */\n+void Af::afFineScan(IPAContext &context)\n+{\n+\tif (coarseCompleted_ != true)\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+ *\n+ * Reset all the parameters to start over the AF process.\n+ *\n+ * \\param[in] context The shared IPA context\n+ *\n+ */\n+void Af::afReset(IPAContext &context)\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+ *\n+ * This fuction compares the previous and current variance. It always picks\n+ * the largest variance to replace the previous one. The image with a larger\n+ * variance also indicates it is a clearer image than previous one. If it\n+ * finds the negative sign of derivative, it returns immediately.\n+ *\n+ * \\param[in] context The shared IPA context\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 = goodFocus_;\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\tgoodFocus_ = 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 returnimmediately.\n+\t\t\t */\n+\t\t\tcontext.frameContext.af.focus = goodFocus_;\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   << goodFocus_\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+ *\n+ * \\return Return true the frame is ignored.\n+ * \\return Return false the frame should be processed.\n+ */\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+ */\n+void Af::afIgnoreFrameReset()\n+{\n+\tignoreCounter_ = kIgnoreFrame;\n+}\n+\n+/**\n+ * \\brief Determine the max contrast image and lens position.\n+ *\n+ * y_table is the AF statistic of IPU3 and is composed of two kinds of filtered\n+ * values. Based on this, the variance of a image could be used to determine\n+ * the clearness of the given image. Ideally, a clear image also has a\n+ * raletively higher contrast. So, the VCM is moved step by step and variance\n+ * of each frame are calculated to find a maximum variance which corresponds\n+ * with a specific focus step. If it is found, that is the best focus step of\n+ * current scene.\n+ *\n+ * \\param[in] context The shared IPA context.\n+ * \\param[in] stats The statistic buffer of 3A of IPU3.\n+ */\n+void Af::process(IPAContext &context, const ipu3_uapi_stats_3a *stats)\n+{\n+\tuint32_t total = 0;\n+\tdouble mean;\n+\tuint64_t var_sum = 0;\n+\ty_table_item_t y_item[IPU3_UAPI_AF_Y_TABLE_MAX_SIZE / sizeof(y_table_item_t)];\n+\tuint32_t z = 0;\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, 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 *\n+\t * For coarse: y1 are used.\n+\t * For fine: y2 results are used.\n+\t */\n+\tif (coarseCompleted_) {\n+\t\tfor (z = 0; z < afRawBufferLen_; z++) {\n+\t\t\ttotal = total + y_item[z].y2_avg;\n+\t\t}\n+\t\tmean = total / afRawBufferLen_;\n+\n+\t\tfor (z = 0; z < afRawBufferLen_; z++) {\n+\t\t\tvar_sum = var_sum + ((y_item[z].y2_avg - mean) *\n+\t\t\t\t\t     (y_item[z].y2_avg - mean));\n+\t\t}\n+\t} else {\n+\t\tfor (z = 0; z < afRawBufferLen_; z++) {\n+\t\t\ttotal = total + y_item[z].y1_avg;\n+\t\t}\n+\t\tmean = total / afRawBufferLen_;\n+\n+\t\tfor (z = 0; z < afRawBufferLen_; z++) {\n+\t\t\tvar_sum = var_sum + ((y_item[z].y1_avg - mean) *\n+\t\t\t\t\t     (y_item[z].y1_avg - mean));\n+\t\t}\n+\t}\n+\n+\t/* determine the average variance of the AF statistic. */\n+\tcurrentVariance_ = static_cast<double>(var_sum) / static_cast<double>(afRawBufferLen_);\n+\n+\tif (context.frameContext.af.stable == true) {\n+\t\tconst uint32_t diff_var = std::abs(currentVariance_ -\n+\t\t\t\t\t\t   context.frameContext.af.maxVariance);\n+\t\tconst double var_ratio = diff_var / context.frameContext.af.maxVariance;\n+\t\tLOG(IPU3Af, Debug) << \"Variance change rate: \"\n+\t\t\t\t   << var_ratio\n+\t\t\t\t   << \" Current VCM step: \"\n+\t\t\t\t   << context.frameContext.af.focus;\n+\n+\t\t/*\n+\t\t * If the change rate is greater than kMaxChange (out of focus),\n+\t\t * trigger AF again.\n+\t\t */\n+\t\tif (var_ratio > kMaxChange) {\n+\t\t\tif (!afNeedIgnoreFrame())\n+\t\t\t\tafReset(context);\n+\t\t} else\n+\t\t\tafIgnoreFrameReset();\n+\t} else {\n+\t\tif (!afNeedIgnoreFrame()) {\n+\t\t\tafCoarseScan(context);\n+\t\t\tafFineScan(context);\n+\t\t}\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..bc5d2064\n--- /dev/null\n+++ b/src/ipa/ipu3/algorithms/af.h\n@@ -0,0 +1,78 @@\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+#ifndef __LIBCAMERA_IPU3_ALGORITHMS_AF_H__\n+#define __LIBCAMERA_IPU3_ALGORITHMS_AF_H__\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+\n+\t/* Used for focus scan. */\n+\tuint32_t focus_;\n+\t/* Focus good */\n+\tuint32_t goodFocus_;\n+\t/* Recent AF statistic variance. */\n+\tdouble currentVariance_;\n+\t/* The frames to be ignore before starting measuring. */\n+\tuint32_t ignoreCounter_;\n+\t/* previous variance. it is used to determine the gradient */\n+\tdouble previousVariance_;\n+\t/* Max scan steps of each pass of AF scaning */\n+\tuint32_t maxStep_;\n+\t/* coarse scan stable. Complete low pass search (coarse) scan) */\n+\tbool coarseCompleted_;\n+\t/* fine scan stable. Complete high pass scan (fine scan) */\n+\tbool fineCompleted_;\n+};\n+\n+} /* namespace ipa::ipu3::algorithms */\n+\n+} /* namespace libcamera */\n+\n+#endif /* __LIBCAMERA_IPU3_ALGORITHMS_AF_H__ */\ndiff --git a/src/ipa/ipu3/algorithms/meson.build b/src/ipa/ipu3/algorithms/meson.build\nindex 4db6ae1d..e1099169 100644\n--- a/src/ipa/ipu3/algorithms/meson.build\n+++ b/src/ipa/ipu3/algorithms/meson.build\n@@ -1,8 +1,9 @@\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',\n-    'tone_mapping.cpp',\n+    'tone_mapping.cpp'\n ])\ndiff --git a/src/ipa/ipu3/ipa_context.cpp b/src/ipa/ipu3/ipa_context.cpp\nindex 86794ac1..ecb2b90a 100644\n--- a/src/ipa/ipu3/ipa_context.cpp\n+++ b/src/ipa/ipu3/ipa_context.cpp\n@@ -67,6 +67,30 @@ namespace libcamera::ipa::ipu3 {\n  *\n  * \\var IPASessionConfiguration::grid.stride\n  * \\brief Number of cells on one line including the ImgU padding\n+ *\n+ */\n+\n+/**\n+ * \\var IPASessionConfiguration::af\n+ * \\brief AF grid configuration of the IPA\n+ *\n+ * \\var IPASessionConfiguration::af.afGrid\n+ *\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 is the image focused?\n  */\n \n /**\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",
        "v7"
    ]
}