6#include <gst/video/video.h>
10#include <QtCore/QLoggingCategory>
11#include <QtMultimedia/QAbstractVideoBuffer>
20class PooledCpuVideoBuffer final :
public QAbstractVideoBuffer
23 PooledCpuVideoBuffer(QVideoFrameFormat format, CpuVideoFramePool::PlaneLayout layout, QByteArray backing)
24 : _format(std::move(
format))
26 , _backing(std::move(backing))
29 ~PooledCpuVideoBuffer()
override
31 if (!_backing.isEmpty()) {
37 MapData map(QVideoFrame::MapMode mode)
override
39 if (mode == QVideoFrame::NotMapped) {
43 data.planeCount = _layout.planeCount;
44 uchar*
base =
reinterpret_cast<uchar*
>(_backing.data());
45 for (
int i = 0; i < _layout.planeCount; ++i) {
46 data.data[i] =
base + _layout.planeOffset[i];
47 data.bytesPerLine[i] = _layout.bytesPerLine[i];
48 data.dataSize[i] =
static_cast<qsizetype
>(_layout.bytesPerLine[i]) * _layout.height[i];
53 QVideoFrameFormat
format()
const override {
return _format; }
56 QVideoFrameFormat _format;
57 CpuVideoFramePool::PlaneLayout _layout;
63class MapThroughGstVideoBuffer final :
public QAbstractVideoBuffer
67 MapThroughGstVideoBuffer(GstBuffer* buffer,
const GstVideoInfo& info, QVideoFrameFormat format,
69 : _buffer(gst_buffer_ref(buffer))
71 , _format(std::move(
format))
72 , _pinned(std::move(pinned))
75 ~MapThroughGstVideoBuffer()
override
78 gst_buffer_unref(_buffer);
79 _pinned->fetch_sub(1, std::memory_order_relaxed);
82 MapData map(QVideoFrame::MapMode mode)
override
84 return (mode == QVideoFrame::ReadOnly) ? _cpuView.map(_buffer, _info) : MapData{};
87 void unmap()
override { _cpuView.unmap(); }
89 QVideoFrameFormat
format()
const override {
return _format; }
94 QVideoFrameFormat _format;
96 CpuVideoFramePool::FrameMapping _cpuView;
100bool tryReservePinnedFrame(std::atomic<int>& pinned)
noexcept
102 int live = pinned.load(std::memory_order_relaxed);
107 }
while (!pinned.compare_exchange_weak(live, live + 1, std::memory_order_relaxed));
115 static CpuVideoFramePool s_pool;
122 const int n = GST_VIDEO_INFO_N_PLANES(&info);
123 if (n <= 0 || n > 4) {
127 for (
int i = 0; i < n; ++i) {
128 L.
bytesPerLine[i] = GST_VIDEO_INFO_PLANE_STRIDE(&info, i);
129 L.
planeOffset[i] =
static_cast<qsizetype
>(GST_VIDEO_INFO_PLANE_OFFSET(&info, i));
132 L.
byteSize =
static_cast<qsizetype
>(GST_VIDEO_INFO_SIZE(&info));
138 QAbstractVideoBuffer::MapData data;
139 data.
planeCount =
static_cast<int>(GST_VIDEO_FRAME_N_PLANES(&frame));
140 for (
int i = 0; i < data.planeCount; ++i) {
141 data.data[i] =
static_cast<uchar*
>(GST_VIDEO_FRAME_PLANE_DATA(&frame, i));
142 data.bytesPerLine[i] = GST_VIDEO_FRAME_PLANE_STRIDE(&frame, i);
144 static_cast<qsizetype
>(GST_VIDEO_FRAME_PLANE_STRIDE(&frame, i)) *
GstHw::planeHeight(frame.info, i);
152 _mapped = buffer && gst_video_frame_map(&_frame, &info, buffer, flags);
168 gst_video_frame_unmap(&_frame);
173QByteArray CpuVideoFramePool::acquireBacking(
const PlaneLayout& layout, QSize size,
174 QVideoFrameFormat::PixelFormat format)
176 QMutexLocker lock(&_mutex);
177 for (std::size_t i = 0; i < _slots.size(); ++i) {
178 auto& slot = _slots[i];
179 if (slot.size == size && slot.format == format) {
182 while (slot.availableCount > 0) {
183 QByteArray b = std::move(slot.available[--slot.availableCount]);
192 return QByteArray(layout.
byteSize, Qt::Uninitialized);
197 QMutexLocker lock(&_mutex);
198 for (std::size_t i = 0; i < _slots.size(); ++i) {
199 auto& slot = _slots[i];
200 if (slot.size == size && slot.format == format) {
201 if (slot.availableCount < kMaxPerSlot) {
202 slot.available[slot.availableCount++] = std::move(backing);
208 Slot* target =
nullptr;
209 for (std::size_t i = 0; i < _slots.size(); ++i) {
210 auto& slot = _slots[i];
211 if (slot.format == QVideoFrameFormat::Format_Invalid) {
217 target = &_slots[_evictCursor];
218 _evictCursor = (_evictCursor + 1) % kMaxSlots;
222 target->format = format;
223 target->available[0] = std::move(backing);
224 target->availableCount = 1;
233 QByteArray backing = acquireBacking(layout, format.frameSize(), format.pixelFormat());
234 auto buffer = std::make_unique<PooledCpuVideoBuffer>(format, layout, std::move(backing));
235 return QVideoFrame(std::move(buffer));
239 const QVideoFrameFormat& format,
242 if (!buffer || !format.isValid() || !pinned) {
247 if (!tryReservePinnedFrame(*pinned)) {
250 auto buf = std::make_unique<MapThroughGstVideoBuffer>(buffer, info, format, pinned);
253 const auto probe = buf->map(QVideoFrame::ReadOnly);
254 if (probe.planeCount <= 0) {
261 const QVideoFrameFormat& format)
263 if (!buffer || !format.isValid()) {
268 if (!source.
mapFrame(buffer, videoInfo)) {
269 static std::atomic<quint64> s_failCount{0};
270 const quint64 count = s_failCount.fetch_add(1, std::memory_order_relaxed) + 1;
271 if ((count & 0x3F) == 1) {
272 qCWarning(CpuVideoFramePoolLog) <<
"copyFromBuffer: gst_video_frame_map failed (count=" << count <<
")";
277 const GstVideoFrame& gstFrame = source.
frame();
280 GstVideoInfo frameInfo = videoInfo;
282 (GST_VIDEO_INFO_WIDTH(&frameInfo) > GST_VIDEO_FRAME_WIDTH(&gstFrame)) ||
283 (GST_VIDEO_INFO_HEIGHT(&frameInfo) > GST_VIDEO_FRAME_HEIGHT(&gstFrame))) {
284 static std::atomic<bool> s_warnedFrameSize{
false};
285 if (!s_warnedFrameSize.exchange(
true, std::memory_order_relaxed)) {
286 qCWarning(CpuVideoFramePoolLog)
287 <<
"copyFromBuffer: no" << format.frameSize() <<
"frame in a buffer of"
288 << GST_VIDEO_FRAME_WIDTH(&gstFrame) <<
"x" << GST_VIDEO_FRAME_HEIGHT(&gstFrame) <<
"— dropping frame";
294 if (!videoFrame.isValid() || !videoFrame.map(QVideoFrame::WriteOnly)) {
298 const int srcPlanes = GST_VIDEO_INFO_N_PLANES(&frameInfo);
299 if (srcPlanes != videoFrame.planeCount()) {
300 static std::atomic<bool> s_warnedPlaneMismatch{
false};
301 if (!s_warnedPlaneMismatch.exchange(
true, std::memory_order_relaxed)) {
302 qCWarning(CpuVideoFramePoolLog) <<
"copyFromBuffer: plane-count mismatch src" << srcPlanes <<
"dst"
303 << videoFrame.planeCount() <<
"— dropping frame";
309 for (
int p = 0; p < srcPlanes; ++p) {
310 const int dstStride = videoFrame.bytesPerLine(p);
311 const int srcStride = GST_VIDEO_FRAME_PLANE_STRIDE(&gstFrame, p);
317 if (activeRowBytes > (std::min) (srcStride, dstStride)) {
318 static std::atomic<bool> s_warnedStrideOverflow{
false};
319 if (!s_warnedStrideOverflow.exchange(
true, std::memory_order_relaxed)) {
320 qCWarning(CpuVideoFramePoolLog)
321 <<
"copyFromBuffer: plane" << p <<
"activeRowBytes" << activeRowBytes <<
"> srcStride" << srcStride
322 <<
"or dstStride" << dstStride <<
"— dropping frame";
327 const uchar* src =
static_cast<const uchar*
>(GST_VIDEO_FRAME_PLANE_DATA(&gstFrame, p));
328 uchar* dst = videoFrame.bits(p);
332 if (srcStride == dstStride && activeRowBytes == srcStride) {
333 std::memcpy(dst, src,
static_cast<size_t>(rows) * srcStride);
335 for (
int y = 0; y < rows; ++y) {
336 std::memcpy(dst + y * dstStride, src + y * srcStride,
static_cast<size_t>(activeRowBytes));
#define QGC_LOGGING_CATEGORY(name, categoryStr)
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.
QAbstractVideoBuffer::MapData map(GstBuffer *buffer, const GstVideoInfo &info)
mapFrame() for reading, as Qt map data; empty when the map fails.
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.
int planeHeight(const GstVideoInfo &info, int plane)
int planeRowBytes(const GstVideoInfo &info, int plane)
bool setFrameSize(GstVideoInfo &info, int width, int height)
GPSCommandSequence base(QByteArrayView port, const GPSBaseStationConfig &base)
Streams RTCM3 corrections and the PVTGeodetic survey status over port in the mode base selects.