shithub: opus

Download patch

ref: dae5c333e1a054483541558ae54aee6bf8fbe6bc
parent: 894f1cf51b87a95b7faa782ca2e01c78bc0de610
author: Koen Vos <[email protected]>
date: Sun Jul 4 05:14:17 EDT 2010

Added an interface to encode/decode from a CELT range coder

--- a/interface/SKP_Silk_SDK_API.h
+++ b/interface/SKP_Silk_SDK_API.h
@@ -31,6 +31,8 @@
 #include "SKP_Silk_control.h"
 #include "SKP_Silk_typedef.h"
 #include "SKP_Silk_errors.h"
+#include "entenc.h"
+#include "entdec.h"
 
 #ifdef __cplusplus
 extern "C"
@@ -84,8 +86,8 @@
     const SKP_SILK_SDK_EncControlStruct *encControl,    /* I:   Control status                                  */
     const SKP_int16                     *samplesIn,     /* I:   Speech sample input vector                      */
     SKP_int                             nSamplesIn,     /* I:   Number of samples in input vector               */
-    SKP_uint8                           *outData,       /* O:   Encoded output vector                           */
-    SKP_int16                           *nBytesOut      /* I/O: Number of bytes in outData (input: Max bytes)   */
+    ec_enc                              *psRangeEnc,    /* I/O  Compressor data structure                       */
+    SKP_int16                           *nBytesOut      /* I/O: Number of bytes in payload (input: Max bytes)   */
 );
 
 /****************************************/
@@ -113,7 +115,7 @@
     void*                               decState,       /* I/O: State                                           */
     SKP_SILK_SDK_DecControlStruct*      decControl,     /* I/O: Control Structure                               */
     SKP_int                             lostFlag,       /* I:   0: no loss, 1 loss                              */
-    const SKP_uint8                     *inData,        /* I:   Encoded input vector                            */
+    ec_dec                              *psRangeDec,    /* I/O  Compressor data structure                       */
     const SKP_int                       nBytesIn,       /* I:   Number of input bytes                           */
     SKP_int16                           *samplesOut,    /* O:   Decoded output speech vector                    */
     SKP_int16                           *nSamplesOut    /* I/O: Number of samples (vector/decoded)              */
@@ -134,7 +136,7 @@
 /* Get table of contents for a packet */
 /**************************************/
 void SKP_Silk_SDK_get_TOC(
-    const SKP_uint8                     *inData,        /* I:   Encoded input vector                            */
+    ec_dec                              *psRangeDec,    /* I/O  Compressor data structure                   */
     const SKP_int16                     nBytesIn,       /* I:   Number of input bytes                           */
     SKP_Silk_TOC_struct                 *Silk_TOC       /* O:   Table of contents                               */
 );
--- a/src_FLP/SKP_Silk_encode_frame_FLP.c
+++ b/src_FLP/SKP_Silk_encode_frame_FLP.c
@@ -6,9 +6,9 @@
 /****************/
 SKP_int SKP_Silk_encode_frame_FLP( 
     SKP_Silk_encoder_state_FLP      *psEnc,             /* I/O  Encoder state FLP                       */
-          SKP_uint8                 *pCode,             /* O    Payload                                 */
-          SKP_int16                 *pnBytesOut,        /* I/O  Number of payload bytes;                */
+    SKP_int16                       *pnBytesOut,        /* I/O  Number of payload bytes;                */
                                                         /*      input: max length; output: used         */
+    ec_enc                          *psRangeEnc,        /* I/O  compressor data structure                */
     const SKP_int16                 *pIn                /* I    Input speech frame                      */
 )
 {
@@ -25,8 +25,6 @@
     SKP_uint8   LBRRpayload[ MAX_ARITHM_BYTES ];
     SKP_int16   nBytesLBRR;
 
-    ec_byte_buffer range_enc_celt_buf;
-
     const SKP_uint16 *FrameTermination_CDF;
 
 TIC(ENCODE_FRAME)
@@ -117,7 +115,7 @@
     /****************************************/
     nBytesLBRR = MAX_ARITHM_BYTES;
 TIC(LBRR)
-    SKP_Silk_LBRR_encode_FLP( psEnc, &sEncCtrl, LBRRpayload, &nBytesLBRR, xfw );
+    //SKP_Silk_LBRR_encode_FLP( psEnc, &sEncCtrl, LBRRpayload, &nBytesLBRR, xfw );
 TOC(LBRR)
 
     /*****************************************/
@@ -148,13 +146,9 @@
     }
 
     /****************************************/
-    /* Initialize arithmetic coder          */
+    /* Initialize range coder               */
     /****************************************/
     if( psEnc->sCmn.nFramesInPayloadBuf == 0 ) {
-        ec_byte_writeinit_buffer( &range_enc_celt_buf, psEnc->sCmn.sRC.buffer, MAX_ARITHM_BYTES );
-        ec_enc_init( &psEnc->sCmn.sRC.range_enc_celt_state, &range_enc_celt_buf );
-
-        SKP_Silk_range_enc_init( &psEnc->sCmn.sRC );
         psEnc->sCmn.nBytesInPayloadBuf = 0;
     }
 
@@ -162,8 +156,8 @@
     /* Encode Parameters                    */
     /****************************************/
 TIC(ENCODE_PARAMS)
-    SKP_Silk_encode_parameters_v4( &psEnc->sCmn, &sEncCtrl.sCmn, &psEnc->sCmn.sRC );
-    FrameTermination_CDF = SKP_Silk_FrameTermination_v4_CDF;
+    SKP_Silk_encode_parameters( &psEnc->sCmn, &sEncCtrl.sCmn, psRangeEnc );
+    FrameTermination_CDF = SKP_Silk_FrameTermination_CDF;
 TOC(ENCODE_PARAMS)
 
     /****************************************/
@@ -178,7 +172,7 @@
     psEnc->sCmn.prevLag      = sEncCtrl.sCmn.pitchL[ psEnc->sCmn.nb_subfr - 1 ];
     psEnc->sCmn.first_frame_after_reset = 0;
 
-    if( psEnc->sCmn.sRC.error ) {
+    if( 0 ) { //psEnc->sCmn.sRC.error ) {
         /* Encoder returned error: Clear payload buffer */
         psEnc->sCmn.nFramesInPayloadBuf = 0;
     } else {
@@ -203,21 +197,23 @@
         }
 
         /* Add the frame termination info to stream */
-        SKP_Silk_range_encoder( &psEnc->sCmn.sRC, frame_terminator, FrameTermination_CDF );
+        ec_encode_bin( psRangeEnc, FrameTermination_CDF[ frame_terminator ], 
+            FrameTermination_CDF[ frame_terminator + 1 ], 16 );
         for( i = 0; i < psEnc->sCmn.nFramesInPayloadBuf; i++ ) {
-            SKP_Silk_encode_pulses( &psEnc->sCmn.sRC, psEnc->sCmn.sigtype[ i ], psEnc->sCmn.QuantOffsetType[ i ], 
+            SKP_Silk_encode_pulses( psRangeEnc, psEnc->sCmn.sigtype[ i ], psEnc->sCmn.QuantOffsetType[ i ], 
                 &psEnc->sCmn.q[ i * psEnc->sCmn.frame_length ], psEnc->sCmn.frame_length );
         }
 
         /* Payload length so far */
-        SKP_Silk_range_encoder_get_length( &psEnc->sCmn.sRC, &nBytes );
+        nBytes = SKP_RSHIFT( ec_enc_tell( psRangeEnc, 0 ) + 7, 3 );
 
         /* Check that there is enough space in external output buffer, and move data */
         if( *pnBytesOut >= nBytes ) {
-            SKP_int bits_in_stream, mask;
-            bits_in_stream = ec_enc_tell( &psEnc->sCmn.sRC.range_enc_celt_state, 0 );
-            ec_enc_done( &psEnc->sCmn.sRC.range_enc_celt_state );
+            //SKP_int bits_in_stream, mask;
+            //bits_in_stream = ec_enc_tell( psRangeEnc, 0 );
+            ec_enc_done( psRangeEnc );
             
+#if 0
             /* Fill up any remaining bits in the last byte with 1s */
             if( bits_in_stream & 7 ) {
                 mask = SKP_RSHIFT( 0xFF, bits_in_stream & 7 );
@@ -226,7 +222,9 @@
                 }
             }
             SKP_memcpy( pCode, psEnc->sCmn.sRC.range_enc_celt_state.buf->buf, nBytes * sizeof( SKP_uint8 ) );
+#endif
 
+#if 0
             if( frame_terminator > SKP_SILK_MORE_FRAMES && 
                     *pnBytesOut >= nBytes + psEnc->sCmn.LBRR_buffer[ LBRR_idx ].nBytes ) {
                 /* Get old packet and add to payload. */
@@ -235,8 +233,9 @@
                     psEnc->sCmn.LBRR_buffer[ LBRR_idx ].nBytes * sizeof( SKP_uint8 ) );
                 nBytes += psEnc->sCmn.LBRR_buffer[ LBRR_idx ].nBytes;
             }
+#endif
             *pnBytesOut = nBytes;
-        
+
             /* Update FEC buffer */
             SKP_memcpy( psEnc->sCmn.LBRR_buffer[ psEnc->sCmn.oldest_LBRR_idx ].payload, LBRRpayload, 
                 nBytesLBRR * sizeof( SKP_uint8 ) );
@@ -245,15 +244,15 @@
             psEnc->sCmn.LBRR_buffer[ psEnc->sCmn.oldest_LBRR_idx ].usage = sEncCtrl.sCmn.LBRR_usage;
             psEnc->sCmn.oldest_LBRR_idx = ( ( psEnc->sCmn.oldest_LBRR_idx + 1 ) & LBRR_IDX_MASK );
 
-            /* Reset the number of frames in payload buffer */
-            psEnc->sCmn.nFramesInPayloadBuf = 0;
         } else {
             /* Not enough space: Payload will be discarded */
             *pnBytesOut = 0;
             nBytes      = 0;
-            psEnc->sCmn.nFramesInPayloadBuf = 0;
             ret = SKP_SILK_ENC_PAYLOAD_BUF_TOO_SHORT;
         }
+
+        /* Reset the number of frames in payload buffer */         
+        psEnc->sCmn.nFramesInPayloadBuf = 0;
     } else {
         /* No payload for you this time */
         *pnBytesOut = 0;
@@ -260,10 +259,11 @@
 
         /* Encode that more frames follows */
         frame_terminator = SKP_SILK_MORE_FRAMES;
-        SKP_Silk_range_encoder( &psEnc->sCmn.sRC, frame_terminator, FrameTermination_CDF );
+        ec_encode_bin( psRangeEnc, FrameTermination_CDF[ frame_terminator ], 
+            FrameTermination_CDF[ frame_terminator + 1 ], 16 );
 
         /* Payload length so far */
-        SKP_Silk_range_encoder_get_length( &psEnc->sCmn.sRC, &nBytes );
+        nBytes = SKP_RSHIFT( ec_enc_tell( psRangeEnc, 0 ) + 7, 3 );
 
         /* Take into account the q signal that isn't in the bitstream yet */
         nBytes += SKP_Silk_pulses_to_bytes( &psEnc->sCmn, 
@@ -270,11 +270,6 @@
             &psEnc->sCmn.q[ psEnc->sCmn.nFramesInPayloadBuf * psEnc->sCmn.frame_length ] );
     }
 
-    /* Check for arithmetic coder errors */
-    if( psEnc->sCmn.sRC.error ) {
-        ret = SKP_SILK_ENC_INTERNAL_ERROR;
-    }
-
     /* simulate number of ms buffered in channel because of exceeding TargetRate */
     psEnc->BufferedInChannel_ms   += ( 8.0f * 1000.0f * ( nBytes - psEnc->sCmn.nBytesInPayloadBuf ) ) / psEnc->sCmn.TargetRate_bps;
     psEnc->BufferedInChannel_ms   -= SUB_FRAME_LENGTH_MS * psEnc->sCmn.nb_subfr;
@@ -314,6 +309,7 @@
     return( ret );
 }
 
+#if 0  //tmp
 /* Low Bitrate Redundancy (LBRR) encoding. Reuse all parameters but encode with lower bitrate           */
 void SKP_Silk_LBRR_encode_FLP(
     SKP_Silk_encoder_state_FLP      *psEnc,             /* I/O  Encoder state FLP                       */
@@ -397,7 +393,7 @@
         /****************************************/
         /* Encode Parameters                    */
         /****************************************/
-        SKP_Silk_encode_parameters_v4( &psEnc->sCmn, &psEncCtrl->sCmn, &psEnc->sCmn.sRC_LBRR );
+        SKP_Silk_encode_parameters( &psEnc->sCmn, &psEncCtrl->sCmn, &psEnc->sCmn.sRC_LBRR );
         
         /****************************************/
         /* Encode Parameters                    */
@@ -418,7 +414,8 @@
             frame_terminator = SKP_SILK_LAST_FRAME;
 
             /* Add the frame termination info to stream */
-            SKP_Silk_range_encoder( &psEnc->sCmn.sRC_LBRR, frame_terminator, SKP_Silk_FrameTermination_v4_CDF );
+            ec_encode_bin( psRangeEnc_LBRR, FrameTermination_CDF[ frame_terminator ], 
+                FrameTermination_CDF[ frame_terminator + 1 ], 16 );
 
             /*********************************************/
             /* Encode quantization indices of excitation */
@@ -429,7 +426,7 @@
             }
 
             /* Payload length so far */
-            SKP_Silk_range_encoder_get_length( &psEnc->sCmn.sRC_LBRR, &nBytes );
+            nBytes = SKP_RSHIFT( ec_enc_tell( psRangeEnc_LBRR, 0 ) + 7, 3 );
 
             /* Check that there is enough space in external output buffer and move data */
             if( *pnBytesOut >= nBytes ) {
@@ -458,7 +455,8 @@
 
             /* Encode that more frames follows */
             frame_terminator = SKP_SILK_MORE_FRAMES;
-            SKP_Silk_range_encoder( &psEnc->sCmn.sRC_LBRR, frame_terminator, SKP_Silk_FrameTermination_v4_CDF );
+            ec_encode_bin( psRangeEnc_LBRR, FrameTermination_CDF[ frame_terminator ], 
+                FrameTermination_CDF[ frame_terminator + 1 ], 16 );
         }
 
         /* Restore original Gains */
@@ -470,3 +468,4 @@
         psEnc->sCmn.typeOffsetPrev     = typeOffset;
     }
 }
+#endif
\ No newline at end of file
--- a/src_FLP/SKP_Silk_main_FLP.h
+++ b/src_FLP/SKP_Silk_main_FLP.h
@@ -78,9 +78,9 @@
 /* Encoder main function */
 SKP_int SKP_Silk_encode_frame_FLP( 
     SKP_Silk_encoder_state_FLP      *psEnc,             /* I/O  Encoder state FLP                       */
-          SKP_uint8                 *pCode,             /* O    Payload                                 */
-          SKP_int16                 *pnBytesOut,        /* I/O  Number of payload bytes;                */
+    SKP_int16                       *pnBytesOut,        /* I/O  Number of payload bytes;                */
                                                         /*      input: max length; output: used         */
+    ec_enc                          *psRangeEnc,        /* I/O  compressor data structure                */
     const SKP_int16                 *pIn                /* I    Input speech frame                      */
 );
 
--- a/src_common/SKP_Silk_code_signs.c
+++ b/src_common/SKP_Silk_code_signs.c
@@ -35,27 +35,25 @@
 
 /* Encodes signs of excitation */
 void SKP_Silk_encode_signs(
-    SKP_Silk_range_coder_state      *sRC,               /* I/O  Range coder state                       */
-    const SKP_int8                  q[],                /* I    Pulse signal                            */
-    const SKP_int                   length,             /* I    Length of input                         */
-    const SKP_int                   sigtype,            /* I    Signal type                             */
-    const SKP_int                   QuantOffsetType,    /* I    Quantization offset type                */
-    const SKP_int                   RateLevelIndex      /* I    Rate level index                        */
+    ec_enc                      *psRangeEnc,        /* I/O  Compressor data structure                   */
+    const SKP_int8              q[],                /* I    pulse signal                                */
+    const SKP_int               length,             /* I    length of input                             */
+    const SKP_int               sigtype,            /* I    Signal type                                 */
+    const SKP_int               QuantOffsetType,    /* I    Quantization offset type                    */
+    const SKP_int               RateLevelIndex      /* I    Rate Level Index                            */
 )
 {
     SKP_int i;
     SKP_int inData;
-    SKP_uint16 cdf[ 3 ];
+    SKP_uint16 prob;
 
     i = SKP_SMULBB( N_RATE_LEVELS - 1, SKP_LSHIFT( sigtype, 1 ) + QuantOffsetType ) + RateLevelIndex;
-    cdf[ 0 ] = 0;
-    cdf[ 1 ] = SKP_Silk_sign_CDF[ i ];
-    cdf[ 2 ] = 65535;
+    prob = 65536 - SKP_Silk_sign_CDF[ i ];
     
     for( i = 0; i < length; i++ ) {
         if( q[ i ] != 0 ) {
             inData = SKP_enc_map( q[ i ] ); /* - = 0, + = 1 */
-            SKP_Silk_range_encoder( sRC, inData, cdf );
+            ec_enc_bit_prob( psRangeEnc, inData, prob );
         }
     }
 }
@@ -62,7 +60,7 @@
 
 /* Decodes signs of excitation */
 void SKP_Silk_decode_signs(
-    SKP_Silk_range_coder_state      *sRC,               /* I/O  Range coder state                           */
+    ec_dec                          *psRangeDec,        /* I/O  Compressor data structure                   */
     SKP_int                         q[],                /* I/O  pulse signal                                */
     const SKP_int                   length,             /* I    length of output                            */
     const SKP_int                   sigtype,            /* I    Signal type                                 */
@@ -81,7 +79,7 @@
     
     for( i = 0; i < length; i++ ) {
         if( q[ i ] > 0 ) {
-            SKP_Silk_range_decoder( &data, sRC, cdf, 1 );
+            SKP_Silk_range_decoder( &data, psRangeDec, cdf, 1 );
             /* attach sign */
             /* implementation with shift, subtraction, multiplication */
             q[ i ] *= SKP_dec_map( data );
--- a/src_common/SKP_Silk_dec_SDK_API.c
+++ b/src_common/SKP_Silk_dec_SDK_API.c
@@ -32,10 +32,10 @@
 /* Decode a frame */
 SKP_int SKP_Silk_SDK_Decode(
     void*                               decState,       /* I/O: State                                           */
-    SKP_SILK_SDK_DecControlStruct*      decControl,     /* I/O: Control structure                               */
+    SKP_SILK_SDK_DecControlStruct*      decControl,     /* I/O: Control Structure                               */
     SKP_int                             lostFlag,       /* I:   0: no loss, 1 loss                              */
-    const SKP_uint8                     *inData,        /* I:   Encoded input vector                            */
-    const SKP_int                       nBytesIn,       /* I:   Number of input Bytes                           */
+    ec_dec                              *psRangeDec,    /* I/O  Compressor data structure                       */
+    const SKP_int                       nBytesIn,       /* I:   Number of input bytes                           */
     SKP_int16                           *samplesOut,    /* O:   Decoded output speech vector                    */
     SKP_int16                           *nSamplesOut    /* I/O: Number of samples (vector/decoded)              */
 )
@@ -65,8 +65,7 @@
     prev_fs_kHz = psDec->fs_kHz;
     
     /* Call decoder for one frame */
-    ret += SKP_Silk_decode_frame( psDec, samplesOut, nSamplesOut, inData, nBytesIn, 
-            lostFlag, &used_bytes );
+    ret += SKP_Silk_decode_frame( psDec, psRangeDec, samplesOut, nSamplesOut, nBytesIn, lostFlag, &used_bytes );
     
     if( used_bytes ) { /* Only Call if not a packet loss */
         if( psDec->nBytesLeft > 0 && psDec->FrameTermination == SKP_SILK_MORE_FRAMES && psDec->nFramesDecoded < 5 ) {
@@ -132,6 +131,7 @@
     return ret;
 }
 
+#if 0
 /* Function to find LBRR information in a packet */
 void SKP_Silk_SDK_search_for_LBRR(
     const SKP_uint8                     *inData,        /* I:   Encoded input vector                            */
@@ -155,10 +155,9 @@
     sDec.nFramesDecoded = 0;
     sDec.fs_kHz         = 0; /* Force update parameters LPC_order etc */
     SKP_memset( sDec.prevNLSF_Q15, 0, MAX_LPC_ORDER * sizeof( SKP_int ) );
-    SKP_Silk_range_dec_init( &sDec.sRC, inData, ( SKP_int32 )nBytesIn );
 
     /* Decode all parameter indices for the whole packet*/
-    SKP_Silk_decode_indices_v4( &sDec );
+    SKP_Silk_decode_indices( &sDec, psRangeDec );
 
     /* Is there usable LBRR in this packet */
     *nLBRRBytes = 0;
@@ -165,7 +164,7 @@
     if( ( sDec.FrameTermination - 1 ) & lost_offset && sDec.FrameTermination > 0 && sDec.nBytesLeft >= 0 ) {
         /* The wanted FEC is present in the packet */
         for( i = 0; i < sDec.nFramesInPacket; i++ ) {
-            SKP_Silk_decode_parameters_v4( &sDec, &sDecCtrl, TempQ, 0 );
+            SKP_Silk_decode_parameters( &sDec, &sDecCtrl, psRangeDec, TempQ, 0 );
             
             if( sDec.nBytesLeft <= 0 || sDec.sRC.error ) {
                 /* Corrupt stream */
@@ -184,10 +183,12 @@
         }
     }
 }
+#endif
 
+#if 0  // todo: clean up, make efficient
 /* Getting type of content for a packet */
 void SKP_Silk_SDK_get_TOC(
-    const SKP_uint8                     *inData,        /* I:   Encoded input vector                            */
+    ec_dec                              *psRangeDec,    /* I/O  Compressor data structure                   */
     const SKP_int16                     nBytesIn,       /* I:   Number of input bytes                           */
     SKP_Silk_TOC_struct                 *Silk_TOC       /* O:   Type of content                                 */
 )
@@ -197,10 +198,9 @@
 
     sDec.nFramesDecoded = 0;
     sDec.fs_kHz         = 0; /* Force update parameters LPC_order etc */
-    SKP_Silk_range_dec_init( &sDec.sRC, inData, ( SKP_int32 )nBytesIn );
 
     /* Decode all parameter indices for the whole packet*/
-    SKP_Silk_decode_indices_v4( &sDec );
+    SKP_Silk_decode_indices( &sDec );
     
     if( sDec.nFramesInPacket > SILK_MAX_FRAMES_PER_PACKET || sDec.sRC.error ) {
         /* Corrupt packet */
@@ -222,6 +222,7 @@
         }
     }
 }
+#endif
 
 /**************************/
 /* Get the version number */
--- a/src_common/SKP_Silk_decode_frame.c
+++ b/src_common/SKP_Silk_decode_frame.c
@@ -33,19 +33,18 @@
 /* Decode frame */
 /****************/
 SKP_int SKP_Silk_decode_frame(
-    SKP_Silk_decoder_state          *psDec,             /* I/O  Pointer to Silk decoder state               */
-    SKP_int16                       pOut[],             /* O    Pointer to output speech frame              */
-    SKP_int16                       *pN,                /* O    Pointer to size of output frame             */
-    const SKP_uint8                 pCode[],            /* I    Pointer to payload                          */
-    const SKP_int                   nBytes,             /* I    Payload length                              */
-    SKP_int                         action,             /* I    Action from Jitter Buffer                   */
-    SKP_int                         *decBytes           /* O    Used bytes to decode this frame             */
+    SKP_Silk_decoder_state      *psDec,             /* I/O  Pointer to Silk decoder state               */
+    ec_dec                      *psRangeDec,        /* I/O  Compressor data structure                   */
+    SKP_int16                   pOut[],             /* O    Pointer to output speech frame              */
+    SKP_int16                   *pN,                /* O    Pointer to size of output frame             */
+    const SKP_int               nBytes,             /* I    Payload length                              */
+    SKP_int                     action,             /* I    Action from Jitter Buffer                   */
+    SKP_int                     *decBytes           /* O    Used bytes to decode this frame             */
 )
 {
     SKP_Silk_decoder_control sDecCtrl;
     SKP_int         L, fs_Khz_old, nb_subfr_old, mv_len, ret = 0;
     SKP_int         Pulses[ MAX_FRAME_LENGTH ];
-    ec_byte_buffer range_dec_celt_buf;
 
 TIC(decode_frame)
 
@@ -66,17 +65,7 @@
         fs_Khz_old    = psDec->fs_kHz;
         nb_subfr_old  = psDec->nb_subfr;
         if( psDec->nFramesDecoded == 0 ) {
-            /* Initialize range decoder state */
-            /* check input */
-            psDec->sRC.error = 0;
-            if( nBytes > MAX_ARITHM_BYTES ) {
-                psDec->sRC.error = RANGE_CODER_DEC_PAYLOAD_TOO_LONG;
-            }
-            ec_byte_writeinit_buffer( &range_dec_celt_buf, psDec->sRC.buffer, nBytes );
-            SKP_memcpy( psDec->sRC.buffer, pCode, nBytes * sizeof( SKP_uint8 ) );
-            ec_dec_init( &psDec->sRC.range_dec_celt_state, &range_dec_celt_buf );
-
-            SKP_Silk_decode_indices_v4( psDec );
+            SKP_Silk_decode_indices( psDec, psRangeDec );
         }
 
         /********************************************/
@@ -83,10 +72,10 @@
         /* Decode parameters and pulse signal       */
         /********************************************/
 TIC(decode_params)
-        SKP_Silk_decode_parameters_v4( psDec, &sDecCtrl, Pulses, 1 );
+        SKP_Silk_decode_parameters( psDec, &sDecCtrl, psRangeDec, Pulses, 1 );
 TOC(decode_params)
 
-        if( psDec->sRC.error ) {
+        if( 0 ) { //psDec->sRC.error ) {
             psDec->nBytesLeft = 0;
 
             action              = 1; /* PLC operation */
@@ -93,14 +82,16 @@
             SKP_Silk_decoder_set_fs( psDec, fs_Khz_old, nb_subfr_old );
 
             /* Avoid crashing */
-            *decBytes = psDec->sRC.range_dec_celt_state.buf->storage; 
+            *decBytes = psRangeDec->buf->storage; 
+            /*
             if( psDec->sRC.error == RANGE_CODER_DEC_PAYLOAD_TOO_LONG ) {
                 ret = SKP_SILK_DEC_PAYLOAD_TOO_LARGE;
             } else {
                 ret = SKP_SILK_DEC_PAYLOAD_ERROR;
             }
+            */
         } else {
-            *decBytes = psDec->sRC.range_dec_celt_state.buf->storage - psDec->nBytesLeft;
+            *decBytes = psRangeDec->buf->storage - psDec->nBytesLeft;
             psDec->nFramesDecoded++;
         
             /* Update lengths. Sampling frequency could have changed */
--- /dev/null
+++ b/src_common/SKP_Silk_decode_indices.c
@@ -1,0 +1,195 @@
+/***********************************************************************
+Copyright (c) 2006-2010, Skype Limited. All rights reserved. 
+Redistribution and use in source and binary forms, with or without 
+modification, (subject to the limitations in the disclaimer below) 
+are permitted provided that the following conditions are met:
+- Redistributions of source code must retain the above copyright notice,
+this list of conditions and the following disclaimer.
+- Redistributions in binary form must reproduce the above copyright 
+notice, this list of conditions and the following disclaimer in the 
+documentation and/or other materials provided with the distribution.
+- Neither the name of Skype Limited, nor the names of specific 
+contributors, may be used to endorse or promote products derived from 
+this software without specific prior written permission.
+NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED 
+BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 
+CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
+BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
+FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
+COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
+INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 
+USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 
+ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+***********************************************************************/
+
+#include "SKP_Silk_main.h"
+
+/* Decode indices from payload */
+void SKP_Silk_decode_indices(
+    SKP_Silk_decoder_state      *psDec,             /* I/O  State                                       */
+    ec_dec                      *psRangeDec         /* I/O  Compressor data structure                   */
+)
+{
+    SKP_int   i, k, Ix, fs_kHz_dec, nb_subfr, FrameIndex = 0, FrameTermination;
+    SKP_int   sigtype, QuantOffsetType, seed_int, nBytesUsed;
+    SKP_int   decode_absolute_lagIndex, delta_lagIndex, prev_lagIndex = 0;
+    const SKP_Silk_NLSF_CB_struct *psNLSF_CB = NULL;
+
+    /************************/
+    /* Decode sampling rate */
+    /************************/
+    /* only done for first frame of packet */
+    if( psDec->nFramesDecoded == 0 ) {
+        SKP_Silk_range_decoder( &Ix, psRangeDec, SKP_Silk_SamplingRates_CDF, SKP_Silk_SamplingRates_offset );
+        fs_kHz_dec = SKP_Silk_SamplingRates_table[ Ix ];
+
+        /* Convert number of subframes to index */
+        SKP_Silk_range_decoder( &Ix, psRangeDec, SKP_Silk_NbSubframes_CDF, SKP_Silk_NbSubframes_offset );
+        nb_subfr = (Ix + 1) << 1;
+        SKP_assert( nb_subfr == MAX_NB_SUBFR >> 1 || nb_subfr == MAX_NB_SUBFR );
+        
+        SKP_Silk_decoder_set_fs( psDec, fs_kHz_dec, nb_subfr );
+    
+        FrameIndex       = 0;
+        FrameTermination = SKP_SILK_MORE_FRAMES;
+    }
+
+    while( FrameTermination == SKP_SILK_MORE_FRAMES ) {
+        /*******************/
+        /* Decode VAD flag */
+        /*******************/
+        SKP_Silk_range_decoder( &psDec->vadFlagBuf[ FrameIndex ], psRangeDec, SKP_Silk_vadflag_CDF, SKP_Silk_vadflag_offset );
+
+        /*******************************************/
+        /* Decode signal type and quantizer offset */
+        /*******************************************/
+        if( FrameIndex == 0 ) {
+            /* first frame in packet: independent coding */
+            SKP_Silk_range_decoder( &Ix, psRangeDec, SKP_Silk_type_offset_CDF, SKP_Silk_type_offset_CDF_offset );
+        } else {
+            /* condidtional coding */
+            SKP_Silk_range_decoder( &Ix, psRangeDec, SKP_Silk_type_offset_joint_CDF[ psDec->typeOffsetPrev ], 
+                    SKP_Silk_type_offset_CDF_offset );
+        }
+        sigtype               = SKP_RSHIFT( Ix, 1 );
+        QuantOffsetType       = Ix & 1;
+        psDec->typeOffsetPrev = Ix;
+
+        /****************/
+        /* Decode gains */
+        /****************/
+        /* first subframe */    
+        if( FrameIndex == 0 ) {
+            /* first frame in packet: independent coding */
+            SKP_Silk_range_decoder( &psDec->GainsIndices[ FrameIndex ][ 0 ], psRangeDec, SKP_Silk_gain_CDF[ sigtype ], SKP_Silk_gain_CDF_offset );
+        } else {
+            /* condidtional coding */
+            SKP_Silk_range_decoder( &psDec->GainsIndices[ FrameIndex ][ 0 ], psRangeDec, SKP_Silk_delta_gain_CDF, SKP_Silk_delta_gain_CDF_offset );
+        }
+
+        /* remaining subframes */
+        for( i = 1; i < psDec->nb_subfr; i++ ) {
+            SKP_Silk_range_decoder( &psDec->GainsIndices[ FrameIndex ][ i ], psRangeDec, SKP_Silk_delta_gain_CDF, SKP_Silk_delta_gain_CDF_offset );
+        }
+        
+        /**********************/
+        /* Decode LSF Indices */
+        /**********************/
+
+        /* Set pointer to LSF VQ CB for the current signal type */
+        psNLSF_CB = psDec->psNLSF_CB[ sigtype ];
+
+        /* Arithmetically decode NLSF path */
+        for( i = 0; i < psNLSF_CB->nStages; i++ ) {
+            SKP_Silk_range_decoder( &psDec->NLSFIndices[ FrameIndex ][ i ], psRangeDec, psNLSF_CB->StartPtr[ i ], psNLSF_CB->MiddleIx[ i ] );
+        }
+        
+        /***********************************/
+        /* Decode LSF interpolation factor */
+        /***********************************/
+        SKP_Silk_range_decoder( &psDec->NLSFInterpCoef_Q2[ FrameIndex ], psRangeDec, SKP_Silk_NLSF_interpolation_factor_CDF, 
+            SKP_Silk_NLSF_interpolation_factor_offset );
+        
+        if( sigtype == SIG_TYPE_VOICED ) {
+            /*********************/
+            /* Decode pitch lags */
+            /*********************/
+            /* Get lag index */
+            decode_absolute_lagIndex = 1;
+            if( FrameIndex > 0 && psDec->sigtype[ FrameIndex - 1 ] == SIG_TYPE_VOICED ) {
+                /* Decode Delta index */
+                SKP_Silk_range_decoder( &delta_lagIndex, psRangeDec, SKP_Silk_pitch_delta_CDF,  SKP_Silk_pitch_delta_CDF_offset );
+                if( delta_lagIndex < ( MAX_DELTA_LAG << 1 ) + 1 ) {
+                    delta_lagIndex = delta_lagIndex - MAX_DELTA_LAG;
+                    psDec->lagIndex[ FrameIndex ] = prev_lagIndex + delta_lagIndex;
+                    decode_absolute_lagIndex = 0;
+                }
+            }
+            if( decode_absolute_lagIndex ) {
+                /* Absolute decoding */
+                if( psDec->fs_kHz == 8 ) {
+                    SKP_Silk_range_decoder( &psDec->lagIndex[ FrameIndex ], psRangeDec, SKP_Silk_pitch_lag_NB_CDF,  SKP_Silk_pitch_lag_NB_CDF_offset );
+                } else if( psDec->fs_kHz == 12 ) {
+                    SKP_Silk_range_decoder( &psDec->lagIndex[ FrameIndex ], psRangeDec, SKP_Silk_pitch_lag_MB_CDF,  SKP_Silk_pitch_lag_MB_CDF_offset );
+                } else if( psDec->fs_kHz == 16 ) {
+                    SKP_Silk_range_decoder( &psDec->lagIndex[ FrameIndex ], psRangeDec, SKP_Silk_pitch_lag_WB_CDF,  SKP_Silk_pitch_lag_WB_CDF_offset );
+                } else {
+                    SKP_Silk_range_decoder( &psDec->lagIndex[ FrameIndex ], psRangeDec, SKP_Silk_pitch_lag_SWB_CDF, SKP_Silk_pitch_lag_SWB_CDF_offset );
+                }
+            }
+            prev_lagIndex = psDec->lagIndex[ FrameIndex ];
+
+            /* Get countour index */
+            if( psDec->fs_kHz == 8 ) {
+                /* Less codevectors used in 8 khz mode */
+                SKP_Silk_range_decoder( &psDec->contourIndex[ FrameIndex ], psRangeDec, SKP_Silk_pitch_contour_NB_CDF, SKP_Silk_pitch_contour_NB_CDF_offset );
+            } else {
+                /* Joint for 12, 16, and 24 khz */
+                SKP_Silk_range_decoder( &psDec->contourIndex[ FrameIndex ], psRangeDec, SKP_Silk_pitch_contour_CDF, SKP_Silk_pitch_contour_CDF_offset );
+            }
+            
+            /********************/
+            /* Decode LTP gains */
+            /********************/
+            /* Decode PERIndex value */
+            SKP_Silk_range_decoder( &psDec->PERIndex[ FrameIndex ], psRangeDec, SKP_Silk_LTP_per_index_CDF, SKP_Silk_LTP_per_index_CDF_offset );
+            
+            for( k = 0; k < psDec->nb_subfr; k++ ) {
+                SKP_Silk_range_decoder( &psDec->LTPIndex[ FrameIndex ][ k ], psRangeDec, SKP_Silk_LTP_gain_CDF_ptrs[ psDec->PERIndex[ FrameIndex ] ], 
+                    SKP_Silk_LTP_gain_CDF_offsets[ psDec->PERIndex[ FrameIndex ] ] );
+            }
+
+            /**********************/
+            /* Decode LTP scaling */
+            /**********************/
+            SKP_Silk_range_decoder( &psDec->LTP_scaleIndex[ FrameIndex ], psRangeDec, SKP_Silk_LTPscale_CDF, SKP_Silk_LTPscale_offset );
+        }
+
+        /***************/
+        /* Decode seed */
+        /***************/
+        SKP_Silk_range_decoder( &seed_int, psRangeDec, SKP_Silk_Seed_CDF, SKP_Silk_Seed_offset );
+        psDec->Seed[ FrameIndex ] = ( SKP_int32 )seed_int;
+        /**************************************/
+        /* Decode Frame termination indicator */
+        /**************************************/
+        SKP_Silk_range_decoder( &FrameTermination, psRangeDec, SKP_Silk_FrameTermination_CDF, SKP_Silk_FrameTermination_offset );
+
+        psDec->sigtype[ FrameIndex ]         = sigtype;
+        psDec->QuantOffsetType[ FrameIndex ] = QuantOffsetType;
+
+        FrameIndex++;
+    }
+
+    /****************************************/
+    /* get number of bytes used so far      */
+    /****************************************/
+    nBytesUsed = SKP_RSHIFT( ec_dec_tell( psRangeDec, 0 ) + 7, 3 );
+    psDec->nBytesLeft = psRangeDec->buf->storage - nBytesUsed;
+
+    psDec->nFramesInPacket  = FrameIndex;
+    psDec->FrameTermination = FrameTermination;
+}
--- a/src_common/SKP_Silk_decode_indices_v4.c
+++ /dev/null
@@ -1,202 +1,0 @@
-/***********************************************************************
-Copyright (c) 2006-2010, Skype Limited. All rights reserved. 
-Redistribution and use in source and binary forms, with or without 
-modification, (subject to the limitations in the disclaimer below) 
-are permitted provided that the following conditions are met:
-- Redistributions of source code must retain the above copyright notice,
-this list of conditions and the following disclaimer.
-- Redistributions in binary form must reproduce the above copyright 
-notice, this list of conditions and the following disclaimer in the 
-documentation and/or other materials provided with the distribution.
-- Neither the name of Skype Limited, nor the names of specific 
-contributors, may be used to endorse or promote products derived from 
-this software without specific prior written permission.
-NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED 
-BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 
-CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
-BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
-FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
-COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
-INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
-NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 
-USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 
-ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
-(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
-OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-***********************************************************************/
-
-#include "SKP_Silk_main.h"
-
-/* Decode indices from payload */
-void SKP_Silk_decode_indices_v4(
-    SKP_Silk_decoder_state      *psDec            /* I/O    State                              */
-)
-{
-    SKP_int   i, k, Ix, fs_kHz_dec, nb_subfr, FrameIndex = 0, FrameTermination;
-    SKP_int   sigtype, QuantOffsetType, seed_int, nBytesUsed;
-    SKP_int   decode_absolute_lagIndex, delta_lagIndex, prev_lagIndex = 0;
-    const SKP_Silk_NLSF_CB_struct *psNLSF_CB = NULL;
-    SKP_Silk_range_coder_state  *psRC = &psDec->sRC;
-    /************************/
-    /* Decode sampling rate */
-    /************************/
-    /* only done for first frame of packet */
-    if( psDec->nFramesDecoded == 0 ) {
-        SKP_Silk_range_decoder( &Ix, psRC, SKP_Silk_SamplingRates_CDF, SKP_Silk_SamplingRates_offset );
-
-        /* check that sampling rate is supported */
-        if( Ix < 0 || Ix > 3 ) {
-            psRC->error = RANGE_CODER_ILLEGAL_SAMPLING_RATE;
-            return;
-        }
-        fs_kHz_dec = SKP_Silk_SamplingRates_table[ Ix ];
-
-        /* Convert number of subframes to index */
-        SKP_Silk_range_decoder( &Ix, psRC, SKP_Silk_NbSubframes_CDF, SKP_Silk_NbSubframes_offset );
-        nb_subfr = (Ix + 1) << 1;
-        SKP_assert( nb_subfr == MAX_NB_SUBFR >> 1 || nb_subfr == MAX_NB_SUBFR );
-        
-        SKP_Silk_decoder_set_fs( psDec, fs_kHz_dec, nb_subfr );
-    
-        FrameIndex       = 0;
-        FrameTermination = SKP_SILK_MORE_FRAMES;
-    }
-
-    while( FrameTermination == SKP_SILK_MORE_FRAMES ) {
-        /*******************/
-        /* Decode VAD flag */
-        /*******************/
-        SKP_Silk_range_decoder( &psDec->vadFlagBuf[ FrameIndex ], psRC, SKP_Silk_vadflag_CDF, SKP_Silk_vadflag_offset );
-
-        /*******************************************/
-        /* Decode signal type and quantizer offset */
-        /*******************************************/
-        if( FrameIndex == 0 ) {
-            /* first frame in packet: independent coding */
-            SKP_Silk_range_decoder( &Ix, psRC, SKP_Silk_type_offset_CDF, SKP_Silk_type_offset_CDF_offset );
-        } else {
-            /* condidtional coding */
-            SKP_Silk_range_decoder( &Ix, psRC, SKP_Silk_type_offset_joint_CDF[ psDec->typeOffsetPrev ], 
-                    SKP_Silk_type_offset_CDF_offset );
-        }
-        sigtype               = SKP_RSHIFT( Ix, 1 );
-        QuantOffsetType       = Ix & 1;
-        psDec->typeOffsetPrev = Ix;
-
-        /****************/
-        /* Decode gains */
-        /****************/
-        /* first subframe */    
-        if( FrameIndex == 0 ) {
-            /* first frame in packet: independent coding */
-            SKP_Silk_range_decoder( &psDec->GainsIndices[ FrameIndex ][ 0 ], psRC, SKP_Silk_gain_CDF[ sigtype ], SKP_Silk_gain_CDF_offset );
-        } else {
-            /* condidtional coding */
-            SKP_Silk_range_decoder( &psDec->GainsIndices[ FrameIndex ][ 0 ], psRC, SKP_Silk_delta_gain_CDF, SKP_Silk_delta_gain_CDF_offset );
-        }
-
-        /* remaining subframes */
-        for( i = 1; i < psDec->nb_subfr; i++ ) {
-            SKP_Silk_range_decoder( &psDec->GainsIndices[ FrameIndex ][ i ], psRC, SKP_Silk_delta_gain_CDF, SKP_Silk_delta_gain_CDF_offset );
-        }
-        
-        /**********************/
-        /* Decode LSF Indices */
-        /**********************/
-
-        /* Set pointer to LSF VQ CB for the current signal type */
-        psNLSF_CB = psDec->psNLSF_CB[ sigtype ];
-
-        /* Arithmetically decode NLSF path */
-        SKP_Silk_range_decoder_multi( psDec->NLSFIndices[ FrameIndex ], psRC, psNLSF_CB->StartPtr, psNLSF_CB->MiddleIx, psNLSF_CB->nStages );
-        
-        /***********************************/
-        /* Decode LSF interpolation factor */
-        /***********************************/
-        SKP_Silk_range_decoder( &psDec->NLSFInterpCoef_Q2[ FrameIndex ], psRC, SKP_Silk_NLSF_interpolation_factor_CDF, 
-            SKP_Silk_NLSF_interpolation_factor_offset );
-        
-        if( sigtype == SIG_TYPE_VOICED ) {
-            /*********************/
-            /* Decode pitch lags */
-            /*********************/
-            /* Get lag index */
-            decode_absolute_lagIndex = 1;
-            if( FrameIndex > 0 && psDec->sigtype[ FrameIndex - 1 ] == SIG_TYPE_VOICED ) {
-                /* Decode Delta index */
-                SKP_Silk_range_decoder( &delta_lagIndex,psRC, SKP_Silk_pitch_delta_CDF,  SKP_Silk_pitch_delta_CDF_offset );
-                if( delta_lagIndex < ( MAX_DELTA_LAG << 1 ) + 1 ) {
-                    delta_lagIndex = delta_lagIndex - MAX_DELTA_LAG;
-                    psDec->lagIndex[ FrameIndex ] = prev_lagIndex + delta_lagIndex;
-                    decode_absolute_lagIndex = 0;
-                }
-            }
-            if( decode_absolute_lagIndex ) {
-                /* Absolute decoding */
-                if( psDec->fs_kHz == 8 ) {
-                    SKP_Silk_range_decoder( &psDec->lagIndex[ FrameIndex ], psRC, SKP_Silk_pitch_lag_NB_CDF,  SKP_Silk_pitch_lag_NB_CDF_offset );
-                } else if( psDec->fs_kHz == 12 ) {
-                    SKP_Silk_range_decoder( &psDec->lagIndex[ FrameIndex ], psRC, SKP_Silk_pitch_lag_MB_CDF,  SKP_Silk_pitch_lag_MB_CDF_offset );
-                } else if( psDec->fs_kHz == 16 ) {
-                    SKP_Silk_range_decoder( &psDec->lagIndex[ FrameIndex ], psRC, SKP_Silk_pitch_lag_WB_CDF,  SKP_Silk_pitch_lag_WB_CDF_offset );
-                } else {
-                    SKP_Silk_range_decoder( &psDec->lagIndex[ FrameIndex ], psRC, SKP_Silk_pitch_lag_SWB_CDF, SKP_Silk_pitch_lag_SWB_CDF_offset );
-                }
-            }
-            prev_lagIndex = psDec->lagIndex[ FrameIndex ];
-
-            /* Get countour index */
-            if( psDec->fs_kHz == 8 ) {
-                /* Less codevectors used in 8 khz mode */
-                SKP_Silk_range_decoder( &psDec->contourIndex[ FrameIndex ], psRC, SKP_Silk_pitch_contour_NB_CDF, SKP_Silk_pitch_contour_NB_CDF_offset );
-            } else {
-                /* Joint for 12, 16, and 24 khz */
-                SKP_Silk_range_decoder( &psDec->contourIndex[ FrameIndex ], psRC, SKP_Silk_pitch_contour_CDF, SKP_Silk_pitch_contour_CDF_offset );
-            }
-            
-            /********************/
-            /* Decode LTP gains */
-            /********************/
-            /* Decode PERIndex value */
-            SKP_Silk_range_decoder( &psDec->PERIndex[ FrameIndex ], psRC, SKP_Silk_LTP_per_index_CDF, SKP_Silk_LTP_per_index_CDF_offset );
-            
-            for( k = 0; k < psDec->nb_subfr; k++ ) {
-                SKP_Silk_range_decoder( &psDec->LTPIndex[ FrameIndex ][ k ], psRC, SKP_Silk_LTP_gain_CDF_ptrs[ psDec->PERIndex[ FrameIndex ] ], 
-                    SKP_Silk_LTP_gain_CDF_offsets[ psDec->PERIndex[ FrameIndex ] ] );
-            }
-
-            /**********************/
-            /* Decode LTP scaling */
-            /**********************/
-            SKP_Silk_range_decoder( &psDec->LTP_scaleIndex[ FrameIndex ], psRC, SKP_Silk_LTPscale_CDF, SKP_Silk_LTPscale_offset );
-        }
-
-        /***************/
-        /* Decode seed */
-        /***************/
-        SKP_Silk_range_decoder( &seed_int, psRC, SKP_Silk_Seed_CDF, SKP_Silk_Seed_offset );
-        psDec->Seed[ FrameIndex ] = ( SKP_int32 )seed_int;
-        /**************************************/
-        /* Decode Frame termination indicator */
-        /**************************************/
-        SKP_Silk_range_decoder( &FrameTermination, psRC, SKP_Silk_FrameTermination_v4_CDF, SKP_Silk_FrameTermination_v4_offset );
-
-        psDec->sigtype[ FrameIndex ]         = sigtype;
-        psDec->QuantOffsetType[ FrameIndex ] = QuantOffsetType;
-
-        FrameIndex++;
-    }
-
-    /****************************************/
-    /* get number of bytes used so far      */
-    /****************************************/
-    SKP_Silk_range_decoder_get_length( psRC, &nBytesUsed );
-    psDec->nBytesLeft = psRC->range_dec_celt_state.buf->storage - nBytesUsed;
-
-    if( psDec->nBytesLeft < 0 ) {
-        psRC->error = RANGE_CODER_READ_BEYOND_BUFFER;
-    }
-
-    psDec->nFramesInPacket  = FrameIndex;
-    psDec->FrameTermination = FrameTermination;
-}
--- /dev/null
+++ b/src_common/SKP_Silk_decode_parameters.c
@@ -1,0 +1,151 @@
+/***********************************************************************
+Copyright (c) 2006-2010, Skype Limited. All rights reserved. 
+Redistribution and use in source and binary forms, with or without 
+modification, (subject to the limitations in the disclaimer below) 
+are permitted provided that the following conditions are met:
+- Redistributions of source code must retain the above copyright notice,
+this list of conditions and the following disclaimer.
+- Redistributions in binary form must reproduce the above copyright 
+notice, this list of conditions and the following disclaimer in the 
+documentation and/or other materials provided with the distribution.
+- Neither the name of Skype Limited, nor the names of specific 
+contributors, may be used to endorse or promote products derived from 
+this software without specific prior written permission.
+NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED 
+BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 
+CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
+BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
+FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
+COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
+INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 
+USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 
+ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+***********************************************************************/
+
+#include "SKP_Silk_main.h"
+
+/* Decode parameters from payload */
+void SKP_Silk_decode_parameters(
+    SKP_Silk_decoder_state      *psDec,                                 /* I/O  State                                    */
+    SKP_Silk_decoder_control    *psDecCtrl,                             /* I/O  Decoder control                          */
+    ec_dec                      *psRangeDec,                            /* I/O  Compressor data structure                */
+    SKP_int                     q[ MAX_FRAME_LENGTH ],                  /* O    Excitation signal                        */
+    const SKP_int               fullDecoding                            /* I    Flag to tell if only arithmetic decoding */
+)
+{
+    SKP_int   i, k, Ix, nBytesUsed;
+    SKP_int   pNLSF_Q15[ MAX_LPC_ORDER ], pNLSF0_Q15[ MAX_LPC_ORDER ];
+    const SKP_int16 *cbk_ptr_Q14;
+    const SKP_Silk_NLSF_CB_struct *psNLSF_CB = NULL;
+    
+    psDec->FrameTermination       = SKP_SILK_MORE_FRAMES;
+    psDecCtrl->sigtype            = psDec->sigtype[ psDec->nFramesDecoded ];
+    psDecCtrl->QuantOffsetType    = psDec->QuantOffsetType[ psDec->nFramesDecoded ];
+    psDec->vadFlag                = psDec->vadFlagBuf[ psDec->nFramesDecoded ];
+    psDecCtrl->NLSFInterpCoef_Q2  = psDec->NLSFInterpCoef_Q2[ psDec->nFramesDecoded ];
+    psDecCtrl->Seed               = psDec->Seed[ psDec->nFramesDecoded ];
+
+    /* Dequant Gains */
+    SKP_Silk_gains_dequant( psDecCtrl->Gains_Q16, psDec->GainsIndices[ psDec->nFramesDecoded ], 
+        &psDec->LastGainIndex, psDec->nFramesDecoded, psDec->nb_subfr );
+    /****************/
+    /* Decode NLSFs */
+    /****************/
+
+    /* Set pointer to NLSF VQ CB for the current signal type */
+    psNLSF_CB = psDec->psNLSF_CB[ psDecCtrl->sigtype ];
+
+    /* From the NLSF path, decode an NLSF vector */
+    SKP_Silk_NLSF_MSVQ_decode( pNLSF_Q15, psNLSF_CB, psDec->NLSFIndices[ psDec->nFramesDecoded ], psDec->LPC_order );
+
+    /* Convert NLSF parameters to AR prediction filter coefficients */
+    SKP_Silk_NLSF2A_stable( psDecCtrl->PredCoef_Q12[ 1 ], pNLSF_Q15, psDec->LPC_order );
+    
+    /* If just reset, e.g., because internal Fs changed, do not allow interpolation */
+    /* improves the case of packet loss in the first frame after a switch           */
+    if( psDec->first_frame_after_reset == 1 ) {
+        psDecCtrl->NLSFInterpCoef_Q2 = 4;
+    }
+
+    if( psDecCtrl->NLSFInterpCoef_Q2 < 4 ) {
+        /* Calculation of the interpolated NLSF0 vector from the interpolation factor, */ 
+        /* the previous NLSF1, and the current NLSF1                                   */
+        for( i = 0; i < psDec->LPC_order; i++ ) {
+            pNLSF0_Q15[ i ] = psDec->prevNLSF_Q15[ i ] + SKP_RSHIFT( SKP_MUL( psDecCtrl->NLSFInterpCoef_Q2, 
+                ( pNLSF_Q15[ i ] - psDec->prevNLSF_Q15[ i ] ) ), 2 );
+        }
+
+        /* Convert NLSF parameters to AR prediction filter coefficients */
+        SKP_Silk_NLSF2A_stable( psDecCtrl->PredCoef_Q12[ 0 ], pNLSF0_Q15, psDec->LPC_order );
+    } else {
+        /* Copy LPC coefficients for first half from second half */
+        SKP_memcpy( psDecCtrl->PredCoef_Q12[ 0 ], psDecCtrl->PredCoef_Q12[ 1 ], 
+            psDec->LPC_order * sizeof( SKP_int16 ) );
+    }
+
+    SKP_memcpy( psDec->prevNLSF_Q15, pNLSF_Q15, psDec->LPC_order * sizeof( SKP_int ) );
+
+    /* After a packet loss do BWE of LPC coefs */
+    if( psDec->lossCnt ) {
+        SKP_Silk_bwexpander( psDecCtrl->PredCoef_Q12[ 0 ], psDec->LPC_order, BWE_AFTER_LOSS_Q16 );
+        SKP_Silk_bwexpander( psDecCtrl->PredCoef_Q12[ 1 ], psDec->LPC_order, BWE_AFTER_LOSS_Q16 );
+    }
+
+    if( psDecCtrl->sigtype == SIG_TYPE_VOICED ) {
+        /*********************/
+        /* Decode pitch lags */
+        /*********************/
+        
+        /* Decode pitch values */
+        SKP_Silk_decode_pitch( psDec->lagIndex[ psDec->nFramesDecoded ], psDec->contourIndex[ psDec->nFramesDecoded ], 
+                               psDecCtrl->pitchL, psDec->fs_kHz, psDec->nb_subfr );
+
+        /********************/
+        /* Decode LTP gains */
+        /********************/
+        psDecCtrl->PERIndex = psDec->PERIndex[ psDec->nFramesDecoded ];
+        
+        /* Decode Codebook Index */
+        cbk_ptr_Q14 = SKP_Silk_LTP_vq_ptrs_Q14[ psDecCtrl->PERIndex ]; /* set pointer to start of codebook */
+
+        for( k = 0; k < psDec->nb_subfr; k++ ) {
+            Ix = psDec->LTPIndex[ psDec->nFramesDecoded ][ k ];
+            for( i = 0; i < LTP_ORDER; i++ ) {
+                psDecCtrl->LTPCoef_Q14[ SKP_SMULBB( k, LTP_ORDER ) + i ] = cbk_ptr_Q14[ SKP_SMULBB( Ix, LTP_ORDER ) + i ];
+            }
+        }
+
+        /**********************/
+        /* Decode LTP scaling */
+        /**********************/
+        Ix = psDec->LTP_scaleIndex[ psDec->nFramesDecoded ];
+        psDecCtrl->LTP_scale_Q14 = SKP_Silk_LTPScales_table_Q14[ Ix ];
+    } else {
+        SKP_assert( psDecCtrl->sigtype == SIG_TYPE_UNVOICED );
+        SKP_memset( psDecCtrl->pitchL,      0, psDec->nb_subfr * sizeof( SKP_int ) );
+        SKP_memset( psDecCtrl->LTPCoef_Q14, 0, psDec->nb_subfr * LTP_ORDER * sizeof( SKP_int16 ) );
+        psDecCtrl->PERIndex      = 0;
+        psDecCtrl->LTP_scale_Q14 = 0;
+    }
+
+    /*********************************************/
+    /* Decode quantization indices of excitation */
+    /*********************************************/
+TIC(decode_pulses)
+    SKP_Silk_decode_pulses( psRangeDec, psDecCtrl, q, psDec->frame_length );
+TOC(decode_pulses)
+
+    /****************************************/
+    /* get number of bytes used so far      */
+    /****************************************/
+    nBytesUsed = SKP_RSHIFT( ec_dec_tell( psRangeDec, 0 ) + 7, 3 );
+    psDec->nBytesLeft = psRangeDec->buf->storage - nBytesUsed;
+
+    if( psDec->nFramesInPacket == (psDec->nFramesDecoded + 1)) {
+        /* To indicate the packet has been fully decoded */
+        psDec->FrameTermination = SKP_SILK_LAST_FRAME;
+    }
+}
--- a/src_common/SKP_Silk_decode_parameters_v4.c
+++ /dev/null
@@ -1,162 +1,0 @@
-/***********************************************************************
-Copyright (c) 2006-2010, Skype Limited. All rights reserved. 
-Redistribution and use in source and binary forms, with or without 
-modification, (subject to the limitations in the disclaimer below) 
-are permitted provided that the following conditions are met:
-- Redistributions of source code must retain the above copyright notice,
-this list of conditions and the following disclaimer.
-- Redistributions in binary form must reproduce the above copyright 
-notice, this list of conditions and the following disclaimer in the 
-documentation and/or other materials provided with the distribution.
-- Neither the name of Skype Limited, nor the names of specific 
-contributors, may be used to endorse or promote products derived from 
-this software without specific prior written permission.
-NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED 
-BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 
-CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
-BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
-FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
-COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
-INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
-NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 
-USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 
-ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
-(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
-OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-***********************************************************************/
-
-#include "SKP_Silk_main.h"
-
-/* Decode parameters from payload */
-void SKP_Silk_decode_parameters_v4(
-    SKP_Silk_decoder_state      *psDec,                                 /* I/O  State                                    */
-    SKP_Silk_decoder_control    *psDecCtrl,                             /* I/O  Decoder control                          */
-    SKP_int                     q[ MAX_FRAME_LENGTH ],                  /* O    Excitation signal                        */
-    const SKP_int               fullDecoding                            /* I    Flag to tell if only arithmetic decoding */
-)
-{
-    SKP_int   i, k, Ix, nBytesUsed;
-    SKP_int   pNLSF_Q15[ MAX_LPC_ORDER ], pNLSF0_Q15[ MAX_LPC_ORDER ];
-    const SKP_int16 *cbk_ptr_Q14;
-    const SKP_Silk_NLSF_CB_struct *psNLSF_CB = NULL;
-    SKP_Silk_range_coder_state  *psRC = &psDec->sRC;
-    
-    psDec->FrameTermination       = SKP_SILK_MORE_FRAMES;
-    psDecCtrl->sigtype            = psDec->sigtype[ psDec->nFramesDecoded ];
-    psDecCtrl->QuantOffsetType    = psDec->QuantOffsetType[ psDec->nFramesDecoded ];
-    psDec->vadFlag                = psDec->vadFlagBuf[ psDec->nFramesDecoded ];
-    psDecCtrl->NLSFInterpCoef_Q2  = psDec->NLSFInterpCoef_Q2[ psDec->nFramesDecoded ];
-    psDecCtrl->Seed               = psDec->Seed[ psDec->nFramesDecoded ];
-
-    /* Dequant Gains */
-    SKP_Silk_gains_dequant( psDecCtrl->Gains_Q16, psDec->GainsIndices[ psDec->nFramesDecoded ], 
-        &psDec->LastGainIndex, psDec->nFramesDecoded, psDec->nb_subfr );
-    /****************/
-    /* Decode NLSFs */
-    /****************/
-
-    /* Set pointer to NLSF VQ CB for the current signal type */
-    psNLSF_CB = psDec->psNLSF_CB[ psDecCtrl->sigtype ];
-
-    /* From the NLSF path, decode an NLSF vector */
-    SKP_Silk_NLSF_MSVQ_decode( pNLSF_Q15, psNLSF_CB, psDec->NLSFIndices[ psDec->nFramesDecoded ], psDec->LPC_order );
-
-    /* Convert NLSF parameters to AR prediction filter coefficients */
-    SKP_Silk_NLSF2A_stable( psDecCtrl->PredCoef_Q12[ 1 ], pNLSF_Q15, psDec->LPC_order );
-    
-    /* If just reset, e.g., because internal Fs changed, do not allow interpolation */
-    /* improves the case of packet loss in the first frame after a switch           */
-    if( psDec->first_frame_after_reset == 1 ) {
-        psDecCtrl->NLSFInterpCoef_Q2 = 4;
-    }
-
-    if( psDecCtrl->NLSFInterpCoef_Q2 < 4 ) {
-        /* Calculation of the interpolated NLSF0 vector from the interpolation factor, */ 
-        /* the previous NLSF1, and the current NLSF1                                   */
-        for( i = 0; i < psDec->LPC_order; i++ ) {
-            pNLSF0_Q15[ i ] = psDec->prevNLSF_Q15[ i ] + SKP_RSHIFT( SKP_MUL( psDecCtrl->NLSFInterpCoef_Q2, 
-                ( pNLSF_Q15[ i ] - psDec->prevNLSF_Q15[ i ] ) ), 2 );
-        }
-
-        /* Convert NLSF parameters to AR prediction filter coefficients */
-        SKP_Silk_NLSF2A_stable( psDecCtrl->PredCoef_Q12[ 0 ], pNLSF0_Q15, psDec->LPC_order );
-    } else {
-        /* Copy LPC coefficients for first half from second half */
-        SKP_memcpy( psDecCtrl->PredCoef_Q12[ 0 ], psDecCtrl->PredCoef_Q12[ 1 ], 
-            psDec->LPC_order * sizeof( SKP_int16 ) );
-    }
-
-    SKP_memcpy( psDec->prevNLSF_Q15, pNLSF_Q15, psDec->LPC_order * sizeof( SKP_int ) );
-
-    /* After a packet loss do BWE of LPC coefs */
-    if( psDec->lossCnt ) {
-        SKP_Silk_bwexpander( psDecCtrl->PredCoef_Q12[ 0 ], psDec->LPC_order, BWE_AFTER_LOSS_Q16 );
-        SKP_Silk_bwexpander( psDecCtrl->PredCoef_Q12[ 1 ], psDec->LPC_order, BWE_AFTER_LOSS_Q16 );
-    }
-
-    if( psDecCtrl->sigtype == SIG_TYPE_VOICED ) {
-        /*********************/
-        /* Decode pitch lags */
-        /*********************/
-        
-        /* Decode pitch values */
-        SKP_Silk_decode_pitch( psDec->lagIndex[ psDec->nFramesDecoded ], psDec->contourIndex[ psDec->nFramesDecoded ], 
-                               psDecCtrl->pitchL, psDec->fs_kHz, psDec->nb_subfr );
-
-        /********************/
-        /* Decode LTP gains */
-        /********************/
-        psDecCtrl->PERIndex = psDec->PERIndex[ psDec->nFramesDecoded ];
-        
-        /* Decode Codebook Index */
-        cbk_ptr_Q14 = SKP_Silk_LTP_vq_ptrs_Q14[ psDecCtrl->PERIndex ]; /* set pointer to start of codebook */
-
-        for( k = 0; k < psDec->nb_subfr; k++ ) {
-            Ix = psDec->LTPIndex[ psDec->nFramesDecoded ][ k ];
-            for( i = 0; i < LTP_ORDER; i++ ) {
-                psDecCtrl->LTPCoef_Q14[ SKP_SMULBB( k, LTP_ORDER ) + i ] = cbk_ptr_Q14[ SKP_SMULBB( Ix, LTP_ORDER ) + i ];
-            }
-        }
-
-        /**********************/
-        /* Decode LTP scaling */
-        /**********************/
-        Ix = psDec->LTP_scaleIndex[ psDec->nFramesDecoded ];
-        psDecCtrl->LTP_scale_Q14 = SKP_Silk_LTPScales_table_Q14[ Ix ];
-    } else {
-        SKP_assert( psDecCtrl->sigtype == SIG_TYPE_UNVOICED );
-        SKP_memset( psDecCtrl->pitchL,      0, psDec->nb_subfr * sizeof( SKP_int ) );
-        SKP_memset( psDecCtrl->LTPCoef_Q14, 0, psDec->nb_subfr * LTP_ORDER * sizeof( SKP_int16 ) );
-        psDecCtrl->PERIndex      = 0;
-        psDecCtrl->LTP_scale_Q14 = 0;
-    }
-
-    /*********************************************/
-    /* Decode quantization indices of excitation */
-    /*********************************************/
-TIC(decode_pulses)
-    SKP_Silk_decode_pulses( psRC, psDecCtrl, q, psDec->frame_length );
-TOC(decode_pulses)
-
-    /****************************************/
-    /* get number of bytes used so far      */
-    /****************************************/
-    SKP_Silk_range_decoder_get_length( psRC, &nBytesUsed );
-    psDec->nBytesLeft = psRC->range_dec_celt_state.buf->storage - nBytesUsed;
-
-    if( psDec->nBytesLeft < 0 ) {
-        psRC->error = RANGE_CODER_READ_BEYOND_BUFFER;
-    }
-
-    /****************************************/
-    /* check remaining bits in last byte    */
-    /****************************************/
-    if( psDec->nBytesLeft == 0 ) {
-        SKP_Silk_range_coder_check_after_decoding( psRC );
-    }
-
-    if( psDec->nFramesInPacket == (psDec->nFramesDecoded + 1)) {
-        /* To indicate the packet has been fully decoded */
-        psDec->FrameTermination = SKP_SILK_LAST_FRAME;
-    }
-}
--- a/src_common/SKP_Silk_decode_pulses.c
+++ b/src_common/SKP_Silk_decode_pulses.c
@@ -31,7 +31,7 @@
 /* Decode quantization indices of excitation */
 /*********************************************/
 void SKP_Silk_decode_pulses(
-    SKP_Silk_range_coder_state      *psRC,              /* I/O  Range coder state                           */
+    ec_dec                          *psRangeDec,        /* I/O  Compressor data structure                   */
     SKP_Silk_decoder_control        *psDecCtrl,         /* I/O  Decoder control                             */
     SKP_int                         q[],                /* O    Excitation signal                           */
     const SKP_int                   frame_length        /* I    Frame length (preliminary)                  */
@@ -45,7 +45,7 @@
     /*********************/
     /* Decode rate level */
     /*********************/
-    SKP_Silk_range_decoder( &psDecCtrl->RateLevelIndex, psRC, 
+    SKP_Silk_range_decoder( &psDecCtrl->RateLevelIndex, psRangeDec, 
             SKP_Silk_rate_levels_CDF[ psDecCtrl->sigtype ], SKP_Silk_rate_levels_CDF_offset );
 
     /* Calculate number of shell blocks */
@@ -61,12 +61,12 @@
     cdf_ptr = SKP_Silk_pulses_per_block_CDF[ psDecCtrl->RateLevelIndex ];
     for( i = 0; i < iter; i++ ) {
         nLshifts[ i ] = 0;
-        SKP_Silk_range_decoder( &sum_pulses[ i ], psRC, cdf_ptr, SKP_Silk_pulses_per_block_CDF_offset );
+        SKP_Silk_range_decoder( &sum_pulses[ i ], psRangeDec, cdf_ptr, SKP_Silk_pulses_per_block_CDF_offset );
 
         /* LSB indication */
         while( sum_pulses[ i ] == ( MAX_PULSES + 1 ) ) {
             nLshifts[ i ]++;
-            SKP_Silk_range_decoder( &sum_pulses[ i ], psRC, 
+            SKP_Silk_range_decoder( &sum_pulses[ i ], psRangeDec, 
                     SKP_Silk_pulses_per_block_CDF[ N_RATE_LEVELS - 1 ], SKP_Silk_pulses_per_block_CDF_offset );
         }
     }
@@ -76,7 +76,7 @@
     /***************************************************/
     for( i = 0; i < iter; i++ ) {
         if( sum_pulses[ i ] > 0 ) {
-            SKP_Silk_shell_decoder( &q[ SKP_SMULBB( i, SHELL_CODEC_FRAME_LENGTH ) ], psRC, sum_pulses[ i ] );
+            SKP_Silk_shell_decoder( &q[ SKP_SMULBB( i, SHELL_CODEC_FRAME_LENGTH ) ], psRangeDec, sum_pulses[ i ] );
         } else {
             SKP_memset( &q[ SKP_SMULBB( i, SHELL_CODEC_FRAME_LENGTH ) ], 0, SHELL_CODEC_FRAME_LENGTH * sizeof( SKP_int ) );
         }
@@ -93,7 +93,7 @@
                 abs_q = pulses_ptr[ k ];
                 for( j = 0; j < nLS; j++ ) {
                     abs_q = SKP_LSHIFT( abs_q, 1 ); 
-                    SKP_Silk_range_decoder( &bit, psRC, SKP_Silk_lsb_CDF, 1 );
+                    SKP_Silk_range_decoder( &bit, psRangeDec, SKP_Silk_lsb_CDF, 1 );
                     abs_q += bit;
                 }
                 pulses_ptr[ k ] = abs_q;
@@ -104,6 +104,6 @@
     /****************************************/
     /* Decode and add signs to pulse signal */
     /****************************************/
-    SKP_Silk_decode_signs( psRC, q, frame_length, psDecCtrl->sigtype, 
+    SKP_Silk_decode_signs( psRangeDec, q, frame_length, psDecCtrl->sigtype, 
         psDecCtrl->QuantOffsetType, psDecCtrl->RateLevelIndex);
 }
--- a/src_common/SKP_Silk_enc_API.c
+++ b/src_common/SKP_Silk_enc_API.c
@@ -121,8 +121,8 @@
     const SKP_Silk_EncodeControlStruct  *encControl,    /* I:   Control structure                               */
     const SKP_int16                     *samplesIn,     /* I:   Speech sample input vector                      */
     SKP_int                             nSamplesIn,     /* I:   Number of samples in input vector               */
-    SKP_uint8                           *outData,       /* O:   Encoded output vector                           */
-    SKP_int16                           *nBytesOut      /* I/O: Number of bytes in outData (input: Max bytes)   */
+    ec_enc                              *psRangeEnc,    /* I/O  Compressor data structure                       */
+    SKP_int16                           *nBytesOut      /* I/O: Number of bytes in payload (input: Max bytes)   */
 )
 {
     SKP_int   max_internal_fs_kHz, PacketSize_ms, PacketLoss_perc, UseInBandFEC, UseDTX, ret = SKP_SILK_NO_ERROR;
@@ -214,12 +214,12 @@
             if( MaxBytesOut == 0 ) {
                 /* No payload obtained so far */
                 MaxBytesOut = *nBytesOut;
-                if( ( ret = SKP_Silk_encode_frame_Fxx( psEnc, outData, &MaxBytesOut, psEnc->sCmn.inputBuf ) ) != 0 ) {
+                if( ( ret = SKP_Silk_encode_frame_Fxx( psEnc, &MaxBytesOut, psRangeEnc, psEnc->sCmn.inputBuf ) ) != 0 ) {
                     SKP_assert( 0 );
                 }
             } else {
-                /* outData already contains a payload */
-                if( ( ret = SKP_Silk_encode_frame_Fxx( psEnc, outData, nBytesOut, psEnc->sCmn.inputBuf ) ) != 0 ) {
+                /* Already contains a payload */
+                if( ( ret = SKP_Silk_encode_frame_Fxx( psEnc, nBytesOut, psRangeEnc, psEnc->sCmn.inputBuf ) ) != 0 ) {
                     SKP_assert( 0 );
                 }
                 /* Check that no second payload was created */
--- /dev/null
+++ b/src_common/SKP_Silk_encode_parameters.c
@@ -1,0 +1,252 @@
+/***********************************************************************
+Copyright (c) 2006-2010, Skype Limited. All rights reserved. 
+Redistribution and use in source and binary forms, with or without 
+modification, (subject to the limitations in the disclaimer below) 
+are permitted provided that the following conditions are met:
+- Redistributions of source code must retain the above copyright notice,
+this list of conditions and the following disclaimer.
+- Redistributions in binary form must reproduce the above copyright 
+notice, this list of conditions and the following disclaimer in the 
+documentation and/or other materials provided with the distribution.
+- Neither the name of Skype Limited, nor the names of specific 
+contributors, may be used to endorse or promote products derived from 
+this software without specific prior written permission.
+NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED 
+BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 
+CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
+BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
+FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
+COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
+INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 
+USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 
+ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+***********************************************************************/
+
+#include "SKP_Silk_main.h"
+
+/*******************************************/
+/* Encode parameters to create the payload */
+/*******************************************/
+void SKP_Silk_encode_parameters(
+    SKP_Silk_encoder_state      *psEncC,            /* I/O  Encoder state                               */
+    SKP_Silk_encoder_control    *psEncCtrlC,        /* I/O  Encoder control                             */
+    ec_enc                      *psRangeEnc         /* I/O  Compressor data structure                   */
+)
+{
+    SKP_int   i, k, typeOffset;
+    SKP_int   encode_absolute_lagIndex, delta_lagIndex;
+    const SKP_Silk_NLSF_CB_struct *psNLSF_CB;
+
+#ifdef SAVE_ALL_INTERNAL_DATA
+    SKP_int nBytes_lagIndex, nBytes_contourIndex, nBytes_LTP;
+    SKP_int nBytes_after, nBytes_before;
+#endif
+
+    /*************************************/
+    /* Encode sampling rate and          */
+    /* number of subframes in each frame */
+    /*************************************/
+    /* only done for first frame in packet */
+    if( psEncC->nFramesInPayloadBuf == 0 ) {
+        /* get sampling rate index */
+        for( i = 0; i < 3; i++ ) {
+            if( SKP_Silk_SamplingRates_table[ i ] == psEncC->fs_kHz ) {
+                break;
+            }
+        }
+        ec_encode_bin( psRangeEnc, SKP_Silk_SamplingRates_CDF[ i ], SKP_Silk_SamplingRates_CDF[ i + 1 ], 16 );
+
+        /* Convert number of subframes to index */
+        SKP_assert( psEncC->nb_subfr == MAX_NB_SUBFR >> 1 || psEncC->nb_subfr == MAX_NB_SUBFR );
+        i = (psEncC->nb_subfr >> 1) - 1;
+        ec_enc_bit_prob( psRangeEnc, i, 65536 - SKP_Silk_NbSubframes_CDF[ 1 ] );
+    }
+
+    /*********************************************/
+    /* Encode VAD flag                           */
+    /*********************************************/
+    ec_enc_bit_prob( psRangeEnc, psEncC->vadFlag, 65536 - SKP_Silk_vadflag_CDF[ 1 ] );
+
+    /*******************************************/
+    /* Encode signal type and quantizer offset */
+    /*******************************************/
+    typeOffset = 2 * psEncCtrlC->sigtype + psEncCtrlC->QuantOffsetType;
+    if( psEncC->nFramesInPayloadBuf == 0 ) {
+        /* first frame in packet: independent coding */
+        ec_encode_bin( psRangeEnc, SKP_Silk_type_offset_CDF[ typeOffset ], 
+            SKP_Silk_type_offset_CDF[ typeOffset + 1 ], 16 );
+    } else {
+        /* condidtional coding */
+        ec_encode_bin( psRangeEnc, SKP_Silk_type_offset_joint_CDF[ psEncC->typeOffsetPrev ][ typeOffset ], 
+            SKP_Silk_type_offset_joint_CDF[ psEncC->typeOffsetPrev ][ typeOffset + 1 ], 16 );
+    }
+    psEncC->typeOffsetPrev = typeOffset;
+
+    /****************/
+    /* Encode gains */
+    /****************/
+#ifdef SAVE_ALL_INTERNAL_DATA
+    nBytes_before = SKP_RSHIFT( ec_enc_tell( psRangeEnc, 0 ) + 7, 3 );
+#endif
+    /* first subframe */
+    if( psEncC->nFramesInPayloadBuf == 0 ) {
+        /* first frame in packet: independent coding */
+        ec_encode_bin( psRangeEnc, SKP_Silk_gain_CDF[ psEncCtrlC->sigtype ][ psEncCtrlC->GainsIndices[ 0 ] ], 
+            SKP_Silk_gain_CDF[ psEncCtrlC->sigtype ][ psEncCtrlC->GainsIndices[ 0 ] + 1 ], 16 );
+    } else {
+        /* condidtional coding */
+        ec_encode_bin( psRangeEnc, SKP_Silk_delta_gain_CDF[ psEncCtrlC->GainsIndices[ 0 ] ], 
+            SKP_Silk_delta_gain_CDF[ psEncCtrlC->GainsIndices[ 0 ] + 1 ], 16 );
+    }
+
+    /* remaining subframes */
+    for( i = 1; i < psEncC->nb_subfr; i++ ) {
+        ec_encode_bin( psRangeEnc, SKP_Silk_delta_gain_CDF[ psEncCtrlC->GainsIndices[ i ] ], 
+            SKP_Silk_delta_gain_CDF[ psEncCtrlC->GainsIndices[ i ] + 1 ], 16 );
+    }
+
+#ifdef SAVE_ALL_INTERNAL_DATA
+    nBytes_after = SKP_RSHIFT( ec_enc_tell( psRangeEnc, 0 ) + 7, 3 );
+    nBytes_after -= nBytes_before; // bytes just added
+    DEBUG_STORE_DATA( nBytes_gains.dat, &nBytes_after, sizeof( SKP_int ) );
+#endif
+
+    /****************/
+    /* Encode NLSFs */
+    /****************/
+#ifdef SAVE_ALL_INTERNAL_DATA
+    nBytes_before = SKP_RSHIFT( ec_enc_tell( psRangeEnc, 0 ) + 7, 3 );
+#endif
+    /* Range encoding of the NLSF path */
+    psNLSF_CB = psEncC->psNLSF_CB[ psEncCtrlC->sigtype ];
+    for( i = 0; i < psNLSF_CB->nStages; i++ ) {
+        ec_encode_bin( psRangeEnc, psNLSF_CB->StartPtr[ i ][ psEncCtrlC->NLSFIndices[ i ] ], 
+            psNLSF_CB->StartPtr[ i ][ psEncCtrlC->NLSFIndices[ i ] + 1 ], 16 );
+    }
+
+    /* Encode NLSF interpolation factor */
+    SKP_assert( psEncC->useInterpolatedNLSFs == 1 || psEncCtrlC->NLSFInterpCoef_Q2 == ( 1 << 2 ) );
+    ec_encode_bin( psRangeEnc, SKP_Silk_NLSF_interpolation_factor_CDF[ psEncCtrlC->NLSFInterpCoef_Q2 ], 
+        SKP_Silk_NLSF_interpolation_factor_CDF[ psEncCtrlC->NLSFInterpCoef_Q2 + 1 ], 16 );
+
+#ifdef SAVE_ALL_INTERNAL_DATA
+    nBytes_after = SKP_RSHIFT( ec_enc_tell( psRangeEnc, 0 ) + 7, 3 );
+    nBytes_after -= nBytes_before; // bytes just added
+    DEBUG_STORE_DATA( nBytes_LSF.dat, &nBytes_after, sizeof( SKP_int ) );
+#endif
+
+    if( psEncCtrlC->sigtype == SIG_TYPE_VOICED ) {
+        /*********************/
+        /* Encode pitch lags */
+        /*********************/
+#ifdef SAVE_ALL_INTERNAL_DATA
+        nBytes_before = SKP_RSHIFT( ec_enc_tell( psRangeEnc, 0 ) + 7, 3 );
+#endif
+        /* lag index */
+        encode_absolute_lagIndex = 1;
+        if( psEncC->nFramesInPayloadBuf > 0 && psEncC->prev_sigtype == SIG_TYPE_VOICED ) {
+            /* Delta Encoding */
+            delta_lagIndex = psEncCtrlC->lagIndex - psEncC->prev_lagIndex;
+            if( delta_lagIndex > MAX_DELTA_LAG ) {
+                delta_lagIndex = ( MAX_DELTA_LAG << 1 ) + 1;
+            } else if ( delta_lagIndex < -MAX_DELTA_LAG ) {
+                delta_lagIndex = ( MAX_DELTA_LAG << 1 ) + 1;
+            } else {
+                delta_lagIndex = delta_lagIndex + MAX_DELTA_LAG;
+                encode_absolute_lagIndex = 0; /* Only use delta */
+            }
+            ec_encode_bin( psRangeEnc, SKP_Silk_pitch_delta_CDF[ delta_lagIndex ], 
+                SKP_Silk_pitch_delta_CDF[ delta_lagIndex + 1 ], 16 );
+        }
+        if( encode_absolute_lagIndex ) {
+            /* Absolute encoding */
+            if( psEncC->fs_kHz == 8 ) {
+                ec_encode_bin( psRangeEnc, SKP_Silk_pitch_lag_NB_CDF[ psEncCtrlC->lagIndex ], 
+                    SKP_Silk_pitch_lag_NB_CDF[ psEncCtrlC->lagIndex + 1 ], 16 );
+            } else if( psEncC->fs_kHz == 12 ) {
+                ec_encode_bin( psRangeEnc, SKP_Silk_pitch_lag_MB_CDF[ psEncCtrlC->lagIndex ], 
+                    SKP_Silk_pitch_lag_MB_CDF[ psEncCtrlC->lagIndex + 1 ], 16 );
+            } else if( psEncC->fs_kHz == 16 ) {
+                ec_encode_bin( psRangeEnc, SKP_Silk_pitch_lag_WB_CDF[ psEncCtrlC->lagIndex ], 
+                    SKP_Silk_pitch_lag_WB_CDF[ psEncCtrlC->lagIndex + 1 ], 16 );
+            } else {
+                ec_encode_bin( psRangeEnc, SKP_Silk_pitch_lag_SWB_CDF[ psEncCtrlC->lagIndex ], 
+                    SKP_Silk_pitch_lag_SWB_CDF[ psEncCtrlC->lagIndex + 1 ], 16 );
+            }
+        }
+        psEncC->prev_lagIndex = psEncCtrlC->lagIndex;
+
+#ifdef SAVE_ALL_INTERNAL_DATA
+        nBytes_after = SKP_RSHIFT( ec_enc_tell( psRangeEnc, 0 ) + 7, 3 );
+        nBytes_lagIndex = nBytes_after - nBytes_before; // bytes just added
+#endif
+
+#ifdef SAVE_ALL_INTERNAL_DATA
+        nBytes_before = SKP_RSHIFT( ec_enc_tell( psRangeEnc, 0 ) + 7, 3 );
+#endif
+        /* countour index */
+        if( psEncC->fs_kHz == 8 ) {
+            /* Less codevectors used in 8 khz mode */
+            ec_encode_bin( psRangeEnc, SKP_Silk_pitch_contour_NB_CDF[ psEncCtrlC->contourIndex ], 
+                SKP_Silk_pitch_contour_NB_CDF[ psEncCtrlC->contourIndex + 1 ], 16 );
+        } else {
+            /* Joint for 12, 16, 24 khz */
+            ec_encode_bin( psRangeEnc, SKP_Silk_pitch_contour_CDF[ psEncCtrlC->contourIndex ], 
+                SKP_Silk_pitch_contour_CDF[ psEncCtrlC->contourIndex + 1 ], 16 );
+        }
+#ifdef SAVE_ALL_INTERNAL_DATA
+        nBytes_after = SKP_RSHIFT( ec_enc_tell( psRangeEnc, 0 ) + 7, 3 );
+        nBytes_contourIndex = nBytes_after - nBytes_before; // bytes just added
+#endif
+
+        /********************/
+        /* Encode LTP gains */
+        /********************/
+#ifdef SAVE_ALL_INTERNAL_DATA
+        nBytes_before = SKP_RSHIFT( ec_enc_tell( psRangeEnc, 0 ) + 7, 3 );
+#endif
+        /* PERIndex value */
+        ec_encode_bin( psRangeEnc, SKP_Silk_LTP_per_index_CDF[ psEncCtrlC->PERIndex ], 
+            SKP_Silk_LTP_per_index_CDF[ psEncCtrlC->PERIndex + 1 ], 16 );
+
+        /* Codebook Indices */
+        for( k = 0; k < psEncC->nb_subfr; k++ ) {
+            ec_encode_bin( psRangeEnc, SKP_Silk_LTP_gain_CDF_ptrs[ psEncCtrlC->PERIndex ][ psEncCtrlC->LTPIndex[ k ] ], 
+                SKP_Silk_LTP_gain_CDF_ptrs[ psEncCtrlC->PERIndex ][ psEncCtrlC->LTPIndex[ k ] + 1 ], 16 );
+        }
+
+        /**********************/
+        /* Encode LTP scaling */
+        /**********************/
+        ec_encode_bin( psRangeEnc, SKP_Silk_LTPscale_CDF[ psEncCtrlC->LTP_scaleIndex ], 
+            SKP_Silk_LTPscale_CDF[ psEncCtrlC->LTP_scaleIndex + 1 ], 16 );
+#ifdef SAVE_ALL_INTERNAL_DATA
+        nBytes_after = SKP_RSHIFT( ec_enc_tell( psRangeEnc, 0 ) + 7, 3 );
+        nBytes_LTP = nBytes_after - nBytes_before; // bytes just added
+#endif
+    }
+#ifdef SAVE_ALL_INTERNAL_DATA
+    else { 
+        // Unvoiced speech
+        nBytes_lagIndex     = 0;
+        nBytes_contourIndex = 0;
+        nBytes_LTP          = 0;
+    }
+    DEBUG_STORE_DATA( nBytes_lagIndex.dat,      &nBytes_lagIndex,       sizeof( SKP_int ) );
+    DEBUG_STORE_DATA( nBytes_contourIndex.dat,  &nBytes_contourIndex,   sizeof( SKP_int ) );
+    DEBUG_STORE_DATA( nBytes_LTP.dat,           &nBytes_LTP,            sizeof( SKP_int ) );
+#endif
+
+#ifdef SAVE_ALL_INTERNAL_DATA
+    nBytes_before = SKP_RSHIFT( ec_enc_tell( psRangeEnc, 0 ) + 7, 3 );
+#endif
+
+    /***************/
+    /* Encode seed */
+    /***************/
+    ec_encode_bin( psRangeEnc, SKP_Silk_Seed_CDF[ psEncCtrlC->Seed ], 
+        SKP_Silk_Seed_CDF[ psEncCtrlC->Seed + 1 ], 16 );
+}
--- a/src_common/SKP_Silk_encode_parameters_v4.c
+++ /dev/null
@@ -1,232 +1,0 @@
-/***********************************************************************
-Copyright (c) 2006-2010, Skype Limited. All rights reserved. 
-Redistribution and use in source and binary forms, with or without 
-modification, (subject to the limitations in the disclaimer below) 
-are permitted provided that the following conditions are met:
-- Redistributions of source code must retain the above copyright notice,
-this list of conditions and the following disclaimer.
-- Redistributions in binary form must reproduce the above copyright 
-notice, this list of conditions and the following disclaimer in the 
-documentation and/or other materials provided with the distribution.
-- Neither the name of Skype Limited, nor the names of specific 
-contributors, may be used to endorse or promote products derived from 
-this software without specific prior written permission.
-NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED 
-BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 
-CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
-BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
-FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
-COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
-INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
-NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 
-USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 
-ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
-(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
-OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-***********************************************************************/
-
-#include "SKP_Silk_main.h"
-
-/*******************************************/
-/* Encode parameters to create the payload */
-/*******************************************/
-void SKP_Silk_encode_parameters_v4(
-    SKP_Silk_encoder_state          *psEncC,        /* I/O  Encoder state                   */
-    SKP_Silk_encoder_control        *psEncCtrlC,    /* I/O  Encoder control                 */
-    SKP_Silk_range_coder_state      *psRC           /* I/O  Range encoder state             */
-)
-{
-    SKP_int   i, k, typeOffset;
-    SKP_int   encode_absolute_lagIndex, delta_lagIndex;
-    const SKP_Silk_NLSF_CB_struct *psNLSF_CB;
-
-#ifdef SAVE_ALL_INTERNAL_DATA
-    SKP_int nBytes_lagIndex, nBytes_contourIndex, nBytes_LTP;
-    SKP_int nBytes_after, nBytes_before;
-#endif
-
-    /*************************************/
-    /* Encode sampling rate and          */
-    /* number of subframes in each frame */
-    /*************************************/
-    /* only done for first frame in packet */
-    if( psEncC->nFramesInPayloadBuf == 0 ) {
-        /* get sampling rate index */
-        for( i = 0; i < 3; i++ ) {
-            if( SKP_Silk_SamplingRates_table[ i ] == psEncC->fs_kHz ) {
-                break;
-            }
-        }
-        SKP_Silk_range_encoder( psRC, i, SKP_Silk_SamplingRates_CDF );
-
-        /* Convert number of subframes to index */
-        SKP_assert( psEncC->nb_subfr == MAX_NB_SUBFR >> 1 || psEncC->nb_subfr == MAX_NB_SUBFR );
-        i = (psEncC->nb_subfr >> 1) - 1;
-        SKP_Silk_range_encoder( psRC, i, SKP_Silk_NbSubframes_CDF );
-    }
-
-    /*********************************************/
-    /* Encode VAD flag                           */
-    /*********************************************/
-    SKP_Silk_range_encoder( psRC, psEncC->vadFlag, SKP_Silk_vadflag_CDF );
-
-    /*******************************************/
-    /* Encode signal type and quantizer offset */
-    /*******************************************/
-    typeOffset = 2 * psEncCtrlC->sigtype + psEncCtrlC->QuantOffsetType;
-    if( psEncC->nFramesInPayloadBuf == 0 ) {
-        /* first frame in packet: independent coding */
-        SKP_Silk_range_encoder( psRC, typeOffset, SKP_Silk_type_offset_CDF );
-    } else {
-        /* condidtional coding */
-        SKP_Silk_range_encoder( psRC, typeOffset, SKP_Silk_type_offset_joint_CDF[ psEncC->typeOffsetPrev ] );
-    }
-    psEncC->typeOffsetPrev = typeOffset;
-
-    /****************/
-    /* Encode gains */
-    /****************/
-#ifdef SAVE_ALL_INTERNAL_DATA
-    SKP_Silk_range_encoder_get_length( psRC, &nBytes_before );
-#endif
-    /* first subframe */
-    if( psEncC->nFramesInPayloadBuf == 0 ) {
-        /* first frame in packet: independent coding */
-        SKP_Silk_range_encoder( psRC, psEncCtrlC->GainsIndices[ 0 ], SKP_Silk_gain_CDF[ psEncCtrlC->sigtype ] );
-    } else {
-        /* condidtional coding */
-        SKP_Silk_range_encoder( psRC, psEncCtrlC->GainsIndices[ 0 ], SKP_Silk_delta_gain_CDF );
-    }
-
-    /* remaining subframes */
-    for( i = 1; i < psEncC->nb_subfr; i++ ) {
-        SKP_Silk_range_encoder( psRC, psEncCtrlC->GainsIndices[ i ], SKP_Silk_delta_gain_CDF );
-    }
-
-#ifdef SAVE_ALL_INTERNAL_DATA
-    SKP_Silk_range_encoder_get_length( psRC, &nBytes_after );
-    nBytes_after -= nBytes_before; // bytes just added
-    DEBUG_STORE_DATA( nBytes_gains.dat, &nBytes_after, sizeof( SKP_int ) );
-#endif
-
-    /****************/
-    /* Encode NLSFs */
-    /****************/
-#ifdef SAVE_ALL_INTERNAL_DATA
-    SKP_Silk_range_encoder_get_length( psRC, &nBytes_before );
-#endif
-    /* Range encoding of the NLSF path */
-    psNLSF_CB = psEncC->psNLSF_CB[ psEncCtrlC->sigtype ];
-    SKP_Silk_range_encoder_multi( psRC, psEncCtrlC->NLSFIndices, psNLSF_CB->StartPtr, psNLSF_CB->nStages );
-
-    /* Encode NLSF interpolation factor */
-    SKP_assert( psEncC->useInterpolatedNLSFs == 1 || psEncCtrlC->NLSFInterpCoef_Q2 == ( 1 << 2 ) );
-    SKP_Silk_range_encoder( psRC, psEncCtrlC->NLSFInterpCoef_Q2, SKP_Silk_NLSF_interpolation_factor_CDF );
-
-#ifdef SAVE_ALL_INTERNAL_DATA
-    SKP_Silk_range_encoder_get_length( psRC, &nBytes_after );
-    nBytes_after -= nBytes_before; // bytes just added
-    DEBUG_STORE_DATA( nBytes_LSF.dat, &nBytes_after, sizeof( SKP_int ) );
-#endif
-
-    if( psEncCtrlC->sigtype == SIG_TYPE_VOICED ) {
-        /*********************/
-        /* Encode pitch lags */
-        /*********************/
-#ifdef SAVE_ALL_INTERNAL_DATA
-        SKP_Silk_range_encoder_get_length( psRC, &nBytes_before );
-#endif
-        /* lag index */
-        encode_absolute_lagIndex = 1;
-        if( psEncC->nFramesInPayloadBuf > 0 && psEncC->prev_sigtype == SIG_TYPE_VOICED ) {
-            /* Delta Encoding */
-            delta_lagIndex = psEncCtrlC->lagIndex - psEncC->prev_lagIndex;
-            if( delta_lagIndex > MAX_DELTA_LAG ) {
-                delta_lagIndex = ( MAX_DELTA_LAG << 1 ) + 1;
-            } else if ( delta_lagIndex < -MAX_DELTA_LAG ) {
-                delta_lagIndex = ( MAX_DELTA_LAG << 1 ) + 1;
-            } else {
-                delta_lagIndex = delta_lagIndex + MAX_DELTA_LAG;
-                encode_absolute_lagIndex = 0; /* Only use delta */
-            }
-            SKP_Silk_range_encoder( psRC, delta_lagIndex, SKP_Silk_pitch_delta_CDF );
-        }
-        if( encode_absolute_lagIndex ) {
-            /* Absolute encoding */
-            if( psEncC->fs_kHz == 8 ) {
-                SKP_Silk_range_encoder( psRC, psEncCtrlC->lagIndex, SKP_Silk_pitch_lag_NB_CDF );
-            } else if( psEncC->fs_kHz == 12 ) {
-                SKP_Silk_range_encoder( psRC, psEncCtrlC->lagIndex, SKP_Silk_pitch_lag_MB_CDF );
-            } else if( psEncC->fs_kHz == 16 ) {
-                SKP_Silk_range_encoder( psRC, psEncCtrlC->lagIndex, SKP_Silk_pitch_lag_WB_CDF );
-            } else {
-                SKP_Silk_range_encoder( psRC, psEncCtrlC->lagIndex, SKP_Silk_pitch_lag_SWB_CDF );
-            }
-        }
-        psEncC->prev_lagIndex = psEncCtrlC->lagIndex;
-
-#ifdef SAVE_ALL_INTERNAL_DATA
-        SKP_Silk_range_encoder_get_length( psRC, &nBytes_after );
-        nBytes_lagIndex = nBytes_after - nBytes_before; // bytes just added
-#endif
-
-#ifdef SAVE_ALL_INTERNAL_DATA
-        SKP_Silk_range_encoder_get_length( psRC, &nBytes_before );
-#endif
-        /* countour index */
-        if( psEncC->fs_kHz == 8 ) {
-            /* Less codevectors used in 8 khz mode */
-            SKP_Silk_range_encoder( psRC, psEncCtrlC->contourIndex, SKP_Silk_pitch_contour_NB_CDF );
-        } else {
-            /* Joint for 12, 16, 24 khz */
-            SKP_Silk_range_encoder( psRC, psEncCtrlC->contourIndex, SKP_Silk_pitch_contour_CDF );
-        }
-#ifdef SAVE_ALL_INTERNAL_DATA
-        SKP_Silk_range_encoder_get_length( psRC, &nBytes_after ); 
-        nBytes_contourIndex = nBytes_after - nBytes_before; // bytes just added
-#endif
-
-        /********************/
-        /* Encode LTP gains */
-        /********************/
-#ifdef SAVE_ALL_INTERNAL_DATA
-        SKP_Silk_range_encoder_get_length( psRC, &nBytes_before );
-#endif
-        /* PERIndex value */
-        SKP_Silk_range_encoder( psRC, psEncCtrlC->PERIndex, SKP_Silk_LTP_per_index_CDF );
-
-        /* Codebook Indices */
-        for( k = 0; k < psEncC->nb_subfr; k++ ) {
-            SKP_Silk_range_encoder( psRC, psEncCtrlC->LTPIndex[ k ], SKP_Silk_LTP_gain_CDF_ptrs[ psEncCtrlC->PERIndex ] );
-        }
-
-        /**********************/
-        /* Encode LTP scaling */
-        /**********************/
-        SKP_Silk_range_encoder( psRC, psEncCtrlC->LTP_scaleIndex, SKP_Silk_LTPscale_CDF );
-#ifdef SAVE_ALL_INTERNAL_DATA
-        SKP_Silk_range_encoder_get_length( psRC, &nBytes_after );
-        nBytes_LTP = nBytes_after - nBytes_before; // bytes just added
-#endif
-    }
-#ifdef SAVE_ALL_INTERNAL_DATA
-    else { 
-        // Unvoiced speech
-        nBytes_lagIndex     = 0;
-        nBytes_contourIndex = 0;
-        nBytes_LTP          = 0;
-    }
-    DEBUG_STORE_DATA( nBytes_lagIndex.dat,      &nBytes_lagIndex,       sizeof( SKP_int ) );
-    DEBUG_STORE_DATA( nBytes_contourIndex.dat,  &nBytes_contourIndex,   sizeof( SKP_int ) );
-    DEBUG_STORE_DATA( nBytes_LTP.dat,           &nBytes_LTP,            sizeof( SKP_int ) );
-#endif
-
-#ifdef SAVE_ALL_INTERNAL_DATA
-    SKP_Silk_range_encoder_get_length( psRC, &nBytes_before );
-#endif
-
-    /***************/
-    /* Encode seed */
-    /***************/
-    SKP_Silk_range_encoder( psRC, psEncCtrlC->Seed, SKP_Silk_Seed_CDF );
-}
--- a/src_common/SKP_Silk_encode_pulses.c
+++ b/src_common/SKP_Silk_encode_pulses.c
@@ -71,11 +71,11 @@
 
 /* Encode quantization indices of excitation */
 void SKP_Silk_encode_pulses(
-    SKP_Silk_range_coder_state      *psRC,          /* I/O  Range coder state               */
-    const SKP_int                   sigtype,        /* I    Sigtype                         */
-    const SKP_int                   QuantOffsetType,/* I    QuantOffsetType                 */
-    SKP_int8                        q[],            /* I    quantization indices            */
-    const SKP_int                   frame_length    /* I    Frame length                    */
+    ec_enc                      *psRangeEnc,        /* I/O  compressor data structure                   */
+    const SKP_int               sigtype,            /* I    Sigtype                                     */
+    const SKP_int               QuantOffsetType,    /* I    QuantOffsetType                             */
+    SKP_int8                    q[],                /* I    quantization indices                        */
+    const SKP_int               frame_length        /* I    Frame length                                */
 )
 {
     SKP_int   i, k, j, iter, bit, nLS, scale_down, RateLevelIndex = 0;
@@ -167,7 +167,8 @@
             RateLevelIndex = k;
         }
     }
-    SKP_Silk_range_encoder( psRC, RateLevelIndex, SKP_Silk_rate_levels_CDF[ sigtype ] );
+    ec_encode_bin( psRangeEnc, SKP_Silk_rate_levels_CDF[ sigtype ][ RateLevelIndex ], 
+        SKP_Silk_rate_levels_CDF[ sigtype ][ RateLevelIndex + 1 ], 16 );
 
     /***************************************************/
     /* Sum-Weighted-Pulses Encoding                    */
@@ -175,13 +176,15 @@
     cdf_ptr = SKP_Silk_pulses_per_block_CDF[ RateLevelIndex ];
     for( i = 0; i < iter; i++ ) {
         if( nRshifts[ i ] == 0 ) {
-            SKP_Silk_range_encoder( psRC, sum_pulses[ i ], cdf_ptr );
+            ec_encode_bin( psRangeEnc, cdf_ptr[ sum_pulses[ i ] ], cdf_ptr[ sum_pulses[ i ] + 1 ], 16 );
         } else {
-            SKP_Silk_range_encoder( psRC, MAX_PULSES + 1, cdf_ptr );
+            ec_encode_bin( psRangeEnc, cdf_ptr[ MAX_PULSES + 1 ], cdf_ptr[ MAX_PULSES + 2 ], 16 );
             for( k = 0; k < nRshifts[ i ] - 1; k++ ) {
-                SKP_Silk_range_encoder( psRC, MAX_PULSES + 1, SKP_Silk_pulses_per_block_CDF[ N_RATE_LEVELS - 1 ] );
+                ec_encode_bin( psRangeEnc, SKP_Silk_pulses_per_block_CDF[ N_RATE_LEVELS - 1 ][ sum_pulses[ i ] ], 
+                    SKP_Silk_pulses_per_block_CDF[ N_RATE_LEVELS - 1 ][ sum_pulses[ i ] + 1 ], 16 );
             }
-            SKP_Silk_range_encoder( psRC, sum_pulses[ i ], SKP_Silk_pulses_per_block_CDF[ N_RATE_LEVELS - 1 ] );
+            ec_encode_bin( psRangeEnc, SKP_Silk_pulses_per_block_CDF[ N_RATE_LEVELS - 1 ][ sum_pulses[ i ] ], 
+                SKP_Silk_pulses_per_block_CDF[ N_RATE_LEVELS - 1 ][ sum_pulses[ i ] + 1 ], 16 );
         }
     }
 
@@ -190,7 +193,7 @@
     /******************/
     for( i = 0; i < iter; i++ ) {
         if( sum_pulses[ i ] > 0 ) {
-            SKP_Silk_shell_encoder( psRC, &abs_pulses[ i * SHELL_CODEC_FRAME_LENGTH ] );
+            SKP_Silk_shell_encoder( psRangeEnc, &abs_pulses[ i * SHELL_CODEC_FRAME_LENGTH ] );
         }
     }
 
@@ -205,10 +208,10 @@
                 abs_q = (SKP_int8)SKP_abs( pulses_ptr[ k ] );
                 for( j = nLS; j > 0; j-- ) {
                     bit = SKP_RSHIFT( abs_q, j ) & 1;
-                    SKP_Silk_range_encoder( psRC, bit, SKP_Silk_lsb_CDF );
+                    ec_enc_bit_prob( psRangeEnc, bit, 65536 - SKP_Silk_lsb_CDF[ 1 ] );
                 }
                 bit = abs_q & 1;
-                SKP_Silk_range_encoder( psRC, bit, SKP_Silk_lsb_CDF );
+                ec_enc_bit_prob( psRangeEnc, bit, 65536 - SKP_Silk_lsb_CDF[ 1 ] );
             }
         }
     }
@@ -217,6 +220,6 @@
     /****************/
     /* Encode signs */
     /****************/
-    SKP_Silk_encode_signs( psRC, q, frame_length, sigtype, QuantOffsetType, RateLevelIndex );
+    SKP_Silk_encode_signs( psRangeEnc, q, frame_length, sigtype, QuantOffsetType, RateLevelIndex );
 #endif
 }
--- a/src_common/SKP_Silk_main.h
+++ b/src_common/SKP_Silk_main.h
@@ -44,7 +44,7 @@
 
 /* Encodes signs of excitation */
 void SKP_Silk_encode_signs(
-    SKP_Silk_range_coder_state  *psRC,              /* I/O  Range coder state                           */
+    ec_enc                      *psRangeEnc,        /* I/O  Compressor data structure                   */
     const SKP_int8              q[],                /* I    pulse signal                                */
     const SKP_int               length,             /* I    length of input                             */
     const SKP_int               sigtype,            /* I    Signal type                                 */
@@ -54,7 +54,7 @@
 
 /* Decodes signs of excitation */
 void SKP_Silk_decode_signs(
-    SKP_Silk_range_coder_state  *psRC,              /* I/O  Range coder state                           */
+    ec_dec                      *psRangeDec,        /* I/O  Compressor data structure                   */
     SKP_int                     q[],                /* I/O  pulse signal                                */
     const SKP_int               length,             /* I    length of output                            */
     const SKP_int               sigtype,            /* I    Signal type                                 */
@@ -68,7 +68,7 @@
 
 /* Encode quantization indices of excitation */
 void SKP_Silk_encode_pulses(
-    SKP_Silk_range_coder_state  *psRC,              /* I/O  Range coder state                           */
+    ec_enc                      *psRangeEnc,        /* I/O  compressor data structure                   */
     const SKP_int               sigtype,            /* I    Sigtype                                     */
     const SKP_int               QuantOffsetType,    /* I    QuantOffsetType                             */
     SKP_int8                    q[],                /* I    quantization indices                        */
@@ -77,7 +77,7 @@
 
 /* Shell encoder, operates on one shell code frame of 16 pulses */
 void SKP_Silk_shell_encoder(
-    SKP_Silk_range_coder_state  *psRC,              /* I/O  compressor data structure                   */
+    ec_enc                      *psRangeEnc,        /* I/O  compressor data structure                   */
     const SKP_int               *pulses0            /* I    data: nonnegative pulse amplitudes          */
 );
 
@@ -84,7 +84,7 @@
 /* Shell decoder, operates on one shell code frame of 16 pulses */
 void SKP_Silk_shell_decoder(
     SKP_int                     *pulses0,           /* O    data: nonnegative pulse amplitudes          */
-    SKP_Silk_range_coder_state  *psRC,              /* I/O  compressor data structure                   */
+    ec_dec                      *psRangeDec,        /* I/O  Compressor data structure                   */
     const SKP_int               pulses4             /* I    number of pulses per pulse-subframe         */
 );
 
@@ -91,81 +91,14 @@
 /***************/
 /* Range coder */
 /***************/
-/* Range encoder for one symbol */
-void SKP_Silk_range_encoder(
-    SKP_Silk_range_coder_state  *psRC,              /* I/O  compressor data structure                   */
-    const SKP_int               data,               /* I    uncompressed data                           */
-    const SKP_uint16            prob[]              /* I    cumulative density functions                */
-);
-    
-/* Range encoder for one symbol, with uniform PDF*/
-void SKP_Silk_range_encode_uniform(
-    SKP_Silk_range_coder_state      *psRC,              /* I/O  compressor data structure                   */
-    const SKP_int                   data,               /* I    uncompressed data                           */
-    const SKP_int                   N                   /* I    number of possible outcomes                 */
-);
-
-/* Range encoder for multiple symbols */
-void SKP_Silk_range_encoder_multi(
-    SKP_Silk_range_coder_state  *psRC,              /* I/O  compressor data structure                   */
-    const SKP_int               data[],             /* I    uncompressed data    [nSymbols]             */
-    const SKP_uint16 * const    prob[],             /* I    cumulative density functions                */
-    const SKP_int               nSymbols            /* I    number of data symbols                      */
-);
-
 /* Range decoder for one symbol */
 void SKP_Silk_range_decoder(
     SKP_int                     data[],             /* O    uncompressed data                           */
-    SKP_Silk_range_coder_state  *psRC,              /* I/O  compressor data structure                   */
+    ec_dec                      *psRangeDec,        /* I/O  Compressor data structure                   */
     const SKP_uint16            prob[],             /* I    cumulative density function                 */
     SKP_int                     probIx              /* I    initial (middle) entry of cdf               */
 );
 
-/* Range decoder for one symbol, with uniform PDF*/
-void SKP_Silk_range_decode_uniform(
-    SKP_int                         data[],             /* O    uncompressed data                           */
-    SKP_Silk_range_coder_state      *psRC,              /* I/O  compressor data structure                   */
-    const SKP_int                   N                   /* I    number of possible outcomes                 */
-);
-
-/* Range decoder for multiple symbols */
-void SKP_Silk_range_decoder_multi(
-    SKP_int                     data[],             /* O    uncompressed data                [nSymbols] */
-    SKP_Silk_range_coder_state  *psRC,              /* I/O  compressor data structure                   */
-    const SKP_uint16 * const    prob[],             /* I    cumulative density functions                */
-    const SKP_int               probStartIx[],      /* I    initial (middle) entries of cdfs [nSymbols] */
-    const SKP_int               nSymbols            /* I    number of data symbols                      */
-);
-
-/* Initialize range coder structure for encoder */
-void SKP_Silk_range_enc_init(
-    SKP_Silk_range_coder_state  *psRC               /* O    compressor data structure                   */
-);
-
-/* Initialize range coder structure for decoder */
-void SKP_Silk_range_dec_init(
-    SKP_Silk_range_coder_state  *psRC,              /* O    compressor data structure                   */
-    const SKP_uint8             buffer[],           /* I    buffer for compressed data [bufferLength]   */
-    const SKP_int32             bufferLength        /* I    buffer length (in bytes)                    */
-);
-
-/* Determine length of bitstream */
-SKP_int SKP_Silk_range_encoder_get_length(          /* O    returns number of BITS in stream            */
-    SKP_Silk_range_coder_state          *psRC,      /* I    compressed data structure                   */
-    SKP_int                             *nBytes     /* O    number of BYTES in stream                   */
-);
-
-/* Determine length of bitstream */
-SKP_int SKP_Silk_range_decoder_get_length(          /* O    returns number of BITS in stream            */
-    SKP_Silk_range_coder_state          *psRC,      /* I    compressed data structure                   */
-    SKP_int                             *nBytes     /* O    number of BYTES in stream                   */
-);
-
-/* Check that any remaining bits in the last byte are set to 1 */
-void SKP_Silk_range_coder_check_after_decoding(
-    SKP_Silk_range_coder_state  *psRC               /* I/O  compressed data structure                   */
-);
-
 /* Gain scalar quantization with hysteresis, uniform on log scale */
 void SKP_Silk_gains_quant(
     SKP_int                         ind[ MAX_NB_SUBFR ],        /* O    gain indices                            */
@@ -311,9 +244,9 @@
 /****************/
 SKP_int SKP_Silk_decode_frame(
     SKP_Silk_decoder_state      *psDec,             /* I/O  Pointer to Silk decoder state               */
+    ec_dec                      *psRangeDec,        /* I/O  Compressor data structure                   */
     SKP_int16                   pOut[],             /* O    Pointer to output speech frame              */
     SKP_int16                   *pN,                /* O    Pointer to size of output frame             */
-    const SKP_uint8             pCode[],            /* I    Pointer to payload                          */
     const SKP_int               nBytes,             /* I    Payload length                              */
     SKP_int                     action,             /* I    Action from Jitter Buffer                   */
     SKP_int                     *decBytes           /* O    Used bytes to decode this frame             */
@@ -320,14 +253,16 @@
 );
 
 /* Decode indices from payload v4 Bitstream */
-void SKP_Silk_decode_indices_v4(
-    SKP_Silk_decoder_state      *psDec              /* I/O  State                                       */
+void SKP_Silk_decode_indices(
+    SKP_Silk_decoder_state      *psDec,             /* I/O  State                                       */
+    ec_dec                      *psRangeDec         /* I/O  Compressor data structure                   */
 );
 
 /* Decode parameters from payload v4 Bitstream */
-void SKP_Silk_decode_parameters_v4(
+void SKP_Silk_decode_parameters(
     SKP_Silk_decoder_state      *psDec,                                 /* I/O  State                                    */
     SKP_Silk_decoder_control    *psDecCtrl,                             /* I/O  Decoder control                          */
+    ec_dec                      *psRangeDec,                            /* I/O  Compressor data structure                */
     SKP_int                     q[ MAX_FRAME_LENGTH ],                  /* O    Excitation signal                        */
     const SKP_int               fullDecoding                            /* I    Flag to tell if only arithmetic decoding */
 );
@@ -354,7 +289,7 @@
 
 /* Decode quantization indices of excitation (Shell coding) */
 void SKP_Silk_decode_pulses(
-    SKP_Silk_range_coder_state  *psRC,              /* I/O  Range coder state                           */
+    ec_dec                      *psRangeDec,        /* I/O  Compressor data structure                   */
     SKP_Silk_decoder_control    *psDecCtrl,         /* I/O  Decoder control                             */
     SKP_int                     q[],                /* O    Excitation signal                           */
     const SKP_int               frame_length        /* I    Frame length (preliminary)                  */
@@ -378,10 +313,10 @@
 );
 
 /* Encoding of various parameters */
-void SKP_Silk_encode_parameters_v4(
+void SKP_Silk_encode_parameters(
     SKP_Silk_encoder_state      *psEncC,            /* I/O  Encoder state                               */
     SKP_Silk_encoder_control    *psEncCtrlC,        /* I/O  Encoder control                             */
-    SKP_Silk_range_coder_state  *psRC               /* I/O  Range encoder state                         */
+    ec_enc                      *psRangeEnc         /* I/O  Compressor data structure                   */
 );
 
 /* Extract lowest layer encoding */
--- a/src_common/SKP_Silk_range_coder.c
+++ b/src_common/SKP_Silk_range_coder.c
@@ -27,77 +27,10 @@
 
 #include "SKP_Silk_main.h"
 
-#define MAX_SIZE 10000
-
-/* Range encoder for one symbol */
-void SKP_Silk_range_encoder(
-    SKP_Silk_range_coder_state      *psRC,              /* I/O  compressor data structure                   */
-    const SKP_int                   data,               /* I    uncompressed data                           */
-    const SKP_uint16                prob[]              /* I    cumulative density functions                */
-)
-{
-    SKP_uint32 low_Q16, high_Q16;
-
-    if( psRC->error ) {
-        return;
-    }
-    low_Q16  = prob[ data ];
-    high_Q16 = prob[ data + 1 ];
-    
-#ifdef SAVE_ALL_INTERNAL_DATA
-    DEBUG_STORE_DATA( enc_l.dat, &low_Q16,  sizeof(SKP_uint32) );
-    DEBUG_STORE_DATA( enc_h.dat, &high_Q16, sizeof(SKP_uint32) );
-    DEBUG_STORE_DATA( enc.dat,   &data,     sizeof(SKP_int) );
-#endif
-
-    if( prob[ 2 ] == 65535 ) {
-        /* Instead of detection, we could add a separate function and call when we know that input is a bit */
-        ec_enc_bit_prob( &psRC->range_enc_celt_state, data, 65536 - prob[ 1 ] );
-    } else {
-        ec_encode_bin( &psRC->range_enc_celt_state, low_Q16, high_Q16, 16 );
-    }
-}
-
-/* Range encoder for one symbol, with uniform PDF*/
-void SKP_Silk_range_encode_uniform(
-    SKP_Silk_range_coder_state      *psRC,              /* I/O  compressor data structure                   */
-    const SKP_int                   data,               /* I    uncompressed data                           */
-    const SKP_int                   N                   /* I    number of possible outcomes                 */
-)
-{
-    SKP_int i;
-    SKP_uint16 delta, prob[ MAX_SIZE + 1 ];
-
-    SKP_assert( N < MAX_SIZE );
-
-    delta = ( SKP_uint16 )SKP_DIV32_16( 65535, N );
-    prob[ 0 ] = 0;
-    for( i = 0; i < N - 1; i++ ) {
-        prob[ i + 1 ] = prob[ i ] + delta;
-    }
-    prob[ N ] = 65535;
-
-    SKP_Silk_range_encoder( psRC, data, prob );
-}
-
-/* Range encoder for multiple symbols */
-void SKP_Silk_range_encoder_multi(
-    SKP_Silk_range_coder_state      *psRC,              /* I/O  compressor data structure                   */
-    const SKP_int                   data[],             /* I    uncompressed data    [nSymbols]             */
-    const SKP_uint16 * const        prob[],             /* I    cumulative density functions                */
-    const SKP_int                   nSymbols            /* I    number of data symbols                      */
-)
-{
-    SKP_int k;
-    for( k = 0; k < nSymbols; k++ ) {
-        SKP_Silk_range_encoder( psRC, data[ k ], prob[ k ] );
-    }
-}
-
 /* Range decoder for one symbol */
 void SKP_Silk_range_decoder(
     SKP_int                         data[],             /* O    uncompressed data                           */
-    SKP_Silk_range_coder_state      *psRC,              /* I/O  compressor data structure                   */
+    ec_dec                          *psRangeDec,        /* I/O  Compressor data structure                   */
     const SKP_uint16                prob[],             /* I    cumulative density function                 */
     SKP_int                         probIx              /* I    initial (middle) entry of cdf               */
 )
@@ -107,21 +40,12 @@
     SKP_uint32 low_Q16_returned;
     SKP_int    temp;
 
-    if( psRC->error ) {
-        /* Set output to zero */
-        *data = 0;
-        return;
-    }
-
     if( prob[ 2 ] == 65535 ) {
         /* Instead of detection, we could add a separate function and call when we know that output is a bit */
-        *data = ec_dec_bit_prob( &psRC->range_dec_celt_state, 65536 - prob[ 1 ] );
+        *data = ec_dec_bit_prob( psRangeDec, 65536 - prob[ 1 ] );
     } else {
-        low_Q16_returned = ec_decode_bin( &psRC->range_dec_celt_state, 16 );
-    }
+        low_Q16_returned = ec_decode_bin( psRangeDec, 16 );
 
-    /* OPTIMIZE ME WITH BI-SECTION */
-    if( prob[ 2 ] != 65535 ) {
 #if 1
         temp = 0;
         while( low_Q16_returned >= prob[ ++temp ] ) {}
@@ -150,141 +74,9 @@
         DEBUG_STORE_DATA( dec_l.dat,  &low_Q16,           sizeof( SKP_uint32 ) );
         DEBUG_STORE_DATA( dec_h.dat,  &high_Q16,          sizeof( SKP_uint32 ) );
 #endif  
-        ec_dec_update( &psRC->range_dec_celt_state, low_Q16, high_Q16,( 1 << 16 ) );
+        ec_dec_update( psRangeDec, low_Q16, high_Q16,( 1 << 16 ) );
     }
 #ifdef SAVE_ALL_INTERNAL_DATA
     DEBUG_STORE_DATA( dec.dat, data, sizeof( SKP_int ) );
 #endif
-}
-
-/* Range decoder for one symbol, with uniform PDF*/
-void SKP_Silk_range_decode_uniform(
-    SKP_int                         data[],             /* O    uncompressed data                           */
-    SKP_Silk_range_coder_state      *psRC,              /* I/O  compressor data structure                   */
-    const SKP_int                   N                   /* I    number of possible outcomes                 */
-)
-{
-    SKP_int i;
-    SKP_uint16 delta, prob[ MAX_SIZE + 1 ];
-
-    SKP_assert( N < MAX_SIZE );
-
-    delta = ( SKP_uint16 )SKP_DIV32_16( 65535, N );
-    prob[ 0 ] = 0;
-    for( i = 0; i < N - 1; i++ ) {
-        prob[ i + 1 ] = prob[ i ] + delta;
-    }
-    prob[ N ] = 65535;
-
-    SKP_Silk_range_decoder( data, psRC, prob, ( N >> 1 ) );
-}
-
-/* Range decoder for multiple symbols */
-void SKP_Silk_range_decoder_multi(
-    SKP_int                         data[],             /* O    uncompressed data                [nSymbols] */
-    SKP_Silk_range_coder_state      *psRC,              /* I/O  compressor data structure                   */
-    const SKP_uint16 * const        prob[],             /* I    cumulative density functions                */
-    const SKP_int                   probStartIx[],      /* I    initial (middle) entries of cdfs [nSymbols] */
-    const SKP_int                   nSymbols            /* I    number of data symbols                      */
-)
-{
-    SKP_int k;
-    for( k = 0; k < nSymbols; k++ ) {
-        SKP_Silk_range_decoder( &data[ k ], psRC, prob[ k ], probStartIx[ k ] );
-    }
-}
-
-/* Initialize range encoder */
-void SKP_Silk_range_enc_init(
-    SKP_Silk_range_coder_state      *psRC               /* O    compressor data structure                   */
-)
-{
-    psRC->error        = 0;
-}
-
-/* Initialize range decoder */
-void SKP_Silk_range_dec_init(
-    SKP_Silk_range_coder_state      *psRC,              /* O    compressor data structure                   */
-    const SKP_uint8                 buffer[],           /* I    buffer for compressed data [bufferLength]   */
-    const SKP_int32                 bufferLength        /* I    buffer length (in bytes)                    */
-)
-{
-    /* check input */
-    if( bufferLength > MAX_ARITHM_BYTES ) {
-        psRC->error = RANGE_CODER_DEC_PAYLOAD_TOO_LONG;
-        return;
-    }
-    /* Initialize structure */
-    /* Copy to internal buffer */
-    SKP_memcpy( psRC->buffer, buffer, bufferLength * sizeof( SKP_uint8 ) ); 
-    psRC->bufferLength = bufferLength;
-    psRC->bufferIx = 0;
-    psRC->base_Q32 = 
-        SKP_LSHIFT_uint( ( SKP_uint32 )buffer[ 0 ], 24 ) | 
-        SKP_LSHIFT_uint( ( SKP_uint32 )buffer[ 1 ], 16 ) | 
-        SKP_LSHIFT_uint( ( SKP_uint32 )buffer[ 2 ],  8 ) | 
-                         ( SKP_uint32 )buffer[ 3 ];
-    psRC->range_Q16 = 0x0000FFFF;
-    psRC->error     = 0;
-}
-
-/* Determine length of bitstream */
-SKP_int SKP_Silk_range_encoder_get_length(              /* O    returns number of BITS in stream            */
-    SKP_Silk_range_coder_state          *psRC,          /* I    compressed data structure                   */
-    SKP_int                             *nBytes         /* O    number of BYTES in stream                   */
-)
-{
-    SKP_int nBits;
-
-    /* Get number of bits in bitstream */
-    nBits = ec_enc_tell( &psRC->range_enc_celt_state, 0 );
-
-    /* Round up to an integer number of bytes */
-    *nBytes = SKP_RSHIFT( nBits + 7, 3 );
-
-    /* Return number of bits in bitstream */
-    return nBits;
-}
-
-/* Determine length of bitstream */
-SKP_int SKP_Silk_range_decoder_get_length(              /* O    returns number of BITS in stream            */
-    SKP_Silk_range_coder_state          *psRC,          /* I    compressed data structure                   */
-    SKP_int                             *nBytes         /* O    number of BYTES in stream                   */
-)
-{
-    SKP_int nBits;
-
-    /* Get number of bits in bitstream */
-    nBits = ec_dec_tell( &psRC->range_dec_celt_state, 0 );
-
-    /* Round up to an integer number of bytes */
-    *nBytes = SKP_RSHIFT( nBits + 7, 3 );
-
-    /* Return number of bits in bitstream */
-    return nBits;
-}
-
-/* Check that any remaining bits in the last byte are set to 1 */
-void SKP_Silk_range_coder_check_after_decoding(
-    SKP_Silk_range_coder_state      *psRC               /* I/O  compressed data structure                   */
-)
-{
-    SKP_int bits_in_stream, nBytes, mask;
-
-    bits_in_stream = SKP_Silk_range_decoder_get_length( psRC, &nBytes );
-
-    /* Make sure not to read beyond buffer */
-    if( nBytes - 1 >= psRC->range_dec_celt_state.buf->storage ) {
-        psRC->error = RANGE_CODER_DECODER_CHECK_FAILED;
-        return;
-    }
-
-    /* Test any remaining bits in last byte */
-    if( bits_in_stream & 7 ) {
-        mask = SKP_RSHIFT( 0xFF, bits_in_stream & 7 );
-        if( ( psRC->range_dec_celt_state.buf->buf[ nBytes - 1 ] & mask ) != mask ) {
-            psRC->error = RANGE_CODER_DECODER_CHECK_FAILED;
-            return;
-        }
-    }
 }
--- a/src_common/SKP_Silk_shell_coder.c
+++ b/src_common/SKP_Silk_shell_coder.c
@@ -42,7 +42,7 @@
 }
 
 SKP_INLINE void encode_split(
-    SKP_Silk_range_coder_state  *sRC,           /* I/O: compressor data structure                   */
+    ec_enc                      *psRangeEnc,    /* I/O  compressor data structure                   */
     const SKP_int               p_child1,       /* I:   pulse amplitude of first child subframe     */
     const SKP_int               p,              /* I:   pulse amplitude of current subframe         */
     const SKP_uint16            *shell_table    /* I:   table of shell cdfs                         */
@@ -52,7 +52,11 @@
 
     if( p > 0 ) {
         cdf = &shell_table[ SKP_Silk_shell_code_table_offsets[ p ] ];
-        SKP_Silk_range_encoder( sRC, p_child1, cdf );
+        if( cdf[ 2 ] == 65535 ) {
+            ec_enc_bit_prob( psRangeEnc, p_child1, 65536 - cdf[ 1 ] );
+        } else {
+            ec_encode_bin( psRangeEnc, cdf[ p_child1 ], cdf[ p_child1 + 1 ], 16 );
+        }
     }
 }
 
@@ -59,7 +63,7 @@
 SKP_INLINE void decode_split(
     SKP_int                     *p_child1,      /* O:   pulse amplitude of first child subframe     */
     SKP_int                     *p_child2,      /* O:   pulse amplitude of second child subframe    */
-    SKP_Silk_range_coder_state  *sRC,           /* I/O: compressor data structure                   */
+    ec_dec                      *psRangeDec,    /* I/O  Compressor data structure                   */
     const SKP_int               p,              /* I:   pulse amplitude of current subframe         */
     const SKP_uint16            *shell_table    /* I:   table of shell cdfs                         */
 )
@@ -70,7 +74,7 @@
     if( p > 0 ) {
         cdf_middle = SKP_RSHIFT( p, 1 );
         cdf = &shell_table[ SKP_Silk_shell_code_table_offsets[ p ] ];
-        SKP_Silk_range_decoder( p_child1, sRC, cdf, cdf_middle );
+        SKP_Silk_range_decoder( p_child1, psRangeDec, cdf, cdf_middle );
         p_child2[ 0 ] = p - p_child1[ 0 ];
     } else {
         p_child1[ 0 ] = 0;
@@ -80,8 +84,8 @@
 
 /* Shell encoder, operates on one shell code frame of 16 pulses */
 void SKP_Silk_shell_encoder(
-    SKP_Silk_range_coder_state      *sRC,               /* I/O  compressor data structure                   */
-    const SKP_int                   *pulses0            /* I    data: nonnegative pulse amplitudes          */
+    ec_enc                      *psRangeEnc,        /* I/O  compressor data structure                   */
+    const SKP_int               *pulses0            /* I    data: nonnegative pulse amplitudes          */
 )
 {
     SKP_int pulses1[ 8 ], pulses2[ 4 ], pulses3[ 2 ], pulses4[ 1 ];
@@ -95,27 +99,27 @@
     combine_pulses( pulses3, pulses2, 2 );
     combine_pulses( pulses4, pulses3, 1 );
 
-    encode_split( sRC, pulses3[  0 ], pulses4[ 0 ], SKP_Silk_shell_code_table3 );
+    encode_split( psRangeEnc, pulses3[  0 ], pulses4[ 0 ], SKP_Silk_shell_code_table3 );
 
-    encode_split( sRC, pulses2[  0 ], pulses3[ 0 ], SKP_Silk_shell_code_table2 );
+    encode_split( psRangeEnc, pulses2[  0 ], pulses3[ 0 ], SKP_Silk_shell_code_table2 );
 
-    encode_split( sRC, pulses1[  0 ], pulses2[ 0 ], SKP_Silk_shell_code_table1 );
-    encode_split( sRC, pulses0[  0 ], pulses1[ 0 ], SKP_Silk_shell_code_table0 );
-    encode_split( sRC, pulses0[  2 ], pulses1[ 1 ], SKP_Silk_shell_code_table0 );
+    encode_split( psRangeEnc, pulses1[  0 ], pulses2[ 0 ], SKP_Silk_shell_code_table1 );
+    encode_split( psRangeEnc, pulses0[  0 ], pulses1[ 0 ], SKP_Silk_shell_code_table0 );
+    encode_split( psRangeEnc, pulses0[  2 ], pulses1[ 1 ], SKP_Silk_shell_code_table0 );
 
-    encode_split( sRC, pulses1[  2 ], pulses2[ 1 ], SKP_Silk_shell_code_table1 );
-    encode_split( sRC, pulses0[  4 ], pulses1[ 2 ], SKP_Silk_shell_code_table0 );
-    encode_split( sRC, pulses0[  6 ], pulses1[ 3 ], SKP_Silk_shell_code_table0 );
+    encode_split( psRangeEnc, pulses1[  2 ], pulses2[ 1 ], SKP_Silk_shell_code_table1 );
+    encode_split( psRangeEnc, pulses0[  4 ], pulses1[ 2 ], SKP_Silk_shell_code_table0 );
+    encode_split( psRangeEnc, pulses0[  6 ], pulses1[ 3 ], SKP_Silk_shell_code_table0 );
 
-    encode_split( sRC, pulses2[  2 ], pulses3[ 1 ], SKP_Silk_shell_code_table2 );
+    encode_split( psRangeEnc, pulses2[  2 ], pulses3[ 1 ], SKP_Silk_shell_code_table2 );
 
-    encode_split( sRC, pulses1[  4 ], pulses2[ 2 ], SKP_Silk_shell_code_table1 );
-    encode_split( sRC, pulses0[  8 ], pulses1[ 4 ], SKP_Silk_shell_code_table0 );
-    encode_split( sRC, pulses0[ 10 ], pulses1[ 5 ], SKP_Silk_shell_code_table0 );
+    encode_split( psRangeEnc, pulses1[  4 ], pulses2[ 2 ], SKP_Silk_shell_code_table1 );
+    encode_split( psRangeEnc, pulses0[  8 ], pulses1[ 4 ], SKP_Silk_shell_code_table0 );
+    encode_split( psRangeEnc, pulses0[ 10 ], pulses1[ 5 ], SKP_Silk_shell_code_table0 );
 
-    encode_split( sRC, pulses1[  6 ], pulses2[ 3 ], SKP_Silk_shell_code_table1 );
-    encode_split( sRC, pulses0[ 12 ], pulses1[ 6 ], SKP_Silk_shell_code_table0 );
-    encode_split( sRC, pulses0[ 14 ], pulses1[ 7 ], SKP_Silk_shell_code_table0 );
+    encode_split( psRangeEnc, pulses1[  6 ], pulses2[ 3 ], SKP_Silk_shell_code_table1 );
+    encode_split( psRangeEnc, pulses0[ 12 ], pulses1[ 6 ], SKP_Silk_shell_code_table0 );
+    encode_split( psRangeEnc, pulses0[ 14 ], pulses1[ 7 ], SKP_Silk_shell_code_table0 );
 }
 
 
@@ -122,7 +126,7 @@
 /* Shell decoder, operates on one shell code frame of 16 pulses */
 void SKP_Silk_shell_decoder(
     SKP_int                         *pulses0,           /* O    data: nonnegative pulse amplitudes          */
-    SKP_Silk_range_coder_state      *sRC,               /* I/O  compressor data structure                   */
+    ec_dec                          *psRangeDec,        /* I/O  Compressor data structure                   */
     const SKP_int                   pulses4             /* I    number of pulses per pulse-subframe         */
 )
 {
@@ -131,25 +135,25 @@
     /* this function operates on one shell code frame of 16 pulses */
     SKP_assert( SHELL_CODEC_FRAME_LENGTH == 16 );
 
-    decode_split( &pulses3[  0 ], &pulses3[  1 ], sRC, pulses4,      SKP_Silk_shell_code_table3 );
+    decode_split( &pulses3[  0 ], &pulses3[  1 ], psRangeDec, pulses4,      SKP_Silk_shell_code_table3 );
 
-    decode_split( &pulses2[  0 ], &pulses2[  1 ], sRC, pulses3[ 0 ], SKP_Silk_shell_code_table2 );
+    decode_split( &pulses2[  0 ], &pulses2[  1 ], psRangeDec, pulses3[ 0 ], SKP_Silk_shell_code_table2 );
 
-    decode_split( &pulses1[  0 ], &pulses1[  1 ], sRC, pulses2[ 0 ], SKP_Silk_shell_code_table1 );
-    decode_split( &pulses0[  0 ], &pulses0[  1 ], sRC, pulses1[ 0 ], SKP_Silk_shell_code_table0 );
-    decode_split( &pulses0[  2 ], &pulses0[  3 ], sRC, pulses1[ 1 ], SKP_Silk_shell_code_table0 );
+    decode_split( &pulses1[  0 ], &pulses1[  1 ], psRangeDec, pulses2[ 0 ], SKP_Silk_shell_code_table1 );
+    decode_split( &pulses0[  0 ], &pulses0[  1 ], psRangeDec, pulses1[ 0 ], SKP_Silk_shell_code_table0 );
+    decode_split( &pulses0[  2 ], &pulses0[  3 ], psRangeDec, pulses1[ 1 ], SKP_Silk_shell_code_table0 );
 
-    decode_split( &pulses1[  2 ], &pulses1[  3 ], sRC, pulses2[ 1 ], SKP_Silk_shell_code_table1 );
-    decode_split( &pulses0[  4 ], &pulses0[  5 ], sRC, pulses1[ 2 ], SKP_Silk_shell_code_table0 );
-    decode_split( &pulses0[  6 ], &pulses0[  7 ], sRC, pulses1[ 3 ], SKP_Silk_shell_code_table0 );
+    decode_split( &pulses1[  2 ], &pulses1[  3 ], psRangeDec, pulses2[ 1 ], SKP_Silk_shell_code_table1 );
+    decode_split( &pulses0[  4 ], &pulses0[  5 ], psRangeDec, pulses1[ 2 ], SKP_Silk_shell_code_table0 );
+    decode_split( &pulses0[  6 ], &pulses0[  7 ], psRangeDec, pulses1[ 3 ], SKP_Silk_shell_code_table0 );
 
-    decode_split( &pulses2[  2 ], &pulses2[  3 ], sRC, pulses3[ 1 ], SKP_Silk_shell_code_table2 );
+    decode_split( &pulses2[  2 ], &pulses2[  3 ], psRangeDec, pulses3[ 1 ], SKP_Silk_shell_code_table2 );
 
-    decode_split( &pulses1[  4 ], &pulses1[  5 ], sRC, pulses2[ 2 ], SKP_Silk_shell_code_table1 );
-    decode_split( &pulses0[  8 ], &pulses0[  9 ], sRC, pulses1[ 4 ], SKP_Silk_shell_code_table0 );
-    decode_split( &pulses0[ 10 ], &pulses0[ 11 ], sRC, pulses1[ 5 ], SKP_Silk_shell_code_table0 );
+    decode_split( &pulses1[  4 ], &pulses1[  5 ], psRangeDec, pulses2[ 2 ], SKP_Silk_shell_code_table1 );
+    decode_split( &pulses0[  8 ], &pulses0[  9 ], psRangeDec, pulses1[ 4 ], SKP_Silk_shell_code_table0 );
+    decode_split( &pulses0[ 10 ], &pulses0[ 11 ], psRangeDec, pulses1[ 5 ], SKP_Silk_shell_code_table0 );
 
-    decode_split( &pulses1[  6 ], &pulses1[  7 ], sRC, pulses2[ 3 ], SKP_Silk_shell_code_table1 );
-    decode_split( &pulses0[ 12 ], &pulses0[ 13 ], sRC, pulses1[ 6 ], SKP_Silk_shell_code_table0 );
-    decode_split( &pulses0[ 14 ], &pulses0[ 15 ], sRC, pulses1[ 7 ], SKP_Silk_shell_code_table0 );
+    decode_split( &pulses1[  6 ], &pulses1[  7 ], psRangeDec, pulses2[ 3 ], SKP_Silk_shell_code_table1 );
+    decode_split( &pulses0[ 12 ], &pulses0[ 13 ], psRangeDec, pulses1[ 6 ], SKP_Silk_shell_code_table0 );
+    decode_split( &pulses0[ 14 ], &pulses0[ 15 ], psRangeDec, pulses1[ 7 ], SKP_Silk_shell_code_table0 );
 }
--- a/src_common/SKP_Silk_structs.h
+++ b/src_common/SKP_Silk_structs.h
@@ -83,6 +83,7 @@
 /*******************************/
 /* Range encoder/decoder state */
 /*******************************/
+#if 0
 typedef struct {
     SKP_int32   bufferLength;
     SKP_int32   bufferIx;
@@ -90,10 +91,10 @@
     SKP_uint32  range_Q16;
     SKP_int32   error;
     SKP_uint8   buffer[ MAX_ARITHM_BYTES ];     /* Buffer containing payload                                */
-    /* The CELT entropy encoder + decoder */
-    ec_enc      range_enc_celt_state;
+    /* The CELT entropy decoder */
     ec_dec      range_dec_celt_state;
 } SKP_Silk_range_coder_state;
+#endif
 
 /* Input frequency range detection struct */
 typedef struct {
@@ -138,8 +139,8 @@
 /* Encoder state                */
 /********************************/
 typedef struct {
-    SKP_Silk_range_coder_state      sRC;                            /* Range coder state                                                    */
-    SKP_Silk_range_coder_state      sRC_LBRR;                       /* Range coder state (for low bitrate redundancy)                       */
+    //SKP_Silk_range_coder_state      sRC;                            /* Range coder state                                                    */
+    //SKP_Silk_range_coder_state      sRC_LBRR;                       /* Range coder state (for low bitrate redundancy)                       */
 #if HIGH_PASS_INPUT
     SKP_int32                       In_HP_State[ 2 ];               /* High pass filter state                                               */
 #endif
@@ -277,7 +278,7 @@
 /* Decoder state                */
 /********************************/
 typedef struct {
-    SKP_Silk_range_coder_state  sRC;                            /* Range coder state                                                    */
+    //SKP_Silk_range_coder_state  sRC;                            /* Range coder state                                                    */
     SKP_int32       prev_inv_gain_Q16;
     SKP_int32       sLTP_Q16[ 2 * MAX_FRAME_LENGTH ];
     SKP_int32       sLPC_Q14[ MAX_FRAME_LENGTH / MAX_NB_SUBFR + MAX_LPC_ORDER ];
--- a/src_common/SKP_Silk_tables.h
+++ b/src_common/SKP_Silk_tables.h
@@ -148,8 +148,8 @@
 extern const SKP_int16  SKP_Silk_Dec_B_HP_8[ DEC_HP_ORDER + 1 ];                                    /*   3 */
 
 /* Table for frame termination indication */
-extern const SKP_uint16 SKP_Silk_FrameTermination_v4_CDF[ 6 ];
-extern const SKP_int    SKP_Silk_FrameTermination_v4_offset;
+extern const SKP_uint16 SKP_Silk_FrameTermination_CDF[ 6 ];
+extern const SKP_int    SKP_Silk_FrameTermination_offset;
 
 /* Table for random seed */
 extern const SKP_uint16 SKP_Silk_Seed_CDF[ 5 ];
--- a/src_common/SKP_Silk_tables_other.c
+++ b/src_common/SKP_Silk_tables_other.c
@@ -105,8 +105,8 @@
 const SKP_int    SKP_Silk_NLSF_interpolation_factor_offset   = 4;
 
 /* Table for frame termination indication */
-const SKP_uint16 SKP_Silk_FrameTermination_v4_CDF[ 6 ] = {0,   13107,   26214,   39321,   52428,   65535};
-const SKP_int    SKP_Silk_FrameTermination_v4_offset   = 4;
+const SKP_uint16 SKP_Silk_FrameTermination_CDF[ 6 ] = {0,   13107,   26214,   39321,   52428,   65535};
+const SKP_int    SKP_Silk_FrameTermination_offset   = 4;
 
 /* Table for random seed */
 const SKP_uint16 SKP_Silk_Seed_CDF[ 5 ] = {0, 16384, 32768, 49152, 65535};
--- a/src_common/Silk_CommonFLP.vcproj
+++ b/src_common/Silk_CommonFLP.vcproj
@@ -184,11 +184,11 @@
 				>
 			</File>
 			<File
-				RelativePath=".\SKP_Silk_decode_indices_v4.c"
+				RelativePath=".\SKP_Silk_decode_indices.c"
 				>
 			</File>
 			<File
-				RelativePath=".\SKP_Silk_decode_parameters_v4.c"
+				RelativePath=".\SKP_Silk_decode_parameters.c"
 				>
 			</File>
 			<File
@@ -208,7 +208,7 @@
 				>
 			</File>
 			<File
-				RelativePath=".\SKP_Silk_encode_parameters_v4.c"
+				RelativePath=".\SKP_Silk_encode_parameters.c"
 				>
 			</File>
 			<File
--- a/test/Dec_SDK.vcproj
+++ b/test/Dec_SDK.vcproj
@@ -1,7 +1,7 @@
 <?xml version="1.0" encoding="Windows-1252"?>
 <VisualStudioProject
 	ProjectType="Visual C++"
-	Version="8,00"
+	Version="8.00"
 	Name="Dec"
 	ProjectGUID="{82685D7F-0589-42BD-877C-31A952D53A8E}"
 	RootNamespace="Test"
@@ -40,7 +40,7 @@
 			<Tool
 				Name="VCCLCompilerTool"
 				Optimization="0"
-				AdditionalIncludeDirectories="../interface;../src_SigProc_FIX"
+				AdditionalIncludeDirectories="../../../celt-0.8.0/libcelt;../interface;../src_SigProc_FIX"
 				PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE;COMPILE_SDK"
 				MinimalRebuild="true"
 				BasicRuntimeChecks="3"
@@ -121,7 +121,7 @@
 			/>
 			<Tool
 				Name="VCCLCompilerTool"
-				AdditionalIncludeDirectories="../interface;../src_SigProc_FIX"
+				AdditionalIncludeDirectories="../../../celt-0.8.0/libcelt;../interface;../src_SigProc_FIX"
 				PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE;COMPILE_SDK"
 				RuntimeLibrary="0"
 				FloatingPointModel="2"
--- a/test/Dec_SDK.vcproj.SKYPE-KOEN.koen.user
+++ /dev/null
@@ -1,65 +1,0 @@
-<?xml version="1.0" encoding="Windows-1252"?>
-<VisualStudioUserFile
-	ProjectType="Visual C++"
-	Version="8.00"
-	ShowAllFiles="false"
-	>
-	<Configurations>
-		<Configuration
-			Name="Debug|Win32"
-			>
-			<DebugSettings
-				Command="$(TargetPath)"
-				WorkingDirectory=""
-				CommandArguments=""
-				Attach="false"
-				DebuggerType="3"
-				Remote="1"
-				RemoteMachine="SKYPE-KOEN"
-				RemoteCommand=""
-				HttpUrl=""
-				PDBPath=""
-				SQLDebugging=""
-				Environment=""
-				EnvironmentMerge="true"
-				DebuggerFlavor=""
-				MPIRunCommand=""
-				MPIRunArguments=""
-				MPIRunWorkingDirectory=""
-				ApplicationCommand=""
-				ApplicationArguments=""
-				ShimCommand=""
-				MPIAcceptMode=""
-				MPIAcceptFilter=""
-			/>
-		</Configuration>
-		<Configuration
-			Name="Release|Win32"
-			>
-			<DebugSettings
-				Command="$(TargetPath)"
-				WorkingDirectory=""
-				CommandArguments=""
-				Attach="false"
-				DebuggerType="3"
-				Remote="1"
-				RemoteMachine="SKYPE-KOEN"
-				RemoteCommand=""
-				HttpUrl=""
-				PDBPath=""
-				SQLDebugging=""
-				Environment=""
-				EnvironmentMerge="true"
-				DebuggerFlavor=""
-				MPIRunCommand=""
-				MPIRunArguments=""
-				MPIRunWorkingDirectory=""
-				ApplicationCommand=""
-				ApplicationArguments=""
-				ShimCommand=""
-				MPIAcceptMode=""
-				MPIAcceptFilter=""
-			/>
-		</Configuration>
-	</Configurations>
-</VisualStudioUserFile>
--- a/test/Decoder.c
+++ b/test/Decoder.c
@@ -47,7 +47,7 @@
 #define MAX_FRAME_LENGTH        480
 #define MAX_FRAME_LENGTH_MS     20
 #define MAX_API_FS_KHZ          48
-#define MAX_LBRR_DELAY          2
+#define MAX_LBRR_DELAY          0
 
 #ifdef _SYSTEM_IS_BIG_ENDIAN
 /* Function to convert a little endian int16 to a */
@@ -108,6 +108,8 @@
     float     loss_prob;
     SKP_int32 frames, lost, quiet;
     SKP_SILK_SDK_DecControlStruct DecControl;
+    ec_byte_buffer range_dec_celt_buf;
+    ec_dec         range_dec_celt_state;
 
     if( argc < 3 ) {
         print_usage( argv );
@@ -217,6 +219,10 @@
             break;
         }
 
+        /* Initialize range decoder state */
+        ec_byte_writeinit_buffer( &range_dec_celt_buf, payloadEnd, nBytes );
+        ec_dec_init( &range_dec_celt_state, &range_dec_celt_buf );
+
         /* Simulate losses */
         if( ( (float)rand() / (float)RAND_MAX >= loss_prob / 100 ) && counter > 0 ) {
             nBytesPerPacket[ MAX_LBRR_DELAY ] = nBytes;
@@ -234,7 +240,7 @@
             for( i = 0; i < MAX_LBRR_DELAY; i++ ) {
                 if( nBytesPerPacket[ i + 1 ] > 0 ) {
                     starttime = GetHighResolutionTime();
-                    SKP_Silk_SDK_search_for_LBRR( payloadPtr, nBytesPerPacket[ i + 1 ], i + 1, FECpayload, &nBytesFEC );
+                    //SKP_Silk_SDK_search_for_LBRR( payloadPtr, nBytesPerPacket[ i + 1 ], i + 1, FECpayload, &nBytesFEC );
                     tottime += GetHighResolutionTime() - starttime;
                     if( nBytesFEC > 0 ) {
                         payloadToDec = FECpayload;
@@ -261,7 +267,7 @@
             do {
                 /* Decode 20 ms */
                 starttime = GetHighResolutionTime();
-                ret = SKP_Silk_SDK_Decode( psDec, &DecControl, 0, payloadToDec, nBytes, outPtr, &len );
+                ret = SKP_Silk_SDK_Decode( psDec, &DecControl, 0, &range_dec_celt_state, nBytes, outPtr, &len );
                 tottime += GetHighResolutionTime() - starttime;
                 if( ret ) {
                     printf( "\nSKP_Silk_SDK_Decode returned %d", ret );
@@ -287,7 +293,7 @@
             for( i = 0; i < DecControl.framesPerPacket; i++ ) {
                 /* Generate 20 ms */
                 starttime = GetHighResolutionTime();
-                ret = SKP_Silk_SDK_Decode( psDec, &DecControl, 1, payloadToDec, nBytes, outPtr, &len );
+                ret = SKP_Silk_SDK_Decode( psDec, &DecControl, 1, &range_dec_celt_state, nBytes, outPtr, &len );
                 tottime += GetHighResolutionTime() - starttime;
                 if( ret ) {
                     printf( "\nSKP_Silk_Decode returned %d", ret );
@@ -335,7 +341,7 @@
             for( i = 0; i < MAX_LBRR_DELAY; i++ ) {
                 if( nBytesPerPacket[ i + 1 ] > 0 ) {
                     starttime = GetHighResolutionTime();
-                    SKP_Silk_SDK_search_for_LBRR( payloadPtr, nBytesPerPacket[ i + 1 ], i + 1, FECpayload, &nBytesFEC );
+                    //SKP_Silk_SDK_search_for_LBRR( payloadPtr, nBytesPerPacket[ i + 1 ], i + 1, FECpayload, &nBytesFEC );
                     tottime += GetHighResolutionTime() - starttime;
                     if( nBytesFEC > 0 ) {
                         payloadToDec = FECpayload;
@@ -362,7 +368,7 @@
             do {
                 /* Decode 20 ms */
                 starttime = GetHighResolutionTime();
-                ret = SKP_Silk_SDK_Decode( psDec, &DecControl, 0, payloadToDec, nBytes, outPtr, &len );
+                ret = SKP_Silk_SDK_Decode( psDec, &DecControl, 0, &range_dec_celt_state, nBytes, outPtr, &len );
                 tottime += GetHighResolutionTime() - starttime;
                 if( ret ) {
                     printf( "\nSKP_Silk_SDK_Decode returned %d", ret );
@@ -385,7 +391,7 @@
             /* Generate 20 ms */
             for( i = 0; i < DecControl.framesPerPacket; i++ ) {
                 starttime = GetHighResolutionTime();
-                ret = SKP_Silk_SDK_Decode( psDec, &DecControl, 1, payloadToDec, nBytes, outPtr, &len );
+                ret = SKP_Silk_SDK_Decode( psDec, &DecControl, 1, &range_dec_celt_state, nBytes, outPtr, &len );
                 tottime += GetHighResolutionTime() - starttime;
                 if( ret ) {
                     printf( "\nSKP_Silk_Decode returned %d", ret );
--- a/test/Enc_SDK.vcproj
+++ b/test/Enc_SDK.vcproj
@@ -1,7 +1,7 @@
 <?xml version="1.0" encoding="Windows-1252"?>
 <VisualStudioProject
 	ProjectType="Visual C++"
-	Version="8,00"
+	Version="8.00"
 	Name="Enc"
 	ProjectGUID="{6D97A8EF-5724-4D85-8BF4-C583714BBA78}"
 	RootNamespace="Enc"
@@ -40,7 +40,7 @@
 			<Tool
 				Name="VCCLCompilerTool"
 				Optimization="0"
-				AdditionalIncludeDirectories="../interface;../src_SigProc_FIX"
+				AdditionalIncludeDirectories="../../../celt-0.8.0/libcelt;../interface;../src_SigProc_FIX"
 				PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE"
 				MinimalRebuild="true"
 				BasicRuntimeChecks="3"
@@ -121,7 +121,7 @@
 			/>
 			<Tool
 				Name="VCCLCompilerTool"
-				AdditionalIncludeDirectories="../interface;../src_SigProc_FIX"
+				AdditionalIncludeDirectories="../../../celt-0.8.0/libcelt;../interface;../src_SigProc_FIX"
 				PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE"
 				RuntimeLibrary="0"
 				FloatingPointModel="2"
--- a/test/Enc_SDK.vcproj.SKYPE-KOEN.koen.user
+++ /dev/null
@@ -1,65 +1,0 @@
-<?xml version="1.0" encoding="Windows-1252"?>
-<VisualStudioUserFile
-	ProjectType="Visual C++"
-	Version="8.00"
-	ShowAllFiles="false"
-	>
-	<Configurations>
-		<Configuration
-			Name="Debug|Win32"
-			>
-			<DebugSettings
-				Command="$(TargetPath)"
-				WorkingDirectory=""
-				CommandArguments=""
-				Attach="false"
-				DebuggerType="3"
-				Remote="1"
-				RemoteMachine="SKYPE-KOEN"
-				RemoteCommand=""
-				HttpUrl=""
-				PDBPath=""
-				SQLDebugging=""
-				Environment=""
-				EnvironmentMerge="true"
-				DebuggerFlavor=""
-				MPIRunCommand=""
-				MPIRunArguments=""
-				MPIRunWorkingDirectory=""
-				ApplicationCommand=""
-				ApplicationArguments=""
-				ShimCommand=""
-				MPIAcceptMode=""
-				MPIAcceptFilter=""
-			/>
-		</Configuration>
-		<Configuration
-			Name="Release|Win32"
-			>
-			<DebugSettings
-				Command="$(TargetPath)"
-				WorkingDirectory=""
-				CommandArguments=""
-				Attach="false"
-				DebuggerType="3"
-				Remote="1"
-				RemoteMachine="SKYPE-KOEN"
-				RemoteCommand=""
-				HttpUrl=""
-				PDBPath=""
-				SQLDebugging=""
-				Environment=""
-				EnvironmentMerge="true"
-				DebuggerFlavor=""
-				MPIRunCommand=""
-				MPIRunArguments=""
-				MPIRunWorkingDirectory=""
-				ApplicationCommand=""
-				ApplicationArguments=""
-				ShimCommand=""
-				MPIAcceptMode=""
-				MPIAcceptFilter=""
-			/>
-		</Configuration>
-	</Configurations>
-</VisualStudioUserFile>
--- a/test/Encoder.c
+++ b/test/Encoder.c
@@ -99,7 +99,6 @@
     SKP_int32 k, args, totPackets, totActPackets, ret;
     SKP_int16 nBytes;
     double    sumBytes, sumActBytes, avg_rate, act_rate, nrg;
-    SKP_uint8 payload[ MAX_BYTES_PER_FRAME * MAX_INPUT_FRAMES ];
     SKP_int16 in[ MAX_FRAME_LENGTH_MS * MAX_API_FS_KHZ * MAX_INPUT_FRAMES ];
     char      speechInFileName[ 150 ], bitOutFileName[ 150 ];
     FILE      *bitOutFile, *speechInFile;
@@ -111,6 +110,9 @@
 #ifdef _SYSTEM_IS_BIG_ENDIAN
     SKP_int16 nBytes_LE;
 #endif
+    SKP_uint8      range_buf[ MAX_BYTES_PER_FRAME * MAX_INPUT_FRAMES ];
+    ec_byte_buffer range_enc_celt_buf;
+    ec_enc         range_enc_celt_state;
 
     /* default settings */
     SKP_int32 API_fs_Hz = 24000;
@@ -121,6 +123,7 @@
     SKP_int32 packetLoss_perc = 0, complexity_mode = 2, smplsSinceLastPacket;
     SKP_int32 INBandFEC_enabled = 0, DTX_enabled = 0, quiet = 0;
     SKP_SILK_SDK_EncControlStruct encControl; // Struct for input to encoder
+
         
     if( argc < 3 ) {
         print_usage( argv );
@@ -235,8 +238,12 @@
     smplsSinceLastPacket = 0;
     sumBytes             = 0.0;
     sumActBytes          = 0.0;
-    
+
     while( 1 ) {
+        /* Init range coder */
+        ec_byte_writeinit_buffer( &range_enc_celt_buf, range_buf, MAX_BYTES_PER_FRAME * MAX_INPUT_FRAMES );
+        ec_enc_init( &range_enc_celt_state, &range_enc_celt_buf );
+        
         /* Read input from file */
         counter = fread( in, sizeof( SKP_int16 ), ( frameSizeReadFromFile_ms * API_fs_Hz ) / 1000, speechInFile );
 #ifdef _SYSTEM_IS_BIG_ENDIAN
@@ -251,7 +258,7 @@
 
         starttime = GetHighResolutionTime();
         /* Silk Encoder */
-        ret = SKP_Silk_SDK_Encode( psEnc, &encControl, in, (SKP_int16)counter, payload, &nBytes );
+        ret = SKP_Silk_SDK_Encode( psEnc, &encControl, in, (SKP_int16)counter, &range_enc_celt_state, &nBytes );
         if( ret ) {
             printf( "\nSKP_Silk_Encode returned %d", ret );
             break;
@@ -294,7 +301,7 @@
 #endif
 
             /* Write payload */
-            fwrite( payload, sizeof( SKP_uint8 ), nBytes, bitOutFile );
+            fwrite( range_buf, sizeof( SKP_uint8 ), nBytes, bitOutFile );
         
             if( !quiet ) {
                 fprintf( stderr, "\rPackets encoded:              %d", totPackets );
--- a/test/SignalCompare.vcproj.SKYPE-KOEN.koen.user
+++ /dev/null
@@ -1,65 +1,0 @@
-<?xml version="1.0" encoding="Windows-1252"?>
-<VisualStudioUserFile
-	ProjectType="Visual C++"
-	Version="8.00"
-	ShowAllFiles="false"
-	>
-	<Configurations>
-		<Configuration
-			Name="Debug|Win32"
-			>
-			<DebugSettings
-				Command="$(TargetPath)"
-				WorkingDirectory=""
-				CommandArguments=""
-				Attach="false"
-				DebuggerType="3"
-				Remote="1"
-				RemoteMachine="SKYPE-KOEN"
-				RemoteCommand=""
-				HttpUrl=""
-				PDBPath=""
-				SQLDebugging=""
-				Environment=""
-				EnvironmentMerge="true"
-				DebuggerFlavor=""
-				MPIRunCommand=""
-				MPIRunArguments=""
-				MPIRunWorkingDirectory=""
-				ApplicationCommand=""
-				ApplicationArguments=""
-				ShimCommand=""
-				MPIAcceptMode=""
-				MPIAcceptFilter=""
-			/>
-		</Configuration>
-		<Configuration
-			Name="Release|Win32"
-			>
-			<DebugSettings
-				Command="$(TargetPath)"
-				WorkingDirectory=""
-				CommandArguments=""
-				Attach="false"
-				DebuggerType="3"
-				Remote="1"
-				RemoteMachine="SKYPE-KOEN"
-				RemoteCommand=""
-				HttpUrl=""
-				PDBPath=""
-				SQLDebugging=""
-				Environment=""
-				EnvironmentMerge="true"
-				DebuggerFlavor=""
-				MPIRunCommand=""
-				MPIRunArguments=""
-				MPIRunWorkingDirectory=""
-				ApplicationCommand=""
-				ApplicationArguments=""
-				ShimCommand=""
-				MPIAcceptMode=""
-				MPIAcceptFilter=""
-			/>
-		</Configuration>
-	</Configurations>
-</VisualStudioUserFile>