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