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 #include "mulaw-conversion.h"
30 #undef ZEROTRAP /* turn on the trap as per the MIL-STD */
31 #define BIAS 0x84 /* define the add-in bias for 16 bit samples */
35 mulaw_encode (gint16 * in, guint8 * out, gint numsamples)
37 static const gint16 exp_lut[256] = {
38 0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
39 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
40 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
41 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
42 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
43 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
44 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
45 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
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,
51 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
52 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
53 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7
55 gint16 sign, exponent, mantissa;
60 for (i = 0; i < numsamples; i++) {
62 /** get the sample into sign-magnitude **/
63 sign = (sample >> 8) & 0x80; /* set aside the sign */
65 sample = -sample; /* get magnitude */
67 /* sample can be zero because we can overflow in the inversion,
68 * checking against the unsigned version solves this */
69 if (((guint16) sample) > CLIP)
70 sample = CLIP; /* clip the magnitude */
72 /** convert from 16 bit linear to ulaw **/
73 sample = sample + BIAS;
74 exponent = exp_lut[(sample >> 7) & 0xFF];
75 mantissa = (sample >> (exponent + 3)) & 0x0F;
76 ulawbyte = ~(sign | (exponent << 4) | mantissa);
79 ulawbyte = 0x02; /* optional CCITT trap */
86 * This routine converts from ulaw to 16 bit linear
89 * Craig Reese: IDA/Supercomputing Research Center
92 * 1) CCITT Recommendation G.711 (very difficult to follow)
93 * 2) MIL-STD-188-113,"Interoperability and Performance Standards
94 * for Analog-to_Digital Conversion Techniques,"
97 * Input: 8 bit ulaw sample
98 * Output: signed 16 bit linear sample
102 mulaw_decode (guint8 * in, gint16 * out, gint numsamples)
104 static const gint16 exp_lut[8] =
105 { 0, 132, 396, 924, 1980, 4092, 8316, 16764 };
106 gint16 sign, exponent, mantissa;
111 for (i = 0; i < numsamples; i++) {
113 ulawbyte = ~ulawbyte;
114 sign = (ulawbyte & 0x80);
115 exponent = (ulawbyte >> 4) & 0x07;
116 mantissa = ulawbyte & 0x0F;
117 linear = exp_lut[exponent] + (mantissa << (exponent + 3));