common_utils.h 5.0 KB

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