component_dec_renderer.c 26 KB

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