long_term.c 23 KB

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