libcamera  v0.3.1+12-19bbca3c
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
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 
18 #include <libcamera/base/class.h>
19 #include <libcamera/base/log.h>
20 
21 #include <libcamera/color_space.h>
22 #include <libcamera/geometry.h>
23 
27 
28 namespace libcamera {
29 
30 class MediaDevice;
31 
33 {
34 public:
35  enum class Type {
36  Image,
37  Metadata,
38  EmbeddedData,
39  };
40 
41  bool isValid() const { return code != 0; }
42 
43  static const MediaBusFormatInfo &info(uint32_t code);
44 
45  const char *name;
46  uint32_t code;
48  unsigned int bitsPerPixel;
50 };
51 
52 struct V4L2SubdeviceCapability final : v4l2_subdev_capability {
53  bool isReadOnly() const
54  {
55  return capabilities & V4L2_SUBDEV_CAP_RO_SUBDEV;
56  }
57  bool hasStreams() const
58  {
59  return capabilities & V4L2_SUBDEV_CAP_STREAMS;
60  }
61 };
62 
64  uint32_t code;
66  std::optional<ColorSpace> colorSpace;
67 
68  const std::string toString() const;
69 };
70 
71 std::ostream &operator<<(std::ostream &out, const V4L2SubdeviceFormat &f);
72 
73 class V4L2Subdevice : public V4L2Device
74 {
75 public:
76  using Formats = std::map<unsigned int, std::vector<SizeRange>>;
77 
78  enum Whence {
79  TryFormat = V4L2_SUBDEV_FORMAT_TRY,
80  ActiveFormat = V4L2_SUBDEV_FORMAT_ACTIVE,
81  };
82 
83  struct Stream {
85  : pad(0), stream(0)
86  {
87  }
88 
89  Stream(unsigned int p, unsigned int s)
90  : pad(p), stream(s)
91  {
92  }
93 
94  unsigned int pad;
95  unsigned int stream;
96  };
97 
98  struct Route {
100  : flags(0)
101  {
102  }
103 
104  Route(const Stream &snk, const Stream &src, uint32_t f)
105  : sink(snk), source(src), flags(f)
106  {
107  }
108 
111  uint32_t flags;
112  };
113 
114  using Routing = std::vector<Route>;
115 
116  explicit V4L2Subdevice(const MediaEntity *entity);
117  ~V4L2Subdevice();
118 
119  int open();
120 
121  const MediaEntity *entity() const { return entity_; }
122 
123  int getSelection(const Stream &stream, unsigned int target,
124  Rectangle *rect);
125  int getSelection(unsigned int pad, unsigned int target, Rectangle *rect)
126  {
127  return getSelection({ pad, 0 }, target, rect);
128  }
129  int setSelection(const Stream &stream, unsigned int target,
130  Rectangle *rect);
131  int setSelection(unsigned int pad, unsigned int target, Rectangle *rect)
132  {
133  return setSelection({ pad, 0 }, target, rect);
134  }
135 
136  Formats formats(const Stream &stream);
137  Formats formats(unsigned int pad)
138  {
139  return formats({ pad, 0 });
140  }
141 
142  int getFormat(const Stream &stream, V4L2SubdeviceFormat *format,
143  Whence whence = ActiveFormat);
144  int getFormat(unsigned int pad, V4L2SubdeviceFormat *format,
145  Whence whence = ActiveFormat)
146  {
147  return getFormat({ pad, 0 }, format, whence);
148  }
149  int setFormat(const Stream &stream, V4L2SubdeviceFormat *format,
150  Whence whence = ActiveFormat);
151  int setFormat(unsigned int pad, V4L2SubdeviceFormat *format,
152  Whence whence = ActiveFormat)
153  {
154  return setFormat({ pad, 0 }, format, whence);
155  }
156 
157  int getRouting(Routing *routing, Whence whence = ActiveFormat);
158  int setRouting(Routing *routing, Whence whence = ActiveFormat);
159 
160  const std::string &model();
161  const V4L2SubdeviceCapability &caps() const { return caps_; }
162 
163  static std::unique_ptr<V4L2Subdevice>
164  fromEntityName(const MediaDevice *media, const std::string &entity);
165 
166 protected:
167  std::string logPrefix() const override;
168 
169 private:
171 
172  std::optional<ColorSpace>
173  toColorSpace(const v4l2_mbus_framefmt &format) const;
174 
175  std::vector<unsigned int> enumPadCodes(const Stream &stream);
176  std::vector<SizeRange> enumPadSizes(const Stream &stream,
177  unsigned int code);
178 
179  int getRoutingLegacy(Routing *routing, Whence whence);
180  int setRoutingLegacy(Routing *routing, Whence whence);
181 
182  const MediaEntity *entity_;
183 
184  std::string model_;
185  struct V4L2SubdeviceCapability caps_;
186 };
187 
188 bool operator==(const V4L2Subdevice::Stream &lhs, const V4L2Subdevice::Stream &rhs);
189 static inline bool operator!=(const V4L2Subdevice::Stream &lhs,
190  const V4L2Subdevice::Stream &rhs)
191 {
192  return !(lhs == rhs);
193 }
194 
195 std::ostream &operator<<(std::ostream &out, const V4L2Subdevice::Stream &stream);
196 std::ostream &operator<<(std::ostream &out, const V4L2Subdevice::Route &route);
197 std::ostream &operator<<(std::ostream &out, const V4L2Subdevice::Routing &routing);
198 
199 } /* namespace libcamera */
Provides a class hierarchy that represents the media objects exposed by the Linux kernel Media Contro...
Size size
The image size in pixels.
Definition: v4l2_subdevice.h:65
Utilities to help constructing class interfaces.
The MediaDevice represents a Media Controller device with its full graph of connected objects...
Definition: media_device.h:25
Base class for V4L2VideoDevice and V4L2Subdevice.
Definition: v4l2_device.h:31
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:144
Information about media bus formats.
Definition: v4l2_subdevice.h:32
unsigned int stream
The stream number.
Definition: v4l2_subdevice.h:95
Stream source
The source stream of the route.
Definition: v4l2_subdevice.h:110
Class and enums to represent color spaces.
uint32_t code
The image format bus code.
Definition: v4l2_subdevice.h:64
std::optional< ColorSpace > colorSpace
The color space of the pixels.
Definition: v4l2_subdevice.h:66
Route()
Construct a Route with default streams.
Definition: v4l2_subdevice.h:99
Top-level libcamera namespace.
Definition: backtrace.h:17
uint32_t code
The media bus format code described by this instance (MEDIA_BUS_FMT_*)
Definition: v4l2_subdevice.h:46
Stream sink
The sink stream of the route.
Definition: v4l2_subdevice.h:109
const MediaEntity * entity() const
Retrieve the media entity associated with the subdevice.
Definition: v4l2_subdevice.h:121
int setSelection(unsigned int pad, unsigned int target, Rectangle *rect)
Set selection rectangle rect for target.
Definition: v4l2_subdevice.h:131
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:151
bool operator==(const V4L2Subdevice::Stream &lhs, const V4L2Subdevice::Stream &rhs)
Compare streams for equality.
Definition: v4l2_subdevice.cpp:904
Describe a two-dimensional size.
Definition: geometry.h:52
std::map< unsigned int, std::vector< SizeRange > > Formats
A map of supported media bus formats to frame sizes.
Definition: v4l2_subdevice.h:76
The MediaEntity represents an entity in the media graph.
Definition: media_object.h:88
std::vector< Route > Routing
V4L2 subdevice routing table.
Definition: v4l2_subdevice.h:114
ColourEncoding
The colour encoding type.
Definition: formats.h:24
Type
The format type.
Definition: v4l2_subdevice.h:35
int getSelection(unsigned int pad, unsigned int target, Rectangle *rect)
Get selection rectangle rect for target.
Definition: v4l2_subdevice.h:125
#define LIBCAMERA_DISABLE_COPY(klass)
Disable copy construction and assignment of the klass.
Describe a rectangle&#39;s position and dimensions.
Definition: geometry.h:242
Type type
The media bus format type.
Definition: v4l2_subdevice.h:47
bool isValid() const
Check if the media bus format info is valid.
Definition: v4l2_subdevice.h:41
Common base for V4L2 devices and subdevices.
V4L2 subdevice stream.
Definition: v4l2_subdevice.h:83
The V4L2 sub-device image format and sizes.
Definition: v4l2_subdevice.h:63
std::ostream & operator<<(std::ostream &out, const Point &p)
Insert a text representation of a Point into an output stream.
Definition: geometry.cpp:91
const V4L2SubdeviceCapability & caps() const
Retrieve the subdevice V4L2 capabilities.
Definition: v4l2_subdevice.h:161
bool hasStreams() const
Retrieve if a subdevice supports the V4L2 streams API.
Definition: v4l2_subdevice.h:57
Formats formats(unsigned int pad)
Enumerate all media bus codes and frame sizes on a pad.
Definition: v4l2_subdevice.h:137
bool isReadOnly() const
Retrieve if a subdevice is registered as read-only.
Definition: v4l2_subdevice.h:53
V4L2 subdevice routing table entry.
Definition: v4l2_subdevice.h:98
Stream(unsigned int p, unsigned int s)
Construct a Stream with a given pad and stream number.
Definition: v4l2_subdevice.h:89
Route(const Stream &snk, const Stream &src, uint32_t f)
Construct a Route from sink to source.
Definition: v4l2_subdevice.h:104
static const MediaBusFormatInfo & info(uint32_t code)
Retrieve information about a media bus format.
Definition: v4l2_subdevice.cpp:720
uint32_t flags
The route flags (V4L2_SUBDEV_ROUTE_FL_*)
Definition: v4l2_subdevice.h:111
unsigned int pad
The 0-indexed pad number.
Definition: v4l2_subdevice.h:94
const char * name
The format name as a human-readable string, used as the text representation of the format...
Definition: v4l2_subdevice.h:45
Data structures related to geometric objects.
Whence
Specify the type of format for getFormat() and setFormat() operations.
Definition: v4l2_subdevice.h:78
Types and helper functions to handle libcamera image formats.
Logging infrastructure.
Stream()
Construct a Stream with pad and stream set to 0.
Definition: v4l2_subdevice.h:84
PixelFormatInfo::ColourEncoding colourEncoding
The colour encoding type.
Definition: v4l2_subdevice.h:49
A V4L2 subdevice as exposed by the Linux kernel.
Definition: v4l2_subdevice.h:73
unsigned int bitsPerPixel
The average number of bits per pixel.
Definition: v4l2_subdevice.h:48
struct v4l2_subdev_capability object wrapper and helpers
Definition: v4l2_subdevice.h:52