Sat Aug 6 00:39:32 2011

Asterisk developer's documentation


ulaw.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: 40722 $")
00029 
00030 #include "asterisk/ulaw.h"
00031 
00032 #define ZEROTRAP    /*!< turn on the trap as per the MIL-STD */
00033 #define BIAS 0x84   /*!< define the add-in bias for 16 bit samples */
00034 #define CLIP 32635
00035 
00036 unsigned char __ast_lin2mu[16384];
00037 short __ast_mulaw[256];
00038 
00039 
00040 static unsigned char linear2ulaw(short sample)
00041 {
00042    static int exp_lut[256] = {
00043       0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,
00044       4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
00045       5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
00046       5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
00047       6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
00048       6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
00049       6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
00050       6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
00051       7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
00052       7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
00053       7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
00054       7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
00055       7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
00056       7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
00057       7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
00058       7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7 };
00059    int sign, exponent, mantissa;
00060    unsigned char ulawbyte;
00061 
00062    /* Get the sample into sign-magnitude. */
00063    sign = (sample >> 8) & 0x80;          /* set aside the sign */
00064    if (sign != 0) 
00065       sample = -sample;              /* get magnitude */
00066    if (sample > CLIP)
00067       sample = CLIP;             /* clip the magnitude */
00068 
00069    /* Convert from 16 bit linear to ulaw. */
00070    sample = sample + BIAS;
00071    exponent = exp_lut[(sample >> 7) & 0xFF];
00072    mantissa = (sample >> (exponent + 3)) & 0x0F;
00073    ulawbyte = ~(sign | (exponent << 4) | mantissa);
00074 #ifdef ZEROTRAP
00075    if (ulawbyte == 0)
00076       ulawbyte = 0x02;   /* optional CCITT trap */
00077 #endif
00078 
00079    return ulawbyte;
00080 }
00081 
00082 /*!
00083  * \brief  Set up mu-law conversion table
00084  */
00085 void ast_ulaw_init(void)
00086 {
00087    int i;
00088    for (i = 0; i < 256; i++) {
00089       short mu, e, f, y;
00090       static short etab[] = {0,132,396,924,1980,4092,8316,16764};
00091 
00092       mu = 255 - i;
00093       e = (mu & 0x70) / 16;
00094       f = mu & 0x0f;
00095       y = f * (1 << (e + 3));
00096       y += etab[e];
00097       if (mu & 0x80)
00098          y = -y;
00099       __ast_mulaw[i] = y;
00100    }
00101    /* set up the reverse (mu-law) conversion table */
00102    for (i = -32768; i < 32768; i++) {
00103       __ast_lin2mu[((unsigned short)i) >> 2] = linear2ulaw(i);
00104    }
00105 }
00106 

Generated on Sat Aug 6 00:39:32 2011 for Asterisk - the Open Source PBX by  doxygen 1.4.7