Debian Codegraph @

sys/qsv/libmfx/dispatcher/vpl/mfx_dispatcher_vpl_lowlatency.cpp

raw
1 /*############################################################################
2 # Copyright (C) Intel Corporation
3 #
4 # SPDX-License-Identifier: MIT
5 ############################################################################*/
6
7 #include "vpl/mfx_dispatcher_vpl.h"
8
9 #if defined(_WIN32) || defined(_WIN64)
10 #include "vpl/mfx_dispatcher_vpl_win.h"
11
12 #if defined _M_IX86
13 // Windows x86
14 #define LIB_ONEVPL L"libmfx32-gen.dll"
15 #define LIB_MSDK L"libmfxhw32.dll"
16 #else
17 // Windows x64
18 #define LIB_ONEVPL L"libmfx64-gen.dll"
19 #define LIB_MSDK L"libmfxhw64.dll"
20 #endif
21 #elif defined(__linux__)
22 // Linux x64
23 #define LIB_ONEVPL "libmfx-gen.so.1.2"
24 #define LIB_MSDK "libmfxhw64.so.1"
25 #endif
26
27 // For Windows:
28 // VPL - load from Driver Store, look only for libmfx64-gen.dll (32)
29 // MSDK - load from Driver Store, look only for libmfxhw64.dll (32)
30 // MSDK - fallback, load from %windir%\system32 or %windir%\syswow64
31
32 // For Linux:
33 // VPL - load from system paths in LoadLibsFromMultipleDirs(), look only for libmfx-gen.so.1.2
34 // MSDK - load from system paths in LoadLibsFromMultipleDirs(), look only for libmfxhw64.so.1
35
36 // library names
37 static const CHAR_TYPE *libNameVPL = LIB_ONEVPL;
38 static const CHAR_TYPE *libNameMSDK = LIB_MSDK;
39
40 // required exports
41 static const char *reqFuncVPL = "MFXInitialize";
42 static const char *reqFuncMSDK = "MFXInitEx";
43
44 LibInfo *LoaderCtxVPL::AddSingleLibrary(STRING_TYPE libPath, LibType libType) {
45 LibInfo *libInfo = nullptr;
46
47 #if defined(_WIN32) || defined(_WIN64)
48 // try to open library
49 mfxModuleHandle hLib = MFX::mfx_dll_load(libPath.c_str());
50 if (!hLib)
51 return nullptr;
52
53 // check for required entrypoint function
54 const char *reqFunc = (libType == LibTypeVPL ? reqFuncVPL : reqFuncMSDK);
55 VPLFunctionPtr pProc = (VPLFunctionPtr)MFX::mfx_dll_get_addr(hLib, reqFunc);
56 MFX::mfx_dll_free(hLib);
57
58 // entrypoint function missing - invalid library
59 if (!pProc)
60 return nullptr;
61 #else
62 // try to open library
63 void *hLib = dlopen(libPath.c_str(), RTLD_LOCAL | RTLD_NOW);
64 if (!hLib)
65 return nullptr;
66
67 // check for required entrypoint function
68 const char *reqFunc = (libType == LibTypeVPL ? reqFuncVPL : reqFuncMSDK);
69 VPLFunctionPtr pProc = (VPLFunctionPtr)dlsym(hLib, reqFunc);
70 dlclose(hLib);
71
72 // entrypoint function missing - invalid library
73 if (!pProc)
74 return nullptr;
75 #endif
76
77 // create new LibInfo and add to list
78 libInfo = new LibInfo;
79 if (!libInfo)
80 return nullptr;
81
82 libInfo->libNameFull = libPath;
83 libInfo->libType = libType;
84 libInfo->libPriority = (libType == LibTypeVPL ? LIB_PRIORITY_01 : LIB_PRIORITY_LEGACY);
85
86 return libInfo;
87 }
88
89 mfxStatus LoaderCtxVPL::LoadLibsFromDriverStore(mfxU32 numAdapters,
90 const std::vector<DXGI1DeviceInfo> &adapterInfo,
91 LibType libType) {
92 #if defined(_WIN32) || defined(_WIN64)
93 mfxStatus sts = MFX_ERR_NONE;
94
95 mfxI32 storageID = MFX::MFX_UNKNOWN_KEY;
96 const CHAR_TYPE *libName = nullptr;
97 const char *reqFunc = nullptr;
98
99 if (libType == LibTypeVPL) {
100 storageID = MFX::MFX_DRIVER_STORE_ONEVPL;
101 libName = libNameVPL;
102 reqFunc = reqFuncVPL;
103 }
104 else if (libType == LibTypeMSDK) {
105 storageID = MFX::MFX_DRIVER_STORE;
106 libName = libNameMSDK;
107 reqFunc = reqFuncMSDK;
108 }
109 else {
110 return MFX_ERR_UNSUPPORTED;
111 }
112
113 // get path to Windows driver store
114 STRING_TYPE libPath;
115 mfxU32 adapterID;
116 for (adapterID = 0; adapterID < numAdapters; adapterID++) {
117 // get driver store path for this adapter
118 libPath.clear();
119 sts = MFX::MFXLibraryIterator::GetDriverStoreDir(libPath,
120 MAX_VPL_SEARCH_PATH,
121 adapterInfo[adapterID].deviceID,
122 storageID);
123 if (sts != MFX_ERR_NONE || libPath.size() == 0)
124 continue;
125
126 // try to open library
127 libPath += libName;
128 LibInfo *libInfo = AddSingleLibrary(libPath, libType);
129
130 // if successful, add to list and return (stop at first
131 if (libInfo) {
132 m_libInfoList.push_back(libInfo);
133 return MFX_ERR_NONE;
134 }
135 }
136
137 return MFX_ERR_UNSUPPORTED;
138 #else
139 // Linux - not supported
140 return MFX_ERR_UNSUPPORTED;
141 #endif
142 }
143
144 mfxStatus LoaderCtxVPL::LoadLibsFromSystemDir(LibType libType) {
145 #if defined(_WIN32) || defined(_WIN64)
146 mfxStatus sts = MFX_ERR_NONE;
147
148 const CHAR_TYPE *libName = nullptr;
149 const char *reqFunc = nullptr;
150
151 if (libType == LibTypeVPL) {
152 libName = libNameVPL;
153 reqFunc = reqFuncVPL;
154 }
155 else if (libType == LibTypeMSDK) {
156 libName = libNameMSDK;
157 reqFunc = reqFuncMSDK;
158 }
159 else {
160 return MFX_ERR_UNSUPPORTED;
161 }
162
163 // get path to Windows system dir
164 STRING_TYPE libPath;
165 libPath.clear();
166
167 std::list<STRING_TYPE> winSysDir;
168 ParseEnvSearchPaths(L"windir", winSysDir);
169
170 // should resolve to a single directory, otherwise something went wrong
171 if (winSysDir.size() == 1) {
172 #if defined _M_IX86
173 libPath = winSysDir.front() + L"\\syswow64\\";
174 #else
175 libPath = winSysDir.front() + L"\\system32\\";
176 #endif
177 }
178 else {
179 return MFX_ERR_UNSUPPORTED;
180 }
181
182 // try to open library
183 libPath += libName;
184 LibInfo *libInfo = AddSingleLibrary(libPath, libType);
185
186 // if successful, add to list and return (stop at first
187 if (libInfo) {
188 m_libInfoList.push_back(libInfo);
189 return MFX_ERR_NONE;
190 }
191
192 return MFX_ERR_UNSUPPORTED;
193 #else
194 // Linux - not supported
195 return MFX_ERR_UNSUPPORTED;
196 #endif
197 }
198
199 mfxStatus LoaderCtxVPL::LoadLibsFromMultipleDirs(LibType libType) {
200 #ifdef __linux__
201 // clang-format off
202
203 // standard paths for RT installation on Linux
204 std::vector<std::string> llSearchDir = {
205 "/usr/lib/x86_64-linux-gnu",
206 "/lib",
207 "/usr/lib",
208 "/lib64",
209 "/usr/lib64",
210 };
211
212 // clang-format on
213
214 const CHAR_TYPE *libName = nullptr;
215
216 if (libType == LibTypeVPL) {
217 libName = libNameVPL;
218 }
219 else if (libType == LibTypeMSDK) {
220 libName = libNameMSDK;
221
222 // additional search directories for MSDK
223 llSearchDir.push_back("/opt/intel/mediasdk/lib");
224 llSearchDir.push_back("/opt/intel/mediasdk/lib64");
225 }
226 else {
227 return MFX_ERR_UNSUPPORTED;
228 }
229
230 for (const auto &searchDir : llSearchDir) {
231 STRING_TYPE libPath;
232 libPath = searchDir;
233 libPath += "/";
234 libPath += libName;
235
236 // try to open library
237 LibInfo *libInfo = AddSingleLibrary(libPath, libType);
238
239 // if successful, add to list and return (stop at first success)
240 if (libInfo) {
241 m_libInfoList.push_back(libInfo);
242 return MFX_ERR_NONE;
243 }
244 }
245
246 return MFX_ERR_UNSUPPORTED;
247 #else
248 return MFX_ERR_UNSUPPORTED;
249 #endif
250 }
251
252 mfxStatus LoaderCtxVPL::LoadLibsLowLatency() {
253 DISP_LOG_FUNCTION(&m_dispLog);
254
255 #if defined(_WIN32) || defined(_WIN64)
256 mfxStatus sts = MFX_ERR_NONE;
257
258 // check driver store
259 mfxU32 numAdapters = 0;
260
261 std::vector<DXGI1DeviceInfo> adapterInfo;
262 bool bEnumSuccess = MFX::DXGI1Device::GetAdapterList(adapterInfo);
263 numAdapters = (mfxU32)adapterInfo.size();
264
265 // error - no graphics adapters found
266 if (!bEnumSuccess || numAdapters == 0)
267 return MFX_ERR_UNSUPPORTED;
268
269 // try loading oneVPL from driver store
270 sts = LoadLibsFromDriverStore(numAdapters, adapterInfo, LibTypeVPL);
271 if (sts == MFX_ERR_NONE) {
272 LibInfo *libInfo = m_libInfoList.back();
273
274 sts = LoadSingleLibrary(libInfo);
275 if (sts == MFX_ERR_NONE) {
276 LoadAPIExports(libInfo, LibTypeVPL);
277 m_bNeedLowLatencyQuery = false;
278 return MFX_ERR_NONE;
279 }
280 UnloadSingleLibrary(libInfo); // failed - unload and move to next location
281 }
282
283 // try loading MSDK from driver store
284 sts = LoadLibsFromDriverStore(numAdapters, adapterInfo, LibTypeMSDK);
285 if (sts == MFX_ERR_NONE) {
286 LibInfo *libInfo = m_libInfoList.back();
287
288 sts = LoadSingleLibrary(libInfo);
289 if (sts == MFX_ERR_NONE) {
290 mfxU32 numFunctions = LoadAPIExports(libInfo, LibTypeMSDK);
291
292 if (numFunctions == NumMSDKFunctions) {
293 mfxVariant var = {};
294 var.Type = MFX_VARIANT_TYPE_PTR;
295 var.Data.Ptr = (mfxHDL) "mfxhw64";
296
297 auto it = m_configCtxList.begin();
298
299 while (it != m_configCtxList.end()) {
300 ConfigCtxVPL *config = (*it);
301 sts = config->SetFilterProperty((const mfxU8 *)"mfxImplDescription.ImplName",
302 var);
303 if (sts != MFX_ERR_NONE)
304 return MFX_ERR_UNSUPPORTED;
305 it++;
306 }
307
308 m_bNeedLowLatencyQuery = false;
309 return MFX_ERR_NONE;
310 }
311 }
312 UnloadSingleLibrary(libInfo); // failed - unload and move to next location
313 }
314
315 // try loading MSDK from sysdir %windir%\system32 and %windir%\syswow64
316 sts = LoadLibsFromSystemDir(LibTypeMSDK);
317 if (sts == MFX_ERR_NONE) {
318 LibInfo *libInfo = m_libInfoList.front();
319
320 sts = LoadSingleLibrary(libInfo);
321 if (sts == MFX_ERR_NONE) {
322 mfxU32 numFunctions = LoadAPIExports(libInfo, LibTypeMSDK);
323
324 if (numFunctions == NumMSDKFunctions) {
325 mfxVariant var = {};
326 var.Type = MFX_VARIANT_TYPE_PTR;
327 var.Data.Ptr = (mfxHDL) "mfxhw64";
328
329 auto it = m_configCtxList.begin();
330
331 while (it != m_configCtxList.end()) {
332 ConfigCtxVPL *config = (*it);
333 sts = config->SetFilterProperty((const mfxU8 *)"mfxImplDescription.ImplName",
334 var);
335 if (sts != MFX_ERR_NONE)
336 return MFX_ERR_UNSUPPORTED;
337 it++;
338 }
339 m_bNeedLowLatencyQuery = false;
340 return MFX_ERR_NONE;
341 }
342 }
343 UnloadSingleLibrary(libInfo); // failed - unload and move to next location
344 }
345
346 return MFX_ERR_UNSUPPORTED;
347 #else
348 mfxStatus sts = MFX_ERR_NONE;
349
350 // try loading VPL from Linux system directories
351 sts = LoadLibsFromMultipleDirs(LibTypeVPL);
352 if (sts == MFX_ERR_NONE) {
353 LibInfo *libInfo = m_libInfoList.back();
354
355 sts = LoadSingleLibrary(libInfo);
356 if (sts == MFX_ERR_NONE) {
357 LoadAPIExports(libInfo, LibTypeVPL);
358 m_bNeedLowLatencyQuery = false;
359 return MFX_ERR_NONE;
360 }
361 UnloadSingleLibrary(libInfo); // failed - unload and move to next location
362 }
363
364 // try loading MSDK from Linux system directories
365 sts = LoadLibsFromMultipleDirs(LibTypeMSDK);
366 if (sts == MFX_ERR_NONE) {
367 LibInfo *libInfo = m_libInfoList.back();
368
369 sts = LoadSingleLibrary(libInfo);
370 if (sts == MFX_ERR_NONE) {
371 mfxU32 numFunctions = LoadAPIExports(libInfo, LibTypeMSDK);
372
373 if (numFunctions == NumMSDKFunctions) {
374 mfxVariant var = {};
375 var.Type = MFX_VARIANT_TYPE_PTR;
376 var.Data.Ptr = (mfxHDL) "mfxhw64";
377
378 auto it = m_configCtxList.begin();
379
380 while (it != m_configCtxList.end()) {
381 ConfigCtxVPL *config = (*it);
382 sts = config->SetFilterProperty((const mfxU8 *)"mfxImplDescription.ImplName",
383 var);
384 if (sts != MFX_ERR_NONE)
385 return MFX_ERR_UNSUPPORTED;
386 it++;
387 }
388 m_bNeedLowLatencyQuery = false;
389 return MFX_ERR_NONE;
390 }
391 }
392 UnloadSingleLibrary(libInfo); // failed - unload and move to next location
393 }
394
395 return MFX_ERR_UNSUPPORTED;
396 #endif
397 }
398
399 // try creating a session in order to get runtime API version
400 mfxStatus LoaderCtxVPL::QuerySessionLowLatency(LibInfo *libInfo,
401 mfxU32 adapterID,
402 mfxVersion *ver) {
403 mfxStatus sts;
404 mfxSession session = nullptr;
405
406 mfxVersion reqVersion;
407 if (libInfo->libType == LibTypeVPL) {
408 reqVersion.Major = 2;
409 reqVersion.Minor = 0;
410 }
411 else {
412 reqVersion.Major = 1;
413 reqVersion.Minor = 0;
414 }
415
416 // set acceleration mode
417 mfxInitializationParam vplParam = {};
418 vplParam.AccelerationMode = m_specialConfig.accelerationMode;
419
420 // set adapter ID for both MSDK and VPL
421 vplParam.VendorImplID = adapterID;
422 mfxIMPL hwImpl = msdkImplTab[adapterID];
423
424 mfxU16 deviceID;
425 sts = MFXInitEx2(reqVersion,
426 vplParam,
427 hwImpl,
428 &session,
429 &deviceID,
430 (CHAR_TYPE *)libInfo->libNameFull.c_str());
431
432 if (sts == MFX_ERR_NONE) {
433 sts = MFXQueryVersion(session, ver);
434 MFXClose(session);
435 }
436
437 return sts;
438 }