1
0

common_utils.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. #include "common_utils.h"
  2. // =================================================================
  3. // Utility functions, not directly tied to Intel Media SDK functionality
  4. //
  5. struct adapter_info adapters[MAX_ADAPTERS] = {0};
  6. size_t adapter_count = 0;
  7. size_t adapter_index = 0;
  8. void PrintErrString(int err, const char *filestr, int line)
  9. {
  10. switch (err) {
  11. case 0:
  12. printf("\n No error.\n");
  13. break;
  14. case -1:
  15. printf("\n Unknown error: %s %d\n", filestr, line);
  16. break;
  17. case -2:
  18. printf("\n Null pointer. Check filename/path + permissions? %s %d\n", filestr, line);
  19. break;
  20. case -3:
  21. printf("\n Unsupported feature/library load error. %s %d\n", filestr, line);
  22. break;
  23. case -4:
  24. printf("\n Could not allocate memory. %s %d\n", filestr, line);
  25. break;
  26. case -5:
  27. printf("\n Insufficient IO buffers. %s %d\n", filestr, line);
  28. break;
  29. case -6:
  30. printf("\n Invalid handle. %s %d\n", filestr, line);
  31. break;
  32. case -7:
  33. printf("\n Memory lock failure. %s %d\n", filestr, line);
  34. break;
  35. case -8:
  36. printf("\n Function called before initialization. %s %d\n", filestr, line);
  37. break;
  38. case -9:
  39. printf("\n Specified object not found. %s %d\n", filestr, line);
  40. break;
  41. case -10:
  42. printf("\n More input data expected. %s %d\n", filestr, line);
  43. break;
  44. case -11:
  45. printf("\n More output surfaces expected. %s %d\n", filestr, line);
  46. break;
  47. case -12:
  48. printf("\n Operation aborted. %s %d\n", filestr, line);
  49. break;
  50. case -13:
  51. printf("\n HW device lost. %s %d\n", filestr, line);
  52. break;
  53. case -14:
  54. printf("\n Incompatible video parameters. %s %d\n", filestr, line);
  55. break;
  56. case -15:
  57. printf("\n Invalid video parameters. %s %d\n", filestr, line);
  58. break;
  59. case -16:
  60. printf("\n Undefined behavior. %s %d\n", filestr, line);
  61. break;
  62. case -17:
  63. printf("\n Device operation failure. %s %d\n", filestr, line);
  64. break;
  65. case -18:
  66. printf("\n More bitstream data expected. %s %d\n", filestr, line);
  67. break;
  68. case -19:
  69. printf("\n Incompatible audio parameters. %s %d\n", filestr, line);
  70. break;
  71. case -20:
  72. printf("\n Invalid audio parameters. %s %d\n", filestr, line);
  73. break;
  74. default:
  75. printf("\nError code %d,\t%s\t%d\n\n", err, filestr, line);
  76. }
  77. }
  78. mfxStatus ReadPlaneData(mfxU16 w, mfxU16 h, mfxU8 *buf, mfxU8 *ptr, mfxU16 pitch, mfxU16 offset, FILE *fSource)
  79. {
  80. mfxU32 nBytesRead;
  81. for (mfxU16 i = 0; i < h; i++) {
  82. nBytesRead = (mfxU32)fread(buf, 1, w, fSource);
  83. if (w != nBytesRead)
  84. return MFX_ERR_MORE_DATA;
  85. for (mfxU16 j = 0; j < w; j++)
  86. ptr[i * pitch + j * 2 + offset] = buf[j];
  87. }
  88. return MFX_ERR_NONE;
  89. }
  90. mfxStatus LoadRawFrame(mfxFrameSurface1 *pSurface, FILE *fSource)
  91. {
  92. if (!fSource) {
  93. // Simulate instantaneous access to 1000 "empty" frames.
  94. static int frameCount = 0;
  95. if (1000 == frameCount++)
  96. return MFX_ERR_MORE_DATA;
  97. else
  98. return MFX_ERR_NONE;
  99. }
  100. mfxStatus sts = MFX_ERR_NONE;
  101. mfxU32 nBytesRead;
  102. mfxU16 w, h, i, pitch;
  103. mfxU8 *ptr;
  104. mfxFrameInfo *pInfo = &pSurface->Info;
  105. mfxFrameData *pData = &pSurface->Data;
  106. if (pInfo->CropH > 0 && pInfo->CropW > 0) {
  107. w = pInfo->CropW;
  108. h = pInfo->CropH;
  109. } else {
  110. w = pInfo->Width;
  111. h = pInfo->Height;
  112. }
  113. pitch = pData->Pitch;
  114. ptr = pData->Y + pInfo->CropX + pInfo->CropY * pData->Pitch;
  115. // read luminance plane
  116. for (i = 0; i < h; i++) {
  117. nBytesRead = (mfxU32)fread(ptr + i * pitch, 1, w, fSource);
  118. if (w != nBytesRead)
  119. return MFX_ERR_MORE_DATA;
  120. }
  121. mfxU8 buf[2048]; // maximum supported chroma width for nv12
  122. w /= 2;
  123. h /= 2;
  124. ptr = pData->UV + pInfo->CropX + (pInfo->CropY / 2) * pitch;
  125. if (w > 2048)
  126. return MFX_ERR_UNSUPPORTED;
  127. // load U
  128. sts = ReadPlaneData(w, h, buf, ptr, pitch, 0, fSource);
  129. if (MFX_ERR_NONE != sts)
  130. return sts;
  131. // load V
  132. sts = ReadPlaneData(w, h, buf, ptr, pitch, 1, fSource);
  133. if (MFX_ERR_NONE != sts)
  134. return sts;
  135. return MFX_ERR_NONE;
  136. }
  137. mfxStatus LoadRawRGBFrame(mfxFrameSurface1 *pSurface, FILE *fSource)
  138. {
  139. if (!fSource) {
  140. // Simulate instantaneous access to 1000 "empty" frames.
  141. static int frameCount = 0;
  142. if (1000 == frameCount++)
  143. return MFX_ERR_MORE_DATA;
  144. else
  145. return MFX_ERR_NONE;
  146. }
  147. size_t nBytesRead;
  148. mfxU16 w, h;
  149. mfxFrameInfo *pInfo = &pSurface->Info;
  150. if (pInfo->CropH > 0 && pInfo->CropW > 0) {
  151. w = pInfo->CropW;
  152. h = pInfo->CropH;
  153. } else {
  154. w = pInfo->Width;
  155. h = pInfo->Height;
  156. }
  157. for (mfxU16 i = 0; i < h; i++) {
  158. nBytesRead = fread(pSurface->Data.B + i * pSurface->Data.Pitch, 1, w * 4, fSource);
  159. if ((size_t)(w * 4) != nBytesRead)
  160. return MFX_ERR_MORE_DATA;
  161. }
  162. return MFX_ERR_NONE;
  163. }
  164. mfxStatus WriteBitStreamFrame(mfxBitstream *pMfxBitstream, FILE *fSink)
  165. {
  166. mfxU32 nBytesWritten =
  167. (mfxU32)fwrite(pMfxBitstream->Data + pMfxBitstream->DataOffset, 1, pMfxBitstream->DataLength, fSink);
  168. if (nBytesWritten != pMfxBitstream->DataLength)
  169. return MFX_ERR_UNDEFINED_BEHAVIOR;
  170. pMfxBitstream->DataLength = 0;
  171. return MFX_ERR_NONE;
  172. }
  173. mfxStatus ReadBitStreamData(mfxBitstream *pBS, FILE *fSource)
  174. {
  175. memmove(pBS->Data, pBS->Data + pBS->DataOffset, pBS->DataLength);
  176. pBS->DataOffset = 0;
  177. mfxU32 nBytesRead = (mfxU32)fread(pBS->Data + pBS->DataLength, 1, pBS->MaxLength - pBS->DataLength, fSource);
  178. if (0 == nBytesRead)
  179. return MFX_ERR_MORE_DATA;
  180. pBS->DataLength += nBytesRead;
  181. return MFX_ERR_NONE;
  182. }
  183. mfxStatus WriteSection(mfxU8 *plane, mfxU16 factor, mfxU16 chunksize, mfxFrameInfo *pInfo, mfxFrameData *pData,
  184. mfxU32 i, mfxU32 j, FILE *fSink)
  185. {
  186. if (chunksize != fwrite(plane + (pInfo->CropY * pData->Pitch / factor + pInfo->CropX) + i * pData->Pitch + j, 1,
  187. chunksize, fSink))
  188. return MFX_ERR_UNDEFINED_BEHAVIOR;
  189. return MFX_ERR_NONE;
  190. }
  191. mfxStatus WriteRawFrame(mfxFrameSurface1 *pSurface, FILE *fSink)
  192. {
  193. mfxFrameInfo *pInfo = &pSurface->Info;
  194. mfxFrameData *pData = &pSurface->Data;
  195. mfxU32 i, j, h, w;
  196. mfxStatus sts = MFX_ERR_NONE;
  197. for (i = 0; i < pInfo->CropH; i++)
  198. sts = WriteSection(pData->Y, 1, pInfo->CropW, pInfo, pData, i, 0, fSink);
  199. h = pInfo->CropH / 2;
  200. w = pInfo->CropW;
  201. for (i = 0; i < h; i++)
  202. for (j = 0; j < w; j += 2)
  203. sts = WriteSection(pData->UV, 2, 1, pInfo, pData, i, j, fSink);
  204. for (i = 0; i < h; i++)
  205. for (j = 1; j < w; j += 2)
  206. sts = WriteSection(pData->UV, 2, 1, pInfo, pData, i, j, fSink);
  207. return sts;
  208. }
  209. int GetFreeTaskIndex(Task *pTaskPool, mfxU16 nPoolSize)
  210. {
  211. if (pTaskPool)
  212. for (int i = 0; i < nPoolSize; i++)
  213. if (!pTaskPool[i].syncp)
  214. return i;
  215. return MFX_ERR_NOT_FOUND;
  216. }
  217. void ClearYUVSurfaceSysMem(mfxFrameSurface1 *pSfc, mfxU16 width, mfxU16 height)
  218. {
  219. // In case simulating direct access to frames we initialize the allocated surfaces with default pattern
  220. memset(pSfc->Data.Y, 100, width * height); // Y plane
  221. memset(pSfc->Data.U, 50, (width * height) / 2); // UV plane
  222. }
  223. // Get free raw frame surface
  224. int GetFreeSurfaceIndex(mfxFrameSurface1 **pSurfacesPool, mfxU16 nPoolSize)
  225. {
  226. if (pSurfacesPool)
  227. for (mfxU16 i = 0; i < nPoolSize; i++)
  228. if (0 == pSurfacesPool[i]->Data.Locked)
  229. return i;
  230. return MFX_ERR_NOT_FOUND;
  231. }
  232. char mfxFrameTypeString(mfxU16 FrameType)
  233. {
  234. mfxU8 FrameTmp = FrameType & 0xF;
  235. char FrameTypeOut;
  236. switch (FrameTmp) {
  237. case MFX_FRAMETYPE_I:
  238. FrameTypeOut = 'I';
  239. break;
  240. case MFX_FRAMETYPE_P:
  241. FrameTypeOut = 'P';
  242. break;
  243. case MFX_FRAMETYPE_B:
  244. FrameTypeOut = 'B';
  245. break;
  246. default:
  247. FrameTypeOut = '*';
  248. }
  249. return FrameTypeOut;
  250. }