libcamera v0.0.0+3240-f2a18172-dirty (2021-12-20T12:34:02+00:00)
Supporting cameras in Linux since 2019
v4l2_videodevice.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_videodevice.h - V4L2 Video Device
6 */
7#ifndef __LIBCAMERA_INTERNAL_V4L2_VIDEODEVICE_H__
8#define __LIBCAMERA_INTERNAL_V4L2_VIDEODEVICE_H__
9
10#include <array>
11#include <atomic>
12#include <memory>
13#include <stdint.h>
14#include <string>
15#include <vector>
16
17#include <linux/videodev2.h>
18
20#include <libcamera/base/log.h>
22
24#include <libcamera/geometry.h>
26
30
31namespace libcamera {
32
33class EventNotifier;
34class FileDescriptor;
35class MediaDevice;
36class MediaEntity;
37
38struct V4L2Capability final : v4l2_capability {
39 const char *driver() const
40 {
41 return reinterpret_cast<const char *>(v4l2_capability::driver);
42 }
43 const char *card() const
44 {
45 return reinterpret_cast<const char *>(v4l2_capability::card);
46 }
47 const char *bus_info() const
48 {
49 return reinterpret_cast<const char *>(v4l2_capability::bus_info);
50 }
51 unsigned int device_caps() const
52 {
53 return capabilities & V4L2_CAP_DEVICE_CAPS
54 ? v4l2_capability::device_caps
55 : v4l2_capability::capabilities;
56 }
57 bool isMultiplanar() const
58 {
59 return device_caps() & (V4L2_CAP_VIDEO_CAPTURE_MPLANE |
60 V4L2_CAP_VIDEO_OUTPUT_MPLANE |
61 V4L2_CAP_VIDEO_M2M_MPLANE);
62 }
63 bool isCapture() const
64 {
65 return device_caps() & (V4L2_CAP_VIDEO_CAPTURE |
66 V4L2_CAP_VIDEO_CAPTURE_MPLANE |
67 V4L2_CAP_META_CAPTURE);
68 }
69 bool isOutput() const
70 {
71 return device_caps() & (V4L2_CAP_VIDEO_OUTPUT |
72 V4L2_CAP_VIDEO_OUTPUT_MPLANE |
73 V4L2_CAP_META_OUTPUT);
74 }
75 bool isVideo() const
76 {
77 return device_caps() & (V4L2_CAP_VIDEO_CAPTURE |
78 V4L2_CAP_VIDEO_CAPTURE_MPLANE |
79 V4L2_CAP_VIDEO_OUTPUT |
80 V4L2_CAP_VIDEO_OUTPUT_MPLANE);
81 }
82 bool isM2M() const
83 {
84 return device_caps() & (V4L2_CAP_VIDEO_M2M |
85 V4L2_CAP_VIDEO_M2M_MPLANE);
86 }
87 bool isMeta() const
88 {
89 return device_caps() & (V4L2_CAP_META_CAPTURE |
90 V4L2_CAP_META_OUTPUT);
91 }
92 bool isVideoCapture() const
93 {
94 return isVideo() && isCapture();
95 }
96 bool isVideoOutput() const
97 {
98 return isVideo() && isOutput();
99 }
100 bool isMetaCapture() const
101 {
102 return isMeta() && isCapture();
103 }
104 bool isMetaOutput() const
105 {
106 return isMeta() && isOutput();
107 }
108 bool hasStreaming() const
109 {
110 return device_caps() & V4L2_CAP_STREAMING;
111 }
113 {
114 return device_caps() & V4L2_CAP_IO_MC;
115 }
116};
117
119{
120public:
121 V4L2BufferCache(unsigned int numEntries);
122 V4L2BufferCache(const std::vector<std::unique_ptr<FrameBuffer>> &buffers);
124
125 int get(const FrameBuffer &buffer);
126 void put(unsigned int index);
127
128private:
129 class Entry
130 {
131 public:
132 Entry();
133 Entry(bool free, uint64_t lastUsed, const FrameBuffer &buffer);
134
135 bool operator==(const FrameBuffer &buffer) const;
136
137 bool free_;
138 uint64_t lastUsed_;
139
140 private:
141 struct Plane {
142 Plane(const FrameBuffer::Plane &plane)
143 : fd(plane.fd.fd()), length(plane.length)
144 {
145 }
146
147 int fd;
148 unsigned int length;
149 };
150
151 std::vector<Plane> planes_;
152 };
153
154 std::atomic<uint64_t> lastUsedCounter_;
155 std::vector<Entry> cache_;
156 /* \todo Expose the miss counter through an instrumentation API. */
157 unsigned int missCounter_;
158};
159
161{
162public:
163 struct Plane {
164 uint32_t size = 0;
165 uint32_t bpl = 0;
166 };
167
170
171 std::array<Plane, 3> planes;
172 unsigned int planesCount = 0;
173
174 const std::string toString() const;
175};
176
178{
179public:
180 using Formats = std::map<V4L2PixelFormat, std::vector<SizeRange>>;
181
182 explicit V4L2VideoDevice(const std::string &deviceNode);
183 explicit V4L2VideoDevice(const MediaEntity *entity);
185
186 int open();
187 int open(int handle, enum v4l2_buf_type type);
188 void close();
189
190 const char *driverName() const { return caps_.driver(); }
191 const char *deviceName() const { return caps_.card(); }
192 const char *busName() const { return caps_.bus_info(); }
193
194 const V4L2Capability &caps() const { return caps_; }
195
196 int getFormat(V4L2DeviceFormat *format);
197 int tryFormat(V4L2DeviceFormat *format);
198 int setFormat(V4L2DeviceFormat *format);
199 Formats formats(uint32_t code = 0);
200
201 int setSelection(unsigned int target, Rectangle *rect);
202
203 int allocateBuffers(unsigned int count,
204 std::vector<std::unique_ptr<FrameBuffer>> *buffers);
205 int exportBuffers(unsigned int count,
206 std::vector<std::unique_ptr<FrameBuffer>> *buffers);
207 int importBuffers(unsigned int count);
208 int releaseBuffers();
209
210 int queueBuffer(FrameBuffer *buffer);
212
213 int streamOn();
214 int streamOff();
215
216 static std::unique_ptr<V4L2VideoDevice>
217 fromEntityName(const MediaDevice *media, const std::string &entity);
218
219protected:
220 std::string logPrefix() const override;
221
222private:
224
225 int getFormatMeta(V4L2DeviceFormat *format);
226 int trySetFormatMeta(V4L2DeviceFormat *format, bool set);
227
228 int getFormatMultiplane(V4L2DeviceFormat *format);
229 int trySetFormatMultiplane(V4L2DeviceFormat *format, bool set);
230
231 int getFormatSingleplane(V4L2DeviceFormat *format);
232 int trySetFormatSingleplane(V4L2DeviceFormat *format, bool set);
233
234 std::vector<V4L2PixelFormat> enumPixelformats(uint32_t code);
235 std::vector<SizeRange> enumSizes(V4L2PixelFormat pixelFormat);
236
237 int requestBuffers(unsigned int count, enum v4l2_memory memoryType);
238 int createBuffers(unsigned int count,
239 std::vector<std::unique_ptr<FrameBuffer>> *buffers);
240 std::unique_ptr<FrameBuffer> createBuffer(unsigned int index);
241 FileDescriptor exportDmabufFd(unsigned int index, unsigned int plane);
242
243 void bufferAvailable();
244 FrameBuffer *dequeueBuffer();
245
246 V4L2Capability caps_;
247 V4L2DeviceFormat format_;
248 const PixelFormatInfo *formatInfo_;
249
250 enum v4l2_buf_type bufferType_;
251 enum v4l2_memory memoryType_;
252
253 V4L2BufferCache *cache_;
254 std::map<unsigned int, FrameBuffer *> queuedBuffers_;
255
256 EventNotifier *fdBufferNotifier_;
257
258 bool streaming_;
259};
260
262{
263public:
264 V4L2M2MDevice(const std::string &deviceNode);
266
267 int open();
268 void close();
269
270 V4L2VideoDevice *output() { return output_; }
271 V4L2VideoDevice *capture() { return capture_; }
272
273private:
274 std::string deviceNode_;
275
276 V4L2VideoDevice *output_;
277 V4L2VideoDevice *capture_;
278};
279
280} /* namespace libcamera */
281
282#endif /* __LIBCAMERA_INTERNAL_V4L2_VIDEODEVICE_H__ */
Utilities to help constructing class interfaces.
#define LIBCAMERA_DISABLE_COPY(klass)
Disable copy construction and assignment of the klass.
Notify of activity on a file descriptor.
Definition: event_notifier.h:20
RAII-style wrapper for file descriptors.
Definition: file_descriptor.h:16
int fd() const
Retrieve the numerical file descriptor.
Definition: file_descriptor.h:28
Frame buffer data and its associated dynamic metadata.
Definition: framebuffer.h:49
The MediaDevice represents a Media Controller device with its full graph of connected objects.
Definition: media_device.h:25
The MediaEntity represents an entity in the media graph.
Definition: media_object.h:89
Information about pixel formats.
Definition: formats.h:23
Describe a rectangle's position and dimensions.
Definition: geometry.h:237
Generic signal and slot communication mechanism.
Definition: signal.h:39
Describe a two-dimensional size.
Definition: geometry.h:51
Hot cache of associations between V4L2 buffer indexes and FrameBuffer.
Definition: v4l2_videodevice.h:119
V4L2BufferCache(unsigned int numEntries)
Create an empty cache with numEntries entries.
Definition: v4l2_videodevice.cpp:175
int get(const FrameBuffer &buffer)
Find the best V4L2 buffer for a FrameBuffer.
Definition: v4l2_videodevice.cpp:217
void put(unsigned int index)
Mark buffer index as free in the cache.
Definition: v4l2_videodevice.cpp:259
The V4L2 video device image format and sizes.
Definition: v4l2_videodevice.h:161
V4L2PixelFormat fourcc
The fourcc code describing the pixel encoding scheme.
Definition: v4l2_videodevice.h:168
const std::string toString() const
Assemble and return a string describing the format.
Definition: v4l2_videodevice.cpp:405
std::array< Plane, 3 > planes
The per-plane memory size information.
Definition: v4l2_videodevice.h:171
Size size
The image size in pixels.
Definition: v4l2_videodevice.h:169
unsigned int planesCount
The number of valid data planes.
Definition: v4l2_videodevice.h:172
Base class for V4L2VideoDevice and V4L2Subdevice.
Definition: v4l2_device.h:27
const std::string & deviceNode() const
Retrieve the device node path.
Definition: v4l2_device.h:39
Memory-to-Memory video device.
Definition: v4l2_videodevice.h:262
V4L2VideoDevice * capture()
Retrieve the capture V4L2VideoDevice instance.
Definition: v4l2_videodevice.h:271
void close()
Close the memory-to-memory device, releasing any resources acquired by open()
Definition: v4l2_videodevice.cpp:1945
int open()
Open a V4L2 Memory to Memory device.
Definition: v4l2_videodevice.cpp:1902
V4L2VideoDevice * output()
Retrieve the output V4L2VideoDevice instance.
Definition: v4l2_videodevice.h:270
V4L2M2MDevice(const std::string &deviceNode)
Create a new V4L2M2MDevice from the deviceNode.
Definition: v4l2_videodevice.cpp:1881
V4L2 pixel format FourCC wrapper.
Definition: v4l2_pixelformat.h:21
V4L2VideoDevice object and API.
Definition: v4l2_videodevice.h:178
std::map< V4L2PixelFormat, std::vector< SizeRange > > Formats
A map of supported V4L2 pixel formats to frame sizes.
Definition: v4l2_videodevice.h:180
const char * driverName() const
Retrieve the name of the V4L2 device driver.
Definition: v4l2_videodevice.h:190
int importBuffers(unsigned int count)
Prepare the device to import count buffers.
Definition: v4l2_videodevice.cpp:1429
int releaseBuffers()
Release resources allocated by allocateBuffers() or importBuffers()
Definition: v4l2_videodevice.cpp:1457
int tryFormat(V4L2DeviceFormat *format)
Try an image format on the V4L2 video device.
Definition: v4l2_videodevice.cpp:771
const char * deviceName() const
Retrieve the name of the V4L2 video device.
Definition: v4l2_videodevice.h:191
int allocateBuffers(unsigned int count, std::vector< std::unique_ptr< FrameBuffer > > *buffers)
Allocate and export buffers from the video device.
Definition: v4l2_videodevice.cpp:1210
std::string logPrefix() const override
Retrieve a string to be prefixed to the log message.
Definition: v4l2_videodevice.cpp:740
int open()
Open the V4L2 video device node and query its capabilities.
Definition: v4l2_videodevice.cpp:526
int streamOn()
Start the video stream.
Definition: v4l2_videodevice.cpp:1773
int queueBuffer(FrameBuffer *buffer)
Queue a buffer to the video device.
Definition: v4l2_videodevice.cpp:1481
V4L2VideoDevice(const std::string &deviceNode)
Construct a V4L2VideoDevice.
Definition: v4l2_videodevice.cpp:493
int streamOff()
Stop the video stream.
Definition: v4l2_videodevice.cpp:1802
int getFormat(V4L2DeviceFormat *format)
Retrieve the image format set on the V4L2 video device.
Definition: v4l2_videodevice.cpp:751
int exportBuffers(unsigned int count, std::vector< std::unique_ptr< FrameBuffer > > *buffers)
Export buffers from the video device.
Definition: v4l2_videodevice.cpp:1259
void close()
Close the video device, releasing any resources acquired by open()
Definition: v4l2_videodevice.cpp:703
const char * busName() const
Retrieve the location of the device in the system.
Definition: v4l2_videodevice.h:192
static std::unique_ptr< V4L2VideoDevice > fromEntityName(const MediaDevice *media, const std::string &entity)
Create a new video device instance from entity in media device media.
Definition: v4l2_videodevice.cpp:1840
int setSelection(unsigned int target, Rectangle *rect)
Set a selection rectangle rect for target.
Definition: v4l2_videodevice.cpp:1124
const V4L2Capability & caps() const
Retrieve the device V4L2 capabilities.
Definition: v4l2_videodevice.h:194
Signal< FrameBuffer * > bufferReady
A Signal emitted when a framebuffer completes.
Definition: v4l2_videodevice.h:211
int setFormat(V4L2DeviceFormat *format)
Configure an image format on the V4L2 video device.
Definition: v4l2_videodevice.cpp:790
Formats formats(uint32_t code=0)
Enumerate all pixel formats and frame sizes.
Definition: v4l2_videodevice.cpp:1001
Frame buffer handling.
Data structures related to geometric objects.
Types and helper functions to handle libcamera image formats.
Logging infrastructure.
Top-level libcamera namespace.
Definition: backtrace.h:17
bool operator==(const Point &lhs, const Point &rhs)
Compare points for equality.
Definition: geometry.cpp:75
libcamera pixel format
Signal & slot implementation.
A memory region to store a single plane of a frame.
Definition: framebuffer.h:53
unsigned int length
The plane length in bytes.
Definition: framebuffer.h:57
FileDescriptor fd
The dmabuf file descriptor.
Definition: framebuffer.h:55
struct v4l2_capability object wrapper and helpers
Definition: v4l2_videodevice.h:38
bool hasStreaming() const
Determine if the video device can perform Streaming I/O.
Definition: v4l2_videodevice.h:108
unsigned int device_caps() const
Retrieve the capabilities of the video device.
Definition: v4l2_videodevice.h:51
bool isVideoOutput() const
Identify if the video device outputs images.
Definition: v4l2_videodevice.h:96
bool isM2M() const
Identify if the device is a Memory-to-Memory device.
Definition: v4l2_videodevice.h:82
bool isMetaCapture() const
Identify if the video device captures image meta-data.
Definition: v4l2_videodevice.h:100
bool hasMediaController() const
Determine if the video device uses Media Controller to configure I/O.
Definition: v4l2_videodevice.h:112
bool isOutput() const
Identify if the video device outputs data.
Definition: v4l2_videodevice.h:69
bool isVideoCapture() const
Identify if the video device captures images.
Definition: v4l2_videodevice.h:92
bool isMultiplanar() const
Identify if the video device implements the V4L2 multiplanar APIs.
Definition: v4l2_videodevice.h:57
bool isMeta() const
Identify if the video device captures or outputs image meta-data.
Definition: v4l2_videodevice.h:87
bool isCapture() const
Identify if the video device captures data.
Definition: v4l2_videodevice.h:63
bool isMetaOutput() const
Identify if the video device outputs image meta-data.
Definition: v4l2_videodevice.h:104
const char * bus_info() const
Retrieve the location of the video device in the system.
Definition: v4l2_videodevice.h:47
const char * card() const
Retrieve the video device card name.
Definition: v4l2_videodevice.h:43
bool isVideo() const
Identify if the video device captures or outputs images.
Definition: v4l2_videodevice.h:75
const char * driver() const
Retrieve the driver module name.
Definition: v4l2_videodevice.h:39
Per-plane memory size information.
Definition: v4l2_videodevice.h:163
uint32_t size
The plane total memory size (in bytes)
Definition: v4l2_videodevice.h:164
uint32_t bpl
The plane line stride (in bytes)
Definition: v4l2_videodevice.h:165
Common base for V4L2 devices and subdevices.
V4L2 Pixel Format.