component_coda9_dec_decoder.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162
  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. #include "cnm_app.h"
  28. #include "misc/debug.h"
  29. #include "misc/bw_monitor.h"
  30. #include "sys/time.h"
  31. #define EXTRA_FRAME_BUFFER_NUM 1
  32. #define INDEX_FRAME_DIS_QUEUE_SIZE 255
  33. #define MAX_SIZE_HEADER_DATA 255
  34. typedef enum {
  35. DEC_INT_STATUS_NONE, // Interrupt not asserted yet
  36. DEC_INT_STATUS_EMPTY, // Need more es
  37. DEC_INT_STATUS_DONE, // Interrupt asserted
  38. DEC_INT_STATUS_TIMEOUT, // Interrupt not asserted during given time.
  39. DEC_INT_STATUS_FIELD, // One filed picture(top or bottom) was decoded(need a next field picture)
  40. } DEC_INT_STATUS;
  41. typedef enum {
  42. DEC_STATE_NONE,
  43. DEC_STATE_OPEN_DECODER,
  44. DEC_STATE_INIT_SEQ,
  45. DEC_STATE_REGISTER_FB,
  46. DEC_STATE_DECODING,
  47. DEC_STATE_CLOSE,
  48. } DecoderState;
  49. typedef struct {
  50. BOOL isHdrData;
  51. Int32 hdrDataSize;
  52. Uint8 hdrData[MAX_SIZE_HEADER_DATA];
  53. } HeaderData;
  54. typedef struct {
  55. TestDecConfig testDecConfig;
  56. DecOpenParam decOpenParam;
  57. DecParam decParam;
  58. FrameBufferFormat wtlFormat;
  59. DecHandle handle;
  60. Uint64 startTimeout;
  61. vpu_buffer_t vbUserData;
  62. BOOL doFlush;
  63. BOOL stateDoing;
  64. DecoderState state;
  65. DecInitialInfo initialInfo;
  66. Uint32 numDecoded; /*!<< The number of decoded frames */
  67. Uint32 numOutput;
  68. PhysicalAddress decodedAddr;
  69. Uint32 frameNumToStop;
  70. BOOL doReset;
  71. Uint32 cyclePerTick;
  72. ParamDecPPUFrameBuffer ppuFbParam;
  73. Queue* idxFrameDisQ;
  74. BOOL needMoreStream;
  75. Int32 linearFbCnt;
  76. BOOL seqChangeReqest;
  77. Int32 seqChangedStreamEndFlag;
  78. Int32 seqChangedRdPtr;
  79. Int32 seqChangedWrPtr;
  80. BOOL prevScaleX;
  81. BOOL prevScaleY;
  82. BOOL isTheoraHeader;
  83. BOOL oneFiledDecoded;
  84. BOOL chunkReuseRequired;
  85. BOOL isDecodedFlag; // only used in pic_enc_mode
  86. HeaderData hdrInfo;
  87. ParamDecBitstreamBufPos bsPos;
  88. } DecoderContext;
  89. void AppendPicHeaderData(DecoderContext* ctx, PortContainerES* streamData) {
  90. PhysicalAddress rdPtr, wrPtr;
  91. Uint32 room;
  92. Uint8* pStreamBuffer = NULL;
  93. Int32 totalBufSize = 0;
  94. Int32 coreIdx = ctx->testDecConfig.coreIdx;
  95. //Append PPS data to bistreambuffer which will be decoded.
  96. VPU_DecGetBitstreamBuffer(ctx->handle ,&rdPtr, &wrPtr, &room);
  97. if (ctx->hdrInfo.hdrDataSize < room) {
  98. totalBufSize = (wrPtr - rdPtr) + ctx->hdrInfo.hdrDataSize;
  99. pStreamBuffer = (Uint8*)osal_malloc(totalBufSize+1);
  100. if (NULL != pStreamBuffer) {
  101. osal_memset(pStreamBuffer, 0x00, totalBufSize+1);
  102. osal_memcpy(pStreamBuffer, ctx->hdrInfo.hdrData, ctx->hdrInfo.hdrDataSize);
  103. vdi_read_memory(coreIdx, rdPtr, pStreamBuffer+ctx->hdrInfo.hdrDataSize, wrPtr - rdPtr,ctx->testDecConfig.streamEndian);
  104. vdi_write_memory(coreIdx, rdPtr, pStreamBuffer, totalBufSize, ctx->testDecConfig.streamEndian);
  105. VPU_DecUpdateBitstreamBuffer(ctx->handle, ctx->hdrInfo.hdrDataSize);
  106. }
  107. }
  108. if (NULL != pStreamBuffer) {
  109. osal_free(pStreamBuffer);
  110. }
  111. }
  112. BOOL CheckConsumedHeaderStream(DecoderContext* ctx)
  113. {
  114. PhysicalAddress rdPtr =0, wrPtr =0;
  115. Uint32 room =0;
  116. if (NULL == ctx->handle) {
  117. return FALSE;
  118. }
  119. VPU_DecGetBitstreamBuffer(ctx->handle, &rdPtr, &wrPtr, &room);
  120. if (wrPtr != rdPtr) {
  121. return FALSE;
  122. }
  123. return TRUE;
  124. }
  125. void AlignPictureSize(DecoderContext* ctx, CNMComListenerDecDone* lsnpPicDone)
  126. {
  127. TestDecConfig* decConfig = NULL;
  128. if (NULL == ctx || NULL == lsnpPicDone) {
  129. VLOG(INFO, "%s:%d NULL pointer exception\n", __FUNCTION__, __LINE__);
  130. return;
  131. }
  132. decConfig = &(ctx->testDecConfig);
  133. if (0 > lsnpPicDone->output->indexFrameDisplay) {
  134. return;
  135. }
  136. switch (decConfig->compareType) {
  137. default :
  138. break;
  139. }
  140. }
  141. static BOOL RegisterFrameBuffers(ComponentImpl* com)
  142. {
  143. DecoderContext* ctx = (DecoderContext*)com->context;
  144. FrameBuffer* pFrame = NULL;
  145. Uint32 framebufStride = 0;
  146. ParamDecFrameBuffer paramFb;
  147. RetCode result;
  148. DecInitialInfo* codecInfo = &ctx->initialInfo;
  149. BOOL success;
  150. CNMComponentParamRet ret;
  151. CNMComListenerDecRegisterFb lsnpRegisterFb;
  152. MaverickCacheConfig cacheCfg;
  153. ctx->stateDoing = TRUE;
  154. ret = ComponentGetParameter(com, com->sinkPort.connectedComponent, GET_PARAM_RENDERER_FRAME_BUF, (void*)&paramFb);
  155. if (ComponentParamReturnTest(ret, &success) == FALSE) {
  156. return success;
  157. }
  158. MaverickCache2Config(&cacheCfg, TRUE /*Decoder*/, ctx->testDecConfig.cbcrInterleave,
  159. ctx->testDecConfig.coda9.frameCacheBypass,
  160. ctx->testDecConfig.coda9.frameCacheBurst,
  161. ctx->testDecConfig.coda9.frameCacheMerge,
  162. ctx->testDecConfig.mapType,
  163. ctx->testDecConfig.coda9.frameCacheWayShape);
  164. VPU_DecGiveCommand(ctx->handle, SET_CACHE_CONFIG, &cacheCfg);
  165. pFrame = paramFb.fb;
  166. framebufStride = paramFb.stride;
  167. VLOG(INFO, "<%s> LINEAR: %d, Tiled: %d\n", __FUNCTION__, paramFb.linearNum, paramFb.nonLinearNum);
  168. ctx->linearFbCnt = paramFb.linearNum;
  169. result = VPU_DecRegisterFrameBuffer(ctx->handle, pFrame, paramFb.linearNum, framebufStride, codecInfo->picHeight, ctx->testDecConfig.mapType);
  170. lsnpRegisterFb.handle = ctx->handle;
  171. lsnpRegisterFb.numLinearFb = paramFb.linearNum;
  172. lsnpRegisterFb.numNonLinearFb = paramFb.nonLinearNum;
  173. ComponentNotifyListeners(com, COMPONENT_EVENT_DEC_REGISTER_FB, (void*)&lsnpRegisterFb);
  174. if (result != RETCODE_SUCCESS) {
  175. VLOG(ERR, "%s:%d Failed to VPU_DecRegisterFrameBufferEx(%d)\n", __FUNCTION__, __LINE__, result);
  176. return FALSE;
  177. }
  178. ctx->stateDoing = FALSE;
  179. ret = ComponentGetParameter(com, com->sinkPort.connectedComponent, GET_PARAM_RENDERER_PPU_FRAME_BUF, &(ctx->ppuFbParam));
  180. if (ComponentParamReturnTest(ret, &success) == FALSE) {
  181. return FALSE;
  182. }
  183. if(TRUE == ctx->ppuFbParam.enablePPU) {
  184. if(NULL != ctx->ppuFbParam.fb) {
  185. VPU_DecGiveCommand(ctx->handle, SET_ROTATOR_OUTPUT, ctx->ppuFbParam.fb);
  186. if (ctx->testDecConfig.coda9.rotate > 0) {
  187. VPU_DecGiveCommand(ctx->handle, ENABLE_ROTATION, NULL);
  188. }
  189. if (ctx->testDecConfig.coda9.mirror > 0) {
  190. VPU_DecGiveCommand(ctx->handle, ENABLE_MIRRORING, NULL);
  191. }
  192. if (ctx->testDecConfig.coda9.enableDering == TRUE) {
  193. VPU_DecGiveCommand(ctx->handle, ENABLE_DERING, NULL);
  194. }
  195. } else {
  196. VLOG(ERR, "%s:%d NULL pointer Exception - ppuFb\n", __FUNCTION__, __LINE__);
  197. return FALSE;
  198. }
  199. }
  200. PrepareDecoderTest(ctx->handle);
  201. return TRUE;
  202. }
  203. static BOOL PreSequenceChange(ComponentImpl* com, DecOutputInfo* outputInfo)
  204. {
  205. DecoderContext* ctx = (DecoderContext*)com->context;
  206. RetCode ret;
  207. VLOG(INFO, "-----PRE SEQUENCE CHANGED -----\n");
  208. ctx->seqChangedRdPtr = outputInfo->rdPtr;
  209. ctx->seqChangedWrPtr = outputInfo->wrPtr;
  210. VLOG(INFO, "seqChangeRdPtr: 0x%08x, WrPtr: 0x%08x\n", ctx->seqChangedRdPtr, ctx->seqChangedWrPtr);
  211. if (RETCODE_SUCCESS != (ret=VPU_DecSetRdPtr(ctx->handle, ctx->seqChangedRdPtr, TRUE))) {
  212. VLOG(ERR, "%s:%d Failed to VPU_DecSetRdPtr(%d), ret(%d)\n", __FUNCTION__, __LINE__, ctx->seqChangedRdPtr, ret);
  213. return FALSE;
  214. }
  215. ctx->seqChangedStreamEndFlag = outputInfo->streamEndFlag; //BIT_BIT_STREAM_PARAM(0x114)
  216. VPU_DecUpdateBitstreamBuffer(ctx->handle, 1); // let f/w to know stream end condition in bitstream buffer. force to know that bitstream buffer will be empty.
  217. VPU_DecUpdateBitstreamBuffer(ctx->handle, STREAM_END_SET_FLAG); // set to stream end condition to pump out a delayed framebuffer.
  218. return TRUE;
  219. }
  220. static BOOL SequenceChange(ComponentImpl* com, DecOutputInfo* outputInfo)
  221. {
  222. DecoderContext* ctx = (DecoderContext*)com->context;
  223. DecInitialInfo initialInfo;
  224. ParamDecBitstreamBuffer bsBuf;
  225. BOOL resOf = TRUE;
  226. Uint32 comState = COMPONENT_STATE_NONE;
  227. VLOG(INFO, "----- SEQUENCE CHANGED -----\n");
  228. // Get current(changed) sequence information.
  229. VPU_DecGiveCommand(ctx->handle, DEC_GET_SEQ_INFO, &initialInfo);
  230. // Flush all remaining framebuffers of previous sequence.
  231. VLOG(INFO, "sequenceChanged : %x\n", outputInfo->sequenceChanged);
  232. VLOG(INFO, "SEQUENCE NO : %d\n", initialInfo.sequenceNo);
  233. VLOG(INFO, "DPB COUNT : %d\n", initialInfo.minFrameBufferCount);
  234. VLOG(INFO, "BITDEPTH : LUMA(%d), CHROMA(%d)\n", initialInfo.lumaBitdepth, initialInfo.chromaBitdepth);
  235. VLOG(INFO, "SIZE : WIDTH(%d), HEIGHT(%d)\n", initialInfo.picWidth, initialInfo.picHeight);
  236. ComponentSetParameter(com, com->sinkPort.connectedComponent, SET_PARAM_RENDERER_FREE_FRAMEBUFFERS, (void*)&outputInfo->frameDisplayFlag);
  237. VPU_DecGiveCommand(ctx->handle, DEC_RESET_FRAMEBUF_INFO, NULL);
  238. VPU_DecSetRdPtr(ctx->handle, ctx->seqChangedRdPtr, TRUE);
  239. if (ctx->seqChangedStreamEndFlag == 1) { // No more bitstream in the buffer.
  240. VPU_DecUpdateBitstreamBuffer(ctx->handle, STREAM_END_SET_FLAG);
  241. }
  242. else {
  243. VPU_DecUpdateBitstreamBuffer(ctx->handle, STREAM_END_CLEAR_FLAG);
  244. }
  245. if (ctx->seqChangedWrPtr >= ctx->seqChangedRdPtr) {
  246. resOf = (RETCODE_SUCCESS == VPU_DecUpdateBitstreamBuffer(ctx->handle, ctx->seqChangedWrPtr - ctx->seqChangedRdPtr));
  247. }
  248. else {
  249. ComponentGetParameter(com, com->srcPort.connectedComponent, GET_PARAM_FEEDER_BITSTREAM_BUF, &bsBuf);
  250. resOf = (RETCODE_SUCCESS == VPU_DecUpdateBitstreamBuffer(ctx->handle, (bsBuf.bs->phys_addr + bsBuf.bs->size) - ctx->seqChangedRdPtr +
  251. (ctx->seqChangedWrPtr - bsBuf.bs->phys_addr)));
  252. }
  253. //ctx->state = DEC_STATE_REGISTER_FB;
  254. ctx->state = DEC_STATE_INIT_SEQ;
  255. osal_memcpy((void*)&ctx->initialInfo, (void*)&initialInfo, sizeof(DecInitialInfo));
  256. comState = COMPONENT_STATE_CREATED;
  257. ComponentSetParameter(com, com->sinkPort.connectedComponent, SET_PARAM_RENDERER_CHANGE_COM_STATE, (void*)&comState);
  258. VLOG(INFO, "----------------------------\n");
  259. return resOf;
  260. }
  261. static BOOL CheckAndDoSequenceChange(ComponentImpl* com, DecOutputInfo* outputInfo)
  262. {
  263. DecoderContext* ctx = (DecoderContext*)com->context;
  264. BOOL resOf = TRUE;
  265. if (0 != outputInfo->sequenceChanged) {
  266. ctx->seqChangeReqest = TRUE;
  267. resOf = PreSequenceChange(com, outputInfo);
  268. }
  269. if (TRUE == ctx->seqChangeReqest && DISPLAY_IDX_FLAG_SEQ_END == outputInfo->indexFrameDisplay) {
  270. resOf = SequenceChange(com, outputInfo);
  271. }
  272. return resOf;
  273. }
  274. static DEC_INT_STATUS HandlingInterruptFlagNoCQ(ComponentImpl* com, InterruptBit waitInterrptFlag)
  275. {
  276. DecoderContext* ctx = (DecoderContext*)com->context;
  277. DecHandle handle = ctx->handle;
  278. Int32 interruptFlag = 0;
  279. Uint32 interruptWaitTime = VPU_WAIT_TIME_OUT;
  280. Uint32 interruptTimeout = VPU_DEC_TIMEOUT;
  281. DEC_INT_STATUS status = DEC_INT_STATUS_NONE;
  282. CNMComListenerDecInt lsn;
  283. BOOL repeat = TRUE;
  284. ctx->startTimeout = osal_gettime();
  285. do {
  286. interruptFlag = VPU_WaitInterruptEx(handle, interruptWaitTime);
  287. if (INTERRUPT_TIMEOUT_VALUE == interruptFlag) {
  288. Uint64 currentTimeout = osal_gettime();
  289. if ((currentTimeout - ctx->startTimeout) > interruptTimeout) {
  290. VLOG(ERR, "\n INSNTANCE #%d INTERRUPT TIMEOUT.\n", handle->instIndex);
  291. status = DEC_INT_STATUS_TIMEOUT;
  292. break;
  293. }
  294. interruptFlag = 0;
  295. }
  296. if (interruptFlag < 0) {
  297. VLOG(ERR, "<%s:%d> interruptFlag is negative value! %08x\n", __FUNCTION__, __LINE__, interruptFlag);
  298. status = DEC_INT_STATUS_NONE;
  299. }
  300. if (interruptFlag & (1 << INT_BIT_DEC_FIELD)) {
  301. if (BS_MODE_PIC_END == ctx->testDecConfig.bitstreamMode) {
  302. status = DEC_INT_STATUS_FIELD;
  303. break;
  304. }
  305. }
  306. if (interruptFlag) {
  307. VPU_ClearInterruptEx(handle, interruptFlag);
  308. }
  309. if (interruptFlag & (1 << waitInterrptFlag)) {
  310. status = DEC_INT_STATUS_DONE;
  311. repeat = FALSE;
  312. }
  313. if (interruptFlag & (1 << INT_BIT_BIT_BUF_EMPTY)) {
  314. status = DEC_INT_STATUS_EMPTY;
  315. break;
  316. }
  317. } while (repeat);
  318. lsn.handle = handle;
  319. lsn.flag = interruptFlag;
  320. ComponentNotifyListeners(com, COMPONENT_EVENT_DEC_INTERRUPT, (void*)&lsn);
  321. return status;
  322. }
  323. static BOOL DoReset(ComponentImpl* com)
  324. {
  325. DecoderContext* ctx = (DecoderContext*)com->context;
  326. BitStreamMode bsMode = ctx->decOpenParam.bitstreamMode;
  327. DecHandle handle = ctx->handle;
  328. Uint32 timeoutCount;
  329. VLOG(INFO, "========== %s ==========\n", __FUNCTION__);
  330. VLOG(INFO, "> FLUSH RENDERER\n");
  331. /* Send the renderer the signal to drop all frames. */
  332. ComponentSetParameter(com, com->sinkPort.connectedComponent, SET_PARAM_RENDERER_FLUSH, NULL);
  333. VLOG(INFO, "> EXPLICIT_END_SET_FLAG\n");
  334. // In order to stop processing bitstream.
  335. VPU_DecUpdateBitstreamBuffer(handle, EXPLICIT_END_SET_FLAG);
  336. // Clear DPB
  337. timeoutCount = 0;
  338. VLOG(INFO, "> Flush VPU internal buffer\n");
  339. while (VPU_DecFrameBufferFlush(handle, NULL, NULL) == RETCODE_VPU_STILL_RUNNING) {
  340. if (timeoutCount >= VPU_DEC_TIMEOUT) {
  341. VLOG(ERR, "NO RESPONSE FROM VPU_DecFrameBufferFlush()\n");
  342. return FALSE;
  343. }
  344. timeoutCount++;
  345. }
  346. VLOG(INFO, "> Reset VPU\n");
  347. if (VPU_SWReset(handle->coreIdx, SW_RESET_SAFETY, handle) != RETCODE_SUCCESS) {
  348. VLOG(ERR, "<%s:%d> Failed to VPU_SWReset()\n", __FUNCTION__, __LINE__);
  349. return FALSE;
  350. }
  351. // Clear CPB
  352. if (bsMode == BS_MODE_INTERRUPT) {
  353. PhysicalAddress curWrPtr;
  354. VLOG(INFO, "> Clear CPB\n");
  355. VPU_DecGetBitstreamBuffer(handle, NULL, &curWrPtr, NULL);
  356. VPU_DecSetRdPtr(handle, curWrPtr, TRUE);
  357. }
  358. VLOG(INFO, "> STREAM_END_CLEAR_FLAG\n");
  359. // Clear stream-end flag
  360. VPU_DecUpdateBitstreamBuffer(handle, STREAM_END_CLEAR_FLAG);
  361. VLOG(INFO, "========== %s ==========\n", __FUNCTION__);
  362. ComponentNotifyListeners(com, COMPONENT_EVENT_DEC_RESET_DONE, NULL);
  363. ctx->doReset = FALSE;
  364. ctx->startTimeout = 0ULL;
  365. return TRUE;
  366. }
  367. static BOOL Decode(ComponentImpl* com, PortContainerES* in, PortContainerDisplay* out)
  368. {
  369. DecoderContext* ctx = (DecoderContext*)com->context;
  370. DecOutputInfo decOutputInfo;
  371. DEC_INT_STATUS intStatus = DEC_INT_STATUS_NONE;
  372. CNMComListenerStartDecOneFrame lsnpPicStart;
  373. BitStreamMode bsMode = ctx->decOpenParam.bitstreamMode;
  374. RetCode result;
  375. CNMComListenerDecDone lsnpPicDone;
  376. CNMComListenerDecReadyOneFrame lsnpReadyOneFrame = {0,};
  377. BOOL doDecode;
  378. lsnpReadyOneFrame.handle = ctx->handle;
  379. ComponentNotifyListeners(com, COMPONENT_EVENT_DEC_READY_ONE_FRAME, (void*)&lsnpReadyOneFrame);
  380. ctx->stateDoing = TRUE;
  381. /* decode a frame except when the bitstream mode is PIC_END and no data */
  382. doDecode = !(bsMode == BS_MODE_PIC_END && in == NULL);
  383. if (FALSE == doDecode) {
  384. return TRUE;
  385. }
  386. if (TRUE == com->pause) {
  387. return TRUE;
  388. }
  389. if (BS_MODE_PIC_END == bsMode) {
  390. if (TRUE == ctx->hdrInfo.isHdrData) {
  391. ctx->hdrInfo.isHdrData = FALSE;
  392. AppendPicHeaderData(ctx, in);
  393. }
  394. }
  395. if (ctx->needMoreStream == FALSE) {
  396. /* Empty interrupt wasn't asserted */
  397. result = VPU_DecStartOneFrame(ctx->handle, &ctx->decParam);
  398. lsnpPicStart.handle = ctx->handle;
  399. lsnpPicStart.result = result;
  400. lsnpPicStart.decParam = ctx->decParam;
  401. ComponentNotifyListeners(com, COMPONENT_EVENT_DEC_START_ONE_FRAME, (void*)&lsnpPicStart);
  402. if (result != RETCODE_SUCCESS) {
  403. return FALSE;
  404. }
  405. }
  406. if (TRUE == ctx->oneFiledDecoded && BS_MODE_PIC_END == ctx->testDecConfig.bitstreamMode) {
  407. VPU_ClearInterrupt(ctx->testDecConfig.coreIdx);
  408. ctx->oneFiledDecoded = FALSE;
  409. }
  410. ctx->needMoreStream = FALSE;
  411. intStatus = HandlingInterruptFlagNoCQ(com, INT_BIT_PIC_RUN);
  412. if ((DEC_INT_STATUS_TIMEOUT == intStatus)) {
  413. HandleDecoderError(ctx->handle, ctx->numDecoded, NULL);
  414. PrintDecVpuStatus(ctx->handle);
  415. DoReset(com);
  416. return FALSE;
  417. }
  418. else if (DEC_INT_STATUS_EMPTY == intStatus) {
  419. ctx->needMoreStream = TRUE;
  420. return TRUE;
  421. }
  422. else if (DEC_INT_STATUS_FIELD == intStatus) {
  423. ctx->needMoreStream = TRUE;
  424. ctx->oneFiledDecoded = TRUE;
  425. if(out) out->reuse = TRUE;
  426. return TRUE;
  427. }
  428. //default value - chunkReuseRequired
  429. ctx->chunkReuseRequired = FALSE;
  430. ctx->isDecodedFlag = FALSE;
  431. // Get data from the sink component.
  432. if ((result=VPU_DecGetOutputInfo(ctx->handle, &decOutputInfo)) == RETCODE_SUCCESS) {
  433. DisplayDecodedInformation(ctx->handle, ctx->decOpenParam.bitstreamFormat, ctx->numDecoded, &decOutputInfo, ctx->testDecConfig.performance, ctx->cyclePerTick);
  434. }
  435. if (TRUE == ctx->ppuFbParam.enablePPU) {
  436. if (decOutputInfo.indexFrameDisplay >= 0) {
  437. Queue_Enqueue(ctx->idxFrameDisQ, &(decOutputInfo.indexFrameDisplay));
  438. if(1 == ctx->idxFrameDisQ->count) {
  439. if (TRUE == decOutputInfo.chunkReuseRequired) {
  440. ctx->chunkReuseRequired = TRUE;
  441. if (0 <= decOutputInfo.indexFrameDecoded) ctx->isDecodedFlag = TRUE;
  442. if (in) in->reuse = TRUE;
  443. }
  444. return TRUE;
  445. }
  446. }
  447. }
  448. lsnpPicDone.handle = ctx->handle;
  449. lsnpPicDone.ret = result;
  450. lsnpPicDone.decParam = &ctx->decParam;
  451. lsnpPicDone.output = &decOutputInfo;
  452. lsnpPicDone.numDecoded = ctx->numDecoded;
  453. lsnpPicDone.vbUser = ctx->vbUserData;
  454. AlignPictureSize(ctx, &lsnpPicDone);
  455. ComponentNotifyListeners(com, COMPONENT_EVENT_DEC_GET_OUTPUT_INFO, (void*)&lsnpPicDone);
  456. if (result == RETCODE_REPORT_NOT_READY) {
  457. return TRUE; // Not decoded yet. Try again
  458. }
  459. else if (result != RETCODE_SUCCESS) {
  460. /* ERROR */
  461. VLOG(ERR, "Failed to decode error : %d \n", result);
  462. return FALSE;
  463. }
  464. if ((decOutputInfo.decodingSuccess & 0x01) == 0) {
  465. VLOG(ERR, "VPU_DecGetOutputInfo decode fail framdIdx %d error(0x%08x) reason(0x%08x), reasonExt(0x%08x)\n",
  466. ctx->numDecoded, decOutputInfo.decodingSuccess, decOutputInfo.errorReason, decOutputInfo.errorReasonExt);
  467. return FALSE;
  468. }
  469. if (FALSE == CheckAndDoSequenceChange(com, &decOutputInfo)) {
  470. return FALSE;
  471. }
  472. else {
  473. if (TRUE == ctx->seqChangeReqest && DISPLAY_IDX_FLAG_SEQ_END == decOutputInfo.indexFrameDisplay) {
  474. ctx->seqChangeReqest = FALSE;
  475. ctx->stateDoing = FALSE; //Go back to DecodeHeader()
  476. return TRUE;
  477. }
  478. }
  479. if (decOutputInfo.indexFrameDecoded >= 0) {
  480. // Return a used data to a source port.
  481. ctx->numDecoded++;
  482. out->reuse = FALSE;
  483. }
  484. if ((decOutputInfo.indexFrameDisplay >= 0) || (decOutputInfo.indexFrameDisplay == DISPLAY_IDX_FLAG_SEQ_END)) {
  485. ctx->numOutput++;
  486. out->last = (BOOL)(decOutputInfo.indexFrameDisplay == DISPLAY_IDX_FLAG_SEQ_END);
  487. out->reuse = FALSE;
  488. }
  489. if (decOutputInfo.indexFrameDisplay == DISPLAY_IDX_FLAG_SEQ_END) {
  490. ctx->stateDoing = FALSE;
  491. com->terminate = TRUE;
  492. }
  493. if (BS_MODE_PIC_END == ctx->testDecConfig.bitstreamMode) {
  494. if (DECODED_IDX_FLAG_NO_FB == decOutputInfo.indexFrameDecoded) {
  495. //reuse a chunk of bitstream
  496. if (in) in->reuse = TRUE;
  497. }
  498. }
  499. if (TRUE == decOutputInfo.chunkReuseRequired) {
  500. //reuse a chunk of bitstream
  501. if (in) in->reuse = TRUE;
  502. ctx->chunkReuseRequired = TRUE;
  503. if (0 <= decOutputInfo.indexFrameDecoded) {
  504. ctx->isDecodedFlag = TRUE;
  505. }
  506. }
  507. if (out->reuse == FALSE) {
  508. osal_memcpy((void*)&out->decInfo, (void*)&decOutputInfo, sizeof(DecOutputInfo));
  509. if (ctx->ppuFbParam.enablePPU && 1 < ctx->idxFrameDisQ->count) {
  510. if (NULL != Queue_Peek(ctx->idxFrameDisQ)) {
  511. out->decInfo.indexFrameDisplay = *((Int32*)Queue_Dequeue(ctx->idxFrameDisQ));
  512. }
  513. }
  514. }
  515. else { // TRUE == out->reuse
  516. if (ctx->ppuFbParam.enablePPU && 1 < ctx->idxFrameDisQ->count) {
  517. if (NULL != Queue_Peek(ctx->idxFrameDisQ) && DECODED_IDX_FLAG_NO_FB == decOutputInfo.indexFrameDecoded) {
  518. out->decInfo.indexFrameDisplay = *((Int32*)Queue_Dequeue(ctx->idxFrameDisQ));
  519. VPU_DecClrDispFlag(ctx->handle, out->decInfo.indexFrameDisplay);
  520. }
  521. }
  522. }
  523. if (ctx->frameNumToStop > 0) {
  524. if (ctx->frameNumToStop == ctx->numOutput) {
  525. com->terminate = TRUE;
  526. }
  527. }
  528. return TRUE;
  529. }
  530. static CNMComponentParamRet GetParameterDecoder(ComponentImpl* from, ComponentImpl* com, GetParameterCMD commandType, void* data)
  531. {
  532. DecoderContext* ctx = (DecoderContext*)com->context;
  533. BOOL result = TRUE;
  534. ParamDecNeedFrameBufferNum* fbNum;
  535. ParamVpuStatus* status;
  536. QueueStatusInfo cqInfo;
  537. PortContainerES* container;
  538. vpu_buffer_t vb;
  539. Int32 productID = -1;
  540. if (NULL == ctx) {
  541. VLOG(ERR, "%s:%d NULL Pointer Exception\n", __FUNCTION__, __LINE__);
  542. return CNM_COMPONENT_PARAM_FAILURE;
  543. }
  544. productID = ctx->testDecConfig.productId;
  545. if (ctx->handle == NULL) return CNM_COMPONENT_PARAM_NOT_READY;
  546. if (ctx->doReset == TRUE) return CNM_COMPONENT_PARAM_NOT_READY;
  547. switch(commandType) {
  548. case GET_PARAM_COM_IS_CONTAINER_CONUSUMED:
  549. // This query command is sent from the comonponent core.
  550. // If input data are consumed in sequence, it should return TRUE through PortContainer::consumed.
  551. container = (PortContainerES*)data;
  552. vb = container->buf;
  553. if (vb.phys_addr <= ctx->decodedAddr && ctx->decodedAddr < (vb.phys_addr+vb.size)) {
  554. container->consumed = TRUE;
  555. ctx->decodedAddr = 0;
  556. }
  557. break;
  558. case GET_PARAM_DEC_HANDLE:
  559. *(DecHandle*)data = ctx->handle;
  560. break;
  561. case GET_PARAM_DEC_FRAME_BUF_NUM:
  562. if (ctx->state <= DEC_STATE_INIT_SEQ) return CNM_COMPONENT_PARAM_NOT_READY;
  563. fbNum = (ParamDecNeedFrameBufferNum*)data;
  564. if (PRODUCT_ID_960 == productID || PRODUCT_ID_980 == productID) {
  565. //linear Buffer
  566. fbNum->linearNum = ctx->initialInfo.minFrameBufferCount + EXTRA_FRAME_BUFFER_NUM; // max_dec_pic_buffering
  567. if(ctx->testDecConfig.enableWTL) {
  568. //num of tiled buffers are same with num of linear buffers
  569. //CODA series do not use compressed buffers,
  570. //In this case, compressNum means number of tiled buffers
  571. fbNum->nonLinearNum = fbNum->linearNum;
  572. }
  573. else {
  574. fbNum->nonLinearNum =0;
  575. }
  576. }
  577. break;
  578. case GET_PARAM_DEC_BITSTREAM_BUF_POS:
  579. if (0 == ctx->bsPos.rdPtr && 0 == ctx->bsPos.wrPtr) return CNM_COMPONENT_PARAM_NOT_READY;
  580. *(ParamDecBitstreamBufPos*)data = ctx->bsPos;
  581. break;
  582. case GET_PARAM_DEC_CODEC_INFO:
  583. if (ctx->state <= DEC_STATE_INIT_SEQ) return CNM_COMPONENT_PARAM_NOT_READY;
  584. VPU_DecGiveCommand(ctx->handle, DEC_GET_SEQ_INFO, data);
  585. break;
  586. case GET_PARAM_VPU_STATUS:
  587. if (ctx->state != DEC_STATE_DECODING) return CNM_COMPONENT_PARAM_NOT_READY;
  588. VPU_DecGiveCommand(ctx->handle, DEC_GET_QUEUE_STATUS, &cqInfo);
  589. status = (ParamVpuStatus*)data;
  590. status->cq = cqInfo;
  591. break;
  592. default:
  593. result = FALSE;
  594. break;
  595. }
  596. return (result == TRUE) ? CNM_COMPONENT_PARAM_SUCCESS : CNM_COMPONENT_PARAM_FAILURE;
  597. }
  598. static CNMComponentParamRet SetParameterDecoder(ComponentImpl* from, ComponentImpl* com, SetParameterCMD commandType, void* data)
  599. {
  600. BOOL result = TRUE;
  601. DecoderContext* ctx = (DecoderContext*)com->context;
  602. Int32 skipCmd;
  603. switch(commandType) {
  604. case SET_PARAM_COM_PAUSE:
  605. com->pause = *(BOOL*)data;
  606. break;
  607. case SET_PARAM_DEC_SKIP_COMMAND:
  608. skipCmd = *(Int32*)data;
  609. ctx->decParam.skipframeMode = skipCmd;
  610. if (skipCmd == WAVE_SKIPMODE_NON_IRAP) {
  611. Uint32 userDataMask = (1<<H265_USERDATA_FLAG_RECOVERY_POINT);
  612. ctx->decParam.craAsBlaFlag = TRUE;
  613. /* For finding recovery point */
  614. VPU_DecGiveCommand(ctx->handle, ENABLE_REP_USERDATA, &userDataMask);
  615. }
  616. else {
  617. ctx->decParam.craAsBlaFlag = FALSE;
  618. }
  619. if (ctx->numDecoded > 0) {
  620. ctx->doFlush = (BOOL)(ctx->decParam.skipframeMode == WAVE_SKIPMODE_NON_IRAP);
  621. }
  622. break;
  623. case SET_PARAM_DEC_RESET:
  624. ctx->doReset = TRUE;
  625. break;
  626. default:
  627. result = FALSE;
  628. break;
  629. }
  630. return (result == TRUE) ? CNM_COMPONENT_PARAM_SUCCESS : CNM_COMPONENT_PARAM_FAILURE;
  631. }
  632. static BOOL UpdateBitstream(DecoderContext* ctx, PortContainerES* in)
  633. {
  634. RetCode ret = RETCODE_SUCCESS;
  635. BitStreamMode bsMode = ctx->decOpenParam.bitstreamMode;
  636. BOOL update = TRUE;
  637. Uint32 updateSize;
  638. PhysicalAddress rdPtr, wrPtr;
  639. Uint32 room;
  640. if (NULL == in) return TRUE;
  641. if (BS_MODE_PIC_END == bsMode) {
  642. if (TRUE == ctx->isTheoraHeader) {
  643. //Theora decoder need to parse bitstream before decoding
  644. ctx->isTheoraHeader = FALSE;
  645. in->reuse = FALSE;
  646. return TRUE;
  647. }
  648. if (FALSE == ctx->chunkReuseRequired) {
  649. VPU_DecSetRdPtr(ctx->handle, in->buf.phys_addr, TRUE);
  650. }
  651. else if (TRUE == ctx->chunkReuseRequired && FALSE == ctx->isDecodedFlag) {
  652. VPU_DecSetRdPtr(ctx->handle, in->buf.phys_addr, TRUE);
  653. }
  654. }
  655. else {
  656. if (in->size > 0) {
  657. VPU_DecGetBitstreamBuffer(ctx->handle, &rdPtr, &wrPtr, &room);
  658. if (room < in->size) {
  659. in->reuse = TRUE;
  660. return TRUE;
  661. }
  662. }
  663. }
  664. if (TRUE == in->last) {
  665. updateSize = (in->size == 0) ? STREAM_END_SET_FLAG : in->size;
  666. }
  667. else {
  668. updateSize = in->size;
  669. update = (in->size > 0 && in->last == FALSE);
  670. }
  671. if (TRUE == update && FALSE == ctx->seqChangeReqest) {
  672. if ((ret=VPU_DecUpdateBitstreamBuffer(ctx->handle, updateSize)) != RETCODE_SUCCESS) {
  673. VLOG(INFO, "<%s:%d> Failed to VPU_DecUpdateBitstreamBuffer() ret(%d)\n", __FUNCTION__, __LINE__, ret);
  674. return FALSE;
  675. }
  676. if (TRUE == in->last) {
  677. VPU_DecUpdateBitstreamBuffer(ctx->handle, STREAM_END_SET_FLAG);
  678. }
  679. }
  680. in->reuse = FALSE;
  681. return TRUE;
  682. }
  683. static BOOL OpenDecoder(ComponentImpl* com)
  684. {
  685. DecoderContext* ctx = (DecoderContext*)com->context;
  686. ParamDecBitstreamBuffer bsBuf;
  687. CNMComponentParamRet ret;
  688. CNMComListenerDecOpen lspn = {0};
  689. BOOL success = FALSE;
  690. RetCode retCode;
  691. VpuReportConfig_t decReportCfg;
  692. ctx->stateDoing = TRUE;
  693. ret = ComponentGetParameter(com, com->srcPort.connectedComponent, GET_PARAM_FEEDER_BITSTREAM_BUF, &bsBuf);
  694. if (ComponentParamReturnTest(ret, &success) == FALSE) {
  695. return success;
  696. }
  697. ctx->decOpenParam.bitstreamBuffer = bsBuf.bs->phys_addr;
  698. ctx->decOpenParam.bitstreamBufferSize = bsBuf.bs->size * bsBuf.num;
  699. retCode = VPU_DecOpen(&ctx->handle, &ctx->decOpenParam);
  700. lspn.handle = ctx->handle;
  701. lspn.ret = retCode;
  702. ComponentNotifyListeners(com, COMPONENT_EVENT_DEC_OPEN, (void*)&lspn);
  703. if (retCode != RETCODE_SUCCESS) {
  704. VLOG(ERR, "<%s:%d> Failed to VPU_DecOpen(ret:%d)\n", __FUNCTION__, __LINE__, retCode);
  705. HandleDecoderError(NULL, 0, NULL);
  706. return FALSE;
  707. }
  708. //Enable Register logs
  709. //VPU_DecGiveCommand(ctx->handle, ENABLE_LOGGING, 0);
  710. osal_memset((void*)&decReportCfg, 0x00, sizeof(VpuReportConfig_t));
  711. decReportCfg.userDataEnable = ctx->testDecConfig.enableUserData;
  712. decReportCfg.userDataReportMode = 1;
  713. OpenDecReport(ctx->testDecConfig.coreIdx, &decReportCfg);
  714. ConfigDecReport(ctx->testDecConfig.coreIdx, ctx->handle, ctx->testDecConfig.bitFormat);
  715. if (ctx->testDecConfig.thumbnailMode == TRUE) {
  716. VPU_DecGiveCommand(ctx->handle, ENABLE_DEC_THUMBNAIL_MODE, NULL);
  717. }
  718. ctx->stateDoing = FALSE;
  719. return TRUE;
  720. }
  721. static BOOL DecodeHeader(ComponentImpl* com, PortContainerES* in, PortContainerDisplay* out)
  722. {
  723. DecoderContext* ctx = (DecoderContext*)com->context;
  724. DecHandle handle = ctx->handle;
  725. Uint32 coreIdx = ctx->testDecConfig.coreIdx;
  726. RetCode ret = RETCODE_SUCCESS;
  727. DEC_INT_STATUS status;
  728. DecInitialInfo* initialInfo = &ctx->initialInfo;
  729. SecAxiUse secAxiUse;
  730. CNMComListenerDecCompleteSeq lsnpCompleteSeq;
  731. CodStd bitFormat = ctx->testDecConfig.bitFormat;
  732. PhysicalAddress rdPtr, wrPtr;
  733. Uint32 room;
  734. if (FALSE == ctx->stateDoing) {
  735. ret = VPU_DecIssueSeqInit(handle);
  736. ComponentNotifyListeners(com, COMPONENT_EVENT_DEC_ISSUE_SEQ, NULL);
  737. if (ret != RETCODE_SUCCESS) {
  738. VLOG(ERR, "%s:%d Failed to VPU_DecIssueSeqInit() ret(%d)\n", __FUNCTION__, __LINE__, ret);
  739. return FALSE;
  740. }
  741. }
  742. ctx->stateDoing = TRUE;
  743. if (FALSE == com->terminate) {
  744. status=HandlingInterruptFlagNoCQ(com, INT_BIT_SEQ_INIT);
  745. if (status == DEC_INT_STATUS_TIMEOUT) {
  746. VPU_DecUpdateBitstreamBuffer(handle, STREAM_END_SIZE);
  747. VPU_SWReset(coreIdx, SW_RESET_FORCE, handle);
  748. VPU_DecUpdateBitstreamBuffer(handle, STREAM_END_CLEAR_FLAG); // To finish bitstream empty status
  749. return FALSE;
  750. }
  751. else if (status == DEC_INT_STATUS_DONE) {
  752. VLOG(INFO, "[INT] INT_BIT_SEQ_INIT \n");
  753. }
  754. else if (status == DEC_INT_STATUS_NONE) {
  755. //just re-try
  756. return TRUE;
  757. }
  758. else if (status == DEC_INT_STATUS_EMPTY) {
  759. //it occurs only when the total picture size is smaller than chunk size.
  760. return TRUE;
  761. }
  762. else {
  763. VLOG(INFO, "%s:%d Unknown interrupt status: %d\n", __FUNCTION__, __LINE__, status);
  764. return FALSE;
  765. }
  766. }
  767. ret = VPU_DecCompleteSeqInit(handle, initialInfo);
  768. strcpy(lsnpCompleteSeq.refYuvPath, ctx->testDecConfig.refYuvPath);
  769. lsnpCompleteSeq.ret = ret;
  770. lsnpCompleteSeq.initialInfo = initialInfo;
  771. lsnpCompleteSeq.wtlFormat = ctx->wtlFormat;
  772. lsnpCompleteSeq.cbcrInterleave = ctx->decOpenParam.cbcrInterleave;
  773. lsnpCompleteSeq.bitstreamFormat = ctx->decOpenParam.bitstreamFormat;
  774. ComponentNotifyListeners(com, COMPONENT_EVENT_DEC_COMPLETE_SEQ, (void*)&lsnpCompleteSeq);
  775. if (ret != RETCODE_SUCCESS) {
  776. VLOG(ERR, "%s:%d FAILED TO DEC_PIC_HDR: ret(%d), SEQERR(%08x)\n", __FUNCTION__, __LINE__, ret, initialInfo->seqInitErrReason);
  777. return FALSE;
  778. }
  779. if (BS_MODE_PIC_END == ctx->testDecConfig.bitstreamMode) {
  780. // first picture includes a coded frame in sequence header of Theora, ffmpeg
  781. if (STD_THO != bitFormat && STD_H263 != bitFormat && STD_RV != bitFormat && STD_VP8 !=bitFormat) {
  782. //Parsing of Header has been done.
  783. if (FALSE == CheckConsumedHeaderStream(ctx)) {
  784. ctx->hdrInfo.isHdrData = TRUE;
  785. osal_memset(ctx->hdrInfo.hdrData, 0x00, MAX_SIZE_HEADER_DATA);
  786. VPU_DecGetBitstreamBuffer(ctx->handle ,&rdPtr, &wrPtr, &room);
  787. ctx->hdrInfo.hdrDataSize = wrPtr - rdPtr;
  788. if (MAX_SIZE_HEADER_DATA < ctx->hdrInfo.hdrDataSize) {
  789. ctx->hdrInfo.hdrDataSize = 0;
  790. ctx->hdrInfo.isHdrData = FALSE;
  791. } else {
  792. vdi_read_memory(ctx->testDecConfig.coreIdx, rdPtr, ctx->hdrInfo.hdrData, wrPtr - rdPtr,ctx->testDecConfig.streamEndian);
  793. if (in) {
  794. in->reuse = FALSE;
  795. in->consumed = TRUE;
  796. }
  797. }
  798. } else {
  799. if (in) {
  800. in->reuse = FALSE;
  801. in->consumed = TRUE;
  802. }
  803. }
  804. }
  805. }
  806. if (STD_THO == bitFormat) {
  807. // first picture includes a coded frame in sequence header of Theora, ffmpeg
  808. ctx->isTheoraHeader = TRUE;
  809. }
  810. if (ctx->decOpenParam.wtlEnable == TRUE) {
  811. VPU_DecGiveCommand(ctx->handle, DEC_SET_WTL_FRAME_FORMAT, &ctx->wtlFormat);
  812. }
  813. /* Setting up the secondary AXI is depending on H/W configuration.
  814. * Note that turn off all the secondary AXI configuration
  815. * if target ASIC has no memory only for IP, LF and BIT.
  816. */
  817. osal_memset(&secAxiUse, 0x00, sizeof(SecAxiUse));
  818. secAxiUse.u.coda9.useBitEnable = (ctx->testDecConfig.secondaryAXI>>0) & 0x01;
  819. secAxiUse.u.coda9.useIpEnable = (ctx->testDecConfig.secondaryAXI>>1) & 0x01;
  820. secAxiUse.u.coda9.useDbkYEnable = (ctx->testDecConfig.secondaryAXI>>2) & 0x01;
  821. secAxiUse.u.coda9.useDbkCEnable = (ctx->testDecConfig.secondaryAXI>>3) & 0x01;
  822. secAxiUse.u.coda9.useOvlEnable = (ctx->testDecConfig.secondaryAXI>>4) & 0x01;
  823. secAxiUse.u.coda9.useBtpEnable = (ctx->testDecConfig.secondaryAXI>>5) & 0x01;
  824. VPU_DecGiveCommand(ctx->handle, SET_SEC_AXI, &secAxiUse);
  825. ctx->stateDoing = FALSE;
  826. return TRUE;
  827. }
  828. static BOOL ExecuteDecoder(ComponentImpl* com, PortContainer* in, PortContainer* out)
  829. {
  830. DecoderContext* ctx = (DecoderContext*)com->context;
  831. BOOL ret = FALSE;
  832. BitStreamMode bsMode = ctx->decOpenParam.bitstreamMode;
  833. if (ctx->doReset == TRUE) {
  834. if (DoReset(com) == FALSE) return FALSE;
  835. }
  836. if (ctx->state == DEC_STATE_INIT_SEQ || ctx->state == DEC_STATE_DECODING) {
  837. if (UpdateBitstream(ctx, (PortContainerES*)in) == FALSE) {
  838. return FALSE;
  839. }
  840. if (in) {
  841. // In ring-buffer mode, it has to return back a container immediately.
  842. if (bsMode == BS_MODE_PIC_END) {
  843. if (ctx->state == DEC_STATE_INIT_SEQ) {
  844. in->reuse = TRUE;
  845. }
  846. in->consumed = (in->reuse == FALSE);
  847. }
  848. else {
  849. in->consumed = (in->reuse == FALSE);
  850. }
  851. } else { // NULL == in
  852. if (BS_MODE_PIC_END == bsMode) {
  853. if (DEC_STATE_INIT_SEQ == ctx->state || DEC_STATE_DECODING == ctx->state) {
  854. return TRUE;
  855. }
  856. }
  857. else {
  858. if (DEC_STATE_INIT_SEQ == ctx->state) {
  859. return TRUE;
  860. }
  861. }
  862. }
  863. }
  864. switch (ctx->state) {
  865. case DEC_STATE_OPEN_DECODER:
  866. ret = OpenDecoder(com);
  867. if (ctx->stateDoing == FALSE) ctx->state = DEC_STATE_INIT_SEQ;
  868. break;
  869. case DEC_STATE_INIT_SEQ:
  870. ret = DecodeHeader(com, (PortContainerES*)in, (PortContainerDisplay*)out);
  871. if (ctx->stateDoing == FALSE) ctx->state = DEC_STATE_REGISTER_FB;
  872. break;
  873. case DEC_STATE_REGISTER_FB:
  874. ret = RegisterFrameBuffers(com);
  875. if (ctx->stateDoing == FALSE) {
  876. ctx->state = DEC_STATE_DECODING;
  877. DisplayDecodedInformation(ctx->handle, ctx->decOpenParam.bitstreamFormat, 0, NULL, ctx->testDecConfig.performance, 0);
  878. }
  879. break;
  880. case DEC_STATE_DECODING:
  881. ret = Decode(com, (PortContainerES*)in, (PortContainerDisplay*)out);
  882. break;
  883. default:
  884. ret = FALSE;
  885. break;
  886. }
  887. if (ctx->handle) {
  888. PhysicalAddress rdPtr, wrPtr;
  889. Uint32 room;
  890. VPU_DecGetBitstreamBuffer(ctx->handle, &rdPtr, &wrPtr, &room);
  891. ctx->bsPos.rdPtr = rdPtr;
  892. ctx->bsPos.wrPtr = wrPtr;
  893. ctx->bsPos.avail = room;
  894. }
  895. if (ret == FALSE || com->terminate == TRUE) {
  896. ComponentNotifyListeners(com, COMPONENT_EVENT_DEC_DECODED_ALL, (void*)ctx->handle);
  897. if (out) {
  898. out->reuse = FALSE;
  899. out->last = TRUE;
  900. }
  901. }
  902. return ret;
  903. }
  904. static BOOL PrepareDecoder(ComponentImpl* com, BOOL* done)
  905. {
  906. *done = TRUE;
  907. return TRUE;
  908. }
  909. static void ReleaseDecoder(ComponentImpl* com)
  910. {
  911. }
  912. static BOOL DestroyDecoder(ComponentImpl* com)
  913. {
  914. DecoderContext* ctx = (DecoderContext*)com->context;
  915. BOOL success = TRUE;
  916. RetCode ret = RETCODE_SUCCESS;
  917. CloseDecReport (ctx->handle);
  918. VPU_DecUpdateBitstreamBuffer(ctx->handle, STREAM_END_SET_FLAG);
  919. ret = VPU_DecClose(ctx->handle);
  920. if (RETCODE_SUCCESS != ret) {
  921. VLOG(ERR, "%s:%d FAILED TO VPU_DecClose() ret(%d)\n", __FUNCTION__, __LINE__, ret);
  922. }
  923. Queue_Destroy(ctx->idxFrameDisQ);
  924. ComponentNotifyListeners(com, COMPONENT_EVENT_DEC_CLOSE, NULL);
  925. if (ctx->vbUserData.size) {
  926. vdi_free_dma_memory(ctx->testDecConfig.coreIdx, &ctx->vbUserData, DEC_ETC, ctx->handle->instIndex);
  927. }
  928. VPU_DeInit(ctx->decOpenParam.coreIdx);
  929. osal_free(ctx);
  930. return success;
  931. }
  932. static Component CreateDecoder(ComponentImpl* com, CNMComponentConfig* componentParam)
  933. {
  934. DecoderContext* ctx;
  935. Uint32 coreIdx = componentParam->testDecConfig.coreIdx;
  936. Uint16* firmware = (Uint16*)componentParam->bitcode;
  937. Uint32 firmwareSize = componentParam->sizeOfBitcode;
  938. RetCode retCode;
  939. Int32 productId = PRODUCT_ID_NONE;
  940. retCode = VPU_InitWithBitcode(coreIdx, firmware, firmwareSize);
  941. if (retCode != RETCODE_SUCCESS && retCode != RETCODE_CALLED_BEFORE) {
  942. VLOG(INFO, "%s:%d Failed to VPU_InitiWithBitcode, ret(%08x)\n", __FUNCTION__, __LINE__, retCode);
  943. return FALSE;
  944. }
  945. com->context = (DecoderContext*)osal_malloc(sizeof(DecoderContext));
  946. osal_memset(com->context, 0, sizeof(DecoderContext));
  947. ctx = (DecoderContext*)com->context;
  948. productId = VPU_GetProductId(coreIdx);
  949. VLOG(INFO, "PRODUCT ID: %d\n", productId);
  950. PrintVpuVersionInfo(coreIdx);
  951. memcpy(&(ctx->decOpenParam), &(componentParam->decOpenParam), sizeof(DecOpenParam));
  952. ctx->wtlFormat = componentParam->testDecConfig.wtlFormat;
  953. ctx->frameNumToStop = componentParam->testDecConfig.forceOutNum;
  954. ctx->testDecConfig = componentParam->testDecConfig;
  955. ctx->state = DEC_STATE_OPEN_DECODER;
  956. ctx->stateDoing = FALSE;
  957. ctx->idxFrameDisQ = Queue_Create(INDEX_FRAME_DIS_QUEUE_SIZE, sizeof(Int32));
  958. return (Component)com;
  959. }
  960. ComponentImpl coda9DecoderComponentImpl = {
  961. "coda9_decoder",
  962. NULL,
  963. {0,},
  964. {0,},
  965. sizeof(PortContainerDisplay),
  966. 5,
  967. CreateDecoder,
  968. GetParameterDecoder,
  969. SetParameterDecoder,
  970. PrepareDecoder,
  971. ExecuteDecoder,
  972. ReleaseDecoder,
  973. DestroyDecoder
  974. };