ipa: rkisp1: Add support of Lens Shading Correction control

The Lens Shading Correction algorithm applies multipliers to all pixels
to compensate for the lens shading effect. The coefficients are
specified in a downscaled table in the YAML tuning file.

Signed-off-by: Florian Sylvestre <fsylvestre@baylibre.com>
Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
This commit is contained in:
Florian Sylvestre 2022-07-27 10:40:50 +02:00 committed by Laurent Pinchart
parent a9bffb7222
commit b3e96411d8
7 changed files with 301 additions and 0 deletions

View file

@ -0,0 +1,178 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
* Copyright (C) 2021-2022, Ideas On Board
*
* lsc.cpp - RkISP1 Lens Shading Correction control
*/
#include "lsc.h"
#include <cmath>
#include <numeric>
#include <libcamera/base/log.h>
#include <libcamera/base/utils.h>
#include "libcamera/internal/yaml_parser.h"
#include "linux/rkisp1-config.h"
/**
* \file lsc.h
*/
namespace libcamera {
namespace ipa::rkisp1::algorithms {
/**
* \class LensShadingCorrection
* \brief RkISP1 Lens Shading Correction control
*
* Due to the optical characteristics of the lens, the light intensity received
* by the sensor is not uniform.
*
* The Lens Shading Correction algorithm applies multipliers to all pixels
* to compensate for the lens shading effect. The coefficients are
* specified in a downscaled table in the YAML tuning file.
*/
LOG_DEFINE_CATEGORY(RkISP1Lsc)
static std::vector<double> parseSizes(const YamlObject &tuningData,
const char *prop)
{
std::vector<double> sizes =
tuningData[prop].getList<double>().value_or(utils::defopt);
if (sizes.size() != RKISP1_CIF_ISP_LSC_SECTORS_TBL_SIZE) {
LOG(RkISP1Lsc, Error)
<< "Invalid '" << prop << "' values: expected "
<< RKISP1_CIF_ISP_LSC_SECTORS_TBL_SIZE
<< " elements, got " << sizes.size();
return {};
}
/*
* The sum of all elements must be 0.5 to satisfy hardware constraints.
* Validate it here, allowing a 1% tolerance as rounding errors may
* prevent an exact match (further adjustments will be performed in
* LensShadingCorrection::prepare()).
*/
float sum = std::accumulate(sizes.begin(), sizes.end(), 0.0f);
if (sum < 0.495 || sum > 0.505) {
LOG(RkISP1Lsc, Error)
<< "Invalid '" << prop << "' values: sum of the elements"
<< " should be 0.5, got " << sum;
return {};
}
return sizes;
}
static std::vector<uint16_t> parseTable(const YamlObject &tuningData,
const char *prop)
{
static constexpr unsigned int kLscNumSamples =
RKISP1_CIF_ISP_LSC_SAMPLES_MAX * RKISP1_CIF_ISP_LSC_SAMPLES_MAX;
std::vector<uint16_t> table =
tuningData[prop].getList<uint16_t>().value_or(utils::defopt);
if (table.size() != kLscNumSamples) {
LOG(RkISP1Lsc, Error)
<< "Invalid '" << prop << "' values: expected "
<< kLscNumSamples
<< " elements, got " << table.size();
return {};
}
return table;
}
LensShadingCorrection::LensShadingCorrection()
: initialized_(false)
{
}
/**
* \copydoc libcamera::ipa::Algorithm::init
*/
int LensShadingCorrection::init([[maybe_unused]] IPAContext &context,
const YamlObject &tuningData)
{
xSize_ = parseSizes(tuningData, "x-size");
ySize_ = parseSizes(tuningData, "y-size");
if (xSize_.empty() || ySize_.empty())
return -EINVAL;
rData_ = parseTable(tuningData, "r");
grData_ = parseTable(tuningData, "gr");
gbData_ = parseTable(tuningData, "gb");
bData_ = parseTable(tuningData, "b");
if (rData_.empty() || grData_.empty() ||
gbData_.empty() || bData_.empty())
return -EINVAL;
initialized_ = true;
return 0;
}
/**
* \copydoc libcamera::ipa::Algorithm::prepare
*/
void LensShadingCorrection::prepare(IPAContext &context,
rkisp1_params_cfg *params)
{
if (context.frameContext.frameCount > 0)
return;
if (!initialized_)
return;
struct rkisp1_cif_isp_lsc_config &config = params->others.lsc_config;
const Size &size = context.configuration.sensor.size;
Size totalSize{};
for (unsigned int i = 0; i < RKISP1_CIF_ISP_LSC_SECTORS_TBL_SIZE; ++i) {
config.x_size_tbl[i] = xSize_[i] * size.width;
config.y_size_tbl[i] = ySize_[i] * size.height;
/*
* To prevent unexpected behavior of the ISP, the sum of x_size_tbl and
* y_size_tbl items shall be equal to respectively size.width/2 and
* size.height/2. Enforce it by computing the last tables value to avoid
* rounding-induced errors.
*/
if (i == RKISP1_CIF_ISP_LSC_SECTORS_TBL_SIZE - 1) {
config.x_size_tbl[i] = size.width / 2 - totalSize.width;
config.y_size_tbl[i] = size.height / 2 - totalSize.height;
}
totalSize.width += config.x_size_tbl[i];
totalSize.height += config.y_size_tbl[i];
config.x_grad_tbl[i] = std::round(32768 / config.x_size_tbl[i]);
config.y_grad_tbl[i] = std::round(32768 / config.y_size_tbl[i]);
}
std::copy(rData_.begin(), rData_.end(),
&params->others.lsc_config.r_data_tbl[0][0]);
std::copy(grData_.begin(), grData_.end(),
&params->others.lsc_config.gr_data_tbl[0][0]);
std::copy(gbData_.begin(), gbData_.end(),
&params->others.lsc_config.gb_data_tbl[0][0]);
std::copy(bData_.begin(), bData_.end(),
&params->others.lsc_config.b_data_tbl[0][0]);
params->module_en_update |= RKISP1_CIF_ISP_MODULE_LSC;
params->module_ens |= RKISP1_CIF_ISP_MODULE_LSC;
params->module_cfg_update |= RKISP1_CIF_ISP_MODULE_LSC;
}
REGISTER_IPA_ALGORITHM(LensShadingCorrection, "LensShadingCorrection")
} /* namespace ipa::rkisp1::algorithms */
} /* namespace libcamera */

View file

@ -0,0 +1,38 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
* Copyright (C) 2021-2022, Ideas On Board
*
* lsc.h - RkISP1 Lens Shading Correction control
*/
#pragma once
#include "algorithm.h"
namespace libcamera {
namespace ipa::rkisp1::algorithms {
class LensShadingCorrection : public Algorithm
{
public:
LensShadingCorrection();
~LensShadingCorrection() = default;
int init(IPAContext &context, const YamlObject &tuningData) override;
void prepare(IPAContext &context, rkisp1_params_cfg *params) override;
private:
bool initialized_;
std::vector<uint16_t> rData_;
std::vector<uint16_t> grData_;
std::vector<uint16_t> gbData_;
std::vector<uint16_t> bData_;
std::vector<double> xSize_;
std::vector<double> ySize_;
};
} /* namespace ipa::rkisp1::algorithms */
} /* namespace libcamera */

View file

@ -5,4 +5,5 @@ rkisp1_ipa_algorithms = files([
'awb.cpp', 'awb.cpp',
'blc.cpp', 'blc.cpp',
'gsl.cpp', 'gsl.cpp',
'lsc.cpp',
]) ])

View file

@ -16,4 +16,83 @@ algorithms:
red: [ 0, 256, 512, 768, 1024, 1280, 1536, 1792, 2048, 2304, 2560, 2816, 3072, 3328, 3584, 3840, 4095 ] red: [ 0, 256, 512, 768, 1024, 1280, 1536, 1792, 2048, 2304, 2560, 2816, 3072, 3328, 3584, 3840, 4095 ]
green: [ 0, 256, 512, 768, 1024, 1280, 1536, 1792, 2048, 2304, 2560, 2816, 3072, 3328, 3584, 3840, 4095 ] green: [ 0, 256, 512, 768, 1024, 1280, 1536, 1792, 2048, 2304, 2560, 2816, 3072, 3328, 3584, 3840, 4095 ]
blue: [ 0, 256, 512, 768, 1024, 1280, 1536, 1792, 2048, 2304, 2560, 2816, 3072, 3328, 3584, 3840, 4095 ] blue: [ 0, 256, 512, 768, 1024, 1280, 1536, 1792, 2048, 2304, 2560, 2816, 3072, 3328, 3584, 3840, 4095 ]
- LensShadingCorrection:
x-size: [ 0.0625, 0.0625, 0.0625, 0.0625, 0.0625, 0.0625, 0.0625, 0.0625 ]
y-size: [ 0.0625, 0.0625, 0.0625, 0.0625, 0.0625, 0.0625, 0.0625, 0.0625 ]
r: [
1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024,
1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024,
1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024,
1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024,
1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024,
1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024,
1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024,
1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024,
1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024,
1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024,
1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024,
1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024,
1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024,
1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024,
1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024,
1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024,
1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024,
]
gr: [
1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024,
1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024,
1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024,
1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024,
1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024,
1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024,
1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024,
1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024,
1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024,
1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024,
1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024,
1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024,
1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024,
1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024,
1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024,
1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024,
1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024,
]
gb: [
1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024,
1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024,
1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024,
1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024,
1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024,
1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024,
1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024,
1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024,
1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024,
1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024,
1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024,
1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024,
1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024,
1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024,
1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024,
1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024,
1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024,
]
b: [
1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024,
1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024,
1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024,
1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024,
1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024,
1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024,
1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024,
1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024,
1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024,
1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024,
1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024,
1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024,
1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024,
1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024,
1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024,
1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024,
1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024,
]
... ...

View file

@ -95,6 +95,9 @@ namespace libcamera::ipa::rkisp1 {
* *
* \var IPASessionConfiguration::sensor.lineDuration * \var IPASessionConfiguration::sensor.lineDuration
* \brief Line duration in microseconds * \brief Line duration in microseconds
*
* \var IPASessionConfiguration::sensor.size
* \brief Sensor output resolution
*/ */
/** /**

View file

@ -33,6 +33,7 @@ struct IPASessionConfiguration {
struct { struct {
utils::Duration lineDuration; utils::Duration lineDuration;
Size size;
} sensor; } sensor;
struct { struct {

View file

@ -210,6 +210,7 @@ int IPARkISP1::configure([[maybe_unused]] const IPACameraSensorInfo &info,
/* Set the hardware revision for the algorithms. */ /* Set the hardware revision for the algorithms. */
context_.configuration.hw.revision = hwRevision_; context_.configuration.hw.revision = hwRevision_;
context_.configuration.sensor.size = info.outputSize;
context_.configuration.sensor.lineDuration = info.lineLength * 1.0s / info.pixelRate; context_.configuration.sensor.lineDuration = info.lineLength * 1.0s / info.pixelRate;
/* /*