image.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 "xplatform_image.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. #include <stdlib.h>
  37. #include <string.h>
  38. #if !(defined(__ANSI__))
  39. // Desktop
  40. #include <windows.h>
  41. #else
  42. // ANSI
  43. #include <time.h>
  44. #endif
  45. Int grgiZigzagInv4x4_lowpass [] = {
  46. 0, 1, 4, 5, 2, 8, 6, 9,
  47. 3, 12, 10, 7, 13, 11, 14, 15
  48. };
  49. Int grgiZigzagInv4x4H [] = {
  50. 0, 1, 4, 5, 2, 8, 6, 9,
  51. 3, 12, 10, 7, 13, 11, 14, 15
  52. };
  53. Int grgiZigzagInv4x4V [] = {
  54. 0, 4, 8, 5, 1, 12, 9, 6, 2, 13, 3, 15, 7, 10, 14, 11
  55. };
  56. const Int gSignificantRunBin[] = {
  57. -1,-1,-1,-1,
  58. 2,2,2,
  59. 1,1,1,1,
  60. 0,0,0,0
  61. };
  62. const Int gSignificantRunFixedLength[] = {
  63. 0,0,1,1,3,
  64. 0,0,1,1,2,
  65. 0,0,0,0,1,
  66. };
  67. /*************************************************************************
  68. UpdateModelMB : update adaptive model at end of macroblock
  69. (for lowest resolution only)
  70. *************************************************************************/
  71. #define MODELWEIGHT 70//90
  72. Void UpdateModelMB (COLORFORMAT cf, Int iChannels, Int iLaplacianMean[], CAdaptiveModel *pModel)
  73. {
  74. Int j;
  75. static const Int aWeight0[3] = { 240/*DC*/, 12/*LP*/, 1 };
  76. static const Int aWeight1[3][MAX_CHANNELS] = {
  77. { 0,240,120,80, 60,48,40,34, 30,27,24,22, 20,18,17,16 },
  78. { 0,12,6,4, 3,2,2,2, 2,1,1,1, 1,1,1,1 },
  79. { 0,16,8,5, 4,3,3,2, 2,2,2,1, 1,1,1,1 }
  80. };
  81. static const Int aWeight2[6] = { 120,37,2,/*420*/ 120,18,1/*422*/ };
  82. iLaplacianMean[0] *= aWeight0[pModel->m_band - BAND_DC];
  83. if (cf == YUV_420) {
  84. iLaplacianMean[1] *= aWeight2[pModel->m_band - BAND_DC];
  85. }
  86. else if (cf == YUV_422) {
  87. iLaplacianMean[1] *= aWeight2[3 + (pModel->m_band) - BAND_DC];
  88. }
  89. else {
  90. iLaplacianMean[1] *= aWeight1[pModel->m_band - BAND_DC][iChannels - 1];
  91. if (pModel->m_band == BAND_AC)
  92. iLaplacianMean[1] >>= 4;
  93. }
  94. for (j = 0; j < 2; j++) {
  95. Int iLM = iLaplacianMean[j];
  96. Int iMS = pModel->m_iFlcState[j];
  97. Int iDelta = (iLM - MODELWEIGHT) >> 2;
  98. if (iDelta <= -8) {
  99. iDelta += 4;
  100. if (iDelta < -16)
  101. iDelta = -16;
  102. iMS += iDelta;
  103. if (iMS < -8) {
  104. if (pModel->m_iFlcBits[j] == 0)
  105. iMS = -8;
  106. else {
  107. iMS = 0;
  108. pModel->m_iFlcBits[j]--;
  109. }
  110. }
  111. }
  112. else if (iDelta >= 8) {
  113. iDelta -= 4;
  114. if (iDelta > 15)
  115. iDelta = 15;
  116. iMS += iDelta;
  117. if (iMS > 8) {
  118. if (pModel->m_iFlcBits[j] >= 15) {
  119. pModel->m_iFlcBits[j] = 15;
  120. iMS = 8;
  121. }
  122. else {
  123. iMS = 0;
  124. pModel->m_iFlcBits[j]++;
  125. }
  126. }
  127. }
  128. pModel->m_iFlcState[j] = iMS;
  129. if (cf == Y_ONLY)
  130. break;
  131. }
  132. }
  133. Void ResetCodingContext(CCodingContext *pContext)
  134. {
  135. // reset bit reduction models
  136. memset (&(pContext->m_aModelAC), 0, sizeof(CAdaptiveModel));
  137. pContext->m_aModelAC.m_band = BAND_AC;
  138. memset (&(pContext->m_aModelLP), 0, sizeof(CAdaptiveModel));
  139. pContext->m_aModelLP.m_band = BAND_LP;
  140. pContext->m_aModelLP.m_iFlcBits[0] = pContext->m_aModelLP.m_iFlcBits[1] = 4;
  141. memset (&(pContext->m_aModelDC), 0, sizeof(CAdaptiveModel));
  142. pContext->m_aModelDC.m_band = BAND_DC;
  143. pContext->m_aModelDC.m_iFlcBits[0] = pContext->m_aModelDC.m_iFlcBits[1] = 8;
  144. // reset CBP models
  145. pContext->m_iCBPCountMax = pContext->m_iCBPCountZero = 1;
  146. pContext->m_aCBPModel.m_iCount0[0] = pContext->m_aCBPModel.m_iCount0[1] = -4;
  147. pContext->m_aCBPModel.m_iCount1[0] = pContext->m_aCBPModel.m_iCount1[1] = 4;
  148. pContext->m_aCBPModel.m_iState[0] = pContext->m_aCBPModel.m_iState[1] = 0;
  149. }
  150. /*************************************************************************
  151. Initialize zigzag scan parameters
  152. *************************************************************************/
  153. Void InitZigzagScan(CCodingContext * pContext)
  154. {
  155. if (NULL != pContext) {
  156. Int i;
  157. for (i=0; i<16; i++) {
  158. pContext->m_aScanLowpass[i].uScan = grgiZigzagInv4x4_lowpass[i];
  159. pContext->m_aScanHoriz[i].uScan = dctIndex[0][grgiZigzagInv4x4H[i]];
  160. pContext->m_aScanVert[i].uScan = dctIndex[0][grgiZigzagInv4x4V[i]];
  161. }
  162. }
  163. }