Loading...
Searching...
No Matches
v4l2_subdevice.h
Go to the documentation of this file.
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
* Copyright (C) 2019, Google Inc.
*
* V4L2 Subdevice
*/
#pragma once
#include <memory>
#include <optional>
#include <ostream>
#include <stdint.h>
#include <string>
#include <vector>
#include <linux/v4l2-subdev.h>
#include <libcamera/base/class.h>
#include <libcamera/base/log.h>
#include <libcamera/base/regex.h>
#include <libcamera/color_space.h>
#include <libcamera/geometry.h>
#include "libcamera/internal/formats.h"
#include "libcamera/internal/media_object.h"
#include "libcamera/internal/v4l2_device.h"
namespace libcamera {
class MediaDevice;
class MediaBusFormatInfo
{
public:
enum class Type {
Image,
Metadata,
EmbeddedData,
};
bool isValid() const { return code != 0; }
static const MediaBusFormatInfo &info(uint32_t code);
const char *name;
uint32_t code;
Type type;
unsigned int bitsPerPixel;
PixelFormatInfo::ColourEncoding colourEncoding;
};
struct V4L2SubdeviceCapability final : v4l2_subdev_capability {
bool isReadOnly() const
{
return capabilities & V4L2_SUBDEV_CAP_RO_SUBDEV;
}
bool hasStreams() const
{
return capabilities & V4L2_SUBDEV_CAP_STREAMS;
}
};
struct V4L2SubdeviceFormat {
uint32_t code;
Size size;
std::optional<ColorSpace> colorSpace;
std::string toString() const;
};
std::ostream &operator<<(std::ostream &out, const V4L2SubdeviceFormat &f);
class V4L2Subdevice : public V4L2Device
{
public:
using Formats = std::map<unsigned int, std::vector<SizeRange>>;
enum Whence {
TryFormat = V4L2_SUBDEV_FORMAT_TRY,
ActiveFormat = V4L2_SUBDEV_FORMAT_ACTIVE,
};
struct Stream {
Stream()
: pad(0), stream(0)
{
}
Stream(unsigned int p, unsigned int s)
: pad(p), stream(s)
{
}
unsigned int pad;
unsigned int stream;
};
struct Route {
Route()
: flags(0)
{
}
Route(const Stream &snk, const Stream &src, uint32_t f)
: sink(snk), source(src), flags(f)
{
}
Stream sink;
Stream source;
uint32_t flags;
};
using Routing = std::vector<Route>;
explicit V4L2Subdevice(const MediaEntity *entity);
~V4L2Subdevice();
int open();
const MediaEntity *entity() const { return entity_; }
int getSelection(const Stream &stream, unsigned int target,
Rectangle *rect);
int getSelection(unsigned int pad, unsigned int target, Rectangle *rect)
{
return getSelection({ pad, 0 }, target, rect);
}
int setSelection(const Stream &stream, unsigned int target,
Rectangle *rect);
int setSelection(unsigned int pad, unsigned int target, Rectangle *rect)
{
return setSelection({ pad, 0 }, target, rect);
}
Formats formats(const Stream &stream);
Formats formats(unsigned int pad)
{
return formats({ pad, 0 });
}
int getFormat(const Stream &stream, V4L2SubdeviceFormat *format,
Whence whence = ActiveFormat);
int getFormat(unsigned int pad, V4L2SubdeviceFormat *format,
Whence whence = ActiveFormat)
{
return getFormat({ pad, 0 }, format, whence);
}
int setFormat(const Stream &stream, V4L2SubdeviceFormat *format,
Whence whence = ActiveFormat);
int setFormat(unsigned int pad, V4L2SubdeviceFormat *format,
Whence whence = ActiveFormat)
{
return setFormat({ pad, 0 }, format, whence);
}
int getRouting(Routing *routing, Whence whence = ActiveFormat);
int setRouting(Routing *routing, Whence whence = ActiveFormat);
const std::string &model();
const V4L2SubdeviceCapability &caps() const { return caps_; }
static std::unique_ptr<V4L2Subdevice>
fromEntityName(const MediaDevice *media, const std::string &entity);
static std::unique_ptr<V4L2Subdevice>
fromEntityName(const MediaDevice *media, const std::regex &entity);
protected:
std::string logPrefix() const override;
private:
LIBCAMERA_DISABLE_COPY(V4L2Subdevice)
std::optional<ColorSpace>
toColorSpace(const v4l2_mbus_framefmt &format) const;
std::vector<unsigned int> enumPadCodes(const Stream &stream);
std::vector<SizeRange> enumPadSizes(const Stream &stream,
unsigned int code);
int getRoutingLegacy(Routing *routing, Whence whence);
int setRoutingLegacy(Routing *routing, Whence whence);
const MediaEntity *entity_;
std::string model_;
struct V4L2SubdeviceCapability caps_;
};
bool operator==(const V4L2Subdevice::Stream &lhs, const V4L2Subdevice::Stream &rhs);
static inline bool operator!=(const V4L2Subdevice::Stream &lhs,
const V4L2Subdevice::Stream &rhs)
{
return !(lhs == rhs);
}
std::ostream &operator<<(std::ostream &out, const V4L2Subdevice::Stream &stream);
std::ostream &operator<<(std::ostream &out, const V4L2Subdevice::Route &route);
std::ostream &operator<<(std::ostream &out, const V4L2Subdevice::Routing &routing);
} /* 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::MediaBusFormatInfo
Information about media bus formats.
Definition v4l2_subdevice.h:35
libcamera::MediaBusFormatInfo::type
Type type
The media bus format type.
Definition v4l2_subdevice.h:49
libcamera::MediaBusFormatInfo::isValid
bool isValid() const
Check if the media bus format info is valid.
Definition v4l2_subdevice.h:43
libcamera::MediaBusFormatInfo::code
uint32_t code
The media bus format code described by this instance (MEDIA_BUS_FMT_*).
Definition v4l2_subdevice.h:48
libcamera::MediaBusFormatInfo::name
const char * name
The format name as a human-readable string, used as the text representation of the format.
Definition v4l2_subdevice.h:47
libcamera::MediaBusFormatInfo::bitsPerPixel
unsigned int bitsPerPixel
The average number of bits per pixel.
Definition v4l2_subdevice.h:50
libcamera::MediaBusFormatInfo::info
static const MediaBusFormatInfo & info(uint32_t code)
Retrieve information about a media bus format.
Definition v4l2_subdevice.cpp:833
libcamera::MediaBusFormatInfo::colourEncoding
PixelFormatInfo::ColourEncoding colourEncoding
The colour encoding type.
Definition v4l2_subdevice.h:51
libcamera::MediaBusFormatInfo::Type
Type
The format type.
Definition v4l2_subdevice.h:37
libcamera::MediaBusFormatInfo::Type::EmbeddedData
@ EmbeddedData
The format describes sensor embedded data.
Definition v4l2_subdevice.h:40
libcamera::MediaBusFormatInfo::Type::Image
@ Image
The format describes image data.
Definition v4l2_subdevice.h:38
libcamera::MediaBusFormatInfo::Type::Metadata
@ Metadata
The format describes generic metadata.
Definition v4l2_subdevice.h:39
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::ColourEncoding
ColourEncoding
The colour encoding type.
Definition formats.h:23
libcamera::Rectangle
Describe a rectangle's position and dimensions.
Definition geometry.h:247
libcamera::Size
Describe a two-dimensional size.
Definition geometry.h:51
libcamera::Stream
Video stream for a camera.
Definition stream.h:76
libcamera::V4L2Device::V4L2Device
V4L2Device(const std::string &deviceNode)
Construct a V4L2Device.
Definition v4l2_device.cpp:59
libcamera::V4L2Subdevice
A V4L2 subdevice as exposed by the Linux kernel.
Definition v4l2_subdevice.h:76
libcamera::V4L2Subdevice::fromEntityName
static std::unique_ptr< V4L2Subdevice > fromEntityName(const MediaDevice *media, const std::string &entity)
Create a new video subdevice instance from entity in media device media.
Definition v4l2_subdevice.cpp:1762
libcamera::V4L2Subdevice::open
int open()
Open a V4L2 subdevice.
Definition v4l2_subdevice.cpp:1134
libcamera::V4L2Subdevice::setSelection
int setSelection(unsigned int pad, unsigned int target, Rectangle *rect)
Set selection rectangle rect for target.
Definition v4l2_subdevice.h:133
libcamera::V4L2Subdevice::logPrefix
std::string logPrefix() const override
Retrieve a string to be prefixed to the log message.
Definition v4l2_subdevice.cpp:1790
libcamera::V4L2Subdevice::setRouting
int setRouting(Routing *routing, Whence whence=ActiveFormat)
Set a routing table on the V4L2 subdevice.
Definition v4l2_subdevice.cpp:1638
libcamera::V4L2Subdevice::getSelection
int getSelection(unsigned int pad, unsigned int target, Rectangle *rect)
Get selection rectangle rect for target.
Definition v4l2_subdevice.h:127
libcamera::V4L2Subdevice::setSelection
int setSelection(const Stream &stream, unsigned int target, Rectangle *rect)
Set selection rectangle rect for target.
Definition v4l2_subdevice.cpp:1236
libcamera::V4L2Subdevice::caps
const V4L2SubdeviceCapability & caps() const
Retrieve the subdevice V4L2 capabilities.
Definition v4l2_subdevice.h:163
libcamera::V4L2Subdevice::getSelection
int getSelection(const Stream &stream, unsigned int target, Rectangle *rect)
Get selection rectangle rect for target.
Definition v4l2_subdevice.cpp:1188
libcamera::V4L2Subdevice::Formats
std::map< unsigned int, std::vector< SizeRange > > Formats
A map of supported media bus formats to frame sizes.
Definition v4l2_subdevice.h:78
libcamera::V4L2Subdevice::Whence
Whence
Specify the type of format for getFormat() and setFormat() operations.
Definition v4l2_subdevice.h:80
libcamera::V4L2Subdevice::TryFormat
@ TryFormat
The format operation applies to TRY formats.
Definition v4l2_subdevice.h:81
libcamera::V4L2Subdevice::ActiveFormat
@ ActiveFormat
The format operation applies to ACTIVE formats.
Definition v4l2_subdevice.h:82
libcamera::V4L2Subdevice::formats
Formats formats(const Stream &stream)
Enumerate all media bus codes and frame sizes on a stream.
Definition v4l2_subdevice.cpp:1290
libcamera::V4L2Subdevice::setFormat
int setFormat(unsigned int pad, V4L2SubdeviceFormat *format, Whence whence=ActiveFormat)
Set an image format on one of the V4L2 subdevice pads.
Definition v4l2_subdevice.h:153
libcamera::V4L2Subdevice::entity
const MediaEntity * entity() const
Retrieve the media entity associated with the subdevice.
Definition v4l2_subdevice.h:123
libcamera::V4L2Subdevice::setFormat
int setFormat(const Stream &stream, V4L2SubdeviceFormat *format, Whence whence=ActiveFormat)
Set an image format on one of the V4L2 subdevice pads.
Definition v4l2_subdevice.cpp:1412
libcamera::V4L2Subdevice::model
const std::string & model()
Retrieve the model name of the device.
Definition v4l2_subdevice.cpp:1712
libcamera::V4L2Subdevice::V4L2Subdevice
V4L2Subdevice(const MediaEntity *entity)
Create a V4L2 subdevice from a MediaEntity using its device node path.
Definition v4l2_subdevice.cpp:1120
libcamera::V4L2Subdevice::getFormat
int getFormat(unsigned int pad, V4L2SubdeviceFormat *format, Whence whence=ActiveFormat)
Retrieve the image format set on one of the V4L2 subdevice pads.
Definition v4l2_subdevice.h:146
libcamera::V4L2Subdevice::Routing
std::vector< Route > Routing
V4L2 subdevice routing table.
Definition v4l2_subdevice.h:116
libcamera::V4L2Subdevice::getFormat
int getFormat(const Stream &stream, V4L2SubdeviceFormat *format, Whence whence=ActiveFormat)
Retrieve the image format set on one of the V4L2 subdevice streams.
Definition v4l2_subdevice.cpp:1365
libcamera::V4L2Subdevice::getRouting
int getRouting(Routing *routing, Whence whence=ActiveFormat)
Retrieve the subdevice's internal routing table.
Definition v4l2_subdevice.cpp:1548
libcamera::V4L2Subdevice::formats
Formats formats(unsigned int pad)
Enumerate all media bus codes and frame sizes on a pad.
Definition v4l2_subdevice.h:139
color_space.h
Class and enums to represent color spaces.
geometry.h
Data structures related to geometric objects.
formats.h
Types and helper functions to handle libcamera image formats.
log.h
Logging infrastructure.
media_object.h
Provides a class hierarchy that represents the media objects exposed by the Linux kernel Media Contro...
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
libcamera::V4L2SubdeviceCapability
struct v4l2_subdev_capability object wrapper and helpers
Definition v4l2_subdevice.h:54
libcamera::V4L2SubdeviceCapability::hasStreams
bool hasStreams() const
Retrieve if a subdevice supports the V4L2 streams API.
Definition v4l2_subdevice.h:59
libcamera::V4L2SubdeviceCapability::isReadOnly
bool isReadOnly() const
Retrieve if a subdevice is registered as read-only.
Definition v4l2_subdevice.h:55
libcamera::V4L2SubdeviceFormat
The V4L2 sub-device image format and sizes.
Definition v4l2_subdevice.h:65
libcamera::V4L2SubdeviceFormat::size
Size size
The image size in pixels.
Definition v4l2_subdevice.h:67
libcamera::V4L2SubdeviceFormat::colorSpace
std::optional< ColorSpace > colorSpace
The color space of the pixels.
Definition v4l2_subdevice.h:68
libcamera::V4L2SubdeviceFormat::code
uint32_t code
The image format bus code.
Definition v4l2_subdevice.h:66
libcamera::V4L2SubdeviceFormat::toString
std::string toString() const
Assemble and return a string describing the format.
Definition v4l2_subdevice.cpp:929
libcamera::V4L2Subdevice::Route
V4L2 subdevice routing table entry.
Definition v4l2_subdevice.h:100
libcamera::V4L2Subdevice::Route::source
Stream source
The source stream of the route.
Definition v4l2_subdevice.h:112
libcamera::V4L2Subdevice::Route::flags
uint32_t flags
The route flags (V4L2_SUBDEV_ROUTE_FL_*).
Definition v4l2_subdevice.h:113
libcamera::V4L2Subdevice::Route::sink
Stream sink
The sink stream of the route.
Definition v4l2_subdevice.h:111
libcamera::V4L2Subdevice::Route::Route
Route()
Construct a Route with default streams.
Definition v4l2_subdevice.h:101
libcamera::V4L2Subdevice::Route::Route
Route(const Stream &snk, const Stream &src, uint32_t f)
Construct a Route from sink to source.
Definition v4l2_subdevice.h:106
libcamera::V4L2Subdevice::Stream
V4L2 subdevice stream.
Definition v4l2_subdevice.h:85
libcamera::V4L2Subdevice::Stream::Stream
Stream(unsigned int p, unsigned int s)
Construct a Stream with a given pad and stream number.
Definition v4l2_subdevice.h:91
libcamera::V4L2Subdevice::Stream::Stream
Stream()
Construct a Stream with pad and stream set to 0.
Definition v4l2_subdevice.h:86
libcamera::V4L2Subdevice::Stream::pad
unsigned int pad
The 0-indexed pad number.
Definition v4l2_subdevice.h:96
libcamera::V4L2Subdevice::Stream::stream
unsigned int stream
The stream number.
Definition v4l2_subdevice.h:97
v4l2_device.h
Common base for V4L2 devices and subdevices.