ADPCM and G.726 performance improvements courtesy fOSSiL (bug #2843)

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@4249 65c4cc65-6c06-0410-ace0-fbb531ad65f3
1.2-netsec
Mark Spencer 21 years ago
parent 9bf48f9ce7
commit 8de7794637

@ -25,6 +25,9 @@
#include <string.h>
#include <unistd.h>
/* define NOT_BLI to use a faster but not bit-level identical version */
/* #define NOT_BLI */
#define BUFFER_SIZE 8096 /* size for the translation buffers */
AST_MUTEX_DEFINE_STATIC(localuser_lock);
@ -41,28 +44,28 @@ static char *tdesc = "Adaptive Differential PCM Coder/Decoder";
* Step size index shift table
*/
static short indsft[8] = { -1, -1, -1, -1, 2, 4, 6, 8 };
static int indsft[8] = { -1, -1, -1, -1, 2, 4, 6, 8 };
/*
* Step size table, where stpsz[i]=floor[16*(11/10)^i]
*/
static short stpsz[49] = {
static int stpsz[49] = {
16, 17, 19, 21, 23, 25, 28, 31, 34, 37, 41, 45, 50, 55, 60, 66, 73,
80, 88, 97, 107, 118, 130, 143, 157, 173, 190, 209, 230, 253, 279,
307, 337, 371, 408, 449, 494, 544, 598, 658, 724, 796, 876, 963,
1060, 1166, 1282, 1411, 1552
};
/*
* Nibble to bit map
/*
* Decoder/Encoder state
* States for both encoder and decoder are synchronized
*/
static short nbl2bit[16][4] = {
{1, 0, 0, 0}, {1, 0, 0, 1}, {1, 0, 1, 0}, {1, 0, 1, 1},
{1, 1, 0, 0}, {1, 1, 0, 1}, {1, 1, 1, 0}, {1, 1, 1, 1},
{-1, 0, 0, 0}, {-1, 0, 0, 1}, {-1, 0, 1, 0}, {-1, 0, 1, 1},
{-1, 1, 0, 0}, {-1, 1, 0, 1}, {-1, 1, 1, 0}, {-1, 1, 1, 1}
struct adpcm_state {
int ssindex;
int signal;
int zero_count;
int next_flag;
};
/*
@ -76,51 +79,64 @@ static short nbl2bit[16][4] = {
* Sets the index to the step size table for the next encode.
*/
static inline void
decode (unsigned char encoded, short *ssindex, short *signal, unsigned char *rkey, unsigned char *next)
static inline short
decode(int encoded, struct adpcm_state* state)
{
short diff, step;
step = stpsz[*ssindex];
diff = step * nbl2bit[encoded][1] +
(step >> 1) * nbl2bit[encoded][2] +
(step >> 2) * nbl2bit[encoded][3] +
(step >> 3);
if (nbl2bit[encoded][2] && (step & 0x1))
diff++;
diff *= nbl2bit[encoded][0];
int diff;
int step;
int sign;
step = stpsz[state->ssindex];
sign = encoded & 0x08;
encoded &= 0x07;
#ifdef NOT_BLI
diff = (((encoded << 1) + 1) * step) >> 3;
#else /* BLI code */
diff = step >> 3;
if (encoded & 4) diff += step;
if (encoded & 2) diff += step >> 1;
if (encoded & 1) diff += step >> 2;
if ((encoded >> 1) & step & 0x1)
diff++;
#endif
if (sign)
diff = -diff;
if ( *next & 0x1 )
*signal -= 8;
else if ( *next & 0x2 )
*signal += 8;
if (state->next_flag & 0x1)
state->signal -= 8;
else if (state->next_flag & 0x2)
state->signal += 8;
*signal += diff;
state->signal += diff;
if (*signal > 2047)
*signal = 2047;
else if (*signal < -2047)
*signal = -2047;
if (state->signal > 2047)
state->signal = 2047;
else if (state->signal < -2047)
state->signal = -2047;
*next = 0;
state->next_flag = 0;
#ifdef AUTO_RETURN
if( encoded & 0x7 )
*rkey = 0;
else if ( ++(*rkey) == 24 ) {
*rkey = 0;
if (*signal > 0)
*next = 0x1;
else if (*signal < 0)
*next = 0x2;
}
if (encoded)
state->zero_count = 0;
else if (++(state->zero_count) == 24)
{
state->zero_count = 0;
if (state->signal > 0)
state->next_flag = 0x1;
else if (state->signal < 0)
state->next_flag = 0x2;
}
#endif
*ssindex = *ssindex + indsft[(encoded & 7)];
if (*ssindex < 0)
*ssindex = 0;
else if (*ssindex > 48)
*ssindex = 48;
state->ssindex += indsft[encoded];
if (state->ssindex < 0)
state->ssindex = 0;
else if (state->ssindex > 48)
state->ssindex = 48;
return state->signal << 4;
}
/*
@ -135,44 +151,63 @@ decode (unsigned char encoded, short *ssindex, short *signal, unsigned char *rke
* signal gets updated with each pass.
*/
static inline unsigned char
adpcm (short csig, short *ssindex, short *signal, unsigned char *rkey, unsigned char *next)
static inline int
adpcm(short csig, struct adpcm_state* state)
{
short diff, step;
unsigned char encoded;
step = stpsz[*ssindex];
/*
* Clip csig if too large or too small
*/
csig >>= 4;
diff = csig - *signal;
if (diff < 0)
{
encoded = 8;
diff = -diff;
}
else
encoded = 0;
if (diff >= step)
{
encoded |= 4;
diff -= step;
}
step >>= 1;
if (diff >= step)
{
encoded |= 2;
diff -= step;
}
step >>= 1;
if (diff >= step)
encoded |= 1;
decode (encoded, ssindex, signal, rkey, next);
return (encoded);
int diff;
int step;
int encoded;
/*
* Clip csig if too large or too small
*/
csig >>= 4;
step = stpsz[state->ssindex];
diff = csig - state->signal;
#ifdef NOT_BLI
if (diff < 0)
{
encoded = (-diff << 2) / step;
if (encoded > 7)
encoded = 7;
encoded |= 0x08;
}
else
{
encoded = (diff << 2) / step;
if (encoded > 7)
encoded = 7;
}
#else /* BLI code */
if (diff < 0)
{
encoded = 8;
diff = -diff;
}
else
encoded = 0;
if (diff >= step)
{
encoded |= 4;
diff -= step;
}
step >>= 1;
if (diff >= step)
{
encoded |= 2;
diff -= step;
}
step >>= 1;
if (diff >= step)
encoded |= 1;
#endif /* NOT_BLI */
/* feedback to state */
decode(encoded, state);
return encoded;
}
/*
@ -185,10 +220,7 @@ struct adpcm_encoder_pvt
char offset[AST_FRIENDLY_OFFSET]; /* Space to build offset */
short inbuf[BUFFER_SIZE]; /* Unencoded signed linear values */
unsigned char outbuf[BUFFER_SIZE]; /* Encoded ADPCM, two nibbles to a word */
short ssindex;
short signal;
unsigned char zero_count;
unsigned char next_flag;
struct adpcm_state state;
int tail;
};
@ -201,10 +233,7 @@ struct adpcm_decoder_pvt
struct ast_frame f;
char offset[AST_FRIENDLY_OFFSET]; /* Space to build offset */
short outbuf[BUFFER_SIZE]; /* Decoded signed linear values */
short ssindex;
short signal;
unsigned char zero_count;
unsigned char next_flag;
struct adpcm_state state;
int tail;
};
@ -287,11 +316,8 @@ adpcmtolin_framein (struct ast_translator_pvt *pvt, struct ast_frame *f)
b = f->data;
for (x=0;x<f->datalen;x++) {
decode((b[x] >> 4) & 0xf, &tmp->ssindex, &tmp->signal, &tmp->zero_count, &tmp->next_flag);
tmp->outbuf[tmp->tail++] = tmp->signal << 4;
decode(b[x] & 0x0f, &tmp->ssindex, &tmp->signal, &tmp->zero_count, &tmp->next_flag);
tmp->outbuf[tmp->tail++] = tmp->signal << 4;
tmp->outbuf[tmp->tail++] = decode((b[x] >> 4) & 0xf, &tmp->state);
tmp->outbuf[tmp->tail++] = decode(b[x] & 0x0f, &tmp->state);
}
return 0;
@ -374,26 +400,25 @@ static struct ast_frame *
lintoadpcm_frameout (struct ast_translator_pvt *pvt)
{
struct adpcm_encoder_pvt *tmp = (struct adpcm_encoder_pvt *) pvt;
unsigned char adpcm0, adpcm1;
int i_max, i;
if (tmp->tail < 2) return NULL;
i_max = (tmp->tail / 2) * 2;
i_max = tmp->tail & ~1; /* atomic size is 2 samples */
/* What is this, state debugging? should be #ifdef'd then
tmp->outbuf[0] = tmp->ssindex & 0xff;
tmp->outbuf[1] = (tmp->signal >> 8) & 0xff;
tmp->outbuf[2] = (tmp->signal & 0xff);
tmp->outbuf[3] = tmp->zero_count;
tmp->outbuf[4] = tmp->next_flag;
*/
for (i = 0; i < i_max; i+=2)
{
adpcm0 = adpcm (tmp->inbuf[i], &tmp->ssindex, &tmp->signal, &tmp->zero_count, &tmp->next_flag);
adpcm1 = adpcm (tmp->inbuf[i+1], &tmp->ssindex, &tmp->signal, &tmp->zero_count, &tmp->next_flag);
tmp->outbuf[i/2] = (adpcm0 << 4) | adpcm1;
tmp->outbuf[i/2] =
(adpcm(tmp->inbuf[i ], &tmp->state) << 4) |
(adpcm(tmp->inbuf[i+1], &tmp->state) );
};

@ -25,6 +25,22 @@
#include <string.h>
#include <unistd.h>
#define WANT_ASM
#include "log2comp.h"
/* define NOT_BLI to use a faster but not bit-level identical version */
/* #define NOT_BLI */
#if defined(NOT_BLI)
# if defined(_MSC_VER)
typedef __int64 sint64;
# elif defined(__GNUC__)
typedef long long sint64;
# else
# error 64-bit integer type is not defined for your compiler/platform
# endif
#endif
#define BUFFER_SIZE 8096 /* size for the translation buffers */
#define BUF_SHIFT 5
@ -49,96 +65,52 @@ static char *tdesc = "ITU G.726-32kbps G726 Transcoder";
*/
struct g726_state {
long yl; /* Locked or steady state step size multiplier. */
short yu; /* Unlocked or non-steady state step size multiplier. */
short dms; /* Short term energy estimate. */
short dml; /* Long term energy estimate. */
short ap; /* Linear weighting coefficient of 'yl' and 'yu'. */
short a[2]; /* Coefficients of pole portion of prediction filter. */
short b[6]; /* Coefficients of zero portion of prediction filter. */
short pk[2]; /*
* Signs of previous two samples of a partially
int yu; /* Unlocked or non-steady state step size multiplier. */
int dms; /* Short term energy estimate. */
int dml; /* Long term energy estimate. */
int ap; /* Linear weighting coefficient of 'yl' and 'yu'. */
int a[2]; /* Coefficients of pole portion of prediction filter.
* stored as fixed-point 1==2^14 */
int b[6]; /* Coefficients of zero portion of prediction filter.
* stored as fixed-point 1==2^14 */
int pk[2]; /* Signs of previous two samples of a partially
* reconstructed signal.
*/
short dq[6]; /*
* Previous 6 samples of the quantized difference
* signal represented in an internal floating point
* format.
*/
short sr[2]; /*
* Previous 2 samples of the quantized difference
* signal represented in an internal floating point
* format.
*/
char td; /* delayed tone detect, new in 1988 version */
int dq[6]; /* Previous 6 samples of the quantized difference signal
* stored as fixed point 1==2^12,
* or in internal floating point format */
int sr[2]; /* Previous 2 samples of the quantized difference signal
* stored as fixed point 1==2^12,
* or in internal floating point format */
int td; /* delayed tone detect, new in 1988 version */
};
static short qtab_721[7] = {-124, 80, 178, 246, 300, 349, 400};
static int qtab_721[7] = {-124, 80, 178, 246, 300, 349, 400};
/*
* Maps G.721 code word to reconstructed scale factor normalized log
* magnitude values.
*/
static short _dqlntab[16] = {-2048, 4, 135, 213, 273, 323, 373, 425,
static int _dqlntab[16] = {-2048, 4, 135, 213, 273, 323, 373, 425,
425, 373, 323, 273, 213, 135, 4, -2048};
/* Maps G.721 code word to log of scale factor multiplier. */
static short _witab[16] = {-12, 18, 41, 64, 112, 198, 355, 1122,
static int _witab[16] = {-12, 18, 41, 64, 112, 198, 355, 1122,
1122, 355, 198, 112, 64, 41, 18, -12};
/*
* Maps G.721 code words to a set of values whose long and short
* term averages are computed and then compared to give an indication
* how stationary (steady state) the signal is.
*/
static short _fitab[16] = {0, 0, 0, 0x200, 0x200, 0x200, 0x600, 0xE00,
static int _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,
/* Deprecated
static int power2[15] = {1, 2, 4, 8, 0x10, 0x20, 0x40, 0x80,
0x100, 0x200, 0x400, 0x800, 0x1000, 0x2000, 0x4000};
/*
* quan()
*
* quantizes the input val against the table of size short integers.
* It returns i if table[i - 1] <= val < table[i].
*
* Using linear search for simple coding.
*/
static int quan(int val, short *table, int size)
{
int i;
for (i = 0; i < size; i++)
if (val < *table++)
break;
return (i);
}
/*
* fmult()
*
* returns the integer product of the 14-bit integer "an" and
* "floating point" representation (4-bit exponent, 6-bit mantessa) "srn".
*/
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);
}
*/
/*
* g72x_init_state()
@ -156,46 +128,117 @@ static void g726_init_state(struct g726_state *state_ptr)
state_ptr->dms = 0;
state_ptr->dml = 0;
state_ptr->ap = 0;
for (cnta = 0; cnta < 2; cnta++) {
for (cnta = 0; cnta < 2; cnta++)
{
state_ptr->a[cnta] = 0;
state_ptr->pk[cnta] = 0;
#ifdef NOT_BLI
state_ptr->sr[cnta] = 1;
#else
state_ptr->sr[cnta] = 32;
#endif
}
for (cnta = 0; cnta < 6; cnta++) {
for (cnta = 0; cnta < 6; cnta++)
{
state_ptr->b[cnta] = 0;
#ifdef NOT_BLI
state_ptr->dq[cnta] = 1;
#else
state_ptr->dq[cnta] = 32;
#endif
}
state_ptr->td = 0;
}
/*
* predictor_zero()
* quan()
*
* computes the estimated signal from 6-zero predictor.
* quantizes the input val against the table of integers.
* It returns i if table[i - 1] <= val < table[i].
*
* Using linear search for simple coding.
*/
static int predictor_zero(struct g726_state *state_ptr)
static int quan(int val, int *table, int size)
{
int i;
int sezi;
sezi = fmult(state_ptr->b[0] >> 2, state_ptr->dq[0]);
for (i = 1; i < 6; i++) /* ACCUM */
sezi += fmult(state_ptr->b[i] >> 2, state_ptr->dq[i]);
return (sezi);
for (i = 0; i < size && val >= *table; ++i, ++table)
;
return (i);
}
#ifdef NOT_BLI /* faster non-identical version */
/*
* predictor_zero()
*
* computes the estimated signal from 6-zero predictor.
*
*/
static int predictor_zero(struct g726_state *state_ptr)
{ /* divide by 2 is necessary here to handle negative numbers correctly */
int i;
sint64 sezi;
for (sezi = 0, i = 0; i < 6; i++) /* ACCUM */
sezi += (sint64)state_ptr->b[i] * state_ptr->dq[i];
return (int)(sezi >> 13) / 2 /* 2^14 */;
}
/*
* predictor_pole()
*
* computes the estimated signal from 2-pole predictor.
*
*/
static int predictor_pole(struct g726_state *state_ptr)
{ /* divide by 2 is necessary here to handle negative numbers correctly */
return (int)(((sint64)state_ptr->a[1] * state_ptr->sr[1] +
(sint64)state_ptr->a[0] * state_ptr->sr[0]) >> 13) / 2 /* 2^14 */;
}
#else /* NOT_BLI - identical version */
/*
* fmult()
*
* returns the integer product of the fixed-point number "an" (1==2^12) and
* "floating point" representation (4-bit exponent, 6-bit mantessa) "srn".
*/
static int fmult(int an, int srn)
{
int anmag, anexp, anmant;
int wanexp, wanmant;
int retval;
anmag = (an > 0) ? an : ((-an) & 0x1FFF);
anexp = log2(anmag) - 5;
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 int predictor_zero(struct g726_state *state_ptr)
{
int i;
int sezi;
for (sezi = 0, i = 0; i < 6; i++) /* ACCUM */
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]));
fmult(state_ptr->a[0] >> 2, state_ptr->sr[0]));
}
#endif /* NOT_BLI */
/*
* step_size()
*
@ -234,14 +277,14 @@ static int step_size(struct g726_state *state_ptr)
static int quantize(
int d, /* Raw difference signal sample */
int y, /* Step size multiplier */
short *table, /* quantization table */
int size) /* table size of short integers */
int *table, /* quantization table */
int size) /* table size of integers */
{
short dqm; /* Magnitude of 'd' */
short exp; /* Integer part of base 2 log of 'd' */
short mant; /* Fractional part of base 2 log */
short dl; /* Log of magnitude of 'd' */
short dln; /* Step size scale factor normalized log */
int dqm; /* Magnitude of 'd' */
int exp; /* Integer part of base 2 log of 'd' */
int mant; /* Fractional part of base 2 log */
int dl; /* Log of magnitude of 'd' */
int dln; /* Step size scale factor normalized log */
int i;
/*
@ -250,9 +293,11 @@ static int quantize(
* Compute base 2 log of 'd', and store in 'dl'.
*/
dqm = abs(d);
exp = quan(dqm >> 1, power2, 15);
exp = log2(dqm);
if (exp < 0)
exp = 0;
mant = ((dqm << 7) >> exp) & 0x7F; /* Fractional portion. */
dl = (exp << 7) + mant;
dl = (exp << 7) | mant;
/*
* SUBTB
@ -287,20 +332,29 @@ static int reconstruct(
int dqln, /* G.72x codeword */
int y) /* Step size multiplier */
{
short dql; /* Log of 'dq' magnitude */
short dex; /* Integer part of log */
short dqt;
short dq; /* Reconstructed difference signal sample */
int dql; /* Log of 'dq' magnitude */
int dex; /* Integer part of log */
int dqt;
int dq; /* Reconstructed difference signal sample */
dql = dqln + (y >> 2); /* ADDA */
if (dql < 0) {
return ((sign) ? -0x8000 : 0);
#ifdef NOT_BLI
return (sign) ? -1 : 1;
#else
return (sign) ? -0x8000 : 0;
#endif
} else { /* ANTILOG */
dex = (dql >> 7) & 15;
dqt = 128 + (dql & 127);
#ifdef NOT_BLI
dq = ((dqt << 19) >> (14 - dex));
return (sign) ? -dq : dq;
#else
dq = (dqt << 7) >> (14 - dex);
return ((sign) ? (dq - 0x8000) : dq);
return (sign) ? (dq - 0x8000) : dq;
#endif
}
}
@ -320,19 +374,26 @@ static void update(
struct g726_state *state_ptr) /* coder state pointer */
{
int cnt;
short mag, exp; /* Adaptive predictor, FLOAT A */
short a2p=0; /* LIMC */
short a1ul; /* UPA1 */
short pks1; /* UPA2 */
short fa1;
char tr; /* tone/transition detector */
short ylint, thr2, dqthr;
short ylfrac, thr1;
short pk0;
int mag; /* Adaptive predictor, FLOAT A */
#ifndef NOT_BLI
int exp;
#endif
int a2p=0; /* LIMC */
int a1ul; /* UPA1 */
int pks1; /* UPA2 */
int fa1;
int tr; /* tone/transition detector */
int ylint, thr2, dqthr;
int ylfrac, thr1;
int pk0;
pk0 = (dqsez < 0) ? 1 : 0; /* needed in updating predictor poles */
#ifdef NOT_BLI
mag = abs(dq / 0x1000); /* prediction difference magnitude */
#else
mag = dq & 0x7FFF; /* prediction difference magnitude */
#endif
/* TRANS */
ylint = state_ptr->yl >> 15; /* exponent part of yl */
ylfrac = (state_ptr->yl >> 10) & 0x1F; /* fractional part of yl */
@ -431,7 +492,8 @@ static void update(
state_ptr->b[cnt] -= state_ptr->b[cnt] >> 9;
else /* for G.721 and 24Kbps G.723 */
state_ptr->b[cnt] -= state_ptr->b[cnt] >> 8;
if (dq & 0x7FFF) { /* XOR */
if (mag)
{ /* XOR */
if ((dq ^ state_ptr->dq[cnt]) >= 0)
state_ptr->b[cnt] += 128;
else
@ -442,29 +504,37 @@ static void update(
for (cnt = 5; cnt > 0; cnt--)
state_ptr->dq[cnt] = state_ptr->dq[cnt-1];
#ifdef NOT_BLI
state_ptr->dq[0] = dq;
#else
/* FLOAT A : convert dq[0] to 4-bit exp, 6-bit mantissa f.p. */
if (mag == 0) {
state_ptr->dq[0] = (dq >= 0) ? 0x20 : 0xFC20;
state_ptr->dq[0] = (dq >= 0) ? 0x20 : 0x20 - 0x400;
} else {
exp = quan(mag, power2, 15);
exp = log2(mag) + 1;
state_ptr->dq[0] = (dq >= 0) ?
(exp << 6) + ((mag << 6) >> exp) :
(exp << 6) + ((mag << 6) >> exp) - 0x400;
}
#endif
state_ptr->sr[1] = state_ptr->sr[0];
#ifdef NOT_BLI
state_ptr->sr[0] = sr;
#else
/* FLOAT B : convert sr to 4-bit exp., 6-bit mantissa f.p. */
if (sr == 0) {
state_ptr->sr[0] = 0x20;
} else if (sr > 0) {
exp = quan(sr, power2, 15);
exp = log2(sr) + 1;
state_ptr->sr[0] = (exp << 6) + ((sr << 6) >> exp);
} else if (sr > -32768) {
} else if (sr > -0x8000) {
mag = -sr;
exp = quan(mag, power2, 15);
exp = log2(mag) + 1;
state_ptr->sr[0] = (exp << 6) + ((mag << 6) >> exp) - 0x400;
} else
state_ptr->sr[0] = 0xFC20;
state_ptr->sr[0] = 0x20 - 0x400;
#endif
/* DELAY A */
state_ptr->pk[1] = state_ptr->pk[0];
@ -508,30 +578,44 @@ static void update(
*/
static int g726_decode(int i, struct g726_state *state_ptr)
{
short sezi, sei, sez, se; /* ACCUM */
short y; /* MIX */
short sr; /* ADDB */
short dq;
short dqsez;
int sezi, sez, se; /* ACCUM */
int y; /* MIX */
int sr; /* ADDB */
int dq;
int dqsez;
i &= 0x0f; /* mask to get proper bits */
#ifdef NOT_BLI
sezi = predictor_zero(state_ptr);
sez = sezi;
se = sezi + predictor_pole(state_ptr); /* estimated signal */
#else
sezi = predictor_zero(state_ptr);
sez = sezi >> 1;
sei = sezi + predictor_pole(state_ptr);
se = sei >> 1; /* se = estimated signal */
se = (sezi + predictor_pole(state_ptr)) >> 1; /* estimated signal */
#endif
y = step_size(state_ptr); /* dynamic quantizer step size */
dq = reconstruct(i & 0x08, _dqlntab[i], y); /* quantized diff. */
sr = (dq < 0) ? (se - (dq & 0x3FFF)) : se + dq; /* reconst. signal */
dq = reconstruct(i & 8, _dqlntab[i], y); /* quantized diff. */
dqsez = sr - se + sez; /* pole prediction diff. */
#ifdef NOT_BLI
sr = se + dq; /* reconst. signal */
dqsez = dq + sez; /* pole prediction diff. */
#else
sr = (dq < 0) ? se - (dq & 0x3FFF) : se + dq; /* reconst. signal */
dqsez = sr - se + sez; /* pole prediction diff. */
#endif
update(4, y, _witab[i] << 5, _fitab[i], dq, sr, dqsez, state_ptr);
#ifdef NOT_BLI
return (sr >> 10); /* sr was 26-bit dynamic range */
#else
return (sr << 2); /* sr was 14-bit dynamic range */
#endif
}
/*
* g726_encode()
*
@ -540,30 +624,45 @@ static int g726_decode(int i, struct g726_state *state_ptr)
*/
static int g726_encode(int sl, struct g726_state *state_ptr)
{
short sezi, se, sez; /* ACCUM */
short d; /* SUBTA */
short sr; /* ADDB */
short y; /* MIX */
short dqsez; /* ADDC */
short dq, i;
int sezi, se, sez; /* ACCUM */
int d; /* SUBTA */
int sr; /* ADDB */
int y; /* MIX */
int dqsez; /* ADDC */
int dq, i;
#ifdef NOT_BLI
sl <<= 10; /* 26-bit dynamic range */
sezi = predictor_zero(state_ptr);
sez = sezi;
se = sezi + predictor_pole(state_ptr); /* estimated signal */
#else
sl >>= 2; /* 14-bit dynamic range */
sezi = predictor_zero(state_ptr);
sez = sezi >> 1;
se = (sezi + predictor_pole(state_ptr)) >> 1; /* estimated signal */
#endif
d = sl - se; /* estimation difference */
/* quantize the prediction difference */
y = step_size(state_ptr); /* quantizer step size */
#ifdef NOT_BLI
d /= 0x1000;
#endif
i = quantize(d, y, qtab_721, 7); /* i = G726 code */
dq = reconstruct(i & 8, _dqlntab[i], y); /* quantized est diff */
#ifdef NOT_BLI
sr = se + dq; /* reconst. signal */
dqsez = dq + sez; /* pole prediction diff. */
#else
sr = (dq < 0) ? se - (dq & 0x3FFF) : se + dq; /* reconst. signal */
dqsez = sr + sez - se; /* pole prediction diff. */
dqsez = sr - se + sez; /* pole prediction diff. */
#endif
update(4, y, _witab[i] << 5, _fitab[i], dq, sr, dqsez, state_ptr);

Loading…
Cancel
Save