Asterisk - The Open Source Telephony Project  18.5.0
Macros | Functions | Variables
ulaw.c File Reference

u-Law to Signed linear conversion More...

#include "asterisk.h"
#include "asterisk/ulaw.h"
#include "asterisk/logger.h"
Include dependency graph for ulaw.c:

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

◆ BIAS

#define BIAS   0x84

define the add-in bias for 16 bit samples

Definition at line 44 of file ulaw.c.

Referenced by linear2ulaw().

◆ CLIP

#define CLIP   32635

Definition at line 45 of file ulaw.c.

Referenced by linear2ulaw().

Function Documentation

◆ ast_ulaw_init()

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 173 of file ulaw.c.

References __ast_lin2mu, __ast_mulaw, AST_LIN2MU, ast_log, AST_MULAW, AST_ULAW_STEP, ast_udptl::f, linear2ulaw(), LOG_NOTICE, and LOG_WARNING.

Referenced by asterisk_daemon().

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

◆ linear2ulaw()

static unsigned char linear2ulaw ( short  sample)
static

Definition at line 52 of file ulaw.c.

References __ast_lin2mu, __ast_mulaw, AST_ULAW_TAB_SIZE, BIAS, and CLIP.

Referenced by ast_ulaw_init().

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

Variable Documentation

◆ __ast_lin2mu

unsigned char __ast_lin2mu[16384]

converts signed linear to mulaw

Definition at line 49 of file ulaw.c.

Referenced by ast_ulaw_init(), and linear2ulaw().

◆ __ast_mulaw

short __ast_mulaw[256]

help

Definition at line 50 of file ulaw.c.

Referenced by ast_ulaw_init(), and linear2ulaw().