summaryrefslogtreecommitdiff
path: root/pjmedia/src/pjmedia-codec/g722
diff options
context:
space:
mode:
authorDavid M. Lee <dlee@digium.com>2013-01-07 14:24:28 -0600
committerDavid M. Lee <dlee@digium.com>2013-01-07 14:24:28 -0600
commitf3ab456a17af1c89a6e3be4d20c5944853df1cb0 (patch)
treed00e1a332cd038a6d906a1ea0ac91e1a4458e617 /pjmedia/src/pjmedia-codec/g722
Import pjproject-2.0.1
Diffstat (limited to 'pjmedia/src/pjmedia-codec/g722')
-rw-r--r--pjmedia/src/pjmedia-codec/g722/g722_dec.c549
-rw-r--r--pjmedia/src/pjmedia-codec/g722/g722_dec.h79
-rw-r--r--pjmedia/src/pjmedia-codec/g722/g722_enc.c576
-rw-r--r--pjmedia/src/pjmedia-codec/g722/g722_enc.h78
4 files changed, 1282 insertions, 0 deletions
diff --git a/pjmedia/src/pjmedia-codec/g722/g722_dec.c b/pjmedia/src/pjmedia-codec/g722/g722_dec.c
new file mode 100644
index 0000000..c6edc36
--- /dev/null
+++ b/pjmedia/src/pjmedia-codec/g722/g722_dec.c
@@ -0,0 +1,549 @@
+/* $Id: g722_dec.c 3553 2011-05-05 06:14:19Z nanang $ */
+/*
+ * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
+ * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+/*
+ * Based on implementation found in Carnegie Mellon Speech Group Software
+ * depository (ftp://ftp.cs.cmu.edu/project/fgdata/index.html). No copyright
+ * was claimed in the original source codes.
+ */
+#include <pjmedia/errno.h>
+#include <pj/assert.h>
+#include <pj/pool.h>
+
+#include "g722_dec.h"
+
+#if defined(PJMEDIA_HAS_G722_CODEC) && (PJMEDIA_HAS_G722_CODEC != 0)
+
+#define MODE 1
+
+#define SATURATE(v, max, min) \
+ if (v>max) v = max; \
+ else if (v<min) v = min
+
+extern const int g722_qmf_coeff[24];
+
+static const int qm4[16] =
+{
+ 0, -20456, -12896, -8968,
+ -6288, -4240, -2584, -1200,
+ 20456, 12896, 8968, 6288,
+ 4240, 2584, 1200, 0
+};
+static const int ilb[32] = {
+ 2048, 2093, 2139, 2186, 2233, 2282, 2332,
+ 2383, 2435, 2489, 2543, 2599, 2656, 2714,
+ 2774, 2834, 2896, 2960, 3025, 3091, 3158,
+ 3228, 3298, 3371, 3444, 3520, 3597, 3676,
+ 3756, 3838, 3922, 4008
+};
+
+
+static int block2l (int il, int detl)
+{
+ int dlt ;
+ int ril, wd2 ;
+
+ /* INVQAL */
+ ril = il >> 2 ;
+ wd2 = qm4[ril] ;
+ dlt = (detl * wd2) >> 15 ;
+
+ return (dlt) ;
+}
+
+
+static int block3l (g722_dec_t *dec, int il)
+{
+ int detl ;
+ int ril, il4, wd, wd1, wd2, wd3, nbpl, depl ;
+ static const int wl[8] = {
+ -60, -30, 58, 172, 334, 538, 1198, 3042
+ };
+ static const int rl42[16] = {
+ 0, 7, 6, 5, 4, 3, 2, 1, 7, 6, 5, 4, 3, 2, 1, 0
+ };
+
+ /* LOGSCL */
+ ril = il >> 2 ;
+ il4 = rl42[ril] ;
+ wd = (dec->nbl * 32512) >> 15 ;
+ nbpl = wd + wl[il4] ;
+
+ if (nbpl < 0) nbpl = 0 ;
+ if (nbpl > 18432) nbpl = 18432 ;
+
+ /* SCALEL */
+ wd1 = (nbpl >> 6) & 31 ;
+ wd2 = nbpl >> 11 ;
+ if ((8 - wd2) < 0) wd3 = ilb[wd1] << (wd2 - 8) ;
+ else wd3 = ilb[wd1] >> (8 - wd2) ;
+ depl = wd3 << 2 ;
+
+ /* DELAYA */
+ dec->nbl = nbpl ;
+ /* DELAYL */
+ detl = depl ;
+
+ return (detl) ;
+}
+
+
+static int block4l (g722_dec_t *dec, int dl)
+{
+ int sl = dec->slow ;
+ int i ;
+ int wd, wd1, wd2, wd3, wd4, wd5/*, wd6 */;
+
+ dec->dlt[0] = dl;
+
+ /* RECONS */
+ dec->rlt[0] = sl + dec->dlt[0] ;
+ SATURATE(dec->rlt[0], 32767, -32768);
+
+ /* PARREC */
+ dec->plt[0] = dec->dlt[0] + dec->szl ;
+ SATURATE(dec->plt[0], 32767, -32768);
+
+ /* UPPOL2 */
+ dec->sgl[0] = dec->plt[0] >> 15 ;
+ dec->sgl[1] = dec->plt[1] >> 15 ;
+ dec->sgl[2] = dec->plt[2] >> 15 ;
+
+ wd1 = dec->al[1] << 2;
+ SATURATE(wd1, 32767, -32768);
+
+ if ( dec->sgl[0] == dec->sgl[1] ) wd2 = - wd1 ;
+ else wd2 = wd1 ;
+ if (wd2 > 32767) wd2 = 32767;
+ wd2 = wd2 >> 7 ;
+
+ if ( dec->sgl[0] == dec->sgl[2] ) wd3 = 128 ;
+ else wd3 = - 128 ;
+
+ wd4 = wd2 + wd3 ;
+ wd5 = (dec->al[2] * 32512) >> 15 ;
+
+ dec->apl[2] = wd4 + wd5 ;
+ SATURATE(dec->apl[2], 12288, -12288);
+
+ /* UPPOL1 */
+ dec->sgl[0] = dec->plt[0] >> 15 ;
+ dec->sgl[1] = dec->plt[1] >> 15 ;
+
+ if ( dec->sgl[0] == dec->sgl[1] ) wd1 = 192 ;
+ else wd1 = - 192 ;
+
+ wd2 = (dec->al[1] * 32640) >> 15 ;
+
+ dec->apl[1] = wd1 + wd2 ;
+ SATURATE(dec->apl[1], 32767, -32768);
+
+ wd3 = (15360 - dec->apl[2]) ;
+ SATURATE(wd3, 32767, -32768);
+ if ( dec->apl[1] > wd3) dec->apl[1] = wd3 ;
+ if ( dec->apl[1] < -wd3) dec->apl[1] = -wd3 ;
+
+ /* UPZERO */
+ if ( dec->dlt[0] == 0 ) wd1 = 0 ;
+ else wd1 = 128 ;
+
+ dec->sgl[0] = dec->dlt[0] >> 15 ;
+
+ for ( i = 1; i < 7; i++ ) {
+ dec->sgl[i] = dec->dlt[i] >> 15 ;
+ if ( dec->sgl[i] == dec->sgl[0] ) wd2 = wd1 ;
+ else wd2 = - wd1 ;
+ wd3 = (dec->bl[i] * 32640) >> 15 ;
+ dec->bpl[i] = wd2 + wd3 ;
+ SATURATE(dec->bpl[i], 32767, -32768);
+ }
+
+ /* DELAYA */
+ for ( i = 6; i > 0; i-- ) {
+ dec->dlt[i] = dec->dlt[i-1] ;
+ dec->bl[i] = dec->bpl[i] ;
+ }
+
+ for ( i = 2; i > 0; i-- ) {
+ dec->rlt[i] = dec->rlt[i-1] ;
+ dec->plt[i] = dec->plt[i-1] ;
+ dec->al[i] = dec->apl[i] ;
+ }
+
+ /* FILTEP */
+ wd1 = dec->rlt[1] << 1;
+ SATURATE(wd1, 32767, -32768);
+ wd1 = ( dec->al[1] * wd1 ) >> 15 ;
+
+ wd2 = dec->rlt[2] << 1;
+ SATURATE(wd2, 32767, -32768);
+ wd2 = ( dec->al[2] * wd2 ) >> 15 ;
+
+ dec->spl = wd1 + wd2 ;
+ SATURATE(dec->spl, 32767, -32768);
+
+ /* FILTEZ */
+ dec->szl = 0 ;
+ for (i=6; i>0; i--) {
+ wd = dec->dlt[i] << 1;
+ SATURATE(wd, 32767, -32768);
+ dec->szl += (dec->bl[i] * wd) >> 15 ;
+ SATURATE(dec->szl, 32767, -32768);
+ }
+
+ /* PREDIC */
+ sl = dec->spl + dec->szl ;
+ SATURATE(sl, 32767, -32768);
+
+ return (sl) ;
+}
+
+static int block5l (int ilr, int sl, int detl, int mode)
+{
+ int yl ;
+ int ril, dl, wd2 = 0;
+ static const int qm5[32] = {
+ -280, -280, -23352, -17560,
+ -14120, -11664, -9752, -8184,
+ -6864, -5712, -4696, -3784,
+ -2960, -2208, -1520, -880,
+ 23352, 17560, 14120, 11664,
+ 9752, 8184, 6864, 5712,
+ 4696, 3784, 2960, 2208,
+ 1520, 880, 280, -280
+ };
+ static const int qm6[64] = {
+ -136, -136, -136, -136,
+ -24808, -21904, -19008, -16704,
+ -14984, -13512, -12280, -11192,
+ -10232, -9360, -8576, -7856,
+ -7192, -6576, -6000, -5456,
+ -4944, -4464, -4008, -3576,
+ -3168, -2776, -2400, -2032,
+ -1688, -1360, -1040, -728,
+ 24808, 21904, 19008, 16704,
+ 14984, 13512, 12280, 11192,
+ 10232, 9360, 8576, 7856,
+ 7192, 6576, 6000, 5456,
+ 4944, 4464, 4008, 3576,
+ 3168, 2776, 2400, 2032,
+ 1688, 1360, 1040, 728,
+ 432, 136, -432, -136
+ };
+
+ /* INVQBL */
+ if (mode == 1) {
+ ril = ilr ;
+ wd2 = qm6[ril] ;
+ }
+
+ if (mode == 2) {
+ ril = ilr >> 1 ;
+ wd2 = qm5[ril] ;
+ }
+
+ if (mode == 3) {
+ ril = ilr >> 2 ;
+ wd2 = qm4[ril] ;
+ }
+
+ dl = (detl * wd2 ) >> 15 ;
+
+ /* RECONS */
+ yl = sl + dl ;
+ SATURATE(yl, 32767, -32768);
+
+ return (yl) ;
+}
+
+static int block6l (int yl)
+{
+ int rl ;
+
+ rl = yl ;
+ SATURATE(rl, 16383, -16384);
+
+ return (rl) ;
+}
+
+static int block2h (int ih, int deth)
+{
+ int dh ;
+ int wd2 ;
+ static const int qm2[4] = {-7408, -1616, 7408, 1616} ;
+
+ /* INVQAH */
+ wd2 = qm2[ih] ;
+ dh = (deth * wd2) >> 15 ;
+
+ return (dh) ;
+}
+
+static int block3h (g722_dec_t *dec, int ih)
+{
+ int deth ;
+ int ih2, wd, wd1, wd2, wd3, nbph, deph ;
+ static const int wh[3] = {0, -214, 798} ;
+ static const int rh2[4] = {2, 1, 2, 1} ;
+
+ /* LOGSCH */
+ ih2 = rh2[ih] ;
+ wd = (dec->nbh * 32512) >> 15 ;
+ nbph = wd + wh[ih2] ;
+
+ if (nbph < 0) nbph = 0 ;
+ if (nbph > 22528) nbph = 22528 ;
+
+
+ /* SCALEH */
+ wd1 = (nbph >> 6) & 31 ;
+ wd2 = nbph >> 11 ;
+ if ((10 - wd2) < 0) wd3 = ilb[wd1] << (wd2 - 10) ;
+ else wd3 = ilb[wd1] >> (10 - wd2) ;
+ deph = wd3 << 2 ;
+
+ /* DELAYA */
+ dec->nbh = nbph ;
+
+ /* DELAYH */
+ deth = deph ;
+
+ return (deth) ;
+}
+
+static int block4h (g722_dec_t *dec, int d)
+{
+ int sh = dec->shigh;
+ int i ;
+ int wd, wd1, wd2, wd3, wd4, wd5/*, wd6 */;
+
+ dec->dh[0] = d;
+
+ /* RECONS */
+ dec->rh[0] = sh + dec->dh[0] ;
+ SATURATE(dec->rh[0], 32767, -32768);
+
+ /* PARREC */
+ dec->ph[0] = dec->dh[0] + dec->szh ;
+ SATURATE(dec->ph[0], 32767, -32768);
+
+ /* UPPOL2 */
+ dec->sgh[0] = dec->ph[0] >> 15 ;
+ dec->sgh[1] = dec->ph[1] >> 15 ;
+ dec->sgh[2] = dec->ph[2] >> 15 ;
+
+ wd1 = dec->ah[1] << 2;
+ SATURATE(wd1, 32767, -32768);
+
+ if ( dec->sgh[0] == dec->sgh[1] ) wd2 = - wd1 ;
+ else wd2 = wd1 ;
+ if (wd2 > 32767) wd2 = 32767;
+
+ wd2 = wd2 >> 7 ;
+
+ if ( dec->sgh[0] == dec->sgh[2] ) wd3 = 128 ;
+ else wd3 = - 128 ;
+
+ wd4 = wd2 + wd3 ;
+ wd5 = (dec->ah[2] * 32512) >> 15 ;
+
+ dec->aph[2] = wd4 + wd5 ;
+ SATURATE(dec->aph[2], 12288, -12288);
+
+ /* UPPOL1 */
+ dec->sgh[0] = dec->ph[0] >> 15 ;
+ dec->sgh[1] = dec->ph[1] >> 15 ;
+
+ if ( dec->sgh[0] == dec->sgh[1] ) wd1 = 192 ;
+ else wd1 = - 192 ;
+
+ wd2 = (dec->ah[1] * 32640) >> 15 ;
+
+ dec->aph[1] = wd1 + wd2 ;
+ SATURATE(dec->aph[1], 32767, -32768);
+ //dec->aph[2]?
+ //if (aph[2] > 32767) aph[2] = 32767;
+ //if (aph[2] < -32768) aph[2] = -32768;
+
+ wd3 = (15360 - dec->aph[2]) ;
+ SATURATE(wd3, 32767, -32768);
+ if ( dec->aph[1] > wd3) dec->aph[1] = wd3 ;
+ if ( dec->aph[1] < -wd3) dec->aph[1] = -wd3 ;
+
+ /* UPZERO */
+ if ( dec->dh[0] == 0 ) wd1 = 0 ;
+ if ( dec->dh[0] != 0 ) wd1 = 128 ;
+
+ dec->sgh[0] = dec->dh[0] >> 15 ;
+
+ for ( i = 1; i < 7; i++ ) {
+ dec->sgh[i] = dec->dh[i] >> 15 ;
+ if ( dec->sgh[i] == dec->sgh[0] ) wd2 = wd1 ;
+ else wd2 = - wd1 ;
+ wd3 = (dec->bh[i] * 32640) >> 15 ;
+ dec->bph[i] = wd2 + wd3 ;
+ }
+
+ /* DELAYA */
+ for ( i = 6; i > 0; i-- ) {
+ dec->dh[i] = dec->dh[i-1] ;
+ dec->bh[i] = dec->bph[i] ;
+ }
+
+ for ( i = 2; i > 0; i-- ) {
+ dec->rh[i] = dec->rh[i-1] ;
+ dec->ph[i] = dec->ph[i-1] ;
+ dec->ah[i] = dec->aph[i] ;
+ }
+
+ /* FILTEP */
+ wd1 = dec->rh[1] << 1 ;
+ SATURATE(wd1, 32767, -32768);
+ wd1 = ( dec->ah[1] * wd1 ) >> 15 ;
+
+ wd2 = dec->rh[2] << 1;
+ SATURATE(wd2, 32767, -32768);
+ wd2 = ( dec->ah[2] * wd2 ) >> 15 ;
+
+ dec->sph = wd1 + wd2 ;
+ SATURATE(dec->sph, 32767, -32768);
+
+ /* FILTEZ */
+ dec->szh = 0 ;
+ for (i=6; i>0; i--) {
+ wd = dec->dh[i] << 1;
+ SATURATE(wd, 32767, -32768);
+ dec->szh += (dec->bh[i] * wd) >> 15 ;
+ SATURATE(dec->szh, 32767, -32768);
+ }
+
+ /* PREDIC */
+ sh = dec->sph + dec->szh ;
+ SATURATE(sh, 32767, -32768);
+
+ return (sh) ;
+}
+
+static int block5h (int dh, int sh)
+{
+ int rh ;
+
+ rh = dh + sh;
+ SATURATE(rh, 16383, -16384);
+
+ return (rh) ;
+}
+
+void rx_qmf(g722_dec_t *dec, int rl, int rh, int *xout1, int *xout2)
+{
+ int i;
+
+ pj_memmove(&dec->xd[1], dec->xd, 11*sizeof(dec->xd[0]));
+ pj_memmove(&dec->xs[1], dec->xs, 11*sizeof(dec->xs[0]));
+
+ /* RECA */
+ dec->xd[0] = rl - rh ;
+ if (dec->xd[0] > 16383) dec->xd[0] = 16383;
+ else if (dec->xd[0] < -16384) dec->xd[0] = -16384;
+
+ /* RECB */
+ dec->xs[0] = rl + rh ;
+ if (dec->xs[0] > 16383) dec->xs[0] = 16383;
+ else if (dec->xs[0] < -16384) dec->xs[0] = -16384;
+
+ /* ACCUMC */
+ *xout1 = 0;
+ for (i=0; i<12; ++i) *xout1 += dec->xd[i] * g722_qmf_coeff[2*i];
+ *xout1 = *xout1 >> 12 ;
+ if (*xout1 > 16383) *xout1 = 16383 ;
+ else if (*xout1 < -16384) *xout1 = -16384 ;
+
+ /* ACCUMD */
+ *xout2 = 0;
+ for (i=0; i<12; ++i) *xout2 += dec->xs[i] * g722_qmf_coeff[2*i+1];
+ *xout2 = *xout2 >> 12 ;
+ if (*xout2 > 16383) *xout2 = 16383 ;
+ else if (*xout2 < -16384) *xout2 = -16384 ;
+}
+
+
+PJ_DEF(pj_status_t) g722_dec_init(g722_dec_t *dec)
+{
+ PJ_ASSERT_RETURN(dec, PJ_EINVAL);
+
+ pj_bzero(dec, sizeof(g722_dec_t));
+
+ dec->detlow = 32;
+ dec->dethigh = 8;
+
+ return PJ_SUCCESS;
+}
+
+PJ_DEF(pj_status_t) g722_dec_decode( g722_dec_t *dec,
+ void *in,
+ pj_size_t in_size,
+ pj_int16_t out[],
+ pj_size_t *nsamples)
+{
+ unsigned i;
+ int ilowr, ylow, rlow, dlowt;
+ int ihigh, rhigh, dhigh;
+ int pcm1, pcm2;
+ pj_uint8_t *in_ = (pj_uint8_t*) in;
+
+ PJ_ASSERT_RETURN(dec && in && in_size && out && nsamples, PJ_EINVAL);
+ PJ_ASSERT_RETURN(*nsamples >= (in_size << 1), PJ_ETOOSMALL);
+
+ for(i = 0; i < in_size; ++i) {
+ ilowr = in_[i] & 63;
+ ihigh = (in_[i] >> 6) & 3;
+
+ /* low band decoder */
+ ylow = block5l (ilowr, dec->slow, dec->detlow, MODE) ;
+ rlow = block6l (ylow) ;
+ dlowt = block2l (ilowr, dec->detlow) ;
+ dec->detlow = block3l (dec, ilowr) ;
+ dec->slow = block4l (dec, dlowt) ;
+ /* rlow <= output low band pcm */
+
+ /* high band decoder */
+ dhigh = block2h (ihigh, dec->dethigh) ;
+ rhigh = block5h (dhigh, dec->shigh) ;
+ dec->dethigh = block3h (dec, ihigh) ;
+ dec->shigh = block4h (dec, dhigh) ;
+ /* rhigh <= output high band pcm */
+
+ rx_qmf(dec, rlow, rhigh, &pcm1, &pcm2);
+ out[i*2] = (pj_int16_t)pcm1;
+ out[i*2+1] = (pj_int16_t)pcm2;
+ }
+
+ *nsamples = in_size << 1;
+
+ return PJ_SUCCESS;
+}
+
+PJ_DEF(pj_status_t) g722_dec_deinit(g722_dec_t *dec)
+{
+ pj_bzero(dec, sizeof(g722_dec_t));
+
+ return PJ_SUCCESS;
+}
+
+#endif
diff --git a/pjmedia/src/pjmedia-codec/g722/g722_dec.h b/pjmedia/src/pjmedia-codec/g722/g722_dec.h
new file mode 100644
index 0000000..3cf841e
--- /dev/null
+++ b/pjmedia/src/pjmedia-codec/g722/g722_dec.h
@@ -0,0 +1,79 @@
+/* $Id: g722_dec.h 3553 2011-05-05 06:14:19Z nanang $ */
+/*
+ * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
+ * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+/*
+ * Based on implementation found in Carnegie Mellon Speech Group Software
+ * depository (ftp://ftp.cs.cmu.edu/project/fgdata/index.html). No copyright
+ * was claimed in the original source codes.
+ */
+#ifndef __PJMEDIA_CODEC_G722_DEC_H__
+#define __PJMEDIA_CODEC_G722_DEC_H__
+
+#include <pjmedia-codec/types.h>
+
+/* Decoder state */
+typedef struct g722_dec_t {
+ /* PCM low band */
+ int slow;
+ int detlow;
+ int spl;
+ int szl;
+ int rlt [3];
+ int al [3];
+ int apl [3];
+ int plt [3];
+ int dlt [7];
+ int bl [7];
+ int bpl [7];
+ int sgl [7];
+ int nbl;
+
+ /* PCM high band*/
+ int shigh;
+ int dethigh;
+ int sph;
+ int szh;
+ int rh [3];
+ int ah [3];
+ int aph [3];
+ int ph [3];
+ int dh [7];
+ int bh [7];
+ int bph [7];
+ int sgh [7];
+ int nbh;
+
+ /* QMF signal history */
+ int xd[12];
+ int xs[12];
+} g722_dec_t;
+
+
+PJ_DECL(pj_status_t) g722_dec_init(g722_dec_t *dec);
+
+PJ_DECL(pj_status_t) g722_dec_decode(g722_dec_t *dec,
+ void *in,
+ pj_size_t in_size,
+ pj_int16_t out[],
+ pj_size_t *nsamples);
+
+PJ_DECL(pj_status_t) g722_dec_deinit(g722_dec_t *dec);
+
+#endif /* __PJMEDIA_CODEC_G722_DEC_H__ */
+
diff --git a/pjmedia/src/pjmedia-codec/g722/g722_enc.c b/pjmedia/src/pjmedia-codec/g722/g722_enc.c
new file mode 100644
index 0000000..d0b2011
--- /dev/null
+++ b/pjmedia/src/pjmedia-codec/g722/g722_enc.c
@@ -0,0 +1,576 @@
+/* $Id: g722_enc.c 3553 2011-05-05 06:14:19Z nanang $ */
+/*
+ * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
+ * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+/*
+ * Based on implementation found in Carnegie Mellon Speech Group Software
+ * depository (ftp://ftp.cs.cmu.edu/project/fgdata/index.html). No copyright
+ * was claimed in the original source codes.
+ */
+#include <pjmedia/errno.h>
+#include <pj/assert.h>
+#include <pj/pool.h>
+
+#include "g722_enc.h"
+
+#if defined(PJMEDIA_HAS_G722_CODEC) && (PJMEDIA_HAS_G722_CODEC != 0)
+
+#define SATURATE(v, max, min) \
+ if (v>max) v = max; \
+ else if (v<min) v = min
+
+/* QMF tap coefficients */
+const int g722_qmf_coeff[24] = {
+ 3, -11, -11, 53, 12, -156,
+ 32, 362, -210, -805, 951, 3876,
+ 3876, 951, -805, -210, 362, 32,
+ -156, 12, 53, -11, -11, 3
+};
+
+
+static int block1l (int xl, int sl, int detl)
+{
+ int il ;
+
+ int i, el, sil, mil, wd, wd1, hdu ;
+
+ static const int q6[32] = {
+ 0, 35, 72, 110, 150, 190, 233, 276, 323,
+ 370, 422, 473, 530, 587, 650, 714, 786,
+ 858, 940, 1023, 1121, 1219, 1339, 1458,
+ 1612, 1765, 1980, 2195, 2557, 2919, 0, 0
+ };
+
+ static const int iln[32] = {
+ 0, 63, 62, 31, 30, 29, 28, 27, 26, 25,
+ 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14,
+ 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 0
+ };
+
+ static const int ilp[32] = {
+ 0, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52,
+ 51, 50, 49, 48, 47, 46, 45, 44, 43, 42, 41,
+ 40, 39, 38, 37, 36, 35, 34, 33, 32, 0
+ };
+
+ /* SUBTRA */
+
+ el = xl - sl ;
+ SATURATE(el, 32767, -32768);
+
+ /* QUANTL */
+
+ sil = el >> 15 ;
+ if (sil == 0 ) wd = el ;
+ else wd = (32767 - el) & 32767 ;
+
+ mil = 1 ;
+
+ for (i = 1; i < 30; i++) {
+ hdu = (q6[i] << 3) * detl;
+ wd1 = (hdu >> 15) ;
+ if (wd >= wd1) mil = (i + 1) ;
+ else break ;
+ }
+
+ if (sil == -1 ) il = iln[mil] ;
+ else il = ilp[mil] ;
+
+ return (il) ;
+}
+
+static int block2l (int il, int detl)
+{
+ int dlt;
+ int ril, wd2 ;
+ static const int qm4[16] = {
+ 0, -20456, -12896, -8968,
+ -6288, -4240, -2584, -1200,
+ 20456, 12896, 8968, 6288,
+ 4240, 2584, 1200, 0
+ };
+
+ /* INVQAL */
+ ril = il >> 2 ;
+ wd2 = qm4[ril] ;
+ dlt = (detl * wd2) >> 15 ;
+
+ return (dlt) ;
+}
+
+static int block3l (g722_enc_t *enc, int il)
+{
+ int detl;
+ int ril, il4, wd, wd1, wd2, wd3, nbpl, depl ;
+ static int const wl[8] = {
+ -60, -30, 58, 172, 334, 538, 1198, 3042
+ } ;
+ static int const rl42[16] = {
+ 0, 7, 6, 5, 4, 3, 2, 1, 7, 6, 5, 4, 3, 2, 1, 0
+ };
+ static const int ilb[32] = {
+ 2048, 2093, 2139, 2186, 2233, 2282, 2332,
+ 2383, 2435, 2489, 2543, 2599, 2656, 2714,
+ 2774, 2834, 2896, 2960, 3025, 3091, 3158,
+ 3228, 3298, 3371, 3444, 3520, 3597, 3676,
+ 3756, 3838, 3922, 4008
+ };
+
+ /* LOGSCL */
+
+ ril = il >> 2 ;
+ il4 = rl42[ril] ;
+
+ wd = (enc->nbl * 32512) >> 15 ;
+ nbpl = wd + wl[il4] ;
+
+ if (nbpl < 0) nbpl = 0 ;
+ if (nbpl > 18432) nbpl = 18432 ;
+
+ /* SCALEL */
+
+ wd1 = (nbpl >> 6) & 31 ;
+ wd2 = nbpl >> 11 ;
+ if ((8 - wd2) < 0) wd3 = ilb[wd1] << (wd2 - 8) ;
+ else wd3 = ilb[wd1] >> (8 - wd2) ;
+ depl = wd3 << 2 ;
+
+ /* DELAYA */
+ enc->nbl = nbpl ;
+
+ /* DELAYL */
+ detl = depl ;
+
+#ifdef DEBUG_VERBOSE
+ printf ("BLOCK3L il=%4d, ril=%4d, il4=%4d, nbl=%4d, wd=%4d, nbpl=%4d\n",
+ il, ril, il4, enc->nbl, wd, nbpl) ;
+ printf ("wd1=%4d, wd2=%4d, wd3=%4d, depl=%4d, detl=%4d\n",
+ wd1, wd2, wd3, depl, detl) ;
+#endif
+
+ return (detl) ;
+}
+
+static int block4l (g722_enc_t *enc, int dl)
+{
+ int sl = enc->slow;
+ int i ;
+ int wd, wd1, wd2, wd3, wd4, wd5 /*, wd6 */;
+
+ enc->dlt[0] = dl;
+
+ /* RECONS */
+
+ enc->rlt[0] = sl + enc->dlt[0] ;
+ SATURATE(enc->rlt[0], 32767, -32768);
+
+ /* PARREC */
+
+ enc->plt[0] = enc->dlt[0] + enc->szl ;
+ SATURATE(enc->plt[0], 32767, -32768);
+
+ /* UPPOL2 */
+
+ enc->sgl[0] = enc->plt[0] >> 15 ;
+ enc->sgl[1] = enc->plt[1] >> 15 ;
+ enc->sgl[2] = enc->plt[2] >> 15 ;
+
+ wd1 = enc->al[1] << 2;
+ SATURATE(wd1, 32767, -32768);
+
+ if ( enc->sgl[0] == enc->sgl[1] ) wd2 = - wd1 ;
+ else wd2 = wd1 ;
+ if ( wd2 > 32767 ) wd2 = 32767;
+
+ wd2 = wd2 >> 7 ;
+
+ if ( enc->sgl[0] == enc->sgl[2] ) wd3 = 128 ;
+ else wd3 = - 128 ;
+
+ wd4 = wd2 + wd3 ;
+ wd5 = (enc->al[2] * 32512) >> 15 ;
+
+ enc->apl[2] = wd4 + wd5 ;
+ SATURATE(enc->apl[2], 12288, -12288);
+
+ /* UPPOL1 */
+
+ enc->sgl[0] = enc->plt[0] >> 15 ;
+ enc->sgl[1] = enc->plt[1] >> 15 ;
+
+ if ( enc->sgl[0] == enc->sgl[1] ) wd1 = 192 ;
+ else wd1 = - 192 ;
+
+ wd2 = (enc->al[1] * 32640) >> 15 ;
+
+ enc->apl[1] = wd1 + wd2 ;
+ SATURATE(enc->apl[1], 32767, -32768);
+
+ wd3 = (15360 - enc->apl[2]) ;
+ SATURATE(wd3, 32767, -32768);
+
+ if ( enc->apl[1] > wd3) enc->apl[1] = wd3 ;
+ if ( enc->apl[1] < -wd3) enc->apl[1] = -wd3 ;
+
+ /* UPZERO */
+
+ if ( enc->dlt[0] == 0 ) wd1 = 0 ;
+ else wd1 = 128 ;
+
+ enc->sgl[0] = enc->dlt[0] >> 15 ;
+
+ for ( i = 1; i < 7; i++ ) {
+ enc->sgl[i] = enc->dlt[i] >> 15 ;
+ if ( enc->sgl[i] == enc->sgl[0] ) wd2 = wd1 ;
+ else wd2 = - wd1 ;
+ wd3 = (enc->bl[i] * 32640) >> 15 ;
+ enc->bpl[i] = wd2 + wd3 ;
+ SATURATE(enc->bpl[i], 32767, -32768);
+ }
+
+ /* DELAYA */
+
+ for ( i = 6; i > 0; i-- ) {
+ enc->dlt[i] = enc->dlt[i-1] ;
+ enc->bl[i] = enc->bpl[i] ;
+ }
+
+ for ( i = 2; i > 0; i-- ) {
+ enc->rlt[i] = enc->rlt[i-1] ;
+ enc->plt[i] = enc->plt[i-1] ;
+ enc->al[i] = enc->apl[i] ;
+ }
+
+ /* FILTEP */
+
+ wd1 = enc->rlt[1] + enc->rlt[1];
+ SATURATE(wd1, 32767, -32768);
+ wd1 = ( enc->al[1] * wd1 ) >> 15 ;
+
+ wd2 = enc->rlt[2] + enc->rlt[2];
+ SATURATE(wd2, 32767, -32768);
+ wd2 = ( enc->al[2] * wd2 ) >> 15 ;
+
+ enc->spl = wd1 + wd2 ;
+ SATURATE(enc->spl, 32767, -32768);
+
+ /* FILTEZ */
+
+ enc->szl = 0 ;
+ for (i=6; i>0; i--) {
+ wd = enc->dlt[i] + enc->dlt[i];
+ SATURATE(wd, 32767, -32768);
+ enc->szl += (enc->bl[i] * wd) >> 15 ;
+ SATURATE(enc->szl, 32767, -32768);
+ }
+
+ /* PREDIC */
+
+ sl = enc->spl + enc->szl ;
+ SATURATE(sl, 32767, -32768);
+
+ return (sl) ;
+}
+
+static int block1h (int xh, int sh, int deth)
+{
+ int ih ;
+
+ int eh, sih, mih, wd, wd1, hdu ;
+
+ static const int ihn[3] = { 0, 1, 0 } ;
+ static const int ihp[3] = { 0, 3, 2 } ;
+
+ /* SUBTRA */
+
+ eh = xh - sh ;
+ SATURATE(eh, 32767, -32768);
+
+ /* QUANTH */
+
+ sih = eh >> 15 ;
+ if (sih == 0 ) wd = eh ;
+ else wd = (32767 - eh) & 32767 ;
+
+ hdu = (564 << 3) * deth;
+ wd1 = (hdu >> 15) ;
+ if (wd >= wd1) mih = 2 ;
+ else mih = 1 ;
+
+ if (sih == -1 ) ih = ihn[mih] ;
+ else ih = ihp[mih] ;
+
+ return (ih) ;
+}
+
+static int block2h (int ih, int deth)
+{
+ int dh ;
+ int wd2 ;
+ static const int qm2[4] = {-7408, -1616, 7408, 1616};
+
+ /* INVQAH */
+
+ wd2 = qm2[ih] ;
+ dh = (deth * wd2) >> 15 ;
+
+ return (dh) ;
+}
+
+static int block3h (g722_enc_t *enc, int ih)
+{
+ int deth ;
+ int ih2, wd, wd1, wd2, wd3, nbph, deph ;
+ static const int wh[3] = {0, -214, 798} ;
+ static const int rh2[4] = {2, 1, 2, 1} ;
+ static const int ilb[32] = {
+ 2048, 2093, 2139, 2186, 2233, 2282, 2332,
+ 2383, 2435, 2489, 2543, 2599, 2656, 2714,
+ 2774, 2834, 2896, 2960, 3025, 3091, 3158,
+ 3228, 3298, 3371, 3444, 3520, 3597, 3676,
+ 3756, 3838, 3922, 4008
+ };
+
+ /* LOGSCH */
+
+ ih2 = rh2[ih] ;
+ wd = (enc->nbh * 32512) >> 15 ;
+ nbph = wd + wh[ih2] ;
+
+ if (nbph < 0) nbph = 0 ;
+ if (nbph > 22528) nbph = 22528 ;
+
+ /* SCALEH */
+
+ wd1 = (nbph >> 6) & 31 ;
+ wd2 = nbph >> 11 ;
+ if ((10-wd2) < 0) wd3 = ilb[wd1] << (wd2-10) ;
+ else wd3 = ilb[wd1] >> (10-wd2) ;
+ deph = wd3 << 2 ;
+
+ /* DELAYA */
+ enc->nbh = nbph ;
+ /* DELAYH */
+ deth = deph ;
+
+ return (deth) ;
+}
+
+static int block4h (g722_enc_t *enc, int d)
+{
+ int sh = enc->shigh;
+ int i ;
+ int wd, wd1, wd2, wd3, wd4, wd5 /*, wd6 */;
+
+ enc->dh[0] = d;
+
+ /* RECONS */
+
+ enc->rh[0] = sh + enc->dh[0] ;
+ SATURATE(enc->rh[0], 32767, -32768);
+
+ /* PARREC */
+
+ enc->ph[0] = enc->dh[0] + enc->szh ;
+ SATURATE(enc->ph[0], 32767, -32768);
+
+ /* UPPOL2 */
+
+ enc->sgh[0] = enc->ph[0] >> 15 ;
+ enc->sgh[1] = enc->ph[1] >> 15 ;
+ enc->sgh[2] = enc->ph[2] >> 15 ;
+
+ wd1 = enc->ah[1] << 2;
+ SATURATE(wd1, 32767, -32768);
+
+ if ( enc->sgh[0] == enc->sgh[1] ) wd2 = - wd1 ;
+ else wd2 = wd1 ;
+ if ( wd2 > 32767 ) wd2 = 32767;
+
+ wd2 = wd2 >> 7 ;
+
+ if ( enc->sgh[0] == enc->sgh[2] ) wd3 = 128 ;
+ else wd3 = - 128 ;
+
+ wd4 = wd2 + wd3 ;
+ wd5 = (enc->ah[2] * 32512) >> 15 ;
+
+ enc->aph[2] = wd4 + wd5 ;
+ SATURATE(enc->aph[2], 12288, -12288);
+
+ /* UPPOL1 */
+
+ enc->sgh[0] = enc->ph[0] >> 15 ;
+ enc->sgh[1] = enc->ph[1] >> 15 ;
+
+ if ( enc->sgh[0] == enc->sgh[1] ) wd1 = 192 ;
+ else wd1 = - 192 ;
+
+ wd2 = (enc->ah[1] * 32640) >> 15 ;
+
+ enc->aph[1] = wd1 + wd2 ;
+ SATURATE(enc->aph[1], 32767, -32768);
+
+ wd3 = (15360 - enc->aph[2]) ;
+ SATURATE(wd3, 32767, -32768);
+
+ if ( enc->aph[1] > wd3) enc->aph[1] = wd3 ;
+ else if ( enc->aph[1] < -wd3) enc->aph[1] = -wd3 ;
+
+ /* UPZERO */
+
+ if ( enc->dh[0] == 0 ) wd1 = 0 ;
+ else wd1 = 128 ;
+
+ enc->sgh[0] = enc->dh[0] >> 15 ;
+
+ for ( i = 1; i < 7; i++ ) {
+ enc->sgh[i] = enc->dh[i] >> 15 ;
+ if ( enc->sgh[i] == enc->sgh[0] ) wd2 = wd1 ;
+ else wd2 = - wd1 ;
+ wd3 = (enc->bh[i] * 32640) >> 15 ;
+ enc->bph[i] = wd2 + wd3 ;
+ SATURATE(enc->bph[i], 32767, -32768);
+ }
+
+ /* DELAYA */
+ for ( i = 6; i > 0; i-- ) {
+ enc->dh[i] = enc->dh[i-1] ;
+ enc->bh[i] = enc->bph[i] ;
+ }
+
+ for ( i = 2; i > 0; i-- ) {
+ enc->rh[i] = enc->rh[i-1] ;
+ enc->ph[i] = enc->ph[i-1] ;
+ enc->ah[i] = enc->aph[i] ;
+ }
+
+ /* FILTEP */
+
+ wd1 = enc->rh[1] + enc->rh[1];
+ SATURATE(wd1, 32767, -32768);
+ wd1 = ( enc->ah[1] * wd1 ) >> 15 ;
+
+ wd2 = enc->rh[2] + enc->rh[2];
+ SATURATE(wd2, 32767, -32768);
+ wd2 = ( enc->ah[2] * wd2 ) >> 15 ;
+
+ enc->sph = wd1 + wd2 ;
+ SATURATE(enc->sph, 32767, -32768);
+
+ /* FILTEZ */
+
+ enc->szh = 0 ;
+ for (i=6; i>0; i--) {
+ wd = enc->dh[i] + enc->dh[i];
+ SATURATE(wd, 32767, -32768);
+ enc->szh += (enc->bh[i] * wd) >> 15 ;
+ SATURATE(enc->szh, 32767, -32768);
+ }
+
+ /* PREDIC */
+
+ sh = enc->sph + enc->szh ;
+ SATURATE(sh, 32767, -32768);
+
+ return (sh) ;
+}
+
+/* PROCESS PCM THROUGH THE QMF FILTER */
+static void tx_qmf(g722_enc_t *enc, int pcm1, int pcm2, int *lo, int *hi)
+{
+ int sumodd, sumeven;
+ int i;
+
+ pj_memmove(&enc->x[2], enc->x, 22 * sizeof(enc->x[0]));
+ enc->x[1] = pcm1;
+ enc->x[0] = pcm2;
+
+ sumodd = 0;
+ for (i=1; i<24; i+=2) sumodd += enc->x[i] * g722_qmf_coeff[i];
+
+ sumeven = 0;
+ for (i=0; i<24; i+=2) sumeven += enc->x[i] * g722_qmf_coeff[i];
+
+ *lo = (sumeven + sumodd) >> 13 ;
+ *hi = (sumeven - sumodd) >> 13 ;
+
+ SATURATE(*lo, 16383, -16384);
+ SATURATE(*hi, 16383, -16383);
+}
+
+
+PJ_DEF(pj_status_t) g722_enc_init(g722_enc_t *enc)
+{
+ PJ_ASSERT_RETURN(enc, PJ_EINVAL);
+
+ pj_bzero(enc, sizeof(g722_enc_t));
+
+ enc->detlow = 32;
+ enc->dethigh = 8;
+
+ return PJ_SUCCESS;
+}
+
+PJ_DEF(pj_status_t) g722_enc_encode( g722_enc_t *enc,
+ pj_int16_t in[],
+ pj_size_t nsamples,
+ void *out,
+ pj_size_t *out_size)
+{
+ unsigned i;
+ int xlow, ilow, dlowt;
+ int xhigh, ihigh, dhigh;
+ pj_uint8_t *out_ = (pj_uint8_t*) out;
+
+ PJ_ASSERT_RETURN(enc && in && nsamples && out && out_size, PJ_EINVAL);
+ PJ_ASSERT_RETURN(nsamples % 2 == 0, PJ_EINVAL);
+ PJ_ASSERT_RETURN(*out_size >= (nsamples >> 1), PJ_ETOOSMALL);
+
+ for(i = 0; i < nsamples; i += 2) {
+ tx_qmf(enc, in[i], in[i+1], &xlow, &xhigh);
+
+ /* low band encoder */
+ ilow = block1l (xlow, enc->slow, enc->detlow) ;
+ dlowt = block2l (ilow, enc->detlow) ;
+ enc->detlow = block3l (enc, ilow) ;
+ enc->slow = block4l (enc, dlowt) ;
+
+ /* high band encoder */
+ ihigh = block1h (xhigh, enc->shigh, enc->dethigh) ;
+ dhigh = block2h (ihigh, enc->dethigh) ;
+ enc->dethigh = block3h (enc, ihigh) ;
+ enc->shigh = block4h (enc, dhigh) ;
+
+ /* bits mix low & high adpcm */
+ out_[i/2] = (pj_uint8_t)((ihigh << 6) | ilow);
+ }
+
+ *out_size = nsamples >> 1;
+
+ return PJ_SUCCESS;
+}
+
+PJ_DEF(pj_status_t) g722_enc_deinit(g722_enc_t *enc)
+{
+ pj_bzero(enc, sizeof(g722_enc_t));
+
+ return PJ_SUCCESS;
+}
+
+#endif
diff --git a/pjmedia/src/pjmedia-codec/g722/g722_enc.h b/pjmedia/src/pjmedia-codec/g722/g722_enc.h
new file mode 100644
index 0000000..96f9af6
--- /dev/null
+++ b/pjmedia/src/pjmedia-codec/g722/g722_enc.h
@@ -0,0 +1,78 @@
+/* $Id: g722_enc.h 3553 2011-05-05 06:14:19Z nanang $ */
+/*
+ * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
+ * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+/*
+ * Based on implementation found in Carnegie Mellon Speech Group Software
+ * depository (ftp://ftp.cs.cmu.edu/project/fgdata/index.html). No copyright
+ * was claimed in the original source codes.
+ */
+#ifndef __PJMEDIA_CODEC_G722_ENC_H__
+#define __PJMEDIA_CODEC_G722_ENC_H__
+
+#include <pjmedia-codec/types.h>
+
+/* Encoder state */
+typedef struct g722_enc_t {
+ /* PCM low band */
+ int slow;
+ int detlow;
+ int spl;
+ int szl;
+ int rlt [3];
+ int al [3];
+ int apl [3];
+ int plt [3];
+ int dlt [7];
+ int bl [7];
+ int bpl [7];
+ int sgl [7];
+ int nbl;
+
+ /* PCM high band*/
+ int shigh;
+ int dethigh;
+ int sph;
+ int szh;
+ int rh [3];
+ int ah [3];
+ int aph [3];
+ int ph [3];
+ int dh [7];
+ int bh [7];
+ int bph [7];
+ int sgh [7];
+ int nbh;
+
+ /* QMF signal history */
+ int x[24];
+} g722_enc_t;
+
+
+PJ_DECL(pj_status_t) g722_enc_init(g722_enc_t *enc);
+
+PJ_DECL(pj_status_t) g722_enc_encode(g722_enc_t *enc,
+ pj_int16_t in[],
+ pj_size_t nsamples,
+ void *out,
+ pj_size_t *out_size);
+
+PJ_DECL(pj_status_t) g722_enc_deinit(g722_enc_t *enc);
+
+#endif /* __PJMEDIA_CODEC_G722_ENC_H__ */
+