Exemplo n.º 1
0
        public int Encode(G722CodecState state, byte[] outputBuffer, short[] inputBuffer, int inputBufferCount)
        {
            int result = 0;
            int num    = 0;
            int i      = 0;

            while (i < inputBufferCount)
            {
                int num2;
                int j;
                if (state.ItuTestMode)
                {
                    num = (num2 = inputBuffer[i++] >> 1);
                }
                else if (state.EncodeFrom8000Hz)
                {
                    num2 = inputBuffer[i++] >> 1;
                }
                else
                {
                    for (j = 0; j < 22; j++)
                    {
                        state.QmfSignalHistory[j] = state.QmfSignalHistory[j + 2];
                    }
                    state.QmfSignalHistory[22] = (int)inputBuffer[i++];
                    state.QmfSignalHistory[23] = (int)inputBuffer[i++];
                    int num3 = 0;
                    int num4 = 0;
                    for (j = 0; j < 12; j++)
                    {
                        num4 += state.QmfSignalHistory[2 * j] * G722Codec.qmf_coeffs[j];
                        num3 += state.QmfSignalHistory[2 * j + 1] * G722Codec.qmf_coeffs[11 - j];
                    }
                    num2 = num3 + num4 >> 14;
                    num  = num3 - num4 >> 14;
                }
                int num5 = (int)G722Codec.Saturate(num2 - state.Band[0].s);
                int num6 = (num5 >= 0) ? num5 : (-(num5 + 1));
                int num7;
                for (j = 1; j < 30; j++)
                {
                    num7 = G722Codec.q6[j] * state.Band[0].det >> 12;
                    if (num6 < num7)
                    {
                        break;
                    }
                }
                int num8  = (num5 < 0) ? G722Codec.iln[j] : G722Codec.ilp[j];
                int num9  = num8 >> 2;
                int num10 = G722Codec.qm4[num9];
                int d     = state.Band[0].det * num10 >> 15;
                int num11 = G722Codec.rl42[num9];
                num6             = state.Band[0].nb * 127 >> 7;
                state.Band[0].nb = num6 + G722Codec.wl[num11];
                if (state.Band[0].nb < 0)
                {
                    state.Band[0].nb = 0;
                }
                else if (state.Band[0].nb > 18432)
                {
                    state.Band[0].nb = 18432;
                }
                num7  = (state.Band[0].nb >> 6 & 31);
                num10 = 8 - (state.Band[0].nb >> 11);
                int num12 = (num10 < 0) ? (G722Codec.ilb[num7] << -num10) : (G722Codec.ilb[num7] >> num10);
                state.Band[0].det = num12 << 2;
                G722Codec.Block4(state, 0, d);
                int num13;
                if (state.EncodeFrom8000Hz)
                {
                    num13 = (192 | num8) >> 8 - state.BitsPerSample;
                }
                else
                {
                    int num14 = (int)G722Codec.Saturate(num - state.Band[1].s);
                    num6 = ((num14 >= 0) ? num14 : (-(num14 + 1)));
                    num7 = 564 * state.Band[1].det >> 12;
                    int num15 = (num6 >= num7) ? 2 : 1;
                    int num16 = (num14 < 0) ? G722Codec.ihn[num15] : G722Codec.ihp[num15];
                    num10 = G722Codec.qm2[num16];
                    int d2    = state.Band[1].det * num10 >> 15;
                    int num17 = G722Codec.rh2[num16];
                    num6             = state.Band[1].nb * 127 >> 7;
                    state.Band[1].nb = num6 + G722Codec.wh[num17];
                    if (state.Band[1].nb < 0)
                    {
                        state.Band[1].nb = 0;
                    }
                    else if (state.Band[1].nb > 22528)
                    {
                        state.Band[1].nb = 22528;
                    }
                    num7              = (state.Band[1].nb >> 6 & 31);
                    num10             = 10 - (state.Band[1].nb >> 11);
                    num12             = ((num10 < 0) ? (G722Codec.ilb[num7] << -num10) : (G722Codec.ilb[num7] >> num10));
                    state.Band[1].det = num12 << 2;
                    G722Codec.Block4(state, 1, d2);
                    num13 = (num16 << 6 | num8) >> 8 - state.BitsPerSample;
                }
                if (state.Packed)
                {
                    state.OutBuffer |= (uint)((uint)num13 << state.OutBits);
                    state.OutBits   += state.BitsPerSample;
                    if (state.OutBits >= 8)
                    {
                        outputBuffer[result++] = (byte)(state.OutBuffer & 255u);
                        state.OutBits         -= 8;
                        state.OutBuffer      >>= 8;
                    }
                }
                else
                {
                    outputBuffer[result++] = (byte)num13;
                }
            }
            return(result);
        }
Exemplo n.º 2
0
        private static void Block4(G722CodecState s, int band, int d)
        {
            s.Band[band].d[0] = d;
            s.Band[band].r[0] = (int)G722Codec.Saturate(s.Band[band].s + d);
            s.Band[band].p[0] = (int)G722Codec.Saturate(s.Band[band].sz + d);
            for (int i = 0; i < 3; i++)
            {
                s.Band[band].sg[i] = s.Band[band].p[i] >> 15;
            }
            int num  = (int)G722Codec.Saturate(s.Band[band].a[1] << 2);
            int num2 = (s.Band[band].sg[0] == s.Band[band].sg[1]) ? (-num) : num;

            if (num2 > 32767)
            {
                num2 = 32767;
            }
            int num3 = (s.Band[band].sg[0] == s.Band[band].sg[2]) ? 128 : -128;

            num3 += num2 >> 7;
            num3 += s.Band[band].a[2] * 32512 >> 15;
            if (num3 > 12288)
            {
                num3 = 12288;
            }
            else if (num3 < -12288)
            {
                num3 = -12288;
            }
            s.Band[band].ap[2] = num3;
            s.Band[band].sg[0] = s.Band[band].p[0] >> 15;
            s.Band[band].sg[1] = s.Band[band].p[1] >> 15;
            num  = ((s.Band[band].sg[0] == s.Band[band].sg[1]) ? 192 : -192);
            num2 = s.Band[band].a[1] * 32640 >> 15;
            s.Band[band].ap[1] = (int)G722Codec.Saturate(num + num2);
            num3 = (int)G722Codec.Saturate(15360 - s.Band[band].ap[2]);
            if (s.Band[band].ap[1] > num3)
            {
                s.Band[band].ap[1] = num3;
            }
            else if (s.Band[band].ap[1] < -num3)
            {
                s.Band[band].ap[1] = -num3;
            }
            num = ((d == 0) ? 0 : 128);
            s.Band[band].sg[0] = d >> 15;
            for (int i = 1; i < 7; i++)
            {
                s.Band[band].sg[i] = s.Band[band].d[i] >> 15;
                num2 = ((s.Band[band].sg[i] == s.Band[band].sg[0]) ? num : (-num));
                num3 = s.Band[band].b[i] * 32640 >> 15;
                s.Band[band].bp[i] = (int)G722Codec.Saturate(num2 + num3);
            }
            for (int 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 (int 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];
            }
            num             = (int)G722Codec.Saturate(s.Band[band].r[1] + s.Band[band].r[1]);
            num             = s.Band[band].a[1] * num >> 15;
            num2            = (int)G722Codec.Saturate(s.Band[band].r[2] + s.Band[band].r[2]);
            num2            = s.Band[band].a[2] * num2 >> 15;
            s.Band[band].sp = (int)G722Codec.Saturate(num + num2);
            s.Band[band].sz = 0;
            for (int i = 6; i > 0; i--)
            {
                num              = (int)G722Codec.Saturate(s.Band[band].d[i] + s.Band[band].d[i]);
                s.Band[band].sz += s.Band[band].b[i] * num >> 15;
            }
            s.Band[band].sz = (int)G722Codec.Saturate(s.Band[band].sz);
            s.Band[band].s  = (int)G722Codec.Saturate(s.Band[band].sp + s.Band[band].sz);
        }