libcamera  v0.0.4
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 
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 #include <libcamera/transform.h>
24 
28 
29 namespace libcamera {
30 
31 class MediaDevice;
32 
33 struct V4L2SubdeviceCapability final : v4l2_subdev_capability {
34  bool isReadOnly() const
35  {
36  return capabilities & V4L2_SUBDEV_CAP_RO_SUBDEV;
37  }
38  bool hasStreams() const
39  {
40  return capabilities & V4L2_SUBDEV_CAP_MPLEXED;
41  }
42 };
43 
45  uint32_t mbus_code;
47  std::optional<ColorSpace> colorSpace;
49 
50  const std::string toString() const;
51  uint8_t bitsPerPixel() const;
52 };
53 
54 std::ostream &operator<<(std::ostream &out, const V4L2SubdeviceFormat &f);
55 
56 class V4L2Subdevice : public V4L2Device
57 {
58 public:
59  using Formats = std::map<unsigned int, std::vector<SizeRange>>;
60 
61  enum Whence {
62  TryFormat = V4L2_SUBDEV_FORMAT_TRY,
63  ActiveFormat = V4L2_SUBDEV_FORMAT_ACTIVE,
64  };
65 
66  class Routing : public std::vector<struct v4l2_subdev_route>
67  {
68  public:
69  std::string toString() const;
70  };
71 
72  explicit V4L2Subdevice(const MediaEntity *entity);
73  ~V4L2Subdevice();
74 
75  int open();
76 
77  const MediaEntity *entity() const { return entity_; }
78 
79  int getSelection(unsigned int pad, unsigned int target,
80  Rectangle *rect);
81  int setSelection(unsigned int pad, unsigned int target,
82  Rectangle *rect);
83 
84  Formats formats(unsigned int pad);
85 
86  int getFormat(unsigned int pad, V4L2SubdeviceFormat *format,
87  Whence whence = ActiveFormat);
88  int setFormat(unsigned int pad, V4L2SubdeviceFormat *format,
89  Whence whence = ActiveFormat);
90 
91  int getRouting(Routing *routing, Whence whence = ActiveFormat);
92  int setRouting(Routing *routing, Whence whence = ActiveFormat);
93 
94  const std::string &model();
95  const V4L2SubdeviceCapability &caps() const { return caps_; }
96 
97  static std::unique_ptr<V4L2Subdevice>
98  fromEntityName(const MediaDevice *media, const std::string &entity);
99 
100 protected:
101  std::string logPrefix() const override;
102 
103 private:
105 
106  std::optional<ColorSpace>
107  toColorSpace(const v4l2_mbus_framefmt &format) const;
108 
109  std::vector<unsigned int> enumPadCodes(unsigned int pad);
110  std::vector<SizeRange> enumPadSizes(unsigned int pad,
111  unsigned int code);
112 
113  const MediaEntity *entity_;
114 
115  std::string model_;
116  struct V4L2SubdeviceCapability caps_;
117 };
118 
119 } /* 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:67
std::string toString() const
Assemble and return a string describing the routing table.
Definition: v4l2_subdevice.cpp:315
A V4L2 subdevice as exposed by the Linux kernel.
Definition: v4l2_subdevice.h:57
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:742
int open()
Open a V4L2 subdevice.
Definition: v4l2_subdevice.cpp:349
int setSelection(unsigned int pad, unsigned int target, Rectangle *rect)
Set selection rectangle rect for target.
Definition: v4l2_subdevice.cpp:424
std::string logPrefix() const override
Retrieve a string to be prefixed to the log message.
Definition: v4l2_subdevice.cpp:752
int setRouting(Routing *routing, Whence whence=ActiveFormat)
Set a routing table on the V4L2 subdevice.
Definition: v4l2_subdevice.cpp:654
int getSelection(unsigned int pad, unsigned int target, Rectangle *rect)
Get selection rectangle rect for target.
Definition: v4l2_subdevice.cpp:388
const V4L2SubdeviceCapability & caps() const
Retrieve the subdevice V4L2 capabilities.
Definition: v4l2_subdevice.h:95
Formats formats(unsigned int pad)
Enumerate all media bus codes and frame sizes on a pad.
Definition: v4l2_subdevice.cpp:463
std::map< unsigned int, std::vector< SizeRange > > Formats
A map of supported media bus formats to frame sizes.
Definition: v4l2_subdevice.h:59
Whence
Specify the type of format for getFormat() and setFormat() operations.
Definition: v4l2_subdevice.h:61
@ ActiveFormat
The format operation applies to ACTIVE formats.
Definition: v4l2_subdevice.h:63
@ TryFormat
The format operation applies to TRY formats.
Definition: v4l2_subdevice.h:62
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:562
const std::string & model()
Retrieve the model name of the device.
Definition: v4l2_subdevice.cpp:690
V4L2Subdevice(const MediaEntity *entity)
Create a V4L2 subdevice from a MediaEntity using its device node path.
Definition: v4l2_subdevice.cpp:335
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:527
int getRouting(Routing *routing, Whence whence=ActiveFormat)
Retrieve the subdevice's internal routing table.
Definition: v4l2_subdevice.cpp:604
const MediaEntity * entity() const
Retrieve the media entity associated with the subdevice.
Definition: v4l2_subdevice.h:77
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
Transform
Enum to represent a 2D plane transform.
Definition: transform.h:14
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:33
bool isReadOnly() const
Retrieve if a subdevice is registered as read-only.
Definition: v4l2_subdevice.h:34
bool hasStreams() const
Retrieve if a subdevice supports the V4L2 streams API.
Definition: v4l2_subdevice.h:38
The V4L2 sub-device image format and sizes.
Definition: v4l2_subdevice.h:44
std::optional< ColorSpace > colorSpace
The color space of the pixels.
Definition: v4l2_subdevice.h:47
uint32_t mbus_code
The image format bus code.
Definition: v4l2_subdevice.h:45
const std::string toString() const
Assemble and return a string describing the format.
Definition: v4l2_subdevice.cpp:230
uint8_t bitsPerPixel() const
Retrieve the number of bits per pixel for the V4L2 subdevice format.
Definition: v4l2_subdevice.cpp:243
Size size
The image size in pixels.
Definition: v4l2_subdevice.h:46
Transform transform
The transform (vertical/horizontal flips) to be applied on the subdev.
Definition: v4l2_subdevice.h:48
Enum to represent and manipulate 2D plane transforms.
Common base for V4L2 devices and subdevices.