]>
git.saurik.com Git - wxWidgets.git/blob - utils/wxMMedia2/lib/g711.cpp
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
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.
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.
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.
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.
22 * Sun Microsystems, Inc.
24 * Mountain View, California 94043
27 #include <wx/wxprec.h>
32 * u-law, A-law and linear PCM conversions.
34 #define SIGN_BIT (0x80) /* Sign bit for a A-law byte. */
35 #define QUANT_MASK (0xf) /* Quantization field mask. */
36 #define NSEGS (8) /* Number of A-law segments. */
37 #define SEG_SHIFT (4) /* Left shift for segment number. */
38 #define SEG_MASK (0x70) /* Segment field mask. */
40 static short seg_end
[8] = {0xFF, 0x1FF, 0x3FF, 0x7FF,
41 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF};
43 /* copy from CCITT G.711 specifications */
44 unsigned char _u2a
[128] = { /* u- to A-law conversions */
45 1, 1, 2, 2, 3, 3, 4, 4,
46 5, 5, 6, 6, 7, 7, 8, 8,
47 9, 10, 11, 12, 13, 14, 15, 16,
48 17, 18, 19, 20, 21, 22, 23, 24,
49 25, 27, 29, 31, 33, 34, 35, 36,
50 37, 38, 39, 40, 41, 42, 43, 44,
51 46, 48, 49, 50, 51, 52, 53, 54,
52 55, 56, 57, 58, 59, 60, 61, 62,
53 64, 65, 66, 67, 68, 69, 70, 71,
54 72, 73, 74, 75, 76, 77, 78, 79,
55 81, 82, 83, 84, 85, 86, 87, 88,
56 89, 90, 91, 92, 93, 94, 95, 96,
57 97, 98, 99, 100, 101, 102, 103, 104,
58 105, 106, 107, 108, 109, 110, 111, 112,
59 113, 114, 115, 116, 117, 118, 119, 120,
60 121, 122, 123, 124, 125, 126, 127, 128};
62 unsigned char _a2u
[128] = { /* A- to u-law conversions */
63 1, 3, 5, 7, 9, 11, 13, 15,
64 16, 17, 18, 19, 20, 21, 22, 23,
65 24, 25, 26, 27, 28, 29, 30, 31,
66 32, 32, 33, 33, 34, 34, 35, 35,
67 36, 37, 38, 39, 40, 41, 42, 43,
68 44, 45, 46, 47, 48, 48, 49, 49,
69 50, 51, 52, 53, 54, 55, 56, 57,
70 58, 59, 60, 61, 62, 63, 64, 64,
71 65, 66, 67, 68, 69, 70, 71, 72,
72 73, 74, 75, 76, 77, 78, 79, 79,
73 80, 81, 82, 83, 84, 85, 86, 87,
74 88, 89, 90, 91, 92, 93, 94, 95,
75 96, 97, 98, 99, 100, 101, 102, 103,
76 104, 105, 106, 107, 108, 109, 110, 111,
77 112, 113, 114, 115, 116, 117, 118, 119,
78 120, 121, 122, 123, 124, 125, 126, 127};
88 for (i
= 0; i
< size
; i
++) {
96 * linear2alaw() - Convert a 16-bit linear PCM value to 8-bit A-law
98 * linear2alaw() accepts an 16-bit integer and encodes it as A-law data.
100 * Linear Input Code Compressed Code
101 * ------------------------ ---------------
102 * 0000000wxyza 000wxyz
103 * 0000001wxyza 001wxyz
104 * 000001wxyzab 010wxyz
105 * 00001wxyzabc 011wxyz
106 * 0001wxyzabcd 100wxyz
107 * 001wxyzabcde 101wxyz
108 * 01wxyzabcdef 110wxyz
109 * 1wxyzabcdefg 111wxyz
111 * For further information see John C. Bellamy's Digital Telephony, 1982,
112 * John Wiley & Sons, pps 98-111 and 472-476.
116 int pcm_val
) /* 2's complement (16-bit range) */
123 mask
= 0xD5; /* sign (7th) bit = 1 */
125 mask
= 0x55; /* sign bit = 0 */
126 pcm_val
= -pcm_val
- 8;
129 /* Convert the scaled magnitude to segment number. */
130 seg
= search(pcm_val
, seg_end
, 8);
132 /* Combine the sign, segment, and quantization bits. */
134 if (seg
>= 8) /* out of range, return maximum value. */
135 return (0x7F ^ mask
);
137 aval
= seg
<< SEG_SHIFT
;
139 aval
|= (pcm_val
>> 4) & QUANT_MASK
;
141 aval
|= (pcm_val
>> (seg
+ 3)) & QUANT_MASK
;
142 return (aval
^ mask
);
147 * alaw2linear() - Convert an A-law value to 16-bit linear PCM
159 t
= (a_val
& QUANT_MASK
) << 4;
160 seg
= ((unsigned)a_val
& SEG_MASK
) >> SEG_SHIFT
;
172 return ((a_val
& SIGN_BIT
) ? t
: -t
);
175 #define BIAS (0x84) /* Bias for linear code. */
178 * linear2ulaw() - Convert a linear PCM value to u-law
180 * In order to simplify the encoding process, the original linear magnitude
181 * is biased by adding 33 which shifts the encoding range from (0 - 8158) to
182 * (33 - 8191). The result can be seen in the following encoding table:
184 * Biased Linear Input Code Compressed Code
185 * ------------------------ ---------------
186 * 00000001wxyza 000wxyz
187 * 0000001wxyzab 001wxyz
188 * 000001wxyzabc 010wxyz
189 * 00001wxyzabcd 011wxyz
190 * 0001wxyzabcde 100wxyz
191 * 001wxyzabcdef 101wxyz
192 * 01wxyzabcdefg 110wxyz
193 * 1wxyzabcdefgh 111wxyz
195 * Each biased linear code has a leading 1 which identifies the segment
196 * number. The value of the segment number is equal to 7 minus the number
197 * of leading 0's. The quantization interval is directly available as the
198 * four bits wxyz. * The trailing bits (a - h) are ignored.
200 * Ordinarily the complement of the resulting code word is used for
201 * transmission, and so the code word is complemented before it is returned.
203 * For further information see John C. Bellamy's Digital Telephony, 1982,
204 * John Wiley & Sons, pps 98-111 and 472-476.
208 int pcm_val
) /* 2's complement (16-bit range) */
214 /* Get the sign and the magnitude of the value. */
216 pcm_val
= BIAS
- pcm_val
;
223 /* Convert the scaled magnitude to segment number. */
224 seg
= search(pcm_val
, seg_end
, 8);
227 * Combine the sign, segment, quantization bits;
228 * and complement the code word.
230 if (seg
>= 8) /* out of range, return maximum value. */
231 return (0x7F ^ mask
);
233 uval
= (seg
<< 4) | ((pcm_val
>> (seg
+ 3)) & 0xF);
234 return (uval
^ mask
);
240 * ulaw2linear() - Convert a u-law value to 16-bit linear PCM
242 * First, a biased linear code is derived from the code word. An unbiased
243 * output can then be obtained by subtracting 33 from the biased code.
245 * Note that this function expects to be passed the complement of the
246 * original code word. This is in keeping with ISDN conventions.
254 /* Complement to obtain normal u-law value. */
258 * Extract and bias the quantization bits. Then
259 * shift up by the segment number and subtract out the bias.
261 t
= ((u_val
& QUANT_MASK
) << 3) + BIAS
;
262 t
<<= ((unsigned)u_val
& SEG_MASK
) >> SEG_SHIFT
;
264 return ((u_val
& SIGN_BIT
) ? (BIAS
- t
) : (t
- BIAS
));
267 /* A-law to u-law conversion */
273 return ((aval
& 0x80) ? (0xFF ^ _a2u
[aval
^ 0xD5]) :
274 (0x7F ^ _a2u
[aval
^ 0x55]));
277 /* u-law to A-law conversion */
283 return ((uval
& 0x80) ? (0xD5 ^ (_u2a
[0xFF ^ uval
] - 1)) :
284 (0x55 ^ (_u2a
[0x7F ^ uval
] - 1)));