![]() |
Home page |
Mailing list |
Docs
Asterisk developer's documentation :: Codename Pineapple
alaw.c
Go to the documentation of this file.
00001 /* 00002 * Asterisk -- An open source telephony toolkit. 00003 * 00004 * Copyright (C) 1999 - 2005, Digium, Inc. 00005 * 00006 * Mark Spencer <markster@digium.com> 00007 * 00008 * See http://www.asterisk.org for more information about 00009 * the Asterisk project. Please do not directly contact 00010 * any of the maintainers of this project for assistance; 00011 * the project provides a web site, mailing lists and IRC 00012 * channels for your use. 00013 * 00014 * This program is free software, distributed under the terms of 00015 * the GNU General Public License Version 2. See the LICENSE file 00016 * at the top of the source tree. 00017 */ 00018 00019 /*! \file 00020 * 00021 * \brief u-Law to Signed linear conversion 00022 * 00023 * \author Mark Spencer <markster@digium.com> 00024 */ 00025 00026 #include "asterisk.h" 00027 00028 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 51499 $") 00029 00030 #include "asterisk/alaw.h" 00031 00032 #define AMI_MASK 0x55 00033 00034 static inline unsigned char linear2alaw (short int linear) 00035 { 00036 int mask; 00037 int seg; 00038 int pcm_val; 00039 static int seg_end[8] = 00040 { 00041 0xFF, 0x1FF, 0x3FF, 0x7FF, 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF 00042 }; 00043 00044 pcm_val = linear; 00045 if (pcm_val >= 0) { 00046 /* Sign (7th) bit = 1 */ 00047 mask = AMI_MASK | 0x80; 00048 } else { 00049 /* Sign bit = 0 */ 00050 mask = AMI_MASK; 00051 pcm_val = -pcm_val; 00052 } 00053 00054 /* Convert the scaled magnitude to segment number. */ 00055 for (seg = 0; seg < 8; seg++) { 00056 if (pcm_val <= seg_end[seg]) 00057 break; 00058 } 00059 /* Combine the sign, segment, and quantization bits. */ 00060 return ((seg << 4) | ((pcm_val >> ((seg) ? (seg + 3) : 4)) & 0x0F)) ^ mask; 00061 } 00062 /*- End of function --------------------------------------------------------*/ 00063 00064 static inline short int alaw2linear (unsigned char alaw) 00065 { 00066 int i; 00067 int seg; 00068 00069 alaw ^= AMI_MASK; 00070 i = ((alaw & 0x0F) << 4); 00071 seg = (((int) alaw & 0x70) >> 4); 00072 if (seg) 00073 i = (i + 0x100) << (seg - 1); 00074 return (short int) ((alaw & 0x80) ? i : -i); 00075 } 00076 00077 unsigned char __ast_lin2a[8192]; 00078 short __ast_alaw[256]; 00079 00080 void ast_alaw_init(void) 00081 { 00082 int i; 00083 /* 00084 * Set up mu-law conversion table 00085 */ 00086 for (i = 0; i < 256; i++) { 00087 __ast_alaw[i] = alaw2linear(i); 00088 } 00089 /* set up the reverse (mu-law) conversion table */ 00090 for (i = -32768; i < 32768; i++) { 00091 __ast_lin2a[((unsigned short)i) >> 3] = linear2alaw(i); 00092 } 00093 00094 } 00095