common_utils.cpp 7.2 KB

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