common_utils_windows.cpp 4.6 KB

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