New bandwidth detection code
[opus.git] / src / opus_encoder.c
index 3762a1e..5199d66 100644 (file)
@@ -69,6 +69,7 @@ struct OpusEncoder {
     int          vbr_constraint;
     opus_int32   bitrate_bps;
     opus_int32   user_bitrate_bps;
+    int          lsb_depth;
     int          encoder_buffer;
 
 #define OPUS_ENCODER_RESET_START stream_channels
@@ -210,6 +211,7 @@ int opus_encoder_init(OpusEncoder* st, opus_int32 Fs, int channels, int applicat
     st->user_forced_mode = OPUS_AUTO;
     st->voice_ratio = -1;
     st->encoder_buffer = st->Fs/100;
+    st->lsb_depth = 24;
 
     /* Delay compensation of 4 ms (2.5 ms for SILK's extra look-ahead 
        + 1.5 ms for SILK resamplers and stereo prediction) */
@@ -401,7 +403,7 @@ static void dc_reject(const opus_val16 *in, opus_int32 cutoff_Hz, opus_val16 *ou
    int c, i;
    float coef;
 
-   coef = 4.*cutoff_Hz/Fs;
+   coef = 4.0f*cutoff_Hz/Fs;
    for (c=0;c<channels;c++)
    {
       for (i=0;i<len;i++)
@@ -458,22 +460,37 @@ static void gain_fade(const opus_val16 *in, opus_val16 *out, opus_val16 g1, opus
     int i;
     int inc;
     int overlap;
+    int c;
     inc = 48000/Fs;
     overlap=overlap48/inc;
-    for (i=0;i<overlap;i++)
+    if (channels==1)
     {
-       opus_val16 g, w;
-       w = MULT16_16_Q15(window[i*inc], window[i*inc]);
-       g = SHR32(MAC16_16(MULT16_16(w,g2),
-             Q15ONE-w, g1), 15);
-       out[i*channels] = MULT16_16_Q15(g, in[i*channels]);
-       out[i*channels+1] = MULT16_16_Q15(g, in[i*channels+1]);
+       for (i=0;i<overlap;i++)
+       {
+          opus_val16 g, w;
+          w = MULT16_16_Q15(window[i*inc], window[i*inc]);
+          g = SHR32(MAC16_16(MULT16_16(w,g2),
+                Q15ONE-w, g1), 15);
+          out[i] = MULT16_16_Q15(g, in[i]);
+       }
+    } else {
+       for (i=0;i<overlap;i++)
+       {
+          opus_val16 g, w;
+          w = MULT16_16_Q15(window[i*inc], window[i*inc]);
+          g = SHR32(MAC16_16(MULT16_16(w,g2),
+                Q15ONE-w, g1), 15);
+          out[i*2] = MULT16_16_Q15(g, in[i*2]);
+          out[i*2+1] = MULT16_16_Q15(g, in[i*2+1]);
+       }
     }
-    for (;i<frame_size;i++)
-    {
-       out[i*channels] = MULT16_16_Q15(g2, in[i*channels]);
-       out[i*channels+1] = MULT16_16_Q15(g2, in[i*channels+1]);
+    c=0;do {
+       for (;i<frame_size;i++)
+       {
+          out[i*channels+c] = MULT16_16_Q15(g2, in[i*channels+c]);
+       }
     }
+    while (++c<channels);
 }
 
 OpusEncoder *opus_encoder_create(opus_int32 Fs, int channels, int application, int *error)
@@ -844,6 +861,13 @@ opus_int32 opus_encode_float(OpusEncoder *st, const opus_val16 *pcm, int frame_s
         st->bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
     if (st->Fs <= 8000 && st->bandwidth > OPUS_BANDWIDTH_NARROWBAND)
         st->bandwidth = OPUS_BANDWIDTH_NARROWBAND;
+#ifndef FIXED_POINT
+    if (analysis_info.valid)
+    {
+       st->bandwidth = IMIN(st->bandwidth, analysis_info.opus_bandwidth);
+    }
+#endif
+    celt_encoder_ctl(celt_enc, OPUS_SET_LSB_DEPTH(st->lsb_depth));
 
     /* If max_data_bytes represents less than 8 kb/s, switch to CELT-only mode */
     if (max_data_bytes < (frame_rate > 50 ? 12000 : 8000)*frame_size / (st->Fs * 8))
@@ -961,9 +985,9 @@ opus_int32 opus_encode_float(OpusEncoder *st, const opus_val16 *pcm, int frame_s
        int nb_analysis_frames;
        nb_analysis_frames = frame_size/(st->Fs/100);
        for (i=0;i<nb_analysis_frames;i++)
-          tonality_analysis(&st->analysis, &analysis_info, celt_enc, pcm_buf+i*(st->Fs/100)*st->channels, st->channels);
+          tonality_analysis(&st->analysis, &analysis_info, celt_enc, pcm_buf+i*(st->Fs/100)*st->channels, st->channels, st->lsb_depth);
        if (st->signal_type == OPUS_AUTO)
-          st->voice_ratio = floor(.5+100*(1-analysis_info.music_prob));
+          st->voice_ratio = (int)floor(.5+100*(1-analysis_info.music_prob));
     } else {
        analysis_info.valid = 0;
        st->voice_ratio = -1;
@@ -1194,7 +1218,7 @@ opus_int32 opus_encode_float(OpusEncoder *st, const opus_val16 *pcm, int frame_s
        const CELTMode *celt_mode;
 
        celt_encoder_ctl(celt_enc, CELT_GET_MODE(&celt_mode));
-       gain_fade(pcm_buf, pcm_buf,
+       gain_fade(pcm_buf+extra_buffer*st->channels, pcm_buf+extra_buffer*st->channels,
              st->prev_HB_gain, HB_gain, celt_mode->overlap, frame_size, st->channels, celt_mode->window, st->Fs);
     }
     st->prev_HB_gain = HB_gain;
@@ -1685,13 +1709,15 @@ int opus_encoder_ctl(OpusEncoder *st, int request, ...)
         case OPUS_SET_LSB_DEPTH_REQUEST:
         {
             opus_int32 value = va_arg(ap, opus_int32);
-            ret = celt_encoder_ctl(celt_enc, OPUS_SET_LSB_DEPTH(value));
+            if (value<8 || value>24)
+               goto bad_arg;
+            st->lsb_depth=value;
         }
         break;
         case OPUS_GET_LSB_DEPTH_REQUEST:
         {
             opus_int32 *value = va_arg(ap, opus_int32*);
-            celt_encoder_ctl(celt_enc, OPUS_GET_LSB_DEPTH(value));
+            *value = st->lsb_depth;
         }
         break;
         case OPUS_RESET_STATE: