{"id":22889,"url":"https://patchwork.libcamera.org/api/patches/22889/?format=json","web_url":"https://patchwork.libcamera.org/patch/22889/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20250227105733.187611-7-keke.li@amlogic.com>","date":"2025-02-27T10:57:28","name":"[v3,06/11] ipa: c3-isp: Add Awb algorithm","commit_ref":null,"pull_url":null,"state":"new","archived":false,"hash":"049813f83a2990728b47db4798718c3b56f0f345","submitter":{"id":217,"url":"https://patchwork.libcamera.org/api/people/217/?format=json","name":"Keke Li","email":"keke.li@amlogic.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/22889/mbox/","series":[{"id":5027,"url":"https://patchwork.libcamera.org/api/series/5027/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=5027","date":"2025-02-27T10:57:22","name":"Add Amlogic C3 ISP pipeline handler and IPA","version":3,"mbox":"https://patchwork.libcamera.org/series/5027/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/22889/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/22889/checks/","tags":{},"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 69ED1BF415\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu, 27 Feb 2025 10:58:56 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 277FB6875D;\n\tThu, 27 Feb 2025 11:58:56 +0100 (CET)","from mail-sh.amlogic.com (unknown [58.32.228.46])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id F394A68749\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 27 Feb 2025 11:58:54 +0100 (CET)","from droid10.amlogic.com (10.18.11.213) by mail-sh.amlogic.com\n\t(10.18.11.5) with Microsoft SMTP Server id 15.1.2507.39;\n\tThu, 27 Feb 2025 18:58:53 +0800"],"From":"Keke Li <keke.li@amlogic.com>","To":"<libcamera-devel@lists.libcamera.org>","CC":"<kieran.bingham@ideasonboard.com>, <laurent.pinchart@ideasonboard.com>, \n\t<dan.scally@ideasonboard.com>, Keke Li <keke.li@amlogic.com>","Subject":"[PATCH v3 06/11] ipa: c3-isp: Add Awb algorithm","Date":"Thu, 27 Feb 2025 18:57:28 +0800","Message-ID":"<20250227105733.187611-7-keke.li@amlogic.com>","X-Mailer":"git-send-email 2.29.0","In-Reply-To":"<20250227105733.187611-1-keke.li@amlogic.com>","References":"<20250227105733.187611-1-keke.li@amlogic.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Content-Type":"text/plain","X-Originating-IP":"[10.18.11.213]","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>"},"content":"Add a new Awb algorithm for the C3 ISP.\n\nSigned-off-by: Keke Li <keke.li@amlogic.com>\n---\n src/ipa/c3-isp/algorithms/awb.cpp     | 200 ++++++++++++++++++++++++++\n src/ipa/c3-isp/algorithms/awb.h       |  39 +++++\n src/ipa/c3-isp/algorithms/meson.build |   1 +\n 3 files changed, 240 insertions(+)\n create mode 100644 src/ipa/c3-isp/algorithms/awb.cpp\n create mode 100644 src/ipa/c3-isp/algorithms/awb.h","diff":"diff --git a/src/ipa/c3-isp/algorithms/awb.cpp b/src/ipa/c3-isp/algorithms/awb.cpp\nnew file mode 100644\nindex 00000000..786dcc81\n--- /dev/null\n+++ b/src/ipa/c3-isp/algorithms/awb.cpp\n@@ -0,0 +1,200 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2024, Amlogic\n+ *\n+ * C3ISP AWB control algorithm\n+ */\n+\n+#include \"awb.h\"\n+\n+#include <cmath>\n+\n+#include <libcamera/base/log.h>\n+#include <libcamera/base/utils.h>\n+\n+#include <libcamera/control_ids.h>\n+\n+#include \"libipa/colours.h\"\n+#include \"libipa/fixedpoint.h\"\n+\n+/**\n+ * \\file awb.h\n+ */\n+\n+namespace libcamera {\n+\n+namespace ipa::c3isp::algorithms {\n+\n+/**\n+ * \\class Awb\n+ * \\brief A Grey world white balance correction algorithm\n+ */\n+\n+LOG_DEFINE_CATEGORY(C3ISPAwb)\n+\n+Awb::Awb()\n+{\n+}\n+\n+/**\n+ * \\copydoc libcamera::ipa::Algorithm::configure\n+ */\n+int Awb::configure(IPAContext &context,\n+\t\t   [[maybe_unused]] const IPACameraSensorInfo &configInfo)\n+{\n+\tcontext.activeState.awb.rGain = 1.0;\n+\tcontext.activeState.awb.bGain = 1.0;\n+\n+\treturn 0;\n+}\n+\n+void Awb::fillGainsParam(IPAContext &context, IPAFrameContext &frameContext,\n+\t\t\t C3ISPParams *params)\n+{\n+\tdouble rGain = context.activeState.awb.rGain;\n+\tdouble bGain = context.activeState.awb.bGain;\n+\n+\tauto AWBGains = params->block<BlockType::AWBGains>();\n+\tAWBGains.setEnabled(C3_ISP_PARAMS_BLOCK_FL_ENABLE);\n+\n+\tAWBGains->gr_gain = floatingToFixedPoint<4, 8, uint16_t, double>(1.0);\n+\tAWBGains->r_gain = floatingToFixedPoint<4, 8, uint16_t, double>(rGain);\n+\tAWBGains->b_gain = floatingToFixedPoint<4, 8, uint16_t, double>(bGain);\n+\tAWBGains->gb_gain = floatingToFixedPoint<4, 8, uint16_t, double>(1.0);\n+\n+\tframeContext.awb.rGain = rGain;\n+\tframeContext.awb.bGain = bGain;\n+}\n+\n+void Awb::fillConfigParam(IPAContext &context, C3ISPParams *params)\n+{\n+\tauto AWBCfg = params->block<BlockType::AWBConfig>();\n+\tAWBCfg.setEnabled(C3_ISP_PARAMS_BLOCK_FL_ENABLE);\n+\n+\tAWBCfg->tap_point = C3_ISP_AWB_STATS_TAP_BEFORE_WB;\n+\tAWBCfg->satur_vald = 1;\n+\n+\t/* We use the full 32x24 zoning scheme */\n+\tAWBCfg->horiz_zones_num = 32;\n+\tAWBCfg->vert_zones_num = 24;\n+\n+\t/* The ratios themselves are stored in Q4.8 format */\n+\tAWBCfg->rg_min = 75;\n+\tAWBCfg->rg_max = 256;\n+\tAWBCfg->bg_min = 44;\n+\tAWBCfg->bg_max = 222;\n+\n+\t/* The triming of ratios are stored in Q4.8 format */\n+\tAWBCfg->rg_low = 93;\n+\tAWBCfg->rg_high = 244;\n+\tAWBCfg->bg_low = 61;\n+\tAWBCfg->bg_high = 205;\n+\n+\tSpan<uint8_t> weights{ AWBCfg->zone_weight, C3_ISP_AWB_MAX_ZONES };\n+\tstd::fill(weights.begin(), weights.end(), 1);\n+\n+\tSize sensorSize = context.configuration.sensor.size;\n+\tuint8_t maxPointNum =\n+\t\tstd::max(AWBCfg->horiz_zones_num, AWBCfg->vert_zones_num) + 1;\n+\n+\tfor (unsigned int i = 0; i < maxPointNum; i++) {\n+\t\tuint16_t hidx = i * sensorSize.width / AWBCfg->horiz_zones_num;\n+\n+\t\t/* Aligned with 2 */\n+\t\thidx = hidx / 2 * 2;\n+\t\tAWBCfg->horiz_coord[i] = std::min(hidx, (uint16_t)sensorSize.width);\n+\n+\t\tuint16_t vidx = i * sensorSize.height / AWBCfg->vert_zones_num;\n+\n+\t\t/* Aligned with 2 */\n+\t\tvidx = vidx / 2 * 2;\n+\t\tAWBCfg->vert_coord[i] = std::min(vidx, (uint16_t)sensorSize.height);\n+\t}\n+}\n+\n+/**\n+ * \\copydoc libcamera::ipa::Algorithm::prepare\n+ */\n+void Awb::prepare(IPAContext &context, const uint32_t frame,\n+\t\t  IPAFrameContext &frameContext, C3ISPParams *params)\n+{\n+\tfillGainsParam(context, frameContext, params);\n+\n+\tif (frame > 0)\n+\t\treturn;\n+\n+\tfillConfigParam(context, params);\n+}\n+\n+/**\n+ * \\copydoc libcamera::ipa::Algorithm::process\n+ */\n+void Awb::process(IPAContext &context, [[maybe_unused]] const uint32_t frame,\n+\t\t  IPAFrameContext &frameContext, const c3_isp_stats_info *stats,\n+\t\t  ControlList &metadata)\n+{\n+\tIPAActiveState &activeState = context.activeState;\n+\tconst struct c3_isp_awb_stats *awb = &stats->awb;\n+\n+\t/* The AWB statistics are R:G and B:G ratios of the zones. */\n+\tuint32_t counted_zones = 0;\n+\tdouble rgSum = 0, bgSum = 0;\n+\n+\tfor (unsigned int i = 0; i < C3_ISP_AWB_MAX_ZONES; i++) {\n+\t\tif (!awb->stats[i].pixel_sum)\n+\t\t\tcontinue;\n+\n+\t\t/* The statistics are in Q4.12 format. */\n+\t\trgSum += fixedToFloatingPoint<4, 12, double, uint16_t>(awb->stats[i].rg);\n+\t\tbgSum += fixedToFloatingPoint<4, 12, double, uint16_t>(awb->stats[i].bg);\n+\n+\t\tcounted_zones++;\n+\t}\n+\n+\tdouble rgAvg, bgAvg;\n+\tif (!counted_zones) {\n+\t\trgAvg = 1.0;\n+\t\tbgAvg = 1.0;\n+\t} else {\n+\t\trgAvg = rgSum / counted_zones;\n+\t\tbgAvg = bgSum / counted_zones;\n+\t}\n+\n+\t/*\n+\t * To simplify the calculation, the average green is hardcoded\n+\t * to 1.0.\n+\t */\n+\tactiveState.awb.temperatureK = estimateCCT({ { rgAvg, 1.0, bgAvg } });\n+\n+\t/* Metadata shall contain the up to date measurement */\n+\tmetadata.set(controls::ColourTemperature, activeState.awb.temperatureK);\n+\n+\t/*\n+\t * Estimate the red and blue gains to apply in a grey world.\n+\t * The green gain is hardcoded to 1.0. Avoid division by zero\n+\t * by clamping the divisor to mininum value of 0.0625.\n+\t */\n+\tdouble rGain = 1 / std::max(rgAvg, 0.0625);\n+\tdouble bGain = 1 / std::max(bgAvg, 0.0625);\n+\n+\t/* Filter the values to avoid oscillations. */\n+\tdouble speed = 0.2;\n+\trGain = speed * rGain + (1 - speed) * activeState.awb.rGain;\n+\tbGain = speed * bGain + (1 - speed) * activeState.awb.bGain;\n+\n+\tactiveState.awb.rGain = rGain;\n+\tactiveState.awb.bGain = bGain;\n+\n+\tmetadata.set(controls::ColourGains, { static_cast<float>(frameContext.awb.rGain),\n+\t\t\t\t\t      static_cast<float>(frameContext.awb.bGain) });\n+\n+\tLOG(C3ISPAwb, Debug) << \"Gains: R \" << activeState.awb.rGain\n+\t\t\t     << \", B \" << activeState.awb.bGain\n+\t\t\t     << \", Ct \" << activeState.awb.temperatureK << \"K\";\n+}\n+\n+REGISTER_IPA_ALGORITHM(Awb, \"Awb\")\n+\n+} /* namespace ipa::c3isp::algorithms */\n+\n+} /* namespace libcamera */\ndiff --git a/src/ipa/c3-isp/algorithms/awb.h b/src/ipa/c3-isp/algorithms/awb.h\nnew file mode 100644\nindex 00000000..bd036c11\n--- /dev/null\n+++ b/src/ipa/c3-isp/algorithms/awb.h\n@@ -0,0 +1,39 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2024, Amlogic\n+ *\n+ * C3ISP AWB control algorithm\n+ */\n+\n+#pragma once\n+\n+#include \"algorithm.h\"\n+\n+namespace libcamera {\n+\n+namespace ipa::c3isp::algorithms {\n+\n+class Awb : public Algorithm\n+{\n+public:\n+\tAwb();\n+\t~Awb() = default;\n+\n+\tint configure(IPAContext &context, const IPACameraSensorInfo &configInfo) override;\n+\tvoid prepare(IPAContext &context, const uint32_t frame,\n+\t\t     IPAFrameContext &frameContext,\n+\t\t     C3ISPParams *params) override;\n+\tvoid process(IPAContext &context, const uint32_t frame,\n+\t\t     IPAFrameContext &frameContext,\n+\t\t     const c3_isp_stats_info *stats,\n+\t\t     ControlList &metadata) override;\n+\n+private:\n+\tvoid fillGainsParam(IPAContext &context, IPAFrameContext &frameContext,\n+\t\t\t    C3ISPParams *params);\n+\tvoid fillConfigParam(IPAContext &context, C3ISPParams *params);\n+};\n+\n+} /* namespace ipa::c3isp::algorithms */\n+\n+} /* namespace libcamera */\ndiff --git a/src/ipa/c3-isp/algorithms/meson.build b/src/ipa/c3-isp/algorithms/meson.build\nindex 3e44fe91..9460da99 100644\n--- a/src/ipa/c3-isp/algorithms/meson.build\n+++ b/src/ipa/c3-isp/algorithms/meson.build\n@@ -2,4 +2,5 @@\n \n c3isp_ipa_algorithms = files([\n     'agc.cpp',\n+    'awb.cpp',\n ])\n","prefixes":["v3","06/11"]}