libcamera  v0.5.0+49-e5442c31
Supporting cameras in Linux since 2019
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
converter_v4l2_m2m.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  * V4l2 M2M Format converter interface
7  */
8 
9 #pragma once
10 
11 #include <functional>
12 #include <map>
13 #include <memory>
14 #include <string>
15 #include <tuple>
16 #include <vector>
17 
18 #include <libcamera/base/log.h>
19 #include <libcamera/base/signal.h>
20 
21 #include <libcamera/pixel_format.h>
22 
24 
25 namespace libcamera {
26 
27 class FrameBuffer;
28 class MediaDevice;
29 class Size;
30 class SizeRange;
31 class Stream;
32 struct StreamConfiguration;
33 class Rectangle;
34 class V4L2M2MDevice;
35 
37 {
38 public:
40 
41  int loadConfiguration([[maybe_unused]] const std::string &filename) override { return 0; }
42  bool isValid() const override { return m2m_ != nullptr; }
43 
44  std::vector<PixelFormat> formats(PixelFormat input) override;
45  SizeRange sizes(const Size &input) override;
46 
47  std::tuple<unsigned int, unsigned int>
48  strideAndFrameSize(const PixelFormat &pixelFormat, const Size &size) override;
49 
50  Size adjustInputSize(const PixelFormat &pixFmt,
51  const Size &size, Alignment align = Alignment::Down) override;
52  Size adjustOutputSize(const PixelFormat &pixFmt,
53  const Size &size, Alignment align = Alignment::Down) override;
54 
55  int configure(const StreamConfiguration &inputCfg,
56  const std::vector<std::reference_wrapper<StreamConfiguration>>
57  &outputCfg) override;
58  bool isConfigured(const Stream *stream) const override;
59  int exportBuffers(const Stream *stream, unsigned int count,
60  std::vector<std::unique_ptr<FrameBuffer>> *buffers) override;
61 
62  int start() override;
63  void stop() override;
64 
65  int validateOutput(StreamConfiguration *cfg, bool *adjusted,
66  Alignment align = Alignment::Down) override;
67 
68  int queueBuffers(FrameBuffer *input,
69  const std::map<const Stream *, FrameBuffer *> &outputs) override;
70 
71  int setInputCrop(const Stream *stream, Rectangle *rect) override;
72  std::pair<Rectangle, Rectangle> inputCropBounds() override { return inputCropBounds_; }
73  std::pair<Rectangle, Rectangle> inputCropBounds(const Stream *stream) override;
74 
75 private:
76  class V4L2M2MStream : protected Loggable
77  {
78  public:
79  V4L2M2MStream(V4L2M2MConverter *converter, const Stream *stream);
80 
81  bool isValid() const { return m2m_ != nullptr; }
82 
83  int configure(const StreamConfiguration &inputCfg,
84  const StreamConfiguration &outputCfg);
85  int exportBuffers(unsigned int count,
86  std::vector<std::unique_ptr<FrameBuffer>> *buffers);
87 
88  int start();
89  void stop();
90 
91  int queueBuffers(FrameBuffer *input, FrameBuffer *output);
92 
93  int setInputSelection(unsigned int target, Rectangle *rect);
94  int getInputSelection(unsigned int target, Rectangle *rect);
95 
96  std::pair<Rectangle, Rectangle> inputCropBounds();
97 
98  protected:
99  std::string logPrefix() const override;
100 
101  private:
102  void captureBufferReady(FrameBuffer *buffer);
103  void outputBufferReady(FrameBuffer *buffer);
104 
105  V4L2M2MConverter *converter_;
106  const Stream *stream_;
107  std::unique_ptr<V4L2M2MDevice> m2m_;
108 
109  unsigned int inputBufferCount_;
110  unsigned int outputBufferCount_;
111 
112  std::pair<Rectangle, Rectangle> inputCropBounds_;
113  };
114 
115  Size adjustSizes(const Size &size, const std::vector<SizeRange> &ranges,
116  Alignment align);
117 
118  std::unique_ptr<V4L2M2MDevice> m2m_;
119 
120  std::map<const Stream *, std::unique_ptr<V4L2M2MStream>> streams_;
121  std::map<FrameBuffer *, unsigned int> queue_;
122  std::pair<Rectangle, Rectangle> inputCropBounds_;
123 };
124 
125 } /* namespace libcamera */
int setInputCrop(const Stream *stream, Rectangle *rect) override
Set the crop rectangle rect for stream.
Definition: converter_v4l2_m2m.cpp:586
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
bool isConfigured(const Stream *stream) const override
Check if a given stream is configured.
Definition: converter_v4l2_m2m.cpp:565
Video stream for a camera.
Definition: stream.h:75
Base class to support log message extensions.
Definition: log.h:91
Top-level libcamera namespace.
Definition: backtrace.h:17
SizeRange sizes(const Size &input) override
Retrieve the range of minimum and maximum output sizes for an input size.
Definition: converter_v4l2_m2m.cpp:341
int loadConfiguration([[maybe_unused]] const std::string &filename) override
Definition: converter_v4l2_m2m.h:41
Size adjustInputSize(const PixelFormat &pixFmt, const Size &size, Alignment align=Alignment::Down) override
Adjust the converter input size to a valid value.
Definition: converter_v4l2_m2m.cpp:407
Frame buffer data and its associated dynamic metadata.
Definition: framebuffer.h:48
Describe a two-dimensional size.
Definition: geometry.h:50
The V4L2 M2M converter implements the converter interface based on V4L2 M2M device.
Definition: converter_v4l2_m2m.h:36
Signal & slot implementation.
Describe a rectangle&#39;s position and dimensions.
Definition: geometry.h:240
Abstract converter.
int start() override
Start the converter streaming operation.
Definition: converter_v4l2_m2m.cpp:623
Signal< FrameBuffer * > outputBufferReady
A signal emitted on each frame buffer completion of the output queue.
Definition: converter.h:89
void stop() override
Stop the converter streaming operation.
Definition: converter_v4l2_m2m.cpp:641
std::vector< PixelFormat > formats(PixelFormat input) override
Definition: converter_v4l2_m2m.cpp:301
bool isValid() const override
Definition: converter_v4l2_m2m.h:42
int configure(const StreamConfiguration &inputCfg, const std::vector< std::reference_wrapper< StreamConfiguration >> &outputCfg) override
Configure a set of output stream conversion from an input stream.
Definition: converter_v4l2_m2m.cpp:528
std::tuple< unsigned int, unsigned int > strideAndFrameSize(const PixelFormat &pixelFormat, const Size &size) override
Retrieve the output stride and frame size for an input configutation.
Definition: converter_v4l2_m2m.cpp:390
int exportBuffers(const Stream *stream, unsigned int count, std::vector< std::unique_ptr< FrameBuffer >> *buffers) override
Export buffers from the converter device.
Definition: converter_v4l2_m2m.cpp:573
Describe a range of sizes.
Definition: geometry.h:198
Alignment
The alignment mode specified when adjusting the converter input or output sizes.
Definition: converter.h:44
Logging infrastructure.
int queueBuffers(FrameBuffer *input, const std::map< const Stream *, FrameBuffer *> &outputs) override
Queue buffers to converter device.
Definition: converter_v4l2_m2m.cpp:697
Size adjustOutputSize(const PixelFormat &pixFmt, const Size &size, Alignment align=Alignment::Down) override
Adjust the converter output size to a valid value.
Definition: converter_v4l2_m2m.cpp:426
Configuration parameters for a stream.
Definition: stream.h:40
V4L2M2MConverter(MediaDevice *media)
Construct a V4L2M2MConverter instance.
Definition: converter_v4l2_m2m.cpp:264
libcamera pixel format
int validateOutput(StreamConfiguration *cfg, bool *adjusted, Alignment align=Alignment::Down) override
Validate and possibily adjust cfg to a valid converter output.
Definition: converter_v4l2_m2m.cpp:650
std::pair< Rectangle, Rectangle > inputCropBounds() override
Retrieve the crop bounds of the converter.
Definition: converter_v4l2_m2m.h:72
Abstract Base Class for converter.
Definition: converter.h:34