Exemplo n.º 1
0
        /// <summary>
        /// Encodes a buffer of G722
        /// </summary>
        /// <param name="state">Codec state</param>
        /// <param name="outputBuffer">Output buffer (to contain encoded G722)</param>
        /// <param name="inputBuffer">PCM 16 bit samples to encode</param>
        /// <param name="inputBufferCount">Number of samples in the input buffer to encode</param>
        /// <returns>Number of encoded bytes written into output buffer</returns>
        public int Encode(G722CodecState state, byte[] outputBuffer, short[] inputBuffer, int inputBufferCount)
        {
            int dlow;
            int dhigh;
            int el;
            int wd;
            int wd1;
            int ril;
            int wd2;
            int il4;
            int ih2;
            int wd3;
            int eh;
            int mih;
            int i;
            int j;
            // Low and high band PCM from the QMF
            int xlow;
            int xhigh;
            int g722_bytes;
            // Even and odd tap accumulators
            int sumeven;
            int sumodd;
            int ihigh;
            int ilow;
            int code;

            g722_bytes = 0;
            xhigh      = 0;
            for (j = 0; j < inputBufferCount;)
            {
                if (state.ItuTestMode)
                {
                    xlow      =
                        xhigh = inputBuffer[j++] >> 1;
                }
                else
                {
                    if (state.EncodeFrom8000Hz)
                    {
                        xlow = inputBuffer[j++] >> 1;
                    }
                    else
                    {
                        // Apply the transmit QMF
                        // Shuffle the buffer down
                        for (i = 0; i < 22; i++)
                        {
                            state.QmfSignalHistory[i] = state.QmfSignalHistory[i + 2];
                        }
                        state.QmfSignalHistory[22] = inputBuffer[j++];
                        state.QmfSignalHistory[23] = inputBuffer[j++];

                        // Discard every other QMF output
                        sumeven = 0;
                        sumodd  = 0;
                        for (i = 0; i < 12; i++)
                        {
                            sumodd  += state.QmfSignalHistory[2 * i] * qmf_coeffs[i];
                            sumeven += state.QmfSignalHistory[2 * i + 1] * qmf_coeffs[11 - i];
                        }
                        xlow  = (sumeven + sumodd) >> 14;
                        xhigh = (sumeven - sumodd) >> 14;
                    }
                }
                // Block 1L, SUBTRA
                el = Saturate(xlow - state.Band[0].s);

                // Block 1L, QUANTL
                wd = (el >= 0) ? el : -(el + 1);

                for (i = 1; i < 30; i++)
                {
                    wd1 = (q6[i] * state.Band[0].det) >> 12;
                    if (wd < wd1)
                    {
                        break;
                    }
                }
                ilow = (el < 0) ? iln[i] : ilp[i];

                // Block 2L, INVQAL
                ril  = ilow >> 2;
                wd2  = qm4[ril];
                dlow = (state.Band[0].det * wd2) >> 15;

                // Block 3L, LOGSCL
                il4 = rl42[ril];
                wd  = (state.Band[0].nb * 127) >> 7;
                state.Band[0].nb = wd + wl[il4];
                if (state.Band[0].nb < 0)
                {
                    state.Band[0].nb = 0;
                }
                else if (state.Band[0].nb > 18432)
                {
                    state.Band[0].nb = 18432;
                }

                // Block 3L, SCALEL
                wd1 = (state.Band[0].nb >> 6) & 31;
                wd2 = 8 - (state.Band[0].nb >> 11);
                wd3 = (wd2 < 0) ? (ilb[wd1] << -wd2) : (ilb[wd1] >> wd2);
                state.Band[0].det = wd3 << 2;

                Block4(state, 0, dlow);

                if (state.EncodeFrom8000Hz)
                {
                    // Just leave the high bits as zero
                    code = (0xC0 | ilow) >> (8 - state.BitsPerSample);
                }
                else
                {
                    // Block 1H, SUBTRA
                    eh = Saturate(xhigh - state.Band[1].s);

                    // Block 1H, QUANTH
                    wd    = (eh >= 0) ? eh : -(eh + 1);
                    wd1   = (564 * state.Band[1].det) >> 12;
                    mih   = (wd >= wd1) ? 2 : 1;
                    ihigh = (eh < 0) ? ihn[mih] : ihp[mih];

                    // Block 2H, INVQAH
                    wd2   = qm2[ihigh];
                    dhigh = (state.Band[1].det * wd2) >> 15;

                    // Block 3H, LOGSCH
                    ih2 = rh2[ihigh];
                    wd  = (state.Band[1].nb * 127) >> 7;
                    state.Band[1].nb = wd + wh[ih2];
                    if (state.Band[1].nb < 0)
                    {
                        state.Band[1].nb = 0;
                    }
                    else if (state.Band[1].nb > 22528)
                    {
                        state.Band[1].nb = 22528;
                    }

                    // Block 3H, SCALEH
                    wd1 = (state.Band[1].nb >> 6) & 31;
                    wd2 = 10 - (state.Band[1].nb >> 11);
                    wd3 = (wd2 < 0) ? (ilb[wd1] << -wd2) : (ilb[wd1] >> wd2);
                    state.Band[1].det = wd3 << 2;

                    Block4(state, 1, dhigh);
                    code = ((ihigh << 6) | ilow) >> (8 - state.BitsPerSample);
                }

                if (state.Packed)
                {
                    // Pack the code bits
                    state.OutBuffer |= (uint)(code << state.OutBits);
                    state.OutBits   += state.BitsPerSample;
                    if (state.OutBits >= 8)
                    {
                        outputBuffer[g722_bytes++] = (byte)(state.OutBuffer & 0xFF);
                        state.OutBits    -= 8;
                        state.OutBuffer >>= 8;
                    }
                }
                else
                {
                    outputBuffer[g722_bytes++] = (byte)code;
                }
            }
            return(g722_bytes);
        }
Exemplo n.º 2
0
        static void Block4(G722CodecState s, int band, int d)
        {
            int wd1;
            int wd2;
            int wd3;
            int i;

            // Block 4, RECONS
            s.Band[band].d[0] = d;
            s.Band[band].r[0] = Saturate(s.Band[band].s + d);

            // Block 4, PARREC
            s.Band[band].p[0] = Saturate(s.Band[band].sz + d);

            // Block 4, UPPOL2
            for (i = 0; i < 3; i++)
            {
                s.Band[band].sg[i] = s.Band[band].p[i] >> 15;
            }
            wd1 = Saturate(s.Band[band].a[1] << 2);

            wd2 = (s.Band[band].sg[0] == s.Band[band].sg[1]) ? -wd1 : wd1;
            if (wd2 > 32767)
            {
                wd2 = 32767;
            }
            wd3  = (s.Band[band].sg[0] == s.Band[band].sg[2]) ? 128 : -128;
            wd3 += (wd2 >> 7);
            wd3 += (s.Band[band].a[2] * 32512) >> 15;
            if (wd3 > 12288)
            {
                wd3 = 12288;
            }
            else if (wd3 < -12288)
            {
                wd3 = -12288;
            }
            s.Band[band].ap[2] = wd3;

            // Block 4, UPPOL1
            s.Band[band].sg[0] = s.Band[band].p[0] >> 15;
            s.Band[band].sg[1] = s.Band[band].p[1] >> 15;
            wd1 = (s.Band[band].sg[0] == s.Band[band].sg[1]) ? 192 : -192;
            wd2 = (s.Band[band].a[1] * 32640) >> 15;

            s.Band[band].ap[1] = Saturate(wd1 + wd2);
            wd3 = Saturate(15360 - s.Band[band].ap[2]);
            if (s.Band[band].ap[1] > wd3)
            {
                s.Band[band].ap[1] = wd3;
            }
            else if (s.Band[band].ap[1] < -wd3)
            {
                s.Band[band].ap[1] = -wd3;
            }

            // Block 4, UPZERO
            wd1 = (d == 0) ? 0 : 128;
            s.Band[band].sg[0] = d >> 15;
            for (i = 1; i < 7; i++)
            {
                s.Band[band].sg[i] = s.Band[band].d[i] >> 15;
                wd2 = (s.Band[band].sg[i] == s.Band[band].sg[0]) ? wd1 : -wd1;
                wd3 = (s.Band[band].b[i] * 32640) >> 15;
                s.Band[band].bp[i] = Saturate(wd2 + wd3);
            }

            // Block 4, DELAYA
            for (i = 6; i > 0; i--)
            {
                s.Band[band].d[i] = s.Band[band].d[i - 1];
                s.Band[band].b[i] = s.Band[band].bp[i];
            }

            for (i = 2; i > 0; i--)
            {
                s.Band[band].r[i] = s.Band[band].r[i - 1];
                s.Band[band].p[i] = s.Band[band].p[i - 1];
                s.Band[band].a[i] = s.Band[band].ap[i];
            }

            // Block 4, FILTEP
            wd1             = Saturate(s.Band[band].r[1] + s.Band[band].r[1]);
            wd1             = (s.Band[band].a[1] * wd1) >> 15;
            wd2             = Saturate(s.Band[band].r[2] + s.Band[band].r[2]);
            wd2             = (s.Band[band].a[2] * wd2) >> 15;
            s.Band[band].sp = Saturate(wd1 + wd2);

            // Block 4, FILTEZ
            s.Band[band].sz = 0;
            for (i = 6; i > 0; i--)
            {
                wd1              = Saturate(s.Band[band].d[i] + s.Band[band].d[i]);
                s.Band[band].sz += (s.Band[band].b[i] * wd1) >> 15;
            }
            s.Band[band].sz = Saturate(s.Band[band].sz);

            // Block 4, PREDIC
            s.Band[band].s = Saturate(s.Band[band].sp + s.Band[band].sz);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Decodes a buffer of G722
        /// </summary>
        /// <param name="state">Codec state</param>
        /// <param name="outputBuffer">Output buffer (to contain decompressed PCM samples)</param>
        /// <param name="inputG722Data"></param>
        /// <param name="inputLength">Number of bytes in input G722 data to decode</param>
        /// <returns>Number of samples written into output buffer</returns>
        public int Decode(G722CodecState state, short[] outputBuffer, byte[] inputG722Data, int inputLength)
        {
            int dlowt;
            int rlow;
            int ihigh;
            int dhigh;
            int rhigh;
            int xout1;
            int xout2;
            int wd1;
            int wd2;
            int wd3;
            int code;
            int outlen;
            int i;
            int j;

            outlen = 0;
            rhigh  = 0;
            for (j = 0; j < inputLength;)
            {
                if (state.Packed)
                {
                    // Unpack the code bits
                    if (state.InBits < state.BitsPerSample)
                    {
                        state.InBuffer |= (uint)(inputG722Data[j++] << state.InBits);
                        state.InBits   += 8;
                    }
                    code             = (int)state.InBuffer & ((1 << state.BitsPerSample) - 1);
                    state.InBuffer >>= state.BitsPerSample;
                    state.InBits    -= state.BitsPerSample;
                }
                else
                {
                    code = inputG722Data[j++];
                }

                switch (state.BitsPerSample)
                {
                default:
                case 8:
                    wd1   = code & 0x3F;
                    ihigh = (code >> 6) & 0x03;
                    wd2   = qm6[wd1];
                    wd1 >>= 2;
                    break;

                case 7:
                    wd1   = code & 0x1F;
                    ihigh = (code >> 5) & 0x03;
                    wd2   = qm5[wd1];
                    wd1 >>= 1;
                    break;

                case 6:
                    wd1   = code & 0x0F;
                    ihigh = (code >> 4) & 0x03;
                    wd2   = qm4[wd1];
                    break;
                }

                // Block 5L, LOW BAND INVQBL
                wd2 = (state.Band[0].det * wd2) >> 15;

                // Block 5L, RECONS
                rlow = state.Band[0].s + wd2;

                // Block 6L, LIMIT
                if (rlow > 16383)
                {
                    rlow = 16383;
                }
                else if (rlow < -16384)
                {
                    rlow = -16384;
                }

                // Block 2L, INVQAL
                wd2   = qm4[wd1];
                dlowt = (state.Band[0].det * wd2) >> 15;

                // Block 3L, LOGSCL
                wd2  = rl42[wd1];
                wd1  = (state.Band[0].nb * 127) >> 7;
                wd1 += wl[wd2];
                if (wd1 < 0)
                {
                    wd1 = 0;
                }
                else if (wd1 > 18432)
                {
                    wd1 = 18432;
                }
                state.Band[0].nb = wd1;

                // Block 3L, SCALEL
                wd1 = (state.Band[0].nb >> 6) & 31;
                wd2 = 8 - (state.Band[0].nb >> 11);
                wd3 = (wd2 < 0) ? (ilb[wd1] << -wd2) : (ilb[wd1] >> wd2);
                state.Band[0].det = wd3 << 2;

                Block4(state, 0, dlowt);

                if (!state.EncodeFrom8000Hz)
                {
                    // Block 2H, INVQAH
                    wd2   = qm2[ihigh];
                    dhigh = (state.Band[1].det * wd2) >> 15;

                    // Block 5H, RECONS
                    rhigh = dhigh + state.Band[1].s;

                    // Block 6H, LIMIT
                    if (rhigh > 16383)
                    {
                        rhigh = 16383;
                    }
                    else if (rhigh < -16384)
                    {
                        rhigh = -16384;
                    }

                    // Block 2H, INVQAH
                    wd2  = rh2[ihigh];
                    wd1  = (state.Band[1].nb * 127) >> 7;
                    wd1 += wh[wd2];
                    if (wd1 < 0)
                    {
                        wd1 = 0;
                    }
                    else if (wd1 > 22528)
                    {
                        wd1 = 22528;
                    }
                    state.Band[1].nb = wd1;

                    // Block 3H, SCALEH
                    wd1 = (state.Band[1].nb >> 6) & 31;
                    wd2 = 10 - (state.Band[1].nb >> 11);
                    wd3 = (wd2 < 0) ? (ilb[wd1] << -wd2) : (ilb[wd1] >> wd2);
                    state.Band[1].det = wd3 << 2;

                    Block4(state, 1, dhigh);
                }

                if (state.ItuTestMode)
                {
                    outputBuffer[outlen++] = (short)(rlow << 1);
                    outputBuffer[outlen++] = (short)(rhigh << 1);
                }
                else
                {
                    if (state.EncodeFrom8000Hz)
                    {
                        outputBuffer[outlen++] = (short)(rlow << 1);
                    }
                    else
                    {
                        // Apply the receive QMF
                        for (i = 0; i < 22; i++)
                        {
                            state.QmfSignalHistory[i] = state.QmfSignalHistory[i + 2];
                        }
                        state.QmfSignalHistory[22] = rlow + rhigh;
                        state.QmfSignalHistory[23] = rlow - rhigh;

                        xout1 = 0;
                        xout2 = 0;
                        for (i = 0; i < 12; i++)
                        {
                            xout2 += state.QmfSignalHistory[2 * i] * qmf_coeffs[i];
                            xout1 += state.QmfSignalHistory[2 * i + 1] * qmf_coeffs[11 - i];
                        }
                        outputBuffer[outlen++] = (short)(xout1 >> 11);
                        outputBuffer[outlen++] = (short)(xout2 >> 11);
                    }
                }
            }
            return(outlen);
        }