Esempio n. 1
0
 public G722ChatCodec()
 {
     bitrate      = 64000;
     encoderState = new G722CodecState(bitrate, G722Flags.None);
     decoderState = new G722CodecState(bitrate, G722Flags.None);
     codec        = new G722Codec();
     RecordFormat = new WaveFormat(16000, 1);
 }
Esempio n. 2
0
 public G722ChatCodec()
 {
     BitsPerSecond = 64000;
     _encoderState = new G722CodecState(BitsPerSecond, G722Flags.None);
     _decoderState = new G722CodecState(BitsPerSecond, G722Flags.None);
     _codec        = new G722Codec();
     RecordFormat  = new WaveFormat(16000, 1);
 }
Esempio n. 3
0
 public G722ChatCodec()
 {
     this.bitrate         = 64000;
     this.encoderState    = new G722CodecState(bitrate, G722Flags.None);
     this.decoderState    = new G722CodecState(bitrate, G722Flags.None);
     this.codec           = new G722Codec();
     this.recordingFormat = new WaveFormat(16000, 1);
 }
Esempio n. 4
0
 public G722ChatCodec()
 {
     Bitrate      = 64000;
     EncoderState = new G722CodecState(Bitrate, G722Flags.None);
     DecoderState = new G722CodecState(Bitrate, G722Flags.None);
     Codec        = new G722Codec();
     RecordFormat = new WaveFormat(16000, 1);
 }
Esempio n. 5
0
        // public int Decode(short[] pcm, byte[] encoded)
        // {
        //     if (g722Codec == null)
        //     {
        //         g722Codec = new G722Codec();
        //         g722CodecState = new G722CodecState(G722_BIT_RATE, G722Flags.None);
        //     }
        //     var requiredDecodedSampleCount = encoded.Length * 2;
        //     if (pcm.Length < requiredDecodedSampleCount)
        //     {
        //         return -1;
        //     }
        //     var decodedSampleCount = g722Codec.Decode(g722CodecState, pcm,
        //         encoded, encoded.Length);
        //     return decodedSampleCount;
        // }
        public short[] Decode(byte[] encoded)
        {
            if (g722Codec == null)
            {
                g722Codec = new G722Codec();
                g722CodecState = new G722CodecState(G722_BIT_RATE, G722Flags.None);
            }

            var requiredDecodedSampleCount = encoded.Length * 2;
            var pcm = new short[requiredDecodedSampleCount];

            g722Codec.Decode(g722CodecState,pcm,encoded,encoded.Length);
            return pcm;
        }
Esempio n. 6
0
        // public int Encode(byte[] encoded, short[] pcm)
        // {
        //     if (g722Codec == null)
        //     {
        //         g722Codec = new G722Codec();
        //         g722CodecState = new G722CodecState(G722_BIT_RATE, G722Flags.None);
        //     }

        //     var requiredEncodedByteCount = pcm.Length / 2;
        //     if (encoded.Length < requiredEncodedByteCount)
        //     {
        //         return -1;
        //     }

        //     var encodedByteCount = g722Codec.Encode(g722CodecState, encoded,
        //         pcm, pcm.Length);
        //     return encodedByteCount;
        // }

        public byte[] Encode(short[] pcm)
        {
            if (g722Codec == null)
            {
                g722Codec      = new G722Codec();
                g722CodecState = new G722CodecState(G722_BIT_RATE, G722Flags.None);
            }

            var requiredEncodedByteCount = pcm.Length / 2;
            var enc = new byte[requiredEncodedByteCount];

            g722Codec.Encode(g722CodecState, enc, pcm, pcm.Length);
            return(enc);
        }
Esempio n. 7
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);
        }
Esempio n. 8
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 static 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 = 0;
            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;

            Band band0 = state.Band[0], band1 = state.Band[1];

            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 - band0.s);

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

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

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

                // Block 3L, LOGSCL
                il4      = rl42[ril];
                wd       = (band0.nb * 127) >> 7;
                band0.nb = wd + wl[il4];

                //if (band0.nb < 0)
                //    band0.nb = 0;
                //else if (band0.nb > 18432)
                //    band0.nb = 18432;

                //Could cache Band0
                band0.nb = Common.Binary.Clamp(band0.nb, 0, 18432);

                // Block 3L, SCALEL
                wd1       = (band0.nb >> 6) & 31;
                wd2       = 8 - (band0.nb >> 11);
                wd3       = (wd2 < 0) ? (ilb[wd1] << -wd2) : (ilb[wd1] >> wd2);
                band0.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 - band1.s);

                    // Block 1H, QUANTH
                    wd  = (eh >= 0) ? eh : -(eh + 1);
                    wd1 = (564 * band1.det) >> 12;
                    mih = (wd >= wd1) ? 2 : 1;

                    //Lookup correct value in array
                    //ihigh = (eh < 0) ? ihn[mih] : ihp[mih];

                    //Calculate correct value without array
                    ihigh = (eh < 0) ? (mih == 1 ? 1 : 0) : mih == 1 ? 3 : mih;

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

                    // Block 3H, LOGSCH
                    //Lookup with array
                    //ih2 = rh2[ihigh];

                    //Avoid lookup, ihigh == 0 or even gets 2, odd gets 1
                    ih2 = Common.Binary.IsEven(ref ihigh) ? 2 : 1;

                    wd       = (band1.nb * 127) >> 7;
                    band1.nb = wd + wh[ih2];

                    //Clamp?

                    band1.nb = Common.Binary.Clamp(band1.nb, 0, 22528);

                    //if (band1.nb < 0)
                    //    band1.nb = 0;
                    //else if (band1.nb > 22528)
                    //    band1.nb = 22528;

                    // Block 3H, SCALEH
                    wd1       = (band1.nb >> 6) & 31;
                    wd2       = 10 - (band1.nb >> 11);
                    wd3       = (wd2 < 0) ? (ilb[wd1] << -wd2) : (ilb[wd1] >> wd2);
                    band1.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);
        }
Esempio n. 9
0
        //static readonly int[] ihn = { 0, 1, 0 };
        //static readonly int[] ihp = { 0, 3, 2 };

        /// <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 static 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;

            Band band0 = state.Band[0], band1 = state.Band[1];

            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 = (band0.det * wd2) >> 15;

                // Block 5L, RECONS
                rlow = band0.s + wd2;

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

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

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

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

                Block4(state, 0, dlowt);

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

                    // Block 5H, RECONS
                    rhigh = dhigh + band1.s;

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

                    // Block 2H, INVQAH
                    //Lookup with array
                    //wd2 = rh2[ihigh];

                    //Avoid lookup, ihigh == 0 or even gets 2, odd gets 1
                    wd2 = Common.Binary.IsEven(ref ihigh) ? 2 : 1;

                    wd1  = (band1.nb * 127) >> 7;
                    wd1 += wh[wd2];

                    //if (wd1 < 0)
                    //    wd1 = 0;
                    //else if (wd1 > 22528)
                    //    wd1 = 22528;

                    band1.nb = Common.Binary.Clamp(wd1, 0, 22528);

                    // Block 3H, SCALEH
                    wd1       = (band1.nb >> 6) & 31;
                    wd2       = 10 - (band1.nb >> 11);
                    wd3       = (wd2 < 0) ? (ilb[wd1] << -wd2) : (ilb[wd1] >> wd2);
                    band1.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);
        }