From patchwork Sun Feb 18 16:49:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 19506 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 B13D5C3261 for ; Sun, 18 Feb 2024 16:49:08 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 4BD8C6280B; Sun, 18 Feb 2024 17:49:08 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="CgvWHKxa"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 6CDF162805 for ; Sun, 18 Feb 2024 17:49:06 +0100 (CET) Received: from pendragon.ideasonboard.com (89-27-53-110.bb.dnainternet.fi [89.27.53.110]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 755D3975; Sun, 18 Feb 2024 17:49:00 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1708274940; bh=ekL3UFx/W8KHH4sICYYS0S9s/wuLgXxMVSzzEZkPxAw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=CgvWHKxaJjbwICQESEV8PJVzD63AYIIGZtojiMuJ0QqmPUXo+msDo3Q9UPUDHaYLQ lhixDtoPHF/vH/oPQoPF+DFBz4Swpo6t8jDr1dUlsXr8pruxEmwp+AaaSYnK7cO745 CZO7CyJLfshzZbE+Bp/lyBlWDc2sPIClwfLt6MRI= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Subject: [PATCH v3 1/5] ipa: rkisp1: agc: Wrap variable length C arrays in spans Date: Sun, 18 Feb 2024 18:49:04 +0200 Message-ID: <20240218164908.15921-2-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240218164908.15921-1-laurent.pinchart@ideasonboard.com> References: <20240218164908.15921-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 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: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" The RkISP1 statistics structure contains multiple arrays whose length varies depending on the hardware revision. Accessing those arrays is error-prone, wrap them in spans at the top level to reduce risks of out-of-bound accesses. Signed-off-by: Laurent Pinchart Reviewed-by: Paul Elder Reviewed-by: Stefan Klug --- src/ipa/rkisp1/algorithms/agc.cpp | 27 +++++++++++++-------------- src/ipa/rkisp1/algorithms/agc.h | 5 +++-- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/ipa/rkisp1/algorithms/agc.cpp b/src/ipa/rkisp1/algorithms/agc.cpp index e5aeb3426eff..f83a9ba1686a 100644 --- a/src/ipa/rkisp1/algorithms/agc.cpp +++ b/src/ipa/rkisp1/algorithms/agc.cpp @@ -186,8 +186,8 @@ void Agc::prepare(IPAContext &context, const uint32_t frame, /* Produce the luminance histogram. */ params->meas.hst_config.mode = RKISP1_CIF_ISP_HISTOGRAM_MODE_Y_HISTOGRAM; /* Set an average weighted histogram. */ - for (unsigned int histBin = 0; histBin < numHistBins_; histBin++) - params->meas.hst_config.hist_weight[histBin] = 1; + Span weights{ params->meas.hst_config.hist_weight, numHistBins_ }; + std::fill(weights.begin(), weights.end(), 1); /* Step size can't be less than 3. */ params->meas.hst_config.histogram_predivider = 4; @@ -318,7 +318,7 @@ void Agc::computeExposure(IPAContext &context, IPAFrameContext &frameContext, /** * \brief Estimate the relative luminance of the frame with a given gain - * \param[in] ae The RkISP1 statistics and ISP results + * \param[in] expMeans The mean luminance values, from the RkISP1 statistics * \param[in] gain The gain to apply to the frame * * This function estimates the average relative luminance of the frame that @@ -342,28 +342,27 @@ void Agc::computeExposure(IPAContext &context, IPAFrameContext &frameContext, * * \return The relative luminance */ -double Agc::estimateLuminance(const rkisp1_cif_isp_ae_stat *ae, - double gain) +double Agc::estimateLuminance(Span expMeans, double gain) { double ySum = 0.0; /* Sum the averages, saturated to 255. */ - for (unsigned int aeCell = 0; aeCell < numCells_; aeCell++) - ySum += std::min(ae->exp_mean[aeCell] * gain, 255.0); + for (uint8_t expMean : expMeans) + ySum += std::min(expMean * gain, 255.0); /* \todo Weight with the AWB gains */ - return ySum / numCells_ / 255; + return ySum / expMeans.size() / 255; } /** * \brief Estimate the mean value of the top 2% of the histogram - * \param[in] hist The histogram statistics computed by the ImgU + * \param[in] hist The histogram statistics computed by the RkISP1 * \return The mean value of the top 2% of the histogram */ -double Agc::measureBrightness(const rkisp1_cif_isp_hist_stat *hist) const +double Agc::measureBrightness(Span hist) const { - Histogram histogram{ Span(hist->hist_bins, numHistBins_) }; + Histogram histogram{ hist }; /* Estimate the quantile mean of the top 2% of the histogram. */ return histogram.interQuantileMean(0.98, 1.0); } @@ -415,11 +414,11 @@ void Agc::process(IPAContext &context, [[maybe_unused]] const uint32_t frame, const rkisp1_cif_isp_stat *params = &stats->params; ASSERT(stats->meas_type & RKISP1_CIF_ISP_STAT_AUTOEXP); - const rkisp1_cif_isp_ae_stat *ae = ¶ms->ae; - const rkisp1_cif_isp_hist_stat *hist = ¶ms->hist; + Span ae{ params->ae.exp_mean, numCells_ }; + Span hist{ params->hist.hist_bins, numHistBins_ }; double iqMean = measureBrightness(hist); - double iqMeanGain = kEvGainTarget * numHistBins_ / iqMean; + double iqMeanGain = kEvGainTarget * hist.size() / iqMean; /* * Estimate the gain needed to achieve a relative luminance target. To diff --git a/src/ipa/rkisp1/algorithms/agc.h b/src/ipa/rkisp1/algorithms/agc.h index 8a22263741b6..ce8594f393ab 100644 --- a/src/ipa/rkisp1/algorithms/agc.h +++ b/src/ipa/rkisp1/algorithms/agc.h @@ -9,6 +9,7 @@ #include +#include #include #include @@ -42,8 +43,8 @@ private: void computeExposure(IPAContext &Context, IPAFrameContext &frameContext, double yGain, double iqMeanGain); utils::Duration filterExposure(utils::Duration exposureValue); - double estimateLuminance(const rkisp1_cif_isp_ae_stat *ae, double gain); - double measureBrightness(const rkisp1_cif_isp_hist_stat *hist) const; + double estimateLuminance(Span expMeans, double gain); + double measureBrightness(Span hist) const; void fillMetadata(IPAContext &context, IPAFrameContext &frameContext, ControlList &metadata);