strPredQuant.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. #define ORIENT_WEIGHT 4
  30. /* reciprocal (pMantissa, exponent) lookup table */
  31. typedef struct tagQPManExp
  32. {
  33. int iMan;
  34. int iExp;
  35. } QPManExp;
  36. static QPManExp gs_QPRecipTable[32] = {
  37. {0x0, 0}, // 0, invalid
  38. {0x0, 0}, // 1, lossless
  39. {0x0, 1}, // 2
  40. {0xaaaaaaab, 1},
  41. {0x0, 2}, // 4
  42. {0xcccccccd, 2},
  43. {0xaaaaaaab, 2},
  44. {0x92492493, 2},
  45. {0x0, 3}, // 8
  46. {0xe38e38e4, 3},
  47. {0xcccccccd, 3},
  48. {0xba2e8ba3, 3},
  49. {0xaaaaaaab, 3},
  50. {0x9d89d89e, 3},
  51. {0x92492493, 3},
  52. {0x88888889, 3},
  53. {0x0, 4}, // 16
  54. {0xf0f0f0f1, 4},
  55. {0xe38e38e4, 4},
  56. {0xd79435e6, 4},
  57. {0xcccccccd, 4},
  58. {0xc30c30c4, 4},
  59. {0xba2e8ba3, 4},
  60. {0xb21642c9, 4},
  61. {0xaaaaaaab, 4},
  62. {0xa3d70a3e, 4},
  63. {0x9d89d89e, 4},
  64. {0x97b425ee, 4},
  65. {0x92492493, 4},
  66. {0x8d3dcb09, 4},
  67. {0x88888889, 4},
  68. {0x84210843, 4},
  69. };
  70. /*************************************************************************
  71. QPRemapping
  72. *************************************************************************/
  73. Void remapQP(CWMIQuantizer * pQP, I32 iShift, Bool bScaledArith)
  74. {
  75. U8 uiQPIndex = pQP->iIndex;
  76. if(uiQPIndex == 0) // Lossless mode!
  77. pQP->iQP = 1, pQP->iMan = pQP->iExp = pQP->iOffset = 0;
  78. else if (!bScaledArith) {
  79. I32 man = 0, exp = 0;
  80. const I32 ciShift = SHIFTZERO - (SHIFTZERO + QPFRACBITS); // == -QPFRACBITS
  81. if (pQP->iIndex < 32)
  82. man = (pQP->iIndex + 3) >> 2, exp = ciShift + 2;
  83. else if (pQP->iIndex < 48)
  84. man = (16 + (pQP->iIndex & 0xf) + 1) >> 1, exp = ((pQP->iIndex >> 4) - 1) + 1 + ciShift;
  85. else
  86. man = 16 + (pQP->iIndex & 0xf), exp = ((pQP->iIndex >> 4) - 1) + ciShift;
  87. pQP->iQP = man << exp;
  88. pQP->iMan = gs_QPRecipTable[man].iMan;
  89. pQP->iExp = gs_QPRecipTable[man].iExp + exp;
  90. pQP->iOffset = ((pQP->iQP * 3 + 1) >> 3);
  91. #if defined(WMP_OPT_QT)
  92. pQP->f1_QP = 1.0f / pQP->iQP;
  93. pQP->d1_QP = 1.0 / pQP->iQP;
  94. #endif
  95. }
  96. else {
  97. I32 man = 0, exp = 0;
  98. if(pQP->iIndex < 16)
  99. man = pQP->iIndex, exp = iShift;
  100. else
  101. man = 16 + (pQP->iIndex & 0xf), exp = ((pQP->iIndex >> 4) - 1) + iShift;
  102. pQP->iQP = man << exp;
  103. pQP->iMan = gs_QPRecipTable[man].iMan;
  104. pQP->iExp = gs_QPRecipTable[man].iExp + exp;
  105. pQP->iOffset = ((pQP->iQP * 3 + 1) >> 3);
  106. #if defined(WMP_OPT_QT)
  107. pQP->f1_QP = 1.0f / pQP->iQP;
  108. pQP->d1_QP = 1.0 / pQP->iQP;
  109. #endif
  110. }
  111. }
  112. /* allocate PredInfo buffers */
  113. Int allocatePredInfo(CWMImageStrCodec *pSC)
  114. {
  115. size_t i, j;
  116. // COLORFORMAT cf = pSC->m_param.cfColorFormat;
  117. const size_t mbWidth = pSC->cmbWidth;
  118. const size_t iChannels = pSC->m_param.cNumChannels;
  119. CWMIPredInfo* pMemory;
  120. Bool b32Bit = sizeof(size_t) == 4;
  121. if(b32Bit) // integer overlow/underflow check for 32-bit system
  122. if(((mbWidth >> 16) * iChannels * 2 * sizeof(CWMIPredInfo)) & 0xffff0000)
  123. return ICERR_ERROR;
  124. pMemory = (CWMIPredInfo *)malloc(mbWidth * iChannels * 2 * sizeof(CWMIPredInfo));
  125. if (pMemory == NULL)
  126. return ICERR_ERROR;
  127. pSC->pPredInfoMemory = pMemory;
  128. for(i = 0; i < iChannels; i ++){
  129. pSC->PredInfo[i] = pMemory;
  130. pMemory += mbWidth;
  131. pSC->PredInfoPrevRow[i] = pMemory;
  132. pMemory += mbWidth;
  133. for(j = 0; j < mbWidth; j ++){
  134. pSC->PredInfo[i][j].piAD = pSC->PredInfo[i][j].iAD;
  135. pSC->PredInfoPrevRow[i][j].piAD = pSC->PredInfoPrevRow[i][j].iAD;
  136. }
  137. }
  138. return ICERR_OK;
  139. }
  140. /* clear PredInfo buffers */
  141. Void freePredInfo(CWMImageStrCodec *pSC)
  142. {
  143. if (pSC->pPredInfoMemory)
  144. free (pSC->pPredInfoMemory);
  145. pSC->pPredInfoMemory = NULL;
  146. }
  147. /* get AC prediction mode: 0(from left) 1(from top) 2(none) */
  148. Int getACPredMode(CWMIMBInfo * pMBInfo, COLORFORMAT cf)
  149. {
  150. //Int blkIdx = (cf == Y_ONLY ? 16 : (cf == YUV_420 ? 24 : (cf == YUV_422 ? 32 : 48)));
  151. PixelI * pCoeffs = pMBInfo->iBlockDC[0];
  152. Int StrH = abs(pCoeffs[1]) + abs(pCoeffs[2]) + abs(pCoeffs[3]);
  153. Int StrV = abs(pCoeffs[4]) + abs(pCoeffs[8]) + abs(pCoeffs[12]);
  154. if(cf != Y_ONLY && cf != NCOMPONENT){
  155. PixelI * pCoeffsU = pMBInfo->iBlockDC[1];
  156. PixelI * pCoeffsV = pMBInfo->iBlockDC[2];
  157. StrH += abs(pCoeffsU[1]) + abs(pCoeffsV[1]);
  158. if(cf == YUV_420){
  159. StrV += abs(pCoeffsU[2]) + abs(pCoeffsV[2]);
  160. }
  161. else if (cf == YUV_422){
  162. StrV += abs(pCoeffsU[2]) + abs(pCoeffsV[2]) + abs(pCoeffsU[6]) + abs(pCoeffsV[6]);
  163. StrH += abs(pCoeffsU[5]) + abs(pCoeffsV[5]);
  164. }
  165. else { // YUV_444 or CMYK
  166. StrV += abs(pCoeffsU[4]) + abs(pCoeffsV[4]);
  167. }
  168. }
  169. return (StrH * ORIENT_WEIGHT < StrV ? 1 : (StrV * ORIENT_WEIGHT < StrH ? 0 : 2));
  170. }
  171. /* get DCAC prediction mode: 0(from left) 1(from top) 2(none) */
  172. Int getDCACPredMode(CWMImageStrCodec *pSC, size_t mbX)
  173. {
  174. Int iDCMode, iADMode = 2; // DC: 0(left) 1(top) 2(mean) 3(no)
  175. // AD: 0(left) 1(top) 2(no)
  176. if(pSC->m_bCtxLeft && pSC->m_bCtxTop){ // topleft corner, no prediction
  177. iDCMode = 3;
  178. }
  179. else if(pSC->m_bCtxLeft){
  180. iDCMode = 1; // left column, predict from top
  181. }
  182. else if(pSC->m_bCtxTop){
  183. iDCMode = 0; // top row, predict from left
  184. }
  185. else{
  186. COLORFORMAT cf = pSC->m_param.cfColorFormat;
  187. Int iL = pSC->PredInfo[0][mbX - 1].iDC, iT = pSC->PredInfoPrevRow[0][mbX].iDC, iTL = pSC->PredInfoPrevRow[0][mbX - 1].iDC;
  188. Int StrH, StrV;
  189. if(cf == Y_ONLY || cf == NCOMPONENT){ // CMYK uses YUV metric
  190. StrH = abs(iTL - iL);
  191. StrV = abs(iTL - iT);
  192. }
  193. else{
  194. CWMIPredInfo * pTU = pSC->PredInfoPrevRow[1] + mbX, * pLU = pSC->PredInfo[1] + mbX - 1, * pTLU = pTU - 1;
  195. CWMIPredInfo * pTV = pSC->PredInfoPrevRow[2] + mbX, * pLV = pSC->PredInfo[2] + mbX - 1, * pTLV = pTV - 1;
  196. Int scale = (cf == YUV_420 ? 8 : (cf == YUV_422 ? 4 : 2));
  197. StrH = abs(iTL - iL) * scale + abs(pTLU->iDC - pLU->iDC) + abs(pTLV->iDC - pLV->iDC);
  198. StrV = abs(iTL - iT) * scale + abs(pTLU->iDC - pTU->iDC) + abs(pTLV->iDC - pTV->iDC);
  199. }
  200. iDCMode = (StrH * ORIENT_WEIGHT < StrV ? 1 : (StrV * ORIENT_WEIGHT < StrH ? 0 : 2));
  201. }
  202. if(iDCMode == 1 && pSC->MBInfo.iQIndexLP == pSC->PredInfoPrevRow[0][mbX].iQPIndex)
  203. iADMode = 1;
  204. if(iDCMode == 0 && pSC->MBInfo.iQIndexLP == pSC->PredInfo[0][mbX - 1].iQPIndex)
  205. iADMode = 0;
  206. return (iDCMode + (iADMode << 2));
  207. }
  208. Void copyAC(PixelI * src, PixelI * dst)
  209. {
  210. /* first row of ACs */
  211. dst[0] = src[1];
  212. dst[1] = src[2];
  213. dst[2] = src[3];
  214. /* first column of ACs */
  215. dst[3] = src[4];
  216. dst[4] = src[8];
  217. dst[5] = src[12];
  218. }
  219. /* info of current MB to be saved for future prediction */
  220. Void updatePredInfo(CWMImageStrCodec *pSC, CWMIMBInfo * pMBInfo, size_t mbX, COLORFORMAT cf)
  221. {
  222. CWMIPredInfo *pPredInfo;
  223. PixelI * p;
  224. Int i, iChannels = (cf == YUV_420 || cf == YUV_422) ? 1 : (Int) pSC->m_param.cNumChannels;
  225. for(i = 0; i < iChannels; i ++){
  226. pPredInfo = pSC->PredInfo[i] + mbX;
  227. p = pMBInfo->iBlockDC[i];//[dcBlkIdx + i];
  228. /* DC of DC block */
  229. pPredInfo->iDC = p[0];
  230. /* QP Index */
  231. pPredInfo->iQPIndex = pMBInfo->iQIndexLP;
  232. /* first row and first column of ACs of DC block */
  233. copyAC(p, pPredInfo->piAD);
  234. }
  235. if(cf == YUV_420){ // 420 UV channels
  236. for(i = 1U; i < 3U; i ++){
  237. pPredInfo = pSC->PredInfo[i] + mbX;
  238. p = pMBInfo->iBlockDC[i];//[dcBlkIdx + i];
  239. /* DC of DC block */
  240. pPredInfo->iDC = p[0];
  241. /* QP Index */
  242. pPredInfo->iQPIndex = pMBInfo->iQIndexLP;
  243. /* first row and first column of ACs of DC block */
  244. pPredInfo->piAD[0] = p[1];
  245. pPredInfo->piAD[1] = p[2];
  246. }
  247. }
  248. else if(cf == YUV_422){ // 420 UV channels
  249. for(i = 1U; i < 3U; i ++){
  250. pPredInfo = pSC->PredInfo[i] + mbX;
  251. /* QP Index */
  252. pPredInfo->iQPIndex = pMBInfo->iQIndexLP;
  253. p = pMBInfo->iBlockDC[i];//[dcBlkIdx + i];
  254. /* DC of DC block */
  255. pPredInfo->iDC = p[0];
  256. /* first row and first column of ACs of first DC block */
  257. pPredInfo->piAD[0] = p[1];
  258. pPredInfo->piAD[1] = p[2];
  259. /* first row and first column of ACs of second DC block */
  260. pPredInfo->piAD[2] = p[5];
  261. pPredInfo->piAD[3] = p[6];
  262. pPredInfo->piAD[4] = p[4]; //AC of 1D HT!!!
  263. }
  264. }
  265. }