{"id":26924,"url":"https://patchwork.libcamera.org/api/patches/26924/?format=json","web_url":"https://patchwork.libcamera.org/patch/26924/","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":"<20260618-rppx1-ipa-v1-9-32337264cfcd@ideasonboard.com>","date":"2026-06-18T10:18:48","name":"[09/14] ipa: libipa: Introduce V4L2Stats","commit_ref":null,"pull_url":null,"state":"new","archived":false,"hash":"e3b995317c108f58dd58e7badf6ab9ff10fc7606","submitter":{"id":143,"url":"https://patchwork.libcamera.org/api/people/143/?format=json","name":"Jacopo Mondi","email":"jacopo.mondi@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/26924/mbox/","series":[{"id":6003,"url":"https://patchwork.libcamera.org/api/series/6003/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=6003","date":"2026-06-18T10:18:39","name":"libcamera: Add support for R-Car Gen4 and RPP-X1 ISP","version":1,"mbox":"https://patchwork.libcamera.org/series/6003/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/26924/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/26924/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 EEAF5C328C\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu, 18 Jun 2026 10:19:09 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 3140E629E0;\n\tThu, 18 Jun 2026 12:19:08 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 131FF629E0\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 18 Jun 2026 12:18:59 +0200 (CEST)","from [192.168.125.177] (mob-109-113-4-199.net.vodafone.it\n\t[109.113.4.199])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 99FCC2689;\n\tThu, 18 Jun 2026 12:18:23 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"k3O46RE+\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1781777904;\n\tbh=BCwNM9Uwi/BjvuMp45FftguRa2jcveo6lNW3w56Hse0=;\n\th=From:Date:Subject:References:In-Reply-To:To:Cc:From;\n\tb=k3O46RE+6GcoE5KQBTuRXu5Rz1H8WMPA5laElfkRYfla8XAb86LYFrOPlI4tzOtPS\n\tkJR/0EvRsCwSHw/hLHD1ZldOoEuqz9DKNOCMAVZ7XptE2aIysSIXEVwArywEKl4dr5\n\tCvr5B7piygou7qyGT5XyBpF9Lp+v33y0KtYWsg+I=","From":"Jacopo Mondi <jacopo.mondi@ideasonboard.com>","Date":"Thu, 18 Jun 2026 12:18:48 +0200","Subject":"[PATCH 09/14] ipa: libipa: Introduce V4L2Stats","MIME-Version":"1.0","Content-Type":"text/plain; charset=\"utf-8\"","Content-Transfer-Encoding":"7bit","Message-Id":"<20260618-rppx1-ipa-v1-9-32337264cfcd@ideasonboard.com>","References":"<20260618-rppx1-ipa-v1-0-32337264cfcd@ideasonboard.com>","In-Reply-To":"<20260618-rppx1-ipa-v1-0-32337264cfcd@ideasonboard.com>","To":"=?utf-8?q?Niklas_S=C3=B6derlund?=\n\t<niklas.soderlund+renesas@ragnatech.se>, \n\tlibcamera-devel@lists.libcamera.org","Cc":"Jacopo Mondi <jacopo.mondi@ideasonboard.com>, \n\tKieran Bingham <kieran.bingham@ideasonboard.com>","X-Mailer":"b4 0.14.3","X-Developer-Signature":"v=1; a=openpgp-sha256; l=10203;\n\ti=jacopo.mondi@ideasonboard.com; h=from:subject:message-id;\n\tbh=BCwNM9Uwi/BjvuMp45FftguRa2jcveo6lNW3w56Hse0=;\n\tb=owEBbQKS/ZANAwAKAXI0Bo8WoVY8AcsmYgBqM8YKV0+6SIWv5CC9K4hkQvoXCP0MHnMbV9LuU\n\tx4/iGHqHZOJAjMEAAEKAB0WIQS1xD1IgJogio9YOMByNAaPFqFWPAUCajPGCgAKCRByNAaPFqFW\n\tPLb8D/44Ie784HDZHLCBhSOuHKrt+TGZH7J5cKy15U249Tg3vBozg+sffG7r5NTh9tle+smX0bU\n\t5y+v/hWwPHiNq11vC8aFdK8CvEgoxhz2DFOvWIZf91MJ0qS9LeFuxBiMjCe2DG/Zw7IAFqD8917\n\tuYNtwGDfOvKkXM3PNfpKiDdX6DtDGLkdZq3NLu6kvth7gbnFyGqY10wZgyhZONdjX5Zk7VReYxp\n\tw1c4CjwwQDnppxYX9HuLTzxg86r8xvF+WR2/JV1G7yJHt5F/oPdCD4Lh1I0lJ12G608kMi2EIpw\n\t6wY7p94Uf4NwGTtGCzhRVMdGC9pMMyT3aF8sUnEASJ9s2DG/EtN5d/ntc5Nxq9QYwGCIArNjtXr\n\ttX+M6d0e/elvQYPzhO6RAnqiGdIEYFxOIcxHoqUdbLRRBQWEYxaLIJoqpBmRbaOoNlzfyVxhMdi\n\td+tzAT7eCy3T723fPFOkUies6p3FUWxqRAEVlace37nfXVxTbzziTroyW3oiSyhyA81RWx9Z5tt\n\ttb0EeY4QKGVQ6EOrWMDV0h3IJtJDilDrjQpyv2ZGO0rn3y37PKTWqqLZPrhLGrG0iJzpBW6JMPX\n\txqMhsfOKjesrNTrhJxw4dBWuIT/ODoGIBh69vKJm9nnA10VREQTvki0hA279NhQKb1tkVrEB0m6\n\tmyEsIQMj30g42kA==","X-Developer-Key":"i=jacopo.mondi@ideasonboard.com; a=openpgp;\n\tfpr=72392EDC88144A65C701EA9BA5826A2587AD026B","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":"Add a V4L2Stats class similar in spirit to the existing V4L2Params\nclass to allow IPA modules to easily sub-class it to access ISP\nstatistics blocks serialized into a v4l2_isp_buffer.\n\nSigned-off-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>\nReviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n---\n src/ipa/libipa/meson.build    |   2 +\n src/ipa/libipa/v4l2_stats.cpp | 241 ++++++++++++++++++++++++++++++++++++++++++\n src/ipa/libipa/v4l2_stats.h   |  67 ++++++++++++\n 3 files changed, 310 insertions(+)","diff":"diff --git a/src/ipa/libipa/meson.build b/src/ipa/libipa/meson.build\nindex edf8eabd8b78..635abad778e6 100644\n--- a/src/ipa/libipa/meson.build\n+++ b/src/ipa/libipa/meson.build\n@@ -20,6 +20,7 @@ libipa_headers = files([\n     'pwl.h',\n     'quantized.h',\n     'v4l2_params.h',\n+    'v4l2_stats.h',\n ])\n \n libipa_sources = files([\n@@ -42,6 +43,7 @@ libipa_sources = files([\n     'pwl.cpp',\n     'quantized.cpp',\n     'v4l2_params.cpp',\n+    'v4l2_stats.cpp',\n ])\n \n libipa_includes = include_directories('..')\ndiff --git a/src/ipa/libipa/v4l2_stats.cpp b/src/ipa/libipa/v4l2_stats.cpp\nnew file mode 100644\nindex 000000000000..64f546d85069\n--- /dev/null\n+++ b/src/ipa/libipa/v4l2_stats.cpp\n@@ -0,0 +1,241 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2026, Ideas On Board\n+ *\n+ * V4L2 Statistics\n+ */\n+\n+#include \"v4l2_stats.h\"\n+\n+#include <libcamera/base/log.h>\n+\n+namespace libcamera {\n+\n+namespace ipa {\n+\n+LOG_DEFINE_CATEGORY(V4L2Stats)\n+\n+/**\n+ * \\file v4l2_stats.cpp\n+ * \\brief Helper class to handle an ISP statistics buffer compatible with\n+ * the generic V4L2 ISP format\n+ *\n+ * The Linux kernel defines a generic buffer format for ISP statistics.\n+ * The format describes a serialisation method that allows userspace to\n+ * access statistics data from a binary buffer.\n+ *\n+ * The V4L2Stats class implements support for the V4L2 ISP statistics buffer\n+ * format and allows users to retrieve an ISP statistics block.\n+ *\n+ * IPA implementations using these helpers should define an enumeration of ISP\n+ * blocks supported by the IPA module and use a set of common abstractions to\n+ * help their derived implementation of V4L2Stats translate the enumerated ISP\n+ * block identifiers to the actual type of the statistics data as defined by\n+ * the kernel interface.\n+ */\n+\n+/**\n+ * \\class V4L2StatsBase\n+ * \\brief Base class for V4L2Stats\n+ *\n+ * The V4L2StatsBase is an integral part of V4L2Stats. It serves as a\n+ * container for all code that does not depend on the V4L2Stats template\n+ * arguments, to avoid duplicate copies of inline code.\n+ */\n+\n+/**\n+ * \\brief Construct an instance of V4L2StatsBase\n+ * \\param[in] data Reference to the v4l2-buffer memory mapped area\n+ * \\param[in] version The ISP parameters version the implementation supports\n+ *\n+ * Parse the statistics buffer and construct a cache that maps the block type to\n+ * the memory location of the statistics block in the buffer.\n+ *\n+ * After construction users of this class shall check the validity of the\n+ * constructed instance using operator bool().\n+ */\n+V4L2StatsBase::V4L2StatsBase(Span<uint8_t> data, unsigned int version)\n+\t: data_(data), valid_(false)\n+{\n+\tconst struct v4l2_isp_buffer *stats =\n+\t\treinterpret_cast<const struct v4l2_isp_buffer *>(data_.data());\n+\n+\tif (data_.size() - sizeof(*stats) < stats->data_size) {\n+\t\tLOG(V4L2Stats, Error)\n+\t\t\t<< \"Stats buffer size mismatch: \" << stats->data_size;\n+\t\treturn;\n+\t}\n+\n+\tif (version != stats->version) {\n+\t\tLOG(V4L2Stats, Error)\n+\t\t\t<< \"Unsupported v4l2-isp version: \" << stats->version;\n+\t\treturn;\n+\t}\n+\n+\t/* Construct the cache for easier lookup. */\n+\tsize_t left = stats->data_size;\n+\tconst __u8 *d = stats->data;\n+\n+\twhile (left > 0) {\n+\t\tconst struct v4l2_isp_block_header *header =\n+\t\t\treinterpret_cast<const struct v4l2_isp_block_header *>(d);\n+\n+\t\tif (left < sizeof(*header) || header->size < sizeof(*header)) {\n+\t\t\tLOG(V4L2Stats, Error)\n+\t\t\t\t<< \"Block type \" << header->type\n+\t\t\t\t<< \" size is not valid\";\n+\t\t\treturn;\n+\t\t}\n+\n+\t\tif (left < header->size) {\n+\t\t\tLOG(V4L2Stats, Error)\n+\t\t\t\t<< \"Not enough space for block type \" << header->type;\n+\t\t\treturn;\n+\t\t}\n+\n+\t\tauto [it, inserted] = cache_.try_emplace(header->type, d, header->size);\n+\t\tif (!inserted) {\n+\t\t\tLOG(V4L2Stats, Error)\n+\t\t\t\t<< \"Duplicated block type \" << header->type;\n+\t\t\treturn;\n+\t\t}\n+\n+\t\td += header->size;\n+\t\tleft -= header->size;\n+\t}\n+\n+\tvalid_ = true;\n+}\n+\n+/**\n+ * \\brief Retrieve an ISP statistics block a returns a reference to it\n+ * \\param[in] blockType The kernel-defined ISP block identifier, used to\n+ * identify the block header\n+ * \\param[in] blockSize The ISP statistics block size, for validation\n+ *\n+ * Retrieve a span to the statistics block memory location by accessing the\n+ * cache built at class construction time.\n+ *\n+ * \\return The memory location of the ISP statistics block, or an empty Span\n+ * if \\a blockType is not supported\n+ */\n+Span<const uint8_t> V4L2StatsBase::block(unsigned int blockType, size_t blockSize) const\n+{\n+\tconst auto it = cache_.find(blockType);\n+\tif (it == cache_.end()) {\n+\t\tLOG(V4L2Stats, Error) << \"Unsupported stats block type: \"\n+\t\t\t\t      << blockType;\n+\t\treturn {};\n+\t}\n+\n+\tconst struct v4l2_isp_block_header *header =\n+\t\treinterpret_cast<const struct v4l2_isp_block_header *>(it->second.data());\n+\tif (header->size != blockSize) {\n+\t\tLOG(V4L2Stats, Error)\n+\t\t\t<< \"Block type \" << blockType\n+\t\t\t<< \" size mistmatch: expected \"\n+\t\t\t<< blockSize << \" got:\"\n+\t\t\t<< header->size;\n+\t\treturn {};\n+\t}\n+\n+\treturn it->second;\n+}\n+\n+/**\n+ * \\fn V4L2StatsBase::operator bool()\n+ * \\brief Retrieve if a statistics block is valid\n+ * \\return True if the statistics block is valid, false otherwise\n+ */\n+\n+/**\n+ * \\class V4L2Stats\n+ * \\brief Helper class that represent an ISP statistics buffer\n+ *\n+ * This class represents an ISP statistics buffer. It is constructed with a\n+ * reference to the memory mapped buffer that has been dequeued from the ISP\n+ * driver.\n+ *\n+ * This class is templated with the type of the enumeration of ISP blocks that\n+ * each IPA module is expected to support. IPA modules are expected to derive\n+ * this class by providing a 'stats_traits' type that helps the class associate\n+ * a block type with the actual memory area that represents the ISP statistics\n+ * block.\n+ *\n+ * \\code{.cpp}\n+ *\n+ * // Define the supported ISP statistics blocks\n+ * enum class myISPStats {\n+ *\tAgc,\n+ *\tAwb,\n+ *\t...\n+ * };\n+ *\n+ * // Maps the C++ enum type to the kernel enum type and concrete parameter type\n+ * template<myISPStats B>\n+ * struct block_type {\n+ * };\n+ *\n+ * template<>\n+ * struct block_type<myISPStats::Agc> {\n+ *\tusing type = struct my_isp_kernel_stats_type_agc;\n+ *\tstatic constexpr kernel_enum_type blockType = MY_ISP_STATS_TYPE_AGC;\n+ * };\n+ *\n+ * template<>\n+ * struct block_type<myISPStats::Awb> {\n+ *\tusing type = struct my_isp_kernel_stats_type_awb;\n+ *\tstatic constexpr kernel_enum_type blockType = MY_ISP_STATS_TYPE_AWB;\n+ * };\n+ *\n+ *\n+ * // Convenience type to associate a block id to the 'block_type' overload\n+ * struct stats_traits {\n+ * \tusing id_type = myISPStats;\n+ * \ttemplate<id_type Id> using id_to_details = block_type<Id>;\n+ * };\n+ *\n+ * ...\n+ *\n+ * // Derive the V4L2Stats class by providing stats_traits\n+ * class MyISPStats : public V4L2Stats<stats_traits>\n+ * {\n+ * public:\n+ * \tMyISPStats::MyISPStats(Span<uint8_t> data, unsigned int version)\n+ * \t\t: V4L2Stats(data, version)\n+ * \t{\n+ * \t}\n+ * };\n+ *\n+ * \\endcode\n+ *\n+ * Users of this class can then easily access an ISP statistics block using the\n+ * block() function.\n+ *\n+ * \\code{.cpp}\n+ *\n+ * MyISPStats stats(data);\n+ *\n+ * auto awb = stats.block<myISPStats::AWB>();\n+ * auto mean_r = awb->mean_r;\n+ * auto mean_g = awb->mean_g;\n+ * auto mean_b = awb->mean_b;\n+ * \\endcode\n+ */\n+\n+/**\n+ * \\fn V4L2Stats::V4L2Stats()\n+ * \\brief Construct an instance of V4L2Stats\n+ * \\param[in] data Reference to the v4l2-buffer memory mapped area\n+ * \\param[in] version The expected V4L2 ISP serialization format version\n+ */\n+\n+/**\n+ * \\fn V4L2Stats::block() const\n+ * \\brief Retrieve a pointer to an ISP statistics block\n+ * \\return A pointer to the ISP statistics block\n+ */\n+\n+} /* namespace ipa */\n+\n+} /* namespace libcamera */\ndiff --git a/src/ipa/libipa/v4l2_stats.h b/src/ipa/libipa/v4l2_stats.h\nnew file mode 100644\nindex 000000000000..b96395e5f551\n--- /dev/null\n+++ b/src/ipa/libipa/v4l2_stats.h\n@@ -0,0 +1,67 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2026, Ideas On Board\n+ *\n+ * V4L2 ISP Statistics\n+ */\n+\n+#pragma once\n+\n+#include <map>\n+#include <stdint.h>\n+\n+#include <linux/media/v4l2-isp.h>\n+\n+#include <libcamera/base/span.h>\n+\n+namespace libcamera {\n+\n+namespace ipa {\n+\n+class V4L2StatsBase\n+{\n+public:\n+\tV4L2StatsBase(Span<uint8_t> data, unsigned int version);\n+\n+\tSpan<const uint8_t> block(unsigned int blockType, size_t blockSize) const;\n+\tconstexpr explicit operator bool()\n+\t{\n+\t\treturn valid_;\n+\t}\n+\n+private:\n+\tstd::map<uint16_t, Span<const uint8_t>> cache_;\n+\tSpan<uint8_t> data_;\n+\tbool valid_;\n+};\n+\n+template<typename Traits>\n+class V4L2Stats : public V4L2StatsBase\n+{\n+public:\n+\tstatic_assert(std::is_same_v<std::underlying_type_t<typename Traits::id_type>, uint16_t>);\n+\n+\tV4L2Stats(Span<uint8_t> data, unsigned int version)\n+\t\t: V4L2StatsBase(data, version)\n+\t{\n+\t}\n+\n+\ttemplate<typename Traits::id_type Id>\n+\tconst typename Traits::template id_to_details<Id>::type *\n+\tblock() const\n+\t{\n+\t\tusing Details = typename Traits::template id_to_details<Id>;\n+\n+\t\tusing Type = typename Details::type;\n+\t\tconstexpr auto kernelId = Details::blockType;\n+\n+\t\tauto data = V4L2StatsBase::block(kernelId, sizeof(Type));\n+\n+\t\treturn data.size() > 0 ?\n+\t\t       reinterpret_cast<const Type *>(data.data()) : nullptr;\n+\t}\n+};\n+\n+} /* namespace ipa */\n+\n+} /* namespace libcamera */\n","prefixes":["09/14"]}