Let's also include aclocal.m4
[asterisk-bristuff.git] / codecs / codec_g726.c
blobbfc49ad9c8e3cb592a0b44132299ecafff7057ae
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 <fcntl.h>
35 #include <netinet/in.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
41 #include "asterisk/lock.h"
42 #include "asterisk/logger.h"
43 #include "asterisk/linkedlists.h"
44 #include "asterisk/module.h"
45 #include "asterisk/config.h"
46 #include "asterisk/options.h"
47 #include "asterisk/translate.h"
48 #include "asterisk/channel.h"
49 #include "asterisk/utils.h"
51 #define WANT_ASM
52 #include "log2comp.h"
54 /* define NOT_BLI to use a faster but not bit-level identical version */
55 /* #define NOT_BLI */
57 #if defined(NOT_BLI)
58 # if defined(_MSC_VER)
59 typedef __int64 sint64;
60 # elif defined(__GNUC__)
61 typedef long long sint64;
62 # else
63 # error 64-bit integer type is not defined for your compiler/platform
64 # endif
65 #endif
67 #define BUFFER_SAMPLES 8096 /* size for the translation buffers */
68 #define BUF_SHIFT 5
70 /* Sample frame data */
72 #include "slin_g726_ex.h"
73 #include "g726_slin_ex.h"
76 * The following is the definition of the state structure
77 * used by the G.726 encoder and decoder to preserve their internal
78 * state between successive calls. The meanings of the majority
79 * of the state structure fields are explained in detail in the
80 * CCITT Recommendation G.721. The field names are essentially identical
81 * to variable names in the bit level description of the coding algorithm
82 * included in this Recommendation.
84 struct g726_state {
85 long yl; /* Locked or steady state step size multiplier. */
86 int yu; /* Unlocked or non-steady state step size multiplier. */
87 int dms; /* Short term energy estimate. */
88 int dml; /* Long term energy estimate. */
89 int ap; /* Linear weighting coefficient of 'yl' and 'yu'. */
90 int a[2]; /* Coefficients of pole portion of prediction filter.
91 * stored as fixed-point 1==2^14 */
92 int b[6]; /* Coefficients of zero portion of prediction filter.
93 * stored as fixed-point 1==2^14 */
94 int pk[2]; /* Signs of previous two samples of a partially
95 * reconstructed signal. */
96 int dq[6]; /* Previous 6 samples of the quantized difference signal
97 * stored as fixed point 1==2^12,
98 * or in internal floating point format */
99 int sr[2]; /* Previous 2 samples of the quantized difference signal
100 * stored as fixed point 1==2^12,
101 * or in internal floating point format */
102 int td; /* delayed tone detect, new in 1988 version */
105 static int qtab_721[7] = {-124, 80, 178, 246, 300, 349, 400};
107 * Maps G.721 code word to reconstructed scale factor normalized log
108 * magnitude values.
110 static int _dqlntab[16] = {-2048, 4, 135, 213, 273, 323, 373, 425,
111 425, 373, 323, 273, 213, 135, 4, -2048};
113 /* Maps G.721 code word to log of scale factor multiplier. */
114 static int _witab[16] = {-12, 18, 41, 64, 112, 198, 355, 1122,
115 1122, 355, 198, 112, 64, 41, 18, -12};
117 * Maps G.721 code words to a set of values whose long and short
118 * term averages are computed and then compared to give an indication
119 * how stationary (steady state) the signal is.
121 static int _fitab[16] = {0, 0, 0, 0x200, 0x200, 0x200, 0x600, 0xE00,
122 0xE00, 0x600, 0x200, 0x200, 0x200, 0, 0, 0};
126 * g72x_init_state()
128 * This routine initializes and/or resets the g726_state structure
129 * pointed to by 'state_ptr'.
130 * All the initial state values are specified in the CCITT G.721 document.
132 static void g726_init_state(struct g726_state *state_ptr)
134 int cnta;
136 state_ptr->yl = 34816;
137 state_ptr->yu = 544;
138 state_ptr->dms = 0;
139 state_ptr->dml = 0;
140 state_ptr->ap = 0;
141 for (cnta = 0; cnta < 2; cnta++) {
142 state_ptr->a[cnta] = 0;
143 state_ptr->pk[cnta] = 0;
144 #ifdef NOT_BLI
145 state_ptr->sr[cnta] = 1;
146 #else
147 state_ptr->sr[cnta] = 32;
148 #endif
150 for (cnta = 0; cnta < 6; cnta++) {
151 state_ptr->b[cnta] = 0;
152 #ifdef NOT_BLI
153 state_ptr->dq[cnta] = 1;
154 #else
155 state_ptr->dq[cnta] = 32;
156 #endif
158 state_ptr->td = 0;
162 * quan()
164 * quantizes the input val against the table of integers.
165 * It returns i if table[i - 1] <= val < table[i].
167 * Using linear search for simple coding.
169 static int quan(int val, int *table, int size)
171 int i;
173 for (i = 0; i < size && val >= *table; ++i, ++table)
175 return (i);
178 #ifdef NOT_BLI /* faster non-identical version */
181 * predictor_zero()
183 * computes the estimated signal from 6-zero predictor.
186 static int predictor_zero(struct g726_state *state_ptr)
187 { /* divide by 2 is necessary here to handle negative numbers correctly */
188 int i;
189 sint64 sezi;
190 for (sezi = 0, i = 0; i < 6; i++) /* ACCUM */
191 sezi += (sint64)state_ptr->b[i] * state_ptr->dq[i];
192 return (int)(sezi >> 13) / 2 /* 2^14 */;
196 * predictor_pole()
198 * computes the estimated signal from 2-pole predictor.
201 static int predictor_pole(struct g726_state *state_ptr)
202 { /* divide by 2 is necessary here to handle negative numbers correctly */
203 return (int)(((sint64)state_ptr->a[1] * state_ptr->sr[1] +
204 (sint64)state_ptr->a[0] * state_ptr->sr[0]) >> 13) / 2 /* 2^14 */;
207 #else /* NOT_BLI - identical version */
209 * fmult()
211 * returns the integer product of the fixed-point number "an" (1==2^12) and
212 * "floating point" representation (4-bit exponent, 6-bit mantessa) "srn".
214 static int fmult(int an, int srn)
216 int anmag, anexp, anmant;
217 int wanexp, wanmant;
218 int retval;
220 anmag = (an > 0) ? an : ((-an) & 0x1FFF);
221 anexp = ilog2(anmag) - 5;
222 anmant = (anmag == 0) ? 32 :
223 (anexp >= 0) ? anmag >> anexp : anmag << -anexp;
224 wanexp = anexp + ((srn >> 6) & 0xF) - 13;
226 wanmant = (anmant * (srn & 077) + 0x30) >> 4;
227 retval = (wanexp >= 0) ? ((wanmant << wanexp) & 0x7FFF) :
228 (wanmant >> -wanexp);
230 return (((an ^ srn) < 0) ? -retval : retval);
233 static int predictor_zero(struct g726_state *state_ptr)
235 int i;
236 int sezi;
237 for (sezi = 0, i = 0; i < 6; i++) /* ACCUM */
238 sezi += fmult(state_ptr->b[i] >> 2, state_ptr->dq[i]);
239 return sezi;
242 static int predictor_pole(struct g726_state *state_ptr)
244 return (fmult(state_ptr->a[1] >> 2, state_ptr->sr[1]) +
245 fmult(state_ptr->a[0] >> 2, state_ptr->sr[0]));
248 #endif /* NOT_BLI */
251 * step_size()
253 * computes the quantization step size of the adaptive quantizer.
256 static int step_size(struct g726_state *state_ptr)
258 int y;
259 int dif;
260 int al;
262 if (state_ptr->ap >= 256)
263 return (state_ptr->yu);
264 else {
265 y = state_ptr->yl >> 6;
266 dif = state_ptr->yu - y;
267 al = state_ptr->ap >> 2;
268 if (dif > 0)
269 y += (dif * al) >> 6;
270 else if (dif < 0)
271 y += (dif * al + 0x3F) >> 6;
272 return (y);
277 * quantize()
279 * Given a raw sample, 'd', of the difference signal and a
280 * quantization step size scale factor, 'y', this routine returns the
281 * ADPCM codeword to which that sample gets quantized. The step
282 * size scale factor division operation is done in the log base 2 domain
283 * as a subtraction.
285 static int quantize(
286 int d, /* Raw difference signal sample */
287 int y, /* Step size multiplier */
288 int *table, /* quantization table */
289 int size) /* table size of integers */
291 int dqm; /* Magnitude of 'd' */
292 int exp; /* Integer part of base 2 log of 'd' */
293 int mant; /* Fractional part of base 2 log */
294 int dl; /* Log of magnitude of 'd' */
295 int dln; /* Step size scale factor normalized log */
296 int i;
299 * LOG
301 * Compute base 2 log of 'd', and store in 'dl'.
303 dqm = abs(d);
304 exp = ilog2(dqm);
305 if (exp < 0)
306 exp = 0;
307 mant = ((dqm << 7) >> exp) & 0x7F; /* Fractional portion. */
308 dl = (exp << 7) | mant;
311 * SUBTB
313 * "Divide" by step size multiplier.
315 dln = dl - (y >> 2);
318 * QUAN
320 * Obtain codword i for 'd'.
322 i = quan(dln, table, size);
323 if (d < 0) /* take 1's complement of i */
324 return ((size << 1) + 1 - i);
325 else if (i == 0) /* take 1's complement of 0 */
326 return ((size << 1) + 1); /* new in 1988 */
327 else
328 return (i);
332 * reconstruct()
334 * Returns reconstructed difference signal 'dq' obtained from
335 * codeword 'i' and quantization step size scale factor 'y'.
336 * Multiplication is performed in log base 2 domain as addition.
338 static int reconstruct(
339 int sign, /* 0 for non-negative value */
340 int dqln, /* G.72x codeword */
341 int y) /* Step size multiplier */
343 int dql; /* Log of 'dq' magnitude */
344 int dex; /* Integer part of log */
345 int dqt;
346 int dq; /* Reconstructed difference signal sample */
348 dql = dqln + (y >> 2); /* ADDA */
350 if (dql < 0) {
351 #ifdef NOT_BLI
352 return (sign) ? -1 : 1;
353 #else
354 return (sign) ? -0x8000 : 0;
355 #endif
356 } else { /* ANTILOG */
357 dex = (dql >> 7) & 15;
358 dqt = 128 + (dql & 127);
359 #ifdef NOT_BLI
360 dq = ((dqt << 19) >> (14 - dex));
361 return (sign) ? -dq : dq;
362 #else
363 dq = (dqt << 7) >> (14 - dex);
364 return (sign) ? (dq - 0x8000) : dq;
365 #endif
370 * update()
372 * updates the state variables for each output code
374 static void update(
375 int code_size, /* distinguish 723_40 with others */
376 int y, /* quantizer step size */
377 int wi, /* scale factor multiplier */
378 int fi, /* for long/short term energies */
379 int dq, /* quantized prediction difference */
380 int sr, /* reconstructed signal */
381 int dqsez, /* difference from 2-pole predictor */
382 struct g726_state *state_ptr) /* coder state pointer */
384 int cnt;
385 int mag; /* Adaptive predictor, FLOAT A */
386 #ifndef NOT_BLI
387 int exp;
388 #endif
389 int a2p=0; /* LIMC */
390 int a1ul; /* UPA1 */
391 int pks1; /* UPA2 */
392 int fa1;
393 int tr; /* tone/transition detector */
394 int ylint, thr2, dqthr;
395 int ylfrac, thr1;
396 int pk0;
398 pk0 = (dqsez < 0) ? 1 : 0; /* needed in updating predictor poles */
400 #ifdef NOT_BLI
401 mag = abs(dq / 0x1000); /* prediction difference magnitude */
402 #else
403 mag = dq & 0x7FFF; /* prediction difference magnitude */
404 #endif
405 /* TRANS */
406 ylint = state_ptr->yl >> 15; /* exponent part of yl */
407 ylfrac = (state_ptr->yl >> 10) & 0x1F; /* fractional part of yl */
408 thr1 = (32 + ylfrac) << ylint; /* threshold */
409 thr2 = (ylint > 9) ? 31 << 10 : thr1; /* limit thr2 to 31 << 10 */
410 dqthr = (thr2 + (thr2 >> 1)) >> 1; /* dqthr = 0.75 * thr2 */
411 if (state_ptr->td == 0) /* signal supposed voice */
412 tr = 0;
413 else if (mag <= dqthr) /* supposed data, but small mag */
414 tr = 0; /* treated as voice */
415 else /* signal is data (modem) */
416 tr = 1;
419 * Quantizer scale factor adaptation.
422 /* FUNCTW & FILTD & DELAY */
423 /* update non-steady state step size multiplier */
424 state_ptr->yu = y + ((wi - y) >> 5);
426 /* LIMB */
427 if (state_ptr->yu < 544) /* 544 <= yu <= 5120 */
428 state_ptr->yu = 544;
429 else if (state_ptr->yu > 5120)
430 state_ptr->yu = 5120;
432 /* FILTE & DELAY */
433 /* update steady state step size multiplier */
434 state_ptr->yl += state_ptr->yu + ((-state_ptr->yl) >> 6);
437 * Adaptive predictor coefficients.
439 if (tr == 1) { /* reset a's and b's for modem signal */
440 state_ptr->a[0] = 0;
441 state_ptr->a[1] = 0;
442 state_ptr->b[0] = 0;
443 state_ptr->b[1] = 0;
444 state_ptr->b[2] = 0;
445 state_ptr->b[3] = 0;
446 state_ptr->b[4] = 0;
447 state_ptr->b[5] = 0;
448 } else { /* update a's and b's */
449 pks1 = pk0 ^ state_ptr->pk[0]; /* UPA2 */
451 /* update predictor pole a[1] */
452 a2p = state_ptr->a[1] - (state_ptr->a[1] >> 7);
453 if (dqsez != 0) {
454 fa1 = (pks1) ? state_ptr->a[0] : -state_ptr->a[0];
455 if (fa1 < -8191) /* a2p = function of fa1 */
456 a2p -= 0x100;
457 else if (fa1 > 8191)
458 a2p += 0xFF;
459 else
460 a2p += fa1 >> 5;
462 if (pk0 ^ state_ptr->pk[1])
463 /* LIMC */
464 if (a2p <= -12160)
465 a2p = -12288;
466 else if (a2p >= 12416)
467 a2p = 12288;
468 else
469 a2p -= 0x80;
470 else if (a2p <= -12416)
471 a2p = -12288;
472 else if (a2p >= 12160)
473 a2p = 12288;
474 else
475 a2p += 0x80;
478 /* TRIGB & DELAY */
479 state_ptr->a[1] = a2p;
481 /* UPA1 */
482 /* update predictor pole a[0] */
483 state_ptr->a[0] -= state_ptr->a[0] >> 8;
484 if (dqsez != 0) {
485 if (pks1 == 0)
486 state_ptr->a[0] += 192;
487 else
488 state_ptr->a[0] -= 192;
490 /* LIMD */
491 a1ul = 15360 - a2p;
492 if (state_ptr->a[0] < -a1ul)
493 state_ptr->a[0] = -a1ul;
494 else if (state_ptr->a[0] > a1ul)
495 state_ptr->a[0] = a1ul;
497 /* UPB : update predictor zeros b[6] */
498 for (cnt = 0; cnt < 6; cnt++) {
499 if (code_size == 5) /* for 40Kbps G.723 */
500 state_ptr->b[cnt] -= state_ptr->b[cnt] >> 9;
501 else /* for G.721 and 24Kbps G.723 */
502 state_ptr->b[cnt] -= state_ptr->b[cnt] >> 8;
503 if (mag)
504 { /* XOR */
505 if ((dq ^ state_ptr->dq[cnt]) >= 0)
506 state_ptr->b[cnt] += 128;
507 else
508 state_ptr->b[cnt] -= 128;
513 for (cnt = 5; cnt > 0; cnt--)
514 state_ptr->dq[cnt] = state_ptr->dq[cnt-1];
515 #ifdef NOT_BLI
516 state_ptr->dq[0] = dq;
517 #else
518 /* FLOAT A : convert dq[0] to 4-bit exp, 6-bit mantissa f.p. */
519 if (mag == 0) {
520 state_ptr->dq[0] = (dq >= 0) ? 0x20 : 0x20 - 0x400;
521 } else {
522 exp = ilog2(mag) + 1;
523 state_ptr->dq[0] = (dq >= 0) ?
524 (exp << 6) + ((mag << 6) >> exp) :
525 (exp << 6) + ((mag << 6) >> exp) - 0x400;
527 #endif
529 state_ptr->sr[1] = state_ptr->sr[0];
530 #ifdef NOT_BLI
531 state_ptr->sr[0] = sr;
532 #else
533 /* FLOAT B : convert sr to 4-bit exp., 6-bit mantissa f.p. */
534 if (sr == 0) {
535 state_ptr->sr[0] = 0x20;
536 } else if (sr > 0) {
537 exp = ilog2(sr) + 1;
538 state_ptr->sr[0] = (exp << 6) + ((sr << 6) >> exp);
539 } else if (sr > -0x8000) {
540 mag = -sr;
541 exp = ilog2(mag) + 1;
542 state_ptr->sr[0] = (exp << 6) + ((mag << 6) >> exp) - 0x400;
543 } else
544 state_ptr->sr[0] = 0x20 - 0x400;
545 #endif
547 /* DELAY A */
548 state_ptr->pk[1] = state_ptr->pk[0];
549 state_ptr->pk[0] = pk0;
551 /* TONE */
552 if (tr == 1) /* this sample has been treated as data */
553 state_ptr->td = 0; /* next one will be treated as voice */
554 else if (a2p < -11776) /* small sample-to-sample correlation */
555 state_ptr->td = 1; /* signal may be data */
556 else /* signal is voice */
557 state_ptr->td = 0;
560 * Adaptation speed control.
562 state_ptr->dms += (fi - state_ptr->dms) >> 5; /* FILTA */
563 state_ptr->dml += (((fi << 2) - state_ptr->dml) >> 7); /* FILTB */
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;
579 * g726_decode()
581 * Description:
583 * Decodes a 4-bit code of G.726-32 encoded data of i and
584 * returns the resulting linear PCM, A-law or u-law value.
585 * return -1 for unknown out_coding value.
587 static int g726_decode(int i, struct g726_state *state_ptr)
589 int sezi, sez, se; /* ACCUM */
590 int y; /* MIX */
591 int sr; /* ADDB */
592 int dq;
593 int dqsez;
595 i &= 0x0f; /* mask to get proper bits */
596 #ifdef NOT_BLI
597 sezi = predictor_zero(state_ptr);
598 sez = sezi;
599 se = sezi + predictor_pole(state_ptr); /* estimated signal */
600 #else
601 sezi = predictor_zero(state_ptr);
602 sez = sezi >> 1;
603 se = (sezi + predictor_pole(state_ptr)) >> 1; /* estimated signal */
604 #endif
606 y = step_size(state_ptr); /* dynamic quantizer step size */
608 dq = reconstruct(i & 8, _dqlntab[i], y); /* quantized diff. */
610 #ifdef NOT_BLI
611 sr = se + dq; /* reconst. signal */
612 dqsez = dq + sez; /* pole prediction diff. */
613 #else
614 sr = (dq < 0) ? se - (dq & 0x3FFF) : se + dq; /* reconst. signal */
615 dqsez = sr - se + sez; /* pole prediction diff. */
616 #endif
618 update(4, y, _witab[i] << 5, _fitab[i], dq, sr, dqsez, state_ptr);
620 #ifdef NOT_BLI
621 return (sr >> 10); /* sr was 26-bit dynamic range */
622 #else
623 return (sr << 2); /* sr was 14-bit dynamic range */
624 #endif
628 * g726_encode()
630 * Encodes the input vale of linear PCM, A-law or u-law data sl and returns
631 * the resulting code. -1 is returned for unknown input coding value.
633 static int g726_encode(int sl, struct g726_state *state_ptr)
635 int sezi, se, sez; /* ACCUM */
636 int d; /* SUBTA */
637 int sr; /* ADDB */
638 int y; /* MIX */
639 int dqsez; /* ADDC */
640 int dq, i;
642 #ifdef NOT_BLI
643 sl <<= 10; /* 26-bit dynamic range */
645 sezi = predictor_zero(state_ptr);
646 sez = sezi;
647 se = sezi + predictor_pole(state_ptr); /* estimated signal */
648 #else
649 sl >>= 2; /* 14-bit dynamic range */
651 sezi = predictor_zero(state_ptr);
652 sez = sezi >> 1;
653 se = (sezi + predictor_pole(state_ptr)) >> 1; /* estimated signal */
654 #endif
656 d = sl - se; /* estimation difference */
658 /* quantize the prediction difference */
659 y = step_size(state_ptr); /* quantizer step size */
660 #ifdef NOT_BLI
661 d /= 0x1000;
662 #endif
663 i = quantize(d, y, qtab_721, 7); /* i = G726 code */
665 dq = reconstruct(i & 8, _dqlntab[i], y); /* quantized est diff */
667 #ifdef NOT_BLI
668 sr = se + dq; /* reconst. signal */
669 dqsez = dq + sez; /* pole prediction diff. */
670 #else
671 sr = (dq < 0) ? se - (dq & 0x3FFF) : se + dq; /* reconst. signal */
672 dqsez = sr - se + sez; /* pole prediction diff. */
673 #endif
675 update(4, y, _witab[i] << 5, _fitab[i], dq, sr, dqsez, state_ptr);
677 return (i);
681 * Private workspace for translating signed linear signals to G726.
682 * Don't bother to define two distinct structs.
685 struct g726_coder_pvt {
686 /* buffer any odd byte in input - 0x80 + (value & 0xf) if present */
687 unsigned char next_flag;
688 struct g726_state g726;
691 /*! \brief init a new instance of g726_coder_pvt. */
692 static int lintog726_new(struct ast_trans_pvt *pvt)
694 struct g726_coder_pvt *tmp = pvt->pvt;
696 g726_init_state(&tmp->g726);
698 return 0;
701 /*! \brief decode packed 4-bit G726 values (AAL2 packing) and store in buffer. */
702 static int g726aal2tolin_framein (struct ast_trans_pvt *pvt, struct ast_frame *f)
704 struct g726_coder_pvt *tmp = pvt->pvt;
705 unsigned char *src = f->data;
706 int16_t *dst = (int16_t *) pvt->outbuf + pvt->samples;
707 unsigned int i;
709 for (i = 0; i < f->datalen; i++) {
710 *dst++ = g726_decode((src[i] >> 4) & 0xf, &tmp->g726);
711 *dst++ = g726_decode(src[i] & 0x0f, &tmp->g726);
714 pvt->samples += f->samples;
715 pvt->datalen += 2 * f->samples; /* 2 bytes/sample */
717 return 0;
720 /*! \brief compress and store data (4-bit G726 samples, AAL2 packing) in outbuf */
721 static int lintog726aal2_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
723 struct g726_coder_pvt *tmp = pvt->pvt;
724 int16_t *src = f->data;
725 unsigned int i;
727 for (i = 0; i < f->samples; i++) {
728 unsigned char d = g726_encode(src[i], &tmp->g726); /* this sample */
730 if (tmp->next_flag & 0x80) { /* merge with leftover sample */
731 pvt->outbuf[pvt->datalen++] = ((tmp->next_flag & 0xf)<< 4) | d;
732 pvt->samples += 2; /* 2 samples per byte */
733 tmp->next_flag = 0;
734 } else {
735 tmp->next_flag = 0x80 | d;
739 return 0;
742 /*! \brief decode packed 4-bit G726 values (RFC3551 packing) and store in buffer. */
743 static int g726tolin_framein (struct ast_trans_pvt *pvt, struct ast_frame *f)
745 struct g726_coder_pvt *tmp = pvt->pvt;
746 unsigned char *src = f->data;
747 int16_t *dst = (int16_t *) pvt->outbuf + pvt->samples;
748 unsigned int i;
750 for (i = 0; i < f->datalen; i++) {
751 *dst++ = g726_decode(src[i] & 0x0f, &tmp->g726);
752 *dst++ = g726_decode((src[i] >> 4) & 0xf, &tmp->g726);
755 pvt->samples += f->samples;
756 pvt->datalen += 2 * f->samples; /* 2 bytes/sample */
758 return 0;
761 /*! \brief compress and store data (4-bit G726 samples, RFC3551 packing) in outbuf */
762 static int lintog726_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
764 struct g726_coder_pvt *tmp = pvt->pvt;
765 int16_t *src = f->data;
766 unsigned int i;
768 for (i = 0; i < f->samples; i++) {
769 unsigned char d = g726_encode(src[i], &tmp->g726); /* this sample */
771 if (tmp->next_flag & 0x80) { /* merge with leftover sample */
772 pvt->outbuf[pvt->datalen++] = (d << 4) | (tmp->next_flag & 0xf);
773 pvt->samples += 2; /* 2 samples per byte */
774 tmp->next_flag = 0;
775 } else {
776 tmp->next_flag = 0x80 | d;
780 return 0;
783 /*! \brief convert G726-32 RFC3551 packed data into AAL2 packed data (or vice-versa) */
784 static int g726tog726aal2_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
786 unsigned char *src = f->data;
787 unsigned char *dst = (unsigned char *) pvt->outbuf + pvt->samples;
788 unsigned int i;
790 for (i = 0; i < f->datalen; i++)
791 *dst++ = ((src[i] & 0xf) << 4) | (src[i] >> 4);
793 pvt->samples += f->samples;
794 pvt->datalen += f->samples; /* 1 byte/sample */
796 return 0;
799 static struct ast_frame *g726tolin_sample(void)
801 static struct ast_frame f = {
802 .frametype = AST_FRAME_VOICE,
803 .subclass = AST_FORMAT_G726,
804 .datalen = sizeof(g726_slin_ex),
805 .samples = sizeof(g726_slin_ex) * 2, /* 2 samples per byte */
806 .src = __PRETTY_FUNCTION__,
807 .data = g726_slin_ex,
810 return &f;
813 static struct ast_frame *lintog726_sample (void)
815 static struct ast_frame f = {
816 .frametype = AST_FRAME_VOICE,
817 .subclass = AST_FORMAT_SLINEAR,
818 .datalen = sizeof(slin_g726_ex),
819 .samples = sizeof(slin_g726_ex) / 2, /* 1 sample per 2 bytes */
820 .src = __PRETTY_FUNCTION__,
821 .data = slin_g726_ex,
824 return &f;
827 static struct ast_translator g726tolin = {
828 .name = "g726tolin",
829 .srcfmt = AST_FORMAT_G726,
830 .dstfmt = AST_FORMAT_SLINEAR,
831 .newpvt = lintog726_new, /* same for both directions */
832 .framein = g726tolin_framein,
833 .sample = g726tolin_sample,
834 .desc_size = sizeof(struct g726_coder_pvt),
835 .buffer_samples = BUFFER_SAMPLES,
836 .buf_size = BUFFER_SAMPLES * 2,
837 .plc_samples = 160,
840 static struct ast_translator lintog726 = {
841 .name = "lintog726",
842 .srcfmt = AST_FORMAT_SLINEAR,
843 .dstfmt = AST_FORMAT_G726,
844 .newpvt = lintog726_new, /* same for both directions */
845 .framein = lintog726_framein,
846 .sample = lintog726_sample,
847 .desc_size = sizeof(struct g726_coder_pvt),
848 .buffer_samples = BUFFER_SAMPLES,
849 .buf_size = BUFFER_SAMPLES/2,
852 static struct ast_translator g726aal2tolin = {
853 .name = "g726aal2tolin",
854 .srcfmt = AST_FORMAT_G726_AAL2,
855 .dstfmt = AST_FORMAT_SLINEAR,
856 .newpvt = lintog726_new, /* same for both directions */
857 .framein = g726aal2tolin_framein,
858 .sample = g726tolin_sample,
859 .desc_size = sizeof(struct g726_coder_pvt),
860 .buffer_samples = BUFFER_SAMPLES,
861 .buf_size = BUFFER_SAMPLES * 2,
862 .plc_samples = 160,
865 static struct ast_translator lintog726aal2 = {
866 .name = "lintog726aal2",
867 .srcfmt = AST_FORMAT_SLINEAR,
868 .dstfmt = AST_FORMAT_G726_AAL2,
869 .newpvt = lintog726_new, /* same for both directions */
870 .framein = lintog726aal2_framein,
871 .sample = lintog726_sample,
872 .desc_size = sizeof(struct g726_coder_pvt),
873 .buffer_samples = BUFFER_SAMPLES,
874 .buf_size = BUFFER_SAMPLES / 2,
877 static struct ast_translator g726tog726aal2 = {
878 .name = "g726tog726aal2",
879 .srcfmt = AST_FORMAT_G726,
880 .dstfmt = AST_FORMAT_G726_AAL2,
881 .framein = g726tog726aal2_framein, /* same for both directions */
882 .sample = lintog726_sample,
883 .buffer_samples = BUFFER_SAMPLES,
884 .buf_size = BUFFER_SAMPLES,
887 static struct ast_translator g726aal2tog726 = {
888 .name = "g726aal2tog726",
889 .srcfmt = AST_FORMAT_G726_AAL2,
890 .dstfmt = AST_FORMAT_G726,
891 .framein = g726tog726aal2_framein, /* same for both directions */
892 .sample = lintog726_sample,
893 .buffer_samples = BUFFER_SAMPLES,
894 .buf_size = BUFFER_SAMPLES,
897 static void parse_config(void)
899 struct ast_variable *var;
900 struct ast_config *cfg = ast_config_load("codecs.conf");
902 if (!cfg)
903 return;
904 for (var = ast_variable_browse(cfg, "plc"); var; var = var->next) {
905 if (!strcasecmp(var->name, "genericplc")) {
906 g726tolin.useplc = ast_true(var->value) ? 1 : 0;
907 if (option_verbose > 2)
908 ast_verbose(VERBOSE_PREFIX_3 "codec_g726: %susing generic PLC\n",
909 g726tolin.useplc ? "" : "not ");
912 ast_config_destroy(cfg);
915 static int reload(void)
917 parse_config();
919 return 0;
922 static int unload_module(void)
924 int res = 0;
926 res |= ast_unregister_translator(&g726tolin);
927 res |= ast_unregister_translator(&lintog726);
929 res |= ast_unregister_translator(&g726aal2tolin);
930 res |= ast_unregister_translator(&lintog726aal2);
932 res |= ast_unregister_translator(&g726aal2tog726);
933 res |= ast_unregister_translator(&g726tog726aal2);
935 return res;
938 static int load_module(void)
940 int res = 0;
943 parse_config();
945 res |= ast_register_translator(&g726tolin);
946 res |= ast_register_translator(&lintog726);
948 res |= ast_register_translator(&g726aal2tolin);
949 res |= ast_register_translator(&lintog726aal2);
951 res |= ast_register_translator(&g726aal2tog726);
952 res |= ast_register_translator(&g726tog726aal2);
954 if (res)
955 unload_module();
957 return res;
960 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "ITU G.726-32kbps G726 Transcoder",
961 .load = load_module,
962 .unload = unload_module,
963 .reload = reload,