libcamera v0.7.0+1-4ceceb68
Supporting cameras in Linux since 2019
Loading...
Searching...
No Matches
v4l2_subdevice.h
Go to the documentation of this file.
1/* SPDX-License-Identifier: LGPL-2.1-or-later */
2/*
3 * Copyright (C) 2019, Google Inc.
4 *
5 * V4L2 Subdevice
6 */
7
8#pragma once
9
10#include <memory>
11#include <optional>
12#include <ostream>
13#include <stdint.h>
14#include <string>
15#include <vector>
16
17#include <linux/v4l2-subdev.h>
18
20#include <libcamera/base/log.h>
21#include <libcamera/base/regex.h>
22
24#include <libcamera/geometry.h>
25
29
30namespace libcamera {
31
32class MediaDevice;
33
35{
36public:
37 enum class Type {
38 Image,
41 };
42
43 bool isValid() const { return code != 0; }
44
45 static const MediaBusFormatInfo &info(uint32_t code);
46
47 const char *name;
48 uint32_t code;
50 unsigned int bitsPerPixel;
52};
53
54struct V4L2SubdeviceCapability final : v4l2_subdev_capability {
55 bool isReadOnly() const
56 {
57 return capabilities & V4L2_SUBDEV_CAP_RO_SUBDEV;
58 }
59 bool hasStreams() const
60 {
61 return capabilities & V4L2_SUBDEV_CAP_STREAMS;
62 }
63};
64
66 uint32_t code;
68 std::optional<ColorSpace> colorSpace;
69
70 std::string toString() const;
71};
72
73std::ostream &operator<<(std::ostream &out, const V4L2SubdeviceFormat &f);
74
76{
77public:
78 using Formats = std::map<unsigned int, std::vector<SizeRange>>;
79
80 enum Whence {
81 TryFormat = V4L2_SUBDEV_FORMAT_TRY,
82 ActiveFormat = V4L2_SUBDEV_FORMAT_ACTIVE,
83 };
84
85 struct Stream {
87 : pad(0), stream(0)
88 {
89 }
90
91 Stream(unsigned int p, unsigned int s)
92 : pad(p), stream(s)
93 {
94 }
95
96 unsigned int pad;
97 unsigned int stream;
98 };
99
100 struct Route {
102 : flags(0)
103 {
104 }
105
106 Route(const Stream &snk, const Stream &src, uint32_t f)
107 : sink(snk), source(src), flags(f)
108 {
109 }
110
113 uint32_t flags;
114 };
115
116 using Routing = std::vector<Route>;
117
118 explicit V4L2Subdevice(const MediaEntity *entity);
120
121 int open();
122
123 const MediaEntity *entity() const { return entity_; }
124
125 int getSelection(const Stream &stream, unsigned int target,
126 Rectangle *rect);
127 int getSelection(unsigned int pad, unsigned int target, Rectangle *rect)
128 {
129 return getSelection({ pad, 0 }, target, rect);
130 }
131 int setSelection(const Stream &stream, unsigned int target,
132 Rectangle *rect);
133 int setSelection(unsigned int pad, unsigned int target, Rectangle *rect)
134 {
135 return setSelection({ pad, 0 }, target, rect);
136 }
137
138 Formats formats(const Stream &stream);
139 Formats formats(unsigned int pad)
140 {
141 return formats({ pad, 0 });
142 }
143
144 int getFormat(const Stream &stream, V4L2SubdeviceFormat *format,
145 Whence whence = ActiveFormat);
146 int getFormat(unsigned int pad, V4L2SubdeviceFormat *format,
147 Whence whence = ActiveFormat)
148 {
149 return getFormat({ pad, 0 }, format, whence);
150 }
151 int setFormat(const Stream &stream, V4L2SubdeviceFormat *format,
152 Whence whence = ActiveFormat);
153 int setFormat(unsigned int pad, V4L2SubdeviceFormat *format,
154 Whence whence = ActiveFormat)
155 {
156 return setFormat({ pad, 0 }, format, whence);
157 }
158
159 int getRouting(Routing *routing, Whence whence = ActiveFormat);
160 int setRouting(Routing *routing, Whence whence = ActiveFormat);
161
162 const std::string &model();
163 const V4L2SubdeviceCapability &caps() const { return caps_; }
164
165 static std::unique_ptr<V4L2Subdevice>
166 fromEntityName(const MediaDevice *media, const std::string &entity);
167 static std::unique_ptr<V4L2Subdevice>
168 fromEntityName(const MediaDevice *media, const std::regex &entity);
169
170protected:
171 std::string logPrefix() const override;
172
173private:
175
176 std::optional<ColorSpace>
177 toColorSpace(const v4l2_mbus_framefmt &format) const;
178
179 std::vector<unsigned int> enumPadCodes(const Stream &stream);
180 std::vector<SizeRange> enumPadSizes(const Stream &stream,
181 unsigned int code);
182
183 int getRoutingLegacy(Routing *routing, Whence whence);
184 int setRoutingLegacy(Routing *routing, Whence whence);
185
186 const MediaEntity *entity_;
187
188 std::string model_;
189 struct V4L2SubdeviceCapability caps_;
190};
191
192bool operator==(const V4L2Subdevice::Stream &lhs, const V4L2Subdevice::Stream &rhs);
193static inline bool operator!=(const V4L2Subdevice::Stream &lhs,
194 const V4L2Subdevice::Stream &rhs)
195{
196 return !(lhs == rhs);
197}
198
199std::ostream &operator<<(std::ostream &out, const V4L2Subdevice::Stream &stream);
200std::ostream &operator<<(std::ostream &out, const V4L2Subdevice::Route &route);
201std::ostream &operator<<(std::ostream &out, const V4L2Subdevice::Routing &routing);
202
203} /* namespace libcamera */
Utilities to help constructing class interfaces.
#define LIBCAMERA_DISABLE_COPY(klass)
Disable copy construction and assignment of the klass.
Definition class.h:27
Information about media bus formats.
Definition v4l2_subdevice.h:35
unsigned int bitsPerPixel
The average number of bits per pixel.
Definition v4l2_subdevice.h:50
bool isValid() const
Check if the media bus format info is valid.
Definition v4l2_subdevice.h:43
uint32_t code
The media bus format code described by this instance (MEDIA_BUS_FMT_*)
Definition v4l2_subdevice.h:48
Type type
The media bus format type.
Definition v4l2_subdevice.h:49
const char * name
The format name as a human-readable string, used as the text representation of the format.
Definition v4l2_subdevice.h:47
static const MediaBusFormatInfo & info(uint32_t code)
Retrieve information about a media bus format.
Definition v4l2_subdevice.cpp:824
Type
The format type.
Definition v4l2_subdevice.h:37
@ EmbeddedData
The format describes sensor embedded data.
@ Image
The format describes image data.
@ Metadata
The format describes generic metadata.
PixelFormatInfo::ColourEncoding colourEncoding
The colour encoding type.
Definition v4l2_subdevice.h:51
The MediaDevice represents a Media Controller device with its full graph of connected objects.
Definition media_device.h:28
The MediaEntity represents an entity in the media graph.
Definition media_object.h:97
ColourEncoding
The colour encoding type.
Definition formats.h:23
Describe a rectangle's position and dimensions.
Definition geometry.h:247
Describe a two-dimensional size.
Definition geometry.h:51
Video stream for a camera.
Definition stream.h:76
Base class for V4L2VideoDevice and V4L2Subdevice.
Definition v4l2_device.h:34
A V4L2 subdevice as exposed by the Linux kernel.
Definition v4l2_subdevice.h:76
std::vector< Route > Routing
V4L2 subdevice routing table.
Definition v4l2_subdevice.h:116
static std::unique_ptr< V4L2Subdevice > fromEntityName(const MediaDevice *media, const std::string &entity)
Create a new video subdevice instance from entity in media device media.
Definition v4l2_subdevice.cpp:1753
int getSelection(unsigned int pad, unsigned int target, Rectangle *rect)
Get selection rectangle rect for target.
Definition v4l2_subdevice.h:127
int open()
Open a V4L2 subdevice.
Definition v4l2_subdevice.cpp:1125
int setFormat(unsigned int pad, V4L2SubdeviceFormat *format, Whence whence=ActiveFormat)
Set an image format on one of the V4L2 subdevice pads.
Definition v4l2_subdevice.h:153
std::string logPrefix() const override
Retrieve a string to be prefixed to the log message.
Definition v4l2_subdevice.cpp:1781
int setRouting(Routing *routing, Whence whence=ActiveFormat)
Set a routing table on the V4L2 subdevice.
Definition v4l2_subdevice.cpp:1629
int setSelection(const Stream &stream, unsigned int target, Rectangle *rect)
Set selection rectangle rect for target.
Definition v4l2_subdevice.cpp:1227
const V4L2SubdeviceCapability & caps() const
Retrieve the subdevice V4L2 capabilities.
Definition v4l2_subdevice.h:163
int getSelection(const Stream &stream, unsigned int target, Rectangle *rect)
Get selection rectangle rect for target.
Definition v4l2_subdevice.cpp:1179
std::map< unsigned int, std::vector< SizeRange > > Formats
A map of supported media bus formats to frame sizes.
Definition v4l2_subdevice.h:78
int setSelection(unsigned int pad, unsigned int target, Rectangle *rect)
Set selection rectangle rect for target.
Definition v4l2_subdevice.h:133
Whence
Specify the type of format for getFormat() and setFormat() operations.
Definition v4l2_subdevice.h:80
@ ActiveFormat
The format operation applies to ACTIVE formats.
Definition v4l2_subdevice.h:82
@ TryFormat
The format operation applies to TRY formats.
Definition v4l2_subdevice.h:81
Formats formats(unsigned int pad)
Enumerate all media bus codes and frame sizes on a pad.
Definition v4l2_subdevice.h:139
Formats formats(const Stream &stream)
Enumerate all media bus codes and frame sizes on a stream.
Definition v4l2_subdevice.cpp:1281
int setFormat(const Stream &stream, V4L2SubdeviceFormat *format, Whence whence=ActiveFormat)
Set an image format on one of the V4L2 subdevice pads.
Definition v4l2_subdevice.cpp:1403
int getFormat(unsigned int pad, V4L2SubdeviceFormat *format, Whence whence=ActiveFormat)
Retrieve the image format set on one of the V4L2 subdevice pads.
Definition v4l2_subdevice.h:146
const std::string & model()
Retrieve the model name of the device.
Definition v4l2_subdevice.cpp:1703
int getFormat(const Stream &stream, V4L2SubdeviceFormat *format, Whence whence=ActiveFormat)
Retrieve the image format set on one of the V4L2 subdevice streams.
Definition v4l2_subdevice.cpp:1356
int getRouting(Routing *routing, Whence whence=ActiveFormat)
Retrieve the subdevice's internal routing table.
Definition v4l2_subdevice.cpp:1539
const MediaEntity * entity() const
Retrieve the media entity associated with the subdevice.
Definition v4l2_subdevice.h:123
Class and enums to represent color spaces.
Data structures related to geometric objects.
Types and helper functions to handle libcamera image formats.
Logging infrastructure.
Provides a class hierarchy that represents the media objects exposed by the Linux kernel Media Contro...
Top-level libcamera namespace.
Definition backtrace.h:17
std::ostream & operator<<(std::ostream &out, const Point &p)
Insert a text representation of a Point into an output stream.
Definition geometry.cpp:91
struct v4l2_subdev_capability object wrapper and helpers
Definition v4l2_subdevice.h:54
bool isReadOnly() const
Retrieve if a subdevice is registered as read-only.
Definition v4l2_subdevice.h:55
bool hasStreams() const
Retrieve if a subdevice supports the V4L2 streams API.
Definition v4l2_subdevice.h:59
The V4L2 sub-device image format and sizes.
Definition v4l2_subdevice.h:65
std::optional< ColorSpace > colorSpace
The color space of the pixels.
Definition v4l2_subdevice.h:68
uint32_t code
The image format bus code.
Definition v4l2_subdevice.h:66
Size size
The image size in pixels.
Definition v4l2_subdevice.h:67
std::string toString() const
Assemble and return a string describing the format.
Definition v4l2_subdevice.cpp:920
V4L2 subdevice routing table entry.
Definition v4l2_subdevice.h:100
Stream sink
The sink stream of the route.
Definition v4l2_subdevice.h:111
Route()
Construct a Route with default streams.
Definition v4l2_subdevice.h:101
uint32_t flags
The route flags (V4L2_SUBDEV_ROUTE_FL_*)
Definition v4l2_subdevice.h:113
Stream source
The source stream of the route.
Definition v4l2_subdevice.h:112
Route(const Stream &snk, const Stream &src, uint32_t f)
Construct a Route from sink to source.
Definition v4l2_subdevice.h:106
V4L2 subdevice stream.
Definition v4l2_subdevice.h:85
Stream(unsigned int p, unsigned int s)
Construct a Stream with a given pad and stream number.
Definition v4l2_subdevice.h:91
unsigned int stream
The stream number.
Definition v4l2_subdevice.h:97
Stream()
Construct a Stream with pad and stream set to 0.
Definition v4l2_subdevice.h:86
unsigned int pad
The 0-indexed pad number.
Definition v4l2_subdevice.h:96
Common base for V4L2 devices and subdevices.