From patchwork Fri May 1 19:13:09 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: d3vv3 X-Patchwork-Id: 26592 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 63AF9BDCB5 for ; Fri, 1 May 2026 19:55:27 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 3863063025; Fri, 1 May 2026 21:55:26 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="GC2oKlUA"; dkim-atps=neutral Received: from mail-wm1-x332.google.com (mail-wm1-x332.google.com [IPv6:2a00:1450:4864:20::332]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 579B162FB1 for ; Fri, 1 May 2026 21:14:21 +0200 (CEST) Received: by mail-wm1-x332.google.com with SMTP id 5b1f17b1804b1-48984d29fe3so32435505e9.0 for ; Fri, 01 May 2026 12:14:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1777662861; x=1778267661; darn=lists.libcamera.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=rzGFobzzw+DDdH/ZpLcVh6y/sbzTr3KV2kFj73Dff/E=; b=GC2oKlUArFjGWZxqpQeuq52Dh1Mf4GKeNeCI+TJdY8QBtepPgHZZEf8NXNxDG5IaVn W7GtFhuVc7HmR1gwloMX+uxi9z1x0QF08ZUK2Vx+c2PvQV0HhtrsJJf3EzsGuOLAZTXF lv1FRjgyprdxSyyZ3IZ2EqwA0ee4fBGsACtq3xF3f5dMK0YFNNfsD4oD7TdD8M1PxijS 0mMceNoavD6tyZAhjGd+a3mZc2rn75lzflZiNvY3h4NLuBmJ2kFKSIYc+m9F/9jryTRw 0Lz5KsuXBOi/a/F23kL7q7IdSXaK7jVgElsECr4nDRYFZXz0zg8fPvVd+3yq05H65eoL 8ceg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777662861; x=1778267661; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=rzGFobzzw+DDdH/ZpLcVh6y/sbzTr3KV2kFj73Dff/E=; b=YYyb3SllZuEpMJM1Z0QpyNhdX7bVw3rjIjDLRVa6L0U2ymO42z+iHBf8ZUPbL4YOVN 4K1B3ki99s7icCkDkfUeuOevKliDZHF47rP+3ZG6pCKBPdLywFkkdY9zC5IUYfV9N4Ai yCM5NB1rhj8oO7EI+oFiweadgTyDyJT95rhKtNzND+8Wsg5DHKcAjVDsZ4m9rA/hLv2W B0qwxn8nUF0r6hDXvY5k9gicZ0lOeNODl3T8qQTUhNDBKgV20aL/BDo3iVebHbGahLt2 fOTvY7ZUqasc5j7beTLD52UZi9bWBhq9/SuRbkqBFrKQeKTZ/xiFgp7hphX5FBq84euj bxig== X-Gm-Message-State: AOJu0Yy+gPH96f17W4WxfwFIGkAG87Urtj4rsXn1RHph3im+xWuAg/Lc IHHGDURbLrwLuIwmTbCpX/kz5lpPxeIFP9Al7W0PetAA1b2DwR82Hq6kc892zA== X-Gm-Gg: AeBDieuxhq1AcmFZwPw+0o7u8GnYbdyGln5vmceV0kFSoAL/dSaAfyUK3atEHY6QMgW nJuiR6yc2hC6EcxLTg7lYtntdZSr6PMbqPjWAjPsbniDOkVJAVrjljKS86W0yW8MB82uZsWW0nh uzol2E90E4rImnQubKweUCGB5DC3DCtANjtNX7zwK9CePWsSrBfFnt/ED4zmNH37wC1OaitQwzT rONOgq2DmysriE4zLDYMSJ8viRQ2pZBdWAxxqGJ3+FsyI/Hy5B9tBN0D5WM9owm/STfN0of8FLu 1fkFoiGKE57s6O3tpWRp8qZpU6RqapFaqQEzNOxEAhtOrdmDfcvMxsRwCiNpWtvEdXaf7PJtjCt 9yx8N3FFDqs7NCjw8IK6Yhq1s1a9VXYGuos4SQ6QMpEfOP5Oy5LRRpPoBNj2bJX2W6Dd2l/B426 0AVTpXhXVJGSsbkOsGq5wUh9sOwIEN6B2l92yvyBmDDJLmGe6yVO9SnLP8Ke29u2h5OWxrfzOIe sMZuynplxJSAECqaSMOFvCajg1pOhGx X-Received: by 2002:a05:600c:a404:b0:488:9fb7:376d with SMTP id 5b1f17b1804b1-48a9867710cmr5101955e9.28.1777662860569; Fri, 01 May 2026 12:14:20 -0700 (PDT) Received: from dexps.speedport.ip (p200300eda74453f7a35e44f7d2f0427a.dip0.t-ipconnect.de. [2003:ed:a744:53f7:a35e:44f7:d2f0:427a]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-48a8eb69698sm139146305e9.1.2026.05.01.12.14.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 May 2026 12:14:20 -0700 (PDT) From: d3vv3 To: libcamera-devel@lists.libcamera.org Cc: d3vv3 Subject: [PATCH] ipa: simple: awb: Add temporal smoothing and per-channel gain limits Date: Fri, 1 May 2026 21:13:09 +0200 Message-ID: <20260501191400.985920-1-devve.3@gmail.com> X-Mailer: git-send-email 2.54.0 MIME-Version: 1.0 X-Mailman-Approved-At: Fri, 01 May 2026 21:55:24 +0200 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" Add configurable YAML parameters maxGainR, maxGainB, and speed to AWB. Replace the single hardcoded max gain (4.0x) with per-channel limits, and apply exponential moving average smoothing to reduce colour temperature oscillation between frames. Signed-off-by: d3vv3 --- src/ipa/simple/algorithms/awb.cpp | 34 ++++++++++++++++++++++++++----- src/ipa/simple/algorithms/awb.h | 6 ++++++ 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/src/ipa/simple/algorithms/awb.cpp b/src/ipa/simple/algorithms/awb.cpp index f5c88ea6..0f9964a4 100644 --- a/src/ipa/simple/algorithms/awb.cpp +++ b/src/ipa/simple/algorithms/awb.cpp @@ -14,6 +14,8 @@ #include +#include "libcamera/internal/yaml_parser.h" + #include "libipa/colours.h" #include "simple/ipa_context.h" @@ -23,6 +25,21 @@ LOG_DEFINE_CATEGORY(IPASoftAwb) namespace ipa::soft::algorithms { +int Awb::init([[maybe_unused]] IPAContext &context, + const ValueNode &tuningData) +{ + maxGainR_ = tuningData["maxGainR"].get().value_or(4.0f); + maxGainB_ = tuningData["maxGainB"].get().value_or(4.0f); + speed_ = tuningData["speed"].get().value_or(1.0f); + + LOG(IPASoftAwb, Info) + << "AWB: maxGainR " << maxGainR_ + << ", maxGainB " << maxGainB_ + << ", speed " << speed_; + + return 0; +} + int Awb::configure(IPAContext &context, [[maybe_unused]] const IPAConfigInfo &configInfo) { @@ -84,14 +101,21 @@ void Awb::process(IPAContext &context, const RGB sum = stats->sum_.max(offset + minValid) - offset; /* - * Calculate red and blue gains for AWB. - * Clamp max gain at 4.0, this also avoids 0 division. + * Calculate red and blue gains for AWB. Clamp max gain to avoid + * division by zero and extreme color casts. */ auto &gains = context.activeState.awb.gains; + float rawRGain = sum.r() <= sum.g() / maxGainR_ ? maxGainR_ : + static_cast(sum.g()) / sum.r(); + float rawBGain = sum.b() <= sum.g() / maxGainB_ ? maxGainB_ : + static_cast(sum.g()) / sum.b(); + + /* Apply temporal smoothing to avoid rapid white balance changes. */ + float alpha = std::clamp(speed_, 0.01f, 1.0f); gains = { { - sum.r() <= sum.g() / 4 ? 4.0f : static_cast(sum.g()) / sum.r(), - 1.0, - sum.b() <= sum.g() / 4 ? 4.0f : static_cast(sum.g()) / sum.b(), + gains.r() * (1.0f - alpha) + rawRGain * alpha, + 1.0f, + gains.b() * (1.0f - alpha) + rawBGain * alpha, } }; RGB rgbGains{ { 1 / gains.r(), 1 / gains.g(), 1 / gains.b() } }; diff --git a/src/ipa/simple/algorithms/awb.h b/src/ipa/simple/algorithms/awb.h index ad993f39..0aedc1d1 100644 --- a/src/ipa/simple/algorithms/awb.h +++ b/src/ipa/simple/algorithms/awb.h @@ -19,6 +19,7 @@ public: Awb() = default; ~Awb() = default; + int init(IPAContext &context, const ValueNode &tuningData) override; int configure(IPAContext &context, const IPAConfigInfo &configInfo) override; void prepare(IPAContext &context, const uint32_t frame, @@ -29,6 +30,11 @@ public: IPAFrameContext &frameContext, const SwIspStats *stats, ControlList &metadata) override; + +private: + float maxGainR_; + float maxGainB_; + float speed_; }; } /* namespace ipa::soft::algorithms */