123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927 |
- #include <asterisk/lock.h>
- #include <asterisk/logger.h>
- #include <asterisk/module.h>
- #include <asterisk/translate.h>
- #include <asterisk/channel.h>
- #include <fcntl.h>
- #include <netinet/in.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #define BUFFER_SIZE 8096
- #define BUF_SHIFT 5
- AST_MUTEX_DEFINE_STATIC(localuser_lock);
- static int localusecnt = 0;
- static char *tdesc = "ITU G.726-32kbps G726 Transcoder";
- #include "slin_g726_ex.h"
- #include "g726_slin_ex.h"
- struct g726_state {
- long yl;
- short yu;
- short dms;
- short dml;
- short ap;
- short a[2];
- short b[6];
- short pk[2];
- short dq[6];
- short sr[2];
- char td;
- };
- static short qtab_721[7] = {-124, 80, 178, 246, 300, 349, 400};
- static short _dqlntab[16] = {-2048, 4, 135, 213, 273, 323, 373, 425,
- 425, 373, 323, 273, 213, 135, 4, -2048};
- static short _witab[16] = {-12, 18, 41, 64, 112, 198, 355, 1122,
- 1122, 355, 198, 112, 64, 41, 18, -12};
- static short _fitab[16] = {0, 0, 0, 0x200, 0x200, 0x200, 0x600, 0xE00,
- 0xE00, 0x600, 0x200, 0x200, 0x200, 0, 0, 0};
- static short power2[15] = {1, 2, 4, 8, 0x10, 0x20, 0x40, 0x80,
- 0x100, 0x200, 0x400, 0x800, 0x1000, 0x2000, 0x4000};
- static int quan(int val, short *table, int size)
- {
- int i;
- for (i = 0; i < size; i++)
- if (val < *table++)
- break;
- return (i);
- }
- static int fmult(int an, int srn)
- {
- short anmag, anexp, anmant;
- short wanexp, wanmant;
- short retval;
- anmag = (an > 0) ? an : ((-an) & 0x1FFF);
- anexp = quan(anmag, power2, 15) - 6;
- anmant = (anmag == 0) ? 32 :
- (anexp >= 0) ? anmag >> anexp : anmag << -anexp;
- wanexp = anexp + ((srn >> 6) & 0xF) - 13;
- wanmant = (anmant * (srn & 077) + 0x30) >> 4;
- retval = (wanexp >= 0) ? ((wanmant << wanexp) & 0x7FFF) :
- (wanmant >> -wanexp);
- return (((an ^ srn) < 0) ? -retval : retval);
- }
- static void g726_init_state(struct g726_state *state_ptr)
- {
- int cnta;
- state_ptr->yl = 34816;
- state_ptr->yu = 544;
- state_ptr->dms = 0;
- state_ptr->dml = 0;
- state_ptr->ap = 0;
- for (cnta = 0; cnta < 2; cnta++) {
- state_ptr->a[cnta] = 0;
- state_ptr->pk[cnta] = 0;
- state_ptr->sr[cnta] = 32;
- }
- for (cnta = 0; cnta < 6; cnta++) {
- state_ptr->b[cnta] = 0;
- state_ptr->dq[cnta] = 32;
- }
- state_ptr->td = 0;
- }
- static int predictor_zero(struct g726_state *state_ptr)
- {
- int i;
- int sezi;
- sezi = fmult(state_ptr->b[0] >> 2, state_ptr->dq[0]);
- for (i = 1; i < 6; i++)
- sezi += fmult(state_ptr->b[i] >> 2, state_ptr->dq[i]);
- return (sezi);
- }
- static int predictor_pole(struct g726_state *state_ptr)
- {
- return (fmult(state_ptr->a[1] >> 2, state_ptr->sr[1]) +
- fmult(state_ptr->a[0] >> 2, state_ptr->sr[0]));
- }
- static int step_size(struct g726_state *state_ptr)
- {
- int y;
- int dif;
- int al;
- if (state_ptr->ap >= 256)
- return (state_ptr->yu);
- else {
- y = state_ptr->yl >> 6;
- dif = state_ptr->yu - y;
- al = state_ptr->ap >> 2;
- if (dif > 0)
- y += (dif * al) >> 6;
- else if (dif < 0)
- y += (dif * al + 0x3F) >> 6;
- return (y);
- }
- }
- static int quantize(
- int d,
- int y,
- short *table,
- int size)
- {
- short dqm;
- short exp;
- short mant;
- short dl;
- short dln;
- int i;
-
- dqm = abs(d);
- exp = quan(dqm >> 1, power2, 15);
- mant = ((dqm << 7) >> exp) & 0x7F;
- dl = (exp << 7) + mant;
-
- dln = dl - (y >> 2);
-
- i = quan(dln, table, size);
- if (d < 0)
- return ((size << 1) + 1 - i);
- else if (i == 0)
- return ((size << 1) + 1);
- else
- return (i);
- }
- static int reconstruct(
- int sign,
- int dqln,
- int y)
- {
- short dql;
- short dex;
- short dqt;
- short dq;
- dql = dqln + (y >> 2);
- if (dql < 0) {
- return ((sign) ? -0x8000 : 0);
- } else {
- dex = (dql >> 7) & 15;
- dqt = 128 + (dql & 127);
- dq = (dqt << 7) >> (14 - dex);
- return ((sign) ? (dq - 0x8000) : dq);
- }
- }
- static void update(
- int code_size,
- int y,
- int wi,
- int fi,
- int dq,
- int sr,
- int dqsez,
- struct g726_state *state_ptr)
- {
- int cnt;
- short mag, exp;
- short a2p=0;
- short a1ul;
- short pks1;
- short fa1;
- char tr;
- short ylint, thr2, dqthr;
- short ylfrac, thr1;
- short pk0;
- pk0 = (dqsez < 0) ? 1 : 0;
- mag = dq & 0x7FFF;
-
- ylint = state_ptr->yl >> 15;
- ylfrac = (state_ptr->yl >> 10) & 0x1F;
- thr1 = (32 + ylfrac) << ylint;
- thr2 = (ylint > 9) ? 31 << 10 : thr1;
- dqthr = (thr2 + (thr2 >> 1)) >> 1;
- if (state_ptr->td == 0)
- tr = 0;
- else if (mag <= dqthr)
- tr = 0;
- else
- tr = 1;
-
-
-
- state_ptr->yu = y + ((wi - y) >> 5);
-
- if (state_ptr->yu < 544)
- state_ptr->yu = 544;
- else if (state_ptr->yu > 5120)
- state_ptr->yu = 5120;
-
-
- state_ptr->yl += state_ptr->yu + ((-state_ptr->yl) >> 6);
-
- if (tr == 1) {
- state_ptr->a[0] = 0;
- state_ptr->a[1] = 0;
- state_ptr->b[0] = 0;
- state_ptr->b[1] = 0;
- state_ptr->b[2] = 0;
- state_ptr->b[3] = 0;
- state_ptr->b[4] = 0;
- state_ptr->b[5] = 0;
- } else {
- pks1 = pk0 ^ state_ptr->pk[0];
-
- a2p = state_ptr->a[1] - (state_ptr->a[1] >> 7);
- if (dqsez != 0) {
- fa1 = (pks1) ? state_ptr->a[0] : -state_ptr->a[0];
- if (fa1 < -8191)
- a2p -= 0x100;
- else if (fa1 > 8191)
- a2p += 0xFF;
- else
- a2p += fa1 >> 5;
- if (pk0 ^ state_ptr->pk[1])
-
- if (a2p <= -12160)
- a2p = -12288;
- else if (a2p >= 12416)
- a2p = 12288;
- else
- a2p -= 0x80;
- else if (a2p <= -12416)
- a2p = -12288;
- else if (a2p >= 12160)
- a2p = 12288;
- else
- a2p += 0x80;
- }
-
- state_ptr->a[1] = a2p;
-
-
- state_ptr->a[0] -= state_ptr->a[0] >> 8;
- if (dqsez != 0) {
- if (pks1 == 0)
- state_ptr->a[0] += 192;
- else
- state_ptr->a[0] -= 192;
- }
-
- a1ul = 15360 - a2p;
- if (state_ptr->a[0] < -a1ul)
- state_ptr->a[0] = -a1ul;
- else if (state_ptr->a[0] > a1ul)
- state_ptr->a[0] = a1ul;
-
- for (cnt = 0; cnt < 6; cnt++) {
- if (code_size == 5)
- state_ptr->b[cnt] -= state_ptr->b[cnt] >> 9;
- else
- state_ptr->b[cnt] -= state_ptr->b[cnt] >> 8;
- if (dq & 0x7FFF) {
- if ((dq ^ state_ptr->dq[cnt]) >= 0)
- state_ptr->b[cnt] += 128;
- else
- state_ptr->b[cnt] -= 128;
- }
- }
- }
- for (cnt = 5; cnt > 0; cnt--)
- state_ptr->dq[cnt] = state_ptr->dq[cnt-1];
-
- if (mag == 0) {
- state_ptr->dq[0] = (dq >= 0) ? 0x20 : 0xFC20;
- } else {
- exp = quan(mag, power2, 15);
- state_ptr->dq[0] = (dq >= 0) ?
- (exp << 6) + ((mag << 6) >> exp) :
- (exp << 6) + ((mag << 6) >> exp) - 0x400;
- }
- state_ptr->sr[1] = state_ptr->sr[0];
-
- if (sr == 0) {
- state_ptr->sr[0] = 0x20;
- } else if (sr > 0) {
- exp = quan(sr, power2, 15);
- state_ptr->sr[0] = (exp << 6) + ((sr << 6) >> exp);
- } else if (sr > -32768) {
- mag = -sr;
- exp = quan(mag, power2, 15);
- state_ptr->sr[0] = (exp << 6) + ((mag << 6) >> exp) - 0x400;
- } else
- state_ptr->sr[0] = 0xFC20;
-
- state_ptr->pk[1] = state_ptr->pk[0];
- state_ptr->pk[0] = pk0;
-
- if (tr == 1)
- state_ptr->td = 0;
- else if (a2p < -11776)
- state_ptr->td = 1;
- else
- state_ptr->td = 0;
-
- state_ptr->dms += (fi - state_ptr->dms) >> 5;
- state_ptr->dml += (((fi << 2) - state_ptr->dml) >> 7);
- if (tr == 1)
- state_ptr->ap = 256;
- else if (y < 1536)
- state_ptr->ap += (0x200 - state_ptr->ap) >> 4;
- else if (state_ptr->td == 1)
- state_ptr->ap += (0x200 - state_ptr->ap) >> 4;
- else if (abs((state_ptr->dms << 2) - state_ptr->dml) >=
- (state_ptr->dml >> 3))
- state_ptr->ap += (0x200 - state_ptr->ap) >> 4;
- else
- state_ptr->ap += (-state_ptr->ap) >> 4;
- }
- static int g726_decode(int i, struct g726_state *state_ptr)
- {
- short sezi, sei, sez, se;
- short y;
- short sr;
- short dq;
- short dqsez;
- i &= 0x0f;
- sezi = predictor_zero(state_ptr);
- sez = sezi >> 1;
- sei = sezi + predictor_pole(state_ptr);
- se = sei >> 1;
- y = step_size(state_ptr);
- dq = reconstruct(i & 0x08, _dqlntab[i], y);
- sr = (dq < 0) ? (se - (dq & 0x3FFF)) : se + dq;
- dqsez = sr - se + sez;
- update(4, y, _witab[i] << 5, _fitab[i], dq, sr, dqsez, state_ptr);
- return (sr << 2);
- }
- static int g726_encode(int sl, struct g726_state *state_ptr)
- {
- short sezi, se, sez;
- short d;
- short sr;
- short y;
- short dqsez;
- short dq, i;
- sl >>= 2;
- sezi = predictor_zero(state_ptr);
- sez = sezi >> 1;
- se = (sezi + predictor_pole(state_ptr)) >> 1;
- d = sl - se;
-
- y = step_size(state_ptr);
- i = quantize(d, y, qtab_721, 7);
- dq = reconstruct(i & 8, _dqlntab[i], y);
- sr = (dq < 0) ? se - (dq & 0x3FFF) : se + dq;
- dqsez = sr + sez - se;
- update(4, y, _witab[i] << 5, _fitab[i], dq, sr, dqsez, state_ptr);
- return (i);
- }
- struct g726_encoder_pvt
- {
- struct ast_frame f;
- char offset[AST_FRIENDLY_OFFSET];
- unsigned char outbuf[BUFFER_SIZE];
- unsigned char next_flag;
- struct g726_state g726;
- int tail;
- };
- struct g726_decoder_pvt
- {
- struct ast_frame f;
- char offset[AST_FRIENDLY_OFFSET];
- short outbuf[BUFFER_SIZE];
- struct g726_state g726;
- int tail;
- };
- static struct ast_translator_pvt *
- g726tolin_new (void)
- {
- struct g726_decoder_pvt *tmp;
- tmp = malloc (sizeof (struct g726_decoder_pvt));
- if (tmp)
- {
- memset(tmp, 0, sizeof(*tmp));
- tmp->tail = 0;
- localusecnt++;
- g726_init_state(&tmp->g726);
- ast_update_use_count ();
- }
- return (struct ast_translator_pvt *) tmp;
- }
- static struct ast_translator_pvt *
- lintog726_new (void)
- {
- struct g726_encoder_pvt *tmp;
- tmp = malloc (sizeof (struct g726_encoder_pvt));
- if (tmp)
- {
- memset(tmp, 0, sizeof(*tmp));
- localusecnt++;
- tmp->tail = 0;
- g726_init_state(&tmp->g726);
- ast_update_use_count ();
- }
- return (struct ast_translator_pvt *) tmp;
- }
- static int
- g726tolin_framein (struct ast_translator_pvt *pvt, struct ast_frame *f)
- {
- struct g726_decoder_pvt *tmp = (struct g726_decoder_pvt *) pvt;
- unsigned char *b;
- int x;
- b = f->data;
- for (x=0;x<f->datalen;x++) {
- if (tmp->tail >= BUFFER_SIZE) {
- ast_log(LOG_WARNING, "Out of buffer space!\n");
- return -1;
- }
- tmp->outbuf[tmp->tail++] = g726_decode((b[x] >> 4) & 0xf, &tmp->g726);
- if (tmp->tail >= BUFFER_SIZE) {
- ast_log(LOG_WARNING, "Out of buffer space!\n");
- return -1;
- }
- tmp->outbuf[tmp->tail++] = g726_decode(b[x] & 0x0f, &tmp->g726);
- }
- return 0;
- }
- static struct ast_frame *
- g726tolin_frameout (struct ast_translator_pvt *pvt)
- {
- struct g726_decoder_pvt *tmp = (struct g726_decoder_pvt *) pvt;
- if (!tmp->tail)
- return NULL;
- tmp->f.frametype = AST_FRAME_VOICE;
- tmp->f.subclass = AST_FORMAT_SLINEAR;
- tmp->f.datalen = tmp->tail * 2;
- tmp->f.samples = tmp->tail;
- tmp->f.mallocd = 0;
- tmp->f.offset = AST_FRIENDLY_OFFSET;
- tmp->f.src = __PRETTY_FUNCTION__;
- tmp->f.data = tmp->outbuf;
- tmp->tail = 0;
- return &tmp->f;
- }
- static int
- lintog726_framein (struct ast_translator_pvt *pvt, struct ast_frame *f)
- {
- struct g726_encoder_pvt *tmp = (struct g726_encoder_pvt *) pvt;
- short *s = f->data;
- int samples = f->datalen / 2;
- int x;
- for (x=0;x<samples;x++) {
- if (tmp->next_flag & 0x80) {
- if (tmp->tail >= BUFFER_SIZE) {
- ast_log(LOG_WARNING, "Out of buffer space\n");
- return -1;
- }
- tmp->outbuf[tmp->tail++] = ((tmp->next_flag & 0xf)<< 4) | g726_encode(s[x], &tmp->g726);
- tmp->next_flag = 0;
- } else {
- tmp->next_flag = 0x80 | g726_encode(s[x], &tmp->g726);
- }
- }
- return 0;
- }
- static struct ast_frame *
- lintog726_frameout (struct ast_translator_pvt *pvt)
- {
- struct g726_encoder_pvt *tmp = (struct g726_encoder_pvt *) pvt;
-
- if (!tmp->tail)
- return NULL;
- tmp->f.frametype = AST_FRAME_VOICE;
- tmp->f.subclass = AST_FORMAT_G726;
- tmp->f.samples = tmp->tail * 2;
- tmp->f.mallocd = 0;
- tmp->f.offset = AST_FRIENDLY_OFFSET;
- tmp->f.src = __PRETTY_FUNCTION__;
- tmp->f.data = tmp->outbuf;
- tmp->f.datalen = tmp->tail;
- tmp->tail = 0;
- return &tmp->f;
- }
- static struct ast_frame *
- g726tolin_sample (void)
- {
- static struct ast_frame f;
- f.frametype = AST_FRAME_VOICE;
- f.subclass = AST_FORMAT_G726;
- f.datalen = sizeof (g726_slin_ex);
- f.samples = sizeof(g726_slin_ex) * 2;
- f.mallocd = 0;
- f.offset = 0;
- f.src = __PRETTY_FUNCTION__;
- f.data = g726_slin_ex;
- return &f;
- }
- static struct ast_frame *
- lintog726_sample (void)
- {
- static struct ast_frame f;
- f.frametype = AST_FRAME_VOICE;
- f.subclass = AST_FORMAT_SLINEAR;
- f.datalen = sizeof (slin_g726_ex);
-
- f.samples = sizeof (slin_g726_ex) / 2;
- f.mallocd = 0;
- f.offset = 0;
- f.src = __PRETTY_FUNCTION__;
- f.data = slin_g726_ex;
- return &f;
- }
- static void
- g726_destroy (struct ast_translator_pvt *pvt)
- {
- free (pvt);
- localusecnt--;
- ast_update_use_count ();
- }
- static struct ast_translator g726tolin = {
- "g726tolin",
- AST_FORMAT_G726,
- AST_FORMAT_SLINEAR,
- g726tolin_new,
- g726tolin_framein,
- g726tolin_frameout,
- g726_destroy,
-
- g726tolin_sample
- };
- static struct ast_translator lintog726 = {
- "lintog726",
- AST_FORMAT_SLINEAR,
- AST_FORMAT_G726,
- lintog726_new,
- lintog726_framein,
- lintog726_frameout,
- g726_destroy,
-
- lintog726_sample
- };
- int
- unload_module (void)
- {
- int res;
- ast_mutex_lock (&localuser_lock);
- res = ast_unregister_translator (&lintog726);
- if (!res)
- res = ast_unregister_translator (&g726tolin);
- if (localusecnt)
- res = -1;
- ast_mutex_unlock (&localuser_lock);
- return res;
- }
- int
- load_module (void)
- {
- int res;
- res = ast_register_translator (&g726tolin);
- if (!res)
- res = ast_register_translator (&lintog726);
- else
- ast_unregister_translator (&g726tolin);
- return res;
- }
- char *
- description (void)
- {
- return tdesc;
- }
- int
- usecount (void)
- {
- int res;
- STANDARD_USECOUNT (res);
- return res;
- }
- char *
- key ()
- {
- return ASTERISK_GPL_KEY;
- }
|