void WebRtcIlbcfix_Refiner( WebRtc_Word16 *updStartPos, /* (o) updated start point (Q-2) */ WebRtc_Word16 *idata, /* (i) original data buffer */ WebRtc_Word16 idatal, /* (i) dimension of idata */ WebRtc_Word16 centerStartPos, /* (i) beginning center segment */ WebRtc_Word16 estSegPos, /* (i) estimated beginning other segment (Q-2) */ WebRtc_Word16 *surround, /* (i/o) The contribution from this sequence summed with earlier contributions */ WebRtc_Word16 gain /* (i) Gain to use for this sequence */ ){ WebRtc_Word16 estSegPosRounded,searchSegStartPos,searchSegEndPos,corrdim; WebRtc_Word16 tloc,tloc2,i,st,en,fraction; WebRtc_Word32 maxtemp, scalefact; WebRtc_Word16 *filtStatePtr, *polyPtr; /* Stack based */ WebRtc_Word16 filt[7]; WebRtc_Word32 corrVecUps[ENH_CORRDIM*ENH_UPS0]; WebRtc_Word32 corrVecTemp[ENH_CORRDIM]; WebRtc_Word16 vect[ENH_VECTL]; WebRtc_Word16 corrVec[ENH_CORRDIM]; /* defining array bounds */ estSegPosRounded=WEBRTC_SPL_RSHIFT_W16((estSegPos - 2),2); searchSegStartPos=estSegPosRounded-ENH_SLOP; if (searchSegStartPos<0) { searchSegStartPos=0; } searchSegEndPos=estSegPosRounded+ENH_SLOP; if(searchSegEndPos+ENH_BLOCKL >= idatal) { searchSegEndPos=idatal-ENH_BLOCKL-1; } corrdim=searchSegEndPos-searchSegStartPos+1; /* compute upsampled correlation and find location of max */ WebRtcIlbcfix_MyCorr(corrVecTemp,idata+searchSegStartPos, (WebRtc_Word16)(corrdim+ENH_BLOCKL-1),idata+centerStartPos,ENH_BLOCKL); /* Calculate the rescaling factor for the correlation in order to put the correlation in a WebRtc_Word16 vector instead */ maxtemp=WebRtcSpl_MaxAbsValueW32(corrVecTemp, (WebRtc_Word16)corrdim); scalefact=WebRtcSpl_GetSizeInBits(maxtemp)-15; if (scalefact>0) { for (i=0;i<corrdim;i++) { corrVec[i]=(WebRtc_Word16)WEBRTC_SPL_RSHIFT_W32(corrVecTemp[i], scalefact); } } else { for (i=0;i<corrdim;i++) { corrVec[i]=(WebRtc_Word16)corrVecTemp[i]; } } /* In order to guarantee that all values are initialized */ for (i=corrdim;i<ENH_CORRDIM;i++) { corrVec[i]=0; } /* Upsample the correlation */ WebRtcIlbcfix_EnhUpsample(corrVecUps,corrVec); /* Find maximum */ tloc=WebRtcSpl_MaxIndexW32(corrVecUps, (WebRtc_Word16) (ENH_UPS0*corrdim)); /* make vector can be upsampled without ever running outside bounds */ *updStartPos = (WebRtc_Word16)WEBRTC_SPL_MUL_16_16(searchSegStartPos,4) + tloc + 4; tloc2 = WEBRTC_SPL_RSHIFT_W16((tloc+3), 2); st=searchSegStartPos+tloc2-ENH_FL0; /* initialize the vector to be filtered, stuff with zeros when data is outside idata buffer */ if(st<0){ WebRtcSpl_MemSetW16(vect, 0, (WebRtc_Word16)(-st)); WEBRTC_SPL_MEMCPY_W16(&vect[-st], idata, (ENH_VECTL+st)); } else{ en=st+ENH_VECTL; if(en>idatal){ WEBRTC_SPL_MEMCPY_W16(vect, &idata[st], (ENH_VECTL-(en-idatal))); WebRtcSpl_MemSetW16(&vect[ENH_VECTL-(en-idatal)], 0, (WebRtc_Word16)(en-idatal)); } else { WEBRTC_SPL_MEMCPY_W16(vect, &idata[st], ENH_VECTL); } } /* Calculate which of the 4 fractions to use */ fraction=(WebRtc_Word16)WEBRTC_SPL_MUL_16_16(tloc2,ENH_UPS0)-tloc; /* compute the segment (this is actually a convolution) */ filtStatePtr = filt + 6; polyPtr = (WebRtc_Word16*)WebRtcIlbcfix_kEnhPolyPhaser[fraction]; for (i=0;i<7;i++) { *filtStatePtr-- = *polyPtr++; } WebRtcSpl_FilterMAFastQ12( &vect[6], vect, filt, ENH_FLO_MULT2_PLUS1, ENH_BLOCKL); /* Add the contribution from this vector (scaled with gain) to the total surround vector */ WebRtcSpl_AddAffineVectorToVector( surround, vect, gain, (WebRtc_Word32)32768, 16, ENH_BLOCKL); return; }
void WebRtcIlbcfix_CbSearchCore( int32_t *cDot, /* (i) Cross Correlation */ int16_t range, /* (i) Search range */ int16_t stage, /* (i) Stage of this search */ int16_t *inverseEnergy, /* (i) Inversed energy */ int16_t *inverseEnergyShift, /* (i) Shifts of inversed energy with the offset 2*16-29 */ int32_t *Crit, /* (o) The criteria */ int16_t *bestIndex, /* (o) Index that corresponds to maximum criteria (in this vector) */ int32_t *bestCrit, /* (o) Value of critera for the chosen index */ int16_t *bestCritSh) /* (o) The domain of the chosen criteria */ { int32_t maxW32, tmp32; int16_t max, sh, tmp16; int i; int32_t *cDotPtr; int16_t cDotSqW16; int16_t *inverseEnergyPtr; int32_t *critPtr; int16_t *inverseEnergyShiftPtr; /* Don't allow negative values for stage 0 */ if (stage==0) { cDotPtr=cDot; for (i=0;i<range;i++) { *cDotPtr=WEBRTC_SPL_MAX(0, (*cDotPtr)); cDotPtr++; } } /* Normalize cDot to int16_t, calculate the square of cDot and store the upper int16_t */ maxW32 = WebRtcSpl_MaxAbsValueW32(cDot, range); sh = (int16_t)WebRtcSpl_NormW32(maxW32); cDotPtr = cDot; inverseEnergyPtr = inverseEnergy; critPtr = Crit; inverseEnergyShiftPtr=inverseEnergyShift; max=WEBRTC_SPL_WORD16_MIN; for (i=0;i<range;i++) { /* Calculate cDot*cDot and put the result in a int16_t */ tmp32 = WEBRTC_SPL_LSHIFT_W32(*cDotPtr,sh); tmp16 = (int16_t)WEBRTC_SPL_RSHIFT_W32(tmp32,16); cDotSqW16 = (int16_t)(((int32_t)(tmp16)*(tmp16))>>16); /* Calculate the criteria (cDot*cDot/energy) */ *critPtr=WEBRTC_SPL_MUL_16_16(cDotSqW16, (*inverseEnergyPtr)); /* Extract the maximum shift value under the constraint that the criteria is not zero */ if ((*critPtr)!=0) { max = WEBRTC_SPL_MAX((*inverseEnergyShiftPtr), max); } inverseEnergyPtr++; inverseEnergyShiftPtr++; critPtr++; cDotPtr++; } /* If no max shifts still at initialization value, set shift to zero */ if (max==WEBRTC_SPL_WORD16_MIN) { max = 0; } /* Modify the criterias, so that all of them use the same Q domain */ critPtr=Crit; inverseEnergyShiftPtr=inverseEnergyShift; for (i=0;i<range;i++) { /* Guarantee that the shift value is less than 16 in order to simplify for DSP's (and guard against >31) */ tmp16 = WEBRTC_SPL_MIN(16, max-(*inverseEnergyShiftPtr)); (*critPtr)=WEBRTC_SPL_SHIFT_W32((*critPtr),-tmp16); critPtr++; inverseEnergyShiftPtr++; } /* Find the index of the best value */ *bestIndex = WebRtcSpl_MaxIndexW32(Crit, range); *bestCrit = Crit[*bestIndex]; /* Calculate total shifts of this criteria */ *bestCritSh = 32 - 2*sh + max; return; }