From patchwork Wed Apr 26 13:10:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Naushir Patuck X-Patchwork-Id: 18557 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id BE6D4BDCBD for ; Wed, 26 Apr 2023 13:13:40 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 8B4C6627DF; Wed, 26 Apr 2023 15:13:40 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1682514820; bh=zMnuCMQzW5A9CsO1NyiO6bJ4PVWkLOb/UvsOjK0kGek=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=JCScAaSAHwnm+FgW0wCkJRUY8NIipGeSvcpc9sQ6eAWVasYszw3c0tvOHs79DD5wW +gCTQIoraPNos4xh6uVhnTnmG2R2MvgSqTSLrMdrxudRh2aQAlI1mB+VIfdfvwep2w 6hxfRJqav28WjX1MD8M0bS+s+KVL9tynuDoNufAFZ0zNh+czBNQHZ/mfBWmu8P3mJl Wsi412yR/hakDQqGW4ujishEoAVPUCUTyk+TUnrW3o6NZLci2aSBqSbHdLkoafCoi6 86C/zU6KwF29sa0PNe2lQaqf3Gwbys1SSgC1VCAAMWFObOEs5/2v+n7JKYZ42A+pgb qVvsaPl4HgF8Q== Received: from mail-wm1-x32f.google.com (mail-wm1-x32f.google.com [IPv6:2a00:1450:4864:20::32f]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 7F440627D4 for ; Wed, 26 Apr 2023 15:13:38 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (2048-bit key; unprotected) header.d=raspberrypi.com header.i=@raspberrypi.com header.b="c5P0ORXU"; dkim-atps=neutral Received: by mail-wm1-x32f.google.com with SMTP id 5b1f17b1804b1-3f19a80a330so30979845e9.2 for ; Wed, 26 Apr 2023 06:13:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=raspberrypi.com; s=google; t=1682514818; x=1685106818; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=vY50V2GR0AO2EgoMd7bJCL30YLCQYhdChGBDbVstcds=; b=c5P0ORXUdV2ZBfFNa18U+wWn5j7SKFMLBjEKDxuzwgOUHYTrS7HvnDPGgZP/EEPi9s 41HNpFXBX3a9rnYe2joLCAYGhVyFYFP/vB1eU+D3jX74UU+IYq9o0U0/wTDYGS6t4h/Z f2s0YDq41hfePl2t4tG55BlBEM/blTDgSKMjYc4S9jQJvdRrd2kQ5up4fQK57iX/oQjw bFyCSm4L7otZgGLIoaZqENWFNFX1vAnofVfo3FuaR9TlE18NkyLm0K8z3mcuusI6GyeT SnxiUlKXYN61fDtT2PUxi6/fRKuvKe9gRXCh1tFmlGxT/gcz5kpP43egEnBtKVSbos6n t+qA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682514818; x=1685106818; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=vY50V2GR0AO2EgoMd7bJCL30YLCQYhdChGBDbVstcds=; b=kBD9tQZX291Bq394ah3gD7gqv3L38BzwFF7O4vapI4eMMhKN7kat5cyZZwC+GMgDE7 ehD2KDGBP7W/2z2vv5+rbGG/h5B+X7YvFu+zemfeyc73z12f1oz9v42xG9seIlQjSuuN 5z7cVaash+24Qpl3fkMpZ5PCNsaf3+KM10///eal7AIaQF9OMWKyr54BoTO2dsgppSOG scDTKFaD38nsrbHaKYT/IMcKOg5qGhcqmSD3hn64JnlwDTX5Qc7ukSXIL90lg00rPR7v 1bqV/ejKk6qEbj1MmDHHqWr4dkj0Ju4X/BHD67U7JI+92vWwXOKQKa/hBieIJJERZywq BRlQ== X-Gm-Message-State: AAQBX9c9y52atWIShVJgKrU8ZJhL7lwYk14GuA4iQlmiDzDM87PSi9eQ xqHk35kW66kitaUNp3dHhSdL6+7DSrBLY3OMKSXSrQ== X-Google-Smtp-Source: AKy350bXDvIGNYrqRVxDWIjARAuxsBchkgMfn7p46JGzsjaVVWv5Amf+qMbdXAsPKxmiezDk5S73Fw== X-Received: by 2002:a7b:cb96:0:b0:3f0:5519:9049 with SMTP id m22-20020a7bcb96000000b003f055199049mr13944098wmi.8.1682514817672; Wed, 26 Apr 2023 06:13:37 -0700 (PDT) Received: from localhost.localdomain ([93.93.133.154]) by smtp.gmail.com with ESMTPSA id k5-20020adff5c5000000b002f103ca90cdsm15780949wrp.101.2023.04.26.06.13.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 26 Apr 2023 06:13:37 -0700 (PDT) To: libcamera-devel@lists.libcamera.org Date: Wed, 26 Apr 2023 14:10:53 +0100 Message-Id: <20230426131057.21550-10-naush@raspberrypi.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230426131057.21550-1-naush@raspberrypi.com> References: <20230426131057.21550-1-naush@raspberrypi.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH 09/13] ipa: raspberrypi: agc: Move weights out of AGC X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Naushir Patuck via libcamera-devel From: Naushir Patuck Reply-To: Naushir Patuck Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" From: David Plowman The region weights for the the AGC zones were previously handled by the AGC algorithm. Now it's the IPA (vc4.cpp) that applies them directly to the statistics that we pass to the AGC. Signed-off-by: David Plowman Signed-off-by: Naushir Patuck Reviewed-by: Jacopo Mondi --- src/ipa/rpi/controller/agc_algorithm.h | 3 +++ src/ipa/rpi/controller/rpi/agc.cpp | 27 +++++++++++++++++--------- src/ipa/rpi/controller/rpi/agc.h | 1 + src/ipa/rpi/vc4/vc4.cpp | 26 ++++++++++++++++++------- 4 files changed, 41 insertions(+), 16 deletions(-) diff --git a/src/ipa/rpi/controller/agc_algorithm.h b/src/ipa/rpi/controller/agc_algorithm.h index 36e6c11058ee..b6949daa7135 100644 --- a/src/ipa/rpi/controller/agc_algorithm.h +++ b/src/ipa/rpi/controller/agc_algorithm.h @@ -6,6 +6,8 @@ */ #pragma once +#include + #include #include "algorithm.h" @@ -18,6 +20,7 @@ public: AgcAlgorithm(Controller *controller) : Algorithm(controller) {} /* An AGC algorithm must provide the following: */ virtual unsigned int getConvergenceFrames() const = 0; + virtual std::vector const &getWeights() const = 0; virtual void setEv(double ev) = 0; virtual void setFlickerPeriod(libcamera::utils::Duration flickerPeriod) = 0; virtual void setFixedShutter(libcamera::utils::Duration fixedShutter) = 0; diff --git a/src/ipa/rpi/controller/rpi/agc.cpp b/src/ipa/rpi/controller/rpi/agc.cpp index e6fb7b8dbeb3..e79c82e2e65b 100644 --- a/src/ipa/rpi/controller/rpi/agc.cpp +++ b/src/ipa/rpi/controller/rpi/agc.cpp @@ -292,6 +292,18 @@ unsigned int Agc::getConvergenceFrames() const return config_.convergenceFrames; } +std::vector const &Agc::getWeights() const +{ + /* + * In case someone calls setMeteringMode and then this before the + * algorithm has run and updated the meteringMode_ pointer. + */ + auto it = config_.meteringModes.find(meteringModeName_); + if (it == config_.meteringModes.end()) + return meteringMode_->weights; + return it->second.weights; +} + void Agc::setEv(double ev) { ev_ = ev; @@ -595,19 +607,16 @@ static double computeInitialY(StatisticsPtr &stats, AwbStatus const &awb, ASSERT(weights.size() == stats->agcRegions.numRegions()); /* - * Note how the calculation below means that equal weights give you - * "average" metering (i.e. all pixels equally important). + * Note that the weights are applied by the IPA to the statistics directly, + * before they are given to us here. */ double rSum = 0, gSum = 0, bSum = 0, pixelSum = 0; for (unsigned int i = 0; i < stats->agcRegions.numRegions(); i++) { auto ®ion = stats->agcRegions.get(i); - double rAcc = std::min(region.val.rSum * gain, (maxVal - 1) * region.counted); - double gAcc = std::min(region.val.gSum * gain, (maxVal - 1) * region.counted); - double bAcc = std::min(region.val.bSum * gain, (maxVal - 1) * region.counted); - rSum += rAcc * weights[i]; - gSum += gAcc * weights[i]; - bSum += bAcc * weights[i]; - pixelSum += region.counted * weights[i]; + rSum += std::min(region.val.rSum * gain, (maxVal - 1) * region.counted); + gSum += std::min(region.val.gSum * gain, (maxVal - 1) * region.counted); + bSum += std::min(region.val.bSum * gain, (maxVal - 1) * region.counted); + pixelSum += region.counted; } if (pixelSum == 0.0) { LOG(RPiAgc, Warning) << "computeInitialY: pixelSum is zero"; diff --git a/src/ipa/rpi/controller/rpi/agc.h b/src/ipa/rpi/controller/rpi/agc.h index 4e5f272fac78..939f97295a02 100644 --- a/src/ipa/rpi/controller/rpi/agc.h +++ b/src/ipa/rpi/controller/rpi/agc.h @@ -69,6 +69,7 @@ public: char const *name() const override; int read(const libcamera::YamlObject ¶ms) override; unsigned int getConvergenceFrames() const override; + std::vector const &getWeights() const override; void setEv(double ev) override; void setFlickerPeriod(libcamera::utils::Duration flickerPeriod) override; void setMaxShutter(libcamera::utils::Duration maxShutter) override; diff --git a/src/ipa/rpi/vc4/vc4.cpp b/src/ipa/rpi/vc4/vc4.cpp index 0d929cda6c4a..0e022c2aeed3 100644 --- a/src/ipa/rpi/vc4/vc4.cpp +++ b/src/ipa/rpi/vc4/vc4.cpp @@ -211,13 +211,25 @@ RPiController::StatisticsPtr IpaVc4::platformProcessStats(Span mem) stats->awb_stats[i].counted, stats->awb_stats[i].notcounted }); - statistics->agcRegions.init(hw.agcRegions); - for (i = 0; i < statistics->agcRegions.numRegions(); i++) - statistics->agcRegions.set(i, { { stats->agc_stats[i].r_sum << scale, - stats->agc_stats[i].g_sum << scale, - stats->agc_stats[i].b_sum << scale }, - stats->agc_stats[i].counted, - stats->awb_stats[i].notcounted }); + RPiController::AgcAlgorithm *agc = dynamic_cast( + controller_.getAlgorithm("agc")); + if (!agc) { + LOG(IPARPI, Debug) << "No AGC algorithm - not copying statistics"; + statistics->agcRegions.init(0); + } else { + statistics->agcRegions.init(hw.agcRegions); + const std::vector &weights = agc->getWeights(); + for (i = 0; i < statistics->agcRegions.numRegions(); i++) { + uint64_t rSum = (stats->agc_stats[i].r_sum << scale) * weights[i]; + uint64_t gSum = (stats->agc_stats[i].g_sum << scale) * weights[i]; + uint64_t bSum = (stats->agc_stats[i].b_sum << scale) * weights[i]; + uint32_t counted = stats->agc_stats[i].counted * weights[i]; + uint32_t notcounted = stats->agc_stats[i].notcounted * weights[i]; + statistics->agcRegions.set(i, { { rSum, gSum, bSum }, + counted, + notcounted }); + } + } statistics->focusRegions.init(hw.focusRegions); for (i = 0; i < statistics->focusRegions.numRegions(); i++)