component_dec_decoder.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098
  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. #ifdef USE_FEEDING_METHOD_BUFFER
  264. VLOG(TRACE, "\n INSNTANCE #%d INTERRUPT TIMEOUT.\n", handle->instIndex);
  265. ctx->startTimeout = 0ULL;
  266. #else
  267. VLOG(ERR, "\n INSNTANCE #%d INTERRUPT TIMEOUT.\n", handle->instIndex);
  268. #endif
  269. status = DEC_INT_STATUS_TIMEOUT;
  270. break;
  271. }
  272. interruptFlag = 0;
  273. }
  274. if (interruptFlag < 0) {
  275. VLOG(ERR, "<%s:%d> interruptFlag is negative value! %08x\n", __FUNCTION__, __LINE__, interruptFlag);
  276. }
  277. if (interruptFlag > 0) {
  278. VPU_ClearInterruptEx(handle, interruptFlag);
  279. ctx->startTimeout = 0ULL;
  280. status = DEC_INT_STATUS_DONE;
  281. if (interruptFlag & (1<<INT_WAVE5_INIT_SEQ)) {
  282. break;
  283. }
  284. if (interruptFlag & (1<<INT_WAVE5_DEC_PIC)) {
  285. break;
  286. }
  287. if (interruptFlag & (1<<INT_WAVE5_BSBUF_EMPTY)) {
  288. status = DEC_INT_STATUS_EMPTY;
  289. break;
  290. }
  291. }
  292. } while (FALSE);
  293. if (interruptFlag != 0) {
  294. lsn.handle = handle;
  295. lsn.flag = interruptFlag;
  296. lsn.decIndex = ctx->numDecoded;
  297. ComponentNotifyListeners(com, COMPONENT_EVENT_DEC_INTERRUPT, (void*)&lsn);
  298. }
  299. return status;
  300. }
  301. static BOOL DoReset(ComponentImpl* com)
  302. {
  303. DecoderContext* ctx = (DecoderContext*)com->context;
  304. DecHandle handle = ctx->handle;
  305. BOOL pause = TRUE;
  306. VLOG(INFO, "========== %s ==========\n", __FUNCTION__);
  307. ComponentSetParameter(com, com->srcPort.connectedComponent, SET_PARAM_COM_PAUSE, (void*)&pause);
  308. ComponentSetParameter(com, com->srcPort.connectedComponent, SET_PARAM_FEEDER_RESET, (void*)&pause);
  309. ClearDpb(com, FALSE);
  310. VLOG(INFO, "> Reset VPU\n");
  311. if (VPU_SWReset(handle->coreIdx, SW_RESET_SAFETY, handle) != RETCODE_SUCCESS) {
  312. VLOG(ERR, "<%s:%d> Failed to VPU_SWReset()\n", __FUNCTION__, __LINE__);
  313. return FALSE;
  314. }
  315. ClearCpb(com);
  316. VLOG(INFO, "========== %s ==========\n", __FUNCTION__);
  317. ComponentNotifyListeners(com, COMPONENT_EVENT_DEC_RESET_DONE, NULL);
  318. VLOG(INFO, "> FLUSH INPUT PORT\n");
  319. ComponentPortFlush(com);
  320. pause = FALSE;
  321. ComponentSetParameter(com, com->srcPort.connectedComponent, SET_PARAM_COM_PAUSE, (void*)&pause);
  322. ctx->doReset = FALSE;
  323. ctx->startTimeout = 0ULL;
  324. ctx->autoErrorRecovery.enable = TRUE;
  325. ctx->autoErrorRecovery.skipCmd = ctx->decParam.skipframeMode;
  326. ctx->decParam.skipframeMode = WAVE_SKIPMODE_NON_IRAP;
  327. return TRUE;
  328. }
  329. static BOOL Decode(ComponentImpl* com, PortContainerES* in, PortContainerDisplay* out)
  330. {
  331. DecoderContext* ctx = (DecoderContext*)com->context;
  332. DecOutputInfo decOutputInfo;
  333. DEC_INT_STATUS intStatus;
  334. CNMComListenerStartDecOneFrame lsnpPicStart;
  335. BitStreamMode bsMode = ctx->decOpenParam.bitstreamMode;
  336. RetCode result;
  337. CNMComListenerDecDone lsnpPicDone;
  338. CNMComListenerDecReadyOneFrame lsnpReadyOneFrame = {0,};
  339. BOOL doDecode;
  340. QueueStatusInfo qStatus;
  341. lsnpReadyOneFrame.handle = ctx->handle;
  342. ComponentNotifyListeners(com, COMPONENT_EVENT_DEC_READY_ONE_FRAME, (void*)&lsnpReadyOneFrame);
  343. ctx->stateDoing = TRUE;
  344. if (PrepareSkip(com) == FALSE) {
  345. return FALSE;
  346. }
  347. /* decode a frame except when the bitstream mode is PIC_END and no data */
  348. doDecode = !(bsMode == BS_MODE_PIC_END && in == NULL);
  349. doDecode &= (BOOL)(com->pause == FALSE);
  350. VPU_DecGiveCommand(ctx->handle, DEC_GET_QUEUE_STATUS, &qStatus);
  351. if (COMMAND_QUEUE_DEPTH == qStatus.instanceQueueCount) {
  352. doDecode = FALSE;
  353. }
  354. /* The simple load balancer : To use this function, call InitLoadBalancer() before decoding process. */
  355. if (TRUE == doDecode) {
  356. doDecode = LoadBalancerGetMyTurn(ctx->handle->instIndex);
  357. }
  358. if (TRUE == doDecode) {
  359. result = VPU_DecStartOneFrame(ctx->handle, &ctx->decParam);
  360. lsnpPicStart.result = result;
  361. lsnpPicStart.decParam = ctx->decParam;
  362. ComponentNotifyListeners(com, COMPONENT_EVENT_DEC_START_ONE_FRAME, (void*)&lsnpPicStart);
  363. if (result == RETCODE_SUCCESS) {
  364. /* The simple load balancer : pass its turn */
  365. LoadBalancerSetNextTurn();
  366. }
  367. else if (result == RETCODE_QUEUEING_FAILURE) {
  368. // Just retry
  369. if (in) in->reuse = (bsMode == BS_MODE_PIC_END);
  370. VPU_DecGiveCommand(ctx->handle, DEC_GET_QUEUE_STATUS, (void*)&qStatus);
  371. }
  372. else if (result == RETCODE_VPU_RESPONSE_TIMEOUT) {
  373. VLOG(INFO, "<%s:%d> Failed to VPU_DecStartOneFrame() ret(%d)\n", __FUNCTION__, __LINE__, result);
  374. CNMErrorSet(CNM_ERROR_HANGUP);
  375. HandleDecoderError(ctx->handle, ctx->numDecoded, NULL);
  376. return FALSE;
  377. }
  378. else {
  379. ChekcAndPrintDebugInfo(ctx->handle, FALSE, result);
  380. return FALSE;
  381. }
  382. }
  383. else {
  384. if (in) in->reuse = (bsMode == BS_MODE_PIC_END);
  385. }
  386. intStatus=HandlingInterruptFlag(com);
  387. switch (intStatus) {
  388. case DEC_INT_STATUS_TIMEOUT:
  389. #ifdef USE_FEEDING_METHOD_BUFFER
  390. if (doDecode)
  391. {
  392. DoReset(com);
  393. return FALSE;
  394. }
  395. #else
  396. DoReset(com);
  397. return FALSE;
  398. #endif
  399. case DEC_INT_STATUS_EMPTY:
  400. case DEC_INT_STATUS_NONE:
  401. return TRUE;
  402. default:
  403. break;
  404. }
  405. // Get data from the sink component.
  406. if ((result=VPU_DecGetOutputInfo(ctx->handle, &decOutputInfo)) == RETCODE_SUCCESS) {
  407. DisplayDecodedInformation(ctx->handle, ctx->decOpenParam.bitstreamFormat, ctx->numDecoded, &decOutputInfo, ctx->testDecConfig.performance, ctx->cyclePerTick);
  408. }
  409. lsnpPicDone.handle = ctx->handle;
  410. lsnpPicDone.ret = result;
  411. lsnpPicDone.decParam = &ctx->decParam;
  412. lsnpPicDone.output = &decOutputInfo;
  413. lsnpPicDone.numDecoded = ctx->numDecoded;
  414. lsnpPicDone.vbUser = ctx->vbUserData;
  415. ComponentNotifyListeners(com, COMPONENT_EVENT_DEC_GET_OUTPUT_INFO, (void*)&lsnpPicDone);
  416. if (result == RETCODE_REPORT_NOT_READY) {
  417. return TRUE; // Not decoded yet. Try again
  418. }
  419. else if (result != RETCODE_SUCCESS) {
  420. /* ERROR */
  421. VLOG(ERR, "Failed to decode error\n");
  422. ChekcAndPrintDebugInfo(ctx->handle, FALSE, result);
  423. return FALSE;
  424. }
  425. if ((decOutputInfo.decodingSuccess & 0x01) == 0) {
  426. VLOG(ERR, "VPU_DecGetOutputInfo decode fail framdIdx %d error(0x%08x) reason(0x%08x), reasonExt(0x%08x)\n",
  427. ctx->numDecoded, decOutputInfo.decodingSuccess, decOutputInfo.errorReason, decOutputInfo.errorReasonExt);
  428. if (WAVE5_SYSERR_WATCHDOG_TIMEOUT == decOutputInfo.errorReason || WAVE5_SYSERR_DEC_VLC_BUF_FULL == decOutputInfo.errorReason) {
  429. VLOG(ERR, "WAVE5_SYSERR_WATCHDOG_TIMEOUT\n");
  430. }
  431. else if ( WAVE5_SYSERR_DEC_VLC_BUF_FULL == decOutputInfo.errorReason) {
  432. VLOG(ERR, "VLC_BUFFER FULL\n");
  433. }
  434. else if (decOutputInfo.errorReason == WAVE5_SPECERR_OVER_PICTURE_WIDTH_SIZE || decOutputInfo.errorReason == WAVE5_SPECERR_OVER_PICTURE_HEIGHT_SIZE) {
  435. VLOG(ERR, "Not supported Width or Height(%dx%d)\n", decOutputInfo.decPicWidth, decOutputInfo.decPicHeight);
  436. return FALSE;
  437. }
  438. }
  439. else {
  440. if (TRUE == ctx->autoErrorRecovery.enable) {
  441. ctx->decParam.skipframeMode = ctx->autoErrorRecovery.skipCmd;
  442. ctx->autoErrorRecovery.enable = FALSE;
  443. ctx->autoErrorRecovery.skipCmd = WAVE_SKIPMODE_WAVE_NONE;
  444. }
  445. }
  446. if (CheckAndDoSequenceChange(com, &decOutputInfo) == FALSE) {
  447. return FALSE;
  448. }
  449. if (decOutputInfo.indexFrameDecoded >= 0 || decOutputInfo.indexFrameDecoded == DECODED_IDX_FLAG_SKIP) {
  450. // Return a used data to a source port.
  451. ctx->numDecoded++;
  452. ctx->decodedAddr = decOutputInfo.bytePosFrameStart;
  453. out->reuse = FALSE;
  454. }
  455. if ((decOutputInfo.indexFrameDisplay >= 0) || (decOutputInfo.indexFrameDisplay == DISPLAY_IDX_FLAG_SEQ_END)) {
  456. ctx->numOutput++;
  457. out->last = (BOOL)(decOutputInfo.indexFrameDisplay == DISPLAY_IDX_FLAG_SEQ_END);
  458. out->reuse = FALSE;
  459. }
  460. if (decOutputInfo.indexFrameDisplay == DISPLAY_IDX_FLAG_SEQ_END) {
  461. ctx->stateDoing = FALSE;
  462. com->terminate = TRUE;
  463. }
  464. if (out->reuse == FALSE) {
  465. osal_memcpy((void*)&out->decInfo, (void*)&decOutputInfo, sizeof(DecOutputInfo));
  466. }
  467. if (ctx->frameNumToStop > 0) {
  468. if (ctx->frameNumToStop == ctx->numOutput) {
  469. com->terminate = TRUE;
  470. }
  471. }
  472. return TRUE;
  473. }
  474. static CNMComponentParamRet GetParameterDecoder(ComponentImpl* from, ComponentImpl* com, GetParameterCMD commandType, void* data)
  475. {
  476. DecoderContext* ctx = (DecoderContext*)com->context;
  477. BOOL result = TRUE;
  478. PhysicalAddress rdPtr, wrPtr;
  479. Uint32 room;
  480. ParamDecBitstreamBufPos* bsPos = NULL;
  481. ParamDecNeedFrameBufferNum* fbNum;
  482. ParamVpuStatus* status;
  483. QueueStatusInfo cqInfo;
  484. PortContainerES* container;
  485. vpu_buffer_t vb;
  486. if (ctx->handle == NULL) return CNM_COMPONENT_PARAM_NOT_READY;
  487. if (ctx->doReset == TRUE) return CNM_COMPONENT_PARAM_NOT_READY;
  488. switch(commandType) {
  489. case GET_PARAM_COM_IS_CONTAINER_CONUSUMED:
  490. // This query command is sent from the comonponent core.
  491. // If input data are consumed in sequence, it should return TRUE through PortContainer::consumed.
  492. container = (PortContainerES*)data;
  493. vb = container->buf;
  494. if (vb.phys_addr <= ctx->decodedAddr && ctx->decodedAddr < (vb.phys_addr+vb.size)) {
  495. container->consumed = TRUE;
  496. ctx->decodedAddr = 0;
  497. }
  498. break;
  499. case GET_PARAM_DEC_HANDLE:
  500. *(DecHandle*)data = ctx->handle;
  501. break;
  502. case GET_PARAM_DEC_FRAME_BUF_NUM:
  503. if (ctx->state <= DEC_STATE_INIT_SEQ) return CNM_COMPONENT_PARAM_NOT_READY;
  504. fbNum = (ParamDecNeedFrameBufferNum*)data;
  505. fbNum->nonLinearNum = ctx->initialInfo.minFrameBufferCount + EXTRA_FRAME_BUFFER_NUM; // max_dec_pic_buffering
  506. if (ctx->decOpenParam.wtlEnable == TRUE) {
  507. fbNum->linearNum = (ctx->initialInfo.frameBufDelay+1) + EXTRA_FRAME_BUFFER_NUM; // The frameBufDelay can be zero.
  508. if ((ctx->decOpenParam.bitstreamFormat == STD_VP9) || (ctx->decOpenParam.bitstreamFormat == STD_AVS2) || (ctx->decOpenParam.bitstreamFormat == STD_AV1)) {
  509. fbNum->linearNum = fbNum->nonLinearNum;
  510. }
  511. if (ctx->testDecConfig.performance == TRUE) {
  512. if ((ctx->decOpenParam.bitstreamFormat == STD_VP9) || (ctx->decOpenParam.bitstreamFormat == STD_AVS2)) {
  513. fbNum->linearNum++;
  514. fbNum->nonLinearNum++;
  515. } else if (ctx->decOpenParam.bitstreamFormat == STD_AV1) {
  516. fbNum->linearNum++;
  517. fbNum->nonLinearNum++;
  518. }
  519. else {
  520. fbNum->linearNum += 3;
  521. }
  522. }
  523. }
  524. else {
  525. fbNum->linearNum = 0;
  526. }
  527. #ifdef USE_FEEDING_METHOD_BUFFER
  528. fbNum->linearNum += 6;
  529. if (fbNum->linearNum > MAX_GDI_IDX)
  530. fbNum->linearNum = MAX_GDI_IDX;
  531. #endif
  532. break;
  533. case GET_PARAM_DEC_BITSTREAM_BUF_POS:
  534. if (ctx->state < DEC_STATE_INIT_SEQ) return CNM_COMPONENT_PARAM_NOT_READY;
  535. VPU_DecGetBitstreamBuffer(ctx->handle, &rdPtr, &wrPtr, &room);
  536. bsPos = (ParamDecBitstreamBufPos*)data;
  537. bsPos->rdPtr = rdPtr;
  538. bsPos->wrPtr = wrPtr;
  539. bsPos->avail = room;
  540. break;
  541. case GET_PARAM_DEC_CODEC_INFO:
  542. if (ctx->state <= DEC_STATE_INIT_SEQ) return CNM_COMPONENT_PARAM_NOT_READY;
  543. VPU_DecGiveCommand(ctx->handle, DEC_GET_SEQ_INFO, data);
  544. break;
  545. case GET_PARAM_VPU_STATUS:
  546. if (ctx->state != DEC_STATE_DECODING) return CNM_COMPONENT_PARAM_NOT_READY;
  547. VPU_DecGiveCommand(ctx->handle, DEC_GET_QUEUE_STATUS, &cqInfo);
  548. status = (ParamVpuStatus*)data;
  549. status->cq = cqInfo;
  550. break;
  551. default:
  552. result = FALSE;
  553. break;
  554. }
  555. return (result == TRUE) ? CNM_COMPONENT_PARAM_SUCCESS : CNM_COMPONENT_PARAM_FAILURE;
  556. }
  557. static CNMComponentParamRet SetParameterDecoder(ComponentImpl* from, ComponentImpl* com, SetParameterCMD commandType, void* data)
  558. {
  559. BOOL result = TRUE;
  560. DecoderContext* ctx = (DecoderContext*)com->context;
  561. Int32 skipCmd;
  562. ParamDecTargetTid* tid = NULL;
  563. switch(commandType) {
  564. case SET_PARAM_COM_PAUSE:
  565. com->pause = *(BOOL*)data;
  566. break;
  567. case SET_PARAM_DEC_SKIP_COMMAND:
  568. skipCmd = *(Int32*)data;
  569. ctx->decParam.skipframeMode = skipCmd;
  570. if (skipCmd == WAVE_SKIPMODE_NON_IRAP) {
  571. Uint32 userDataMask = (1<<H265_USERDATA_FLAG_RECOVERY_POINT);
  572. ctx->decParam.craAsBlaFlag = TRUE;
  573. /* For finding recovery point */
  574. VPU_DecGiveCommand(ctx->handle, ENABLE_REP_USERDATA, &userDataMask);
  575. }
  576. else {
  577. ctx->decParam.craAsBlaFlag = FALSE;
  578. }
  579. if (ctx->numDecoded > 0) {
  580. ctx->doFlush = (BOOL)(ctx->decParam.skipframeMode == WAVE_SKIPMODE_NON_IRAP);
  581. }
  582. break;
  583. case SET_PARAM_DEC_RESET:
  584. ctx->doReset = TRUE;
  585. break;
  586. case SET_PARAM_DEC_TARGET_TID:
  587. tid = (ParamDecTargetTid*)data;
  588. /*! NOTE: DO NOT CHANGE THE ORDER OF COMMANDS BELOW. */
  589. VPU_DecGiveCommand(ctx->handle, DEC_SET_TEMPORAL_ID_MODE, (void*)&tid->tidMode);
  590. VPU_DecGiveCommand(ctx->handle, DEC_SET_TARGET_TEMPORAL_ID, (void*)&tid->targetTid);
  591. break;
  592. case SET_PARAM_DEC_FLUSH:
  593. ClearDpb(com, TRUE);
  594. break;
  595. default:
  596. result = FALSE;
  597. break;
  598. }
  599. return (result == TRUE) ? CNM_COMPONENT_PARAM_SUCCESS : CNM_COMPONENT_PARAM_FAILURE;
  600. }
  601. static BOOL UpdateBitstream(DecoderContext* ctx, PortContainerES* in)
  602. {
  603. RetCode ret = RETCODE_SUCCESS;
  604. BitStreamMode bsMode = ctx->decOpenParam.bitstreamMode;
  605. BOOL update = TRUE;
  606. Uint32 updateSize;
  607. if (in == NULL) return TRUE;
  608. if (bsMode == BS_MODE_PIC_END) {
  609. VPU_DecSetRdPtr(ctx->handle, in->buf.phys_addr, TRUE);
  610. }
  611. else {
  612. if (in->size > 0) {
  613. PhysicalAddress rdPtr, wrPtr;
  614. Uint32 room;
  615. VPU_DecGetBitstreamBuffer(ctx->handle, &rdPtr, &wrPtr, &room);
  616. if ((Int32)room < in->size) {
  617. in->reuse = TRUE;
  618. return TRUE;
  619. }
  620. }
  621. }
  622. if (in->last == TRUE) {
  623. updateSize = (in->size == 0) ? STREAM_END_SET_FLAG : in->size;
  624. }
  625. else {
  626. updateSize = in->size;
  627. update = (in->size > 0 && in->last == FALSE);
  628. }
  629. if (update == TRUE) {
  630. if ((ret=VPU_DecUpdateBitstreamBuffer(ctx->handle, updateSize)) != RETCODE_SUCCESS) {
  631. VLOG(INFO, "<%s:%d> Failed to VPU_DecUpdateBitstreamBuffer() ret(%d)\n", __FUNCTION__, __LINE__, ret);
  632. ChekcAndPrintDebugInfo(ctx->handle, FALSE, ret);
  633. return FALSE;
  634. }
  635. if (in->last == TRUE) {
  636. VPU_DecUpdateBitstreamBuffer(ctx->handle, STREAM_END_SET_FLAG);
  637. }
  638. }
  639. in->reuse = FALSE;
  640. return TRUE;
  641. }
  642. static BOOL OpenDecoder(ComponentImpl* com)
  643. {
  644. DecoderContext* ctx = (DecoderContext*)com->context;
  645. ParamDecBitstreamBuffer bsBuf;
  646. CNMComponentParamRet ret;
  647. CNMComListenerDecOpen lspn = {0};
  648. BOOL success = FALSE;
  649. BitStreamMode bsMode = ctx->decOpenParam.bitstreamMode;
  650. vpu_buffer_t vbUserData;
  651. RetCode retCode;
  652. ctx->stateDoing = TRUE;
  653. ret = ComponentGetParameter(com, com->srcPort.connectedComponent, GET_PARAM_FEEDER_BITSTREAM_BUF, &bsBuf);
  654. if (ComponentParamReturnTest(ret, &success) == FALSE) {
  655. return success;
  656. }
  657. ctx->decOpenParam.bitstreamBuffer = (bsMode == BS_MODE_PIC_END) ? 0 : bsBuf.bs->phys_addr;
  658. ctx->decOpenParam.bitstreamBufferSize = (bsMode == BS_MODE_PIC_END) ? 0 : bsBuf.bs->size;
  659. retCode = VPU_DecOpen(&ctx->handle, &ctx->decOpenParam);
  660. lspn.handle = ctx->handle;
  661. lspn.ret = retCode;
  662. ComponentNotifyListeners(com, COMPONENT_EVENT_DEC_OPEN, (void*)&lspn);
  663. if (retCode != RETCODE_SUCCESS) {
  664. VLOG(ERR, "<%s:%d> Failed to VPU_DecOpen(ret:%d)\n", __FUNCTION__, __LINE__, retCode);
  665. #ifdef USE_FEEDING_METHOD_BUFFER
  666. if (retCode == RETCODE_INSUFFICIENT_RESOURCE)
  667. ComponentNotifyListeners(com, COMPONENT_EVENT_DEC_INSUFFIC_RESOURCE, NULL);
  668. #endif
  669. if ( retCode == RETCODE_VPU_RESPONSE_TIMEOUT)
  670. CNMErrorSet(CNM_ERROR_HANGUP);
  671. return FALSE;
  672. }
  673. LoadBalancerAddInstance(ctx->handle->instIndex);
  674. // VPU_DecGiveCommand(ctx->handle, ENABLE_LOGGING, 0);
  675. if (ctx->vbUserData.size == 0) {
  676. vbUserData.size = (1320*1024); /* 40KB * (queue_depth + report_queue_depth+1) = 40KB * (16 + 16 +1) */
  677. vdi_allocate_dma_memory(ctx->testDecConfig.coreIdx, &vbUserData, DEC_ETC, ctx->handle->instIndex);
  678. }
  679. VPU_DecGiveCommand(ctx->handle, SET_ADDR_REP_USERDATA, (void*)&vbUserData.phys_addr);
  680. VPU_DecGiveCommand(ctx->handle, SET_SIZE_REP_USERDATA, (void*)&vbUserData.size);
  681. VPU_DecGiveCommand(ctx->handle, ENABLE_REP_USERDATA, (void*)&ctx->testDecConfig.enableUserData);
  682. VPU_DecGiveCommand(ctx->handle, SET_CYCLE_PER_TICK, (void*)&ctx->cyclePerTick);
  683. if (ctx->testDecConfig.thumbnailMode == TRUE) {
  684. VPU_DecGiveCommand(ctx->handle, ENABLE_DEC_THUMBNAIL_MODE, NULL);
  685. }
  686. ctx->vbUserData = vbUserData;
  687. ctx->stateDoing = FALSE;
  688. return TRUE;
  689. }
  690. static BOOL DecodeHeader(ComponentImpl* com, PortContainerES* in, BOOL* done)
  691. {
  692. DecoderContext* ctx = (DecoderContext*)com->context;
  693. DecHandle handle = ctx->handle;
  694. Uint32 coreIdx = ctx->testDecConfig.coreIdx;
  695. RetCode ret = RETCODE_SUCCESS;
  696. DEC_INT_STATUS status;
  697. DecInitialInfo* initialInfo = &ctx->initialInfo;
  698. SecAxiUse secAxiUse;
  699. CNMComListenerDecCompleteSeq lsnpCompleteSeq;
  700. DecInfo* pDecInfo = VPU_HANDLE_TO_DECINFO(ctx->handle);
  701. *done = FALSE;
  702. if (BS_MODE_PIC_END == ctx->decOpenParam.bitstreamMode && NULL == in) {
  703. return TRUE;
  704. }
  705. if (ctx->stateDoing == FALSE) {
  706. /* previous state done */
  707. ret = VPU_DecIssueSeqInit(handle);
  708. if (RETCODE_QUEUEING_FAILURE == ret) {
  709. return TRUE; // Try again
  710. }
  711. ComponentNotifyListeners(com, COMPONENT_EVENT_DEC_ISSUE_SEQ, NULL);
  712. if (ret != RETCODE_SUCCESS) {
  713. ChekcAndPrintDebugInfo(ctx->handle, FALSE, ret);
  714. VLOG(ERR, "%s:%d Failed to VPU_DecIssueSeqInit() ret(%d)\n", __FUNCTION__, __LINE__, ret);
  715. return FALSE;
  716. }
  717. }
  718. ctx->stateDoing = TRUE;
  719. while (com->terminate == FALSE) {
  720. if ((status=HandlingInterruptFlag(com)) == DEC_INT_STATUS_DONE) {
  721. break;
  722. }
  723. else if (status == DEC_INT_STATUS_TIMEOUT) {
  724. HandleDecoderError(ctx->handle, 0, NULL);
  725. VPU_DecUpdateBitstreamBuffer(handle, STREAM_END_SIZE); /* To finish bitstream empty status */
  726. VPU_SWReset(coreIdx, SW_RESET_SAFETY, handle);
  727. VPU_DecUpdateBitstreamBuffer(handle, STREAM_END_CLEAR_FLAG); /* To finish bitstream empty status */
  728. return FALSE;
  729. }
  730. else if (status == DEC_INT_STATUS_EMPTY) {
  731. return TRUE;
  732. }
  733. else if (status == DEC_INT_STATUS_NONE) {
  734. return TRUE;
  735. }
  736. else {
  737. VLOG(INFO, "%s:%d Unknown interrupt status: %d\n", __FUNCTION__, __LINE__, status);
  738. return FALSE;
  739. }
  740. }
  741. ret = VPU_DecCompleteSeqInit(handle, initialInfo);
  742. if (pDecInfo->productCode == WAVE521C_DUAL_CODE) {
  743. if ( initialInfo->lumaBitdepth == 8 && initialInfo->chromaBitdepth == 8) {
  744. ctx->testDecConfig.mapType = COMPRESSED_FRAME_MAP_DUAL_CORE_8BIT;
  745. }
  746. else {
  747. ctx->testDecConfig.mapType = COMPRESSED_FRAME_MAP_DUAL_CORE_10BIT;
  748. }
  749. }
  750. strcpy(lsnpCompleteSeq.refYuvPath, ctx->testDecConfig.refYuvPath);
  751. lsnpCompleteSeq.ret = ret;
  752. lsnpCompleteSeq.initialInfo = initialInfo;
  753. lsnpCompleteSeq.wtlFormat = ctx->wtlFormat;
  754. lsnpCompleteSeq.cbcrInterleave = ctx->decOpenParam.cbcrInterleave;
  755. lsnpCompleteSeq.bitstreamFormat = ctx->decOpenParam.bitstreamFormat;
  756. ComponentNotifyListeners(com, COMPONENT_EVENT_DEC_COMPLETE_SEQ, (void*)&lsnpCompleteSeq);
  757. if (ret != RETCODE_SUCCESS) {
  758. VLOG(ERR, "%s:%d FAILED TO DEC_PIC_HDR: ret(%d), SEQERR(%08x)\n", __FUNCTION__, __LINE__, ret, initialInfo->seqInitErrReason);
  759. ChekcAndPrintDebugInfo(ctx->handle, FALSE, ret);
  760. return FALSE;
  761. }
  762. if (ctx->decOpenParam.wtlEnable == TRUE) {
  763. VPU_DecGiveCommand(ctx->handle, DEC_SET_WTL_FRAME_FORMAT, &ctx->wtlFormat);
  764. }
  765. /* Set up the secondary AXI is depending on H/W configuration.
  766. * Note that turn off all the secondary AXI configuration
  767. * if target ASIC has no memory only for IP, LF and BIT.
  768. */
  769. secAxiUse.u.wave.useIpEnable = (ctx->testDecConfig.secondaryAXI&0x01) ? TRUE : FALSE;
  770. secAxiUse.u.wave.useLfRowEnable = (ctx->testDecConfig.secondaryAXI&0x02) ? TRUE : FALSE;
  771. secAxiUse.u.wave.useBitEnable = (ctx->testDecConfig.secondaryAXI&0x04) ? TRUE : FALSE;
  772. secAxiUse.u.wave.useSclEnable = (ctx->testDecConfig.secondaryAXI&0x08) ? TRUE : FALSE;
  773. VPU_DecGiveCommand(ctx->handle, SET_SEC_AXI, &secAxiUse);
  774. // Set up scale
  775. if (ctx->testDecConfig.scaleDownWidth > 0 || ctx->testDecConfig.scaleDownHeight > 0) {
  776. ScalerInfo sclInfo = {0};
  777. sclInfo.scaleWidth = ctx->testDecConfig.scaleDownWidth;
  778. sclInfo.scaleHeight = ctx->testDecConfig.scaleDownHeight;
  779. VLOG(INFO, "[SCALE INFO] %dx%d\n", sclInfo.scaleWidth, sclInfo.scaleHeight);
  780. sclInfo.enScaler = TRUE;
  781. if (VPU_DecGiveCommand(ctx->handle, DEC_SET_SCALER_INFO, (void*)&sclInfo) != RETCODE_SUCCESS) {
  782. VLOG(ERR, "Failed to VPU_DecGiveCommand(DEC_SET_SCALER_INFO)\n");
  783. return FALSE;
  784. }
  785. }
  786. ctx->stateDoing = FALSE;
  787. *done = TRUE;
  788. return TRUE;
  789. }
  790. static BOOL ExecuteDecoder(ComponentImpl* com, PortContainer* in , PortContainer* out)
  791. {
  792. DecoderContext* ctx = (DecoderContext*)com->context;
  793. BOOL ret = FALSE;;
  794. BitStreamMode bsMode = ctx->decOpenParam.bitstreamMode;
  795. BOOL done = FALSE;
  796. if (in) in->reuse = TRUE;
  797. if (out) out->reuse = TRUE;
  798. if (ctx->state == DEC_STATE_INIT_SEQ || ctx->state == DEC_STATE_DECODING) {
  799. if (UpdateBitstream(ctx, (PortContainerES*)in) == FALSE) {
  800. return FALSE;
  801. }
  802. if (in) {
  803. // In ring-buffer mode, it has to return back a container immediately.
  804. if (bsMode == BS_MODE_PIC_END) {
  805. if (ctx->state == DEC_STATE_INIT_SEQ) {
  806. in->reuse = TRUE;
  807. }
  808. in->consumed = FALSE;
  809. }
  810. else {
  811. in->consumed = (in->reuse == FALSE);
  812. }
  813. }
  814. }
  815. if (ctx->doReset == TRUE) {
  816. if (in) in->reuse = FALSE;
  817. return DoReset(com);
  818. }
  819. switch (ctx->state) {
  820. case DEC_STATE_OPEN_DECODER:
  821. ret = OpenDecoder(com);
  822. if (ctx->stateDoing == FALSE) ctx->state = DEC_STATE_INIT_SEQ;
  823. break;
  824. case DEC_STATE_INIT_SEQ:
  825. ret = DecodeHeader(com, (PortContainerES*)in, &done);
  826. if (TRUE == done) ctx->state = DEC_STATE_REGISTER_FB;
  827. break;
  828. case DEC_STATE_REGISTER_FB:
  829. ret = RegisterFrameBuffers(com);
  830. if (ctx->stateDoing == FALSE) {
  831. ctx->state = DEC_STATE_DECODING;
  832. DisplayDecodedInformation(ctx->handle, ctx->decOpenParam.bitstreamFormat, 0, NULL, ctx->testDecConfig.performance, 0);
  833. }
  834. break;
  835. case DEC_STATE_DECODING:
  836. ret = Decode(com, (PortContainerES*)in, (PortContainerDisplay*)out);
  837. break;
  838. default:
  839. ret = FALSE;
  840. break;
  841. }
  842. if (ret == FALSE || com->terminate == TRUE) {
  843. ComponentNotifyListeners(com, COMPONENT_EVENT_DEC_DECODED_ALL, (void*)ctx->handle);
  844. if (out) {
  845. out->reuse = FALSE;
  846. out->last = TRUE;
  847. }
  848. }
  849. return ret;
  850. }
  851. static BOOL PrepareDecoder(ComponentImpl* com, BOOL* done)
  852. {
  853. *done = TRUE;
  854. return TRUE;
  855. }
  856. static void ReleaseDecoder(ComponentImpl* com)
  857. {
  858. // Nothing to do
  859. }
  860. static BOOL DestroyDecoder(ComponentImpl* com)
  861. {
  862. DecoderContext* ctx = (DecoderContext*)com->context;
  863. DEC_INT_STATUS intStatus;
  864. BOOL success = TRUE;
  865. Uint32 timeout = 0;
  866. Uint32 i = 0;
  867. if (NULL != ctx->handle) {
  868. LoadBalancerRemoveInstance(ctx->handle->instIndex);
  869. VPU_DecUpdateBitstreamBuffer(ctx->handle, STREAM_END_SET_FLAG);
  870. while (VPU_DecClose(ctx->handle) == RETCODE_VPU_STILL_RUNNING) {
  871. if ((intStatus=HandlingInterruptFlag(com)) == DEC_INT_STATUS_TIMEOUT) {
  872. HandleDecoderError(ctx->handle, ctx->numDecoded, NULL);
  873. VLOG(ERR, "<%s:%d> NO RESPONSE FROM VPU_DecClose()\n", __FUNCTION__, __LINE__);
  874. success = FALSE;
  875. break;
  876. }
  877. else if (intStatus == DEC_INT_STATUS_DONE) {
  878. DecOutputInfo outputInfo;
  879. VLOG(INFO, "VPU_DecClose() : CLEAR REMAIN INTERRUPT\n");
  880. VPU_DecGetOutputInfo(ctx->handle, &outputInfo);
  881. continue;
  882. }
  883. if (timeout > VPU_BUSY_CHECK_TIMEOUT) {
  884. VLOG(ERR, "<%s:%d> Failed to VPU_DecClose\n", __FUNCTION__, __LINE__);
  885. }
  886. for (i=0; i<MAX_REG_FRAME; i++) {
  887. VPU_DecClrDispFlag(ctx->handle, i);
  888. }
  889. timeout++;
  890. }
  891. ComponentNotifyListeners(com, COMPONENT_EVENT_DEC_CLOSE, NULL);
  892. }
  893. if (ctx->vbUserData.size) {
  894. vdi_free_dma_memory(ctx->testDecConfig.coreIdx, &ctx->vbUserData, DEC_ETC, ctx->handle->instIndex);
  895. }
  896. VPU_DeInit(ctx->decOpenParam.coreIdx);
  897. osal_free(ctx);
  898. return success;
  899. }
  900. static Component CreateDecoder(ComponentImpl* com, CNMComponentConfig* componentParam)
  901. {
  902. DecoderContext* ctx;
  903. Uint32 coreIdx = componentParam->testDecConfig.coreIdx;
  904. Uint16* firmware = (Uint16*)componentParam->bitcode;
  905. Uint32 firmwareSize = componentParam->sizeOfBitcode;
  906. RetCode retCode;
  907. retCode = VPU_InitWithBitcode(coreIdx, firmware, firmwareSize);
  908. if (retCode != RETCODE_SUCCESS && retCode != RETCODE_CALLED_BEFORE) {
  909. VLOG(INFO, "%s:%d Failed to VPU_InitiWithBitcode, ret(%08x)\n", __FUNCTION__, __LINE__, retCode);
  910. return FALSE;
  911. }
  912. com->context = (DecoderContext*)osal_malloc(sizeof(DecoderContext));
  913. osal_memset(com->context, 0, sizeof(DecoderContext));
  914. ctx = (DecoderContext*)com->context;
  915. retCode = PrintVpuProductInfo(coreIdx, &ctx->attr);
  916. if (retCode == RETCODE_VPU_RESPONSE_TIMEOUT ) {
  917. CNMErrorSet(CNM_ERROR_HANGUP);
  918. VLOG(INFO, "<%s:%d> Failed to PrintVpuProductInfo()\n", __FUNCTION__, __LINE__);
  919. HandleDecoderError(ctx->handle, ctx->numDecoded, NULL);
  920. return FALSE;
  921. }
  922. ctx->cyclePerTick = 32768;
  923. if (TRUE == ctx->attr.supportNewTimer)
  924. ctx->cyclePerTick = 256;
  925. memcpy(&(ctx->decOpenParam), &(componentParam->decOpenParam), sizeof(DecOpenParam));
  926. ctx->wtlFormat = componentParam->testDecConfig.wtlFormat;
  927. ctx->frameNumToStop = componentParam->testDecConfig.forceOutNum;
  928. ctx->testDecConfig = componentParam->testDecConfig;
  929. ctx->state = DEC_STATE_OPEN_DECODER;
  930. ctx->stateDoing = FALSE;
  931. VLOG(INFO, "PRODUCT ID: %d\n", ctx->attr.productId);
  932. return (Component)com;
  933. }
  934. ComponentImpl waveDecoderComponentImpl = {
  935. "wave_decoder",
  936. NULL,
  937. {0,},
  938. {0,},
  939. sizeof(PortContainerDisplay),
  940. 5,
  941. CreateDecoder,
  942. GetParameterDecoder,
  943. SetParameterDecoder,
  944. PrepareDecoder,
  945. ExecuteDecoder,
  946. ReleaseDecoder,
  947. DestroyDecoder
  948. };