/* Copyright (C) 2002-2006 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. */ #ifdef HAVE_CONFIG_H #include "config-speex.h" #endif #ifndef FIXED_POINT #include #endif #include "ltp.h" #include "stack_alloc.h" #include "filters.h" #include "speex/speex_bits.h" #include "math_approx.h" #include "os_support.h" #ifndef NULL #define NULL 0 #endif #ifdef _USE_SSE #include "ltp_sse.h" #elif defined (ARM4_ASM) || defined(ARM5E_ASM) #include "ltp_arm4.h" #elif defined (COLDFIRE_ASM) #define OVERRIDE_INNER_PROD #elif defined (BFIN_ASM) #include "ltp_bfin.h" #endif #ifndef OVERRIDE_INNER_PROD spx_word32_t inner_prod(const spx_word16_t *x, const spx_word16_t *y, int len) { spx_word32_t sum=0; len >>= 2; while(len--) { spx_word32_t part=0; part = MAC16_16(part,*x++,*y++); part = MAC16_16(part,*x++,*y++); part = MAC16_16(part,*x++,*y++); part = MAC16_16(part,*x++,*y++); /* HINT: If you had a 40-bit accumulator, you could shift only at the end */ sum = ADD32(sum,SHR32(part,6)); } return sum; } #endif #ifndef SPEEX_DISABLE_ENCODER #ifndef OVERRIDE_PITCH_XCORR #if 0 /* HINT: Enable this for machines with enough registers (i.e. not x86) */ void pitch_xcorr(const spx_word16_t *_x, const spx_word16_t *_y, spx_word32_t *corr, int len, int nb_pitch, char *stack) { int i,j; for (i=0;i16383) { scaledown=1; break; } } /* If the weighted input is close to saturation, then we scale it down */ if (scaledown) { for (i=-end;iMULT16_16(best_score[N-1],ADD16(1,ener16[i-start]))) { /* We can safely put it last and then check */ best_score[N-1]=tmp; best_ener[N-1]=ener16[i-start]+1; pitch[N-1]=i; /* Check if it comes in front of others */ for (j=0;jMULT16_16(best_score[j],ADD16(1,ener16[i-start]))) { for (k=N-1;k>j;k--) { best_score[k]=best_score[k-1]; best_ener[k]=best_ener[k-1]; pitch[k]=pitch[k-1]; } best_score[j]=tmp; best_ener[j]=ener16[i-start]+1; pitch[j]=i; break; } } } } /* Compute open-loop gain if necessary */ if (gain) { for (j=0;jbest_sum && gain_sum<=max_gain) { best_sum=sum; best_cdbk=i; } } return best_cdbk; } #endif /** Finds the best quantized 3-tap pitch predictor by analysis by synthesis */ static spx_word32_t pitch_gain_search_3tap( const spx_word16_t target[], /* Target vector */ const spx_coef_t ak[], /* LPCs for this subframe */ const spx_coef_t awk1[], /* Weighted LPCs #1 for this subframe */ const spx_coef_t awk2[], /* Weighted LPCs #2 for this subframe */ spx_sig_t exc[], /* Excitation */ const signed char *gain_cdbk, int gain_cdbk_size, int pitch, /* Pitch value */ int p, /* Number of LPC coeffs */ int nsf, /* Number of samples in subframe */ SpeexBits *bits, char *stack, const spx_word16_t *exc2, const spx_word16_t *r, spx_word16_t *new_target, int *cdbk_index, int plc_tuning, spx_word32_t cumul_gain, int scaledown ) { int i,j; VARDECL(spx_word16_t *tmp1); VARDECL(spx_word16_t *e); spx_word16_t *x[3]; spx_word32_t corr[3]; spx_word32_t A[3][3]; spx_word16_t gain[3]; spx_word32_t err; spx_word16_t max_gain=128; int best_cdbk=0; ALLOC(tmp1, 3*nsf, spx_word16_t); ALLOC(e, nsf, spx_word16_t); if (cumul_gain > 262144) max_gain = 31; x[0]=tmp1; x[1]=tmp1+nsf; x[2]=tmp1+2*nsf; for (j=0;j=0;i--) { spx_word16_t e0=exc2[-pitch-1+i]; #ifdef FIXED_POINT /* Scale excitation down if needed (avoiding overflow) */ if (scaledown) e0 = SHR16(e0,1); #endif x[i][0]=MULT16_16_Q14(r[0], e0); for (j=0;j30) plc_tuning=30; #ifdef FIXED_POINT C[0] = SHL32(C[0],1); C[1] = SHL32(C[1],1); C[2] = SHL32(C[2],1); C[3] = SHL32(C[3],1); C[4] = SHL32(C[4],1); C[5] = SHL32(C[5],1); C[6] = MAC16_32_Q15(C[6],MULT16_16_16(plc_tuning,655),C[6]); C[7] = MAC16_32_Q15(C[7],MULT16_16_16(plc_tuning,655),C[7]); C[8] = MAC16_32_Q15(C[8],MULT16_16_16(plc_tuning,655),C[8]); normalize16(C, C16, 32767, 9); #else C[6]*=.5*(1+.02*plc_tuning); C[7]*=.5*(1+.02*plc_tuning); C[8]*=.5*(1+.02*plc_tuning); #endif best_cdbk = pitch_gain_search_3tap_vq(gain_cdbk, gain_cdbk_size, C16, max_gain); #ifdef FIXED_POINT gain[0] = ADD16(32,(spx_word16_t)gain_cdbk[best_cdbk*4]); gain[1] = ADD16(32,(spx_word16_t)gain_cdbk[best_cdbk*4+1]); gain[2] = ADD16(32,(spx_word16_t)gain_cdbk[best_cdbk*4+2]); /*printf ("%d %d %d %d\n",gain[0],gain[1],gain[2], best_cdbk);*/ #else gain[0] = 0.015625*gain_cdbk[best_cdbk*4] + .5; gain[1] = 0.015625*gain_cdbk[best_cdbk*4+1]+ .5; gain[2] = 0.015625*gain_cdbk[best_cdbk*4+2]+ .5; #endif *cdbk_index=best_cdbk; } SPEEX_MEMSET(exc, 0, nsf); for (i=0;i<3;i++) { int j; int tmp1, tmp3; int pp=pitch+1-i; tmp1=nsf; if (tmp1>pp) tmp1=pp; for (j=0;jpp+pitch) tmp3=pp+pitch; for (j=tmp1;jgain_bits; gain_cdbk = params->gain_cdbk + 4*gain_cdbk_size*cdbk_offset; N=complexity; if (N>10) N=10; if (N<1) N=1; ALLOC(nbest, N, int); params = (const ltp_params*) par; if (endpitch_bits); speex_bits_pack(bits, 0, params->gain_bits); SPEEX_MEMSET(exc, 0, nsf); return start; } #ifdef FIXED_POINT /* Check if we need to scale everything down in the pitch search to avoid overflows */ for (i=0;i16383) { scaledown=1; break; } } for (i=-end;i16383) { scaledown=1; break; } } #endif if (N>end-start+1) N=end-start+1; if (end != start) open_loop_nbest_pitch(sw, start, end, nsf, nbest, NULL, N, stack); else nbest[0] = start; ALLOC(best_exc, nsf, spx_sig_t); ALLOC(new_target, nsf, spx_word16_t); ALLOC(best_target, nsf, spx_word16_t); for (i=0;ipitch_bits); speex_bits_pack(bits, best_gain_index, params->gain_bits); #ifdef FIXED_POINT *cumul_gain = MULT16_32_Q13(SHL16(params->gain_cdbk[4*best_gain_index+3],8), MAX32(1024,*cumul_gain)); #else *cumul_gain = 0.03125*MAX32(1024,*cumul_gain)*params->gain_cdbk[4*best_gain_index+3]; #endif /*printf ("%f\n", cumul_gain);*/ /*printf ("encode pitch: %d %d\n", best_pitch, best_gain_index);*/ SPEEX_COPY(exc, best_exc, nsf); SPEEX_COPY(target, best_target, nsf); #ifdef FIXED_POINT /* Scale target back up if needed */ if (scaledown) { for (i=0;igain_bits; gain_cdbk = params->gain_cdbk + 4*gain_cdbk_size*cdbk_offset; 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);*/ #ifdef FIXED_POINT gain[0] = ADD16(32,(spx_word16_t)gain_cdbk[gain_index*4]); gain[1] = ADD16(32,(spx_word16_t)gain_cdbk[gain_index*4+1]); gain[2] = ADD16(32,(spx_word16_t)gain_cdbk[gain_index*4+2]); #else gain[0] = 0.015625*gain_cdbk[gain_index*4]+.5; gain[1] = 0.015625*gain_cdbk[gain_index*4+1]+.5; gain[2] = 0.015625*gain_cdbk[gain_index*4+2]+.5; #endif if (count_lost && pitch > subframe_offset) { spx_word16_t gain_sum; if (1) { #ifdef FIXED_POINT spx_word16_t tmp = count_lost < 4 ? last_pitch_gain : SHR16(last_pitch_gain,1); if (tmp>62) tmp=62; #else spx_word16_t tmp = count_lost < 4 ? last_pitch_gain : 0.5 * last_pitch_gain; if (tmp>.95) tmp=.95; #endif gain_sum = gain_3tap_to_1tap(gain); if (gain_sum > tmp) { spx_word16_t fact = DIV32_16(SHL32(EXTEND32(tmp),14),gain_sum); for (i=0;i<3;i++) gain[i]=MULT16_16_Q14(fact,gain[i]); } } } *pitch_val = pitch; gain_val[0]=gain[0]; gain_val[1]=gain[1]; gain_val[2]=gain[2]; gain[0] = SHL16(gain[0],7); gain[1] = SHL16(gain[1],7); gain[2] = SHL16(gain[2],7); SPEEX_MEMSET(exc_out, 0, nsf); for (i=0;i<3;i++) { int j; int tmp1, tmp3; int pp=pitch+1-i; tmp1=nsf; if (tmp1>pp) tmp1=pp; for (j=0;jpp+pitch) tmp3=pp+pitch; for (j=tmp1;j63) pitch_coef=63; #else if (pitch_coef>.99) pitch_coef=.99; #endif for (i=0;i63) pitch_coef=63; #else if (pitch_coef>.99) pitch_coef=.99; #endif for (i=0;i