[{"id":38850,"web_url":"https://patchwork.libcamera.org/comment/38850/","msgid":"<85pl32thxv.fsf@mzamazal-thinkpadp1gen7.tpbc.csb>","date":"2026-05-11T09:27:08","subject":"Re: [PATCH v2 02/10] ipa: simple: awb: Add temporal smoothing and\n\tper-channel gain limits","submitter":{"id":177,"url":"https://patchwork.libcamera.org/api/people/177/","name":"Milan Zamazal","email":"mzamazal@redhat.com"},"content":"d3vv3 <devve.3@gmail.com> writes:\n\n> Add configurable YAML parameters maxGainR, maxGainB, and speed to AWB.\n> Replace the single hardcoded max gain (4.0x) with per-channel limits,\n> and apply exponential moving average smoothing to reduce colour\n> temperature oscillation between frames.\n>\n> Signed-off-by: d3vv3 <devve.3@gmail.com>\n\nReviewed-by: Milan Zamazal <mzamazal@redhat.com>\n\n> ---\n>  src/ipa/simple/algorithms/awb.cpp | 34 ++++++++++++++++++++++++++-----\n>  src/ipa/simple/algorithms/awb.h   |  6 ++++++\n>  2 files changed, 35 insertions(+), 5 deletions(-)\n>\n> diff --git a/src/ipa/simple/algorithms/awb.cpp b/src/ipa/simple/algorithms/awb.cpp\n> index f5c88ea6..937aabc8 100644\n> --- a/src/ipa/simple/algorithms/awb.cpp\n> +++ b/src/ipa/simple/algorithms/awb.cpp\n> @@ -14,6 +14,8 @@\n>  \n>  #include <libcamera/control_ids.h>\n>  \n> +#include \"libcamera/internal/yaml_parser.h\"\n> +\n>  #include \"libipa/colours.h\"\n>  #include \"simple/ipa_context.h\"\n>  \n> @@ -23,6 +25,21 @@ LOG_DEFINE_CATEGORY(IPASoftAwb)\n>  \n>  namespace ipa::soft::algorithms {\n>  \n> +int Awb::init([[maybe_unused]] IPAContext &context,\n> +\t      const ValueNode &tuningData)\n> +{\n> +\tmaxGainR_ = tuningData[\"maxGainR\"].get<float>().value_or(4.0f);\n> +\tmaxGainB_ = tuningData[\"maxGainB\"].get<float>().value_or(4.0f);\n> +\tspeed_ = tuningData[\"speed\"].get<float>().value_or(1.0f);\n> +\n> +\tLOG(IPASoftAwb, Debug)\n> +\t\t<< \"AWB: maxGainR \" << maxGainR_\n> +\t\t<< \", maxGainB \" << maxGainB_\n> +\t\t<< \", speed \" << speed_;\n> +\n> +\treturn 0;\n> +}\n> +\n>  int Awb::configure(IPAContext &context,\n>  \t\t   [[maybe_unused]] const IPAConfigInfo &configInfo)\n>  {\n> @@ -84,14 +101,21 @@ void Awb::process(IPAContext &context,\n>  \tconst RGB<uint64_t> sum = stats->sum_.max(offset + minValid) - offset;\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 * Calculate red and blue gains for AWB. Clamp max gain to avoid\n> +\t * division by zero and extreme color casts.\n>  \t */\n>  \tauto &gains = context.activeState.awb.gains;\n> +\tfloat rawRGain = sum.r() <= sum.g() / maxGainR_ ? maxGainR_ :\n> +\t\t\t\tstatic_cast<float>(sum.g()) / sum.r();\n> +\tfloat rawBGain = sum.b() <= sum.g() / maxGainB_ ? maxGainB_ :\n> +\t\t\t\tstatic_cast<float>(sum.g()) / sum.b();\n> +\n> +\t/* Apply temporal smoothing to avoid rapid white balance changes. */\n> +\tfloat alpha = std::clamp(speed_, 0.01f, 1.0f);\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\tgains.r() * (1.0f - alpha) + rawRGain * alpha,\n> +\t\t1.0f,\n> +\t\tgains.b() * (1.0f - alpha) + rawBGain * alpha,\n>  \t} };\n>  \n>  \tRGB<double> rgbGains{ { 1 / gains.r(), 1 / gains.g(), 1 / gains.b() } };\n> diff --git a/src/ipa/simple/algorithms/awb.h b/src/ipa/simple/algorithms/awb.h\n> index ad993f39..0aedc1d1 100644\n> --- a/src/ipa/simple/algorithms/awb.h\n> +++ b/src/ipa/simple/algorithms/awb.h\n> @@ -19,6 +19,7 @@ public:\n>  \tAwb() = default;\n>  \t~Awb() = default;\n>  \n> +\tint init(IPAContext &context, const ValueNode &tuningData) override;\n>  \tint configure(IPAContext &context, const IPAConfigInfo &configInfo) override;\n>  \tvoid prepare(IPAContext &context,\n>  \t\t     const uint32_t frame,\n> @@ -29,6 +30,11 @@ public:\n>  \t\t     IPAFrameContext &frameContext,\n>  \t\t     const SwIspStats *stats,\n>  \t\t     ControlList &metadata) override;\n> +\n> +private:\n> +\tfloat maxGainR_;\n> +\tfloat maxGainB_;\n> +\tfloat speed_;\n>  };\n>  \n>  } /* namespace ipa::soft::algorithms */","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 BF08BBDCBD\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon, 11 May 2026 09:27:16 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 506E16301E;\n\tMon, 11 May 2026 11:27:15 +0200 (CEST)","from us-smtp-delivery-124.mimecast.com\n\t(us-smtp-delivery-124.mimecast.com [170.10.133.124])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 4811E62010\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 11 May 2026 11:27:14 +0200 (CEST)","from mail-wr1-f70.google.com (mail-wr1-f70.google.com\n\t[209.85.221.70]) by relay.mimecast.com with ESMTP with STARTTLS\n\t(version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id\n\tus-mta-582-JjiG7OfoNpiUY3xuMdDi4w-1; Mon, 11 May 2026 05:27:11 -0400","by mail-wr1-f70.google.com with SMTP id\n\tffacd0b85a97d-44f56d5523eso3469334f8f.1\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 11 May 2026 02:27:11 -0700 (PDT)","from mzamazal-thinkpadp1gen7.tpbc.csb\n\t(ip-77-48-47-4.net.vodafone.cz. [77.48.47.4])\n\tby smtp.gmail.com with ESMTPSA id\n\tffacd0b85a97d-4548ec6aea4sm28350653f8f.10.2026.05.11.02.27.08\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tMon, 11 May 2026 02:27:09 -0700 (PDT)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=redhat.com header.i=@redhat.com\n\theader.b=\"PYb5BLd+\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com;\n\ts=mimecast20190719; t=1778491633;\n\th=from:from:reply-to:subject:subject:date:date:message-id:message-id:\n\tto:to:cc:cc:mime-version:mime-version:content-type:content-type:\n\tin-reply-to:in-reply-to:references:references;\n\tbh=qiTjK5PIRyl1m+vCBA2f1vG4TagcILTS9vXtdmacQ00=;\n\tb=PYb5BLd+q9KkDC310JY4qVya2IgQiUndGtGneyZcd/E5m3lgU6ozvXP9bV0kY3N3xiioyI\n\tD5lJECUa2hq60KuLKaCW1UUHSDf9dVTfDcqkLldIRHAEPO6v6kc6nJrzoRMfhps8+S4TaY\n\tFUsI3T3gAMoqjybCw5rjd7Bm68O/fZg=","X-MC-Unique":"JjiG7OfoNpiUY3xuMdDi4w-1","X-Mimecast-MFC-AGG-ID":"JjiG7OfoNpiUY3xuMdDi4w_1778491631","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20251104; t=1778491630; x=1779096430;\n\th=mime-version:user-agent:message-id:date:references:in-reply-to\n\t:subject:cc:to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject\n\t:date:message-id:reply-to;\n\tbh=qiTjK5PIRyl1m+vCBA2f1vG4TagcILTS9vXtdmacQ00=;\n\tb=b7uc2tJcI4BZM+7SblkM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bmhFrd03gzAugZYhG0XIob+oG8Gmi0Ej5gn4th\n\tZIvNKKNQv+ig==","X-Received":["by 2002:a05:600c:3e05:b0:488:9ed3:1492 with SMTP id\n\t5b1f17b1804b1-48e51f2a997mr401284955e9.10.1778491630164; \n\tMon, 11 May 2026 02:27:10 -0700 (PDT)","by 2002:a05:600c:3e05:b0:488:9ed3:1492 with SMTP id\n\t5b1f17b1804b1-48e51f2a997mr401284405e9.10.1778491629614; \n\tMon, 11 May 2026 02:27:09 -0700 (PDT)"],"From":"Milan Zamazal <mzamazal@redhat.com>","To":"d3vv3 <devve.3@gmail.com>","Cc":"libcamera-devel@lists.libcamera.org","Subject":"Re: [PATCH v2 02/10] ipa: simple: awb: Add temporal smoothing and\n\tper-channel gain limits","In-Reply-To":"<20260506230722.1041596-3-devve.3@gmail.com> (d3vv3's message of\n\t\"Thu, 7 May 2026 01:07:14 +0200\")","References":"<20260506230722.1041596-1-devve.3@gmail.com>\n\t<20260506230722.1041596-3-devve.3@gmail.com>","Date":"Mon, 11 May 2026 11:27:08 +0200","Message-ID":"<85pl32thxv.fsf@mzamazal-thinkpadp1gen7.tpbc.csb>","User-Agent":"Gnus/5.13 (Gnus v5.13)","MIME-Version":"1.0","X-Mimecast-Spam-Score":"0","X-Mimecast-MFC-PROC-ID":"PNGaYkmBpBc9V8BOj3FsbkM6ensDC3rJ-yal5rTjyN0_1778491631","X-Mimecast-Originator":"redhat.com","Content-Type":"text/plain","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>"}}]