summaryrefslogtreecommitdiff
path: root/src/ipa/simple/soft_simple.cpp
diff options
context:
space:
mode:
authorMilan Zamazal <mzamazal@redhat.com>2024-09-27 15:46:20 +0200
committerKieran Bingham <kieran.bingham@ideasonboard.com>2024-09-27 15:01:57 +0100
commit7fab2062d8274065e870de20382720a0cdb11193 (patch)
tree127f9bdd5a462b94a333ab1fefa02978a1ba24b6 /src/ipa/simple/soft_simple.cpp
parent079d5f30ef324952c4c32876576cfbdcec0843b9 (diff)
libcamera: software_isp: Move color handling to an algorithm module
After black level handling has been moved to an algorithm module, white balance and the construction of color tables can be moved to algorithm modules too. This time, the moved code is split between stats processing and parameter construction methods. It is also split to two algorithm modules: - White balance computation. - Gamma table computation and color lookup tables construction. While this applies the color gains computed by the white balance algorithm, it is not directly related to white balance. And we may want to modify the color lookup tables in future according to other parameters than just gamma and white balance gains. Gamma table computation and color lookup tables construction could be split to separate algorithms too. But there is no big need for that now so they are kept together for simplicity. This is the only part of the software ISP algorithms that sets the parameters so emitting setIspParams can be moved to prepare() method. A more natural representation of the gains (and black level) would be floating point numbers. This is not done here in order to minimize changes in code movements. It will be addressed in a followup patch. Signed-off-by: Milan Zamazal <mzamazal@redhat.com> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com> Acked-by: Umang Jain <umang.jain@ideasonboard.com> Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
Diffstat (limited to 'src/ipa/simple/soft_simple.cpp')
-rw-r--r--src/ipa/simple/soft_simple.cpp64
1 files changed, 2 insertions, 62 deletions
diff --git a/src/ipa/simple/soft_simple.cpp b/src/ipa/simple/soft_simple.cpp
index 3332e2b1..f8d923c5 100644
--- a/src/ipa/simple/soft_simple.cpp
+++ b/src/ipa/simple/soft_simple.cpp
@@ -5,8 +5,6 @@
* Simple Software Image Processing Algorithm module
*/
-#include <cmath>
-#include <numeric>
#include <stdint.h>
#include <sys/mman.h>
@@ -93,9 +91,6 @@ private:
std::unique_ptr<CameraSensorHelper> camHelper_;
ControlInfoMap sensorInfoMap_;
- static constexpr unsigned int kGammaLookupSize = 1024;
- std::array<uint8_t, kGammaLookupSize> gammaTable_;
- int lastBlackLevel_ = -1;
/* Local parameter storage */
struct IPAContext context_;
@@ -283,6 +278,7 @@ void IPASoftSimple::fillParamsBuffer(const uint32_t frame)
IPAFrameContext &frameContext = context_.frameContexts.get(frame);
for (auto const &algo : algorithms())
algo->prepare(context_, frame, frameContext, params_);
+ setIspParams.emit();
}
void IPASoftSimple::processStats(const uint32_t frame,
@@ -300,62 +296,6 @@ void IPASoftSimple::processStats(const uint32_t frame,
for (auto const &algo : algorithms())
algo->process(context_, frame, frameContext, stats_, metadata);
- SwIspStats::Histogram histogram = stats_->yHistogram;
- const uint8_t blackLevel = context_.activeState.blc.level;
-
- /*
- * 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(), 0);
- const uint64_t offset = blackLevel * nPixels;
- const uint64_t sumR = stats_->sumR_ - offset / 4;
- const uint64_t sumG = stats_->sumG_ - offset / 2;
- const uint64_t sumB = stats_->sumB_ - offset / 4;
-
- /*
- * Calculate red and blue gains for AWB.
- * Clamp max gain at 4.0, this also avoids 0 division.
- * Gain: 128 = 0.5, 256 = 1.0, 512 = 2.0, etc.
- */
- const unsigned int gainR = sumR <= sumG / 4 ? 1024 : 256 * sumG / sumR;
- const unsigned int gainB = sumB <= sumG / 4 ? 1024 : 256 * sumG / sumB;
- /* Green gain and gamma values are fixed */
- constexpr unsigned int gainG = 256;
-
- /* Update the gamma table if needed */
- if (blackLevel != lastBlackLevel_) {
- constexpr float gamma = 0.5;
- const unsigned int blackIndex = blackLevel * kGammaLookupSize / 256;
- std::fill(gammaTable_.begin(), gammaTable_.begin() + blackIndex, 0);
- const float divisor = kGammaLookupSize - blackIndex - 1.0;
- for (unsigned int i = blackIndex; i < kGammaLookupSize; i++)
- gammaTable_[i] = UINT8_MAX *
- std::pow((i - blackIndex) / divisor, gamma);
-
- lastBlackLevel_ = blackLevel;
- }
-
- for (unsigned int i = 0; i < DebayerParams::kRGBLookupSize; i++) {
- constexpr unsigned int div =
- DebayerParams::kRGBLookupSize * 256 / kGammaLookupSize;
- unsigned int idx;
-
- /* Apply gamma after gain! */
- idx = std::min({ i * gainR / div, (kGammaLookupSize - 1) });
- params_->red[i] = gammaTable_[idx];
-
- idx = std::min({ i * gainG / div, (kGammaLookupSize - 1) });
- params_->green[i] = gammaTable_[idx];
-
- idx = std::min({ i * gainB / div, (kGammaLookupSize - 1) });
- params_->blue[i] = gammaTable_[idx];
- }
-
- setIspParams.emit();
-
/* \todo Switch to the libipa/algorithm.h API someday. */
/*
@@ -372,6 +312,7 @@ void IPASoftSimple::processStats(const uint32_t frame,
* Calculate Mean Sample Value (MSV) according to formula from:
* https://www.araa.asn.au/acra/acra2007/papers/paper84final.pdf
*/
+ const uint8_t blackLevel = context_.activeState.blc.level;
const unsigned int blackLevelHistIdx =
blackLevel / (256 / SwIspStats::kYHistogramSize);
const unsigned int histogramSize =
@@ -421,7 +362,6 @@ void IPASoftSimple::processStats(const uint32_t frame,
LOG(IPASoft, Debug) << "exposureMSV " << exposureMSV
<< " exp " << exposure_ << " again " << again_
- << " gain R/B " << gainR << "/" << gainB
<< " black level " << static_cast<unsigned int>(blackLevel);
}