Skip to content

Commit 8133595

Browse files
committed
Fix SILK LPC lint and coverage
1 parent 66b918d commit 8133595

3 files changed

Lines changed: 47 additions & 21 deletions

File tree

internal/silk/decoder.go

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -598,11 +598,14 @@ func (d *Decoder) normalizeLSFInterpolation(n2Q15 []int16, nanoseconds int) (n1Q
598598
if wQ2 == 4 || !d.haveDecoded {
599599
return nil, wQ2
600600
}
601+
if len(d.n0Q15) != len(n2Q15) {
602+
return nil, wQ2
603+
}
601604

602605
n1Q15 = make([]int16, len(n2Q15))
603606
for k := range n1Q15 {
604-
interpolated := int32(wQ2) * (int32(n2Q15[k]) - int32(d.n0Q15[k])) >> 2
605-
n1Q15[k] = int16(int32(d.n0Q15[k]) + interpolated) //nolint:gosec // G115
607+
interpolated := int32(wQ2) * (int32(n2Q15[k]) - int32(d.n0Q15[k])) >> 2 //nolint:gosec // G602
608+
n1Q15[k] = int16(int32(d.n0Q15[k]) + interpolated) //nolint:gosec // G115
606609
}
607610

608611
return
@@ -1229,7 +1232,7 @@ func (d *Decoder) limitLPCCoefficientsRange(a32Q17 []int32) {
12291232
// sc_Q16[k+1] = (sc_Q16[0]*sc_Q16[k] + 32768) >> 16
12301233
//
12311234
// https://datatracker.ietf.org/doc/html/rfc6716#section-4.2.7.5.7
1232-
expandLPCCoefficientsBandwidth(a32Q17, int32(scQ16)) //nolint:gosec // G115
1235+
expandLPCCoefficientsBandwidth(a32Q17, int32(scQ16)) //nolint:gosec // G115
12331236
} else {
12341237
break
12351238
}
@@ -1277,7 +1280,7 @@ func (d *Decoder) limitLPCFilterPredictionGain(a32Q17 []int32) (aQ12 []float32)
12771280
for n := range a32Q17 {
12781281
aQ12Int[n] = int16((a32Q17[n] + 16) >> 5) //nolint:gosec // G115
12791282
}
1280-
for i := 0; i < 16; i++ {
1283+
for i := range 16 {
12811284
if lpcInversePredictionGain(aQ12Int) >= 107374 {
12821285
break
12831286
}
@@ -1289,7 +1292,7 @@ func (d *Decoder) limitLPCFilterPredictionGain(a32Q17 []int32) (aQ12 []float32)
12891292
// expansion we re-quantize to Q12 before checking stability again,
12901293
// because the C reference measures the gain on the exact coefficients
12911294
// used by reconstruction.
1292-
expandLPCCoefficientsBandwidth(a32Q17, int32(65536-(2<<i)))
1295+
expandLPCCoefficientsBandwidth(a32Q17, int32(65536-(2<<i)))
12931296
for n := range a32Q17 {
12941297
aQ12Int[n] = int16((a32Q17[n] + 16) >> 5) //nolint:gosec // G115
12951298
}
@@ -1300,9 +1303,10 @@ func (d *Decoder) limitLPCFilterPredictionGain(a32Q17 []int32) (aQ12 []float32)
13001303
aQ12[n] = float32(aQ12Int[n])
13011304
}
13021305

1303-
return
1306+
return aQ12
13041307
}
13051308

1309+
//nolint:cyclop
13061310
func lpcInversePredictionGain(aQ12 []int16) int32 {
13071311
const (
13081312
inversePredictionGainQA = 24
@@ -1313,7 +1317,7 @@ func lpcInversePredictionGain(aQ12 []int16) int32 {
13131317
var atmpQA [2][16]int32
13141318
aNewQA := atmpQA[order&1][:]
13151319
dcResp := int32(0)
1316-
for k := 0; k < order; k++ {
1320+
for k := range order {
13171321
dcResp += int32(aQ12[k])
13181322
aNewQA[k] = int32(aQ12[k]) << (inversePredictionGainQA - 12)
13191323
}
@@ -1328,27 +1332,30 @@ func lpcInversePredictionGain(aQ12 []int16) int32 {
13281332
}
13291333

13301334
invGainQ30 := int32(1 << 30)
1331-
for k := order - 1; k > 0; k-- {
1335+
for coefIndex := order - 1; coefIndex > 0; coefIndex-- {
13321336
// This is the fixed-point Levinson recurrence from RFC 6716 section
13331337
// 4.2.7.5.8. The code intentionally mirrors
13341338
// silk_LPC_inverse_pred_gain_QA() in LPC_inv_pred_gain.c, including
13351339
// the Q24/Q30 scaling, the reflection-coefficient stability checks,
13361340
// and the saturating numerator update, because tiny arithmetic
13371341
// differences here can flip a filter from stable to unstable.
1338-
if aNewQA[k] > inversePredictionGainALimit || aNewQA[k] < -inversePredictionGainALimit {
1342+
if aNewQA[coefIndex] > inversePredictionGainALimit || aNewQA[coefIndex] < -inversePredictionGainALimit {
13391343
return 0
13401344
}
13411345

1342-
rcQ31 := -(aNewQA[k] << (31 - inversePredictionGainQA))
1346+
rcQ31 := -(aNewQA[coefIndex] << (31 - inversePredictionGainQA))
13431347
rcMult1Q30 := int32(1<<30) - smmul(rcQ31, rcQ31)
13441348
mult2Q := 32 - clz32(absInt32(rcMult1Q30))
13451349
rcMult2 := inverse32VarQ(rcMult1Q30, mult2Q+30)
13461350
invGainQ30 = smmul(invGainQ30, rcMult1Q30) << 2
13471351

13481352
aOldQA := aNewQA
1349-
aNewQA = atmpQA[k&1][:]
1350-
for n := 0; n < k; n++ {
1351-
tmpQA := saturatingSubInt32(aOldQA[n], int32(rshiftRound64(int64(aOldQA[k-n-1])*int64(rcQ31), 31)))
1353+
aNewQA = atmpQA[coefIndex&1][:]
1354+
for n := 0; n < coefIndex; n++ {
1355+
tmpQA := saturatingSubInt32(
1356+
aOldQA[n],
1357+
int32(rshiftRound64(int64(aOldQA[coefIndex-n-1])*int64(rcQ31), 31)), //nolint:gosec // G115
1358+
)
13521359
tmp64 := rshiftRound64(int64(tmpQA)*int64(rcMult2), mult2Q)
13531360
if tmp64 > math.MaxInt32 || tmp64 < math.MinInt32 {
13541361
return 0
@@ -1658,7 +1665,7 @@ func (d *Decoder) ltpSynthesis(
16581665
// then let out_end be set to (j - (s-2)*n) and let LTP_scale_Q14 be set
16591666
// to 16384. Otherwise, set out_end to (j - s*n) and set LTP_scale_Q14
16601667
// to the Q14 LTP scaling value from Section 4.2.7.6.3.
1661-
var out_end int //nolint:staticcheck,var-naming,revive
1668+
var out_end int //nolint:staticcheck,revive
16621669
if s < 2 || wQ2 == 4 {
16631670
out_end = -s * n
16641671
} else {

internal/silk/silk.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func minInt16(a, b int16) int16 {
8181
func saturatingAddInt16(a, b int16) int16 {
8282
sum := int32(a) + int32(b)
8383

84-
return int16(clamp(math.MinInt16, sum, math.MaxInt16))
84+
return int16(clamp(math.MinInt16, sum, math.MaxInt16)) //nolint:gosec // G115
8585
}
8686

8787
// saturatingSubInt32 mirrors silk_SUB_SAT32() from the RFC 6716 C macros.
@@ -161,7 +161,7 @@ func ilog(n int) int {
161161
}
162162

163163
func clz32(in int32) int {
164-
return bits.LeadingZeros32(uint32(in))
164+
return bits.LeadingZeros32(uint32(in)) //nolint:gosec // G115
165165
}
166166

167167
// rshiftRound64 mirrors silk_RSHIFT_ROUND64() from the RFC 6716 C macros.
@@ -176,8 +176,8 @@ func rshiftRound64(a int64, shift int) int64 {
176176
}
177177

178178
// rshiftRound32 mirrors silk_RSHIFT_ROUND() from the RFC 6716 C macros.
179-
// This is used by the bandwidth expander and multiply helpers to match the
180-
// reference fixed-point rounding behavior.
179+
// This is used by the multiply helpers to match the reference fixed-point
180+
// rounding behavior.
181181
func rshiftRound32(a int32, shift int) int32 {
182182
if shift == 1 {
183183
return (a >> 1) + (a & 1)
@@ -190,14 +190,14 @@ func rshiftRound32(a int32, shift int) int32 {
190190
// 32 bits of a signed 32x32 multiply. RFC 6716 section 4.2.7.5.8 expresses
191191
// several of the LPC inverse-gain updates in terms of this operation.
192192
func smmul(a, b int32) int32 {
193-
return int32((int64(a) * int64(b)) >> 32)
193+
return int32((int64(a) * int64(b)) >> 32) //nolint:gosec // G115
194194
}
195195

196196
// smulwb mirrors silk_SMULWB() from the RFC 6716 C macros. It multiplies a
197197
// 32-bit value by the low 16 bits of another 32-bit value and is one half of
198198
// the reference implementation's SMULWW/SMLAWW decomposition.
199199
func smulwb(a, b int32) int32 {
200-
return int32((int64(a) * int64(int16(b))) >> 16)
200+
return int32((int64(a) * int64(int16(b))) >> 16) //nolint:gosec // G115
201201
}
202202

203203
// smlaWW mirrors silk_SMLAWW() from the RFC 6716 C macros. The inverse helper
@@ -242,7 +242,7 @@ func inverse32VarQ(b32 int32, qRes int) int32 {
242242
// same recurrence with a fixed chirp sequence for prediction-gain limiting.
243243
func expandLPCCoefficientsBandwidth(ar []int32, chirpQ16 int32) {
244244
initialChirpQ16 := chirpQ16
245-
for i := 0; i < len(ar); i++ {
245+
for i := range ar {
246246
ar[i] = int32((int64(ar[i]) * int64(chirpQ16)) >> 16) //nolint:gosec // G115
247247
if i+1 < len(ar) {
248248
chirpQ16 = int32((int64(initialChirpQ16)*int64(chirpQ16) + 32768) >> 16) //nolint:gosec // G115

internal/silk/silk_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,22 @@ func TestIlog(t *testing.T) {
1818
assert.Equal(t, 3, ilog(4))
1919
assert.Equal(t, 3, ilog(7))
2020
}
21+
22+
func TestSaturatingAddInt16(t *testing.T) {
23+
assert.Equal(t, int16(123), saturatingAddInt16(100, 23))
24+
assert.Equal(t, int16(32767), saturatingAddInt16(32760, 100))
25+
assert.Equal(t, int16(-32768), saturatingAddInt16(-32760, -100))
26+
}
27+
28+
func TestSaturatingSubInt32(t *testing.T) {
29+
assert.Equal(t, int32(77), saturatingSubInt32(100, 23))
30+
assert.Equal(t, int32(2147483647), saturatingSubInt32(2147483640, -100))
31+
assert.Equal(t, int32(-2147483648), saturatingSubInt32(-2147483640, 100))
32+
}
33+
34+
func TestFixedPointHelpers(t *testing.T) {
35+
assert.Equal(t, int32(5), absInt32(-5))
36+
assert.Equal(t, int64(3), rshiftRound64(5, 1))
37+
assert.Equal(t, int32(3), rshiftRound32(5, 1))
38+
assert.Equal(t, int32(0), inverse32VarQ(1<<30, 29))
39+
}

0 commit comments

Comments
 (0)