Alien-FreeImage

 view release on metacpan or  search on metacpan

src/Source/LibJXR/image/decode/segdec.c  view on Meta::CPAN

        }

        iCBPCY = pSC->MBInfo.iCBP[(i + 1) & 0xf];
        assert (MAX_CHANNELS == 16);
    }

    /** update model at end of MB **/
    UpdateModelMB (cf, iChannels, aLaplacianMean, &(pContext->m_aModelAC));
    return ICERR_OK;
}

/*************************************************************************
    DecodeSignificantAbsLevel
*************************************************************************/
#ifndef X86OPT_INLINE
static Int DecodeSignificantAbsLevel (struct CAdaptiveHuffman *pAHexpt, BitIOInfo* pIO)
#else
static __forceinline Int DecodeSignificantAbsLevel (struct CAdaptiveHuffman *pAHexpt, BitIOInfo* pIO)
#endif
{
    UInt iIndex;
    Int iFixed, iLevel;
    static const Int aRemap[] = { 2, 3, 4, 6, 10, 14 };
    static const Int aFixedLength[] = { 0, 0, 1, 2, 2, 2 };

    iIndex = (UInt)getHuff (pAHexpt->m_hufDecTable, pIO);
    assert(iIndex <= 6);
    pAHexpt->m_iDiscriminant += pAHexpt->m_pDelta[iIndex];
    if (iIndex < 2) {
        iLevel = iIndex + 2; // = aRemap[iIndex]
    }
    else if (iIndex < 6) {
        iFixed = aFixedLength[iIndex];
        iLevel = aRemap[iIndex] + _getBit16 (pIO, iFixed);
    }
    else{
        iFixed = _getBit16 (pIO, 4) + 4;
        if (iFixed == 19) {
            iFixed += _getBit16 (pIO, 2);
            if (iFixed == 22) {
                iFixed += _getBit16 (pIO, 3);
            }
        }
        iLevel = 2 + (1 << iFixed);
        iIndex = getBit32 (pIO, iFixed);
        iLevel += iIndex;
    }
    return iLevel;
}

U8 decodeQPIndex(BitIOInfo* pIO,U8 cBits)
{
    if(_getBit16(pIO, 1) == 0)
        return 0;
    return (U8)(_getBit16(pIO, cBits) + 1);
}

/*************************************************************************
    DecodeSecondStageCoeff
*************************************************************************/
Int DecodeMacroblockLowpass (CWMImageStrCodec * pSC, CCodingContext *pContext,
        Int iMBX, Int iMBYdummy)
{
    const COLORFORMAT cf = pSC->m_param.cfColorFormat;
    const Int iChannels = (Int) pSC->m_param.cNumChannels;
    const Int iFullPlanes = (cf == YUV_420 || cf == YUV_422) ? 2 : iChannels;
    Int k;
	CAdaptiveScan *pScan = pContext->m_aScanLowpass;
    BitIOInfo* pIO = pContext->m_pIOLP;
    Int iModelBits = pContext->m_aModelLP.m_iFlcBits[0];
    Int aRLCoeffs[32], iNumNonzero = 0, iIndex = 0;
    Int aLaplacianMean[2] = { 0, 0}, *pLM = aLaplacianMean;
    Int iChannel, iCBP = 0;
#ifndef ARMOPT_BITIO    // ARM opt always uses 32-bit version of getBits
    U32 (*getBits)(BitIOInfo* pIO, U32 cBits) = _getBit16;
#endif
    CWMIMBInfo * pMBInfo = &pSC->MBInfo;
    I32 *aDC[MAX_CHANNELS];

    UNREFERENCED_PARAMETER( iMBX );
    UNREFERENCED_PARAMETER( iMBYdummy );

    readIS_L1(pSC, pIO);
    if((pSC->WMISCP.bfBitstreamFormat != SPATIAL) && (pSC->pTile[pSC->cTileColumn].cBitsLP > 0))  // MB-based LP QP index
        pMBInfo->iQIndexLP = decodeQPIndex(pIO, pSC->pTile[pSC->cTileColumn].cBitsLP);
    
    // set arrays
    for (k = 0; k < (Int) pSC->m_param.cNumChannels; k++) {
        aDC[k & 15] = pMBInfo->iBlockDC[k];
    }

    /** reset adaptive scan totals **/
    if (pSC->m_bResetRGITotals) {
        int iScale = 2;
        int iWeight = iScale * 16;
		pScan[0].uTotal = MAXTOTAL;
        for (k = 1; k < 16; k++) {
			pScan[k].uTotal = iWeight;
            iWeight -= iScale;
        }
    }

    /** in raw mode, this can take 6% of the bits in the extreme low rate case!!! **/
    if (cf == YUV_420 || cf == YUV_422 || cf == YUV_444) {
        int iCountM = pContext->m_iCBPCountMax, iCountZ = pContext->m_iCBPCountZero;
        int iMax = iFullPlanes * 4 - 5; /* actually (1 << iNChannels) - 1 **/
        if (iCountZ <= 0 || iCountM < 0) {
            iCBP = 0;
            if (_getBool16 (pIO)) {
                iCBP = 1;
                k = _getBit16 (pIO, iFullPlanes - 1);
                if (k) {
                    iCBP = k * 2 + _getBit16(pIO, 1);
                }
            }
            if (iCountM < iCountZ)
                iCBP = iMax - iCBP;
        }
        else {
            iCBP = _getBit16(pIO, iFullPlanes);
        }

src/Source/LibJXR/image/decode/segdec.c  view on Meta::CPAN

                    if (pCoeffs[k]) {
                        Int r1 = _rotl(pCoeffs[k], iModelBits);
                        pCoeffs[k] = (r1 ^ getBits(pIO, iModelBits)) - (r1 & iMask);
                    }
#else // WIN32
                    if (pCoeffs[k] > 0) {
                        pCoeffs[k] <<= iModelBits;
                        pCoeffs[k] += getBits (pIO, iModelBits);
                    }
                    else if (pCoeffs[k] < 0) {
                        pCoeffs[k] <<= iModelBits;
                        pCoeffs[k] -= getBits (pIO, iModelBits);
                    }
#endif // WIN32
                    else {
                        //pCoeffs[k] = getBits (pIO, iModelBits);
                        //if (pCoeffs[k] && _getBool16 (pIO))
                        //    pCoeffs[k] = -pCoeffs[k];
                        Int r1 = _peekBit16 (pIO, iModelBits + 1);
                        pCoeffs[k] = ((r1 >> 1) ^ (-(r1 & 1))) + (r1 & 1);
                        _flushBit16 (pIO, iModelBits + (pCoeffs[k] != 0));
                    }
                }
            }
        }
        pLM = aLaplacianMean + 1;
        iModelBits = pContext->m_aModelLP.m_iFlcBits[1];

        iCBP >>= 1;
    }

    UpdateModelMB (cf, iChannels, aLaplacianMean, &(pContext->m_aModelLP));

    if (pSC->m_bResetContext) {
        AdaptLowpassDec(pContext);
    }

    return ICERR_OK;
}

/*************************************************************************
    8 bit YUV 420 macroblock decode function with 4x4 transform
    Index order is as follows:
    Y:              U:      V:
     0  1  4  5     16 17   20 21
     2  3  6  7     18 19   22 23
     8  9 12 13
    10 11 14 15

    DCAC coefficients stored for 4x4 - offsets (x == no storage)
    Y:
    x x x [0..3]
    x x x [4..7]
    x x x [8..11]
    [16..19] [20..23] [24..27] [28..31,12..15]

    U, V:
    x [0..3]
    [8..11] [4..7,12..15]
*************************************************************************/
Int DecodeMacroblockDC(CWMImageStrCodec * pSC, CCodingContext *pContext, Int iMBX, Int iMBY)
{
    CWMITile * pTile = pSC->pTile + pSC->cTileColumn;
    CWMIMBInfo * pMBInfo = &pSC->MBInfo;
    const COLORFORMAT cf = pSC->m_param.cfColorFormat;
    const Int iChannels = (Int) pSC->m_param.cNumChannels;
    BitIOInfo* pIO = pContext->m_pIODC;
    Int iIndex, i;
    Int aLaplacianMean[2] = { 0, 0}, *pLM = aLaplacianMean;
    Int iModelBits = pContext->m_aModelDC.m_iFlcBits[0];
    struct CAdaptiveHuffman *pAH;
    Int iQDCY, iQDCU, iQDCV;
    // const Int iChromaElements = (cf == YUV_420) ? 8 * 8 : ((cf == YUV_422) ? 8 * 16 : 16 * 16);

    UNREFERENCED_PARAMETER( iMBX );
    UNREFERENCED_PARAMETER( iMBY );

    for (i = 0; i < iChannels; i++)
        memset (pMBInfo->iBlockDC[i], 0, 16 * sizeof (I32));

    readIS_L1(pSC, pIO);

    pMBInfo->iQIndexLP = pMBInfo->iQIndexHP = 0;

    if(pSC->WMISCP.bfBitstreamFormat == SPATIAL && pSC->WMISCP.sbSubband != SB_DC_ONLY){
        if(pTile->cBitsLP > 0)  // MB-based LP QP index
            pMBInfo->iQIndexLP = decodeQPIndex(pIO, pTile->cBitsLP);
        if( pSC->WMISCP.sbSubband != SB_NO_HIGHPASS && pTile->cBitsHP > 0)  // MB-based HP QP index
            pMBInfo->iQIndexHP = decodeQPIndex(pIO, pTile->cBitsHP);
    }
    if(pTile->cBitsHP == 0 && pTile->cNumQPHP > 1) // use LP QP
        pMBInfo->iQIndexHP = pMBInfo->iQIndexLP;
    if (pMBInfo->iQIndexLP >= pTile->cNumQPLP || pMBInfo->iQIndexHP >= pTile->cNumQPHP)
        return ICERR_ERROR;

    if(cf == Y_ONLY || cf == CMYK || cf == NCOMPONENT) {
        for (i = 0; i < iChannels; i++) {
            iQDCY = 0;
            /** get luminance DC **/
            if (_getBool16 (pIO)) {
                iQDCY = DecodeSignificantAbsLevel(pContext->m_pAHexpt[3], pIO) - 1;
                *pLM += 1;
            }
            if (iModelBits) {
                iQDCY = (iQDCY << iModelBits) | _getBit16(pIO, iModelBits);
            }
            if (iQDCY && _getBool16 (pIO))
                iQDCY = -iQDCY;
            pMBInfo->iBlockDC[i][0] = iQDCY;

            pLM = aLaplacianMean + 1;
            iModelBits = pContext->m_aModelDC.m_iFlcBits[1];
        }
    }
    else {
        /** find significant level in 3D **/
        pAH = pContext->m_pAHexpt[2];
        iIndex = getHuff (pAH->m_hufDecTable, pIO);
        iQDCY = iIndex >> 2;
        iQDCU = (iIndex >> 1) & 1;
        iQDCV = iIndex & 1;

        /** get luminance DC **/
        if (iQDCY) {
            iQDCY = DecodeSignificantAbsLevel(pContext->m_pAHexpt[3], pIO) - 1;
            *pLM += 1;
        }
        if (iModelBits) {
            iQDCY = (iQDCY << iModelBits) | _getBit16(pIO, iModelBits);
        }
        if (iQDCY && _getBool16 (pIO))
            iQDCY = -iQDCY;
        pMBInfo->iBlockDC[0][0] = iQDCY;

        /** get chrominance DC **/        
        pLM = aLaplacianMean + 1;
        iModelBits = pContext->m_aModelDC.m_iFlcBits[1];

        if (iQDCU) {
            iQDCU = DecodeSignificantAbsLevel(pContext->m_pAHexpt[4], pIO) - 1;
            *pLM += 1;
        }
        if (iModelBits) {
            iQDCU = (iQDCU << iModelBits) | _getBit16(pIO, iModelBits);
        }
        if (iQDCU && _getBool16 (pIO))
            iQDCU = -iQDCU;
        pMBInfo->iBlockDC[1][0] = iQDCU;

        if (iQDCV) {
            iQDCV = DecodeSignificantAbsLevel(pContext->m_pAHexpt[4], pIO) - 1;
            *pLM += 1;
        }
        if (iModelBits) {
            iQDCV = (iQDCV << iModelBits) | _getBit16(pIO, iModelBits);
        }
        if (iQDCV && _getBool16 (pIO))
            iQDCV = -iQDCV;
        pMBInfo->iBlockDC[2][0] = iQDCV;
    }

    UpdateModelMB (cf, iChannels, aLaplacianMean, &(pContext->m_aModelDC));
    
    if(((!(pSC->WMISCP.bfBitstreamFormat != FREQUENCY || pSC->m_Dparam->cThumbnailScale < 16)) || pSC->WMISCP.sbSubband == SB_DC_ONLY) && pSC->m_bResetContext){
        Int kk;
        for (kk = 2; kk < 5; kk++) {
            if (ICERR_OK != AdaptDecFixed (pContext->m_pAHexpt[kk])) {
                return ICERR_ERROR;
            }
        }
    }

    return ICERR_OK;
}

/*************************************************************************
    DecodeMacroblockHighpass
*************************************************************************/
Int DecodeMacroblockHighpass (CWMImageStrCodec *pSC, CCodingContext *pContext, 
                      Int iMBX, Int iMBY)
{   
    /** reset adaptive scan totals **/
    if (pSC->m_bResetRGITotals) {
        int iScale = 2, k;
        int iWeight = iScale * 16;
        pContext->m_aScanHoriz[0].uTotal = pContext->m_aScanVert[0].uTotal = MAXTOTAL;
        for (k = 1; k < 16; k++) {
            pContext->m_aScanHoriz[k].uTotal = pContext->m_aScanVert[k].uTotal = iWeight;
            iWeight -= iScale;
        }
    }
    if((pSC->WMISCP.bfBitstreamFormat != SPATIAL) && (pSC->pTile[pSC->cTileColumn].cBitsHP > 0)) { // MB-based HP QP index
        pSC->MBInfo.iQIndexHP = decodeQPIndex(pContext->m_pIOAC, pSC->pTile[pSC->cTileColumn].cBitsHP);
        if (pSC->MBInfo.iQIndexHP >= pSC->pTile[pSC->cTileColumn].cNumQPHP)
            goto ErrorExit;
    }
    else if(pSC->pTile[pSC->cTileColumn].cBitsHP == 0 && pSC->pTile[pSC->cTileColumn].cNumQPHP > 1) // use LP QP
        pSC->MBInfo.iQIndexHP = pSC->MBInfo.iQIndexLP;


    DecodeCBP (pSC, pContext);
    predCBPDec(pSC, pContext);

    if (DecodeCoeffs (pSC, pContext, iMBX, iMBY, 
        pContext->m_pIOAC, pContext->m_pIOFL) != ICERR_OK)
        goto ErrorExit;

    if (pSC->m_bResetContext) {
        AdaptHighpassDec(pContext);
    }

    return ICERR_OK;
ErrorExit:
    return ICERR_ERROR;
}

/*************************************************************************
    Adapt
*************************************************************************/
Int AdaptLowpassDec(CCodingContext * pSC)
{
    Int kk;
    for (kk = 0; kk < CONTEXTX + CTDC; kk++) {
        if (ICERR_OK != AdaptDecFixed (pSC->m_pAHexpt[kk])) {
            goto ErrorExit;
        }
    }
    return ICERR_OK;

ErrorExit:
    return ICERR_ERROR;

}

Int AdaptHighpassDec(CCodingContext * pSC)
{
    Int kk;
    if (ICERR_OK != AdaptDecFixed (pSC->m_pAdaptHuffCBPCY)) {
        goto ErrorExit;



( run in 0.454 second using v1.01-cache-2.11-cpan-71847e10f99 )