?? g722.cpp
字號:
/**********************************************************************
codec_ftns.c - Encoder and Decoder functions.
***********************************************************************/
#include <math.h>
/******** Function Declarations ********/
int encode(int,int);
void decode(int);
int filtez(int *bpl,int *dlt);
void upzero(int dlt,int *dlti,int *bli);
int filtep(int rlt1,int al1,int rlt2,int al2);
int quantl(int el,int detl);
int logscl(int il,int nbl);
int scalel(int nbl,int shift_constant);
int uppol2(int al1,int al2,int plt,int plt1,int plt2);
int uppol1(int al1,int apl2,int plt,int plt1);
int logsch(int ih,int nbh);
void reset();
/******** Transmit QMF Variable Declarations ********/
int tqmf[24]; /* TQMF delay samples */
int h[24] = { /* TQMF filter coeficients */
12, -44, -44, 212, 48, -624, 128, 1448,
-840, -3220, 3804, 15504, 15504, 3804, -3220, -840,
1448, 128, -624, 48, 212, -44, -44, 12
};
/******** Receive QMF Variable Declarations ********/
int accumc[11],accumd[11]; /* Receive QMF Accumulators */
int xout1,xout2; /* outputs of decode */
int slout, xlout;
/******** Lo and Hi Band Encoder Variable Declarations ********/
/* Quantizer Lookup Tables */
int decis_levl[30] = { /* Decision Levels: Pre-multiplied by 8 */
280, 576, 880, 1200, 1520, 1864, 2208, 2584,
2960, 3376, 3784, 4240, 4696, 5200, 5712, 6288,
6864, 7520, 8184, 8968, 9752, 10712, 11664, 12896,
14120, 15840, 17560, 20456, 23352, 32767
};
int quant26bt_pos[31] = { /* 6-bit positive quantization values */
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, 32
};
int quant26bt_neg[31] = { /* 6-bit negative quantization values */
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, 4
};
/* Inverse Quantizer Lookup Tables */
int qq4_code4_table[16] = {
0, -20456, -12896, -8968, -6288, -4240, -2584, -1200,
20456, 12896, 8968, 6288, 4240, 2584, 1200, 0
};
int qq5_code5_table[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
};
int qq6_code6_table[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
};
int qq2_code2_table[4] = {
-7408, -1616, 7408, 1616
};
/* Quantizer Adaptation Values */
int wl_code_table[16] = {
-60, 3042, 1198, 538, 334, 172, 58, -30,
3042, 1198, 538, 334, 172, 58, -30, -60
};
int wh_code_table[4] = {
798, -214, 798, -214
};
int wl_table[8] = {
-60, -30, 58, 172, 334, 538, 1198, 3042
};
int ilb_table[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
};
int delay_bpl[6];
int delay_dltx[6];
int nbl;
int al1,al2;
int plt,plt1,plt2;
int rs;
int dlt;
int rlt,rlt1,rlt2;
int detl;
int delay_dhx[6];
int delay_bph[6];
int nbh;
int ah1,ah2;
int ph1,ph2;
int rh1,rh2;
int deth;
/******** Lo and Hi Band Decoder Variable Declarations ********/
int dec_deth,dec_detl,dec_dlt;
int dec_del_bpl[6];
int dec_del_dltx[6];
int dec_plt,dec_plt1,dec_plt2;
int dec_szl,dec_spl,dec_sl;
int dec_rlt1,dec_rlt2,dec_rlt;
int dec_al1,dec_al2;
int dec_nbl,dec_yh,dec_dh,dec_nbh;
/******** filtez Variable Declarations ********/
int dec_del_bph[6];
int dec_del_dhx[6];
int dec_szh;
/******** filtep Variable Declarations ********/
int dec_rh1,dec_rh2;
int dec_ah1,dec_ah2;
int dec_ph,dec_sph;
int dec_sh,dec_rh;
int dec_ph1,dec_ph2;
/***************************************************************
encode: input samples in xin1 = first value, xin2 = second value
performs the encoder processing.
returns il and ih stored together
***************************************************************/
int encode(int xin1,int xin2)
{
int i; /* tqmf index */
int *h_ptr; /* pointer to tqmf coeficients */
int *tqmf_ptr,*tqmf_ptr1; /* pointers to tqmf delay values */
long int xa,xb; /* odd and even values */
int xl,xh; /* low and high bands */
int decis;
int sh; /* comes from adaptive predictor */
int eh; /* hi band error */
int dh;
int il,ih; /* encoder outputs */
int szh,sph,ph,yh; /* high band predictor outputs */
int szl,spl,sl,el; /* low band predictor outputs */
/*BEGIN: Transmit Quadrature Mirror Filter*/
h_ptr = h;
tqmf_ptr = tqmf;
/* first multiply and accumulate */
xa = (long)(*tqmf_ptr++) * (*h_ptr++);
xb = (long)(*tqmf_ptr++) * (*h_ptr++);
/* multiply and accumulate loop */
for(i=0 ; i<10 ; i++) {
xa += (long)(*tqmf_ptr++) * (*h_ptr++);
xb += (long)(*tqmf_ptr++) * (*h_ptr++);
}
/* final multiply and accumulate */
xa += (long)(*tqmf_ptr++) * (*h_ptr++);
xb += (long)(*tqmf_ptr) * (*h_ptr++);
/* update delay line tqmf with new samples */
tqmf_ptr1 = tqmf_ptr - 2;
for(i = 0 ; i < 22 ; i++) *tqmf_ptr-- = *tqmf_ptr1--;
*tqmf_ptr-- = xin1;
*tqmf_ptr = xin2;
/* combine filter outputs to produce hi/lo sub-bands */
xl = (xa + xb) >> 15;
xh = (xa - xb) >> 15;
xlout = xl;
/*END: Transmit Quadrature Mirror Filter*/
/*BEGIN: Lo Sub-Band Encoder*/
/* compute the predictor output zero section */
szl = filtez(delay_bpl, delay_dltx);
/* compute the predictor output pole section */
spl = filtep(rlt1,al1,rlt2,al2);
/* compute the predictor output value */
sl = szl + spl;
slout = sl;
/* compute the error signal el(n) */
el = xl - sl;
/* adaptive quantizer qmplementation */
il = quantl(el,detl);
/* inverse adaptive quantizer implementation */
dlt = ((long)detl*qq4_code4_table[il >> 2]) >> 15;
/* quantizer adaptation implementation */
/* first compute the log scaling factor nbl */
nbl = logscl(il, nbl);
/* then compute the linear scaling factor detl */
/* 8 is a scale factor so that scalel can be used */
/* for the hi-band encoder */
detl = scalel(nbl,8);
/* adaptive prediction implementation */
/* compute the partial reconstructed signal */
plt = dlt + szl;
/* update the zero section predictor coefficients */
upzero(dlt,delay_dltx,delay_bpl);
/* update second pole section predictor coefficient */
al2 = uppol2(al1,al2,plt,plt1,plt2);
/* update first pole section predictor coefficient */
al1 = uppol1(al1,al2,plt,plt1);
/* compute the quantized recontructed signal for adaptive predic */
rlt = sl + dlt;
/* implement delays for next time */
rlt2 = rlt1;
rlt1 = rlt;
plt2 = plt1;
plt1 = plt;
/*END: Lo Sub-Band Encoder*/
/*BEGIN: Hi Sub-Band Encoder*/
/* compute the predictor output zero section */
szh = filtez(delay_bph,delay_dhx);
/* compute the predictor output pole section */
sph = filtep(rh1,ah1,rh2,ah2);
/* compute the predictor output value */
sh = sph + szh;
/* compute the error signal eh(n) */
eh = xh - sh;
/* adaptive quantizer qmplementation */
if(eh >= 0) {
ih = 3; /* 2,3 are pos codes */
}
else {
ih = 1; /* 0,1 are neg codes */
}
decis = (564L*(long)deth) >> 12L;
if(abs(eh) > decis) ih--; /* mih = 2 case */
/* inverse adaptive quantizer implementation */
dh = ((long)deth*qq2_code2_table[ih]) >> 15L ;
/* quantizer adaptation implementation */
/* first compute the log scaling factor nbh */
nbh = logsch(ih,nbh);
/* then compute the linear scaling factor deth */
deth = scalel(nbh,10);
/* adaptive prediction implementation */
/* compute the partial reconstructed signal */
ph = dh + szh;
/* update the zero section predictor coefficients */
upzero(dh,delay_dhx,delay_bph);
/* update second pole section predictor coefficient */
ah2 = uppol2(ah1,ah2,ph,ph1,ph2);
/* update first pole section predictor coefficient */
ah1 = uppol1(ah1,ah2,ph,ph1);
/* compute the quantized recontructed signal for adaptive predic */
yh = sh + dh;
/* implement delays for next time */
rh2 = rh1;
rh1 = yh;
ph2 = ph1;
ph1 = ph;
/*END: Hi Sub-Band Encoder*/
/*Multiplex ih and il to get 8-bit signal*/
return(il | (ih << 6));
}
/***************************************************************
decode: Input is the output of the encoder
performs the decoder processing.
result in xout1 and xout2
***************************************************************/
void decode(int input)
{
int i; /* rqmf filter index */
int xa1,xa2; /* qmf accumulators */
int *h_ptr; /* qmf coeficient pointer */
int *ac_ptr,*ac_ptr1; /* delay buffer pointers */
int *ad_ptr,*ad_ptr1;
int ilr,ih; /* encoded error */
int xs,xd;
int rl,rh;
int dl;
/*Split Received Word into Lo-Band and Hi-Band */
ilr = input & 0x3f;
ih = input >> 6;
/*BEGIN: Lo Sub-Band Decoder*/
/* compute predictor output for zero section */
dec_szl = filtez(dec_del_bpl,dec_del_dltx);
/* compute predictor output signal for pole section */
dec_spl = filtep(dec_rlt1,dec_al1,dec_rlt2,dec_al2);
/* compute the predictor output value */
dec_sl = dec_spl + dec_szl;
/* compute quantized difference signal for adaptive predic */
dec_dlt = ((long)dec_detl*qq4_code4_table[ilr >> 2]) >> 15;
/* compute quantized difference signal for decoder output */
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -