/* 1D parabolic interpolation . All input and output values are in Q8 */ static __inline void Intrp1DQ8(int32_t *x, int32_t *fx, int32_t *y, int32_t *fy) { int16_t sign1=1, sign2=1; int32_t r32, q32, t32, nom32, den32; int16_t t16, tmp16, tmp16_1; if ((fx[0]>0) && (fx[2]>0)) { r32=fx[1]-fx[2]; q32=fx[0]-fx[1]; nom32=q32+r32; den32=WEBRTC_SPL_MUL_32_16((q32-r32), 2); if (nom32<0) sign1=-1; if (den32<0) sign2=-1; /* t = (q32+r32)/(2*(q32-r32)) = (fx[0]-fx[1] + fx[1]-fx[2])/(2 * fx[0]-fx[1] - (fx[1]-fx[2]))*/ /* (Signs are removed because WebRtcSpl_DivResultInQ31 can't handle negative numbers) */ t32=WebRtcSpl_DivResultInQ31(WEBRTC_SPL_MUL_32_16(nom32, sign1),WEBRTC_SPL_MUL_32_16(den32, sign2)); /* t in Q31, without signs */ t16=(int16_t)WEBRTC_SPL_RSHIFT_W32(t32, 23); /* Q8 */ t16=t16*sign1*sign2; /* t in Q8 with signs */ *y = x[0]+t16; /* Q8 */ // *y = x[1]+t16; /* Q8 */ /* The following code calculates fy in three steps */ /* fy = 0.5 * t * (t-1) * fx[0] + (1-t*t) * fx[1] + 0.5 * t * (t+1) * fx[2]; */ /* Part I: 0.5 * t * (t-1) * fx[0] */ tmp16_1=(int16_t)WEBRTC_SPL_MUL_16_16(t16,t16); /* Q8*Q8=Q16 */ tmp16_1 = WEBRTC_SPL_RSHIFT_W16(tmp16_1,2); /* Q16>>2 = Q14 */ t16 = (int16_t)WEBRTC_SPL_MUL_16_16(t16, 64); /* Q8<<6 = Q14 */ tmp16 = tmp16_1-t16; *fy = WEBRTC_SPL_MUL_16_32_RSFT15(tmp16, fx[0]); /* (Q14 * Q8 >>15)/2 = Q8 */ /* Part II: (1-t*t) * fx[1] */ tmp16 = 16384-tmp16_1; /* 1 in Q14 - Q14 */ *fy += WEBRTC_SPL_MUL_16_32_RSFT14(tmp16, fx[1]);/* Q14 * Q8 >> 14 = Q8 */ /* Part III: 0.5 * t * (t+1) * fx[2] */ tmp16 = tmp16_1+t16; *fy += WEBRTC_SPL_MUL_16_32_RSFT15(tmp16, fx[2]);/* (Q14 * Q8 >>15)/2 = Q8 */ } else { *y = x[0]; *fy= fx[1]; } }
void WebRtcIsacfix_Time2SpecC(int16_t *inre1Q9, int16_t *inre2Q9, int16_t *outreQ7, int16_t *outimQ7) { int k; int32_t tmpreQ16[FRAMESAMPLES/2], tmpimQ16[FRAMESAMPLES/2]; int16_t tmp1rQ14, tmp1iQ14; int32_t xrQ16, xiQ16, yrQ16, yiQ16; int32_t v1Q16, v2Q16; int16_t factQ19, sh; /* Multiply with complex exponentials and combine into one complex vector */ factQ19 = 16921; // 0.5/sqrt(240) in Q19 is round(.5/sqrt(240)*(2^19)) = 16921 for (k = 0; k < FRAMESAMPLES/2; k++) { tmp1rQ14 = WebRtcIsacfix_kCosTab1[k]; tmp1iQ14 = WebRtcIsacfix_kSinTab1[k]; xrQ16 = WEBRTC_SPL_RSHIFT_W32(WEBRTC_SPL_MUL_16_16(tmp1rQ14, inre1Q9[k]) + WEBRTC_SPL_MUL_16_16(tmp1iQ14, inre2Q9[k]), 7); xiQ16 = WEBRTC_SPL_RSHIFT_W32(WEBRTC_SPL_MUL_16_16(tmp1rQ14, inre2Q9[k]) - WEBRTC_SPL_MUL_16_16(tmp1iQ14, inre1Q9[k]), 7); tmpreQ16[k] = WEBRTC_SPL_RSHIFT_W32(WEBRTC_SPL_MUL_16_32_RSFT16(factQ19, xrQ16)+4, 3); // (Q16*Q19>>16)>>3 = Q16 tmpimQ16[k] = WEBRTC_SPL_RSHIFT_W32(WEBRTC_SPL_MUL_16_32_RSFT16(factQ19, xiQ16)+4, 3); // (Q16*Q19>>16)>>3 = Q16 } xrQ16 = WebRtcSpl_MaxAbsValueW32(tmpreQ16, FRAMESAMPLES/2); yrQ16 = WebRtcSpl_MaxAbsValueW32(tmpimQ16, FRAMESAMPLES/2); if (yrQ16>xrQ16) { xrQ16 = yrQ16; } sh = WebRtcSpl_NormW32(xrQ16); sh = sh-24; //if sh becomes >=0, then we should shift sh steps to the left, and the domain will become Q(16+sh) //if sh becomes <0, then we should shift -sh steps to the right, and the domain will become Q(16+sh) //"Fastest" vectors if (sh>=0) { for (k=0; k<FRAMESAMPLES/2; k++) { inre1Q9[k] = (int16_t) WEBRTC_SPL_LSHIFT_W32(tmpreQ16[k], sh); //Q(16+sh) inre2Q9[k] = (int16_t) WEBRTC_SPL_LSHIFT_W32(tmpimQ16[k], sh); //Q(16+sh) } } else { int32_t round = WEBRTC_SPL_LSHIFT_W32((int32_t)1, -sh-1); for (k=0; k<FRAMESAMPLES/2; k++) { inre1Q9[k] = (int16_t) WEBRTC_SPL_RSHIFT_W32(tmpreQ16[k]+round, -sh); //Q(16+sh) inre2Q9[k] = (int16_t) WEBRTC_SPL_RSHIFT_W32(tmpimQ16[k]+round, -sh); //Q(16+sh) } } /* Get DFT */ WebRtcIsacfix_FftRadix16Fastest(inre1Q9, inre2Q9, -1); // real call //"Fastest" vectors if (sh>=0) { for (k=0; k<FRAMESAMPLES/2; k++) { tmpreQ16[k] = WEBRTC_SPL_RSHIFT_W32((int32_t)inre1Q9[k], sh); //Q(16+sh) -> Q16 tmpimQ16[k] = WEBRTC_SPL_RSHIFT_W32((int32_t)inre2Q9[k], sh); //Q(16+sh) -> Q16 } } else { for (k=0; k<FRAMESAMPLES/2; k++) { tmpreQ16[k] = WEBRTC_SPL_LSHIFT_W32((int32_t)inre1Q9[k], -sh); //Q(16+sh) -> Q16 tmpimQ16[k] = WEBRTC_SPL_LSHIFT_W32((int32_t)inre2Q9[k], -sh); //Q(16+sh) -> Q16 } } /* Use symmetry to separate into two complex vectors and center frames in time around zero */ for (k = 0; k < FRAMESAMPLES/4; k++) { xrQ16 = tmpreQ16[k] + tmpreQ16[FRAMESAMPLES/2 - 1 - k]; yiQ16 = -tmpreQ16[k] + tmpreQ16[FRAMESAMPLES/2 - 1 - k]; xiQ16 = tmpimQ16[k] - tmpimQ16[FRAMESAMPLES/2 - 1 - k]; yrQ16 = tmpimQ16[k] + tmpimQ16[FRAMESAMPLES/2 - 1 - k]; tmp1rQ14 = -WebRtcIsacfix_kSinTab2[FRAMESAMPLES/4 - 1 - k]; tmp1iQ14 = WebRtcIsacfix_kSinTab2[k]; v1Q16 = WEBRTC_SPL_MUL_16_32_RSFT14(tmp1rQ14, xrQ16) - WEBRTC_SPL_MUL_16_32_RSFT14(tmp1iQ14, xiQ16); v2Q16 = WEBRTC_SPL_MUL_16_32_RSFT14(tmp1iQ14, xrQ16) + WEBRTC_SPL_MUL_16_32_RSFT14(tmp1rQ14, xiQ16); outreQ7[k] = (int16_t) WEBRTC_SPL_RSHIFT_W32(v1Q16, 9); outimQ7[k] = (int16_t) WEBRTC_SPL_RSHIFT_W32(v2Q16, 9); v1Q16 = -WEBRTC_SPL_MUL_16_32_RSFT14(tmp1iQ14, yrQ16) - WEBRTC_SPL_MUL_16_32_RSFT14(tmp1rQ14, yiQ16); v2Q16 = -WEBRTC_SPL_MUL_16_32_RSFT14(tmp1rQ14, yrQ16) + WEBRTC_SPL_MUL_16_32_RSFT14(tmp1iQ14, yiQ16); outreQ7[FRAMESAMPLES/2 - 1 - k] = (int16_t)WEBRTC_SPL_RSHIFT_W32(v1Q16, 9); //CalcLrIntQ(v1Q16, 9); outimQ7[FRAMESAMPLES/2 - 1 - k] = (int16_t)WEBRTC_SPL_RSHIFT_W32(v2Q16, 9); //CalcLrIntQ(v2Q16, 9); } }
void WebRtcIsacfix_Spec2TimeC(int16_t *inreQ7, int16_t *inimQ7, int32_t *outre1Q16, int32_t *outre2Q16) { int k; int16_t tmp1rQ14, tmp1iQ14; int32_t xrQ16, xiQ16, yrQ16, yiQ16; int32_t tmpInRe, tmpInIm, tmpInRe2, tmpInIm2; int16_t factQ11; int16_t sh; for (k = 0; k < FRAMESAMPLES/4; k++) { /* Move zero in time to beginning of frames */ tmp1rQ14 = -WebRtcIsacfix_kSinTab2[FRAMESAMPLES/4 - 1 - k]; tmp1iQ14 = WebRtcIsacfix_kSinTab2[k]; tmpInRe = WEBRTC_SPL_LSHIFT_W32((int32_t) inreQ7[k], 9); // Q7 -> Q16 tmpInIm = WEBRTC_SPL_LSHIFT_W32((int32_t) inimQ7[k], 9); // Q7 -> Q16 tmpInRe2 = WEBRTC_SPL_LSHIFT_W32((int32_t) inreQ7[FRAMESAMPLES/2 - 1 - k], 9); // Q7 -> Q16 tmpInIm2 = WEBRTC_SPL_LSHIFT_W32((int32_t) inimQ7[FRAMESAMPLES/2 - 1 - k], 9); // Q7 -> Q16 xrQ16 = WEBRTC_SPL_MUL_16_32_RSFT14(tmp1rQ14, tmpInRe) + WEBRTC_SPL_MUL_16_32_RSFT14(tmp1iQ14, tmpInIm); xiQ16 = WEBRTC_SPL_MUL_16_32_RSFT14(tmp1rQ14, tmpInIm) - WEBRTC_SPL_MUL_16_32_RSFT14(tmp1iQ14, tmpInRe); yrQ16 = -WEBRTC_SPL_MUL_16_32_RSFT14(tmp1rQ14, tmpInIm2) - WEBRTC_SPL_MUL_16_32_RSFT14(tmp1iQ14, tmpInRe2); yiQ16 = -WEBRTC_SPL_MUL_16_32_RSFT14(tmp1rQ14, tmpInRe2) + WEBRTC_SPL_MUL_16_32_RSFT14(tmp1iQ14, tmpInIm2); /* Combine into one vector, z = x + j * y */ outre1Q16[k] = xrQ16 - yiQ16; outre1Q16[FRAMESAMPLES/2 - 1 - k] = xrQ16 + yiQ16; outre2Q16[k] = xiQ16 + yrQ16; outre2Q16[FRAMESAMPLES/2 - 1 - k] = -xiQ16 + yrQ16; } /* Get IDFT */ tmpInRe = WebRtcSpl_MaxAbsValueW32(outre1Q16, 240); tmpInIm = WebRtcSpl_MaxAbsValueW32(outre2Q16, 240); if (tmpInIm>tmpInRe) { tmpInRe = tmpInIm; } sh = WebRtcSpl_NormW32(tmpInRe); sh = sh-24; //if sh becomes >=0, then we should shift sh steps to the left, and the domain will become Q(16+sh) //if sh becomes <0, then we should shift -sh steps to the right, and the domain will become Q(16+sh) //"Fastest" vectors if (sh>=0) { for (k=0; k<240; k++) { inreQ7[k] = (int16_t) WEBRTC_SPL_LSHIFT_W32(outre1Q16[k], sh); //Q(16+sh) inimQ7[k] = (int16_t) WEBRTC_SPL_LSHIFT_W32(outre2Q16[k], sh); //Q(16+sh) } } else { int32_t round = WEBRTC_SPL_LSHIFT_W32((int32_t)1, -sh-1); for (k=0; k<240; k++) { inreQ7[k] = (int16_t) WEBRTC_SPL_RSHIFT_W32(outre1Q16[k]+round, -sh); //Q(16+sh) inimQ7[k] = (int16_t) WEBRTC_SPL_RSHIFT_W32(outre2Q16[k]+round, -sh); //Q(16+sh) } } WebRtcIsacfix_FftRadix16Fastest(inreQ7, inimQ7, 1); // real call //"Fastest" vectors if (sh>=0) { for (k=0; k<240; k++) { outre1Q16[k] = WEBRTC_SPL_RSHIFT_W32((int32_t)inreQ7[k], sh); //Q(16+sh) -> Q16 outre2Q16[k] = WEBRTC_SPL_RSHIFT_W32((int32_t)inimQ7[k], sh); //Q(16+sh) -> Q16 } } else { for (k=0; k<240; k++) { outre1Q16[k] = WEBRTC_SPL_LSHIFT_W32((int32_t)inreQ7[k], -sh); //Q(16+sh) -> Q16 outre2Q16[k] = WEBRTC_SPL_LSHIFT_W32((int32_t)inimQ7[k], -sh); //Q(16+sh) -> Q16 } } /* Divide through by the normalizing constant: */ /* scale all values with 1/240, i.e. with 273 in Q16 */ /* 273/65536 ~= 0.0041656 */ /* 1/240 ~= 0.0041666 */ for (k=0; k<240; k++) { outre1Q16[k] = WEBRTC_SPL_MUL_16_32_RSFT16(273, outre1Q16[k]); outre2Q16[k] = WEBRTC_SPL_MUL_16_32_RSFT16(273, outre2Q16[k]); } /* Demodulate and separate */ factQ11 = 31727; // sqrt(240) in Q11 is round(15.49193338482967*2048) = 31727 for (k = 0; k < FRAMESAMPLES/2; k++) { tmp1rQ14 = WebRtcIsacfix_kCosTab1[k]; tmp1iQ14 = WebRtcIsacfix_kSinTab1[k]; xrQ16 = WEBRTC_SPL_MUL_16_32_RSFT14(tmp1rQ14, outre1Q16[k]) - WEBRTC_SPL_MUL_16_32_RSFT14(tmp1iQ14, outre2Q16[k]); xiQ16 = WEBRTC_SPL_MUL_16_32_RSFT14(tmp1rQ14, outre2Q16[k]) + WEBRTC_SPL_MUL_16_32_RSFT14(tmp1iQ14, outre1Q16[k]); xrQ16 = WEBRTC_SPL_MUL_16_32_RSFT11(factQ11, xrQ16); xiQ16 = WEBRTC_SPL_MUL_16_32_RSFT11(factQ11, xiQ16); outre2Q16[k] = xiQ16; outre1Q16[k] = xrQ16; } }
void WebRtcIsacfix_PitchFilterGains(const int16_t* indatQ0, PitchFiltstr* pfp, int16_t* lagsQ7, int16_t* gainsQ12) { int k, n, m, ind, pos, pos3QQ; int16_t ubufQQ[PITCH_INTBUFFSIZE]; int16_t oldLagQ7, lagdeltaQ7, curLagQ7; const int16_t* fracoeffQQ = NULL; int16_t scale; int16_t cnt = 0, frcQQ, indW16 = 0, tmpW16; int32_t tmpW32, tmp2W32, csum1QQ, esumxQQ; // Set up buffer and states. memcpy(ubufQQ, pfp->ubufQQ, sizeof(pfp->ubufQQ)); oldLagQ7 = pfp->oldlagQ7; // No interpolation if pitch lag step is big. if ((WEBRTC_SPL_MUL_16_16_RSFT(lagsQ7[0], 3, 1) < oldLagQ7) || (lagsQ7[0] > WEBRTC_SPL_MUL_16_16_RSFT(oldLagQ7, 3, 1))) { oldLagQ7 = lagsQ7[0]; } ind = 0; pos = ind + PITCH_BUFFSIZE; scale = 0; for (k = 0; k < PITCH_SUBFRAMES; k++) { // Calculate interpolation steps. lagdeltaQ7 = lagsQ7[k] - oldLagQ7; lagdeltaQ7 = (int16_t)WEBRTC_SPL_MUL_16_16_RSFT_WITH_ROUND( lagdeltaQ7, kDivFactor, 15); curLagQ7 = oldLagQ7; oldLagQ7 = lagsQ7[k]; csum1QQ = 1; esumxQQ = 1; // Same as function WebRtcIsacfix_PitchFilter(), we break the pitch // filtering into two for-loops (5 x 12) below. for (cnt = 0; cnt < kSegments; cnt++) { // Update parameters for each segment. curLagQ7 += lagdeltaQ7; indW16 = (int16_t)CalcLrIntQ(curLagQ7, 7); tmpW16 = WEBRTC_SPL_LSHIFT_W16(indW16, 7); tmpW16 -= curLagQ7; frcQQ = WEBRTC_SPL_RSHIFT_W16(tmpW16, 4); frcQQ += 4; if (frcQQ == PITCH_FRACS) { frcQQ = 0; } fracoeffQQ = kIntrpCoef[frcQQ]; pos3QQ = pos - (indW16 + 4); for (n = 0; n < PITCH_SUBFRAME_LEN / kSegments; n++) { // Filter to get fractional pitch. tmpW32 = 0; for (m = 0; m < PITCH_FRACORDER; m++) { tmpW32 += WEBRTC_SPL_MUL_16_16(ubufQQ[pos3QQ + m], fracoeffQQ[m]); } // Subtract from input and update buffer. ubufQQ[pos] = indatQ0[ind]; tmp2W32 = WEBRTC_SPL_MUL_16_32_RSFT14(indatQ0[ind], tmpW32); tmpW32 += 8192; tmpW16 = (int16_t)WEBRTC_SPL_RSHIFT_W32(tmpW32, 14); tmpW32 = WEBRTC_SPL_MUL_16_16(tmpW16, tmpW16); if ((tmp2W32 > 1073700000) || (csum1QQ > 1073700000) || (tmpW32 > 1073700000) || (esumxQQ > 1073700000)) { // 2^30 scale++; csum1QQ = WEBRTC_SPL_RSHIFT_W32(csum1QQ, 1); esumxQQ = WEBRTC_SPL_RSHIFT_W32(esumxQQ, 1); } tmp2W32 = WEBRTC_SPL_RSHIFT_W32(tmp2W32, scale); csum1QQ += tmp2W32; tmpW32 = WEBRTC_SPL_RSHIFT_W32(tmpW32, scale); esumxQQ += tmpW32; ind++; pos++; pos3QQ++; } } if (csum1QQ < esumxQQ) { tmp2W32 = WebRtcSpl_DivResultInQ31(csum1QQ, esumxQQ); // Gain should be half the correlation. tmpW32 = WEBRTC_SPL_RSHIFT_W32(tmp2W32, 20); } else { tmpW32 = 4096; } gainsQ12[k] = (int16_t)WEBRTC_SPL_SAT(PITCH_MAX_GAIN_Q12, tmpW32, 0); } // Export buffer and states. memcpy(pfp->ubufQQ, ubufQQ + PITCH_FRAME_LEN, sizeof(pfp->ubufQQ)); pfp->oldlagQ7 = lagsQ7[PITCH_SUBFRAMES - 1]; pfp->oldgainQ12 = gainsQ12[PITCH_SUBFRAMES - 1]; }