libcamera: src/ipa/libipa/module.h Source File

libcamera v0.2.0 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 * module.h - 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
22namespace libcamera {
23
24 LOG_DECLARE_CATEGORY(IPAModuleAlgo)
25
26namespace ipa {
27
28template<typename _Context, typename _FrameContext, typename _Config,
29 typename _Params, typename _Stats>
30 class 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
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
73private:
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:91
libcamera::YamlObject
A class representing the tree structure of the YAML content.
Definition yaml_parser.h:26
libcamera::YamlObject::asDict
DictAdapter asDict() const
Wrap a dictionary YamlObject in an adapter that exposes iterators.
Definition yaml_parser.h:206
libcamera::ipa::Module
The base class for all IPA modules.
Definition module.h:31
libcamera::ipa::Module::Stats
_Stats Stats
The type of the IPA statistics and ISP results.
Definition module.h:37
libcamera::ipa::Module::FrameContext
_FrameContext FrameContext
The type of the frame context.
Definition module.h:34
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::Config
_Config Config
The type of the IPA configuration data.
Definition module.h:35
libcamera::ipa::Module::Context
_Context Context
The type of the shared IPA context.
Definition module.h:33
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::createAlgorithms
int createAlgorithms(Context &context, const YamlObject &algorithms)
Create algorithms from YAML configuration data.
Definition module.h:46
libcamera::ipa::Module::Params
_Params Params
The type of the ISP specific parameters.
Definition module.h:36
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:47
LOG
#define LOG(category, severity)
Log a message.
Definition log.h:122
libcamera
Top-level libcamera namespace.
Definition backtrace.h:17
utils.h
Miscellaneous utility functions.
yaml_parser.h
A YAML parser helper.