Loading...
Searching...
No Matches
module.h
Go to the documentation of this file.
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
* Copyright (C) 2022, Ideas On Board
*
* IPA module
*/
#pragma once
#include <list>
#include <memory>
#include <string>
#include <vector>
#include <libcamera/base/log.h>
#include <libcamera/base/utils.h>
#include "libcamera/internal/yaml_parser.h"
#include "algorithm.h"
namespace libcamera {
LOG_DECLARE_CATEGORY(IPAModuleAlgo)
namespace ipa {
template<typename _Context, typename _FrameContext, typename _Config,
typename _Params, typename _Stats>
class Module : public Loggable
{
public:
using Context = _Context;
using FrameContext = _FrameContext;
using Config = _Config;
using Params = _Params;
using Stats = _Stats;
virtual ~Module() {}
const std::list<std::unique_ptr<Algorithm<Module>>> &algorithms() const
{
return algorithms_;
}
int createAlgorithms(Context &context, const YamlObject &algorithms)
{
const auto &list = algorithms.asList();
for (const auto &[i, algo] : utils::enumerate(list)) {
if (!algo.isDictionary()) {
LOG(IPAModuleAlgo, Error)
<< "Invalid YAML syntax for algorithm " << i;
algorithms_.clear();
return -EINVAL;
}
int ret = createAlgorithm(context, algo);
if (ret) {
algorithms_.clear();
return ret;
}
}
return 0;
}
static void registerAlgorithm(AlgorithmFactoryBase<Module> *factory)
{
factories().push_back(factory);
}
private:
int createAlgorithm(Context &context, const YamlObject &data)
{
const auto &[name, algoData] = *data.asDict().begin();
/*
* Optionally, algorithms can be disabled via the tuning file
* by including enabled: false as a parameter within the
* algorithm tuning data. This is not an error, so we return 0.
*/
if (!algoData["enabled"].get<bool>(true)) {
LOG(IPAModuleAlgo, Info)
<< "Algorithm '" << name << "' disabled via tuning file";
return 0;
}
std::unique_ptr<Algorithm<Module>> algo = createAlgorithm(name);
if (!algo) {
LOG(IPAModuleAlgo, Error)
<< "Algorithm '" << name << "' not found";
return -EINVAL;
}
int ret = algo->init(context, algoData);
if (ret) {
LOG(IPAModuleAlgo, Error)
<< "Algorithm '" << name << "' failed to initialize";
return ret;
}
LOG(IPAModuleAlgo, Debug)
<< "Instantiated algorithm '" << name << "'";
algorithms_.push_back(std::move(algo));
return 0;
}
static std::unique_ptr<Algorithm<Module>> createAlgorithm(const std::string &name)
{
for (const AlgorithmFactoryBase<Module> *factory : factories()) {
if (factory->name() == name)
return factory->create();
}
return nullptr;
}
static std::vector<AlgorithmFactoryBase<Module> *> &factories()
{
/*
* The static factories map is defined inside the function to ensure
* it gets initialized on first use, without any dependency on
* link order.
*/
static std::vector<AlgorithmFactoryBase<Module> *> factories;
return factories;
}
std::list<std::unique_ptr<Algorithm<Module>>> algorithms_;
};
} /* namespace ipa */
} /* 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:129
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)
Wrap an iterable to enumerate index and value in a range-based loop.
Definition utils.h:316
yaml_parser.h
A YAML parser helper.