00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include <asterisk/ulaw.h>
00015
00016 #define ZEROTRAP
00017 #define BIAS 0x84
00018 #define CLIP 32635
00019
00020 unsigned char __ast_lin2mu[16384];
00021 short __ast_mulaw[256];
00022
00023 static unsigned char
00024 linear2ulaw(short sample)
00025 {
00026 static int exp_lut[256] = {0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,
00027 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
00028 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
00029 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
00030 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
00031 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
00032 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
00033 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
00034 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
00035 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
00036 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
00037 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
00038 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
00039 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
00040 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
00041 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7};
00042 int sign, exponent, mantissa;
00043 unsigned char ulawbyte;
00044
00045
00046 sign = (sample >> 8) & 0x80;
00047 if (sign != 0) sample = -sample;
00048 if (sample > CLIP) sample = CLIP;
00049
00050
00051 sample = sample + BIAS;
00052 exponent = exp_lut[(sample >> 7) & 0xFF];
00053 mantissa = (sample >> (exponent + 3)) & 0x0F;
00054 ulawbyte = ~(sign | (exponent << 4) | mantissa);
00055 #ifdef ZEROTRAP
00056 if (ulawbyte == 0) ulawbyte = 0x02;
00057 #endif
00058
00059 return(ulawbyte);
00060 }
00061
00062 void ast_ulaw_init(void)
00063 {
00064 int i;
00065
00066
00067
00068 for(i = 0;i < 256;i++)
00069 {
00070 short mu,e,f,y;
00071 static short etab[]={0,132,396,924,1980,4092,8316,16764};
00072
00073 mu = 255-i;
00074 e = (mu & 0x70)/16;
00075 f = mu & 0x0f;
00076 y = f * (1 << (e + 3));
00077 y += etab[e];
00078 if (mu & 0x80) y = -y;
00079 __ast_mulaw[i] = y;
00080 }
00081
00082 for(i = -32768; i < 32768; i++)
00083 {
00084 __ast_lin2mu[((unsigned short)i) >> 2] = linear2ulaw(i);
00085 }
00086
00087 }
00088