1diff --git a/celt/bands.c b/celt/bands.c 2index 6962587..32e1de6 100644 3--- a/celt/bands.c 4+++ b/celt/bands.c 5@@ -1234,9 +1234,23 @@ void quant_all_bands(int encode, const CELTMode *m, int start, int end, 6 b = 0; 7 } 8 9- if (resynth && M*eBands[i]-N >= M*eBands[start] && (update_lowband || lowband_offset==0)) 10+ if (resynth && (M*eBands[i]-N >= M*eBands[start] || i==start+1) && (update_lowband || lowband_offset==0)) 11 lowband_offset = i; 12 13+ if (i == start+1) 14+ { 15+ int n1, n2; 16+ int offset; 17+ n1 = M*(eBands[start+1]-eBands[start]); 18+ n2 = M*(eBands[start+2]-eBands[start+1]); 19+ offset = M*eBands[start]; 20+ /* Duplicate enough of the first band folding data to be able to fold the second band. 21+ Copies no data for CELT-only mode. */ 22+ OPUS_COPY(&norm[offset+n1], &norm[offset+2*n1 - n2], n2-n1); 23+ if (C==2) 24+ OPUS_COPY(&norm2[offset+n1], &norm2[offset+2*n1 - n2], n2-n1); 25+ } 26+ 27 tf_change = tf_res[i]; 28 if (i>=m->effEBands) 29 { 30@@ -1257,7 +1271,7 @@ void quant_all_bands(int encode, const CELTMode *m, int start, int end, 31 fold_start = lowband_offset; 32 while(M*eBands[--fold_start] > effective_lowband); 33 fold_end = lowband_offset-1; 34- while(M*eBands[++fold_end] < effective_lowband+N); 35+ while(++fold_end < i && M*eBands[fold_end] < effective_lowband+N); 36 x_cm = y_cm = 0; 37 fold_i = fold_start; do { 38 x_cm |= collapse_masks[fold_i*C+0]; 39diff --git a/celt/quant_bands.c b/celt/quant_bands.c 40index e5ed9ef..82fb823 100644 41--- a/celt/quant_bands.c 42+++ b/celt/quant_bands.c 43@@ -552,6 +552,7 @@ void log2Amp(const CELTMode *m, int start, int end, 44 { 45 opus_val16 lg = ADD16(oldEBands[i+c*m->nbEBands], 46 SHL16((opus_val16)eMeans[i],6)); 47+ lg = MIN32(QCONST32(32.f, 16), lg); 48 eBands[i+c*m->nbEBands] = PSHR32(celt_exp2(lg),4); 49 } 50 for (;i<m->nbEBands;i++) 51diff --git a/silk/LPC_inv_pred_gain.c b/silk/LPC_inv_pred_gain.c 52index 60c439b..6c301da 100644 53--- a/silk/LPC_inv_pred_gain.c 54+++ b/silk/LPC_inv_pred_gain.c 55@@ -84,8 +84,13 @@ static opus_int32 LPC_inverse_pred_gain_QA( /* O Returns inver 56 57 /* Update AR coefficient */ 58 for( n = 0; n < k; n++ ) { 59- tmp_QA = Aold_QA[ n ] - MUL32_FRAC_Q( Aold_QA[ k - n - 1 ], rc_Q31, 31 ); 60- Anew_QA[ n ] = MUL32_FRAC_Q( tmp_QA, rc_mult2 , mult2Q ); 61+ opus_int64 tmp64; 62+ tmp_QA = silk_SUB_SAT32( Aold_QA[ n ], MUL32_FRAC_Q( Aold_QA[ k - n - 1 ], rc_Q31, 31 ) ); 63+ tmp64 = silk_RSHIFT_ROUND64( silk_SMULL( tmp_QA, rc_mult2 ), mult2Q); 64+ if( tmp64 > silk_int32_MAX || tmp64 < silk_int32_MIN ) { 65+ return 0; 66+ } 67+ Anew_QA[ n ] = ( opus_int32 )tmp64; 68 } 69 } 70 71diff --git a/silk/NLSF_stabilize.c b/silk/NLSF_stabilize.c 72index 979aaba..2ef2398 100644 73--- a/silk/NLSF_stabilize.c 74+++ b/silk/NLSF_stabilize.c 75@@ -134,7 +134,7 @@ void silk_NLSF_stabilize( 76 77 /* Keep delta_min distance between the NLSFs */ 78 for( i = 1; i < L; i++ ) 79- NLSF_Q15[i] = silk_max_int( NLSF_Q15[i], NLSF_Q15[i-1] + NDeltaMin_Q15[i] ); 80+ NLSF_Q15[i] = silk_max_int( NLSF_Q15[i], silk_ADD_SAT16( NLSF_Q15[i-1], NDeltaMin_Q15[i] ) ); 81 82 /* Last NLSF should be no higher than 1 - NDeltaMin[L] */ 83 NLSF_Q15[L-1] = silk_min_int( NLSF_Q15[L-1], (1<<15) - NDeltaMin_Q15[L] ); 84diff --git a/silk/dec_API.c b/silk/dec_API.c 85index efd7918..21bb7e0 100644 86--- a/silk/dec_API.c 87+++ b/silk/dec_API.c 88@@ -72,6 +72,9 @@ opus_int silk_InitDecoder( /* O Returns error co 89 for( n = 0; n < DECODER_NUM_CHANNELS; n++ ) { 90 ret = silk_init_decoder( &channel_state[ n ] ); 91 } 92+ silk_memset(&((silk_decoder *)decState)->sStereo, 0, sizeof(((silk_decoder *)decState)->sStereo)); 93+ /* Not strictly needed, but it's cleaner that way */ 94+ ((silk_decoder *)decState)->prev_decode_only_middle = 0; 95 96 return ret; 97 } 98diff --git a/silk/resampler_private_IIR_FIR.c b/silk/resampler_private_IIR_FIR.c 99index dbd6d9a..91a43aa 100644 100--- a/silk/resampler_private_IIR_FIR.c 101+++ b/silk/resampler_private_IIR_FIR.c 102@@ -75,10 +75,10 @@ void silk_resampler_private_IIR_FIR( 103 silk_resampler_state_struct *S = (silk_resampler_state_struct *)SS; 104 opus_int32 nSamplesIn; 105 opus_int32 max_index_Q16, index_increment_Q16; 106- opus_int16 buf[ RESAMPLER_MAX_BATCH_SIZE_IN + RESAMPLER_ORDER_FIR_12 ]; 107+ opus_int16 buf[ 2*RESAMPLER_MAX_BATCH_SIZE_IN + RESAMPLER_ORDER_FIR_12 ]; 108 109 /* Copy buffered samples to start of buffer */ 110- silk_memcpy( buf, S->sFIR, RESAMPLER_ORDER_FIR_12 * sizeof( opus_int32 ) ); 111+ silk_memcpy( buf, S->sFIR, RESAMPLER_ORDER_FIR_12 * sizeof( opus_int16 ) ); 112 113 /* Iterate over blocks of frameSizeIn input samples */ 114 index_increment_Q16 = S->invRatio_Q16; 115@@ -95,13 +95,13 @@ void silk_resampler_private_IIR_FIR( 116 117 if( inLen > 0 ) { 118 /* More iterations to do; copy last part of filtered signal to beginning of buffer */ 119- silk_memcpy( buf, &buf[ nSamplesIn << 1 ], RESAMPLER_ORDER_FIR_12 * sizeof( opus_int32 ) ); 120+ silk_memmove( buf, &buf[ nSamplesIn << 1 ], RESAMPLER_ORDER_FIR_12 * sizeof( opus_int16 ) ); 121 } else { 122 break; 123 } 124 } 125 126 /* Copy last part of filtered signal to the state for the next call */ 127- silk_memcpy( S->sFIR, &buf[ nSamplesIn << 1 ], RESAMPLER_ORDER_FIR_12 * sizeof( opus_int32 ) ); 128+ silk_memcpy( S->sFIR, &buf[ nSamplesIn << 1 ], RESAMPLER_ORDER_FIR_12 * sizeof( opus_int16 ) ); 129 } 130 131diff --git a/src/opus_decoder.c b/src/opus_decoder.c 132index 0cc56f8..8a30fbc 100644 133--- a/src/opus_decoder.c 134+++ b/src/opus_decoder.c 135@@ -595,16 +595,14 @@ static int opus_packet_parse_impl(const unsigned char *data, int len, 136 /* Padding flag is bit 6 */ 137 if (ch&0x40) 138 { 139- int padding=0; 140 int p; 141 do { 142 if (len<=0) 143 return OPUS_INVALID_PACKET; 144 p = *data++; 145 len--; 146- padding += p==255 ? 254: p; 147+ len -= p==255 ? 254: p; 148 } while (p==255); 149- len -= padding; 150 } 151 if (len<0) 152 return OPUS_INVALID_PACKET; 153diff --git a/run_vectors.sh b/run_vectors.sh 154index 7cd23ed..4841b0a 100755 155--- a/run_vectors.sh 156+++ b/run_vectors.sh 157@@ -1,3 +1,5 @@ 158+#!/bin/sh 159+# 160 # Copyright (c) 2011-2012 IETF Trust, Jean-Marc Valin. All rights reserved. 161 # 162 # This file is extracted from RFC6716. Please see that RFC for additional 163@@ -31,10 +33,8 @@ 164 # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 165 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 166 167-#!/bin/sh 168- 169-rm logs_mono.txt 170-rm logs_stereo.txt 171+rm -f logs_mono.txt logs_mono2.txt 172+rm -f logs_stereo.txt logs_stereo2.txt 173 174 if [ "$#" -ne "3" ]; then 175 echo "usage: run_vectors.sh <exec path> <vector path> <rate>" 176@@ -45,18 +45,23 @@ CMD_PATH=$1 177 VECTOR_PATH=$2 178 RATE=$3 179 180-OPUS_DEMO=$CMD_PATH/opus_demo 181-OPUS_COMPARE=$CMD_PATH/opus_compare 182+: ${OPUS_DEMO:=$CMD_PATH/opus_demo} 183+: ${OPUS_COMPARE:=$CMD_PATH/opus_compare} 184 185 if [ -d $VECTOR_PATH ]; then 186 echo Test vectors found in $VECTOR_PATH 187 else 188 echo No test vectors found 189- #Don't make the test fail here because the test vectors will be 190- #distributed separately 191+ #Don't make the test fail here because the test vectors 192+ #will be distributed separately 193 exit 0 194 fi 195 196+if [ ! -x $OPUS_COMPARE ]; then 197+ echo ERROR: Compare program not found: $OPUS_COMPARE 198+ exit 1 199+fi 200+ 201 if [ -x $OPUS_DEMO ]; then 202 echo Decoding with $OPUS_DEMO 203 else 204@@ -82,9 +87,11 @@ do 205 echo ERROR: decoding failed 206 exit 1 207 fi 208- $OPUS_COMPARE -r $RATE $VECTOR_PATH/testvector$file.dec tmp.out >> logs_mono.txt 2>&1 209+ $OPUS_COMPARE -r $RATE $VECTOR_PATH/testvector${file}.dec tmp.out >> logs_mono.txt 2>&1 210 float_ret=$? 211- if [ "$float_ret" -eq "0" ]; then 212+ $OPUS_COMPARE -r $RATE $VECTOR_PATH/testvector${file}m.dec tmp.out >> logs_mono2.txt 2>&1 213+ float_ret2=$? 214+ if [ "$float_ret" -eq "0" ] || [ "$float_ret2" -eq "0" ]; then 215 echo output matches reference 216 else 217 echo ERROR: output does not match reference 218@@ -111,9 +118,11 @@ do 219 echo ERROR: decoding failed 220 exit 1 221 fi 222- $OPUS_COMPARE -s -r $RATE $VECTOR_PATH/testvector$file.dec tmp.out >> logs_stereo.txt 2>&1 223+ $OPUS_COMPARE -s -r $RATE $VECTOR_PATH/testvector${file}.dec tmp.out >> logs_stereo.txt 2>&1 224 float_ret=$? 225- if [ "$float_ret" -eq "0" ]; then 226+ $OPUS_COMPARE -s -r $RATE $VECTOR_PATH/testvector${file}m.dec tmp.out >> logs_stereo2.txt 2>&1 227+ float_ret2=$? 228+ if [ "$float_ret" -eq "0" ] || [ "$float_ret2" -eq "0" ]; then 229 echo output matches reference 230 else 231 echo ERROR: output does not match reference 232@@ -125,5 +134,10 @@ done 233 234 235 echo All tests have passed successfully 236-grep quality logs_mono.txt | awk '{sum+=$4}END{print "Average mono quality is", sum/NR, "%"}' 237-grep quality logs_stereo.txt | awk '{sum+=$4}END{print "Average stereo quality is", sum/NR, "%"}' 238+mono1=`grep quality logs_mono.txt | awk '{sum+=$4}END{if (NR == 12) sum /= 12; else sum = 0; print sum}'` 239+mono2=`grep quality logs_mono2.txt | awk '{sum+=$4}END{if (NR == 12) sum /= 12; else sum = 0; print sum}'` 240+echo $mono1 $mono2 | awk '{if ($2 > $1) $1 = $2; print "Average mono quality is", $1, "%"}' 241+ 242+stereo1=`grep quality logs_stereo.txt | awk '{sum+=$4}END{if (NR == 12) sum /= 12; else sum = 0; print sum}'` 243+stereo2=`grep quality logs_stereo2.txt | awk '{sum+=$4}END{if (NR == 12) sum /= 12; else sum = 0; print sum}'` 244+echo $stereo1 $stereo2 | awk '{if ($2 > $1) $1 = $2; print "Average stereo quality is", $1, "%"}' 245