blob: 91bf8a957e9835ebe7ec53589bc208ada5ec6666 [file] [log] [blame]
Tristan Matthews0a329cc2013-07-17 13:20:14 -04001/*
2 * Copyright 1992 by Jutta Degener and Carsten Bormann, Technische
3 * Universitaet Berlin. See the accompanying file "COPYRIGHT" for
4 * details. THERE IS ABSOLUTELY NO WARRANTY FOR THIS SOFTWARE.
5 */
6
7/* $Header: /tmp_amd/presto/export/kbs/jutta/src/gsm/RCS/long_term.c,v 1.6 1996/07/02 12:33:19 jutta Exp $ */
8
9#include "config.h"
10#include <stdio.h>
11#include <assert.h>
12
13#include "private.h"
14
15#include "gsm.h"
16#include "proto.h"
17
18/*
19 * 4.2.11 .. 4.2.12 LONG TERM PREDICTOR (LTP) SECTION
20 */
21
22
23/*
24 * This module computes the LTP gain (bc) and the LTP lag (Nc)
25 * for the long term analysis filter. This is done by calculating a
26 * maximum of the cross-correlation function between the current
27 * sub-segment short term residual signal d[0..39] (output of
28 * the short term analysis filter; for simplification the index
29 * of this array begins at 0 and ends at 39 for each sub-segment of the
30 * RPE-LTP analysis) and the previous reconstructed short term
31 * residual signal dp[ -120 .. -1 ]. A dynamic scaling must be
32 * performed to avoid overflow.
33 */
34
35 /* The next procedure exists in six versions. First two integer
36 * version (if USE_FLOAT_MUL is not defined); then four floating
37 * point versions, twice with proper scaling (USE_FLOAT_MUL defined),
38 * once without (USE_FLOAT_MUL and FAST defined, and fast run-time
39 * option used). Every pair has first a Cut version (see the -C
40 * option to toast or the LTP_CUT option to gsm_option()), then the
41 * uncut one. (For a detailed explanation of why this is altogether
42 * a bad idea, see Henry Spencer and Geoff Collyer, ``#ifdef Considered
43 * Harmful''.)
44 */
45
46#ifndef USE_FLOAT_MUL
47
48#ifdef LTP_CUT
49
50static void Cut_Calculation_of_the_LTP_parameters P5((st, d,dp,bc_out,Nc_out),
51
52 struct gsm_state * st,
53
54 register word * d, /* [0..39] IN */
55 register word * dp, /* [-120..-1] IN */
56 word * bc_out, /* OUT */
57 word * Nc_out /* OUT */
58)
59{
60 register int k, lambda;
61 word Nc, bc;
62 word wt[40];
63
64 longword L_result;
65 longword L_max, L_power;
66 word R, S, dmax, scal, best_k;
67 word ltp_cut;
68
69 register word temp, wt_k;
70
71 /* Search of the optimum scaling of d[0..39].
72 */
73 dmax = 0;
74 for (k = 0; k <= 39; k++) {
75 temp = d[k];
76 temp = GSM_ABS( temp );
77 if (temp > dmax) {
78 dmax = temp;
79 best_k = k;
80 }
81 }
82 temp = 0;
83 if (dmax == 0) scal = 0;
84 else {
85 assert(dmax > 0);
86 temp = gsm_norm( (longword)dmax << 16 );
87 }
88 if (temp > 6) scal = 0;
89 else scal = 6 - temp;
90 assert(scal >= 0);
91
92 /* Search for the maximum cross-correlation and coding of the LTP lag
93 */
94 L_max = 0;
95 Nc = 40; /* index for the maximum cross-correlation */
96 wt_k = SASR(d[best_k], scal);
97
98 for (lambda = 40; lambda <= 120; lambda++) {
99 L_result = (longword)wt_k * dp[best_k - lambda];
100 if (L_result > L_max) {
101 Nc = lambda;
102 L_max = L_result;
103 }
104 }
105 *Nc_out = Nc;
106 L_max <<= 1;
107
108 /* Rescaling of L_max
109 */
110 assert(scal <= 100 && scal >= -100);
111 L_max = L_max >> (6 - scal); /* sub(6, scal) */
112
113 assert( Nc <= 120 && Nc >= 40);
114
115 /* Compute the power of the reconstructed short term residual
116 * signal dp[..]
117 */
118 L_power = 0;
119 for (k = 0; k <= 39; k++) {
120
121 register longword L_temp;
122
123 L_temp = SASR( dp[k - Nc], 3 );
124 L_power += L_temp * L_temp;
125 }
126 L_power <<= 1; /* from L_MULT */
127
128 /* Normalization of L_max and L_power
129 */
130
131 if (L_max <= 0) {
132 *bc_out = 0;
133 return;
134 }
135 if (L_max >= L_power) {
136 *bc_out = 3;
137 return;
138 }
139
140 temp = gsm_norm( L_power );
141
142 R = SASR( L_max << temp, 16 );
143 S = SASR( L_power << temp, 16 );
144
145 /* Coding of the LTP gain
146 */
147
148 /* Table 4.3a must be used to obtain the level DLB[i] for the
149 * quantization of the LTP gain b to get the coded version bc.
150 */
151 for (bc = 0; bc <= 2; bc++) if (R <= gsm_mult(S, gsm_DLB[bc])) break;
152 *bc_out = bc;
153}
154
155#endif /* LTP_CUT */
156
157static void Calculation_of_the_LTP_parameters P4((d,dp,bc_out,Nc_out),
158 register word * d, /* [0..39] IN */
159 register word * dp, /* [-120..-1] IN */
160 word * bc_out, /* OUT */
161 word * Nc_out /* OUT */
162)
163{
164 register int k, lambda;
165 word Nc, bc;
166 word wt[40];
167
168 longword L_max, L_power;
169 word R, S, dmax, scal;
170 register word temp;
171
172 /* Search of the optimum scaling of d[0..39].
173 */
174 dmax = 0;
175
176 for (k = 0; k <= 39; k++) {
177 temp = d[k];
178 temp = GSM_ABS( temp );
179 if (temp > dmax) dmax = temp;
180 }
181
182 temp = 0;
183 if (dmax == 0) scal = 0;
184 else {
185 assert(dmax > 0);
186 temp = gsm_norm( (longword)dmax << 16 );
187 }
188
189 if (temp > 6) scal = 0;
190 else scal = 6 - temp;
191
192 assert(scal >= 0);
193
194 /* Initialization of a working array wt
195 */
196
197 for (k = 0; k <= 39; k++) wt[k] = SASR( d[k], scal );
198
199 /* Search for the maximum cross-correlation and coding of the LTP lag
200 */
201 L_max = 0;
202 Nc = 40; /* index for the maximum cross-correlation */
203
204 for (lambda = 40; lambda <= 120; lambda++) {
205
206# undef STEP
207# define STEP(k) (longword)wt[k] * dp[k - lambda]
208
209 register longword L_result;
210
211 L_result = STEP(0) ; L_result += STEP(1) ;
212 L_result += STEP(2) ; L_result += STEP(3) ;
213 L_result += STEP(4) ; L_result += STEP(5) ;
214 L_result += STEP(6) ; L_result += STEP(7) ;
215 L_result += STEP(8) ; L_result += STEP(9) ;
216 L_result += STEP(10) ; L_result += STEP(11) ;
217 L_result += STEP(12) ; L_result += STEP(13) ;
218 L_result += STEP(14) ; L_result += STEP(15) ;
219 L_result += STEP(16) ; L_result += STEP(17) ;
220 L_result += STEP(18) ; L_result += STEP(19) ;
221 L_result += STEP(20) ; L_result += STEP(21) ;
222 L_result += STEP(22) ; L_result += STEP(23) ;
223 L_result += STEP(24) ; L_result += STEP(25) ;
224 L_result += STEP(26) ; L_result += STEP(27) ;
225 L_result += STEP(28) ; L_result += STEP(29) ;
226 L_result += STEP(30) ; L_result += STEP(31) ;
227 L_result += STEP(32) ; L_result += STEP(33) ;
228 L_result += STEP(34) ; L_result += STEP(35) ;
229 L_result += STEP(36) ; L_result += STEP(37) ;
230 L_result += STEP(38) ; L_result += STEP(39) ;
231
232 if (L_result > L_max) {
233
234 Nc = lambda;
235 L_max = L_result;
236 }
237 }
238
239 *Nc_out = Nc;
240
241 L_max <<= 1;
242
243 /* Rescaling of L_max
244 */
245 assert(scal <= 100 && scal >= -100);
246 L_max = L_max >> (6 - scal); /* sub(6, scal) */
247
248 assert( Nc <= 120 && Nc >= 40);
249
250 /* Compute the power of the reconstructed short term residual
251 * signal dp[..]
252 */
253 L_power = 0;
254 for (k = 0; k <= 39; k++) {
255
256 register longword L_temp;
257
258 L_temp = SASR( dp[k - Nc], 3 );
259 L_power += L_temp * L_temp;
260 }
261 L_power <<= 1; /* from L_MULT */
262
263 /* Normalization of L_max and L_power
264 */
265
266 if (L_max <= 0) {
267 *bc_out = 0;
268 return;
269 }
270 if (L_max >= L_power) {
271 *bc_out = 3;
272 return;
273 }
274
275 temp = gsm_norm( L_power );
276
277 R = SASR( L_max << temp, 16 );
278 S = SASR( L_power << temp, 16 );
279
280 /* Coding of the LTP gain
281 */
282
283 /* Table 4.3a must be used to obtain the level DLB[i] for the
284 * quantization of the LTP gain b to get the coded version bc.
285 */
286 for (bc = 0; bc <= 2; bc++) if (R <= gsm_mult(S, gsm_DLB[bc])) break;
287 *bc_out = bc;
288}
289
290#else /* USE_FLOAT_MUL */
291
292#ifdef LTP_CUT
293
294static void Cut_Calculation_of_the_LTP_parameters P5((st, d,dp,bc_out,Nc_out),
295 struct gsm_state * st, /* IN */
296 register word * d, /* [0..39] IN */
297 register word * dp, /* [-120..-1] IN */
298 word * bc_out, /* OUT */
299 word * Nc_out /* OUT */
300)
301{
302 register int k, lambda;
303 word Nc, bc;
304 word ltp_cut;
305
306 float wt_float[40];
307 float dp_float_base[120], * dp_float = dp_float_base + 120;
308
309 longword L_max, L_power;
310 word R, S, dmax, scal;
311 register word temp;
312
313 /* Search of the optimum scaling of d[0..39].
314 */
315 dmax = 0;
316
317 for (k = 0; k <= 39; k++) {
318 temp = d[k];
319 temp = GSM_ABS( temp );
320 if (temp > dmax) dmax = temp;
321 }
322
323 temp = 0;
324 if (dmax == 0) scal = 0;
325 else {
326 assert(dmax > 0);
327 temp = gsm_norm( (longword)dmax << 16 );
328 }
329
330 if (temp > 6) scal = 0;
331 else scal = 6 - temp;
332
333 assert(scal >= 0);
334 ltp_cut = (longword)SASR(dmax, scal) * st->ltp_cut / 100;
335
336
337 /* Initialization of a working array wt
338 */
339
340 for (k = 0; k < 40; k++) {
341 register word w = SASR( d[k], scal );
342 if (w < 0 ? w > -ltp_cut : w < ltp_cut) {
343 wt_float[k] = 0.0;
344 }
345 else {
346 wt_float[k] = w;
347 }
348 }
349 for (k = -120; k < 0; k++) dp_float[k] = dp[k];
350
351 /* Search for the maximum cross-correlation and coding of the LTP lag
352 */
353 L_max = 0;
354 Nc = 40; /* index for the maximum cross-correlation */
355
356 for (lambda = 40; lambda <= 120; lambda += 9) {
357
358 /* Calculate L_result for l = lambda .. lambda + 9.
359 */
360 register float *lp = dp_float - lambda;
361
362 register float W;
363 register float a = lp[-8], b = lp[-7], c = lp[-6],
364 d = lp[-5], e = lp[-4], f = lp[-3],
365 g = lp[-2], h = lp[-1];
366 register float E;
367 register float S0 = 0, S1 = 0, S2 = 0, S3 = 0, S4 = 0,
368 S5 = 0, S6 = 0, S7 = 0, S8 = 0;
369
370# undef STEP
371# define STEP(K, a, b, c, d, e, f, g, h) \
372 if ((W = wt_float[K]) != 0.0) { \
373 E = W * a; S8 += E; \
374 E = W * b; S7 += E; \
375 E = W * c; S6 += E; \
376 E = W * d; S5 += E; \
377 E = W * e; S4 += E; \
378 E = W * f; S3 += E; \
379 E = W * g; S2 += E; \
380 E = W * h; S1 += E; \
381 a = lp[K]; \
382 E = W * a; S0 += E; } else (a = lp[K])
383
384# define STEP_A(K) STEP(K, a, b, c, d, e, f, g, h)
385# define STEP_B(K) STEP(K, b, c, d, e, f, g, h, a)
386# define STEP_C(K) STEP(K, c, d, e, f, g, h, a, b)
387# define STEP_D(K) STEP(K, d, e, f, g, h, a, b, c)
388# define STEP_E(K) STEP(K, e, f, g, h, a, b, c, d)
389# define STEP_F(K) STEP(K, f, g, h, a, b, c, d, e)
390# define STEP_G(K) STEP(K, g, h, a, b, c, d, e, f)
391# define STEP_H(K) STEP(K, h, a, b, c, d, e, f, g)
392
393 STEP_A( 0); STEP_B( 1); STEP_C( 2); STEP_D( 3);
394 STEP_E( 4); STEP_F( 5); STEP_G( 6); STEP_H( 7);
395
396 STEP_A( 8); STEP_B( 9); STEP_C(10); STEP_D(11);
397 STEP_E(12); STEP_F(13); STEP_G(14); STEP_H(15);
398
399 STEP_A(16); STEP_B(17); STEP_C(18); STEP_D(19);
400 STEP_E(20); STEP_F(21); STEP_G(22); STEP_H(23);
401
402 STEP_A(24); STEP_B(25); STEP_C(26); STEP_D(27);
403 STEP_E(28); STEP_F(29); STEP_G(30); STEP_H(31);
404
405 STEP_A(32); STEP_B(33); STEP_C(34); STEP_D(35);
406 STEP_E(36); STEP_F(37); STEP_G(38); STEP_H(39);
407
408 if (S0 > L_max) { L_max = S0; Nc = lambda; }
409 if (S1 > L_max) { L_max = S1; Nc = lambda + 1; }
410 if (S2 > L_max) { L_max = S2; Nc = lambda + 2; }
411 if (S3 > L_max) { L_max = S3; Nc = lambda + 3; }
412 if (S4 > L_max) { L_max = S4; Nc = lambda + 4; }
413 if (S5 > L_max) { L_max = S5; Nc = lambda + 5; }
414 if (S6 > L_max) { L_max = S6; Nc = lambda + 6; }
415 if (S7 > L_max) { L_max = S7; Nc = lambda + 7; }
416 if (S8 > L_max) { L_max = S8; Nc = lambda + 8; }
417
418 }
419 *Nc_out = Nc;
420
421 L_max <<= 1;
422
423 /* Rescaling of L_max
424 */
425 assert(scal <= 100 && scal >= -100);
426 L_max = L_max >> (6 - scal); /* sub(6, scal) */
427
428 assert( Nc <= 120 && Nc >= 40);
429
430 /* Compute the power of the reconstructed short term residual
431 * signal dp[..]
432 */
433 L_power = 0;
434 for (k = 0; k <= 39; k++) {
435
436 register longword L_temp;
437
438 L_temp = SASR( dp[k - Nc], 3 );
439 L_power += L_temp * L_temp;
440 }
441 L_power <<= 1; /* from L_MULT */
442
443 /* Normalization of L_max and L_power
444 */
445
446 if (L_max <= 0) {
447 *bc_out = 0;
448 return;
449 }
450 if (L_max >= L_power) {
451 *bc_out = 3;
452 return;
453 }
454
455 temp = gsm_norm( L_power );
456
457 R = SASR( L_max << temp, 16 );
458 S = SASR( L_power << temp, 16 );
459
460 /* Coding of the LTP gain
461 */
462
463 /* Table 4.3a must be used to obtain the level DLB[i] for the
464 * quantization of the LTP gain b to get the coded version bc.
465 */
466 for (bc = 0; bc <= 2; bc++) if (R <= gsm_mult(S, gsm_DLB[bc])) break;
467 *bc_out = bc;
468}
469
470#endif /* LTP_CUT */
471
472static void Calculation_of_the_LTP_parameters P4((d,dp,bc_out,Nc_out),
473 register word * d, /* [0..39] IN */
474 register word * dp, /* [-120..-1] IN */
475 word * bc_out, /* OUT */
476 word * Nc_out /* OUT */
477)
478{
479 register int k, lambda;
480 word Nc, bc;
481
482 float wt_float[40];
483 float dp_float_base[120], * dp_float = dp_float_base + 120;
484
485 longword L_max, L_power;
486 word R, S, dmax, scal;
487 register word temp;
488
489 /* Search of the optimum scaling of d[0..39].
490 */
491 dmax = 0;
492
493 for (k = 0; k <= 39; k++) {
494 temp = d[k];
495 temp = GSM_ABS( temp );
496 if (temp > dmax) dmax = temp;
497 }
498
499 temp = 0;
500 if (dmax == 0) scal = 0;
501 else {
502 assert(dmax > 0);
503 temp = gsm_norm( (longword)dmax << 16 );
504 }
505
506 if (temp > 6) scal = 0;
507 else scal = 6 - temp;
508
509 assert(scal >= 0);
510
511 /* Initialization of a working array wt
512 */
513
514 for (k = 0; k < 40; k++) wt_float[k] = SASR( d[k], scal );
515 for (k = -120; k < 0; k++) dp_float[k] = dp[k];
516
517 /* Search for the maximum cross-correlation and coding of the LTP lag
518 */
519 L_max = 0;
520 Nc = 40; /* index for the maximum cross-correlation */
521
522 for (lambda = 40; lambda <= 120; lambda += 9) {
523
524 /* Calculate L_result for l = lambda .. lambda + 9.
525 */
526 register float *lp = dp_float - lambda;
527
528 register float W;
529 register float a = lp[-8], b = lp[-7], c = lp[-6],
530 d = lp[-5], e = lp[-4], f = lp[-3],
531 g = lp[-2], h = lp[-1];
532 register float E;
533 register float S0 = 0, S1 = 0, S2 = 0, S3 = 0, S4 = 0,
534 S5 = 0, S6 = 0, S7 = 0, S8 = 0;
535
536# undef STEP
537# define STEP(K, a, b, c, d, e, f, g, h) \
538 W = wt_float[K]; \
539 E = W * a; S8 += E; \
540 E = W * b; S7 += E; \
541 E = W * c; S6 += E; \
542 E = W * d; S5 += E; \
543 E = W * e; S4 += E; \
544 E = W * f; S3 += E; \
545 E = W * g; S2 += E; \
546 E = W * h; S1 += E; \
547 a = lp[K]; \
548 E = W * a; S0 += E
549
550# define STEP_A(K) STEP(K, a, b, c, d, e, f, g, h)
551# define STEP_B(K) STEP(K, b, c, d, e, f, g, h, a)
552# define STEP_C(K) STEP(K, c, d, e, f, g, h, a, b)
553# define STEP_D(K) STEP(K, d, e, f, g, h, a, b, c)
554# define STEP_E(K) STEP(K, e, f, g, h, a, b, c, d)
555# define STEP_F(K) STEP(K, f, g, h, a, b, c, d, e)
556# define STEP_G(K) STEP(K, g, h, a, b, c, d, e, f)
557# define STEP_H(K) STEP(K, h, a, b, c, d, e, f, g)
558
559 STEP_A( 0); STEP_B( 1); STEP_C( 2); STEP_D( 3);
560 STEP_E( 4); STEP_F( 5); STEP_G( 6); STEP_H( 7);
561
562 STEP_A( 8); STEP_B( 9); STEP_C(10); STEP_D(11);
563 STEP_E(12); STEP_F(13); STEP_G(14); STEP_H(15);
564
565 STEP_A(16); STEP_B(17); STEP_C(18); STEP_D(19);
566 STEP_E(20); STEP_F(21); STEP_G(22); STEP_H(23);
567
568 STEP_A(24); STEP_B(25); STEP_C(26); STEP_D(27);
569 STEP_E(28); STEP_F(29); STEP_G(30); STEP_H(31);
570
571 STEP_A(32); STEP_B(33); STEP_C(34); STEP_D(35);
572 STEP_E(36); STEP_F(37); STEP_G(38); STEP_H(39);
573
574 if (S0 > L_max) { L_max = S0; Nc = lambda; }
575 if (S1 > L_max) { L_max = S1; Nc = lambda + 1; }
576 if (S2 > L_max) { L_max = S2; Nc = lambda + 2; }
577 if (S3 > L_max) { L_max = S3; Nc = lambda + 3; }
578 if (S4 > L_max) { L_max = S4; Nc = lambda + 4; }
579 if (S5 > L_max) { L_max = S5; Nc = lambda + 5; }
580 if (S6 > L_max) { L_max = S6; Nc = lambda + 6; }
581 if (S7 > L_max) { L_max = S7; Nc = lambda + 7; }
582 if (S8 > L_max) { L_max = S8; Nc = lambda + 8; }
583 }
584 *Nc_out = Nc;
585
586 L_max <<= 1;
587
588 /* Rescaling of L_max
589 */
590 assert(scal <= 100 && scal >= -100);
591 L_max = L_max >> (6 - scal); /* sub(6, scal) */
592
593 assert( Nc <= 120 && Nc >= 40);
594
595 /* Compute the power of the reconstructed short term residual
596 * signal dp[..]
597 */
598 L_power = 0;
599 for (k = 0; k <= 39; k++) {
600
601 register longword L_temp;
602
603 L_temp = SASR( dp[k - Nc], 3 );
604 L_power += L_temp * L_temp;
605 }
606 L_power <<= 1; /* from L_MULT */
607
608 /* Normalization of L_max and L_power
609 */
610
611 if (L_max <= 0) {
612 *bc_out = 0;
613 return;
614 }
615 if (L_max >= L_power) {
616 *bc_out = 3;
617 return;
618 }
619
620 temp = gsm_norm( L_power );
621
622 R = SASR( L_max << temp, 16 );
623 S = SASR( L_power << temp, 16 );
624
625 /* Coding of the LTP gain
626 */
627
628 /* Table 4.3a must be used to obtain the level DLB[i] for the
629 * quantization of the LTP gain b to get the coded version bc.
630 */
631 for (bc = 0; bc <= 2; bc++) if (R <= gsm_mult(S, gsm_DLB[bc])) break;
632 *bc_out = bc;
633}
634
635#ifdef FAST
636#ifdef LTP_CUT
637
638static void Cut_Fast_Calculation_of_the_LTP_parameters P5((st,
639 d,dp,bc_out,Nc_out),
640 struct gsm_state * st, /* IN */
641 register word * d, /* [0..39] IN */
642 register word * dp, /* [-120..-1] IN */
643 word * bc_out, /* OUT */
644 word * Nc_out /* OUT */
645)
646{
647 register int k, lambda;
648 register float wt_float;
649 word Nc, bc;
650 word wt_max, best_k, ltp_cut;
651
652 float dp_float_base[120], * dp_float = dp_float_base + 120;
653
654 register float L_result, L_max, L_power;
655
656 wt_max = 0;
657
658 for (k = 0; k < 40; ++k) {
659 if ( d[k] > wt_max) wt_max = d[best_k = k];
660 else if (-d[k] > wt_max) wt_max = -d[best_k = k];
661 }
662
663 assert(wt_max >= 0);
664 wt_float = (float)wt_max;
665
666 for (k = -120; k < 0; ++k) dp_float[k] = (float)dp[k];
667
668 /* Search for the maximum cross-correlation and coding of the LTP lag
669 */
670 L_max = 0;
671 Nc = 40; /* index for the maximum cross-correlation */
672
673 for (lambda = 40; lambda <= 120; lambda++) {
674 L_result = wt_float * dp_float[best_k - lambda];
675 if (L_result > L_max) {
676 Nc = lambda;
677 L_max = L_result;
678 }
679 }
680
681 *Nc_out = Nc;
682 if (L_max <= 0.) {
683 *bc_out = 0;
684 return;
685 }
686
687 /* Compute the power of the reconstructed short term residual
688 * signal dp[..]
689 */
690 dp_float -= Nc;
691 L_power = 0;
692 for (k = 0; k < 40; ++k) {
693 register float f = dp_float[k];
694 L_power += f * f;
695 }
696
697 if (L_max >= L_power) {
698 *bc_out = 3;
699 return;
700 }
701
702 /* Coding of the LTP gain
703 * Table 4.3a must be used to obtain the level DLB[i] for the
704 * quantization of the LTP gain b to get the coded version bc.
705 */
706 lambda = L_max / L_power * 32768.;
707 for (bc = 0; bc <= 2; ++bc) if (lambda <= gsm_DLB[bc]) break;
708 *bc_out = bc;
709}
710
711#endif /* LTP_CUT */
712
713static void Fast_Calculation_of_the_LTP_parameters P4((d,dp,bc_out,Nc_out),
714 register word * d, /* [0..39] IN */
715 register word * dp, /* [-120..-1] IN */
716 word * bc_out, /* OUT */
717 word * Nc_out /* OUT */
718)
719{
720 register int k, lambda;
721 word Nc, bc;
722
723 float wt_float[40];
724 float dp_float_base[120], * dp_float = dp_float_base + 120;
725
726 register float L_max, L_power;
727
728 for (k = 0; k < 40; ++k) wt_float[k] = (float)d[k];
729 for (k = -120; k < 0; ++k) dp_float[k] = (float)dp[k];
730
731 /* Search for the maximum cross-correlation and coding of the LTP lag
732 */
733 L_max = 0;
734 Nc = 40; /* index for the maximum cross-correlation */
735
736 for (lambda = 40; lambda <= 120; lambda += 9) {
737
738 /* Calculate L_result for l = lambda .. lambda + 9.
739 */
740 register float *lp = dp_float - lambda;
741
742 register float W;
743 register float a = lp[-8], b = lp[-7], c = lp[-6],
744 d = lp[-5], e = lp[-4], f = lp[-3],
745 g = lp[-2], h = lp[-1];
746 register float E;
747 register float S0 = 0, S1 = 0, S2 = 0, S3 = 0, S4 = 0,
748 S5 = 0, S6 = 0, S7 = 0, S8 = 0;
749
750# undef STEP
751# define STEP(K, a, b, c, d, e, f, g, h) \
752 W = wt_float[K]; \
753 E = W * a; S8 += E; \
754 E = W * b; S7 += E; \
755 E = W * c; S6 += E; \
756 E = W * d; S5 += E; \
757 E = W * e; S4 += E; \
758 E = W * f; S3 += E; \
759 E = W * g; S2 += E; \
760 E = W * h; S1 += E; \
761 a = lp[K]; \
762 E = W * a; S0 += E
763
764# define STEP_A(K) STEP(K, a, b, c, d, e, f, g, h)
765# define STEP_B(K) STEP(K, b, c, d, e, f, g, h, a)
766# define STEP_C(K) STEP(K, c, d, e, f, g, h, a, b)
767# define STEP_D(K) STEP(K, d, e, f, g, h, a, b, c)
768# define STEP_E(K) STEP(K, e, f, g, h, a, b, c, d)
769# define STEP_F(K) STEP(K, f, g, h, a, b, c, d, e)
770# define STEP_G(K) STEP(K, g, h, a, b, c, d, e, f)
771# define STEP_H(K) STEP(K, h, a, b, c, d, e, f, g)
772
773 STEP_A( 0); STEP_B( 1); STEP_C( 2); STEP_D( 3);
774 STEP_E( 4); STEP_F( 5); STEP_G( 6); STEP_H( 7);
775
776 STEP_A( 8); STEP_B( 9); STEP_C(10); STEP_D(11);
777 STEP_E(12); STEP_F(13); STEP_G(14); STEP_H(15);
778
779 STEP_A(16); STEP_B(17); STEP_C(18); STEP_D(19);
780 STEP_E(20); STEP_F(21); STEP_G(22); STEP_H(23);
781
782 STEP_A(24); STEP_B(25); STEP_C(26); STEP_D(27);
783 STEP_E(28); STEP_F(29); STEP_G(30); STEP_H(31);
784
785 STEP_A(32); STEP_B(33); STEP_C(34); STEP_D(35);
786 STEP_E(36); STEP_F(37); STEP_G(38); STEP_H(39);
787
788 if (S0 > L_max) { L_max = S0; Nc = lambda; }
789 if (S1 > L_max) { L_max = S1; Nc = lambda + 1; }
790 if (S2 > L_max) { L_max = S2; Nc = lambda + 2; }
791 if (S3 > L_max) { L_max = S3; Nc = lambda + 3; }
792 if (S4 > L_max) { L_max = S4; Nc = lambda + 4; }
793 if (S5 > L_max) { L_max = S5; Nc = lambda + 5; }
794 if (S6 > L_max) { L_max = S6; Nc = lambda + 6; }
795 if (S7 > L_max) { L_max = S7; Nc = lambda + 7; }
796 if (S8 > L_max) { L_max = S8; Nc = lambda + 8; }
797 }
798 *Nc_out = Nc;
799
800 if (L_max <= 0.) {
801 *bc_out = 0;
802 return;
803 }
804
805 /* Compute the power of the reconstructed short term residual
806 * signal dp[..]
807 */
808 dp_float -= Nc;
809 L_power = 0;
810 for (k = 0; k < 40; ++k) {
811 register float f = dp_float[k];
812 L_power += f * f;
813 }
814
815 if (L_max >= L_power) {
816 *bc_out = 3;
817 return;
818 }
819
820 /* Coding of the LTP gain
821 * Table 4.3a must be used to obtain the level DLB[i] for the
822 * quantization of the LTP gain b to get the coded version bc.
823 */
824 lambda = L_max / L_power * 32768.;
825 for (bc = 0; bc <= 2; ++bc) if (lambda <= gsm_DLB[bc]) break;
826 *bc_out = bc;
827}
828
829#endif /* FAST */
830#endif /* USE_FLOAT_MUL */
831
832
833/* 4.2.12 */
834
835static void Long_term_analysis_filtering P6((bc,Nc,dp,d,dpp,e),
836 word bc, /* IN */
837 word Nc, /* IN */
838 register word * dp, /* previous d [-120..-1] IN */
839 register word * d, /* d [0..39] IN */
840 register word * dpp, /* estimate [0..39] OUT */
841 register word * e /* long term res. signal [0..39] OUT */
842)
843/*
844 * In this part, we have to decode the bc parameter to compute
845 * the samples of the estimate dpp[0..39]. The decoding of bc needs the
846 * use of table 4.3b. The long term residual signal e[0..39]
847 * is then calculated to be fed to the RPE encoding section.
848 */
849{
850 register int k;
851 register longword ltmp;
852
853# undef STEP
854# define STEP(BP) \
855 for (k = 0; k <= 39; k++) { \
856 dpp[k] = GSM_MULT_R( BP, dp[k - Nc]); \
857 e[k] = GSM_SUB( d[k], dpp[k] ); \
858 }
859
860 switch (bc) {
861 case 0: STEP( 3277 ); break;
862 case 1: STEP( 11469 ); break;
863 case 2: STEP( 21299 ); break;
864 case 3: STEP( 32767 ); break;
865 }
866}
867
868void Gsm_Long_Term_Predictor P7((S,d,dp,e,dpp,Nc,bc), /* 4x for 160 samples */
869
870 struct gsm_state * S,
871
872 word * d, /* [0..39] residual signal IN */
873 word * dp, /* [-120..-1] d' IN */
874
875 word * e, /* [0..39] OUT */
876 word * dpp, /* [0..39] OUT */
877 word * Nc, /* correlation lag OUT */
878 word * bc /* gain factor OUT */
879)
880{
881 assert( d ); assert( dp ); assert( e );
882 assert( dpp); assert( Nc ); assert( bc );
883
884#if defined(FAST) && defined(USE_FLOAT_MUL)
885 if (S->fast)
886#if defined (LTP_CUT)
887 if (S->ltp_cut)
888 Cut_Fast_Calculation_of_the_LTP_parameters(S,
889 d, dp, bc, Nc);
890 else
891#endif /* LTP_CUT */
892 Fast_Calculation_of_the_LTP_parameters(d, dp, bc, Nc );
893 else
894#endif /* FAST & USE_FLOAT_MUL */
895#ifdef LTP_CUT
896 if (S->ltp_cut)
897 Cut_Calculation_of_the_LTP_parameters(S, d, dp, bc, Nc);
898 else
899#endif
900 Calculation_of_the_LTP_parameters(d, dp, bc, Nc);
901
902 Long_term_analysis_filtering( *bc, *Nc, dp, d, dpp, e );
903}
904
905/* 4.3.2 */
906void Gsm_Long_Term_Synthesis_Filtering P5((S,Ncr,bcr,erp,drp),
907 struct gsm_state * S,
908
909 word Ncr,
910 word bcr,
911 register word * erp, /* [0..39] IN */
912 register word * drp /* [-120..-1] IN, [-120..40] OUT */
913)
914/*
915 * This procedure uses the bcr and Ncr parameter to realize the
916 * long term synthesis filtering. The decoding of bcr needs
917 * table 4.3b.
918 */
919{
920 register longword ltmp; /* for ADD */
921 register int k;
922 word brp, drpp, Nr;
923
924 /* Check the limits of Nr.
925 */
926 Nr = Ncr < 40 || Ncr > 120 ? S->nrp : Ncr;
927 S->nrp = Nr;
928 assert(Nr >= 40 && Nr <= 120);
929
930 /* Decoding of the LTP gain bcr
931 */
932 brp = gsm_QLB[ bcr ];
933
934 /* Computation of the reconstructed short term residual
935 * signal drp[0..39]
936 */
937 assert(brp != MIN_WORD);
938
939 for (k = 0; k <= 39; k++) {
940 drpp = GSM_MULT_R( brp, drp[ k - Nr ] );
941 drp[k] = GSM_ADD( erp[k], drpp );
942 }
943
944 /*
945 * Update of the reconstructed short term residual signal
946 * drp[ -1..-120 ]
947 */
948
949 for (k = 0; k <= 119; k++) drp[ -120 + k ] = drp[ -80 + k ];
950}