common_utils.cpp 7.6 KB

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