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