component_dec_decoder.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072
  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. #define EXTRA_FRAME_BUFFER_NUM 1
  30. typedef enum {
  31. DEC_INT_STATUS_NONE, // Interrupt not asserted yet
  32. DEC_INT_STATUS_EMPTY, // Need more es
  33. DEC_INT_STATUS_DONE, // Interrupt asserted
  34. DEC_INT_STATUS_TIMEOUT, // Interrupt not asserted during given time.
  35. } DEC_INT_STATUS;
  36. typedef enum {
  37. DEC_STATE_NONE,
  38. DEC_STATE_OPEN_DECODER,
  39. DEC_STATE_INIT_SEQ,
  40. DEC_STATE_REGISTER_FB,
  41. DEC_STATE_DECODING,
  42. DEC_STATE_CLOSE,
  43. } DecoderState;
  44. typedef struct {
  45. TestDecConfig testDecConfig;
  46. DecOpenParam decOpenParam;
  47. DecParam decParam;
  48. FrameBufferFormat wtlFormat;
  49. DecHandle handle;
  50. Uint64 startTimeout;
  51. vpu_buffer_t vbUserData;
  52. BOOL doFlush;
  53. BOOL stateDoing;
  54. DecoderState state;
  55. DecInitialInfo initialInfo;
  56. Uint32 numDecoded; /*!<< The number of decoded frames */
  57. Uint32 numOutput;
  58. PhysicalAddress decodedAddr;
  59. Uint32 frameNumToStop;
  60. BOOL doReset;
  61. Uint32 cyclePerTick;
  62. VpuAttr attr;
  63. struct {
  64. BOOL enable;
  65. Uint32 skipCmd; /*!<< a skip command to be restored */
  66. } autoErrorRecovery;
  67. } DecoderContext;
  68. static BOOL RegisterFrameBuffers(ComponentImpl* com)
  69. {
  70. DecoderContext* ctx = (DecoderContext*)com->context;
  71. FrameBuffer* pFrame = NULL;
  72. Uint32 framebufStride = 0;
  73. ParamDecFrameBuffer paramFb;
  74. RetCode result;
  75. DecInitialInfo* codecInfo = &ctx->initialInfo;
  76. BOOL success;
  77. CNMComponentParamRet ret;
  78. CNMComListenerDecRegisterFb lsnpRegisterFb;
  79. ctx->stateDoing = TRUE;
  80. ret = ComponentGetParameter(com, com->sinkPort.connectedComponent, GET_PARAM_RENDERER_FRAME_BUF, (void*)&paramFb);
  81. if (ComponentParamReturnTest(ret, &success) == FALSE) {
  82. return success;
  83. }
  84. pFrame = paramFb.fb;
  85. framebufStride = paramFb.stride;
  86. VLOG(TRACE, "<%s> COMPRESSED: %d, LINEAR: %d\n", __FUNCTION__, paramFb.nonLinearNum, paramFb.linearNum);
  87. if (ctx->attr.productId == PRODUCT_ID_521 && ctx->attr.supportDualCore == TRUE) {
  88. if (ctx->initialInfo.lumaBitdepth == 8 && ctx->initialInfo.chromaBitdepth == 8)
  89. result = VPU_DecRegisterFrameBufferEx(ctx->handle, pFrame, paramFb.nonLinearNum, paramFb.linearNum, framebufStride, codecInfo->picHeight, COMPRESSED_FRAME_MAP_DUAL_CORE_8BIT);
  90. else
  91. result = VPU_DecRegisterFrameBufferEx(ctx->handle, pFrame, paramFb.nonLinearNum, paramFb.linearNum, framebufStride, codecInfo->picHeight, COMPRESSED_FRAME_MAP_DUAL_CORE_10BIT);
  92. }
  93. else {
  94. result = VPU_DecRegisterFrameBufferEx(ctx->handle, pFrame, paramFb.nonLinearNum, paramFb.linearNum, framebufStride, codecInfo->picHeight, COMPRESSED_FRAME_MAP);
  95. }
  96. lsnpRegisterFb.handle = ctx->handle;
  97. lsnpRegisterFb.numNonLinearFb = paramFb.nonLinearNum;
  98. lsnpRegisterFb.numLinearFb = paramFb.linearNum;
  99. ComponentNotifyListeners(com, COMPONENT_EVENT_DEC_REGISTER_FB, (void*)&lsnpRegisterFb);
  100. if (result != RETCODE_SUCCESS) {
  101. VLOG(ERR, "%s:%d Failed to VPU_DecRegisterFrameBufferEx(%d)\n", __FUNCTION__, __LINE__, result);
  102. ChekcAndPrintDebugInfo(ctx->handle, FALSE, result);
  103. return FALSE;
  104. }
  105. ctx->stateDoing = FALSE;
  106. return TRUE;
  107. }
  108. static BOOL SequenceChange(ComponentImpl* com, DecOutputInfo* outputInfo)
  109. {
  110. DecoderContext* ctx = (DecoderContext*)com->context;
  111. BOOL dpbChanged, sizeChanged, bitDepthChanged;
  112. Uint32 sequenceChangeFlag = outputInfo->sequenceChanged;
  113. dpbChanged = (sequenceChangeFlag&SEQ_CHANGE_ENABLE_DPB_COUNT) ? TRUE : FALSE;
  114. sizeChanged = (sequenceChangeFlag&SEQ_CHANGE_ENABLE_SIZE) ? TRUE : FALSE;
  115. bitDepthChanged = (sequenceChangeFlag&SEQ_CHANGE_ENABLE_BITDEPTH) ? TRUE : FALSE;
  116. if (dpbChanged || sizeChanged || bitDepthChanged) {
  117. DecInitialInfo initialInfo;
  118. VLOG(INFO, "----- SEQUENCE CHANGED -----\n");
  119. // Get current(changed) sequence information.
  120. VPU_DecGiveCommand(ctx->handle, DEC_GET_SEQ_INFO, &initialInfo);
  121. // Flush all remaining framebuffers of previous sequence.
  122. VLOG(INFO, "sequenceChanged : %x\n", sequenceChangeFlag);
  123. VLOG(INFO, "SEQUENCE NO : %d\n", initialInfo.sequenceNo);
  124. VLOG(INFO, "DPB COUNT : %d\n", initialInfo.minFrameBufferCount);
  125. VLOG(INFO, "BITDEPTH : LUMA(%d), CHROMA(%d)\n", initialInfo.lumaBitdepth, initialInfo.chromaBitdepth);
  126. VLOG(INFO, "SIZE : WIDTH(%d), HEIGHT(%d)\n", initialInfo.picWidth, initialInfo.picHeight);
  127. ComponentSetParameter(com, com->sinkPort.connectedComponent, SET_PARAM_RENDERER_FREE_FRAMEBUFFERS, (void*)&outputInfo->frameDisplayFlag);
  128. VPU_DecGiveCommand(ctx->handle, DEC_RESET_FRAMEBUF_INFO, NULL);
  129. if (ctx->testDecConfig.scaleDownWidth > 0 || ctx->testDecConfig.scaleDownHeight > 0) {
  130. ScalerInfo sclInfo = {0};
  131. sclInfo.scaleWidth = CalcScaleDown(initialInfo.picWidth, ctx->testDecConfig.scaleDownWidth);
  132. sclInfo.scaleHeight = CalcScaleDown(initialInfo.picHeight, ctx->testDecConfig.scaleDownHeight);
  133. VLOG(INFO, "[SCALE INFO] %dx%d to %dx%d\n", initialInfo.picWidth, initialInfo.picHeight, sclInfo.scaleWidth, sclInfo.scaleHeight);
  134. sclInfo.enScaler = TRUE;
  135. if (VPU_DecGiveCommand(ctx->handle, DEC_SET_SCALER_INFO, (void*)&sclInfo) != RETCODE_SUCCESS) {
  136. VLOG(ERR, "Failed to VPU_DecGiveCommand(DEC_SET_SCALER_INFO)\n");
  137. return FALSE;
  138. }
  139. }
  140. ctx->state = DEC_STATE_REGISTER_FB;
  141. osal_memcpy((void*)&ctx->initialInfo, (void*)&initialInfo, sizeof(DecInitialInfo));
  142. ComponentSetParameter(com, com->sinkPort.connectedComponent, SET_PARAM_RENDERER_ALLOC_FRAMEBUFFERS, NULL);
  143. VLOG(INFO, "----------------------------\n");
  144. }
  145. return TRUE;
  146. }
  147. static BOOL CheckAndDoSequenceChange(ComponentImpl* com, DecOutputInfo* outputInfo)
  148. {
  149. if (outputInfo->sequenceChanged == 0) {
  150. return TRUE;
  151. }
  152. else {
  153. return SequenceChange(com, outputInfo);
  154. }
  155. }
  156. static void ClearDpb(ComponentImpl* com, BOOL backupDpb)
  157. {
  158. DecoderContext* ctx = (DecoderContext*)com->context;
  159. Uint32 timeoutCount;
  160. Int32 intReason;
  161. DecOutputInfo outputInfo;
  162. BOOL pause;
  163. Uint32 idx;
  164. Uint32 flushedFbs = 0;
  165. QueueStatusInfo cqInfo;
  166. const Uint32 flushTimeout = 100;
  167. if (TRUE == backupDpb) {
  168. pause = TRUE;
  169. ComponentSetParameter(com, com->sinkPort.connectedComponent, SET_PARAM_COM_PAUSE, (void*)&pause);
  170. }
  171. /* Send the renderer the signal to drop all frames.
  172. * VPU_DecClrDispFlag() is called in SE_PARAM_RENDERER_FLUSH.
  173. */
  174. ComponentSetParameter(com, com->sinkPort.connectedComponent, SET_PARAM_RENDERER_FLUSH, (void*)&flushedFbs);
  175. while (RETCODE_SUCCESS == VPU_DecGetOutputInfo(ctx->handle, &outputInfo)) {
  176. if (0 <= outputInfo.indexFrameDisplay) {
  177. flushedFbs |= outputInfo.indexFrameDisplay;
  178. VPU_DecClrDispFlag(ctx->handle, outputInfo.indexFrameDisplay);
  179. VLOG(INFO, "<%s> FLUSH DPB INDEX: %d\n", __FUNCTION__, outputInfo.indexFrameDisplay);
  180. }
  181. osal_msleep(1);
  182. }
  183. VLOG(INFO, "========== FLUSH FRAMEBUFFER & CMDs ========== \n");
  184. timeoutCount = 0;
  185. while (VPU_DecFrameBufferFlush(ctx->handle, NULL, NULL) == RETCODE_VPU_STILL_RUNNING) {
  186. /* Clear an interrupt */
  187. if (0 < (intReason=VPU_WaitInterruptEx(ctx->handle, VPU_WAIT_TIME_OUT_CQ))) {
  188. VPU_ClearInterruptEx(ctx->handle, intReason);
  189. VPU_DecGetOutputInfo(ctx->handle, &outputInfo); // ignore the return value and outputinfo
  190. if (0 <= outputInfo.indexFrameDisplay) {
  191. flushedFbs |= outputInfo.indexFrameDisplay;
  192. VPU_DecClrDispFlag(ctx->handle, outputInfo.indexFrameDisplay);
  193. }
  194. }
  195. if (timeoutCount >= flushTimeout) {
  196. VLOG(ERR, "NO RESPONSE FROM VPU_DecFrameBufferFlush()\n");
  197. return;
  198. }
  199. timeoutCount++;
  200. }
  201. VPU_DecGetOutputInfo(ctx->handle, &outputInfo);
  202. VPU_DecGiveCommand(ctx->handle, DEC_GET_QUEUE_STATUS, &cqInfo);
  203. VLOG(INFO, "<%s> REPORT_QUEUE(%d), INSTANCE_QUEUE(%d)\n", __FUNCTION__, cqInfo.reportQueueCount, cqInfo.instanceQueueCount);
  204. if (TRUE == backupDpb) {
  205. for (idx=0; idx<32; idx++) {
  206. if (flushedFbs & (1<<idx)) {
  207. VLOG(INFO, "SET DISPLAY FLAG : %d\n", idx);
  208. VPU_DecGiveCommand(ctx->handle, DEC_SET_DISPLAY_FLAG , &idx);
  209. }
  210. }
  211. pause = FALSE;
  212. ComponentSetParameter(com, com->sinkPort.connectedComponent, SET_PARAM_COM_PAUSE, (void*)&pause);
  213. }
  214. }
  215. static void ClearCpb(ComponentImpl* com)
  216. {
  217. DecoderContext* ctx = (DecoderContext*)com->context;
  218. PhysicalAddress curRdPtr, curWrPtr;
  219. if (BS_MODE_INTERRUPT == ctx->decOpenParam.bitstreamMode) {
  220. /* Clear CPB */
  221. // In order to stop processing bitstream.
  222. VLOG(INFO, "CLEAR CPB\n");
  223. VPU_DecUpdateBitstreamBuffer(ctx->handle, EXPLICIT_END_SET_FLAG);
  224. VPU_DecGetBitstreamBuffer(ctx->handle, &curRdPtr, &curWrPtr, NULL);
  225. VPU_DecSetRdPtr(ctx->handle, curWrPtr, TRUE);
  226. }
  227. }
  228. static BOOL PrepareSkip(ComponentImpl* com)
  229. {
  230. DecoderContext* ctx = (DecoderContext*)com->context;
  231. // Flush the decoder
  232. if (ctx->doFlush == TRUE) {
  233. ClearDpb(com, FALSE);
  234. ClearCpb(com);
  235. ctx->doFlush = FALSE;
  236. }
  237. return TRUE;
  238. }
  239. static DEC_INT_STATUS HandlingInterruptFlag(ComponentImpl* com)
  240. {
  241. DecoderContext* ctx = (DecoderContext*)com->context;
  242. DecHandle handle = ctx->handle;
  243. Int32 interruptFlag = 0;
  244. Uint32 interruptWaitTime = VPU_WAIT_TIME_OUT_CQ;
  245. Uint32 interruptTimeout = VPU_DEC_TIMEOUT;
  246. DEC_INT_STATUS status = DEC_INT_STATUS_NONE;
  247. CNMComListenerDecInt lsn;
  248. if (ctx->startTimeout == 0ULL) {
  249. ctx->startTimeout = osal_gettime();
  250. }
  251. do {
  252. interruptFlag = VPU_WaitInterruptEx(handle, interruptWaitTime);
  253. if (INTERRUPT_TIMEOUT_VALUE == interruptFlag) {
  254. Uint64 currentTimeout = osal_gettime();
  255. if (0 < interruptTimeout && (currentTimeout - ctx->startTimeout) > interruptTimeout) {
  256. VLOG(ERR, "\n INSNTANCE #%d INTERRUPT TIMEOUT.\n", handle->instIndex);
  257. status = DEC_INT_STATUS_TIMEOUT;
  258. break;
  259. }
  260. interruptFlag = 0;
  261. }
  262. if (interruptFlag < 0) {
  263. VLOG(ERR, "<%s:%d> interruptFlag is negative value! %08x\n", __FUNCTION__, __LINE__, interruptFlag);
  264. }
  265. if (interruptFlag > 0) {
  266. VPU_ClearInterruptEx(handle, interruptFlag);
  267. ctx->startTimeout = 0ULL;
  268. status = DEC_INT_STATUS_DONE;
  269. if (interruptFlag & (1<<INT_WAVE5_INIT_SEQ)) {
  270. break;
  271. }
  272. if (interruptFlag & (1<<INT_WAVE5_DEC_PIC)) {
  273. break;
  274. }
  275. if (interruptFlag & (1<<INT_WAVE5_BSBUF_EMPTY)) {
  276. status = DEC_INT_STATUS_EMPTY;
  277. break;
  278. }
  279. }
  280. } while (FALSE);
  281. if (interruptFlag != 0) {
  282. lsn.handle = handle;
  283. lsn.flag = interruptFlag;
  284. lsn.decIndex = ctx->numDecoded;
  285. ComponentNotifyListeners(com, COMPONENT_EVENT_DEC_INTERRUPT, (void*)&lsn);
  286. }
  287. return status;
  288. }
  289. static BOOL DoReset(ComponentImpl* com)
  290. {
  291. DecoderContext* ctx = (DecoderContext*)com->context;
  292. DecHandle handle = ctx->handle;
  293. BOOL pause = TRUE;
  294. VLOG(INFO, "========== %s ==========\n", __FUNCTION__);
  295. ComponentSetParameter(com, com->srcPort.connectedComponent, SET_PARAM_COM_PAUSE, (void*)&pause);
  296. ComponentSetParameter(com, com->srcPort.connectedComponent, SET_PARAM_FEEDER_RESET, (void*)&pause);
  297. ClearDpb(com, FALSE);
  298. VLOG(INFO, "> Reset VPU\n");
  299. if (VPU_SWReset(handle->coreIdx, SW_RESET_SAFETY, handle) != RETCODE_SUCCESS) {
  300. VLOG(ERR, "<%s:%d> Failed to VPU_SWReset()\n", __FUNCTION__, __LINE__);
  301. return FALSE;
  302. }
  303. ClearCpb(com);
  304. VLOG(INFO, "========== %s ==========\n", __FUNCTION__);
  305. ComponentNotifyListeners(com, COMPONENT_EVENT_DEC_RESET_DONE, NULL);
  306. VLOG(INFO, "> FLUSH INPUT PORT\n");
  307. ComponentPortFlush(com);
  308. pause = FALSE;
  309. ComponentSetParameter(com, com->srcPort.connectedComponent, SET_PARAM_COM_PAUSE, (void*)&pause);
  310. ctx->doReset = FALSE;
  311. ctx->startTimeout = 0ULL;
  312. ctx->autoErrorRecovery.enable = TRUE;
  313. ctx->autoErrorRecovery.skipCmd = ctx->decParam.skipframeMode;
  314. ctx->decParam.skipframeMode = WAVE_SKIPMODE_NON_IRAP;
  315. return TRUE;
  316. }
  317. static BOOL Decode(ComponentImpl* com, PortContainerES* in, PortContainerDisplay* out)
  318. {
  319. DecoderContext* ctx = (DecoderContext*)com->context;
  320. DecOutputInfo decOutputInfo;
  321. DEC_INT_STATUS intStatus;
  322. CNMComListenerStartDecOneFrame lsnpPicStart;
  323. BitStreamMode bsMode = ctx->decOpenParam.bitstreamMode;
  324. RetCode result;
  325. CNMComListenerDecDone lsnpPicDone;
  326. CNMComListenerDecReadyOneFrame lsnpReadyOneFrame = {0,};
  327. BOOL doDecode;
  328. QueueStatusInfo qStatus;
  329. lsnpReadyOneFrame.handle = ctx->handle;
  330. ComponentNotifyListeners(com, COMPONENT_EVENT_DEC_READY_ONE_FRAME, (void*)&lsnpReadyOneFrame);
  331. ctx->stateDoing = TRUE;
  332. if (PrepareSkip(com) == FALSE) {
  333. return FALSE;
  334. }
  335. /* decode a frame except when the bitstream mode is PIC_END and no data */
  336. doDecode = !(bsMode == BS_MODE_PIC_END && in == NULL);
  337. doDecode &= (BOOL)(com->pause == FALSE);
  338. VPU_DecGiveCommand(ctx->handle, DEC_GET_QUEUE_STATUS, &qStatus);
  339. if (COMMAND_QUEUE_DEPTH == qStatus.instanceQueueCount) {
  340. doDecode = FALSE;
  341. }
  342. /* The simple load balancer : To use this function, call InitLoadBalancer() before decoding process. */
  343. if (TRUE == doDecode) {
  344. doDecode = LoadBalancerGetMyTurn(ctx->handle->instIndex);
  345. }
  346. if (TRUE == doDecode) {
  347. result = VPU_DecStartOneFrame(ctx->handle, &ctx->decParam);
  348. lsnpPicStart.result = result;
  349. lsnpPicStart.decParam = ctx->decParam;
  350. ComponentNotifyListeners(com, COMPONENT_EVENT_DEC_START_ONE_FRAME, (void*)&lsnpPicStart);
  351. if (result == RETCODE_SUCCESS) {
  352. /* The simple load balancer : pass its turn */
  353. LoadBalancerSetNextTurn();
  354. }
  355. else if (result == RETCODE_QUEUEING_FAILURE) {
  356. // Just retry
  357. if (in) in->reuse = (bsMode == BS_MODE_PIC_END);
  358. VPU_DecGiveCommand(ctx->handle, DEC_GET_QUEUE_STATUS, (void*)&qStatus);
  359. }
  360. else if (result == RETCODE_VPU_RESPONSE_TIMEOUT) {
  361. VLOG(INFO, "<%s:%d> Failed to VPU_DecStartOneFrame() ret(%d)\n", __FUNCTION__, __LINE__, result);
  362. CNMErrorSet(CNM_ERROR_HANGUP);
  363. HandleDecoderError(ctx->handle, ctx->numDecoded, NULL);
  364. return FALSE;
  365. }
  366. else {
  367. ChekcAndPrintDebugInfo(ctx->handle, FALSE, result);
  368. return FALSE;
  369. }
  370. }
  371. else {
  372. if (in) in->reuse = (bsMode == BS_MODE_PIC_END);
  373. }
  374. intStatus=HandlingInterruptFlag(com);
  375. switch (intStatus) {
  376. case DEC_INT_STATUS_TIMEOUT:
  377. DoReset(com);
  378. return FALSE;
  379. case DEC_INT_STATUS_EMPTY:
  380. case DEC_INT_STATUS_NONE:
  381. return TRUE;
  382. default:
  383. break;
  384. }
  385. // Get data from the sink component.
  386. if ((result=VPU_DecGetOutputInfo(ctx->handle, &decOutputInfo)) == RETCODE_SUCCESS) {
  387. DisplayDecodedInformation(ctx->handle, ctx->decOpenParam.bitstreamFormat, ctx->numDecoded, &decOutputInfo, ctx->testDecConfig.performance, ctx->cyclePerTick);
  388. }
  389. lsnpPicDone.handle = ctx->handle;
  390. lsnpPicDone.ret = result;
  391. lsnpPicDone.decParam = &ctx->decParam;
  392. lsnpPicDone.output = &decOutputInfo;
  393. lsnpPicDone.numDecoded = ctx->numDecoded;
  394. lsnpPicDone.vbUser = ctx->vbUserData;
  395. ComponentNotifyListeners(com, COMPONENT_EVENT_DEC_GET_OUTPUT_INFO, (void*)&lsnpPicDone);
  396. if (result == RETCODE_REPORT_NOT_READY) {
  397. return TRUE; // Not decoded yet. Try again
  398. }
  399. else if (result != RETCODE_SUCCESS) {
  400. /* ERROR */
  401. VLOG(ERR, "Failed to decode error\n");
  402. ChekcAndPrintDebugInfo(ctx->handle, FALSE, result);
  403. return FALSE;
  404. }
  405. if ((decOutputInfo.decodingSuccess & 0x01) == 0) {
  406. VLOG(ERR, "VPU_DecGetOutputInfo decode fail framdIdx %d error(0x%08x) reason(0x%08x), reasonExt(0x%08x)\n",
  407. ctx->numDecoded, decOutputInfo.decodingSuccess, decOutputInfo.errorReason, decOutputInfo.errorReasonExt);
  408. if (WAVE5_SYSERR_WATCHDOG_TIMEOUT == decOutputInfo.errorReason || WAVE5_SYSERR_DEC_VLC_BUF_FULL == decOutputInfo.errorReason) {
  409. VLOG(ERR, "WAVE5_SYSERR_WATCHDOG_TIMEOUT\n");
  410. }
  411. else if ( WAVE5_SYSERR_DEC_VLC_BUF_FULL == decOutputInfo.errorReason) {
  412. VLOG(ERR, "VLC_BUFFER FULL\n");
  413. }
  414. else if (decOutputInfo.errorReason == WAVE5_SPECERR_OVER_PICTURE_WIDTH_SIZE || decOutputInfo.errorReason == WAVE5_SPECERR_OVER_PICTURE_HEIGHT_SIZE) {
  415. VLOG(ERR, "Not supported Width or Height(%dx%d)\n", decOutputInfo.decPicWidth, decOutputInfo.decPicHeight);
  416. return FALSE;
  417. }
  418. }
  419. else {
  420. if (TRUE == ctx->autoErrorRecovery.enable) {
  421. ctx->decParam.skipframeMode = ctx->autoErrorRecovery.skipCmd;
  422. ctx->autoErrorRecovery.enable = FALSE;
  423. ctx->autoErrorRecovery.skipCmd = WAVE_SKIPMODE_WAVE_NONE;
  424. }
  425. }
  426. if (CheckAndDoSequenceChange(com, &decOutputInfo) == FALSE) {
  427. return FALSE;
  428. }
  429. if (decOutputInfo.indexFrameDecoded >= 0 || decOutputInfo.indexFrameDecoded == DECODED_IDX_FLAG_SKIP) {
  430. // Return a used data to a source port.
  431. ctx->numDecoded++;
  432. ctx->decodedAddr = decOutputInfo.bytePosFrameStart;
  433. out->reuse = FALSE;
  434. }
  435. if ((decOutputInfo.indexFrameDisplay >= 0) || (decOutputInfo.indexFrameDisplay == DISPLAY_IDX_FLAG_SEQ_END)) {
  436. ctx->numOutput++;
  437. out->last = (BOOL)(decOutputInfo.indexFrameDisplay == DISPLAY_IDX_FLAG_SEQ_END);
  438. out->reuse = FALSE;
  439. }
  440. if (decOutputInfo.indexFrameDisplay == DISPLAY_IDX_FLAG_SEQ_END) {
  441. ctx->stateDoing = FALSE;
  442. com->terminate = TRUE;
  443. }
  444. if (out->reuse == FALSE) {
  445. osal_memcpy((void*)&out->decInfo, (void*)&decOutputInfo, sizeof(DecOutputInfo));
  446. }
  447. if (ctx->frameNumToStop > 0) {
  448. if (ctx->frameNumToStop == ctx->numOutput) {
  449. com->terminate = TRUE;
  450. }
  451. }
  452. return TRUE;
  453. }
  454. static CNMComponentParamRet GetParameterDecoder(ComponentImpl* from, ComponentImpl* com, GetParameterCMD commandType, void* data)
  455. {
  456. DecoderContext* ctx = (DecoderContext*)com->context;
  457. BOOL result = TRUE;
  458. PhysicalAddress rdPtr, wrPtr;
  459. Uint32 room;
  460. ParamDecBitstreamBufPos* bsPos = NULL;
  461. ParamDecNeedFrameBufferNum* fbNum;
  462. ParamVpuStatus* status;
  463. QueueStatusInfo cqInfo;
  464. PortContainerES* container;
  465. vpu_buffer_t vb;
  466. if (ctx->handle == NULL) return CNM_COMPONENT_PARAM_NOT_READY;
  467. if (ctx->doReset == TRUE) return CNM_COMPONENT_PARAM_NOT_READY;
  468. switch(commandType) {
  469. case GET_PARAM_COM_IS_CONTAINER_CONUSUMED:
  470. // This query command is sent from the comonponent core.
  471. // If input data are consumed in sequence, it should return TRUE through PortContainer::consumed.
  472. container = (PortContainerES*)data;
  473. vb = container->buf;
  474. if (vb.phys_addr <= ctx->decodedAddr && ctx->decodedAddr < (vb.phys_addr+vb.size)) {
  475. container->consumed = TRUE;
  476. ctx->decodedAddr = 0;
  477. }
  478. break;
  479. case GET_PARAM_DEC_HANDLE:
  480. *(DecHandle*)data = ctx->handle;
  481. break;
  482. case GET_PARAM_DEC_FRAME_BUF_NUM:
  483. if (ctx->state <= DEC_STATE_INIT_SEQ) return CNM_COMPONENT_PARAM_NOT_READY;
  484. fbNum = (ParamDecNeedFrameBufferNum*)data;
  485. fbNum->nonLinearNum = ctx->initialInfo.minFrameBufferCount + EXTRA_FRAME_BUFFER_NUM; // max_dec_pic_buffering
  486. if (ctx->decOpenParam.wtlEnable == TRUE) {
  487. fbNum->linearNum = (ctx->initialInfo.frameBufDelay+1) + EXTRA_FRAME_BUFFER_NUM; // The frameBufDelay can be zero.
  488. if ((ctx->decOpenParam.bitstreamFormat == STD_VP9) || (ctx->decOpenParam.bitstreamFormat == STD_AVS2) || (ctx->decOpenParam.bitstreamFormat == STD_AV1)) {
  489. fbNum->linearNum = fbNum->nonLinearNum;
  490. }
  491. if (ctx->testDecConfig.performance == TRUE) {
  492. if ((ctx->decOpenParam.bitstreamFormat == STD_VP9) || (ctx->decOpenParam.bitstreamFormat == STD_AVS2)) {
  493. fbNum->linearNum++;
  494. fbNum->nonLinearNum++;
  495. } else if (ctx->decOpenParam.bitstreamFormat == STD_AV1) {
  496. fbNum->linearNum++;
  497. fbNum->nonLinearNum++;
  498. }
  499. else {
  500. fbNum->linearNum += 3;
  501. }
  502. }
  503. }
  504. else {
  505. fbNum->linearNum = 0;
  506. }
  507. #ifdef USE_FEEDING_METHOD_BUFFER
  508. fbNum->linearNum = 10;
  509. #endif
  510. break;
  511. case GET_PARAM_DEC_BITSTREAM_BUF_POS:
  512. if (ctx->state < DEC_STATE_INIT_SEQ) return CNM_COMPONENT_PARAM_NOT_READY;
  513. VPU_DecGetBitstreamBuffer(ctx->handle, &rdPtr, &wrPtr, &room);
  514. bsPos = (ParamDecBitstreamBufPos*)data;
  515. bsPos->rdPtr = rdPtr;
  516. bsPos->wrPtr = wrPtr;
  517. bsPos->avail = room;
  518. break;
  519. case GET_PARAM_DEC_CODEC_INFO:
  520. if (ctx->state <= DEC_STATE_INIT_SEQ) return CNM_COMPONENT_PARAM_NOT_READY;
  521. VPU_DecGiveCommand(ctx->handle, DEC_GET_SEQ_INFO, data);
  522. break;
  523. case GET_PARAM_VPU_STATUS:
  524. if (ctx->state != DEC_STATE_DECODING) return CNM_COMPONENT_PARAM_NOT_READY;
  525. VPU_DecGiveCommand(ctx->handle, DEC_GET_QUEUE_STATUS, &cqInfo);
  526. status = (ParamVpuStatus*)data;
  527. status->cq = cqInfo;
  528. break;
  529. default:
  530. result = FALSE;
  531. break;
  532. }
  533. return (result == TRUE) ? CNM_COMPONENT_PARAM_SUCCESS : CNM_COMPONENT_PARAM_FAILURE;
  534. }
  535. static CNMComponentParamRet SetParameterDecoder(ComponentImpl* from, ComponentImpl* com, SetParameterCMD commandType, void* data)
  536. {
  537. BOOL result = TRUE;
  538. DecoderContext* ctx = (DecoderContext*)com->context;
  539. Int32 skipCmd;
  540. ParamDecTargetTid* tid = NULL;
  541. switch(commandType) {
  542. case SET_PARAM_COM_PAUSE:
  543. com->pause = *(BOOL*)data;
  544. break;
  545. case SET_PARAM_DEC_SKIP_COMMAND:
  546. skipCmd = *(Int32*)data;
  547. ctx->decParam.skipframeMode = skipCmd;
  548. if (skipCmd == WAVE_SKIPMODE_NON_IRAP) {
  549. Uint32 userDataMask = (1<<H265_USERDATA_FLAG_RECOVERY_POINT);
  550. ctx->decParam.craAsBlaFlag = TRUE;
  551. /* For finding recovery point */
  552. VPU_DecGiveCommand(ctx->handle, ENABLE_REP_USERDATA, &userDataMask);
  553. }
  554. else {
  555. ctx->decParam.craAsBlaFlag = FALSE;
  556. }
  557. if (ctx->numDecoded > 0) {
  558. ctx->doFlush = (BOOL)(ctx->decParam.skipframeMode == WAVE_SKIPMODE_NON_IRAP);
  559. }
  560. break;
  561. case SET_PARAM_DEC_RESET:
  562. ctx->doReset = TRUE;
  563. break;
  564. case SET_PARAM_DEC_TARGET_TID:
  565. tid = (ParamDecTargetTid*)data;
  566. /*! NOTE: DO NOT CHANGE THE ORDER OF COMMANDS BELOW. */
  567. VPU_DecGiveCommand(ctx->handle, DEC_SET_TEMPORAL_ID_MODE, (void*)&tid->tidMode);
  568. VPU_DecGiveCommand(ctx->handle, DEC_SET_TARGET_TEMPORAL_ID, (void*)&tid->targetTid);
  569. break;
  570. case SET_PARAM_DEC_FLUSH:
  571. ClearDpb(com, TRUE);
  572. break;
  573. default:
  574. result = FALSE;
  575. break;
  576. }
  577. return (result == TRUE) ? CNM_COMPONENT_PARAM_SUCCESS : CNM_COMPONENT_PARAM_FAILURE;
  578. }
  579. static BOOL UpdateBitstream(DecoderContext* ctx, PortContainerES* in)
  580. {
  581. RetCode ret = RETCODE_SUCCESS;
  582. BitStreamMode bsMode = ctx->decOpenParam.bitstreamMode;
  583. BOOL update = TRUE;
  584. Uint32 updateSize;
  585. if (in == NULL) return TRUE;
  586. if (bsMode == BS_MODE_PIC_END) {
  587. VPU_DecSetRdPtr(ctx->handle, in->buf.phys_addr, TRUE);
  588. }
  589. else {
  590. if (in->size > 0) {
  591. PhysicalAddress rdPtr, wrPtr;
  592. Uint32 room;
  593. VPU_DecGetBitstreamBuffer(ctx->handle, &rdPtr, &wrPtr, &room);
  594. if ((Int32)room < in->size) {
  595. in->reuse = TRUE;
  596. return TRUE;
  597. }
  598. }
  599. }
  600. if (in->last == TRUE) {
  601. updateSize = (in->size == 0) ? STREAM_END_SET_FLAG : in->size;
  602. }
  603. else {
  604. updateSize = in->size;
  605. update = (in->size > 0 && in->last == FALSE);
  606. }
  607. if (update == TRUE) {
  608. if ((ret=VPU_DecUpdateBitstreamBuffer(ctx->handle, updateSize)) != RETCODE_SUCCESS) {
  609. VLOG(INFO, "<%s:%d> Failed to VPU_DecUpdateBitstreamBuffer() ret(%d)\n", __FUNCTION__, __LINE__, ret);
  610. ChekcAndPrintDebugInfo(ctx->handle, FALSE, ret);
  611. return FALSE;
  612. }
  613. if (in->last == TRUE) {
  614. VPU_DecUpdateBitstreamBuffer(ctx->handle, STREAM_END_SET_FLAG);
  615. }
  616. }
  617. in->reuse = FALSE;
  618. return TRUE;
  619. }
  620. static BOOL OpenDecoder(ComponentImpl* com)
  621. {
  622. DecoderContext* ctx = (DecoderContext*)com->context;
  623. ParamDecBitstreamBuffer bsBuf;
  624. CNMComponentParamRet ret;
  625. CNMComListenerDecOpen lspn = {0};
  626. BOOL success = FALSE;
  627. BitStreamMode bsMode = ctx->decOpenParam.bitstreamMode;
  628. vpu_buffer_t vbUserData;
  629. RetCode retCode;
  630. ctx->stateDoing = TRUE;
  631. ret = ComponentGetParameter(com, com->srcPort.connectedComponent, GET_PARAM_FEEDER_BITSTREAM_BUF, &bsBuf);
  632. if (ComponentParamReturnTest(ret, &success) == FALSE) {
  633. return success;
  634. }
  635. ctx->decOpenParam.bitstreamBuffer = (bsMode == BS_MODE_PIC_END) ? 0 : bsBuf.bs->phys_addr;
  636. ctx->decOpenParam.bitstreamBufferSize = (bsMode == BS_MODE_PIC_END) ? 0 : bsBuf.bs->size;
  637. retCode = VPU_DecOpen(&ctx->handle, &ctx->decOpenParam);
  638. lspn.handle = ctx->handle;
  639. lspn.ret = retCode;
  640. ComponentNotifyListeners(com, COMPONENT_EVENT_DEC_OPEN, (void*)&lspn);
  641. if (retCode != RETCODE_SUCCESS) {
  642. VLOG(ERR, "<%s:%d> Failed to VPU_DecOpen(ret:%d)\n", __FUNCTION__, __LINE__, retCode);
  643. if ( retCode == RETCODE_VPU_RESPONSE_TIMEOUT)
  644. CNMErrorSet(CNM_ERROR_HANGUP);
  645. return FALSE;
  646. }
  647. LoadBalancerAddInstance(ctx->handle->instIndex);
  648. // VPU_DecGiveCommand(ctx->handle, ENABLE_LOGGING, 0);
  649. if (ctx->vbUserData.size == 0) {
  650. vbUserData.size = (1320*1024); /* 40KB * (queue_depth + report_queue_depth+1) = 40KB * (16 + 16 +1) */
  651. vdi_allocate_dma_memory(ctx->testDecConfig.coreIdx, &vbUserData, DEC_ETC, ctx->handle->instIndex);
  652. }
  653. VPU_DecGiveCommand(ctx->handle, SET_ADDR_REP_USERDATA, (void*)&vbUserData.phys_addr);
  654. VPU_DecGiveCommand(ctx->handle, SET_SIZE_REP_USERDATA, (void*)&vbUserData.size);
  655. VPU_DecGiveCommand(ctx->handle, ENABLE_REP_USERDATA, (void*)&ctx->testDecConfig.enableUserData);
  656. VPU_DecGiveCommand(ctx->handle, SET_CYCLE_PER_TICK, (void*)&ctx->cyclePerTick);
  657. if (ctx->testDecConfig.thumbnailMode == TRUE) {
  658. VPU_DecGiveCommand(ctx->handle, ENABLE_DEC_THUMBNAIL_MODE, NULL);
  659. }
  660. ctx->vbUserData = vbUserData;
  661. ctx->stateDoing = FALSE;
  662. return TRUE;
  663. }
  664. static BOOL DecodeHeader(ComponentImpl* com, PortContainerES* in, BOOL* done)
  665. {
  666. DecoderContext* ctx = (DecoderContext*)com->context;
  667. DecHandle handle = ctx->handle;
  668. Uint32 coreIdx = ctx->testDecConfig.coreIdx;
  669. RetCode ret = RETCODE_SUCCESS;
  670. DEC_INT_STATUS status;
  671. DecInitialInfo* initialInfo = &ctx->initialInfo;
  672. SecAxiUse secAxiUse;
  673. CNMComListenerDecCompleteSeq lsnpCompleteSeq;
  674. DecInfo* pDecInfo = VPU_HANDLE_TO_DECINFO(ctx->handle);
  675. *done = FALSE;
  676. if (BS_MODE_PIC_END == ctx->decOpenParam.bitstreamMode && NULL == in) {
  677. return TRUE;
  678. }
  679. if (ctx->stateDoing == FALSE) {
  680. /* previous state done */
  681. ret = VPU_DecIssueSeqInit(handle);
  682. if (RETCODE_QUEUEING_FAILURE == ret) {
  683. return TRUE; // Try again
  684. }
  685. ComponentNotifyListeners(com, COMPONENT_EVENT_DEC_ISSUE_SEQ, NULL);
  686. if (ret != RETCODE_SUCCESS) {
  687. ChekcAndPrintDebugInfo(ctx->handle, FALSE, ret);
  688. VLOG(ERR, "%s:%d Failed to VPU_DecIssueSeqInit() ret(%d)\n", __FUNCTION__, __LINE__, ret);
  689. return FALSE;
  690. }
  691. }
  692. ctx->stateDoing = TRUE;
  693. while (com->terminate == FALSE) {
  694. if ((status=HandlingInterruptFlag(com)) == DEC_INT_STATUS_DONE) {
  695. break;
  696. }
  697. else if (status == DEC_INT_STATUS_TIMEOUT) {
  698. HandleDecoderError(ctx->handle, 0, NULL);
  699. VPU_DecUpdateBitstreamBuffer(handle, STREAM_END_SIZE); /* To finish bitstream empty status */
  700. VPU_SWReset(coreIdx, SW_RESET_SAFETY, handle);
  701. VPU_DecUpdateBitstreamBuffer(handle, STREAM_END_CLEAR_FLAG); /* To finish bitstream empty status */
  702. return FALSE;
  703. }
  704. else if (status == DEC_INT_STATUS_EMPTY) {
  705. return TRUE;
  706. }
  707. else if (status == DEC_INT_STATUS_NONE) {
  708. return TRUE;
  709. }
  710. else {
  711. VLOG(INFO, "%s:%d Unknown interrupt status: %d\n", __FUNCTION__, __LINE__, status);
  712. return FALSE;
  713. }
  714. }
  715. ret = VPU_DecCompleteSeqInit(handle, initialInfo);
  716. if (pDecInfo->productCode == WAVE521C_DUAL_CODE) {
  717. if ( initialInfo->lumaBitdepth == 8 && initialInfo->chromaBitdepth == 8) {
  718. ctx->testDecConfig.mapType = COMPRESSED_FRAME_MAP_DUAL_CORE_8BIT;
  719. }
  720. else {
  721. ctx->testDecConfig.mapType = COMPRESSED_FRAME_MAP_DUAL_CORE_10BIT;
  722. }
  723. }
  724. strcpy(lsnpCompleteSeq.refYuvPath, ctx->testDecConfig.refYuvPath);
  725. lsnpCompleteSeq.ret = ret;
  726. lsnpCompleteSeq.initialInfo = initialInfo;
  727. lsnpCompleteSeq.wtlFormat = ctx->wtlFormat;
  728. lsnpCompleteSeq.cbcrInterleave = ctx->decOpenParam.cbcrInterleave;
  729. lsnpCompleteSeq.bitstreamFormat = ctx->decOpenParam.bitstreamFormat;
  730. ComponentNotifyListeners(com, COMPONENT_EVENT_DEC_COMPLETE_SEQ, (void*)&lsnpCompleteSeq);
  731. if (ret != RETCODE_SUCCESS) {
  732. VLOG(ERR, "%s:%d FAILED TO DEC_PIC_HDR: ret(%d), SEQERR(%08x)\n", __FUNCTION__, __LINE__, ret, initialInfo->seqInitErrReason);
  733. ChekcAndPrintDebugInfo(ctx->handle, FALSE, ret);
  734. return FALSE;
  735. }
  736. if (ctx->decOpenParam.wtlEnable == TRUE) {
  737. VPU_DecGiveCommand(ctx->handle, DEC_SET_WTL_FRAME_FORMAT, &ctx->wtlFormat);
  738. }
  739. /* Set up the secondary AXI is depending on H/W configuration.
  740. * Note that turn off all the secondary AXI configuration
  741. * if target ASIC has no memory only for IP, LF and BIT.
  742. */
  743. secAxiUse.u.wave.useIpEnable = (ctx->testDecConfig.secondaryAXI&0x01) ? TRUE : FALSE;
  744. secAxiUse.u.wave.useLfRowEnable = (ctx->testDecConfig.secondaryAXI&0x02) ? TRUE : FALSE;
  745. secAxiUse.u.wave.useBitEnable = (ctx->testDecConfig.secondaryAXI&0x04) ? TRUE : FALSE;
  746. secAxiUse.u.wave.useSclEnable = (ctx->testDecConfig.secondaryAXI&0x08) ? TRUE : FALSE;
  747. VPU_DecGiveCommand(ctx->handle, SET_SEC_AXI, &secAxiUse);
  748. // Set up scale
  749. if (ctx->testDecConfig.scaleDownWidth > 0 || ctx->testDecConfig.scaleDownHeight > 0) {
  750. ScalerInfo sclInfo = {0};
  751. sclInfo.scaleWidth = ctx->testDecConfig.scaleDownWidth;
  752. sclInfo.scaleHeight = ctx->testDecConfig.scaleDownHeight;
  753. VLOG(INFO, "[SCALE INFO] %dx%d\n", sclInfo.scaleWidth, sclInfo.scaleHeight);
  754. sclInfo.enScaler = TRUE;
  755. if (VPU_DecGiveCommand(ctx->handle, DEC_SET_SCALER_INFO, (void*)&sclInfo) != RETCODE_SUCCESS) {
  756. VLOG(ERR, "Failed to VPU_DecGiveCommand(DEC_SET_SCALER_INFO)\n");
  757. return FALSE;
  758. }
  759. }
  760. ctx->stateDoing = FALSE;
  761. *done = TRUE;
  762. return TRUE;
  763. }
  764. static BOOL ExecuteDecoder(ComponentImpl* com, PortContainer* in , PortContainer* out)
  765. {
  766. DecoderContext* ctx = (DecoderContext*)com->context;
  767. BOOL ret = FALSE;;
  768. BitStreamMode bsMode = ctx->decOpenParam.bitstreamMode;
  769. BOOL done = FALSE;
  770. if (in) in->reuse = TRUE;
  771. if (out) out->reuse = TRUE;
  772. if (ctx->state == DEC_STATE_INIT_SEQ || ctx->state == DEC_STATE_DECODING) {
  773. if (UpdateBitstream(ctx, (PortContainerES*)in) == FALSE) {
  774. return FALSE;
  775. }
  776. if (in) {
  777. // In ring-buffer mode, it has to return back a container immediately.
  778. if (bsMode == BS_MODE_PIC_END) {
  779. if (ctx->state == DEC_STATE_INIT_SEQ) {
  780. in->reuse = TRUE;
  781. }
  782. in->consumed = FALSE;
  783. }
  784. else {
  785. in->consumed = (in->reuse == FALSE);
  786. }
  787. }
  788. }
  789. if (ctx->doReset == TRUE) {
  790. if (in) in->reuse = FALSE;
  791. return DoReset(com);
  792. }
  793. switch (ctx->state) {
  794. case DEC_STATE_OPEN_DECODER:
  795. ret = OpenDecoder(com);
  796. if (ctx->stateDoing == FALSE) ctx->state = DEC_STATE_INIT_SEQ;
  797. break;
  798. case DEC_STATE_INIT_SEQ:
  799. ret = DecodeHeader(com, (PortContainerES*)in, &done);
  800. if (TRUE == done) ctx->state = DEC_STATE_REGISTER_FB;
  801. break;
  802. case DEC_STATE_REGISTER_FB:
  803. ret = RegisterFrameBuffers(com);
  804. if (ctx->stateDoing == FALSE) {
  805. ctx->state = DEC_STATE_DECODING;
  806. DisplayDecodedInformation(ctx->handle, ctx->decOpenParam.bitstreamFormat, 0, NULL, ctx->testDecConfig.performance, 0);
  807. }
  808. break;
  809. case DEC_STATE_DECODING:
  810. ret = Decode(com, (PortContainerES*)in, (PortContainerDisplay*)out);
  811. break;
  812. default:
  813. ret = FALSE;
  814. break;
  815. }
  816. if (ret == FALSE || com->terminate == TRUE) {
  817. ComponentNotifyListeners(com, COMPONENT_EVENT_DEC_DECODED_ALL, (void*)ctx->handle);
  818. if (out) {
  819. out->reuse = FALSE;
  820. out->last = TRUE;
  821. }
  822. }
  823. return ret;
  824. }
  825. static BOOL PrepareDecoder(ComponentImpl* com, BOOL* done)
  826. {
  827. *done = TRUE;
  828. return TRUE;
  829. }
  830. static void ReleaseDecoder(ComponentImpl* com)
  831. {
  832. // Nothing to do
  833. }
  834. static BOOL DestroyDecoder(ComponentImpl* com)
  835. {
  836. DecoderContext* ctx = (DecoderContext*)com->context;
  837. DEC_INT_STATUS intStatus;
  838. BOOL success = TRUE;
  839. Uint32 timeout = 0;
  840. Uint32 i = 0;
  841. if (NULL != ctx->handle) {
  842. LoadBalancerRemoveInstance(ctx->handle->instIndex);
  843. VPU_DecUpdateBitstreamBuffer(ctx->handle, STREAM_END_SET_FLAG);
  844. while (VPU_DecClose(ctx->handle) == RETCODE_VPU_STILL_RUNNING) {
  845. if ((intStatus=HandlingInterruptFlag(com)) == DEC_INT_STATUS_TIMEOUT) {
  846. HandleDecoderError(ctx->handle, ctx->numDecoded, NULL);
  847. VLOG(ERR, "<%s:%d> NO RESPONSE FROM VPU_DecClose()\n", __FUNCTION__, __LINE__);
  848. success = FALSE;
  849. break;
  850. }
  851. else if (intStatus == DEC_INT_STATUS_DONE) {
  852. DecOutputInfo outputInfo;
  853. VLOG(INFO, "VPU_DecClose() : CLEAR REMAIN INTERRUPT\n");
  854. VPU_DecGetOutputInfo(ctx->handle, &outputInfo);
  855. continue;
  856. }
  857. if (timeout > VPU_BUSY_CHECK_TIMEOUT) {
  858. VLOG(ERR, "<%s:%d> Failed to VPU_DecClose\n", __FUNCTION__, __LINE__);
  859. }
  860. for (i=0; i<MAX_REG_FRAME; i++) {
  861. VPU_DecClrDispFlag(ctx->handle, i);
  862. }
  863. timeout++;
  864. }
  865. ComponentNotifyListeners(com, COMPONENT_EVENT_DEC_CLOSE, NULL);
  866. }
  867. if (ctx->vbUserData.size) {
  868. vdi_free_dma_memory(ctx->testDecConfig.coreIdx, &ctx->vbUserData, DEC_ETC, ctx->handle->instIndex);
  869. }
  870. VPU_DeInit(ctx->decOpenParam.coreIdx);
  871. osal_free(ctx);
  872. return success;
  873. }
  874. static Component CreateDecoder(ComponentImpl* com, CNMComponentConfig* componentParam)
  875. {
  876. DecoderContext* ctx;
  877. Uint32 coreIdx = componentParam->testDecConfig.coreIdx;
  878. Uint16* firmware = (Uint16*)componentParam->bitcode;
  879. Uint32 firmwareSize = componentParam->sizeOfBitcode;
  880. RetCode retCode;
  881. retCode = VPU_InitWithBitcode(coreIdx, firmware, firmwareSize);
  882. if (retCode != RETCODE_SUCCESS && retCode != RETCODE_CALLED_BEFORE) {
  883. VLOG(INFO, "%s:%d Failed to VPU_InitiWithBitcode, ret(%08x)\n", __FUNCTION__, __LINE__, retCode);
  884. return FALSE;
  885. }
  886. com->context = (DecoderContext*)osal_malloc(sizeof(DecoderContext));
  887. osal_memset(com->context, 0, sizeof(DecoderContext));
  888. ctx = (DecoderContext*)com->context;
  889. retCode = PrintVpuProductInfo(coreIdx, &ctx->attr);
  890. if (retCode == RETCODE_VPU_RESPONSE_TIMEOUT ) {
  891. CNMErrorSet(CNM_ERROR_HANGUP);
  892. VLOG(INFO, "<%s:%d> Failed to PrintVpuProductInfo()\n", __FUNCTION__, __LINE__);
  893. HandleDecoderError(ctx->handle, ctx->numDecoded, NULL);
  894. return FALSE;
  895. }
  896. ctx->cyclePerTick = 32768;
  897. if (TRUE == ctx->attr.supportNewTimer)
  898. ctx->cyclePerTick = 256;
  899. memcpy(&(ctx->decOpenParam), &(componentParam->decOpenParam), sizeof(DecOpenParam));
  900. ctx->wtlFormat = componentParam->testDecConfig.wtlFormat;
  901. ctx->frameNumToStop = componentParam->testDecConfig.forceOutNum;
  902. ctx->testDecConfig = componentParam->testDecConfig;
  903. ctx->state = DEC_STATE_OPEN_DECODER;
  904. ctx->stateDoing = FALSE;
  905. VLOG(INFO, "PRODUCT ID: %d\n", ctx->attr.productId);
  906. return (Component)com;
  907. }
  908. ComponentImpl waveDecoderComponentImpl = {
  909. "wave_decoder",
  910. NULL,
  911. {0,},
  912. {0,},
  913. sizeof(PortContainerDisplay),
  914. 5,
  915. CreateDecoder,
  916. GetParameterDecoder,
  917. SetParameterDecoder,
  918. PrepareDecoder,
  919. ExecuteDecoder,
  920. ReleaseDecoder,
  921. DestroyDecoder
  922. };