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
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
* Copyright (C) 2023, Ideas on Board Oy
*
* mali-c55.cpp - Mali-C55 ISP image processing algorithms
*/
#include <map>
#include <string.h>
#include <vector>
#include <linux/mali-c55-config.h>
#include <linux/v4l2-controls.h>
#include <libcamera/base/file.h>
#include <libcamera/base/log.h>
#include <libcamera/control_ids.h>
#include <libcamera/ipa/ipa_interface.h>
#include <libcamera/ipa/ipa_module_info.h>
#include <libcamera/ipa/mali-c55_ipa_interface.h>
#include "libcamera/internal/bayer_format.h"
#include "libcamera/internal/mapped_framebuffer.h"
#include "libcamera/internal/yaml_parser.h"
#include "algorithms/algorithm.h"
#include "libipa/camera_sensor_helper.h"
#include "ipa_context.h"
namespace libcamera {
LOG_DEFINE_CATEGORY(IPAMaliC55)
namespace ipa::mali_c55 {
/* Maximum number of frame contexts to be held */
static constexpr uint32_t kMaxFrameContexts = 16;
class IPAMaliC55 : public IPAMaliC55Interface, public Module
{
public:
IPAMaliC55();
int init(const IPASettings &settings, const IPAConfigInfo &ipaConfig,
ControlInfoMap *ipaControls) override;
int start() override;
void stop() override;
int configure(const IPAConfigInfo &ipaConfig, uint8_t bayerOrder,
ControlInfoMap *ipaControls) override;
void mapBuffers(const std::vector<IPABuffer> &buffers, bool readOnly) override;
void unmapBuffers(const std::vector<IPABuffer> &buffers) override;
void queueRequest(const uint32_t request, const ControlList &controls) override;
void fillParams(unsigned int request, uint32_t bufferId) override;
void processStats(unsigned int request, unsigned int bufferId,
const ControlList &sensorControls) override;
protected:
std::string logPrefix() const override;
private:
void updateControls(const IPACameraSensorInfo &sensorInfo,
const ControlInfoMap &sensorControls,
ControlInfoMap *ipaControls);
void setControls();
std::map<unsigned int, MappedFrameBuffer> buffers_;
ControlInfoMap sensorControls_;
/* Interface to the Camera Helper */
std::unique_ptr<CameraSensorHelper> camHelper_;
/* Local parameter storage */
struct IPAContext context_;
};
namespace {
} /* namespace */
IPAMaliC55::IPAMaliC55()
: context_(kMaxFrameContexts)
{
}
std::string IPAMaliC55::logPrefix() const
{
return "mali-c55";
}
int IPAMaliC55::init(const IPASettings &settings, const IPAConfigInfo &ipaConfig,
ControlInfoMap *ipaControls)
{
camHelper_ = CameraSensorHelperFactoryBase::create(settings.sensorModel);
if (!camHelper_) {
LOG(IPAMaliC55, Error)
<< "Failed to create camera sensor helper for "
<< settings.sensorModel;
return -ENODEV;
}
File file(settings.configurationFile);
if (!file.open(File::OpenModeFlag::ReadOnly)) {
int ret = file.error();
LOG(IPAMaliC55, Error)
<< "Failed to open configuration file "
<< settings.configurationFile << ": " << strerror(-ret);
return ret;
}
std::unique_ptr<libcamera::YamlObject> data = YamlParser::parse(file);
if (!data)
return -EINVAL;
if (!data->contains("algorithms")) {
LOG(IPAMaliC55, Error)
<< "Tuning file doesn't contain any algorithm";
return -EINVAL;
}
int ret = createAlgorithms(context_, (*data)["algorithms"]);
if (ret)
return ret;
updateControls(ipaConfig.sensorInfo, ipaConfig.sensorControls, ipaControls);
return 0;
}
void IPAMaliC55::setControls()
{
ControlList ctrls(sensorControls_);
setSensorControls.emit(ctrls);
}
int IPAMaliC55::start()
{
return 0;
}
void IPAMaliC55::stop()
{
context_.frameContexts.clear();
}
void IPAMaliC55::updateControls(const IPACameraSensorInfo &sensorInfo,
const ControlInfoMap &sensorControls,
ControlInfoMap *ipaControls)
{
ControlInfoMap::Map ctrlMap;
/*
* Compute the frame duration limits.
*
* The frame length is computed assuming a fixed line length combined
* with the vertical frame sizes.
*/
const ControlInfo &v4l2HBlank = sensorControls.find(V4L2_CID_HBLANK)->second;
uint32_t hblank = v4l2HBlank.def().get<int32_t>();
uint32_t lineLength = sensorInfo.outputSize.width + hblank;
const ControlInfo &v4l2VBlank = sensorControls.find(V4L2_CID_VBLANK)->second;
std::array<uint32_t, 3> frameHeights{
v4l2VBlank.min().get<int32_t>() + sensorInfo.outputSize.height,
v4l2VBlank.max().get<int32_t>() + sensorInfo.outputSize.height,
v4l2VBlank.def().get<int32_t>() + sensorInfo.outputSize.height,
};
std::array<int64_t, 3> frameDurations;
for (unsigned int i = 0; i < frameHeights.size(); ++i) {
uint64_t frameSize = lineLength * frameHeights[i];
frameDurations[i] = frameSize / (sensorInfo.pixelRate / 1000000U);
}
ctrlMap[&controls::FrameDurationLimits] = ControlInfo(frameDurations[0],
frameDurations[1],
frameDurations[2]);
/*
* Compute exposure time limits from the V4L2_CID_EXPOSURE control
* limits and the line duration.
*/
double lineDuration = sensorInfo.minLineLength / sensorInfo.pixelRate;
const ControlInfo &v4l2Exposure = sensorControls.find(V4L2_CID_EXPOSURE)->second;
int32_t minExposure = v4l2Exposure.min().get<int32_t>() * lineDuration;
int32_t maxExposure = v4l2Exposure.max().get<int32_t>() * lineDuration;
int32_t defExposure = v4l2Exposure.def().get<int32_t>() * lineDuration;
ctrlMap[&controls::ExposureTime] = ControlInfo(minExposure, maxExposure, defExposure);
/* Compute the analogue gain limits. */
const ControlInfo &v4l2Gain = sensorControls.find(V4L2_CID_ANALOGUE_GAIN)->second;
float minGain = camHelper_->gain(v4l2Gain.min().get<int32_t>());
float maxGain = camHelper_->gain(v4l2Gain.max().get<int32_t>());
float defGain = camHelper_->gain(v4l2Gain.def().get<int32_t>());
ctrlMap[&controls::AnalogueGain] = ControlInfo(minGain, maxGain, defGain);
/*
* Merge in any controls that we support either statically or from the
* algorithms.
*/
ctrlMap.merge(context_.ctrlMap);
*ipaControls = ControlInfoMap(std::move(ctrlMap), controls::controls);
}
int IPAMaliC55::configure(const IPAConfigInfo &ipaConfig,
[[maybe_unused]] uint8_t bayerOrder,
ControlInfoMap *ipaControls)
{
sensorControls_ = ipaConfig.sensorControls;
/* Clear the IPA context before the streaming session. */
context_.configuration = {};
context_.activeState = {};
context_.frameContexts.clear();
const IPACameraSensorInfo &info = ipaConfig.sensorInfo;
updateControls(info, ipaConfig.sensorControls, ipaControls);
for (auto const &a : algorithms()) {
Algorithm *algo = static_cast<Algorithm *>(a.get());
int ret = algo->configure(context_, info);
if (ret)
return ret;
}
return 0;
}
void IPAMaliC55::mapBuffers(const std::vector<IPABuffer> &buffers, bool readOnly)
{
for (const IPABuffer &buffer : buffers) {
const FrameBuffer fb(buffer.planes);
buffers_.emplace(
buffer.id,
MappedFrameBuffer(
&fb,
readOnly ? MappedFrameBuffer::MapFlag::Read
: MappedFrameBuffer::MapFlag::ReadWrite));
}
}
void IPAMaliC55::unmapBuffers(const std::vector<IPABuffer> &buffers)
{
for (const IPABuffer &buffer : buffers) {
auto it = buffers_.find(buffer.id);
if (it == buffers_.end())
continue;
buffers_.erase(buffer.id);
}
}
void IPAMaliC55::queueRequest(const uint32_t request, const ControlList &controls)
{
IPAFrameContext &frameContext = context_.frameContexts.alloc(request);
for (auto const &a : algorithms()) {
Algorithm *algo = static_cast<Algorithm *>(a.get());
algo->queueRequest(context_, request, frameContext, controls);
}
}
void IPAMaliC55::fillParams(unsigned int request,
[[maybe_unused]] uint32_t bufferId)
{
struct mali_c55_params_buffer *params;
IPAFrameContext &frameContext = context_.frameContexts.get(request);
params = reinterpret_cast<mali_c55_params_buffer *>(
buffers_.at(bufferId).planes()[0].data());
memset(params, 0, sizeof(mali_c55_params_buffer));
params->version = MALI_C55_PARAM_BUFFER_V1;
for (auto const &algo : algorithms()) {
algo->prepare(context_, request, frameContext, params);
ASSERT(params->total_size <= MALI_C55_PARAMS_MAX_SIZE);
}
paramsComputed.emit(request);
}
void IPAMaliC55::processStats(unsigned int request, unsigned int bufferId,
const ControlList &sensorControls)
{
IPAFrameContext &frameContext = context_.frameContexts.get(request);
const mali_c55_stats_buffer *stats = nullptr;
stats = reinterpret_cast<mali_c55_stats_buffer *>(
buffers_.at(bufferId).planes()[0].data());
frameContext.agc.exposure =
sensorControls.get(V4L2_CID_EXPOSURE).get<int32_t>();
frameContext.agc.sensorGain =
camHelper_->gain(sensorControls.get(V4L2_CID_ANALOGUE_GAIN).get<int32_t>());
ControlList metadata(controls::controls);
for (auto const &a : algorithms()) {
Algorithm *algo = static_cast<Algorithm *>(a.get());
algo->process(context_, request, frameContext, stats, metadata);
}
setControls();
statsProcessed.emit(request, metadata);
}
} /* namespace ipa::mali_c55 */
/*
* External IPA module interface
*/
extern "C" {
const struct IPAModuleInfo ipaModuleInfo = {
IPA_MODULE_API_VERSION,
1,
"mali-c55",
"mali-c55",
};
IPAInterface *ipaCreate()
{
return new ipa::mali_c55::IPAMaliC55();
}
} /* extern "C" */
} /* namespace libcamera */
|