Asterisk - The Open Source Telephony Project  18.5.0
Macros | Functions
rpe.c File Reference
#include <stdio.h>
#include <assert.h>
#include "private.h"
#include "gsm.h"
#include "proto.h"
Include dependency graph for rpe.c:

Go to the source code of this file.

Macros

#define STEP(i, H)   (e[ k + i ] * (longword)H)
 
#define STEP(m, i)
 

Functions

static void Weighting_filter P2 ((e, x), register word *e, word *x)
 
static void RPE_grid_selection P3 ((x, xM, Mc_out), word *x, word *xM, word *Mc_out)
 
static void APCM_quantization_xmaxc_to_exp_mant P3 ((xmaxc, exp_out, mant_out), word xmaxc, word *exp_out, word *mant_out)
 
static void RPE_grid_positioning P3 ((Mc, xMp, ep), word Mc, register word *xMp, register word *ep)
 
static void APCM_inverse_quantization P4 ((xMc, mant, exp, xMp), register word *xMc, word mant, word exp, register word *xMp)
 
static void APCM_quantization P5 ((xM, xMc, mant_out, exp_out, xmaxc_out), word *xM, word *xMc, word *mant_out, word *exp_out, word *xmaxc_out)
 
void Gsm_RPE_Encoding P5 ((S, e, xmaxc, Mc, xMc), struct gsm_state *S, word *e, word *xmaxc, word *Mc, word *xMc)
 
void Gsm_RPE_Decoding P5 ((S, xmaxcr, Mcr, xMcr, erp), struct gsm_state *S, word xmaxcr, word Mcr, word *xMcr, word *erp)
 

Macro Definition Documentation

◆ STEP [1/2]

#define STEP (   i,
 
)    (e[ k + i ] * (longword)H)

Referenced by P2(), and P3().

◆ STEP [2/2]

#define STEP (   m,
 
)
Value:
L_temp = SASR( x[m + 3 * i], 2 ); \
L_result += L_temp * L_temp;
#define SASR(x, by)

Function Documentation

◆ P2()

static void Weighting_filter P2 ( (e, x)  ,
register word e,
word x 
)
static

Definition at line 24 of file rpe.c.

References MAX_WORD, MIN_WORD, SASR, and STEP.

34 {
35  /* word wt[ 50 ]; */
36 
37  register longword L_result;
38  register int k /* , i */ ;
39 
40  /* Initialization of a temporary working array wt[0...49]
41  */
42 
43  /* for (k = 0; k <= 4; k++) wt[k] = 0;
44  * for (k = 5; k <= 44; k++) wt[k] = *e++;
45  * for (k = 45; k <= 49; k++) wt[k] = 0;
46  *
47  * (e[-5..-1] and e[40..44] are allocated by the caller,
48  * are initially zero and are not written anywhere.)
49  */
50  e -= 5;
51 
52  /* Compute the signal x[0..39]
53  */
54  for (k = 0; k <= 39; k++) {
55 
56  L_result = 8192 >> 1;
57 
58  /* for (i = 0; i <= 10; i++) {
59  * L_temp = GSM_L_MULT( wt[k+i], gsm_H[i] );
60  * L_result = GSM_L_ADD( L_result, L_temp );
61  * }
62  */
63 
64 #undef STEP
65 #define STEP( i, H ) (e[ k + i ] * (longword)H)
66 
67  /* Every one of these multiplications is done twice --
68  * but I don't see an elegant way to optimize this.
69  * Do you?
70  */
71 
72 #ifdef STUPID_COMPILER
73  L_result += STEP( 0, -134 ) ;
74  L_result += STEP( 1, -374 ) ;
75  /* + STEP( 2, 0 ) */
76  L_result += STEP( 3, 2054 ) ;
77  L_result += STEP( 4, 5741 ) ;
78  L_result += STEP( 5, 8192 ) ;
79  L_result += STEP( 6, 5741 ) ;
80  L_result += STEP( 7, 2054 ) ;
81  /* + STEP( 8, 0 ) */
82  L_result += STEP( 9, -374 ) ;
83  L_result += STEP( 10, -134 ) ;
84 #else
85  L_result +=
86  STEP( 0, -134 )
87  + STEP( 1, -374 )
88  /* + STEP( 2, 0 ) */
89  + STEP( 3, 2054 )
90  + STEP( 4, 5741 )
91  + STEP( 5, 8192 )
92  + STEP( 6, 5741 )
93  + STEP( 7, 2054 )
94  /* + STEP( 8, 0 ) */
95  + STEP( 9, -374 )
96  + STEP(10, -134 )
97  ;
98 #endif
99 
100  /* L_result = GSM_L_ADD( L_result, L_result ); (* scaling(x2) *)
101  * L_result = GSM_L_ADD( L_result, L_result ); (* scaling(x4) *)
102  *
103  * x[k] = SASR( L_result, 16 );
104  */
105 
106  /* 2 adds vs. >>16 => 14, minus one shift to compensate for
107  * those we lost when replacing L_MULT by '*'.
108  */
109 
110  L_result = SASR( L_result, 13 );
111  x[k] = (word)( L_result < MIN_WORD ? MIN_WORD
112  : (L_result > MAX_WORD ? MAX_WORD : L_result ));
113  }
114 }
#define STEP(i, H)
#define MAX_WORD
#define SASR(x, by)
#define MIN_WORD
long longword
short word

◆ P3() [1/3]

static void RPE_grid_selection P3 ( (x, xM, Mc_out)  ,
word x,
word xM,
word Mc_out 
)
static

Definition at line 119 of file rpe.c.

References Mc, and STEP.

Referenced by P3().

128 {
129  /* register word temp1; */
130  register int /* m, */ i;
131  register longword L_result, L_temp;
132  longword EM; /* xxx should be L_EM? */
133  word Mc;
134 
135  longword L_common_0_3;
136 
137  EM = 0;
138  Mc = 0;
139 
140  /* for (m = 0; m <= 3; m++) {
141  * L_result = 0;
142  *
143  *
144  * for (i = 0; i <= 12; i++) {
145  *
146  * temp1 = SASR( x[m + 3*i], 2 );
147  *
148  * assert(temp1 != MIN_WORD);
149  *
150  * L_temp = GSM_L_MULT( temp1, temp1 );
151  * L_result = GSM_L_ADD( L_temp, L_result );
152  * }
153  *
154  * if (L_result > EM) {
155  * Mc = m;
156  * EM = L_result;
157  * }
158  * }
159  */
160 
161 #undef STEP
162 #define STEP( m, i ) L_temp = SASR( x[m + 3 * i], 2 ); \
163  L_result += L_temp * L_temp;
164 
165  /* common part of 0 and 3 */
166 
167  L_result = 0;
168  STEP( 0, 1 ); STEP( 0, 2 ); STEP( 0, 3 ); STEP( 0, 4 );
169  STEP( 0, 5 ); STEP( 0, 6 ); STEP( 0, 7 ); STEP( 0, 8 );
170  STEP( 0, 9 ); STEP( 0, 10); STEP( 0, 11); STEP( 0, 12);
171  L_common_0_3 = L_result;
172 
173  /* i = 0 */
174 
175  STEP( 0, 0 );
176  L_result <<= 1; /* implicit in L_MULT */
177  EM = L_result;
178 
179  /* i = 1 */
180 
181  L_result = 0;
182  STEP( 1, 0 );
183  STEP( 1, 1 ); STEP( 1, 2 ); STEP( 1, 3 ); STEP( 1, 4 );
184  STEP( 1, 5 ); STEP( 1, 6 ); STEP( 1, 7 ); STEP( 1, 8 );
185  STEP( 1, 9 ); STEP( 1, 10); STEP( 1, 11); STEP( 1, 12);
186  L_result <<= 1;
187  if (L_result > EM) {
188  Mc = 1;
189  EM = L_result;
190  }
191 
192  /* i = 2 */
193 
194  L_result = 0;
195  STEP( 2, 0 );
196  STEP( 2, 1 ); STEP( 2, 2 ); STEP( 2, 3 ); STEP( 2, 4 );
197  STEP( 2, 5 ); STEP( 2, 6 ); STEP( 2, 7 ); STEP( 2, 8 );
198  STEP( 2, 9 ); STEP( 2, 10); STEP( 2, 11); STEP( 2, 12);
199  L_result <<= 1;
200  if (L_result > EM) {
201  Mc = 2;
202  EM = L_result;
203  }
204 
205  /* i = 3 */
206 
207  L_result = L_common_0_3;
208  STEP( 3, 12 );
209  L_result <<= 1;
210  if (L_result > EM) {
211  Mc = 3;
212  EM = L_result;
213  }
214 
215  /**/
216 
217  /* Down-sampling by a factor 3 to get the selected xM[0..12]
218  * RPE sequence.
219  */
220  for (i = 0; i <= 12; i ++) xM[i] = x[Mc + 3*i];
221  *Mc_out = Mc;
222 }
#define Mc
#define STEP(i, H)
long longword
short word

◆ P3() [2/3]

static void APCM_quantization_xmaxc_to_exp_mant P3 ( (xmaxc, exp_out, mant_out)  ,
word  xmaxc,
word exp_out,
word mant_out 
)
static

Definition at line 226 of file rpe.c.

References SASR.

230 {
231  word exp, mant;
232 
233  /* Compute exponent and mantissa of the decoded version of xmaxc
234  */
235 
236  exp = 0;
237  if (xmaxc > 15) exp = SASR(xmaxc, 3) - 1;
238  mant = xmaxc - (exp << 3);
239 
240  if (mant == 0) {
241  exp = -4;
242  mant = 7;
243  }
244  else {
245  while (mant <= 7) {
246  mant = mant << 1 | 1;
247  exp--;
248  }
249  mant -= 8;
250  }
251 
252  assert( exp >= -4 && exp <= 6 );
253  assert( mant >= 0 && mant <= 7 );
254 
255  *exp_out = exp;
256  *mant_out = mant;
257 }
#define SASR(x, by)
#define xmaxc
short word

◆ P3() [3/3]

static void RPE_grid_positioning P3 ( (Mc, xMp, ep)  ,
word  Mc,
register word xMp,
register word ep 
)
static

Definition at line 389 of file rpe.c.

References P3().

401 {
402  int i = 13;
403 
404  assert(0 <= Mc && Mc <= 3);
405 
406  switch (Mc) {
407  case 3: *ep++ = 0;
408  case 2: do {
409  *ep++ = 0;
410  case 1: *ep++ = 0;
411  case 0: *ep++ = *xMp++;
412  } while (--i);
413  }
414  while (++Mc < 4) *ep++ = 0;
415 
416  /*
417 
418  int i, k;
419  for (k = 0; k <= 39; k++) ep[k] = 0;
420  for (i = 0; i <= 12; i++) {
421  ep[ Mc + (3*i) ] = xMp[i];
422  }
423  */
424 }
#define Mc

◆ P4()

static void APCM_inverse_quantization P4 ( (xMc, mant, exp, xMp)  ,
register word xMc,
word  mant,
word  exp,
register word xMp 
)
static

Definition at line 352 of file rpe.c.

References GSM_ADD(), gsm_FAC, and GSM_MULT_R.

362 {
363  int i;
364  word temp, temp1, temp2, temp3;
365 
366  assert( mant >= 0 && mant <= 7 );
367 
368  temp1 = gsm_FAC[ mant ]; /* see 4.2-15 for mant */
369  temp2 = gsm_sub( 6, exp ); /* see 4.2-15 for exp */
370  temp3 = gsm_asl( 1, gsm_sub( temp2, 1 ));
371 
372  for (i = 13; i--;) {
373 
374  assert( *xMc <= 7 && *xMc >= 0 ); /* 3 bit unsigned */
375 
376  /* temp = gsm_sub( *xMc++ << 1, 7 ); */
377  temp = (*xMc++ << 1) - 7; /* restore sign */
378  assert( temp <= 7 && temp >= -7 ); /* 4 bit signed */
379 
380  temp <<= 12; /* 16 bit signed */
381  temp = (word)GSM_MULT_R( temp1, temp );
382  temp = GSM_ADD( temp, temp3 );
383  *xMp++ = gsm_asr( temp, temp2 );
384  }
385 }
static word GSM_ADD(longword a, longword b)
#define GSM_MULT_R(a, b)
word gsm_FAC[8]
Definition: table.c:63
short word

◆ P5() [1/3]

static void APCM_quantization P5 ( (xM, xMc, mant_out, exp_out, xmaxc_out)  ,
word xM,
word xMc,
word mant_out,
word exp_out,
word xmaxc_out 
)
static

Definition at line 259 of file rpe.c.

References GSM_ABS, GSM_MULT, gsm_NRFAC, SASR, and xmaxc.

267 {
268  int i, itest;
269 
270  word xmax, xmaxc, temp, temp1, temp2;
271  word exp, mant;
272 
273 
274  /* Find the maximum absolute value xmax of xM[0..12].
275  */
276 
277  xmax = 0;
278  for (i = 0; i <= 12; i++) {
279  temp = xM[i];
280  temp = GSM_ABS(temp);
281  if (temp > xmax) xmax = temp;
282  }
283 
284  /* Qantizing and coding of xmax to get xmaxc.
285  */
286 
287  exp = 0;
288  temp = SASR( xmax, 9 );
289  itest = 0;
290 
291  for (i = 0; i <= 5; i++) {
292 
293  itest |= (temp <= 0);
294  temp = SASR( temp, 1 );
295 
296  assert(exp <= 5);
297  if (itest == 0) exp++; /* exp = add (exp, 1) */
298  }
299 
300  assert(exp <= 6 && exp >= 0);
301  temp = exp + 5;
302 
303  assert(temp <= 11 && temp >= 0);
304  xmaxc = gsm_add( SASR(xmax, temp), exp << 3 );
305 
306  /* Quantizing and coding of the xM[0..12] RPE sequence
307  * to get the xMc[0..12]
308  */
309 
310  APCM_quantization_xmaxc_to_exp_mant( xmaxc, &exp, &mant );
311 
312  /* This computation uses the fact that the decoded version of xmaxc
313  * can be calculated by using the exponent and the mantissa part of
314  * xmaxc (logarithmic table).
315  * So, this method avoids any division and uses only a scaling
316  * of the RPE samples by a function of the exponent. A direct
317  * multiplication by the inverse of the mantissa (NRFAC[0..7]
318  * found in table 4.5) gives the 3 bit coded version xMc[0..12]
319  * of the RPE samples.
320  */
321 
322 
323  /* Direct computation of xMc[0..12] using table 4.5
324  */
325 
326  assert( exp <= 4096 && exp >= -4096);
327  assert( mant >= 0 && mant <= 7 );
328 
329  temp1 = 6 - exp; /* normalization by the exponent */
330  temp2 = gsm_NRFAC[ mant ]; /* inverse mantissa */
331 
332  for (i = 0; i <= 12; i++) {
333 
334  assert(temp1 >= 0 && temp1 < 16);
335 
336  temp = xM[i] << temp1;
337  temp = (word)GSM_MULT( temp, temp2 );
338  temp = SASR(temp, 12);
339  xMc[i] = temp + 4; /* see note below */
340  }
341 
342  /* NOTE: This equation is used to make all the xMc[i] positive.
343  */
344 
345  *mant_out = mant;
346  *exp_out = exp;
347  *xmaxc_out = xmaxc;
348 }
#define SASR(x, by)
word gsm_NRFAC[8]
Definition: table.c:57
#define GSM_MULT(a, b)
#define xmaxc
#define GSM_ABS(a)
short word

◆ P5() [2/3]

void Gsm_RPE_Encoding P5 ( (S, e, xmaxc, Mc, xMc)  ,
struct gsm_state S,
word e,
word xmaxc,
word Mc,
word xMc 
)

Definition at line 451 of file rpe.c.

459 {
460  word x[40];
461  word xM[13], xMp[13];
462  word mant, exp;
463 
464  Weighting_filter(e, x);
465  RPE_grid_selection(x, xM, Mc);
466 
467  APCM_quantization( xM, xMc, &mant, &exp, xmaxc);
468  APCM_inverse_quantization( xMc, mant, exp, xMp);
469 
470  RPE_grid_positioning( *Mc, xMp, e );
471 
472 }
#define Mc
#define xmaxc
short word

◆ P5() [3/3]

void Gsm_RPE_Decoding P5 ( (S, xmaxcr, Mcr, xMcr, erp)  ,
struct gsm_state S,
word  xmaxcr,
word  Mcr,
word xMcr,
word erp 
)

Definition at line 474 of file rpe.c.

482 {
483  word exp, mant;
484  word xMp[ 13 ];
485 
486  APCM_quantization_xmaxc_to_exp_mant( xmaxcr, &exp, &mant );
487  APCM_inverse_quantization( xMcr, mant, exp, xMp );
488  RPE_grid_positioning( Mcr, xMp, erp );
489 
490 }
short word