libcamera  v0.4.0+139-39419ce4
Supporting cameras in Linux since 2019
converter.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 /*
3  * Copyright (C) 2020, Laurent Pinchart
4  * Copyright 2022 NXP
5  *
6  * Generic format converter interface
7  */
8 
9 #pragma once
10 
11 #include <functional>
12 #include <initializer_list>
13 #include <map>
14 #include <memory>
15 #include <string>
16 #include <tuple>
17 #include <utility>
18 #include <vector>
19 
20 #include <libcamera/base/class.h>
21 #include <libcamera/base/flags.h>
22 #include <libcamera/base/signal.h>
23 
24 #include <libcamera/geometry.h>
25 
26 namespace libcamera {
27 
28 class FrameBuffer;
29 class MediaDevice;
30 class PixelFormat;
31 class Stream;
32 struct StreamConfiguration;
33 
34 class Converter
35 {
36 public:
37  enum class Feature {
38  None = 0,
39  InputCrop = (1 << 0),
40  };
41 
43 
44  enum class Alignment {
45  Down = 0,
46  Up,
47  };
48 
49  Converter(MediaDevice *media, Features features = Feature::None);
50  virtual ~Converter();
51 
52  virtual int loadConfiguration(const std::string &filename) = 0;
53 
54  virtual bool isValid() const = 0;
55 
56  virtual std::vector<PixelFormat> formats(PixelFormat input) = 0;
57  virtual SizeRange sizes(const Size &input) = 0;
58 
59  virtual Size adjustInputSize(const PixelFormat &pixFmt,
60  const Size &size,
61  Alignment align = Alignment::Down) = 0;
62  virtual Size adjustOutputSize(const PixelFormat &pixFmt,
63  const Size &size,
64  Alignment align = Alignment::Down) = 0;
65 
66  virtual std::tuple<unsigned int, unsigned int>
67  strideAndFrameSize(const PixelFormat &pixelFormat, const Size &size) = 0;
68 
69  virtual int validateOutput(StreamConfiguration *cfg, bool *adjusted,
70  Alignment align = Alignment::Down) = 0;
71 
72  virtual int configure(const StreamConfiguration &inputCfg,
73  const std::vector<std::reference_wrapper<StreamConfiguration>> &outputCfgs) = 0;
74  virtual bool isConfigured(const Stream *stream) const = 0;
75  virtual int exportBuffers(const Stream *stream, unsigned int count,
76  std::vector<std::unique_ptr<FrameBuffer>> *buffers) = 0;
77 
78  virtual int start() = 0;
79  virtual void stop() = 0;
80 
81  virtual int queueBuffers(FrameBuffer *input,
82  const std::map<const Stream *, FrameBuffer *> &outputs) = 0;
83 
84  virtual int setInputCrop(const Stream *stream, Rectangle *rect) = 0;
85  virtual std::pair<Rectangle, Rectangle> inputCropBounds() = 0;
86  virtual std::pair<Rectangle, Rectangle> inputCropBounds(const Stream *stream) = 0;
87 
90 
91  const std::string &deviceNode() const { return deviceNode_; }
92 
93  Features features() const { return features_; }
94 
95 protected:
97 
98 private:
99  std::string deviceNode_;
100 };
101 
103 {
104 public:
105  ConverterFactoryBase(const std::string name, std::initializer_list<std::string> compatibles);
106  virtual ~ConverterFactoryBase() = default;
107 
108  const std::vector<std::string> &compatibles() const { return compatibles_; }
109 
110  static std::unique_ptr<Converter> create(MediaDevice *media);
111  static std::vector<ConverterFactoryBase *> &factories();
112  static std::vector<std::string> names();
113 
114 private:
116 
117  static void registerType(ConverterFactoryBase *factory);
118 
119  virtual std::unique_ptr<Converter> createInstance(MediaDevice *media) const = 0;
120 
121  std::string name_;
122  std::vector<std::string> compatibles_;
123 };
124 
125 template<typename _Converter>
127 {
128 public:
129  ConverterFactory(const char *name, std::initializer_list<std::string> compatibles)
130  : ConverterFactoryBase(name, compatibles)
131  {
132  }
133 
134  std::unique_ptr<Converter> createInstance(MediaDevice *media) const override
135  {
136  return std::make_unique<_Converter>(media);
137  }
138 };
139 
140 #define REGISTER_CONVERTER(name, converter, compatibles) \
141  static ConverterFactory<converter> global_##converter##Factory(name, compatibles);
142 
143 } /* namespace libcamera */
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
libcamera image pixel format
Definition: pixel_format.h:16
virtual std::vector< PixelFormat > formats(PixelFormat input)=0
Retrieve the list of supported pixel formats for an input pixel format.
virtual int configure(const StreamConfiguration &inputCfg, const std::vector< std::reference_wrapper< StreamConfiguration >> &outputCfgs)=0
Configure a set of output stream conversion from an input stream.
Video stream for a camera.
Definition: stream.h:75
virtual int start()=0
Start the converter streaming operation.
Signal< FrameBuffer * > inputBufferReady
A signal emitted when the input frame buffer completes.
Definition: converter.h:88
Top-level libcamera namespace.
Definition: backtrace.h:17
virtual int setInputCrop(const Stream *stream, Rectangle *rect)=0
Set the crop rectangle rect for stream.
Base class for converter factories.
Definition: converter.h:102
std::unique_ptr< Converter > createInstance(MediaDevice *media) const override
Create an instance of the Converter corresponding to the factory.
Definition: converter.h:134
virtual int loadConfiguration(const std::string &filename)=0
Load converter configuration from file.
Frame buffer data and its associated dynamic metadata.
Definition: framebuffer.h:48
const std::vector< std::string > & compatibles() const
Definition: converter.h:108
Describe a two-dimensional size.
Definition: geometry.h:50
virtual std::pair< Rectangle, Rectangle > inputCropBounds()=0
Retrieve the crop bounds of the converter.
Converter(MediaDevice *media, Features features=Feature::None)
Construct a Converter instance.
Definition: converter.cpp:71
Signal & slot implementation.
virtual void stop()=0
Stop the converter streaming operation.
Describe a rectangle&#39;s position and dimensions.
Definition: geometry.h:240
ConverterFactory(const char *name, std::initializer_list< std::string > compatibles)
Construct a converter factory.
Definition: converter.h:129
virtual int validateOutput(StreamConfiguration *cfg, bool *adjusted, Alignment align=Alignment::Down)=0
Validate and possibily adjust cfg to a valid converter output.
virtual SizeRange sizes(const Size &input)=0
Retrieve the range of minimum and maximum output sizes for an input size.
#define LIBCAMERA_DISABLE_COPY_AND_MOVE(klass)
Disable copy and move construction and assignment of the klass.
virtual std::tuple< unsigned int, unsigned int > strideAndFrameSize(const PixelFormat &pixelFormat, const Size &size)=0
Retrieve the output stride and frame size for an input configutation.
Signal< FrameBuffer * > outputBufferReady
A signal emitted on each frame buffer completion of the output queue.
Definition: converter.h:89
const std::string & deviceNode() const
The converter device node attribute accessor.
Definition: converter.h:91
Registration of ConverterFactory classes and creation of instances.
Definition: converter.h:126
virtual int exportBuffers(const Stream *stream, unsigned int count, std::vector< std::unique_ptr< FrameBuffer >> *buffers)=0
Export buffers from the converter device.
Generic signal and slot communication mechanism.
Definition: object.h:21
Describe a range of sizes.
Definition: geometry.h:198
Features features_
Stores the features supported by the converter.
Definition: converter.h:96
virtual bool isValid() const =0
Check if the converter configuration is valid.
Alignment
The alignment mode specified when adjusting the converter input or output sizes.
Definition: converter.h:44
Enum-based bit fields.
Data structures related to geometric objects.
virtual int queueBuffers(FrameBuffer *input, const std::map< const Stream *, FrameBuffer *> &outputs)=0
Queue buffers to converter device.
Features features() const
Retrieve the features supported by the converter.
Definition: converter.h:93
virtual Size adjustOutputSize(const PixelFormat &pixFmt, const Size &size, Alignment align=Alignment::Down)=0
Adjust the converter output size to a valid value.
virtual Size adjustInputSize(const PixelFormat &pixFmt, const Size &size, Alignment align=Alignment::Down)=0
Adjust the converter input size to a valid value.
Configuration parameters for a stream.
Definition: stream.h:40
Feature
Specify the features supported by the converter.
Definition: converter.h:37
Abstract Base Class for converter.
Definition: converter.h:34
virtual bool isConfigured(const Stream *stream) const =0
Check if a given stream is configured.