Fixes 60 ms speech mode
[opus.git] / src / opus_encoder.c
index 384922d..bbbbcf4 100644 (file)
@@ -32,6 +32,7 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <stdarg.h>
+#include "celt.h"
 #include "opus_encoder.h"
 #include "entenc.h"
 #include "modes.h"
@@ -65,6 +66,7 @@ OpusEncoder *opus_encoder_create(int Fs, int channels)
 
     /* 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;
@@ -76,6 +78,7 @@ OpusEncoder *opus_encoder_create(int Fs, int channels)
     /* 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;
@@ -84,6 +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->first = 1;
 
        st->encoder_buffer = st->Fs/100;
        st->delay_compensation = st->Fs/400;
@@ -107,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;
@@ -131,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)
@@ -152,32 +161,31 @@ int opus_encode(OpusEncoder *st, const short *pcm, int frame_size,
         st->mode = MODE_CELT_ONLY;
     }
 
-    /* FIXME: Remove this once SILK supports stereo */
-    if (st->channels == 2)
-        st->mode = MODE_CELT_ONLY;
-
     /* 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 (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)
@@ -264,12 +272,12 @@ 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.maxInternalSampleRate = 8000;
+               st->silk_mode.desiredInternalSampleRate = 8000;
         } else if (st->bandwidth == BANDWIDTH_MEDIUMBAND) {
-            st->silk_mode.maxInternalSampleRate = 12000;
+               st->silk_mode.desiredInternalSampleRate = 12000;
         } else {
             SKP_assert( st->mode == MODE_HYBRID || st->bandwidth == BANDWIDTH_WIDEBAND );
-            st->silk_mode.maxInternalSampleRate = 16000;
+            st->silk_mode.desiredInternalSampleRate = 16000;
         }
         if( st->mode == MODE_HYBRID ) {
             /* Don't allow bandwidth reduction at lowest bitrates in hybrid mode */
@@ -277,6 +285,7 @@ int opus_encode(OpusEncoder *st, const short *pcm, int frame_size,
         } else {
             st->silk_mode.minInternalSampleRate = 8000;
         }
+        st->silk_mode.maxInternalSampleRate = 16000;
 
         /* Call SILK encoder for the low band */
         nBytes = max_data_bytes-1;
@@ -291,6 +300,8 @@ int opus_encode(OpusEncoder *st, const short *pcm, int frame_size,
             fprintf (stderr, "SILK encode error: %d\n", ret);
             /* Handle error */
         }
+        if (nBytes==0)
+            return 0;
         /* Extract SILK internal bandwidth for signaling in first byte */
         if( st->mode == MODE_SILK_ONLY ) {
             if( st->silk_mode.internalSampleRate == 8000 ) {
@@ -358,6 +369,7 @@ int opus_encode(OpusEncoder *st, const short *pcm, int frame_size,
             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));
                 nb_compr_bytes = max_data_bytes-1;
             } else {
@@ -365,7 +377,6 @@ int opus_encode(OpusEncoder *st, const short *pcm, int frame_size,
             }
         }
 
-
         ec_enc_shrink(&enc, nb_compr_bytes);
        } else {
            nb_compr_bytes = 0;
@@ -381,7 +392,7 @@ int opus_encode(OpusEncoder *st, const short *pcm, int frame_size,
         ec_enc_bit_logp(&enc, redundancy, 12);
         if (redundancy)
         {
-            redundancy_bytes = 40;
+            redundancy_bytes = st->stream_channels*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);
@@ -400,9 +411,8 @@ int opus_encode(OpusEncoder *st, const short *pcm, int frame_size,
     if (redundancy && celt_to_silk)
     {
         celt_encoder_ctl(st->celt_enc, CELT_SET_START_BAND(0));
-        /* FIXME: This is wrong -- we need to set the flags properly */
+        /* FIXME: That's OK for now, but we need to set the flags properly */
         celt_encoder_ctl(st->celt_enc, CELT_SET_VBR(0));
-        /* FIXME: Make sure not to overflow here */
         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);
     }
@@ -482,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;
 }
 
@@ -578,7 +589,10 @@ int opus_encoder_ctl(OpusEncoder *st, int request, ...)
         case OPUS_SET_PACKET_LOSS_PERC_REQUEST:
         {
             int value = va_arg(ap, int);
+            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));
         }
         break;
         case OPUS_GET_PACKET_LOSS_PERC_REQUEST:
@@ -604,7 +618,7 @@ int opus_encoder_ctl(OpusEncoder *st, int request, ...)
         {
             int value = va_arg(ap, int);
             if (value>100 || value<0)
-                return OPUS_BAD_ARG;
+                goto bad_arg;
             st->voice_ratio = value;
         }
         break;
@@ -614,12 +628,27 @@ int opus_encoder_ctl(OpusEncoder *st, int request, ...)
             *value = st->voice_ratio;
         }
         break;
+        case OPUS_SET_VBR_CONSTRAINT_REQUEST:
+        {
+            int value = va_arg(ap, int);
+            st->vbr_constraint = value;
+        }
+        break;
+        case OPUS_GET_VBR_CONSTRAINT_REQUEST:
+        {
+            int *value = va_arg(ap, int*);
+            *value = st->vbr_constraint;
+        }
+        break;
         default:
             fprintf(stderr, "unknown opus_encoder_ctl() request: %d", request);
             break;
     }
-
     va_end(ap);
+    return OPUS_OK;
+bad_arg:
+    va_end(ap);
+    return OPUS_BAD_ARG;
 }
 
 void opus_encoder_destroy(OpusEncoder *st)