From patchwork Wed Oct 9 19:33:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Milan Zamazal X-Patchwork-Id: 21569 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 94C9FC32DE for ; Wed, 9 Oct 2024 19:33:39 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 4331D65370; Wed, 9 Oct 2024 21:33:39 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.b="WSQgfGqe"; dkim-atps=neutral Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 5033163538 for ; Wed, 9 Oct 2024 21:33:37 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1728502416; 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=4s/TcOik+viXMR7VDGt05/Gtrx4cZxUaxgVi26AI9U4=; b=WSQgfGqeFiK4jFdSCIEn+XPxm9dRyIaqq7I3ycnzwXxOFDRSoQwsUn+WDClLg6+KD6LIrj IKbA1Zs0/TiGWabpcqupWLDhfto4BBhMzrGK5oH7Q06TV9bSep4E4Rr5T8uQtY5dNhsncP t8RkKpQN+4XCsQECw1CLYCLPqecQxh4= Received: from mx-prod-mc-05.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-140-4RhMX9aCNpyD-Pr3Q_07Rw-1; Wed, 09 Oct 2024 15:33:32 -0400 X-MC-Unique: 4RhMX9aCNpyD-Pr3Q_07Rw-1 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-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id EF95F19560A1; Wed, 9 Oct 2024 19:33:31 +0000 (UTC) Received: from nuthatch.redhat.com (unknown [10.45.224.78]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 651EC300018D; Wed, 9 Oct 2024 19:33:30 +0000 (UTC) From: Milan Zamazal To: libcamera-devel@lists.libcamera.org Cc: Milan Zamazal , Kieran Bingham Subject: [PATCH v2 1/2] libcamera: software_isp: Add contrast algorithm Date: Wed, 9 Oct 2024 21:33:16 +0200 Message-ID: <20241009193318.2394762-2-mzamazal@redhat.com> In-Reply-To: <20241009193318.2394762-1-mzamazal@redhat.com> References: <20241009193318.2394762-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-Originator: redhat.com 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" Software ISP is currently fully automatic and doesn't allow image modifications by explicitly set control values. The user has no means to make the image looking better. This patch introduces contrast control algorithm, which can improve e.g. a flat looking image. Based on the provided contrast value with a range 0..infinity and 1.0 being the normal value, it applies a simple S-curve modification to the image. The contrast algorithm just handles the provided values, while the S-curve is applied in the gamma algorithm on the computed gamma curve whenever the contrast value changes. Since the algorithm is applied only on the lookup table already present, its overhead is negligible. This is a preparation patch without actually activating the contrast algorithm, which will be done in the following patch. Signed-off-by: Milan Zamazal --- src/ipa/simple/algorithms/contrast.cpp | 45 ++++++++++++++++++++++++++ src/ipa/simple/algorithms/contrast.h | 37 +++++++++++++++++++++ src/ipa/simple/algorithms/lut.cpp | 20 +++++++++--- src/ipa/simple/algorithms/meson.build | 1 + src/ipa/simple/ipa_context.h | 7 ++++ 5 files changed, 105 insertions(+), 5 deletions(-) create mode 100644 src/ipa/simple/algorithms/contrast.cpp create mode 100644 src/ipa/simple/algorithms/contrast.h diff --git a/src/ipa/simple/algorithms/contrast.cpp b/src/ipa/simple/algorithms/contrast.cpp new file mode 100644 index 000000000..1a2c14cf9 --- /dev/null +++ b/src/ipa/simple/algorithms/contrast.cpp @@ -0,0 +1,45 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2024, Red Hat Inc. + * + * Contrast adjustment + */ + +#include "contrast.h" + +#include + +#include + +#include "control_ids.h" + +namespace libcamera { + +LOG_DEFINE_CATEGORY(IPASoftContrast) + +namespace ipa::soft::algorithms { + +int Contrast::configure(typename Module::Context &context, + [[maybe_unused]] const typename Module::Config &configInfo) +{ + context.activeState.knobs.contrast = std::optional(); + return 0; +} + +void Contrast::queueRequest(typename Module::Context &context, + [[maybe_unused]] const uint32_t frame, + [[maybe_unused]] typename Module::FrameContext &frameContext, + const ControlList &controls) +{ + const auto &contrast = controls.get(controls::Contrast); + if (contrast.has_value()) { + context.activeState.knobs.contrast = contrast; + LOG(IPASoftContrast, Debug) << "Setting contrast to " << contrast.value(); + } +} + +REGISTER_IPA_ALGORITHM(Contrast, "Contrast") + +} /* namespace ipa::soft::algorithms */ + +} /* namespace libcamera */ diff --git a/src/ipa/simple/algorithms/contrast.h b/src/ipa/simple/algorithms/contrast.h new file mode 100644 index 000000000..0b3933099 --- /dev/null +++ b/src/ipa/simple/algorithms/contrast.h @@ -0,0 +1,37 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2024, Red Hat Inc. + * + * Contrast adjustment + */ + +#pragma once + +#include + +#include "algorithm.h" + +namespace libcamera { + +namespace ipa::soft::algorithms { + +class Contrast : public Algorithm +{ +public: + Contrast() = default; + ~Contrast() = default; + + int configure(typename Module::Context &context, + const typename Module::Config &configInfo) + override; + + void queueRequest(typename Module::Context &context, + const uint32_t frame, + typename Module::FrameContext &frameContext, + const ControlList &controls) + override; +}; + +} /* namespace ipa::soft::algorithms */ + +} /* namespace libcamera */ diff --git a/src/ipa/simple/algorithms/lut.cpp b/src/ipa/simple/algorithms/lut.cpp index 9744e773a..8ebc824ce 100644 --- a/src/ipa/simple/algorithms/lut.cpp +++ b/src/ipa/simple/algorithms/lut.cpp @@ -32,16 +32,25 @@ int Lut::configure(IPAContext &context, void Lut::updateGammaTable(IPAContext &context) { auto &gammaTable = context.activeState.gamma.gammaTable; - auto blackLevel = context.activeState.blc.level; + const auto blackLevel = context.activeState.blc.level; const unsigned int blackIndex = blackLevel * gammaTable.size() / 256; + const auto contrast = context.activeState.knobs.contrast.value_or(1.0); std::fill(gammaTable.begin(), gammaTable.begin() + blackIndex, 0); const float divisor = gammaTable.size() - blackIndex - 1.0; - for (unsigned int i = blackIndex; i < gammaTable.size(); i++) - gammaTable[i] = UINT8_MAX * std::pow((i - blackIndex) / divisor, - context.configuration.gamma); + for (unsigned int i = blackIndex; i < gammaTable.size(); i++) { + double normalized = (i - blackIndex) / divisor; + /* Apply simple S-curve */ + if (normalized < 0.5) + normalized = 0.5 * std::pow(normalized / 0.5, contrast); + else + normalized = 1.0 - 0.5 * std::pow((1.0 - normalized) / 0.5, contrast); + gammaTable[i] = UINT8_MAX * + std::pow(normalized, context.configuration.gamma); + } context.activeState.gamma.blackLevel = blackLevel; + context.activeState.gamma.contrast = contrast; } void Lut::prepare(IPAContext &context, @@ -55,7 +64,8 @@ void Lut::prepare(IPAContext &context, * observed, it's not permanently prone to minor fluctuations or * rounding errors. */ - if (context.activeState.gamma.blackLevel != context.activeState.blc.level) + if (context.activeState.gamma.blackLevel != context.activeState.blc.level || + context.activeState.gamma.contrast != context.activeState.knobs.contrast) updateGammaTable(context); auto &gains = context.activeState.gains; diff --git a/src/ipa/simple/algorithms/meson.build b/src/ipa/simple/algorithms/meson.build index 37a2eb534..d75a7b2a1 100644 --- a/src/ipa/simple/algorithms/meson.build +++ b/src/ipa/simple/algorithms/meson.build @@ -4,5 +4,6 @@ soft_simple_ipa_algorithms = files([ 'awb.cpp', 'agc.cpp', 'blc.cpp', + 'contrast.cpp', 'lut.cpp', ]) diff --git a/src/ipa/simple/ipa_context.h b/src/ipa/simple/ipa_context.h index 3519f20f6..5118d6abf 100644 --- a/src/ipa/simple/ipa_context.h +++ b/src/ipa/simple/ipa_context.h @@ -8,8 +8,11 @@ #pragma once #include +#include #include +#include + #include namespace libcamera { @@ -44,7 +47,11 @@ struct IPAActiveState { struct { std::array gammaTable; uint8_t blackLevel; + double contrast; } gamma; + struct { + std::optional contrast; // 0..inf, 1 = neutral + } knobs; }; struct IPAFrameContext : public FrameContext {