History

add decoder cabac support and add UT

dong zhang committed 12 years ago

Commit cf857451 parent c7b505132 files · +2615 −470Browse files

Showing the changes to codec/decoder/core/src/decode_slice.cpp (1 of 32 files). Show all

Unified · Split

codec/decoder/core/src/decode_slice.cpp+696−96
@@ -44,6 +44,7 @@ · 43 unmodified lines
4444 #include "decode_slice.h"
4545
4646 #include "parse_mb_syn_cavlc.h"
47+#include "parse_mb_syn_cabac.h"
4748 #include "rec_mb.h"
4849 #include "mv_pred.h"
4950
@@ -318,6 +319,576 @@ void WelsChromaDcIdct (int16_t* pBlock) { · 268 unmodified lines
318319 pBlk[iStride1] = (iE - iB) >> 1;
319320 }
320321
322+void WelsMap4x4NeighToSampleNormal (PWelsNeighAvail pNeighAvail, int32_t* pSampleAvail) {
323+ if (pNeighAvail->iLeftAvail) { //left
324+ pSampleAvail[ 6] =
325+ pSampleAvail[12] =
326+ pSampleAvail[18] =
327+ pSampleAvail[24] = 1;
328+ }
329+ if (pNeighAvail->iLeftTopAvail) { //top_left
330+ pSampleAvail[0] = 1;
331+ }
332+ if (pNeighAvail->iTopAvail) { //top
333+ pSampleAvail[1] =
334+ pSampleAvail[2] =
335+ pSampleAvail[3] =
336+ pSampleAvail[4] = 1;
337+ }
338+ if (pNeighAvail->iRightTopAvail) { //top_right
339+ pSampleAvail[5] = 1;
340+ }
341+}
342+
343+void WelsMap4x4NeighToSampleConstrain1 (PWelsNeighAvail pNeighAvail, int32_t* pSampleAvail) {
344+ if (pNeighAvail->iLeftAvail && IS_INTRA (pNeighAvail->iLeftType)) { //left
345+ pSampleAvail[ 6] =
346+ pSampleAvail[12] =
347+ pSampleAvail[18] =
348+ pSampleAvail[24] = 1;
349+ }
350+ if (pNeighAvail->iLeftTopAvail && IS_INTRA (pNeighAvail->iLeftTopType)) { //top_left
351+ pSampleAvail[0] = 1;
352+ }
353+ if (pNeighAvail->iTopAvail && IS_INTRA (pNeighAvail->iTopType)) { //top
354+ pSampleAvail[1] =
355+ pSampleAvail[2] =
356+ pSampleAvail[3] =
357+ pSampleAvail[4] = 1;
358+ }
359+ if (pNeighAvail->iRightTopAvail && IS_INTRA (pNeighAvail->iRightTopType)) { //top_right
360+ pSampleAvail[5] = 1;
361+ }
362+}
363+void WelsMap16x16NeighToSampleNormal (PWelsNeighAvail pNeighAvail, uint8_t* pSampleAvail) {
364+ if (pNeighAvail->iLeftAvail) {
365+ *pSampleAvail = (1 << 2);
366+ }
367+ if (pNeighAvail->iLeftTopAvail) {
368+ *pSampleAvail |= (1 << 1);
369+ }
370+ if (pNeighAvail->iTopAvail) {
371+ *pSampleAvail |= 1;
372+ }
373+}
374+
375+void WelsMap16x16NeighToSampleConstrain1 (PWelsNeighAvail pNeighAvail, uint8_t* pSampleAvail) {
376+ if (pNeighAvail->iLeftAvail && IS_INTRA (pNeighAvail->iLeftType)) {
377+ *pSampleAvail = (1 << 2);
378+ }
379+ if (pNeighAvail->iLeftTopAvail && IS_INTRA (pNeighAvail->iLeftTopType)) {
380+ *pSampleAvail |= (1 << 1);
381+ }
382+ if (pNeighAvail->iTopAvail && IS_INTRA (pNeighAvail->iTopType)) {
383+ *pSampleAvail |= 1;
384+ }
385+}
386+
387+int32_t ParseIntra4x4Mode (PWelsDecoderContext pCtx, PWelsNeighAvail pNeighAvail, int8_t* pIntraPredMode,
388+ PBitStringAux pBs,
389+ PDqLayer pCurDqLayer) {
390+ int32_t iSampleAvail[5 * 6] = { 0 }; //initialize as 0
391+ int32_t iMbXy = pCurDqLayer->iMbXyIndex;
392+ int32_t iFinalMode, i;
393+
394+ uint8_t uiNeighAvail = 0;
395+ uint32_t uiCode;
396+ int32_t iCode;
397+ pCtx->pMap4x4NeighToSampleFunc (pNeighAvail, iSampleAvail);
398+ uiNeighAvail = (iSampleAvail[6] << 2) | (iSampleAvail[0] << 1) | (iSampleAvail[1]);
399+ for (i = 0; i < 16; i++) {
400+ int32_t iPrevIntra4x4PredMode = 0;
401+ if (pCurDqLayer->sLayerInfo.pPps->bEntropyCodingModeFlag) {
402+ WELS_READ_VERIFY (ParseIntraPredModeLumaCabac (pCtx, iCode));
403+ iPrevIntra4x4PredMode = iCode;
404+ } else {
405+ WELS_READ_VERIFY (BsGetOneBit (pBs, &uiCode));
406+ iPrevIntra4x4PredMode = uiCode;
407+ }
408+ const int32_t kiPredMode = PredIntra4x4Mode (pIntraPredMode, i);
409+
410+ int8_t iBestMode;
411+ if (pCurDqLayer->sLayerInfo.pPps->bEntropyCodingModeFlag) {
412+ if (iPrevIntra4x4PredMode == -1)
413+ iBestMode = kiPredMode;
414+ else
415+ iBestMode = iPrevIntra4x4PredMode + (iPrevIntra4x4PredMode >= kiPredMode);
416+ } else {
417+ if (iPrevIntra4x4PredMode) {
418+ iBestMode = kiPredMode;
419+ } else {
420+ WELS_READ_VERIFY (BsGetBits (pBs, 3, &uiCode));
421+ iBestMode = uiCode + (uiCode >= kiPredMode);
422+ }
423+ }
424+
425+ iFinalMode = CheckIntra4x4PredMode (&iSampleAvail[0], &iBestMode, i);
426+ if (iFinalMode == ERR_INVALID_INTRA4X4_MODE) {
427+ return ERR_INFO_INVALID_I4x4_PRED_MODE;
428+ }
429+
430+ pCurDqLayer->pIntra4x4FinalMode[iMbXy][g_kuiScan4[i]] = iFinalMode;
431+
432+ pIntraPredMode[g_kuiScan8[i]] = iBestMode;
433+
434+ iSampleAvail[g_kuiCache30ScanIdx[i]] = 1;
435+ }
436+ ST32 (&pCurDqLayer->pIntraPredMode[iMbXy][0], LD32 (&pIntraPredMode[1 + 8 * 4]));
437+ pCurDqLayer->pIntraPredMode[iMbXy][4] = pIntraPredMode[4 + 8 * 1];
438+ pCurDqLayer->pIntraPredMode[iMbXy][5] = pIntraPredMode[4 + 8 * 2];
439+ pCurDqLayer->pIntraPredMode[iMbXy][6] = pIntraPredMode[4 + 8 * 3];
440+ if (pCurDqLayer->sLayerInfo.pPps->bEntropyCodingModeFlag) {
441+ WELS_READ_VERIFY (ParseIntraPredModeChromaCabac (pCtx, uiNeighAvail, iCode));
442+ if (iCode > MAX_PRED_MODE_ID_CHROMA) {
443+ return ERR_INFO_INVALID_I_CHROMA_PRED_MODE;
444+ }
445+ pCurDqLayer->pChromaPredMode[iMbXy] = iCode;
446+ } else {
447+ WELS_READ_VERIFY (BsGetUe (pBs, &uiCode)); //intra_chroma_pred_mode
448+ if (uiCode > MAX_PRED_MODE_ID_CHROMA) {
449+ return ERR_INFO_INVALID_I_CHROMA_PRED_MODE;
450+ }
451+ pCurDqLayer->pChromaPredMode[iMbXy] = uiCode;
452+ }
453+
454+ if (-1 == pCurDqLayer->pChromaPredMode[iMbXy]
455+ || CheckIntraChromaPredMode (uiNeighAvail, &pCurDqLayer->pChromaPredMode[iMbXy])) {
456+ return ERR_INFO_INVALID_I_CHROMA_PRED_MODE;
457+ }
458+
459+ return ERR_NONE;
460+}
461+
462+int32_t ParseIntra16x16Mode (PWelsDecoderContext pCtx, PWelsNeighAvail pNeighAvail, PBitStringAux pBs,
463+ PDqLayer pCurDqLayer) {
464+ int32_t iMbXy = pCurDqLayer->iMbXyIndex;
465+ uint8_t uiNeighAvail = 0; //0x07 = 0 1 1 1, means left, top-left, top avail or not. (1: avail, 0: unavail)
466+ uint32_t uiCode;
467+ int32_t iCode;
468+ pCtx->pMap16x16NeighToSampleFunc (pNeighAvail, &uiNeighAvail);
469+
470+ if (CheckIntra16x16PredMode (uiNeighAvail,
471+ &pCurDqLayer->pIntraPredMode[iMbXy][7])) { //invalid iPredMode, must stop decoding
472+ return ERR_INFO_INVALID_I16x16_PRED_MODE;
473+ }
474+ if (pCurDqLayer->sLayerInfo.pPps->bEntropyCodingModeFlag) {
475+ WELS_READ_VERIFY (ParseIntraPredModeChromaCabac (pCtx, uiNeighAvail, iCode));
476+ if (iCode > MAX_PRED_MODE_ID_CHROMA) {
477+ return ERR_INFO_INVALID_I_CHROMA_PRED_MODE;
478+ }
479+ pCurDqLayer->pChromaPredMode[iMbXy] = iCode;
480+ } else {
481+ WELS_READ_VERIFY (BsGetUe (pBs, &uiCode)); //intra_chroma_pred_mode
482+ if (uiCode > MAX_PRED_MODE_ID_CHROMA) {
483+ return ERR_INFO_INVALID_I_CHROMA_PRED_MODE;
484+ }
485+ pCurDqLayer->pChromaPredMode[iMbXy] = uiCode;
486+ }
487+ if (-1 == pCurDqLayer->pChromaPredMode[iMbXy]
488+ || CheckIntraChromaPredMode (uiNeighAvail, &pCurDqLayer->pChromaPredMode[iMbXy])) {
489+ return ERR_INFO_INVALID_I_CHROMA_PRED_MODE;
490+ }
491+
492+ return ERR_NONE;
493+}
494+
495+int32_t WelsDecodeMbCabacISliceBaseMode0 (PWelsDecoderContext pCtx, uint32_t& uiEosFlag) {
496+ PDqLayer pCurLayer = pCtx->pCurDqLayer;
497+ PBitStringAux pBsAux = pCurLayer->pBitStringAux;
498+ PSlice pSlice = &pCurLayer->sLayerInfo.sSliceInLayer;
499+ PSliceHeader pSliceHeader = &pSlice->sSliceHeaderExt.sSliceHeader;
500+ SWelsNeighAvail sNeighAvail;
501+ int32_t iScanIdxStart = pSlice->sSliceHeaderExt.uiScanIdxStart;
502+ int32_t iScanIdxEnd = pSlice->sSliceHeaderExt.uiScanIdxEnd;
503+ int32_t iMbXy = pCurLayer->iMbXyIndex;
504+ int32_t iMbMode, i;
505+ uint32_t uiMbType = 0, uiCbp = 0, uiCbpLuma = 0, uiCbpChroma = 0;
506+
507+ ENFORCE_STACK_ALIGN_1D (uint8_t, pNonZeroCount, 48, 16);
508+
509+ pCurLayer->pInterPredictionDoneFlag[iMbXy] = 0;
510+ pCurLayer->pResidualPredFlag[iMbXy] = pSlice->sSliceHeaderExt.bDefaultResidualPredFlag;
511+ GetNeighborAvailMbType (&sNeighAvail, pCurLayer);
512+ WELS_READ_VERIFY (ParseMBTypeISliceCabac (pCtx, &sNeighAvail, uiMbType));
513+ if (uiMbType > 25) {
514+ return ERR_INFO_INVALID_MB_TYPE;
515+ } else if (25 == uiMbType) { //I_PCM
516+ WELS_READ_VERIFY (ParseIPCMInfoCabac (pCtx));
517+ WELS_READ_VERIFY (ParseEndOfSliceCabac (pCtx, uiEosFlag));
518+ if (uiEosFlag) {
519+ RestoreCabacDecEngineToBS (pCtx->pCabacDecEngine, pCtx->pCurDqLayer->pBitStringAux);
520+ }
521+ return ERR_NONE;
522+ } else if (0 == uiMbType) { //I4x4
523+ ENFORCE_STACK_ALIGN_1D (int8_t, pIntraPredMode, 48, 16);
524+ pCurLayer->pMbType[iMbXy] = MB_TYPE_INTRA4x4;
525+ pCtx->pFillInfoCacheIntra4x4Func (&sNeighAvail, pNonZeroCount, pIntraPredMode, pCurLayer);
526+ WELS_READ_VERIFY (ParseIntra4x4Mode (pCtx, &sNeighAvail, pIntraPredMode, pBsAux, pCurLayer));
527+ //get uiCbp for I4x4
528+ WELS_READ_VERIFY (ParseCbpInfoCabac (pCtx, &sNeighAvail, uiCbp));
529+ pCurLayer->pCbp[iMbXy] = uiCbp;
530+ uiCbpChroma = uiCbp >> 4;
531+ uiCbpLuma = uiCbp & 15;
532+ } else { //I16x16;
533+ pCurLayer->pMbType[iMbXy] = MB_TYPE_INTRA16x16;
534+ pCurLayer->pIntraPredMode[iMbXy][7] = (uiMbType - 1) & 3;
535+ pCurLayer->pCbp[iMbXy] = g_kuiI16CbpTable[ (uiMbType - 1) >> 2];
536+ uiCbpChroma = pCurLayer->pCbp[iMbXy] >> 4;
537+ uiCbpLuma = pCurLayer->pCbp[iMbXy] & 15;
538+ WelsFillCacheNonZeroCount (&sNeighAvail, pNonZeroCount, pCurLayer);
539+ WELS_READ_VERIFY (ParseIntra16x16Mode (pCtx, &sNeighAvail, pBsAux, pCurLayer));
540+ }
541+ iMbMode = BASE_MB;
542+
543+ memset (pCurLayer->pScaledTCoeff[iMbXy], 0, 384 * sizeof (pCurLayer->pScaledTCoeff[iMbXy][0]));
544+ ST32 (&pCurLayer->pNzc[iMbXy][0], 0);
545+ ST32 (&pCurLayer->pNzc[iMbXy][4], 0);
546+ ST32 (&pCurLayer->pNzc[iMbXy][8], 0);
547+ ST32 (&pCurLayer->pNzc[iMbXy][12], 0);
548+ ST32 (&pCurLayer->pNzc[iMbXy][16], 0);
549+ ST32 (&pCurLayer->pNzc[iMbXy][20], 0);
550+ pCurLayer->pCbfDc[iMbXy] = 0;
551+
552+ if (pCurLayer->pCbp[iMbXy] == 0 && IS_INTRA4x4 (pCurLayer->pMbType[iMbXy])) {
553+ pCurLayer->pLumaQp[iMbXy] = pSlice->iLastMbQp;
554+ pCurLayer->pChromaQp[iMbXy] = g_kuiChromaQpTable[WELS_CLIP3 ((pCurLayer->pLumaQp[iMbXy] +
555+ pSliceHeader->pPps->iChromaQpIndexOffset), 0, 51)];
556+ }
557+
558+ if (pCurLayer->pCbp[iMbXy] || MB_TYPE_INTRA16x16 == pCurLayer->pMbType[iMbXy]) {
559+ int32_t iQpDelta, iId8x8, iId4x4;
560+ WELS_READ_VERIFY (ParseDeltaQpCabac (pCtx, iQpDelta));
561+ if (iQpDelta > 25 || iQpDelta < -26) {//out of iQpDelta range
562+ return ERR_INFO_INVALID_QP;
563+ }
564+ pCurLayer->pLumaQp[iMbXy] = (pSlice->iLastMbQp + iQpDelta + 52) % 52; //update last_mb_qp
565+ pSlice->iLastMbQp = pCurLayer->pLumaQp[iMbXy];
566+ pCurLayer->pChromaQp[iMbXy] = g_kuiChromaQpTable[WELS_CLIP3 ((pSlice->iLastMbQp +
567+ pSliceHeader->pPps->iChromaQpIndexOffset), 0, 51)];
568+ if (MB_TYPE_INTRA16x16 == pCurLayer->pMbType[iMbXy]) {
569+ //step1: Luma DC
570+ WELS_READ_VERIFY (ParseResidualBlockCabac (&sNeighAvail, pNonZeroCount, pBsAux, 0, 16, g_kuiLumaDcZigzagScan,
571+ I16_LUMA_DC, pCurLayer->pScaledTCoeff[iMbXy], pCurLayer->pLumaQp[iMbXy], pCtx));
572+ //step2: Luma AC
573+ if (uiCbpLuma) {
574+ for (i = 0; i < 16; i++) {
575+ WELS_READ_VERIFY (ParseResidualBlockCabac (&sNeighAvail, pNonZeroCount, pBsAux, i,
576+ iScanIdxEnd - WELS_MAX (iScanIdxStart, 1) + 1, g_kuiZigzagScan + WELS_MAX (iScanIdxStart, 1), I16_LUMA_AC,
577+ pCurLayer->pScaledTCoeff[iMbXy] + (i << 4), pCurLayer->pLumaQp[iMbXy], pCtx));
578+ }
579+ ST32 (&pCurLayer->pNzc[iMbXy][0], LD32 (&pNonZeroCount[1 + 8 * 1]));
580+ ST32 (&pCurLayer->pNzc[iMbXy][4], LD32 (&pNonZeroCount[1 + 8 * 2]));
581+ ST32 (&pCurLayer->pNzc[iMbXy][8], LD32 (&pNonZeroCount[1 + 8 * 3]));
582+ ST32 (&pCurLayer->pNzc[iMbXy][12], LD32 (&pNonZeroCount[1 + 8 * 4]));
583+ } else { //pNonZeroCount = 0
584+ ST32 (&pCurLayer->pNzc[iMbXy][0], 0);
585+ ST32 (&pCurLayer->pNzc[iMbXy][4], 0);
586+ ST32 (&pCurLayer->pNzc[iMbXy][8], 0);
587+ ST32 (&pCurLayer->pNzc[iMbXy][12], 0);
588+ }
589+ } else { //non-MB_TYPE_INTRA16x16
590+ for (iId8x8 = 0; iId8x8 < 4; iId8x8++) {
591+ if (uiCbpLuma & (1 << iId8x8)) {
592+ int32_t iIdx = (iId8x8 << 2);
593+ for (iId4x4 = 0; iId4x4 < 4; iId4x4++) {
594+ //Luma (DC and AC decoding together)
595+ WELS_READ_VERIFY (ParseResidualBlockCabac (&sNeighAvail, pNonZeroCount, pBsAux, iIdx, iScanIdxEnd - iScanIdxStart + 1,
596+ g_kuiZigzagScan + iScanIdxStart, LUMA_DC_AC, pCurLayer->pScaledTCoeff[iMbXy] + (iIdx << 4), pCurLayer->pLumaQp[iMbXy],
597+ pCtx));
598+ iIdx++;
599+ }
600+ } else {
601+ ST16 (&pNonZeroCount[g_kCacheNzcScanIdx[ (iId8x8 << 2)]], 0);
602+ ST16 (&pNonZeroCount[g_kCacheNzcScanIdx[ (iId8x8 << 2) + 2]], 0);
603+ }
604+ }
605+ ST32 (&pCurLayer->pNzc[iMbXy][0], LD32 (&pNonZeroCount[1 + 8 * 1]));
606+ ST32 (&pCurLayer->pNzc[iMbXy][4], LD32 (&pNonZeroCount[1 + 8 * 2]));
607+ ST32 (&pCurLayer->pNzc[iMbXy][8], LD32 (&pNonZeroCount[1 + 8 * 3]));
608+ ST32 (&pCurLayer->pNzc[iMbXy][12], LD32 (&pNonZeroCount[1 + 8 * 4]));
609+ }
610+
611+ //chroma
612+ //step1: DC
613+ if (1 == uiCbpChroma || 2 == uiCbpChroma) {
614+ //Cb Cr
615+ WELS_READ_VERIFY (ParseResidualBlockCabac (&sNeighAvail, pNonZeroCount, pBsAux, 16 + (0 << 2), 4, g_kuiChromaDcScan,
616+ CHROMA_DC_V, pCurLayer->pScaledTCoeff[iMbXy] + 256 + (0 << 6), pCurLayer->pChromaQp[iMbXy], pCtx));
617+ WELS_READ_VERIFY (ParseResidualBlockCabac (&sNeighAvail, pNonZeroCount, pBsAux, 16 + (1 << 2), 4, g_kuiChromaDcScan,
618+ CHROMA_DC_U, pCurLayer->pScaledTCoeff[iMbXy] + 256 + (1 << 6), pCurLayer->pChromaQp[iMbXy], pCtx));
619+ }
620+
621+ //step2: AC
622+ if (2 == uiCbpChroma) {
623+ for (i = 0; i < 2; i++) { //Cb Cr
624+ int32_t iResProperty = i ? CHROMA_AC_V : CHROMA_AC_U;
625+ int32_t iIdx = 16 + (i << 2);
626+ for (iId4x4 = 0; iId4x4 < 4; iId4x4++) {
627+ WELS_READ_VERIFY (ParseResidualBlockCabac (&sNeighAvail, pNonZeroCount, pBsAux, iIdx,
628+ iScanIdxEnd - WELS_MAX (iScanIdxStart, 1) + 1, g_kuiZigzagScan + WELS_MAX (iScanIdxStart, 1), iResProperty,
629+ pCurLayer->pScaledTCoeff[iMbXy] + (iIdx << 4), pCurLayer->pChromaQp[iMbXy], pCtx));
630+ iIdx++;
631+ }
632+ }
633+ ST16 (&pCurLayer->pNzc[iMbXy][16], LD16 (&pNonZeroCount[6 + 8 * 1]));
634+ ST16 (&pCurLayer->pNzc[iMbXy][20], LD16 (&pNonZeroCount[6 + 8 * 2]));
635+ ST16 (&pCurLayer->pNzc[iMbXy][18], LD16 (&pNonZeroCount[6 + 8 * 4]));
636+ ST16 (&pCurLayer->pNzc[iMbXy][22], LD16 (&pNonZeroCount[6 + 8 * 5]));
637+ } else {
638+ ST16 (&pCurLayer->pNzc[iMbXy][16], 0);
639+ ST16 (&pCurLayer->pNzc[iMbXy][20], 0);
640+ ST16 (&pCurLayer->pNzc[iMbXy][18], 0);
641+ ST16 (&pCurLayer->pNzc[iMbXy][22], 0);
642+ }
643+ } else {
644+ ST32 (&pCurLayer->pNzc[iMbXy][0], 0);
645+ ST32 (&pCurLayer->pNzc[iMbXy][4], 0);
646+ ST32 (&pCurLayer->pNzc[iMbXy][8], 0);
647+ ST32 (&pCurLayer->pNzc[iMbXy][12], 0);
648+ ST32 (&pCurLayer->pNzc[iMbXy][16], 0);
649+ ST32 (&pCurLayer->pNzc[iMbXy][20], 0);
650+ }
651+
652+ WELS_READ_VERIFY (ParseEndOfSliceCabac (pCtx, uiEosFlag));
653+ if (uiEosFlag) {
654+ RestoreCabacDecEngineToBS (pCtx->pCabacDecEngine, pCtx->pCurDqLayer->pBitStringAux);
655+ }
656+ return ERR_NONE;
657+}
658+
659+int32_t WelsDecodeMbCabacISlice (PWelsDecoderContext pCtx, PNalUnit pNalCur, uint32_t& uiEosFlag) {
660+ WELS_READ_VERIFY (WelsDecodeMbCabacISliceBaseMode0 (pCtx, uiEosFlag));
661+ return ERR_NONE;
662+}
663+
664+int32_t WelsDecodeMbCabacPSliceBaseMode0 (PWelsDecoderContext pCtx, PWelsNeighAvail pNeighAvail, uint32_t& uiEosFlag) {
665+ PDqLayer pCurLayer = pCtx->pCurDqLayer;
666+ PBitStringAux pBsAux = pCurLayer->pBitStringAux;
667+ PSlice pSlice = &pCurLayer->sLayerInfo.sSliceInLayer;
668+ PSliceHeader pSliceHeader = &pSlice->sSliceHeaderExt.sSliceHeader;
669+
670+ int32_t iScanIdxStart = pSlice->sSliceHeaderExt.uiScanIdxStart;
671+ int32_t iScanIdxEnd = pSlice->sSliceHeaderExt.uiScanIdxEnd;
672+ int32_t iMbXy = pCurLayer->iMbXyIndex;
673+
674+ int32_t iMbMode, i;
675+ uint32_t uiMbType = 0, uiCbp = 0, uiCbpLuma = 0, uiCbpChroma = 0;
676+
677+ ENFORCE_STACK_ALIGN_1D (uint8_t, pNonZeroCount, 48, 16);
678+
679+ pCurLayer->pInterPredictionDoneFlag[iMbXy] = 0;
680+
681+ WELS_READ_VERIFY (ParseMBTypePSliceCabac (pCtx, pNeighAvail, uiMbType));
682+ // uiMbType = 4 is not allowded.
683+ if (uiMbType < 4) { //Inter mode
684+ int16_t pMotionVector[LIST_A][30][MV_A];
685+ int16_t pMvdCache[LIST_A][30][MV_A];
686+ int8_t pRefIndex[LIST_A][30];
687+ pCurLayer->pMbType[iMbXy] = g_ksInterMbTypeInfo[uiMbType].iType;
688+ WelsFillCacheInterCabac (pNeighAvail, pNonZeroCount, pMotionVector, pMvdCache, pRefIndex, pCurLayer);
689+ WELS_READ_VERIFY (ParseInterMotionInfoCabac (pCtx, pNeighAvail, pNonZeroCount, pMotionVector, pMvdCache, pRefIndex));
690+ iMbMode = BASE_MB;
691+ pCurLayer->pInterPredictionDoneFlag[iMbXy] = 0;
692+ } else { //Intra mode
693+ uiMbType -= 5;
694+ if (uiMbType > 25) {
695+ return ERR_INFO_INVALID_MB_TYPE;
696+ }
697+
698+ if (25 == uiMbType) { //I_PCM
699+ WELS_READ_VERIFY (ParseIPCMInfoCabac (pCtx));
700+ WELS_READ_VERIFY (ParseEndOfSliceCabac (pCtx, uiEosFlag));
701+ if (uiEosFlag) {
702+ RestoreCabacDecEngineToBS (pCtx->pCabacDecEngine, pCtx->pCurDqLayer->pBitStringAux);
703+ }
704+ return ERR_NONE;
705+ } else { //normal Intra mode
706+ if (0 == uiMbType) { //Intra4x4
707+ ENFORCE_STACK_ALIGN_1D (int8_t, pIntraPredMode, 48, 16);
708+ pCurLayer->pMbType[iMbXy] = MB_TYPE_INTRA4x4;
709+ pCtx->pFillInfoCacheIntra4x4Func (pNeighAvail, pNonZeroCount, pIntraPredMode, pCurLayer);
710+ WELS_READ_VERIFY (ParseIntra4x4Mode (pCtx, pNeighAvail, pIntraPredMode, pBsAux, pCurLayer));
711+ } else { //Intra16x16
712+ pCurLayer->pMbType[iMbXy] = MB_TYPE_INTRA16x16;
713+ pCurLayer->pIntraPredMode[iMbXy][7] = (uiMbType - 1) & 3;
714+ pCurLayer->pCbp[iMbXy] = g_kuiI16CbpTable[ (uiMbType - 1) >> 2];
715+ uiCbpChroma = pCurLayer->pCbp[iMbXy] >> 4;
716+ uiCbpLuma = pCurLayer->pCbp[iMbXy] & 15;
717+ WelsFillCacheNonZeroCount (pNeighAvail, pNonZeroCount, pCurLayer);
718+ WELS_READ_VERIFY (ParseIntra16x16Mode (pCtx, pNeighAvail, pBsAux, pCurLayer));
719+ }
720+ iMbMode = BASE_MB;
721+ }
722+ }
723+
724+ if (MB_TYPE_INTRA16x16 != pCurLayer->pMbType[iMbXy]) {
725+ WELS_READ_VERIFY (ParseCbpInfoCabac (pCtx, pNeighAvail, uiCbp));
726+ pCurLayer->pCbp[iMbXy] = uiCbp;
727+ uiCbpChroma = pCurLayer->pCbp[iMbXy] >> 4;
728+ uiCbpLuma = pCurLayer->pCbp[iMbXy] & 15;
729+ }
730+
731+ pCtx->sBlockFunc.pWelsBlockZero16x16Func (pCurLayer->pScaledTCoeff[iMbXy], 16);
732+ pCtx->sBlockFunc.pWelsBlockZero8x8Func (pCurLayer->pScaledTCoeff[iMbXy] + 256, 8);
733+ pCtx->sBlockFunc.pWelsBlockZero8x8Func (pCurLayer->pScaledTCoeff[iMbXy] + 256 + 64, 8);
734+
735+ ST32 (&pCurLayer->pNzc[iMbXy][0], 0);
736+ ST32 (&pCurLayer->pNzc[iMbXy][4], 0);
737+ ST32 (&pCurLayer->pNzc[iMbXy][8], 0);
738+ ST32 (&pCurLayer->pNzc[iMbXy][12], 0);
739+ ST32 (&pCurLayer->pNzc[iMbXy][16], 0);
740+ ST32 (&pCurLayer->pNzc[iMbXy][20], 0);
741+
742+ if (pCurLayer->pCbp[iMbXy] || MB_TYPE_INTRA16x16 == pCurLayer->pMbType[iMbXy]) {
743+ int32_t iQpDelta, iId8x8, iId4x4;
744+
745+ WELS_READ_VERIFY (ParseDeltaQpCabac (pCtx, iQpDelta));
746+ pCurLayer->pLumaQp[iMbXy] = (pSlice->iLastMbQp + iQpDelta + 52) % 52; //update last_mb_qp
747+ pSlice->iLastMbQp = pCurLayer->pLumaQp[iMbXy];
748+ pCurLayer->pChromaQp[iMbXy] = g_kuiChromaQpTable[WELS_CLIP3 (pSlice->iLastMbQp +
749+ pSliceHeader->pPps->iChromaQpIndexOffset, 0, 51)];
750+
751+ if (MB_TYPE_INTRA16x16 == pCurLayer->pMbType[iMbXy]) {
752+ //step1: Luma DC
753+ WELS_READ_VERIFY (ParseResidualBlockCabac (pNeighAvail, pNonZeroCount, pBsAux, 0, 16, g_kuiLumaDcZigzagScan,
754+ I16_LUMA_DC, pCurLayer->pScaledTCoeff[iMbXy], pCurLayer->pLumaQp[iMbXy], pCtx));
755+ //step2: Luma AC
756+ if (uiCbpLuma) {
757+ for (i = 0; i < 16; i++) {
758+ WELS_READ_VERIFY (ParseResidualBlockCabac (pNeighAvail, pNonZeroCount, pBsAux, i, iScanIdxEnd - WELS_MAX (iScanIdxStart,
759+ 1) + 1, g_kuiZigzagScan + WELS_MAX (iScanIdxStart, 1), I16_LUMA_AC, pCurLayer->pScaledTCoeff[iMbXy] + (i << 4),
760+ pCurLayer->pLumaQp[iMbXy], pCtx));
761+ }
762+ ST32 (&pCurLayer->pNzc[iMbXy][0], LD32 (&pNonZeroCount[1 + 8 * 1]));
763+ ST32 (&pCurLayer->pNzc[iMbXy][4], LD32 (&pNonZeroCount[1 + 8 * 2]));
764+ ST32 (&pCurLayer->pNzc[iMbXy][8], LD32 (&pNonZeroCount[1 + 8 * 3]));
765+ ST32 (&pCurLayer->pNzc[iMbXy][12], LD32 (&pNonZeroCount[1 + 8 * 4]));
766+ } else {
767+ ST32 (&pCurLayer->pNzc[iMbXy][0], 0);
768+ ST32 (&pCurLayer->pNzc[iMbXy][4], 0);
769+ ST32 (&pCurLayer->pNzc[iMbXy][8], 0);
770+ ST32 (&pCurLayer->pNzc[iMbXy][12], 0);
771+ }
772+ } else { //non-MB_TYPE_INTRA16x16
773+ for (iId8x8 = 0; iId8x8 < 4; iId8x8++) {
774+ if (uiCbpLuma & (1 << iId8x8)) {
775+ int32_t iIdx = (iId8x8 << 2);
776+ for (iId4x4 = 0; iId4x4 < 4; iId4x4++) {
777+ //Luma (DC and AC decoding together)
778+ WELS_READ_VERIFY (ParseResidualBlockCabac (pNeighAvail, pNonZeroCount, pBsAux, iIdx, iScanIdxEnd - iScanIdxStart + 1,
779+ g_kuiZigzagScan + iScanIdxStart, LUMA_DC_AC, pCurLayer->pScaledTCoeff[iMbXy] + (iIdx << 4), pCurLayer->pLumaQp[iMbXy],
780+ pCtx));
781+ iIdx++;
782+ }
783+ } else {
784+ ST16 (&pNonZeroCount[g_kCacheNzcScanIdx[iId8x8 << 2]], 0);
785+ ST16 (&pNonZeroCount[g_kCacheNzcScanIdx[ (iId8x8 << 2) + 2]], 0);
786+ }
787+ }
788+ ST32 (&pCurLayer->pNzc[iMbXy][0], LD32 (&pNonZeroCount[1 + 8 * 1]));
789+ ST32 (&pCurLayer->pNzc[iMbXy][4], LD32 (&pNonZeroCount[1 + 8 * 2]));
790+ ST32 (&pCurLayer->pNzc[iMbXy][8], LD32 (&pNonZeroCount[1 + 8 * 3]));
791+ ST32 (&pCurLayer->pNzc[iMbXy][12], LD32 (&pNonZeroCount[1 + 8 * 4]));
792+ }
793+
794+ //chroma
795+ //step1: DC
796+ if (1 == uiCbpChroma || 2 == uiCbpChroma) {
797+ for (i = 0; i < 2; i++) {
798+ int32_t iResProperty = i ? CHROMA_DC_V : CHROMA_DC_U;
799+ WELS_READ_VERIFY (ParseResidualBlockCabac (pNeighAvail, pNonZeroCount, pBsAux, 16 + (i << 2), 4, g_kuiChromaDcScan,
800+ iResProperty, pCurLayer->pScaledTCoeff[iMbXy] + 256 + (i << 6), pCurLayer->pChromaQp[iMbXy], pCtx));
801+ }
802+ }
803+ //step2: AC
804+ if (2 == uiCbpChroma) {
805+ for (i = 0; i < 2; i++) {
806+ int32_t iResProperty = i ? CHROMA_AC_V : CHROMA_AC_U;
807+ int32_t index = 16 + (i << 2);
808+ for (iId4x4 = 0; iId4x4 < 4; iId4x4++) {
809+ WELS_READ_VERIFY (ParseResidualBlockCabac (pNeighAvail, pNonZeroCount, pBsAux, index,
810+ iScanIdxEnd - WELS_MAX (iScanIdxStart, 1) + 1, g_kuiZigzagScan + WELS_MAX (iScanIdxStart, 1),
811+ iResProperty, pCurLayer->pScaledTCoeff[iMbXy] + (index << 4), pCurLayer->pChromaQp[iMbXy], pCtx));
812+ index++;
813+ }
814+ }
815+ ST16 (&pCurLayer->pNzc[iMbXy][16], LD16 (&pNonZeroCount[6 + 8 * 1]));
816+ ST16 (&pCurLayer->pNzc[iMbXy][20], LD16 (&pNonZeroCount[6 + 8 * 2]));
817+ ST16 (&pCurLayer->pNzc[iMbXy][18], LD16 (&pNonZeroCount[6 + 8 * 4]));
818+ ST16 (&pCurLayer->pNzc[iMbXy][22], LD16 (&pNonZeroCount[6 + 8 * 5]));
819+ } else {
820+ ST32 (&pCurLayer->pNzc[iMbXy][16], 0);
821+ ST32 (&pCurLayer->pNzc[iMbXy][20], 0);
822+ }
823+ } else {
824+ pCurLayer->pLumaQp[iMbXy] = pSlice->iLastMbQp;
825+ pCurLayer->pChromaQp[iMbXy] = g_kuiChromaQpTable[WELS_CLIP3 (pCurLayer->pLumaQp[iMbXy] +
826+ pSliceHeader->pPps->iChromaQpIndexOffset, 0, 51)];
827+ }
828+
829+ WELS_READ_VERIFY (ParseEndOfSliceCabac (pCtx, uiEosFlag));
830+ if (uiEosFlag) {
831+ RestoreCabacDecEngineToBS (pCtx->pCabacDecEngine, pCtx->pCurDqLayer->pBitStringAux);
832+ }
833+
834+ return ERR_NONE;
835+}
836+
837+int32_t WelsDecodeMbCabacPSlice (PWelsDecoderContext pCtx, PNalUnit pNalCur, uint32_t& uiEosFlag) {
838+ PDqLayer pCurLayer = pCtx->pCurDqLayer;
839+ PSlice pSlice = &pCurLayer->sLayerInfo.sSliceInLayer;
840+ PSliceHeader pSliceHeader = &pSlice->sSliceHeaderExt.sSliceHeader;
841+ uint32_t uiCode;
842+ int32_t iMbXy = pCurLayer->iMbXyIndex;
843+ int32_t i;
844+ SWelsNeighAvail uiNeighAvail;
845+ pCurLayer->pCbp[iMbXy] = 0;
846+ pCurLayer->pCbfDc[iMbXy] = 0;
847+ pCurLayer->pChromaPredMode[iMbXy] = C_PRED_DC;
848+
849+ GetNeighborAvailMbType (&uiNeighAvail, pCurLayer);
850+ WELS_READ_VERIFY (ParseSkipFlagCabac (pCtx, &uiNeighAvail, uiCode));
851+ if (uiCode) {
852+ int16_t pMv[2] = {0};
853+ pCurLayer->pMbType[iMbXy] = MB_TYPE_SKIP;
854+ ST32 (&pCurLayer->pNzc[iMbXy][0], 0);
855+ ST32 (&pCurLayer->pNzc[iMbXy][4], 0);
856+ ST32 (&pCurLayer->pNzc[iMbXy][8], 0);
857+ ST32 (&pCurLayer->pNzc[iMbXy][12], 0);
858+ ST32 (&pCurLayer->pNzc[iMbXy][16], 0);
859+ ST32 (&pCurLayer->pNzc[iMbXy][20], 0);
860+
861+ pCurLayer->pInterPredictionDoneFlag[iMbXy] = 0;
862+ memset (pCurLayer->pRefIndex[0][iMbXy], 0, sizeof (int8_t) * 16);
863+
864+ //predict mv
865+ PredPSkipMvFromNeighbor (pCurLayer, pMv);
866+ for (i = 0; i < 16; i++) {
867+ ST32 (pCurLayer->pMv[0][iMbXy][i], * (uint32_t*)pMv);
868+ ST32 (pCurLayer->pMvd[0][iMbXy][i], 0);
869+ }
870+
871+ if (!pSlice->sSliceHeaderExt.bDefaultResidualPredFlag) {
872+ memset (pCurLayer->pScaledTCoeff[iMbXy], 0, 384 * sizeof (int16_t));
873+ }
874+
875+ //reset rS
876+ pCurLayer->pLumaQp[iMbXy] = pSlice->iLastMbQp; //??????????????? dqaunt of previous mb
877+ pCurLayer->pChromaQp[iMbXy] = g_kuiChromaQpTable[WELS_CLIP3 (pCurLayer->pLumaQp[iMbXy] +
878+ pSliceHeader->pPps->iChromaQpIndexOffset, 0, 51)];
879+
880+ //for neighboring CABAC usage
881+ pSlice->iLastDeltaQp = 0;
882+
883+ WELS_READ_VERIFY (ParseEndOfSliceCabac (pCtx, uiEosFlag));
884+
885+ return ERR_NONE;
886+ }
887+
888+ WELS_READ_VERIFY (WelsDecodeMbCabacPSliceBaseMode0 (pCtx, &uiNeighAvail, uiEosFlag));
889+ return ERR_NONE;
890+}
891+
321892 int32_t WelsDecodeSlice (PWelsDecoderContext pCtx, bool bFirstSliceInLayer, PNalUnit pNalCur) {
322893 PDqLayer pCurLayer = pCtx->pCurDqLayer;
323894 PFmo pFmo = pCtx->pFmo;
@@ -329,34 +900,50 @@ int32_t WelsDecodeSlice (PWelsDecoderContext pCtx, bool bFirstSliceInLayer, PNal · 5 unmodified lines
329900 PSliceHeader pSliceHeader = &pSliceHeaderExt->sSliceHeader;
330901 int32_t iMbX, iMbY;
331902 const int32_t kiCountNumMb = pSliceHeader->pSps->uiTotalMbCount; //need to be correct when fmo or multi slice
332− PBitStringAux pBs = pCurLayer->pBitStringAux;
333− intX_t iUsedBits = 0;
334−
335− PWelsDecMbCavlcFunc pDecMbCavlcFunc;
903+ uint32_t uiEosFlag = 0;
904+ PWelsDecMbFunc pDecMbFunc;
336905
337906 pSlice->iTotalMbInCurSlice = 0; //initialize at the starting of slice decoding.
338907
339− if (P_SLICE == pSliceHeader->eSliceType) {
340− pDecMbCavlcFunc = WelsDecodeMbCavlcPSlice;
341− } else { //I_SLICE
342− pDecMbCavlcFunc = WelsDecodeMbCavlcISlice;
908+ if (pCtx->pPps->bEntropyCodingModeFlag) {
909+ if (pSlice->sSliceHeaderExt.bAdaptiveMotionPredFlag ||
910+ pSlice->sSliceHeaderExt.bAdaptiveBaseModeFlag ||
911+ pSlice->sSliceHeaderExt.bAdaptiveResidualPredFlag) {
912+ WelsLog (& (pCtx->sLogCtx), WELS_LOG_ERROR,
913+ "WelsDecodeSlice()::::ILP flag exist, not supported with CABAC enabled!");
914+ pCtx->iErrorCode |= dsBitstreamError;
915+ return dsBitstreamError;
916+ }
917+ if (P_SLICE == pSliceHeader->eSliceType)
918+ pDecMbFunc = WelsDecodeMbCabacPSlice;
919+ else //I_SLICE. B_SLICE not supported now
920+ pDecMbFunc = WelsDecodeMbCabacISlice;
921+ } else {
922+ if (P_SLICE == pSliceHeader->eSliceType) {
923+ pDecMbFunc = WelsDecodeMbCavlcPSlice;
924+ } else { //I_SLICE
925+ pDecMbFunc = WelsDecodeMbCavlcISlice;
926+ }
343927 }
344928
345929 if (pSliceHeader->pPps->bConstainedIntraPredFlag) {
346930 pCtx->pFillInfoCacheIntra4x4Func = WelsFillCacheConstrain1Intra4x4;
347− pCtx->pParseIntra4x4ModeFunc = ParseIntra4x4ModeConstrain1;
348− pCtx->pParseIntra16x16ModeFunc = ParseIntra16x16ModeConstrain1;
931+ pCtx->pMap4x4NeighToSampleFunc = WelsMap4x4NeighToSampleConstrain1;
932+ pCtx->pMap16x16NeighToSampleFunc = WelsMap16x16NeighToSampleConstrain1;
349933 } else {
350934 pCtx->pFillInfoCacheIntra4x4Func = WelsFillCacheConstrain0Intra4x4;
351− pCtx->pParseIntra4x4ModeFunc = ParseIntra4x4ModeConstrain0;
352− pCtx->pParseIntra16x16ModeFunc = ParseIntra16x16ModeConstrain0;
935+ pCtx->pMap4x4NeighToSampleFunc = WelsMap4x4NeighToSampleNormal;
936+ pCtx->pMap16x16NeighToSampleFunc = WelsMap16x16NeighToSampleNormal;
353937 }
354938
355939 pCtx->eSliceType = pSliceHeader->eSliceType;
356−
357940 if (pCurLayer->sLayerInfo.pPps->bEntropyCodingModeFlag == 1) {
358− //CABAC encoding is unsupported yet!
359− return -1;
941+ int32_t iQp = pSlice->sSliceHeaderExt.sSliceHeader.iSliceQp;
942+ int32_t iCabacInitIdc = pSlice->sSliceHeaderExt.sSliceHeader.iCabacInitIdc;
943+ WelsCabacContextInit (pCtx, pSlice->eSliceType, iCabacInitIdc, iQp);
944+ //InitCabacCtx (pCtx->pCabacCtx, pSlice->eSliceType, iCabacInitIdc, iQp);
945+ pSlice->iLastDeltaQp = 0;
946+ WELS_READ_VERIFY (InitCabacDecEngineFromBS (pCtx->pCabacDecEngine, pCtx->pCurDqLayer->pBitStringAux));
360947 }
361948
362949 iNextMbXyIndex = pSliceHeader->iFirstMbInSlice;
@@ -398,31 +985,21 @@ int32_t WelsDecodeSlice (PWelsDecoderContext pCtx, bool bFirstSliceInLayer, PNal · 35 unmodified lines
398985 }
399986
400987 pCurLayer->pSliceIdc[iNextMbXyIndex] = iSliceIdc;
401− iRet = pDecMbCavlcFunc (pCtx, pNalCur);
988+ iRet = pDecMbFunc (pCtx, pNalCur, uiEosFlag);
402989
403990 if (iRet != ERR_NONE) {
404991 return iRet;
405992 }
406993
407994 ++pSlice->iTotalMbInCurSlice;
408−
995+ if (uiEosFlag) { //end of slice
996+ break;
997+ }
409998 if (pSliceHeader->pPps->uiNumSliceGroups > 1) {
410999 iNextMbXyIndex = FmoNextMb (pFmo, iNextMbXyIndex);
4111000 } else {
4121001 ++iNextMbXyIndex;
4131002 }
414−
415− // check whether there is left bits to read next time in case multiple slices
416− iUsedBits = ((pBs->pCurBuf - pBs->pStartBuf) << 3) - (16 - pBs->iLeftBits);
417− if (iUsedBits == pBs->iBits && 0 >= pCurLayer->sLayerInfo.sSliceInLayer.iMbSkipRun) { // slice boundary
418− break;
419− }
420− if (iUsedBits > pBs->iBits) { //When BS incomplete, as long as find it, SHOULD stop decoding to avoid mosaic or crash.
421− WelsLog (& (pCtx->sLogCtx), WELS_LOG_WARNING,
422− "WelsDecodeSlice()::::pBs incomplete, iUsedBits:%" PRId64" > pBs->iBits:%d, MUST stop decoding.",
423− (int64_t) iUsedBits, pBs->iBits);
424− return -1;
425− }
4261003 iMbX = iNextMbXyIndex % pCurLayer->iMbWidth;
4271004 iMbY = iNextMbXyIndex / pCurLayer->iMbWidth;
4281005 pCurLayer->iMbX = iMbX;
@@ -440,7 +1017,7 @@ int32_t WelsActualDecodeMbCavlcISlice (PWelsDecoderContext pCtx) { · 11 unmodified lines
4401017 PSlice pSlice = &pCurLayer->sLayerInfo.sSliceInLayer;
4411018 PSliceHeader pSliceHeader = &pSlice->sSliceHeaderExt.sSliceHeader;
4421019
443− SNeighAvail sNeighAvail;
1020+ SWelsNeighAvail sNeighAvail;
4441021
4451022 int32_t iScanIdxStart = pSlice->sSliceHeaderExt.uiScanIdxStart;
4461023 int32_t iScanIdxEnd = pSlice->sSliceHeaderExt.uiScanIdxEnd;
@@ -455,11 +1032,11 @@ int32_t WelsActualDecodeMbCavlcISlice (PWelsDecoderContext pCtx) { · 8 unmodified lines
4551032 int32_t iCode;
4561033
4571034 ENFORCE_STACK_ALIGN_1D (uint8_t, pNonZeroCount, 48, 16);
458−
1035+ GetNeighborAvailMbType (&sNeighAvail, pCurLayer);
4591036 pCurLayer->pInterPredictionDoneFlag[iMbXy] = 0;
4601037 pCurLayer->pResidualPredFlag[iMbXy] = pSlice->sSliceHeaderExt.bDefaultResidualPredFlag;
4611038
462− WELS_READ_VERIFY (BsGetUe (pBs, &uiCode)); //mb_type
1039+ WELS_READ_VERIFY (BsGetUe (pBs, &uiCode)); //uiMbType
4631040 uiMbType = uiCode;
4641041 if (uiMbType > 25) {
4651042 return ERR_INFO_INVALID_MB_TYPE;
@@ -508,20 +1085,18 @@ int32_t WelsActualDecodeMbCavlcISlice (PWelsDecoderContext pCtx) { · 42 unmodified lines
5081085 }
5091086
5101087 pBs->pCurBuf += 384;
511− InitReadBits (pBs);
5121088
5131089 //step 3: update QP and pNonZeroCount
5141090 pCurLayer->pLumaQp[iMbXy] = 0;
5151091 pCurLayer->pChromaQp[iMbXy] = 0;
5161092 memset (pNzc, 16, sizeof (pCurLayer->pNzc[iMbXy])); //Rec. 9.2.1 for PCM, nzc=16
1093+ WELS_READ_VERIFY(InitReadBits (pBs, 0));
5171094 return 0;
5181095 } else if (0 == uiMbType) { //reference to JM
5191096 ENFORCE_STACK_ALIGN_1D (int8_t, pIntraPredMode, 48, 16);
5201097 pCurLayer->pMbType[iMbXy] = MB_TYPE_INTRA4x4;
5211098 pCtx->pFillInfoCacheIntra4x4Func (&sNeighAvail, pNonZeroCount, pIntraPredMode, pCurLayer);
522− if (pCtx->pParseIntra4x4ModeFunc (&sNeighAvail, pIntraPredMode, pBs, pCurLayer)) {
523− return -1;
524− }
1099+ WELS_READ_VERIFY (ParseIntra4x4Mode (pCtx, &sNeighAvail, pIntraPredMode, pBs, pCurLayer));
5251100
5261101 //uiCbp
5271102 WELS_READ_VERIFY (BsGetUe (pBs, &uiCode)); //coded_block_pattern
@@ -542,9 +1117,7 @@ int32_t WelsActualDecodeMbCavlcISlice (PWelsDecoderContext pCtx) { · 14 unmodified lines
5421117 uiCbpC = pCurLayer->pCbp[iMbXy] >> 4;
5431118 uiCbpL = pCurLayer->pCbp[iMbXy] & 15;
5441119 WelsFillCacheNonZeroCount (&sNeighAvail, pNonZeroCount, pCurLayer);
545− if (pCtx->pParseIntra16x16ModeFunc (&sNeighAvail, pBs, pCurLayer)) {
546− return -1;
547− }
1120+ WELS_READ_VERIFY (ParseIntra16x16Mode (pCtx, &sNeighAvail, pBs, pCurLayer));
5481121 }
5491122
5501123 memset (pCurLayer->pScaledTCoeff[iMbXy], 0, 384 * sizeof (pCurLayer->pScaledTCoeff[iMbXy][0]));
@@ -572,19 +1145,7 @@ int32_t WelsActualDecodeMbCavlcISlice (PWelsDecoderContext pCtx) { · 21 unmodified lines
5721145 return ERR_INFO_INVALID_QP;
5731146 }
5741147
575− pCurLayer->pLumaQp[iMbXy] = pSlice->iLastMbQp + iQpDelta; //update iLastMbQp
576− //refer to JVT-X201wcm1.doc equation(7-35)
577− if ((unsigned) (pCurLayer->pLumaQp[iMbXy]) > 51) {
578− if (pCurLayer->pLumaQp[iMbXy] < 0) {
579− pCurLayer->pLumaQp[iMbXy] += 52;
580− } else {
581− pCurLayer->pLumaQp[iMbXy] -= 52;
582− }
583− }
584− //QP should be in the range of [0, 51]
585− if (pCurLayer->pLumaQp[iMbXy] < 0 || pCurLayer->pLumaQp[iMbXy] > 51) {
586− return ERR_INFO_INVALID_QP;
587− }
1148+ pCurLayer->pLumaQp[iMbXy] = (pSlice->iLastMbQp + iQpDelta + 52) % 52; //update last_mb_qp
5881149 pSlice->iLastMbQp = pCurLayer->pLumaQp[iMbXy];
5891150 pCurLayer->pChromaQp[iMbXy] = g_kuiChromaQpTable[WELS_CLIP3 (pSlice->iLastMbQp +
5901151 pSliceHeader->pPps->iChromaQpIndexOffset, 0,
@@ -673,14 +1234,14 @@ int32_t WelsActualDecodeMbCavlcISlice (PWelsDecoderContext pCtx) { · 82 unmodified lines
6731234 return 0;
6741235 }
6751236
676−int32_t WelsDecodeMbCavlcISlice (PWelsDecoderContext pCtx, PNalUnit pNalCur) {
1237+int32_t WelsDecodeMbCavlcISlice (PWelsDecoderContext pCtx, PNalUnit pNalCur, uint32_t& uiEosFlag) {
6771238 PDqLayer pCurLayer = pCtx->pCurDqLayer;
6781239 PBitStringAux pBs = pCurLayer->pBitStringAux;
6791240 PSliceHeaderExt pSliceHeaderExt = &pCurLayer->sLayerInfo.sSliceInLayer.sSliceHeaderExt;
6801241 int32_t iBaseModeFlag;
6811242 int32_t iRet = 0; //should have the return value to indicate decoding error or not, It's NECESSARY--2010.4.15
6821243 uint32_t uiCode;
683−
1244+ intX_t iUsedBits;
6841245 if (pSliceHeaderExt->bAdaptiveBaseModeFlag == 1) {
6851246 WELS_READ_VERIFY (BsGetOneBit (pBs, &uiCode)); //base_mode_flag
6861247 iBaseModeFlag = uiCode;
@@ -698,6 +1259,19 @@ int32_t WelsDecodeMbCavlcISlice (PWelsDecoderContext pCtx, PNalUnit pNalCur) { · 11 unmodified lines
6981259 return iRet;
6991260 }
7001261
1262+ // check whether there is left bits to read next time in case multiple slices
1263+ iUsedBits = ((pBs->pCurBuf - pBs->pStartBuf) << 3) - (16 - pBs->iLeftBits);
1264+ // sub 1, for stop bit
1265+ if ((iUsedBits == (pBs->iBits - 1)) && (0 >= pCurLayer->sLayerInfo.sSliceInLayer.iMbSkipRun)) { // slice boundary
1266+ uiEosFlag = 1;
1267+ }
1268+ if (iUsedBits > (pBs->iBits -
1269+ 1)) { //When BS incomplete, as long as find it, SHOULD stop decoding to avoid mosaic or crash.
1270+ WelsLog (& (pCtx->sLogCtx), WELS_LOG_WARNING,
1271+ "WelsDecodeMbCavlcISlice()::::pBs incomplete, iUsedBits:%"PRId64" > pBs->iBits:%d, MUST stop decoding.",
1272+ (int64_t) iUsedBits, pBs->iBits);
1273+ return -1;
1274+ }
7011275 return 0;
7021276 }
7031277
@@ -708,11 +1282,10 @@ int32_t WelsActualDecodeMbCavlcPSlice (PWelsDecoderContext pCtx) { · 4 unmodified lines
7081282 PSlice pSlice = &pCurLayer->sLayerInfo.sSliceInLayer;
7091283 PSliceHeader pSliceHeader = &pSlice->sSliceHeaderExt.sSliceHeader;
7101284
711− SNeighAvail sNeighAvail;
712−
7131285 int32_t iScanIdxStart = pSlice->sSliceHeaderExt.uiScanIdxStart;
7141286 int32_t iScanIdxEnd = pSlice->sSliceHeaderExt.uiScanIdxEnd;
7151287
1288+ SWelsNeighAvail sNeighAvail;
7161289 int32_t iMbX = pCurLayer->iMbX;
7171290 int32_t iMbY = pCurLayer->iMbY;
7181291 const int32_t iMbXy = pCurLayer->iMbXyIndex;
@@ -721,15 +1294,13 @@ int32_t WelsActualDecodeMbCavlcPSlice (PWelsDecoderContext pCtx) { · 2 unmodified lines
7211294 uint32_t uiMbType = 0, uiCbp = 0, uiCbpL = 0, uiCbpC = 0;
7221295 uint32_t uiCode;
7231296 int32_t iCode;
724−
1297+ GetNeighborAvailMbType (&sNeighAvail, pCurLayer);
7251298 ENFORCE_STACK_ALIGN_1D (uint8_t, pNonZeroCount, 48, 16);
7261299 pCurLayer->pInterPredictionDoneFlag[iMbXy] = 0;//2009.10.23
727−
728− WELS_READ_VERIFY (BsGetUe (pBs, &uiCode)); //mb_type
1300+ WELS_READ_VERIFY (BsGetUe (pBs, &uiCode)); //uiMbType
7291301 uiMbType = uiCode;
7301302 if (uiMbType < 5) { //inter MB type
7311303 int16_t iMotionVector[LIST_A][30][MV_A];
732−
7331304 int8_t iRefIndex[LIST_A][30];
7341305 pCurLayer->pMbType[iMbXy] = g_ksInterMbTypeInfo[uiMbType].iType;
7351306 WelsFillCacheInter (&sNeighAvail, pNonZeroCount, iMotionVector, iRefIndex, pCurLayer);
@@ -800,7 +1371,6 @@ int32_t WelsActualDecodeMbCavlcPSlice (PWelsDecoderContext pCtx) { · 64 unmodified lines
8001371 }
8011372
8021373 pBs->pCurBuf += 384;
803− InitReadBits (pBs);
8041374
8051375 //step 3: update QP and pNonZeroCount
8061376 pCurLayer->pLumaQp[iMbXy] = 0;
@@ -812,13 +1382,14 @@ int32_t WelsActualDecodeMbCavlcPSlice (PWelsDecoderContext pCtx) { · 5 unmodified lines
8121382 ST32A4 (&pNzc[12], 0x10101010);
8131383 ST32A4 (&pNzc[16], 0x10101010);
8141384 ST32A4 (&pNzc[20], 0x10101010);
1385+ WELS_READ_VERIFY (InitReadBits (pBs, 0));
8151386 return 0;
8161387 } else {
8171388 if (0 == uiMbType) {
8181389 ENFORCE_STACK_ALIGN_1D (int8_t, pIntraPredMode, 48, 16);
8191390 pCurLayer->pMbType[iMbXy] = MB_TYPE_INTRA4x4;
8201391 pCtx->pFillInfoCacheIntra4x4Func (&sNeighAvail, pNonZeroCount, pIntraPredMode, pCurLayer);
821− if (pCtx->pParseIntra4x4ModeFunc (&sNeighAvail, pIntraPredMode, pBs, pCurLayer)) {
1392+ if (ParseIntra4x4Mode (pCtx, &sNeighAvail, pIntraPredMode, pBs, pCurLayer)) {
8221393 return -1;
8231394 }
8241395 } else { //I_PCM exclude, we can ignore it
@@ -828,7 +1399,7 @@ int32_t WelsActualDecodeMbCavlcPSlice (PWelsDecoderContext pCtx) { · 3 unmodified lines
8281399 uiCbpC = pCurLayer->pCbp[iMbXy] >> 4;
8291400 uiCbpL = pCurLayer->pCbp[iMbXy] & 15;
8301401 WelsFillCacheNonZeroCount (&sNeighAvail, pNonZeroCount, pCurLayer);
831− if (pCtx->pParseIntra16x16ModeFunc (&sNeighAvail, pBs, pCurLayer)) {
1402+ if (ParseIntra16x16Mode (pCtx, &sNeighAvail, pBs, pCurLayer)) {
8321403 return -1;
8331404 }
8341405 }
@@ -877,19 +1448,7 @@ int32_t WelsActualDecodeMbCavlcPSlice (PWelsDecoderContext pCtx) { · 42 unmodified lines
8771448 return ERR_INFO_INVALID_QP;
8781449 }
8791450
880− pCurLayer->pLumaQp[iMbXy] = pSlice->iLastMbQp + iQpDelta; //update iLastMbQp
881− //refer to JVT-X201wcm1.doc equation(7-35)
882− if ((unsigned) (pCurLayer->pLumaQp[iMbXy]) > 51) {
883− if (pCurLayer->pLumaQp[iMbXy] < 0) {
884− pCurLayer->pLumaQp[iMbXy] += 52;
885− } else {
886− pCurLayer->pLumaQp[iMbXy] -= 52;
887− }
888− }
889− //QP should be in the range of [0, 51]
890− if (pCurLayer->pLumaQp[iMbXy] < 0 || pCurLayer->pLumaQp[iMbXy] > 51) {
891− return ERR_INFO_INVALID_QP;
892− }
1451+ pCurLayer->pLumaQp[iMbXy] = (pSlice->iLastMbQp + iQpDelta + 52) % 52; //update last_mb_qp
8931452 pSlice->iLastMbQp = pCurLayer->pLumaQp[iMbXy];
8941453 pCurLayer->pChromaQp[iMbXy] = g_kuiChromaQpTable[WELS_CLIP3 (pSlice->iLastMbQp +
8951454 pSliceHeader->pPps->iChromaQpIndexOffset, 0,
@@ -978,12 +1537,12 @@ int32_t WelsActualDecodeMbCavlcPSlice (PWelsDecoderContext pCtx) { · 82 unmodified lines
9781537 return 0;
9791538 }
9801539
981−int32_t WelsDecodeMbCavlcPSlice (PWelsDecoderContext pCtx, PNalUnit pNalCur) {
1540+int32_t WelsDecodeMbCavlcPSlice (PWelsDecoderContext pCtx, PNalUnit pNalCur, uint32_t& uiEosFlag) {
9821541 PDqLayer pCurLayer = pCtx->pCurDqLayer;
9831542 PBitStringAux pBs = pCurLayer->pBitStringAux;
9841543 PSlice pSlice = &pCurLayer->sLayerInfo.sSliceInLayer;
9851544 PSliceHeader pSliceHeader = &pSlice->sSliceHeaderExt.sSliceHeader;
986−
1545+ intX_t iUsedBits;
9871546 const int32_t iMbXy = pCurLayer->iMbXyIndex;
9881547 int8_t* pNzc = pCurLayer->pNzc[iMbXy];
9891548 int32_t iBaseModeFlag, i;
@@ -996,7 +1555,6 @@ int32_t WelsDecodeMbCavlcPSlice (PWelsDecoderContext pCtx, PNalUnit pNalCur) { · 6 unmodified lines
9961555 if (-1 == pSlice->iMbSkipRun) {
9971556 return -1;
9981557 }
999−
10001558 }
10011559 if (pSlice->iMbSkipRun--) {
10021560 int16_t iMv[2];
@@ -1031,27 +1589,37 @@ int32_t WelsDecodeMbCavlcPSlice (PWelsDecoderContext pCtx, PNalUnit pNalCur) { · 28 unmodified lines
10311589 }
10321590
10331591 pCurLayer->pCbp[iMbXy] = 0;
1034−
1035− return 0;
1036− }
1037−
1038− if (pSlice->sSliceHeaderExt.bAdaptiveBaseModeFlag == 1) {
1039− WELS_READ_VERIFY (BsGetOneBit (pBs, &uiCode)); //base_mode_flag
1040− iBaseModeFlag = uiCode;
10411592 } else {
1042− iBaseModeFlag = pSlice->sSliceHeaderExt.bDefaultBaseModeFlag;
1593+ if (pSlice->sSliceHeaderExt.bAdaptiveBaseModeFlag == 1) {
1594+ WELS_READ_VERIFY (BsGetOneBit (pBs, &uiCode)); //base_mode_flag
1595+ iBaseModeFlag = uiCode;
1596+ } else {
1597+ iBaseModeFlag = pSlice->sSliceHeaderExt.bDefaultBaseModeFlag;
1598+ }
1599+ if (!iBaseModeFlag) {
1600+ iRet = WelsActualDecodeMbCavlcPSlice (pCtx);
1601+ } else {
1602+ WelsLog (& (pCtx->sLogCtx), WELS_LOG_WARNING, "iBaseModeFlag (%d) != 0, inter-layer prediction not supported.",
1603+ iBaseModeFlag);
1604+ return GENERATE_ERROR_NO (ERR_LEVEL_SLICE_HEADER, ERR_INFO_UNSUPPORTED_ILP);
1605+ }
1606+ if (iRet) { //occur error when parsing, MUST STOP decoding
1607+ return iRet;
1608+ }
10431609 }
1044− if (!iBaseModeFlag) {
1045− iRet = WelsActualDecodeMbCavlcPSlice (pCtx);
1046− } else {
1047− WelsLog (& (pCtx->sLogCtx), WELS_LOG_WARNING, "iBaseModeFlag (%d) != 0, inter-layer prediction not supported.",
1048− iBaseModeFlag);
1049− return GENERATE_ERROR_NO (ERR_LEVEL_SLICE_HEADER, ERR_INFO_UNSUPPORTED_ILP);
1610+ // check whether there is left bits to read next time in case multiple slices
1611+ iUsedBits = ((pBs->pCurBuf - pBs->pStartBuf) << 3) - (16 - pBs->iLeftBits);
1612+ // sub 1, for stop bit
1613+ if ((iUsedBits == (pBs->iBits - 1)) && (0 >= pCurLayer->sLayerInfo.sSliceInLayer.iMbSkipRun)) { // slice boundary
1614+ uiEosFlag = 1;
10501615 }
1051− if (iRet) { //occur error when parsing, MUST STOP decoding
1052− return iRet;
1616+ if (iUsedBits > (pBs->iBits -
1617+ 1)) { //When BS incomplete, as long as find it, SHOULD stop decoding to avoid mosaic or crash.
1618+ WelsLog (& (pCtx->sLogCtx), WELS_LOG_WARNING,
1619+ "WelsDecodeMbCavlcISlice()::::pBs incomplete, iUsedBits:%"PRId64" > pBs->iBits:%d, MUST stop decoding.",
1620+ (int64_t) iUsedBits, pBs->iBits);
1621+ return -1;
10531622 }
1054−
10551623 return 0;
10561624 }
10571625
@@ -1069,6 +1637,21 @@ void WelsBlockFuncInit (SBlockFunc* pFunc, int32_t iCpu) { · 11 unmodified lines
10691637 pFunc->pWelsSetNonZeroCountFunc = SetNonZeroCount_AArch64_neon;
10701638 }
10711639 #endif
1640+
1641+ pFunc->pWelsBlockZero16x16Func = WelsBlockZero16x16_c;
1642+ pFunc->pWelsBlockZero8x8Func = WelsBlockZero8x8_c;
1643+ //TO DO add neon and X86
1644+#ifdef HAVE_NEON
1645+ if (iCpu & WELS_CPU_NEON) {
1646+
1647+ }
1648+#endif
1649+
1650+#ifdef HAVE_NEON_AARCH64
1651+ if (iCpu & WELS_CPU_NEON) {
1652+
1653+ }
1654+#endif
10721655 }
10731656
10741657 void SetNonZeroCount_c (int8_t* pNonZeroCount) {
@@ -1079,4 +1662,21 @@ void SetNonZeroCount_c (int8_t* pNonZeroCount) { · 4 unmodified lines
10791662 }
10801663 }
10811664
1665+void WelsBlockInit (int16_t* pBlock, int iW, int iH, int iStride, uint8_t uiVal) {
1666+ int32_t i;
1667+ int16_t* pDst = pBlock;
1668+
1669+ for (i = 0; i < iH; i++) {
1670+ memset (pDst, uiVal, iW * sizeof (int16_t));
1671+ pDst += iStride;
1672+ }
1673+}
1674+void WelsBlockZero16x16_c (int16_t* pBlock, int32_t iStride) {
1675+ WelsBlockInit (pBlock, 16, 16, iStride, 0);
1676+}
1677+
1678+void WelsBlockZero8x8_c (int16_t* pBlock, int32_t iStride) {
1679+ WelsBlockInit (pBlock, 8, 8, iStride, 0);
1680+}
1681+
10821682 } // namespace WelsDec

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t