Main Page | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

ulaw.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- A telephony toolkit for Linux.
00003  *
00004  * u-Law to Signed linear conversion
00005  * 
00006  * Copyright (C) 1999, Mark Spencer
00007  *
00008  * Mark Spencer <markster@linux-support.net>
00009  *
00010  * This program is free software, distributed under the terms of
00011  * the GNU General Public License
00012  */
00013 
00014 #include <asterisk/ulaw.h>
00015 
00016 #define ZEROTRAP    /* turn on the trap as per the MIL-STD */
00017 #define BIAS 0x84   /* define the add-in bias for 16 bit samples */
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   /* Get the sample into sign-magnitude. */
00046   sign = (sample >> 8) & 0x80;          /* set aside the sign */
00047   if (sign != 0) sample = -sample;              /* get magnitude */
00048   if (sample > CLIP) sample = CLIP;             /* clip the magnitude */
00049 
00050   /* Convert from 16 bit linear to ulaw. */
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;   /* optional CCITT trap */
00057 #endif
00058 
00059   return(ulawbyte);
00060 }
00061 
00062 void ast_ulaw_init(void)
00063 {
00064    int i;
00065    /* 
00066     *  Set up mu-law conversion table
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      /* set up the reverse (mu-law) conversion table */
00082    for(i = -32768; i < 32768; i++)
00083       {
00084       __ast_lin2mu[((unsigned short)i) >> 2] = linear2ulaw(i);
00085       }
00086 
00087 }
00088 

Generated on Sat Nov 25 19:09:50 2006 for Asterisk by  doxygen 1.4.2