Show a patch.

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

{
    "id": 26736,
    "url": "https://patchwork.libcamera.org/api/patches/26736/?format=api",
    "web_url": "https://patchwork.libcamera.org/patch/26736/",
    "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": "<20260512-extensible-stats-v4-2-e0425f6171e2@ideasonboard.com>",
    "date": "2026-05-12T13:45:06",
    "name": "[v4,2/2] ipa: libipa: Introduce V4L2Stats",
    "commit_ref": null,
    "pull_url": null,
    "state": "new",
    "archived": false,
    "hash": "1b9c48d1bcc04bbcfb0d91f19e715c9c64b7db4b",
    "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/26736/mbox/",
    "series": [
        {
            "id": 5937,
            "url": "https://patchwork.libcamera.org/api/series/5937/?format=api",
            "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=5937",
            "date": "2026-05-12T13:45:04",
            "name": "ipa: libipa: Add support for V4L2 ISP statistics",
            "version": 4,
            "mbox": "https://patchwork.libcamera.org/series/5937/mbox/"
        }
    ],
    "comments": "https://patchwork.libcamera.org/api/patches/26736/comments/",
    "check": "pending",
    "checks": "https://patchwork.libcamera.org/api/patches/26736/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 792F5C32F7\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue, 12 May 2026 13:45:22 +0000 (UTC)",
            "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id E69746302C;\n\tTue, 12 May 2026 15:45:19 +0200 (CEST)",
            "from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 09D4C6271A\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 12 May 2026 15:45:17 +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 ED154243E;\n\tTue, 12 May 2026 15:45:08 +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=\"atA7xAR5\"; dkim-atps=neutral",
        "DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1778593509;\n\tbh=AzzSi2gpxUUj3ll8LbdCwMWxALEnriAqD3AoNr6nrwg=;\n\th=From:Date:Subject:References:In-Reply-To:To:Cc:From;\n\tb=atA7xAR5iC7lV6a8usDeEG+YdfFoLs72kFumAtreofLnru5wWd0NDXYqvywZFhQmT\n\t4FhbBwvuW6h/iuYo6qIeQiPePxulMipAnQ1uEpOlmtMWszK/KQ/1DclHHwjv271jhw\n\tFsdE5mCQn8mY0b+uVzs3Ll4InOhv6gJmYLg1a44k=",
        "From": "Jacopo Mondi <jacopo.mondi@ideasonboard.com>",
        "Date": "Tue, 12 May 2026 15:45:06 +0200",
        "Subject": "[PATCH v4 2/2] ipa: libipa: Introduce V4L2Stats",
        "MIME-Version": "1.0",
        "Content-Type": "text/plain; charset=\"utf-8\"",
        "Content-Transfer-Encoding": "7bit",
        "Message-Id": "<20260512-extensible-stats-v4-2-e0425f6171e2@ideasonboard.com>",
        "References": "<20260512-extensible-stats-v4-0-e0425f6171e2@ideasonboard.com>",
        "In-Reply-To": "<20260512-extensible-stats-v4-0-e0425f6171e2@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=10305;\n\ti=jacopo.mondi@ideasonboard.com; h=from:subject:message-id;\n\tbh=AzzSi2gpxUUj3ll8LbdCwMWxALEnriAqD3AoNr6nrwg=;\n\tb=owEBbQKS/ZANAwAKAXI0Bo8WoVY8AcsmYgBqAy7rvXhrwdOHpcDo2sQGsto2e6kiCs6Ym4uLG\n\tHn38w7bGtyJAjMEAAEKAB0WIQS1xD1IgJogio9YOMByNAaPFqFWPAUCagMu6wAKCRByNAaPFqFW\n\tPPK3D/4qytS7orTk9wL+1QJ3ZRxCqWI99uNRzRHub4/boDHewoDmNksISsDSsSDrJHVL52ON3G/\n\t/HWgyfy1IiPIJ3LPSZX252yHZCJLQlcIsPgYo/3FPNK6wBgqzf+w840fLUvSqETAw2DNP7Gb7Fl\n\ttAh8bC3yn3rXkAX+EqXYLwCe26R7/FqSA7HQnBzE/qm0XThRRQ0pUMrrZATAt6xkKNjt6B5Ez4j\n\tvel2WYZMqFP8aufv29+YyBeFJZqOeSmosg7poJo5S88utqwZiDgOtJ0c3X8DiwQYMAW6kcwLFrk\n\taVr9AgUPKir1pigGqMZvjY2+LuN64xijxmHxfZ66gOGMFXfUBlw9hVJMRx9ow8igJsXk/oed+hr\n\tNuM9Bx8/u1u7DUpCN72zn832LRP4INiwNfYqTyAAkik+MMzZJlEyJ8761S2vDak9OWKC/2lKX6F\n\txAAXnBo06k2e3vLObp6+184AiCAUtLUStKRSAH1+srtD88GPBeY03ua+7F4qNVkv4oE8eAZDbbO\n\tXy13JgCoB/D7ticPR/5PLL2PngtKQZPJkoYWwP7k02BpzJhMmabCz0J2j6YmT2jxX4lhGs5bSEi\n\tCd/JEDoqvf37pSlTDvzLS8lUubJy3q+Xqi2a/m8AUV1vAQQR14/E9Tvay6V6Bk37eEJoYo7XMtY\n\tWCcIeo6QSeb1JfQ==",
        "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 | 243 ++++++++++++++++++++++++++++++++++++++++++\n src/ipa/libipa/v4l2_stats.h   |  67 ++++++++++++\n 3 files changed, 312 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..7aa2add2f265\n--- /dev/null\n+++ b/src/ipa/libipa/v4l2_stats.cpp\n@@ -0,0 +1,243 @@\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 a block type to\n+ * the memory location of a 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 return 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+ * \\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+ * // 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)\n+ * \t\t: V4L2Stats(data, V4L2_ISP_VERSION_V1)\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+ *\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 or nullptr if the block is\n+ * not present in the statistics buffer\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": [
        "v4",
        "2/2"
    ]
}