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