component_dec_decoder.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070
  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. break;
  508. case GET_PARAM_DEC_BITSTREAM_BUF_POS:
  509. if (ctx->state < DEC_STATE_INIT_SEQ) return CNM_COMPONENT_PARAM_NOT_READY;
  510. VPU_DecGetBitstreamBuffer(ctx->handle, &rdPtr, &wrPtr, &room);
  511. bsPos = (ParamDecBitstreamBufPos*)data;
  512. bsPos->rdPtr = rdPtr;
  513. bsPos->wrPtr = wrPtr;
  514. bsPos->avail = room;
  515. break;
  516. case GET_PARAM_DEC_CODEC_INFO:
  517. if (ctx->state <= DEC_STATE_INIT_SEQ) return CNM_COMPONENT_PARAM_NOT_READY;
  518. VPU_DecGiveCommand(ctx->handle, DEC_GET_SEQ_INFO, data);
  519. break;
  520. case GET_PARAM_VPU_STATUS:
  521. if (ctx->state != DEC_STATE_DECODING) return CNM_COMPONENT_PARAM_NOT_READY;
  522. VPU_DecGiveCommand(ctx->handle, DEC_GET_QUEUE_STATUS, &cqInfo);
  523. status = (ParamVpuStatus*)data;
  524. status->cq = cqInfo;
  525. break;
  526. default:
  527. result = FALSE;
  528. break;
  529. }
  530. return (result == TRUE) ? CNM_COMPONENT_PARAM_SUCCESS : CNM_COMPONENT_PARAM_FAILURE;
  531. }
  532. static CNMComponentParamRet SetParameterDecoder(ComponentImpl* from, ComponentImpl* com, SetParameterCMD commandType, void* data)
  533. {
  534. BOOL result = TRUE;
  535. DecoderContext* ctx = (DecoderContext*)com->context;
  536. Int32 skipCmd;
  537. ParamDecTargetTid* tid = NULL;
  538. switch(commandType) {
  539. case SET_PARAM_COM_PAUSE:
  540. com->pause = *(BOOL*)data;
  541. break;
  542. case SET_PARAM_DEC_SKIP_COMMAND:
  543. skipCmd = *(Int32*)data;
  544. ctx->decParam.skipframeMode = skipCmd;
  545. if (skipCmd == WAVE_SKIPMODE_NON_IRAP) {
  546. Uint32 userDataMask = (1<<H265_USERDATA_FLAG_RECOVERY_POINT);
  547. ctx->decParam.craAsBlaFlag = TRUE;
  548. /* For finding recovery point */
  549. VPU_DecGiveCommand(ctx->handle, ENABLE_REP_USERDATA, &userDataMask);
  550. }
  551. else {
  552. ctx->decParam.craAsBlaFlag = FALSE;
  553. }
  554. if (ctx->numDecoded > 0) {
  555. ctx->doFlush = (BOOL)(ctx->decParam.skipframeMode == WAVE_SKIPMODE_NON_IRAP);
  556. }
  557. break;
  558. case SET_PARAM_DEC_RESET:
  559. ctx->doReset = TRUE;
  560. break;
  561. case SET_PARAM_DEC_TARGET_TID:
  562. tid = (ParamDecTargetTid*)data;
  563. /*! NOTE: DO NOT CHANGE THE ORDER OF COMMANDS BELOW. */
  564. VPU_DecGiveCommand(ctx->handle, DEC_SET_TEMPORAL_ID_MODE, (void*)&tid->tidMode);
  565. VPU_DecGiveCommand(ctx->handle, DEC_SET_TARGET_TEMPORAL_ID, (void*)&tid->targetTid);
  566. break;
  567. case SET_PARAM_DEC_FLUSH:
  568. ClearDpb(com, TRUE);
  569. break;
  570. default:
  571. result = FALSE;
  572. break;
  573. }
  574. return (result == TRUE) ? CNM_COMPONENT_PARAM_SUCCESS : CNM_COMPONENT_PARAM_FAILURE;
  575. }
  576. static BOOL UpdateBitstream(DecoderContext* ctx, PortContainerES* in)
  577. {
  578. RetCode ret = RETCODE_SUCCESS;
  579. BitStreamMode bsMode = ctx->decOpenParam.bitstreamMode;
  580. BOOL update = TRUE;
  581. Uint32 updateSize;
  582. if (in == NULL) return TRUE;
  583. if (bsMode == BS_MODE_PIC_END) {
  584. VPU_DecSetRdPtr(ctx->handle, in->buf.phys_addr, TRUE);
  585. }
  586. else {
  587. if (in->size > 0) {
  588. PhysicalAddress rdPtr, wrPtr;
  589. Uint32 room;
  590. VPU_DecGetBitstreamBuffer(ctx->handle, &rdPtr, &wrPtr, &room);
  591. if ((Int32)room < in->size) {
  592. in->reuse = TRUE;
  593. return TRUE;
  594. }
  595. }
  596. }
  597. if (in->last == TRUE) {
  598. updateSize = (in->size == 0) ? STREAM_END_SET_FLAG : in->size;
  599. }
  600. else {
  601. updateSize = in->size;
  602. update = (in->size > 0 && in->last == FALSE);
  603. }
  604. if (update == TRUE) {
  605. if ((ret=VPU_DecUpdateBitstreamBuffer(ctx->handle, updateSize)) != RETCODE_SUCCESS) {
  606. VLOG(INFO, "<%s:%d> Failed to VPU_DecUpdateBitstreamBuffer() ret(%d)\n", __FUNCTION__, __LINE__, ret);
  607. ChekcAndPrintDebugInfo(ctx->handle, FALSE, ret);
  608. return FALSE;
  609. }
  610. if (in->last == TRUE) {
  611. VPU_DecUpdateBitstreamBuffer(ctx->handle, STREAM_END_SET_FLAG);
  612. }
  613. }
  614. in->reuse = FALSE;
  615. return TRUE;
  616. }
  617. static BOOL OpenDecoder(ComponentImpl* com)
  618. {
  619. DecoderContext* ctx = (DecoderContext*)com->context;
  620. ParamDecBitstreamBuffer bsBuf;
  621. CNMComponentParamRet ret;
  622. CNMComListenerDecOpen lspn = {0};
  623. BOOL success = FALSE;
  624. BitStreamMode bsMode = ctx->decOpenParam.bitstreamMode;
  625. vpu_buffer_t vbUserData;
  626. RetCode retCode;
  627. ctx->stateDoing = TRUE;
  628. ret = ComponentGetParameter(com, com->srcPort.connectedComponent, GET_PARAM_FEEDER_BITSTREAM_BUF, &bsBuf);
  629. if (ComponentParamReturnTest(ret, &success) == FALSE) {
  630. return success;
  631. }
  632. ctx->decOpenParam.bitstreamBuffer = (bsMode == BS_MODE_PIC_END) ? 0 : bsBuf.bs->phys_addr;
  633. ctx->decOpenParam.bitstreamBufferSize = (bsMode == BS_MODE_PIC_END) ? 0 : bsBuf.bs->size;
  634. retCode = VPU_DecOpen(&ctx->handle, &ctx->decOpenParam);
  635. lspn.handle = ctx->handle;
  636. lspn.ret = retCode;
  637. ComponentNotifyListeners(com, COMPONENT_EVENT_DEC_OPEN, (void*)&lspn);
  638. if (retCode != RETCODE_SUCCESS) {
  639. VLOG(ERR, "<%s:%d> Failed to VPU_DecOpen(ret:%d)\n", __FUNCTION__, __LINE__, retCode);
  640. if ( retCode == RETCODE_VPU_RESPONSE_TIMEOUT)
  641. CNMErrorSet(CNM_ERROR_HANGUP);
  642. return FALSE;
  643. }
  644. LoadBalancerAddInstance(ctx->handle->instIndex);
  645. // VPU_DecGiveCommand(ctx->handle, ENABLE_LOGGING, 0);
  646. if (ctx->vbUserData.size == 0) {
  647. vbUserData.size = (1320*1024); /* 40KB * (queue_depth + report_queue_depth+1) = 40KB * (16 + 16 +1) */
  648. vdi_allocate_dma_memory(ctx->testDecConfig.coreIdx, &vbUserData, DEC_ETC, ctx->handle->instIndex);
  649. }
  650. VPU_DecGiveCommand(ctx->handle, SET_ADDR_REP_USERDATA, (void*)&vbUserData.phys_addr);
  651. VPU_DecGiveCommand(ctx->handle, SET_SIZE_REP_USERDATA, (void*)&vbUserData.size);
  652. VPU_DecGiveCommand(ctx->handle, ENABLE_REP_USERDATA, (void*)&ctx->testDecConfig.enableUserData);
  653. VPU_DecGiveCommand(ctx->handle, SET_CYCLE_PER_TICK, (void*)&ctx->cyclePerTick);
  654. if (ctx->testDecConfig.thumbnailMode == TRUE) {
  655. VPU_DecGiveCommand(ctx->handle, ENABLE_DEC_THUMBNAIL_MODE, NULL);
  656. }
  657. ctx->vbUserData = vbUserData;
  658. ctx->stateDoing = FALSE;
  659. return TRUE;
  660. }
  661. static BOOL DecodeHeader(ComponentImpl* com, PortContainerES* in, BOOL* done)
  662. {
  663. DecoderContext* ctx = (DecoderContext*)com->context;
  664. DecHandle handle = ctx->handle;
  665. Uint32 coreIdx = ctx->testDecConfig.coreIdx;
  666. RetCode ret = RETCODE_SUCCESS;
  667. DEC_INT_STATUS status;
  668. DecInitialInfo* initialInfo = &ctx->initialInfo;
  669. SecAxiUse secAxiUse;
  670. CNMComListenerDecCompleteSeq lsnpCompleteSeq;
  671. DecInfo* pDecInfo = VPU_HANDLE_TO_DECINFO(ctx->handle);
  672. *done = FALSE;
  673. if (BS_MODE_PIC_END == ctx->decOpenParam.bitstreamMode && NULL == in) {
  674. return TRUE;
  675. }
  676. if (ctx->stateDoing == FALSE) {
  677. /* previous state done */
  678. ret = VPU_DecIssueSeqInit(handle);
  679. if (RETCODE_QUEUEING_FAILURE == ret) {
  680. return TRUE; // Try again
  681. }
  682. ComponentNotifyListeners(com, COMPONENT_EVENT_DEC_ISSUE_SEQ, NULL);
  683. if (ret != RETCODE_SUCCESS) {
  684. ChekcAndPrintDebugInfo(ctx->handle, FALSE, ret);
  685. VLOG(ERR, "%s:%d Failed to VPU_DecIssueSeqInit() ret(%d)\n", __FUNCTION__, __LINE__, ret);
  686. return FALSE;
  687. }
  688. }
  689. ctx->stateDoing = TRUE;
  690. while (com->terminate == FALSE) {
  691. if ((status=HandlingInterruptFlag(com)) == DEC_INT_STATUS_DONE) {
  692. break;
  693. }
  694. else if (status == DEC_INT_STATUS_TIMEOUT) {
  695. HandleDecoderError(ctx->handle, 0, NULL);
  696. VPU_DecUpdateBitstreamBuffer(handle, STREAM_END_SIZE); /* To finish bitstream empty status */
  697. VPU_SWReset(coreIdx, SW_RESET_SAFETY, handle);
  698. VPU_DecUpdateBitstreamBuffer(handle, STREAM_END_CLEAR_FLAG); /* To finish bitstream empty status */
  699. return FALSE;
  700. }
  701. else if (status == DEC_INT_STATUS_EMPTY) {
  702. return TRUE;
  703. }
  704. else if (status == DEC_INT_STATUS_NONE) {
  705. return TRUE;
  706. }
  707. else {
  708. VLOG(INFO, "%s:%d Unknown interrupt status: %d\n", __FUNCTION__, __LINE__, status);
  709. return FALSE;
  710. }
  711. }
  712. ret = VPU_DecCompleteSeqInit(handle, initialInfo);
  713. if (pDecInfo->productCode == WAVE521C_DUAL_CODE) {
  714. if ( initialInfo->lumaBitdepth == 8 && initialInfo->chromaBitdepth == 8) {
  715. ctx->testDecConfig.mapType = COMPRESSED_FRAME_MAP_DUAL_CORE_8BIT;
  716. }
  717. else {
  718. ctx->testDecConfig.mapType = COMPRESSED_FRAME_MAP_DUAL_CORE_10BIT;
  719. }
  720. }
  721. strcpy(lsnpCompleteSeq.refYuvPath, ctx->testDecConfig.refYuvPath);
  722. lsnpCompleteSeq.ret = ret;
  723. lsnpCompleteSeq.initialInfo = initialInfo;
  724. lsnpCompleteSeq.wtlFormat = ctx->wtlFormat;
  725. lsnpCompleteSeq.cbcrInterleave = ctx->decOpenParam.cbcrInterleave;
  726. lsnpCompleteSeq.bitstreamFormat = ctx->decOpenParam.bitstreamFormat;
  727. ComponentNotifyListeners(com, COMPONENT_EVENT_DEC_COMPLETE_SEQ, (void*)&lsnpCompleteSeq);
  728. if (ret != RETCODE_SUCCESS) {
  729. VLOG(ERR, "%s:%d FAILED TO DEC_PIC_HDR: ret(%d), SEQERR(%08x)\n", __FUNCTION__, __LINE__, ret, initialInfo->seqInitErrReason);
  730. ChekcAndPrintDebugInfo(ctx->handle, FALSE, ret);
  731. return FALSE;
  732. }
  733. if (ctx->decOpenParam.wtlEnable == TRUE) {
  734. VPU_DecGiveCommand(ctx->handle, DEC_SET_WTL_FRAME_FORMAT, &ctx->wtlFormat);
  735. }
  736. /* Set up the secondary AXI is depending on H/W configuration.
  737. * Note that turn off all the secondary AXI configuration
  738. * if target ASIC has no memory only for IP, LF and BIT.
  739. */
  740. secAxiUse.u.wave.useIpEnable = (ctx->testDecConfig.secondaryAXI&0x01) ? TRUE : FALSE;
  741. secAxiUse.u.wave.useLfRowEnable = (ctx->testDecConfig.secondaryAXI&0x02) ? TRUE : FALSE;
  742. secAxiUse.u.wave.useBitEnable = (ctx->testDecConfig.secondaryAXI&0x04) ? TRUE : FALSE;
  743. secAxiUse.u.wave.useSclEnable = (ctx->testDecConfig.secondaryAXI&0x08) ? TRUE : FALSE;
  744. VPU_DecGiveCommand(ctx->handle, SET_SEC_AXI, &secAxiUse);
  745. // Set up scale
  746. if (ctx->testDecConfig.scaleDownWidth > 0 || ctx->testDecConfig.scaleDownHeight > 0) {
  747. ScalerInfo sclInfo = {0};
  748. sclInfo.scaleWidth = ctx->testDecConfig.scaleDownWidth;
  749. sclInfo.scaleHeight = ctx->testDecConfig.scaleDownHeight;
  750. VLOG(INFO, "[SCALE INFO] %dx%d\n", sclInfo.scaleWidth, sclInfo.scaleHeight);
  751. sclInfo.enScaler = TRUE;
  752. if (VPU_DecGiveCommand(ctx->handle, DEC_SET_SCALER_INFO, (void*)&sclInfo) != RETCODE_SUCCESS) {
  753. VLOG(ERR, "Failed to VPU_DecGiveCommand(DEC_SET_SCALER_INFO)\n");
  754. return FALSE;
  755. }
  756. }
  757. ctx->stateDoing = FALSE;
  758. *done = TRUE;
  759. return TRUE;
  760. }
  761. static BOOL ExecuteDecoder(ComponentImpl* com, PortContainer* in , PortContainer* out)
  762. {
  763. DecoderContext* ctx = (DecoderContext*)com->context;
  764. BOOL ret = FALSE;;
  765. BitStreamMode bsMode = ctx->decOpenParam.bitstreamMode;
  766. BOOL done = FALSE;
  767. if (in) in->reuse = TRUE;
  768. if (out) out->reuse = TRUE;
  769. if (ctx->state == DEC_STATE_INIT_SEQ || ctx->state == DEC_STATE_DECODING) {
  770. if (UpdateBitstream(ctx, (PortContainerES*)in) == FALSE) {
  771. return FALSE;
  772. }
  773. if (in) {
  774. // In ring-buffer mode, it has to return back a container immediately.
  775. if (bsMode == BS_MODE_PIC_END) {
  776. if (ctx->state == DEC_STATE_INIT_SEQ) {
  777. in->reuse = TRUE;
  778. }
  779. in->consumed = FALSE;
  780. }
  781. else {
  782. in->consumed = (in->reuse == FALSE);
  783. }
  784. }
  785. }
  786. if (ctx->doReset == TRUE) {
  787. if (in) in->reuse = FALSE;
  788. return DoReset(com);
  789. }
  790. switch (ctx->state) {
  791. case DEC_STATE_OPEN_DECODER:
  792. ret = OpenDecoder(com);
  793. if (ctx->stateDoing == FALSE) ctx->state = DEC_STATE_INIT_SEQ;
  794. break;
  795. case DEC_STATE_INIT_SEQ:
  796. ret = DecodeHeader(com, (PortContainerES*)in, &done);
  797. if (TRUE == done) ctx->state = DEC_STATE_REGISTER_FB;
  798. break;
  799. case DEC_STATE_REGISTER_FB:
  800. ret = RegisterFrameBuffers(com);
  801. if (ctx->stateDoing == FALSE) {
  802. ctx->state = DEC_STATE_DECODING;
  803. DisplayDecodedInformation(ctx->handle, ctx->decOpenParam.bitstreamFormat, 0, NULL, ctx->testDecConfig.performance, 0);
  804. }
  805. break;
  806. case DEC_STATE_DECODING:
  807. ret = Decode(com, (PortContainerES*)in, (PortContainerDisplay*)out);
  808. break;
  809. default:
  810. ret = FALSE;
  811. break;
  812. }
  813. if (ret == FALSE || com->terminate == TRUE) {
  814. ComponentNotifyListeners(com, COMPONENT_EVENT_DEC_DECODED_ALL, (void*)ctx->handle);
  815. if (out) {
  816. out->reuse = FALSE;
  817. out->last = TRUE;
  818. }
  819. }
  820. return ret;
  821. }
  822. static BOOL PrepareDecoder(ComponentImpl* com, BOOL* done)
  823. {
  824. *done = TRUE;
  825. return TRUE;
  826. }
  827. static void ReleaseDecoder(ComponentImpl* com)
  828. {
  829. // Nothing to do
  830. }
  831. static BOOL DestroyDecoder(ComponentImpl* com)
  832. {
  833. DecoderContext* ctx = (DecoderContext*)com->context;
  834. DEC_INT_STATUS intStatus;
  835. BOOL success = TRUE;
  836. Uint32 timeout = 0;
  837. Uint32 i = 0;
  838. if (NULL != ctx->handle) {
  839. LoadBalancerRemoveInstance(ctx->handle->instIndex);
  840. VPU_DecUpdateBitstreamBuffer(ctx->handle, STREAM_END_SET_FLAG);
  841. while (VPU_DecClose(ctx->handle) == RETCODE_VPU_STILL_RUNNING) {
  842. if ((intStatus=HandlingInterruptFlag(com)) == DEC_INT_STATUS_TIMEOUT) {
  843. HandleDecoderError(ctx->handle, ctx->numDecoded, NULL);
  844. VLOG(ERR, "<%s:%d> NO RESPONSE FROM VPU_DecClose()\n", __FUNCTION__, __LINE__);
  845. success = FALSE;
  846. break;
  847. }
  848. else if (intStatus == DEC_INT_STATUS_DONE) {
  849. DecOutputInfo outputInfo;
  850. VLOG(INFO, "VPU_DecClose() : CLEAR REMAIN INTERRUPT\n");
  851. VPU_DecGetOutputInfo(ctx->handle, &outputInfo);
  852. continue;
  853. }
  854. if (timeout > VPU_BUSY_CHECK_TIMEOUT) {
  855. VLOG(ERR, "<%s:%d> Failed to VPU_DecClose\n", __FUNCTION__, __LINE__);
  856. }
  857. for (i=0; i<MAX_REG_FRAME; i++) {
  858. VPU_DecClrDispFlag(ctx->handle, i);
  859. }
  860. timeout++;
  861. }
  862. ComponentNotifyListeners(com, COMPONENT_EVENT_DEC_CLOSE, NULL);
  863. }
  864. if (ctx->vbUserData.size) {
  865. vdi_free_dma_memory(ctx->testDecConfig.coreIdx, &ctx->vbUserData, DEC_ETC, ctx->handle->instIndex);
  866. }
  867. VPU_DeInit(ctx->decOpenParam.coreIdx);
  868. osal_free(ctx);
  869. return success;
  870. }
  871. static Component CreateDecoder(ComponentImpl* com, CNMComponentConfig* componentParam)
  872. {
  873. DecoderContext* ctx;
  874. Uint32 coreIdx = componentParam->testDecConfig.coreIdx;
  875. Uint16* firmware = (Uint16*)componentParam->bitcode;
  876. Uint32 firmwareSize = componentParam->sizeOfBitcode;
  877. RetCode retCode;
  878. retCode = VPU_InitWithBitcode(coreIdx, firmware, firmwareSize);
  879. if (retCode != RETCODE_SUCCESS && retCode != RETCODE_CALLED_BEFORE) {
  880. VLOG(INFO, "%s:%d Failed to VPU_InitiWithBitcode, ret(%08x)\n", __FUNCTION__, __LINE__, retCode);
  881. return FALSE;
  882. }
  883. com->context = (DecoderContext*)osal_malloc(sizeof(DecoderContext));
  884. osal_memset(com->context, 0, sizeof(DecoderContext));
  885. ctx = (DecoderContext*)com->context;
  886. retCode = PrintVpuProductInfo(coreIdx, &ctx->attr);
  887. if (retCode == RETCODE_VPU_RESPONSE_TIMEOUT ) {
  888. CNMErrorSet(CNM_ERROR_HANGUP);
  889. VLOG(INFO, "<%s:%d> Failed to PrintVpuProductInfo()\n", __FUNCTION__, __LINE__);
  890. HandleDecoderError(ctx->handle, ctx->numDecoded, NULL);
  891. return FALSE;
  892. }
  893. ctx->cyclePerTick = 32768;
  894. if (TRUE == ctx->attr.supportNewTimer)
  895. ctx->cyclePerTick = 256;
  896. memcpy(&(ctx->decOpenParam), &(componentParam->decOpenParam), sizeof(DecOpenParam));
  897. ctx->wtlFormat = componentParam->testDecConfig.wtlFormat;
  898. ctx->frameNumToStop = componentParam->testDecConfig.forceOutNum;
  899. ctx->testDecConfig = componentParam->testDecConfig;
  900. ctx->state = DEC_STATE_OPEN_DECODER;
  901. ctx->stateDoing = FALSE;
  902. VLOG(INFO, "PRODUCT ID: %d\n", ctx->attr.productId);
  903. return (Component)com;
  904. }
  905. ComponentImpl waveDecoderComponentImpl = {
  906. "wave_decoder",
  907. NULL,
  908. {0,},
  909. {0,},
  910. sizeof(PortContainerDisplay),
  911. 5,
  912. CreateDecoder,
  913. GetParameterDecoder,
  914. SetParameterDecoder,
  915. PrepareDecoder,
  916. ExecuteDecoder,
  917. ReleaseDecoder,
  918. DestroyDecoder
  919. };