Loading...
Searching...
No Matches
v4l2_params.h
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
* Copyright (C) 2025, Ideas On Board
*
* V4L2 Parameters
*/
#pragma once
#include <map>
#include <stdint.h>
#include <string.h>
#include <linux/media/v4l2-isp.h>
#include <libcamera/base/log.h>
#include <libcamera/base/span.h>
namespace libcamera {
namespace ipa {
LOG_DECLARE_CATEGORY(V4L2Params)
template<typename T>
class V4L2ParamsBlock
{
public:
V4L2ParamsBlock(const Span<uint8_t> data)
: data_(data)
{
}
virtual ~V4L2ParamsBlock() {}
virtual void setEnabled(bool enabled)
{
struct v4l2_isp_params_block_header *header =
reinterpret_cast<struct v4l2_isp_params_block_header *>(data_.data());
header->flags &= ~(V4L2_ISP_PARAMS_FL_BLOCK_ENABLE |
V4L2_ISP_PARAMS_FL_BLOCK_DISABLE);
header->flags |= enabled ? V4L2_ISP_PARAMS_FL_BLOCK_ENABLE
: V4L2_ISP_PARAMS_FL_BLOCK_DISABLE;
}
virtual const T *operator->() const
{
return reinterpret_cast<const T *>(data_.data());
}
virtual T *operator->()
{
return reinterpret_cast<T *>(data_.data());
}
virtual const T &operator*() const
{
return *reinterpret_cast<const T *>(data_.data());
}
virtual T &operator*()
{
return *reinterpret_cast<T *>(data_.data());
}
protected:
Span<uint8_t> data_;
};
template<typename Traits>
class V4L2Params
{
public:
V4L2Params(Span<uint8_t> data, unsigned int version)
: data_(data)
{
struct v4l2_isp_params_buffer *params =
reinterpret_cast<struct v4l2_isp_params_buffer *>(data_.data());
params->data_size = 0;
params->version = version;
used_ = offsetof(struct v4l2_isp_params_buffer, data);
}
size_t bytesused() const { return used_; }
template<typename Traits::id_type Id>
auto block()
{
using Details = typename Traits::template id_to_details<Id>;
using Type = typename Details::type;
constexpr auto kernelId = Details::blockType;
auto data = block(Id, kernelId, sizeof(Type));
return V4L2ParamsBlock<Type>(data);
}
protected:
Span<uint8_t> block(typename Traits::id_type type,
unsigned int blockType, size_t blockSize)
{
/*
* Look up the block in the cache first. If an algorithm
* requests the same block type twice, it should get the same
* block.
*/
auto cacheIt = blocks_.find(type);
if (cacheIt != blocks_.end())
return cacheIt->second;
/*
* Make sure we don't run out of space. Assert as otherwise
* we get a segfault as soon as someone tries to access the
* empty Span<> returned from here.
*/
if (blockSize > data_.size() - used_) {
LOG(Fatal)
<< "Parameters buffer out of space; potential version mismatch between driver and libcamera";
return {};
}
/* Allocate a new block, clear its memory, and initialize its header. */
Span<uint8_t> block = data_.subspan(used_, blockSize);
memset(block.data(), 0, block.size());
struct v4l2_isp_params_block_header *header =
reinterpret_cast<struct v4l2_isp_params_block_header *>(block.data());
header->type = blockType;
header->size = block.size();
used_ += block.size();
reinterpret_cast<struct v4l2_isp_params_buffer *>
(data_.data())->data_size += block.size();
/* Update the cache. */
blocks_[type] = block;
return block;
}
Span<uint8_t> data_;
size_t used_;
std::map<typename Traits::id_type, Span<uint8_t>> blocks_;
};
} /* namespace ipa */
} /* namespace libcamera */
libcamera::ipa::V4L2ParamsBlock
Helper class that represents an ISP configuration block.
Definition v4l2_params.h:27
libcamera::ipa::V4L2ParamsBlock::operator->
virtual T * operator->()
Access the ISP configuration block casting it to the kernel-defined ISP configuration type.
Definition v4l2_params.h:52
libcamera::ipa::V4L2ParamsBlock::data_
Span< uint8_t > data_
Memory area reserved for the ISP configuration block.
Definition v4l2_params.h:68
libcamera::ipa::V4L2ParamsBlock::setEnabled
virtual void setEnabled(bool enabled)
Enable/disable an ISP configuration block.
Definition v4l2_params.h:36
libcamera::ipa::V4L2ParamsBlock::V4L2ParamsBlock
V4L2ParamsBlock(const Span< uint8_t > data)
Construct a V4L2ParamsBlock with memory represented by data.
Definition v4l2_params.h:29
libcamera::ipa::V4L2ParamsBlock::operator*
virtual const T & operator*() const
Access the ISP configuration block casting it to the kernel-defined ISP configuration type.
Definition v4l2_params.h:57
libcamera::ipa::V4L2ParamsBlock::operator->
virtual const T * operator->() const
Access the ISP configuration block casting it to the kernel-defined ISP configuration type.
Definition v4l2_params.h:47
libcamera::ipa::V4L2ParamsBlock::operator*
virtual T & operator*()
Access the ISP configuration block casting it to the kernel-defined ISP configuration type.
Definition v4l2_params.h:62
libcamera::ipa::V4L2Params
Helper class that represent an ISP configuration buffer.
Definition v4l2_params.h:73
libcamera::ipa::V4L2Params::block
Span< uint8_t > block(typename Traits::id_type type, unsigned int blockType, size_t blockSize)
Populate an ISP configuration block a returns a reference to its memory.
Definition v4l2_params.h:101
libcamera::ipa::V4L2Params::blocks_
std::map< typename Traits::id_type, Span< uint8_t > > blocks_
Cache of ISP configuration blocks.
Definition v4l2_params.h:147
libcamera::ipa::V4L2Params::data_
Span< uint8_t > data_
The ISP parameters buffer memory.
Definition v4l2_params.h:144
libcamera::ipa::V4L2Params::V4L2Params
V4L2Params(Span< uint8_t > data, unsigned int version)
Construct an instance of V4L2Params.
Definition v4l2_params.h:75
libcamera::ipa::V4L2Params::block
auto block()
Retrieve the location of an ISP configuration block a return it.
Definition v4l2_params.h:89
libcamera::ipa::V4L2Params::used_
size_t used_
The number of bytes used in the parameters buffer.
Definition v4l2_params.h:145
libcamera::ipa::V4L2Params::bytesused
size_t bytesused() const
Retrieve the used size of the parameters buffer (in bytes).
Definition v4l2_params.h:86
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