~ubuntu-branches/ubuntu/dapper/speex/dapper-security

« back to all changes in this revision

Viewing changes to libspeex/nb_celp.c

  • Committer: Bazaar Package Importer
  • Author(s): Mark Purcell
  • Date: 2005-12-07 23:22:21 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20051207232221-nme7vf9m182p7dpe
Tags: 1.1.11.1-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
48
48
#include "misc.h"
49
49
#include <speex/speex_callbacks.h>
50
50
 
51
 
#include <stdio.h>
 
51
#ifdef VORBIS_PSYCHO
 
52
#include "vorbis_psy.h"
 
53
#endif
52
54
 
53
55
#ifndef M_PI
54
56
#define M_PI           3.14159265358979323846  /* pi */
60
62
 
61
63
#define SUBMODE(x) st->submodes[st->submodeID]->x
62
64
 
 
65
/* Default size for the encoder and decoder stack (can be changed at compile time).
 
66
   This does not apply when using variable-size arrays or alloca. */
 
67
#ifndef NB_ENC_STACK
 
68
#define NB_ENC_STACK (8000*sizeof(spx_sig_t))
 
69
#endif
 
70
 
 
71
#ifndef NB_DEC_STACK
 
72
#define NB_DEC_STACK (4000*sizeof(spx_sig_t))
 
73
#endif
 
74
 
63
75
 
64
76
#ifdef FIXED_POINT
65
77
const spx_word32_t ol_gain_table[32]={18900, 25150, 33468, 44536, 59265, 78865, 104946, 139653, 185838, 247297, 329081, 437913, 582736, 775454, 1031906, 1373169, 1827293, 2431601, 3235761, 4305867, 5729870, 7624808, 10146425, 13501971, 17967238, 23909222, 31816294, 42338330, 56340132, 74972501, 99766822, 132760927};
96
108
   const SpeexNBMode *mode;
97
109
   int i;
98
110
 
99
 
   mode=(SpeexNBMode *)m->mode;
100
 
   st = (EncState*)speex_alloc(sizeof(EncState)+8000*sizeof(spx_sig_t));
 
111
   mode=(const SpeexNBMode *)m->mode;
 
112
   st = (EncState*)speex_alloc(sizeof(EncState));
101
113
   if (!st)
102
114
      return NULL;
103
 
 
104
 
   st->stack = ((char*)st) + sizeof(EncState);
 
115
#if defined(VAR_ARRAYS) || defined (USE_ALLOCA)
 
116
   st->stack = NULL;
 
117
#else
 
118
   st->stack = (char*)speex_alloc_scratch(NB_ENC_STACK);
 
119
#endif
105
120
   
106
121
   st->mode=m;
107
122
 
110
125
   st->nbSubframes=mode->frameSize/mode->subframeSize;
111
126
   st->subframeSize=mode->subframeSize;
112
127
   st->lpcSize = mode->lpcSize;
113
 
   st->bufSize = mode->bufSize;
114
128
   st->gamma1=mode->gamma1;
115
129
   st->gamma2=mode->gamma2;
116
130
   st->min_pitch=mode->pitchStart;
127
141
   st->lbr_48k=mode->lbr48k;
128
142
#endif
129
143
 
 
144
#ifdef VORBIS_PSYCHO
 
145
   st->psy = vorbis_psy_init(8000, 128);
 
146
   st->curve = speex_alloc(64*sizeof(float));
 
147
   st->old_curve = speex_alloc(64*sizeof(float));
 
148
#endif
 
149
 
130
150
   /* Allocating input buffer */
131
 
   st->inBuf = PUSH(st->stack, st->bufSize, spx_sig_t);
132
 
   st->frame = st->inBuf + st->bufSize - st->windowSize;
 
151
   st->inBuf = speex_alloc((st->windowSize)*sizeof(spx_sig_t));
 
152
   st->frame = st->inBuf;
133
153
   /* Allocating excitation buffer */
134
 
   st->excBuf = PUSH(st->stack, st->bufSize, spx_sig_t);
135
 
   st->exc = st->excBuf + st->bufSize - st->windowSize;
136
 
   st->swBuf = PUSH(st->stack, st->bufSize, spx_sig_t);
137
 
   st->sw = st->swBuf + st->bufSize - st->windowSize;
138
 
 
139
 
   st->exc2Buf = PUSH(st->stack, st->bufSize, spx_sig_t);
140
 
   st->exc2 = st->exc2Buf + st->bufSize - st->windowSize;
141
 
 
142
 
   st->innov = PUSH(st->stack, st->frameSize, spx_sig_t);
 
154
   st->excBuf = speex_alloc((mode->frameSize+mode->pitchEnd+1)*sizeof(spx_sig_t));
 
155
   st->exc = st->excBuf + mode->pitchEnd + 1;
 
156
   st->swBuf = speex_alloc((mode->frameSize+mode->pitchEnd+1)*sizeof(spx_sig_t));
 
157
   st->sw = st->swBuf + mode->pitchEnd + 1;
 
158
 
 
159
   st->innov = speex_alloc((st->frameSize)*sizeof(spx_sig_t));
143
160
 
144
161
   /* Asymmetric "pseudo-Hamming" window */
145
162
   {
146
163
      int part1, part2;
147
164
      part1=st->frameSize - (st->subframeSize>>1);
148
165
      part2=(st->frameSize>>1) + (st->subframeSize>>1);
149
 
      st->window = PUSH(st->stack, st->windowSize, spx_word16_t);
 
166
      st->window = speex_alloc((st->windowSize)*sizeof(spx_word16_t));
150
167
      for (i=0;i<part1;i++)
151
168
         st->window[i]=(spx_word16_t)(SIG_SCALING*(.54-.46*cos(M_PI*i/part1)));
152
169
      for (i=0;i<part2;i++)
153
170
         st->window[part1+i]=(spx_word16_t)(SIG_SCALING*(.54+.46*cos(M_PI*i/part2)));
154
171
   }
155
172
   /* Create the window for autocorrelation (lag-windowing) */
156
 
   st->lagWindow = PUSH(st->stack, st->lpcSize+1, spx_word16_t);
 
173
   st->lagWindow = speex_alloc((st->lpcSize+1)*sizeof(spx_word16_t));
157
174
   for (i=0;i<st->lpcSize+1;i++)
158
175
      st->lagWindow[i]=16384*exp(-.5*sqr(2*M_PI*st->lag_factor*i));
159
176
 
160
 
   st->autocorr = PUSH(st->stack, st->lpcSize+1, spx_word16_t);
161
 
 
162
 
   st->buf2 = PUSH(st->stack, st->windowSize, spx_sig_t);
163
 
 
164
 
   st->lpc = PUSH(st->stack, st->lpcSize+1, spx_coef_t);
165
 
   st->interp_lpc = PUSH(st->stack, st->lpcSize+1, spx_coef_t);
166
 
   st->interp_qlpc = PUSH(st->stack, st->lpcSize+1, spx_coef_t);
167
 
   st->bw_lpc1 = PUSH(st->stack, st->lpcSize+1, spx_coef_t);
168
 
   st->bw_lpc2 = PUSH(st->stack, st->lpcSize+1, spx_coef_t);
169
 
 
170
 
   st->lsp = PUSH(st->stack, st->lpcSize, spx_lsp_t);
171
 
   st->qlsp = PUSH(st->stack, st->lpcSize, spx_lsp_t);
172
 
   st->old_lsp = PUSH(st->stack, st->lpcSize, spx_lsp_t);
173
 
   st->old_qlsp = PUSH(st->stack, st->lpcSize, spx_lsp_t);
174
 
   st->interp_lsp = PUSH(st->stack, st->lpcSize, spx_lsp_t);
175
 
   st->interp_qlsp = PUSH(st->stack, st->lpcSize, spx_lsp_t);
 
177
   st->autocorr = speex_alloc((st->lpcSize+1)*sizeof(spx_word16_t));
 
178
 
 
179
   st->lpc = speex_alloc((st->lpcSize)*sizeof(spx_coef_t));
 
180
   st->interp_lpc = speex_alloc((st->lpcSize)*sizeof(spx_coef_t));
 
181
   st->interp_qlpc = speex_alloc((st->lpcSize)*sizeof(spx_coef_t));
 
182
   st->bw_lpc1 = speex_alloc((st->lpcSize)*sizeof(spx_coef_t));
 
183
   st->bw_lpc2 = speex_alloc((st->lpcSize)*sizeof(spx_coef_t));
 
184
 
 
185
   st->lsp = speex_alloc((st->lpcSize)*sizeof(spx_lsp_t));
 
186
   st->qlsp = speex_alloc((st->lpcSize)*sizeof(spx_lsp_t));
 
187
   st->old_lsp = speex_alloc((st->lpcSize)*sizeof(spx_lsp_t));
 
188
   st->old_qlsp = speex_alloc((st->lpcSize)*sizeof(spx_lsp_t));
 
189
   st->interp_lsp = speex_alloc((st->lpcSize)*sizeof(spx_lsp_t));
 
190
   st->interp_qlsp = speex_alloc((st->lpcSize)*sizeof(spx_lsp_t));
176
191
 
177
192
   st->first = 1;
178
193
   for (i=0;i<st->lpcSize;i++)
180
195
      st->lsp[i]=LSP_SCALING*(M_PI*((float)(i+1)))/(st->lpcSize+1);
181
196
   }
182
197
 
183
 
   st->mem_sp = PUSH(st->stack, st->lpcSize, spx_mem_t);
184
 
   st->mem_sw = PUSH(st->stack, st->lpcSize, spx_mem_t);
185
 
   st->mem_sw_whole = PUSH(st->stack, st->lpcSize, spx_mem_t);
186
 
   st->mem_exc = PUSH(st->stack, st->lpcSize, spx_mem_t);
187
 
 
188
 
   st->pi_gain = PUSH(st->stack, st->nbSubframes, spx_word32_t);
189
 
 
190
 
   st->pitch = PUSH(st->stack, st->nbSubframes, int);
191
 
 
192
 
   st->vbr = PUSHS(st->stack, VBRState);
 
198
   st->mem_sp = speex_alloc((st->lpcSize)*sizeof(spx_mem_t));
 
199
   st->mem_sw = speex_alloc((st->lpcSize)*sizeof(spx_mem_t));
 
200
   st->mem_sw_whole = speex_alloc((st->lpcSize)*sizeof(spx_mem_t));
 
201
   st->mem_exc = speex_alloc((st->lpcSize)*sizeof(spx_mem_t));
 
202
 
 
203
   st->pi_gain = speex_alloc((st->nbSubframes)*sizeof(spx_word32_t));
 
204
 
 
205
   st->pitch = speex_alloc((st->nbSubframes)*sizeof(int));
 
206
 
 
207
   st->vbr = speex_alloc(sizeof(VBRState));
193
208
   vbr_init(st->vbr);
194
209
   st->vbr_quality = 8;
195
210
   st->vbr_enabled = 0;
198
213
   st->abr_enabled = 0;
199
214
   st->abr_drift = 0;
200
215
 
 
216
   st->plc_tuning = 2;
201
217
   st->complexity=2;
202
218
   st->sampling_rate=8000;
203
219
   st->dtx_count=0;
212
228
{
213
229
   EncState *st=(EncState *)state;
214
230
   /* Free all allocated memory */
 
231
#if !(defined(VAR_ARRAYS) || defined (USE_ALLOCA))
 
232
   speex_free_scratch(st->stack);
 
233
#endif
 
234
 
 
235
   speex_free (st->inBuf);
 
236
   speex_free (st->excBuf);
 
237
   speex_free (st->innov);
 
238
   speex_free (st->interp_qlpc);
 
239
   speex_free (st->qlsp);
 
240
   speex_free (st->old_qlsp);
 
241
   speex_free (st->interp_qlsp);
 
242
   speex_free (st->swBuf);
 
243
 
 
244
   speex_free (st->window);
 
245
   speex_free (st->lagWindow);
 
246
   speex_free (st->autocorr);
 
247
   speex_free (st->lpc);
 
248
   speex_free (st->lsp);
 
249
 
 
250
   speex_free (st->interp_lpc);
 
251
   speex_free (st->bw_lpc1);
 
252
   speex_free (st->bw_lpc2);
 
253
   speex_free (st->old_lsp);
 
254
   speex_free (st->interp_lsp);
 
255
   speex_free (st->mem_sp);
 
256
   speex_free (st->mem_sw);
 
257
   speex_free (st->mem_sw_whole);
 
258
   speex_free (st->mem_exc);
 
259
   speex_free (st->pi_gain);
 
260
   speex_free (st->pitch);
215
261
 
216
262
   vbr_destroy(st->vbr);
 
263
   speex_free (st->vbr);
 
264
 
 
265
#ifdef VORBIS_PSYCHO
 
266
   vorbis_psy_destroy(st->psy);
 
267
   speex_free (st->curve);
 
268
   speex_free (st->old_curve);
 
269
#endif
217
270
 
218
271
   /*Free state memory... should be last*/
219
272
   speex_free(st);
226
279
   int ol_pitch;
227
280
   spx_word16_t ol_pitch_coef;
228
281
   spx_word32_t ol_gain;
229
 
   spx_sig_t *res, *target;
230
 
   spx_mem_t *mem;
 
282
   VARDECL(spx_sig_t *res);
 
283
   VARDECL(spx_sig_t *target);
 
284
   VARDECL(spx_mem_t *mem);
231
285
   char *stack;
232
 
   spx_sig_t *syn_resp;
233
 
   spx_sig_t *orig;
 
286
   VARDECL(spx_word16_t *syn_resp);
 
287
   VARDECL(spx_sig_t *real_exc);
234
288
#ifdef EPIC_48K
235
289
   int pitch_half[2];
236
290
   int ol_pitch_id=0;
241
295
   stack=st->stack;
242
296
 
243
297
   /* Copy new data in input buffer */
244
 
   speex_move(st->inBuf, st->inBuf+st->frameSize, (st->bufSize-st->frameSize)*sizeof(spx_sig_t));
 
298
   speex_move(st->inBuf, st->inBuf+st->frameSize, (st->windowSize-st->frameSize)*sizeof(spx_sig_t));
245
299
   for (i=0;i<st->frameSize;i++)
246
 
      st->inBuf[st->bufSize-st->frameSize+i] = SHL((int)in[i], SIG_SHIFT);
 
300
      st->inBuf[st->windowSize-st->frameSize+i] = SHL32(EXTEND32(in[i]), SIG_SHIFT);
247
301
 
248
302
   /* Move signals 1 frame towards the past */
249
 
   speex_move(st->exc2Buf, st->exc2Buf+st->frameSize, (st->bufSize-st->frameSize)*sizeof(spx_sig_t));
250
 
   speex_move(st->excBuf, st->excBuf+st->frameSize, (st->bufSize-st->frameSize)*sizeof(spx_sig_t));
251
 
   speex_move(st->swBuf, st->swBuf+st->frameSize, (st->bufSize-st->frameSize)*sizeof(spx_sig_t));
252
 
 
 
303
   speex_move(st->excBuf, st->excBuf+st->frameSize, (st->max_pitch+1)*sizeof(spx_sig_t));
 
304
   speex_move(st->swBuf, st->swBuf+st->frameSize, (st->max_pitch+1)*sizeof(spx_sig_t));
253
305
 
254
306
   {
255
 
      spx_word16_t *w_sig;
256
 
      w_sig = PUSH(stack, st->windowSize, spx_word16_t);
 
307
      VARDECL(spx_word16_t *w_sig);
 
308
      ALLOC(w_sig, st->windowSize, spx_word16_t);
257
309
      /* Window for analysis */
258
310
      for (i=0;i<st->windowSize;i++)
259
 
         w_sig[i] = SHR(MULT16_16(SHR((spx_word32_t)(st->frame[i]),SIG_SHIFT),st->window[i]),SIG_SHIFT);
 
311
         w_sig[i] = EXTRACT16(SHR32(MULT16_16(EXTRACT16(SHR32(st->frame[i],SIG_SHIFT)),st->window[i]),SIG_SHIFT));
260
312
 
261
313
      /* Compute auto-correlation */
262
314
      _spx_autocorr(w_sig, st->autocorr, st->lpcSize+1, st->windowSize);
263
315
   }
264
 
   st->autocorr[0] = (spx_word16_t) (st->autocorr[0]*st->lpc_floor); /* Noise floor in auto-correlation domain */
 
316
   st->autocorr[0] = ADD16(st->autocorr[0],MULT16_16_Q15(st->autocorr[0],st->lpc_floor)); /* Noise floor in auto-correlation domain */
265
317
 
266
318
   /* Lag windowing: equivalent to filtering in the power-spectrum domain */
267
319
   for (i=0;i<st->lpcSize+1;i++)
268
320
      st->autocorr[i] = MULT16_16_Q14(st->autocorr[i],st->lagWindow[i]);
269
321
 
270
322
   /* Levinson-Durbin */
271
 
   _spx_lpc(st->lpc+1, st->autocorr, st->lpcSize);
272
 
   st->lpc[0]=(spx_coef_t)LPC_SCALING;
 
323
   _spx_lpc(st->lpc, st->autocorr, st->lpcSize);
273
324
 
274
325
   /* LPC to LSPs (x-domain) transform */
275
326
   roots=lpc_to_lsp (st->lpc, st->lpcSize, st->lsp, 15, LSP_DELTA1, stack);
380
431
 
381
432
      } else {
382
433
#endif
383
 
         ol_gain = SHL(compute_rms(st->exc, st->frameSize),SIG_SHIFT);
 
434
         ol_gain = SHL32(EXTEND32(compute_rms(st->exc, st->frameSize)),SIG_SHIFT);
384
435
#ifdef EPIC_48K
385
436
      }
386
437
#endif
387
438
   }
388
439
 
 
440
#ifdef VORBIS_PSYCHO
 
441
   compute_curve(st->psy, st->frame+52, st->curve);
 
442
   if (st->first)
 
443
      for (i=0;i<64;i++)
 
444
         st->old_curve[i] = st->curve[i];
 
445
#endif
 
446
 
389
447
   /*VBR stuff*/
390
448
   if (st->vbr && (st->vbr_enabled||st->vad_enabled))
391
449
   {
510
568
   if (st->submodes[st->submodeID] == NULL)
511
569
   {
512
570
      for (i=0;i<st->frameSize;i++)
513
 
         st->exc[i]=st->exc2[i]=st->sw[i]=VERY_SMALL;
 
571
         st->exc[i]=st->sw[i]=VERY_SMALL;
514
572
 
515
573
      for (i=0;i<st->lpcSize;i++)
516
574
         st->mem_sw[i]=0;
520
578
      /* Final signal synthesis from excitation */
521
579
      iir_mem2(st->exc, st->interp_qlpc, st->frame, st->frameSize, st->lpcSize, st->mem_sp);
522
580
 
 
581
#ifdef RESYNTH
523
582
      for (i=0;i<st->frameSize;i++)
524
583
         in[i]=st->frame[i];
525
 
 
 
584
#endif
526
585
      return 0;
527
586
 
528
587
   }
625
684
   }
626
685
 
627
686
   /* Filter response */
628
 
   res = PUSH(stack, st->subframeSize, spx_sig_t);
 
687
   ALLOC(res, st->subframeSize, spx_sig_t);
629
688
   /* Target signal */
630
 
   target = PUSH(stack, st->subframeSize, spx_sig_t);
631
 
   syn_resp = PUSH(stack, st->subframeSize, spx_sig_t);
632
 
   mem = PUSH(stack, st->lpcSize, spx_mem_t);
633
 
   orig = PUSH(stack, st->frameSize, spx_sig_t);
634
 
   for (i=0;i<st->frameSize;i++)
635
 
      orig[i]=st->frame[i];
 
689
   ALLOC(target, st->subframeSize, spx_sig_t);
 
690
   ALLOC(syn_resp, st->subframeSize, spx_word16_t);
 
691
   ALLOC(real_exc, st->subframeSize, spx_sig_t);
 
692
   ALLOC(mem, st->lpcSize, spx_mem_t);
636
693
 
637
694
   /* Loop on sub-frames */
638
695
   for (sub=0;sub<st->nbSubframes;sub++)
639
696
   {
640
697
      int   offset;
641
 
      spx_sig_t *sp, *sw, *exc, *exc2;
 
698
      spx_sig_t *sp, *sw, *exc;
642
699
      int pitch;
643
 
 
 
700
      int response_bound = st->subframeSize;
644
701
#ifdef EPIC_48K
645
702
      if (st->lbr_48k)
646
703
      {
660
717
      /* Weighted signal */
661
718
      sw=st->sw+offset;
662
719
 
663
 
      exc2=st->exc2+offset;
664
 
 
665
 
 
666
720
      /* LSP interpolation (quantized and unquantized) */
667
721
      lsp_interpolate(st->old_lsp, st->lsp, st->interp_lsp, st->lpcSize, sub, st->nbSubframes);
668
722
      lsp_interpolate(st->old_qlsp, st->qlsp, st->interp_qlsp, st->lpcSize, sub, st->nbSubframes);
678
732
 
679
733
      /* Compute analysis filter gain at w=pi (for use in SB-CELP) */
680
734
      {
681
 
         spx_word32_t pi_g=st->interp_qlpc[0];
682
 
         for (i=1;i<=st->lpcSize;i+=2)
 
735
         spx_word32_t pi_g=LPC_SCALING;
 
736
         for (i=0;i<st->lpcSize;i+=2)
683
737
         {
684
 
            pi_g += -st->interp_qlpc[i] +  st->interp_qlpc[i+1];
 
738
            /*pi_g += -st->interp_qlpc[i] +  st->interp_qlpc[i+1];*/
 
739
            pi_g = ADD32(pi_g, SUB32(st->interp_qlpc[i+1],st->interp_qlpc[i]));
685
740
         }
686
741
         st->pi_gain[sub] = pi_g;
687
742
      }
688
743
 
689
 
 
 
744
#ifdef VORBIS_PSYCHO
 
745
      {
 
746
         float curr_curve[64];
 
747
         float fact = ((float)sub+1.0f)/st->nbSubframes;
 
748
         for (i=0;i<64;i++)
 
749
            curr_curve[i] = (1.0f-fact)*st->old_curve[i] + fact*st->curve[i];
 
750
         curve_to_lpc(st->psy, curr_curve, st->bw_lpc1, st->bw_lpc2, 10);
 
751
      }
 
752
#else
690
753
      /* Compute bandwidth-expanded (unquantized) LPCs for perceptual weighting */
691
754
      bw_lpc(st->gamma1, st->interp_lpc, st->bw_lpc1, st->lpcSize);
692
755
      if (st->gamma2>=0)
697
760
         for (i=1;i<=st->lpcSize;i++)
698
761
            st->bw_lpc2[i]=0;
699
762
      }
 
763
#endif
700
764
 
701
 
      /* Compute impulse response of A(z/g1) / ( A(z)*A(z/g2) )*/
702
765
      for (i=0;i<st->subframeSize;i++)
703
 
         exc[i]=VERY_SMALL;
704
 
      exc[0]=SIG_SCALING;
705
 
      syn_percep_zero(exc, st->interp_qlpc, st->bw_lpc1, st->bw_lpc2, syn_resp, st->subframeSize, st->lpcSize, stack);
706
 
 
 
766
         real_exc[i] = exc[i];
 
767
      
 
768
      if (st->complexity==0)
 
769
         response_bound >>= 1;
 
770
      compute_impulse_response(st->interp_qlpc, st->bw_lpc1, st->bw_lpc2, syn_resp, response_bound, st->lpcSize, stack);
 
771
      for (i=response_bound;i<st->subframeSize;i++)
 
772
         syn_resp[i]=VERY_SMALL;
 
773
      
707
774
      /* Reset excitation */
708
775
      for (i=0;i<st->subframeSize;i++)
709
776
         exc[i]=VERY_SMALL;
710
 
      for (i=0;i<st->subframeSize;i++)
711
 
         exc2[i]=VERY_SMALL;
712
777
 
713
778
      /* Compute zero response of A(z/g1) / ( A(z/g2) * A(z) ) */
714
779
      for (i=0;i<st->lpcSize;i++)
715
780
         mem[i]=st->mem_sp[i];
 
781
#ifdef SHORTCUTS2
 
782
      iir_mem2(exc, st->interp_qlpc, exc, response_bound, st->lpcSize, mem);
 
783
      for (i=0;i<st->lpcSize;i++)
 
784
         mem[i]=st->mem_sw[i];
 
785
      filter_mem2(exc, st->bw_lpc1, st->bw_lpc2, res, response_bound, st->lpcSize, mem);
 
786
      for (i=response_bound;i<st->subframeSize;i++)
 
787
         res[i]=0;
 
788
#else
716
789
      iir_mem2(exc, st->interp_qlpc, exc, st->subframeSize, st->lpcSize, mem);
717
 
      
718
790
      for (i=0;i<st->lpcSize;i++)
719
791
         mem[i]=st->mem_sw[i];
720
792
      filter_mem2(exc, st->bw_lpc1, st->bw_lpc2, res, st->subframeSize, st->lpcSize, mem);
 
793
#endif
721
794
      
722
795
      /* Compute weighted signal */
723
796
      for (i=0;i<st->lpcSize;i++)
724
797
         mem[i]=st->mem_sw[i];
725
798
      filter_mem2(sp, st->bw_lpc1, st->bw_lpc2, sw, st->subframeSize, st->lpcSize, mem);
726
799
      
 
800
      if (st->complexity==0)
 
801
         for (i=0;i<st->lpcSize;i++)
 
802
            st->mem_sw[i]=mem[i];
 
803
      
727
804
      /* Compute target signal */
728
805
      for (i=0;i<st->subframeSize;i++)
729
806
         target[i]=sw[i]-res[i];
730
807
 
731
808
      for (i=0;i<st->subframeSize;i++)
732
 
         exc[i]=exc2[i]=0;
 
809
         exc[i]=0;
733
810
 
734
811
      /* If we have a long-term predictor (otherwise, something's wrong) */
735
812
      if (SUBMODE(ltp_quant))
767
844
            pitch = SUBMODE(ltp_quant)(target, sw, st->interp_qlpc, st->bw_lpc1, st->bw_lpc2,
768
845
                                       exc, SUBMODE(ltp_params), pit_min, pit_max, ol_pitch_coef,
769
846
                                       st->lpcSize, st->subframeSize, bits, stack, 
770
 
                                       exc2, syn_resp, st->complexity, ol_pitch_id);
 
847
                                       exc, syn_resp, st->complexity, ol_pitch_id, st->plc_tuning);
771
848
         } else {
772
849
#endif
773
850
 
775
852
         pitch = SUBMODE(ltp_quant)(target, sw, st->interp_qlpc, st->bw_lpc1, st->bw_lpc2,
776
853
                                    exc, SUBMODE(ltp_params), pit_min, pit_max, ol_pitch_coef,
777
854
                                    st->lpcSize, st->subframeSize, bits, stack, 
778
 
                                    exc2, syn_resp, st->complexity, 0);
 
855
                                    exc, syn_resp, st->complexity, 0, st->plc_tuning);
779
856
#ifdef EPIC_48K
780
857
         }
781
858
#endif
785
862
         speex_error ("No pitch prediction, what's wrong");
786
863
      }
787
864
 
788
 
      /* Update target for adaptive codebook contribution */
789
 
      syn_percep_zero(exc, st->interp_qlpc, st->bw_lpc1, st->bw_lpc2, res, st->subframeSize, st->lpcSize, stack);
790
 
      for (i=0;i<st->subframeSize;i++)
791
 
         target[i]=SATURATE(SUB32(target[i],res[i]),805306368);
792
 
 
793
 
 
794
865
      /* Quantization of innovation */
795
866
      {
796
867
         spx_sig_t *innov;
801
872
         for (i=0;i<st->subframeSize;i++)
802
873
            innov[i]=0;
803
874
         
804
 
         residue_percep_zero(target, st->interp_qlpc, st->bw_lpc1, st->bw_lpc2, st->buf2, st->subframeSize, st->lpcSize, stack);
805
 
 
806
 
         ener = SHL(compute_rms(st->buf2, st->subframeSize),SIG_SHIFT);
807
 
 
808
 
         /*for (i=0;i<st->subframeSize;i++)
809
 
            printf ("%f\n", st->buf2[i]/ener);
810
 
         */
 
875
         for (i=0;i<st->subframeSize;i++)
 
876
            real_exc[i] = SUB32(real_exc[i], exc[i]);
 
877
 
 
878
         ener = SHL32(EXTEND32(compute_rms(real_exc, st->subframeSize)),SIG_SHIFT);
811
879
         
812
880
         /*FIXME: Should use DIV32_16 and make sure result fits in 16 bits */
813
881
#ifdef FIXED_POINT
814
882
         {
815
 
            spx_word32_t f = DIV32(ener,PSHR(ol_gain,SIG_SHIFT));
816
 
            if (f<32768)
 
883
            spx_word32_t f = DIV32(ener,PSHR32(ol_gain,SIG_SHIFT));
 
884
            if (f<=32767)
817
885
               fine_gain = f;
818
886
            else
819
887
               fine_gain = 32767;
820
888
         }
821
889
#else
822
 
         fine_gain = DIV32_16(ener,PSHR(ol_gain,SIG_SHIFT));
 
890
         fine_gain = DIV32_16(ener,PSHR32(ol_gain,SIG_SHIFT));
823
891
#endif
824
892
         /* Calculate gain correction for the sub-frame (if any) */
825
893
         if (SUBMODE(have_subframe_gain)) 
850
918
            /* Codebook search */
851
919
            SUBMODE(innovation_quant)(target, st->interp_qlpc, st->bw_lpc1, st->bw_lpc2, 
852
920
                                      SUBMODE(innovation_params), st->lpcSize, st->subframeSize, 
853
 
                                      innov, syn_resp, bits, stack, st->complexity);
 
921
                                      innov, syn_resp, bits, stack, st->complexity, SUBMODE(double_codebook));
854
922
            
855
923
            /* De-normalize innovation and update excitation */
856
924
            signal_mul(innov, innov, ener, st->subframeSize);
857
925
 
858
926
            for (i=0;i<st->subframeSize;i++)
859
 
               exc[i] += innov[i];
 
927
               exc[i] = ADD32(exc[i],innov[i]);
860
928
         } else {
861
929
            speex_error("No fixed codebook");
862
930
         }
864
932
         /* In some (rare) modes, we do a second search (more bits) to reduce noise even more */
865
933
         if (SUBMODE(double_codebook)) {
866
934
            char *tmp_stack=stack;
867
 
            spx_sig_t *innov2 = PUSH(tmp_stack, st->subframeSize, spx_sig_t);
 
935
            VARDECL(spx_sig_t *innov2);
 
936
            ALLOC(innov2, st->subframeSize, spx_sig_t);
868
937
            for (i=0;i<st->subframeSize;i++)
869
938
               innov2[i]=0;
870
939
            for (i=0;i<st->subframeSize;i++)
871
940
               target[i]*=2.2;
872
941
            SUBMODE(innovation_quant)(target, st->interp_qlpc, st->bw_lpc1, st->bw_lpc2, 
873
942
                                      SUBMODE(innovation_params), st->lpcSize, st->subframeSize, 
874
 
                                      innov2, syn_resp, bits, tmp_stack, st->complexity);
875
 
            signal_mul(innov2, innov2, ener*(1/2.2), st->subframeSize);
 
943
                                      innov2, syn_resp, bits, stack, st->complexity, 0);
 
944
            signal_mul(innov2, innov2, (spx_word32_t) (ener*(1.f/2.2f)), st->subframeSize);
876
945
            for (i=0;i<st->subframeSize;i++)
877
 
               exc[i] += innov2[i];
 
946
               exc[i] = ADD32(exc[i],innov2[i]);
 
947
            stack = tmp_stack;
878
948
         }
879
949
 
880
 
         signal_mul(target, target, ener, st->subframeSize);
881
950
      }
882
951
 
883
 
      /*Keep the previous memory*/
884
 
      for (i=0;i<st->lpcSize;i++)
885
 
         mem[i]=st->mem_sp[i];
886
952
      /* Final signal synthesis from excitation */
887
953
      iir_mem2(exc, st->interp_qlpc, sp, st->subframeSize, st->lpcSize, st->mem_sp);
888
954
 
889
955
      /* Compute weighted signal again, from synthesized speech (not sure it's the right thing) */
890
 
      filter_mem2(sp, st->bw_lpc1, st->bw_lpc2, sw, st->subframeSize, st->lpcSize, st->mem_sw);
891
 
      for (i=0;i<st->subframeSize;i++)
892
 
         exc2[i]=exc[i];
 
956
      if (st->complexity!=0)
 
957
         filter_mem2(sp, st->bw_lpc1, st->bw_lpc2, sw, st->subframeSize, st->lpcSize, st->mem_sw);
 
958
      
893
959
   }
894
960
 
895
961
   /* Store the LSPs for interpolation in the next frame */
912
978
   /* The next frame will not be the first (Duh!) */
913
979
   st->first = 0;
914
980
 
915
 
   if (0) {
916
 
      float ener=0, err=0;
917
 
      float snr;
918
 
      for (i=0;i<st->frameSize;i++)
919
 
      {
920
 
         ener+=st->frame[i]*st->frame[i];
921
 
         err += (st->frame[i]-orig[i])*(st->frame[i]-orig[i]);
922
 
      }
923
 
      snr = 10*log10((ener+1)/(err+1));
924
 
      /*printf ("%f %f %f\n", snr, ener, err);*/
925
 
   }
926
 
 
 
981
#ifdef RESYNTH
927
982
   /* Replace input by synthesized speech */
928
983
   for (i=0;i<st->frameSize;i++)
929
984
   {
930
 
      spx_word32_t sig = PSHR(st->frame[i],SIG_SHIFT);
 
985
      spx_word32_t sig = PSHR32(st->frame[i],SIG_SHIFT);
931
986
      if (sig>32767)
932
987
         sig = 32767;
933
988
      if (sig<-32767)
934
989
         sig = -32767;
935
990
     in[i]=sig;
936
991
   }
 
992
#endif
937
993
 
938
994
   if (SUBMODE(innovation_quant) == noise_codebook_quant || st->submodeID==0)
939
995
      st->bounded_pitch = 1;
950
1006
   const SpeexNBMode *mode;
951
1007
   int i;
952
1008
 
953
 
   mode=(SpeexNBMode*)m->mode;
954
 
   st = (DecState *)speex_alloc(sizeof(DecState)+4000*sizeof(spx_sig_t));
 
1009
   mode=(const SpeexNBMode*)m->mode;
 
1010
   st = (DecState *)speex_alloc(sizeof(DecState));
 
1011
   if (!st)
 
1012
      return NULL;
 
1013
#if defined(VAR_ARRAYS) || defined (USE_ALLOCA)
 
1014
   st->stack = NULL;
 
1015
#else
 
1016
   st->stack = (char*)speex_alloc_scratch(NB_DEC_STACK);
 
1017
#endif
 
1018
 
955
1019
   st->mode=m;
956
1020
 
957
 
   st->stack = ((char*)st) + sizeof(DecState);
958
1021
 
959
1022
   st->encode_submode = 1;
960
1023
#ifdef EPIC_48K
964
1027
   st->first=1;
965
1028
   /* Codec parameters, should eventually have several "modes"*/
966
1029
   st->frameSize = mode->frameSize;
967
 
   st->windowSize = st->frameSize*3/2;
968
1030
   st->nbSubframes=mode->frameSize/mode->subframeSize;
969
1031
   st->subframeSize=mode->subframeSize;
970
1032
   st->lpcSize = mode->lpcSize;
971
 
   st->bufSize = mode->bufSize;
972
1033
   st->min_pitch=mode->pitchStart;
973
1034
   st->max_pitch=mode->pitchEnd;
974
1035
 
978
1039
   st->lpc_enh_enabled=0;
979
1040
 
980
1041
 
981
 
   st->inBuf = PUSH(st->stack, st->bufSize, spx_sig_t);
982
 
   st->frame = st->inBuf + st->bufSize - st->windowSize;
983
 
   st->excBuf = PUSH(st->stack, st->bufSize, spx_sig_t);
984
 
   st->exc = st->excBuf + st->bufSize - st->windowSize;
985
 
   for (i=0;i<st->bufSize;i++)
 
1042
   st->inBuf = speex_alloc((st->frameSize)*sizeof(spx_sig_t));
 
1043
   st->frame = st->inBuf;
 
1044
   st->excBuf = speex_alloc((st->frameSize + st->max_pitch + 1)*sizeof(spx_sig_t));
 
1045
   st->exc = st->excBuf + st->max_pitch + 1;
 
1046
   for (i=0;i<st->frameSize;i++)
986
1047
      st->inBuf[i]=0;
987
 
   for (i=0;i<st->bufSize;i++)
 
1048
   for (i=0;i<st->frameSize + st->max_pitch + 1;i++)
988
1049
      st->excBuf[i]=0;
989
 
   st->innov = PUSH(st->stack, st->frameSize, spx_sig_t);
 
1050
   st->innov = speex_alloc((st->frameSize)*sizeof(spx_sig_t));
990
1051
 
991
 
   st->interp_qlpc = PUSH(st->stack, st->lpcSize+1, spx_coef_t);
992
 
   st->qlsp = PUSH(st->stack, st->lpcSize, spx_lsp_t);
993
 
   st->old_qlsp = PUSH(st->stack, st->lpcSize, spx_lsp_t);
994
 
   st->interp_qlsp = PUSH(st->stack, st->lpcSize, spx_lsp_t);
995
 
   st->mem_sp = PUSH(st->stack, 5*st->lpcSize, spx_mem_t);
996
 
   st->comb_mem = PUSHS(st->stack, CombFilterMem);
 
1052
   st->interp_qlpc = speex_alloc(st->lpcSize*sizeof(spx_coef_t));
 
1053
   st->qlsp = speex_alloc(st->lpcSize*sizeof(spx_lsp_t));
 
1054
   st->old_qlsp = speex_alloc(st->lpcSize*sizeof(spx_lsp_t));
 
1055
   st->interp_qlsp = speex_alloc(st->lpcSize*sizeof(spx_lsp_t));
 
1056
   st->mem_sp = speex_alloc((5*st->lpcSize)*sizeof(spx_mem_t));
 
1057
   st->comb_mem = speex_alloc(sizeof(CombFilterMem));
997
1058
   comb_filter_mem_init (st->comb_mem);
998
1059
 
999
 
   st->pi_gain = PUSH(st->stack, st->nbSubframes, spx_word32_t);
 
1060
   st->pi_gain = speex_alloc((st->nbSubframes)*sizeof(spx_word32_t));
1000
1061
   st->last_pitch = 40;
1001
1062
   st->count_lost=0;
1002
1063
   st->pitch_gain_buf[0] = st->pitch_gain_buf[1] = st->pitch_gain_buf[2] = 0;
1003
1064
   st->pitch_gain_buf_idx = 0;
1004
 
 
 
1065
   st->seed = 1000;
 
1066
   
1005
1067
   st->sampling_rate=8000;
1006
1068
   st->last_ol_gain = 0;
1007
1069
 
1024
1086
   DecState *st;
1025
1087
   st=(DecState*)state;
1026
1088
   
 
1089
#if !(defined(VAR_ARRAYS) || defined (USE_ALLOCA))
 
1090
   speex_free_scratch(st->stack);
 
1091
#endif
 
1092
 
 
1093
   speex_free (st->inBuf);
 
1094
   speex_free (st->excBuf);
 
1095
   speex_free (st->innov);
 
1096
   speex_free (st->interp_qlpc);
 
1097
   speex_free (st->qlsp);
 
1098
   speex_free (st->old_qlsp);
 
1099
   speex_free (st->interp_qlsp);
 
1100
   speex_free (st->mem_sp);
 
1101
   speex_free (st->comb_mem);
 
1102
   speex_free (st->pi_gain);
 
1103
 
1027
1104
   speex_free(state);
1028
1105
}
1029
1106
 
1030
1107
#define median3(a, b, c)        ((a) < (b) ? ((b) < (c) ? (b) : ((a) < (c) ? (c) : (a))) : ((c) < (b) ? (b) : ((c) < (a) ? (c) : (a))))
1031
1108
 
 
1109
#ifdef FIXED_POINT
 
1110
const spx_word16_t attenuation[10] = {32767, 31483, 27923, 22861, 17278, 12055, 7764, 4616, 2533, 1283};
 
1111
#else
 
1112
const spx_word16_t attenuation[10] = {1., 0.961, 0.852, 0.698, 0.527, 0.368, 0.237, 0.141, 0.077, 0.039};
 
1113
 
 
1114
#endif
 
1115
 
1032
1116
static void nb_decode_lost(DecState *st, spx_word16_t *out, char *stack)
1033
1117
{
1034
1118
   int i, sub;
1035
 
   spx_coef_t *awk1, *awk2, *awk3;
1036
 
   float pitch_gain, fact;
 
1119
   int pitch_val;
 
1120
   VARDECL(spx_coef_t *awk1);
 
1121
   VARDECL(spx_coef_t *awk2);
 
1122
   VARDECL(spx_coef_t *awk3);
 
1123
   spx_word16_t pitch_gain;
 
1124
   spx_word16_t fact;
1037
1125
   spx_word16_t gain_med;
 
1126
   spx_word16_t innov_gain;
 
1127
   
 
1128
   if (st->count_lost<10)
 
1129
      fact = attenuation[st->count_lost];
 
1130
   else
 
1131
      fact = 0;
1038
1132
 
1039
 
   fact = exp(-.04*st->count_lost*st->count_lost);
1040
1133
   gain_med = median3(st->pitch_gain_buf[0], st->pitch_gain_buf[1], st->pitch_gain_buf[2]);
1041
1134
   if (gain_med < st->last_pitch_gain)
1042
1135
      st->last_pitch_gain = gain_med;
1043
1136
   
 
1137
#ifdef FIXED_POINT
 
1138
   pitch_gain = st->last_pitch_gain;
 
1139
   if (pitch_gain>54)
 
1140
      pitch_gain = 54;
 
1141
   pitch_gain = SHL(pitch_gain, 9);
 
1142
#else   
1044
1143
   pitch_gain = GAIN_SCALING_1*st->last_pitch_gain;
1045
 
   if (pitch_gain>.95)
1046
 
      pitch_gain=.95;
 
1144
   if (pitch_gain>.85)
 
1145
      pitch_gain=.85;
 
1146
#endif
1047
1147
 
1048
 
   pitch_gain *= fact;
 
1148
   pitch_gain = MULT16_16_Q15(fact,pitch_gain) + VERY_SMALL;
1049
1149
 
1050
1150
   /* Shift all buffers by one frame */
1051
 
   speex_move(st->inBuf, st->inBuf+st->frameSize, (st->bufSize-st->frameSize)*sizeof(spx_sig_t));
1052
 
   speex_move(st->excBuf, st->excBuf+st->frameSize, (st->bufSize-st->frameSize)*sizeof(spx_sig_t));
 
1151
   /*speex_move(st->inBuf, st->inBuf+st->frameSize, (st->bufSize-st->frameSize)*sizeof(spx_sig_t));*/
 
1152
   speex_move(st->excBuf, st->excBuf+st->frameSize, (st->max_pitch + 1)*sizeof(spx_sig_t));
1053
1153
 
1054
 
   awk1=PUSH(stack, (st->lpcSize+1), spx_coef_t);
1055
 
   awk2=PUSH(stack, (st->lpcSize+1), spx_coef_t);
1056
 
   awk3=PUSH(stack, (st->lpcSize+1), spx_coef_t);
 
1154
   ALLOC(awk1, (st->lpcSize+1), spx_coef_t);
 
1155
   ALLOC(awk2, (st->lpcSize+1), spx_coef_t);
 
1156
   ALLOC(awk3, (st->lpcSize+1), spx_coef_t);
1057
1157
 
1058
1158
   for (sub=0;sub<st->nbSubframes;sub++)
1059
1159
   {
1089
1189
      /* FIXME: THIS CAN BE IMPROVED */
1090
1190
      /*if (pitch_gain>.95)
1091
1191
        pitch_gain=.95;*/
1092
 
      {
1093
 
         float innov_gain=0;
1094
 
         for (i=0;i<st->frameSize;i++)
1095
 
            innov_gain += 1.0*st->innov[i]*st->innov[i];
1096
 
         innov_gain=sqrt(innov_gain/st->frameSize);
 
1192
      innov_gain = compute_rms(st->innov, st->frameSize);
 
1193
      pitch_val = st->last_pitch + SHR32((spx_int32_t)speex_rand(1+st->count_lost, &st->seed),SIG_SHIFT);
 
1194
      if (pitch_val > st->max_pitch)
 
1195
         pitch_val = st->max_pitch;
 
1196
      if (pitch_val < st->min_pitch)
 
1197
         pitch_val = st->min_pitch;
1097
1198
      for (i=0;i<st->subframeSize;i++)
1098
1199
      {
1099
 
#if 0
1100
 
         exc[i] = pitch_gain * exc[i - st->last_pitch] + fact*sqrt(1-pitch_gain)*st->innov[i+offset];
1101
 
         /*Just so it give the same lost packets as with if 0*/
1102
 
         /*rand();*/
1103
 
#else
1104
 
         /*exc[i]=pitch_gain*exc[i-st->last_pitch] +  fact*st->innov[i+offset];*/
1105
 
         exc[i]=pitch_gain*exc[i-st->last_pitch] + 
1106
 
         fact*sqrt(1-pitch_gain)*speex_rand(innov_gain);
1107
 
#endif
1108
 
      }
1109
 
      }
 
1200
         exc[i]= MULT16_32_Q15(pitch_gain, (exc[i-pitch_val]+VERY_SMALL)) + 
 
1201
               MULT16_32_Q15(fact, MULT16_32_Q15(SHL(Q15ONE,15)-SHL(MULT16_16(pitch_gain,pitch_gain),1),speex_rand(innov_gain, &st->seed)));
 
1202
      }
 
1203
      
1110
1204
      for (i=0;i<st->subframeSize;i++)
1111
1205
         sp[i]=exc[i];
1112
1206
      
1127
1221
 
1128
1222
   for (i=0;i<st->frameSize;i++)
1129
1223
   {
1130
 
      spx_word32_t sig = PSHR(st->frame[i],SIG_SHIFT);
 
1224
      spx_word32_t sig = PSHR32(st->frame[i],SIG_SHIFT);
1131
1225
      if (sig>32767)
1132
1226
         sig = 32767;
1133
1227
      if (sig<-32767)
1137
1231
   
1138
1232
   st->first = 0;
1139
1233
   st->count_lost++;
1140
 
   st->pitch_gain_buf[st->pitch_gain_buf_idx++] = GAIN_SCALING*pitch_gain;
 
1234
   st->pitch_gain_buf[st->pitch_gain_buf_idx++] = PSHR(pitch_gain,9);
1141
1235
   if (st->pitch_gain_buf_idx > 2) /* rollover */
1142
1236
      st->pitch_gain_buf_idx = 0;
1143
1237
}
1156
1250
   int wideband;
1157
1251
   int m;
1158
1252
   char *stack;
1159
 
   spx_coef_t *awk1, *awk2, *awk3;
 
1253
   VARDECL(spx_coef_t *awk1);
 
1254
   VARDECL(spx_coef_t *awk2);
 
1255
   VARDECL(spx_coef_t *awk3);
1160
1256
   spx_word16_t pitch_average=0;
1161
1257
#ifdef EPIC_48K
1162
1258
   int pitch_half[2];
1263
1359
   }
1264
1360
 
1265
1361
   /* Shift all buffers by one frame */
1266
 
   speex_move(st->inBuf, st->inBuf+st->frameSize, (st->bufSize-st->frameSize)*sizeof(spx_sig_t));
1267
 
   speex_move(st->excBuf, st->excBuf+st->frameSize, (st->bufSize-st->frameSize)*sizeof(spx_sig_t));
 
1362
   speex_move(st->excBuf, st->excBuf+st->frameSize, (st->max_pitch + 1)*sizeof(spx_sig_t));
1268
1363
 
1269
1364
   /* If null mode (no transmission), just set a couple things to zero*/
1270
1365
   if (st->submodes[st->submodeID] == NULL)
1271
1366
   {
1272
 
      spx_coef_t *lpc;
1273
 
      lpc = PUSH(stack,11, spx_coef_t);
1274
 
      bw_lpc(GAMMA_SCALING*.93, st->interp_qlpc, lpc, 10);
1275
 
      /*for (i=0;i<st->frameSize;i++)
1276
 
        st->exc[i]=0;*/
 
1367
      VARDECL(spx_coef_t *lpc);
 
1368
      ALLOC(lpc, st->lpcSize, spx_coef_t);
 
1369
      bw_lpc(GAMMA_SCALING*.93, st->interp_qlpc, lpc, st->lpcSize);
1277
1370
      {
1278
1371
         float innov_gain=0;
1279
1372
         float pgain=GAIN_SCALING_1*st->last_pitch_gain;
1280
1373
         if (pgain>.6)
1281
1374
            pgain=.6;
1282
 
         for (i=0;i<st->frameSize;i++)
1283
 
            innov_gain += st->innov[i]*st->innov[i];
1284
 
         innov_gain=sqrt(innov_gain/st->frameSize);
1285
 
         for (i=0;i<st->frameSize;i++)
1286
 
            st->exc[i]=0;
 
1375
         innov_gain = compute_rms(st->innov, st->frameSize);
 
1376
         for (i=0;i<st->frameSize;i++)
 
1377
            st->exc[i]=VERY_SMALL;
1287
1378
         speex_rand_vec(innov_gain, st->exc, st->frameSize);
1288
1379
      }
1289
1380
 
1295
1386
 
1296
1387
      for (i=0;i<st->frameSize;i++)
1297
1388
      {
1298
 
         spx_word32_t sig = PSHR(st->frame[i],SIG_SHIFT);
 
1389
         spx_word32_t sig = PSHR32(st->frame[i],SIG_SHIFT);
1299
1390
         if (sig>32767)
1300
1391
            sig = 32767;
1301
1392
         if (sig<-32767)
1313
1404
   /*Damp memory if a frame was lost and the LSP changed too much*/
1314
1405
   if (st->count_lost)
1315
1406
   {
1316
 
      float lsp_dist=0, fact;
 
1407
      spx_word16_t fact;
 
1408
      spx_word32_t lsp_dist=0;
1317
1409
      for (i=0;i<st->lpcSize;i++)
1318
 
         lsp_dist += fabs(st->old_qlsp[i] - st->qlsp[i]);
1319
 
      lsp_dist /= LSP_SCALING*LSP_SCALING;
 
1410
         lsp_dist = ADD32(lsp_dist, EXTEND32(ABS(st->old_qlsp[i] - st->qlsp[i])));
 
1411
#ifdef FIXED_POINT
 
1412
      fact = SHR16(19661,SHR32(lsp_dist,LSP_SHIFT+2));      
 
1413
#else
1320
1414
      fact = .6*exp(-.2*lsp_dist);
 
1415
#endif
1321
1416
      for (i=0;i<2*st->lpcSize;i++)
1322
 
         st->mem_sp[i] = (spx_mem_t)(st->mem_sp[i]*fact);
 
1417
         st->mem_sp[i] = MULT16_32_Q15(fact,st->mem_sp[i]);
1323
1418
   }
1324
1419
 
1325
1420
 
1374
1469
   }
1375
1470
#endif
1376
1471
 
1377
 
   awk1=PUSH(stack, st->lpcSize+1, spx_coef_t);
1378
 
   awk2=PUSH(stack, st->lpcSize+1, spx_coef_t);
1379
 
   awk3=PUSH(stack, st->lpcSize+1, spx_coef_t);
 
1472
   ALLOC(awk1, st->lpcSize+1, spx_coef_t);
 
1473
   ALLOC(awk2, st->lpcSize+1, spx_coef_t);
 
1474
   ALLOC(awk3, st->lpcSize+1, spx_coef_t);
1380
1475
 
1381
1476
   if (st->submodeID==1)
1382
1477
   {
1436
1531
 
1437
1532
      /* Compute analysis filter at w=pi */
1438
1533
      {
1439
 
         spx_word32_t pi_g=st->interp_qlpc[0];
1440
 
         for (i=1;i<=st->lpcSize;i+=2)
 
1534
         spx_word32_t pi_g=LPC_SCALING;
 
1535
         for (i=0;i<st->lpcSize;i+=2)
1441
1536
         {
1442
 
            pi_g += -st->interp_qlpc[i] +  st->interp_qlpc[i+1];
 
1537
            /*pi_g += -st->interp_qlpc[i] +  st->interp_qlpc[i+1];*/
 
1538
            pi_g = ADD32(pi_g, SUB32(st->interp_qlpc[i+1],st->interp_qlpc[i]));
1443
1539
         }
1444
1540
         st->pi_gain[sub] = pi_g;
1445
1541
      }
1503
1599
         /* If we had lost frames, check energy of last received frame */
1504
1600
         if (st->count_lost && ol_gain < st->last_ol_gain)
1505
1601
         {
1506
 
            float fact = (float)ol_gain/(st->last_ol_gain+1);
1507
 
            for (i=0;i<st->subframeSize;i++)
1508
 
               exc[i]*=fact;
 
1602
            /*float fact = (float)ol_gain/(st->last_ol_gain+1);
 
1603
            for (i=0;i<st->subframeSize;i++)
 
1604
            exc[i]*=fact;*/
 
1605
            spx_word16_t fact = DIV32_16(SHL32(EXTEND32(ol_gain),15),st->last_ol_gain+1);
 
1606
            for (i=0;i<st->subframeSize;i++)
 
1607
               exc[i] = MULT16_32_Q15(fact, exc[i]);
1509
1608
         }
1510
1609
 
1511
1610
         tmp = gain_3tap_to_1tap(pitch_gain);
1589
1688
            }
1590
1689
         } else {
1591
1690
            for (i=0;i<st->subframeSize;i++)
1592
 
               exc[i]+=innov[i];
 
1691
               exc[i]=ADD32(exc[i],innov[i]);
1593
1692
            /*print_vec(exc, 40, "innov");*/
1594
1693
         }
1595
1694
         /* Decode second codebook (only for some modes) */
1596
1695
         if (SUBMODE(double_codebook))
1597
1696
         {
1598
1697
            char *tmp_stack=stack;
1599
 
            spx_sig_t *innov2 = PUSH(tmp_stack, st->subframeSize, spx_sig_t);
 
1698
            VARDECL(spx_sig_t *innov2);
 
1699
            ALLOC(innov2, st->subframeSize, spx_sig_t);
1600
1700
            for (i=0;i<st->subframeSize;i++)
1601
1701
               innov2[i]=0;
1602
 
            SUBMODE(innovation_unquant)(innov2, SUBMODE(innovation_params), st->subframeSize, bits, tmp_stack);
1603
 
            signal_mul(innov2, innov2, ener*(1/2.2), st->subframeSize);
 
1702
            SUBMODE(innovation_unquant)(innov2, SUBMODE(innovation_params), st->subframeSize, bits, stack);
 
1703
            signal_mul(innov2, innov2, (spx_word32_t) (ener*(1/2.2)), st->subframeSize);
1604
1704
            for (i=0;i<st->subframeSize;i++)
1605
 
               exc[i] += innov2[i];
 
1705
               exc[i] = ADD32(exc[i],innov2[i]);
 
1706
            stack = tmp_stack;
1606
1707
         }
1607
1708
 
1608
1709
      }
1609
1710
 
 
1711
      /* If the last packet was lost, re-scale the excitation to obtain the same energy as encoded in ol_gain */
 
1712
      if (st->count_lost) 
 
1713
      {
 
1714
         spx_word16_t exc_ener;
 
1715
         spx_word32_t gain32;
 
1716
         spx_word16_t gain;
 
1717
         exc_ener = compute_rms (exc, st->subframeSize);
 
1718
         gain32 = DIV32(ol_gain, ADD16(exc_ener,1));
 
1719
#ifdef FIXED_POINT
 
1720
         if (gain32 > 32768)
 
1721
            gain32 = 32768;
 
1722
         gain = EXTRACT16(gain32);
 
1723
#else
 
1724
         if (gain32 > 2)
 
1725
            gain32=2;
 
1726
         gain = gain32;
 
1727
#endif
 
1728
         for (i=0;i<st->subframeSize;i++)
 
1729
            exc[i] = MULT16_32_Q14(gain, exc[i]);
 
1730
      }
 
1731
 
1610
1732
      for (i=0;i<st->subframeSize;i++)
1611
1733
         sp[i]=exc[i];
1612
1734
 
1634
1756
   /*Copy output signal*/   
1635
1757
   for (i=0;i<st->frameSize;i++)
1636
1758
   {
1637
 
      spx_word32_t sig = PSHR(st->frame[i],SIG_SHIFT);
 
1759
      spx_word32_t sig = PSHR32(st->frame[i],SIG_SHIFT);
1638
1760
      if (sig>32767)
1639
1761
         sig = 32767;
1640
1762
      if (sig<-32767)
1654
1776
   st->count_lost=0;
1655
1777
   st->last_pitch = best_pitch;
1656
1778
#ifdef FIXED_POINT
1657
 
   st->last_pitch_gain = PSHR(pitch_average,2);
 
1779
   st->last_pitch_gain = PSHR16(pitch_average,2);
1658
1780
#else
1659
1781
   st->last_pitch_gain = .25*pitch_average;   
1660
1782
#endif
1743
1865
            quality = 0;
1744
1866
         if (quality > 10)
1745
1867
            quality = 10;
1746
 
         st->submodeSelect = st->submodeID = ((SpeexNBMode*)(st->mode->mode))->quality_map[quality];
 
1868
         st->submodeSelect = st->submodeID = ((const SpeexNBMode*)(st->mode->mode))->quality_map[quality];
1747
1869
      }
1748
1870
      break;
1749
1871
   case SPEEX_SET_COMPLEXITY:
1750
1872
      st->complexity = (*(int*)ptr);
1751
 
      if (st->complexity<1)
1752
 
         st->complexity=1;
 
1873
      if (st->complexity<0)
 
1874
         st->complexity=0;
1753
1875
      break;
1754
1876
   case SPEEX_GET_COMPLEXITY:
1755
1877
      (*(int*)ptr) = st->complexity;
1789
1911
            st->lsp[i]=(M_PI*((float)(i+1)))/(st->lpcSize+1);
1790
1912
         for (i=0;i<st->lpcSize;i++)
1791
1913
            st->mem_sw[i]=st->mem_sw_whole[i]=st->mem_sp[i]=st->mem_exc[i]=0;
1792
 
         for (i=0;i<st->bufSize;i++)
1793
 
            st->excBuf[i]=st->swBuf[i]=st->inBuf[i]=st->exc2Buf[i]=0;
 
1914
         for (i=0;i<st->frameSize+st->max_pitch+1;i++)
 
1915
            st->excBuf[i]=st->swBuf[i]=0;
 
1916
         for (i=0;i<st->windowSize;i++)
 
1917
            st->inBuf[i]=0;
1794
1918
      }
1795
1919
      break;
1796
1920
   case SPEEX_SET_SUBMODE_ENCODING:
1802
1926
   case SPEEX_GET_LOOKAHEAD:
1803
1927
      (*(int*)ptr)=(st->windowSize-st->frameSize);
1804
1928
      break;
 
1929
   case SPEEX_SET_PLC_TUNING:
 
1930
      st->plc_tuning = (*(int*)ptr);
 
1931
      if (st->plc_tuning>100)
 
1932
         st->plc_tuning=100;
 
1933
      break;
 
1934
   case SPEEX_GET_PLC_TUNING:
 
1935
      (*(int*)ptr)=(st->plc_tuning);
 
1936
      break;
1805
1937
   case SPEEX_GET_PI_GAIN:
1806
1938
      {
1807
1939
         int i;
1892
2024
         int i;
1893
2025
         for (i=0;i<2*st->lpcSize;i++)
1894
2026
            st->mem_sp[i]=0;
1895
 
         for (i=0;i<st->bufSize;i++)
1896
 
            st->excBuf[i]=st->inBuf[i]=0;
 
2027
         for (i=0;i<st->frameSize + st->max_pitch + 1;i++)
 
2028
            st->excBuf[i]=0;
 
2029
         for (i=0;i<st->frameSize;i++)
 
2030
            st->inBuf[i] = 0;
1897
2031
      }
1898
2032
      break;
1899
2033
   case SPEEX_SET_SUBMODE_ENCODING: