component_dec_decoder.c 40 KB

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