common_utils.cpp 7.3 KB

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