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

alaw.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/alaw.h>
00015 
00016 #define AMI_MASK 0x55
00017 
00018 static inline unsigned char linear2alaw (short int linear)
00019 {
00020     int mask;
00021     int seg;
00022     int pcm_val;
00023     static int seg_end[8] =
00024     {
00025          0xFF, 0x1FF, 0x3FF, 0x7FF, 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF
00026     };
00027     
00028     pcm_val = linear;
00029     if (pcm_val >= 0)
00030     {
00031         /* Sign (7th) bit = 1 */
00032         mask = AMI_MASK | 0x80;
00033     }
00034     else
00035     {
00036         /* Sign bit = 0 */
00037         mask = AMI_MASK;
00038         pcm_val = -pcm_val;
00039     }
00040 
00041     /* Convert the scaled magnitude to segment number. */
00042     for (seg = 0;  seg < 8;  seg++)
00043     {
00044         if (pcm_val <= seg_end[seg])
00045        break;
00046     }
00047     /* Combine the sign, segment, and quantization bits. */
00048     return  ((seg << 4) | ((pcm_val >> ((seg)  ?  (seg + 3)  :  4)) & 0x0F)) ^ mask;
00049 }
00050 /*- End of function --------------------------------------------------------*/
00051 
00052 static inline short int alaw2linear (unsigned char alaw)
00053 {
00054     int i;
00055     int seg;
00056 
00057     alaw ^= AMI_MASK;
00058     i = ((alaw & 0x0F) << 4);
00059     seg = (((int) alaw & 0x70) >> 4);
00060     if (seg)
00061         i = (i + 0x100) << (seg - 1);
00062     return (short int) ((alaw & 0x80)  ?  i  :  -i);
00063 }
00064 
00065 unsigned char __ast_lin2a[8192];
00066 short __ast_alaw[256];
00067 
00068 void ast_alaw_init(void)
00069 {
00070    int i;
00071    /* 
00072     *  Set up mu-law conversion table
00073     */
00074    for(i = 0;i < 256;i++)
00075       {
00076            __ast_alaw[i] = alaw2linear(i);
00077       }
00078      /* set up the reverse (mu-law) conversion table */
00079    for(i = -32768; i < 32768; i++)
00080       {
00081       __ast_lin2a[((unsigned short)i) >> 3] = linear2alaw(i);
00082       }
00083 
00084 }
00085 

Generated on Fri Oct 31 07:05:04 2003 for Asterisk by doxygen 1.3.4