Patch Detail
Show a patch.
GET /api/1.1/patches/22341/?format=api
{ "id": 22341, "url": "https://patchwork.libcamera.org/api/1.1/patches/22341/?format=api", "web_url": "https://patchwork.libcamera.org/patch/22341/", "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": "<20241216094933.198027-2-paul.elder@ideasonboard.com>", "date": "2024-12-16T09:49:32", "name": "[v2,1/2] ipa: libipa: Add Lux helper", "commit_ref": null, "pull_url": null, "state": "superseded", "archived": false, "hash": "f9029cf65e14fe09984ddbafa832e8adab20996f", "submitter": { "id": 17, "url": "https://patchwork.libcamera.org/api/1.1/people/17/?format=api", "name": "Paul Elder", "email": "paul.elder@ideasonboard.com" }, "delegate": null, "mbox": "https://patchwork.libcamera.org/patch/22341/mbox/", "series": [ { "id": 4893, "url": "https://patchwork.libcamera.org/api/1.1/series/4893/?format=api", "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=4893", "date": "2024-12-16T09:49:31", "name": "ipa: rkisp1: Add lux estimation", "version": 2, "mbox": "https://patchwork.libcamera.org/series/4893/mbox/" } ], "comments": "https://patchwork.libcamera.org/api/patches/22341/comments/", "check": "pending", "checks": "https://patchwork.libcamera.org/api/patches/22341/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 6F24BC32F9\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon, 16 Dec 2024 09:49:48 +0000 (UTC)", "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 2910267F4C;\n\tMon, 16 Dec 2024 10:49:48 +0100 (CET)", "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 043DE67ECA\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 16 Dec 2024 10:49:46 +0100 (CET)", "from neptunite.flets-east.jp (unknown\n\t[IPv6:2404:7a81:160:2100:c11a:e451:861f:3d32])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 1F96F22F;\n\tMon, 16 Dec 2024 10:49:07 +0100 (CET)" ], "Authentication-Results": "lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"KyLxdlVi\"; dkim-atps=neutral", "DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1734342549;\n\tbh=BLV+dkjia2FTYj0w45GpsmVB1QlijaLBzBoYRUil/YU=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=KyLxdlVidrZRmM1HNE8dGJN4ViBq+r2BefFj/zCcbs/nO4ylJalxYQiIcsvvHDQZd\n\t3iHFLJt8UbHAmn6WNb0BcNtHFd0Kd8jWejU7Y8+Gh+kqRI9TCPDNkHsW3Y/G+AKjQX\n\t6+9/gdOeS975m9nr20+LN5/g73nQhyAeEraCmzFQ=", "From": "Paul Elder <paul.elder@ideasonboard.com>", "To": "libcamera-devel@lists.libcamera.org, laurent.pinchart@ideasonboard.com, \n\tstefan.klug@ideasonboard.com", "Cc": "Paul Elder <paul.elder@ideasonboard.com>", "Subject": "[PATCH v2 1/2] ipa: libipa: Add Lux helper", "Date": "Mon, 16 Dec 2024 18:49:32 +0900", "Message-Id": "<20241216094933.198027-2-paul.elder@ideasonboard.com>", "X-Mailer": "git-send-email 2.39.2", "In-Reply-To": "<20241216094933.198027-1-paul.elder@ideasonboard.com>", "References": "<20241216094933.198027-1-paul.elder@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": "Add a Lux helper to libipa that does the estimation of the lux level\ngiven gain, exposure, and luminance histogram. The helper also\nhandles reading the reference values from the tuning file. These are\nexpected to be common operations of lux algorithm modules in IPAs, and\nis modeled/copied from Raspberry Pi.\n\nSigned-off-by: Paul Elder <paul.elder@ideasonboard.com>\n\n---\nChanges in v2:\n- improve documentation\n- add binSize member variable and corresponding setter\n- remove aperture\n- split gain into analogue and digital\n- change tuning file names into camel case\n---\n src/ipa/libipa/lux.cpp | 183 +++++++++++++++++++++++++++++++++++++\n src/ipa/libipa/lux.h | 48 ++++++++++\n src/ipa/libipa/meson.build | 2 +\n 3 files changed, 233 insertions(+)\n create mode 100644 src/ipa/libipa/lux.cpp\n create mode 100644 src/ipa/libipa/lux.h", "diff": "diff --git a/src/ipa/libipa/lux.cpp b/src/ipa/libipa/lux.cpp\nnew file mode 100644\nindex 000000000000..55b771705ab7\n--- /dev/null\n+++ b/src/ipa/libipa/lux.cpp\n@@ -0,0 +1,183 @@\n+/* SPDX-License-Identifier: BSD-2-Clause */\n+/*\n+ * Copyright (C) 2019, Raspberry Pi Ltd\n+ * Copyright (C) 2024, Paul Elder <paul.elder@ideasonboard.com>\n+ *\n+ * Helper class that implements lux estimation\n+ */\n+#include \"lux.h\"\n+\n+#include <algorithm>\n+#include <chrono>\n+\n+#include <libcamera/base/log.h>\n+\n+#include \"libcamera/internal/yaml_parser.h\"\n+\n+#include \"histogram.h\"\n+\n+/**\n+ * \\file lux.h\n+ * \\brief Helper class that implements lux estimation\n+ *\n+ * As estimating the lux level of an image is expected to be a common\n+ * operation, it is implemented in a helper in libipa. It can be used to adjust\n+ * the target Y value in AGC or for Bayesian AWB estimation.\n+ */\n+\n+namespace libcamera {\n+\n+using namespace std::literals::chrono_literals;\n+\n+LOG_DEFINE_CATEGORY(Lux)\n+\n+namespace ipa {\n+\n+/**\n+ * \\class Lux\n+ * \\brief Class that implements lux estimation\n+ *\n+ * IPAs that wish to use lux esimation should create a Lux algorithm module\n+ * that lightly wraps this module by providing the platform-specific luminance\n+ * histogram. The Lux entry in the tuning file must then precede the algorithms\n+ * that depend on the estimated lux value.\n+ */\n+\n+/**\n+ * \\var Lux::binSize_\n+ * \\brief The maximum count of each bin\n+ *\n+ * This must be populated via setBinSize() before estimateLux() is used.\n+ */\n+\n+/**\n+ * \\var Lux::referenceExposureTime_\n+ * \\brief The exposure time of the reference image, in microseconds.\n+ */\n+\n+/**\n+ * \\var Lux::referenceAnalogueGain_\n+ * \\brief The analogue gain of the reference image.\n+ */\n+\n+/**\n+ * \\var Lux::referenceDigitalGain_\n+ * \\brief The analogue gain of the reference image.\n+ */\n+\n+/**\n+ * \\var Lux::referenceY_\n+ * \\brief The measured luminance of the reference image, out of the bin size.\n+ *\n+ * \\sa binSize_\n+ */\n+\n+/**\n+ * \\var Lux::referenceLux_\n+ * \\brief The estimated lux level of the reference image.\n+ */\n+\n+/**\n+ * \\brief Configure the bin size of the Lux helper module\n+ * \\param[in] binSize The maximum count of each bin\n+ *\n+ * This must be configured befure estimateLux() is called, as it is\n+ * platform-specific.\n+ */\n+void Lux::setBinSize(unsigned int binSize)\n+{\n+\tbinSize_ = binSize;\n+}\n+\n+/**\n+ * \\brief Parse tuning data\n+ * \\param[in] tuningData The YamlObject representing the tuning data\n+ *\n+ * This function parses yaml tuning data for the common Lux module. It requires\n+ * reference exposure time, analogue gain, digital gain, and lux values.\n+ *\n+ * \\code{.unparsed}\n+ * algorithms:\n+ * - Lux:\n+ * referenceExposureTime: 10000\n+ * referenceAnalogueGain: 4.0\n+ * referenceDigitalGain: 1.0\n+ * referenceY: 12000\n+ * \\endcode\n+ *\n+ * \\return 0 on success or a negative error code\n+ */\n+int Lux::readYaml(const YamlObject &tuningData)\n+{\n+\tauto value = tuningData[\"referenceExposureTime\"].get<double>();\n+\tif (!value) {\n+\t\tLOG(Lux, Error) << \"Missing tuning parameter: 'referenceExposureTime'\";\n+\t\treturn -EINVAL;\n+\t}\n+\treferenceExposureTime_ = *value * 1.0us;\n+\n+\tvalue = tuningData[\"referenceAnalogueGain\"].get<double>();\n+\tif (!value) {\n+\t\tLOG(Lux, Error) << \"Missing tuning parameter: 'referenceAnalogueGain'\";\n+\t\treturn -EINVAL;\n+\t}\n+\treferenceAnalogueGain_ = *value;\n+\n+\tvalue = tuningData[\"referenceDigitalGain\"].get<double>();\n+\tif (!value) {\n+\t\tLOG(Lux, Error) << \"Missing tuning parameter: 'referenceDigitalGain'\";\n+\t\treturn -EINVAL;\n+\t}\n+\treferenceDigitalGain_ = *value;\n+\n+\tvalue = tuningData[\"referenceY\"].get<double>();\n+\tif (!value) {\n+\t\tLOG(Lux, Error) << \"Missing tuning parameter: 'referenceY'\";\n+\t\treturn -EINVAL;\n+\t}\n+\treferenceY_ = *value;\n+\n+\tvalue = tuningData[\"referenceLux\"].get<double>();\n+\tif (!value) {\n+\t\tLOG(Lux, Error) << \"Missing tuning parameter: 'referenceLux'\";\n+\t\treturn -EINVAL;\n+\t}\n+\treferenceLux_ = *value;\n+\n+\treturn 0;\n+}\n+\n+/**\n+ * \\brief Estimate lux given runtime values\n+ * \\param[in] exposureTime Exposure time applied to the frame\n+ * \\param[in] aGain Analogue gain applied to the frame\n+ * \\param[in] dGain Digital gain applied to the frame\n+ * \\param[in] yHist Histogram from the ISP statistics\n+ *\n+ * Estimate the lux given the exposure time, gain, and histogram.\n+ *\n+ * The bin size must be configured via setBinSize() before this function can be\n+ * called.\n+ *\n+ * \\return Estimated lux value\n+ */\n+double Lux::estimateLux(utils::Duration exposureTime,\n+\t\t\tdouble aGain, double dGain,\n+\t\t\tconst Histogram &yHist) const\n+{\n+\tdouble currentY = yHist.interQuantileMean(0, 1);\n+\tdouble exposureTimeRatio = referenceExposureTime_ / exposureTime;\n+\tdouble aGainRatio = referenceAnalogueGain_ / aGain;\n+\tdouble dGainRatio = referenceDigitalGain_ / dGain;\n+\tdouble yRatio = currentY * (binSize_ / yHist.bins()) / referenceY_;\n+\n+\tdouble estimatedLux = exposureTimeRatio * aGainRatio * dGainRatio *\n+\t\t\t yRatio * referenceLux_;\n+\n+\tLOG(Lux, Debug) << \"Estimated lux \" << estimatedLux;\n+\treturn estimatedLux;\n+}\n+\n+} /* namespace ipa */\n+\n+} /* namespace libcamera */\ndiff --git a/src/ipa/libipa/lux.h b/src/ipa/libipa/lux.h\nnew file mode 100644\nindex 000000000000..3e6dcc5c3413\n--- /dev/null\n+++ b/src/ipa/libipa/lux.h\n@@ -0,0 +1,48 @@\n+/* SPDX-License-Identifier: BSD-2-Clause */\n+/*\n+ * Copyright (C) 2019, Raspberry Pi Ltd\n+ * Copyright (C) 2024, Paul Elder <paul.elder@ideasonboard.com>\n+ *\n+ * Helper class that implements lux estimation\n+ */\n+\n+#pragma once\n+\n+#include <algorithm>\n+#include <tuple>\n+#include <vector>\n+\n+#include <libcamera/base/utils.h>\n+\n+#include \"libcamera/internal/yaml_parser.h\"\n+\n+#include \"histogram.h\"\n+\n+namespace libcamera {\n+\n+namespace ipa {\n+\n+class Lux\n+{\n+public:\n+\tLux() = default;\n+\t~Lux() = default;\n+\n+\tvoid setBinSize(unsigned int binSize);\n+\tint readYaml(const YamlObject &tuningData);\n+\tdouble estimateLux(utils::Duration exposureTime,\n+\t\t\t double aGain, double dGain,\n+\t\t\t const Histogram &yHist) const;\n+\n+private:\n+\tunsigned int binSize_;\n+\tutils::Duration referenceExposureTime_;\n+\tdouble referenceAnalogueGain_;\n+\tdouble referenceDigitalGain_;\n+\tdouble referenceY_;\n+\tdouble referenceLux_;\n+};\n+\n+} /* namespace ipa */\n+\n+} /* namespace libcamera */\ndiff --git a/src/ipa/libipa/meson.build b/src/ipa/libipa/meson.build\nindex 93ae25da5e83..189ec0311bd3 100644\n--- a/src/ipa/libipa/meson.build\n+++ b/src/ipa/libipa/meson.build\n@@ -10,6 +10,7 @@ libipa_headers = files([\n 'histogram.h',\n 'interpolator.h',\n 'lsc_polynomial.h',\n+ 'lux.h',\n 'module.h',\n 'pwl.h',\n 'vector.h',\n@@ -25,6 +26,7 @@ libipa_sources = files([\n 'histogram.cpp',\n 'interpolator.cpp',\n 'lsc_polynomial.cpp',\n+ 'lux.cpp',\n 'module.cpp',\n 'pwl.cpp',\n 'vector.cpp',\n", "prefixes": [ "v2", "1/2" ] }