common_utils.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. #pragma once
  9. #include <stdio.h>
  10. #include "mfxvideo++.h"
  11. // =================================================================
  12. // OS-specific definitions of types, macro, etc...
  13. // The following should be defined:
  14. // - mfxTime
  15. // - MSDK_FOPEN
  16. // - MSDK_SLEEP
  17. #if defined(_WIN32) || defined(_WIN64)
  18. #include "bits/windows_defs.h"
  19. #elif defined(__linux__)
  20. #include "bits/linux_defs.h"
  21. #endif
  22. // =================================================================
  23. // Helper macro definitions...
  24. #define MSDK_PRINT_RET_MSG(ERR) \
  25. { \
  26. PrintErrString(ERR, __FILE__, __LINE__); \
  27. }
  28. #define MSDK_CHECK_RESULT(P, X, ERR) \
  29. { \
  30. if ((X) > (P)) { \
  31. MSDK_PRINT_RET_MSG(ERR); \
  32. return ERR; \
  33. } \
  34. }
  35. #define MSDK_CHECK_POINTER(P, ERR) \
  36. { \
  37. if (!(P)) { \
  38. MSDK_PRINT_RET_MSG(ERR); \
  39. return ERR; \
  40. } \
  41. }
  42. #define MSDK_CHECK_ERROR(P, X, ERR) \
  43. { \
  44. if ((X) == (P)) { \
  45. MSDK_PRINT_RET_MSG(ERR); \
  46. return ERR; \
  47. } \
  48. }
  49. #define MSDK_IGNORE_MFX_STS(P, X) \
  50. { \
  51. if ((X) == (P)) { \
  52. P = MFX_ERR_NONE; \
  53. } \
  54. }
  55. #define MSDK_BREAK_ON_ERROR(P) \
  56. { \
  57. if (MFX_ERR_NONE != (P)) \
  58. break; \
  59. }
  60. #define MSDK_SAFE_DELETE_ARRAY(P) \
  61. { \
  62. if (P) { \
  63. delete[] P; \
  64. P = NULL; \
  65. } \
  66. }
  67. #define MSDK_ALIGN32(X) (((mfxU32)((X) + 31)) & (~(mfxU32)31))
  68. #define MSDK_ALIGN16(value) (((value + 15) >> 4) << 4)
  69. #define MSDK_SAFE_RELEASE(X) \
  70. { \
  71. if (X) { \
  72. X->Release(); \
  73. X = NULL; \
  74. } \
  75. }
  76. #define MSDK_MAX(A, B) (((A) > (B)) ? (A) : (B))
  77. // Usage of the following two macros are only required for certain Windows DirectX11 use cases
  78. #define WILL_READ 0x1000
  79. #define WILL_WRITE 0x2000
  80. // =================================================================
  81. // Intel Media SDK memory allocator entrypoints....
  82. // Implementation of this functions is OS/Memory type specific.
  83. mfxStatus simple_alloc(mfxHDL pthis, mfxFrameAllocRequest *request,
  84. mfxFrameAllocResponse *response);
  85. mfxStatus simple_lock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr);
  86. mfxStatus simple_unlock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr);
  87. mfxStatus simple_gethdl(mfxHDL pthis, mfxMemId mid, mfxHDL *handle);
  88. mfxStatus simple_free(mfxHDL pthis, mfxFrameAllocResponse *response);
  89. mfxStatus simple_copytex(mfxHDL pthis, mfxMemId mid, mfxU32 tex_handle,
  90. mfxU64 lock_key, mfxU64 *next_key);
  91. // =================================================================
  92. // Utility functions, not directly tied to Media SDK functionality
  93. //
  94. void PrintErrString(int err, const char *filestr, int line);
  95. // LoadRawFrame: Reads raw frame from YUV file (YV12) into NV12 surface
  96. // - YV12 is a more common format for YUV files than NV12 (therefore the conversion during read and write)
  97. // - For the simulation case (fSource = NULL), the surface is filled with default image data
  98. // LoadRawRGBFrame: Reads raw RGB32 frames from file into RGB32 surface
  99. // - For the simulation case (fSource = NULL), the surface is filled with default image data
  100. mfxStatus LoadRawFrame(mfxFrameSurface1 *pSurface, FILE *fSource);
  101. mfxStatus LoadRawRGBFrame(mfxFrameSurface1 *pSurface, FILE *fSource);
  102. // Write raw YUV (NV12) surface to YUV (YV12) file
  103. mfxStatus WriteRawFrame(mfxFrameSurface1 *pSurface, FILE *fSink);
  104. // Write bit stream data for frame to file
  105. mfxStatus WriteBitStreamFrame(mfxBitstream *pMfxBitstream, FILE *fSink);
  106. // Read bit stream data from file. Stream is read as large chunks (= many frames)
  107. mfxStatus ReadBitStreamData(mfxBitstream *pBS, FILE *fSource);
  108. void ClearYUVSurfaceSysMem(mfxFrameSurface1 *pSfc, mfxU16 width, mfxU16 height);
  109. void ClearYUVSurfaceVMem(mfxMemId memId);
  110. void ClearRGBSurfaceVMem(mfxMemId memId);
  111. // Get free raw frame surface
  112. int GetFreeSurfaceIndex(mfxFrameSurface1 **pSurfacesPool, mfxU16 nPoolSize);
  113. // For use with asynchronous task management
  114. typedef struct {
  115. mfxBitstream mfxBS;
  116. mfxSyncPoint syncp;
  117. } Task;
  118. // Get free task
  119. int GetFreeTaskIndex(Task *pTaskPool, mfxU16 nPoolSize);
  120. // Initialize Intel Media SDK Session, device/display and memory manager
  121. mfxStatus Initialize(mfxIMPL impl, mfxVersion ver, MFXVideoSession *pSession,
  122. mfxFrameAllocator *pmfxAllocator,
  123. mfxHDL *deviceHandle = NULL,
  124. bool bCreateSharedHandles = false, bool dx9hack = false);
  125. // Release resources (device/display)
  126. void Release();
  127. // Convert frame type to string
  128. char mfxFrameTypeString(mfxU16 FrameType);
  129. void mfxGetTime(mfxTime *timestamp);
  130. //void mfxInitTime(); might need this for Windows
  131. double TimeDiffMsec(mfxTime tfinish, mfxTime tstart);