QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
CpuVideoFramePool.h
Go to the documentation of this file.
1#pragma once
2
3#include <array>
4#include <atomic>
5#include <gst/gst.h>
6#include <gst/video/video-frame.h>
7#include <gst/video/video-info.h>
8#include <memory>
9
10#include <QtCore/QByteArray>
11#include <QtCore/QMutex>
12#include <QtCore/QSize>
13#include <QtMultimedia/QAbstractVideoBuffer>
14#include <QtMultimedia/QVideoFrame>
15#include <QtMultimedia/QVideoFrameFormat>
16
18class CpuVideoFramePool
19{
20public:
21 static CpuVideoFramePool& instance();
22
24 QVideoFrame acquireFrame(const QVideoFrameFormat& format, const GstVideoInfo& info);
25
30 static constexpr int kMaxPinnedFrames = 4;
31
33 using PinnedFrameCounter = std::shared_ptr<std::atomic<int>>;
34
38 static std::unique_ptr<QAbstractVideoBuffer> wrapZeroCopy(GstBuffer* buffer, const GstVideoInfo& info,
39 const QVideoFrameFormat& format,
40 const PinnedFrameCounter& pinned);
41
43 {
44 int planeCount = 0;
45 int bytesPerLine[4] = {};
46 int height[4] = {};
47 qsizetype planeOffset[4] = {};
48 qsizetype byteSize = 0;
49 };
50
52 static PlaneLayout computeLayout(const GstVideoInfo& info);
53
55 static QAbstractVideoBuffer::MapData mapDataFor(const GstVideoFrame& frame);
56
60 {
61 public:
62 FrameMapping() = default;
63
65
66 FrameMapping(const FrameMapping&) = delete;
68
70 bool mapFrame(GstBuffer* buffer, const GstVideoInfo& info, GstMapFlags flags = GST_MAP_READ);
71
73 const GstVideoFrame& frame() const { return _frame; }
74
76 QAbstractVideoBuffer::MapData map(GstBuffer* buffer, const GstVideoInfo& info);
77 void unmap() noexcept;
78
79 private:
80 GstVideoFrame _frame = {};
81 bool _mapped = false;
82 };
83
87 static QVideoFrame copyFromBuffer(GstBuffer* buffer, const GstVideoInfo& videoInfo,
88 const QVideoFrameFormat& format);
89
91 void releaseBacking(QSize size, QVideoFrameFormat::PixelFormat format, QByteArray&& backing);
92
93private:
94 CpuVideoFramePool() = default;
95
96 QByteArray acquireBacking(const PlaneLayout& layout, QSize size, QVideoFrameFormat::PixelFormat format);
97
98 static constexpr int kMaxPerSlot = 4;
99 // Fixed-size slot array; round-robin eviction caps memory when resolution keeps changing.
100 static constexpr int kMaxSlots = 8;
101
102 struct Slot
103 {
104 QSize size;
105 QVideoFrameFormat::PixelFormat format = QVideoFrameFormat::Format_Invalid;
106 std::array<QByteArray, kMaxPerSlot> available;
107 int availableCount = 0;
108 };
109
110 QMutex _mutex;
111 std::array<Slot, kMaxSlots> _slots;
112 int _evictCursor = 0;
113};
bool mapFrame(GstBuffer *buffer, const GstVideoInfo &info, GstMapFlags flags=GST_MAP_READ)
Maps buffer with flags on the first call; false when that fails.
const GstVideoFrame & frame() const
The mapped frame; valid while mapFrame() succeeded and unmap() hasn't run.
FrameMapping(const FrameMapping &)=delete
QAbstractVideoBuffer::MapData map(GstBuffer *buffer, const GstVideoInfo &info)
mapFrame() for reading, as Qt map data; empty when the map fails.
FrameMapping & operator=(const FrameMapping &)=delete
static std::unique_ptr< QAbstractVideoBuffer > wrapZeroCopy(GstBuffer *buffer, const GstVideoInfo &info, const QVideoFrameFormat &format, const PinnedFrameCounter &pinned)
std::shared_ptr< std::atomic< int > > PinnedFrameCounter
Live zero-copy frames of one sink; shared so frames Qt still holds after the sink is gone can release...
static CpuVideoFramePool & instance()
static QVideoFrame copyFromBuffer(GstBuffer *buffer, const GstVideoInfo &videoInfo, const QVideoFrameFormat &format)
static constexpr int kMaxPinnedFrames
void releaseBacking(QSize size, QVideoFrameFormat::PixelFormat format, QByteArray &&backing)
Return a backing array to the pool; called by PooledCpuVideoBuffer's destructor only.
static PlaneLayout computeLayout(const GstVideoInfo &info)
Destination plane layout mirroring info (strides/offsets from the decoder). planeCount==0 means unsup...
QVideoFrame acquireFrame(const QVideoFrameFormat &format, const GstVideoInfo &info)
Returns a pool-backed frame sized to info, or freshly allocates on a pool miss; invalid if unsupporte...
static QAbstractVideoBuffer::MapData mapDataFor(const GstVideoFrame &frame)
Qt map data for a frame mapped with gst_video_frame_map, each plane sized by its own row count.