libcamera v0.5.1 Supporting cameras in Linux since 2019 |

Loading...
Searching...
No Matches
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
42 using Features = Flags<Feature>;
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
88 Signal<FrameBuffer *> inputBufferReady;
89 Signal<FrameBuffer *> outputBufferReady;
90
91 const std::string &deviceNode() const { return deviceNode_; }
92
93 Features features() const { return features_; }
94
95 protected:
96 Features features_;
97
98 private:
99 std::string deviceNode_;
100};
101
102 class ConverterFactoryBase
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:
115 LIBCAMERA_DISABLE_COPY_AND_MOVE(ConverterFactoryBase)
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>
126 class ConverterFactory : public ConverterFactoryBase
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 */
class.h
Utilities to help constructing class interfaces.
LIBCAMERA_DISABLE_COPY_AND_MOVE
#define LIBCAMERA_DISABLE_COPY_AND_MOVE(klass)
Disable copy and move construction and assignment of the klass.
Definition class.h:29
libcamera::ConverterFactoryBase
Base class for converter factories.
Definition converter.h:103
libcamera::ConverterFactoryBase::compatibles
const std::vector< std::string > & compatibles() const
Definition converter.h:108
libcamera::ConverterFactoryBase::factories
static std::vector< ConverterFactoryBase * > & factories()
Retrieve the list of all converter factories.
Definition converter.cpp:400
libcamera::ConverterFactoryBase::ConverterFactoryBase
ConverterFactoryBase(const std::string name, std::initializer_list< std::string > compatibles)
Construct a converter factory base.
Definition converter.cpp:314
libcamera::ConverterFactoryBase::create
static std::unique_ptr< Converter > create(MediaDevice *media)
Create an instance of the converter corresponding to the media device.
Definition converter.cpp:335
libcamera::ConverterFactoryBase::names
static std::vector< std::string > names()
Retrieve the list of all converter factory names.
Definition converter.cpp:379
libcamera::ConverterFactory::createInstance
std::unique_ptr< Converter > createInstance(MediaDevice *media) const override
Create an instance of the Converter corresponding to the factory.
Definition converter.h:134
libcamera::ConverterFactory::ConverterFactory
ConverterFactory(const char *name, std::initializer_list< std::string > compatibles)
Construct a converter factory.
Definition converter.h:129
libcamera::Converter
Abstract Base Class for converter.
Definition converter.h:35
libcamera::Converter::queueBuffers
virtual int queueBuffers(FrameBuffer *input, const std::map< const Stream *, FrameBuffer * > &outputs)=0
Queue buffers to converter device.
libcamera::Converter::features_
Features features_
Stores the features supported by the converter.
Definition converter.h:96
libcamera::Converter::exportBuffers
virtual int exportBuffers(const Stream *stream, unsigned int count, std::vector< std::unique_ptr< FrameBuffer > > *buffers)=0
Export buffers from the converter device.
libcamera::Converter::formats
virtual std::vector< PixelFormat > formats(PixelFormat input)=0
Retrieve the list of supported pixel formats for an input pixel format.
libcamera::Converter::start
virtual int start()=0
Start the converter streaming operation.
libcamera::Converter::inputCropBounds
virtual std::pair< Rectangle, Rectangle > inputCropBounds()=0
Retrieve the crop bounds of the converter.
libcamera::Converter::sizes
virtual SizeRange sizes(const Size &input)=0
Retrieve the range of minimum and maximum output sizes for an input size.
libcamera::Converter::adjustInputSize
virtual Size adjustInputSize(const PixelFormat &pixFmt, const Size &size, Alignment align=Alignment::Down)=0
Adjust the converter input size to a valid value.
libcamera::Converter::inputCropBounds
virtual std::pair< Rectangle, Rectangle > inputCropBounds(const Stream *stream)=0
Retrieve the crop bounds for stream.
libcamera::Converter::Features
Flags< Feature > Features
A bitwise combination of features supported by the converter.
Definition converter.h:42
libcamera::Converter::setInputCrop
virtual int setInputCrop(const Stream *stream, Rectangle *rect)=0
Set the crop rectangle rect for stream.
libcamera::Converter::adjustOutputSize
virtual Size adjustOutputSize(const PixelFormat &pixFmt, const Size &size, Alignment align=Alignment::Down)=0
Adjust the converter output size to a valid value.
libcamera::Converter::Converter
Converter(MediaDevice *media, Features features=Feature::None)
Construct a Converter instance.
Definition converter.cpp:71
libcamera::Converter::outputBufferReady
Signal< FrameBuffer * > outputBufferReady
A signal emitted on each frame buffer completion of the output queue.
Definition converter.h:89
libcamera::Converter::isValid
virtual bool isValid() const =0
Check if the converter configuration is valid.
libcamera::Converter::features
Features features() const
Retrieve the features supported by the converter.
Definition converter.h:93
libcamera::Converter::Alignment
Alignment
The alignment mode specified when adjusting the converter input or output sizes.
Definition converter.h:44
libcamera::Converter::Alignment::Down
@ Down
Adjust the Converter sizes to a smaller valid size.
Definition converter.h:45
libcamera::Converter::Alignment::Up
@ Up
Adjust the Converter sizes to a larger valid size.
Definition converter.h:46
libcamera::Converter::configure
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.
libcamera::Converter::loadConfiguration
virtual int loadConfiguration(const std::string &filename)=0
Load converter configuration from file.
libcamera::Converter::isConfigured
virtual bool isConfigured(const Stream *stream) const =0
Check if a given stream is configured.
libcamera::Converter::Feature
Feature
Specify the features supported by the converter.
Definition converter.h:37
libcamera::Converter::Feature::None
@ None
No extra features supported by the converter.
Definition converter.h:38
libcamera::Converter::Feature::InputCrop
@ InputCrop
Cropping capability at input is supported by the converter.
Definition converter.h:39
libcamera::Converter::inputBufferReady
Signal< FrameBuffer * > inputBufferReady
A signal emitted when the input frame buffer completes.
Definition converter.h:88
libcamera::Converter::deviceNode
const std::string & deviceNode() const
The converter device node attribute accessor.
Definition converter.h:91
libcamera::Converter::strideAndFrameSize
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.
libcamera::Converter::stop
virtual void stop()=0
Stop the converter streaming operation.
libcamera::Converter::validateOutput
virtual int validateOutput(StreamConfiguration *cfg, bool *adjusted, Alignment align=Alignment::Down)=0
Validate and possibily adjust cfg to a valid converter output.
libcamera::Flags
Type-safe container for enum-based bitfields.
Definition flags.h:16
libcamera::FrameBuffer
Frame buffer data and its associated dynamic metadata.
Definition framebuffer.h:49
libcamera::MediaDevice
The MediaDevice represents a Media Controller device with its full graph of connected objects.
Definition media_device.h:25
libcamera::PixelFormat
libcamera image pixel format
Definition pixel_format.h:17
libcamera::Rectangle
Describe a rectangle's position and dimensions.
Definition geometry.h:241
libcamera::Signal
Generic signal and slot communication mechanism.
Definition signal.h:39
libcamera::SizeRange
Describe a range of sizes.
Definition geometry.h:199
libcamera::Size
Describe a two-dimensional size.
Definition geometry.h:51
libcamera::Stream
Video stream for a camera.
Definition stream.h:76
flags.h
Enum-based bit fields.
geometry.h
Data structures related to geometric objects.
libcamera
Top-level libcamera namespace.
Definition backtrace.h:17
signal.h
Signal & slot implementation.
libcamera::StreamConfiguration
Configuration parameters for a stream.
Definition stream.h:40