Ignore:
Timestamp:
Jul 26, 2006 5:04:54 PM (18 years ago)
Author:
bennylp
Message:
  • Bring speex codec up to date with their SVN trunk
  • Speex codec should work in FIXED_POINT mode when PJ_HAS_FLOATING_POINT is set to zero.
  • ulaw2linear will return zero if zero is given (this would make the VAD works better, and it also fixed click noise when call is established/hangup).
File:
1 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk/pjmedia/src/pjmedia-codec/speex/ltp.c

    r516 r628  
    5656 
    5757#ifndef OVERRIDE_INNER_PROD 
    58 static spx_word32_t inner_prod(const spx_word16_t *x, const spx_word16_t *y, int len) 
     58spx_word32_t inner_prod(const spx_word16_t *x, const spx_word16_t *y, int len) 
    5959{ 
    6060   spx_word32_t sum=0; 
     
    7676#ifndef OVERRIDE_PITCH_XCORR 
    7777#if 0 /* HINT: Enable this for machines with enough registers (i.e. not x86) */ 
    78 static void pitch_xcorr(const spx_word16_t *_x, const spx_word16_t *_y, spx_word32_t *corr, int len, int nb_pitch, char *stack) 
     78void pitch_xcorr(const spx_word16_t *_x, const spx_word16_t *_y, spx_word32_t *corr, int len, int nb_pitch, char *stack) 
    7979{ 
    8080   int i,j; 
     
    139139} 
    140140#else 
    141 static void pitch_xcorr(const spx_word16_t *_x, const spx_word16_t *_y, spx_word32_t *corr, int len, int nb_pitch, char *stack) 
     141void pitch_xcorr(const spx_word16_t *_x, const spx_word16_t *_y, spx_word32_t *corr, int len, int nb_pitch, char *stack) 
    142142{ 
    143143   int i; 
     
    153153 
    154154#ifndef OVERRIDE_COMPUTE_PITCH_ERROR 
    155 static inline spx_word32_t compute_pitch_error(spx_word32_t *C, spx_word16_t *g, spx_word16_t pitch_control) 
     155static inline spx_word32_t compute_pitch_error(spx_word16_t *C, spx_word16_t *g, spx_word16_t pitch_control) 
    156156{ 
    157157   spx_word32_t sum = 0; 
    158    sum = ADD32(sum,MULT16_32_Q15(MULT16_16_16(g[0],pitch_control),C[0])); 
    159    sum = ADD32(sum,MULT16_32_Q15(MULT16_16_16(g[1],pitch_control),C[1])); 
    160    sum = ADD32(sum,MULT16_32_Q15(MULT16_16_16(g[2],pitch_control),C[2])); 
    161    sum = SUB32(sum,MULT16_32_Q15(MULT16_16_16(g[0],g[1]),C[3])); 
    162    sum = SUB32(sum,MULT16_32_Q15(MULT16_16_16(g[2],g[1]),C[4])); 
    163    sum = SUB32(sum,MULT16_32_Q15(MULT16_16_16(g[2],g[0]),C[5])); 
    164    sum = SUB32(sum,MULT16_32_Q15(MULT16_16_16(g[0],g[0]),C[6])); 
    165    sum = SUB32(sum,MULT16_32_Q15(MULT16_16_16(g[1],g[1]),C[7])); 
    166    sum = SUB32(sum,MULT16_32_Q15(MULT16_16_16(g[2],g[2]),C[8])); 
     158   sum = ADD32(sum,MULT16_16(MULT16_16_16(g[0],pitch_control),C[0])); 
     159   sum = ADD32(sum,MULT16_16(MULT16_16_16(g[1],pitch_control),C[1])); 
     160   sum = ADD32(sum,MULT16_16(MULT16_16_16(g[2],pitch_control),C[2])); 
     161   sum = SUB32(sum,MULT16_16(MULT16_16_16(g[0],g[1]),C[3])); 
     162   sum = SUB32(sum,MULT16_16(MULT16_16_16(g[2],g[1]),C[4])); 
     163   sum = SUB32(sum,MULT16_16(MULT16_16_16(g[2],g[0]),C[5])); 
     164   sum = SUB32(sum,MULT16_16(MULT16_16_16(g[0],g[0]),C[6])); 
     165   sum = SUB32(sum,MULT16_16(MULT16_16_16(g[1],g[1]),C[7])); 
     166   sum = SUB32(sum,MULT16_16(MULT16_16_16(g[2],g[2]),C[8])); 
    167167   return sum; 
    168168} 
    169169#endif 
    170170 
    171 void open_loop_nbest_pitch(spx_sig_t *sw, int start, int end, int len, int *pitch, spx_word16_t *gain, int N, char *stack) 
     171#ifndef OVERRIDE_OPEN_LOOP_NBEST_PITCH 
     172void open_loop_nbest_pitch(spx_word16_t *sw, int start, int end, int len, int *pitch, spx_word16_t *gain, int N, char *stack) 
    172173{ 
    173174   int i,j,k; 
    174175   VARDECL(spx_word32_t *best_score); 
     176   VARDECL(spx_word32_t *best_ener); 
    175177   spx_word32_t e0; 
    176178   VARDECL(spx_word32_t *corr); 
    177179   VARDECL(spx_word32_t *energy); 
    178    VARDECL(spx_word32_t *score); 
    179    VARDECL(spx_word16_t *swn2); 
    180    spx_word16_t *swn; 
    181180 
    182181   ALLOC(best_score, N, spx_word32_t); 
     182   ALLOC(best_ener, N, spx_word32_t); 
    183183   ALLOC(corr, end-start+1, spx_word32_t); 
    184184   ALLOC(energy, end-start+2, spx_word32_t); 
    185    ALLOC(score, end-start+1, spx_word32_t); 
    186  
    187 #ifdef FIXED_POINT 
    188    ALLOC(swn2, end+len, spx_word16_t); 
    189    normalize16(sw-end, swn2, 16384, end+len); 
    190    swn = swn2 + end; 
    191 #else 
    192    swn = sw; 
    193 #endif 
    194185 
    195186   for (i=0;i<N;i++) 
    196187   { 
    197188        best_score[i]=-1; 
     189        best_ener[i]=0; 
    198190        pitch[i]=start; 
    199191   } 
    200192 
    201  
    202    energy[0]=inner_prod(swn-start, swn-start, len); 
    203    e0=inner_prod(swn, swn, len); 
    204    for (i=start;i<=end;i++) 
     193   energy[0]=inner_prod(sw-start, sw-start, len); 
     194   e0=inner_prod(sw, sw, len); 
     195   for (i=start;i<end;i++) 
    205196   { 
    206197      /* Update energy for next pitch*/ 
    207       energy[i-start+1] = SUB32(ADD32(energy[i-start],SHR32(MULT16_16(swn[-i-1],swn[-i-1]),6)), SHR32(MULT16_16(swn[-i+len-1],swn[-i+len-1]),6)); 
     198      energy[i-start+1] = SUB32(ADD32(energy[i-start],SHR32(MULT16_16(sw[-i-1],sw[-i-1]),6)), SHR32(MULT16_16(sw[-i+len-1],sw[-i+len-1]),6)); 
    208199      if (energy[i-start+1] < 0) 
    209200         energy[i-start+1] = 0; 
    210201   } 
    211202 
    212    pitch_xcorr(swn, swn-end, corr, len, end-start+1, stack); 
    213  
     203   pitch_xcorr(sw, sw-end, corr, len, end-start+1, stack); 
     204 
     205   /* FIXME: Fixed-point and floating-point code should be merged */ 
    214206#ifdef FIXED_POINT 
    215207   { 
     
    218210      ALLOC(corr16, end-start+1, spx_word16_t); 
    219211      ALLOC(ener16, end-start+1, spx_word16_t); 
    220       normalize16(corr, corr16, 16384, end-start+1); 
    221       normalize16(energy, ener16, 16384, end-start+1); 
     212      /* Normalize to 180 so we can square it and it still fits in 16 bits */ 
     213      normalize16(corr, corr16, 180, end-start+1); 
     214      normalize16(energy, ener16, 180, end-start+1); 
    222215 
    223216      for (i=start;i<=end;i++) 
    224217      { 
    225          spx_word16_t g; 
    226          spx_word32_t tmp; 
    227          tmp = corr16[i-start]; 
    228          if (tmp>0) 
     218         spx_word16_t tmp = MULT16_16_16(corr16[i-start],corr16[i-start]); 
     219         /* Instead of dividing the tmp by the energy, we multiply on the other side */ 
     220         if (MULT16_16(tmp,best_ener[N-1])>MULT16_16(best_score[N-1],ADD16(1,ener16[i-start]))) 
    229221         { 
    230             if (SHR16(corr16[i-start],4)>ener16[i-start]) 
    231                tmp = SHL32(EXTEND32(ener16[i-start]),14); 
    232             else if (-SHR16(corr16[i-start],4)>ener16[i-start]) 
    233                tmp = -SHL32(EXTEND32(ener16[i-start]),14); 
    234             else 
    235                tmp = SHL32(tmp,10); 
    236             g = DIV32_16(tmp, 8+ener16[i-start]); 
    237             score[i-start] = MULT16_16(corr16[i-start],g); 
    238          } else 
    239          { 
    240             score[i-start] = 1; 
     222            /* We can safely put it last and then check */ 
     223            best_score[N-1]=tmp; 
     224            best_ener[N-1]=ener16[i-start]+1; 
     225            pitch[N-1]=i; 
     226            /* Check if it comes in front of others */ 
     227            for (j=0;j<N-1;j++) 
     228            { 
     229               if (MULT16_16(tmp,best_ener[j])>MULT16_16(best_score[j],ADD16(1,ener16[i-start]))) 
     230               { 
     231                  for (k=N-1;k>j;k--) 
     232                  { 
     233                     best_score[k]=best_score[k-1]; 
     234                     best_ener[k]=best_ener[k-1]; 
     235                     pitch[k]=pitch[k-1]; 
     236                  } 
     237                  best_score[j]=tmp; 
     238                  best_ener[j]=ener16[i-start]+1; 
     239                  pitch[j]=i; 
     240                  break; 
     241               } 
     242            } 
    241243         } 
    242244      } 
     
    245247   for (i=start;i<=end;i++) 
    246248   { 
    247       float g = corr[i-start]/(1+energy[i-start]); 
    248       if (g>16) 
    249          g = 16; 
    250       else if (g<-16) 
    251          g = -16; 
    252       score[i-start] = g*corr[i-start]; 
    253    } 
    254 #endif 
    255  
    256    /* Extract best scores */ 
    257    for (i=start;i<=end;i++) 
    258    { 
    259       if (score[i-start]>best_score[N-1]) 
     249      float tmp = corr[i-start]*corr[i-start]; 
     250      if (tmp*best_ener[N-1]>best_score[N-1]*(1+energy[i-start])) 
    260251      { 
    261252         for (j=0;j<N;j++) 
    262253         { 
    263             if (score[i-start] > best_score[j]) 
     254            if (tmp*best_ener[j]>best_score[j]*(1+energy[i-start])) 
    264255            { 
    265256               for (k=N-1;k>j;k--) 
    266257               { 
    267258                  best_score[k]=best_score[k-1]; 
     259                  best_ener[k]=best_ener[k-1]; 
    268260                  pitch[k]=pitch[k-1]; 
    269261               } 
    270                best_score[j]=score[i-start]; 
     262               best_score[j]=tmp; 
     263               best_ener[j]=energy[i-start]+1; 
    271264               pitch[j]=i; 
    272265               break; 
     
    275268      } 
    276269   } 
     270#endif 
    277271 
    278272   /* Compute open-loop gain */ 
     
    291285   } 
    292286} 
    293  
     287#endif 
     288 
     289#ifndef OVERRIDE_PITCH_GAIN_SEARCH_3TAP_VQ 
     290static int pitch_gain_search_3tap_vq( 
     291  const signed char *gain_cdbk, 
     292  int                gain_cdbk_size, 
     293  spx_word16_t      *C16, 
     294  spx_word16_t       max_gain 
     295) 
     296{ 
     297  const signed char *ptr=gain_cdbk; 
     298  int                best_cdbk=0; 
     299  spx_word32_t       best_sum=-VERY_LARGE32; 
     300  spx_word32_t       sum=0; 
     301  spx_word16_t       g[3]; 
     302  spx_word16_t       pitch_control=64; 
     303  spx_word16_t       gain_sum; 
     304  int                i; 
     305 
     306  for (i=0;i<gain_cdbk_size;i++) { 
     307          
     308    ptr = gain_cdbk+4*i; 
     309    g[0]=ADD16((spx_word16_t)ptr[0],32); 
     310    g[1]=ADD16((spx_word16_t)ptr[1],32); 
     311    g[2]=ADD16((spx_word16_t)ptr[2],32); 
     312    gain_sum = (spx_word16_t)ptr[3]; 
     313          
     314    sum = compute_pitch_error(C16, g, pitch_control); 
     315          
     316    if (sum>best_sum && gain_sum<=max_gain) { 
     317      best_sum=sum; 
     318      best_cdbk=i; 
     319    } 
     320  } 
     321 
     322  return best_cdbk; 
     323} 
     324#endif 
    294325 
    295326/** Finds the best quantized 3-tap pitch predictor by analysis by synthesis */ 
    296 static spx_word64_t pitch_gain_search_3tap( 
    297 const spx_sig_t target[],       /* Target vector */ 
     327static spx_word32_t pitch_gain_search_3tap( 
     328const spx_word16_t target[],       /* Target vector */ 
    298329const spx_coef_t ak[],          /* LPCs for this subframe */ 
    299330const spx_coef_t awk1[],        /* Weighted LPCs #1 for this subframe */ 
    300331const spx_coef_t awk2[],        /* Weighted LPCs #2 for this subframe */ 
    301332spx_sig_t exc[],                /* Excitation */ 
    302 const void *par, 
     333const signed char *gain_cdbk, 
     334int gain_cdbk_size, 
    303335int   pitch,                    /* Pitch value */ 
    304336int   p,                        /* Number of LPC coeffs */ 
     
    306338SpeexBits *bits, 
    307339char *stack, 
    308 const spx_sig_t *exc2, 
     340const spx_word16_t *exc2, 
    309341const spx_word16_t *r, 
    310 spx_sig_t *new_target, 
     342spx_word16_t *new_target, 
    311343int  *cdbk_index, 
    312 int cdbk_offset, 
    313 int plc_tuning 
     344int plc_tuning, 
     345spx_word32_t cumul_gain 
    314346) 
    315347{ 
    316348   int i,j; 
    317    VARDECL(spx_sig_t *tmp1); 
    318    VARDECL(spx_sig_t *tmp2); 
    319    spx_sig_t *x[3]; 
    320    spx_sig_t *e[3]; 
     349   VARDECL(spx_word16_t *tmp1); 
     350   VARDECL(spx_word16_t *e); 
     351   spx_word16_t *x[3]; 
    321352   spx_word32_t corr[3]; 
    322353   spx_word32_t A[3][3]; 
    323    int   gain_cdbk_size; 
    324    const signed char *gain_cdbk; 
    325354   spx_word16_t gain[3]; 
    326    spx_word64_t err; 
    327  
    328    const ltp_params *params; 
    329    params = (const ltp_params*) par; 
    330    gain_cdbk_size = 1<<params->gain_bits; 
    331    gain_cdbk = params->gain_cdbk + 3*gain_cdbk_size*cdbk_offset; 
    332    ALLOC(tmp1, 3*nsf, spx_sig_t); 
    333    ALLOC(tmp2, 3*nsf, spx_sig_t); 
    334  
     355   spx_word32_t err; 
     356   spx_word16_t max_gain=128; 
     357   int          best_cdbk=0; 
     358 
     359   ALLOC(tmp1, 3*nsf, spx_word16_t); 
     360   ALLOC(e, nsf, spx_word16_t); 
     361 
     362   if (cumul_gain > 262144) 
     363      max_gain = 31; 
     364    
    335365   x[0]=tmp1; 
    336366   x[1]=tmp1+nsf; 
    337367   x[2]=tmp1+2*nsf; 
    338368    
    339    e[0]=tmp2; 
    340    e[1]=tmp2+nsf; 
    341    e[2]=tmp2+2*nsf; 
    342    for (i=2;i>=0;i--) 
    343    { 
    344       int pp=pitch+1-i; 
     369   { 
     370      VARDECL(spx_mem_t *mm); 
     371      int pp=pitch-1; 
     372      ALLOC(mm, p, spx_mem_t); 
    345373      for (j=0;j<nsf;j++) 
    346374      { 
    347375         if (j-pp<0) 
    348             e[i][j]=exc2[j-pp]; 
     376            e[j]=exc2[j-pp]; 
    349377         else if (j-pp-pitch<0) 
    350             e[i][j]=exc2[j-pp-pitch]; 
     378            e[j]=exc2[j-pp-pitch]; 
    351379         else 
    352             e[i][j]=0; 
     380            e[j]=0; 
    353381      } 
    354  
    355       if (i==2) 
    356          syn_percep_zero(e[i], ak, awk1, awk2, x[i], nsf, p, stack); 
    357       else { 
    358          for (j=0;j<nsf-1;j++) 
    359             x[i][j+1]=x[i+1][j]; 
    360          x[i][0]=0; 
    361          for (j=0;j<nsf;j++) 
    362          { 
    363             x[i][j]=ADD32(x[i][j],SHL32(MULT16_32_Q15(r[j], e[i][0]),1)); 
    364          } 
    365       } 
    366    } 
    367  
    368 #ifdef FIXED_POINT 
    369    { 
    370       /* If using fixed-point, we need to normalize the signals first */ 
    371       spx_word16_t *y[3]; 
    372       VARDECL(spx_word16_t *ytmp); 
    373       VARDECL(spx_word16_t *t); 
    374  
    375       spx_sig_t max_val=1; 
    376       int sig_shift; 
    377        
    378       ALLOC(ytmp, 3*nsf, spx_word16_t); 
    379 #if 0 
    380       ALLOC(y[0], nsf, spx_word16_t); 
    381       ALLOC(y[1], nsf, spx_word16_t); 
    382       ALLOC(y[2], nsf, spx_word16_t); 
    383 #else 
    384       y[0] = ytmp; 
    385       y[1] = ytmp+nsf; 
    386       y[2] = ytmp+2*nsf; 
    387 #endif 
    388       ALLOC(t, nsf, spx_word16_t); 
    389       for (j=0;j<3;j++) 
    390       { 
    391          for (i=0;i<nsf;i++) 
    392          { 
    393             spx_sig_t tmp = x[j][i]; 
    394             if (tmp<0) 
    395                tmp = -tmp; 
    396             if (tmp > max_val) 
    397                max_val = tmp; 
    398          } 
    399       } 
    400       for (i=0;i<nsf;i++) 
    401       { 
    402          spx_sig_t tmp = target[i]; 
    403          if (tmp<0) 
    404             tmp = -tmp; 
    405          if (tmp > max_val) 
    406             max_val = tmp; 
    407       } 
    408  
    409       sig_shift=0; 
    410       while (max_val>16384) 
    411       { 
    412          sig_shift++; 
    413          max_val >>= 1; 
    414       } 
    415  
    416       for (j=0;j<3;j++) 
    417       { 
    418          for (i=0;i<nsf;i++) 
    419          { 
    420             y[j][i] = EXTRACT16(SHR32(x[j][i],sig_shift)); 
    421          } 
    422       } 
    423       for (i=0;i<nsf;i++) 
    424       { 
    425          t[i] = EXTRACT16(SHR32(target[i],sig_shift)); 
    426       } 
    427  
    428       for (i=0;i<3;i++) 
    429          corr[i]=inner_prod(y[i],t,nsf); 
    430        
    431       for (i=0;i<3;i++) 
    432          for (j=0;j<=i;j++) 
    433             A[i][j]=A[j][i]=inner_prod(y[i],y[j],nsf); 
    434    } 
    435 #else 
    436    { 
    437       for (i=0;i<3;i++) 
    438          corr[i]=inner_prod(x[i],target,nsf); 
    439        
    440       for (i=0;i<3;i++) 
    441          for (j=0;j<=i;j++) 
    442             A[i][j]=A[j][i]=inner_prod(x[i],x[j],nsf); 
    443    } 
    444 #endif 
     382      for (j=0;j<p;j++) 
     383         mm[j] = 0; 
     384      iir_mem16(e, ak, e, nsf, p, mm, stack); 
     385      for (j=0;j<p;j++) 
     386         mm[j] = 0; 
     387      filter_mem16(e, awk1, awk2, e, nsf, p, mm, stack); 
     388      for (j=0;j<nsf;j++) 
     389         x[2][j] = e[j]; 
     390   } 
     391   for (i=1;i>=0;i--) 
     392   { 
     393      spx_word16_t e0=exc2[-pitch-1+i]; 
     394      x[i][0]=MULT16_16_Q14(r[0], e0); 
     395      for (j=0;j<nsf-1;j++) 
     396         x[i][j+1]=ADD32(x[i+1][j],MULT16_16_P14(r[j+1], e0)); 
     397   } 
     398 
     399   for (i=0;i<3;i++) 
     400      corr[i]=inner_prod(x[i],target,nsf); 
     401   for (i=0;i<3;i++) 
     402      for (j=0;j<=i;j++) 
     403         A[i][j]=A[j][i]=inner_prod(x[i],x[j],nsf); 
    445404 
    446405   { 
    447406      spx_word32_t C[9]; 
    448       const signed char *ptr=gain_cdbk; 
    449       int best_cdbk=0; 
    450       spx_word32_t best_sum=0; 
     407#ifdef FIXED_POINT 
     408      spx_word16_t C16[9]; 
     409#else 
     410      spx_word16_t *C16=C; 
     411#endif       
    451412      C[0]=corr[2]; 
    452413      C[1]=corr[1]; 
     
    462423      if (plc_tuning<2) 
    463424         plc_tuning=2; 
    464 #ifdef FIXED_POINT 
    465       C[0] = MAC16_32_Q15(C[0],MULT16_16_16(plc_tuning,-327),C[0]); 
    466       C[1] = MAC16_32_Q15(C[1],MULT16_16_16(plc_tuning,-327),C[1]); 
    467       C[2] = MAC16_32_Q15(C[2],MULT16_16_16(plc_tuning,-327),C[2]); 
     425      if (plc_tuning>30) 
     426         plc_tuning=30; 
     427#ifdef FIXED_POINT 
    468428      C[0] = SHL32(C[0],1); 
    469429      C[1] = SHL32(C[1],1); 
     
    472432      C[4] = SHL32(C[4],1); 
    473433      C[5] = SHL32(C[5],1); 
    474 #else 
    475       C[0]*=1-.01*plc_tuning; 
    476       C[1]*=1-.01*plc_tuning; 
    477       C[2]*=1-.01*plc_tuning; 
    478       C[6]*=.5*(1+.01*plc_tuning); 
    479       C[7]*=.5*(1+.01*plc_tuning); 
    480       C[8]*=.5*(1+.01*plc_tuning); 
    481 #endif 
    482       for (i=0;i<gain_cdbk_size;i++) 
    483       { 
    484          spx_word32_t sum=0; 
    485          spx_word16_t g[3]; 
    486          spx_word16_t pitch_control=64; 
    487          spx_word16_t gain_sum; 
    488           
    489          ptr = gain_cdbk+3*i; 
    490          g[0]=ADD16((spx_word16_t)ptr[0],32); 
    491          g[1]=ADD16((spx_word16_t)ptr[1],32); 
    492          g[2]=ADD16((spx_word16_t)ptr[2],32); 
    493  
    494          /* We favor "safe" pitch values to handle packet loss better */ 
    495          gain_sum = ADD16(ADD16(g[1],MAX16(g[0], 0)),MAX16(g[2], 0)); 
    496          if (gain_sum > 64) 
    497          { 
    498             gain_sum = SUB16(gain_sum, 64); 
    499             if (gain_sum > 127) 
    500                gain_sum = 127; 
    501 #ifdef FIXED_POINT 
    502             pitch_control =  SUB16(64,EXTRACT16(PSHR32(MULT16_16(64,MULT16_16_16(plc_tuning, gain_sum)),10))); 
    503 #else 
    504             pitch_control = 64*(1.-.001*plc_tuning*gain_sum); 
    505 #endif 
    506             if (pitch_control < 0) 
    507                pitch_control = 0; 
    508          } 
    509           
    510          sum = compute_pitch_error(C, g, pitch_control); 
    511           
    512          if (sum>best_sum || i==0) 
    513          { 
    514             best_sum=sum; 
    515             best_cdbk=i; 
    516          } 
    517       } 
    518 #ifdef FIXED_POINT 
    519       gain[0] = ADD16(32,(spx_word16_t)gain_cdbk[best_cdbk*3]); 
    520       gain[1] = ADD16(32,(spx_word16_t)gain_cdbk[best_cdbk*3+1]); 
    521       gain[2] = ADD16(32,(spx_word16_t)gain_cdbk[best_cdbk*3+2]); 
     434      C[6] = MAC16_32_Q15(C[6],MULT16_16_16(plc_tuning,655),C[6]); 
     435      C[7] = MAC16_32_Q15(C[7],MULT16_16_16(plc_tuning,655),C[7]); 
     436      C[8] = MAC16_32_Q15(C[8],MULT16_16_16(plc_tuning,655),C[8]); 
     437      normalize16(C, C16, 32767, 9); 
     438#else 
     439      C[6]*=.5*(1+.02*plc_tuning); 
     440      C[7]*=.5*(1+.02*plc_tuning); 
     441      C[8]*=.5*(1+.02*plc_tuning); 
     442#endif 
     443 
     444      best_cdbk = pitch_gain_search_3tap_vq(gain_cdbk, gain_cdbk_size, C16, max_gain); 
     445 
     446#ifdef FIXED_POINT 
     447      gain[0] = ADD16(32,(spx_word16_t)gain_cdbk[best_cdbk*4]); 
     448      gain[1] = ADD16(32,(spx_word16_t)gain_cdbk[best_cdbk*4+1]); 
     449      gain[2] = ADD16(32,(spx_word16_t)gain_cdbk[best_cdbk*4+2]); 
    522450      /*printf ("%d %d %d %d\n",gain[0],gain[1],gain[2], best_cdbk);*/ 
    523451#else 
    524       gain[0] = 0.015625*gain_cdbk[best_cdbk*3]  + .5; 
    525       gain[1] = 0.015625*gain_cdbk[best_cdbk*3+1]+ .5; 
    526       gain[2] = 0.015625*gain_cdbk[best_cdbk*3+2]+ .5; 
     452      gain[0] = 0.015625*gain_cdbk[best_cdbk*4]  + .5; 
     453      gain[1] = 0.015625*gain_cdbk[best_cdbk*4+1]+ .5; 
     454      gain[2] = 0.015625*gain_cdbk[best_cdbk*4+2]+ .5; 
    527455#endif 
    528456      *cdbk_index=best_cdbk; 
    529457   } 
    530458 
    531 #ifdef FIXED_POINT 
    532459   for (i=0;i<nsf;i++) 
    533      exc[i]=SHL32(ADD32(ADD32(MULT16_32_Q15(SHL16(gain[0],7),e[2][i]), MULT16_32_Q15(SHL16(gain[1],7),e[1][i])), 
    534                         MULT16_32_Q15(SHL16(gain[2],7),e[0][i])), 2); 
    535     
    536    err=0; 
     460      exc[i]=0; 
     461   for (i=0;i<3;i++) 
     462   { 
     463      int j; 
     464      int tmp1, tmp3; 
     465      int pp=pitch+1-i; 
     466      tmp1=nsf; 
     467      if (tmp1>pp) 
     468         tmp1=pp; 
     469      for (j=0;j<tmp1;j++) 
     470         exc[j]=MAC16_16(exc[j],SHL16(gain[2-i],7),exc2[j-pp]); 
     471      tmp3=nsf; 
     472      if (tmp3>pp+pitch) 
     473         tmp3=pp+pitch; 
     474      for (j=tmp1;j<tmp3;j++) 
     475         exc[j]=MAC16_16(exc[j],SHL16(gain[2-i],7),exc2[j-pp-pitch]); 
     476   } 
    537477   for (i=0;i<nsf;i++) 
    538478   { 
    539       spx_word16_t perr2; 
    540       spx_sig_t tmp = SHL32(ADD32(ADD32(MULT16_32_Q15(SHL16(gain[0],7),x[2][i]),MULT16_32_Q15(SHL16(gain[1],7),x[1][i])), 
    541                                   MULT16_32_Q15(SHL16(gain[2],7),x[0][i])),2); 
    542       spx_sig_t perr=SUB32(target[i],tmp); 
    543       new_target[i] = SUB32(target[i], tmp); 
    544       perr2 = EXTRACT16(PSHR32(perr,15)); 
    545       err = ADD64(err,MULT16_16(perr2,perr2)); 
    546        
    547    } 
    548 #else 
    549    for (i=0;i<nsf;i++) 
    550       exc[i]=gain[0]*e[2][i]+gain[1]*e[1][i]+gain[2]*e[0][i]; 
    551     
    552    err=0; 
    553    for (i=0;i<nsf;i++) 
    554    { 
    555       spx_sig_t tmp = gain[2]*x[0][i]+gain[1]*x[1][i]+gain[0]*x[2][i]; 
    556       new_target[i] = target[i] - tmp; 
    557       err+=new_target[i]*new_target[i]; 
    558    } 
    559 #endif 
     479      spx_word32_t tmp = ADD32(ADD32(MULT16_16(gain[0],x[2][i]),MULT16_16(gain[1],x[1][i])), 
     480                            MULT16_16(gain[2],x[0][i])); 
     481      new_target[i] = SUB16(target[i], EXTRACT16(PSHR32(tmp,6))); 
     482   } 
     483   err = inner_prod(new_target, new_target, nsf); 
    560484 
    561485   return err; 
    562486} 
    563  
    564487 
    565488/** Finds the best quantized 3-tap pitch predictor by analysis by synthesis */ 
    566489int pitch_search_3tap( 
    567 spx_sig_t target[],                 /* Target vector */ 
    568 spx_sig_t *sw, 
     490spx_word16_t target[],                 /* Target vector */ 
     491spx_word16_t *sw, 
    569492spx_coef_t ak[],                     /* LPCs for this subframe */ 
    570493spx_coef_t awk1[],                   /* Weighted LPCs #1 for this subframe */ 
     
    579502SpeexBits *bits, 
    580503char *stack, 
    581 spx_sig_t *exc2, 
     504spx_word16_t *exc2, 
    582505spx_word16_t *r, 
    583506int complexity, 
    584507int cdbk_offset, 
    585 int plc_tuning 
     508int plc_tuning, 
     509spx_word32_t *cumul_gain 
    586510) 
    587511{ 
     
    589513   int cdbk_index, pitch=0, best_gain_index=0; 
    590514   VARDECL(spx_sig_t *best_exc); 
    591    VARDECL(spx_sig_t *new_target); 
    592    VARDECL(spx_sig_t *best_target); 
     515   VARDECL(spx_word16_t *new_target); 
     516   VARDECL(spx_word16_t *best_target); 
    593517   int best_pitch=0; 
    594    spx_word64_t err, best_err=-1; 
     518   spx_word32_t err, best_err=-1; 
    595519   int N; 
    596520   const ltp_params *params; 
     521   const signed char *gain_cdbk; 
     522   int   gain_cdbk_size; 
     523    
    597524   VARDECL(int *nbest); 
    598  
     525    
     526   params = (const ltp_params*) par; 
     527   gain_cdbk_size = 1<<params->gain_bits; 
     528   gain_cdbk = params->gain_cdbk + 4*gain_cdbk_size*cdbk_offset; 
     529    
    599530   N=complexity; 
    600531   if (N>10) 
     
    614545      return start; 
    615546   } 
    616     
    617    ALLOC(best_exc, nsf, spx_sig_t); 
    618    ALLOC(new_target, nsf, spx_sig_t); 
    619    ALLOC(best_target, nsf, spx_sig_t); 
    620547    
    621548   if (N>end-start+1) 
     
    625552   else 
    626553      nbest[0] = start; 
     554    
     555   ALLOC(best_exc, nsf, spx_sig_t); 
     556   ALLOC(new_target, nsf, spx_word16_t); 
     557   ALLOC(best_target, nsf, spx_word16_t); 
     558    
    627559   for (i=0;i<N;i++) 
    628560   { 
     
    630562      for (j=0;j<nsf;j++) 
    631563         exc[j]=0; 
    632       err=pitch_gain_search_3tap(target, ak, awk1, awk2, exc, par, pitch, p, nsf, 
    633                                  bits, stack, exc2, r, new_target, &cdbk_index, cdbk_offset, plc_tuning); 
     564      err=pitch_gain_search_3tap(target, ak, awk1, awk2, exc, gain_cdbk, gain_cdbk_size, pitch, p, nsf, 
     565                                 bits, stack, exc2, r, new_target, &cdbk_index, plc_tuning, *cumul_gain); 
    634566      if (err<best_err || best_err<0) 
    635567      { 
     
    643575      } 
    644576   } 
    645     
    646577   /*printf ("pitch: %d %d\n", best_pitch, best_gain_index);*/ 
    647578   speex_bits_pack(bits, best_pitch-start, params->pitch_bits); 
    648579   speex_bits_pack(bits, best_gain_index, params->gain_bits); 
     580#ifdef FIXED_POINT 
     581   *cumul_gain = MULT16_32_Q13(SHL16(params->gain_cdbk[4*best_gain_index+3],8), MAX32(1024,*cumul_gain)); 
     582#else 
     583   *cumul_gain = 0.03125*MAX32(1024,*cumul_gain)*params->gain_cdbk[4*best_gain_index+3]; 
     584#endif 
     585   /*printf ("%f\n", cumul_gain);*/ 
    649586   /*printf ("encode pitch: %d %d\n", best_pitch, best_gain_index);*/ 
    650587   for (i=0;i<nsf;i++) 
     
    657594 
    658595void pitch_unquant_3tap( 
    659 spx_sig_t exc[],                    /* Excitation */ 
     596spx_word16_t exc[],             /* Input excitation */ 
     597spx_word32_t exc_out[],         /* Output excitation */ 
    660598int   start,                    /* Smallest pitch value allowed */ 
    661599int   end,                      /* Largest pitch value allowed */ 
    662 spx_word16_t pitch_coef,               /* Voicing (pitch) coefficient */ 
     600spx_word16_t pitch_coef,        /* Voicing (pitch) coefficient */ 
    663601const void *par, 
    664602int   nsf,                      /* Number of samples in subframe */ 
     
    683621   params = (const ltp_params*) par; 
    684622   gain_cdbk_size = 1<<params->gain_bits; 
    685    gain_cdbk = params->gain_cdbk + 3*gain_cdbk_size*cdbk_offset; 
     623   gain_cdbk = params->gain_cdbk + 4*gain_cdbk_size*cdbk_offset; 
    686624 
    687625   pitch = speex_bits_unpack_unsigned(bits, params->pitch_bits); 
     
    690628   /*printf ("decode pitch: %d %d\n", pitch, gain_index);*/ 
    691629#ifdef FIXED_POINT 
    692    gain[0] = ADD16(32,(spx_word16_t)gain_cdbk[gain_index*3]); 
    693    gain[1] = ADD16(32,(spx_word16_t)gain_cdbk[gain_index*3+1]); 
    694    gain[2] = ADD16(32,(spx_word16_t)gain_cdbk[gain_index*3+2]); 
    695 #else 
    696    gain[0] = 0.015625*gain_cdbk[gain_index*3]+.5; 
    697    gain[1] = 0.015625*gain_cdbk[gain_index*3+1]+.5; 
    698    gain[2] = 0.015625*gain_cdbk[gain_index*3+2]+.5; 
     630   gain[0] = ADD16(32,(spx_word16_t)gain_cdbk[gain_index*4]); 
     631   gain[1] = ADD16(32,(spx_word16_t)gain_cdbk[gain_index*4+1]); 
     632   gain[2] = ADD16(32,(spx_word16_t)gain_cdbk[gain_index*4+2]); 
     633#else 
     634   gain[0] = 0.015625*gain_cdbk[gain_index*4]+.5; 
     635   gain[1] = 0.015625*gain_cdbk[gain_index*4+1]+.5; 
     636   gain[2] = 0.015625*gain_cdbk[gain_index*4+2]+.5; 
    699637#endif 
    700638 
     
    729667   gain_val[1]=gain[1]; 
    730668   gain_val[2]=gain[2]; 
    731  
    732    { 
    733       spx_sig_t *e[3]; 
    734       VARDECL(spx_sig_t *tmp2); 
    735       ALLOC(tmp2, 3*nsf, spx_sig_t); 
    736       e[0]=tmp2; 
    737       e[1]=tmp2+nsf; 
    738       e[2]=tmp2+2*nsf; 
    739        
    740       for (i=0;i<3;i++) 
    741       { 
    742          int j; 
    743          int pp=pitch+1-i; 
    744 #if 0 
    745          for (j=0;j<nsf;j++) 
    746          { 
    747             if (j-pp<0) 
    748                e[i][j]=exc[j-pp]; 
    749             else if (j-pp-pitch<0) 
    750                e[i][j]=exc[j-pp-pitch]; 
    751             else 
    752                e[i][j]=0; 
    753          } 
    754 #else 
    755          { 
    756             int tmp1, tmp3; 
    757             tmp1=nsf; 
    758             if (tmp1>pp) 
    759                tmp1=pp; 
    760             for (j=0;j<tmp1;j++) 
    761                e[i][j]=exc[j-pp]; 
    762             tmp3=nsf; 
    763             if (tmp3>pp+pitch) 
    764                tmp3=pp+pitch; 
    765             for (j=tmp1;j<tmp3;j++) 
    766                e[i][j]=exc[j-pp-pitch]; 
    767             for (j=tmp3;j<nsf;j++) 
    768                e[i][j]=0; 
    769          } 
    770 #endif 
    771       } 
    772  
    773 #ifdef FIXED_POINT 
    774       { 
    775          for (i=0;i<nsf;i++) 
    776             exc[i]=SHL32(ADD32(ADD32(MULT16_32_Q15(SHL16(gain[0],7),e[2][i]), MULT16_32_Q15(SHL16(gain[1],7),e[1][i])), 
    777                                MULT16_32_Q15(SHL16(gain[2],7),e[0][i])), 2); 
    778       } 
    779 #else 
    780       for (i=0;i<nsf;i++) 
    781          exc[i]=VERY_SMALL+gain[0]*e[2][i]+gain[1]*e[1][i]+gain[2]*e[0][i]; 
    782 #endif 
    783    } 
     669   gain[0] = SHL16(gain[0],7); 
     670   gain[1] = SHL16(gain[1],7); 
     671   gain[2] = SHL16(gain[2],7); 
     672   for (i=0;i<nsf;i++) 
     673      exc_out[i]=0; 
     674   for (i=0;i<3;i++) 
     675   { 
     676      int j; 
     677      int tmp1, tmp3; 
     678      int pp=pitch+1-i; 
     679      tmp1=nsf; 
     680      if (tmp1>pp) 
     681         tmp1=pp; 
     682      for (j=0;j<tmp1;j++) 
     683         exc_out[j]=MAC16_16(exc_out[j],gain[2-i],exc[j-pp]); 
     684      tmp3=nsf; 
     685      if (tmp3>pp+pitch) 
     686         tmp3=pp+pitch; 
     687      for (j=tmp1;j<tmp3;j++) 
     688         exc_out[j]=MAC16_16(exc_out[j],gain[2-i],exc[j-pp-pitch]); 
     689   } 
     690   /*for (i=0;i<nsf;i++) 
     691   exc[i]=PSHR32(exc32[i],13);*/ 
    784692} 
    785693 
     
    787695/** Forced pitch delay and gain */ 
    788696int forced_pitch_quant( 
    789 spx_sig_t target[],                 /* Target vector */ 
    790 spx_sig_t *sw, 
     697spx_word16_t target[],                 /* Target vector */ 
     698spx_word16_t *sw, 
    791699spx_coef_t ak[],                     /* LPCs for this subframe */ 
    792700spx_coef_t awk1[],                   /* Weighted LPCs #1 for this subframe */ 
     
    801709SpeexBits *bits, 
    802710char *stack, 
    803 spx_sig_t *exc2, 
     711spx_word16_t *exc2, 
    804712spx_word16_t *r, 
    805713int complexity, 
    806714int cdbk_offset, 
    807 int plc_tuning 
     715int plc_tuning, 
     716spx_word32_t *cumul_gain 
    808717) 
    809718{ 
    810719   int i; 
    811    float coef = GAIN_SCALING_1*pitch_coef; 
    812    if (coef>.99) 
    813       coef=.99; 
     720   VARDECL(spx_sig_t *res); 
     721   ALLOC(res, nsf, spx_sig_t); 
     722#ifdef FIXED_POINT 
     723   if (pitch_coef>63) 
     724      pitch_coef=63; 
     725#else 
     726   if (pitch_coef>.99) 
     727      pitch_coef=.99; 
     728#endif 
     729   for (i=0;i<nsf&&i<start;i++) 
     730   { 
     731      exc[i]=MULT16_16(SHL16(pitch_coef, 7),exc2[i-start]); 
     732   } 
     733   for (;i<nsf;i++) 
     734   { 
     735      exc[i]=MULT16_32_Q15(SHL16(pitch_coef, 9),exc[i-start]); 
     736   } 
     737   syn_percep_zero(exc, ak, awk1, awk2, res, nsf, p, stack); 
    814738   for (i=0;i<nsf;i++) 
    815    { 
    816       exc[i]=exc[i-start]*coef; 
    817    } 
     739      target[i]=EXTRACT16(SATURATE(SUB32(EXTEND32(target[i]),PSHR32(res[i],SIG_SHIFT-1)),32700)); 
    818740   return start; 
    819741} 
     
    821743/** Unquantize forced pitch delay and gain */ 
    822744void forced_pitch_unquant( 
    823 spx_sig_t exc[],                    /* Excitation */ 
     745spx_word16_t exc[],             /* Input excitation */ 
     746spx_word32_t exc_out[],         /* Output excitation */ 
    824747int   start,                    /* Smallest pitch value allowed */ 
    825748int   end,                      /* Largest pitch value allowed */ 
    826 spx_word16_t pitch_coef,               /* Voicing (pitch) coefficient */ 
     749spx_word16_t pitch_coef,        /* Voicing (pitch) coefficient */ 
    827750const void *par, 
    828751int   nsf,                      /* Number of samples in subframe */ 
     
    838761{ 
    839762   int i; 
    840    float coef = GAIN_SCALING_1*pitch_coef; 
    841    if (coef>.99) 
    842       coef=.99; 
     763#ifdef FIXED_POINT 
     764   if (pitch_coef>63) 
     765      pitch_coef=63; 
     766#else 
     767   if (pitch_coef>.99) 
     768      pitch_coef=.99; 
     769#endif 
    843770   for (i=0;i<nsf;i++) 
    844771   { 
    845       exc[i]=exc[i-start]*coef; 
     772      exc_out[i]=MULT16_16(exc[i-start],SHL16(pitch_coef,7)); 
     773      exc[i] = PSHR(exc_out[i],13); 
    846774   } 
    847775   *pitch_val = start; 
Note: See TracChangeset for help on using the changeset viewer.