yuvfeeder.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. // SPDX-License-Identifier: LGPL-2.1 OR BSD-3-Clause
  2. /*
  3. * Copyright (c) 2019, Chips&Media
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  16. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  19. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  22. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <string.h>
  27. #include <stdarg.h>
  28. #include "main_helper.h"
  29. extern YuvFeederImpl loaderYuvFeederImpl;
  30. extern YuvFeederImpl cfbcYuvFeederImpl;
  31. BOOL loaderYuvFeeder_Create(
  32. YuvFeederImpl *impl,
  33. const char* path,
  34. Uint32 packed,
  35. Uint32 fbStride,
  36. Uint32 fbHeight
  37. );
  38. BOOL loaderYuvFeeder_Destory(
  39. YuvFeederImpl *impl
  40. );
  41. BOOL loaderYuvFeeder_Feed(
  42. YuvFeederImpl* impl,
  43. Int32 coreIdx,
  44. FrameBuffer* fb,
  45. size_t picWidth,
  46. size_t picHeight,
  47. Uint32 srcFbIndex,
  48. void* arg
  49. );
  50. BOOL loaderYuvFeeder_Configure(
  51. YuvFeederImpl* impl,
  52. Uint32 cmd,
  53. YuvInfo yuv
  54. );
  55. BOOL cfbcYuvFeeder_Create(
  56. YuvFeederImpl *impl,
  57. const char* path,
  58. Uint32 packed,
  59. Uint32 fbStride,
  60. Uint32 fbHeight
  61. );
  62. BOOL cfbcYuvFeeder_Destory(
  63. YuvFeederImpl *impl
  64. );
  65. BOOL cfbcYuvFeeder_Feed(
  66. YuvFeederImpl* impl,
  67. Int32 coreIdx,
  68. FrameBuffer* fb,
  69. size_t picWidth,
  70. size_t picHeight,
  71. Uint32 srcFbIndex,
  72. void* arg
  73. );
  74. BOOL cfbcYuvFeeder_Configure(
  75. YuvFeederImpl* impl,
  76. Uint32 cmd,
  77. YuvInfo yuv
  78. );
  79. static BOOL yuvYuvFeeder_Create(
  80. YuvFeederImpl *impl,
  81. const char* path,
  82. Uint32 packed,
  83. Uint32 fbStride,
  84. Uint32 fbHeight)
  85. {
  86. yuvContext* ctx;
  87. osal_file_t* fp;
  88. Uint8* pYuv;
  89. if ((fp=osal_fopen(path, "rb")) == NULL) {
  90. VLOG(ERR, "%s:%d failed to open yuv file: %s\n", __FUNCTION__, __LINE__, path);
  91. return FALSE;
  92. }
  93. if ( packed == 1 )
  94. pYuv = osal_malloc(fbStride*fbHeight*3*2*2);//packed, unsigned short
  95. else
  96. pYuv = osal_malloc(fbStride*fbHeight*3*2);//unsigned short
  97. if ((ctx=(yuvContext*)osal_malloc(sizeof(yuvContext))) == NULL) {
  98. osal_free(pYuv);
  99. osal_fclose(fp);
  100. return FALSE;
  101. }
  102. osal_memset(ctx, 0, sizeof(yuvContext));
  103. ctx->fp = fp;
  104. ctx->pYuv = pYuv;
  105. impl->context = ctx;
  106. return TRUE;
  107. }
  108. static BOOL yuvYuvFeeder_Destory(
  109. YuvFeederImpl *impl
  110. )
  111. {
  112. yuvContext* ctx = (yuvContext*)impl->context;
  113. osal_fclose(ctx->fp);
  114. osal_free(ctx->pYuv);
  115. osal_free(ctx);
  116. return TRUE;
  117. }
  118. static BOOL yuvYuvFeeder_Feed(
  119. YuvFeederImpl* impl,
  120. Int32 coreIdx,
  121. FrameBuffer* fb,
  122. size_t picWidth,
  123. size_t picHeight,
  124. Uint32 srcFbIndex,
  125. void* arg
  126. )
  127. {
  128. yuvContext* ctx = (yuvContext*)impl->context;
  129. Uint8* pYuv = ctx->pYuv;
  130. size_t frameSize;
  131. size_t frameSizeY;
  132. size_t frameSizeC;
  133. Int32 bitdepth=0;
  134. Int32 yuv3p4b=0;
  135. Int32 packedFormat=0;
  136. Uint32 outWidth=0;
  137. Uint32 outHeight=0;
  138. CalcYuvSize(fb->format, picWidth, picHeight, fb->cbcrInterleave, &frameSizeY, &frameSizeC, &frameSize, &bitdepth, &packedFormat, &yuv3p4b);
  139. // Load source one picture image to encode to SDRAM frame buffer.
  140. if (!osal_fread(pYuv, 1, frameSize, ctx->fp)) {
  141. if (osal_feof(ctx->fp) == 0)
  142. VLOG(ERR, "Yuv Data osal_fread failed file handle is 0x%x\n", ctx->fp);
  143. return FALSE;
  144. }
  145. if (fb->mapType == LINEAR_FRAME_MAP ) {
  146. outWidth = (yuv3p4b&&packedFormat==0) ? ((picWidth+31)/32)*32 : picWidth;
  147. outHeight = (yuv3p4b) ? ((picHeight+7)/8)*8 : picHeight;
  148. if ( yuv3p4b && packedFormat) {
  149. outWidth = ((picWidth*2)+2)/3*4;
  150. }
  151. else if(packedFormat) {
  152. outWidth *= 2; // 8bit packed mode(YUYV) only. (need to add 10bit cases later)
  153. if (bitdepth != 0) // 10bit packed
  154. outWidth *= 2;
  155. }
  156. LoadYuvImageByYCbCrLine(impl->handle, coreIdx, pYuv, outWidth, outHeight, fb, srcFbIndex);
  157. //LoadYuvImageBurstFormat(coreIdx, pYuv, outWidth, outHeight, fb, ctx->srcPlanar);
  158. }
  159. else {
  160. TiledMapConfig mapConfig;
  161. osal_memset((void*)&mapConfig, 0x00, sizeof(TiledMapConfig));
  162. if (arg != NULL) {
  163. osal_memcpy((void*)&mapConfig, arg, sizeof(TiledMapConfig));
  164. }
  165. LoadTiledImageYuvBurst(impl->handle, coreIdx, pYuv, picWidth, picHeight, fb, mapConfig);
  166. }
  167. return TRUE;
  168. }
  169. static BOOL yuvYuvFeeder_Configure(
  170. YuvFeederImpl* impl,
  171. Uint32 cmd,
  172. YuvInfo yuv
  173. )
  174. {
  175. yuvContext* ctx = (yuvContext*)impl->context;
  176. UNREFERENCED_PARAMETER(cmd);
  177. ctx->fbStride = yuv.srcStride;
  178. ctx->cbcrInterleave = yuv.cbcrInterleave;
  179. ctx->srcPlanar = yuv.srcPlanar;
  180. return TRUE;
  181. }
  182. YuvFeederImpl yuvYuvFeederImpl = {
  183. NULL,
  184. yuvYuvFeeder_Create,
  185. yuvYuvFeeder_Feed,
  186. yuvYuvFeeder_Destory,
  187. yuvYuvFeeder_Configure
  188. };
  189. /*lint -esym(438, ap) */
  190. YuvFeeder YuvFeeder_Create(
  191. Uint32 type,
  192. const char* srcFilePath,
  193. YuvInfo yuvInfo
  194. )
  195. {
  196. AbstractYuvFeeder *feeder;
  197. YuvFeederImpl *impl;
  198. BOOL success = FALSE;
  199. if (srcFilePath == NULL) {
  200. VLOG(ERR, "%s:%d src path is NULL\n", __FUNCTION__, __LINE__);
  201. return NULL;
  202. }
  203. // Create YuvFeeder for type.
  204. switch (type) {
  205. case SOURCE_YUV:
  206. impl = osal_malloc(sizeof(YuvFeederImpl));
  207. impl->Create = yuvYuvFeeder_Create;
  208. impl->Feed = yuvYuvFeeder_Feed;
  209. impl->Destroy = yuvYuvFeeder_Destory;
  210. impl->Configure = yuvYuvFeeder_Configure;
  211. if ((success=impl->Create(impl, srcFilePath, yuvInfo.packedFormat, yuvInfo.srcStride, yuvInfo.srcHeight)) == TRUE) {
  212. impl->Configure(impl, 0, yuvInfo);
  213. }
  214. break;
  215. case SOURCE_YUV_WITH_LOADER:
  216. impl = osal_malloc(sizeof(YuvFeederImpl));
  217. impl->Create = loaderYuvFeeder_Create;
  218. impl->Feed = loaderYuvFeeder_Feed;
  219. impl->Destroy = loaderYuvFeeder_Destory;
  220. impl->Configure = loaderYuvFeeder_Configure;
  221. if ((success=impl->Create(impl, srcFilePath, yuvInfo.packedFormat, yuvInfo.srcStride, yuvInfo.srcHeight)) == TRUE) {
  222. impl->Configure(impl, 0, yuvInfo);
  223. }
  224. break;
  225. default:
  226. VLOG(ERR, "%s:%d Unknown YuvFeeder Type\n", __FUNCTION__, __LINE__);
  227. return NULL;
  228. }
  229. if (success == FALSE)
  230. return NULL;
  231. feeder = (AbstractYuvFeeder*)osal_malloc(sizeof(AbstractYuvFeeder));
  232. if ( !feeder )
  233. return NULL;
  234. feeder->impl = impl;
  235. return feeder;
  236. }
  237. /*lint +esym(438, ap) */
  238. BOOL YuvFeeder_Destroy(
  239. YuvFeeder feeder
  240. )
  241. {
  242. YuvFeederImpl* impl = NULL;
  243. AbstractYuvFeeder* yuvfeeder = (AbstractYuvFeeder*)feeder;
  244. if (yuvfeeder == NULL) {
  245. VLOG(ERR, "%s:%d Invalid handle\n", __FUNCTION__, __LINE__);
  246. return FALSE;
  247. }
  248. impl = yuvfeeder->impl;
  249. impl->Destroy(impl);
  250. osal_free(impl);
  251. return TRUE;
  252. }
  253. BOOL YuvFeeder_Feed(
  254. YuvFeeder feeder,
  255. Uint32 coreIdx,
  256. FrameBuffer* fb,
  257. size_t picWidth,
  258. size_t picHeight,
  259. Uint32 srcFbIndex,
  260. void* arg
  261. )
  262. {
  263. YuvFeederImpl* impl = NULL;
  264. AbstractYuvFeeder* absFeeder = (AbstractYuvFeeder*)feeder;
  265. Int32 ret;
  266. if (absFeeder == NULL) {
  267. VLOG(ERR, "%s:%d Invalid handle\n", __FUNCTION__, __LINE__);
  268. return FALSE;
  269. }
  270. impl = absFeeder->impl;
  271. ret = impl->Feed(impl, coreIdx, fb, picWidth, picHeight, srcFbIndex, arg);
  272. return ret;
  273. }