{"id":18228,"url":"https://patchwork.libcamera.org/api/patches/18228/?format=json","web_url":"https://patchwork.libcamera.org/patch/18228/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/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":"<20230131134646.9868-1-naush@raspberrypi.com>","date":"2023-01-31T13:46:46","name":"[libcamera-devel] ipa: raspberrypi: Normalise region sums to 16-bits","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"e44a725c42b77b4f0be995e3e38e0e99fafbfd21","submitter":{"id":34,"url":"https://patchwork.libcamera.org/api/people/34/?format=json","name":"Naushir Patuck","email":"naush@raspberrypi.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/18228/mbox/","series":[{"id":3734,"url":"https://patchwork.libcamera.org/api/series/3734/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=3734","date":"2023-01-31T13:46:46","name":"[libcamera-devel] ipa: raspberrypi: Normalise region sums to 16-bits","version":1,"mbox":"https://patchwork.libcamera.org/series/3734/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/18228/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/18228/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 CA0C7C323E\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue, 31 Jan 2023 13:46:53 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 109E5625E4;\n\tTue, 31 Jan 2023 14:46:53 +0100 (CET)","from mail-wr1-x42f.google.com (mail-wr1-x42f.google.com\n\t[IPv6:2a00:1450:4864:20::42f])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 0E3A9625D0\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 31 Jan 2023 14:46:50 +0100 (CET)","by mail-wr1-x42f.google.com with SMTP id q5so14294743wrv.0\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 31 Jan 2023 05:46:50 -0800 (PST)","from localhost.localdomain ([193.209.96.36])\n\tby smtp.gmail.com with ESMTPSA id\n\tz14-20020a5d4c8e000000b002bfc2d0eff0sm14683100wrs.47.2023.01.31.05.46.49\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tTue, 31 Jan 2023 05:46:49 -0800 (PST)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1675172813;\n\tbh=JN4gQ1MBFYa/8+ILfy4U82x+R4HVJcVm9NutWcPmol8=;\n\th=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe:\n\tList-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:\n\tFrom;\n\tb=lOEVySZ2xrTueoMgCQz5uxarSaYMJJGPxjOqXj9Rk1Pe4YEuFuW6ld3ww8ALqaqrb\n\tufvsHIjnMshVt6vxUzXpcw6GkElqi1fjwYf1FtKRbipVd0TKjx8e5PWk9nk6Qvi3J3\n\tA4FTRyoUrCxpCyWuT0v/riHi4Gjzn34Jmlw3TYb5Dg5BibgwKrxvq+15q0updwapB5\n\tqdM/H+tNnV1iM2PdXBzzdzWH5eHYCuAwWB/imGMwPURE+q2JeO9cY5DFtK3YsJR2Ra\n\tqc89COHXgwo0jPlIiiUxheA2VDUkXGjnT5elIXVHlUSNbHeYNS14U+cQzW7HoXUt3Q\n\tIN7cDM+FuBRCQ==","v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=raspberrypi.com; s=google;\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=L25f+/Pygp7MQMp/Xzzj481+r909qsH9nphy5s3SDT8=;\n\tb=Ie1Kx5tCb3AOTVD/k6OeI6GZwWmgnqekT8KN/SMHu+VVRv/XdW6IGZiEc+bwxhDzk+\n\tmWBIVCRp0ZIr0+HvIzj+cXhhNJ73gwywin4Ezmejr/4/0ZUi0N6ThmNieTvgmY/POj4y\n\t0pU6ZPYkildxCE4usDMtCGOB69k3MOw4gGrmPn0RTuFQAsHw/ONEsNmkWwVgESzp2Wo1\n\tBtfiMYOte6ymaBRPL73R+rRLjdynQEuEuOtS9sANrLp0aErWPZd5Tj1Dq6Rt/c8W9w0k\n\t4nahT7PDVrP3mhVrqPzR/yXUgQD2oas+UGotf56BzbQx59kZRcvotC/JmHznsLAnfKT4\n\tHkXg=="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (2048-bit key; \n\tunprotected) header.d=raspberrypi.com\n\theader.i=@raspberrypi.com\n\theader.b=\"Ie1Kx5tC\"; dkim-atps=neutral","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20210112;\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=L25f+/Pygp7MQMp/Xzzj481+r909qsH9nphy5s3SDT8=;\n\tb=bEdezsnmXuTylYw7Utzy/lIprv7+lel0f8a7LLk6de7quuouLcRpJrYYvz+3mhC7lq\n\tErPg4ultriy+rEUk7u8T/GFrNAgwGBEC8BGUs8D2tqM3iqkSfxPG+gGpKyTh1/nrdDmv\n\tEWkrNrWR1Ad6l2IZdwlGpY7enTPL2q9YrKLWOwcMlg0vIeR9FMBGHEoSwJj5DZDiu31N\n\tbu0/Ctsws6m1DGtSnj/2s9gMbFdGO8qEVX6lg7c93qYKtTkK1ZcTeqLhGeV6AnQsTnMZ\n\t7eRbbgLiDJkET/S5gYLM/lzk+OfwZs63hB0eAsiVYvBLMNGOO1LbjKNvJThvSnADtKsu\n\tGkaw==","X-Gm-Message-State":"AO0yUKVwc9HkuAYwwLyEd2PicClbasLezTqtaN8gAY6hyK4b6axUM9zs\n\tG3GRxAovi7Os44dzIhixAkolkMSpCHQyw/zDPSz94A==","X-Google-Smtp-Source":"AK7set8ACA1hO8DJ/3dwtTlfIxFNpEGcvsvOfM5kIOHi8i1hHJ7ViTXSHC+TtbX8OaNBa9YvrKEynQ==","X-Received":"by 2002:adf:ed83:0:b0:28b:ca44:641f with SMTP id\n\tc3-20020adfed83000000b0028bca44641fmr2947622wro.30.1675172810258; \n\tTue, 31 Jan 2023 05:46:50 -0800 (PST)","To":"libcamera-devel@lists.libcamera.org","Date":"Tue, 31 Jan 2023 13:46:46 +0000","Message-Id":"<20230131134646.9868-1-naush@raspberrypi.com>","X-Mailer":"git-send-email 2.25.1","In-Reply-To":"<20221213114836.15473-6-naush@raspberrypi.com>","References":"<20221213114836.15473-6-naush@raspberrypi.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH] ipa: raspberrypi: Normalise region sums\n\tto 16-bits","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>","From":"Naushir Patuck via libcamera-devel\n\t<libcamera-devel@lists.libcamera.org>","Reply-To":"Naushir Patuck <naush@raspberrypi.com>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"The VC4 ISP uses a pipeline bit-depth of 13-bits. The AGC algorithm needs to\nknow this bit-depth when computing the Y value for the image.\n\nInstead of hardcoding the VC4 bit-depth in the AGC source code, normalise all\nregion sums to 16-bits when filling the Statistics structure. AWB and ALSC are\nagnostic about pipeline depth, so do not need changing.\n\nSigned-off-by: Naushir Patuck <naush@raspberrypi.com>\nReviewed-by: David Plowman <david.plowman@raspberrypi.com>\n---\n src/ipa/raspberrypi/controller/rpi/agc.cpp | 11 +++++------\n src/ipa/raspberrypi/raspberrypi.cpp        | 18 ++++++++++++------\n src/ipa/raspberrypi/statistics.h           |  6 ++++++\n 3 files changed, 23 insertions(+), 12 deletions(-)","diff":"diff --git a/src/ipa/raspberrypi/controller/rpi/agc.cpp b/src/ipa/raspberrypi/controller/rpi/agc.cpp\nindex 868c30f03d66..ea0c82b5c4c8 100644\n--- a/src/ipa/raspberrypi/controller/rpi/agc.cpp\n+++ b/src/ipa/raspberrypi/controller/rpi/agc.cpp\n@@ -28,8 +28,6 @@ LOG_DEFINE_CATEGORY(RPiAgc)\n \n #define NAME \"rpi.agc\"\n \n-static constexpr unsigned int PipelineBits = 13; /* seems to be a 13-bit pipeline */\n-\n int AgcMeteringMode::read(const libcamera::YamlObject &params)\n {\n \tconst YamlObject &yamlWeights = params[\"weights\"];\n@@ -586,6 +584,7 @@ void Agc::fetchAwbStatus(Metadata *imageMetadata)\n static double computeInitialY(StatisticsPtr &stats, AwbStatus const &awb,\n \t\t\t      double weights[], double gain)\n {\n+\tconstexpr unsigned int maxVal = 1 << Statistics::NormalisationFactorPow2;\n \t/*\n \t * Note how the calculation below means that equal weights give you\n \t * \"average\" metering (i.e. all pixels equally important).\n@@ -593,9 +592,9 @@ static double computeInitialY(StatisticsPtr &stats, AwbStatus const &awb,\n \tdouble rSum = 0, gSum = 0, bSum = 0, pixelSum = 0;\n \tfor (unsigned int i = 0; i < stats->agcRegions.numRegions(); i++) {\n \t\tauto &region = stats->agcRegions.get(i);\n-\t\tdouble rAcc = std::min<double>(region.val.rSum * gain, ((1 << PipelineBits) - 1) * region.counted);\n-\t\tdouble gAcc = std::min<double>(region.val.gSum * gain, ((1 << PipelineBits) - 1) * region.counted);\n-\t\tdouble bAcc = std::min<double>(region.val.bSum * gain, ((1 << PipelineBits) - 1) * region.counted);\n+\t\tdouble rAcc = std::min<double>(region.val.rSum * gain, (maxVal - 1) * region.counted);\n+\t\tdouble gAcc = std::min<double>(region.val.gSum * gain, (maxVal - 1) * region.counted);\n+\t\tdouble bAcc = std::min<double>(region.val.bSum * gain, (maxVal - 1) * region.counted);\n \t\trSum += rAcc * weights[i];\n \t\tgSum += gAcc * weights[i];\n \t\tbSum += bAcc * weights[i];\n@@ -608,7 +607,7 @@ static double computeInitialY(StatisticsPtr &stats, AwbStatus const &awb,\n \tdouble ySum = rSum * awb.gainR * .299 +\n \t\t      gSum * awb.gainG * .587 +\n \t\t      bSum * awb.gainB * .114;\n-\treturn ySum / pixelSum / (1 << PipelineBits);\n+\treturn ySum / pixelSum / maxVal;\n }\n \n /*\ndiff --git a/src/ipa/raspberrypi/raspberrypi.cpp b/src/ipa/raspberrypi/raspberrypi.cpp\nindex cff079bbafb3..1cf7649c3303 100644\n--- a/src/ipa/raspberrypi/raspberrypi.cpp\n+++ b/src/ipa/raspberrypi/raspberrypi.cpp\n@@ -1154,11 +1154,17 @@ RPiController::StatisticsPtr IPARPi::fillStatistics(bcm2835_isp_stats *stats) co\n \t/* RGB histograms are not used, so do not populate them. */\n \tstatistics->yHist = std::move(RPiController::Histogram(stats->hist[0].g_hist, NUM_HISTOGRAM_BINS));\n \n+\t/*\n+\t * All region sums are based on a 13-bit pipeline bit-depth. Normalise\n+\t * this to 16-bits for the AGC/AWB/ALSC algorithms.\n+\t */\n+\tconstexpr unsigned int scale = Statistics::NormalisationFactorPow2 - 13;\n+\n \tstatistics->awbRegions.init({ DEFAULT_AWB_REGIONS_X, DEFAULT_AWB_REGIONS_Y });\n \tfor (i = 0; i < statistics->awbRegions.numRegions(); i++)\n-\t\tstatistics->awbRegions.set(i, { { stats->awb_stats[i].r_sum,\n-\t\t\t\t\t\t  stats->awb_stats[i].g_sum,\n-\t\t\t\t\t\t  stats->awb_stats[i].b_sum },\n+\t\tstatistics->awbRegions.set(i, { { stats->awb_stats[i].r_sum << scale,\n+\t\t\t\t\t\t  stats->awb_stats[i].g_sum << scale,\n+\t\t\t\t\t\t  stats->awb_stats[i].b_sum << scale },\n \t\t\t\t\t\tstats->awb_stats[i].counted,\n \t\t\t\t\t\tstats->awb_stats[i].notcounted });\n \n@@ -1168,9 +1174,9 @@ RPiController::StatisticsPtr IPARPi::fillStatistics(bcm2835_isp_stats *stats) co\n \t */\n \tstatistics->agcRegions.init(15);\n \tfor (i = 0; i < statistics->agcRegions.numRegions(); i++)\n-\t\tstatistics->agcRegions.set(i, { { stats->agc_stats[i].r_sum,\n-\t\t\t\t\t\t  stats->agc_stats[i].g_sum,\n-\t\t\t\t\t\t  stats->agc_stats[i].b_sum },\n+\t\tstatistics->agcRegions.set(i, { { stats->agc_stats[i].r_sum << scale,\n+\t\t\t\t\t\t  stats->agc_stats[i].g_sum << scale,\n+\t\t\t\t\t\t  stats->agc_stats[i].b_sum << scale },\n \t\t\t\t\t\tstats->agc_stats[i].counted,\n \t\t\t\t\t\tstats->awb_stats[i].notcounted });\n \ndiff --git a/src/ipa/raspberrypi/statistics.h b/src/ipa/raspberrypi/statistics.h\nindex affb7272c963..22a72c94a90d 100644\n--- a/src/ipa/raspberrypi/statistics.h\n+++ b/src/ipa/raspberrypi/statistics.h\n@@ -30,6 +30,12 @@ using RgbyRegions = RegionStats<RgbySums>;\n using FocusRegions = RegionStats<uint64_t>;\n \n struct Statistics {\n+\t/*\n+\t * All histogram and region based statistics are scaled to a maximum\n+\t * value given by (1 << NormalisationFactorPow2) - 1.\n+\t */\n+\tstatic constexpr unsigned int NormalisationFactorPow2 = 16;\n+\n \t/*\n \t * Positioning of the AGC statistics gathering in the pipeline:\n \t * Pre-WB correction or post-WB correction.\n","prefixes":["libcamera-devel"]}