Fixes a rare integer overflow in the SILK "50% of max rate" rule
[opus.git] / src / opus_encoder.c
index a6340f3..e9a6c25 100644 (file)
 #include "config.h"
 #endif
 
-#include <string.h>
-#include <stdlib.h>
-#include <stdio.h>
 #include <stdarg.h>
 #include "celt.h"
 #include "entenc.h"
 #include "modes.h"
-#include "silk_API.h"
+#include "API.h"
 #include "stack_alloc.h"
 #include "float_cast.h"
 #include "opus.h"
 #include "arch.h"
 #include "opus_private.h"
+#include "os_support.h"
 
+#include "tuning_parameters.h"
 #ifdef FIXED_POINT
-#define celt_encode_native celt_encode
+#include "fixed/structs_FIX.h"
 #else
-#define celt_encode_native celt_encode_float
+#include "float/structs_FLP.h"
 #endif
 
 #define MAX_ENCODER_BUFFER 480
@@ -55,81 +54,112 @@ struct OpusEncoder {
     int          celt_enc_offset;
     int          silk_enc_offset;
     silk_EncControlStruct silk_mode;
-    int          hybrid_stereo_width_Q14;
-    int          channels;
-    int          stream_channels;
-    int          force_mono;
-
-    int          mode;
     int          application;
-    int          prev_mode;
+    int          channels;
+    int          delay_compensation;
+    int          force_channels;
     int          signal_type;
-    int          bandwidth;
     int          user_bandwidth;
+    int          user_forced_mode;
     int          voice_ratio;
-    /* Sampling rate (at the API level) */
-    int          Fs;
+    opus_int32   Fs;
     int          use_vbr;
     int          vbr_constraint;
-    int          bitrate_bps;
-    int          user_bitrate_bps;
+    opus_int32   bitrate_bps;
+    opus_int32   user_bitrate_bps;
     int          encoder_buffer;
-    int          delay_compensation;
+
+#define OPUS_ENCODER_RESET_START stream_channels
+    int          stream_channels;
+    opus_int16   hybrid_stereo_width_Q14;
+    opus_int32   variable_HP_smth2_Q15;
+    opus_val32   hp_mem[4];
+    int          mode;
+    int          prev_mode;
+    int          prev_channels;
+    int          prev_framesize;
+    int          bandwidth;
+    /* Sampling rate (at the API level) */
     int          first;
     opus_val16   delay_buffer[MAX_ENCODER_BUFFER*2];
 
-    int          rangeFinal;
+    opus_uint32  rangeFinal;
 };
 
-/* Transition tables for the voice and audio modes. First column is the
+/* Transition tables for the voice and music. First column is the
    middle (memoriless) threshold. The second column is the hysteresis
    (difference with the middle) */
-static const int voice_bandwidth_thresholds[10] = {
-               11000, 1000, /* NB<->MB */
-               14000, 1000, /* MB<->WB */
-               21000, 2000, /* WB<->SWB */
-               29000, 2000, /* SWB<->FB */
+static const opus_int32 mono_voice_bandwidth_thresholds[8] = {
+        11000, 1000, /* NB<->MB */
+        14000, 1000, /* MB<->WB */
+        21000, 2000, /* WB<->SWB */
+        29000, 2000, /* SWB<->FB */
 };
-static const int audio_bandwidth_thresholds[10] = {
-               30000,    0, /* MB not allowed */
-               20000, 2000, /* MB<->WB */
-               26000, 2000, /* WB<->SWB */
-               33000, 2000, /* SWB<->FB */
+static const opus_int32 mono_music_bandwidth_thresholds[8] = {
+        14000, 1000, /* MB not allowed */
+        18000, 2000, /* MB<->WB */
+        24000, 2000, /* WB<->SWB */
+        33000, 2000, /* SWB<->FB */
+};
+static const opus_int32 stereo_voice_bandwidth_thresholds[8] = {
+        11000, 1000, /* NB<->MB */
+        14000, 1000, /* MB<->WB */
+        21000, 2000, /* WB<->SWB */
+        32000, 2000, /* SWB<->FB */
+};
+static const opus_int32 stereo_music_bandwidth_thresholds[8] = {
+        14000, 1000, /* MB not allowed */
+        18000, 2000, /* MB<->WB */
+        24000, 2000, /* WB<->SWB */
+        48000, 2000, /* SWB<->FB */
+};
+/* Threshold bit-rates for switching between mono and stereo */
+static const opus_int32 stereo_voice_threshold = 26000;
+static const opus_int32 stereo_music_threshold = 36000;
+
+/* Threshold bit-rate for switching between SILK/hybrid and CELT-only */
+static const opus_int32 mode_thresholds[2][2] = {
+      /* voice */ /* music */
+      {  48000,      24000}, /* mono */
+      {  48000,      24000}, /* stereo */
 };
 
-
+static const int celt_delay_table[5] = {
+/* API 8  12  16  24  48 */
+      10, 16, 21, 27, 55
+};
 int opus_encoder_get_size(int channels)
 {
     int silkEncSizeBytes, celtEncSizeBytes;
     int ret;
+    if (channels<1 || channels > 2)
+        return 0;
     ret = silk_Get_Encoder_Size( &silkEncSizeBytes );
-    if(ret)
+    if (ret)
         return 0;
     silkEncSizeBytes = align(silkEncSizeBytes);
     celtEncSizeBytes = celt_encoder_get_size(channels);
     return align(sizeof(OpusEncoder))+silkEncSizeBytes+celtEncSizeBytes;
 }
 
-OpusEncoder *opus_encoder_init(OpusEncoder* st, int Fs, int channels, int application)
+int opus_encoder_init(OpusEncoder* st, opus_int32 Fs, int channels, int application)
 {
     void *silk_enc;
     CELTEncoder *celt_enc;
     int err;
     int ret, silkEncSizeBytes;
 
-    if (channels > 2 || channels<1)
-        return NULL;
-    if (application < OPUS_APPLICATION_VOIP || application > OPUS_APPLICATION_AUDIO)
-        return NULL;
-    if (Fs != 8000 && Fs != 12000 && Fs != 16000 && Fs != 24000 && Fs != 48000)
-        return NULL;
+   if((Fs!=48000&&Fs!=24000&&Fs!=16000&&Fs!=12000&&Fs!=8000)||(channels!=1&&channels!=2)||
+        (application != OPUS_APPLICATION_VOIP && application != OPUS_APPLICATION_AUDIO
+        && application != OPUS_APPLICATION_RESTRICTED_LOWDELAY))
+        return OPUS_BAD_ARG;
 
-    memset(st, 0, opus_encoder_get_size(channels));
+    OPUS_CLEAR((char*)st, opus_encoder_get_size(channels));
     /* Create SILK encoder */
     ret = silk_Get_Encoder_Size( &silkEncSizeBytes );
-    if( ret )
-       return NULL;
-       silkEncSizeBytes = align(silkEncSizeBytes);
+    if (ret)
+        return OPUS_BAD_ARG;
+    silkEncSizeBytes = align(silkEncSizeBytes);
     st->silk_enc_offset = align(sizeof(OpusEncoder));
     st->celt_enc_offset = st->silk_enc_offset+silkEncSizeBytes;
     silk_enc = (char*)st+st->silk_enc_offset;
@@ -140,8 +170,7 @@ OpusEncoder *opus_encoder_init(OpusEncoder* st, int Fs, int channels, int applic
     st->Fs = Fs;
 
     ret = silk_InitEncoder( silk_enc, &st->silk_mode );
-    if( ret )
-        goto failure;
+    if(ret)return OPUS_INTERNAL_ERROR;
 
     /* default SILK parameters */
     st->silk_mode.nChannelsAPI              = channels;
@@ -157,40 +186,40 @@ OpusEncoder *opus_encoder_init(OpusEncoder* st, int Fs, int channels, int applic
     st->silk_mode.useInBandFEC              = 0;
     st->silk_mode.useDTX                    = 0;
     st->silk_mode.useCBR                    = 0;
-    st->silk_mode.HP_cutoff_Hz              = 0;
-
-    st->hybrid_stereo_width_Q14             = 1 << 14;
 
     /* Create CELT encoder */
     /* Initialize CELT encoder */
-    celt_encoder_init(celt_enc, Fs, channels, &err);
-    if (err != CELT_OK)
-               goto failure;
+    err = celt_encoder_init(celt_enc, Fs, channels);
+    if(err!=OPUS_OK)return OPUS_INTERNAL_ERROR;
+
     celt_encoder_ctl(celt_enc, CELT_SET_SIGNALLING(0));
+    celt_encoder_ctl(celt_enc, OPUS_SET_COMPLEXITY(10));
 
-    st->mode = MODE_HYBRID;
-    st->bandwidth = OPUS_BANDWIDTH_FULLBAND;
-    st->use_vbr = 0;
-    st->user_bitrate_bps = OPUS_BITRATE_AUTO;
+    st->use_vbr = 1;
+    st->user_bitrate_bps = OPUS_AUTO;
     st->bitrate_bps = 3000+Fs*channels;
     st->application = application;
-    st->signal_type = OPUS_SIGNAL_AUTO;
-    st->user_bandwidth = OPUS_BANDWIDTH_AUTO;
-    st->voice_ratio = 90;
-    st->first = 1;
-
+    st->signal_type = OPUS_AUTO;
+    st->user_bandwidth = OPUS_AUTO;
+    st->force_channels = OPUS_AUTO;
+    st->user_forced_mode = OPUS_AUTO;
+    st->voice_ratio = -1;
     st->encoder_buffer = st->Fs/100;
+
     st->delay_compensation = st->Fs/400;
-    if (st->Fs > 16000)
-        st->delay_compensation += 10;
-    return st;
 
-failure:
-    free(st);
-    return NULL;
+    st->delay_compensation += celt_delay_table[rateID(st->Fs)];
+
+    st->hybrid_stereo_width_Q14             = 1 << 14;
+    st->variable_HP_smth2_Q15 = silk_LSHIFT( silk_lin2log( VARIABLE_HP_MIN_CUTOFF_HZ ), 8 );
+    st->first = 1;
+    st->mode = MODE_HYBRID;
+    st->bandwidth = OPUS_BANDWIDTH_FULLBAND;
+
+    return OPUS_OK;
 }
 
-static unsigned char gen_toc(int mode, int framerate, int bandwidth, int channels)
+static unsigned char gen_toc(int mode, int framerate, int bandwidth, int silk_bandwidth, int channels)
 {
    int period;
    unsigned char toc;
@@ -202,7 +231,7 @@ static unsigned char gen_toc(int mode, int framerate, int bandwidth, int channel
    }
    if (mode == MODE_SILK_ONLY)
    {
-       toc = (bandwidth-OPUS_BANDWIDTH_NARROWBAND)<<5;
+       toc = (silk_bandwidth-OPUS_BANDWIDTH_NARROWBAND)<<5;
        toc |= (period-2)<<3;
    } else if (mode == MODE_CELT_ONLY)
    {
@@ -221,19 +250,160 @@ static unsigned char gen_toc(int mode, int framerate, int bandwidth, int channel
    toc |= (channels==2)<<2;
    return toc;
 }
-OpusEncoder *opus_encoder_create(int Fs, int channels, int mode)
+
+#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
+)
 {
-    char *raw_state = (char *)malloc(opus_encoder_get_size(channels));
-    if (raw_state == NULL)
-       return NULL;
-    return opus_encoder_init((OpusEncoder*)raw_state, Fs, channels, mode);
+    /* 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;
+
+   silk_assert( cutoff_Hz <= silk_int32_MAX / SILK_FIX_CONST( 1.5 * 3.14159 / 1000, 19 ) );
+   Fc_Q19 = silk_DIV32_16( silk_SMULBB( SILK_FIX_CONST( 1.5 * 3.14159 / 1000, 19 ), cutoff_Hz ), Fs/1000 );
+   silk_assert( Fc_Q19 > 0 && Fc_Q19 < 32768 );
+
+   r_Q28 = SILK_FIX_CONST( 1.0, 28 ) - silk_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 ] = silk_LSHIFT( -r_Q28, 1 );
+   B_Q28[ 2 ] = r_Q28;
+
+   /* -r * ( 2 - Fc * Fc ); */
+   r_Q22  = silk_RSHIFT( r_Q28, 6 );
+   A_Q28[ 0 ] = silk_SMULWW( r_Q22, silk_SMULWW( Fc_Q19, Fc_Q19 ) - SILK_FIX_CONST( 2.0,  22 ) );
+   A_Q28[ 1 ] = silk_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
+}
+
+static void stereo_fade(const opus_val16 *in, opus_val16 *out, opus_val16 g1, opus_val16 g2,
+        int overlap, int frame_size, int channels, const opus_val16 *window, opus_int32 Fs)
+{
+    int i;
+    int inc = 48000/Fs;
+    g1 = Q15ONE-g1;
+    g2 = Q15ONE-g2;
+    for (i=0;i<overlap;i++)
+    {
+       opus_val32 diff;
+       opus_val16 g, w;
+       w = MULT16_16_Q15(window[i*inc], window[i*inc]);
+       g = SHR32(MAC16_16(MULT16_16(w,g2),
+             Q15ONE-w, g1), 15);
+       diff = EXTRACT16(HALF32((opus_val32)in[i*channels] - (opus_val32)in[i*channels+1]));
+       diff = MULT16_16_Q15(g, diff);
+       out[i*channels] = out[i*channels] - diff;
+       out[i*channels+1] = out[i*channels+1] + diff;
+    }
+    for (;i<frame_size;i++)
+    {
+       opus_val32 diff;
+       diff = EXTRACT16(HALF32((opus_val32)in[i*channels] - (opus_val32)in[i*channels+1]));
+       diff = MULT16_16_Q15(g2, diff);
+       out[i*channels] = out[i*channels] - diff;
+       out[i*channels+1] = out[i*channels+1] + diff;
+    }
+}
+
+OpusEncoder *opus_encoder_create(opus_int32 Fs, int channels, int application, int *error)
+{
+   int ret;
+   OpusEncoder *st;
+   if((Fs!=48000&&Fs!=24000&&Fs!=16000&&Fs!=12000&&Fs!=8000)||(channels!=1&&channels!=2)||
+       (application != OPUS_APPLICATION_VOIP && application != OPUS_APPLICATION_AUDIO
+       && application != OPUS_APPLICATION_RESTRICTED_LOWDELAY))
+   {
+      if (error)
+         *error = OPUS_BAD_ARG;
+      return NULL;
+   }
+   st = (OpusEncoder *)opus_alloc(opus_encoder_get_size(channels));
+   if (st == NULL)
+   {
+      if (error)
+         *error = OPUS_ALLOC_FAIL;
+      return NULL;
+   }
+   ret = opus_encoder_init(st, Fs, channels, application);
+   if (error)
+      *error = ret;
+   if (ret != OPUS_OK)
+   {
+      opus_free(st);
+      st = NULL;
+   }
+   return st;
+}
+
+static opus_int32 user_bitrate_to_bitrate(OpusEncoder *st, int frame_size, int max_data_bytes)
+{
+  if(!frame_size)frame_size=st->Fs/400;
+  if (st->user_bitrate_bps==OPUS_AUTO)
+    return 60*st->Fs/frame_size + st->Fs*st->channels;
+  else if (st->user_bitrate_bps==OPUS_BITRATE_MAX)
+    return max_data_bytes*8*st->Fs/frame_size;
+  else
+    return st->user_bitrate_bps;
 }
+
 #ifdef FIXED_POINT
+#define opus_encode_native opus_encode
 int opus_encode(OpusEncoder *st, const opus_val16 *pcm, int frame_size,
-               unsigned char *data, int max_data_bytes)
+                unsigned char *data, int max_data_bytes)
 #else
+#define opus_encode_native opus_encode_float
 int opus_encode_float(OpusEncoder *st, const opus_val16 *pcm, int frame_size,
-      unsigned char *data, int max_data_bytes)
+                      unsigned char *data, int max_data_bytes)
 #endif
 {
     void *silk_enc;
@@ -252,112 +422,127 @@ int opus_encode_float(OpusEncoder *st, const opus_val16 *pcm, int frame_size,
     VARDECL(opus_val16, pcm_buf);
     int nb_compr_bytes;
     int to_celt = 0;
-    opus_int32 mono_rate;
     opus_uint32 redundant_rng = 0;
+    int cutoff_Hz, hp_freq_smth1;
+    int voice_est;
+    opus_int32 equiv_rate;
+    int delay_compensation;
     ALLOC_STACK;
 
+    max_data_bytes = IMIN(1276, max_data_bytes);
+
     st->rangeFinal = 0;
     if (400*frame_size != st->Fs && 200*frame_size != st->Fs && 100*frame_size != st->Fs &&
          50*frame_size != st->Fs &&  25*frame_size != st->Fs &&  50*frame_size != 3*st->Fs)
-        return OPUS_BAD_ARG;
+    {
+       RESTORE_STACK;
+       return OPUS_BAD_ARG;
+    }
     silk_enc = (char*)st+st->silk_enc_offset;
     celt_enc = (CELTEncoder*)((char*)st+st->celt_enc_offset);
 
-    if (st->user_bitrate_bps==OPUS_BITRATE_AUTO)
-        st->bitrate_bps = 60*st->Fs/frame_size + st->Fs*st->channels;
+    if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY)
+       delay_compensation = 0;
     else
-        st->bitrate_bps = st->user_bitrate_bps;
+       delay_compensation = st->delay_compensation;
 
-    /* Rate-dependent mono-stereo decision */
-    if (st->force_mono)
-    {
-        st->stream_channels = 1;
-    } else if (st->mode == MODE_CELT_ONLY && st->channels == 2)
-    {
-        opus_int32 decision_rate;
-        decision_rate = st->bitrate_bps + st->voice_ratio*st->voice_ratio;
-        /* Add some hysteresis */
-        if (st->stream_channels == 2)
-            decision_rate += 4000;
-        else
-            decision_rate -= 4000;
-        if (decision_rate>48000)
-            st->stream_channels = 2;
-        else
-            st->stream_channels = 1;
-    } else {
-            st->stream_channels = st->channels;
-    }
+    st->bitrate_bps = user_bitrate_to_bitrate(st, frame_size, max_data_bytes);
 
-#ifdef FUZZING
-    if (st->channels == 2 && (rand()&0x1F)==0)
-       st->stream_channels = 3-st->stream_channels;
-#endif
+    /* Equivalent 20-ms rate for mode/channel/bandwidth decisions */
+    equiv_rate = st->bitrate_bps - 60*(st->Fs/frame_size - 50);
 
-    /* Equivalent bit-rate for mono */
-    mono_rate = st->bitrate_bps;
-    if (st->stream_channels==2)
-        mono_rate = 2*mono_rate/3;
-    /* Compensate for smaller frame sizes assuming an equivalent overhead
-       of 60 bits/frame */
-    mono_rate -= 60*(st->Fs/frame_size - 50);
+    if (st->signal_type == OPUS_SIGNAL_VOICE)
+       voice_est = 127;
+    else if (st->signal_type == OPUS_SIGNAL_MUSIC)
+       voice_est = 0;
+    else if (st->voice_ratio >= 0)
+       voice_est = st->voice_ratio*327>>8;
+    else if (st->application == OPUS_APPLICATION_VOIP)
+       voice_est = 115;
+    else
+       voice_est = 64;
 
-#ifdef FUZZING
-    if ((rand()&0xF)==0)
+    if (st->force_channels!=OPUS_AUTO && st->channels == 2)
     {
-       if ((rand()&0x1)==0)
-          st->mode = MODE_CELT_ONLY;
-       else
-          st->mode = MODE_SILK_ONLY;
+        st->stream_channels = st->force_channels;
     } else {
-       if (st->prev_mode==MODE_CELT_ONLY)
-          st->mode = MODE_CELT_ONLY;
-       else
-          st->mode = MODE_SILK_ONLY;
-    }
+#ifdef FUZZING
+       /* Random mono/stereo decision */
+       if (st->channels == 2 && (rand()&0x1F)==0)
+          st->stream_channels = 3-st->stream_channels;
 #else
+       /* Rate-dependent mono-stereo decision */
+       if (st->channels == 2)
+       {
+          opus_int32 stereo_threshold;
+          stereo_threshold = stereo_music_threshold + ((voice_est*voice_est*(stereo_voice_threshold-stereo_music_threshold))>>14);
+          if (st->stream_channels == 2)
+             stereo_threshold -= 4000;
+          else
+             stereo_threshold += 4000;
+          st->stream_channels = (equiv_rate > stereo_threshold) ? 2 : 1;
+       } else {
+          st->stream_channels = st->channels;
+       }
+#endif
+    }
+
     /* Mode selection depending on application and signal type */
-    if (st->application==OPUS_APPLICATION_VOIP)
+    if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY)
     {
-        opus_int32 threshold = 20000;
-        /* Hysteresis */
-        if (st->prev_mode == MODE_CELT_ONLY)
-            threshold -= 4000;
-        else if (st->prev_mode>0)
-            threshold += 4000;
-
-        /* OPUS_APPLICATION_VOIP defaults to MODE_SILK_ONLY */
-        if (st->signal_type == OPUS_SIGNAL_MUSIC && mono_rate > threshold)
-            st->mode = MODE_CELT_ONLY;
-        else
-            st->mode = MODE_SILK_ONLY;
-    } else {/* OPUS_APPLICATION_AUDIO */
-        opus_int32 threshold;
-        /* SILK/CELT threshold is higher for voice than for music */
-        threshold = 36000;
-        if (st->signal_type == OPUS_SIGNAL_MUSIC)
-            threshold -= 20000;
-        else if (st->signal_type == OPUS_SIGNAL_VOICE)
-            threshold += 8000;
-
-        /* Hysteresis */
-        if (st->prev_mode == MODE_CELT_ONLY)
-            threshold -= 4000;
-        else if (st->prev_mode>0)
-            threshold += 4000;
-
-        if (mono_rate>threshold)
-            st->mode = MODE_CELT_ONLY;
-        else
-            st->mode = MODE_SILK_ONLY;
-    }
+       st->mode = MODE_CELT_ONLY;
+    } else if (st->user_forced_mode == OPUS_AUTO)
+    {
+#ifdef FUZZING
+       /* Random mode switching */
+       if ((rand()&0xF)==0)
+       {
+          if ((rand()&0x1)==0)
+             st->mode = MODE_CELT_ONLY;
+          else
+             st->mode = MODE_SILK_ONLY;
+       } else {
+          if (st->prev_mode==MODE_CELT_ONLY)
+             st->mode = MODE_CELT_ONLY;
+          else
+             st->mode = MODE_SILK_ONLY;
+       }
+#else
+       int chan;
+       opus_int32 mode_voice, mode_music;
+       opus_int32 threshold;
+
+       chan = (st->channels==2) && st->force_channels!=1;
+       mode_voice = mode_thresholds[chan][0];
+       mode_music = mode_thresholds[chan][1];
+       threshold = mode_music + ((voice_est*voice_est*(mode_voice-mode_music))>>14);
+
+       /* Hysteresis */
+       if (st->prev_mode == MODE_CELT_ONLY)
+           threshold -= 4000;
+       else if (st->prev_mode>0)
+           threshold += 4000;
+
+       st->mode = (equiv_rate >= threshold) ? MODE_CELT_ONLY: MODE_SILK_ONLY;
 #endif
+    } else {
+       st->mode = st->user_forced_mode;
+    }
+
     /* Override the chosen mode to make sure we meet the requested frame size */
-    if (st->mode == MODE_CELT_ONLY && frame_size > st->Fs/50)
-       st->mode = MODE_SILK_ONLY;
     if (st->mode != MODE_CELT_ONLY && frame_size < st->Fs/100)
        st->mode = MODE_CELT_ONLY;
 
+    if (st->stream_channels == 1 && st->prev_channels ==2 && st->silk_mode.toMono==0
+          && st->mode != MODE_CELT_ONLY && st->prev_mode != MODE_CELT_ONLY)
+    {
+       /* Delay stereo->mono transition by two frames so that SILK can do a smooth downmix */
+       st->silk_mode.toMono = 1;
+       st->stream_channels = 2;
+    } else {
+       st->silk_mode.toMono = 0;
+    }
+
     if (st->prev_mode > 0 &&
         ((st->mode != MODE_CELT_ONLY && st->prev_mode == MODE_CELT_ONLY) ||
     (st->mode == MODE_CELT_ONLY && st->prev_mode != MODE_CELT_ONLY)))
@@ -386,10 +571,24 @@ int opus_encode_float(OpusEncoder *st, const opus_val16 *pcm, int frame_size,
     /* Automatic (rate-dependent) bandwidth selection */
     if (st->mode == MODE_CELT_ONLY || st->first || st->silk_mode.allowBandwidthSwitch)
     {
-        const int *bandwidth_thresholds;
+        const opus_int32 *voice_bandwidth_thresholds, *music_bandwidth_thresholds;
+        opus_int32 bandwidth_thresholds[8];
         int bandwidth = OPUS_BANDWIDTH_FULLBAND;
 
-        bandwidth_thresholds = st->mode == MODE_CELT_ONLY ? audio_bandwidth_thresholds : voice_bandwidth_thresholds;
+        if (st->channels==2 && st->force_channels!=1)
+        {
+           voice_bandwidth_thresholds = stereo_voice_bandwidth_thresholds;
+           music_bandwidth_thresholds = stereo_music_bandwidth_thresholds;
+        } else {
+           voice_bandwidth_thresholds = mono_voice_bandwidth_thresholds;
+           music_bandwidth_thresholds = mono_music_bandwidth_thresholds;
+        }
+        /* Interpolate bandwidth thresholds depending on voice estimation */
+        for (i=0;i<8;i++)
+        {
+           bandwidth_thresholds[i] = music_bandwidth_thresholds[i]
+                    + ((voice_est*voice_est*(voice_bandwidth_thresholds[i]-music_bandwidth_thresholds[i]))>>14);
+        }
         do {
             int threshold, hysteresis;
             threshold = bandwidth_thresholds[2*(bandwidth-OPUS_BANDWIDTH_MEDIUMBAND)];
@@ -401,7 +600,7 @@ int opus_encode_float(OpusEncoder *st, const opus_val16 *pcm, int frame_size,
                 else
                     threshold += hysteresis;
             }
-            if (mono_rate >= threshold)
+            if (equiv_rate >= threshold)
                 break;
         } while (--bandwidth>OPUS_BANDWIDTH_NARROWBAND);
         st->bandwidth = bandwidth;
@@ -411,6 +610,9 @@ int opus_encode_float(OpusEncoder *st, const opus_val16 *pcm, int frame_size,
             st->bandwidth = OPUS_BANDWIDTH_WIDEBAND;
     }
 
+    if (st->user_bandwidth != OPUS_AUTO)
+        st->bandwidth = st->user_bandwidth;
+
     /* Prevents Opus from wasting bits on frequencies that are above
        the Nyquist rate of the input signal */
     if (st->Fs <= 24000 && st->bandwidth > OPUS_BANDWIDTH_SUPERWIDEBAND)
@@ -422,30 +624,99 @@ int opus_encode_float(OpusEncoder *st, const opus_val16 *pcm, int frame_size,
     if (st->Fs <= 8000 && st->bandwidth > OPUS_BANDWIDTH_NARROWBAND)
         st->bandwidth = OPUS_BANDWIDTH_NARROWBAND;
 
-    if (st->user_bandwidth != OPUS_BANDWIDTH_AUTO)
-        st->bandwidth = st->user_bandwidth;
 
     /* Can't support higher than wideband for >20 ms frames */
-    if (frame_size > st->Fs/50 && st->bandwidth > OPUS_BANDWIDTH_WIDEBAND)
-       st->bandwidth = OPUS_BANDWIDTH_WIDEBAND;
+    if (frame_size > st->Fs/50 && (st->mode == MODE_CELT_ONLY || st->bandwidth > OPUS_BANDWIDTH_WIDEBAND))
+    {
+       VARDECL(unsigned char, tmp_data);
+       VARDECL(unsigned char, rp_);
+       int nb_frames;
+       int bak_mode, bak_bandwidth, bak_channels, bak_to_mono;
+       OpusRepacketizer *rp;
+       int bytes_per_frame;
+
+
+       nb_frames = frame_size > st->Fs/25 ? 3 : 2;
+       bytes_per_frame = max_data_bytes/nb_frames-3;
+
+       ALLOC(tmp_data, nb_frames*bytes_per_frame, unsigned char);
+       ALLOC(rp_, opus_repacketizer_get_size(), unsigned char);
+
+       rp = opus_repacketizer_init((OpusRepacketizer*)rp_);
+
+       bak_mode = st->user_forced_mode;
+       bak_bandwidth = st->user_bandwidth;
+       bak_channels = st->force_channels;
 
+       st->user_forced_mode = st->mode;
+       st->user_bandwidth = st->bandwidth;
+       st->force_channels = st->stream_channels;
+       bak_to_mono = st->silk_mode.toMono;
+
+       if (bak_to_mono)
+          st->force_channels = 1;
+       else
+          st->prev_channels = st->stream_channels;
+       for (i=0;i<nb_frames;i++)
+       {
+          int tmp_len;
+          st->silk_mode.toMono = 0;
+          tmp_len = opus_encode_native(st, pcm+i*(st->channels*st->Fs/50), st->Fs/50, tmp_data+i*bytes_per_frame, bytes_per_frame);
+          ret = opus_repacketizer_cat(rp, tmp_data+i*bytes_per_frame, tmp_len);
+       }
+       ret = opus_repacketizer_out(rp, data, max_data_bytes);
+
+       st->user_forced_mode = bak_mode;
+       st->user_bandwidth = bak_bandwidth;
+       st->force_channels = bak_channels;
+       st->silk_mode.toMono = bak_to_mono;
+       RESTORE_STACK;
+       return ret;
+    }
     /* CELT mode doesn't support mediumband, use wideband instead */
     if (st->mode == MODE_CELT_ONLY && st->bandwidth == OPUS_BANDWIDTH_MEDIUMBAND)
         st->bandwidth = OPUS_BANDWIDTH_WIDEBAND;
 
     /* Chooses the appropriate mode for speech
-       *NEVER* switch to/from CELT-only mode here as this will */
+       *NEVER* switch to/from CELT-only mode here as this will invalidate some assumptions */
     if (st->mode == MODE_SILK_ONLY && st->bandwidth > OPUS_BANDWIDTH_WIDEBAND)
         st->mode = MODE_HYBRID;
     if (st->mode == MODE_HYBRID && st->bandwidth <= OPUS_BANDWIDTH_WIDEBAND)
         st->mode = MODE_SILK_ONLY;
 
-    bytes_target = st->bitrate_bps * frame_size / (st->Fs * 8) - 1;
+    /* If max_data_bytes represents less than 8 kb/s, switch to CELT-only mode */
+    if (max_data_bytes < 8000*frame_size / (st->Fs * 8))
+       st->mode = MODE_CELT_ONLY;
+    /* printf("%d %d %d %d\n", st->bitrate_bps, st->stream_channels, st->mode, st->bandwidth); */
+    bytes_target = IMIN(max_data_bytes, st->bitrate_bps * frame_size / (st->Fs * 8)) - 1;
 
     data += 1;
 
     ec_enc_init(&enc, data, max_data_bytes-1);
 
+    ALLOC(pcm_buf, (delay_compensation+frame_size)*st->channels, opus_val16);
+    for (i=0;i<delay_compensation*st->channels;i++)
+       pcm_buf[i] = st->delay_buffer[(st->encoder_buffer-delay_compensation)*st->channels+i];
+
+    if (st->mode == MODE_CELT_ONLY)
+       hp_freq_smth1 = silk_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 = silk_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( silk_RSHIFT( st->variable_HP_smth2_Q15, 8 ) );
+
+    if (st->application == OPUS_APPLICATION_VOIP)
+    {
+       hp_cutoff(pcm, cutoff_Hz, &pcm_buf[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[delay_compensation*st->channels + i] = pcm[i];
+    }
+
     /* SILK processing */
     if (st->mode != MODE_CELT_ONLY)
     {
@@ -481,6 +752,9 @@ int opus_encode_float(OpusEncoder *st, const opus_val16 *pcm, int frame_size,
                 st->silk_mode.bitRate = ( st->bitrate_bps - 8*st->Fs/frame_size ) * 4/5;
             }
         }
+        /* SILK is not allow to use more than 50% of max_data_bytes */
+        if (max_data_bytes < st->silk_mode.bitRate*frame_size / (st->Fs * 4))
+           st->silk_mode.bitRate = max_data_bytes*st->Fs*4/frame_size;
 
         st->silk_mode.payloadSize_ms = 1000 * frame_size / st->Fs;
         st->silk_mode.nChannelsAPI = st->channels;
@@ -490,7 +764,7 @@ int opus_encode_float(OpusEncoder *st, const opus_val16 *pcm, int frame_size,
         } else if (st->bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) {
             st->silk_mode.desiredInternalSampleRate = 12000;
         } else {
-            SKP_assert( st->mode == MODE_HYBRID || st->bandwidth == OPUS_BANDWIDTH_WIDEBAND );
+            silk_assert( st->mode == MODE_HYBRID || st->bandwidth == OPUS_BANDWIDTH_WIDEBAND );
             st->silk_mode.desiredInternalSampleRate = 16000;
         }
         if( st->mode == MODE_HYBRID ) {
@@ -510,25 +784,27 @@ int opus_encode_float(OpusEncoder *st, const opus_val16 *pcm, int frame_size,
             pcm_silk = st->delay_buffer;
 #else
             for (i=0;i<st->encoder_buffer*st->channels;i++)
-               pcm_silk[i] = FLOAT2INT16(st->delay_buffer[i]);
+                pcm_silk[i] = FLOAT2INT16(st->delay_buffer[i]);
 #endif
             silk_Encode( silk_enc, &st->silk_mode, pcm_silk, st->encoder_buffer, NULL, &zero, 1 );
         }
 
 #ifdef FIXED_POINT
-        pcm_silk = pcm;
+        pcm_silk = pcm_buf+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[delay_compensation*st->channels + i]);
 #endif
         ret = silk_Encode( silk_enc, &st->silk_mode, pcm_silk, frame_size, &enc, &nBytes, 0 );
         if( ret ) {
-            fprintf (stderr, "SILK encode error: %d\n", ret);
+            /*fprintf (stderr, "SILK encode error: %d\n", ret);*/
             /* Handle error */
+           return OPUS_INTERNAL_ERROR;
         }
         if (nBytes==0)
         {
-           data[-1] = gen_toc(st->mode, st->Fs/frame_size, st->bandwidth, st->stream_channels);
+           data[-1] = gen_toc(st->mode, st->Fs/frame_size, st->bandwidth, silk_internal_bandwidth, st->stream_channels);
+           RESTORE_STACK;
            return 1;
         }
         /* Extract SILK internal bandwidth for signaling in first byte */
@@ -541,7 +817,7 @@ int opus_encode_float(OpusEncoder *st, const opus_val16 *pcm, int frame_size,
                 silk_internal_bandwidth = OPUS_BANDWIDTH_WIDEBAND;
             }
         } else {
-            SKP_assert( st->silk_mode.internalSampleRate == 16000 );
+            silk_assert( st->silk_mode.internalSampleRate == 16000 );
         }
     }
 
@@ -555,9 +831,9 @@ int opus_encode_float(OpusEncoder *st, const opus_val16 *pcm, int frame_size,
                 endband = 13;
                 break;
             case OPUS_BANDWIDTH_MEDIUMBAND:
-           case OPUS_BANDWIDTH_WIDEBAND:
-               endband = 17;
-               break;
+            case OPUS_BANDWIDTH_WIDEBAND:
+                endband = 17;
+                break;
             case OPUS_BANDWIDTH_SUPERWIDEBAND:
                 endband = 19;
                 break;
@@ -570,16 +846,16 @@ int opus_encode_float(OpusEncoder *st, const opus_val16 *pcm, int frame_size,
     }
     if (st->mode != MODE_SILK_ONLY)
     {
-        celt_encoder_ctl(celt_enc, CELT_SET_VBR(0));
-        celt_encoder_ctl(celt_enc, CELT_SET_BITRATE(-1));
+        celt_encoder_ctl(celt_enc, OPUS_SET_VBR(0));
+        celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(OPUS_BITRATE_MAX));
         if (st->prev_mode == MODE_SILK_ONLY)
         {
             unsigned char dummy[10];
-            celt_encoder_ctl(celt_enc, CELT_RESET_STATE);
+            celt_encoder_ctl(celt_enc, OPUS_RESET_STATE);
             celt_encoder_ctl(celt_enc, CELT_SET_START_BAND(0));
             celt_encoder_ctl(celt_enc, CELT_SET_PREDICTION(0));
-            /* TODO: This wastes CPU a bit compared to just prefilling the buffer */
-            celt_encode_native(celt_enc, &st->delay_buffer[(st->encoder_buffer-st->delay_compensation-st->Fs/400)*st->channels], st->Fs/400, dummy, 10);
+            /* NOTE: We could speed this up slightly (at the expense of code size) by just adding a function that prefills the buffer */
+            celt_encode_with_ec(celt_enc, &st->delay_buffer[(st->encoder_buffer-delay_compensation-st->Fs/400)*st->channels], st->Fs/400, dummy, 10, NULL);
         } else {
             celt_encoder_ctl(celt_enc, CELT_SET_PREDICTION(2));
         }
@@ -598,9 +874,9 @@ int opus_encode_float(OpusEncoder *st, const opus_val16 *pcm, int frame_size,
         } else {
             if (st->use_vbr)
             {
-                celt_encoder_ctl(celt_enc, CELT_SET_VBR(1));
-                celt_encoder_ctl(celt_enc, CELT_SET_VBR_CONSTRAINT(st->vbr_constraint));
-                celt_encoder_ctl(celt_enc, CELT_SET_BITRATE(st->bitrate_bps));
+                celt_encoder_ctl(celt_enc, OPUS_SET_VBR(1));
+                celt_encoder_ctl(celt_enc, OPUS_SET_VBR_CONSTRAINT(st->vbr_constraint));
+                celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(st->bitrate_bps));
                 nb_compr_bytes = max_data_bytes-1;
             } else {
                 nb_compr_bytes = bytes_target;
@@ -611,52 +887,62 @@ int opus_encode_float(OpusEncoder *st, const opus_val16 *pcm, int frame_size,
         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->channels*(st->encoder_buffer-(frame_size+delay_compensation));i++)
+        st->delay_buffer[i] = st->delay_buffer[i+st->channels*frame_size];
+    for (;i<st->encoder_buffer*st->channels;i++)
+        st->delay_buffer[i] = pcm_buf[(frame_size+delay_compensation-st->encoder_buffer)*st->channels+i];
 
-    if( st->mode == MODE_HYBRID && st->stream_channels == 2 ) {
+
+    if (st->mode != MODE_HYBRID || st->stream_channels==1)
+       st->hybrid_stereo_width_Q14 = st->silk_mode.stereoWidth_Q14 = 1<<14;
+    if( st->channels == 2 ) {
         /* Apply stereo width reduction (at low bitrates) */
         if( st->hybrid_stereo_width_Q14 < (1 << 14) || st->silk_mode.stereoWidth_Q14 < (1 << 14) ) {
-            int width_Q14, delta_Q14, nSamples_8ms, diff;
-            nSamples_8ms = ( st->Fs * 8 ) / 1000;
-            width_Q14 = (1 << 14 ) - st->hybrid_stereo_width_Q14;
-            delta_Q14 = ( st->hybrid_stereo_width_Q14 - st->silk_mode.stereoWidth_Q14 ) / nSamples_8ms;
-            for( i = 0; i < nSamples_8ms; i++ ) {
-                width_Q14 += delta_Q14;
-                diff = pcm_buf[ 2*i+1 ] - (opus_int32)pcm_buf[ 2*i ];
-                diff = ( diff * width_Q14 ) >> 15;
-                pcm_buf[ 2*i ]   = (opus_int16)( pcm_buf[ 2*i ]   + diff );
-                pcm_buf[ 2*i+1 ] = (opus_int16)( pcm_buf[ 2*i+1 ] - diff );
-            }
-            for( ; i < frame_size; i++ ) {
-                diff = pcm_buf[ 2*i+1 ] - (opus_int32)pcm_buf[ 2*i ];
-                diff = ( diff * width_Q14 ) >> 15;
-                pcm_buf[ 2*i ]   = (opus_int16)( pcm_buf[ 2*i ]   + diff );
-                pcm_buf[ 2*i+1 ] = (opus_int16)( pcm_buf[ 2*i+1 ] - diff );
-            }
+            opus_val16 g1, g2;
+            const CELTMode *celt_mode;
+
+            celt_encoder_ctl(celt_enc, CELT_GET_MODE(&celt_mode));
+            g1 = st->hybrid_stereo_width_Q14;
+            g2 = st->silk_mode.stereoWidth_Q14;
+#ifdef FIXED_POINT
+            g1 *= (1./16384);
+            g2 *= (1./16384);
+#else
+            g1 = g1==16384 ? Q15ONE : SHL16(g1,1);
+            g2 = g2==16384 ? Q15ONE : SHL16(g2,1);
+#endif
+            stereo_fade(pcm_buf, pcm_buf, g1, g2, celt_mode->overlap,
+                  frame_size, st->channels, celt_mode->window, st->Fs);
             st->hybrid_stereo_width_Q14 = st->silk_mode.stereoWidth_Q14;
         }
     }
 
-    if (st->mode != MODE_CELT_ONLY)
+    if ( st->mode != MODE_CELT_ONLY && ec_tell(&enc)+17+20*(st->mode == MODE_HYBRID) < 8*(max_data_bytes-1))
     {
-        /* Check if we have a redundant 0-8 kHz band */
-        ec_enc_bit_logp(&enc, redundancy, 12);
+        /* For SILK mode, the redundancy is inferred from the length */
+        if (st->mode == MODE_HYBRID && (redundancy || ec_tell(&enc)+37 < 8*nb_compr_bytes))
+           ec_enc_bit_logp(&enc, redundancy, 12);
         if (redundancy)
         {
+            int max_redundancy;
+            ec_enc_bit_logp(&enc, celt_to_silk, 1);
+            if (st->mode == MODE_HYBRID)
+               max_redundancy = (max_data_bytes-1)-nb_compr_bytes-1;
+            else
+               max_redundancy = (max_data_bytes-1)-((ec_tell(&enc)+7)>>3);
             /* Target the same bit-rate for redundancy as for the rest,
                up to a max of 257 bytes */
-            redundancy_bytes = IMIN(257, st->bitrate_bps/1600);
-            ec_enc_bit_logp(&enc, celt_to_silk, 1);
+            redundancy_bytes = IMIN(max_redundancy, st->bitrate_bps/1600);
+            redundancy_bytes = IMIN(257, IMAX(2, redundancy_bytes));
             if (st->mode == MODE_HYBRID)
-               ec_enc_uint(&enc, redundancy_bytes-2, 256);
+                ec_enc_uint(&enc, redundancy_bytes-2, 256);
         }
-        start_band = 17;
+    } else {
+        redundancy = 0;
     }
 
+    if (st->mode != MODE_CELT_ONLY)start_band=17;
+
     if (st->mode == MODE_SILK_ONLY)
     {
         ret = (ec_tell(&enc)+7)>>3;
@@ -671,7 +957,7 @@ int opus_encode_float(OpusEncoder *st, const opus_val16 *pcm, int frame_size,
             while(ret>2&&data[ret-1]==0)ret--;
         nb_compr_bytes = ret;
     } else {
-       nb_compr_bytes = IMIN(1275-redundancy_bytes, nb_compr_bytes);
+       nb_compr_bytes = IMIN((max_data_bytes-1)-redundancy_bytes, nb_compr_bytes);
        ec_enc_shrink(&enc, nb_compr_bytes);
     }
 
@@ -680,10 +966,10 @@ int opus_encode_float(OpusEncoder *st, const opus_val16 *pcm, int frame_size,
     if (redundancy && celt_to_silk)
     {
         celt_encoder_ctl(celt_enc, CELT_SET_START_BAND(0));
-        celt_encoder_ctl(celt_enc, CELT_SET_VBR(0));
-        celt_encode_native(celt_enc, pcm_buf, st->Fs/200, data+nb_compr_bytes, redundancy_bytes);
-        celt_encoder_ctl(celt_enc, CELT_GET_RANGE(&redundant_rng));
-        celt_encoder_ctl(celt_enc, CELT_RESET_STATE);
+        celt_encoder_ctl(celt_enc, OPUS_SET_VBR(0));
+        celt_encode_with_ec(celt_enc, pcm_buf, st->Fs/200, data+nb_compr_bytes, redundancy_bytes, NULL);
+        celt_encoder_ctl(celt_enc, OPUS_GET_FINAL_RANGE(&redundant_rng));
+        celt_encoder_ctl(celt_enc, OPUS_RESET_STATE);
     }
 
     celt_encoder_ctl(celt_enc, CELT_SET_START_BAND(start_band));
@@ -691,6 +977,8 @@ int opus_encode_float(OpusEncoder *st, const opus_val16 *pcm, int frame_size,
     if (st->mode != MODE_SILK_ONLY)
     {
         ret = celt_encode_with_ec(celt_enc, pcm_buf, frame_size, NULL, nb_compr_bytes, &enc);
+        if (ret < 0)
+           return OPUS_INTERNAL_ERROR;
     }
 
     /* 5 ms redundant frame for SILK->CELT */
@@ -700,33 +988,22 @@ int opus_encode_float(OpusEncoder *st, const opus_val16 *pcm, int frame_size,
         N2 = st->Fs/200;
         N4 = st->Fs/400;
 
-        celt_encoder_ctl(celt_enc, CELT_RESET_STATE);
+        celt_encoder_ctl(celt_enc, OPUS_RESET_STATE);
         celt_encoder_ctl(celt_enc, CELT_SET_START_BAND(0));
         celt_encoder_ctl(celt_enc, CELT_SET_PREDICTION(0));
 
-        /* TODO: We could speed up prefilling here */
-        celt_encode_native(celt_enc, pcm_buf+st->channels*(frame_size-N2-N4), N4, data+nb_compr_bytes, redundancy_bytes);
+        /* NOTE: We could speed this up slightly (at the expense of code size) by just adding a function that prefills the buffer */
+        celt_encode_with_ec(celt_enc, pcm_buf+st->channels*(frame_size-N2-N4), N4, data+nb_compr_bytes, redundancy_bytes, NULL);
 
-        celt_encode_native(celt_enc, pcm_buf+st->channels*(frame_size-N2), N2, data+nb_compr_bytes, redundancy_bytes);
-        celt_encoder_ctl(celt_enc, CELT_GET_RANGE(&redundant_rng));
+        celt_encode_with_ec(celt_enc, pcm_buf+st->channels*(frame_size-N2), N2, data+nb_compr_bytes, redundancy_bytes, NULL);
+        celt_encoder_ctl(celt_enc, OPUS_GET_FINAL_RANGE(&redundant_rng));
     }
 
 
-    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--;
-    data[0] = gen_toc(st->mode, st->Fs/frame_size, st->bandwidth, st->stream_channels);
+    data[0] = gen_toc(st->mode, st->Fs/frame_size, st->bandwidth, silk_internal_bandwidth, st->stream_channels);
 
     st->rangeFinal = enc.rng ^ redundant_rng;
 
@@ -734,7 +1011,11 @@ int opus_encode_float(OpusEncoder *st, const opus_val16 *pcm, int frame_size,
         st->prev_mode = MODE_CELT_ONLY;
     else
         st->prev_mode = st->mode;
+    st->prev_channels = st->stream_channels;
+    st->prev_framesize = frame_size;
+
     st->first = 0;
+    RESTORE_STACK;
     return ret+1+redundancy_bytes;
 }
 
@@ -779,9 +1060,11 @@ int opus_encode(OpusEncoder *st, const opus_int16 *pcm, int frame_size,
 
 int opus_encoder_ctl(OpusEncoder *st, int request, ...)
 {
+    int ret;
     CELTEncoder *celt_enc;
     va_list ap;
 
+    ret = OPUS_OK;
     va_start(ap, request);
 
     celt_enc = (CELTEncoder*)((char*)st+st->celt_enc_offset);
@@ -790,56 +1073,67 @@ int opus_encoder_ctl(OpusEncoder *st, int request, ...)
     {
         case OPUS_SET_APPLICATION_REQUEST:
         {
-               opus_int32 value = va_arg(ap, opus_int32);
+            opus_int32 value = va_arg(ap, opus_int32);
+            if (   (value != OPUS_APPLICATION_VOIP && value != OPUS_APPLICATION_AUDIO
+                 && value != OPUS_APPLICATION_RESTRICTED_LOWDELAY)
+               || (!st->first && st->application != value))
+            {
+               ret = OPUS_BAD_ARG;
+               break;
+            }
             st->application = value;
         }
         break;
         case OPUS_GET_APPLICATION_REQUEST:
         {
-               opus_int32 *value = va_arg(ap, opus_int32*);
-            *value = st->mode;
+            opus_int32 *value = va_arg(ap, opus_int32*);
+            *value = st->application;
         }
         break;
         case OPUS_SET_BITRATE_REQUEST:
         {
-               opus_int32 value = va_arg(ap, opus_int32);
-            if (value != OPUS_BITRATE_AUTO)
+            opus_int32 value = va_arg(ap, opus_int32);
+            if (value != OPUS_AUTO && value != OPUS_BITRATE_MAX)
             {
                 if (value <= 0)
                     goto bad_arg;
                 else if (value <= 500)
                     value = 500;
+                else if (value > (opus_int32)300000*st->channels)
+                    value = (opus_int32)300000*st->channels;
             }
             st->user_bitrate_bps = value;
         }
         break;
         case OPUS_GET_BITRATE_REQUEST:
         {
-               opus_int32 *value = va_arg(ap, opus_int32*);
-            *value = st->bitrate_bps;
+            opus_int32 *value = va_arg(ap, opus_int32*);
+            *value = user_bitrate_to_bitrate(st, st->prev_framesize, 1276);
         }
         break;
-        case OPUS_SET_FORCE_MONO_REQUEST:
+        case OPUS_SET_FORCE_CHANNELS_REQUEST:
         {
-               opus_int32 value = va_arg(ap, opus_int32);
-            st->force_mono = value;
+            opus_int32 value = va_arg(ap, opus_int32);
+            if((value<1 || value>st->channels) && value != OPUS_AUTO)
+                return OPUS_BAD_ARG;
+            st->force_channels = value;
         }
         break;
-        case OPUS_GET_FORCE_MONO_REQUEST:
+        case OPUS_GET_FORCE_CHANNELS_REQUEST:
         {
-               opus_int32 *value = va_arg(ap, opus_int32*);
-            *value = !!st->force_mono;
+            opus_int32 *value = va_arg(ap, opus_int32*);
+            *value = st->force_channels;
         }
         break;
         case OPUS_SET_BANDWIDTH_REQUEST:
         {
-               opus_int32 value = va_arg(ap, opus_int32);
-            if (value < OPUS_BANDWIDTH_AUTO || value > OPUS_BANDWIDTH_FULLBAND)
-               return OPUS_BAD_ARG;
+            opus_int32 value = va_arg(ap, opus_int32);
+            if ((value < OPUS_BANDWIDTH_NARROWBAND || value > OPUS_BANDWIDTH_FULLBAND) && value != OPUS_AUTO)
+                return OPUS_BAD_ARG;
             st->user_bandwidth = value;
             if (st->user_bandwidth == OPUS_BANDWIDTH_NARROWBAND) {
                 st->silk_mode.maxInternalSampleRate = 8000;
-            } else if (st->bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) {
+            } else if (st->user_bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) {
                 st->silk_mode.maxInternalSampleRate = 12000;
             } else {
                 st->silk_mode.maxInternalSampleRate = 16000;
@@ -848,117 +1142,131 @@ int opus_encoder_ctl(OpusEncoder *st, int request, ...)
         break;
         case OPUS_GET_BANDWIDTH_REQUEST:
         {
-               opus_int32 *value = va_arg(ap, opus_int32*);
+            opus_int32 *value = va_arg(ap, opus_int32*);
             *value = st->bandwidth;
         }
         break;
         case OPUS_SET_DTX_REQUEST:
         {
-               opus_int32 value = va_arg(ap, opus_int32);
+            opus_int32 value = va_arg(ap, opus_int32);
+            if(value<0 || value>1)
+                return OPUS_BAD_ARG;
             st->silk_mode.useDTX = value;
         }
         break;
         case OPUS_GET_DTX_REQUEST:
         {
-               opus_int32 *value = va_arg(ap, opus_int32*);
+            opus_int32 *value = va_arg(ap, opus_int32*);
             *value = st->silk_mode.useDTX;
         }
         break;
         case OPUS_SET_COMPLEXITY_REQUEST:
         {
-               opus_int32 value = va_arg(ap, opus_int32);
+            opus_int32 value = va_arg(ap, opus_int32);
+            if(value<0 || value>10)
+                return OPUS_BAD_ARG;
             st->silk_mode.complexity = value;
-            celt_encoder_ctl(celt_enc, CELT_SET_COMPLEXITY(value));
+            celt_encoder_ctl(celt_enc, OPUS_SET_COMPLEXITY(value));
         }
         break;
         case OPUS_GET_COMPLEXITY_REQUEST:
         {
-               opus_int32 *value = va_arg(ap, opus_int32*);
+            opus_int32 *value = va_arg(ap, opus_int32*);
             *value = st->silk_mode.complexity;
         }
         break;
-        case OPUS_SET_INBAND_FEC_FLAG_REQUEST:
+        case OPUS_SET_INBAND_FEC_REQUEST:
         {
-               opus_int32 value = va_arg(ap, opus_int32);
+            opus_int32 value = va_arg(ap, opus_int32);
+            if(value<0 || value>1)
+                return OPUS_BAD_ARG;
             st->silk_mode.useInBandFEC = value;
         }
         break;
-        case OPUS_GET_INBAND_FEC_FLAG_REQUEST:
+        case OPUS_GET_INBAND_FEC_REQUEST:
         {
-               opus_int32 *value = va_arg(ap, opus_int32*);
+            opus_int32 *value = va_arg(ap, opus_int32*);
             *value = st->silk_mode.useInBandFEC;
         }
         break;
         case OPUS_SET_PACKET_LOSS_PERC_REQUEST:
         {
-               opus_int32 value = va_arg(ap, opus_int32);
+            opus_int32 value = va_arg(ap, opus_int32);
             if (value < 0 || value > 100)
                 return OPUS_BAD_ARG;
             st->silk_mode.packetLossPercentage = value;
-            celt_encoder_ctl(celt_enc, CELT_SET_LOSS_PERC(value));
+            celt_encoder_ctl(celt_enc, OPUS_SET_PACKET_LOSS_PERC(value));
         }
         break;
         case OPUS_GET_PACKET_LOSS_PERC_REQUEST:
         {
-               opus_int32 *value = va_arg(ap, opus_int32*);
+            opus_int32 *value = va_arg(ap, opus_int32*);
             *value = st->silk_mode.packetLossPercentage;
         }
         break;
         case OPUS_SET_VBR_REQUEST:
         {
-               opus_int32 value = va_arg(ap, opus_int32);
+            opus_int32 value = va_arg(ap, opus_int32);
+            if(value<0 || value>1)
+                return OPUS_BAD_ARG;
             st->use_vbr = value;
             st->silk_mode.useCBR = 1-value;
         }
         break;
         case OPUS_GET_VBR_REQUEST:
         {
-               opus_int32 *value = va_arg(ap, opus_int32*);
+            opus_int32 *value = va_arg(ap, opus_int32*);
             *value = st->use_vbr;
         }
         break;
         case OPUS_SET_VOICE_RATIO_REQUEST:
         {
-               opus_int32 value = va_arg(ap, opus_int32);
-            if (value>100 || value<0)
+            opus_int32 value = va_arg(ap, opus_int32);
+            if (value>100 || value<-1)
                 goto bad_arg;
             st->voice_ratio = value;
         }
         break;
         case OPUS_GET_VOICE_RATIO_REQUEST:
         {
-               opus_int32 *value = va_arg(ap, opus_int32*);
+            opus_int32 *value = va_arg(ap, opus_int32*);
             *value = st->voice_ratio;
         }
         break;
         case OPUS_SET_VBR_CONSTRAINT_REQUEST:
         {
-               opus_int32 value = va_arg(ap, opus_int32);
+            opus_int32 value = va_arg(ap, opus_int32);
+            if(value<0 || value>1)
+                return OPUS_BAD_ARG;
             st->vbr_constraint = value;
         }
         break;
         case OPUS_GET_VBR_CONSTRAINT_REQUEST:
         {
-               opus_int32 *value = va_arg(ap, opus_int32*);
+            opus_int32 *value = va_arg(ap, opus_int32*);
             *value = st->vbr_constraint;
         }
         break;
         case OPUS_SET_SIGNAL_REQUEST:
         {
-               opus_int32 value = va_arg(ap, opus_int32);
+            opus_int32 value = va_arg(ap, opus_int32);
+            if(value!=OPUS_AUTO && value!=OPUS_SIGNAL_VOICE && value!=OPUS_SIGNAL_MUSIC)
+                return OPUS_BAD_ARG;
             st->signal_type = value;
         }
         break;
         case OPUS_GET_SIGNAL_REQUEST:
         {
-               opus_int32 *value = va_arg(ap, opus_int32*);
+            opus_int32 *value = va_arg(ap, opus_int32*);
             *value = st->signal_type;
         }
         break;
         case OPUS_GET_LOOKAHEAD_REQUEST:
         {
-               opus_int32 *value = va_arg(ap, opus_int32*);
-            *value = st->delay_compensation+st->Fs/400;
+            opus_int32 *value = va_arg(ap, opus_int32*);
+            *value = st->Fs/400;
+            if (st->application != OPUS_APPLICATION_RESTRICTED_LOWDELAY)
+               *value += st->delay_compensation;
         }
         break;
         case OPUS_GET_FINAL_RANGE_REQUEST:
@@ -967,12 +1275,41 @@ int opus_encoder_ctl(OpusEncoder *st, int request, ...)
             *value = st->rangeFinal;
         }
         break;
+        case OPUS_RESET_STATE:
+        {
+           void *silk_enc;
+           silk_EncControlStruct dummy;
+           silk_enc = (char*)st+st->silk_enc_offset;
+
+           OPUS_CLEAR((char*)&st->OPUS_ENCODER_RESET_START,
+                 sizeof(OpusEncoder)-
+                 ((char*)&st->OPUS_ENCODER_RESET_START - (char*)st));
+
+           celt_encoder_ctl(celt_enc, OPUS_RESET_STATE);
+           silk_InitEncoder( silk_enc, &dummy );
+           st->stream_channels = st->channels;
+           st->hybrid_stereo_width_Q14             = 1 << 14;
+           st->first = 1;
+           st->mode = MODE_HYBRID;
+           st->bandwidth = OPUS_BANDWIDTH_FULLBAND;
+           st->variable_HP_smth2_Q15 = silk_LSHIFT( silk_lin2log( VARIABLE_HP_MIN_CUTOFF_HZ ), 8 );
+        }
+        break;
+        case OPUS_SET_FORCE_MODE_REQUEST:
+        {
+            opus_int32 value = va_arg(ap, opus_int32);
+            if ((value < MODE_SILK_ONLY || value > MODE_CELT_ONLY) && value != OPUS_AUTO)
+               goto bad_arg;
+            st->user_forced_mode = value;
+        }
+        break;
         default:
-            fprintf(stderr, "unknown opus_encoder_ctl() request: %d", request);
+            /* fprintf(stderr, "unknown opus_encoder_ctl() request: %d", request);*/
+            ret = OPUS_UNIMPLEMENTED;
             break;
     }
     va_end(ap);
-    return OPUS_OK;
+    return ret;
 bad_arg:
     va_end(ap);
     return OPUS_BAD_ARG;
@@ -980,5 +1317,5 @@ bad_arg:
 
 void opus_encoder_destroy(OpusEncoder *st)
 {
-    free(st);
+    opus_free(st);
 }