v4l2_videodevice.h
Go to the documentation of this file.
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
* Copyright (C) 2019, Google Inc.
*
* v4l2_videodevice.h - V4L2 Video Device
*/
#ifndef __LIBCAMERA_INTERNAL_V4L2_VIDEODEVICE_H__
#define __LIBCAMERA_INTERNAL_V4L2_VIDEODEVICE_H__
#include <atomic>
#include <memory>
#include <stdint.h>
#include <string>
#include <vector>
#include <linux/videodev2.h>
#include <libcamera/buffer.h>
#include <libcamera/geometry.h>
#include <libcamera/pixel_format.h>
#include <libcamera/signal.h>
#include "libcamera/internal/formats.h"
#include "libcamera/internal/log.h"
#include "libcamera/internal/v4l2_device.h"
#include "libcamera/internal/v4l2_pixelformat.h"
namespace libcamera {
class EventNotifier;
class FileDescriptor;
class MediaDevice;
class MediaEntity;
struct V4L2Capability final : v4l2_capability {
const char *driver() const
{
return reinterpret_cast<const char *>(v4l2_capability::driver);
}
const char *card() const
{
return reinterpret_cast<const char *>(v4l2_capability::card);
}
const char *bus_info() const
{
return reinterpret_cast<const char *>(v4l2_capability::bus_info);
}
unsigned int device_caps() const
{
return capabilities & V4L2_CAP_DEVICE_CAPS
? v4l2_capability::device_caps
: v4l2_capability::capabilities;
}
bool isMultiplanar() const
{
return device_caps() & (V4L2_CAP_VIDEO_CAPTURE_MPLANE |
V4L2_CAP_VIDEO_OUTPUT_MPLANE |
V4L2_CAP_VIDEO_M2M_MPLANE);
}
bool isCapture() const
{
return device_caps() & (V4L2_CAP_VIDEO_CAPTURE |
V4L2_CAP_VIDEO_CAPTURE_MPLANE |
V4L2_CAP_META_CAPTURE);
}
bool isOutput() const
{
return device_caps() & (V4L2_CAP_VIDEO_OUTPUT |
V4L2_CAP_VIDEO_OUTPUT_MPLANE |
V4L2_CAP_META_OUTPUT);
}
bool isVideo() const
{
return device_caps() & (V4L2_CAP_VIDEO_CAPTURE |
V4L2_CAP_VIDEO_CAPTURE_MPLANE |
V4L2_CAP_VIDEO_OUTPUT |
V4L2_CAP_VIDEO_OUTPUT_MPLANE);
}
bool isM2M() const
{
return device_caps() & (V4L2_CAP_VIDEO_M2M |
V4L2_CAP_VIDEO_M2M_MPLANE);
}
bool isMeta() const
{
return device_caps() & (V4L2_CAP_META_CAPTURE |
V4L2_CAP_META_OUTPUT);
}
bool isVideoCapture() const
{
return isVideo() && isCapture();
}
bool isVideoOutput() const
{
return isVideo() && isOutput();
}
bool isMetaCapture() const
{
return isMeta() && isCapture();
}
bool isMetaOutput() const
{
return isMeta() && isOutput();
}
bool hasStreaming() const
{
return device_caps() & V4L2_CAP_STREAMING;
}
};
class V4L2BufferCache
{
public:
V4L2BufferCache(unsigned int numEntries);
V4L2BufferCache(const std::vector<std::unique_ptr<FrameBuffer>> &buffers);
~V4L2BufferCache();
int get(const FrameBuffer &buffer);
void put(unsigned int index);
private:
class Entry
{
public:
Entry();
Entry(bool free, uint64_t lastUsed, const FrameBuffer &buffer);
bool operator==(const FrameBuffer &buffer) const;
bool free;
uint64_t lastUsed;
private:
struct Plane {
Plane(const FrameBuffer::Plane &plane)
: fd(plane.fd.fd()), length(plane.length)
{
}
int fd;
unsigned int length;
};
std::vector<Plane> planes_;
};
std::atomic<uint64_t> lastUsedCounter_;
std::vector<Entry> cache_;
/* \todo Expose the miss counter through an instrumentation API. */
unsigned int missCounter_;
};
class V4L2DeviceFormat
{
public:
V4L2PixelFormat fourcc;
Size size;
struct {
uint32_t size;
uint32_t bpl;
} planes[3];
unsigned int planesCount;
const std::string toString() const;
};
class V4L2VideoDevice : public V4L2Device
{
public:
explicit V4L2VideoDevice(const std::string &deviceNode);
explicit V4L2VideoDevice(const MediaEntity *entity);
V4L2VideoDevice(const V4L2VideoDevice &) = delete;
~V4L2VideoDevice();
V4L2VideoDevice &operator=(const V4L2VideoDevice &) = delete;
int open();
int open(int handle, enum v4l2_buf_type type);
void close();
const char *driverName() const { return caps_.driver(); }
const char *deviceName() const { return caps_.card(); }
const char *busName() const { return caps_.bus_info(); }
const V4L2Capability &caps() const { return caps_; }
int getFormat(V4L2DeviceFormat *format);
int setFormat(V4L2DeviceFormat *format);
std::map<V4L2PixelFormat, std::vector<SizeRange>> formats(uint32_t code = 0);
int setSelection(unsigned int target, Rectangle *rect);
int allocateBuffers(unsigned int count,
std::vector<std::unique_ptr<FrameBuffer>> *buffers);
int exportBuffers(unsigned int count,
std::vector<std::unique_ptr<FrameBuffer>> *buffers);
int importBuffers(unsigned int count);
int releaseBuffers();
int queueBuffer(FrameBuffer *buffer);
Signal<FrameBuffer *> bufferReady;
int setFrameStartEnabled(bool enable);
Signal<uint32_t> frameStart;
int streamOn();
int streamOff();
static V4L2VideoDevice *fromEntityName(const MediaDevice *media,
const std::string &entity);
V4L2PixelFormat toV4L2PixelFormat(const PixelFormat &pixelFormat);
protected:
std::string logPrefix() const override;
private:
int getFormatMeta(V4L2DeviceFormat *format);
int setFormatMeta(V4L2DeviceFormat *format);
int getFormatMultiplane(V4L2DeviceFormat *format);
int setFormatMultiplane(V4L2DeviceFormat *format);
int getFormatSingleplane(V4L2DeviceFormat *format);
int setFormatSingleplane(V4L2DeviceFormat *format);
std::vector<V4L2PixelFormat> enumPixelformats(uint32_t code);
std::vector<SizeRange> enumSizes(V4L2PixelFormat pixelFormat);
int requestBuffers(unsigned int count, enum v4l2_memory memoryType);
int createBuffers(unsigned int count,
std::vector<std::unique_ptr<FrameBuffer>> *buffers);
std::unique_ptr<FrameBuffer> createBuffer(unsigned int index);
FileDescriptor exportDmabufFd(unsigned int index, unsigned int plane);
void bufferAvailable(EventNotifier *notifier);
FrameBuffer *dequeueBuffer();
void eventAvailable(EventNotifier *notifier);
V4L2Capability caps_;
enum v4l2_buf_type bufferType_;
enum v4l2_memory memoryType_;
V4L2BufferCache *cache_;
std::map<unsigned int, FrameBuffer *> queuedBuffers_;
EventNotifier *fdBufferNotifier_;
EventNotifier *fdEventNotifier_;
bool frameStartEnabled_;
};
class V4L2M2MDevice
{
public:
V4L2M2MDevice(const std::string &deviceNode);
~V4L2M2MDevice();
int open();
void close();
V4L2VideoDevice *output() { return output_; }
V4L2VideoDevice *capture() { return capture_; }
private:
std::string deviceNode_;
V4L2VideoDevice *output_;
V4L2VideoDevice *capture_;
};
} /* namespace libcamera */
#endif /* __LIBCAMERA_INTERNAL_V4L2_VIDEODEVICE_H__ */
buffer.h
Buffer handling.
libcamera::EventNotifier
Notify of activity on a file descriptor.
Definition: event_notifier.h:18
libcamera::FileDescriptor
RAII-style wrapper for file descriptors.
Definition: file_descriptor.h:15
libcamera::FileDescriptor::fd
int fd() const
Retrieve the numerical file descriptor.
Definition: file_descriptor.h:27
libcamera::FrameBuffer
Frame buffer data and its associated dynamic metadata.
Definition: buffer.h:37
libcamera::MediaDevice
The MediaDevice represents a Media Controller device with its full graph of connected objects.
Definition: media_device.h:25
libcamera::MediaEntity
The MediaEntity represents an entity in the media graph.
Definition: media_object.h:86
libcamera::PixelFormat
libcamera image pixel format
Definition: pixel_format.h:17
libcamera::Signal
Generic signal and slot communication mechanism.
Definition: signal.h:39
libcamera::V4L2BufferCache
Hot cache of associations between V4L2 buffer indexes and FrameBuffer.
Definition: v4l2_videodevice.h:112
libcamera::V4L2BufferCache::V4L2BufferCache
V4L2BufferCache(unsigned int numEntries)
Create an empty cache with numEntries entries.
Definition: v4l2_videodevice.cpp:164
libcamera::V4L2BufferCache::get
int get(const FrameBuffer &buffer)
Find the best V4L2 buffer for a FrameBuffer.
Definition: v4l2_videodevice.cpp:206
libcamera::V4L2BufferCache::put
void put(unsigned int index)
Mark buffer index as free in the cache.
Definition: v4l2_videodevice.cpp:248
libcamera::V4L2DeviceFormat
The V4L2 video device image format and sizes.
Definition: v4l2_videodevice.h:154
libcamera::V4L2DeviceFormat::fourcc
V4L2PixelFormat fourcc
The fourcc code describing the pixel encoding scheme.
Definition: v4l2_videodevice.h:156
libcamera::V4L2DeviceFormat::toString
const std::string toString() const
Assemble and return a string describing the format.
Definition: v4l2_videodevice.cpp:385
libcamera::V4L2DeviceFormat::size
Size size
The image size in pixels.
Definition: v4l2_videodevice.h:157
libcamera::V4L2DeviceFormat::planes
struct libcamera::V4L2DeviceFormat::@2 planes[3]
The per-plane memory size information.
libcamera::V4L2DeviceFormat::planesCount
unsigned int planesCount
The number of valid data planes.
Definition: v4l2_videodevice.h:163
libcamera::V4L2Device
Base class for V4L2VideoDevice and V4L2Subdevice.
Definition: v4l2_device.h:22
libcamera::V4L2Device::deviceNode
const std::string & deviceNode() const
Retrieve the device node path.
Definition: v4l2_device.h:32
libcamera::V4L2M2MDevice
Memory-to-Memory video device.
Definition: v4l2_videodevice.h:257
libcamera::V4L2M2MDevice::capture
V4L2VideoDevice * capture()
Retrieve the capture V4L2VideoDevice instance.
Definition: v4l2_videodevice.h:266
libcamera::V4L2M2MDevice::close
void close()
Close the memory-to-memory device, releasing any resources acquired by open()
Definition: v4l2_videodevice.cpp:1758
libcamera::V4L2M2MDevice::open
int open()
Open a V4L2 Memory to Memory device.
Definition: v4l2_videodevice.cpp:1715
libcamera::V4L2M2MDevice::output
V4L2VideoDevice * output()
Retrieve the output V4L2VideoDevice instance.
Definition: v4l2_videodevice.h:265
libcamera::V4L2M2MDevice::V4L2M2MDevice
V4L2M2MDevice(const std::string &deviceNode)
Create a new V4L2M2MDevice from the deviceNode.
Definition: v4l2_videodevice.cpp:1694
libcamera::V4L2PixelFormat
V4L2 pixel format FourCC wrapper.
Definition: v4l2_pixelformat.h:21
libcamera::V4L2VideoDevice
V4L2VideoDevice object and API.
Definition: v4l2_videodevice.h:169
libcamera::V4L2VideoDevice::formats
std::map< V4L2PixelFormat, std::vector< SizeRange > > formats(uint32_t code=0)
Enumerate all pixel formats and frame sizes.
Definition: v4l2_videodevice.cpp:928
libcamera::V4L2VideoDevice::driverName
const char * driverName() const
Retrieve the name of the V4L2 device driver.
Definition: v4l2_videodevice.h:182
libcamera::V4L2VideoDevice::toV4L2PixelFormat
V4L2PixelFormat toV4L2PixelFormat(const PixelFormat &pixelFormat)
Convert PixelFormat to its corresponding V4L2 FourCC.
Definition: v4l2_videodevice.cpp:1657
libcamera::V4L2VideoDevice::importBuffers
int importBuffers(unsigned int count)
Prepare the device to import count buffers.
Definition: v4l2_videodevice.cpp:1302
libcamera::V4L2VideoDevice::releaseBuffers
int releaseBuffers()
Release resources allocated by allocateBuffers() or importBuffers()
Definition: v4l2_videodevice.cpp:1330
libcamera::V4L2VideoDevice::deviceName
const char * deviceName() const
Retrieve the name of the V4L2 video device.
Definition: v4l2_videodevice.h:183
libcamera::V4L2VideoDevice::logPrefix
std::string logPrefix() const override
Retrieve a string to be prefixed to the log message.
Definition: v4l2_videodevice.cpp:706
libcamera::V4L2VideoDevice::allocateBuffers
int allocateBuffers(unsigned int count, std::vector< std::unique_ptr< FrameBuffer >> *buffers)
Allocate and export buffers from the video device.
Definition: v4l2_videodevice.cpp:1132
libcamera::V4L2VideoDevice::open
int open()
Open the V4L2 video device node and query its capabilities.
Definition: v4l2_videodevice.cpp:501
libcamera::V4L2VideoDevice::streamOn
int streamOn()
Start the video stream.
Definition: v4l2_videodevice.cpp:1576
libcamera::V4L2VideoDevice::queueBuffer
int queueBuffer(FrameBuffer *buffer)
Queue a buffer to the video device.
Definition: v4l2_videodevice.cpp:1354
libcamera::V4L2VideoDevice::frameStart
Signal< uint32_t > frameStart
A Signal emitted when capture of a frame has started.
Definition: v4l2_videodevice.h:205
libcamera::V4L2VideoDevice::V4L2VideoDevice
V4L2VideoDevice(const std::string &deviceNode)
Construct a V4L2VideoDevice.
Definition: v4l2_videodevice.cpp:468
libcamera::V4L2VideoDevice::streamOff
int streamOff()
Stop the video stream.
Definition: v4l2_videodevice.cpp:1600
libcamera::V4L2VideoDevice::setFrameStartEnabled
int setFrameStartEnabled(bool enable)
Enable or disable frame start event notification.
Definition: v4l2_videodevice.cpp:1547
libcamera::V4L2VideoDevice::getFormat
int getFormat(V4L2DeviceFormat *format)
Retrieve the image format set on the V4L2 video device.
Definition: v4l2_videodevice.cpp:716
libcamera::V4L2VideoDevice::close
void close()
Close the video device, releasing any resources acquired by open()
Definition: v4l2_videodevice.cpp:670
libcamera::V4L2VideoDevice::busName
const char * busName() const
Retrieve the location of the device in the system.
Definition: v4l2_videodevice.h:184
libcamera::V4L2VideoDevice::setSelection
int setSelection(unsigned int target, Rectangle *rect)
Set a selection rectangle rect for target.
Definition: v4l2_videodevice.cpp:1051
libcamera::V4L2VideoDevice::caps
const V4L2Capability & caps() const
Retrieve the device V4L2 capabilities.
Definition: v4l2_videodevice.h:186
libcamera::V4L2VideoDevice::bufferReady
Signal< FrameBuffer * > bufferReady
A Signal emitted when a framebuffer completes.
Definition: v4l2_videodevice.h:202
libcamera::V4L2VideoDevice::fromEntityName
static 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:1636
libcamera::V4L2VideoDevice::setFormat
int setFormat(V4L2DeviceFormat *format)
Configure an image format on the V4L2 video device.
Definition: v4l2_videodevice.cpp:735
libcamera::V4L2VideoDevice::exportBuffers
int exportBuffers(unsigned int count, std::vector< std::unique_ptr< FrameBuffer >> *buffers)
Export buffers from the video device.
Definition: v4l2_videodevice.cpp:1176
geometry.h
Data structures related to geometric objects.
libcamera::operator==
bool operator==(const Rectangle &lhs, const Rectangle &rhs)
Compare rectangles for equality.
Definition: geometry.cpp:69
formats.h
Types and helper methods to handle libcamera image formats.
log.h
Logging infrastructure.
pixel_format.h
libcamera pixel format
signal.h
Signal & slot implementation.
libcamera::FrameBuffer::Plane
A memory region to store a single plane of a frame.
Definition: buffer.h:39
libcamera::FrameBuffer::Plane::length
unsigned int length
The plane length in bytes.
Definition: buffer.h:41
libcamera::FrameBuffer::Plane::fd
FileDescriptor fd
The dmabuf file descriptor.
Definition: buffer.h:40
libcamera::Rectangle
Describe a rectangle's position and dimensions.
Definition: geometry.h:15
libcamera::Size
Describe a two-dimensional size.
Definition: geometry.h:30
libcamera::V4L2Capability
struct v4l2_capability object wrapper and helpers
Definition: v4l2_videodevice.h:35
libcamera::V4L2Capability::hasStreaming
bool hasStreaming() const
Determine if the video device can perform Streaming I/O.
Definition: v4l2_videodevice.h:105
libcamera::V4L2Capability::device_caps
unsigned int device_caps() const
Retrieve the capabilities of the video device.
Definition: v4l2_videodevice.h:48
libcamera::V4L2Capability::isVideoOutput
bool isVideoOutput() const
Identify if the video device outputs images.
Definition: v4l2_videodevice.h:93
libcamera::V4L2Capability::isM2M
bool isM2M() const
Identify if the device is a Memory-to-Memory device.
Definition: v4l2_videodevice.h:79
libcamera::V4L2Capability::isMetaCapture
bool isMetaCapture() const
Identify if the video device captures image meta-data.
Definition: v4l2_videodevice.h:97
libcamera::V4L2Capability::isOutput
bool isOutput() const
Identify if the video device outputs data.
Definition: v4l2_videodevice.h:66
libcamera::V4L2Capability::isVideoCapture
bool isVideoCapture() const
Identify if the video device captures images.
Definition: v4l2_videodevice.h:89
libcamera::V4L2Capability::isMultiplanar
bool isMultiplanar() const
Identify if the video device implements the V4L2 multiplanar APIs.
Definition: v4l2_videodevice.h:54
libcamera::V4L2Capability::isMeta
bool isMeta() const
Identify if the video device captures or outputs image meta-data.
Definition: v4l2_videodevice.h:84
libcamera::V4L2Capability::isCapture
bool isCapture() const
Identify if the video device captures data.
Definition: v4l2_videodevice.h:60
libcamera::V4L2Capability::isMetaOutput
bool isMetaOutput() const
Identify if the video device outputs image meta-data.
Definition: v4l2_videodevice.h:101
libcamera::V4L2Capability::bus_info
const char * bus_info() const
Retrieve the location of the video device in the system.
Definition: v4l2_videodevice.h:44
libcamera::V4L2Capability::card
const char * card() const
Retrieve the video device card name.
Definition: v4l2_videodevice.h:40
libcamera::V4L2Capability::isVideo
bool isVideo() const
Identify if the video device captures or outputs images.
Definition: v4l2_videodevice.h:72
libcamera::V4L2Capability::driver
const char * driver() const
Retrieve the driver module name.
Definition: v4l2_videodevice.h:36
v4l2_device.h
Common base for V4L2 devices and subdevices.
v4l2_pixelformat.h
V4L2 Pixel Format.