1 /* 2 * SpanDSP - a series of DSP components for telephony 3 * 4 * g711.h - In line A-law and u-law conversion routines 5 * 6 * Written by Steve Underwood <steveu@coppice.org> 7 * 8 * Copyright (C) 2001 Steve Underwood 9 * 10 * Despite my general liking of the GPL, I place this code in the 11 * public domain for the benefit of all mankind - even the slimy 12 * ones who might try to proprietize my work and use it to my 13 * detriment. 14 * 15 * $Id: g711.h,v 1.1 2006/06/07 15:46:39 steveu Exp $ 16 */ 17 18 /*! \file */ 19 20 /*! \page g711_page A-law and mu-law handling 21 Lookup tables for A-law and u-law look attractive, until you consider the impact 22 on the CPU cache. If it causes a substantial area of your processor cache to get 23 hit too often, cache sloshing will severely slow things down. The main reason 24 these routines are slow in C, is the lack of direct access to the CPU's "find 25 the first 1" instruction. A little in-line assembler fixes that, and the 26 conversion routines can be faster than lookup tables, in most real world usage. 27 A "find the first 1" instruction is available on most modern CPUs, and is a 28 much underused feature. 29 30 If an assembly language method of bit searching is not available, these routines 31 revert to a method that can be a little slow, so the cache thrashing might not 32 seem so bad :( 33 34 Feel free to submit patches to add fast "find the first 1" support for your own 35 favourite processor. 36 37 Look up tables are used for transcoding between A-law and u-law, since it is 38 difficult to achieve the precise transcoding procedure laid down in the G.711 39 specification by other means. 40 */ 41 42 #if !defined(_G711_H_) 43 #define _G711_H_ 44 45 #ifdef __cplusplus 46 extern "C" { 47 #endif 48 49 #ifdef _MSC_VER 50 #ifndef __inline__ 51 #define __inline__ __inline 52 #endif 53 typedef unsigned __int8 uint8_t; 54 typedef __int16 int16_t; 55 typedef __int32 int32_t; 56 typedef unsigned __int16 uint16_t; 57 #else 58 #include <stdint.h> 59 #endif 60 61 #if defined(__i386__) 62 /*! \brief Find the bit position of the highest set bit in a word 63 \param bits The word to be searched 64 \return The bit number of the highest set bit, or -1 if the word is zero. */ 65 static __inline__ int top_bit(unsigned int bits) 66 { 67 int res; 68 69 __asm__ __volatile__(" movl $-1,%%edx;\n" 70 " bsrl %%eax,%%edx;\n" 71 : "=d" (res) 72 : "a" (bits)); 73 return res; 74 } 75 /*- End of function --------------------------------------------------------*/ 76 77 /*! \brief Find the bit position of the lowest set bit in a word 78 \param bits The word to be searched 79 \return The bit number of the lowest set bit, or -1 if the word is zero. */ 80 static __inline__ int bottom_bit(unsigned int bits) 81 { 82 int res; 83 84 __asm__ __volatile__(" movl $-1,%%edx;\n" 85 " bsfl %%eax,%%edx;\n" 86 : "=d" (res) 87 : "a" (bits)); 88 return res; 89 } 90 /*- End of function --------------------------------------------------------*/ 91 #elif defined(__x86_64__) 92 static __inline__ int top_bit(unsigned int bits) 93 { 94 int res; 95 96 __asm__ __volatile__(" movq $-1,%%rdx;\n" 97 " bsrq %%rax,%%rdx;\n" 98 : "=d" (res) 99 : "a" (bits)); 100 return res; 101 } 102 /*- End of function --------------------------------------------------------*/ 103 104 static __inline__ int bottom_bit(unsigned int bits) 105 { 106 int res; 107 108 __asm__ __volatile__(" movq $-1,%%rdx;\n" 109 " bsfq %%rax,%%rdx;\n" 110 : "=d" (res) 111 : "a" (bits)); 112 return res; 113 } 114 /*- End of function --------------------------------------------------------*/ 115 #else 116 static __inline__ int top_bit(unsigned int bits) 117 { 118 int i; 119 120 if (bits == 0) 121 return -1; 122 i = 0; 123 if (bits & 0xFFFF0000) 124 { 125 bits &= 0xFFFF0000; 126 i += 16; 127 } 128 if (bits & 0xFF00FF00) 129 { 130 bits &= 0xFF00FF00; 131 i += 8; 132 } 133 if (bits & 0xF0F0F0F0) 134 { 135 bits &= 0xF0F0F0F0; 136 i += 4; 137 } 138 if (bits & 0xCCCCCCCC) 139 { 140 bits &= 0xCCCCCCCC; 141 i += 2; 142 } 143 if (bits & 0xAAAAAAAA) 144 { 145 bits &= 0xAAAAAAAA; 146 i += 1; 147 } 148 return i; 149 } 150 /*- End of function --------------------------------------------------------*/ 151 152 static __inline__ int bottom_bit(unsigned int bits) 153 { 154 int i; 155 156 if (bits == 0) 157 return -1; 158 i = 32; 159 if (bits & 0x0000FFFF) 160 { 161 bits &= 0x0000FFFF; 162 i -= 16; 163 } 164 if (bits & 0x00FF00FF) 165 { 166 bits &= 0x00FF00FF; 167 i -= 8; 168 } 169 if (bits & 0x0F0F0F0F) 170 { 171 bits &= 0x0F0F0F0F; 172 i -= 4; 173 } 174 if (bits & 0x33333333) 175 { 176 bits &= 0x33333333; 177 i -= 2; 178 } 179 if (bits & 0x55555555) 180 { 181 bits &= 0x55555555; 182 i -= 1; 183 } 184 return i; 185 } 186 /*- End of function --------------------------------------------------------*/ 187 #endif 188 189 /* N.B. It is tempting to use look-up tables for A-law and u-law conversion. 190 * However, you should consider the cache footprint. 191 * 192 * A 64K byte table for linear to x-law and a 512 byte table for x-law to 193 * linear sound like peanuts these days, and shouldn't an array lookup be 194 * real fast? No! When the cache sloshes as badly as this one will, a tight 195 * calculation may be better. The messiest part is normally finding the 196 * segment, but a little inline assembly can fix that on an i386, x86_64 and 197 * many other modern processors. 198 */ 199 200 /* 201 * Mu-law is basically as follows: 202 * 203 * Biased Linear Input Code Compressed Code 204 * ------------------------ --------------- 205 * 00000001wxyza 000wxyz 206 * 0000001wxyzab 001wxyz 207 * 000001wxyzabc 010wxyz 208 * 00001wxyzabcd 011wxyz 209 * 0001wxyzabcde 100wxyz 210 * 001wxyzabcdef 101wxyz 211 * 01wxyzabcdefg 110wxyz 212 * 1wxyzabcdefgh 111wxyz 213 * 214 * Each biased linear code has a leading 1 which identifies the segment 215 * number. The value of the segment number is equal to 7 minus the number 216 * of leading 0's. The quantization interval is directly available as the 217 * four bits wxyz. * The trailing bits (a - h) are ignored. 218 * 219 * Ordinarily the complement of the resulting code word is used for 220 * transmission, and so the code word is complemented before it is returned. 221 * 222 * For further information see John C. Bellamy's Digital Telephony, 1982, 223 * John Wiley & Sons, pps 98-111 and 472-476. 224 */ 225 226 /*#define ULAW_ZEROTRAP*/ /* turn on the trap as per the MIL-STD */ 227 #define ULAW_BIAS 0x84 /* Bias for linear code. */ 228 229 /*! \brief Encode a linear sample to u-law 230 \param linear The sample to encode. 231 \return The u-law value. 232 */ 233 static __inline__ uint8_t linear_to_ulaw(int linear) 234 { 235 uint8_t u_val; 236 int mask; 237 int seg; 238 239 /* Get the sign and the magnitude of the value. */ 240 if (linear < 0) 241 { 242 linear = ULAW_BIAS - linear; 243 mask = 0x7F; 244 } 245 else 246 { 247 linear = ULAW_BIAS + linear; 248 mask = 0xFF; 249 } 250 251 seg = top_bit(linear | 0xFF) - 7; 252 253 /* 254 * Combine the sign, segment, quantization bits, 255 * and complement the code word. 256 */ 257 if (seg >= 8) 258 u_val = (uint8_t) (0x7F ^ mask); 259 else 260 u_val = (uint8_t) (((seg << 4) | ((linear >> (seg + 3)) & 0xF)) ^ mask); 261 #ifdef ULAW_ZEROTRAP 262 /* Optional ITU trap */ 263 if (u_val == 0) 264 u_val = 0x02; 265 #endif 266 return u_val; 267 } 268 /*- End of function --------------------------------------------------------*/ 269 270 /*! \brief Decode an u-law sample to a linear value. 271 \param ulaw The u-law sample to decode. 272 \return The linear value. 273 */ 274 static __inline__ int16_t ulaw_to_linear(uint8_t ulaw) 275 { 276 int t; 277 278 /* Complement to obtain normal u-law value. */ 279 ulaw = ~ulaw; 280 /* 281 * Extract and bias the quantization bits. Then 282 * shift up by the segment number and subtract out the bias. 283 */ 284 t = (((ulaw & 0x0F) << 3) + ULAW_BIAS) << (((int) ulaw & 0x70) >> 4); 285 return (int16_t) ((ulaw & 0x80) ? (ULAW_BIAS - t) : (t - ULAW_BIAS)); 286 } 287 /*- End of function --------------------------------------------------------*/ 288 289 /* 290 * A-law is basically as follows: 291 * 292 * Linear Input Code Compressed Code 293 * ----------------- --------------- 294 * 0000000wxyza 000wxyz 295 * 0000001wxyza 001wxyz 296 * 000001wxyzab 010wxyz 297 * 00001wxyzabc 011wxyz 298 * 0001wxyzabcd 100wxyz 299 * 001wxyzabcde 101wxyz 300 * 01wxyzabcdef 110wxyz 301 * 1wxyzabcdefg 111wxyz 302 * 303 * For further information see John C. Bellamy's Digital Telephony, 1982, 304 * John Wiley & Sons, pps 98-111 and 472-476. 305 */ 306 307 #define ALAW_AMI_MASK 0x55 308 309 /*! \brief Encode a linear sample to A-law 310 \param linear The sample to encode. 311 \return The A-law value. 312 */ 313 static __inline__ uint8_t linear_to_alaw(int linear) 314 { 315 int mask; 316 int seg; 317 318 if (linear >= 0) 319 { 320 /* Sign (bit 7) bit = 1 */ 321 mask = ALAW_AMI_MASK | 0x80; 322 } 323 else 324 { 325 /* Sign (bit 7) bit = 0 */ 326 mask = ALAW_AMI_MASK; 327 linear = -linear - 8; 328 } 329 330 /* Convert the scaled magnitude to segment number. */ 331 seg = top_bit(linear | 0xFF) - 7; 332 if (seg >= 8) 333 { 334 if (linear >= 0) 335 { 336 /* Out of range. Return maximum value. */ 337 return (uint8_t) (0x7F ^ mask); 338 } 339 /* We must be just a tiny step below zero */ 340 return (uint8_t) (0x00 ^ mask); 341 } 342 /* Combine the sign, segment, and quantization bits. */ 343 return (uint8_t) (((seg << 4) | ((linear >> ((seg) ? (seg + 3) : 4)) & 0x0F)) ^ mask); 344 } 345 /*- End of function --------------------------------------------------------*/ 346 347 /*! \brief Decode an A-law sample to a linear value. 348 \param alaw The A-law sample to decode. 349 \return The linear value. 350 */ 351 static __inline__ int16_t alaw_to_linear(uint8_t alaw) 352 { 353 int i; 354 int seg; 355 356 alaw ^= ALAW_AMI_MASK; 357 i = ((alaw & 0x0F) << 4); 358 seg = (((int) alaw & 0x70) >> 4); 359 if (seg) 360 i = (i + 0x108) << (seg - 1); 361 else 362 i += 8; 363 return (int16_t) ((alaw & 0x80) ? i : -i); 364 } 365 /*- End of function --------------------------------------------------------*/ 366 367 /*! \brief Transcode from A-law to u-law, using the procedure defined in G.711. 368 \param alaw The A-law sample to transcode. 369 \return The best matching u-law value. 370 */ 371 uint8_t alaw_to_ulaw(uint8_t alaw); 372 373 /*! \brief Transcode from u-law to A-law, using the procedure defined in G.711. 374 \param alaw The u-law sample to transcode. 375 \return The best matching A-law value. 376 */ 377 uint8_t ulaw_to_alaw(uint8_t ulaw); 378 379 #ifdef __cplusplus 380 } 381 #endif 382 383 #endif 384 /*- End of file ------------------------------------------------------------*/ 385 386 /* For Emacs: 387 * Local Variables: 388 * mode:c 389 * indent-tabs-mode:t 390 * tab-width:4 391 * c-basic-offset:4 392 * End: 393 * For VIM: 394 * vim:set softtabstop=4 shiftwidth=4 tabstop=4: 395 */