From 404ce2e011bff0c60c4c4a08428b7196cfbf33b6 Mon Sep 17 00:00:00 2001 From: Ilya Fedin Date: Wed, 31 May 2023 08:27:47 +0400 Subject: [PATCH] Add check for vaapi/vdpau libraries before loading them with implib --- .../SourceFiles/ffmpeg/ffmpeg_utility.cpp | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/Telegram/SourceFiles/ffmpeg/ffmpeg_utility.cpp b/Telegram/SourceFiles/ffmpeg/ffmpeg_utility.cpp index d1642c0d4..0d166a1e3 100644 --- a/Telegram/SourceFiles/ffmpeg/ffmpeg_utility.cpp +++ b/Telegram/SourceFiles/ffmpeg/ffmpeg_utility.cpp @@ -18,6 +18,9 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL extern "C" { #include +#if !defined DESKTOP_APP_USE_PACKAGED && !defined Q_OS_WIN && !defined Q_OS_MAC +#include +#endif // !DESKTOP_APP_USE_PACKAGED && !Q_OS_WIN && !Q_OS_MAC } // extern "C" namespace FFmpeg { @@ -85,6 +88,47 @@ void PremultiplyLine(uchar *dst, const uchar *src, int intsCount) { #endif // LIB_FFMPEG_USE_QT_PRIVATE_API } +#if !defined DESKTOP_APP_USE_PACKAGED && !defined Q_OS_WIN && !defined Q_OS_MAC +[[nodiscard]] auto CheckHwLibs() { + auto list = std::deque{ + AV_PIX_FMT_CUDA, + }; + const auto vdpau = [&] { + if (const auto handle = dlopen("libvdpau.so.1", RTLD_LAZY)) { + dlclose(handle); + } + if (dlerror()) { + return false; + } + return true; + }(); + if (vdpau) { + list.push_front(AV_PIX_FMT_VDPAU); + } + const auto va = [&] { + const auto list = std::array{ + "libva-drm.so.1", + "libva-x11.so.1", + "libva.so.1", + "libdrm.so.2", + }; + for (const auto lib : list) { + if (const auto handle = dlopen(lib, RTLD_LAZY)) { + dlclose(handle); + } + if (dlerror()) { + return false; + } + } + return true; + }(); + if (va) { + list.push_front(AV_PIX_FMT_VAAPI); + } + return list; +} +#endif // !DESKTOP_APP_USE_PACKAGED && !Q_OS_WIN && !Q_OS_MAC + [[nodiscard]] bool InitHw(AVCodecContext *context, AVHWDeviceType type) { AVCodecContext *parent = static_cast(context->opaque); @@ -125,6 +169,9 @@ void PremultiplyLine(uchar *dst, const uchar *src, int intsCount) { } return false; }; +#if !defined DESKTOP_APP_USE_PACKAGED && !defined Q_OS_WIN && !defined Q_OS_MAC + static const auto list = CheckHwLibs(); +#else // !DESKTOP_APP_USE_PACKAGED && !Q_OS_WIN && !Q_OS_MAC const auto list = std::array{ #ifdef Q_OS_WIN AV_PIX_FMT_D3D11, @@ -138,6 +185,7 @@ void PremultiplyLine(uchar *dst, const uchar *src, int intsCount) { AV_PIX_FMT_CUDA, #endif // Q_OS_WIN || Q_OS_MAC }; +#endif // DESKTOP_APP_USE_PACKAGED || Q_OS_WIN || Q_OS_MAC for (const auto format : list) { if (!has(format)) { continue;