1 /*
2 * This routine converts from linear to ulaw
3 * 29 September 1989
4 *
5 * Craig Reese: IDA/Supercomputing Research Center
6 * Joe Campbell: Department of Defense
7 *
8 * References:
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,"
16 * 17 February 1987
17 *
18 * Input: Signed 16 bit linear sample
19 * Output: 8 bit ulaw sample
20 */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include <glib.h>
27
28 #include "mulaw-conversion.h"
29
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 */
32 #define CLIP 32635
33
34 void
mulaw_encode(gint16 * in,guint8 * out,gint numsamples)35 mulaw_encode (gint16 * in, guint8 * out, gint numsamples)
36 {
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
54 };
55 gint16 sign, exponent, mantissa;
56 gint16 sample;
57 guint8 ulawbyte;
58 gint i;
59
60 for (i = 0; i < numsamples; i++) {
61 sample = in[i];
62 /** get the sample into sign-magnitude **/
63 sign = (sample >> 8) & 0x80; /* set aside the sign */
64 if (sign != 0) {
65 sample = -sample; /* get magnitude */
66 }
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 */
71
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);
77 #ifdef ZEROTRAP
78 if (ulawbyte == 0)
79 ulawbyte = 0x02; /* optional CCITT trap */
80 #endif
81 out[i] = ulawbyte;
82 }
83 }
84
85 /*
86 * This routine converts from ulaw to 16 bit linear
87 * 29 September 1989
88 *
89 * Craig Reese: IDA/Supercomputing Research Center
90 *
91 * References:
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,"
95 * 17 February 1987
96 *
97 * Input: 8 bit ulaw sample
98 * Output: signed 16 bit linear sample
99 */
100
101 void
mulaw_decode(guint8 * in,gint16 * out,gint numsamples)102 mulaw_decode (guint8 * in, gint16 * out, gint numsamples)
103 {
104 static const gint16 exp_lut[8] =
105 { 0, 132, 396, 924, 1980, 4092, 8316, 16764 };
106 gint16 sign, exponent, mantissa;
107 guint8 ulawbyte;
108 gint16 linear;
109 gint i;
110
111 for (i = 0; i < numsamples; i++) {
112 ulawbyte = in[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));
118 if (sign != 0)
119 linear = -linear;
120 out[i] = linear;
121 }
122 }
123