Implements OPUS_RESET_STATE for the encoder (untested as well)
[opus.git] / src / opus_encoder.c
index 8d4d0e4..a2b3f1e 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2010 Xiph.Org Foundation, Skype Limited
+/* Copyright (c) 2010-2011 Xiph.Org Foundation, Skype Limited
    Written by Jean-Marc Valin and Koen Vos */
 /*
    Redistribution and use in source and binary forms, with or without
 #include "config.h"
 #endif
 
-#include <stdlib.h>
-#include <stdio.h>
 #include <stdarg.h>
 #include "celt.h"
-#include "opus_encoder.h"
 #include "entenc.h"
 #include "modes.h"
-#include "SKP_Silk_SDK_API.h"
+#include "silk_API.h"
+#include "stack_alloc.h"
+#include "float_cast.h"
+#include "opus.h"
+#include "arch.h"
+#include "opus_private.h"
+#include "os_support.h"
 
-OpusEncoder *opus_encoder_create(int Fs, int channels)
+#ifdef FIXED_POINT
+#define celt_encode_native celt_encode
+#else
+#define celt_encode_native celt_encode_float
+#endif
+
+#define MAX_ENCODER_BUFFER 480
+
+struct OpusEncoder {
+    int          celt_enc_offset;
+    int          silk_enc_offset;
+    silk_EncControlStruct silk_mode;
+    int          application;
+    int          channels;
+    int          delay_compensation;
+    int          force_mono;
+    int          signal_type;
+    int          user_bandwidth;
+    int          voice_ratio;
+    int          Fs;
+    int          use_vbr;
+    int          vbr_constraint;
+    int          bitrate_bps;
+    int          user_bitrate_bps;
+    int          encoder_buffer;
+
+#define OPUS_ENCODER_RESET_START stream_channels
+    int          stream_channels;
+    int          hybrid_stereo_width_Q14;
+    int          mode;
+    int          prev_mode;
+    int          bandwidth;
+    /* Sampling rate (at the API level) */
+    int          first;
+    opus_val16   delay_buffer[MAX_ENCODER_BUFFER*2];
+
+    int          rangeFinal;
+};
+
+/* Transition tables for the voice and audio modes. 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 int audio_bandwidth_thresholds[10] = {
+        30000,    0, /* MB not allowed */
+        20000, 2000, /* MB<->WB */
+        26000, 2000, /* WB<->SWB */
+        33000, 2000, /* SWB<->FB */
+};
+
+
+int opus_encoder_get_size(int channels)
+{
+    int silkEncSizeBytes, celtEncSizeBytes;
+    int ret;
+    ret = silk_Get_Encoder_Size( &silkEncSizeBytes );
+    if (ret)
+        return 0;
+    silkEncSizeBytes = align(silkEncSizeBytes);
+    celtEncSizeBytes = celt_encoder_get_size(channels);
+    return align(sizeof(OpusEncoder))+silkEncSizeBytes+celtEncSizeBytes;
+}
+
+int opus_encoder_init(OpusEncoder* st, int Fs, int channels, int application)
 {
+    void *silk_enc;
+    CELTEncoder *celt_enc;
     int err;
-    char *raw_state;
-       OpusEncoder *st;
-       int ret, silkEncSizeBytes, celtEncSizeBytes;
+    int ret, silkEncSizeBytes;
+
+    if (channels > 2 || channels < 1)
+        return OPUS_BAD_ARG;
+    if (application < OPUS_APPLICATION_VOIP || application > OPUS_APPLICATION_AUDIO)
+        return OPUS_BAD_ARG;
+    if (Fs != 8000 && Fs != 12000 && Fs != 16000 && Fs != 24000 && Fs != 48000)
+        return OPUS_BAD_ARG;
 
+    OPUS_CLEAR((char*)st, opus_encoder_get_size(channels));
     /* Create SILK encoder */
-    ret = SKP_Silk_SDK_Get_Encoder_Size( &silkEncSizeBytes );
-    if( ret ) {
-       /* Handle error */
-    }
-    celtEncSizeBytes = celt_encoder_get_size(channels);
-    raw_state = calloc(sizeof(OpusEncoder)+silkEncSizeBytes+celtEncSizeBytes, 1);
-    st = (OpusEncoder*)raw_state;
-    st->silk_enc = (void*)(raw_state+sizeof(OpusEncoder));
-    st->celt_enc = (CELTEncoder*)(raw_state+sizeof(OpusEncoder)+silkEncSizeBytes);
+    ret = silk_Get_Encoder_Size( &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;
+    celt_enc = (CELTEncoder*)((char*)st+st->celt_enc_offset);
+
     st->stream_channels = st->channels = channels;
 
     st->Fs = Fs;
 
-    ret = SKP_Silk_SDK_InitEncoder( st->silk_enc, &st->silk_mode );
-    if( ret ) {
-        /* Handle error */
-    }
+    ret = silk_InitEncoder( silk_enc, &st->silk_mode );
+    if (ret)
+        goto failure;
 
     /* default SILK parameters */
-    st->silk_mode.API_sampleRate        = st->Fs;
-    st->silk_mode.nChannels             = channels;
-    st->silk_mode.maxInternalSampleRate = 16000;
-    st->silk_mode.minInternalSampleRate = 8000;
-    st->silk_mode.payloadSize_ms        = 20;
-    st->silk_mode.packetLossPercentage  = 0;
-    st->silk_mode.useInBandFEC          = 0;
-    st->silk_mode.useDTX                = 0;
-    st->silk_mode.complexity            = 10;
+    st->silk_mode.nChannelsAPI              = channels;
+    st->silk_mode.nChannelsInternal         = channels;
+    st->silk_mode.API_sampleRate            = st->Fs;
+    st->silk_mode.maxInternalSampleRate     = 16000;
+    st->silk_mode.minInternalSampleRate     = 8000;
+    st->silk_mode.desiredInternalSampleRate = 16000;
+    st->silk_mode.payloadSize_ms            = 20;
+    st->silk_mode.bitRate                   = 25000;
+    st->silk_mode.packetLossPercentage      = 0;
+    st->silk_mode.complexity                = 10;
+    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 */
-       st->celt_enc = celt_encoder_init(st->celt_enc, Fs, channels, &err);
-    celt_encoder_ctl(st->celt_enc, CELT_SET_SIGNALLING(0));
-
-       st->mode = MODE_HYBRID;
-       st->bandwidth = BANDWIDTH_FULLBAND;
-       st->use_vbr = 0;
-       st->bitrate_bps = 32000;
-       st->user_mode = OPUS_MODE_AUTO;
-       st->user_bandwidth = BANDWIDTH_AUTO;
-       st->voice_ratio = 90;
-       st->first = 1;
-
-       st->encoder_buffer = st->Fs/100;
-       st->delay_compensation = st->Fs/400;
-       if (st->Fs > 16000)
-               st->delay_compensation += 10;
-       return st;
+    /* Initialize CELT encoder */
+    celt_encoder_init(celt_enc, Fs, channels, &err);
+    if (err != CELT_OK)
+        goto failure;
+    celt_encoder_ctl(celt_enc, CELT_SET_SIGNALLING(0));
+
+    st->use_vbr = 0;
+    st->user_bitrate_bps = OPUS_BITRATE_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->encoder_buffer = st->Fs/100;
+
+    st->delay_compensation = st->Fs/400;
+    /* This part is meant to compensate for the resampler delay as a function
+       of the API sampling rate */
+    if (st->Fs == 48000)
+        st->delay_compensation += 23;
+    else if (st->Fs == 24000)
+       st->delay_compensation += 15;
+    else
+       st->delay_compensation += 2;
+
+    st->hybrid_stereo_width_Q14             = 1 << 14;
+    st->first = 1;
+    st->mode = MODE_HYBRID;
+    st->bandwidth = OPUS_BANDWIDTH_FULLBAND;
+
+    return OPUS_OK;
+
+failure:
+    opus_free(st);
+    return OPUS_INTERNAL_ERROR;
 }
 
-int opus_encode(OpusEncoder *st, const short *pcm, int frame_size,
-               unsigned char *data, int max_data_bytes)
+static unsigned char gen_toc(int mode, int framerate, int bandwidth, int silk_bandwidth, int channels)
+{
+   int period;
+   unsigned char toc;
+   period = 0;
+   while (framerate < 400)
+   {
+       framerate <<= 1;
+       period++;
+   }
+   if (mode == MODE_SILK_ONLY)
+   {
+       toc = (silk_bandwidth-OPUS_BANDWIDTH_NARROWBAND)<<5;
+       toc |= (period-2)<<3;
+   } else if (mode == MODE_CELT_ONLY)
+   {
+       int tmp = bandwidth-OPUS_BANDWIDTH_MEDIUMBAND;
+       if (tmp < 0)
+           tmp = 0;
+       toc = 0x80;
+       toc |= tmp << 5;
+       toc |= period<<3;
+   } else /* Hybrid */
+   {
+       toc = 0x60;
+       toc |= (bandwidth-OPUS_BANDWIDTH_SUPERWIDEBAND)<<4;
+       toc |= (period-2)<<3;
+   }
+   toc |= (channels==2)<<2;
+   return toc;
+}
+OpusEncoder *opus_encoder_create(int Fs, int channels, int mode, int *error)
 {
+   int ret;
+   OpusEncoder *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, mode);
+   if (error)
+      *error = ret;
+   if (ret != OPUS_OK)
+   {
+      opus_free(st);
+      st = NULL;
+   }
+   return st;
+}
+#ifdef FIXED_POINT
+int opus_encode(OpusEncoder *st, const opus_val16 *pcm, int frame_size,
+                unsigned char *data, int max_data_bytes)
+#else
+int opus_encode_float(OpusEncoder *st, const opus_val16 *pcm, int frame_size,
+                      unsigned char *data, int max_data_bytes)
+#endif
+{
+    void *silk_enc;
+    CELTEncoder *celt_enc;
     int i;
-       int ret=0;
-       SKP_int32 nBytes;
-       ec_enc enc;
-       int framerate, period;
-    int silk_internal_bandwidth;
+    int ret=0;
+    int nBytes;
+    ec_enc enc;
+    int silk_internal_bandwidth=-1;
     int bytes_target;
     int prefill=0;
     int start_band = 0;
     int redundancy = 0;
     int redundancy_bytes = 0;
     int celt_to_silk = 0;
-    /* TODO: This is 60 only so we can handle 60ms speech/audio switching 
-       it shouldn't bee too hard to reduce to 20 ms if needed */
-    short pcm_buf[3*960*2];
+    VARDECL(opus_val16, pcm_buf);
     int nb_compr_bytes;
     int to_celt = 0;
-    celt_int32 mono_rate;
+    opus_int32 mono_rate;
+    opus_uint32 redundant_rng = 0;
+    ALLOC_STACK;
 
-    /* Rete-dependent mono-stereo decision */
-    if (st->channels == 2)
+    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)
     {
-        celt_int32 decision_rate;
+       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;
+    else
+        st->bitrate_bps = st->user_bitrate_bps;
+
+    /* 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)
@@ -133,129 +308,184 @@ int opus_encode(OpusEncoder *st, const short *pcm, int frame_size,
         else
             st->stream_channels = 1;
     } else {
-        st->stream_channels = 1;
+            st->stream_channels = st->channels;
     }
+
+#ifdef FUZZING
+    if (st->channels == 2 && (rand()&0x1F)==0)
+       st->stream_channels = 3-st->stream_channels;
+#endif
+
     /* Equivalent bit-rate for mono */
     mono_rate = st->bitrate_bps;
     if (st->stream_channels==2)
-        mono_rate = (mono_rate+10000)/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);
 
-    /* Mode selection */
-    if (st->user_mode==OPUS_MODE_AUTO)
+#ifdef FUZZING
+    if ((rand()&0xF)==0)
     {
-        celt_int32 decision_rate;
+       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
+    /* Mode selection depending on application and signal type */
+    if (st->application==OPUS_APPLICATION_VOIP)
+    {
+        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;
+        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 */
-        decision_rate = mono_rate - 3*st->voice_ratio*st->voice_ratio;
+        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)
+            threshold += 8000;
+
         /* Hysteresis */
         if (st->prev_mode == MODE_CELT_ONLY)
-            decision_rate += 4000;
+            threshold -= 4000;
         else if (st->prev_mode>0)
-            decision_rate -= 4000;
-        if (decision_rate>24000)
+            threshold += 4000;
+
+        if (mono_rate>threshold)
             st->mode = MODE_CELT_ONLY;
         else
             st->mode = MODE_SILK_ONLY;
-    } else if (st->user_mode==OPUS_MODE_VOICE)
+    }
+#endif
+    /* 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->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)))
     {
-        st->mode = MODE_SILK_ONLY;
-    } else {/* OPUS_AUDIO_MODE */
-        st->mode = MODE_CELT_ONLY;
+        redundancy = 1;
+        celt_to_silk = (st->mode != MODE_CELT_ONLY);
+        if (!celt_to_silk)
+        {
+            /* Switch to SILK/hybrid if frame size is 10 ms or more*/
+            if (frame_size >= st->Fs/100)
+            {
+                st->mode = st->prev_mode;
+                to_celt = 1;
+            } else {
+                redundancy=0;
+            }
+        }
+    }
+    if (st->mode != MODE_CELT_ONLY && st->prev_mode == MODE_CELT_ONLY)
+    {
+        silk_EncControlStruct dummy;
+        silk_InitEncoder( silk_enc, &dummy);
+        prefill=1;
     }
 
     /* Automatic (rate-dependent) bandwidth selection */
-    if (st->mode == MODE_CELT_ONLY)
-    {
-       if (mono_rate>35000 || (mono_rate>28000 && st->bandwidth==BANDWIDTH_FULLBAND))
-               st->bandwidth = BANDWIDTH_FULLBAND;
-       else if (mono_rate>28000 || (mono_rate>24000 && st->bandwidth>=BANDWIDTH_SUPERWIDEBAND))
-               st->bandwidth = BANDWIDTH_SUPERWIDEBAND;
-       else if (mono_rate>24000 || (mono_rate>18000 && st->bandwidth>=BANDWIDTH_WIDEBAND))
-               st->bandwidth = BANDWIDTH_WIDEBAND;
-       else
-               st->bandwidth = BANDWIDTH_NARROWBAND;
-    } else if (st->first || st->silk_mode.allowBandwidthSwitch)
+    if (st->mode == MODE_CELT_ONLY || st->first || st->silk_mode.allowBandwidthSwitch)
     {
-       if (mono_rate>30000 || (mono_rate>26000 && st->bandwidth==BANDWIDTH_FULLBAND))
-               st->bandwidth = BANDWIDTH_FULLBAND;
-       else if (mono_rate>22000 || (mono_rate>18000 && st->bandwidth>=BANDWIDTH_SUPERWIDEBAND))
-               st->bandwidth = BANDWIDTH_SUPERWIDEBAND;
-       else if (mono_rate>16000 || (mono_rate>13000 && st->bandwidth>=BANDWIDTH_WIDEBAND))
-               st->bandwidth = BANDWIDTH_WIDEBAND;
-       else if (mono_rate>13000 || (mono_rate>10000 && st->bandwidth>=BANDWIDTH_MEDIUMBAND))
-               st->bandwidth = BANDWIDTH_MEDIUMBAND;
-       else
-               st->bandwidth = BANDWIDTH_NARROWBAND;
-       /* Prevents any transition to SWB/FB until the SILK layer has fully
-          switched to WB mode and turned the variable LP filter off */
-       if (!st->silk_mode.inWBmodeWithoutVariableLP && st->bandwidth > BANDWIDTH_WIDEBAND)
-               st->bandwidth = BANDWIDTH_WIDEBAND;
+        const int *bandwidth_thresholds;
+        int bandwidth = OPUS_BANDWIDTH_FULLBAND;
+
+        bandwidth_thresholds = st->mode == MODE_CELT_ONLY ? audio_bandwidth_thresholds : voice_bandwidth_thresholds;
+        do {
+            int threshold, hysteresis;
+            threshold = bandwidth_thresholds[2*(bandwidth-OPUS_BANDWIDTH_MEDIUMBAND)];
+            hysteresis = bandwidth_thresholds[2*(bandwidth-OPUS_BANDWIDTH_MEDIUMBAND)+1];
+            if (!st->first)
+            {
+                if (st->bandwidth >= bandwidth)
+                    threshold -= hysteresis;
+                else
+                    threshold += hysteresis;
+            }
+            if (mono_rate >= threshold)
+                break;
+        } while (--bandwidth>OPUS_BANDWIDTH_NARROWBAND);
+        st->bandwidth = bandwidth;
+        /* Prevents any transition to SWB/FB until the SILK layer has fully
+           switched to WB mode and turned the variable LP filter off */
+        if (st->mode != MODE_CELT_ONLY && !st->silk_mode.inWBmodeWithoutVariableLP && st->bandwidth > OPUS_BANDWIDTH_WIDEBAND)
+            st->bandwidth = OPUS_BANDWIDTH_WIDEBAND;
     }
 
     /* Prevents Opus from wasting bits on frequencies that are above
        the Nyquist rate of the input signal */
-    if (st->Fs <= 24000 && st->bandwidth > BANDWIDTH_SUPERWIDEBAND)
-       st->bandwidth = BANDWIDTH_SUPERWIDEBAND;
-    if (st->Fs <= 16000 && st->bandwidth > BANDWIDTH_WIDEBAND)
-       st->bandwidth = BANDWIDTH_WIDEBAND;
-    if (st->Fs <= 12000 && st->bandwidth > BANDWIDTH_MEDIUMBAND)
-       st->bandwidth = BANDWIDTH_MEDIUMBAND;
-    if (st->Fs <= 8000 && st->bandwidth > BANDWIDTH_NARROWBAND)
-       st->bandwidth = BANDWIDTH_NARROWBAND;
-
-    if (st->user_bandwidth != BANDWIDTH_AUTO)
-       st->bandwidth = st->user_bandwidth;
-
-    /* Prevents nonsensical configurations, i.e. modes that don't exist */
-    if (frame_size < st->Fs/100 && st->mode != MODE_CELT_ONLY)
-        st->mode = MODE_CELT_ONLY;
-    if (frame_size > st->Fs/50 && st->mode != MODE_SILK_ONLY)
-        st->mode = MODE_SILK_ONLY;
-    if (st->mode == MODE_CELT_ONLY && st->bandwidth == BANDWIDTH_MEDIUMBAND)
-        st->bandwidth = BANDWIDTH_WIDEBAND;
-    if (st->mode == MODE_SILK_ONLY && st->bandwidth > BANDWIDTH_WIDEBAND)
+    if (st->Fs <= 24000 && st->bandwidth > OPUS_BANDWIDTH_SUPERWIDEBAND)
+        st->bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND;
+    if (st->Fs <= 16000 && st->bandwidth > OPUS_BANDWIDTH_WIDEBAND)
+        st->bandwidth = OPUS_BANDWIDTH_WIDEBAND;
+    if (st->Fs <= 12000 && st->bandwidth > OPUS_BANDWIDTH_MEDIUMBAND)
+        st->bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
+    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;
+
+    /* 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 */
+    if (st->mode == MODE_SILK_ONLY && st->bandwidth > OPUS_BANDWIDTH_WIDEBAND)
         st->mode = MODE_HYBRID;
-    if (st->mode == MODE_HYBRID && st->bandwidth <= BANDWIDTH_WIDEBAND)
+    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;
-
-       data += 1;
-       if (st->mode != MODE_CELT_ONLY && st->prev_mode == MODE_CELT_ONLY)
-       {
-               SKP_SILK_SDK_EncControlStruct dummy;
-               SKP_Silk_SDK_InitEncoder( st->silk_enc, &dummy);
-               prefill=1;
-       }
-       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)))
-       {
-           redundancy = 1;
-           celt_to_silk = (st->mode != MODE_CELT_ONLY);
-           if (!celt_to_silk)
-           {
-               /* Switch to SILK/hybrid if frame size is 10 ms or more*/
-               if (frame_size >= st->Fs/100)
-               {
-                       st->mode = st->prev_mode;
-                       to_celt = 1;
-               } else {
-                       redundancy=0;
-               }
-           }
-       }
-
-       ec_enc_init(&enc, data, max_data_bytes-1);
-
-       /* SILK processing */
+    bytes_target = st->bitrate_bps * frame_size / (st->Fs * 8) - 1;
+
+    data += 1;
+
+    ec_enc_init(&enc, data, max_data_bytes-1);
+
+    /* SILK processing */
     if (st->mode != MODE_CELT_ONLY)
     {
+#ifdef FIXED_POINT
+       const opus_int16 *pcm_silk;
+#else
+       VARDECL(opus_int16, pcm_silk);
+       ALLOC(pcm_silk, st->channels*frame_size, opus_int16);
+#endif
         st->silk_mode.bitRate = st->bitrate_bps - 8*st->Fs/frame_size;
         if( st->mode == MODE_HYBRID ) {
-            if( st->bandwidth == BANDWIDTH_SUPERWIDEBAND ) {
+            st->silk_mode.bitRate /= st->stream_channels;
+            if( st->bandwidth == OPUS_BANDWIDTH_SUPERWIDEBAND ) {
                 if( st->Fs == 100 * frame_size ) {
                     /* 24 kHz, 10 ms */
                     st->silk_mode.bitRate = ( ( st->silk_mode.bitRate + 2000 + st->use_vbr * 1000 ) * 2 ) / 3;
@@ -272,6 +502,7 @@ int opus_encode(OpusEncoder *st, const short *pcm, int frame_size,
                     st->silk_mode.bitRate = ( st->silk_mode.bitRate + 9000 + st->use_vbr * 1000 ) / 2;
                 }
             }
+            st->silk_mode.bitRate *= st->stream_channels;
             /* don't let SILK use more than 80% */
             if( st->silk_mode.bitRate > ( st->bitrate_bps - 8*st->Fs/frame_size ) * 4/5 ) {
                 st->silk_mode.bitRate = ( st->bitrate_bps - 8*st->Fs/frame_size ) * 4/5;
@@ -279,12 +510,14 @@ int opus_encode(OpusEncoder *st, const short *pcm, int frame_size,
         }
 
         st->silk_mode.payloadSize_ms = 1000 * frame_size / st->Fs;
-        if (st->bandwidth == BANDWIDTH_NARROWBAND) {
-               st->silk_mode.desiredInternalSampleRate = 8000;
-        } else if (st->bandwidth == BANDWIDTH_MEDIUMBAND) {
-               st->silk_mode.desiredInternalSampleRate = 12000;
+        st->silk_mode.nChannelsAPI = st->channels;
+        st->silk_mode.nChannelsInternal = st->stream_channels;
+        if (st->bandwidth == OPUS_BANDWIDTH_NARROWBAND) {
+            st->silk_mode.desiredInternalSampleRate = 8000;
+        } else if (st->bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) {
+            st->silk_mode.desiredInternalSampleRate = 12000;
         } else {
-            SKP_assert( st->mode == MODE_HYBRID || st->bandwidth == BANDWIDTH_WIDEBAND );
+            SKP_assert( st->mode == MODE_HYBRID || st->bandwidth == OPUS_BANDWIDTH_WIDEBAND );
             st->silk_mode.desiredInternalSampleRate = 16000;
         }
         if( st->mode == MODE_HYBRID ) {
@@ -296,28 +529,45 @@ int opus_encode(OpusEncoder *st, const short *pcm, int frame_size,
         st->silk_mode.maxInternalSampleRate = 16000;
 
         /* Call SILK encoder for the low band */
-        nBytes = max_data_bytes-1;
+        nBytes = IMIN(1275, max_data_bytes-1);
         if (prefill)
         {
             int zero=0;
-               SKP_Silk_SDK_Encode( st->silk_enc, &st->silk_mode, st->delay_buffer, st->encoder_buffer, NULL, &zero, 1 );
+#ifdef FIXED_POINT
+            pcm_silk = st->delay_buffer;
+#else
+            for (i=0;i<st->encoder_buffer*st->channels;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 );
         }
 
-        ret = SKP_Silk_SDK_Encode( st->silk_enc, &st->silk_mode, pcm, frame_size, &enc, &nBytes, 0 );
+#ifdef FIXED_POINT
+        pcm_silk = pcm;
+#else
+        for (i=0;i<frame_size*st->channels;i++)
+            pcm_silk[i] = FLOAT2INT16(pcm[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)
-            return 0;
+        {
+           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 */
         if( st->mode == MODE_SILK_ONLY ) {
             if( st->silk_mode.internalSampleRate == 8000 ) {
-                silk_internal_bandwidth = BANDWIDTH_NARROWBAND;
+                silk_internal_bandwidth = OPUS_BANDWIDTH_NARROWBAND;
             } else if( st->silk_mode.internalSampleRate == 12000 ) {
-                silk_internal_bandwidth = BANDWIDTH_MEDIUMBAND;
+                silk_internal_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
             } else if( st->silk_mode.internalSampleRate == 16000 ) {
-                silk_internal_bandwidth = BANDWIDTH_WIDEBAND;
+                silk_internal_bandwidth = OPUS_BANDWIDTH_WIDEBAND;
             }
         } else {
             SKP_assert( st->silk_mode.internalSampleRate == 16000 );
@@ -325,41 +575,42 @@ int opus_encode(OpusEncoder *st, const short *pcm, int frame_size,
     }
 
     /* CELT processing */
-       {
-           int endband=21;
-
-           switch(st->bandwidth)
-           {
-           case BANDWIDTH_NARROWBAND:
-               endband = 13;
-               break;
-           case BANDWIDTH_WIDEBAND:
-               endband = 17;
-               break;
-           case BANDWIDTH_SUPERWIDEBAND:
-               endband = 19;
-               break;
-           case BANDWIDTH_FULLBAND:
-               endband = 21;
-               break;
-           }
-           celt_encoder_ctl(st->celt_enc, CELT_SET_END_BAND(endband));
-           celt_encoder_ctl(st->celt_enc, CELT_SET_CHANNELS(st->stream_channels));
-       }
-       if (st->mode != MODE_SILK_ONLY)
-       {
-        celt_encoder_ctl(st->celt_enc, CELT_SET_VBR(0));
-        celt_encoder_ctl(st->celt_enc, CELT_SET_BITRATE(510000));
+    {
+        int endband=21;
+
+        switch(st->bandwidth)
+        {
+            case OPUS_BANDWIDTH_NARROWBAND:
+                endband = 13;
+                break;
+            case OPUS_BANDWIDTH_MEDIUMBAND:
+            case OPUS_BANDWIDTH_WIDEBAND:
+                endband = 17;
+                break;
+            case OPUS_BANDWIDTH_SUPERWIDEBAND:
+                endband = 19;
+                break;
+            case OPUS_BANDWIDTH_FULLBAND:
+                endband = 21;
+                break;
+        }
+        celt_encoder_ctl(celt_enc, CELT_SET_END_BAND(endband));
+        celt_encoder_ctl(celt_enc, CELT_SET_CHANNELS(st->stream_channels));
+    }
+    if (st->mode != MODE_SILK_ONLY)
+    {
+        celt_encoder_ctl(celt_enc, CELT_SET_VBR(0));
+        celt_encoder_ctl(celt_enc, CELT_SET_BITRATE(-1));
         if (st->prev_mode == MODE_SILK_ONLY)
         {
-               unsigned char dummy[10];
-               celt_encoder_ctl(st->celt_enc, CELT_RESET_STATE);
-               celt_encoder_ctl(st->celt_enc, CELT_SET_START_BAND(0));
-               celt_encoder_ctl(st->celt_enc, CELT_SET_PREDICTION(0));
-               /* FIXME: This wastes CPU a bit compared to just prefilling the buffer */
-               celt_encode(st->celt_enc, &st->delay_buffer[(st->encoder_buffer-st->delay_compensation-st->Fs/400)*st->channels], st->Fs/400, dummy, 10);
+            unsigned char dummy[10];
+            celt_encoder_ctl(celt_enc, CELT_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);
         } else {
-               celt_encoder_ctl(st->celt_enc, CELT_SET_PREDICTION(2));
+            celt_encoder_ctl(celt_enc, CELT_SET_PREDICTION(2));
         }
 
         if (st->mode == MODE_HYBRID)
@@ -376,34 +627,61 @@ int opus_encode(OpusEncoder *st, const short *pcm, int frame_size,
         } else {
             if (st->use_vbr)
             {
-                celt_encoder_ctl(st->celt_enc, CELT_SET_VBR(1));
-                celt_encoder_ctl(st->celt_enc, CELT_SET_VBR_CONSTRAINT(st->vbr_constraint));
-                celt_encoder_ctl(st->celt_enc, CELT_SET_BITRATE(st->bitrate_bps));
+                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));
                 nb_compr_bytes = max_data_bytes-1;
             } else {
                 nb_compr_bytes = bytes_target;
             }
         }
 
-        ec_enc_shrink(&enc, nb_compr_bytes);
-       } else {
-           nb_compr_bytes = 0;
-       }
+    } else {
+        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];
+
+    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) ) {
+            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 );
+            }
+            st->hybrid_stereo_width_Q14 = st->silk_mode.stereoWidth_Q14;
+        }
+    }
+
     if (st->mode != MODE_CELT_ONLY)
     {
         /* Check if we have a redundant 0-8 kHz band */
         ec_enc_bit_logp(&enc, redundancy, 12);
         if (redundancy)
         {
-            redundancy_bytes = st->stream_channels*st->bitrate_bps/1600;
+            /* 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);
             if (st->mode == MODE_HYBRID)
-               ec_enc_uint(&enc, redundancy_bytes-2, 256);
+                ec_enc_uint(&enc, redundancy_bytes-2, 256);
         }
         start_band = 17;
     }
@@ -412,26 +690,37 @@ int opus_encode(OpusEncoder *st, const short *pcm, int frame_size,
     {
         ret = (ec_tell(&enc)+7)>>3;
         ec_enc_done(&enc);
+        /*When in LPC only mode it's perfectly
+          reasonable to strip off trailing zero bytes as
+          the required range decoder behavior is to
+          fill these in. This can't be done when the MDCT
+          modes are used because the decoder needs to know
+          the actual length for allocation purposes.*/
+        if(!redundancy)
+            while(ret>2&&data[ret-1]==0)ret--;
         nb_compr_bytes = ret;
+    } else {
+       nb_compr_bytes = IMIN(1275-redundancy_bytes, nb_compr_bytes);
+       ec_enc_shrink(&enc, nb_compr_bytes);
     }
 
+
     /* 5 ms redundant frame for CELT->SILK */
     if (redundancy && celt_to_silk)
     {
-        celt_encoder_ctl(st->celt_enc, CELT_SET_START_BAND(0));
-        /* FIXME: That's OK for now, but we need to set the flags properly */
-        celt_encoder_ctl(st->celt_enc, CELT_SET_VBR(0));
-        celt_encode(st->celt_enc, pcm_buf, st->Fs/200, data+nb_compr_bytes, redundancy_bytes);
-        celt_encoder_ctl(st->celt_enc, CELT_RESET_STATE);
+        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(st->celt_enc, CELT_SET_START_BAND(start_band));
+    celt_encoder_ctl(celt_enc, CELT_SET_START_BAND(start_band));
 
     if (st->mode != MODE_SILK_ONLY)
-       {
-           /* Encode high band with CELT */
-           ret = celt_encode_with_ec(st->celt_enc, pcm_buf, frame_size, NULL, nb_compr_bytes, &enc);
-       }
+    {
+        ret = celt_encode_with_ec(celt_enc, pcm_buf, frame_size, NULL, nb_compr_bytes, &enc);
+    }
 
     /* 5 ms redundant frame for SILK->CELT */
     if (redundancy && !celt_to_silk)
@@ -440,111 +729,149 @@ int opus_encode(OpusEncoder *st, const short *pcm, int frame_size,
         N2 = st->Fs/200;
         N4 = st->Fs/400;
 
-        celt_encoder_ctl(st->celt_enc, CELT_RESET_STATE);
-        celt_encoder_ctl(st->celt_enc, CELT_SET_START_BAND(0));
-        celt_encoder_ctl(st->celt_enc, CELT_SET_PREDICTION(0));
+        celt_encoder_ctl(celt_enc, CELT_RESET_STATE);
+        celt_encoder_ctl(celt_enc, CELT_SET_START_BAND(0));
+        celt_encoder_ctl(celt_enc, CELT_SET_PREDICTION(0));
 
-        /* FIXME: Do proper prefilling here */
-        celt_encode(st->celt_enc, pcm_buf+st->channels*(frame_size-N2-N4), N4, data+nb_compr_bytes, redundancy_bytes);
+        /* 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);
 
-        celt_encode(st->celt_enc, pcm_buf+st->channels*(frame_size-N2), N2, data+nb_compr_bytes, redundancy_bytes);
+        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));
     }
 
 
     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];
+        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];
+        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--;
-       framerate = st->Fs/frame_size;
-       period = 0;
-       while (framerate < 400)
-       {
-           framerate <<= 1;
-           period++;
-       }
-    if (st->mode == MODE_SILK_ONLY)
-    {
-        data[0] = (silk_internal_bandwidth-BANDWIDTH_NARROWBAND)<<5;
-        data[0] |= (period-2)<<3;
-    } else if (st->mode == MODE_CELT_ONLY)
-    {
-        int tmp = st->bandwidth-BANDWIDTH_MEDIUMBAND;
-        if (tmp < 0)
-            tmp = 0;
-        data[0] = 0x80;
-        data[0] |= tmp << 5;
-        data[0] |= period<<3;
-    } else /* Hybrid */
-    {
-        data[0] = 0x60;
-        data[0] |= (st->bandwidth-BANDWIDTH_SUPERWIDEBAND)<<4;
-        data[0] |= (period-2)<<3;
-    }
-    data[0] |= (st->stream_channels==2)<<2;
-    /*printf ("%x\n", (int)data[0]);*/
+    /* Signalling the mode in the first byte */
+    data--;
+    data[0] = gen_toc(st->mode, st->Fs/frame_size, st->bandwidth, silk_internal_bandwidth, st->stream_channels);
+
+    st->rangeFinal = enc.rng ^ redundant_rng;
 
-#if OPUS_TEST_RANGE_CODER_STATE
-    st->rangeFinal = enc.rng;
-#endif
     if (to_celt)
         st->prev_mode = MODE_CELT_ONLY;
     else
         st->prev_mode = st->mode;
     st->first = 0;
+    RESTORE_STACK;
     return ret+1+redundancy_bytes;
 }
 
+#ifdef FIXED_POINT
+
+#ifndef DISABLE_FLOAT_API
+int opus_encode_float(OpusEncoder *st, const float *pcm, int frame_size,
+      unsigned char *data, int max_data_bytes)
+{
+   int i, ret;
+   VARDECL(opus_int16, in);
+   ALLOC_STACK;
+
+   ALLOC(in, frame_size*st->channels, opus_int16);
+
+   for (i=0;i<frame_size*st->channels;i++)
+      in[i] = FLOAT2INT16(pcm[i]);
+   ret = opus_encode(st, in, frame_size, data, max_data_bytes);
+   RESTORE_STACK;
+   return ret;
+}
+#endif
+
+#else
+int opus_encode(OpusEncoder *st, const opus_int16 *pcm, int frame_size,
+      unsigned char *data, int max_data_bytes)
+{
+   int i, ret;
+   VARDECL(float, in);
+   ALLOC_STACK;
+
+   ALLOC(in, frame_size*st->channels, float);
+
+   for (i=0;i<frame_size*st->channels;i++)
+      in[i] = (1./32768)*pcm[i];
+   ret = opus_encode_float(st, in, frame_size, data, max_data_bytes);
+   RESTORE_STACK;
+   return ret;
+}
+#endif
+
+
 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);
+
     switch (request)
     {
-        case OPUS_SET_MODE_REQUEST:
+        case OPUS_SET_APPLICATION_REQUEST:
         {
-            int value = va_arg(ap, int);
-            st->user_mode = value;
+            opus_int32 value = va_arg(ap, opus_int32);
+            st->application = value;
         }
         break;
-        case OPUS_GET_MODE_REQUEST:
+        case OPUS_GET_APPLICATION_REQUEST:
         {
-            int *value = va_arg(ap, int*);
+            opus_int32 *value = va_arg(ap, opus_int32*);
             *value = st->mode;
         }
         break;
         case OPUS_SET_BITRATE_REQUEST:
         {
-            int value = va_arg(ap, int);
-            st->bitrate_bps = value;
+            opus_int32 value = va_arg(ap, opus_int32);
+            if (value != OPUS_BITRATE_AUTO)
+            {
+                if (value <= 0)
+                    goto bad_arg;
+                else if (value <= 500)
+                    value = 500;
+            }
+            st->user_bitrate_bps = value;
         }
         break;
         case OPUS_GET_BITRATE_REQUEST:
         {
-            int *value = va_arg(ap, int*);
+            opus_int32 *value = va_arg(ap, opus_int32*);
             *value = st->bitrate_bps;
         }
         break;
+        case OPUS_SET_FORCE_MONO_REQUEST:
+        {
+            opus_int32 value = va_arg(ap, opus_int32);
+            st->force_mono = value;
+        }
+        break;
+        case OPUS_GET_FORCE_MONO_REQUEST:
+        {
+            opus_int32 *value = va_arg(ap, opus_int32*);
+            *value = !!st->force_mono;
+        }
+        break;
         case OPUS_SET_BANDWIDTH_REQUEST:
         {
-            int value = va_arg(ap, int);
-            if (value < BANDWIDTH_AUTO || value > BANDWIDTH_FULLBAND)
-               return OPUS_BAD_ARG;
+            opus_int32 value = va_arg(ap, opus_int32);
+            if (value < OPUS_BANDWIDTH_AUTO || value > OPUS_BANDWIDTH_FULLBAND)
+                return OPUS_BAD_ARG;
             st->user_bandwidth = value;
-            if (st->user_bandwidth == BANDWIDTH_NARROWBAND) {
+            if (st->user_bandwidth == OPUS_BANDWIDTH_NARROWBAND) {
                 st->silk_mode.maxInternalSampleRate = 8000;
-            } else if (st->bandwidth == BANDWIDTH_MEDIUMBAND) {
+            } else if (st->bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) {
                 st->silk_mode.maxInternalSampleRate = 12000;
             } else {
                 st->silk_mode.maxInternalSampleRate = 16000;
@@ -553,78 +880,78 @@ int opus_encoder_ctl(OpusEncoder *st, int request, ...)
         break;
         case OPUS_GET_BANDWIDTH_REQUEST:
         {
-            int *value = va_arg(ap, int*);
+            opus_int32 *value = va_arg(ap, opus_int32*);
             *value = st->bandwidth;
         }
         break;
-        case OPUS_SET_DTX_FLAG_REQUEST:
+        case OPUS_SET_DTX_REQUEST:
         {
-            int value = va_arg(ap, int);
+            opus_int32 value = va_arg(ap, opus_int32);
             st->silk_mode.useDTX = value;
         }
         break;
-        case OPUS_GET_DTX_FLAG_REQUEST:
+        case OPUS_GET_DTX_REQUEST:
         {
-            int *value = va_arg(ap, int*);
+            opus_int32 *value = va_arg(ap, opus_int32*);
             *value = st->silk_mode.useDTX;
         }
         break;
         case OPUS_SET_COMPLEXITY_REQUEST:
         {
-            int value = va_arg(ap, int);
+            opus_int32 value = va_arg(ap, opus_int32);
             st->silk_mode.complexity = value;
-            celt_encoder_ctl(st->celt_enc, CELT_SET_COMPLEXITY(value));
+            celt_encoder_ctl(celt_enc, CELT_SET_COMPLEXITY(value));
         }
         break;
         case OPUS_GET_COMPLEXITY_REQUEST:
         {
-            int *value = va_arg(ap, int*);
+            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:
         {
-            int value = va_arg(ap, int);
+            opus_int32 value = va_arg(ap, opus_int32);
             st->silk_mode.useInBandFEC = value;
         }
         break;
-        case OPUS_GET_INBAND_FEC_FLAG_REQUEST:
+        case OPUS_GET_INBAND_FEC_REQUEST:
         {
-            int *value = va_arg(ap, int*);
+            opus_int32 *value = va_arg(ap, opus_int32*);
             *value = st->silk_mode.useInBandFEC;
         }
         break;
         case OPUS_SET_PACKET_LOSS_PERC_REQUEST:
         {
-            int value = va_arg(ap, int);
+            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(st->celt_enc, CELT_SET_LOSS_PERC(value));
+            celt_encoder_ctl(celt_enc, CELT_SET_LOSS_PERC(value));
         }
         break;
         case OPUS_GET_PACKET_LOSS_PERC_REQUEST:
         {
-            int *value = va_arg(ap, int*);
+            opus_int32 *value = va_arg(ap, opus_int32*);
             *value = st->silk_mode.packetLossPercentage;
         }
         break;
-        case OPUS_SET_VBR_FLAG_REQUEST:
+        case OPUS_SET_VBR_REQUEST:
         {
-            int value = va_arg(ap, int);
+            opus_int32 value = va_arg(ap, opus_int32);
             st->use_vbr = value;
             st->silk_mode.useCBR = 1-value;
         }
         break;
-        case OPUS_GET_VBR_FLAG_REQUEST:
+        case OPUS_GET_VBR_REQUEST:
         {
-            int *value = va_arg(ap, int*);
+            opus_int32 *value = va_arg(ap, opus_int32*);
             *value = st->use_vbr;
         }
         break;
         case OPUS_SET_VOICE_RATIO_REQUEST:
         {
-            int value = va_arg(ap, int);
+            opus_int32 value = va_arg(ap, opus_int32);
             if (value>100 || value<0)
                 goto bad_arg;
             st->voice_ratio = value;
@@ -632,28 +959,72 @@ int opus_encoder_ctl(OpusEncoder *st, int request, ...)
         break;
         case OPUS_GET_VOICE_RATIO_REQUEST:
         {
-            int *value = va_arg(ap, int*);
+            opus_int32 *value = va_arg(ap, opus_int32*);
             *value = st->voice_ratio;
         }
         break;
         case OPUS_SET_VBR_CONSTRAINT_REQUEST:
         {
-            int value = va_arg(ap, int);
+            opus_int32 value = va_arg(ap, opus_int32);
             st->vbr_constraint = value;
         }
         break;
         case OPUS_GET_VBR_CONSTRAINT_REQUEST:
         {
-            int *value = va_arg(ap, int*);
+            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);
+            st->signal_type = value;
+        }
+        break;
+        case OPUS_GET_SIGNAL_REQUEST:
+        {
+            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;
+        }
+        break;
+        case OPUS_GET_FINAL_RANGE_REQUEST:
+        {
+            opus_uint32 *value = va_arg(ap, opus_uint32*);
+            *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,
+                 opus_encoder_get_size(st->channels)-
+                 ((char*)&st->OPUS_ENCODER_RESET_START - (char*)st));
+
+           celt_encoder_ctl(celt_enc, CELT_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;
+        }
+        break;
         default:
-            fprintf(stderr, "unknown opus_encoder_ctl() request: %d", request);
+            /* fprintf(stderr, "unknown opus_encoder_ctl() request: %d", request);*/
+            ret = OPUS_BAD_ARG;
             break;
     }
     va_end(ap);
-    return OPUS_OK;
+    return ret;
 bad_arg:
     va_end(ap);
     return OPUS_BAD_ARG;
@@ -661,12 +1032,5 @@ bad_arg:
 
 void opus_encoder_destroy(OpusEncoder *st)
 {
-       free(st);
+    opus_free(st);
 }
-
-#if OPUS_TEST_RANGE_CODER_STATE
-int opus_encoder_get_final_range(OpusEncoder *st)
-{
-    return st->rangeFinal;
-}
-#endif