#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include "g726.h"
static const int qtab_726_16[1] =
{
261
};
static const int qtab_726_24[3] =
{
8, 218, 331
};
static const int qtab_726_32[7] =
{
-124, 80, 178, 246, 300, 349, 400
};
static const int qtab_726_40[15] =
{
-122, -16, 68, 139, 198, 250, 298, 339,
378, 413, 445, 475, 502, 528, 553
};
static __inline int top_bit(unsigned int bits)
{
#if defined(__i386__) || defined(__x86_64__)
int res;
__asm__ (" xorl %[res],%[res];\n"
" decl %[res];\n"
" bsrl %[bits],%[res]\n"
: [res] "=&r" (res)
: [bits] "rm" (bits));
return res;
#elif defined(__ppc__) || defined(__powerpc__)
int res;
__asm__ ("cntlzw %[res],%[bits];\n"
: [res] "=&r" (res)
: [bits] "r" (bits));
return 31 - res;
#elif defined(_M_IX86) // Visual Studio x86
__asm
{
xor eax, eax
dec eax
bsr eax, bits
}
#else
int res;
if (bits == 0)
return -1;
res = 0;
if (bits & 0xFFFF0000)
{
bits &= 0xFFFF0000;
res += 16;
}
if (bits & 0xFF00FF00)
{
bits &= 0xFF00FF00;
res += 8;
}
if (bits & 0xF0F0F0F0)
{
bits &= 0xF0F0F0F0;
res += 4;
}
if (bits & 0xCCCCCCCC)
{
bits &= 0xCCCCCCCC;
res += 2;
}
if (bits & 0xAAAAAAAA)
{
bits &= 0xAAAAAAAA;
res += 1;
}
return res;
#endif
}
static bitstream_state_t *bitstream_init(bitstream_state_t *s)
{
if (s == NULL)
return NULL;
s->bitstream = 0;
s->residue = 0;
return s;
}
/*
* Given a raw sample, 'd', of the difference signal and a
* quantization step size scale factor, 'y', this routine returns the
* ADPCM codeword to which that sample gets quantized. The step
* size scale factor division operation is done in the log base 2 domain
* as a subtraction.
*/
static short quantize(int d, /* Raw difference signal sample */
int y, /* Step size multiplier */
const int table[], /* quantization table */
int quantizer_states) /* table size of short 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 i;
int size;
/*
* LOG
*
* Compute base 2 log of 'd', and store in 'dl'.
*/
dqm = (short) abs(d);
exp = (short) (top_bit(dqm >> 1) + 1);
/* Fractional portion. */
mant = ((dqm << 7) >> exp) & 0x7F;
dl = (exp << 7) + mant;
/*
* SUBTB
*
* "Divide" by step size multiplier.
*/
dln = dl - (short) (y >> 2);
/*
* QUAN
*
* Search for codword i for 'dln'.
*/
size = (quantizer_states - 1) >> 1;
for (i = 0; i < size; i++)
{
if (dln < table[i])
break;
}
if (d < 0)
{
/* Take 1's complement of i */
return (short) ((size << 1) + 1 - i);
}
if (i == 0 && (quantizer_states & 1))
{
/* Zero is only valid if there are an even number of states, so
take the 1's complement if the code is zero. */
return (short) quantizer_states;
}
return (short) i;
}
/*- End of function --------------------------------------------------------*/
/*
* returns the integer product of the 14-bit integer "an" and
* "floating point" representation (4-bit exponent, 6-bit mantessa) "srn".
*/
static short fmult(short an, short srn)
{
short anmag;
short anexp;
short anmant;
short wanexp;
short wanmant;
short retval;
anmag = (an > 0) ? an : ((-an) & 0x1FFF);
anexp = (short) (top_bit(anmag) - 5);
anmant = (anmag == 0) ? 32 : (anexp >= 0) ? (anmag >> anexp) : (anmag << -anexp);
wanexp = anexp + ((srn >> 6) & 0xF) - 13;
wanmant = (anmant*(srn & 0x3F) + 0x30) >> 4;
retval = (wanexp >= 0) ? ((wanmant << wanexp) & 0x7FFF) : (wanmant >> -wanexp);
return (((an ^ srn) < 0) ? -retval : retval);
}
/*
* Compute the estimated signal from the 6-zero predictor.
*/
static __inline short predictor_zero(g726_state_t *s)
{
int i;
int sezi;
sezi = fmult(s->b[0] >> 2, s->dq[0]);
/* ACCUM */
for (i = 1; i < 6; i++)
sezi += fmult(s->b[i] >> 2, s->dq[i]);
return (short) sezi;
}
/*- End of function --------------------------------------------------------*/
/*
* Computes the estimated signal from the 2-pole predictor.
*/
static __inline short predictor_pole(g726_state_t *s)
{
return (fmult(s->a[1] >> 2, s->sr[1]) + fmult(s->a[0] >> 2, s->sr[0]));
}
/*
* Computes the quantization step size of the adaptive quantizer.
*/
static int step_size(g726_state_t *s)
{
int y;
int dif;
int al;
if (s->ap >= 256)
return s->yu;
y = s->yl >> 6;
dif = s->yu - y;
al = s->ap >> 2;
if (dif > 0)
y += (dif*al) >> 6;
else if (dif < 0)
y += (dif*al + 0x3F) >> 6;
return y;
}
/*- End of function --------------------------------------------------------*/
/*
* Returns reconstructed difference signal 'dq' obtained from
* codeword 'i' and quantization step size scale factor 'y'.
* Multiplication is performed in log base 2 domain as addition.
*/
static short reconstruct(int sign, /* 0 for non-negative value */
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 */
dql = (short) (dqln + (y >> 2)); /* ADDA */
if (dql < 0)
return ((sign) ? -0x8000 : 0);
/* ANTILOG */
dex = (dql >> 7) & 15;
dqt = 128 + (dql & 127);
dq = (dqt << 7) >> (14 - dex);
return ((sign) ? (dq - 0x8000) : dq);
}
/*- End of function --------------------------------------------------------*/
/*
* updates the state variables for each output code
*/
static void update(g726_state_t *s,
int y, /* quantizer step size */
int wi, /* scale factor multiplier */
int fi, /* for long/short term energies */
int dq, /* quantized prediction difference */
int sr, /* reconstructed signal */
int dqsez) /* difference from 2-pole predictor */
{
short mag;
short exp;
short a2p; /* LIMC */
short a1ul; /* UPA1 */
short pks1; /* UPA2 */
short fa1;
short ylint;
short dqthr;
short ylfrac;
short thr;
short pk0;
int i;
int tr;
a2p = 0;
/* Needed in updating predictor poles */
pk0 = (dqsez < 0) ? 1 : 0;
/* prediction difference magnitude */
mag = (short) (dq & 0x7FFF);
/* TRANS */
ylint = (short) (s->yl >> 15); /* exponent part of yl */
ylfrac = (short) ((s->yl >> 10) & 0x1F); /* fractional part of yl */
/* Limit threshold to 31 << 10 */
thr = (ylint > 9) ? (31 << 10) : ((32 + ylfrac) << ylint);
dqthr = (thr + (thr >> 1)) >> 1; /* dqthr = 0.75 * thr */
if (!s->td) /* signal supposed voice */
tr = 0;
else if (mag <= dqthr) /* supposed data, but small mag */
tr = 0; /* treated as voice */
else /* signal is data (modem) */
tr = 1;
/*
* Quantizer scale factor adaptation.
*/
/* FUNCTW & FILTD & DELAY */
/* update non-steady state step size multiplier */
s->yu = (short) (y + ((wi - y) >> 5));
/* LIMB */
if (s->yu < 544)
s->yu = 544;
else if (s->yu > 5120)
s->yu = 5120;
/* FILTE & DELAY */
/* update steady state step size multiplier */
s->yl += s->yu + ((-s->yl) >> 6);
/*
* Adaptive predictor coefficients.
*/
if (tr)
{
/* Reset the a's and b's for a modem signal */
s->a[0] = 0;
s->a[1] = 0;
s->b[0] = 0;
s->b[1] = 0;
s->b[2] = 0;
s->b[3] = 0;
s->b[4] = 0;
s->b[5] = 0;
}
else
{
/* Update the a's and b's */
/* UPA2 */
pks1 = pk0 ^ s->pk[0];
/* Update predictor pole a[1] */
a2p = s->a[1] - (s->a[1] >> 7);
if (dqsez != 0)
{
fa1 = (pk
评论0