libcamera  v0.0.0+3-3b6e2d50-dirty (2022-01-27T16:18:04+08: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 
19 #include <libcamera/base/class.h>
20 #include <libcamera/base/log.h>
21 #include <libcamera/base/signal.h>
22 
23 #include <libcamera/framebuffer.h>
24 #include <libcamera/geometry.h>
25 #include <libcamera/pixel_format.h>
26 
30 
31 namespace libcamera {
32 
33 class EventNotifier;
34 class FileDescriptor;
35 class MediaDevice;
36 class MediaEntity;
37 
38 struct 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  }
112 };
113 
115 {
116 public:
117  V4L2BufferCache(unsigned int numEntries);
118  V4L2BufferCache(const std::vector<std::unique_ptr<FrameBuffer>> &buffers);
119  ~V4L2BufferCache();
120 
121  int get(const FrameBuffer &buffer);
122  void put(unsigned int index);
123 
124 private:
125  class Entry
126  {
127  public:
128  Entry();
129  Entry(bool free, uint64_t lastUsed, const FrameBuffer &buffer);
130 
131  bool operator==(const FrameBuffer &buffer) const;
132 
133  bool free_;
134  uint64_t lastUsed_;
135 
136  private:
137  struct Plane {
138  Plane(const FrameBuffer::Plane &plane)
139  : fd(plane.fd.fd()), length(plane.length)
140  {
141  }
142 
143  int fd;
144  unsigned int length;
145  };
146 
147  std::vector<Plane> planes_;
148  };
149 
150  std::atomic<uint64_t> lastUsedCounter_;
151  std::vector<Entry> cache_;
152  /* \todo Expose the miss counter through an instrumentation API. */
153  unsigned int missCounter_;
154 };
155 
157 {
158 public:
159  struct Plane {
160  uint32_t size = 0;
161  uint32_t bpl = 0;
162  };
163 
166 
167  std::array<Plane, 3> planes;
168  unsigned int planesCount = 0;
169 
170  const std::string toString() const;
171 };
172 
174 {
175 public:
176  using Formats = std::map<V4L2PixelFormat, std::vector<SizeRange>>;
177 
178  explicit V4L2VideoDevice(const std::string &deviceNode);
179  explicit V4L2VideoDevice(const MediaEntity *entity);
180  ~V4L2VideoDevice();
181 
182  int open();
183  int open(int handle, enum v4l2_buf_type type);
184  void close();
185 
186  const char *driverName() const { return caps_.driver(); }
187  const char *deviceName() const { return caps_.card(); }
188  const char *busName() const { return caps_.bus_info(); }
189 
190  const V4L2Capability &caps() const { return caps_; }
191 
192  int getFormat(V4L2DeviceFormat *format);
193  int tryFormat(V4L2DeviceFormat *format);
194  int setFormat(V4L2DeviceFormat *format);
195  Formats formats(uint32_t code = 0);
196 
197  int setSelection(unsigned int target, Rectangle *rect);
198 
199  int allocateBuffers(unsigned int count,
200  std::vector<std::unique_ptr<FrameBuffer>> *buffers);
201  int exportBuffers(unsigned int count,
202  std::vector<std::unique_ptr<FrameBuffer>> *buffers);
203  int importBuffers(unsigned int count);
204  int releaseBuffers();
205 
206  int queueBuffer(FrameBuffer *buffer);
208 
209  int streamOn();
210  int streamOff();
211 
212  static std::unique_ptr<V4L2VideoDevice>
213  fromEntityName(const MediaDevice *media, const std::string &entity);
214 
215 protected:
216  std::string logPrefix() const override;
217 
218 private:
220 
221  int getFormatMeta(V4L2DeviceFormat *format);
222  int trySetFormatMeta(V4L2DeviceFormat *format, bool set);
223 
224  int getFormatMultiplane(V4L2DeviceFormat *format);
225  int trySetFormatMultiplane(V4L2DeviceFormat *format, bool set);
226 
227  int getFormatSingleplane(V4L2DeviceFormat *format);
228  int trySetFormatSingleplane(V4L2DeviceFormat *format, bool set);
229 
230  std::vector<V4L2PixelFormat> enumPixelformats(uint32_t code);
231  std::vector<SizeRange> enumSizes(V4L2PixelFormat pixelFormat);
232 
233  int requestBuffers(unsigned int count, enum v4l2_memory memoryType);
234  int createBuffers(unsigned int count,
235  std::vector<std::unique_ptr<FrameBuffer>> *buffers);
236  std::unique_ptr<FrameBuffer> createBuffer(unsigned int index);
237  FileDescriptor exportDmabufFd(unsigned int index, unsigned int plane);
238 
239  void bufferAvailable();
240  FrameBuffer *dequeueBuffer();
241 
242  V4L2Capability caps_;
243  V4L2DeviceFormat format_;
244  const PixelFormatInfo *formatInfo_;
245 
246  enum v4l2_buf_type bufferType_;
247  enum v4l2_memory memoryType_;
248 
249  V4L2BufferCache *cache_;
250  std::map<unsigned int, FrameBuffer *> queuedBuffers_;
251 
252  EventNotifier *fdBufferNotifier_;
253 
254  bool streaming_;
255 };
256 
258 {
259 public:
260  V4L2M2MDevice(const std::string &deviceNode);
261  ~V4L2M2MDevice();
262 
263  int open();
264  void close();
265 
266  V4L2VideoDevice *output() { return output_; }
267  V4L2VideoDevice *capture() { return capture_; }
268 
269 private:
270  std::string deviceNode_;
271 
272  V4L2VideoDevice *output_;
273  V4L2VideoDevice *capture_;
274 };
275 
276 } /* namespace libcamera */
277 
278 #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:207
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:115
V4L2BufferCache(unsigned int numEntries)
Create an empty cache with numEntries entries.
Definition: v4l2_videodevice.cpp:169
int get(const FrameBuffer &buffer)
Find the best V4L2 buffer for a FrameBuffer.
Definition: v4l2_videodevice.cpp:211
void put(unsigned int index)
Mark buffer index as free in the cache.
Definition: v4l2_videodevice.cpp:253
The V4L2 video device image format and sizes.
Definition: v4l2_videodevice.h:157
V4L2PixelFormat fourcc
The fourcc code describing the pixel encoding scheme.
Definition: v4l2_videodevice.h:164
const std::string toString() const
Assemble and return a string describing the format.
Definition: v4l2_videodevice.cpp:399
std::array< Plane, 3 > planes
The per-plane memory size information.
Definition: v4l2_videodevice.h:167
Size size
The image size in pixels.
Definition: v4l2_videodevice.h:165
unsigned int planesCount
The number of valid data planes.
Definition: v4l2_videodevice.h:168
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:258
V4L2VideoDevice * capture()
Retrieve the capture V4L2VideoDevice instance.
Definition: v4l2_videodevice.h:267
void close()
Close the memory-to-memory device, releasing any resources acquired by open()
Definition: v4l2_videodevice.cpp:1926
int open()
Open a V4L2 Memory to Memory device.
Definition: v4l2_videodevice.cpp:1883
V4L2VideoDevice * output()
Retrieve the output V4L2VideoDevice instance.
Definition: v4l2_videodevice.h:266
V4L2M2MDevice(const std::string &deviceNode)
Create a new V4L2M2MDevice from the deviceNode.
Definition: v4l2_videodevice.cpp:1862
V4L2 pixel format FourCC wrapper.
Definition: v4l2_pixelformat.h:21
V4L2VideoDevice object and API.
Definition: v4l2_videodevice.h:174
std::map< V4L2PixelFormat, std::vector< SizeRange > > Formats
A map of supported V4L2 pixel formats to frame sizes.
Definition: v4l2_videodevice.h:176
const char * driverName() const
Retrieve the name of the V4L2 device driver.
Definition: v4l2_videodevice.h:186
int importBuffers(unsigned int count)
Prepare the device to import count buffers.
Definition: v4l2_videodevice.cpp:1423
int releaseBuffers()
Release resources allocated by allocateBuffers() or importBuffers()
Definition: v4l2_videodevice.cpp:1451
int tryFormat(V4L2DeviceFormat *format)
Try an image format on the V4L2 video device.
Definition: v4l2_videodevice.cpp:765
const char * deviceName() const
Retrieve the name of the V4L2 video device.
Definition: v4l2_videodevice.h:187
std::string logPrefix() const override
Retrieve a string to be prefixed to the log message.
Definition: v4l2_videodevice.cpp:734
int allocateBuffers(unsigned int count, std::vector< std::unique_ptr< FrameBuffer >> *buffers)
Allocate and export buffers from the video device.
Definition: v4l2_videodevice.cpp:1204
int open()
Open the V4L2 video device node and query its capabilities.
Definition: v4l2_videodevice.cpp:520
int streamOn()
Start the video stream.
Definition: v4l2_videodevice.cpp:1754
int queueBuffer(FrameBuffer *buffer)
Queue a buffer to the video device.
Definition: v4l2_videodevice.cpp:1475
V4L2VideoDevice(const std::string &deviceNode)
Construct a V4L2VideoDevice.
Definition: v4l2_videodevice.cpp:487
int streamOff()
Stop the video stream.
Definition: v4l2_videodevice.cpp:1783
int getFormat(V4L2DeviceFormat *format)
Retrieve the image format set on the V4L2 video device.
Definition: v4l2_videodevice.cpp:745
void close()
Close the video device, releasing any resources acquired by open()
Definition: v4l2_videodevice.cpp:697
const char * busName() const
Retrieve the location of the device in the system.
Definition: v4l2_videodevice.h:188
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:1821
int setSelection(unsigned int target, Rectangle *rect)
Set a selection rectangle rect for target.
Definition: v4l2_videodevice.cpp:1118
const V4L2Capability & caps() const
Retrieve the device V4L2 capabilities.
Definition: v4l2_videodevice.h:190
Signal< FrameBuffer * > bufferReady
A Signal emitted when a framebuffer completes.
Definition: v4l2_videodevice.h:207
int setFormat(V4L2DeviceFormat *format)
Configure an image format on the V4L2 video device.
Definition: v4l2_videodevice.cpp:784
Formats formats(uint32_t code=0)
Enumerate all pixel formats and frame sizes.
Definition: v4l2_videodevice.cpp:995
int exportBuffers(unsigned int count, std::vector< std::unique_ptr< FrameBuffer >> *buffers)
Export buffers from the video device.
Definition: v4l2_videodevice.cpp:1253
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: bound_method.h:15
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 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:159
uint32_t size
The plane total memory size (in bytes)
Definition: v4l2_videodevice.h:160
uint32_t bpl
The plane line stride (in bytes)
Definition: v4l2_videodevice.h:161
Common base for V4L2 devices and subdevices.
V4L2 Pixel Format.