[{"id":26803,"web_url":"https://patchwork.libcamera.org/comment/26803/","msgid":"<20230331075818.j3q3oggzaup4naq6@uno.localdomain>","date":"2023-03-31T07:58:18","subject":"Re: [libcamera-devel] [PATCH v3 10/10] ipa: raspberrypi: Generalise\n\tthe focus reporting code","submitter":{"id":143,"url":"https://patchwork.libcamera.org/api/people/143/","name":"Jacopo Mondi","email":"jacopo.mondi@ideasonboard.com"},"content":"Hi Naush\n\nOn Fri, Mar 31, 2023 at 08:23:41AM +0100, Naushir Patuck via libcamera-devel wrote:\n> Use the generalised focus statistics structure to compute the centre\n> window focus FoM value. This avoids needed to hard-code a specific\n> grid size.\n>\n> Remove the focus reporting algorithm as the functionality is duplicated\n> by this bit of IPA code. Remove focus_status.h as it is no longer needed.\n>\n> Signed-off-by: Naushir Patuck <naush@raspberrypi.com>\n\nThanks for explaining why we can't populate focus.status in the AF\nalgorithm itself.\n\nReviewed-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>\n\n> ---\n>  src/ipa/raspberrypi/controller/focus_status.h | 20 --------\n>  src/ipa/raspberrypi/controller/rpi/af.h       |  1 -\n>  src/ipa/raspberrypi/controller/rpi/focus.cpp  | 49 -------------------\n>  src/ipa/raspberrypi/data/imx290.json          |  3 --\n>  src/ipa/raspberrypi/data/imx296.json          |  3 --\n>  src/ipa/raspberrypi/data/imx296_mono.json     |  3 --\n>  src/ipa/raspberrypi/data/imx477.json          |  3 --\n>  src/ipa/raspberrypi/data/imx477_noir.json     |  3 --\n>  .../raspberrypi/data/imx477_scientific.json   |  3 --\n>  src/ipa/raspberrypi/data/imx477_v1.json       |  3 --\n>  src/ipa/raspberrypi/data/imx708.json          |  3 --\n>  src/ipa/raspberrypi/data/imx708_noir.json     |  3 --\n>  src/ipa/raspberrypi/data/imx708_wide.json     |  3 --\n>  .../raspberrypi/data/imx708_wide_noir.json    |  3 --\n>  src/ipa/raspberrypi/meson.build               |  1 -\n>  src/ipa/raspberrypi/raspberrypi.cpp           | 32 +++++++++---\n>  16 files changed, 24 insertions(+), 112 deletions(-)\n>  delete mode 100644 src/ipa/raspberrypi/controller/focus_status.h\n>  delete mode 100644 src/ipa/raspberrypi/controller/rpi/focus.cpp\n>\n> diff --git a/src/ipa/raspberrypi/controller/focus_status.h b/src/ipa/raspberrypi/controller/focus_status.h\n> deleted file mode 100644\n> index 8b74e59840c1..000000000000\n> --- a/src/ipa/raspberrypi/controller/focus_status.h\n> +++ /dev/null\n> @@ -1,20 +0,0 @@\n> -/* SPDX-License-Identifier: BSD-2-Clause */\n> -/*\n> - * Copyright (C) 2020, Raspberry Pi Ltd\n> - *\n> - * focus_status.h - focus measurement status\n> - */\n> -#pragma once\n> -\n> -#include <linux/bcm2835-isp.h>\n> -\n> -/*\n> - * The focus algorithm should post the following structure into the image's\n> - * \"focus.status\" metadata. Recall that it's only reporting focus (contrast)\n> - * measurements, it's not driving any kind of auto-focus algorithm!\n> - */\n> -\n> -struct FocusStatus {\n> -\tunsigned int num;\n> -\tuint32_t focusMeasures[FOCUS_REGIONS];\n> -};\n> diff --git a/src/ipa/raspberrypi/controller/rpi/af.h b/src/ipa/raspberrypi/controller/rpi/af.h\n> index b479feb88c39..6d2bae671a22 100644\n> --- a/src/ipa/raspberrypi/controller/rpi/af.h\n> +++ b/src/ipa/raspberrypi/controller/rpi/af.h\n> @@ -28,7 +28,6 @@\n>   * \"nuisance\" scans. During each interval where PDAF is not working, only\n>   * ONE scan will be performed; CAF cannot track objects using CDAF alone.\n>   *\n> - * This algorithm is unrelated to \"rpi.focus\" which merely reports CDAF FoM.\n>   */\n>\n>  namespace RPiController {\n> diff --git a/src/ipa/raspberrypi/controller/rpi/focus.cpp b/src/ipa/raspberrypi/controller/rpi/focus.cpp\n> deleted file mode 100644\n> index ea3cc00e42c3..000000000000\n> --- a/src/ipa/raspberrypi/controller/rpi/focus.cpp\n> +++ /dev/null\n> @@ -1,49 +0,0 @@\n> -/* SPDX-License-Identifier: BSD-2-Clause */\n> -/*\n> - * Copyright (C) 2020, Raspberry Pi Ltd\n> - *\n> - * focus.cpp - focus algorithm\n> - */\n> -#include <stdint.h>\n> -\n> -#include <libcamera/base/log.h>\n> -\n> -#include \"../focus_status.h\"\n> -#include \"focus.h\"\n> -\n> -using namespace RPiController;\n> -using namespace libcamera;\n> -\n> -LOG_DEFINE_CATEGORY(RPiFocus)\n> -\n> -#define NAME \"rpi.focus\"\n> -\n> -Focus::Focus(Controller *controller)\n> -\t: Algorithm(controller)\n> -{\n> -}\n> -\n> -char const *Focus::name() const\n> -{\n> -\treturn NAME;\n> -}\n> -\n> -void Focus::process(StatisticsPtr &stats, Metadata *imageMetadata)\n> -{\n> -\tFocusStatus status;\n> -\tfor (unsigned int i = 0; i < stats->focusRegions.numRegions(); i++)\n> -\t\tstatus.focusMeasures[i] = stats->focusRegions.get(i).val;\n> -\tstatus.num = stats->focusRegions.numRegions();\n> -\timageMetadata->set(\"focus.status\", status);\n> -\n> -\tLOG(RPiFocus, Debug)\n> -\t\t<< \"Focus contrast measure: \"\n> -\t\t<< (status.focusMeasures[5] + status.focusMeasures[6]) / 10;\n> -}\n> -\n> -/* Register algorithm with the system. */\n> -static Algorithm *create(Controller *controller)\n> -{\n> -\treturn new Focus(controller);\n> -}\n> -static RegisterAlgorithm reg(NAME, &create);\n> diff --git a/src/ipa/raspberrypi/data/imx290.json b/src/ipa/raspberrypi/data/imx290.json\n> index bfb9c6093d26..ace68d0ebf1c 100644\n> --- a/src/ipa/raspberrypi/data/imx290.json\n> +++ b/src/ipa/raspberrypi/data/imx290.json\n> @@ -195,9 +195,6 @@\n>                      }\n>                  ]\n>              }\n> -        },\n> -        {\n> -            \"rpi.focus\": { }\n>          }\n>      ]\n>  }\n> \\ No newline at end of file\n> diff --git a/src/ipa/raspberrypi/data/imx296.json b/src/ipa/raspberrypi/data/imx296.json\n> index 346f5b658957..ae8722c46a85 100644\n> --- a/src/ipa/raspberrypi/data/imx296.json\n> +++ b/src/ipa/raspberrypi/data/imx296.json\n> @@ -532,9 +532,6 @@\n>                  \"strength\": 1.0,\n>                  \"limit\": 0.18\n>              }\n> -        },\n> -        {\n> -            \"rpi.focus\": { }\n>          }\n>      ]\n>  }\n> diff --git a/src/ipa/raspberrypi/data/imx296_mono.json b/src/ipa/raspberrypi/data/imx296_mono.json\n> index e9fa30c6a08d..30965b4b8d61 100644\n> --- a/src/ipa/raspberrypi/data/imx296_mono.json\n> +++ b/src/ipa/raspberrypi/data/imx296_mono.json\n> @@ -228,9 +228,6 @@\n>                  \"strength\": 1.0,\n>                  \"limit\": 0.18\n>              }\n> -        },\n> -        {\n> -            \"rpi.focus\": { }\n>          }\n>      ]\n>  }\n> diff --git a/src/ipa/raspberrypi/data/imx477.json b/src/ipa/raspberrypi/data/imx477.json\n> index bfc0774ffdfc..daffc268ff43 100644\n> --- a/src/ipa/raspberrypi/data/imx477.json\n> +++ b/src/ipa/raspberrypi/data/imx477.json\n> @@ -513,9 +513,6 @@\n>          },\n>          {\n>              \"rpi.sharpen\": { }\n> -        },\n> -        {\n> -            \"rpi.focus\": { }\n>          }\n>      ]\n>  }\n> \\ No newline at end of file\n> diff --git a/src/ipa/raspberrypi/data/imx477_noir.json b/src/ipa/raspberrypi/data/imx477_noir.json\n> index dadd97bc3d78..52d7f072d0ce 100644\n> --- a/src/ipa/raspberrypi/data/imx477_noir.json\n> +++ b/src/ipa/raspberrypi/data/imx477_noir.json\n> @@ -424,9 +424,6 @@\n>          },\n>          {\n>              \"rpi.sharpen\": { }\n> -        },\n> -        {\n> -            \"rpi.focus\": { }\n>          }\n>      ]\n>  }\n> \\ No newline at end of file\n> diff --git a/src/ipa/raspberrypi/data/imx477_scientific.json b/src/ipa/raspberrypi/data/imx477_scientific.json\n> index 17c4ed0a5e74..26c692fdbab9 100644\n> --- a/src/ipa/raspberrypi/data/imx477_scientific.json\n> +++ b/src/ipa/raspberrypi/data/imx477_scientific.json\n> @@ -474,9 +474,6 @@\n>          },\n>          {\n>              \"rpi.sharpen\": { }\n> -        },\n> -        {\n> -            \"rpi.focus\": { }\n>          }\n>      ]\n>  }\n> \\ No newline at end of file\n> diff --git a/src/ipa/raspberrypi/data/imx477_v1.json b/src/ipa/raspberrypi/data/imx477_v1.json\n> index 5bcaac67069b..d64020091efa 100644\n> --- a/src/ipa/raspberrypi/data/imx477_v1.json\n> +++ b/src/ipa/raspberrypi/data/imx477_v1.json\n> @@ -511,9 +511,6 @@\n>          },\n>          {\n>              \"rpi.sharpen\": { }\n> -        },\n> -        {\n> -            \"rpi.focus\": { }\n>          }\n>      ]\n>  }\n> \\ No newline at end of file\n> diff --git a/src/ipa/raspberrypi/data/imx708.json b/src/ipa/raspberrypi/data/imx708.json\n> index c38b2d4cf256..b9830a3bf692 100644\n> --- a/src/ipa/raspberrypi/data/imx708.json\n> +++ b/src/ipa/raspberrypi/data/imx708.json\n> @@ -512,9 +512,6 @@\n>          {\n>              \"rpi.sharpen\": { }\n>          },\n> -        {\n> -            \"rpi.focus\": { }\n> -        },\n>          {\n>              \"rpi.af\":\n>              {\n> diff --git a/src/ipa/raspberrypi/data/imx708_noir.json b/src/ipa/raspberrypi/data/imx708_noir.json\n> index 082274e34c53..075f70355cec 100644\n> --- a/src/ipa/raspberrypi/data/imx708_noir.json\n> +++ b/src/ipa/raspberrypi/data/imx708_noir.json\n> @@ -512,9 +512,6 @@\n>          {\n>              \"rpi.sharpen\": { }\n>          },\n> -        {\n> -            \"rpi.focus\": { }\n> -        },\n>          {\n>              \"rpi.af\":\n>              {\n> diff --git a/src/ipa/raspberrypi/data/imx708_wide.json b/src/ipa/raspberrypi/data/imx708_wide.json\n> index cdc61436995d..b772efee3b96 100644\n> --- a/src/ipa/raspberrypi/data/imx708_wide.json\n> +++ b/src/ipa/raspberrypi/data/imx708_wide.json\n> @@ -403,9 +403,6 @@\n>          {\n>              \"rpi.sharpen\": { }\n>          },\n> -        {\n> -            \"rpi.focus\": { }\n> -        },\n>          {\n>              \"rpi.af\":\n>              {\n> diff --git a/src/ipa/raspberrypi/data/imx708_wide_noir.json b/src/ipa/raspberrypi/data/imx708_wide_noir.json\n> index 8a7f59910833..c5f6b53dca7a 100644\n> --- a/src/ipa/raspberrypi/data/imx708_wide_noir.json\n> +++ b/src/ipa/raspberrypi/data/imx708_wide_noir.json\n> @@ -403,9 +403,6 @@\n>          {\n>              \"rpi.sharpen\": { }\n>          },\n> -        {\n> -            \"rpi.focus\": { }\n> -        },\n>          {\n>              \"rpi.af\":\n>              {\n> diff --git a/src/ipa/raspberrypi/meson.build b/src/ipa/raspberrypi/meson.build\n> index 9230e17bca22..de78cbd80f9c 100644\n> --- a/src/ipa/raspberrypi/meson.build\n> +++ b/src/ipa/raspberrypi/meson.build\n> @@ -33,7 +33,6 @@ rpi_ipa_sources = files([\n>      'controller/rpi/awb.cpp',\n>      'controller/rpi/sharpen.cpp',\n>      'controller/rpi/black_level.cpp',\n> -    'controller/rpi/focus.cpp',\n>      'controller/rpi/geq.cpp',\n>      'controller/rpi/noise.cpp',\n>      'controller/rpi/lux.cpp',\n> diff --git a/src/ipa/raspberrypi/raspberrypi.cpp b/src/ipa/raspberrypi/raspberrypi.cpp\n> index e2662938c9d3..9f3f3ac3b15d 100644\n> --- a/src/ipa/raspberrypi/raspberrypi.cpp\n> +++ b/src/ipa/raspberrypi/raspberrypi.cpp\n> @@ -50,7 +50,6 @@\n>  #include \"denoise_algorithm.h\"\n>  #include \"denoise_status.h\"\n>  #include \"dpc_status.h\"\n> -#include \"focus_status.h\"\n>  #include \"geq_status.h\"\n>  #include \"lux_status.h\"\n>  #include \"metadata.h\"\n> @@ -642,14 +641,28 @@ void IPARPi::reportMetadata(unsigned int ipaContext)\n>  \t\t\t\t\t static_cast<int32_t>(blackLevelStatus->blackLevelG),\n>  \t\t\t\t\t static_cast<int32_t>(blackLevelStatus->blackLevelB) });\n>\n> -\tFocusStatus *focusStatus = rpiMetadata.getLocked<FocusStatus>(\"focus.status\");\n> -\tif (focusStatus && focusStatus->num == 12) {\n> +\tRPiController::FocusRegions *focusStatus =\n> +\t\trpiMetadata.getLocked<RPiController::FocusRegions>(\"focus.status\");\n> +\tif (focusStatus) {\n>  \t\t/*\n> -\t\t * We get a 4x3 grid of regions by default. Calculate the average\n> -\t\t * FoM over the central two positions to give an overall scene FoM.\n> -\t\t * This can change later if it is not deemed suitable.\n> +\t\t * Calculate the average FoM over the central (symmetric) positions\n> +\t\t * to give an overall scene FoM. This can change later if it is\n> +\t\t * not deemed suitable.\n>  \t\t */\n> -\t\tint32_t focusFoM = (focusStatus->focusMeasures[5] + focusStatus->focusMeasures[6]) / 2;\n> +\t\tlibcamera::Size size = focusStatus->size();\n> +\t\tunsigned rows = size.height;\n> +\t\tunsigned cols = size.width;\n> +\n> +\t\tuint64_t sum = 0;\n> +\t\tunsigned int numRegions = 0;\n> +\t\tfor (unsigned r = rows / 3; r < rows - rows / 3; ++r) {\n> +\t\t\tfor (unsigned c = cols / 4; c < cols - cols / 4; ++c) {\n> +\t\t\t\tsum += focusStatus->get({ (int)c, (int)r }).val;\n> +\t\t\t\tnumRegions++;\n> +\t\t\t}\n> +\t\t}\n> +\n> +\t\tuint32_t focusFoM = (sum / numRegions) >> 16;\n>  \t\tlibcameraMetadata_.set(controls::FocusFoM, focusFoM);\n>  \t}\n>\n> @@ -1428,7 +1441,6 @@ RPiController::StatisticsPtr IPARPi::fillStatistics(bcm2835_isp_stats *stats) co\n>  \t\tstatistics->focusRegions.set(i, { stats->focus_stats[i].contrast_val[1][1] / 1000,\n>  \t\t\t\t\t\t  stats->focus_stats[i].contrast_val_num[1][1],\n>  \t\t\t\t\t\t  stats->focus_stats[i].contrast_val_num[1][0] });\n> -\n>  \treturn statistics;\n>  }\n>\n> @@ -1445,6 +1457,10 @@ void IPARPi::processStats(unsigned int bufferId, unsigned int ipaContext)\n>  \tSpan<uint8_t> mem = it->second.planes()[0];\n>  \tbcm2835_isp_stats *stats = reinterpret_cast<bcm2835_isp_stats *>(mem.data());\n>  \tRPiController::StatisticsPtr statistics = fillStatistics(stats);\n> +\n> +\t/* Save the focus stats in the metadata structure to report out later. */\n> +\trpiMetadata_[ipaContext].set(\"focus.status\", statistics->focusRegions);\n> +\n>  \thelper_->process(statistics, rpiMetadata);\n>  \tcontroller_.process(statistics, &rpiMetadata);\n>\n> --\n> 2.34.1\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 723C1C326B\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri, 31 Mar 2023 07:58:23 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 9573462724;\n\tFri, 31 Mar 2023 09:58:22 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id C7E1961ECB\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 31 Mar 2023 09:58:20 +0200 (CEST)","from ideasonboard.com (93-61-96-190.ip145.fastwebnet.it\n\t[93.61.96.190])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 555D8A39;\n\tFri, 31 Mar 2023 09:58:20 +0200 (CEST)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1680249502;\n\tbh=OpcGVajCZXpq1bmzAdrcExDICbWJTdF1URgoakLd6j8=;\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=4U5ag0pgpdf1QX98ErWL0qqmrPLASxCaQiAUzDX/qKaDQXFazGcBKZqw/tG3+ASnV\n\txtpJYo1PgHN33eaoHDwXrak0RJN5dNFNuji78kxTOUWHyO/3UlLD3mFG60h2rjKzDC\n\txdIiSURaEptPLW6Tui6D0pTlESRezCMFyXPvNaTWQq4vv9C/D6DkV8RU1OLOLChNMy\n\tYdRMdXMWf0I0GUVcu4BlzLyGeHwDa5J7WiwXXPTP44Ex5Rst3XfpO8puAjwf/ENqMv\n\tY/zyJSrtV4Sx/UpFf/z/7ig/2qLwmA899kGzhKF1hQeH3AlLOtPrHvCw37X9TtK5NC\n\tPL4TljV1JbpoA==","v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1680249500;\n\tbh=OpcGVajCZXpq1bmzAdrcExDICbWJTdF1URgoakLd6j8=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=DOJaPM/4bkso7HgVGm1L7yx2kORr5WSZVtAjJ4xGHX8xRUOh0rF0ZO/ypZ1szuW2u\n\tNertSqBS6V4YlWFPs5O5+6Z0AynydjqKyBw+/r4sfGh90rJgNMIDh4uXAbE1Kvreiz\n\t7LcMXwI3+vrQt99UBZFYs1nHOVZHWcUiCLIvuHco="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"DOJaPM/4\"; dkim-atps=neutral","Date":"Fri, 31 Mar 2023 09:58:18 +0200","To":"Naushir Patuck <naush@raspberrypi.com>","Message-ID":"<20230331075818.j3q3oggzaup4naq6@uno.localdomain>","References":"<20230331072341.24851-1-naush@raspberrypi.com>\n\t<20230331072341.24851-11-naush@raspberrypi.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20230331072341.24851-11-naush@raspberrypi.com>","Subject":"Re: [libcamera-devel] [PATCH v3 10/10] ipa: raspberrypi: Generalise\n\tthe focus reporting code","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":"libcamera-devel@lists.libcamera.org","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}},{"id":26804,"web_url":"https://patchwork.libcamera.org/comment/26804/","msgid":"<CAHW6GY+WUxLzwEb1X2Wrm=KLN_fzHWAsuJTYeQKkeRQJuhypMA@mail.gmail.com>","date":"2023-03-31T08:18:08","subject":"Re: [libcamera-devel] [PATCH v3 10/10] ipa: raspberrypi: Generalise\n\tthe focus reporting code","submitter":{"id":42,"url":"https://patchwork.libcamera.org/api/people/42/","name":"David Plowman","email":"david.plowman@raspberrypi.com"},"content":"Hi Naush\n\nThanks for the patch.\n\nOn Fri, 31 Mar 2023 at 08:41, Naushir Patuck via libcamera-devel\n<libcamera-devel@lists.libcamera.org> wrote:\n>\n> Use the generalised focus statistics structure to compute the centre\n> window focus FoM value. This avoids needed to hard-code a specific\n> grid size.\n>\n> Remove the focus reporting algorithm as the functionality is duplicated\n> by this bit of IPA code. Remove focus_status.h as it is no longer needed.\n>\n> Signed-off-by: Naushir Patuck <naush@raspberrypi.com>\n\nI think this is all fine. My only very slight misgiving is that\ngetting the focus FOM out (for manual lens adjustment) is ever so\nslightly harder now for folks who are running without X Windows, but\nhopefully writing 5 lines of Python is not beyond too many people...\n\nReviewed-by: David Plowman <david.plowman@raspberrypi.com>\n\nThanks!\nDavid\n\n> ---\n>  src/ipa/raspberrypi/controller/focus_status.h | 20 --------\n>  src/ipa/raspberrypi/controller/rpi/af.h       |  1 -\n>  src/ipa/raspberrypi/controller/rpi/focus.cpp  | 49 -------------------\n>  src/ipa/raspberrypi/data/imx290.json          |  3 --\n>  src/ipa/raspberrypi/data/imx296.json          |  3 --\n>  src/ipa/raspberrypi/data/imx296_mono.json     |  3 --\n>  src/ipa/raspberrypi/data/imx477.json          |  3 --\n>  src/ipa/raspberrypi/data/imx477_noir.json     |  3 --\n>  .../raspberrypi/data/imx477_scientific.json   |  3 --\n>  src/ipa/raspberrypi/data/imx477_v1.json       |  3 --\n>  src/ipa/raspberrypi/data/imx708.json          |  3 --\n>  src/ipa/raspberrypi/data/imx708_noir.json     |  3 --\n>  src/ipa/raspberrypi/data/imx708_wide.json     |  3 --\n>  .../raspberrypi/data/imx708_wide_noir.json    |  3 --\n>  src/ipa/raspberrypi/meson.build               |  1 -\n>  src/ipa/raspberrypi/raspberrypi.cpp           | 32 +++++++++---\n>  16 files changed, 24 insertions(+), 112 deletions(-)\n>  delete mode 100644 src/ipa/raspberrypi/controller/focus_status.h\n>  delete mode 100644 src/ipa/raspberrypi/controller/rpi/focus.cpp\n>\n> diff --git a/src/ipa/raspberrypi/controller/focus_status.h b/src/ipa/raspberrypi/controller/focus_status.h\n> deleted file mode 100644\n> index 8b74e59840c1..000000000000\n> --- a/src/ipa/raspberrypi/controller/focus_status.h\n> +++ /dev/null\n> @@ -1,20 +0,0 @@\n> -/* SPDX-License-Identifier: BSD-2-Clause */\n> -/*\n> - * Copyright (C) 2020, Raspberry Pi Ltd\n> - *\n> - * focus_status.h - focus measurement status\n> - */\n> -#pragma once\n> -\n> -#include <linux/bcm2835-isp.h>\n> -\n> -/*\n> - * The focus algorithm should post the following structure into the image's\n> - * \"focus.status\" metadata. Recall that it's only reporting focus (contrast)\n> - * measurements, it's not driving any kind of auto-focus algorithm!\n> - */\n> -\n> -struct FocusStatus {\n> -       unsigned int num;\n> -       uint32_t focusMeasures[FOCUS_REGIONS];\n> -};\n> diff --git a/src/ipa/raspberrypi/controller/rpi/af.h b/src/ipa/raspberrypi/controller/rpi/af.h\n> index b479feb88c39..6d2bae671a22 100644\n> --- a/src/ipa/raspberrypi/controller/rpi/af.h\n> +++ b/src/ipa/raspberrypi/controller/rpi/af.h\n> @@ -28,7 +28,6 @@\n>   * \"nuisance\" scans. During each interval where PDAF is not working, only\n>   * ONE scan will be performed; CAF cannot track objects using CDAF alone.\n>   *\n> - * This algorithm is unrelated to \"rpi.focus\" which merely reports CDAF FoM.\n>   */\n>\n>  namespace RPiController {\n> diff --git a/src/ipa/raspberrypi/controller/rpi/focus.cpp b/src/ipa/raspberrypi/controller/rpi/focus.cpp\n> deleted file mode 100644\n> index ea3cc00e42c3..000000000000\n> --- a/src/ipa/raspberrypi/controller/rpi/focus.cpp\n> +++ /dev/null\n> @@ -1,49 +0,0 @@\n> -/* SPDX-License-Identifier: BSD-2-Clause */\n> -/*\n> - * Copyright (C) 2020, Raspberry Pi Ltd\n> - *\n> - * focus.cpp - focus algorithm\n> - */\n> -#include <stdint.h>\n> -\n> -#include <libcamera/base/log.h>\n> -\n> -#include \"../focus_status.h\"\n> -#include \"focus.h\"\n> -\n> -using namespace RPiController;\n> -using namespace libcamera;\n> -\n> -LOG_DEFINE_CATEGORY(RPiFocus)\n> -\n> -#define NAME \"rpi.focus\"\n> -\n> -Focus::Focus(Controller *controller)\n> -       : Algorithm(controller)\n> -{\n> -}\n> -\n> -char const *Focus::name() const\n> -{\n> -       return NAME;\n> -}\n> -\n> -void Focus::process(StatisticsPtr &stats, Metadata *imageMetadata)\n> -{\n> -       FocusStatus status;\n> -       for (unsigned int i = 0; i < stats->focusRegions.numRegions(); i++)\n> -               status.focusMeasures[i] = stats->focusRegions.get(i).val;\n> -       status.num = stats->focusRegions.numRegions();\n> -       imageMetadata->set(\"focus.status\", status);\n> -\n> -       LOG(RPiFocus, Debug)\n> -               << \"Focus contrast measure: \"\n> -               << (status.focusMeasures[5] + status.focusMeasures[6]) / 10;\n> -}\n> -\n> -/* Register algorithm with the system. */\n> -static Algorithm *create(Controller *controller)\n> -{\n> -       return new Focus(controller);\n> -}\n> -static RegisterAlgorithm reg(NAME, &create);\n> diff --git a/src/ipa/raspberrypi/data/imx290.json b/src/ipa/raspberrypi/data/imx290.json\n> index bfb9c6093d26..ace68d0ebf1c 100644\n> --- a/src/ipa/raspberrypi/data/imx290.json\n> +++ b/src/ipa/raspberrypi/data/imx290.json\n> @@ -195,9 +195,6 @@\n>                      }\n>                  ]\n>              }\n> -        },\n> -        {\n> -            \"rpi.focus\": { }\n>          }\n>      ]\n>  }\n> \\ No newline at end of file\n> diff --git a/src/ipa/raspberrypi/data/imx296.json b/src/ipa/raspberrypi/data/imx296.json\n> index 346f5b658957..ae8722c46a85 100644\n> --- a/src/ipa/raspberrypi/data/imx296.json\n> +++ b/src/ipa/raspberrypi/data/imx296.json\n> @@ -532,9 +532,6 @@\n>                  \"strength\": 1.0,\n>                  \"limit\": 0.18\n>              }\n> -        },\n> -        {\n> -            \"rpi.focus\": { }\n>          }\n>      ]\n>  }\n> diff --git a/src/ipa/raspberrypi/data/imx296_mono.json b/src/ipa/raspberrypi/data/imx296_mono.json\n> index e9fa30c6a08d..30965b4b8d61 100644\n> --- a/src/ipa/raspberrypi/data/imx296_mono.json\n> +++ b/src/ipa/raspberrypi/data/imx296_mono.json\n> @@ -228,9 +228,6 @@\n>                  \"strength\": 1.0,\n>                  \"limit\": 0.18\n>              }\n> -        },\n> -        {\n> -            \"rpi.focus\": { }\n>          }\n>      ]\n>  }\n> diff --git a/src/ipa/raspberrypi/data/imx477.json b/src/ipa/raspberrypi/data/imx477.json\n> index bfc0774ffdfc..daffc268ff43 100644\n> --- a/src/ipa/raspberrypi/data/imx477.json\n> +++ b/src/ipa/raspberrypi/data/imx477.json\n> @@ -513,9 +513,6 @@\n>          },\n>          {\n>              \"rpi.sharpen\": { }\n> -        },\n> -        {\n> -            \"rpi.focus\": { }\n>          }\n>      ]\n>  }\n> \\ No newline at end of file\n> diff --git a/src/ipa/raspberrypi/data/imx477_noir.json b/src/ipa/raspberrypi/data/imx477_noir.json\n> index dadd97bc3d78..52d7f072d0ce 100644\n> --- a/src/ipa/raspberrypi/data/imx477_noir.json\n> +++ b/src/ipa/raspberrypi/data/imx477_noir.json\n> @@ -424,9 +424,6 @@\n>          },\n>          {\n>              \"rpi.sharpen\": { }\n> -        },\n> -        {\n> -            \"rpi.focus\": { }\n>          }\n>      ]\n>  }\n> \\ No newline at end of file\n> diff --git a/src/ipa/raspberrypi/data/imx477_scientific.json b/src/ipa/raspberrypi/data/imx477_scientific.json\n> index 17c4ed0a5e74..26c692fdbab9 100644\n> --- a/src/ipa/raspberrypi/data/imx477_scientific.json\n> +++ b/src/ipa/raspberrypi/data/imx477_scientific.json\n> @@ -474,9 +474,6 @@\n>          },\n>          {\n>              \"rpi.sharpen\": { }\n> -        },\n> -        {\n> -            \"rpi.focus\": { }\n>          }\n>      ]\n>  }\n> \\ No newline at end of file\n> diff --git a/src/ipa/raspberrypi/data/imx477_v1.json b/src/ipa/raspberrypi/data/imx477_v1.json\n> index 5bcaac67069b..d64020091efa 100644\n> --- a/src/ipa/raspberrypi/data/imx477_v1.json\n> +++ b/src/ipa/raspberrypi/data/imx477_v1.json\n> @@ -511,9 +511,6 @@\n>          },\n>          {\n>              \"rpi.sharpen\": { }\n> -        },\n> -        {\n> -            \"rpi.focus\": { }\n>          }\n>      ]\n>  }\n> \\ No newline at end of file\n> diff --git a/src/ipa/raspberrypi/data/imx708.json b/src/ipa/raspberrypi/data/imx708.json\n> index c38b2d4cf256..b9830a3bf692 100644\n> --- a/src/ipa/raspberrypi/data/imx708.json\n> +++ b/src/ipa/raspberrypi/data/imx708.json\n> @@ -512,9 +512,6 @@\n>          {\n>              \"rpi.sharpen\": { }\n>          },\n> -        {\n> -            \"rpi.focus\": { }\n> -        },\n>          {\n>              \"rpi.af\":\n>              {\n> diff --git a/src/ipa/raspberrypi/data/imx708_noir.json b/src/ipa/raspberrypi/data/imx708_noir.json\n> index 082274e34c53..075f70355cec 100644\n> --- a/src/ipa/raspberrypi/data/imx708_noir.json\n> +++ b/src/ipa/raspberrypi/data/imx708_noir.json\n> @@ -512,9 +512,6 @@\n>          {\n>              \"rpi.sharpen\": { }\n>          },\n> -        {\n> -            \"rpi.focus\": { }\n> -        },\n>          {\n>              \"rpi.af\":\n>              {\n> diff --git a/src/ipa/raspberrypi/data/imx708_wide.json b/src/ipa/raspberrypi/data/imx708_wide.json\n> index cdc61436995d..b772efee3b96 100644\n> --- a/src/ipa/raspberrypi/data/imx708_wide.json\n> +++ b/src/ipa/raspberrypi/data/imx708_wide.json\n> @@ -403,9 +403,6 @@\n>          {\n>              \"rpi.sharpen\": { }\n>          },\n> -        {\n> -            \"rpi.focus\": { }\n> -        },\n>          {\n>              \"rpi.af\":\n>              {\n> diff --git a/src/ipa/raspberrypi/data/imx708_wide_noir.json b/src/ipa/raspberrypi/data/imx708_wide_noir.json\n> index 8a7f59910833..c5f6b53dca7a 100644\n> --- a/src/ipa/raspberrypi/data/imx708_wide_noir.json\n> +++ b/src/ipa/raspberrypi/data/imx708_wide_noir.json\n> @@ -403,9 +403,6 @@\n>          {\n>              \"rpi.sharpen\": { }\n>          },\n> -        {\n> -            \"rpi.focus\": { }\n> -        },\n>          {\n>              \"rpi.af\":\n>              {\n> diff --git a/src/ipa/raspberrypi/meson.build b/src/ipa/raspberrypi/meson.build\n> index 9230e17bca22..de78cbd80f9c 100644\n> --- a/src/ipa/raspberrypi/meson.build\n> +++ b/src/ipa/raspberrypi/meson.build\n> @@ -33,7 +33,6 @@ rpi_ipa_sources = files([\n>      'controller/rpi/awb.cpp',\n>      'controller/rpi/sharpen.cpp',\n>      'controller/rpi/black_level.cpp',\n> -    'controller/rpi/focus.cpp',\n>      'controller/rpi/geq.cpp',\n>      'controller/rpi/noise.cpp',\n>      'controller/rpi/lux.cpp',\n> diff --git a/src/ipa/raspberrypi/raspberrypi.cpp b/src/ipa/raspberrypi/raspberrypi.cpp\n> index e2662938c9d3..9f3f3ac3b15d 100644\n> --- a/src/ipa/raspberrypi/raspberrypi.cpp\n> +++ b/src/ipa/raspberrypi/raspberrypi.cpp\n> @@ -50,7 +50,6 @@\n>  #include \"denoise_algorithm.h\"\n>  #include \"denoise_status.h\"\n>  #include \"dpc_status.h\"\n> -#include \"focus_status.h\"\n>  #include \"geq_status.h\"\n>  #include \"lux_status.h\"\n>  #include \"metadata.h\"\n> @@ -642,14 +641,28 @@ void IPARPi::reportMetadata(unsigned int ipaContext)\n>                                          static_cast<int32_t>(blackLevelStatus->blackLevelG),\n>                                          static_cast<int32_t>(blackLevelStatus->blackLevelB) });\n>\n> -       FocusStatus *focusStatus = rpiMetadata.getLocked<FocusStatus>(\"focus.status\");\n> -       if (focusStatus && focusStatus->num == 12) {\n> +       RPiController::FocusRegions *focusStatus =\n> +               rpiMetadata.getLocked<RPiController::FocusRegions>(\"focus.status\");\n> +       if (focusStatus) {\n>                 /*\n> -                * We get a 4x3 grid of regions by default. Calculate the average\n> -                * FoM over the central two positions to give an overall scene FoM.\n> -                * This can change later if it is not deemed suitable.\n> +                * Calculate the average FoM over the central (symmetric) positions\n> +                * to give an overall scene FoM. This can change later if it is\n> +                * not deemed suitable.\n>                  */\n> -               int32_t focusFoM = (focusStatus->focusMeasures[5] + focusStatus->focusMeasures[6]) / 2;\n> +               libcamera::Size size = focusStatus->size();\n> +               unsigned rows = size.height;\n> +               unsigned cols = size.width;\n> +\n> +               uint64_t sum = 0;\n> +               unsigned int numRegions = 0;\n> +               for (unsigned r = rows / 3; r < rows - rows / 3; ++r) {\n> +                       for (unsigned c = cols / 4; c < cols - cols / 4; ++c) {\n> +                               sum += focusStatus->get({ (int)c, (int)r }).val;\n> +                               numRegions++;\n> +                       }\n> +               }\n> +\n> +               uint32_t focusFoM = (sum / numRegions) >> 16;\n>                 libcameraMetadata_.set(controls::FocusFoM, focusFoM);\n>         }\n>\n> @@ -1428,7 +1441,6 @@ RPiController::StatisticsPtr IPARPi::fillStatistics(bcm2835_isp_stats *stats) co\n>                 statistics->focusRegions.set(i, { stats->focus_stats[i].contrast_val[1][1] / 1000,\n>                                                   stats->focus_stats[i].contrast_val_num[1][1],\n>                                                   stats->focus_stats[i].contrast_val_num[1][0] });\n> -\n>         return statistics;\n>  }\n>\n> @@ -1445,6 +1457,10 @@ void IPARPi::processStats(unsigned int bufferId, unsigned int ipaContext)\n>         Span<uint8_t> mem = it->second.planes()[0];\n>         bcm2835_isp_stats *stats = reinterpret_cast<bcm2835_isp_stats *>(mem.data());\n>         RPiController::StatisticsPtr statistics = fillStatistics(stats);\n> +\n> +       /* Save the focus stats in the metadata structure to report out later. */\n> +       rpiMetadata_[ipaContext].set(\"focus.status\", statistics->focusRegions);\n> +\n>         helper_->process(statistics, rpiMetadata);\n>         controller_.process(statistics, &rpiMetadata);\n>\n> --\n> 2.34.1\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 8A0AFC0F2A\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri, 31 Mar 2023 08:18:22 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id CC3EE62724;\n\tFri, 31 Mar 2023 10:18:21 +0200 (CEST)","from mail-ot1-x32f.google.com (mail-ot1-x32f.google.com\n\t[IPv6:2607:f8b0:4864:20::32f])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 5668E61ECB\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 31 Mar 2023 10:18:20 +0200 (CEST)","by mail-ot1-x32f.google.com with SMTP id\n\tr40-20020a05683044a800b006a14270bc7eso8094150otv.6\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 31 Mar 2023 01:18:20 -0700 (PDT)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1680250701;\n\tbh=urm/G2uAyoWRX/FOV3d6T/a6rePvN0yePDDiLAhrtck=;\n\th=References:In-Reply-To:Date:To:Subject:List-Id:List-Unsubscribe:\n\tList-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc:\n\tFrom;\n\tb=ElK12fXNnTK1OPy5B1aQQdjagagP87Ef3NPhwzQytOqrXF93DyynsVaoZVUwc3mnC\n\tCPCjV4dqJV0c5y5OPPNsgHt2kj8GnZxnmm8DOEAgVWPNc/NB3A3r51AOsBnjgLwx3B\n\t2rpmC8tTx8zD+WEOJDKK/2dm13YxrV/RToU9HbR9IKx2kOOQ3lYHjOakR17xhr041B\n\tQnnl3hknos8YKW+Ua0XcPuMvVmVPdlwvxrbLpdRVWWbPB4B5iEMK1umLEYpzbAs8mg\n\t0LJgBM4uaWG1i5YEdNeL8F/ummssLGgkGFCGedsqRxP/9Kcqf+rUuNSynywoW4m5Qz\n\thtM2PS8VJiAxQ==","v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=raspberrypi.com; s=google; t=1680250699;\n\th=cc:to:subject:message-id:date:from:in-reply-to:references\n\t:mime-version:from:to:cc:subject:date:message-id:reply-to;\n\tbh=jiPv9gS+wcnlNsN3wvWv40YjTHTGSiw7H3DNUrvYscA=;\n\tb=LrFJ9Vs9N6lhZ/jWpnrUK2VGP5Mf+a+yFtMHGzqqPuZ/Cv4B6QOGsYBRMk8mLqsL6c\n\t/Fab+PC5MbVOCwE5I1UxYVfplmyzRZnSB73Yf/0C6CGPIDd5MYDjkm0Uk/tJGV6hwLaw\n\tY8H3yRy+fotd/ymAjqooZtp+PtI0prnxyRHw0cZiGPlxeTM+t8gxiWcqwTainTobSDbs\n\tS/z4Dss+nDAjvwDArbGQrm38RSWOFYGPAV6fCN8a/trMVL9K3BcoIhjJEV4FZwlHotGx\n\txXzjNwrLBEwvVlPoWc8VnwR7jgeXpWQd6ix6s1FRy5bmQm8eT7yF4NLVn1Q8L0Oc5mEd\n\tPXgA=="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (2048-bit key; \n\tunprotected) header.d=raspberrypi.com\n\theader.i=@raspberrypi.com\n\theader.b=\"LrFJ9Vs9\"; dkim-atps=neutral","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20210112; t=1680250699;\n\th=cc:to:subject:message-id:date:from:in-reply-to:references\n\t:mime-version:x-gm-message-state:from:to:cc:subject:date:message-id\n\t:reply-to;\n\tbh=jiPv9gS+wcnlNsN3wvWv40YjTHTGSiw7H3DNUrvYscA=;\n\tb=dmItXKbsGIOlXuoOdnX2hUld2y30ktWx6oLG2U8DxmCovggdgZYjdMmODV29JcVJfJ\n\toEQaHNPkOj//sEA2vRjzB9qz+poatKsy1XSNTPwdD46ESzXZXXLiqq1OCvSCriJlmfV9\n\tQ9D8DvZJObDGHSelVSSehYxwSjBxykh1Y1rNlp0tZpFJEjvSqHPxYFVy5Ry2N7+nAt7i\n\trWuqMvpwM1mu0GNiIrhSImIPD5irT+vyeufGtTKjrgFmjsbwjFIAMoQHEtAcw20c3Mhn\n\thKn3ME6c5YaHub/BcMTJfjvKgATmNzxZbWY6p5+HOtlYX2/PxwaeIlD5sxv08Q2KBWue\n\tsYHA==","X-Gm-Message-State":"AO0yUKUViOOmXS62WFTNQ7bfO30latalh0a7VOIzVJmwQAmEL/5zNm2k\n\tbylX6hcrFu0slgMrO2DRQ5j9HUagINk3D5bgdTMntA==","X-Google-Smtp-Source":"AK7set8V0WCp1RMojQZG9KJNocx1SG6fBAAoFcJVkBhA50pULM88PNl9qgxqIFb8VfnRzrYTPM/3NmkUNu357P7zl0w=","X-Received":"by 2002:a05:6830:18cd:b0:69f:262:c524 with SMTP id\n\tv13-20020a05683018cd00b0069f0262c524mr8556760ote.4.1680250698886;\n\tFri, 31 Mar 2023 01:18:18 -0700 (PDT)","MIME-Version":"1.0","References":"<20230331072341.24851-1-naush@raspberrypi.com>\n\t<20230331072341.24851-11-naush@raspberrypi.com>","In-Reply-To":"<20230331072341.24851-11-naush@raspberrypi.com>","Date":"Fri, 31 Mar 2023 09:18:08 +0100","Message-ID":"<CAHW6GY+WUxLzwEb1X2Wrm=KLN_fzHWAsuJTYeQKkeRQJuhypMA@mail.gmail.com>","To":"Naushir Patuck <naush@raspberrypi.com>","Content-Type":"text/plain; charset=\"UTF-8\"","Subject":"Re: [libcamera-devel] [PATCH v3 10/10] ipa: raspberrypi: Generalise\n\tthe focus reporting code","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":"David Plowman via libcamera-devel <libcamera-devel@lists.libcamera.org>","Reply-To":"David Plowman <david.plowman@raspberrypi.com>","Cc":"libcamera-devel@lists.libcamera.org","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]