Fixes 60 ms speech mode
[opus.git] / src / opus_encoder.c
index f377359..bbbbcf4 100644 (file)
@@ -87,7 +87,7 @@ OpusEncoder *opus_encoder_create(int Fs, int channels)
        st->user_mode = OPUS_MODE_AUTO;
        st->user_bandwidth = BANDWIDTH_AUTO;
        st->voice_ratio = 90;
-       st->bandwidth_change = 1;
+       st->first = 1;
 
        st->encoder_buffer = st->Fs/100;
        st->delay_compensation = st->Fs/400;
@@ -111,7 +111,9 @@ int opus_encode(OpusEncoder *st, const short *pcm, int frame_size,
     int redundancy = 0;
     int redundancy_bytes = 0;
     int celt_to_silk = 0;
-    short pcm_buf[960*2];
+    /* 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];
     int nb_compr_bytes;
     int to_celt = 0;
     celt_int32 mono_rate;
@@ -135,6 +137,9 @@ int opus_encode(OpusEncoder *st, const short *pcm, int frame_size,
     mono_rate = st->bitrate_bps;
     if (st->stream_channels==2)
         mono_rate = (mono_rate+10000)/2;
+    /* 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)
@@ -157,30 +162,30 @@ int opus_encode(OpusEncoder *st, const short *pcm, int frame_size,
     }
 
     /* Bandwidth selection */
-    if (st->bandwidth_change)
+    if (st->mode == MODE_CELT_ONLY)
     {
-       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 (mono_rate>28000 || (mono_rate>24000 && st->bandwidth==BANDWIDTH_FULLBAND))
-                       st->bandwidth = BANDWIDTH_FULLBAND;
-               else if (mono_rate>24000 || (mono_rate>18000 && st->bandwidth==BANDWIDTH_SUPERWIDEBAND))
-                       st->bandwidth = BANDWIDTH_SUPERWIDEBAND;
-               else if (mono_rate>18000 || (mono_rate>14000 && st->bandwidth==BANDWIDTH_WIDEBAND))
-                       st->bandwidth = BANDWIDTH_WIDEBAND;
-               else if (mono_rate>14000 || (mono_rate>11000 && st->bandwidth==BANDWIDTH_MEDIUMBAND))
-                       st->bandwidth = BANDWIDTH_MEDIUMBAND;
-               else
-                       st->bandwidth = BANDWIDTH_NARROWBAND;
-       }
+       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 (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;
+       if (!st->silk_mode.inWBmodeWithoutVariableLP && st->bandwidth > BANDWIDTH_WIDEBAND)
+               st->bandwidth = BANDWIDTH_WIDEBAND;
     }
 
     if (st->Fs <= 24000 && st->bandwidth > BANDWIDTH_SUPERWIDEBAND)
@@ -295,7 +300,6 @@ int opus_encode(OpusEncoder *st, const short *pcm, int frame_size,
             fprintf (stderr, "SILK encode error: %d\n", ret);
             /* Handle error */
         }
-        st->bandwidth_change = nBytes==0 || (enc.buf[0]&0x80)==0;
         if (nBytes==0)
             return 0;
         /* Extract SILK internal bandwidth for signaling in first byte */
@@ -310,8 +314,6 @@ int opus_encode(OpusEncoder *st, const short *pcm, int frame_size,
         } else {
             SKP_assert( st->silk_mode.internalSampleRate == 16000 );
         }
-    } else {
-       st->bandwidth_change = 1;
     }
 
     /* CELT processing */
@@ -490,6 +492,7 @@ int opus_encode(OpusEncoder *st, const short *pcm, int frame_size,
         st->prev_mode = MODE_CELT_ONLY;
     else
         st->prev_mode = st->mode;
+    st->first = 0;
     return ret+1+redundancy_bytes;
 }