g711.h

Go to the documentation of this file.
00001 /*
00002  * SpanDSP - a series of DSP components for telephony
00003  *
00004  * g711.h - In line A-law and u-law conversion routines
00005  *
00006  * Written by Steve Underwood <steveu@coppice.org>
00007  *
00008  * Copyright (C) 2001 Steve Underwood
00009  *
00010  * All rights reserved.
00011  *
00012  * This program is free software; you can redistribute it and/or modify
00013  * it under the terms of the GNU General Public License version 2, or
00014  * the Lesser GNU General Public License version 2.1, as published by
00015  * the Free Software Foundation.
00016  *
00017  * This program is distributed in the hope that it will be useful,
00018  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00019  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020  * GNU General Public License for more details.
00021  *
00022  * You should have received a copy of the GNU General Public License
00023  * along with this program; if not, write to the Free Software
00024  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00025  *
00026  * $Id: g711.h,v 1.9 2008/02/09 15:32:26 steveu Exp $
00027  */
00028 
00029 /*! \file */
00030 
00031 /*! \page g711_page A-law and mu-law handling
00032 Lookup tables for A-law and u-law look attractive, until you consider the impact
00033 on the CPU cache. If it causes a substantial area of your processor cache to get
00034 hit too often, cache sloshing will severely slow things down. The main reason
00035 these routines are slow in C, is the lack of direct access to the CPU's "find
00036 the first 1" instruction. A little in-line assembler fixes that, and the
00037 conversion routines can be faster than lookup tables, in most real world usage.
00038 A "find the first 1" instruction is available on most modern CPUs, and is a
00039 much underused feature. 
00040 
00041 If an assembly language method of bit searching is not available, these routines
00042 revert to a method that can be a little slow, so the cache thrashing might not
00043 seem so bad :(
00044 
00045 Feel free to submit patches to add fast "find the first 1" support for your own
00046 favourite processor.
00047 
00048 Look up tables are used for transcoding between A-law and u-law, since it is
00049 difficult to achieve the precise transcoding procedure laid down in the G.711
00050 specification by other means.
00051 */
00052 
00053 #if !defined(_SPANDSP_G711_H_)
00054 #define _SPANDSP_G711_H_
00055 
00056 #if defined(__cplusplus)
00057 extern "C"
00058 {
00059 #endif
00060 
00061 /* N.B. It is tempting to use look-up tables for A-law and u-law conversion.
00062  *      However, you should consider the cache footprint.
00063  *
00064  *      A 64K byte table for linear to x-law and a 512 byte table for x-law to
00065  *      linear sound like peanuts these days, and shouldn't an array lookup be
00066  *      real fast? No! When the cache sloshes as badly as this one will, a tight
00067  *      calculation may be better. The messiest part is normally finding the
00068  *      segment, but a little inline assembly can fix that on an i386, x86_64 and
00069  *      many other modern processors.
00070  */
00071  
00072 /*
00073  * Mu-law is basically as follows:
00074  *
00075  *      Biased Linear Input Code        Compressed Code
00076  *      ------------------------        ---------------
00077  *      00000001wxyza                   000wxyz
00078  *      0000001wxyzab                   001wxyz
00079  *      000001wxyzabc                   010wxyz
00080  *      00001wxyzabcd                   011wxyz
00081  *      0001wxyzabcde                   100wxyz
00082  *      001wxyzabcdef                   101wxyz
00083  *      01wxyzabcdefg                   110wxyz
00084  *      1wxyzabcdefgh                   111wxyz
00085  *
00086  * Each biased linear code has a leading 1 which identifies the segment
00087  * number. The value of the segment number is equal to 7 minus the number
00088  * of leading 0's. The quantization interval is directly available as the
00089  * four bits wxyz.  * The trailing bits (a - h) are ignored.
00090  *
00091  * Ordinarily the complement of the resulting code word is used for
00092  * transmission, and so the code word is complemented before it is returned.
00093  *
00094  * For further information see John C. Bellamy's Digital Telephony, 1982,
00095  * John Wiley & Sons, pps 98-111 and 472-476.
00096  */
00097 
00098 //#define ULAW_ZEROTRAP                 /* turn on the trap as per the MIL-STD */
00099 #define ULAW_BIAS        0x84           /* Bias for linear code. */
00100 
00101 /*! \brief Encode a linear sample to u-law
00102     \param linear The sample to encode.
00103     \return The u-law value.
00104 */
00105 static __inline__ uint8_t linear_to_ulaw(int linear)
00106 {
00107     uint8_t u_val;
00108     int mask;
00109     int seg;
00110 
00111     /* Get the sign and the magnitude of the value. */
00112     if (linear >= 0)
00113     {
00114         linear = ULAW_BIAS + linear;
00115         mask = 0xFF;
00116     }
00117     else
00118     {
00119         linear = ULAW_BIAS - linear;
00120         mask = 0x7F;
00121     }
00122 
00123     seg = top_bit(linear | 0xFF) - 7;
00124 
00125     /*
00126      * Combine the sign, segment, quantization bits,
00127      * and complement the code word.
00128      */
00129     if (seg >= 8)
00130         u_val = (uint8_t) (0x7F ^ mask);
00131     else
00132         u_val = (uint8_t) (((seg << 4) | ((linear >> (seg + 3)) & 0xF)) ^ mask);
00133 #ifdef ULAW_ZEROTRAP
00134     /* Optional ITU trap */
00135     if (u_val == 0)
00136         u_val = 0x02;
00137 #endif
00138     return  u_val;
00139 }
00140 /*- End of function --------------------------------------------------------*/
00141 
00142 /*! \brief Decode an u-law sample to a linear value.
00143     \param ulaw The u-law sample to decode.
00144     \return The linear value.
00145 */
00146 static __inline__ int16_t ulaw_to_linear(uint8_t ulaw)
00147 {
00148     int t;
00149     
00150     /* Complement to obtain normal u-law value. */
00151     ulaw = ~ulaw;
00152     /*
00153      * Extract and bias the quantization bits. Then
00154      * shift up by the segment number and subtract out the bias.
00155      */
00156     t = (((ulaw & 0x0F) << 3) + ULAW_BIAS) << (((int) ulaw & 0x70) >> 4);
00157     return  (int16_t) ((ulaw & 0x80)  ?  (ULAW_BIAS - t)  :  (t - ULAW_BIAS));
00158 }
00159 /*- End of function --------------------------------------------------------*/
00160 
00161 /*
00162  * A-law is basically as follows:
00163  *
00164  *      Linear Input Code        Compressed Code
00165  *      -----------------        ---------------
00166  *      0000000wxyza             000wxyz
00167  *      0000001wxyza             001wxyz
00168  *      000001wxyzab             010wxyz
00169  *      00001wxyzabc             011wxyz
00170  *      0001wxyzabcd             100wxyz
00171  *      001wxyzabcde             101wxyz
00172  *      01wxyzabcdef             110wxyz
00173  *      1wxyzabcdefg             111wxyz
00174  *
00175  * For further information see John C. Bellamy's Digital Telephony, 1982,
00176  * John Wiley & Sons, pps 98-111 and 472-476.
00177  */
00178 
00179 #define ALAW_AMI_MASK       0x55
00180 
00181 /*! \brief Encode a linear sample to A-law
00182     \param linear The sample to encode.
00183     \return The A-law value.
00184 */
00185 static __inline__ uint8_t linear_to_alaw(int linear)
00186 {
00187     int mask;
00188     int seg;
00189     
00190     if (linear >= 0)
00191     {
00192         /* Sign (bit 7) bit = 1 */
00193         mask = ALAW_AMI_MASK | 0x80;
00194     }
00195     else
00196     {
00197         /* Sign (bit 7) bit = 0 */
00198         mask = ALAW_AMI_MASK;
00199         linear = -linear - 1;
00200     }
00201 
00202     /* Convert the scaled magnitude to segment number. */
00203     seg = top_bit(linear | 0xFF) - 7;
00204     if (seg >= 8)
00205     {
00206         if (linear >= 0)
00207         {
00208             /* Out of range. Return maximum value. */
00209             return (uint8_t) (0x7F ^ mask);
00210         }
00211         /* We must be just a tiny step below zero */
00212         return (uint8_t) (0x00 ^ mask);
00213     }
00214     /* Combine the sign, segment, and quantization bits. */
00215     return (uint8_t) (((seg << 4) | ((linear >> ((seg)  ?  (seg + 3)  :  4)) & 0x0F)) ^ mask);
00216 }
00217 /*- End of function --------------------------------------------------------*/
00218 
00219 /*! \brief Decode an A-law sample to a linear value.
00220     \param alaw The A-law sample to decode.
00221     \return The linear value.
00222 */
00223 static __inline__ int16_t alaw_to_linear(uint8_t alaw)
00224 {
00225     int i;
00226     int seg;
00227 
00228     alaw ^= ALAW_AMI_MASK;
00229     i = ((alaw & 0x0F) << 4);
00230     seg = (((int) alaw & 0x70) >> 4);
00231     if (seg)
00232         i = (i + 0x108) << (seg - 1);
00233     else
00234         i += 8;
00235     return (int16_t) ((alaw & 0x80)  ?  i  :  -i);
00236 }
00237 /*- End of function --------------------------------------------------------*/
00238 
00239 /*! \brief Transcode from A-law to u-law, using the procedure defined in G.711.
00240     \param alaw The A-law sample to transcode.
00241     \return The best matching u-law value.
00242 */
00243 uint8_t alaw_to_ulaw(uint8_t alaw);
00244 
00245 /*! \brief Transcode from u-law to A-law, using the procedure defined in G.711.
00246     \param ulaw The u-law sample to transcode.
00247     \return The best matching A-law value.
00248 */
00249 uint8_t ulaw_to_alaw(uint8_t ulaw);
00250 
00251 #if defined(__cplusplus)
00252 }
00253 #endif
00254 
00255 #endif
00256 /*- End of file ------------------------------------------------------------*/

Generated on Thu Mar 20 17:20:24 2008 for libspandsp by  doxygen 1.5.5