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