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$")
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"
44 /* define NOT_BLI to use a faster but not bit-level identical version */
48 # if defined(_MSC_VER)
49 typedef __int64 sint64
;
50 # elif defined(__GNUC__)
51 typedef long long sint64
;
53 # error 64-bit integer type is not defined for your compiler/platform
57 #define BUFFER_SAMPLES 8096 /* size for the translation buffers */
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.
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
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};
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
)
126 state_ptr
->yl
= 34816;
131 for (cnta
= 0; cnta
< 2; cnta
++) {
132 state_ptr
->a
[cnta
] = 0;
133 state_ptr
->pk
[cnta
] = 0;
135 state_ptr
->sr
[cnta
] = 1;
137 state_ptr
->sr
[cnta
] = 32;
140 for (cnta
= 0; cnta
< 6; cnta
++) {
141 state_ptr
->b
[cnta
] = 0;
143 state_ptr
->dq
[cnta
] = 1;
145 state_ptr
->dq
[cnta
] = 32;
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
)
163 for (i
= 0; i
< size
&& val
>= *table
; ++i
, ++table
)
168 #ifdef NOT_BLI /* faster non-identical version */
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 */
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 */;
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 */
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
;
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
)
227 for (sezi
= 0, i
= 0; i
< 6; i
++) /* ACCUM */
228 sezi
+= fmult(state_ptr
->b
[i
] >> 2, state_ptr
->dq
[i
]);
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]));
243 * computes the quantization step size of the adaptive quantizer.
246 static int step_size(struct g726_state
*state_ptr
)
252 if (state_ptr
->ap
>= 256)
253 return (state_ptr
->yu
);
255 y
= state_ptr
->yl
>> 6;
256 dif
= state_ptr
->yu
- y
;
257 al
= state_ptr
->ap
>> 2;
259 y
+= (dif
* al
) >> 6;
261 y
+= (dif
* al
+ 0x3F) >> 6;
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
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 */
291 * Compute base 2 log of 'd', and store in 'dl'.
297 mant
= ((dqm
<< 7) >> exp
) & 0x7F; /* Fractional portion. */
298 dl
= (exp
<< 7) | mant
;
303 * "Divide" by step size multiplier.
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 */
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 */
336 int dq
; /* Reconstructed difference signal sample */
338 dql
= dqln
+ (y
>> 2); /* ADDA */
342 return (sign
) ? -1 : 1;
344 return (sign
) ? -0x8000 : 0;
346 } else { /* ANTILOG */
347 dex
= (dql
>> 7) & 15;
348 dqt
= 128 + (dql
& 127);
350 dq
= ((dqt
<< 19) >> (14 - dex
));
351 return (sign
) ? -dq
: dq
;
353 dq
= (dqt
<< 7) >> (14 - dex
);
354 return (sign
) ? (dq
- 0x8000) : dq
;
362 * updates the state variables for each output code
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 */
375 int mag
; /* Adaptive predictor, FLOAT A */
379 int a2p
=0; /* LIMC */
383 int tr
; /* tone/transition detector */
384 int ylint
, thr2
, dqthr
;
388 pk0
= (dqsez
< 0) ? 1 : 0; /* needed in updating predictor poles */
391 mag
= abs(dq
/ 0x1000); /* prediction difference magnitude */
393 mag
= dq
& 0x7FFF; /* prediction difference magnitude */
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 */
403 else if (mag
<= dqthr
) /* supposed data, but small mag */
404 tr
= 0; /* treated as voice */
405 else /* signal is data (modem) */
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);
417 if (state_ptr
->yu
< 544) /* 544 <= yu <= 5120 */
419 else if (state_ptr
->yu
> 5120)
420 state_ptr
->yu
= 5120;
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 */
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);
444 fa1
= (pks1
) ? state_ptr
->a
[0] : -state_ptr
->a
[0];
445 if (fa1
< -8191) /* a2p = function of fa1 */
452 if (pk0
^ state_ptr
->pk
[1])
456 else if (a2p
>= 12416)
460 else if (a2p
<= -12416)
462 else if (a2p
>= 12160)
469 state_ptr
->a
[1] = a2p
;
472 /* update predictor pole a[0] */
473 state_ptr
->a
[0] -= state_ptr
->a
[0] >> 8;
476 state_ptr
->a
[0] += 192;
478 state_ptr
->a
[0] -= 192;
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;
495 if ((dq
^ state_ptr
->dq
[cnt
]) >= 0)
496 state_ptr
->b
[cnt
] += 128;
498 state_ptr
->b
[cnt
] -= 128;
503 for (cnt
= 5; cnt
> 0; cnt
--)
504 state_ptr
->dq
[cnt
] = state_ptr
->dq
[cnt
-1];
506 state_ptr
->dq
[0] = dq
;
508 /* FLOAT A : convert dq[0] to 4-bit exp, 6-bit mantissa f.p. */
510 state_ptr
->dq
[0] = (dq
>= 0) ? 0x20 : 0x20 - 0x400;
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;
519 state_ptr
->sr
[1] = state_ptr
->sr
[0];
521 state_ptr
->sr
[0] = sr
;
523 /* FLOAT B : convert sr to 4-bit exp., 6-bit mantissa f.p. */
525 state_ptr
->sr
[0] = 0x20;
528 state_ptr
->sr
[0] = (exp
<< 6) + ((sr
<< 6) >> exp
);
529 } else if (sr
> -0x8000) {
531 exp
= ilog2(mag
) + 1;
532 state_ptr
->sr
[0] = (exp
<< 6) + ((mag
<< 6) >> exp
) - 0x400;
534 state_ptr
->sr
[0] = 0x20 - 0x400;
538 state_ptr
->pk
[1] = state_ptr
->pk
[0];
539 state_ptr
->pk
[0] = pk0
;
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 */
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 */
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;
565 state_ptr
->ap
+= (-state_ptr
->ap
) >> 4;
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 */
585 i
&= 0x0f; /* mask to get proper bits */
587 sezi
= predictor_zero(state_ptr
);
589 se
= sezi
+ predictor_pole(state_ptr
); /* estimated signal */
591 sezi
= predictor_zero(state_ptr
);
593 se
= (sezi
+ predictor_pole(state_ptr
)) >> 1; /* estimated signal */
596 y
= step_size(state_ptr
); /* dynamic quantizer step size */
598 dq
= reconstruct(i
& 8, _dqlntab
[i
], y
); /* quantized diff. */
601 sr
= se
+ dq
; /* reconst. signal */
602 dqsez
= dq
+ sez
; /* pole prediction diff. */
604 sr
= (dq
< 0) ? se
- (dq
& 0x3FFF) : se
+ dq
; /* reconst. signal */
605 dqsez
= sr
- se
+ sez
; /* pole prediction diff. */
608 update(4, y
, _witab
[i
] << 5, _fitab
[i
], dq
, sr
, dqsez
, state_ptr
);
611 return (sr
>> 10); /* sr was 26-bit dynamic range */
613 return (sr
<< 2); /* sr was 14-bit dynamic range */
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 */
629 int dqsez
; /* ADDC */
633 sl
<<= 10; /* 26-bit dynamic range */
635 sezi
= predictor_zero(state_ptr
);
637 se
= sezi
+ predictor_pole(state_ptr
); /* estimated signal */
639 sl
>>= 2; /* 14-bit dynamic range */
641 sezi
= predictor_zero(state_ptr
);
643 se
= (sezi
+ predictor_pole(state_ptr
)) >> 1; /* estimated signal */
646 d
= sl
- se
; /* estimation difference */
648 /* quantize the prediction difference */
649 y
= step_size(state_ptr
); /* quantizer step size */
653 i
= quantize(d
, y
, qtab_721
, 7); /* i = G726 code */
655 dq
= reconstruct(i
& 8, _dqlntab
[i
], y
); /* quantized est diff */
658 sr
= se
+ dq
; /* reconst. signal */
659 dqsez
= dq
+ sez
; /* pole prediction diff. */
661 sr
= (dq
< 0) ? se
- (dq
& 0x3FFF) : se
+ dq
; /* reconst. signal */
662 dqsez
= sr
- se
+ sez
; /* pole prediction diff. */
665 update(4, y
, _witab
[i
] << 5, _fitab
[i
], dq
, sr
, dqsez
, state_ptr
);
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
);
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
;
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 */
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
;
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 */
725 tmp
->next_flag
= 0x80 | d
;
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
;
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 */
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
;
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 */
766 tmp
->next_flag
= 0x80 | d
;
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
;
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 */
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
,
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
,
817 static struct ast_translator 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,
830 static struct ast_translator 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,
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
);
895 if (cfg
== CONFIG_STATUS_FILEUNCHANGED
)
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
);
908 static int reload(void)
911 return AST_MODULE_LOAD_DECLINE
;
912 return AST_MODULE_LOAD_SUCCESS
;
915 static int unload_module(void)
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
);
931 static int load_module(void)
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
);
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",
958 .unload
= unload_module
,