{"id":27167,"url":"https://patchwork.libcamera.org/api/patches/27167/?format=json","web_url":"https://patchwork.libcamera.org/patch/27167/","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":"<20260703122543.1991189-9-paul.elder@ideasonboard.com>","date":"2026-07-03T12:25:14","name":"[RFC,08/19] ipa: rkisp2: algo: bls: Implement black level subtraction","commit_ref":null,"pull_url":null,"state":"new","archived":false,"hash":"e29fadec0c879580bcc5726ba11422d5d77581fe","submitter":{"id":17,"url":"https://patchwork.libcamera.org/api/people/17/?format=json","name":"Paul Elder","email":"paul.elder@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/27167/mbox/","series":[{"id":6035,"url":"https://patchwork.libcamera.org/api/series/6035/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=6035","date":"2026-07-03T12:25:06","name":"Add support for rkisp2","version":1,"mbox":"https://patchwork.libcamera.org/series/6035/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/27167/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/27167/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 6CB45C328C\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri,  3 Jul 2026 12:26:31 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 1706F65FD9;\n\tFri,  3 Jul 2026 14:26:31 +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 C4D9765FCA\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri,  3 Jul 2026 14:26:28 +0200 (CEST)","from neptunite.hamster-moth.ts.net (unknown\n\t[IPv6:2404:7a81:160:2100:a2cc:2f45:3bd7:2589])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id E45811121;\n\tFri,  3 Jul 2026 14:25:39 +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=\"fUVSbv7M\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1783081542;\n\tbh=b9X1JcNno86vhftBspY6DOelKj8JrllY5rJUTzW1Ne0=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=fUVSbv7Mipyu65I7+rrDc0qSmJQbsUBVpMA5a0BhNfN00UsJruiJ7b0P2sEwSrpEh\n\tp4w0S97SQgMKeMrHBmB6BHi2DHiR47Bf3sGHc26SfnfgwfGVamK8J6MmbskBsZe4iK\n\tjif/zsrH3eQ9MXOG9TtYGkkXTu88a8DGiAQGcTM8=","From":"Paul Elder <paul.elder@ideasonboard.com>","To":"laurent.pinchart@ideasonboard.com","Cc":"Paul Elder <paul.elder@ideasonboard.com>, michael.riesch@collabora.com, \n\txuhf@rock-chips.com, stefan.klug@ideasonboard.com,\n\tkieran.bingham@ideasonboard.com, dan.scally@ideasonboard.com,\n\tjacopo.mondi@ideasonboard.com, nicolas.dufresne@collabora.com,\n\tlibcamera-devel@lists.libcamera.org","Subject":"[RFC PATCH 08/19] ipa: rkisp2: algo: bls: Implement black level\n\tsubtraction","Date":"Fri,  3 Jul 2026 21:25:14 +0900","Message-ID":"<20260703122543.1991189-9-paul.elder@ideasonboard.com>","X-Mailer":"git-send-email 2.47.2","In-Reply-To":"<20260703122543.1991189-1-paul.elder@ideasonboard.com>","References":"<20260703122543.1991189-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":"Implement a static black level subtraction algorithm for the rkisp2 IPA.\n\nSigned-off-by: Paul Elder <paul.elder@ideasonboard.com>\n---\n src/ipa/rkisp2/algorithms/bls.cpp     | 154 ++++++++++++++++++++++++++\n src/ipa/rkisp2/algorithms/bls.h       |  41 +++++++\n src/ipa/rkisp2/algorithms/meson.build |   1 +\n 3 files changed, 196 insertions(+)\n create mode 100644 src/ipa/rkisp2/algorithms/bls.cpp\n create mode 100644 src/ipa/rkisp2/algorithms/bls.h","diff":"diff --git a/src/ipa/rkisp2/algorithms/bls.cpp b/src/ipa/rkisp2/algorithms/bls.cpp\nnew file mode 100644\nindex 000000000000..aba2b7a867be\n--- /dev/null\n+++ b/src/ipa/rkisp2/algorithms/bls.cpp\n@@ -0,0 +1,154 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2026, Ideas On Board\n+ *\n+ * RkISP2 Black Level Subtraction control\n+ */\n+\n+#include \"bls.h\"\n+\n+#include <linux/videodev2.h>\n+\n+#include <libcamera/base/log.h>\n+\n+#include <libcamera/control_ids.h>\n+\n+/**\n+ * \\file bls.h\n+ */\n+\n+namespace libcamera {\n+\n+namespace ipa::rkisp2::algorithms {\n+\n+/**\n+ * \\class BlackLevelSubtraction\n+ * \\brief RkISP2 Black Level Subtraction control\n+ *\n+ * The pixels output by the camera normally include a black level, because\n+ * sensors do not always report a signal level of '0' for black. Pixels at or\n+ * below this level should be considered black. To achieve that, the RkISP2 BLS\n+ * algorithm subtracts a configurable offset from all pixels.\n+ *\n+ * The black level can be measured at runtime from an optical dark region of the\n+ * camera sensor, or measured during the camera tuning process. The first option\n+ * isn't currently supported.\n+ *\n+ * \\todo Add support for black level in the metadata so that we can capture\n+ * proper raw images for tuning\n+ */\n+\n+LOG_DEFINE_CATEGORY(RkISP2Bls)\n+\n+/**\n+ * \\copydoc libcamera::ipa::Algorithm::init\n+ */\n+int BlackLevelSubtraction::init(IPAContext &context, const ValueNode &tuningData)\n+{\n+\tstd::optional<int16_t> levelRed = tuningData[\"R\"].get<int16_t>();\n+\tstd::optional<int16_t> levelGreenR = tuningData[\"Gr\"].get<int16_t>();\n+\tstd::optional<int16_t> levelGreenB = tuningData[\"Gb\"].get<int16_t>();\n+\tstd::optional<int16_t> levelBlue = tuningData[\"B\"].get<int16_t>();\n+\tbool tuningHasLevels = levelRed && levelGreenR && levelGreenB && levelBlue;\n+\n+\tauto blackLevel = context.camHelper->blackLevel();\n+\tif (!blackLevel) {\n+\t\t/*\n+\t\t * Not all camera sensor helpers have been updated with black\n+\t\t * levels. Print a warning and fall back to the levels from the\n+\t\t * tuning data to preserve backward compatibility. This should\n+\t\t * be removed once all helpers provide the data.\n+\t\t */\n+\t\tLOG(RkISP2Bls, Warning)\n+\t\t\t<< \"No black levels provided by camera sensor helper\"\n+\t\t\t<< \", please fix\";\n+\n+\t\tblackLevelRed_ = levelRed.value_or(4096);\n+\t\tblackLevelGreenR_ = levelGreenR.value_or(4096);\n+\t\tblackLevelGreenB_ = levelGreenB.value_or(4096);\n+\t\tblackLevelBlue_ = levelBlue.value_or(4096);\n+\t} else if (tuningHasLevels) {\n+\t\t/*\n+\t\t * If black levels are provided in the tuning file, use them to\n+\t\t * avoid breaking existing camera tuning. This is deprecated and\n+\t\t * will be removed.\n+\t\t */\n+\t\tLOG(RkISP2Bls, Warning)\n+\t\t\t<< \"Deprecated: black levels overwritten by tuning file\";\n+\n+\t\tblackLevelRed_ = *levelRed;\n+\t\tblackLevelGreenR_ = *levelGreenR;\n+\t\tblackLevelGreenB_ = *levelGreenB;\n+\t\tblackLevelBlue_ = *levelBlue;\n+\t} else {\n+\t\tblackLevelRed_ = *blackLevel;\n+\t\tblackLevelGreenR_ = *blackLevel;\n+\t\tblackLevelGreenB_ = *blackLevel;\n+\t\tblackLevelBlue_ = *blackLevel;\n+\t}\n+\n+\tLOG(RkISP2Bls, Debug)\n+\t\t<< \"Black levels: red \" << blackLevelRed_\n+\t\t<< \", green (red) \" << blackLevelGreenR_\n+\t\t<< \", green (blue) \" << blackLevelGreenB_\n+\t\t<< \", blue \" << blackLevelBlue_;\n+\n+\treturn 0;\n+}\n+\n+int BlackLevelSubtraction::configure([[maybe_unused]] IPAContext &context,\n+\t\t\t\t     [[maybe_unused]] const IPACameraSensorInfo &configInfo)\n+{\n+\t/* \\todo Save the cfa */\n+\n+\treturn 0;\n+}\n+\n+/**\n+ * \\copydoc libcamera::ipa::Algorithm::prepare\n+ */\n+void BlackLevelSubtraction::prepare(IPAContext &context,\n+\t\t\t\t    const uint32_t frame,\n+\t\t\t\t    [[maybe_unused]] IPAFrameContext &frameContext,\n+\t\t\t\t    RkISP2Params *params)\n+{\n+\tif (context.configuration.raw)\n+\t\treturn;\n+\n+\tif (frame > 1)\n+\t\treturn;\n+\n+\tauto config = params->block<RkISP2Blocks::Bls>();\n+\tconfig.setEnabled(true);\n+\n+\tconfig->enable_auto = 0;\n+\n+\t/* Scale down to the 12-bit black levels used by the BLS block. */\n+\t/* \\todo Handle cfa properly */\n+\tconfig->bls_fixed_val.a = blackLevelRed_ >> 4;\n+\tconfig->bls_fixed_val.b = blackLevelGreenR_ >> 4;\n+\tconfig->bls_fixed_val.c = blackLevelGreenB_ >> 4;\n+\tconfig->bls_fixed_val.d = blackLevelBlue_ >> 4;\n+}\n+\n+/**\n+ * \\copydoc libcamera::ipa::Algorithm::process\n+ */\n+void BlackLevelSubtraction::process([[maybe_unused]] IPAContext &context,\n+\t\t\t\t    [[maybe_unused]] const uint32_t frame,\n+\t\t\t\t    [[maybe_unused]] IPAFrameContext &frameContext,\n+\t\t\t\t    [[maybe_unused]] const rkisp2_stats_buffer *stats,\n+\t\t\t\t    ControlList &metadata)\n+{\n+\tmetadata.set(controls::SensorBlackLevels,\n+\t\t     { static_cast<int32_t>(blackLevelRed_),\n+\t\t       static_cast<int32_t>(blackLevelGreenR_),\n+\t\t       static_cast<int32_t>(blackLevelGreenB_),\n+\t\t       static_cast<int32_t>(blackLevelBlue_) });\n+}\n+\n+REGISTER_IPA_ALGORITHM(BlackLevelSubtraction, \"BlackLevelSubtraction\")\n+\n+} /* namespace ipa::rkisp2::algorithms */\n+\n+} /* namespace libcamera */\ndiff --git a/src/ipa/rkisp2/algorithms/bls.h b/src/ipa/rkisp2/algorithms/bls.h\nnew file mode 100644\nindex 000000000000..5377ae76a029\n--- /dev/null\n+++ b/src/ipa/rkisp2/algorithms/bls.h\n@@ -0,0 +1,41 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2026, Ideas On Board\n+ *\n+ * RkISP2 Black Level Subtraction control\n+ */\n+\n+#pragma once\n+\n+#include \"algorithm.h\"\n+\n+namespace libcamera {\n+\n+namespace ipa::rkisp2::algorithms {\n+\n+class BlackLevelSubtraction : public Algorithm\n+{\n+public:\n+\tBlackLevelSubtraction() = default;\n+\t~BlackLevelSubtraction() = default;\n+\n+\tint init(IPAContext &context, const ValueNode &tuningData) override;\n+\tint configure(IPAContext &context,\n+\t\t      const IPACameraSensorInfo &configInfo) override;\n+\tvoid prepare(IPAContext &context, const uint32_t frame,\n+\t\t     IPAFrameContext &frameContext,\n+\t\t     RkISP2Params *params) override;\n+\tvoid process(IPAContext &context, const uint32_t frame,\n+\t\t     IPAFrameContext &frameContext,\n+\t\t     const rkisp2_stats_buffer *stats,\n+\t\t     ControlList &metadata) override;\n+\n+private:\n+\tint16_t blackLevelRed_;\n+\tint16_t blackLevelGreenR_;\n+\tint16_t blackLevelGreenB_;\n+\tint16_t blackLevelBlue_;\n+};\n+\n+} /* namespace ipa::rkisp2::algorithms */\n+} /* namespace libcamera */\ndiff --git a/src/ipa/rkisp2/algorithms/meson.build b/src/ipa/rkisp2/algorithms/meson.build\nindex 4bb81e48eb01..36996918b39e 100644\n--- a/src/ipa/rkisp2/algorithms/meson.build\n+++ b/src/ipa/rkisp2/algorithms/meson.build\n@@ -1,5 +1,6 @@\n # SPDX-License-Identifier: CC0-1.0\n \n rkisp2_ipa_algorithms = files([\n+    'bls.cpp',\n ])\n \n","prefixes":["RFC","08/19"]}