[{"id":36357,"web_url":"https://patchwork.libcamera.org/comment/36357/","msgid":"<176095505077.336133.10722989546023049525@localhost>","date":"2025-10-20T10:10:50","subject":"Re: [PATCH 4/5] ipa: rpi: vc4: Use a floating statistics region for\n\ta full image Y sum","submitter":{"id":184,"url":"https://patchwork.libcamera.org/api/people/184/","name":"Stefan Klug","email":"stefan.klug@ideasonboard.com"},"content":"Hi David,\n\nQuoting David Plowman (2025-10-17 12:05:40)\n> We're going to use a \"floating statistics region\" to store a full\n> image Y sum. The VC4 platform actually has no floating region for\n> this, but we can synthesize such a region as follows in software.\n> \n> We know that the 15 AGC regions that we do have are arranged to cover\n> the whole image, and they cannot be changed. Adding up the R, G and B\n> values here will get us most of the way to Y. But we do also need to\n> know the most recent colour gains, so code must also be added to\n> remember the last AWB status.\n> \n> With this change, algorithms can now look at the first floating region\n> on both VC4 and PiSP platforms to get a full image Y average value.\n> \n> Signed-off-by: David Plowman <david.plowman@raspberrypi.com>\n> ---\n>  src/ipa/rpi/vc4/vc4.cpp | 33 +++++++++++++++++++++++++++++++--\n>  1 file changed, 31 insertions(+), 2 deletions(-)\n> \n> diff --git a/src/ipa/rpi/vc4/vc4.cpp b/src/ipa/rpi/vc4/vc4.cpp\n> index ba43e474..fe754ace 100644\n> --- a/src/ipa/rpi/vc4/vc4.cpp\n> +++ b/src/ipa/rpi/vc4/vc4.cpp\n> @@ -43,6 +43,9 @@ public:\n>         IpaVc4()\n>                 : IpaBase(), lsTable_(nullptr)\n>         {\n> +               lastAwbStatus_.gainR = 1.0;\n> +               lastAwbStatus_.gainG = 1.0;\n> +               lastAwbStatus_.gainB = 1.0;\n>         }\n>  \n>         ~IpaVc4()\n> @@ -81,6 +84,9 @@ private:\n>         /* LS table allocation passed in from the pipeline handler. */\n>         SharedFD lsTableHandle_;\n>         void *lsTable_;\n> +\n> +       /* Remember the most recent AWB values. */\n> +       AwbStatus lastAwbStatus_;\n>  };\n>  \n>  int32_t IpaVc4::platformInit([[maybe_unused]] const InitParams &params, [[maybe_unused]] InitResult *result)\n> @@ -144,8 +150,10 @@ void IpaVc4::platformPrepareIsp([[maybe_unused]] const PrepareParams &params,\n>         std::unique_lock<RPiController::Metadata> lock(rpiMetadata);\n>  \n>         AwbStatus *awbStatus = rpiMetadata.getLocked<AwbStatus>(\"awb.status\");\n> -       if (awbStatus)\n> +       if (awbStatus) {\n>                 applyAWB(awbStatus, ctrls);\n> +               lastAwbStatus_ = *awbStatus;\n> +       }\n>  \n>         CcmStatus *ccmStatus = rpiMetadata.getLocked<CcmStatus>(\"ccm.status\");\n>         if (ccmStatus)\n> @@ -226,7 +234,15 @@ RPiController::StatisticsPtr IpaVc4::platformProcessStats(Span<uint8_t> mem)\n>                 LOG(IPARPI, Debug) << \"No AGC algorithm - not copying statistics\";\n>                 statistics->agcRegions.init(0);\n>         } else {\n> -               statistics->agcRegions.init(hw.agcRegions);\n> +               uint64_t sumR = 0;\n> +               uint64_t sumG = 0;\n> +               uint64_t sumB = 0;\n> +               uint32_t countedSum = 0;\n> +               uint32_t notCountedSum = 0;\n\nWhy not a RgbyRegions::Region as accumulator?\n\nBut either way works.\n\nReviewed-by: Stefan Klug <stefan.klug@ideasonboard.com> \n\nBest regards,\nStefan\n\n> +               /* We're going to pretend there's a floating region where we will put a full image Y sum. */\n> +               const unsigned int numFloating = 1;\n> +\n> +               statistics->agcRegions.init(hw.agcRegions, numFloating);\n>                 const std::vector<double> &weights = agc->getWeights();\n>                 for (i = 0; i < statistics->agcRegions.numRegions(); i++) {\n>                         uint64_t rSum = (stats->agc_stats[i].r_sum << scale) * weights[i];\n> @@ -237,7 +253,20 @@ RPiController::StatisticsPtr IpaVc4::platformProcessStats(Span<uint8_t> mem)\n>                         statistics->agcRegions.set(i, { { rSum, gSum, bSum },\n>                                                         counted,\n>                                                         notcounted });\n> +\n> +                       /* Accumulate values for the full image Y sum. */\n> +                       sumR += stats->agc_stats[i].r_sum << scale;\n> +                       sumG += stats->agc_stats[i].g_sum << scale;\n> +                       sumB += stats->agc_stats[i].b_sum << scale;\n> +                       countedSum += stats->agc_stats[i].counted;\n> +                       notCountedSum += stats->agc_stats[i].notcounted;\n>                 }\n> +\n> +               /* The \"floating\" region has the Y sum for the entire image. */\n> +               uint64_t sumY = sumR * lastAwbStatus_.gainR * 0.299 + sumG * lastAwbStatus_.gainG * 0.587 +\n> +                               sumB * lastAwbStatus_.gainB * 0.114;\n> +               statistics->agcRegions.setFloating(0, { { sumR, sumG, sumB, sumY },\n> +                                                       countedSum, notCountedSum });\n>         }\n>  \n>         statistics->focusRegions.init(hw.focusRegions);\n> -- \n> 2.47.3\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 7357DBE080\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon, 20 Oct 2025 10:10:56 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 91E1D60706;\n\tMon, 20 Oct 2025 12:10: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 CF13560706\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 20 Oct 2025 12:10:53 +0200 (CEST)","from ideasonboard.com (unknown\n\t[IPv6:2a00:6020:448c:6c00:9f50:5b33:d3ec:e1ea])\n\tby perceval.ideasonboard.com (Postfix) with UTF8SMTPSA id 0A5846AF;\n\tMon, 20 Oct 2025 12:09:11 +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=\"L6Nff58M\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1760954951;\n\tbh=d6FnsN7suMjKqkmJseB4M6LOP+ZlzvImCTTnBG2mmIo=;\n\th=In-Reply-To:References:Subject:From:Cc:To:Date:From;\n\tb=L6Nff58Mo3EL9tDd9KPS4kR4mAd1VeFDdsDetyyqnZidaSAFMNE9wYpuEp/9WTewB\n\t4dA75LHxJE4b31IgFiw6Ogc/n1pOGmevz+WghEvAiXYbFz35ysAI9QcNx8AkbRGPIw\n\tFYhUZrdPU6Ne9nTDl6+CMTPLSvfxtzcUKPj4vPGo=","Content-Type":"text/plain; charset=\"utf-8\"","MIME-Version":"1.0","Content-Transfer-Encoding":"quoted-printable","In-Reply-To":"<20251017102704.3887-5-david.plowman@raspberrypi.com>","References":"<20251017102704.3887-1-david.plowman@raspberrypi.com>\n\t<20251017102704.3887-5-david.plowman@raspberrypi.com>","Subject":"Re: [PATCH 4/5] ipa: rpi: vc4: Use a floating statistics region for\n\ta full image Y sum","From":"Stefan Klug <stefan.klug@ideasonboard.com>","Cc":"David Plowman <david.plowman@raspberrypi.com>","To":"David Plowman <david.plowman@raspberrypi.com>,\n\tlibcamera-devel@lists.libcamera.org","Date":"Mon, 20 Oct 2025 12:10:50 +0200","Message-ID":"<176095505077.336133.10722989546023049525@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>"}},{"id":36359,"web_url":"https://patchwork.libcamera.org/comment/36359/","msgid":"<CAHW6GYLF6a3155mtG1B7bSrZtEoe65=0L4haX510-=G=yam18Q@mail.gmail.com>","date":"2025-10-20T10:32:08","subject":"Re: [PATCH 4/5] ipa: rpi: vc4: Use a floating statistics region for\n\ta full image Y sum","submitter":{"id":42,"url":"https://patchwork.libcamera.org/api/people/42/","name":"David Plowman","email":"david.plowman@raspberrypi.com"},"content":"Hi Stefan\n\nThanks for this (and the other!) reviews.\n\nOn Mon, 20 Oct 2025 at 11:10, Stefan Klug <stefan.klug@ideasonboard.com> wrote:\n>\n> Hi David,\n>\n> Quoting David Plowman (2025-10-17 12:05:40)\n> > We're going to use a \"floating statistics region\" to store a full\n> > image Y sum. The VC4 platform actually has no floating region for\n> > this, but we can synthesize such a region as follows in software.\n> >\n> > We know that the 15 AGC regions that we do have are arranged to cover\n> > the whole image, and they cannot be changed. Adding up the R, G and B\n> > values here will get us most of the way to Y. But we do also need to\n> > know the most recent colour gains, so code must also be added to\n> > remember the last AWB status.\n> >\n> > With this change, algorithms can now look at the first floating region\n> > on both VC4 and PiSP platforms to get a full image Y average value.\n> >\n> > Signed-off-by: David Plowman <david.plowman@raspberrypi.com>\n> > ---\n> >  src/ipa/rpi/vc4/vc4.cpp | 33 +++++++++++++++++++++++++++++++--\n> >  1 file changed, 31 insertions(+), 2 deletions(-)\n> >\n> > diff --git a/src/ipa/rpi/vc4/vc4.cpp b/src/ipa/rpi/vc4/vc4.cpp\n> > index ba43e474..fe754ace 100644\n> > --- a/src/ipa/rpi/vc4/vc4.cpp\n> > +++ b/src/ipa/rpi/vc4/vc4.cpp\n> > @@ -43,6 +43,9 @@ public:\n> >         IpaVc4()\n> >                 : IpaBase(), lsTable_(nullptr)\n> >         {\n> > +               lastAwbStatus_.gainR = 1.0;\n> > +               lastAwbStatus_.gainG = 1.0;\n> > +               lastAwbStatus_.gainB = 1.0;\n> >         }\n> >\n> >         ~IpaVc4()\n> > @@ -81,6 +84,9 @@ private:\n> >         /* LS table allocation passed in from the pipeline handler. */\n> >         SharedFD lsTableHandle_;\n> >         void *lsTable_;\n> > +\n> > +       /* Remember the most recent AWB values. */\n> > +       AwbStatus lastAwbStatus_;\n> >  };\n> >\n> >  int32_t IpaVc4::platformInit([[maybe_unused]] const InitParams &params, [[maybe_unused]] InitResult *result)\n> > @@ -144,8 +150,10 @@ void IpaVc4::platformPrepareIsp([[maybe_unused]] const PrepareParams &params,\n> >         std::unique_lock<RPiController::Metadata> lock(rpiMetadata);\n> >\n> >         AwbStatus *awbStatus = rpiMetadata.getLocked<AwbStatus>(\"awb.status\");\n> > -       if (awbStatus)\n> > +       if (awbStatus) {\n> >                 applyAWB(awbStatus, ctrls);\n> > +               lastAwbStatus_ = *awbStatus;\n> > +       }\n> >\n> >         CcmStatus *ccmStatus = rpiMetadata.getLocked<CcmStatus>(\"ccm.status\");\n> >         if (ccmStatus)\n> > @@ -226,7 +234,15 @@ RPiController::StatisticsPtr IpaVc4::platformProcessStats(Span<uint8_t> mem)\n> >                 LOG(IPARPI, Debug) << \"No AGC algorithm - not copying statistics\";\n> >                 statistics->agcRegions.init(0);\n> >         } else {\n> > -               statistics->agcRegions.init(hw.agcRegions);\n> > +               uint64_t sumR = 0;\n> > +               uint64_t sumG = 0;\n> > +               uint64_t sumB = 0;\n> > +               uint32_t countedSum = 0;\n> > +               uint32_t notCountedSum = 0;\n>\n> Why not a RgbyRegions::Region as accumulator?\n\nGood point - I'll look into that.\n\nThanks!\n\nDavid\n\n>\n> But either way works.\n>\n> Reviewed-by: Stefan Klug <stefan.klug@ideasonboard.com>\n>\n> Best regards,\n> Stefan\n>\n> > +               /* We're going to pretend there's a floating region where we will put a full image Y sum. */\n> > +               const unsigned int numFloating = 1;\n> > +\n> > +               statistics->agcRegions.init(hw.agcRegions, numFloating);\n> >                 const std::vector<double> &weights = agc->getWeights();\n> >                 for (i = 0; i < statistics->agcRegions.numRegions(); i++) {\n> >                         uint64_t rSum = (stats->agc_stats[i].r_sum << scale) * weights[i];\n> > @@ -237,7 +253,20 @@ RPiController::StatisticsPtr IpaVc4::platformProcessStats(Span<uint8_t> mem)\n> >                         statistics->agcRegions.set(i, { { rSum, gSum, bSum },\n> >                                                         counted,\n> >                                                         notcounted });\n> > +\n> > +                       /* Accumulate values for the full image Y sum. */\n> > +                       sumR += stats->agc_stats[i].r_sum << scale;\n> > +                       sumG += stats->agc_stats[i].g_sum << scale;\n> > +                       sumB += stats->agc_stats[i].b_sum << scale;\n> > +                       countedSum += stats->agc_stats[i].counted;\n> > +                       notCountedSum += stats->agc_stats[i].notcounted;\n> >                 }\n> > +\n> > +               /* The \"floating\" region has the Y sum for the entire image. */\n> > +               uint64_t sumY = sumR * lastAwbStatus_.gainR * 0.299 + sumG * lastAwbStatus_.gainG * 0.587 +\n> > +                               sumB * lastAwbStatus_.gainB * 0.114;\n> > +               statistics->agcRegions.setFloating(0, { { sumR, sumG, sumB, sumY },\n> > +                                                       countedSum, notCountedSum });\n> >         }\n> >\n> >         statistics->focusRegions.init(hw.focusRegions);\n> > --\n> > 2.47.3\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 61821BE080\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon, 20 Oct 2025 10:32:23 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 898D260713;\n\tMon, 20 Oct 2025 12:32:22 +0200 (CEST)","from mail-qk1-x732.google.com (mail-qk1-x732.google.com\n\t[IPv6:2607:f8b0:4864:20::732])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 7CFE960706\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 20 Oct 2025 12:32:20 +0200 (CEST)","by mail-qk1-x732.google.com with SMTP id\n\taf79cd13be357-88e51cf965dso913119885a.2\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 20 Oct 2025 03:32:20 -0700 (PDT)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (2048-bit key;\n\tunprotected) header.d=raspberrypi.com header.i=@raspberrypi.com\n\theader.b=\"SXV0sWUW\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=raspberrypi.com; s=google; t=1760956339; x=1761561139;\n\tdarn=lists.libcamera.org; \n\th=cc:to:subject:message-id:date:from:in-reply-to:references\n\t:mime-version:from:to:cc:subject:date:message-id:reply-to;\n\tbh=kvfS7g2w83Mpb5qykCtPk9+aH/Z3gqVKF5skxsOUkwA=;\n\tb=SXV0sWUWFDETwgRECsh+0UxZO8e5oDTF8RzH4LVbYph7gwEoj8laDGJHChSDMt8b3z\n\tq6CjQiwbPYaWz/gfxSDZAB2/7ZBAgTy5uR4h5VcYUZ4aUvkbdCgijNGVvkaq/hA4ksQT\n\t6bizpweZQQAaNEBWvM07GKmQAzQKbb1GyojGsTgxIBDPyLZuceFYRh6ZWKfLfdTFez0j\n\tjdw4Xd05UOIAzaGj0pIMXMpSkRFsC7NUu4ENw0zY2pyJ3xDmRc9sccX9yMbgaqLUr4DW\n\tO2EhjZnzyLRHDad5QIIKQJDemHVcavURiZkZdT6Z16FQXIY8BbJkOO0DGcUppm7Sb2O/\n\tbL6Q==","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20230601; t=1760956339; x=1761561139;\n\th=cc:to:subject:message-id:date:from:in-reply-to:references\n\t:mime-version:x-gm-message-state:from:to:cc:subject:date:message-id\n\t:reply-to;\n\tbh=kvfS7g2w83Mpb5q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ahvBrDNZPVFgwwZensnOhKqsXcp8yG+c8I=","X-Received":"by 2002:a05:620a:4621:b0:870:6891:9c19 with SMTP id\n\taf79cd13be357-8906e8ae76dmr1542716585a.19.1760956339175;\n\tMon, 20 Oct 2025 03:32:19 -0700 (PDT)","MIME-Version":"1.0","References":"<20251017102704.3887-1-david.plowman@raspberrypi.com>\n\t<20251017102704.3887-5-david.plowman@raspberrypi.com>\n\t<176095505077.336133.10722989546023049525@localhost>","In-Reply-To":"<176095505077.336133.10722989546023049525@localhost>","From":"David Plowman <david.plowman@raspberrypi.com>","Date":"Mon, 20 Oct 2025 11:32:08 +0100","X-Gm-Features":"AS18NWAC6PpP_Gp04BEIkoMIXzSXdOqnfJq0G0zfuT2nV_aCrKI5bBYGNvVzhWE","Message-ID":"<CAHW6GYLF6a3155mtG1B7bSrZtEoe65=0L4haX510-=G=yam18Q@mail.gmail.com>","Subject":"Re: [PATCH 4/5] ipa: rpi: vc4: Use a floating statistics region for\n\ta full image Y sum","To":"Stefan Klug <stefan.klug@ideasonboard.com>","Cc":"libcamera-devel@lists.libcamera.org","Content-Type":"text/plain; charset=\"UTF-8\"","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>"}},{"id":36399,"web_url":"https://patchwork.libcamera.org/comment/36399/","msgid":"<CAEmqJPrHPAVV_6BmsviaQ4R4jZhgs05CRvk4U0w3Q-6eTsEpaw@mail.gmail.com>","date":"2025-10-23T07:52:48","subject":"Re: [PATCH 4/5] ipa: rpi: vc4: Use a floating statistics region for\n\ta full image Y sum","submitter":{"id":34,"url":"https://patchwork.libcamera.org/api/people/34/","name":"Naushir Patuck","email":"naush@raspberrypi.com"},"content":"Hi David,\n\nOn Mon, 20 Oct 2025 at 11:10, Stefan Klug <stefan.klug@ideasonboard.com> wrote:\n>\n> Hi David,\n>\n> Quoting David Plowman (2025-10-17 12:05:40)\n> > We're going to use a \"floating statistics region\" to store a full\n> > image Y sum. The VC4 platform actually has no floating region for\n> > this, but we can synthesize such a region as follows in software.\n> >\n> > We know that the 15 AGC regions that we do have are arranged to cover\n> > the whole image, and they cannot be changed. Adding up the R, G and B\n> > values here will get us most of the way to Y. But we do also need to\n> > know the most recent colour gains, so code must also be added to\n> > remember the last AWB status.\n> >\n> > With this change, algorithms can now look at the first floating region\n> > on both VC4 and PiSP platforms to get a full image Y average value.\n> >\n> > Signed-off-by: David Plowman <david.plowman@raspberrypi.com>\n> > ---\n> >  src/ipa/rpi/vc4/vc4.cpp | 33 +++++++++++++++++++++++++++++++--\n> >  1 file changed, 31 insertions(+), 2 deletions(-)\n> >\n> > diff --git a/src/ipa/rpi/vc4/vc4.cpp b/src/ipa/rpi/vc4/vc4.cpp\n> > index ba43e474..fe754ace 100644\n> > --- a/src/ipa/rpi/vc4/vc4.cpp\n> > +++ b/src/ipa/rpi/vc4/vc4.cpp\n> > @@ -43,6 +43,9 @@ public:\n> >         IpaVc4()\n> >                 : IpaBase(), lsTable_(nullptr)\n> >         {\n> > +               lastAwbStatus_.gainR = 1.0;\n> > +               lastAwbStatus_.gainG = 1.0;\n> > +               lastAwbStatus_.gainB = 1.0;\n> >         }\n> >\n> >         ~IpaVc4()\n> > @@ -81,6 +84,9 @@ private:\n> >         /* LS table allocation passed in from the pipeline handler. */\n> >         SharedFD lsTableHandle_;\n> >         void *lsTable_;\n> > +\n> > +       /* Remember the most recent AWB values. */\n> > +       AwbStatus lastAwbStatus_;\n> >  };\n> >\n> >  int32_t IpaVc4::platformInit([[maybe_unused]] const InitParams &params, [[maybe_unused]] InitResult *result)\n> > @@ -144,8 +150,10 @@ void IpaVc4::platformPrepareIsp([[maybe_unused]] const PrepareParams &params,\n> >         std::unique_lock<RPiController::Metadata> lock(rpiMetadata);\n> >\n> >         AwbStatus *awbStatus = rpiMetadata.getLocked<AwbStatus>(\"awb.status\");\n> > -       if (awbStatus)\n> > +       if (awbStatus) {\n> >                 applyAWB(awbStatus, ctrls);\n> > +               lastAwbStatus_ = *awbStatus;\n> > +       }\n> >\n> >         CcmStatus *ccmStatus = rpiMetadata.getLocked<CcmStatus>(\"ccm.status\");\n> >         if (ccmStatus)\n> > @@ -226,7 +234,15 @@ RPiController::StatisticsPtr IpaVc4::platformProcessStats(Span<uint8_t> mem)\n> >                 LOG(IPARPI, Debug) << \"No AGC algorithm - not copying statistics\";\n> >                 statistics->agcRegions.init(0);\n> >         } else {\n> > -               statistics->agcRegions.init(hw.agcRegions);\n> > +               uint64_t sumR = 0;\n> > +               uint64_t sumG = 0;\n> > +               uint64_t sumB = 0;\n> > +               uint32_t countedSum = 0;\n> > +               uint32_t notCountedSum = 0;\n>\n> Why not a RgbyRegions::Region as accumulator?\n\n\nYes, that sounds sensible, and avoids having very similar accumulator names :)\n\nWith that change:\n\nReviewed-by: Naushir Patuck <naush@raspberrypi.com>\n\n>\n>\n> But either way works.\n>\n> Reviewed-by: Stefan Klug <stefan.klug@ideasonboard.com>\n>\n> Best regards,\n> Stefan\n>\n> > +               /* We're going to pretend there's a floating region where we will put a full image Y sum. */\n> > +               const unsigned int numFloating = 1;\n> > +\n> > +               statistics->agcRegions.init(hw.agcRegions, numFloating);\n> >                 const std::vector<double> &weights = agc->getWeights();\n> >                 for (i = 0; i < statistics->agcRegions.numRegions(); i++) {\n> >                         uint64_t rSum = (stats->agc_stats[i].r_sum << scale) * weights[i];\n> > @@ -237,7 +253,20 @@ RPiController::StatisticsPtr IpaVc4::platformProcessStats(Span<uint8_t> mem)\n> >                         statistics->agcRegions.set(i, { { rSum, gSum, bSum },\n> >                                                         counted,\n> >                                                         notcounted });\n> > +\n> > +                       /* Accumulate values for the full image Y sum. */\n> > +                       sumR += stats->agc_stats[i].r_sum << scale;\n> > +                       sumG += stats->agc_stats[i].g_sum << scale;\n> > +                       sumB += stats->agc_stats[i].b_sum << scale;\n> > +                       countedSum += stats->agc_stats[i].counted;\n> > +                       notCountedSum += stats->agc_stats[i].notcounted;\n> >                 }\n> > +\n> > +               /* The \"floating\" region has the Y sum for the entire image. */\n> > +               uint64_t sumY = sumR * lastAwbStatus_.gainR * 0.299 + sumG * lastAwbStatus_.gainG * 0.587 +\n> > +                               sumB * lastAwbStatus_.gainB * 0.114;\n> > +               statistics->agcRegions.setFloating(0, { { sumR, sumG, sumB, sumY },\n> > +                                                       countedSum, notCountedSum });\n> >         }\n> >\n> >         statistics->focusRegions.init(hw.focusRegions);\n> > --\n> > 2.47.3\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 39216C3259\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu, 23 Oct 2025 07:53:28 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 3E49F607B9;\n\tThu, 23 Oct 2025 09:53:27 +0200 (CEST)","from mail-vs1-xe36.google.com (mail-vs1-xe36.google.com\n\t[IPv6:2607:f8b0:4864:20::e36])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 6F8C6607A8\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 23 Oct 2025 09:53:25 +0200 (CEST)","by mail-vs1-xe36.google.com with SMTP id\n\tada2fe7eead31-5db08dd7131so40673137.3\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 23 Oct 2025 00:53:25 -0700 (PDT)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (2048-bit key;\n\tunprotected) header.d=raspberrypi.com header.i=@raspberrypi.com\n\theader.b=\"K6nHPM8w\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=raspberrypi.com; s=google; t=1761206004; x=1761810804;\n\tdarn=lists.libcamera.org; \n\th=cc:to:subject:message-id:date:from:in-reply-to:references\n\t:mime-version:from:to:cc:subject:date:message-id:reply-to;\n\tbh=O37ifbM83iL4g9bgGr1rb4YHTNWKbOABgfuxryT8wOY=;\n\tb=K6nHPM8wfGq+f39Nphqb856rgXlEwB2zcoJjLM492qyE/Nx4AsnVyqjATUdA5vlCYZ\n\tdA9/omwQnPGE+jz+brQakPMfyYBzoW4Vk7sIfhr3uHNWWUWJVj8V/S1BkV0aFbFaT2Sz\n\t61wayHeqAkdXpEVYnlA4Fu6+cPygT92x5GvTtEzvlowUdNHGaZWBuCZ6BLYnnbhoJ2Os\n\tJgUC1KonAkozC3HmnFrnvb1sUCIAz8l5FReu9mE5+FzGiYKcReygPhcpLmQNtlLJ27lJ\n\tUUnqFf8xUL1ZgW0s6kgpmx4VL/aUVJRCeqKRW7az0ipZpnvA2BDnLsqecWwg574rz2xz\n\t3bfw==","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20230601; t=1761206004; x=1761810804;\n\th=cc:to:subject:message-id:date:from:in-reply-to:references\n\t:mime-version:x-gm-message-state:from:to:cc:subject:date:message-id\n\t:reply-to;\n\tbh=O37ifbM83iL4g9bgGr1rb4YHTNWKbOABgfuxryT8wOY=;\n\tb=ON0lJmf1qjVBodFYxkPltdXX+d71nrzwGeRQh1oBIb2vG2O01FyRaClE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Chvsu3bk=","X-Received":"by 2002:a05:6102:54a2:b0:5db:3585:78bf with SMTP id\n\tada2fe7eead31-5db35857a86mr24706137.4.1761206004276; Thu, 23 Oct 2025\n\t00:53:24 -0700 (PDT)","MIME-Version":"1.0","References":"<20251017102704.3887-1-david.plowman@raspberrypi.com>\n\t<20251017102704.3887-5-david.plowman@raspberrypi.com>\n\t<176095505077.336133.10722989546023049525@localhost>","In-Reply-To":"<176095505077.336133.10722989546023049525@localhost>","From":"Naushir Patuck <naush@raspberrypi.com>","Date":"Thu, 23 Oct 2025 08:52:48 +0100","X-Gm-Features":"AWmQ_bk6Pp6IFWLmgdsuxbK27WuVmVmkuDM-CZY2IfpFkBmNR8ltpExKLcnB-Ww","Message-ID":"<CAEmqJPrHPAVV_6BmsviaQ4R4jZhgs05CRvk4U0w3Q-6eTsEpaw@mail.gmail.com>","Subject":"Re: [PATCH 4/5] ipa: rpi: vc4: Use a floating statistics region for\n\ta full image Y sum","To":"Stefan Klug <stefan.klug@ideasonboard.com>","Cc":"David Plowman <david.plowman@raspberrypi.com>,\n\tlibcamera-devel@lists.libcamera.org","Content-Type":"text/plain; charset=\"UTF-8\"","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>"}}]