1 //*@@@+++@@@@******************************************************************
2 //
3 // Copyright � Microsoft Corp.
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are met:
8 //
9 // � Redistributions of source code must retain the above copyright notice,
10 // this list of conditions and the following disclaimer.
11 // � Redistributions in binary form must reproduce the above copyright notice,
12 // this list of conditions and the following disclaimer in the documentation
13 // and/or other materials provided with the distribution.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 // POSSIBILITY OF SUCH DAMAGE.
26 //
27 //*@@@---@@@@******************************************************************
28
29 #include "strcodec.h"
30 #include "decode.h"
31
32 #ifdef MEM_TRACE
33 #define TRACE_MALLOC 1
34 #define TRACE_NEW 0
35 #define TRACE_HEAP 0
36 #include "memtrace.h"
37 #endif
38
39 extern const int dctIndex[3][16];
40 extern const int blkOffset[16];
41 extern const int blkOffsetUV[4];
42 static Int DecodeSignificantAbsLevel (struct CAdaptiveHuffman *pAHexpt, BitIOInfo* pIO);
43
44 //#undef X86OPT_INLINE
45
46 #ifdef X86OPT_INLINE
47 #define _FORCEINLINE __forceinline
48 #else // X86OPT_INLINE
49 #define _FORCEINLINE
50 #endif // X86OPT_INLINE
51
52 //================================================================
53 // Memory access functions
54 //================================================================
_load4(void * pv)55 static U32 _FORCEINLINE _load4(void* pv)
56 {
57 #ifdef _BIG__ENDIAN_
58 return (*(U32*)pv);
59 #else // _BIG__ENDIAN_
60 #if defined(_M_IA64) || defined(_ARM_)
61 U32 v;
62 v = ((U16 *) pv)[0];
63 v |= ((U32)((U16 *) pv)[1]) << 16;
64 return _byteswap_ulong(v);
65 #else // _M_IA64
66 return _byteswap_ulong(*(U32*)pv);
67 #endif // _M_IA64
68 #endif // _BIG__ENDIAN_
69 }
70
_peekBit16(BitIOInfo * pIO,U32 cBits)71 static _FORCEINLINE U32 _peekBit16(BitIOInfo* pIO, U32 cBits)
72 {
73 PEEKBIT16(pIO, cBits);
74 // masking is not needed here because shift of unsigned int is implemented as a logical shift (SHR)!
75 }
76
77 #define LOAD16 _load4
_flushBit16(BitIOInfo * pIO,U32 cBits)78 static _FORCEINLINE U32 _flushBit16(BitIOInfo* pIO, U32 cBits)
79 {
80 FLUSHBIT16(pIO, cBits);
81 }
82
_getBit16(BitIOInfo * pIO,U32 cBits)83 static _FORCEINLINE U32 _getBit16(BitIOInfo* pIO, U32 cBits)
84 {
85 U32 uiRet = _peekBit16(pIO, cBits);
86 _flushBit16(pIO, cBits);
87
88 return uiRet;
89 }
90
91 #define SIGN_BIT(TypeOrValue) (((UInt) 1) << (8 * sizeof (TypeOrValue) - 1))
92 /***********************************************************************************************************
93 Huffman decode (input is a fully built Huffman table)
94 ***********************************************************************************************************/
getHuff(const short * pDecodeTable,BitIOInfo * pIO)95 Int getHuff(const short *pDecodeTable, BitIOInfo* pIO)
96 {
97 Int iSymbol, iSymbolHuff;
98 iSymbol = pDecodeTable[peekBit16(pIO, HUFFMAN_DECODE_ROOT_BITS)];
99
100 flushBit16(pIO, iSymbol < 0 ? HUFFMAN_DECODE_ROOT_BITS : iSymbol & ((1 << HUFFMAN_DECODE_ROOT_BITS_LOG) - 1));
101 iSymbolHuff = iSymbol >> HUFFMAN_DECODE_ROOT_BITS_LOG;
102
103 if (iSymbolHuff < 0) {
104 iSymbolHuff = iSymbol;
105 while ((iSymbolHuff = pDecodeTable[iSymbolHuff + SIGN_BIT (pDecodeTable[0]) + getBit16(pIO, 1)]) < 0);
106 }
107 return (iSymbolHuff);
108 }
109
110 #if 1
_getBool16(BitIOInfo * pIO)111 static _FORCEINLINE U32 _getBool16(BitIOInfo* pIO)
112 {
113 U32 uiRet = pIO->uiAccumulator >> 31;//_peekBit16(pIO, 1);
114 //_flushBit16(pIO, 1);
115 pIO->cBitsUsed++;
116 if (pIO->cBitsUsed < 16) {
117 pIO->uiAccumulator <<= 1;
118 }
119 else {
120 pIO->pbCurrent = MASKPTR(pIO->pbCurrent + ((pIO->cBitsUsed >> 3)/* & 2*/), pIO->iMask);
121 pIO->cBitsUsed &= 16 - 1;
122 pIO->uiAccumulator = LOAD16(pIO->pbCurrent) << pIO->cBitsUsed;
123 }
124
125 return uiRet;
126 }
127
_getSign(BitIOInfo * pIO)128 static _FORCEINLINE I32 _getSign(BitIOInfo* pIO)
129 {
130 I32 uiRet = (int) pIO->uiAccumulator >> 31;//_peekBit16(pIO, 1);
131 //_flushBit16(pIO, 1);
132 pIO->cBitsUsed++;
133 if (pIO->cBitsUsed < 16) {
134 pIO->uiAccumulator <<= 1;
135 }
136 else {
137 pIO->pbCurrent = MASKPTR(pIO->pbCurrent + ((pIO->cBitsUsed >> 3)/* & 2*/), pIO->iMask);
138 pIO->cBitsUsed &= 16 - 1;
139 pIO->uiAccumulator = LOAD16(pIO->pbCurrent) << pIO->cBitsUsed;
140 }
141
142 return uiRet;
143 }
144 #else
145 #define _getBool16(x) _getBit16((x),1)
146 #define _getSign(x) (-_getBit16((x),1))
147 #endif
148
149 /** this function returns cBits if zero is read, or a signed value if first cBits are not all zero **/
_getBit16s(BitIOInfo * pIO,U32 cBits)150 static _FORCEINLINE I32 _getBit16s(BitIOInfo* pIO, U32 cBits)
151 {
152 I32 iRet = (I32)_peekBit16(pIO, cBits + 1);
153 iRet = ((iRet >> 1) ^ (-(iRet & 1))) + (iRet & 1);
154 _flushBit16(pIO, cBits + (iRet != 0));
155 return iRet;
156 }
157
158 /*************************************************************************
159 Huffman decoding with short tables
160 *************************************************************************/
_getHuffShort(const short * pDecodeTable,BitIOInfo * pIO)161 static _FORCEINLINE Int _getHuffShort(const short *pDecodeTable, BitIOInfo* pIO)
162 {
163 Int iSymbol = pDecodeTable[_peekBit16(pIO, HUFFMAN_DECODE_ROOT_BITS)];
164 assert(iSymbol >= 0);
165 // for some strange reason, inlining flushBit doesn't work well
166 flushBit16(pIO, iSymbol & ((1 << HUFFMAN_DECODE_ROOT_BITS_LOG) - 1));
167 return (iSymbol >> HUFFMAN_DECODE_ROOT_BITS_LOG);
168 }
169 /*************************************************************************
170 Adapt + Huffman init
171 *************************************************************************/
AdaptDecFixed(CAdaptiveHuffman * pAH)172 static Int AdaptDecFixed (CAdaptiveHuffman *pAH)
173 {
174 AdaptDiscriminant (pAH);
175 return ICERR_OK;
176 }
177
178 /*************************************************************************
179 DecodeCBP
180 *************************************************************************/
DecodeCBP(CWMImageStrCodec * pSC,CCodingContext * pContext)181 static Void DecodeCBP(CWMImageStrCodec * pSC, CCodingContext *pContext)
182 {
183 BitIOInfo* pIO = pContext->m_pIOAC;
184 const COLORFORMAT cf = pSC->m_param.cfColorFormat;
185 const Int iChannel = (cf == NCOMPONENT || cf == CMYK) ? (Int) pSC->m_param.cNumChannels : 1;
186 Int iCBPCY, iCBPCU , iCBPCV;
187 Int k, iBlock, i;
188 Int iNumCBP;
189 Bool bIsChroma;
190 CAdaptiveHuffman *pAHCBP = pContext->m_pAdaptHuffCBPCY;
191 CAdaptiveHuffman *pAHCBP1 = pContext->m_pAdaptHuffCBPCY1;
192 CAdaptiveHuffman *pAHex1 = pContext->m_pAHexpt[1];
193
194 readIS_L1(pSC, pIO);
195
196 for (i = 0; i < iChannel; i++) {
197
198 iCBPCY = iCBPCU = iCBPCV = 0;
199 iNumCBP = _getHuffShort(pAHCBP1->m_hufDecTable, pIO);
200 pAHCBP1->m_iDiscriminant += pAHCBP1->m_pDelta[iNumCBP];
201
202 switch (iNumCBP) {
203 case 2:
204 iNumCBP = _getBit16(pIO, 2);
205 if (iNumCBP == 0)
206 iNumCBP = 3;
207 else if (iNumCBP == 1)
208 iNumCBP = 5;
209 else {
210 static const Int aTab[] = { 6, 9, 10, 12 };
211 iNumCBP = aTab[iNumCBP * 2 + _getBool16 (pIO) - 4];
212 }
213 break;
214 case 1:
215 iNumCBP = 1 << _getBit16(pIO, 2);
216 break;
217 case 3:
218 iNumCBP = 0xf ^ (1 << _getBit16(pIO, 2));
219 break;
220 case 4:
221 iNumCBP = 0xf;
222 }
223
224 for (iBlock = 0; iBlock < 4; iBlock++) {
225 if (iNumCBP & (1 << iBlock)) {
226 static const UInt gFLC0[] = { 0,2,1,2,2,0 };
227 static const UInt gOff0[] = { 0,4,2,8,12,1 };
228 static const UInt gOut0[] = { 0,15,3,12, 1,2,4,8, 5,6,9,10, 7,11,13,14 };
229 Int iNumBlockCBP = getHuff(pAHCBP->m_hufDecTable, pIO);
230 unsigned int val = (unsigned int) iNumBlockCBP + 1, iCode1;
231
232 pAHCBP->m_iDiscriminant += pAHCBP->m_pDelta[iNumBlockCBP];
233 iNumBlockCBP = 0;
234
235 if (val >= 6) { // chroma present
236 if (_getBool16 (pIO)) {
237 iNumBlockCBP = 0x10;
238 }
239 else if (_getBool16 (pIO)) {
240 iNumBlockCBP = 0x20;
241 }
242 else {
243 iNumBlockCBP = 0x30;
244 }
245 if (val == 9) {
246 if (_getBool16 (pIO)) {
247 // do nothing
248 }
249 else if (_getBool16 (pIO)) {
250 val = 10;
251 }
252 else {
253 val = 11;
254 }
255 }
256 val -= 6;
257 }
258 iCode1 = gOff0[val];
259 if (gFLC0[val]) {
260 iCode1 += _getBit16(pIO, gFLC0[val]);
261 }
262 iNumBlockCBP += gOut0[iCode1];
263
264 switch (cf) {
265 case YUV_444:
266 iCBPCY |= ((iNumBlockCBP & 0xf) << (iBlock * 4));
267 for (k = 0; k < 2; k++) {
268 bIsChroma = ((iNumBlockCBP>>(k+4)) & 0x01);
269 if (bIsChroma) { // U is present in block
270 Int iCode = _getHuffShort(pAHex1->m_hufDecTable, pIO);
271 switch (iCode) {
272 case 1:
273 iCode = _getBit16(pIO, 2);
274 if (iCode == 0)
275 iCode = 3;
276 else if (iCode == 1)
277 iCode = 5;
278 else {
279 static const Int aTab[] = { 6, 9, 10, 12 };
280 iCode = aTab[iCode * 2 + _getBool16 (pIO) - 4];
281 }
282 break;
283 case 0:
284 iCode = 1 << _getBit16(pIO, 2);
285 break;
286 case 2:
287 iCode = 0xf ^ (1 << _getBit16(pIO, 2));
288 break;
289 case 3:
290 iCode = 0xf;
291 }
292 if (k == 0)
293 iCBPCU |= (iCode << (iBlock * 4));
294 else
295 iCBPCV |= (iCode << (iBlock * 4));
296 }
297 }
298 break;
299
300 case YUV_420:
301 iCBPCY |= ((iNumBlockCBP & 0xf) << (iBlock * 4));
302 iCBPCU |= ((iNumBlockCBP >> 4) & 0x1) << (iBlock);
303 iCBPCV |= ((iNumBlockCBP >> 5) & 0x1) << (iBlock);
304 break;
305
306 case YUV_422:
307 iCBPCY |= ((iNumBlockCBP & 0xf) << (iBlock * 4));
308 for (k = 0; k < 2; k ++) {
309 Int iCode = 5;
310 const Int iShift[4] = {0, 1, 4, 5};
311 if((iNumBlockCBP >> (k + 4)) & 0x01) {
312 if(_getBool16(pIO)) {
313 iCode = 1;
314 }
315 else if(_getBool16(pIO)){
316 iCode = 4;
317 }
318 iCode <<= iShift[iBlock];
319 if(k == 0) iCBPCU |= iCode;
320 else iCBPCV |= iCode;
321 }
322 }
323 break;
324
325 default:
326 iCBPCY |= (iNumBlockCBP << (iBlock * 4));
327 }
328 }
329 }
330
331 pSC->MBInfo.iDiffCBP[i] = iCBPCY;
332 if (cf == YUV_420 || cf == YUV_444 || cf == YUV_422) {
333 pSC->MBInfo.iDiffCBP[1] = iCBPCU;
334 pSC->MBInfo.iDiffCBP[2] = iCBPCV;
335 }
336 }
337 }
338
339 /*************************************************************************
340 Experimental code -- decodeBlock
341 SR = <0 1 2> == <last, nonsignificant, significant run>
342 alphabet 12:
343 pAHexpt[0] == <SR', SL, SR | first symbol>
344 alphabet 6:
345 pAHexpt[1] == <SR', SL | continuous>
346 pAHexpt[2] == <SR', SL | continuous>
347 alphabet 4:
348 pAHexpt[3] == <SR', SL | 2 free slots> (SR may be last or insignificant only)
349 alphabet f(run) (this can be extended to 6 contexts - SL and SR')
350 pAHexpt[4] == <run | continuous>
351 alphabet f(lev) (this can be extended to 9 contexts)
352 pAHexpt[5-6] == <lev | continuous> first symbol
353 pAHexpt[7-8] == <lev | continuous> condition on SRn no use
354 *************************************************************************/
355
DecodeSignificantRun(Int iMaxRun,struct CAdaptiveHuffman * pAHexpt,BitIOInfo * pIO)356 Int _FORCEINLINE DecodeSignificantRun (Int iMaxRun, struct CAdaptiveHuffman *pAHexpt, BitIOInfo* pIO)
357 {
358 Int iIndex;
359 static const Int aRemap[] = {1,2,3,5,7, 1,2,3,5,7, /*1,2,3,4,6, */1,2,3,4,5 };
360 Int iBin = gSignificantRunBin[iMaxRun];
361 Int iRun = 0, iFLC = 0;
362
363 if (iMaxRun < 5) {
364 if (iMaxRun == 1) {
365 return 1;
366 }
367 else if (_getBool16 (pIO)) {
368 return 1;
369 }
370 else if (iMaxRun == 2 || _getBool16 (pIO)) {
371 return 2;
372 }
373 else if (iMaxRun == 3 || _getBool16 (pIO)) {
374 return 3;
375 }
376 return 4;
377 }
378 iIndex = _getHuffShort (pAHexpt->m_hufDecTable, pIO);
379 iIndex += iBin * 5;
380 iRun = aRemap[iIndex];
381 iFLC = gSignificantRunFixedLength[iIndex];
382 if (iFLC) {
383 iRun += _getBit16 (pIO, iFLC);
384 }
385 return iRun;
386 }
387
388 #ifndef X86OPT_INLINE
DecodeFirstIndex(Int * pIndex,struct CAdaptiveHuffman * pAHexpt,BitIOInfo * pIO)389 static Void DecodeFirstIndex (Int *pIndex, struct CAdaptiveHuffman *pAHexpt,
390 BitIOInfo* pIO)
391 #else
392 static __forceinline Void DecodeFirstIndex (Int *pIndex, struct CAdaptiveHuffman *pAHexpt,
393 BitIOInfo* pIO)
394 #endif
395 {
396 Int iIndex;
397 iIndex = getHuff (pAHexpt->m_hufDecTable, pIO);
398 pAHexpt->m_iDiscriminant += pAHexpt->m_pDelta[iIndex];
399 pAHexpt->m_iDiscriminant1 += pAHexpt->m_pDelta1[iIndex];
400 *pIndex = iIndex;
401 }
402
403 #ifndef X86OPT_INLINE
DecodeIndex(Int * pIndex,Int iLoc,struct CAdaptiveHuffman * pAHexpt,BitIOInfo * pIO)404 static Void DecodeIndex (Int *pIndex, Int iLoc, struct CAdaptiveHuffman *pAHexpt,
405 BitIOInfo* pIO)
406 #else
407 static __forceinline Void DecodeIndex (Int *pIndex, Int iLoc,
408 struct CAdaptiveHuffman *pAHexpt, BitIOInfo* pIO)
409 #endif
410 {
411 Int iIndex;
412 if (iLoc < 15) {
413 iIndex = _getHuffShort (pAHexpt->m_hufDecTable, pIO);
414 pAHexpt->m_iDiscriminant += pAHexpt->m_pDelta[iIndex];
415 pAHexpt->m_iDiscriminant1 += pAHexpt->m_pDelta1[iIndex];
416 *pIndex = iIndex;
417 }
418 else if (iLoc == 15) {
419 if (_getBool16 (pIO) == 0) {
420 iIndex = 0;
421 }
422 else if (_getBool16 (pIO) == 0) {
423 iIndex = 2;
424 }
425 else {
426 iIndex = 1 + 2 * _getBool16 (pIO);
427 }
428 *pIndex = iIndex;
429 }
430 else { //if (iLoc == 16) { /* deterministic */
431 Int iSL = _getBit16 (pIO, 1/* + 1*/);
432 *pIndex = iSL;// >> 1;
433 }
434 }
435
DecodeBlock(Bool bChroma,Int * aLocalCoef,struct CAdaptiveHuffman ** pAHexpt,const Int iContextOffset,BitIOInfo * pIO,Int iLocation)436 static _FORCEINLINE Int DecodeBlock (Bool bChroma, Int *aLocalCoef, struct CAdaptiveHuffman **pAHexpt,
437 const Int iContextOffset, BitIOInfo* pIO, Int iLocation)
438 {
439 Int iSR, iSRn, iIndex, iNumNonzero = 1, iCont, iSign;
440 struct CAdaptiveHuffman **pAH1 = pAHexpt + iContextOffset + bChroma * 3;
441
442 /** first symbol **/
443 DecodeFirstIndex (&iIndex, /*&iSign, */pAH1[0], pIO);
444 iSR = (iIndex & 1);
445 iSRn = iIndex >> 2;
446
447 iCont = iSR & iSRn;
448 iSign = _getSign(pIO);
449
450 if (iIndex & 2 /* iSL */) {
451 aLocalCoef[1] = (DecodeSignificantAbsLevel (pAHexpt[6 + iContextOffset + iCont], pIO) ^ iSign) - iSign;
452 }
453 else {
454 aLocalCoef[1] = (1 | iSign); // 0 -> 1; -1 -> -1
455 }
456 aLocalCoef[0] = 0;
457 if (iSR == 0) {
458 aLocalCoef[0] = DecodeSignificantRun (15 - iLocation, pAHexpt[0], pIO);
459 }
460 iLocation += aLocalCoef[0] + 1;
461
462 while (iSRn != 0) {
463 iSR = iSRn & 1;
464 aLocalCoef[iNumNonzero * 2] = 0;
465 if (iSR == 0) {
466 aLocalCoef[iNumNonzero * 2] = DecodeSignificantRun (15 - iLocation, pAHexpt[0], pIO);
467 }
468 iLocation += aLocalCoef[iNumNonzero * 2] + 1;
469 DecodeIndex (&iIndex, /*&iSign, */iLocation, pAH1[iCont + 1], pIO);
470 iSRn = iIndex >> 1;
471
472 assert (iSRn >= 0 && iSRn < 3);
473 iCont &= iSRn; /** huge difference! **/
474 iSign = _getSign(pIO);
475
476 if (iIndex & 1 /* iSL */) {
477 aLocalCoef[iNumNonzero * 2 + 1] =
478 (DecodeSignificantAbsLevel (pAHexpt[6 + iContextOffset + iCont], pIO) ^ iSign) - iSign;
479 }
480 else {
481 aLocalCoef[iNumNonzero * 2 + 1] = (1 | iSign); // 0 -> 1; -1 -> -1 (was 1 + (iSign * 2))
482 }
483 iNumNonzero++;
484 }
485 return iNumNonzero;
486 }
487
488 /*************************************************************************
489 DecodeBlockHighpass :
490 *************************************************************************/
DecodeBlockHighpass(const Bool bChroma,struct CAdaptiveHuffman ** pAHexpt,BitIOInfo * pIO,const Int iQP,Int * pCoef,CAdaptiveScan * pScan)491 static _FORCEINLINE Int DecodeBlockHighpass (const Bool bChroma, struct CAdaptiveHuffman **pAHexpt,
492 BitIOInfo* pIO, const Int iQP, Int *pCoef, CAdaptiveScan *pScan)
493 {
494 const Int iContextOffset = CTDC + CONTEXTX;
495 UInt iLoc = 1;
496 Int iSR, iSRn, iIndex, iNumNonzero = 1, iCont, iSign, iLevel;
497 struct CAdaptiveHuffman **pAH1 = pAHexpt + iContextOffset + bChroma * 3;
498 const CAdaptiveScan *pConstScan = (const CAdaptiveScan *) pScan;
499
500 /** first symbol **/
501 DecodeFirstIndex (&iIndex, /*&iSign, */pAH1[0], pIO);
502 iSR = (iIndex & 1);
503 iSRn = iIndex >> 2;
504
505 iCont = iSR & iSRn;
506 iSign = _getSign(pIO);
507
508 iLevel = (iQP ^ iSign) - iSign;
509 if (iIndex & 2 /* iSL */) {
510 iLevel *= DecodeSignificantAbsLevel (pAHexpt[6 + iContextOffset + iCont], pIO);// ^ iSign) - iSign;
511 }
512 //else {
513 // iLevel = (1 | iSign); // 0 -> 1; -1 -> -1
514 //}
515 if (iSR == 0) {
516 iLoc += DecodeSignificantRun (15 - iLoc, pAHexpt[0], pIO);
517 }
518 iLoc &= 0xf;
519 pCoef[pConstScan[iLoc].uScan] = (PixelI) iLevel;//(PixelI)(iQP * iLevel);
520 pScan[iLoc].uTotal++;
521 if (iLoc && pScan[iLoc].uTotal > pScan[iLoc - 1].uTotal) {
522 CAdaptiveScan cTemp = pScan[iLoc];
523 pScan[iLoc] = pScan[iLoc - 1];
524 pScan[iLoc - 1] = cTemp;
525 }
526 iLoc = (iLoc + 1) & 0xf;
527 //iLoc++;
528
529 while (iSRn != 0) {
530 iSR = iSRn & 1;
531 if (iSR == 0) {
532 iLoc += DecodeSignificantRun (15 - iLoc, pAHexpt[0], pIO);
533 if (iLoc >= 16)
534 return 16;
535 }
536 DecodeIndex (&iIndex, /*&iSign, */iLoc + 1, pAH1[iCont + 1], pIO);
537 iSRn = iIndex >> 1;
538
539 assert (iSRn >= 0 && iSRn < 3);
540 iCont &= iSRn; /** huge difference! **/
541 iSign = _getSign(pIO);
542
543 iLevel = (iQP ^ iSign) - iSign;
544 if (iIndex & 1 /* iSL */) {
545 iLevel *= DecodeSignificantAbsLevel (pAHexpt[6 + iContextOffset + iCont], pIO);// ^ iSign) - iSign;
546 //iLevel = (DecodeSignificantAbsLevel (pAHexpt[6 + iContextOffset + iCont], pIO) ^ iSign) - iSign;
547 }
548 //else {
549 // iLevel = (1 | iSign); // 0 -> 1; -1 -> -1 (was 1 + (iSign * 2))
550 //}
551
552 pCoef[pConstScan[iLoc].uScan] = (PixelI) iLevel;//(PixelI)(iQP * iLevel);
553 pScan[iLoc].uTotal++;
554 if (iLoc && pScan[iLoc].uTotal > pScan[iLoc - 1].uTotal) {
555 CAdaptiveScan cTemp = pScan[iLoc];
556 pScan[iLoc] = pScan[iLoc - 1];
557 pScan[iLoc - 1] = cTemp;
558 }
559
560 iLoc = (iLoc + 1) & 0xf;
561 iNumNonzero++;
562 }
563 return iNumNonzero;
564 }
565
566 /*************************************************************************
567 DecodeBlockAdaptive
568 *************************************************************************/
DecodeBlockAdaptive(Bool bNoSkip,Bool bChroma,CAdaptiveHuffman ** pAdHuff,BitIOInfo * pIO,BitIOInfo * pIOFL,PixelI * pCoeffs,CAdaptiveScan * pScan,const Int iModelBits,const Int iTrim,const Int iQP,const Int * pOrder,const Bool bSkipFlexbits)569 static _FORCEINLINE Int DecodeBlockAdaptive (Bool bNoSkip, Bool bChroma, CAdaptiveHuffman **pAdHuff,
570 BitIOInfo *pIO, BitIOInfo *pIOFL,
571 PixelI *pCoeffs, CAdaptiveScan *pScan,
572 const Int iModelBits, const Int iTrim, const Int iQP,
573 const Int *pOrder, const Bool bSkipFlexbits)
574 {
575 // const Int iLocation = 1;
576 // const Int iContextOffset = CTDC + CONTEXTX;
577 Int kk, iNumNonzero = 0, iFlex = iModelBits - iTrim;
578
579 if (iFlex < 0 || bSkipFlexbits)
580 iFlex = 0;
581
582 if (bNoSkip) {
583 const Int iQP1 = (iQP << iModelBits);
584 iNumNonzero = DecodeBlockHighpass (bChroma, pAdHuff, pIO, iQP1, pCoeffs, pScan);
585 }
586 if (iFlex) {
587 UInt k;
588 if (iQP + iTrim == 1) { // only iTrim = 0, iQP = 1 is legal
589 assert (iTrim == 0);
590 assert (iQP == 1);
591
592 for (k = 1; k < 16; k++) {
593 PixelI *pk = pCoeffs + pOrder[k];
594 if (*pk < 0) {
595 Int fine = _getBit16(pIOFL, iFlex);
596 *pk -= (PixelI)(fine);
597 }
598 else if (*pk > 0) {
599 Int fine = _getBit16(pIOFL, iFlex);
600 *pk += (PixelI)(fine);
601 }
602 else {
603 *pk = (PixelI)(_getBit16s(pIOFL, iFlex));
604 }
605 }
606 }
607 else {
608 const Int iQP1 = iQP << iTrim;
609 for (k = 1; k < 16; k++) {
610 kk = pCoeffs[pOrder[k]];
611 if (kk < 0) {
612 Int fine = _getBit16(pIOFL, iFlex);
613 pCoeffs[pOrder[k]] -= (PixelI)(iQP1 * fine);
614 }
615 else if (kk > 0) {
616 Int fine = _getBit16(pIOFL, iFlex);
617 pCoeffs[pOrder[k]] += (PixelI)(iQP1 * fine);
618 }
619 else {
620 pCoeffs[pOrder[k]] = (PixelI)(iQP1 * _getBit16s(pIOFL, iFlex));
621 }
622 }
623 }
624 }
625
626 return iNumNonzero;
627 }
628
629
630 /*************************************************************************
631 GetCoeffs
632 *************************************************************************/
DecodeCoeffs(CWMImageStrCodec * pSC,CCodingContext * pContext,Int iMBX,Int iMBY,BitIOInfo * pIO,BitIOInfo * pIOFL)633 static _FORCEINLINE Int DecodeCoeffs (CWMImageStrCodec * pSC, CCodingContext *pContext,
634 Int iMBX, Int iMBY,
635 BitIOInfo* pIO, BitIOInfo *pIOFL)
636 {
637 CWMITile * pTile = pSC->pTile + pSC->cTileColumn;
638 const COLORFORMAT cf = pSC->m_param.cfColorFormat;
639 const Int iChannels = (Int) pSC->m_param.cNumChannels;
640 const Int iPlanes = (cf == YUV_420 || cf == YUV_422) ? 1 : iChannels;
641 Int iQP;
642 CAdaptiveScan *pScan;
643 PixelI *pCoeffs;
644 Int i, iBlock, iSubblock, iNBlocks = 4;
645 Int iModelBits = pContext->m_aModelAC.m_iFlcBits[0];
646 Int aLaplacianMean[2] = { 0, 0}, *pLM = aLaplacianMean + 0;
647 const Int *pOrder = dctIndex[0];
648 const Int iOrient = pSC->MBInfo.iOrientation;
649 Bool bChroma = FALSE;
650
651 Int iCBPCU = pSC->MBInfo.iCBP[1];
652 Int iCBPCV = pSC->MBInfo.iCBP[2];
653 Int iCBPCY = pSC->MBInfo.iCBP[0];
654
655 UNREFERENCED_PARAMETER( iMBX );
656 UNREFERENCED_PARAMETER( iMBY );
657
658 /** set scan arrays and other MB level constants **/
659 if (iOrient == 1) {
660 pScan = pContext->m_aScanVert;
661 }
662 else {
663 pScan = pContext->m_aScanHoriz;
664 }
665
666 if (cf == YUV_420) {
667 iNBlocks = 6;
668 iCBPCY += (iCBPCU << 16) + (iCBPCV << 20);
669 }
670 else if (cf == YUV_422) {
671 iNBlocks = 8;
672 iCBPCY += (iCBPCU << 16) + (iCBPCV << 24);
673 }
674
675 for (i = 0; i < iPlanes; i++) {
676 Int iIndex = 0, iNumNonZero;
677
678 if(pSC->WMISCP.sbSubband != SB_NO_FLEXBITS)
679 readIS_L1(pSC, pIOFL);
680
681 for (iBlock = 0; iBlock < iNBlocks; iBlock++) {
682
683 readIS_L2(pSC, pIO);
684 if (pIO != pIOFL)
685 readIS_L2(pSC, pIOFL);
686
687 iQP = (pSC->m_param.bTranscode ? 1 : pTile->pQuantizerHP[iPlanes > 1 ? i : (iBlock > 3 ? (cf == YUV_420 ? iBlock - 3 : iBlock / 2 - 1) : 0)][pSC->MBInfo.iQIndexHP].iQP);
688
689 for (iSubblock = 0; iSubblock < 4; iSubblock++, iIndex++, iCBPCY >>= 1) {
690 pCoeffs = pSC->p1MBbuffer[i] + blkOffset[iIndex & 0xf];
691
692 //if (iBlock < 4) {//(cf == YUV_444) {
693 //bBlockNoSkip = ((iTempCBPC & (1 << iIndex1)) != 0);
694 //pCoeffs = pSC->p1MBbuffer[iBlock >> 2] + blkOffset[iIndex & 0xf];
695 //}
696 //else {
697 if (iBlock >= 4) {
698 if(cf == YUV_420) {
699 pCoeffs = pSC->p1MBbuffer[iBlock - 3] + blkOffsetUV[iSubblock];
700 }
701 else { // YUV_422
702 pCoeffs = pSC->p1MBbuffer[1 + (1 & (iBlock >> 1))] + ((iBlock & 1) * 32) + blkOffsetUV_422[iSubblock];
703 }
704 }
705
706 /** read AC values **/
707 assert (pSC->m_Dparam->bSkipFlexbits == 0 || pSC->WMISCP.bfBitstreamFormat == FREQUENCY || pSC->WMISCP.sbSubband == SB_NO_FLEXBITS);
708 iNumNonZero = DecodeBlockAdaptive ((iCBPCY & 1), bChroma, pContext->m_pAHexpt,
709 pIO, pIOFL, pCoeffs, pScan, iModelBits, pContext->m_iTrimFlexBits,
710 iQP, pOrder, pSC->m_Dparam->bSkipFlexbits);
711 if(iNumNonZero > 16) // something is wrong!
712 return ICERR_ERROR;
713 // shouldn't this be > 15?
714 (*pLM) += iNumNonZero;
715 }
716 if (iBlock == 3) {
717 iModelBits = pContext->m_aModelAC.m_iFlcBits[1];
718 pLM = aLaplacianMean + 1;
719 bChroma = TRUE;
720 }
721 }
722
723 iCBPCY = pSC->MBInfo.iCBP[(i + 1) & 0xf];
724 assert (MAX_CHANNELS == 16);
725 }
726
727 /** update model at end of MB **/
728 UpdateModelMB (cf, iChannels, aLaplacianMean, &(pContext->m_aModelAC));
729 return ICERR_OK;
730 }
731
732 /*************************************************************************
733 DecodeSignificantAbsLevel
734 *************************************************************************/
735 #ifndef X86OPT_INLINE
DecodeSignificantAbsLevel(struct CAdaptiveHuffman * pAHexpt,BitIOInfo * pIO)736 static Int DecodeSignificantAbsLevel (struct CAdaptiveHuffman *pAHexpt, BitIOInfo* pIO)
737 #else
738 static __forceinline Int DecodeSignificantAbsLevel (struct CAdaptiveHuffman *pAHexpt, BitIOInfo* pIO)
739 #endif
740 {
741 UInt iIndex;
742 Int iFixed, iLevel;
743 static const Int aRemap[] = { 2, 3, 4, 6, 10, 14 };
744 static const Int aFixedLength[] = { 0, 0, 1, 2, 2, 2 };
745
746 iIndex = (UInt)getHuff (pAHexpt->m_hufDecTable, pIO);
747 assert(iIndex <= 6);
748 pAHexpt->m_iDiscriminant += pAHexpt->m_pDelta[iIndex];
749 if (iIndex < 2) {
750 iLevel = iIndex + 2; // = aRemap[iIndex]
751 }
752 else if (iIndex < 6) {
753 iFixed = aFixedLength[iIndex];
754 iLevel = aRemap[iIndex] + _getBit16 (pIO, iFixed);
755 }
756 else{
757 iFixed = _getBit16 (pIO, 4) + 4;
758 if (iFixed == 19) {
759 iFixed += _getBit16 (pIO, 2);
760 if (iFixed == 22) {
761 iFixed += _getBit16 (pIO, 3);
762 }
763 }
764 iLevel = 2 + (1 << iFixed);
765 iIndex = getBit32 (pIO, iFixed);
766 iLevel += iIndex;
767 }
768 return iLevel;
769 }
770
decodeQPIndex(BitIOInfo * pIO,U8 cBits)771 U8 decodeQPIndex(BitIOInfo* pIO,U8 cBits)
772 {
773 if(_getBit16(pIO, 1) == 0)
774 return 0;
775 return (U8)(_getBit16(pIO, cBits) + 1);
776 }
777
778 /*************************************************************************
779 DecodeSecondStageCoeff
780 *************************************************************************/
DecodeMacroblockLowpass(CWMImageStrCodec * pSC,CCodingContext * pContext,Int iMBX,Int iMBYdummy)781 Int DecodeMacroblockLowpass (CWMImageStrCodec * pSC, CCodingContext *pContext,
782 Int iMBX, Int iMBYdummy)
783 {
784 const COLORFORMAT cf = pSC->m_param.cfColorFormat;
785 const Int iChannels = (Int) pSC->m_param.cNumChannels;
786 const Int iFullPlanes = (cf == YUV_420 || cf == YUV_422) ? 2 : iChannels;
787 Int k;
788 CAdaptiveScan *pScan = pContext->m_aScanLowpass;
789 BitIOInfo* pIO = pContext->m_pIOLP;
790 Int iModelBits = pContext->m_aModelLP.m_iFlcBits[0];
791 Int aRLCoeffs[32], iNumNonzero = 0, iIndex = 0;
792 Int aLaplacianMean[2] = { 0, 0}, *pLM = aLaplacianMean;
793 Int iChannel, iCBP = 0;
794 #ifndef ARMOPT_BITIO // ARM opt always uses 32-bit version of getBits
795 U32 (*getBits)(BitIOInfo* pIO, U32 cBits) = _getBit16;
796 #endif
797 CWMIMBInfo * pMBInfo = &pSC->MBInfo;
798 I32 *aDC[MAX_CHANNELS];
799
800 UNREFERENCED_PARAMETER( iMBX );
801 UNREFERENCED_PARAMETER( iMBYdummy );
802
803 readIS_L1(pSC, pIO);
804 if((pSC->WMISCP.bfBitstreamFormat != SPATIAL) && (pSC->pTile[pSC->cTileColumn].cBitsLP > 0)) // MB-based LP QP index
805 pMBInfo->iQIndexLP = decodeQPIndex(pIO, pSC->pTile[pSC->cTileColumn].cBitsLP);
806
807 // set arrays
808 for (k = 0; k < (Int) pSC->m_param.cNumChannels; k++) {
809 aDC[k & 15] = pMBInfo->iBlockDC[k];
810 }
811
812 /** reset adaptive scan totals **/
813 if (pSC->m_bResetRGITotals) {
814 int iScale = 2;
815 int iWeight = iScale * 16;
816 pScan[0].uTotal = MAXTOTAL;
817 for (k = 1; k < 16; k++) {
818 pScan[k].uTotal = iWeight;
819 iWeight -= iScale;
820 }
821 }
822
823 /** in raw mode, this can take 6% of the bits in the extreme low rate case!!! **/
824 if (cf == YUV_420 || cf == YUV_422 || cf == YUV_444) {
825 int iCountM = pContext->m_iCBPCountMax, iCountZ = pContext->m_iCBPCountZero;
826 int iMax = iFullPlanes * 4 - 5; /* actually (1 << iNChannels) - 1 **/
827 if (iCountZ <= 0 || iCountM < 0) {
828 iCBP = 0;
829 if (_getBool16 (pIO)) {
830 iCBP = 1;
831 k = _getBit16 (pIO, iFullPlanes - 1);
832 if (k) {
833 iCBP = k * 2 + _getBit16(pIO, 1);
834 }
835 }
836 if (iCountM < iCountZ)
837 iCBP = iMax - iCBP;
838 }
839 else {
840 iCBP = _getBit16(pIO, iFullPlanes);
841 }
842
843 iCountM += 1 - 4 * (iCBP == iMax);//(b + c - 2*a);
844 iCountZ += 1 - 4 * (iCBP == 0);//(a + b - 2*c);
845 if (iCountM < -8)
846 iCountM = -8;
847 else if (iCountM > 7)
848 iCountM = 7;
849 pContext->m_iCBPCountMax = iCountM;
850
851 if (iCountZ < -8)
852 iCountZ = -8;
853 else if (iCountZ > 7)
854 iCountZ = 7;
855 pContext->m_iCBPCountZero = iCountZ;
856 }
857 else { /** 1 or N channel **/
858 for (iChannel = 0; iChannel < iChannels; iChannel++)
859 iCBP |= (getBits (pIO, 1) << iChannel);
860 }
861
862 #ifndef ARMOPT_BITIO // ARM opt always uses 32-bit version of getBits
863 if (pContext->m_aModelLP.m_iFlcBits[0] > 14 || pContext->m_aModelLP.m_iFlcBits[1] > 14) {
864 getBits = getBit32;
865 }
866 #endif
867
868 for (iChannel = 0; iChannel < iFullPlanes; iChannel++) {
869 PixelI *pCoeffs = aDC[iChannel];
870
871 if (iCBP & 1) {
872 iNumNonzero = DecodeBlock (iChannel > 0, aRLCoeffs, pContext->m_pAHexpt,
873 CTDC, pIO, 1 + 9 * ((cf == YUV_420) && (iChannel == 1))
874 + ((cf == YUV_422) && (iChannel == 1)));
875
876 if ((cf == YUV_420 || cf == YUV_422) && iChannel) {
877 Int aTemp[16]; //14 required, 16 for security
878 static const Int aRemap[] = { 4, 1,2,3, 5,6,7 };
879 const Int *pRemap = aRemap + (cf == YUV_420);
880 const Int iCount = (cf == YUV_420) ? 6 : 14;
881
882 (*pLM) += iNumNonzero;
883 iIndex = 0;
884 memset (aTemp, 0, sizeof(aTemp));
885
886 for (k = 0; k < iNumNonzero; k++) {
887 iIndex += aRLCoeffs[k * 2];
888 aTemp[iIndex & 0xf] = aRLCoeffs[k * 2 + 1];
889 iIndex++;
890 }
891
892 for (k = 0; k < iCount; k++) {
893 aDC[(k & 1) + 1][pRemap[k >> 1]] = aTemp[k];
894 }
895 }
896 else {
897 (*pLM) += iNumNonzero;
898 iIndex = 1;
899
900 for (k = 0; k < iNumNonzero; k++) {
901 iIndex += aRLCoeffs[k * 2];
902 pCoeffs[pScan[iIndex].uScan] = aRLCoeffs[k * 2 + 1];
903 pScan[iIndex].uTotal++;
904 if (pScan[iIndex].uTotal > pScan[iIndex - 1].uTotal) {
905 CAdaptiveScan cTemp = pScan[iIndex];
906 pScan[iIndex] = pScan[iIndex - 1];
907 pScan[iIndex - 1] = cTemp;
908 }
909 iIndex++;
910 }
911 }
912 }
913
914 if (iModelBits) {
915 if ((cf == YUV_420 || cf == YUV_422) && iChannel) {
916 for (k = 1; k < (cf == YUV_420 ? 4 : 8); k++) {
917 if (aDC[1][k] > 0) {
918 aDC[1][k] <<= iModelBits;
919 aDC[1][k] += getBits (pIO, iModelBits);
920 }
921 else if (aDC[1][k] < 0) {
922 aDC[1][k] <<= iModelBits;
923 aDC[1][k] -= getBits (pIO, iModelBits);
924 }
925 else {
926 aDC[1][k] = getBits (pIO, iModelBits);
927 if (aDC[1][k] && _getBool16 (pIO))
928 aDC[1][k] = -aDC[1][k];
929 }
930
931 if (aDC[2][k] > 0) {
932 aDC[2][k] <<= iModelBits;
933 aDC[2][k] += getBits (pIO, iModelBits);
934 }
935 else if (aDC[2][k] < 0) {
936 aDC[2][k] <<= iModelBits;
937 aDC[2][k] -= getBits (pIO, iModelBits);
938 }
939 else {
940 aDC[2][k] = getBits (pIO, iModelBits);
941 if (aDC[2][k] && _getBool16 (pIO))
942 aDC[2][k] = -aDC[2][k];
943 }
944 }
945 }
946 else {
947 #ifdef WIN32
948 const Int iMask = (1 << iModelBits) - 1;
949 #endif // WIN32
950 for (k = 1; k < 16; k++) {
951 #ifdef WIN32
952 if (pCoeffs[k]) {
953 Int r1 = _rotl(pCoeffs[k], iModelBits);
954 pCoeffs[k] = (r1 ^ getBits(pIO, iModelBits)) - (r1 & iMask);
955 }
956 #else // WIN32
957 if (pCoeffs[k] > 0) {
958 pCoeffs[k] <<= iModelBits;
959 pCoeffs[k] += getBits (pIO, iModelBits);
960 }
961 else if (pCoeffs[k] < 0) {
962 pCoeffs[k] <<= iModelBits;
963 pCoeffs[k] -= getBits (pIO, iModelBits);
964 }
965 #endif // WIN32
966 else {
967 //pCoeffs[k] = getBits (pIO, iModelBits);
968 //if (pCoeffs[k] && _getBool16 (pIO))
969 // pCoeffs[k] = -pCoeffs[k];
970 Int r1 = _peekBit16 (pIO, iModelBits + 1);
971 pCoeffs[k] = ((r1 >> 1) ^ (-(r1 & 1))) + (r1 & 1);
972 _flushBit16 (pIO, iModelBits + (pCoeffs[k] != 0));
973 }
974 }
975 }
976 }
977 pLM = aLaplacianMean + 1;
978 iModelBits = pContext->m_aModelLP.m_iFlcBits[1];
979
980 iCBP >>= 1;
981 }
982
983 UpdateModelMB (cf, iChannels, aLaplacianMean, &(pContext->m_aModelLP));
984
985 if (pSC->m_bResetContext) {
986 AdaptLowpassDec(pContext);
987 }
988
989 return ICERR_OK;
990 }
991
992 /*************************************************************************
993 8 bit YUV 420 macroblock decode function with 4x4 transform
994 Index order is as follows:
995 Y: U: V:
996 0 1 4 5 16 17 20 21
997 2 3 6 7 18 19 22 23
998 8 9 12 13
999 10 11 14 15
1000
1001 DCAC coefficients stored for 4x4 - offsets (x == no storage)
1002 Y:
1003 x x x [0..3]
1004 x x x [4..7]
1005 x x x [8..11]
1006 [16..19] [20..23] [24..27] [28..31,12..15]
1007
1008 U, V:
1009 x [0..3]
1010 [8..11] [4..7,12..15]
1011 *************************************************************************/
DecodeMacroblockDC(CWMImageStrCodec * pSC,CCodingContext * pContext,Int iMBX,Int iMBY)1012 Int DecodeMacroblockDC(CWMImageStrCodec * pSC, CCodingContext *pContext, Int iMBX, Int iMBY)
1013 {
1014 CWMITile * pTile = pSC->pTile + pSC->cTileColumn;
1015 CWMIMBInfo * pMBInfo = &pSC->MBInfo;
1016 const COLORFORMAT cf = pSC->m_param.cfColorFormat;
1017 const Int iChannels = (Int) pSC->m_param.cNumChannels;
1018 BitIOInfo* pIO = pContext->m_pIODC;
1019 Int iIndex, i;
1020 Int aLaplacianMean[2] = { 0, 0}, *pLM = aLaplacianMean;
1021 Int iModelBits = pContext->m_aModelDC.m_iFlcBits[0];
1022 struct CAdaptiveHuffman *pAH;
1023 Int iQDCY, iQDCU, iQDCV;
1024 // const Int iChromaElements = (cf == YUV_420) ? 8 * 8 : ((cf == YUV_422) ? 8 * 16 : 16 * 16);
1025
1026 UNREFERENCED_PARAMETER( iMBX );
1027 UNREFERENCED_PARAMETER( iMBY );
1028
1029 for (i = 0; i < iChannels; i++)
1030 memset (pMBInfo->iBlockDC[i], 0, 16 * sizeof (I32));
1031
1032 readIS_L1(pSC, pIO);
1033
1034 pMBInfo->iQIndexLP = pMBInfo->iQIndexHP = 0;
1035
1036 if(pSC->WMISCP.bfBitstreamFormat == SPATIAL && pSC->WMISCP.sbSubband != SB_DC_ONLY){
1037 if(pTile->cBitsLP > 0) // MB-based LP QP index
1038 pMBInfo->iQIndexLP = decodeQPIndex(pIO, pTile->cBitsLP);
1039 if( pSC->WMISCP.sbSubband != SB_NO_HIGHPASS && pTile->cBitsHP > 0) // MB-based HP QP index
1040 pMBInfo->iQIndexHP = decodeQPIndex(pIO, pTile->cBitsHP);
1041 }
1042 if(pTile->cBitsHP == 0 && pTile->cNumQPHP > 1) // use LP QP
1043 pMBInfo->iQIndexHP = pMBInfo->iQIndexLP;
1044 if (pMBInfo->iQIndexLP >= pTile->cNumQPLP || pMBInfo->iQIndexHP >= pTile->cNumQPHP)
1045 return ICERR_ERROR;
1046
1047 if(cf == Y_ONLY || cf == CMYK || cf == NCOMPONENT) {
1048 for (i = 0; i < iChannels; i++) {
1049 iQDCY = 0;
1050 /** get luminance DC **/
1051 if (_getBool16 (pIO)) {
1052 iQDCY = DecodeSignificantAbsLevel(pContext->m_pAHexpt[3], pIO) - 1;
1053 *pLM += 1;
1054 }
1055 if (iModelBits) {
1056 iQDCY = (iQDCY << iModelBits) | _getBit16(pIO, iModelBits);
1057 }
1058 if (iQDCY && _getBool16 (pIO))
1059 iQDCY = -iQDCY;
1060 pMBInfo->iBlockDC[i][0] = iQDCY;
1061
1062 pLM = aLaplacianMean + 1;
1063 iModelBits = pContext->m_aModelDC.m_iFlcBits[1];
1064 }
1065 }
1066 else {
1067 /** find significant level in 3D **/
1068 pAH = pContext->m_pAHexpt[2];
1069 iIndex = getHuff (pAH->m_hufDecTable, pIO);
1070 iQDCY = iIndex >> 2;
1071 iQDCU = (iIndex >> 1) & 1;
1072 iQDCV = iIndex & 1;
1073
1074 /** get luminance DC **/
1075 if (iQDCY) {
1076 iQDCY = DecodeSignificantAbsLevel(pContext->m_pAHexpt[3], pIO) - 1;
1077 *pLM += 1;
1078 }
1079 if (iModelBits) {
1080 iQDCY = (iQDCY << iModelBits) | _getBit16(pIO, iModelBits);
1081 }
1082 if (iQDCY && _getBool16 (pIO))
1083 iQDCY = -iQDCY;
1084 pMBInfo->iBlockDC[0][0] = iQDCY;
1085
1086 /** get chrominance DC **/
1087 pLM = aLaplacianMean + 1;
1088 iModelBits = pContext->m_aModelDC.m_iFlcBits[1];
1089
1090 if (iQDCU) {
1091 iQDCU = DecodeSignificantAbsLevel(pContext->m_pAHexpt[4], pIO) - 1;
1092 *pLM += 1;
1093 }
1094 if (iModelBits) {
1095 iQDCU = (iQDCU << iModelBits) | _getBit16(pIO, iModelBits);
1096 }
1097 if (iQDCU && _getBool16 (pIO))
1098 iQDCU = -iQDCU;
1099 pMBInfo->iBlockDC[1][0] = iQDCU;
1100
1101 if (iQDCV) {
1102 iQDCV = DecodeSignificantAbsLevel(pContext->m_pAHexpt[4], pIO) - 1;
1103 *pLM += 1;
1104 }
1105 if (iModelBits) {
1106 iQDCV = (iQDCV << iModelBits) | _getBit16(pIO, iModelBits);
1107 }
1108 if (iQDCV && _getBool16 (pIO))
1109 iQDCV = -iQDCV;
1110 pMBInfo->iBlockDC[2][0] = iQDCV;
1111 }
1112
1113 UpdateModelMB (cf, iChannels, aLaplacianMean, &(pContext->m_aModelDC));
1114
1115 if(((!(pSC->WMISCP.bfBitstreamFormat != FREQUENCY || pSC->m_Dparam->cThumbnailScale < 16)) || pSC->WMISCP.sbSubband == SB_DC_ONLY) && pSC->m_bResetContext){
1116 Int kk;
1117 for (kk = 2; kk < 5; kk++) {
1118 if (ICERR_OK != AdaptDecFixed (pContext->m_pAHexpt[kk])) {
1119 return ICERR_ERROR;
1120 }
1121 }
1122 }
1123
1124 return ICERR_OK;
1125 }
1126
1127 /*************************************************************************
1128 DecodeMacroblockHighpass
1129 *************************************************************************/
DecodeMacroblockHighpass(CWMImageStrCodec * pSC,CCodingContext * pContext,Int iMBX,Int iMBY)1130 Int DecodeMacroblockHighpass (CWMImageStrCodec *pSC, CCodingContext *pContext,
1131 Int iMBX, Int iMBY)
1132 {
1133 /** reset adaptive scan totals **/
1134 if (pSC->m_bResetRGITotals) {
1135 int iScale = 2, k;
1136 int iWeight = iScale * 16;
1137 pContext->m_aScanHoriz[0].uTotal = pContext->m_aScanVert[0].uTotal = MAXTOTAL;
1138 for (k = 1; k < 16; k++) {
1139 pContext->m_aScanHoriz[k].uTotal = pContext->m_aScanVert[k].uTotal = iWeight;
1140 iWeight -= iScale;
1141 }
1142 }
1143 if((pSC->WMISCP.bfBitstreamFormat != SPATIAL) && (pSC->pTile[pSC->cTileColumn].cBitsHP > 0)) { // MB-based HP QP index
1144 pSC->MBInfo.iQIndexHP = decodeQPIndex(pContext->m_pIOAC, pSC->pTile[pSC->cTileColumn].cBitsHP);
1145 if (pSC->MBInfo.iQIndexHP >= pSC->pTile[pSC->cTileColumn].cNumQPHP)
1146 goto ErrorExit;
1147 }
1148 else if(pSC->pTile[pSC->cTileColumn].cBitsHP == 0 && pSC->pTile[pSC->cTileColumn].cNumQPHP > 1) // use LP QP
1149 pSC->MBInfo.iQIndexHP = pSC->MBInfo.iQIndexLP;
1150
1151
1152 DecodeCBP (pSC, pContext);
1153 predCBPDec(pSC, pContext);
1154
1155 if (DecodeCoeffs (pSC, pContext, iMBX, iMBY,
1156 pContext->m_pIOAC, pContext->m_pIOFL) != ICERR_OK)
1157 goto ErrorExit;
1158
1159 if (pSC->m_bResetContext) {
1160 AdaptHighpassDec(pContext);
1161 }
1162
1163 return ICERR_OK;
1164 ErrorExit:
1165 return ICERR_ERROR;
1166 }
1167
1168 /*************************************************************************
1169 Adapt
1170 *************************************************************************/
AdaptLowpassDec(CCodingContext * pSC)1171 Int AdaptLowpassDec(CCodingContext * pSC)
1172 {
1173 Int kk;
1174 for (kk = 0; kk < CONTEXTX + CTDC; kk++) {
1175 if (ICERR_OK != AdaptDecFixed (pSC->m_pAHexpt[kk])) {
1176 goto ErrorExit;
1177 }
1178 }
1179 return ICERR_OK;
1180
1181 ErrorExit:
1182 return ICERR_ERROR;
1183
1184 }
1185
AdaptHighpassDec(CCodingContext * pSC)1186 Int AdaptHighpassDec(CCodingContext * pSC)
1187 {
1188 Int kk;
1189 if (ICERR_OK != AdaptDecFixed (pSC->m_pAdaptHuffCBPCY)) {
1190 goto ErrorExit;
1191 }
1192 if (ICERR_OK != AdaptDecFixed (pSC->m_pAdaptHuffCBPCY1)) {
1193 goto ErrorExit;
1194 }
1195 for (kk = 0; kk < CONTEXTX; kk++) {
1196 if (ICERR_OK != AdaptDecFixed (pSC->m_pAHexpt[kk + CONTEXTX + CTDC])) {
1197 goto ErrorExit;
1198 }
1199 }
1200
1201 return ICERR_OK;
1202
1203 ErrorExit:
1204 return ICERR_ERROR;
1205 }
1206