QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
GstQgcAllocation.cc
Go to the documentation of this file.
1#include "GstQgcAllocation.h"
2
3#include <gst/video/video.h>
4#include <memory>
5
6#include <QtCore/QtEnvironmentVariables>
7
8#include "GstScoped.h"
12#if defined(QGC_HAS_GST_GLMEMORY_GPU_PATH)
13#include <gst/gl/gstglsyncmeta.h>
14#endif
15
16namespace GstQgc {
17
18namespace {
19
20// Most decoder buffers downstream holds at once: kMaxPinnedFrames, plus the texture bundle Qt moves out of a reused
21// in-flight slot and keeps (with its sample) until that render frame ends.
22constexpr guint kProposedMinBuffers = CpuVideoFramePool::kMaxPinnedFrames + 1;
23
24// QGC_GST_PROPOSE_ALLOCATION_MIN_BUFFERS overrides kProposedMinBuffers, like Qt's
25// QT_GSTREAMER_PROPOSE_ALLOCATION_MIN_BUFFERS, e.g. to save CMA memory with 4K decoders on i.MX or Raspberry Pi.
26guint proposedMinBuffers()
27{
28 bool ok = false;
29 const int minBuffers = qEnvironmentVariableIntValue("QGC_GST_PROPOSE_ALLOCATION_MIN_BUFFERS", &ok);
30 return (ok && (minBuffers > 0)) ? static_cast<guint>(minBuffers) : kProposedMinBuffers;
31}
32
33// Advertise every meta API qgcqvideosink/GstQgcFrameMap consume so upstream keeps them;
34// else gst-vaapi/v4l2 crop into a copy of each frame instead of attaching the crop meta.
35void addConsumedAllocationMetas(GstQuery* query, bool cropMeta = true)
36{
37 gst_query_add_allocation_meta(query, GST_VIDEO_META_API_TYPE, NULL);
38 if (cropMeta) {
39 gst_query_add_allocation_meta(query, GST_VIDEO_CROP_META_API_TYPE, NULL);
40 }
41#if defined(QGC_HAS_GST_GLMEMORY_GPU_PATH)
42 // As upstream's Qt sink asks: glcolorconvert then ends each frame it renders with a sync point, which flushes its
43 // context. GstGlVideoBuffer doesn't wait on that fence: it sets one of its own once the frame is mapped.
44 gst_query_add_allocation_meta(query, GST_GL_SYNC_META_API_TYPE, NULL);
45#endif
46}
47
48// A GstVideoBufferPool, unlike a plain GstBufferPool, supports VIDEO_META and attaches GstVideoMeta per buffer, so
49// decoders that check the option (avdec direct rendering) decode straight into it instead of copying.
50bool tryProposeMetaPool(GstQuery* query, GstCaps* caps, gsize size, guint minBuffers)
51{
52 const std::unique_ptr<GstBufferPool, GStreamer::GstObjectDeleter> pool{gst_video_buffer_pool_new()};
53 if (!pool) {
54 return false;
55 }
56
57 GstStructure* config = gst_buffer_pool_get_config(pool.get());
58 gst_buffer_pool_config_set_params(config, caps, size, minBuffers, 0);
59 gst_buffer_pool_config_add_option(config, GST_BUFFER_POOL_OPTION_VIDEO_META);
60 if (!gst_buffer_pool_set_config(pool.get(), config)) {
61 return false;
62 }
63 gst_query_add_allocation_pool(query, pool.get(), size, minBuffers, 0);
64 return true;
65}
66
67} // namespace
68
69bool populateAllocationQuery(GstQuery* query)
70{
71 GstCaps* caps = nullptr;
72 gboolean need_pool = FALSE;
73 gst_query_parse_allocation(query, &caps, &need_pool);
74 if (!caps) {
75 return false;
76 }
77
78 GstVideoInfo vinfo;
79 if (!GstHw::dmaDrmAwareVideoInfo(caps, &vinfo)) {
80 return false;
81 }
82 const gsize size = GST_VIDEO_INFO_SIZE(&vinfo);
83 const guint minBuffers = proposedMinBuffers();
84
85 GstCapsFeatures* features = gst_caps_get_features(caps, 0);
86 const bool is_system_memory =
87 !features || gst_caps_features_is_equal(features, GST_CAPS_FEATURES_MEMORY_SYSTEM_MEMORY);
88
89 if (!is_system_memory) {
90 if (!HwBuffers::proposeDeviceBoundPool(query, caps, &vinfo, size, minBuffers)) {
91 gst_query_add_allocation_pool(query, NULL, size, minBuffers, 0);
92 }
93 // A d3d11 or d3d12 decoder offered crop meta hands over its whole texture when the picture starts off the
94 // texture's origin, with the meta saying where; the D3D paths copy the picture from the origin. Without the
95 // offer the decoder crops such a picture itself.
96 const bool d3dMemory = gst_caps_features_contains(features, "memory:D3D11Memory") ||
97 gst_caps_features_contains(features, "memory:D3D12Memory");
98 addConsumedAllocationMetas(query, !d3dMemory);
99 return true;
100 }
101
102 // The entry carries the buffers the sink holds, which a producer with a pool of its own adds to that pool's size:
103 // it goes in without a pool when none is asked for, as in upstream's sinks.
104 if (!need_pool || !tryProposeMetaPool(query, caps, size, minBuffers)) {
105 gst_query_add_allocation_pool(query, NULL, size, minBuffers, 0);
106 }
107 addConsumedAllocationMetas(query);
108 return true;
109}
110
111} // namespace GstQgc
static constexpr int kMaxPinnedFrames
bool dmaDrmAwareVideoInfo(GstCaps *caps, GstVideoInfo *info)
GstVideoSink that pushes decoded frames into a Qt QVideoSink; sole sink in qgcvideosinkbin....
bool populateAllocationQuery(GstQuery *query)
bool proposeDeviceBoundPool(GstQuery *query, GstCaps *caps, const GstVideoInfo *vinfo, gsize size, guint minBuffers) noexcept
Definition HwBuffers.cc:275