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