libcamera v0.7.0+1-4ceceb68
Supporting cameras in Linux since 2019
Loading...
Searching...
No Matches
module.h
Go to the documentation of this file.
1/* SPDX-License-Identifier: LGPL-2.1-or-later */
2/*
3 * Copyright (C) 2022, Ideas On Board
4 *
5 * IPA module
6 */
7
8#pragma once
9
10#include <list>
11#include <memory>
12#include <string>
13#include <vector>
14
15#include <libcamera/base/log.h>
17
19
20#include "algorithm.h"
21
22namespace libcamera {
23
24LOG_DECLARE_CATEGORY(IPAModuleAlgo)
25
26namespace ipa {
27
28template<typename _Context, typename _FrameContext, typename _Config,
29 typename _Params, typename _Stats>
30class Module : public Loggable
31{
32public:
33 using Context = _Context;
34 using FrameContext = _FrameContext;
35 using Config = _Config;
36 using Params = _Params;
37 using Stats = _Stats;
38
39 virtual ~Module() {}
40
41 const std::list<std::unique_ptr<Algorithm<Module>>> &algorithms() const
42 {
43 return algorithms_;
44 }
45
47 {
48 const auto &list = algorithms.asList();
49
50 for (const auto &[i, algo] : utils::enumerate(list)) {
51 if (!algo.isDictionary()) {
52 LOG(IPAModuleAlgo, Error)
53 << "Invalid YAML syntax for algorithm " << i;
54 algorithms_.clear();
55 return -EINVAL;
56 }
57
58 int ret = createAlgorithm(context, algo);
59 if (ret) {
60 algorithms_.clear();
61 return ret;
62 }
63 }
64
65 return 0;
66 }
67
68 static void registerAlgorithm(AlgorithmFactoryBase<Module> *factory)
69 {
70 factories().push_back(factory);
71 }
72
73private:
74 int createAlgorithm(Context &context, const YamlObject &data)
75 {
76 const auto &[name, algoData] = *data.asDict().begin();
77
78 /*
79 * Optionally, algorithms can be disabled via the tuning file
80 * by including enabled: false as a parameter within the
81 * algorithm tuning data. This is not an error, so we return 0.
82 */
83 if (!algoData["enabled"].get<bool>(true)) {
84 LOG(IPAModuleAlgo, Info)
85 << "Algorithm '" << name << "' disabled via tuning file";
86 return 0;
87 }
88
89 std::unique_ptr<Algorithm<Module>> algo = createAlgorithm(name);
90 if (!algo) {
91 LOG(IPAModuleAlgo, Error)
92 << "Algorithm '" << name << "' not found";
93 return -EINVAL;
94 }
95
96 int ret = algo->init(context, algoData);
97 if (ret) {
98 LOG(IPAModuleAlgo, Error)
99 << "Algorithm '" << name << "' failed to initialize";
100 return ret;
101 }
102
103 LOG(IPAModuleAlgo, Debug)
104 << "Instantiated algorithm '" << name << "'";
105
106 algorithms_.push_back(std::move(algo));
107 return 0;
108 }
109
110 static std::unique_ptr<Algorithm<Module>> createAlgorithm(const std::string &name)
111 {
112 for (const AlgorithmFactoryBase<Module> *factory : factories()) {
113 if (factory->name() == name)
114 return factory->create();
115 }
116
117 return nullptr;
118 }
119
120 static std::vector<AlgorithmFactoryBase<Module> *> &factories()
121 {
122 /*
123 * The static factories map is defined inside the function to ensure
124 * it gets initialized on first use, without any dependency on
125 * link order.
126 */
127 static std::vector<AlgorithmFactoryBase<Module> *> factories;
128 return factories;
129 }
130
131 std::list<std::unique_ptr<Algorithm<Module>>> algorithms_;
132};
133
134} /* namespace ipa */
135
136} /* namespace libcamera */
Base class to support log message extensions.
Definition log.h:92
A class representing the tree structure of the YAML content.
Definition yaml_parser.h:28
DictAdapter asDict() const
Wrap a dictionary YamlObject in an adapter that exposes iterators.
Definition yaml_parser.h:205
The base class for all IPA modules.
Definition module.h:31
_Context Context
The type of the shared IPA context.
Definition module.h:33
_Params Params
The type of the ISP specific parameters.
Definition module.h:36
_FrameContext FrameContext
The type of the frame context.
Definition module.h:34
static void registerAlgorithm(AlgorithmFactoryBase< Module > *factory)
Add an algorithm factory class to the list of available algorithms.
Definition module.h:68
_Config Config
The type of the IPA configuration data.
Definition module.h:35
const std::list< std::unique_ptr< Algorithm< Module > > > & algorithms() const
Retrieve the list of instantiated algorithms.
Definition module.h:41
int createAlgorithms(Context &context, const YamlObject &algorithms)
Create algorithms from YAML configuration data.
Definition module.h:46
_Stats Stats
The type of the IPA statistics and ISP results.
Definition module.h:37
Algorithm common interface.
Logging infrastructure.
#define LOG_DECLARE_CATEGORY(name)
Declare a category of log messages.
Definition log.h:51
#define LOG(category, severity)
Log a message.
Definition log.h:129
Top-level libcamera namespace.
Definition backtrace.h:17
Miscellaneous utility functions.
A YAML parser helper.