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 static int seg_uend[8] = {0x3F, 0x7F, 0xFF, 0x1FF,
53 			    0x3FF, 0x7FF, 0xFFF, 0x1FFF};
54 
55 /* copy from CCITT G.711 specifications */
56 unsigned char u2a[128] = {			/* u- to A-law conversions */
57 	1,	1,	2,	2,	3,	3,	4,	4,
58 	5,	5,	6,	6,	7,	7,	8,	8,
59 	9,	10,	11,	12,	13,	14,	15,	16,
60 	17,	18,	19,	20,	21,	22,	23,	24,
61 	25,	27,	29,	31,	33,	34,	35,	36,
62 	37,	38,	39,	40,	41,	42,	43,	44,
63 	46,	48,	49,	50,	51,	52,	53,	54,
64 	55,	56,	57,	58,	59,	60,	61,	62,
65 	64,	65,	66,	67,	68,	69,	70,	71,
66 	72,	73,	74,	75,	76,	77,	78,	79,
67 /* corrected:
68 	81,	82,	83,	84,	85,	86,	87,	88,
69    should be: */
70 	80,	82,	83,	84,	85,	86,	87,	88,
71 	89,	90,	91,	92,	93,	94,	95,	96,
72 	97,	98,	99,	100,	101,	102,	103,	104,
73 	105,	106,	107,	108,	109,	110,	111,	112,
74 	113,	114,	115,	116,	117,	118,	119,	120,
75 	121,	122,	123,	124,	125,	126,	127,	128};
76 
77 unsigned char a2u[128] = {			/* A- to u-law conversions */
78 	1,	3,	5,	7,	9,	11,	13,	15,
79 	16,	17,	18,	19,	20,	21,	22,	23,
80 	24,	25,	26,	27,	28,	29,	30,	31,
81 	32,	32,	33,	33,	34,	34,	35,	35,
82 	36,	37,	38,	39,	40,	41,	42,	43,
83 	44,	45,	46,	47,	48,	48,	49,	49,
84 	50,	51,	52,	53,	54,	55,	56,	57,
85 	58,	59,	60,	61,	62,	63,	64,	64,
86 	65,	66,	67,	68,	69,	70,	71,	72,
87 /* corrected:
88 	73,	74,	75,	76,	77,	78,	79,	79,
89    should be: */
90 	73,	74,	75,	76,	77,	78,	79,	80,
91 
92 	80,	81,	82,	83,	84,	85,	86,	87,
93 	88,	89,	90,	91,	92,	93,	94,	95,
94 	96,	97,	98,	99,	100,	101,	102,	103,
95 	104,	105,	106,	107,	108,	109,	110,	111,
96 	112,	113,	114,	115,	116,	117,	118,	119,
97 	120,	121,	122,	123,	124,	125,	126,	127};
98 
99 static int
search(int val,int * table,int size)100 search(
101 	int		val,	/* changed from "short" *drago* */
102 	int *	table,
103 	int		size)	/* changed from "short" *drago* */
104 {
105 	int		i;		/* changed from "short" *drago* */
106 
107 	for (i = 0; i < size; i++) {
108 		if (val <= *table++)
109 			return (i);
110 	}
111 	return (size);
112 }
113 
114 /*
115  * linear2alaw() - Convert a 16-bit linear PCM value to 8-bit A-law
116  *
117  * linear2alaw() accepts an 16-bit integer and encodes it as A-law data.
118  *
119  *		Linear Input Code	Compressed Code
120  *	------------------------	---------------
121  *	0000000wxyza			000wxyz
122  *	0000001wxyza			001wxyz
123  *	000001wxyzab			010wxyz
124  *	00001wxyzabc			011wxyz
125  *	0001wxyzabcd			100wxyz
126  *	001wxyzabcde			101wxyz
127  *	01wxyzabcdef			110wxyz
128  *	1wxyzabcdefg			111wxyz
129  *
130  * For further information see John C. Bellamy's Digital Telephony, 1982,
131  * John Wiley & Sons, pps 98-111 and 472-476.
132  */
linear2alaw(int pcm_val)133 int linear2alaw(int	pcm_val)        /* 2's complement (16-bit range) */
134                                         /* changed from "short" *drago* */
135 {
136 	int		mask;	/* changed from "short" *drago* */
137 	int		seg;	/* changed from "short" *drago* */
138 	int		aval;
139 
140 	pcm_val = pcm_val >> 3;
141 
142 	if (pcm_val >= 0) {
143 		mask = 0xD5;		/* sign (7th) bit = 1 */
144 	} else {
145 		mask = 0x55;		/* sign bit = 0 */
146 		pcm_val = -pcm_val - 1;
147 	}
148 
149 	/* Convert the scaled magnitude to segment number. */
150 	seg = search(pcm_val, seg_aend, 8);
151 
152 	/* Combine the sign, segment, and quantization bits. */
153 
154 	if (seg >= 8)		/* out of range, return maximum value. */
155 		return (0x7F ^ mask);
156 	else {
157 		aval = seg << SEG_SHIFT;
158 		if (seg < 2)
159 			aval |= (pcm_val >> 1) & QUANT_MASK;
160 		else
161 			aval |= (pcm_val >> seg) & QUANT_MASK;
162 		return (aval ^ mask);
163 	}
164 }
165 
166 /*
167  * alaw2linear() - Convert an A-law value to 16-bit linear PCM
168  *
169  */
alaw2linear(int a_val)170 int alaw2linear(int	a_val)
171 {
172 	int		t;      /* changed from "short" *drago* */
173 	int		seg;    /* changed from "short" *drago* */
174 
175 	a_val ^= 0x55;
176 
177 	t = (a_val & QUANT_MASK) << 4;
178 	seg = ((unsigned)a_val & SEG_MASK) >> SEG_SHIFT;
179 	switch (seg) {
180 	case 0:
181 		t += 8;
182 		break;
183 	case 1:
184 		t += 0x108;
185 		break;
186 	default:
187 		t += 0x108;
188 		t <<= seg - 1;
189 	}
190 	return ((a_val & SIGN_BIT) ? t : -t);
191 }
192 
193 #define	BIAS		(0x84)		/* Bias for linear code. */
194 #define CLIP            8159
195 
196 /*
197  * linear2ulaw() - Convert a linear PCM value to u-law
198  *
199  * In order to simplify the encoding process, the original linear magnitude
200  * is biased by adding 33 which shifts the encoding range from (0 - 8158) to
201  * (33 - 8191). The result can be seen in the following encoding table:
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  */
linear2ulaw(int pcm_val)225 int linear2ulaw( int	pcm_val)	/* 2's complement (16-bit range) */
226 {
227 	int		mask;
228 	int		seg;
229 	int		uval;
230 
231 	/* Get the sign and the magnitude of the value. */
232 	pcm_val = pcm_val >> 2;
233 	if (pcm_val < 0) {
234 		pcm_val = -pcm_val;
235 		mask = 0x7F;
236 	} else {
237 		mask = 0xFF;
238 	}
239         if ( pcm_val > CLIP ) pcm_val = CLIP;		/* clip the magnitude */
240 	pcm_val += (BIAS >> 2);
241 
242 	/* Convert the scaled magnitude to segment number. */
243 	seg = search(pcm_val, seg_uend, 8);
244 
245 	/*
246 	 * Combine the sign, segment, quantization bits;
247 	 * and complement the code word.
248 	 */
249 	if (seg >= 8)		/* out of range, return maximum value. */
250 		return (0x7F ^ mask);
251 	else {
252 		uval = (seg << 4) | ((pcm_val >> (seg + 1)) & 0xF);
253 		return (uval ^ mask);
254 	}
255 
256 }
257 
258 /*
259  * ulaw2linear() - Convert a u-law value to 16-bit linear PCM
260  *
261  * First, a biased linear code is derived from the code word. An unbiased
262  * output can then be obtained by subtracting 33 from the biased code.
263  *
264  * Note that this function expects to be passed the complement of the
265  * original code word. This is in keeping with ISDN conventions.
266  */
ulaw2linear(int u_val)267 int ulaw2linear( int	u_val)
268 {
269 	int t;
270 
271 	/* Complement to obtain normal u-law value. */
272 	u_val = ~u_val;
273 
274 	/*
275 	 * Extract and bias the quantization bits. Then
276 	 * shift up by the segment number and subtract out the bias.
277 	 */
278 	t = ((u_val & QUANT_MASK) << 3) + BIAS;
279 	t <<= (u_val & SEG_MASK) >> SEG_SHIFT;
280 
281 	return ((u_val & SIGN_BIT) ? (BIAS - t) : (t - BIAS));
282 }
283 
284 #if 0
285 
286 /* A-law to u-law conversion */
287 static int alaw2ulaw (int	aval)
288 {
289 	aval &= 0xff;
290 	return ((aval & 0x80) ? (0xFF ^ a2u[aval ^ 0xD5]) :
291 	    (0x7F ^ a2u[aval ^ 0x55]));
292 }
293 
294 /* u-law to A-law conversion */
295 static int ulaw2alaw (int	uval)
296 {
297 	uval &= 0xff;
298 	return ((uval & 0x80) ? (0xD5 ^ (u2a[0xFF ^ uval] - 1)) :
299 	    (0x55 ^ (u2a[0x7F ^ uval] - 1)));
300 }
301 
302 #endif
303 
304