50 # if defined(_MSC_VER)
51 typedef __int64 sint64;
52 # elif defined(__GNUC__)
53 typedef long long sint64;
55 # error 64-bit integer type is not defined for your compiler/platform
59 #define BUFFER_SAMPLES 8096
63 #include "asterisk/slin.h"
96 static int qtab_721[7] = {-124, 80, 178, 246, 300, 349, 400};
101 static int _dqlntab[16] = {-2048, 4, 135, 213, 273, 323, 373, 425,
102 425, 373, 323, 273, 213, 135, 4, -2048};
105 static int _witab[16] = {-12, 18, 41, 64, 112, 198, 355, 1122,
106 1122, 355, 198, 112, 64, 41, 18, -12};
112 static int _fitab[16] = {0, 0, 0, 0x200, 0x200, 0x200, 0x600, 0xE00,
113 0xE00, 0x600, 0x200, 0x200, 0x200, 0, 0, 0};
123 static void g726_init_state(
struct g726_state *state_ptr)
127 state_ptr->yl = 34816;
132 for (cnta = 0; cnta < 2; cnta++) {
133 state_ptr->a[cnta] = 0;
134 state_ptr->pk[cnta] = 0;
136 state_ptr->sr[cnta] = 1;
138 state_ptr->sr[cnta] = 32;
141 for (cnta = 0; cnta < 6; cnta++) {
142 state_ptr->b[cnta] = 0;
144 state_ptr->dq[cnta] = 1;
146 state_ptr->dq[cnta] = 32;
160 static int quan(
int val,
int *table,
int size)
164 for (i = 0; i < size && val >= *table; ++i, ++table)
177 static int predictor_zero(
struct g726_state *state_ptr)
181 for (sezi = 0, i = 0; i < 6; i++)
182 sezi += (sint64)state_ptr->b[i] * state_ptr->dq[i];
183 return (
int)(sezi >> 13) / 2 ;
192 static int predictor_pole(
struct g726_state *state_ptr)
194 return (
int)(((sint64)state_ptr->a[1] * state_ptr->sr[1] +
195 (sint64)state_ptr->a[0] * state_ptr->sr[0]) >> 13) / 2 ;
205 static int fmult(
int an,
int srn)
207 int anmag, anexp, anmant;
211 anmag = (an > 0) ? an : ((-an) & 0x1FFF);
212 anexp = ilog2(anmag) - 5;
213 anmant = (anmag == 0) ? 32 :
214 (anexp >= 0) ? anmag >> anexp : anmag << -anexp;
215 wanexp = anexp + ((srn >> 6) & 0xF) - 13;
217 wanmant = (anmant * (srn & 077) + 0x30) >> 4;
218 retval = (wanexp >= 0) ? ((wanmant << wanexp) & 0x7FFF) :
219 (wanmant >> -wanexp);
221 return (((an ^ srn) < 0) ? -retval : retval);
224 static int predictor_zero(
struct g726_state *state_ptr)
228 for (sezi = 0, i = 0; i < 6; i++)
229 sezi += fmult(state_ptr->b[i] >> 2, state_ptr->dq[i]);
233 static int predictor_pole(
struct g726_state *state_ptr)
235 return (fmult(state_ptr->a[1] >> 2, state_ptr->sr[1]) +
236 fmult(state_ptr->a[0] >> 2, state_ptr->sr[0]));
247 static int step_size(
struct g726_state *state_ptr)
251 if (state_ptr->ap >= 256) {
252 return state_ptr->yu;
255 y = state_ptr->yl >> 6;
256 dif = state_ptr->yu - y;
257 al = state_ptr->ap >> 2;
260 y += (dif * al) >> 6;
261 }
else if (dif < 0) {
262 y += (dif * al + 0x3F) >> 6;
299 mant = ((dqm << 7) >> exp) & 0x7F;
300 dl = (exp << 7) | mant;
314 i = quan(dln, table, size);
316 return ((size << 1) + 1 - i);
318 return ((size << 1) + 1);
331 static int reconstruct(
341 dql = dqln + (y >> 2);
345 return (sign) ? -1 : 1;
347 return (sign) ? -0x8000 : 0;
350 dex = (dql >> 7) & 15;
351 dqt = 128 + (dql & 127);
353 dq = ((dqt << 19) >> (14 - dex));
354 return (sign) ? -dq : dq;
356 dq = (dqt << 7) >> (14 - dex);
357 return (sign) ? (dq - 0x8000) : dq;
387 int ylint, thr2, dqthr;
391 pk0 = (dqsez < 0) ? 1 : 0;
394 mag = abs(dq / 0x1000);
399 ylint = state_ptr->yl >> 15;
400 ylfrac = (state_ptr->yl >> 10) & 0x1F;
401 thr1 = (32 + ylfrac) << ylint;
402 thr2 = (ylint > 9) ? 31 << 10 : thr1;
403 dqthr = (thr2 + (thr2 >> 1)) >> 1;
404 if (state_ptr->td == 0) {
406 }
else if (mag <= dqthr) {
417 state_ptr->yu = y + ((wi - y) >> 5);
420 if (state_ptr->yu < 544) {
422 }
else if (state_ptr->yu > 5120) {
423 state_ptr->yu = 5120;
428 state_ptr->yl += state_ptr->yu + ((-state_ptr->yl) >> 6);
443 pks1 = pk0 ^ state_ptr->pk[0];
446 a2p = state_ptr->a[1] - (state_ptr->a[1] >> 7);
448 fa1 = (pks1) ? state_ptr->a[0] : -state_ptr->a[0];
451 }
else if (fa1 > 8191) {
457 if (pk0 ^ state_ptr->pk[1]) {
461 }
else if (a2p >= 12416) {
466 }
else if (a2p <= -12416) {
468 }
else if (a2p >= 12160) {
476 state_ptr->a[1] = a2p;
480 state_ptr->a[0] -= state_ptr->a[0] >> 8;
483 state_ptr->a[0] += 192;
485 state_ptr->a[0] -= 192;
489 if (state_ptr->a[0] < -a1ul) {
490 state_ptr->a[0] = -a1ul;
491 }
else if (state_ptr->a[0] > a1ul) {
492 state_ptr->a[0] = a1ul;
496 for (cnt = 0; cnt < 6; cnt++) {
497 if (code_size == 5) {
498 state_ptr->b[cnt] -= state_ptr->b[cnt] >> 9;
500 state_ptr->b[cnt] -= state_ptr->b[cnt] >> 8;
503 if ((dq ^ state_ptr->dq[cnt]) >= 0) {
504 state_ptr->b[cnt] += 128;
506 state_ptr->b[cnt] -= 128;
512 for (cnt = 5; cnt > 0; cnt--)
513 state_ptr->dq[cnt] = state_ptr->dq[cnt-1];
515 state_ptr->dq[0] = dq;
519 state_ptr->dq[0] = (dq >= 0) ? 0x20 : 0x20 - 0x400;
521 exp = ilog2(mag) + 1;
522 state_ptr->dq[0] = (dq >= 0) ?
523 (exp << 6) + ((mag << 6) >> exp) :
524 (exp << 6) + ((mag << 6) >> exp) - 0x400;
528 state_ptr->sr[1] = state_ptr->sr[0];
530 state_ptr->sr[0] = sr;
534 state_ptr->sr[0] = 0x20;
537 state_ptr->sr[0] = (exp << 6) + ((sr << 6) >> exp);
538 }
else if (sr > -0x8000) {
540 exp = ilog2(mag) + 1;
541 state_ptr->sr[0] = (exp << 6) + ((mag << 6) >> exp) - 0x400;
543 state_ptr->sr[0] = 0x20 - 0x400;
547 state_ptr->pk[1] = state_ptr->pk[0];
548 state_ptr->pk[0] = pk0;
553 }
else if (a2p < -11776) {
562 state_ptr->dms += (fi - state_ptr->dms) >> 5;
563 state_ptr->dml += (((fi << 2) - state_ptr->dml) >> 7);
567 }
else if (y < 1536) {
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;
588 static int g726_decode(
int i,
struct g726_state *state_ptr)
598 sezi = predictor_zero(state_ptr);
600 se = sezi + predictor_pole(state_ptr);
602 sezi = predictor_zero(state_ptr);
604 se = (sezi + predictor_pole(state_ptr)) >> 1;
607 y = step_size(state_ptr);
609 dq = reconstruct(i & 8, _dqlntab[i], y);
615 sr = (dq < 0) ? se - (dq & 0x3FFF) : se + dq;
616 dqsez = sr - se + sez;
619 update(4, y, _witab[i] << 5, _fitab[i], dq, sr, dqsez, state_ptr);
634 static int g726_encode(
int sl,
struct g726_state *state_ptr)
646 sezi = predictor_zero(state_ptr);
648 se = sezi + predictor_pole(state_ptr);
652 sezi = predictor_zero(state_ptr);
654 se = (sezi + predictor_pole(state_ptr)) >> 1;
660 y = step_size(state_ptr);
664 i = quantize(d, y, qtab_721, 7);
666 dq = reconstruct(i & 8, _dqlntab[i], y);
672 sr = (dq < 0) ? se - (dq & 0x3FFF) : se + dq;
673 dqsez = sr - se + sez;
676 update(4, y, _witab[i] << 5, _fitab[i], dq, sr, dqsez, state_ptr);
688 unsigned char next_flag;
697 g726_init_state(&tmp->g726);
706 unsigned char *src = f->
data.ptr;
707 int16_t *dst = pvt->outbuf.i16 + pvt->
samples;
710 for (i = 0; i < f->
datalen; i++) {
711 *dst++ = g726_decode((src[i] >> 4) & 0xf, &tmp->g726);
712 *dst++ = g726_decode(src[i] & 0x0f, &tmp->g726);
725 int16_t *src = f->
data.ptr;
728 for (i = 0; i < f->
samples; i++) {
729 unsigned char d = g726_encode(src[i], &tmp->g726);
731 if (tmp->next_flag & 0x80) {
732 pvt->outbuf.
c[pvt->
datalen++] = ((tmp->next_flag & 0xf)<< 4) | d;
736 tmp->next_flag = 0x80 | d;
747 unsigned char *src = f->
data.ptr;
748 int16_t *dst = pvt->outbuf.i16 + pvt->
samples;
751 for (i = 0; i < f->
datalen; i++) {
752 *dst++ = g726_decode(src[i] & 0x0f, &tmp->g726);
753 *dst++ = g726_decode((src[i] >> 4) & 0xf, &tmp->g726);
766 int16_t *src = f->
data.ptr;
769 for (i = 0; i < f->
samples; i++) {
770 unsigned char d = g726_encode(src[i], &tmp->g726);
772 if (tmp->next_flag & 0x80) {
773 pvt->outbuf.
c[pvt->
datalen++] = (d << 4) | (tmp->next_flag & 0xf);
777 tmp->next_flag = 0x80 | d;
788 .type = AST_MEDIA_TYPE_AUDIO,
793 .type = AST_MEDIA_TYPE_AUDIO,
799 .sample = g726_sample,
801 .buffer_samples = BUFFER_SAMPLES,
802 .buf_size = BUFFER_SAMPLES * 2,
809 .type = AST_MEDIA_TYPE_AUDIO,
814 .type = AST_MEDIA_TYPE_AUDIO,
820 .sample = slin8_sample,
822 .buffer_samples = BUFFER_SAMPLES,
823 .buf_size = BUFFER_SAMPLES/2,
827 .
name =
"g726aal2tolin",
830 .type = AST_MEDIA_TYPE_AUDIO,
835 .type = AST_MEDIA_TYPE_AUDIO,
841 .sample = g726_sample,
843 .buffer_samples = BUFFER_SAMPLES,
844 .buf_size = BUFFER_SAMPLES * 2,
848 .
name =
"lintog726aal2",
851 .type = AST_MEDIA_TYPE_AUDIO,
856 .type = AST_MEDIA_TYPE_AUDIO,
859 .format =
"g726aal2",
862 .sample = slin8_sample,
864 .buffer_samples = BUFFER_SAMPLES,
865 .buf_size = BUFFER_SAMPLES / 2,
868 static int unload_module(
void)
881 static int load_module(
void)
899 AST_MODULE_INFO(
ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT,
"ITU G.726-32kbps G726 Transcoder",
900 .support_level = AST_MODULE_SUPPORT_CORE,
902 .unload = unload_module,
int datalen
actual space used in outbuf
Asterisk locking-related definitions:
Asterisk main include file. File version handling, generic pbx functions.
Descriptor of a translator.
Support for translation of data formats. translate.c.
static int lintog726aal2_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
compress and store data (4-bit G726 samples, AAL2 packing) in outbuf
Configuration File Parser.
static int g726aal2tolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
decode packed 4-bit G726 values (AAL2 packing) and store in buffer.
log2comp.h - various base 2 log computation versions
#define ast_register_translator(t)
See __ast_register_translator()
int ast_unregister_translator(struct ast_translator *t)
Unregister a translator Unregisters the given translator.
A set of macros to manage forward-linked lists.
static int lintog726_new(struct ast_trans_pvt *pvt)
init a new instance of g726_coder_pvt.
Default structure for translators, with the basic fields and buffers, all allocated as part of the sa...
union ast_frame::@224 data
Module has failed to load, may be in an inconsistent state.
static int g726tolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
decode packed 4-bit G726 values (RFC3551 packing) and store in buffer.
static int lintog726_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
compress and store data (4-bit G726 samples, RFC3551 packing) in outbuf
Data structure associated with a single frame of data.
#define ASTERISK_GPL_KEY
The text the key() function should return.
Asterisk module definitions.