Fixes 60 ms speech mode
[opus.git] / src / opus_encoder.c
index 2334aa2..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"
@@ -39,6 +40,7 @@
 
 OpusEncoder *opus_encoder_create(int Fs, int channels)
 {
+    int err;
     char *raw_state;
        OpusEncoder *st;
        int ret, silkEncSizeBytes, celtEncSizeBytes;
@@ -64,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;
@@ -74,13 +77,22 @@ 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, NULL);
+       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;
 }
 
@@ -95,6 +107,110 @@ int opus_encode(OpusEncoder *st, const short *pcm, int frame_size,
     int silk_internal_bandwidth;
     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];
+    int nb_compr_bytes;
+    int to_celt = 0;
+    celt_int32 mono_rate;
+
+    if (st->channels == 2)
+    {
+        celt_int32 decision_rate;
+        decision_rate = st->bitrate_bps + st->voice_ratio*st->voice_ratio;
+        if (st->stream_channels == 2)
+            decision_rate += 4000;
+        else
+            decision_rate -= 4000;
+        if (decision_rate>48000)
+            st->stream_channels = 2;
+        else
+            st->stream_channels = 1;
+    } else {
+        st->stream_channels = 1;
+    }
+    /* Equivalent bit-rate for mono */
+    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)
+    {
+        celt_int32 decision_rate;
+        decision_rate = mono_rate - 3*st->voice_ratio*st->voice_ratio;
+        if (st->prev_mode == MODE_CELT_ONLY)
+            decision_rate += 4000;
+        else if (st->prev_mode>0)
+            decision_rate -= 4000;
+        if (decision_rate>24000)
+            st->mode = MODE_CELT_ONLY;
+        else
+            st->mode = MODE_SILK_ONLY;
+    } else if (st->user_mode==OPUS_MODE_VOICE)
+    {
+        st->mode = MODE_SILK_ONLY;
+    } else {/* OPUS_AUDIO_MODE */
+        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 (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)
+       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;
+
+    /* Preventing non-sensical configurations */
+    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)
+        st->mode = MODE_HYBRID;
+    if (st->mode == MODE_HYBRID && st->bandwidth <= BANDWIDTH_WIDEBAND)
+        st->mode = MODE_SILK_ONLY;
 
        bytes_target = st->bitrate_bps * frame_size / (st->Fs * 8) - 1;
 
@@ -105,6 +221,24 @@ int opus_encode(OpusEncoder *st, const short *pcm, int frame_size,
                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);
 
@@ -138,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 */
@@ -151,17 +285,23 @@ 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;
         if (prefill)
-               SKP_Silk_SDK_Encode( st->silk_enc, &st->silk_mode, st->delay_buffer, ENCODER_BUFFER, NULL, 0, 1 );
+        {
+            int zero=0;
+               SKP_Silk_SDK_Encode( st->silk_enc, &st->silk_mode, st->delay_buffer, st->encoder_buffer, NULL, &zero, 1 );
+        }
 
         ret = SKP_Silk_SDK_Encode( st->silk_enc, &st->silk_mode, pcm, frame_size, &enc, &nBytes, 0 );
         if( ret ) {
             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 ) {
@@ -177,11 +317,8 @@ int opus_encode(OpusEncoder *st, const short *pcm, int frame_size,
     }
 
     /* CELT processing */
-       if (st->mode != MODE_SILK_ONLY)
        {
-               int endband;
-           short pcm_buf[960*2];
-           int nb_compr_bytes;
+           int endband=21;
 
            switch(st->bandwidth)
            {
@@ -200,23 +337,26 @@ int opus_encode(OpusEncoder *st, const short *pcm, int frame_size,
            }
            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));
         if (st->prev_mode == MODE_SILK_ONLY)
         {
-               unsigned char dummy[2];
+               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[(ENCODER_BUFFER-ENCODER_DELAY_COMPENSATION-120)*st->channels], 120, dummy, 10);
+               celt_encode(st->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));
         }
+
         if (st->mode == MODE_HYBRID)
         {
             int len;
-            celt_encoder_ctl(st->celt_enc, CELT_SET_START_BAND(17));
 
             len = (ec_tell(&enc)+7)>>3;
             if( st->use_vbr ) {
@@ -226,10 +366,10 @@ int opus_encode(OpusEncoder *st, const short *pcm, int frame_size,
                 nb_compr_bytes = len > bytes_target ? len : bytes_target;
             }
         } else {
-            celt_encoder_ctl(st->celt_enc, CELT_SET_START_BAND(0));
             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 {
@@ -237,32 +377,84 @@ int opus_encode(OpusEncoder *st, const short *pcm, int frame_size,
             }
         }
 
-        for (i=0;i<IMIN(frame_size, ENCODER_DELAY_COMPENSATION)*st->channels;i++)
-            pcm_buf[i] = st->delay_buffer[(ENCODER_BUFFER-ENCODER_DELAY_COMPENSATION)*st->channels+i];
-        for (;i<frame_size*st->channels;i++)
-            pcm_buf[i] = pcm[i-ENCODER_DELAY_COMPENSATION*st->channels];
-
         ec_enc_shrink(&enc, nb_compr_bytes);
+       } else {
+           nb_compr_bytes = 0;
+       }
+
+    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_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;
+            ec_enc_bit_logp(&enc, celt_to_silk, 1);
+            if (st->mode == MODE_HYBRID)
+               ec_enc_uint(&enc, redundancy_bytes-2, 256);
+        }
+        start_band = 17;
+    }
+
+    if (st->mode == MODE_SILK_ONLY)
+    {
+        ret = (ec_tell(&enc)+7)>>3;
+        ec_enc_done(&enc);
+        nb_compr_bytes = ret;
+    }
+
+    /* 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(st->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);
-
-           if (frame_size>ENCODER_BUFFER)
-           {
-               for (i=0;i<ENCODER_BUFFER*st->channels;i++)
-                   st->delay_buffer[i] = pcm[(frame_size-ENCODER_BUFFER)*st->channels+i];
-           } else {
-               int tmp = 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];
-           }
-       } else {
-           ret = (ec_tell(&enc)+7)>>3;
-           ec_enc_done(&enc);
        }
 
+    /* 5 ms redundant frame for SILK->CELT */
+    if (redundancy && !celt_to_silk)
+    {
+        int N2, N4;
+        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));
+
+        /* 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);
+
+        celt_encode(st->celt_enc, pcm_buf+st->channels*(frame_size-N2), N2, data+nb_compr_bytes, redundancy_bytes);
+    }
+
+
+    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];
+    } 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];
+    }
+
        /* Signalling the mode in the first byte */
        data--;
        framerate = st->Fs/frame_size;
@@ -296,11 +488,15 @@ int opus_encode(OpusEncoder *st, const short *pcm, int frame_size,
 #if OPUS_TEST_RANGE_CODER_STATE
     st->rangeFinal = enc.rng;
 #endif
-    st->prev_mode = st->mode;
-    return ret+1;
+    if (to_celt)
+        st->prev_mode = MODE_CELT_ONLY;
+    else
+        st->prev_mode = st->mode;
+    st->first = 0;
+    return ret+1+redundancy_bytes;
 }
 
-void opus_encoder_ctl(OpusEncoder *st, int request, ...)
+int opus_encoder_ctl(OpusEncoder *st, int request, ...)
 {
     va_list ap;
 
@@ -311,7 +507,7 @@ void opus_encoder_ctl(OpusEncoder *st, int request, ...)
         case OPUS_SET_MODE_REQUEST:
         {
             int value = va_arg(ap, int);
-            st->mode = value;
+            st->user_mode = value;
         }
         break;
         case OPUS_GET_MODE_REQUEST:
@@ -335,8 +531,10 @@ void opus_encoder_ctl(OpusEncoder *st, int request, ...)
         case OPUS_SET_BANDWIDTH_REQUEST:
         {
             int value = va_arg(ap, int);
-            st->bandwidth = value;
-            if (st->bandwidth == BANDWIDTH_NARROWBAND) {
+            if (value < BANDWIDTH_AUTO || value > BANDWIDTH_FULLBAND)
+               return OPUS_BAD_ARG;
+            st->user_bandwidth = value;
+            if (st->user_bandwidth == BANDWIDTH_NARROWBAND) {
                 st->silk_mode.maxInternalSampleRate = 8000;
             } else if (st->bandwidth == BANDWIDTH_MEDIUMBAND) {
                 st->silk_mode.maxInternalSampleRate = 12000;
@@ -391,7 +589,10 @@ void 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:
@@ -413,12 +614,41 @@ void opus_encoder_ctl(OpusEncoder *st, int request, ...)
             *value = st->use_vbr;
         }
         break;
+        case OPUS_SET_VOICE_RATIO_REQUEST:
+        {
+            int value = va_arg(ap, int);
+            if (value>100 || value<0)
+                goto bad_arg;
+            st->voice_ratio = value;
+        }
+        break;
+        case OPUS_GET_VOICE_RATIO_REQUEST:
+        {
+            int *value = va_arg(ap, int*);
+            *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)