common_utils.h 5.6 KB

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