1 /*
2  * This source code is a product of Sun Microsystems, Inc. and is provided
3  * for unrestricted use.  Users may copy or modify this source code without
4  * charge.
5  *
6  * SUN SOURCE CODE IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING
7  * THE WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
8  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
9  *
10  * Sun source code is provided with no support and without any obligation on
11  * the part of Sun Microsystems, Inc. to assist in its use, correction,
12  * modification or enhancement.
13  *
14  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
15  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS SOFTWARE
16  * OR ANY PART THEREOF.
17  *
18  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
19  * or profits or other special, indirect and consequential damages, even if
20  * Sun has been advised of the possibility of such damages.
21  *
22  * Sun Microsystems, Inc.
23  * 2550 Garcia Avenue
24  * Mountain View, California  94043
25  */
26 
27 /*
28  * g711.c
29  *
30  * u-law, A-law and linear PCM conversions.
31  */
32 
33 /*
34  * December 30, 1994:
35  * Functions linear2alaw, linear2ulaw have been updated to correctly
36  * convert unquantized 16 bit values.
37  * Tables for direct u- to A-law and A- to u-law conversions have been
38  * corrected.
39  * Borge Lindberg, Center for PersonKommunikation, Aalborg University.
40  * bli@cpk.auc.dk
41  *
42  */
43 
44 #define	SIGN_BIT	(0x80)		/* Sign bit for a A-law byte. */
45 #define	QUANT_MASK	(0xf)		/* Quantization field mask. */
46 #define	NSEGS		(8)		/* Number of A-law segments. */
47 #define	SEG_SHIFT	(4)		/* Left shift for segment number. */
48 #define	SEG_MASK	(0x70)		/* Segment field mask. */
49 
50 static int seg_aend[8] = {0x1F, 0x3F, 0x7F, 0xFF,
51 			    0x1FF, 0x3FF, 0x7FF, 0xFFF};
52 
53 /* copy from CCITT G.711 specifications */
54 unsigned char u2a[128] = {			/* u- to A-law conversions */
55 	1,	1,	2,	2,	3,	3,	4,	4,
56 	5,	5,	6,	6,	7,	7,	8,	8,
57 	9,	10,	11,	12,	13,	14,	15,	16,
58 	17,	18,	19,	20,	21,	22,	23,	24,
59 	25,	27,	29,	31,	33,	34,	35,	36,
60 	37,	38,	39,	40,	41,	42,	43,	44,
61 	46,	48,	49,	50,	51,	52,	53,	54,
62 	55,	56,	57,	58,	59,	60,	61,	62,
63 	64,	65,	66,	67,	68,	69,	70,	71,
64 	72,	73,	74,	75,	76,	77,	78,	79,
65 /* corrected:
66 	81,	82,	83,	84,	85,	86,	87,	88,
67    should be: */
68 	80,	82,	83,	84,	85,	86,	87,	88,
69 	89,	90,	91,	92,	93,	94,	95,	96,
70 	97,	98,	99,	100,	101,	102,	103,	104,
71 	105,	106,	107,	108,	109,	110,	111,	112,
72 	113,	114,	115,	116,	117,	118,	119,	120,
73 	121,	122,	123,	124,	125,	126,	127,	128};
74 
75 unsigned char a2u[128] = {			/* A- to u-law conversions */
76 	1,	3,	5,	7,	9,	11,	13,	15,
77 	16,	17,	18,	19,	20,	21,	22,	23,
78 	24,	25,	26,	27,	28,	29,	30,	31,
79 	32,	32,	33,	33,	34,	34,	35,	35,
80 	36,	37,	38,	39,	40,	41,	42,	43,
81 	44,	45,	46,	47,	48,	48,	49,	49,
82 	50,	51,	52,	53,	54,	55,	56,	57,
83 	58,	59,	60,	61,	62,	63,	64,	64,
84 	65,	66,	67,	68,	69,	70,	71,	72,
85 /* corrected:
86 	73,	74,	75,	76,	77,	78,	79,	79,
87    should be: */
88 	73,	74,	75,	76,	77,	78,	79,	80,
89 
90 	80,	81,	82,	83,	84,	85,	86,	87,
91 	88,	89,	90,	91,	92,	93,	94,	95,
92 	96,	97,	98,	99,	100,	101,	102,	103,
93 	104,	105,	106,	107,	108,	109,	110,	111,
94 	112,	113,	114,	115,	116,	117,	118,	119,
95 	120,	121,	122,	123,	124,	125,	126,	127};
96 
97 static int
search(int val,int * table,int size)98 search(
99 	int		val,	//changed from "short" *drago*
100 	int *	table,
101 	int		size)	//changed from "short" *drago*
102 {
103 	int		i;		//changed from "short" *drago*
104 
105 	for (i = 0; i < size; i++) {
106 		if (val <= *table++)
107 			return (i);
108 	}
109 	return (size);
110 }
111 
112 /*
113  * linear2alaw() - Convert a 16-bit linear PCM value to 8-bit A-law
114  *
115  * linear2alaw() accepts an 16-bit integer and encodes it as A-law data.
116  *
117  *		Linear Input Code	Compressed Code
118  *	------------------------	---------------
119  *	0000000wxyza			000wxyz
120  *	0000001wxyza			001wxyz
121  *	000001wxyzab			010wxyz
122  *	00001wxyzabc			011wxyz
123  *	0001wxyzabcd			100wxyz
124  *	001wxyzabcde			101wxyz
125  *	01wxyzabcdef			110wxyz
126  *	1wxyzabcdefg			111wxyz
127  *
128  * For further information see John C. Bellamy's Digital Telephony, 1982,
129  * John Wiley & Sons, pps 98-111 and 472-476.
130  */
linear2alaw(int pcm_val)131 int linear2alaw(int	pcm_val)	/* 2's complement (16-bit range) */
132 								//changed from "short" *drago*
133 {
134 	int		mask;	//changed from "short" *drago*
135 	int		seg;	//changed from "short" *drago*
136 	int		aval;
137 
138 	pcm_val = pcm_val >> 3;
139 
140 	if (pcm_val >= 0) {
141 		mask = 0xD5;		/* sign (7th) bit = 1 */
142 	} else {
143 		mask = 0x55;		/* sign bit = 0 */
144 		pcm_val = -pcm_val - 1;
145 	}
146 
147 	/* Convert the scaled magnitude to segment number. */
148 	seg = search(pcm_val, seg_aend, 8);
149 
150 	/* Combine the sign, segment, and quantization bits. */
151 
152 	if (seg >= 8)		/* out of range, return maximum value. */
153 		return (0x7F ^ mask);
154 	else {
155 		aval = seg << SEG_SHIFT;
156 		if (seg < 2)
157 			aval |= (pcm_val >> 1) & QUANT_MASK;
158 		else
159 			aval |= (pcm_val >> seg) & QUANT_MASK;
160 		return (aval ^ mask);
161 	}
162 }
163 
164 /*
165  * alaw2linear() - Convert an A-law value to 16-bit linear PCM
166  *
167  */
alaw2linear(int a_val)168 int alaw2linear(int	a_val)
169 {
170 	int		t;		//changed from "short" *drago*
171 	int		seg;	//changed from "short" *drago*
172 
173 	a_val ^= 0x55;
174 
175 	t = (a_val & QUANT_MASK) << 4;
176 	seg = ((unsigned)a_val & SEG_MASK) >> SEG_SHIFT;
177 	switch (seg) {
178 	case 0:
179 		t += 8;
180 		break;
181 	case 1:
182 		t += 0x108;
183 		break;
184 	default:
185 		t += 0x108;
186 		t <<= seg - 1;
187 	}
188 	return ((a_val & SIGN_BIT) ? t : -t);
189 }
190 
191 #define	BIAS		(0x84)		/* Bias for linear code. */
192 #define CLIP            8159
193 
194 /*
195  * linear2ulaw() - Convert a linear PCM value to u-law
196  *
197  * In order to simplify the encoding process, the original linear magnitude
198  * is biased by adding 33 which shifts the encoding range from (0 - 8158) to
199  * (33 - 8191). The result can be seen in the following encoding table:
200  *
201  *	Biased Linear Input Code	Compressed Code
202  *	------------------------	---------------
203  *	00000001wxyza			000wxyz
204  *	0000001wxyzab			001wxyz
205  *	000001wxyzabc			010wxyz
206  *	00001wxyzabcd			011wxyz
207  *	0001wxyzabcde			100wxyz
208  *	001wxyzabcdef			101wxyz
209  *	01wxyzabcdefg			110wxyz
210  *	1wxyzabcdefgh			111wxyz
211  *
212  * Each biased linear code has a leading 1 which identifies the segment
213  * number. The value of the segment number is equal to 7 minus the number
214  * of leading 0's. The quantization interval is directly available as the
215  * four bits wxyz.  * The trailing bits (a - h) are ignored.
216  *
217  * Ordinarily the complement of the resulting code word is used for
218  * transmission, and so the code word is complemented before it is returned.
219  *
220  * For further information see John C. Bellamy's Digital Telephony, 1982,
221  * John Wiley & Sons, pps 98-111 and 472-476.
222  */
223 /*
224  * Original code has been changed to give results symmetric to 0. Before,
225  * negative values would have a small bias towards zero.
226  * Also, use bit scan operators, if available.
227  */
linear2ulaw(int pcm_val)228 int linear2ulaw( int	pcm_val)	/* 2's complement (16-bit range) */
229 {
230 	int		mask;
231 	int		seg;
232 	int		uval;
233 
234 	/* Get the sign and the magnitude of the value. */
235 	if (pcm_val < 0) {
236 		pcm_val = -pcm_val;
237 		mask = 0x7F;
238 	} else {
239 		mask = 0xFF;
240 	}
241 
242         if ( pcm_val >= (7904<<2) ) { /* Clipped range + last interval */
243 		return (0x7F ^ mask);
244 	}
245 
246 	/* Convert the scaled magnitude to segment number. */
247 #if defined(GCC_HAS_CLZ)
248 	seg = 24 - __builtin_clz(pcm_val+131);
249 #elif defined(VC_HAS_BSR)
250 	_BitScanReverse(&seg, (pcm_val+131));
251 	seg -= 8;
252 #else
253 	seg = 0;
254 	if( (pcm_val+131) >= 0x100<<3 ) seg = 4;
255 	while( (pcm_val+131) >= 0x100<<seg ) seg++;
256 #endif
257 
258 	/*
259 	 * Combine the sign, segment, quantization bits;
260 	 * and complement the code word.
261 	 */
262 	uval = (seg << 4) | (( (pcm_val+131) >> (seg + 3)) & 0xF);
263 	return (uval ^ mask);
264 
265 }
266 
267 /*
268  * ulaw2linear() - Convert a u-law value to 16-bit linear PCM
269  *
270  * First, a biased linear code is derived from the code word. An unbiased
271  * output can then be obtained by subtracting 33 from the biased code.
272  *
273  * Note that this function expects to be passed the complement of the
274  * original code word. This is in keeping with ISDN conventions.
275  */
ulaw2linear(int u_val)276 int ulaw2linear( int	u_val)
277 {
278 	int t;
279 
280 	/* Complement to obtain normal u-law value. */
281 	u_val = ~u_val;
282 
283 	/*
284 	 * Extract and bias the quantization bits. Then
285 	 * shift up by the segment number and subtract out the bias.
286 	 */
287 	t = ((u_val & QUANT_MASK) << 3) + BIAS;
288 	t <<= (u_val & SEG_MASK) >> SEG_SHIFT;
289 
290 	return ((u_val & SIGN_BIT) ? (BIAS - t) : (t - BIAS));
291 }
292 
293 /* A-law to u-law conversion */
alaw2ulaw(int aval)294 int alaw2ulaw (int	aval)
295 {
296 	aval &= 0xff;
297 	return ((aval & 0x80) ? (0xFF ^ a2u[aval ^ 0xD5]) :
298 	    (0x7F ^ a2u[aval ^ 0x55]));
299 }
300 
301 /* u-law to A-law conversion */
ulaw2alaw(int uval)302 int ulaw2alaw (int	uval)
303 {
304 	uval &= 0xff;
305 	return ((uval & 0x80) ? (0xD5 ^ (u2a[0xFF ^ uval] - 1)) :
306 	    (0x55 ^ (u2a[0x7F ^ uval] - 1)));
307 }
308 
309 
310