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

📄 utilgsmamr.c

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

      shift = shift1 + 2 - shift2;
      if(shift >= 0) tmp[i] >>= shift;
      else           tmp[i] <<= -shift;
   }

   ippsSum_16s_Sfs(tmp, LP_ORDER_SIZE, &diff, 0);

   if(diff > 5325) {
      *vHgAverageVar = *vHgAverageVar + 1;
      if(*vHgAverageVar > 10) *vHgAverageCount = 0;
   } else *vHgAverageVar = 0;

   /* Compute mix constant (bgMix) */
   bgMix = 8192;    /* 1 in Q13 */
   if ((rate <= IPP_SPCHBR_6700) || (rate == IPP_SPCHBR_10200))
   {
      /* if errors and presumed noise make smoothing probability stronger */
      if (((((potDegBadFrame != 0) && (vPrevDegBadFr != 0)) || (badFrame != 0) || (vPrevBadFr != 0)) &&
          (vVoiceHangover > 1) && (vBackgroundNoise != 0) &&
          ((rate == IPP_SPCHBR_4750) || (rate == IPP_SPCHBR_5150) || (rate == IPP_SPCHBR_5900)) ))
      {
         if (diff > 4506) {
            if (6554 < diff) bgMix = 8192;
            else             bgMix = (diff - 4506) << 2;
         }
         else bgMix = 0;
      } else {
         if (diff > 3277) {
            if(5325 < diff)  bgMix = 8192;
            else             bgMix = (diff - 3277) << 2;
         }
         else bgMix = 0;
      }

      if((*vHgAverageCount < 40) || (diff > 5325)) bgMix = 8192;

      ippsSum_16s32s_Sfs(a_GainHistory+2, CBGAIN_HIST_SIZE-2, &L_sum, 0);
      L_sum*=6554;
      cbGainMean = (short)((L_sum + 0x4000) >> 15);

      if (((badFrame != 0) || (vPrevBadFr != 0)) && (vBackgroundNoise != 0) &&
          ((rate == IPP_SPCHBR_4750) ||
           (rate == IPP_SPCHBR_5150) ||
           (rate == IPP_SPCHBR_5900)) )
      {
         ippsSum_16s32s_Sfs(a_GainHistory, CBGAIN_HIST_SIZE, &L_sum, 0);
         L_sum*=4681;
         cbGainMean = (short)((L_sum + 0x4000) >> 15);
      }

      L_sum = bgMix*cbGainMix;
      L_sum += 8192*cbGainMean;
      L_sum -= bgMix*cbGainMean;
      cbGainMix = (short)((L_sum + 0x1000) >> 13);
   }

   *vHgAverageCount = *vHgAverageCount + 1;
   return cbGainMix;
}
/***************************************************************************
 *  Function:  ownCheckLSPVec_GSMAMR()                                                     *
 ***************************************************************************/
short ownCheckLSPVec_GSMAMR(short *count, short *lsp)
{
   short i, dist, dist_min1, dist_min2, dist_thresh;

   dist_min1 = IPP_MAX_16S;
   for (i = 3; i < LP_ORDER_SIZE-2; i++) {
      dist = lsp[i] - lsp[i+1];
      if(dist < dist_min1) dist_min1 = dist;
   }

   dist_min2 = IPP_MAX_16S;
   for (i = 1; i < 3; i++) {
      dist = lsp[i] - lsp[i+1];
      if (dist < dist_min2) dist_min2 = dist;
   }

   if(lsp[1] > 32000) dist_thresh = 600;
   else if (lsp[1] > 30500) dist_thresh = 800;
   else dist_thresh = 1100;

   if (dist_min1 < 1500 || dist_min2 < dist_thresh) *count = *count + 1;
   else                                             *count = 0;

   if (*count >= 12) {
      *count = 12;
      return 1;
   } else return 0;
   /* End of ownCheckLSPVec_GSMAMR() */
}
/***************************************************************************
 *  Function:  ownSubframePostProc_GSMAMR()
 ***************************************************************************/
int ownSubframePostProc_GSMAMR(short *pSpeechVec, IppSpchBitRate rate, short numSubfr,
                               short gainPitch, short gainCode, short *pAQnt, short *pLocSynth,
                               short *pTargetPitchVec, short *code, short *pFltAdaptExc, short *pFltVec,
                               short *a_MemorySyn, short *a_MemoryErr, short *a_Memory_W0, short *pLTPExc,
                               short *vFlagSharp)
{
   short i, j, k;
   short temp;
   int TmpVal;
   short tempShift;
   short kShift;
   short pitchFactor;

   if (rate != IPP_SPCHBR_12200) {
      tempShift = 1;
      kShift = 2;
      pitchFactor = gainPitch;
   } else {
      tempShift = 2;
      kShift = 4;
      pitchFactor = gainPitch >> 1;
   }

   *vFlagSharp = gainPitch;
   if(*vFlagSharp > PITCH_SHARP_MAX) *vFlagSharp = PITCH_SHARP_MAX;

   for (i = 0; i < SUBFR_SIZE_GSMAMR; i++) {
      TmpVal = (pLTPExc[i + numSubfr]*pitchFactor) + (code[i]*gainCode);
      TmpVal <<= tempShift;
      pLTPExc[i + numSubfr] = (short)((TmpVal + 0x4000) >> 15);
   }

   ippsSynthesisFilter_NR_16s_Sfs(pAQnt, &pLTPExc[numSubfr], &pLocSynth[numSubfr], SUBFR_SIZE_GSMAMR, 12, a_MemorySyn);
   ippsCopy_16s(&pLocSynth[numSubfr + SUBFR_SIZE_GSMAMR - LP_ORDER_SIZE], a_MemorySyn, LP_ORDER_SIZE);

   for (i = SUBFR_SIZE_GSMAMR - LP_ORDER_SIZE, j = 0; i < SUBFR_SIZE_GSMAMR; i++, j++) {
      a_MemoryErr[j] = pSpeechVec[numSubfr + i] - pLocSynth[numSubfr + i];
      temp = (short)((pFltAdaptExc[i]*gainPitch) >> 14);
      k = (short)((pFltVec[i]*gainCode) >> (15 - kShift));
      a_Memory_W0[j] = pTargetPitchVec[i] - temp - k;
   }

   return 0;
   /* End of ownSubframePostProc_GSMAMR() */
}

static __ALIGN32 CONST short TableInterN6[LEN_FIRFLT] =
{
    29443, 28346, 25207, 20449, 14701,  8693, 3143, -1352, -4402, -5865,
    -5850, -4673, -2783,  -672,  1211,  2536, 3130,  2991,  2259,  1170,
        0, -1001, -1652, -1868, -1666, -1147, -464,   218,   756,  1060,
     1099,   904,   550,   135,  -245,  -514, -634,  -602,  -451,  -231,
        0,   191,   308,   340,   296,   198,   78,   -36,  -120,  -163,
     -165,  -132,   -79,   -19,    34,    73,   91,    89,    70,    38,
     0
};

/*************************************************************************
 *  Function:   ownPredExcMode3_6_GSMAMR()
 *************************************************************************/
void ownPredExcMode3_6_GSMAMR(short *pLTPExc, short T0, short frac, short lenSubfr, short flag3)
{
    short i, j, k;
    short *x0, *x1, *x2;
    const short *c1, *c2;
    int s;

    x0 = &pLTPExc[-T0];

    frac = -frac;
    if(flag3 != 0) frac <<= 1;

    if (frac < 0) {
        frac += MAX_UPSAMPLING;
        x0--;
    }

    for (j = 0; j < lenSubfr; j++) {
        x1 = x0++;
        x2 = x0;
        c1 = &TableInterN6[frac];
        c2 = &TableInterN6[MAX_UPSAMPLING - frac];

        s = 0;
        for (i = 0, k = 0; i < LEN_INTERPOL_10; i++, k += MAX_UPSAMPLING) {
            s += x1[-i]*c1[k];
            s += x2[i]*c2[k];
        }
        pLTPExc[j] = (short)((s + 0x4000) >> 15);
    }

    return;
    /* End of ownPredExcMode3_6_GSMAMR() */
}

static __ALIGN32 CONST short TblPhImpLow_M795[] =
{
  26777,    801,   2505,   -683,  -1382,    582,    604,  -1274,
   3511,  -5894,   4534,   -499,  -1940,   3011,  -5058,   5614,
  -1990,  -1061,  -1459,   4442,   -700,  -5335,   4609,    452,
   -589,  -3352,   2953,   1267,  -1212,  -2590,   1731,   3670,
  -4475,   -975,   4391,  -2537,    949,  -1363,   -979,   5734,
  26777,    801,   2505,   -683,  -1382,    582,    604,  -1274,
   3511,  -5894,   4534,   -499,  -1940,   3011,  -5058,   5614,
  -1990,  -1061,  -1459,   4442,   -700,  -5335,   4609,    452,
   -589,  -3352,   2953,   1267,  -1212,  -2590,   1731,   3670,
   -4475,   -975,   4391,  -2537,    949,  -1363,   -979,   5734
};

static __ALIGN32 CONST short TblPhImpLow[] =
{
  14690,  11518,   1268,  -2761,  -5671,   7514,    -35,  -2807,
  -3040,   4823,   2952,  -8424,   3785,   1455,   2179,  -8637,
   8051,  -2103,  -1454,    777,   1108,  -2385,   2254,   -363,
   -674,  -2103,   6046,  -5681,   1072,   3123,  -5058,   5312,
  -2329,  -3728,   6924,  -3889,    675,  -1775,     29,  10145,
  14690,  11518,   1268,  -2761,  -5671,   7514,    -35,  -2807,
  -3040,   4823,   2952,  -8424,   3785,   1455,   2179,  -8637,
   8051,  -2103,  -1454,    777,   1108,  -2385,   2254,   -363,
   -674,  -2103,   6046,  -5681,   1072,   3123,  -5058,   5312,
  -2329,  -3728,   6924,  -3889,    675,  -1775,     29,  10145
};
static __ALIGN32 CONST short TblPhImpMid[] =
{
  30274,   3831,  -4036,   2972,  -1048,  -1002,   2477,  -3043,
   2815,  -2231,   1753,  -1611,   1714,  -1775,   1543,  -1008,
    429,   -169,    472,  -1264,   2176,  -2706,   2523,  -1621,
    344,    826,  -1529,   1724,  -1657,   1701,  -2063,   2644,
  -3060,   2897,  -1978,    557,    780,  -1369,    842,    655,
  30274,   3831,  -4036,   2972,  -1048,  -1002,   2477,  -3043,
   2815,  -2231,   1753,  -1611,   1714,  -1775,   1543,  -1008,
    429,   -169,    472,  -1264,   2176,  -2706,   2523,  -1621,
    344,    826,  -1529,   1724,  -1657,   1701,  -2063,   2644,
  -3060,   2897,  -1978,    557,    780,  -1369,    842,    655
};

/*************************************************************************
*  Proc:   ownPhaseDispersion_GSMAMR
**************************************************************************/
void ownPhaseDispersion_GSMAMR(sPhaseDispSt *state, IppSpchBitRate rate, short *pLTPExcSignal,
                               short cbGain, short ltpGain, short *innovVec, short pitchFactor,
                               short tmpShift)
{
   short i, i1;
   short tmp1;
   short impNr;

   IPP_ALIGNED_ARRAY(16, short, inno_sav, SUBFR_SIZE_GSMAMR);
   IPP_ALIGNED_ARRAY(16, short, ps_poss, SUBFR_SIZE_GSMAMR);
   short nze, nPulse;
   const short *ph_imp;

   for(i = LTP_GAIN_MEM_SIZE-1; i > 0; i--)
       state->a_GainMemory[i] = state->a_GainMemory[i-1];

   state->a_GainMemory[0] = ltpGain;

   if(ltpGain < LTP_THRESH2) {    /* if (ltpGain < 0.9) */
       if(ltpGain > LTP_THRESH1) impNr = 1;
       else                      impNr = 0;
   } else impNr = 2;

   tmp1 = ShiftL_16s(state->vPrevGain, 1);
   if(cbGain > tmp1) state->vOnSetGain = 2;
   else {
      if (state->vOnSetGain > 0) state->vOnSetGain -= 1;
   }

   if(state->vOnSetGain == 0) {
       i1 = 0;
       for(i = 0; i < LTP_GAIN_MEM_SIZE; i++) {
           if(state->a_GainMemory[i] < LTP_THRESH1) i1 ++;
       }
       if(i1 > 2) impNr = 0;
   }

   if((impNr > state->vPrevState + 1) && (state->vOnSetGain == 0)) impNr -= 1;

   if((impNr < 2) && (state->vOnSetGain > 0)) impNr += 1;

   if(cbGain < 10) impNr = 2;

   if(state->vFlagLockGain == 1) impNr = 0;

   state->vPrevState = impNr;
   state->vPrevGain = cbGain;

   if((rate != IPP_SPCHBR_12200) && (rate != IPP_SPCHBR_10200) && (rate != IPP_SPCHBR_7400) && (impNr < 2)) {
       nze = 0;
       for (i = 0; i < SUBFR_SIZE_GSMAMR; i++) {
           if (innovVec[i] != 0) {
               ps_poss[nze] = i;
               nze++;
           }
           inno_sav[i] = innovVec[i];
           innovVec[i] = 0;
       }

       if(rate == IPP_SPCHBR_7950) {
          if(impNr == 0) ph_imp = TblPhImpLow_M795;
          else           ph_imp = TblPhImpMid;
       } else {
          if(impNr == 0) ph_imp = TblPhImpLow;
          else           ph_imp = TblPhImpMid;
       }

       for (nPulse = 0; nPulse < nze; nPulse++) {
           short amp = inno_sav[ps_poss[nPulse]];
           const short *posImp = &ph_imp[SUBFR_SIZE_GSMAMR-ps_poss[nPulse]];

           /*ippsMulC_16s_Sfs(&ph_imp[SUBFR_SIZE_GSMAMR-ps_poss[nPulse]],inno_sav[ps_poss[nPulse]],tmpV,L_SUBFR,15);
           ippsAdd_16s_I(tmpV,innovVec,SUBFR_SIZE_GSMAMR);*/
           for(i = 0; i < SUBFR_SIZE_GSMAMR; i++)
              innovVec[i] += (amp * posImp[i]) >> 15;
       }
   }

   ippsInterpolateC_NR_G729_16s_Sfs(innovVec, cbGain, pLTPExcSignal, pitchFactor,
                                    pLTPExcSignal, SUBFR_SIZE_GSMAMR, (15-tmpShift));
   return;
   /* End of ownPhaseDispersion_GSMAMR() */
}
/*************************************************************************
*  Function    : ownSourceChDetectorUpdate_GSMAMR
**************************************************************************/
short ownSourceChDetectorUpdate_GSMAMR(short *a_EnergyHistVector, short *vCountHangover,
                                       short *ltpGainHist, short *pSpeechVec, short *vVoiceHangover)
{
   short i;
   short prevVoiced, inbgNoise;
   short temp;
   short ltpLimit, frameEnergyMin;
   short currEnergy, noiseFloor, maxEnergy, maxEnergyLastPart;
   int s;

   currEnergy = 0;
   s =  0;

   ippsDotProd_16s32s_Sfs(pSpeechVec, pSpeechVec, FRAME_SIZE_GSMAMR, &s, 0);
   currEnergy = (short)(s >> 13);
   frameEnergyMin = 32767;

   for(i = 0; i < ENERGY_HIST_SIZE; i++) {
      if(a_EnergyHistVector[i] < frameEnergyMin)
         frameEnergyMin = a_EnergyHistVector[i];
   }

   noiseFloor = (short)Mul16_32s(frameEnergyMin);
   maxEnergy = a_EnergyHistVector[0];

   for(i = 1; i < ENERGY_HIST_SIZE-4; i++) {
      if(maxEnergy < a_EnergyHistVector[i])
         maxEnergy = a_EnergyHistVector[i];
   }

   maxEnergyLastPart = a_EnergyHistVector[2*ENERGY_HIST_SIZE/3];
   for(i = 2*ENERGY_HIST_SIZE/3+1; i < ENERGY_HIST_SIZE; i++) {
      if(maxEnergyLastPart < a_EnergyHistVector[i])
         maxEnergyLastPart = a_EnergyHistVector[i];
   }

   inbgNoise = 0;
   if((maxEnergy > LOW_NOISE_LIMIT) &&
      (currEnergy < THRESH_ENERGY_LIMIT) &&
      (currEnergy > LOW_NOISE_LIMIT) &&
      ((currEnergy < noiseFloor) || (maxEnergyLastPart < UPP_NOISE_LIMIT)))
   {
      if (*vCountHangover + 1 > 30) *vCountHangover = 30;
      else                          *vCountHangover = *vCountHangover + 1;
   } else {
      *vCountHangover = 0;
   }

   if(*vCountHangover > 1) inbgNoise = 1;

   for (i = 0; i < ENERGY_HIST_SIZE-1; i++)
      a_EnergyHistVector[i] = a_EnergyHistVector[i+1];

   a_EnergyHistVector[ENERGY_HIST_SIZE-1] = currEnergy;

   ltpLimit = 13926;
   if(*vCountHangover > 8)  ltpLimit = 15565;
   if(*vCountHangover > 15) ltpLimit = 16383;

⌨️ 快捷键说明

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