libcamera v0.5.1
Supporting cameras in Linux since 2019
Loading...
Searching...
No Matches
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>
20
22
24
25namespace libcamera {
26
27class FrameBuffer;
28class MediaDevice;
29class Size;
30class SizeRange;
31class Stream;
33class Rectangle;
34class V4L2M2MDevice;
35
37{
38public:
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
75private:
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 */
Converter(MediaDevice *media, Features features=Feature::None)
Construct a Converter instance.
Definition converter.cpp:71
Signal< FrameBuffer * > outputBufferReady
A signal emitted on each frame buffer completion of the output queue.
Definition converter.h:89
Alignment
The alignment mode specified when adjusting the converter input or output sizes.
Definition converter.h:44
@ Down
Adjust the Converter sizes to a smaller valid size.
Definition converter.h:45
Frame buffer data and its associated dynamic metadata.
Definition framebuffer.h:49
Base class to support log message extensions.
Definition log.h:92
virtual std::string logPrefix() const =0
Retrieve a string to be prefixed to the log message.
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
Describe a rectangle's position and dimensions.
Definition geometry.h:241
Describe a range of sizes.
Definition geometry.h:199
Describe a two-dimensional size.
Definition geometry.h:51
Video stream for a camera.
Definition stream.h:76
The V4L2 M2M converter implements the converter interface based on V4L2 M2M device.
Definition converter_v4l2_m2m.h:37
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
bool isConfigured(const Stream *stream) const override
Check if a given stream is configured.
Definition converter_v4l2_m2m.cpp:565
V4L2M2MConverter(MediaDevice *media)
Construct a V4L2M2MConverter instance.
Definition converter_v4l2_m2m.cpp:264
int setInputCrop(const Stream *stream, Rectangle *rect) override
Set the crop rectangle rect for stream.
Definition converter_v4l2_m2m.cpp:586
int start() override
Start the converter streaming operation.
Definition converter_v4l2_m2m.cpp:623
bool isValid() const override
Definition converter_v4l2_m2m.h:42
std::pair< Rectangle, Rectangle > inputCropBounds() override
Retrieve the crop bounds of the converter.
Definition converter_v4l2_m2m.h:72
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
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 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::vector< PixelFormat > formats(PixelFormat input) override
Definition converter_v4l2_m2m.cpp:301
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
int loadConfiguration(const std::string &filename) override
Definition converter_v4l2_m2m.h:41
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
void stop() override
Stop the converter streaming operation.
Definition converter_v4l2_m2m.cpp:641
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 queueBuffers(FrameBuffer *input, const std::map< const Stream *, FrameBuffer * > &outputs) override
Queue buffers to converter device.
Definition converter_v4l2_m2m.cpp:697
Memory-to-Memory video device.
Definition v4l2_videodevice.h:295
Abstract converter.
Logging infrastructure.
Top-level libcamera namespace.
Definition backtrace.h:17
libcamera pixel format
Signal & slot implementation.
Configuration parameters for a stream.
Definition stream.h:40