libcamera  v0.3.1+12-19bbca3c
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 <vector>
18 
19 #include <libcamera/base/class.h>
20 #include <libcamera/base/signal.h>
21 
22 #include <libcamera/geometry.h>
23 
24 namespace libcamera {
25 
26 class FrameBuffer;
27 class MediaDevice;
28 class PixelFormat;
29 class Stream;
30 struct StreamConfiguration;
31 
32 class Converter
33 {
34 public:
35  Converter(MediaDevice *media);
36  virtual ~Converter();
37 
38  virtual int loadConfiguration(const std::string &filename) = 0;
39 
40  virtual bool isValid() const = 0;
41 
42  virtual std::vector<PixelFormat> formats(PixelFormat input) = 0;
43  virtual SizeRange sizes(const Size &input) = 0;
44 
45  virtual std::tuple<unsigned int, unsigned int>
46  strideAndFrameSize(const PixelFormat &pixelFormat, const Size &size) = 0;
47 
48  virtual int configure(const StreamConfiguration &inputCfg,
49  const std::vector<std::reference_wrapper<StreamConfiguration>> &outputCfgs) = 0;
50  virtual int exportBuffers(const Stream *stream, unsigned int count,
51  std::vector<std::unique_ptr<FrameBuffer>> *buffers) = 0;
52 
53  virtual int start() = 0;
54  virtual void stop() = 0;
55 
56  virtual int queueBuffers(FrameBuffer *input,
57  const std::map<const Stream *, FrameBuffer *> &outputs) = 0;
58 
61 
62  const std::string &deviceNode() const { return deviceNode_; }
63 
64 private:
65  std::string deviceNode_;
66 };
67 
69 {
70 public:
71  ConverterFactoryBase(const std::string name, std::initializer_list<std::string> compatibles);
72  virtual ~ConverterFactoryBase() = default;
73 
74  const std::vector<std::string> &compatibles() const { return compatibles_; }
75 
76  static std::unique_ptr<Converter> create(MediaDevice *media);
77  static std::vector<ConverterFactoryBase *> &factories();
78  static std::vector<std::string> names();
79 
80 private:
82 
83  static void registerType(ConverterFactoryBase *factory);
84 
85  virtual std::unique_ptr<Converter> createInstance(MediaDevice *media) const = 0;
86 
87  std::string name_;
88  std::vector<std::string> compatibles_;
89 };
90 
91 template<typename _Converter>
93 {
94 public:
95  ConverterFactory(const char *name, std::initializer_list<std::string> compatibles)
96  : ConverterFactoryBase(name, compatibles)
97  {
98  }
99 
100  std::unique_ptr<Converter> createInstance(MediaDevice *media) const override
101  {
102  return std::make_unique<_Converter>(media);
103  }
104 };
105 
106 #define REGISTER_CONVERTER(name, converter, compatibles) \
107  static ConverterFactory<converter> global_##converter##Factory(name, compatibles);
108 
109 } /* 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:25
libcamera image pixel format
Definition: pixel_format.h:17
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:74
virtual int start()=0
Start the converter streaming operation.
Signal< FrameBuffer * > inputBufferReady
A signal emitted when the input frame buffer completes.
Definition: converter.h:59
Top-level libcamera namespace.
Definition: backtrace.h:17
Base class for converter factories.
Definition: converter.h:68
std::unique_ptr< Converter > createInstance(MediaDevice *media) const override
Create an instance of the Converter corresponding to the factory.
Definition: converter.h:100
virtual int loadConfiguration(const std::string &filename)=0
Load converter configuration from file.
Frame buffer data and its associated dynamic metadata.
Definition: framebuffer.h:49
const std::vector< std::string > & compatibles() const
Definition: converter.h:74
Describe a two-dimensional size.
Definition: geometry.h:52
Signal & slot implementation.
virtual void stop()=0
Stop the converter streaming operation.
ConverterFactory(const char *name, std::initializer_list< std::string > compatibles)
Construct a converter factory.
Definition: converter.h:95
Converter(MediaDevice *media)
Construct a Converter instance.
Definition: converter.cpp:44
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:60
const std::string & deviceNode() const
The converter device node attribute accessor.
Definition: converter.h:62
Registration of ConverterFactory classes and creation of instances.
Definition: converter.h:92
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:20
Describe a range of sizes.
Definition: geometry.h:200
virtual bool isValid() const =0
Check if the converter configuration is valid.
Data structures related to geometric objects.
virtual int queueBuffers(FrameBuffer *input, const std::map< const Stream *, FrameBuffer *> &outputs)=0
Queue buffers to converter device.
Configuration parameters for a stream.
Definition: stream.h:41
Abstract Base Class for converter.
Definition: converter.h:32