common_utils_windows.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #include "common_utils.h"
  2. // ATTENTION: If D3D surfaces are used, DX9_D3D or DX11_D3D must be set in project settings or hardcoded here
  3. #ifdef DX9_D3D
  4. #include "common_directx.h"
  5. #elif DX11_D3D
  6. #include "common_directx11.h"
  7. #include "common_directx9.h"
  8. #endif
  9. #include <intrin.h>
  10. /* =======================================================
  11. * Windows implementation of OS-specific utility functions
  12. */
  13. mfxStatus Initialize(mfxIMPL impl, mfxVersion ver, MFXVideoSession *pSession,
  14. mfxFrameAllocator *pmfxAllocator, mfxHDL *deviceHandle,
  15. bool bCreateSharedHandles, bool dx9hack)
  16. {
  17. bCreateSharedHandles; // (Lain) Currently unused
  18. pmfxAllocator; // (Lain) Currently unused
  19. mfxStatus sts = MFX_ERR_NONE;
  20. // If mfxFrameAllocator is provided it means we need to setup DirectX device and memory allocator
  21. if (pmfxAllocator && !dx9hack) {
  22. // Initialize Intel Media SDK Session
  23. sts = pSession->Init(impl, &ver);
  24. MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
  25. // Create DirectX device context
  26. if (deviceHandle == NULL || *deviceHandle == NULL) {
  27. sts = CreateHWDevice(*pSession, deviceHandle, NULL,
  28. bCreateSharedHandles);
  29. MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
  30. }
  31. if (deviceHandle == NULL || *deviceHandle == NULL)
  32. return MFX_ERR_DEVICE_FAILED;
  33. // Provide device manager to Media SDK
  34. sts = pSession->SetHandle(DEVICE_MGR_TYPE, *deviceHandle);
  35. MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
  36. pmfxAllocator->pthis =
  37. *pSession; // We use Media SDK session ID as the allocation identifier
  38. pmfxAllocator->Alloc = simple_alloc;
  39. pmfxAllocator->Free = simple_free;
  40. pmfxAllocator->Lock = simple_lock;
  41. pmfxAllocator->Unlock = simple_unlock;
  42. pmfxAllocator->GetHDL = simple_gethdl;
  43. // Since we are using video memory we must provide Media SDK with an external allocator
  44. sts = pSession->SetFrameAllocator(pmfxAllocator);
  45. MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
  46. } else if (pmfxAllocator && dx9hack) {
  47. // Initialize Intel Media SDK Session
  48. sts = pSession->Init(impl, &ver);
  49. MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
  50. // Create DirectX device context
  51. if (deviceHandle == NULL || *deviceHandle == NULL) {
  52. sts = DX9_CreateHWDevice(*pSession, deviceHandle, NULL,
  53. false);
  54. MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
  55. }
  56. if (*deviceHandle == NULL)
  57. return MFX_ERR_DEVICE_FAILED;
  58. // Provide device manager to Media SDK
  59. sts = pSession->SetHandle(MFX_HANDLE_D3D9_DEVICE_MANAGER,
  60. *deviceHandle);
  61. MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
  62. pmfxAllocator->pthis =
  63. *pSession; // We use Media SDK session ID as the allocation identifier
  64. pmfxAllocator->Alloc = dx9_simple_alloc;
  65. pmfxAllocator->Free = dx9_simple_free;
  66. pmfxAllocator->Lock = dx9_simple_lock;
  67. pmfxAllocator->Unlock = dx9_simple_unlock;
  68. pmfxAllocator->GetHDL = dx9_simple_gethdl;
  69. // Since we are using video memory we must provide Media SDK with an external allocator
  70. sts = pSession->SetFrameAllocator(pmfxAllocator);
  71. MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
  72. } else {
  73. // Initialize Intel Media SDK Session
  74. sts = pSession->Init(impl, &ver);
  75. MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
  76. }
  77. return sts;
  78. }
  79. void Release()
  80. {
  81. #if defined(DX9_D3D) || defined(DX11_D3D)
  82. CleanupHWDevice();
  83. DX9_CleanupHWDevice();
  84. #endif
  85. }
  86. void mfxGetTime(mfxTime *timestamp)
  87. {
  88. QueryPerformanceCounter(timestamp);
  89. }
  90. double TimeDiffMsec(mfxTime tfinish, mfxTime tstart)
  91. {
  92. static LARGE_INTEGER tFreq = {0};
  93. if (!tFreq.QuadPart)
  94. QueryPerformanceFrequency(&tFreq);
  95. double freq = (double)tFreq.QuadPart;
  96. return 1000.0 * ((double)tfinish.QuadPart - (double)tstart.QuadPart) /
  97. freq;
  98. }
  99. void util_cpuid(int cpuinfo[4], int flags)
  100. {
  101. return __cpuid(cpuinfo, flags);
  102. }
  103. /* (Lain) Functions currently unused */
  104. #if 0
  105. void ClearYUVSurfaceVMem(mfxMemId memId)
  106. {
  107. #if defined(DX9_D3D) || defined(DX11_D3D)
  108. ClearYUVSurfaceD3D(memId);
  109. #endif
  110. }
  111. void ClearRGBSurfaceVMem(mfxMemId memId)
  112. {
  113. #if defined(DX9_D3D) || defined(DX11_D3D)
  114. ClearRGBSurfaceD3D(memId);
  115. #endif
  116. }
  117. #endif