⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 g72x.cpp

📁 Wxpython Implemented on Windows CE, Source code
💻 CPP
📖 第 1 页 / 共 2 页
字号:


/*
 * update()
 *
 * updates the state variables for each output code
 */
void
update(
    int        code_size,    /* distinguish 723_40 with others */
    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 */
    struct g72x_state *state_ptr)    /* coder state pointer */
{
    int          cnt;
    short        mag;        /* Adaptive predictor, FLOAT A */
    short        a2p;        /* LIMC */
    short        a1ul;       /* UPA1 */
    short        pks1;       /* UPA2 */
    short        fa1;
    char         tr;         /* tone/transition detector */
    short        ylint, thr2, dqthr;
    short        ylfrac, thr1;
    short        pk0;

    pk0 = (dqsez < 0) ? 1 : 0;    /* needed in updating predictor poles */

    mag = dq & 0x7FFF;        /* prediction difference magnitude */
    /* TRANS */
    ylint = short (state_ptr->yl >> 15);    /* exponent part of yl */
    ylfrac = (state_ptr->yl >> 10) & 0x1F;    /* fractional part of yl */
    thr1 = (32 + ylfrac) << ylint;        /* threshold */
    thr2 = (ylint > 9) ? 31 << 10 : thr1;    /* limit thr2 to 31 << 10 */
    dqthr = (thr2 + (thr2 >> 1)) >> 1;    /* dqthr = 0.75 * thr2 */
    if (state_ptr->td == 0)        /* 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 */
    state_ptr->yu = y + ((wi - y) >> 5);

    /* LIMB */
    if (state_ptr->yu < 544)    /* 544 <= yu <= 5120 */
        state_ptr->yu = 544;
    else if (state_ptr->yu > 5120)
        state_ptr->yu = 5120;

    /* FILTE & DELAY */
    /* update steady state step size multiplier */
    state_ptr->yl += state_ptr->yu + ((-state_ptr->yl) >> 6);

    /*
     * Adaptive predictor coefficients.
     */
    if (tr == 1) {            /* reset a's and b's for modem signal */
        state_ptr->a[0] = 0;
        state_ptr->a[1] = 0;
        state_ptr->b[0] = 0;
        state_ptr->b[1] = 0;
        state_ptr->b[2] = 0;
        state_ptr->b[3] = 0;
        state_ptr->b[4] = 0;
        state_ptr->b[5] = 0;

        a2p = 0;        /* eliminate Compiler Warnings */
    } else {            /* update a's and b's */
        pks1 = pk0 ^ state_ptr->pk[0];        /* UPA2 */

        /* update predictor pole a[1] */
        a2p = state_ptr->a[1] - (state_ptr->a[1] >> 7);
        if (dqsez != 0) {
            fa1 = (pks1) ? state_ptr->a[0] : -state_ptr->a[0];
            if (fa1 < -8191)    /* a2p = function of fa1 */
                a2p -= 0x100;
            else if (fa1 > 8191)
                a2p += 0xFF;
            else
                a2p += fa1 >> 5;

            if (pk0 ^ state_ptr->pk[1])
                /* LIMC */
                if (a2p <= -12160)
                    a2p = -12288;
                else if (a2p >= 12416)
                    a2p = 12288;
                else
                    a2p -= 0x80;
            else if (a2p <= -12416)
                a2p = -12288;
            else if (a2p >= 12160)
                a2p = 12288;
            else
                a2p += 0x80;
        }

        /* TRIGB & DELAY */
        state_ptr->a[1] = a2p;

        /* UPA1 */
        /* update predictor pole a[0] */
        state_ptr->a[0] -= state_ptr->a[0] >> 8;
        if (dqsez != 0)
            if (pks1 == 0)
                state_ptr->a[0] += 192;
            else
                state_ptr->a[0] -= 192;

        /* LIMD */
        a1ul = 15360 - a2p;
        if (state_ptr->a[0] < -a1ul)
            state_ptr->a[0] = -a1ul;
        else if (state_ptr->a[0] > a1ul)
            state_ptr->a[0] = a1ul;

        /* UPB : update predictor zeros b[6] */
        for (cnt = 0; cnt < 6; cnt++) {
            if (code_size == 5)        /* for 40Kbps G.723 */
                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 ((dq ^ state_ptr->dq[cnt]) >= 0)
                    state_ptr->b[cnt] += 128;
                else
                    state_ptr->b[cnt] -= 128;
            }
        }
    }

    for (cnt = 5; cnt > 0; cnt--)
        state_ptr->dq[cnt] = state_ptr->dq[cnt-1];
    /* 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;
    } else {
        state_ptr->dq[0] = (dq >= 0) ?
             base2 (mag) : base2 (mag) - 0x400;
    }

    state_ptr->sr[1] = state_ptr->sr[0];
    /* 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) {
        state_ptr->sr[0] = base2(sr);
    } else if (sr > -32768) {
        mag = -sr;
        state_ptr->sr[0] = base2(mag) - 0x400;
    } else {
        const unsigned short c = 0xFC20;
        state_ptr->sr[0] = short(c);
    }

    /* DELAY A */
    state_ptr->pk[1] = state_ptr->pk[0];
    state_ptr->pk[0] = pk0;

    /* TONE */
    if (tr == 1)        /* this sample has been treated as data */
        state_ptr->td = 0;    /* next one will be treated as voice */
    else if (a2p < -11776)    /* small sample-to-sample correlation */
        state_ptr->td = 1;    /* signal may be data */
    else                      /* signal is voice */
        state_ptr->td = 0;

    /*
     * Adaptation speed control.
     */
    state_ptr->dms += (fi - state_ptr->dms) >> 5;        /* FILTA */
    state_ptr->dml += (((fi << 2) - state_ptr->dml) >> 7);    /* FILTB */

    if (tr == 1)
        state_ptr->ap = 256;
    else if (y < 1536)                    /* SUBTC */
        state_ptr->ap += (0x200 - state_ptr->ap) >> 4;
    else if (state_ptr->td == 1)
        state_ptr->ap += (0x200 - state_ptr->ap) >> 4;
    else if (abs((state_ptr->dms << 2) - state_ptr->dml) >=
        (state_ptr->dml >> 3))
        state_ptr->ap += (0x200 - state_ptr->ap) >> 4;
    else
        state_ptr->ap += (-state_ptr->ap) >> 4;
}

/*
 * tandem_adjust(sr, se, y, i, sign)
 *
 * At the end of ADPCM decoding, it simulates an encoder which may be receiving
 * the output of this decoder as a tandem process. If the output of the
 * simulated encoder differs from the input to this decoder, the decoder output
 * is adjusted by one level of A-law or u-law codes.
 *
 * Input:
 *    sr    decoder output linear PCM sample,
 *    se    predictor estimate sample,
 *    y     quantizer step size,
 *    i     decoder input code,
 *    sign  sign bit of code i
 *
 * Return:
 *    adjusted A-law or u-law compressed sample.
 */
int
tandem_adjust_alaw(
    int        sr,    /* decoder output linear PCM sample */
    int        se,    /* predictor estimate sample */
    int        y,     /* quantizer step size */
    int        i,     /* decoder input code */
    int        sign,
    short     *qtab)
{
    unsigned char    sp;    /* A-law compressed 8-bit code */
    short            dx;    /* prediction error */
    char             id;    /* quantized prediction error */
    int              sd;    /* adjusted A-law decoded sample value */
    int              im;    /* biased magnitude of i */
    int              imx;   /* biased magnitude of id */

    if (sr <= -32768)
        sr = -1;
    sp = linear2alaw((sr >> 1) << 3);    /* short to A-law compression */
    dx = (alaw2linear(sp) >> 2) - se;    /* 16-bit prediction error */
    id = quantize(dx, y, qtab, sign - 1);

    if (id == i) {            /* no adjustment on sp */
        return (sp);
    } else {            /* sp adjustment needed */
        /* ADPCM codes : 8, 9, ... F, 0, 1, ... , 6, 7 */
        im = i ^ sign;        /* 2's complement to biased unsigned */
        imx = id ^ sign;

        if (imx > im) {        /* sp adjusted to next lower value */
            if (sp & 0x80) {
                sd = (sp == 0xD5) ? 0x55 :
                    ((sp ^ 0x55) - 1) ^ 0x55;
            } else {
                sd = (sp == 0x2A) ? 0x2A :
                    ((sp ^ 0x55) + 1) ^ 0x55;
            }
        } else {        /* sp adjusted to next higher value */
            if (sp & 0x80)
                sd = (sp == 0xAA) ? 0xAA :
                    ((sp ^ 0x55) + 1) ^ 0x55;
            else
                sd = (sp == 0x55) ? 0xD5 :
                    ((sp ^ 0x55) - 1) ^ 0x55;
        }
        return (sd);
    }
}

int
tandem_adjust_ulaw(
    int        sr,    /* decoder output linear PCM sample */
    int        se,    /* predictor estimate sample */
    int        y,    /* quantizer step size */
    int        i,    /* decoder input code */
    int        sign,
    short     *qtab)
{
    unsigned char    sp;    /* u-law compressed 8-bit code */
    short            dx;    /* prediction error */
    char             id;    /* quantized prediction error */
    int              sd;    /* adjusted u-law decoded sample value */
    int              im;    /* biased magnitude of i */
    int              imx;   /* biased magnitude of id */

    if (sr <= -32768)
        sr = 0;
    sp = linear2ulaw(sr << 2);    /* short to u-law compression */
    dx = (ulaw2linear(sp) >> 2) - se;    /* 16-bit prediction error */
    id = quantize(dx, y, qtab, sign - 1);
    if (id == i) {
        return (sp);
    } else {
        /* ADPCM codes : 8, 9, ... F, 0, 1, ... , 6, 7 */
        im = i ^ sign;        /* 2's complement to biased unsigned */
        imx = id ^ sign;
        if (imx > im) {        /* sp adjusted to next lower value */
            if (sp & 0x80)
                sd = (sp == 0xFF) ? 0x7E : sp + 1;
            else
                sd = (sp == 0) ? 0 : sp - 1;

        } else {        /* sp adjusted to next higher value */
            if (sp & 0x80)
                sd = (sp == 0x80) ? 0x80 : sp - 1;
            else
                sd = (sp == 0x7F) ? 0xFE : sp + 1;
        }
        return (sd);
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -