@@ -31,6 +31,14 @@ struct SwIspStats {
* \brief Sums of colour channels of all the sampled pixels
*/
RGB<uint64_t> sum_;
+ /**
+ * \brief Number of sampled pixels that contributed to sum_
+ *
+ * Saturated pixels are left out of the sums, so this can be smaller
+ * than the number of pixels sampled. It is needed to subtract the black
+ * level from the sums, which is a per-pixel offset.
+ */
+ uint64_t sumSamples_;
/**
* \brief Number of bins in the yHistogram
*/
@@ -7,7 +7,6 @@
#include "awb.h"
-#include <numeric>
#include <stdint.h>
#include <libcamera/base/log.h>
@@ -118,16 +117,24 @@ SoftIspAwbStats Awb::calculateRgbMeans(IPAContext &context,
if (!stats->valid)
return {};
- const SwIspStats::Histogram &histogram = stats->yHistogram;
const uint8_t blackLevel = context.activeState.blc.level;
+ /*
+ * Saturated pixels are not counted in the sums, so the number of
+ * contributing pixels comes from the statistics rather than from the
+ * histogram, which counts every pixel sampled.
+ */
+ const uint64_t nPixels = stats->sumSamples_;
+
+ /* Nothing but saturation was sampled, so there is nothing to say. */
+ if (!nPixels)
+ return {};
+
/*
* Black level must be subtracted to get the correct AWB ratios, they
* would be off if they were computed from the whole brightness range
* rather than from the sensor range.
*/
- const uint64_t nPixels = std::accumulate(
- histogram.begin(), histogram.end(), uint64_t(0));
const uint64_t offset = blackLevel * nPixels;
const uint64_t minValid = 1;
@@ -167,6 +167,14 @@ SwStatsCpu::SwStatsCpu(const CameraManager &cm)
<< "Failed to create shared memory for statistics";
}
+/*
+ * Pixels with any colour channel at or above this value are treated as
+ * saturated. Fully saturated pixels don't represent the values for white
+ * balance computation correctly; nearly saturated pixels may provide inaccurate
+ * values from hardware.
+ */
+static constexpr unsigned int kSaturationThreshold = 248;
+
static constexpr unsigned int kRedYMul = 77; /* 0.299 * 256 */
static constexpr unsigned int kGreenYMul = 150; /* 0.587 * 256 */
static constexpr unsigned int kBlueYMul = 29; /* 0.114 * 256 */
@@ -177,22 +185,29 @@ static constexpr unsigned int kBlueYMul = 29; /* 0.114 * 256 */
\
uint64_t sumR = 0; \
uint64_t sumG = 0; \
- uint64_t sumB = 0;
-
-#define SWSTATS_ACCUMULATE_LINE_STATS(div) \
- sumR += r; \
- sumG += g; \
- sumB += b; \
- \
- yVal = r * kRedYMul; \
- yVal += g * kGreenYMul; \
- yVal += b * kBlueYMul; \
+ uint64_t sumB = 0; \
+ uint64_t nSamples = 0;
+
+#define SWSTATS_ACCUMULATE_LINE_STATS(div) \
+ if (r < kSaturationThreshold * (div) && \
+ g < kSaturationThreshold * (div) && \
+ b < kSaturationThreshold * (div)) { \
+ sumR += r; \
+ sumG += g; \
+ sumB += b; \
+ nSamples++; \
+ } \
+ \
+ yVal = r * kRedYMul; \
+ yVal += g * kGreenYMul; \
+ yVal += b * kBlueYMul; \
stats.yHistogram[yVal * SwIspStats::kYHistogramSize / (256 * 256 * (div))]++;
#define SWSTATS_FINISH_LINE_STATS() \
stats.sum_.r() += sumR; \
stats.sum_.g() += sumG; \
- stats.sum_.b() += sumB;
+ stats.sum_.b() += sumB; \
+ stats.sumSamples_ += nSamples;
void SwStatsCpu::statsBGGR8Line0(const uint8_t *src[], SwIspStats &stats)
{
@@ -391,6 +406,7 @@ void SwStatsCpu::startFrame(uint32_t frame)
for (auto &s : stats_) {
s.sum_ = RGB<uint64_t>({ 0, 0, 0 });
+ s.sumSamples_ = 0;
s.yHistogram.fill(0);
}
}
@@ -408,9 +424,11 @@ void SwStatsCpu::finishFrame(uint32_t frame, uint32_t bufferId)
if (valid) {
sharedStats_->sum_ = RGB<uint64_t>({ 0, 0, 0 });
+ sharedStats_->sumSamples_ = 0;
sharedStats_->yHistogram.fill(0);
for (const auto &s : stats_) {
sharedStats_->sum_ += s.sum_;
+ sharedStats_->sumSamples_ += s.sumSamples_;
for (unsigned int j = 0; j < SwIspStats::kYHistogramSize; j++)
sharedStats_->yHistogram[j] += s.yHistogram[j];
}