libcamera v0.7.0+1-4ceceb68
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 ControlList;
28class FrameBuffer;
29class MediaDevice;
30class Size;
31class SizeRange;
32class Stream;
33struct StreamConfiguration;
34class Rectangle;
35class V4L2M2MDevice;
36
38{
39public:
40 V4L2M2MConverter(std::shared_ptr<MediaDevice> media);
41
42 int loadConfiguration([[maybe_unused]] const std::string &filename) override { return 0; }
43 bool isValid() const override { return m2m_ != nullptr; }
44
45 std::vector<PixelFormat> formats(PixelFormat input) override;
46 SizeRange sizes(const Size &input) override;
47
48 std::tuple<unsigned int, unsigned int>
49 strideAndFrameSize(const PixelFormat &pixelFormat, const Size &size) override;
50
51 Size adjustInputSize(const PixelFormat &pixFmt,
52 const Size &size, Alignment align = Alignment::Down) override;
53 Size adjustOutputSize(const PixelFormat &pixFmt,
54 const Size &size, Alignment align = Alignment::Down) override;
55
56 int configure(const StreamConfiguration &inputCfg,
57 const std::vector<std::reference_wrapper<StreamConfiguration>>
58 &outputCfg) override;
59 bool isConfigured(const Stream *stream) const override;
60 int exportBuffers(const Stream *stream, unsigned int count,
61 std::vector<std::unique_ptr<FrameBuffer>> *buffers) override;
62
63 int start() override;
64 void stop() override;
65
66 int validateOutput(StreamConfiguration *cfg, bool *adjusted,
67 Alignment align = Alignment::Down) override;
68
69 int queueBuffers(FrameBuffer *input,
70 const std::map<const Stream *, FrameBuffer *> &outputs,
71 const V4L2Request *request = nullptr) override;
72
73 int setInputCrop(const Stream *stream, Rectangle *rect) override;
74 std::pair<Rectangle, Rectangle> inputCropBounds() override { return inputCropBounds_; }
75 std::pair<Rectangle, Rectangle> inputCropBounds(const Stream *stream) override;
76
77 int applyControls(const Stream *stream, ControlList &ctrls, const V4L2Request *request = nullptr);
78
79 int allocateRequests(unsigned int count,
80 std::vector<std::unique_ptr<V4L2Request>> *requests);
81
82 bool supportsRequests();
83
84private:
85 class V4L2M2MStream : protected Loggable
86 {
87 public:
88 V4L2M2MStream(V4L2M2MConverter *converter, const Stream *stream);
89
90 bool isValid() const { return m2m_ != nullptr; }
91
92 int configure(const StreamConfiguration &inputCfg,
93 const StreamConfiguration &outputCfg);
94 int exportBuffers(unsigned int count,
95 std::vector<std::unique_ptr<FrameBuffer>> *buffers);
96
97 int start();
98 void stop();
99
100 int applyControls(ControlList &ctrls, const V4L2Request *request = nullptr);
101
102 int queueBuffers(FrameBuffer *input, FrameBuffer *output,
103 const V4L2Request *request = nullptr);
104
105 int setInputSelection(unsigned int target, Rectangle *rect);
106 int getInputSelection(unsigned int target, Rectangle *rect);
107
108 std::pair<Rectangle, Rectangle> inputCropBounds();
109
110 protected:
111 std::string logPrefix() const override;
112
113 private:
114 void captureBufferReady(FrameBuffer *buffer);
115 void outputBufferReady(FrameBuffer *buffer);
116
117 V4L2M2MConverter *converter_;
118 const Stream *stream_;
119 std::unique_ptr<V4L2M2MDevice> m2m_;
120
121 unsigned int inputBufferCount_;
122 unsigned int outputBufferCount_;
123
124 std::pair<Rectangle, Rectangle> inputCropBounds_;
125 };
126
127 Size adjustSizes(const Size &size, const std::vector<SizeRange> &ranges,
128 Alignment align);
129
130 std::unique_ptr<V4L2M2MDevice> m2m_;
131
132 std::map<const Stream *, std::unique_ptr<V4L2M2MStream>> streams_;
133 std::map<FrameBuffer *, unsigned int> queue_;
134 std::pair<Rectangle, Rectangle> inputCropBounds_;
135
136 std::shared_ptr<MediaDevice> media_;
137};
138
139} /* namespace libcamera */
Associate a list of ControlId with their values for an object.
Definition controls.h:409
Abstract Base Class for converter.
Definition converter.h:36
Signal< FrameBuffer * > outputBufferReady
A signal emitted on each frame buffer completion of the output queue.
Definition converter.h:91
Alignment
The alignment mode specified when adjusting the converter input or output sizes.
Definition converter.h:45
@ Down
Adjust the Converter sizes to a smaller valid size.
Frame buffer data and its associated dynamic metadata.
Definition framebuffer.h:50
Base class to support log message extensions.
Definition log.h:92
libcamera image pixel format
Definition pixel_format.h:17
Describe a rectangle's position and dimensions.
Definition geometry.h:247
Describe a range of sizes.
Definition geometry.h:205
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:38
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:448
bool isConfigured(const Stream *stream) const override
Check if a given stream is configured.
Definition converter_v4l2_m2m.cpp:587
int setInputCrop(const Stream *stream, Rectangle *rect) override
Set the crop rectangle rect for stream.
Definition converter_v4l2_m2m.cpp:608
int start() override
Start the converter streaming operation.
Definition converter_v4l2_m2m.cpp:645
bool isValid() const override
Definition converter_v4l2_m2m.h:43
bool supportsRequests()
Check if requests are supported.
Definition converter_v4l2_m2m.cpp:802
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:595
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:412
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:550
int allocateRequests(unsigned int count, std::vector< std::unique_ptr< V4L2Request > > *requests)
Allocate requests.
Definition converter_v4l2_m2m.cpp:786
std::pair< Rectangle, Rectangle > inputCropBounds() override
Retrieve the crop bounds of the converter.
Definition converter_v4l2_m2m.h:74
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:429
int loadConfiguration(const std::string &filename) override
Definition converter_v4l2_m2m.h:42
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:672
int queueBuffers(FrameBuffer *input, const std::map< const Stream *, FrameBuffer * > &outputs, const V4L2Request *request=nullptr) override
Queue buffers to converter device.
Definition converter_v4l2_m2m.cpp:720
void stop() override
Stop the converter streaming operation.
Definition converter_v4l2_m2m.cpp:663
int applyControls(const Stream *stream, ControlList &ctrls, const V4L2Request *request=nullptr)
Apply controls.
Definition converter_v4l2_m2m.cpp:773
SizeRange sizes(const Size &input) override
Retrieve the range of minimum and maximum output sizes for an input size.
Definition converter_v4l2_m2m.cpp:363
V4L2Request object and API.
Definition v4l2_request.h:22
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