[{"id":37913,"web_url":"https://patchwork.libcamera.org/comment/37913/","msgid":"<176917829300.302817.15331273037194731250@localhost>","date":"2026-01-23T14:24:53","subject":"Re: [PATCH v6 12/16] ipa: mali-c55: Convert AWB to UQ<4, 8> usage","submitter":{"id":184,"url":"https://patchwork.libcamera.org/api/people/184/","name":"Stefan Klug","email":"stefan.klug@ideasonboard.com"},"content":"Hi Kieran,\n\nQuoting Kieran Bingham (2026-01-21 18:37:31)\n> Utilise the new FixedPoint type to explicitly calculate gains for AWB in Q4.8\n> format. This ensures that reporting of gains in metadata reflect the true\n> AWB gains applied.\n> \n> Reviewed-by: Isaac Scott <isaac.scott@ideasonboard.com>\n> Reviewed-by: Barnabás Pőcze <barnabas.pocze@ideasonboard.com>\n> Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n> \n> ---\n> v5:\n> - Use UQ<4, 8> directly\n> - Change debug prints to use operator<<\n> - Now based on top of float types instead of doubles\n> \n> Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n> ---\n>  src/ipa/mali-c55/algorithms/awb.cpp | 36 ++++++++++++++---------------\n>  src/ipa/mali-c55/ipa_context.h      | 10 ++++----\n>  2 files changed, 24 insertions(+), 22 deletions(-)\n> \n> diff --git a/src/ipa/mali-c55/algorithms/awb.cpp b/src/ipa/mali-c55/algorithms/awb.cpp\n> index b179dd7f0c1c..a5150bbf3d97 100644\n> --- a/src/ipa/mali-c55/algorithms/awb.cpp\n> +++ b/src/ipa/mali-c55/algorithms/awb.cpp\n> @@ -37,8 +37,8 @@ int Awb::configure([[maybe_unused]] IPAContext &context,\n>          * for the first frame we will make no assumptions and leave the R/B\n>          * channels unmodified.\n>          */\n> -       context.activeState.awb.rGain = 1.0;\n> -       context.activeState.awb.bGain = 1.0;\n> +       context.activeState.awb.rGain = 1.0f;\n> +       context.activeState.awb.bGain = 1.0f;\n>  \n>         return 0;\n>  }\n> @@ -46,8 +46,8 @@ int Awb::configure([[maybe_unused]] IPAContext &context,\n>  void Awb::fillGainsParamBlock(MaliC55Params *params, IPAContext &context,\n>                                 IPAFrameContext &frameContext)\n>  {\n> -       double rGain = context.activeState.awb.rGain;\n> -       double bGain = context.activeState.awb.bGain;\n> +       UQ<4, 8> rGain = context.activeState.awb.rGain;\n> +       UQ<4, 8> bGain = context.activeState.awb.bGain;\n>  \n>         /*\n>          * The gains here map as follows:\n> @@ -61,10 +61,10 @@ void Awb::fillGainsParamBlock(MaliC55Params *params, IPAContext &context,\n>          */\n>         auto block = params->block<MaliC55Blocks::AwbGains>();\n>  \n> -       block->gain00 = floatingToFixedPoint<4, 8, uint16_t, double>(rGain);\n> -       block->gain01 = floatingToFixedPoint<4, 8, uint16_t, double>(1.0);\n> -       block->gain10 = floatingToFixedPoint<4, 8, uint16_t, double>(1.0);\n> -       block->gain11 = floatingToFixedPoint<4, 8, uint16_t, double>(bGain);\n> +       block->gain00 = rGain.quantized();\n> +       block->gain01 = UQ<4, 8>(1.0f).quantized();\n> +       block->gain10 = UQ<4, 8>(1.0f).quantized();\n> +       block->gain11 = bGain.quantized();\n>  \n>         frameContext.awb.rGain = rGain;\n>         frameContext.awb.bGain = bGain;\n> @@ -140,18 +140,18 @@ void Awb::process(IPAContext &context, const uint32_t frame,\n>          * gain figures that we can apply to approximate a grey world.\n>          */\n>         unsigned int counted_zones = 0;\n> -       double rgSum = 0, bgSum = 0;\n> +       float rgSum = 0, bgSum = 0;\n>  \n>         for (unsigned int i = 0; i < 225; i++) {\n>                 if (!awb_ratios[i].num_pixels)\n>                         continue;\n>  \n>                 /*\n> -                * The statistics are in Q4.8 format, so we convert to double\n> +                * The statistics are in Q4.8 format, so we convert to float\n>                  * here.\n>                  */\n> -               rgSum += fixedToFloatingPoint<4, 8, double, uint16_t>(awb_ratios[i].avg_rg_gr);\n> -               bgSum += fixedToFloatingPoint<4, 8, double, uint16_t>(awb_ratios[i].avg_bg_br);\n> +               rgSum += UQ<4, 8>(awb_ratios[i].avg_rg_gr).value();\n> +               bgSum += UQ<4, 8>(awb_ratios[i].avg_bg_br).value();\n>                 counted_zones++;\n>         }\n>  \n> @@ -174,8 +174,8 @@ void Awb::process(IPAContext &context, const uint32_t frame,\n>          * figure by the gains that were applied when the statistics for this\n>          * frame were generated.\n>          */\n> -       float rRatio = rgAvg / frameContext.awb.rGain;\n> -       float bRatio = bgAvg / frameContext.awb.bGain;\n> +       float rRatio = rgAvg / frameContext.awb.rGain.value();\n> +       float bRatio = bgAvg / frameContext.awb.bGain.value();\n>  \n>         /*\n>          * And then we can simply invert the ratio to find the gain we should\n> @@ -191,15 +191,15 @@ void Awb::process(IPAContext &context, const uint32_t frame,\n>          * want to fix the miscolouring as quickly as possible.\n>          */\n>         float speed = frame < kNumStartupFrames ? 1.0 : 0.2;\n> -       rGain = speed * rGain + context.activeState.awb.rGain * (1.0 - speed);\n> -       bGain = speed * bGain + context.activeState.awb.bGain * (1.0 - speed);\n> +       rGain = speed * rGain + context.activeState.awb.rGain.value() * (1.0 - speed);\n> +       bGain = speed * bGain + context.activeState.awb.bGain.value() * (1.0 - speed);\n>  \n>         context.activeState.awb.rGain = rGain;\n>         context.activeState.awb.bGain = bGain;\n>  \n>         metadata.set(controls::ColourGains, {\n> -               static_cast<float>(frameContext.awb.rGain),\n> -               static_cast<float>(frameContext.awb.bGain),\n> +               frameContext.awb.rGain.value(),\n> +               frameContext.awb.bGain.value(),\n>         });\n>  \n>         LOG(MaliC55Awb, Debug) << \"For frame number \" << frame << \": \"\n> diff --git a/src/ipa/mali-c55/ipa_context.h b/src/ipa/mali-c55/ipa_context.h\n> index 13885eb83b5c..08f78e4f74ce 100644\n> --- a/src/ipa/mali-c55/ipa_context.h\n> +++ b/src/ipa/mali-c55/ipa_context.h\n> @@ -14,6 +14,8 @@\n>  \n>  #include <libipa/fc_queue.h>\n>  \n> +#include \"libipa/fixedpoint.h\"\n> +\n>  namespace libcamera {\n>  \n>  namespace ipa::mali_c55 {\n> @@ -53,8 +55,8 @@ struct IPAActiveState {\n>         } agc;\n>  \n>         struct {\n> -               double rGain;\n> -               double bGain;\n> +               UQ<4, 8> rGain;\n> +               UQ<4, 8> bGain;\n>         } awb;\n>  };\n>  \n> @@ -66,8 +68,8 @@ struct IPAFrameContext : public FrameContext {\n>         } agc;\n>  \n>         struct {\n> -               double rGain;\n> -               double bGain;\n> +               UQ<4, 8> rGain;\n> +               UQ<4, 8> bGain;\n\nThis is the only part I don't really like. We encode hardware specific\nsizes in the context structure which I think are better kept in the\nalgorithm (this is the same in the RkISP1). But as you already planned\nto pull in the context types from the corresponding algorithms I guess\nthis is temporary.\n\nReviewed-by: Stefan Klug <stefan.klug@ideasonboard.com> \n\nCheers,\nStefan\n\n>         } awb;\n>  };\n>  \n> -- \n> 2.52.0\n>","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 828C6BDCBF\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri, 23 Jan 2026 14:24:59 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 73B6661FC9;\n\tFri, 23 Jan 2026 15:24:58 +0100 (CET)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 5282F615B2\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 23 Jan 2026 15:24:56 +0100 (CET)","from ideasonboard.com (unknown\n\t[IPv6:2a00:6020:448c:6c00:1d92:9f27:5dd1:dc89])\n\tby perceval.ideasonboard.com (Postfix) with UTF8SMTPSA id AD915103D; \n\tFri, 23 Jan 2026 15:24:22 +0100 (CET)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"ZIi2JGMA\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1769178262;\n\tbh=x9uBcWnHAD27CdbUsNZSVzwjWnwwlSZd2Rb4wOFYInQ=;\n\th=In-Reply-To:References:Subject:From:Cc:To:Date:From;\n\tb=ZIi2JGMAHGdOcdqIsDotGBzRIhczK2qc5xTds4XH3a59AGW3vqjuo2QR7d1JqUxzS\n\t0P94nnfbXlHa3uJXIEE+tFyHiCYa84OpoEic5cHyzHMq0G8XNgTUZ/ZGSlscK6wEu+\n\tYUcmneAYcZOD+ZG8LGZmUHtysSx8FZOvm1CdCiVs=","Content-Type":"text/plain; charset=\"utf-8\"","MIME-Version":"1.0","Content-Transfer-Encoding":"quoted-printable","In-Reply-To":"<20260121173737.376113-13-kieran.bingham@ideasonboard.com>","References":"<20260121173737.376113-1-kieran.bingham@ideasonboard.com>\n\t<20260121173737.376113-13-kieran.bingham@ideasonboard.com>","Subject":"Re: [PATCH v6 12/16] ipa: mali-c55: Convert AWB to UQ<4, 8> usage","From":"Stefan Klug <stefan.klug@ideasonboard.com>","Cc":"Kieran Bingham <kieran.bingham@ideasonboard.com>, Isaac Scott\n\t<isaac.scott@ideasonboard.com>, =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?=\n\t<barnabas.pocze@ideasonboard.com>","To":"Kieran Bingham <kieran.bingham@ideasonboard.com>,\n\tlibcamera devel <libcamera-devel@lists.libcamera.org>","Date":"Fri, 23 Jan 2026 15:24:53 +0100","Message-ID":"<176917829300.302817.15331273037194731250@localhost>","User-Agent":"alot/0.12.dev8+g2c003385c862.d20250602","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>"}}]