# SPDX-License-Identifier: GPL-2.0-or-later # # Copyright (C) 2022, Paul Elder # # raspberrypi_parser.py - Parser for Raspberry Pi config file format from .parser import Parser import json import numbers import libtuning.utils as utils class RaspberryPiParser(Parser): def __init__(self): super().__init__() # The string in the 'disable' and 'plot' lists are formatted as # 'rpi.{module_name}'. # @brief Enumerate, as a module, @a listt if its value exists in @a dictt # and it is the name of a valid module in @a modules def _enumerate_rpi_modules(self, listt, dictt, modules): for x in listt: name = x.replace('rpi.', '') if name not in dictt: continue module = utils.get_module_by_typename(modules, name) if module is not None: yield module def _valid_macbeth_option(self, value): if not isinstance(value, dict): return False if list(value.keys()) != ['small', 'show']: return False for val in value.values(): if not isinstance(val, numbers.Number): return False return True def parse(self, config_file: str, modules: list) -> (dict, list): with open(config_file, 'r') as config_json: config = json.load(config_json) disable = [] for module in self._enumerate_rpi_modules(config['disable'], config, modules): disable.append(module) # Remove the disabled module's config too config.pop(module.name) config.pop('disable') # The raspberrypi config format has 'plot' map to a list of module # names which should be plotted. libtuning has each module contain the # plot information in itself so do this conversion. for module in self._enumerate_rpi_modules(config['plot'], config, modules): # It's fine to set the value of a potentially disabled module, as # the object still exists at this point module.appendValue('debug', 'plot') config.pop('plot') # Convert the keys from module name to module instance new_config = {} for module_name in config: module = utils.get_module_by_type_name(modules, module_name) if module is not None: new_config[module] = config[module_name] new_config['general'] = {} if 'blacklevel' in config: if not isinstance(config['blacklevel'], numbers.Number): raise TypeError('Config "blacklevel" must be a number') # Raspberry Pi's ctt config has magic blacklevel value -1 to mean # "get it from the image metadata". Since we already do that in # Image, don't save it to the config here. if config['blacklevel'] >= 0: new_config['general']['blacklevel'] = config['blacklevel'] if 'macbeth' in config: if not self._valid_macbeth_option(config['macbeth']): raise TypeError('Config "macbeth" must be a dict: {"small": number, "show": number}') new_config['general']['macbeth'] = config['macbeth'] else: new_config['general']['macbeth'] = {'small': 0, 'show': 0} return new_config, disable ba670163c8'>treecommitdiff
path: root/src/libcamera/ipa_module.cpp
blob: c9ff7de30e215b858dcebb47bc98cbc67a698e59 (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
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
 * Copyright (C) 2019, Google Inc.
 *
 * ipa_module.cpp - Image Processing Algorithm module
 */

#include "libcamera/internal/ipa_module.h"

#include <algorithm>
#include <array>
#include <ctype.h>
#include <dlfcn.h>
#include <elf.h>
#include <errno.h>
#include <fcntl.h>
#include <link.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#include <libcamera/base/file.h>
#include <libcamera/base/log.h>
#include <libcamera/base/span.h>
#include <libcamera/base/utils.h>

#include "libcamera/internal/pipeline_handler.h"

/**
 * \file ipa_module.h
 * \brief Image Processing Algorithm module
 */

/**
 * \file ipa_module_info.h
 * \brief Image Processing Algorithm module information
 */

namespace libcamera {

LOG_DEFINE_CATEGORY(IPAModule)

namespace {

template<typename T>
typename std::remove_extent_t<T> *elfPointer(Span<const uint8_t> elf,
					     off_t offset, size_t objSize)
{
	size_t size = offset + objSize;
	if (size > elf.size() || size < objSize)
		return nullptr;

	return reinterpret_cast<typename std::remove_extent_t<T> *>
		(reinterpret_cast<const char *>(elf.data()) + offset);
}

template<typename T>
typename std::remove_extent_t<T> *elfPointer(Span<const uint8_t> elf,
					     off_t offset)
{
	return elfPointer<T>(elf, offset, sizeof(T));
}

int elfVerifyIdent(Span<const uint8_t> elf)
{
	const char *e_ident = elfPointer<const char[EI_NIDENT]>(elf, 0);
	if (!e_ident)
		return -ENOEXEC;

	if (e_ident[EI_MAG0] != ELFMAG0 ||
	    e_ident[EI_MAG1] != ELFMAG1 ||
	    e_ident[EI_MAG2] != ELFMAG2 ||
	    e_ident[EI_MAG3] != ELFMAG3 ||
	    e_ident[EI_VERSION] != EV_CURRENT)
		return -ENOEXEC;

	int bitClass = sizeof(unsigned long) == 4 ? ELFCLASS32 : ELFCLASS64;