libcamera v0.5.1 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>
16#include <libcamera/base/utils.h>
17
18#include "libcamera/internal/yaml_parser.h"
19
20#include "algorithm.h"
21
22 namespace libcamera {
23
24 LOG_DECLARE_CATEGORY(IPAModuleAlgo)
25
26 namespace ipa {
27
28 template<typename _Context, typename _FrameContext, typename _Config,
29 typename _Params, typename _Stats>
30 class Module : public Loggable
31{
32 public:
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
46 int createAlgorithms(Context &context, const YamlObject &algorithms)
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
73 private:
74 int createAlgorithm(Context &context, const YamlObject &data)
75 {
76 const auto &[name, algoData] = *data.asDict().begin();
77 std::unique_ptr<Algorithm<Module>> algo = createAlgorithm(name);
78 if (!algo) {
79 LOG(IPAModuleAlgo, Error)
80 << "Algorithm '" << name << "' not found";
81 return -EINVAL;
82 }
83
84 int ret = algo->init(context, algoData);
85 if (ret) {
86 LOG(IPAModuleAlgo, Error)
87 << "Algorithm '" << name << "' failed to initialize";
88 return ret;
89 }
90
91 LOG(IPAModuleAlgo, Debug)
92 << "Instantiated algorithm '" << name << "'";
93
94 algorithms_.push_back(std::move(algo));
95 return 0;
96 }
97
98 static std::unique_ptr<Algorithm<Module>> createAlgorithm(const std::string &name)
99 {
100 for (const AlgorithmFactoryBase<Module> *factory : factories()) {
101 if (factory->name() == name)
102 return factory->create();
103 }
104
105 return nullptr;
106 }
107
108 static std::vector<AlgorithmFactoryBase<Module> *> &factories()
109 {
110 /*
111 * The static factories map is defined inside the function to ensure
112 * it gets initialized on first use, without any dependency on
113 * link order.
114 */
115 static std::vector<AlgorithmFactoryBase<Module> *> factories;
116 return factories;
117 }
118
119 std::list<std::unique_ptr<Algorithm<Module>>> algorithms_;
120};
121
122} /* namespace ipa */
123
124} /* namespace libcamera */
libcamera::Loggable
Base class to support log message extensions.
Definition log.h:92
libcamera::YamlObject
A class representing the tree structure of the YAML content.
Definition yaml_parser.h:28
libcamera::YamlObject::asDict
DictAdapter asDict() const
Wrap a dictionary YamlObject in an adapter that exposes iterators.
Definition yaml_parser.h:205
libcamera::ipa::Module
The base class for all IPA modules.
Definition module.h:31
libcamera::ipa::Module< IPAContext, IPAFrameContext, IPAConfigInfo, ipu3_uapi_params, ipu3_uapi_stats_3a >::FrameContext
IPAFrameContext FrameContext
Definition module.h:34
libcamera::ipa::Module::algorithms
const std::list< std::unique_ptr< Algorithm< Module > > > & algorithms() const
Retrieve the list of instantiated algorithms.
Definition module.h:41
libcamera::ipa::Module< IPAContext, IPAFrameContext, IPAConfigInfo, ipu3_uapi_params, ipu3_uapi_stats_3a >::Params
ipu3_uapi_params Params
Definition module.h:36
libcamera::ipa::Module::createAlgorithms
int createAlgorithms(Context &context, const YamlObject &algorithms)
Create algorithms from YAML configuration data.
Definition module.h:46
libcamera::ipa::Module< IPAContext, IPAFrameContext, IPAConfigInfo, ipu3_uapi_params, ipu3_uapi_stats_3a >::Context
IPAContext Context
Definition module.h:33
libcamera::ipa::Module< IPAContext, IPAFrameContext, IPAConfigInfo, ipu3_uapi_params, ipu3_uapi_stats_3a >::Stats
ipu3_uapi_stats_3a Stats
Definition module.h:37
libcamera::ipa::Module::registerAlgorithm
static void registerAlgorithm(AlgorithmFactoryBase< Module > *factory)
Add an algorithm factory class to the list of available algorithms.
Definition module.h:68
libcamera::ipa::Module< IPAContext, IPAFrameContext, IPAConfigInfo, ipu3_uapi_params, ipu3_uapi_stats_3a >::Config
IPAConfigInfo Config
Definition module.h:35
algorithm.h
Algorithm common interface.
log.h
Logging infrastructure.
LOG_DECLARE_CATEGORY
#define LOG_DECLARE_CATEGORY(name)
Declare a category of log messages.
Definition log.h:51
LOG
#define LOG(category, severity)
Log a message.
Definition log.h:123
libcamera::ipa
The IPA (Image Processing Algorithm) namespace.
Definition af.cpp:58
libcamera
Top-level libcamera namespace.
Definition backtrace.h:17
utils.h
Miscellaneous utility functions.
libcamera::utils::enumerate
auto enumerate(T &iterable) -> details::enumerate_adapter< decltype(iterable.begin())>
Wrap an iterable to enumerate index and value in a range-based loop.
Definition utils.h:357
yaml_parser.h
A YAML parser helper.