FreeSWITCH API Documentation  1.7.0
Macros | Functions
g711.h File Reference
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define ULAW_BIAS   0x84 /* Bias for linear code. */
 
#define ALAW_AMI_MASK   0x55
 

Functions

static __inline__ int top_bit (unsigned int bits)
 
static __inline__ int bottom_bit (unsigned int bits)
 
static __inline__ uint8_t linear_to_ulaw (int linear)
 Encode a linear sample to u-law. More...
 
static __inline__ int16_t ulaw_to_linear (uint8_t ulaw)
 Decode an u-law sample to a linear value. More...
 
static __inline__ uint8_t linear_to_alaw (int linear)
 Encode a linear sample to A-law. More...
 
static __inline__ int16_t alaw_to_linear (uint8_t alaw)
 Decode an A-law sample to a linear value. More...
 
uint8_t alaw_to_ulaw (uint8_t alaw)
 Transcode from A-law to u-law, using the procedure defined in G.711. More...
 
uint8_t ulaw_to_alaw (uint8_t ulaw)
 Transcode from u-law to A-law, using the procedure defined in G.711. More...
 

Macro Definition Documentation

#define ALAW_AMI_MASK   0x55

Definition at line 273 of file g711.h.

Referenced by alaw_to_linear(), and linear_to_alaw().

#define ULAW_BIAS   0x84 /* Bias for linear code. */

Definition at line 198 of file g711.h.

Referenced by linear_to_ulaw(), and ulaw_to_linear().

Function Documentation

static __inline__ int16_t alaw_to_linear ( uint8_t  alaw)
static

Decode an A-law sample to a linear value.

Parameters
alawThe A-law sample to decode.
Returns
The linear value.

Definition at line 311 of file g711.h.

References ALAW_AMI_MASK.

Referenced by switch_g711a_decode().

311  {
312  int i;
313  int seg;
314 
315  alaw ^= ALAW_AMI_MASK;
316  i = ((alaw & 0x0F) << 4);
317  seg = (((int) alaw & 0x70) >> 4);
318  if (seg)
319  i = (i + 0x108) << (seg - 1);
320  else
321  i += 8;
322  return (int16_t) ((alaw & 0x80) ? i : -i);
323  }
#define ALAW_AMI_MASK
Definition: g711.h:273
uint8_t alaw_to_ulaw ( uint8_t  alaw)

Transcode from A-law to u-law, using the procedure defined in G.711.

Parameters
alawThe A-law sample to transcode.
Returns
The best matching u-law value.

Definition at line 79 of file g711.c.

References alaw_to_ulaw_table.

80 {
81  return alaw_to_ulaw_table[alaw];
82 }
static const uint8_t alaw_to_ulaw_table[256]
Definition: g711.c:60
static __inline__ int bottom_bit ( unsigned int  bits)
static

Definition at line 129 of file g711.h.

129  {
130  int i;
131 
132  if (bits == 0)
133  return -1;
134  i = 32;
135  if (bits & 0x0000FFFF) {
136  bits &= 0x0000FFFF;
137  i -= 16;
138  }
139  if (bits & 0x00FF00FF) {
140  bits &= 0x00FF00FF;
141  i -= 8;
142  }
143  if (bits & 0x0F0F0F0F) {
144  bits &= 0x0F0F0F0F;
145  i -= 4;
146  }
147  if (bits & 0x33333333) {
148  bits &= 0x33333333;
149  i -= 2;
150  }
151  if (bits & 0x55555555) {
152  bits &= 0x55555555;
153  i -= 1;
154  }
155  return i;
156  }
static __inline__ uint8_t linear_to_alaw ( int  linear)
static

Encode a linear sample to A-law.

Parameters
linearThe sample to encode.
Returns
The A-law value.

Definition at line 279 of file g711.h.

References ALAW_AMI_MASK, and top_bit().

Referenced by switch_g711a_encode().

279  {
280  int mask;
281  int seg;
282 
283  if (linear >= 0) {
284  /* Sign (bit 7) bit = 1 */
285  mask = ALAW_AMI_MASK | 0x80;
286  } else {
287  /* Sign (bit 7) bit = 0 */
288  mask = ALAW_AMI_MASK;
289  linear = -linear - 8;
290  }
291 
292  /* Convert the scaled magnitude to segment number. */
293  seg = top_bit(linear | 0xFF) - 7;
294  if (seg >= 8) {
295  if (linear >= 0) {
296  /* Out of range. Return maximum value. */
297  return (uint8_t) (0x7F ^ mask);
298  }
299  /* We must be just a tiny step below zero */
300  return (uint8_t) (0x00 ^ mask);
301  }
302  /* Combine the sign, segment, and quantization bits. */
303  return (uint8_t) (((seg << 4) | ((linear >> ((seg) ? (seg + 3) : 4)) & 0x0F)) ^ mask);
304  }
#define ALAW_AMI_MASK
Definition: g711.h:273
static __inline__ int top_bit(unsigned int bits)
Definition: g711.h:99
static __inline__ uint8_t linear_to_ulaw ( int  linear)
static

Encode a linear sample to u-law.

Parameters
linearThe sample to encode.
Returns
The u-law value.

Definition at line 204 of file g711.h.

References top_bit(), and ULAW_BIAS.

Referenced by switch_g711u_encode().

204  {
205  uint8_t u_val;
206  int mask;
207  int seg;
208 
209  /* Get the sign and the magnitude of the value. */
210  if (linear < 0) {
211  linear = ULAW_BIAS - linear;
212  mask = 0x7F;
213  } else {
214  linear = ULAW_BIAS + linear;
215  mask = 0xFF;
216  }
217 
218  seg = top_bit(linear | 0xFF) - 7;
219 
220  /*
221  * Combine the sign, segment, quantization bits,
222  * and complement the code word.
223  */
224  if (seg >= 8)
225  u_val = (uint8_t) (0x7F ^ mask);
226  else
227  u_val = (uint8_t) (((seg << 4) | ((linear >> (seg + 3)) & 0xF)) ^ mask);
228 #ifdef ULAW_ZEROTRAP
229  /* Optional ITU trap */
230  if (u_val == 0)
231  u_val = 0x02;
232 #endif
233  return u_val;
234  }
static __inline__ int top_bit(unsigned int bits)
Definition: g711.h:99
#define ULAW_BIAS
Definition: g711.h:198
static __inline__ int top_bit ( unsigned int  bits)
static

Definition at line 99 of file g711.h.

Referenced by linear_to_alaw(), and linear_to_ulaw().

99  {
100  int i;
101 
102  if (bits == 0)
103  return -1;
104  i = 0;
105  if (bits & 0xFFFF0000) {
106  bits &= 0xFFFF0000;
107  i += 16;
108  }
109  if (bits & 0xFF00FF00) {
110  bits &= 0xFF00FF00;
111  i += 8;
112  }
113  if (bits & 0xF0F0F0F0) {
114  bits &= 0xF0F0F0F0;
115  i += 4;
116  }
117  if (bits & 0xCCCCCCCC) {
118  bits &= 0xCCCCCCCC;
119  i += 2;
120  }
121  if (bits & 0xAAAAAAAA) {
122  bits &= 0xAAAAAAAA;
123  i += 1;
124  }
125  return i;
126  }
uint8_t ulaw_to_alaw ( uint8_t  ulaw)

Transcode from u-law to A-law, using the procedure defined in G.711.

Parameters
ulawThe u-law sample to transcode.
Returns
The best matching A-law value.

Definition at line 86 of file g711.c.

References ulaw_to_alaw_table.

87 {
88  return ulaw_to_alaw_table[ulaw];
89 }
static const uint8_t ulaw_to_alaw_table[256]
Definition: g711.c:38
static __inline__ int16_t ulaw_to_linear ( uint8_t  ulaw)
static

Decode an u-law sample to a linear value.

Parameters
ulawThe u-law sample to decode.
Returns
The linear value.

Definition at line 241 of file g711.h.

References ULAW_BIAS.

Referenced by switch_g711u_decode().

241  {
242  int t;
243 
244  /* Complement to obtain normal u-law value. */
245  ulaw = ~ulaw;
246  /*
247  * Extract and bias the quantization bits. Then
248  * shift up by the segment number and subtract out the bias.
249  */
250  t = (((ulaw & 0x0F) << 3) + ULAW_BIAS) << (((int) ulaw & 0x70) >> 4);
251  return (int16_t) ((ulaw & 0x80) ? (ULAW_BIAS - t) : (t - ULAW_BIAS));
252  }
#define ULAW_BIAS
Definition: g711.h:198