QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
GStreamerDecoderRankPlan.cc
Go to the documentation of this file.
2
3#include <algorithm>
4#include <gst/gstpluginfeature.h>
5
6namespace GStreamer {
7
8namespace {
9
10// What forcing a decoder family gives it at least. GStreamer already ranks some decoders PRIMARY + 1 (the d3d11 and va
11// ones), so a forced family goes above the highest other decoder (see prioritizeFamily).
12constexpr uint16_t kPrioritizedRank = GST_RANK_PRIMARY + 1;
13// Above kPrioritizedRank, so the GPU-memory decoders the platform prefers win autoplug.
14constexpr uint16_t kZeroCopyRank = GST_RANK_PRIMARY + 2;
15
16bool autoplugged(const DecoderInfo& decoder)
17{
18 return decoder.rank >= GST_RANK_MARGINAL;
19}
20
21class Planner
22{
23public:
24 explicit Planner(const QList<DecoderInfo>& decoders)
25 : _decoders(decoders)
26 , _ranks(decoders.size())
27 {}
28
29 template <typename Predicate>
30 qsizetype rank(Predicate&& matches, uint16_t rank)
31 {
32 qsizetype ranked = 0;
33 for (qsizetype i = 0; i < _decoders.size(); ++i) {
34 if (matches(_decoders.at(i))) {
35 _ranks[i] = rank;
36 ++ranked;
37 }
38 }
39 return ranked;
40 }
41
42 void rankPrefix(const char* prefix, uint16_t rank)
43 {
44 (void) this->rank([prefix](const DecoderInfo& decoder) { return decoder.name.startsWith(prefix); }, rank);
45 }
46
47 void rankFamily(VideoDecoderOption family, uint16_t rank)
48 {
49 (void) this->rank([family](const DecoderInfo& decoder) { return decoder.family == family; }, rank);
50 }
51
52 // @p family's decoders, at least kPrioritizedRank, above every other decoder; those stay as fallbacks for the
53 // codecs the family can't decode. Returns the rank the family got.
54 uint16_t prioritizeFamily(VideoDecoderOption family)
55 {
56 unsigned highestOtherRank = GST_RANK_NONE;
57 for (const DecoderInfo& decoder : _decoders) {
58 if (decoder.family != family) {
59 highestOtherRank = std::max(highestOtherRank, decoder.rank);
60 }
61 }
62 const unsigned rank = std::max<unsigned>(kPrioritizedRank, std::min<unsigned>(highestOtherRank, 0xFFFD) + 1);
63 rankFamily(family, static_cast<uint16_t>(rank));
64 return static_cast<uint16_t>(rank);
65 }
66
67 // Every autoplugged decoder of the other class out of autoplug. Those of the class keep GStreamer's ranks, so its
68 // order among them holds and one it ranks NONE (experimental, or unfit for the platform) stays out.
69 void demoteOtherClass(bool hardware, QStringList& warnings)
70 {
71 const bool classAutoplugged = std::ranges::any_of(_decoders, [&](const DecoderInfo& decoder) {
72 return (decoder.hardware == hardware) && autoplugged(decoder);
73 });
74 if (!_decoders.isEmpty() && !classAutoplugged) {
75 warnings.append(QStringLiteral("No %1 video decoder that GStreamer autoplugs is installed: "
76 "video will not decode with this option.")
77 .arg(hardware ? QStringLiteral("hardware") : QStringLiteral("software")));
78 }
79 (void) rank([&](const DecoderInfo& decoder) { return (decoder.hardware != hardware) && autoplugged(decoder); },
80 GST_RANK_NONE);
81 }
82
83 // The decoders whose DMABuf output the GPU paths import, above every other. A va decoder GStreamer ranks NONE
84 // (vajpegdec, vah266dec) stays out of autoplug, except where it ranks them all NONE, as it did before 1.24.
85 void prioritizeZeroCopy()
86 {
87 const auto va = [](const DecoderInfo& decoder) { return decoder.family == VideoDecoderOption::VAAPI; };
88 const bool someAutoplugged = std::ranges::any_of(
89 _decoders, [&](const DecoderInfo& decoder) { return va(decoder) && autoplugged(decoder); });
90 (void) rank(
91 [&](const DecoderInfo& decoder) { return va(decoder) && (!someAutoplugged || autoplugged(decoder)); },
92 kZeroCopyRank);
93 rankPrefix("v4l2sl", kZeroCopyRank);
94 }
95
96 // Every hardware decoder that would rank above @p rank, down to it.
97 void lowerHardware(uint16_t rank)
98 {
99 for (qsizetype i = 0; i < _decoders.size(); ++i) {
100 const DecoderInfo& decoder = _decoders.at(i);
101 const unsigned planned = _ranks.at(i) ? *_ranks.at(i) : decoder.rank;
102 if (decoder.hardware && (planned > rank)) {
103 _ranks[i] = rank;
104 }
105 }
106 }
107
108 QList<RankChange> changes() const
109 {
110 QList<RankChange> changes;
111 for (qsizetype i = 0; i < _decoders.size(); ++i) {
112 if (_ranks.at(i)) {
113 changes.append({.feature = _decoders.at(i).name, .rank = *_ranks.at(i)});
114 }
115 }
116 return changes;
117 }
118
119private:
120 const QList<DecoderInfo>& _decoders;
121 QList<std::optional<uint16_t>> _ranks;
122};
123
124} // namespace
125
126RankPlan planRanks(VideoDecoderOption option, const QList<DecoderInfo>& decoders, const RankTraits& traits)
127{
128 RankPlan plan;
129 Planner planner(decoders);
130 uint16_t familyRank = 0;
131
132 switch (option) {
134 if (traits.preferZeroCopyDecoders) {
135 planner.rankPrefix("vaapi", GST_RANK_NONE);
136 planner.prioritizeZeroCopy();
137 }
138 break;
140 planner.demoteOtherClass(false, plan.warnings);
141 break;
143 planner.demoteOtherClass(true, plan.warnings);
144 if (traits.preferZeroCopyDecoders) {
145 // Decoders of one rank autoplug in name order, the legacy vaapi ones before the va ones. The vaapi
146 // ones stay as the hardware fallback for a codec nothing else decodes.
147 planner.prioritizeZeroCopy();
148 }
149 break;
151 // gst-vulkan's decoders output VulkanImage memory only, which no sink bin offers: Qt can't sample the
152 // images of their VkDevice.
153 plan.warnings.append(
154 QStringLiteral("Forcing Vulkan video decoders: they output VulkanImage memory only, which the video "
155 "sink does not take, so the streams they decode will not display."));
156 [[fallthrough]];
162 familyRank = planner.prioritizeFamily(option);
163 break;
164 }
165
166 if (traits.rhiD3DApi) {
167 // A Direct3D 12 RHI samples d3d12 frames, a Direct3D 11 one d3d11 frames, and d3d12 ones too when its device
168 // opens them.
169 const bool sampledD3D12 = (*traits.rhiD3DApi == D3DApi::D3D12) || traits.d3d11OpensD3D12;
170 const bool sampledD3D11 = (*traits.rhiD3DApi == D3DApi::D3D11);
171 const char* const preferred = sampledD3D12 ? "d3d12" : "d3d11";
172 const auto onRhiAdapter = [&traits](const DecoderInfo& decoder) {
173 return (traits.rhiAdapterLuid == 0) || (decoder.adapterLuid == 0) ||
174 (decoder.adapterLuid == traits.rhiAdapterLuid);
175 };
176 const auto prefer = [&](uint16_t rank) {
177 (void) planner.rank(
178 [&](const DecoderInfo& decoder) { return decoder.name.startsWith(preferred) && onRhiAdapter(decoder); },
179 rank);
180 };
181 const bool forced = (option == VideoDecoderOption::DirectX3D);
182 if ((option == VideoDecoderOption::Default) || (option == VideoDecoderOption::Hardware)) {
183 prefer(kZeroCopyRank);
184 } else if (forced && sampledD3D12 && sampledD3D11) {
185 // Forcing the family ranks all of it above every other decoder: the preferred API goes first within it.
186 prefer(static_cast<uint16_t>(familyRank + 1));
187 }
188 if (forced || (option == VideoDecoderOption::Default) || (option == VideoDecoderOption::Hardware)) {
189 (void) planner.rank(
190 [&](const DecoderInfo& decoder) {
191 return (decoder.family == VideoDecoderOption::DirectX3D) && !onRhiAdapter(decoder);
192 },
193 GST_RANK_SECONDARY);
194 }
195 if (!sampledD3D12) {
196 planner.rankPrefix("d3d12", GST_RANK_NONE);
197 }
198 if (!sampledD3D11) {
199 planner.rankPrefix("d3d11", GST_RANK_NONE);
200 }
201 }
202
203 if (traits.softwareRendering && (option == VideoDecoderOption::Default)) {
204 // After the platform rules, which raise hardware decoders.
205 planner.lowerHardware(GST_RANK_SECONDARY);
206 }
207
208 plan.changes = planner.changes();
209 return plan;
210}
211
212} // namespace GStreamer
VideoDecoderOption
The decoders VideoSettings' forceVideoDecoder can force. The values are persisted.
RankPlan planRanks(VideoDecoderOption option, const QList< DecoderInfo > &decoders, const RankTraits &traits)
The rank changes option makes to decoders, from their original ranks, on a platform with traits.
A video decoder factory as planRanks() sees it.
QList< RankChange > changes
At most one per decoder, in the order of planRanks()'s input.
QStringList warnings
Why the option may not do what it says, for the log.
The platform rules planRanks() adds to an option.
std::optional< D3DApi > rhiD3DApi