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