rpe.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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/rpe.c,v 1.3 1994/05/10 20:18:46 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. /* 4.2.13 .. 4.2.17 RPE ENCODING SECTION
  14. */
  15. /* 4.2.13 */
  16. static void Weighting_filter P2((e, x),
  17. register word * e, /* signal [-5..0.39.44] IN */
  18. word * x /* signal [0..39] OUT */
  19. )
  20. /*
  21. * The coefficients of the weighting filter are stored in a table
  22. * (see table 4.4). The following scaling is used:
  23. *
  24. * H[0..10] = integer( real_H[ 0..10] * 8192 );
  25. */
  26. {
  27. /* word wt[ 50 ]; */
  28. register longword L_result;
  29. register int k /* , i */ ;
  30. /* Initialization of a temporary working array wt[0...49]
  31. */
  32. /* for (k = 0; k <= 4; k++) wt[k] = 0;
  33. * for (k = 5; k <= 44; k++) wt[k] = *e++;
  34. * for (k = 45; k <= 49; k++) wt[k] = 0;
  35. *
  36. * (e[-5..-1] and e[40..44] are allocated by the caller,
  37. * are initially zero and are not written anywhere.)
  38. */
  39. e -= 5;
  40. /* Compute the signal x[0..39]
  41. */
  42. for (k = 0; k <= 39; k++) {
  43. L_result = 8192 >> 1;
  44. /* for (i = 0; i <= 10; i++) {
  45. * L_temp = GSM_L_MULT( wt[k+i], gsm_H[i] );
  46. * L_result = GSM_L_ADD( L_result, L_temp );
  47. * }
  48. */
  49. #undef STEP
  50. #define STEP( i, H ) (e[ k + i ] * (longword)H)
  51. /* Every one of these multiplications is done twice --
  52. * but I don't see an elegant way to optimize this.
  53. * Do you?
  54. */
  55. #ifdef STUPID_COMPILER
  56. L_result += STEP( 0, -134 ) ;
  57. L_result += STEP( 1, -374 ) ;
  58. /* + STEP( 2, 0 ) */
  59. L_result += STEP( 3, 2054 ) ;
  60. L_result += STEP( 4, 5741 ) ;
  61. L_result += STEP( 5, 8192 ) ;
  62. L_result += STEP( 6, 5741 ) ;
  63. L_result += STEP( 7, 2054 ) ;
  64. /* + STEP( 8, 0 ) */
  65. L_result += STEP( 9, -374 ) ;
  66. L_result += STEP( 10, -134 ) ;
  67. #else
  68. L_result +=
  69. STEP( 0, -134 )
  70. + STEP( 1, -374 )
  71. /* + STEP( 2, 0 ) */
  72. + STEP( 3, 2054 )
  73. + STEP( 4, 5741 )
  74. + STEP( 5, 8192 )
  75. + STEP( 6, 5741 )
  76. + STEP( 7, 2054 )
  77. /* + STEP( 8, 0 ) */
  78. + STEP( 9, -374 )
  79. + STEP(10, -134 )
  80. ;
  81. #endif
  82. /* L_result = GSM_L_ADD( L_result, L_result ); (* scaling(x2) *)
  83. * L_result = GSM_L_ADD( L_result, L_result ); (* scaling(x4) *)
  84. *
  85. * x[k] = SASR( L_result, 16 );
  86. */
  87. /* 2 adds vs. >>16 => 14, minus one shift to compensate for
  88. * those we lost when replacing L_MULT by '*'.
  89. */
  90. L_result = SASR( L_result, 13 );
  91. x[k] = ( L_result < MIN_WORD ? MIN_WORD
  92. : (L_result > MAX_WORD ? MAX_WORD : L_result ));
  93. }
  94. }
  95. /* 4.2.14 */
  96. static void RPE_grid_selection P3((x,xM,Mc_out),
  97. word * x, /* [0..39] IN */
  98. word * xM, /* [0..12] OUT */
  99. word * Mc_out /* OUT */
  100. )
  101. /*
  102. * The signal x[0..39] is used to select the RPE grid which is
  103. * represented by Mc.
  104. */
  105. {
  106. /* register word temp1; */
  107. register int /* m, */ i;
  108. register longword L_result, L_temp;
  109. longword EM; /* xxx should be L_EM? */
  110. word Mc;
  111. longword L_common_0_3;
  112. EM = 0;
  113. Mc = 0;
  114. /* for (m = 0; m <= 3; m++) {
  115. * L_result = 0;
  116. *
  117. *
  118. * for (i = 0; i <= 12; i++) {
  119. *
  120. * temp1 = SASR( x[m + 3*i], 2 );
  121. *
  122. * assert(temp1 != MIN_WORD);
  123. *
  124. * L_temp = GSM_L_MULT( temp1, temp1 );
  125. * L_result = GSM_L_ADD( L_temp, L_result );
  126. * }
  127. *
  128. * if (L_result > EM) {
  129. * Mc = m;
  130. * EM = L_result;
  131. * }
  132. * }
  133. */
  134. #undef STEP
  135. #define STEP( m, i ) L_temp = SASR( x[m + 3 * i], 2 ); \
  136. L_result += L_temp * L_temp;
  137. /* common part of 0 and 3 */
  138. L_result = 0;
  139. STEP( 0, 1 ); STEP( 0, 2 ); STEP( 0, 3 ); STEP( 0, 4 );
  140. STEP( 0, 5 ); STEP( 0, 6 ); STEP( 0, 7 ); STEP( 0, 8 );
  141. STEP( 0, 9 ); STEP( 0, 10); STEP( 0, 11); STEP( 0, 12);
  142. L_common_0_3 = L_result;
  143. /* i = 0 */
  144. STEP( 0, 0 );
  145. L_result <<= 1; /* implicit in L_MULT */
  146. EM = L_result;
  147. /* i = 1 */
  148. L_result = 0;
  149. STEP( 1, 0 );
  150. STEP( 1, 1 ); STEP( 1, 2 ); STEP( 1, 3 ); STEP( 1, 4 );
  151. STEP( 1, 5 ); STEP( 1, 6 ); STEP( 1, 7 ); STEP( 1, 8 );
  152. STEP( 1, 9 ); STEP( 1, 10); STEP( 1, 11); STEP( 1, 12);
  153. L_result <<= 1;
  154. if (L_result > EM) {
  155. Mc = 1;
  156. EM = L_result;
  157. }
  158. /* i = 2 */
  159. L_result = 0;
  160. STEP( 2, 0 );
  161. STEP( 2, 1 ); STEP( 2, 2 ); STEP( 2, 3 ); STEP( 2, 4 );
  162. STEP( 2, 5 ); STEP( 2, 6 ); STEP( 2, 7 ); STEP( 2, 8 );
  163. STEP( 2, 9 ); STEP( 2, 10); STEP( 2, 11); STEP( 2, 12);
  164. L_result <<= 1;
  165. if (L_result > EM) {
  166. Mc = 2;
  167. EM = L_result;
  168. }
  169. /* i = 3 */
  170. L_result = L_common_0_3;
  171. STEP( 3, 12 );
  172. L_result <<= 1;
  173. if (L_result > EM) {
  174. Mc = 3;
  175. EM = L_result;
  176. }
  177. /**/
  178. /* Down-sampling by a factor 3 to get the selected xM[0..12]
  179. * RPE sequence.
  180. */
  181. for (i = 0; i <= 12; i ++) xM[i] = x[Mc + 3*i];
  182. *Mc_out = Mc;
  183. }
  184. /* 4.12.15 */
  185. static void APCM_quantization_xmaxc_to_exp_mant P3((xmaxc,exp_out,mant_out),
  186. word xmaxc, /* IN */
  187. word * exp_out, /* OUT */
  188. word * mant_out ) /* OUT */
  189. {
  190. word exp, mant;
  191. /* Compute exponent and mantissa of the decoded version of xmaxc
  192. */
  193. exp = 0;
  194. if (xmaxc > 15) exp = SASR(xmaxc, 3) - 1;
  195. mant = xmaxc - (exp << 3);
  196. if (mant == 0) {
  197. exp = -4;
  198. mant = 7;
  199. }
  200. else {
  201. while (mant <= 7) {
  202. mant = mant << 1 | 1;
  203. exp--;
  204. }
  205. mant -= 8;
  206. }
  207. assert( exp >= -4 && exp <= 6 );
  208. assert( mant >= 0 && mant <= 7 );
  209. *exp_out = exp;
  210. *mant_out = mant;
  211. }
  212. static void APCM_quantization P5((xM,xMc,mant_out,exp_out,xmaxc_out),
  213. word * xM, /* [0..12] IN */
  214. word * xMc, /* [0..12] OUT */
  215. word * mant_out, /* OUT */
  216. word * exp_out, /* OUT */
  217. word * xmaxc_out /* OUT */
  218. )
  219. {
  220. int i, itest;
  221. word xmax, xmaxc, temp, temp1, temp2;
  222. word exp, mant;
  223. /* Find the maximum absolute value xmax of xM[0..12].
  224. */
  225. xmax = 0;
  226. for (i = 0; i <= 12; i++) {
  227. temp = xM[i];
  228. temp = GSM_ABS(temp);
  229. if (temp > xmax) xmax = temp;
  230. }
  231. /* Qantizing and coding of xmax to get xmaxc.
  232. */
  233. exp = 0;
  234. temp = SASR( xmax, 9 );
  235. itest = 0;
  236. for (i = 0; i <= 5; i++) {
  237. itest |= (temp <= 0);
  238. temp = SASR( temp, 1 );
  239. assert(exp <= 5);
  240. if (itest == 0) exp++; /* exp = add (exp, 1) */
  241. }
  242. assert(exp <= 6 && exp >= 0);
  243. temp = exp + 5;
  244. assert(temp <= 11 && temp >= 0);
  245. xmaxc = gsm_add( SASR(xmax, temp), exp << 3 );
  246. /* Quantizing and coding of the xM[0..12] RPE sequence
  247. * to get the xMc[0..12]
  248. */
  249. APCM_quantization_xmaxc_to_exp_mant( xmaxc, &exp, &mant );
  250. /* This computation uses the fact that the decoded version of xmaxc
  251. * can be calculated by using the exponent and the mantissa part of
  252. * xmaxc (logarithmic table).
  253. * So, this method avoids any division and uses only a scaling
  254. * of the RPE samples by a function of the exponent. A direct
  255. * multiplication by the inverse of the mantissa (NRFAC[0..7]
  256. * found in table 4.5) gives the 3 bit coded version xMc[0..12]
  257. * of the RPE samples.
  258. */
  259. /* Direct computation of xMc[0..12] using table 4.5
  260. */
  261. assert( exp <= 4096 && exp >= -4096);
  262. assert( mant >= 0 && mant <= 7 );
  263. temp1 = 6 - exp; /* normalization by the exponent */
  264. temp2 = gsm_NRFAC[ mant ]; /* inverse mantissa */
  265. for (i = 0; i <= 12; i++) {
  266. assert(temp1 >= 0 && temp1 < 16);
  267. temp = xM[i] << temp1;
  268. temp = GSM_MULT( temp, temp2 );
  269. temp = SASR(temp, 12);
  270. xMc[i] = temp + 4; /* see note below */
  271. }
  272. /* NOTE: This equation is used to make all the xMc[i] positive.
  273. */
  274. *mant_out = mant;
  275. *exp_out = exp;
  276. *xmaxc_out = xmaxc;
  277. }
  278. /* 4.2.16 */
  279. static void APCM_inverse_quantization P4((xMc,mant,exp,xMp),
  280. register word * xMc, /* [0..12] IN */
  281. word mant,
  282. word exp,
  283. register word * xMp) /* [0..12] OUT */
  284. /*
  285. * This part is for decoding the RPE sequence of coded xMc[0..12]
  286. * samples to obtain the xMp[0..12] array. Table 4.6 is used to get
  287. * the mantissa of xmaxc (FAC[0..7]).
  288. */
  289. {
  290. int i;
  291. word temp, temp1, temp2, temp3;
  292. longword ltmp;
  293. assert( mant >= 0 && mant <= 7 );
  294. temp1 = gsm_FAC[ mant ]; /* see 4.2-15 for mant */
  295. temp2 = gsm_sub( 6, exp ); /* see 4.2-15 for exp */
  296. temp3 = gsm_asl( 1, gsm_sub( temp2, 1 ));
  297. for (i = 13; i--;) {
  298. assert( *xMc <= 7 && *xMc >= 0 ); /* 3 bit unsigned */
  299. /* temp = gsm_sub( *xMc++ << 1, 7 ); */
  300. temp = (*xMc++ << 1) - 7; /* restore sign */
  301. assert( temp <= 7 && temp >= -7 ); /* 4 bit signed */
  302. temp <<= 12; /* 16 bit signed */
  303. temp = GSM_MULT_R( temp1, temp );
  304. temp = GSM_ADD( temp, temp3 );
  305. *xMp++ = gsm_asr( temp, temp2 );
  306. }
  307. }
  308. /* 4.2.17 */
  309. static void RPE_grid_positioning P3((Mc,xMp,ep),
  310. word Mc, /* grid position IN */
  311. register word * xMp, /* [0..12] IN */
  312. register word * ep /* [0..39] OUT */
  313. )
  314. /*
  315. * This procedure computes the reconstructed long term residual signal
  316. * ep[0..39] for the LTP analysis filter. The inputs are the Mc
  317. * which is the grid position selection and the xMp[0..12] decoded
  318. * RPE samples which are upsampled by a factor of 3 by inserting zero
  319. * values.
  320. */
  321. {
  322. int i = 13;
  323. assert(0 <= Mc && Mc <= 3);
  324. switch (Mc) {
  325. case 3: *ep++ = 0;
  326. case 2: do {
  327. *ep++ = 0;
  328. case 1: *ep++ = 0;
  329. case 0: *ep++ = *xMp++;
  330. } while (--i);
  331. }
  332. while (++Mc < 4) *ep++ = 0;
  333. /*
  334. int i, k;
  335. for (k = 0; k <= 39; k++) ep[k] = 0;
  336. for (i = 0; i <= 12; i++) {
  337. ep[ Mc + (3*i) ] = xMp[i];
  338. }
  339. */
  340. }
  341. /* 4.2.18 */
  342. /* This procedure adds the reconstructed long term residual signal
  343. * ep[0..39] to the estimated signal dpp[0..39] from the long term
  344. * analysis filter to compute the reconstructed short term residual
  345. * signal dp[-40..-1]; also the reconstructed short term residual
  346. * array dp[-120..-41] is updated.
  347. */
  348. #if 0 /* Has been inlined in code.c */
  349. void Gsm_Update_of_reconstructed_short_time_residual_signal P3((dpp, ep, dp),
  350. word * dpp, /* [0...39] IN */
  351. word * ep, /* [0...39] IN */
  352. word * dp) /* [-120...-1] IN/OUT */
  353. {
  354. int k;
  355. for (k = 0; k <= 79; k++)
  356. dp[ -120 + k ] = dp[ -80 + k ];
  357. for (k = 0; k <= 39; k++)
  358. dp[ -40 + k ] = gsm_add( ep[k], dpp[k] );
  359. }
  360. #endif /* Has been inlined in code.c */
  361. void Gsm_RPE_Encoding P5((S,e,xmaxc,Mc,xMc),
  362. struct gsm_state * S,
  363. word * e, /* -5..-1][0..39][40..44 IN/OUT */
  364. word * xmaxc, /* OUT */
  365. word * Mc, /* OUT */
  366. word * xMc) /* [0..12] OUT */
  367. {
  368. word x[40];
  369. word xM[13], xMp[13];
  370. word mant, exp;
  371. Weighting_filter(e, x);
  372. RPE_grid_selection(x, xM, Mc);
  373. APCM_quantization( xM, xMc, &mant, &exp, xmaxc);
  374. APCM_inverse_quantization( xMc, mant, exp, xMp);
  375. RPE_grid_positioning( *Mc, xMp, e );
  376. }
  377. void Gsm_RPE_Decoding P5((S, xmaxcr, Mcr, xMcr, erp),
  378. struct gsm_state * S,
  379. word xmaxcr,
  380. word Mcr,
  381. word * xMcr, /* [0..12], 3 bits IN */
  382. word * erp /* [0..39] OUT */
  383. )
  384. {
  385. word exp, mant;
  386. word xMp[ 13 ];
  387. APCM_quantization_xmaxc_to_exp_mant( xmaxcr, &exp, &mant );
  388. APCM_inverse_quantization( xMcr, mant, exp, xMp );
  389. RPE_grid_positioning( Mcr, xMp, erp );
  390. }