From patchwork Wed Nov 18 16:46:43 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: GuyliandeJong X-Patchwork-Id: 29023 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 156B7C3304 for ; Wed, 18 Nov 2026 16:46:51 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id B54096A900; Wed, 18 Nov 2026 17:46:50 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (2048-bit key; unprotected) header.d=proton.me header.i=@proton.me header.b="l28wJf2Z"; dkim-atps=neutral Received: from mail-4316.protonmail.ch (mail-4316.protonmail.ch [185.70.43.16]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 714A66A8FA for ; Wed, 18 Nov 2026 17:46:49 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=protonmail2; t=1795020408; x=1795279608; bh=ACGa6Ev5jWO/JMLfyPtURwObeFZB+7HOZcYiddwFyfo=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=l28wJf2ZTaOLNJ6A8l+Zx5CGiUFYQ82Ip1E32jzp7+YGJORcUdl5shrejnQdA1ZaG Ursoaq4zUKi0hpjPF6nMbXhU5jjDwN4R9Gxi2efmqMLkHcNE6WA62HIEVJq/ZYNfJ1 CmWFhd8e6Wfu5S9+F8go2mHaV1sqIEsjN6QLojewUYeBxJ14VuCNzZQQkZoEmyaTIU 7X7PUK7C2TC+CfwoMsJm5HyyVWHYZYKnp5GDByS1qpk5Sqh7C4BvR5ef8iZdX8uRPp XFSfDngJM3PGlALhB0Gv9693xG9aOULxPr7JR7tWSE9+4nsWYK9BRUSzZ2ycSsMKjK E60w2p5GQ4KRw== Date: Wed, 18 Nov 2026 16:46:43 +0000 To: libcamera-devel@lists.libcamera.org From: GuyliandeJong Cc: Daniel Scally , Barnabas Pocze , Afonso Figuinha Subject: [PATCH 2/6] ipa: ipu3: lsc: Subtract the base gain from the shading LUT values Message-ID: <20261118164621.15513-3-guyliandejong@proton.me> In-Reply-To: <20261118164621.15513-1-guyliandejong@proton.me> References: <20261118164621.15513-1-guyliandejong@proton.me> Feedback-ID: 112452169:user:proton X-Pm-Message-ID: fdd4d345eb1d1569915877a65cf73b7ea074da14 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" The ImgU adds a base gain of 1.0 to the shading LUT values, as the comment in prepare() already assumes. The gains produced by LscAlgorithm are total gains in UQ<2, 10> format, where 1024 is a gain of 1.0, and are written to the LUT as is. Every gain is thus applied with an extra 1.0: a table of unity gains doubles the image brightness instead of leaving it unchanged, and the relative correction towards the corners is weakened. Measured on an OV9734 with a fixed exposure and a polynomial table of unity gains, enabling shading correction made the image about 1.7 times brighter. With this change it leaves the image unchanged, and a radial table leaves the centre of the image unchanged while brightening the corners. Subtract the base gain of 1.0 when filling the LUT, clamping gains lower than 1.0 as the hardware can't apply them. Fixes: 279d355ef8f7 ("ipa: ipu3: Add Lens Shading Correction algorithm") Signed-off-by: GuyliandeJong --- src/ipa/ipu3/algorithms/lsc.cpp | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/ipa/ipu3/algorithms/lsc.cpp b/src/ipa/ipu3/algorithms/lsc.cpp index 729a23ff..1991d6ee 100644 --- a/src/ipa/ipu3/algorithms/lsc.cpp +++ b/src/ipa/ipu3/algorithms/lsc.cpp @@ -32,6 +32,9 @@ static constexpr unsigned int kMaxNumHCells = 73; static constexpr unsigned int kMaxNumVCells = 56; static constexpr int kColourTemperatureQuantization = 10; +/* Gain of 1.0 in the UQ<2, 10> format of the LscAlgorithm gains */ +static constexpr uint16_t kUnityGain = 1 << 10; + /** * \copydoc libcamera::ipa::Algorithm::init */ @@ -238,6 +241,11 @@ void Lsc::prepare([[maybe_unused]] IPAContext &context, [[maybe_unused]] const u * And that a base gain of 1.0 is added to those configured values. As a * gain of more than 5.0 is fairly unlikely, let's fix gain_factor to 0 * for now and revisit if needed. + * + * Measurements on an OV9734 match this: a LUT value of 0 leaves the + * image unchanged, and 1024 doubles it. As LscAlgorithm produces the + * total gain, the base gain of 1.0 is subtracted when filling the LUT + * below. Gains lower than 1.0 can't be represented and are clamped. */ config->general.gain_factor = 0; @@ -260,12 +268,16 @@ void Lsc::prepare([[maybe_unused]] IPAContext &context, [[maybe_unused]] const u config->grid.grid_height_per_slice; unsigned int i = 0; + const auto lutValue = [](uint16_t gain) -> uint16_t { + return gain > kUnityGain ? gain - kUnityGain : 0; + }; + for (unsigned int s = 0; s < numSets; s++) { for (unsigned int c = 0; c < cellsPerSet && i < totalCells; c++, i++) { - lut->sets[s].r_and_gr[c].r = set.at("r")[i]; - lut->sets[s].r_and_gr[c].gr = set.at("gr")[i]; - lut->sets[s].gb_and_b[c].gb = set.at("gb")[i]; - lut->sets[s].gb_and_b[c].b = set.at("b")[i]; + lut->sets[s].r_and_gr[c].r = lutValue(set.at("r")[i]); + lut->sets[s].r_and_gr[c].gr = lutValue(set.at("gr")[i]); + lut->sets[s].gb_and_b[c].gb = lutValue(set.at("gb")[i]); + lut->sets[s].gb_and_b[c].b = lutValue(set.at("b")[i]); } }