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.
25 * \brief codec_g726.c - translate between signed linear and ITU G.726-32kbps (both RFC3551 and AAL2 codeword packing)
32 ASTERISK_FILE_VERSION(__FILE__
, "$Revision$")
35 #include <netinet/in.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"
54 /* define NOT_BLI to use a faster but not bit-level identical version */
58 # if defined(_MSC_VER)
59 typedef __int64 sint64
;
60 # elif defined(__GNUC__)
61 typedef long long sint64
;
63 # error 64-bit integer type is not defined for your compiler/platform
67 #define BUFFER_SAMPLES 8096 /* size for the translation buffers */
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.
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
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};
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
)
136 state_ptr
->yl
= 34816;
141 for (cnta
= 0; cnta
< 2; cnta
++) {
142 state_ptr
->a
[cnta
] = 0;
143 state_ptr
->pk
[cnta
] = 0;
145 state_ptr
->sr
[cnta
] = 1;
147 state_ptr
->sr
[cnta
] = 32;
150 for (cnta
= 0; cnta
< 6; cnta
++) {
151 state_ptr
->b
[cnta
] = 0;
153 state_ptr
->dq
[cnta
] = 1;
155 state_ptr
->dq
[cnta
] = 32;
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
)
173 for (i
= 0; i
< size
&& val
>= *table
; ++i
, ++table
)
178 #ifdef NOT_BLI /* faster non-identical version */
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 */
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 */;
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 */
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
;
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
)
237 for (sezi
= 0, i
= 0; i
< 6; i
++) /* ACCUM */
238 sezi
+= fmult(state_ptr
->b
[i
] >> 2, state_ptr
->dq
[i
]);
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]));
253 * computes the quantization step size of the adaptive quantizer.
256 static int step_size(struct g726_state
*state_ptr
)
262 if (state_ptr
->ap
>= 256)
263 return (state_ptr
->yu
);
265 y
= state_ptr
->yl
>> 6;
266 dif
= state_ptr
->yu
- y
;
267 al
= state_ptr
->ap
>> 2;
269 y
+= (dif
* al
) >> 6;
271 y
+= (dif
* al
+ 0x3F) >> 6;
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
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 */
301 * Compute base 2 log of 'd', and store in 'dl'.
307 mant
= ((dqm
<< 7) >> exp
) & 0x7F; /* Fractional portion. */
308 dl
= (exp
<< 7) | mant
;
313 * "Divide" by step size multiplier.
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 */
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 */
346 int dq
; /* Reconstructed difference signal sample */
348 dql
= dqln
+ (y
>> 2); /* ADDA */
352 return (sign
) ? -1 : 1;
354 return (sign
) ? -0x8000 : 0;
356 } else { /* ANTILOG */
357 dex
= (dql
>> 7) & 15;
358 dqt
= 128 + (dql
& 127);
360 dq
= ((dqt
<< 19) >> (14 - dex
));
361 return (sign
) ? -dq
: dq
;
363 dq
= (dqt
<< 7) >> (14 - dex
);
364 return (sign
) ? (dq
- 0x8000) : dq
;
372 * updates the state variables for each output code
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 */
385 int mag
; /* Adaptive predictor, FLOAT A */
389 int a2p
=0; /* LIMC */
393 int tr
; /* tone/transition detector */
394 int ylint
, thr2
, dqthr
;
398 pk0
= (dqsez
< 0) ? 1 : 0; /* needed in updating predictor poles */
401 mag
= abs(dq
/ 0x1000); /* prediction difference magnitude */
403 mag
= dq
& 0x7FFF; /* prediction difference magnitude */
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 */
413 else if (mag
<= dqthr
) /* supposed data, but small mag */
414 tr
= 0; /* treated as voice */
415 else /* signal is data (modem) */
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);
427 if (state_ptr
->yu
< 544) /* 544 <= yu <= 5120 */
429 else if (state_ptr
->yu
> 5120)
430 state_ptr
->yu
= 5120;
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 */
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);
454 fa1
= (pks1
) ? state_ptr
->a
[0] : -state_ptr
->a
[0];
455 if (fa1
< -8191) /* a2p = function of fa1 */
462 if (pk0
^ state_ptr
->pk
[1])
466 else if (a2p
>= 12416)
470 else if (a2p
<= -12416)
472 else if (a2p
>= 12160)
479 state_ptr
->a
[1] = a2p
;
482 /* update predictor pole a[0] */
483 state_ptr
->a
[0] -= state_ptr
->a
[0] >> 8;
486 state_ptr
->a
[0] += 192;
488 state_ptr
->a
[0] -= 192;
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;
505 if ((dq
^ state_ptr
->dq
[cnt
]) >= 0)
506 state_ptr
->b
[cnt
] += 128;
508 state_ptr
->b
[cnt
] -= 128;
513 for (cnt
= 5; cnt
> 0; cnt
--)
514 state_ptr
->dq
[cnt
] = state_ptr
->dq
[cnt
-1];
516 state_ptr
->dq
[0] = dq
;
518 /* FLOAT A : convert dq[0] to 4-bit exp, 6-bit mantissa f.p. */
520 state_ptr
->dq
[0] = (dq
>= 0) ? 0x20 : 0x20 - 0x400;
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;
529 state_ptr
->sr
[1] = state_ptr
->sr
[0];
531 state_ptr
->sr
[0] = sr
;
533 /* FLOAT B : convert sr to 4-bit exp., 6-bit mantissa f.p. */
535 state_ptr
->sr
[0] = 0x20;
538 state_ptr
->sr
[0] = (exp
<< 6) + ((sr
<< 6) >> exp
);
539 } else if (sr
> -0x8000) {
541 exp
= ilog2(mag
) + 1;
542 state_ptr
->sr
[0] = (exp
<< 6) + ((mag
<< 6) >> exp
) - 0x400;
544 state_ptr
->sr
[0] = 0x20 - 0x400;
548 state_ptr
->pk
[1] = state_ptr
->pk
[0];
549 state_ptr
->pk
[0] = pk0
;
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 */
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 */
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;
575 state_ptr
->ap
+= (-state_ptr
->ap
) >> 4;
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 */
595 i
&= 0x0f; /* mask to get proper bits */
597 sezi
= predictor_zero(state_ptr
);
599 se
= sezi
+ predictor_pole(state_ptr
); /* estimated signal */
601 sezi
= predictor_zero(state_ptr
);
603 se
= (sezi
+ predictor_pole(state_ptr
)) >> 1; /* estimated signal */
606 y
= step_size(state_ptr
); /* dynamic quantizer step size */
608 dq
= reconstruct(i
& 8, _dqlntab
[i
], y
); /* quantized diff. */
611 sr
= se
+ dq
; /* reconst. signal */
612 dqsez
= dq
+ sez
; /* pole prediction diff. */
614 sr
= (dq
< 0) ? se
- (dq
& 0x3FFF) : se
+ dq
; /* reconst. signal */
615 dqsez
= sr
- se
+ sez
; /* pole prediction diff. */
618 update(4, y
, _witab
[i
] << 5, _fitab
[i
], dq
, sr
, dqsez
, state_ptr
);
621 return (sr
>> 10); /* sr was 26-bit dynamic range */
623 return (sr
<< 2); /* sr was 14-bit dynamic range */
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 */
639 int dqsez
; /* ADDC */
643 sl
<<= 10; /* 26-bit dynamic range */
645 sezi
= predictor_zero(state_ptr
);
647 se
= sezi
+ predictor_pole(state_ptr
); /* estimated signal */
649 sl
>>= 2; /* 14-bit dynamic range */
651 sezi
= predictor_zero(state_ptr
);
653 se
= (sezi
+ predictor_pole(state_ptr
)) >> 1; /* estimated signal */
656 d
= sl
- se
; /* estimation difference */
658 /* quantize the prediction difference */
659 y
= step_size(state_ptr
); /* quantizer step size */
663 i
= quantize(d
, y
, qtab_721
, 7); /* i = G726 code */
665 dq
= reconstruct(i
& 8, _dqlntab
[i
], y
); /* quantized est diff */
668 sr
= se
+ dq
; /* reconst. signal */
669 dqsez
= dq
+ sez
; /* pole prediction diff. */
671 sr
= (dq
< 0) ? se
- (dq
& 0x3FFF) : se
+ dq
; /* reconst. signal */
672 dqsez
= sr
- se
+ sez
; /* pole prediction diff. */
675 update(4, y
, _witab
[i
] << 5, _fitab
[i
], dq
, sr
, dqsez
, state_ptr
);
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
);
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
;
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 */
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
;
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 */
735 tmp
->next_flag
= 0x80 | d
;
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
;
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 */
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
;
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 */
776 tmp
->next_flag
= 0x80 | d
;
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
;
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 */
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
,
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
,
827 static struct ast_translator 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,
840 static struct ast_translator 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,
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");
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)
922 static int unload_module(void)
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
);
938 static int load_module(void)
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
);
960 AST_MODULE_INFO(ASTERISK_GPL_KEY
, AST_MODFLAG_DEFAULT
, "ITU G.726-32kbps G726 Transcoder",
962 .unload
= unload_module
,