segdec.c 42 KB

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