Loading...
Searching...
No Matches
v4l2_videodevice.h
Go to the documentation of this file.
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
* Copyright (C) 2019, Google Inc.
*
* V4L2 Video Device
*/
#pragma once
#include <array>
#include <memory>
#include <optional>
#include <ostream>
#include <queue>
#include <stdint.h>
#include <string>
#include <unordered_set>
#include <vector>
#include <linux/videodev2.h>
#include <libcamera/base/class.h>
#include <libcamera/base/log.h>
#include <libcamera/base/signal.h>
#include <libcamera/base/timer.h>
#include <libcamera/base/unique_fd.h>
#include <libcamera/base/utils.h>
#include <libcamera/color_space.h>
#include <libcamera/framebuffer.h>
#include <libcamera/geometry.h>
#include <libcamera/pixel_format.h>
#include "libcamera/internal/formats.h"
#include "libcamera/internal/v4l2_device.h"
#include "libcamera/internal/v4l2_pixelformat.h"
#include "libcamera/internal/v4l2_request.h"
namespace libcamera {
class EventNotifier;
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;
}
bool hasMediaController() const
{
return device_caps() & V4L2_CAP_IO_MC;
}
};
class V4L2BufferCache
{
public:
V4L2BufferCache(unsigned int numEntries);
V4L2BufferCache(const std::vector<std::unique_ptr<FrameBuffer>> &buffers);
~V4L2BufferCache();
bool isEmpty() const;
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.get()), length(plane.length)
{
}
int fd;
unsigned int length;
};
std::vector<Plane> planes_;
};
uint64_t lastUsedCounter_;
std::vector<Entry> cache_;
/* \todo Expose the miss counter through an instrumentation API. */
unsigned int missCounter_;
};
class V4L2DeviceFormat
{
public:
struct Plane {
uint32_t size = 0;
uint32_t bpl = 0;
};
V4L2PixelFormat fourcc;
Size size;
std::optional<ColorSpace> colorSpace;
std::array<Plane, 3> planes;
unsigned int planesCount = 0;
std::string toString() const;
};
std::ostream &operator<<(std::ostream &out, const V4L2DeviceFormat &f);
class V4L2VideoDevice : public V4L2Device
{
public:
using Formats = std::map<V4L2PixelFormat, std::vector<SizeRange>>;
explicit V4L2VideoDevice(const std::string &deviceNode);
explicit V4L2VideoDevice(const MediaEntity *entity);
~V4L2VideoDevice();
int open();
int open(SharedFD 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 tryFormat(V4L2DeviceFormat *format);
int setFormat(V4L2DeviceFormat *format);
Formats formats(uint32_t code = 0);
int getSelection(unsigned int target, Rectangle *rect);
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, const V4L2Request *request = nullptr);
Signal<FrameBuffer *> bufferReady;
int streamOn();
int streamOff();
void setDequeueTimeout(utils::Duration timeout);
Signal<> dequeueTimeout;
static std::unique_ptr<V4L2VideoDevice>
fromEntityName(const MediaDevice *media, const std::string &entity);
V4L2PixelFormat toV4L2PixelFormat(const PixelFormat &pixelFormat) const;
protected:
std::string logPrefix() const override;
private:
LIBCAMERA_DISABLE_COPY(V4L2VideoDevice)
enum class State {
Streaming,
Stopping,
Stopped,
};
int initFormats();
int getFormatMeta(V4L2DeviceFormat *format);
int trySetFormatMeta(V4L2DeviceFormat *format, bool set);
int getFormatMultiplane(V4L2DeviceFormat *format);
int trySetFormatMultiplane(V4L2DeviceFormat *format, bool set);
int getFormatSingleplane(V4L2DeviceFormat *format);
int trySetFormatSingleplane(V4L2DeviceFormat *format, bool set);
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);
UniqueFD exportDmabufFd(unsigned int index, unsigned int plane);
void bufferAvailable();
FrameBuffer *dequeueBuffer();
int queueToDevice(FrameBuffer *buffer);
void watchdogExpired();
template<typename T>
static std::optional<ColorSpace> toColorSpace(const T &v4l2Format);
V4L2Capability caps_;
V4L2DeviceFormat format_;
const PixelFormatInfo *formatInfo_;
std::unordered_set<V4L2PixelFormat> pixelFormats_;
enum v4l2_buf_type bufferType_;
enum v4l2_memory memoryType_;
V4L2BufferCache *cache_;
std::map<unsigned int, FrameBuffer *> queuedBuffers_;
std::queue<FrameBuffer *> pendingBuffersToQueue_;
EventNotifier *fdBufferNotifier_;
State state_;
std::optional<unsigned int> firstFrame_;
Timer watchdog_;
utils::Duration watchdogDuration_;
};
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 */
class.h
Utilities to help constructing class interfaces.
LIBCAMERA_DISABLE_COPY
#define LIBCAMERA_DISABLE_COPY(klass)
Disable copy construction and assignment of the klass.
Definition class.h:27
libcamera::EventNotifier
Notify of activity on a file descriptor.
Definition event_notifier.h:20
libcamera::FrameBuffer
Frame buffer data and its associated dynamic metadata.
Definition framebuffer.h:50
libcamera::MediaDevice
The MediaDevice represents a Media Controller device with its full graph of connected objects.
Definition media_device.h:28
libcamera::MediaEntity
The MediaEntity represents an entity in the media graph.
Definition media_object.h:97
libcamera::PixelFormatInfo
Information about pixel formats.
Definition formats.h:21
libcamera::PixelFormat
libcamera image pixel format
Definition pixel_format.h:17
libcamera::Rectangle
Describe a rectangle's position and dimensions.
Definition geometry.h:247
libcamera::SharedFD
RAII-style wrapper for file descriptors.
Definition shared_fd.h:17
libcamera::SharedFD::get
int get() const
Retrieve the numerical file descriptor.
Definition shared_fd.h:30
libcamera::Signal
Generic signal and slot communication mechanism.
Definition signal.h:39
libcamera::Size
Describe a two-dimensional size.
Definition geometry.h:51
libcamera::Timer
Single-shot timer interface.
Definition timer.h:22
libcamera::UniqueFD
unique_ptr-like wrapper for a file descriptor
Definition unique_fd.h:17
libcamera::V4L2BufferCache
Hot cache of associations between V4L2 buffer indexes and FrameBuffer.
Definition v4l2_videodevice.h:126
libcamera::V4L2BufferCache::V4L2BufferCache
V4L2BufferCache(unsigned int numEntries)
Create an empty cache with numEntries entries.
Definition v4l2_videodevice.cpp:175
libcamera::V4L2BufferCache::get
int get(const FrameBuffer &buffer)
Find the best V4L2 buffer for a FrameBuffer.
Definition v4l2_videodevice.cpp:230
libcamera::V4L2BufferCache::isEmpty
bool isEmpty() const
Check if all the entries in the cache are unused.
Definition v4l2_videodevice.cpp:207
libcamera::V4L2BufferCache::put
void put(unsigned int index)
Mark buffer index as free in the cache.
Definition v4l2_videodevice.cpp:272
libcamera::V4L2DeviceFormat
The V4L2 video device image format and sizes.
Definition v4l2_videodevice.h:169
libcamera::V4L2DeviceFormat::fourcc
V4L2PixelFormat fourcc
The fourcc code describing the pixel encoding scheme.
Definition v4l2_videodevice.h:176
libcamera::V4L2DeviceFormat::planesCount
unsigned int planesCount
The number of valid data planes.
Definition v4l2_videodevice.h:181
libcamera::V4L2DeviceFormat::toString
std::string toString() const
Assemble and return a string describing the format.
Definition v4l2_videodevice.cpp:433
libcamera::V4L2DeviceFormat::colorSpace
std::optional< ColorSpace > colorSpace
The color space of the pixels.
Definition v4l2_videodevice.h:178
libcamera::V4L2DeviceFormat::planes
std::array< Plane, 3 > planes
The per-plane memory size information.
Definition v4l2_videodevice.h:180
libcamera::V4L2DeviceFormat::size
Size size
The image size in pixels.
Definition v4l2_videodevice.h:177
libcamera::V4L2Device::deviceNode
const std::string & deviceNode() const
Retrieve the device node path.
Definition v4l2_device.h:46
libcamera::V4L2Device::V4L2Device
V4L2Device(const std::string &deviceNode)
Construct a V4L2Device.
Definition v4l2_device.cpp:59
libcamera::V4L2M2MDevice::close
void close()
Close the memory-to-memory device, releasing any resources acquired by open().
Definition v4l2_videodevice.cpp:2323
libcamera::V4L2M2MDevice::output
V4L2VideoDevice * output()
Retrieve the output V4L2VideoDevice instance.
Definition v4l2_videodevice.h:309
libcamera::V4L2M2MDevice::capture
V4L2VideoDevice * capture()
Retrieve the capture V4L2VideoDevice instance.
Definition v4l2_videodevice.h:310
libcamera::V4L2M2MDevice::open
int open()
Open a V4L2 Memory to Memory device.
Definition v4l2_videodevice.cpp:2286
libcamera::V4L2M2MDevice::V4L2M2MDevice
V4L2M2MDevice(const std::string &deviceNode)
Create a new V4L2M2MDevice from the deviceNode.
Definition v4l2_videodevice.cpp:2265
libcamera::V4L2PixelFormat
V4L2 pixel format FourCC wrapper.
Definition v4l2_pixelformat.h:24
libcamera::V4L2Request
V4L2Request object and API.
Definition v4l2_request.h:22
libcamera::V4L2VideoDevice
V4L2VideoDevice object and API.
Definition v4l2_videodevice.h:189
libcamera::V4L2VideoDevice::importBuffers
int importBuffers(unsigned int count)
Prepare the device to import count buffers.
Definition v4l2_videodevice.cpp:1589
libcamera::V4L2VideoDevice::releaseBuffers
int releaseBuffers()
Release resources allocated by allocateBuffers() or importBuffers().
Definition v4l2_videodevice.cpp:1617
libcamera::V4L2VideoDevice::tryFormat
int tryFormat(V4L2DeviceFormat *format)
Try an image format on the V4L2 video device.
Definition v4l2_videodevice.cpp:832
libcamera::V4L2VideoDevice::queueBuffer
int queueBuffer(FrameBuffer *buffer, const V4L2Request *request=nullptr)
Queue a buffer to the video device if possible.
Definition v4l2_videodevice.cpp:1654
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:1370
libcamera::V4L2VideoDevice::logPrefix
std::string logPrefix() const override
Retrieve a string to be prefixed to the log message.
Definition v4l2_videodevice.cpp:794
libcamera::V4L2VideoDevice::Formats
std::map< V4L2PixelFormat, std::vector< SizeRange > > Formats
A map of supported V4L2 pixel formats to frame sizes.
Definition v4l2_videodevice.h:191
libcamera::V4L2VideoDevice::setDequeueTimeout
void setDequeueTimeout(utils::Duration timeout)
Set the dequeue timeout value.
Definition v4l2_videodevice.cpp:2144
libcamera::V4L2VideoDevice::open
int open()
Open the V4L2 video device node and query its capabilities.
Definition v4l2_videodevice.cpp:571
libcamera::V4L2VideoDevice::bufferReady
Signal< FrameBuffer * > bufferReady
A Signal emitted when a framebuffer completes.
Definition v4l2_videodevice.h:223
libcamera::V4L2VideoDevice::busName
const char * busName() const
Retrieve the location of the device in the system.
Definition v4l2_videodevice.h:203
libcamera::V4L2VideoDevice::streamOn
int streamOn()
Start the video stream.
Definition v4l2_videodevice.cpp:2058
libcamera::V4L2VideoDevice::V4L2VideoDevice
V4L2VideoDevice(const std::string &deviceNode)
Construct a V4L2VideoDevice.
Definition v4l2_videodevice.cpp:536
libcamera::V4L2VideoDevice::streamOff
int streamOff()
Stop the video stream.
Definition v4l2_videodevice.cpp:2091
libcamera::V4L2VideoDevice::getFormat
int getFormat(V4L2DeviceFormat *format)
Retrieve the image format set on the V4L2 video device.
Definition v4l2_videodevice.cpp:805
libcamera::V4L2VideoDevice::getSelection
int getSelection(unsigned int target, Rectangle *rect)
Get the selection rectangle for target.
Definition v4l2_videodevice.cpp:1251
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:1419
libcamera::V4L2VideoDevice::dequeueTimeout
Signal dequeueTimeout
A Signal emitted when the dequeue watchdog timer expires.
Definition v4l2_videodevice.h:229
libcamera::V4L2VideoDevice::driverName
const char * driverName() const
Retrieve the name of the V4L2 device driver.
Definition v4l2_videodevice.h:201
libcamera::V4L2VideoDevice::close
void close()
Close the video device, releasing any resources acquired by open().
Definition v4l2_videodevice.cpp:757
libcamera::V4L2VideoDevice::caps
const V4L2Capability & caps() const
Retrieve the device V4L2 capabilities.
Definition v4l2_videodevice.h:205
libcamera::V4L2VideoDevice::fromEntityName
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:2181
libcamera::V4L2VideoDevice::setSelection
int setSelection(unsigned int target, Rectangle *rect)
Set a selection rectangle rect for target.
Definition v4l2_videodevice.cpp:1283
libcamera::V4L2VideoDevice::toV4L2PixelFormat
V4L2PixelFormat toV4L2PixelFormat(const PixelFormat &pixelFormat) const
Convert PixelFormat to a V4L2PixelFormat supported by the device.
Definition v4l2_videodevice.cpp:2212
libcamera::V4L2VideoDevice::setFormat
int setFormat(V4L2DeviceFormat *format)
Configure an image format on the V4L2 video device.
Definition v4l2_videodevice.cpp:858
libcamera::V4L2VideoDevice::formats
Formats formats(uint32_t code=0)
Enumerate all pixel formats and frame sizes.
Definition v4l2_videodevice.cpp:1128
libcamera::V4L2VideoDevice::deviceName
const char * deviceName() const
Retrieve the name of the V4L2 video device.
Definition v4l2_videodevice.h:202
libcamera::utils::Duration
Helper class from std::chrono::duration that represents a time duration in nanoseconds with double pr...
Definition utils.h:322
color_space.h
Class and enums to represent color spaces.
framebuffer.h
Frame buffer handling.
geometry.h
Data structures related to geometric objects.
formats.h
Types and helper functions to handle libcamera image formats.
log.h
Logging infrastructure.
libcamera
Top-level libcamera namespace.
Definition backtrace.h:17
libcamera::operator<<
std::ostream & operator<<(std::ostream &out, const Point &p)
Insert a text representation of a Point into an output stream.
Definition geometry.cpp:91
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 framebuffer.h:54
libcamera::FrameBuffer::Plane::fd
SharedFD fd
The dmabuf file descriptor.
Definition framebuffer.h:56
libcamera::FrameBuffer::Plane::length
unsigned int length
The plane length in bytes.
Definition framebuffer.h:58
libcamera::V4L2Capability
struct v4l2_capability object wrapper and helpers
Definition v4l2_videodevice.h:45
libcamera::V4L2Capability::hasStreaming
bool hasStreaming() const
Determine if the video device can perform Streaming I/O.
Definition v4l2_videodevice.h:115
libcamera::V4L2Capability::bus_info
const char * bus_info() const
Retrieve the location of the video device in the system.
Definition v4l2_videodevice.h:54
libcamera::V4L2Capability::isVideoCapture
bool isVideoCapture() const
Identify if the video device captures images.
Definition v4l2_videodevice.h:99
libcamera::V4L2Capability::isMultiplanar
bool isMultiplanar() const
Identify if the video device implements the V4L2 multiplanar APIs.
Definition v4l2_videodevice.h:64
libcamera::V4L2Capability::isOutput
bool isOutput() const
Identify if the video device outputs data.
Definition v4l2_videodevice.h:76
libcamera::V4L2Capability::isMetaOutput
bool isMetaOutput() const
Identify if the video device outputs image meta-data.
Definition v4l2_videodevice.h:111
libcamera::V4L2Capability::isMeta
bool isMeta() const
Identify if the video device captures or outputs image meta-data.
Definition v4l2_videodevice.h:94
libcamera::V4L2Capability::isMetaCapture
bool isMetaCapture() const
Identify if the video device captures image meta-data.
Definition v4l2_videodevice.h:107
libcamera::V4L2Capability::hasMediaController
bool hasMediaController() const
Determine if the video device uses Media Controller to configure I/O.
Definition v4l2_videodevice.h:119
libcamera::V4L2Capability::driver
const char * driver() const
Retrieve the driver module name.
Definition v4l2_videodevice.h:46
libcamera::V4L2Capability::isM2M
bool isM2M() const
Identify if the device is a Memory-to-Memory device.
Definition v4l2_videodevice.h:89
libcamera::V4L2Capability::isVideoOutput
bool isVideoOutput() const
Identify if the video device outputs images.
Definition v4l2_videodevice.h:103
libcamera::V4L2Capability::isVideo
bool isVideo() const
Identify if the video device captures or outputs images.
Definition v4l2_videodevice.h:82
libcamera::V4L2Capability::isCapture
bool isCapture() const
Identify if the video device captures data.
Definition v4l2_videodevice.h:70
libcamera::V4L2Capability::device_caps
unsigned int device_caps() const
Retrieve the capabilities of the video device.
Definition v4l2_videodevice.h:58
libcamera::V4L2Capability::card
const char * card() const
Retrieve the video device card name.
Definition v4l2_videodevice.h:50
libcamera::V4L2DeviceFormat::Plane
Per-plane memory size information.
Definition v4l2_videodevice.h:171
libcamera::V4L2DeviceFormat::Plane::size
uint32_t size
The plane total memory size (in bytes).
Definition v4l2_videodevice.h:172
libcamera::V4L2DeviceFormat::Plane::bpl
uint32_t bpl
The plane line stride (in bytes).
Definition v4l2_videodevice.h:173
timer.h
Generic timer.
unique_fd.h
File descriptor wrapper that owns a file descriptor.
utils.h
Miscellaneous utility functions.
v4l2_device.h
Common base for V4L2 devices and subdevices.
v4l2_pixelformat.h
V4L2 Pixel Format.
v4l2_request.h
V4L2 Request.