Update SILK code using the CELT range coder
[opus.git] / src_common / SKP_Silk_structs.h
similarity index 84%
rename from src/SKP_Silk_structs.h
rename to src_common/SKP_Silk_structs.h
index ce96e22..cd6ed25 100644 (file)
@@ -31,6 +31,8 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 #include "SKP_Silk_typedef.h"\r
 #include "SKP_Silk_SigProc_FIX.h"\r
 #include "SKP_Silk_define.h"\r
+#include "entenc.h"\r
+#include "entdec.h"\r
 \r
 #ifdef __cplusplus\r
 extern "C"\r
@@ -44,7 +46,8 @@ extern "C"
 typedef struct {\r
     SKP_int16   xq[           2 * MAX_FRAME_LENGTH ]; /* Buffer for quantized output signal */\r
     SKP_int32   sLTP_shp_Q10[ 2 * MAX_FRAME_LENGTH ];\r
-    SKP_int32   sLPC_Q14[ MAX_FRAME_LENGTH / NB_SUBFR + MAX_LPC_ORDER ];\r
+    SKP_int32   sLPC_Q14[ MAX_FRAME_LENGTH / MAX_NB_SUBFR + MAX_LPC_ORDER ];\r
+    SKP_int32   sAR2_Q14[ MAX_SHAPE_LPC_ORDER ];\r
     SKP_int32   sLF_AR_shp_Q12;\r
     SKP_int     lagPrev;\r
     SKP_int     sLTP_buf_idx;\r
@@ -87,6 +90,9 @@ typedef struct {
     SKP_uint32  range_Q16;\r
     SKP_int32   error;\r
     SKP_uint8   buffer[ MAX_ARITHM_BYTES ];     /* Buffer containing payload                                */\r
+    /* The CELT entropy encoder + decoder */\r
+    ec_enc      range_enc_celt_state;\r
+    ec_dec      range_dec_celt_state;\r
 } SKP_Silk_range_coder_state;\r
 \r
 /* Input frequency range detection struct */\r
@@ -147,10 +153,15 @@ typedef struct {
     SKP_int                         typeOffsetPrev;                 /* Previous signal type and quantization offset                         */\r
     SKP_int                         prevLag;\r
     SKP_int                         prev_lagIndex;\r
-    SKP_int                         fs_kHz;                         /* Sampling frequency (kHz)                                             */\r
+    SKP_int32                       API_fs_Hz;                      /* API sampling frequency (Hz)                                          */\r
+    SKP_int32                       prev_API_fs_Hz;                 /* Previous API sampling frequency (Hz)                                 */\r
+    SKP_int                         maxInternal_fs_kHz;             /* Maximum internal sampling frequency (kHz)                            */\r
+    SKP_int                         fs_kHz;                         /* Internal sampling frequency (kHz)                                    */\r
     SKP_int                         fs_kHz_changed;                 /* Did we switch yet?                                                   */\r
+    SKP_int                         nb_subfr;                       /* Number of 5 ms subframes in a frame                                  */\r
     SKP_int                         frame_length;                   /* Frame length (samples)                                               */\r
     SKP_int                         subfr_length;                   /* Subframe length (samples)                                            */\r
+    SKP_int                         ltp_mem_length;                 /* Length of LTP memory                                                 */\r
     SKP_int                         la_pitch;                       /* Look-ahead for pitch analysis (samples)                              */\r
     SKP_int                         la_shape;                       /* Look-ahead for noise shape analysis (samples)                        */\r
     SKP_int32                       TargetRate_bps;                 /* Target bitrate (bps)                                                 */\r
@@ -182,28 +193,16 @@ typedef struct {
     /* Struct for Inband LBRR */ \r
     SKP_SILK_LBRR_struct            LBRR_buffer[ MAX_LBRR_DELAY ];\r
     SKP_int                         oldest_LBRR_idx;\r
+    SKP_int                         useInBandFEC;                   /* Saves the API setting for query                                      */\r
     SKP_int                         LBRR_enabled;\r
     SKP_int                         LBRR_GainIncreases;             /* Number of shifts to Gains to get LBRR rate Voiced frames             */\r
 \r
     /* Bitrate control */\r
-    SKP_int32                       bitrateDiff;                    /* accumulated diff. between the target bitrate and the SWB/WB limits   */\r
+    SKP_int32                       bitrateDiff;                    /* Accumulated diff. between the target bitrate and the switch bitrates */\r
+    SKP_int32                       bitrate_threshold_up;           /* Threshold for switching to a higher internal sample frequency        */\r
+    SKP_int32                       bitrate_threshold_down;         /* Threshold for switching to a lower internal sample frequency         */\r
 \r
-#if LOW_COMPLEXITY_ONLY\r
-    /* state for downsampling from 24 to 16 kHz in low complexity mode */\r
-    SKP_int16                       resample24To16state[ SigProc_Resample_2_3_coarse_NUM_FIR_COEFS - 1 ];\r
-#else\r
-    SKP_int32                       resample24To16state[ 11 ];      /* state for downsampling from 24 to 16 kHz                             */\r
-#endif\r
-    SKP_int32                       resample24To12state[ 6 ];       /* state for downsampling from 24 to 12 kHz                             */\r
-    SKP_int32                       resample24To8state[ 7 ];        /* state for downsampling from 24 to  8 kHz                             */\r
-    SKP_int32                       resample16To12state[ 15 ];      /* state for downsampling from 16 to 12 kHz                             */\r
-    SKP_int32                       resample16To8state[ 6 ];        /* state for downsampling from 16 to  8 kHz                             */\r
-#if LOW_COMPLEXITY_ONLY\r
-    /* state for downsampling from 12 to 8 kHz in low complexity mode */\r
-    SKP_int16                       resample12To8state[ SigProc_Resample_2_3_coarse_NUM_FIR_COEFS - 1 ];    \r
-#else\r
-    SKP_int32                       resample12To8state[ 11 ];       /* state for downsampling from 12 to  8 kHz                             */\r
-#endif\r
+    SKP_Silk_resampler_state_struct  resampler_state;\r
 \r
     /* DTX */\r
     SKP_int                         noSpeechCounter;                /* Counts concecutive nonactive frames, used by DTX                     */\r
@@ -215,15 +214,11 @@ typedef struct {
     SKP_Silk_detect_SWB_state       sSWBdetect;\r
 \r
 \r
-    /********************************************/\r
-    /* Buffers etc used by the new bitstream V4 */\r
-    /********************************************/\r
-    SKP_int                         q[ MAX_FRAME_LENGTH * MAX_FRAMES_PER_PACKET ];      /* pulse signal buffer */\r
-    SKP_int                         q_LBRR[ MAX_FRAME_LENGTH * MAX_FRAMES_PER_PACKET ]; /* pulse signal buffer */\r
+    /* Buffers */\r
+       SKP_int8                        q[ MAX_FRAME_LENGTH * MAX_FRAMES_PER_PACKET ];      /* pulse signal buffer */\r
+    SKP_int8                        q_LBRR[ MAX_FRAME_LENGTH * MAX_FRAMES_PER_PACKET ]; /* pulse signal buffer */\r
     SKP_int                         sigtype[ MAX_FRAMES_PER_PACKET ];\r
     SKP_int                         QuantOffsetType[ MAX_FRAMES_PER_PACKET ];\r
-    SKP_int                         extension_layer;                                    /* Add extension layer      */\r
-    SKP_int                         bitstream_v;                                        /* Holds bitstream version  */\r
 } SKP_Silk_encoder_state;\r
 \r
 \r
@@ -235,10 +230,11 @@ typedef struct {
     SKP_int     lagIndex;\r
     SKP_int     contourIndex;\r
     SKP_int     PERIndex;\r
-    SKP_int     LTPIndex[ NB_SUBFR ];\r
+    SKP_int     LTPIndex[ MAX_NB_SUBFR ];\r
     SKP_int     NLSFIndices[ NLSF_MSVQ_MAX_CB_STAGES ];  /* NLSF path of quantized LSF vector   */\r
     SKP_int     NLSFInterpCoef_Q2;\r
-    SKP_int     GainsIndices[ NB_SUBFR ];\r
+    SKP_int     GainsIndices[ MAX_NB_SUBFR ];\r
+       SKP_int     warping_Q16;\r
     SKP_int32   Seed;\r
     SKP_int     LTP_scaleIndex;\r
     SKP_int     RateLevelIndex;\r
@@ -246,13 +242,14 @@ typedef struct {
     SKP_int     sigtype;\r
 \r
     /* Prediction and coding parameters */\r
-    SKP_int     pitchL[ NB_SUBFR ];\r
+    SKP_int     pitchL[ MAX_NB_SUBFR ];\r
 \r
     SKP_int     LBRR_usage;                     /* Low bitrate redundancy usage                             */\r
 } SKP_Silk_encoder_control;\r
 \r
 /* Struct for Packet Loss Concealment */\r
 typedef struct {\r
+    SKP_int     pitchL[ MAX_NB_SUBFR ];         /* Pitch lag per 5 ms for last 20 ms                        */\r
     SKP_int32   pitchL_Q8;                      /* Pitch lag to use for voiced concealment                  */\r
     SKP_int16   LTPCoef_Q14[ LTP_ORDER ];       /* LTP coeficients to use for voiced concealment            */\r
     SKP_int16   prevLPC_Q12[ MAX_LPC_ORDER ];\r
@@ -262,7 +259,7 @@ typedef struct {
     SKP_int32   conc_energy;\r
     SKP_int     conc_energy_shift;\r
     SKP_int16   prevLTP_scale_Q14;\r
-    SKP_int32   prevGain_Q16[ NB_SUBFR ];\r
+    SKP_int32   prevGain_Q16[ MAX_NB_SUBFR ];\r
     SKP_int     fs_kHz;\r
 } SKP_Silk_PLC_struct;\r
 \r
@@ -283,7 +280,7 @@ typedef struct {
     SKP_Silk_range_coder_state  sRC;                            /* Range coder state                                                    */\r
     SKP_int32       prev_inv_gain_Q16;\r
     SKP_int32       sLTP_Q16[ 2 * MAX_FRAME_LENGTH ];\r
-    SKP_int32       sLPC_Q14[ MAX_FRAME_LENGTH / NB_SUBFR + MAX_LPC_ORDER ];\r
+    SKP_int32       sLPC_Q14[ MAX_FRAME_LENGTH / MAX_NB_SUBFR + MAX_LPC_ORDER ];\r
     SKP_int32       exc_Q10[ MAX_FRAME_LENGTH ];\r
     SKP_int32       res_Q10[ MAX_FRAME_LENGTH ];\r
     SKP_int16       outBuf[ 2 * MAX_FRAME_LENGTH ];             /* Buffer for output signal                                             */\r
@@ -296,8 +293,11 @@ typedef struct {
     const SKP_int16 *HP_A;                                      /* HP filter AR coefficients                                            */\r
     const SKP_int16 *HP_B;                                      /* HP filter MA coefficients                                            */\r
     SKP_int         fs_kHz;                                     /* Sampling frequency in kHz                                            */\r
+    SKP_int32       prev_API_sampleRate;                        /* Previous API sample frequency (Hz)                                   */\r
+    SKP_int         nb_subfr;                                   /* Number of 5 ms subframes in a frame                                  */\r
     SKP_int         frame_length;                               /* Frame length (samples)                                               */\r
     SKP_int         subfr_length;                               /* Subframe length (samples)                                            */\r
+    SKP_int         ltp_mem_length;                             /* Length of LTP memory                                                 */\r
     SKP_int         LPC_order;                                  /* LPC order                                                            */\r
     SKP_int         prevNLSF_Q15[ MAX_LPC_ORDER ];              /* Used to interpolate LSFs                                             */\r
     SKP_int         first_frame_after_reset;                    /* Flag for deactivating NLSF interp. and fluc. reduction after resets  */\r
@@ -309,20 +309,20 @@ typedef struct {
     SKP_int         moreInternalDecoderFrames;\r
     SKP_int         FrameTermination;\r
 \r
-    SKP_int32       resampleState[ 15 ];\r
+    SKP_Silk_resampler_state_struct  resampler_state;\r
 \r
     const SKP_Silk_NLSF_CB_struct *psNLSF_CB[ 2 ];      /* Pointers to voiced/unvoiced NLSF codebooks */\r
 \r
     SKP_int         sigtype[               MAX_FRAMES_PER_PACKET ];\r
     SKP_int         QuantOffsetType[       MAX_FRAMES_PER_PACKET ];\r
-    SKP_int         GainsIndices[          MAX_FRAMES_PER_PACKET ][ NB_SUBFR ];\r
-    SKP_int         GainsIndices_EnhLayer[ MAX_FRAMES_PER_PACKET ][ NB_SUBFR ];\r
+    SKP_int         GainsIndices[          MAX_FRAMES_PER_PACKET ][ MAX_NB_SUBFR ];\r
+    SKP_int         GainsIndices_EnhLayer[ MAX_FRAMES_PER_PACKET ][ MAX_NB_SUBFR ];\r
     SKP_int         NLSFIndices[           MAX_FRAMES_PER_PACKET ][ NLSF_MSVQ_MAX_CB_STAGES ];\r
     SKP_int         NLSFInterpCoef_Q2[     MAX_FRAMES_PER_PACKET ];\r
     SKP_int         lagIndex[              MAX_FRAMES_PER_PACKET ];\r
     SKP_int         contourIndex[          MAX_FRAMES_PER_PACKET ];\r
     SKP_int         PERIndex[              MAX_FRAMES_PER_PACKET ];\r
-    SKP_int         LTPIndex[              MAX_FRAMES_PER_PACKET ][ NB_SUBFR ];\r
+    SKP_int         LTPIndex[              MAX_FRAMES_PER_PACKET ][ MAX_NB_SUBFR ];\r
     SKP_int         LTP_scaleIndex[        MAX_FRAMES_PER_PACKET ];\r
     SKP_int         Seed[                  MAX_FRAMES_PER_PACKET ];\r
     SKP_int         vadFlagBuf[            MAX_FRAMES_PER_PACKET ];\r
@@ -336,21 +336,11 @@ typedef struct {
     SKP_Silk_CNG_struct sCNG;\r
 \r
     /* Stuff used for PLC */\r
-    SKP_Silk_PLC_struct sPLC;\r
     SKP_int         lossCnt;\r
     SKP_int         prev_sigtype;                               /* Previous sigtype                                                     */\r
-#ifdef USE_INTERPOLATION_PLC\r
-    SKP_int16       prevQuant[ MAX_FRAME_LENGTH ];\r
-    SKP_int         prevPitchL[ NB_SUBFR ];                     /* Previous Lags used                                                   */\r
-    SKP_int16       prevLTPCoef_Q14[ NB_SUBFR * LTP_ORDER ];    /* Previous LTCoefs used                                                */\r
-    SKP_int16       prevAR_Q12[ MAX_LPC_ORDER ];\r
-    SKP_int         interpolDistance;                           /* Number of frames between old and new recieved packet                 */\r
-#endif\r
-\r
-\r
 \r
+    SKP_Silk_PLC_struct sPLC;\r
 \r
-    SKP_int                 bitstream_v;                        /* Holds bitstream version                                              */\r
 } SKP_Silk_decoder_state;\r
 \r
 /************************/\r
@@ -358,12 +348,12 @@ typedef struct {
 /************************/\r
 typedef struct {\r
     /* prediction and coding parameters */\r
-    SKP_int             pitchL[ NB_SUBFR ];\r
-    SKP_int32           Gains_Q16[ NB_SUBFR ];\r
+    SKP_int             pitchL[ MAX_NB_SUBFR ];\r
+    SKP_int32           Gains_Q16[ MAX_NB_SUBFR ];\r
     SKP_int32           Seed;\r
     /* holds interpolated and final coefficients, 4-byte aligned */\r
     SKP_array_of_int16_4_byte_aligned( PredCoef_Q12[ 2 ], MAX_LPC_ORDER );\r
-    SKP_int16           LTPCoef_Q14[ LTP_ORDER * NB_SUBFR ];\r
+    SKP_int16           LTPCoef_Q14[ LTP_ORDER * MAX_NB_SUBFR ];\r
     SKP_int             LTP_scale_Q14;\r
 \r
     /* quantization indices */\r