component_dec_renderer.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  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 "component.h"
  28. typedef struct {
  29. DecHandle handle; /*!<< A decoder handle */
  30. osal_thread_t threadHandle;
  31. TestDecConfig testDecConfig;
  32. Uint32 framebufStride;
  33. Uint32 displayPeriodTime;
  34. FrameBuffer pFrame[MAX_REG_FRAME];
  35. vpu_buffer_t pFbMem[MAX_REG_FRAME];
  36. BOOL enablePPU;
  37. FrameBuffer pPPUFrame[MAX_REG_FRAME];
  38. vpu_buffer_t pPPUFbMem[MAX_REG_FRAME];
  39. Queue* ppuQ;
  40. BOOL fbAllocated;
  41. ParamDecNeedFrameBufferNum fbCount;
  42. Queue* seqMemQ;
  43. osal_mutex_t lock;
  44. char outputPath[256];
  45. FILE* fpOutput[OUTPUT_FP_NUMBER];
  46. #ifdef USE_FEEDING_METHOD_BUFFER
  47. BOOL MemoryOptimization;
  48. BOOL useBufferExternal;
  49. Uint32 totalBufferNumber;
  50. Uint32 currentBufferNumber;
  51. FrameBuffer pLinearFrame[MAX_REG_FRAME];
  52. vpu_buffer_t pLinearFbMem[MAX_REG_FRAME];
  53. #endif
  54. } RendererContext;
  55. typedef struct SequenceMemInfo {
  56. Uint32 nonLinearNum;
  57. Uint32 linearNum;
  58. Uint32 remainingCount;
  59. Uint32 sequenceNo;
  60. vpu_buffer_t pFbMem[MAX_REG_FRAME];
  61. vpu_buffer_t vbFbcYTbl[MAX_REG_FRAME];
  62. vpu_buffer_t vbFbcCTbl[MAX_REG_FRAME];
  63. vpu_buffer_t vbTask;
  64. } SequenceMemInfo;
  65. static void FreeFrameBuffer(DecHandle handle, Uint32 idx, SequenceMemInfo* info)
  66. {
  67. Int32 coreIdx = handle->coreIdx;
  68. if (info->pFbMem[idx].size > 0) {
  69. if (idx < info->nonLinearNum)
  70. vdi_free_dma_memory(coreIdx, &info->pFbMem[idx], DEC_FBC, handle->instIndex);
  71. else
  72. vdi_free_dma_memory(coreIdx, &info->pFbMem[idx], DEC_FB_LINEAR, handle->instIndex);
  73. osal_memset((void*)&info->pFbMem[idx], 0x00, sizeof(vpu_buffer_t));
  74. }
  75. if (info->vbFbcYTbl[idx].size > 0) {
  76. vdi_free_dma_memory(coreIdx, &info->vbFbcYTbl[idx], DEC_FBCY_TBL, handle->instIndex);
  77. osal_memset((void*)&info->vbFbcYTbl[idx], 0x00, sizeof(vpu_buffer_t));
  78. }
  79. if (info->vbFbcCTbl[idx].size > 0) {
  80. vdi_free_dma_memory(coreIdx, &info->vbFbcCTbl[idx], DEC_FBCC_TBL, handle->instIndex);
  81. osal_memset((void*)&info->vbFbcCTbl[idx], 0x00, sizeof(vpu_buffer_t));
  82. }
  83. if (info->vbTask.size > 0) {
  84. vdi_free_dma_memory(coreIdx, &info->vbTask, DEC_TASK, handle->instIndex);
  85. osal_memset((void*)&info->vbTask, 0x00, sizeof(vpu_buffer_t));
  86. }
  87. }
  88. static BOOL SetParamFreeFrameBuffers(ComponentImpl* com, Uint32 fbFlags)
  89. {
  90. RendererContext* ctx = (RendererContext*)com->context;
  91. BOOL remainingFbs[MAX_REG_FRAME] = {0};
  92. Uint32 idx;
  93. Uint32 fbIndex;
  94. BOOL wtlEnable = ctx->testDecConfig.enableWTL;
  95. DecGetFramebufInfo curFbInfo;
  96. Uint32 coreIdx = ctx->testDecConfig.coreIdx;
  97. SequenceMemInfo seqMem = {0};
  98. osal_mutex_lock(ctx->lock);
  99. VPU_DecGiveCommand(ctx->handle, DEC_GET_FRAMEBUF_INFO, (void*)&curFbInfo);
  100. if (PRODUCT_ID_W_SERIES(ctx->testDecConfig.productId)) {
  101. for (idx=0; idx<MAX_GDI_IDX; idx++) {
  102. fbIndex = idx;
  103. if ((fbFlags>>idx) & 0x01) {
  104. if (wtlEnable == TRUE) {
  105. fbIndex = VPU_CONVERT_WTL_INDEX(ctx->handle, idx);
  106. }
  107. seqMem.remainingCount++;
  108. remainingFbs[fbIndex] = TRUE;
  109. }
  110. }
  111. }
  112. seqMem.nonLinearNum = ctx->fbCount.nonLinearNum;
  113. seqMem.linearNum = ctx->fbCount.linearNum;
  114. osal_memcpy((void*)seqMem.pFbMem, ctx->pFbMem, sizeof(ctx->pFbMem));
  115. osal_memcpy((void*)seqMem.vbFbcYTbl, curFbInfo.vbFbcYTbl, sizeof(curFbInfo.vbFbcYTbl));
  116. osal_memcpy((void*)seqMem.vbFbcCTbl, curFbInfo.vbFbcCTbl, sizeof(curFbInfo.vbFbcCTbl));
  117. osal_memcpy((void*)&seqMem.vbTask, &curFbInfo.vbTask, sizeof(curFbInfo.vbTask));
  118. for (idx=0; idx<MAX_REG_FRAME; idx++) {
  119. if (remainingFbs[idx] == FALSE) {
  120. FreeFrameBuffer(ctx->handle, idx, &seqMem);
  121. }
  122. // Free a mvcol buffer
  123. if(curFbInfo.vbMvCol[idx].size > 0) {
  124. vdi_free_dma_memory(coreIdx, &curFbInfo.vbMvCol[idx], DEC_MV, ctx->handle->instIndex);
  125. }
  126. }
  127. if (seqMem.remainingCount > 0) {
  128. Queue_Enqueue(ctx->seqMemQ, (void*)&seqMem);
  129. }
  130. ctx->fbAllocated = FALSE;
  131. osal_mutex_unlock(ctx->lock);
  132. return TRUE;
  133. }
  134. static BOOL ReallocateFrameBuffers(ComponentImpl* com, ParamDecReallocFB* param)
  135. {
  136. RendererContext* ctx = (RendererContext*)com->context;
  137. Int32 fbcIndex = param->compressedIdx;
  138. Int32 linearIndex = param->linearIdx;
  139. vpu_buffer_t* pFbMem = ctx->pFbMem;
  140. FrameBuffer* pFrame = ctx->pFrame;
  141. FrameBuffer* newFbs = param->newFbs;
  142. if (fbcIndex >= 0) {
  143. /* Release the FBC framebuffer */
  144. vdi_free_dma_memory(ctx->testDecConfig.coreIdx, &pFbMem[fbcIndex], DEC_FBC, ctx->handle->instIndex);
  145. osal_memset((void*)&pFbMem[fbcIndex], 0x00, sizeof(vpu_buffer_t));
  146. }
  147. if (linearIndex >= 0) {
  148. /* Release the linear framebuffer */
  149. vdi_free_dma_memory(ctx->testDecConfig.coreIdx, &pFbMem[linearIndex], DEC_FB_LINEAR, ctx->handle->instIndex);
  150. osal_memset((void*)&pFbMem[linearIndex], 0x00, sizeof(vpu_buffer_t));
  151. }
  152. if (fbcIndex >= 0) {
  153. newFbs[0].myIndex = fbcIndex;
  154. newFbs[0].width = param->width;
  155. newFbs[0].height = param->height;
  156. pFrame[fbcIndex] = newFbs[0];
  157. }
  158. if (linearIndex >= 0) {
  159. newFbs[1].myIndex = linearIndex;
  160. newFbs[1].width = param->width;
  161. newFbs[1].height = param->height;
  162. pFrame[linearIndex] = newFbs[1];
  163. }
  164. return TRUE;
  165. }
  166. #ifdef USE_FEEDING_METHOD_BUFFER
  167. void Render_DecClrDispFlag(void *context, int index)
  168. {
  169. RendererContext* ctx = (RendererContext*)context;
  170. VPU_DecClrDispFlag(ctx->handle, index);
  171. }
  172. #endif
  173. static void DisplayFrame(RendererContext* ctx, DecOutputInfo* result)
  174. {
  175. Int32 productID = ctx->testDecConfig.productId;
  176. osal_mutex_lock(ctx->lock);
  177. if (result->indexFrameDisplay >= 0) {
  178. DecInitialInfo initialInfo;
  179. VPU_DecGiveCommand(ctx->handle, DEC_GET_SEQ_INFO, &initialInfo);
  180. // No renderer device
  181. if (PRODUCT_ID_W_SERIES(productID) && initialInfo.sequenceNo != result->sequenceNo) {
  182. // Free a framebuffer of previous sequence
  183. SequenceMemInfo* memInfo = Queue_Peek(ctx->seqMemQ);
  184. if (memInfo != NULL) {
  185. FreeFrameBuffer(ctx->handle, result->indexFrameDisplay, memInfo);
  186. if (memInfo->remainingCount == 0) {
  187. VLOG(ERR, "%s:%d remainingCout must be greater than zero\n", __FUNCTION__, __LINE__);
  188. }
  189. memInfo->remainingCount--;
  190. if (memInfo->remainingCount == 0) {
  191. Queue_Dequeue(ctx->seqMemQ);
  192. }
  193. }
  194. }
  195. else {
  196. VPU_DecClrDispFlag(ctx->handle, result->indexFrameDisplay);
  197. }
  198. }
  199. osal_mutex_unlock(ctx->lock);
  200. }
  201. static BOOL FlushFrameBuffers(ComponentImpl* com, Uint32* flushedIndexes)
  202. {
  203. PortContainerDisplay* srcData;
  204. RendererContext* ctx = (RendererContext*)com->context;
  205. Int32 idx = 0;
  206. osal_mutex_lock(ctx->lock);
  207. if (flushedIndexes) *flushedIndexes = 0;
  208. while ((srcData=(PortContainerDisplay*)ComponentPortGetData(&com->srcPort)) != NULL) {
  209. idx = srcData->decInfo.indexFrameDisplay;
  210. if (0 <= idx) {
  211. VPU_DecClrDispFlag(ctx->handle, idx);
  212. if (flushedIndexes) *flushedIndexes |= (1<<idx);
  213. }
  214. ComponentPortSetData(&com->srcPort, (PortContainer*)srcData);
  215. }
  216. osal_mutex_unlock(ctx->lock);
  217. return TRUE;
  218. }
  219. #ifdef USE_FEEDING_METHOD_BUFFER
  220. void SetRenderTotalBufferNumber(ComponentImpl* com, Uint32 number)
  221. {
  222. RendererContext* ctx = (RendererContext*)com->context;
  223. ctx->fbCount.linearNum = ctx->totalBufferNumber = number;
  224. }
  225. Uint32 GetRenderTotalBufferNumber(ComponentImpl* com)
  226. {
  227. RendererContext* ctx = (RendererContext*)com->context;
  228. return ctx->totalBufferNumber;
  229. }
  230. void* AllocateFrameBuffer2(ComponentImpl* com, Uint32 size)
  231. {
  232. RendererContext* ctx = (RendererContext*)com->context;
  233. int i = 0;
  234. if (ctx->handle == NULL) {
  235. ctx->MemoryOptimization = FALSE;
  236. return NULL;
  237. }
  238. for (i = 0;i < MAX_REG_FRAME; i ++){
  239. if (ctx->pLinearFbMem[i].phys_addr == 0)
  240. {
  241. VLOG(INFO, "Found empty frame at index %d\r\n", i);
  242. break;
  243. }
  244. }
  245. if (i == MAX_REG_FRAME)
  246. {
  247. VLOG(ERR, "Could not found empty frame at index\r\n");
  248. return NULL;
  249. }
  250. // TODO: Adjust ctx->fbCount
  251. void *ret = AllocateDecFrameBuffer2(ctx->handle, &ctx->testDecConfig, size, &ctx->pLinearFrame[i], &ctx->pLinearFbMem[i]);
  252. if (!ret)
  253. ComponentNotifyListeners(com, COMPONENT_EVENT_DEC_INSUFFIC_RESOURCE, NULL);
  254. else
  255. ctx->currentBufferNumber ++;
  256. return ret;
  257. }
  258. void FreeFrameBuffer2(ComponentImpl* com, void* pBuffer)
  259. {
  260. RendererContext* ctx = (RendererContext*)com->context;
  261. int i = 0;
  262. if (ctx->handle == NULL) {
  263. ctx->MemoryOptimization = FALSE;
  264. return;
  265. }
  266. for (i = 0;i < MAX_REG_FRAME; i ++){
  267. if (ctx->pLinearFbMem[i].virt_addr == (unsigned long)pBuffer)
  268. {
  269. VLOG(INFO, "free dmabuffer at index %d\r\n", i);
  270. vdi_free_dma_memory(ctx->testDecConfig.coreIdx, &ctx->pLinearFbMem[i], DEC_FB_LINEAR, ctx->handle->instIndex);
  271. return;
  272. }
  273. }
  274. VLOG(ERR, "FreeFrameBuffer2 can not find buffer %p\r\n", pBuffer);
  275. }
  276. Uint64 RemapDMABuffer(ComponentImpl* com, Uint64 virtAddress, Uint32 size)
  277. {
  278. RendererContext* ctx;
  279. if (!com)
  280. return 0;
  281. ctx = (RendererContext*)com->context;
  282. if (ctx->handle == NULL) {
  283. ctx->MemoryOptimization = FALSE;
  284. return 0;
  285. }
  286. return RemapVaddr(ctx->handle, virtAddress, size);
  287. }
  288. BOOL AttachDMABuffer(ComponentImpl* com, Uint64 virtAddress, Uint32 size)
  289. {
  290. BOOL ret = FALSE;
  291. RendererContext* ctx = (RendererContext*)com->context;
  292. int i = 0;
  293. if (ctx->handle == NULL) {
  294. ctx->MemoryOptimization = FALSE;
  295. return FALSE;
  296. }
  297. for (i = 0;i < MAX_REG_FRAME; i ++){
  298. if (ctx->pLinearFbMem[i].phys_addr == 0)
  299. {
  300. VLOG(INFO, "Found empty frame at index %d\r\n", i);
  301. break;
  302. }
  303. }
  304. if (i == MAX_REG_FRAME)
  305. {
  306. VLOG(ERR, "Could not found empty frame at index\r\n");
  307. return FALSE;
  308. }
  309. ret = AttachDecDMABuffer(ctx->handle, &ctx->testDecConfig, virtAddress, size, &ctx->pLinearFrame[i], &ctx->pLinearFbMem[i]);
  310. ctx->currentBufferNumber ++;
  311. ctx->useBufferExternal = TRUE;
  312. return ret;
  313. }
  314. #endif
  315. static BOOL AllocateFrameBuffer(ComponentImpl* com)
  316. {
  317. RendererContext* ctx = (RendererContext*)com->context;
  318. BOOL success;
  319. Uint32 compressedNum;
  320. Uint32 linearNum;
  321. CNMComponentParamRet ret;
  322. ret = ComponentGetParameter(com, com->srcPort.connectedComponent, GET_PARAM_DEC_FRAME_BUF_NUM, &ctx->fbCount);
  323. if (ComponentParamReturnTest(ret, &success) == FALSE) {
  324. return success;
  325. }
  326. osal_memset((void*)ctx->pFbMem, 0x00, sizeof(ctx->pFbMem));
  327. osal_memset((void*)ctx->pFrame, 0x00, sizeof(ctx->pFrame));
  328. #ifdef USE_FEEDING_METHOD_BUFFER
  329. ctx->totalBufferNumber = ctx->fbCount.linearNum;
  330. #endif
  331. compressedNum = ctx->fbCount.nonLinearNum;
  332. linearNum = ctx->fbCount.linearNum;
  333. if (compressedNum == 0 && linearNum == 0) {
  334. VLOG(ERR, "%s:%d The number of framebuffers are zero. compressed %d, linear: %d\n",
  335. __FUNCTION__, __LINE__, compressedNum, linearNum);
  336. return FALSE;
  337. }
  338. #ifdef USE_FEEDING_METHOD_BUFFER
  339. if (ctx->MemoryOptimization){
  340. linearNum = 0;
  341. }
  342. #endif
  343. if (AllocateDecFrameBuffer(ctx->handle, &ctx->testDecConfig, compressedNum, linearNum, ctx->pFrame, ctx->pFbMem, &ctx->framebufStride) == FALSE) {
  344. VLOG(INFO, "%s:%d Failed to AllocateDecFrameBuffer()\n", __FUNCTION__, __LINE__);
  345. #ifdef USE_FEEDING_METHOD_BUFFER
  346. ComponentNotifyListeners(com, COMPONENT_EVENT_DEC_INSUFFIC_RESOURCE, NULL);
  347. #endif
  348. return FALSE;
  349. }
  350. ctx->fbAllocated = TRUE;
  351. return TRUE;
  352. }
  353. static BOOL AllocatePPUFrameBuffer(ComponentImpl* com)
  354. {
  355. RendererContext* ctx = (RendererContext*)com->context;
  356. TestDecConfig* decConfig = &(ctx->testDecConfig);
  357. BOOL resOf = FALSE;
  358. osal_memset((void*)ctx->pPPUFbMem, 0x00, sizeof(ctx->pPPUFbMem));
  359. osal_memset((void*)ctx->pPPUFrame, 0x00, sizeof(ctx->pPPUFrame));
  360. if(PRODUCT_ID_960 == decConfig->productId || PRODUCT_ID_980 == decConfig->productId) {
  361. if (FALSE == Coda9AllocateDecPPUFrameBuffer(&(ctx->enablePPU), ctx->handle, &ctx->testDecConfig, ctx->pPPUFrame, ctx->pPPUFbMem, ctx->ppuQ)) {
  362. VLOG(INFO, "%s:%d Failed to Coda9AllocateDecPPUFrameBuffer()\n", __FUNCTION__, __LINE__);
  363. }
  364. resOf = TRUE;
  365. }
  366. return resOf;
  367. }
  368. static CNMComponentParamRet GetParameterRenderer(ComponentImpl* from, ComponentImpl* com, GetParameterCMD commandType, void* data)
  369. {
  370. RendererContext* ctx = (RendererContext*)com->context;
  371. ParamDecFrameBuffer* allocFb = NULL;
  372. ParamDecPPUFrameBuffer* allocPPUFb = NULL;
  373. PortContainer* container;
  374. if (ctx->fbAllocated == FALSE) return CNM_COMPONENT_PARAM_NOT_READY;
  375. switch(commandType) {
  376. case GET_PARAM_COM_IS_CONTAINER_CONUSUMED:
  377. // This query command is sent from the comonponent core.
  378. // If input data are consumed in sequence, it should return TRUE through PortContainer::consumed.
  379. container = (PortContainer*)data;
  380. container->consumed = TRUE;
  381. break;
  382. case GET_PARAM_RENDERER_FRAME_BUF:
  383. #ifdef USE_FEEDING_METHOD_BUFFER
  384. if (ctx->MemoryOptimization){
  385. if (ctx->currentBufferNumber < ctx->totalBufferNumber) return CNM_COMPONENT_PARAM_NOT_READY;
  386. int i = 0;
  387. for (i = 0;i < MAX_REG_FRAME; i ++) {
  388. if (ctx->pFbMem[i].phys_addr == 0) {
  389. break;
  390. }
  391. }
  392. VLOG(INFO, "The start index of LINEAR buffer = %d\n", i);
  393. for (int j = 0; j < MAX_REG_FRAME; j ++) {
  394. if (ctx->pLinearFbMem[j].phys_addr != 0) {
  395. memcpy(&ctx->pFbMem[i + j], &ctx->pLinearFbMem[j], sizeof(vpu_buffer_t));
  396. memcpy(&ctx->pFrame[i + j], &ctx->pLinearFrame[j], sizeof(FrameBuffer));
  397. }
  398. }
  399. }
  400. //TODO: Adjust Liner number
  401. #endif
  402. allocFb = (ParamDecFrameBuffer*)data;
  403. allocFb->stride = ctx->framebufStride;
  404. allocFb->linearNum = ctx->fbCount.linearNum;
  405. allocFb->nonLinearNum = ctx->fbCount.nonLinearNum;
  406. allocFb->fb = ctx->pFrame;
  407. break;
  408. case GET_PARAM_RENDERER_PPU_FRAME_BUF:
  409. allocPPUFb = (ParamDecPPUFrameBuffer*)data;
  410. if (TRUE == AllocatePPUFrameBuffer(com)) {
  411. allocPPUFb->enablePPU = ctx->enablePPU;
  412. allocPPUFb->ppuQ = ctx->ppuQ;
  413. allocPPUFb->fb = ctx->pPPUFrame;
  414. }
  415. else {
  416. allocPPUFb->enablePPU = FALSE;
  417. allocPPUFb->ppuQ = NULL;
  418. allocPPUFb->fb = NULL;
  419. }
  420. break;
  421. default:
  422. return CNM_COMPONENT_PARAM_NOT_FOUND;
  423. }
  424. return CNM_COMPONENT_PARAM_SUCCESS;
  425. }
  426. static CNMComponentParamRet SetParameterRenderer(ComponentImpl* from, ComponentImpl* com, SetParameterCMD commandType, void* data)
  427. {
  428. RendererContext* ctx = (RendererContext*)com->context;
  429. BOOL result = TRUE;
  430. UNREFERENCED_PARAMETER(ctx);
  431. switch(commandType) {
  432. case SET_PARAM_RENDERER_REALLOC_FRAMEBUFFER:
  433. result = ReallocateFrameBuffers(com, (ParamDecReallocFB*)data);
  434. break;
  435. case SET_PARAM_RENDERER_FREE_FRAMEBUFFERS:
  436. result = SetParamFreeFrameBuffers(com, *(Uint32*)data);
  437. break;
  438. case SET_PARAM_RENDERER_FLUSH:
  439. result = FlushFrameBuffers(com, (Uint32*)data);
  440. break;
  441. case SET_PARAM_RENDERER_ALLOC_FRAMEBUFFERS:
  442. result = AllocateFrameBuffer(com);
  443. break;
  444. case SET_PARAM_RENDERER_CHANGE_COM_STATE:
  445. result = ComponentChangeState(com, *(Uint32*)data);
  446. break;
  447. default:
  448. return CNM_COMPONENT_PARAM_NOT_FOUND;
  449. }
  450. if (result == TRUE) return CNM_COMPONENT_PARAM_SUCCESS;
  451. else return CNM_COMPONENT_PARAM_FAILURE;
  452. }
  453. static BOOL ExecuteRenderer(ComponentImpl* com, PortContainer* in, PortContainer* out)
  454. {
  455. RendererContext* ctx = (RendererContext*)com->context;
  456. PortContainerDisplay* srcData = (PortContainerDisplay*)in;
  457. Int32 indexFrameDisplay;
  458. TestDecConfig* decConfig = &ctx->testDecConfig;
  459. if (TRUE == com->pause) {
  460. return TRUE;
  461. }
  462. #ifdef USE_FEEDING_METHOD_BUFFER
  463. PortContainerExternal *output;
  464. if (!Queue_Get_Cnt(com->sinkPort.inputQ)) {
  465. in->reuse = TRUE;
  466. return TRUE;
  467. }
  468. #endif
  469. in->reuse = TRUE;
  470. indexFrameDisplay = srcData->decInfo.indexFrameDisplay;
  471. if (indexFrameDisplay == DISPLAY_IDX_FLAG_SEQ_END) {
  472. #ifdef USE_FEEDING_METHOD_BUFFER
  473. while ((output = (PortContainerExternal*)ComponentPortGetData(&com->sinkPort)) != NULL)
  474. {
  475. output->nFlags = 0x1;
  476. output->nFilledLen = 0;
  477. ComponentNotifyListeners(com, COMPONENT_EVENT_DEC_FILL_BUFFER_DONE, (void *)output);
  478. }
  479. com->terminate = TRUE;
  480. ComponentNotifyListeners(com, COMPONENT_EVENT_DEC_DECODED_ALL, NULL);
  481. #else
  482. com->terminate = TRUE;
  483. #endif
  484. }
  485. else if (indexFrameDisplay >= 0) {
  486. #ifdef USE_FEEDING_METHOD_BUFFER
  487. VpuRect rcDisplay = {0,};
  488. TiledMapType mapType = srcData->decInfo.dispFrame.mapType;
  489. rcDisplay.right = srcData->decInfo.dispPicWidth;
  490. rcDisplay.bottom = srcData->decInfo.dispPicHeight;
  491. Uint32 width, height, bpp;
  492. size_t sizeYuv;
  493. Uint32 count = 0, total_count = 0;
  494. output = (PortContainerExternal*)ComponentPortGetData(&com->sinkPort);
  495. if (output == NULL) {
  496. in->reuse = TRUE;
  497. return TRUE;
  498. }
  499. if (ctx->MemoryOptimization){
  500. uint8_t *dmaBuffer = NULL;
  501. if (FALSE ==GetYUVFromFrameBuffer2(NULL, &dmaBuffer, output->nFilledLen, ctx->handle, &srcData->decInfo.dispFrame, rcDisplay, &width, &height, &bpp, &sizeYuv)) {
  502. VLOG(ERR, "GetYUVFromFrameBuffer2 FAIL!\n");
  503. }
  504. total_count = Queue_Get_Cnt(com->sinkPort.inputQ) + 1;
  505. VLOG(INFO, "pBuffer = %p, dmaBuffer = %p, index = %d/%d\n", output->pBuffer, dmaBuffer, count, total_count);
  506. while (output->pBuffer != dmaBuffer)
  507. {
  508. Queue_Enqueue(com->sinkPort.inputQ, (void *)output);
  509. count ++;
  510. if (count >= total_count)
  511. {
  512. VLOG(TRACE, "A wrong Frame Found\n");
  513. output = NULL;
  514. break;
  515. }
  516. output = (PortContainerExternal*)ComponentPortGetData(&com->sinkPort);
  517. VLOG(INFO, "pBuffer = %p, dmaBuffer = %p, index = %d/%d\n", output->pBuffer, dmaBuffer, count, total_count);
  518. }
  519. } else {
  520. if (FALSE ==GetYUVFromFrameBuffer2(output->pBuffer, NULL, output->nFilledLen, ctx->handle, &srcData->decInfo.dispFrame, rcDisplay, &width, &height, &bpp, &sizeYuv)) {
  521. VLOG(ERR, "GetYUVFromFrameBuffer2 FAIL!\n");
  522. }
  523. VPU_DecClrDispFlag(ctx->handle, srcData->decInfo.indexFrameDisplay);
  524. }
  525. if (output){
  526. output->nFilledLen = sizeYuv;
  527. output->index = srcData->decInfo.indexFrameDisplay;
  528. if(srcData->last)
  529. output->nFlags |= 0x1;
  530. ComponentNotifyListeners(com, COMPONENT_EVENT_DEC_FILL_BUFFER_DONE, (void *)output);
  531. }
  532. (void)DisplayFrame;
  533. (void)decConfig;
  534. (void)mapType;
  535. #else
  536. if (strlen((const char*)decConfig->outputPath) > 0) {
  537. VpuRect rcDisplay = {0,};
  538. TiledMapType mapType = srcData->decInfo.dispFrame.mapType;
  539. rcDisplay.right = srcData->decInfo.dispPicWidth;
  540. rcDisplay.bottom = srcData->decInfo.dispPicHeight;
  541. if (decConfig->scaleDownWidth > 0 || decConfig->scaleDownHeight > 0) {
  542. rcDisplay.right = VPU_CEIL(srcData->decInfo.dispPicWidth, 16);
  543. }
  544. if (ctx->fpOutput[0] == NULL) {
  545. if (OpenDisplayBufferFile(decConfig->bitFormat, decConfig->outputPath, rcDisplay, mapType, ctx->fpOutput) == FALSE) {
  546. return FALSE;
  547. }
  548. }
  549. SaveDisplayBufferToFile(ctx->handle, decConfig->bitFormat, srcData->decInfo.dispFrame, rcDisplay, ctx->fpOutput);
  550. }
  551. DisplayFrame(ctx, &srcData->decInfo);
  552. osal_msleep(ctx->displayPeriodTime);
  553. #endif
  554. }
  555. srcData->consumed = TRUE;
  556. srcData->reuse = FALSE;
  557. if (srcData->last == TRUE) {
  558. #ifdef USE_FEEDING_METHOD_BUFFER
  559. ComponentNotifyListeners(com, COMPONENT_EVENT_DEC_DECODED_ALL, NULL);
  560. while ((output = (PortContainerExternal*)ComponentPortGetData(&com->sinkPort)) != NULL)
  561. {
  562. output->nFlags = 0x1;
  563. output->nFilledLen = 0;
  564. VLOG(INFO, "Flush output port\r\n");
  565. ComponentNotifyListeners(com, COMPONENT_EVENT_DEC_FILL_BUFFER_DONE, (void *)output);
  566. }
  567. #endif
  568. com->terminate = TRUE;
  569. }
  570. return TRUE;
  571. }
  572. static BOOL PrepareRenderer(ComponentImpl* com, BOOL* done)
  573. {
  574. RendererContext* ctx = (RendererContext*)com->context;
  575. BOOL ret;
  576. if (ctx->handle == NULL) {
  577. CNMComponentParamRet paramRet;
  578. BOOL success;
  579. paramRet = ComponentGetParameter(com, com->srcPort.connectedComponent, GET_PARAM_DEC_HANDLE, &ctx->handle);
  580. if (ComponentParamReturnTest(paramRet, &success) == FALSE) {
  581. return success;
  582. }
  583. }
  584. if ((ret=AllocateFrameBuffer(com)) == FALSE || ctx->fbAllocated == FALSE) {
  585. return ret;
  586. }
  587. #ifdef USE_FEEDING_METHOD_BUFFER
  588. ComponentNotifyListeners(com, COMPONENT_EVENT_DEC_REGISTER_FB, &ctx->pFrame[0]);
  589. #endif
  590. // Nothing to do
  591. *done = TRUE;
  592. return TRUE;
  593. }
  594. static void ReleaseRenderer(ComponentImpl* com)
  595. {
  596. RendererContext* ctx = (RendererContext*)com->context;
  597. Uint32 coreIdx = ctx->testDecConfig.coreIdx;
  598. Uint32 i;
  599. #ifdef USE_FEEDING_METHOD_BUFFER
  600. for (i=0; i<MAX_REG_FRAME; i++) {
  601. if (ctx->pFbMem[i].size) {
  602. if (i < ctx->fbCount.nonLinearNum)
  603. {
  604. vdi_free_dma_memory(coreIdx, &ctx->pFbMem[i], DEC_FBC, ctx->handle->instIndex);
  605. }else{
  606. if(!ctx->MemoryOptimization)
  607. vdi_free_dma_memory(coreIdx, &ctx->pFbMem[i], DEC_FB_LINEAR, ctx->handle->instIndex);
  608. }
  609. }
  610. }
  611. #else
  612. for (i=0; i<MAX_REG_FRAME; i++) {
  613. if (ctx->pFbMem[i].size) {
  614. if (i < ctx->fbCount.linearNum)
  615. vdi_free_dma_memory(coreIdx, &ctx->pFbMem[i], DEC_FBC, ctx->handle->instIndex);
  616. else
  617. vdi_free_dma_memory(coreIdx, &ctx->pFbMem[i], DEC_FB_LINEAR, ctx->handle->instIndex);
  618. }
  619. }
  620. #endif
  621. for (i=0; i<MAX_REG_FRAME; i++) {
  622. if (ctx->pPPUFrame[i].size) vdi_free_dma_memory(coreIdx, &ctx->pPPUFbMem[i], DEC_ETC, ctx->handle->instIndex);
  623. }
  624. }
  625. static BOOL DestroyRenderer(ComponentImpl* com)
  626. {
  627. RendererContext* ctx = (RendererContext*)com->context;
  628. CloseDisplayBufferFile(ctx->fpOutput);
  629. Queue_Destroy(ctx->seqMemQ);
  630. Queue_Destroy(ctx->ppuQ);
  631. osal_mutex_destroy(ctx->lock);
  632. osal_free(ctx);
  633. return TRUE;
  634. }
  635. static Component CreateRenderer(ComponentImpl* com, CNMComponentConfig* componentParam)
  636. {
  637. RendererContext* ctx;
  638. com->context = (RendererContext*)osal_malloc(sizeof(RendererContext));
  639. osal_memset(com->context, 0, sizeof(RendererContext));
  640. ctx = com->context;
  641. #ifdef USE_FEEDING_METHOD_BUFFER
  642. osal_memset(ctx->pLinearFbMem, 0, sizeof(ctx->pLinearFbMem));
  643. osal_memset(ctx->pLinearFrame, 0, sizeof(ctx->pLinearFrame));
  644. #endif
  645. osal_memcpy((void*)&ctx->testDecConfig, (void*)&componentParam->testDecConfig, sizeof(TestDecConfig));
  646. if (componentParam->testDecConfig.fps > 0)
  647. ctx->displayPeriodTime = (1000 / componentParam->testDecConfig.fps);
  648. else
  649. ctx->displayPeriodTime = 1000/30;
  650. ctx->fbAllocated = FALSE;
  651. ctx->seqMemQ = Queue_Create(10, sizeof(SequenceMemInfo));
  652. ctx->lock = osal_mutex_create();
  653. ctx->ppuQ = Queue_Create(MAX_REG_FRAME, sizeof(FrameBuffer));
  654. #ifdef USE_FEEDING_METHOD_BUFFER
  655. if (componentParam->MemoryOptimization)
  656. {
  657. ctx->MemoryOptimization = TRUE;
  658. }
  659. else
  660. {
  661. ctx->MemoryOptimization = FALSE;
  662. }
  663. ctx->totalBufferNumber = 12;
  664. ctx->currentBufferNumber = 0;
  665. #endif
  666. return (Component)com;
  667. }
  668. ComponentImpl rendererComponentImpl = {
  669. "renderer",
  670. NULL,
  671. {0,},
  672. {0,},
  673. sizeof(PortContainer), /* No output */
  674. 5,
  675. CreateRenderer,
  676. GetParameterRenderer,
  677. SetParameterRenderer,
  678. PrepareRenderer,
  679. ExecuteRenderer,
  680. ReleaseRenderer,
  681. DestroyRenderer
  682. };