Fixes 60 ms speech mode
[opus.git] / src / opus_decoder.c
index ab6066f..14ab6cc 100644 (file)
@@ -73,14 +73,19 @@ OpusDecoder *opus_decoder_create(int Fs, int channels)
        return st;
 }
 
-static void smooth_fade(const short *in1, const short *in2, short *out, int overlap, int channels)
+static void smooth_fade(const short *in1, const short *in2, short *out,
+        int overlap, int channels, const celt_word16 *window, int Fs)
 {
        int i, c;
+       int inc = 48000/Fs;
        for (c=0;c<channels;c++)
        {
-               /* FIXME: Make this 16-bit safe, remove division */
                for (i=0;i<overlap;i++)
-                       out[i*channels+c] = (i*in2[i*channels+c] + (overlap-i)*in1[i*channels+c])/overlap;
+               {
+                   celt_word16 w = MULT16_16_Q15(window[i*inc], window[i*inc]);
+                   out[i*channels+c] = SHR32(MAC16_16(MULT16_16(w,in2[i*channels+c]),
+                           Q15ONE-w, in1[i*channels+c]), 15);
+               }
        }
 }
 
@@ -118,7 +123,12 @@ static int opus_decode_frame(OpusDecoder *st, const unsigned char *data,
     int celt_to_silk=0;
     short redundant_audio[240*2];
     int c;
+    int F2_5, F5, F10;
+    const celt_word16 *window;
 
+    F10 = st->Fs/100;
+    F5 = F10>>1;
+    F2_5 = F5>>1;
     /* Payloads of 1 (2 including ToC) or 0 trigger the PLC/DTX */
     if (len<=1)
        data = NULL;
@@ -135,17 +145,13 @@ static int opus_decode_frame(OpusDecoder *st, const unsigned char *data,
     if (st->stream_channels > st->channels)
         return OPUS_CORRUPTED_DATA;
 
-    /* FIXME: Remove this when we add SILK stereo support */
-    if (st->stream_channels == 2 && mode != MODE_CELT_ONLY)
-        return OPUS_UNIMPLEMENTED;
-
     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))
     {
        transition = 1;
        if (mode == MODE_CELT_ONLY)
-           opus_decode_frame(st, NULL, 0, pcm_transition, IMAX(st->Fs/100, audiosize), 0);
+           opus_decode_frame(st, NULL, 0, pcm_transition, IMAX(F10, audiosize), 0);
     }
     if (audiosize > frame_size)
     {
@@ -181,6 +187,7 @@ static int opus_decode_frame(OpusDecoder *st, const unsigned char *data,
             /* Hybrid mode */
             DecControl.internalSampleRate = 16000;
         }
+        DecControl.nChannels = st->channels;
 
         lost_flag = data == NULL ? 1 : 2 * decode_fec;
         decoded_samples = 0;
@@ -188,12 +195,12 @@ static int opus_decode_frame(OpusDecoder *st, const unsigned char *data,
             /* Call SILK decoder */
             int first_frame = decoded_samples == 0;
             silk_ret = SKP_Silk_SDK_Decode( st->silk_dec, &DecControl, 
-                lost_flag, first_frame, &dec, len, pcm_ptr, &silk_frame_size );
+                lost_flag, first_frame, &dec, pcm_ptr, &silk_frame_size );
             if( silk_ret ) {
                 fprintf (stderr, "SILK decode error\n");
                 /* Handle error */
             }
-            pcm_ptr += silk_frame_size;
+            pcm_ptr += silk_frame_size * st->channels;
             decoded_samples += silk_frame_size;
         } while( decoded_samples < frame_size );
     } else {
@@ -250,12 +257,12 @@ static int opus_decode_frame(OpusDecoder *st, const unsigned char *data,
         transition = 0;
 
     if (transition && mode != MODE_CELT_ONLY)
-        opus_decode_frame(st, NULL, 0, pcm_transition, IMAX(st->Fs/100, audiosize), 0);
+        opus_decode_frame(st, NULL, 0, pcm_transition, IMAX(F10, audiosize), 0);
 
     /* 5 ms redundant frame for CELT->SILK*/
     if (redundancy && celt_to_silk)
     {
-        celt_decode(st->celt_dec, data+len, redundancy_bytes, redundant_audio, st->Fs/200);
+        celt_decode(st->celt_dec, data+len, redundancy_bytes, redundant_audio, F5);
         celt_decoder_ctl(st->celt_dec, CELT_RESET_STATE);
     }
 
@@ -273,41 +280,40 @@ static int opus_decode_frame(OpusDecoder *st, const unsigned char *data,
             pcm[i] = ADD_SAT16(pcm[i], pcm_celt[i]);
     }
 
+
+    {
+        const CELTMode *celt_mode;
+        celt_decoder_ctl(st->celt_dec, CELT_GET_MODE(&celt_mode));
+        window = celt_mode->window;
+    }
+
     /* 5 ms redundant frame for SILK->CELT */
     if (redundancy && !celt_to_silk)
     {
-        int N2, N4;
-        N2 = st->Fs/200;
-        N4 = st->Fs/400;
         celt_decoder_ctl(st->celt_dec, CELT_RESET_STATE);
         celt_decoder_ctl(st->celt_dec, CELT_SET_START_BAND(0));
 
-        celt_decode(st->celt_dec, data+len, redundancy_bytes, redundant_audio, N2);
-        smooth_fade(pcm+st->channels*(frame_size-N4), redundant_audio+st->channels*N4,
-                       pcm+st->channels*(frame_size-N4), N4, st->channels);
+        celt_decode(st->celt_dec, data+len, redundancy_bytes, redundant_audio, F5);
+        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);
     }
     if (redundancy && celt_to_silk)
     {
-        int N2, N4;
-        N2 = st->Fs/200;
-        N4 = st->Fs/400;
-
         for (c=0;c<st->channels;c++)
         {
-            for (i=0;i<N4;i++)
+            for (i=0;i<F2_5;i++)
                 pcm[st->channels*i+c] = redundant_audio[st->channels*i];
         }
-        smooth_fade(redundant_audio+st->channels*N4, pcm+st->channels*N4, pcm+st->channels*N4, N4, st->channels);
+        smooth_fade(redundant_audio+st->channels*F2_5, pcm+st->channels*F2_5,
+                pcm+st->channels*F2_5, F2_5, st->channels, window, st->Fs);
     }
     if (transition)
     {
-       int plc_length, overlap;
-       plc_length = IMIN(audiosize, 10+st->Fs/400);
-       for (i=0;i<plc_length;i++)
+       for (i=0;i<F2_5;i++)
                pcm[i] = pcm_transition[i];
-
-       overlap = IMIN(st->Fs/400, IMAX(0, audiosize-plc_length));
-       smooth_fade(pcm_transition+plc_length, pcm+plc_length, pcm+plc_length, overlap, st->channels);
+       if (audiosize >= F5)
+           smooth_fade(pcm_transition+F2_5, pcm+F2_5, pcm+F2_5, F2_5,
+                   st->channels, window, st->Fs);
     }
 #if OPUS_TEST_RANGE_CODER_STATE
     st->rangeFinal = dec.rng;
@@ -391,6 +397,22 @@ int opus_decode(OpusDecoder *st, const unsigned char *data,
                if (st->frame_size*count*25 > 3*st->Fs)
                    return OPUS_CORRUPTED_DATA;
                len--;
+               /* Padding bit */
+               if (ch&0x40)
+               {
+                       int padding=0;
+                       int p;
+                       do {
+                               if (len<=0)
+                                       return OPUS_CORRUPTED_DATA;
+                               p = *data++;
+                               len--;
+                               padding += p==255 ? 254: p;
+                       } while (p==255);
+                       len -= padding;
+               }
+               if (len<0)
+                       return OPUS_CORRUPTED_DATA;
                /* Bit 7 is VBR flag (bit 6 is ignored) */
                if (ch&0x80)
                {