Asterisk - The Open Source Telephony Project  18.5.0
codec_g726.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2006, Digium, Inc.
5  *
6  * Mark Spencer <[email protected]>
7  * Kevin P. Fleming <[email protected]>
8  *
9  * Based on frompcm.c and topcm.c from the Emiliano MIPL browser/
10  * interpreter. See http://www.bsdtelephony.com.mx
11  *
12  * See http://www.asterisk.org for more information about
13  * the Asterisk project. Please do not directly contact
14  * any of the maintainers of this project for assistance;
15  * the project provides a web site, mailing lists and IRC
16  * channels for your use.
17  *
18  * This program is free software, distributed under the terms of
19  * the GNU General Public License Version 2. See the LICENSE file
20  * at the top of the source tree.
21  */
22 
23 /*! \file
24  *
25  * \brief codec_g726.c - translate between signed linear and ITU G.726-32kbps (both RFC3551 and AAL2 codeword packing)
26  *
27  * \ingroup codecs
28  */
29 
30 /*** MODULEINFO
31  <support_level>core</support_level>
32  ***/
33 
34 #include "asterisk.h"
35 
36 #include "asterisk/lock.h"
37 #include "asterisk/linkedlists.h"
38 #include "asterisk/module.h"
39 #include "asterisk/config.h"
40 #include "asterisk/translate.h"
41 #include "asterisk/utils.h"
42 
43 #define WANT_ASM
44 #include "log2comp.h"
45 
46 /* define NOT_BLI to use a faster but not bit-level identical version */
47 /* #define NOT_BLI */
48 
49 #if defined(NOT_BLI)
50 # if defined(_MSC_VER)
51 typedef __int64 sint64;
52 # elif defined(__GNUC__)
53 typedef long long sint64;
54 # else
55 # error 64-bit integer type is not defined for your compiler/platform
56 # endif
57 #endif
58 
59 #define BUFFER_SAMPLES 8096 /* size for the translation buffers */
60 #define BUF_SHIFT 5
61 
62 /* Sample frame data */
63 #include "asterisk/slin.h"
64 #include "ex_g726.h"
65 
66 /*
67  * The following is the definition of the state structure
68  * used by the G.726 encoder and decoder to preserve their internal
69  * state between successive calls. The meanings of the majority
70  * of the state structure fields are explained in detail in the
71  * CCITT Recommendation G.721. The field names are essentially identical
72  * to variable names in the bit level description of the coding algorithm
73  * included in this Recommendation.
74  */
75 struct g726_state {
76  long yl; /* Locked or steady state step size multiplier. */
77  int yu; /* Unlocked or non-steady state step size multiplier. */
78  int dms; /* Short term energy estimate. */
79  int dml; /* Long term energy estimate. */
80  int ap; /* Linear weighting coefficient of 'yl' and 'yu'. */
81  int a[2]; /* Coefficients of pole portion of prediction filter.
82  * stored as fixed-point 1==2^14 */
83  int b[6]; /* Coefficients of zero portion of prediction filter.
84  * stored as fixed-point 1==2^14 */
85  int pk[2]; /* Signs of previous two samples of a partially
86  * reconstructed signal. */
87  int dq[6]; /* Previous 6 samples of the quantized difference signal
88  * stored as fixed point 1==2^12,
89  * or in internal floating point format */
90  int sr[2]; /* Previous 2 samples of the quantized difference signal
91  * stored as fixed point 1==2^12,
92  * or in internal floating point format */
93  int td; /* delayed tone detect, new in 1988 version */
94 };
95 
96 static int qtab_721[7] = {-124, 80, 178, 246, 300, 349, 400};
97 /*
98  * Maps G.721 code word to reconstructed scale factor normalized log
99  * magnitude values.
100  */
101 static int _dqlntab[16] = {-2048, 4, 135, 213, 273, 323, 373, 425,
102  425, 373, 323, 273, 213, 135, 4, -2048};
103 
104 /* Maps G.721 code word to log of scale factor multiplier. */
105 static int _witab[16] = {-12, 18, 41, 64, 112, 198, 355, 1122,
106  1122, 355, 198, 112, 64, 41, 18, -12};
107 /*
108  * Maps G.721 code words to a set of values whose long and short
109  * term averages are computed and then compared to give an indication
110  * how stationary (steady state) the signal is.
111  */
112 static int _fitab[16] = {0, 0, 0, 0x200, 0x200, 0x200, 0x600, 0xE00,
113  0xE00, 0x600, 0x200, 0x200, 0x200, 0, 0, 0};
114 
115 
116 /*
117  * g72x_init_state()
118  *
119  * This routine initializes and/or resets the g726_state structure
120  * pointed to by 'state_ptr'.
121  * All the initial state values are specified in the CCITT G.721 document.
122  */
123 static void g726_init_state(struct g726_state *state_ptr)
124 {
125  int cnta;
126 
127  state_ptr->yl = 34816;
128  state_ptr->yu = 544;
129  state_ptr->dms = 0;
130  state_ptr->dml = 0;
131  state_ptr->ap = 0;
132  for (cnta = 0; cnta < 2; cnta++) {
133  state_ptr->a[cnta] = 0;
134  state_ptr->pk[cnta] = 0;
135 #ifdef NOT_BLI
136  state_ptr->sr[cnta] = 1;
137 #else
138  state_ptr->sr[cnta] = 32;
139 #endif
140  }
141  for (cnta = 0; cnta < 6; cnta++) {
142  state_ptr->b[cnta] = 0;
143 #ifdef NOT_BLI
144  state_ptr->dq[cnta] = 1;
145 #else
146  state_ptr->dq[cnta] = 32;
147 #endif
148  }
149  state_ptr->td = 0;
150 }
151 
152 /*
153  * quan()
154  *
155  * quantizes the input val against the table of integers.
156  * It returns i if table[i - 1] <= val < table[i].
157  *
158  * Using linear search for simple coding.
159  */
160 static int quan(int val, int *table, int size)
161 {
162  int i;
163 
164  for (i = 0; i < size && val >= *table; ++i, ++table)
165  ;
166  return i;
167 }
168 
169 #ifdef NOT_BLI /* faster non-identical version */
170 
171 /*
172  * predictor_zero()
173  *
174  * computes the estimated signal from 6-zero predictor.
175  *
176  */
177 static int predictor_zero(struct g726_state *state_ptr)
178 { /* divide by 2 is necessary here to handle negative numbers correctly */
179  int i;
180  sint64 sezi;
181  for (sezi = 0, i = 0; i < 6; i++) /* ACCUM */
182  sezi += (sint64)state_ptr->b[i] * state_ptr->dq[i];
183  return (int)(sezi >> 13) / 2 /* 2^14 */;
184 }
185 
186 /*
187  * predictor_pole()
188  *
189  * computes the estimated signal from 2-pole predictor.
190  *
191  */
192 static int predictor_pole(struct g726_state *state_ptr)
193 { /* divide by 2 is necessary here to handle negative numbers correctly */
194  return (int)(((sint64)state_ptr->a[1] * state_ptr->sr[1] +
195  (sint64)state_ptr->a[0] * state_ptr->sr[0]) >> 13) / 2 /* 2^14 */;
196 }
197 
198 #else /* NOT_BLI - identical version */
199 /*
200  * fmult()
201  *
202  * returns the integer product of the fixed-point number "an" (1==2^12) and
203  * "floating point" representation (4-bit exponent, 6-bit mantessa) "srn".
204  */
205 static int fmult(int an, int srn)
206 {
207  int anmag, anexp, anmant;
208  int wanexp, wanmant;
209  int retval;
210 
211  anmag = (an > 0) ? an : ((-an) & 0x1FFF);
212  anexp = ilog2(anmag) - 5;
213  anmant = (anmag == 0) ? 32 :
214  (anexp >= 0) ? anmag >> anexp : anmag << -anexp;
215  wanexp = anexp + ((srn >> 6) & 0xF) - 13;
216 
217  wanmant = (anmant * (srn & 077) + 0x30) >> 4;
218  retval = (wanexp >= 0) ? ((wanmant << wanexp) & 0x7FFF) :
219  (wanmant >> -wanexp);
220 
221  return (((an ^ srn) < 0) ? -retval : retval);
222 }
223 
224 static int predictor_zero(struct g726_state *state_ptr)
225 {
226  int i;
227  int sezi;
228  for (sezi = 0, i = 0; i < 6; i++) /* ACCUM */
229  sezi += fmult(state_ptr->b[i] >> 2, state_ptr->dq[i]);
230  return sezi;
231 }
232 
233 static int predictor_pole(struct g726_state *state_ptr)
234 {
235  return (fmult(state_ptr->a[1] >> 2, state_ptr->sr[1]) +
236  fmult(state_ptr->a[0] >> 2, state_ptr->sr[0]));
237 }
238 
239 #endif /* NOT_BLI */
240 
241 /*
242  * step_size()
243  *
244  * computes the quantization step size of the adaptive quantizer.
245  *
246  */
247 static int step_size(struct g726_state *state_ptr)
248 {
249  int y, dif, al;
250 
251  if (state_ptr->ap >= 256) {
252  return state_ptr->yu;
253  }
254 
255  y = state_ptr->yl >> 6;
256  dif = state_ptr->yu - y;
257  al = state_ptr->ap >> 2;
258 
259  if (dif > 0) {
260  y += (dif * al) >> 6;
261  } else if (dif < 0) {
262  y += (dif * al + 0x3F) >> 6;
263  }
264  return y;
265 }
266 
267 /*
268  * quantize()
269  *
270  * Given a raw sample, 'd', of the difference signal and a
271  * quantization step size scale factor, 'y', this routine returns the
272  * ADPCM codeword to which that sample gets quantized. The step
273  * size scale factor division operation is done in the log base 2 domain
274  * as a subtraction.
275  */
276 static int quantize(
277  int d, /* Raw difference signal sample */
278  int y, /* Step size multiplier */
279  int *table, /* quantization table */
280  int size) /* table size of integers */
281 {
282  int dqm; /* Magnitude of 'd' */
283  int exp; /* Integer part of base 2 log of 'd' */
284  int mant; /* Fractional part of base 2 log */
285  int dl; /* Log of magnitude of 'd' */
286  int dln; /* Step size scale factor normalized log */
287  int i;
288 
289  /*
290  * LOG
291  *
292  * Compute base 2 log of 'd', and store in 'dl'.
293  */
294  dqm = abs(d);
295  exp = ilog2(dqm);
296  if (exp < 0) {
297  exp = 0;
298  }
299  mant = ((dqm << 7) >> exp) & 0x7F; /* Fractional portion. */
300  dl = (exp << 7) | mant;
301 
302  /*
303  * SUBTB
304  *
305  * "Divide" by step size multiplier.
306  */
307  dln = dl - (y >> 2);
308 
309  /*
310  * QUAN
311  *
312  * Obtain codword i for 'd'.
313  */
314  i = quan(dln, table, size);
315  if (d < 0) { /* take 1's complement of i */
316  return ((size << 1) + 1 - i);
317  } else if (i == 0) { /* take 1's complement of 0 */
318  return ((size << 1) + 1); /* new in 1988 */
319  } else {
320  return i;
321  }
322 }
323 
324 /*
325  * reconstruct()
326  *
327  * Returns reconstructed difference signal 'dq' obtained from
328  * codeword 'i' and quantization step size scale factor 'y'.
329  * Multiplication is performed in log base 2 domain as addition.
330  */
331 static int reconstruct(
332  int sign, /* 0 for non-negative value */
333  int dqln, /* G.72x codeword */
334  int y) /* Step size multiplier */
335 {
336  int dql; /* Log of 'dq' magnitude */
337  int dex; /* Integer part of log */
338  int dqt;
339  int dq; /* Reconstructed difference signal sample */
340 
341  dql = dqln + (y >> 2); /* ADDA */
342 
343  if (dql < 0) {
344 #ifdef NOT_BLI
345  return (sign) ? -1 : 1;
346 #else
347  return (sign) ? -0x8000 : 0;
348 #endif
349  } else { /* ANTILOG */
350  dex = (dql >> 7) & 15;
351  dqt = 128 + (dql & 127);
352 #ifdef NOT_BLI
353  dq = ((dqt << 19) >> (14 - dex));
354  return (sign) ? -dq : dq;
355 #else
356  dq = (dqt << 7) >> (14 - dex);
357  return (sign) ? (dq - 0x8000) : dq;
358 #endif
359  }
360 }
361 
362 /*
363  * update()
364  *
365  * updates the state variables for each output code
366  */
367 static void update(
368  int code_size, /* distinguish 723_40 with others */
369  int y, /* quantizer step size */
370  int wi, /* scale factor multiplier */
371  int fi, /* for long/short term energies */
372  int dq, /* quantized prediction difference */
373  int sr, /* reconstructed signal */
374  int dqsez, /* difference from 2-pole predictor */
375  struct g726_state *state_ptr) /* coder state pointer */
376 {
377  int cnt;
378  int mag; /* Adaptive predictor, FLOAT A */
379 #ifndef NOT_BLI
380  int exp;
381 #endif
382  int a2p=0; /* LIMC */
383  int a1ul; /* UPA1 */
384  int pks1; /* UPA2 */
385  int fa1;
386  int tr; /* tone/transition detector */
387  int ylint, thr2, dqthr;
388  int ylfrac, thr1;
389  int pk0;
390 
391  pk0 = (dqsez < 0) ? 1 : 0; /* needed in updating predictor poles */
392 
393 #ifdef NOT_BLI
394  mag = abs(dq / 0x1000); /* prediction difference magnitude */
395 #else
396  mag = dq & 0x7FFF; /* prediction difference magnitude */
397 #endif
398  /* TRANS */
399  ylint = state_ptr->yl >> 15; /* exponent part of yl */
400  ylfrac = (state_ptr->yl >> 10) & 0x1F; /* fractional part of yl */
401  thr1 = (32 + ylfrac) << ylint; /* threshold */
402  thr2 = (ylint > 9) ? 31 << 10 : thr1; /* limit thr2 to 31 << 10 */
403  dqthr = (thr2 + (thr2 >> 1)) >> 1; /* dqthr = 0.75 * thr2 */
404  if (state_ptr->td == 0) { /* signal supposed voice */
405  tr = 0;
406  } else if (mag <= dqthr) { /* supposed data, but small mag */
407  tr = 0; /* treated as voice */
408  } else { /* signal is data (modem) */
409  tr = 1;
410  }
411  /*
412  * Quantizer scale factor adaptation.
413  */
414 
415  /* FUNCTW & FILTD & DELAY */
416  /* update non-steady state step size multiplier */
417  state_ptr->yu = y + ((wi - y) >> 5);
418 
419  /* LIMB */
420  if (state_ptr->yu < 544) { /* 544 <= yu <= 5120 */
421  state_ptr->yu = 544;
422  } else if (state_ptr->yu > 5120) {
423  state_ptr->yu = 5120;
424  }
425 
426  /* FILTE & DELAY */
427  /* update steady state step size multiplier */
428  state_ptr->yl += state_ptr->yu + ((-state_ptr->yl) >> 6);
429 
430  /*
431  * Adaptive predictor coefficients.
432  */
433  if (tr == 1) { /* reset a's and b's for modem signal */
434  state_ptr->a[0] = 0;
435  state_ptr->a[1] = 0;
436  state_ptr->b[0] = 0;
437  state_ptr->b[1] = 0;
438  state_ptr->b[2] = 0;
439  state_ptr->b[3] = 0;
440  state_ptr->b[4] = 0;
441  state_ptr->b[5] = 0;
442  } else { /* update a's and b's */
443  pks1 = pk0 ^ state_ptr->pk[0]; /* UPA2 */
444 
445  /* update predictor pole a[1] */
446  a2p = state_ptr->a[1] - (state_ptr->a[1] >> 7);
447  if (dqsez != 0) {
448  fa1 = (pks1) ? state_ptr->a[0] : -state_ptr->a[0];
449  if (fa1 < -8191) { /* a2p = function of fa1 */
450  a2p -= 0x100;
451  } else if (fa1 > 8191) {
452  a2p += 0xFF;
453  } else {
454  a2p += fa1 >> 5;
455  }
456 
457  if (pk0 ^ state_ptr->pk[1]) {
458  /* LIMC */
459  if (a2p <= -12160) {
460  a2p = -12288;
461  } else if (a2p >= 12416) {
462  a2p = 12288;
463  } else {
464  a2p -= 0x80;
465  }
466  } else if (a2p <= -12416) {
467  a2p = -12288;
468  } else if (a2p >= 12160) {
469  a2p = 12288;
470  } else {
471  a2p += 0x80;
472  }
473  }
474 
475  /* TRIGB & DELAY */
476  state_ptr->a[1] = a2p;
477 
478  /* UPA1 */
479  /* update predictor pole a[0] */
480  state_ptr->a[0] -= state_ptr->a[0] >> 8;
481  if (dqsez != 0) {
482  if (pks1 == 0)
483  state_ptr->a[0] += 192;
484  else
485  state_ptr->a[0] -= 192;
486  }
487  /* LIMD */
488  a1ul = 15360 - a2p;
489  if (state_ptr->a[0] < -a1ul) {
490  state_ptr->a[0] = -a1ul;
491  } else if (state_ptr->a[0] > a1ul) {
492  state_ptr->a[0] = a1ul;
493  }
494 
495  /* UPB : update predictor zeros b[6] */
496  for (cnt = 0; cnt < 6; cnt++) {
497  if (code_size == 5) { /* for 40Kbps G.723 */
498  state_ptr->b[cnt] -= state_ptr->b[cnt] >> 9;
499  } else { /* for G.721 and 24Kbps G.723 */
500  state_ptr->b[cnt] -= state_ptr->b[cnt] >> 8;
501  }
502  if (mag) { /* XOR */
503  if ((dq ^ state_ptr->dq[cnt]) >= 0) {
504  state_ptr->b[cnt] += 128;
505  } else {
506  state_ptr->b[cnt] -= 128;
507  }
508  }
509  }
510  }
511 
512  for (cnt = 5; cnt > 0; cnt--)
513  state_ptr->dq[cnt] = state_ptr->dq[cnt-1];
514 #ifdef NOT_BLI
515  state_ptr->dq[0] = dq;
516 #else
517  /* FLOAT A : convert dq[0] to 4-bit exp, 6-bit mantissa f.p. */
518  if (mag == 0) {
519  state_ptr->dq[0] = (dq >= 0) ? 0x20 : 0x20 - 0x400;
520  } else {
521  exp = ilog2(mag) + 1;
522  state_ptr->dq[0] = (dq >= 0) ?
523  (exp << 6) + ((mag << 6) >> exp) :
524  (exp << 6) + ((mag << 6) >> exp) - 0x400;
525  }
526 #endif
527 
528  state_ptr->sr[1] = state_ptr->sr[0];
529 #ifdef NOT_BLI
530  state_ptr->sr[0] = sr;
531 #else
532  /* FLOAT B : convert sr to 4-bit exp., 6-bit mantissa f.p. */
533  if (sr == 0) {
534  state_ptr->sr[0] = 0x20;
535  } else if (sr > 0) {
536  exp = ilog2(sr) + 1;
537  state_ptr->sr[0] = (exp << 6) + ((sr << 6) >> exp);
538  } else if (sr > -0x8000) {
539  mag = -sr;
540  exp = ilog2(mag) + 1;
541  state_ptr->sr[0] = (exp << 6) + ((mag << 6) >> exp) - 0x400;
542  } else
543  state_ptr->sr[0] = 0x20 - 0x400;
544 #endif
545 
546  /* DELAY A */
547  state_ptr->pk[1] = state_ptr->pk[0];
548  state_ptr->pk[0] = pk0;
549 
550  /* TONE */
551  if (tr == 1) { /* this sample has been treated as data */
552  state_ptr->td = 0; /* next one will be treated as voice */
553  } else if (a2p < -11776) { /* small sample-to-sample correlation */
554  state_ptr->td = 1; /* signal may be data */
555  } else { /* signal is voice */
556  state_ptr->td = 0;
557  }
558 
559  /*
560  * Adaptation speed control.
561  */
562  state_ptr->dms += (fi - state_ptr->dms) >> 5; /* FILTA */
563  state_ptr->dml += (((fi << 2) - state_ptr->dml) >> 7); /* FILTB */
564 
565  if (tr == 1) {
566  state_ptr->ap = 256;
567  } else if (y < 1536) { /* SUBTC */
568  state_ptr->ap += (0x200 - state_ptr->ap) >> 4;
569  } else if (state_ptr->td == 1) {
570  state_ptr->ap += (0x200 - state_ptr->ap) >> 4;
571  } else if (abs((state_ptr->dms << 2) - state_ptr->dml) >=
572  (state_ptr->dml >> 3)) {
573  state_ptr->ap += (0x200 - state_ptr->ap) >> 4;
574  } else {
575  state_ptr->ap += (-state_ptr->ap) >> 4;
576  }
577 }
578 
579 /*
580  * g726_decode()
581  *
582  * Description:
583  *
584  * Decodes a 4-bit code of G.726-32 encoded data of i and
585  * returns the resulting linear PCM, A-law or u-law value.
586  * return -1 for unknown out_coding value.
587  */
588 static int g726_decode(int i, struct g726_state *state_ptr)
589 {
590  int sezi, sez, se; /* ACCUM */
591  int y; /* MIX */
592  int sr; /* ADDB */
593  int dq;
594  int dqsez;
595 
596  i &= 0x0f; /* mask to get proper bits */
597 #ifdef NOT_BLI
598  sezi = predictor_zero(state_ptr);
599  sez = sezi;
600  se = sezi + predictor_pole(state_ptr); /* estimated signal */
601 #else
602  sezi = predictor_zero(state_ptr);
603  sez = sezi >> 1;
604  se = (sezi + predictor_pole(state_ptr)) >> 1; /* estimated signal */
605 #endif
606 
607  y = step_size(state_ptr); /* dynamic quantizer step size */
608 
609  dq = reconstruct(i & 8, _dqlntab[i], y); /* quantized diff. */
610 
611 #ifdef NOT_BLI
612  sr = se + dq; /* reconst. signal */
613  dqsez = dq + sez; /* pole prediction diff. */
614 #else
615  sr = (dq < 0) ? se - (dq & 0x3FFF) : se + dq; /* reconst. signal */
616  dqsez = sr - se + sez; /* pole prediction diff. */
617 #endif
618 
619  update(4, y, _witab[i] << 5, _fitab[i], dq, sr, dqsez, state_ptr);
620 
621 #ifdef NOT_BLI
622  return (sr >> 10); /* sr was 26-bit dynamic range */
623 #else
624  return (sr << 2); /* sr was 14-bit dynamic range */
625 #endif
626 }
627 
628 /*
629  * g726_encode()
630  *
631  * Encodes the input vale of linear PCM, A-law or u-law data sl and returns
632  * the resulting code. -1 is returned for unknown input coding value.
633  */
634 static int g726_encode(int sl, struct g726_state *state_ptr)
635 {
636  int sezi, se, sez; /* ACCUM */
637  int d; /* SUBTA */
638  int sr; /* ADDB */
639  int y; /* MIX */
640  int dqsez; /* ADDC */
641  int dq, i;
642 
643 #ifdef NOT_BLI
644  sl <<= 10; /* 26-bit dynamic range */
645 
646  sezi = predictor_zero(state_ptr);
647  sez = sezi;
648  se = sezi + predictor_pole(state_ptr); /* estimated signal */
649 #else
650  sl >>= 2; /* 14-bit dynamic range */
651 
652  sezi = predictor_zero(state_ptr);
653  sez = sezi >> 1;
654  se = (sezi + predictor_pole(state_ptr)) >> 1; /* estimated signal */
655 #endif
656 
657  d = sl - se; /* estimation difference */
658 
659  /* quantize the prediction difference */
660  y = step_size(state_ptr); /* quantizer step size */
661 #ifdef NOT_BLI
662  d /= 0x1000;
663 #endif
664  i = quantize(d, y, qtab_721, 7); /* i = G726 code */
665 
666  dq = reconstruct(i & 8, _dqlntab[i], y); /* quantized est diff */
667 
668 #ifdef NOT_BLI
669  sr = se + dq; /* reconst. signal */
670  dqsez = dq + sez; /* pole prediction diff. */
671 #else
672  sr = (dq < 0) ? se - (dq & 0x3FFF) : se + dq; /* reconst. signal */
673  dqsez = sr - se + sez; /* pole prediction diff. */
674 #endif
675 
676  update(4, y, _witab[i] << 5, _fitab[i], dq, sr, dqsez, state_ptr);
677 
678  return i;
679 }
680 
681 /*
682  * Private workspace for translating signed linear signals to G726.
683  * Don't bother to define two distinct structs.
684  */
685 
687  /* buffer any odd byte in input - 0x80 + (value & 0xf) if present */
688  unsigned char next_flag;
689  struct g726_state g726;
690 };
691 
692 /*! \brief init a new instance of g726_coder_pvt. */
693 static int lintog726_new(struct ast_trans_pvt *pvt)
694 {
695  struct g726_coder_pvt *tmp = pvt->pvt;
696 
697  g726_init_state(&tmp->g726);
698 
699  return 0;
700 }
701 
702 /*! \brief decode packed 4-bit G726 values (AAL2 packing) and store in buffer. */
703 static int g726aal2tolin_framein (struct ast_trans_pvt *pvt, struct ast_frame *f)
704 {
705  struct g726_coder_pvt *tmp = pvt->pvt;
706  unsigned char *src = f->data.ptr;
707  int16_t *dst = pvt->outbuf.i16 + pvt->samples;
708  unsigned int i;
709 
710  for (i = 0; i < f->datalen; i++) {
711  *dst++ = g726_decode((src[i] >> 4) & 0xf, &tmp->g726);
712  *dst++ = g726_decode(src[i] & 0x0f, &tmp->g726);
713  }
714 
715  pvt->samples += f->samples;
716  pvt->datalen += 2 * f->samples; /* 2 bytes/sample */
717 
718  return 0;
719 }
720 
721 /*! \brief compress and store data (4-bit G726 samples, AAL2 packing) in outbuf */
722 static int lintog726aal2_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
723 {
724  struct g726_coder_pvt *tmp = pvt->pvt;
725  int16_t *src = f->data.ptr;
726  unsigned int i;
727 
728  for (i = 0; i < f->samples; i++) {
729  unsigned char d = g726_encode(src[i], &tmp->g726); /* this sample */
730 
731  if (tmp->next_flag & 0x80) { /* merge with leftover sample */
732  pvt->outbuf.c[pvt->datalen++] = ((tmp->next_flag & 0xf)<< 4) | d;
733  pvt->samples += 2; /* 2 samples per byte */
734  tmp->next_flag = 0;
735  } else {
736  tmp->next_flag = 0x80 | d;
737  }
738  }
739 
740  return 0;
741 }
742 
743 /*! \brief decode packed 4-bit G726 values (RFC3551 packing) and store in buffer. */
744 static int g726tolin_framein (struct ast_trans_pvt *pvt, struct ast_frame *f)
745 {
746  struct g726_coder_pvt *tmp = pvt->pvt;
747  unsigned char *src = f->data.ptr;
748  int16_t *dst = pvt->outbuf.i16 + pvt->samples;
749  unsigned int i;
750 
751  for (i = 0; i < f->datalen; i++) {
752  *dst++ = g726_decode(src[i] & 0x0f, &tmp->g726);
753  *dst++ = g726_decode((src[i] >> 4) & 0xf, &tmp->g726);
754  }
755 
756  pvt->samples += f->samples;
757  pvt->datalen += 2 * f->samples; /* 2 bytes/sample */
758 
759  return 0;
760 }
761 
762 /*! \brief compress and store data (4-bit G726 samples, RFC3551 packing) in outbuf */
763 static int lintog726_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
764 {
765  struct g726_coder_pvt *tmp = pvt->pvt;
766  int16_t *src = f->data.ptr;
767  unsigned int i;
768 
769  for (i = 0; i < f->samples; i++) {
770  unsigned char d = g726_encode(src[i], &tmp->g726); /* this sample */
771 
772  if (tmp->next_flag & 0x80) { /* merge with leftover sample */
773  pvt->outbuf.c[pvt->datalen++] = (d << 4) | (tmp->next_flag & 0xf);
774  pvt->samples += 2; /* 2 samples per byte */
775  tmp->next_flag = 0;
776  } else {
777  tmp->next_flag = 0x80 | d;
778  }
779  }
780 
781  return 0;
782 }
783 
784 static struct ast_translator g726tolin = {
785  .name = "g726tolin",
786  .src_codec = {
787  .name = "g726",
788  .type = AST_MEDIA_TYPE_AUDIO,
789  .sample_rate = 8000,
790  },
791  .dst_codec = {
792  .name = "slin",
793  .type = AST_MEDIA_TYPE_AUDIO,
794  .sample_rate = 8000,
795  },
796  .format = "slin",
797  .newpvt = lintog726_new, /* same for both directions */
798  .framein = g726tolin_framein,
799  .sample = g726_sample,
800  .desc_size = sizeof(struct g726_coder_pvt),
801  .buffer_samples = BUFFER_SAMPLES,
802  .buf_size = BUFFER_SAMPLES * 2,
803 };
804 
805 static struct ast_translator lintog726 = {
806  .name = "lintog726",
807  .src_codec = {
808  .name = "slin",
809  .type = AST_MEDIA_TYPE_AUDIO,
810  .sample_rate = 8000,
811  },
812  .dst_codec = {
813  .name = "g726",
814  .type = AST_MEDIA_TYPE_AUDIO,
815  .sample_rate = 8000,
816  },
817  .format = "g726",
818  .newpvt = lintog726_new, /* same for both directions */
819  .framein = lintog726_framein,
820  .sample = slin8_sample,
821  .desc_size = sizeof(struct g726_coder_pvt),
822  .buffer_samples = BUFFER_SAMPLES,
823  .buf_size = BUFFER_SAMPLES/2,
824 };
825 
826 static struct ast_translator g726aal2tolin = {
827  .name = "g726aal2tolin",
828  .src_codec = {
829  .name = "g726aal2",
830  .type = AST_MEDIA_TYPE_AUDIO,
831  .sample_rate = 8000,
832  },
833  .dst_codec = {
834  .name = "slin",
835  .type = AST_MEDIA_TYPE_AUDIO,
836  .sample_rate = 8000,
837  },
838  .format = "slin",
839  .newpvt = lintog726_new, /* same for both directions */
840  .framein = g726aal2tolin_framein,
841  .sample = g726_sample,
842  .desc_size = sizeof(struct g726_coder_pvt),
843  .buffer_samples = BUFFER_SAMPLES,
844  .buf_size = BUFFER_SAMPLES * 2,
845 };
846 
847 static struct ast_translator lintog726aal2 = {
848  .name = "lintog726aal2",
849  .src_codec = {
850  .name = "slin",
851  .type = AST_MEDIA_TYPE_AUDIO,
852  .sample_rate = 8000,
853  },
854  .dst_codec = {
855  .name = "g726aal2",
856  .type = AST_MEDIA_TYPE_AUDIO,
857  .sample_rate = 8000,
858  },
859  .format = "g726aal2",
860  .newpvt = lintog726_new, /* same for both directions */
861  .framein = lintog726aal2_framein,
862  .sample = slin8_sample,
863  .desc_size = sizeof(struct g726_coder_pvt),
864  .buffer_samples = BUFFER_SAMPLES,
865  .buf_size = BUFFER_SAMPLES / 2,
866 };
867 
868 static int unload_module(void)
869 {
870  int res = 0;
871 
872  res |= ast_unregister_translator(&g726tolin);
873  res |= ast_unregister_translator(&lintog726);
874 
875  res |= ast_unregister_translator(&g726aal2tolin);
876  res |= ast_unregister_translator(&lintog726aal2);
877 
878  return res;
879 }
880 
881 static int load_module(void)
882 {
883  int res = 0;
884 
885  res |= ast_register_translator(&g726tolin);
886  res |= ast_register_translator(&lintog726);
887 
888  res |= ast_register_translator(&g726aal2tolin);
889  res |= ast_register_translator(&lintog726aal2);
890 
891  if (res) {
892  unload_module();
894  }
895 
897 }
898 
899 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "ITU G.726-32kbps G726 Transcoder",
900  .support_level = AST_MODULE_SUPPORT_CORE,
901  .load = load_module,
902  .unload = unload_module,
903 );
static int qtab_721[7]
Definition: codec_g726.c:96
int datalen
actual space used in outbuf
Definition: translate.h:218
static int g726_encode(int sl, struct g726_state *state_ptr)
Definition: codec_g726.c:634
static int _witab[16]
Definition: codec_g726.c:105
4-bit G.726 data
static void g726_init_state(struct g726_state *state_ptr)
Definition: codec_g726.c:123
Asterisk locking-related definitions:
Asterisk main include file. File version handling, generic pbx functions.
unsigned char next_flag
Definition: codec_g726.c:688
int b[6]
Definition: codec_g726.c:83
Definition: ast_expr2.c:325
Descriptor of a translator.
Definition: translate.h:137
short int16_t
Definition: db.h:59
static void update(int code_size, int y, int wi, int fi, int dq, int sr, int dqsez, struct g726_state *state_ptr)
Definition: codec_g726.c:367
int a[2]
Definition: codec_g726.c:81
Support for translation of data formats. translate.c.
static struct ast_frame * g726_sample(void)
Definition: ex_g726.h:18
static struct test_val d
int dq[6]
Definition: codec_g726.c:87
static int tmp()
Definition: bt_open.c:389
struct g726_state g726
Definition: codec_g726.c:689
if(!yyg->yy_init)
Definition: ast_expr2f.c:868
static int step_size(struct g726_state *state_ptr)
Definition: codec_g726.c:247
static struct ast_frame * slin8_sample(void)
Definition: slin.h:64
void * pvt
Definition: translate.h:219
static int lintog726aal2_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
compress and store data (4-bit G726 samples, AAL2 packing) in outbuf
Definition: codec_g726.c:722
static int load_module(void)
Definition: codec_g726.c:881
static struct ast_translator lintog726
Definition: codec_g726.c:805
int16_t * i16
Definition: translate.h:223
Utility functions.
static struct ast_translator lintog726aal2
Definition: codec_g726.c:847
static char * table
Definition: cdr_odbc.c:58
Configuration File Parser.
static struct ast_translator g726aal2tolin
Definition: codec_g726.c:826
static int g726aal2tolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
decode packed 4-bit G726 values (AAL2 packing) and store in buffer.
Definition: codec_g726.c:703
log2comp.h - various base 2 log computation versions
#define BUFFER_SAMPLES
Definition: codec_g726.c:59
int sr[2]
Definition: codec_g726.c:90
#define ast_register_translator(t)
See __ast_register_translator()
Definition: translate.h:257
int ast_unregister_translator(struct ast_translator *t)
Unregister a translator Unregisters the given tranlator.
Definition: translate.c:1333
A set of macros to manage forward-linked lists.
static struct ast_translator g726tolin
Definition: codec_g726.c:784
union ast_trans_pvt::@327 outbuf
static int lintog726_new(struct ast_trans_pvt *pvt)
init a new instance of g726_coder_pvt.
Definition: codec_g726.c:693
static int unload_module(void)
Definition: codec_g726.c:868
Default structure for translators, with the basic fields and buffers, all allocated as part of the sa...
Definition: translate.h:213
static int _fitab[16]
Definition: codec_g726.c:112
static int _dqlntab[16]
Definition: codec_g726.c:101
static int quan(int val, int *table, int size)
Definition: codec_g726.c:160
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
static int ilog2(int val)
Definition: log2comp.h:67
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS|AST_MODFLAG_LOAD_ORDER, "HTTP Phone Provisioning",.support_level=AST_MODULE_SUPPORT_EXTENDED,.load=load_module,.unload=unload_module,.reload=reload,.load_pri=AST_MODPRI_CHANNEL_DEPEND,.requires="http",)
static int reconstruct(int sign, int dqln, int y)
Definition: codec_g726.c:331
#define abs(x)
Definition: f2c.h:195
static int g726tolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
decode packed 4-bit G726 values (RFC3551 packing) and store in buffer.
Definition: codec_g726.c:744
int pk[2]
Definition: codec_g726.c:85
static int lintog726_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
compress and store data (4-bit G726 samples, RFC3551 packing) in outbuf
Definition: codec_g726.c:763
static ENTRY retval
Definition: hsearch.c:50
static int predictor_pole(struct g726_state *state_ptr)
Definition: codec_g726.c:233
Data structure associated with a single frame of data.
static int fmult(int an, int srn)
Definition: codec_g726.c:205
static int g726_decode(int i, struct g726_state *state_ptr)
Definition: codec_g726.c:588
union ast_frame::@263 data
static int quantize(int d, int y, int *table, int size)
Definition: codec_g726.c:276
static int predictor_zero(struct g726_state *state_ptr)
Definition: codec_g726.c:224
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
Asterisk module definitions.
char name[80]
Definition: translate.h:138