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 #define 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 = CLIP; /* clip the magnitude */
64 /** convert from 16 bit linear to ulaw **/
65 sample = sample + BIAS;
66 exponent = exp_lut[(sample >> 7) & 0xFF];
67 mantissa = (sample >> (exponent + 3)) & 0x0F;
68 ulawbyte = ~(sign | (exponent << 4) | mantissa);
71 ulawbyte = 0x02; /* optional CCITT trap */
78 * This routine converts from ulaw to 16 bit linear
81 * Craig Reese: IDA/Supercomputing Research Center
84 * 1) CCITT Recommendation G.711 (very difficult to follow)
85 * 2) MIL-STD-188-113,"Interoperability and Performance Standards
86 * for Analog-to_Digital Conversion Techniques,"
89 * Input: 8 bit ulaw sample
90 * Output: signed 16 bit linear sample
94 mulaw_decode (guint8 * in, gint16 * out, gint numsamples)
96 static gint16 exp_lut[8] = { 0, 132, 396, 924, 1980, 4092, 8316, 16764 };
97 gint16 sign, exponent, mantissa;
101 for (i = 0; i < numsamples; i++) {
103 ulawbyte = ~ulawbyte;
104 sign = (ulawbyte & 0x80);
105 exponent = (ulawbyte >> 4) & 0x07;
106 mantissa = ulawbyte & 0x0F;
107 linear = exp_lut[exponent] + (mantissa << (exponent + 3));