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
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
* Copyright (C) 2024, Ideas On Board
*
* RkISP1 Color Correction Matrix control algorithm
*/
#include "ccm.h"
#include <map>
#include <libcamera/base/log.h>
#include <libcamera/base/utils.h>
#include <libcamera/control_ids.h>
#include <libcamera/ipa/core_ipa_interface.h>
#include "libcamera/internal/yaml_parser.h"
#include "libipa/fixedpoint.h"
#include "libipa/interpolator.h"
/**
* \file ccm.h
*/
namespace libcamera {
namespace ipa::rkisp1::algorithms {
/**
* \class Ccm
* \brief A color correction matrix algorithm
*/
LOG_DEFINE_CATEGORY(RkISP1Ccm)
constexpr Matrix<float, 3, 3> kIdentity3x3 = Matrix<float, 3, 3>::identity();
/**
* \copydoc libcamera::ipa::Algorithm::init
*/
int Ccm::init([[maybe_unused]] IPAContext &context, const YamlObject &tuningData)
{
auto &cmap = context.ctrlMap;
cmap[&controls::ColourCorrectionMatrix] = ControlInfo(
ControlValue(-8.0f),
ControlValue(7.993f),
ControlValue(kIdentity3x3.data()));
int ret = ccm_.readYaml(tuningData["ccms"], "ct", "ccm");
if (ret < 0) {
LOG(RkISP1Ccm, Warning)
<< "Failed to parse 'ccm' "
<< "parameter from tuning file; falling back to unit matrix";
ccm_.setData({ { 0, kIdentity3x3 } });
}
ret = offsets_.readYaml(tuningData["ccms"], "ct", "offsets");
if (ret < 0) {
LOG(RkISP1Ccm, Warning)
<< "Failed to parse 'offsets' "
<< "parameter from tuning file; falling back to zero offsets";
offsets_.setData({ { 0, Matrix<int16_t, 3, 1>({ 0, 0, 0 }) } });
}
return 0;
}
/**
* \copydoc libcamera::ipa::Algorithm::configure
*/
int Ccm::configure(IPAContext &context,
[[maybe_unused]] const IPACameraSensorInfo &configInfo)
{
auto &as = context.activeState;
as.ccm.manual = kIdentity3x3;
as.ccm.automatic = ccm_.getInterpolated(as.awb.automatic.temperatureK);
return 0;
}
void Ccm::queueRequest(IPAContext &context,
[[maybe_unused]] const uint32_t frame,
IPAFrameContext &frameContext,
const ControlList &controls)
{
/* Nothing to do here, the ccm will be calculated in prepare() */
if (frameContext.awb.autoEnabled)
return;
auto &ccm = context.activeState.ccm;
const auto &colourTemperature = controls.get(controls::ColourTemperature);
const auto &ccmMatrix = controls.get(controls::ColourCorrectionMatrix);
if (ccmMatrix) {
ccm.manual = Matrix<float, 3, 3>(*ccmMatrix);
LOG(RkISP1Ccm, Debug)
<< "Setting manual CCM from CCM control to " << ccm.manual;
} else if (colourTemperature) {
ccm.manual = ccm_.getInterpolated(*colourTemperature);
LOG(RkISP1Ccm, Debug)
<< "Setting manual CCM from CT control to " << ccm.manual;
}
frameContext.ccm.ccm = ccm.manual;
}
void Ccm::setParameters(struct rkisp1_cif_isp_ctk_config &config,
const Matrix<float, 3, 3> &matrix,
const Matrix<int16_t, 3, 1> &offsets)
{
/*
* 4 bit integer and 7 bit fractional, ranging from -8 (0x400) to
* +7.9921875 (0x3ff)
*/
for (unsigned int i = 0; i < 3; i++) {
for (unsigned int j = 0; j < 3; j++)
config.coeff[i][j] =
floatingToFixedPoint<4, 7, uint16_t, double>(matrix[i][j]);
}
for (unsigned int i = 0; i < 3; i++)
config.ct_offset[i] = offsets[i][0] & 0xfff;
LOG(RkISP1Ccm, Debug) << "Setting matrix " << matrix;
LOG(RkISP1Ccm, Debug) << "Setting offsets " << offsets;
}
/**
* \copydoc libcamera::ipa::Algorithm::prepare
*/
void Ccm::prepare(IPAContext &context, const uint32_t frame,
IPAFrameContext &frameContext, RkISP1Params *params)
{
if (!frameContext.awb.autoEnabled) {
auto config = params->block<BlockType::Ctk>();
config.setEnabled(true);
setParameters(*config, frameContext.ccm.ccm, Matrix<int16_t, 3, 1>());
return;
}
uint32_t ct = frameContext.awb.temperatureK;
if (frame > 0 && ct == ct_) {
frameContext.ccm.ccm = context.activeState.ccm.automatic;
return;
}
ct_ = ct;
Matrix<float, 3, 3> ccm = ccm_.getInterpolated(ct);
Matrix<int16_t, 3, 1> offsets = offsets_.getInterpolated(ct);
context.activeState.ccm.automatic = ccm;
frameContext.ccm.ccm = ccm;
auto config = params->block<BlockType::Ctk>();
config.setEnabled(true);
setParameters(*config, ccm, offsets);
}
/**
* \copydoc libcamera::ipa::Algorithm::process
*/
void Ccm::process([[maybe_unused]] IPAContext &context,
[[maybe_unused]] const uint32_t frame,
IPAFrameContext &frameContext,
[[maybe_unused]] const rkisp1_stat_buffer *stats,
ControlList &metadata)
{
metadata.set(controls::ColourCorrectionMatrix, frameContext.ccm.ccm.data());
}
REGISTER_IPA_ALGORITHM(Ccm, "Ccm")
} /* namespace ipa::rkisp1::algorithms */
} /* namespace libcamera */
|