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 * Despite my general liking of the GPL, I place this code in the 00011 * public domain for the benefit of all mankind - even the slimy 00012 * ones who might try to proprietize my work and use it to my 00013 * detriment. 00014 * 00015 * $Id: g711.h,v 1.1 2006/06/07 15:46:39 steveu Exp $ 00016 */ 00017 00018 /*! \file */ 00019 00020 /*! \page g711_page A-law and mu-law handling 00021 Lookup tables for A-law and u-law look attractive, until you consider the impact 00022 on the CPU cache. If it causes a substantial area of your processor cache to get 00023 hit too often, cache sloshing will severely slow things down. The main reason 00024 these routines are slow in C, is the lack of direct access to the CPU's "find 00025 the first 1" instruction. A little in-line assembler fixes that, and the 00026 conversion routines can be faster than lookup tables, in most real world usage. 00027 A "find the first 1" instruction is available on most modern CPUs, and is a 00028 much underused feature. 00029 00030 If an assembly language method of bit searching is not available, these routines 00031 revert to a method that can be a little slow, so the cache thrashing might not 00032 seem so bad :( 00033 00034 Feel free to submit patches to add fast "find the first 1" support for your own 00035 favourite processor. 00036 00037 Look up tables are used for transcoding between A-law and u-law, since it is 00038 difficult to achieve the precise transcoding procedure laid down in the G.711 00039 specification by other means. 00040 */ 00041 00042 #if !defined(_G711_H_) 00043 #define _G711_H_ 00044 00045 #ifdef __cplusplus 00046 extern "C" { 00047 #endif 00048 00049 #if defined(__i386__) 00050 /*! \brief Find the bit position of the highest set bit in a word 00051 \param bits The word to be searched 00052 \return The bit number of the highest set bit, or -1 if the word is zero. */ 00053 static __inline__ int top_bit(unsigned int bits) 00054 { 00055 int res; 00056 00057 __asm__ __volatile__(" movl $-1,%%edx;\n" 00058 " bsrl %%eax,%%edx;\n" 00059 : "=d" (res) 00060 : "a" (bits)); 00061 return res; 00062 } 00063 /*- End of function --------------------------------------------------------*/ 00064 00065 /*! \brief Find the bit position of the lowest set bit in a word 00066 \param bits The word to be searched 00067 \return The bit number of the lowest set bit, or -1 if the word is zero. */ 00068 static __inline__ int bottom_bit(unsigned int bits) 00069 { 00070 int res; 00071 00072 __asm__ __volatile__(" movl $-1,%%edx;\n" 00073 " bsfl %%eax,%%edx;\n" 00074 : "=d" (res) 00075 : "a" (bits)); 00076 return res; 00077 } 00078 /*- End of function --------------------------------------------------------*/ 00079 #elif defined(__x86_64__) 00080 static __inline__ int top_bit(unsigned int bits) 00081 { 00082 int res; 00083 00084 __asm__ __volatile__(" movq $-1,%%rdx;\n" 00085 " bsrq %%rax,%%rdx;\n" 00086 : "=d" (res) 00087 : "a" (bits)); 00088 return res; 00089 } 00090 /*- End of function --------------------------------------------------------*/ 00091 00092 static __inline__ int bottom_bit(unsigned int bits) 00093 { 00094 int res; 00095 00096 __asm__ __volatile__(" movq $-1,%%rdx;\n" 00097 " bsfq %%rax,%%rdx;\n" 00098 : "=d" (res) 00099 : "a" (bits)); 00100 return res; 00101 } 00102 /*- End of function --------------------------------------------------------*/ 00103 #else 00104 static __inline__ int top_bit(unsigned int bits) 00105 { 00106 int i; 00107 00108 if (bits == 0) 00109 return -1; 00110 i = 0; 00111 if (bits & 0xFFFF0000) 00112 { 00113 bits &= 0xFFFF0000; 00114 i += 16; 00115 } 00116 if (bits & 0xFF00FF00) 00117 { 00118 bits &= 0xFF00FF00; 00119 i += 8; 00120 } 00121 if (bits & 0xF0F0F0F0) 00122 { 00123 bits &= 0xF0F0F0F0; 00124 i += 4; 00125 } 00126 if (bits & 0xCCCCCCCC) 00127 { 00128 bits &= 0xCCCCCCCC; 00129 i += 2; 00130 } 00131 if (bits & 0xAAAAAAAA) 00132 { 00133 bits &= 0xAAAAAAAA; 00134 i += 1; 00135 } 00136 return i; 00137 } 00138 /*- End of function --------------------------------------------------------*/ 00139 00140 static __inline__ int bottom_bit(unsigned int bits) 00141 { 00142 int i; 00143 00144 if (bits == 0) 00145 return -1; 00146 i = 32; 00147 if (bits & 0x0000FFFF) 00148 { 00149 bits &= 0x0000FFFF; 00150 i -= 16; 00151 } 00152 if (bits & 0x00FF00FF) 00153 { 00154 bits &= 0x00FF00FF; 00155 i -= 8; 00156 } 00157 if (bits & 0x0F0F0F0F) 00158 { 00159 bits &= 0x0F0F0F0F; 00160 i -= 4; 00161 } 00162 if (bits & 0x33333333) 00163 { 00164 bits &= 0x33333333; 00165 i -= 2; 00166 } 00167 if (bits & 0x55555555) 00168 { 00169 bits &= 0x55555555; 00170 i -= 1; 00171 } 00172 return i; 00173 } 00174 /*- End of function --------------------------------------------------------*/ 00175 #endif 00176 00177 /* N.B. It is tempting to use look-up tables for A-law and u-law conversion. 00178 * However, you should consider the cache footprint. 00179 * 00180 * A 64K byte table for linear to x-law and a 512 byte table for x-law to 00181 * linear sound like peanuts these days, and shouldn't an array lookup be 00182 * real fast? No! When the cache sloshes as badly as this one will, a tight 00183 * calculation may be better. The messiest part is normally finding the 00184 * segment, but a little inline assembly can fix that on an i386, x86_64 and 00185 * many other modern processors. 00186 */ 00187 00188 /* 00189 * Mu-law is basically as follows: 00190 * 00191 * Biased Linear Input Code Compressed Code 00192 * ------------------------ --------------- 00193 * 00000001wxyza 000wxyz 00194 * 0000001wxyzab 001wxyz 00195 * 000001wxyzabc 010wxyz 00196 * 00001wxyzabcd 011wxyz 00197 * 0001wxyzabcde 100wxyz 00198 * 001wxyzabcdef 101wxyz 00199 * 01wxyzabcdefg 110wxyz 00200 * 1wxyzabcdefgh 111wxyz 00201 * 00202 * Each biased linear code has a leading 1 which identifies the segment 00203 * number. The value of the segment number is equal to 7 minus the number 00204 * of leading 0's. The quantization interval is directly available as the 00205 * four bits wxyz. * The trailing bits (a - h) are ignored. 00206 * 00207 * Ordinarily the complement of the resulting code word is used for 00208 * transmission, and so the code word is complemented before it is returned. 00209 * 00210 * For further information see John C. Bellamy's Digital Telephony, 1982, 00211 * John Wiley & Sons, pps 98-111 and 472-476. 00212 */ 00213 00214 //#define ULAW_ZEROTRAP /* turn on the trap as per the MIL-STD */ 00215 #define ULAW_BIAS 0x84 /* Bias for linear code. */ 00216 00217 /*! \brief Encode a linear sample to u-law 00218 \param linear The sample to encode. 00219 \return The u-law value. 00220 */ 00221 static __inline__ uint8_t linear_to_ulaw(int linear) 00222 { 00223 uint8_t u_val; 00224 int mask; 00225 int seg; 00226 00227 /* Get the sign and the magnitude of the value. */ 00228 if (linear < 0) 00229 { 00230 linear = ULAW_BIAS - linear; 00231 mask = 0x7F; 00232 } 00233 else 00234 { 00235 linear = ULAW_BIAS + linear; 00236 mask = 0xFF; 00237 } 00238 00239 seg = top_bit(linear | 0xFF) - 7; 00240 00241 /* 00242 * Combine the sign, segment, quantization bits, 00243 * and complement the code word. 00244 */ 00245 if (seg >= 8) 00246 u_val = (uint8_t) (0x7F ^ mask); 00247 else 00248 u_val = (uint8_t) (((seg << 4) | ((linear >> (seg + 3)) & 0xF)) ^ mask); 00249 #ifdef ULAW_ZEROTRAP 00250 /* Optional ITU trap */ 00251 if (u_val == 0) 00252 u_val = 0x02; 00253 #endif 00254 return u_val; 00255 } 00256 /*- End of function --------------------------------------------------------*/ 00257 00258 /*! \brief Decode an u-law sample to a linear value. 00259 \param ulaw The u-law sample to decode. 00260 \return The linear value. 00261 */ 00262 static __inline__ int16_t ulaw_to_linear(uint8_t ulaw) 00263 { 00264 int t; 00265 00266 /* Complement to obtain normal u-law value. */ 00267 ulaw = ~ulaw; 00268 /* 00269 * Extract and bias the quantization bits. Then 00270 * shift up by the segment number and subtract out the bias. 00271 */ 00272 t = (((ulaw & 0x0F) << 3) + ULAW_BIAS) << (((int) ulaw & 0x70) >> 4); 00273 return (int16_t) ((ulaw & 0x80) ? (ULAW_BIAS - t) : (t - ULAW_BIAS)); 00274 } 00275 /*- End of function --------------------------------------------------------*/ 00276 00277 /* 00278 * A-law is basically as follows: 00279 * 00280 * Linear Input Code Compressed Code 00281 * ----------------- --------------- 00282 * 0000000wxyza 000wxyz 00283 * 0000001wxyza 001wxyz 00284 * 000001wxyzab 010wxyz 00285 * 00001wxyzabc 011wxyz 00286 * 0001wxyzabcd 100wxyz 00287 * 001wxyzabcde 101wxyz 00288 * 01wxyzabcdef 110wxyz 00289 * 1wxyzabcdefg 111wxyz 00290 * 00291 * For further information see John C. Bellamy's Digital Telephony, 1982, 00292 * John Wiley & Sons, pps 98-111 and 472-476. 00293 */ 00294 00295 #define ALAW_AMI_MASK 0x55 00296 00297 /*! \brief Encode a linear sample to A-law 00298 \param linear The sample to encode. 00299 \return The A-law value. 00300 */ 00301 static __inline__ uint8_t linear_to_alaw(int linear) 00302 { 00303 int mask; 00304 int seg; 00305 00306 if (linear >= 0) 00307 { 00308 /* Sign (bit 7) bit = 1 */ 00309 mask = ALAW_AMI_MASK | 0x80; 00310 } 00311 else 00312 { 00313 /* Sign (bit 7) bit = 0 */ 00314 mask = ALAW_AMI_MASK; 00315 linear = -linear - 8; 00316 } 00317 00318 /* Convert the scaled magnitude to segment number. */ 00319 seg = top_bit(linear | 0xFF) - 7; 00320 if (seg >= 8) 00321 { 00322 if (linear >= 0) 00323 { 00324 /* Out of range. Return maximum value. */ 00325 return (uint8_t) (0x7F ^ mask); 00326 } 00327 /* We must be just a tiny step below zero */ 00328 return (uint8_t) (0x00 ^ mask); 00329 } 00330 /* Combine the sign, segment, and quantization bits. */ 00331 return (uint8_t) (((seg << 4) | ((linear >> ((seg) ? (seg + 3) : 4)) & 0x0F)) ^ mask); 00332 } 00333 /*- End of function --------------------------------------------------------*/ 00334 00335 /*! \brief Decode an A-law sample to a linear value. 00336 \param alaw The A-law sample to decode. 00337 \return The linear value. 00338 */ 00339 static __inline__ int16_t alaw_to_linear(uint8_t alaw) 00340 { 00341 int i; 00342 int seg; 00343 00344 alaw ^= ALAW_AMI_MASK; 00345 i = ((alaw & 0x0F) << 4); 00346 seg = (((int) alaw & 0x70) >> 4); 00347 if (seg) 00348 i = (i + 0x108) << (seg - 1); 00349 else 00350 i += 8; 00351 return (int16_t) ((alaw & 0x80) ? i : -i); 00352 } 00353 /*- End of function --------------------------------------------------------*/ 00354 00355 /*! \brief Transcode from A-law to u-law, using the procedure defined in G.711. 00356 \param alaw The A-law sample to transcode. 00357 \return The best matching u-law value. 00358 */ 00359 uint8_t alaw_to_ulaw(uint8_t alaw); 00360 00361 /*! \brief Transcode from u-law to A-law, using the procedure defined in G.711. 00362 \param alaw The u-law sample to transcode. 00363 \return The best matching A-law value. 00364 */ 00365 uint8_t ulaw_to_alaw(uint8_t ulaw); 00366 00367 #ifdef __cplusplus 00368 } 00369 #endif 00370 00371 #endif 00372 /*- End of file ------------------------------------------------------------*/