libcamera: src/ipa/libipa/v4l2_params.h Source File

libcamera v0.7.2 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 ISP 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#include <libcamera/base/utils.h>
19
20namespace libcamera {
21
22namespace ipa {
23
24 LOG_DECLARE_CATEGORY(V4L2Params)
25
26template<typename T>
27 class V4L2ParamsBlock
28{
29public:
30 V4L2ParamsBlock(const Span<uint8_t> data)
31 : data_(data)
32 {
33 }
34
35 virtual ~V4L2ParamsBlock() {}
36
37 virtual void setEnabled(bool enabled)
38 {
39 struct v4l2_isp_params_block_header *header =
40 reinterpret_cast<struct v4l2_isp_params_block_header *>(data_.data());
41
42 header->flags &= ~(V4L2_ISP_PARAMS_FL_BLOCK_ENABLE |
43 V4L2_ISP_PARAMS_FL_BLOCK_DISABLE);
44 header->flags |= enabled ? V4L2_ISP_PARAMS_FL_BLOCK_ENABLE
45 : V4L2_ISP_PARAMS_FL_BLOCK_DISABLE;
46 }
47
48 virtual const T *operator->() const
49 {
50 return reinterpret_cast<const T *>(data_.data());
51 }
52
53 virtual T *operator->()
54 {
55 return reinterpret_cast<T *>(data_.data());
56 }
57
58 virtual const T &operator*() const
59 {
60 return *reinterpret_cast<const T *>(data_.data());
61 }
62
63 virtual T &operator*()
64 {
65 return *reinterpret_cast<T *>(data_.data());
66 }
67
68protected:
69 Span<uint8_t> data_;
70};
71
72 class V4L2ParamsBase
73{
74protected:
75 V4L2ParamsBase(Span<uint8_t> data, unsigned int version);
76
77public:
78 size_t bytesused() const { return used_; }
79
80protected:
81 Span<uint8_t> block(uint16_t type, unsigned int blockType,
82 size_t blockSize);
83
84 Span<uint8_t> data_;
85 size_t used_;
86
87 std::map<uint16_t, Span<uint8_t>> blocks_;
88};
89
90template<typename Traits>
91 class V4L2Params : public V4L2ParamsBase
92{
93public:
94 static_assert(std::is_same_v<std::underlying_type_t<typename Traits::id_type>, uint16_t>);
95
96 V4L2Params(Span<uint8_t> data, unsigned int version)
97 : V4L2ParamsBase(data, version)
98 {
99 }
100
101 template<typename Traits::id_type Id>
102 auto block()
103 {
104 using Details = typename Traits::template id_to_details<Id>;
105
106 using Type = typename Details::type;
107 constexpr auto kernelId = Details::blockType;
108
109 auto data = V4L2ParamsBase::block(utils::to_underlying(Id),
110 kernelId, sizeof(Type));
111 return V4L2ParamsBlock<Type>(data);
112 }
113};
114
115} /* namespace ipa */
116
117} /* namespace libcamera */
libcamera::ipa::V4L2ParamsBase::V4L2ParamsBase
V4L2ParamsBase(Span< uint8_t > data, unsigned int version)
Construct an instance of V4L2ParamsBase.
Definition v4l2_params.cpp:134
libcamera::ipa::V4L2ParamsBase::data_
Span< uint8_t > data_
The ISP parameters buffer memory.
Definition v4l2_params.h:84
libcamera::ipa::V4L2ParamsBase::used_
size_t used_
The number of bytes used in the parameters buffer.
Definition v4l2_params.h:85
libcamera::ipa::V4L2ParamsBase::block
Span< uint8_t > block(uint16_t type, unsigned int blockType, size_t blockSize)
Populate an ISP configuration block a returns a reference to its memory.
Definition v4l2_params.cpp:170
libcamera::ipa::V4L2ParamsBase::bytesused
size_t bytesused() const
Retrieve the used size of the parameters buffer (in bytes).
Definition v4l2_params.h:78
libcamera::ipa::V4L2ParamsBase::blocks_
std::map< uint16_t, Span< uint8_t > > blocks_
Cache of ISP configuration blocks.
Definition v4l2_params.h:87
libcamera::ipa::V4L2ParamsBlock
Helper class that represents an ISP configuration block.
Definition v4l2_params.h:28
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:53
libcamera::ipa::V4L2ParamsBlock::data_
Span< uint8_t > data_
Memory area reserved for the ISP configuration block.
Definition v4l2_params.h:69
libcamera::ipa::V4L2ParamsBlock::setEnabled
virtual void setEnabled(bool enabled)
Enable/disable an ISP configuration block.
Definition v4l2_params.h:37
libcamera::ipa::V4L2ParamsBlock::V4L2ParamsBlock
V4L2ParamsBlock(const Span< uint8_t > data)
Construct a V4L2ParamsBlock with memory represented by data.
Definition v4l2_params.h:30
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:58
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:48
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:63
libcamera::ipa::V4L2Params
Helper class that represent an ISP configuration buffer.
Definition v4l2_params.h:92
libcamera::ipa::V4L2Params::V4L2Params
V4L2Params(Span< uint8_t > data, unsigned int version)
Construct an instance of V4L2Params.
Definition v4l2_params.h:96
libcamera::ipa::V4L2Params::block
auto block()
Retrieve the location of an ISP configuration block a return it.
Definition v4l2_params.h:102
log.h
Logging infrastructure.
LOG_DECLARE_CATEGORY
#define LOG_DECLARE_CATEGORY(name)
Declare a category of log messages.
Definition log.h:51
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::to_underlying
constexpr std::underlying_type_t< Enum > to_underlying(Enum e) noexcept
Convert an enumeration to its underlygin type.
Definition utils.h:379