Patch Detail
Show a patch.
GET /api/patches/26696/?format=api
{ "id": 26696, "url": "https://patchwork.libcamera.org/api/patches/26696/?format=api", "web_url": "https://patchwork.libcamera.org/patch/26696/", "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": "<20260508-extensible-stats-v3-2-f2174ab4e124@ideasonboard.com>", "date": "2026-05-08T16:48:52", "name": "[v3,2/2] ipa: libipa: Introduce V4L2Stats", "commit_ref": null, "pull_url": null, "state": "superseded", "archived": false, "hash": "e8354990a5a6f5e98f086f90b33e684017e9f43b", "submitter": { "id": 143, "url": "https://patchwork.libcamera.org/api/people/143/?format=api", "name": "Jacopo Mondi", "email": "jacopo.mondi@ideasonboard.com" }, "delegate": null, "mbox": "https://patchwork.libcamera.org/patch/26696/mbox/", "series": [ { "id": 5925, "url": "https://patchwork.libcamera.org/api/series/5925/?format=api", "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=5925", "date": "2026-05-08T16:48:50", "name": "ipa: libipa: Add support for V4L2 ISP statistics", "version": 3, "mbox": "https://patchwork.libcamera.org/series/5925/mbox/" } ], "comments": "https://patchwork.libcamera.org/api/patches/26696/comments/", "check": "pending", "checks": "https://patchwork.libcamera.org/api/patches/26696/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 EB85ABE173\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri, 8 May 2026 16:49:05 +0000 (UTC)", "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 5271C62FEA;\n\tFri, 8 May 2026 18:49:03 +0200 (CEST)", "from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 19DDB62FEA\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 8 May 2026 18:49:00 +0200 (CEST)", "from [192.168.1.7] (net-93-65-100-155.cust.vodafonedsl.it\n\t[93.65.100.155])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 185DA1B0C;\n\tFri, 8 May 2026 18:48:55 +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=\"p9wZopgb\"; dkim-atps=neutral", "DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1778258935;\n\tbh=szU5xhnmuC2awTXjEVJDengDDKnLwzecQQwbP1zkErI=;\n\th=From:Date:Subject:References:In-Reply-To:To:Cc:From;\n\tb=p9wZopgbRf4MSYmc8BKx7IqwXIiC+EvzviSt7BHch/0F5RnQ+p237hTcikWL4yefY\n\tSIiWzdMmI5HrZe3SnCPUWcubTk3kZPicvLkTsaSFdGNzPqN4VUxgjQTvGz1gGlTDty\n\tpssxLJLD66ke8jznOV5F7BliAWDLk8mRf5fMlnrE=", "From": "Jacopo Mondi <jacopo.mondi@ideasonboard.com>", "Date": "Fri, 08 May 2026 18:48:52 +0200", "Subject": "[PATCH v3 2/2] ipa: libipa: Introduce V4L2Stats", "MIME-Version": "1.0", "Content-Type": "text/plain; charset=\"utf-8\"", "Content-Transfer-Encoding": "7bit", "Message-Id": "<20260508-extensible-stats-v3-2-f2174ab4e124@ideasonboard.com>", "References": "<20260508-extensible-stats-v3-0-f2174ab4e124@ideasonboard.com>", "In-Reply-To": "<20260508-extensible-stats-v3-0-f2174ab4e124@ideasonboard.com>", "To": "libcamera-devel@lists.libcamera.org, \n\tAntoine Bouyer <antoine.bouyer@nxp.com>", "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=10638;\n\ti=jacopo.mondi@ideasonboard.com; h=from:subject:message-id;\n\tbh=szU5xhnmuC2awTXjEVJDengDDKnLwzecQQwbP1zkErI=;\n\tb=owEBbQKS/ZANAwAKAXI0Bo8WoVY8AcsmYgBp/hP6Mt4n3s63HSOLO7wLohNhwY6SGLQORfiVE\n\tYx6tLqIWi+JAjMEAAEKAB0WIQS1xD1IgJogio9YOMByNAaPFqFWPAUCaf4T+gAKCRByNAaPFqFW\n\tPKkHD/4g+TAqfOWs2lmzAdD78AOeJ3cFc/9w2CBmfoF6EGBBZBj6z8Wsf4J1h2UZnDMtJnEGFWA\n\tH2gfTBW18+jycoQtUk6cKYx4IiT/lJEWQLz3vwhNU3DoDdmRRH/fBs/1Ej2sqmEH2BsfGOndD3K\n\tF4raT6C9PMdgtw0I5SfJK2N1YtZuRwOy7MzNinFgAM6fXXknrVG6OHt/1k77n5B34XsBX3hZz3w\n\tFB6R3ZUcW/2RDYd4vSBbY1QE7G1B5QNuCYOEVDC++CoMdEU7gjvkY771RweZFZ9uM3+toj176ex\n\tQ8jD7oo0sDQ/KLh9TknVTWSxlbZVrAnQzPgrbWl49R2aMW+spGmhJ27pQqfxzA+RtRRnVQ6lbMl\n\t97s1wS8mzsZrA7KpOvj/e/VfD1o6gV4cgqg+UEuMGMKcFjkEx5Ajqopq9pq2VE1rEowajS8FEGQ\n\tZd5rqBFi7+tYTKxl0cXlUpDTGZKb3XuX1cP40605YWRW9x4jylsM/BmroRaE1LZ5Er7tq0KlfRl\n\tht+PyZfbp9tSOnF7Bwo5QgwTgVQIp9ZnASnUkbkhIVF+brbKHoeXw/XUxw8zBSOs4TOA1y0GrbN\n\to9wX/pClbCrVUbREnuXYnK/xY3JWG+mJ4dCgIHBUEIA+jwv5e+ODwEVA8nrxnink6fPbHYZEMiy\n\t8q8sc6rYz9Hz2rQ==", "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 | 256 ++++++++++++++++++++++++++++++++++++++++++\n src/ipa/libipa/v4l2_stats.h | 67 +++++++++++\n 3 files changed, 325 insertions(+)", "diff": "diff --git a/src/ipa/libipa/meson.build b/src/ipa/libipa/meson.build\nindex 963c5ee73063..16f4b095f220 100644\n--- a/src/ipa/libipa/meson.build\n+++ b/src/ipa/libipa/meson.build\n@@ -19,6 +19,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@@ -40,6 +41,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..0f807b2431a4\n--- /dev/null\n+++ b/src/ipa/libipa/v4l2_stats.cpp\n@@ -0,0 +1,256 @@\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+#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 (header->size > stats->data + stats->data_size - d) {\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\tcache_[header->type] = Span<const uint8_t>(d, header->size);\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 V4L2StatsBase has been successfully constructed\n+ * \\return True if the instance has been constructed successfully, false\n+ * otherwise\n+ */\n+\n+/**\n+ * \\var V4L2StatsBase::cache_\n+ * \\brief Map the statistics block types to the memory area where stats are\n+ * located\n+ */\n+\n+/**\n+ * \\var V4L2StatsBase::data_\n+ * \\brief The ISP statistics buffer memory\n+ */\n+\n+/**\n+ * \\var V4L2StatsBase::valid_\n+ * \\brief Flag to signal valid construction of a V4L2StatsBase instance\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; return an\n+ * // std::optional to make it easy to check if the class has been constructed\n+ * // correctly\n+ * class MyISPStats : public V4L2Stats<stats_traits>\n+ * {\n+ * public:\n+ *\tstatic std::optional<MyISPStats> from(Span<uint8_t> data)\n+ *\t{\n+ *\t\tMyISPStats s(data, V4L2_ISP_VERSION_V..);\n+ *\n+ *\t\treturn s ? std::make_optional(s) : std::nullopt;\n+ *\t}\n+ *\n+ * private:\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..88e23f363f36\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+protected:\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+\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+\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+protected:\n+\tV4L2Stats(Span<uint8_t> data, unsigned int version)\n+\t\t: V4L2StatsBase(data, version)\n+\t{\n+\t}\n+};\n+\n+} /* namespace ipa */\n+\n+} /* namespace libcamera */\n", "prefixes": [ "v3", "2/2" ] }