//*@@@+++@@@@******************************************************************1//2// Copyright © Microsoft Corp.3// All rights reserved.4//5// Redistribution and use in source and binary forms, with or without6// modification, are permitted provided that the following conditions are met:7//8// • Redistributions of source code must retain the above copyright notice,9// this list of conditions and the following disclaimer.10// • Redistributions in binary form must reproduce the above copyright notice,11// this list of conditions and the following disclaimer in the documentation12// and/or other materials provided with the distribution.13//14// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"15// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE16// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE17// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE18// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR19// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF20// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS21// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN22// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)23// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE24// POSSIBILITY OF SUCH DAMAGE.25//26//*@@@---@@@@******************************************************************2728#include <stdio.h>29#include <stdlib.h>30#include "encode.h"31#include "strcodec.h"32#include "common.h"3334#ifdef MEM_TRACE35#define TRACE_MALLOC 136#define TRACE_NEW 037#define TRACE_HEAP 038#include "memtrace.h"39#endif4041/*************************************************************************42Context allocation43In theory it is possible to independently set uiTrimFlexBits for44each tile, but for now we assume only one user specified value is45used for the entire image46*************************************************************************/47Int AllocateCodingContextEnc(CWMImageStrCodec *pSC, Int iNumContexts, Int iTrimFlexBits)48{49Int i, iCBPSize, k;50static const Int aAlphabet[] = {5,4,8,7,7, 12,6,6,12,6,6,7,7, 12,6,6,12,6,6,7,7};5152if (iTrimFlexBits < 0)53iTrimFlexBits = 0;54else if (iTrimFlexBits > 15)55iTrimFlexBits = 15;56pSC->m_param.bTrimFlexbitsFlag = (iTrimFlexBits > 0);5758if (iNumContexts < 1 || iNumContexts > MAX_TILES) // only between 1 and 256 allowed59return ICERR_ERROR;6061if (pSC == NULL)62return ICERR_ERROR;6364pSC->m_pCodingContext = malloc (iNumContexts * sizeof (CCodingContext));65if (pSC->m_pCodingContext == NULL) {66pSC->cNumCodingContext = 0;67return ICERR_ERROR;68}69memset (pSC->m_pCodingContext, 0, iNumContexts * sizeof (CCodingContext));7071pSC->cNumCodingContext = iNumContexts;72iCBPSize = (pSC->m_param.cfColorFormat == Y_ONLY || pSC->m_param.cfColorFormat == NCOMPONENT73|| pSC->m_param.cfColorFormat == CMYK) ? 5 : 9;7475/** allocate / initialize members **/76for (i = 0; i < iNumContexts; i++) {77CCodingContext *pContext = &(pSC->m_pCodingContext[i]);7879/** allocate adaptive Huffman encoder **/80pContext->m_pAdaptHuffCBPCY = Allocate (iCBPSize, ENCODER);81if(pContext->m_pAdaptHuffCBPCY == NULL) {82return ICERR_ERROR;83}84pContext->m_pAdaptHuffCBPCY1 = Allocate(5, ENCODER);85if(pContext->m_pAdaptHuffCBPCY1 == NULL){86return ICERR_ERROR;87}8889for(k = 0; k < NUMVLCTABLES; k ++){90pContext->m_pAHexpt[k] = Allocate(aAlphabet[k], ENCODER);91if(pContext->m_pAHexpt[k] == NULL){92return ICERR_ERROR;93}94}9596ResetCodingContextEnc(pContext);97pContext->m_iTrimFlexBits = iTrimFlexBits;98}99100return ICERR_OK;101}102103/*************************************************************************104Context reset on encoder105*************************************************************************/106Void ResetCodingContextEnc(CCodingContext *pContext)107{108Int k;109/** set flags **/110pContext->m_pAdaptHuffCBPCY->m_bInitialize = FALSE;111pContext->m_pAdaptHuffCBPCY1->m_bInitialize = FALSE;112for(k = 0; k < NUMVLCTABLES; k ++)113pContext->m_pAHexpt[k]->m_bInitialize = FALSE;114115// reset VLC tables116AdaptLowpassEnc (pContext);117AdaptHighpassEnc (pContext);118119// reset zigzag patterns, totals120InitZigzagScan(pContext);121// reset bit reduction and cbp models122ResetCodingContext(pContext);123}124125/*************************************************************************126Context deletion127*************************************************************************/128Void FreeCodingContextEnc(CWMImageStrCodec *pSC)129{130Int iContexts = (Int)(pSC->cNumCodingContext), i, k;131if (iContexts > 0 && pSC->m_pCodingContext) {132133for (i = 0; i < iContexts; i++) {134CCodingContext *pContext = &(pSC->m_pCodingContext[i]);135Clean (pContext->m_pAdaptHuffCBPCY);136Clean (pContext->m_pAdaptHuffCBPCY1);137for (k = 0; k < NUMVLCTABLES; k++)138Clean (pContext->m_pAHexpt[k]);139}140free (pSC->m_pCodingContext);141}142}143144145146