2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief u-Law to Signed linear conversion
23 * \author Mark Spencer <markster@digium.com>
28 ASTERISK_FILE_VERSION(__FILE__
, "$Revision$")
30 #include "asterisk/ulaw.h"
32 #define ZEROTRAP /*!< turn on the trap as per the MIL-STD */
33 #define BIAS 0x84 /*!< define the add-in bias for 16 bit samples */
36 unsigned char __ast_lin2mu
[16384];
37 short __ast_mulaw
[256];
40 static unsigned char linear2ulaw(short sample
)
42 static int exp_lut
[256] = {
43 0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,
44 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
45 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
46 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
47 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
48 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
49 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
50 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
51 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
52 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
53 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
54 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
55 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
56 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
57 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
58 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7 };
59 int sign
, exponent
, mantissa
;
60 unsigned char ulawbyte
;
62 /* Get the sample into sign-magnitude. */
63 sign
= (sample
>> 8) & 0x80; /* set aside the sign */
65 sample
= -sample
; /* get magnitude */
67 sample
= CLIP
; /* clip the magnitude */
69 /* Convert from 16 bit linear to ulaw. */
70 sample
= sample
+ BIAS
;
71 exponent
= exp_lut
[(sample
>> 7) & 0xFF];
72 mantissa
= (sample
>> (exponent
+ 3)) & 0x0F;
73 ulawbyte
= ~(sign
| (exponent
<< 4) | mantissa
);
76 ulawbyte
= 0x02; /* optional CCITT trap */
83 * \brief Set up mu-law conversion table
85 void ast_ulaw_init(void)
88 for (i
= 0; i
< 256; i
++) {
90 static short etab
[] = {0,132,396,924,1980,4092,8316,16764};
95 y
= f
* (1 << (e
+ 3));
101 /* set up the reverse (mu-law) conversion table */
102 for (i
= -32768; i
< 32768; i
++) {
103 __ast_lin2mu
[((unsigned short)i
) >> 2] = linear2ulaw(i
);