segenc.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186
  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 <stdio.h>
  29. #include <stdlib.h>
  30. #include "strcodec.h"
  31. #include "encode.h"
  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. /** local function definitions **/
  39. #ifdef X86OPT_INLINE
  40. __forceinline
  41. #endif
  42. static Int EncodeBlock (Bool bChroma, const Int *aLocalCoef, Int iNumNonzero,
  43. struct CAdaptiveHuffman **pAHexpt,
  44. Int iContextOffset, BitIOInfo* pOut, UInt iLocation);
  45. /*************************************************************************
  46. EncodeSignificantAbsLevel
  47. *************************************************************************/
  48. #ifdef X86OPT_INLINE
  49. //__forceinline
  50. #endif
  51. static Void EncodeSignificantAbsLevel (UInt iAbsLevel, struct CAdaptiveHuffman *pAHexpt, BitIOInfo* pOut)
  52. {
  53. Int iIndex, iFixed, aIndex[] = { 0,1,2,2, 3,3,3,3, 4,4,4,4, 5,5,5,5 };
  54. Int aFixedLength[] = { 0, 0, 1, 2, 2, 2 };
  55. assert(iAbsLevel > 0);
  56. iAbsLevel--;
  57. if (iAbsLevel >= 16) {
  58. Int i = iAbsLevel;
  59. iIndex = 6;
  60. /** find leftmost bit **/
  61. i >>= 5;
  62. iFixed = 4;
  63. while (i) { /** caution - infinite loop if not careful **/
  64. iFixed++;
  65. assert (iFixed < 30);
  66. i >>= 1;
  67. }
  68. pAHexpt->m_iDiscriminant += pAHexpt->m_pDelta[iIndex];
  69. putBit16z(pOut, pAHexpt->m_pTable[iIndex * 2 + 1], pAHexpt->m_pTable[iIndex * 2 + 2]);
  70. if (iFixed > 18) {
  71. putBit16z (pOut, 15, 4);
  72. if (iFixed > 21) {
  73. putBit16z (pOut, 3, 2);
  74. putBit16 (pOut, iFixed - 22, 3); // 22 - 29
  75. }
  76. else
  77. putBit16z (pOut, iFixed - 19, 2); // 19 20 21
  78. }
  79. else {
  80. putBit16z(pOut, (iFixed - 4), 4);
  81. }
  82. putBit32(pOut, iAbsLevel, iFixed);
  83. }
  84. else {
  85. iIndex = aIndex[iAbsLevel];
  86. iFixed = aFixedLength[iIndex];
  87. pAHexpt->m_iDiscriminant += pAHexpt->m_pDelta[iIndex];
  88. putBit16z(pOut, pAHexpt->m_pTable[iIndex * 2 + 1], pAHexpt->m_pTable[iIndex * 2 + 2]);
  89. putBit32(pOut, iAbsLevel, iFixed);
  90. }
  91. }
  92. /*************************************************************************
  93. EncodeMacroblockDC
  94. *************************************************************************/
  95. Void encodeQPIndex(BitIOInfo* pIO, U8 iIndex,U8 cBits)
  96. {
  97. if(iIndex == 0)
  98. putBit16z(pIO, 0, 1);
  99. else{
  100. putBit16z(pIO, 1, 1);
  101. putBit16z(pIO, iIndex - 1, cBits);
  102. }
  103. }
  104. Int EncodeMacroblockDC (CWMImageStrCodec *pSC, CCodingContext *pContext, Int iMBX, Int iMBY)
  105. {
  106. CWMITile * pTile = pSC->pTile + pSC->cTileColumn;
  107. BitIOInfo* pIO = pContext->m_pIODC;
  108. CWMIMBInfo *pMBInfo = &pSC->MBInfo;
  109. Int iIndex, j = 0;
  110. struct CAdaptiveHuffman *pAH;
  111. Int aLaplacianMean[2] = { 0, 0}, *pLM = aLaplacianMean;
  112. Int iModelBits = pContext->m_aModelDC.m_iFlcBits[0];
  113. COLORFORMAT cf = pSC->m_param.cfColorFormat;
  114. const Int iChannels = (Int) pSC->m_param.cNumChannels;
  115. UNREFERENCED_PARAMETER( iMBX );
  116. UNREFERENCED_PARAMETER( iMBY );
  117. writeIS_L1(pSC, pIO);
  118. if(pSC->m_param.bTranscode == FALSE){
  119. pMBInfo->iQIndexLP = (U8)(pTile->cNumQPLP > 1 ? (rand() % pTile->cNumQPLP) : 0);
  120. pMBInfo->iQIndexHP = (U8)(pTile->cNumQPHP > 1 ? (rand() % pTile->cNumQPHP) : 0);
  121. }
  122. if(pTile->cBitsHP == 0 && pTile->cNumQPHP > 1) // use LP QP
  123. pMBInfo->iQIndexHP = pMBInfo->iQIndexLP;
  124. if(pSC->WMISCP.bfBitstreamFormat == SPATIAL && pSC->WMISCP.sbSubband != SB_DC_ONLY){
  125. if(pTile->cBitsLP > 0) // MB-based LP QP index
  126. encodeQPIndex(pIO, pMBInfo->iQIndexLP, pTile->cBitsLP);
  127. if( pSC->WMISCP.sbSubband != SB_NO_HIGHPASS && pTile->cBitsHP > 0) // MB-based HP QP index
  128. encodeQPIndex(pIO, pMBInfo->iQIndexHP, pTile->cBitsHP);
  129. }
  130. if(pSC->m_param.bTranscode == FALSE)
  131. pSC->Quantize(pSC);
  132. predMacroblockEnc(pSC);
  133. /** code path for Y_ONLY, CMYK and N_CHANNEL DC **/
  134. if(cf == Y_ONLY || cf == CMYK || cf == NCOMPONENT) {
  135. Int iQDC, iDC, iSign;
  136. for (j = 0; j < iChannels; j++) {
  137. iDC = pMBInfo->iBlockDC[j][0];
  138. iSign = (iDC < 0);
  139. iDC = abs(iDC);
  140. iQDC = iDC >> iModelBits;
  141. /** send luminance DC **/
  142. if (iQDC) {
  143. putBit16z(pIO, 1, 1);
  144. EncodeSignificantAbsLevel((UInt) iQDC, pContext->m_pAHexpt[3], pIO);
  145. *pLM += 1;
  146. }
  147. else {
  148. putBit16z(pIO, 0, 1);
  149. }
  150. putBit16(pIO, iDC, iModelBits);
  151. if (iDC) {
  152. putBit16z(pIO, iSign, 1);
  153. }
  154. pLM = aLaplacianMean + 1;
  155. iModelBits = pContext->m_aModelDC.m_iFlcBits[1];
  156. }
  157. }
  158. else { /** code path for YUV DC **/
  159. Int iDCY, iDCU, iDCV, iQDCY, iQDCU, iQDCV;
  160. pAH = pContext->m_pAHexpt[2];
  161. iQDCY = abs(iDCY = pMBInfo->iBlockDC[0][0]);
  162. iQDCU = abs(iDCU = pMBInfo->iBlockDC[1][0]);
  163. iQDCV = abs(iDCV = pMBInfo->iBlockDC[2][0]);
  164. if (iModelBits) {
  165. iQDCY >>= iModelBits;
  166. }
  167. iModelBits = pContext->m_aModelDC.m_iFlcBits[1];
  168. if (iModelBits) {
  169. iQDCU >>= iModelBits;
  170. iQDCV >>= iModelBits;
  171. }
  172. iModelBits = pContext->m_aModelDC.m_iFlcBits[0];
  173. iIndex = (iQDCY != 0) * 4 + (iQDCU != 0) * 2 + (iQDCV != 0);
  174. putBit16z(pIO, pAH->m_pTable[iIndex * 2 + 1], pAH->m_pTable[iIndex * 2 + 2]);
  175. /** send luminance DC **/
  176. if (iQDCY) {
  177. EncodeSignificantAbsLevel((UInt) iQDCY, pContext->m_pAHexpt[3], pIO);
  178. *pLM += 1;
  179. }
  180. putBit16(pIO, abs(iDCY), iModelBits);
  181. if (iDCY) {
  182. putBit16z(pIO, (iDCY < 0), 1);
  183. }
  184. /** send chroma DC **/
  185. pLM = aLaplacianMean + 1;
  186. iModelBits = pContext->m_aModelDC.m_iFlcBits[1];
  187. if (iQDCU) {
  188. EncodeSignificantAbsLevel((UInt) iQDCU, pContext->m_pAHexpt[4], pIO);
  189. *pLM += 1;
  190. }
  191. putBit16(pIO, abs(iDCU), iModelBits);
  192. if (iDCU) {
  193. putBit16z(pIO, (iDCU < 0), 1);
  194. }
  195. if (iQDCV) {
  196. EncodeSignificantAbsLevel((UInt) iQDCV, pContext->m_pAHexpt[4], pIO);
  197. *pLM += 1;
  198. }
  199. putBit16(pIO, abs(iDCV), iModelBits);
  200. if (iDCV) {
  201. putBit16z(pIO, (iDCV < 0), 1);
  202. }
  203. }
  204. UpdateModelMB (cf, iChannels, aLaplacianMean, &(pContext->m_aModelDC));
  205. if (pSC->m_bResetContext && pSC->WMISCP.sbSubband == SB_DC_ONLY) {
  206. AdaptDiscriminant(pContext->m_pAHexpt[2]);
  207. AdaptDiscriminant(pContext->m_pAHexpt[3]);
  208. AdaptDiscriminant(pContext->m_pAHexpt[4]);
  209. }
  210. return ICERR_OK;
  211. }
  212. /*************************************************************************
  213. Scan block with zero model bits
  214. *************************************************************************/
  215. #ifdef X86OPT_INLINE
  216. __forceinline
  217. #endif
  218. static Int AdaptiveScanZero (const PixelI *pCoeffs, CAdaptiveScan *pScan,
  219. Int *pRLCoeffs, const Int iCount)
  220. {
  221. Int k, iRun = 1, iLevel, iNumNonzero = 0;
  222. iLevel = pCoeffs[pScan[1].uScan];
  223. if (iLevel) {
  224. pScan[1].uTotal++;
  225. pRLCoeffs[iNumNonzero * 2] = 0;
  226. pRLCoeffs[iNumNonzero * 2 + 1] = iLevel;
  227. iNumNonzero++;
  228. iRun = 0;
  229. }
  230. for (k = 2; k < iCount; k++) {
  231. iLevel = pCoeffs[pScan[k].uScan];
  232. iRun++;
  233. if (iLevel) {
  234. pScan[k].uTotal++;
  235. if (pScan[k].uTotal > pScan[k - 1].uTotal) {
  236. CAdaptiveScan cTemp = pScan[k];
  237. pScan[k] = pScan[k - 1];
  238. pScan[k - 1] = cTemp;
  239. }
  240. pRLCoeffs[iNumNonzero * 2] = iRun - 1;
  241. pRLCoeffs[iNumNonzero * 2 + 1] = iLevel;
  242. iNumNonzero++;
  243. iRun = 0;
  244. }
  245. }
  246. return iNumNonzero;
  247. }
  248. /*************************************************************************
  249. Scan block with nonzero model bits, all trimmed
  250. *************************************************************************/
  251. #ifdef X86OPT_INLINE
  252. __forceinline
  253. #endif
  254. static Int AdaptiveScanTrim (const PixelI *pCoeffs, CAdaptiveScan *pScan,
  255. const Int iModelBits, Int *pRLCoeffs, const Int iCount)
  256. {
  257. Int k, iRun = 1, iLevel, iNumNonzero = 0;
  258. Int iTemp;
  259. unsigned int iThOff = (1 << iModelBits) - 1, iTh = iThOff * 2 + 1;
  260. iLevel = pCoeffs[pScan[1].uScan];
  261. if ((unsigned int)(iLevel + iThOff) >= iTh) {
  262. iTemp = abs (iLevel) >> iModelBits;
  263. pScan[1].uTotal++;
  264. pRLCoeffs[iNumNonzero * 2] = 0;
  265. pRLCoeffs[iNumNonzero * 2 + 1] = (iLevel < 0) ? -iTemp : iTemp;
  266. iNumNonzero++;
  267. iRun = 0;
  268. }
  269. for (k = 2; k < iCount; k++) {
  270. iRun++;
  271. iLevel = pCoeffs[pScan[k].uScan];
  272. if ((unsigned int)(iLevel + iThOff) >= iTh) {
  273. iTemp = abs (iLevel) >> iModelBits;
  274. pScan[k].uTotal++;
  275. if (pScan[k].uTotal > pScan[k - 1].uTotal) {
  276. CAdaptiveScan cTemp = pScan[k];
  277. pScan[k] = pScan[k - 1];
  278. pScan[k - 1] = cTemp;
  279. }
  280. pRLCoeffs[iNumNonzero * 2] = iRun - 1;
  281. pRLCoeffs[iNumNonzero * 2 + 1] = (iLevel < 0) ? -iTemp : iTemp;
  282. iNumNonzero++;
  283. iRun = 0;
  284. }
  285. }
  286. return iNumNonzero;
  287. }
  288. /*************************************************************************
  289. Scan block with nonzero model bits
  290. *************************************************************************/
  291. /** saves around 1.5% at QP=1 (no SIMD opt) **/
  292. #define USE_GRES_LUT
  293. #ifdef USE_GRES_LUT
  294. static const Int gRes[] = {
  295. 65*2+1, 63*2+1, 61*2+1, 59*2+1, 57*2+1, 55*2+1, 53*2+1, 51*2+1, 49*2+1, 47*2+1, 45*2+1, 43*2+1, 41*2+1,
  296. 39*2+1, 37*2+1, 35*2+1, 33*2+1, 31*2+1, 29*2+1, 27*2+1, 25*2+1, 23*2+1, 21*2+1, 19*2+1, 17*2+1, 15*2+1,
  297. 13*2+1, 11*2+1, 9*2+1, 7*2+1, 5*2+1, 3*2+1,
  298. 0,
  299. 2*2+1, 4*2+1, 6*2+1, 8*2+1, 10*2+1, 12*2+1, 14*2+1, 16*2+1, 18*2+1, 20*2+1, 22*2+1, 24*2+1,
  300. 26*2+1, 28*2+1, 30*2+1, 32*2+1, 34*2+1, 36*2+1, 38*2+1, 40*2+1, 42*2+1, 44*2+1, 46*2+1, 48*2+1, 50*2+1,
  301. 52*2+1, 54*2+1, 56*2+1, 58*2+1, 60*2+1, 62*2+1, 64*2+1 };
  302. #endif // USE_GRES_LUT
  303. #ifdef X86OPT_INLINE
  304. //__forceinline
  305. #endif
  306. static Int AdaptiveScan (const PixelI *pCoeffs, Int *pResidual,
  307. CAdaptiveScan *pScan,
  308. const Int iModelBits, const Int iTrimBits,
  309. Int *pRLCoeffs, const Int iCount)
  310. {
  311. if (iModelBits == 0) {
  312. return AdaptiveScanZero (pCoeffs, pScan, pRLCoeffs, iCount);
  313. }
  314. else if (iModelBits <= iTrimBits) {
  315. return AdaptiveScanTrim (pCoeffs, pScan, iModelBits, pRLCoeffs, iCount);
  316. }
  317. else if (iTrimBits == 0
  318. #ifdef USE_GRES_LUT
  319. && iModelBits < 6
  320. #endif // USE_GRES_LUT
  321. ) {
  322. Int k, iRun = 0, iLevel, iNumNonzero = 0;
  323. Int iTemp, iTemp1;
  324. const unsigned int iThOff = (1 << iModelBits) - 1, iTh = iThOff * 2 + 1;
  325. iLevel = pCoeffs[pScan[1].uScan];
  326. if ((unsigned int)(iLevel + iThOff) >= iTh) {
  327. iTemp1 = abs (iLevel);
  328. iTemp = iTemp1 >> iModelBits;
  329. pResidual[pScan[1].uScan] = (iTemp1 & iThOff) * 2;
  330. pScan[1].uTotal++;
  331. pRLCoeffs[iNumNonzero * 2] = iRun;
  332. pRLCoeffs[iNumNonzero * 2 + 1] = (iLevel < 0) ? -iTemp : iTemp;
  333. iNumNonzero++;
  334. iRun = 0;
  335. }
  336. else {
  337. iRun++;
  338. #ifdef USE_GRES_LUT
  339. pResidual[pScan[1].uScan] = gRes[(iLevel + 32)];
  340. #else // USE_GRES_LUT
  341. iTemp = -(iLevel < 0);
  342. pResidual[pScan[1].uScan] = (iLevel ^ iTemp) * 4 + (6 & iTemp) + (iLevel != 0);
  343. #endif // USE_GRES_LUT
  344. }
  345. for (k = 2; k < iCount; k++) {
  346. const Int sk = pScan[k].uScan;
  347. //pResidual++;
  348. iLevel = pCoeffs[sk];
  349. if ((unsigned int)(iLevel + iThOff) >= iTh) {
  350. const Int iSign = -(iLevel < 0);
  351. iTemp1 = (iSign ^ iLevel) - iSign;
  352. iTemp = iTemp1 >> iModelBits;
  353. pResidual[sk] = (iTemp1 & iThOff) * 2;
  354. pScan[k].uTotal++;
  355. if (pScan[k].uTotal > pScan[k - 1].uTotal) {
  356. CAdaptiveScan cTemp = pScan[k];
  357. pScan[k] = pScan[k - 1];
  358. pScan[k - 1] = cTemp;
  359. }
  360. pRLCoeffs[iNumNonzero * 2] = iRun;
  361. pRLCoeffs[iNumNonzero * 2 + 1] = (iTemp ^ iSign) - iSign;
  362. iNumNonzero++;
  363. iRun = 0;
  364. }
  365. else {
  366. iRun++;
  367. #ifdef USE_GRES_LUT
  368. pResidual[sk] = gRes[(iLevel + 32)];
  369. #else // USE_GRES_LUT
  370. iTemp = -(iLevel < 0);
  371. pResidual[sk] = (iLevel ^ iTemp) * 4 + (6 & iTemp) + (iLevel != 0);
  372. #endif // USE_GRES_LUT
  373. ////(abs(iLevel) * 4) + ((iLevel < 0) * 2) + (iLevel != 0);
  374. }
  375. }
  376. return iNumNonzero;
  377. }
  378. else {
  379. Int k, iRun = 0, iLevel, iNumNonzero = 0;
  380. Int iTemp, iTemp1;
  381. const unsigned int iThOff = (1 << iModelBits) - 1, iTh = iThOff * 2 + 1;
  382. iLevel = pCoeffs[pScan[1].uScan];
  383. //pResidual++;
  384. if ((unsigned int)(iLevel + iThOff) >= iTh) {
  385. iTemp1 = abs (iLevel);
  386. iTemp = iTemp1 >> iModelBits;
  387. pResidual[pScan[1].uScan] = ((iTemp1 & iThOff) >> iTrimBits) * 2;
  388. pScan[1].uTotal++;
  389. pRLCoeffs[iNumNonzero * 2] = iRun;
  390. pRLCoeffs[iNumNonzero * 2 + 1] = (iLevel < 0) ? -iTemp : iTemp;
  391. iNumNonzero++;
  392. iRun = 0;
  393. }
  394. else {
  395. iRun++;
  396. iTemp = -(iLevel < 0);
  397. iLevel = ((iLevel + iTemp) >> iTrimBits) - iTemp; // round towards zero
  398. iTemp = -(iLevel < 0);
  399. pResidual[pScan[1].uScan] = (iLevel ^ iTemp) * 4 + (6 & iTemp) + (iLevel != 0);
  400. }
  401. for (k = 2; k < iCount; k++) {
  402. const Int sk = pScan[k].uScan;
  403. //pResidual++;
  404. iLevel = pCoeffs[sk];
  405. if ((unsigned int)(iLevel + iThOff) >= iTh) {
  406. iTemp1 = abs (iLevel);
  407. iTemp = iTemp1 >> iModelBits;
  408. pResidual[sk] = ((iTemp1 & iThOff) >> iTrimBits) * 2;
  409. pScan[k].uTotal++;
  410. if (pScan[k].uTotal > pScan[k - 1].uTotal) {
  411. CAdaptiveScan cTemp = pScan[k];
  412. pScan[k] = pScan[k - 1];
  413. pScan[k - 1] = cTemp;
  414. }
  415. pRLCoeffs[iNumNonzero * 2] = iRun;
  416. pRLCoeffs[iNumNonzero * 2 + 1] = (iLevel < 0) ? -iTemp : iTemp;
  417. iNumNonzero++;
  418. iRun = 0;
  419. }
  420. else {
  421. iRun++;
  422. iTemp = -(iLevel < 0);
  423. iLevel = ((iLevel + iTemp) >> iTrimBits) - iTemp; // round towards zero
  424. iTemp = -(iLevel < 0);
  425. pResidual[sk] = (iLevel ^ iTemp) * 4 + (6 & iTemp) + (iLevel != 0);
  426. }
  427. }
  428. return iNumNonzero;
  429. }
  430. }
  431. /*************************************************************************
  432. EncodeMacroblockLowpass
  433. *************************************************************************/
  434. Int EncodeMacroblockLowpass (CWMImageStrCodec *pSC, CCodingContext *pContext, Int iMBX, Int iMBY)
  435. {
  436. const COLORFORMAT cf = pSC->m_param.cfColorFormat;
  437. const Int iChannels = (Int) pSC->m_param.cNumChannels;
  438. Int iFullChannels = (cf == YUV_420 || cf == YUV_422) ? 1 : iChannels;
  439. CWMIMBInfo *pMBInfo = &pSC->MBInfo;
  440. BitIOInfo* pIO = pContext->m_pIOLP;
  441. CAdaptiveScan *pScan = pContext->m_aScanLowpass;
  442. Int k, /*iPrevRun = -1,*/ iRun = 0;// iLastIndex = 0;
  443. Int iModelBits = pContext->m_aModelLP.m_iFlcBits[0];
  444. PixelI aBuf[2][8];
  445. Int aLaplacianMean[2] = {0, 0}, *pLM = aLaplacianMean;
  446. Int iChannel, iVal;
  447. Int aRLCoeffs[MAX_CHANNELS][32], iNumCoeffs[MAX_CHANNELS];
  448. const I32 *aDC[MAX_CHANNELS];
  449. Int aResidual[MAX_CHANNELS][16];
  450. Void (*putBits)(BitIOInfo* pIO, U32 uiBits, U32 cBits) = putBit16;
  451. UNREFERENCED_PARAMETER( iMBX );
  452. UNREFERENCED_PARAMETER( iMBY );
  453. if (iChannels > MAX_CHANNELS)
  454. return ICERR_ERROR;
  455. if((pSC->WMISCP.bfBitstreamFormat != SPATIAL) && (pSC->pTile[pSC->cTileColumn].cBitsLP > 0)) // MB-based LP QP index
  456. encodeQPIndex(pIO, pMBInfo->iQIndexLP, pSC->pTile[pSC->cTileColumn].cBitsLP);
  457. // set arrays
  458. for (k = 0; k < iChannels; k++) {
  459. aDC[k] = pMBInfo->iBlockDC[k];
  460. }
  461. /** reset adaptive scan totals **/
  462. if (pSC->m_bResetRGITotals) {
  463. int iScale = 2;
  464. int iWeight = iScale * 16;
  465. pScan[0].uTotal = MAXTOTAL;
  466. for (k = 1; k < 16; k++) {
  467. pScan[k].uTotal = iWeight;
  468. iWeight -= iScale;
  469. }
  470. }
  471. /** scan 4x4 transform **/
  472. for (iChannel = 0; iChannel < iFullChannels; iChannel++) {
  473. iNumCoeffs[iChannel] = AdaptiveScan (aDC[iChannel], aResidual[iChannel],
  474. pScan, iModelBits, 0, aRLCoeffs[iChannel], 16);
  475. iModelBits = pContext->m_aModelLP.m_iFlcBits[1];
  476. }
  477. if (cf == YUV_420 || cf == YUV_422) { /** interleave U and V **/
  478. static const Int aRemap[] = { 4, 1,2,3, 5,6,7 };
  479. const Int *pRemap = aRemap + (cf == YUV_420);
  480. const Int iCount = (cf == YUV_420) ? 6 : 14;
  481. Int iCoef = 0;
  482. iRun = 0;
  483. iModelBits = pContext->m_aModelLP.m_iFlcBits[1];
  484. for (k = 0; k < iCount; k++) {
  485. Int iIndex = pRemap[k >> 1];
  486. Int iDC = aDC[(k & 1) + 1][iIndex];
  487. aBuf[k & 1][iIndex] = iVal = abs (iDC) >> iModelBits;
  488. if (iVal) {
  489. aRLCoeffs[1][iCoef * 2] = iRun;
  490. aRLCoeffs[1][iCoef * 2 + 1] = (iDC < 0) ? -iVal : iVal;
  491. iCoef++;
  492. iRun = 0;
  493. }
  494. else {
  495. iRun++;
  496. }
  497. }
  498. iNumCoeffs[1] = iCoef;
  499. }
  500. /** in raw mode, this can take 6% of the bits in the extreme low rate case!!! **/
  501. if (cf == YUV_420 || cf == YUV_422)
  502. iFullChannels = 2;
  503. if (cf == YUV_420 || cf == YUV_422 || cf == YUV_444) {
  504. int iCBP, iMax = iFullChannels * 4 - 5; /* actually (1 << iNChannels) - 1 **/
  505. int iCountM = pContext->m_iCBPCountMax, iCountZ = pContext->m_iCBPCountZero;
  506. iCBP = (iNumCoeffs[0] > 0) + (iNumCoeffs[1] > 0) * 2;
  507. if (iFullChannels == 3)
  508. iCBP += (iNumCoeffs[2] > 0) * 4;
  509. if (iCountZ <= 0 || iCountM < 0) {
  510. iVal = iCBP;
  511. if (iCountM < iCountZ) {
  512. iVal = iMax - iCBP;
  513. }
  514. if (iVal == 0)
  515. putBit16z(pIO, 0, 1);
  516. else if (iVal == 1)
  517. putBit16z(pIO, (iFullChannels + 1) & 0x6, iFullChannels); // 2 or 4
  518. else
  519. putBit16z(pIO, iVal + iMax + 1, iFullChannels + 1); // cbp + 4 or cbp + 8
  520. }
  521. else {
  522. putBit16z(pIO, iCBP, iFullChannels);
  523. }
  524. iCountM += 1 - 4 * (iCBP == iMax);//(b + c - 2*a);
  525. iCountZ += 1 - 4 * (iCBP == 0);//(a + b - 2*c);
  526. if (iCountM < -8)
  527. iCountM = -8;
  528. else if (iCountM > 7)
  529. iCountM = 7;
  530. pContext->m_iCBPCountMax = iCountM;
  531. if (iCountZ < -8)
  532. iCountZ = -8;
  533. else if (iCountZ > 7)
  534. iCountZ = 7;
  535. pContext->m_iCBPCountZero = iCountZ;
  536. }
  537. else { /** 1 or N channel **/
  538. for (iChannel = 0; iChannel < iChannels; iChannel++) {
  539. putBit16z(pIO, (iNumCoeffs[iChannel] > 0), 1);
  540. }
  541. }
  542. // set appropriate function pointer
  543. if (pContext->m_aModelLP.m_iFlcBits[0] > 14 || pContext->m_aModelLP.m_iFlcBits[1] > 14) {
  544. putBits = putBit32;
  545. }
  546. iModelBits = pContext->m_aModelLP.m_iFlcBits[0];
  547. for (iChannel = 0; iChannel < iFullChannels; iChannel++) {
  548. const Int *pRL = aRLCoeffs[iChannel];
  549. Int iCoef = iNumCoeffs[iChannel];
  550. if (iCoef) {
  551. (*pLM) += iCoef;
  552. if(EncodeBlock (iChannel > 0, pRL, iCoef, pContext->m_pAHexpt, CTDC,
  553. pIO, 1 + 9 * ((cf == YUV_420) && (iChannel == 1)) + ((cf == YUV_422) && (iChannel == 1))) != ICERR_OK)
  554. return ICERR_ERROR;
  555. }
  556. if (iModelBits) {
  557. if ((cf == YUV_420 || cf == YUV_422) && iChannel) { // 420/422 chroma
  558. for (k = 1; k < ((cf == YUV_420) ? 4 : 8); k++) {
  559. putBits(pIO, abs(aDC[1][k]), iModelBits);
  560. if (aBuf[0][k] == 0 && aDC[1][k]) {
  561. putBit16z(pIO, (aDC[1][k] < 0), 1);
  562. }
  563. putBits(pIO, abs(aDC[2][k]), iModelBits);
  564. if (aBuf[1][k] == 0 && aDC[2][k]) {
  565. putBit16z(pIO, (aDC[2][k] < 0), 1);
  566. }
  567. }
  568. }
  569. else { // normal case
  570. for (k = 1; k < 16; k++) {
  571. putBit16z(pIO, aResidual[iChannel][k] >> 1, iModelBits + (aResidual[iChannel][k] & 1));
  572. }
  573. }
  574. }
  575. pLM = aLaplacianMean + 1;
  576. iModelBits = pContext->m_aModelLP.m_iFlcBits[1];
  577. }
  578. writeIS_L1(pSC, pIO);
  579. UpdateModelMB (cf, iChannels, aLaplacianMean, &pContext->m_aModelLP);
  580. if (pSC->m_bResetContext) {
  581. AdaptLowpassEnc(pContext);
  582. }
  583. return ICERR_OK;
  584. }
  585. /*************************************************************************
  586. Adapt
  587. *************************************************************************/
  588. Void AdaptLowpassEnc(CCodingContext *pSC)
  589. {
  590. Int kk;
  591. for (kk = 0; kk < CONTEXTX + CTDC; kk++) { /** adapt fixed code (index 0 and 1) as well **/
  592. AdaptDiscriminant (pSC->m_pAHexpt[kk]);
  593. }
  594. }
  595. Void AdaptHighpassEnc(CCodingContext *pSC)
  596. {
  597. Int kk;
  598. //Adapt (pSC->m_pAdaptHuffCBPCY, FALSE);
  599. AdaptDiscriminant (pSC->m_pAdaptHuffCBPCY);
  600. AdaptDiscriminant (pSC->m_pAdaptHuffCBPCY1);
  601. for (kk = 0; kk < CONTEXTX; kk++) { /** adapt fixed code **/
  602. AdaptDiscriminant (pSC->m_pAHexpt[kk + CONTEXTX + CTDC]);
  603. }
  604. }
  605. /*************************************************************************
  606. Experimental code -- encodeBlock
  607. SR = <0 1 2> == <last, nonsignificant, significant run>
  608. alphabet 12:
  609. pAHexpt[0] == <SR', SL, SR | first symbol>
  610. alphabet 6:
  611. pAHexpt[1] == <SR', SL | continuous>
  612. pAHexpt[2] == <SR', SL | continuous>
  613. alphabet 4:
  614. pAHexpt[3] == <SR', SL | 1 free slot> (SR may be last or insignificant only)
  615. alphabet f(run) (this can be extended to 6 contexts - SL and SR')
  616. pAHexpt[4] == <run | continuous>
  617. alphabet f(lev) (this can be extended to 9 contexts)
  618. pAHexpt[5-6] == <lev | continuous> first symbol
  619. pAHexpt[7-8] == <lev | continuous> condition on SRn no use
  620. *************************************************************************/
  621. #ifdef X86OPT_INLINE
  622. __forceinline
  623. #endif
  624. static Void EncodeSignificantRun (Int iRun, Int iMaxRun, struct CAdaptiveHuffman *pAHexpt, BitIOInfo* pOut)
  625. {
  626. Int iIndex, iFLC, iBin;
  627. static const Int aIndex[] = {
  628. 0,1,2,2,3,3,4,4,4,4,4,4,4,4,
  629. 0,1,2,2,3,3,4,4,4,4,0,0,0,0,
  630. 0,1,2,3,4,4
  631. };
  632. if (iMaxRun < 5) {
  633. //if (iMaxRun == 4) {
  634. //static const Int gCode[] = { 0, 1, 1, 1 };
  635. static const Int gLen[] = { 3, 3, 2, 1 };
  636. if (iMaxRun > 1)
  637. putBit16z(pOut, (iMaxRun != iRun), gLen[iMaxRun - iRun] - (4 - iMaxRun));
  638. //}
  639. //else if (iMaxRun == 3) {
  640. // if (iRun == 1) {
  641. // putBit16z(pOut, 1, 1);
  642. // }
  643. // else {
  644. // putBit16z(pOut, 3 ^ iRun, 2);
  645. // }
  646. //}
  647. //else if (iMaxRun == 2) {
  648. // putBit16z(pOut, 2 - iRun, 1);
  649. //}
  650. return;
  651. }
  652. iBin = gSignificantRunBin[iMaxRun];
  653. iIndex = aIndex[iRun + iBin * 14 - 1];
  654. iFLC = gSignificantRunFixedLength[iIndex + iBin * 5];
  655. putBit16z(pOut, pAHexpt->m_pTable[iIndex * 2 + 1], pAHexpt->m_pTable[iIndex * 2 + 2]);
  656. //this always uses table 0
  657. //pAHexpt->m_iDiscriminant += pAHexpt->m_pDelta[iIndex];
  658. putBit16(pOut, iRun + 1, iFLC);
  659. }
  660. #ifdef X86OPT_INLINE
  661. __forceinline
  662. #endif
  663. static Void EncodeFirstIndex (Bool bChroma, Int iLoc, Int iCont, Int iIndex, Int iSign,
  664. struct CAdaptiveHuffman **ppAHexpt, BitIOInfo* pOut)
  665. {
  666. // Int iContext = iCont + 1 + bChroma * 3;
  667. struct CAdaptiveHuffman *pAHexpt = ppAHexpt[bChroma * 3];
  668. UNREFERENCED_PARAMETER( iLoc );
  669. UNREFERENCED_PARAMETER( iCont );
  670. pAHexpt->m_iDiscriminant += pAHexpt->m_pDelta[iIndex];
  671. pAHexpt->m_iDiscriminant1 += pAHexpt->m_pDelta1[iIndex];
  672. putBit16z(pOut, pAHexpt->m_pTable[iIndex * 2 + 1] * 2 + iSign, pAHexpt->m_pTable[iIndex * 2 + 2] + 1);
  673. return;
  674. }
  675. #ifdef X86OPT_INLINE
  676. __forceinline
  677. #endif
  678. static Void EncodeIndex (Bool bChroma, Int iLoc, Int iCont, Int iIndex, Int iSign,
  679. struct CAdaptiveHuffman **ppAHexpt, BitIOInfo* pOut)
  680. {
  681. Int iContext = iCont + 1 + bChroma * 3;
  682. if (iLoc < 15) {
  683. struct CAdaptiveHuffman *pAHexpt = ppAHexpt[iContext];
  684. pAHexpt->m_iDiscriminant += pAHexpt->m_pDelta[iIndex];
  685. pAHexpt->m_iDiscriminant1 += pAHexpt->m_pDelta1[iIndex];
  686. putBit16z(pOut, pAHexpt->m_pTable[iIndex * 2 + 1] * 2 + iSign, pAHexpt->m_pTable[iIndex * 2 + 2] + 1);
  687. }
  688. else if (iLoc == 15) {
  689. static const U32 gCode[] = { 0, 6, 2, 7 };
  690. static const U32 gLen[] = { 1, 3, 2, 3 };
  691. putBit16z(pOut, gCode[iIndex] * 2 + iSign, gLen[iIndex] + 1);
  692. return;
  693. }
  694. else {//if (iLoc == 16) {
  695. putBit16z(pOut, iIndex * 2 + iSign, 1 + 1);
  696. return;
  697. }
  698. }
  699. #ifdef X86OPT_INLINE
  700. __forceinline
  701. #endif
  702. static Int EncodeBlock (Bool bChroma, const Int *aLocalCoef, Int iNumNonzero,
  703. struct CAdaptiveHuffman **pAHexpt, Int iContextOffset,
  704. BitIOInfo* pOut, UInt iLocation)
  705. {
  706. Int iSR, iSL, iSRn, iIndex, k, iCont, iLev;
  707. /** first symbol **/
  708. iLev = aLocalCoef[1];
  709. iSR = (aLocalCoef[0] == 0);
  710. iSL = ((unsigned int) (iLev + 1) > 2U);
  711. iSRn = 1;
  712. if (iNumNonzero == 1) {
  713. iSRn = 0;
  714. }
  715. else if (aLocalCoef[2] > 0) {
  716. iSRn = 2;
  717. }
  718. iIndex = iSRn * 4 + iSL * 2 + iSR;
  719. EncodeFirstIndex (bChroma, iLocation, 0, iIndex, (iLev < 0), pAHexpt + iContextOffset, pOut);
  720. iCont = iSR & iSRn;
  721. if (iSL) {
  722. EncodeSignificantAbsLevel ((UInt)(abs(iLev) - 1), pAHexpt[6 + iContextOffset + iCont], pOut);
  723. }
  724. if (iSR == 0) {
  725. EncodeSignificantRun (aLocalCoef[0], 15 - iLocation, pAHexpt[0], pOut);
  726. }
  727. iLocation += aLocalCoef[0] + 1;
  728. for (k = 1; k < iNumNonzero; k++) {
  729. if (iSRn == 2) {
  730. EncodeSignificantRun (aLocalCoef[k * 2], 15 - iLocation, pAHexpt[0], pOut);
  731. }
  732. iLocation += aLocalCoef[k * 2] + 1;
  733. iSRn = 1;
  734. if (k == iNumNonzero - 1) {
  735. iSRn = 0;
  736. }
  737. else if (aLocalCoef[k * 2 + 2] > 0) {
  738. iSRn = 2;
  739. }
  740. //iSL = (abs(aLocalCoef[k * 2 + 1]) > 1);
  741. iLev = aLocalCoef[k * 2 + 1];
  742. iSL = ((unsigned int) (iLev + 1) > 2U);
  743. iIndex = iSRn * 2 + iSL;
  744. EncodeIndex (bChroma, iLocation, iCont, iIndex, (iLev < 0), pAHexpt + iContextOffset, pOut);
  745. iCont &= iSRn; /** big difference! **/
  746. if (iSL) {
  747. EncodeSignificantAbsLevel ((UInt)(abs(iLev) - 1), pAHexpt[6 + iContextOffset + iCont], pOut);
  748. }
  749. //else {
  750. // putBit16z(pOut, (iLev < 0), 1);
  751. //}
  752. }
  753. return ICERR_OK;
  754. }
  755. /*************************************************************************
  756. CodeCoeffs
  757. *************************************************************************/
  758. #ifdef X86OPT_INLINE
  759. __forceinline
  760. #endif
  761. static Int CodeCoeffs (CWMImageStrCodec * pSC, CCodingContext *pContext,
  762. Int iMBX, Int iMBY, BitIOInfo* pIO, BitIOInfo* pIOFL)
  763. {
  764. const COLORFORMAT cf = pSC->m_param.cfColorFormat;
  765. const Int iChannels = (Int) pSC->m_param.cNumChannels;
  766. const Int iPlanes = (cf == YUV_420 || cf == YUV_422) ? 1 : iChannels;
  767. CWMIMBInfo * pMBInfo = &pSC->MBInfo;
  768. CAdaptiveScan *pScan;
  769. Int iBlock, iNBlocks = 4;
  770. Int iSubblock, iIndex = 0;
  771. Int i, k;
  772. const Int iNumCoeffs = 16;
  773. Int iModelBits = pContext->m_aModelAC.m_iFlcBits[0], iFlex = 0, iTrim = 0, iMask = 0;
  774. Int aLaplacianMean[2] = { 0, 0}, *pLM = aLaplacianMean;
  775. Bool bChroma = FALSE;
  776. UNREFERENCED_PARAMETER( iMBX );
  777. UNREFERENCED_PARAMETER( iMBY );
  778. assert (iModelBits < 16);
  779. if (pContext->m_iTrimFlexBits <= iModelBits && pSC->WMISCP.sbSubband != SB_NO_FLEXBITS) {
  780. iTrim = pContext->m_iTrimFlexBits;
  781. iFlex = iModelBits - pContext->m_iTrimFlexBits;
  782. iMask = (1 << iFlex) - 1;
  783. }
  784. if(pSC->WMISCP.sbSubband != SB_NO_FLEXBITS)
  785. writeIS_L1(pSC, pIOFL);
  786. /** set scan arrays **/
  787. if (pMBInfo->iOrientation == 1) {
  788. pScan = pContext->m_aScanVert;
  789. }
  790. else {
  791. pScan = pContext->m_aScanHoriz;
  792. }
  793. /** write out coefficients **/
  794. for (i = 0; i < iPlanes; i++) {
  795. Int iPattern = pMBInfo->iCBP[i];
  796. if (cf == YUV_420) {
  797. iNBlocks = 6;
  798. iPattern += (pMBInfo->iCBP[1] << 16) + (pMBInfo->iCBP[2] << 20);
  799. }
  800. else if (cf == YUV_422) {
  801. iNBlocks = 8;
  802. iPattern += (pMBInfo->iCBP[1] << 16) + (pMBInfo->iCBP[2] << 24);
  803. }
  804. for (iBlock = iIndex = 0; iBlock < iNBlocks; iBlock++) {
  805. writeIS_L2(pSC, pIO);
  806. if (pIO != pIOFL)
  807. writeIS_L2(pSC, pIOFL);
  808. for (iSubblock = 0; iSubblock < 4; iSubblock++, iPattern >>= 1, iIndex ++) {
  809. const PixelI *pCoeffs = NULL;
  810. if(iBlock < 4){
  811. pCoeffs = pSC->pPlane[i] + blkOffset[iIndex];
  812. }
  813. else if(cf == YUV_420){
  814. pCoeffs = pSC->pPlane[iBlock - 3] + blkOffsetUV[iSubblock];
  815. }
  816. else if(cf == YUV_422){
  817. pCoeffs = pSC->pPlane[1 + ((iBlock - 4) >> 1)] + blkOffsetUV_422[(iBlock & 1) * 4 + iSubblock];
  818. }
  819. /** put AC bits **/
  820. if ((iPattern & 1) == 0) {
  821. if (iFlex) {
  822. /** FLC only, all else is skipped **/
  823. for (k = 1; k < iNumCoeffs; k++) {
  824. Int data = pCoeffs[dctIndex[0][k]];
  825. Int atdata = (abs(data) >> iTrim);
  826. Int word = atdata & iMask, len = iFlex;
  827. if (atdata) {
  828. word += word + (data < 0);
  829. len++;
  830. }
  831. putBit16z(pIOFL, word, len);
  832. }
  833. }
  834. }
  835. else {
  836. // WARNING!!! interaction between lowpass coefficients and highpass scan ordering - may lead to break in decoding when model bits is nonzero!
  837. // Fix is to use same scan order in model bits transmission, and defer update of scan order to end of block
  838. /** collect coefficients **/
  839. Int aLocalCoef[32], iNumNonzero = 0;
  840. Int aResidual[16];
  841. iNumNonzero = AdaptiveScan (pCoeffs, aResidual,
  842. pScan, iModelBits, iTrim, aLocalCoef, 16);
  843. (*pLM) += iNumNonzero;
  844. EncodeBlock (bChroma, aLocalCoef, iNumNonzero, pContext->m_pAHexpt, CTDC + CONTEXTX, pIO, 1);
  845. if (iFlex) {
  846. for (k = 1; k < iNumCoeffs; k++) {
  847. putBit16z(pIOFL, aResidual[dctIndex[0][k]] >> 1, iFlex + (aResidual[dctIndex[0][k]] & 1));
  848. }
  849. }
  850. }
  851. }
  852. if (iBlock == 3) {
  853. iModelBits = pContext->m_aModelAC.m_iFlcBits[1];
  854. assert (iModelBits < 16);
  855. pLM = aLaplacianMean + 1;
  856. bChroma = TRUE;
  857. iTrim = iFlex = iMask = 0;
  858. if (pContext->m_iTrimFlexBits <= iModelBits && pSC->WMISCP.sbSubband != SB_NO_FLEXBITS) {
  859. iTrim = pContext->m_iTrimFlexBits;
  860. iFlex = iModelBits - iTrim;
  861. iMask = (1 << iFlex) - 1;
  862. }
  863. }
  864. }
  865. }
  866. /** update model at end of MB **/
  867. UpdateModelMB (cf, iChannels, aLaplacianMean, &pContext->m_aModelAC);
  868. return ICERR_OK;
  869. }
  870. /*************************************************************************
  871. CodeCBP
  872. *************************************************************************/
  873. static Void CodeCBP (CWMImageStrCodec * pSC, CCodingContext *pContext,
  874. Int iMBX, Int iMBY, BitIOInfo *pIO)
  875. {
  876. const COLORFORMAT cf = pSC->m_param.cfColorFormat;
  877. const Int iChannel = (cf == NCOMPONENT || cf == CMYK) ? (Int) pSC->m_param.cNumChannels : 1;
  878. Int iDiffCBPCY, iDiffCBPCU = 0, iDiffCBPCV = 0, iDY;
  879. Int iBlock, i, k;
  880. static const Int aNumOnes[] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4 };
  881. static const Int aTabLen[] = { 0, 2, 2, 2, 2, 2, 3, 2, 2, 3, 3, 2, 3, 2, 2, 0 };
  882. static const Int aTabCode[] = { 0, 0, 1, 0, 2, 1, 4, 3, 3, 5, 6, 2, 7, 1, 0, 0 };
  883. CAdaptiveHuffman *pAH;
  884. Int iCount, iPattern, iCode, iCodeU = 0, iCodeV = 0;
  885. UNREFERENCED_PARAMETER( iMBX );
  886. UNREFERENCED_PARAMETER( iMBY );
  887. predCBPEnc(pSC, pContext);
  888. writeIS_L1(pSC, pIO);
  889. iDiffCBPCU = pSC->MBInfo.iDiffCBP[1];
  890. iDiffCBPCV = pSC->MBInfo.iDiffCBP[2];
  891. for (i = 0; i < iChannel; i++) {
  892. iDiffCBPCY = pSC->MBInfo.iDiffCBP[i];
  893. if(cf == YUV_420){ // PackCBP420
  894. iDiffCBPCY = (iDiffCBPCY & 0xf) + ((iDiffCBPCU & 1) << 4) + ((iDiffCBPCV & 1) << 5) +
  895. ((iDiffCBPCY & 0x00f0) << 2) + ((iDiffCBPCU & 2) << 9) + ((iDiffCBPCV & 2) << 10) +
  896. ((iDiffCBPCY & 0x0f00) << 4) + ((iDiffCBPCU & 4) << 14) + ((iDiffCBPCV & 4) << 15) +
  897. ((iDiffCBPCY & 0xf000) << 6) + ((iDiffCBPCU & 8) << 19) + ((iDiffCBPCV & 8) << 20);
  898. }
  899. else if(cf == YUV_422){// PackCBP422
  900. iDiffCBPCY = (iDiffCBPCY & 0xf) + ((iDiffCBPCU & 1) << 4) + ((iDiffCBPCU & 4) << 3) +
  901. ((iDiffCBPCV & 1) << 6) + ((iDiffCBPCV & 4) << 5) +
  902. ((iDiffCBPCY & 0x00f0) << 4) + ((iDiffCBPCU & 2) << 11) + ((iDiffCBPCU & 8) << 10) +
  903. ((iDiffCBPCV & 2) << 13) + ((iDiffCBPCV & 8) << 12) +
  904. ((iDiffCBPCY & 0x0f00) << 8) + ((iDiffCBPCU & 16) << 16) + ((iDiffCBPCU & 64) << 15) +
  905. ((iDiffCBPCV & 16) << 18) + ((iDiffCBPCV & 64) << 17) +
  906. ((iDiffCBPCY & 0xf000) << 12) + ((iDiffCBPCU & 32) << 23) + ((iDiffCBPCU & 128) << 22) +
  907. ((iDiffCBPCV & 32) << 25) + ((iDiffCBPCV & 128) << 24);
  908. }
  909. /** send CBPCY **/
  910. iPattern = 0;
  911. iDY = iDiffCBPCY;
  912. if (cf == YUV_444) {
  913. iDY |= (iDiffCBPCU | iDiffCBPCV);
  914. }
  915. for (iBlock = 0; iBlock < 4; iBlock++) {
  916. if(cf == YUV_422) {
  917. iPattern |= ((iDY & 0xff) != 0) * 0x10;
  918. iDY >>= 8;
  919. }
  920. else if (cf == YUV_420) {
  921. iPattern |= ((iDY & 0x3f) != 0) * 0x10;
  922. iDY >>= 6;
  923. }
  924. else {
  925. iPattern |= ((iDY & 0xf) != 0) * 0x10;
  926. iDY >>= 4;
  927. }
  928. iPattern >>= 1;
  929. }
  930. pAH = pContext->m_pAdaptHuffCBPCY1;
  931. iCount = aNumOnes[iPattern];
  932. putBit16z(pIO, pAH->m_pTable[iCount * 2 + 1], pAH->m_pTable[iCount * 2 + 2]);
  933. pAH->m_iDiscriminant += pAH->m_pDelta[iCount];
  934. if (aTabLen[iPattern]) {
  935. putBit16z(pIO, aTabCode[iPattern], aTabLen[iPattern]);
  936. }
  937. for (iBlock = 0; iBlock < 4; iBlock++) {
  938. switch (cf) {
  939. case YUV_444:
  940. iCode = iDiffCBPCY & 0xf;
  941. iCodeU = iDiffCBPCU & 0xf;
  942. iCodeV = iDiffCBPCV & 0xf;
  943. iCode |= ((iCodeU != 0) << 4);
  944. iCode |= ((iCodeV != 0) << 5);
  945. iDiffCBPCY >>= 4;
  946. iDiffCBPCU >>= 4;
  947. iDiffCBPCV >>= 4;
  948. break;
  949. case YUV_422:
  950. iCode = iDiffCBPCY & 0xff;
  951. iDiffCBPCY >>= 8;
  952. break;
  953. case YUV_420:
  954. iCode = iDiffCBPCY & 0x3f;
  955. iDiffCBPCY >>= 6;
  956. break;
  957. default:
  958. iCode = iDiffCBPCY & 0xf;
  959. iDiffCBPCY >>= 4;
  960. }
  961. if (iCode) {
  962. static const Int gTab0[16] = { 0,1,1,2, 1,3,3,4, 1,3,3,4, 2,4,4,5 };
  963. static const Int gFL0[16] = { 0,2,2,1, 2,2,2,2, 2,2,2,2, 1,2,2,0 };
  964. static const Int gCode0[16] = { 0,0,1,0, 2,0,1,0, 3,2,3,1, 1,2,3,0 };
  965. int val, iChroma = (iCode >> 4);
  966. iCode &= 0xf;
  967. if(cf == YUV_422) {
  968. iCodeU = (iChroma & 3);
  969. iCodeV = ((iChroma >> 2) & 3);
  970. iChroma = (iCodeU == 0 ? 0 : 1);
  971. if(iCodeV != 0) {
  972. iChroma += 2;
  973. }
  974. }
  975. if (iChroma) {
  976. if (gTab0[iCode] > 2) {
  977. val = 8;
  978. }
  979. else {
  980. val = gTab0[iCode] + 6 - 1;
  981. }
  982. }
  983. else {
  984. val = gTab0[iCode] - 1;
  985. }
  986. pAH = pContext->m_pAdaptHuffCBPCY;
  987. putBit16z(pIO, pAH->m_pTable[val * 2 + 1], pAH->m_pTable[val * 2 + 2]);
  988. pAH->m_iDiscriminant += pAH->m_pDelta[val];
  989. if (iChroma) {
  990. if (iChroma == 1)
  991. putBit16z(pIO, 1, 1);
  992. else
  993. putBit16z(pIO, 3 - iChroma, 2);
  994. }
  995. if (val == 8) {
  996. if (gTab0[iCode] == 3) {
  997. putBit16z(pIO, 1, 1);
  998. }
  999. else {
  1000. putBit16z(pIO, 5 - gTab0[iCode], 2);
  1001. }
  1002. }
  1003. if (gFL0[iCode]) {
  1004. putBit16z(pIO, gCode0[iCode], gFL0[iCode]);
  1005. }
  1006. if (cf == YUV_444) {
  1007. pAH = pContext->m_pAHexpt[1];
  1008. iPattern = iCodeU;
  1009. for (k = 0; k < 2; k++) {
  1010. if (iPattern) {
  1011. iCount = aNumOnes[iPattern];
  1012. iCount--;
  1013. putBit16z(pIO, pAH->m_pTable[iCount * 2 + 1], pAH->m_pTable[iCount * 2 + 2]);
  1014. if (aTabLen[iPattern]) {
  1015. putBit16z(pIO, aTabCode[iPattern], aTabLen[iPattern]);
  1016. }
  1017. }
  1018. iPattern = iCodeV;
  1019. }
  1020. }
  1021. else if (cf == YUV_422){
  1022. iPattern = iCodeU;
  1023. for(k = 0; k < 2; k ++) {
  1024. if(iPattern) {
  1025. if (iPattern == 1)
  1026. putBit16z(pIO, 1, 1);
  1027. else {
  1028. putBit16z(pIO, 3 - iPattern, 2);
  1029. }
  1030. }
  1031. iPattern = iCodeV;
  1032. }
  1033. }
  1034. }
  1035. }
  1036. }
  1037. }
  1038. /*************************************************************************
  1039. macroblock encode function using 4x4 transforms
  1040. *************************************************************************/
  1041. Int EncodeMacroblockHighpass(CWMImageStrCodec * pSC, CCodingContext *pContext, Int iMBX, Int iMBY)
  1042. {
  1043. BitIOInfo* pIO = pContext->m_pIOAC;
  1044. BitIOInfo* pIOFL = pContext->m_pIOFL;
  1045. if((pSC->WMISCP.bfBitstreamFormat != SPATIAL) && (pSC->pTile[pSC->cTileColumn].cBitsHP > 0)) // MB-based HP QP index
  1046. encodeQPIndex(pIO, pSC->MBInfo.iQIndexHP, pSC->pTile[pSC->cTileColumn].cBitsHP);
  1047. /** reset adaptive scan totals **/
  1048. if (pSC->m_bResetRGITotals) {
  1049. Int iScale = 2;
  1050. Int iWeight = iScale * 16;
  1051. Int k;
  1052. pContext->m_aScanHoriz[0].uTotal = pContext->m_aScanVert[0].uTotal = MAXTOTAL;
  1053. for (k = 1; k < 16; k++) {
  1054. pContext->m_aScanHoriz[k].uTotal = pContext->m_aScanVert[k].uTotal = iWeight;
  1055. iWeight -= iScale;
  1056. }
  1057. }
  1058. CodeCBP(pSC, pContext, iMBX, iMBY, pIO);
  1059. if(CodeCoeffs(pSC, pContext, iMBX, iMBY, pIO, pIOFL) != ICERR_OK)
  1060. return ICERR_ERROR;
  1061. if (pSC->m_bResetContext) {
  1062. AdaptHighpassEnc(pContext);
  1063. }
  1064. return ICERR_OK;
  1065. }