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