1 /*************************************************************************/
2 /* */
3 /* Language Technologies Institute */
4 /* Carnegie Mellon University */
5 /* Copyright (c) 2000 */
6 /* All Rights Reserved. */
7 /* */
8 /* Permission is hereby granted, free of charge, to use and distribute */
9 /* this software and its documentation without restriction, including */
10 /* without limitation the rights to use, copy, modify, merge, publish, */
11 /* distribute, sublicense, and/or sell copies of this work, and to */
12 /* permit persons to whom this work is furnished to do so, subject to */
13 /* the following conditions: */
14 /* 1. The code must retain the above copyright notice, this list of */
15 /* conditions and the following disclaimer. */
16 /* 2. Any modifications must be clearly marked as such. */
17 /* 3. Original authors' names are not deleted. */
18 /* 4. The authors' names are not used to endorse or promote products */
19 /* derived from this software without specific prior written */
20 /* permission. */
21 /* */
22 /* CARNEGIE MELLON UNIVERSITY AND THE CONTRIBUTORS TO THIS WORK */
23 /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */
24 /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */
25 /* SHALL CARNEGIE MELLON UNIVERSITY NOR THE CONTRIBUTORS BE LIABLE */
26 /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */
27 /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */
28 /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */
29 /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */
30 /* THIS SOFTWARE. */
31 /* */
32 /*************************************************************************/
33 /* Author: Alan W Black (awb@cs.cmu.edu) */
34 /* Date: October 2000 */
35 /*************************************************************************/
36 /* */
37 /* Convertion routines for various waveform encodings */
38 /* */
39 /* This contains software written external to Flite */
40 /* ulaw code came via the Edinburgh Speech Tools */
41 /* g72x codec came from Sun Microsystems */
42 /* */
43 /*************************************************************************/
44
45 #include "g72x.h"
46
47 /*
48 ** This routine converts from linear to ulaw.
49 **
50 ** Craig Reese: IDA/Supercomputing Research Center
51 ** Joe Campbell: Department of Defense
52 ** 29 September 1989
53 **
54 ** References:
55 ** 1) CCITT Recommendation G.711 (very difficult to follow)
56 ** 2) "A New Digital Technique for Implementation of Any
57 ** Continuous PCM Companding Law," Villeret, Michel,
58 ** et al. 1973 IEEE Int. Conf. on Communications, Vol 1,
59 ** 1973, pg. 11.12-11.17
60 ** 3) MIL-STD-188-113,"Interoperability and Performance Standards
61 ** for Analog-to_Digital Conversion Techniques,"
62 ** 17 February 1987
63 **
64 ** Input: Signed 16 bit linear sample
65 ** Output: 8 bit ulaw sample
66 */
67
68 #define ZEROTRAP /* turn on the trap as per the MIL-STD */
69 #define BIAS 0x84 /* define the add-in bias for 16 bit samples */
70 #define CLIP 32635
71
cst_short_to_ulaw(short sample)72 unsigned char cst_short_to_ulaw(short sample)
73 {
74 static const int exp_lut[256] = {0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,
75 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
76 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
77 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
78 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
79 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
80 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
81 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
82 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
83 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
84 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
85 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
86 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
87 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
88 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
89 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7};
90 int sign, exponent, mantissa;
91 unsigned char ulawbyte;
92
93 /* Get the sample into sign-magnitude. */
94 sign = (sample >> 8) & 0x80; /* set aside the sign */
95 if ( sign != 0 ) sample = -sample; /* get magnitude */
96 if ( sample > CLIP ) sample = CLIP; /* clip the magnitude */
97
98 /* Convert from 16 bit linear to ulaw. */
99 sample = sample + BIAS;
100 exponent = exp_lut[( sample >> 7 ) & 0xFF];
101 mantissa = ( sample >> ( exponent + 3 ) ) & 0x0F;
102 ulawbyte = ~ ( sign | ( exponent << 4 ) | mantissa );
103 #ifdef ZEROTRAP
104 if ( ulawbyte == 0 ) ulawbyte = 0x02; /* optional CCITT trap */
105 #endif
106
107 return ulawbyte;
108 }
109
110 /*
111 ** This routine converts from ulaw to 16 bit linear.
112 **
113 ** Craig Reese: IDA/Supercomputing Research Center
114 ** 29 September 1989
115 **
116 ** References:
117 ** 1) CCITT Recommendation G.711 (very difficult to follow)
118 ** 2) MIL-STD-188-113,"Interoperability and Performance Standards
119 ** for Analog-to_Digital Conversion Techniques,"
120 ** 17 February 1987
121 **
122 ** Input: 8 bit ulaw sample
123 ** Output: signed 16 bit linear sample
124 */
125
cst_ulaw_to_short(unsigned char ulawbyte)126 short cst_ulaw_to_short( unsigned char ulawbyte )
127 {
128 static const int exp_lut[8] = { 0, 132, 396, 924, 1980, 4092, 8316, 16764 };
129 int sign, exponent, mantissa;
130 short sample;
131
132 ulawbyte = ~ ulawbyte;
133 sign = ( ulawbyte & 0x80 );
134 exponent = ( ulawbyte >> 4 ) & 0x07;
135 mantissa = ulawbyte & 0x0F;
136 sample = exp_lut[exponent] + ( mantissa << ( exponent + 3 ) );
137 if ( sign != 0 ) sample = -sample;
138
139 return sample;
140 }
141
142
cst_g721_decode(int * actual_size,int size,const unsigned char * packed_residual)143 unsigned char *cst_g721_decode(int *actual_size,int size,
144 const unsigned char *packed_residual
145 )
146 {
147 struct g72x_state state;
148 short sample_short;
149 unsigned char *unpacked_residual;
150 unsigned char code, xcode;
151 int ur=0;
152 int dec_bits;
153
154 *actual_size = size*2;
155 unpacked_residual = cst_alloc(unsigned char, *actual_size);
156 g72x_init_state(&state);
157 dec_bits = 4; /* g721 4-bit encoding */
158
159 for (ur=0; ur <*actual_size; ur++)
160 {
161 xcode = packed_residual[ur/2];
162 if (ur % 2 == 0)
163 code = (xcode & 0xF0) >> dec_bits;
164 else
165 code = (xcode & 0x0F);
166 sample_short = g721_decoder(code,AUDIO_ENCODING_LINEAR,&state);
167 unpacked_residual[ur] = cst_short_to_ulaw(sample_short);
168 }
169
170 return unpacked_residual;
171 }
172
cst_g721_encode(int * packed_size,int actual_size,const unsigned char * unpacked_residual)173 unsigned char *cst_g721_encode(int *packed_size,int actual_size,
174 const unsigned char *unpacked_residual
175 )
176 {
177 struct g72x_state state;
178 unsigned char *packed_residual;
179 unsigned char code, xcode=0;
180 int ur=0;
181 int dec_bits;
182
183 *packed_size = (actual_size+1)/2; /* will round down to even number */
184 packed_residual = cst_alloc(unsigned char, *packed_size);
185
186 g72x_init_state(&state);
187 dec_bits = 4; /* g721 4-bit encoding */
188
189 for (ur=0; ur < actual_size; ur++)
190 {
191 code = g721_encoder((int)cst_ulaw_to_short(unpacked_residual[ur]),
192 AUDIO_ENCODING_LINEAR,&state);
193 if (ur % 2 == 0)
194 {
195 xcode = 0;
196 xcode = code << dec_bits;
197 }
198 else
199 {
200 xcode += code;
201 packed_residual[ur/2] = xcode;
202 }
203 }
204
205 return packed_residual;
206
207 }
208