QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
GstQgcCaps.cc
Go to the documentation of this file.
1#include "GstQgcCaps.h"
2
3#include <gst/gst.h>
4#include <vector>
5#ifdef QGC_UNITTEST_BUILD
6#include <utility>
7
8#include <QtCore/QMutex>
9#endif
10
11#include "GstQgcVideoFormats.h"
12#include "GstScoped.h"
14
15namespace GstQgc {
16
17static_assert(GpuBinMode{} == GpuBinMode::Passthrough,
18 "SinkCapsPolicy::gpuBin's value-initialized default must be Passthrough");
19
20namespace {
21
22#if defined(QGC_HAS_GST_DMABUF_GPU_PATH)
24std::string sinkDmaDrmCaps([[maybe_unused]] void* display)
25{
26 std::string caps;
27#if GST_CHECK_VERSION(1, 24, 0)
28 const std::vector<GstVideoFormat> formats = advertisedFormats(kOfferDmaDrm);
29 // Best-effort: advertise the GPU's actually-supported (format, modifier) pairs (EGL query) so modifier-aware
30 // drivers negotiate a tiling QGC's importer can consume. Additive; empty on any query failure.
31 caps += GstHw::buildSupportedDmaDrmCaps(formats, display);
32#endif
33 return caps;
34}
35#endif
36
37// texture-target=2D is load-bearing: the GL path samples GL_TEXTURE_2D only, and it makes glcolorconvert convert
38// amcvideodec's external-OES Surface textures; with the default ANY target external-OES passes through and is sampled
39// black.
40constexpr const char* kGlMemory2D = "video/x-raw(memory:GLMemory), texture-target=2D";
41
42constexpr const char* kD3D12Memory = "video/x-raw(memory:D3D12Memory)";
43// What the D3D12Convert bin's d3d12convert outputs: single-plane formats, the only ones Qt's D3D12 QRhi can sample.
44constexpr const char* kD3D12ConvertFormats = "(string){ RGBA, BGRA }";
45#if defined(QGC_HAS_GST_D3D11_GPU_PATH) && defined(QGC_HAS_GST_D3D12_GPU_PATH)
46// What decoders output as D3D12Memory, each one resource a Direct3D 11 device opens as a texture with both planes.
47constexpr const char* kD3D12DecoderFormats = "(string){ NV12, P010_10LE }";
48#endif
49
51std::string gpuMemoryCaps(const SinkCapsPolicy& policy, const std::string& formats)
52{
53 std::string caps;
54 [[maybe_unused]] const auto offer = [&caps, &formats](const char* mediaType) {
55 caps += mediaType;
56 caps += ", format=";
57 caps += formats;
58 caps += "; ";
59 };
60 switch (policy.target.backend) {
62 // DMABuf and AHardwareBuffer reach Qt as EGLImages on its EGLDisplay; GLMemory through the GL share group,
63 // which gst-gl joins on EGL and GLX.
64#if defined(QGC_HAS_GST_DMABUF_GPU_PATH)
65 if (policy.target.egl && policy.dmaBufDisplay) {
66 caps += sinkDmaDrmCaps(policy.dmaBufDisplay);
67 // Legacy memory:DMABuf covers v4l2h264dec/Mali/V3D LINEAR. Unrestricted DMA_DRM is never offered (only
68 // the importable modifiers above): gst-va iHD would negotiate tiled+CCS, which no per-plane import can
69 // read.
70 offer("video/x-raw(memory:DMABuf)");
71 }
72#endif
73#if defined(QGC_HAS_GST_GLMEMORY_GPU_PATH)
74 if (policy.target.sharesGlContext() && (policy.gpuBin != GpuBinMode::DirectDmaBuf)) {
75 offer(kGlMemory2D);
76 }
77#endif
78#if defined(QGC_HAS_GST_AHARDWAREBUFFER_GPU_PATH)
79 if (policy.target.egl) {
80 offer("video/x-raw(memory:AHardwareBuffer)");
81 }
82#endif
83 break;
85#if defined(QGC_HAS_GST_D3D11_GPU_PATH)
86#if defined(QGC_HAS_GST_D3D12_GPU_PATH)
87 // First: its decoder works on a device of its own, a D3D11Memory one on the device Qt renders with.
88 if (policy.d3d11OpensD3D12) {
89 caps += std::string(kD3D12Memory) + ", format=" + kD3D12DecoderFormats + "; ";
90 }
91#endif
92 offer("video/x-raw(memory:D3D11Memory)");
93#endif
94 break;
96 // DMA_DRM only, in the layouts Qt's Vulkan device imports: Vulkan needs the modifier, which legacy
97 // memory:DMABuf caps don't carry.
98#if defined(QGC_HAS_GST_DMABUF_VULKAN_IMPORT) && GST_CHECK_VERSION(1, 24, 0)
99 if (policy.dmaBufDisplay) {
101 }
102#endif
103 break;
108 // Direct3D12: D3D12Memory only through the D3D12Convert bin (buildSinkCaps). Metal: vtdec's
109 // IOSurface-backed CoreVideo buffers arrive as system memory.
110 break;
111 }
112 return caps;
113}
114
116std::string sinkMemoryCaps(const SinkCapsPolicy& policy)
117{
118 if (policy.gpuZeroCopy && (policy.gpuBin == GpuBinMode::GlUpload)) {
119 return std::string(kGlMemory2D) + ", format=" + advertisedFormatList(kOfferGlUpload);
120 }
121 if (policy.gpuZeroCopy && (policy.gpuBin == GpuBinMode::D3D12Convert)) {
122 return std::string(kD3D12Memory) + ", format=" + kD3D12ConvertFormats;
123 }
124 const std::string formats = advertisedFormatList();
125 return (policy.gpuZeroCopy ? gpuMemoryCaps(policy, formats) : std::string()) + "video/x-raw, format=" + formats;
126}
127
129std::string sinkCapsWithSquarePixels(const std::string& caps)
130{
131 const GStreamer::GstCapsPtr parsed = GStreamer::adoptCaps(gst_caps_from_string(caps.c_str()));
132 if (!parsed) {
133 return caps;
134 }
135 gst_caps_set_simple(parsed.get(), "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1, nullptr);
136 const GStreamer::GCharPtr text = GStreamer::adoptGChar(gst_caps_to_string(parsed.get()));
137 return text ? std::string(text.get()) : caps;
138}
139
140} // namespace
141
142std::string buildSinkCaps(const SinkCapsPolicy& policy)
143{
144 // Square pixels in GPU memory as well as in system memory: the frame's size is all the VideoOutput has to go by.
145 // Only system memory is scaled to them, by the bin's converter (and D3D12Memory by d3d12convert), so a decoder
146 // with other pixels negotiates system memory.
147 const std::string caps = sinkMemoryCaps(policy);
148 return policy.squarePixels ? sinkCapsWithSquarePixels(caps) : caps;
149}
150
151#ifdef QGC_UNITTEST_BUILD
152namespace {
153
154Q_CONSTINIT QBasicMutex s_testEnvironmentMutex;
155std::optional<SinkEnvironment> s_testEnvironment;
156
157} // namespace
158
159ScopedSinkEnvironmentForTest::ScopedSinkEnvironmentForTest(const SinkEnvironment& environment)
160{
161 const QMutexLocker lock(&s_testEnvironmentMutex);
162 _previous = std::exchange(s_testEnvironment, environment);
163}
164
165ScopedSinkEnvironmentForTest::~ScopedSinkEnvironmentForTest()
166{
167 const QMutexLocker lock(&s_testEnvironmentMutex);
168 s_testEnvironment = _previous;
169}
170
171std::optional<SinkEnvironment> sinkEnvironmentForTest()
172{
173 const QMutexLocker lock(&s_testEnvironmentMutex);
174 return s_testEnvironment;
175}
176#endif
177
178} // namespace GstQgc
std::unique_ptr< GstCaps, GstCapsDeleter > GstCapsPtr
Definition GstScoped.h:66
GCharPtr adoptGChar(gchar *str) noexcept
Definition GstScoped.h:134
GstCapsPtr adoptCaps(GstCaps *caps) noexcept
Definition GstScoped.h:109
std::unique_ptr< gchar, GFreeDeleter > GCharPtr
Definition GstScoped.h:71
std::string buildSupportedDmaDrmCaps(std::span< const GstVideoFormat > formats, void *display)
std::string buildVulkanDmaDrmCaps(std::span< const GstVideoFormat > formats)
GstVideoSink that pushes decoded frames into a Qt QVideoSink; sole sink in qgcvideosinkbin....
constexpr unsigned kOfferDmaDrm
The EGL-derived DMA_DRM caps: formats the DMABuf importer takes with an explicit modifier.
constexpr unsigned kOfferGlUpload
@ GlUpload
glupload → glcolorconvert → GLMemory 2D; gst-gl imports LINEAR and vendor-tiled DMABuf.
@ Passthrough
Every compiled native memory straight into qgcqvideosink (Windows, Apple, ...).
std::vector< GstVideoFormat > advertisedFormats(unsigned requiredOffers=0)
The advertised formats carrying every flag in requiredOffers, in table order.
std::string advertisedFormatList(unsigned requiredOffers=0)
@ Unknown
No scene graph yet, and the GUI thread hasn't recorded the requested API either.
@ Null
QRhi's Null backend or the software renderer: no GPU import.
std::string buildSinkCaps(const SinkCapsPolicy &policy)
Everything qgcqvideosink's caps depend on.
Definition GstQgcCaps.h:15
bool squarePixels
Ask for square pixels, in every kind of memory. The bin's converter scales system memory to them.
Definition GstQgcCaps.h:26