From patchwork Fri Jul 3 12:25:19 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Elder X-Patchwork-Id: 27172 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 9BF6CC328C for ; Fri, 3 Jul 2026 12:26:47 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 57CB765FEA; Fri, 3 Jul 2026 14:26:47 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="BzS6oDX7"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 7268265FDF for ; Fri, 3 Jul 2026 14:26:46 +0200 (CEST) Received: from neptunite.hamster-moth.ts.net (unknown [IPv6:2404:7a81:160:2100:a2cc:2f45:3bd7:2589]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 995ED8E0; Fri, 3 Jul 2026 14:25:57 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1783081560; bh=4LOkjL+2uX7mJcASdeNtWhBkBEuTo6ErAi5Zr5hjz7U=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BzS6oDX7OFtH5J+iUNp5cVdehrIq3ok7Hl01IqLB9bhtn6lKRW3/UBoRLNb5VTSyc kxGcDz45id3IBrj/eyfwk6JFUZgVQWG3pCrWo+hA/w0nCzl1JwP534U0UJHtSgpE6h FuQDQOqcanpXFx7jduEm7S4CGXB8jYeswu++43ik= From: Paul Elder To: laurent.pinchart@ideasonboard.com Cc: Paul Elder , michael.riesch@collabora.com, xuhf@rock-chips.com, stefan.klug@ideasonboard.com, kieran.bingham@ideasonboard.com, dan.scally@ideasonboard.com, jacopo.mondi@ideasonboard.com, nicolas.dufresne@collabora.com, libcamera-devel@lists.libcamera.org Subject: [RFC PATCH 13/19] ipa: rkisp2: algo: goc: Implement gamma out correction Date: Fri, 3 Jul 2026 21:25:19 +0900 Message-ID: <20260703122543.1991189-14-paul.elder@ideasonboard.com> X-Mailer: git-send-email 2.47.2 In-Reply-To: <20260703122543.1991189-1-paul.elder@ideasonboard.com> References: <20260703122543.1991189-1-paul.elder@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" Implement a manual gamma out correction algorithm for the rkisp2 IPA. It takes the gamma control and applies the gamma out correction curve to the ISP. Signed-off-by: Paul Elder --- src/ipa/rkisp2/algorithms/goc.cpp | 143 ++++++++++++++++++++++++++ src/ipa/rkisp2/algorithms/goc.h | 42 ++++++++ src/ipa/rkisp2/algorithms/meson.build | 1 + 3 files changed, 186 insertions(+) create mode 100644 src/ipa/rkisp2/algorithms/goc.cpp create mode 100644 src/ipa/rkisp2/algorithms/goc.h diff --git a/src/ipa/rkisp2/algorithms/goc.cpp b/src/ipa/rkisp2/algorithms/goc.cpp new file mode 100644 index 000000000000..d5cabeab8900 --- /dev/null +++ b/src/ipa/rkisp2/algorithms/goc.cpp @@ -0,0 +1,143 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2026, Ideas On Board + * + * RkISP2 Gamma out control + */ + +#include "goc.h" + +#include + +#include +#include + +#include + +#include "linux/rkisp2-config.h" + +/** + * \file goc.h + */ + +namespace libcamera { + +namespace ipa::rkisp2::algorithms { + +/** + * \class GammaOutCorrection + * \brief RkISP2 Gamma out correction + * + * This algorithm implements the gamma out curve for the RkISP2. It defaults to + * a gamma value of 2.2. + * + * As gamma is internally represented as a piecewise linear function with only + * 17 knots, the difference between gamma=2.2 and sRGB gamma is minimal. + * Therefore sRGB gamma was not implemented as special case. + * + * Useful links: + * - https://www.cambridgeincolour.com/tutorials/gamma-correction.htm + * - https://en.wikipedia.org/wiki/SRGB + */ + +LOG_DEFINE_CATEGORY(RkISP2Gamma) + +const float kDefaultGamma = 2.2f; + +/** + * \copydoc libcamera::ipa::Algorithm::init + */ +int GammaOutCorrection::init(IPAContext &context, const ValueNode &tuningData) +{ + defaultGamma_ = tuningData["gamma"].get(kDefaultGamma); + context.ctrlMap[&controls::Gamma] = ControlInfo(0.1f, 10.0f, defaultGamma_); + + return 0; +} + +/** + * \copydoc libcamera::ipa::Algorithm::configure + */ +int GammaOutCorrection::configure(IPAContext &context, + [[maybe_unused]] const IPACameraSensorInfo &configInfo) +{ + context.activeState.goc.gamma = defaultGamma_; + return 0; +} + +/** + * \copydoc libcamera::ipa::Algorithm::queueRequest + */ +void GammaOutCorrection::queueRequest(IPAContext &context, + [[maybe_unused]] const uint32_t frame, + IPAFrameContext &frameContext, + const ControlList &controls) +{ + frameContext.goc.gamma = context.activeState.goc.gamma; + + const auto &gamma = controls.get(controls::Gamma); + if (!gamma) + return; + + context.activeState.goc.gamma = *gamma; + + frameContext.goc.gamma = context.activeState.goc.gamma; +} + +/** + * \copydoc libcamera::ipa::Algorithm::prepare + */ +void GammaOutCorrection::prepare([[maybe_unused]] IPAContext &context, + [[maybe_unused]] const uint32_t frame, + [[maybe_unused]] IPAFrameContext &frameContext, + [[maybe_unused]] RkISP2Params *params) +{ + /* \todo Optimize so we don't have to write it every frame */ + + /* + * The logarithmic segments as specified in the reference, plus an + * additional 0 to make the loop easier + * + * Just use 44 mode for now. 48 mode is when the last four 512 are + * split into eight pieces of 256. + */ + static constexpr std::array segments = { + 0, 1, 1, 1, 1, 1, 1, 1, 1, + 2, 2, 2, 2, 4, 4, 4, 4, + 8, 8, 8, 8, 16, 16, 16, 16, + 32, 32, 32, 32, 64, 64, 64, 64, + 128, 128, 128, 128, 256, 256, 256, 256, + 512, 512, 512, 512 + }; + + auto config = params->block(); + config.setEnabled(true); + + unsigned x = 0; + for (const auto [i, size] : utils::enumerate(segments)) { + config->gamma_y[i] = static_cast(std::pow(x / 4096.0, 1.0 / frameContext.goc.gamma) * 4096.0); + x += size; + } + + config->mode = RKISP2_ISP_GOC_MODE_LOGARITHMIC; + config->segments = RKISP2_ISP_GOC_SEGMENTS_44; + config->offset = 0; +} + +/** + * \copydoc libcamera::ipa::Algorithm::process + */ +void GammaOutCorrection::process([[maybe_unused]] IPAContext &context, + [[maybe_unused]] const uint32_t frame, + IPAFrameContext &frameContext, + [[maybe_unused]] const rkisp2_stats_buffer *stats, + ControlList &metadata) +{ + metadata.set(controls::Gamma, frameContext.goc.gamma); +} + +REGISTER_IPA_ALGORITHM(GammaOutCorrection, "GammaOutCorrection") + +} /* namespace ipa::rkisp2::algorithms */ + +} /* namespace libcamera */ diff --git a/src/ipa/rkisp2/algorithms/goc.h b/src/ipa/rkisp2/algorithms/goc.h new file mode 100644 index 000000000000..e859f6606cd6 --- /dev/null +++ b/src/ipa/rkisp2/algorithms/goc.h @@ -0,0 +1,42 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2026, Ideas On Board + * + * RkISP2 Gamma out control + */ + +#pragma once + +#include "algorithm.h" + +namespace libcamera { + +namespace ipa::rkisp2::algorithms { + +class GammaOutCorrection : public Algorithm +{ +public: + GammaOutCorrection() = default; + ~GammaOutCorrection() = default; + + 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, + RkISP2Params *params) override; + void process(IPAContext &context, const uint32_t frame, + IPAFrameContext &frameContext, + const rkisp2_stats_buffer *stats, + ControlList &metadata) override; + +private: + float defaultGamma_; +}; + +} /* namespace ipa::rkisp2::algorithms */ +} /* namespace libcamera */ diff --git a/src/ipa/rkisp2/algorithms/meson.build b/src/ipa/rkisp2/algorithms/meson.build index 01829e25d254..3d73b0a99320 100644 --- a/src/ipa/rkisp2/algorithms/meson.build +++ b/src/ipa/rkisp2/algorithms/meson.build @@ -6,5 +6,6 @@ rkisp2_ipa_algorithms = files([ 'bls.cpp', 'ccm.cpp', 'csm.cpp', + 'goc.cpp', ])