Blocked revisions 117809 via svnmerge
[asterisk-bristuff.git] / codecs / codec_g726.c
blob8d1346e6692f37647cb357c66c4369341f871b36
1 /*
2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2006, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
7 * Kevin P. Fleming <kpfleming@digium.com>
9 * Based on frompcm.c and topcm.c from the Emiliano MIPL browser/
10 * interpreter. See http://www.bsdtelephony.com.mx
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.
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.
23 /*! \file
25 * \brief codec_g726.c - translate between signed linear and ITU G.726-32kbps (both RFC3551 and AAL2 codeword packing)
27 * \ingroup codecs
30 #include "asterisk.h"
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
34 #include "asterisk/lock.h"
35 #include "asterisk/linkedlists.h"
36 #include "asterisk/module.h"
37 #include "asterisk/config.h"
38 #include "asterisk/translate.h"
39 #include "asterisk/utils.h"
41 #define WANT_ASM
42 #include "log2comp.h"
44 /* define NOT_BLI to use a faster but not bit-level identical version */
45 /* #define NOT_BLI */
47 #if defined(NOT_BLI)
48 # if defined(_MSC_VER)
49 typedef __int64 sint64;
50 # elif defined(__GNUC__)
51 typedef long long sint64;
52 # else
53 # error 64-bit integer type is not defined for your compiler/platform
54 # endif
55 #endif
57 #define BUFFER_SAMPLES 8096 /* size for the translation buffers */
58 #define BUF_SHIFT 5
60 /* Sample frame data */
62 #include "slin_g726_ex.h"
63 #include "g726_slin_ex.h"
66 * The following is the definition of the state structure
67 * used by the G.726 encoder and decoder to preserve their internal
68 * state between successive calls. The meanings of the majority
69 * of the state structure fields are explained in detail in the
70 * CCITT Recommendation G.721. The field names are essentially identical
71 * to variable names in the bit level description of the coding algorithm
72 * included in this Recommendation.
74 struct g726_state {
75 long yl; /* Locked or steady state step size multiplier. */
76 int yu; /* Unlocked or non-steady state step size multiplier. */
77 int dms; /* Short term energy estimate. */
78 int dml; /* Long term energy estimate. */
79 int ap; /* Linear weighting coefficient of 'yl' and 'yu'. */
80 int a[2]; /* Coefficients of pole portion of prediction filter.
81 * stored as fixed-point 1==2^14 */
82 int b[6]; /* Coefficients of zero portion of prediction filter.
83 * stored as fixed-point 1==2^14 */
84 int pk[2]; /* Signs of previous two samples of a partially
85 * reconstructed signal. */
86 int dq[6]; /* Previous 6 samples of the quantized difference signal
87 * stored as fixed point 1==2^12,
88 * or in internal floating point format */
89 int sr[2]; /* Previous 2 samples of the quantized difference signal
90 * stored as fixed point 1==2^12,
91 * or in internal floating point format */
92 int td; /* delayed tone detect, new in 1988 version */
95 static int qtab_721[7] = {-124, 80, 178, 246, 300, 349, 400};
97 * Maps G.721 code word to reconstructed scale factor normalized log
98 * magnitude values.
100 static int _dqlntab[16] = {-2048, 4, 135, 213, 273, 323, 373, 425,
101 425, 373, 323, 273, 213, 135, 4, -2048};
103 /* Maps G.721 code word to log of scale factor multiplier. */
104 static int _witab[16] = {-12, 18, 41, 64, 112, 198, 355, 1122,
105 1122, 355, 198, 112, 64, 41, 18, -12};
107 * Maps G.721 code words to a set of values whose long and short
108 * term averages are computed and then compared to give an indication
109 * how stationary (steady state) the signal is.
111 static int _fitab[16] = {0, 0, 0, 0x200, 0x200, 0x200, 0x600, 0xE00,
112 0xE00, 0x600, 0x200, 0x200, 0x200, 0, 0, 0};
116 * g72x_init_state()
118 * This routine initializes and/or resets the g726_state structure
119 * pointed to by 'state_ptr'.
120 * All the initial state values are specified in the CCITT G.721 document.
122 static void g726_init_state(struct g726_state *state_ptr)
124 int cnta;
126 state_ptr->yl = 34816;
127 state_ptr->yu = 544;
128 state_ptr->dms = 0;
129 state_ptr->dml = 0;
130 state_ptr->ap = 0;
131 for (cnta = 0; cnta < 2; cnta++) {
132 state_ptr->a[cnta] = 0;
133 state_ptr->pk[cnta] = 0;
134 #ifdef NOT_BLI
135 state_ptr->sr[cnta] = 1;
136 #else
137 state_ptr->sr[cnta] = 32;
138 #endif
140 for (cnta = 0; cnta < 6; cnta++) {
141 state_ptr->b[cnta] = 0;
142 #ifdef NOT_BLI
143 state_ptr->dq[cnta] = 1;
144 #else
145 state_ptr->dq[cnta] = 32;
146 #endif
148 state_ptr->td = 0;
152 * quan()
154 * quantizes the input val against the table of integers.
155 * It returns i if table[i - 1] <= val < table[i].
157 * Using linear search for simple coding.
159 static int quan(int val, int *table, int size)
161 int i;
163 for (i = 0; i < size && val >= *table; ++i, ++table)
165 return (i);
168 #ifdef NOT_BLI /* faster non-identical version */
171 * predictor_zero()
173 * computes the estimated signal from 6-zero predictor.
176 static int predictor_zero(struct g726_state *state_ptr)
177 { /* divide by 2 is necessary here to handle negative numbers correctly */
178 int i;
179 sint64 sezi;
180 for (sezi = 0, i = 0; i < 6; i++) /* ACCUM */
181 sezi += (sint64)state_ptr->b[i] * state_ptr->dq[i];
182 return (int)(sezi >> 13) / 2 /* 2^14 */;
186 * predictor_pole()
188 * computes the estimated signal from 2-pole predictor.
191 static int predictor_pole(struct g726_state *state_ptr)
192 { /* divide by 2 is necessary here to handle negative numbers correctly */
193 return (int)(((sint64)state_ptr->a[1] * state_ptr->sr[1] +
194 (sint64)state_ptr->a[0] * state_ptr->sr[0]) >> 13) / 2 /* 2^14 */;
197 #else /* NOT_BLI - identical version */
199 * fmult()
201 * returns the integer product of the fixed-point number "an" (1==2^12) and
202 * "floating point" representation (4-bit exponent, 6-bit mantessa) "srn".
204 static int fmult(int an, int srn)
206 int anmag, anexp, anmant;
207 int wanexp, wanmant;
208 int retval;
210 anmag = (an > 0) ? an : ((-an) & 0x1FFF);
211 anexp = ilog2(anmag) - 5;
212 anmant = (anmag == 0) ? 32 :
213 (anexp >= 0) ? anmag >> anexp : anmag << -anexp;
214 wanexp = anexp + ((srn >> 6) & 0xF) - 13;
216 wanmant = (anmant * (srn & 077) + 0x30) >> 4;
217 retval = (wanexp >= 0) ? ((wanmant << wanexp) & 0x7FFF) :
218 (wanmant >> -wanexp);
220 return (((an ^ srn) < 0) ? -retval : retval);
223 static int predictor_zero(struct g726_state *state_ptr)
225 int i;
226 int sezi;
227 for (sezi = 0, i = 0; i < 6; i++) /* ACCUM */
228 sezi += fmult(state_ptr->b[i] >> 2, state_ptr->dq[i]);
229 return sezi;
232 static int predictor_pole(struct g726_state *state_ptr)
234 return (fmult(state_ptr->a[1] >> 2, state_ptr->sr[1]) +
235 fmult(state_ptr->a[0] >> 2, state_ptr->sr[0]));
238 #endif /* NOT_BLI */
241 * step_size()
243 * computes the quantization step size of the adaptive quantizer.
246 static int step_size(struct g726_state *state_ptr)
248 int y;
249 int dif;
250 int al;
252 if (state_ptr->ap >= 256)
253 return (state_ptr->yu);
254 else {
255 y = state_ptr->yl >> 6;
256 dif = state_ptr->yu - y;
257 al = state_ptr->ap >> 2;
258 if (dif > 0)
259 y += (dif * al) >> 6;
260 else if (dif < 0)
261 y += (dif * al + 0x3F) >> 6;
262 return (y);
267 * quantize()
269 * Given a raw sample, 'd', of the difference signal and a
270 * quantization step size scale factor, 'y', this routine returns the
271 * ADPCM codeword to which that sample gets quantized. The step
272 * size scale factor division operation is done in the log base 2 domain
273 * as a subtraction.
275 static int quantize(
276 int d, /* Raw difference signal sample */
277 int y, /* Step size multiplier */
278 int *table, /* quantization table */
279 int size) /* table size of integers */
281 int dqm; /* Magnitude of 'd' */
282 int exp; /* Integer part of base 2 log of 'd' */
283 int mant; /* Fractional part of base 2 log */
284 int dl; /* Log of magnitude of 'd' */
285 int dln; /* Step size scale factor normalized log */
286 int i;
289 * LOG
291 * Compute base 2 log of 'd', and store in 'dl'.
293 dqm = abs(d);
294 exp = ilog2(dqm);
295 if (exp < 0)
296 exp = 0;
297 mant = ((dqm << 7) >> exp) & 0x7F; /* Fractional portion. */
298 dl = (exp << 7) | mant;
301 * SUBTB
303 * "Divide" by step size multiplier.
305 dln = dl - (y >> 2);
308 * QUAN
310 * Obtain codword i for 'd'.
312 i = quan(dln, table, size);
313 if (d < 0) /* take 1's complement of i */
314 return ((size << 1) + 1 - i);
315 else if (i == 0) /* take 1's complement of 0 */
316 return ((size << 1) + 1); /* new in 1988 */
317 else
318 return (i);
322 * reconstruct()
324 * Returns reconstructed difference signal 'dq' obtained from
325 * codeword 'i' and quantization step size scale factor 'y'.
326 * Multiplication is performed in log base 2 domain as addition.
328 static int reconstruct(
329 int sign, /* 0 for non-negative value */
330 int dqln, /* G.72x codeword */
331 int y) /* Step size multiplier */
333 int dql; /* Log of 'dq' magnitude */
334 int dex; /* Integer part of log */
335 int dqt;
336 int dq; /* Reconstructed difference signal sample */
338 dql = dqln + (y >> 2); /* ADDA */
340 if (dql < 0) {
341 #ifdef NOT_BLI
342 return (sign) ? -1 : 1;
343 #else
344 return (sign) ? -0x8000 : 0;
345 #endif
346 } else { /* ANTILOG */
347 dex = (dql >> 7) & 15;
348 dqt = 128 + (dql & 127);
349 #ifdef NOT_BLI
350 dq = ((dqt << 19) >> (14 - dex));
351 return (sign) ? -dq : dq;
352 #else
353 dq = (dqt << 7) >> (14 - dex);
354 return (sign) ? (dq - 0x8000) : dq;
355 #endif
360 * update()
362 * updates the state variables for each output code
364 static void update(
365 int code_size, /* distinguish 723_40 with others */
366 int y, /* quantizer step size */
367 int wi, /* scale factor multiplier */
368 int fi, /* for long/short term energies */
369 int dq, /* quantized prediction difference */
370 int sr, /* reconstructed signal */
371 int dqsez, /* difference from 2-pole predictor */
372 struct g726_state *state_ptr) /* coder state pointer */
374 int cnt;
375 int mag; /* Adaptive predictor, FLOAT A */
376 #ifndef NOT_BLI
377 int exp;
378 #endif
379 int a2p=0; /* LIMC */
380 int a1ul; /* UPA1 */
381 int pks1; /* UPA2 */
382 int fa1;
383 int tr; /* tone/transition detector */
384 int ylint, thr2, dqthr;
385 int ylfrac, thr1;
386 int pk0;
388 pk0 = (dqsez < 0) ? 1 : 0; /* needed in updating predictor poles */
390 #ifdef NOT_BLI
391 mag = abs(dq / 0x1000); /* prediction difference magnitude */
392 #else
393 mag = dq & 0x7FFF; /* prediction difference magnitude */
394 #endif
395 /* TRANS */
396 ylint = state_ptr->yl >> 15; /* exponent part of yl */
397 ylfrac = (state_ptr->yl >> 10) & 0x1F; /* fractional part of yl */
398 thr1 = (32 + ylfrac) << ylint; /* threshold */
399 thr2 = (ylint > 9) ? 31 << 10 : thr1; /* limit thr2 to 31 << 10 */
400 dqthr = (thr2 + (thr2 >> 1)) >> 1; /* dqthr = 0.75 * thr2 */
401 if (state_ptr->td == 0) /* signal supposed voice */
402 tr = 0;
403 else if (mag <= dqthr) /* supposed data, but small mag */
404 tr = 0; /* treated as voice */
405 else /* signal is data (modem) */
406 tr = 1;
409 * Quantizer scale factor adaptation.
412 /* FUNCTW & FILTD & DELAY */
413 /* update non-steady state step size multiplier */
414 state_ptr->yu = y + ((wi - y) >> 5);
416 /* LIMB */
417 if (state_ptr->yu < 544) /* 544 <= yu <= 5120 */
418 state_ptr->yu = 544;
419 else if (state_ptr->yu > 5120)
420 state_ptr->yu = 5120;
422 /* FILTE & DELAY */
423 /* update steady state step size multiplier */
424 state_ptr->yl += state_ptr->yu + ((-state_ptr->yl) >> 6);
427 * Adaptive predictor coefficients.
429 if (tr == 1) { /* reset a's and b's for modem signal */
430 state_ptr->a[0] = 0;
431 state_ptr->a[1] = 0;
432 state_ptr->b[0] = 0;
433 state_ptr->b[1] = 0;
434 state_ptr->b[2] = 0;
435 state_ptr->b[3] = 0;
436 state_ptr->b[4] = 0;
437 state_ptr->b[5] = 0;
438 } else { /* update a's and b's */
439 pks1 = pk0 ^ state_ptr->pk[0]; /* UPA2 */
441 /* update predictor pole a[1] */
442 a2p = state_ptr->a[1] - (state_ptr->a[1] >> 7);
443 if (dqsez != 0) {
444 fa1 = (pks1) ? state_ptr->a[0] : -state_ptr->a[0];
445 if (fa1 < -8191) /* a2p = function of fa1 */
446 a2p -= 0x100;
447 else if (fa1 > 8191)
448 a2p += 0xFF;
449 else
450 a2p += fa1 >> 5;
452 if (pk0 ^ state_ptr->pk[1])
453 /* LIMC */
454 if (a2p <= -12160)
455 a2p = -12288;
456 else if (a2p >= 12416)
457 a2p = 12288;
458 else
459 a2p -= 0x80;
460 else if (a2p <= -12416)
461 a2p = -12288;
462 else if (a2p >= 12160)
463 a2p = 12288;
464 else
465 a2p += 0x80;
468 /* TRIGB & DELAY */
469 state_ptr->a[1] = a2p;
471 /* UPA1 */
472 /* update predictor pole a[0] */
473 state_ptr->a[0] -= state_ptr->a[0] >> 8;
474 if (dqsez != 0) {
475 if (pks1 == 0)
476 state_ptr->a[0] += 192;
477 else
478 state_ptr->a[0] -= 192;
480 /* LIMD */
481 a1ul = 15360 - a2p;
482 if (state_ptr->a[0] < -a1ul)
483 state_ptr->a[0] = -a1ul;
484 else if (state_ptr->a[0] > a1ul)
485 state_ptr->a[0] = a1ul;
487 /* UPB : update predictor zeros b[6] */
488 for (cnt = 0; cnt < 6; cnt++) {
489 if (code_size == 5) /* for 40Kbps G.723 */
490 state_ptr->b[cnt] -= state_ptr->b[cnt] >> 9;
491 else /* for G.721 and 24Kbps G.723 */
492 state_ptr->b[cnt] -= state_ptr->b[cnt] >> 8;
493 if (mag)
494 { /* XOR */
495 if ((dq ^ state_ptr->dq[cnt]) >= 0)
496 state_ptr->b[cnt] += 128;
497 else
498 state_ptr->b[cnt] -= 128;
503 for (cnt = 5; cnt > 0; cnt--)
504 state_ptr->dq[cnt] = state_ptr->dq[cnt-1];
505 #ifdef NOT_BLI
506 state_ptr->dq[0] = dq;
507 #else
508 /* FLOAT A : convert dq[0] to 4-bit exp, 6-bit mantissa f.p. */
509 if (mag == 0) {
510 state_ptr->dq[0] = (dq >= 0) ? 0x20 : 0x20 - 0x400;
511 } else {
512 exp = ilog2(mag) + 1;
513 state_ptr->dq[0] = (dq >= 0) ?
514 (exp << 6) + ((mag << 6) >> exp) :
515 (exp << 6) + ((mag << 6) >> exp) - 0x400;
517 #endif
519 state_ptr->sr[1] = state_ptr->sr[0];
520 #ifdef NOT_BLI
521 state_ptr->sr[0] = sr;
522 #else
523 /* FLOAT B : convert sr to 4-bit exp., 6-bit mantissa f.p. */
524 if (sr == 0) {
525 state_ptr->sr[0] = 0x20;
526 } else if (sr > 0) {
527 exp = ilog2(sr) + 1;
528 state_ptr->sr[0] = (exp << 6) + ((sr << 6) >> exp);
529 } else if (sr > -0x8000) {
530 mag = -sr;
531 exp = ilog2(mag) + 1;
532 state_ptr->sr[0] = (exp << 6) + ((mag << 6) >> exp) - 0x400;
533 } else
534 state_ptr->sr[0] = 0x20 - 0x400;
535 #endif
537 /* DELAY A */
538 state_ptr->pk[1] = state_ptr->pk[0];
539 state_ptr->pk[0] = pk0;
541 /* TONE */
542 if (tr == 1) /* this sample has been treated as data */
543 state_ptr->td = 0; /* next one will be treated as voice */
544 else if (a2p < -11776) /* small sample-to-sample correlation */
545 state_ptr->td = 1; /* signal may be data */
546 else /* signal is voice */
547 state_ptr->td = 0;
550 * Adaptation speed control.
552 state_ptr->dms += (fi - state_ptr->dms) >> 5; /* FILTA */
553 state_ptr->dml += (((fi << 2) - state_ptr->dml) >> 7); /* FILTB */
555 if (tr == 1)
556 state_ptr->ap = 256;
557 else if (y < 1536) /* SUBTC */
558 state_ptr->ap += (0x200 - state_ptr->ap) >> 4;
559 else if (state_ptr->td == 1)
560 state_ptr->ap += (0x200 - state_ptr->ap) >> 4;
561 else if (abs((state_ptr->dms << 2) - state_ptr->dml) >=
562 (state_ptr->dml >> 3))
563 state_ptr->ap += (0x200 - state_ptr->ap) >> 4;
564 else
565 state_ptr->ap += (-state_ptr->ap) >> 4;
569 * g726_decode()
571 * Description:
573 * Decodes a 4-bit code of G.726-32 encoded data of i and
574 * returns the resulting linear PCM, A-law or u-law value.
575 * return -1 for unknown out_coding value.
577 static int g726_decode(int i, struct g726_state *state_ptr)
579 int sezi, sez, se; /* ACCUM */
580 int y; /* MIX */
581 int sr; /* ADDB */
582 int dq;
583 int dqsez;
585 i &= 0x0f; /* mask to get proper bits */
586 #ifdef NOT_BLI
587 sezi = predictor_zero(state_ptr);
588 sez = sezi;
589 se = sezi + predictor_pole(state_ptr); /* estimated signal */
590 #else
591 sezi = predictor_zero(state_ptr);
592 sez = sezi >> 1;
593 se = (sezi + predictor_pole(state_ptr)) >> 1; /* estimated signal */
594 #endif
596 y = step_size(state_ptr); /* dynamic quantizer step size */
598 dq = reconstruct(i & 8, _dqlntab[i], y); /* quantized diff. */
600 #ifdef NOT_BLI
601 sr = se + dq; /* reconst. signal */
602 dqsez = dq + sez; /* pole prediction diff. */
603 #else
604 sr = (dq < 0) ? se - (dq & 0x3FFF) : se + dq; /* reconst. signal */
605 dqsez = sr - se + sez; /* pole prediction diff. */
606 #endif
608 update(4, y, _witab[i] << 5, _fitab[i], dq, sr, dqsez, state_ptr);
610 #ifdef NOT_BLI
611 return (sr >> 10); /* sr was 26-bit dynamic range */
612 #else
613 return (sr << 2); /* sr was 14-bit dynamic range */
614 #endif
618 * g726_encode()
620 * Encodes the input vale of linear PCM, A-law or u-law data sl and returns
621 * the resulting code. -1 is returned for unknown input coding value.
623 static int g726_encode(int sl, struct g726_state *state_ptr)
625 int sezi, se, sez; /* ACCUM */
626 int d; /* SUBTA */
627 int sr; /* ADDB */
628 int y; /* MIX */
629 int dqsez; /* ADDC */
630 int dq, i;
632 #ifdef NOT_BLI
633 sl <<= 10; /* 26-bit dynamic range */
635 sezi = predictor_zero(state_ptr);
636 sez = sezi;
637 se = sezi + predictor_pole(state_ptr); /* estimated signal */
638 #else
639 sl >>= 2; /* 14-bit dynamic range */
641 sezi = predictor_zero(state_ptr);
642 sez = sezi >> 1;
643 se = (sezi + predictor_pole(state_ptr)) >> 1; /* estimated signal */
644 #endif
646 d = sl - se; /* estimation difference */
648 /* quantize the prediction difference */
649 y = step_size(state_ptr); /* quantizer step size */
650 #ifdef NOT_BLI
651 d /= 0x1000;
652 #endif
653 i = quantize(d, y, qtab_721, 7); /* i = G726 code */
655 dq = reconstruct(i & 8, _dqlntab[i], y); /* quantized est diff */
657 #ifdef NOT_BLI
658 sr = se + dq; /* reconst. signal */
659 dqsez = dq + sez; /* pole prediction diff. */
660 #else
661 sr = (dq < 0) ? se - (dq & 0x3FFF) : se + dq; /* reconst. signal */
662 dqsez = sr - se + sez; /* pole prediction diff. */
663 #endif
665 update(4, y, _witab[i] << 5, _fitab[i], dq, sr, dqsez, state_ptr);
667 return (i);
671 * Private workspace for translating signed linear signals to G726.
672 * Don't bother to define two distinct structs.
675 struct g726_coder_pvt {
676 /* buffer any odd byte in input - 0x80 + (value & 0xf) if present */
677 unsigned char next_flag;
678 struct g726_state g726;
681 /*! \brief init a new instance of g726_coder_pvt. */
682 static int lintog726_new(struct ast_trans_pvt *pvt)
684 struct g726_coder_pvt *tmp = pvt->pvt;
686 g726_init_state(&tmp->g726);
688 return 0;
691 /*! \brief decode packed 4-bit G726 values (AAL2 packing) and store in buffer. */
692 static int g726aal2tolin_framein (struct ast_trans_pvt *pvt, struct ast_frame *f)
694 struct g726_coder_pvt *tmp = pvt->pvt;
695 unsigned char *src = f->data.ptr;
696 int16_t *dst = (int16_t *) pvt->outbuf + pvt->samples;
697 unsigned int i;
699 for (i = 0; i < f->datalen; i++) {
700 *dst++ = g726_decode((src[i] >> 4) & 0xf, &tmp->g726);
701 *dst++ = g726_decode(src[i] & 0x0f, &tmp->g726);
704 pvt->samples += f->samples;
705 pvt->datalen += 2 * f->samples; /* 2 bytes/sample */
707 return 0;
710 /*! \brief compress and store data (4-bit G726 samples, AAL2 packing) in outbuf */
711 static int lintog726aal2_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
713 struct g726_coder_pvt *tmp = pvt->pvt;
714 int16_t *src = f->data.ptr;
715 unsigned int i;
717 for (i = 0; i < f->samples; i++) {
718 unsigned char d = g726_encode(src[i], &tmp->g726); /* this sample */
720 if (tmp->next_flag & 0x80) { /* merge with leftover sample */
721 pvt->outbuf[pvt->datalen++] = ((tmp->next_flag & 0xf)<< 4) | d;
722 pvt->samples += 2; /* 2 samples per byte */
723 tmp->next_flag = 0;
724 } else {
725 tmp->next_flag = 0x80 | d;
729 return 0;
732 /*! \brief decode packed 4-bit G726 values (RFC3551 packing) and store in buffer. */
733 static int g726tolin_framein (struct ast_trans_pvt *pvt, struct ast_frame *f)
735 struct g726_coder_pvt *tmp = pvt->pvt;
736 unsigned char *src = f->data.ptr;
737 int16_t *dst = (int16_t *) pvt->outbuf + pvt->samples;
738 unsigned int i;
740 for (i = 0; i < f->datalen; i++) {
741 *dst++ = g726_decode(src[i] & 0x0f, &tmp->g726);
742 *dst++ = g726_decode((src[i] >> 4) & 0xf, &tmp->g726);
745 pvt->samples += f->samples;
746 pvt->datalen += 2 * f->samples; /* 2 bytes/sample */
748 return 0;
751 /*! \brief compress and store data (4-bit G726 samples, RFC3551 packing) in outbuf */
752 static int lintog726_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
754 struct g726_coder_pvt *tmp = pvt->pvt;
755 int16_t *src = f->data.ptr;
756 unsigned int i;
758 for (i = 0; i < f->samples; i++) {
759 unsigned char d = g726_encode(src[i], &tmp->g726); /* this sample */
761 if (tmp->next_flag & 0x80) { /* merge with leftover sample */
762 pvt->outbuf[pvt->datalen++] = (d << 4) | (tmp->next_flag & 0xf);
763 pvt->samples += 2; /* 2 samples per byte */
764 tmp->next_flag = 0;
765 } else {
766 tmp->next_flag = 0x80 | d;
770 return 0;
773 /*! \brief convert G726-32 RFC3551 packed data into AAL2 packed data (or vice-versa) */
774 static int g726tog726aal2_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
776 unsigned char *src = f->data.ptr;
777 unsigned char *dst = (unsigned char *) pvt->outbuf + pvt->samples;
778 unsigned int i;
780 for (i = 0; i < f->datalen; i++)
781 *dst++ = ((src[i] & 0xf) << 4) | (src[i] >> 4);
783 pvt->samples += f->samples;
784 pvt->datalen += f->samples; /* 1 byte/sample */
786 return 0;
789 static struct ast_frame *g726tolin_sample(void)
791 static struct ast_frame f = {
792 .frametype = AST_FRAME_VOICE,
793 .subclass = AST_FORMAT_G726,
794 .datalen = sizeof(g726_slin_ex),
795 .samples = sizeof(g726_slin_ex) * 2, /* 2 samples per byte */
796 .src = __PRETTY_FUNCTION__,
797 .data.ptr = g726_slin_ex,
800 return &f;
803 static struct ast_frame *lintog726_sample (void)
805 static struct ast_frame f = {
806 .frametype = AST_FRAME_VOICE,
807 .subclass = AST_FORMAT_SLINEAR,
808 .datalen = sizeof(slin_g726_ex),
809 .samples = sizeof(slin_g726_ex) / 2, /* 1 sample per 2 bytes */
810 .src = __PRETTY_FUNCTION__,
811 .data.ptr = slin_g726_ex,
814 return &f;
817 static struct ast_translator g726tolin = {
818 .name = "g726tolin",
819 .srcfmt = AST_FORMAT_G726,
820 .dstfmt = AST_FORMAT_SLINEAR,
821 .newpvt = lintog726_new, /* same for both directions */
822 .framein = g726tolin_framein,
823 .sample = g726tolin_sample,
824 .desc_size = sizeof(struct g726_coder_pvt),
825 .buffer_samples = BUFFER_SAMPLES,
826 .buf_size = BUFFER_SAMPLES * 2,
827 .plc_samples = 160,
830 static struct ast_translator lintog726 = {
831 .name = "lintog726",
832 .srcfmt = AST_FORMAT_SLINEAR,
833 .dstfmt = AST_FORMAT_G726,
834 .newpvt = lintog726_new, /* same for both directions */
835 .framein = lintog726_framein,
836 .sample = lintog726_sample,
837 .desc_size = sizeof(struct g726_coder_pvt),
838 .buffer_samples = BUFFER_SAMPLES,
839 .buf_size = BUFFER_SAMPLES/2,
842 static struct ast_translator g726aal2tolin = {
843 .name = "g726aal2tolin",
844 .srcfmt = AST_FORMAT_G726_AAL2,
845 .dstfmt = AST_FORMAT_SLINEAR,
846 .newpvt = lintog726_new, /* same for both directions */
847 .framein = g726aal2tolin_framein,
848 .sample = g726tolin_sample,
849 .desc_size = sizeof(struct g726_coder_pvt),
850 .buffer_samples = BUFFER_SAMPLES,
851 .buf_size = BUFFER_SAMPLES * 2,
852 .plc_samples = 160,
855 static struct ast_translator lintog726aal2 = {
856 .name = "lintog726aal2",
857 .srcfmt = AST_FORMAT_SLINEAR,
858 .dstfmt = AST_FORMAT_G726_AAL2,
859 .newpvt = lintog726_new, /* same for both directions */
860 .framein = lintog726aal2_framein,
861 .sample = lintog726_sample,
862 .desc_size = sizeof(struct g726_coder_pvt),
863 .buffer_samples = BUFFER_SAMPLES,
864 .buf_size = BUFFER_SAMPLES / 2,
867 static struct ast_translator g726tog726aal2 = {
868 .name = "g726tog726aal2",
869 .srcfmt = AST_FORMAT_G726,
870 .dstfmt = AST_FORMAT_G726_AAL2,
871 .framein = g726tog726aal2_framein, /* same for both directions */
872 .sample = lintog726_sample,
873 .buffer_samples = BUFFER_SAMPLES,
874 .buf_size = BUFFER_SAMPLES,
877 static struct ast_translator g726aal2tog726 = {
878 .name = "g726aal2tog726",
879 .srcfmt = AST_FORMAT_G726_AAL2,
880 .dstfmt = AST_FORMAT_G726,
881 .framein = g726tog726aal2_framein, /* same for both directions */
882 .sample = lintog726_sample,
883 .buffer_samples = BUFFER_SAMPLES,
884 .buf_size = BUFFER_SAMPLES,
887 static int parse_config(int reload)
889 struct ast_variable *var;
890 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
891 struct ast_config *cfg = ast_config_load("codecs.conf", config_flags);
893 if (cfg == NULL)
894 return 0;
895 if (cfg == CONFIG_STATUS_FILEUNCHANGED)
896 return 0;
897 for (var = ast_variable_browse(cfg, "plc"); var; var = var->next) {
898 if (!strcasecmp(var->name, "genericplc")) {
899 g726tolin.useplc = ast_true(var->value) ? 1 : 0;
900 ast_verb(3, "codec_g726: %susing generic PLC\n",
901 g726tolin.useplc ? "" : "not ");
904 ast_config_destroy(cfg);
905 return 0;
908 static int reload(void)
910 if (parse_config(1))
911 return AST_MODULE_LOAD_DECLINE;
912 return AST_MODULE_LOAD_SUCCESS;
915 static int unload_module(void)
917 int res = 0;
919 res |= ast_unregister_translator(&g726tolin);
920 res |= ast_unregister_translator(&lintog726);
922 res |= ast_unregister_translator(&g726aal2tolin);
923 res |= ast_unregister_translator(&lintog726aal2);
925 res |= ast_unregister_translator(&g726aal2tog726);
926 res |= ast_unregister_translator(&g726tog726aal2);
928 return res;
931 static int load_module(void)
933 int res = 0;
936 if (parse_config(0))
937 return AST_MODULE_LOAD_DECLINE;
939 res |= ast_register_translator(&g726tolin);
940 res |= ast_register_translator(&lintog726);
942 res |= ast_register_translator(&g726aal2tolin);
943 res |= ast_register_translator(&lintog726aal2);
945 res |= ast_register_translator(&g726aal2tog726);
946 res |= ast_register_translator(&g726tog726aal2);
948 if (res) {
949 unload_module();
950 return AST_MODULE_LOAD_FAILURE;
953 return AST_MODULE_LOAD_SUCCESS;
956 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "ITU G.726-32kbps G726 Transcoder",
957 .load = load_module,
958 .unload = unload_module,
959 .reload = reload,