Asterisk - The Open Source Telephony Project  18.5.0
Data Structures | Macros | Enumerations | Functions
g722.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  g722_decode_state_t
 
struct  g722_encode_state_t
 

Macros

#define INT16_MAX   32767
 
#define INT16_MIN   (-32768)
 

Enumerations

enum  { G722_SAMPLE_RATE_8000 = 0x0001, G722_PACKED = 0x0002 }
 

Functions

int g722_decode (g722_decode_state_t *s, int16_t amp[], const uint8_t g722_data[], int len)
 
g722_decode_state_tg722_decode_init (g722_decode_state_t *s, int rate, int options)
 
int g722_decode_release (g722_decode_state_t *s)
 
int g722_encode (g722_encode_state_t *s, uint8_t g722_data[], const int16_t amp[], int len)
 
g722_encode_state_tg722_encode_init (g722_encode_state_t *s, int rate, int options)
 
int g722_encode_release (g722_encode_state_t *s)
 

Macro Definition Documentation

◆ INT16_MAX

#define INT16_MAX   32767

Definition at line 50 of file g722.h.

Referenced by saturate().

◆ INT16_MIN

#define INT16_MIN   (-32768)

Definition at line 53 of file g722.h.

Referenced by saturate().

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
G722_SAMPLE_RATE_8000 
G722_PACKED 

Definition at line 43 of file g722.h.

44 {
45  G722_SAMPLE_RATE_8000 = 0x0001,
46  G722_PACKED = 0x0002
47 };

Function Documentation

◆ g722_decode()

int g722_decode ( g722_decode_state_t s,
int16_t  amp[],
const uint8_t  g722_data[],
int  len 
)

Definition at line 186 of file g722_decode.c.

References g722_decode_state_t::band, g722_decode_state_t::bits_per_sample, block4(), g722_decode_state_t::det, g722_decode_state_t::eight_k, g722_decode_state_t::in_bits, g722_decode_state_t::in_buffer, g722_decode_state_t::itu_test_mode, len(), g722_decode_state_t::nb, g722_decode_state_t::packed, g722_decode_state_t::s, and g722_decode_state_t::x.

Referenced by g722tolin_framein().

187 {
188  static const int wl[8] = {-60, -30, 58, 172, 334, 538, 1198, 3042 };
189  static const int rl42[16] = {0, 7, 6, 5, 4, 3, 2, 1, 7, 6, 5, 4, 3, 2, 1, 0 };
190  static const int ilb[32] =
191  {
192  2048, 2093, 2139, 2186, 2233, 2282, 2332,
193  2383, 2435, 2489, 2543, 2599, 2656, 2714,
194  2774, 2834, 2896, 2960, 3025, 3091, 3158,
195  3228, 3298, 3371, 3444, 3520, 3597, 3676,
196  3756, 3838, 3922, 4008
197  };
198  static const int wh[3] = {0, -214, 798};
199  static const int rh2[4] = {2, 1, 2, 1};
200  static const int qm2[4] = {-7408, -1616, 7408, 1616};
201  static const int qm4[16] =
202  {
203  0, -20456, -12896, -8968,
204  -6288, -4240, -2584, -1200,
205  20456, 12896, 8968, 6288,
206  4240, 2584, 1200, 0
207  };
208  static const int qm5[32] =
209  {
210  -280, -280, -23352, -17560,
211  -14120, -11664, -9752, -8184,
212  -6864, -5712, -4696, -3784,
213  -2960, -2208, -1520, -880,
214  23352, 17560, 14120, 11664,
215  9752, 8184, 6864, 5712,
216  4696, 3784, 2960, 2208,
217  1520, 880, 280, -280
218  };
219  static const int qm6[64] =
220  {
221  -136, -136, -136, -136,
222  -24808, -21904, -19008, -16704,
223  -14984, -13512, -12280, -11192,
224  -10232, -9360, -8576, -7856,
225  -7192, -6576, -6000, -5456,
226  -4944, -4464, -4008, -3576,
227  -3168, -2776, -2400, -2032,
228  -1688, -1360, -1040, -728,
229  24808, 21904, 19008, 16704,
230  14984, 13512, 12280, 11192,
231  10232, 9360, 8576, 7856,
232  7192, 6576, 6000, 5456,
233  4944, 4464, 4008, 3576,
234  3168, 2776, 2400, 2032,
235  1688, 1360, 1040, 728,
236  432, 136, -432, -136
237  };
238  static const int qmf_coeffs[12] =
239  {
240  3, -11, 12, 32, -210, 951, 3876, -805, 362, -156, 53, -11,
241  };
242 
243  int dlowt;
244  int rlow;
245  int ihigh;
246  int dhigh;
247  int rhigh;
248  int xout1;
249  int xout2;
250  int wd1;
251  int wd2;
252  int wd3;
253  int code;
254  int outlen;
255  int i;
256  int j;
257 
258  outlen = 0;
259  rhigh = 0;
260  for (j = 0; j < len; )
261  {
262  if (s->packed)
263  {
264  /* Unpack the code bits */
265  if (s->in_bits < s->bits_per_sample)
266  {
267  s->in_buffer |= (g722_data[j++] << s->in_bits);
268  s->in_bits += 8;
269  }
270  code = s->in_buffer & ((1 << s->bits_per_sample) - 1);
271  s->in_buffer >>= s->bits_per_sample;
272  s->in_bits -= s->bits_per_sample;
273  }
274  else
275  {
276  code = g722_data[j++];
277  }
278 
279  switch (s->bits_per_sample)
280  {
281  default:
282  case 8:
283  wd1 = code & 0x3F;
284  ihigh = (code >> 6) & 0x03;
285  wd2 = qm6[wd1];
286  wd1 >>= 2;
287  break;
288  case 7:
289  wd1 = code & 0x1F;
290  ihigh = (code >> 5) & 0x03;
291  wd2 = qm5[wd1];
292  wd1 >>= 1;
293  break;
294  case 6:
295  wd1 = code & 0x0F;
296  ihigh = (code >> 4) & 0x03;
297  wd2 = qm4[wd1];
298  break;
299  }
300  /* Block 5L, LOW BAND INVQBL */
301  wd2 = (s->band[0].det*wd2) >> 15;
302  /* Block 5L, RECONS */
303  rlow = s->band[0].s + wd2;
304  /* Block 6L, LIMIT */
305  if (rlow > 16383)
306  rlow = 16383;
307  else if (rlow < -16384)
308  rlow = -16384;
309 
310  /* Block 2L, INVQAL */
311  wd2 = qm4[wd1];
312  dlowt = (s->band[0].det*wd2) >> 15;
313 
314  /* Block 3L, LOGSCL */
315  wd2 = rl42[wd1];
316  wd1 = (s->band[0].nb*127) >> 7;
317  wd1 += wl[wd2];
318  if (wd1 < 0)
319  wd1 = 0;
320  else if (wd1 > 18432)
321  wd1 = 18432;
322  s->band[0].nb = wd1;
323 
324  /* Block 3L, SCALEL */
325  wd1 = (s->band[0].nb >> 6) & 31;
326  wd2 = 8 - (s->band[0].nb >> 11);
327  wd3 = (wd2 < 0) ? (ilb[wd1] << -wd2) : (ilb[wd1] >> wd2);
328  s->band[0].det = wd3 << 2;
329 
330  block4(s, 0, dlowt);
331 
332  if (!s->eight_k)
333  {
334  /* Block 2H, INVQAH */
335  wd2 = qm2[ihigh];
336  dhigh = (s->band[1].det*wd2) >> 15;
337  /* Block 5H, RECONS */
338  rhigh = dhigh + s->band[1].s;
339  /* Block 6H, LIMIT */
340  if (rhigh > 16383)
341  rhigh = 16383;
342  else if (rhigh < -16384)
343  rhigh = -16384;
344 
345  /* Block 2H, INVQAH */
346  wd2 = rh2[ihigh];
347  wd1 = (s->band[1].nb*127) >> 7;
348  wd1 += wh[wd2];
349  if (wd1 < 0)
350  wd1 = 0;
351  else if (wd1 > 22528)
352  wd1 = 22528;
353  s->band[1].nb = wd1;
354 
355  /* Block 3H, SCALEH */
356  wd1 = (s->band[1].nb >> 6) & 31;
357  wd2 = 10 - (s->band[1].nb >> 11);
358  wd3 = (wd2 < 0) ? (ilb[wd1] << -wd2) : (ilb[wd1] >> wd2);
359  s->band[1].det = wd3 << 2;
360 
361  block4(s, 1, dhigh);
362  }
363 
364  if (s->itu_test_mode)
365  {
366  amp[outlen++] = (int16_t) (rlow << 1);
367  amp[outlen++] = (int16_t) (rhigh << 1);
368  }
369  else
370  {
371  if (s->eight_k)
372  {
373  amp[outlen++] = (int16_t) (rlow << 1);
374  }
375  else
376  {
377  /* Apply the receive QMF */
378  for (i = 0; i < 22; i++)
379  s->x[i] = s->x[i + 2];
380  s->x[22] = rlow + rhigh;
381  s->x[23] = rlow - rhigh;
382 
383  xout1 = 0;
384  xout2 = 0;
385  for (i = 0; i < 12; i++)
386  {
387  xout2 += s->x[2*i]*qmf_coeffs[i];
388  xout1 += s->x[2*i + 1]*qmf_coeffs[11 - i];
389  }
390  amp[outlen++] = (int16_t) (xout1 >> 11);
391  amp[outlen++] = (int16_t) (xout2 >> 11);
392  }
393  }
394  }
395  return outlen;
396 }
short int16_t
Definition: db.h:59
int bits_per_sample
Definition: g722.h:104
unsigned int in_buffer
Definition: g722.h:126
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
static void block4(g722_decode_state_t *s, int band, int d)
Definition: g722_decode.c:63
int itu_test_mode
Definition: g722.h:98
struct g722_decode_state_t::@186 band[2]

◆ g722_decode_init()

g722_decode_state_t* g722_decode_init ( g722_decode_state_t s,
int  rate,
int  options 
)

Definition at line 153 of file g722_decode.c.

References g722_decode_state_t::band, g722_decode_state_t::bits_per_sample, g722_decode_state_t::det, g722_decode_state_t::eight_k, FALSE, G722_PACKED, G722_SAMPLE_RATE_8000, malloc(), NULL, g722_decode_state_t::packed, and TRUE.

Referenced by g722tolin16_new(), and g722tolin_new().

154 {
155  if (s == NULL)
156  {
157  if ((s = (g722_decode_state_t *) malloc(sizeof(*s))) == NULL)
158  return NULL;
159  }
160  memset(s, 0, sizeof(*s));
161  if (rate == 48000)
162  s->bits_per_sample = 6;
163  else if (rate == 56000)
164  s->bits_per_sample = 7;
165  else
166  s->bits_per_sample = 8;
168  s->eight_k = TRUE;
169  if ((options & G722_PACKED) && s->bits_per_sample != 8)
170  s->packed = TRUE;
171  else
172  s->packed = FALSE;
173  s->band[0].det = 32;
174  s->band[1].det = 8;
175  return s;
176 }
int bits_per_sample
Definition: g722.h:104
#define TRUE
Definition: g722_decode.c:44
#define NULL
Definition: resample.c:96
#define FALSE
Definition: g722_decode.c:41
char * malloc()
static struct test_options options
struct g722_decode_state_t::@186 band[2]

◆ g722_decode_release()

int g722_decode_release ( g722_decode_state_t s)

Definition at line 179 of file g722_decode.c.

References free().

180 {
181  free(s);
182  return 0;
183 }
void free()

◆ g722_encode()

int g722_encode ( g722_encode_state_t s,
uint8_t  g722_data[],
const int16_t  amp[],
int  len 
)

Definition at line 185 of file g722_encode.c.

References g722_encode_state_t::band, g722_encode_state_t::bits_per_sample, block4(), g722_encode_state_t::det, g722_encode_state_t::eight_k, el, g722_encode_state_t::itu_test_mode, len(), g722_encode_state_t::nb, g722_encode_state_t::out_bits, g722_encode_state_t::out_buffer, g722_encode_state_t::packed, g722_encode_state_t::s, saturate(), and g722_encode_state_t::x.

Referenced by lintog722_framein().

186 {
187  static const int q6[32] =
188  {
189  0, 35, 72, 110, 150, 190, 233, 276,
190  323, 370, 422, 473, 530, 587, 650, 714,
191  786, 858, 940, 1023, 1121, 1219, 1339, 1458,
192  1612, 1765, 1980, 2195, 2557, 2919, 0, 0
193  };
194  static const int iln[32] =
195  {
196  0, 63, 62, 31, 30, 29, 28, 27,
197  26, 25, 24, 23, 22, 21, 20, 19,
198  18, 17, 16, 15, 14, 13, 12, 11,
199  10, 9, 8, 7, 6, 5, 4, 0
200  };
201  static const int ilp[32] =
202  {
203  0, 61, 60, 59, 58, 57, 56, 55,
204  54, 53, 52, 51, 50, 49, 48, 47,
205  46, 45, 44, 43, 42, 41, 40, 39,
206  38, 37, 36, 35, 34, 33, 32, 0
207  };
208  static const int wl[8] =
209  {
210  -60, -30, 58, 172, 334, 538, 1198, 3042
211  };
212  static const int rl42[16] =
213  {
214  0, 7, 6, 5, 4, 3, 2, 1, 7, 6, 5, 4, 3, 2, 1, 0
215  };
216  static const int ilb[32] =
217  {
218  2048, 2093, 2139, 2186, 2233, 2282, 2332,
219  2383, 2435, 2489, 2543, 2599, 2656, 2714,
220  2774, 2834, 2896, 2960, 3025, 3091, 3158,
221  3228, 3298, 3371, 3444, 3520, 3597, 3676,
222  3756, 3838, 3922, 4008
223  };
224  static const int qm4[16] =
225  {
226  0, -20456, -12896, -8968,
227  -6288, -4240, -2584, -1200,
228  20456, 12896, 8968, 6288,
229  4240, 2584, 1200, 0
230  };
231  static const int qm2[4] =
232  {
233  -7408, -1616, 7408, 1616
234  };
235  static const int qmf_coeffs[12] =
236  {
237  3, -11, 12, 32, -210, 951, 3876, -805, 362, -156, 53, -11,
238  };
239  static const int ihn[3] = {0, 1, 0};
240  static const int ihp[3] = {0, 3, 2};
241  static const int wh[3] = {0, -214, 798};
242  static const int rh2[4] = {2, 1, 2, 1};
243 
244  int dlow;
245  int dhigh;
246  int el;
247  int wd;
248  int wd1;
249  int ril;
250  int wd2;
251  int il4;
252  int ih2;
253  int wd3;
254  int eh;
255  int mih;
256  int i;
257  int j;
258  /* Low and high band PCM from the QMF */
259  int xlow;
260  int xhigh;
261  int g722_bytes;
262  /* Even and odd tap accumulators */
263  int sumeven;
264  int sumodd;
265  int ihigh;
266  int ilow;
267  int code;
268 
269  g722_bytes = 0;
270  xhigh = 0;
271  for (j = 0; j < len; )
272  {
273  if (s->itu_test_mode)
274  {
275  xlow =
276  xhigh = amp[j++] >> 1;
277  }
278  else
279  {
280  if (s->eight_k)
281  {
282  xlow = amp[j++] >> 1;
283  }
284  else
285  {
286  /* Apply the transmit QMF */
287  /* Shuffle the buffer down */
288  for (i = 0; i < 22; i++)
289  s->x[i] = s->x[i + 2];
290  s->x[22] = amp[j++];
291  s->x[23] = amp[j++];
292 
293  /* Discard every other QMF output */
294  sumeven = 0;
295  sumodd = 0;
296  for (i = 0; i < 12; i++)
297  {
298  sumodd += s->x[2*i]*qmf_coeffs[i];
299  sumeven += s->x[2*i + 1]*qmf_coeffs[11 - i];
300  }
301  xlow = (sumeven + sumodd) >> 14;
302  xhigh = (sumeven - sumodd) >> 14;
303  }
304  }
305  /* Block 1L, SUBTRA */
306  el = saturate(xlow - s->band[0].s);
307 
308  /* Block 1L, QUANTL */
309  wd = (el >= 0) ? el : -(el + 1);
310 
311  for (i = 1; i < 30; i++)
312  {
313  wd1 = (q6[i]*s->band[0].det) >> 12;
314  if (wd < wd1)
315  break;
316  }
317  ilow = (el < 0) ? iln[i] : ilp[i];
318 
319  /* Block 2L, INVQAL */
320  ril = ilow >> 2;
321  wd2 = qm4[ril];
322  dlow = (s->band[0].det*wd2) >> 15;
323 
324  /* Block 3L, LOGSCL */
325  il4 = rl42[ril];
326  wd = (s->band[0].nb*127) >> 7;
327  s->band[0].nb = wd + wl[il4];
328  if (s->band[0].nb < 0)
329  s->band[0].nb = 0;
330  else if (s->band[0].nb > 18432)
331  s->band[0].nb = 18432;
332 
333  /* Block 3L, SCALEL */
334  wd1 = (s->band[0].nb >> 6) & 31;
335  wd2 = 8 - (s->band[0].nb >> 11);
336  wd3 = (wd2 < 0) ? (ilb[wd1] << -wd2) : (ilb[wd1] >> wd2);
337  s->band[0].det = wd3 << 2;
338 
339  block4(s, 0, dlow);
340 
341  if (s->eight_k)
342  {
343  /* Just leave the high bits as zero */
344  code = (0xC0 | ilow) >> (8 - s->bits_per_sample);
345  }
346  else
347  {
348  /* Block 1H, SUBTRA */
349  eh = saturate(xhigh - s->band[1].s);
350 
351  /* Block 1H, QUANTH */
352  wd = (eh >= 0) ? eh : -(eh + 1);
353  wd1 = (564*s->band[1].det) >> 12;
354  mih = (wd >= wd1) ? 2 : 1;
355  ihigh = (eh < 0) ? ihn[mih] : ihp[mih];
356 
357  /* Block 2H, INVQAH */
358  wd2 = qm2[ihigh];
359  dhigh = (s->band[1].det*wd2) >> 15;
360 
361  /* Block 3H, LOGSCH */
362  ih2 = rh2[ihigh];
363  wd = (s->band[1].nb*127) >> 7;
364  s->band[1].nb = wd + wh[ih2];
365  if (s->band[1].nb < 0)
366  s->band[1].nb = 0;
367  else if (s->band[1].nb > 22528)
368  s->band[1].nb = 22528;
369 
370  /* Block 3H, SCALEH */
371  wd1 = (s->band[1].nb >> 6) & 31;
372  wd2 = 10 - (s->band[1].nb >> 11);
373  wd3 = (wd2 < 0) ? (ilb[wd1] << -wd2) : (ilb[wd1] >> wd2);
374  s->band[1].det = wd3 << 2;
375 
376  block4(s, 1, dhigh);
377  code = ((ihigh << 6) | ilow) >> (8 - s->bits_per_sample);
378  }
379 
380  if (s->packed)
381  {
382  /* Pack the code bits */
383  s->out_buffer |= (code << s->out_bits);
384  s->out_bits += s->bits_per_sample;
385  if (s->out_bits >= 8)
386  {
387  g722_data[g722_bytes++] = (uint8_t) (s->out_buffer & 0xFF);
388  s->out_bits -= 8;
389  s->out_buffer >>= 8;
390  }
391  }
392  else
393  {
394  g722_data[g722_bytes++] = (uint8_t) code;
395  }
396  }
397  return g722_bytes;
398 }
int x[24]
Definition: g722.h:69
int bits_per_sample
Definition: g722.h:66
struct g722_encode_state_t::@185 band[2]
static EditLine * el
Definition: asterisk.c:340
int itu_test_mode
Definition: g722.h:60
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
static __inline__ int16_t saturate(int32_t amp)
Definition: g722_encode.c:49
unsigned int out_buffer
Definition: g722.h:90
static void block4(g722_encode_state_t *s, int band, int d)
Definition: g722_encode.c:63

◆ g722_encode_init()

g722_encode_state_t* g722_encode_init ( g722_encode_state_t s,
int  rate,
int  options 
)

Definition at line 152 of file g722_encode.c.

References g722_encode_state_t::band, g722_encode_state_t::bits_per_sample, g722_encode_state_t::det, g722_encode_state_t::eight_k, FALSE, G722_PACKED, G722_SAMPLE_RATE_8000, malloc(), NULL, g722_encode_state_t::packed, and TRUE.

Referenced by lin16tog722_new(), and lintog722_new().

153 {
154  if (s == NULL)
155  {
156  if ((s = (g722_encode_state_t *) malloc(sizeof(*s))) == NULL)
157  return NULL;
158  }
159  memset(s, 0, sizeof(*s));
160  if (rate == 48000)
161  s->bits_per_sample = 6;
162  else if (rate == 56000)
163  s->bits_per_sample = 7;
164  else
165  s->bits_per_sample = 8;
167  s->eight_k = TRUE;
168  if ((options & G722_PACKED) && s->bits_per_sample != 8)
169  s->packed = TRUE;
170  else
171  s->packed = FALSE;
172  s->band[0].det = 32;
173  s->band[1].det = 8;
174  return s;
175 }
#define FALSE
Definition: g722_encode.c:43
int bits_per_sample
Definition: g722.h:66
struct g722_encode_state_t::@185 band[2]
#define NULL
Definition: resample.c:96
char * malloc()
static struct test_options options
#define TRUE
Definition: g722_encode.c:46

◆ g722_encode_release()

int g722_encode_release ( g722_encode_state_t s)

Definition at line 178 of file g722_encode.c.

References free().

179 {
180  free(s);
181  return 0;
182 }
void free()