1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
|
/* 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 <algorithm>
#include <cmath>
#include <numeric>
#include <libcamera/base/log.h>
#include <libcamera/base/utils.h>
#include <libcamera/control_ids.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(std::vector<double>{});
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()).
*/
double sum = std::accumulate(sizes.begin(), sizes.end(), 0.0);
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(std::vector<uint16_t>{});
if (table.size() != kLscNumSamples) {
LOG(RkISP1Lsc, Error)
<< "Invalid '" << prop << "' values: expected "
<< kLscNumSamples
<< " elements, got " << table.size();
return {};
}
return table;
}
LensShadingCorrection::LensShadingCorrection()
: lastCt_({ 0, 0 })
{
}
/**
* \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;
/* Get all defined sets to apply. */
const YamlObject &yamlSets = tuningData["sets"];
if (!yamlSets.isList()) {
LOG(RkISP1Lsc, Error)
<< "'sets' parameter not found in tuning file";
return -EINVAL;
}
const auto &sets = yamlSets.asList();
for (const auto &yamlSet : sets) {
uint32_t ct = yamlSet["ct"].get<uint32_t>(0);
if (sets_.count(ct)) {
LOG(RkISP1Lsc, Error)
<< "Multiple sets found for color temperature "
<< ct;
return -EINVAL;
}
Components &set = sets_[ct];
set.ct = ct;
set.r = parseTable(yamlSet, "r");
set.gr = parseTable(yamlSet, "gr");
set.gb = parseTable(yamlSet, "gb");
set.b = parseTable(yamlSet, "b");
if (set.r.empty() || set.gr.empty() ||
set.gb.empty() || set.b.empty()) {
LOG(RkISP1Lsc, Error)
<< "Set for color temperature " << ct
<< " is missing tables";
return -EINVAL;
}
}
if (sets_.empty()) {
LOG(RkISP1Lsc, Error) << "Failed to load any sets";
return -EINVAL;
}
return 0;
}
/**
* \copydoc libcamera::ipa::Algorithm::configure
*/
int LensShadingCorrection::configure(IPAContext &context,
[[maybe_unused]] const IPACameraSensorInfo &configInfo)
{
const Size &size = context.configuration.sensor.size;
Size totalSize{};
for (unsigned int i = 0; i < RKISP1_CIF_ISP_LSC_SECTORS_TBL_SIZE; ++i) {
xSizes_[i] = xSize_[i] * size.width;
ySizes_[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) {
xSizes_[i] = size.width / 2 - totalSize.width;
ySizes_[i] = size.height / 2 - totalSize.height;
}
totalSize.width += xSizes_[i];
totalSize.height += ySizes_[i];
xGrad_[i] = std::round(32768 / xSizes_[i]);
yGrad_[i] = std::round(32768 / ySizes_[i]);
}
/* Enable LSC on first run unless explicitly disabled by application. */
context.activeState.lsc.enable = true;
context.activeState.lsc.active = false;
return 0;
}
void LensShadingCorrection::disableLSC(rkisp1_params_cfg *params)
{
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;
}
void LensShadingCorrection::setParameters(rkisp1_params_cfg *params)
{
struct rkisp1_cif_isp_lsc_config &config = params->others.lsc_config;
memcpy(config.x_grad_tbl, xGrad_, sizeof(config.x_grad_tbl));
memcpy(config.y_grad_tbl, yGrad_, sizeof(config.y_grad_tbl));
memcpy(config.x_size_tbl, xSizes_, sizeof(config.x_size_tbl));
memcpy(config.y_size_tbl, ySizes_, sizeof(config.y_size_tbl));
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;
}
void LensShadingCorrection::copyTable(rkisp1_cif_isp_lsc_config &config,
const Components &set)
{
std::copy(set.r.begin(), set.r.end(), &config.r_data_tbl[0][0]);
std::copy(set.gr.begin(), set.gr.end(), &config.gr_data_tbl[0][0]);
std::copy(set.gb.begin(), set.gb.end(), &config.gb_data_tbl[0][0]);
std::copy(set.b.begin(), set.b.end(), &config.b_data_tbl[0][0]);
}
/*
* Interpolate LSC parameters based on color temperature value.
*/
void LensShadingCorrection::interpolateTable(rkisp1_cif_isp_lsc_config &config,
const Components &set0,
const Components &set1,
const uint32_t ct)
{
double coeff0 = (set1.ct - ct) / static_cast<double>(set1.ct - set0.ct);
double coeff1 = (ct - set0.ct) / static_cast<double>(set1.ct - set0.ct);
for (unsigned int i = 0; i < RKISP1_CIF_ISP_LSC_SAMPLES_MAX; ++i) {
for (unsigned int j = 0; j < RKISP1_CIF_ISP_LSC_SAMPLES_MAX; ++j) {
unsigned int sample = i * RKISP1_CIF_ISP_LSC_SAMPLES_MAX + j;
config.r_data_tbl[i][j] =
set0.r[sample] * coeff0 +
set1.r[sample] * coeff1;
config.gr_data_tbl[i][j] =
set0.gr[sample] * coeff0 +
set1.gr[sample] * coeff1;
config.gb_data_tbl[i][j] =
set0.gb[sample] * coeff0 +
set1.gb[sample] * coeff1;
config.b_data_tbl[i][j] =
set0.b[sample] * coeff0 +
set1.b[sample] * coeff1;
}
}
}
/**
* \copydoc libcamera::ipa::Algorithm::queueRequest
*/
void LensShadingCorrection::queueRequest(IPAContext &context,
[[maybe_unused]] const uint32_t frame,
[[maybe_unused]] IPAFrameContext &frameContext,
const ControlList &controls)
{
auto lsc = &context.activeState.lsc;
const auto lscEnable = controls.get(controls::LensShadingEnable);
if (lscEnable && *lscEnable != lsc->enable) {
LOG(RkISP1Lsc, Debug)
<< (*lscEnable ? "Enabling" : "Disabling") << " LSC";
lsc->enable = *lscEnable;
}
}
/**
* \copydoc libcamera::ipa::Algorithm::prepare
*/
void LensShadingCorrection::prepare(IPAContext &context,
const uint32_t frame,
[[maybe_unused]] IPAFrameContext &frameContext,
rkisp1_params_cfg *params)
{
auto lsc = &context.activeState.lsc;
if (lsc->enable != lsc->active) {
/* If LSC has to be disabled, disable it and return here. */
if (!lsc->enable) {
disableLSC(params);
lsc->active = false;
return;
}
lsc->active = true;
}
/* Nothing more to do here if LSC is not active. */
if (!lsc->active)
return;
struct rkisp1_cif_isp_lsc_config &config = params->others.lsc_config;
/*
* If there is only one set, the configuration has already been done
* for first frame.
*/
if (sets_.size() == 1 && frame > 0)
return;
/*
* If there is only one set, pick it. We can ignore lastCt_, as it will
* never be relevant.
*/
if (sets_.size() == 1) {
setParameters(params);
copyTable(config, sets_.cbegin()->second);
return;
}
uint32_t ct = context.activeState.awb.temperatureK;
ct = std::clamp(ct, sets_.cbegin()->first, sets_.crbegin()->first);
/*
* If the original is the same, then it means the same adjustment would
* be made. If the adjusted is the same, then it means that it's the
* same as what was actually applied. Thus in these cases we can skip
* reprogramming the LSC.
*
* original == adjusted can only happen if an interpolation
* happened, or if original has an exact entry in sets_. This means
* that if original != adjusted, then original was adjusted to
* the nearest available entry in sets_, resulting in adjusted.
* Clearly, any ct value that is in between original and adjusted
* will be adjusted to the same adjusted value, so we can skip
* reprogramming the LSC table.
*
* We also skip updating the original value, as the last one had a
* larger bound and thus a larger range of ct values that will be
* adjusted to the same adjusted.
*/
if ((lastCt_.original <= ct && ct <= lastCt_.adjusted) ||
(lastCt_.adjusted <= ct && ct <= lastCt_.original))
return;
setParameters(params);
/*
* The color temperature matches exactly one of the available LSC tables.
*/
if (sets_.count(ct)) {
copyTable(config, sets_[ct]);
lastCt_ = { ct, ct };
return;
}
/* No shortcuts left; we need to round or interpolate */
auto iter = sets_.upper_bound(ct);
const Components &set1 = iter->second;
const Components &set0 = (--iter)->second;
uint32_t ct0 = set0.ct;
uint32_t ct1 = set1.ct;
uint32_t diff0 = ct - ct0;
uint32_t diff1 = ct1 - ct;
static constexpr double kThreshold = 0.1;
float threshold = kThreshold * (ct1 - ct0);
if (diff0 < threshold || diff1 < threshold) {
const Components &set = diff0 < diff1 ? set0 : set1;
LOG(RkISP1Lsc, Debug) << "using LSC table for " << set.ct;
copyTable(config, set);
lastCt_ = { ct, set.ct };
return;
}
/*
* ct is not within 10% of the difference between the neighbouring
* color temperatures, so we need to interpolate.
*/
LOG(RkISP1Lsc, Debug)
<< "ct is " << ct << ", interpolating between "
<< ct0 << " and " << ct1;
interpolateTable(config, set0, set1, ct);
lastCt_ = { ct, ct };
}
void LensShadingCorrection::process(IPAContext &context,
[[maybe_unused]] const uint32_t frame,
[[maybe_unused]] IPAFrameContext &frameContext,
[[maybe_unused]] const rkisp1_stat_buffer *stats,
ControlList &metadata)
{
metadata.set(controls::LensShadingEnable, context.activeState.lsc.active);
}
REGISTER_IPA_ALGORITHM(LensShadingCorrection, "LensShadingCorrection")
} /* namespace ipa::rkisp1::algorithms */
} /* namespace libcamera */
|