Debian Codegraph @

sys/qsv/libmfx/dispatcher/vpl/mfx_dispatcher_vpl.h

raw
1 /*############################################################################
2 # Copyright (C) Intel Corporation
3 #
4 # SPDX-License-Identifier: MIT
5 ############################################################################*/
6
7 #ifndef DISPATCHER_VPL_MFX_DISPATCHER_VPL_H_
8 #define DISPATCHER_VPL_MFX_DISPATCHER_VPL_H_
9
10 #include <algorithm>
11 #include <cstdlib>
12 #include <list>
13 #include <memory>
14 #include <sstream>
15 #include <string>
16 #include <vector>
17
18 #include "vpl/mfxdispatcher.h"
19 #include "vpl/mfxvideo.h"
20
21 #include "./mfx_dispatcher_vpl_log.h"
22
23 #if defined(_WIN32) || defined(_WIN64)
24 #include <windows.h>
25
26 // use wide char on Windows
27 #define MAKE_STRING(x) L##x
28 typedef std::wstring STRING_TYPE;
29 typedef wchar_t CHAR_TYPE;
30 #else
31 #include <dirent.h>
32 #include <dlfcn.h>
33 #include <string.h>
34 #include <unistd.h>
35
36 // use standard char on Linux
37 #define MAKE_STRING(x) x
38 typedef std::string STRING_TYPE;
39 typedef char CHAR_TYPE;
40 #endif
41
42 #if defined(_WIN32) || defined(_WIN64)
43 #if defined _M_IX86
44 // Windows x86
45 #define MSDK_LIB_NAME L"libmfxhw32."
46 #else
47 // Windows x64
48 #define MSDK_LIB_NAME L"libmfxhw64."
49 #endif
50 #define ONEVPL_PRIORITY_PATH_VAR L"ONEVPL_PRIORITY_PATH"
51 #elif defined(__linux__)
52 // Linux x64
53 #define MSDK_LIB_NAME "libmfxhw64."
54 #define ONEVPL_PRIORITY_PATH_VAR "ONEVPL_PRIORITY_PATH"
55 #endif
56
57 #define MSDK_MIN_VERSION_MAJOR 1
58 #define MSDK_MIN_VERSION_MINOR 0
59
60 #define MAX_MSDK_ACCEL_MODES 16 // see mfxcommon.h --> mfxAccelerationMode
61
62 #define MAX_WINDOWS_ADAPTER_ID 3 // check adapterID in range [0,3]
63
64 #define MAX_NUM_IMPL_MSDK 4
65
66 #define MAX_VPL_SEARCH_PATH 4096
67
68 #define MAX_ENV_VAR_LEN 32768
69
70 #define DEVICE_ID_UNKNOWN 0xffffffff
71 #define ADAPTER_IDX_UNKNOWN 0xffffffff
72
73 #define TAB_SIZE(type, tab) (sizeof(tab) / sizeof(type))
74 #define MAKE_MFX_VERSION(major, minor) \
75 { \
76 { (minor), (major) } \
77 }
78
79 // internal function to load dll by full path, fail if unsuccessful
80 mfxStatus MFXInitEx2(mfxVersion version,
81 mfxInitializationParam vplParam,
82 mfxIMPL hwImpl,
83 mfxSession *session,
84 mfxU16 *deviceID,
85 CHAR_TYPE *dllName);
86
87 typedef void(MFX_CDECL *VPLFunctionPtr)(void);
88
89 extern const mfxIMPL msdkImplTab[MAX_NUM_IMPL_MSDK];
90
91 enum LibType {
92 LibTypeUnknown = -1,
93
94 LibTypeVPL = 0,
95 LibTypeMSDK,
96
97 NumLibTypes
98 };
99
100 enum VPLFunctionIdx {
101 // 2.0
102 IdxMFXQueryImplsDescription = 0,
103 IdxMFXReleaseImplDescription,
104 IdxMFXMemory_GetSurfaceForVPP,
105 IdxMFXMemory_GetSurfaceForEncode,
106 IdxMFXMemory_GetSurfaceForDecode,
107 IdxMFXInitialize,
108
109 // 2.1
110 IdxMFXMemory_GetSurfaceForVPPOut,
111 IdxMFXVideoDECODE_VPP_Init,
112 IdxMFXVideoDECODE_VPP_DecodeFrameAsync,
113 IdxMFXVideoDECODE_VPP_Reset,
114 IdxMFXVideoDECODE_VPP_GetChannelParam,
115 IdxMFXVideoDECODE_VPP_Close,
116 IdxMFXVideoVPP_ProcessFrameAsync,
117
118 NumVPLFunctions
119 };
120
121 // select MSDK functions for 1.x style caps query
122 enum MSDKCompatFunctionIdx {
123 IdxMFXInitEx = 0,
124 IdxMFXClose,
125
126 NumMSDKFunctions
127 };
128
129 // both Windows and Linux use char* for function names
130 struct VPLFunctionDesc {
131 const char *pName;
132 mfxVersion apiVersion;
133 };
134
135 // DX adapter info
136 struct DXGI1DeviceInfo {
137 mfxU32 vendorID;
138 mfxU32 deviceID;
139 mfxU64 luid;
140 };
141
142 // priority of runtime loading, based on oneAPI-spec
143 enum LibPriority {
144 LIB_PRIORITY_SPECIAL = 0, // highest priority regardless of other priority rules
145
146 LIB_PRIORITY_01 = 1,
147 LIB_PRIORITY_02 = 2,
148 LIB_PRIORITY_03 = 3,
149 LIB_PRIORITY_04 = 4,
150 LIB_PRIORITY_05 = 5,
151
152 LIB_PRIORITY_LEGACY_DRIVERSTORE = 10000,
153 LIB_PRIORITY_LEGACY,
154 };
155
156 enum CfgPropState {
157 CFG_PROP_STATE_NOT_SET = 0,
158 CFG_PROP_STATE_SUPPORTED,
159 CFG_PROP_STATE_UNSUPPORTED,
160 };
161
162 enum PropRanges {
163 PROP_RANGE_DEC_W = 0,
164 PROP_RANGE_DEC_H,
165 PROP_RANGE_ENC_W,
166 PROP_RANGE_ENC_H,
167 PROP_RANGE_VPP_W,
168 PROP_RANGE_VPP_H,
169
170 NUM_PROP_RANGES
171 };
172
173 // must match eProp_TotalProps, is checked with static_assert in _config.cpp
174 // (should throw error at compile time if !=)
175 #define NUM_TOTAL_FILTER_PROPS 56
176
177 // typedef child structures for easier reading
178 typedef struct mfxDecoderDescription::decoder DecCodec;
179 typedef struct mfxDecoderDescription::decoder::decprofile DecProfile;
180 typedef struct mfxDecoderDescription::decoder::decprofile::decmemdesc DecMemDesc;
181
182 typedef struct mfxEncoderDescription::encoder EncCodec;
183 typedef struct mfxEncoderDescription::encoder::encprofile EncProfile;
184 typedef struct mfxEncoderDescription::encoder::encprofile::encmemdesc EncMemDesc;
185
186 typedef struct mfxVPPDescription::filter VPPFilter;
187 typedef struct mfxVPPDescription::filter::memdesc VPPMemDesc;
188 typedef struct mfxVPPDescription::filter::memdesc::format VPPFormat;
189
190 // flattened version of single enc/dec/vpp configs
191 // each struct contains all _settable_ props
192 // i.e. not implied values like NumCodecs
193 struct DecConfig {
194 mfxU32 CodecID;
195 mfxU16 MaxcodecLevel;
196 mfxU32 Profile;
197 mfxResourceType MemHandleType;
198 mfxRange32U Width;
199 mfxRange32U Height;
200 mfxU32 ColorFormat;
201 };
202
203 struct EncConfig {
204 mfxU32 CodecID;
205 mfxU16 MaxcodecLevel;
206 mfxU16 BiDirectionalPrediction;
207 mfxU16 ReportedStats;
208 mfxU32 Profile;
209 mfxResourceType MemHandleType;
210 mfxRange32U Width;
211 mfxRange32U Height;
212 mfxU32 ColorFormat;
213 };
214
215 struct VPPConfig {
216 mfxU32 FilterFourCC;
217 mfxU16 MaxDelayInFrames;
218 mfxResourceType MemHandleType;
219 mfxRange32U Width;
220 mfxRange32U Height;
221 mfxU32 InFormat;
222 mfxU32 OutFormat;
223 };
224
225 // special props which are passed in via MFXSetConfigProperty()
226 // these are updated with every call to ValidateConfig() and may
227 // be used in MFXCreateSession()
228 struct SpecialConfig {
229 bool bIsSet_deviceHandleType;
230 mfxHandleType deviceHandleType;
231
232 bool bIsSet_deviceHandle;
233 mfxHDL deviceHandle;
234
235 bool bIsSet_accelerationMode;
236 mfxAccelerationMode accelerationMode;
237
238 bool bIsSet_ApiVersion;
239 mfxVersion ApiVersion;
240
241 bool bIsSet_dxgiAdapterIdx;
242 mfxU32 dxgiAdapterIdx;
243
244 bool bIsSet_NumThread;
245 mfxU32 NumThread;
246
247 bool bIsSet_DeviceCopy;
248 mfxU16 DeviceCopy;
249
250 bool bIsSet_ExtBuffer;
251 std::vector<mfxExtBuffer *> ExtBuffers;
252 };
253
254 // config class implementation
255 class ConfigCtxVPL {
256 public:
257 ConfigCtxVPL();
258 ~ConfigCtxVPL();
259
260 // set a single filter property (KV pair)
261 mfxStatus SetFilterProperty(const mfxU8 *name, mfxVariant value);
262
263 static bool CheckLowLatencyConfig(std::list<ConfigCtxVPL *> configCtxList,
264 SpecialConfig *specialConfig);
265
266 // compare library caps vs. set of configuration filters
267 static mfxStatus ValidateConfig(const mfxImplDescription *libImplDesc,
268 const mfxImplementedFunctions *libImplFuncs,
269 #ifdef ONEVPL_EXPERIMENTAL
270 const mfxExtendedDeviceId *libImplExtDevID,
271 #endif
272 std::list<ConfigCtxVPL *> configCtxList,
273 LibType libType,
274 SpecialConfig *specialConfig);
275
276 // parse deviceID for x86 devices
277 static bool ParseDeviceIDx86(mfxChar *cDeviceID, mfxU32 &deviceID, mfxU32 &adapterIdx);
278
279 // loader object this config is associated with - needed to
280 // rebuild valid implementation list after each calling
281 // MFXSetConfigFilterProperty()
282 class LoaderCtxVPL *m_parentLoader;
283
284 private:
285 static __inline std::string GetNextProp(std::list<std::string> &s) {
286 if (s.empty())
287 return "";
288 std::string t = s.front();
289 s.pop_front();
290 return t;
291 }
292
293 mfxStatus ValidateAndSetProp(mfxI32 idx, mfxVariant value);
294 mfxStatus SetFilterPropertyDec(std::list<std::string> &propParsedString, mfxVariant value);
295 mfxStatus SetFilterPropertyEnc(std::list<std::string> &propParsedString, mfxVariant value);
296 mfxStatus SetFilterPropertyVPP(std::list<std::string> &propParsedString, mfxVariant value);
297
298 static mfxStatus GetFlatDescriptionsDec(const mfxImplDescription *libImplDesc,
299 std::list<DecConfig> &decConfigList);
300
301 static mfxStatus GetFlatDescriptionsEnc(const mfxImplDescription *libImplDesc,
302 std::list<EncConfig> &encConfigList);
303
304 static mfxStatus GetFlatDescriptionsVPP(const mfxImplDescription *libImplDesc,
305 std::list<VPPConfig> &vppConfigList);
306
307 static mfxStatus CheckPropsGeneral(const mfxVariant cfgPropsAll[],
308 const mfxImplDescription *libImplDesc);
309
310 static mfxStatus CheckPropsDec(const mfxVariant cfgPropsAll[],
311 std::list<DecConfig> decConfigList);
312
313 static mfxStatus CheckPropsEnc(const mfxVariant cfgPropsAll[],
314 std::list<EncConfig> encConfigList);
315
316 static mfxStatus CheckPropsVPP(const mfxVariant cfgPropsAll[],
317 std::list<VPPConfig> vppConfigList);
318
319 static mfxStatus CheckPropString(const mfxChar *implString, const std::string filtString);
320
321 #ifdef ONEVPL_EXPERIMENTAL
322 static mfxStatus CheckPropsExtDevID(const mfxVariant cfgPropsAll[],
323 const mfxExtendedDeviceId *libImplExtDevID);
324
325 #endif
326
327 mfxVariant m_propVar[NUM_TOTAL_FILTER_PROPS];
328
329 // special containers for properties which are passed by pointer
330 // (save a copy of the whole object based on property name)
331 mfxRange32U m_propRange32U[NUM_PROP_RANGES];
332 std::string m_implName;
333 std::string m_implLicense;
334 std::string m_implKeywords;
335 std::string m_deviceIdStr;
336 std::string m_implFunctionName;
337
338 mfxU8 m_extDevLUID8U[8];
339 std::string m_extDevNameStr;
340
341 std::vector<mfxU8> m_extBuf;
342
343 __inline bool SetExtBuf(mfxExtBuffer *extBuf) {
344 if (!extBuf)
345 return false;
346
347 mfxU32 BufferSz = extBuf->BufferSz;
348 if (BufferSz > 0) {
349 m_extBuf.resize(BufferSz);
350 std::copy((mfxU8 *)extBuf, (mfxU8 *)extBuf + BufferSz, m_extBuf.begin());
351 return true;
352 }
353 return false;
354 }
355
356 __inline bool GetExtBuf(mfxExtBuffer **extBuf) {
357 if (!extBuf)
358 return false;
359
360 *extBuf = nullptr;
361 if (!m_extBuf.empty()) {
362 *extBuf = (mfxExtBuffer *)m_extBuf.data();
363 return true;
364 }
365 return false;
366 }
367
368 __inline void ClearExtBuf() {
369 m_extBuf.clear();
370 }
371 };
372
373 // MSDK compatibility loader implementation
374 class LoaderCtxMSDK {
375 public:
376 LoaderCtxMSDK();
377 ~LoaderCtxMSDK();
378
379 // public function to be called by VPL dispatcher
380 // do not allocate any new memory here, so no need for a matching Release functions
381 mfxStatus QueryMSDKCaps(STRING_TYPE libNameFull,
382 mfxImplDescription **implDesc,
383 mfxImplementedFunctions **implFuncs,
384 mfxU32 adapterID,
385 bool bSkipD3D9Check);
386
387 static mfxStatus QueryAPIVersion(STRING_TYPE libNameFull, mfxVersion *msdkVersion);
388
389 #ifdef ONEVPL_EXPERIMENTAL
390 static mfxStatus QueryExtDeviceID(mfxExtendedDeviceId *extDeviceID,
391 mfxU32 adapterID,
392 mfxU16 deviceID,
393 mfxU64 luid);
394 #endif
395
396 // required by MFXCreateSession
397 mfxIMPL m_msdkAdapter;
398 mfxIMPL m_msdkAdapterD3D9;
399
400 mfxU16 m_deviceID;
401 mfxU64 m_luid;
402
403 #ifdef ONEVPL_EXPERIMENTAL
404 mfxExtendedDeviceId m_extDeviceID;
405 #endif
406
407 private:
408 // session management
409 mfxStatus OpenSession(mfxSession *session,
410 STRING_TYPE libNameFull,
411 mfxAccelerationMode accelMode,
412 mfxIMPL hwImpl);
413 void CloseSession(mfxSession *session);
414
415 // utility functions
416 static mfxAccelerationMode CvtAccelType(mfxIMPL implType, mfxIMPL implMethod);
417 static mfxStatus GetDefaultAccelType(mfxU32 adapterID, mfxIMPL *implDefault, mfxU64 *luid);
418 static mfxStatus CheckD3D9Support(mfxU64 luid, STRING_TYPE libNameFull, mfxIMPL *implD3D9);
419 static mfxStatus GetRenderNodeDescription(mfxU32 adapterID, mfxU32 &vendorID, mfxU16 &deviceID);
420
421 // internal state variables
422 STRING_TYPE m_libNameFull;
423 mfxImplDescription m_id; // base description struct
424 mfxAccelerationMode m_accelMode[MAX_MSDK_ACCEL_MODES];
425 mfxU16 m_loaderDeviceID;
426
427 __inline bool IsVersionSupported(mfxVersion reqVersion, mfxVersion actualVersion) {
428 if (actualVersion.Major > reqVersion.Major) {
429 return true;
430 }
431 else if ((actualVersion.Major == reqVersion.Major) &&
432 (actualVersion.Minor >= reqVersion.Minor)) {
433 return true;
434 }
435 return false;
436 }
437 };
438
439 struct LibInfo {
440 // during search store candidate file names
441 // and priority based on rules in spec
442 STRING_TYPE libNameFull;
443 mfxU32 libPriority;
444 LibType libType;
445
446 // if valid library, store file handle
447 // and table of exported functions
448 void *hModuleVPL;
449 VPLFunctionPtr vplFuncTable[NumVPLFunctions]; // NOLINT
450
451 // loader context for legacy MSDK
452 LoaderCtxMSDK msdkCtx[MAX_NUM_IMPL_MSDK];
453
454 // API version of legacy MSDK
455 mfxVersion msdkVersion;
456
457 // user-friendly version of path for MFX_IMPLCAPS_IMPLPATH query
458 mfxChar implCapsPath[MAX_VPL_SEARCH_PATH];
459
460 // avoid warnings
461 LibInfo()
462 : libNameFull(),
463 libPriority(0),
464 libType(LibTypeUnknown),
465 hModuleVPL(nullptr),
466 vplFuncTable(),
467 msdkCtx(),
468 msdkVersion(),
469 implCapsPath() {}
470
471 private:
472 // make this class non-copyable
473 LibInfo(const LibInfo &);
474 void operator=(const LibInfo &);
475 };
476
477 struct ImplInfo {
478 // library containing this implementation
479 LibInfo *libInfo;
480
481 // description of implementation
482 mfxHDL implDesc;
483
484 // list of implemented functions
485 mfxHDL implFuncs;
486
487 #ifdef ONEVPL_EXPERIMENTAL
488 mfxHDL implExtDeviceID;
489 #endif
490
491 // used for session initialization with this implementation
492 mfxInitializationParam vplParam;
493 mfxVersion version;
494
495 // if MSDK library, index of corresponding adapter (i.e. which LoaderCtxMSDK)
496 mfxU32 msdkImplIdx;
497
498 // adapter index in multi-adapter systems
499 mfxU32 adapterIdx;
500
501 // local index for libraries with more than one implementation
502 mfxU32 libImplIdx;
503
504 // index of valid libraries - updates with every call to MFXSetConfigFilterProperty()
505 mfxI32 validImplIdx;
506
507 // avoid warnings
508 ImplInfo()
509 : libInfo(nullptr),
510 implDesc(nullptr),
511 implFuncs(nullptr),
512 #ifdef ONEVPL_EXPERIMENTAL
513 implExtDeviceID(nullptr),
514 #endif
515 vplParam(),
516 version(),
517 msdkImplIdx(0),
518 adapterIdx(ADAPTER_IDX_UNKNOWN),
519 libImplIdx(0),
520 validImplIdx(-1) {
521 }
522 };
523
524 // loader class implementation
525 class LoaderCtxVPL {
526 public:
527 LoaderCtxVPL();
528 ~LoaderCtxVPL();
529
530 // manage library implementations
531 mfxStatus BuildListOfCandidateLibs();
532 mfxU32 CheckValidLibraries();
533 mfxStatus QueryLibraryCaps();
534 mfxStatus UnloadAllLibraries();
535
536 // query capabilities of each implementation
537 mfxStatus FullLoadAndQuery();
538 mfxStatus QueryImpl(mfxU32 idx, mfxImplCapsDeliveryFormat format, mfxHDL *idesc);
539 mfxStatus ReleaseImpl(mfxHDL idesc);
540
541 // update list of valid implementations based on current filter props
542 mfxStatus UpdateValidImplList(void);
543 mfxStatus PrioritizeImplList(void);
544
545 // create mfxSession
546 mfxStatus CreateSession(mfxU32 idx, mfxSession *session);
547
548 // manage configuration filters
549 ConfigCtxVPL *AddConfigFilter();
550 mfxStatus FreeConfigFilters();
551
552 // manage logging
553 mfxStatus InitDispatcherLog();
554 DispatcherLogVPL *GetLogger();
555
556 // low latency initialization
557 mfxStatus LoadLibsLowLatency();
558 mfxStatus UpdateLowLatency();
559
560 bool m_bLowLatency;
561 bool m_bNeedUpdateValidImpls;
562 bool m_bNeedFullQuery;
563 bool m_bNeedLowLatencyQuery;
564 bool m_bPriorityPathEnabled;
565
566 private:
567 // helper functions
568 mfxStatus LoadSingleLibrary(LibInfo *libInfo);
569 mfxStatus UnloadSingleLibrary(LibInfo *libInfo);
570 mfxStatus UnloadSingleImplementation(ImplInfo *implInfo);
571 VPLFunctionPtr GetFunctionAddr(void *hModuleVPL, const char *pName);
572
573 mfxU32 GetSearchPathsDriverStore(std::list<STRING_TYPE> &searchDirs, LibType libType);
574 mfxU32 GetSearchPathsSystemDefault(std::list<STRING_TYPE> &searchDirs);
575 mfxU32 GetSearchPathsCurrentExe(std::list<STRING_TYPE> &searchDirs);
576 mfxU32 GetSearchPathsCurrentDir(std::list<STRING_TYPE> &searchDirs);
577 mfxU32 GetSearchPathsLegacy(std::list<STRING_TYPE> &searchDirs);
578
579 mfxU32 ParseEnvSearchPaths(const CHAR_TYPE *envVarName, std::list<STRING_TYPE> &searchDirs);
580 mfxU32 ParseLegacySearchPaths(std::list<STRING_TYPE> &searchDirs);
581
582 mfxStatus SearchDirForLibs(STRING_TYPE searchDir,
583 std::list<LibInfo *> &libInfoList,
584 mfxU32 priority,
585 bool bLoadVPLOnly = false);
586
587 mfxU32 LoadAPIExports(LibInfo *libInfo, LibType libType);
588 mfxStatus ValidateAPIExports(VPLFunctionPtr *vplFuncTable, mfxVersion reportedVersion);
589 bool IsValidX86GPU(ImplInfo *implInfo, mfxU32 &deviceID, mfxU32 &adapterIdx);
590 mfxStatus UpdateImplPath(LibInfo *libInfo);
591
592 mfxStatus LoadLibsFromDriverStore(mfxU32 numAdapters,
593 const std::vector<DXGI1DeviceInfo> &adapterInfo,
594 LibType libType);
595 mfxStatus LoadLibsFromSystemDir(LibType libType);
596 mfxStatus LoadLibsFromMultipleDirs(LibType libType);
597
598 LibInfo *AddSingleLibrary(STRING_TYPE libPath, LibType libType);
599 mfxStatus QuerySessionLowLatency(LibInfo *libInfo, mfxU32 adapterID, mfxVersion *ver);
600
601 std::list<LibInfo *> m_libInfoList;
602 std::list<ImplInfo *> m_implInfoList;
603 std::list<ConfigCtxVPL *> m_configCtxList;
604 std::vector<DXGI1DeviceInfo> m_gpuAdapterInfo;
605
606 SpecialConfig m_specialConfig;
607
608 mfxU32 m_implIdxNext;
609 bool m_bKeepCapsUntilUnload;
610 CHAR_TYPE m_envVar[MAX_ENV_VAR_LEN];
611
612 // logger object - enabled with ONEVPL_DISPATCHER_LOG environment variable
613 DispatcherLogVPL m_dispLog;
614 };
615
616 #endif // DISPATCHER_VPL_MFX_DISPATCHER_VPL_H_