History

B-frame decoding support for Main and High Profile with two test cases.

xiaotianshi2 committed 8 years ago

Commit 657d2171 parent e3e838355 files · +17936 −13825Browse files

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

Unified · Split

codec/decoder/core/src/decode_slice.cpp+2402−2027
@@ -1,2027 +1,2402 @@
1/*!1/*!
2 * \copy2 * \copy
3 * Copyright (c) 2008-2013, Cisco Systems3 * Copyright (c) 2008-2013, Cisco Systems
4 * All rights reserved.4 * All rights reserved.
5 *5 *
6 * Redistribution and use in source and binary forms, with or without6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions7 * modification, are permitted provided that the following conditions
8 * are met:8 * are met:
9 *9 *
10 * * Redistributions of source code must retain the above copyright10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.11 * notice, this list of conditions and the following disclaimer.
12 *12 *
13 * * Redistributions in binary form must reproduce the above copyright13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the15 * the documentation and/or other materials provided with the
16 * distribution.16 * distribution.
17 *17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,22 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT26 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE28 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.29 * POSSIBILITY OF SUCH DAMAGE.
30 *30 *
31 *31 *
32 * Abstract32 * Abstract
33 * current slice decoding33 * current slice decoding
34 *34 *
35 * History35 * History
36 * 07/10/2008 Created36 * 07/10/2008 Created
37 * 08/09/2013 Modified37 * 08/09/2013 Modified
38 *38 *
39 *****************************************************************************/39 *****************************************************************************/
40 40
41 41
42#include "deblocking.h"42#include "deblocking.h"
43 43
44#include "decode_slice.h"44#include "decode_slice.h"
45 45
46#include "parse_mb_syn_cavlc.h"46#include "parse_mb_syn_cavlc.h"
47#include "parse_mb_syn_cabac.h"47#include "parse_mb_syn_cabac.h"
48#include "rec_mb.h"48#include "rec_mb.h"
49#include "mv_pred.h"49#include "mv_pred.h"
50 50
51#include "cpu_core.h"51#include "cpu_core.h"
52 52
53namespace WelsDec {53namespace WelsDec {
54 54
55int32_t WelsTargetSliceConstruction (PWelsDecoderContext pCtx) {55static inline int32_t iAbs (int32_t x) {
56 PDqLayer pCurLayer = pCtx->pCurDqLayer;56 static const int32_t INT_BITS = (sizeof (int) * CHAR_BIT) - 1;
57 PSlice pCurSlice = &pCurLayer->sLayerInfo.sSliceInLayer;57 int32_t y = x >> INT_BITS;
58 PSliceHeader pSliceHeader = &pCurSlice->sSliceHeaderExt.sSliceHeader;58 return (x ^ y) - y;
59 59}
60 int32_t iTotalMbTargetLayer = pSliceHeader->pSps->uiTotalMbCount;60
61 61extern PPicture AllocPicture (PWelsDecoderContext pCtx, const int32_t kiPicWidth, const int32_t kiPicHeight);
62 int32_t iCurLayerWidth = pCurLayer->iMbWidth << 4;62
63 int32_t iCurLayerHeight = pCurLayer->iMbHeight << 4;63int32_t WelsTargetSliceConstruction (PWelsDecoderContext pCtx) {
64 64 PDqLayer pCurLayer = pCtx->pCurDqLayer;
65 int32_t iNextMbXyIndex = 0;65 PSlice pCurSlice = &pCurLayer->sLayerInfo.sSliceInLayer;
66 PFmo pFmo = pCtx->pFmo;66 PSliceHeader pSliceHeader = &pCurSlice->sSliceHeaderExt.sSliceHeader;
67 67
68 int32_t iTotalNumMb = pCurSlice->iTotalMbInCurSlice;68 int32_t iTotalMbTargetLayer = pSliceHeader->pSps->uiTotalMbCount;
69 int32_t iCountNumMb = 0;69
70 PDeblockingFilterMbFunc pDeblockMb;70 int32_t iCurLayerWidth = pCurLayer->iMbWidth << 4;
71 71 int32_t iCurLayerHeight = pCurLayer->iMbHeight << 4;
72 if (!pCtx->bAvcBasedFlag && iCurLayerWidth != pCtx->iCurSeqIntervalMaxPicWidth) {72
73 return ERR_INFO_WIDTH_MISMATCH;73 int32_t iNextMbXyIndex = 0;
74 }74 PFmo pFmo = pCtx->pFmo;
75 75
76 iNextMbXyIndex = pSliceHeader->iFirstMbInSlice;76 int32_t iTotalNumMb = pCurSlice->iTotalMbInCurSlice;
77 pCurLayer->iMbX = iNextMbXyIndex % pCurLayer->iMbWidth;77 int32_t iCountNumMb = 0;
78 pCurLayer->iMbY = iNextMbXyIndex / pCurLayer->iMbWidth;78 PDeblockingFilterMbFunc pDeblockMb;
79 pCurLayer->iMbXyIndex = iNextMbXyIndex;79
80 80 if (!pCtx->bAvcBasedFlag && iCurLayerWidth != pCtx->iCurSeqIntervalMaxPicWidth) {
81 if (0 == iNextMbXyIndex) {81 return ERR_INFO_WIDTH_MISMATCH;
82 pCurLayer->pDec->iSpsId = pCtx->pSps->iSpsId;82 }
83 pCurLayer->pDec->iPpsId = pCtx->pPps->iPpsId;83
84 84 iNextMbXyIndex = pSliceHeader->iFirstMbInSlice;
85 pCurLayer->pDec->uiQualityId = pCurLayer->sLayerInfo.sNalHeaderExt.uiQualityId;85 pCurLayer->iMbX = iNextMbXyIndex % pCurLayer->iMbWidth;
86 }86 pCurLayer->iMbY = iNextMbXyIndex / pCurLayer->iMbWidth;
87 87 pCurLayer->iMbXyIndex = iNextMbXyIndex;
88 do {88
89 if (iCountNumMb >= iTotalNumMb) {89 if (0 == iNextMbXyIndex) {
90 break;90 pCurLayer->pDec->iSpsId = pCtx->pSps->iSpsId;
91 }91 pCurLayer->pDec->iPpsId = pCtx->pPps->iPpsId;
92 92
93 if (!pCtx->pParam->bParseOnly) { //for parse only, actual recon MB unnecessary93 pCurLayer->pDec->uiQualityId = pCurLayer->sLayerInfo.sNalHeaderExt.uiQualityId;
94 if (WelsTargetMbConstruction (pCtx)) {94 }
95 WelsLog (& (pCtx->sLogCtx), WELS_LOG_WARNING,95
96 "WelsTargetSliceConstruction():::MB(%d, %d) construction error. pCurSlice_type:%d",96 do {
97 pCurLayer->iMbX, pCurLayer->iMbY, pCurSlice->eSliceType);97 if (iCountNumMb >= iTotalNumMb) {
98 98 break;
99 return ERR_INFO_MB_RECON_FAIL;99 }
100 }100
101 }101 if (!pCtx->pParam->bParseOnly) { //for parse only, actual recon MB unnecessary
102 102 if (WelsTargetMbConstruction (pCtx)) {
103 ++iCountNumMb;103 WelsLog (& (pCtx->sLogCtx), WELS_LOG_WARNING,
104 if (!pCurLayer->pMbCorrectlyDecodedFlag[iNextMbXyIndex]) { //already con-ed, overwrite104 "WelsTargetSliceConstruction():::MB(%d, %d) construction error. pCurSlice_type:%d",
105 pCurLayer->pMbCorrectlyDecodedFlag[iNextMbXyIndex] = true;105 pCurLayer->iMbX, pCurLayer->iMbY, pCurSlice->eSliceType);
106 pCtx->pDec->iMbEcedPropNum += (pCurLayer->pMbRefConcealedFlag[iNextMbXyIndex] ? 1 : 0);106
107 ++pCtx->iTotalNumMbRec;107 return ERR_INFO_MB_RECON_FAIL;
108 }108 }
109 109 }
110 if (pCtx->iTotalNumMbRec > iTotalMbTargetLayer) {110
111 WelsLog (& (pCtx->sLogCtx), WELS_LOG_WARNING,111 ++iCountNumMb;
112 "WelsTargetSliceConstruction():::pCtx->iTotalNumMbRec:%d, iTotalMbTargetLayer:%d",112 if (!pCurLayer->pMbCorrectlyDecodedFlag[iNextMbXyIndex]) { //already con-ed, overwrite
113 pCtx->iTotalNumMbRec, iTotalMbTargetLayer);113 pCurLayer->pMbCorrectlyDecodedFlag[iNextMbXyIndex] = true;
114 114 pCtx->pDec->iMbEcedPropNum += (pCurLayer->pMbRefConcealedFlag[iNextMbXyIndex] ? 1 : 0);
115 return ERR_INFO_MB_NUM_EXCEED_FAIL;115 ++pCtx->iTotalNumMbRec;
116 }116 }
117 117
118 if (pSliceHeader->pPps->uiNumSliceGroups > 1) {118 if (pCtx->iTotalNumMbRec > iTotalMbTargetLayer) {
119 iNextMbXyIndex = FmoNextMb (pFmo, iNextMbXyIndex);119 WelsLog (& (pCtx->sLogCtx), WELS_LOG_WARNING,
120 } else {120 "WelsTargetSliceConstruction():::pCtx->iTotalNumMbRec:%d, iTotalMbTargetLayer:%d",
121 ++iNextMbXyIndex;121 pCtx->iTotalNumMbRec, iTotalMbTargetLayer);
122 }122
123 if (-1 == iNextMbXyIndex || iNextMbXyIndex >= iTotalMbTargetLayer) { // slice group boundary or end of a frame123 return ERR_INFO_MB_NUM_EXCEED_FAIL;
124 break;124 }
125 }125
126 pCurLayer->iMbX = iNextMbXyIndex % pCurLayer->iMbWidth;126 if (pSliceHeader->pPps->uiNumSliceGroups > 1) {
127 pCurLayer->iMbY = iNextMbXyIndex / pCurLayer->iMbWidth;127 iNextMbXyIndex = FmoNextMb (pFmo, iNextMbXyIndex);
128 pCurLayer->iMbXyIndex = iNextMbXyIndex;128 } else {
129 } while (1);129 ++iNextMbXyIndex;
130 130 }
131 pCtx->pDec->iWidthInPixel = iCurLayerWidth;131 if (-1 == iNextMbXyIndex || iNextMbXyIndex >= iTotalMbTargetLayer) { // slice group boundary or end of a frame
132 pCtx->pDec->iHeightInPixel = iCurLayerHeight;132 break;
133 133 }
134 if ((pCurSlice->eSliceType != I_SLICE) && (pCurSlice->eSliceType != P_SLICE))134 pCurLayer->iMbX = iNextMbXyIndex % pCurLayer->iMbWidth;
135 return ERR_NONE; //no error but just ignore the type unsupported135 pCurLayer->iMbY = iNextMbXyIndex / pCurLayer->iMbWidth;
136 136 pCurLayer->iMbXyIndex = iNextMbXyIndex;
137 if (pCtx->pParam->bParseOnly) //for parse only, deblocking should not go on137 } while (1);
138 return ERR_NONE;138
139 139 pCtx->pDec->iWidthInPixel = iCurLayerWidth;
140 pDeblockMb = WelsDeblockingMb;140 pCtx->pDec->iHeightInPixel = iCurLayerHeight;
141 141
142 if (1 == pSliceHeader->uiDisableDeblockingFilterIdc142 if ((pCurSlice->eSliceType != I_SLICE) && (pCurSlice->eSliceType != P_SLICE) && (pCurSlice->eSliceType != B_SLICE))
143 || pCtx->pCurDqLayer->sLayerInfo.sSliceInLayer.iTotalMbInCurSlice <= 0) {143 return ERR_NONE; //no error but just ignore the type unsupported
144 return ERR_NONE;//NO_SUPPORTED_FILTER_IDX144
145 } else {145 if (pCtx->pParam->bParseOnly) //for parse only, deblocking should not go on
146 WelsDeblockingFilterSlice (pCtx, pDeblockMb);146 return ERR_NONE;
147 }147
148 // any other filter_idc not supported here, 7/22/2010148 pDeblockMb = WelsDeblockingMb;
149 149
150 return ERR_NONE;150 if (1 == pSliceHeader->uiDisableDeblockingFilterIdc
151}151 || pCtx->pCurDqLayer->sLayerInfo.sSliceInLayer.iTotalMbInCurSlice <= 0) {
152 152 return ERR_NONE;//NO_SUPPORTED_FILTER_IDX
153int32_t WelsMbInterSampleConstruction (PWelsDecoderContext pCtx, PDqLayer pCurLayer,153 } else {
154 uint8_t* pDstY, uint8_t* pDstU, uint8_t* pDstV, int32_t iStrideL, int32_t iStrideC) {154 WelsDeblockingFilterSlice (pCtx, pDeblockMb);
155 int32_t iMbXy = pCurLayer->iMbXyIndex;155 }
156 int32_t i, iIndex, iOffset;156 // any other filter_idc not supported here, 7/22/2010
157 157
158 if (pCurLayer->pTransformSize8x8Flag[iMbXy]) {158 return ERR_NONE;
159 for (i = 0; i < 4; i++) {159}
160 iIndex = g_kuiMbCountScan4Idx[i << 2];160
161 if (pCurLayer->pNzc[iMbXy][iIndex] || pCurLayer->pNzc[iMbXy][iIndex + 1] || pCurLayer->pNzc[iMbXy][iIndex + 4]161int32_t WelsMbInterSampleConstruction (PWelsDecoderContext pCtx, PDqLayer pCurLayer,
162 || pCurLayer->pNzc[iMbXy][iIndex + 5]) {162 uint8_t* pDstY, uint8_t* pDstU, uint8_t* pDstV, int32_t iStrideL, int32_t iStrideC) {
163 iOffset = ((iIndex >> 2) << 2) * iStrideL + ((iIndex % 4) << 2);163 int32_t iMbXy = pCurLayer->iMbXyIndex;
164 pCtx->pIdctResAddPredFunc8x8 (pDstY + iOffset, iStrideL, pCurLayer->pScaledTCoeff[iMbXy] + (i << 6));164 int32_t i, iIndex, iOffset;
165 }165
166 }166 if (pCurLayer->pTransformSize8x8Flag[iMbXy]) {
167 } else {167 for (i = 0; i < 4; i++) {
168 // luma.168 iIndex = g_kuiMbCountScan4Idx[i << 2];
169 const int8_t* pNzc = pCurLayer->pNzc[iMbXy];169 if (pCurLayer->pNzc[iMbXy][iIndex] || pCurLayer->pNzc[iMbXy][iIndex + 1] || pCurLayer->pNzc[iMbXy][iIndex + 4]
170 int16_t* pScaledTCoeff = pCurLayer->pScaledTCoeff[iMbXy];170 || pCurLayer->pNzc[iMbXy][iIndex + 5]) {
171 pCtx->pIdctFourResAddPredFunc (pDstY + 0 * iStrideL + 0, iStrideL, pScaledTCoeff + 0 * 64, pNzc + 0);171 iOffset = ((iIndex >> 2) << 2) * iStrideL + ((iIndex % 4) << 2);
172 pCtx->pIdctFourResAddPredFunc (pDstY + 0 * iStrideL + 8, iStrideL, pScaledTCoeff + 1 * 64, pNzc + 2);172 pCtx->pIdctResAddPredFunc8x8 (pDstY + iOffset, iStrideL, pCurLayer->pScaledTCoeff[iMbXy] + (i << 6));
173 pCtx->pIdctFourResAddPredFunc (pDstY + 8 * iStrideL + 0, iStrideL, pScaledTCoeff + 2 * 64, pNzc + 8);173 }
174 pCtx->pIdctFourResAddPredFunc (pDstY + 8 * iStrideL + 8, iStrideL, pScaledTCoeff + 3 * 64, pNzc + 10);174 }
175 }175 } else {
176 176 // luma.
177 const int8_t* pNzc = pCurLayer->pNzc[iMbXy];177 const int8_t* pNzc = pCurLayer->pNzc[iMbXy];
178 int16_t* pScaledTCoeff = pCurLayer->pScaledTCoeff[iMbXy];178 int16_t* pScaledTCoeff = pCurLayer->pScaledTCoeff[iMbXy];
179 // Cb.179 pCtx->pIdctFourResAddPredFunc (pDstY + 0 * iStrideL + 0, iStrideL, pScaledTCoeff + 0 * 64, pNzc + 0);
180 pCtx->pIdctFourResAddPredFunc (pDstU, iStrideC, pScaledTCoeff + 4 * 64, pNzc + 16);180 pCtx->pIdctFourResAddPredFunc (pDstY + 0 * iStrideL + 8, iStrideL, pScaledTCoeff + 1 * 64, pNzc + 2);
181 // Cr.181 pCtx->pIdctFourResAddPredFunc (pDstY + 8 * iStrideL + 0, iStrideL, pScaledTCoeff + 2 * 64, pNzc + 8);
182 pCtx->pIdctFourResAddPredFunc (pDstV, iStrideC, pScaledTCoeff + 5 * 64, pNzc + 18);182 pCtx->pIdctFourResAddPredFunc (pDstY + 8 * iStrideL + 8, iStrideL, pScaledTCoeff + 3 * 64, pNzc + 10);
183 183 }
184 return ERR_NONE;184
185}185 const int8_t* pNzc = pCurLayer->pNzc[iMbXy];
186int32_t WelsMbInterConstruction (PWelsDecoderContext pCtx, PDqLayer pCurLayer) {186 int16_t* pScaledTCoeff = pCurLayer->pScaledTCoeff[iMbXy];
187 int32_t iMbX = pCurLayer->iMbX;187 // Cb.
188 int32_t iMbY = pCurLayer->iMbY;188 pCtx->pIdctFourResAddPredFunc (pDstU, iStrideC, pScaledTCoeff + 4 * 64, pNzc + 16);
189 uint8_t* pDstY, *pDstCb, *pDstCr;189 // Cr.
190 190 pCtx->pIdctFourResAddPredFunc (pDstV, iStrideC, pScaledTCoeff + 5 * 64, pNzc + 18);
191 int32_t iLumaStride = pCtx->pDec->iLinesize[0];191
192 int32_t iChromaStride = pCtx->pDec->iLinesize[1];192 return ERR_NONE;
193 193}
194 pDstY = pCurLayer->pDec->pData[0] + ((iMbY * iLumaStride + iMbX) << 4);194int32_t WelsMbInterConstruction (PWelsDecoderContext pCtx, PDqLayer pCurLayer) {
195 pDstCb = pCurLayer->pDec->pData[1] + ((iMbY * iChromaStride + iMbX) << 3);195 int32_t iMbX = pCurLayer->iMbX;
196 pDstCr = pCurLayer->pDec->pData[2] + ((iMbY * iChromaStride + iMbX) << 3);196 int32_t iMbY = pCurLayer->iMbY;
197 197 uint8_t* pDstY, *pDstCb, *pDstCr;
198 GetInterPred (pDstY, pDstCb, pDstCr, pCtx);198
199 WelsMbInterSampleConstruction (pCtx, pCurLayer, pDstY, pDstCb, pDstCr, iLumaStride, iChromaStride);199 int32_t iLumaStride = pCtx->pDec->iLinesize[0];
200 200 int32_t iChromaStride = pCtx->pDec->iLinesize[1];
201 pCtx->sBlockFunc.pWelsSetNonZeroCountFunc (201
202 pCurLayer->pNzc[pCurLayer->iMbXyIndex]); // set all none-zero nzc to 1; dbk can be opti!202 pDstY = pCurLayer->pDec->pData[0] + ((iMbY * iLumaStride + iMbX) << 4);
203 return ERR_NONE;203 pDstCb = pCurLayer->pDec->pData[1] + ((iMbY * iChromaStride + iMbX) << 3);
204}204 pDstCr = pCurLayer->pDec->pData[2] + ((iMbY * iChromaStride + iMbX) << 3);
205 205
206void WelsLumaDcDequantIdct (int16_t* pBlock, int32_t iQp, PWelsDecoderContext pCtx) {206 if (pCtx->eSliceType == P_SLICE) {
207 const int32_t kiQMul = pCtx->bUseScalingList ? pCtx->pDequant_coeff4x4[0][iQp][0] : (g_kuiDequantCoeff[iQp][0] << 4);207 GetInterPred (pDstY, pDstCb, pDstCr, pCtx);
208#define STRIDE 16208 } else {
209 int32_t i;209 if (pCtx->pTempDec == NULL)
210 int32_t iTemp[16]; //FIXME check if this is a good idea210 pCtx->pTempDec = AllocPicture (pCtx, pCtx->pSps->iMbWidth << 4, pCtx->pSps->iMbHeight << 4);
211 int16_t* pBlk = pBlock;211 uint8_t* pTempDstYCbCr[3];
212 static const int32_t kiXOffset[4] = {0, STRIDE, STRIDE << 2, 5 * STRIDE};212 uint8_t* pDstYCbCr[3];
213 static const int32_t kiYOffset[4] = {0, STRIDE << 1, STRIDE << 3, 10 * STRIDE};213 pTempDstYCbCr[0] = pCtx->pTempDec->pData[0] + ((iMbY * iLumaStride + iMbX) << 4);
214 214 pTempDstYCbCr[1] = pCtx->pTempDec->pData[1] + ((iMbY * iChromaStride + iMbX) << 3);
215 for (i = 0; i < 4; i++) {215 pTempDstYCbCr[2] = pCtx->pTempDec->pData[2] + ((iMbY * iChromaStride + iMbX) << 3);
216 const int32_t kiOffset = kiYOffset[i];216 pDstYCbCr[0] = pDstY;
217 const int32_t kiX1 = kiOffset + kiXOffset[2];217 pDstYCbCr[1] = pDstCb;
218 const int32_t kiX2 = STRIDE + kiOffset;218 pDstYCbCr[2] = pDstCr;
219 const int32_t kiX3 = kiOffset + kiXOffset[3];219 GetInterBPred (pDstYCbCr, pTempDstYCbCr, pCtx);
220 const int32_t kiI4 = i << 2; // 4*i220 }
221 const int32_t kiZ0 = pBlk[kiOffset] + pBlk[kiX1];221 WelsMbInterSampleConstruction (pCtx, pCurLayer, pDstY, pDstCb, pDstCr, iLumaStride, iChromaStride);
222 const int32_t kiZ1 = pBlk[kiOffset] - pBlk[kiX1];222
223 const int32_t kiZ2 = pBlk[kiX2] - pBlk[kiX3];223 pCtx->sBlockFunc.pWelsSetNonZeroCountFunc (
224 const int32_t kiZ3 = pBlk[kiX2] + pBlk[kiX3];224 pCurLayer->pNzc[pCurLayer->iMbXyIndex]); // set all none-zero nzc to 1; dbk can be opti!
225 225 return ERR_NONE;
226 iTemp[kiI4] = kiZ0 + kiZ3;226}
227 iTemp[1 + kiI4] = kiZ1 + kiZ2;227
228 iTemp[2 + kiI4] = kiZ1 - kiZ2;228void WelsLumaDcDequantIdct (int16_t* pBlock, int32_t iQp, PWelsDecoderContext pCtx) {
229 iTemp[3 + kiI4] = kiZ0 - kiZ3;229 const int32_t kiQMul = pCtx->bUseScalingList ? pCtx->pDequant_coeff4x4[0][iQp][0] : (g_kuiDequantCoeff[iQp][0] << 4);
230 }230#define STRIDE 16
231 231 int32_t i;
232 for (i = 0; i < 4; i++) {232 int32_t iTemp[16]; //FIXME check if this is a good idea
233 const int32_t kiOffset = kiXOffset[i];233 int16_t* pBlk = pBlock;
234 const int32_t kiI4 = 4 + i;234 static const int32_t kiXOffset[4] = {0, STRIDE, STRIDE << 2, 5 * STRIDE};
235 const int32_t kiZ0 = iTemp[i] + iTemp[4 + kiI4];235 static const int32_t kiYOffset[4] = {0, STRIDE << 1, STRIDE << 3, 10 * STRIDE};
236 const int32_t kiZ1 = iTemp[i] - iTemp[4 + kiI4];236
237 const int32_t kiZ2 = iTemp[kiI4] - iTemp[8 + kiI4];237 for (i = 0; i < 4; i++) {
238 const int32_t kiZ3 = iTemp[kiI4] + iTemp[8 + kiI4];238 const int32_t kiOffset = kiYOffset[i];
239 239 const int32_t kiX1 = kiOffset + kiXOffset[2];
240 pBlk[kiOffset] = ((kiZ0 + kiZ3) * kiQMul + (1 << 5)) >> 6; //FIXME think about merging this into decode_resdual240 const int32_t kiX2 = STRIDE + kiOffset;
241 pBlk[kiYOffset[1] + kiOffset] = ((kiZ1 + kiZ2) * kiQMul + (1 << 5)) >> 6;241 const int32_t kiX3 = kiOffset + kiXOffset[3];
242 pBlk[kiYOffset[2] + kiOffset] = ((kiZ1 - kiZ2) * kiQMul + (1 << 5)) >> 6;242 const int32_t kiI4 = i << 2; // 4*i
243 pBlk[kiYOffset[3] + kiOffset] = ((kiZ0 - kiZ3) * kiQMul + (1 << 5)) >> 6;243 const int32_t kiZ0 = pBlk[kiOffset] + pBlk[kiX1];
244 }244 const int32_t kiZ1 = pBlk[kiOffset] - pBlk[kiX1];
245#undef STRIDE245 const int32_t kiZ2 = pBlk[kiX2] - pBlk[kiX3];
246}246 const int32_t kiZ3 = pBlk[kiX2] + pBlk[kiX3];
247 247
248int32_t WelsMbIntraPredictionConstruction (PWelsDecoderContext pCtx, PDqLayer pCurLayer, bool bOutput) {248 iTemp[kiI4] = kiZ0 + kiZ3;
249//seems IPCM should not enter this path249 iTemp[1 + kiI4] = kiZ1 + kiZ2;
250 int32_t iMbXy = pCurLayer->iMbXyIndex;250 iTemp[2 + kiI4] = kiZ1 - kiZ2;
251 251 iTemp[3 + kiI4] = kiZ0 - kiZ3;
252 WelsFillRecNeededMbInfo (pCtx, bOutput, pCurLayer);252 }
253 253
254 if (IS_INTRA16x16 (pCurLayer->pMbType[iMbXy])) {254 for (i = 0; i < 4; i++) {
255 RecI16x16Mb (iMbXy, pCtx, pCurLayer->pScaledTCoeff[iMbXy], pCurLayer);255 const int32_t kiOffset = kiXOffset[i];
256 } else if (IS_INTRA8x8 (pCurLayer->pMbType[iMbXy])) {256 const int32_t kiI4 = 4 + i;
257 RecI8x8Mb (iMbXy, pCtx, pCurLayer->pScaledTCoeff[iMbXy], pCurLayer);257 const int32_t kiZ0 = iTemp[i] + iTemp[4 + kiI4];
258 } else if (IS_INTRA4x4 (pCurLayer->pMbType[iMbXy])) {258 const int32_t kiZ1 = iTemp[i] - iTemp[4 + kiI4];
259 RecI4x4Mb (iMbXy, pCtx, pCurLayer->pScaledTCoeff[iMbXy], pCurLayer);259 const int32_t kiZ2 = iTemp[kiI4] - iTemp[8 + kiI4];
260 }260 const int32_t kiZ3 = iTemp[kiI4] + iTemp[8 + kiI4];
261 return ERR_NONE;261
262}262 pBlk[kiOffset] = ((kiZ0 + kiZ3) * kiQMul + (1 << 5)) >> 6; //FIXME think about merging this into decode_resdual
263 263 pBlk[kiYOffset[1] + kiOffset] = ((kiZ1 + kiZ2) * kiQMul + (1 << 5)) >> 6;
264int32_t WelsMbInterPrediction (PWelsDecoderContext pCtx, PDqLayer pCurLayer) {264 pBlk[kiYOffset[2] + kiOffset] = ((kiZ1 - kiZ2) * kiQMul + (1 << 5)) >> 6;
265 int32_t iMbX = pCurLayer->iMbX;265 pBlk[kiYOffset[3] + kiOffset] = ((kiZ0 - kiZ3) * kiQMul + (1 << 5)) >> 6;
266 int32_t iMbY = pCurLayer->iMbY;266 }
267 uint8_t* pDstY, *pDstCb, *pDstCr;267#undef STRIDE
268 268}
269 int32_t iLumaStride = pCtx->pDec->iLinesize[0];269
270 int32_t iChromaStride = pCtx->pDec->iLinesize[1];270int32_t WelsMbIntraPredictionConstruction (PWelsDecoderContext pCtx, PDqLayer pCurLayer, bool bOutput) {
271 271//seems IPCM should not enter this path
272 pDstY = pCurLayer->pDec->pData[0] + ((iMbY * iLumaStride + iMbX) << 4);272 int32_t iMbXy = pCurLayer->iMbXyIndex;
273 pDstCb = pCurLayer->pDec->pData[1] + ((iMbY * iChromaStride + iMbX) << 3);273
274 pDstCr = pCurLayer->pDec->pData[2] + ((iMbY * iChromaStride + iMbX) << 3);274 WelsFillRecNeededMbInfo (pCtx, bOutput, pCurLayer);
275 275
276 GetInterPred (pDstY, pDstCb, pDstCr, pCtx);276 if (IS_INTRA16x16 (pCurLayer->pMbType[iMbXy])) {
277 277 RecI16x16Mb (iMbXy, pCtx, pCurLayer->pScaledTCoeff[iMbXy], pCurLayer);
278 return ERR_NONE;278 } else if (IS_INTRA8x8 (pCurLayer->pMbType[iMbXy])) {
279}279 RecI8x8Mb (iMbXy, pCtx, pCurLayer->pScaledTCoeff[iMbXy], pCurLayer);
280 280 } else if (IS_INTRA4x4 (pCurLayer->pMbType[iMbXy])) {
281int32_t WelsTargetMbConstruction (PWelsDecoderContext pCtx) {281 RecI4x4Mb (iMbXy, pCtx, pCurLayer->pScaledTCoeff[iMbXy], pCurLayer);
282 PDqLayer pCurLayer = pCtx->pCurDqLayer;282 }
283 if (MB_TYPE_INTRA_PCM == pCurLayer->pMbType[pCurLayer->iMbXyIndex]) {283 return ERR_NONE;
284 //already decoded and reconstructed when parsing284}
285 return ERR_NONE;285
286 } else if (IS_INTRA (pCurLayer->pMbType[pCurLayer->iMbXyIndex])) {286int32_t WelsMbInterPrediction (PWelsDecoderContext pCtx, PDqLayer pCurLayer) {
287 WelsMbIntraPredictionConstruction (pCtx, pCurLayer, 1);287 int32_t iMbX = pCurLayer->iMbX;
288 } else if (IS_INTER (pCurLayer->pMbType[pCurLayer->iMbXyIndex])) { //InterMB288 int32_t iMbY = pCurLayer->iMbY;
289 if (0 == pCurLayer->pCbp[pCurLayer->iMbXyIndex]) { //uiCbp==0 include SKIP289 uint8_t* pDstY, *pDstCb, *pDstCr;
290 WelsMbInterPrediction (pCtx, pCurLayer);290
291 } else {291 int32_t iLumaStride = pCtx->pDec->iLinesize[0];
292 WelsMbInterConstruction (pCtx, pCurLayer);292 int32_t iChromaStride = pCtx->pDec->iLinesize[1];
293 }293
294 } else {294 pDstY = pCurLayer->pDec->pData[0] + ((iMbY * iLumaStride + iMbX) << 4);
295 WelsLog (& (pCtx->sLogCtx), WELS_LOG_WARNING, "WelsTargetMbConstruction():::::Unknown MB type: %d",295 pDstCb = pCurLayer->pDec->pData[1] + ((iMbY * iChromaStride + iMbX) << 3);
296 pCurLayer->pMbType[pCurLayer->iMbXyIndex]);296 pDstCr = pCurLayer->pDec->pData[2] + ((iMbY * iChromaStride + iMbX) << 3);
297 return ERR_INFO_MB_RECON_FAIL;297
298 }298 if (pCtx->eSliceType == P_SLICE) {
299 299 GetInterPred (pDstY, pDstCb, pDstCr, pCtx);
300 return ERR_NONE;300 } else {
301}301 if (pCtx->pTempDec == NULL)
302 302 pCtx->pTempDec = AllocPicture (pCtx, pCtx->pSps->iMbWidth << 4, pCtx->pSps->iMbHeight << 4);
303void WelsChromaDcIdct (int16_t* pBlock) {303 uint8_t* pTempDstYCbCr[3];
304 int32_t iStride = 32;304 uint8_t* pDstYCbCr[3];
305 int32_t iXStride = 16;305 pTempDstYCbCr[0] = pCtx->pTempDec->pData[0] + ((iMbY * iLumaStride + iMbX) << 4);
306 int32_t iStride1 = iXStride + iStride;306 pTempDstYCbCr[1] = pCtx->pTempDec->pData[1] + ((iMbY * iChromaStride + iMbX) << 3);
307 int16_t* pBlk = pBlock;307 pTempDstYCbCr[2] = pCtx->pTempDec->pData[2] + ((iMbY * iChromaStride + iMbX) << 3);
308 int32_t iA, iB, iC, iD, iE;308 pDstYCbCr[0] = pDstY;
309 309 pDstYCbCr[1] = pDstCb;
310 iA = pBlk[0];310 pDstYCbCr[2] = pDstCr;
311 iB = pBlk[iXStride];311 GetInterBPred (pDstYCbCr, pTempDstYCbCr, pCtx);
312 iC = pBlk[iStride];312 }
313 iD = pBlk[iStride1];313 return ERR_NONE;
314 314}
315 iE = iA - iB;315
316 iA += iB;316int32_t WelsTargetMbConstruction (PWelsDecoderContext pCtx) {
317 iB = iC - iD;317 PDqLayer pCurLayer = pCtx->pCurDqLayer;
318 iC += iD;318 if (MB_TYPE_INTRA_PCM == pCurLayer->pMbType[pCurLayer->iMbXyIndex]) {
319 319 //already decoded and reconstructed when parsing
320 pBlk[0] = (iA + iC);320 return ERR_NONE;
321 pBlk[iXStride] = (iE + iB);321 } else if (IS_INTRA (pCurLayer->pMbType[pCurLayer->iMbXyIndex])) {
322 pBlk[iStride] = (iA - iC);322 WelsMbIntraPredictionConstruction (pCtx, pCurLayer, 1);
323 pBlk[iStride1] = (iE - iB);323 } else if (IS_INTER (pCurLayer->pMbType[pCurLayer->iMbXyIndex])) { //InterMB
324}324 if (0 == pCurLayer->pCbp[pCurLayer->iMbXyIndex]) { //uiCbp==0 include SKIP
325 325 WelsMbInterPrediction (pCtx, pCurLayer);
326void WelsMapNxNNeighToSampleNormal (PWelsNeighAvail pNeighAvail, int32_t* pSampleAvail) {326 } else {
327 if (pNeighAvail->iLeftAvail) { //left327 WelsMbInterConstruction (pCtx, pCurLayer);
328 pSampleAvail[ 6] =328 }
329 pSampleAvail[12] =329 } else {
330 pSampleAvail[18] =330 WelsLog (& (pCtx->sLogCtx), WELS_LOG_WARNING, "WelsTargetMbConstruction():::::Unknown MB type: %d",
331 pSampleAvail[24] = 1;331 pCurLayer->pMbType[pCurLayer->iMbXyIndex]);
332 }332 return ERR_INFO_MB_RECON_FAIL;
333 if (pNeighAvail->iLeftTopAvail) { //top_left333 }
334 pSampleAvail[0] = 1;334
335 }335 return ERR_NONE;
336 if (pNeighAvail->iTopAvail) { //top336}
337 pSampleAvail[1] =337
338 pSampleAvail[2] =338void WelsChromaDcIdct (int16_t* pBlock) {
339 pSampleAvail[3] =339 int32_t iStride = 32;
340 pSampleAvail[4] = 1;340 int32_t iXStride = 16;
341 }341 int32_t iStride1 = iXStride + iStride;
342 if (pNeighAvail->iRightTopAvail) { //top_right342 int16_t* pBlk = pBlock;
343 pSampleAvail[5] = 1;343 int32_t iA, iB, iC, iD, iE;
344 }344
345}345 iA = pBlk[0];
346 346 iB = pBlk[iXStride];
347void WelsMapNxNNeighToSampleConstrain1 (PWelsNeighAvail pNeighAvail, int32_t* pSampleAvail) {347 iC = pBlk[iStride];
348 if (pNeighAvail->iLeftAvail && IS_INTRA (pNeighAvail->iLeftType)) { //left348 iD = pBlk[iStride1];
349 pSampleAvail[ 6] =349
350 pSampleAvail[12] =350 iE = iA - iB;
351 pSampleAvail[18] =351 iA += iB;
352 pSampleAvail[24] = 1;352 iB = iC - iD;
353 }353 iC += iD;
354 if (pNeighAvail->iLeftTopAvail && IS_INTRA (pNeighAvail->iLeftTopType)) { //top_left354
355 pSampleAvail[0] = 1;355 pBlk[0] = (iA + iC);
356 }356 pBlk[iXStride] = (iE + iB);
357 if (pNeighAvail->iTopAvail && IS_INTRA (pNeighAvail->iTopType)) { //top357 pBlk[iStride] = (iA - iC);
358 pSampleAvail[1] =358 pBlk[iStride1] = (iE - iB);
359 pSampleAvail[2] =359}
360 pSampleAvail[3] =360
361 pSampleAvail[4] = 1;361void WelsMapNxNNeighToSampleNormal (PWelsNeighAvail pNeighAvail, int32_t* pSampleAvail) {
362 }362 if (pNeighAvail->iLeftAvail) { //left
363 if (pNeighAvail->iRightTopAvail && IS_INTRA (pNeighAvail->iRightTopType)) { //top_right363 pSampleAvail[ 6] =
364 pSampleAvail[5] = 1;364 pSampleAvail[12] =
365 }365 pSampleAvail[18] =
366}366 pSampleAvail[24] = 1;
367void WelsMap16x16NeighToSampleNormal (PWelsNeighAvail pNeighAvail, uint8_t* pSampleAvail) {367 }
368 if (pNeighAvail->iLeftAvail) {368 if (pNeighAvail->iLeftTopAvail) { //top_left
369 *pSampleAvail = (1 << 2);369 pSampleAvail[0] = 1;
370 }370 }
371 if (pNeighAvail->iLeftTopAvail) {371 if (pNeighAvail->iTopAvail) { //top
372 *pSampleAvail |= (1 << 1);372 pSampleAvail[1] =
373 }373 pSampleAvail[2] =
374 if (pNeighAvail->iTopAvail) {374 pSampleAvail[3] =
375 *pSampleAvail |= 1;375 pSampleAvail[4] = 1;
376 }376 }
377}377 if (pNeighAvail->iRightTopAvail) { //top_right
378 378 pSampleAvail[5] = 1;
379void WelsMap16x16NeighToSampleConstrain1 (PWelsNeighAvail pNeighAvail, uint8_t* pSampleAvail) {379 }
380 if (pNeighAvail->iLeftAvail && IS_INTRA (pNeighAvail->iLeftType)) {380}
381 *pSampleAvail = (1 << 2);381
382 }382void WelsMapNxNNeighToSampleConstrain1 (PWelsNeighAvail pNeighAvail, int32_t* pSampleAvail) {
383 if (pNeighAvail->iLeftTopAvail && IS_INTRA (pNeighAvail->iLeftTopType)) {383 if (pNeighAvail->iLeftAvail && IS_INTRA (pNeighAvail->iLeftType)) { //left
384 *pSampleAvail |= (1 << 1);384 pSampleAvail[ 6] =
385 }385 pSampleAvail[12] =
386 if (pNeighAvail->iTopAvail && IS_INTRA (pNeighAvail->iTopType)) {386 pSampleAvail[18] =
387 *pSampleAvail |= 1;387 pSampleAvail[24] = 1;
388 }388 }
389}389 if (pNeighAvail->iLeftTopAvail && IS_INTRA (pNeighAvail->iLeftTopType)) { //top_left
390 390 pSampleAvail[0] = 1;
391int32_t ParseIntra4x4Mode (PWelsDecoderContext pCtx, PWelsNeighAvail pNeighAvail, int8_t* pIntraPredMode,391 }
392 PBitStringAux pBs,392 if (pNeighAvail->iTopAvail && IS_INTRA (pNeighAvail->iTopType)) { //top
393 PDqLayer pCurDqLayer) {393 pSampleAvail[1] =
394 int32_t iSampleAvail[5 * 6] = { 0 }; //initialize as 0394 pSampleAvail[2] =
395 int32_t iMbXy = pCurDqLayer->iMbXyIndex;395 pSampleAvail[3] =
396 int32_t iFinalMode, i;396 pSampleAvail[4] = 1;
397 397 }
398 uint8_t uiNeighAvail = 0;398 if (pNeighAvail->iRightTopAvail && IS_INTRA (pNeighAvail->iRightTopType)) { //top_right
399 uint32_t uiCode;399 pSampleAvail[5] = 1;
400 int32_t iCode;400 }
401 pCtx->pMapNxNNeighToSampleFunc (pNeighAvail, iSampleAvail);401}
402 uiNeighAvail = (iSampleAvail[6] << 2) | (iSampleAvail[0] << 1) | (iSampleAvail[1]);402void WelsMap16x16NeighToSampleNormal (PWelsNeighAvail pNeighAvail, uint8_t* pSampleAvail) {
403 for (i = 0; i < 16; i++) {403 if (pNeighAvail->iLeftAvail) {
404 int32_t iPrevIntra4x4PredMode = 0;404 *pSampleAvail = (1 << 2);
405 if (pCurDqLayer->sLayerInfo.pPps->bEntropyCodingModeFlag) {405 }
406 WELS_READ_VERIFY (ParseIntraPredModeLumaCabac (pCtx, iCode));406 if (pNeighAvail->iLeftTopAvail) {
407 iPrevIntra4x4PredMode = iCode;407 *pSampleAvail |= (1 << 1);
408 } else {408 }
409 WELS_READ_VERIFY (BsGetOneBit (pBs, &uiCode));409 if (pNeighAvail->iTopAvail) {
410 iPrevIntra4x4PredMode = uiCode;410 *pSampleAvail |= 1;
411 }411 }
412 const int32_t kiPredMode = PredIntra4x4Mode (pIntraPredMode, i);412}
413 413
414 int8_t iBestMode;414void WelsMap16x16NeighToSampleConstrain1 (PWelsNeighAvail pNeighAvail, uint8_t* pSampleAvail) {
415 if (pCurDqLayer->sLayerInfo.pPps->bEntropyCodingModeFlag) {415 if (pNeighAvail->iLeftAvail && IS_INTRA (pNeighAvail->iLeftType)) {
416 if (iPrevIntra4x4PredMode == -1)416 *pSampleAvail = (1 << 2);
417 iBestMode = kiPredMode;417 }
418 else418 if (pNeighAvail->iLeftTopAvail && IS_INTRA (pNeighAvail->iLeftTopType)) {
419 iBestMode = iPrevIntra4x4PredMode + (iPrevIntra4x4PredMode >= kiPredMode);419 *pSampleAvail |= (1 << 1);
420 } else {420 }
421 if (iPrevIntra4x4PredMode) {421 if (pNeighAvail->iTopAvail && IS_INTRA (pNeighAvail->iTopType)) {
422 iBestMode = kiPredMode;422 *pSampleAvail |= 1;
423 } else {423 }
424 WELS_READ_VERIFY (BsGetBits (pBs, 3, &uiCode));424}
425 iBestMode = uiCode + ((int32_t) uiCode >= kiPredMode);425
426 }426int32_t ParseIntra4x4Mode (PWelsDecoderContext pCtx, PWelsNeighAvail pNeighAvail, int8_t* pIntraPredMode,
427 }427 PBitStringAux pBs,
428 428 PDqLayer pCurDqLayer) {
429 iFinalMode = CheckIntraNxNPredMode (&iSampleAvail[0], &iBestMode, i, false);429 int32_t iSampleAvail[5 * 6] = { 0 }; //initialize as 0
430 if (iFinalMode == GENERATE_ERROR_NO (ERR_LEVEL_MB_DATA, ERR_INVALID_INTRA4X4_MODE)) {430 int32_t iMbXy = pCurDqLayer->iMbXyIndex;
431 return GENERATE_ERROR_NO (ERR_LEVEL_MB_DATA, ERR_INFO_INVALID_I4x4_PRED_MODE);431 int32_t iFinalMode, i;
432 }432
433 433 uint8_t uiNeighAvail = 0;
434 pCurDqLayer->pIntra4x4FinalMode[iMbXy][g_kuiScan4[i]] = iFinalMode;434 uint32_t uiCode;
435 435 int32_t iCode;
436 pIntraPredMode[g_kuiScan8[i]] = iBestMode;436 pCtx->pMapNxNNeighToSampleFunc (pNeighAvail, iSampleAvail);
437 437 uiNeighAvail = (iSampleAvail[6] << 2) | (iSampleAvail[0] << 1) | (iSampleAvail[1]);
438 iSampleAvail[g_kuiCache30ScanIdx[i]] = 1;438 for (i = 0; i < 16; i++) {
439 }439 int32_t iPrevIntra4x4PredMode = 0;
440 ST32 (&pCurDqLayer->pIntraPredMode[iMbXy][0], LD32 (&pIntraPredMode[1 + 8 * 4]));440 if (pCurDqLayer->sLayerInfo.pPps->bEntropyCodingModeFlag) {
441 pCurDqLayer->pIntraPredMode[iMbXy][4] = pIntraPredMode[4 + 8 * 1];441 WELS_READ_VERIFY (ParseIntraPredModeLumaCabac (pCtx, iCode));
442 pCurDqLayer->pIntraPredMode[iMbXy][5] = pIntraPredMode[4 + 8 * 2];442 iPrevIntra4x4PredMode = iCode;
443 pCurDqLayer->pIntraPredMode[iMbXy][6] = pIntraPredMode[4 + 8 * 3];443 } else {
444 444 WELS_READ_VERIFY (BsGetOneBit (pBs, &uiCode));
445 if (pCtx->pSps->uiChromaFormatIdc == 0)//no need parse chroma445 iPrevIntra4x4PredMode = uiCode;
446 return ERR_NONE;446 }
447 447 const int32_t kiPredMode = PredIntra4x4Mode (pIntraPredMode, i);
448 if (pCurDqLayer->sLayerInfo.pPps->bEntropyCodingModeFlag) {448
449 WELS_READ_VERIFY (ParseIntraPredModeChromaCabac (pCtx, uiNeighAvail, iCode));449 int8_t iBestMode;
450 if (iCode > MAX_PRED_MODE_ID_CHROMA) {450 if (pCurDqLayer->sLayerInfo.pPps->bEntropyCodingModeFlag) {
451 return GENERATE_ERROR_NO (ERR_LEVEL_MB_DATA, ERR_INFO_INVALID_I_CHROMA_PRED_MODE);451 if (iPrevIntra4x4PredMode == -1)
452 }452 iBestMode = kiPredMode;
453 pCurDqLayer->pChromaPredMode[iMbXy] = iCode;453 else
454 } else {454 iBestMode = iPrevIntra4x4PredMode + (iPrevIntra4x4PredMode >= kiPredMode);
455 WELS_READ_VERIFY (BsGetUe (pBs, &uiCode)); //intra_chroma_pred_mode455 } else {
456 if (uiCode > MAX_PRED_MODE_ID_CHROMA) {456 if (iPrevIntra4x4PredMode) {
457 return GENERATE_ERROR_NO (ERR_LEVEL_MB_DATA, ERR_INFO_INVALID_I_CHROMA_PRED_MODE);457 iBestMode = kiPredMode;
458 }458 } else {
459 pCurDqLayer->pChromaPredMode[iMbXy] = uiCode;459 WELS_READ_VERIFY (BsGetBits (pBs, 3, &uiCode));
460 }460 iBestMode = uiCode + ((int32_t) uiCode >= kiPredMode);
461 461 }
462 if (-1 == pCurDqLayer->pChromaPredMode[iMbXy]462 }
463 || CheckIntraChromaPredMode (uiNeighAvail, &pCurDqLayer->pChromaPredMode[iMbXy])) {463
464 return GENERATE_ERROR_NO (ERR_LEVEL_MB_DATA, ERR_INFO_INVALID_I_CHROMA_PRED_MODE);464 iFinalMode = CheckIntraNxNPredMode (&iSampleAvail[0], &iBestMode, i, false);
465 }465 if (iFinalMode == GENERATE_ERROR_NO (ERR_LEVEL_MB_DATA, ERR_INVALID_INTRA4X4_MODE)) {
466 return ERR_NONE;466 return GENERATE_ERROR_NO (ERR_LEVEL_MB_DATA, ERR_INFO_INVALID_I4x4_PRED_MODE);
467}467 }
468 468
469int32_t ParseIntra8x8Mode (PWelsDecoderContext pCtx, PWelsNeighAvail pNeighAvail, int8_t* pIntraPredMode,469 pCurDqLayer->pIntra4x4FinalMode[iMbXy][g_kuiScan4[i]] = iFinalMode;
470 PBitStringAux pBs,470
471 PDqLayer pCurDqLayer) {471 pIntraPredMode[g_kuiScan8[i]] = iBestMode;
472 // Similar with Intra_4x4, can put them together when needed472
473 int32_t iSampleAvail[5 * 6] = { 0 }; //initialize as 0473 iSampleAvail[g_kuiCache30ScanIdx[i]] = 1;
474 int32_t iMbXy = pCurDqLayer->iMbXyIndex;474 }
475 int32_t iFinalMode, i;475 ST32 (&pCurDqLayer->pIntraPredMode[iMbXy][0], LD32 (&pIntraPredMode[1 + 8 * 4]));
476 476 pCurDqLayer->pIntraPredMode[iMbXy][4] = pIntraPredMode[4 + 8 * 1];
477 uint8_t uiNeighAvail = 0;477 pCurDqLayer->pIntraPredMode[iMbXy][5] = pIntraPredMode[4 + 8 * 2];
478 uint32_t uiCode;478 pCurDqLayer->pIntraPredMode[iMbXy][6] = pIntraPredMode[4 + 8 * 3];
479 int32_t iCode;479
480 pCtx->pMapNxNNeighToSampleFunc (pNeighAvail, iSampleAvail);480 if (pCtx->pSps->uiChromaFormatIdc == 0)//no need parse chroma
481 // Top-Right : Left : Top-Left : Top481 return ERR_NONE;
482 uiNeighAvail = (iSampleAvail[5] << 3) | (iSampleAvail[6] << 2) | (iSampleAvail[0] << 1) | (iSampleAvail[1]);482
483 483 if (pCurDqLayer->sLayerInfo.pPps->bEntropyCodingModeFlag) {
484 pCurDqLayer->pIntraNxNAvailFlag[iMbXy] = uiNeighAvail;484 WELS_READ_VERIFY (ParseIntraPredModeChromaCabac (pCtx, uiNeighAvail, iCode));
485 485 if (iCode > MAX_PRED_MODE_ID_CHROMA) {
486 for (i = 0; i < 4; i++) {486 return GENERATE_ERROR_NO (ERR_LEVEL_MB_DATA, ERR_INFO_INVALID_I_CHROMA_PRED_MODE);
487 int32_t iPrevIntra4x4PredMode = 0;487 }
488 if (pCurDqLayer->sLayerInfo.pPps->bEntropyCodingModeFlag) {488 pCurDqLayer->pChromaPredMode[iMbXy] = iCode;
489 WELS_READ_VERIFY (ParseIntraPredModeLumaCabac (pCtx, iCode));489 } else {
490 iPrevIntra4x4PredMode = iCode;490 WELS_READ_VERIFY (BsGetUe (pBs, &uiCode)); //intra_chroma_pred_mode
491 } else {491 if (uiCode > MAX_PRED_MODE_ID_CHROMA) {
492 WELS_READ_VERIFY (BsGetOneBit (pBs, &uiCode));492 return GENERATE_ERROR_NO (ERR_LEVEL_MB_DATA, ERR_INFO_INVALID_I_CHROMA_PRED_MODE);
493 iPrevIntra4x4PredMode = uiCode;493 }
494 }494 pCurDqLayer->pChromaPredMode[iMbXy] = uiCode;
495 const int32_t kiPredMode = PredIntra4x4Mode (pIntraPredMode, i << 2);495 }
496 496
497 int8_t iBestMode;497 if (-1 == pCurDqLayer->pChromaPredMode[iMbXy]
498 if (pCurDqLayer->sLayerInfo.pPps->bEntropyCodingModeFlag) {498 || CheckIntraChromaPredMode (uiNeighAvail, &pCurDqLayer->pChromaPredMode[iMbXy])) {
499 if (iPrevIntra4x4PredMode == -1)499 return GENERATE_ERROR_NO (ERR_LEVEL_MB_DATA, ERR_INFO_INVALID_I_CHROMA_PRED_MODE);
500 iBestMode = kiPredMode;500 }
501 else501 return ERR_NONE;
502 iBestMode = iPrevIntra4x4PredMode + (iPrevIntra4x4PredMode >= kiPredMode);502}
503 } else {503
504 if (iPrevIntra4x4PredMode) {504int32_t ParseIntra8x8Mode (PWelsDecoderContext pCtx, PWelsNeighAvail pNeighAvail, int8_t* pIntraPredMode,
505 iBestMode = kiPredMode;505 PBitStringAux pBs,
506 } else {506 PDqLayer pCurDqLayer) {
507 WELS_READ_VERIFY (BsGetBits (pBs, 3, &uiCode));507 // Similar with Intra_4x4, can put them together when needed
508 iBestMode = uiCode + ((int32_t) uiCode >= kiPredMode);508 int32_t iSampleAvail[5 * 6] = { 0 }; //initialize as 0
509 }509 int32_t iMbXy = pCurDqLayer->iMbXyIndex;
510 }510 int32_t iFinalMode, i;
511 511
512 iFinalMode = CheckIntraNxNPredMode (&iSampleAvail[0], &iBestMode, i << 2, true);512 uint8_t uiNeighAvail = 0;
513 513 uint32_t uiCode;
514 if (iFinalMode == GENERATE_ERROR_NO (ERR_LEVEL_MB_DATA, ERR_INVALID_INTRA4X4_MODE)) {514 int32_t iCode;
515 return GENERATE_ERROR_NO (ERR_LEVEL_MB_DATA, ERR_INFO_INVALID_I4x4_PRED_MODE);515 pCtx->pMapNxNNeighToSampleFunc (pNeighAvail, iSampleAvail);
516 }516 // Top-Right : Left : Top-Left : Top
517 517 uiNeighAvail = (iSampleAvail[5] << 3) | (iSampleAvail[6] << 2) | (iSampleAvail[0] << 1) | (iSampleAvail[1]);
518 for (int j = 0; j < 4; j++) {518
519 pCurDqLayer->pIntra4x4FinalMode[iMbXy][g_kuiScan4[ (i << 2) + j]] = iFinalMode;519 pCurDqLayer->pIntraNxNAvailFlag[iMbXy] = uiNeighAvail;
520 pIntraPredMode[g_kuiScan8[ (i << 2) + j]] = iBestMode;520
521 iSampleAvail[g_kuiCache30ScanIdx[ (i << 2) + j]] = 1;521 for (i = 0; i < 4; i++) {
522 }522 int32_t iPrevIntra4x4PredMode = 0;
523 }523 if (pCurDqLayer->sLayerInfo.pPps->bEntropyCodingModeFlag) {
524 ST32 (&pCurDqLayer->pIntraPredMode[iMbXy][0], LD32 (&pIntraPredMode[1 + 8 * 4]));524 WELS_READ_VERIFY (ParseIntraPredModeLumaCabac (pCtx, iCode));
525 pCurDqLayer->pIntraPredMode[iMbXy][4] = pIntraPredMode[4 + 8 * 1];525 iPrevIntra4x4PredMode = iCode;
526 pCurDqLayer->pIntraPredMode[iMbXy][5] = pIntraPredMode[4 + 8 * 2];526 } else {
527 pCurDqLayer->pIntraPredMode[iMbXy][6] = pIntraPredMode[4 + 8 * 3];527 WELS_READ_VERIFY (BsGetOneBit (pBs, &uiCode));
528 528 iPrevIntra4x4PredMode = uiCode;
529 if (pCtx->pSps->uiChromaFormatIdc == 0)529 }
530 return ERR_NONE;530 const int32_t kiPredMode = PredIntra4x4Mode (pIntraPredMode, i << 2);
531 531
532 if (pCurDqLayer->sLayerInfo.pPps->bEntropyCodingModeFlag) {532 int8_t iBestMode;
533 WELS_READ_VERIFY (ParseIntraPredModeChromaCabac (pCtx, uiNeighAvail, iCode));533 if (pCurDqLayer->sLayerInfo.pPps->bEntropyCodingModeFlag) {
534 if (iCode > MAX_PRED_MODE_ID_CHROMA) {534 if (iPrevIntra4x4PredMode == -1)
535 return GENERATE_ERROR_NO (ERR_LEVEL_MB_DATA, ERR_INFO_INVALID_I_CHROMA_PRED_MODE);535 iBestMode = kiPredMode;
536 }536 else
537 pCurDqLayer->pChromaPredMode[iMbXy] = iCode;537 iBestMode = iPrevIntra4x4PredMode + (iPrevIntra4x4PredMode >= kiPredMode);
538 } else {538 } else {
539 WELS_READ_VERIFY (BsGetUe (pBs, &uiCode)); //intra_chroma_pred_mode539 if (iPrevIntra4x4PredMode) {
540 if (uiCode > MAX_PRED_MODE_ID_CHROMA) {540 iBestMode = kiPredMode;
541 return GENERATE_ERROR_NO (ERR_LEVEL_MB_DATA, ERR_INFO_INVALID_I_CHROMA_PRED_MODE);541 } else {
542 }542 WELS_READ_VERIFY (BsGetBits (pBs, 3, &uiCode));
543 pCurDqLayer->pChromaPredMode[iMbXy] = uiCode;543 iBestMode = uiCode + ((int32_t) uiCode >= kiPredMode);
544 }544 }
545 545 }
546 if (-1 == pCurDqLayer->pChromaPredMode[iMbXy]546
547 || CheckIntraChromaPredMode (uiNeighAvail, &pCurDqLayer->pChromaPredMode[iMbXy])) {547 iFinalMode = CheckIntraNxNPredMode (&iSampleAvail[0], &iBestMode, i << 2, true);
548 return GENERATE_ERROR_NO (ERR_LEVEL_MB_DATA, ERR_INFO_INVALID_I_CHROMA_PRED_MODE);548
549 }549 if (iFinalMode == GENERATE_ERROR_NO (ERR_LEVEL_MB_DATA, ERR_INVALID_INTRA4X4_MODE)) {
550 550 return GENERATE_ERROR_NO (ERR_LEVEL_MB_DATA, ERR_INFO_INVALID_I4x4_PRED_MODE);
551 return ERR_NONE;551 }
552}552
553 553 for (int j = 0; j < 4; j++) {
554int32_t ParseIntra16x16Mode (PWelsDecoderContext pCtx, PWelsNeighAvail pNeighAvail, PBitStringAux pBs,554 pCurDqLayer->pIntra4x4FinalMode[iMbXy][g_kuiScan4[ (i << 2) + j]] = iFinalMode;
555 PDqLayer pCurDqLayer) {555 pIntraPredMode[g_kuiScan8[ (i << 2) + j]] = iBestMode;
556 int32_t iMbXy = pCurDqLayer->iMbXyIndex;556 iSampleAvail[g_kuiCache30ScanIdx[ (i << 2) + j]] = 1;
557 uint8_t uiNeighAvail = 0; //0x07 = 0 1 1 1, means left, top-left, top avail or not. (1: avail, 0: unavail)557 }
558 uint32_t uiCode;558 }
559 int32_t iCode;559 ST32 (&pCurDqLayer->pIntraPredMode[iMbXy][0], LD32 (&pIntraPredMode[1 + 8 * 4]));
560 pCtx->pMap16x16NeighToSampleFunc (pNeighAvail, &uiNeighAvail);560 pCurDqLayer->pIntraPredMode[iMbXy][4] = pIntraPredMode[4 + 8 * 1];
561 561 pCurDqLayer->pIntraPredMode[iMbXy][5] = pIntraPredMode[4 + 8 * 2];
562 if (CheckIntra16x16PredMode (uiNeighAvail,562 pCurDqLayer->pIntraPredMode[iMbXy][6] = pIntraPredMode[4 + 8 * 3];
563 &pCurDqLayer->pIntraPredMode[iMbXy][7])) { //invalid iPredMode, must stop decoding563
564 return GENERATE_ERROR_NO (ERR_LEVEL_MB_DATA, ERR_INFO_INVALID_I16x16_PRED_MODE);564 if (pCtx->pSps->uiChromaFormatIdc == 0)
565 }565 return ERR_NONE;
566 if (pCtx->pSps->uiChromaFormatIdc == 0)566
567 return ERR_NONE;567 if (pCurDqLayer->sLayerInfo.pPps->bEntropyCodingModeFlag) {
568 568 WELS_READ_VERIFY (ParseIntraPredModeChromaCabac (pCtx, uiNeighAvail, iCode));
569 if (pCurDqLayer->sLayerInfo.pPps->bEntropyCodingModeFlag) {569 if (iCode > MAX_PRED_MODE_ID_CHROMA) {
570 WELS_READ_VERIFY (ParseIntraPredModeChromaCabac (pCtx, uiNeighAvail, iCode));570 return GENERATE_ERROR_NO (ERR_LEVEL_MB_DATA, ERR_INFO_INVALID_I_CHROMA_PRED_MODE);
571 if (iCode > MAX_PRED_MODE_ID_CHROMA) {571 }
572 return GENERATE_ERROR_NO (ERR_LEVEL_MB_DATA, ERR_INFO_INVALID_I_CHROMA_PRED_MODE);572 pCurDqLayer->pChromaPredMode[iMbXy] = iCode;
573 }573 } else {
574 pCurDqLayer->pChromaPredMode[iMbXy] = iCode;574 WELS_READ_VERIFY (BsGetUe (pBs, &uiCode)); //intra_chroma_pred_mode
575 } else {575 if (uiCode > MAX_PRED_MODE_ID_CHROMA) {
576 WELS_READ_VERIFY (BsGetUe (pBs, &uiCode)); //intra_chroma_pred_mode576 return GENERATE_ERROR_NO (ERR_LEVEL_MB_DATA, ERR_INFO_INVALID_I_CHROMA_PRED_MODE);
577 if (uiCode > MAX_PRED_MODE_ID_CHROMA) {577 }
578 return GENERATE_ERROR_NO (ERR_LEVEL_MB_DATA, ERR_INFO_INVALID_I_CHROMA_PRED_MODE);578 pCurDqLayer->pChromaPredMode[iMbXy] = uiCode;
579 }579 }
580 pCurDqLayer->pChromaPredMode[iMbXy] = uiCode;580
581 }581 if (-1 == pCurDqLayer->pChromaPredMode[iMbXy]
582 if (-1 == pCurDqLayer->pChromaPredMode[iMbXy]582 || CheckIntraChromaPredMode (uiNeighAvail, &pCurDqLayer->pChromaPredMode[iMbXy])) {
583 || CheckIntraChromaPredMode (uiNeighAvail, &pCurDqLayer->pChromaPredMode[iMbXy])) {583 return GENERATE_ERROR_NO (ERR_LEVEL_MB_DATA, ERR_INFO_INVALID_I_CHROMA_PRED_MODE);
584 return GENERATE_ERROR_NO (ERR_LEVEL_MB_DATA, ERR_INFO_INVALID_I_CHROMA_PRED_MODE);584 }
585 }585
586 586 return ERR_NONE;
587 return ERR_NONE;587}
588}588
589 589int32_t ParseIntra16x16Mode (PWelsDecoderContext pCtx, PWelsNeighAvail pNeighAvail, PBitStringAux pBs,
590int32_t WelsDecodeMbCabacISliceBaseMode0 (PWelsDecoderContext pCtx, uint32_t& uiEosFlag) {590 PDqLayer pCurDqLayer) {
591 PDqLayer pCurLayer = pCtx->pCurDqLayer;591 int32_t iMbXy = pCurDqLayer->iMbXyIndex;
592 PBitStringAux pBsAux = pCurLayer->pBitStringAux;592 uint8_t uiNeighAvail = 0; //0x07 = 0 1 1 1, means left, top-left, top avail or not. (1: avail, 0: unavail)
593 PSlice pSlice = &pCurLayer->sLayerInfo.sSliceInLayer;593 uint32_t uiCode;
594 PSliceHeader pSliceHeader = &pSlice->sSliceHeaderExt.sSliceHeader;594 int32_t iCode;
595 SWelsNeighAvail sNeighAvail;595 pCtx->pMap16x16NeighToSampleFunc (pNeighAvail, &uiNeighAvail);
596 int32_t iScanIdxStart = pSlice->sSliceHeaderExt.uiScanIdxStart;596
597 int32_t iScanIdxEnd = pSlice->sSliceHeaderExt.uiScanIdxEnd;597 if (CheckIntra16x16PredMode (uiNeighAvail,
598 int32_t iMbXy = pCurLayer->iMbXyIndex;598 &pCurDqLayer->pIntraPredMode[iMbXy][7])) { //invalid iPredMode, must stop decoding
599 int32_t i;599 return GENERATE_ERROR_NO (ERR_LEVEL_MB_DATA, ERR_INFO_INVALID_I16x16_PRED_MODE);
600 uint32_t uiMbType = 0, uiCbp = 0, uiCbpLuma = 0, uiCbpChroma = 0;600 }
601 601 if (pCtx->pSps->uiChromaFormatIdc == 0)
602 ENFORCE_STACK_ALIGN_1D (uint8_t, pNonZeroCount, 48, 16);602 return ERR_NONE;
603 603
604 pCurLayer->pNoSubMbPartSizeLessThan8x8Flag[iMbXy] = true;604 if (pCurDqLayer->sLayerInfo.pPps->bEntropyCodingModeFlag) {
605 pCurLayer->pTransformSize8x8Flag[iMbXy] = false;605 WELS_READ_VERIFY (ParseIntraPredModeChromaCabac (pCtx, uiNeighAvail, iCode));
606 606 if (iCode > MAX_PRED_MODE_ID_CHROMA) {
607 pCurLayer->pInterPredictionDoneFlag[iMbXy] = 0;607 return GENERATE_ERROR_NO (ERR_LEVEL_MB_DATA, ERR_INFO_INVALID_I_CHROMA_PRED_MODE);
608 pCurLayer->pResidualPredFlag[iMbXy] = pSlice->sSliceHeaderExt.bDefaultResidualPredFlag;608 }
609 GetNeighborAvailMbType (&sNeighAvail, pCurLayer);609 pCurDqLayer->pChromaPredMode[iMbXy] = iCode;
610 WELS_READ_VERIFY (ParseMBTypeISliceCabac (pCtx, &sNeighAvail, uiMbType));610 } else {
611 if (uiMbType > 25) {611 WELS_READ_VERIFY (BsGetUe (pBs, &uiCode)); //intra_chroma_pred_mode
612 return GENERATE_ERROR_NO (ERR_LEVEL_MB_DATA, ERR_INFO_INVALID_MB_TYPE);612 if (uiCode > MAX_PRED_MODE_ID_CHROMA) {
613 } else if (!pCtx->pSps->uiChromaFormatIdc && ((uiMbType >= 5 && uiMbType <= 12) || (uiMbType >= 17613 return GENERATE_ERROR_NO (ERR_LEVEL_MB_DATA, ERR_INFO_INVALID_I_CHROMA_PRED_MODE);
614 && uiMbType <= 24))) {614 }
615 return GENERATE_ERROR_NO (ERR_LEVEL_MB_DATA, ERR_INFO_INVALID_MB_TYPE);615 pCurDqLayer->pChromaPredMode[iMbXy] = uiCode;
616 } else if (25 == uiMbType) { //I_PCM616 }
617 WelsLog (& (pCtx->sLogCtx), WELS_LOG_DEBUG, "I_PCM mode exists in I slice!");617 if (-1 == pCurDqLayer->pChromaPredMode[iMbXy]
618 WELS_READ_VERIFY (ParseIPCMInfoCabac (pCtx));618 || CheckIntraChromaPredMode (uiNeighAvail, &pCurDqLayer->pChromaPredMode[iMbXy])) {
619 pSlice->iLastDeltaQp = 0;619 return GENERATE_ERROR_NO (ERR_LEVEL_MB_DATA, ERR_INFO_INVALID_I_CHROMA_PRED_MODE);
620 WELS_READ_VERIFY (ParseEndOfSliceCabac (pCtx, uiEosFlag));620 }
621 if (uiEosFlag) {621
622 RestoreCabacDecEngineToBS (pCtx->pCabacDecEngine, pCtx->pCurDqLayer->pBitStringAux);622 return ERR_NONE;
623 }623}
624 return ERR_NONE;624
625 } else if (0 == uiMbType) { //I4x4625int32_t WelsDecodeMbCabacISliceBaseMode0 (PWelsDecoderContext pCtx, uint32_t& uiEosFlag) {
626 ENFORCE_STACK_ALIGN_1D (int8_t, pIntraPredMode, 48, 16);626 PDqLayer pCurLayer = pCtx->pCurDqLayer;
627 pCurLayer->pMbType[iMbXy] = MB_TYPE_INTRA4x4;627 PBitStringAux pBsAux = pCurLayer->pBitStringAux;
628 if (pCtx->pPps->bTransform8x8ModeFlag) {628 PSlice pSlice = &pCurLayer->sLayerInfo.sSliceInLayer;
629 // Transform 8x8 cabac will be added soon629 PSliceHeader pSliceHeader = &pSlice->sSliceHeaderExt.sSliceHeader;
630 WELS_READ_VERIFY (ParseTransformSize8x8FlagCabac (pCtx, &sNeighAvail, pCtx->pCurDqLayer->pTransformSize8x8Flag[iMbXy]));630 SWelsNeighAvail sNeighAvail;
631 }631 int32_t iScanIdxStart = pSlice->sSliceHeaderExt.uiScanIdxStart;
632 if (pCtx->pCurDqLayer->pTransformSize8x8Flag[iMbXy]) {632 int32_t iScanIdxEnd = pSlice->sSliceHeaderExt.uiScanIdxEnd;
633 uiMbType = pCurLayer->pMbType[iMbXy] = MB_TYPE_INTRA8x8;633 int32_t iMbXy = pCurLayer->iMbXyIndex;
634 pCtx->pFillInfoCacheIntraNxNFunc (&sNeighAvail, pNonZeroCount, pIntraPredMode, pCurLayer);634 int32_t i;
635 WELS_READ_VERIFY (ParseIntra8x8Mode (pCtx, &sNeighAvail, pIntraPredMode, pBsAux, pCurLayer));635 uint32_t uiMbType = 0, uiCbp = 0, uiCbpLuma = 0, uiCbpChroma = 0;
636 } else {636
637 pCtx->pFillInfoCacheIntraNxNFunc (&sNeighAvail, pNonZeroCount, pIntraPredMode, pCurLayer);637 ENFORCE_STACK_ALIGN_1D (uint8_t, pNonZeroCount, 48, 16);
638 WELS_READ_VERIFY (ParseIntra4x4Mode (pCtx, &sNeighAvail, pIntraPredMode, pBsAux, pCurLayer));638
639 }639 pCurLayer->pNoSubMbPartSizeLessThan8x8Flag[iMbXy] = true;
640 //get uiCbp for I4x4640 pCurLayer->pTransformSize8x8Flag[iMbXy] = false;
641 WELS_READ_VERIFY (ParseCbpInfoCabac (pCtx, &sNeighAvail, uiCbp));641
642 pCurLayer->pCbp[iMbXy] = uiCbp;642 pCurLayer->pInterPredictionDoneFlag[iMbXy] = 0;
643 pSlice->iLastDeltaQp = uiCbp == 0 ? 0 : pSlice->iLastDeltaQp;643 pCurLayer->pResidualPredFlag[iMbXy] = pSlice->sSliceHeaderExt.bDefaultResidualPredFlag;
644 uiCbpChroma = pCtx->pSps->uiChromaFormatIdc ? uiCbp >> 4 : 0;644 GetNeighborAvailMbType (&sNeighAvail, pCurLayer);
645 uiCbpLuma = uiCbp & 15;645 WELS_READ_VERIFY (ParseMBTypeISliceCabac (pCtx, &sNeighAvail, uiMbType));
646 } else { //I16x16;646 if (uiMbType > 25) {
647 pCurLayer->pMbType[iMbXy] = MB_TYPE_INTRA16x16;647 return GENERATE_ERROR_NO (ERR_LEVEL_MB_DATA, ERR_INFO_INVALID_MB_TYPE);
648 pCurLayer->pTransformSize8x8Flag[iMbXy] = false;648 } else if (!pCtx->pSps->uiChromaFormatIdc && ((uiMbType >= 5 && uiMbType <= 12) || (uiMbType >= 17
649 pCurLayer->pNoSubMbPartSizeLessThan8x8Flag[iMbXy] = true;649 && uiMbType <= 24))) {
650 pCurLayer->pIntraPredMode[iMbXy][7] = (uiMbType - 1) & 3;650 return GENERATE_ERROR_NO (ERR_LEVEL_MB_DATA, ERR_INFO_INVALID_MB_TYPE);
651 pCurLayer->pCbp[iMbXy] = g_kuiI16CbpTable[ (uiMbType - 1) >> 2];651 } else if (25 == uiMbType) { //I_PCM
652 uiCbpChroma = pCtx->pSps->uiChromaFormatIdc ? pCurLayer->pCbp[iMbXy] >> 4 : 0 ;652 WelsLog (& (pCtx->sLogCtx), WELS_LOG_DEBUG, "I_PCM mode exists in I slice!");
653 uiCbpLuma = pCurLayer->pCbp[iMbXy] & 15;653 WELS_READ_VERIFY (ParseIPCMInfoCabac (pCtx));
654 WelsFillCacheNonZeroCount (&sNeighAvail, pNonZeroCount, pCurLayer);654 pSlice->iLastDeltaQp = 0;
655 WELS_READ_VERIFY (ParseIntra16x16Mode (pCtx, &sNeighAvail, pBsAux, pCurLayer));655 WELS_READ_VERIFY (ParseEndOfSliceCabac (pCtx, uiEosFlag));
656 }656 if (uiEosFlag) {
657 657 RestoreCabacDecEngineToBS (pCtx->pCabacDecEngine, pCtx->pCurDqLayer->pBitStringAux);
658 ST32 (&pCurLayer->pNzc[iMbXy][0], 0);658 }
659 ST32 (&pCurLayer->pNzc[iMbXy][4], 0);659 return ERR_NONE;
660 ST32 (&pCurLayer->pNzc[iMbXy][8], 0);660 } else if (0 == uiMbType) { //I4x4
661 ST32 (&pCurLayer->pNzc[iMbXy][12], 0);661 ENFORCE_STACK_ALIGN_1D (int8_t, pIntraPredMode, 48, 16);
662 ST32 (&pCurLayer->pNzc[iMbXy][16], 0);662 pCurLayer->pMbType[iMbXy] = MB_TYPE_INTRA4x4;
663 ST32 (&pCurLayer->pNzc[iMbXy][20], 0);663 if (pCtx->pPps->bTransform8x8ModeFlag) {
664 pCurLayer->pCbfDc[iMbXy] = 0;664 // Transform 8x8 cabac will be added soon
665 665 WELS_READ_VERIFY (ParseTransformSize8x8FlagCabac (pCtx, &sNeighAvail, pCtx->pCurDqLayer->pTransformSize8x8Flag[iMbXy]));
666 if (pCurLayer->pCbp[iMbXy] == 0 && IS_INTRANxN (pCurLayer->pMbType[iMbXy])) {666 }
667 pCurLayer->pLumaQp[iMbXy] = pSlice->iLastMbQp;667 if (pCtx->pCurDqLayer->pTransformSize8x8Flag[iMbXy]) {
668 for (i = 0; i < 2; i++) {668 uiMbType = pCurLayer->pMbType[iMbXy] = MB_TYPE_INTRA8x8;
669 pCurLayer->pChromaQp[iMbXy][i] = g_kuiChromaQpTable[WELS_CLIP3 ((pCurLayer->pLumaQp[iMbXy] +669 pCtx->pFillInfoCacheIntraNxNFunc (&sNeighAvail, pNonZeroCount, pIntraPredMode, pCurLayer);
670 pSliceHeader->pPps->iChromaQpIndexOffset[i]), 0, 51)];670 WELS_READ_VERIFY (ParseIntra8x8Mode (pCtx, &sNeighAvail, pIntraPredMode, pBsAux, pCurLayer));
671 }671 } else {
672 }672 pCtx->pFillInfoCacheIntraNxNFunc (&sNeighAvail, pNonZeroCount, pIntraPredMode, pCurLayer);
673 673 WELS_READ_VERIFY (ParseIntra4x4Mode (pCtx, &sNeighAvail, pIntraPredMode, pBsAux, pCurLayer));
674 if (pCurLayer->pCbp[iMbXy] || MB_TYPE_INTRA16x16 == pCurLayer->pMbType[iMbXy]) {674 }
675 memset (pCurLayer->pScaledTCoeff[iMbXy], 0, 384 * sizeof (pCurLayer->pScaledTCoeff[iMbXy][0]));675 //get uiCbp for I4x4
676 int32_t iQpDelta, iId8x8, iId4x4;676 WELS_READ_VERIFY (ParseCbpInfoCabac (pCtx, &sNeighAvail, uiCbp));
677 WELS_READ_VERIFY (ParseDeltaQpCabac (pCtx, iQpDelta));677 pCurLayer->pCbp[iMbXy] = uiCbp;
678 if (iQpDelta > 25 || iQpDelta < -26) {//out of iQpDelta range678 pSlice->iLastDeltaQp = uiCbp == 0 ? 0 : pSlice->iLastDeltaQp;
679 return GENERATE_ERROR_NO (ERR_LEVEL_MB_DATA, ERR_INFO_INVALID_QP);679 uiCbpChroma = pCtx->pSps->uiChromaFormatIdc ? uiCbp >> 4 : 0;
680 }680 uiCbpLuma = uiCbp & 15;
681 pCurLayer->pLumaQp[iMbXy] = (pSlice->iLastMbQp + iQpDelta + 52) % 52; //update last_mb_qp681 } else { //I16x16;
682 pSlice->iLastMbQp = pCurLayer->pLumaQp[iMbXy];682 pCurLayer->pMbType[iMbXy] = MB_TYPE_INTRA16x16;
683 for (i = 0; i < 2; i++) {683 pCurLayer->pTransformSize8x8Flag[iMbXy] = false;
684 pCurLayer->pChromaQp[iMbXy][i] = g_kuiChromaQpTable[WELS_CLIP3 ((pSlice->iLastMbQp +684 pCurLayer->pNoSubMbPartSizeLessThan8x8Flag[iMbXy] = true;
685 pSliceHeader->pPps->iChromaQpIndexOffset[i]), 0, 51)];685 pCurLayer->pIntraPredMode[iMbXy][7] = (uiMbType - 1) & 3;
686 }686 pCurLayer->pCbp[iMbXy] = g_kuiI16CbpTable[ (uiMbType - 1) >> 2];
687 if (MB_TYPE_INTRA16x16 == pCurLayer->pMbType[iMbXy]) {687 uiCbpChroma = pCtx->pSps->uiChromaFormatIdc ? pCurLayer->pCbp[iMbXy] >> 4 : 0 ;
688 //step1: Luma DC688 uiCbpLuma = pCurLayer->pCbp[iMbXy] & 15;
689 WELS_READ_VERIFY (ParseResidualBlockCabac (&sNeighAvail, pNonZeroCount, pBsAux, 0, 16, g_kuiLumaDcZigzagScan,689 WelsFillCacheNonZeroCount (&sNeighAvail, pNonZeroCount, pCurLayer);
690 I16_LUMA_DC, pCurLayer->pScaledTCoeff[iMbXy], pCurLayer->pLumaQp[iMbXy], pCtx));690 WELS_READ_VERIFY (ParseIntra16x16Mode (pCtx, &sNeighAvail, pBsAux, pCurLayer));
691 //step2: Luma AC691 }
692 if (uiCbpLuma) {692
693 for (i = 0; i < 16; i++) {693 ST32 (&pCurLayer->pNzc[iMbXy][0], 0);
694 WELS_READ_VERIFY (ParseResidualBlockCabac (&sNeighAvail, pNonZeroCount, pBsAux, i,694 ST32 (&pCurLayer->pNzc[iMbXy][4], 0);
695 iScanIdxEnd - WELS_MAX (iScanIdxStart, 1) + 1, g_kuiZigzagScan + WELS_MAX (iScanIdxStart, 1), I16_LUMA_AC,695 ST32 (&pCurLayer->pNzc[iMbXy][8], 0);
696 pCurLayer->pScaledTCoeff[iMbXy] + (i << 4), pCurLayer->pLumaQp[iMbXy], pCtx));696 ST32 (&pCurLayer->pNzc[iMbXy][12], 0);
697 }697 ST32 (&pCurLayer->pNzc[iMbXy][16], 0);
698 ST32 (&pCurLayer->pNzc[iMbXy][0], LD32 (&pNonZeroCount[1 + 8 * 1]));698 ST32 (&pCurLayer->pNzc[iMbXy][20], 0);
699 ST32 (&pCurLayer->pNzc[iMbXy][4], LD32 (&pNonZeroCount[1 + 8 * 2]));699 pCurLayer->pCbfDc[iMbXy] = 0;
700 ST32 (&pCurLayer->pNzc[iMbXy][8], LD32 (&pNonZeroCount[1 + 8 * 3]));700
701 ST32 (&pCurLayer->pNzc[iMbXy][12], LD32 (&pNonZeroCount[1 + 8 * 4]));701 if (pCurLayer->pCbp[iMbXy] == 0 && IS_INTRANxN (pCurLayer->pMbType[iMbXy])) {
702 } else { //pNonZeroCount = 0702 pCurLayer->pLumaQp[iMbXy] = pSlice->iLastMbQp;
703 ST32 (&pCurLayer->pNzc[iMbXy][0], 0);703 for (i = 0; i < 2; i++) {
704 ST32 (&pCurLayer->pNzc[iMbXy][4], 0);704 pCurLayer->pChromaQp[iMbXy][i] = g_kuiChromaQpTable[WELS_CLIP3 ((pCurLayer->pLumaQp[iMbXy] +
705 ST32 (&pCurLayer->pNzc[iMbXy][8], 0);705 pSliceHeader->pPps->iChromaQpIndexOffset[i]), 0, 51)];
706 ST32 (&pCurLayer->pNzc[iMbXy][12], 0);706 }
707 }707 }
708 } else { //non-MB_TYPE_INTRA16x16708
709 if (pCurLayer->pTransformSize8x8Flag[iMbXy]) {709 if (pCurLayer->pCbp[iMbXy] || MB_TYPE_INTRA16x16 == pCurLayer->pMbType[iMbXy]) {
710 // Transform 8x8 support for CABAC710 memset (pCurLayer->pScaledTCoeff[iMbXy], 0, 384 * sizeof (pCurLayer->pScaledTCoeff[iMbXy][0]));
711 for (iId8x8 = 0; iId8x8 < 4; iId8x8++) {711 int32_t iQpDelta, iId8x8, iId4x4;
712 if (uiCbpLuma & (1 << iId8x8)) {712 WELS_READ_VERIFY (ParseDeltaQpCabac (pCtx, iQpDelta));
713 WELS_READ_VERIFY (ParseResidualBlockCabac8x8 (&sNeighAvail, pNonZeroCount, pBsAux, (iId8x8 << 2),713 if (iQpDelta > 25 || iQpDelta < -26) {//out of iQpDelta range
714 iScanIdxEnd - iScanIdxStart + 1, g_kuiZigzagScan8x8 + iScanIdxStart, LUMA_DC_AC_INTRA_8,714 return GENERATE_ERROR_NO (ERR_LEVEL_MB_DATA, ERR_INFO_INVALID_QP);
715 pCurLayer->pScaledTCoeff[iMbXy] + (iId8x8 << 6), pCurLayer->pLumaQp[iMbXy], pCtx));715 }
716 } else {716 pCurLayer->pLumaQp[iMbXy] = (pSlice->iLastMbQp + iQpDelta + 52) % 52; //update last_mb_qp
717 ST16 (&pNonZeroCount[g_kCacheNzcScanIdx[ (iId8x8 << 2)]], 0);717 pSlice->iLastMbQp = pCurLayer->pLumaQp[iMbXy];
718 ST16 (&pNonZeroCount[g_kCacheNzcScanIdx[ (iId8x8 << 2) + 2]], 0);718 for (i = 0; i < 2; i++) {
719 }719 pCurLayer->pChromaQp[iMbXy][i] = g_kuiChromaQpTable[WELS_CLIP3 ((pSlice->iLastMbQp +
720 }720 pSliceHeader->pPps->iChromaQpIndexOffset[i]), 0, 51)];
721 ST32 (&pCurLayer->pNzc[iMbXy][0], LD32 (&pNonZeroCount[1 + 8 * 1]));721 }
722 ST32 (&pCurLayer->pNzc[iMbXy][4], LD32 (&pNonZeroCount[1 + 8 * 2]));722 if (MB_TYPE_INTRA16x16 == pCurLayer->pMbType[iMbXy]) {
723 ST32 (&pCurLayer->pNzc[iMbXy][8], LD32 (&pNonZeroCount[1 + 8 * 3]));723 //step1: Luma DC
724 ST32 (&pCurLayer->pNzc[iMbXy][12], LD32 (&pNonZeroCount[1 + 8 * 4]));724 WELS_READ_VERIFY (ParseResidualBlockCabac (&sNeighAvail, pNonZeroCount, pBsAux, 0, 16, g_kuiLumaDcZigzagScan,
725 } else {725 I16_LUMA_DC, pCurLayer->pScaledTCoeff[iMbXy], pCurLayer->pLumaQp[iMbXy], pCtx));
726 for (iId8x8 = 0; iId8x8 < 4; iId8x8++) {726 //step2: Luma AC
727 if (uiCbpLuma & (1 << iId8x8)) {727 if (uiCbpLuma) {
728 int32_t iIdx = (iId8x8 << 2);728 for (i = 0; i < 16; i++) {
729 for (iId4x4 = 0; iId4x4 < 4; iId4x4++) {729 WELS_READ_VERIFY (ParseResidualBlockCabac (&sNeighAvail, pNonZeroCount, pBsAux, i,
730 //Luma (DC and AC decoding together)730 iScanIdxEnd - WELS_MAX (iScanIdxStart, 1) + 1, g_kuiZigzagScan + WELS_MAX (iScanIdxStart, 1), I16_LUMA_AC,
731 WELS_READ_VERIFY (ParseResidualBlockCabac (&sNeighAvail, pNonZeroCount, pBsAux, iIdx, iScanIdxEnd - iScanIdxStart + 1,731 pCurLayer->pScaledTCoeff[iMbXy] + (i << 4), pCurLayer->pLumaQp[iMbXy], pCtx));
732 g_kuiZigzagScan + iScanIdxStart, LUMA_DC_AC_INTRA, pCurLayer->pScaledTCoeff[iMbXy] + (iIdx << 4),732 }
733 pCurLayer->pLumaQp[iMbXy], pCtx));733 ST32 (&pCurLayer->pNzc[iMbXy][0], LD32 (&pNonZeroCount[1 + 8 * 1]));
734 iIdx++;734 ST32 (&pCurLayer->pNzc[iMbXy][4], LD32 (&pNonZeroCount[1 + 8 * 2]));
735 }735 ST32 (&pCurLayer->pNzc[iMbXy][8], LD32 (&pNonZeroCount[1 + 8 * 3]));
736 } else {736 ST32 (&pCurLayer->pNzc[iMbXy][12], LD32 (&pNonZeroCount[1 + 8 * 4]));
737 ST16 (&pNonZeroCount[g_kCacheNzcScanIdx[ (iId8x8 << 2)]], 0);737 } else { //pNonZeroCount = 0
738 ST16 (&pNonZeroCount[g_kCacheNzcScanIdx[ (iId8x8 << 2) + 2]], 0);738 ST32 (&pCurLayer->pNzc[iMbXy][0], 0);
739 }739 ST32 (&pCurLayer->pNzc[iMbXy][4], 0);
740 }740 ST32 (&pCurLayer->pNzc[iMbXy][8], 0);
741 ST32 (&pCurLayer->pNzc[iMbXy][0], LD32 (&pNonZeroCount[1 + 8 * 1]));741 ST32 (&pCurLayer->pNzc[iMbXy][12], 0);
742 ST32 (&pCurLayer->pNzc[iMbXy][4], LD32 (&pNonZeroCount[1 + 8 * 2]));742 }
743 ST32 (&pCurLayer->pNzc[iMbXy][8], LD32 (&pNonZeroCount[1 + 8 * 3]));743 } else { //non-MB_TYPE_INTRA16x16
744 ST32 (&pCurLayer->pNzc[iMbXy][12], LD32 (&pNonZeroCount[1 + 8 * 4]));744 if (pCurLayer->pTransformSize8x8Flag[iMbXy]) {
745 }745 // Transform 8x8 support for CABAC
746 }746 for (iId8x8 = 0; iId8x8 < 4; iId8x8++) {
747 int32_t iMbResProperty;747 if (uiCbpLuma & (1 << iId8x8)) {
748 //chroma748 WELS_READ_VERIFY (ParseResidualBlockCabac8x8 (&sNeighAvail, pNonZeroCount, pBsAux, (iId8x8 << 2),
749 //step1: DC749 iScanIdxEnd - iScanIdxStart + 1, g_kuiZigzagScan8x8 + iScanIdxStart, LUMA_DC_AC_INTRA_8,
750 if (1 == uiCbpChroma || 2 == uiCbpChroma) {750 pCurLayer->pScaledTCoeff[iMbXy] + (iId8x8 << 6), pCurLayer->pLumaQp[iMbXy], pCtx));
751 //Cb Cr751 } else {
752 for (i = 0; i < 2; i++) {752 ST16 (&pNonZeroCount[g_kCacheNzcScanIdx[ (iId8x8 << 2)]], 0);
753 iMbResProperty = i ? CHROMA_DC_V : CHROMA_DC_U;753 ST16 (&pNonZeroCount[g_kCacheNzcScanIdx[ (iId8x8 << 2) + 2]], 0);
754 WELS_READ_VERIFY (ParseResidualBlockCabac (&sNeighAvail, pNonZeroCount, pBsAux, 16 + (i << 2), 4, g_kuiChromaDcScan,754 }
755 iMbResProperty, pCurLayer->pScaledTCoeff[iMbXy] + 256 + (i << 6), pCurLayer->pChromaQp[iMbXy][i], pCtx));755 }
756 }756 ST32 (&pCurLayer->pNzc[iMbXy][0], LD32 (&pNonZeroCount[1 + 8 * 1]));
757 }757 ST32 (&pCurLayer->pNzc[iMbXy][4], LD32 (&pNonZeroCount[1 + 8 * 2]));
758 758 ST32 (&pCurLayer->pNzc[iMbXy][8], LD32 (&pNonZeroCount[1 + 8 * 3]));
759 //step2: AC759 ST32 (&pCurLayer->pNzc[iMbXy][12], LD32 (&pNonZeroCount[1 + 8 * 4]));
760 if (2 == uiCbpChroma) {760 } else {
761 for (i = 0; i < 2; i++) { //Cb Cr761 for (iId8x8 = 0; iId8x8 < 4; iId8x8++) {
762 iMbResProperty = i ? CHROMA_AC_V : CHROMA_AC_U;762 if (uiCbpLuma & (1 << iId8x8)) {
763 int32_t iIdx = 16 + (i << 2);763 int32_t iIdx = (iId8x8 << 2);
764 for (iId4x4 = 0; iId4x4 < 4; iId4x4++) {764 for (iId4x4 = 0; iId4x4 < 4; iId4x4++) {
765 WELS_READ_VERIFY (ParseResidualBlockCabac (&sNeighAvail, pNonZeroCount, pBsAux, iIdx,765 //Luma (DC and AC decoding together)
766 iScanIdxEnd - WELS_MAX (iScanIdxStart, 1) + 1, g_kuiZigzagScan + WELS_MAX (iScanIdxStart, 1), iMbResProperty,766 WELS_READ_VERIFY (ParseResidualBlockCabac (&sNeighAvail, pNonZeroCount, pBsAux, iIdx, iScanIdxEnd - iScanIdxStart + 1,
767 pCurLayer->pScaledTCoeff[iMbXy] + (iIdx << 4), pCurLayer->pChromaQp[iMbXy][i], pCtx));767 g_kuiZigzagScan + iScanIdxStart, LUMA_DC_AC_INTRA, pCurLayer->pScaledTCoeff[iMbXy] + (iIdx << 4),
768 iIdx++;768 pCurLayer->pLumaQp[iMbXy], pCtx));
769 }769 iIdx++;
770 }770 }
771 ST16 (&pCurLayer->pNzc[iMbXy][16], LD16 (&pNonZeroCount[6 + 8 * 1]));771 } else {
772 ST16 (&pCurLayer->pNzc[iMbXy][20], LD16 (&pNonZeroCount[6 + 8 * 2]));772 ST16 (&pNonZeroCount[g_kCacheNzcScanIdx[ (iId8x8 << 2)]], 0);
773 ST16 (&pCurLayer->pNzc[iMbXy][18], LD16 (&pNonZeroCount[6 + 8 * 4]));773 ST16 (&pNonZeroCount[g_kCacheNzcScanIdx[ (iId8x8 << 2) + 2]], 0);
774 ST16 (&pCurLayer->pNzc[iMbXy][22], LD16 (&pNonZeroCount[6 + 8 * 5]));774 }
775 } else {775 }
776 ST16 (&pCurLayer->pNzc[iMbXy][16], 0);776 ST32 (&pCurLayer->pNzc[iMbXy][0], LD32 (&pNonZeroCount[1 + 8 * 1]));
777 ST16 (&pCurLayer->pNzc[iMbXy][20], 0);777 ST32 (&pCurLayer->pNzc[iMbXy][4], LD32 (&pNonZeroCount[1 + 8 * 2]));
778 ST16 (&pCurLayer->pNzc[iMbXy][18], 0);778 ST32 (&pCurLayer->pNzc[iMbXy][8], LD32 (&pNonZeroCount[1 + 8 * 3]));
779 ST16 (&pCurLayer->pNzc[iMbXy][22], 0);779 ST32 (&pCurLayer->pNzc[iMbXy][12], LD32 (&pNonZeroCount[1 + 8 * 4]));
780 }780 }
781 } else {781 }
782 ST32 (&pCurLayer->pNzc[iMbXy][0], 0);782 int32_t iMbResProperty;
783 ST32 (&pCurLayer->pNzc[iMbXy][4], 0);783 //chroma
784 ST32 (&pCurLayer->pNzc[iMbXy][8], 0);784 //step1: DC
785 ST32 (&pCurLayer->pNzc[iMbXy][12], 0);785 if (1 == uiCbpChroma || 2 == uiCbpChroma) {
786 ST32 (&pCurLayer->pNzc[iMbXy][16], 0);786 //Cb Cr
787 ST32 (&pCurLayer->pNzc[iMbXy][20], 0);787 for (i = 0; i < 2; i++) {
788 }788 iMbResProperty = i ? CHROMA_DC_V : CHROMA_DC_U;
789 789 WELS_READ_VERIFY (ParseResidualBlockCabac (&sNeighAvail, pNonZeroCount, pBsAux, 16 + (i << 2), 4, g_kuiChromaDcScan,
790 WELS_READ_VERIFY (ParseEndOfSliceCabac (pCtx, uiEosFlag));790 iMbResProperty, pCurLayer->pScaledTCoeff[iMbXy] + 256 + (i << 6), pCurLayer->pChromaQp[iMbXy][i], pCtx));
791 if (uiEosFlag) {791 }
792 RestoreCabacDecEngineToBS (pCtx->pCabacDecEngine, pCtx->pCurDqLayer->pBitStringAux);792 }
793 }793
794 return ERR_NONE;794 //step2: AC
795}795 if (2 == uiCbpChroma) {
796 796 for (i = 0; i < 2; i++) { //Cb Cr
797int32_t WelsDecodeMbCabacISlice (PWelsDecoderContext pCtx, PNalUnit pNalCur, uint32_t& uiEosFlag) {797 iMbResProperty = i ? CHROMA_AC_V : CHROMA_AC_U;
798 WELS_READ_VERIFY (WelsDecodeMbCabacISliceBaseMode0 (pCtx, uiEosFlag));798 int32_t iIdx = 16 + (i << 2);
799 return ERR_NONE;799 for (iId4x4 = 0; iId4x4 < 4; iId4x4++) {
800}800 WELS_READ_VERIFY (ParseResidualBlockCabac (&sNeighAvail, pNonZeroCount, pBsAux, iIdx,
801 801 iScanIdxEnd - WELS_MAX (iScanIdxStart, 1) + 1, g_kuiZigzagScan + WELS_MAX (iScanIdxStart, 1), iMbResProperty,
802int32_t WelsDecodeMbCabacPSliceBaseMode0 (PWelsDecoderContext pCtx, PWelsNeighAvail pNeighAvail, uint32_t& uiEosFlag) {802 pCurLayer->pScaledTCoeff[iMbXy] + (iIdx << 4), pCurLayer->pChromaQp[iMbXy][i], pCtx));
803 PDqLayer pCurLayer = pCtx->pCurDqLayer;803 iIdx++;
804 PBitStringAux pBsAux = pCurLayer->pBitStringAux;804 }
805 PSlice pSlice = &pCurLayer->sLayerInfo.sSliceInLayer;805 }
806 PSliceHeader pSliceHeader = &pSlice->sSliceHeaderExt.sSliceHeader;806 ST16 (&pCurLayer->pNzc[iMbXy][16], LD16 (&pNonZeroCount[6 + 8 * 1]));
807 807 ST16 (&pCurLayer->pNzc[iMbXy][20], LD16 (&pNonZeroCount[6 + 8 * 2]));
808 int32_t iScanIdxStart = pSlice->sSliceHeaderExt.uiScanIdxStart;808 ST16 (&pCurLayer->pNzc[iMbXy][18], LD16 (&pNonZeroCount[6 + 8 * 4]));
809 int32_t iScanIdxEnd = pSlice->sSliceHeaderExt.uiScanIdxEnd;809 ST16 (&pCurLayer->pNzc[iMbXy][22], LD16 (&pNonZeroCount[6 + 8 * 5]));
810 int32_t iMbXy = pCurLayer->iMbXyIndex;810 } else {
811 int32_t iMbResProperty;811 ST16 (&pCurLayer->pNzc[iMbXy][16], 0);
812 int32_t i;812 ST16 (&pCurLayer->pNzc[iMbXy][20], 0);
813 uint32_t uiMbType = 0, uiCbp = 0, uiCbpLuma = 0, uiCbpChroma = 0;813 ST16 (&pCurLayer->pNzc[iMbXy][18], 0);
814 814 ST16 (&pCurLayer->pNzc[iMbXy][22], 0);
815 ENFORCE_STACK_ALIGN_1D (uint8_t, pNonZeroCount, 48, 16);815 }
816 816 } else {
817 pCurLayer->pInterPredictionDoneFlag[iMbXy] = 0;817 ST32 (&pCurLayer->pNzc[iMbXy][0], 0);
818 818 ST32 (&pCurLayer->pNzc[iMbXy][4], 0);
819 WELS_READ_VERIFY (ParseMBTypePSliceCabac (pCtx, pNeighAvail, uiMbType));819 ST32 (&pCurLayer->pNzc[iMbXy][8], 0);
820 // uiMbType = 4 is not allowded.820 ST32 (&pCurLayer->pNzc[iMbXy][12], 0);
821 if (uiMbType < 4) { //Inter mode821 ST32 (&pCurLayer->pNzc[iMbXy][16], 0);
822 int16_t pMotionVector[LIST_A][30][MV_A];822 ST32 (&pCurLayer->pNzc[iMbXy][20], 0);
823 int16_t pMvdCache[LIST_A][30][MV_A];823 }
824 int8_t pRefIndex[LIST_A][30];824
825 pCurLayer->pMbType[iMbXy] = g_ksInterMbTypeInfo[uiMbType].iType;825 WELS_READ_VERIFY (ParseEndOfSliceCabac (pCtx, uiEosFlag));
826 WelsFillCacheInterCabac (pNeighAvail, pNonZeroCount, pMotionVector, pMvdCache, pRefIndex, pCurLayer);826 if (uiEosFlag) {
827 WELS_READ_VERIFY (ParseInterMotionInfoCabac (pCtx, pNeighAvail, pNonZeroCount, pMotionVector, pMvdCache, pRefIndex));827 RestoreCabacDecEngineToBS (pCtx->pCabacDecEngine, pCtx->pCurDqLayer->pBitStringAux);
828 pCurLayer->pInterPredictionDoneFlag[iMbXy] = 0;828 }
829 } else { //Intra mode829 return ERR_NONE;
830 uiMbType -= 5;830}
831 if (uiMbType > 25)831
832 return GENERATE_ERROR_NO (ERR_LEVEL_MB_DATA, ERR_INFO_INVALID_MB_TYPE);832int32_t WelsDecodeMbCabacISlice (PWelsDecoderContext pCtx, PNalUnit pNalCur, uint32_t& uiEosFlag) {
833 if (!pCtx->pSps->uiChromaFormatIdc && ((uiMbType >= 5 && uiMbType <= 12) || (uiMbType >= 17 && uiMbType <= 24)))833 WELS_READ_VERIFY (WelsDecodeMbCabacISliceBaseMode0 (pCtx, uiEosFlag));
834 return GENERATE_ERROR_NO (ERR_LEVEL_MB_DATA, ERR_INFO_INVALID_MB_TYPE);834 return ERR_NONE;
835 835}
836 if (25 == uiMbType) { //I_PCM836
837 WelsLog (& (pCtx->sLogCtx), WELS_LOG_DEBUG, "I_PCM mode exists in P slice!");837int32_t WelsDecodeMbCabacPSliceBaseMode0 (PWelsDecoderContext pCtx, PWelsNeighAvail pNeighAvail, uint32_t& uiEosFlag) {
838 WELS_READ_VERIFY (ParseIPCMInfoCabac (pCtx));838 PDqLayer pCurLayer = pCtx->pCurDqLayer;
839 pSlice->iLastDeltaQp = 0;839 PBitStringAux pBsAux = pCurLayer->pBitStringAux;
840 WELS_READ_VERIFY (ParseEndOfSliceCabac (pCtx, uiEosFlag));840 PSlice pSlice = &pCurLayer->sLayerInfo.sSliceInLayer;
841 if (uiEosFlag) {841 PSliceHeader pSliceHeader = &pSlice->sSliceHeaderExt.sSliceHeader;
842 RestoreCabacDecEngineToBS (pCtx->pCabacDecEngine, pCtx->pCurDqLayer->pBitStringAux);842
843 }843 int32_t iScanIdxStart = pSlice->sSliceHeaderExt.uiScanIdxStart;
844 return ERR_NONE;844 int32_t iScanIdxEnd = pSlice->sSliceHeaderExt.uiScanIdxEnd;
845 } else { //normal Intra mode845 int32_t iMbXy = pCurLayer->iMbXyIndex;
846 if (0 == uiMbType) { //Intra4x4846 int32_t iMbResProperty;
847 ENFORCE_STACK_ALIGN_1D (int8_t, pIntraPredMode, 48, 16);847 int32_t i;
848 pCurLayer->pMbType[iMbXy] = MB_TYPE_INTRA4x4;848 uint32_t uiMbType = 0, uiCbp = 0, uiCbpLuma = 0, uiCbpChroma = 0;
849 if (pCtx->pPps->bTransform8x8ModeFlag) {849
850 WELS_READ_VERIFY (ParseTransformSize8x8FlagCabac (pCtx, pNeighAvail, pCtx->pCurDqLayer->pTransformSize8x8Flag[iMbXy]));850 ENFORCE_STACK_ALIGN_1D (uint8_t, pNonZeroCount, 48, 16);
851 }851
852 if (pCtx->pCurDqLayer->pTransformSize8x8Flag[iMbXy]) {852 pCurLayer->pInterPredictionDoneFlag[iMbXy] = 0;
853 uiMbType = pCurLayer->pMbType[iMbXy] = MB_TYPE_INTRA8x8;853
854 pCtx->pFillInfoCacheIntraNxNFunc (pNeighAvail, pNonZeroCount, pIntraPredMode, pCurLayer);854 WELS_READ_VERIFY (ParseMBTypePSliceCabac (pCtx, pNeighAvail, uiMbType));
855 WELS_READ_VERIFY (ParseIntra8x8Mode (pCtx, pNeighAvail, pIntraPredMode, pBsAux, pCurLayer));855 // uiMbType = 4 is not allowded.
856 } else {856 if (uiMbType < 4) { //Inter mode
857 pCtx->pFillInfoCacheIntraNxNFunc (pNeighAvail, pNonZeroCount, pIntraPredMode, pCurLayer);857 int16_t pMotionVector[LIST_A][30][MV_A];
858 WELS_READ_VERIFY (ParseIntra4x4Mode (pCtx, pNeighAvail, pIntraPredMode, pBsAux, pCurLayer));858 int16_t pMvdCache[LIST_A][30][MV_A];
859 }859 int8_t pRefIndex[LIST_A][30];
860 } else { //Intra16x16860 pCurLayer->pMbType[iMbXy] = g_ksInterPMbTypeInfo[uiMbType].iType;
861 pCurLayer->pMbType[iMbXy] = MB_TYPE_INTRA16x16;861 WelsFillCacheInterCabac (pNeighAvail, pNonZeroCount, pMotionVector, pMvdCache, pRefIndex, pCurLayer);
862 pCurLayer->pTransformSize8x8Flag[iMbXy] = false;862 WELS_READ_VERIFY (ParseInterPMotionInfoCabac (pCtx, pNeighAvail, pNonZeroCount, pMotionVector, pMvdCache, pRefIndex));
863 pCurLayer->pNoSubMbPartSizeLessThan8x8Flag[iMbXy] = true;863 pCurLayer->pInterPredictionDoneFlag[iMbXy] = 0;
864 pCurLayer->pIntraPredMode[iMbXy][7] = (uiMbType - 1) & 3;864 } else { //Intra mode
865 pCurLayer->pCbp[iMbXy] = g_kuiI16CbpTable[ (uiMbType - 1) >> 2];865 uiMbType -= 5;
866 uiCbpChroma = pCtx->pSps->uiChromaFormatIdc ? pCurLayer->pCbp[iMbXy] >> 4 : 0;866 if (uiMbType > 25)
867 uiCbpLuma = pCurLayer->pCbp[iMbXy] & 15;867 return GENERATE_ERROR_NO (ERR_LEVEL_MB_DATA, ERR_INFO_INVALID_MB_TYPE);
868 WelsFillCacheNonZeroCount (pNeighAvail, pNonZeroCount, pCurLayer);868 if (!pCtx->pSps->uiChromaFormatIdc && ((uiMbType >= 5 && uiMbType <= 12) || (uiMbType >= 17 && uiMbType <= 24)))
869 WELS_READ_VERIFY (ParseIntra16x16Mode (pCtx, pNeighAvail, pBsAux, pCurLayer));869 return GENERATE_ERROR_NO (ERR_LEVEL_MB_DATA, ERR_INFO_INVALID_MB_TYPE);
870 }870
871 }871 if (25 == uiMbType) { //I_PCM
872 }872 WelsLog (& (pCtx->sLogCtx), WELS_LOG_DEBUG, "I_PCM mode exists in P slice!");
873 873 WELS_READ_VERIFY (ParseIPCMInfoCabac (pCtx));
874 ST32 (&pCurLayer->pNzc[iMbXy][0], 0);874 pSlice->iLastDeltaQp = 0;
875 ST32 (&pCurLayer->pNzc[iMbXy][4], 0);875 WELS_READ_VERIFY (ParseEndOfSliceCabac (pCtx, uiEosFlag));
876 ST32 (&pCurLayer->pNzc[iMbXy][8], 0);876 if (uiEosFlag) {
877 ST32 (&pCurLayer->pNzc[iMbXy][12], 0);877 RestoreCabacDecEngineToBS (pCtx->pCabacDecEngine, pCtx->pCurDqLayer->pBitStringAux);
878 ST32 (&pCurLayer->pNzc[iMbXy][16], 0);878 }
879 ST32 (&pCurLayer->pNzc[iMbXy][20], 0);879 return ERR_NONE;
880 880 } else { //normal Intra mode
881 if (MB_TYPE_INTRA16x16 != pCurLayer->pMbType[iMbXy]) {881 if (0 == uiMbType) { //Intra4x4
882 WELS_READ_VERIFY (ParseCbpInfoCabac (pCtx, pNeighAvail, uiCbp));882 ENFORCE_STACK_ALIGN_1D (int8_t, pIntraPredMode, 48, 16);
883 883 pCurLayer->pMbType[iMbXy] = MB_TYPE_INTRA4x4;
884 pCurLayer->pCbp[iMbXy] = uiCbp;884 if (pCtx->pPps->bTransform8x8ModeFlag) {
885 pSlice->iLastDeltaQp = uiCbp == 0 ? 0 : pSlice->iLastDeltaQp;885 WELS_READ_VERIFY (ParseTransformSize8x8FlagCabac (pCtx, pNeighAvail, pCtx->pCurDqLayer->pTransformSize8x8Flag[iMbXy]));
886 uiCbpChroma = pCtx->pSps->uiChromaFormatIdc ? pCurLayer->pCbp[iMbXy] >> 4 : 0 ;886 }
887 uiCbpLuma = pCurLayer->pCbp[iMbXy] & 15;887 if (pCtx->pCurDqLayer->pTransformSize8x8Flag[iMbXy]) {
888 }888 uiMbType = pCurLayer->pMbType[iMbXy] = MB_TYPE_INTRA8x8;
889 889 pCtx->pFillInfoCacheIntraNxNFunc (pNeighAvail, pNonZeroCount, pIntraPredMode, pCurLayer);
890 if (pCurLayer->pCbp[iMbXy] || MB_TYPE_INTRA16x16 == pCurLayer->pMbType[iMbXy]) {890 WELS_READ_VERIFY (ParseIntra8x8Mode (pCtx, pNeighAvail, pIntraPredMode, pBsAux, pCurLayer));
891 891 } else {
892 if (MB_TYPE_INTRA16x16 != pCurLayer->pMbType[iMbXy]) {892 pCtx->pFillInfoCacheIntraNxNFunc (pNeighAvail, pNonZeroCount, pIntraPredMode, pCurLayer);
893 // Need modification when B picutre add in893 WELS_READ_VERIFY (ParseIntra4x4Mode (pCtx, pNeighAvail, pIntraPredMode, pBsAux, pCurLayer));
894 bool bNeedParseTransformSize8x8Flag =894 }
895 (((pCurLayer->pMbType[iMbXy] >= MB_TYPE_16x16 && pCurLayer->pMbType[iMbXy] <= MB_TYPE_8x16)895 } else { //Intra16x16
896 || pCurLayer->pNoSubMbPartSizeLessThan8x8Flag[iMbXy])896 pCurLayer->pMbType[iMbXy] = MB_TYPE_INTRA16x16;
897 && (pCurLayer->pMbType[iMbXy] != MB_TYPE_INTRA8x8)897 pCurLayer->pTransformSize8x8Flag[iMbXy] = false;
898 && (pCurLayer->pMbType[iMbXy] != MB_TYPE_INTRA4x4)898 pCurLayer->pNoSubMbPartSizeLessThan8x8Flag[iMbXy] = true;
899 && ((pCurLayer->pCbp[iMbXy] & 0x0F) > 0)899 pCurLayer->pIntraPredMode[iMbXy][7] = (uiMbType - 1) & 3;
900 && (pCtx->pPps->bTransform8x8ModeFlag));900 pCurLayer->pCbp[iMbXy] = g_kuiI16CbpTable[ (uiMbType - 1) >> 2];
901 901 uiCbpChroma = pCtx->pSps->uiChromaFormatIdc ? pCurLayer->pCbp[iMbXy] >> 4 : 0;
902 if (bNeedParseTransformSize8x8Flag) {902 uiCbpLuma = pCurLayer->pCbp[iMbXy] & 15;
903 WELS_READ_VERIFY (ParseTransformSize8x8FlagCabac (pCtx, pNeighAvail,903 WelsFillCacheNonZeroCount (pNeighAvail, pNonZeroCount, pCurLayer);
904 pCtx->pCurDqLayer->pTransformSize8x8Flag[iMbXy])); //transform_size_8x8_flag904 WELS_READ_VERIFY (ParseIntra16x16Mode (pCtx, pNeighAvail, pBsAux, pCurLayer));
905 }905 }
906 }906 }
907 907 }
908 memset (pCurLayer->pScaledTCoeff[iMbXy], 0, 384 * sizeof (pCurLayer->pScaledTCoeff[iMbXy][0]));908
909 909 ST32 (&pCurLayer->pNzc[iMbXy][0], 0);
910 int32_t iQpDelta, iId8x8, iId4x4;910 ST32 (&pCurLayer->pNzc[iMbXy][4], 0);
911 911 ST32 (&pCurLayer->pNzc[iMbXy][8], 0);
912 WELS_READ_VERIFY (ParseDeltaQpCabac (pCtx, iQpDelta));912 ST32 (&pCurLayer->pNzc[iMbXy][12], 0);
913 if (iQpDelta > 25 || iQpDelta < -26) { //out of iQpDelta range913 ST32 (&pCurLayer->pNzc[iMbXy][16], 0);
914 return GENERATE_ERROR_NO (ERR_LEVEL_MB_DATA, ERR_INFO_INVALID_QP);914 ST32 (&pCurLayer->pNzc[iMbXy][20], 0);
915 }915
916 pCurLayer->pLumaQp[iMbXy] = (pSlice->iLastMbQp + iQpDelta + 52) % 52; //update last_mb_qp916 if (MB_TYPE_INTRA16x16 != pCurLayer->pMbType[iMbXy]) {
917 pSlice->iLastMbQp = pCurLayer->pLumaQp[iMbXy];917 WELS_READ_VERIFY (ParseCbpInfoCabac (pCtx, pNeighAvail, uiCbp));
918 for (i = 0; i < 2; i++) {918
919 pCurLayer->pChromaQp[iMbXy][i] = g_kuiChromaQpTable[WELS_CLIP3 (pSlice->iLastMbQp +919 pCurLayer->pCbp[iMbXy] = uiCbp;
920 pSliceHeader->pPps->iChromaQpIndexOffset[i], 0, 51)];920 pSlice->iLastDeltaQp = uiCbp == 0 ? 0 : pSlice->iLastDeltaQp;
921 }921 uiCbpChroma = pCtx->pSps->uiChromaFormatIdc ? pCurLayer->pCbp[iMbXy] >> 4 : 0 ;
922 922 uiCbpLuma = pCurLayer->pCbp[iMbXy] & 15;
923 if (MB_TYPE_INTRA16x16 == pCurLayer->pMbType[iMbXy]) {923 }
924 //step1: Luma DC924
925 WELS_READ_VERIFY (ParseResidualBlockCabac (pNeighAvail, pNonZeroCount, pBsAux, 0, 16, g_kuiLumaDcZigzagScan,925 if (pCurLayer->pCbp[iMbXy] || MB_TYPE_INTRA16x16 == pCurLayer->pMbType[iMbXy]) {
926 I16_LUMA_DC, pCurLayer->pScaledTCoeff[iMbXy], pCurLayer->pLumaQp[iMbXy], pCtx));926
927 //step2: Luma AC927 if (MB_TYPE_INTRA16x16 != pCurLayer->pMbType[iMbXy]) {
928 if (uiCbpLuma) {928 // Need modification when B picutre add in
929 for (i = 0; i < 16; i++) {929 bool bNeedParseTransformSize8x8Flag =
930 WELS_READ_VERIFY (ParseResidualBlockCabac (pNeighAvail, pNonZeroCount, pBsAux, i, iScanIdxEnd - WELS_MAX (iScanIdxStart,930 (((pCurLayer->pMbType[iMbXy] >= MB_TYPE_16x16 && pCurLayer->pMbType[iMbXy] <= MB_TYPE_8x16)
931 1) + 1, g_kuiZigzagScan + WELS_MAX (iScanIdxStart, 1), I16_LUMA_AC, pCurLayer->pScaledTCoeff[iMbXy] + (i << 4),931 || pCurLayer->pNoSubMbPartSizeLessThan8x8Flag[iMbXy])
932 pCurLayer->pLumaQp[iMbXy], pCtx));932 && (pCurLayer->pMbType[iMbXy] != MB_TYPE_INTRA8x8)
933 }933 && (pCurLayer->pMbType[iMbXy] != MB_TYPE_INTRA4x4)
934 ST32 (&pCurLayer->pNzc[iMbXy][0], LD32 (&pNonZeroCount[1 + 8 * 1]));934 && ((pCurLayer->pCbp[iMbXy] & 0x0F) > 0)
935 ST32 (&pCurLayer->pNzc[iMbXy][4], LD32 (&pNonZeroCount[1 + 8 * 2]));935 && (pCtx->pPps->bTransform8x8ModeFlag));
936 ST32 (&pCurLayer->pNzc[iMbXy][8], LD32 (&pNonZeroCount[1 + 8 * 3]));936
937 ST32 (&pCurLayer->pNzc[iMbXy][12], LD32 (&pNonZeroCount[1 + 8 * 4]));937 if (bNeedParseTransformSize8x8Flag) {
938 } else {938 WELS_READ_VERIFY (ParseTransformSize8x8FlagCabac (pCtx, pNeighAvail,
939 ST32 (&pCurLayer->pNzc[iMbXy][0], 0);939 pCtx->pCurDqLayer->pTransformSize8x8Flag[iMbXy])); //transform_size_8x8_flag
940 ST32 (&pCurLayer->pNzc[iMbXy][4], 0);940 }
941 ST32 (&pCurLayer->pNzc[iMbXy][8], 0);941 }
942 ST32 (&pCurLayer->pNzc[iMbXy][12], 0);942
943 }943 memset (pCurLayer->pScaledTCoeff[iMbXy], 0, 384 * sizeof (pCurLayer->pScaledTCoeff[iMbXy][0]));
944 } else { //non-MB_TYPE_INTRA16x16944
945 if (pCtx->pCurDqLayer->pTransformSize8x8Flag[iMbXy]) {945 int32_t iQpDelta, iId8x8, iId4x4;
946 // Transform 8x8 support for CABAC946
947 for (iId8x8 = 0; iId8x8 < 4; iId8x8++) {947 WELS_READ_VERIFY (ParseDeltaQpCabac (pCtx, iQpDelta));
948 if (uiCbpLuma & (1 << iId8x8)) {948 if (iQpDelta > 25 || iQpDelta < -26) { //out of iQpDelta range
949 WELS_READ_VERIFY (ParseResidualBlockCabac8x8 (pNeighAvail, pNonZeroCount, pBsAux, (iId8x8 << 2),949 return GENERATE_ERROR_NO (ERR_LEVEL_MB_DATA, ERR_INFO_INVALID_QP);
950 iScanIdxEnd - iScanIdxStart + 1, g_kuiZigzagScan8x8 + iScanIdxStart,950 }
951 IS_INTRA (pCurLayer->pMbType[iMbXy]) ? LUMA_DC_AC_INTRA_8 : LUMA_DC_AC_INTER_8,951 pCurLayer->pLumaQp[iMbXy] = (pSlice->iLastMbQp + iQpDelta + 52) % 52; //update last_mb_qp
952 pCurLayer->pScaledTCoeff[iMbXy] + (iId8x8 << 6), pCurLayer->pLumaQp[iMbXy], pCtx));952 pSlice->iLastMbQp = pCurLayer->pLumaQp[iMbXy];
953 } else {953 for (i = 0; i < 2; i++) {
954 ST16 (&pNonZeroCount[g_kCacheNzcScanIdx[ (iId8x8 << 2)]], 0);954 pCurLayer->pChromaQp[iMbXy][i] = g_kuiChromaQpTable[WELS_CLIP3 (pSlice->iLastMbQp +
955 ST16 (&pNonZeroCount[g_kCacheNzcScanIdx[ (iId8x8 << 2) + 2]], 0);955 pSliceHeader->pPps->iChromaQpIndexOffset[i], 0, 51)];
956 }956 }
957 }957
958 ST32 (&pCurLayer->pNzc[iMbXy][0], LD32 (&pNonZeroCount[1 + 8 * 1]));958 if (MB_TYPE_INTRA16x16 == pCurLayer->pMbType[iMbXy]) {
959 ST32 (&pCurLayer->pNzc[iMbXy][4], LD32 (&pNonZeroCount[1 + 8 * 2]));959 //step1: Luma DC
960 ST32 (&pCurLayer->pNzc[iMbXy][8], LD32 (&pNonZeroCount[1 + 8 * 3]));960 WELS_READ_VERIFY (ParseResidualBlockCabac (pNeighAvail, pNonZeroCount, pBsAux, 0, 16, g_kuiLumaDcZigzagScan,
961 ST32 (&pCurLayer->pNzc[iMbXy][12], LD32 (&pNonZeroCount[1 + 8 * 4]));961 I16_LUMA_DC, pCurLayer->pScaledTCoeff[iMbXy], pCurLayer->pLumaQp[iMbXy], pCtx));
962 } else {962 //step2: Luma AC
963 iMbResProperty = (IS_INTRA (pCurLayer->pMbType[iMbXy])) ? LUMA_DC_AC_INTRA : LUMA_DC_AC_INTER;963 if (uiCbpLuma) {
964 for (iId8x8 = 0; iId8x8 < 4; iId8x8++) {964 for (i = 0; i < 16; i++) {
965 if (uiCbpLuma & (1 << iId8x8)) {965 WELS_READ_VERIFY (ParseResidualBlockCabac (pNeighAvail, pNonZeroCount, pBsAux, i, iScanIdxEnd - WELS_MAX (iScanIdxStart,
966 int32_t iIdx = (iId8x8 << 2);966 1) + 1, g_kuiZigzagScan + WELS_MAX (iScanIdxStart, 1), I16_LUMA_AC, pCurLayer->pScaledTCoeff[iMbXy] + (i << 4),
967 for (iId4x4 = 0; iId4x4 < 4; iId4x4++) {967 pCurLayer->pLumaQp[iMbXy], pCtx));
968 //Luma (DC and AC decoding together)968 }
969 WELS_READ_VERIFY (ParseResidualBlockCabac (pNeighAvail, pNonZeroCount, pBsAux, iIdx, iScanIdxEnd - iScanIdxStart + 1,969 ST32 (&pCurLayer->pNzc[iMbXy][0], LD32 (&pNonZeroCount[1 + 8 * 1]));
970 g_kuiZigzagScan + iScanIdxStart, iMbResProperty, pCurLayer->pScaledTCoeff[iMbXy] + (iIdx << 4),970 ST32 (&pCurLayer->pNzc[iMbXy][4], LD32 (&pNonZeroCount[1 + 8 * 2]));
971 pCurLayer->pLumaQp[iMbXy],971 ST32 (&pCurLayer->pNzc[iMbXy][8], LD32 (&pNonZeroCount[1 + 8 * 3]));
972 pCtx));972 ST32 (&pCurLayer->pNzc[iMbXy][12], LD32 (&pNonZeroCount[1 + 8 * 4]));
973 iIdx++;973 } else {
974 }974 ST32 (&pCurLayer->pNzc[iMbXy][0], 0);
975 } else {975 ST32 (&pCurLayer->pNzc[iMbXy][4], 0);
976 ST16 (&pNonZeroCount[g_kCacheNzcScanIdx[iId8x8 << 2]], 0);976 ST32 (&pCurLayer->pNzc[iMbXy][8], 0);
977 ST16 (&pNonZeroCount[g_kCacheNzcScanIdx[ (iId8x8 << 2) + 2]], 0);977 ST32 (&pCurLayer->pNzc[iMbXy][12], 0);
978 }978 }
979 }979 } else { //non-MB_TYPE_INTRA16x16
980 ST32 (&pCurLayer->pNzc[iMbXy][0], LD32 (&pNonZeroCount[1 + 8 * 1]));980 if (pCtx->pCurDqLayer->pTransformSize8x8Flag[iMbXy]) {
981 ST32 (&pCurLayer->pNzc[iMbXy][4], LD32 (&pNonZeroCount[1 + 8 * 2]));981 // Transform 8x8 support for CABAC
982 ST32 (&pCurLayer->pNzc[iMbXy][8], LD32 (&pNonZeroCount[1 + 8 * 3]));982 for (iId8x8 = 0; iId8x8 < 4; iId8x8++) {
983 ST32 (&pCurLayer->pNzc[iMbXy][12], LD32 (&pNonZeroCount[1 + 8 * 4]));983 if (uiCbpLuma & (1 << iId8x8)) {
984 }984 WELS_READ_VERIFY (ParseResidualBlockCabac8x8 (pNeighAvail, pNonZeroCount, pBsAux, (iId8x8 << 2),
985 }985 iScanIdxEnd - iScanIdxStart + 1, g_kuiZigzagScan8x8 + iScanIdxStart,
986 986 IS_INTRA (pCurLayer->pMbType[iMbXy]) ? LUMA_DC_AC_INTRA_8 : LUMA_DC_AC_INTER_8,
987 //chroma987 pCurLayer->pScaledTCoeff[iMbXy] + (iId8x8 << 6), pCurLayer->pLumaQp[iMbXy], pCtx));
988 //step1: DC988 } else {
989 if (1 == uiCbpChroma || 2 == uiCbpChroma) {989 ST16 (&pNonZeroCount[g_kCacheNzcScanIdx[ (iId8x8 << 2)]], 0);
990 for (i = 0; i < 2; i++) {990 ST16 (&pNonZeroCount[g_kCacheNzcScanIdx[ (iId8x8 << 2) + 2]], 0);
991 if (IS_INTRA (pCurLayer->pMbType[iMbXy]))991 }
992 iMbResProperty = i ? CHROMA_DC_V : CHROMA_DC_U;992 }
993 else993 ST32 (&pCurLayer->pNzc[iMbXy][0], LD32 (&pNonZeroCount[1 + 8 * 1]));
994 iMbResProperty = i ? CHROMA_DC_V_INTER : CHROMA_DC_U_INTER;994 ST32 (&pCurLayer->pNzc[iMbXy][4], LD32 (&pNonZeroCount[1 + 8 * 2]));
995 995 ST32 (&pCurLayer->pNzc[iMbXy][8], LD32 (&pNonZeroCount[1 + 8 * 3]));
996 WELS_READ_VERIFY (ParseResidualBlockCabac (pNeighAvail, pNonZeroCount, pBsAux, 16 + (i << 2), 4, g_kuiChromaDcScan,996 ST32 (&pCurLayer->pNzc[iMbXy][12], LD32 (&pNonZeroCount[1 + 8 * 4]));
997 iMbResProperty, pCurLayer->pScaledTCoeff[iMbXy] + 256 + (i << 6), pCurLayer->pChromaQp[iMbXy][i], pCtx));997 } else {
998 }998 iMbResProperty = (IS_INTRA (pCurLayer->pMbType[iMbXy])) ? LUMA_DC_AC_INTRA : LUMA_DC_AC_INTER;
999 }999 for (iId8x8 = 0; iId8x8 < 4; iId8x8++) {
1000 //step2: AC1000 if (uiCbpLuma & (1 << iId8x8)) {
1001 if (2 == uiCbpChroma) {1001 int32_t iIdx = (iId8x8 << 2);
1002 for (i = 0; i < 2; i++) {1002 for (iId4x4 = 0; iId4x4 < 4; iId4x4++) {
1003 if (IS_INTRA (pCurLayer->pMbType[iMbXy]))1003 //Luma (DC and AC decoding together)
1004 iMbResProperty = i ? CHROMA_AC_V : CHROMA_AC_U;1004 WELS_READ_VERIFY (ParseResidualBlockCabac (pNeighAvail, pNonZeroCount, pBsAux, iIdx, iScanIdxEnd - iScanIdxStart + 1,
1005 else1005 g_kuiZigzagScan + iScanIdxStart, iMbResProperty, pCurLayer->pScaledTCoeff[iMbXy] + (iIdx << 4),
1006 iMbResProperty = i ? CHROMA_AC_V_INTER : CHROMA_AC_U_INTER;1006 pCurLayer->pLumaQp[iMbXy],
1007 int32_t index = 16 + (i << 2);1007 pCtx));
1008 for (iId4x4 = 0; iId4x4 < 4; iId4x4++) {1008 iIdx++;
1009 WELS_READ_VERIFY (ParseResidualBlockCabac (pNeighAvail, pNonZeroCount, pBsAux, index,1009 }
1010 iScanIdxEnd - WELS_MAX (iScanIdxStart, 1) + 1, g_kuiZigzagScan + WELS_MAX (iScanIdxStart, 1),1010 } else {
1011 iMbResProperty, pCurLayer->pScaledTCoeff[iMbXy] + (index << 4), pCurLayer->pChromaQp[iMbXy][i], pCtx));1011 ST16 (&pNonZeroCount[g_kCacheNzcScanIdx[iId8x8 << 2]], 0);
1012 index++;1012 ST16 (&pNonZeroCount[g_kCacheNzcScanIdx[ (iId8x8 << 2) + 2]], 0);
1013 }1013 }
1014 }1014 }
1015 ST16 (&pCurLayer->pNzc[iMbXy][16], LD16 (&pNonZeroCount[6 + 8 * 1]));1015 ST32 (&pCurLayer->pNzc[iMbXy][0], LD32 (&pNonZeroCount[1 + 8 * 1]));
1016 ST16 (&pCurLayer->pNzc[iMbXy][20], LD16 (&pNonZeroCount[6 + 8 * 2]));1016 ST32 (&pCurLayer->pNzc[iMbXy][4], LD32 (&pNonZeroCount[1 + 8 * 2]));
1017 ST16 (&pCurLayer->pNzc[iMbXy][18], LD16 (&pNonZeroCount[6 + 8 * 4]));1017 ST32 (&pCurLayer->pNzc[iMbXy][8], LD32 (&pNonZeroCount[1 + 8 * 3]));
1018 ST16 (&pCurLayer->pNzc[iMbXy][22], LD16 (&pNonZeroCount[6 + 8 * 5]));1018 ST32 (&pCurLayer->pNzc[iMbXy][12], LD32 (&pNonZeroCount[1 + 8 * 4]));
1019 } else {1019 }
1020 ST32 (&pCurLayer->pNzc[iMbXy][16], 0);1020 }
1021 ST32 (&pCurLayer->pNzc[iMbXy][20], 0);1021
1022 }1022 //chroma
1023 } else {1023 //step1: DC
1024 pCurLayer->pLumaQp[iMbXy] = pSlice->iLastMbQp;1024 if (1 == uiCbpChroma || 2 == uiCbpChroma) {
1025 for (i = 0; i < 2; i++) {1025 for (i = 0; i < 2; i++) {
1026 pCurLayer->pChromaQp[iMbXy][i] = g_kuiChromaQpTable[WELS_CLIP3 (pCurLayer->pLumaQp[iMbXy] +1026 if (IS_INTRA (pCurLayer->pMbType[iMbXy]))
1027 pSliceHeader->pPps->iChromaQpIndexOffset[i], 0, 51)];1027 iMbResProperty = i ? CHROMA_DC_V : CHROMA_DC_U;
1028 }1028 else
1029 }1029 iMbResProperty = i ? CHROMA_DC_V_INTER : CHROMA_DC_U_INTER;
1030 1030
1031 WELS_READ_VERIFY (ParseEndOfSliceCabac (pCtx, uiEosFlag));1031 WELS_READ_VERIFY (ParseResidualBlockCabac (pNeighAvail, pNonZeroCount, pBsAux, 16 + (i << 2), 4, g_kuiChromaDcScan,
1032 if (uiEosFlag) {1032 iMbResProperty, pCurLayer->pScaledTCoeff[iMbXy] + 256 + (i << 6), pCurLayer->pChromaQp[iMbXy][i], pCtx));
1033 RestoreCabacDecEngineToBS (pCtx->pCabacDecEngine, pCtx->pCurDqLayer->pBitStringAux);1033 }
1034 }1034 }
1035 1035 //step2: AC
1036 return ERR_NONE;1036 if (2 == uiCbpChroma) {
1037}1037 for (i = 0; i < 2; i++) {
1038 1038 if (IS_INTRA (pCurLayer->pMbType[iMbXy]))
1039int32_t WelsDecodeMbCabacPSlice (PWelsDecoderContext pCtx, PNalUnit pNalCur, uint32_t& uiEosFlag) {1039 iMbResProperty = i ? CHROMA_AC_V : CHROMA_AC_U;
1040 PDqLayer pCurLayer = pCtx->pCurDqLayer;1040 else
1041 PSlice pSlice = &pCurLayer->sLayerInfo.sSliceInLayer;1041 iMbResProperty = i ? CHROMA_AC_V_INTER : CHROMA_AC_U_INTER;
1042 PSliceHeader pSliceHeader = &pSlice->sSliceHeaderExt.sSliceHeader;1042 int32_t index = 16 + (i << 2);
1043 PPicture* ppRefPic = pCtx->sRefPic.pRefList[LIST_0];1043 for (iId4x4 = 0; iId4x4 < 4; iId4x4++) {
1044 uint32_t uiCode;1044 WELS_READ_VERIFY (ParseResidualBlockCabac (pNeighAvail, pNonZeroCount, pBsAux, index,
1045 int32_t iMbXy = pCurLayer->iMbXyIndex;1045 iScanIdxEnd - WELS_MAX (iScanIdxStart, 1) + 1, g_kuiZigzagScan + WELS_MAX (iScanIdxStart, 1),
1046 int32_t i;1046 iMbResProperty, pCurLayer->pScaledTCoeff[iMbXy] + (index << 4), pCurLayer->pChromaQp[iMbXy][i], pCtx));
1047 SWelsNeighAvail uiNeighAvail;1047 index++;
1048 pCurLayer->pCbp[iMbXy] = 0;1048 }
1049 pCurLayer->pCbfDc[iMbXy] = 0;1049 }
1050 pCurLayer->pChromaPredMode[iMbXy] = C_PRED_DC;1050 ST16 (&pCurLayer->pNzc[iMbXy][16], LD16 (&pNonZeroCount[6 + 8 * 1]));
1051 1051 ST16 (&pCurLayer->pNzc[iMbXy][20], LD16 (&pNonZeroCount[6 + 8 * 2]));
1052 pCurLayer->pNoSubMbPartSizeLessThan8x8Flag[iMbXy] = true;1052 ST16 (&pCurLayer->pNzc[iMbXy][18], LD16 (&pNonZeroCount[6 + 8 * 4]));
1053 pCurLayer->pTransformSize8x8Flag[iMbXy] = false;1053 ST16 (&pCurLayer->pNzc[iMbXy][22], LD16 (&pNonZeroCount[6 + 8 * 5]));
1054 1054 } else {
1055 GetNeighborAvailMbType (&uiNeighAvail, pCurLayer);1055 ST32 (&pCurLayer->pNzc[iMbXy][16], 0);
1056 WELS_READ_VERIFY (ParseSkipFlagCabac (pCtx, &uiNeighAvail, uiCode));1056 ST32 (&pCurLayer->pNzc[iMbXy][20], 0);
1057 1057 }
1058 if (uiCode) {1058 } else {
1059 int16_t pMv[2] = {0};1059 pCurLayer->pLumaQp[iMbXy] = pSlice->iLastMbQp;
1060 pCurLayer->pMbType[iMbXy] = MB_TYPE_SKIP;1060 for (i = 0; i < 2; i++) {
1061 ST32 (&pCurLayer->pNzc[iMbXy][0], 0);1061 pCurLayer->pChromaQp[iMbXy][i] = g_kuiChromaQpTable[WELS_CLIP3 (pCurLayer->pLumaQp[iMbXy] +
1062 ST32 (&pCurLayer->pNzc[iMbXy][4], 0);1062 pSliceHeader->pPps->iChromaQpIndexOffset[i], 0, 51)];
1063 ST32 (&pCurLayer->pNzc[iMbXy][8], 0);1063 }
1064 ST32 (&pCurLayer->pNzc[iMbXy][12], 0);1064 }
1065 ST32 (&pCurLayer->pNzc[iMbXy][16], 0);1065
1066 ST32 (&pCurLayer->pNzc[iMbXy][20], 0);1066 WELS_READ_VERIFY (ParseEndOfSliceCabac (pCtx, uiEosFlag));
1067 1067 if (uiEosFlag) {
1068 pCurLayer->pInterPredictionDoneFlag[iMbXy] = 0;1068 RestoreCabacDecEngineToBS (pCtx->pCabacDecEngine, pCtx->pCurDqLayer->pBitStringAux);
1069 memset (pCurLayer->pRefIndex[0][iMbXy], 0, sizeof (int8_t) * 16);1069 }
1070 pCtx->bMbRefConcealed = pCtx->bRPLRError || pCtx->bMbRefConcealed || ! (ppRefPic[0] && ppRefPic[0]->bIsComplete);1070
1071 //predict mv1071 return ERR_NONE;
1072 PredPSkipMvFromNeighbor (pCurLayer, pMv);1072}
1073 for (i = 0; i < 16; i++) {1073
1074 ST32 (pCurLayer->pMv[0][iMbXy][i], * (uint32_t*)pMv);1074int32_t WelsDecodeMbCabacBSliceBaseMode0 (PWelsDecoderContext pCtx, PWelsNeighAvail pNeighAvail, uint32_t& uiEosFlag) {
1075 ST32 (pCurLayer->pMvd[0][iMbXy][i], 0);1075 PDqLayer pCurLayer = pCtx->pCurDqLayer;
1076 }1076 PBitStringAux pBsAux = pCurLayer->pBitStringAux;
1077 1077 PSlice pSlice = &pCurLayer->sLayerInfo.sSliceInLayer;
1078 //if (!pSlice->sSliceHeaderExt.bDefaultResidualPredFlag) {1078 PSliceHeader pSliceHeader = &pSlice->sSliceHeaderExt.sSliceHeader;
1079 // memset (pCurLayer->pScaledTCoeff[iMbXy], 0, 384 * sizeof (int16_t));1079
1080 //}1080 int32_t iScanIdxStart = pSlice->sSliceHeaderExt.uiScanIdxStart;
1081 1081 int32_t iScanIdxEnd = pSlice->sSliceHeaderExt.uiScanIdxEnd;
1082 //reset rS1082 int32_t iMbXy = pCurLayer->iMbXyIndex;
1083 pCurLayer->pLumaQp[iMbXy] = pSlice->iLastMbQp; //??????????????? dqaunt of previous mb1083 int32_t iMbResProperty;
1084 for (i = 0; i < 2; i++) {1084 int32_t i;
1085 pCurLayer->pChromaQp[iMbXy][i] = g_kuiChromaQpTable[WELS_CLIP3 (pCurLayer->pLumaQp[iMbXy] +1085 uint32_t uiMbType = 0, uiCbp = 0, uiCbpLuma = 0, uiCbpChroma = 0;
1086 pSliceHeader->pPps->iChromaQpIndexOffset[i], 0, 51)];1086
1087 }1087 ENFORCE_STACK_ALIGN_1D (uint8_t, pNonZeroCount, 48, 16);
1088 1088
1089 //for neighboring CABAC usage1089 pCurLayer->pInterPredictionDoneFlag[iMbXy] = 0;
1090 pSlice->iLastDeltaQp = 0;1090
1091 1091 WELS_READ_VERIFY (ParseMBTypeBSliceCabac (pCtx, pNeighAvail, uiMbType));
1092 WELS_READ_VERIFY (ParseEndOfSliceCabac (pCtx, uiEosFlag));1092
1093 1093 if (uiMbType < 23) { //Inter B mode
1094 return ERR_NONE;1094 int16_t pMotionVector[LIST_A][30][MV_A];
1095 }1095 int16_t pMvdCache[LIST_A][30][MV_A];
1096 1096 int8_t pRefIndex[LIST_A][30];
1097 WELS_READ_VERIFY (WelsDecodeMbCabacPSliceBaseMode0 (pCtx, &uiNeighAvail, uiEosFlag));1097 int8_t pDirect[30];
1098 return ERR_NONE;1098 pCurLayer->pMbType[iMbXy] = g_ksInterBMbTypeInfo[uiMbType].iType;
1099}1099 WelsFillCacheInterCabac (pNeighAvail, pNonZeroCount, pMotionVector, pMvdCache, pRefIndex, pCurLayer);
1100// Calculate deqaunt coeff scaling list value1100 WelsFillDirectCacheCabac (pNeighAvail, pDirect, pCurLayer);
1101int32_t WelsCalcDeqCoeffScalingList (PWelsDecoderContext pCtx) {1101 WELS_READ_VERIFY (ParseInterBMotionInfoCabac (pCtx, pNeighAvail, pNonZeroCount, pMotionVector, pMvdCache, pRefIndex,
1102 if (pCtx->pSps->bSeqScalingMatrixPresentFlag || pCtx->pPps->bPicScalingMatrixPresentFlag) {1102 pDirect));
1103 pCtx->bUseScalingList = true;1103 pCurLayer->pInterPredictionDoneFlag[iMbXy] = 0;
1104 1104 } else { //Intra mode
1105 if (!pCtx->bDequantCoeff4x4Init || (pCtx->iDequantCoeffPpsid != pCtx->pPps->iPpsId)) {1105 uiMbType -= 23;
1106 int i, q, x, y;1106 if (uiMbType > 25)
1107 //Init dequant coeff value for different QP1107 return GENERATE_ERROR_NO (ERR_LEVEL_MB_DATA, ERR_INFO_INVALID_MB_TYPE);
1108 for (i = 0; i < 6; i++) {1108 if (!pCtx->pSps->uiChromaFormatIdc && ((uiMbType >= 5 && uiMbType <= 12) || (uiMbType >= 17 && uiMbType <= 24)))
1109 pCtx->pDequant_coeff4x4[i] = pCtx->pDequant_coeff_buffer4x4[i];1109 return GENERATE_ERROR_NO (ERR_LEVEL_MB_DATA, ERR_INFO_INVALID_MB_TYPE);
1110 pCtx->pDequant_coeff8x8[i] = pCtx->pDequant_coeff_buffer8x8[i];1110
1111 for (q = 0; q < 51; q++) {1111 if (25 == uiMbType) { //I_PCM
1112 for (x = 0; x < 16; x++) {1112 WelsLog (& (pCtx->sLogCtx), WELS_LOG_DEBUG, "I_PCM mode exists in P slice!");
1113 pCtx->pDequant_coeff4x4[i][q][x] = pCtx->pPps->bPicScalingMatrixPresentFlag ? pCtx->pPps->iScalingList4x4[i][x] *1113 WELS_READ_VERIFY (ParseIPCMInfoCabac (pCtx));
1114 g_kuiDequantCoeff[q][x & 0x07] : pCtx->pSps->iScalingList4x4[i][x] * g_kuiDequantCoeff[q][x & 0x07];1114 pSlice->iLastDeltaQp = 0;
1115 }1115 WELS_READ_VERIFY (ParseEndOfSliceCabac (pCtx, uiEosFlag));
1116 for (y = 0; y < 64; y++) {1116 if (uiEosFlag) {
1117 pCtx->pDequant_coeff8x8[i][q][y] = pCtx->pPps->bPicScalingMatrixPresentFlag ? pCtx->pPps->iScalingList8x8[i][y] *1117 RestoreCabacDecEngineToBS (pCtx->pCabacDecEngine, pCtx->pCurDqLayer->pBitStringAux);
1118 g_kuiMatrixV[q % 6][y / 8][y % 8] : pCtx->pSps->iScalingList8x8[i][y] * g_kuiMatrixV[q % 6][y / 8][y % 8];1118 }
1119 }1119 return ERR_NONE;
1120 }1120 } else { //normal Intra mode
1121 }1121 if (0 == uiMbType) { //Intra4x4
1122 pCtx->bDequantCoeff4x4Init = true;1122 ENFORCE_STACK_ALIGN_1D (int8_t, pIntraPredMode, 48, 16);
1123 pCtx->iDequantCoeffPpsid = pCtx->pPps->iPpsId;1123 pCurLayer->pMbType[iMbXy] = MB_TYPE_INTRA4x4;
1124 }1124 if (pCtx->pPps->bTransform8x8ModeFlag) {
1125 } else1125 WELS_READ_VERIFY (ParseTransformSize8x8FlagCabac (pCtx, pNeighAvail, pCtx->pCurDqLayer->pTransformSize8x8Flag[iMbXy]));
1126 pCtx->bUseScalingList = false;1126 }
1127 return ERR_NONE;1127 if (pCtx->pCurDqLayer->pTransformSize8x8Flag[iMbXy]) {
1128}1128 uiMbType = pCurLayer->pMbType[iMbXy] = MB_TYPE_INTRA8x8;
1129 1129 pCtx->pFillInfoCacheIntraNxNFunc (pNeighAvail, pNonZeroCount, pIntraPredMode, pCurLayer);
1130int32_t WelsDecodeSlice (PWelsDecoderContext pCtx, bool bFirstSliceInLayer, PNalUnit pNalCur) {1130 WELS_READ_VERIFY (ParseIntra8x8Mode (pCtx, pNeighAvail, pIntraPredMode, pBsAux, pCurLayer));
1131 PDqLayer pCurLayer = pCtx->pCurDqLayer;1131 } else {
1132 PFmo pFmo = pCtx->pFmo;1132 pCtx->pFillInfoCacheIntraNxNFunc (pNeighAvail, pNonZeroCount, pIntraPredMode, pCurLayer);
1133 int32_t iRet;1133 WELS_READ_VERIFY (ParseIntra4x4Mode (pCtx, pNeighAvail, pIntraPredMode, pBsAux, pCurLayer));
1134 int32_t iNextMbXyIndex, iSliceIdc;1134 }
1135 1135 } else { //Intra16x16
1136 PSlice pSlice = &pCurLayer->sLayerInfo.sSliceInLayer;1136 pCurLayer->pMbType[iMbXy] = MB_TYPE_INTRA16x16;
1137 PSliceHeaderExt pSliceHeaderExt = &pSlice->sSliceHeaderExt;1137 pCurLayer->pTransformSize8x8Flag[iMbXy] = false;
1138 PSliceHeader pSliceHeader = &pSliceHeaderExt->sSliceHeader;1138 pCurLayer->pNoSubMbPartSizeLessThan8x8Flag[iMbXy] = true;
1139 int32_t iMbX, iMbY;1139 pCurLayer->pIntraPredMode[iMbXy][7] = (uiMbType - 1) & 3;
1140 const int32_t kiCountNumMb = pSliceHeader->pSps->uiTotalMbCount; //need to be correct when fmo or multi slice1140 pCurLayer->pCbp[iMbXy] = g_kuiI16CbpTable[ (uiMbType - 1) >> 2];
1141 uint32_t uiEosFlag = 0;1141 uiCbpChroma = pCtx->pSps->uiChromaFormatIdc ? pCurLayer->pCbp[iMbXy] >> 4 : 0;
1142 PWelsDecMbFunc pDecMbFunc;1142 uiCbpLuma = pCurLayer->pCbp[iMbXy] & 15;
1143 1143 WelsFillCacheNonZeroCount (pNeighAvail, pNonZeroCount, pCurLayer);
1144 pSlice->iTotalMbInCurSlice = 0; //initialize at the starting of slice decoding.1144 WELS_READ_VERIFY (ParseIntra16x16Mode (pCtx, pNeighAvail, pBsAux, pCurLayer));
1145 1145 }
1146 if (pCtx->pPps->bEntropyCodingModeFlag) {1146 }
1147 if (pSlice->sSliceHeaderExt.bAdaptiveMotionPredFlag ||1147 }
1148 pSlice->sSliceHeaderExt.bAdaptiveBaseModeFlag ||1148
1149 pSlice->sSliceHeaderExt.bAdaptiveResidualPredFlag) {1149 ST32 (&pCurLayer->pNzc[iMbXy][0], 0);
1150 WelsLog (& (pCtx->sLogCtx), WELS_LOG_ERROR,1150 ST32 (&pCurLayer->pNzc[iMbXy][4], 0);
1151 "WelsDecodeSlice()::::ILP flag exist, not supported with CABAC enabled!");1151 ST32 (&pCurLayer->pNzc[iMbXy][8], 0);
1152 pCtx->iErrorCode |= dsBitstreamError;1152 ST32 (&pCurLayer->pNzc[iMbXy][12], 0);
1153 return dsBitstreamError;1153 ST32 (&pCurLayer->pNzc[iMbXy][16], 0);
1154 }1154 ST32 (&pCurLayer->pNzc[iMbXy][20], 0);
1155 if (P_SLICE == pSliceHeader->eSliceType)1155
1156 pDecMbFunc = WelsDecodeMbCabacPSlice;1156 if (MB_TYPE_INTRA16x16 != pCurLayer->pMbType[iMbXy]) {
1157 else //I_SLICE. B_SLICE not supported now1157 WELS_READ_VERIFY (ParseCbpInfoCabac (pCtx, pNeighAvail, uiCbp));
1158 pDecMbFunc = WelsDecodeMbCabacISlice;1158
1159 } else {1159 pCurLayer->pCbp[iMbXy] = uiCbp;
1160 if (P_SLICE == pSliceHeader->eSliceType) {1160 pSlice->iLastDeltaQp = uiCbp == 0 ? 0 : pSlice->iLastDeltaQp;
1161 pDecMbFunc = WelsDecodeMbCavlcPSlice;1161 uiCbpChroma = pCtx->pSps->uiChromaFormatIdc ? pCurLayer->pCbp[iMbXy] >> 4 : 0;
1162 } else { //I_SLICE1162 uiCbpLuma = pCurLayer->pCbp[iMbXy] & 15;
1163 pDecMbFunc = WelsDecodeMbCavlcISlice;1163 }
1164 }1164
1165 }1165 if (pCurLayer->pCbp[iMbXy] || MB_TYPE_INTRA16x16 == pCurLayer->pMbType[iMbXy]) {
1166 1166
1167 if (pSliceHeader->pPps->bConstainedIntraPredFlag) {1167 if (MB_TYPE_INTRA16x16 != pCurLayer->pMbType[iMbXy]) {
1168 pCtx->pFillInfoCacheIntraNxNFunc = WelsFillCacheConstrain1IntraNxN;1168 // Need modification when B picutre add in
1169 pCtx->pMapNxNNeighToSampleFunc = WelsMapNxNNeighToSampleConstrain1;1169 bool bNeedParseTransformSize8x8Flag =
1170 pCtx->pMap16x16NeighToSampleFunc = WelsMap16x16NeighToSampleConstrain1;1170 (((IS_INTER_16x16 (pCurLayer->pMbType[iMbXy]) || IS_DIRECT (pCurLayer->pMbType[iMbXy])
1171 } else {1171 || IS_INTER_16x8 (pCurLayer->pMbType[iMbXy]) || IS_INTER_8x16 (pCurLayer->pMbType[iMbXy]))
1172 pCtx->pFillInfoCacheIntraNxNFunc = WelsFillCacheConstrain0IntraNxN;1172 || pCurLayer->pNoSubMbPartSizeLessThan8x8Flag[iMbXy])
1173 pCtx->pMapNxNNeighToSampleFunc = WelsMapNxNNeighToSampleNormal;1173 && (pCurLayer->pMbType[iMbXy] != MB_TYPE_INTRA8x8)
1174 pCtx->pMap16x16NeighToSampleFunc = WelsMap16x16NeighToSampleNormal;1174 && (pCurLayer->pMbType[iMbXy] != MB_TYPE_INTRA4x4)
1175 }1175 && ((pCurLayer->pCbp[iMbXy] & 0x0F) > 0)
1176 1176 && (pCtx->pPps->bTransform8x8ModeFlag));
1177 pCtx->eSliceType = pSliceHeader->eSliceType;1177
1178 if (pCurLayer->sLayerInfo.pPps->bEntropyCodingModeFlag == 1) {1178 if (bNeedParseTransformSize8x8Flag) {
1179 int32_t iQp = pSlice->sSliceHeaderExt.sSliceHeader.iSliceQp;1179 WELS_READ_VERIFY (ParseTransformSize8x8FlagCabac (pCtx, pNeighAvail,
1180 int32_t iCabacInitIdc = pSlice->sSliceHeaderExt.sSliceHeader.iCabacInitIdc;1180 pCtx->pCurDqLayer->pTransformSize8x8Flag[iMbXy])); //transform_size_8x8_flag
1181 WelsCabacContextInit (pCtx, pSlice->eSliceType, iCabacInitIdc, iQp);1181 }
1182 //InitCabacCtx (pCtx->pCabacCtx, pSlice->eSliceType, iCabacInitIdc, iQp);1182 }
1183 pSlice->iLastDeltaQp = 0;1183
1184 WELS_READ_VERIFY (InitCabacDecEngineFromBS (pCtx->pCabacDecEngine, pCtx->pCurDqLayer->pBitStringAux));1184 memset (pCurLayer->pScaledTCoeff[iMbXy], 0, 384 * sizeof (pCurLayer->pScaledTCoeff[iMbXy][0]));
1185 }1185
1186 //try to calculate the dequant_coeff1186 int32_t iQpDelta, iId8x8, iId4x4;
1187 WelsCalcDeqCoeffScalingList (pCtx);1187
1188 1188 WELS_READ_VERIFY (ParseDeltaQpCabac (pCtx, iQpDelta));
1189 iNextMbXyIndex = pSliceHeader->iFirstMbInSlice;1189 if (iQpDelta > 25 || iQpDelta < -26) { //out of iQpDelta range
1190 iMbX = iNextMbXyIndex % pCurLayer->iMbWidth;1190 return GENERATE_ERROR_NO (ERR_LEVEL_MB_DATA, ERR_INFO_INVALID_QP);
1191 iMbY = iNextMbXyIndex / pCurLayer->iMbWidth; // error is introduced by multiple slices case, 11/23/20091191 }
1192 pSlice->iMbSkipRun = -1;1192 pCurLayer->pLumaQp[iMbXy] = (pSlice->iLastMbQp + iQpDelta + 52) % 52; //update last_mb_qp
1193 iSliceIdc = (pSliceHeader->iFirstMbInSlice << 7) + pCurLayer->uiLayerDqId;1193 pSlice->iLastMbQp = pCurLayer->pLumaQp[iMbXy];
1194 1194 for (i = 0; i < 2; i++) {
1195 pCurLayer->iMbX = iMbX;1195 pCurLayer->pChromaQp[iMbXy][i] = g_kuiChromaQpTable[WELS_CLIP3 (pSlice->iLastMbQp +
1196 pCurLayer->iMbY = iMbY;1196 pSliceHeader->pPps->iChromaQpIndexOffset[i], 0, 51)];
1197 pCurLayer->iMbXyIndex = iNextMbXyIndex;1197 }
1198 1198
1199 do {1199 if (MB_TYPE_INTRA16x16 == pCurLayer->pMbType[iMbXy]) {
1200 if ((-1 == iNextMbXyIndex) || (iNextMbXyIndex >= kiCountNumMb)) { // slice group boundary or end of a frame1200 //step1: Luma DC
1201 break;1201 WELS_READ_VERIFY (ParseResidualBlockCabac (pNeighAvail, pNonZeroCount, pBsAux, 0, 16, g_kuiLumaDcZigzagScan,
1202 }1202 I16_LUMA_DC, pCurLayer->pScaledTCoeff[iMbXy], pCurLayer->pLumaQp[iMbXy], pCtx));
1203 1203 //step2: Luma AC
1204 pCurLayer->pSliceIdc[iNextMbXyIndex] = iSliceIdc;1204 if (uiCbpLuma) {
1205 pCtx->bMbRefConcealed = false;1205 for (i = 0; i < 16; i++) {
1206 iRet = pDecMbFunc (pCtx, pNalCur, uiEosFlag);1206 WELS_READ_VERIFY (ParseResidualBlockCabac (pNeighAvail, pNonZeroCount, pBsAux, i, iScanIdxEnd - WELS_MAX (iScanIdxStart,
1207 pCurLayer->pMbRefConcealedFlag[iNextMbXyIndex] = pCtx->bMbRefConcealed;1207 1) + 1, g_kuiZigzagScan + WELS_MAX (iScanIdxStart, 1), I16_LUMA_AC, pCurLayer->pScaledTCoeff[iMbXy] + (i << 4),
1208 if (iRet != ERR_NONE) {1208 pCurLayer->pLumaQp[iMbXy], pCtx));
1209 return iRet;1209 }
1210 }1210 ST32 (&pCurLayer->pNzc[iMbXy][0], LD32 (&pNonZeroCount[1 + 8 * 1]));
1211 1211 ST32 (&pCurLayer->pNzc[iMbXy][4], LD32 (&pNonZeroCount[1 + 8 * 2]));
1212 ++pSlice->iTotalMbInCurSlice;1212 ST32 (&pCurLayer->pNzc[iMbXy][8], LD32 (&pNonZeroCount[1 + 8 * 3]));
1213 if (uiEosFlag) { //end of slice1213 ST32 (&pCurLayer->pNzc[iMbXy][12], LD32 (&pNonZeroCount[1 + 8 * 4]));
1214 break;1214 } else {
1215 }1215 ST32 (&pCurLayer->pNzc[iMbXy][0], 0);
1216 if (pSliceHeader->pPps->uiNumSliceGroups > 1) {1216 ST32 (&pCurLayer->pNzc[iMbXy][4], 0);
1217 iNextMbXyIndex = FmoNextMb (pFmo, iNextMbXyIndex);1217 ST32 (&pCurLayer->pNzc[iMbXy][8], 0);
1218 } else {1218 ST32 (&pCurLayer->pNzc[iMbXy][12], 0);
1219 ++iNextMbXyIndex;1219 }
1220 }1220 } else { //non-MB_TYPE_INTRA16x16
1221 iMbX = iNextMbXyIndex % pCurLayer->iMbWidth;1221 if (pCtx->pCurDqLayer->pTransformSize8x8Flag[iMbXy]) {
1222 iMbY = iNextMbXyIndex / pCurLayer->iMbWidth;1222 // Transform 8x8 support for CABAC
1223 pCurLayer->iMbX = iMbX;1223 for (iId8x8 = 0; iId8x8 < 4; iId8x8++) {
1224 pCurLayer->iMbY = iMbY;1224 if (uiCbpLuma & (1 << iId8x8)) {
1225 pCurLayer->iMbXyIndex = iNextMbXyIndex;1225 WELS_READ_VERIFY (ParseResidualBlockCabac8x8 (pNeighAvail, pNonZeroCount, pBsAux, (iId8x8 << 2),
1226 } while (1);1226 iScanIdxEnd - iScanIdxStart + 1, g_kuiZigzagScan8x8 + iScanIdxStart,
1227 1227 IS_INTRA (pCurLayer->pMbType[iMbXy]) ? LUMA_DC_AC_INTRA_8 : LUMA_DC_AC_INTER_8,
1228 return ERR_NONE;1228 pCurLayer->pScaledTCoeff[iMbXy] + (iId8x8 << 6), pCurLayer->pLumaQp[iMbXy], pCtx));
1229}1229 } else {
1230 1230 ST16 (&pNonZeroCount[g_kCacheNzcScanIdx[ (iId8x8 << 2)]], 0);
1231int32_t WelsActualDecodeMbCavlcISlice (PWelsDecoderContext pCtx) {1231 ST16 (&pNonZeroCount[g_kCacheNzcScanIdx[ (iId8x8 << 2) + 2]], 0);
1232 SVlcTable* pVlcTable = &pCtx->sVlcTable;1232 }
1233 PDqLayer pCurLayer = pCtx->pCurDqLayer;1233 }
1234 PBitStringAux pBs = pCurLayer->pBitStringAux;1234 ST32 (&pCurLayer->pNzc[iMbXy][0], LD32 (&pNonZeroCount[1 + 8 * 1]));
1235 PSlice pSlice = &pCurLayer->sLayerInfo.sSliceInLayer;1235 ST32 (&pCurLayer->pNzc[iMbXy][4], LD32 (&pNonZeroCount[1 + 8 * 2]));
1236 PSliceHeader pSliceHeader = &pSlice->sSliceHeaderExt.sSliceHeader;1236 ST32 (&pCurLayer->pNzc[iMbXy][8], LD32 (&pNonZeroCount[1 + 8 * 3]));
1237 1237 ST32 (&pCurLayer->pNzc[iMbXy][12], LD32 (&pNonZeroCount[1 + 8 * 4]));
1238 SWelsNeighAvail sNeighAvail;1238 } else {
1239 int32_t iMbResProperty;1239 iMbResProperty = (IS_INTRA (pCurLayer->pMbType[iMbXy])) ? LUMA_DC_AC_INTRA : LUMA_DC_AC_INTER;
1240 1240 for (iId8x8 = 0; iId8x8 < 4; iId8x8++) {
1241 int32_t iScanIdxStart = pSlice->sSliceHeaderExt.uiScanIdxStart;1241 if (uiCbpLuma & (1 << iId8x8)) {
1242 int32_t iScanIdxEnd = pSlice->sSliceHeaderExt.uiScanIdxEnd;1242 int32_t iIdx = (iId8x8 << 2);
1243 1243 for (iId4x4 = 0; iId4x4 < 4; iId4x4++) {
1244 int32_t iMbX = pCurLayer->iMbX;1244 //Luma (DC and AC decoding together)
1245 int32_t iMbY = pCurLayer->iMbY;1245 WELS_READ_VERIFY (ParseResidualBlockCabac (pNeighAvail, pNonZeroCount, pBsAux, iIdx, iScanIdxEnd - iScanIdxStart + 1,
1246 const int32_t iMbXy = pCurLayer->iMbXyIndex;1246 g_kuiZigzagScan + iScanIdxStart, iMbResProperty, pCurLayer->pScaledTCoeff[iMbXy] + (iIdx << 4),
1247 int8_t* pNzc = pCurLayer->pNzc[iMbXy];1247 pCurLayer->pLumaQp[iMbXy],
1248 int32_t i;1248 pCtx));
1249 int32_t iRet = ERR_NONE;1249 iIdx++;
1250 uint32_t uiMbType = 0, uiCbp = 0, uiCbpL = 0, uiCbpC = 0;1250 }
1251 uint32_t uiCode;1251 } else {
1252 int32_t iCode;1252 ST16 (&pNonZeroCount[g_kCacheNzcScanIdx[iId8x8 << 2]], 0);
1253 1253 ST16 (&pNonZeroCount[g_kCacheNzcScanIdx[ (iId8x8 << 2) + 2]], 0);
1254 ENFORCE_STACK_ALIGN_1D (uint8_t, pNonZeroCount, 48, 16);1254 }
1255 GetNeighborAvailMbType (&sNeighAvail, pCurLayer);1255 }
1256 pCurLayer->pInterPredictionDoneFlag[iMbXy] = 0;1256 ST32 (&pCurLayer->pNzc[iMbXy][0], LD32 (&pNonZeroCount[1 + 8 * 1]));
1257 pCurLayer->pResidualPredFlag[iMbXy] = pSlice->sSliceHeaderExt.bDefaultResidualPredFlag;1257 ST32 (&pCurLayer->pNzc[iMbXy][4], LD32 (&pNonZeroCount[1 + 8 * 2]));
1258 1258 ST32 (&pCurLayer->pNzc[iMbXy][8], LD32 (&pNonZeroCount[1 + 8 * 3]));
1259 pCurLayer->pNoSubMbPartSizeLessThan8x8Flag[iMbXy] = true;1259 ST32 (&pCurLayer->pNzc[iMbXy][12], LD32 (&pNonZeroCount[1 + 8 * 4]));
1260 pCurLayer->pTransformSize8x8Flag[iMbXy] = false;1260 }
1261 1261 }
1262 WELS_READ_VERIFY (BsGetUe (pBs, &uiCode)); //uiMbType1262
1263 uiMbType = uiCode;1263 //chroma
1264 if (uiMbType > 25)1264 //step1: DC
1265 return GENERATE_ERROR_NO (ERR_LEVEL_MB_DATA, ERR_INFO_INVALID_MB_TYPE);1265 if (1 == uiCbpChroma || 2 == uiCbpChroma) {
1266 if (!pCtx->pSps->uiChromaFormatIdc && ((uiMbType >= 5 && uiMbType <= 12) || (uiMbType >= 17 && uiMbType <= 24)))1266 for (i = 0; i < 2; i++) {
1267 return GENERATE_ERROR_NO (ERR_LEVEL_MB_DATA, ERR_INFO_INVALID_MB_TYPE);1267 if (IS_INTRA (pCurLayer->pMbType[iMbXy]))
1268 1268 iMbResProperty = i ? CHROMA_DC_V : CHROMA_DC_U;
1269 if (25 == uiMbType) {1269 else
1270 WelsLog (& (pCtx->sLogCtx), WELS_LOG_DEBUG, "I_PCM mode exists in I slice!");1270 iMbResProperty = i ? CHROMA_DC_V_INTER : CHROMA_DC_U_INTER;
1271 int32_t iDecStrideL = pCurLayer->pDec->iLinesize[0];1271
1272 int32_t iDecStrideC = pCurLayer->pDec->iLinesize[1];1272 WELS_READ_VERIFY (ParseResidualBlockCabac (pNeighAvail, pNonZeroCount, pBsAux, 16 + (i << 2), 4, g_kuiChromaDcScan,
1273 1273 iMbResProperty, pCurLayer->pScaledTCoeff[iMbXy] + 256 + (i << 6), pCurLayer->pChromaQp[iMbXy][i], pCtx));
1274 int32_t iOffsetL = (iMbX + iMbY * iDecStrideL) << 4;1274 }
1275 int32_t iOffsetC = (iMbX + iMbY * iDecStrideC) << 3;1275 }
1276 1276 //step2: AC
1277 uint8_t* pDecY = pCurLayer->pDec->pData[0] + iOffsetL;1277 if (2 == uiCbpChroma) {
1278 uint8_t* pDecU = pCurLayer->pDec->pData[1] + iOffsetC;1278 for (i = 0; i < 2; i++) {
1279 uint8_t* pDecV = pCurLayer->pDec->pData[2] + iOffsetC;1279 if (IS_INTRA (pCurLayer->pMbType[iMbXy]))
1280 1280 iMbResProperty = i ? CHROMA_AC_V : CHROMA_AC_U;
1281 uint8_t* pTmpBsBuf;1281 else
1282 1282 iMbResProperty = i ? CHROMA_AC_V_INTER : CHROMA_AC_U_INTER;
1283 1283 int32_t index = 16 + (i << 2);
1284 int32_t i;1284 for (iId4x4 = 0; iId4x4 < 4; iId4x4++) {
1285 int32_t iCopySizeY = (sizeof (uint8_t) << 4);1285 WELS_READ_VERIFY (ParseResidualBlockCabac (pNeighAvail, pNonZeroCount, pBsAux, index,
1286 int32_t iCopySizeUV = (sizeof (uint8_t) << 3);1286 iScanIdxEnd - WELS_MAX (iScanIdxStart, 1) + 1, g_kuiZigzagScan + WELS_MAX (iScanIdxStart, 1),
1287 1287 iMbResProperty, pCurLayer->pScaledTCoeff[iMbXy] + (index << 4), pCurLayer->pChromaQp[iMbXy][i], pCtx));
1288 int32_t iIndex = ((-pBs->iLeftBits) >> 3) + 2;1288 index++;
1289 1289 }
1290 pCurLayer->pMbType[iMbXy] = MB_TYPE_INTRA_PCM;1290 }
1291 1291 ST16 (&pCurLayer->pNzc[iMbXy][16], LD16 (&pNonZeroCount[6 + 8 * 1]));
1292 //step 1: locating bit-stream pointer [must align into integer byte]1292 ST16 (&pCurLayer->pNzc[iMbXy][20], LD16 (&pNonZeroCount[6 + 8 * 2]));
1293 pBs->pCurBuf -= iIndex;1293 ST16 (&pCurLayer->pNzc[iMbXy][18], LD16 (&pNonZeroCount[6 + 8 * 4]));
1294 1294 ST16 (&pCurLayer->pNzc[iMbXy][22], LD16 (&pNonZeroCount[6 + 8 * 5]));
1295 //step 2: copy pixel from bit-stream into fdec [reconstruction]1295 } else {
1296 pTmpBsBuf = pBs->pCurBuf;1296 ST32 (&pCurLayer->pNzc[iMbXy][16], 0);
1297 if (!pCtx->pParam->bParseOnly) {1297 ST32 (&pCurLayer->pNzc[iMbXy][20], 0);
1298 for (i = 0; i < 16; i++) { //luma1298 }
1299 memcpy (pDecY , pTmpBsBuf, iCopySizeY);1299 } else {
1300 pDecY += iDecStrideL;1300 pCurLayer->pLumaQp[iMbXy] = pSlice->iLastMbQp;
1301 pTmpBsBuf += 16;1301 for (i = 0; i < 2; i++) {
1302 }1302 pCurLayer->pChromaQp[iMbXy][i] = g_kuiChromaQpTable[WELS_CLIP3 (pCurLayer->pLumaQp[iMbXy] +
1303 for (i = 0; i < 8; i++) { //cb1303 pSliceHeader->pPps->iChromaQpIndexOffset[i], 0, 51)];
1304 memcpy (pDecU, pTmpBsBuf, iCopySizeUV);1304 }
1305 pDecU += iDecStrideC;1305 }
1306 pTmpBsBuf += 8;1306
1307 }1307 WELS_READ_VERIFY (ParseEndOfSliceCabac (pCtx, uiEosFlag));
1308 for (i = 0; i < 8; i++) { //cr1308 if (uiEosFlag) {
1309 memcpy (pDecV, pTmpBsBuf, iCopySizeUV);1309 RestoreCabacDecEngineToBS (pCtx->pCabacDecEngine, pCtx->pCurDqLayer->pBitStringAux);
1310 pDecV += iDecStrideC;1310 }
1311 pTmpBsBuf += 8;1311
1312 }1312 return ERR_NONE;
1313 }1313}
1314 1314
1315 pBs->pCurBuf += 384;1315
1316 1316int32_t WelsDecodeMbCabacPSlice (PWelsDecoderContext pCtx, PNalUnit pNalCur, uint32_t& uiEosFlag) {
1317 //step 3: update QP and pNonZeroCount1317 PDqLayer pCurLayer = pCtx->pCurDqLayer;
1318 pCurLayer->pLumaQp[iMbXy] = 0;1318 PSlice pSlice = &pCurLayer->sLayerInfo.sSliceInLayer;
1319 memset (pCurLayer->pChromaQp[iMbXy], 0, sizeof (pCurLayer->pChromaQp[iMbXy]));1319 PSliceHeader pSliceHeader = &pSlice->sSliceHeaderExt.sSliceHeader;
1320 memset (pNzc, 16, sizeof (pCurLayer->pNzc[iMbXy])); //Rec. 9.2.1 for PCM, nzc=161320 PPicture* ppRefPic = pCtx->sRefPic.pRefList[LIST_0];
1321 WELS_READ_VERIFY (InitReadBits (pBs, 0));1321 uint32_t uiCode;
1322 return ERR_NONE;1322 int32_t iMbXy = pCurLayer->iMbXyIndex;
1323 } else if (0 == uiMbType) { //reference to JM1323 int32_t i;
1324 ENFORCE_STACK_ALIGN_1D (int8_t, pIntraPredMode, 48, 16);1324 SWelsNeighAvail uiNeighAvail;
1325 pCurLayer->pMbType[iMbXy] = MB_TYPE_INTRA4x4;1325 pCurLayer->pCbp[iMbXy] = 0;
1326 if (pCtx->pPps->bTransform8x8ModeFlag) {1326 pCurLayer->pCbfDc[iMbXy] = 0;
1327 WELS_READ_VERIFY (BsGetOneBit (pBs, &uiCode)); //transform_size_8x8_flag1327 pCurLayer->pChromaPredMode[iMbXy] = C_PRED_DC;
1328 pCurLayer->pTransformSize8x8Flag[iMbXy] = !!uiCode;1328
1329 if (pCurLayer->pTransformSize8x8Flag[iMbXy]) {1329 pCurLayer->pNoSubMbPartSizeLessThan8x8Flag[iMbXy] = true;
1330 uiMbType = pCurLayer->pMbType[iMbXy] = MB_TYPE_INTRA8x8;1330 pCurLayer->pTransformSize8x8Flag[iMbXy] = false;
1331 }1331
1332 }1332 GetNeighborAvailMbType (&uiNeighAvail, pCurLayer);
1333 if (!pCurLayer->pTransformSize8x8Flag[iMbXy]) {1333 WELS_READ_VERIFY (ParseSkipFlagCabac (pCtx, &uiNeighAvail, uiCode));
1334 pCtx->pFillInfoCacheIntraNxNFunc (&sNeighAvail, pNonZeroCount, pIntraPredMode, pCurLayer);1334
1335 WELS_READ_VERIFY (ParseIntra4x4Mode (pCtx, &sNeighAvail, pIntraPredMode, pBs, pCurLayer));1335 if (uiCode) {
1336 } else {1336 int16_t pMv[2] = {0};
1337 pCtx->pFillInfoCacheIntraNxNFunc (&sNeighAvail, pNonZeroCount, pIntraPredMode, pCurLayer);1337 pCurLayer->pMbType[iMbXy] = MB_TYPE_SKIP;
1338 WELS_READ_VERIFY (ParseIntra8x8Mode (pCtx, &sNeighAvail, pIntraPredMode, pBs, pCurLayer));1338 ST32 (&pCurLayer->pNzc[iMbXy][0], 0);
1339 }1339 ST32 (&pCurLayer->pNzc[iMbXy][4], 0);
1340 1340 ST32 (&pCurLayer->pNzc[iMbXy][8], 0);
1341 //uiCbp1341 ST32 (&pCurLayer->pNzc[iMbXy][12], 0);
1342 WELS_READ_VERIFY (BsGetUe (pBs, &uiCode)); //coded_block_pattern1342 ST32 (&pCurLayer->pNzc[iMbXy][16], 0);
1343 uiCbp = uiCode;1343 ST32 (&pCurLayer->pNzc[iMbXy][20], 0);
1344 //G.9.1 Alternative parsing process for coded pBlock pattern1344
1345 if (pCtx->pSps->uiChromaFormatIdc && (uiCbp > 47))1345 pCurLayer->pInterPredictionDoneFlag[iMbXy] = 0;
1346 return GENERATE_ERROR_NO (ERR_LEVEL_MB_DATA, ERR_INFO_INVALID_CBP);1346 memset (pCurLayer->pRefIndex[0][iMbXy], 0, sizeof (int8_t) * 16);
1347 if (!pCtx->pSps->uiChromaFormatIdc && (uiCbp > 15))1347 pCtx->bMbRefConcealed = pCtx->bRPLRError || pCtx->bMbRefConcealed || ! (ppRefPic[0] && ppRefPic[0]->bIsComplete);
1348 return GENERATE_ERROR_NO (ERR_LEVEL_MB_DATA, ERR_INFO_INVALID_CBP);1348 //predict mv
1349 1349 PredPSkipMvFromNeighbor (pCurLayer, pMv);
1350 if (pCtx->pSps->uiChromaFormatIdc)1350 for (i = 0; i < 16; i++) {
1351 uiCbp = g_kuiIntra4x4CbpTable[uiCbp];1351 ST32 (pCurLayer->pMv[0][iMbXy][i], * (uint32_t*)pMv);
1352 else1352 ST32 (pCurLayer->pMvd[0][iMbXy][i], 0);
1353 uiCbp = g_kuiIntra4x4CbpTable400[uiCbp];1353 }
1354 pCurLayer->pCbp[iMbXy] = uiCbp;1354
1355 uiCbpC = uiCbp >> 4;1355 //if (!pSlice->sSliceHeaderExt.bDefaultResidualPredFlag) {
1356 uiCbpL = uiCbp & 15;1356 // memset (pCurLayer->pScaledTCoeff[iMbXy], 0, 384 * sizeof (int16_t));
1357 } else { //I_PCM exclude, we can ignore it1357 //}
1358 pCurLayer->pMbType[iMbXy] = MB_TYPE_INTRA16x16;1358
1359 pCurLayer->pTransformSize8x8Flag[iMbXy] = false;1359 //reset rS
1360 pCurLayer->pNoSubMbPartSizeLessThan8x8Flag[iMbXy] = true;1360 pCurLayer->pLumaQp[iMbXy] = pSlice->iLastMbQp; //??????????????? dqaunt of previous mb
1361 pCurLayer->pIntraPredMode[iMbXy][7] = (uiMbType - 1) & 3;1361 for (i = 0; i < 2; i++) {
1362 pCurLayer->pCbp[iMbXy] = g_kuiI16CbpTable[ (uiMbType - 1) >> 2];1362 pCurLayer->pChromaQp[iMbXy][i] = g_kuiChromaQpTable[WELS_CLIP3 (pCurLayer->pLumaQp[iMbXy] +
1363 uiCbpC = pCtx->pSps->uiChromaFormatIdc ? pCurLayer->pCbp[iMbXy] >> 4 : 0;1363 pSliceHeader->pPps->iChromaQpIndexOffset[i], 0, 51)];
1364 uiCbpL = pCurLayer->pCbp[iMbXy] & 15;1364 }
1365 WelsFillCacheNonZeroCount (&sNeighAvail, pNonZeroCount, pCurLayer);1365
1366 WELS_READ_VERIFY (ParseIntra16x16Mode (pCtx, &sNeighAvail, pBs, pCurLayer));1366 //for neighboring CABAC usage
1367 }1367 pSlice->iLastDeltaQp = 0;
1368 1368
1369 ST32A4 (&pNzc[0], 0);1369 WELS_READ_VERIFY (ParseEndOfSliceCabac (pCtx, uiEosFlag));
1370 ST32A4 (&pNzc[4], 0);1370
1371 ST32A4 (&pNzc[8], 0);1371 return ERR_NONE;
1372 ST32A4 (&pNzc[12], 0);1372 }
1373 ST32A4 (&pNzc[16], 0);1373
1374 ST32A4 (&pNzc[20], 0);1374 WELS_READ_VERIFY (WelsDecodeMbCabacPSliceBaseMode0 (pCtx, &uiNeighAvail, uiEosFlag));
1375 1375 return ERR_NONE;
1376 if (pCurLayer->pCbp[iMbXy] == 0 && IS_INTRANxN (pCurLayer->pMbType[iMbXy])) {1376}
1377 pCurLayer->pLumaQp[iMbXy] = pSlice->iLastMbQp;1377
1378 for (i = 0; i < 2; i++) {1378
1379 pCurLayer->pChromaQp[iMbXy][i] = g_kuiChromaQpTable[WELS_CLIP3 (pCurLayer->pLumaQp[iMbXy] +1379int32_t WelsDecodeMbCabacBSlice (PWelsDecoderContext pCtx, PNalUnit pNalCur, uint32_t& uiEosFlag) {
1380 pSliceHeader->pPps->iChromaQpIndexOffset[i], 0, 51)];1380 PDqLayer pCurLayer = pCtx->pCurDqLayer;
1381 }1381 PSlice pSlice = &pCurLayer->sLayerInfo.sSliceInLayer;
1382 1382 PSliceHeader pSliceHeader = &pSlice->sSliceHeaderExt.sSliceHeader;
1383 }1383 PPicture* ppRefPicL0 = pCtx->sRefPic.pRefList[LIST_0];
1384 1384 PPicture* ppRefPicL1 = pCtx->sRefPic.pRefList[LIST_1];
1385 if (pCurLayer->pCbp[iMbXy] || MB_TYPE_INTRA16x16 == pCurLayer->pMbType[iMbXy]) {1385 uint32_t uiCode;
1386 memset (pCurLayer->pScaledTCoeff[iMbXy], 0, 384 * sizeof (pCurLayer->pScaledTCoeff[iMbXy][0]));1386 int32_t iMbXy = pCurLayer->iMbXyIndex;
1387 int32_t iQpDelta, iId8x8, iId4x4;1387 int32_t i;
1388 1388 SWelsNeighAvail uiNeighAvail;
1389 WELS_READ_VERIFY (BsGetSe (pBs, &iCode)); //mb_qp_delta1389 pCurLayer->pCbp[iMbXy] = 0;
1390 iQpDelta = iCode;1390 pCurLayer->pCbfDc[iMbXy] = 0;
1391 1391 pCurLayer->pChromaPredMode[iMbXy] = C_PRED_DC;
1392 if (iQpDelta > 25 || iQpDelta < -26) { //out of iQpDelta range1392
1393 return GENERATE_ERROR_NO (ERR_LEVEL_MB_DATA, ERR_INFO_INVALID_QP);1393 pCurLayer->pNoSubMbPartSizeLessThan8x8Flag[iMbXy] = true;
1394 }1394 pCurLayer->pTransformSize8x8Flag[iMbXy] = false;
1395 1395
1396 pCurLayer->pLumaQp[iMbXy] = (pSlice->iLastMbQp + iQpDelta + 52) % 52; //update last_mb_qp1396 GetNeighborAvailMbType (&uiNeighAvail, pCurLayer);
1397 pSlice->iLastMbQp = pCurLayer->pLumaQp[iMbXy];1397 WELS_READ_VERIFY (ParseSkipFlagCabac (pCtx, &uiNeighAvail, uiCode));
1398 for (i = 0; i < 2; i++) {1398
1399 pCurLayer->pChromaQp[iMbXy][i] = g_kuiChromaQpTable[WELS_CLIP3 (pSlice->iLastMbQp +1399 memset (pCurLayer->pDirect[iMbXy], 0, sizeof (int8_t) * 16);
1400 pSliceHeader->pPps->iChromaQpIndexOffset[i], 0,1400
1401 51)];1401 if (uiCode) {
1402 }1402 int16_t pMv[LIST_A][2] = { {0, 0}, { 0, 0 } };
1403 1403 int8_t ref[LIST_A] = { 0 };
1404 1404 pCurLayer->pMbType[iMbXy] = MB_TYPE_SKIP | MB_TYPE_DIRECT;
1405 BsStartCavlc (pBs);1405 ST32 (&pCurLayer->pNzc[iMbXy][0], 0);
1406 1406 ST32 (&pCurLayer->pNzc[iMbXy][4], 0);
1407 if (MB_TYPE_INTRA16x16 == pCurLayer->pMbType[iMbXy]) {1407 ST32 (&pCurLayer->pNzc[iMbXy][8], 0);
1408 //step1: Luma DC1408 ST32 (&pCurLayer->pNzc[iMbXy][12], 0);
1409 if ((iRet = WelsResidualBlockCavlc (pVlcTable, pNonZeroCount, pBs, 0, 16, g_kuiLumaDcZigzagScan, I16_LUMA_DC,1409 ST32 (&pCurLayer->pNzc[iMbXy][16], 0);
1410 pCurLayer->pScaledTCoeff[iMbXy], pCurLayer->pLumaQp[iMbXy], pCtx)) != ERR_NONE) {1410 ST32 (&pCurLayer->pNzc[iMbXy][20], 0);
1411 return iRet;//abnormal1411
1412 }1412 pCurLayer->pInterPredictionDoneFlag[iMbXy] = 0;
1413 //step2: Luma AC1413 memset (pCurLayer->pRefIndex[LIST_0][iMbXy], 0, sizeof (int8_t) * 16);
1414 if (uiCbpL) {1414 memset (pCurLayer->pRefIndex[LIST_1][iMbXy], 0, sizeof (int8_t) * 16);
1415 for (i = 0; i < 16; i++) {1415 pCtx->bMbRefConcealed = pCtx->bRPLRError || pCtx->bMbRefConcealed || ! (ppRefPicL0[0] && ppRefPicL0[0]->bIsComplete)
1416 if ((iRet = WelsResidualBlockCavlc (pVlcTable, pNonZeroCount, pBs, i, iScanIdxEnd - WELS_MAX (iScanIdxStart, 1) + 1,1416 || ! (ppRefPicL1[0] && ppRefPicL1[0]->bIsComplete);
1417 g_kuiZigzagScan + WELS_MAX (iScanIdxStart, 1), I16_LUMA_AC, pCurLayer->pScaledTCoeff[iMbXy] + (i << 4),1417
1418 pCurLayer->pLumaQp[iMbXy], pCtx)) != ERR_NONE) {1418 if (pSliceHeader->iDirectSpatialMvPredFlag) {
1419 return iRet;//abnormal1419
1420 }1420 //predict direct spatial mv
1421 }1421 PredMvBDirectSpatial (pCtx, pMv, ref);
1422 ST32A4 (&pNzc[0], LD32 (&pNonZeroCount[1 + 8 * 1]));1422 } else {
1423 ST32A4 (&pNzc[4], LD32 (&pNonZeroCount[1 + 8 * 2]));1423 //temporal direct mode
1424 ST32A4 (&pNzc[8], LD32 (&pNonZeroCount[1 + 8 * 3]));1424 ComputeColocated (pCtx);
1425 ST32A4 (&pNzc[12], LD32 (&pNonZeroCount[1 + 8 * 4]));1425 PredBDirectTemporal (pCtx, pMv, ref);
1426 }1426 }
1427 } else { //non-MB_TYPE_INTRA16x161427
1428 if (pCurLayer->pTransformSize8x8Flag[iMbXy]) {1428 //reset rS
1429 for (iId8x8 = 0; iId8x8 < 4; iId8x8++) {1429 pCurLayer->pLumaQp[iMbXy] = pSlice->iLastMbQp; //??????????????? dqaunt of previous mb
1430 iMbResProperty = (IS_INTRA (pCurLayer->pMbType[iMbXy])) ? LUMA_DC_AC_INTRA_8 : LUMA_DC_AC_INTER_8;1430 for (i = 0; i < 2; i++) {
1431 if (uiCbpL & (1 << iId8x8)) {1431 pCurLayer->pChromaQp[iMbXy][i] = g_kuiChromaQpTable[WELS_CLIP3 (pCurLayer->pLumaQp[iMbXy] +
1432 int32_t iIndex = (iId8x8 << 2);1432 pSliceHeader->pPps->iChromaQpIndexOffset[i], 0, 51)];
1433 for (iId4x4 = 0; iId4x4 < 4; iId4x4++) {1433 }
1434 if ((iRet = WelsResidualBlockCavlc8x8 (pVlcTable, pNonZeroCount, pBs, iIndex, iScanIdxEnd - iScanIdxStart + 1,1434
1435 g_kuiZigzagScan8x8 + iScanIdxStart, iMbResProperty, pCurLayer->pScaledTCoeff[iMbXy] + (iId8x8 << 6), iId4x4,1435 //for neighboring CABAC usage
1436 pCurLayer->pLumaQp[iMbXy], pCtx)) != ERR_NONE) {1436 pSlice->iLastDeltaQp = 0;
1437 return iRet;1437
1438 }1438 WELS_READ_VERIFY (ParseEndOfSliceCabac (pCtx, uiEosFlag));
1439 iIndex++;1439
1440 }1440 return ERR_NONE;
1441 } else {1441 }
1442 ST16 (&pNonZeroCount[g_kuiCache48CountScan4Idx[iId8x8 << 2]], 0);1442
1443 ST16 (&pNonZeroCount[g_kuiCache48CountScan4Idx[ (iId8x8 << 2) + 2]], 0);1443 WELS_READ_VERIFY (WelsDecodeMbCabacBSliceBaseMode0 (pCtx, &uiNeighAvail, uiEosFlag));
1444 }1444 return ERR_NONE;
1445 }1445}
1446 ST32A4 (&pNzc[0], LD32 (&pNonZeroCount[1 + 8 * 1]));1446
1447 ST32A4 (&pNzc[4], LD32 (&pNonZeroCount[1 + 8 * 2]));1447// Calculate deqaunt coeff scaling list value
1448 ST32A4 (&pNzc[8], LD32 (&pNonZeroCount[1 + 8 * 3]));1448int32_t WelsCalcDeqCoeffScalingList (PWelsDecoderContext pCtx) {
1449 ST32A4 (&pNzc[12], LD32 (&pNonZeroCount[1 + 8 * 4]));1449 if (pCtx->pSps->bSeqScalingMatrixPresentFlag || pCtx->pPps->bPicScalingMatrixPresentFlag) {
1450 } else {1450 pCtx->bUseScalingList = true;
1451 for (iId8x8 = 0; iId8x8 < 4; iId8x8++) {1451
1452 if (uiCbpL & (1 << iId8x8)) {1452 if (!pCtx->bDequantCoeff4x4Init || (pCtx->iDequantCoeffPpsid != pCtx->pPps->iPpsId)) {
1453 int32_t iIndex = (iId8x8 << 2);1453 int i, q, x, y;
1454 for (iId4x4 = 0; iId4x4 < 4; iId4x4++) {1454 //Init dequant coeff value for different QP
1455 //Luma (DC and AC decoding together)1455 for (i = 0; i < 6; i++) {
1456 if ((iRet = WelsResidualBlockCavlc (pVlcTable, pNonZeroCount, pBs, iIndex, iScanIdxEnd - iScanIdxStart + 1,1456 pCtx->pDequant_coeff4x4[i] = pCtx->pDequant_coeff_buffer4x4[i];
1457 g_kuiZigzagScan + iScanIdxStart, LUMA_DC_AC_INTRA, pCurLayer->pScaledTCoeff[iMbXy] + (iIndex << 4),1457 pCtx->pDequant_coeff8x8[i] = pCtx->pDequant_coeff_buffer8x8[i];
1458 pCurLayer->pLumaQp[iMbXy], pCtx)) != ERR_NONE) {1458 for (q = 0; q < 51; q++) {
1459 return iRet;//abnormal1459 for (x = 0; x < 16; x++) {
1460 }1460 pCtx->pDequant_coeff4x4[i][q][x] = pCtx->pPps->bPicScalingMatrixPresentFlag ? pCtx->pPps->iScalingList4x4[i][x] *
1461 iIndex++;1461 g_kuiDequantCoeff[q][x & 0x07] : pCtx->pSps->iScalingList4x4[i][x] * g_kuiDequantCoeff[q][x & 0x07];
1462 }1462 }
1463 } else {1463 for (y = 0; y < 64; y++) {
1464 ST16 (&pNonZeroCount[g_kuiCache48CountScan4Idx[ (iId8x8 << 2)]], 0);1464 pCtx->pDequant_coeff8x8[i][q][y] = pCtx->pPps->bPicScalingMatrixPresentFlag ? pCtx->pPps->iScalingList8x8[i][y] *
1465 ST16 (&pNonZeroCount[g_kuiCache48CountScan4Idx[ (iId8x8 << 2) + 2]], 0);1465 g_kuiMatrixV[q % 6][y / 8][y % 8] : pCtx->pSps->iScalingList8x8[i][y] * g_kuiMatrixV[q % 6][y / 8][y % 8];
1466 }1466 }
1467 }1467 }
1468 ST32A4 (&pNzc[0], LD32 (&pNonZeroCount[1 + 8 * 1]));1468 }
1469 ST32A4 (&pNzc[4], LD32 (&pNonZeroCount[1 + 8 * 2]));1469 pCtx->bDequantCoeff4x4Init = true;
1470 ST32A4 (&pNzc[8], LD32 (&pNonZeroCount[1 + 8 * 3]));1470 pCtx->iDequantCoeffPpsid = pCtx->pPps->iPpsId;
1471 ST32A4 (&pNzc[12], LD32 (&pNonZeroCount[1 + 8 * 4]));1471 }
1472 }1472 } else
1473 }1473 pCtx->bUseScalingList = false;
1474 1474 return ERR_NONE;
1475 //chroma1475}
1476 //step1: DC1476
1477 if (1 == uiCbpC || 2 == uiCbpC) {1477int32_t WelsDecodeSlice (PWelsDecoderContext pCtx, bool bFirstSliceInLayer, PNalUnit pNalCur) {
1478 for (i = 0; i < 2; i++) { //Cb Cr1478 PDqLayer pCurLayer = pCtx->pCurDqLayer;
1479 iMbResProperty = i ? CHROMA_DC_V : CHROMA_DC_U;1479 PFmo pFmo = pCtx->pFmo;
1480 if ((iRet = WelsResidualBlockCavlc (pVlcTable, pNonZeroCount, pBs, 16 + (i << 2), 4, g_kuiChromaDcScan, iMbResProperty,1480 int32_t iRet;
1481 pCurLayer->pScaledTCoeff[iMbXy] + 256 + (i << 6), pCurLayer->pChromaQp[iMbXy][i], pCtx)) != ERR_NONE) {1481 int32_t iNextMbXyIndex, iSliceIdc;
1482 return iRet;//abnormal1482
1483 }1483 PSlice pSlice = &pCurLayer->sLayerInfo.sSliceInLayer;
1484 }1484 PSliceHeaderExt pSliceHeaderExt = &pSlice->sSliceHeaderExt;
1485 }1485 PSliceHeader pSliceHeader = &pSliceHeaderExt->sSliceHeader;
1486 1486 int32_t iMbX, iMbY;
1487 //step2: AC1487 const int32_t kiCountNumMb = pSliceHeader->pSps->uiTotalMbCount; //need to be correct when fmo or multi slice
1488 if (2 == uiCbpC) {1488 uint32_t uiEosFlag = 0;
1489 for (i = 0; i < 2; i++) { //Cb Cr1489 PWelsDecMbFunc pDecMbFunc;
1490 iMbResProperty = i ? CHROMA_AC_V : CHROMA_AC_U;1490
1491 int32_t iIndex = 16 + (i << 2);1491 pSlice->iTotalMbInCurSlice = 0; //initialize at the starting of slice decoding.
1492 for (iId4x4 = 0; iId4x4 < 4; iId4x4++) {1492
1493 if ((iRet = WelsResidualBlockCavlc (pVlcTable, pNonZeroCount, pBs, iIndex, iScanIdxEnd - WELS_MAX (iScanIdxStart,1493 if (pCtx->pPps->bEntropyCodingModeFlag) {
1494 1) + 1, g_kuiZigzagScan + WELS_MAX (iScanIdxStart, 1), iMbResProperty, pCurLayer->pScaledTCoeff[iMbXy] + (iIndex << 4),1494 if (pSlice->sSliceHeaderExt.bAdaptiveMotionPredFlag ||
1495 pCurLayer->pChromaQp[iMbXy][i], pCtx)) != ERR_NONE) {1495 pSlice->sSliceHeaderExt.bAdaptiveBaseModeFlag ||
1496 return iRet;//abnormal1496 pSlice->sSliceHeaderExt.bAdaptiveResidualPredFlag) {
1497 }1497 WelsLog (& (pCtx->sLogCtx), WELS_LOG_ERROR,
1498 iIndex++;1498 "WelsDecodeSlice()::::ILP flag exist, not supported with CABAC enabled!");
1499 }1499 pCtx->iErrorCode |= dsBitstreamError;
1500 }1500 return dsBitstreamError;
1501 ST16A2 (&pNzc[16], LD16A2 (&pNonZeroCount[6 + 8 * 1]));1501 }
1502 ST16A2 (&pNzc[20], LD16A2 (&pNonZeroCount[6 + 8 * 2]));1502 if (P_SLICE == pSliceHeader->eSliceType)
1503 ST16A2 (&pNzc[18], LD16A2 (&pNonZeroCount[6 + 8 * 4]));1503 pDecMbFunc = WelsDecodeMbCabacPSlice;
1504 ST16A2 (&pNzc[22], LD16A2 (&pNonZeroCount[6 + 8 * 5]));1504 else if (B_SLICE == pSliceHeader->eSliceType)
1505 }1505 pDecMbFunc = WelsDecodeMbCabacBSlice;
1506 BsEndCavlc (pBs);1506 else //I_SLICE. B_SLICE is being supported
1507 }1507 pDecMbFunc = WelsDecodeMbCabacISlice;
1508 1508 } else {
1509 return ERR_NONE;1509 if (P_SLICE == pSliceHeader->eSliceType) {
1510}1510 pDecMbFunc = WelsDecodeMbCavlcPSlice;
1511 1511 } else { //I_SLICE
1512int32_t WelsDecodeMbCavlcISlice (PWelsDecoderContext pCtx, PNalUnit pNalCur, uint32_t& uiEosFlag) {1512 pDecMbFunc = WelsDecodeMbCavlcISlice;
1513 PDqLayer pCurLayer = pCtx->pCurDqLayer;1513 }
1514 PBitStringAux pBs = pCurLayer->pBitStringAux;1514 }
1515 PSliceHeaderExt pSliceHeaderExt = &pCurLayer->sLayerInfo.sSliceInLayer.sSliceHeaderExt;1515
1516 int32_t iBaseModeFlag;1516 if (pSliceHeader->pPps->bConstainedIntraPredFlag) {
1517 int32_t iRet = 0; //should have the return value to indicate decoding error or not, It's NECESSARY--2010.4.151517 pCtx->pFillInfoCacheIntraNxNFunc = WelsFillCacheConstrain1IntraNxN;
1518 uint32_t uiCode;1518 pCtx->pMapNxNNeighToSampleFunc = WelsMapNxNNeighToSampleConstrain1;
1519 intX_t iUsedBits;1519 pCtx->pMap16x16NeighToSampleFunc = WelsMap16x16NeighToSampleConstrain1;
1520 if (pSliceHeaderExt->bAdaptiveBaseModeFlag == 1) {1520 } else {
1521 WELS_READ_VERIFY (BsGetOneBit (pBs, &uiCode)); //base_mode_flag1521 pCtx->pFillInfoCacheIntraNxNFunc = WelsFillCacheConstrain0IntraNxN;
1522 iBaseModeFlag = uiCode;1522 pCtx->pMapNxNNeighToSampleFunc = WelsMapNxNNeighToSampleNormal;
1523 } else {1523 pCtx->pMap16x16NeighToSampleFunc = WelsMap16x16NeighToSampleNormal;
1524 iBaseModeFlag = pSliceHeaderExt->bDefaultBaseModeFlag;1524 }
1525 }1525
1526 if (!iBaseModeFlag) {1526 pCtx->eSliceType = pSliceHeader->eSliceType;
1527 iRet = WelsActualDecodeMbCavlcISlice (pCtx);1527 if (pCurLayer->sLayerInfo.pPps->bEntropyCodingModeFlag == 1) {
1528 } else {1528 int32_t iQp = pSlice->sSliceHeaderExt.sSliceHeader.iSliceQp;
1529 WelsLog (& (pCtx->sLogCtx), WELS_LOG_WARNING, "iBaseModeFlag (%d) != 0, inter-layer prediction not supported.",1529 int32_t iCabacInitIdc = pSlice->sSliceHeaderExt.sSliceHeader.iCabacInitIdc;
1530 iBaseModeFlag);1530 WelsCabacContextInit (pCtx, pSlice->eSliceType, iCabacInitIdc, iQp);
1531 return GENERATE_ERROR_NO (ERR_LEVEL_SLICE_HEADER, ERR_INFO_UNSUPPORTED_ILP);1531 //InitCabacCtx (pCtx->pCabacCtx, pSlice->eSliceType, iCabacInitIdc, iQp);
1532 }1532 pSlice->iLastDeltaQp = 0;
1533 if (iRet) { //occur error when parsing, MUST STOP decoding1533 WELS_READ_VERIFY (InitCabacDecEngineFromBS (pCtx->pCabacDecEngine, pCtx->pCurDqLayer->pBitStringAux));
1534 return iRet;1534 }
1535 }1535 //try to calculate the dequant_coeff
1536 1536 WelsCalcDeqCoeffScalingList (pCtx);
1537 // check whether there is left bits to read next time in case multiple slices1537
1538 iUsedBits = ((pBs->pCurBuf - pBs->pStartBuf) << 3) - (16 - pBs->iLeftBits);1538 iNextMbXyIndex = pSliceHeader->iFirstMbInSlice;
1539 // sub 1, for stop bit1539 iMbX = iNextMbXyIndex % pCurLayer->iMbWidth;
1540 if ((iUsedBits == (pBs->iBits - 1)) && (0 >= pCurLayer->sLayerInfo.sSliceInLayer.iMbSkipRun)) { // slice boundary1540 iMbY = iNextMbXyIndex / pCurLayer->iMbWidth; // error is introduced by multiple slices case, 11/23/2009
1541 uiEosFlag = 1;1541 pSlice->iMbSkipRun = -1;
1542 }1542 iSliceIdc = (pSliceHeader->iFirstMbInSlice << 7) + pCurLayer->uiLayerDqId;
1543 if (iUsedBits > (pBs->iBits -1543
1544 1)) { //When BS incomplete, as long as find it, SHOULD stop decoding to avoid mosaic or crash.1544 pCurLayer->iMbX = iMbX;
1545 WelsLog (& (pCtx->sLogCtx), WELS_LOG_WARNING,1545 pCurLayer->iMbY = iMbY;
1546 "WelsDecodeMbCavlcISlice()::::pBs incomplete, iUsedBits:%" PRId64 " > pBs->iBits:%d, MUST stop decoding.",1546 pCurLayer->iMbXyIndex = iNextMbXyIndex;
1547 (int64_t) iUsedBits, pBs->iBits);1547
1548 return GENERATE_ERROR_NO (ERR_LEVEL_MB_DATA, ERR_INFO_BS_INCOMPLETE);1548 do {
1549 }1549 if ((-1 == iNextMbXyIndex) || (iNextMbXyIndex >= kiCountNumMb)) { // slice group boundary or end of a frame
1550 return ERR_NONE;1550 break;
1551}1551 }
1552 1552
1553int32_t WelsActualDecodeMbCavlcPSlice (PWelsDecoderContext pCtx) {1553 pCurLayer->pSliceIdc[iNextMbXyIndex] = iSliceIdc;
1554 SVlcTable* pVlcTable = &pCtx->sVlcTable;1554 pCtx->bMbRefConcealed = false;
1555 PDqLayer pCurLayer = pCtx->pCurDqLayer;1555 iRet = pDecMbFunc (pCtx, pNalCur, uiEosFlag);
1556 PBitStringAux pBs = pCurLayer->pBitStringAux;1556 pCurLayer->pMbRefConcealedFlag[iNextMbXyIndex] = pCtx->bMbRefConcealed;
1557 PSlice pSlice = &pCurLayer->sLayerInfo.sSliceInLayer;1557 if (iRet != ERR_NONE) {
1558 PSliceHeader pSliceHeader = &pSlice->sSliceHeaderExt.sSliceHeader;1558 return iRet;
1559 1559 }
1560 int32_t iScanIdxStart = pSlice->sSliceHeaderExt.uiScanIdxStart;1560
1561 int32_t iScanIdxEnd = pSlice->sSliceHeaderExt.uiScanIdxEnd;1561 ++pSlice->iTotalMbInCurSlice;
1562 1562 if (uiEosFlag) { //end of slice
1563 SWelsNeighAvail sNeighAvail;1563 break;
1564 int32_t iMbX = pCurLayer->iMbX;1564 }
1565 int32_t iMbY = pCurLayer->iMbY;1565 if (pSliceHeader->pPps->uiNumSliceGroups > 1) {
1566 const int32_t iMbXy = pCurLayer->iMbXyIndex;1566 iNextMbXyIndex = FmoNextMb (pFmo, iNextMbXyIndex);
1567 int8_t* pNzc = pCurLayer->pNzc[iMbXy];1567 } else {
1568 int32_t i;1568 ++iNextMbXyIndex;
1569 int32_t iRet = ERR_NONE;1569 }
1570 uint32_t uiMbType = 0, uiCbp = 0, uiCbpL = 0, uiCbpC = 0;1570 iMbX = iNextMbXyIndex % pCurLayer->iMbWidth;
1571 uint32_t uiCode;1571 iMbY = iNextMbXyIndex / pCurLayer->iMbWidth;
1572 int32_t iCode;1572 pCurLayer->iMbX = iMbX;
1573 int32_t iMbResProperty;1573 pCurLayer->iMbY = iMbY;
1574 1574 pCurLayer->iMbXyIndex = iNextMbXyIndex;
1575 GetNeighborAvailMbType (&sNeighAvail, pCurLayer);1575 } while (1);
1576 ENFORCE_STACK_ALIGN_1D (uint8_t, pNonZeroCount, 48, 16);1576
1577 pCurLayer->pInterPredictionDoneFlag[iMbXy] = 0;//2009.10.231577 return ERR_NONE;
1578 WELS_READ_VERIFY (BsGetUe (pBs, &uiCode)); //uiMbType1578}
1579 uiMbType = uiCode;1579
1580 if (uiMbType < 5) { //inter MB type1580int32_t WelsActualDecodeMbCavlcISlice (PWelsDecoderContext pCtx) {
1581 int16_t iMotionVector[LIST_A][30][MV_A];1581 SVlcTable* pVlcTable = &pCtx->sVlcTable;
1582 int8_t iRefIndex[LIST_A][30];1582 PDqLayer pCurLayer = pCtx->pCurDqLayer;
1583 pCurLayer->pMbType[iMbXy] = g_ksInterMbTypeInfo[uiMbType].iType;1583 PBitStringAux pBs = pCurLayer->pBitStringAux;
1584 WelsFillCacheInter (&sNeighAvail, pNonZeroCount, iMotionVector, iRefIndex, pCurLayer);1584 PSlice pSlice = &pCurLayer->sLayerInfo.sSliceInLayer;
1585 1585 PSliceHeader pSliceHeader = &pSlice->sSliceHeaderExt.sSliceHeader;
1586 if ((iRet = ParseInterInfo (pCtx, iMotionVector, iRefIndex, pBs)) != ERR_NONE) {1586
1587 return iRet;//abnormal1587 SWelsNeighAvail sNeighAvail;
1588 }1588 int32_t iMbResProperty;
1589 1589
1590 if (pSlice->sSliceHeaderExt.bAdaptiveResidualPredFlag == 1) {1590 int32_t iScanIdxStart = pSlice->sSliceHeaderExt.uiScanIdxStart;
1591 WELS_READ_VERIFY (BsGetOneBit (pBs, &uiCode)); //residual_prediction_flag1591 int32_t iScanIdxEnd = pSlice->sSliceHeaderExt.uiScanIdxEnd;
1592 pCurLayer->pResidualPredFlag[iMbXy] = uiCode;1592
1593 } else {1593 int32_t iMbX = pCurLayer->iMbX;
1594 pCurLayer->pResidualPredFlag[iMbXy] = pSlice->sSliceHeaderExt.bDefaultResidualPredFlag;1594 int32_t iMbY = pCurLayer->iMbY;
1595 }1595 const int32_t iMbXy = pCurLayer->iMbXyIndex;
1596 1596 int8_t* pNzc = pCurLayer->pNzc[iMbXy];
1597 if (pCurLayer->pResidualPredFlag[iMbXy] == 0) {1597 int32_t i;
1598 pCurLayer->pInterPredictionDoneFlag[iMbXy] = 0;1598 int32_t iRet = ERR_NONE;
1599 } else {1599 uint32_t uiMbType = 0, uiCbp = 0, uiCbpL = 0, uiCbpC = 0;
1600 WelsLog (& (pCtx->sLogCtx), WELS_LOG_WARNING, "residual_pred_flag = 1 not supported.");1600 uint32_t uiCode;
1601 return GENERATE_ERROR_NO (ERR_LEVEL_MB_DATA, ERR_INFO_UNSUPPORTED_ILP);1601 int32_t iCode;
1602 }1602
1603 } else { //intra MB type1603 ENFORCE_STACK_ALIGN_1D (uint8_t, pNonZeroCount, 48, 16);
1604 uiMbType -= 5;1604 GetNeighborAvailMbType (&sNeighAvail, pCurLayer);
1605 if (uiMbType > 25)1605 pCurLayer->pInterPredictionDoneFlag[iMbXy] = 0;
1606 return GENERATE_ERROR_NO (ERR_LEVEL_MB_DATA, ERR_INFO_INVALID_MB_TYPE);1606 pCurLayer->pResidualPredFlag[iMbXy] = pSlice->sSliceHeaderExt.bDefaultResidualPredFlag;
1607 if (!pCtx->pSps->uiChromaFormatIdc && ((uiMbType >= 5 && uiMbType <= 12) || (uiMbType >= 17 && uiMbType <= 24)))1607
1608 return GENERATE_ERROR_NO (ERR_LEVEL_MB_DATA, ERR_INFO_INVALID_MB_TYPE);1608 pCurLayer->pNoSubMbPartSizeLessThan8x8Flag[iMbXy] = true;
1609 1609 pCurLayer->pTransformSize8x8Flag[iMbXy] = false;
1610 if (25 == uiMbType) {1610
1611 WelsLog (& (pCtx->sLogCtx), WELS_LOG_DEBUG, "I_PCM mode exists in P slice!");1611 WELS_READ_VERIFY (BsGetUe (pBs, &uiCode)); //uiMbType
1612 int32_t iDecStrideL = pCurLayer->pDec->iLinesize[0];1612 uiMbType = uiCode;
1613 int32_t iDecStrideC = pCurLayer->pDec->iLinesize[1];1613 if (uiMbType > 25)
1614 1614 return GENERATE_ERROR_NO (ERR_LEVEL_MB_DATA, ERR_INFO_INVALID_MB_TYPE);
1615 int32_t iOffsetL = (iMbX + iMbY * iDecStrideL) << 4;1615 if (!pCtx->pSps->uiChromaFormatIdc && ((uiMbType >= 5 && uiMbType <= 12) || (uiMbType >= 17 && uiMbType <= 24)))
1616 int32_t iOffsetC = (iMbX + iMbY * iDecStrideC) << 3;1616 return GENERATE_ERROR_NO (ERR_LEVEL_MB_DATA, ERR_INFO_INVALID_MB_TYPE);
1617 1617
1618 uint8_t* pDecY = pCurLayer->pDec->pData[0] + iOffsetL;1618 if (25 == uiMbType) {
1619 uint8_t* pDecU = pCurLayer->pDec->pData[1] + iOffsetC;1619 WelsLog (& (pCtx->sLogCtx), WELS_LOG_DEBUG, "I_PCM mode exists in I slice!");
1620 uint8_t* pDecV = pCurLayer->pDec->pData[2] + iOffsetC;1620 int32_t iDecStrideL = pCurLayer->pDec->iLinesize[0];
1621 1621 int32_t iDecStrideC = pCurLayer->pDec->iLinesize[1];
1622 uint8_t* pTmpBsBuf;1622
1623 1623 int32_t iOffsetL = (iMbX + iMbY * iDecStrideL) << 4;
1624 int32_t i;1624 int32_t iOffsetC = (iMbX + iMbY * iDecStrideC) << 3;
1625 int32_t iCopySizeY = (sizeof (uint8_t) << 4);1625
1626 int32_t iCopySizeUV = (sizeof (uint8_t) << 3);1626 uint8_t* pDecY = pCurLayer->pDec->pData[0] + iOffsetL;
1627 1627 uint8_t* pDecU = pCurLayer->pDec->pData[1] + iOffsetC;
1628 int32_t iIndex = ((-pBs->iLeftBits) >> 3) + 2;1628 uint8_t* pDecV = pCurLayer->pDec->pData[2] + iOffsetC;
1629 1629
1630 pCurLayer->pMbType[iMbXy] = MB_TYPE_INTRA_PCM;1630 uint8_t* pTmpBsBuf;
1631 1631
1632 //step 1: locating bit-stream pointer [must align into integer byte]1632
1633 pBs->pCurBuf -= iIndex;1633 int32_t i;
1634 1634 int32_t iCopySizeY = (sizeof (uint8_t) << 4);
1635 //step 2: copy pixel from bit-stream into fdec [reconstruction]1635 int32_t iCopySizeUV = (sizeof (uint8_t) << 3);
1636 pTmpBsBuf = pBs->pCurBuf;1636
1637 if (!pCtx->pParam->bParseOnly) {1637 int32_t iIndex = ((-pBs->iLeftBits) >> 3) + 2;
1638 for (i = 0; i < 16; i++) { //luma1638
1639 memcpy (pDecY, pTmpBsBuf, iCopySizeY);1639 pCurLayer->pMbType[iMbXy] = MB_TYPE_INTRA_PCM;
1640 pDecY += iDecStrideL;1640
1641 pTmpBsBuf += 16;1641 //step 1: locating bit-stream pointer [must align into integer byte]
1642 }1642 pBs->pCurBuf -= iIndex;
1643 1643
1644 for (i = 0; i < 8; i++) { //cb1644 //step 2: copy pixel from bit-stream into fdec [reconstruction]
1645 memcpy (pDecU, pTmpBsBuf, iCopySizeUV);1645 pTmpBsBuf = pBs->pCurBuf;
1646 pDecU += iDecStrideC;1646 if (!pCtx->pParam->bParseOnly) {
1647 pTmpBsBuf += 8;1647 for (i = 0; i < 16; i++) { //luma
1648 }1648 memcpy (pDecY, pTmpBsBuf, iCopySizeY);
1649 for (i = 0; i < 8; i++) { //cr1649 pDecY += iDecStrideL;
1650 memcpy (pDecV, pTmpBsBuf, iCopySizeUV);1650 pTmpBsBuf += 16;
1651 pDecV += iDecStrideC;1651 }
1652 pTmpBsBuf += 8;1652 for (i = 0; i < 8; i++) { //cb
1653 }1653 memcpy (pDecU, pTmpBsBuf, iCopySizeUV);
1654 }1654 pDecU += iDecStrideC;
1655 1655 pTmpBsBuf += 8;
1656 pBs->pCurBuf += 384;1656 }
1657 1657 for (i = 0; i < 8; i++) { //cr
1658 //step 3: update QP and pNonZeroCount1658 memcpy (pDecV, pTmpBsBuf, iCopySizeUV);
1659 pCurLayer->pLumaQp[iMbXy] = 0;1659 pDecV += iDecStrideC;
1660 pCurLayer->pChromaQp[iMbXy][0] = pCurLayer->pChromaQp[iMbXy][1] = 0;1660 pTmpBsBuf += 8;
1661 //Rec. 9.2.1 for PCM, nzc=161661 }
1662 ST32A4 (&pNzc[0], 0x10101010);1662 }
1663 ST32A4 (&pNzc[4], 0x10101010);1663
1664 ST32A4 (&pNzc[8], 0x10101010);1664 pBs->pCurBuf += 384;
1665 ST32A4 (&pNzc[12], 0x10101010);1665
1666 ST32A4 (&pNzc[16], 0x10101010);1666 //step 3: update QP and pNonZeroCount
1667 ST32A4 (&pNzc[20], 0x10101010);1667 pCurLayer->pLumaQp[iMbXy] = 0;
1668 WELS_READ_VERIFY (InitReadBits (pBs, 0));1668 memset (pCurLayer->pChromaQp[iMbXy], 0, sizeof (pCurLayer->pChromaQp[iMbXy]));
1669 return ERR_NONE;1669 memset (pNzc, 16, sizeof (pCurLayer->pNzc[iMbXy])); //Rec. 9.2.1 for PCM, nzc=16
1670 } else {1670 WELS_READ_VERIFY (InitReadBits (pBs, 0));
1671 if (0 == uiMbType) {1671 return ERR_NONE;
1672 ENFORCE_STACK_ALIGN_1D (int8_t, pIntraPredMode, 48, 16);1672 } else if (0 == uiMbType) { //reference to JM
1673 pCurLayer->pMbType[iMbXy] = MB_TYPE_INTRA4x4;1673 ENFORCE_STACK_ALIGN_1D (int8_t, pIntraPredMode, 48, 16);
1674 if (pCtx->pPps->bTransform8x8ModeFlag) {1674 pCurLayer->pMbType[iMbXy] = MB_TYPE_INTRA4x4;
1675 WELS_READ_VERIFY (BsGetOneBit (pBs, &uiCode)); //transform_size_8x8_flag1675 if (pCtx->pPps->bTransform8x8ModeFlag) {
1676 pCurLayer->pTransformSize8x8Flag[iMbXy] = !!uiCode;1676 WELS_READ_VERIFY (BsGetOneBit (pBs, &uiCode)); //transform_size_8x8_flag
1677 if (pCurLayer->pTransformSize8x8Flag[iMbXy]) {1677 pCurLayer->pTransformSize8x8Flag[iMbXy] = !!uiCode;
1678 uiMbType = pCurLayer->pMbType[iMbXy] = MB_TYPE_INTRA8x8;1678 if (pCurLayer->pTransformSize8x8Flag[iMbXy]) {
1679 }1679 uiMbType = pCurLayer->pMbType[iMbXy] = MB_TYPE_INTRA8x8;
1680 }1680 }
1681 if (!pCurLayer->pTransformSize8x8Flag[iMbXy]) {1681 }
1682 pCtx->pFillInfoCacheIntraNxNFunc (&sNeighAvail, pNonZeroCount, pIntraPredMode, pCurLayer);1682 if (!pCurLayer->pTransformSize8x8Flag[iMbXy]) {
1683 WELS_READ_VERIFY (ParseIntra4x4Mode (pCtx, &sNeighAvail, pIntraPredMode, pBs, pCurLayer));1683 pCtx->pFillInfoCacheIntraNxNFunc (&sNeighAvail, pNonZeroCount, pIntraPredMode, pCurLayer);
1684 } else {1684 WELS_READ_VERIFY (ParseIntra4x4Mode (pCtx, &sNeighAvail, pIntraPredMode, pBs, pCurLayer));
1685 pCtx->pFillInfoCacheIntraNxNFunc (&sNeighAvail, pNonZeroCount, pIntraPredMode, pCurLayer);1685 } else {
1686 WELS_READ_VERIFY (ParseIntra8x8Mode (pCtx, &sNeighAvail, pIntraPredMode, pBs, pCurLayer));1686 pCtx->pFillInfoCacheIntraNxNFunc (&sNeighAvail, pNonZeroCount, pIntraPredMode, pCurLayer);
1687 }1687 WELS_READ_VERIFY (ParseIntra8x8Mode (pCtx, &sNeighAvail, pIntraPredMode, pBs, pCurLayer));
1688 } else { //I_PCM exclude, we can ignore it1688 }
1689 pCurLayer->pMbType[iMbXy] = MB_TYPE_INTRA16x16;1689
1690 pCurLayer->pTransformSize8x8Flag[iMbXy] = false;1690 //uiCbp
1691 pCurLayer->pNoSubMbPartSizeLessThan8x8Flag[iMbXy] = true;1691 WELS_READ_VERIFY (BsGetUe (pBs, &uiCode)); //coded_block_pattern
1692 pCurLayer->pIntraPredMode[iMbXy][7] = (uiMbType - 1) & 3;1692 uiCbp = uiCode;
1693 pCurLayer->pCbp[iMbXy] = g_kuiI16CbpTable[ (uiMbType - 1) >> 2];1693 //G.9.1 Alternative parsing process for coded pBlock pattern
1694 uiCbpC = pCtx->pSps->uiChromaFormatIdc ? pCurLayer->pCbp[iMbXy] >> 4 : 0;1694 if (pCtx->pSps->uiChromaFormatIdc && (uiCbp > 47))
1695 uiCbpL = pCurLayer->pCbp[iMbXy] & 15;1695 return GENERATE_ERROR_NO (ERR_LEVEL_MB_DATA, ERR_INFO_INVALID_CBP);
1696 WelsFillCacheNonZeroCount (&sNeighAvail, pNonZeroCount, pCurLayer);1696 if (!pCtx->pSps->uiChromaFormatIdc && (uiCbp > 15))
1697 if ((iRet = ParseIntra16x16Mode (pCtx, &sNeighAvail, pBs, pCurLayer)) != ERR_NONE) {1697 return GENERATE_ERROR_NO (ERR_LEVEL_MB_DATA, ERR_INFO_INVALID_CBP);
1698 return iRet;1698
1699 }1699 if (pCtx->pSps->uiChromaFormatIdc)
1700 }1700 uiCbp = g_kuiIntra4x4CbpTable[uiCbp];
1701 }1701 else
1702 }1702 uiCbp = g_kuiIntra4x4CbpTable400[uiCbp];
1703 1703 pCurLayer->pCbp[iMbXy] = uiCbp;
1704 if (MB_TYPE_INTRA16x16 != pCurLayer->pMbType[iMbXy]) {1704 uiCbpC = uiCbp >> 4;
1705 WELS_READ_VERIFY (BsGetUe (pBs, &uiCode)); //coded_block_pattern1705 uiCbpL = uiCbp & 15;
1706 uiCbp = uiCode;1706 } else { //I_PCM exclude, we can ignore it
1707 {1707 pCurLayer->pMbType[iMbXy] = MB_TYPE_INTRA16x16;
1708 if (pCtx->pSps->uiChromaFormatIdc && (uiCbp > 47))1708 pCurLayer->pTransformSize8x8Flag[iMbXy] = false;
1709 return GENERATE_ERROR_NO (ERR_LEVEL_MB_DATA, ERR_INFO_INVALID_CBP);1709 pCurLayer->pNoSubMbPartSizeLessThan8x8Flag[iMbXy] = true;
1710 if (!pCtx->pSps->uiChromaFormatIdc && (uiCbp > 15))1710 pCurLayer->pIntraPredMode[iMbXy][7] = (uiMbType - 1) & 3;
1711 return GENERATE_ERROR_NO (ERR_LEVEL_MB_DATA, ERR_INFO_INVALID_CBP);1711 pCurLayer->pCbp[iMbXy] = g_kuiI16CbpTable[ (uiMbType - 1) >> 2];
1712 if (MB_TYPE_INTRA4x4 == pCurLayer->pMbType[iMbXy] || MB_TYPE_INTRA8x8 == pCurLayer->pMbType[iMbXy]) {1712 uiCbpC = pCtx->pSps->uiChromaFormatIdc ? pCurLayer->pCbp[iMbXy] >> 4 : 0;
1713 1713 uiCbpL = pCurLayer->pCbp[iMbXy] & 15;
1714 uiCbp = pCtx->pSps->uiChromaFormatIdc ? g_kuiIntra4x4CbpTable[uiCbp] : g_kuiIntra4x4CbpTable400[uiCbp];1714 WelsFillCacheNonZeroCount (&sNeighAvail, pNonZeroCount, pCurLayer);
1715 } else //inter1715 WELS_READ_VERIFY (ParseIntra16x16Mode (pCtx, &sNeighAvail, pBs, pCurLayer));
1716 uiCbp = pCtx->pSps->uiChromaFormatIdc ? g_kuiInterCbpTable[uiCbp] : g_kuiInterCbpTable400[uiCbp];1716 }
1717 }1717
1718 1718 ST32A4 (&pNzc[0], 0);
1719 pCurLayer->pCbp[iMbXy] = uiCbp;1719 ST32A4 (&pNzc[4], 0);
1720 uiCbpC = pCurLayer->pCbp[iMbXy] >> 4;1720 ST32A4 (&pNzc[8], 0);
1721 uiCbpL = pCurLayer->pCbp[iMbXy] & 15;1721 ST32A4 (&pNzc[12], 0);
1722 1722 ST32A4 (&pNzc[16], 0);
1723 // Need modification when B picutre add in1723 ST32A4 (&pNzc[20], 0);
1724 bool bNeedParseTransformSize8x8Flag =1724
1725 (((pCurLayer->pMbType[iMbXy] >= MB_TYPE_16x16 && pCurLayer->pMbType[iMbXy] <= MB_TYPE_8x16)1725 if (pCurLayer->pCbp[iMbXy] == 0 && IS_INTRANxN (pCurLayer->pMbType[iMbXy])) {
1726 || pCurLayer->pNoSubMbPartSizeLessThan8x8Flag[iMbXy])1726 pCurLayer->pLumaQp[iMbXy] = pSlice->iLastMbQp;
1727 && (pCurLayer->pMbType[iMbXy] != MB_TYPE_INTRA8x8)1727 for (i = 0; i < 2; i++) {
1728 && (pCurLayer->pMbType[iMbXy] != MB_TYPE_INTRA4x4)1728 pCurLayer->pChromaQp[iMbXy][i] = g_kuiChromaQpTable[WELS_CLIP3 (pCurLayer->pLumaQp[iMbXy] +
1729 && (uiCbpL > 0)1729 pSliceHeader->pPps->iChromaQpIndexOffset[i], 0, 51)];
1730 && (pCtx->pPps->bTransform8x8ModeFlag));1730 }
1731 1731
1732 if (bNeedParseTransformSize8x8Flag) {1732 }
1733 WELS_READ_VERIFY (BsGetOneBit (pBs, &uiCode)); //transform_size_8x8_flag1733
1734 pCurLayer->pTransformSize8x8Flag[iMbXy] = !!uiCode;1734 if (pCurLayer->pCbp[iMbXy] || MB_TYPE_INTRA16x16 == pCurLayer->pMbType[iMbXy]) {
1735 }1735 memset (pCurLayer->pScaledTCoeff[iMbXy], 0, 384 * sizeof (pCurLayer->pScaledTCoeff[iMbXy][0]));
1736 }1736 int32_t iQpDelta, iId8x8, iId4x4;
1737 1737
1738 ST32A4 (&pNzc[0], 0);1738 WELS_READ_VERIFY (BsGetSe (pBs, &iCode)); //mb_qp_delta
1739 ST32A4 (&pNzc[4], 0);1739 iQpDelta = iCode;
1740 ST32A4 (&pNzc[8], 0);1740
1741 ST32A4 (&pNzc[12], 0);1741 if (iQpDelta > 25 || iQpDelta < -26) { //out of iQpDelta range
1742 ST32A4 (&pNzc[16], 0);1742 return GENERATE_ERROR_NO (ERR_LEVEL_MB_DATA, ERR_INFO_INVALID_QP);
1743 ST32A4 (&pNzc[20], 0);1743 }
1744 if (pCurLayer->pCbp[iMbXy] == 0 && !IS_INTRA16x16 (pCurLayer->pMbType[iMbXy]) && !IS_I_BL (pCurLayer->pMbType[iMbXy])) {1744
1745 pCurLayer->pLumaQp[iMbXy] = pSlice->iLastMbQp;1745 pCurLayer->pLumaQp[iMbXy] = (pSlice->iLastMbQp + iQpDelta + 52) % 52; //update last_mb_qp
1746 for (i = 0; i < 2; i++) {1746 pSlice->iLastMbQp = pCurLayer->pLumaQp[iMbXy];
1747 pCurLayer->pChromaQp[iMbXy][i] = g_kuiChromaQpTable[WELS_CLIP3 (pCurLayer->pLumaQp[iMbXy] +1747 for (i = 0; i < 2; i++) {
1748 pSliceHeader->pPps->iChromaQpIndexOffset[i], 0, 51)];1748 pCurLayer->pChromaQp[iMbXy][i] = g_kuiChromaQpTable[WELS_CLIP3 (pSlice->iLastMbQp +
1749 }1749 pSliceHeader->pPps->iChromaQpIndexOffset[i], 0,
1750 }1750 51)];
1751 1751 }
1752 if (pCurLayer->pCbp[iMbXy] || MB_TYPE_INTRA16x16 == pCurLayer->pMbType[iMbXy]) {1752
1753 int32_t iQpDelta, iId8x8, iId4x4;1753
1754 memset (pCurLayer->pScaledTCoeff[iMbXy], 0, MB_COEFF_LIST_SIZE * sizeof (int16_t));1754 BsStartCavlc (pBs);
1755 WELS_READ_VERIFY (BsGetSe (pBs, &iCode)); //mb_qp_delta1755
1756 iQpDelta = iCode;1756 if (MB_TYPE_INTRA16x16 == pCurLayer->pMbType[iMbXy]) {
1757 1757 //step1: Luma DC
1758 if (iQpDelta > 25 || iQpDelta < -26) { //out of iQpDelta range1758 if ((iRet = WelsResidualBlockCavlc (pVlcTable, pNonZeroCount, pBs, 0, 16, g_kuiLumaDcZigzagScan, I16_LUMA_DC,
1759 return GENERATE_ERROR_NO (ERR_LEVEL_MB_DATA, ERR_INFO_INVALID_QP);1759 pCurLayer->pScaledTCoeff[iMbXy], pCurLayer->pLumaQp[iMbXy], pCtx)) != ERR_NONE) {
1760 }1760 return iRet;//abnormal
1761 1761 }
1762 pCurLayer->pLumaQp[iMbXy] = (pSlice->iLastMbQp + iQpDelta + 52) % 52; //update last_mb_qp1762 //step2: Luma AC
1763 pSlice->iLastMbQp = pCurLayer->pLumaQp[iMbXy];1763 if (uiCbpL) {
1764 for (i = 0; i < 2; i++) {1764 for (i = 0; i < 16; i++) {
1765 pCurLayer->pChromaQp[iMbXy][i] = g_kuiChromaQpTable[WELS_CLIP3 (pSlice->iLastMbQp +1765 if ((iRet = WelsResidualBlockCavlc (pVlcTable, pNonZeroCount, pBs, i, iScanIdxEnd - WELS_MAX (iScanIdxStart, 1) + 1,
1766 pSliceHeader->pPps->iChromaQpIndexOffset[i], 0,1766 g_kuiZigzagScan + WELS_MAX (iScanIdxStart, 1), I16_LUMA_AC, pCurLayer->pScaledTCoeff[iMbXy] + (i << 4),
1767 51)];1767 pCurLayer->pLumaQp[iMbXy], pCtx)) != ERR_NONE) {
1768 }1768 return iRet;//abnormal
1769 1769 }
1770 BsStartCavlc (pBs);1770 }
1771 1771 ST32A4 (&pNzc[0], LD32 (&pNonZeroCount[1 + 8 * 1]));
1772 if (MB_TYPE_INTRA16x16 == pCurLayer->pMbType[iMbXy]) {1772 ST32A4 (&pNzc[4], LD32 (&pNonZeroCount[1 + 8 * 2]));
1773 //step1: Luma DC1773 ST32A4 (&pNzc[8], LD32 (&pNonZeroCount[1 + 8 * 3]));
1774 if ((iRet = WelsResidualBlockCavlc (pVlcTable, pNonZeroCount, pBs, 0, 16, g_kuiLumaDcZigzagScan, I16_LUMA_DC,1774 ST32A4 (&pNzc[12], LD32 (&pNonZeroCount[1 + 8 * 4]));
1775 pCurLayer->pScaledTCoeff[iMbXy], pCurLayer->pLumaQp[iMbXy], pCtx)) != ERR_NONE) {1775 }
1776 return iRet;//abnormal1776 } else { //non-MB_TYPE_INTRA16x16
1777 }1777 if (pCurLayer->pTransformSize8x8Flag[iMbXy]) {
1778 //step2: Luma AC1778 for (iId8x8 = 0; iId8x8 < 4; iId8x8++) {
1779 if (uiCbpL) {1779 iMbResProperty = (IS_INTRA (pCurLayer->pMbType[iMbXy])) ? LUMA_DC_AC_INTRA_8 : LUMA_DC_AC_INTER_8;
1780 for (i = 0; i < 16; i++) {1780 if (uiCbpL & (1 << iId8x8)) {
1781 if ((iRet = WelsResidualBlockCavlc (pVlcTable, pNonZeroCount, pBs, i, iScanIdxEnd - WELS_MAX (iScanIdxStart, 1) + 1,1781 int32_t iIndex = (iId8x8 << 2);
1782 g_kuiZigzagScan + WELS_MAX (iScanIdxStart, 1), I16_LUMA_AC, pCurLayer->pScaledTCoeff[iMbXy] + (i << 4),1782 for (iId4x4 = 0; iId4x4 < 4; iId4x4++) {
1783 pCurLayer->pLumaQp[iMbXy], pCtx)) != ERR_NONE) {1783 if ((iRet = WelsResidualBlockCavlc8x8 (pVlcTable, pNonZeroCount, pBs, iIndex, iScanIdxEnd - iScanIdxStart + 1,
1784 return iRet;//abnormal1784 g_kuiZigzagScan8x8 + iScanIdxStart, iMbResProperty, pCurLayer->pScaledTCoeff[iMbXy] + (iId8x8 << 6), iId4x4,
1785 }1785 pCurLayer->pLumaQp[iMbXy], pCtx)) != ERR_NONE) {
1786 }1786 return iRet;
1787 ST32A4 (&pNzc[0], LD32 (&pNonZeroCount[1 + 8 * 1]));1787 }
1788 ST32A4 (&pNzc[4], LD32 (&pNonZeroCount[1 + 8 * 2]));1788 iIndex++;
1789 ST32A4 (&pNzc[8], LD32 (&pNonZeroCount[1 + 8 * 3]));1789 }
1790 ST32A4 (&pNzc[12], LD32 (&pNonZeroCount[1 + 8 * 4]));1790 } else {
1791 }1791 ST16 (&pNonZeroCount[g_kuiCache48CountScan4Idx[iId8x8 << 2]], 0);
1792 } else { //non-MB_TYPE_INTRA16x161792 ST16 (&pNonZeroCount[g_kuiCache48CountScan4Idx[ (iId8x8 << 2) + 2]], 0);
1793 if (pCurLayer->pTransformSize8x8Flag[iMbXy]) {1793 }
1794 for (iId8x8 = 0; iId8x8 < 4; iId8x8++) {1794 }
1795 iMbResProperty = (IS_INTRA (pCurLayer->pMbType[iMbXy])) ? LUMA_DC_AC_INTRA_8 : LUMA_DC_AC_INTER_8;1795 ST32A4 (&pNzc[0], LD32 (&pNonZeroCount[1 + 8 * 1]));
1796 if (uiCbpL & (1 << iId8x8)) {1796 ST32A4 (&pNzc[4], LD32 (&pNonZeroCount[1 + 8 * 2]));
1797 int32_t iIndex = (iId8x8 << 2);1797 ST32A4 (&pNzc[8], LD32 (&pNonZeroCount[1 + 8 * 3]));
1798 for (iId4x4 = 0; iId4x4 < 4; iId4x4++) {1798 ST32A4 (&pNzc[12], LD32 (&pNonZeroCount[1 + 8 * 4]));
1799 if ((iRet = WelsResidualBlockCavlc8x8 (pVlcTable, pNonZeroCount, pBs, iIndex, iScanIdxEnd - iScanIdxStart + 1,1799 } else {
1800 g_kuiZigzagScan8x8 + iScanIdxStart, iMbResProperty, pCurLayer->pScaledTCoeff[iMbXy] + (iId8x8 << 6), iId4x4,1800 for (iId8x8 = 0; iId8x8 < 4; iId8x8++) {
1801 pCurLayer->pLumaQp[iMbXy], pCtx)) != ERR_NONE) {1801 if (uiCbpL & (1 << iId8x8)) {
1802 return iRet;1802 int32_t iIndex = (iId8x8 << 2);
1803 }1803 for (iId4x4 = 0; iId4x4 < 4; iId4x4++) {
1804 iIndex++;1804 //Luma (DC and AC decoding together)
1805 }1805 if ((iRet = WelsResidualBlockCavlc (pVlcTable, pNonZeroCount, pBs, iIndex, iScanIdxEnd - iScanIdxStart + 1,
1806 } else {1806 g_kuiZigzagScan + iScanIdxStart, LUMA_DC_AC_INTRA, pCurLayer->pScaledTCoeff[iMbXy] + (iIndex << 4),
1807 ST16 (&pNonZeroCount[g_kuiCache48CountScan4Idx[iId8x8 << 2]], 0);1807 pCurLayer->pLumaQp[iMbXy], pCtx)) != ERR_NONE) {
1808 ST16 (&pNonZeroCount[g_kuiCache48CountScan4Idx[ (iId8x8 << 2) + 2]], 0);1808 return iRet;//abnormal
1809 }1809 }
1810 }1810 iIndex++;
1811 ST32A4 (&pNzc[0], LD32 (&pNonZeroCount[1 + 8 * 1]));1811 }
1812 ST32A4 (&pNzc[4], LD32 (&pNonZeroCount[1 + 8 * 2]));1812 } else {
1813 ST32A4 (&pNzc[8], LD32 (&pNonZeroCount[1 + 8 * 3]));1813 ST16 (&pNonZeroCount[g_kuiCache48CountScan4Idx[ (iId8x8 << 2)]], 0);
1814 ST32A4 (&pNzc[12], LD32 (&pNonZeroCount[1 + 8 * 4]));1814 ST16 (&pNonZeroCount[g_kuiCache48CountScan4Idx[ (iId8x8 << 2) + 2]], 0);
1815 } else { // Normal T4x41815 }
1816 for (iId8x8 = 0; iId8x8 < 4; iId8x8++) {1816 }
1817 iMbResProperty = (IS_INTRA (pCurLayer->pMbType[iMbXy])) ? LUMA_DC_AC_INTRA : LUMA_DC_AC_INTER;1817 ST32A4 (&pNzc[0], LD32 (&pNonZeroCount[1 + 8 * 1]));
1818 if (uiCbpL & (1 << iId8x8)) {1818 ST32A4 (&pNzc[4], LD32 (&pNonZeroCount[1 + 8 * 2]));
1819 int32_t iIndex = (iId8x8 << 2);1819 ST32A4 (&pNzc[8], LD32 (&pNonZeroCount[1 + 8 * 3]));
1820 for (iId4x4 = 0; iId4x4 < 4; iId4x4++) {1820 ST32A4 (&pNzc[12], LD32 (&pNonZeroCount[1 + 8 * 4]));
1821 //Luma (DC and AC decoding together)1821 }
1822 if ((iRet = WelsResidualBlockCavlc (pVlcTable, pNonZeroCount, pBs, iIndex, iScanIdxEnd - iScanIdxStart + 1,1822 }
1823 g_kuiZigzagScan + iScanIdxStart, iMbResProperty, pCurLayer->pScaledTCoeff[iMbXy] + (iIndex << 4),1823
1824 pCurLayer->pLumaQp[iMbXy], pCtx)) != ERR_NONE) {1824 //chroma
1825 return iRet;//abnormal1825 //step1: DC
1826 }1826 if (1 == uiCbpC || 2 == uiCbpC) {
1827 iIndex++;1827 for (i = 0; i < 2; i++) { //Cb Cr
1828 }1828 iMbResProperty = i ? CHROMA_DC_V : CHROMA_DC_U;
1829 } else {1829 if ((iRet = WelsResidualBlockCavlc (pVlcTable, pNonZeroCount, pBs, 16 + (i << 2), 4, g_kuiChromaDcScan, iMbResProperty,
1830 ST16 (&pNonZeroCount[g_kuiCache48CountScan4Idx[iId8x8 << 2]], 0);1830 pCurLayer->pScaledTCoeff[iMbXy] + 256 + (i << 6), pCurLayer->pChromaQp[iMbXy][i], pCtx)) != ERR_NONE) {
1831 ST16 (&pNonZeroCount[g_kuiCache48CountScan4Idx[ (iId8x8 << 2) + 2]], 0);1831 return iRet;//abnormal
1832 }1832 }
1833 }1833 }
1834 ST32A4 (&pNzc[0], LD32 (&pNonZeroCount[1 + 8 * 1]));1834 }
1835 ST32A4 (&pNzc[4], LD32 (&pNonZeroCount[1 + 8 * 2]));1835
1836 ST32A4 (&pNzc[8], LD32 (&pNonZeroCount[1 + 8 * 3]));1836 //step2: AC
1837 ST32A4 (&pNzc[12], LD32 (&pNonZeroCount[1 + 8 * 4]));1837 if (2 == uiCbpC) {
1838 }1838 for (i = 0; i < 2; i++) { //Cb Cr
1839 }1839 iMbResProperty = i ? CHROMA_AC_V : CHROMA_AC_U;
1840 1840 int32_t iIndex = 16 + (i << 2);
1841 1841 for (iId4x4 = 0; iId4x4 < 4; iId4x4++) {
1842 //chroma1842 if ((iRet = WelsResidualBlockCavlc (pVlcTable, pNonZeroCount, pBs, iIndex, iScanIdxEnd - WELS_MAX (iScanIdxStart,
1843 //step1: DC1843 1) + 1, g_kuiZigzagScan + WELS_MAX (iScanIdxStart, 1), iMbResProperty, pCurLayer->pScaledTCoeff[iMbXy] + (iIndex << 4),
1844 if (1 == uiCbpC || 2 == uiCbpC) {1844 pCurLayer->pChromaQp[iMbXy][i], pCtx)) != ERR_NONE) {
1845 for (i = 0; i < 2; i++) { //Cb Cr1845 return iRet;//abnormal
1846 if (IS_INTRA (pCurLayer->pMbType[iMbXy]))1846 }
1847 iMbResProperty = i ? CHROMA_DC_V : CHROMA_DC_U;1847 iIndex++;
1848 else1848 }
1849 iMbResProperty = i ? CHROMA_DC_V_INTER : CHROMA_DC_U_INTER;1849 }
1850 1850 ST16A2 (&pNzc[16], LD16A2 (&pNonZeroCount[6 + 8 * 1]));
1851 if ((iRet = WelsResidualBlockCavlc (pVlcTable, pNonZeroCount, pBs, 16 + (i << 2), 4, g_kuiChromaDcScan, iMbResProperty,1851 ST16A2 (&pNzc[20], LD16A2 (&pNonZeroCount[6 + 8 * 2]));
1852 pCurLayer->pScaledTCoeff[iMbXy] + 256 + (i << 6), pCurLayer->pChromaQp[iMbXy][i], pCtx)) != ERR_NONE) {1852 ST16A2 (&pNzc[18], LD16A2 (&pNonZeroCount[6 + 8 * 4]));
1853 return iRet;//abnormal1853 ST16A2 (&pNzc[22], LD16A2 (&pNonZeroCount[6 + 8 * 5]));
1854 }1854 }
1855 }1855 BsEndCavlc (pBs);
1856 } else {1856 }
1857 }1857
1858 //step2: AC1858 return ERR_NONE;
1859 if (2 == uiCbpC) {1859}
1860 for (i = 0; i < 2; i++) { //Cb Cr1860
1861 if (IS_INTRA (pCurLayer->pMbType[iMbXy]))1861int32_t WelsDecodeMbCavlcISlice (PWelsDecoderContext pCtx, PNalUnit pNalCur, uint32_t& uiEosFlag) {
1862 iMbResProperty = i ? CHROMA_AC_V : CHROMA_AC_U;1862 PDqLayer pCurLayer = pCtx->pCurDqLayer;
1863 else1863 PBitStringAux pBs = pCurLayer->pBitStringAux;
1864 iMbResProperty = i ? CHROMA_AC_V_INTER : CHROMA_AC_U_INTER;1864 PSliceHeaderExt pSliceHeaderExt = &pCurLayer->sLayerInfo.sSliceInLayer.sSliceHeaderExt;
1865 1865 int32_t iBaseModeFlag;
1866 int32_t iIndex = 16 + (i << 2);1866 int32_t iRet = 0; //should have the return value to indicate decoding error or not, It's NECESSARY--2010.4.15
1867 for (iId4x4 = 0; iId4x4 < 4; iId4x4++) {1867 uint32_t uiCode;
1868 if ((iRet = WelsResidualBlockCavlc (pVlcTable, pNonZeroCount, pBs, iIndex, iScanIdxEnd - WELS_MAX (iScanIdxStart,1868 intX_t iUsedBits;
1869 1) + 1, g_kuiZigzagScan + WELS_MAX (iScanIdxStart, 1), iMbResProperty, pCurLayer->pScaledTCoeff[iMbXy] + (iIndex << 4),1869 if (pSliceHeaderExt->bAdaptiveBaseModeFlag == 1) {
1870 pCurLayer->pChromaQp[iMbXy][i], pCtx)) != ERR_NONE) {1870 WELS_READ_VERIFY (BsGetOneBit (pBs, &uiCode)); //base_mode_flag
1871 return iRet;//abnormal1871 iBaseModeFlag = uiCode;
1872 }1872 } else {
1873 iIndex++;1873 iBaseModeFlag = pSliceHeaderExt->bDefaultBaseModeFlag;
1874 }1874 }
1875 }1875 if (!iBaseModeFlag) {
1876 ST16A2 (&pNzc[16], LD16A2 (&pNonZeroCount[6 + 8 * 1]));1876 iRet = WelsActualDecodeMbCavlcISlice (pCtx);
1877 ST16A2 (&pNzc[20], LD16A2 (&pNonZeroCount[6 + 8 * 2]));1877 } else {
1878 ST16A2 (&pNzc[18], LD16A2 (&pNonZeroCount[6 + 8 * 4]));1878 WelsLog (& (pCtx->sLogCtx), WELS_LOG_WARNING, "iBaseModeFlag (%d) != 0, inter-layer prediction not supported.",
1879 ST16A2 (&pNzc[22], LD16A2 (&pNonZeroCount[6 + 8 * 5]));1879 iBaseModeFlag);
1880 }1880 return GENERATE_ERROR_NO (ERR_LEVEL_SLICE_HEADER, ERR_INFO_UNSUPPORTED_ILP);
1881 BsEndCavlc (pBs);1881 }
1882 }1882 if (iRet) { //occur error when parsing, MUST STOP decoding
1883 1883 return iRet;
1884 return ERR_NONE;1884 }
1885}1885
1886 1886 // check whether there is left bits to read next time in case multiple slices
1887int32_t WelsDecodeMbCavlcPSlice (PWelsDecoderContext pCtx, PNalUnit pNalCur, uint32_t& uiEosFlag) {1887 iUsedBits = ((pBs->pCurBuf - pBs->pStartBuf) << 3) - (16 - pBs->iLeftBits);
1888 PDqLayer pCurLayer = pCtx->pCurDqLayer;1888 // sub 1, for stop bit
1889 PBitStringAux pBs = pCurLayer->pBitStringAux;1889 if ((iUsedBits == (pBs->iBits - 1)) && (0 >= pCurLayer->sLayerInfo.sSliceInLayer.iMbSkipRun)) { // slice boundary
1890 PSlice pSlice = &pCurLayer->sLayerInfo.sSliceInLayer;1890 uiEosFlag = 1;
1891 PSliceHeader pSliceHeader = &pSlice->sSliceHeaderExt.sSliceHeader;1891 }
1892 PPicture* ppRefPic = pCtx->sRefPic.pRefList[LIST_0];1892 if (iUsedBits > (pBs->iBits -
1893 intX_t iUsedBits;1893 1)) { //When BS incomplete, as long as find it, SHOULD stop decoding to avoid mosaic or crash.
1894 const int32_t iMbXy = pCurLayer->iMbXyIndex;1894 WelsLog (& (pCtx->sLogCtx), WELS_LOG_WARNING,
1895 int8_t* pNzc = pCurLayer->pNzc[iMbXy];1895 "WelsDecodeMbCavlcISlice()::::pBs incomplete, iUsedBits:%" PRId64 " > pBs->iBits:%d, MUST stop decoding.",
1896 int32_t iBaseModeFlag, i;1896 (int64_t) iUsedBits, pBs->iBits);
1897 int32_t iRet = 0; //should have the return value to indicate decoding error or not, It's NECESSARY--2010.4.151897 return GENERATE_ERROR_NO (ERR_LEVEL_MB_DATA, ERR_INFO_BS_INCOMPLETE);
1898 uint32_t uiCode;1898 }
1899 1899 return ERR_NONE;
1900 pCurLayer->pNoSubMbPartSizeLessThan8x8Flag[iMbXy] = true;1900}
1901 pCurLayer->pTransformSize8x8Flag[iMbXy] = false;1901
1902 1902int32_t WelsActualDecodeMbCavlcPSlice (PWelsDecoderContext pCtx) {
1903 if (-1 == pSlice->iMbSkipRun) {1903 SVlcTable* pVlcTable = &pCtx->sVlcTable;
1904 WELS_READ_VERIFY (BsGetUe (pBs, &uiCode)); //mb_skip_run1904 PDqLayer pCurLayer = pCtx->pCurDqLayer;
1905 pSlice->iMbSkipRun = uiCode;1905 PBitStringAux pBs = pCurLayer->pBitStringAux;
1906 if (-1 == pSlice->iMbSkipRun) {1906 PSlice pSlice = &pCurLayer->sLayerInfo.sSliceInLayer;
1907 return GENERATE_ERROR_NO (ERR_LEVEL_MB_DATA, ERR_INFO_INVALID_MB_SKIP_RUN);1907 PSliceHeader pSliceHeader = &pSlice->sSliceHeaderExt.sSliceHeader;
1908 }1908
1909 }1909 int32_t iScanIdxStart = pSlice->sSliceHeaderExt.uiScanIdxStart;
1910 if (pSlice->iMbSkipRun--) {1910 int32_t iScanIdxEnd = pSlice->sSliceHeaderExt.uiScanIdxEnd;
1911 int16_t iMv[2];1911
1912 1912 SWelsNeighAvail sNeighAvail;
1913 pCurLayer->pMbType[iMbXy] = MB_TYPE_SKIP;1913 int32_t iMbX = pCurLayer->iMbX;
1914 ST32A4 (&pNzc[0], 0);1914 int32_t iMbY = pCurLayer->iMbY;
1915 ST32A4 (&pNzc[4], 0);1915 const int32_t iMbXy = pCurLayer->iMbXyIndex;
1916 ST32A4 (&pNzc[8], 0);1916 int8_t* pNzc = pCurLayer->pNzc[iMbXy];
1917 ST32A4 (&pNzc[12], 0);1917 int32_t i;
1918 ST32A4 (&pNzc[16], 0);1918 int32_t iRet = ERR_NONE;
1919 ST32A4 (&pNzc[20], 0);1919 uint32_t uiMbType = 0, uiCbp = 0, uiCbpL = 0, uiCbpC = 0;
1920 1920 uint32_t uiCode;
1921 pCurLayer->pInterPredictionDoneFlag[iMbXy] = 0;1921 int32_t iCode;
1922 memset (pCurLayer->pRefIndex[0][iMbXy], 0, sizeof (int8_t) * 16);1922 int32_t iMbResProperty;
1923 pCtx->bMbRefConcealed = pCtx->bRPLRError || pCtx->bMbRefConcealed || ! (ppRefPic[0] && ppRefPic[0]->bIsComplete);1923
1924 //predict iMv1924 GetNeighborAvailMbType (&sNeighAvail, pCurLayer);
1925 PredPSkipMvFromNeighbor (pCurLayer, iMv);1925 ENFORCE_STACK_ALIGN_1D (uint8_t, pNonZeroCount, 48, 16);
1926 for (i = 0; i < 16; i++) {1926 pCurLayer->pInterPredictionDoneFlag[iMbXy] = 0;//2009.10.23
1927 ST32A2 (pCurLayer->pMv[0][iMbXy][i], * (uint32_t*)iMv);1927 WELS_READ_VERIFY (BsGetUe (pBs, &uiCode)); //uiMbType
1928 }1928 uiMbType = uiCode;
1929 1929 if (uiMbType < 5) { //inter MB type
1930 //if (!pSlice->sSliceHeaderExt.bDefaultResidualPredFlag) {1930 int16_t iMotionVector[LIST_A][30][MV_A];
1931 // memset (pCurLayer->pScaledTCoeff[iMbXy], 0, 384 * sizeof (int16_t));1931 int8_t iRefIndex[LIST_A][30];
1932 //}1932 pCurLayer->pMbType[iMbXy] = g_ksInterPMbTypeInfo[uiMbType].iType;
1933 1933 WelsFillCacheInter (&sNeighAvail, pNonZeroCount, iMotionVector, iRefIndex, pCurLayer);
1934 //reset rS1934
1935 if (!pSlice->sSliceHeaderExt.bDefaultResidualPredFlag ||1935 if ((iRet = ParseInterInfo (pCtx, iMotionVector, iRefIndex, pBs)) != ERR_NONE) {
1936 (pNalCur->sNalHeaderExt.uiQualityId == 0 && pNalCur->sNalHeaderExt.uiDependencyId == 0)) {1936 return iRet;//abnormal
1937 pCurLayer->pLumaQp[iMbXy] = pSlice->iLastMbQp;1937 }
1938 for (i = 0; i < 2; i++) {1938
1939 pCurLayer->pChromaQp[iMbXy][i] = g_kuiChromaQpTable[WELS_CLIP3 (pCurLayer->pLumaQp[iMbXy] +1939 if (pSlice->sSliceHeaderExt.bAdaptiveResidualPredFlag == 1) {
1940 pSliceHeader->pPps->iChromaQpIndexOffset[i], 0, 51)];1940 WELS_READ_VERIFY (BsGetOneBit (pBs, &uiCode)); //residual_prediction_flag
1941 }1941 pCurLayer->pResidualPredFlag[iMbXy] = uiCode;
1942 }1942 } else {
1943 1943 pCurLayer->pResidualPredFlag[iMbXy] = pSlice->sSliceHeaderExt.bDefaultResidualPredFlag;
1944 pCurLayer->pCbp[iMbXy] = 0;1944 }
1945 } else {1945
1946 if (pSlice->sSliceHeaderExt.bAdaptiveBaseModeFlag == 1) {1946 if (pCurLayer->pResidualPredFlag[iMbXy] == 0) {
1947 WELS_READ_VERIFY (BsGetOneBit (pBs, &uiCode)); //base_mode_flag1947 pCurLayer->pInterPredictionDoneFlag[iMbXy] = 0;
1948 iBaseModeFlag = uiCode;1948 } else {
1949 } else {1949 WelsLog (& (pCtx->sLogCtx), WELS_LOG_WARNING, "residual_pred_flag = 1 not supported.");
1950 iBaseModeFlag = pSlice->sSliceHeaderExt.bDefaultBaseModeFlag;1950 return GENERATE_ERROR_NO (ERR_LEVEL_MB_DATA, ERR_INFO_UNSUPPORTED_ILP);
1951 }1951 }
1952 if (!iBaseModeFlag) {1952 } else { //intra MB type
1953 iRet = WelsActualDecodeMbCavlcPSlice (pCtx);1953 uiMbType -= 5;
1954 } else {1954 if (uiMbType > 25)
1955 WelsLog (& (pCtx->sLogCtx), WELS_LOG_WARNING, "iBaseModeFlag (%d) != 0, inter-layer prediction not supported.",1955 return GENERATE_ERROR_NO (ERR_LEVEL_MB_DATA, ERR_INFO_INVALID_MB_TYPE);
1956 iBaseModeFlag);1956 if (!pCtx->pSps->uiChromaFormatIdc && ((uiMbType >= 5 && uiMbType <= 12) || (uiMbType >= 17 && uiMbType <= 24)))
1957 return GENERATE_ERROR_NO (ERR_LEVEL_SLICE_HEADER, ERR_INFO_UNSUPPORTED_ILP);1957 return GENERATE_ERROR_NO (ERR_LEVEL_MB_DATA, ERR_INFO_INVALID_MB_TYPE);
1958 }1958
1959 if (iRet) { //occur error when parsing, MUST STOP decoding1959 if (25 == uiMbType) {
1960 return iRet;1960 WelsLog (& (pCtx->sLogCtx), WELS_LOG_DEBUG, "I_PCM mode exists in P slice!");
1961 }1961 int32_t iDecStrideL = pCurLayer->pDec->iLinesize[0];
1962 }1962 int32_t iDecStrideC = pCurLayer->pDec->iLinesize[1];
1963 // check whether there is left bits to read next time in case multiple slices1963
1964 iUsedBits = ((pBs->pCurBuf - pBs->pStartBuf) << 3) - (16 - pBs->iLeftBits);1964 int32_t iOffsetL = (iMbX + iMbY * iDecStrideL) << 4;
1965 // sub 1, for stop bit1965 int32_t iOffsetC = (iMbX + iMbY * iDecStrideC) << 3;
1966 if ((iUsedBits == (pBs->iBits - 1)) && (0 >= pCurLayer->sLayerInfo.sSliceInLayer.iMbSkipRun)) { // slice boundary1966
1967 uiEosFlag = 1;1967 uint8_t* pDecY = pCurLayer->pDec->pData[0] + iOffsetL;
1968 }1968 uint8_t* pDecU = pCurLayer->pDec->pData[1] + iOffsetC;
1969 if (iUsedBits > (pBs->iBits -1969 uint8_t* pDecV = pCurLayer->pDec->pData[2] + iOffsetC;
1970 1)) { //When BS incomplete, as long as find it, SHOULD stop decoding to avoid mosaic or crash.1970
1971 WelsLog (& (pCtx->sLogCtx), WELS_LOG_WARNING,1971 uint8_t* pTmpBsBuf;
1972 "WelsDecodeMbCavlcISlice()::::pBs incomplete, iUsedBits:%" PRId64 " > pBs->iBits:%d, MUST stop decoding.",1972
1973 (int64_t) iUsedBits, pBs->iBits);1973 int32_t i;
1974 return GENERATE_ERROR_NO (ERR_LEVEL_MB_DATA, ERR_INFO_BS_INCOMPLETE);1974 int32_t iCopySizeY = (sizeof (uint8_t) << 4);
1975 }1975 int32_t iCopySizeUV = (sizeof (uint8_t) << 3);
1976 return ERR_NONE;1976
1977}1977 int32_t iIndex = ((-pBs->iLeftBits) >> 3) + 2;
1978 1978
1979void WelsBlockFuncInit (SBlockFunc* pFunc, int32_t iCpu) {1979 pCurLayer->pMbType[iMbXy] = MB_TYPE_INTRA_PCM;
1980 pFunc->pWelsSetNonZeroCountFunc = WelsNonZeroCount_c;1980
1981 pFunc->pWelsBlockZero16x16Func = WelsBlockZero16x16_c;1981 //step 1: locating bit-stream pointer [must align into integer byte]
1982 pFunc->pWelsBlockZero8x8Func = WelsBlockZero8x8_c;1982 pBs->pCurBuf -= iIndex;
1983 1983
1984#ifdef HAVE_NEON1984 //step 2: copy pixel from bit-stream into fdec [reconstruction]
1985 if (iCpu & WELS_CPU_NEON) {1985 pTmpBsBuf = pBs->pCurBuf;
1986 pFunc->pWelsSetNonZeroCountFunc = WelsNonZeroCount_neon;1986 if (!pCtx->pParam->bParseOnly) {
1987 pFunc->pWelsBlockZero16x16Func = WelsBlockZero16x16_neon;1987 for (i = 0; i < 16; i++) { //luma
1988 pFunc->pWelsBlockZero8x8Func = WelsBlockZero8x8_neon;1988 memcpy (pDecY, pTmpBsBuf, iCopySizeY);
1989 }1989 pDecY += iDecStrideL;
1990#endif1990 pTmpBsBuf += 16;
1991 1991 }
1992#ifdef HAVE_NEON_AARCH641992
1993 if (iCpu & WELS_CPU_NEON) {1993 for (i = 0; i < 8; i++) { //cb
1994 pFunc->pWelsSetNonZeroCountFunc = WelsNonZeroCount_AArch64_neon;1994 memcpy (pDecU, pTmpBsBuf, iCopySizeUV);
1995 pFunc->pWelsBlockZero16x16Func = WelsBlockZero16x16_AArch64_neon;1995 pDecU += iDecStrideC;
1996 pFunc->pWelsBlockZero8x8Func = WelsBlockZero8x8_AArch64_neon;1996 pTmpBsBuf += 8;
1997 }1997 }
1998#endif1998 for (i = 0; i < 8; i++) { //cr
1999 1999 memcpy (pDecV, pTmpBsBuf, iCopySizeUV);
2000#if defined(X86_ASM)2000 pDecV += iDecStrideC;
2001 if (iCpu & WELS_CPU_SSE2) {2001 pTmpBsBuf += 8;
2002 pFunc->pWelsSetNonZeroCountFunc = WelsNonZeroCount_sse2;2002 }
2003 pFunc->pWelsBlockZero16x16Func = WelsBlockZero16x16_sse2;2003 }
2004 pFunc->pWelsBlockZero8x8Func = WelsBlockZero8x8_sse2;2004
2005 }2005 pBs->pCurBuf += 384;
2006#endif2006
2007 2007 //step 3: update QP and pNonZeroCount
2008}2008 pCurLayer->pLumaQp[iMbXy] = 0;
2009 2009 pCurLayer->pChromaQp[iMbXy][0] = pCurLayer->pChromaQp[iMbXy][1] = 0;
2010void WelsBlockInit (int16_t* pBlock, int iW, int iH, int iStride, uint8_t uiVal) {2010 //Rec. 9.2.1 for PCM, nzc=16
2011 int32_t i;2011 ST32A4 (&pNzc[0], 0x10101010);
2012 int16_t* pDst = pBlock;2012 ST32A4 (&pNzc[4], 0x10101010);
2013 2013 ST32A4 (&pNzc[8], 0x10101010);
2014 for (i = 0; i < iH; i++) {2014 ST32A4 (&pNzc[12], 0x10101010);
2015 memset (pDst, uiVal, iW * sizeof (int16_t));2015 ST32A4 (&pNzc[16], 0x10101010);
2016 pDst += iStride;2016 ST32A4 (&pNzc[20], 0x10101010);
2017 }2017 WELS_READ_VERIFY (InitReadBits (pBs, 0));
2018}2018 return ERR_NONE;
2019void WelsBlockZero16x16_c (int16_t* pBlock, int32_t iStride) {2019 } else {
2020 WelsBlockInit (pBlock, 16, 16, iStride, 0);2020 if (0 == uiMbType) {
2021}2021 ENFORCE_STACK_ALIGN_1D (int8_t, pIntraPredMode, 48, 16);
2022 2022 pCurLayer->pMbType[iMbXy] = MB_TYPE_INTRA4x4;
2023void WelsBlockZero8x8_c (int16_t* pBlock, int32_t iStride) {2023 if (pCtx->pPps->bTransform8x8ModeFlag) {
2024 WelsBlockInit (pBlock, 8, 8, iStride, 0);2024 WELS_READ_VERIFY (BsGetOneBit (pBs, &uiCode)); //transform_size_8x8_flag
2025}2025 pCurLayer->pTransformSize8x8Flag[iMbXy] = !!uiCode;
2026 2026 if (pCurLayer->pTransformSize8x8Flag[iMbXy]) {
2027} // namespace WelsDec2027 uiMbType = pCurLayer->pMbType[iMbXy] = MB_TYPE_INTRA8x8;
2028 }
2029 }
2030 if (!pCurLayer->pTransformSize8x8Flag[iMbXy]) {
2031 pCtx->pFillInfoCacheIntraNxNFunc (&sNeighAvail, pNonZeroCount, pIntraPredMode, pCurLayer);
2032 WELS_READ_VERIFY (ParseIntra4x4Mode (pCtx, &sNeighAvail, pIntraPredMode, pBs, pCurLayer));
2033 } else {
2034 pCtx->pFillInfoCacheIntraNxNFunc (&sNeighAvail, pNonZeroCount, pIntraPredMode, pCurLayer);
2035 WELS_READ_VERIFY (ParseIntra8x8Mode (pCtx, &sNeighAvail, pIntraPredMode, pBs, pCurLayer));
2036 }
2037 } else { //I_PCM exclude, we can ignore it
2038 pCurLayer->pMbType[iMbXy] = MB_TYPE_INTRA16x16;
2039 pCurLayer->pTransformSize8x8Flag[iMbXy] = false;
2040 pCurLayer->pNoSubMbPartSizeLessThan8x8Flag[iMbXy] = true;
2041 pCurLayer->pIntraPredMode[iMbXy][7] = (uiMbType - 1) & 3;
2042 pCurLayer->pCbp[iMbXy] = g_kuiI16CbpTable[ (uiMbType - 1) >> 2];
2043 uiCbpC = pCtx->pSps->uiChromaFormatIdc ? pCurLayer->pCbp[iMbXy] >> 4 : 0;
2044 uiCbpL = pCurLayer->pCbp[iMbXy] & 15;
2045 WelsFillCacheNonZeroCount (&sNeighAvail, pNonZeroCount, pCurLayer);
2046 if ((iRet = ParseIntra16x16Mode (pCtx, &sNeighAvail, pBs, pCurLayer)) != ERR_NONE) {
2047 return iRet;
2048 }
2049 }
2050 }
2051 }
2052
2053 if (MB_TYPE_INTRA16x16 != pCurLayer->pMbType[iMbXy]) {
2054 WELS_READ_VERIFY (BsGetUe (pBs, &uiCode)); //coded_block_pattern
2055 uiCbp = uiCode;
2056 {
2057 if (pCtx->pSps->uiChromaFormatIdc && (uiCbp > 47))
2058 return GENERATE_ERROR_NO (ERR_LEVEL_MB_DATA, ERR_INFO_INVALID_CBP);
2059 if (!pCtx->pSps->uiChromaFormatIdc && (uiCbp > 15))
2060 return GENERATE_ERROR_NO (ERR_LEVEL_MB_DATA, ERR_INFO_INVALID_CBP);
2061 if (MB_TYPE_INTRA4x4 == pCurLayer->pMbType[iMbXy] || MB_TYPE_INTRA8x8 == pCurLayer->pMbType[iMbXy]) {
2062
2063 uiCbp = pCtx->pSps->uiChromaFormatIdc ? g_kuiIntra4x4CbpTable[uiCbp] : g_kuiIntra4x4CbpTable400[uiCbp];
2064 } else //inter
2065 uiCbp = pCtx->pSps->uiChromaFormatIdc ? g_kuiInterCbpTable[uiCbp] : g_kuiInterCbpTable400[uiCbp];
2066 }
2067
2068 pCurLayer->pCbp[iMbXy] = uiCbp;
2069 uiCbpC = pCurLayer->pCbp[iMbXy] >> 4;
2070 uiCbpL = pCurLayer->pCbp[iMbXy] & 15;
2071
2072 // Need modification when B picutre add in
2073 bool bNeedParseTransformSize8x8Flag =
2074 (((pCurLayer->pMbType[iMbXy] >= MB_TYPE_16x16 && pCurLayer->pMbType[iMbXy] <= MB_TYPE_8x16)
2075 || pCurLayer->pNoSubMbPartSizeLessThan8x8Flag[iMbXy])
2076 && (pCurLayer->pMbType[iMbXy] != MB_TYPE_INTRA8x8)
2077 && (pCurLayer->pMbType[iMbXy] != MB_TYPE_INTRA4x4)
2078 && (uiCbpL > 0)
2079 && (pCtx->pPps->bTransform8x8ModeFlag));
2080
2081 if (bNeedParseTransformSize8x8Flag) {
2082 WELS_READ_VERIFY (BsGetOneBit (pBs, &uiCode)); //transform_size_8x8_flag
2083 pCurLayer->pTransformSize8x8Flag[iMbXy] = !!uiCode;
2084 }
2085 }
2086
2087 ST32A4 (&pNzc[0], 0);
2088 ST32A4 (&pNzc[4], 0);
2089 ST32A4 (&pNzc[8], 0);
2090 ST32A4 (&pNzc[12], 0);
2091 ST32A4 (&pNzc[16], 0);
2092 ST32A4 (&pNzc[20], 0);
2093 if (pCurLayer->pCbp[iMbXy] == 0 && !IS_INTRA16x16 (pCurLayer->pMbType[iMbXy]) && !IS_I_BL (pCurLayer->pMbType[iMbXy])) {
2094 pCurLayer->pLumaQp[iMbXy] = pSlice->iLastMbQp;
2095 for (i = 0; i < 2; i++) {
2096 pCurLayer->pChromaQp[iMbXy][i] = g_kuiChromaQpTable[WELS_CLIP3 (pCurLayer->pLumaQp[iMbXy] +
2097 pSliceHeader->pPps->iChromaQpIndexOffset[i], 0, 51)];
2098 }
2099 }
2100
2101 if (pCurLayer->pCbp[iMbXy] || MB_TYPE_INTRA16x16 == pCurLayer->pMbType[iMbXy]) {
2102 int32_t iQpDelta, iId8x8, iId4x4;
2103 memset (pCurLayer->pScaledTCoeff[iMbXy], 0, MB_COEFF_LIST_SIZE * sizeof (int16_t));
2104 WELS_READ_VERIFY (BsGetSe (pBs, &iCode)); //mb_qp_delta
2105 iQpDelta = iCode;
2106
2107 if (iQpDelta > 25 || iQpDelta < -26) { //out of iQpDelta range
2108 return GENERATE_ERROR_NO (ERR_LEVEL_MB_DATA, ERR_INFO_INVALID_QP);
2109 }
2110
2111 pCurLayer->pLumaQp[iMbXy] = (pSlice->iLastMbQp + iQpDelta + 52) % 52; //update last_mb_qp
2112 pSlice->iLastMbQp = pCurLayer->pLumaQp[iMbXy];
2113 for (i = 0; i < 2; i++) {
2114 pCurLayer->pChromaQp[iMbXy][i] = g_kuiChromaQpTable[WELS_CLIP3 (pSlice->iLastMbQp +
2115 pSliceHeader->pPps->iChromaQpIndexOffset[i], 0,
2116 51)];
2117 }
2118
2119 BsStartCavlc (pBs);
2120
2121 if (MB_TYPE_INTRA16x16 == pCurLayer->pMbType[iMbXy]) {
2122 //step1: Luma DC
2123 if ((iRet = WelsResidualBlockCavlc (pVlcTable, pNonZeroCount, pBs, 0, 16, g_kuiLumaDcZigzagScan, I16_LUMA_DC,
2124 pCurLayer->pScaledTCoeff[iMbXy], pCurLayer->pLumaQp[iMbXy], pCtx)) != ERR_NONE) {
2125 return iRet;//abnormal
2126 }
2127 //step2: Luma AC
2128 if (uiCbpL) {
2129 for (i = 0; i < 16; i++) {
2130 if ((iRet = WelsResidualBlockCavlc (pVlcTable, pNonZeroCount, pBs, i, iScanIdxEnd - WELS_MAX (iScanIdxStart, 1) + 1,
2131 g_kuiZigzagScan + WELS_MAX (iScanIdxStart, 1), I16_LUMA_AC, pCurLayer->pScaledTCoeff[iMbXy] + (i << 4),
2132 pCurLayer->pLumaQp[iMbXy], pCtx)) != ERR_NONE) {
2133 return iRet;//abnormal
2134 }
2135 }
2136 ST32A4 (&pNzc[0], LD32 (&pNonZeroCount[1 + 8 * 1]));
2137 ST32A4 (&pNzc[4], LD32 (&pNonZeroCount[1 + 8 * 2]));
2138 ST32A4 (&pNzc[8], LD32 (&pNonZeroCount[1 + 8 * 3]));
2139 ST32A4 (&pNzc[12], LD32 (&pNonZeroCount[1 + 8 * 4]));
2140 }
2141 } else { //non-MB_TYPE_INTRA16x16
2142 if (pCurLayer->pTransformSize8x8Flag[iMbXy]) {
2143 for (iId8x8 = 0; iId8x8 < 4; iId8x8++) {
2144 iMbResProperty = (IS_INTRA (pCurLayer->pMbType[iMbXy])) ? LUMA_DC_AC_INTRA_8 : LUMA_DC_AC_INTER_8;
2145 if (uiCbpL & (1 << iId8x8)) {
2146 int32_t iIndex = (iId8x8 << 2);
2147 for (iId4x4 = 0; iId4x4 < 4; iId4x4++) {
2148 if ((iRet = WelsResidualBlockCavlc8x8 (pVlcTable, pNonZeroCount, pBs, iIndex, iScanIdxEnd - iScanIdxStart + 1,
2149 g_kuiZigzagScan8x8 + iScanIdxStart, iMbResProperty, pCurLayer->pScaledTCoeff[iMbXy] + (iId8x8 << 6), iId4x4,
2150 pCurLayer->pLumaQp[iMbXy], pCtx)) != ERR_NONE) {
2151 return iRet;
2152 }
2153 iIndex++;
2154 }
2155 } else {
2156 ST16 (&pNonZeroCount[g_kuiCache48CountScan4Idx[iId8x8 << 2]], 0);
2157 ST16 (&pNonZeroCount[g_kuiCache48CountScan4Idx[ (iId8x8 << 2) + 2]], 0);
2158 }
2159 }
2160 ST32A4 (&pNzc[0], LD32 (&pNonZeroCount[1 + 8 * 1]));
2161 ST32A4 (&pNzc[4], LD32 (&pNonZeroCount[1 + 8 * 2]));
2162 ST32A4 (&pNzc[8], LD32 (&pNonZeroCount[1 + 8 * 3]));
2163 ST32A4 (&pNzc[12], LD32 (&pNonZeroCount[1 + 8 * 4]));
2164 } else { // Normal T4x4
2165 for (iId8x8 = 0; iId8x8 < 4; iId8x8++) {
2166 iMbResProperty = (IS_INTRA (pCurLayer->pMbType[iMbXy])) ? LUMA_DC_AC_INTRA : LUMA_DC_AC_INTER;
2167 if (uiCbpL & (1 << iId8x8)) {
2168 int32_t iIndex = (iId8x8 << 2);
2169 for (iId4x4 = 0; iId4x4 < 4; iId4x4++) {
2170 //Luma (DC and AC decoding together)
2171 if ((iRet = WelsResidualBlockCavlc (pVlcTable, pNonZeroCount, pBs, iIndex, iScanIdxEnd - iScanIdxStart + 1,
2172 g_kuiZigzagScan + iScanIdxStart, iMbResProperty, pCurLayer->pScaledTCoeff[iMbXy] + (iIndex << 4),
2173 pCurLayer->pLumaQp[iMbXy], pCtx)) != ERR_NONE) {
2174 return iRet;//abnormal
2175 }
2176 iIndex++;
2177 }
2178 } else {
2179 ST16 (&pNonZeroCount[g_kuiCache48CountScan4Idx[iId8x8 << 2]], 0);
2180 ST16 (&pNonZeroCount[g_kuiCache48CountScan4Idx[ (iId8x8 << 2) + 2]], 0);
2181 }
2182 }
2183 ST32A4 (&pNzc[0], LD32 (&pNonZeroCount[1 + 8 * 1]));
2184 ST32A4 (&pNzc[4], LD32 (&pNonZeroCount[1 + 8 * 2]));
2185 ST32A4 (&pNzc[8], LD32 (&pNonZeroCount[1 + 8 * 3]));
2186 ST32A4 (&pNzc[12], LD32 (&pNonZeroCount[1 + 8 * 4]));
2187 }
2188 }
2189
2190
2191 //chroma
2192 //step1: DC
2193 if (1 == uiCbpC || 2 == uiCbpC) {
2194 for (i = 0; i < 2; i++) { //Cb Cr
2195 if (IS_INTRA (pCurLayer->pMbType[iMbXy]))
2196 iMbResProperty = i ? CHROMA_DC_V : CHROMA_DC_U;
2197 else
2198 iMbResProperty = i ? CHROMA_DC_V_INTER : CHROMA_DC_U_INTER;
2199
2200 if ((iRet = WelsResidualBlockCavlc (pVlcTable, pNonZeroCount, pBs, 16 + (i << 2), 4, g_kuiChromaDcScan, iMbResProperty,
2201 pCurLayer->pScaledTCoeff[iMbXy] + 256 + (i << 6), pCurLayer->pChromaQp[iMbXy][i], pCtx)) != ERR_NONE) {
2202 return iRet;//abnormal
2203 }
2204 }
2205 } else {
2206 }
2207 //step2: AC
2208 if (2 == uiCbpC) {
2209 for (i = 0; i < 2; i++) { //Cb Cr
2210 if (IS_INTRA (pCurLayer->pMbType[iMbXy]))
2211 iMbResProperty = i ? CHROMA_AC_V : CHROMA_AC_U;
2212 else
2213 iMbResProperty = i ? CHROMA_AC_V_INTER : CHROMA_AC_U_INTER;
2214
2215 int32_t iIndex = 16 + (i << 2);
2216 for (iId4x4 = 0; iId4x4 < 4; iId4x4++) {
2217 if ((iRet = WelsResidualBlockCavlc (pVlcTable, pNonZeroCount, pBs, iIndex, iScanIdxEnd - WELS_MAX (iScanIdxStart,
2218 1) + 1, g_kuiZigzagScan + WELS_MAX (iScanIdxStart, 1), iMbResProperty, pCurLayer->pScaledTCoeff[iMbXy] + (iIndex << 4),
2219 pCurLayer->pChromaQp[iMbXy][i], pCtx)) != ERR_NONE) {
2220 return iRet;//abnormal
2221 }
2222 iIndex++;
2223 }
2224 }
2225 ST16A2 (&pNzc[16], LD16A2 (&pNonZeroCount[6 + 8 * 1]));
2226 ST16A2 (&pNzc[20], LD16A2 (&pNonZeroCount[6 + 8 * 2]));
2227 ST16A2 (&pNzc[18], LD16A2 (&pNonZeroCount[6 + 8 * 4]));
2228 ST16A2 (&pNzc[22], LD16A2 (&pNonZeroCount[6 + 8 * 5]));
2229 }
2230 BsEndCavlc (pBs);
2231 }
2232
2233 return ERR_NONE;
2234}
2235
2236int32_t WelsDecodeMbCavlcPSlice (PWelsDecoderContext pCtx, PNalUnit pNalCur, uint32_t& uiEosFlag) {
2237 PDqLayer pCurLayer = pCtx->pCurDqLayer;
2238 PBitStringAux pBs = pCurLayer->pBitStringAux;
2239 PSlice pSlice = &pCurLayer->sLayerInfo.sSliceInLayer;
2240 PSliceHeader pSliceHeader = &pSlice->sSliceHeaderExt.sSliceHeader;
2241 PPicture* ppRefPic = pCtx->sRefPic.pRefList[LIST_0];
2242 intX_t iUsedBits;
2243 const int32_t iMbXy = pCurLayer->iMbXyIndex;
2244 int8_t* pNzc = pCurLayer->pNzc[iMbXy];
2245 int32_t iBaseModeFlag, i;
2246 int32_t iRet = 0; //should have the return value to indicate decoding error or not, It's NECESSARY--2010.4.15
2247 uint32_t uiCode;
2248
2249 pCurLayer->pNoSubMbPartSizeLessThan8x8Flag[iMbXy] = true;
2250 pCurLayer->pTransformSize8x8Flag[iMbXy] = false;
2251
2252 if (-1 == pSlice->iMbSkipRun) {
2253 WELS_READ_VERIFY (BsGetUe (pBs, &uiCode)); //mb_skip_run
2254 pSlice->iMbSkipRun = uiCode;
2255 if (-1 == pSlice->iMbSkipRun) {
2256 return GENERATE_ERROR_NO (ERR_LEVEL_MB_DATA, ERR_INFO_INVALID_MB_SKIP_RUN);
2257 }
2258 }
2259 if (pSlice->iMbSkipRun--) {
2260 int16_t iMv[2];
2261
2262 pCurLayer->pMbType[iMbXy] = MB_TYPE_SKIP;
2263 ST32A4 (&pNzc[0], 0);
2264 ST32A4 (&pNzc[4], 0);
2265 ST32A4 (&pNzc[8], 0);
2266 ST32A4 (&pNzc[12], 0);
2267 ST32A4 (&pNzc[16], 0);
2268 ST32A4 (&pNzc[20], 0);
2269
2270 pCurLayer->pInterPredictionDoneFlag[iMbXy] = 0;
2271 memset (pCurLayer->pRefIndex[0][iMbXy], 0, sizeof (int8_t) * 16);
2272 pCtx->bMbRefConcealed = pCtx->bRPLRError || pCtx->bMbRefConcealed || ! (ppRefPic[0] && ppRefPic[0]->bIsComplete);
2273 //predict iMv
2274 PredPSkipMvFromNeighbor (pCurLayer, iMv);
2275 for (i = 0; i < 16; i++) {
2276 ST32A2 (pCurLayer->pMv[0][iMbXy][i], * (uint32_t*)iMv);
2277 }
2278
2279 //if (!pSlice->sSliceHeaderExt.bDefaultResidualPredFlag) {
2280 // memset (pCurLayer->pScaledTCoeff[iMbXy], 0, 384 * sizeof (int16_t));
2281 //}
2282
2283 //reset rS
2284 if (!pSlice->sSliceHeaderExt.bDefaultResidualPredFlag ||
2285 (pNalCur->sNalHeaderExt.uiQualityId == 0 && pNalCur->sNalHeaderExt.uiDependencyId == 0)) {
2286 pCurLayer->pLumaQp[iMbXy] = pSlice->iLastMbQp;
2287 for (i = 0; i < 2; i++) {
2288 pCurLayer->pChromaQp[iMbXy][i] = g_kuiChromaQpTable[WELS_CLIP3 (pCurLayer->pLumaQp[iMbXy] +
2289 pSliceHeader->pPps->iChromaQpIndexOffset[i], 0, 51)];
2290 }
2291 }
2292
2293 pCurLayer->pCbp[iMbXy] = 0;
2294 } else {
2295 if (pSlice->sSliceHeaderExt.bAdaptiveBaseModeFlag == 1) {
2296 WELS_READ_VERIFY (BsGetOneBit (pBs, &uiCode)); //base_mode_flag
2297 iBaseModeFlag = uiCode;
2298 } else {
2299 iBaseModeFlag = pSlice->sSliceHeaderExt.bDefaultBaseModeFlag;
2300 }
2301 if (!iBaseModeFlag) {
2302 iRet = WelsActualDecodeMbCavlcPSlice (pCtx);
2303 } else {
2304 WelsLog (& (pCtx->sLogCtx), WELS_LOG_WARNING, "iBaseModeFlag (%d) != 0, inter-layer prediction not supported.",
2305 iBaseModeFlag);
2306 return GENERATE_ERROR_NO (ERR_LEVEL_SLICE_HEADER, ERR_INFO_UNSUPPORTED_ILP);
2307 }
2308 if (iRet) { //occur error when parsing, MUST STOP decoding
2309 return iRet;
2310 }
2311 }
2312 // check whether there is left bits to read next time in case multiple slices
2313 iUsedBits = ((pBs->pCurBuf - pBs->pStartBuf) << 3) - (16 - pBs->iLeftBits);
2314 // sub 1, for stop bit
2315 if ((iUsedBits == (pBs->iBits - 1)) && (0 >= pCurLayer->sLayerInfo.sSliceInLayer.iMbSkipRun)) { // slice boundary
2316 uiEosFlag = 1;
2317 }
2318 if (iUsedBits > (pBs->iBits -
2319 1)) { //When BS incomplete, as long as find it, SHOULD stop decoding to avoid mosaic or crash.
2320 WelsLog (& (pCtx->sLogCtx), WELS_LOG_WARNING,
2321 "WelsDecodeMbCavlcISlice()::::pBs incomplete, iUsedBits:%" PRId64 " > pBs->iBits:%d, MUST stop decoding.",
2322 (int64_t) iUsedBits, pBs->iBits);
2323 return GENERATE_ERROR_NO (ERR_LEVEL_MB_DATA, ERR_INFO_BS_INCOMPLETE);
2324 }
2325 return ERR_NONE;
2326}
2327
2328void WelsBlockFuncInit (SBlockFunc* pFunc, int32_t iCpu) {
2329 pFunc->pWelsSetNonZeroCountFunc = WelsNonZeroCount_c;
2330 pFunc->pWelsBlockZero16x16Func = WelsBlockZero16x16_c;
2331 pFunc->pWelsBlockZero8x8Func = WelsBlockZero8x8_c;
2332
2333#ifdef HAVE_NEON
2334 if (iCpu & WELS_CPU_NEON) {
2335 pFunc->pWelsSetNonZeroCountFunc = WelsNonZeroCount_neon;
2336 pFunc->pWelsBlockZero16x16Func = WelsBlockZero16x16_neon;
2337 pFunc->pWelsBlockZero8x8Func = WelsBlockZero8x8_neon;
2338 }
2339#endif
2340
2341#ifdef HAVE_NEON_AARCH64
2342 if (iCpu & WELS_CPU_NEON) {
2343 pFunc->pWelsSetNonZeroCountFunc = WelsNonZeroCount_AArch64_neon;
2344 pFunc->pWelsBlockZero16x16Func = WelsBlockZero16x16_AArch64_neon;
2345 pFunc->pWelsBlockZero8x8Func = WelsBlockZero8x8_AArch64_neon;
2346 }
2347#endif
2348
2349#if defined(X86_ASM)
2350 if (iCpu & WELS_CPU_SSE2) {
2351 pFunc->pWelsSetNonZeroCountFunc = WelsNonZeroCount_sse2;
2352 pFunc->pWelsBlockZero16x16Func = WelsBlockZero16x16_sse2;
2353 pFunc->pWelsBlockZero8x8Func = WelsBlockZero8x8_sse2;
2354 }
2355#endif
2356
2357}
2358
2359void WelsBlockInit (int16_t* pBlock, int iW, int iH, int iStride, uint8_t uiVal) {
2360 int32_t i;
2361 int16_t* pDst = pBlock;
2362
2363 for (i = 0; i < iH; i++) {
2364 memset (pDst, uiVal, iW * sizeof (int16_t));
2365 pDst += iStride;
2366 }
2367}
2368void WelsBlockZero16x16_c (int16_t* pBlock, int32_t iStride) {
2369 WelsBlockInit (pBlock, 16, 16, iStride, 0);
2370}
2371
2372void WelsBlockZero8x8_c (int16_t* pBlock, int32_t iStride) {
2373 WelsBlockInit (pBlock, 8, 8, iStride, 0);
2374}
2375bool ComputeColocated (PWelsDecoderContext pCtx) {
2376 PDqLayer pCurLayer = pCtx->pCurDqLayer;
2377 PSlice pCurSlice = &pCurLayer->sLayerInfo.sSliceInLayer;
2378 PSliceHeader pSliceHeader = &pCurSlice->sSliceHeaderExt.sSliceHeader;
2379 if (!pSliceHeader->iDirectSpatialMvPredFlag) {
2380 uint32_t uiShortRefCount = pCtx->sRefPic.uiShortRefCount[LIST_0];
2381 for (int32_t listIdx = LIST_0; listIdx < LIST_A; ++listIdx) {
2382 for (uint32_t i = 0; i < uiShortRefCount; ++i) {
2383 int32_t iTRb = WELS_CLIP3 (-128, 127, pSliceHeader->iPicOrderCntLsb - pCtx->sRefPic.pRefList[listIdx][i]->iFramePoc);
2384 int32_t iTRp = WELS_CLIP3 (-128, 127,
2385 pCtx->sRefPic.pRefList[LIST_1][i]->iFramePoc - pCtx->sRefPic.pRefList[LIST_0][i]->iFramePoc);
2386 if (iTRp != 0) {
2387 int32_t prescale = (16384 + iAbs (iTRp / 2)) / iTRp;
2388 pCurSlice->iMvScale[listIdx][i] = WELS_CLIP3 (-1024, 1023, (iTRb * prescale + 32) >> 6);
2389 } else {
2390 pCurSlice->iMvScale[listIdx][i] = 0x03FFF;
2391 }
2392 }
2393 }
2394 }
2395 //Implement the following
2396 //get Mv_colocated_L1
2397 //and do calculation
2398 //iMvp[LIST_0] = Mv_colocated_L1 * (POC(cur) - POC(L0))/POC(L1) - POC(L0))
2399 //iMvp[LIST_1] = Mv_colocated_L1 * (POC(cur) - POC(L1))/POC(L1) - POC(L0))
2400 return true;
2401}
2402} // 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