Debian Codegraph @

3rdparty/iaxclient/lib/libspeex/ltp.c

raw symbols SCIP
1 /* Copyright (C) 2002 Jean-Marc Valin
2 File: ltp.c
3 Long-Term Prediction functions
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8
9 - Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11
12 - Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
15
16 - Neither the name of the Xiph.org Foundation nor the names of its
17 contributors may be used to endorse or promote products derived from
18 this software without specific prior written permission.
19
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
24 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #ifdef HAVE_CONFIG_H
34 #include "config.h"
35 #endif
36
37 #include <math.h>
38 #include "ltp.h"
39 #include "stack_alloc.h"
40 #include "filters.h"
41 #include <speex/speex_bits.h>
42 #include "math_approx.h"
43
44 #ifndef NULL
45 #define NULL 0
46 #endif
47
48
49 #ifdef _USE_SSE
50 #include "ltp_sse.h"
51 #elif defined (ARM4_ASM) || defined(ARM5E_ASM)
52 #include "ltp_arm4.h"
53 #else
54
55 static spx_word32_t inner_prod(const spx_word16_t *x, const spx_word16_t *y, int len)
56 {
57 spx_word32_t sum=0;
58 len >>= 2;
59 while(len--)
60 {
61 spx_word32_t part=0;
62 part = MAC16_16(part,*x++,*y++);
63 part = MAC16_16(part,*x++,*y++);
64 part = MAC16_16(part,*x++,*y++);
65 part = MAC16_16(part,*x++,*y++);
66 /* HINT: If you had a 40-bit accumulator, you could shift only at the end */
67 sum = ADD32(sum,SHR32(part,6));
68 }
69 return sum;
70 }
71
72 #if 0 /* HINT: Enable this for machines with enough registers (i.e. not x86) */
73 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)
74 {
75 int i,j;
76 for (i=0;i<nb_pitch;i+=4)
77 {
78 /* Compute correlation*/
79 /*corr[nb_pitch-1-i]=inner_prod(x, _y+i, len);*/
80 spx_word32_t sum1=0;
81 spx_word32_t sum2=0;
82 spx_word32_t sum3=0;
83 spx_word32_t sum4=0;
84 const spx_word16_t *y = _y+i;
85 const spx_word16_t *x = _x;
86 spx_word16_t y0, y1, y2, y3;
87 /*y0=y[0];y1=y[1];y2=y[2];y3=y[3];*/
88 y0=*y++;
89 y1=*y++;
90 y2=*y++;
91 y3=*y++;
92 for (j=0;j<len;j+=4)
93 {
94 spx_word32_t part1;
95 spx_word32_t part2;
96 spx_word32_t part3;
97 spx_word32_t part4;
98 part1 = MULT16_16(*x,y0);
99 part2 = MULT16_16(*x,y1);
100 part3 = MULT16_16(*x,y2);
101 part4 = MULT16_16(*x,y3);
102 x++;
103 y0=*y++;
104 part1 = MAC16_16(part1,*x,y1);
105 part2 = MAC16_16(part2,*x,y2);
106 part3 = MAC16_16(part3,*x,y3);
107 part4 = MAC16_16(part4,*x,y0);
108 x++;
109 y1=*y++;
110 part1 = MAC16_16(part1,*x,y2);
111 part2 = MAC16_16(part2,*x,y3);
112 part3 = MAC16_16(part3,*x,y0);
113 part4 = MAC16_16(part4,*x,y1);
114 x++;
115 y2=*y++;
116 part1 = MAC16_16(part1,*x,y3);
117 part2 = MAC16_16(part2,*x,y0);
118 part3 = MAC16_16(part3,*x,y1);
119 part4 = MAC16_16(part4,*x,y2);
120 x++;
121 y3=*y++;
122
123 sum1 = ADD32(sum1,SHR32(part1,6));
124 sum2 = ADD32(sum2,SHR32(part2,6));
125 sum3 = ADD32(sum3,SHR32(part3,6));
126 sum4 = ADD32(sum4,SHR32(part4,6));
127 }
128 corr[nb_pitch-1-i]=sum1;
129 corr[nb_pitch-2-i]=sum2;
130 corr[nb_pitch-3-i]=sum3;
131 corr[nb_pitch-4-i]=sum4;
132 }
133
134 }
135 #else
136 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)
137 {
138 int i;
139 for (i=0;i<nb_pitch;i++)
140 {
141 /* Compute correlation*/
142 corr[nb_pitch-1-i]=inner_prod(_x, _y+i, len);
143 }
144
145 }
146 #endif
147
148
149
150 #endif
151
152 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)
153 {
154 int i,j,k;
155 VARDECL(spx_word32_t *best_score);
156 spx_word32_t e0;
157 VARDECL(spx_word32_t *corr);
158 VARDECL(spx_word32_t *energy);
159 VARDECL(spx_word32_t *score);
160 #ifdef FIXED_POINT
161 VARDECL(spx_word16_t *swn2);
162 #endif
163 spx_word16_t *swn;
164
165 ALLOC(best_score, N, spx_word32_t);
166 ALLOC(corr, end-start+1, spx_word32_t);
167 ALLOC(energy, end-start+2, spx_word32_t);
168 ALLOC(score, end-start+1, spx_word32_t);
169
170 #ifdef FIXED_POINT
171 ALLOC(swn2, end+len, spx_word16_t);
172 normalize16(sw-end, swn2, 16384, end+len);
173 swn = swn2 + end;
174 #else
175 swn = sw;
176 #endif
177
178 for (i=0;i<N;i++)
179 {
180 best_score[i]=-1;
181 pitch[i]=start;
182 }
183
184
185 energy[0]=inner_prod(swn-start, swn-start, len);
186 e0=inner_prod(swn, swn, len);
187 for (i=start;i<=end;i++)
188 {
189 /* Update energy for next pitch*/
190 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));
191 }
192
193 pitch_xcorr(swn, swn-end, corr, len, end-start+1, stack);
194
195 #ifdef FIXED_POINT
196 {
197 VARDECL(spx_word16_t *corr16);
198 VARDECL(spx_word16_t *ener16);
199 ALLOC(corr16, end-start+1, spx_word16_t);
200 ALLOC(ener16, end-start+1, spx_word16_t);
201 normalize16(corr, corr16, 16384, end-start+1);
202 normalize16(energy, ener16, 16384, end-start+1);
203
204 for (i=start;i<=end;i++)
205 {
206 spx_word16_t g;
207 spx_word32_t tmp;
208 tmp = corr16[i-start];
209 if (tmp>0)
210 {
211 if (SHR16(corr16[i-start],4)>ener16[i-start])
212 tmp = SHL32(EXTEND32(ener16[i-start]),14);
213 else if (-SHR16(corr16[i-start],4)>ener16[i-start])
214 tmp = -SHL32(EXTEND32(ener16[i-start]),14);
215 else
216 tmp = SHL32(tmp,10);
217 g = DIV32_16(tmp, 8+ener16[i-start]);
218 score[i-start] = MULT16_16(corr16[i-start],g);
219 } else
220 {
221 score[i-start] = 1;
222 }
223 }
224 }
225 #else
226 for (i=start;i<=end;i++)
227 {
228 float g = corr[i-start]/(1+energy[i-start]);
229 if (g>16)
230 g = 16;
231 else if (g<-16)
232 g = -16;
233 score[i-start] = g*corr[i-start];
234 }
235 #endif
236
237 /* Extract best scores */
238 for (i=start;i<=end;i++)
239 {
240 if (score[i-start]>best_score[N-1])
241 {
242 for (j=0;j<N;j++)
243 {
244 if (score[i-start] > best_score[j])
245 {
246 for (k=N-1;k>j;k--)
247 {
248 best_score[k]=best_score[k-1];
249 pitch[k]=pitch[k-1];
250 }
251 best_score[j]=score[i-start];
252 pitch[j]=i;
253 break;
254 }
255 }
256 }
257 }
258
259 /* Compute open-loop gain */
260 if (gain)
261 {
262 for (j=0;j<N;j++)
263 {
264 spx_word16_t g;
265 i=pitch[j];
266 g = DIV32(corr[i-start], 10+SHR32(MULT16_16(spx_sqrt(e0),spx_sqrt(energy[i-start])),6));
267 /* FIXME: g = max(g,corr/energy) */
268 if (g<0)
269 g = 0;
270 gain[j]=g;
271 }
272 }
273 }
274
275
276 /** Finds the best quantized 3-tap pitch predictor by analysis by synthesis */
277 static spx_word64_t pitch_gain_search_3tap(
278 const spx_sig_t target[], /* Target vector */
279 const spx_coef_t ak[], /* LPCs for this subframe */
280 const spx_coef_t awk1[], /* Weighted LPCs #1 for this subframe */
281 const spx_coef_t awk2[], /* Weighted LPCs #2 for this subframe */
282 spx_sig_t exc[], /* Excitation */
283 const void *par,
284 int pitch, /* Pitch value */
285 int p, /* Number of LPC coeffs */
286 int nsf, /* Number of samples in subframe */
287 SpeexBits *bits,
288 char *stack,
289 const spx_sig_t *exc2,
290 const spx_word16_t *r,
291 spx_sig_t *new_target,
292 int *cdbk_index,
293 int cdbk_offset,
294 int plc_tuning
295 )
296 {
297 int i,j;
298 VARDECL(spx_sig_t *tmp1);
299 VARDECL(spx_sig_t *tmp2);
300 spx_sig_t *x[3];
301 spx_sig_t *e[3];
302 spx_word32_t corr[3];
303 spx_word32_t A[3][3];
304 int gain_cdbk_size;
305 const signed char *gain_cdbk;
306 spx_word16_t gain[3];
307 spx_word64_t err;
308
309 const ltp_params *params;
310 params = (const ltp_params*) par;
311 gain_cdbk_size = 1<<params->gain_bits;
312 gain_cdbk = params->gain_cdbk + 3*gain_cdbk_size*cdbk_offset;
313 ALLOC(tmp1, 3*nsf, spx_sig_t);
314 ALLOC(tmp2, 3*nsf, spx_sig_t);
315
316 x[0]=tmp1;
317 x[1]=tmp1+nsf;
318 x[2]=tmp1+2*nsf;
319
320 e[0]=tmp2;
321 e[1]=tmp2+nsf;
322 e[2]=tmp2+2*nsf;
323 for (i=2;i>=0;i--)
324 {
325 int pp=pitch+1-i;
326 for (j=0;j<nsf;j++)
327 {
328 if (j-pp<0)
329 e[i][j]=exc2[j-pp];
330 else if (j-pp-pitch<0)
331 e[i][j]=exc2[j-pp-pitch];
332 else
333 e[i][j]=0;
334 }
335
336 if (i==2)
337 syn_percep_zero(e[i], ak, awk1, awk2, x[i], nsf, p, stack);
338 else {
339 for (j=0;j<nsf-1;j++)
340 x[i][j+1]=x[i+1][j];
341 x[i][0]=0;
342 for (j=0;j<nsf;j++)
343 {
344 x[i][j]=ADD32(x[i][j],SHL32(MULT16_32_Q15(r[j], e[i][0]),1));
345 }
346 }
347 }
348
349 #ifdef FIXED_POINT
350 {
351 /* If using fixed-point, we need to normalize the signals first */
352 spx_word16_t *y[3];
353 VARDECL(spx_word16_t *ytmp);
354 VARDECL(spx_word16_t *t);
355
356 spx_sig_t max_val=1;
357 int sig_shift;
358
359 ALLOC(ytmp, 3*nsf, spx_word16_t);
360 #if 0
361 ALLOC(y[0], nsf, spx_word16_t);
362 ALLOC(y[1], nsf, spx_word16_t);
363 ALLOC(y[2], nsf, spx_word16_t);
364 #else
365 y[0] = ytmp;
366 y[1] = ytmp+nsf;
367 y[2] = ytmp+2*nsf;
368 #endif
369 ALLOC(t, nsf, spx_word16_t);
370 for (j=0;j<3;j++)
371 {
372 for (i=0;i<nsf;i++)
373 {
374 spx_sig_t tmp = x[j][i];
375 if (tmp<0)
376 tmp = -tmp;
377 if (tmp > max_val)
378 max_val = tmp;
379 }
380 }
381 for (i=0;i<nsf;i++)
382 {
383 spx_sig_t tmp = target[i];
384 if (tmp<0)
385 tmp = -tmp;
386 if (tmp > max_val)
387 max_val = tmp;
388 }
389
390 sig_shift=0;
391 while (max_val>16384)
392 {
393 sig_shift++;
394 max_val >>= 1;
395 }
396
397 for (j=0;j<3;j++)
398 {
399 for (i=0;i<nsf;i++)
400 {
401 y[j][i] = EXTRACT16(SHR32(x[j][i],sig_shift));
402 }
403 }
404 for (i=0;i<nsf;i++)
405 {
406 t[i] = EXTRACT16(SHR32(target[i],sig_shift));
407 }
408
409 for (i=0;i<3;i++)
410 corr[i]=inner_prod(y[i],t,nsf);
411
412 for (i=0;i<3;i++)
413 for (j=0;j<=i;j++)
414 A[i][j]=A[j][i]=inner_prod(y[i],y[j],nsf);
415 }
416 #else
417 {
418 for (i=0;i<3;i++)
419 corr[i]=inner_prod(x[i],target,nsf);
420
421 for (i=0;i<3;i++)
422 for (j=0;j<=i;j++)
423 A[i][j]=A[j][i]=inner_prod(x[i],x[j],nsf);
424 }
425 #endif
426
427 {
428 spx_word32_t C[9];
429 const signed char *ptr=gain_cdbk;
430 int best_cdbk=0;
431 spx_word32_t best_sum=0;
432 C[0]=corr[2];
433 C[1]=corr[1];
434 C[2]=corr[0];
435 C[3]=A[1][2];
436 C[4]=A[0][1];
437 C[5]=A[0][2];
438 C[6]=A[2][2];
439 C[7]=A[1][1];
440 C[8]=A[0][0];
441
442 /*plc_tuning *= 2;*/
443 if (plc_tuning<2)
444 plc_tuning=2;
445 #ifdef FIXED_POINT
446 C[0] = MAC16_32_Q15(C[0],MULT16_16_16(plc_tuning,-327),C[0]);
447 C[1] = MAC16_32_Q15(C[1],MULT16_16_16(plc_tuning,-327),C[1]);
448 C[2] = MAC16_32_Q15(C[2],MULT16_16_16(plc_tuning,-327),C[2]);
449 #else
450 C[0]*=1-.01*plc_tuning;
451 C[1]*=1-.01*plc_tuning;
452 C[2]*=1-.01*plc_tuning;
453 C[6]*=.5*(1+.01*plc_tuning);
454 C[7]*=.5*(1+.01*plc_tuning);
455 C[8]*=.5*(1+.01*plc_tuning);
456 #endif
457 for (i=0;i<gain_cdbk_size;i++)
458 {
459 spx_word32_t sum=0;
460 spx_word16_t g0,g1,g2;
461 spx_word16_t pitch_control=64;
462 spx_word16_t gain_sum;
463
464 ptr = gain_cdbk+3*i;
465 g0=ADD16((spx_word16_t)ptr[0],32);
466 g1=ADD16((spx_word16_t)ptr[1],32);
467 g2=ADD16((spx_word16_t)ptr[2],32);
468
469 gain_sum = g1;
470 if (g0>0)
471 gain_sum += g0;
472 if (g2>0)
473 gain_sum += g2;
474 if (gain_sum > 64)
475 {
476 gain_sum = SUB16(gain_sum, 64);
477 if (gain_sum > 127)
478 gain_sum = 127;
479 #ifdef FIXED_POINT
480 pitch_control = SUB16(64,EXTRACT16(PSHR32(MULT16_16(64,MULT16_16_16(plc_tuning, gain_sum)),10)));
481 #else
482 pitch_control = 64*(1.-.001*plc_tuning*gain_sum);
483 #endif
484 if (pitch_control < 0)
485 pitch_control = 0;
486 }
487
488 sum = ADD32(sum,MULT16_32_Q14(MULT16_16_16(g0,pitch_control),C[0]));
489 sum = ADD32(sum,MULT16_32_Q14(MULT16_16_16(g1,pitch_control),C[1]));
490 sum = ADD32(sum,MULT16_32_Q14(MULT16_16_16(g2,pitch_control),C[2]));
491 sum = SUB32(sum,MULT16_32_Q14(MULT16_16_16(g0,g1),C[3]));
492 sum = SUB32(sum,MULT16_32_Q14(MULT16_16_16(g2,g1),C[4]));
493 sum = SUB32(sum,MULT16_32_Q14(MULT16_16_16(g2,g0),C[5]));
494 sum = SUB32(sum,MULT16_32_Q15(MULT16_16_16(g0,g0),C[6]));
495 sum = SUB32(sum,MULT16_32_Q15(MULT16_16_16(g1,g1),C[7]));
496 sum = SUB32(sum,MULT16_32_Q15(MULT16_16_16(g2,g2),C[8]));
497 /* We could force "safe" pitch values to handle packet loss better */
498
499 if (sum>best_sum || i==0)
500 {
501 best_sum=sum;
502 best_cdbk=i;
503 }
504 }
505 #ifdef FIXED_POINT
506 gain[0] = ADD16(32,(spx_word16_t)gain_cdbk[best_cdbk*3]);
507 gain[1] = ADD16(32,(spx_word16_t)gain_cdbk[best_cdbk*3+1]);
508 gain[2] = ADD16(32,(spx_word16_t)gain_cdbk[best_cdbk*3+2]);
509 /*printf ("%d %d %d %d\n",gain[0],gain[1],gain[2], best_cdbk);*/
510 #else
511 gain[0] = 0.015625*gain_cdbk[best_cdbk*3] + .5;
512 gain[1] = 0.015625*gain_cdbk[best_cdbk*3+1]+ .5;
513 gain[2] = 0.015625*gain_cdbk[best_cdbk*3+2]+ .5;
514 #endif
515 *cdbk_index=best_cdbk;
516 }
517
518 #ifdef FIXED_POINT
519 for (i=0;i<nsf;i++)
520 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])),
521 MULT16_32_Q15(SHL16(gain[2],7),e[0][i])), 2);
522
523 err=0;
524 for (i=0;i<nsf;i++)
525 {
526 spx_word16_t perr2;
527 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])),
528 MULT16_32_Q15(SHL16(gain[2],7),x[0][i])),2);
529 spx_sig_t perr=SUB32(target[i],tmp);
530 new_target[i] = SUB32(target[i], tmp);
531 perr2 = EXTRACT16(PSHR32(perr,15));
532 err = ADD64(err,MULT16_16(perr2,perr2));
533
534 }
535 #else
536 for (i=0;i<nsf;i++)
537 exc[i]=gain[0]*e[2][i]+gain[1]*e[1][i]+gain[2]*e[0][i];
538
539 err=0;
540 for (i=0;i<nsf;i++)
541 {
542 spx_sig_t tmp = gain[2]*x[0][i]+gain[1]*x[1][i]+gain[0]*x[2][i];
543 new_target[i] = target[i] - tmp;
544 err+=new_target[i]*new_target[i];
545 }
546 #endif
547
548 return err;
549 }
550
551
552 /** Finds the best quantized 3-tap pitch predictor by analysis by synthesis */
553 int pitch_search_3tap(
554 spx_sig_t target[], /* Target vector */
555 spx_sig_t *sw,
556 spx_coef_t ak[], /* LPCs for this subframe */
557 spx_coef_t awk1[], /* Weighted LPCs #1 for this subframe */
558 spx_coef_t awk2[], /* Weighted LPCs #2 for this subframe */
559 spx_sig_t exc[], /* Excitation */
560 const void *par,
561 int start, /* Smallest pitch value allowed */
562 int end, /* Largest pitch value allowed */
563 spx_word16_t pitch_coef, /* Voicing (pitch) coefficient */
564 int p, /* Number of LPC coeffs */
565 int nsf, /* Number of samples in subframe */
566 SpeexBits *bits,
567 char *stack,
568 spx_sig_t *exc2,
569 spx_word16_t *r,
570 int complexity,
571 int cdbk_offset,
572 int plc_tuning
573 )
574 {
575 int i,j;
576 int cdbk_index, pitch=0, best_gain_index=0;
577 VARDECL(spx_sig_t *best_exc);
578 VARDECL(spx_sig_t *new_target);
579 VARDECL(spx_sig_t *best_target);
580 int best_pitch=0;
581 spx_word64_t err, best_err=-1;
582 int N;
583 const ltp_params *params;
584 VARDECL(int *nbest);
585
586 N=complexity;
587 if (N>10)
588 N=10;
589 if (N<1)
590 N=1;
591
592 ALLOC(nbest, N, int);
593 params = (const ltp_params*) par;
594
595 if (end<start)
596 {
597 speex_bits_pack(bits, 0, params->pitch_bits);
598 speex_bits_pack(bits, 0, params->gain_bits);
599 for (i=0;i<nsf;i++)
600 exc[i]=0;
601 return start;
602 }
603
604 ALLOC(best_exc, nsf, spx_sig_t);
605 ALLOC(new_target, nsf, spx_sig_t);
606 ALLOC(best_target, nsf, spx_sig_t);
607
608 if (N>end-start+1)
609 N=end-start+1;
610 open_loop_nbest_pitch(sw, start, end, nsf, nbest, NULL, N, stack);
611 for (i=0;i<N;i++)
612 {
613 pitch=nbest[i];
614 for (j=0;j<nsf;j++)
615 exc[j]=0;
616 err=pitch_gain_search_3tap(target, ak, awk1, awk2, exc, par, pitch, p, nsf,
617 bits, stack, exc2, r, new_target, &cdbk_index, cdbk_offset, plc_tuning);
618 if (err<best_err || best_err<0)
619 {
620 for (j=0;j<nsf;j++)
621 best_exc[j]=exc[j];
622 for (j=0;j<nsf;j++)
623 best_target[j]=new_target[j];
624 best_err=err;
625 best_pitch=pitch;
626 best_gain_index=cdbk_index;
627 }
628 }
629
630 /*printf ("pitch: %d %d\n", best_pitch, best_gain_index);*/
631 speex_bits_pack(bits, best_pitch-start, params->pitch_bits);
632 speex_bits_pack(bits, best_gain_index, params->gain_bits);
633 /*printf ("encode pitch: %d %d\n", best_pitch, best_gain_index);*/
634 for (i=0;i<nsf;i++)
635 exc[i]=best_exc[i];
636 for (i=0;i<nsf;i++)
637 target[i]=best_target[i];
638
639 return pitch;
640 }
641
642 void pitch_unquant_3tap(
643 spx_sig_t exc[], /* Excitation */
644 int start, /* Smallest pitch value allowed */
645 int end, /* Largest pitch value allowed */
646 spx_word16_t pitch_coef, /* Voicing (pitch) coefficient */
647 const void *par,
648 int nsf, /* Number of samples in subframe */
649 int *pitch_val,
650 spx_word16_t *gain_val,
651 SpeexBits *bits,
652 char *stack,
653 int count_lost,
654 int subframe_offset,
655 spx_word16_t last_pitch_gain,
656 int cdbk_offset
657 )
658 {
659 int i;
660 int pitch;
661 int gain_index;
662 spx_word16_t gain[3];
663 const signed char *gain_cdbk;
664 int gain_cdbk_size;
665 const ltp_params *params;
666
667 params = (const ltp_params*) par;
668 gain_cdbk_size = 1<<params->gain_bits;
669 gain_cdbk = params->gain_cdbk + 3*gain_cdbk_size*cdbk_offset;
670
671 pitch = speex_bits_unpack_unsigned(bits, params->pitch_bits);
672 pitch += start;
673 gain_index = speex_bits_unpack_unsigned(bits, params->gain_bits);
674 /*printf ("decode pitch: %d %d\n", pitch, gain_index);*/
675 #ifdef FIXED_POINT
676 gain[0] = 32+(spx_word16_t)gain_cdbk[gain_index*3];
677 gain[1] = 32+(spx_word16_t)gain_cdbk[gain_index*3+1];
678 gain[2] = 32+(spx_word16_t)gain_cdbk[gain_index*3+2];
679 #else
680 gain[0] = 0.015625*gain_cdbk[gain_index*3]+.5;
681 gain[1] = 0.015625*gain_cdbk[gain_index*3+1]+.5;
682 gain[2] = 0.015625*gain_cdbk[gain_index*3+2]+.5;
683 #endif
684
685 if (count_lost && pitch > subframe_offset)
686 {
687 float gain_sum;
688 if (1) {
689 float tmp = count_lost < 4 ? GAIN_SCALING_1*last_pitch_gain : 0.4 * GAIN_SCALING_1 * last_pitch_gain;
690 if (tmp>.95)
691 tmp=.95;
692 gain_sum = GAIN_SCALING_1*gain_3tap_to_1tap(gain);
693
694 if (gain_sum > tmp) {
695 float fact = tmp/gain_sum;
696 for (i=0;i<3;i++)
697 gain[i]*=fact;
698
699 }
700
701 }
702
703 }
704
705 *pitch_val = pitch;
706 gain_val[0]=gain[0];
707 gain_val[1]=gain[1];
708 gain_val[2]=gain[2];
709
710 {
711 spx_sig_t *e[3];
712 VARDECL(spx_sig_t *tmp2);
713 ALLOC(tmp2, 3*nsf, spx_sig_t);
714 e[0]=tmp2;
715 e[1]=tmp2+nsf;
716 e[2]=tmp2+2*nsf;
717
718 for (i=0;i<3;i++)
719 {
720 int j;
721 int pp=pitch+1-i;
722 #if 0
723 for (j=0;j<nsf;j++)
724 {
725 if (j-pp<0)
726 e[i][j]=exc[j-pp];
727 else if (j-pp-pitch<0)
728 e[i][j]=exc[j-pp-pitch];
729 else
730 e[i][j]=0;
731 }
732 #else
733 {
734 int tmp1, tmp3;
735 tmp1=nsf;
736 if (tmp1>pp)
737 tmp1=pp;
738 for (j=0;j<tmp1;j++)
739 e[i][j]=exc[j-pp];
740 tmp3=nsf;
741 if (tmp3>pp+pitch)
742 tmp3=pp+pitch;
743 for (j=tmp1;j<tmp3;j++)
744 e[i][j]=exc[j-pp-pitch];
745 for (j=tmp3;j<nsf;j++)
746 e[i][j]=0;
747 }
748 #endif
749 }
750
751 #ifdef FIXED_POINT
752 {
753 for (i=0;i<nsf;i++)
754 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])),
755 MULT16_32_Q15(SHL16(gain[2],7),e[0][i])), 2);
756 }
757 #else
758 for (i=0;i<nsf;i++)
759 exc[i]=VERY_SMALL+gain[0]*e[2][i]+gain[1]*e[1][i]+gain[2]*e[0][i];
760 #endif
761 }
762 }
763
764
765 /** Forced pitch delay and gain */
766 int forced_pitch_quant(
767 spx_sig_t target[], /* Target vector */
768 spx_sig_t *sw,
769 spx_coef_t ak[], /* LPCs for this subframe */
770 spx_coef_t awk1[], /* Weighted LPCs #1 for this subframe */
771 spx_coef_t awk2[], /* Weighted LPCs #2 for this subframe */
772 spx_sig_t exc[], /* Excitation */
773 const void *par,
774 int start, /* Smallest pitch value allowed */
775 int end, /* Largest pitch value allowed */
776 spx_word16_t pitch_coef, /* Voicing (pitch) coefficient */
777 int p, /* Number of LPC coeffs */
778 int nsf, /* Number of samples in subframe */
779 SpeexBits *bits,
780 char *stack,
781 spx_sig_t *exc2,
782 spx_word16_t *r,
783 int complexity,
784 int cdbk_offset,
785 int plc_tuning
786 )
787 {
788 int i;
789 float coef = GAIN_SCALING_1*pitch_coef;
790 if (coef>.99)
791 coef=.99;
792 for (i=0;i<nsf;i++)
793 {
794 exc[i]=exc[i-start]*coef;
795 }
796 return start;
797 }
798
799 /** Unquantize forced pitch delay and gain */
800 void forced_pitch_unquant(
801 spx_sig_t exc[], /* Excitation */
802 int start, /* Smallest pitch value allowed */
803 int end, /* Largest pitch value allowed */
804 spx_word16_t pitch_coef, /* Voicing (pitch) coefficient */
805 const void *par,
806 int nsf, /* Number of samples in subframe */
807 int *pitch_val,
808 spx_word16_t *gain_val,
809 SpeexBits *bits,
810 char *stack,
811 int count_lost,
812 int subframe_offset,
813 spx_word16_t last_pitch_gain,
814 int cdbk_offset
815 )
816 {
817 int i;
818 float coef = GAIN_SCALING_1*pitch_coef;
819 if (coef>.99)
820 coef=.99;
821 for (i=0;i<nsf;i++)
822 {
823 exc[i]=exc[i-start]*coef;
824 }
825 *pitch_val = start;
826 gain_val[0]=gain_val[2]=0;
827 gain_val[1] = pitch_coef;
828 }