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