Debian Codegraph @

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

raw
1 /*############################################################################
2 # Copyright (C) Intel Corporation
3 #
4 # SPDX-License-Identifier: MIT
5 ############################################################################*/
6
7 #include <fstream>
8 #include "vpl/mfx_dispatcher_vpl.h"
9
10 #if defined(_WIN32) || defined(_WIN64)
11 #include "vpl/mfx_dispatcher_vpl_win.h"
12 #endif
13
14 #ifdef __linux__
15 #include <pthread.h>
16 #define strncpy_s(dst, size, src, cnt) strncpy((dst), (src), (cnt)) // NOLINT
17 #endif
18
19 // leave table formatting alone
20 // clang-format off
21
22 static const mfxChar strImplName[MFX_IMPL_NAME_LEN] = "mfxhw64";
23 static const mfxChar strLicense[MFX_STRFIELD_LEN] = "";
24
25 #if defined _M_IX86
26 static const mfxChar strKeywords[MFX_STRFIELD_LEN] = "MSDK,x86";
27 #else
28 static const mfxChar strKeywords[MFX_STRFIELD_LEN] = "MSDK,x64";
29 #endif
30
31 // also used in main loader routine
32 const mfxIMPL msdkImplTab[MAX_NUM_IMPL_MSDK] = {
33 MFX_IMPL_HARDWARE,
34 MFX_IMPL_HARDWARE2,
35 MFX_IMPL_HARDWARE3,
36 MFX_IMPL_HARDWARE4,
37 };
38
39 // not relevant for 1.x runtimes (no internal memory management)
40 #define NUM_POOL_POLICIES_MSDK 0
41
42 static const mfxPoolPolicyDescription PoolPolicies = {
43 {{ 0, 1 }}, // struct Version
44 {}, // reserved
45 NUM_POOL_POLICIES_MSDK, // NumPoolPolicies
46 nullptr,
47 };
48
49 // 1.x function names should match list in enum eFunc
50 static const mfxChar* msdkImplFuncsNames[] = {
51 "MFXInit",
52 "MFXClose",
53 "MFXQueryIMPL",
54 "MFXQueryVersion",
55 "MFXJoinSession",
56 "MFXDisjoinSession",
57 "MFXCloneSession",
58 "MFXSetPriority",
59 "MFXGetPriority",
60 "MFXInitEx",
61 "MFXVideoCORE_SetFrameAllocator",
62 "MFXVideoCORE_SetHandle",
63 "MFXVideoCORE_GetHandle",
64 "MFXVideoCORE_SyncOperation",
65 "MFXVideoENCODE_Query",
66 "MFXVideoENCODE_QueryIOSurf",
67 "MFXVideoENCODE_Init",
68 "MFXVideoENCODE_Reset",
69 "MFXVideoENCODE_Close",
70 "MFXVideoENCODE_GetVideoParam",
71 "MFXVideoENCODE_GetEncodeStat",
72 "MFXVideoENCODE_EncodeFrameAsync",
73 "MFXVideoDECODE_Query",
74 "MFXVideoDECODE_DecodeHeader",
75 "MFXVideoDECODE_QueryIOSurf",
76 "MFXVideoDECODE_Init",
77 "MFXVideoDECODE_Reset",
78 "MFXVideoDECODE_Close",
79 "MFXVideoDECODE_GetVideoParam",
80 "MFXVideoDECODE_GetDecodeStat",
81 "MFXVideoDECODE_SetSkipMode",
82 "MFXVideoDECODE_GetPayload",
83 "MFXVideoDECODE_DecodeFrameAsync",
84 "MFXVideoVPP_Query",
85 "MFXVideoVPP_QueryIOSurf",
86 "MFXVideoVPP_Init",
87 "MFXVideoVPP_Reset",
88 "MFXVideoVPP_Close",
89 "MFXVideoVPP_GetVideoParam",
90 "MFXVideoVPP_GetVPPStat",
91 "MFXVideoVPP_RunFrameVPPAsync",
92 "MFXVideoCORE_QueryPlatform",
93 };
94
95 static const mfxImplementedFunctions msdkImplFuncs = {
96 sizeof(msdkImplFuncsNames) / sizeof(mfxChar*),
97 (mfxChar**)msdkImplFuncsNames
98 };
99
100 // optional extBuf to limit threads created in MSDK session creation
101 // to enable, set vplParam.NumExtParam and vplParam.ExtParam before calling MFXInitEx2()
102 static const mfxExtThreadsParam extThreadParam = {
103 {MFX_EXTBUFF_THREADS_PARAM, sizeof(mfxExtThreadsParam)},
104 2,
105 0,
106 0,
107 {},
108 };
109
110 static const mfxExtBuffer* extParams[1] = {
111 (mfxExtBuffer *)&extThreadParam,
112 };
113
114 // end table formatting
115 // clang-format on
116
117 LoaderCtxMSDK::LoaderCtxMSDK()
118 : m_msdkAdapter(),
119 m_msdkAdapterD3D9(),
120 m_deviceID(0),
121 m_luid(0),
122 #ifdef ONEVPL_EXPERIMENTAL
123 m_extDeviceID(),
124 #endif
125 m_libNameFull(),
126 m_id(),
127 m_accelMode(),
128 m_loaderDeviceID(0) {
129 }
130
131 LoaderCtxMSDK::~LoaderCtxMSDK() {}
132
133 mfxStatus LoaderCtxMSDK::OpenSession(mfxSession *session,
134 STRING_TYPE libNameFull,
135 mfxAccelerationMode accelMode,
136 mfxIMPL hwImpl) {
137 // require API 1.0 or later (both MFXInit and MFXInitEx supported)
138 mfxVersion reqVersion;
139 reqVersion.Major = MSDK_MIN_VERSION_MAJOR;
140 reqVersion.Minor = MSDK_MIN_VERSION_MINOR;
141
142 // set acceleration mode - will be mapped to 1.x API
143 mfxInitializationParam vplParam = {};
144 vplParam.AccelerationMode = accelMode;
145
146 #ifdef __linux__
147 vplParam.ExtParam = (mfxExtBuffer **)&extParams;
148 vplParam.NumExtParam = 1;
149 #else
150 vplParam.ExtParam = nullptr;
151 vplParam.NumExtParam = 0;
152 #endif
153
154 return MFXInitEx2(reqVersion,
155 vplParam,
156 hwImpl,
157 session,
158 &m_loaderDeviceID,
159 (CHAR_TYPE *)libNameFull.c_str());
160 }
161
162 // safe to call more than once (sets/checks for null session)
163 void LoaderCtxMSDK::CloseSession(mfxSession *session) {
164 if (*session)
165 MFXClose(*session);
166
167 *session = nullptr;
168 }
169
170 // map mfxIMPL (1.x) to mfxAccelerationMode (2.x)
171 mfxAccelerationMode LoaderCtxMSDK::CvtAccelType(mfxIMPL implType, mfxIMPL implMethod) {
172 if (implType == MFX_IMPL_HARDWARE) {
173 switch (implMethod) {
174 case MFX_IMPL_VIA_D3D9:
175 return MFX_ACCEL_MODE_VIA_D3D9;
176 case MFX_IMPL_VIA_D3D11:
177 return MFX_ACCEL_MODE_VIA_D3D11;
178 case MFX_IMPL_VIA_VAAPI:
179 return MFX_ACCEL_MODE_VIA_VAAPI;
180 }
181 }
182
183 return MFX_ACCEL_MODE_NA;
184 }
185
186 mfxStatus LoaderCtxMSDK::GetDefaultAccelType(mfxU32 adapterID, mfxIMPL *implDefault, mfxU64 *luid) {
187 #ifdef __linux__
188 // VAAPI only
189 *implDefault = MFX_IMPL_VIA_VAAPI;
190 *luid = 0;
191 return MFX_ERR_NONE;
192 #else
193 // Windows - D3D11 only
194 mfxU32 VendorID = 0, DeviceID = 0;
195 mfxIMPL implTest;
196 mfxStatus sts;
197
198 // check whether adapterID supports D3D11 and has correct VendorID
199 implTest = MFX_IMPL_VIA_D3D11;
200 sts = MFX::SelectImplementationType(adapterID, &implTest, &VendorID, &DeviceID, luid);
201
202 if (sts != MFX_ERR_NONE || VendorID != 0x8086) {
203 implTest = MFX_IMPL_UNSUPPORTED;
204 return MFX_ERR_UNSUPPORTED;
205 }
206
207 *implDefault = implTest;
208
209 return MFX_ERR_NONE;
210 #endif
211 }
212
213 mfxStatus LoaderCtxMSDK::QueryAPIVersion(STRING_TYPE libNameFull, mfxVersion *msdkVersion) {
214 mfxStatus sts;
215 mfxSession session = nullptr;
216
217 mfxVersion reqVersion;
218 reqVersion.Major = MSDK_MIN_VERSION_MAJOR;
219 reqVersion.Minor = MSDK_MIN_VERSION_MINOR;
220
221 // try creating a session with each adapter in order to get MSDK API version
222 // stop with first successful session creation
223 for (mfxU32 adapterID = 0; adapterID < MAX_NUM_IMPL_MSDK; adapterID++) {
224 // try HW session, default acceleration mode
225 mfxIMPL hwImpl = msdkImplTab[adapterID];
226 mfxIMPL implDefault = MFX_IMPL_UNSUPPORTED;
227 mfxU64 luid;
228
229 // if not a valid HW device, try next adapter
230 sts = GetDefaultAccelType(adapterID, &implDefault, &luid);
231 if (sts != MFX_ERR_NONE)
232 continue;
233
234 // set acceleration mode - will be mapped to 1.x API
235 mfxInitializationParam vplParam = {};
236 vplParam.AccelerationMode =
237 (mfxAccelerationMode)CvtAccelType(MFX_IMPL_HARDWARE, implDefault & 0xFF00);
238
239 #ifdef __linux__
240 vplParam.ExtParam = (mfxExtBuffer **)&extParams;
241 vplParam.NumExtParam = 1;
242 #else
243 vplParam.ExtParam = nullptr;
244 vplParam.NumExtParam = 0;
245 #endif
246
247 mfxU16 deviceID;
248 sts = MFXInitEx2(reqVersion,
249 vplParam,
250 hwImpl,
251 &session,
252 &deviceID,
253 (CHAR_TYPE *)libNameFull.c_str());
254
255 if (sts == MFX_ERR_NONE) {
256 sts = MFXQueryVersion(session, msdkVersion);
257 MFXClose(session);
258
259 if (sts == MFX_ERR_NONE)
260 return sts;
261 }
262 }
263
264 return MFX_ERR_UNSUPPORTED;
265 }
266
267 mfxStatus LoaderCtxMSDK::QueryMSDKCaps(STRING_TYPE libNameFull,
268 mfxImplDescription **implDesc,
269 mfxImplementedFunctions **implFuncs,
270 mfxU32 adapterID,
271 bool bSkipD3D9Check) {
272 #ifdef DISABLE_MSDK_COMPAT
273 // disable support for legacy MSDK
274 return MFX_ERR_UNSUPPORTED;
275 #endif
276
277 mfxStatus sts;
278 mfxSession session = nullptr;
279
280 m_libNameFull = libNameFull;
281 m_deviceID = 0;
282
283 #ifdef __linux__
284 // require pthreads to be linked in for MSDK RT to load
285 pthread_key_t pkey;
286 if (pthread_key_create(&pkey, NULL) == 0) {
287 pthread_key_delete(pkey);
288 }
289
290 mfxU32 vendorID = 0;
291 mfxU16 deviceID = 0;
292 sts = GetRenderNodeDescription(adapterID, vendorID, deviceID);
293 if (sts != MFX_ERR_NONE)
294 return MFX_ERR_UNSUPPORTED;
295
296 // on Linux read deviceID from the render node path
297 m_deviceID = deviceID;
298 #endif
299
300 // try HW session, default acceleration mode
301 mfxIMPL hwImpl = msdkImplTab[adapterID];
302 mfxIMPL implDefault = MFX_IMPL_UNSUPPORTED;
303
304 sts = GetDefaultAccelType(adapterID, &implDefault, &m_luid);
305 if (sts != MFX_ERR_NONE)
306 return MFX_ERR_UNSUPPORTED;
307
308 sts = OpenSession(&session,
309 m_libNameFull,
310 (mfxAccelerationMode)CvtAccelType(MFX_IMPL_HARDWARE, implDefault & 0xFF00),
311 hwImpl);
312
313 // adapter unsupported
314 if (sts != MFX_ERR_NONE)
315 return MFX_ERR_UNSUPPORTED;
316
317 // return list of implemented functions
318 *implFuncs = (mfxImplementedFunctions *)(&msdkImplFuncs);
319
320 // clear new 2.0 style description struct
321 memset(&m_id, 0, sizeof(mfxImplDescription));
322 *implDesc = &m_id;
323
324 // fill in top-level capabilities
325 m_id.Version.Version = MFX_IMPLDESCRIPTION_VERSION;
326 m_id.Impl = MFX_IMPL_TYPE_HARDWARE;
327
328 // query API version
329 sts = MFXQueryVersion(session, &m_id.ApiVersion);
330 if (sts != MFX_ERR_NONE) {
331 CloseSession(&session);
332 return sts;
333 }
334
335 // set default acceleration mode
336 m_id.AccelerationMode = CvtAccelType(MFX_IMPL_HARDWARE, implDefault & 0xFF00);
337
338 // fill in acceleration description struct
339 mfxAccelerationModeDescription *accelDesc = &(m_id.AccelerationModeDescription);
340 accelDesc->Version.Version = MFX_ACCELERATIONMODESCRIPTION_VERSION;
341
342 // fill in mode description with just the single (default) mode
343 accelDesc->NumAccelerationModes = 1;
344 accelDesc->Mode = m_accelMode;
345 accelDesc->Mode[0] = m_id.AccelerationMode;
346
347 // return HW accelerator - required by MFXCreateSession
348 m_msdkAdapter = hwImpl;
349
350 // map MFX HW number to VendorImplID
351 m_id.VendorImplID = 0;
352 switch (hwImpl) {
353 case MFX_IMPL_HARDWARE:
354 m_id.VendorImplID = 0;
355 break;
356 case MFX_IMPL_HARDWARE2:
357 m_id.VendorImplID = 1;
358 break;
359 case MFX_IMPL_HARDWARE3:
360 m_id.VendorImplID = 2;
361 break;
362 case MFX_IMPL_HARDWARE4:
363 m_id.VendorImplID = 3;
364 break;
365 }
366
367 // fill in strings
368 strncpy_s(m_id.ImplName, sizeof(m_id.ImplName), strImplName, sizeof(strImplName));
369 m_id.ImplName[sizeof(m_id.ImplName) - 1] = 0;
370 strncpy_s(m_id.License, sizeof(m_id.License), strLicense, sizeof(strLicense));
371 m_id.License[sizeof(m_id.License) - 1] = 0;
372 strncpy_s(m_id.Keywords, sizeof(m_id.Keywords), strKeywords, sizeof(strKeywords));
373 m_id.Keywords[sizeof(m_id.Keywords) - 1] = 0;
374
375 m_id.VendorID = 0x8086;
376 m_id.NumExtParam = 0;
377
378 // fill in pool policies
379 m_id.PoolPolicies = PoolPolicies;
380
381 // fill in device description
382 mfxDeviceDescription *Dev = &(m_id.Dev);
383 memset(Dev, 0, sizeof(mfxDeviceDescription)); // initially empty
384 Dev->MediaAdapterType = MFX_MEDIA_UNKNOWN;
385
386 // query for underlying deviceID (requires API >= 1.19)
387 // for Linux, we may already have the deviceID from parsing render node path earlier
388 if (m_deviceID == 0) {
389 if (IsVersionSupported(MAKE_MFX_VERSION(1, 19), m_id.ApiVersion)) {
390 mfxPlatform platform = {};
391
392 sts = MFXVideoCORE_QueryPlatform(session, &platform);
393 if (sts == MFX_ERR_NONE)
394 m_deviceID = platform.DeviceId;
395
396 // mfxPlatform::MediaAdapterType was added in API 1.31
397 if (IsVersionSupported(MAKE_MFX_VERSION(1, 31), m_id.ApiVersion)) {
398 Dev->MediaAdapterType = platform.MediaAdapterType;
399 }
400 }
401 }
402
403 // if QueryPlatform did not return deviceID, we may have received
404 // it from the loader (MFXInitEx2)
405 if (m_deviceID == 0)
406 m_deviceID = m_loaderDeviceID;
407
408 // store DeviceID as "DevID" (hex) / "AdapterIdx" (dec) to match GPU RT
409 Dev->Version.Version = MFX_DEVICEDESCRIPTION_VERSION;
410 snprintf(Dev->DeviceID, sizeof(Dev->DeviceID), "%x/%d", m_deviceID, m_id.VendorImplID);
411 Dev->NumSubDevices = 0;
412
413 CloseSession(&session);
414
415 #if defined(_WIN32) || defined(_WIN64)
416 if (bSkipD3D9Check == false) {
417 mfxIMPL implD3D9;
418 m_msdkAdapterD3D9 = MFX_IMPL_UNSUPPORTED;
419
420 sts = CheckD3D9Support(m_luid, libNameFull, &implD3D9);
421 if (sts == MFX_ERR_NONE) {
422 m_msdkAdapterD3D9 = implD3D9;
423
424 accelDesc->Mode[accelDesc->NumAccelerationModes] = MFX_ACCEL_MODE_VIA_D3D9;
425 accelDesc->NumAccelerationModes++;
426 }
427 }
428 #endif
429
430 return MFX_ERR_NONE;
431 }
432
433 mfxStatus LoaderCtxMSDK::CheckD3D9Support(mfxU64 luid, STRING_TYPE libNameFull, mfxIMPL *implD3D9) {
434 #if defined(_WIN32) || defined(_WIN64)
435 mfxU32 VendorID = 0, DeviceID = 0;
436 mfxIMPL implTest = MFX_IMPL_VIA_D3D9;
437
438 mfxStatus sts;
439 mfxSession session = nullptr;
440
441 mfxVersion reqVersion;
442 reqVersion.Major = MSDK_MIN_VERSION_MAJOR;
443 reqVersion.Minor = MSDK_MIN_VERSION_MINOR;
444
445 *implD3D9 = MFX_IMPL_UNSUPPORTED;
446
447 mfxU32 idx;
448 for (idx = 0; idx < MAX_NUM_IMPL_MSDK; idx++) {
449 mfxU64 luidD3D9 = 0;
450 sts = MFX::SelectImplementationType(idx, &implTest, &VendorID, &DeviceID, &luidD3D9);
451
452 if (sts != MFX_ERR_NONE || VendorID != 0x8086 || luid != luidD3D9)
453 continue;
454
455 // matching LUID - try creating a D3D9 session
456 mfxInitializationParam vplParam = {};
457 vplParam.AccelerationMode = MFX_ACCEL_MODE_VIA_D3D9;
458
459 // thread limit not enabled on Windows
460 vplParam.ExtParam = nullptr;
461 vplParam.NumExtParam = 0;
462
463 mfxU16 deviceID;
464 sts = MFXInitEx2(reqVersion,
465 vplParam,
466 msdkImplTab[idx],
467 &session,
468 &deviceID,
469 (CHAR_TYPE *)libNameFull.c_str());
470
471 if (sts == MFX_ERR_NONE) {
472 *implD3D9 = msdkImplTab[idx];
473 MFXClose(session);
474 return MFX_ERR_NONE;
475 }
476
477 break; // D3D9 not supported
478 }
479
480 // this adapter (input luid) does not support D3D9
481 return MFX_ERR_UNSUPPORTED;
482 #else
483 return MFX_ERR_UNSUPPORTED;
484 #endif
485 }
486
487 mfxU32 read_device_file(std::string &path) {
488 mfxU32 result = 0;
489 std::string line;
490 std::ifstream dev_str(path);
491 if (!dev_str.is_open()) {
492 return 0;
493 }
494 std::getline(dev_str, line);
495 dev_str.close();
496 try {
497 result = std::stoul(line, 0, 16);
498 }
499 catch (std::invalid_argument &) {
500 return 0;
501 }
502 catch (std::out_of_range &) {
503 return 0;
504 }
505 return result;
506 }
507
508 mfxStatus LoaderCtxMSDK::GetRenderNodeDescription(mfxU32 adapterID,
509 mfxU32 &vendorID,
510 mfxU16 &deviceID) {
511 vendorID = 0;
512 deviceID = 0;
513
514 #if defined(__linux__)
515 mfxU32 DRMRenderNodeNum = 128 + adapterID;
516 std::string nodeStr = std::to_string(DRMRenderNodeNum);
517
518 std::string vendorPath = "/sys/class/drm/renderD" + nodeStr + "/device/vendor";
519 std::string devPath = "/sys/class/drm/renderD" + nodeStr + "/device/device";
520
521 vendorID = read_device_file(vendorPath);
522
523 if (vendorID != 0x8086)
524 return MFX_ERR_UNSUPPORTED;
525
526 deviceID = read_device_file(devPath);
527
528 if (deviceID == 0)
529 return MFX_ERR_UNSUPPORTED;
530
531 return MFX_ERR_NONE;
532 #else
533 return MFX_ERR_UNSUPPORTED;
534 #endif
535 }
536
537 // avoid confusing #ifdef indentation
538 // clang-format off
539
540 #ifdef ONEVPL_EXPERIMENTAL
541
542 mfxStatus LoaderCtxMSDK::QueryExtDeviceID(mfxExtendedDeviceId *extDeviceID,
543 mfxU32 adapterID,
544 mfxU16 deviceID,
545 mfxU64 luid) {
546 // fill extended device ID struct (API >= 2.6)
547 memset(extDeviceID, 0, sizeof(mfxExtendedDeviceId));
548
549 // common properties
550 extDeviceID->Version.Version = MFX_EXTENDEDDEVICEID_VERSION;
551 extDeviceID->VendorID = 0x8086;
552 extDeviceID->DeviceID = deviceID;
553
554 // default - no PCI info
555 // additional dependencies required to obtain these props
556 extDeviceID->PCIDomain = 0xFFFFFFFF;
557 extDeviceID->PCIBus = 0xFFFFFFFF;
558 extDeviceID->PCIDevice = 0xFFFFFFFF;
559 extDeviceID->PCIFunction = 0xFFFFFFFF;
560
561 // default - no LUID
562 extDeviceID->LUIDDeviceNodeMask = 0;
563 extDeviceID->LUIDValid = 0;
564 for (mfxU32 idx = 0; idx < 8; idx++)
565 extDeviceID->DeviceLUID[idx] = 0;
566
567 // default - no DRM node
568 extDeviceID->DRMRenderNodeNum = 0;
569 extDeviceID->DRMPrimaryNodeNum = 0x7FFFFFFF;
570
571 // default - no PCI RevisionID
572 extDeviceID->RevisionID = 0xFFFF;
573
574 snprintf(extDeviceID->DeviceName, sizeof(extDeviceID->DeviceName), "%s", strImplName);
575
576 #if defined(_WIN32) || defined(_WIN64)
577 // fill in Windows-specific properties
578 if (luid) {
579 extDeviceID->LUIDDeviceNodeMask = 1;
580 extDeviceID->LUIDValid = 1;
581
582 // map 64-bit LUID into mfxU8[8]
583 mfxU64 luidArr = luid;
584 for (mfxU32 idx = 0; idx < 8; idx++) {
585 extDeviceID->DeviceLUID[idx] = mfxU8((luidArr)&0xFF);
586 luidArr >>= 8;
587 }
588 }
589 #elif defined(__linux__)
590 extDeviceID->DRMPrimaryNodeNum = adapterID;
591 extDeviceID->DRMRenderNodeNum = 128 + adapterID;
592 #endif
593
594 return MFX_ERR_NONE;
595 }
596 #endif // ONEVPL_EXPERIMENTAL
597
598 // clang-format on