common_utils_windows.cpp 3.9 KB

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