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.
17 /* 4.2.13 .. 4.2.17 RPE ENCODING SECTION
22 static void Weighting_filter P2((e, x),
23 register word * e, /* signal [-5..0.39.44] IN */
24 word * x /* signal [0..39] OUT */
27 * The coefficients of the weighting filter are stored in a table
28 * (see table 4.4). The following scaling is used:
30 * H[0..10] = integer( real_H[ 0..10] * 8192 );
35 register longword L_result;
36 register int k /* , i */ ;
38 /* Initialization of a temporary working array wt[0...49]
41 /* for (k = 0; k <= 4; k++) wt[k] = 0;
42 * for (k = 5; k <= 44; k++) wt[k] = *e++;
43 * for (k = 45; k <= 49; k++) wt[k] = 0;
45 * (e[-5..-1] and e[40..44] are allocated by the caller,
46 * are initially zero and are not written anywhere.)
50 /* Compute the signal x[0..39]
52 for (k = 0; k <= 39; k++) {
56 /* for (i = 0; i <= 10; i++) {
57 * L_temp = GSM_L_MULT( wt[k+i], gsm_H[i] );
58 * L_result = GSM_L_ADD( L_result, L_temp );
63 #define STEP( i, H ) (e[ k + i ] * (longword)H)
65 /* Every one of these multiplications is done twice --
66 * but I don't see an elegant way to optimize this.
70 #ifdef STUPID_COMPILER
71 L_result += STEP( 0, -134 ) ;
72 L_result += STEP( 1, -374 ) ;
74 L_result += STEP( 3, 2054 ) ;
75 L_result += STEP( 4, 5741 ) ;
76 L_result += STEP( 5, 8192 ) ;
77 L_result += STEP( 6, 5741 ) ;
78 L_result += STEP( 7, 2054 ) ;
80 L_result += STEP( 9, -374 ) ;
81 L_result += STEP( 10, -134 ) ;
98 /* L_result = GSM_L_ADD( L_result, L_result ); (* scaling(x2) *)
99 * L_result = GSM_L_ADD( L_result, L_result ); (* scaling(x4) *)
101 * x[k] = SASR( L_result, 16 );
104 /* 2 adds vs. >>16 => 14, minus one shift to compensate for
105 * those we lost when replacing L_MULT by '*'.
108 L_result = SASR( L_result, 13 );
109 x[k] = ( L_result < MIN_WORD ? MIN_WORD
110 : (L_result > MAX_WORD ? MAX_WORD : L_result ));
116 static void RPE_grid_selection P3((x,xM,Mc_out),
117 word * x, /* [0..39] IN */
118 word * xM, /* [0..12] OUT */
119 word * Mc_out /* OUT */
122 * The signal x[0..39] is used to select the RPE grid which is
126 /* register word temp1; */
127 register int /* m, */ i;
128 register longword L_result, L_temp;
129 longword EM; /* xxx should be L_EM? */
132 longword L_common_0_3;
137 /* for (m = 0; m <= 3; m++) {
141 * for (i = 0; i <= 12; i++) {
143 * temp1 = SASR( x[m + 3*i], 2 );
145 * assert(temp1 != MIN_WORD);
147 * L_temp = GSM_L_MULT( temp1, temp1 );
148 * L_result = GSM_L_ADD( L_temp, L_result );
151 * if (L_result > EM) {
159 #define STEP( m, i ) L_temp = SASR( x[m + 3 * i], 2 ); \
160 L_result += L_temp * L_temp;
162 /* common part of 0 and 3 */
165 STEP( 0, 1 ); STEP( 0, 2 ); STEP( 0, 3 ); STEP( 0, 4 );
166 STEP( 0, 5 ); STEP( 0, 6 ); STEP( 0, 7 ); STEP( 0, 8 );
167 STEP( 0, 9 ); STEP( 0, 10); STEP( 0, 11); STEP( 0, 12);
168 L_common_0_3 = L_result;
173 L_result <<= 1; /* implicit in L_MULT */
180 STEP( 1, 1 ); STEP( 1, 2 ); STEP( 1, 3 ); STEP( 1, 4 );
181 STEP( 1, 5 ); STEP( 1, 6 ); STEP( 1, 7 ); STEP( 1, 8 );
182 STEP( 1, 9 ); STEP( 1, 10); STEP( 1, 11); STEP( 1, 12);
193 STEP( 2, 1 ); STEP( 2, 2 ); STEP( 2, 3 ); STEP( 2, 4 );
194 STEP( 2, 5 ); STEP( 2, 6 ); STEP( 2, 7 ); STEP( 2, 8 );
195 STEP( 2, 9 ); STEP( 2, 10); STEP( 2, 11); STEP( 2, 12);
204 L_result = L_common_0_3;
214 /* Down-sampling by a factor 3 to get the selected xM[0..12]
217 for (i = 0; i <= 12; i ++) xM[i] = x[Mc + 3*i];
223 static void APCM_quantization_xmaxc_to_exp_mant P3((xmaxc,exp_out,mant_out),
225 word * exp_out, /* OUT */
226 word * mant_out ) /* OUT */
230 /* Compute exponent and mantissa of the decoded version of xmaxc
234 if (xmaxc > 15) exp = SASR(xmaxc, 3) - 1;
235 mant = xmaxc - (exp << 3);
243 mant = mant << 1 | 1;
249 assert( exp >= -4 && exp <= 6 );
250 assert( mant >= 0 && mant <= 7 );
256 static void APCM_quantization P5((xM,xMc,mant_out,exp_out,xmaxc_out),
257 word * xM, /* [0..12] IN */
259 word * xMc, /* [0..12] OUT */
260 word * mant_out, /* OUT */
261 word * exp_out, /* OUT */
262 word * xmaxc_out /* OUT */
267 word xmax, xmaxc, temp, temp1, temp2;
271 /* Find the maximum absolute value xmax of xM[0..12].
275 for (i = 0; i <= 12; i++) {
277 temp = GSM_ABS(temp);
278 if (temp > xmax) xmax = temp;
281 /* Qantizing and coding of xmax to get xmaxc.
285 temp = SASR( xmax, 9 );
288 for (i = 0; i <= 5; i++) {
290 itest |= (temp <= 0);
291 temp = SASR( temp, 1 );
294 if (itest == 0) exp++; /* exp = add (exp, 1) */
297 assert(exp <= 6 && exp >= 0);
300 assert(temp <= 11 && temp >= 0);
301 xmaxc = gsm_add( SASR(xmax, temp), exp << 3 );
303 /* Quantizing and coding of the xM[0..12] RPE sequence
304 * to get the xMc[0..12]
307 APCM_quantization_xmaxc_to_exp_mant( xmaxc, &exp, &mant );
309 /* This computation uses the fact that the decoded version of xmaxc
310 * can be calculated by using the exponent and the mantissa part of
311 * xmaxc (logarithmic table).
312 * So, this method avoids any division and uses only a scaling
313 * of the RPE samples by a function of the exponent. A direct
314 * multiplication by the inverse of the mantissa (NRFAC[0..7]
315 * found in table 4.5) gives the 3 bit coded version xMc[0..12]
316 * of the RPE samples.
320 /* Direct computation of xMc[0..12] using table 4.5
323 assert( exp <= 4096 && exp >= -4096);
324 assert( mant >= 0 && mant <= 7 );
326 temp1 = 6 - exp; /* normalization by the exponent */
327 temp2 = gsm_NRFAC[ mant ]; /* inverse mantissa */
329 for (i = 0; i <= 12; i++) {
331 assert(temp1 >= 0 && temp1 < 16);
333 temp = xM[i] << temp1;
334 temp = GSM_MULT( temp, temp2 );
335 temp = SASR(temp, 12);
336 xMc[i] = temp + 4; /* see note below */
339 /* NOTE: This equation is used to make all the xMc[i] positive.
349 static void APCM_inverse_quantization P4((xMc,mant,exp,xMp),
350 register word * xMc, /* [0..12] IN */
353 register word * xMp) /* [0..12] OUT */
355 * This part is for decoding the RPE sequence of coded xMc[0..12]
356 * samples to obtain the xMp[0..12] array. Table 4.6 is used to get
357 * the mantissa of xmaxc (FAC[0..7]).
361 word temp, temp1, temp2, temp3;
364 assert( mant >= 0 && mant <= 7 );
366 temp1 = gsm_FAC[ mant ]; /* see 4.2-15 for mant */
367 temp2 = gsm_sub( 6, exp ); /* see 4.2-15 for exp */
368 temp3 = gsm_asl( 1, gsm_sub( temp2, 1 ));
372 assert( *xMc <= 7 && *xMc >= 0 ); /* 3 bit unsigned */
374 /* temp = gsm_sub( *xMc++ << 1, 7 ); */
375 temp = (*xMc++ << 1) - 7; /* restore sign */
376 assert( temp <= 7 && temp >= -7 ); /* 4 bit signed */
378 temp <<= 12; /* 16 bit signed */
379 temp = GSM_MULT_R( temp1, temp );
380 temp = GSM_ADD( temp, temp3 );
381 *xMp++ = gsm_asr( temp, temp2 );
387 static void RPE_grid_positioning P3((Mc,xMp,ep),
388 word Mc, /* grid position IN */
389 register word * xMp, /* [0..12] IN */
390 register word * ep /* [0..39] OUT */
393 * This procedure computes the reconstructed long term residual signal
394 * ep[0..39] for the LTP analysis filter. The inputs are the Mc
395 * which is the grid position selection and the xMp[0..12] decoded
396 * RPE samples which are upsampled by a factor of 3 by inserting zero
402 assert(0 <= Mc && Mc <= 3);
409 case 0: *ep++ = *xMp++;
412 while (++Mc < 4) *ep++ = 0;
417 for (k = 0; k <= 39; k++) ep[k] = 0;
418 for (i = 0; i <= 12; i++) {
419 ep[ Mc + (3*i) ] = xMp[i];
426 /* This procedure adds the reconstructed long term residual signal
427 * ep[0..39] to the estimated signal dpp[0..39] from the long term
428 * analysis filter to compute the reconstructed short term residual
429 * signal dp[-40..-1]; also the reconstructed short term residual
430 * array dp[-120..-41] is updated.
433 #if 0 /* Has been inlined in code.c */
434 void Gsm_Update_of_reconstructed_short_time_residual_signal P3((dpp, ep, dp),
435 word * dpp, /* [0...39] IN */
436 word * ep, /* [0...39] IN */
437 word * dp) /* [-120...-1] IN/OUT */
441 for (k = 0; k <= 79; k++)
442 dp[ -120 + k ] = dp[ -80 + k ];
444 for (k = 0; k <= 39; k++)
445 dp[ -40 + k ] = gsm_add( ep[k], dpp[k] );
447 #endif /* Has been inlined in code.c */
449 void Gsm_RPE_Encoding P5((S,e,xmaxc,Mc,xMc),
451 struct gsm_state * S,
453 word * e, /* -5..-1][0..39][40..44 IN/OUT */
454 word * xmaxc, /* OUT */
456 word * xMc) /* [0..12] OUT */
459 word xM[13], xMp[13];
462 Weighting_filter(e, x);
463 RPE_grid_selection(x, xM, Mc);
465 APCM_quantization( xM, xMc, &mant, &exp, xmaxc);
466 APCM_inverse_quantization( xMc, mant, exp, xMp);
468 RPE_grid_positioning( *Mc, xMp, e );
472 void Gsm_RPE_Decoding P5((S, xmaxcr, Mcr, xMcr, erp),
473 struct gsm_state * S,
477 word * xMcr, /* [0..12], 3 bits IN */
478 word * erp /* [0..39] OUT */
484 APCM_quantization_xmaxc_to_exp_mant( xmaxcr, &exp, &mant );
485 APCM_inverse_quantization( xMcr, mant, exp, xMp );
486 RPE_grid_positioning( Mcr, xMp, erp );