{"id":18595,"url":"https://patchwork.libcamera.org/api/patches/18595/?format=json","web_url":"https://patchwork.libcamera.org/patch/18595/","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":"<20230503122035.32026-11-naush@raspberrypi.com>","date":"2023-05-03T12:20:32","name":"[libcamera-devel,10/13] ipa: raspberrypi: agc: Move weights out of AGC","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"147694391131f001ae09d3b4dfa2d3931e3f651b","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/18595/mbox/","series":[{"id":3863,"url":"https://patchwork.libcamera.org/api/series/3863/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=3863","date":"2023-05-03T12:20:22","name":"Raspberry Pi: Code refactoring","version":1,"mbox":"https://patchwork.libcamera.org/series/3863/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/18595/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/18595/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 2D612C0DA4\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed,  3 May 2023 12:21:03 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id DD33B633BC;\n\tWed,  3 May 2023 14:21:02 +0200 (CEST)","from mail-wm1-x32a.google.com (mail-wm1-x32a.google.com\n\t[IPv6:2a00:1450:4864:20::32a])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 954F5627DE\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed,  3 May 2023 14:21:01 +0200 (CEST)","by mail-wm1-x32a.google.com with SMTP id\n\t5b1f17b1804b1-3f19a7f9424so50264815e9.2\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 03 May 2023 05:21:01 -0700 (PDT)","from localhost.localdomain ([93.93.133.154])\n\tby smtp.gmail.com with ESMTPSA id\n\tf23-20020a7bcd17000000b003ee443bf0c7sm1736785wmj.16.2023.05.03.05.21.00\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tWed, 03 May 2023 05:21:00 -0700 (PDT)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1683116462;\n\tbh=r8OWRW4exBKkEoNUxF9rW+e27c6/Jmh3844KlNQPoEo=;\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:Cc:\n\tFrom;\n\tb=xpCmzxBjJqkuhAdB4aUxcI/ia5ioeD44xbkOhTETt0diSPNurK69XJH6ecuRDymF/\n\tT8OHui9XbGrHr3ydcyhn5C7x2ZwPJ73Tq+4JSUIQqcwzEzWQ5PNdDGgeH/P3SZBsYT\n\tbXDmwo/6bWycBLgKy/TGB/zfp8HK+3VquDuXakAn2f1HOua0jreVuo1lIluah/evvC\n\tj5RJISg73bV481SpiUBAtq7guBUYrOT0C9V/PHclqMEZEaNKJdHF7C4xya10wjHfbn\n\tPfBySb2y8fAIaSX4j2X0v8qpWL9bFVNZzF76+JfGQab6BKSkCfVYeU47SgAP+dYfBa\n\tsCXYLnc5nY+wQ==","v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=raspberrypi.com; s=google; t=1683116461; x=1685708461;\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=K+6m0TmzUBQDH0KaQ5mGecYbrYCVSMAcwOdAkyRWqrM=;\n\tb=HQ+sQKKcW2e/n5CeBtb1x+gtlQZio6sCQN9GJtQ9DVOnqE3GL4dw5Vuo3c528gNSFR\n\tHfVYF/DT3rf2wd/wOQprhqIrDmpAvCLrhrW6Xl6g8hFwnxhbMD0xcEI66j+ezb86iZnL\n\t5V8WEIQEvTTqNiGWn6YwVYPbUVxeiwu6xPGQ4hMW5Ile6eIwpWtIeu6pu92Uc3uJT+Lt\n\t2989A8zgs+4MXvLOnThRZxUKAdBen096eJTvZ2UoKLQ/QYejdTuQaCy1Qp3D214DNCD8\n\tp+rGfXb0GDD8wZZ/BzCcuwS4VlQkmtS8UcplnXR2/yVDTaJ5baULGX/b6SXouL4SfXWo\n\tlelQ=="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (2048-bit key; \n\tunprotected) header.d=raspberrypi.com\n\theader.i=@raspberrypi.com\n\theader.b=\"HQ+sQKKc\"; dkim-atps=neutral","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20221208; t=1683116461; x=1685708461;\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=K+6m0TmzUBQDH0KaQ5mGecYbrYCVSMAcwOdAkyRWqrM=;\n\tb=g9fP+1qtLaRbLuIyQOlZU/2LPv57FUE78yS2klU+1EBSF0/VoWhAHP8/0l8CipbDVL\n\tbDYxdekURNGmjzBFkFUhP1YFg4i4v/Ew2FV0mL90UBsJFfTR925+dO1OmCH9KEIRY16D\n\tFyBlS9esyof1oCm6iAZykASAN3dlvuihou2V3MXVU/07Y9HBvVB+jpBqQfGz9A4vZeoM\n\tWe4BJDPRVzuhcO9NIbATcKVSaQSmre7TsVuD9te+k0pr5Xs/RpKuHzVH8C/24KfJ2esL\n\tdkDs3F1Ixpp1guqf57+CHRUbJKrElM67xiJxFkhj7skwctEnEnNvrjIgfMh5oOW0rDBD\n\t4SOQ==","X-Gm-Message-State":"AC+VfDzjQs11NRBIBaMIjsFnrlO/J/gEFx+9hJ+EKv4TH6mEaHFveA0R\n\tKlVTUW8yU1M5V0JUwKnu/ba6CslGfQBvM/RGTdg2iQ==","X-Google-Smtp-Source":"ACHHUZ7R3Htwphvu97nPFyVpNGQQ89doJVZRhl/ibzJsDId01DcyObENuvdH9EWfWTYfgDvqgitnfg==","X-Received":"by 2002:a1c:f305:0:b0:3f1:93c2:4df7 with SMTP id\n\tq5-20020a1cf305000000b003f193c24df7mr14649326wmq.5.1683116460791; \n\tWed, 03 May 2023 05:21:00 -0700 (PDT)","To":"libcamera-devel@lists.libcamera.org","Date":"Wed,  3 May 2023 13:20:32 +0100","Message-Id":"<20230503122035.32026-11-naush@raspberrypi.com>","X-Mailer":"git-send-email 2.34.1","In-Reply-To":"<20230503122035.32026-1-naush@raspberrypi.com>","References":"<20230503122035.32026-1-naush@raspberrypi.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH 10/13] ipa: raspberrypi: agc: Move weights\n\tout of AGC","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>","Cc":"Jacopo Mondi <jacopo.mondi@ideasonboard.com>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"From: David Plowman <david.plowman@raspberrypi.com>\n\nThe region weights for the the AGC zones are handled by the AGC\nalgorithm. Apply them directly in the IPA (vc4.cpp) to the statistics\nthat we pass to the AGC.\n\nSigned-off-by: David Plowman <david.plowman@raspberrypi>\nSigned-off-by: Naushir Patuck <naush@raspberrypi.com>\nReviewed-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>\n---\n src/ipa/rpi/controller/agc_algorithm.h |  3 +++\n src/ipa/rpi/controller/rpi/agc.cpp     | 27 +++++++++++++++++---------\n src/ipa/rpi/controller/rpi/agc.h       |  1 +\n src/ipa/rpi/vc4/vc4.cpp                | 26 ++++++++++++++++++-------\n 4 files changed, 41 insertions(+), 16 deletions(-)","diff":"diff --git a/src/ipa/rpi/controller/agc_algorithm.h b/src/ipa/rpi/controller/agc_algorithm.h\nindex 36e6c11058ee..b6949daa7135 100644\n--- a/src/ipa/rpi/controller/agc_algorithm.h\n+++ b/src/ipa/rpi/controller/agc_algorithm.h\n@@ -6,6 +6,8 @@\n  */\n #pragma once\n \n+#include <vector>\n+\n #include <libcamera/base/utils.h>\n \n #include \"algorithm.h\"\n@@ -18,6 +20,7 @@ public:\n \tAgcAlgorithm(Controller *controller) : Algorithm(controller) {}\n \t/* An AGC algorithm must provide the following: */\n \tvirtual unsigned int getConvergenceFrames() const = 0;\n+\tvirtual std::vector<double> const &getWeights() const = 0;\n \tvirtual void setEv(double ev) = 0;\n \tvirtual void setFlickerPeriod(libcamera::utils::Duration flickerPeriod) = 0;\n \tvirtual void setFixedShutter(libcamera::utils::Duration fixedShutter) = 0;\ndiff --git a/src/ipa/rpi/controller/rpi/agc.cpp b/src/ipa/rpi/controller/rpi/agc.cpp\nindex e6fb7b8dbeb3..e79c82e2e65b 100644\n--- a/src/ipa/rpi/controller/rpi/agc.cpp\n+++ b/src/ipa/rpi/controller/rpi/agc.cpp\n@@ -292,6 +292,18 @@ unsigned int Agc::getConvergenceFrames() const\n \t\treturn config_.convergenceFrames;\n }\n \n+std::vector<double> const &Agc::getWeights() const\n+{\n+\t/*\n+\t * In case someone calls setMeteringMode and then this before the\n+\t * algorithm has run and updated the meteringMode_ pointer.\n+\t */\n+\tauto it = config_.meteringModes.find(meteringModeName_);\n+\tif (it == config_.meteringModes.end())\n+\t\treturn meteringMode_->weights;\n+\treturn it->second.weights;\n+}\n+\n void Agc::setEv(double ev)\n {\n \tev_ = ev;\n@@ -595,19 +607,16 @@ static double computeInitialY(StatisticsPtr &stats, AwbStatus const &awb,\n \tASSERT(weights.size() == stats->agcRegions.numRegions());\n \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+\t * Note that the weights are applied by the IPA to the statistics directly,\n+\t * before they are given to us here.\n \t */\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, (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-\t\tpixelSum += region.counted * weights[i];\n+\t\trSum += std::min<double>(region.val.rSum * gain, (maxVal - 1) * region.counted);\n+\t\tgSum += std::min<double>(region.val.gSum * gain, (maxVal - 1) * region.counted);\n+\t\tbSum += std::min<double>(region.val.bSum * gain, (maxVal - 1) * region.counted);\n+\t\tpixelSum += region.counted;\n \t}\n \tif (pixelSum == 0.0) {\n \t\tLOG(RPiAgc, Warning) << \"computeInitialY: pixelSum is zero\";\ndiff --git a/src/ipa/rpi/controller/rpi/agc.h b/src/ipa/rpi/controller/rpi/agc.h\nindex 4e5f272fac78..939f97295a02 100644\n--- a/src/ipa/rpi/controller/rpi/agc.h\n+++ b/src/ipa/rpi/controller/rpi/agc.h\n@@ -69,6 +69,7 @@ public:\n \tchar const *name() const override;\n \tint read(const libcamera::YamlObject &params) override;\n \tunsigned int getConvergenceFrames() const override;\n+\tstd::vector<double> const &getWeights() const override;\n \tvoid setEv(double ev) override;\n \tvoid setFlickerPeriod(libcamera::utils::Duration flickerPeriod) override;\n \tvoid setMaxShutter(libcamera::utils::Duration maxShutter) override;\ndiff --git a/src/ipa/rpi/vc4/vc4.cpp b/src/ipa/rpi/vc4/vc4.cpp\nindex a32db9bcaf9a..e38024ac5418 100644\n--- a/src/ipa/rpi/vc4/vc4.cpp\n+++ b/src/ipa/rpi/vc4/vc4.cpp\n@@ -211,13 +211,25 @@ RPiController::StatisticsPtr IpaVc4::platformProcessStats(Span<uint8_t> mem)\n \t\t\t\t\t\tstats->awb_stats[i].counted,\n \t\t\t\t\t\tstats->awb_stats[i].notcounted });\n \n-\tstatistics->agcRegions.init(hw.agcRegions);\n-\tfor (i = 0; i < statistics->agcRegions.numRegions(); i++)\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+\tRPiController::AgcAlgorithm *agc = dynamic_cast<RPiController::AgcAlgorithm *>(\n+\t\tcontroller_.getAlgorithm(\"agc\"));\n+\tif (!agc) {\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\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+\t\t\tuint64_t gSum = (stats->agc_stats[i].g_sum << scale) * weights[i];\n+\t\t\tuint64_t bSum = (stats->agc_stats[i].b_sum << scale) * weights[i];\n+\t\t\tuint32_t counted = stats->agc_stats[i].counted * weights[i];\n+\t\t\tuint32_t notcounted = stats->agc_stats[i].notcounted * weights[i];\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+\t\t}\n+\t}\n \n \tstatistics->focusRegions.init(hw.focusRegions);\n \tfor (i = 0; i < statistics->focusRegions.numRegions(); i++)\n","prefixes":["libcamera-devel","10/13"]}