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