component_enc_feeder.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. /*
  2. * Copyright (c) 2019, Chips&Media
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  15. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  18. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  19. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  20. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  21. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include <string.h>
  26. #include "component.h"
  27. typedef enum {
  28. SRC_FB_TYPE_YUV,
  29. SRC_FB_TYPE_CFRAME
  30. } SRC_FB_TYPE;
  31. typedef enum {
  32. YUV_FEEDER_STATE_WAIT,
  33. YUV_FEEDER_STATE_FEEDING
  34. } YuvFeederState;
  35. typedef struct {
  36. EncHandle handle;
  37. char inputPath[MAX_FILE_PATH];
  38. EncOpenParam encOpenParam;
  39. Int32 rotAngle;
  40. Int32 mirDir;
  41. Int32 frameOutNum;
  42. Int32 yuvMode;
  43. FrameBufferAllocInfo srcFbAllocInfo;
  44. FrameBufferAllocInfo reconFbAllocInfo;
  45. BOOL fbAllocated;
  46. ParamEncNeedFrameBufferNum fbCount;
  47. FrameBuffer pFbRecon[MAX_REG_FRAME];
  48. vpu_buffer_t pFbReconMem[MAX_REG_FRAME];
  49. FrameBuffer pFbSrc[ENC_SRC_BUF_NUM];
  50. vpu_buffer_t pFbSrcMem[ENC_SRC_BUF_NUM];
  51. FrameBuffer pFbOffsetTbl[ENC_SRC_BUF_NUM];
  52. vpu_buffer_t pFbOffsetTblMem[ENC_SRC_BUF_NUM];
  53. YuvFeeder yuvFeeder;
  54. BOOL last;
  55. YuvFeederState state;
  56. BOOL stateDoing;
  57. Int32 feedingNum;
  58. TiledMapConfig mapConfig;
  59. Int32 productID;
  60. BOOL MemoryOptimization;
  61. int totalBufferNumber;
  62. int currentBufferNumber;
  63. } YuvFeederContext;
  64. static void InitYuvFeederContext(YuvFeederContext* ctx, CNMComponentConfig* componentParam)
  65. {
  66. osal_memset((void*)ctx, 0, sizeof(YuvFeederContext));
  67. strcpy(ctx->inputPath, componentParam->testEncConfig.yuvFileName);
  68. ctx->fbAllocated = FALSE;
  69. ctx->encOpenParam = componentParam->encOpenParam;
  70. ctx->yuvMode = componentParam->testEncConfig.yuv_mode;
  71. ctx->fbCount.reconFbNum = 0;
  72. ctx->fbCount.srcFbNum = 0;
  73. ctx->frameOutNum = componentParam->testEncConfig.outNum;
  74. ctx->rotAngle = componentParam->testEncConfig.rotAngle;
  75. ctx->mirDir = componentParam->testEncConfig.mirDir;
  76. osal_memset(&ctx->srcFbAllocInfo, 0x00, sizeof(FrameBufferAllocInfo));
  77. osal_memset(&ctx->reconFbAllocInfo, 0x00, sizeof(FrameBufferAllocInfo));
  78. osal_memset((void*)ctx->pFbRecon, 0x00, sizeof(ctx->pFbRecon));
  79. osal_memset((void*)ctx->pFbReconMem, 0x00, sizeof(ctx->pFbReconMem));
  80. osal_memset((void*)ctx->pFbSrc, 0x00, sizeof(ctx->pFbSrc));
  81. osal_memset((void*)ctx->pFbSrcMem, 0x00, sizeof(ctx->pFbSrcMem));
  82. osal_memset((void*)ctx->pFbOffsetTbl, 0x00, sizeof(ctx->pFbOffsetTbl));
  83. osal_memset((void*)ctx->pFbOffsetTblMem, 0x00, sizeof(ctx->pFbOffsetTblMem));
  84. }
  85. static CNMComponentParamRet GetParameterYuvFeeder(ComponentImpl* from, ComponentImpl* com, GetParameterCMD commandType, void* data)
  86. {
  87. YuvFeederContext* ctx = (YuvFeederContext*)com->context;
  88. BOOL result = TRUE;
  89. ParamEncFrameBuffer* allocFb = NULL;
  90. switch(commandType) {
  91. case GET_PARAM_YUVFEEDER_FRAME_BUF:
  92. if (ctx->fbAllocated == FALSE || ctx->currentBufferNumber < ctx->totalBufferNumber) return CNM_COMPONENT_PARAM_NOT_READY;
  93. allocFb = (ParamEncFrameBuffer*)data;
  94. allocFb->reconFb = ctx->pFbRecon;
  95. allocFb->srcFb = ctx->pFbSrc;
  96. allocFb->reconFbAllocInfo = ctx->reconFbAllocInfo;
  97. allocFb->srcFbAllocInfo = ctx->srcFbAllocInfo;
  98. break;
  99. default:
  100. return CNM_COMPONENT_PARAM_NOT_FOUND;
  101. }
  102. return (result == TRUE) ? CNM_COMPONENT_PARAM_SUCCESS : CNM_COMPONENT_PARAM_FAILURE;
  103. }
  104. static CNMComponentParamRet SetParameterYuvFeeder(ComponentImpl* from, ComponentImpl* com, SetParameterCMD commandType, void* data)
  105. {
  106. BOOL result = TRUE;
  107. switch(commandType) {
  108. default:
  109. VLOG(ERR, "Unknown SetParameterCMD Type : %d\n", commandType);
  110. result = FALSE;
  111. break;
  112. }
  113. return (result == TRUE) ? CNM_COMPONENT_PARAM_SUCCESS : CNM_COMPONENT_PARAM_FAILURE;
  114. }
  115. BOOL GetAllocInfo(ComponentImpl* com, Uint32 size)
  116. {
  117. YuvFeederContext* ctx = (YuvFeederContext*)com->context;
  118. EncOpenParam encOpenParam = ctx->encOpenParam;
  119. FrameBufferAllocInfo fbAllocInfo;
  120. Uint32 fbWidth = 0;
  121. Uint32 fbHeight = 0;
  122. Uint32 fbStride = 0;
  123. SRC_FB_TYPE srcFbType = SRC_FB_TYPE_YUV;
  124. TiledMapType mapType;
  125. osal_memset(&fbAllocInfo, 0x00, sizeof(FrameBufferAllocInfo));
  126. //Buffers for source frames
  127. if (PRODUCT_ID_W_SERIES(ctx->productID)) {
  128. fbWidth = VPU_ALIGN8(encOpenParam.picWidth);
  129. fbHeight = VPU_ALIGN8(encOpenParam.picHeight);
  130. fbAllocInfo.endian = encOpenParam.sourceEndian;
  131. } else {
  132. //CODA
  133. fbWidth = VPU_ALIGN16(encOpenParam.picWidth);
  134. fbHeight = VPU_ALIGN16(encOpenParam.picHeight);
  135. fbAllocInfo.endian = encOpenParam.frameEndian;
  136. }
  137. if (SRC_FB_TYPE_YUV == srcFbType) {
  138. mapType = LINEAR_FRAME_MAP;
  139. }
  140. fbStride = CalcStride(fbWidth, fbHeight, (FrameBufferFormat)encOpenParam.srcFormat, encOpenParam.cbcrInterleave, mapType, FALSE);
  141. fbAllocInfo.format = (FrameBufferFormat)encOpenParam.srcFormat;
  142. fbAllocInfo.cbcrInterleave = encOpenParam.cbcrInterleave;
  143. fbAllocInfo.mapType = mapType;
  144. fbAllocInfo.stride = fbStride;
  145. fbAllocInfo.height = fbHeight;
  146. fbAllocInfo.size = size;
  147. fbAllocInfo.type = FB_TYPE_PPU;
  148. fbAllocInfo.num = ctx->fbCount.srcFbNum;
  149. fbAllocInfo.nv21 = encOpenParam.nv21;
  150. if (PRODUCT_ID_NOT_W_SERIES(ctx->productID)) {
  151. fbAllocInfo.lumaBitDepth = 8;
  152. fbAllocInfo.chromaBitDepth = 8;
  153. }
  154. ctx->srcFbAllocInfo = fbAllocInfo;
  155. return TRUE;
  156. }
  157. void SetFeederTotalBufferNumber(ComponentImpl* com, Uint32 number)
  158. {
  159. YuvFeederContext* ctx = (YuvFeederContext*)com->context;
  160. ctx->totalBufferNumber = number;
  161. }
  162. BOOL AttachDMABuffer(ComponentImpl* com, void *pBuffer, Uint32 size)
  163. {
  164. YuvFeederContext* ctx = (YuvFeederContext*)com->context;
  165. EncOpenParam encOpenParam = ctx->encOpenParam;
  166. int i = 0;
  167. GetAllocInfo(com, size);
  168. for (i = 0;i < ENC_SRC_BUF_NUM; i ++){
  169. if (ctx->pFbSrcMem[i].phys_addr == 0)
  170. {
  171. VLOG(INFO, "Found empty frame at index %d\r\n", i);
  172. break;
  173. }
  174. }
  175. if (i == MAX_REG_FRAME)
  176. {
  177. VLOG(ERR, "Could not found empty frame at index\r\n");
  178. return FALSE;
  179. }
  180. if (FALSE == AttachFBMemory(encOpenParam.coreIdx, &ctx->pFbSrcMem[i], &ctx->pFbSrc[i], pBuffer, size)) {
  181. VLOG(ERR, "failed to attach source buffers\n");
  182. return FALSE;
  183. }
  184. ctx->pFbSrcMem[i].virt_addr = (unsigned long)pBuffer;
  185. ctx->currentBufferNumber ++;
  186. return TRUE;
  187. }
  188. void* AllocateFrameBuffer2(ComponentImpl* com, Uint32 size)
  189. {
  190. YuvFeederContext* ctx = (YuvFeederContext*)com->context;
  191. EncOpenParam encOpenParam = ctx->encOpenParam;
  192. Uint32 fbWidth = 0;
  193. Uint32 fbHeight = 0;
  194. Uint32 fbStride = 0;
  195. Uint32 fbSize = 0;
  196. SRC_FB_TYPE srcFbType = SRC_FB_TYPE_YUV;
  197. DRAMConfig dramConfig;
  198. DRAMConfig* pDramConfig = NULL;
  199. TiledMapType mapType;
  200. FrameBufferAllocInfo fbAllocInfo;
  201. int i = 0;
  202. if (ctx->handle == NULL) {
  203. ctx->MemoryOptimization = FALSE;
  204. return NULL;
  205. }
  206. osal_memset(&fbAllocInfo, 0x00, sizeof(FrameBufferAllocInfo));
  207. osal_memset(&dramConfig, 0x00, sizeof(DRAMConfig));
  208. //Buffers for source frames
  209. if (PRODUCT_ID_W_SERIES(ctx->productID)) {
  210. fbWidth = VPU_ALIGN8(encOpenParam.picWidth);
  211. fbHeight = VPU_ALIGN8(encOpenParam.picHeight);
  212. fbAllocInfo.endian = encOpenParam.sourceEndian;
  213. } else {
  214. //CODA
  215. fbWidth = VPU_ALIGN16(encOpenParam.picWidth);
  216. fbHeight = VPU_ALIGN16(encOpenParam.picHeight);
  217. fbAllocInfo.endian = encOpenParam.frameEndian;
  218. VPU_EncGiveCommand(ctx->handle, GET_DRAM_CONFIG, &dramConfig);
  219. pDramConfig = &dramConfig;
  220. }
  221. if (fbWidth % 32 > 0)
  222. {
  223. ctx->MemoryOptimization = FALSE;
  224. return NULL;
  225. }
  226. if (SRC_FB_TYPE_YUV == srcFbType) {
  227. mapType = LINEAR_FRAME_MAP;
  228. }
  229. fbStride = CalcStride(fbWidth, fbHeight, (FrameBufferFormat)encOpenParam.srcFormat, encOpenParam.cbcrInterleave, mapType, FALSE);
  230. fbSize = VPU_GetFrameBufSize(ctx->handle, encOpenParam.coreIdx, fbStride, fbHeight, mapType, (FrameBufferFormat)encOpenParam.srcFormat, encOpenParam.cbcrInterleave, pDramConfig);
  231. VLOG(INFO, "fbSize = %d\r\n", fbSize);
  232. fbAllocInfo.format = (FrameBufferFormat)encOpenParam.srcFormat;
  233. fbAllocInfo.cbcrInterleave = encOpenParam.cbcrInterleave;
  234. fbAllocInfo.mapType = mapType;
  235. fbAllocInfo.stride = fbStride;
  236. fbAllocInfo.height = fbHeight;
  237. fbAllocInfo.size = fbSize == 0? size : fbSize;
  238. fbAllocInfo.type = FB_TYPE_PPU;
  239. fbAllocInfo.num = ctx->fbCount.srcFbNum;
  240. fbAllocInfo.nv21 = encOpenParam.nv21;
  241. if (PRODUCT_ID_NOT_W_SERIES(ctx->productID)) {
  242. fbAllocInfo.lumaBitDepth = 8;
  243. fbAllocInfo.chromaBitDepth = 8;
  244. }
  245. for (i = 0;i < ENC_SRC_BUF_NUM; i ++){
  246. if (ctx->pFbSrcMem[i].phys_addr == 0)
  247. {
  248. VLOG(INFO, "Found empty frame at index %d\r\n", i);
  249. break;
  250. }
  251. }
  252. if (i == MAX_REG_FRAME)
  253. {
  254. VLOG(ERR, "Could not found empty frame at index\r\n");
  255. return NULL;
  256. }
  257. if (FALSE == AllocFBMemory(encOpenParam.coreIdx, &ctx->pFbSrcMem[i], &ctx->pFbSrc[i], fbAllocInfo.size, 1, ENC_SRC, ctx->handle->instIndex)) {
  258. VLOG(ERR, "failed to allocate source buffers\n");
  259. return NULL;
  260. }
  261. ctx->srcFbAllocInfo = fbAllocInfo;
  262. ctx->currentBufferNumber ++;
  263. return (void *)ctx->pFbSrcMem[i].virt_addr;
  264. }
  265. static BOOL AllocateFrameBuffer(ComponentImpl* com) {
  266. YuvFeederContext* ctx = (YuvFeederContext*)com->context;
  267. EncOpenParam encOpenParam = ctx->encOpenParam;
  268. Uint32 fbWidth = 0;
  269. Uint32 fbHeight = 0;
  270. Uint32 fbStride = 0;
  271. Uint32 fbSize = 0;
  272. SRC_FB_TYPE srcFbType = SRC_FB_TYPE_YUV;
  273. DRAMConfig dramConfig;
  274. DRAMConfig* pDramConfig = NULL;
  275. TiledMapType mapType;
  276. FrameBufferAllocInfo fbAllocInfo;
  277. osal_memset(&fbAllocInfo, 0x00, sizeof(FrameBufferAllocInfo));
  278. osal_memset(&dramConfig, 0x00, sizeof(DRAMConfig));
  279. //Buffers for source frames
  280. if (PRODUCT_ID_W_SERIES(ctx->productID)) {
  281. fbWidth = VPU_ALIGN8(encOpenParam.picWidth);
  282. fbHeight = VPU_ALIGN8(encOpenParam.picHeight);
  283. fbAllocInfo.endian = encOpenParam.sourceEndian;
  284. } else {
  285. //CODA
  286. fbWidth = VPU_ALIGN16(encOpenParam.picWidth);
  287. fbHeight = VPU_ALIGN16(encOpenParam.picHeight);
  288. fbAllocInfo.endian = encOpenParam.frameEndian;
  289. VPU_EncGiveCommand(ctx->handle, GET_DRAM_CONFIG, &dramConfig);
  290. pDramConfig = &dramConfig;
  291. }
  292. VLOG(INFO, "fbWidth = %d fbHeight = %d MemoryOptimization = %d\r\n", fbWidth, fbHeight, ctx->MemoryOptimization);
  293. if (fbWidth % 32 > 0)
  294. {
  295. ctx->MemoryOptimization = FALSE;
  296. }
  297. else if (ctx->MemoryOptimization)
  298. {
  299. goto alloc_fbc;
  300. }
  301. VLOG(INFO, "Alloc FrameBuffers\r\n");
  302. if (SRC_FB_TYPE_YUV == srcFbType) {
  303. mapType = LINEAR_FRAME_MAP;
  304. }
  305. fbStride = CalcStride(fbWidth, fbHeight, (FrameBufferFormat)encOpenParam.srcFormat, encOpenParam.cbcrInterleave, mapType, FALSE);
  306. fbSize = VPU_GetFrameBufSize(ctx->handle, encOpenParam.coreIdx, fbStride, fbHeight, mapType, (FrameBufferFormat)encOpenParam.srcFormat, encOpenParam.cbcrInterleave, pDramConfig);
  307. fbAllocInfo.format = (FrameBufferFormat)encOpenParam.srcFormat;
  308. fbAllocInfo.cbcrInterleave = encOpenParam.cbcrInterleave;
  309. fbAllocInfo.mapType = mapType;
  310. fbAllocInfo.stride = fbStride;
  311. fbAllocInfo.height = fbHeight;
  312. fbAllocInfo.size = fbSize;
  313. fbAllocInfo.type = FB_TYPE_PPU;
  314. fbAllocInfo.num = ctx->fbCount.srcFbNum;
  315. fbAllocInfo.nv21 = encOpenParam.nv21;
  316. if (PRODUCT_ID_NOT_W_SERIES(ctx->productID)) {
  317. fbAllocInfo.lumaBitDepth = 8;
  318. fbAllocInfo.chromaBitDepth = 8;
  319. }
  320. if (FALSE == AllocFBMemory(encOpenParam.coreIdx, ctx->pFbSrcMem, ctx->pFbSrc, fbSize, ctx->fbCount.srcFbNum, ENC_SRC, ctx->handle->instIndex)) {
  321. VLOG(ERR, "failed to allocate source buffers\n");
  322. return FALSE;
  323. }
  324. ctx->srcFbAllocInfo = fbAllocInfo;
  325. ctx->currentBufferNumber = ctx->fbCount.srcFbNum;
  326. alloc_fbc:
  327. //Buffers for reconstructed frames
  328. osal_memset(&fbAllocInfo, 0x00, sizeof(FrameBufferAllocInfo));
  329. if (PRODUCT_ID_W_SERIES(ctx->productID)) {
  330. pDramConfig = NULL;
  331. if (ctx->encOpenParam.bitstreamFormat == STD_AVC) {
  332. fbWidth = VPU_ALIGN16(encOpenParam.picWidth);
  333. fbHeight = VPU_ALIGN16(encOpenParam.picHeight);
  334. } else {
  335. fbWidth = VPU_ALIGN8(encOpenParam.picWidth);
  336. fbHeight = VPU_ALIGN8(encOpenParam.picHeight);
  337. }
  338. if ((ctx->rotAngle != 0 || ctx->mirDir != 0) && !(ctx->rotAngle == 180 && ctx->mirDir == MIRDIR_HOR_VER)) {
  339. fbWidth = VPU_ALIGN32(encOpenParam.picWidth);
  340. fbHeight = VPU_ALIGN32(encOpenParam.picHeight);
  341. }
  342. //TODO if --> else if
  343. if (ctx->rotAngle == 90 || ctx->rotAngle == 270) {
  344. fbWidth = VPU_ALIGN32(encOpenParam.picHeight);
  345. fbHeight = VPU_ALIGN32(encOpenParam.picWidth);
  346. }
  347. if (WAVE521C_DUAL_CODE == (VPU_HANDLE_TO_ENCINFO(ctx->handle)->productCode)) {
  348. mapType = encOpenParam.EncStdParam.waveParam.internalBitDepth==8?COMPRESSED_FRAME_MAP_DUAL_CORE_8BIT:COMPRESSED_FRAME_MAP_DUAL_CORE_10BIT;
  349. } else {
  350. mapType = COMPRESSED_FRAME_MAP;
  351. }
  352. } else {
  353. //CODA
  354. pDramConfig = &dramConfig;
  355. fbWidth = encOpenParam.picWidth;
  356. fbHeight = encOpenParam.picHeight;
  357. if (90 == ctx->rotAngle || 270 == ctx->rotAngle) {
  358. fbWidth = encOpenParam.picHeight;
  359. fbHeight = encOpenParam.picWidth;
  360. }
  361. fbWidth = VPU_ALIGN16(fbWidth);
  362. fbHeight = VPU_ALIGN32(fbHeight);
  363. mapType = mapType;
  364. }
  365. fbStride = CalcStride(fbWidth, fbHeight, (FrameBufferFormat)encOpenParam.outputFormat, encOpenParam.cbcrInterleave, mapType, FALSE);
  366. fbSize = VPU_GetFrameBufSize(ctx->handle, encOpenParam.coreIdx, fbStride, fbHeight, mapType, (FrameBufferFormat)encOpenParam.outputFormat, encOpenParam.cbcrInterleave, pDramConfig);
  367. if (FALSE == AllocFBMemory(encOpenParam.coreIdx, ctx->pFbReconMem, ctx->pFbRecon, fbSize, ctx->fbCount.reconFbNum, ENC_FBC, ctx->handle->instIndex)) {
  368. VLOG(ERR, "failed to allocate recon buffers\n");
  369. return FALSE;
  370. }
  371. fbAllocInfo.stride = fbStride;
  372. fbAllocInfo.height = fbHeight;
  373. fbAllocInfo.type = FB_TYPE_CODEC;
  374. fbAllocInfo.num = ctx->fbCount.reconFbNum;
  375. ctx->reconFbAllocInfo = fbAllocInfo;
  376. return TRUE;
  377. }
  378. static BOOL PrepareYuvFeeder(ComponentImpl* com, BOOL* done)
  379. {
  380. CNMComponentParamRet ret;
  381. YuvFeederContext* ctx = (YuvFeederContext*)com->context;
  382. EncOpenParam* encOpenParam = &ctx->encOpenParam;
  383. BOOL success;
  384. Uint32 i;
  385. YuvFeeder yuvFeeder = NULL;
  386. YuvInfo yuvFeederInfo;
  387. *done = FALSE;
  388. // wait signal GET_PARAM_ENC_FRAME_BUF_NUM
  389. ret = ComponentGetParameter(com, com->sinkPort.connectedComponent, GET_PARAM_ENC_FRAME_BUF_NUM, &ctx->fbCount);
  390. if (ComponentParamReturnTest(ret, &success) == FALSE) {
  391. return success;
  392. }
  393. ret = ComponentGetParameter(com, com->sinkPort.connectedComponent, GET_PARAM_ENC_HANDLE, &ctx->handle);
  394. if (ComponentParamReturnTest(ret, &success) == FALSE) {
  395. return success;
  396. }
  397. if (FALSE == AllocateFrameBuffer(com)) {
  398. VLOG(ERR, "AllocateFramBuffer() error\n");
  399. return FALSE;
  400. }
  401. osal_memset(&yuvFeederInfo, 0x00, sizeof(YuvInfo));
  402. yuvFeederInfo.cbcrInterleave = encOpenParam->cbcrInterleave;
  403. yuvFeederInfo.nv21 = encOpenParam->nv21;
  404. yuvFeederInfo.packedFormat = encOpenParam->packedFormat;
  405. yuvFeederInfo.srcFormat = encOpenParam->srcFormat;
  406. yuvFeederInfo.srcPlanar = TRUE;
  407. yuvFeederInfo.srcStride = ctx->srcFbAllocInfo.stride;
  408. yuvFeederInfo.srcHeight = VPU_CEIL(encOpenParam->picHeight, 8);
  409. yuvFeeder = YuvFeeder_Create(ctx->yuvMode, ctx->inputPath, yuvFeederInfo);
  410. if (yuvFeeder == NULL) {
  411. VLOG(ERR, "YuvFeeder_Create Error\n");
  412. return FALSE;
  413. }
  414. ((AbstractYuvFeeder*)yuvFeeder)->impl->handle = ctx->handle;
  415. ctx->yuvFeeder = yuvFeeder;
  416. // Fill data into the input queue of the sink port.
  417. // The empty input queue of the sink port occurs a hangup problem in this example.
  418. while (Queue_Dequeue(com->sinkPort.inputQ) != NULL);
  419. for (i = 0; i < com->sinkPort.inputQ->size; i++) {
  420. PortContainerYuv defaultData;
  421. osal_memset(&defaultData, 0x00, sizeof(PortContainerYuv));
  422. defaultData.srcFbIndex = -1;
  423. if (i<ctx->fbCount.srcFbNum) {
  424. defaultData.srcFbIndex = i;
  425. Queue_Enqueue(com->sinkPort.inputQ, (void*)&defaultData);
  426. }
  427. }
  428. if (PRODUCT_ID_NOT_W_SERIES(ctx->productID)) {
  429. VPU_EncGiveCommand(ctx->handle, GET_TILEDMAP_CONFIG, &(ctx->mapConfig));
  430. }
  431. ctx->fbAllocated = TRUE;
  432. *done = TRUE;
  433. return TRUE;
  434. }
  435. static BOOL ExecuteYuvFeeder(ComponentImpl* com, PortContainer* in, PortContainer* out)
  436. {
  437. YuvFeederContext* ctx = (YuvFeederContext*)com->context;
  438. EncOpenParam* encOpenParam = &ctx->encOpenParam;
  439. int ret = 0;
  440. PortContainerYuv* sinkData = (PortContainerYuv*)out;
  441. void* extraFeedingInfo = NULL;
  442. #ifdef USE_FEEDING_METHOD_BUFFER
  443. extern void yuvBufferFeeder_SetData(YuvFeederImpl* impl, Uint8* address, Uint32 size);
  444. PortContainerExternal *input = NULL;
  445. input = (PortContainerExternal *)ComponentPortPeekData(&com->srcPort);
  446. if (input == NULL) {
  447. return TRUE;
  448. }
  449. AbstractYuvFeeder* absFeeder = (AbstractYuvFeeder*)ctx->yuvFeeder;
  450. yuvBufferFeeder_SetData(absFeeder->impl, (Uint8 *)input->pBuffer, input->nFilledLen);
  451. #endif
  452. out->reuse = FALSE;
  453. if (ctx->state == YUV_FEEDER_STATE_WAIT) {
  454. CNMComponentParamRet ParamRet;
  455. BOOL success, done = FALSE;
  456. out->reuse = TRUE;
  457. ParamRet = ComponentGetParameter(com, com->sinkPort.connectedComponent, GET_PARAM_ENC_FRAME_BUF_REGISTERED, &done);
  458. if (ComponentParamReturnTest(ParamRet, &success) == FALSE) {
  459. return success;
  460. }
  461. if (done == FALSE) return TRUE;
  462. ctx->state = YUV_FEEDER_STATE_FEEDING;
  463. out->reuse = FALSE;
  464. }
  465. if ( ctx->last ) {
  466. sinkData->last = TRUE;
  467. return TRUE;
  468. }
  469. sinkData->fb = ctx->pFbSrc[sinkData->srcFbIndex];
  470. if (PRODUCT_ID_NOT_W_SERIES(ctx->productID)) {
  471. extraFeedingInfo = &(ctx->mapConfig);
  472. }
  473. #ifdef USE_FEEDING_METHOD_BUFFER
  474. if ((input->nFlags & 0x1) == 0x1)
  475. {
  476. ctx->last = sinkData->last = TRUE;
  477. // return TRUE;
  478. }
  479. #else
  480. ctx->feedingNum++;
  481. if (ctx->feedingNum > ctx->frameOutNum && ctx->frameOutNum != -1) {
  482. ctx->last = sinkData->last = TRUE;
  483. return TRUE;
  484. }
  485. #endif
  486. if (ctx->MemoryOptimization)
  487. {
  488. Uint32 nBufferIndex = input->nBufferIndex;
  489. Uint32 totalCount = Queue_Get_Cnt(com->sinkPort.inputQ);
  490. Uint32 i = 0;
  491. ret = TRUE;
  492. for(i = 0; i < totalCount; i ++) {
  493. VLOG(INFO, "Input index = %d, Output index = %d\n", nBufferIndex, sinkData->srcFbIndex);
  494. if (nBufferIndex == sinkData->srcFbIndex) {
  495. break;
  496. }
  497. void *tmp = ComponentPortGetData(&com->sinkPort);
  498. Queue_Enqueue(com->sinkPort.inputQ, (void *)tmp);
  499. sinkData = (PortContainerYuv*)ComponentPortPeekData(&com->sinkPort);
  500. }
  501. if (i == totalCount)
  502. {
  503. VLOG(ERR, "Could not find match index!\n");
  504. ret = FALSE;
  505. }
  506. } else {
  507. VLOG(INFO, "Normal Feed\r\n");
  508. ret = YuvFeeder_Feed(ctx->yuvFeeder, encOpenParam->coreIdx, &sinkData->fb, encOpenParam->picWidth, encOpenParam->picHeight, sinkData->srcFbIndex, extraFeedingInfo);
  509. }
  510. if (ret == FALSE) {
  511. ctx->last = sinkData->last = TRUE;
  512. }
  513. #ifdef USE_FEEDING_METHOD_BUFFER
  514. if (input != NULL) {
  515. ComponentPortGetData(&com->srcPort);
  516. ComponentNotifyListeners(com, COMPONENT_EVENT_ENC_EMPTY_BUFFER_DONE, (void *)input);
  517. }
  518. if ((input->nFlags & 0x1) == 0x1)
  519. {
  520. while ((input = (PortContainerExternal*)ComponentPortGetData(&com->srcPort)) != NULL)
  521. {
  522. input->nFlags = 0x1;
  523. input->nFilledLen = 0;
  524. ComponentNotifyListeners(com, COMPONENT_EVENT_ENC_EMPTY_BUFFER_DONE, (void *)input);
  525. }
  526. }
  527. #endif
  528. return TRUE;
  529. }
  530. static void ReleaseYuvFeeder(ComponentImpl* com)
  531. {
  532. YuvFeederContext* ctx = (YuvFeederContext*)com->context;
  533. Uint32 i = 0;
  534. for (i = 0; i < ctx->fbCount.reconFbNum*2; i++) {
  535. if (ctx->pFbReconMem[i].size)
  536. vdi_free_dma_memory(ctx->encOpenParam.coreIdx, &ctx->pFbReconMem[i], ENC_FBC, ctx->handle->instIndex);
  537. }
  538. for (i = 0; i < ctx->fbCount.srcFbNum; i++) {
  539. if (ctx->pFbSrcMem[i].size)
  540. vdi_free_dma_memory(ctx->encOpenParam.coreIdx, &ctx->pFbSrcMem[i], ENC_SRC, ctx->handle->instIndex);
  541. if (ctx->pFbOffsetTblMem[i].size)
  542. vdi_free_dma_memory(ctx->encOpenParam.coreIdx, &ctx->pFbOffsetTblMem[i], ENC_FBCY_TBL, ctx->handle->instIndex);
  543. }
  544. }
  545. static BOOL DestroyYuvFeeder(ComponentImpl* com)
  546. {
  547. YuvFeederContext* ctx = (YuvFeederContext*)com->context;
  548. if (ctx->yuvFeeder) YuvFeeder_Destroy(ctx->yuvFeeder);
  549. osal_free(ctx);
  550. return TRUE;
  551. }
  552. static Component CreateYuvFeeder(ComponentImpl* com, CNMComponentConfig* componentParam)
  553. {
  554. YuvFeederContext* ctx;
  555. com->context = osal_malloc(sizeof(YuvFeederContext));
  556. ctx = (YuvFeederContext*)com->context;
  557. InitYuvFeederContext(ctx, componentParam);
  558. if ( ctx->encOpenParam.sourceBufCount > com->numSinkPortQueue )
  559. com->numSinkPortQueue = ctx->encOpenParam.sourceBufCount;//set requested sourceBufCount
  560. ctx->productID = componentParam->testEncConfig.productId;
  561. ctx->MemoryOptimization = TRUE;
  562. ctx->totalBufferNumber = 5;
  563. ctx->currentBufferNumber = 0;
  564. return (Component)com;
  565. }
  566. ComponentImpl yuvFeederComponentImpl = {
  567. "yuvfeeder",
  568. NULL,
  569. {0,},
  570. {0,},
  571. sizeof(PortContainerYuv),
  572. ENC_SRC_BUF_NUM, /* minimum feeder's numSinkPortQueue(relates to source buffer count)*/
  573. CreateYuvFeeder,
  574. GetParameterYuvFeeder,
  575. SetParameterYuvFeeder,
  576. PrepareYuvFeeder,
  577. ExecuteYuvFeeder,
  578. ReleaseYuvFeeder,
  579. DestroyYuvFeeder
  580. };