{"id":24701,"url":"https://patchwork.libcamera.org/api/1.1/patches/24701/?format=json","web_url":"https://patchwork.libcamera.org/patch/24701/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20251017102704.3887-5-david.plowman@raspberrypi.com>","date":"2025-10-17T10:05:40","name":"[4/5] ipa: rpi: vc4: Use a floating statistics region for a full image Y sum","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"15bca5573ee9700bf3fea8cbd8070dc653fc5f8e","submitter":{"id":42,"url":"https://patchwork.libcamera.org/api/1.1/people/42/?format=json","name":"David Plowman","email":"david.plowman@raspberrypi.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/24701/mbox/","series":[{"id":5509,"url":"https://patchwork.libcamera.org/api/1.1/series/5509/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=5509","date":"2025-10-17T10:05:36","name":"Fix and improve full image Y statistics","version":1,"mbox":"https://patchwork.libcamera.org/series/5509/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/24701/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/24701/checks/","tags":{},"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 232E9C3259\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri, 17 Oct 2025 10:27:17 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id E47BA606D6;\n\tFri, 17 Oct 2025 12:27:15 +0200 (CEST)","from mail-wm1-x333.google.com (mail-wm1-x333.google.com\n\t[IPv6:2a00:1450:4864:20::333])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id EA7B5606AA\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 17 Oct 2025 12:27:11 +0200 (CEST)","by mail-wm1-x333.google.com with SMTP id\n\t5b1f17b1804b1-46e542196c7so18941235e9.0\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 17 Oct 2025 03:27:11 -0700 (PDT)","from localhost.localdomain ([2a06:61c0:f337:0:8aba:2a85:b765:a23e])\n\tby smtp.gmail.com with ESMTPSA id\n\t5b1f17b1804b1-47114428dedsm73811905e9.7.2025.10.17.03.27.10\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tFri, 17 Oct 2025 03:27:10 -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=\"LNxNB66I\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=raspberrypi.com; s=google; t=1760696831; x=1761301631;\n\tdarn=lists.libcamera.org; \n\th=content-transfer-encoding:mime-version:references:in-reply-to\n\t:message-id:date:subject:cc:to:from:from:to:cc:subject:date\n\t:message-id:reply-to;\n\tbh=5WgHdFl1AJENoxSlexdPAxRBVpUNyl02PN8mna4Wtas=;\n\tb=LNxNB66IoUl3qoje+ZHg+1aGz8z9WoBo6awbK2Krs/U20Sdj397cPsdi5QtHa2PmHR\n\t3bpcxVUUvgTogc61QvBBxlj6ip4LE8dq43ii84EXKY+076lyUVUPiZmqEYIvpmGaotlJ\n\tY6705HtcFoANM9lgtdEsjK1P1+eM39MEcioXgUzVEJgUstftMei+fGpNGyhMJE0i1ayc\n\tDUwmOfDLcX7uYokF4tC0airwcItkG03p4FL4bMFyfdBKf7gkDzJgYAtXeRZkPJhCma9M\n\tK7XYNY1T7KkjwaAlhtX94zLZztKDbMng7rybAccm7c/hw9GgWim89zVHwUBpS8uYW8Yw\n\tIf1A==","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20230601; t=1760696831; x=1761301631;\n\th=content-transfer-encoding:mime-version:references:in-reply-to\n\t:message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc\n\t:subject:date:message-id:reply-to;\n\tbh=5WgHdFl1AJENoxSlexdPAxRBVpUNyl02PN8mna4Wtas=;\n\tb=PN3iILvFXDTh6fxsb4gs5PBBnSnDzF8mekYuHyfeb4GRyJveZEAhq7Vk3zDGtFa7/0\n\tSSeTEIyDGMC2j5canzp6rjQcL5iBJLS015XZqvlHarAAEd8ZvFgy1Ens0SG8v84CToQH\n\tWck545ufcfuH0uImQHLgu4U5WYgv4xRoqrQhXGqdOYy0hfHc3qT+L7ywRsjTPOMswGd2\n\ti5Gx9QuXqYkkBp5f8Ap2ONvHsL/vbP2BiOmRT3rEhpSAmFE2l4gIqzNJkuO1q3SeGdFs\n\taMlrr1utteM+QEXqrYNTV3DUSwh2wcf5g4xlnd0NF3gV/e9uhDX9CXW73BzG4Q/rzFJs\n\tK5ng==","X-Gm-Message-State":"AOJu0Yxc3E/zXPyquSw9EWhGfy5UyhztohsvwneU436VDi4AUG9Od54b\n\tx7tQVFdDa4bp6z9f8dM02008mNOf1fbccV/HNgMi6NuaEnxyE/BdmiE3RcF3r5SMoHdTNmYyh9e\n\tF7MrQCPg=","X-Gm-Gg":"ASbGncsxekbQcEJ68hPoqV/4ITqEA7vxer5UY2az1AM9VM1qjIJRg0djaZlGT1UDPaV\n\tdbXIYP+gs6w1bC8evl+VQelQXrN0u9QkTQmQjxZiwqWjbBA3e1UYLUXkjWweuOiQsKjmdM9kmnB\n\tacXguXt3elPQRf5p19evGgWKm8+z5hpZv9fWbe7S5C1wxoUwK4dpl8FFj9qYR7LRbw4kiNluxd1\n\tWmICK/oFDzHny2WPV3obcEZKTeGVxzZIYDvNaalGG6metZ2cxKJ3bobUFFq4e6HZgzGKDXih50u\n\tbNtm6z3HgEwjCL1Yux0HjFijxuLYNQ+zRQRXcZRVwHX82iPhWlMkgjnw5r+gdAo03A132FmHtQZ\n\th9n8zAbh9fRezCOmdxSbKFi77o9fv9NYK/+6UnBYQQ7+04CGAnuT2RsyPYFxBnwtm4mB2BPJZuO\n\t5IrA4GtNSLx1UkoCj3zvlMTp3t9ZSoNR9AieltBwEIN+3PpO7f673VG3EAOnQmfN2IbG8HStmPJ\n\t+5BCgeVatCz04d2ZPbv2DEs6g==","X-Google-Smtp-Source":"AGHT+IG07nhaHBJClLuB6nlo9gTfbqjmYZC+ddGWKUPdh6q+C0rs+L9kID7KWmJsHjRnLTtI4zRWJg==","X-Received":"by 2002:a05:600c:548c:b0:45d:5c71:769d with SMTP id\n\t5b1f17b1804b1-4711726289cmr30154785e9.8.1760696831044; \n\tFri, 17 Oct 2025 03:27:11 -0700 (PDT)","From":"David Plowman <david.plowman@raspberrypi.com>","To":"libcamera-devel@lists.libcamera.org","Cc":"David Plowman <david.plowman@raspberrypi.com>","Subject":"[PATCH 4/5] ipa: rpi: vc4: Use a floating statistics region for a\n\tfull image Y sum","Date":"Fri, 17 Oct 2025 11:05:40 +0100","Message-ID":"<20251017102704.3887-5-david.plowman@raspberrypi.com>","X-Mailer":"git-send-email 2.47.3","In-Reply-To":"<20251017102704.3887-1-david.plowman@raspberrypi.com>","References":"<20251017102704.3887-1-david.plowman@raspberrypi.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","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>"},"content":"We're going to use a \"floating statistics region\" to store a full\nimage Y sum. The VC4 platform actually has no floating region for\nthis, but we can synthesize such a region as follows in software.\n\nWe know that the 15 AGC regions that we do have are arranged to cover\nthe whole image, and they cannot be changed. Adding up the R, G and B\nvalues here will get us most of the way to Y. But we do also need to\nknow the most recent colour gains, so code must also be added to\nremember the last AWB status.\n\nWith this change, algorithms can now look at the first floating region\non both VC4 and PiSP platforms to get a full image Y average value.\n\nSigned-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(-)","diff":"diff --git a/src/ipa/rpi/vc4/vc4.cpp b/src/ipa/rpi/vc4/vc4.cpp\nindex 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 \tIpaVc4()\n \t\t: IpaBase(), lsTable_(nullptr)\n \t{\n+\t\tlastAwbStatus_.gainR = 1.0;\n+\t\tlastAwbStatus_.gainG = 1.0;\n+\t\tlastAwbStatus_.gainB = 1.0;\n \t}\n \n \t~IpaVc4()\n@@ -81,6 +84,9 @@ private:\n \t/* LS table allocation passed in from the pipeline handler. */\n \tSharedFD lsTableHandle_;\n \tvoid *lsTable_;\n+\n+\t/* Remember the most recent AWB values. */\n+\tAwbStatus 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 \tstd::unique_lock<RPiController::Metadata> lock(rpiMetadata);\n \n \tAwbStatus *awbStatus = rpiMetadata.getLocked<AwbStatus>(\"awb.status\");\n-\tif (awbStatus)\n+\tif (awbStatus) {\n \t\tapplyAWB(awbStatus, ctrls);\n+\t\tlastAwbStatus_ = *awbStatus;\n+\t}\n \n \tCcmStatus *ccmStatus = rpiMetadata.getLocked<CcmStatus>(\"ccm.status\");\n \tif (ccmStatus)\n@@ -226,7 +234,15 @@ RPiController::StatisticsPtr IpaVc4::platformProcessStats(Span<uint8_t> mem)\n \t\tLOG(IPARPI, Debug) << \"No AGC algorithm - not copying statistics\";\n \t\tstatistics->agcRegions.init(0);\n \t} else {\n-\t\tstatistics->agcRegions.init(hw.agcRegions);\n+\t\tuint64_t sumR = 0;\n+\t\tuint64_t sumG = 0;\n+\t\tuint64_t sumB = 0;\n+\t\tuint32_t countedSum = 0;\n+\t\tuint32_t notCountedSum = 0;\n+\t\t/* We're going to pretend there's a floating region where we will put a full image Y sum. */\n+\t\tconst unsigned int numFloating = 1;\n+\n+\t\tstatistics->agcRegions.init(hw.agcRegions, numFloating);\n \t\tconst std::vector<double> &weights = agc->getWeights();\n \t\tfor (i = 0; i < statistics->agcRegions.numRegions(); i++) {\n \t\t\tuint64_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 \t\t\tstatistics->agcRegions.set(i, { { rSum, gSum, bSum },\n \t\t\t\t\t\t\tcounted,\n \t\t\t\t\t\t\tnotcounted });\n+\n+\t\t\t/* Accumulate values for the full image Y sum. */\n+\t\t\tsumR += stats->agc_stats[i].r_sum << scale;\n+\t\t\tsumG += stats->agc_stats[i].g_sum << scale;\n+\t\t\tsumB += stats->agc_stats[i].b_sum << scale;\n+\t\t\tcountedSum += stats->agc_stats[i].counted;\n+\t\t\tnotCountedSum += stats->agc_stats[i].notcounted;\n \t\t}\n+\n+\t\t/* The \"floating\" region has the Y sum for the entire image. */\n+\t\tuint64_t sumY = sumR * lastAwbStatus_.gainR * 0.299 + sumG * lastAwbStatus_.gainG * 0.587 +\n+\t\t\t\tsumB * lastAwbStatus_.gainB * 0.114;\n+\t\tstatistics->agcRegions.setFloating(0, { { sumR, sumG, sumB, sumY },\n+\t\t\t\t\t\t\tcountedSum, notCountedSum });\n \t}\n \n \tstatistics->focusRegions.init(hw.focusRegions);\n","prefixes":["4/5"]}