decode.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. //*@@@+++@@@@******************************************************************
  2. //
  3. // Copyright © Microsoft Corp.
  4. // All rights reserved.
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions are met:
  8. //
  9. // • Redistributions of source code must retain the above copyright notice,
  10. // this list of conditions and the following disclaimer.
  11. // • Redistributions in binary form must reproduce the above copyright notice,
  12. // this list of conditions and the following disclaimer in the documentation
  13. // and/or other materials provided with the distribution.
  14. //
  15. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  19. // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  20. // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  21. // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. // POSSIBILITY OF SUCH DAMAGE.
  26. //
  27. //*@@@---@@@@******************************************************************
  28. /******************************************************************************
  29. Module Name:
  30. decode.c
  31. Abstract:
  32. Defines the entry point for the console application.
  33. Author:
  34. Revision History:
  35. *******************************************************************************/
  36. #include "strcodec.h"
  37. #include "decode.h"
  38. #ifdef MEM_TRACE
  39. #define TRACE_MALLOC 1
  40. #define TRACE_NEW 0
  41. #define TRACE_HEAP 0
  42. #include "memtrace.h"
  43. #endif
  44. /******************************************************************
  45. Free Adaptive Huffman Table
  46. ******************************************************************/
  47. static Void CleanAH(CAdaptiveHuffman **ppAdHuff)
  48. {
  49. CAdaptiveHuffman *pAdHuff;
  50. if (NULL != ppAdHuff) {
  51. pAdHuff = *ppAdHuff;
  52. if (NULL != pAdHuff) {
  53. free(pAdHuff);
  54. }
  55. *ppAdHuff = NULL;
  56. }
  57. }
  58. static Void CleanAHDec(CCodingContext * pSC)
  59. {
  60. Int kk;
  61. for (kk = 0; kk < NUMVLCTABLES; kk++) {
  62. CleanAH(&(pSC->m_pAHexpt[kk]));
  63. }
  64. CleanAH(&(pSC->m_pAdaptHuffCBPCY));
  65. CleanAH(&(pSC->m_pAdaptHuffCBPCY1));
  66. }
  67. /*************************************************************************
  68. Initialize an adaptive huffman table
  69. *************************************************************************/
  70. static Int InitializeAH(CAdaptiveHuffman **ppAdHuff, Int iSym)
  71. {
  72. Int iMemStatus = 0;
  73. CAdaptiveHuffman *pAdHuff = Allocate(iSym, DECODER);
  74. if (pAdHuff == NULL) {
  75. iMemStatus = -1; // out of memory
  76. goto ErrorExit;
  77. }
  78. //Adapt(pAdHuff, bFixedTables);
  79. //InitHuffman(pAdHuff->m_pHuffman);
  80. //if (ICERR_OK != initHuff(pAdHuff->m_pHuffman, 1, pAdHuff->m_pTable, NULL)) {
  81. // goto ErrorExit;
  82. //}
  83. *ppAdHuff = pAdHuff;
  84. return ICERR_OK;
  85. ErrorExit:
  86. if (pAdHuff) {
  87. free(pAdHuff);
  88. }
  89. *ppAdHuff = NULL;
  90. if (-1 == iMemStatus) {
  91. printf("Insufficient memory to init decoder.\n");
  92. }
  93. return ICERR_ERROR;
  94. }
  95. /*************************************************************************
  96. Context allocation
  97. *************************************************************************/
  98. Int AllocateCodingContextDec(CWMImageStrCodec *pSC, Int iNumContexts)
  99. {
  100. Int i, iCBPSize, k;
  101. static const Int aAlphabet[] = {5,4,8,7,7, 12,6,6,12,6,6,7,7, 12,6,6,12,6,6,7,7};
  102. if (iNumContexts > MAX_TILES || iNumContexts < 1) // only between 1 and MAX_TILES allowed
  103. return ICERR_ERROR;
  104. if (pSC == NULL)
  105. return ICERR_ERROR;
  106. pSC->m_pCodingContext = malloc (iNumContexts * sizeof (CCodingContext));
  107. if (pSC->m_pCodingContext == NULL) {
  108. pSC->cNumCodingContext = 0;
  109. return ICERR_ERROR;
  110. }
  111. memset (pSC->m_pCodingContext, 0, iNumContexts * sizeof (CCodingContext));
  112. pSC->cNumCodingContext = iNumContexts;
  113. iCBPSize = (pSC->m_param.cfColorFormat == Y_ONLY || pSC->m_param.cfColorFormat == NCOMPONENT
  114. || pSC->m_param.cfColorFormat == CMYK) ? 5 : 9;
  115. /** allocate / initialize members **/
  116. for (i = 0; i < iNumContexts; i++) {
  117. CCodingContext *pContext = &(pSC->m_pCodingContext[i]);
  118. /** allocate adaptive Huffman encoder **/
  119. if (InitializeAH(&pContext->m_pAdaptHuffCBPCY, iCBPSize) != ICERR_OK) {
  120. return ICERR_ERROR;
  121. }
  122. if (InitializeAH(&pContext->m_pAdaptHuffCBPCY1, 5) != ICERR_OK) {
  123. return ICERR_ERROR;
  124. }
  125. for(k = 0; k < NUMVLCTABLES; k ++){
  126. if (InitializeAH(&pContext->m_pAHexpt[k], aAlphabet[k]) != ICERR_OK) {
  127. return ICERR_ERROR;
  128. }
  129. }
  130. ResetCodingContextDec(pContext);
  131. }
  132. return ICERR_OK;
  133. }
  134. /*************************************************************************
  135. Context reset on encoder
  136. *************************************************************************/
  137. Void ResetCodingContextDec(CCodingContext *pContext)
  138. {
  139. Int k;
  140. /** set flags **/
  141. pContext->m_pAdaptHuffCBPCY->m_bInitialize = FALSE;
  142. pContext->m_pAdaptHuffCBPCY1->m_bInitialize = FALSE;
  143. for(k = 0; k < NUMVLCTABLES; k ++)
  144. pContext->m_pAHexpt[k]->m_bInitialize = FALSE;
  145. // reset VLC tables
  146. AdaptLowpassDec (pContext);
  147. AdaptHighpassDec (pContext);
  148. // reset zigzag patterns, totals
  149. InitZigzagScan(pContext);
  150. // reset bit reduction and cbp models
  151. ResetCodingContext(pContext);
  152. }
  153. /*************************************************************************
  154. Context deletion
  155. *************************************************************************/
  156. Void FreeCodingContextDec(CWMImageStrCodec *pSC)
  157. {
  158. Int iContexts = (Int)(pSC->cNumCodingContext), i, k;
  159. if (iContexts > 0 && pSC->m_pCodingContext) {
  160. for (i = 0; i < iContexts; i++) {
  161. CCodingContext *pContext = &(pSC->m_pCodingContext[i]);
  162. CleanAH (&pContext->m_pAdaptHuffCBPCY);
  163. CleanAH (&pContext->m_pAdaptHuffCBPCY1);
  164. for (k = 0; k < NUMVLCTABLES; k++)
  165. CleanAH (&pContext->m_pAHexpt[k]);
  166. }
  167. free (pSC->m_pCodingContext);
  168. }
  169. }