Patch Detail
Show a patch.
GET /api/1.1/patches/21011/?format=api
{ "id": 21011, "url": "https://patchwork.libcamera.org/api/1.1/patches/21011/?format=api", "web_url": "https://patchwork.libcamera.org/patch/21011/", "project": { "id": 1, "url": "https://patchwork.libcamera.org/api/1.1/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": "<20240826152224.362773-2-stefan.klug@ideasonboard.com>", "date": "2024-08-26T15:21:59", "name": "[v1,1/8] ipa: libipa: Add generic Interpolator class", "commit_ref": null, "pull_url": null, "state": "superseded", "archived": false, "hash": "b488073fd3f7334a033ebf7dea8d0d897b1bbbe8", "submitter": { "id": 184, "url": "https://patchwork.libcamera.org/api/1.1/people/184/?format=api", "name": "Stefan Klug", "email": "stefan.klug@ideasonboard.com" }, "delegate": null, "mbox": "https://patchwork.libcamera.org/patch/21011/mbox/", "series": [ { "id": 4540, "url": "https://patchwork.libcamera.org/api/1.1/series/4540/?format=api", "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=4540", "date": "2024-08-26T15:21:58", "name": "Implement polynomial lsc loader", "version": 1, "mbox": "https://patchwork.libcamera.org/series/4540/mbox/" } ], "comments": "https://patchwork.libcamera.org/api/patches/21011/comments/", "check": "pending", "checks": "https://patchwork.libcamera.org/api/patches/21011/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 5F052C323E\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon, 26 Aug 2024 15:22:35 +0000 (UTC)", "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 07DEA63417;\n\tMon, 26 Aug 2024 17:22:35 +0200 (CEST)", "from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 2CC5B633D3\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 26 Aug 2024 17:22:32 +0200 (CEST)", "from ideasonboard.com (unknown\n\t[IPv6:2a00:6020:448c:6c00:58b7:f3d:c9d4:defa])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id DB207480;\n\tMon, 26 Aug 2024 17:21:25 +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=\"V7WPcPLE\"; dkim-atps=neutral", "DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1724685686;\n\tbh=Qh1aXpOeDQjgR7o+JU8yS86Fx1ZfxJemb36DeYS2v8A=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=V7WPcPLEdqCbquQoegjRgwhNjA7JY+jgNN3NxQwqADVOp00wlJF6oGD4czcRDOrCc\n\tDyja5GHXxxy843PyFApg0+/fIbS5zQ3GxSN/bwiIj73rWo37iMdqVzMjtTSbz+2wor\n\ten+uJCyUb+pvshedaOLY2ZjPUoQVXwr+gV/sAok8=", "From": "Stefan Klug <stefan.klug@ideasonboard.com>", "To": "libcamera-devel@lists.libcamera.org", "Cc": "Stefan Klug <stefan.klug@ideasonboard.com>", "Subject": "[PATCH v1 1/8] ipa: libipa: Add generic Interpolator class", "Date": "Mon, 26 Aug 2024 17:21:59 +0200", "Message-ID": "<20240826152224.362773-2-stefan.klug@ideasonboard.com>", "X-Mailer": "git-send-email 2.43.0", "In-Reply-To": "<20240826152224.362773-1-stefan.klug@ideasonboard.com>", "References": "<20240826152224.362773-1-stefan.klug@ideasonboard.com>", "MIME-Version": "1.0", "Content-Transfer-Encoding": "8bit", "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": "The MatrixInterpolator is great for interpolation of matrices for different\ncolor temperatures. It has however one limitation - it can only handle\nmatrices. For LSC it would be great to interpolate the LSC tables (or even\npolynomials) using the same approach. Add a generic Interpolator class based on\nthe existing MatrixInterpolator. This calss can be adapted to any other type\nusing partial template specialization.\n\nSigned-off-by: Stefan Klug <stefan.klug@ideasonboard.com>\n---\n src/ipa/libipa/interpolator.cpp | 139 ++++++++++++++++++++++++++++++++\n src/ipa/libipa/interpolator.h | 139 ++++++++++++++++++++++++++++++++\n src/ipa/libipa/meson.build | 2 +\n 3 files changed, 280 insertions(+)\n create mode 100644 src/ipa/libipa/interpolator.cpp\n create mode 100644 src/ipa/libipa/interpolator.h", "diff": "diff --git a/src/ipa/libipa/interpolator.cpp b/src/ipa/libipa/interpolator.cpp\nnew file mode 100644\nindex 000000000000..98462d1509da\n--- /dev/null\n+++ b/src/ipa/libipa/interpolator.cpp\n@@ -0,0 +1,139 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2024, Paul Elder <paul.elder@ideasonboard.com>\n+ *\n+ * Helper class for interpolating maps of matrices\n+ */\n+#include \"interpolator.h\"\n+\n+#include <algorithm>\n+#include <string>\n+\n+#include <libcamera/base/log.h>\n+\n+#include \"libcamera/internal/yaml_parser.h\"\n+\n+#include \"interpolator.h\"\n+\n+/**\n+ * \\file interpolator.h\n+ * \\brief Helper class for linear interpolating a set of objects\n+ */\n+\n+namespace libcamera {\n+\n+LOG_DEFINE_CATEGORY(Interpolator)\n+\n+namespace ipa {\n+\n+/**\n+ * \\class Interpolator\n+ * \\brief Class for storing, retrieving, and interpolating objects\n+ * \\tparam T Type of objects stored in the interpolator\n+ *\n+ * The main use case is to pass a map from color temperatures to corresponding\n+ * objects (eg. matrices for color correction), and then requesting a\n+ * interpolated object for a specific color temperature. This class will\n+ * abstract away the interpolation portion.\n+ */\n+\n+/**\n+ * \\fn Interpolator::Interpolator()\n+ * \\brief Construct a empty interpolator\n+ */\n+\n+/**\n+ * \\fn Interpolator::Interpolator(const std::map<unsigned int, T> &data)\n+ * \\brief Construct a interpolator from a map of objects\n+ * \\param data Map from which to construct the interpolator\n+ */\n+\n+/**\n+ * \\fn Interpolator::Interpolator(const std::map<unsigned int, T> &&data)\n+ * \\brief Construct a interpolator from a map of objects\n+ * \\param data Map from which to construct the interpolator\n+ */\n+\n+/**\n+ * \\fn int Interpolator<T>::readYaml(const libcamera::YamlObject &yaml,\n+\t\t const std::string &key_name,\n+\t\t const std::string &value_name)\n+ * \\brief Initialize an Interpolator instance from yaml\n+ * \\tparam T Type of data stored in the interpolator\n+ * \\param[in] yaml The yaml object that contains the map of unsigned integers to objects\n+ * \\param[in] key_name The name of the key in the yaml object\n+ * \\param[in] value_name The name of the matrix in the yaml object\n+ *\n+ * The yaml object is expected to be a list of maps. Each map has two or more\n+ * pairs: one of \\a key_name to the key value (usually color temperature), and\n+ * one or more of \\a value_name to the object. This is a bit difficult to\n+ * explain, so here is an example (in python, as it is easier to parse than\n+ * yaml):\n+ * [\n+ * {\n+ * 'ct': 2860,\n+ * 'ccm': [ 2.12089, -0.52461, -0.59629,\n+ * -0.85342, 2.80445, -0.95103,\n+ * -0.26897, -1.14788, 2.41685 ],\n+ * 'offsets': [ 0, 0, 0 ]\n+ * },\n+ *\n+ * {\n+ * 'ct': 2960,\n+ * 'ccm': [ 2.26962, -0.54174, -0.72789,\n+ * -0.77008, 2.60271, -0.83262,\n+ * -0.26036, -1.51254, 2.77289 ],\n+ * 'offsets': [ 0, 0, 0 ]\n+ * },\n+ *\n+ * {\n+ * 'ct': 3603,\n+ * 'ccm': [ 2.18644, -0.66148, -0.52496,\n+ * -0.77828, 2.69474, -0.91645,\n+ * -0.25239, -0.83059, 2.08298 ],\n+ * 'offsets': [ 0, 0, 0 ]\n+ * },\n+ * ]\n+ *\n+ * In this case, \\a key_name would be 'ct', and \\a value_name can be either\n+ * 'ccm' or 'offsets'. This way multiple interpolators can be defined in\n+ * one set of color temperature ranges in the tuning file, and they can be\n+ * retrieved separately with the \\a value_name parameter.\n+ *\n+ * \\return Zero on success, negative error code otherwise\n+ */\n+\n+/**\n+ * \\fn void setQuantization(const unsigned int q)\n+ * \\brief Set the quantization value\n+ * \\param[in] q The quantization value\n+ *\n+ * Sets the quantization value. When this is set key gets quantized to this size,\n+ * before doing the interpolation. This can help in reducing the number of\n+ * updated pushed to the hardware.\n+ *\n+ * Note that normally a threshold needs to be combined with quantization.\n+ * Otherwise a value that swings around the edge of the quantization step will\n+ * lead to constant updates.\n+ */\n+\n+/**\n+ * \\fn void setData(std::map<unsigned int, T> &&data)\n+ * \\brief Set the internal map\n+ *\n+ * Overwrites the internal map using move semantics.\n+ */\n+\n+/**\n+ * \\fn const T &getInterpolated(unsigned int key, unsigned int *quantizedKey = nullptr)\n+ * \\brief Retrieve a a interpolated value for the given key\n+ * \\param[in] key The unsigned integer key of the object to retrieve\n+ * \\param[out] quantizedKey If provided, the key value after quantization\n+ * \\return The object corresponding to the key. The object is cached internally,\n+ * so on successive calls with the same key (after quantization) interpolation\n+ * is not recalculated.\n+ */\n+\n+} /* namespace ipa */\n+\n+} /* namespace libcamera */\ndiff --git a/src/ipa/libipa/interpolator.h b/src/ipa/libipa/interpolator.h\nnew file mode 100644\nindex 000000000000..b0241c35bbab\n--- /dev/null\n+++ b/src/ipa/libipa/interpolator.h\n@@ -0,0 +1,139 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2024, Paul Elder <paul.elder@ideasonboard.com>\n+ *\n+ * Helper class for interpolating maps of objects\n+ */\n+\n+#pragma once\n+\n+#include <algorithm>\n+#include <cmath>\n+#include <map>\n+#include <string>\n+#include <tuple>\n+\n+#include <libcamera/base/log.h>\n+\n+#include \"libcamera/internal/yaml_parser.h\"\n+\n+namespace libcamera {\n+\n+LOG_DECLARE_CATEGORY(Interpolator)\n+\n+namespace ipa {\n+\n+template<typename T>\n+class Interpolator\n+{\n+public:\n+\tInterpolator()\n+\t{\n+\t\tquantization_ = 0;\n+\t}\n+\n+\tInterpolator(const std::map<unsigned int, T> &data)\n+\t{\n+\t\tquantization_ = 0;\n+\t\tdata_ = data;\n+\t}\n+\n+\tInterpolator(std::map<unsigned int, T> &&data)\n+\t{\n+\t\tquantization_ = 0;\n+\t\tdata_ = std::move(data);\n+\t}\n+\n+\t~Interpolator() {}\n+\n+\tint readYaml(const libcamera::YamlObject &yaml,\n+\t\t const std::string &key_name,\n+\t\t const std::string &value_name)\n+\t{\n+\t\tdata_.clear();\n+\n+\t\tif (!yaml.isList()) {\n+\t\t\tLOG(Interpolator, Error) << \"yaml object must be a list\";\n+\t\t\treturn -EINVAL;\n+\t\t}\n+\n+\t\tfor (const auto &value : yaml.asList()) {\n+\t\t\tunsigned int ct = std::stoul(value[key_name].get<std::string>(\"\"));\n+\t\t\tstd::optional<T> data =\n+\t\t\t\tvalue[value_name].get<T>();\n+\t\t\tif (!data) {\n+\t\t\t\treturn -EINVAL;\n+\t\t\t}\n+\n+\t\t\tdata_[ct] = *data;\n+\t\t}\n+\n+\t\tif (data_.size() < 1) {\n+\t\t\tLOG(Interpolator, Error) << \"Need at least one element\";\n+\t\t\treturn -EINVAL;\n+\t\t}\n+\n+\t\treturn 0;\n+\t}\n+\n+\tvoid setQuantization(const unsigned int q)\n+\t{\n+\t\tquantization_ = q;\n+\t}\n+\n+\tvoid setData(std::map<unsigned int, T> &&data)\n+\t{\n+\t\tdata_ = std::move(data);\n+\t}\n+\n+\tconst T &getInterpolated(unsigned int key, unsigned int *quantizedKey = nullptr)\n+\t{\n+\t\tASSERT(data_.size() > 0);\n+\n+\t\tif (quantization_ > 0)\n+\t\t\tkey = std::lround(key / static_cast<double>(quantization_)) * quantization_;\n+\n+\t\tif (quantizedKey)\n+\t\t\t*quantizedKey = key;\n+\n+\t\tif (lastInterpolatedKey_.has_value() &&\n+\t\t *lastInterpolatedKey_ == key)\n+\t\t\treturn lastInterpolatedValue_;\n+\n+\t\tif (data_.size() == 1 ||\n+\t\t key <= data_.begin()->first)\n+\t\t\treturn data_.begin()->second;\n+\n+\t\tif (key >= data_.rbegin()->first)\n+\t\t\treturn data_.rbegin()->second;\n+\n+\t\tif (data_.find(key) != data_.end())\n+\t\t\treturn data_[key];\n+\n+\t\t/* The above four guarantee that this will succeed */\n+\t\tauto iter = data_.upper_bound(key);\n+\t\tunsigned int ctUpper = iter->first;\n+\t\tunsigned int ctLower = (--iter)->first;\n+\n+\t\tdouble lambda = (key - ctLower) / static_cast<double>(ctUpper - ctLower);\n+\t\tinterpolate(data_[ctLower], data_[ctUpper], lastInterpolatedValue_, lambda);\n+\t\tlastInterpolatedKey_ = key;\n+\n+\t\treturn lastInterpolatedValue_;\n+\t}\n+\n+\tvoid interpolate(const T &a, const T &b, T &dest, double lambda)\n+\t{\n+\t\tdest = a * (1.0 - lambda) + b * lambda;\n+\t}\n+\n+private:\n+\tstd::map<unsigned int, T> data_;\n+\tT lastInterpolatedValue_;\n+\tstd::optional<unsigned int> lastInterpolatedKey_;\n+\tunsigned int quantization_;\n+};\n+\n+} /* namespace ipa */\n+\n+} /* namespace libcamera */\ndiff --git a/src/ipa/libipa/meson.build b/src/ipa/libipa/meson.build\nindex eff8ce2660f1..2c2712a7d252 100644\n--- a/src/ipa/libipa/meson.build\n+++ b/src/ipa/libipa/meson.build\n@@ -7,6 +7,7 @@ libipa_headers = files([\n 'exposure_mode_helper.h',\n 'fc_queue.h',\n 'histogram.h',\n+ 'interpolator.h',\n 'matrix.h',\n 'matrix_interpolator.h',\n 'module.h',\n@@ -21,6 +22,7 @@ libipa_sources = files([\n 'exposure_mode_helper.cpp',\n 'fc_queue.cpp',\n 'histogram.cpp',\n+ 'interpolator.cpp',\n 'matrix.cpp',\n 'matrix_interpolator.cpp',\n 'module.cpp',\n", "prefixes": [ "v1", "1/8" ] }