ipa: rkisp1: agc: Wrap variable length C arrays in spans

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 <laurent.pinchart@ideasonboard.com>
Reviewed-by: Paul Elder <paul.elder@ideasonboard.com>
Reviewed-by: Stefan Klug <stefan.klug@ideasonboard.com>
This commit is contained in:
Laurent Pinchart 2024-02-16 20:54:37 +02:00
parent 366077c4db
commit 971c4904ff
2 changed files with 16 additions and 16 deletions

View file

@ -186,8 +186,8 @@ void Agc::prepare(IPAContext &context, const uint32_t frame,
/* Produce the luminance histogram. */ /* Produce the luminance histogram. */
params->meas.hst_config.mode = RKISP1_CIF_ISP_HISTOGRAM_MODE_Y_HISTOGRAM; params->meas.hst_config.mode = RKISP1_CIF_ISP_HISTOGRAM_MODE_Y_HISTOGRAM;
/* Set an average weighted histogram. */ /* Set an average weighted histogram. */
for (unsigned int histBin = 0; histBin < numHistBins_; histBin++) Span<uint8_t> weights{ params->meas.hst_config.hist_weight, numHistBins_ };
params->meas.hst_config.hist_weight[histBin] = 1; std::fill(weights.begin(), weights.end(), 1);
/* Step size can't be less than 3. */ /* Step size can't be less than 3. */
params->meas.hst_config.histogram_predivider = 4; 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 * \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 * \param[in] gain The gain to apply to the frame
* *
* This function estimates the average relative luminance of the frame that * 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 * \return The relative luminance
*/ */
double Agc::estimateLuminance(const rkisp1_cif_isp_ae_stat *ae, double Agc::estimateLuminance(Span<const uint8_t> expMeans, double gain)
double gain)
{ {
double ySum = 0.0; double ySum = 0.0;
/* Sum the averages, saturated to 255. */ /* Sum the averages, saturated to 255. */
for (unsigned int aeCell = 0; aeCell < numCells_; aeCell++) for (uint8_t expMean : expMeans)
ySum += std::min(ae->exp_mean[aeCell] * gain, 255.0); ySum += std::min(expMean * gain, 255.0);
/* \todo Weight with the AWB gains */ /* \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 * \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 * \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<const uint32_t> hist) const
{ {
Histogram histogram{ Span<const uint32_t>(hist->hist_bins, numHistBins_) }; Histogram histogram{ hist };
/* Estimate the quantile mean of the top 2% of the histogram. */ /* Estimate the quantile mean of the top 2% of the histogram. */
return histogram.interQuantileMean(0.98, 1.0); 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; const rkisp1_cif_isp_stat *params = &stats->params;
ASSERT(stats->meas_type & RKISP1_CIF_ISP_STAT_AUTOEXP); ASSERT(stats->meas_type & RKISP1_CIF_ISP_STAT_AUTOEXP);
const rkisp1_cif_isp_ae_stat *ae = &params->ae; Span<const uint8_t> ae{ params->ae.exp_mean, numCells_ };
const rkisp1_cif_isp_hist_stat *hist = &params->hist; Span<const uint32_t> hist{ params->hist.hist_bins, numHistBins_ };
double iqMean = measureBrightness(hist); 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 * Estimate the gain needed to achieve a relative luminance target. To

View file

@ -9,6 +9,7 @@
#include <linux/rkisp1-config.h> #include <linux/rkisp1-config.h>
#include <libcamera/base/span.h>
#include <libcamera/base/utils.h> #include <libcamera/base/utils.h>
#include <libcamera/geometry.h> #include <libcamera/geometry.h>
@ -42,8 +43,8 @@ private:
void computeExposure(IPAContext &Context, IPAFrameContext &frameContext, void computeExposure(IPAContext &Context, IPAFrameContext &frameContext,
double yGain, double iqMeanGain); double yGain, double iqMeanGain);
utils::Duration filterExposure(utils::Duration exposureValue); utils::Duration filterExposure(utils::Duration exposureValue);
double estimateLuminance(const rkisp1_cif_isp_ae_stat *ae, double gain); double estimateLuminance(Span<const uint8_t> expMeans, double gain);
double measureBrightness(const rkisp1_cif_isp_hist_stat *hist) const; double measureBrightness(Span<const uint32_t> hist) const;
void fillMetadata(IPAContext &context, IPAFrameContext &frameContext, void fillMetadata(IPAContext &context, IPAFrameContext &frameContext,
ControlList &metadata); ControlList &metadata);