5#include <QtCore/QByteArray>
8#include <QtCore/QString>
23 GList* factories = gst_element_factory_list_get_elements(
24 static_cast<GstElementFactoryListType
>(GST_ELEMENT_FACTORY_TYPE_DECODER | GST_ELEMENT_FACTORY_TYPE_MEDIA_VIDEO),
26 for (GList* node = factories; node !=
nullptr; node = node->next) {
27 visitor(GST_ELEMENT_FACTORY(node->data));
29 gst_plugin_feature_list_free(factories);
34struct DecoderFamilyPrefix
44constexpr DecoderFamilyPrefix kDecoderFamilyPrefixes[] = {
63const DecoderFamilyPrefix* findDecoderFamilyPrefix(
const char* factoryName)
66 for (
const DecoderFamilyPrefix& entry : kDecoderFamilyPrefixes) {
67 if (g_str_has_prefix(lowerName.get(), entry.prefix)) {
82 const DecoderFamilyPrefix* entry = findDecoderFamilyPrefix(factoryName);
92 const gchar* factoryName = gst_plugin_feature_get_name(GST_PLUGIN_FEATURE(factory));
97 if (
const DecoderFamilyPrefix* entry = findDecoderFamilyPrefix(factoryName)) {
98 return entry->isHardware;
101 const auto containsHardware = [](
const gchar* value) {
106 return g_strrstr(lower.get(),
"hardware") !=
nullptr;
109 return containsHardware(gst_element_factory_get_metadata(factory, GST_ELEMENT_METADATA_KLASS)) ||
110 containsHardware(gst_element_factory_get_metadata(factory, GST_ELEMENT_METADATA_DESCRIPTION));
115 GstPluginFeature*
const feature = GST_PLUGIN_FEATURE(factory);
117 return QStringLiteral(
"[%1] %2/%3 rank=%4 (%5)")
119 plugin ? QString::fromUtf8(gst_plugin_get_name(GST_PLUGIN(plugin.get()))) : QStringLiteral(
"?"),
120 QString::fromUtf8(gst_plugin_feature_get_name(feature)))
121 .arg(gst_plugin_feature_get_rank(feature))
122 .arg(QString::fromUtf8(gst_element_factory_get_klass(factory)));
128bool rankedByUser(
const gchar* featureName)
130 const QList<QByteArray> userRanks = qgetenv(
"GST_PLUGIN_FEATURE_RANK").split(
',');
131 return std::ranges::any_of(userRanks, [featureName](
const QByteArray& entry) {
132 return entry.left(entry.indexOf(
':')).trimmed() == featureName;
140qint64 decoderAdapterLuid(GstElementFactory* factory)
142 static QHash<QByteArray, qint64> s_luids;
143 const QByteArray name(gst_plugin_feature_get_name(GST_PLUGIN_FEATURE(factory)));
144 if (
const auto known = s_luids.constFind(name); known != s_luids.constEnd()) {
145 return known.value();
148 if (
const GstElementPtr decoder =
makeElement(name.constData());
149 decoder && g_object_class_find_property(G_OBJECT_GET_CLASS(decoder.get()),
"adapter-luid")) {
150 g_object_get(decoder.get(),
"adapter-luid", &luid,
nullptr);
152 s_luids.insert(name, luid);
158QList<DecoderInfo> installedDecoders()
160 QList<DecoderInfo> decoders;
162 GstPluginFeature*
const feature = GST_PLUGIN_FEATURE(factory);
163 const gchar*
const name = gst_plugin_feature_get_name(feature);
164 DecoderInfo decoder{.name = QByteArray(name),
165 .rank = gst_plugin_feature_get_rank(feature),
170 decoder.adapterLuid = decoderAdapterLuid(factory);
173 decoders.append(decoder);
179RankTraits currentRankTraits()
182#if defined(Q_OS_LINUX)
183 traits.preferZeroCopyDecoders =
true;
184#elif defined(Q_OS_WIN)
193 d3d12 = qEnvironmentVariable(
"QSG_RHI_BACKEND").compare(QLatin1String(
"d3d12"), Qt::CaseInsensitive) == 0;
203 traits.rhiD3DApi = d3d12 ? D3DApi::D3D12 : D3DApi::D3D11;
222 GstRegistry*
const registry = _registry();
226 const QMutexLocker locker(&_mutex);
229 const RankTraits traits = currentRankTraits();
231 qCDebug(GStreamerDecoderRanksLog) <<
"Applying decoder option" <<
static_cast<int>(option) <<
"with"
232 << plan.
changes.size() <<
"rank changes";
233 for (
const QString& warning : plan.
warnings) {
234 qCWarning(GStreamerDecoderRanksLog).noquote() << warning;
237 (void) _setRank(registry, change.
feature.constData(), change.
rank);
240 _appliedTraits = traits;
245 if (_applied && (currentRankTraits() != _appliedTraits)) {
252 if (GstRegistry*
const registry = _registry()) {
253 const QMutexLocker locker(&_mutex);
260 const QMutexLocker locker(&_mutex);
261 return _setRank(registry, featureName, rank);
266 GstRegistry*
const registry = _registry();
267 if (!registry || !featureName || !caps || rankedByUser(featureName)) {
271 GList*
const decoders = gst_element_factory_list_get_elements(GST_ELEMENT_FACTORY_TYPE_DECODER, GST_RANK_MARGINAL);
272 GList*
const candidates = gst_element_factory_list_filter(decoders, caps, GST_PAD_SINK, TRUE);
273 gst_plugin_feature_list_free(decoders);
274 bool otherDecoder =
false;
275 for (GList* node = candidates; node !=
nullptr; node = node->next) {
277 otherDecoder || (g_strcmp0(gst_plugin_feature_get_name(GST_PLUGIN_FEATURE(node->data)), featureName) != 0);
279 gst_plugin_feature_list_free(candidates);
283 const QMutexLocker locker(&_mutex);
284 return _setRank(registry, featureName, GST_RANK_NONE);
287bool DecoderRankApplier::_setRank(GstRegistry* registry,
const char* featureName, uint16_t rank)
289 if (!registry || !featureName) {
292 const GstFeaturePtr feature = adoptFeature(gst_registry_lookup_feature(registry, featureName));
297 if (rankedByUser(featureName)) {
298 qCDebug(GStreamerDecoderRanksLog) <<
" Keeping the GST_PLUGIN_FEATURE_RANK rank of" << featureName;
301 const QByteArray key(featureName);
302 if (!_originalRanks.contains(key)) {
303 _originalRanks.insert(key, gst_plugin_feature_get_rank(feature.get()));
305 qCDebug(GStreamerDecoderRanksLog) <<
" Changing feature (" << featureName <<
") to use rank:" << rank;
306 gst_plugin_feature_set_rank(feature.get(), rank);
310GstRegistry* DecoderRankApplier::_registry()
312 if (!gst_is_initialized()) {
315 GstRegistry*
const registry = gst_registry_get();
317 qCCritical(GStreamerDecoderRanksLog) <<
"Failed to get gstreamer registry.";
322void DecoderRankApplier::_restore(GstRegistry* registry)
324 for (
auto it = _originalRanks.cbegin(); it != _originalRanks.cend(); ++it) {
327 gst_plugin_feature_set_rank(feature.get(), it.value());
365 QList<VideoDecoderOption> families;
369#ifndef QGC_HAS_GST_VULKAN_GPU_PATH
376 families.append(family);
384 if (!GStreamerDecoderRanksLog().isDebugEnabled()) {
395 QList<Decoder> decoders;
397 GstPluginFeature*
const feature = GST_PLUGIN_FEATURE(factory);
398 decoders.append({gst_plugin_feature_get_rank(feature), QByteArray(gst_plugin_feature_get_name(feature)),
402 if (decoders.isEmpty()) {
403 qCDebug(GStreamerDecoderRanksLog) <<
"No video decoder factories found";
407 std::ranges::sort(decoders, [](
const Decoder& lhs,
const Decoder& rhs) {
408 return (lhs.rank != rhs.rank) ? (lhs.rank > rhs.rank) : (lhs.name < rhs.name);
411 qCDebug(GStreamerDecoderRanksLog) <<
"Video decoder ranks:";
412 for (
const Decoder& decoder : std::as_const(decoders)) {
413 qCDebug(GStreamerDecoderRanksLog).noquote() <<
" " + decoder.description;
#define QGC_LOGGING_CATEGORY(name, categoryStr)
VideoDecoderOption
The decoders VideoSettings' forceVideoDecoder can force. The values are persisted.
void apply(VideoDecoderOption option)
Restores the original ranks, then applies option's plan for the installed decoders on this platform.
bool setRank(GstRegistry *registry, const char *featureName, uint16_t rank)
Sets featureName's rank in registry. False when the registry or name is null or the feature is absent...
DecoderRankApplier(const DecoderRankApplier &)=delete
bool demoteFailedDecoder(const char *featureName, const GstCaps *caps)
void restore()
Restores the original ranks.
void reapply()
apply() for the option applied last, when the platform's RankTraits changed since: Qt's device became...
static DecoderRankApplier & instance()
ScopedDecoderOption(VideoDecoderOption option)
QList< VideoDecoderOption > availableDecoderFamilies()
std::unique_ptr< GstObject, GstObjectDeleter > GstObjectPtr
GCharPtr adoptGChar(gchar *str) noexcept
bool changeFeatureRank(GstRegistry *registry, const char *featureName, uint16_t rank)
DecoderRankApplier::setRank() on the process's applier, so setCodecPriorities() restores the rank too...
std::unique_ptr< GstPluginFeature, GstObjectDeleter > GstFeaturePtr
GstElementPtr makeElement(const char *factory, const char *name=nullptr)
void setCodecPriorities(VideoDecoderOption option)
DecoderRankApplier::apply() on the process's applier. GUI thread.
bool demoteFailedDecoder(const char *factoryName, const GstCaps *caps)
DecoderRankApplier::demoteFailedDecoder() on the process's applier. Any thread.
GstObjectPtr adoptObject(GstObject *object) noexcept
bool isHardwareDecoderFactory(GstElementFactory *factory)
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.
VideoDecoderOption decoderFamilyForFactory(const char *factoryName)
GstFeaturePtr adoptFeature(GstPluginFeature *feature) noexcept
QString describeDecoderFactory(GstElementFactory *factory)
"[HW|SW] <plugin>/<factory> rank=<rank> (<klass>)" for decoder logs.
void forEachVideoDecoderFactory(GstRank minRank, const std::function< void(GstElementFactory *)> &visitor)
std::unique_ptr< gchar, GFreeDeleter > GCharPtr
RenderTarget currentRenderTarget()
@ 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.
qint64 graphicsAdapterLuid() noexcept
bool d3d11OpensD3D12() noexcept
bool softwareRenderingRequested() noexcept
What configureQtGraphics() was told: the user asked for software rendering (–swrast)....
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.