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
This commit is contained in:
Mark Spencer
2004-11-15 00:56:53 +00:00
parent 9bf48f9ce7
commit 8de7794637
2 changed files with 353 additions and 229 deletions

View File

@@ -25,6 +25,9 @@
#include <string.h> #include <string.h>
#include <unistd.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 */ #define BUFFER_SIZE 8096 /* size for the translation buffers */
AST_MUTEX_DEFINE_STATIC(localuser_lock); AST_MUTEX_DEFINE_STATIC(localuser_lock);
@@ -41,28 +44,28 @@ static char *tdesc = "Adaptive Differential PCM Coder/Decoder";
* Step size index shift table * 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] * 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, 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, 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, 307, 337, 371, 408, 449, 494, 544, 598, 658, 724, 796, 876, 963,
1060, 1166, 1282, 1411, 1552 1060, 1166, 1282, 1411, 1552
}; };
/* /*
* Nibble to bit map * Decoder/Encoder state
* States for both encoder and decoder are synchronized
*/ */
struct adpcm_state {
static short nbl2bit[16][4] = { int ssindex;
{1, 0, 0, 0}, {1, 0, 0, 1}, {1, 0, 1, 0}, {1, 0, 1, 1}, int signal;
{1, 1, 0, 0}, {1, 1, 0, 1}, {1, 1, 1, 0}, {1, 1, 1, 1}, int zero_count;
{-1, 0, 0, 0}, {-1, 0, 0, 1}, {-1, 0, 1, 0}, {-1, 0, 1, 1}, int next_flag;
{-1, 1, 0, 0}, {-1, 1, 0, 1}, {-1, 1, 1, 0}, {-1, 1, 1, 1}
}; };
/* /*
@@ -76,51 +79,64 @@ static short nbl2bit[16][4] = {
* Sets the index to the step size table for the next encode. * Sets the index to the step size table for the next encode.
*/ */
static inline void static inline short
decode (unsigned char encoded, short *ssindex, short *signal, unsigned char *rkey, unsigned char *next) decode(int encoded, struct adpcm_state* state)
{ {
short diff, step; int diff;
step = stpsz[*ssindex]; int step;
int sign;
diff = step * nbl2bit[encoded][1] + step = stpsz[state->ssindex];
(step >> 1) * nbl2bit[encoded][2] +
(step >> 2) * nbl2bit[encoded][3] +
(step >> 3);
if (nbl2bit[encoded][2] && (step & 0x1))
diff++;
diff *= nbl2bit[encoded][0];
if ( *next & 0x1 ) sign = encoded & 0x08;
*signal -= 8; encoded &= 0x07;
else if ( *next & 0x2 ) #ifdef NOT_BLI
*signal += 8; 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;
*signal += diff; if (state->next_flag & 0x1)
state->signal -= 8;
else if (state->next_flag & 0x2)
state->signal += 8;
if (*signal > 2047) state->signal += diff;
*signal = 2047;
else if (*signal < -2047)
*signal = -2047;
*next = 0; if (state->signal > 2047)
state->signal = 2047;
else if (state->signal < -2047)
state->signal = -2047;
state->next_flag = 0;
#ifdef AUTO_RETURN #ifdef AUTO_RETURN
if( encoded & 0x7 ) if (encoded)
*rkey = 0; state->zero_count = 0;
else if ( ++(*rkey) == 24 ) { else if (++(state->zero_count) == 24)
*rkey = 0; {
if (*signal > 0) state->zero_count = 0;
*next = 0x1; if (state->signal > 0)
else if (*signal < 0) state->next_flag = 0x1;
*next = 0x2; else if (state->signal < 0)
} state->next_flag = 0x2;
}
#endif #endif
*ssindex = *ssindex + indsft[(encoded & 7)]; state->ssindex += indsft[encoded];
if (*ssindex < 0) if (state->ssindex < 0)
*ssindex = 0; state->ssindex = 0;
else if (*ssindex > 48) else if (state->ssindex > 48)
*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. * signal gets updated with each pass.
*/ */
static inline unsigned char static inline int
adpcm (short csig, short *ssindex, short *signal, unsigned char *rkey, unsigned char *next) adpcm(short csig, struct adpcm_state* state)
{ {
short diff, step; int diff;
unsigned char encoded; int step;
step = stpsz[*ssindex]; int encoded;
/*
* Clip csig if too large or too small
*/
csig >>= 4;
diff = csig - *signal; /*
* Clip csig if too large or too small
if (diff < 0) */
{ csig >>= 4;
encoded = 8;
diff = -diff; step = stpsz[state->ssindex];
} diff = csig - state->signal;
else
encoded = 0; #ifdef NOT_BLI
if (diff >= step) if (diff < 0)
{ {
encoded |= 4; encoded = (-diff << 2) / step;
diff -= step; if (encoded > 7)
} encoded = 7;
step >>= 1; encoded |= 0x08;
if (diff >= step) }
{ else
encoded |= 2; {
diff -= step; encoded = (diff << 2) / step;
} if (encoded > 7)
step >>= 1; encoded = 7;
if (diff >= step) }
encoded |= 1; #else /* BLI code */
if (diff < 0)
decode (encoded, ssindex, signal, rkey, next); {
return (encoded); 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 */ char offset[AST_FRIENDLY_OFFSET]; /* Space to build offset */
short inbuf[BUFFER_SIZE]; /* Unencoded signed linear values */ short inbuf[BUFFER_SIZE]; /* Unencoded signed linear values */
unsigned char outbuf[BUFFER_SIZE]; /* Encoded ADPCM, two nibbles to a word */ unsigned char outbuf[BUFFER_SIZE]; /* Encoded ADPCM, two nibbles to a word */
short ssindex; struct adpcm_state state;
short signal;
unsigned char zero_count;
unsigned char next_flag;
int tail; int tail;
}; };
@@ -201,10 +233,7 @@ struct adpcm_decoder_pvt
struct ast_frame f; struct ast_frame f;
char offset[AST_FRIENDLY_OFFSET]; /* Space to build offset */ char offset[AST_FRIENDLY_OFFSET]; /* Space to build offset */
short outbuf[BUFFER_SIZE]; /* Decoded signed linear values */ short outbuf[BUFFER_SIZE]; /* Decoded signed linear values */
short ssindex; struct adpcm_state state;
short signal;
unsigned char zero_count;
unsigned char next_flag;
int tail; int tail;
}; };
@@ -287,11 +316,8 @@ adpcmtolin_framein (struct ast_translator_pvt *pvt, struct ast_frame *f)
b = f->data; b = f->data;
for (x=0;x<f->datalen;x++) { 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++] = decode((b[x] >> 4) & 0xf, &tmp->state);
tmp->outbuf[tmp->tail++] = tmp->signal << 4; tmp->outbuf[tmp->tail++] = decode(b[x] & 0x0f, &tmp->state);
decode(b[x] & 0x0f, &tmp->ssindex, &tmp->signal, &tmp->zero_count, &tmp->next_flag);
tmp->outbuf[tmp->tail++] = tmp->signal << 4;
} }
return 0; return 0;
@@ -374,26 +400,25 @@ static struct ast_frame *
lintoadpcm_frameout (struct ast_translator_pvt *pvt) lintoadpcm_frameout (struct ast_translator_pvt *pvt)
{ {
struct adpcm_encoder_pvt *tmp = (struct adpcm_encoder_pvt *) pvt; struct adpcm_encoder_pvt *tmp = (struct adpcm_encoder_pvt *) pvt;
unsigned char adpcm0, adpcm1;
int i_max, i; int i_max, i;
if (tmp->tail < 2) return NULL; 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[0] = tmp->ssindex & 0xff;
tmp->outbuf[1] = (tmp->signal >> 8) & 0xff; tmp->outbuf[1] = (tmp->signal >> 8) & 0xff;
tmp->outbuf[2] = (tmp->signal & 0xff); tmp->outbuf[2] = (tmp->signal & 0xff);
tmp->outbuf[3] = tmp->zero_count; tmp->outbuf[3] = tmp->zero_count;
tmp->outbuf[4] = tmp->next_flag; tmp->outbuf[4] = tmp->next_flag;
*/
for (i = 0; i < i_max; i+=2) for (i = 0; i < i_max; i+=2)
{ {
adpcm0 = adpcm (tmp->inbuf[i], &tmp->ssindex, &tmp->signal, &tmp->zero_count, &tmp->next_flag); tmp->outbuf[i/2] =
adpcm1 = adpcm (tmp->inbuf[i+1], &tmp->ssindex, &tmp->signal, &tmp->zero_count, &tmp->next_flag); (adpcm(tmp->inbuf[i ], &tmp->state) << 4) |
(adpcm(tmp->inbuf[i+1], &tmp->state) );
tmp->outbuf[i/2] = (adpcm0 << 4) | adpcm1;
}; };

View File

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