From patchwork Fri Jul 3 12:25:18 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Elder X-Patchwork-Id: 27171 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 003DDC328C for ; Fri, 3 Jul 2026 12:26:44 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 9D7E865FE7; Fri, 3 Jul 2026 14:26:44 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="TR/nWMVm"; 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 E876965FDF for ; Fri, 3 Jul 2026 14:26:42 +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 112981121; Fri, 3 Jul 2026 14:25:53 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1783081557; bh=DMsTFHJRsAKgRMApswYooOF/3mCrzmh/8E+8LHvAgdU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=TR/nWMVmFkiwWDGb/pDy5cbvGII7dBMF0M/8UUCGx64762Nmw35drDGnRJSwnhg6n l2adT9SkCRAXsk9K3S0h8+tVINTvMw+APoszHPUl0qzCJj9kRGYtsSu8Se3MGTI1C/ Z6g9zNCyUAGtK3UU0kvEB9FGw2dBDGuMieBMxmS8= 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 12/19] ipa: rkisp2: algo: csm: Implement color space conversion Date: Fri, 3 Jul 2026 21:25:18 +0900 Message-ID: <20260703122543.1991189-13-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 static color space conversion algorithm for the rkisp2 IPA. Signed-off-by: Paul Elder --- src/ipa/rkisp2/algorithms/csm.cpp | 177 ++++++++++++++++++++++++++ src/ipa/rkisp2/algorithms/csm.h | 34 +++++ src/ipa/rkisp2/algorithms/meson.build | 1 + 3 files changed, 212 insertions(+) create mode 100644 src/ipa/rkisp2/algorithms/csm.cpp create mode 100644 src/ipa/rkisp2/algorithms/csm.h diff --git a/src/ipa/rkisp2/algorithms/csm.cpp b/src/ipa/rkisp2/algorithms/csm.cpp new file mode 100644 index 000000000000..1733c2938d23 --- /dev/null +++ b/src/ipa/rkisp2/algorithms/csm.cpp @@ -0,0 +1,177 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2026, Ideas On Board + * + * RkISP2 Color space conversion + */ + +#include "csm.h" + +#include +#include + +#include +#include + +#include +#include + +#include "linux/rkisp2-config.h" + +/** + * \file csm.h + */ + +namespace libcamera { + +namespace ipa::rkisp2::algorithms { + +/** + * \class ColorSpaceConversion + * \brief RkISP2 Color space conversion + * + * This algorithm implements the color space conversion for the RkISP2. + */ + +LOG_DEFINE_CATEGORY(RkISP2Csm) + +namespace { + struct CsmCoeffs { + uint16_t limited[9]; + uint16_t full[9]; + }; + + static const struct CsmCoeffs rec601Coeffs = { + { + 0x0021, 0x0042, 0x000d, + 0x01ed, 0x01db, 0x0038, + 0x0038, 0x01d1, 0x01f7, + }, + { + 0x0026, 0x004b, 0x000f, + 0x01ea, 0x01d6, 0x0040, + 0x0040, 0x01ca, 0x01f6, + }, + }; + + static const struct CsmCoeffs rec709Coeffs = { + { + 0x0018, 0x0050, 0x0008, + 0x01f3, 0x01d5, 0x0038, + 0x0038, 0x01cd, 0x01fb, + }, + { + 0x001b, 0x005c, 0x0009, + 0x01f1, 0x01cf, 0x0040, + 0x0040, 0x01c6, 0x01fa, + }, + }; + + static const struct CsmCoeffs rec2020Coeffs = { + { + 0x001d, 0x004c, 0x0007, + 0x01f0, 0x01d8, 0x0038, + 0x0038, 0x01cd, 0x01fb, + }, + { + 0x0022, 0x0057, 0x0008, + 0x01ee, 0x01d2, 0x0040, + 0x0040, 0x01c5, 0x01fb, + }, + }; + + static const struct CsmCoeffs smpte240mCoeffs = { + { + 0x0018, 0x004f, 0x000a, + 0x01f3, 0x01d5, 0x0038, + 0x0038, 0x01ce, 0x01fa, + }, + { + 0x001b, 0x005a, 0x000b, + 0x01f1, 0x01cf, 0x0040, + 0x0040, 0x01c7, 0x01f9, + }, + }; + + uint16_t identityCsm[9] = { + 1, 0, 0, + 0, 1, 0, + 0, 0, 1, + }; +} /* namespace */ + +/** + * \copydoc libcamera::ipa::Algorithm::init + */ +int ColorSpaceConversion::init([[maybe_unused]] IPAContext &context, + [[maybe_unused]] const ValueNode &tuningData) +{ + return 0; +} + +/** + * \copydoc libcamera::ipa::Algorithm::configure + */ +int ColorSpaceConversion::configure(IPAContext &context, + [[maybe_unused]] const IPACameraSensorInfo &configInfo) +{ + const CsmCoeffs *coeffs; + switch (context.configuration.csm.colorSpaceEncoding) { + case static_cast(ColorSpace::YcbcrEncoding::Rec601): + coeffs = &rec601Coeffs; + break; + case static_cast(ColorSpace::YcbcrEncoding::Rec709): + coeffs = &rec709Coeffs; + break; + case static_cast(ColorSpace::YcbcrEncoding::Rec2020): + coeffs = &rec2020Coeffs; + break; + default: + coeffs = nullptr; + break; + } + + if (!coeffs) { + context.activeState.csm.csm = Matrix(identityCsm); + return 0; + } + + if (context.configuration.csm.colorSpaceRange == static_cast(ColorSpace::Range::Limited)) + context.activeState.csm.csm = Matrix(coeffs->limited); + else + context.activeState.csm.csm = Matrix(coeffs->full); + context.activeState.csm.update = true; + + return 0; +} + +/** + * \copydoc libcamera::ipa::Algorithm::prepare + */ +void ColorSpaceConversion::prepare(IPAContext &context, + [[maybe_unused]] const uint32_t frame, + [[maybe_unused]] IPAFrameContext &frameContext, + RkISP2Params *params) +{ + if (!context.activeState.csm.update) + return; + + auto config = params->block(); + config.setEnabled(true); + + /* + * We'll use the active state directly as we don't support runtime + * configuration of the csm + */ + for (size_t i = 0; i < 3; i++) + for (size_t j = 0; j < 3; j++) + config->coeff[i][j] = context.activeState.csm.csm[i][j]; + + context.activeState.csm.update = false; +} + +REGISTER_IPA_ALGORITHM(ColorSpaceConversion, "ColorSpaceConversion") + +} /* namespace ipa::rkisp2::algorithms */ + +} /* namespace libcamera */ diff --git a/src/ipa/rkisp2/algorithms/csm.h b/src/ipa/rkisp2/algorithms/csm.h new file mode 100644 index 000000000000..d34e4d3959e8 --- /dev/null +++ b/src/ipa/rkisp2/algorithms/csm.h @@ -0,0 +1,34 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2026, Ideas On Board + * + * RkISP2 Color space conversion + */ + +#pragma once + +#include "libcamera/internal/matrix.h" + +#include "algorithm.h" + +namespace libcamera { + +namespace ipa::rkisp2::algorithms { + +class ColorSpaceConversion : public Algorithm +{ +public: + ColorSpaceConversion() = default; + ~ColorSpaceConversion() = default; + + int init(IPAContext &context, const ValueNode &tuningData) override; + int configure(IPAContext &context, + const IPACameraSensorInfo &configInfo) override; + void prepare(IPAContext &context, const uint32_t frame, + IPAFrameContext &frameContext, + RkISP2Params *params) override; +}; + +} /* namespace ipa::rkisp2::algorithms */ + +} /* namespace libcamera */ diff --git a/src/ipa/rkisp2/algorithms/meson.build b/src/ipa/rkisp2/algorithms/meson.build index e7ae3d163bfb..01829e25d254 100644 --- a/src/ipa/rkisp2/algorithms/meson.build +++ b/src/ipa/rkisp2/algorithms/meson.build @@ -5,5 +5,6 @@ rkisp2_ipa_algorithms = files([ 'awb.cpp', 'bls.cpp', 'ccm.cpp', + 'csm.cpp', ])