{"id":26881,"url":"https://patchwork.libcamera.org/api/1.1/patches/26881/?format=json","web_url":"https://patchwork.libcamera.org/patch/26881/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/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":"<20260615-libipa-algorithms-v1-4-e949c937422e@ideasonboard.com>","date":"2026-06-15T14:05:29","name":"[04/11] ipa: simple: awb: Port to use libipa AwbAlgorithm","commit_ref":null,"pull_url":null,"state":"new","archived":false,"hash":"6c3c5eae482d7b76a197263fc97c4bbfcf09f3a0","submitter":{"id":143,"url":"https://patchwork.libcamera.org/api/1.1/people/143/?format=json","name":"Jacopo Mondi","email":"jacopo.mondi@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/26881/mbox/","series":[{"id":5992,"url":"https://patchwork.libcamera.org/api/1.1/series/5992/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=5992","date":"2026-06-15T14:05:25","name":"ipa: libipa: Introduce libipa algorithms","version":1,"mbox":"https://patchwork.libcamera.org/series/5992/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/26881/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/26881/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 A49CDC324C\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon, 15 Jun 2026 14:06:01 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 4299B623ED;\n\tMon, 15 Jun 2026 16:05:55 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 07B4B623CB\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 15 Jun 2026 16:05:48 +0200 (CEST)","from [192.168.1.104] (net-93-65-100-155.cust.vodafonedsl.it\n\t[93.65.100.155])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 3232E1D98;\n\tMon, 15 Jun 2026 16:05:15 +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=\"nODVaxyx\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1781532315;\n\tbh=R0z4AH0Gi3fmsi1UQyPGyIa89+mgUYL1fXQwatR4Xrk=;\n\th=From:Date:Subject:References:In-Reply-To:To:Cc:From;\n\tb=nODVaxyxpqn02qEJvYA7cylc0tQwyPuaqKuJ/gW7GAmOfF1lF+L9NzQgQxRyNl+j/\n\ttOaVkSprLrf+MWNsVfjZXJmMkVCkUnnKCaCLQfXn1J6j8jbs7OKpxpNIkx6qYYJOrF\n\t7IBNg+j4iRz2bI3gT6NMfQ635kfBFR8FD+zhnGOc=","From":"Jacopo Mondi <jacopo.mondi@ideasonboard.com>","Date":"Mon, 15 Jun 2026 16:05:29 +0200","Subject":"[PATCH 04/11] ipa: simple: awb: Port to use libipa AwbAlgorithm","MIME-Version":"1.0","Content-Type":"text/plain; charset=\"utf-8\"","Content-Transfer-Encoding":"7bit","Message-Id":"<20260615-libipa-algorithms-v1-4-e949c937422e@ideasonboard.com>","References":"<20260615-libipa-algorithms-v1-0-e949c937422e@ideasonboard.com>","In-Reply-To":"<20260615-libipa-algorithms-v1-0-e949c937422e@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Cc":"Jacopo Mondi <jacopo.mondi@ideasonboard.com>, \n\tKieran Bingham <kieran.bingham@ideasonboard.com>","X-Mailer":"b4 0.14.3","X-Developer-Signature":"v=1; a=openpgp-sha256; l=10867;\n\ti=jacopo.mondi@ideasonboard.com; h=from:subject:message-id;\n\tbh=S2nsPBnxePsyFPIFSuEoDC87gIkYk7a6ISUmf8FnF9k=;\n\tb=owEBbQKS/ZANAwAKAXI0Bo8WoVY8AcsmYgBqMAa5Lr+g4EWLCIb3m2C2a36m0qWz0wiWvV4oW\n\tj3UZweGwryJAjMEAAEKAB0WIQS1xD1IgJogio9YOMByNAaPFqFWPAUCajAGuQAKCRByNAaPFqFW\n\tPDT/D/0amVQw3/IG6Il0UnLzzsQRH+Qp+Ut8cvrD+OPBdFcX5NgxxtauqRq//ZvtHeLOPGruzSL\n\tLWyhP10I70cwr+UkFJJ/gIHhe97chEBnznhQWiTxfBO1YQQHH5HMk06kM25Te+myoAcNMTgOIYO\n\tWKd9yiTHIIZXtQ30V3bD2qhD/JAcmumIaxVfg/2cBN80iX/nmnXfVsEMiLyERLEH7MQHmrr5AnH\n\tCYzTFLcX6lA5HOjMqN8rHYS86qkOltNG3di1XEtmonQpSNP6/XsdLzkJK5MWHhJzDWELkS85E1V\n\tLKpmn4fzo1Gjh6z6Cj0LlMU04DYZzrDk4DHJcb3MW06K/3i2HoBeWU23e5DeLMscQu2fg4V6s9o\n\tVnXgeprHi3nTgeL6xToW8VsJ1kQKhfoxi6sbsJS8XL6qgAhG1V+ceu8jAydAyA09wPT7W34Y7Li\n\tQfWyQf2v1XBl3j84kWZZyhv9fUSxuK7wCGG/22iROrE/CRKZV8MOHq5ukBSiLhBhODOfTzj6YYo\n\txv+P3SXskdW+N8Ou2ycQ91YCeJvmmxtvScDpPzL9ppiXJ6wKjo/dJisNGFXd1yS/hbPNRru3tOz\n\tamjK65zBNGVwiza/Z5VvDFNr8HpGWYLW4b5428PsyAvmOs4QDAvKmOHUd5fJnX3g1XaZg8Az9fz\n\t2VEQMlLyjSJ8Qkg==","X-Developer-Key":"i=jacopo.mondi@ideasonboard.com; a=openpgp;\n\tfpr=72392EDC88144A65C701EA9BA5826A2587AD026B","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":"From: Kieran Bingham <kieran.bingham@ideasonboard.com>\n\nPort the SoftISP Awb algorithm to use the new libipa implementation\nof AwbAlgorithm.\n\nThe awbAlgo_ class member is initialized with the Q<2, 8> type even if\nthere is no physical register representation for SoftISP.\n\nSigned-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\nSigned-off-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>\n---\n .../internal/software_isp/debayer_params.h         |   2 +-\n src/ipa/simple/algorithms/awb.cpp                  | 112 ++++++++++++++-------\n src/ipa/simple/algorithms/awb.h                    |  29 ++++++\n src/ipa/simple/algorithms/ccm.cpp                  |   2 +-\n src/ipa/simple/ipa_context.h                       |  14 +--\n src/libcamera/software_isp/debayer_cpu.cpp         |   2 +-\n 6 files changed, 117 insertions(+), 44 deletions(-)","diff":"diff --git a/include/libcamera/internal/software_isp/debayer_params.h b/include/libcamera/internal/software_isp/debayer_params.h\nindex 6772b43bced4..b46bbd7a8273 100644\n--- a/include/libcamera/internal/software_isp/debayer_params.h\n+++ b/include/libcamera/internal/software_isp/debayer_params.h\n@@ -24,7 +24,7 @@ struct DebayerParams {\n \tRGB<float> blackLevel = RGB<float>({ 0.0, 0.0, 0.0 });\n \tfloat gamma = 1.0;\n \tfloat contrastExp = 1.0;\n-\tRGB<float> gains = RGB<float>({ 1.0, 1.0, 1.0 });\n+\tRGB<double> gains = RGB<double>({ 1.0, 1.0, 1.0 });\n };\n \n } /* namespace libcamera */\ndiff --git a/src/ipa/simple/algorithms/awb.cpp b/src/ipa/simple/algorithms/awb.cpp\nindex 05155c83d172..01323d9779c5 100644\n--- a/src/ipa/simple/algorithms/awb.cpp\n+++ b/src/ipa/simple/algorithms/awb.cpp\n@@ -15,7 +15,6 @@\n #include <libcamera/control_ids.h>\n \n #include \"libipa/colours.h\"\n-#include \"simple/ipa_context.h\"\n \n namespace libcamera {\n \n@@ -23,41 +22,79 @@ LOG_DEFINE_CATEGORY(IPASoftAwb)\n \n namespace ipa::soft::algorithms {\n \n+/*\n+ * \\todo Replace it with a proper Lux algorithm\n+ */\n+static constexpr unsigned int kDefaultLux = 500;\n+\n+class SimpleAwbStats final : public AwbStats\n+{\n+public:\n+\tSimpleAwbStats() {}\n+\tSimpleAwbStats(const RGB<double> &rgbMeans)\n+\t\t: AwbStats(rgbMeans)\n+\t{\n+\t}\n+\n+\t/* Minimum mean value below which AWB can't operate. */\n+\tdouble minColourValue() const override\n+\t{\n+\t\treturn 0.2;\n+\t}\n+};\n+\n+/**\n+ * \\copydoc libcamera::ipa::Algorithm::init\n+ */\n+int Awb::init(IPAContext &context, const ValueNode &tuningData)\n+{\n+\treturn awbAlgo_.init(tuningData, context.ctrlMap);\n+}\n+\n+/**\n+ * \\copydoc libcamera::ipa::Algorithm::configure\n+ */\n int Awb::configure(IPAContext &context,\n \t\t   [[maybe_unused]] const IPAConfigInfo &configInfo)\n {\n-\tauto &gains = context.activeState.awb.gains;\n-\tgains = { { 1.0, 1.0, 1.0 } };\n+\treturn awbAlgo_.configure(context.activeState.awb,\n+\t\t\t\t  context.configuration.awb);\n+}\n \n-\treturn 0;\n+/**\n+ * \\copydoc libcamera::ipa::Algorithm::queueRequest\n+ */\n+void Awb::queueRequest(IPAContext &context,\n+\t\t       const uint32_t frame,\n+\t\t       IPAFrameContext &frameContext,\n+\t\t       const ControlList &controls)\n+{\n+\tawbAlgo_.queueRequest(context.activeState.awb, frame, frameContext.awb,\n+\t\t\t      controls);\n }\n \n+/**\n+ * \\copydoc libcamera::ipa::Algorithm::prepare\n+ */\n void Awb::prepare(IPAContext &context,\n \t\t  [[maybe_unused]] const uint32_t frame,\n \t\t  IPAFrameContext &frameContext,\n \t\t  DebayerParams *params)\n {\n-\tauto &gains = context.activeState.awb.gains;\n+\tawbAlgo_.prepare(context.activeState.awb, frameContext.awb);\n \n-\tframeContext.gains = gains;\n-\tparams->gains = gains;\n+\tparams->gains = frameContext.awb.gains;\n }\n \n-void Awb::process(IPAContext &context,\n-\t\t  [[maybe_unused]] const uint32_t frame,\n-\t\t  IPAFrameContext &frameContext,\n-\t\t  const SwIspStats *stats,\n-\t\t  ControlList &metadata)\n+SimpleAwbStats Awb::calculateRgbMeans(IPAContext &context,\n+\t\t\t\t      const SwIspStats *stats) const\n {\n+\tif (!stats->valid)\n+\t\treturn {};\n+\n \tconst SwIspStats::Histogram &histogram = stats->yHistogram;\n \tconst uint8_t blackLevel = context.activeState.blc.level;\n \n-\tmetadata.set(controls::ColourGains, { frameContext.gains.r(),\n-\t\t\t\t\t      frameContext.gains.b() });\n-\n-\tif (!stats->valid)\n-\t\treturn;\n-\n \t/*\n \t * Black level must be subtracted to get the correct AWB ratios, they\n \t * would be off if they were computed from the whole brightness range\n@@ -67,30 +104,37 @@ void Awb::process(IPAContext &context,\n \t\thistogram.begin(), histogram.end(), uint64_t(0));\n \tconst uint64_t offset = blackLevel * nPixels;\n \tconst uint64_t minValid = 1;\n+\n \t/*\n \t * Make sure the sums are at least minValid, while preventing unsigned\n \t * integer underflow.\n \t */\n \tconst RGB<uint64_t> sum = stats->sum_.max(offset + minValid) - offset;\n \n+\tRGB<double> rgbMeans = { { static_cast<double>(sum.r() / nPixels),\n+\t\t\t\t   static_cast<double>(sum.g() / nPixels),\n+\t\t\t\t   static_cast<double>(sum.b() / nPixels) } };\n+\n \t/*\n-\t * Calculate red and blue gains for AWB.\n-\t * Clamp max gain at 4.0, this also avoids 0 division.\n+\t * \\todo Determine the minimum allowed thresholds from the mean\n+\t * but we currently have the sum - not the mean value!\n+\t *\n+\t * Currently set to SimpleAwbStats::minColourValue() = 0.2.\n \t */\n-\tauto &gains = context.activeState.awb.gains;\n-\tgains = { {\n-\t\tsum.r() <= sum.g() / 4 ? 4.0f : static_cast<float>(sum.g()) / sum.r(),\n-\t\t1.0,\n-\t\tsum.b() <= sum.g() / 4 ? 4.0f : static_cast<float>(sum.g()) / sum.b(),\n-\t} };\n-\n-\tRGB<double> rgbGains{ { 1 / gains.r(), 1 / gains.g(), 1 / gains.b() } };\n-\tcontext.activeState.awb.temperatureK = estimateCCT(rgbGains);\n-\tmetadata.set(controls::ColourTemperature, context.activeState.awb.temperatureK);\n-\n-\tLOG(IPASoftAwb, Debug)\n-\t\t<< \"gain R/B: \" << gains << \"; temperature: \"\n-\t\t<< context.activeState.awb.temperatureK;\n+\treturn SimpleAwbStats(rgbMeans);\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 SwIspStats *stats,\n+\t\t  ControlList &metadata)\n+{\n+\tSimpleAwbStats awbStats = calculateRgbMeans(context, stats);\n+\n+\tawbAlgo_.process(context.activeState.awb, frameContext.awb, awbStats,\n+\t\t\t kDefaultLux, metadata);\n }\n \n REGISTER_IPA_ALGORITHM(Awb, \"Awb\")\ndiff --git a/src/ipa/simple/algorithms/awb.h b/src/ipa/simple/algorithms/awb.h\nindex ad993f39c180..cb36cd092e51 100644\n--- a/src/ipa/simple/algorithms/awb.h\n+++ b/src/ipa/simple/algorithms/awb.h\n@@ -7,19 +7,37 @@\n \n #pragma once\n \n+#include <libcamera/controls.h>\n+\n+#include \"libcamera/internal/software_isp/debayer_params.h\"\n+#include \"libcamera/internal/value_node.h\"\n+\n+#include \"libipa/awb.h\"\n+#include \"libipa/fixedpoint.h\"\n+#include \"simple/ipa_context.h\"\n+\n #include \"algorithm.h\"\n \n namespace libcamera {\n \n namespace ipa::soft::algorithms {\n \n+class SimpleAwbStats;\n+\n class Awb : public Algorithm\n {\n public:\n \tAwb() = default;\n \t~Awb() = default;\n \n+\tint init(IPAContext &context,\n+\t\t const ValueNode &tuningData) override;\n \tint configure(IPAContext &context, const IPAConfigInfo &configInfo) override;\n+\n+\tvoid queueRequest(IPAContext &context,\n+\t\t\t  [[maybe_unused]] const uint32_t frame,\n+\t\t\t  IPAFrameContext &frameContext,\n+\t\t\t  const ControlList &controls) override;\n \tvoid prepare(IPAContext &context,\n \t\t     const uint32_t frame,\n \t\t     IPAFrameContext &frameContext,\n@@ -29,6 +47,17 @@ public:\n \t\t     IPAFrameContext &frameContext,\n \t\t     const SwIspStats *stats,\n \t\t     ControlList &metadata) override;\n+\n+private:\n+\tSimpleAwbStats calculateRgbMeans(IPAContext &context,\n+\t\t\t\t\t const SwIspStats *stats) const;\n+\n+\t/*\n+\t * There actually is no Q register format for SoftISP, but allow the\n+\t * colour gains to range in the [0.0f, 15.999f] interval, which seems\n+\t * reasonable.\n+\t */\n+\tAwbAlgorithm<UQ<4, 8>> awbAlgo_;\n };\n \n } /* namespace ipa::soft::algorithms */\ndiff --git a/src/ipa/simple/algorithms/ccm.cpp b/src/ipa/simple/algorithms/ccm.cpp\nindex ace9c35dc462..ff37c718c6e4 100644\n--- a/src/ipa/simple/algorithms/ccm.cpp\n+++ b/src/ipa/simple/algorithms/ccm.cpp\n@@ -44,7 +44,7 @@ int Ccm::init([[maybe_unused]] IPAContext &context, const ValueNode &tuningData)\n void Ccm::prepare(IPAContext &context, [[maybe_unused]] const uint32_t frame,\n \t\t  IPAFrameContext &frameContext, [[maybe_unused]] DebayerParams *params)\n {\n-\tconst unsigned int ct = context.activeState.awb.temperatureK;\n+\tconst unsigned int ct = frameContext.awb.temperatureK;\n \n \t/* Change CCM only on bigger temperature changes. */\n \tif (!currentCcm_ ||\ndiff --git a/src/ipa/simple/ipa_context.h b/src/ipa/simple/ipa_context.h\nindex 8ccfacb46a59..0646f42d5618 100644\n--- a/src/ipa/simple/ipa_context.h\n+++ b/src/ipa/simple/ipa_context.h\n@@ -16,6 +16,7 @@\n #include \"libcamera/internal/matrix.h\"\n #include \"libcamera/internal/vector.h\"\n \n+#include <libipa/awb.h>\n #include <libipa/fc_queue.h>\n \n #include \"core_ipa_interface.h\"\n@@ -25,6 +26,8 @@ namespace libcamera {\n namespace ipa::soft {\n \n struct IPASessionConfiguration {\n+\tipa::awb::Session awb;\n+\n \tstruct {\n \t\tint32_t exposureMin, exposureMax;\n \t\tdouble againMin, againMax, again10, againMinStep;\n@@ -36,6 +39,8 @@ struct IPASessionConfiguration {\n };\n \n struct IPAActiveState {\n+\tipa::awb::ActiveState awb;\n+\n \tstruct {\n \t\tint32_t exposure;\n \t\tdouble again;\n@@ -48,11 +53,6 @@ struct IPAActiveState {\n \t\tdouble lastGain;\n \t} blc;\n \n-\tstruct {\n-\t\tRGB<float> gains;\n-\t\tunsigned int temperatureK;\n-\t} awb;\n-\n \tMatrix<float, 3, 3> combinedMatrix;\n \n \tstruct {\n@@ -64,6 +64,8 @@ struct IPAActiveState {\n };\n \n struct IPAFrameContext : public FrameContext {\n+\tipa::awb::FrameContext awb;\n+\n \tMatrix<float, 3, 3> ccm;\n \n \tstruct {\n@@ -71,8 +73,6 @@ struct IPAFrameContext : public FrameContext {\n \t\tdouble gain;\n \t} sensor;\n \n-\tRGB<float> gains;\n-\n \tfloat gamma;\n \tstd::optional<float> contrast;\n \tstd::optional<float> saturation;\ndiff --git a/src/libcamera/software_isp/debayer_cpu.cpp b/src/libcamera/software_isp/debayer_cpu.cpp\nindex d2596d32bbcd..fc171f874833 100644\n--- a/src/libcamera/software_isp/debayer_cpu.cpp\n+++ b/src/libcamera/software_isp/debayer_cpu.cpp\n@@ -1051,7 +1051,7 @@ void DebayerCpu::updateLookupTables(const DebayerParams &params)\n \t\t\tauto &blue = swapRedBlueGains_ ? red_ : blue_;\n \t\t\tfor (unsigned int i = 0; i < kRGBLookupSize; i++) {\n \t\t\t\t/* Apply gamma after gain! */\n-\t\t\t\tconst RGB<float> lutGains = (gains * i / div).min(gammaTableSize - 1);\n+\t\t\t\tconst RGB<double> lutGains = (gains * i / div).min(gammaTableSize - 1);\n \t\t\t\tred[i] = gammaTable_[static_cast<unsigned int>(lutGains.r())];\n \t\t\t\tgreen[i] = gammaTable_[static_cast<unsigned int>(lutGains.g())];\n \t\t\t\tblue[i] = gammaTable_[static_cast<unsigned int>(lutGains.b())];\n","prefixes":["04/11"]}