blob: cd5e3d798082e35100981c8285bdff4cad4c4d07 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
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 * Despite my general liking of the GPL, I place this code in the
11 * public domain for the benefit of all mankind - even the slimy
12 * ones who might try to proprietize my work and use it to my
13 * detriment.
14 *
15 * $Id: g711.h,v 1.1 2006/06/07 15:46:39 steveu Exp $
16 *
17 * Modifications for WebRtc, 2011/04/28, by tlegrand:
18 * -Changed to use WebRtc types
19 * -Changed __inline__ to __inline
20 * -Two changes to make implementation bitexact with ITU-T reference implementation
21 */
22
23/*! \file */
24
25/*! \page g711_page A-law and mu-law handling
26Lookup tables for A-law and u-law look attractive, until you consider the impact
27on the CPU cache. If it causes a substantial area of your processor cache to get
28hit too often, cache sloshing will severely slow things down. The main reason
29these routines are slow in C, is the lack of direct access to the CPU's "find
30the first 1" instruction. A little in-line assembler fixes that, and the
31conversion routines can be faster than lookup tables, in most real world usage.
32A "find the first 1" instruction is available on most modern CPUs, and is a
33much underused feature.
34
35If an assembly language method of bit searching is not available, these routines
36revert to a method that can be a little slow, so the cache thrashing might not
37seem so bad :(
38
39Feel free to submit patches to add fast "find the first 1" support for your own
40favourite processor.
41
42Look up tables are used for transcoding between A-law and u-law, since it is
43difficult to achieve the precise transcoding procedure laid down in the G.711
44specification by other means.
45*/
46
47#if !defined(_G711_H_)
48#define _G711_H_
49
50#ifdef __cplusplus
51extern "C" {
52#endif
53
54#include "typedefs.h"
55
56#if defined(__i386__)
57/*! \brief Find the bit position of the highest set bit in a word
58 \param bits The word to be searched
59 \return The bit number of the highest set bit, or -1 if the word is zero. */
60static __inline__ int top_bit(unsigned int bits)
61{
62 int res;
63
64 __asm__ __volatile__(" movl $-1,%%edx;\n"
65 " bsrl %%eax,%%edx;\n"
66 : "=d" (res)
67 : "a" (bits));
68 return res;
69}
70/*- End of function --------------------------------------------------------*/
71
72/*! \brief Find the bit position of the lowest set bit in a word
73 \param bits The word to be searched
74 \return The bit number of the lowest set bit, or -1 if the word is zero. */
75static __inline__ int bottom_bit(unsigned int bits)
76{
77 int res;
78
79 __asm__ __volatile__(" movl $-1,%%edx;\n"
80 " bsfl %%eax,%%edx;\n"
81 : "=d" (res)
82 : "a" (bits));
83 return res;
84}
85/*- End of function --------------------------------------------------------*/
86#elif defined(__x86_64__)
87static __inline__ int top_bit(unsigned int bits)
88{
89 int res;
90
91 __asm__ __volatile__(" movq $-1,%%rdx;\n"
92 " bsrq %%rax,%%rdx;\n"
93 : "=d" (res)
94 : "a" (bits));
95 return res;
96}
97/*- End of function --------------------------------------------------------*/
98
99static __inline__ int bottom_bit(unsigned int bits)
100{
101 int res;
102
103 __asm__ __volatile__(" movq $-1,%%rdx;\n"
104 " bsfq %%rax,%%rdx;\n"
105 : "=d" (res)
106 : "a" (bits));
107 return res;
108}
109/*- End of function --------------------------------------------------------*/
110#else
111static __inline int top_bit(unsigned int bits)
112{
113 int i;
114
115 if (bits == 0)
116 return -1;
117 i = 0;
118 if (bits & 0xFFFF0000)
119 {
120 bits &= 0xFFFF0000;
121 i += 16;
122 }
123 if (bits & 0xFF00FF00)
124 {
125 bits &= 0xFF00FF00;
126 i += 8;
127 }
128 if (bits & 0xF0F0F0F0)
129 {
130 bits &= 0xF0F0F0F0;
131 i += 4;
132 }
133 if (bits & 0xCCCCCCCC)
134 {
135 bits &= 0xCCCCCCCC;
136 i += 2;
137 }
138 if (bits & 0xAAAAAAAA)
139 {
140 bits &= 0xAAAAAAAA;
141 i += 1;
142 }
143 return i;
144}
145/*- End of function --------------------------------------------------------*/
146
147static __inline int bottom_bit(unsigned int bits)
148{
149 int i;
150
151 if (bits == 0)
152 return -1;
153 i = 32;
154 if (bits & 0x0000FFFF)
155 {
156 bits &= 0x0000FFFF;
157 i -= 16;
158 }
159 if (bits & 0x00FF00FF)
160 {
161 bits &= 0x00FF00FF;
162 i -= 8;
163 }
164 if (bits & 0x0F0F0F0F)
165 {
166 bits &= 0x0F0F0F0F;
167 i -= 4;
168 }
169 if (bits & 0x33333333)
170 {
171 bits &= 0x33333333;
172 i -= 2;
173 }
174 if (bits & 0x55555555)
175 {
176 bits &= 0x55555555;
177 i -= 1;
178 }
179 return i;
180}
181/*- End of function --------------------------------------------------------*/
182#endif
183
184/* N.B. It is tempting to use look-up tables for A-law and u-law conversion.
185 * However, you should consider the cache footprint.
186 *
187 * A 64K byte table for linear to x-law and a 512 byte table for x-law to
188 * linear sound like peanuts these days, and shouldn't an array lookup be
189 * real fast? No! When the cache sloshes as badly as this one will, a tight
190 * calculation may be better. The messiest part is normally finding the
191 * segment, but a little inline assembly can fix that on an i386, x86_64 and
192 * many other modern processors.
193 */
194
195/*
196 * Mu-law is basically as follows:
197 *
198 * Biased Linear Input Code Compressed Code
199 * ------------------------ ---------------
200 * 00000001wxyza 000wxyz
201 * 0000001wxyzab 001wxyz
202 * 000001wxyzabc 010wxyz
203 * 00001wxyzabcd 011wxyz
204 * 0001wxyzabcde 100wxyz
205 * 001wxyzabcdef 101wxyz
206 * 01wxyzabcdefg 110wxyz
207 * 1wxyzabcdefgh 111wxyz
208 *
209 * Each biased linear code has a leading 1 which identifies the segment
210 * number. The value of the segment number is equal to 7 minus the number
211 * of leading 0's. The quantization interval is directly available as the
212 * four bits wxyz. * The trailing bits (a - h) are ignored.
213 *
214 * Ordinarily the complement of the resulting code word is used for
215 * transmission, and so the code word is complemented before it is returned.
216 *
217 * For further information see John C. Bellamy's Digital Telephony, 1982,
218 * John Wiley & Sons, pps 98-111 and 472-476.
219 */
220
221//#define ULAW_ZEROTRAP /* turn on the trap as per the MIL-STD */
222#define ULAW_BIAS 0x84 /* Bias for linear code. */
223
224/*! \brief Encode a linear sample to u-law
225 \param linear The sample to encode.
226 \return The u-law value.
227*/
228static __inline WebRtc_UWord8 linear_to_ulaw(int linear)
229{
230 WebRtc_UWord8 u_val;
231 int mask;
232 int seg;
233
234 /* Get the sign and the magnitude of the value. */
235 if (linear < 0)
236 {
237 /* WebRtc, tlegrand: -1 added to get bitexact to reference implementation */
238 linear = ULAW_BIAS - linear - 1;
239 mask = 0x7F;
240 }
241 else
242 {
243 linear = ULAW_BIAS + linear;
244 mask = 0xFF;
245 }
246
247 seg = top_bit(linear | 0xFF) - 7;
248
249 /*
250 * Combine the sign, segment, quantization bits,
251 * and complement the code word.
252 */
253 if (seg >= 8)
254 u_val = (WebRtc_UWord8) (0x7F ^ mask);
255 else
256 u_val = (WebRtc_UWord8) (((seg << 4) | ((linear >> (seg + 3)) & 0xF)) ^ mask);
257#ifdef ULAW_ZEROTRAP
258 /* Optional ITU trap */
259 if (u_val == 0)
260 u_val = 0x02;
261#endif
262 return u_val;
263}
264/*- End of function --------------------------------------------------------*/
265
266/*! \brief Decode an u-law sample to a linear value.
267 \param ulaw The u-law sample to decode.
268 \return The linear value.
269*/
270static __inline WebRtc_Word16 ulaw_to_linear(WebRtc_UWord8 ulaw)
271{
272 int t;
273
274 /* Complement to obtain normal u-law value. */
275 ulaw = ~ulaw;
276 /*
277 * Extract and bias the quantization bits. Then
278 * shift up by the segment number and subtract out the bias.
279 */
280 t = (((ulaw & 0x0F) << 3) + ULAW_BIAS) << (((int) ulaw & 0x70) >> 4);
281 return (WebRtc_Word16) ((ulaw & 0x80) ? (ULAW_BIAS - t) : (t - ULAW_BIAS));
282}
283/*- End of function --------------------------------------------------------*/
284
285/*
286 * A-law is basically as follows:
287 *
288 * Linear Input Code Compressed Code
289 * ----------------- ---------------
290 * 0000000wxyza 000wxyz
291 * 0000001wxyza 001wxyz
292 * 000001wxyzab 010wxyz
293 * 00001wxyzabc 011wxyz
294 * 0001wxyzabcd 100wxyz
295 * 001wxyzabcde 101wxyz
296 * 01wxyzabcdef 110wxyz
297 * 1wxyzabcdefg 111wxyz
298 *
299 * For further information see John C. Bellamy's Digital Telephony, 1982,
300 * John Wiley & Sons, pps 98-111 and 472-476.
301 */
302
303#define ALAW_AMI_MASK 0x55
304
305/*! \brief Encode a linear sample to A-law
306 \param linear The sample to encode.
307 \return The A-law value.
308*/
309static __inline WebRtc_UWord8 linear_to_alaw(int linear)
310{
311 int mask;
312 int seg;
313
314 if (linear >= 0)
315 {
316 /* Sign (bit 7) bit = 1 */
317 mask = ALAW_AMI_MASK | 0x80;
318 }
319 else
320 {
321 /* Sign (bit 7) bit = 0 */
322 mask = ALAW_AMI_MASK;
323 /* WebRtc, tlegrand: Changed from -8 to -1 to get bitexact to reference
324 * implementation */
325 linear = -linear - 1;
326 }
327
328 /* Convert the scaled magnitude to segment number. */
329 seg = top_bit(linear | 0xFF) - 7;
330 if (seg >= 8)
331 {
332 if (linear >= 0)
333 {
334 /* Out of range. Return maximum value. */
335 return (WebRtc_UWord8) (0x7F ^ mask);
336 }
337 /* We must be just a tiny step below zero */
338 return (WebRtc_UWord8) (0x00 ^ mask);
339 }
340 /* Combine the sign, segment, and quantization bits. */
341 return (WebRtc_UWord8) (((seg << 4) | ((linear >> ((seg) ? (seg + 3) : 4)) & 0x0F)) ^ mask);
342}
343/*- End of function --------------------------------------------------------*/
344
345/*! \brief Decode an A-law sample to a linear value.
346 \param alaw The A-law sample to decode.
347 \return The linear value.
348*/
349static __inline WebRtc_Word16 alaw_to_linear(WebRtc_UWord8 alaw)
350{
351 int i;
352 int seg;
353
354 alaw ^= ALAW_AMI_MASK;
355 i = ((alaw & 0x0F) << 4);
356 seg = (((int) alaw & 0x70) >> 4);
357 if (seg)
358 i = (i + 0x108) << (seg - 1);
359 else
360 i += 8;
361 return (WebRtc_Word16) ((alaw & 0x80) ? i : -i);
362}
363/*- End of function --------------------------------------------------------*/
364
365/*! \brief Transcode from A-law to u-law, using the procedure defined in G.711.
366 \param alaw The A-law sample to transcode.
367 \return The best matching u-law value.
368*/
369WebRtc_UWord8 alaw_to_ulaw(WebRtc_UWord8 alaw);
370
371/*! \brief Transcode from u-law to A-law, using the procedure defined in G.711.
372 \param alaw The u-law sample to transcode.
373 \return The best matching A-law value.
374*/
375WebRtc_UWord8 ulaw_to_alaw(WebRtc_UWord8 ulaw);
376
377#ifdef __cplusplus
378}
379#endif
380
381#endif
382/*- End of file ------------------------------------------------------------*/