common_utils.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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",filestr,line);
  23. break;
  24. case -3:
  25. printf("\n Unsupported feature/library load error. %s %d\n",filestr,line);
  26. break;
  27. case -4:
  28. printf("\n Could not allocate memory. %s %d\n",filestr,line);
  29. break;
  30. case -5:
  31. printf("\n Insufficient IO buffers. %s %d\n",filestr,line);
  32. break;
  33. case -6:
  34. printf("\n Invalid handle. %s %d\n",filestr,line);
  35. break;
  36. case -7:
  37. printf("\n Memory lock failure. %s %d\n",filestr,line);
  38. break;
  39. case -8:
  40. printf("\n Function called before initialization. %s %d\n",filestr,line);
  41. break;
  42. case -9:
  43. printf("\n Specified object not found. %s %d\n",filestr,line);
  44. break;
  45. case -10:
  46. printf("\n More input data expected. %s %d\n",filestr,line);
  47. break;
  48. case -11:
  49. printf("\n More output surfaces expected. %s %d\n",filestr,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,line);
  59. break;
  60. case -15:
  61. printf("\n Invalid video parameters. %s %d\n",filestr,line);
  62. break;
  63. case -16:
  64. printf("\n Undefined behavior. %s %d\n",filestr,line);
  65. break;
  66. case -17:
  67. printf("\n Device operation failure. %s %d\n",filestr,line);
  68. break;
  69. case -18:
  70. printf("\n More bitstream data expected. %s %d\n",filestr,line);
  71. break;
  72. case -19:
  73. printf("\n Incompatible audio parameters. %s %d\n",filestr,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. 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, 1,
  174. 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 !=
  197. fwrite(plane +
  198. (pInfo->CropY * pData->Pitch / factor + pInfo->CropX) +
  199. i * pData->Pitch + j, 1, chunksize, fSink))
  200. return MFX_ERR_UNDEFINED_BEHAVIOR;
  201. return MFX_ERR_NONE;
  202. }
  203. mfxStatus WriteRawFrame(mfxFrameSurface1* pSurface, FILE* fSink)
  204. {
  205. mfxFrameInfo* pInfo = &pSurface->Info;
  206. mfxFrameData* pData = &pSurface->Data;
  207. mfxU32 i, j, h, w;
  208. mfxStatus sts = MFX_ERR_NONE;
  209. for (i = 0; i < pInfo->CropH; i++)
  210. sts =
  211. WriteSection(pData->Y, 1, pInfo->CropW, pInfo, pData, i, 0,
  212. 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 =
  218. WriteSection(pData->UV, 2, 1, pInfo, pData, i, j,
  219. fSink);
  220. for (i = 0; i < h; i++)
  221. for (j = 1; j < w; j += 2)
  222. sts =
  223. WriteSection(pData->UV, 2, 1, pInfo, pData, i, j,
  224. fSink);
  225. return sts;
  226. }
  227. int GetFreeTaskIndex(Task* pTaskPool, mfxU16 nPoolSize)
  228. {
  229. if (pTaskPool)
  230. for (int i = 0; i < nPoolSize; i++)
  231. if (!pTaskPool[i].syncp)
  232. return i;
  233. return MFX_ERR_NOT_FOUND;
  234. }
  235. void ClearYUVSurfaceSysMem(mfxFrameSurface1* pSfc, mfxU16 width, mfxU16 height)
  236. {
  237. // In case simulating direct access to frames we initialize the allocated surfaces with default pattern
  238. memset(pSfc->Data.Y, 100, width * height); // Y plane
  239. memset(pSfc->Data.U, 50, (width * height)/2); // UV plane
  240. }
  241. // Get free raw frame surface
  242. int GetFreeSurfaceIndex(mfxFrameSurface1** pSurfacesPool, mfxU16 nPoolSize)
  243. {
  244. if (pSurfacesPool)
  245. for (mfxU16 i = 0; i < nPoolSize; i++)
  246. if (0 == pSurfacesPool[i]->Data.Locked)
  247. return i;
  248. return MFX_ERR_NOT_FOUND;
  249. }
  250. char mfxFrameTypeString(mfxU16 FrameType)
  251. {
  252. mfxU8 FrameTmp = FrameType & 0xF;
  253. char FrameTypeOut;
  254. switch (FrameTmp) {
  255. case MFX_FRAMETYPE_I:
  256. FrameTypeOut = 'I';
  257. break;
  258. case MFX_FRAMETYPE_P:
  259. FrameTypeOut = 'P';
  260. break;
  261. case MFX_FRAMETYPE_B:
  262. FrameTypeOut = 'B';
  263. break;
  264. default:
  265. FrameTypeOut = '*';
  266. }
  267. return FrameTypeOut;
  268. }