Moves the main headers from src/ to include/
[opus.git] / src / opus_decoder.c
index 8f9774c..4f4d648 100644 (file)
 #include "config.h"
 #endif
 
-#include <string.h>
-#include <stdlib.h>
-#include <stdio.h>
 #include <stdarg.h>
 #include "celt.h"
 #include "opus.h"
 #include "entdec.h"
 #include "modes.h"
-#include "silk_API.h"
+#include "API.h"
 #include "stack_alloc.h"
 #include "float_cast.h"
 #include "opus_private.h"
 #include "os_support.h"
-
-#ifdef FIXED_POINT
-#define celt_decode_native celt_decode
-#else
-#define celt_decode_native celt_decode_float
-#endif
+#include "structs.h"
+#include "define.h"
 
 struct OpusDecoder {
    int          celt_dec_offset;
    int          silk_dec_offset;
    int          channels;
+   opus_int32   Fs;          /** Sampling rate (at the API level) */
+   silk_DecControlStruct DecControl;
+
+   /* Everything beyond this point gets cleared on a reset */
+#define OPUS_DECODER_RESET_START stream_channels
    int          stream_channels;
 
-    int          bandwidth;
-    /* Sampling rate (at the API level) */
-    int          Fs;
-    int          mode;
-    int          prev_mode;
-    int          frame_size;
-    int          prev_redundancy;
+   int          bandwidth;
+   int          mode;
+   int          prev_mode;
+   int          frame_size;
+   int          prev_redundancy;
 
-    int          rangeFinal;
+   opus_uint32  rangeFinal;
 };
 
 #ifdef FIXED_POINT
@@ -75,81 +71,88 @@ static inline opus_int16 SAT16(opus_int32 x) {
 
 int opus_decoder_get_size(int channels)
 {
-       int silkDecSizeBytes, celtDecSizeBytes;
-       int ret;
-    ret = silk_Get_Decoder_Size( &silkDecSizeBytes );
-       if(ret)
-               return 0;
-       silkDecSizeBytes = align(silkDecSizeBytes);
-    celtDecSizeBytes = celt_decoder_get_size(channels);
-    return align(sizeof(OpusDecoder))+silkDecSizeBytes+celtDecSizeBytes;
-
+   int silkDecSizeBytes, celtDecSizeBytes;
+   int ret;
+   if (channels<1 || channels > 2)
+      return 0;
+   ret = silk_Get_Decoder_Size( &silkDecSizeBytes );
+   if(ret)
+      return 0;
+   silkDecSizeBytes = align(silkDecSizeBytes);
+   celtDecSizeBytes = celt_decoder_get_size(channels);
+   return align(sizeof(OpusDecoder))+silkDecSizeBytes+celtDecSizeBytes;
 }
 
-int opus_decoder_init(OpusDecoder *st, int Fs, int channels)
+int opus_decoder_init(OpusDecoder *st, opus_int32 Fs, int channels)
 {
-       void *silk_dec;
-       CELTDecoder *celt_dec;
-       int ret, silkDecSizeBytes;
-
-       if (channels<1 || channels > 2)
-           return OPUS_BAD_ARG;
-       memset(st, 0, opus_decoder_get_size(channels));
-       /* Initialize SILK encoder */
-    ret = silk_Get_Decoder_Size( &silkDecSizeBytes );
-    if( ret ) {
-        return OPUS_INTERNAL_ERROR;
-    }
-    silkDecSizeBytes = align(silkDecSizeBytes);
-    st->silk_dec_offset = align(sizeof(OpusDecoder));
-    st->celt_dec_offset = st->silk_dec_offset+silkDecSizeBytes;
-    silk_dec = (char*)st+st->silk_dec_offset;
-    celt_dec = (CELTDecoder*)((char*)st+st->celt_dec_offset);
-    st->stream_channels = st->channels = channels;
+   void *silk_dec;
+   CELTDecoder *celt_dec;
+   int ret, silkDecSizeBytes;
 
-    st->Fs = Fs;
+   if((Fs!=48000&&Fs!=24000&&Fs!=16000&&Fs!=12000&&Fs!=8000)||(channels!=1&&channels!=2))
+      return OPUS_BAD_ARG;
 
-    /* Reset decoder */
-    ret = silk_InitDecoder( silk_dec );
-    if( ret ) {
-        goto failure;
-    }
+   OPUS_CLEAR((char*)st, opus_decoder_get_size(channels));
+   /* Initialize SILK encoder */
+   ret = silk_Get_Decoder_Size(&silkDecSizeBytes);
+   if(ret)return OPUS_INTERNAL_ERROR;
+
+   silkDecSizeBytes = align(silkDecSizeBytes);
+   st->silk_dec_offset = align(sizeof(OpusDecoder));
+   st->celt_dec_offset = st->silk_dec_offset+silkDecSizeBytes;
+   silk_dec = (char*)st+st->silk_dec_offset;
+   celt_dec = (CELTDecoder*)((char*)st+st->celt_dec_offset);
+   st->stream_channels = st->channels = channels;
+
+   st->Fs = Fs;
+   st->DecControl.API_sampleRate = st->Fs;
+   st->DecControl.nChannelsAPI      = st->channels;
 
-       /* Initialize CELT decoder */
-       celt_decoder_init(celt_dec, Fs, channels, &ret);
-       if (ret != CELT_OK)
-               goto failure;
-    celt_decoder_ctl(celt_dec, CELT_SET_SIGNALLING(0));
-
-       st->prev_mode = 0;
-       st->frame_size = Fs/400;
-       return OPUS_OK;
-failure:
-    opus_free(st);
-    return OPUS_INTERNAL_ERROR;
+   /* Reset decoder */
+   ret = silk_InitDecoder( silk_dec );
+   if(ret)return OPUS_INTERNAL_ERROR;
+
+   /* Initialize CELT decoder */
+   ret = celt_decoder_init(celt_dec, Fs, channels);
+   if(ret!=OPUS_OK)return OPUS_INTERNAL_ERROR;
+
+   celt_decoder_ctl(celt_dec, CELT_SET_SIGNALLING(0));
+
+   st->prev_mode = 0;
+   st->frame_size = Fs/400;
+   return OPUS_OK;
 }
 
-OpusDecoder *opus_decoder_create(int Fs, int channels, int *error)
+OpusDecoder *opus_decoder_create(opus_int32 Fs, int channels, int *error)
 {
    int ret;
-   char *raw_state = (char*)opus_alloc(opus_decoder_get_size(channels));
-   if (raw_state == NULL)
+   OpusDecoder *st;
+   if((Fs!=48000&&Fs!=24000&&Fs!=16000&&Fs!=12000&&Fs!=8000)||(channels!=1&&channels!=2))
+   {
+      if (error)
+         *error = OPUS_BAD_ARG;
+      return NULL;
+   }
+   st = (OpusDecoder *)opus_alloc(opus_decoder_get_size(channels));
+   if (st == NULL)
    {
       if (error)
          *error = OPUS_ALLOC_FAIL;
       return NULL;
    }
-   ret = opus_decoder_init((OpusDecoder*)raw_state, Fs, channels);
+   ret = opus_decoder_init(st, Fs, channels);
+   if (error)
+      *error = ret;
    if (ret != OPUS_OK)
    {
-      opus_free(raw_state);
-      raw_state = NULL;
+      opus_free(st);
+      st = NULL;
    }
-   return (OpusDecoder*)raw_state;
+   return st;
 }
 
 static void smooth_fade(const opus_val16 *in1, const opus_val16 *in2, opus_val16 *out,
-        int overlap, int channels, const opus_val16 *window, int Fs)
+        int overlap, int channels, const opus_val16 *window, opus_int32 Fs)
 {
        int i, c;
        int inc = 48000/Fs;
@@ -183,11 +186,10 @@ static int opus_packet_get_mode(const unsigned char *data)
 static int opus_decode_frame(OpusDecoder *st, const unsigned char *data,
                int len, opus_val16 *pcm, int frame_size, int decode_fec)
 {
-       void *silk_dec;
-       CELTDecoder *celt_dec;
-       int i, silk_ret=0, celt_ret=0;
-       ec_dec dec;
-    silk_DecControlStruct DecControl;
+    void *silk_dec;
+    CELTDecoder *celt_dec;
+    int i, silk_ret=0, celt_ret=0;
+    ec_dec dec;
     opus_int32 silk_frame_size;
     VARDECL(opus_int16, pcm_silk);
     VARDECL(opus_val16, pcm_transition);
@@ -219,7 +221,6 @@ static int opus_decode_frame(OpusDecoder *st, const unsigned char *data,
     {
        data = NULL;
        /* In that case, don't conceal more than what the ToC says */
-       /* FIXME: What if st->frame_size has never been set? */
        frame_size = IMIN(frame_size, st->frame_size);
     }
     if (data != NULL)
@@ -244,9 +245,10 @@ static int opus_decode_frame(OpusDecoder *st, const unsigned char *data,
 
     ALLOC(pcm_transition, F5*st->channels, opus_val16);
 
-    if (data!=NULL && !st->prev_redundancy && mode != st->prev_mode && st->prev_mode > 0
-               && !(mode == MODE_SILK_ONLY && st->prev_mode == MODE_HYBRID)
-               && !(mode == MODE_HYBRID && st->prev_mode == MODE_SILK_ONLY))
+    if (data!=NULL && st->prev_mode > 0 && (
+            (mode == MODE_CELT_ONLY && st->prev_mode != MODE_CELT_ONLY && !st->prev_redundancy)
+         || (mode != MODE_CELT_ONLY && st->prev_mode == MODE_CELT_ONLY) )
+         )
     {
        transition = 1;
        if (mode == MODE_CELT_ONLY)
@@ -254,14 +256,14 @@ static int opus_decode_frame(OpusDecoder *st, const unsigned char *data,
     }
     if (audiosize > frame_size)
     {
-        fprintf(stderr, "PCM buffer too small: %d vs %d (mode = %d)\n", audiosize, frame_size, mode);
+        /*fprintf(stderr, "PCM buffer too small: %d vs %d (mode = %d)\n", audiosize, frame_size, mode);*/
         RESTORE_STACK;
         return OPUS_BAD_ARG;
     } else {
         frame_size = audiosize;
     }
 
-    ALLOC(pcm_silk, frame_size*st->channels, opus_int16);
+    ALLOC(pcm_silk, IMAX(F10, frame_size)*st->channels, opus_int16);
     ALLOC(redundant_audio, F5*st->channels, opus_val16);
 
     /* SILK processing */
@@ -273,24 +275,27 @@ static int opus_decode_frame(OpusDecoder *st, const unsigned char *data,
         if (st->prev_mode==MODE_CELT_ONLY)
                silk_InitDecoder( silk_dec );
 
-        DecControl.API_sampleRate = st->Fs;
-        DecControl.nChannelsAPI      = st->channels;
-        DecControl.nChannelsInternal = st->stream_channels;
-        DecControl.payloadSize_ms = 1000 * audiosize / st->Fs;
-        if( mode == MODE_SILK_ONLY ) {
-            if( st->bandwidth == OPUS_BANDWIDTH_NARROWBAND ) {
-                DecControl.internalSampleRate = 8000;
-            } else if( st->bandwidth == OPUS_BANDWIDTH_MEDIUMBAND ) {
-                DecControl.internalSampleRate = 12000;
-            } else if( st->bandwidth == OPUS_BANDWIDTH_WIDEBAND ) {
-                DecControl.internalSampleRate = 16000;
+        /* The SILK PLC cannot support produce frames of less than 10 ms */
+        st->DecControl.payloadSize_ms = IMAX(10, 1000 * audiosize / st->Fs);
+
+        if (data != NULL)
+        {
+            st->DecControl.nChannelsInternal = st->stream_channels;
+            if( mode == MODE_SILK_ONLY ) {
+                if( st->bandwidth == OPUS_BANDWIDTH_NARROWBAND ) {
+                    st->DecControl.internalSampleRate = 8000;
+                } else if( st->bandwidth == OPUS_BANDWIDTH_MEDIUMBAND ) {
+                    st->DecControl.internalSampleRate = 12000;
+                } else if( st->bandwidth == OPUS_BANDWIDTH_WIDEBAND ) {
+                    st->DecControl.internalSampleRate = 16000;
+                } else {
+                    st->DecControl.internalSampleRate = 16000;
+                    silk_assert( 0 );
+                }
             } else {
-               DecControl.internalSampleRate = 16000;
-                SKP_assert( 0 );
+                /* Hybrid mode */
+                st->DecControl.internalSampleRate = 16000;
             }
-        } else {
-            /* Hybrid mode */
-            DecControl.internalSampleRate = 16000;
         }
 
         lost_flag = data == NULL ? 1 : 2 * decode_fec;
@@ -298,7 +303,7 @@ static int opus_decode_frame(OpusDecoder *st, const unsigned char *data,
         do {
             /* Call SILK decoder */
             int first_frame = decoded_samples == 0;
-            silk_ret = silk_Decode( silk_dec, &DecControl,
+            silk_ret = silk_Decode( silk_dec, &st->DecControl,
                 lost_flag, first_frame, &dec, pcm_ptr, &silk_frame_size );
             if( silk_ret ) {
                if (lost_flag) {
@@ -308,7 +313,7 @@ static int opus_decode_frame(OpusDecoder *st, const unsigned char *data,
                                pcm_ptr[i] = 0;
                } else {
                   RESTORE_STACK;
-                  return OPUS_CORRUPTED_DATA;
+                  return OPUS_INVALID_PACKET;
                }
             }
             pcm_ptr += silk_frame_size * st->channels;
@@ -317,28 +322,26 @@ static int opus_decode_frame(OpusDecoder *st, const unsigned char *data,
     }
 
     start_band = 0;
-    if (mode != MODE_CELT_ONLY && data != NULL)
+    if (!decode_fec && mode != MODE_CELT_ONLY && data != NULL && ec_tell(&dec)+17+20*(st->mode == MODE_HYBRID) <= 8*len)
     {
         /* Check if we have a redundant 0-8 kHz band */
-        redundancy = ec_dec_bit_logp(&dec, 12);
+        if (mode == MODE_HYBRID)
+           redundancy = ec_dec_bit_logp(&dec, 12);
+        else
+           redundancy = 1;
         if (redundancy)
         {
             celt_to_silk = ec_dec_bit_logp(&dec, 1);
-            if (mode == MODE_HYBRID)
-               redundancy_bytes = 2 + ec_dec_uint(&dec, 256);
-            else {
-               redundancy_bytes = len - ((ec_tell(&dec)+7)>>3);
-               /* Can only happen on an invalid packet */
-               if (redundancy_bytes<0)
-               {
-                       redundancy_bytes = 0;
-                       redundancy = 0;
-               }
-            }
+            /* redundancy_bytes will be at least two, in the non-hybrid case due to the ec_tell() check above */
+            redundancy_bytes = mode==MODE_HYBRID ? (opus_int32)ec_dec_uint(&dec, 256)+2 : len-((ec_tell(&dec)+7)>>3);
             len -= redundancy_bytes;
-            if (len<0) {
-               RESTORE_STACK;
-               return OPUS_CORRUPTED_DATA;
+            /* This is a sanity check. It should never happen for a valid packet,
+               so the exact behaviour is not normative. */
+            if (len*8 < ec_tell(&dec))
+            {
+               len=0;
+               redundancy_bytes=0;
+               redundancy = 0;
             }
             /* Shrink decoder because of raw bits */
             dec.storage -= redundancy_bytes;
@@ -380,25 +383,31 @@ static int opus_decode_frame(OpusDecoder *st, const unsigned char *data,
     if (redundancy && celt_to_silk)
     {
         celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(0));
-        celt_decode_native(celt_dec, data+len, redundancy_bytes, redundant_audio, F5);
-        celt_decoder_ctl(celt_dec, CELT_GET_RANGE(&redundant_rng));
-        celt_decoder_ctl(celt_dec, CELT_RESET_STATE);
+        celt_decode_with_ec(celt_dec, data+len, redundancy_bytes, redundant_audio, F5, NULL);
+        celt_decoder_ctl(celt_dec, OPUS_GET_FINAL_RANGE(&redundant_rng));
     }
 
     /* MUST be after PLC */
     celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(start_band));
 
-    if (transition)
-       celt_decoder_ctl(celt_dec, CELT_RESET_STATE);
-
     if (mode != MODE_SILK_ONLY)
     {
-       int celt_frame_size = IMIN(F20, frame_size);
+        int celt_frame_size = IMIN(F20, frame_size);
+        /* Make sure to discard any previous CELT state */
+        if (mode != st->prev_mode && st->prev_mode > 0 && !st->prev_redundancy)
+            celt_decoder_ctl(celt_dec, OPUS_RESET_STATE);
         /* Decode CELT */
         celt_ret = celt_decode_with_ec(celt_dec, decode_fec?NULL:data, len, pcm, celt_frame_size, &dec);
     } else {
+       unsigned char silence[2] = {0xFF, 0xFF};
        for (i=0;i<frame_size*st->channels;i++)
           pcm[i] = 0;
+       /* For hybrid -> SILK transitions, we let the CELT MDCT do a fade-out by decoding a silence frame */
+       if (st->prev_mode == MODE_HYBRID)
+       {
+          celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(0));
+          celt_decode_with_ec(celt_dec, silence, 2, pcm, F2_5, NULL);
+       }
     }
 
     if (mode != MODE_CELT_ONLY)
@@ -408,7 +417,7 @@ static int opus_decode_frame(OpusDecoder *st, const unsigned char *data,
             pcm[i] = SAT16(pcm[i] + pcm_silk[i]);
 #else
         for (i=0;i<frame_size*st->channels;i++)
-            pcm[i] = pcm[i] + (1./32768.)*pcm_silk[i];
+            pcm[i] = pcm[i] + (opus_val16)((1./32768.)*pcm_silk[i]);
 #endif
     }
 
@@ -421,11 +430,11 @@ static int opus_decode_frame(OpusDecoder *st, const unsigned char *data,
     /* 5 ms redundant frame for SILK->CELT */
     if (redundancy && !celt_to_silk)
     {
-        celt_decoder_ctl(celt_dec, CELT_RESET_STATE);
+        celt_decoder_ctl(celt_dec, OPUS_RESET_STATE);
         celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(0));
 
-        celt_decode_native(celt_dec, data+len, redundancy_bytes, redundant_audio, F5);
-        celt_decoder_ctl(celt_dec, CELT_GET_RANGE(&redundant_rng));
+        celt_decode_with_ec(celt_dec, data+len, redundancy_bytes, redundant_audio, F5, NULL);
+        celt_decoder_ctl(celt_dec, OPUS_GET_FINAL_RANGE(&redundant_rng));
         smooth_fade(pcm+st->channels*(frame_size-F2_5), redundant_audio+st->channels*F2_5,
                        pcm+st->channels*(frame_size-F2_5), F2_5, st->channels, window, st->Fs);
     }
@@ -441,12 +450,23 @@ static int opus_decode_frame(OpusDecoder *st, const unsigned char *data,
     }
     if (transition)
     {
-       for (i=0;i<st->channels*F2_5;i++)
-               pcm[i] = pcm_transition[i];
-       if (audiosize >= F5)
-           smooth_fade(pcm_transition+st->channels*F2_5, pcm+st->channels*F2_5,
-                   pcm+st->channels*F2_5, F2_5,
-                   st->channels, window, st->Fs);
+       if (audiosize >= F5)
+       {
+          for (i=0;i<st->channels*F2_5;i++)
+             pcm[i] = pcm_transition[i];
+          smooth_fade(pcm_transition+st->channels*F2_5, pcm+st->channels*F2_5,
+                pcm+st->channels*F2_5, F2_5,
+                st->channels, window, st->Fs);
+       } else {
+          /* Not enough time to do a clean transition, but we do it anyway
+             This will not preserve amplitude perfectly and may introduce
+             a bit of temporal aliasing, but it shouldn't be too bad and
+             that's pretty much the best we can do. In any case, generating this
+             transition it pretty silly in the first place */
+          smooth_fade(pcm_transition, pcm,
+                pcm, F2_5,
+                st->channels, window, st->Fs);
+       }
     }
 
     if (len <= 1)
@@ -455,7 +475,7 @@ static int opus_decode_frame(OpusDecoder *st, const unsigned char *data,
        st->rangeFinal = dec.rng ^ redundant_rng;
 
     st->prev_mode = mode;
-    st->prev_redundancy = redundancy;
+    st->prev_redundancy = redundancy && !celt_to_silk;
     RESTORE_STACK;
        return celt_ret<0 ? celt_ret : audiosize;
 
@@ -515,7 +535,7 @@ static int opus_packet_parse_impl(const unsigned char *data, int len,
       if (!self_delimited)
       {
          if (len&0x1)
-            return OPUS_CORRUPTED_DATA;
+            return OPUS_INVALID_PACKET;
          size[0] = last_size = len/2;
       }
       break;
@@ -525,19 +545,19 @@ static int opus_packet_parse_impl(const unsigned char *data, int len,
       bytes = parse_size(data, len, size);
       len -= bytes;
       if (size[0]<0 || size[0] > len)
-         return OPUS_CORRUPTED_DATA;
+         return OPUS_INVALID_PACKET;
       data += bytes;
       last_size = len-size[0];
       break;
       /* Multiple CBR/VBR frames (from 0 to 120 ms) */
    case 3:
       if (len<1)
-         return OPUS_CORRUPTED_DATA;
+         return OPUS_INVALID_PACKET;
       /* Number of frames encoded in bits 0 to 5 */
       ch = *data++;
       count = ch&0x3F;
       if (count <= 0 || framesize*count > 5760)
-          return OPUS_CORRUPTED_DATA;
+          return OPUS_INVALID_PACKET;
       len--;
       /* Padding flag is bit 6 */
       if (ch&0x40)
@@ -546,7 +566,7 @@ static int opus_packet_parse_impl(const unsigned char *data, int len,
          int p;
          do {
             if (len<=0)
-               return OPUS_CORRUPTED_DATA;
+               return OPUS_INVALID_PACKET;
             p = *data++;
             len--;
             padding += p==255 ? 254: p;
@@ -554,10 +574,10 @@ static int opus_packet_parse_impl(const unsigned char *data, int len,
          len -= padding;
       }
       if (len<0)
-         return OPUS_CORRUPTED_DATA;
+         return OPUS_INVALID_PACKET;
       /* VBR flag is bit 7 */
       cbr = !(ch&0x80);
-      if (cbr)
+      if (!cbr)
       {
          /* VBR case */
          last_size = len;
@@ -566,18 +586,18 @@ static int opus_packet_parse_impl(const unsigned char *data, int len,
             bytes = parse_size(data, len, size+i);
             len -= bytes;
             if (size[i]<0 || size[i] > len)
-               return OPUS_CORRUPTED_DATA;
+               return OPUS_INVALID_PACKET;
             data += bytes;
             last_size -= bytes+size[i];
          }
          if (last_size<0)
-            return OPUS_CORRUPTED_DATA;
+            return OPUS_INVALID_PACKET;
       } else if (!self_delimited)
       {
          /* CBR case */
          last_size = len/count;
          if (last_size*count!=len)
-            return OPUS_CORRUPTED_DATA;
+            return OPUS_INVALID_PACKET;
          for (i=0;i<count-1;i++)
             size[i] = last_size;
       }
@@ -589,17 +609,17 @@ static int opus_packet_parse_impl(const unsigned char *data, int len,
       bytes = parse_size(data, len, size+count-1);
       len -= bytes;
       if (size[count-1]<0 || size[count-1] > len)
-         return OPUS_CORRUPTED_DATA;
+         return OPUS_INVALID_PACKET;
       data += bytes;
       /* For CBR packets, apply the size to all the frames. */
       if (cbr)
       {
          if (size[count-1]*count > len)
-            return OPUS_CORRUPTED_DATA;
+            return OPUS_INVALID_PACKET;
          for (i=0;i<count-1;i++)
             size[i] = size[count-1];
       } else if(size[count-1] > last_size)
-         return OPUS_CORRUPTED_DATA;
+         return OPUS_INVALID_PACKET;
    } else
    {
       /* Because it's not encoded explicitly, it's possible the size of the
@@ -607,7 +627,7 @@ static int opus_packet_parse_impl(const unsigned char *data, int len,
           1275.
          Reject them here.*/
       if (last_size > 1275)
-        return OPUS_CORRUPTED_DATA;
+        return OPUS_INVALID_PACKET;
       size[count-1] = last_size;
    }
 
@@ -646,6 +666,7 @@ int opus_decode_native(OpusDecoder *st, const unsigned char *data,
        int tot_offset;
        /* 48 x 2.5 ms = 120 ms */
        short size[48];
+       if (decode_fec<0 || decode_fec>1)return OPUS_BAD_ARG;
        if (len==0 || data==NULL)
            return opus_decode_frame(st, NULL, 0, pcm, frame_size, 0);
        else if (len<0)
@@ -657,7 +678,7 @@ int opus_decode_native(OpusDecoder *st, const unsigned char *data,
        st->frame_size = opus_packet_get_samples_per_frame(data, st->Fs);
        st->stream_channels = opus_packet_get_nb_channels(data);
 
-       count = opus_packet_parse_impl(data, len, 0, &toc, NULL, size, &offset);
+       count = opus_packet_parse_impl(data, len, self_delimited, &toc, NULL, size, &offset);
        if (count < 0)
           return count;
 
@@ -665,7 +686,7 @@ int opus_decode_native(OpusDecoder *st, const unsigned char *data,
        tot_offset += offset;
 
        if (count*st->frame_size > frame_size)
-               return OPUS_BAD_ARG;
+               return OPUS_BUFFER_TOO_SMALL;
        nb_samples=0;
        for (i=0;i<count;i++)
        {
@@ -705,7 +726,7 @@ int opus_decode_float(OpusDecoder *st, const unsigned char *data,
    if (ret > 0)
    {
       for (i=0;i<ret*st->channels;i++)
-         pcm[i] = (1./32768.)*(out[i]);
+         pcm[i] = (1.f/32768.f)*(out[i]);
    }
    RESTORE_STACK;
    return ret;
@@ -743,31 +764,65 @@ int opus_decode_float(OpusDecoder *st, const unsigned char *data,
 
 int opus_decoder_ctl(OpusDecoder *st, int request, ...)
 {
-    va_list ap;
+   int ret = OPUS_OK;
+   va_list ap;
+   void *silk_dec;
+   CELTDecoder *celt_dec;
 
-    va_start(ap, request);
+   silk_dec = (char*)st+st->silk_dec_offset;
+   celt_dec = (CELTDecoder*)((char*)st+st->celt_dec_offset);
 
-    switch (request)
-    {
-        case OPUS_GET_BANDWIDTH_REQUEST:
-        {
-            opus_int32 *value = va_arg(ap, opus_int32*);
-            *value = st->bandwidth;
-        }
-        break;
-        case OPUS_GET_FINAL_RANGE_REQUEST:
-        {
-            opus_uint32 *value = va_arg(ap, opus_uint32*);
-            *value = st->rangeFinal;
-        }
-        break;
-        default:
-            fprintf(stderr, "unknown opus_decoder_ctl() request: %d", request);
-            break;
-    }
 
-    va_end(ap);
-    return OPUS_OK;
+   va_start(ap, request);
+
+   switch (request)
+   {
+   case OPUS_GET_BANDWIDTH_REQUEST:
+   {
+      opus_int32 *value = va_arg(ap, opus_int32*);
+      *value = st->bandwidth;
+   }
+   break;
+   case OPUS_GET_FINAL_RANGE_REQUEST:
+   {
+      opus_uint32 *value = va_arg(ap, opus_uint32*);
+      *value = st->rangeFinal;
+   }
+   break;
+   case OPUS_RESET_STATE:
+   {
+      OPUS_CLEAR((char*)&st->OPUS_DECODER_RESET_START,
+            sizeof(OpusDecoder)-
+            ((char*)&st->OPUS_DECODER_RESET_START - (char*)st));
+
+      celt_decoder_ctl(celt_dec, OPUS_RESET_STATE);
+      silk_InitDecoder( silk_dec );
+      st->stream_channels = st->channels;
+      st->frame_size = st->Fs/400;
+   }
+   break;
+   case OPUS_GET_PITCH_REQUEST:
+   {
+      int *value = va_arg(ap, opus_int32*);
+      if (value==NULL)
+      {
+         ret = OPUS_BAD_ARG;
+         break;
+      }
+      if (st->prev_mode == MODE_CELT_ONLY)
+         celt_decoder_ctl(celt_dec, OPUS_GET_PITCH(value));
+      else
+         *value = st->DecControl.prevPitchLag;
+   }
+   break;
+   default:
+      /*fprintf(stderr, "unknown opus_decoder_ctl() request: %d", request);*/
+      ret = OPUS_UNIMPLEMENTED;
+      break;
+   }
+
+   va_end(ap);
+   return ret;
 }
 
 void opus_decoder_destroy(OpusDecoder *st)
@@ -794,9 +849,9 @@ int opus_packet_get_bandwidth(const unsigned char *data)
     return bandwidth;
 }
 
-int opus_packet_get_samples_per_frame(const unsigned char *data, int Fs)
+int opus_packet_get_samples_per_frame(const unsigned char *data, opus_int32 Fs)
 {
-       int audiosize;
+    int audiosize;
     if (data[0]&0x80)
     {
         audiosize = ((data[0]>>3)&0x3);
@@ -831,7 +886,7 @@ int opus_packet_get_nb_frames(const unsigned char packet[], int len)
        else if (count!=3)
                return 2;
        else if (len<2)
-               return OPUS_CORRUPTED_DATA;
+               return OPUS_INVALID_PACKET;
        else
                return packet[1]&0x3F;
 }
@@ -843,8 +898,7 @@ int opus_decoder_get_nb_samples(const OpusDecoder *dec, const unsigned char pack
        samples = count*opus_packet_get_samples_per_frame(packet, dec->Fs);
        /* Can't have more than 120 ms */
        if (samples*25 > dec->Fs*3)
-               return OPUS_CORRUPTED_DATA;
+               return OPUS_INVALID_PACKET;
        else
                return samples;
 }
-