component_dec_renderer.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  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. Uint64 RemapDMABuffer(ComponentImpl* com, Uint64 virtAddress, Uint32 size)
  259. {
  260. RendererContext* ctx;
  261. if (!com)
  262. return 0;
  263. ctx = (RendererContext*)com->context;
  264. if (ctx->handle == NULL) {
  265. ctx->MemoryOptimization = FALSE;
  266. return 0;
  267. }
  268. return RemapVaddr(ctx->handle, virtAddress, size);
  269. }
  270. BOOL AttachDMABuffer(ComponentImpl* com, Uint64 virtAddress, Uint32 size)
  271. {
  272. BOOL ret = FALSE;
  273. RendererContext* ctx = (RendererContext*)com->context;
  274. int i = 0;
  275. if (ctx->handle == NULL) {
  276. ctx->MemoryOptimization = FALSE;
  277. return FALSE;
  278. }
  279. for (i = 0;i < MAX_REG_FRAME; i ++){
  280. if (ctx->pLinearFbMem[i].phys_addr == 0)
  281. {
  282. VLOG(INFO, "Found empty frame at index %d\r\n", i);
  283. break;
  284. }
  285. }
  286. if (i == MAX_REG_FRAME)
  287. {
  288. VLOG(ERR, "Could not found empty frame at index\r\n");
  289. return FALSE;
  290. }
  291. ret = AttachDecDMABuffer(ctx->handle, &ctx->testDecConfig, virtAddress, size, &ctx->pLinearFrame[i], &ctx->pLinearFbMem[i]);
  292. ctx->currentBufferNumber ++;
  293. ctx->useBufferExternal = TRUE;
  294. return ret;
  295. }
  296. #endif
  297. static BOOL AllocateFrameBuffer(ComponentImpl* com)
  298. {
  299. RendererContext* ctx = (RendererContext*)com->context;
  300. BOOL success;
  301. Uint32 compressedNum;
  302. Uint32 linearNum;
  303. CNMComponentParamRet ret;
  304. ret = ComponentGetParameter(com, com->srcPort.connectedComponent, GET_PARAM_DEC_FRAME_BUF_NUM, &ctx->fbCount);
  305. if (ComponentParamReturnTest(ret, &success) == FALSE) {
  306. return success;
  307. }
  308. osal_memset((void*)ctx->pFbMem, 0x00, sizeof(ctx->pFbMem));
  309. osal_memset((void*)ctx->pFrame, 0x00, sizeof(ctx->pFrame));
  310. #ifdef USE_FEEDING_METHOD_BUFFER
  311. ctx->totalBufferNumber = ctx->fbCount.linearNum;
  312. #endif
  313. compressedNum = ctx->fbCount.nonLinearNum;
  314. linearNum = ctx->fbCount.linearNum;
  315. if (compressedNum == 0 && linearNum == 0) {
  316. VLOG(ERR, "%s:%d The number of framebuffers are zero. compressed %d, linear: %d\n",
  317. __FUNCTION__, __LINE__, compressedNum, linearNum);
  318. return FALSE;
  319. }
  320. #ifdef USE_FEEDING_METHOD_BUFFER
  321. if (ctx->MemoryOptimization){
  322. linearNum = 0;
  323. }
  324. #endif
  325. if (AllocateDecFrameBuffer(ctx->handle, &ctx->testDecConfig, compressedNum, linearNum, ctx->pFrame, ctx->pFbMem, &ctx->framebufStride) == FALSE) {
  326. VLOG(INFO, "%s:%d Failed to AllocateDecFrameBuffer()\n", __FUNCTION__, __LINE__);
  327. #ifdef USE_FEEDING_METHOD_BUFFER
  328. ComponentNotifyListeners(com, COMPONENT_EVENT_DEC_INSUFFIC_RESOURCE, NULL);
  329. #endif
  330. return FALSE;
  331. }
  332. ctx->fbAllocated = TRUE;
  333. return TRUE;
  334. }
  335. static BOOL AllocatePPUFrameBuffer(ComponentImpl* com)
  336. {
  337. RendererContext* ctx = (RendererContext*)com->context;
  338. TestDecConfig* decConfig = &(ctx->testDecConfig);
  339. BOOL resOf = FALSE;
  340. osal_memset((void*)ctx->pPPUFbMem, 0x00, sizeof(ctx->pPPUFbMem));
  341. osal_memset((void*)ctx->pPPUFrame, 0x00, sizeof(ctx->pPPUFrame));
  342. if(PRODUCT_ID_960 == decConfig->productId || PRODUCT_ID_980 == decConfig->productId) {
  343. if (FALSE == Coda9AllocateDecPPUFrameBuffer(&(ctx->enablePPU), ctx->handle, &ctx->testDecConfig, ctx->pPPUFrame, ctx->pPPUFbMem, ctx->ppuQ)) {
  344. VLOG(INFO, "%s:%d Failed to Coda9AllocateDecPPUFrameBuffer()\n", __FUNCTION__, __LINE__);
  345. }
  346. resOf = TRUE;
  347. }
  348. return resOf;
  349. }
  350. static CNMComponentParamRet GetParameterRenderer(ComponentImpl* from, ComponentImpl* com, GetParameterCMD commandType, void* data)
  351. {
  352. RendererContext* ctx = (RendererContext*)com->context;
  353. ParamDecFrameBuffer* allocFb = NULL;
  354. ParamDecPPUFrameBuffer* allocPPUFb = NULL;
  355. PortContainer* container;
  356. if (ctx->fbAllocated == FALSE) return CNM_COMPONENT_PARAM_NOT_READY;
  357. switch(commandType) {
  358. case GET_PARAM_COM_IS_CONTAINER_CONUSUMED:
  359. // This query command is sent from the comonponent core.
  360. // If input data are consumed in sequence, it should return TRUE through PortContainer::consumed.
  361. container = (PortContainer*)data;
  362. container->consumed = TRUE;
  363. break;
  364. case GET_PARAM_RENDERER_FRAME_BUF:
  365. #ifdef USE_FEEDING_METHOD_BUFFER
  366. if (ctx->MemoryOptimization){
  367. if (ctx->currentBufferNumber < ctx->totalBufferNumber) return CNM_COMPONENT_PARAM_NOT_READY;
  368. int i = 0;
  369. for (i = 0;i < MAX_REG_FRAME; i ++) {
  370. if (ctx->pFbMem[i].phys_addr == 0) {
  371. break;
  372. }
  373. }
  374. VLOG(INFO, "The start index of LINEAR buffer = %d\n", i);
  375. for (int j = 0; j < MAX_REG_FRAME; j ++) {
  376. if (ctx->pLinearFbMem[j].phys_addr != 0) {
  377. memcpy(&ctx->pFbMem[i + j], &ctx->pLinearFbMem[j], sizeof(vpu_buffer_t));
  378. memcpy(&ctx->pFrame[i + j], &ctx->pLinearFrame[j], sizeof(FrameBuffer));
  379. }
  380. }
  381. }
  382. //TODO: Adjust Liner number
  383. #endif
  384. allocFb = (ParamDecFrameBuffer*)data;
  385. allocFb->stride = ctx->framebufStride;
  386. allocFb->linearNum = ctx->fbCount.linearNum;
  387. allocFb->nonLinearNum = ctx->fbCount.nonLinearNum;
  388. allocFb->fb = ctx->pFrame;
  389. break;
  390. case GET_PARAM_RENDERER_PPU_FRAME_BUF:
  391. allocPPUFb = (ParamDecPPUFrameBuffer*)data;
  392. if (TRUE == AllocatePPUFrameBuffer(com)) {
  393. allocPPUFb->enablePPU = ctx->enablePPU;
  394. allocPPUFb->ppuQ = ctx->ppuQ;
  395. allocPPUFb->fb = ctx->pPPUFrame;
  396. }
  397. else {
  398. allocPPUFb->enablePPU = FALSE;
  399. allocPPUFb->ppuQ = NULL;
  400. allocPPUFb->fb = NULL;
  401. }
  402. break;
  403. default:
  404. return CNM_COMPONENT_PARAM_NOT_FOUND;
  405. }
  406. return CNM_COMPONENT_PARAM_SUCCESS;
  407. }
  408. static CNMComponentParamRet SetParameterRenderer(ComponentImpl* from, ComponentImpl* com, SetParameterCMD commandType, void* data)
  409. {
  410. RendererContext* ctx = (RendererContext*)com->context;
  411. BOOL result = TRUE;
  412. UNREFERENCED_PARAMETER(ctx);
  413. switch(commandType) {
  414. case SET_PARAM_RENDERER_REALLOC_FRAMEBUFFER:
  415. result = ReallocateFrameBuffers(com, (ParamDecReallocFB*)data);
  416. break;
  417. case SET_PARAM_RENDERER_FREE_FRAMEBUFFERS:
  418. result = SetParamFreeFrameBuffers(com, *(Uint32*)data);
  419. break;
  420. case SET_PARAM_RENDERER_FLUSH:
  421. result = FlushFrameBuffers(com, (Uint32*)data);
  422. break;
  423. case SET_PARAM_RENDERER_ALLOC_FRAMEBUFFERS:
  424. result = AllocateFrameBuffer(com);
  425. break;
  426. case SET_PARAM_RENDERER_CHANGE_COM_STATE:
  427. result = ComponentChangeState(com, *(Uint32*)data);
  428. break;
  429. default:
  430. return CNM_COMPONENT_PARAM_NOT_FOUND;
  431. }
  432. if (result == TRUE) return CNM_COMPONENT_PARAM_SUCCESS;
  433. else return CNM_COMPONENT_PARAM_FAILURE;
  434. }
  435. static BOOL ExecuteRenderer(ComponentImpl* com, PortContainer* in, PortContainer* out)
  436. {
  437. RendererContext* ctx = (RendererContext*)com->context;
  438. PortContainerDisplay* srcData = (PortContainerDisplay*)in;
  439. Int32 indexFrameDisplay;
  440. TestDecConfig* decConfig = &ctx->testDecConfig;
  441. if (TRUE == com->pause) {
  442. return TRUE;
  443. }
  444. #ifdef USE_FEEDING_METHOD_BUFFER
  445. PortContainerExternal *output;
  446. if (!Queue_Get_Cnt(com->sinkPort.inputQ)) {
  447. in->reuse = TRUE;
  448. return TRUE;
  449. }
  450. #endif
  451. in->reuse = TRUE;
  452. indexFrameDisplay = srcData->decInfo.indexFrameDisplay;
  453. if (indexFrameDisplay == DISPLAY_IDX_FLAG_SEQ_END) {
  454. #ifdef USE_FEEDING_METHOD_BUFFER
  455. while ((output = (PortContainerExternal*)ComponentPortGetData(&com->sinkPort)) != NULL)
  456. {
  457. output->nFlags = 0x1;
  458. output->nFilledLen = 0;
  459. ComponentNotifyListeners(com, COMPONENT_EVENT_DEC_FILL_BUFFER_DONE, (void *)output);
  460. }
  461. com->terminate = TRUE;
  462. ComponentNotifyListeners(com, COMPONENT_EVENT_DEC_DECODED_ALL, NULL);
  463. #else
  464. com->terminate = TRUE;
  465. #endif
  466. }
  467. else if (indexFrameDisplay >= 0) {
  468. #ifdef USE_FEEDING_METHOD_BUFFER
  469. VpuRect rcDisplay = {0,};
  470. TiledMapType mapType = srcData->decInfo.dispFrame.mapType;
  471. rcDisplay.right = srcData->decInfo.dispPicWidth;
  472. rcDisplay.bottom = srcData->decInfo.dispPicHeight;
  473. Uint32 width, height, bpp;
  474. size_t sizeYuv;
  475. Uint32 count = 0, total_count = 0;
  476. output = (PortContainerExternal*)ComponentPortGetData(&com->sinkPort);
  477. if (output == NULL) {
  478. in->reuse = TRUE;
  479. return TRUE;
  480. }
  481. if (ctx->MemoryOptimization){
  482. uint8_t *dmaBuffer = NULL;
  483. if (FALSE ==GetYUVFromFrameBuffer2(NULL, &dmaBuffer, output->nFilledLen, ctx->handle, &srcData->decInfo.dispFrame, rcDisplay, &width, &height, &bpp, &sizeYuv)) {
  484. VLOG(ERR, "GetYUVFromFrameBuffer2 FAIL!\n");
  485. }
  486. total_count = Queue_Get_Cnt(com->sinkPort.inputQ) + 1;
  487. VLOG(INFO, "pBuffer = %p, dmaBuffer = %p, index = %d/%d\n", output->pBuffer, dmaBuffer, count, total_count);
  488. while (output->pBuffer != dmaBuffer)
  489. {
  490. Queue_Enqueue(com->sinkPort.inputQ, (void *)output);
  491. count ++;
  492. if (count >= total_count)
  493. {
  494. VLOG(TRACE, "A wrong Frame Found\n");
  495. output = NULL;
  496. break;
  497. }
  498. output = (PortContainerExternal*)ComponentPortGetData(&com->sinkPort);
  499. VLOG(INFO, "pBuffer = %p, dmaBuffer = %p, index = %d/%d\n", output->pBuffer, dmaBuffer, count, total_count);
  500. }
  501. } else {
  502. if (FALSE ==GetYUVFromFrameBuffer2(output->pBuffer, NULL, output->nFilledLen, ctx->handle, &srcData->decInfo.dispFrame, rcDisplay, &width, &height, &bpp, &sizeYuv)) {
  503. VLOG(ERR, "GetYUVFromFrameBuffer2 FAIL!\n");
  504. }
  505. VPU_DecClrDispFlag(ctx->handle, srcData->decInfo.indexFrameDisplay);
  506. }
  507. if (output){
  508. output->nFilledLen = sizeYuv;
  509. output->index = srcData->decInfo.indexFrameDisplay;
  510. if(srcData->last)
  511. output->nFlags |= 0x1;
  512. ComponentNotifyListeners(com, COMPONENT_EVENT_DEC_FILL_BUFFER_DONE, (void *)output);
  513. }
  514. (void)DisplayFrame;
  515. (void)decConfig;
  516. (void)mapType;
  517. #else
  518. if (strlen((const char*)decConfig->outputPath) > 0) {
  519. VpuRect rcDisplay = {0,};
  520. TiledMapType mapType = srcData->decInfo.dispFrame.mapType;
  521. rcDisplay.right = srcData->decInfo.dispPicWidth;
  522. rcDisplay.bottom = srcData->decInfo.dispPicHeight;
  523. if (decConfig->scaleDownWidth > 0 || decConfig->scaleDownHeight > 0) {
  524. rcDisplay.right = VPU_CEIL(srcData->decInfo.dispPicWidth, 16);
  525. }
  526. if (ctx->fpOutput[0] == NULL) {
  527. if (OpenDisplayBufferFile(decConfig->bitFormat, decConfig->outputPath, rcDisplay, mapType, ctx->fpOutput) == FALSE) {
  528. return FALSE;
  529. }
  530. }
  531. SaveDisplayBufferToFile(ctx->handle, decConfig->bitFormat, srcData->decInfo.dispFrame, rcDisplay, ctx->fpOutput);
  532. }
  533. DisplayFrame(ctx, &srcData->decInfo);
  534. osal_msleep(ctx->displayPeriodTime);
  535. #endif
  536. }
  537. srcData->consumed = TRUE;
  538. srcData->reuse = FALSE;
  539. if (srcData->last == TRUE) {
  540. #ifdef USE_FEEDING_METHOD_BUFFER
  541. ComponentNotifyListeners(com, COMPONENT_EVENT_DEC_DECODED_ALL, NULL);
  542. while ((output = (PortContainerExternal*)ComponentPortGetData(&com->sinkPort)) != NULL)
  543. {
  544. output->nFlags = 0x1;
  545. output->nFilledLen = 0;
  546. VLOG(INFO, "Flush output port\r\n");
  547. ComponentNotifyListeners(com, COMPONENT_EVENT_DEC_FILL_BUFFER_DONE, (void *)output);
  548. }
  549. #endif
  550. com->terminate = TRUE;
  551. }
  552. return TRUE;
  553. }
  554. static BOOL PrepareRenderer(ComponentImpl* com, BOOL* done)
  555. {
  556. RendererContext* ctx = (RendererContext*)com->context;
  557. BOOL ret;
  558. if (ctx->handle == NULL) {
  559. CNMComponentParamRet paramRet;
  560. BOOL success;
  561. paramRet = ComponentGetParameter(com, com->srcPort.connectedComponent, GET_PARAM_DEC_HANDLE, &ctx->handle);
  562. if (ComponentParamReturnTest(paramRet, &success) == FALSE) {
  563. return success;
  564. }
  565. }
  566. if ((ret=AllocateFrameBuffer(com)) == FALSE || ctx->fbAllocated == FALSE) {
  567. return ret;
  568. }
  569. #ifdef USE_FEEDING_METHOD_BUFFER
  570. ComponentNotifyListeners(com, COMPONENT_EVENT_DEC_REGISTER_FB, &ctx->pFrame[0]);
  571. #endif
  572. // Nothing to do
  573. *done = TRUE;
  574. return TRUE;
  575. }
  576. static void ReleaseRenderer(ComponentImpl* com)
  577. {
  578. RendererContext* ctx = (RendererContext*)com->context;
  579. Uint32 coreIdx = ctx->testDecConfig.coreIdx;
  580. Uint32 i;
  581. #ifdef USE_FEEDING_METHOD_BUFFER
  582. if (!ctx->useBufferExternal)
  583. {
  584. #endif
  585. for (i=0; i<MAX_REG_FRAME; i++) {
  586. if (ctx->pFbMem[i].size) {
  587. if (i < ctx->fbCount.linearNum)
  588. vdi_free_dma_memory(coreIdx, &ctx->pFbMem[i], DEC_FBC, ctx->handle->instIndex);
  589. else
  590. vdi_free_dma_memory(coreIdx, &ctx->pFbMem[i], DEC_FB_LINEAR, ctx->handle->instIndex);
  591. }
  592. }
  593. #ifdef USE_FEEDING_METHOD_BUFFER
  594. }
  595. #endif
  596. for (i=0; i<MAX_REG_FRAME; i++) {
  597. if (ctx->pPPUFrame[i].size) vdi_free_dma_memory(coreIdx, &ctx->pPPUFbMem[i], DEC_ETC, ctx->handle->instIndex);
  598. }
  599. }
  600. static BOOL DestroyRenderer(ComponentImpl* com)
  601. {
  602. RendererContext* ctx = (RendererContext*)com->context;
  603. CloseDisplayBufferFile(ctx->fpOutput);
  604. Queue_Destroy(ctx->seqMemQ);
  605. Queue_Destroy(ctx->ppuQ);
  606. osal_mutex_destroy(ctx->lock);
  607. osal_free(ctx);
  608. return TRUE;
  609. }
  610. static Component CreateRenderer(ComponentImpl* com, CNMComponentConfig* componentParam)
  611. {
  612. RendererContext* ctx;
  613. com->context = (RendererContext*)osal_malloc(sizeof(RendererContext));
  614. osal_memset(com->context, 0, sizeof(RendererContext));
  615. ctx = com->context;
  616. #ifdef USE_FEEDING_METHOD_BUFFER
  617. osal_memset(ctx->pLinearFbMem, 0, sizeof(ctx->pLinearFbMem));
  618. osal_memset(ctx->pLinearFrame, 0, sizeof(ctx->pLinearFrame));
  619. #endif
  620. osal_memcpy((void*)&ctx->testDecConfig, (void*)&componentParam->testDecConfig, sizeof(TestDecConfig));
  621. if (componentParam->testDecConfig.fps > 0)
  622. ctx->displayPeriodTime = (1000 / componentParam->testDecConfig.fps);
  623. else
  624. ctx->displayPeriodTime = 1000/30;
  625. ctx->fbAllocated = FALSE;
  626. ctx->seqMemQ = Queue_Create(10, sizeof(SequenceMemInfo));
  627. ctx->lock = osal_mutex_create();
  628. ctx->ppuQ = Queue_Create(MAX_REG_FRAME, sizeof(FrameBuffer));
  629. #ifdef USE_FEEDING_METHOD_BUFFER
  630. if (componentParam->MemoryOptimization)
  631. {
  632. ctx->MemoryOptimization = TRUE;
  633. }
  634. else
  635. {
  636. ctx->MemoryOptimization = FALSE;
  637. }
  638. ctx->totalBufferNumber = 12;
  639. ctx->currentBufferNumber = 0;
  640. #endif
  641. return (Component)com;
  642. }
  643. ComponentImpl rendererComponentImpl = {
  644. "renderer",
  645. NULL,
  646. {0,},
  647. {0,},
  648. sizeof(PortContainer), /* No output */
  649. 5,
  650. CreateRenderer,
  651. GetParameterRenderer,
  652. SetParameterRenderer,
  653. PrepareRenderer,
  654. ExecuteRenderer,
  655. ReleaseRenderer,
  656. DestroyRenderer
  657. };