Wed Jan 8 2020 09:49:46

Asterisk developer's documentation


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 <markster@digium.com>
7  * Kevin P. Fleming <kpfleming@digium.com>
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 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 328209 $")
37 
38 #include "asterisk/lock.h"
39 #include "asterisk/linkedlists.h"
40 #include "asterisk/module.h"
41 #include "asterisk/config.h"
42 #include "asterisk/translate.h"
43 #include "asterisk/utils.h"
44 
45 #define WANT_ASM
46 #include "log2comp.h"
47 
48 /* define NOT_BLI to use a faster but not bit-level identical version */
49 /* #define NOT_BLI */
50 
51 #if defined(NOT_BLI)
52 # if defined(_MSC_VER)
53 typedef __int64 sint64;
54 # elif defined(__GNUC__)
55 typedef long long sint64;
56 # else
57 # error 64-bit integer type is not defined for your compiler/platform
58 # endif
59 #endif
60 
61 #define BUFFER_SAMPLES 8096 /* size for the translation buffers */
62 #define BUF_SHIFT 5
63 
64 /* Sample frame data */
65 #include "asterisk/slin.h"
66 #include "ex_g726.h"
67 
68 /*
69  * The following is the definition of the state structure
70  * used by the G.726 encoder and decoder to preserve their internal
71  * state between successive calls. The meanings of the majority
72  * of the state structure fields are explained in detail in the
73  * CCITT Recommendation G.721. The field names are essentially identical
74  * to variable names in the bit level description of the coding algorithm
75  * included in this Recommendation.
76  */
77 struct g726_state {
78  long yl; /* Locked or steady state step size multiplier. */
79  int yu; /* Unlocked or non-steady state step size multiplier. */
80  int dms; /* Short term energy estimate. */
81  int dml; /* Long term energy estimate. */
82  int ap; /* Linear weighting coefficient of 'yl' and 'yu'. */
83  int a[2]; /* Coefficients of pole portion of prediction filter.
84  * stored as fixed-point 1==2^14 */
85  int b[6]; /* Coefficients of zero portion of prediction filter.
86  * stored as fixed-point 1==2^14 */
87  int pk[2]; /* Signs of previous two samples of a partially
88  * reconstructed signal. */
89  int dq[6]; /* Previous 6 samples of the quantized difference signal
90  * stored as fixed point 1==2^12,
91  * or in internal floating point format */
92  int sr[2]; /* Previous 2 samples of the quantized difference signal
93  * stored as fixed point 1==2^12,
94  * or in internal floating point format */
95  int td; /* delayed tone detect, new in 1988 version */
96 };
97 
98 static int qtab_721[7] = {-124, 80, 178, 246, 300, 349, 400};
99 /*
100  * Maps G.721 code word to reconstructed scale factor normalized log
101  * magnitude values.
102  */
103 static int _dqlntab[16] = {-2048, 4, 135, 213, 273, 323, 373, 425,
104  425, 373, 323, 273, 213, 135, 4, -2048};
105 
106 /* Maps G.721 code word to log of scale factor multiplier. */
107 static int _witab[16] = {-12, 18, 41, 64, 112, 198, 355, 1122,
108  1122, 355, 198, 112, 64, 41, 18, -12};
109 /*
110  * Maps G.721 code words to a set of values whose long and short
111  * term averages are computed and then compared to give an indication
112  * how stationary (steady state) the signal is.
113  */
114 static int _fitab[16] = {0, 0, 0, 0x200, 0x200, 0x200, 0x600, 0xE00,
115  0xE00, 0x600, 0x200, 0x200, 0x200, 0, 0, 0};
116 
117 
118 /*
119  * g72x_init_state()
120  *
121  * This routine initializes and/or resets the g726_state structure
122  * pointed to by 'state_ptr'.
123  * All the initial state values are specified in the CCITT G.721 document.
124  */
125 static void g726_init_state(struct g726_state *state_ptr)
126 {
127  int cnta;
128 
129  state_ptr->yl = 34816;
130  state_ptr->yu = 544;
131  state_ptr->dms = 0;
132  state_ptr->dml = 0;
133  state_ptr->ap = 0;
134  for (cnta = 0; cnta < 2; cnta++) {
135  state_ptr->a[cnta] = 0;
136  state_ptr->pk[cnta] = 0;
137 #ifdef NOT_BLI
138  state_ptr->sr[cnta] = 1;
139 #else
140  state_ptr->sr[cnta] = 32;
141 #endif
142  }
143  for (cnta = 0; cnta < 6; cnta++) {
144  state_ptr->b[cnta] = 0;
145 #ifdef NOT_BLI
146  state_ptr->dq[cnta] = 1;
147 #else
148  state_ptr->dq[cnta] = 32;
149 #endif
150  }
151  state_ptr->td = 0;
152 }
153 
154 /*
155  * quan()
156  *
157  * quantizes the input val against the table of integers.
158  * It returns i if table[i - 1] <= val < table[i].
159  *
160  * Using linear search for simple coding.
161  */
162 static int quan(int val, int *table, int size)
163 {
164  int i;
165 
166  for (i = 0; i < size && val >= *table; ++i, ++table)
167  ;
168  return (i);
169 }
170 
171 #ifdef NOT_BLI /* faster non-identical version */
172 
173 /*
174  * predictor_zero()
175  *
176  * computes the estimated signal from 6-zero predictor.
177  *
178  */
179 static int predictor_zero(struct g726_state *state_ptr)
180 { /* divide by 2 is necessary here to handle negative numbers correctly */
181  int i;
182  sint64 sezi;
183  for (sezi = 0, i = 0; i < 6; i++) /* ACCUM */
184  sezi += (sint64)state_ptr->b[i] * state_ptr->dq[i];
185  return (int)(sezi >> 13) / 2 /* 2^14 */;
186 }
187 
188 /*
189  * predictor_pole()
190  *
191  * computes the estimated signal from 2-pole predictor.
192  *
193  */
194 static int predictor_pole(struct g726_state *state_ptr)
195 { /* divide by 2 is necessary here to handle negative numbers correctly */
196  return (int)(((sint64)state_ptr->a[1] * state_ptr->sr[1] +
197  (sint64)state_ptr->a[0] * state_ptr->sr[0]) >> 13) / 2 /* 2^14 */;
198 }
199 
200 #else /* NOT_BLI - identical version */
201 /*
202  * fmult()
203  *
204  * returns the integer product of the fixed-point number "an" (1==2^12) and
205  * "floating point" representation (4-bit exponent, 6-bit mantessa) "srn".
206  */
207 static int fmult(int an, int srn)
208 {
209  int anmag, anexp, anmant;
210  int wanexp, wanmant;
211  int retval;
212 
213  anmag = (an > 0) ? an : ((-an) & 0x1FFF);
214  anexp = ilog2(anmag) - 5;
215  anmant = (anmag == 0) ? 32 :
216  (anexp >= 0) ? anmag >> anexp : anmag << -anexp;
217  wanexp = anexp + ((srn >> 6) & 0xF) - 13;
218 
219  wanmant = (anmant * (srn & 077) + 0x30) >> 4;
220  retval = (wanexp >= 0) ? ((wanmant << wanexp) & 0x7FFF) :
221  (wanmant >> -wanexp);
222 
223  return (((an ^ srn) < 0) ? -retval : retval);
224 }
225 
226 static int predictor_zero(struct g726_state *state_ptr)
227 {
228  int i;
229  int sezi;
230  for (sezi = 0, i = 0; i < 6; i++) /* ACCUM */
231  sezi += fmult(state_ptr->b[i] >> 2, state_ptr->dq[i]);
232  return sezi;
233 }
234 
235 static int predictor_pole(struct g726_state *state_ptr)
236 {
237  return (fmult(state_ptr->a[1] >> 2, state_ptr->sr[1]) +
238  fmult(state_ptr->a[0] >> 2, state_ptr->sr[0]));
239 }
240 
241 #endif /* NOT_BLI */
242 
243 /*
244  * step_size()
245  *
246  * computes the quantization step size of the adaptive quantizer.
247  *
248  */
249 static int step_size(struct g726_state *state_ptr)
250 {
251  int y;
252  int dif;
253  int al;
254 
255  if (state_ptr->ap >= 256)
256  return (state_ptr->yu);
257  else {
258  y = state_ptr->yl >> 6;
259  dif = state_ptr->yu - y;
260  al = state_ptr->ap >> 2;
261  if (dif > 0)
262  y += (dif * al) >> 6;
263  else if (dif < 0)
264  y += (dif * al + 0x3F) >> 6;
265  return (y);
266  }
267 }
268 
269 /*
270  * quantize()
271  *
272  * Given a raw sample, 'd', of the difference signal and a
273  * quantization step size scale factor, 'y', this routine returns the
274  * ADPCM codeword to which that sample gets quantized. The step
275  * size scale factor division operation is done in the log base 2 domain
276  * as a subtraction.
277  */
278 static int quantize(
279  int d, /* Raw difference signal sample */
280  int y, /* Step size multiplier */
281  int *table, /* quantization table */
282  int size) /* table size of integers */
283 {
284  int dqm; /* Magnitude of 'd' */
285  int exp; /* Integer part of base 2 log of 'd' */
286  int mant; /* Fractional part of base 2 log */
287  int dl; /* Log of magnitude of 'd' */
288  int dln; /* Step size scale factor normalized log */
289  int i;
290 
291  /*
292  * LOG
293  *
294  * Compute base 2 log of 'd', and store in 'dl'.
295  */
296  dqm = abs(d);
297  exp = ilog2(dqm);
298  if (exp < 0)
299  exp = 0;
300  mant = ((dqm << 7) >> exp) & 0x7F; /* Fractional portion. */
301  dl = (exp << 7) | mant;
302 
303  /*
304  * SUBTB
305  *
306  * "Divide" by step size multiplier.
307  */
308  dln = dl - (y >> 2);
309 
310  /*
311  * QUAN
312  *
313  * Obtain codword i for 'd'.
314  */
315  i = quan(dln, table, size);
316  if (d < 0) /* take 1's complement of i */
317  return ((size << 1) + 1 - i);
318  else if (i == 0) /* take 1's complement of 0 */
319  return ((size << 1) + 1); /* new in 1988 */
320  else
321  return (i);
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  /* FILTE & DELAY */
426  /* update steady state step size multiplier */
427  state_ptr->yl += state_ptr->yu + ((-state_ptr->yl) >> 6);
428 
429  /*
430  * Adaptive predictor coefficients.
431  */
432  if (tr == 1) { /* reset a's and b's for modem signal */
433  state_ptr->a[0] = 0;
434  state_ptr->a[1] = 0;
435  state_ptr->b[0] = 0;
436  state_ptr->b[1] = 0;
437  state_ptr->b[2] = 0;
438  state_ptr->b[3] = 0;
439  state_ptr->b[4] = 0;
440  state_ptr->b[5] = 0;
441  } else { /* update a's and b's */
442  pks1 = pk0 ^ state_ptr->pk[0]; /* UPA2 */
443 
444  /* update predictor pole a[1] */
445  a2p = state_ptr->a[1] - (state_ptr->a[1] >> 7);
446  if (dqsez != 0) {
447  fa1 = (pks1) ? state_ptr->a[0] : -state_ptr->a[0];
448  if (fa1 < -8191) /* a2p = function of fa1 */
449  a2p -= 0x100;
450  else if (fa1 > 8191)
451  a2p += 0xFF;
452  else
453  a2p += fa1 >> 5;
454 
455  if (pk0 ^ state_ptr->pk[1])
456  /* LIMC */
457  if (a2p <= -12160)
458  a2p = -12288;
459  else if (a2p >= 12416)
460  a2p = 12288;
461  else
462  a2p -= 0x80;
463  else if (a2p <= -12416)
464  a2p = -12288;
465  else if (a2p >= 12160)
466  a2p = 12288;
467  else
468  a2p += 0x80;
469  }
470 
471  /* TRIGB & DELAY */
472  state_ptr->a[1] = a2p;
473 
474  /* UPA1 */
475  /* update predictor pole a[0] */
476  state_ptr->a[0] -= state_ptr->a[0] >> 8;
477  if (dqsez != 0) {
478  if (pks1 == 0)
479  state_ptr->a[0] += 192;
480  else
481  state_ptr->a[0] -= 192;
482  }
483  /* LIMD */
484  a1ul = 15360 - a2p;
485  if (state_ptr->a[0] < -a1ul)
486  state_ptr->a[0] = -a1ul;
487  else if (state_ptr->a[0] > a1ul)
488  state_ptr->a[0] = a1ul;
489 
490  /* UPB : update predictor zeros b[6] */
491  for (cnt = 0; cnt < 6; cnt++) {
492  if (code_size == 5) /* for 40Kbps G.723 */
493  state_ptr->b[cnt] -= state_ptr->b[cnt] >> 9;
494  else /* for G.721 and 24Kbps G.723 */
495  state_ptr->b[cnt] -= state_ptr->b[cnt] >> 8;
496  if (mag)
497  { /* XOR */
498  if ((dq ^ state_ptr->dq[cnt]) >= 0)
499  state_ptr->b[cnt] += 128;
500  else
501  state_ptr->b[cnt] -= 128;
502  }
503  }
504  }
505 
506  for (cnt = 5; cnt > 0; cnt--)
507  state_ptr->dq[cnt] = state_ptr->dq[cnt-1];
508 #ifdef NOT_BLI
509  state_ptr->dq[0] = dq;
510 #else
511  /* FLOAT A : convert dq[0] to 4-bit exp, 6-bit mantissa f.p. */
512  if (mag == 0) {
513  state_ptr->dq[0] = (dq >= 0) ? 0x20 : 0x20 - 0x400;
514  } else {
515  exp = ilog2(mag) + 1;
516  state_ptr->dq[0] = (dq >= 0) ?
517  (exp << 6) + ((mag << 6) >> exp) :
518  (exp << 6) + ((mag << 6) >> exp) - 0x400;
519  }
520 #endif
521 
522  state_ptr->sr[1] = state_ptr->sr[0];
523 #ifdef NOT_BLI
524  state_ptr->sr[0] = sr;
525 #else
526  /* FLOAT B : convert sr to 4-bit exp., 6-bit mantissa f.p. */
527  if (sr == 0) {
528  state_ptr->sr[0] = 0x20;
529  } else if (sr > 0) {
530  exp = ilog2(sr) + 1;
531  state_ptr->sr[0] = (exp << 6) + ((sr << 6) >> exp);
532  } else if (sr > -0x8000) {
533  mag = -sr;
534  exp = ilog2(mag) + 1;
535  state_ptr->sr[0] = (exp << 6) + ((mag << 6) >> exp) - 0x400;
536  } else
537  state_ptr->sr[0] = 0x20 - 0x400;
538 #endif
539 
540  /* DELAY A */
541  state_ptr->pk[1] = state_ptr->pk[0];
542  state_ptr->pk[0] = pk0;
543 
544  /* TONE */
545  if (tr == 1) /* this sample has been treated as data */
546  state_ptr->td = 0; /* next one will be treated as voice */
547  else if (a2p < -11776) /* small sample-to-sample correlation */
548  state_ptr->td = 1; /* signal may be data */
549  else /* signal is voice */
550  state_ptr->td = 0;
551 
552  /*
553  * Adaptation speed control.
554  */
555  state_ptr->dms += (fi - state_ptr->dms) >> 5; /* FILTA */
556  state_ptr->dml += (((fi << 2) - state_ptr->dml) >> 7); /* FILTB */
557 
558  if (tr == 1)
559  state_ptr->ap = 256;
560  else if (y < 1536) /* SUBTC */
561  state_ptr->ap += (0x200 - state_ptr->ap) >> 4;
562  else if (state_ptr->td == 1)
563  state_ptr->ap += (0x200 - state_ptr->ap) >> 4;
564  else if (abs((state_ptr->dms << 2) - state_ptr->dml) >=
565  (state_ptr->dml >> 3))
566  state_ptr->ap += (0x200 - state_ptr->ap) >> 4;
567  else
568  state_ptr->ap += (-state_ptr->ap) >> 4;
569 }
570 
571 /*
572  * g726_decode()
573  *
574  * Description:
575  *
576  * Decodes a 4-bit code of G.726-32 encoded data of i and
577  * returns the resulting linear PCM, A-law or u-law value.
578  * return -1 for unknown out_coding value.
579  */
580 static int g726_decode(int i, struct g726_state *state_ptr)
581 {
582  int sezi, sez, se; /* ACCUM */
583  int y; /* MIX */
584  int sr; /* ADDB */
585  int dq;
586  int dqsez;
587 
588  i &= 0x0f; /* mask to get proper bits */
589 #ifdef NOT_BLI
590  sezi = predictor_zero(state_ptr);
591  sez = sezi;
592  se = sezi + predictor_pole(state_ptr); /* estimated signal */
593 #else
594  sezi = predictor_zero(state_ptr);
595  sez = sezi >> 1;
596  se = (sezi + predictor_pole(state_ptr)) >> 1; /* estimated signal */
597 #endif
598 
599  y = step_size(state_ptr); /* dynamic quantizer step size */
600 
601  dq = reconstruct(i & 8, _dqlntab[i], y); /* quantized diff. */
602 
603 #ifdef NOT_BLI
604  sr = se + dq; /* reconst. signal */
605  dqsez = dq + sez; /* pole prediction diff. */
606 #else
607  sr = (dq < 0) ? se - (dq & 0x3FFF) : se + dq; /* reconst. signal */
608  dqsez = sr - se + sez; /* pole prediction diff. */
609 #endif
610 
611  update(4, y, _witab[i] << 5, _fitab[i], dq, sr, dqsez, state_ptr);
612 
613 #ifdef NOT_BLI
614  return (sr >> 10); /* sr was 26-bit dynamic range */
615 #else
616  return (sr << 2); /* sr was 14-bit dynamic range */
617 #endif
618 }
619 
620 /*
621  * g726_encode()
622  *
623  * Encodes the input vale of linear PCM, A-law or u-law data sl and returns
624  * the resulting code. -1 is returned for unknown input coding value.
625  */
626 static int g726_encode(int sl, struct g726_state *state_ptr)
627 {
628  int sezi, se, sez; /* ACCUM */
629  int d; /* SUBTA */
630  int sr; /* ADDB */
631  int y; /* MIX */
632  int dqsez; /* ADDC */
633  int dq, i;
634 
635 #ifdef NOT_BLI
636  sl <<= 10; /* 26-bit dynamic range */
637 
638  sezi = predictor_zero(state_ptr);
639  sez = sezi;
640  se = sezi + predictor_pole(state_ptr); /* estimated signal */
641 #else
642  sl >>= 2; /* 14-bit dynamic range */
643 
644  sezi = predictor_zero(state_ptr);
645  sez = sezi >> 1;
646  se = (sezi + predictor_pole(state_ptr)) >> 1; /* estimated signal */
647 #endif
648 
649  d = sl - se; /* estimation difference */
650 
651  /* quantize the prediction difference */
652  y = step_size(state_ptr); /* quantizer step size */
653 #ifdef NOT_BLI
654  d /= 0x1000;
655 #endif
656  i = quantize(d, y, qtab_721, 7); /* i = G726 code */
657 
658  dq = reconstruct(i & 8, _dqlntab[i], y); /* quantized est diff */
659 
660 #ifdef NOT_BLI
661  sr = se + dq; /* reconst. signal */
662  dqsez = dq + sez; /* pole prediction diff. */
663 #else
664  sr = (dq < 0) ? se - (dq & 0x3FFF) : se + dq; /* reconst. signal */
665  dqsez = sr - se + sez; /* pole prediction diff. */
666 #endif
667 
668  update(4, y, _witab[i] << 5, _fitab[i], dq, sr, dqsez, state_ptr);
669 
670  return (i);
671 }
672 
673 /*
674  * Private workspace for translating signed linear signals to G726.
675  * Don't bother to define two distinct structs.
676  */
677 
679  /* buffer any odd byte in input - 0x80 + (value & 0xf) if present */
680  unsigned char next_flag;
681  struct g726_state g726;
682 };
683 
684 /*! \brief init a new instance of g726_coder_pvt. */
685 static int lintog726_new(struct ast_trans_pvt *pvt)
686 {
687  struct g726_coder_pvt *tmp = pvt->pvt;
688 
689  g726_init_state(&tmp->g726);
690 
691  return 0;
692 }
693 
694 /*! \brief decode packed 4-bit G726 values (AAL2 packing) and store in buffer. */
695 static int g726aal2tolin_framein (struct ast_trans_pvt *pvt, struct ast_frame *f)
696 {
697  struct g726_coder_pvt *tmp = pvt->pvt;
698  unsigned char *src = f->data.ptr;
699  int16_t *dst = pvt->outbuf.i16 + pvt->samples;
700  unsigned int i;
701 
702  for (i = 0; i < f->datalen; i++) {
703  *dst++ = g726_decode((src[i] >> 4) & 0xf, &tmp->g726);
704  *dst++ = g726_decode(src[i] & 0x0f, &tmp->g726);
705  }
706 
707  pvt->samples += f->samples;
708  pvt->datalen += 2 * f->samples; /* 2 bytes/sample */
709 
710  return 0;
711 }
712 
713 /*! \brief compress and store data (4-bit G726 samples, AAL2 packing) in outbuf */
714 static int lintog726aal2_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
715 {
716  struct g726_coder_pvt *tmp = pvt->pvt;
717  int16_t *src = f->data.ptr;
718  unsigned int i;
719 
720  for (i = 0; i < f->samples; i++) {
721  unsigned char d = g726_encode(src[i], &tmp->g726); /* this sample */
722 
723  if (tmp->next_flag & 0x80) { /* merge with leftover sample */
724  pvt->outbuf.c[pvt->datalen++] = ((tmp->next_flag & 0xf)<< 4) | d;
725  pvt->samples += 2; /* 2 samples per byte */
726  tmp->next_flag = 0;
727  } else {
728  tmp->next_flag = 0x80 | d;
729  }
730  }
731 
732  return 0;
733 }
734 
735 /*! \brief decode packed 4-bit G726 values (RFC3551 packing) and store in buffer. */
736 static int g726tolin_framein (struct ast_trans_pvt *pvt, struct ast_frame *f)
737 {
738  struct g726_coder_pvt *tmp = pvt->pvt;
739  unsigned char *src = f->data.ptr;
740  int16_t *dst = pvt->outbuf.i16 + pvt->samples;
741  unsigned int i;
742 
743  for (i = 0; i < f->datalen; i++) {
744  *dst++ = g726_decode(src[i] & 0x0f, &tmp->g726);
745  *dst++ = g726_decode((src[i] >> 4) & 0xf, &tmp->g726);
746  }
747 
748  pvt->samples += f->samples;
749  pvt->datalen += 2 * f->samples; /* 2 bytes/sample */
750 
751  return 0;
752 }
753 
754 /*! \brief compress and store data (4-bit G726 samples, RFC3551 packing) in outbuf */
755 static int lintog726_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
756 {
757  struct g726_coder_pvt *tmp = pvt->pvt;
758  int16_t *src = f->data.ptr;
759  unsigned int i;
760 
761  for (i = 0; i < f->samples; i++) {
762  unsigned char d = g726_encode(src[i], &tmp->g726); /* this sample */
763 
764  if (tmp->next_flag & 0x80) { /* merge with leftover sample */
765  pvt->outbuf.c[pvt->datalen++] = (d << 4) | (tmp->next_flag & 0xf);
766  pvt->samples += 2; /* 2 samples per byte */
767  tmp->next_flag = 0;
768  } else {
769  tmp->next_flag = 0x80 | d;
770  }
771  }
772 
773  return 0;
774 }
775 
776 static struct ast_translator g726tolin = {
777  .name = "g726tolin",
778  .srcfmt = AST_FORMAT_G726,
779  .dstfmt = AST_FORMAT_SLINEAR,
780  .newpvt = lintog726_new, /* same for both directions */
781  .framein = g726tolin_framein,
782  .sample = g726_sample,
783  .desc_size = sizeof(struct g726_coder_pvt),
784  .buffer_samples = BUFFER_SAMPLES,
785  .buf_size = BUFFER_SAMPLES * 2,
786 };
787 
788 static struct ast_translator lintog726 = {
789  .name = "lintog726",
790  .srcfmt = AST_FORMAT_SLINEAR,
791  .dstfmt = AST_FORMAT_G726,
792  .newpvt = lintog726_new, /* same for both directions */
793  .framein = lintog726_framein,
794  .sample = slin8_sample,
795  .desc_size = sizeof(struct g726_coder_pvt),
796  .buffer_samples = BUFFER_SAMPLES,
797  .buf_size = BUFFER_SAMPLES/2,
798 };
799 
800 static struct ast_translator g726aal2tolin = {
801  .name = "g726aal2tolin",
802  .srcfmt = AST_FORMAT_G726_AAL2,
803  .dstfmt = AST_FORMAT_SLINEAR,
804  .newpvt = lintog726_new, /* same for both directions */
805  .framein = g726aal2tolin_framein,
806  .sample = g726_sample,
807  .desc_size = sizeof(struct g726_coder_pvt),
808  .buffer_samples = BUFFER_SAMPLES,
809  .buf_size = BUFFER_SAMPLES * 2,
810 };
811 
812 static struct ast_translator lintog726aal2 = {
813  .name = "lintog726aal2",
814  .srcfmt = AST_FORMAT_SLINEAR,
815  .dstfmt = AST_FORMAT_G726_AAL2,
816  .newpvt = lintog726_new, /* same for both directions */
817  .framein = lintog726aal2_framein,
818  .sample = slin8_sample,
819  .desc_size = sizeof(struct g726_coder_pvt),
820  .buffer_samples = BUFFER_SAMPLES,
821  .buf_size = BUFFER_SAMPLES / 2,
822 };
823 
824 static int reload(void)
825 {
827 }
828 
829 static int unload_module(void)
830 {
831  int res = 0;
832 
833  res |= ast_unregister_translator(&g726tolin);
834  res |= ast_unregister_translator(&lintog726);
835 
836  res |= ast_unregister_translator(&g726aal2tolin);
837  res |= ast_unregister_translator(&lintog726aal2);
838 
839  return res;
840 }
841 
842 static int load_module(void)
843 {
844  int res = 0;
845 
846  res |= ast_register_translator(&g726tolin);
847  res |= ast_register_translator(&lintog726);
848 
849  res |= ast_register_translator(&g726aal2tolin);
850  res |= ast_register_translator(&lintog726aal2);
851 
852  if (res) {
853  unload_module();
855  }
856 
858 }
859 
860 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "ITU G.726-32kbps G726 Transcoder",
861  .load = load_module,
862  .unload = unload_module,
863  .reload = reload,
864  );
static int qtab_721[7]
Definition: codec_g726.c:98
int datalen
actual space used in outbuf
Definition: translate.h:140
static int g726_encode(int sl, struct g726_state *state_ptr)
Definition: codec_g726.c:626
static int _witab[16]
Definition: codec_g726.c:107
4-bit G.726 data
static void g726_init_state(struct g726_state *state_ptr)
Definition: codec_g726.c:125
Asterisk main include file. File version handling, generic pbx functions.
unsigned char next_flag
Definition: codec_g726.c:680
union ast_trans_pvt::@213 outbuf
int b[6]
Definition: codec_g726.c:85
Definition: ast_expr2.c:325
Descriptor of a translator.
Definition: translate.h:71
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:83
Support for translation of data formats. translate.c.
void * ptr
Definition: frame.h:160
static struct ast_frame * g726_sample(void)
Definition: ex_g726.h:18
int dq[6]
Definition: codec_g726.c:89
struct g726_state g726
Definition: codec_g726.c:681
Configuration File Parser.
static int step_size(struct g726_state *state_ptr)
Definition: codec_g726.c:249
const char name[80]
Definition: translate.h:72
#define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...)
Definition: module.h:374
static struct ast_frame * slin8_sample(void)
Definition: slin.h:61
void * pvt
Definition: translate.h:141
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:714
static int load_module(void)
Definition: codec_g726.c:842
static struct ast_translator lintog726
Definition: codec_g726.c:788
int16_t * i16
Definition: translate.h:145
Utility functions.
static struct ast_translator lintog726aal2
Definition: codec_g726.c:812
static char * table
Definition: cdr_odbc.c:50
static struct ast_translator g726aal2tolin
Definition: codec_g726.c:800
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:695
log2comp.h - various base 2 log computation versions
#define BUFFER_SAMPLES
Definition: codec_g726.c:61
int datalen
Definition: frame.h:148
int sr[2]
Definition: codec_g726.c:92
#define ast_register_translator(t)
See __ast_register_translator()
Definition: translate.h:170
#define AST_FORMAT_G726
Definition: frame.h:264
#define AST_FORMAT_G726_AAL2
Definition: frame.h:250
int ast_unregister_translator(struct ast_translator *t)
Unregister a translator Unregisters the given tranlator.
Definition: translate.c:942
A set of macros to manage forward-linked lists.
static struct ast_translator g726tolin
Definition: codec_g726.c:776
static int reload(void)
Definition: codec_g726.c:824
static int lintog726_new(struct ast_trans_pvt *pvt)
init a new instance of g726_coder_pvt.
Definition: codec_g726.c:685
static int unload_module(void)
Definition: codec_g726.c:829
Default structure for translators, with the basic fields and buffers, all allocated as part of the sa...
Definition: translate.h:135
static int _fitab[16]
Definition: codec_g726.c:114
static int _dqlntab[16]
Definition: codec_g726.c:103
static int quan(int val, int *table, int size)
Definition: codec_g726.c:162
static struct ast_format f[]
Definition: format_g726.c:181
if(yyss+yystacksize-1<=yyssp)
Definition: ast_expr2.c:1874
static int ilog2(int val)
Definition: log2comp.h:67
static int reconstruct(int sign, int dqln, int y)
Definition: codec_g726.c:331
#define AST_FORMAT_SLINEAR
Definition: frame.h:254
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:736
int pk[2]
Definition: codec_g726.c:87
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:755
static int predictor_pole(struct g726_state *state_ptr)
Definition: codec_g726.c:235
Data structure associated with a single frame of data.
Definition: frame.h:142
static int fmult(int an, int srn)
Definition: codec_g726.c:207
static int g726_decode(int i, struct g726_state *state_ptr)
Definition: codec_g726.c:580
static int quantize(int d, int y, int *table, int size)
Definition: codec_g726.c:278
static int predictor_zero(struct g726_state *state_ptr)
Definition: codec_g726.c:226
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:38
Asterisk module definitions.
union ast_frame::@172 data
#define ASTERISK_FILE_VERSION(file, version)
Register/unregister a source code file with the core.
Definition: asterisk.h:180
int samples
Definition: frame.h:150