yuvfeeder.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. //--=========================================================================--
  2. // This file is a part of VPU Reference API project
  3. //-----------------------------------------------------------------------------
  4. //
  5. // This confidential and proprietary software may be used only
  6. // as authorized by a licensing agreement from Chips&Media Inc.
  7. // In the event of publication, the following notice is applicable:
  8. //
  9. // (C) COPYRIGHT 2006 - 2013 CHIPS&MEDIA INC.
  10. // ALL RIGHTS RESERVED
  11. //
  12. // The entire notice above must be reproduced on all authorized
  13. // copies.
  14. //
  15. //--=========================================================================--
  16. #include <string.h>
  17. #include <stdarg.h>
  18. #include "main_helper.h"
  19. extern YuvFeederImpl loaderYuvFeederImpl;
  20. BOOL loaderYuvFeeder_Create(
  21. YuvFeederImpl *impl,
  22. const char* path,
  23. Uint32 packed,
  24. Uint32 fbStride,
  25. Uint32 fbHeight
  26. );
  27. BOOL loaderYuvFeeder_Destory(
  28. YuvFeederImpl *impl
  29. );
  30. BOOL loaderYuvFeeder_Feed(
  31. YuvFeederImpl* impl,
  32. Int32 coreIdx,
  33. FrameBuffer* fb,
  34. size_t picWidth,
  35. size_t picHeight,
  36. void* arg
  37. );
  38. BOOL loaderYuvFeeder_Configure(
  39. YuvFeederImpl* impl,
  40. Uint32 cmd,
  41. YuvInfo yuv
  42. );
  43. static BOOL bufferYuvFeeder_Create(
  44. YuvFeederImpl *impl,
  45. const char* path,
  46. Uint32 packed,
  47. Uint32 fbStride,
  48. Uint32 fbHeight)
  49. {
  50. yuvContext* ctx;
  51. if ((ctx=(yuvContext*)osal_malloc(sizeof(yuvContext))) == NULL) {
  52. return FALSE;
  53. }
  54. osal_memset(ctx, 0, sizeof(yuvContext));
  55. impl->context = ctx;
  56. return TRUE;
  57. }
  58. static BOOL bufferYuvFeeder_Destory(
  59. YuvFeederImpl *impl
  60. )
  61. {
  62. yuvContext* ctx = (yuvContext*)impl->context;
  63. osal_free(ctx);
  64. return TRUE;
  65. }
  66. static BOOL bufferYuvFeeder_Feed(
  67. YuvFeederImpl* impl,
  68. Int32 coreIdx,
  69. FrameBuffer* fb,
  70. size_t picWidth,
  71. size_t picHeight,
  72. void* arg
  73. )
  74. {
  75. yuvContext* ctx = (yuvContext*)impl->context;
  76. Uint8* pYuv = arg;
  77. size_t frameSize;
  78. size_t frameSizeY;
  79. size_t frameSizeC;
  80. Int32 bitdepth=0;
  81. Int32 yuv3p4b=0;
  82. Int32 packedFormat=0;
  83. Uint32 outWidth=0;
  84. Uint32 outHeight=0;
  85. CalcYuvSize(fb->format, picWidth, picHeight, fb->cbcrInterleave, &frameSizeY, &frameSizeC, &frameSize, &bitdepth, &packedFormat, &yuv3p4b);
  86. if (fb->mapType == LINEAR_FRAME_MAP ) {
  87. outWidth = (yuv3p4b&&packedFormat==0) ? ((picWidth+31)/32)*32 : picWidth;
  88. outHeight = (yuv3p4b) ? ((picHeight+7)/8)*8 : picHeight;
  89. if ( yuv3p4b && packedFormat) {
  90. outWidth = ((picWidth*2)+2)/3*4;
  91. }
  92. else if(packedFormat) {
  93. outWidth *= 2; // 8bit packed mode(YUYV) only. (need to add 10bit cases later)
  94. if (bitdepth != 0) // 10bit packed
  95. outWidth *= 2;
  96. }
  97. LoadYuvImageBurstFormat(coreIdx, pYuv, outWidth, outHeight, fb, ctx->srcPlanar);
  98. }
  99. else {
  100. TiledMapConfig mapConfig;
  101. osal_memset((void*)&mapConfig, 0x00, sizeof(TiledMapConfig));
  102. if (arg != NULL) {
  103. osal_memcpy((void*)&mapConfig, arg, sizeof(TiledMapConfig));
  104. }
  105. LoadTiledImageYuvBurst(coreIdx, pYuv, picWidth, picHeight, fb, mapConfig);
  106. }
  107. return TRUE;
  108. }
  109. static BOOL bufferYuvFeeder_Configure(
  110. YuvFeederImpl* impl,
  111. Uint32 cmd,
  112. YuvInfo yuv
  113. )
  114. {
  115. yuvContext* ctx = (yuvContext*)impl->context;
  116. UNREFERENCED_PARAMETER(cmd);
  117. ctx->fbStride = yuv.srcStride;
  118. ctx->cbcrInterleave = yuv.cbcrInterleave;
  119. ctx->srcPlanar = yuv.srcPlanar;
  120. return TRUE;
  121. }
  122. static BOOL yuvYuvFeeder_Create(
  123. YuvFeederImpl *impl,
  124. const char* path,
  125. Uint32 packed,
  126. Uint32 fbStride,
  127. Uint32 fbHeight)
  128. {
  129. yuvContext* ctx;
  130. osal_file_t* fp;
  131. Uint8* pYuv;
  132. if ((fp=osal_fopen(path, "rb")) == NULL) {
  133. VLOG(ERR, "%s:%d failed to open yuv file: %s\n", __FUNCTION__, __LINE__, path);
  134. return FALSE;
  135. }
  136. if ( packed == 1 )
  137. pYuv = osal_malloc(fbStride*fbHeight*3*2*2);//packed, unsigned short
  138. else
  139. pYuv = osal_malloc(fbStride*fbHeight*3*2);//unsigned short
  140. if ((ctx=(yuvContext*)osal_malloc(sizeof(yuvContext))) == NULL) {
  141. osal_free(pYuv);
  142. osal_fclose(fp);
  143. return FALSE;
  144. }
  145. osal_memset(ctx, 0, sizeof(yuvContext));
  146. ctx->fp = fp;
  147. ctx->pYuv = pYuv;
  148. impl->context = ctx;
  149. return TRUE;
  150. }
  151. static BOOL yuvYuvFeeder_Destory(
  152. YuvFeederImpl *impl
  153. )
  154. {
  155. yuvContext* ctx = (yuvContext*)impl->context;
  156. osal_fclose(ctx->fp);
  157. osal_free(ctx->pYuv);
  158. osal_free(ctx);
  159. return TRUE;
  160. }
  161. static BOOL yuvYuvFeeder_Feed(
  162. YuvFeederImpl* impl,
  163. Int32 coreIdx,
  164. FrameBuffer* fb,
  165. size_t picWidth,
  166. size_t picHeight,
  167. void* arg
  168. )
  169. {
  170. yuvContext* ctx = (yuvContext*)impl->context;
  171. Uint8* pYuv = ctx->pYuv;
  172. size_t frameSize;
  173. size_t frameSizeY;
  174. size_t frameSizeC;
  175. Int32 bitdepth=0;
  176. Int32 yuv3p4b=0;
  177. Int32 packedFormat=0;
  178. Uint32 outWidth=0;
  179. Uint32 outHeight=0;
  180. CalcYuvSize(fb->format, picWidth, picHeight, fb->cbcrInterleave, &frameSizeY, &frameSizeC, &frameSize, &bitdepth, &packedFormat, &yuv3p4b);
  181. // Load source one picture image to encode to SDRAM frame buffer.
  182. if (!osal_fread(pYuv, 1, frameSize, ctx->fp)) {
  183. if (osal_feof(ctx->fp) == 0)
  184. VLOG(ERR, "Yuv Data osal_fread failed file handle is 0x%x\n", ctx->fp);
  185. return FALSE;
  186. }
  187. if (fb->mapType == LINEAR_FRAME_MAP ) {
  188. outWidth = (yuv3p4b&&packedFormat==0) ? ((picWidth+31)/32)*32 : picWidth;
  189. outHeight = (yuv3p4b) ? ((picHeight+7)/8)*8 : picHeight;
  190. if ( yuv3p4b && packedFormat) {
  191. outWidth = ((picWidth*2)+2)/3*4;
  192. }
  193. else if(packedFormat) {
  194. outWidth *= 2; // 8bit packed mode(YUYV) only. (need to add 10bit cases later)
  195. if (bitdepth != 0) // 10bit packed
  196. outWidth *= 2;
  197. }
  198. LoadYuvImageBurstFormat(coreIdx, pYuv, outWidth, outHeight, fb, ctx->srcPlanar);
  199. }
  200. else {
  201. TiledMapConfig mapConfig;
  202. osal_memset((void*)&mapConfig, 0x00, sizeof(TiledMapConfig));
  203. if (arg != NULL) {
  204. osal_memcpy((void*)&mapConfig, arg, sizeof(TiledMapConfig));
  205. }
  206. LoadTiledImageYuvBurst(coreIdx, pYuv, picWidth, picHeight, fb, mapConfig);
  207. }
  208. return TRUE;
  209. }
  210. static BOOL yuvYuvFeeder_Configure(
  211. YuvFeederImpl* impl,
  212. Uint32 cmd,
  213. YuvInfo yuv
  214. )
  215. {
  216. yuvContext* ctx = (yuvContext*)impl->context;
  217. UNREFERENCED_PARAMETER(cmd);
  218. ctx->fbStride = yuv.srcStride;
  219. ctx->cbcrInterleave = yuv.cbcrInterleave;
  220. ctx->srcPlanar = yuv.srcPlanar;
  221. return TRUE;
  222. }
  223. YuvFeederImpl yuvYuvFeederImpl = {
  224. NULL,
  225. yuvYuvFeeder_Create,
  226. yuvYuvFeeder_Feed,
  227. yuvYuvFeeder_Destory,
  228. yuvYuvFeeder_Configure
  229. };
  230. /*lint -esym(438, ap) */
  231. YuvFeeder YuvFeeder_Create(
  232. Uint32 type,
  233. const char* srcFilePath,
  234. YuvInfo yuvInfo
  235. )
  236. {
  237. AbstractYuvFeeder *feeder;
  238. YuvFeederImpl *impl;
  239. BOOL success = FALSE;
  240. if ((type != SOURCE_YUV_WITH_BUFFER) && (srcFilePath == NULL)) {
  241. VLOG(ERR, "%s:%d src path is NULL, tpye %d\n", __FUNCTION__, __LINE__, type);
  242. return NULL;
  243. }
  244. // Create YuvFeeder for type.
  245. switch (type) {
  246. case SOURCE_YUV:
  247. impl = osal_malloc(sizeof(YuvFeederImpl));
  248. impl->Create = &yuvYuvFeeder_Create;
  249. impl->Feed = &yuvYuvFeeder_Feed;
  250. impl->Destroy = &yuvYuvFeeder_Destory;
  251. impl->Configure = &yuvYuvFeeder_Configure;
  252. if ((success=impl->Create(impl, srcFilePath, yuvInfo.packedFormat, yuvInfo.srcStride, yuvInfo.srcHeight)) == TRUE) {
  253. impl->Configure(impl, 0, yuvInfo);
  254. }
  255. break;
  256. case SOURCE_YUV_WITH_LOADER:
  257. impl = osal_malloc(sizeof(YuvFeederImpl));
  258. impl->Create = &loaderYuvFeeder_Create;
  259. impl->Feed = &loaderYuvFeeder_Feed;
  260. impl->Destroy = &loaderYuvFeeder_Destory;
  261. impl->Configure = &loaderYuvFeeder_Configure;
  262. if ((success=impl->Create(impl, srcFilePath, yuvInfo.packedFormat, yuvInfo.srcStride, yuvInfo.srcHeight)) == TRUE) {
  263. impl->Configure(impl, 0, yuvInfo);
  264. }
  265. break;
  266. case SOURCE_YUV_WITH_BUFFER:
  267. impl = osal_malloc(sizeof(YuvFeederImpl));
  268. impl->Create = &bufferYuvFeeder_Create;
  269. impl->Feed = &bufferYuvFeeder_Feed;
  270. impl->Destroy = &bufferYuvFeeder_Destory;
  271. impl->Configure = &bufferYuvFeeder_Configure;
  272. if ((success=impl->Create(impl, srcFilePath, yuvInfo.packedFormat, yuvInfo.srcStride, yuvInfo.srcHeight)) == TRUE) {
  273. impl->Configure(impl, 0, yuvInfo);
  274. }
  275. break;
  276. default:
  277. VLOG(ERR, "%s:%d Unknown YuvFeeder Type\n", __FUNCTION__, __LINE__);
  278. success = FALSE;
  279. break;
  280. }
  281. if (success == FALSE)
  282. return NULL;
  283. feeder = (AbstractYuvFeeder*)osal_malloc(sizeof(AbstractYuvFeeder));
  284. feeder->impl = impl;
  285. return feeder;
  286. }
  287. /*lint +esym(438, ap) */
  288. BOOL YuvFeeder_Destroy(
  289. YuvFeeder feeder
  290. )
  291. {
  292. YuvFeederImpl* impl = NULL;
  293. AbstractYuvFeeder* yuvfeeder = (AbstractYuvFeeder*)feeder;
  294. if (yuvfeeder == NULL) {
  295. VLOG(ERR, "%s:%d Invalid handle\n", __FUNCTION__, __LINE__);
  296. return FALSE;
  297. }
  298. impl = yuvfeeder->impl;
  299. impl->Destroy(impl);
  300. osal_free(impl);
  301. return TRUE;
  302. }
  303. BOOL YuvFeeder_Feed(
  304. YuvFeeder feeder,
  305. Uint32 coreIdx,
  306. FrameBuffer* fb,
  307. size_t picWidth,
  308. size_t picHeight,
  309. void* arg
  310. )
  311. {
  312. YuvFeederImpl* impl = NULL;
  313. AbstractYuvFeeder* absFeeder = (AbstractYuvFeeder*)feeder;
  314. Int32 ret;
  315. if (absFeeder == NULL) {
  316. VLOG(ERR, "%s:%d Invalid handle\n", __FUNCTION__, __LINE__);
  317. return FALSE;
  318. }
  319. impl = absFeeder->impl;
  320. ret = impl->Feed(impl, coreIdx, fb, picWidth, picHeight, arg);
  321. if ( ret == TRUE)
  322. fb->srcBufState = SRC_BUFFER_SRC_LOADED;
  323. return ret;
  324. }