Wed Jan 8 2020 09:50:21

Asterisk developer's documentation


ulaw.c File Reference

u-Law to Signed linear conversion More...

#include "asterisk.h"
#include "asterisk/ulaw.h"
#include "asterisk/logger.h"

Go to the source code of this file.

Macros

#define BIAS   0x84
 
#define CLIP   32635
 

Functions

void ast_ulaw_init (void)
 Set up mu-law conversion table. More...
 
static unsigned char linear2ulaw (short sample)
 

Variables

unsigned char __ast_lin2mu [16384]
 converts signed linear to mulaw More...
 
short __ast_mulaw [256]
 

Detailed Description

u-Law to Signed linear conversion

Author
Mark Spencer marks.nosp@m.ter@.nosp@m.digiu.nosp@m.m.co.nosp@m.m

Definition in file ulaw.c.

Macro Definition Documentation

#define BIAS   0x84

define the add-in bias for 16 bit samples

Definition at line 46 of file ulaw.c.

Referenced by linear2ulaw().

#define CLIP   32635

Definition at line 47 of file ulaw.c.

Referenced by linear2ulaw().

Function Documentation

void ast_ulaw_init ( void  )

Set up mu-law conversion table.

To init the ulaw to slinear conversion stuff, this needs to be run.

Definition at line 175 of file ulaw.c.

References AST_LIN2MU, ast_log(), AST_MULAW, AST_ULAW_STEP, f, linear2ulaw(), LOG_NOTICE, and LOG_WARNING.

Referenced by load_module(), and main().

176 {
177  int i;
178 
179  /*
180  * Set up mu-law conversion table
181  */
182 #ifndef G711_NEW_ALGORITHM
183  for (i = 0;i < 256;i++) {
184  short mu,e,f,y;
185  static const short etab[]={0,132,396,924,1980,4092,8316,16764};
186 
187  mu = 255-i;
188  e = (mu & 0x70)/16;
189  f = mu & 0x0f;
190  y = f * (1 << (e + 3));
191  y += etab[e];
192  if (mu & 0x80) y = -y;
193  __ast_mulaw[i] = y;
194  }
195  /* set up the reverse (mu-law) conversion table */
196  for (i = -32768; i < 32768; i++) {
197  __ast_lin2mu[((unsigned short)i) >> 2] = linear2ulaw(i);
198  }
199 #else
200 
201  for (i = 0; i < 256; i++) {
202  __ast_mulaw[i] = ulaw2linear(i);
203  }
204  /* set up the reverse (mu-law) conversion table */
205  for (i = 0; i <= 32768; i += AST_ULAW_STEP) {
206  AST_LIN2MU_LOOKUP(i) = linear2ulaw(i, 0 /* half-cooked */);
207  }
208 #endif
209 
210 #ifdef TEST_CODING_TABLES
211  for (i = -32768; i < 32768; ++i) {
212 #ifndef G711_NEW_ALGORITHM
213  unsigned char e1 = linear2ulaw(i);
214 #else
215  unsigned char e1 = linear2ulaw(i, 1);
216 #endif
217  short d1 = ulaw2linear(e1);
218  unsigned char e2 = AST_LIN2MU(i);
219  short d2 = ulaw2linear(e2);
220  short d3 = AST_MULAW(e1);
221 
222  if (e1 != e2 || d1 != d3 || d2 != d3) {
223  ast_log(LOG_WARNING, "u-Law coding tables test failed on %d: e1=%u, e2=%u, d1=%d, d2=%d\n",
224  i, (unsigned)e1, (unsigned)e2, (int)d1, (int)d2);
225  }
226  }
227  ast_log(LOG_NOTICE, "u-Law coding table test complete.\n");
228 #endif /* TEST_CODING_TABLES */
229 
230 #ifdef TEST_TANDEM_TRANSCODING
231  /* tandem transcoding test */
232  for (i = -32768; i < 32768; ++i) {
233  unsigned char e1 = AST_LIN2MU(i);
234  short d1 = AST_MULAW(e1);
235  unsigned char e2 = AST_LIN2MU(d1);
236  short d2 = AST_MULAW(e2);
237  unsigned char e3 = AST_LIN2MU(d2);
238  short d3 = AST_MULAW(e3);
239 
240  if (i < 0 && e1 == 0x7f && e2 == 0xff && e3 == 0xff)
241  continue; /* known and normal negative 0 case */
242 
243  if (e1 != e2 || e2 != e3 || d1 != d2 || d2 != d3) {
244  ast_log(LOG_WARNING, "u-Law tandem transcoding test failed on %d: e1=%u, e2=%u, d1=%d, d2=%d, d3=%d\n",
245  i, (unsigned)e1, (unsigned)e2, (int)d1, (int)d2, (int)d3);
246  }
247  }
248  ast_log(LOG_NOTICE, "u-Law tandem transcoding test complete.\n");
249 #endif /* TEST_TANDEM_TRANSCODING */
250 }
static unsigned char linear2ulaw(short sample)
Definition: ulaw.c:54
#define LOG_WARNING
Definition: logger.h:144
short __ast_mulaw[256]
Definition: ulaw.c:52
#define AST_MULAW(a)
Definition: ulaw.h:85
#define AST_ULAW_STEP
Definition: ulaw.h:33
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
#define LOG_NOTICE
Definition: logger.h:133
unsigned char __ast_lin2mu[16384]
converts signed linear to mulaw
Definition: ulaw.c:51
static struct ast_format f[]
Definition: format_g726.c:181
#define AST_LIN2MU(a)
Definition: ulaw.h:49
static unsigned char linear2ulaw ( short  sample)
static

Definition at line 54 of file ulaw.c.

References BIAS, and CLIP.

Referenced by ast_ulaw_init().

55 {
56  static int exp_lut[256] = {
57  0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,
58  4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
59  5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
60  5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
61  6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
62  6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
63  6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
64  6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
65  7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
66  7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
67  7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
68  7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
69  7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
70  7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
71  7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
72  7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7 };
73  int sign, exponent, mantissa;
74  unsigned char ulawbyte;
75 
76  /* Get the sample into sign-magnitude. */
77  sign = (sample >> 8) & 0x80; /* set aside the sign */
78  if (sign != 0)
79  sample = -sample; /* get magnitude */
80  if (sample > CLIP)
81  sample = CLIP; /* clip the magnitude */
82 
83  /* Convert from 16 bit linear to ulaw. */
84  sample = sample + BIAS;
85  exponent = exp_lut[(sample >> 7) & 0xFF];
86  mantissa = (sample >> (exponent + 3)) & 0x0F;
87  ulawbyte = ~(sign | (exponent << 4) | mantissa);
88 
89 #ifdef ZEROTRAP
90  if (ulawbyte == 0)
91  ulawbyte = 0x02; /* optional CCITT trap */
92 #endif
93 
94  return ulawbyte;
95 }
#define BIAS
Definition: ulaw.c:46
#define CLIP
Definition: ulaw.c:47

Variable Documentation

unsigned char __ast_lin2mu[16384]

converts signed linear to mulaw

Definition at line 51 of file ulaw.c.

short __ast_mulaw[256]

help

Definition at line 52 of file ulaw.c.