component_enc_encoder.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225
  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 <time.h>
  27. #include <sys/time.h>
  28. #include "cnm_app.h"
  29. #include "component.h"
  30. #include "misc/debug.h"
  31. #ifndef min
  32. #define min(a,b) (((a) < (b)) ? (a) : (b))
  33. #endif
  34. #ifndef max
  35. #define max(a,b) (((a) > (b)) ? (a) : (b))
  36. #endif
  37. typedef enum {
  38. ENC_INT_STATUS_NONE, // Interrupt not asserted yet
  39. ENC_INT_STATUS_FULL, // Need more buffer
  40. ENC_INT_STATUS_DONE, // Interrupt asserted
  41. ENC_INT_STATUS_LOW_LATENCY,
  42. ENC_INT_STATUS_TIMEOUT, // Interrupt not asserted during given time.
  43. ENC_INT_STATUS_SRC_RELEASED,
  44. } ENC_INT_STATUS;
  45. typedef enum {
  46. ENCODER_STATE_OPEN,
  47. ENCODER_STATE_INIT_SEQ,
  48. ENCODER_STATE_REGISTER_FB,
  49. ENCODER_STATE_ENCODE_HEADER,
  50. ENCODER_STATE_ENCODING,
  51. } EncoderState;
  52. typedef struct {
  53. EncHandle handle;
  54. TestEncConfig testEncConfig;
  55. EncOpenParam encOpenParam;
  56. ParamEncNeedFrameBufferNum fbCount;
  57. Uint32 fbCountValid;
  58. Uint32 frameIdx;
  59. #ifdef SUPPORT_LOOK_AHEAD_RC
  60. Uint32 encPicCnt;
  61. #endif
  62. vpu_buffer_t vbCustomLambda;
  63. vpu_buffer_t vbScalingList;
  64. Uint32 customLambda[NUM_CUSTOM_LAMBDA];
  65. UserScalingList scalingList;
  66. vpu_buffer_t vbCustomMap[MAX_REG_FRAME];
  67. EncoderState state;
  68. BOOL stateDoing;
  69. EncInitialInfo initialInfo;
  70. Queue* encOutQ;
  71. EncParam encParam;
  72. Int32 encodedSrcFrmIdxArr[ENC_SRC_BUF_NUM];
  73. ParamEncBitstreamBuffer bsBuf;
  74. BOOL fullInterrupt;
  75. Uint32 changedCount;
  76. Uint64 startTimeout;
  77. #ifdef SUPPORT_LOOK_AHEAD_RC
  78. int larcEnable;
  79. int larcPass;
  80. int larcSize;
  81. EncLarcInfo larcInfo;
  82. int larcPass2Started;
  83. int larcPass1Finish;
  84. #endif
  85. Uint32 cyclePerTick;
  86. vpu_buffer_t *bsBuffer[20];
  87. } EncoderContext;
  88. static BOOL FindEsBuffer(EncoderContext* ctx, PhysicalAddress addr, vpu_buffer_t* bs)
  89. {
  90. Uint32 i;
  91. for (i=0; i<ctx->bsBuf.num; i++) {
  92. if (addr == ctx->bsBuf.bs[i].phys_addr) {
  93. *bs = ctx->bsBuf.bs[i];
  94. return TRUE;
  95. }
  96. }
  97. return FALSE;
  98. }
  99. static void SetEncPicParam(ComponentImpl* com, PortContainerYuv* in, EncParam* encParam)
  100. {
  101. EncoderContext* ctx = (EncoderContext*)com->context;
  102. TestEncConfig testEncConfig = ctx->testEncConfig;
  103. Uint32 frameIdx = ctx->frameIdx;
  104. Int32 srcFbWidth = VPU_ALIGN8(ctx->encOpenParam.picWidth);
  105. Int32 srcFbHeight = VPU_ALIGN8(ctx->encOpenParam.picHeight);
  106. Uint32 productId;
  107. vpu_buffer_t* buf = (vpu_buffer_t*)Queue_Peek(ctx->encOutQ);
  108. productId = VPU_GetProductId(ctx->encOpenParam.coreIdx);
  109. encParam->picStreamBufferAddr = buf->phys_addr;
  110. encParam->picStreamBufferSize = buf->size;
  111. encParam->srcIdx = in->srcFbIndex;
  112. encParam->srcEndFlag = in->last;
  113. encParam->sourceFrame = &in->fb;
  114. encParam->sourceFrame->sourceLBurstEn = 0;
  115. if (testEncConfig.useAsLongtermPeriod > 0 && testEncConfig.refLongtermPeriod > 0) {
  116. encParam->useCurSrcAsLongtermPic = (frameIdx % testEncConfig.useAsLongtermPeriod) == 0 ? 1 : 0;
  117. encParam->useLongtermRef = (frameIdx % testEncConfig.refLongtermPeriod) == 0 ? 1 : 0;
  118. }
  119. encParam->skipPicture = 0;
  120. encParam->forceAllCtuCoefDropEnable = 0;
  121. encParam->forcePicQpEnable = 0;
  122. encParam->forcePicQpI = 0;
  123. encParam->forcePicQpP = 0;
  124. encParam->forcePicQpB = 0;
  125. encParam->forcePicTypeEnable = 0;
  126. encParam->forcePicType = 0;
  127. if (testEncConfig.forceIdrPicIdx == frameIdx) {
  128. encParam->forcePicTypeEnable = 1;
  129. encParam->forcePicType = 3; // IDR
  130. }
  131. // FW will encode header data implicitly when changing the header syntaxes
  132. encParam->codeOption.implicitHeaderEncode = 1;
  133. encParam->codeOption.encodeAUD = testEncConfig.encAUD;
  134. encParam->codeOption.encodeEOS = testEncConfig.encEOS;
  135. encParam->codeOption.encodeEOB = testEncConfig.encEOB;
  136. // set custom map param
  137. if (productId != PRODUCT_ID_521)
  138. encParam->customMapOpt.roiAvgQp = testEncConfig.roi_avg_qp;
  139. encParam->customMapOpt.customLambdaMapEnable = testEncConfig.lambda_map_enable;
  140. encParam->customMapOpt.customModeMapEnable = testEncConfig.mode_map_flag & 0x1;
  141. encParam->customMapOpt.customCoefDropEnable = (testEncConfig.mode_map_flag & 0x2) >> 1;
  142. encParam->customMapOpt.customRoiMapEnable = testEncConfig.roi_enable;
  143. if (in->prevMapReuse == FALSE) {
  144. // packaging roi/lambda/mode data to custom map buffer.
  145. if (encParam->customMapOpt.customRoiMapEnable || encParam->customMapOpt.customLambdaMapEnable ||
  146. encParam->customMapOpt.customModeMapEnable || encParam->customMapOpt.customCoefDropEnable) {
  147. SetMapData(testEncConfig.coreIdx, testEncConfig, ctx->encOpenParam, encParam, srcFbWidth, srcFbHeight, ctx->vbCustomMap[encParam->srcIdx].phys_addr);
  148. }
  149. // host should set proper value.
  150. // set weighted prediction param.
  151. if ((testEncConfig.wp_param_flag & 0x1)) {
  152. char lineStr[256] = {0,};
  153. Uint32 meanY, meanCb, meanCr, sigmaY, sigmaCb, sigmaCr;
  154. Uint32 maxMean = (ctx->encOpenParam.bitstreamFormat == STD_AVC) ? 0xffff : ((1 << ctx->encOpenParam.EncStdParam.waveParam.internalBitDepth) - 1);
  155. Uint32 maxSigma = (ctx->encOpenParam.bitstreamFormat == STD_AVC) ? 0xffff : ((1 << (ctx->encOpenParam.EncStdParam.waveParam.internalBitDepth + 6)) - 1);
  156. fgets(lineStr, 256, testEncConfig.wp_param_file);
  157. sscanf(lineStr, "%d %d %d %d %d %d\n", &meanY, &meanCb, &meanCr, &sigmaY, &sigmaCb, &sigmaCr);
  158. meanY = max(min(maxMean, meanY), 0);
  159. meanCb = max(min(maxMean, meanCb), 0);
  160. meanCr = max(min(maxMean, meanCr), 0);
  161. sigmaY = max(min(maxSigma, sigmaY), 0);
  162. sigmaCb = max(min(maxSigma, sigmaCb), 0);
  163. sigmaCr = max(min(maxSigma, sigmaCr), 0);
  164. // set weighted prediction param.
  165. encParam->wpPixSigmaY = sigmaY;
  166. encParam->wpPixSigmaCb = sigmaCb;
  167. encParam->wpPixSigmaCr = sigmaCr;
  168. encParam->wpPixMeanY = meanY;
  169. encParam->wpPixMeanCb = meanCb;
  170. encParam->wpPixMeanCr = meanCr;
  171. }
  172. }
  173. }
  174. static BOOL RegisterFrameBuffers(ComponentImpl* com)
  175. {
  176. EncoderContext* ctx= (EncoderContext*)com->context;
  177. FrameBuffer* pReconFb = NULL;
  178. FrameBuffer* pSrcFb = NULL;
  179. FrameBufferAllocInfo srcFbAllocInfo;
  180. Uint32 reconFbStride = 0;
  181. Uint32 reconFbHeight = 0;
  182. ParamEncFrameBuffer paramFb;
  183. RetCode result;
  184. CNMComponentParamRet ret;
  185. BOOL success;
  186. Uint32 idx;
  187. EncInfo* pEncInfo = VPU_HANDLE_TO_ENCINFO(ctx->handle);
  188. ctx->stateDoing = TRUE;
  189. ret = ComponentGetParameter(com, com->srcPort.connectedComponent, GET_PARAM_YUVFEEDER_FRAME_BUF, (void*)&paramFb);
  190. if (ComponentParamReturnTest(ret, &success) == FALSE) return success;
  191. pReconFb = paramFb.reconFb;
  192. reconFbStride = paramFb.reconFbAllocInfo.stride;
  193. reconFbHeight = paramFb.reconFbAllocInfo.height;
  194. if (pEncInfo->productCode == WAVE521C_DUAL_CODE) {
  195. TiledMapType mapType = ctx->encOpenParam.EncStdParam.waveParam.internalBitDepth==8?COMPRESSED_FRAME_MAP_DUAL_CORE_8BIT:COMPRESSED_FRAME_MAP_DUAL_CORE_10BIT;
  196. result = VPU_EncRegisterFrameBuffer(ctx->handle, pReconFb, ctx->fbCount.reconFbNum, reconFbStride, reconFbHeight, mapType);
  197. }
  198. else
  199. result = VPU_EncRegisterFrameBuffer(ctx->handle, pReconFb, ctx->fbCount.reconFbNum, reconFbStride, reconFbHeight, COMPRESSED_FRAME_MAP);
  200. if (result != RETCODE_SUCCESS) {
  201. VLOG(ERR, "%s:%d Failed to VPU_EncRegisterFrameBuffer(%d)\n", __FUNCTION__, __LINE__, result);
  202. ChekcAndPrintDebugInfo(ctx->handle, TRUE, result);
  203. return FALSE;
  204. }
  205. ComponentNotifyListeners(com, COMPONENT_EVENT_ENC_REGISTER_FB, NULL);
  206. pSrcFb = paramFb.srcFb;
  207. srcFbAllocInfo = paramFb.srcFbAllocInfo;
  208. result = VPU_EncAllocateFrameBuffer(ctx->handle, srcFbAllocInfo, pSrcFb);
  209. if (result != RETCODE_SUCCESS) {
  210. VLOG(ERR, "VPU_EncAllocateFrameBuffer fail to allocate source frame buffer\n");
  211. ChekcAndPrintDebugInfo(ctx->handle, TRUE, result);
  212. return FALSE;
  213. }
  214. if (ctx->testEncConfig.roi_enable || ctx->testEncConfig.lambda_map_enable || ctx->testEncConfig.mode_map_flag) {
  215. for (idx = 0; idx < ctx->fbCount.srcFbNum; idx++) {
  216. ctx->vbCustomMap[idx].size = (ctx->encOpenParam.bitstreamFormat == STD_AVC) ? MAX_MB_NUM : MAX_CTU_NUM * 8;
  217. if (vdi_allocate_dma_memory(ctx->testEncConfig.coreIdx, &ctx->vbCustomMap[idx], ENC_ETC, ctx->handle->instIndex) < 0) {
  218. VLOG(ERR, "fail to allocate ROI buffer\n");
  219. return FALSE;
  220. }
  221. }
  222. }
  223. ctx->stateDoing = FALSE;
  224. return TRUE;
  225. }
  226. static CNMComponentParamRet GetParameterEncoder(ComponentImpl* from, ComponentImpl* com, GetParameterCMD commandType, void* data)
  227. {
  228. EncoderContext* ctx = (EncoderContext*)com->context;
  229. BOOL result = TRUE;
  230. ParamEncNeedFrameBufferNum* fbCount;
  231. PortContainerYuv* container;
  232. ParamVpuStatus* status;
  233. QueueStatusInfo cqInfo;
  234. #ifdef SUPPORT_LOOK_AHEAD_RC
  235. EncLarcInfo *larcInfo;
  236. #endif
  237. switch(commandType) {
  238. case GET_PARAM_COM_IS_CONTAINER_CONUSUMED:
  239. container = (PortContainerYuv*)data;
  240. if (ctx->encodedSrcFrmIdxArr[container->srcFbIndex]) {
  241. ctx->encodedSrcFrmIdxArr[container->srcFbIndex] = 0;
  242. container->consumed = TRUE;
  243. }
  244. break;
  245. case GET_PARAM_ENC_HANDLE:
  246. if (ctx->handle == NULL) return CNM_COMPONENT_PARAM_NOT_READY;
  247. *(EncHandle*)data = ctx->handle;
  248. break;
  249. case GET_PARAM_ENC_FRAME_BUF_NUM:
  250. if (ctx->fbCountValid == FALSE) return CNM_COMPONENT_PARAM_NOT_READY;
  251. fbCount = (ParamEncNeedFrameBufferNum*)data;
  252. fbCount->reconFbNum = ctx->fbCount.reconFbNum;
  253. fbCount->srcFbNum = ctx->fbCount.srcFbNum;
  254. break;
  255. case GET_PARAM_ENC_FRAME_BUF_REGISTERED:
  256. if (ctx->state <= ENCODER_STATE_REGISTER_FB) return CNM_COMPONENT_PARAM_NOT_READY;
  257. *(BOOL*)data = TRUE;
  258. break;
  259. #ifdef SUPPORT_LOOK_AHEAD_RC
  260. case GET_PARAM_ENC_LARC_INFO:
  261. if ( ctx->larcEnable == FALSE ) {
  262. result = FALSE;
  263. break;
  264. }
  265. larcInfo = (EncLarcInfo*)data;
  266. if ( ctx->larcPass == LOOK_AHEAD_RC_PASS1 ) {
  267. larcInfo->Pass1encPicCnt = ctx->encPicCnt;
  268. larcInfo->larcPass1Finish = ctx->larcPass1Finish;
  269. osal_memcpy(larcInfo->larcData, ctx->larcInfo.larcData, sizeof(larcInfo->larcData));
  270. }
  271. else {
  272. larcInfo->Pass2encPicCnt = ctx->encPicCnt;
  273. }
  274. break;
  275. #endif
  276. case GET_PARAM_VPU_STATUS:
  277. if (ctx->state != ENCODER_STATE_ENCODING) return CNM_COMPONENT_PARAM_NOT_READY;
  278. VPU_EncGiveCommand(ctx->handle, ENC_GET_QUEUE_STATUS, &cqInfo);
  279. status = (ParamVpuStatus*)data;
  280. status->cq = cqInfo;
  281. break;
  282. default:
  283. result = FALSE;
  284. break;
  285. }
  286. return (result == TRUE) ? CNM_COMPONENT_PARAM_SUCCESS : CNM_COMPONENT_PARAM_FAILURE;
  287. }
  288. static CNMComponentParamRet SetParameterEncoder(ComponentImpl* from, ComponentImpl* com, SetParameterCMD commandType, void* data)
  289. {
  290. BOOL result = TRUE;
  291. switch (commandType) {
  292. case SET_PARAM_COM_PAUSE:
  293. com->pause = *(BOOL*)data;
  294. break;
  295. default:
  296. VLOG(ERR, "Unknown SetParameterCMD Type : %d\n", commandType);
  297. result = FALSE;
  298. break;
  299. }
  300. return (result == TRUE) ? CNM_COMPONENT_PARAM_SUCCESS : CNM_COMPONENT_PARAM_FAILURE;
  301. }
  302. static ENC_INT_STATUS HandlingInterruptFlag(ComponentImpl* com)
  303. {
  304. EncoderContext* ctx = (EncoderContext*)com->context;
  305. EncHandle handle = ctx->handle;
  306. Int32 interruptFlag = 0;
  307. Uint32 interruptWaitTime = VPU_WAIT_TIME_OUT_CQ;
  308. Uint32 interruptTimeout = VPU_ENC_TIMEOUT;
  309. ENC_INT_STATUS status = ENC_INT_STATUS_NONE;
  310. if (ctx->startTimeout == 0ULL) {
  311. ctx->startTimeout = osal_gettime();
  312. }
  313. do {
  314. interruptFlag = VPU_WaitInterruptEx(handle, interruptWaitTime);
  315. if (INTERRUPT_TIMEOUT_VALUE == interruptFlag) {
  316. Uint64 currentTimeout = osal_gettime();
  317. if ((currentTimeout - ctx->startTimeout) > interruptTimeout) {
  318. VLOG(ERR, "<%s:%d> startTimeout(%lld) currentTime(%lld) diff(%d)\n",
  319. __FUNCTION__, __LINE__, ctx->startTimeout, currentTimeout, (Uint32)(currentTimeout - ctx->startTimeout));
  320. CNMErrorSet(CNM_ERROR_HANGUP);
  321. status = ENC_INT_STATUS_TIMEOUT;
  322. break;
  323. }
  324. interruptFlag = 0;
  325. }
  326. if (interruptFlag < 0) {
  327. VLOG(ERR, "<%s:%d> interruptFlag is negative value! %08x\n", __FUNCTION__, __LINE__, interruptFlag);
  328. }
  329. if (interruptFlag > 0) {
  330. VPU_ClearInterruptEx(handle, interruptFlag);
  331. ctx->startTimeout = 0ULL;
  332. if (interruptFlag & (1<<INT_WAVE5_ENC_SET_PARAM)) {
  333. status = ENC_INT_STATUS_DONE;
  334. break;
  335. }
  336. if (interruptFlag & (1<<INT_WAVE5_ENC_PIC)) {
  337. status = ENC_INT_STATUS_DONE;
  338. break;
  339. }
  340. if (interruptFlag & (1<<INT_WAVE5_BSBUF_FULL)) {
  341. status = ENC_INT_STATUS_FULL;
  342. break;
  343. }
  344. if (interruptFlag & (1<<INT_WAVE5_ENC_LOW_LATENCY)) {
  345. status = ENC_INT_STATUS_LOW_LATENCY;
  346. }
  347. #ifdef SUPPORT_SOURCE_RELEASE_INTERRUPT
  348. if (interruptFlag & (1 << INT_WAVE5_ENC_SRC_RELEASE)) {
  349. status = ENC_INT_STATUS_SRC_RELEASED;
  350. }
  351. #endif
  352. }
  353. } while (FALSE);
  354. return status;
  355. }
  356. static BOOL SetSequenceInfo(ComponentImpl* com)
  357. {
  358. EncoderContext* ctx = (EncoderContext*)com->context;
  359. EncHandle handle = ctx->handle;
  360. RetCode ret = RETCODE_SUCCESS;
  361. ENC_INT_STATUS status;
  362. EncInitialInfo* initialInfo = &ctx->initialInfo;
  363. CNMComListenerEncCompleteSeq lsnpCompleteSeq = {0};
  364. #ifdef SUPPORT_LOOK_AHEAD_RC
  365. if ( ctx->larcPass == LOOK_AHEAD_RC_PASS2 && ctx->larcPass2Started == FALSE) {
  366. BOOL success;
  367. ret = ComponentGetParameter(com, com->srcPort.connectedComponent, GET_PARAM_ENC_LARC_INFO, &ctx->larcInfo);
  368. if (ComponentParamReturnTest(ret, &success) == FALSE) {
  369. return success;
  370. }
  371. ctx->stateDoing = FALSE;
  372. if ( ctx->larcInfo.Pass1encPicCnt < ctx->larcSize && FALSE == ctx->larcInfo.larcPass1Finish) {
  373. ctx->stateDoing = TRUE;
  374. return success;
  375. }
  376. if (ctx->larcInfo.larcPass1Finish == TRUE)
  377. ctx->larcInfo.larcData[ctx->larcInfo.Pass1encPicCnt][0] = -1;
  378. ctx->larcPass2Started = TRUE;
  379. if ( ctx->larcPass == LOOK_AHEAD_RC_PASS2 ) {
  380. VPU_EncGiveCommand(handle, ENC_SET_LARC_DATA, &ctx->larcInfo);
  381. }
  382. }
  383. #endif
  384. if (ctx->stateDoing == FALSE) {
  385. do {
  386. ret = VPU_EncIssueSeqInit(handle);
  387. } while (ret == RETCODE_QUEUEING_FAILURE && com->terminate == FALSE);
  388. if (ret != RETCODE_SUCCESS) {
  389. VLOG(ERR, "%s:%d Failed to VPU_EncIssueSeqInit() ret(%d)\n", __FUNCTION__, __LINE__, ret);
  390. ChekcAndPrintDebugInfo(ctx->handle, TRUE, ret);
  391. return FALSE;
  392. }
  393. ComponentNotifyListeners(com, COMPONENT_EVENT_ENC_ISSUE_SEQ, NULL);
  394. }
  395. ctx->stateDoing = TRUE;
  396. while (com->terminate == FALSE) {
  397. if ((status=HandlingInterruptFlag(com)) == ENC_INT_STATUS_DONE) {
  398. break;
  399. }
  400. else if (status == ENC_INT_STATUS_NONE) {
  401. return TRUE;
  402. }
  403. else if (status == ENC_INT_STATUS_TIMEOUT) {
  404. VLOG(INFO, "%s:%d INSTANCE #%d INTERRUPT TIMEOUT\n", __FUNCTION__, __LINE__, handle->instIndex);
  405. HandleEncoderError(ctx->handle, ctx->frameIdx, NULL);
  406. return FALSE;
  407. }
  408. else {
  409. VLOG(INFO, "%s:%d Unknown interrupt status: %d\n", __FUNCTION__, __LINE__, status);
  410. return FALSE;
  411. }
  412. }
  413. if ((ret=VPU_EncCompleteSeqInit(handle, initialInfo)) != RETCODE_SUCCESS) {
  414. VLOG(ERR, "%s:%d FAILED TO ENC_PIC_HDR: ret(%d), SEQERR(%08x)\n",
  415. __FUNCTION__, __LINE__, ret, initialInfo->seqInitErrReason);
  416. ChekcAndPrintDebugInfo(ctx->handle, TRUE, ret);
  417. return FALSE;
  418. }
  419. lsnpCompleteSeq.handle = handle;
  420. ComponentNotifyListeners(com, COMPONENT_EVENT_ENC_COMPLETE_SEQ, (void*)&lsnpCompleteSeq);
  421. ctx->fbCount.reconFbNum = initialInfo->minFrameBufferCount;
  422. ctx->fbCount.srcFbNum = initialInfo->minSrcFrameCount + COMMAND_QUEUE_DEPTH + EXTRA_SRC_BUFFER_NUM;
  423. if ( ctx->encOpenParam.sourceBufCount > ctx->fbCount.srcFbNum)
  424. ctx->fbCount.srcFbNum = ctx->encOpenParam.sourceBufCount;
  425. ctx->fbCountValid = TRUE;
  426. VLOG(INFO, "[ENCODER] Required reconFbCount=%d, srcFbCount=%d, outNum=%d, %dx%d\n",
  427. ctx->fbCount.reconFbNum, ctx->fbCount.srcFbNum, ctx->testEncConfig.outNum, ctx->encOpenParam.picWidth, ctx->encOpenParam.picHeight);
  428. ctx->stateDoing = FALSE;
  429. return TRUE;
  430. }
  431. static BOOL EncodeHeader(ComponentImpl* com)
  432. {
  433. EncoderContext* ctx = (EncoderContext*)com->context;
  434. EncHandle handle = ctx->handle;
  435. RetCode ret = RETCODE_SUCCESS;
  436. EncHeaderParam encHeaderParam;
  437. vpu_buffer_t* buf;
  438. ctx->stateDoing = TRUE;
  439. buf = Queue_Dequeue(ctx->encOutQ);
  440. osal_memset(&encHeaderParam, 0x00, sizeof(EncHeaderParam));
  441. encHeaderParam.buf = buf->phys_addr;
  442. encHeaderParam.size = buf->size;
  443. if (ctx->encOpenParam.bitstreamFormat == STD_HEVC) {
  444. encHeaderParam.headerType = CODEOPT_ENC_VPS | CODEOPT_ENC_SPS | CODEOPT_ENC_PPS;
  445. }
  446. else {
  447. // H.264
  448. encHeaderParam.headerType = CODEOPT_ENC_SPS | CODEOPT_ENC_PPS;
  449. }
  450. while(1) {
  451. ret = VPU_EncGiveCommand(handle, ENC_PUT_VIDEO_HEADER, &encHeaderParam);
  452. if ( ret != RETCODE_QUEUEING_FAILURE )
  453. break;
  454. #if defined(HAPS_SIM) || defined(CNM_SIM_DPI_INTERFACE)
  455. osal_msleep(1);
  456. #endif
  457. }
  458. DisplayEncodedInformation(ctx->handle, ctx->encOpenParam.bitstreamFormat, 0, NULL, 0, 0, ctx->testEncConfig.performance);
  459. ctx->stateDoing = FALSE;
  460. return TRUE;
  461. }
  462. static BOOL Encode(ComponentImpl* com, PortContainerYuv* in, PortContainerES* out)
  463. {
  464. EncoderContext* ctx = (EncoderContext*)com->context;
  465. BOOL doEncode = FALSE;
  466. BOOL doChangeParam = FALSE;
  467. EncParam* encParam = &ctx->encParam;
  468. EncOutputInfo encOutputInfo;
  469. ENC_INT_STATUS intStatus;
  470. RetCode result;
  471. CNMComListenerHandlingInt lsnpHandlingInt = {0};
  472. CNMComListenerEncDone lsnpPicDone = {0};
  473. CNMComListenerDecReadyOneFrame lsnpReadyOneFrame = {0,};
  474. ENC_QUERY_WRPTR_SEL encWrPtrSel = GET_ENC_PIC_DONE_WRPTR;
  475. QueueStatusInfo qStatus;
  476. int i=0;
  477. #ifdef SUPPORT_LOOK_AHEAD_RC
  478. CNMComponentParamRet ret;
  479. BOOL success;
  480. #endif
  481. lsnpReadyOneFrame.handle = ctx->handle;
  482. ComponentNotifyListeners(com, COMPONENT_EVENT_ENC_READY_ONE_FRAME, &lsnpReadyOneFrame);
  483. #ifdef SUPPORT_LOOK_AHEAD_RC
  484. if ( ctx->larcPass == LOOK_AHEAD_RC_PASS1 ) {
  485. ret = ComponentGetParameter(com, com->sinkPort.connectedComponent, GET_PARAM_ENC_LARC_INFO, &ctx->larcInfo);
  486. if (ComponentParamReturnTest(ret, &success) == FALSE) {
  487. if (in) in->reuse = TRUE;
  488. if (out) out->reuse = TRUE;
  489. return success;
  490. }
  491. if (!(ctx->larcInfo.Pass2encPicCnt + LOOK_AHEAD_RC_DATA_SIZE > ctx->frameIdx)) {
  492. if (in) in->reuse = TRUE;
  493. if (out) out->reuse = TRUE;
  494. return TRUE;
  495. }
  496. }
  497. if ( ctx->larcPass == LOOK_AHEAD_RC_PASS2 ) {
  498. ret = ComponentGetParameter(com, com->srcPort.connectedComponent, GET_PARAM_ENC_LARC_INFO, &ctx->larcInfo);
  499. if (ComponentParamReturnTest(ret, &success) == FALSE) {
  500. return success;
  501. }
  502. if ( !(ctx->larcInfo.Pass1encPicCnt > ctx->frameIdx + ctx->larcSize) && ctx->larcInfo.larcPass1Finish == FALSE) {
  503. if (in) in->reuse = TRUE;
  504. if (out) out->reuse = TRUE;
  505. return TRUE;
  506. }
  507. for (i = 0; i < 3; i++) {
  508. if ( ctx->larcInfo.larcPass1Finish == TRUE && ctx->larcInfo.Pass1encPicCnt <= ctx->frameIdx + ctx->larcSize - 1) {//-larcSize-1:already set in Wave5VpuEncSetLarcData
  509. encParam->larcData[i] = -1;
  510. break;
  511. }
  512. else
  513. encParam->larcData[i] = ctx->larcInfo.larcData[(ctx->frameIdx+ctx->larcSize-1) % LOOK_AHEAD_RC_DATA_SIZE][i];
  514. }
  515. }
  516. #endif
  517. ctx->stateDoing = TRUE;
  518. if (out) {
  519. if (out->buf.phys_addr != 0) {
  520. Queue_Enqueue(ctx->encOutQ, (void*)&out->buf);
  521. out->buf.phys_addr = 0;
  522. out->buf.size = 0;
  523. }
  524. if (encParam->srcEndFlag == TRUE) {
  525. doEncode = (BOOL)(Queue_Get_Cnt(ctx->encOutQ) > 0);
  526. }
  527. }
  528. if (in) {
  529. if (Queue_Get_Cnt(ctx->encOutQ) > 0) {
  530. SetEncPicParam(com, in, encParam);
  531. doEncode = TRUE;
  532. in->prevMapReuse = TRUE;
  533. }
  534. }
  535. if (TRUE == com->pause)
  536. doEncode = FALSE;
  537. VPU_EncGiveCommand(ctx->handle, ENC_GET_QUEUE_STATUS, &qStatus);
  538. if (COMMAND_QUEUE_DEPTH == qStatus.instanceQueueCount) {
  539. doEncode = FALSE;
  540. }
  541. if ((ctx->testEncConfig.numChangeParam > ctx->changedCount) &&
  542. (ctx->testEncConfig.changeParam[ctx->changedCount].setParaChgFrmNum == ctx->frameIdx)) {
  543. doChangeParam = TRUE;
  544. }
  545. if (doChangeParam == TRUE) {
  546. result = SetChangeParam(ctx->handle, ctx->testEncConfig, ctx->encOpenParam, ctx->changedCount);
  547. if (result == RETCODE_SUCCESS) {
  548. VLOG(TRACE, "ENC_SET_PARA_CHANGE queue success\n");
  549. ctx->changedCount++;
  550. }
  551. else if (result == RETCODE_QUEUEING_FAILURE) { // Just retry
  552. VLOG(INFO, "ENC_SET_PARA_CHANGE Queue Full\n");
  553. doEncode = FALSE;
  554. }
  555. else { // Error
  556. VLOG(ERR, "VPU_EncGiveCommand[ENC_SET_PARA_CHANGE] failed Error code is 0x%x \n", result);
  557. ChekcAndPrintDebugInfo(ctx->handle, TRUE, result);
  558. return FALSE;
  559. }
  560. }
  561. /* The simple load balancer : To use this function, call InitLoadBalancer() before decoding process. */
  562. if (TRUE == doEncode) {
  563. if (in) in->reuse = TRUE;
  564. doEncode = LoadBalancerGetMyTurn(ctx->handle->instIndex);
  565. }
  566. if (doEncode == TRUE) {
  567. CNMComListenerEncStartOneFrame lsn;
  568. result = VPU_EncStartOneFrame(ctx->handle, encParam);
  569. if (result == RETCODE_SUCCESS) {
  570. /* The simple load balancer : pass its turn */
  571. LoadBalancerSetNextTurn();
  572. if (in) in->prevMapReuse = FALSE;
  573. Queue_Dequeue(ctx->encOutQ);
  574. ctx->frameIdx++;
  575. if (in) in->reuse = FALSE;
  576. if (out) out->reuse = FALSE;
  577. }
  578. else if (result == RETCODE_QUEUEING_FAILURE) { // Just retry
  579. // Just retry
  580. VPU_EncGiveCommand(ctx->handle, ENC_GET_QUEUE_STATUS, (void*)&qStatus);
  581. if (qStatus.instanceQueueCount == 0) {
  582. VLOG(ERR, "<%s:%d> The queue is empty but it can't add a command\n", __FUNCTION__, __LINE__);
  583. return FALSE;
  584. }
  585. }
  586. else { // Error
  587. VLOG(ERR, "VPU_EncStartOneFrame failed Error code is 0x%x \n", result);
  588. ChekcAndPrintDebugInfo(ctx->handle, TRUE, result);
  589. CNMErrorSet(CNM_ERROR_HANGUP);
  590. HandleEncoderError(ctx->handle, ctx->frameIdx, NULL);
  591. return FALSE;
  592. }
  593. lsn.handle = ctx->handle;
  594. lsn.result = result;
  595. ComponentNotifyListeners(com, COMPONENT_EVENT_ENC_START_ONE_FRAME, (void*)&lsn);
  596. #ifdef SUPPORT_TESTCASE_CQ_16
  597. if (lsn.result == COMMAND_QUEUE_NOT_FULL) {
  598. if (out) {
  599. out->size = 0;
  600. out->reuse = FALSE;
  601. }
  602. return TRUE; /* Try again */
  603. }
  604. #endif
  605. }
  606. if ((intStatus=HandlingInterruptFlag(com)) == ENC_INT_STATUS_TIMEOUT) {
  607. HandleEncoderError(ctx->handle, ctx->frameIdx, NULL);
  608. VPU_SWReset(ctx->testEncConfig.coreIdx, SW_RESET_SAFETY, ctx->handle);
  609. return FALSE;
  610. }
  611. else if (intStatus == ENC_INT_STATUS_FULL || intStatus == ENC_INT_STATUS_LOW_LATENCY) {
  612. CNMComListenerEncFull lsnpFull;
  613. PhysicalAddress paRdPtr;
  614. PhysicalAddress paWrPtr;
  615. int size;
  616. encWrPtrSel = (intStatus==ENC_INT_STATUS_FULL) ? GET_ENC_BSBUF_FULL_WRPTR : GET_ENC_LOW_LATENCY_WRPTR;
  617. VPU_EncGiveCommand(ctx->handle, ENC_WRPTR_SEL, &encWrPtrSel);
  618. VPU_EncGetBitstreamBuffer(ctx->handle, &paRdPtr, &paWrPtr, &size);
  619. VLOG(TRACE, "<%s:%d> INT_BSBUF_FULL inst=%d, %p, %p\n", __FUNCTION__, __LINE__, ctx->handle->instIndex, paRdPtr, paWrPtr);
  620. lsnpFull.handle = ctx->handle;
  621. ComponentNotifyListeners(com, COMPONENT_EVENT_ENC_FULL_INTERRUPT, (void*)&lsnpFull);
  622. if ( out ) {
  623. if (ctx->encOpenParam.ringBufferEnable == TRUE) {
  624. out->buf.phys_addr = paRdPtr;
  625. out->buf.size = size;
  626. out->size = size;
  627. out->reuse = FALSE;
  628. out->streamBufFull = TRUE;
  629. out->rdPtr = paRdPtr;
  630. out->wrPtr = paWrPtr;
  631. out->paBsBufStart = ctx->encOpenParam.bitstreamBuffer;
  632. out->paBsBufEnd = ctx->encOpenParam.bitstreamBuffer + ctx->encOpenParam.bitstreamBufferSize;
  633. }
  634. else {
  635. if (FindEsBuffer(ctx, paRdPtr, &out->buf) == FALSE) {
  636. VLOG(ERR, "%s:%d Failed to find buffer(%p)\n", __FUNCTION__, __LINE__, paRdPtr);
  637. return FALSE;
  638. }
  639. out->size = size;
  640. out->reuse = FALSE;
  641. out->streamBufFull = TRUE;
  642. }
  643. }
  644. ctx->fullInterrupt = TRUE;
  645. return TRUE;
  646. }
  647. #ifdef SUPPORT_SOURCE_RELEASE_INTERRUPT
  648. else if (intStatus == ENC_INT_STATUS_SRC_RELEASED) {
  649. Uint32 srcBufFlag = 0;
  650. VPU_EncGiveCommand(ctx->handle, ENC_GET_SRC_BUF_FLAG, &srcBufFlag);
  651. for (i = 0; i < ctx->fbCount.srcFbNum; i++) {
  652. if ( (srcBufFlag >> i) & 0x01) {
  653. ctx->encodedSrcFrmIdxArr[i] = 1;
  654. }
  655. }
  656. return TRUE;
  657. }
  658. #endif
  659. else if (intStatus == ENC_INT_STATUS_NONE) {
  660. if (out) {
  661. out->size = 0;
  662. out->reuse = FALSE;
  663. }
  664. return TRUE; /* Try again */
  665. }
  666. ComponentNotifyListeners(com, COMPONENT_EVENT_ENC_HANDLING_INT, (void*)&lsnpHandlingInt);
  667. VPU_EncGiveCommand(ctx->handle, ENC_WRPTR_SEL, &encWrPtrSel);
  668. osal_memset(&encOutputInfo, 0x00, sizeof(EncOutputInfo));
  669. encOutputInfo.result = VPU_EncGetOutputInfo(ctx->handle, &encOutputInfo);
  670. if (encOutputInfo.result == RETCODE_REPORT_NOT_READY) {
  671. return TRUE; /* Not encoded yet */
  672. }
  673. else if (encOutputInfo.result == RETCODE_VLC_BUF_FULL) {
  674. VLOG(ERR, "VLC BUFFER FULL!!! ALLOCATE MORE TASK BUFFER(%d)!!!\n", ONE_TASKBUF_SIZE_FOR_CQ);
  675. }
  676. else if (encOutputInfo.result != RETCODE_SUCCESS) {
  677. /* ERROR */
  678. VLOG(ERR, "Failed to encode error = %d, %x\n", encOutputInfo.result, encOutputInfo.errorReason);
  679. ChekcAndPrintDebugInfo(ctx->handle, TRUE, encOutputInfo.result);
  680. HandleEncoderError(ctx->handle, encOutputInfo.encPicCnt, &encOutputInfo);
  681. VPU_SWReset(ctx->testEncConfig.coreIdx, SW_RESET_SAFETY, ctx->handle);
  682. return FALSE;
  683. }
  684. else {
  685. ;/* SUCCESS */
  686. }
  687. if (encOutputInfo.reconFrameIndex == RECON_IDX_FLAG_CHANGE_PARAM) {
  688. VLOG(TRACE, "CHANGE PARAMETER!\n");
  689. return TRUE; /* Try again */
  690. }
  691. else {
  692. DisplayEncodedInformation(ctx->handle, ctx->encOpenParam.bitstreamFormat, ctx->frameIdx, &encOutputInfo, encParam->srcEndFlag, encParam->srcIdx, ctx->testEncConfig.performance);
  693. }
  694. lsnpPicDone.handle = ctx->handle;
  695. lsnpPicDone.output = &encOutputInfo;
  696. lsnpPicDone.fullInterrupted = ctx->fullInterrupt;
  697. ComponentNotifyListeners(com, COMPONENT_EVENT_ENC_GET_OUTPUT_INFO, (void*)&lsnpPicDone);
  698. if ( encOutputInfo.result != RETCODE_SUCCESS )
  699. return FALSE;
  700. for (i = 0; i < ctx->fbCount.srcFbNum; i++) {
  701. if ( (encOutputInfo.releaseSrcFlag >> i) & 0x01) {
  702. ctx->encodedSrcFrmIdxArr[i] = 1;
  703. }
  704. }
  705. ctx->fullInterrupt = FALSE;
  706. if ( out ) {
  707. if (ctx->encOpenParam.ringBufferEnable == TRUE) {
  708. out->buf.phys_addr = encOutputInfo.rdPtr;
  709. out->size = encOutputInfo.bitstreamSize;
  710. out->reuse = (BOOL)(out->size == 0);
  711. }
  712. else {
  713. if (FindEsBuffer(ctx, encOutputInfo.bitstreamBuffer, &out->buf) == FALSE) {
  714. VLOG(ERR, "%s:%d Failed to find buffer(%p)\n", __FUNCTION__, __LINE__, encOutputInfo.bitstreamBuffer);
  715. return FALSE;
  716. }
  717. out->size = encOutputInfo.bitstreamSize;
  718. out->reuse = (BOOL)(out->size == 0);
  719. }
  720. }
  721. // Finished encoding a frame
  722. if (encOutputInfo.reconFrameIndex == RECON_IDX_FLAG_ENC_END) {
  723. if (ctx->testEncConfig.outNum != encOutputInfo.encPicCnt && ctx->testEncConfig.outNum != -1) {
  724. VLOG(ERR, "outnum(%d) != encoded cnt(%d)\n", ctx->testEncConfig.outNum, encOutputInfo.encPicCnt);
  725. return FALSE;
  726. }
  727. #ifdef SUPPORT_LOOK_AHEAD_RC
  728. if (TRUE == ctx->larcEnable && ctx->larcPass == LOOK_AHEAD_RC_PASS1) {
  729. //no terminate here. PASS2 will terminate PASS1 component
  730. ctx->larcPass1Finish = TRUE;
  731. if (out) out->reuse = FALSE;
  732. ctx->stateDoing = FALSE;
  733. }
  734. else {
  735. #endif
  736. if(out) out->last = TRUE; // Send finish signal
  737. if(out) out->reuse = FALSE;
  738. ctx->stateDoing = FALSE;
  739. com->terminate = TRUE;
  740. #ifdef SUPPORT_LOOK_AHEAD_RC
  741. }
  742. #endif
  743. //to read remain data
  744. if (ctx->encOpenParam.ringBufferEnable == TRUE && ctx->encOpenParam.ringBufferWrapEnable == FALSE) {
  745. if(out) out->rdPtr = encOutputInfo.rdPtr;
  746. if(out) out->wrPtr = encOutputInfo.wrPtr;
  747. if(out) out->size = encOutputInfo.wrPtr - encOutputInfo.rdPtr;
  748. }
  749. }
  750. #ifdef SUPPORT_LOOK_AHEAD_RC
  751. ctx->encPicCnt = encOutputInfo.encPicCnt;
  752. if (ctx->larcPass == LOOK_AHEAD_RC_PASS1
  753. && encOutputInfo.reconFrameIndex != RECON_IDX_FLAG_HEADER_ONLY
  754. && encOutputInfo.bitstreamSize > 0) {
  755. for (i = 0; i < 3; i++) {
  756. ctx->larcInfo.larcData[(ctx->encPicCnt-1) % LOOK_AHEAD_RC_DATA_SIZE][i] = encOutputInfo.larcData[i];
  757. }
  758. }
  759. #endif
  760. return TRUE;
  761. }
  762. static BOOL AllocateCustomBuffer(EncHandle handle, ComponentImpl* com)
  763. {
  764. EncoderContext* ctx = (EncoderContext*)com->context;
  765. TestEncConfig testEncConfig = ctx->testEncConfig;
  766. EncInfo* pEncInfo;
  767. EncOpenParam* pOpenParam;
  768. EncWaveParam* pParam;
  769. pEncInfo = &handle->CodecInfo->encInfo;
  770. pOpenParam = &pEncInfo->openParam;
  771. pParam = &pOpenParam->EncStdParam.waveParam;
  772. /* Allocate Buffer and Set Data */
  773. if (pParam->scalingListEnable) {
  774. ctx->vbScalingList.size = 0x1000;
  775. if (vdi_allocate_dma_memory(testEncConfig.coreIdx, &ctx->vbScalingList, ENC_ETC, ctx->handle->instIndex) < 0) {
  776. VLOG(ERR, "fail to allocate scaling list buffer\n");
  777. return FALSE;
  778. }
  779. pParam->userScalingListAddr = ctx->vbScalingList.phys_addr;
  780. parse_user_scaling_list(&ctx->scalingList, testEncConfig.scaling_list_file, testEncConfig.stdMode);
  781. vdi_write_memory(testEncConfig.coreIdx, ctx->vbScalingList.phys_addr, (unsigned char*)&ctx->scalingList, ctx->vbScalingList.size, VDI_LITTLE_ENDIAN);
  782. }
  783. if (pParam->customLambdaEnable) {
  784. ctx->vbCustomLambda.size = 0x200;
  785. if (vdi_allocate_dma_memory(testEncConfig.coreIdx, &ctx->vbCustomLambda, ENC_ETC, ctx->handle->instIndex) < 0) {
  786. VLOG(ERR, "fail to allocate Lambda map buffer\n");
  787. return FALSE;
  788. }
  789. pParam->customLambdaAddr = ctx->vbCustomLambda.phys_addr;
  790. parse_custom_lambda(ctx->customLambda, testEncConfig.custom_lambda_file);
  791. vdi_write_memory(testEncConfig.coreIdx, ctx->vbCustomLambda.phys_addr, (unsigned char*)&ctx->customLambda[0], ctx->vbCustomLambda.size, VDI_LITTLE_ENDIAN);
  792. }
  793. return TRUE;
  794. }
  795. static BOOL OpenEncoder(ComponentImpl* com)
  796. {
  797. EncoderContext* ctx = (EncoderContext*)com->context;
  798. SecAxiUse secAxiUse;
  799. MirrorDirection mirrorDirection;
  800. RetCode result;
  801. CNMComListenerEncOpen lspn = {0};
  802. ctx->stateDoing = TRUE;
  803. ctx->encOpenParam.bitstreamBuffer = ctx->bsBuf.bs[0].phys_addr;
  804. ctx->encOpenParam.bitstreamBufferSize = ctx->bsBuf.bs[0].size;
  805. if ((result = VPU_EncOpen(&ctx->handle, &ctx->encOpenParam)) != RETCODE_SUCCESS) {
  806. VLOG(ERR, "VPU_EncOpen failed Error code is 0x%x \n", result);
  807. if ( result == RETCODE_VPU_RESPONSE_TIMEOUT ) {
  808. CNMErrorSet(CNM_ERROR_HANGUP);
  809. }
  810. CNMAppStop();
  811. return FALSE;
  812. }
  813. //VPU_EncGiveCommand(ctx->handle, ENABLE_LOGGING, 0);
  814. LoadBalancerAddInstance(ctx->handle->instIndex);
  815. if (AllocateCustomBuffer(ctx->handle, com) == FALSE) return FALSE;
  816. lspn.handle = ctx->handle;
  817. ComponentNotifyListeners(com, COMPONENT_EVENT_ENC_OPEN, (void*)&lspn);
  818. if (ctx->testEncConfig.rotAngle != 0 || ctx->testEncConfig.mirDir != 0) {
  819. VPU_EncGiveCommand(ctx->handle, ENABLE_ROTATION, 0);
  820. VPU_EncGiveCommand(ctx->handle, ENABLE_MIRRORING, 0);
  821. VPU_EncGiveCommand(ctx->handle, SET_ROTATION_ANGLE, &ctx->testEncConfig.rotAngle);
  822. mirrorDirection = (MirrorDirection)ctx->testEncConfig.mirDir;
  823. VPU_EncGiveCommand(ctx->handle, SET_MIRROR_DIRECTION, &mirrorDirection);
  824. }
  825. osal_memset(&secAxiUse, 0x00, sizeof(SecAxiUse));
  826. secAxiUse.u.wave.useEncRdoEnable = (ctx->testEncConfig.secondaryAXI & 0x1) ? TRUE : FALSE; //USE_RDO_INTERNAL_BUF
  827. secAxiUse.u.wave.useEncLfEnable = (ctx->testEncConfig.secondaryAXI & 0x2) ? TRUE : FALSE; //USE_LF_INTERNAL_BUF
  828. VPU_EncGiveCommand(ctx->handle, SET_SEC_AXI, &secAxiUse);
  829. VPU_EncGiveCommand(ctx->handle, SET_CYCLE_PER_TICK, (void*)&ctx->cyclePerTick);
  830. ctx->stateDoing = FALSE;
  831. return TRUE;
  832. }
  833. static BOOL ExecuteEncoder(ComponentImpl* com, PortContainer* in, PortContainer* out)
  834. {
  835. EncoderContext* ctx = (EncoderContext*)com->context;
  836. BOOL ret;
  837. switch (ctx->state) {
  838. case ENCODER_STATE_OPEN:
  839. ret = OpenEncoder(com);
  840. if (ctx->stateDoing == FALSE) ctx->state = ENCODER_STATE_INIT_SEQ;
  841. break;
  842. case ENCODER_STATE_INIT_SEQ:
  843. ret = SetSequenceInfo(com);
  844. if (ctx->stateDoing == FALSE) ctx->state = ENCODER_STATE_REGISTER_FB;
  845. break;
  846. case ENCODER_STATE_REGISTER_FB:
  847. ret = RegisterFrameBuffers(com);
  848. if (ctx->stateDoing == FALSE) ctx->state = ENCODER_STATE_ENCODE_HEADER;
  849. break;
  850. case ENCODER_STATE_ENCODE_HEADER:
  851. ret = EncodeHeader(com);
  852. if (ctx->stateDoing == FALSE) ctx->state = ENCODER_STATE_ENCODING;
  853. break;
  854. case ENCODER_STATE_ENCODING:
  855. ret = Encode(com, (PortContainerYuv*)in, (PortContainerES*)out);
  856. break;
  857. default:
  858. ret = FALSE;
  859. break;
  860. }
  861. if (ret == FALSE || com->terminate == TRUE) {
  862. ComponentNotifyListeners(com, COMPONENT_EVENT_ENC_ENCODED_ALL, (void*)ctx->handle);
  863. if (out != NULL) {
  864. out->reuse = FALSE;
  865. out->last = TRUE;
  866. }
  867. }
  868. return ret;
  869. }
  870. static BOOL PrepareEncoder(ComponentImpl* com, BOOL* done)
  871. {
  872. EncoderContext* ctx = (EncoderContext*)com->context;
  873. TestEncConfig* testEncConfig = &ctx->testEncConfig;
  874. CNMComponentParamRet ret;
  875. BOOL success;
  876. Uint32 i;
  877. *done = FALSE;
  878. ret = ComponentGetParameter(com, com->sinkPort.connectedComponent, GET_PARAM_READER_BITSTREAM_BUF, &ctx->bsBuf);
  879. if (ComponentParamReturnTest(ret, &success) == FALSE) return success;
  880. if (ctx->encOpenParam.ringBufferEnable == TRUE) {
  881. ctx->encOutQ = Queue_Create(com->numSinkPortQueue, sizeof(vpu_buffer_t));
  882. for (i=0; i<com->numSinkPortQueue; i++) {
  883. if ( i < ctx->bsBuf.num) {
  884. ctx->bsBuffer[i] = &ctx->bsBuf.bs[i]; }
  885. else {
  886. ctx->bsBuffer[i] = osal_malloc(sizeof(vpu_buffer_t));
  887. osal_memcpy(ctx->bsBuffer[i], ctx->bsBuffer[0], sizeof(vpu_buffer_t));
  888. }
  889. Queue_Enqueue(ctx->encOutQ, (void*)&ctx->bsBuffer[i]);//same addr enqueue
  890. }
  891. } else {
  892. ctx->encOutQ = Queue_Create(ctx->bsBuf.num, sizeof(vpu_buffer_t));
  893. for (i=0; i<ctx->bsBuf.num; i++) {
  894. Queue_Enqueue(ctx->encOutQ, (void*)&ctx->bsBuf.bs[i]);
  895. }
  896. }
  897. /* Open Data File*/
  898. if (ctx->encOpenParam.EncStdParam.waveParam.scalingListEnable) {
  899. if (testEncConfig->scaling_list_fileName) {
  900. ChangePathStyle(testEncConfig->scaling_list_fileName);
  901. if ((testEncConfig->scaling_list_file = osal_fopen(testEncConfig->scaling_list_fileName, "r")) == NULL) {
  902. VLOG(ERR, "fail to open scaling list file, %s\n", testEncConfig->scaling_list_fileName);
  903. return FALSE;
  904. }
  905. }
  906. }
  907. if (ctx->encOpenParam.EncStdParam.waveParam.customLambdaEnable) {
  908. if (testEncConfig->custom_lambda_fileName) {
  909. ChangePathStyle(testEncConfig->custom_lambda_fileName);
  910. if ((testEncConfig->custom_lambda_file = osal_fopen(testEncConfig->custom_lambda_fileName, "r")) == NULL) {
  911. VLOG(ERR, "fail to open custom lambda file, %s\n", testEncConfig->custom_lambda_fileName);
  912. return FALSE;
  913. }
  914. }
  915. }
  916. if (testEncConfig->roi_enable) {
  917. if (testEncConfig->roi_file_name) {
  918. ChangePathStyle(testEncConfig->roi_file_name);
  919. if ((testEncConfig->roi_file = osal_fopen(testEncConfig->roi_file_name, "r")) == NULL) {
  920. VLOG(ERR, "fail to open ROI file, %s\n", testEncConfig->roi_file_name);
  921. return FALSE;
  922. }
  923. }
  924. }
  925. if (testEncConfig->lambda_map_enable) {
  926. if (testEncConfig->lambda_map_fileName) {
  927. ChangePathStyle(testEncConfig->lambda_map_fileName);
  928. if ((testEncConfig->lambda_map_file = osal_fopen(testEncConfig->lambda_map_fileName, "r")) == NULL) {
  929. VLOG(ERR, "fail to open lambda map file, %s\n", testEncConfig->lambda_map_fileName);
  930. return FALSE;
  931. }
  932. }
  933. }
  934. if (testEncConfig->mode_map_flag) {
  935. if (testEncConfig->mode_map_fileName) {
  936. ChangePathStyle(testEncConfig->mode_map_fileName);
  937. if ((testEncConfig->mode_map_file = osal_fopen(testEncConfig->mode_map_fileName, "r")) == NULL) {
  938. VLOG(ERR, "fail to open custom mode map file, %s\n", testEncConfig->mode_map_fileName);
  939. return FALSE;
  940. }
  941. }
  942. }
  943. if (testEncConfig->wp_param_flag & 0x1) {
  944. if (testEncConfig->wp_param_fileName) {
  945. ChangePathStyle(testEncConfig->wp_param_fileName);
  946. if ((testEncConfig->wp_param_file = osal_fopen(testEncConfig->wp_param_fileName, "r")) == NULL) {
  947. VLOG(ERR, "fail to open Weight Param file, %s\n", testEncConfig->wp_param_fileName);
  948. return FALSE;
  949. }
  950. }
  951. }
  952. *done = TRUE;
  953. return TRUE;
  954. }
  955. static void ReleaseEncoder(ComponentImpl* com)
  956. {
  957. // Nothing to do
  958. }
  959. static BOOL DestroyEncoder(ComponentImpl* com)
  960. {
  961. EncoderContext* ctx = (EncoderContext*)com->context;
  962. Uint32 i = 0;
  963. BOOL success = TRUE;
  964. ENC_INT_STATUS intStatus;
  965. if ( NULL == ctx )
  966. return FALSE;
  967. if ( ctx && ctx->handle) {
  968. LoadBalancerRemoveInstance(ctx->handle->instIndex);
  969. }
  970. while (VPU_EncClose(ctx->handle) == RETCODE_VPU_STILL_RUNNING) {
  971. if ((intStatus = HandlingInterruptFlag(com)) == ENC_INT_STATUS_TIMEOUT) {
  972. HandleEncoderError(ctx->handle, ctx->frameIdx, NULL);
  973. VLOG(ERR, "NO RESPONSE FROM VPU_EncClose2()\n");
  974. success = FALSE;
  975. break;
  976. }
  977. else if (intStatus == ENC_INT_STATUS_DONE) {
  978. EncOutputInfo outputInfo;
  979. VLOG(INFO, "VPU_EncClose() : CLEAR REMAIN INTERRUPT\n");
  980. VPU_EncGetOutputInfo(ctx->handle, &outputInfo);
  981. continue;
  982. }
  983. osal_msleep(10);
  984. }
  985. ComponentNotifyListeners(com, COMPONENT_EVENT_ENC_CLOSE, NULL);
  986. for (i = 0; i < ctx->fbCount.srcFbNum; i++) {
  987. if (ctx->vbCustomMap[i].size)
  988. vdi_free_dma_memory(ctx->testEncConfig.coreIdx, &ctx->vbCustomMap[i], ENC_ETC, ctx->handle->instIndex);
  989. }
  990. if (ctx->vbCustomLambda.size)
  991. vdi_free_dma_memory(ctx->testEncConfig.coreIdx, &ctx->vbCustomLambda, ENC_ETC, ctx->handle->instIndex);
  992. if (ctx->vbScalingList.size)
  993. vdi_free_dma_memory(ctx->testEncConfig.coreIdx, &ctx->vbScalingList, ENC_ETC, ctx->handle->instIndex);
  994. if (ctx->testEncConfig.roi_file)
  995. osal_fclose(ctx->testEncConfig.roi_file);
  996. if (ctx->testEncConfig.lambda_map_file)
  997. osal_fclose(ctx->testEncConfig.lambda_map_file);
  998. if (ctx->testEncConfig.mode_map_file)
  999. osal_fclose(ctx->testEncConfig.mode_map_file);
  1000. if (ctx->testEncConfig.scaling_list_file)
  1001. osal_fclose(ctx->testEncConfig.scaling_list_file);
  1002. if (ctx->testEncConfig.custom_lambda_file)
  1003. osal_fclose(ctx->testEncConfig.custom_lambda_file);
  1004. if (ctx->testEncConfig.wp_param_file)
  1005. osal_fclose(ctx->testEncConfig.wp_param_file);
  1006. if (ctx->encOutQ) Queue_Destroy(ctx->encOutQ);
  1007. VPU_DeInit(ctx->testEncConfig.coreIdx);
  1008. osal_free(ctx);
  1009. return success;
  1010. }
  1011. static Component CreateEncoder(ComponentImpl* com, CNMComponentConfig* componentParam)
  1012. {
  1013. EncoderContext* ctx;
  1014. RetCode retCode;
  1015. Uint32 coreIdx = componentParam->testEncConfig.coreIdx;
  1016. Uint16* firmware = (Uint16*)componentParam->bitcode;
  1017. Uint32 firmwareSize = componentParam->sizeOfBitcode;
  1018. Uint32 i;
  1019. VpuAttr productInfo;
  1020. retCode = VPU_InitWithBitcode(coreIdx, firmware, firmwareSize);
  1021. if (retCode != RETCODE_SUCCESS && retCode != RETCODE_CALLED_BEFORE) {
  1022. VLOG(INFO, "%s:%d Failed to VPU_InitWidthBitCode, ret(%08x)\n", __FUNCTION__, __LINE__, retCode);
  1023. return FALSE;
  1024. }
  1025. com->context = osal_malloc(sizeof(EncoderContext));
  1026. ctx = (EncoderContext*)com->context;
  1027. osal_memset((void*)ctx, 0, sizeof(EncoderContext));
  1028. retCode = PrintVpuProductInfo(coreIdx, &productInfo);
  1029. if (retCode == RETCODE_VPU_RESPONSE_TIMEOUT ) {
  1030. CNMErrorSet(CNM_ERROR_HANGUP);
  1031. VLOG(INFO, "<%s:%d> Failed to PrintVpuProductInfo()\n", __FUNCTION__, __LINE__);
  1032. HandleEncoderError(ctx->handle, 0, NULL);
  1033. return FALSE;
  1034. }
  1035. ctx->cyclePerTick = 32768;
  1036. if (TRUE == productInfo.supportNewTimer)
  1037. ctx->cyclePerTick = 256;
  1038. ctx->handle = NULL;
  1039. ctx->frameIdx = 0;
  1040. ctx->fbCount.reconFbNum = 0;
  1041. ctx->fbCount.srcFbNum = 0;
  1042. ctx->testEncConfig = componentParam->testEncConfig;
  1043. ctx->encOpenParam = componentParam->encOpenParam;
  1044. for (i=0; i<ENC_SRC_BUF_NUM ; i++ ) {
  1045. ctx->encodedSrcFrmIdxArr[i] = 0;
  1046. }
  1047. osal_memset(&ctx->vbCustomLambda, 0x00, sizeof(vpu_buffer_t));
  1048. osal_memset(&ctx->vbScalingList, 0x00, sizeof(vpu_buffer_t));
  1049. osal_memset(&ctx->scalingList, 0x00, sizeof(UserScalingList));
  1050. osal_memset(&ctx->customLambda[0], 0x00, sizeof(ctx->customLambda));
  1051. osal_memset(ctx->vbCustomMap, 0x00, sizeof(ctx->vbCustomMap));
  1052. if (ctx->encOpenParam.ringBufferEnable)
  1053. com->numSinkPortQueue = 10;
  1054. else
  1055. com->numSinkPortQueue = componentParam->encOpenParam.streamBufCount;
  1056. #ifdef SUPPORT_LOOK_AHEAD_RC
  1057. ctx->larcEnable = ctx->encOpenParam.EncStdParam.waveParam.larcEnable;
  1058. if (TRUE == ctx->larcEnable) {
  1059. ctx->larcPass = ctx->encOpenParam.EncStdParam.waveParam.larcPass;
  1060. ctx->larcSize = ctx->encOpenParam.EncStdParam.waveParam.larcSize;
  1061. osal_memset((void*)ctx->larcInfo.larcData, -1, sizeof(ctx->larcInfo.larcData));
  1062. }
  1063. #endif
  1064. return (Component)com;
  1065. }
  1066. ComponentImpl waveEncoderComponentImpl = {
  1067. "wave_encoder",
  1068. NULL,
  1069. {0,},
  1070. {0,},
  1071. sizeof(PortContainerES),
  1072. 5, /* encoder's numSinkPortQueue(relates to streambufcount) */
  1073. CreateEncoder,
  1074. GetParameterEncoder,
  1075. SetParameterEncoder,
  1076. PrepareEncoder,
  1077. ExecuteEncoder,
  1078. ReleaseEncoder,
  1079. DestroyEncoder
  1080. };