summaryrefslogtreecommitdiff
path: root/test/serialization
AgeCommit message (Expand)Author
2021-08-12test: control serialization: Test lookup by ControlIdJacopo Mondi
2021-08-12libcamera: controls: Create ControlInfoMap with ControlIdMapJacopo Mondi
2021-06-25libcamera/base: Validate internal headers as privateKieran Bingham
2021-06-25libcamera/base: Move extended base functionalityKieran Bingham
2021-03-02tests: Test IPA serializer generationPaul Elder
2021-03-02tests: Add IPADataSerializer testPaul Elder
2021-02-11meson: Fix coding style when declaring arraysLaurent Pinchart
2020-08-05libcamera: pipelines: Use sensor ID as camera nameNiklas Söderlund
2020-05-16libcamera: Move internal headers to include/libcamera/internal/Laurent Pinchart
2020-05-13licenses: License all meson files under CC0-1.0Laurent Pinchart
2020-04-27test: Use float values for brightness, contrast and saturationLaurent Pinchart
2020-03-20libcamera: controls: Rename ControlRange to ControlInfoLaurent Pinchart
2019-11-20test: Add control serialization testJacopo Mondi
ef='#n123'>123 124
/* 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();
		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 */