common_utils.h 5.7 KB

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