rpe.c
上传用户:shw771010
上传日期:2022-01-05
资源大小:991k
文件大小:11k
源码类别:

Audio

开发平台:

Unix_Linux

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