spandsp 0.0.6

g711.h
Go to the documentation of this file.
1/*
2 * SpanDSP - a series of DSP components for telephony
3 *
4 * g711.h - In line A-law and u-law conversion routines
5 *
6 * Written by Steve Underwood <steveu@coppice.org>
7 *
8 * Copyright (C) 2001 Steve Underwood
9 *
10 * All rights reserved.
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 2.1,
14 * as published by the Free Software Foundation.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 */
25
26/*! \file */
27
28/*! \page g711_page A-law and mu-law handling
29 Lookup tables for A-law and u-law look attractive, until you consider the impact
30 on the CPU cache. If it causes a substantial area of your processor cache to get
31 hit too often, cache sloshing will severely slow things down. The main reason
32 these routines are slow in C, is the lack of direct access to the CPU's "find
33 the first 1" instruction. A little in-line assembler fixes that, and the
34 conversion routines can be faster than lookup tables, in most real world usage.
35 A "find the first 1" instruction is available on most modern CPUs, and is a
36 much underused feature.
37
38 If an assembly language method of bit searching is not available, these routines
39 revert to a method that can be a little slow, so the cache thrashing might not
40 seem so bad :(
41
42 Feel free to submit patches to add fast "find the first 1" support for your own
43 favourite processor.
44
45 Look up tables are used for transcoding between A-law and u-law, since it is
46 difficult to achieve the precise transcoding procedure laid down in the G.711
47 specification by other means.
48*/
49
50#if !defined(_SPANDSP_G711_H_)
51#define _SPANDSP_G711_H_
52
53/* The usual values to use on idle channels, to emulate silence */
54/*! Idle value for A-law channels */
55#define G711_ALAW_IDLE_OCTET 0x5D
56/*! Idle value for u-law channels */
57#define G711_ULAW_IDLE_OCTET 0xFF
58
59 enum
60{
61 G711_ALAW = 0,
62 G711_ULAW
63};
64
65/*!
66 G.711 state
67 */
68 typedef struct g711_state_s g711_state_t;
69
70#if defined(__cplusplus)
71 extern "C"
72{
73#endif
74
75/* N.B. It is tempting to use look-up tables for A-law and u-law conversion.
76 * However, you should consider the cache footprint.
77 *
78 * A 64K byte table for linear to x-law and a 512 byte table for x-law to
79 * linear sound like peanuts these days, and shouldn't an array lookup be
80 * real fast? No! When the cache sloshes as badly as this one will, a tight
81 * calculation may be better. The messiest part is normally finding the
82 * segment, but a little inline assembly can fix that on an i386, x86_64 and
83 * many other modern processors.
84 */
85
86/*
87 * Mu-law is basically as follows:
88 *
89 * Biased Linear Input Code Compressed Code
90 * ------------------------ ---------------
91 * 00000001wxyza 000wxyz
92 * 0000001wxyzab 001wxyz
93 * 000001wxyzabc 010wxyz
94 * 00001wxyzabcd 011wxyz
95 * 0001wxyzabcde 100wxyz
96 * 001wxyzabcdef 101wxyz
97 * 01wxyzabcdefg 110wxyz
98 * 1wxyzabcdefgh 111wxyz
99 *
100 * Each biased linear code has a leading 1 which identifies the segment
101 * number. The value of the segment number is equal to 7 minus the number
102 * of leading 0's. The quantization interval is directly available as the
103 * four bits wxyz. * The trailing bits (a - h) are ignored.
104 *
105 * Ordinarily the complement of the resulting code word is used for
106 * transmission, and so the code word is complemented before it is returned.
107 *
108 * For further information see John C. Bellamy's Digital Telephony, 1982,
109 * John Wiley & Sons, pps 98-111 and 472-476.
110 */
111
112/* Enable the trap as per the MIL-STD */
113//#define G711_ULAW_ZEROTRAP
114/*! Bias for u-law encoding from linear. */
115#define G711_ULAW_BIAS 0x84
116
117/*! \brief Encode a linear sample to u-law
118 \param linear The sample to encode.
119 \return The u-law value.
120*/
121 static __inline__ uint8_t linear_to_ulaw(int linear)
122{
123 uint8_t u_val;
124 int mask;
125 int seg;
126
127 /* Get the sign and the magnitude of the value. */
128 if (linear >= 0)
129 {
130 linear = G711_ULAW_BIAS + linear;
131 mask = 0xFF;
132 }
133 else
134 {
135 linear = G711_ULAW_BIAS - linear;
136 mask = 0x7F;
137 }
138
139 seg = top_bit(linear | 0xFF) - 7;
140
141 /*
142 * Combine the sign, segment, quantization bits,
143 * and complement the code word.
144 */
145 if (seg >= 8)
146 u_val = (uint8_t) (0x7F ^ mask);
147 else
148 u_val = (uint8_t) (((seg << 4) | ((linear >> (seg + 3)) & 0xF)) ^ mask);
149#if defined(G711_ULAW_ZEROTRAP)
150 /* Optional ITU trap */
151 if (u_val == 0)
152 u_val = 0x02;
153#endif
154 return u_val;
155}
156/*- End of function --------------------------------------------------------*/
157
158/*! \brief Decode an u-law sample to a linear value.
159 \param ulaw The u-law sample to decode.
160 \return The linear value.
161*/
162 static __inline__ int16_t ulaw_to_linear(uint8_t ulaw)
163{
164 int t;
165
166 /* Complement to obtain normal u-law value. */
167 ulaw = ~ulaw;
168 /*
169 * Extract and bias the quantization bits. Then
170 * shift up by the segment number and subtract out the bias.
171 */
172 t = (((ulaw & 0x0F) << 3) + G711_ULAW_BIAS) << (((int) ulaw & 0x70) >> 4);
173 return (int16_t) ((ulaw & 0x80) ? (G711_ULAW_BIAS - t) : (t - G711_ULAW_BIAS));
174}
175/*- End of function --------------------------------------------------------*/
176
177/*
178 * A-law is basically as follows:
179 *
180 * Linear Input Code Compressed Code
181 * ----------------- ---------------
182 * 0000000wxyza 000wxyz
183 * 0000001wxyza 001wxyz
184 * 000001wxyzab 010wxyz
185 * 00001wxyzabc 011wxyz
186 * 0001wxyzabcd 100wxyz
187 * 001wxyzabcde 101wxyz
188 * 01wxyzabcdef 110wxyz
189 * 1wxyzabcdefg 111wxyz
190 *
191 * For further information see John C. Bellamy's Digital Telephony, 1982,
192 * John Wiley & Sons, pps 98-111 and 472-476.
193 */
194
195/*! The A-law alternate mark inversion mask */
196#define G711_ALAW_AMI_MASK 0x55
197
198/*! \brief Encode a linear sample to A-law
199 \param linear The sample to encode.
200 \return The A-law value.
201*/
202 static __inline__ uint8_t linear_to_alaw(int linear)
203{
204 int mask;
205 int seg;
206
207 if (linear >= 0)
208 {
209 /* Sign (bit 7) bit = 1 */
210 mask = G711_ALAW_AMI_MASK | 0x80;
211 }
212 else
213 {
214 /* Sign (bit 7) bit = 0 */
215 mask = G711_ALAW_AMI_MASK;
216 linear = -linear - 1;
217 }
218
219 /* Convert the scaled magnitude to segment number. */
220 seg = top_bit(linear | 0xFF) - 7;
221 if (seg >= 8)
222 {
223 if (linear >= 0)
224 {
225 /* Out of range. Return maximum value. */
226 return (uint8_t) (0x7F ^ mask);
227 }
228 /* We must be just a tiny step below zero */
229 return (uint8_t) (0x00 ^ mask);
230 }
231 /* Combine the sign, segment, and quantization bits. */
232 return (uint8_t) (((seg << 4) | ((linear >> ((seg) ? (seg + 3) : 4)) & 0x0F)) ^ mask);
233}
234/*- End of function --------------------------------------------------------*/
235
236/*! \brief Decode an A-law sample to a linear value.
237 \param alaw The A-law sample to decode.
238 \return The linear value.
239*/
240 static __inline__ int16_t alaw_to_linear(uint8_t alaw)
241{
242 int i;
243 int seg;
244
245 alaw ^= G711_ALAW_AMI_MASK;
246 i = ((alaw & 0x0F) << 4);
247 seg = (((int) alaw & 0x70) >> 4);
248 if (seg)
249 i = (i + 0x108) << (seg - 1);
250 else
251 i += 8;
252 return (int16_t) ((alaw & 0x80) ? i : -i);
253}
254/*- End of function --------------------------------------------------------*/
255
256/*! \brief Transcode from A-law to u-law, using the procedure defined in G.711.
257 \param alaw The A-law sample to transcode.
258 \return The best matching u-law value.
259*/
260SPAN_DECLARE(uint8_t) alaw_to_ulaw(uint8_t alaw);
261
262/*! \brief Transcode from u-law to A-law, using the procedure defined in G.711.
263 \param ulaw The u-law sample to transcode.
264 \return The best matching A-law value.
265*/
266SPAN_DECLARE(uint8_t) ulaw_to_alaw(uint8_t ulaw);
267
268/*! \brief Decode from u-law or A-law to linear.
269 \param s The G.711 context.
270 \param amp The linear audio buffer.
271 \param g711_data The G.711 data.
272 \param g711_bytes The number of G.711 samples to decode.
273 \return The number of samples of linear audio produced.
274*/
275SPAN_DECLARE(int) g711_decode(g711_state_t *s,
276 int16_t amp[],
277 const uint8_t g711_data[],
278 int g711_bytes);
279
280/*! \brief Encode from linear to u-law or A-law.
281 \param s The G.711 context.
282 \param g711_data The G.711 data.
283 \param amp The linear audio buffer.
284 \param len The number of samples to encode.
285 \return The number of G.711 samples produced.
286*/
287SPAN_DECLARE(int) g711_encode(g711_state_t *s,
288 uint8_t g711_data[],
289 const int16_t amp[],
290 int len);
291
292/*! \brief Transcode between u-law and A-law.
293 \param s The G.711 context.
294 \param g711_out The resulting G.711 data.
295 \param g711_in The original G.711 data.
296 \param g711_bytes The number of G.711 samples to transcode.
297 \return The number of G.711 samples produced.
298*/
299SPAN_DECLARE(int) g711_transcode(g711_state_t *s,
300 uint8_t g711_out[],
301 const uint8_t g711_in[],
302 int g711_bytes);
303
304/*! Initialise a G.711 encode or decode context.
305 \param s The G.711 context.
306 \param mode The G.711 mode.
307 \return A pointer to the G.711 context, or NULL for error. */
308SPAN_DECLARE(g711_state_t *) g711_init(g711_state_t *s, int mode);
309
310/*! Release a G.711 encode or decode context.
311 \param s The G.711 context.
312 \return 0 for OK. */
313SPAN_DECLARE(int) g711_release(g711_state_t *s);
314
315/*! Free a G.711 encode or decode context.
316 \param s The G.711 context.
317 \return 0 for OK. */
318SPAN_DECLARE(int) g711_free(g711_state_t *s);
319
320#if defined(__cplusplus)
321}
322#endif
323
324#endif
325/*- End of file ------------------------------------------------------------*/
G711_ALAW_AMI_MASK
#define G711_ALAW_AMI_MASK
Definition g711.h:196
alaw_to_ulaw
uint8_t alaw_to_ulaw(uint8_t alaw)
Transcode from A-law to u-law, using the procedure defined in G.711.
Definition g711.c:86
G711_ULAW_BIAS
#define G711_ULAW_BIAS
Definition g711.h:115
ulaw_to_alaw
uint8_t ulaw_to_alaw(uint8_t ulaw)
Transcode from u-law to A-law, using the procedure defined in G.711.
Definition g711.c:92
g711_state_t
struct g711_state_s g711_state_t
Definition g711.h:68
g711_transcode
int g711_transcode(g711_state_t *s, uint8_t g711_out[], const uint8_t g711_in[], int g711_bytes)
Transcode between u-law and A-law.
Definition g711.c:146
g711_init
g711_state_t * g711_init(g711_state_t *s, int mode)
Definition g711.c:170
g711_encode
int g711_encode(g711_state_t *s, uint8_t g711_data[], const int16_t amp[], int len)
Encode from linear to u-law or A-law.
Definition g711.c:122
g711_decode
int g711_decode(g711_state_t *s, int16_t amp[], const uint8_t g711_data[], int g711_bytes)
Decode from u-law or A-law to linear.
Definition g711.c:98
g711_release
int g711_release(g711_state_t *s)
Definition g711.c:182
g711_free
int g711_free(g711_state_t *s)
Definition g711.c:188
g711_state_s
Definition private/g711.h:33
g711_state_s::mode
int mode
Definition private/g711.h:35