2 * This routine converts from linear to ulaw
5 * Craig Reese: IDA/Supercomputing Research Center
6 * Joe Campbell: Department of Defense
9 * 1) CCITT Recommendation G.711 (very difficult to follow)
10 * 2) "A New Digital Technique for Implementation of Any
11 * Continuous PCM Companding Law," Villeret, Michel,
12 * et al. 1973 IEEE Int. Conf. on Communications, Vol 1,
13 * 1973, pg. 11.12-11.17
14 * 3) MIL-STD-188-113,"Interoperability and Performance Standards
15 * for Analog-to_Digital Conversion Techniques,"
18 * Input: Signed 16 bit linear sample
19 * Output: 8 bit ulaw sample
28 #undef ZEROTRAP /* turn on the trap as per the MIL-STD */
29 #define BIAS 0x84 /* define the add-in bias for 16 bit samples */
33 mulaw_encode (gint16 * in, guint8 * out, gint numsamples)
35 static gint16 exp_lut[256] = { 0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
36 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
37 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
38 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
39 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
40 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
41 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
42 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
43 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
44 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
45 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
46 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
47 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
48 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
49 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
50 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7
52 gint16 sign, exponent, mantissa, i;
56 for (i = 0; i < numsamples; i++) {
58 /** get the sample into sign-magnitude **/
59 sign = (sample >> 8) & 0x80; /* set aside the sign */
61 sample = -sample; /* get magnitude */
63 /* sample can be zero because we can overflow in the inversion,
64 * checking against the unsigned version solves this */
65 if (((guint16) sample) > CLIP)
66 sample = CLIP; /* clip the magnitude */
68 /** convert from 16 bit linear to ulaw **/
69 sample = sample + BIAS;
70 exponent = exp_lut[(sample >> 7) & 0xFF];
71 mantissa = (sample >> (exponent + 3)) & 0x0F;
72 ulawbyte = ~(sign | (exponent << 4) | mantissa);
75 ulawbyte = 0x02; /* optional CCITT trap */
82 * This routine converts from ulaw to 16 bit linear
85 * Craig Reese: IDA/Supercomputing Research Center
88 * 1) CCITT Recommendation G.711 (very difficult to follow)
89 * 2) MIL-STD-188-113,"Interoperability and Performance Standards
90 * for Analog-to_Digital Conversion Techniques,"
93 * Input: 8 bit ulaw sample
94 * Output: signed 16 bit linear sample
98 mulaw_decode (guint8 * in, gint16 * out, gint numsamples)
100 static gint16 exp_lut[8] = { 0, 132, 396, 924, 1980, 4092, 8316, 16764 };
101 gint16 sign, exponent, mantissa;
105 for (i = 0; i < numsamples; i++) {
107 ulawbyte = ~ulawbyte;
108 sign = (ulawbyte & 0x80);
109 exponent = (ulawbyte >> 4) & 0x07;
110 mantissa = ulawbyte & 0x0F;
111 linear = exp_lut[exponent] + (mantissa << (exponent + 3));