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 gint16 exp_lut[256] = { 0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
38 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
39 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
40 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
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 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 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,
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
54 gint16 sign, exponent, mantissa;
59 for (i = 0; i < numsamples; i++) {
61 /** get the sample into sign-magnitude **/
62 sign = (sample >> 8) & 0x80; /* set aside the sign */
64 sample = -sample; /* get magnitude */
66 /* sample can be zero because we can overflow in the inversion,
67 * checking against the unsigned version solves this */
68 if (((guint16) sample) > CLIP)
69 sample = CLIP; /* clip the magnitude */
71 /** convert from 16 bit linear to ulaw **/
72 sample = sample + BIAS;
73 exponent = exp_lut[(sample >> 7) & 0xFF];
74 mantissa = (sample >> (exponent + 3)) & 0x0F;
75 ulawbyte = ~(sign | (exponent << 4) | mantissa);
78 ulawbyte = 0x02; /* optional CCITT trap */
85 * This routine converts from ulaw to 16 bit linear
88 * Craig Reese: IDA/Supercomputing Research Center
91 * 1) CCITT Recommendation G.711 (very difficult to follow)
92 * 2) MIL-STD-188-113,"Interoperability and Performance Standards
93 * for Analog-to_Digital Conversion Techniques,"
96 * Input: 8 bit ulaw sample
97 * Output: signed 16 bit linear sample
101 mulaw_decode (guint8 * in, gint16 * out, gint numsamples)
103 static gint16 exp_lut[8] = { 0, 132, 396, 924, 1980, 4092, 8316, 16764 };
104 gint16 sign, exponent, mantissa;
109 for (i = 0; i < numsamples; i++) {
111 ulawbyte = ~ulawbyte;
112 sign = (ulawbyte & 0x80);
113 exponent = (ulawbyte >> 4) & 0x07;
114 mantissa = ulawbyte & 0x0F;
115 linear = exp_lut[exponent] + (mantissa << (exponent + 3));