encode.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 "encode.h"
  31. #include "strcodec.h"
  32. #include "common.h"
  33. #ifdef MEM_TRACE
  34. #define TRACE_MALLOC 1
  35. #define TRACE_NEW 0
  36. #define TRACE_HEAP 0
  37. #include "memtrace.h"
  38. #endif
  39. /*************************************************************************
  40. Context allocation
  41. In theory it is possible to independently set uiTrimFlexBits for
  42. each tile, but for now we assume only one user specified value is
  43. used for the entire image
  44. *************************************************************************/
  45. Int AllocateCodingContextEnc(CWMImageStrCodec *pSC, Int iNumContexts, Int iTrimFlexBits)
  46. {
  47. Int i, iCBPSize, k;
  48. 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};
  49. if (iTrimFlexBits < 0)
  50. iTrimFlexBits = 0;
  51. else if (iTrimFlexBits > 15)
  52. iTrimFlexBits = 15;
  53. pSC->m_param.bTrimFlexbitsFlag = (iTrimFlexBits > 0);
  54. if (iNumContexts < 1 || iNumContexts > MAX_TILES) // only between 1 and 256 allowed
  55. return ICERR_ERROR;
  56. if (pSC == NULL)
  57. return ICERR_ERROR;
  58. pSC->m_pCodingContext = malloc (iNumContexts * sizeof (CCodingContext));
  59. if (pSC->m_pCodingContext == NULL) {
  60. pSC->cNumCodingContext = 0;
  61. return ICERR_ERROR;
  62. }
  63. memset (pSC->m_pCodingContext, 0, iNumContexts * sizeof (CCodingContext));
  64. pSC->cNumCodingContext = iNumContexts;
  65. iCBPSize = (pSC->m_param.cfColorFormat == Y_ONLY || pSC->m_param.cfColorFormat == NCOMPONENT
  66. || pSC->m_param.cfColorFormat == CMYK) ? 5 : 9;
  67. /** allocate / initialize members **/
  68. for (i = 0; i < iNumContexts; i++) {
  69. CCodingContext *pContext = &(pSC->m_pCodingContext[i]);
  70. /** allocate adaptive Huffman encoder **/
  71. pContext->m_pAdaptHuffCBPCY = Allocate (iCBPSize, ENCODER);
  72. if(pContext->m_pAdaptHuffCBPCY == NULL) {
  73. return ICERR_ERROR;
  74. }
  75. pContext->m_pAdaptHuffCBPCY1 = Allocate(5, ENCODER);
  76. if(pContext->m_pAdaptHuffCBPCY1 == NULL){
  77. return ICERR_ERROR;
  78. }
  79. for(k = 0; k < NUMVLCTABLES; k ++){
  80. pContext->m_pAHexpt[k] = Allocate(aAlphabet[k], ENCODER);
  81. if(pContext->m_pAHexpt[k] == NULL){
  82. return ICERR_ERROR;
  83. }
  84. }
  85. ResetCodingContextEnc(pContext);
  86. pContext->m_iTrimFlexBits = iTrimFlexBits;
  87. }
  88. return ICERR_OK;
  89. }
  90. /*************************************************************************
  91. Context reset on encoder
  92. *************************************************************************/
  93. Void ResetCodingContextEnc(CCodingContext *pContext)
  94. {
  95. Int k;
  96. /** set flags **/
  97. pContext->m_pAdaptHuffCBPCY->m_bInitialize = FALSE;
  98. pContext->m_pAdaptHuffCBPCY1->m_bInitialize = FALSE;
  99. for(k = 0; k < NUMVLCTABLES; k ++)
  100. pContext->m_pAHexpt[k]->m_bInitialize = FALSE;
  101. // reset VLC tables
  102. AdaptLowpassEnc (pContext);
  103. AdaptHighpassEnc (pContext);
  104. // reset zigzag patterns, totals
  105. InitZigzagScan(pContext);
  106. // reset bit reduction and cbp models
  107. ResetCodingContext(pContext);
  108. }
  109. /*************************************************************************
  110. Context deletion
  111. *************************************************************************/
  112. Void FreeCodingContextEnc(CWMImageStrCodec *pSC)
  113. {
  114. Int iContexts = (Int)(pSC->cNumCodingContext), i, k;
  115. if (iContexts > 0 && pSC->m_pCodingContext) {
  116. for (i = 0; i < iContexts; i++) {
  117. CCodingContext *pContext = &(pSC->m_pCodingContext[i]);
  118. Clean (pContext->m_pAdaptHuffCBPCY);
  119. Clean (pContext->m_pAdaptHuffCBPCY1);
  120. for (k = 0; k < NUMVLCTABLES; k++)
  121. Clean (pContext->m_pAHexpt[k]);
  122. }
  123. free (pSC->m_pCodingContext);
  124. }
  125. }