shithub: opus

Download patch

ref: bafbd08db132d387bcef0637258f90e862a8e4ea
parent: e335065a1ba72c474b2bf9324e789d5e1dc1e884
parent: ec2802210cd0d62514f4cb9bc4db2f7df1259ad4
author: Jean-Marc Valin <[email protected]>
date: Thu Sep 1 17:59:50 EDT 2011

Merge branch 'exp-highpass'

--- a/silk/silk_HP_variable_cutoff.c
+++ b/silk/silk_HP_variable_cutoff.c
@@ -41,85 +41,38 @@
     const opus_int                   nChannels           /* I    Number of channels                      */
 )
 {
-    opus_int   quality_Q15, cutoff_Hz;
-    opus_int32 B_Q28[ 3 ], A_Q28[ 2 ];
-    opus_int32 Fc_Q19, r_Q28, r_Q22;
-    opus_int32 pitch_freq_Hz_Q16, pitch_freq_log_Q7, delta_freq_Q7;
-    silk_encoder_state *psEncC1 = &state_Fxx[ 0 ].sCmn;
+   opus_int   quality_Q15;
+   opus_int32 pitch_freq_Hz_Q16, pitch_freq_log_Q7, delta_freq_Q7;
+   silk_encoder_state *psEncC1 = &state_Fxx[ 0 ].sCmn;
 
-    if( psEncC1->HP_cutoff_Hz == 0 ) {
-        /* Adaptive cutoff frequency: estimate low end of pitch frequency range */
-        if( psEncC1->prevSignalType == TYPE_VOICED ) {
-            /* difference, in log domain */
-            pitch_freq_Hz_Q16 = SKP_DIV32_16( SKP_LSHIFT( SKP_MUL( psEncC1->fs_kHz, 1000 ), 16 ), psEncC1->prevLag );
-            pitch_freq_log_Q7 = silk_lin2log( pitch_freq_Hz_Q16 ) - ( 16 << 7 );
+   /* Adaptive cutoff frequency: estimate low end of pitch frequency range */
+   if( psEncC1->prevSignalType == TYPE_VOICED ) {
+      /* difference, in log domain */
+      pitch_freq_Hz_Q16 = SKP_DIV32_16( SKP_LSHIFT( SKP_MUL( psEncC1->fs_kHz, 1000 ), 16 ), psEncC1->prevLag );
+      pitch_freq_log_Q7 = silk_lin2log( pitch_freq_Hz_Q16 ) - ( 16 << 7 );
 
-            /* adjustment based on quality */
-            quality_Q15 = psEncC1->input_quality_bands_Q15[ 0 ];
-            pitch_freq_log_Q7 = SKP_SMLAWB( pitch_freq_log_Q7, SKP_SMULWB( SKP_LSHIFT( -quality_Q15, 2 ), quality_Q15 ),
-                pitch_freq_log_Q7 - ( silk_lin2log( SILK_FIX_CONST( VARIABLE_HP_MIN_CUTOFF_HZ, 16 ) ) - ( 16 << 7 ) ) );
+      /* adjustment based on quality */
+      quality_Q15 = psEncC1->input_quality_bands_Q15[ 0 ];
+      pitch_freq_log_Q7 = SKP_SMLAWB( pitch_freq_log_Q7, SKP_SMULWB( SKP_LSHIFT( -quality_Q15, 2 ), quality_Q15 ),
+            pitch_freq_log_Q7 - ( silk_lin2log( SILK_FIX_CONST( VARIABLE_HP_MIN_CUTOFF_HZ, 16 ) ) - ( 16 << 7 ) ) );
 
-            /* delta_freq = pitch_freq_log - psEnc->variable_HP_smth1; */
-            delta_freq_Q7 = pitch_freq_log_Q7 - SKP_RSHIFT( psEncC1->variable_HP_smth1_Q15, 8 );
-            if( delta_freq_Q7 < 0 ) {
-                /* less smoothing for decreasing pitch frequency, to track something close to the minimum */
-                delta_freq_Q7 = SKP_MUL( delta_freq_Q7, 3 );
-            }
+      /* delta_freq = pitch_freq_log - psEnc->variable_HP_smth1; */
+      delta_freq_Q7 = pitch_freq_log_Q7 - SKP_RSHIFT( psEncC1->variable_HP_smth1_Q15, 8 );
+      if( delta_freq_Q7 < 0 ) {
+         /* less smoothing for decreasing pitch frequency, to track something close to the minimum */
+         delta_freq_Q7 = SKP_MUL( delta_freq_Q7, 3 );
+      }
 
-            /* limit delta, to reduce impact of outliers in pitch estimation */
-            delta_freq_Q7 = SKP_LIMIT_32( delta_freq_Q7, -SILK_FIX_CONST( VARIABLE_HP_MAX_DELTA_FREQ, 7 ), SILK_FIX_CONST( VARIABLE_HP_MAX_DELTA_FREQ, 7 ) );
+      /* limit delta, to reduce impact of outliers in pitch estimation */
+      delta_freq_Q7 = SKP_LIMIT_32( delta_freq_Q7, -SILK_FIX_CONST( VARIABLE_HP_MAX_DELTA_FREQ, 7 ), SILK_FIX_CONST( VARIABLE_HP_MAX_DELTA_FREQ, 7 ) );
 
-            /* update smoother */
-            psEncC1->variable_HP_smth1_Q15 = SKP_SMLAWB( psEncC1->variable_HP_smth1_Q15,
-                SKP_SMULBB( psEncC1->speech_activity_Q8, delta_freq_Q7 ), SILK_FIX_CONST( VARIABLE_HP_SMTH_COEF1, 16 ) );
+      /* update smoother */
+      psEncC1->variable_HP_smth1_Q15 = SKP_SMLAWB( psEncC1->variable_HP_smth1_Q15,
+            SKP_SMULBB( psEncC1->speech_activity_Q8, delta_freq_Q7 ), SILK_FIX_CONST( VARIABLE_HP_SMTH_COEF1, 16 ) );
 
-            /* limit frequency range */
-            psEncC1->variable_HP_smth1_Q15 = SKP_LIMIT_32( psEncC1->variable_HP_smth1_Q15,
-                SKP_LSHIFT( silk_lin2log( VARIABLE_HP_MIN_CUTOFF_HZ ), 8 ),
-                SKP_LSHIFT( silk_lin2log( VARIABLE_HP_MAX_CUTOFF_HZ ), 8 ) );
-        }
-    } else {
-        /* Externally-controlled cutoff frequency */
-        cutoff_Hz = SKP_LIMIT( psEncC1->HP_cutoff_Hz, 10, 500 );
-        psEncC1->variable_HP_smth1_Q15 = SKP_LSHIFT( silk_lin2log( cutoff_Hz ), 8 );
-    }
-
-    /* second smoother */
-    psEncC1->variable_HP_smth2_Q15 = SKP_SMLAWB( psEncC1->variable_HP_smth2_Q15,
-        psEncC1->variable_HP_smth1_Q15 - psEncC1->variable_HP_smth2_Q15, SILK_FIX_CONST( VARIABLE_HP_SMTH_COEF2, 16 ) );
-
-    /* convert from log scale to Hertz */
-    cutoff_Hz = silk_log2lin( SKP_RSHIFT( psEncC1->variable_HP_smth2_Q15, 8 ) );
-
-    /********************************/
-    /* Compute Filter Coefficients  */
-    /********************************/
-    /* compute cut-off frequency, in radians    */
-    /* Fc_num   = 1.5 * 3.14159 * cutoff_Hz     */
-    /* Fc_denom = 1e3f * psEncC1->fs_kHz        */
-    SKP_assert( cutoff_Hz <= SKP_int32_MAX / SILK_FIX_CONST( 1.5 * 3.14159 / 1000, 19 ) );
-    Fc_Q19 = SKP_DIV32_16( SKP_SMULBB( SILK_FIX_CONST( 1.5 * 3.14159 / 1000, 19 ), cutoff_Hz ), psEncC1->fs_kHz );
-    SKP_assert( Fc_Q19 > 0 && Fc_Q19 < 32768 );
-
-    r_Q28 = SILK_FIX_CONST( 1.0, 28 ) - SKP_MUL( SILK_FIX_CONST( 0.92, 9 ), Fc_Q19 );
-
-    /* b = r * [ 1; -2; 1 ]; */
-    /* a = [ 1; -2 * r * ( 1 - 0.5 * Fc^2 ); r^2 ]; */
-    B_Q28[ 0 ] = r_Q28;
-    B_Q28[ 1 ] = SKP_LSHIFT( -r_Q28, 1 );
-    B_Q28[ 2 ] = r_Q28;
-
-    /* -r * ( 2 - Fc * Fc ); */
-    r_Q22  = SKP_RSHIFT( r_Q28, 6 );
-    A_Q28[ 0 ] = SKP_SMULWW( r_Q22, SKP_SMULWW( Fc_Q19, Fc_Q19 ) - SILK_FIX_CONST( 2.0,  22 ) );
-    A_Q28[ 1 ] = SKP_SMULWW( r_Q22, r_Q22 );
-
-    /********************************/
-    /* High-Pass Filter             */
-    /********************************/
-    silk_biquad_alt( psEncC1->inputBuf, B_Q28, A_Q28, psEncC1->In_HP_State, psEncC1->inputBuf, psEncC1->frame_length );
-    if( nChannels == 2 ) {
-        silk_biquad_alt( state_Fxx[ 1 ].sCmn.inputBuf, B_Q28, A_Q28, state_Fxx[ 1 ].sCmn.In_HP_State,
-            state_Fxx[ 1 ].sCmn.inputBuf, state_Fxx[ 1 ].sCmn.frame_length );
-    }
+      /* limit frequency range */
+      psEncC1->variable_HP_smth1_Q15 = SKP_LIMIT_32( psEncC1->variable_HP_smth1_Q15,
+            SKP_LSHIFT( silk_lin2log( VARIABLE_HP_MIN_CUTOFF_HZ ), 8 ),
+            SKP_LSHIFT( silk_lin2log( VARIABLE_HP_MAX_CUTOFF_HZ ), 8 ) );
+   }
 }
--- a/silk/silk_LP_variable_cutoff.c
+++ b/silk/silk_LP_variable_cutoff.c
@@ -131,6 +131,6 @@
 
         /* ARMA low-pass filtering */
         SKP_assert( TRANSITION_NB == 3 && TRANSITION_NA == 2 );
-        silk_biquad_alt( frame, B_Q28, A_Q28, psLP->In_LP_State, frame, frame_length );
+        silk_biquad_alt( frame, B_Q28, A_Q28, psLP->In_LP_State, frame, frame_length, 1);
     }
 }
--- a/silk/silk_SigProc_FIX.h
+++ b/silk/silk_SigProc_FIX.h
@@ -126,7 +126,8 @@
     const opus_int32     *A_Q28,        /* I:    AR coefficients [2]          */
     opus_int32           *S,            /* I/O:  State vector [2]             */
     opus_int16           *out,          /* O:    output signal                */
-    const opus_int32     len            /* I:    signal length (must be even) */
+    const opus_int32     len,           /* I:    signal length (must be even) */
+    int stride
 );
 
 /* Variable order MA prediction error filter. */
--- a/silk/silk_biquad_alt.c
+++ b/silk/silk_biquad_alt.c
@@ -46,7 +46,8 @@
     const opus_int32      *A_Q28,         /* I:    AR coefficients [2]            */
     opus_int32            *S,             /* I/O:  State vector [2]               */
     opus_int16            *out,           /* O:    Output signal                  */
-    const opus_int32      len             /* I:    Signal length (must be even)   */
+    const opus_int32      len,            /* I:    Signal length (must be even)   */
+    int stride
 )
 {
     /* DIRECT FORM II TRANSPOSED (uses 2 element state vector) */
@@ -61,7 +62,7 @@
 
     for( k = 0; k < len; k++ ) {
         /* S[ 0 ], S[ 1 ]: Q12 */
-        inval = in[ k ];
+        inval = in[ k*stride ];
         out32_Q14 = SKP_LSHIFT( SKP_SMLAWB( S[ 0 ], B_Q28[ 0 ], inval ), 2 );
 
         S[ 0 ] = S[1] + SKP_RSHIFT_ROUND( SKP_SMULWB( out32_Q14, A0_L_Q28 ), 14 );
@@ -73,6 +74,6 @@
         S[ 1 ] = SKP_SMLAWB( S[ 1 ], B_Q28[ 2 ], inval );
 
         /* Scale back to Q0 and saturate */
-        out[ k ] = (opus_int16)SKP_SAT16( SKP_RSHIFT( out32_Q14 + (1<<14) - 1, 14 ) );
+        out[ k*stride ] = (opus_int16)SKP_SAT16( SKP_RSHIFT( out32_Q14 + (1<<14) - 1, 14 ) );
     }
 }
--- a/silk/silk_control.h
+++ b/silk/silk_control.h
@@ -83,9 +83,6 @@
     /* I:   Flag to use constant bitrate                                                    */
     opus_int useCBR;
 
-    /* I:   Cutoff frequency of input HP filter (of zero: adaptive)                         */
-    opus_int HP_cutoff_Hz;
-
     /* O:   Internal sampling rate used, in Hertz; 8000/12000/16000                         */
     opus_int32 internalSampleRate;
 
--- a/silk/silk_enc_API.c
+++ b/silk/silk_enc_API.c
@@ -112,7 +112,6 @@
     encStatus->useInBandFEC              = state_Fxx[ 0 ].sCmn.useInBandFEC;
     encStatus->useDTX                    = state_Fxx[ 0 ].sCmn.useDTX;
     encStatus->useCBR                    = state_Fxx[ 0 ].sCmn.useCBR;
-    encStatus->HP_cutoff_Hz              = state_Fxx[ 0 ].sCmn.HP_cutoff_Hz;
     encStatus->internalSampleRate        = SKP_SMULBB( state_Fxx[ 0 ].sCmn.fs_kHz, 1000 );
     encStatus->allowBandwidthSwitch      = state_Fxx[ 0 ].sCmn.allow_bandwidth_switch;
     encStatus->inWBmodeWithoutVariableLP = state_Fxx[ 0 ].sCmn.fs_kHz == 16 && state_Fxx[ 0 ].sCmn.sLP.mode == 0;
@@ -302,11 +301,7 @@
                 }
             }
 
-            /* High-pass filter, deactivated if less than zero */
-            if(encControl->HP_cutoff_Hz>=0) {
-                psEnc->state_Fxx[ 0 ].sCmn.HP_cutoff_Hz = encControl->HP_cutoff_Hz;
-                silk_HP_variable_cutoff( psEnc->state_Fxx, psEnc->nChannelsInternal );
-            }
+            silk_HP_variable_cutoff( psEnc->state_Fxx, psEnc->nChannelsInternal );
 
             /* Total target bits for packet */
             nBits = SKP_DIV32_16( SKP_MUL( encControl->bitRate, encControl->payloadSize_ms ), 1000 );
--- a/silk/silk_structs.h
+++ b/silk/silk_structs.h
@@ -132,7 +132,6 @@
     opus_int32                       In_HP_State[ 2 ];               /* High pass filter state                                               */
     opus_int32                       variable_HP_smth1_Q15;          /* State of first smoother                                              */
     opus_int32                       variable_HP_smth2_Q15;          /* State of second smoother                                             */
-    opus_int                         HP_cutoff_Hz;                   /* Fixed cutoff frequency (if zero: adaptive)                           */
     silk_LP_state                   sLP;                            /* Low pass filter state                                                */
     silk_VAD_state                  sVAD;                           /* Voice activity detector state                                        */
     silk_nsq_state                  sNSQ;                           /* Noise Shape Quantizer State                                          */
--- a/src/opus_encoder.c
+++ b/src/opus_encoder.c
@@ -41,6 +41,13 @@
 #include "opus_private.h"
 #include "os_support.h"
 
+#include "silk_tuning_parameters.h"
+#ifdef FIXED_POINT
+#include "fixed/silk_structs_FIX.h"
+#else
+#include "float/silk_structs_FLP.h"
+#endif
+
 #define MAX_ENCODER_BUFFER 480
 
 struct OpusEncoder {
@@ -64,6 +71,8 @@
 #define OPUS_ENCODER_RESET_START stream_channels
     int          stream_channels;
     int          hybrid_stereo_width_Q14;
+    opus_int32   variable_HP_smth2_Q15;
+    opus_val32   hp_mem[4];
     int          mode;
     int          prev_mode;
     int          bandwidth;
@@ -150,7 +159,6 @@
     st->silk_mode.useInBandFEC              = 0;
     st->silk_mode.useDTX                    = 0;
     st->silk_mode.useCBR                    = 0;
-    st->silk_mode.HP_cutoff_Hz              = 0;
 
     /* Create CELT encoder */
     /* Initialize CELT encoder */
@@ -179,6 +187,7 @@
        st->delay_compensation += 2;
 
     st->hybrid_stereo_width_Q14             = 1 << 14;
+    st->variable_HP_smth2_Q15 = SKP_LSHIFT( silk_lin2log( VARIABLE_HP_MIN_CUTOFF_HZ ), 8 );
     st->first = 1;
     st->mode = MODE_HYBRID;
     st->bandwidth = OPUS_BANDWIDTH_FULLBAND;
@@ -221,6 +230,82 @@
    toc |= (channels==2)<<2;
    return toc;
 }
+
+#ifndef FIXED_POINT
+void silk_biquad_float(
+    const opus_val16      *in,            /* I:    Input signal                   */
+    const opus_int32      *B_Q28,         /* I:    MA coefficients [3]            */
+    const opus_int32      *A_Q28,         /* I:    AR coefficients [2]            */
+    opus_val32            *S,             /* I/O:  State vector [2]               */
+    opus_val16            *out,           /* O:    Output signal                  */
+    const opus_int32      len,            /* I:    Signal length (must be even)   */
+    int stride
+)
+{
+    /* DIRECT FORM II TRANSPOSED (uses 2 element state vector) */
+    opus_int   k;
+    opus_val32 vout;
+    opus_val32 inval;
+    opus_val32 A[2], B[3];
+
+    A[0] = A_Q28[0] * (1./((opus_int32)1<<28));
+    A[1] = A_Q28[1] * (1./((opus_int32)1<<28));
+    B[0] = B_Q28[0] * (1./((opus_int32)1<<28));
+    B[1] = B_Q28[1] * (1./((opus_int32)1<<28));
+    B[2] = B_Q28[2] * (1./((opus_int32)1<<28));
+
+    /* Negate A_Q28 values and split in two parts */
+
+    for( k = 0; k < len; k++ ) {
+        /* S[ 0 ], S[ 1 ]: Q12 */
+        inval = in[ k*stride ];
+        vout = S[ 0 ] + B[0]*inval;
+
+        S[ 0 ] = S[1] - vout*A[0] + B[1]*inval;
+
+        S[ 1 ] = - vout*A[1] + B[2]*inval;
+
+        /* Scale back to Q0 and saturate */
+        out[ k*stride ] = vout;
+    }
+}
+#endif
+
+static void hp_cutoff(const opus_val16 *in, opus_int32 cutoff_Hz, opus_val16 *out, opus_val32 *hp_mem, int len, int channels, opus_int32 Fs)
+{
+   opus_int32 B_Q28[ 3 ], A_Q28[ 2 ];
+   opus_int32 Fc_Q19, r_Q28, r_Q22;
+
+   SKP_assert( cutoff_Hz <= SKP_int32_MAX / SILK_FIX_CONST( 1.5 * 3.14159 / 1000, 19 ) );
+   Fc_Q19 = SKP_DIV32_16( SKP_SMULBB( SILK_FIX_CONST( 1.5 * 3.14159 / 1000, 19 ), cutoff_Hz ), Fs/1000 );
+   SKP_assert( Fc_Q19 > 0 && Fc_Q19 < 32768 );
+
+   r_Q28 = SILK_FIX_CONST( 1.0, 28 ) - SKP_MUL( SILK_FIX_CONST( 0.92, 9 ), Fc_Q19 );
+
+   /* b = r * [ 1; -2; 1 ]; */
+   /* a = [ 1; -2 * r * ( 1 - 0.5 * Fc^2 ); r^2 ]; */
+   B_Q28[ 0 ] = r_Q28;
+   B_Q28[ 1 ] = SKP_LSHIFT( -r_Q28, 1 );
+   B_Q28[ 2 ] = r_Q28;
+
+   /* -r * ( 2 - Fc * Fc ); */
+   r_Q22  = SKP_RSHIFT( r_Q28, 6 );
+   A_Q28[ 0 ] = SKP_SMULWW( r_Q22, SKP_SMULWW( Fc_Q19, Fc_Q19 ) - SILK_FIX_CONST( 2.0,  22 ) );
+   A_Q28[ 1 ] = SKP_SMULWW( r_Q22, r_Q22 );
+
+#ifdef FIXED_POINT
+   silk_biquad_alt( in, B_Q28, A_Q28, hp_mem, out, len, channels );
+   if( channels == 2 ) {
+       silk_biquad_alt( in+1, B_Q28, A_Q28, hp_mem+2, out+1, len, channels );
+   }
+#else
+   silk_biquad_float( in, B_Q28, A_Q28, hp_mem, out, len, channels );
+   if( channels == 2 ) {
+       silk_biquad_float( in+1, B_Q28, A_Q28, hp_mem+2, out+1, len, channels );
+   }
+#endif
+}
+
 OpusEncoder *opus_encoder_create(int Fs, int channels, int mode, int *error)
 {
    int ret;
@@ -267,6 +352,7 @@
     int to_celt = 0;
     opus_int32 mono_rate;
     opus_uint32 redundant_rng = 0;
+    int cutoff_Hz, hp_freq_smth1;
     ALLOC_STACK;
 
     st->rangeFinal = 0;
@@ -338,7 +424,6 @@
         opus_int32 threshold;
         threshold = 20000;
         /* OPUS_APPLICATION_VOIP default to auto high-pass */
-        st->silk_mode.HP_cutoff_Hz=0;
         /* Hysteresis */
         if (st->prev_mode == MODE_CELT_ONLY)
             threshold -= 4000;
@@ -355,7 +440,6 @@
         /* SILK/CELT threshold is higher for voice than for music */
         threshold = 36000;
         /* OPUS_APPLICATION_AUDIO disables the high-pass */
-        st->silk_mode.HP_cutoff_Hz=-1;
         if (st->signal_type == OPUS_SIGNAL_MUSIC)
             threshold -= 20000;
         else if (st->signal_type == OPUS_SIGNAL_VOICE)
@@ -467,6 +551,29 @@
 
     ec_enc_init(&enc, data, max_data_bytes-1);
 
+    ALLOC(pcm_buf, (st->delay_compensation+frame_size)*st->channels, opus_val16);
+    for (i=0;i<st->delay_compensation*st->channels;i++)
+       pcm_buf[i] = st->delay_buffer[(st->encoder_buffer-st->delay_compensation)*st->channels+i];
+
+    if (st->mode == MODE_CELT_ONLY)
+       hp_freq_smth1 = SKP_LSHIFT( silk_lin2log( VARIABLE_HP_MIN_CUTOFF_HZ ), 8 );
+    else
+       hp_freq_smth1 = ((silk_encoder*)silk_enc)->state_Fxx[0].sCmn.variable_HP_smth1_Q15;
+
+    st->variable_HP_smth2_Q15 = SKP_SMLAWB( st->variable_HP_smth2_Q15,
+          hp_freq_smth1 - st->variable_HP_smth2_Q15, SILK_FIX_CONST( VARIABLE_HP_SMTH_COEF2, 16 ) );
+
+    /* convert from log scale to Hertz */
+    cutoff_Hz = silk_log2lin( SKP_RSHIFT( st->variable_HP_smth2_Q15, 8 ) );
+
+    if (st->application == OPUS_APPLICATION_VOIP)
+    {
+       hp_cutoff(pcm, cutoff_Hz, &pcm_buf[st->delay_compensation*st->channels], st->hp_mem, frame_size, st->channels, st->Fs);
+    } else {
+       for (i=0;i<frame_size*st->channels;i++)
+          pcm_buf[st->delay_compensation*st->channels + i] = pcm[i];
+    }
+
     /* SILK processing */
     if (st->mode != MODE_CELT_ONLY)
     {
@@ -537,10 +644,10 @@
         }
 
 #ifdef FIXED_POINT
-        pcm_silk = pcm;
+        pcm_silk = pcm_buf+st->delay_compensation*st->channels;
 #else
         for (i=0;i<frame_size*st->channels;i++)
-            pcm_silk[i] = FLOAT2INT16(pcm[i]);
+            pcm_silk[i] = FLOAT2INT16(pcm_buf[st->delay_compensation*st->channels + i]);
 #endif
         ret = silk_Encode( silk_enc, &st->silk_mode, pcm_silk, frame_size, &enc, &nBytes, 0 );
         if( ret ) {
@@ -634,12 +741,10 @@
         nb_compr_bytes = 0;
     }
 
-    ALLOC(pcm_buf, IMAX(frame_size, st->Fs/200)*st->channels, opus_val16);
-    for (i=0;i<IMIN(frame_size, st->delay_compensation)*st->channels;i++)
-        pcm_buf[i] = st->delay_buffer[(st->encoder_buffer-st->delay_compensation)*st->channels+i];
-    for (;i<frame_size*st->channels;i++)
-        pcm_buf[i] = pcm[i-st->delay_compensation*st->channels];
+    for (i=0;i<st->encoder_buffer*st->channels;i++)
+        st->delay_buffer[i] = pcm_buf[(frame_size+st->delay_compensation-st->encoder_buffer)*st->channels+i];
 
+
     if( st->mode == MODE_HYBRID && st->stream_channels == 2 ) {
         /* Apply stereo width reduction (at low bitrates) */
         if( st->hybrid_stereo_width_Q14 < (1 << 14) || st->silk_mode.stereoWidth_Q14 < (1 << 14) ) {
@@ -735,17 +840,6 @@
     }
 
 
-    if (frame_size>st->encoder_buffer)
-    {
-        for (i=0;i<st->encoder_buffer*st->channels;i++)
-            st->delay_buffer[i] = pcm[(frame_size-st->encoder_buffer)*st->channels+i];
-    } else {
-        int tmp = st->encoder_buffer-frame_size;
-        for (i=0;i<tmp*st->channels;i++)
-            st->delay_buffer[i] = st->delay_buffer[i+frame_size*st->channels];
-        for (i=0;i<frame_size*st->channels;i++)
-            st->delay_buffer[tmp*st->channels+i] = pcm[i];
-    }
 
     /* Signalling the mode in the first byte */
     data--;
@@ -1010,6 +1104,7 @@
            st->first = 1;
            st->mode = MODE_HYBRID;
            st->bandwidth = OPUS_BANDWIDTH_FULLBAND;
+           st->variable_HP_smth2_Q15 = SKP_LSHIFT( silk_lin2log( VARIABLE_HP_MIN_CUTOFF_HZ ), 8 );
         }
         break;
         default: