[{"id":34149,"web_url":"https://patchwork.libcamera.org/comment/34149/","msgid":"<aBuWRrbolKL7cAhn@pyrite.rasen.tech>","date":"2025-05-07T17:20:06","subject":"Re: [PATCH v3 12/16] ipa: rkisp1: Implement manual\n\tColourCorrectionMatrix control","submitter":{"id":17,"url":"https://patchwork.libcamera.org/api/people/17/","name":"Paul Elder","email":"paul.elder@ideasonboard.com"},"content":"On Thu, Apr 03, 2025 at 05:49:17PM +0200, Stefan Klug wrote:\n> Add a manual ColourCorrectionMatrix control. This was already discussed\n> while implementing manual colour temperature but was never implemented.\n> The control allows to manually specify the CCM when AwbEnable is false.\n> \n> Signed-off-by: Stefan Klug <stefan.klug@ideasonboard.com>\n> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n\nReviewed-by: Paul Elder <paul.elder@ideasonboard.com>\n\n> \n> ---\n> \n> Changes in v2:\n> - None\n> \n> Changes in v3:\n> - Removed unnecessary log statement\n> - Improved debug logging for manual ccm\n> - Added documentation for IPAActiveState::ccm\n> - Added documentation for IPAFrameContext::ccm\n> ---\n>  src/ipa/rkisp1/algorithms/ccm.cpp | 62 ++++++++++++++++++++++++++++---\n>  src/ipa/rkisp1/algorithms/ccm.h   |  6 +++\n>  src/ipa/rkisp1/ipa_context.cpp    | 19 ++++++++++\n>  src/ipa/rkisp1/ipa_context.h      |  3 +-\n>  4 files changed, 84 insertions(+), 6 deletions(-)\n> \n> diff --git a/src/ipa/rkisp1/algorithms/ccm.cpp b/src/ipa/rkisp1/algorithms/ccm.cpp\n> index 2e5e91006b55..3a96a5427bc6 100644\n> --- a/src/ipa/rkisp1/algorithms/ccm.cpp\n> +++ b/src/ipa/rkisp1/algorithms/ccm.cpp\n> @@ -36,17 +36,25 @@ namespace ipa::rkisp1::algorithms {\n>  \n>  LOG_DEFINE_CATEGORY(RkISP1Ccm)\n>  \n> +constexpr Matrix<float, 3, 3> kIdentity3x3 = Matrix<float, 3, 3>::identity();\n> +\n>  /**\n>   * \\copydoc libcamera::ipa::Algorithm::init\n>   */\n>  int Ccm::init([[maybe_unused]] IPAContext &context, const YamlObject &tuningData)\n>  {\n> +\tauto &cmap = context.ctrlMap;\n> +\tcmap[&controls::ColourCorrectionMatrix] = ControlInfo(\n> +\t\tControlValue(-8.0f),\n> +\t\tControlValue(7.993f),\n> +\t\tControlValue(kIdentity3x3.data()));\n> +\n>  \tint ret = ccm_.readYaml(tuningData[\"ccms\"], \"ct\", \"ccm\");\n>  \tif (ret < 0) {\n>  \t\tLOG(RkISP1Ccm, Warning)\n>  \t\t\t<< \"Failed to parse 'ccm' \"\n>  \t\t\t<< \"parameter from tuning file; falling back to unit matrix\";\n> -\t\tccm_.setData({ { 0, Matrix<float, 3, 3>::identity() } });\n> +\t\tccm_.setData({ { 0, kIdentity3x3 } });\n>  \t}\n>  \n>  \tret = offsets_.readYaml(tuningData[\"ccms\"], \"ct\", \"offsets\");\n> @@ -61,13 +69,51 @@ int Ccm::init([[maybe_unused]] IPAContext &context, const YamlObject &tuningData\n>  \treturn 0;\n>  }\n>  \n> +/**\n> + * \\copydoc libcamera::ipa::Algorithm::configure\n> + */\n> +int Ccm::configure(IPAContext &context,\n> +\t\t   [[maybe_unused]] const IPACameraSensorInfo &configInfo)\n> +{\n> +\tauto &as = context.activeState;\n> +\tas.ccm.manual = kIdentity3x3;\n> +\tas.ccm.automatic = ccm_.getInterpolated(as.awb.automatic.temperatureK);\n> +\treturn 0;\n> +}\n> +\n> +void Ccm::queueRequest(IPAContext &context,\n> +\t\t       [[maybe_unused]] const uint32_t frame,\n> +\t\t       IPAFrameContext &frameContext,\n> +\t\t       const ControlList &controls)\n> +{\n> +\t/* Nothing to do here, the ccm will be calculated in prepare() */\n> +\tif (frameContext.awb.autoEnabled)\n> +\t\treturn;\n> +\n> +\tauto &ccm = context.activeState.ccm;\n> +\n> +\tconst auto &colourTemperature = controls.get(controls::ColourTemperature);\n> +\tconst auto &ccmMatrix = controls.get(controls::ColourCorrectionMatrix);\n> +\tif (ccmMatrix) {\n> +\t\tccm.manual = Matrix<float, 3, 3>(*ccmMatrix);\n> +\t\tLOG(RkISP1Ccm, Debug)\n> +\t\t\t<< \"Setting manual CCM from CCM control to \" << ccm.manual;\n> +\t} else if (colourTemperature) {\n> +\t\tccm.manual = ccm_.getInterpolated(*colourTemperature);\n> +\t\tLOG(RkISP1Ccm, Debug)\n> +\t\t\t<< \"Setting manual CCM from CT control to \" << ccm.manual;\n> +\t}\n> +\n> +\tframeContext.ccm.ccm = ccm.manual;\n> +}\n> +\n>  void Ccm::setParameters(struct rkisp1_cif_isp_ctk_config &config,\n>  \t\t\tconst Matrix<float, 3, 3> &matrix,\n>  \t\t\tconst Matrix<int16_t, 3, 1> &offsets)\n>  {\n>  \t/*\n>  \t * 4 bit integer and 7 bit fractional, ranging from -8 (0x400) to\n> -\t * +7.992 (0x3ff)\n> +\t * +7.9921875 (0x3ff)\n>  \t */\n>  \tfor (unsigned int i = 0; i < 3; i++) {\n>  \t\tfor (unsigned int j = 0; j < 3; j++)\n> @@ -88,14 +134,20 @@ void Ccm::setParameters(struct rkisp1_cif_isp_ctk_config &config,\n>  void Ccm::prepare(IPAContext &context, const uint32_t frame,\n>  \t\t  IPAFrameContext &frameContext, RkISP1Params *params)\n>  {\n> -\tuint32_t ct = frameContext.awb.temperatureK;\n> +\tif (!frameContext.awb.autoEnabled) {\n> +\t\tauto config = params->block<BlockType::Ctk>();\n> +\t\tconfig.setEnabled(true);\n> +\t\tsetParameters(*config, frameContext.ccm.ccm, Matrix<int16_t, 3, 1>());\n> +\t\treturn;\n> +\t}\n>  \n> +\tuint32_t ct = frameContext.awb.temperatureK;\n>  \t/*\n>  \t * \\todo The colour temperature will likely be noisy, add filtering to\n>  \t * avoid updating the CCM matrix all the time.\n>  \t */\n>  \tif (frame > 0 && ct == ct_) {\n> -\t\tframeContext.ccm.ccm = context.activeState.ccm.ccm;\n> +\t\tframeContext.ccm.ccm = context.activeState.ccm.automatic;\n>  \t\treturn;\n>  \t}\n>  \n> @@ -103,7 +155,7 @@ void Ccm::prepare(IPAContext &context, const uint32_t frame,\n>  \tMatrix<float, 3, 3> ccm = ccm_.getInterpolated(ct);\n>  \tMatrix<int16_t, 3, 1> offsets = offsets_.getInterpolated(ct);\n>  \n> -\tcontext.activeState.ccm.ccm = ccm;\n> +\tcontext.activeState.ccm.automatic = ccm;\n>  \tframeContext.ccm.ccm = ccm;\n>  \n>  \tauto config = params->block<BlockType::Ctk>();\n> diff --git a/src/ipa/rkisp1/algorithms/ccm.h b/src/ipa/rkisp1/algorithms/ccm.h\n> index a5d9a9a45e5d..c301e6e531c8 100644\n> --- a/src/ipa/rkisp1/algorithms/ccm.h\n> +++ b/src/ipa/rkisp1/algorithms/ccm.h\n> @@ -26,6 +26,12 @@ public:\n>  \t~Ccm() = default;\n>  \n>  \tint init(IPAContext &context, const YamlObject &tuningData) override;\n> +\tint configure(IPAContext &context,\n> +\t\t      const IPACameraSensorInfo &configInfo) override;\n> +\tvoid queueRequest(IPAContext &context,\n> +\t\t\t  const uint32_t frame,\n> +\t\t\t  IPAFrameContext &frameContext,\n> +\t\t\t  const ControlList &controls) override;\n>  \tvoid prepare(IPAContext &context, const uint32_t frame,\n>  \t\t     IPAFrameContext &frameContext,\n>  \t\t     RkISP1Params *params) override;\n> diff --git a/src/ipa/rkisp1/ipa_context.cpp b/src/ipa/rkisp1/ipa_context.cpp\n> index 7bc42e6de415..cec73c9610da 100644\n> --- a/src/ipa/rkisp1/ipa_context.cpp\n> +++ b/src/ipa/rkisp1/ipa_context.cpp\n> @@ -210,6 +210,17 @@ namespace libcamera::ipa::rkisp1 {\n>   * \\brief Whether the Auto White Balance algorithm is enabled\n>   */\n>  \n> +/**\n> + * \\var IPAActiveState::ccm\n> + * \\brief State for the Colour Correction Matrix algorithm\n> + *\n> + * \\var IPAActiveState::ccm.manual\n> + * \\brief Manual CCM (set through requests)\n> + *\n> + * \\var IPAActiveState::awb.automatic\n> + * \\brief Automatic CCM (computed by the algorithm)\n> + */\n> +\n>  /**\n>   * \\var IPAActiveState::cproc\n>   * \\brief State for the Color Processing algorithm\n> @@ -355,6 +366,14 @@ namespace libcamera::ipa::rkisp1 {\n>   * \\brief Whether the Auto White Balance algorithm is enabled\n>   */\n>  \n> +/**\n> + * \\var IPAFrameContext::ccm\n> + * \\brief Colour Correction Matrix parameters for this frame\n> + *\n> + * \\struct IPAFrameContext::ccm.ccm\n> + * \\brief Colour Correction Matrix\n> + */\n> +\n>  /**\n>   * \\var IPAFrameContext::cproc\n>   * \\brief Color Processing parameters for this frame\n> diff --git a/src/ipa/rkisp1/ipa_context.h b/src/ipa/rkisp1/ipa_context.h\n> index 769e9f114e23..f0d504215d34 100644\n> --- a/src/ipa/rkisp1/ipa_context.h\n> +++ b/src/ipa/rkisp1/ipa_context.h\n> @@ -101,7 +101,8 @@ struct IPAActiveState {\n>  \t} awb;\n>  \n>  \tstruct {\n> -\t\tMatrix<float, 3, 3> ccm;\n> +\t\tMatrix<float, 3, 3> manual;\n> +\t\tMatrix<float, 3, 3> automatic;\n>  \t} ccm;\n>  \n>  \tstruct {\n> -- \n> 2.43.0\n>","headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 96931C3226\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed,  7 May 2025 17:20:13 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 023A968B2F;\n\tWed,  7 May 2025 19:20:13 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 8691668AD8\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed,  7 May 2025 19:20:11 +0200 (CEST)","from pyrite.rasen.tech (unknown\n\t[IPv6:2001:861:3a80:3300:4f2f:8c2c:b3ef:17d4])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id CC662C59;\n\tWed,  7 May 2025 19:19:59 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"SJzyMbds\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1746638399;\n\tbh=+rA5vLQap625K2Vx9lauNwgmHmODIwibFpwpwltV/eg=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=SJzyMbdsi9iKevG5iuQP1PtlztzQ5GBEwYT1y8mfzlP03I1ieBAhsVrvpDjTk6xzt\n\tSD+AALrxpqpTYyyX1+6BCKdrEKzSn3XKrp0HOaaO2aVtXamarcNp/3eVjeep7w4/cO\n\t9N3yE/8wSsfgkggkIucfmXG0HZVw8znuOfZ8F34Y=","Date":"Wed, 7 May 2025 19:20:06 +0200","From":"Paul Elder <paul.elder@ideasonboard.com>","To":"Stefan Klug <stefan.klug@ideasonboard.com>","Cc":"libcamera-devel@lists.libcamera.org,\n\tKieran Bingham <kieran.bingham@ideasonboard.com>","Subject":"Re: [PATCH v3 12/16] ipa: rkisp1: Implement manual\n\tColourCorrectionMatrix control","Message-ID":"<aBuWRrbolKL7cAhn@pyrite.rasen.tech>","References":"<20250403154925.382973-1-stefan.klug@ideasonboard.com>\n\t<20250403154925.382973-13-stefan.klug@ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=us-ascii","Content-Disposition":"inline","In-Reply-To":"<20250403154925.382973-13-stefan.klug@ideasonboard.com>","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]