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>
19#include <libcamera/base/signal.h>
20
21#include <libcamera/pixel_format.h>
22
23#include "libcamera/internal/converter.h"
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
36 class V4L2M2MConverter : public Converter
37{
38 public:
39 V4L2M2MConverter(MediaDevice *media);
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 */
libcamera::Converter::Converter
Converter(MediaDevice *media, Features features=Feature::None)
Construct a Converter instance.
Definition converter.cpp:71
libcamera::Converter::outputBufferReady
Signal< FrameBuffer * > outputBufferReady
A signal emitted on each frame buffer completion of the output queue.
Definition converter.h:89
libcamera::Converter::Alignment
Alignment
The alignment mode specified when adjusting the converter input or output sizes.
Definition converter.h:44
libcamera::Converter::Alignment::Down
@ Down
Adjust the Converter sizes to a smaller valid size.
Definition converter.h:45
libcamera::FrameBuffer
Frame buffer data and its associated dynamic metadata.
Definition framebuffer.h:49
libcamera::Loggable
Base class to support log message extensions.
Definition log.h:92
libcamera::MediaDevice
The MediaDevice represents a Media Controller device with its full graph of connected objects.
Definition media_device.h:25
libcamera::PixelFormat
libcamera image pixel format
Definition pixel_format.h:17
libcamera::Rectangle
Describe a rectangle's position and dimensions.
Definition geometry.h:241
libcamera::SizeRange
Describe a range of sizes.
Definition geometry.h:199
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:37
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:426
libcamera::V4L2M2MConverter::isConfigured
bool isConfigured(const Stream *stream) const override
Check if a given stream is configured.
Definition converter_v4l2_m2m.cpp:565
libcamera::V4L2M2MConverter::V4L2M2MConverter
V4L2M2MConverter(MediaDevice *media)
Construct a V4L2M2MConverter instance.
Definition converter_v4l2_m2m.cpp:264
libcamera::V4L2M2MConverter::setInputCrop
int setInputCrop(const Stream *stream, Rectangle *rect) override
Set the crop rectangle rect for stream.
Definition converter_v4l2_m2m.cpp:586
libcamera::V4L2M2MConverter::start
int start() override
Start the converter streaming operation.
Definition converter_v4l2_m2m.cpp:623
libcamera::V4L2M2MConverter::isValid
bool isValid() const override
Definition converter_v4l2_m2m.h:42
libcamera::V4L2M2MConverter::inputCropBounds
std::pair< Rectangle, Rectangle > inputCropBounds() override
Retrieve the crop bounds of the converter.
Definition converter_v4l2_m2m.h:72
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:573
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:390
libcamera::V4L2M2MConverter::configure
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
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:407
libcamera::V4L2M2MConverter::loadConfiguration
int loadConfiguration(const std::string &filename) override
Definition converter_v4l2_m2m.h:41
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:650
libcamera::V4L2M2MConverter::stop
void stop() override
Stop the converter streaming operation.
Definition converter_v4l2_m2m.cpp:641
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:341
libcamera::V4L2M2MConverter::queueBuffers
int queueBuffers(FrameBuffer *input, const std::map< const Stream *, FrameBuffer * > &outputs) override
Queue buffers to converter device.
Definition converter_v4l2_m2m.cpp:697
libcamera::V4L2M2MDevice
Memory-to-Memory video device.
Definition v4l2_videodevice.h:295
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