⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 owng729fp.c

📁 G.711,G.723.1,G.726,G.729,GSM CODEC C/C++ code
💻 C
📖 第 1 页 / 共 5 页
字号:

    if (phDispState - phdState->prevDispState > 1 && !phdState->onset) phDispState -= 1;

    if (phdState->onset) {
        if (phDispState < 2) phDispState++;
    }

    phdState->prevDispState=phDispState;
    phdState->prevCbGain = valCodebookGain;

    if (phDispState == 0) {
       pTable = ImpLow;
    } else if (phDispState == 1) {
       pTable = ImpMiddle;
    } else if (phDispState == 2) {
       pTable = ImpHigh;
    }

    for (nPulse=0; nPulse<numNonZeroElem; nPulse++) {
      lPos = pPos[nPulse];
      for (i=lPos; i<SUBFR_LEN; i++)
         pSrcDstInnovation[i] += pMemory[lPos] * pTable[i-lPos];
      for (i=0; i < lPos; i++)
         pSrcDstInnovation[i] += pMemory[lPos] * pTable[SUBFR_LEN-lPos+i];
    }

    ippsAdaptiveCodebookContribution_G729_32f(-valCodebookGain, pSrcDstInnovation, pScaledLTP, pDstFltExcSignal);

    return;
}

static void GlobalStationnarityAdaptation_G729E(G729FPEncoder_Obj* encoderObj, float valBackwardPredGain, float valForwardPredGain, int valLPCMode)
{
    short sTmp;
    /* First adaptation based on previous backward / forward decisions */
    if (valLPCMode == 1) { /* Backward stationnary mode */

        (encoderObj->sBWDStatInd)++;
        CLIP_TO_UPLEVEL(encoderObj->sBWDStatInd,21);
        if(encoderObj->sValBWDStatInd < 32517) encoderObj->sValBWDStatInd  += 250;
        else encoderObj->sValBWDStatInd = 32767;

        /* after 20 backward frames => increase stat */
        if (encoderObj->sBWDStatInd == 20) {
            if(encoderObj->sGlobalStatInd < 30267) encoderObj->sGlobalStatInd += 2500;
            else encoderObj->sGlobalStatInd = 32767;
        }
        else if (encoderObj->sBWDStatInd > 20) encoderObj->sGlobalStatInd += 500;

    }

    else if ((valLPCMode == 0)&&(encoderObj->prevLPCMode == 1)) { /* Backward -> Forward transition */

        /* Transition occurs after less than 20 backward frames => decrease stat */
        if (encoderObj->sBWDStatInd < 20) {
            sTmp = (short)(5000 - encoderObj->sValBWDStatInd);
            encoderObj->sGlobalStatInd = (short)(encoderObj->sGlobalStatInd-sTmp);
        }

        /* Reset consecutive backward frames counter */
        encoderObj->sBWDStatInd = 0;
        encoderObj->sValBWDStatInd = 0;

    }

    /* Second adaptation based on prediction gains */

    if (encoderObj->sGlobalStatInd < 13000) {

        if      (valBackwardPredGain > valForwardPredGain + TH4) encoderObj->sGlobalStatInd += 3200;
        else if (valBackwardPredGain > valForwardPredGain + TH3) encoderObj->sGlobalStatInd += 2400;
        else if (valBackwardPredGain > valForwardPredGain + TH2) encoderObj->sGlobalStatInd += 1600;
        else if (valBackwardPredGain > valForwardPredGain + TH1) encoderObj->sGlobalStatInd +=  800;
        else if (valBackwardPredGain > valForwardPredGain)       encoderObj->sGlobalStatInd +=  400;

    }

    if      (valBackwardPredGain < valForwardPredGain -  TH5) encoderObj->sGlobalStatInd -= 6400;
    else if (valBackwardPredGain < valForwardPredGain -  TH4) encoderObj->sGlobalStatInd -= 3200;
    else if (valBackwardPredGain < valForwardPredGain -  TH3) encoderObj->sGlobalStatInd -= 1600;
    else if (valBackwardPredGain < valForwardPredGain -  TH2) encoderObj->sGlobalStatInd -=  800;
    else if (valBackwardPredGain < valForwardPredGain -  TH1) encoderObj->sGlobalStatInd -=  400;

    CLIP_TO_UPLEVEL(encoderObj->sGlobalStatInd,32000);
    CLIP_TO_LOWLEVEL(encoderObj->sGlobalStatInd,0);
    return;
}

void SetLPCMode_G729FPE(G729FPEncoder_Obj* encoderObj, float *pSrcSignal, float *pSrcForwardLPCFilter,
                      float *pSrcBackwardLPCFilter, int *pDstLPCMode, float *pSrcLSP,float *pExtBuff)
{

    int     i;
    float  *pLPCFlt, *PPtr;
    float  fGap, forwardPredGain, backwardPredGain, intBackwardPredGain;
    float  LSPThreshold, LSPDist, fTmp;
    float ener_DB_pSrcSignal;

    PPtr = &pExtBuff[0]; /*FRM_LEN elements*/

    ener_DB_pSrcSignal = CalcEnergy_dB_G729(pSrcSignal, FRM_LEN);

    pLPCFlt = pSrcBackwardLPCFilter + BWD_LPC_ORDERP1;
    /* Calc backward filter prediction gain (without interpolation ) */
    ippsConvBiased_32f(pLPCFlt,BWD_LPC_ORDER+1,pSrcSignal,FRM_LEN+BWD_LPC_ORDER,PPtr,FRM_LEN,BWD_LPC_ORDER);
    backwardPredGain = ener_DB_pSrcSignal - CalcEnergy_dB_G729(PPtr, FRM_LEN);

    /* Interpolated backward filter for the first sub-frame       */
    InterpolatedBackwardFilter_G729(pSrcBackwardLPCFilter, encoderObj->PrevFlt, &encoderObj->fInterpolationCoeff);

    /* Calc interpolated backward filter prediction gain */
    ippsConvBiased_32f(pSrcBackwardLPCFilter,BWD_LPC_ORDER+1,pSrcSignal,SUBFR_LEN+BWD_LPC_ORDER,PPtr,SUBFR_LEN,BWD_LPC_ORDER);
    ippsConvBiased_32f(pLPCFlt,BWD_LPC_ORDER+1,&pSrcSignal[SUBFR_LEN],SUBFR_LEN+BWD_LPC_ORDER,&PPtr[SUBFR_LEN],SUBFR_LEN,BWD_LPC_ORDER);
    intBackwardPredGain = ener_DB_pSrcSignal - CalcEnergy_dB_G729(PPtr, FRM_LEN);

    /* Calc forward filter prediction gain */
    ippsConvBiased_32f(pSrcForwardLPCFilter,LPC_ORDER+1,pSrcSignal,SUBFR_LEN+LPC_ORDER,PPtr,SUBFR_LEN,LPC_ORDER);
    ippsConvBiased_32f(&pSrcForwardLPCFilter[LPC_ORDERP1],LPC_ORDER+1,&pSrcSignal[SUBFR_LEN],SUBFR_LEN+LPC_ORDER,&PPtr[SUBFR_LEN],SUBFR_LEN,LPC_ORDER);
    forwardPredGain = ener_DB_pSrcSignal - CalcEnergy_dB_G729(PPtr, FRM_LEN);

    /* Choose: backward/forward mode.*/
    /* 1st criterion with prediction gains. The global stationnarity index
         is used to adapt the threshold value " GAP ".*/

    /* Do the threshold adaptation according to the global stationnarity indicator */
    fGap = (float)(encoderObj->sGlobalStatInd) * GAP_FACT;
    fGap += 1.f;

    if ( (intBackwardPredGain > forwardPredGain - fGap)&& (backwardPredGain > forwardPredGain - fGap)&&
         (backwardPredGain > 0.f)               && (intBackwardPredGain > 0.f) ) *pDstLPCMode = 1;
    else *pDstLPCMode = 0;

    if (encoderObj->sGlobalStatInd < 13000) *pDstLPCMode = 0; /* => Forward mode imposed */

    /* 2nd criterion with a distance between 2 successive LSP vectors */
    /* Computation of the LPC distance */
    LSPDist = 0;
    for(i=0; i<LPC_ORDER; i++){
        fTmp = encoderObj->OldLSP[i] - pSrcLSP[i];
        LSPDist += fTmp * fTmp;
    }

    /* Adaptation of the LSPs thresholds */
    if (encoderObj->sGlobalStatInd < 32000) {
        LSPThreshold = 0.f;
    }
    else {
        LSPThreshold = 0.03f;
    }

    /* Switching backward -> forward forbidden in case of a LPC stationnary */
    if ((LSPDist < LSPThreshold) &&(*pDstLPCMode == 0)&&(encoderObj->prevLPCMode == 1)
         &&(backwardPredGain > 0.f)&&(intBackwardPredGain > 0.f)) {
        *pDstLPCMode = 1;
    }

    /* Low energy frame => Forward mode chosen */

    if (ener_DB_pSrcSignal < THRES_ENERGY) {
        *pDstLPCMode = 0;
        if (encoderObj->sGlobalStatInd > 13000) encoderObj->sGlobalStatInd = 13000;
    }
    else isBackwardModeDominant_G729(&encoderObj->isBWDDominant, *pDstLPCMode,&encoderObj->sBWDFrmCounter,&encoderObj->sFWDFrmCounter);

    /* Adaptation of the global stationnarity indicator */
    if (ener_DB_pSrcSignal >= THRES_ENERGY) GlobalStationnarityAdaptation_G729E(encoderObj,backwardPredGain, forwardPredGain, *pDstLPCMode);
    if(*pDstLPCMode == 0) encoderObj->fInterpolationCoeff = 1.1f;
    return;
}

static void NormalizedCorrelation(float *pSrcExc, float *pSrcTargetVector, float *pSrcImpulseResponse,
                                  int len, int lagMin, int lagMax, float *pDstCorr, float *pTmpFltPastExc)
{
   int    i, k;
   Ipp64f dEnergy, dTmp;

   k = -lagMin;

   ippsConvBiased_32f( &pSrcExc[k], len , pSrcImpulseResponse, len ,  pTmpFltPastExc, len ,0);

   /* loop for every possible period */
   for (i = lagMin; i < lagMax; i++) {
      /* Compute energie of pFltExc[] */
      ippsDotProd_32f64f(pTmpFltPastExc, pTmpFltPastExc, len, &dEnergy);

      /* Compute correlation between pSrcTargetVector[] and pFltExc[] */
      ippsDotProd_32f64f(pSrcTargetVector, pTmpFltPastExc, len, &dTmp);

      /* Normalize correlation = correlation * (1/sqrt(energie)) */
      pDstCorr[i] = (Ipp32f)(dTmp)/((Ipp32f)sqrt(dEnergy+0.01));

      /* modify the filtered excitation pFltExc[] for the next iteration */
      k--;
      ippsFilteredExcitation_G729_32f( pSrcImpulseResponse, pTmpFltPastExc, len, pSrcExc[k]);
   }

   /* Compute energie of pFltExc[] */
   ippsDotProd_32f64f(pTmpFltPastExc, pTmpFltPastExc, len, &dEnergy);

   /* Compute correlation between pSrcTargetVector[] and pFltExc[] */
   ippsDotProd_32f64f(pSrcTargetVector, pTmpFltPastExc, len, &dTmp);

   /* Normalize correlation = correlation * (1/sqrt(energie)) */
   pDstCorr[lagMax] = (Ipp32f)(dTmp)/((Ipp32f)sqrt(dEnergy+0.01));

   return;

}

static __ALIGN32 CONST float ReorderInter_3[2*3*INTERPOL4_LEN] = {
 0.015738f,-0.047624f, 0.084078f, 0.900839f,
 0.084078f,-0.047624f, 0.015738f, 0.000000f,
 0.000000f, 0.016285f,-0.105570f, 0.760084f,
 0.424082f,-0.121120f, 0.031217f,-0.005925f,
-0.005925f, 0.031217f,-0.121120f, 0.424082f,
 0.760084f,-0.105570f, 0.016285f, 0.000000f
};

static float Interpolation_3(float *pSrc, int lFrac)
{
   int i;
   float sum, *x1;
   const float *c;

   x1 = &pSrc[-(INTERPOL4_LEN-1)];
   if (lFrac < 0) {
      x1--;
      lFrac += UP_SAMPLING;
   }
   c = &ReorderInter_3[2*INTERPOL4_LEN*lFrac];

   sum = 0.0f;
   for(i=0; i< 2*INTERPOL4_LEN; i++)
      sum+= x1[i] * c[i];

   return sum;
}

int AdaptiveCodebookSearch_G729_32f(float *pSrcExc, float *pSrcTargetVector, float *pSrcImpulseResponse, int len,
                        int minLag, int maxLag, int valSubframeNum, int *pDstFracPitch, G729Codec_Type codecType,float *pExtBuff)
{
   int    i, lFracPart;
   int    lBestLag, lMin, lMax;
   float  max;
   float  fIntCorr;
   float  *pFltExc;     /* filtered past excitation */
   float  *pCorr;
   int midLag;

   pFltExc = &pExtBuff[0];

   /* Find interval to compute normalized correlation */
   lMin = minLag - INTERPOL4_LEN;
   lMax = maxLag + INTERPOL4_LEN;

   pCorr = &pExtBuff[SUBFR_LEN - lMin]; /*Actually (10+2*INTERPOL4_LEN) from [SUBFR_LEN]. pCorr[lMin:lMax]*/

   /* Compute normalized correlation between target and filtered excitation */
   NormalizedCorrelation(pSrcExc, pSrcTargetVector, pSrcImpulseResponse, len, lMin, lMax, pCorr,pFltExc);

   /* find integer pitch */
   max = pCorr[minLag];
   lBestLag  = minLag;

   for(i= minLag+1; i<=maxLag; i++) {
      if( pCorr[i] >= max) {
         max = pCorr[i];
         lBestLag = i;
      }
   }

   /* If first subframe and lBestLag > 84 do not search fractionnal pitch */

   if( (valSubframeNum == 0) && (lBestLag > 84) ) {
      *pDstFracPitch = 0;
      return(lBestLag);
   }

   /* test the fractions around lBestLag and choose the one which maximizes the interpolated normalized correlation */

   if (codecType == G729D_CODEC) {    /* 6.4 kbps */
      if (valSubframeNum == 0) {
         max  = Interpolation_3(&pCorr[lBestLag], -2);
         lFracPart = -2;

         for (i = -1; i <= 2; i++) {
            fIntCorr = Interpolation_3(&pCorr[lBestLag], i);
            if(fIntCorr > max) {
               max = fIntCorr;
               lFracPart = i;
            }
         }
      } else {
         midLag = maxLag - 4;
         if ((lBestLag == midLag - 1) || lBestLag == midLag) {
            max  = Interpolation_3(&pCorr[lBestLag], -2);
            lFracPart = -2;

            for (i = -1; i <= 2; i++) {
               fIntCorr = Interpolation_3(&pCorr[lBestLag], i);
               if(fIntCorr > max) {
                  max = fIntCorr;
                  lFracPart = i;
               }
            }
         } else if (lBestLag == midLag - 2) {
            max  = Interpolation_3(&pCorr[lBestLag], 0);
            lFracPart = 0;

            for (i = 1; i <= 2; i++) {
               fIntCorr = Interpolation_3(&pCorr[lBestLag], i);
               if(fIntCorr > max) {
                  max = fIntCorr;
                  lFracPart = i;
               }
            }
         } else if (lBestLag == midLag + 1) {
            max  = Interpolation_3(&pCorr[lBestLag], -2);
            lFracPart = -2;

            for (i = -1; i <= 0; i++) {
               fIntCorr = Interpolation_3(&pCorr[lBestLag], i);
               if(fIntCorr > max) {
                  max = fIntCorr;
                  lFracPart = i;
               }
            }
         } else
            lFracPart = 0;
      }
   } else {
      max  = Interpolation_3(&pCorr[lBestLag], -2);
      lFracPart = -2;

      for (i = -1; i <= 2; i++) {
         fIntCorr = Interpolation_3(&pCorr[lBestLag], i);
         if(fIntCorr > max) {
            max = fIntCorr;
            lFracPart = i;
         }
      }
   }

   /* limit the fraction value in the interval [-1,0,1] */

   if (lFracPart == -2) {
      lFracPart = 1;
      lBestLag -= 1;
   }
   if (lFracPart == 2) {
      lFracPart = -1;
      lBestLag += 1;
   }

   *pDstFracPitch = lFracPart;

   return lBestLag;
}

void CNGGetSize(Ipp32s *pDstSize)
{
   *pDstSize = sizeof(CNGmemory);
   return;
}

void CNGInit(char *cngMem)
{
   CNGmemory *cngState = (CNGmemory *)cngMem;
   ippsZero_16s((short*)cngState,sizeof(CNGmemory)>>1) ;

   ippsZero_32f(cngState->SumAutoCorrs,SUMAUTOCORRS_SIZE);
   ippsZero_32f(cngState->AutoCorrs,AUTOCORRS_SIZE);
   ippsZero_32f(cngState->Energies,GAINS_NUM);

   cngState->fCurrGain = 0.;
   cngState->lAutoCorrsCounter = 0;
   cngState->lFltChangeFlag = 0;
}

static float gaussGen(short *sCNGSeed)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -