/* Copyright (C) 2002 Jean-Marc Valin File: ltp.c Long-Term Prediction functions Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. - Neither the name of the Xiph.org Foundation nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include #include "ltp.h" #include "stack_alloc.h" #include "filters.h" #include "speex_bits.h" #ifdef _USE_SSE #include "ltp_sse.h" #else static float inner_prod(float *x, float *y, int len) { int i; float sum1=0,sum2=0,sum3=0,sum4=0; for (i=0;ibest_score[N-1]) { float g1, g; g1 = corr[i-start]/(energy[i-start]+10); g = sqrt(g1*corr[i-start]/(e0+10)); if (g>g1) g=g1; if (g<0) g=0; for (j=0;j best_score[j]) { for (k=N-1;k>j;k--) { best_score[k]=best_score[k-1]; pitch[k]=pitch[k-1]; gain[k] = gain[k-1]; } best_score[j]=score[i-start]; pitch[j]=i; gain[j]=g; break; } } } } } /** Finds the best quantized 3-tap pitch predictor by analysis by synthesis */ float pitch_gain_search_3tap( float target[], /* Target vector */ float ak[], /* LPCs for this subframe */ float awk1[], /* Weighted LPCs #1 for this subframe */ float awk2[], /* Weighted LPCs #2 for this subframe */ float exc[], /* Excitation */ void *par, int pitch, /* Pitch value */ int p, /* Number of LPC coeffs */ int nsf, /* Number of samples in subframe */ SpeexBits *bits, char *stack, float *exc2, float *r, int *cdbk_index ) { int i,j; float *tmp, *tmp2; float *x[3]; float *e[3]; float corr[3]; float A[3][3]; float gain[3]; int gain_cdbk_size; signed char *gain_cdbk; float err1,err2; ltp_params *params; params = (ltp_params*) par; gain_cdbk=params->gain_cdbk; gain_cdbk_size=1<gain_bits; tmp = PUSH(stack, 3*nsf, float); tmp2 = PUSH(stack, 3*nsf, float); x[0]=tmp; x[1]=tmp+nsf; x[2]=tmp+2*nsf; e[0]=tmp2; e[1]=tmp2+nsf; e[2]=tmp2+2*nsf; for (i=2;i>=0;i--) { int pp=pitch+1-i; for (j=0;j0) tot+=ptr[0]; if (ptr[2]>0) tot+=ptr[2]; if (tot>1) continue; } if (sum>best_sum || i==0) { best_sum=sum; best_cdbk=i; } } gain[0] = 0.015625*gain_cdbk[best_cdbk*3] + .5; gain[1] = 0.015625*gain_cdbk[best_cdbk*3+1]+ .5; gain[2] = 0.015625*gain_cdbk[best_cdbk*3+2]+ .5; *cdbk_index=best_cdbk; } for (i=0;i10) N=10; nbest=PUSH(stack, N, int); gains = PUSH(stack, N, float); params = (ltp_params*) par; if (N==0 || endpitch_bits); speex_bits_pack(bits, 0, params->gain_bits); for (i=0;iend-start+1) N=end-start+1; open_loop_nbest_pitch(sw, start, end, nsf, nbest, gains, N, stack); for (i=0;ipitch_bits); speex_bits_pack(bits, best_gain_index, params->gain_bits); /*printf ("encode pitch: %d %d\n", best_pitch, best_gain_index);*/ for (i=0;igain_cdbk; pitch = speex_bits_unpack_unsigned(bits, params->pitch_bits); pitch += start; gain_index = speex_bits_unpack_unsigned(bits, params->gain_bits); /*printf ("decode pitch: %d %d\n", pitch, gain_index);*/ gain[0] = 0.015625*gain_cdbk[gain_index*3]+.5; gain[1] = 0.015625*gain_cdbk[gain_index*3+1]+.5; gain[2] = 0.015625*gain_cdbk[gain_index*3+2]+.5; if (count_lost && pitch > subframe_offset) { float gain_sum; if (1) { float tmp = count_lost < 4 ? last_pitch_gain : 0.4 * last_pitch_gain; if (tmp>.95) tmp=.95; gain_sum = fabs(gain[1]); if (gain[0]>0) gain_sum += gain[0]; else gain_sum -= .5*gain[0]; if (gain[2]>0) gain_sum += gain[2]; else gain_sum -= .5*gain[2]; if (gain_sum > tmp) { float fact = tmp/gain_sum; for (i=0;i<3;i++) gain[i]*=fact; } } if (0) { gain_sum = fabs(gain[0])+fabs(gain[1])+fabs(gain[2]); if (gain_sum>.95) { float fact = .95/gain_sum; for (i=0;i<3;i++) gain[i]*=fact; } } } *pitch_val = pitch; /**gain_val = gain[0]+gain[1]+gain[2];*/ gain_val[0]=gain[0]; gain_val[1]=gain[1]; gain_val[2]=gain[2]; { float *e[3]; float *tmp2; tmp2=PUSH(stack, 3*nsf, float); e[0]=tmp2; e[1]=tmp2+nsf; e[2]=tmp2+2*nsf; for (i=0;i<3;i++) { int j; int pp=pitch+1-i; #if 0 for (j=0;jpp) tmp1=pp; for (j=0;jpp+pitch) tmp3=pp+pitch; for (j=tmp1;j.99) pitch_coef=.99; for (i=0;i.99) pitch_coef=.99; for (i=0;i