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

libcamera v0.7.2 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>
19#include <libcamera/base/signal.h>
20
21#include <libcamera/pixel_format.h>
22
23#include "libcamera/internal/converter.h"
24
25namespace libcamera {
26
27class ControlList;
28class FrameBuffer;
29class MediaDevice;
30class Size;
31class SizeRange;
32class Stream;
33struct StreamConfiguration;
34class Rectangle;
35class V4L2M2MDevice;
36
37 class V4L2M2MConverter : public Converter
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<const 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 */
libcamera::ControlList
Associate a list of ControlId with their values for an object.
Definition controls.h:409
libcamera::Converter::outputBufferReady
Signal< FrameBuffer * > outputBufferReady
A signal emitted on each frame buffer completion of the output queue.
Definition converter.h:91
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::Converter
Converter(std::shared_ptr< MediaDevice > media, Features features=Feature::None)
Construct a Converter instance.
Definition converter.cpp:72
libcamera::FrameBuffer
Frame buffer data and its associated dynamic metadata.
Definition framebuffer.h:50
libcamera::Loggable
Base class to support log message extensions.
Definition log.h:92
libcamera::Loggable::logPrefix
virtual std::string logPrefix() const =0
Retrieve a string to be prefixed to the log message.
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::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::V4L2M2MConverter
The V4L2 M2M converter implements the converter interface based on V4L2 M2M device.
Definition converter_v4l2_m2m.h:38
libcamera::V4L2M2MConverter::adjustOutputSize
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
libcamera::V4L2M2MConverter::isConfigured
bool isConfigured(const Stream *stream) const override
Check if a given stream is configured.
Definition converter_v4l2_m2m.cpp:587
libcamera::V4L2M2MConverter::setInputCrop
int setInputCrop(const Stream *stream, Rectangle *rect) override
Set the crop rectangle rect for stream.
Definition converter_v4l2_m2m.cpp:608
libcamera::V4L2M2MConverter::start
int start() override
Start the converter streaming operation.
Definition converter_v4l2_m2m.cpp:645
libcamera::V4L2M2MConverter::isValid
bool isValid() const override
Definition converter_v4l2_m2m.h:43
libcamera::V4L2M2MConverter::inputCropBounds
std::pair< Rectangle, Rectangle > inputCropBounds() override
Retrieve the crop bounds of the converter.
Definition converter_v4l2_m2m.h:74
libcamera::V4L2M2MConverter::supportsRequests
bool supportsRequests()
Check if requests are supported.
Definition converter_v4l2_m2m.cpp:802
libcamera::V4L2M2MConverter::exportBuffers
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
libcamera::V4L2M2MConverter::strideAndFrameSize
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
libcamera::V4L2M2MConverter::V4L2M2MConverter
V4L2M2MConverter(std::shared_ptr< MediaDevice > media)
Construct a V4L2M2MConverter instance.
Definition converter_v4l2_m2m.cpp:286
libcamera::V4L2M2MConverter::allocateRequests
int allocateRequests(unsigned int count, std::vector< std::unique_ptr< V4L2Request > > *requests)
Allocate requests.
Definition converter_v4l2_m2m.cpp:786
libcamera::V4L2M2MConverter::formats
std::vector< PixelFormat > formats(PixelFormat input) override
Definition converter_v4l2_m2m.cpp:323
libcamera::V4L2M2MConverter::adjustInputSize
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
libcamera::V4L2M2MConverter::loadConfiguration
int loadConfiguration(const std::string &filename) override
Definition converter_v4l2_m2m.h:42
libcamera::V4L2M2MConverter::validateOutput
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
libcamera::V4L2M2MConverter::configure
int configure(const StreamConfiguration &inputCfg, const std::vector< std::reference_wrapper< const StreamConfiguration > > &outputCfg) override
Configure a set of output stream conversion from an input stream.
Definition converter_v4l2_m2m.cpp:550
libcamera::V4L2M2MConverter::queueBuffers
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
libcamera::V4L2M2MConverter::stop
void stop() override
Stop the converter streaming operation.
Definition converter_v4l2_m2m.cpp:663
libcamera::V4L2M2MConverter::applyControls
int applyControls(const Stream *stream, ControlList &ctrls, const V4L2Request *request=nullptr)
Apply controls.
Definition converter_v4l2_m2m.cpp:773
libcamera::V4L2M2MConverter::sizes
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
libcamera::V4L2M2MDevice
Memory-to-Memory video device.
Definition v4l2_videodevice.h:296
libcamera::V4L2Request
V4L2Request object and API.
Definition v4l2_request.h:22
converter.h
Abstract converter.
log.h
Logging infrastructure.
libcamera
Top-level libcamera namespace.
Definition backtrace.h:17
pixel_format.h
libcamera pixel format
signal.h
Signal & slot implementation.
libcamera::StreamConfiguration
Configuration parameters for a stream.
Definition stream.h:40