libcamera v0.1.0+127-8e215127-dirty (2023-12-02T01:06:12+00:00)
Supporting cameras in Linux since 2019
v4l2_subdevice.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_subdevice.h - V4L2 Subdevice
6 */
7
8#pragma once
9
10#include <memory>
11#include <optional>
12#include <ostream>
13#include <string>
14#include <vector>
15
16#include <linux/v4l2-subdev.h>
17
19#include <libcamera/base/log.h>
20
22#include <libcamera/geometry.h>
23
27
28namespace libcamera {
29
30class MediaDevice;
31
32struct V4L2SubdeviceCapability final : v4l2_subdev_capability {
33 bool isReadOnly() const
34 {
35 return capabilities & V4L2_SUBDEV_CAP_RO_SUBDEV;
36 }
37 bool hasStreams() const
38 {
39 return capabilities & V4L2_SUBDEV_CAP_MPLEXED;
40 }
41};
42
44 uint32_t mbus_code;
46 std::optional<ColorSpace> colorSpace;
47
48 const std::string toString() const;
49 uint8_t bitsPerPixel() const;
50};
51
52std::ostream &operator<<(std::ostream &out, const V4L2SubdeviceFormat &f);
53
55{
56public:
57 using Formats = std::map<unsigned int, std::vector<SizeRange>>;
58
59 enum Whence {
60 TryFormat = V4L2_SUBDEV_FORMAT_TRY,
61 ActiveFormat = V4L2_SUBDEV_FORMAT_ACTIVE,
62 };
63
64 class Routing : public std::vector<struct v4l2_subdev_route>
65 {
66 public:
67 std::string toString() const;
68 };
69
70 explicit V4L2Subdevice(const MediaEntity *entity);
72
73 int open();
74
75 const MediaEntity *entity() const { return entity_; }
76
77 int getSelection(unsigned int pad, unsigned int target,
78 Rectangle *rect);
79 int setSelection(unsigned int pad, unsigned int target,
80 Rectangle *rect);
81
82 Formats formats(unsigned int pad);
83
84 int getFormat(unsigned int pad, V4L2SubdeviceFormat *format,
85 Whence whence = ActiveFormat);
86 int setFormat(unsigned int pad, V4L2SubdeviceFormat *format,
87 Whence whence = ActiveFormat);
88
89 int getRouting(Routing *routing, Whence whence = ActiveFormat);
90 int setRouting(Routing *routing, Whence whence = ActiveFormat);
91
92 const std::string &model();
93 const V4L2SubdeviceCapability &caps() const { return caps_; }
94
95 static std::unique_ptr<V4L2Subdevice>
96 fromEntityName(const MediaDevice *media, const std::string &entity);
97
98protected:
99 std::string logPrefix() const override;
100
101private:
103
104 std::optional<ColorSpace>
105 toColorSpace(const v4l2_mbus_framefmt &format) const;
106
107 std::vector<unsigned int> enumPadCodes(unsigned int pad);
108 std::vector<SizeRange> enumPadSizes(unsigned int pad,
109 unsigned int code);
110
111 const MediaEntity *entity_;
112
113 std::string model_;
114 struct V4L2SubdeviceCapability caps_;
115};
116
117} /* namespace libcamera */
Utilities to help constructing class interfaces.
#define LIBCAMERA_DISABLE_COPY(klass)
Disable copy construction and assignment of the klass.
The MediaDevice represents a Media Controller device with its full graph of connected objects.
Definition: media_device.h:26
The MediaEntity represents an entity in the media graph.
Definition: media_object.h:89
Describe a rectangle's position and dimensions.
Definition: geometry.h:243
Describe a two-dimensional size.
Definition: geometry.h:53
Base class for V4L2VideoDevice and V4L2Subdevice.
Definition: v4l2_device.h:32
V4L2 subdevice routing table.
Definition: v4l2_subdevice.h:65
std::string toString() const
Assemble and return a string describing the routing table.
Definition: v4l2_subdevice.cpp:308
A V4L2 subdevice as exposed by the Linux kernel.
Definition: v4l2_subdevice.h:55
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:735
int open()
Open a V4L2 subdevice.
Definition: v4l2_subdevice.cpp:342
int setSelection(unsigned int pad, unsigned int target, Rectangle *rect)
Set selection rectangle rect for target.
Definition: v4l2_subdevice.cpp:417
std::string logPrefix() const override
Retrieve a string to be prefixed to the log message.
Definition: v4l2_subdevice.cpp:745
int setRouting(Routing *routing, Whence whence=ActiveFormat)
Set a routing table on the V4L2 subdevice.
Definition: v4l2_subdevice.cpp:647
int getSelection(unsigned int pad, unsigned int target, Rectangle *rect)
Get selection rectangle rect for target.
Definition: v4l2_subdevice.cpp:381
const V4L2SubdeviceCapability & caps() const
Retrieve the subdevice V4L2 capabilities.
Definition: v4l2_subdevice.h:93
Formats formats(unsigned int pad)
Enumerate all media bus codes and frame sizes on a pad.
Definition: v4l2_subdevice.cpp:456
std::map< unsigned int, std::vector< SizeRange > > Formats
A map of supported media bus formats to frame sizes.
Definition: v4l2_subdevice.h:57
Whence
Specify the type of format for getFormat() and setFormat() operations.
Definition: v4l2_subdevice.h:59
@ ActiveFormat
The format operation applies to ACTIVE formats.
Definition: v4l2_subdevice.h:61
@ TryFormat
The format operation applies to TRY formats.
Definition: v4l2_subdevice.h:60
int setFormat(unsigned int pad, V4L2SubdeviceFormat *format, Whence whence=ActiveFormat)
Set an image format on one of the V4L2 subdevice pads.
Definition: v4l2_subdevice.cpp:555
const std::string & model()
Retrieve the model name of the device.
Definition: v4l2_subdevice.cpp:683
V4L2Subdevice(const MediaEntity *entity)
Create a V4L2 subdevice from a MediaEntity using its device node path.
Definition: v4l2_subdevice.cpp:328
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.cpp:520
int getRouting(Routing *routing, Whence whence=ActiveFormat)
Retrieve the subdevice's internal routing table.
Definition: v4l2_subdevice.cpp:597
const MediaEntity * entity() const
Retrieve the media entity associated with the subdevice.
Definition: v4l2_subdevice.h:75
Class and enums to represent color spaces.
Data structures related to geometric objects.
Types and helper functions to handle libcamera image formats.
Logging infrastructure.
Provides a class hierarchy that represents the media objects exposed by the Linux kernel Media Contro...
Top-level libcamera namespace.
Definition: backtrace.h:17
std::ostream & operator<<(std::ostream &out, const Point &p)
Insert a text representation of a Point into an output stream.
Definition: geometry.cpp:91
struct v4l2_subdev_capability object wrapper and helpers
Definition: v4l2_subdevice.h:32
bool isReadOnly() const
Retrieve if a subdevice is registered as read-only.
Definition: v4l2_subdevice.h:33
bool hasStreams() const
Retrieve if a subdevice supports the V4L2 streams API.
Definition: v4l2_subdevice.h:37
The V4L2 sub-device image format and sizes.
Definition: v4l2_subdevice.h:43
std::optional< ColorSpace > colorSpace
The color space of the pixels.
Definition: v4l2_subdevice.h:46
uint32_t mbus_code
The image format bus code.
Definition: v4l2_subdevice.h:44
const std::string toString() const
Assemble and return a string describing the format.
Definition: v4l2_subdevice.cpp:223
uint8_t bitsPerPixel() const
Retrieve the number of bits per pixel for the V4L2 subdevice format.
Definition: v4l2_subdevice.cpp:236
Size size
The image size in pixels.
Definition: v4l2_subdevice.h:45
Common base for V4L2 devices and subdevices.