libcamera: include/libcamera/internal/converter.h Source File

libcamera v0.7.2 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#include "libcamera/internal/v4l2_request.h"
26
27namespace libcamera {
28
29class FrameBuffer;
30class MediaDevice;
31class PixelFormat;
32class Stream;
33struct StreamConfiguration;
34
35 class Converter
36{
37public:
38 enum class Feature {
39 None = 0,
40 InputCrop = (1 << 0),
41 };
42
43 using Features = Flags<Feature>;
44
45 enum class Alignment {
46 Down = 0,
47 Up,
48 };
49
50 Converter(std::shared_ptr<MediaDevice> media, Features features = Feature::None);
51 virtual ~Converter();
52
53 virtual int loadConfiguration(const std::string &filename) = 0;
54
55 virtual bool isValid() const = 0;
56
57 virtual std::vector<PixelFormat> formats(PixelFormat input) = 0;
58 virtual SizeRange sizes(const Size &input) = 0;
59
60 virtual Size adjustInputSize(const PixelFormat &pixFmt,
61 const Size &size,
62 Alignment align = Alignment::Down) = 0;
63 virtual Size adjustOutputSize(const PixelFormat &pixFmt,
64 const Size &size,
65 Alignment align = Alignment::Down) = 0;
66
67 virtual std::tuple<unsigned int, unsigned int>
68 strideAndFrameSize(const PixelFormat &pixelFormat, const Size &size) = 0;
69
70 virtual int validateOutput(StreamConfiguration *cfg, bool *adjusted,
71 Alignment align = Alignment::Down) = 0;
72
73 virtual int configure(const StreamConfiguration &inputCfg,
74 const std::vector<std::reference_wrapper<const StreamConfiguration>> &outputCfgs) = 0;
75 virtual bool isConfigured(const Stream *stream) const = 0;
76 virtual int exportBuffers(const Stream *stream, unsigned int count,
77 std::vector<std::unique_ptr<FrameBuffer>> *buffers) = 0;
78
79 virtual int start() = 0;
80 virtual void stop() = 0;
81
82 virtual int queueBuffers(FrameBuffer *input,
83 const std::map<const Stream *, FrameBuffer *> &outputs,
84 const V4L2Request *request = nullptr) = 0;
85
86 virtual int setInputCrop(const Stream *stream, Rectangle *rect) = 0;
87 virtual std::pair<Rectangle, Rectangle> inputCropBounds() = 0;
88 virtual std::pair<Rectangle, Rectangle> inputCropBounds(const Stream *stream) = 0;
89
90 Signal<FrameBuffer *> inputBufferReady;
91 Signal<FrameBuffer *> outputBufferReady;
92
93 const std::string &deviceNode() const { return deviceNode_; }
94
95 Features features() const { return features_; }
96
97protected:
98 Features features_;
99
100private:
101 std::string deviceNode_;
102};
103
104 class ConverterFactoryBase
105{
106public:
107 ConverterFactoryBase(const std::string name, std::initializer_list<std::string> compatibles);
108 virtual ~ConverterFactoryBase() = default;
109
110 const std::vector<std::string> &compatibles() const { return compatibles_; }
111
112 static std::unique_ptr<Converter> create(std::shared_ptr<MediaDevice> media);
113 static std::vector<ConverterFactoryBase *> &factories();
114 static std::vector<std::string> names();
115
116private:
117 LIBCAMERA_DISABLE_COPY_AND_MOVE(ConverterFactoryBase)
118
119 static void registerType(ConverterFactoryBase *factory);
120
121 virtual std::unique_ptr<Converter>
122 createInstance(std::shared_ptr<MediaDevice> media) const = 0;
123
124 std::string name_;
125 std::vector<std::string> compatibles_;
126};
127
128template<typename _Converter>
129 class ConverterFactory : public ConverterFactoryBase
130{
131public:
132 ConverterFactory(const char *name, std::initializer_list<std::string> compatibles)
133 : ConverterFactoryBase(name, compatibles)
134 {
135 }
136
137 std::unique_ptr<Converter> createInstance(std::shared_ptr<MediaDevice> media) const override
138 {
139 return std::make_unique<_Converter>(media);
140 }
141};
142
143#define REGISTER_CONVERTER(name, converter, compatibles) \
144 static ConverterFactory<converter> global_##converter##Factory(name, compatibles);
145
146} /* 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:105
libcamera::ConverterFactoryBase::compatibles
const std::vector< std::string > & compatibles() const
Definition converter.h:110
libcamera::ConverterFactoryBase::factories
static std::vector< ConverterFactoryBase * > & factories()
Retrieve the list of all converter factories.
Definition converter.cpp:404
libcamera::ConverterFactoryBase::ConverterFactoryBase
ConverterFactoryBase(const std::string name, std::initializer_list< std::string > compatibles)
Construct a converter factory base.
Definition converter.cpp:318
libcamera::ConverterFactoryBase::names
static std::vector< std::string > names()
Retrieve the list of all converter factory names.
Definition converter.cpp:383
libcamera::ConverterFactoryBase::create
static std::unique_ptr< Converter > create(std::shared_ptr< MediaDevice > media)
Create an instance of the converter corresponding to the media device.
Definition converter.cpp:339
libcamera::ConverterFactory::ConverterFactory
ConverterFactory(const char *name, std::initializer_list< std::string > compatibles)
Construct a converter factory.
Definition converter.h:132
libcamera::ConverterFactory::createInstance
std::unique_ptr< Converter > createInstance(std::shared_ptr< MediaDevice > media) const override
Create an instance of the Converter corresponding to the factory.
Definition converter.h:137
libcamera::Converter
Abstract Base Class for converter.
Definition converter.h:36
libcamera::Converter::features_
Features features_
Stores the features supported by the converter.
Definition converter.h:98
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:43
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::outputBufferReady
Signal< FrameBuffer * > outputBufferReady
A signal emitted on each frame buffer completion of the output queue.
Definition converter.h:91
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:95
libcamera::Converter::Alignment
Alignment
The alignment mode specified when adjusting the converter input or output sizes.
Definition converter.h:45
libcamera::Converter::Alignment::Down
@ Down
Adjust the Converter sizes to a smaller valid size.
Definition converter.h:46
libcamera::Converter::queueBuffers
virtual int queueBuffers(FrameBuffer *input, const std::map< const Stream *, FrameBuffer * > &outputs, const V4L2Request *request=nullptr)=0
Queue buffers to converter device.
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::configure
virtual int configure(const StreamConfiguration &inputCfg, const std::vector< std::reference_wrapper< const StreamConfiguration > > &outputCfgs)=0
Configure a set of output stream conversion from an input stream.
libcamera::Converter::Feature
Feature
Specify the features supported by the converter.
Definition converter.h:38
libcamera::Converter::Feature::None
@ None
No extra features 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:90
libcamera::Converter::deviceNode
const std::string & deviceNode() const
The converter device node attribute accessor.
Definition converter.h:93
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::Converter
Converter(std::shared_ptr< MediaDevice > media, Features features=Feature::None)
Construct a Converter instance.
Definition converter.cpp:72
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:50
libcamera::MediaDevice
The MediaDevice represents a Media Controller device with its full graph of connected objects.
Definition media_device.h:28
libcamera::PixelFormat
libcamera image pixel format
Definition pixel_format.h:17
libcamera::Rectangle
Describe a rectangle's position and dimensions.
Definition geometry.h:247
libcamera::Signal
Generic signal and slot communication mechanism.
Definition signal.h:39
libcamera::SizeRange
Describe a range of sizes.
Definition geometry.h:205
libcamera::Size
Describe a two-dimensional size.
Definition geometry.h:51
libcamera::Stream
Video stream for a camera.
Definition stream.h:76
libcamera::V4L2Request
V4L2Request object and API.
Definition v4l2_request.h:22
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
v4l2_request.h
V4L2 Request.