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.
7 /* $Header: /cvsroot/sox/sox/libgsm/rpe.c,v 1.2 2007/11/04 16:32:36 robs Exp $ */
16 /* 4.2.13 .. 4.2.17 RPE ENCODING SECTION
21 static void Weighting_filter (
22 register word
* e
, /* signal [-5..0.39.44] IN */
23 word
* x
/* signal [0..39] OUT */
26 * The coefficients of the weighting filter are stored in a table
27 * (see table 4.4). The following scaling is used:
29 * H[0..10] = integer( real_H[ 0..10] * 8192 );
34 register longword L_result
;
35 register int k
/* , i */ ;
37 /* Initialization of a temporary working array wt[0...49]
40 /* for (k = 0; k <= 4; k++) wt[k] = 0;
41 * for (k = 5; k <= 44; k++) wt[k] = *e++;
42 * for (k = 45; k <= 49; k++) wt[k] = 0;
44 * (e[-5..-1] and e[40..44] are allocated by the caller,
45 * are initially zero and are not written anywhere.)
49 /* Compute the signal x[0..39]
51 for (k
= 0; k
<= 39; k
++) {
55 /* for (i = 0; i <= 10; i++) {
56 * L_temp = GSM_L_MULT( wt[k+i], gsm_H[i] );
57 * L_result = GSM_L_ADD( L_result, L_temp );
62 #define STEP( i, H ) (e[ k + i ] * (longword)H)
64 /* Every one of these multiplications is done twice --
65 * but I don't see an elegant way to optimize this.
69 #ifdef STUPID_COMPILER
70 L_result
+= STEP( 0, -134 ) ;
71 L_result
+= STEP( 1, -374 ) ;
73 L_result
+= STEP( 3, 2054 ) ;
74 L_result
+= STEP( 4, 5741 ) ;
75 L_result
+= STEP( 5, 8192 ) ;
76 L_result
+= STEP( 6, 5741 ) ;
77 L_result
+= STEP( 7, 2054 ) ;
79 L_result
+= STEP( 9, -374 ) ;
80 L_result
+= STEP( 10, -134 ) ;
97 /* L_result = GSM_L_ADD( L_result, L_result ); (* scaling(x2) *)
98 * L_result = GSM_L_ADD( L_result, L_result ); (* scaling(x4) *)
100 * x[k] = SASR( L_result, 16 );
103 /* 2 adds vs. >>16 => 14, minus one shift to compensate for
104 * those we lost when replacing L_MULT by '*'.
107 L_result
= SASR( L_result
, 13 );
108 x
[k
] = ( L_result
< MIN_WORD
? MIN_WORD
109 : (L_result
> MAX_WORD
? MAX_WORD
: L_result
));
115 static void RPE_grid_selection (
116 word
* x
, /* [0..39] IN */
117 word
* xM
, /* [0..12] OUT */
118 word
* Mc_out
/* OUT */
121 * The signal x[0..39] is used to select the RPE grid which is
125 /* register word temp1; */
126 register int /* m, */ i
;
127 register longword L_result
, L_temp
;
128 longword EM
; /* xxx should be L_EM? */
131 longword L_common_0_3
;
136 /* for (m = 0; m <= 3; m++) {
140 * for (i = 0; i <= 12; i++) {
142 * temp1 = SASR( x[m + 3*i], 2 );
144 * assert(temp1 != MIN_WORD);
146 * L_temp = GSM_L_MULT( temp1, temp1 );
147 * L_result = GSM_L_ADD( L_temp, L_result );
150 * if (L_result > EM) {
158 #define STEP( m, i ) L_temp = SASR( x[m + 3 * i], 2 ); \
159 L_result += L_temp * L_temp;
161 /* common part of 0 and 3 */
164 STEP( 0, 1 ); STEP( 0, 2 ); STEP( 0, 3 ); STEP( 0, 4 );
165 STEP( 0, 5 ); STEP( 0, 6 ); STEP( 0, 7 ); STEP( 0, 8 );
166 STEP( 0, 9 ); STEP( 0, 10); STEP( 0, 11); STEP( 0, 12);
167 L_common_0_3
= L_result
;
172 L_result
<<= 1; /* implicit in L_MULT */
179 STEP( 1, 1 ); STEP( 1, 2 ); STEP( 1, 3 ); STEP( 1, 4 );
180 STEP( 1, 5 ); STEP( 1, 6 ); STEP( 1, 7 ); STEP( 1, 8 );
181 STEP( 1, 9 ); STEP( 1, 10); STEP( 1, 11); STEP( 1, 12);
192 STEP( 2, 1 ); STEP( 2, 2 ); STEP( 2, 3 ); STEP( 2, 4 );
193 STEP( 2, 5 ); STEP( 2, 6 ); STEP( 2, 7 ); STEP( 2, 8 );
194 STEP( 2, 9 ); STEP( 2, 10); STEP( 2, 11); STEP( 2, 12);
203 L_result
= L_common_0_3
;
213 /* Down-sampling by a factor 3 to get the selected xM[0..12]
216 for (i
= 0; i
<= 12; i
++) xM
[i
] = x
[Mc
+ 3*i
];
222 static void APCM_quantization_xmaxc_to_exp_mant (
224 word
* exp_out
, /* OUT */
225 word
* mant_out
) /* OUT */
229 /* Compute exponent and mantissa of the decoded version of xmaxc
233 if (xmaxc
> 15) exp
= SASR(xmaxc
, 3) - 1;
234 mant
= xmaxc
- (exp
<< 3);
242 mant
= mant
<< 1 | 1;
248 assert( exp
>= -4 && exp
<= 6 );
249 assert( mant
>= 0 && mant
<= 7 );
255 static void APCM_quantization (
256 word
* xM
, /* [0..12] IN */
258 word
* xMc
, /* [0..12] OUT */
259 word
* mant_out
, /* OUT */
260 word
* exp_out
, /* OUT */
261 word
* xmaxc_out
/* OUT */
266 word xmax
, xmaxc
, temp
, temp1
, temp2
;
270 /* Find the maximum absolute value xmax of xM[0..12].
274 for (i
= 0; i
<= 12; i
++) {
276 temp
= GSM_ABS(temp
);
277 if (temp
> xmax
) xmax
= temp
;
280 /* Qantizing and coding of xmax to get xmaxc.
284 temp
= SASR( xmax
, 9 );
287 for (i
= 0; i
<= 5; i
++) {
289 itest
|= (temp
<= 0);
290 temp
= SASR( temp
, 1 );
293 if (itest
== 0) exp
++; /* exp = add (exp, 1) */
296 assert(exp
<= 6 && exp
>= 0);
299 assert(temp
<= 11 && temp
>= 0);
300 xmaxc
= gsm_add( SASR(xmax
, temp
), exp
<< 3 );
302 /* Quantizing and coding of the xM[0..12] RPE sequence
303 * to get the xMc[0..12]
306 APCM_quantization_xmaxc_to_exp_mant( xmaxc
, &exp
, &mant
);
308 /* This computation uses the fact that the decoded version of xmaxc
309 * can be calculated by using the exponent and the mantissa part of
310 * xmaxc (logarithmic table).
311 * So, this method avoids any division and uses only a scaling
312 * of the RPE samples by a function of the exponent. A direct
313 * multiplication by the inverse of the mantissa (NRFAC[0..7]
314 * found in table 4.5) gives the 3 bit coded version xMc[0..12]
315 * of the RPE samples.
319 /* Direct computation of xMc[0..12] using table 4.5
322 assert( exp
<= 4096 && exp
>= -4096);
323 assert( mant
>= 0 && mant
<= 7 );
325 temp1
= 6 - exp
; /* normalization by the exponent */
326 temp2
= gsm_NRFAC
[ mant
]; /* inverse mantissa */
328 for (i
= 0; i
<= 12; i
++) {
330 assert(temp1
>= 0 && temp1
< 16);
332 temp
= xM
[i
] << temp1
;
333 temp
= GSM_MULT( temp
, temp2
);
334 temp
= SASR(temp
, 12);
335 xMc
[i
] = temp
+ 4; /* see note below */
338 /* NOTE: This equation is used to make all the xMc[i] positive.
348 static void APCM_inverse_quantization (
349 register word
* xMc
, /* [0..12] IN */
352 register word
* xMp
) /* [0..12] OUT */
354 * This part is for decoding the RPE sequence of coded xMc[0..12]
355 * samples to obtain the xMp[0..12] array. Table 4.6 is used to get
356 * the mantissa of xmaxc (FAC[0..7]).
360 word temp
, temp1
, temp2
, temp3
;
363 assert( mant
>= 0 && mant
<= 7 );
365 temp1
= gsm_FAC
[ mant
]; /* see 4.2-15 for mant */
366 temp2
= gsm_sub( 6, exp
); /* see 4.2-15 for exp */
367 temp3
= gsm_asl( 1, gsm_sub( temp2
, 1 ));
371 assert( *xMc
<= 7 && *xMc
>= 0 ); /* 3 bit unsigned */
373 /* temp = gsm_sub( *xMc++ << 1, 7 ); */
374 temp
= (*xMc
++ << 1) - 7; /* restore sign */
375 assert( temp
<= 7 && temp
>= -7 ); /* 4 bit signed */
377 temp
<<= 12; /* 16 bit signed */
378 temp
= GSM_MULT_R( temp1
, temp
);
379 temp
= GSM_ADD( temp
, temp3
);
380 *xMp
++ = gsm_asr( temp
, temp2
);
386 static void RPE_grid_positioning (
387 word Mc
, /* grid position IN */
388 register word
* xMp
, /* [0..12] IN */
389 register word
* ep
/* [0..39] OUT */
392 * This procedure computes the reconstructed long term residual signal
393 * ep[0..39] for the LTP analysis filter. The inputs are the Mc
394 * which is the grid position selection and the xMp[0..12] decoded
395 * RPE samples which are upsampled by a factor of 3 by inserting zero
401 assert(0 <= Mc
&& Mc
<= 3);
408 case 0: *ep
++ = *xMp
++;
411 while (++Mc
< 4) *ep
++ = 0;
416 for (k = 0; k <= 39; k++) ep[k] = 0;
417 for (i = 0; i <= 12; i++) {
418 ep[ Mc + (3*i) ] = xMp[i];
425 /* This procedure adds the reconstructed long term residual signal
426 * ep[0..39] to the estimated signal dpp[0..39] from the long term
427 * analysis filter to compute the reconstructed short term residual
428 * signal dp[-40..-1]; also the reconstructed short term residual
429 * array dp[-120..-41] is updated.
432 #if 0 /* Has been inlined in code.c */
433 void Gsm_Update_of_reconstructed_short_time_residual_signal
P3((dpp
, ep
, dp
),
434 word
* dpp
, /* [0...39] IN */
435 word
* ep
, /* [0...39] IN */
436 word
* dp
) /* [-120...-1] IN/OUT */
440 for (k
= 0; k
<= 79; k
++)
441 dp
[ -120 + k
] = dp
[ -80 + k
];
443 for (k
= 0; k
<= 39; k
++)
444 dp
[ -40 + k
] = gsm_add( ep
[k
], dpp
[k
] );
446 #endif /* Has been inlined in code.c */
448 void Gsm_RPE_Encoding (
450 struct gsm_state
* S
,
452 word
* e
, /* -5..-1][0..39][40..44 IN/OUT */
453 word
* xmaxc
, /* OUT */
455 word
* xMc
) /* [0..12] OUT */
458 word xM
[13], xMp
[13];
461 (void)S
; /* Denotes intentionally unused */
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 (
473 struct gsm_state
* S
,
477 word
* xMcr
, /* [0..12], 3 bits IN */
478 word
* erp
/* [0..39] OUT */
484 (void)S
; /* Denotes intentionally unused */
485 APCM_quantization_xmaxc_to_exp_mant( xmaxcr
, &exp
, &mant
);
486 APCM_inverse_quantization( xMcr
, mant
, exp
, xMp
);
487 RPE_grid_positioning( Mcr
, xMp
, erp
);