clarify the fact that voicemail IMAP storage cannot be built against a distro's binar...
[asterisk-bristuff.git] / main / ulaw.c
blob2735f6ccee2639d4d6b878e7f8c0c728b3e78365
1 /*
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.
19 /*! \file
21 * \brief u-Law to Signed linear conversion
23 * \author Mark Spencer <markster@digium.com>
26 #include "asterisk.h"
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 */
34 #define CLIP 32635
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 */
64 if (sign != 0)
65 sample = -sample; /* get magnitude */
66 if (sample > CLIP)
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);
74 #ifdef ZEROTRAP
75 if (ulawbyte == 0)
76 ulawbyte = 0x02; /* optional CCITT trap */
77 #endif
79 return ulawbyte;
82 /*!
83 * \brief Set up mu-law conversion table
85 void ast_ulaw_init(void)
87 int i;
88 for (i = 0; i < 256; i++) {
89 short mu, e, f, y;
90 static short etab[] = {0,132,396,924,1980,4092,8316,16764};
92 mu = 255 - i;
93 e = (mu & 0x70) / 16;
94 f = mu & 0x0f;
95 y = f * (1 << (e + 3));
96 y += etab[e];
97 if (mu & 0x80)
98 y = -y;
99 __ast_mulaw[i] = y;
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);