From patchwork Wed Dec 2 15:13:03 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Milan Zamazal X-Patchwork-Id: 29140 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 7950FC3304 for ; Wed, 2 Dec 2026 15:13:31 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 09D166AAF4; Wed, 2 Dec 2026 16:13:31 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.b="CxiEICTr"; dkim-atps=neutral Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id A5CF46AAF6 for ; Wed, 2 Dec 2026 16:13:26 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1796224406; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=sqwCy4B5XwxH0jNVi5NWgzn5Luq+ct7NoZZzuJgYE4U=; b=CxiEICTrYnAd81Y9qrB6uQgVraEKrNcqQDzdKBhPdsERCS5cAc8G0BhVcXLxhCRDfc8ESm 8JqBF+idjBxvtcehrHMfpbPbfsRzF+hme2tgiBAEX+7NKw30iizBVemA4nHqT0T0fectC1 pGyo5IagUXcMQy3/3NNNAcQJYilhDoM= X-MC-Rep: binary re single Received: from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-575-LzZUK2u0PJ-kMD7HlAU9Vg-1; Wed, 2 Dec 2026 15:13:21 +0000 X-MC-Unique: LzZUK2u0PJ-kMD7HlAU9Vg-1 X-Mimecast-MFC-AGG-ID: LzZUK2u0PJ-kMD7HlAU9Vg_1796224400 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 3785E192C196; Wed, 2 Dec 2026 15:13:20 +0000 (UTC) Received: from mzamazal-thinkpadp1gen7.redhat.corp (headnet04.pony-001.prod.iad2.dc.redhat.com [10.2.32.116]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id EAEC53000398; Wed, 2 Dec 2026 15:13:18 +0000 (UTC) From: Milan Zamazal To: libcamera-devel@lists.libcamera.org Cc: Milan Zamazal , Hans de Goede , Robert Mader Subject: [PATCH v4 3/4] ipa: softisp: Support any LSC table sizes Date: Wed, 2 Dec 2026 16:13:03 +0100 Message-ID: <20261202151305.132644-4-mzamazal@redhat.com> In-Reply-To: <20261202151305.132644-1-mzamazal@redhat.com> References: <20261202151305.132644-1-mzamazal@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: rJ7bvWoixl_zQC8orJIWWNiCIY6PDOUjJCU7CC3xlh4_1796224400 X-Mimecast-Originator: redhat.com content-type: text/plain; charset="US-ASCII"; x-default=true X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org This adds support of any lens shading correction table size to the software ISP and its IPA. The tuning file can specify the table grid size using a newly introduced `gridSize' parameter, for example: - Lsc: gridSize: width: 32 height: 32 sets: ... If unspecified, the old size of 17x17 is used for backward compatibility. While debayering uses a gridSize_ std::optional variable for the purpose, it cannot be used here because Mojo apparently doesn't support optional's. So separate "size" and "enabled" variables are used when talking to the IPA. Signed-off-by: Milan Zamazal --- .../internal/software_isp/debayer_params.h | 21 +++----- .../internal/software_isp/software_isp.h | 1 - include/libcamera/ipa/softisp.mojom | 3 +- src/ipa/softisp/algorithms/lsc.cpp | 53 ++++++++++++++----- src/ipa/softisp/algorithms/lsc.h | 6 ++- src/ipa/softisp/ipa_context.h | 1 + src/ipa/softisp/softisp.cpp | 10 ++-- src/libcamera/software_isp/debayer.cpp | 13 ----- src/libcamera/software_isp/software_isp.cpp | 9 ++-- 9 files changed, 67 insertions(+), 50 deletions(-) diff --git a/include/libcamera/internal/software_isp/debayer_params.h b/include/libcamera/internal/software_isp/debayer_params.h index 300c3f2d6..0e3c3995c 100644 --- a/include/libcamera/internal/software_isp/debayer_params.h +++ b/include/libcamera/internal/software_isp/debayer_params.h @@ -10,8 +10,10 @@ #pragma once -#include #include +#include + +#include #include "libcamera/internal/matrix.h" #include "libcamera/internal/vector.h" @@ -32,23 +34,12 @@ struct DebayerParams { * must be a multiple of 4 (the initial value of GL_UNPACK_ALIGNMENT) * and lscLut is also aligned to 4 bytes. **/ - static constexpr unsigned int kLscGridSize = 17; static constexpr unsigned int kLscValuesPerCell = 4; static constexpr unsigned int kLscBytesPerCell = kLscValuesPerCell * sizeof(uint8_t); - using LscLookupTable = - std::array; - static constexpr auto identityLscLut = [] { - LscLookupTable lut = {}; - /* lut.fill(64) could be used, but it fails with older gcc versions */ - for (size_t i = 0; kLscValuesPerCell * i < lut.size(); i++) { - lut[i * kLscValuesPerCell + 0] = 64; /* == UQ<2, 6>(1.0f).quantized() */ - lut[i * kLscValuesPerCell + 1] = 64; - lut[i * kLscValuesPerCell + 2] = 64; - } - return lut; - }(); - alignas(4) LscLookupTable lscLut = identityLscLut; + using LscLookupTable = std::vector; + /* Minimum allowed alignment for the vector is 8. */ + alignas(8) LscLookupTable lscLut{}; uint64_t lscLutVersion = 0; }; diff --git a/include/libcamera/internal/software_isp/software_isp.h b/include/libcamera/internal/software_isp/software_isp.h index c8ed91949..9958f6966 100644 --- a/include/libcamera/internal/software_isp/software_isp.h +++ b/include/libcamera/internal/software_isp/software_isp.h @@ -101,7 +101,6 @@ private: DebayerParams debayerParams_; DmaBufAllocator dmaHeap_; bool ccmEnabled_; - bool lscEnabled_; std::optional lscGridSize_; std::unique_ptr ipa_; diff --git a/include/libcamera/ipa/softisp.mojom b/include/libcamera/ipa/softisp.mojom index e42742ed7..e10819e93 100644 --- a/include/libcamera/ipa/softisp.mojom +++ b/include/libcamera/ipa/softisp.mojom @@ -20,7 +20,8 @@ interface IPASoftIspInterface { libcamera.ControlInfoMap sensorControls) => (int32 ret, libcamera.ControlInfoMap ipaControls, - bool ccmEnabled, bool lscEnabled); + bool ccmEnabled, bool lscEnabled, + libcamera.Size lscGridSize); start() => (int32 ret); stop(); configure(IPAConfigInfo configInfo) diff --git a/src/ipa/softisp/algorithms/lsc.cpp b/src/ipa/softisp/algorithms/lsc.cpp index 308939f19..d49ab58ab 100644 --- a/src/ipa/softisp/algorithms/lsc.cpp +++ b/src/ipa/softisp/algorithms/lsc.cpp @@ -13,22 +13,45 @@ namespace ipa::softisp::algorithms { LOG_DEFINE_CATEGORY(IPASoftIspLsc) -int Lsc::init(IPAContext &context, const ValueNode &tuningData) +DebayerParams::LscLookupTable identityLscLut(const Size &size) { - static constexpr unsigned int kGridSize = DebayerParams::kLscGridSize; + size_t lutSize = DebayerParams::kLscValuesPerCell * size.width * size.height; + /* 64 == UQ<2, 6>(1.0f).quantized() */ + return DebayerParams::LscLookupTable(lutSize, 64); +} - for (unsigned int i = 0; i < kGridSize; i++) - gridPos_.push_back(static_cast(i) / (kGridSize - 1)); +int Lsc::init(IPAContext &context, const ValueNode &tuningData) +{ + auto &tuningGridSize = tuningData["gridSize"]; + if (tuningGridSize.isEmpty()) { + /* Just for backward compatibility, should be erroneous otherwise. */ + gridSize_ = defaultGridSize_; + } else { + if (!tuningGridSize.isDictionary()) { + LOG(IPASoftIspLsc, Error) + << "Invalid LSC grid size specification in the tuning file"; + return -1; + } + auto gridWidth = tuningGridSize["width"].get(); + auto gridHeight = tuningGridSize["height"].get(); + if (!gridWidth || !gridHeight || gridWidth <= 1 || gridHeight <= 1) { + LOG(IPASoftIspLsc, Error) + << "Invalid LSC grid values in the tuning file"; + return -1; + } + gridSize_ = Size(gridWidth.value(), gridHeight.value()); + } int ret = lscAlgo_.init(tuningData, context.ctrlMap, - { .keys = { "r", "g", "b" }, - .numHSamples = kGridSize, - .numVSamples = kGridSize, - .sensorSize = context.sensorInfo.activeArea.size() }); + { .keys = { "r", "g", "b" }, + .numHSamples = gridSize_.width, + .numVSamples = gridSize_.height, + .sensorSize = context.sensorInfo.activeArea.size() }); if (ret) return ret; context.lscEnabled = true; + context.lscGridSize = gridSize_; return 0; } @@ -36,9 +59,15 @@ int Lsc::init(IPAContext &context, const ValueNode &tuningData) int Lsc::configure(IPAContext &context, [[maybe_unused]] const IPAConfigInfo &configInfo) { + std::vector gridPosX, gridPosY; + for (unsigned int i = 0; i < gridSize_.width; i++) + gridPosX.push_back(static_cast(i) / (gridSize_.width - 1)); + for (unsigned int i = 0; i < gridSize_.height; i++) + gridPosY.push_back(static_cast(i) / (gridSize_.height - 1)); + return lscAlgo_.configure(context.activeState.lsc, context.sensorInfo.analogCrop, - gridPos_, gridPos_); + gridPosX, gridPosY); } void Lsc::prepare([[maybe_unused]] IPAContext &context, @@ -51,7 +80,7 @@ void Lsc::prepare([[maybe_unused]] IPAContext &context, if (!frameContext.lsc.enabled) { if (lastAppliedCt_ != 0) { - params->lscLut = DebayerParams::identityLscLut; + params->lscLut = identityLscLut(gridSize_); params->lscLutVersion++; lastAppliedCt_ = 0; @@ -71,8 +100,8 @@ void Lsc::prepare([[maybe_unused]] IPAContext &context, const auto &blue = set.at("b"); DebayerParams::LscLookupTable &lut = params->lscLut; - constexpr unsigned int gridSize = DebayerParams::kLscGridSize; - for (unsigned int i = 0, j = 0; i < gridSize * gridSize; i++) { + lut.resize(DebayerParams::kLscValuesPerCell * gridSize_.width * gridSize_.height); + for (unsigned int i = 0, j = 0; i < gridSize_.width * gridSize_.height; i++) { lut[j++] = red[i]; lut[j++] = green[i]; lut[j++] = blue[i]; diff --git a/src/ipa/softisp/algorithms/lsc.h b/src/ipa/softisp/algorithms/lsc.h index 83ec5aec2..eebbd0e68 100644 --- a/src/ipa/softisp/algorithms/lsc.h +++ b/src/ipa/softisp/algorithms/lsc.h @@ -17,6 +17,8 @@ namespace libcamera { namespace ipa::softisp::algorithms { +DebayerParams::LscLookupTable identityLscLut(const Size &size); + class Lsc : public Algorithm { public: @@ -41,8 +43,8 @@ public: private: LscAlgorithm> lscAlgo_; - std::vector gridPos_; - + static constexpr Size defaultGridSize_ = Size(17, 17); + Size gridSize_ = defaultGridSize_; unsigned int lastAppliedCt_ = 0; }; diff --git a/src/ipa/softisp/ipa_context.h b/src/ipa/softisp/ipa_context.h index 62d1ae84b..9a4ae726b 100644 --- a/src/ipa/softisp/ipa_context.h +++ b/src/ipa/softisp/ipa_context.h @@ -91,6 +91,7 @@ struct IPAContext { ControlInfoMap::Map ctrlMap; bool ccmEnabled = false; bool lscEnabled = false; + Size lscGridSize; }; } /* namespace ipa::softisp */ diff --git a/src/ipa/softisp/softisp.cpp b/src/ipa/softisp/softisp.cpp index e35f07737..9926715f8 100644 --- a/src/ipa/softisp/softisp.cpp +++ b/src/ipa/softisp/softisp.cpp @@ -27,6 +27,7 @@ #include "libcamera/internal/yaml_parser.h" #include "algorithms/adjust.h" +#include "algorithms/lsc.h" #include "libipa/agc.h" #include "libipa/camera_sensor_helper.h" @@ -59,7 +60,8 @@ public: const ControlInfoMap &sensorControls, ControlInfoMap *ipaControls, bool *ccmEnabled, - bool *lscEnabled) override; + bool *lscEnabled, + Size *lscGridSize) override; int configure(const IPAConfigInfo &configInfo, ControlInfoMap *ipaControls) override; @@ -99,7 +101,8 @@ int IPASoftIsp::init(const IPASettings &settings, const ControlInfoMap &sensorControls, ControlInfoMap *ipaControls, bool *ccmEnabled, - bool *lscEnabled) + bool *lscEnabled, + Size *lscGridSize) { context_.camHelper = CameraSensorHelperFactoryBase::create(settings.sensorModel); if (!context_.camHelper) { @@ -140,6 +143,7 @@ int IPASoftIsp::init(const IPASettings &settings, *ccmEnabled = context_.ccmEnabled; *lscEnabled = context_.lscEnabled; + *lscGridSize = context_.lscGridSize; params_ = nullptr; stats_ = nullptr; @@ -168,7 +172,7 @@ int IPASoftIsp::init(const IPASettings &settings, params_->contrastExp = 1.0; params_->gains = { { 1.0, 1.0, 1.0 } }; params_->lscLutVersion = 0; - params_->lscLut = DebayerParams::identityLscLut; + params_->lscLut = DebayerParams::LscLookupTable(); /* combinedMatrix is reset for each frame. */ } diff --git a/src/libcamera/software_isp/debayer.cpp b/src/libcamera/software_isp/debayer.cpp index 65820eabd..32bd6e432 100644 --- a/src/libcamera/software_isp/debayer.cpp +++ b/src/libcamera/software_isp/debayer.cpp @@ -43,11 +43,6 @@ namespace libcamera { * \brief Contrast value to be used as an exponent */ -/** - * \var DebayerParams::kLscGridSize - * \brief Number of lens shading grid areas in one direction - */ - /** * \var DebayerParams::kLscValuesPerCell * \brief Number of pixel values per each of the lens shading grid areas @@ -71,14 +66,6 @@ namespace libcamera { * \brief Lens shading lookup table */ -/** - * \var DebayerParams::identityLscLut - * \brief Lens shading lookup table of identity mapping - * - * This table can be used to initialise the lens shading lookup table or to - * not apply any real lens shading correction. - */ - /** * \var DebayerParams::lscLutVersion * \brief Incremented on each \a lscLut change diff --git a/src/libcamera/software_isp/software_isp.cpp b/src/libcamera/software_isp/software_isp.cpp index 88349598a..4b86af7b5 100644 --- a/src/libcamera/software_isp/software_isp.cpp +++ b/src/libcamera/software_isp/software_isp.cpp @@ -157,6 +157,8 @@ SoftwareIsp::SoftwareIsp(PipelineHandler *pipe, const CameraSensor *sensor, return; } + bool lscEnabled; + Size gridSize; ret = ipa_->init(IPASettings{ ipaTuningFile, sensor->model() }, debayer_->getStatsFD(), sharedParams_.fd(), @@ -164,15 +166,16 @@ SoftwareIsp::SoftwareIsp(PipelineHandler *pipe, const CameraSensor *sensor, sensor->controls(), ipaControls, &ccmEnabled_, - &lscEnabled_); + &lscEnabled, + &gridSize); if (ret) { LOG(SoftwareIsp, Error) << "IPA init failed"; debayer_.reset(); return; } - if (lscEnabled_) - lscGridSize_ = Size(DebayerParams::kLscGridSize, DebayerParams::kLscGridSize); + if (lscEnabled) + lscGridSize_ = gridSize; else lscGridSize_.reset();