comparison intercom/g711/g711.c @ 2:13be24d74cd2

import intercom-0.4.1
author Peter Meerwald <pmeerw@cosy.sbg.ac.at>
date Fri, 25 Jun 2010 09:57:52 +0200
parents
children
comparison
equal deleted inserted replaced
1:9cadc470e3da 2:13be24d74cd2
1 /* Version 3.01 - 31.Jan.2000
2 =============================================================================
3
4 U U GGG SSS TTTTT
5 U U G S T
6 U U G GG SSS T
7 U U G G S T
8 UUUU GGG SSS T
9
10 ========================================
11 ITU-T - USER'S GROUP ON SOFTWARE TOOLS
12 ========================================
13
14
15 =============================================================
16 COPYRIGHT NOTE: This source code, and all of its derivations,
17 is subject to the "ITU-T General Public License". Please have
18 it read in the distribution disk, or in the ITU-T
19 Recommendation G.191 on "SOFTWARE TOOLS FOR SPEECH AND AUDIO
20 CODING STANDARDS".
21 =============================================================
22
23
24 MODULE: G711.C, G.711 ENCODING/DECODING FUNCTIONS
25
26 ORIGINAL BY:
27
28 Simao Ferraz de Campos Neto Rudolf Hofmann
29 CPqD/Telebras PHILIPS KOMMUNIKATIONS INDUSTRIE AG
30 DDS/Pr.11 Kommunikationssysteme
31 Rd. Mogi Mirim-Campinas Km.118 Thurn-und-Taxis-Strasse 14
32 13.085 - Campinas - SP (Brazil) D-8500 Nuernberg 10 (Germany)
33
34 Phone : +55-192-39-6396 Phone : +49 911 526-2603
35 FAX : +55-192-53-4754 FAX : +49 911 526-3385
36 EMail : tdsimao@venus.cpqd.ansp.br EMail : HF@PKINBG.UUCP
37
38
39 FUNCTIONS:
40
41 alaw_compress: ... compands 1 vector of linear PCM samples to A-law;
42 uses 13 Most Sig.Bits (MSBs) from input and 8 Least
43 Sig. Bits (LSBs) on output.
44
45 alaw_expand: ..... expands 1 vector of A-law samples to linear PCM;
46 use 8 Least Sig. Bits (LSBs) from input and
47 13 Most Sig.Bits (MSBs) on output.
48
49 ulaw_compress: ... compands 1 vector of linear PCM samples to u-law;
50 uses 14 Most Sig.Bits (MSBs) from input and 8 Least
51 Sig. Bits (LSBs) on output.
52
53 ulaw_expand: ..... expands 1 vector of u-law samples to linear PCM
54 use 8 Least Sig. Bits (LSBs) from input and
55 14 Most Sig.Bits (MSBs) on output.
56
57 PROTOTYPES: in g711.h
58
59 HISTORY:
60 Apr/91 1.0 First version of the G711 module
61 10/Dec/1991 2.0 Break-up in individual functions for A,u law;
62 correction of bug in compression routines (use of 1
63 and 2 complement); Demo program inside module.
64 08/Feb/1992 3.0 Demo as separate file;
65 31/Jan/2000 3.01 Updated documentation text; no change in functions
66 <simao.campos@labs.comsat.com>
67 13jan2005 Byte for compressed data
68 =============================================================================
69 */
70
71 /*
72 * .......... I N C L U D E S ..........
73 */
74
75 /* Global prototype functions */
76 #include "g711.h"
77
78 /*
79 * .......... F U N C T I O N S ..........
80 */
81
82 /* ................... Begin of alaw_compress() ..................... */
83 /*
84 ==========================================================================
85
86 FUNCTION NAME: alaw_compress
87
88 DESCRIPTION: ALaw encoding rule according ITU-T Rec. G.711.
89
90 PROTOTYPE: void alaw_compress(long lseg, short *linbuf, short *logbuf)
91
92 PARAMETERS:
93 lseg: (In) number of samples
94 linbuf: (In) buffer with linear samples (only 12 MSBits are taken
95 into account)
96 logbuf: (Out) buffer with compressed samples (8 bit right justified,
97 without sign extension)
98
99 RETURN VALUE: none.
100
101 HISTORY:
102 10.Dec.91 1.0 Separated A-law compression function
103
104 ==========================================================================
105 */
106 void alaw_compress(long lseg, short *linbuf, Byte *logbuf)
107 {
108 short ix, iexp;
109 long n;
110
111 for (n = 0; n < lseg; n++) {
112 ix = linbuf[n] < 0 /* 0 <= ix < 2048 */
113 ? (~linbuf[n]) >> 4 /* 1's complement for negative values */
114 : (linbuf[n]) >> 4;
115
116 /* Do more, if exponent > 0 */
117 if (ix > 15) { /* exponent=0 for ix <= 15 */
118 iexp = 1; /* first step: */
119 while (ix > 16 + 15) { /* find mantissa and exponent */
120 ix >>= 1;
121 iexp++;
122 }
123 ix -= 16; /* second step: remove leading '1' */
124
125 ix += iexp << 4; /* now compute encoded value */
126 }
127 if (linbuf[n] >= 0)
128 ix |= (0x0080); /* add sign bit */
129
130 logbuf[n] = ix ^ (0x0055); /* toggle even bits */
131 }
132 }
133
134 /* ................... End of alaw_compress() ..................... */
135
136
137 /* ................... Begin of alaw_expand() ..................... */
138 /*
139 ==========================================================================
140
141 FUNCTION NAME: alaw_expand
142
143 DESCRIPTION: ALaw decoding rule according ITU-T Rec. G.711.
144
145 PROTOTYPE: void alaw_expand(long lseg, short *logbuf, short *linbuf)
146
147 PARAMETERS:
148 lseg: (In) number of samples
149 logbuf: (In) buffer with compressed samples (8 bit right justified,
150 without sign extension)
151 linbuf: (Out) buffer with linear samples (13 bits left justified)
152
153 RETURN VALUE: none.
154
155 HISTORY:
156 10.Dec.91 1.0 Separated A-law expansion function
157
158 ============================================================================
159 */
160 void alaw_expand(long lseg, Byte *logbuf, short *linbuf)
161 {
162 short ix, mant, iexp;
163 long n;
164
165 for (n = 0; n < lseg; n++) {
166 ix = logbuf[n] ^ (0x0055); /* re-toggle toggled bits */
167
168 ix &= (0x007F); /* remove sign bit */
169 iexp = ix >> 4; /* extract exponent */
170 mant = ix & (0x000F); /* now get mantissa */
171 if (iexp > 0)
172 mant = mant + 16; /* add leading '1', if exponent > 0 */
173
174 mant = (mant << 4) + (0x0008); /* now mantissa left justified and */
175 /* 1/2 quantization step added */
176 if (iexp > 1) /* now left shift according exponent */
177 mant = mant << (iexp - 1);
178
179 linbuf[n] = logbuf[n] > 127 /* invert, if negative sample */
180 ? mant : -mant;
181 }
182 }
183
184 /* ................... End of alaw_expand() ..................... */
185
186
187 /* ................... Begin of ulaw_compress() ..................... */
188 /*
189 ==========================================================================
190
191 FUNCTION NAME: ulaw_compress
192
193 DESCRIPTION: Mu law encoding rule according ITU-T Rec. G.711.
194
195 PROTOTYPE: void ulaw_compress(long lseg, short *linbuf, short *logbuf)
196
197 PARAMETERS:
198 lseg: (In) number of samples
199 linbuf: (In) buffer with linear samples (only 12 MSBits are taken
200 into account)
201 logbuf: (Out) buffer with compressed samples (8 bit right justified,
202 without sign extension)
203
204 RETURN VALUE: none.
205
206 HISTORY:
207 10.Dec.91 1.0 Separated mu-law compression function
208
209 ==========================================================================
210 */
211 void ulaw_compress(long lseg, short *linbuf, Byte *logbuf)
212 {
213 long n; /* samples's count */
214 short i; /* aux.var. */
215 short absno; /* absolute value of linear (input) sample */
216 short segno; /* segment (Table 2/G711, column 1) */
217 short low_nibble; /* low nibble of log companded sample */
218 short high_nibble; /* high nibble of log companded sample */
219
220 for (n = 0; n < lseg; n++) {
221 /* -------------------------------------------------------------------- */
222 /* Change from 14 bit left justified to 14 bit right justified */
223 /* Compute absolute value; adjust for easy processing */
224 /* -------------------------------------------------------------------- */
225 absno = linbuf[n] < 0 /* compute 1's complement in case of */
226 ? ((~linbuf[n]) >> 2) + 33 /* negative samples */
227 : ((linbuf[n]) >> 2) + 33; /* NB: 33 is the difference value */
228 /* between the thresholds for */
229 /* A-law and u-law. */
230 if (absno > (0x1FFF)) /* limitation to "absno" < 8192 */
231 absno = (0x1FFF);
232
233 /* Determination of sample's segment */
234 i = absno >> 6;
235 segno = 1;
236 while (i != 0) {
237 segno++;
238 i >>= 1;
239 }
240
241 /* Mounting the high-nibble of the log-PCM sample */
242 high_nibble = (0x0008) - segno;
243
244 /* Mounting the low-nibble of the log PCM sample */
245 low_nibble = (absno >> segno) /* right shift of mantissa and */
246 &(0x000F); /* masking away leading '1' */
247 low_nibble = (0x000F) - low_nibble;
248
249 /* Joining the high-nibble and the low-nibble of the log PCM sample */
250 logbuf[n] = (high_nibble << 4) | low_nibble;
251
252 /* Add sign bit */
253 if (linbuf[n] >= 0)
254 logbuf[n] = logbuf[n] | (0x0080);
255 }
256 }
257
258 /* ................... End of ulaw_compress() ..................... */
259
260
261
262 /* ................... Begin of ulaw_expand() ..................... */
263 /*
264 ==========================================================================
265
266 FUNCTION NAME: ulaw_expand
267
268 DESCRIPTION: Mu law decoding rule according ITU-T Rec. G.711.
269
270 PROTOTYPE: void ulaw_expand(long lseg, short *logbuf, short *linbuf)
271
272 PARAMETERS:
273 lseg: (In) number of samples
274 logbuf: (In) buffer with compressed samples (8 bit right justified,
275 without sign extension)
276 linbuf: (Out) buffer with linear samples (14 bits left justified)
277
278 RETURN VALUE: none.
279
280 HISTORY:
281 10.Dec.91 1.0 Separated mu law expansion function
282
283 ============================================================================
284 */
285
286 void ulaw_expand(long lseg, Byte *logbuf, short *linbuf)
287 {
288 long n; /* aux.var. */
289 short segment; /* segment (Table 2/G711, column 1) */
290 short mantissa; /* low nibble of log companded sample */
291 short exponent; /* high nibble of log companded sample */
292 short sign; /* sign of output sample */
293 short step;
294
295 for (n = 0; n < lseg; n++) {
296 sign = logbuf[n] < (0x0080) /* sign-bit = 1 for positiv values */
297 ? -1 : 1;
298 mantissa = ~logbuf[n]; /* 1's complement of input value */
299 exponent = (mantissa >> 4) & (0x0007); /* extract exponent */
300 segment = exponent + 1; /* compute segment number */
301 mantissa = mantissa & (0x000F); /* extract mantissa */
302
303 /* Compute Quantized Sample (14 bit left justified!) */
304 step = (4) << segment; /* position of the LSB */
305 /* = 1 quantization step) */
306 linbuf[n] = sign * /* sign */
307 (((0x0080) << exponent) /* '1', preceding the mantissa */
308 +step * mantissa /* left shift of mantissa */
309 + step / 2 /* 1/2 quantization step */
310 - 4 * 33);
311 }
312 }
313
314 /* ................... End of ulaw_expand() ..................... */

Repositories maintained by Peter Meerwald, pmeerw@pmeerw.net.