summaryrefslogtreecommitdiffstats
path: root/multimedia/libcamera/patches/025-backport-ipa-libipa-lsc-make-components-a-map.patch
blob: 02b74977910740ddb6ed063313e8a54c1911ed24 (plain)
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
From 41f8370b8a093787669157f0688edb87a6e3086e Mon Sep 17 00:00:00 2001
From: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
Date: Thu, 2 Jul 2026 11:59:12 +0200
Subject: [PATCH] ipa: libipa: lsc: Make Components a map<>

The current definition of lsc::Components assumes 4 gain vectors named
after the colour components supported by the RkISP1 ISP (r, gr, gb and
b).

As different ISP support different colour channels for their Lsc tables
make lsc::Components a map that associates the name of the colour
channel to the vector of gains.

The use of strings as index for the map comes from the requirement of
parsing the tuning file using string identifiers. Pass the strings to
use for tuning file parsing to the LscAlgorithm::init() function as part
of a newly introduced LscDescriptor type and use the same strings for
indexing the gain arrays.

The newly introduced LscDescriptor type also provides to the
LscAlgorithm class the expected size of the Lsc grid, allowing to remove
RkISP1 specific values from the libipa generic implementation.

Signed-off-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
Reviewed-by: Stefan Klug <stefan.klug@ideasonboard.com>
Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
---
 src/ipa/libipa/lsc.cpp            | 10 ++---
 src/ipa/libipa/lsc.h              |  4 +-
 src/ipa/libipa/lsc_base.cpp       | 49 +++++++++++++++-------
 src/ipa/libipa/lsc_base.h         | 18 ++++----
 src/ipa/libipa/lsc_polynomial.cpp | 59 +++++++++++++--------------
 src/ipa/libipa/lsc_polynomial.h   | 10 ++---
 src/ipa/libipa/lsc_table.cpp      | 68 ++++++++++++++++++-------------
 src/ipa/libipa/lsc_table.h        |  9 +++-
 src/ipa/rkisp1/algorithms/lsc.cpp | 20 ++++++---
 9 files changed, 144 insertions(+), 103 deletions(-)

--- a/src/ipa/libipa/lsc.cpp
+++ b/src/ipa/libipa/lsc.cpp
@@ -86,16 +86,16 @@ namespace lsc {
 
 /**
  * \param[in] tuningData The tuning data
- * \param[in] sensorSize The physical sensor size
  * \param[in] controls The IPA list of supported controls
+ * \param[in] descriptor The LSC engine descriptor
  *
  * Parse \a tuningData according to the settings specified in \a descriptor to
  * populate the LSC data and registers LSC controls in \a controls.
  *
  * \return 0 on success, a negative error code otherwise
  */
-int LscAlgorithm::init(const ValueNode &tuningData, const Size &sensorSize,
-		       ControlInfoMap::Map &controls)
+int LscAlgorithm::init(const ValueNode &tuningData, ControlInfoMap::Map &controls,
+		       const LscDescriptor &descriptor)
 {
 	polynomial_ = false;
 
@@ -108,7 +108,7 @@ int LscAlgorithm::init(const ValueNode &
 		 * \todo Most likely the reference frame should be native_size.
 		 * Let's wait how the internal discussions progress.
 		 */
-		impl_ = std::make_unique<LscPolynomial>(sensorSize);
+		impl_ = std::make_unique<LscPolynomial>(descriptor.sensorSize);
 		polynomial_ = true;
 		LOG(Lsc, Debug) << "Using polynomial Lsc";
 	} else {
@@ -123,7 +123,7 @@ int LscAlgorithm::init(const ValueNode &
 		return -EINVAL;
 	}
 
-	int ret = impl_->parseLscData(yamlSets);
+	int ret = impl_->parseLscData(yamlSets, descriptor);
 	if (ret)
 		return ret;
 
--- a/src/ipa/libipa/lsc.h
+++ b/src/ipa/libipa/lsc.h
@@ -38,8 +38,8 @@ struct FrameContext {
 class LscAlgorithm
 {
 public:
-	int init(const ValueNode &tuningData, const Size &sensorSize,
-		 ControlInfoMap::Map &controls);
+	int init(const ValueNode &tuningData, ControlInfoMap::Map &controls,
+		 const LscDescriptor &descriptor);
 
 	int configure(lsc::ActiveState &state, const Rectangle &analogCrop,
 		      const std::vector<double> &xPos,
--- a/src/ipa/libipa/lsc_base.cpp
+++ b/src/ipa/libipa/lsc_base.cpp
@@ -19,23 +19,20 @@ namespace ipa {
 namespace lsc {
 
 /**
- * \struct Components
+ * \typedef Components
  * \brief Associate colour components with a list of gains
  *
  * LSC tables are defined as a list of gain values associated to a colour
  * component.
  *
- * \var Components::r
- * \brief The list of gains for the Red colour component
+ * As different ISPs support different colour components (usually 'r', 'gr',
+ * 'gb', 'b' or just 'r', 'g', 'b') this class associates a string
+ * identifier for the colour component to a list of gains.
  *
- * \var Components::gr
- * \brief The list of gains for the Green/Red colour component
+ * Each key name shall match an entry in the tuning file.
  *
- * \var Components::gb
- * \brief The list of gains for the Green/Blue colour component
- *
- * \var Components::b
- * \brief The list of gains for the Blue colour component
+ * The list of keys is provided to the LscAlgorithm class using \a
+ * LscDescriptor::keys.
  */
 
 /**
@@ -57,14 +54,33 @@ void Interpolator<lsc::Components>::
 		    lsc::Components &dest,
 		    double lambda)
 {
-	interpolateVector(a.r, b.r, dest.r, lambda);
-	interpolateVector(a.gr, b.gr, dest.gr, lambda);
-	interpolateVector(a.gb, b.gb, dest.gb, lambda);
-	interpolateVector(a.b, b.b, dest.b, lambda);
+	for (auto const &[k, v] : a)
+		interpolateVector(v, b.at(k), dest[k], lambda);
 }
 #endif
 
 /**
+ * \struct LscDescriptor
+ * \brief Describe the ISP LSC engine
+ *
+ * \var LscDescriptor::keys
+ * \brief The list of colour components to which a list of gains is associated
+ * with in the tuning file. Used for parsing the tuning file
+ *
+ * \var LscDescriptor::numHSamples
+ * \brief Number of horizontal gain samples of the ISP LSC grid. Used for
+ * validating the list of gains parsed from tuning file
+ *
+ * \var LscDescriptor::numVSamples
+ * \brief Number of vertical gain samples of the ISP LSC grid. Used for
+ * validating the list of gains parsed from tuning file
+ *
+ * \var LscDescriptor::sensorSize
+ * \brief The physical sensor size. This is the largest frame size used to
+ * generate the LSC table. Only used by the polynomial LSC algorithm
+ */
+
+/**
  * \class LscImplementation
  * \brief Pure virtual base class for LSC algorithm implementations
  *
@@ -80,6 +96,11 @@ void Interpolator<lsc::Components>::
  * \fn LscImplementation::parseLscData
  * \brief Parse \a tuningData
  * \param[in] tuningData The tuning data
+ * \param[in] descriptor The LSC engine descriptor
+ *
+ * Parse the tuning file using the \a descriptor to identify the colour
+ * components in the tuning data and validate the size of the loaded gains
+ * tables.
  *
  * \return 0 on success, a negative error number otherwise
  */
--- a/src/ipa/libipa/lsc_base.h
+++ b/src/ipa/libipa/lsc_base.h
@@ -26,13 +26,7 @@ namespace ipa {
 
 namespace lsc {
 
-struct Components {
-	std::vector<uint16_t> r;
-	std::vector<uint16_t> gr;
-	std::vector<uint16_t> gb;
-	std::vector<uint16_t> b;
-};
-
+using Components = std::map<std::string, std::vector<uint16_t>, std::less<>>;
 using ComponentsMap = std::map<unsigned int, Components>;
 
 } /* namespace lsc */
@@ -56,12 +50,20 @@ void Interpolator<lsc::Components>::
 		    double lambda);
 #endif /* __DOXYGEN__ */
 
+struct LscDescriptor {
+	std::vector<std::string> keys;
+	unsigned int numHSamples;
+	unsigned int numVSamples;
+	Size sensorSize;
+};
+
 class LscImplementation
 {
 public:
 	virtual ~LscImplementation() {}
 
-	virtual int parseLscData(const ValueNode &tuningData) = 0;
+	virtual int parseLscData(const ValueNode &tuningData,
+				 const LscDescriptor &descriptor) = 0;
 
 	virtual lsc::ComponentsMap
 	sampleForCrop(const Rectangle &cropRectangle,
--- a/src/ipa/libipa/lsc_polynomial.cpp
+++ b/src/ipa/libipa/lsc_polynomial.cpp
@@ -133,44 +133,42 @@ void Polynomial::setReferenceImageSize(c
 /**
  * \brief Parse polynomial LSC data
  * \param[in] sets The tuning file content
+ * \param[in] descriptor The LSC engine descriptor
  *
  * Parse the LSC data in polyomial form from the \a sets tuning data.
  *
  * \return 0 on success or a negative error number otherwise
  */
-int LscPolynomial::parseLscData(const ValueNode &sets)
+int LscPolynomial::parseLscData(const ValueNode &sets,
+				const LscDescriptor &descriptor)
 {
 	for (const auto &set : sets.asList()) {
-		std::optional<lsc::Polynomial> pr, pgr, pgb, pb;
 		uint32_t ct = set["ct"].get<uint32_t>(0);
 
-		if (lscData_.count(ct)) {
-			LOG(LscPolynomial, Error)
-				<< "Multiple sets found for "
-				<< "color temperature " << ct;
-			return -EINVAL;
-		}
+		PolynomialComponents components;
+		for (auto &k : descriptor.keys) {
+			auto polynomial = set[k].get<lsc::Polynomial>();
+			if (!polynomial) {
+				LOG(LscPolynomial, Error)
+					<< "Missing polynomial for component "
+					<< k;
+				return -EINVAL;
+			}
+
+			auto [it, inserted] =
+				components.try_emplace(k, std::move(*polynomial));
+			ASSERT(inserted);
 
-		pr = set["r"].get<lsc::Polynomial>();
-		pgr = set["gr"].get<lsc::Polynomial>();
-		pgb = set["gb"].get<lsc::Polynomial>();
-		pb = set["b"].get<lsc::Polynomial>();
+			it->second.setReferenceImageSize(descriptor.sensorSize);
+		}
 
-		if (!(pr || pgr || pgb || pb)) {
+		auto [it, inserted] = lscData_.try_emplace(ct, std::move(components));
+		if (!inserted) {
 			LOG(LscPolynomial, Error)
-				<< "Failed to parse polynomial for "
-				<< "colour temperature " << ct;
+				<< "Multiple sets found for "
+				<< "color temperature " << ct;
 			return -EINVAL;
 		}
-
-		pr->setReferenceImageSize(sensorSize_);
-		pgr->setReferenceImageSize(sensorSize_);
-		pgb->setReferenceImageSize(sensorSize_);
-		pb->setReferenceImageSize(sensorSize_);
-
-		lscData_.emplace(std::piecewise_construct,
-				 std::forward_as_tuple(ct),
-				 std::forward_as_tuple(PolynomialComponents{ *pr, *pgr, *pgb, *pb }));
 	}
 
 	if (lscData_.empty()) {
@@ -210,13 +208,12 @@ LscPolynomial::sampleForCrop(const Recta
 
 	lsc::ComponentsMap components;
 
-	for (const auto &[k, p] : lscData_) {
-		components[k] = {
-			samplePolynomial(p.pr, xPos, yPos, cropRectangle),
-			samplePolynomial(p.pgr, xPos, yPos, cropRectangle),
-			samplePolynomial(p.pgb, xPos, yPos, cropRectangle),
-			samplePolynomial(p.pb, xPos, yPos, cropRectangle)
-		};
+	for (const auto &[t, c] : lscData_) {
+		lsc::Components &comp = components[t];
+
+		for (const auto &[k, p] : c)
+			comp.try_emplace(k, samplePolynomial(p, xPos, yPos,
+							     cropRectangle));
 	}
 
 	return components;
--- a/src/ipa/libipa/lsc_polynomial.h
+++ b/src/ipa/libipa/lsc_polynomial.h
@@ -53,12 +53,7 @@ private:
 class LscPolynomial : public LscImplementation
 {
 private:
-	struct PolynomialComponents {
-		lsc::Polynomial pr;
-		lsc::Polynomial pgr;
-		lsc::Polynomial pgb;
-		lsc::Polynomial pb;
-	};
+	using PolynomialComponents = std::map<std::string, lsc::Polynomial, std::less<>>;
 	using PolynomialComponentsMap = std::map<unsigned int, PolynomialComponents>;
 
 public:
@@ -67,7 +62,8 @@ public:
 	{
 	}
 
-	int parseLscData(const ValueNode &sets) override;
+	int parseLscData(const ValueNode &sets,
+			 const LscDescriptor &descriptor) override;
 
 	lsc::ComponentsMap
 	sampleForCrop(const Rectangle &cropRectangle,
--- a/src/ipa/libipa/lsc_table.cpp
+++ b/src/ipa/libipa/lsc_table.cpp
@@ -7,9 +7,6 @@
 
 #include "lsc_table.h"
 
-/* \todo Remove RkISP1 from libipa. */
-#include "linux/rkisp1-config.h"
-
 namespace libcamera {
 
 LOG_DEFINE_CATEGORY(LscTable)
@@ -29,42 +26,56 @@ namespace ipa {
 /**
  * \brief Parse tabular LSC data
  * \param[in] sets The tuning file content
+ * \param[in] descriptor The LSC engine descriptor
  *
  * Parse the LSC data in tabular form from the \a sets tuning data.
  *
  * \return 0 on success or a negative error number otherwise
  */
-int LscTable::parseLscData(const ValueNode &sets)
+int LscTable::parseLscData(const ValueNode &sets,
+			   const LscDescriptor &descriptor)
 {
 	for (const auto &set : sets.asList()) {
 		uint32_t ct = set["ct"].get<uint32_t>(0);
 
-		if (lscData_.count(ct)) {
-			LOG(LscTable, Error)
-				<< "Multiple sets found for color temperature "
-				<< ct;
-			return -EINVAL;
-		}
+		int ret = parseLscComponent(set, ct, descriptor);
+		if (ret)
+			return ret;
+	}
+
+	if (lscData_.empty()) {
+		LOG(LscTable, Error) << "Failed to load any sets";
+		return -EINVAL;
+	}
 
-		lsc::Components components;
-		components.r = parseTable(set, "r");
-		components.gr = parseTable(set, "gr");
-		components.gb = parseTable(set, "gb");
-		components.b = parseTable(set, "b");
+	return 0;
+}
+
+int LscTable::parseLscComponent(const ValueNode &yamlSet,
+				unsigned int ct, const LscDescriptor &descriptor)
+{
+	lsc::Components component;
+	for (auto &k : descriptor.keys) {
+		auto [it, inserted] =
+			component.try_emplace(k, parseTable(yamlSet,
+							    k.c_str(),
+							    descriptor.numHSamples,
+							    descriptor.numVSamples));
+		ASSERT(inserted);
 
-		if (components.r.empty() || components.gr.empty() ||
-		    components.gb.empty() || components.b.empty()) {
+		if (it->second.empty()) {
 			LOG(LscTable, Error)
-				<< "Set for color temperature " << ct
-				<< " is missing tables";
+				<< "Set " << k << " for color temperature "
+				<< ct << " is missing";
 			return -EINVAL;
 		}
-
-		lscData_.emplace(ct, std::move(components));
 	}
 
-	if (lscData_.empty()) {
-		LOG(LscTable, Error) << "Failed to load any sets";
+	auto [it, inserted] = lscData_.try_emplace(ct, std::move(component));
+	if (!inserted) {
+		LOG(LscTable, Error)
+			<< "Multiple sets found for color temperature "
+			<< ct;
 		return -EINVAL;
 	}
 
@@ -72,17 +83,18 @@ int LscTable::parseLscData(const ValueNo
 }
 
 std::vector<uint16_t> LscTable::parseTable(const ValueNode &tuningData,
-					   const char *prop)
+					   const char *prop,
+					   unsigned int numHSamples,
+					   unsigned int numVSamples)
 {
-	static constexpr unsigned int kLscNumSamples =
-		RKISP1_CIF_ISP_LSC_SAMPLES_MAX * RKISP1_CIF_ISP_LSC_SAMPLES_MAX;
+	unsigned int lscNumSamples = numHSamples * numVSamples;
 
 	std::vector<uint16_t> table =
 		tuningData[prop].get<std::vector<uint16_t>>().value_or(utils::defopt);
-	if (table.size() != kLscNumSamples) {
+	if (table.size() != lscNumSamples) {
 		LOG(LscTable, Error)
 			<< "Invalid '" << prop << "' values: expected "
-			<< kLscNumSamples
+			<< lscNumSamples
 			<< " elements, got " << table.size();
 		return {};
 	}
--- a/src/ipa/libipa/lsc_table.h
+++ b/src/ipa/libipa/lsc_table.h
@@ -26,7 +26,8 @@ namespace ipa {
 class LscTable : public LscImplementation
 {
 public:
-	int parseLscData(const ValueNode &sets) override;
+	int parseLscData(const ValueNode &sets,
+			 const LscDescriptor &descriptor) override;
 
 	lsc::ComponentsMap
 	sampleForCrop([[maybe_unused]] const Rectangle &cropRectangle,
@@ -39,8 +40,12 @@ public:
 	}
 
 private:
+	int parseLscComponent(const ValueNode &yamlSet,
+			      unsigned int ct, const LscDescriptor &descriptor);
 	std::vector<uint16_t> parseTable(const ValueNode &tuningData,
-					 const char *prop);
+					 const char *prop,
+					 unsigned int numHSamples,
+					 unsigned int numVSamples);
 
 	lsc::ComponentsMap lscData_;
 };
--- a/src/ipa/rkisp1/algorithms/lsc.cpp
+++ b/src/ipa/rkisp1/algorithms/lsc.cpp
@@ -123,8 +123,12 @@ int LensShadingCorrection::init([[maybe_
 	xPos_ = sizesListToPositions(xSize_);
 	yPos_ = sizesListToPositions(ySize_);
 
-	return lscAlgo_.init(tuningData, context.sensorInfo.activeAreaSize,
-			     context.ctrlMap);
+	return lscAlgo_.init(tuningData,  context.ctrlMap, {
+				.keys = { "r", "gr", "gb", "b" },
+				.numHSamples = RKISP1_CIF_ISP_LSC_SAMPLES_MAX,
+				.numVSamples = RKISP1_CIF_ISP_LSC_SAMPLES_MAX,
+				.sensorSize = context.sensorInfo.activeAreaSize
+			     });
 }
 
 /**
@@ -173,10 +177,14 @@ void LensShadingCorrection::setParameter
 void LensShadingCorrection::copyTable(rkisp1_cif_isp_lsc_config &config,
 				      const lsc::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]);
+	const auto &r = set.at("r");
+	std::copy(r.begin(), r.end(), &config.r_data_tbl[0][0]);
+	const auto &gr = set.at("gr");
+	std::copy(gr.begin(), gr.end(), &config.gr_data_tbl[0][0]);
+	const auto &gb = set.at("gb");
+	std::copy(gb.begin(), gb.end(), &config.gb_data_tbl[0][0]);
+	const auto &b = set.at("b");
+	std::copy(b.begin(), b.end(), &config.b_data_tbl[0][0]);
 }
 
 /**