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 favorite 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(FREESWITCH_G711_H)
43 #define FREESWITCH_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 #if !defined(_STDINT) && !defined(uint32_t)
54 	typedef unsigned __int8 uint8_t;
55 	typedef __int16 int16_t;
56 	typedef __int32 int32_t;
57 	typedef unsigned __int16 uint16_t;
58 #endif
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. */
top_bit(unsigned int bits)65 	static __inline__ int top_bit(unsigned int bits) {
66 		int res;
67 
68 		__asm__ __volatile__(" movl $-1,%%edx;\n" " bsrl %%eax,%%edx;\n":"=d"(res)
69 							 :"a"    (bits));
70 		        return res;
71 	}
72 	/*- End of function --------------------------------------------------------*//*! \brief Find the bit position of the lowest set bit in a word
73 	   \param bits The word to be searched
bottom_bit(unsigned int bits)74 	   \return The bit number of the lowest set bit, or -1 if the word is zero. */ static __inline__ int bottom_bit(unsigned int bits) {
75 		int res;
76 
77 		__asm__ __volatile__(" movl $-1,%%edx;\n" " bsfl %%eax,%%edx;\n":"=d"(res)
78 							 :"a"    (bits));
79 		return res;
80 	}
81 /*- End of function --------------------------------------------------------*/
82 #elif defined(__x86_64__)
top_bit(unsigned int bits)83 	static __inline__ int top_bit(unsigned int bits) {
84 		int res;
85 
86 		__asm__ __volatile__(" movq $-1,%%rdx;\n" " bsrq %%rax,%%rdx;\n":"=d"(res)
87 							 :"a"    (bits));
88 		        return res;
89 	}
bottom_bit(unsigned int bits)90 /*- End of function --------------------------------------------------------*/ static __inline__ int bottom_bit(unsigned int bits) {
91 		int res;
92 
93 		__asm__ __volatile__(" movq $-1,%%rdx;\n" " bsfq %%rax,%%rdx;\n":"=d"(res)
94 							 :"a"    (bits));
95 		return res;
96 	}
97 /*- End of function --------------------------------------------------------*/
98 #else
top_bit(unsigned int bits)99 	static __inline__ int top_bit(unsigned int bits) {
100 		int i;
101 
102 		if  (bits == 0)
103 			return -1;
104 		    i = 0;
105 		if  (bits & 0xFFFF0000) {
106 			bits &= 0xFFFF0000;
107 			i += 16;
108 		}
109 		if     (bits & 0xFF00FF00) {
110 			bits &= 0xFF00FF00;
111 			i += 8;
112 		}
113 		if (bits & 0xF0F0F0F0) {
114 			bits &= 0xF0F0F0F0;
115 			i += 4;
116 		}
117 		if (bits & 0xCCCCCCCC) {
118 			bits &= 0xCCCCCCCC;
119 			i += 2;
120 		}
121 		if (bits & 0xAAAAAAAA) {
122 			bits &= 0xAAAAAAAA;
123 			i += 1;
124 		}
125 		return i;
126 	}
127 /*- End of function --------------------------------------------------------*/
128 
bottom_bit(unsigned int bits)129 	static __inline__ int bottom_bit(unsigned int bits) {
130 		int i;
131 
132 		if (bits == 0)
133 			return -1;
134 		i = 32;
135 		if (bits & 0x0000FFFF) {
136 			bits &= 0x0000FFFF;
137 			i -= 16;
138 		}
139 		if (bits & 0x00FF00FF) {
140 			bits &= 0x00FF00FF;
141 			i -= 8;
142 		}
143 		if (bits & 0x0F0F0F0F) {
144 			bits &= 0x0F0F0F0F;
145 			i -= 4;
146 		}
147 		if (bits & 0x33333333) {
148 			bits &= 0x33333333;
149 			i -= 2;
150 		}
151 		if (bits & 0x55555555) {
152 			bits &= 0x55555555;
153 			i -= 1;
154 		}
155 		return i;
156 	}
157 /*- End of function --------------------------------------------------------*/
158 #endif
159 
160 /* N.B. It is tempting to use look-up tables for A-law and u-law conversion.
161  *      However, you should consider the cache footprint.
162  *
163  *      A 64K byte table for linear to x-law and a 512 byte table for x-law to
164  *      linear sound like peanuts these days, and shouldn't an array lookup be
165  *      real fast? No! When the cache sloshes as badly as this one will, a tight
166  *      calculation may be better. The messiest part is normally finding the
167  *      segment, but a little inline assembly can fix that on an i386, x86_64 and
168  *      many other modern processors.
169  */
170 
171 /*
172  * Mu-law is basically as follows:
173  *
174  *      Biased Linear Input Code        Compressed Code
175  *      ------------------------        ---------------
176  *      00000001wxyza                   000wxyz
177  *      0000001wxyzab                   001wxyz
178  *      000001wxyzabc                   010wxyz
179  *      00001wxyzabcd                   011wxyz
180  *      0001wxyzabcde                   100wxyz
181  *      001wxyzabcdef                   101wxyz
182  *      01wxyzabcdefg                   110wxyz
183  *      1wxyzabcdefgh                   111wxyz
184  *
185  * Each biased linear code has a leading 1 which identifies the segment
186  * number. The value of the segment number is equal to 7 minus the number
187  * of leading 0's. The quantization interval is directly available as the
188  * four bits wxyz.  * The trailing bits (a - h) are ignored.
189  *
190  * Ordinarily the complement of the resulting code word is used for
191  * transmission, and so the code word is complemented before it is returned.
192  *
193  * For further information see John C. Bellamy's Digital Telephony, 1982,
194  * John Wiley & Sons, pps 98-111 and 472-476.
195  */
196 
197 //#define ULAW_ZEROTRAP                 /* turn on the trap as per the MIL-STD */
198 #define ULAW_BIAS        0x84	/* Bias for linear code. */
199 
200 /*! \brief Encode a linear sample to u-law
201     \param linear The sample to encode.
202     \return The u-law value.
203 */
linear_to_ulaw(int linear)204 	static __inline__ uint8_t linear_to_ulaw(int linear) {
205 		uint8_t u_val;
206 		int mask;
207 		int seg;
208 
209 		/* Get the sign and the magnitude of the value. */
210 		if (linear < 0) {
211 			linear = ULAW_BIAS - linear;
212 			mask = 0x7F;
213 		} else {
214 			linear = ULAW_BIAS + linear;
215 			mask = 0xFF;
216 		}
217 
218 		seg = top_bit(linear | 0xFF) - 7;
219 
220 		/*
221 		 * Combine the sign, segment, quantization bits,
222 		 * and complement the code word.
223 		 */
224 		if (seg >= 8)
225 			u_val = (uint8_t) (0x7F ^ mask);
226 		else
227 			u_val = (uint8_t) (((seg << 4) | ((linear >> (seg + 3)) & 0xF)) ^ mask);
228 #ifdef ULAW_ZEROTRAP
229 		/* Optional ITU trap */
230 		if (u_val == 0)
231 			u_val = 0x02;
232 #endif
233 		return u_val;
234 	}
235 /*- End of function --------------------------------------------------------*/
236 
237 /*! \brief Decode an u-law sample to a linear value.
238     \param ulaw The u-law sample to decode.
239     \return The linear value.
240 */
ulaw_to_linear(uint8_t ulaw)241 	static __inline__ int16_t ulaw_to_linear(uint8_t ulaw) {
242 		int t;
243 
244 		/* Complement to obtain normal u-law value. */
245 		ulaw = ~ulaw;
246 		/*
247 		 * Extract and bias the quantization bits. Then
248 		 * shift up by the segment number and subtract out the bias.
249 		 */
250 		t = (((ulaw & 0x0F) << 3) + ULAW_BIAS) << (((int) ulaw & 0x70) >> 4);
251 		return (int16_t) ((ulaw & 0x80) ? (ULAW_BIAS - t) : (t - ULAW_BIAS));
252 	}
253 /*- End of function --------------------------------------------------------*/
254 
255 /*
256  * A-law is basically as follows:
257  *
258  *      Linear Input Code        Compressed Code
259  *      -----------------        ---------------
260  *      0000000wxyza             000wxyz
261  *      0000001wxyza             001wxyz
262  *      000001wxyzab             010wxyz
263  *      00001wxyzabc             011wxyz
264  *      0001wxyzabcd             100wxyz
265  *      001wxyzabcde             101wxyz
266  *      01wxyzabcdef             110wxyz
267  *      1wxyzabcdefg             111wxyz
268  *
269  * For further information see John C. Bellamy's Digital Telephony, 1982,
270  * John Wiley & Sons, pps 98-111 and 472-476.
271  */
272 
273 #define ALAW_AMI_MASK       0x55
274 
275 /*! \brief Encode a linear sample to A-law
276     \param linear The sample to encode.
277     \return The A-law value.
278 */
linear_to_alaw(int linear)279 	static __inline__ uint8_t linear_to_alaw(int linear) {
280 		int mask;
281 		int seg;
282 
283 		if (linear >= 0) {
284 			/* Sign (bit 7) bit = 1 */
285 			mask = ALAW_AMI_MASK | 0x80;
286 		} else {
287 			/* Sign (bit 7) bit = 0 */
288 			mask = ALAW_AMI_MASK;
289 			linear = -linear - 8;
290 		}
291 
292 		/* Convert the scaled magnitude to segment number. */
293 		seg = top_bit(linear | 0xFF) - 7;
294 		if (seg >= 8) {
295 			if (linear >= 0) {
296 				/* Out of range. Return maximum value. */
297 				return (uint8_t) (0x7F ^ mask);
298 			}
299 			/* We must be just a tiny step below zero */
300 			return (uint8_t) (0x00 ^ mask);
301 		}
302 		/* Combine the sign, segment, and quantization bits. */
303 		return (uint8_t) (((seg << 4) | ((linear >> ((seg) ? (seg + 3) : 4)) & 0x0F)) ^ mask);
304 	}
305 /*- End of function --------------------------------------------------------*/
306 
307 /*! \brief Decode an A-law sample to a linear value.
308     \param alaw The A-law sample to decode.
309     \return The linear value.
310 */
alaw_to_linear(uint8_t alaw)311 	static __inline__ int16_t alaw_to_linear(uint8_t alaw) {
312 		int i;
313 		int seg;
314 
315 		alaw ^= ALAW_AMI_MASK;
316 		i = ((alaw & 0x0F) << 4);
317 		seg = (((int) alaw & 0x70) >> 4);
318 		if (seg)
319 			i = (i + 0x108) << (seg - 1);
320 		else
321 			i += 8;
322 		return (int16_t) ((alaw & 0x80) ? i : -i);
323 	}
324 /*- End of function --------------------------------------------------------*/
325 
326 /*! \brief Transcode from A-law to u-law, using the procedure defined in G.711.
327     \param alaw The A-law sample to transcode.
328     \return The best matching u-law value.
329 */
330 	uint8_t alaw_to_ulaw(uint8_t alaw);
331 
332 /*! \brief Transcode from u-law to A-law, using the procedure defined in G.711.
333     \param ulaw The u-law sample to transcode.
334     \return The best matching A-law value.
335 */
336 	uint8_t ulaw_to_alaw(uint8_t ulaw);
337 
338 #ifdef __cplusplus
339 }
340 #endif
341 
342 #endif
343 /*- End of file ------------------------------------------------------------*/
344