From patchwork Tue Sep 8 16:37:08 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "D. Manresa" X-Patchwork-Id: 28210 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 0B2EDBDCC0 for ; Tue, 8 Sep 2026 16:37:09 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id CB2F768639; Tue, 8 Sep 2026 18:37:07 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="Z81g+b+z"; dkim-atps=neutral Received: from mail-wr1-x42d.google.com (mail-wr1-x42d.google.com [IPv6:2a00:1450:4864:20::42d]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id CBD4568632 for ; Tue, 8 Sep 2026 18:37:03 +0200 (CEST) Received: by mail-wr1-x42d.google.com with SMTP id ffacd0b85a97d-482f9309813so4527093f8f.1 for ; Tue, 08 Sep 2026 09:37:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1788885423; x=1789490223; darn=lists.libcamera.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=PqP5HL02LEAi5XIwSuHOUVN+emle9xozs8+rqIsalFw=; b=Z81g+b+zmvC5YT50C46LIvihCoCnhWJjJSeHN1nYgbupqy1nWzvXwFWpEHFTqre4IA WO7fZm4IvQstBuxRapvDVXhTJ67342MHm5alDgrQu8A7U0apSbAZwN9PBqSsG2lOdNwf ZNUlCNYkC32fQFpkcC/aykkYYwY1FqUoqqwk4Ktc7q37XzQkCH5rw39ovauknviEpmWQ nQpKrhGZFjfOJTQagTvHG6L8itIVwolk6ddjrNu4iUkoH7k97EmmIUY+PZDwUXdrrvt3 SiAgJpq50n1QQhwWsOXohvRz7y//PNxj2MPVswvR2xmiGbAa2NbPv3RWSigEzoHvXS8A xujg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1788885423; x=1789490223; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=PqP5HL02LEAi5XIwSuHOUVN+emle9xozs8+rqIsalFw=; b=I4ma7pdN4valugB+/VNmFTaqdmsA4cWSEUpUFgk9YFAPPAtAij2svH3tN5JT268Ekk g/ytuHf2i+5J1Trcagf5rBYAiHQ6SIoZSjPgDQNqbsrAVVIsB8HSgAJRxe5UhvonLCeF xHU8b78I4ppK5fkzk2TCaoT83RXIrF2SnyXsM5OqH6lInwLYui+LzcMTRi8EEnL3Ymr1 yyioh2+oy8k1P1MbFk/Psjv/6E6T80ckrp7INvi4/2IdObnhGmT8XRvhuMAo/A5B/T46 skW1Y+zl1TEX6bRbssCINiNzDZpAbD+yBpSxdRmMIgfYMrqxwum3JQjievi+BuT9SV4N /NjA== X-Gm-Message-State: AFuF++kbx9pmdsmjz0CWeW68upkbd0GCgujjECNP/gCzjJdGE/6VV8AY 4wfItGJnipnwK7vTfbBD8biom3aCq7ugGrM0j7Mlryp/5xa3K9Xhw3AB8SQ2o09btQ== X-Gm-Gg: AYBFou2caKYviSR/ssz1XwgMNPGpCJy3GHN9vvcsO6x/OyU2mN0x3z9SZMybNM1qvE1 8GrQ8FRbcvzvsRHjDaZJnP8aRTDvEWu625qC0c//Bp+qRcpeh3CqR72SgnfHHi2QSg8FjmmnKuS dh728xZqqKqOgECh1lNQYjB7WHEmNepHJuxWoyrqAKzIb68pDSwg/M00FtmiZdKMVX52zvexwff QlkHVTmomEpgBG+N7qgMlkC2ZVeM/k9MWbdpUEXEVE0YVIYB0X1Wc17j0Vcf7Gy3aXFSb2xIPoR I3WY1sSnDYC8nBf5IBRHDA3bI37lKwSmYvciPjHPCuguhKlygTpV3qCT1BxrCMeL6dUYg6LXpT2 YV26eCwiox0ZddoIQgYcJQ/x54vJIT042Gw4cl+jlbvIfeVnN7YiulP4YxF408nCOfD03wEo/Vd KZmotzzG9tOAbhW68AJVoaLPH+pCc4JPzm68pUWLc5aiqxMHQBw3LAiLJCcX9LxQ+v87grpMPOu ef3XT+fWfHtngo4Jn3iPTsduFO2SGEd6IaOadqPaost7ECUaX14qPN0jaE= X-Received: by 2002:a05:6000:4304:b0:484:3313:fd86 with SMTP id ffacd0b85a97d-485872d6572mr35632914f8f.27.1788885423228; Tue, 08 Sep 2026 09:37:03 -0700 (PDT) Received: from latitude7275 (84.124.213.91.dyn.user.ono.com. [84.124.213.91]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-48588394fa1sm35268362f8f.8.2026.09.08.09.37.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Sep 2026 09:37:02 -0700 (PDT) From: "D. Manresa" To: libcamera-devel@lists.libcamera.org Cc: Daniel Scally Subject: [PATCH v2 2/2] ipa: ipu3: blc: Use the black level from the camera sensor helper Date: Tue, 8 Sep 2026 18:37:08 +0200 Message-ID: <20260908163708.341308-3-dmanresa@gmail.com> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20260908163708.341308-1-dmanresa@gmail.com> References: <20260904062840.46739-3-dmanresa@gmail.com> <20260908163708.341308-1-dmanresa@gmail.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" The black level correction algorithm hard-codes an optical black level of 64 for the four Bayer channels. Take it from the camera sensor helper instead, like the RkISP1 IPA does, falling back to the historical value with a warning when the helper does not provide one. The helper reports the level as a 16-bit value, while the ImgU OB grid expects units of half a 10-bit LSB: on a Dell Latitude 7275 (OV5670, black level 64 at 10 bits) a correction of 64 leaves a residual pedestal in the AWB statistics and in the image, which the colour gains then amplify into a blue or cyan cast; 128 removes the pedestal and 192 clips dark areas to zero. The 16-bit value is therefore shifted right by 5, giving 128 for the OV5670. Measured on the same indoor scene with the uncalibrated tuning file (NV12, limited range, black = 16): with 64 the darkest percentile of Y is 32 and its mean chroma is U/V = 132/131; with 128 they are 15 and 128/128. Signed-off-by: D. Manresa --- src/ipa/ipu3/algorithms/blc.cpp | 56 +++++++++++++++++++++++++++------ src/ipa/ipu3/algorithms/blc.h | 5 +++ 2 files changed, 52 insertions(+), 9 deletions(-) diff --git a/src/ipa/ipu3/algorithms/blc.cpp b/src/ipa/ipu3/algorithms/blc.cpp index 35748fb..1cd2c6b 100644 --- a/src/ipa/ipu3/algorithms/blc.cpp +++ b/src/ipa/ipu3/algorithms/blc.cpp @@ -7,6 +7,10 @@ #include "blc.h" +#include + +#include "libipa/camera_sensor_helper.h" + /** * \file blc.h * \brief IPU3 Black Level Correction control @@ -30,8 +34,46 @@ namespace ipa::ipu3::algorithms { * isn't currently supported. */ +LOG_DEFINE_CATEGORY(IPU3Blc) + +/* + * Black level used when the camera sensor helper does not provide one, + * expressed in ImgU units (see init()). This is the historical value that + * was hard-coded before the level came from the helper. + */ +static constexpr int16_t kDefaultBlackLevel = 64; + BlackLevelCorrection::BlackLevelCorrection() + : blackLevel_(kDefaultBlackLevel) +{ +} + +/** + * \copydoc libcamera::ipa::Algorithm::init + * + * Get the sensor black level from the camera sensor helper. The helper + * reports it as a 16-bit value, while the ImgU OB grid expects it in units + * of half a 10-bit LSB: on an OV5670 (black level 64 at 10 bits) a + * correction of 64 leaves a residual pedestal of about 9/255 in the AWB + * statistics and in the image, 128 removes it, and 192 clips dark areas to + * zero. The 16-bit value is therefore shifted right by 5. + */ +int BlackLevelCorrection::init(IPAContext &context, + [[maybe_unused]] const ValueNode &tuningData) { + std::optional blackLevel = context.camHelper->blackLevel(); + if (!blackLevel) { + LOG(IPU3Blc, Warning) + << "No black level provided by camera sensor helper" + << ", please fix"; + blackLevel_ = kDefaultBlackLevel; + } else { + blackLevel_ = *blackLevel >> 5; + } + + LOG(IPU3Blc, Debug) << "Black level " << blackLevel_; + + return 0; } /** @@ -49,15 +91,11 @@ void BlackLevelCorrection::prepare([[maybe_unused]] IPAContext &context, [[maybe_unused]] IPAFrameContext &frameContext, ipu3_uapi_params *params) { - /* - * The Optical Black Level correction values - * \todo The correction values should come from sensor specific - * tuning processes. This is a first rough approximation. - */ - params->obgrid_param.gr = 64; - params->obgrid_param.r = 64; - params->obgrid_param.b = 64; - params->obgrid_param.gb = 64; + /* The Optical Black Level correction values */ + params->obgrid_param.gr = blackLevel_; + params->obgrid_param.r = blackLevel_; + params->obgrid_param.b = blackLevel_; + params->obgrid_param.gb = blackLevel_; /* Enable the custom black level correction processing */ params->use.obgrid = 1; diff --git a/src/ipa/ipu3/algorithms/blc.h b/src/ipa/ipu3/algorithms/blc.h index 6274804..58cdd92 100644 --- a/src/ipa/ipu3/algorithms/blc.h +++ b/src/ipa/ipu3/algorithms/blc.h @@ -18,9 +18,14 @@ class BlackLevelCorrection : public Algorithm public: BlackLevelCorrection(); + int init(IPAContext &context, const ValueNode &tuningData) override; + void prepare(IPAContext &context, const uint32_t frame, IPAFrameContext &frameContext, ipu3_uapi_params *params) override; + +private: + int16_t blackLevel_; }; } /* namespace ipa::ipu3::algorithms */