From patchwork Mon Nov 16 17:49:17 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= X-Patchwork-Id: 28934 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 199A1C3355 for ; Mon, 16 Nov 2026 17:49:58 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 54BB16A899; Mon, 16 Nov 2026 18:49:53 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="TXv96Kki"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id EE86F6A864 for ; Mon, 16 Nov 2026 18:49:33 +0100 (CET) Received: from pb-laptop.local (185.221.140.49.nat.pool.zt.hu [185.221.140.49]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id DBCB9929; Mon, 16 Nov 2026 18:47:06 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1794851227; bh=ow7pyxZaBIiseq66MvsDOHa2qaBWDVcZzeyaqHfqZoQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=TXv96KkiiZVVMUQwTW+WwsXnJpOFLLNyvp8R9s/8qU7ING3ZK15WFgtjJl27fj1Fw QkygJ1YNO28DPW/UvuROaXKpJuYfJ+cSZOUCu2tLMUFJweYEqyUWoBYZq3Wb7DM/ID gyFqIcYHHNORQvP1PuBeYUY+YaRbjSk4cvu1iX1w= From: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= To: libcamera-devel@lists.libcamera.org Cc: Jacopo Mondi Subject: [PATCH v5 21/22] ipa: rppx1: Add lens shading correction Date: Mon, 16 Nov 2026 18:49:17 +0100 Message-ID: <20261116174919.1056403-22-barnabas.pocze@ideasonboard.com> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261116174919.1056403-1-barnabas.pocze@ideasonboard.com> References: <20261116174919.1056403-1-barnabas.pocze@ideasonboard.com> MIME-Version: 1.0 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 Sender: "libcamera-devel" From: Jacopo Mondi Add the algorithm to the rppx1 ipa module based on the corresponding algorithm in the rkisp1 ipa module. Signed-off-by: Jacopo Mondi Signed-off-by: Barnabás Pőcze Reviewed-by: Kieran Bingham --- src/ipa/rppx1/algorithms/lsc.cpp | 262 +++++++++++++++++++++++++++ src/ipa/rppx1/algorithms/lsc.h | 58 ++++++ src/ipa/rppx1/algorithms/meson.build | 1 + src/ipa/rppx1/ipa_context.h | 5 + src/ipa/rppx1/params.h | 4 + 5 files changed, 330 insertions(+) create mode 100644 src/ipa/rppx1/algorithms/lsc.cpp create mode 100644 src/ipa/rppx1/algorithms/lsc.h diff --git a/src/ipa/rppx1/algorithms/lsc.cpp b/src/ipa/rppx1/algorithms/lsc.cpp new file mode 100644 index 0000000000..c483b503c1 --- /dev/null +++ b/src/ipa/rppx1/algorithms/lsc.cpp @@ -0,0 +1,262 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2026, Ideas On Board + * + * RPP-X1 lens shading correction algorithm + */ + +#include "lsc.h" + +#include +#include +#include +#include +#include + +#include +#include + +#include "libcamera/internal/value_node.h" + +#include + +namespace libcamera { + +namespace ipa::rppx1::algorithms { + +LOG_DEFINE_CATEGORY(RppX1Lsc) + +namespace { + +using GradientQ = Q<12, 0>; + +constexpr int kColourTemperatureQuantization = 10; + +std::vector parseSizes(const ValueNode &tuningData, + const char *prop) +{ + std::vector sizes = + tuningData[prop].get>().value_or(utils::defopt); + if (sizes.size() != RPPX1_LSC_NUM_SECTORS) { + LOG(RppX1Lsc, Error) + << "Invalid '" << prop << "' values: expected " + << RPPX1_LSC_NUM_SECTORS + << " elements, got " << sizes.size(); + return {}; + } + + /* + * The sum of all elements must satisfy hardware constraints. + * Validate it here, allowing a 1% tolerance as rounding errors may + * prevent an exact match (further adjustments will be performed in + * LensShadingCorrection::prepare()). + */ + constexpr double expectedSum = 1.0; + + double sum = std::accumulate(sizes.begin(), sizes.end(), 0.0); + if (std::abs(sum - expectedSum) > 0.01) { + LOG(RppX1Lsc, Error) + << "Invalid '" << prop << "' values: sum of the elements" + << " should be " << expectedSum << ", got " << sum; + return {}; + } + + const auto &[min, max] = std::minmax_element(sizes.begin(), sizes.end()); + if (*min <= 0 || *max > 1) { + LOG(RppX1Lsc, Error) + << "Invalid '" << prop << "' values: elements must be in (0;1]"; + return {}; + } + + return sizes; +} + +std::vector sizesListToPositions(std::span sizes) +{ + std::vector positions(sizes.size() + 1); + + positions[0] = 0; + for (size_t i = 0; i < sizes.size(); i++) + positions[i + 1] = positions[i] + sizes[i]; + + return positions; +} + +unsigned int quantize(unsigned int value, unsigned int step) +{ + return std::lround(value / static_cast(step)) * step; +} + +} /* namespace */ + +int LensShadingCorrection::init([[maybe_unused]] IPAContext &context, + const ValueNode &tuningData) +{ + xSize_ = parseSizes(tuningData, "x-size"); + ySize_ = parseSizes(tuningData, "y-size"); + + if (xSize_.empty() || ySize_.empty()) + return -EINVAL; + + xPos_ = sizesListToPositions(xSize_); + yPos_ = sizesListToPositions(ySize_); + + return lscAlgo_.init(tuningData, context.ctrlMap, { + .keys = { "r", "gr", "gb", "b" }, + .numHSamples = RPPX1_LSC_SAMPLES_MAX, + .numVSamples = RPPX1_LSC_SAMPLES_MAX, + .sensorSize = context.sensorInfo.activeArea.size(), + }); +} + +int LensShadingCorrection::configure(IPAContext &context, + const IPACameraSensorInfo &configInfo) +{ + const Size &size = configInfo.outputSize; + Size totalSize{}; + + for (unsigned int i = 0; i < RPPX1_LSC_NUM_SECTORS; ++i) { + xSizes_[i] = xSize_[i] * size.width; + ySizes_[i] = ySize_[i] * size.height; + + /* + * From the documentation: + * + * The sum of all 16 {x,y}-size values must exactly match + * image {width,height}. + * + * Enforce it by computing the last table value separately to + * avoid rounding-induced errors. + */ + if (i == RPPX1_LSC_NUM_SECTORS - 1) { + xSizes_[i] = size.width - totalSize.width; + ySizes_[i] = size.height - totalSize.height; + } + + LOG(RppX1Lsc, Debug) + << i << ": " << xSizes_[i] << "=" << size.width << '*' << xSize_[i] << ", " + << ySizes_[i] << "=" << size.height << '*' << ySize_[i]; + + /* + * From the documentation: + * + * The sector size in {x,y}-direction must be greater than 7. + */ + if (xSizes_[i] < 8 || ySizes_[i] < 8) + return -EINVAL; + if (xSizes_[i] > 1023 || ySizes_[i] > 1023) + return -EINVAL; + + totalSize.width += xSizes_[i]; + totalSize.height += ySizes_[i]; + + /* + * From the documentation: + * + * The gradient values are related to the sector sizes and can be + * calculated for both x and y direction using the following equation: + * gradient[i] = int(2^15 / size[i] + 1/2) where i in [0;15] + */ + xGrad_[i] = GradientQ(float(1u << 15) / xSizes_[i]).quantized(); + yGrad_[i] = GradientQ(float(1u << 15) / ySizes_[i]).quantized(); + } + + lastAppliedCt_ = 0; + lastAppliedQuantizedCt_ = 0; + + return lscAlgo_.configure(context.activeState.lsc, configInfo.analogCrop, + xPos_, yPos_); +} + +void LensShadingCorrection::setParameters(rppx1_lsc_params &config) +{ + static_assert(sizeof(config.x_sect_size) == sizeof(xSizes_)); + memcpy(config.x_sect_size, xSizes_, sizeof(xSizes_)); + + static_assert(sizeof(config.y_sect_size) == sizeof(ySizes_)); + memcpy(config.y_sect_size, ySizes_, sizeof(ySizes_)); + + static_assert(sizeof(config.x_grad) == sizeof(xGrad_)); + memcpy(config.x_grad, xGrad_, sizeof(xGrad_)); + + static_assert(sizeof(config.y_grad) == sizeof(yGrad_)); + memcpy(config.y_grad, yGrad_, sizeof(yGrad_)); +} + +void LensShadingCorrection::copyTable(rppx1_lsc_params &config, + const lsc::Components &set) +{ + const auto &r = set.at("r"); + std::copy(r.begin(), r.end(), &config.r_data[0][0]); + const auto &gr = set.at("gr"); + std::copy(gr.begin(), gr.end(), &config.gr_data[0][0]); + const auto &gb = set.at("gb"); + std::copy(gb.begin(), gb.end(), &config.gb_data[0][0]); + const auto &b = set.at("b"); + std::copy(b.begin(), b.end(), &config.b_data[0][0]); +} + +void LensShadingCorrection::queueRequest(IPAContext &context, + [[maybe_unused]] const uint32_t frame, + IPAFrameContext &frameContext, + const ControlList &controls) +{ + lscAlgo_.queueRequest(context.activeState.lsc, frameContext.lsc, + controls); +} + +void LensShadingCorrection::prepare([[maybe_unused]] IPAContext &context, + [[maybe_unused]] const uint32_t frame, + IPAFrameContext &frameContext, + RppX1Params *params) +{ + uint32_t ct = frameContext.awb.colourTemperature; + unsigned int quantizedCt = quantize(ct, kColourTemperatureQuantization); + + /* Check if we can skip the update. */ + if (!frameContext.lsc.update) { + if (!frameContext.lsc.enabled) + return; + + /* + * Add a threshold so that oscillations around a quantization + * step don't lead to constant changes. + */ + if (utils::abs_diff(ct, lastAppliedCt_) < kColourTemperatureQuantization / 2) + return; + + if (quantizedCt == lastAppliedQuantizedCt_) + return; + } + + auto config = params->block(); + config.setEnabled(frameContext.lsc.enabled); + + if (!frameContext.lsc.enabled) + return; + + setParameters(*config); + + copyTable(*config, lscAlgo_.interpolateComponents(quantizedCt)); + + lastAppliedCt_ = ct; + lastAppliedQuantizedCt_ = quantizedCt; + + LOG(RppX1Lsc, Debug) + << "ct is " << ct << ", quantized to " << quantizedCt; +} + +void LensShadingCorrection::process([[maybe_unused]] IPAContext &context, + [[maybe_unused]] const uint32_t frame, + IPAFrameContext &frameContext, + [[maybe_unused]] const RppX1Stats *stats, + ControlList &metadata) +{ + lscAlgo_.process(frameContext.lsc, metadata); +} + +REGISTER_IPA_ALGORITHM(LensShadingCorrection, "LensShadingCorrection") + +} /* namespace ipa::rppx1::algorithms */ + +} /* namespace libcamera */ diff --git a/src/ipa/rppx1/algorithms/lsc.h b/src/ipa/rppx1/algorithms/lsc.h new file mode 100644 index 0000000000..d406ebd8ff --- /dev/null +++ b/src/ipa/rppx1/algorithms/lsc.h @@ -0,0 +1,58 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2026, Ideas On Board + * + * RPP-X1 lens shading correction algorithm + */ + +#pragma once + +#include + +#include "libipa/fixedpoint.h" +#include "libipa/lsc.h" + +#include "algorithm.h" + +namespace libcamera { + +namespace ipa::rppx1::algorithms { + +class LensShadingCorrection : public Algorithm +{ +public: + int init(IPAContext &context, const ValueNode &tuningData) override; + int configure(IPAContext &context, const IPACameraSensorInfo &configInfo) override; + void queueRequest(IPAContext &context, const uint32_t frame, + IPAFrameContext &frameContext, + const ControlList &controls) override; + void prepare(IPAContext &context, const uint32_t frame, + IPAFrameContext &frameContext, + RppX1Params *params) override; + void process(IPAContext &context, const uint32_t frame, + IPAFrameContext &frameContext, + const RppX1Stats *stats, + ControlList &metadata) override; +private: + void setParameters(rppx1_lsc_params &config); + void copyTable(rppx1_lsc_params &config, + const lsc::Components &set); + + std::vector xSize_; + std::vector ySize_; + std::vector xPos_; + std::vector yPos_; + uint16_t xGrad_[RPPX1_LSC_NUM_SECTORS]; + uint16_t yGrad_[RPPX1_LSC_NUM_SECTORS]; + uint16_t xSizes_[RPPX1_LSC_NUM_SECTORS]; + uint16_t ySizes_[RPPX1_LSC_NUM_SECTORS]; + + unsigned int lastAppliedCt_ = 0; + unsigned int lastAppliedQuantizedCt_ = 0; + + LscAlgorithm> lscAlgo_; +}; + +} /* namespace ipa::rppx1::algorithms */ + +} /* namespace libcamera */ diff --git a/src/ipa/rppx1/algorithms/meson.build b/src/ipa/rppx1/algorithms/meson.build index 0a73d26ed3..5a1068cd7f 100644 --- a/src/ipa/rppx1/algorithms/meson.build +++ b/src/ipa/rppx1/algorithms/meson.build @@ -7,5 +7,6 @@ rppx1_ipa_algorithms = files([ 'ccm.cpp', 'gamma.cpp', 'gsl.cpp', + 'lsc.cpp', 'lux.cpp', ]) diff --git a/src/ipa/rppx1/ipa_context.h b/src/ipa/rppx1/ipa_context.h index d965311ca1..538c30814b 100644 --- a/src/ipa/rppx1/ipa_context.h +++ b/src/ipa/rppx1/ipa_context.h @@ -22,6 +22,7 @@ #include #include #include +#include #include namespace libcamera { @@ -50,6 +51,8 @@ struct IPAActiveState { ipa::gamma::ActiveState gamma; + ipa::lsc::ActiveState lsc; + ipa::lux::ActiveState lux; }; @@ -70,6 +73,8 @@ struct IPAFrameContext : public FrameContext { ipa::gamma::FrameContext gamma; + ipa::lsc::FrameContext lsc; + ipa::lux::FrameContext lux; }; diff --git a/src/ipa/rppx1/params.h b/src/ipa/rppx1/params.h index 356c6311a9..958b0a9b6e 100644 --- a/src/ipa/rppx1/params.h +++ b/src/ipa/rppx1/params.h @@ -22,6 +22,8 @@ enum class BlockType : uint16_t { ExmPre1, GaHv, HistPost, + LscPre1, + LscPre2, LinPre1, WbMeasPost, }; @@ -46,6 +48,8 @@ RPPX1_DEFINE_BLOCK_TYPE(CcorPost, ccor, CCOR_POST) RPPX1_DEFINE_BLOCK_TYPE(ExmPre1, exm, EXM_PRE1) RPPX1_DEFINE_BLOCK_TYPE(GaHv, ga, GA_HV) RPPX1_DEFINE_BLOCK_TYPE(HistPost, hist, HIST_POST) +RPPX1_DEFINE_BLOCK_TYPE(LscPre1, lsc, LSC_PRE1) +RPPX1_DEFINE_BLOCK_TYPE(LscPre2, lsc, LSC_PRE2) RPPX1_DEFINE_BLOCK_TYPE(LinPre1, lin, LIN_PRE1) RPPX1_DEFINE_BLOCK_TYPE(WbMeasPost, wbmeas, WBMEAS_POST)