fix a flaw found while experimenting with structure alignment and padding; low-fence...
[asterisk-bristuff.git] / main / alaw.c
blobb94772ea6b40041b17c5f7b1ae3154fab8ed40cb
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/alaw.h"
32 #define AMI_MASK 0x55
34 static inline unsigned char linear2alaw (short int linear)
36 int mask;
37 int seg;
38 int pcm_val;
39 static int seg_end[8] =
41 0xFF, 0x1FF, 0x3FF, 0x7FF, 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF
44 pcm_val = linear;
45 if (pcm_val >= 0)
47 /* Sign (7th) bit = 1 */
48 mask = AMI_MASK | 0x80;
50 else
52 /* Sign bit = 0 */
53 mask = AMI_MASK;
54 pcm_val = -pcm_val;
57 /* Convert the scaled magnitude to segment number. */
58 for (seg = 0; seg < 8; seg++)
60 if (pcm_val <= seg_end[seg])
61 break;
63 /* Combine the sign, segment, and quantization bits. */
64 return ((seg << 4) | ((pcm_val >> ((seg) ? (seg + 3) : 4)) & 0x0F)) ^ mask;
66 /*- End of function --------------------------------------------------------*/
68 static inline short int alaw2linear (unsigned char alaw)
70 int i;
71 int seg;
73 alaw ^= AMI_MASK;
74 i = ((alaw & 0x0F) << 4) + 8 /* rounding error */;
75 seg = (((int) alaw & 0x70) >> 4);
76 if (seg)
77 i = (i + 0x100) << (seg - 1);
78 return (short int) ((alaw & 0x80) ? i : -i);
81 unsigned char __ast_lin2a[8192];
82 short __ast_alaw[256];
84 void ast_alaw_init(void)
86 int i;
87 /*
88 * Set up mu-law conversion table
90 for(i = 0;i < 256;i++)
92 __ast_alaw[i] = alaw2linear(i);
94 /* set up the reverse (mu-law) conversion table */
95 for(i = -32768; i < 32768; i++)
97 __ast_lin2a[((unsigned short)i) >> 3] = linear2alaw(i);