yuvfeeder.c 10 KB

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