libcamera v0.7.1 Supporting cameras in Linux since 2019

Loading...
Searching...
No Matches
v4l2_params.h
1/* SPDX-License-Identifier: LGPL-2.1-or-later */
2/*
3 * Copyright (C) 2025, Ideas On Board
4 *
5 * V4L2 Parameters
6 */
7
8#pragma once
9
10#include <map>
11#include <stdint.h>
12#include <string.h>
13
14#include <linux/media/v4l2-isp.h>
15
16#include <libcamera/base/log.h>
17#include <libcamera/base/span.h>
18
19 namespace libcamera {
20
21 namespace ipa {
22
23 LOG_DECLARE_CATEGORY(V4L2Params)
24
25 template<typename T>
26 class V4L2ParamsBlock
27{
28 public:
29 V4L2ParamsBlock(const Span<uint8_t> data)
30 : data_(data)
31 {
32 }
33
34 virtual ~V4L2ParamsBlock() {}
35
36 virtual void setEnabled(bool enabled)
37 {
38 struct v4l2_isp_params_block_header *header =
39 reinterpret_cast<struct v4l2_isp_params_block_header *>(data_.data());
40
41 header->flags &= ~(V4L2_ISP_PARAMS_FL_BLOCK_ENABLE |
42 V4L2_ISP_PARAMS_FL_BLOCK_DISABLE);
43 header->flags |= enabled ? V4L2_ISP_PARAMS_FL_BLOCK_ENABLE
44 : V4L2_ISP_PARAMS_FL_BLOCK_DISABLE;
45 }
46
47 virtual const T *operator->() const
48 {
49 return reinterpret_cast<const T *>(data_.data());
50 }
51
52 virtual T *operator->()
53 {
54 return reinterpret_cast<T *>(data_.data());
55 }
56
57 virtual const T &operator*() const
58 {
59 return *reinterpret_cast<const T *>(data_.data());
60 }
61
62 virtual T &operator*()
63 {
64 return *reinterpret_cast<T *>(data_.data());
65 }
66
67 protected:
68 Span<uint8_t> data_;
69};
70
71 template<typename Traits>
72 class V4L2Params
73{
74 public:
75 V4L2Params(Span<uint8_t> data, unsigned int version)
76 : data_(data)
77 {
78 struct v4l2_isp_params_buffer *params =
79 reinterpret_cast<struct v4l2_isp_params_buffer *>(data_.data());
80 params->data_size = 0;
81 params->version = version;
82
83 used_ = offsetof(struct v4l2_isp_params_buffer, data);
84 }
85
86 size_t bytesused() const { return used_; }
87
88 template<typename Traits::id_type Id>
89 auto block()
90 {
91 using Details = typename Traits::template id_to_details<Id>;
92
93 using Type = typename Details::type;
94 constexpr auto kernelId = Details::blockType;
95
96 auto data = block(Id, kernelId, sizeof(Type));
97 return V4L2ParamsBlock<Type>(data);
98 }
99
100 protected:
101 Span<uint8_t> block(typename Traits::id_type type,
102 unsigned int blockType, size_t blockSize)
103 {
104 /*
105 * Look up the block in the cache first. If an algorithm
106 * requests the same block type twice, it should get the same
107 * block.
108 */
109 auto cacheIt = blocks_.find(type);
110 if (cacheIt != blocks_.end())
111 return cacheIt->second;
112
113 /*
114 * Make sure we don't run out of space. Assert as otherwise
115 * we get a segfault as soon as someone tries to access the
116 * empty Span<> returned from here.
117 */
118 if (blockSize > data_.size() - used_) {
119 LOG(Fatal)
120 << "Parameters buffer out of space; potential version mismatch between driver and libcamera";
121 return {};
122 }
123
124 /* Allocate a new block, clear its memory, and initialize its header. */
125 Span<uint8_t> block = data_.subspan(used_, blockSize);
126 memset(block.data(), 0, block.size());
127
128 struct v4l2_isp_params_block_header *header =
129 reinterpret_cast<struct v4l2_isp_params_block_header *>(block.data());
130 header->type = blockType;
131 header->size = block.size();
132
133 used_ += block.size();
134
135 reinterpret_cast<struct v4l2_isp_params_buffer *>
136 (data_.data())->data_size += block.size();
137
138 /* Update the cache. */
139 blocks_[type] = block;
140
141 return block;
142 }
143
144 Span<uint8_t> data_;
145 size_t used_;
146
147 std::map<typename Traits::id_type, Span<uint8_t>> blocks_;
148};
149
150} /* namespace ipa */
151
152} /* 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