common_utils_windows.cpp 4.3 KB

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