Ejemplo n.º 1
0
Int32 init_sbr_dec(Int32 codecSampleRate,
                   Int   upsampleFac,
                   SBR_DEC *sbrDec,
                   SBR_FRAME_DATA *hFrameData)
{
    Int32 outFrameSize;
    Int32 coreCodecFrameSize = 1024;
#ifdef HQ_SBR
    Int32 i;
#endif


    sbrDec->sbStopCodec    =  upsampleFac << 5;
    sbrDec->prevLowSubband =  upsampleFac << 5;


    /* set sbr sampling frequency */
    sbrDec->outSampleRate = 2 * codecSampleRate;
    outFrameSize = upsampleFac * coreCodecFrameSize;

    hFrameData->nSfb[LO] = 0;    /* number of scale factor bands for high resp.low frequency resolution */
    hFrameData->nSfb[HI] = 0;
    hFrameData->offset   = 0;

    hFrameData->nNfb = hFrameData->sbr_header.noNoiseBands;
    hFrameData->prevEnvIsShort = -1;

    /* Initializes pointers */
#ifdef HQ_SBR
    for (i = 0; i < 5; i++)
    {
        hFrameData->fBuf_man[i]  = hFrameData->fBuffer_man[i];
        hFrameData->fBufN_man[i] = hFrameData->fBufferN_man[i];
        hFrameData->fBuf_exp[i]  = hFrameData->fBuffer_exp[i];
        hFrameData->fBufN_exp[i] = hFrameData->fBufferN_exp[i];
    }
#endif


    pv_memset((void *)hFrameData->sbr_invf_mode_prev,
              0,
              MAX_NUM_NOISE_VALUES*sizeof(INVF_MODE));

    /* Direct assignments */

    sbrDec->noCols = 32;

    sbrDec->bufWriteOffs = 6 + 2;
    sbrDec->bufReadOffs  = 2;
    sbrDec->qmfBufLen = sbrDec->noCols + sbrDec->bufWriteOffs;

    sbrDec->lowBandAddSamples = 288;

    sbrDec->startIndexCodecQmf = 0;

    sbrDec->lowSubband =  32;


    return outFrameSize;
}
Ejemplo n.º 2
0
void differential_Decoding(Int32 enable,
                           Int32 *aIndex,
                           Int32 *aPrevFrameIndex,
                           Int32 DtDf,
                           Int32 nrElements,
                           Int32 stride,
                           Int32 minIdx,
                           Int32 maxIdx)
{
    Int32 i;
    Int32 *ptr_aIndex;

    if (enable == 1)
    {
        ptr_aIndex = aIndex;

        if (DtDf == 0)
        {
            *(ptr_aIndex) = limitMinMax(*ptr_aIndex, minIdx, maxIdx);
            ptr_aIndex++;

            for (i = 1; i < nrElements; i++)
            {
                *(ptr_aIndex) = limitMinMax(aIndex[i-1] + *ptr_aIndex, minIdx, maxIdx);
                ptr_aIndex++;
            }
        }
        else
        {
            if (stride == 1)
            {
                for (i = 0; i < nrElements; i++)
                {
                    *(ptr_aIndex) = limitMinMax(aPrevFrameIndex[i] + *ptr_aIndex, minIdx, maxIdx);
                    ptr_aIndex++;
                }
            }
            else
            {
                for (i = 0; i < nrElements; i++)
                {
                    *(ptr_aIndex) = limitMinMax(aPrevFrameIndex[(i<<1)] + *ptr_aIndex, minIdx, maxIdx);
                    ptr_aIndex++;
                }
            }
        }
    }
    else
    {
        pv_memset((void *)aIndex, 0, nrElements*sizeof(*aIndex));
    }
    if (stride == 2)
    {
        for (i = (nrElements << 1) - 1; i > 0; i--)
        {
            aIndex[i] = aIndex[(i>>1)];
        }
    }
}
//============================================================================
//函数功能:设置静态存储器
//函数参数:"mem"是一个指针,用来存放数据,作为输出参数
//============================================================================
void dec_gain2_amr_wb_init(int16 * mem)
{

    /* 4nd order quantizer energy predictor (init to -14.0 in Q10) */
    mem[0] = -14336;                          /* past_qua_en[0] */
    mem[1] = -14336;                          /* past_qua_en[1] */
    mem[2] = -14336;                          /* past_qua_en[2] */
    mem[3] = -14336;                          /* past_qua_en[3] */
    /* 4  *past_gain_pit  */
    /* 5  *past_gain_code  */
    /* 6  *prev_gc  */
    /* next 5  pbuf[]  */
    /* next 5  gbuf[]  */
    /* next 5  pbuf2[]  */
    pv_memset((void *)&mem[4], 0, 18*sizeof(*mem));

    mem[22] = 21845;

}
Ejemplo n.º 4
0
void sbr_open(Int32 sampleRate,
              SBR_DEC *sbrDec,
              SBRDECODER_DATA * self,
              Bool bDownSampledSbr)

{
    Int16 i ;

    SBR_CHANNEL *SbrChannel;


    SbrChannel = self->SbrChannel;

    for (i = 0; i < MAX_NUM_CHANNELS; i++)
    {
        pv_memset((void *)&(SbrChannel[i]),
                  0,
                  sizeof(SBR_CHANNEL));

        /* init a default header such that we can at least do upsampling later */

        pv_memcpy(&(SbrChannel[i].frameData.sbr_header),
                  &defaultHeader,
                  sizeof(SBR_HEADER_DATA));

        /* should be handled by sample rate mode bit */
        if (sampleRate > 24000 || bDownSampledSbr)
        {
            SbrChannel[i].frameData.sbr_header.sampleRateMode = SINGLE_RATE;
        }


        SbrChannel[i].outFrameSize =
            init_sbr_dec(sampleRate,
                         self->SbrChannel[0].frameData.sbr_header.sampleRateMode,
                         sbrDec,
                         &(SbrChannel[i].frameData));

        SbrChannel[i].syncState     = UPSAMPLING;

        SbrChannel[i].frameData.sUp = 1;        /* reset mode */
    }
}
Ejemplo n.º 5
0
void dec_acelp_4p_in_64(
    int16 index[],  /* (i) : index (20): 5+5+5+5 = 20 bits.                 */
    /* (i) : index (36): 9+9+9+9 = 36 bits.                 */
    /* (i) : index (44): 13+9+13+9 = 44 bits.               */
    /* (i) : index (52): 13+13+13+13 = 52 bits.             */
    /* (i) : index (64): 2+2+2+2+14+14+14+14 = 64 bits.     */
    /* (i) : index (72): 10+2+10+2+10+14+10+14 = 72 bits.   */
    /* (i) : index (88): 11+11+11+11+11+11+11+11 = 88 bits. */
    int16 nbbits,   /* (i) : 20, 36, 44, 52, 64, 72 or 88 bits              */
    int16 code[]    /* (o) Q9: algebraic (fixed) codebook excitation        */
)
{
    int16 k, pos[6];
    int32 L_index;
    pv_memset(code, 0, L_CODE*sizeof(*code));

    /* decode the positions and signs of pulses and build the codeword */


    switch (nbbits)
    {
        case 20:
            for (k = 0; k < NB_TRACK; k++)
            {
                L_index = index[k];
                dec_1p_N1(L_index, 4, 0, pos);
                add_pulses(pos, 1, k, code);
            }
            break;

        case  36:
            for (k = 0; k < NB_TRACK; k++)
            {
                L_index = index[k];
                dec_2p_2N1(L_index, 4, 0, pos);
                add_pulses(pos, 2, k, code);
            }
            break;
        case 44:
            for (k = 0; k < NB_TRACK - 2; k++)
            {
                L_index = index[k];
                dec_3p_3N1(L_index, 4, 0, pos);
                add_pulses(pos, 3, k, code);
            }
            for (k = 2; k < NB_TRACK; k++)
            {
                L_index = index[k];
                dec_2p_2N1(L_index, 4, 0, pos);
                add_pulses(pos, 2, k, code);
            }
            break;
        case 52:
            for (k = 0; k < NB_TRACK; k++)
            {
                L_index = index[k];
                dec_3p_3N1(L_index, 4, 0, pos);
                add_pulses(pos, 3, k, code);
            }
            break;
        case 64:
            for (k = 0; k < NB_TRACK; k++)
            {
                L_index = ((int32)index[k] << 14) + index[k + NB_TRACK];
                dec_4p_4N(L_index, 4, 0, pos);
                add_pulses(pos, 4, k, code);
            }
            break;
        case 72:
            for (k = 0; k < NB_TRACK - 2; k++)
            {
                L_index = ((int32)index[k] << 10) + index[k + NB_TRACK];
                dec_5p_5N(L_index, 4, 0, pos);
                add_pulses(pos, 5, k, code);
            }
            for (k = 2; k < NB_TRACK; k++)
            {
                L_index = ((int32)index[k] << 14) + index[k + NB_TRACK];
                dec_4p_4N(L_index, 4, 0, pos);
                add_pulses(pos, 4, k, code);
            }
            break;
        case 88:
            for (k = 0; k < NB_TRACK; k++)
            {
                L_index = ((int32)index[k] << 11) + index[k + NB_TRACK];
                dec_6p_6N_2(L_index, 4, 0, pos);
                add_pulses(pos, 6, k, code);
            }
        default:
            break;
    }


}
Ejemplo n.º 6
0
/*----------------------------------------------------------------------------
; FUNCTION CODE
----------------------------------------------------------------------------*/
Int getmask(
    FrameInfo   *pFrameInfo,
    BITS        *pInputStream,
    Int         group[],
    Int         max_sfb,
    Int         mask[])
{

    Int     win; /* window index */
    Int     sfb;
    Int     mask_present;
    Int    *pMask;
    Int    *pGroup;
    Int     nwin;
    Int     nCall;
    Int     nToDo;
    UInt32  tempMask;
    UInt32  bitmask;

    pMask  = mask;
    pGroup = group;

    mask_present =
        get9_n_lessbits(
            LEN_MASK_PRES,
            pInputStream);

    switch (mask_present)
    {
        case(MASK_NOT_PRESENT):
            /* special EXTENDED_MS_MASK cases */
            /* no ms at all */
            break;

        case(MASK_ALL_FRAME):
            /* MS for whole spectrum on, mask bits set to 1 */
            nwin = pFrameInfo->num_win;
            for (win = 0; win < nwin; win = *(pGroup++))
            {
                for (sfb = pFrameInfo->sfb_per_win[win]; sfb > 0; sfb--)
                {
                    *(pMask++) = 1; /* cannot use memset for Int type */
                }

            }

            break;

        case(MASK_FROM_BITSTREAM):
            /* MS_mask_present==1, get mask information*/
            nwin = pFrameInfo->num_win;
            for (win = 0; win < nwin; win = *(pGroup++))
            {
                /*
                 * the following code is equivalent to
                 *
                 * for(sfb = max_sfb; sfb > 0; sfb--)
                 * {
                 *   *(pMask++) =
                 *       getbits(
                 *           LEN_MASK,
                 *           pInputStream);
                 * }
                 *
                 * in order to save the calls to getbits, the above
                 * for-loop is broken into two parts
                 */

                nToDo = max_sfb;

                while (nToDo > 0)
                {
                    nCall = nToDo;

                    if (nCall > MAX_GETBITS)
                    {
                        nCall = MAX_GETBITS;
                    }

                    tempMask =
                        getbits(
                            nCall,
                            pInputStream);

                    bitmask = (UInt32) 1 << (nCall - 1);
                    for (sfb = nCall; sfb > 0; sfb--)
                    {
                        *(pMask++) = (Int)((tempMask & bitmask) >> (sfb - 1));
                        bitmask >>= 1;
                    }

                    nToDo -= nCall;
                }

                /*
                 * set remaining sfbs to zero
                 * re-use nCall to save one variable on stack
                 */

                nCall = pFrameInfo->sfb_per_win[win] - max_sfb;


                if (nCall >= 0)
                {
                    pv_memset(pMask,
                              0,
                              nCall*sizeof(*pMask));

                    pMask += nCall;
                }
                else
                {
                    mask_present = MASK_ERROR;
                    break;
                }


            } /* for (win) */

            break;

        default:
            /* error */
            break;

    } /* switch (mask_present) */

    return mask_present;

} /* getmask */
Ejemplo n.º 7
0
SBR_ERROR sbr_get_cpe(SBR_FRAME_DATA * hFrameDataLeft,
                      SBR_FRAME_DATA * hFrameDataRight,
                      BIT_BUFFER  * hBitBuf)
{
    Int32 i;
    Int32 bits;
    SBR_ERROR err =  SBRDEC_OK;

    /* reserved bits */
    bits = buf_getbits(hBitBuf, SI_SBR_RESERVED_PRESENT);

    if (bits)
    {
        buf_getbits(hBitBuf, SI_SBR_RESERVED_BITS_DATA);
        buf_getbits(hBitBuf, SI_SBR_RESERVED_BITS_DATA);
    }

    /* Read coupling flag */
    bits = buf_getbits(hBitBuf, SI_SBR_COUPLING_BITS);

    if (bits)
    {
        hFrameDataLeft->coupling = COUPLING_LEVEL;
        hFrameDataRight->coupling = COUPLING_BAL;
    }
    else
    {
        hFrameDataLeft->coupling = COUPLING_OFF;
        hFrameDataRight->coupling = COUPLING_OFF;
    }


    err = extractFrameInfo(hBitBuf, hFrameDataLeft);

    if (err != SBRDEC_OK)
    {
        return err;
    }

    if (hFrameDataLeft->coupling)
    {

        pv_memcpy(hFrameDataRight->frameInfo,
                  hFrameDataLeft->frameInfo,
                  LENGTH_FRAME_INFO * sizeof(Int32));

        hFrameDataRight->nNoiseFloorEnvelopes = hFrameDataLeft->nNoiseFloorEnvelopes;
        hFrameDataRight->frameClass = hFrameDataLeft->frameClass;


        sbr_get_dir_control_data(hFrameDataLeft, hBitBuf);
        sbr_get_dir_control_data(hFrameDataRight, hBitBuf);

        for (i = 0; i < hFrameDataLeft->nNfb; i++)
        {
            hFrameDataLeft->sbr_invf_mode_prev[i]  = hFrameDataLeft->sbr_invf_mode[i];
            hFrameDataRight->sbr_invf_mode_prev[i] = hFrameDataRight->sbr_invf_mode[i];

            hFrameDataLeft->sbr_invf_mode[i]  = (INVF_MODE) buf_getbits(hBitBuf, SI_SBR_INVF_MODE_BITS);
            hFrameDataRight->sbr_invf_mode[i] = hFrameDataLeft->sbr_invf_mode[i];
        }

        sbr_get_envelope(hFrameDataLeft, hBitBuf);
        sbr_get_noise_floor_data(hFrameDataLeft, hBitBuf);
        sbr_get_envelope(hFrameDataRight, hBitBuf);

    }
    else
    {
        err = extractFrameInfo(hBitBuf, hFrameDataRight);

        if (err != SBRDEC_OK)
        {
            return err;
        }


        sbr_get_dir_control_data(hFrameDataLeft,  hBitBuf);
        sbr_get_dir_control_data(hFrameDataRight, hBitBuf);

        for (i = 0; i <  hFrameDataLeft->nNfb; i++)
        {
            hFrameDataLeft->sbr_invf_mode_prev[i]  = hFrameDataLeft->sbr_invf_mode[i];
            hFrameDataLeft->sbr_invf_mode[i]  =
                (INVF_MODE) buf_getbits(hBitBuf, SI_SBR_INVF_MODE_BITS);
        }

        for (i = 0; i <  hFrameDataRight->nNfb; i++)
        {
            hFrameDataRight->sbr_invf_mode_prev[i] = hFrameDataRight->sbr_invf_mode[i];

            hFrameDataRight->sbr_invf_mode[i] =
                (INVF_MODE) buf_getbits(hBitBuf, SI_SBR_INVF_MODE_BITS);
        }
        sbr_get_envelope(hFrameDataLeft,  hBitBuf);
        sbr_get_envelope(hFrameDataRight, hBitBuf);

        sbr_get_noise_floor_data(hFrameDataLeft,  hBitBuf);

    }

    sbr_get_noise_floor_data(hFrameDataRight, hBitBuf);

    pv_memset((void *)hFrameDataLeft->addHarmonics,
              0,
              hFrameDataLeft->nSfb[HI]*sizeof(Int32));

    pv_memset((void *)hFrameDataRight->addHarmonics,
              0,
              hFrameDataRight->nSfb[HI]*sizeof(Int32));

    sbr_get_additional_data(hFrameDataLeft, hBitBuf);
    sbr_get_additional_data(hFrameDataRight, hBitBuf);

    sbr_extract_extended_data(hBitBuf
// 2010.01.26 : 
// in order to detect ps tag in adts/adif but decoding it
// we remove this definition
////#ifdef PARAMETRICSTEREO
                              , NULL
////#endif
                             );

    return SBRDEC_OK;

}
Ejemplo n.º 8
0
SBR_HEADER_STATUS sbr_get_header_data(SBR_HEADER_DATA   * h_sbr_header,
                                      BIT_BUFFER          * hBitBuf,
                                      SBR_SYNC_STATE     syncState)
{
    SBR_HEADER_DATA lastHeader;
    Int32 headerExtra1, headerExtra2;


    /* Copy header to temporary header */
    if (syncState == SBR_ACTIVE)
    {
        pv_memcpy(&lastHeader, h_sbr_header, sizeof(SBR_HEADER_DATA));
    }
    else
    {
        pv_memset((void *)&lastHeader, 0, sizeof(SBR_HEADER_DATA));
    }


    /* Read new header from bitstream */
    h_sbr_header->ampResolution   = buf_getbits(hBitBuf, SI_SBR_AMP_RES_BITS);
    h_sbr_header->startFreq       = buf_getbits(hBitBuf, SI_SBR_START_FREQ_BITS);
    h_sbr_header->stopFreq        = buf_getbits(hBitBuf, SI_SBR_STOP_FREQ_BITS);
    h_sbr_header->xover_band      = buf_getbits(hBitBuf, SI_SBR_XOVER_BAND_BITS);

    buf_getbits(hBitBuf, SI_SBR_RESERVED_BITS_HDR);

    headerExtra1    = buf_getbits(hBitBuf, SI_SBR_HEADER_EXTRA_1_BITS);
    headerExtra2    = buf_getbits(hBitBuf, SI_SBR_HEADER_EXTRA_2_BITS);

    /* handle extra header information */
    if (headerExtra1)
    {
        h_sbr_header->freqScale   = buf_getbits(hBitBuf, SI_SBR_FREQ_SCALE_BITS);
        h_sbr_header->alterScale  = buf_getbits(hBitBuf, SI_SBR_ALTER_SCALE_BITS);
        h_sbr_header->noise_bands = buf_getbits(hBitBuf, SI_SBR_NOISE_BANDS_BITS);
    }
    else
    { /* Set default values.*/
        h_sbr_header->freqScale   = SBR_FREQ_SCALE_DEFAULT;
        h_sbr_header->alterScale  = SBR_ALTER_SCALE_DEFAULT;
        h_sbr_header->noise_bands = SBR_NOISE_BANDS_DEFAULT;
    }


    if (headerExtra2)
    {
        h_sbr_header->limiterBands    = buf_getbits(hBitBuf, SI_SBR_LIMITER_BANDS_BITS);
        h_sbr_header->limiterGains    = buf_getbits(hBitBuf, SI_SBR_LIMITER_GAINS_BITS);
        h_sbr_header->interpolFreq    = buf_getbits(hBitBuf, SI_SBR_INTERPOL_FREQ_BITS);
        h_sbr_header->smoothingLength = buf_getbits(hBitBuf, SI_SBR_SMOOTHING_LENGTH_BITS);
    }
    else
    { /* Set default values.*/
        h_sbr_header->limiterBands    = SBR_LIMITER_BANDS_DEFAULT;
        h_sbr_header->limiterGains    = SBR_LIMITER_GAINS_DEFAULT;
        h_sbr_header->interpolFreq    = SBR_INTERPOL_FREQ_DEFAULT;
        h_sbr_header->smoothingLength = SBR_SMOOTHING_LENGTH_DEFAULT;
    }

    if (syncState == SBR_ACTIVE)
    {
        h_sbr_header->status = HEADER_OK;

        /* look for new settings */
        if (lastHeader.startFreq   != h_sbr_header->startFreq   ||
                lastHeader.stopFreq    != h_sbr_header->stopFreq    ||
                lastHeader.xover_band  != h_sbr_header->xover_band  ||
                lastHeader.freqScale   != h_sbr_header->freqScale   ||
                lastHeader.alterScale  != h_sbr_header->alterScale  ||
                lastHeader.noise_bands != h_sbr_header->noise_bands)
        {
            h_sbr_header->status = HEADER_RESET;
        }
    }
    else
    {
        h_sbr_header->status = HEADER_RESET;
    }

    return h_sbr_header->status;
}
void ps_bstr_decoding(STRUCT_PS_DEC *ps_dec)
{
    UInt32 env;
    Int32 noIidSteps;

    if (!ps_dec->bPsDataAvail)
    {
        ps_dec->noEnv = 0;
    }

    noIidSteps = ps_dec->bFineIidQ ? NO_IID_STEPS_FINE : NO_IID_STEPS;

    for (env = 0; env < ps_dec->noEnv; env++)
    {
        Int32 *aPrevIidIndex;
        Int32 *aPrevIccIndex;
        if (env == 0)
        {
            aPrevIidIndex = ps_dec->aIidPrevFrameIndex;
            aPrevIccIndex = ps_dec->aIccPrevFrameIndex;
        }
        else
        {
            aPrevIidIndex = ps_dec->aaIidIndex[env-1];
            aPrevIccIndex = ps_dec->aaIccIndex[env-1];
        }

        /*
         * Differential Decoding of IID parameters over time/frequency
         */
        differential_Decoding(ps_dec->bEnableIid,
                              ps_dec->aaIidIndex[env],
                              aPrevIidIndex,
                              ps_dec->abIidDtFlag[env],
                              aNoIidBins[ps_dec->freqResIid],
                              (ps_dec->freqResIid) ? 1 : 2,
                              -noIidSteps,
                              noIidSteps);

        /*
         * Differential Decoding of ICC parameters over time/frequency
         */
        differential_Decoding(ps_dec->bEnableIcc,
                              ps_dec->aaIccIndex[env],
                              aPrevIccIndex,
                              ps_dec->abIccDtFlag[env],
                              aNoIccBins[ps_dec->freqResIcc],
                              (ps_dec->freqResIcc) ? 1 : 2,
                              0,
                              NO_ICC_STEPS - 1);


    }   /* for (env=0; env<ps_dec->noEnv; env++) */

    if (ps_dec->noEnv == 0)
    {
        ps_dec->noEnv = 1;

        if (ps_dec->bEnableIid)
        {   /*  NO_HI_RES_BINS == 34 */
            pv_memmove(ps_dec->aaIidIndex[ps_dec->noEnv-1],
                       ps_dec->aIidPrevFrameIndex,
                       NO_HI_RES_BINS*sizeof(*ps_dec->aIidPrevFrameIndex));

        }
        else
        {
            pv_memset((void *)ps_dec->aaIidIndex[ps_dec->noEnv-1],
                      0,
                      NO_HI_RES_BINS*sizeof(**ps_dec->aaIidIndex));
        }
        if (ps_dec->bEnableIcc)
        {
            pv_memmove(ps_dec->aaIccIndex[ps_dec->noEnv-1],
                       ps_dec->aIccPrevFrameIndex,
                       NO_HI_RES_BINS*sizeof(*ps_dec->aIccPrevFrameIndex));
        }
        else
        {
            pv_memset((void *)ps_dec->aaIccIndex[ps_dec->noEnv-1],
                      0,
                      NO_HI_RES_BINS*sizeof(**ps_dec->aaIccIndex));
        }
    }

    pv_memmove(ps_dec->aIidPrevFrameIndex,
               ps_dec->aaIidIndex[ps_dec->noEnv-1],
               NO_HI_RES_BINS*sizeof(*ps_dec->aIidPrevFrameIndex));

    pv_memmove(ps_dec->aIccPrevFrameIndex,
               ps_dec->aaIccIndex[ps_dec->noEnv-1],
               NO_HI_RES_BINS*sizeof(*ps_dec->aIccPrevFrameIndex));

    ps_dec->bPsDataAvail = 0;

    if (ps_dec->bFrameClass == 0)
    {
        Int32 shift;

        shift = ps_dec->noEnv >> 1;

        ps_dec->aEnvStartStop[0] = 0;

        for (env = 1; env < ps_dec->noEnv; env++)
        {
            ps_dec->aEnvStartStop[env] =
                (env * ps_dec->noSubSamples) >> shift;
        }

        ps_dec->aEnvStartStop[ps_dec->noEnv] = ps_dec->noSubSamples;
    }
Ejemplo n.º 10
0
void low_pass_filt_7k_init(int16 mem[])            /* mem[30] */
{
    pv_memset((void *)mem, 0, (L_FIR)*sizeof(*mem));

    return;
}
Ejemplo n.º 11
0
Int huffspec_fxp(
    FrameInfo *pFrameInfo,
    BITS      *pInputStream,
    Int       nsect,
    SectInfo  *pSectInfo,
    Int       factors[],
    Int32     coef[],
    Int16     quantSpec[],
    Int16     tmp_spec[],
    const FrameInfo  *pLongFrameInfo,
    PulseInfo  *pPulseInfo,
    Int         qFormat[])
{
    /*----------------------------------------------------------------------------
    ; Define all local variables
    ----------------------------------------------------------------------------*/
    const Hcb       *pHcb;
    Int     i;
    Int     sfb;
    Int     idx_count;
    Int     sect_cb;    /* section codebook */
    Int     dim;
    Int     idx;
    Int     stop_idx;     /* index of 1st coef in next sfb */
    Int     sect_start;   /* start index of sfb in one section*/
    Int     sect_end;     /* index of 1st sfb in next section */
    Int     *pSfbStart;
    Int     *pSfb;
    Int16     *pQuantSpec;        /* probably could be short */
    Int     max = 0;
    /* rescaling parameters */
    Int     nsfb;
    Int     tot_sfb;
    Int     fac;

    Int32   *pCoef; /* ptr to coef[], inverse quantized coefs */
    UInt16     scale;

    Int     power_scale_div_4;
    Int     sfbWidth;

    void (*pUnpack_idx)(
        Int16  quant_spec[],
        Int  codeword_indx,
        const Hcb *pHuffCodebook,
        BITS  *pInputStream,
        Int *max);

    Int(*pDec_huff_tab)(BITS *) = NULL;

    UInt32 temp;
    Int    binaryDigits, QFormat;

    /*----------------------------------------------------------------------------
    ; Function body here
    ----------------------------------------------------------------------------*/

    sect_start = 0;
    stop_idx   = 0;

    /* pSfb: ptr to array that holds stop index of each sfb */
    pSfbStart = pFrameInfo->frame_sfb_top;
// 2010.01.11 :
// nsect can be 0
    if (pSfbStart == NULL)
    {
        return (-1);   /*  error condition */
    }

    pSfb      = pSfbStart;

    /* decoding spectral values section by section */
    for (i = nsect; i > 0; i--)
    {
        /* read the codebook and section length */
        sect_cb  =  pSectInfo->sect_cb;     /* codebook */
        if ((sect_cb > 15) || (sect_cb < 0))
        {
            return (-1);   /*  error condition */
        }
        sect_end =  pSectInfo->sect_end;    /* # of sfbs */

        if (sect_end < 0)
        {
            return (-1);   /*  error condition */
        }

        pSectInfo++;

        /*  sect_cb       sect_cb - 1
         *  ZERO_HCB        1111b
         *    1             0000b
         *    2             0001b
         *    3             0010b
         *    4             0011b
         *    5             0100b
         *    6             0101b
         *    7             0110b
         *    8             0111b
         *    9             1000b
         *    10            1001b
         *    11            1010b
         *    12            1011b
         * NOISE_HCB        1100b
         * INTENSITY_HCB2   1101b
         * INTENSITY_HCB    1110b
         * if ( ((sect_cb - 1) & 0xC) == 0xC ) is identical to
         * if !((sect_cb == ZERO_HCB) || (sect_cb == NOISE_HCB) ||
         *      (sec_cb == INTENSITY_HCB) || (sect_cb==INTENSITY_HCB2) )
         * use this compare scheme to speed up the execution
         */

        if (((sect_cb - 1) & 0xC) != 0xC)
        {
            /* decode spec in one section */
            if (sect_cb > BY4BOOKS)
            {
                dim = DIMENSION_2; /* set codebook dimension */
            }
            else
            {
                dim = DIMENSION_4;
            }

            pHcb        = &hcbbook_binary[sect_cb];

            if (sect_cb == ESCBOOK)
            {
                pUnpack_idx = &unpack_idx_esc;
            }
            else if (pHcb->signed_cb == FALSE)
            {
                pUnpack_idx = &unpack_idx_sgn;
            }
            else
            {
                pUnpack_idx = &unpack_idx;
            }


            switch (sect_cb)
            {
                case 1:
                    pDec_huff_tab = decode_huff_cw_tab1;
                    break;
                case 2:
                    pDec_huff_tab = decode_huff_cw_tab2;
                    break;
                case 3:
                    pDec_huff_tab = decode_huff_cw_tab3;
                    break;
                case 4:
                    pDec_huff_tab = decode_huff_cw_tab4;
                    break;
                case 5:
                    pDec_huff_tab = decode_huff_cw_tab5;
                    break;
                case 6:
                    pDec_huff_tab = decode_huff_cw_tab6;
                    break;
                case 7:
                    pDec_huff_tab = decode_huff_cw_tab7;
                    break;
                case 8:
                    pDec_huff_tab = decode_huff_cw_tab8;
                    break;
                case 9:
                    pDec_huff_tab = decode_huff_cw_tab9;
                    break;
                case 10:
                    pDec_huff_tab = decode_huff_cw_tab10;
                    break;
                case 11:
                    pDec_huff_tab = decode_huff_cw_tab11;
                    break;
                default:
                    return (-1); /* error condition */
            }

            /* move ptr to first sfb of current section */
            pQuantSpec  = quantSpec + stop_idx;

            /* step through all sfbs in current section */
            for (sfb = sect_start; sfb < sect_end; sfb++)
            {
                idx_count = *pSfb - stop_idx;
                stop_idx  = *pSfb++;

                /* decode all coefs for one sfb */
                while ((idx_count > 0) && (idx_count < 1024))
                {

                    idx = (*pDec_huff_tab)(pInputStream);

                    (*pUnpack_idx)(pQuantSpec,
                                   idx,
                                   pHcb,
                                   pInputStream,
                                   &max);      /* unpack idx -> coefs */

                    pQuantSpec += dim;
                    idx_count  -= dim;

                } /* while(idx_count) */

            } /* for (sfb=sect_start) */
        }
        else
        {

            /* current section uses ZERO_HCB, NOISE_HCB, etc */

            /* move sfb pointer to the start sfb of next section */
            pSfb        = pSfbStart + sect_end;
            /* number of coefs in current section */
            idx_count   = *(pSfb - 1) - stop_idx;

            if ((idx_count > 1024) || (idx_count < 0))
            {
                return (-1);   /*  error condition */
            }

            /*
             * This memset is necessary in terms of (1) net savings in total
             * MIPS and (2) accurate Q-Formats for fft_rx2
             * In case a scalefactor band uses ZERO_HCB, all coefficients of
             * that sfb should be zeros. Without this call to memset, the
             * coefficients for a ZERO_HCB sfb are the "leftovers" of the
             * previous frame, which may not have all zero values. This leads
             * to a drastical increase in the cycles consumed by esc_iquant_fxp
             * and fft_rx2, which is the most "expensive" function of the
             * library.
             * This memset also guarantees the Q_Format for sfbs with all zero
             * coefficients will be set properly.
             * Profiling data on ARM and TMS320C55x proves that there is a net
             * gain in total MIPS if a memset is called here.
             */
            pv_memset(&quantSpec[stop_idx],
                      0,
                      idx_count * sizeof(quantSpec[0]));

            /*
             * This memset is called because pQuantSpec points to tmp_spec
             * after deinterleaving
             */

            pv_memset(&tmp_spec[stop_idx],
                      0,
                      idx_count * sizeof(tmp_spec[0]));


            /* stop_idx is the index of the 1st coef of next section */
            stop_idx    = *(pSfb - 1);

        }/* if (sect_cb) */

        sect_start = sect_end;

    } /* for (i=nsect) */

    /* noisless coding reconstruction */
    if (pFrameInfo->islong != FALSE)
    {
        if (pPulseInfo->pulse_data_present == 1)
        {
            pulse_nc(quantSpec,
                     pPulseInfo,
                     pLongFrameInfo,
                     &max);    /* add pulse data */
        }

        pQuantSpec = quantSpec;

    }
    else
    {
        deinterleave(quantSpec,
                     tmp_spec,
                     pFrameInfo);

        pQuantSpec = tmp_spec;
    }


    /* inverse quantization, Q_format: Int32 */
    /* rescaling */

    /* what we can do here is assuming that we already know maxInput for each band, we have to go
    though each one of them for re-quant and scaling, and pick the right qFormat to apply to
    all spectral coeffs.*/

    if ((max < 0) || (max > 8192))    /* (8192>>ORDER) == 1024 is the inverseQuantTable size */
    {
        return (-1);   /*  error condition */
    }
    else
    {
        /* Get  (max/SPACING) ^ (1/3), in Q Format  */
        temp = inverseQuantTable[(max >> ORDER) + 1];
    }


    /* Round up before shifting down to Q0 */
    temp += ROUND_UP;

    /* shift down to Q0 and multiply by 2 (FACTOR) in one step */
    temp >>= (QTABLE - 1);

    /* Now get max ^ (4/3) in Q0 */
    temp *= max;


    binaryDigits = 31 - pv_normalize(temp);


    /* Prevent negative shifts caused by low maximums. */
    if (binaryDigits < (SIGNED32BITS - QTABLE))
    {
        binaryDigits = SIGNED32BITS - QTABLE;
    }

    QFormat = SIGNED32BITS - binaryDigits;

    /********************/
    tot_sfb = 0;
    nsfb = pFrameInfo->sfb_per_win[0];
    pCoef = coef;

    for (i = pFrameInfo->num_win; i > 0; i--)
    {
        stop_idx  = 0;

        for (sfb = 0; sfb < nsfb; sfb++)
        {
            sfbWidth   =  pFrameInfo->win_sfb_top[0][sfb] - stop_idx;

            if ((sfbWidth < 0) || (sfbWidth > 1024))
            {
                return (-1);   /*  error condition */
            }

            stop_idx  += sfbWidth;

            fac   = factors[tot_sfb] - SF_OFFSET;
            scale = exptable[fac & 0x3];

            power_scale_div_4 = fac >> 2;

            power_scale_div_4++;

            qFormat[tot_sfb] = QFormat;

            esc_iquant_scaling(pQuantSpec,
                               pCoef,
                               sfbWidth,
                               QFormat,
                               scale,
                               max);

            pQuantSpec += sfbWidth;
            qFormat[tot_sfb] -= power_scale_div_4;
            pCoef += sfbWidth;

            tot_sfb++;

        } /* for (sfb) */
    } /* for (i) */


    /*----------------------------------------------------------------------------
    ; Return status
    ----------------------------------------------------------------------------*/
    return SUCCESS;

} /* huffspec_fxp */
Ejemplo n.º 12
0
void mime_unsorting(uint8 unsorted_bits[],
                    int16 sorted_bits_into_int16[],
                    int16 * frame_type,
                    int16 * mode,
                    uint8 quality,
                    RX_State_wb *st)
{

    int16 i;
    int16 j;
    uint8 temp = 0;
    uint8 *unsorted_bits_ptr = (uint8*)unsorted_bits;

    /* pointer table for bit sorting tables */
    const int16 *AmrWbSortingTables[16] =
    {
        sort_660,   sort_885, sort_1265, sort_1425,
        sort_1585,  sort_1825, sort_1985, sort_2305,
        sort_2385,   sort_SID,      NULL,      NULL,
        NULL,       NULL,      NULL,      NULL
    };

    const int16 * pt_AmrWbSortingTables  = AmrWbSortingTables[*mode];

    /* clear compressed speech bit buffer */
    pv_memset(sorted_bits_into_int16,
              0,
              unpacked_size[*mode]*sizeof(*sorted_bits_into_int16));

    /* unpack and unsort speech or SID bits */


    for (i = unpacked_size[*mode] >> 3; i != 0; i--)
    {
        temp = *(unsorted_bits_ptr++);

        for (j = 2; j != 0; j--)
        {
            switch (temp & 0xf0)
            {
                case 0xf0:
                    sorted_bits_into_int16[*(pt_AmrWbSortingTables++)] = BIT_1;
                    sorted_bits_into_int16[*(pt_AmrWbSortingTables++)] = BIT_1;
                    sorted_bits_into_int16[*(pt_AmrWbSortingTables++)] = BIT_1;
                    sorted_bits_into_int16[*(pt_AmrWbSortingTables++)] = BIT_1;
                    break;
                case 0xe0:
                    sorted_bits_into_int16[*(pt_AmrWbSortingTables++)] = BIT_1;
                    sorted_bits_into_int16[*(pt_AmrWbSortingTables++)] = BIT_1;
                    sorted_bits_into_int16[*(pt_AmrWbSortingTables++)] = BIT_1;
                    pt_AmrWbSortingTables++;
                    break;
                case 0xd0:
                    sorted_bits_into_int16[*(pt_AmrWbSortingTables++)] = BIT_1;
                    sorted_bits_into_int16[*(pt_AmrWbSortingTables++)] = BIT_1;
                    pt_AmrWbSortingTables++;
                    sorted_bits_into_int16[*(pt_AmrWbSortingTables++)] = BIT_1;
                    break;
                case 0xc0:
                    sorted_bits_into_int16[*(pt_AmrWbSortingTables++)] = BIT_1;
                    sorted_bits_into_int16[*(pt_AmrWbSortingTables++)] = BIT_1;
                    pt_AmrWbSortingTables += 2;
                    break;
                case 0xb0:
                    sorted_bits_into_int16[*(pt_AmrWbSortingTables++)] = BIT_1;
                    pt_AmrWbSortingTables++;
                    sorted_bits_into_int16[*(pt_AmrWbSortingTables++)] = BIT_1;
                    sorted_bits_into_int16[*(pt_AmrWbSortingTables++)] = BIT_1;
                    break;
                case 0xa0:
                    sorted_bits_into_int16[*(pt_AmrWbSortingTables++)] = BIT_1;
                    pt_AmrWbSortingTables++;
                    sorted_bits_into_int16[*(pt_AmrWbSortingTables++)] = BIT_1;
                    pt_AmrWbSortingTables++;
                    break;
                case 0x90:
                    sorted_bits_into_int16[*(pt_AmrWbSortingTables++)] = BIT_1;
                    pt_AmrWbSortingTables += 2;
                    sorted_bits_into_int16[*(pt_AmrWbSortingTables++)] = BIT_1;
                    break;
                case 0x80:
                    sorted_bits_into_int16[*(pt_AmrWbSortingTables++)] = BIT_1;
                    pt_AmrWbSortingTables += 3;
                    break;
                case 0x70:
                    pt_AmrWbSortingTables++;
                    sorted_bits_into_int16[*(pt_AmrWbSortingTables++)] = BIT_1;
                    sorted_bits_into_int16[*(pt_AmrWbSortingTables++)] = BIT_1;
                    sorted_bits_into_int16[*(pt_AmrWbSortingTables++)] = BIT_1;
                    break;
                case 0x60:
                    pt_AmrWbSortingTables++;
                    sorted_bits_into_int16[*(pt_AmrWbSortingTables++)] = BIT_1;
                    sorted_bits_into_int16[*(pt_AmrWbSortingTables++)] = BIT_1;
                    pt_AmrWbSortingTables++;
                    break;
                case 0x50:
                    pt_AmrWbSortingTables++;
                    sorted_bits_into_int16[*(pt_AmrWbSortingTables++)] = BIT_1;
                    pt_AmrWbSortingTables++;
                    sorted_bits_into_int16[*(pt_AmrWbSortingTables++)] = BIT_1;
                    break;
                case 0x40:
                    pt_AmrWbSortingTables++;
                    sorted_bits_into_int16[*(pt_AmrWbSortingTables++)] = BIT_1;
                    pt_AmrWbSortingTables += 2;
                    break;
                case 0x30:
                    pt_AmrWbSortingTables += 2;
                    sorted_bits_into_int16[*(pt_AmrWbSortingTables++)] = BIT_1;
                    sorted_bits_into_int16[*(pt_AmrWbSortingTables++)] = BIT_1;
                    break;
                case 0x20:
                    pt_AmrWbSortingTables += 2;
                    sorted_bits_into_int16[*(pt_AmrWbSortingTables++)] = BIT_1;
                    pt_AmrWbSortingTables++;
                    break;
                case 0x10:
                    pt_AmrWbSortingTables += 3;
                    sorted_bits_into_int16[*(pt_AmrWbSortingTables++)] = BIT_1;
                    break;
                default:
                    pt_AmrWbSortingTables += 4;
                    break;
            }
            temp <<= 4;
        }
    }

    if (unpacked_size[*mode] % 4)
    {
        temp <<= 1;

        if (temp & 0x80)
        {
            sorted_bits_into_int16[*(pt_AmrWbSortingTables++)] = BIT_1;
        }
    }

    /* set frame type */
    switch (*mode)
    {
        case MODE_7k:
        case MODE_9k:
        case MODE_12k:
        case MODE_14k:
        case MODE_16k:
        case MODE_18k:
        case MODE_20k:
        case MODE_23k:
        case MODE_24k:
            if (quality)
            {
                *frame_type = RX_SPEECH_GOOD;
            }
            else
            {
                *frame_type = RX_SPEECH_BAD;
            }
            break;

        case MRSID:
            if (quality)
            {
                if (temp & 0x80)
                {
                    *frame_type = RX_SID_UPDATE;
                }
                else
                {
                    *frame_type = RX_SID_FIRST;
                }
            }
            else
            {
                *frame_type = RX_SID_BAD;
            }

            /* set mode index */
            *mode = st->prev_mode;
            break;
        case 14:        /* SPEECH_LOST */
            *frame_type = RX_SPEECH_LOST;
            *mode = st->prev_mode;
            break;
        case 15:        /* NO_DATA */
            *frame_type = RX_NO_DATA;
            *mode = st->prev_mode;
            break;
        default:        /* replace frame with unused mode index by NO_DATA frame */
            *frame_type = RX_NO_DATA;
            *mode = st->prev_mode;
            break;
    }

    st->prev_mode = *mode;

}
ERROR_CODE pvmp3_framedecoder(tPVMP3DecoderExternal *pExt,
                              void              *pMem)
{

    ERROR_CODE        errorCode  = NO_DECODING_ERROR;

    int32   crc_error_count = 0;
    uint32  sent_crc = 0;
    uint32  computed_crc = 0;

    tmp3dec_chan   *pChVars[CHAN];
    tmp3dec_file   *pVars = (tmp3dec_file *)pMem;

    mp3Header info_data;
    mp3Header *info = &info_data;

    pVars->inputStream.pBuffer  = pExt->pInputBuffer;


    pVars->inputStream.usedBits  = pExt->inputBufferUsedLength << 3;
    pVars->inputStream.inputBufferCurrentLength  = pExt->inputBufferCurrentLength;


    errorCode = pvmp3_decode_header(&pVars->inputStream,
                                    info,
                                    &computed_crc);

    if (errorCode != NO_DECODING_ERROR)
    {
        pExt->outputFrameSize = 0;
        return errorCode;
    }

    pVars->num_channels = (info->mode == MPG_MD_MONO) ? 1 : 2;
    pExt->num_channels = pVars->num_channels;

    int32 outputFrameSize = (info->version_x == MPEG_1) ?
                            2 * SUBBANDS_NUMBER * FILTERBANK_BANDS :
                            SUBBANDS_NUMBER * FILTERBANK_BANDS;

    outputFrameSize = (info->mode == MPG_MD_MONO) ? outputFrameSize : outputFrameSize << 1;


    /*
     *  Check if output buffer has enough room to hold output PCM
     */
    if (pExt->outputFrameSize >= outputFrameSize)
    {
        pExt->outputFrameSize = outputFrameSize;
    }
    else
    {
        pExt->outputFrameSize = 0;
        return OUTPUT_BUFFER_TOO_SMALL;
    }


    pChVars[ LEFT] = &pVars->perChan[ LEFT];
    pChVars[RIGHT] = &pVars->perChan[RIGHT];




    if (info->error_protection)
    {
        /*
         *  Get crc content
         */
        sent_crc = getUpTo17bits(&pVars->inputStream, 16);
    }


    if (info->layer_description == 3)
    {
        int32 gr;
        int32 ch;
        uint32 main_data_end;
        int32 bytes_to_discard;
        int16 *ptrOutBuffer = pExt->pOutputBuffer;

        /*
         * Side Information must be extracted from the bitstream and store for use
         * during the decoded of the associated frame
         */

        errorCode = pvmp3_get_side_info(&pVars->inputStream,
                                        &pVars->sideInfo,
                                        info,
                                        &computed_crc);

        if (errorCode != NO_DECODING_ERROR)
        {
            pExt->outputFrameSize = 0;
            return errorCode;
        }

        /*
         *  If CRC was sent, check that matches the one got while parsing data
         *  disable crc if this is the desired mode
         */
        if (info->error_protection)
        {
            if ((computed_crc != sent_crc) && pExt->crcEnabled)
            {
                crc_error_count++;
            }
        }

        /*
         * main data (scalefactors, Huffman coded, etc,) are not necessarily located
         * adjacent to the side-info. Beginning of main data is located using
         * field "main_data_begin" of the current frame. The length does not include
         * header and side info.
         * "main_data_begin" points to the first bit of main data of a frame. It is a negative
         * offset in bytes from the first byte of the sync word
         * main_data_begin = 0  <===> main data start rigth after side info.
         */

        int32 temp = pvmp3_get_main_data_size(info, pVars);


        /*
         *  Check if available data holds a full frame, if not flag an error
         */

        if ((uint32)pVars->predicted_frame_size > pVars->inputStream.inputBufferCurrentLength)
        {
            pExt->outputFrameSize = 0;
            return NO_ENOUGH_MAIN_DATA_ERROR;
        }

        /*
         *  Fill in internal circular buffer
         */
        fillMainDataBuf(pVars, temp);


        main_data_end = pVars->mainDataStream.usedBits >> 3; /* in bytes */
        if ((main_data_end << 3) < pVars->mainDataStream.usedBits)
        {
            main_data_end++;
            pVars->mainDataStream.usedBits = main_data_end << 3;
        }


        bytes_to_discard = pVars->frame_start - pVars->sideInfo.main_data_begin - main_data_end;


        if (main_data_end > BUFSIZE)   /* check overflow on the buffer */
        {
            pVars->frame_start -= BUFSIZE;

            pVars->mainDataStream.usedBits -= (BUFSIZE << 3);
        }

        pVars->frame_start += temp;


        if (bytes_to_discard < 0 || crc_error_count)
        {
            /*
             *  Not enough data to decode, then we should avoid reading this
             *  data ( getting/ignoring sido info and scale data)
             *  Main data could be located in the previous frame, so an unaccounted
             *  frame can cause incorrect processing
             *  Just run the polyphase filter to "clean" the history buffer
             */
            errorCode = NO_ENOUGH_MAIN_DATA_ERROR;

            /*
             *  Clear the input to these filters
             */

            pv_memset((void*)pChVars[RIGHT]->work_buf_int32,
                      0,
                      SUBBANDS_NUMBER*FILTERBANK_BANDS*sizeof(pChVars[RIGHT]->work_buf_int32[0]));

            pv_memset((void*)pChVars[LEFT]->work_buf_int32,
                      0,
                      SUBBANDS_NUMBER*FILTERBANK_BANDS*sizeof(pChVars[LEFT]->work_buf_int32[0]));

            /*  clear circular buffers, to avoid any glitch */
            pv_memset((void*)&pChVars[ LEFT]->circ_buffer[576],
                      0,
                      480*sizeof(pChVars[ LEFT]->circ_buffer[0]));
            pv_memset((void*)&pChVars[RIGHT]->circ_buffer[576],
                      0,
                      480*sizeof(pChVars[RIGHT]->circ_buffer[0]));

            pChVars[ LEFT]->used_freq_lines = 575;
            pChVars[RIGHT]->used_freq_lines = 575;

        }
        else
        {
            pVars->mainDataStream.usedBits += (bytes_to_discard << 3);
        }

        /*
         *  if (fr_ps->header->version_x == MPEG_1), use 2 granules, otherwise just 1
         */
        for (gr = 0; gr < (1 + !(info->version_x)); gr++)
        {
            if (errorCode != NO_ENOUGH_MAIN_DATA_ERROR)
            {
                for (ch = 0; ch < pVars->num_channels; ch++)
                {
                    int32 part2_start = pVars->mainDataStream.usedBits;

                    if (info->version_x == MPEG_1)
                    {

                        pvmp3_get_scale_factors(&pVars->scaleFactors[ch],
                                                &pVars->sideInfo,
                                                gr,
                                                ch,
                                                &pVars->mainDataStream);
                    }
                    else
                    {
                        int32 * tmp = pVars->Scratch_mem;
                        pvmp3_mpeg2_get_scale_factors(&pVars->scaleFactors[ch],
                                                      &pVars->sideInfo,
                                                      gr,
                                                      ch,
                                                      info,
                                                      (uint32 *)tmp,
                                                      &pVars->mainDataStream);
                    }

                    pChVars[ch]->used_freq_lines = pvmp3_huffman_parsing(pChVars[ch]->work_buf_int32,
                                                   &pVars->sideInfo.ch[ch].gran[gr],
                                                   pVars,
                                                   part2_start,
                                                   info);


                    pvmp3_dequantize_sample(pChVars[ch]->work_buf_int32,
                                            &pVars->scaleFactors[ch],
                                            &pVars->sideInfo.ch[ch].gran[gr],
                                            pChVars[ch]->used_freq_lines,
                                            info);




                }   /* for (ch=0; ch<stereo; ch++)  */

                if (pVars->num_channels == 2)
                {

                    int32 used_freq_lines = (pChVars[ LEFT]->used_freq_lines  >
                                             pChVars[RIGHT]->used_freq_lines) ?
                                            pChVars[ LEFT]->used_freq_lines  :
                                            pChVars[RIGHT]->used_freq_lines;

                    pChVars[ LEFT]->used_freq_lines = used_freq_lines;
                    pChVars[RIGHT]->used_freq_lines = used_freq_lines;

                    if (info->version_x == MPEG_1)
                    {
                        pvmp3_stereo_proc(pChVars[ LEFT]->work_buf_int32,
                                          pChVars[RIGHT]->work_buf_int32,
                                          &pVars->scaleFactors[RIGHT],
                                          &pVars->sideInfo.ch[LEFT].gran[gr],
                                          used_freq_lines,
                                          info);
                    }
                    else
                    {
                        int32 * tmp = pVars->Scratch_mem;
                        pvmp3_mpeg2_stereo_proc(pChVars[ LEFT]->work_buf_int32,
                                                pChVars[RIGHT]->work_buf_int32,
                                                &pVars->scaleFactors[RIGHT],
                                                &pVars->sideInfo.ch[ LEFT].gran[gr],
                                                &pVars->sideInfo.ch[RIGHT].gran[gr],
                                                (uint32 *)tmp,
                                                used_freq_lines,
                                                info);
                    }
                }

            } /* if ( errorCode != NO_ENOUGH_MAIN_DATA_ERROR) */

            for (ch = 0; ch < pVars->num_channels; ch++)
            {

                pvmp3_reorder(pChVars[ch]->work_buf_int32,
                              &pVars->sideInfo.ch[ch].gran[gr],
                              &pChVars[ ch]->used_freq_lines,
                              info,
                              pVars->Scratch_mem);

                pvmp3_alias_reduction(pChVars[ch]->work_buf_int32,
                                      &pVars->sideInfo.ch[ch].gran[gr],
                                      &pChVars[ ch]->used_freq_lines,
                                      info);


                /*
                 *   IMDCT
                 */
                /* set mxposition
                 * In case of mixed blocks, # of bands with long
                 * blocks (2 or 4) else 0
                 */
                uint16 mixedBlocksLongBlocks = 0; /*  0 = long or short, 2=mixed, 4=mixed 2.5@8000 */
                if (pVars->sideInfo.ch[ch].gran[gr].mixed_block_flag &&
                        pVars->sideInfo.ch[ch].gran[gr].window_switching_flag)
                {
                    if ((info->version_x == MPEG_2_5) && (info->sampling_frequency == 2))
                    {
                        mixedBlocksLongBlocks = 4; /* mpeg2.5 @ 8 KHz */
                    }
                    else
                    {
                        mixedBlocksLongBlocks = 2;
                    }
                }

                pvmp3_imdct_synth(pChVars[ch]->work_buf_int32,
                                  pChVars[ch]->overlap,
                                  pVars->sideInfo.ch[ch].gran[gr].block_type,
                                  mixedBlocksLongBlocks,
                                  pChVars[ ch]->used_freq_lines,
                                  pVars->Scratch_mem);


                /*
                 *   Polyphase synthesis
                 */

                pvmp3_poly_phase_synthesis(pChVars[ch],
                                           pVars->num_channels,
                                           pExt->equalizerType,
                                           &ptrOutBuffer[ch]);


            }/* end ch loop */

            ptrOutBuffer += pVars->num_channels * SUBBANDS_NUMBER * FILTERBANK_BANDS;
        }  /*   for (gr=0;gr<Max_gr;gr++)  */

        /* skip ancillary data */
        if (info->bitrate_index > 0)
        { /* if not free-format */

            int32 ancillary_data_lenght = pVars->predicted_frame_size << 3;

            ancillary_data_lenght  -= pVars->inputStream.usedBits;

            /* skip ancillary data */
            if (ancillary_data_lenght > 0)
            {
                pVars->inputStream.usedBits += ancillary_data_lenght;
            }

        }

        /*
         *  This overrides a possible NO_ENOUGH_MAIN_DATA_ERROR
         */
        errorCode = NO_DECODING_ERROR;

    }
/*----------------------------------------------------------------------------
; FUNCTION CODE
----------------------------------------------------------------------------*/
Int getics(
    Int             id_syn_ele,
    BITS            *pInputStream,
    Int             common_window,
    tDec_Int_File   *pVars,
    tDec_Int_Chan   *pChVars,
    Int             group[],
    Int             *pMax_sfb,
    Int             *pCodebookMap,
    TNS_frame_info  *pTnsFrameInfo,
    FrameInfo       **pWinMap,
    PulseInfo       *pPulseInfo,
    SectInfo        sect[])
{
    /*----------------------------------------------------------------------------
    ; Define all local variables
    ----------------------------------------------------------------------------*/
    Int     status = SUCCESS;

    Int     nsect = 0;
    Int     i;
    Int     cb;
    Int     sectWidth;
    Int     sectStart;
    Int     totSfb;
    Int     *pGroup;

    FrameInfo *pFrameInfo;

    Int     global_gain; /* originally passed in from huffdecode */
    Bool    present;

    /*----------------------------------------------------------------------------
    ; Function body here
    ----------------------------------------------------------------------------*/
    pGroup = group;

    /* read global gain from Input bitstream */
    global_gain = get9_n_lessbits(LEN_SCL_PCM, pInputStream);

    if (common_window == FALSE)
    {
        status = get_ics_info(pVars->mc_info.audioObjectType,
                              pInputStream,
                              common_window,
                              &pChVars->wnd,
                              &pChVars->wnd_shape_this_bk,
                              group,
                              pMax_sfb,
                              pWinMap,
                              &pChVars->pShareWfxpCoef->lt_status,
                              NULL);
    }

    pFrameInfo = pWinMap[pChVars->wnd];

    /* at least, number of window should be 1 or 8 */
    if ((pFrameInfo->num_win != 1) && (pFrameInfo->num_win != 8))
    {
        status = 1;
    }

    /* First, calculate total number of scalefactor bands
     * for this grouping. Then, decode section data
     */
    if (*pMax_sfb > 0)
    {

        /* calculate total number of sfb */
        i      = 0;
        totSfb = 0;

        do
        {
            totSfb++;

        }
        while (*pGroup++ < pFrameInfo->num_win);

        totSfb  *=  pFrameInfo->sfb_per_win[0];

        /* decode section data */
        nsect = huffcb(sect,
                       pInputStream,
                       pFrameInfo->sectbits,
                       totSfb,
                       pFrameInfo->sfb_per_win[0],
                       *pMax_sfb);

        if (nsect == 0)
        {
            status = 1;     /* decode section data error */

        }/* if (nsect) */

        /* generate "linear" description from section info
         * stored as codebook for each scalefactor band and group
         * when nsect == 0, for-loop does not execute
         */
        sectStart = 0;
        for (i = 0; i < nsect; i++)
        {
            cb  = sect[i].sect_cb;
            sectWidth =  sect[i].sect_end - sectStart;
            sectStart += sectWidth;

            while (sectWidth > 0)
            {
                *pCodebookMap++ = cb;   /* cannot use memset for Int */
                sectWidth--;
            }

        } /* for (i) */

    }
    else
    {
        /* set all sections with ZERO_HCB */
        pv_memset(pCodebookMap,
                  ZERO_HCB,
                  MAXBANDS*sizeof(*pCodebookMap));
        /*
                for (i=MAXBANDS; i>0; i--)
                {
                    *(pCodebookMap++) = ZERO_HCB;
                }
        */

    } /* if (*pMax_sfb) */

    /* calculate band offsets
     * (because of grouping and interleaving this cannot be
     * a constant: store it in pFrameInfo->frame_sfb_top)
     */
    if (pFrameInfo->islong == FALSE)
    {
        calc_gsfb_table(pFrameInfo, group);
    }

    /* decode scale factor data */
    if (status == SUCCESS)
    {
        status = hufffac(pFrameInfo,
                         pInputStream,
                         group,
                         nsect,
                         sect,
                         global_gain,
                         pChVars->pShareWfxpCoef->factors,
                         pVars->scratch.a.huffbook_used);

    } /* if (status) */

    /* noiseless coding */
    if (status == SUCCESS)
    {
        present = get1bits(pInputStream);

        pPulseInfo->pulse_data_present = present;

        if (present != FALSE)
        {
            if (pFrameInfo->islong == 1)
            {
                status = get_pulse_data(pPulseInfo,
                                        pInputStream);
            }
            else
            {
                /* CommonExit(1,"Pulse data not allowed for short blocks"); */
                status = 1;

            } /* if (pFrameInfo) */
        } /* if (present) */

    } /* if (status) */


    /* decode tns data */
    if (status == SUCCESS)
    {
        present = get1bits(pInputStream);

        pTnsFrameInfo->tns_data_present = present;

        if (present != FALSE)
        {
            get_tns(pChVars->pShareWfxpCoef->max_sfb,
                    pInputStream,
                    pChVars->wnd,
                    pFrameInfo,
                    &pVars->mc_info,
                    pTnsFrameInfo,
                    pVars->scratch.a.tns_decode_coef);
        }
        else
        {
            for (i = pFrameInfo->num_win - 1; i >= 0 ; i--)
            {
                pTnsFrameInfo->n_filt[i] = 0;
            }

        } /* if(present) */

    } /* if (status) */

    /* gain control */
    if (status == SUCCESS)
    {
        present =
            get1bits(pInputStream);

        if (present != FALSE)
        {
            /* CommonExit(1, "Gain control not implemented"); */
            status = 1;
        }
    } /* if (status) */

    if (status == SUCCESS)
    {
        /* Support Mono, Dual-Mono, or Stereo */
        if ((id_syn_ele == ID_SCE) || (id_syn_ele == ID_CPE))
        {
            status = huffspec_fxp(pFrameInfo,
                                  pInputStream,
                                  nsect,
                                  sect,
                                  pChVars->pShareWfxpCoef->factors,
                                  pChVars->fxpCoef,
                                  pVars->share.a.quantSpec,
                                  pVars->scratch.tmp_spec,
                                  pWinMap[ONLY_LONG_WINDOW],
                                  pPulseInfo,
                                  pChVars->pShareWfxpCoef->qFormat);
        }
    }

    /*----------------------------------------------------------------------------
    ; Return status
    ----------------------------------------------------------------------------*/

    return status;

} /* getics */
Ejemplo n.º 15
0
void phase_dispersion(
    int16 gain_code,             /* (i) Q0  : gain of code             */
    int16 gain_pit,              /* (i) Q14 : gain of pitch            */
    int16 code[],                /* (i/o)   : code vector              */
    int16 mode,                  /* (i)     : level, 0=hi, 1=lo, 2=off */
    int16 disp_mem[],            /* (i/o)   : static memory (size = 8) */
    int16 ScratchMem[]
)
{
    int16 i, j, state;
    int16 *prev_gain_pit, *prev_gain_code, *prev_state;
    int16 *code2 = ScratchMem;

    prev_state = disp_mem;
    prev_gain_code = disp_mem + 1;
    prev_gain_pit = disp_mem + 2;

    pv_memset((void *)code2, 0, (2*L_SUBFR)*sizeof(*code2));


    if (gain_pit < pitch_0_6)
    {
        state = 0;
    }
    else if (gain_pit < pitch_0_9)
    {
        state = 1;
    }
    else
    {
        state = 2;
    }

    for (i = 5; i > 0; i--)
    {
        prev_gain_pit[i] = prev_gain_pit[i - 1];
    }
    prev_gain_pit[0] = gain_pit;

    if (sub_int16(gain_code, *prev_gain_code) > shl_int16(*prev_gain_code, 1))
    {
        /* onset */
        if (state < 2)
        {
            state++;
        }
    }
    else
    {
        j = 0;
        for (i = 0; i < 6; i++)
        {
            if (prev_gain_pit[i] < pitch_0_6)
            {
                j++;
            }
        }

        if (j > 2)
        {
            state = 0;
        }
        if (state > *prev_state + 1)
        {
            state--;
        }
    }

    *prev_gain_code = gain_code;
    *prev_state = state;

    /* circular convolution */

    state += mode;              /* level of dispersion */

    if (state == 0)
    {
        for (i = 0; i < L_SUBFR; i++)
        {
            if (code[i] != 0)
            {
                for (j = 0; j < L_SUBFR; j++)
                {
                    code2[i + j] = add_int16(code2[i + j], mult_int16_r(code[i], ph_imp_low[j]));
                }
            }
        }
    }
    else if (state == 1)
    {
        for (i = 0; i < L_SUBFR; i++)
        {
            if (code[i] != 0)
            {
                for (j = 0; j < L_SUBFR; j++)
                {
                    code2[i + j] = add_int16(code2[i + j], mult_int16_r(code[i], ph_imp_mid[j]));
                }
            }
        }
    }
    if (state < 2)
    {
        for (i = 0; i < L_SUBFR; i++)
        {
            code[i] = add_int16(code2[i], code2[i + L_SUBFR]);
        }
    }
    return;
}
/*----------------------------------------------------------------------------
; FUNCTION CODE
----------------------------------------------------------------------------*/
OSCL_EXPORT_REF Int PVMP4AudioDecoderInitLibrary(
    tPVMP4AudioDecoderExternal  *pExt,
    void                        *pMem)
{
    tDec_Int_File *pVars;

    pVars = (tDec_Int_File *)pMem;

    /*
     * Initialize all memory. The pointers to channel memory will be
     * set to zero also.
     */
    pv_memset(
        pVars,
        0,
        sizeof(tDec_Int_File));

    /*
     * Pick default values for the library.
     */
    pVars->perChan[0].fxpCoef = pVars->fxpCoef[0];
    pVars->perChan[1].fxpCoef = pVars->fxpCoef[1];

    /* Here, the "shared memory" pointer is set to point
     * at the 1024th element of fxpCoef, because those spaces
     * in memory are not used until the filterbank is called.
     *
     * Therefore, any variables that are only used before
     * the filterbank can occupy this same space in memory.
     */

    pVars->perChan[0].pShareWfxpCoef = (per_chan_share_w_fxpCoef *)
                                       & (pVars->perChan[0].fxpCoef[1024]);

    pVars->perChan[1].pShareWfxpCoef = (per_chan_share_w_fxpCoef *)
                                       & (pVars->perChan[1].fxpCoef[1024]);

    /*
     * This next line informs the function get_prog_config that no
     * configuration has been found thus far, so it is a default
     * configuration.
     */

    pVars->current_program = -1;
    pVars->mc_info.sampling_rate_idx = Fs_44; /* Fs_44 = 4, 44.1kHz */

    /*
     * In the future, the frame length will change with MP4 file format.
     * Presently this variable is used to simply the unit test for
     * the function PVMP4AudioDecodeFrame() .. otherwise the test would
     * have to pass around 1024 length arrays.
     */
    pVars->frameLength = LONG_WINDOW; /* 1024*/

    /*
     * The window types ONLY_LONG_SEQUENCE, LONG_START_SEQUENCE, and
     * LONG_STOP_SEQUENCE share the same information. The only difference
     * between the windows is accounted for in the "filterbank", in
     * the function trans4m_freq_2_time_fxp()
     */

    pVars->winmap[ONLY_LONG_SEQUENCE]   /* 0 */ = &pVars->longFrameInfo;
    pVars->winmap[LONG_START_SEQUENCE]  /* 1 */ = &pVars->longFrameInfo;
    pVars->winmap[EIGHT_SHORT_SEQUENCE] /* 2 */ = &pVars->shortFrameInfo;
    pVars->winmap[LONG_STOP_SEQUENCE]   /* 3 */ = &pVars->longFrameInfo;

    infoinit(
        pVars->mc_info.sampling_rate_idx,
        (FrameInfo   **)pVars->winmap,
        pVars->SFBWidth128);


    /*
     * Clear out external output values. These values are set later at the end
     * of PVMP4AudioDecodeFrames()
     */
    pExt->bitRate = 0;
    pExt->encodedChannels = 0;
    pExt->samplingRate = 0;
    pExt->aacPlusUpsamplingFactor = 1;  /*  Default for regular AAC */
    pVars->aacPlusEnabled = pExt->aacPlusEnabled;


#if defined(AAC_PLUS)
    pVars->sbrDecoderData.setStreamType = 1;        /* Enable Lock for AAC stream type setting  */
#endif

    /*
     * Initialize input buffer variable.
     */

    pExt->inputBufferUsedLength = 0;

    return (MP4AUDEC_SUCCESS);

}  /* PVMP4AudioDecoderInitLibrary */
Ejemplo n.º 17
0
void sbr_dec(Int16 *inPcmData,
             Int16 *ftimeOutPtr,
             SBR_FRAME_DATA * hFrameData,
             int32_t applyProcessing,
             SBR_DEC *sbrDec,
#ifdef HQ_SBR
#ifdef PARAMETRICSTEREO
             Int16 * ftimeOutPtrPS,
             HANDLE_PS_DEC hParametricStereoDec,
#endif
#endif
             tDec_Int_File  *pVars)
{
    int32_t   i;
    int32_t   j;
    int32_t   m;

    int32_t  *frameInfo = hFrameData->frameInfo;
    Int  num_qmf_bands;

#ifdef HQ_SBR
#ifdef PARAMETRICSTEREO

    int32_t env;

    int32_t *qmf_PS_generated_Real;
    int32_t *qmf_PS_generated_Imag;

    int32_t *Sr_x;
    int32_t *Si_x;


#endif
#endif

    int32_t(*scratch_mem)[64];
    Int16 *circular_buffer_s;

    int32_t   k;
    int32_t *Sr;
    int32_t *Si;
    int32_t *ptr_tmp1;
    int32_t *ptr_tmp2;
    scratch_mem = pVars->scratch.scratch_mem;


    if (applyProcessing)
    {
        num_qmf_bands = sbrDec->lowSubband;
    }
    else
    {
        num_qmf_bands = 32;     /* becomes a resampler by 2  */
    }

    /* -------------------------------------------------- */
    /*
     *    Re-Load Buffers
     */
    pv_memmove(&hFrameData->sbrQmfBufferReal[0],
               &hFrameData->HistsbrQmfBufferReal[0],
               6*SBR_NUM_BANDS*sizeof(*hFrameData->sbrQmfBufferReal));
#ifdef HQ_SBR


    if (sbrDec->LC_aacP_DecoderFlag == OFF)
    {
        pv_memmove(&hFrameData->sbrQmfBufferImag[0],
                   &hFrameData->HistsbrQmfBufferImag[0],
                   6*SBR_NUM_BANDS*sizeof(*hFrameData->sbrQmfBufferImag));
    }
#endif
    /* -------------------------------------------------- */


    /*
     *    low band codec signal subband filtering
     */

    for (i = 0; i < 32; i++)
    {

        if (sbrDec->LC_aacP_DecoderFlag == ON)
        {

            calc_sbr_anafilterbank_LC(hFrameData->codecQmfBufferReal[sbrDec->bufWriteOffs + i],
                                      &inPcmData[319] + (i << 5),
                                      scratch_mem,
                                      num_qmf_bands);

        }
#ifdef HQ_SBR
        else
        {

            calc_sbr_anafilterbank(hFrameData->codecQmfBufferReal[sbrDec->bufWriteOffs + i],
                                   hFrameData->codecQmfBufferImag[sbrDec->bufWriteOffs + i],
                                   &inPcmData[319] + (i << 5),
                                   scratch_mem,
                                   num_qmf_bands);
        }
#endif

    }

    if (pVars->ltp_buffer_state)
    {
        pv_memcpy(&inPcmData[-1024-288], &inPcmData[1024], 288*sizeof(*inPcmData));
    }
    else
    {
        pv_memcpy(&inPcmData[1024 + 288], &inPcmData[1024], 288*sizeof(*inPcmData));
    }


    if (applyProcessing)
    {

        /*
         *  Inverse filtering of lowband + HF generation
         */

        if (sbrDec->LC_aacP_DecoderFlag == ON)
        {

            sbr_generate_high_freq((int32_t(*)[32])(hFrameData->codecQmfBufferReal + sbrDec->bufReadOffs),
                                   NULL,
                                   (int32_t *)(hFrameData->sbrQmfBufferReal),
                                   NULL,
                                   hFrameData->sbr_invf_mode,
                                   hFrameData->sbr_invf_mode_prev,
                                   &(sbrDec->FreqBandTableNoise[1]),
                                   sbrDec->NoNoiseBands,
                                   sbrDec->lowSubband,
                                   sbrDec->V_k_master,
                                   sbrDec->Num_Master,
                                   sbrDec->outSampleRate,
                                   frameInfo,
                                   hFrameData->degreeAlias,
                                   scratch_mem,
                                   hFrameData->BwVector,/* */
                                   hFrameData->BwVectorOld,
                                   &(sbrDec->Patch),
                                   sbrDec->LC_aacP_DecoderFlag,
                                   &(sbrDec->highSubband));


            /*
             *      Adjust envelope of current frame.
             */

            calc_sbr_envelope(hFrameData,
                              (int32_t *)(hFrameData->sbrQmfBufferReal),
                              NULL,
                              sbrDec->FreqBandTable,
                              sbrDec->NSfb,
                              sbrDec->FreqBandTableNoise,
                              sbrDec->NoNoiseBands,
                              hFrameData->reset_flag,
                              hFrameData->degreeAlias,
                              &(hFrameData->harm_index),
                              &(hFrameData->phase_index),
                              hFrameData->hFp,
                              &(hFrameData->sUp),
                              sbrDec->limSbc,
                              sbrDec->gateMode,
#ifdef HQ_SBR
                              NULL,
                              NULL,
                              NULL,
                              NULL,
#endif
                              scratch_mem,
                              sbrDec->Patch,
                              sbrDec->sqrt_cache,
                              sbrDec->LC_aacP_DecoderFlag);
        }
#ifdef HQ_SBR
        else
        {

            sbr_generate_high_freq((int32_t(*)[32])(hFrameData->codecQmfBufferReal + sbrDec->bufReadOffs),
                                   (int32_t(*)[32])(hFrameData->codecQmfBufferImag + sbrDec->bufReadOffs),
                                   (int32_t *)(hFrameData->sbrQmfBufferReal),
                                   (int32_t *)(hFrameData->sbrQmfBufferImag),
                                   hFrameData->sbr_invf_mode,
                                   hFrameData->sbr_invf_mode_prev,
                                   &(sbrDec->FreqBandTableNoise[1]),
                                   sbrDec->NoNoiseBands,
                                   sbrDec->lowSubband,
                                   sbrDec->V_k_master,
                                   sbrDec->Num_Master,
                                   sbrDec->outSampleRate,
                                   frameInfo,
                                   NULL,
                                   scratch_mem,
                                   hFrameData->BwVector,
                                   hFrameData->BwVectorOld,
                                   &(sbrDec->Patch),
                                   sbrDec->LC_aacP_DecoderFlag,
                                   &(sbrDec->highSubband));

            /*
             *      Adjust envelope of current frame.
             */

            calc_sbr_envelope(hFrameData,
                              (int32_t *)(hFrameData->sbrQmfBufferReal),
                              (int32_t *)(hFrameData->sbrQmfBufferImag),
                              sbrDec->FreqBandTable,
                              sbrDec->NSfb,
                              sbrDec->FreqBandTableNoise,
                              sbrDec->NoNoiseBands,
                              hFrameData->reset_flag,
                              NULL,
                              &(hFrameData->harm_index),
                              &(hFrameData->phase_index),
                              hFrameData->hFp,
                              &(hFrameData->sUp),
                              sbrDec->limSbc,
                              sbrDec->gateMode,
                              hFrameData->fBuf_man,
                              hFrameData->fBuf_exp,
                              hFrameData->fBufN_man,
                              hFrameData->fBufN_exp,
                              scratch_mem,
                              sbrDec->Patch,
                              sbrDec->sqrt_cache,
                              sbrDec->LC_aacP_DecoderFlag);

        }
#endif


    }
    else   /*  else for applyProcessing */
    {
        /* no sbr, set high band buffers to zero */

        for (i = 0; i < SBR_NUM_COLUMNS; i++)
        {
            pv_memset((void *)&hFrameData->sbrQmfBufferReal[i*SBR_NUM_BANDS],
                      0,
                      SBR_NUM_BANDS*sizeof(*hFrameData->sbrQmfBufferReal));

#ifdef HQ_SBR
            pv_memset((void *)&hFrameData->sbrQmfBufferImag[i*SBR_NUM_BANDS],
                      0,
                      SBR_NUM_BANDS*sizeof(*hFrameData->sbrQmfBufferImag));

#endif
        }

    }


    /*
     *  Synthesis subband filtering.
     */

#ifdef HQ_SBR

#ifdef PARAMETRICSTEREO


    /*
     * psPresentFlag set implies hParametricStereoDec !=NULL, second condition is
     * is just here to prevent CodeSonar warnings.
     */
    if ((pVars->mc_info.psPresentFlag) && (applyProcessing) &&
            (hParametricStereoDec != NULL))
    {

        /*
         *  qmfBufferReal uses the rigth aac channel ( perChan[1] is not used)
         *  followed by the buffer fxpCoef[2][2048]  which makes a total of
         *  2349 + 2048*2 = 6445
         *  These  2 matrices (qmfBufferReal & qmfBufferImag) are
         *  [2][38][64] == 4864 int32_t
         */


        tDec_Int_Chan *tmpx = &pVars->perChan[1];
        /*
         *  dereferencing type-punned pointer avoid
         *  breaking strict-aliasing rules
         */
        int32_t *tmp = (int32_t *)tmpx;
        hParametricStereoDec->qmfBufferReal = (int32_t(*)[64]) tmp;

        tmp = (int32_t *) & hParametricStereoDec->qmfBufferReal[38][0];
        hParametricStereoDec->qmfBufferImag = (int32_t(*)[64]) tmp;

        for (i = 0; i < 32; i++)
        {
            Int   xoverBand;

            if (i < ((hFrameData->frameInfo[1]) << 1))
            {
                xoverBand = sbrDec->prevLowSubband;
            }
            else
            {
                xoverBand = sbrDec->lowSubband;
            }

            if (xoverBand > sbrDec->highSubband)
            {
                xoverBand = 32; /* error condition, default to upsampling mode */
            }

            m = sbrDec->bufReadOffs + i;    /*  2 + i */

            Sr_x = hParametricStereoDec->qmfBufferReal[i];
            Si_x = hParametricStereoDec->qmfBufferImag[i];



            for (int32_t j = 0; j < xoverBand; j++)
            {
                Sr_x[j] = shft_lft_1(hFrameData->codecQmfBufferReal[m][j]);
                Si_x[j] = shft_lft_1(hFrameData->codecQmfBufferImag[m][j]);
            }




            pv_memcpy(&Sr_x[xoverBand],
                      &hFrameData->sbrQmfBufferReal[i*SBR_NUM_BANDS],
                      (sbrDec->highSubband - xoverBand)*sizeof(*Sr_x));

            pv_memcpy(&Si_x[xoverBand],
                      &hFrameData->sbrQmfBufferImag[i*SBR_NUM_BANDS],
                      (sbrDec->highSubband - xoverBand)*sizeof(*Si_x));

            pv_memset((void *)&Sr_x[sbrDec->highSubband],
                      0,
                      (64 - sbrDec->highSubband)*sizeof(*Sr_x));

            pv_memset((void *)&Si_x[sbrDec->highSubband],
                      0,
                      (64 - sbrDec->highSubband)*sizeof(*Si_x));


        }

        for (i = 32; i < 32 + 6; i++)
        {
            m = sbrDec->bufReadOffs + i;     /*  2 + i */

            for (int32_t j = 0; j < 5; j++)
            {
                hParametricStereoDec->qmfBufferReal[i][j] = shft_lft_1(hFrameData->codecQmfBufferReal[m][j]);
                hParametricStereoDec->qmfBufferImag[i][j] = shft_lft_1(hFrameData->codecQmfBufferImag[m][j]);
            }

        }


        /*
         *    Update Buffers
         */
        for (i = 0; i < sbrDec->bufWriteOffs; i++)     /* sbrDec->bufWriteOffs set to 8 and unchanged */
        {
            j = sbrDec->noCols + i;                    /* sbrDec->noCols set to 32 and unchanged */

            pv_memmove(hFrameData->codecQmfBufferReal[i],         /* to    */
                       hFrameData->codecQmfBufferReal[j],        /* from  */
                       sizeof(*hFrameData->codecQmfBufferReal[i]) << 5);

            pv_memmove(hFrameData->codecQmfBufferImag[i],
                       hFrameData->codecQmfBufferImag[j],
                       sizeof(*hFrameData->codecQmfBufferImag[i]) << 5);
        }


        pv_memmove(&hFrameData->HistsbrQmfBufferReal[0],
                   &hFrameData->sbrQmfBufferReal[32*SBR_NUM_BANDS],
                   6*SBR_NUM_BANDS*sizeof(*hFrameData->sbrQmfBufferReal));

        pv_memmove(&hFrameData->HistsbrQmfBufferImag[0],
                   &hFrameData->sbrQmfBufferImag[32*SBR_NUM_BANDS],
                   6*SBR_NUM_BANDS*sizeof(*hFrameData->sbrQmfBufferImag));


        /*
         *   Needs whole QMF matrix formed before applying
         *   Parametric stereo processing.
         */

        qmf_PS_generated_Real = scratch_mem[0];
        qmf_PS_generated_Imag = scratch_mem[1];
        env = 0;

        /*
         *  Set circular buffer for Left channel
         */

        circular_buffer_s = (Int16 *)scratch_mem[7];


        if (pVars->mc_info.bDownSampledSbr)
        {
            pv_memmove(&circular_buffer_s[2048],
                       hFrameData->V,
                       640*sizeof(*circular_buffer_s));
        }
        else
        {
            pv_memmove(&circular_buffer_s[4096],
                       hFrameData->V,
                       1152*sizeof(*circular_buffer_s));

        }


        /*
         *  Set Circular buffer for PS hybrid analysis
         */
        for (i = 0, j = 0; i < 3; i++)
        {

            pv_memmove(&scratch_mem[2][32 + j     ],
                       hParametricStereoDec->hHybrid->mQmfBufferReal[i],
                       HYBRID_FILTER_LENGTH_m_1*sizeof(*hParametricStereoDec->hHybrid->mQmfBufferReal));
            pv_memmove(&scratch_mem[2][32 + j + 44],
                       hParametricStereoDec->hHybrid->mQmfBufferImag[i],
                       HYBRID_FILTER_LENGTH_m_1*sizeof(*hParametricStereoDec->hHybrid->mQmfBufferImag));
            j += 88;
        }

        pv_memset((void *)&qmf_PS_generated_Real[hParametricStereoDec->usb],
                  0,
                  (64 - hParametricStereoDec->usb)*sizeof(*qmf_PS_generated_Real));

        pv_memset((void *)&qmf_PS_generated_Imag[hParametricStereoDec->usb],
                  0,
                  (64 - hParametricStereoDec->usb)*sizeof(*qmf_PS_generated_Imag));


        for (i = 0; i < 32; i++)
        {
            if (i == (Int)hParametricStereoDec-> aEnvStartStop[env])
            {
                ps_init_stereo_mixing(hParametricStereoDec, env, sbrDec->highSubband);
                env++;
            }


            ps_applied(hParametricStereoDec,
                       &hParametricStereoDec->qmfBufferReal[i],
                       &hParametricStereoDec->qmfBufferImag[i],
                       qmf_PS_generated_Real,
                       qmf_PS_generated_Imag,
                       scratch_mem[2],
                       i);

            /* Create time samples for regular mono channel */

            if (pVars->mc_info.bDownSampledSbr)
            {
                calc_sbr_synfilterbank(hParametricStereoDec->qmfBufferReal[i],  /* realSamples  */
                                       hParametricStereoDec->qmfBufferImag[i], /* imagSamples  */
                                       ftimeOutPtr + (i << 6),
                                       &circular_buffer_s[1984 - (i<<6)],
                                       pVars->mc_info.bDownSampledSbr);
            }
            else
            {
                calc_sbr_synfilterbank(hParametricStereoDec->qmfBufferReal[i],  /* realSamples  */
                                       hParametricStereoDec->qmfBufferImag[i], /* imagSamples  */
                                       ftimeOutPtr + (i << 7),
                                       &circular_buffer_s[3968 - (i<<7)],
                                       pVars->mc_info.bDownSampledSbr);

            }

            pv_memmove(hParametricStereoDec->qmfBufferReal[i], qmf_PS_generated_Real, 64*sizeof(*qmf_PS_generated_Real));
            pv_memmove(hParametricStereoDec->qmfBufferImag[i], qmf_PS_generated_Imag, 64*sizeof(*qmf_PS_generated_Real));

        }


        /*
         *  Save Circular buffer history used on PS hybrid analysis
         */

        for (i = 0, j = 0; i < 3; i++)
        {
            pv_memmove(hParametricStereoDec->hHybrid->mQmfBufferReal[i],
                       &scratch_mem[2][ 64 + j     ],
                       HYBRID_FILTER_LENGTH_m_1*sizeof(*hParametricStereoDec->hHybrid->mQmfBufferReal));

            pv_memmove(hParametricStereoDec->hHybrid->mQmfBufferImag[i],
                       &scratch_mem[2][ 64 + j + 44],
                       HYBRID_FILTER_LENGTH_m_1*sizeof(*hParametricStereoDec->hHybrid->mQmfBufferImag));

            j += 88;
        }

        pv_memmove(hFrameData->V, &circular_buffer_s[0], 1152*sizeof(*circular_buffer_s));

        /*
         *  Set circular buffer for Right channel
         */

        circular_buffer_s = (Int16 *)scratch_mem[5];

        if (pVars->mc_info.bDownSampledSbr)
        {
            pv_memmove(&circular_buffer_s[2048],
                       (int32_t *)hParametricStereoDec->R_ch_qmf_filter_history,
                       640*sizeof(*circular_buffer_s));
        }
        else
        {
            pv_memmove(&circular_buffer_s[4096],
                       (int32_t *)hParametricStereoDec->R_ch_qmf_filter_history,
                       1152*sizeof(*circular_buffer_s));

        }


        for (i = 0; i < 32; i++)
        {
            if (pVars->mc_info.bDownSampledSbr)
            {

                calc_sbr_synfilterbank(hParametricStereoDec->qmfBufferReal[i],  /* realSamples  */
                                       hParametricStereoDec->qmfBufferImag[i], /* imagSamples  */
                                       ftimeOutPtrPS + (i << 6),
                                       &circular_buffer_s[1984 - (i<<6)],
                                       pVars->mc_info.bDownSampledSbr);
            }
            else
            {
                calc_sbr_synfilterbank(hParametricStereoDec->qmfBufferReal[i],  /* realSamples  */
                                       hParametricStereoDec->qmfBufferImag[i], /* imagSamples  */
                                       ftimeOutPtrPS + (i << 7),
                                       &circular_buffer_s[3968 - (i<<7)],
                                       pVars->mc_info.bDownSampledSbr);
            }

        }

        if (pVars->mc_info.bDownSampledSbr)
        {
            pv_memmove((int32_t *)hParametricStereoDec->R_ch_qmf_filter_history, &circular_buffer_s[0], 640*sizeof(*circular_buffer_s));
        }
        else
        {
            pv_memmove((int32_t *)hParametricStereoDec->R_ch_qmf_filter_history, &circular_buffer_s[0], 1152*sizeof(*circular_buffer_s));
        }





    }
    else    /*  else -- sbrEnablePS  */
    {

#endif      /*   PARAMETRICSTEREO */
#endif      /*   HQ_SBR */

        /*
         *  Use shared aac memory as continuous buffer
         */


        Sr  = scratch_mem[0];
        Si  = scratch_mem[1];

        circular_buffer_s = (Int16*)scratch_mem[2];

        if (pVars->mc_info.bDownSampledSbr)
        {

            pv_memmove(&circular_buffer_s[2048],
                       hFrameData->V,
                       640*sizeof(*circular_buffer_s));
        }
        else
        {
            pv_memmove(&circular_buffer_s[4096],
                       hFrameData->V,
                       1152*sizeof(*circular_buffer_s));
        }

        for (i = 0; i < 32; i++)
        {
            Int   xoverBand;

            if (applyProcessing)
            {
                if (i < ((hFrameData->frameInfo[1]) << 1))
                {
                    xoverBand = sbrDec->prevLowSubband;

                }
                else
                {
                    xoverBand = sbrDec->lowSubband;
                }

                if (xoverBand > sbrDec->highSubband)
                {
                    xoverBand = 32; /* error condition, default to upsampling mode */
                }
            }
            else
            {
                xoverBand = 32;
                sbrDec->highSubband = 32;
            }


            m = sbrDec->bufReadOffs + i;    /* sbrDec->bufReadOffs == 2 */


            ptr_tmp1 = (hFrameData->codecQmfBufferReal[m]);
            ptr_tmp2 = Sr;

            if (sbrDec->LC_aacP_DecoderFlag == ON)
            {

                for (k = (xoverBand >> 1); k != 0; k--)
                {
                    *(ptr_tmp2++) = (*(ptr_tmp1++)) >> 9;
                    *(ptr_tmp2++) = (*(ptr_tmp1++)) >> 9;
                }
                if (xoverBand & 1)
                {
                    *(ptr_tmp2++) = (*(ptr_tmp1)) >> 9;
                }

                ptr_tmp1 = &hFrameData->sbrQmfBufferReal[i*SBR_NUM_BANDS];


                for (k = xoverBand; k < sbrDec->highSubband; k++)
                {
                    *(ptr_tmp2++) = (*(ptr_tmp1++)) << 1;
                }

                pv_memset((void *)ptr_tmp2,
                          0,
                          (64 - sbrDec->highSubband)*sizeof(*ptr_tmp2));


                if (pVars->mc_info.bDownSampledSbr)
                {
                    calc_sbr_synfilterbank_LC(Sr,               /* realSamples  */
                                              ftimeOutPtr + (i << 6),
                                              &circular_buffer_s[1984 - (i<<6)],
                                              pVars->mc_info.bDownSampledSbr);
                }
                else
                {
                    calc_sbr_synfilterbank_LC(Sr,               /* realSamples  */
                                              ftimeOutPtr + (i << 7),
                                              &circular_buffer_s[3968 - (i<<7)],
                                              pVars->mc_info.bDownSampledSbr);
                }
            }
#ifdef HQ_SBR
            else
            {

                for (k = xoverBand; k != 0; k--)
                {
                    *(ptr_tmp2++) = shft_lft_1(*(ptr_tmp1++));
                }

                ptr_tmp1 = &hFrameData->sbrQmfBufferReal[i*SBR_NUM_BANDS];
                ptr_tmp2 = &Sr[xoverBand];


                for (k = xoverBand; k < sbrDec->highSubband; k++)
                {
                    *(ptr_tmp2++) = (*(ptr_tmp1++));
                }

                pv_memset((void *)ptr_tmp2,
                          0,
                          (64 - sbrDec->highSubband)*sizeof(*ptr_tmp2));


                ptr_tmp1 = (hFrameData->codecQmfBufferImag[m]);
                ptr_tmp2 = Si;

                for (k = (xoverBand >> 1); k != 0; k--)
                {
                    *(ptr_tmp2++) = shft_lft_1(*(ptr_tmp1++));
                    *(ptr_tmp2++) = shft_lft_1(*(ptr_tmp1++));
                }
                if (xoverBand & 1)
                {
                    *(ptr_tmp2) = shft_lft_1(*(ptr_tmp1));
                }

                ptr_tmp1 = &hFrameData->sbrQmfBufferImag[i*SBR_NUM_BANDS];
                ptr_tmp2 = &Si[xoverBand];

                for (k = xoverBand; k < sbrDec->highSubband; k++)
                {
                    *(ptr_tmp2++) = (*(ptr_tmp1++));
                }

                pv_memset((void *)ptr_tmp2,
                          0,
                          (64 - sbrDec->highSubband)*sizeof(*ptr_tmp2));


                if (pVars->mc_info.bDownSampledSbr)
                {
                    calc_sbr_synfilterbank(Sr,              /* realSamples  */
                                           Si,             /* imagSamples  */
                                           ftimeOutPtr + (i << 6),
                                           &circular_buffer_s[1984 - (i<<6)],
                                           pVars->mc_info.bDownSampledSbr);
                }
                else
                {
                    calc_sbr_synfilterbank(Sr,              /* realSamples  */
                                           Si,             /* imagSamples  */
                                           ftimeOutPtr + (i << 7),
                                           &circular_buffer_s[3968 - (i<<7)],
                                           pVars->mc_info.bDownSampledSbr);
                }
            }
#endif

        }