libcamera  v0.3.1+12-19bbca3c
Supporting cameras in Linux since 2019
module.h
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 
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 
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 */
_Config Config
The type of the IPA configuration data.
Definition: module.h:35
#define LOG(category, severity)
Log a message.
_Params Params
The type of the ISP specific parameters.
Definition: module.h:36
const std::list< std::unique_ptr< Algorithm< Module > > > & algorithms() const
Retrieve the list of instantiated algorithms.
Definition: module.h:41
Base class to support log message extensions.
Definition: log.h:91
Top-level libcamera namespace.
Definition: backtrace.h:17
ListAdapter asList() const
Wrap a list YamlObject in an adapter that exposes iterators.
Definition: yaml_parser.h:196
_FrameContext FrameContext
The type of the frame context.
Definition: module.h:34
Miscellaneous utility functions.
#define LOG_DECLARE_CATEGORY(name)
Declare a category of log messages.
_Stats Stats
The type of the IPA statistics and ISP results.
Definition: module.h:37
int createAlgorithms(Context &context, const YamlObject &algorithms)
Create algorithms from YAML configuration data.
Definition: module.h:46
static void registerAlgorithm(AlgorithmFactoryBase< Module > *factory)
Add an algorithm factory class to the list of available algorithms.
Definition: module.h:68
_Context Context
The type of the shared IPA context.
Definition: module.h:33
DictAdapter asDict() const
Wrap a dictionary YamlObject in an adapter that exposes iterators.
Definition: yaml_parser.h:195
A class representing the tree structure of the YAML content.
Definition: yaml_parser.h:25
Logging infrastructure.
The base class for all IPA modules.
Definition: module.h:30
A YAML parser helper.