From patchwork Wed May 6 23:07:14 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: devve X-Patchwork-Id: 26659 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 D5CCCC32FE for ; Wed, 6 May 2026 23:07:31 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id F0DCC63040; Thu, 7 May 2026 01:07:30 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="mW8fESsE"; dkim-atps=neutral Received: from mail-qk1-x72f.google.com (mail-qk1-x72f.google.com [IPv6:2607:f8b0:4864:20::72f]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 1ED076303C for ; Thu, 7 May 2026 01:07:29 +0200 (CEST) Received: by mail-qk1-x72f.google.com with SMTP id af79cd13be357-8ec9f099fc6so28039985a.0 for ; Wed, 06 May 2026 16:07:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1778108848; x=1778713648; darn=lists.libcamera.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=vpT2omGvrrBC/UWz9mRqAkO01quAcHHbvl8N34ZIhJU=; b=mW8fESsE77c75Pp5SGeBgfUJam1cwdkUVOpyts8V2/qpnLAIZkti5BerdlzDgYJC8p /pGnBtml4Cdr1fYcQVHX2giRRJaZ9sZLcm+xKgQkBwyJ56QMNuTRTropBbkZ4MEMq7hK 8kxbF1Wwl65esQKw/T2/+RcHbt5+CuXCZarOy3oHSD+ZgJwXoyXy+cZ8Y2zN5DYPKakn wiEEL6xW6ZSUI2mjU2qIG1zPd8cZgXe28eDbwfLWsmx4OffvrgNavipZq6pkfqgdq4WM XDq0QQU7Arq/I5BB6l+1nUH6IVYR5SU+DO2e/S52RnnNxkNCJjEBeDBOp2ngjzPh4o9X X6Gw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778108848; x=1778713648; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=vpT2omGvrrBC/UWz9mRqAkO01quAcHHbvl8N34ZIhJU=; b=mmCN4s/Zv/jiXNg7Dh6bmulDVN1lW8BEnxHZ4l8EOrAh/A9bY3P41j5snKQb/EiMRE Ox60yPNLtZ0guyFVFbdsj6kX4OWXFtWIT8wlAgCXoTdiO6TqpuNf0K8cB0Q+5NEIuvaT 58In7D4R6X/JRUGy4wznSy+prcXw5rjQEo71Cie7K34JI3ktvBXu3iq4z2FpBcQifiUZ Zj+rP13+Npuzp9FNiBlmaSs/KEqQSYv3lPEihoueOihzI4LbRRHBqEzz3ZW+SD0CVcUy e1eO+tBvD1Hib4WzBWTKY7EQ+L2a4L+GG2xNHQ+4fI0u+1mq5UlCwhxy9v+rzjUVaFtD etHQ== X-Gm-Message-State: AOJu0YwYZXJjr7+rcyoplNG+3zbZshpYM1MNwnjKZHkCDNjGkTqrqJ0J iWl2oECIjWjYIwBiz2LCviJ5dk/K7c98dsmNC5j0SOXVxqmVZDieVL/c4xVsGA== X-Gm-Gg: AeBDietHxba0+bJaP1zRS9rgLd/eJqijknt3V8iUvcFRv03Mt4rGsOGyToZsNamN2v0 F/h6aNt9SRPGYiQY9El1afriHiGwc73n7ShvvDd9akckklK3Rl63xNOTrEhrVF0uEF/C6t2jJw7 eYkignrfY1oEI40AkimFfwOO3tV7odT0Y7sz9PWLN0XjUnxSlT9ngwZDsG0DuGnL00omqHn1FhH lQHe/48ep+w82q08rIv76Sa/aITO6erwcP7P+1caPFX3sIEdLI9gom8Myqmc/uO8CIxEpDM+pmP kx50yZODT3UC5F/ZJYd6N4BLCvmecGoEdYnQWIcDLxvZOyBFR5/u/oDDkGpixvt0N5Nw11TRJD7 o9b5ZgZsBWFAcbtJzosvVARCwOWDzym7gVzaJJf9ijOiXBDZKQPXQLgAFc29UDadUrbAwm+FHEJ b/6xKcQlvqQXuklwmhBA/hMypPxpLjohp8T8HuUFfAPULia2PNg0QL3IgPrQ5mPKKA2ex4ySKj3 5R1nB2aPCxbCyUjaooI+CeSKkRsGEog X-Received: by 2002:a05:620a:4591:b0:8eb:d38d:2013 with SMTP id af79cd13be357-904d496d4b6mr849920385a.17.1778108847994; Wed, 06 May 2026 16:07:27 -0700 (PDT) Received: from dexps.speedport.ip (p200300eda74453cf3cf3f7929e513b94.dip0.t-ipconnect.de. [2003:ed:a744:53cf:3cf3:f792:9e51:3b94]) by smtp.gmail.com with ESMTPSA id af79cd13be357-8fc2c25324esm2035266385a.23.2026.05.06.16.07.27 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 May 2026 16:07:27 -0700 (PDT) From: d3vv3 To: libcamera-devel@lists.libcamera.org Subject: [PATCH v2 02/10] ipa: simple: awb: Add temporal smoothing and per-channel gain limits Date: Thu, 7 May 2026 01:07:14 +0200 Message-ID: <20260506230722.1041596-3-devve.3@gmail.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260506230722.1041596-1-devve.3@gmail.com> References: <20260506230722.1041596-1-devve.3@gmail.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" 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..937aabc8 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, Debug) + << "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 */