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
27 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
29 #include "asterisk/ulaw.h"
31 #define ZEROTRAP /*!< turn on the trap as per the MIL-STD */
32 #define BIAS 0x84 /*!< define the add-in bias for 16 bit samples */
35 unsigned char __ast_lin2mu[16384];
36 short __ast_mulaw[256];
39 static unsigned char linear2ulaw(short sample)
41 static int exp_lut[256] = {
42 0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,
43 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
44 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
45 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
46 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
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 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
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 int sign, exponent, mantissa;
59 unsigned char ulawbyte;
61 /* Get the sample into sign-magnitude. */
62 sign = (sample >> 8) & 0x80; /* set aside the sign */
64 sample = -sample; /* get magnitude */
66 sample = CLIP; /* clip the magnitude */
68 /* Convert from 16 bit linear to ulaw. */
69 sample = sample + BIAS;
70 exponent = exp_lut[(sample >> 7) & 0xFF];
71 mantissa = (sample >> (exponent + 3)) & 0x0F;
72 ulawbyte = ~(sign | (exponent << 4) | mantissa);
75 ulawbyte = 0x02; /* optional CCITT trap */
82 * \brief Set up mu-law conversion table
84 void ast_ulaw_init(void)
87 for(i = 0;i < 256;i++) {
89 static short etab[]={0,132,396,924,1980,4092,8316,16764};
94 y = f * (1 << (e + 3));
96 if (mu & 0x80) y = -y;
99 /* set up the reverse (mu-law) conversion table */
100 for(i = -32768; i < 32768; i++) {
101 __ast_lin2mu[((unsigned short)i) >> 2] = linear2ulaw(i);