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 <QtCore/QByteArray>
4#include <QtCore/QMutex>
5#include <QtCore/QSize>
6#include <QtMultimedia/QVideoFrame>
7#include <QtMultimedia/QVideoFrameFormat>
8#include <array>
9#include <atomic>
10#include <gst/gst.h>
11#include <gst/video/video-info.h>
12#include <memory>
13
14class QAbstractVideoBuffer;
15
18{
19public:
21
23 QVideoFrame acquireFrame(const QVideoFrameFormat& format, const GstVideoInfo& info);
24
27 static std::unique_ptr<QAbstractVideoBuffer> wrapZeroCopy(GstBuffer* buffer, const GstVideoInfo& info,
28 const QVideoFrameFormat& format);
29
31 quint64 hitCount() const noexcept { return _hits.load(std::memory_order_relaxed); }
32
33 quint64 missCount() const noexcept { return _misses.load(std::memory_order_relaxed); }
34
36 {
37 int planeCount = 0;
38 int bytesPerLine[4] = {};
39 int height[4] = {};
40 qsizetype planeOffset[4] = {};
41 qsizetype byteSize = 0;
42 };
43
45 static PlaneLayout computeLayout(const GstVideoInfo& info);
46
48 static QVideoFrame copyFromBuffer(GstBuffer* buffer, const GstVideoInfo& videoInfo,
49 const QVideoFrameFormat& format);
50
52 void releaseBacking(QSize size, QVideoFrameFormat::PixelFormat format, QByteArray&& backing);
53
54private:
55 CpuVideoFramePool() = default;
56
57 QByteArray acquireBacking(const PlaneLayout& layout, QSize size, QVideoFrameFormat::PixelFormat format);
58
59 static constexpr int kMaxPerSlot = 4;
60 // Fixed-size slot array; round-robin eviction caps memory when resolution keeps changing.
61 static constexpr int kMaxSlots = 8;
62
63 struct Slot
64 {
65 QSize size;
66 QVideoFrameFormat::PixelFormat format = QVideoFrameFormat::Format_Invalid;
67 std::array<QByteArray, kMaxPerSlot> available;
68 int availableCount = 0;
69 };
70
71 QMutex _mutex;
72 std::array<Slot, kMaxSlots> _slots;
73 int _evictCursor = 0;
74 std::atomic<quint64> _hits{0};
75 std::atomic<quint64> _misses{0};
76};
Recycles CPU-backed QVideoFrame storage keyed by (size, pixelFormat) to avoid per-frame malloc churn.
static CpuVideoFramePool & instance()
static QVideoFrame copyFromBuffer(GstBuffer *buffer, const GstVideoInfo &videoInfo, const QVideoFrameFormat &format)
Copy buffer's planes into a pool-allocated QVideoFrame; invalid on stride overflow or unsupported for...
quint64 missCount() const noexcept
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 std::unique_ptr< QAbstractVideoBuffer > wrapZeroCopy(GstBuffer *buffer, const GstVideoInfo &info, const QVideoFrameFormat &format)
quint64 hitCount() const noexcept
Telemetry: total acquireFrame() calls satisfied from the pool vs. fresh alloc.