From 78291b270ab9d253049018c6e280b68ab6f50b85 Mon Sep 17 00:00:00 2001
From: Jean-Marc Valin <jmvalin@jmvalin.ca>
Date: Sat, 8 Oct 2011 22:12:31 -0400
Subject: [PATCH] Fixes a redundancy mismatch bug

When SILK decided not to use the desired bandwidth, the redundancy
could be encoded at the desired bandwidth rather than at the actual
bandwidth used.
---
 src/opus_encoder.c | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)

diff --git a/src/opus_encoder.c b/src/opus_encoder.c
index e9a6c25c5..8be64fbe6 100644
--- a/src/opus_encoder.c
+++ b/src/opus_encoder.c
@@ -219,7 +219,7 @@ int opus_encoder_init(OpusEncoder* st, opus_int32 Fs, int channels, int applicat
     return OPUS_OK;
 }
 
-static unsigned char gen_toc(int mode, int framerate, int bandwidth, int silk_bandwidth, int channels)
+static unsigned char gen_toc(int mode, int framerate, int bandwidth, int channels)
 {
    int period;
    unsigned char toc;
@@ -231,7 +231,7 @@ static unsigned char gen_toc(int mode, int framerate, int bandwidth, int silk_ba
    }
    if (mode == MODE_SILK_ONLY)
    {
-       toc = (silk_bandwidth-OPUS_BANDWIDTH_NARROWBAND)<<5;
+       toc = (bandwidth-OPUS_BANDWIDTH_NARROWBAND)<<5;
        toc |= (period-2)<<3;
    } else if (mode == MODE_CELT_ONLY)
    {
@@ -412,7 +412,6 @@ int opus_encode_float(OpusEncoder *st, const opus_val16 *pcm, int frame_size,
     int ret=0;
     int nBytes;
     ec_enc enc;
-    int silk_internal_bandwidth=-1;
     int bytes_target;
     int prefill=0;
     int start_band = 0;
@@ -803,18 +802,18 @@ int opus_encode_float(OpusEncoder *st, const opus_val16 *pcm, int frame_size,
         }
         if (nBytes==0)
         {
-           data[-1] = gen_toc(st->mode, st->Fs/frame_size, st->bandwidth, silk_internal_bandwidth, st->stream_channels);
+           data[-1] = gen_toc(st->mode, st->Fs/frame_size, st->bandwidth, st->stream_channels);
            RESTORE_STACK;
            return 1;
         }
         /* Extract SILK internal bandwidth for signaling in first byte */
         if( st->mode == MODE_SILK_ONLY ) {
             if( st->silk_mode.internalSampleRate == 8000 ) {
-                silk_internal_bandwidth = OPUS_BANDWIDTH_NARROWBAND;
+               st->bandwidth = OPUS_BANDWIDTH_NARROWBAND;
             } else if( st->silk_mode.internalSampleRate == 12000 ) {
-                silk_internal_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
+               st->bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
             } else if( st->silk_mode.internalSampleRate == 16000 ) {
-                silk_internal_bandwidth = OPUS_BANDWIDTH_WIDEBAND;
+               st->bandwidth = OPUS_BANDWIDTH_WIDEBAND;
             }
         } else {
             silk_assert( st->silk_mode.internalSampleRate == 16000 );
@@ -1003,7 +1002,7 @@ int opus_encode_float(OpusEncoder *st, const opus_val16 *pcm, int frame_size,
 
     /* Signalling the mode in the first byte */
     data--;
-    data[0] = gen_toc(st->mode, st->Fs/frame_size, st->bandwidth, silk_internal_bandwidth, st->stream_channels);
+    data[0] = gen_toc(st->mode, st->Fs/frame_size, st->bandwidth, st->stream_channels);
 
     st->rangeFinal = enc.rng ^ redundant_rng;
 
-- 
GitLab