Implement section-4.2.7.5.3

Reconstructing the Normalized LSF Coefficients.
This commit is contained in:
Sean DuBois 2022-07-27 10:34:51 -04:00
parent a42c71c335
commit b1281d6e3a
2 changed files with 297 additions and 3 deletions

View file

@ -480,4 +480,215 @@ var (
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0},
{0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 0},
}
// +----+----------------------------------------+
// | I1 | Codebook (Q8) |
// +----+----------------------------------------+
// | | 0 1 2 3 4 5 6 7 8 9 |
// | | |
// | 0 | 12 35 60 83 108 132 157 180 206 228 |
// | | |
// | 1 | 15 32 55 77 101 125 151 175 201 225 |
// | | |
// | 2 | 19 42 66 89 114 137 162 184 209 230 |
// | | |
// | 3 | 12 25 50 72 97 120 147 172 200 223 |
// | | |
// | 4 | 26 44 69 90 114 135 159 180 205 225 |
// | | |
// | 5 | 13 22 53 80 106 130 156 180 205 228 |
// | | |
// | 6 | 15 25 44 64 90 115 142 168 196 222 |
// | | |
// | 7 | 19 24 62 82 100 120 145 168 190 214 |
// | | |
// | 8 | 22 31 50 79 103 120 151 170 203 227 |
// | | |
// | 9 | 21 29 45 65 106 124 150 171 196 224 |
// | | |
// | 10 | 30 49 75 97 121 142 165 186 209 229 |
// | | |
// | 11 | 19 25 52 70 93 116 143 166 192 219 |
// | | |
// | 12 | 26 34 62 75 97 118 145 167 194 217 |
// | | |
// | 13 | 25 33 56 70 91 113 143 165 196 223 |
// | | |
// | 14 | 21 34 51 72 97 117 145 171 196 222 |
// | | |
// | 15 | 20 29 50 67 90 117 144 168 197 221 |
// | | |
// | 16 | 22 31 48 66 95 117 146 168 196 222 |
// | | |
// | 17 | 24 33 51 77 116 134 158 180 200 224 |
// | | |
// | 18 | 21 28 70 87 106 124 149 170 194 217 |
// | | |
// | 19 | 26 33 53 64 83 117 152 173 204 225 |
// | | |
// | 20 | 27 34 65 95 108 129 155 174 210 225 |
// | | |
// | 21 | 20 26 72 99 113 131 154 176 200 219 |
// | | |
// | 22 | 34 43 61 78 93 114 155 177 205 229 |
// | | |
// | 23 | 23 29 54 97 124 138 163 179 209 229 |
// | | |
// | 24 | 30 38 56 89 118 129 158 178 200 231 |
// | | |
// | 25 | 21 29 49 63 85 111 142 163 193 222 |
// | | |
// | 26 | 27 48 77 103 133 158 179 196 215 232 |
// | | |
// | 27 | 29 47 74 99 124 151 176 198 220 237 |
// | | |
// | 28 | 33 42 61 76 93 121 155 174 207 225 |
// | | |
// | 29 | 29 53 87 112 136 154 170 188 208 227 |
// | | |
// | 30 | 24 30 52 84 131 150 166 186 203 229 |
// | | |
// | 31 | 37 48 64 84 104 118 156 177 201 230 |
// +----+----------------------------------------+
//
// Table 23: NB/MB Normalized LSF Stage-1 Codebook Vectors
codebookNormalizedLSFStageOneNarrowbandOrMediumband = [][]uint{
{12, 35, 60, 83, 108, 132, 157, 180, 206, 228},
{15, 32, 55, 77, 101, 125, 151, 175, 201, 225},
{19, 42, 66, 89, 114, 137, 162, 184, 209, 230},
{12, 25, 50, 72, 97, 120, 147, 172, 200, 223},
{26, 44, 69, 90, 114, 135, 159, 180, 205, 225},
{13, 22, 53, 80, 106, 130, 156, 180, 205, 228},
{15, 25, 44, 64, 90, 115, 142, 168, 196, 222},
{19, 24, 62, 82, 100, 120, 145, 168, 190, 214},
{22, 31, 50, 79, 103, 120, 151, 170, 203, 227},
{21, 29, 45, 65, 106, 124, 150, 171, 196, 224},
{30, 49, 75, 97, 121, 142, 165, 186, 209, 229},
{19, 25, 52, 70, 93, 116, 143, 166, 192, 219},
{26, 34, 62, 75, 97, 118, 145, 167, 194, 217},
{25, 33, 56, 70, 91, 113, 143, 165, 196, 223},
{21, 34, 51, 72, 97, 117, 145, 171, 196, 222},
{20, 29, 50, 67, 90, 117, 144, 168, 197, 221},
{22, 31, 48, 66, 95, 117, 146, 168, 196, 222},
{24, 33, 51, 77, 116, 134, 158, 180, 200, 224},
{21, 28, 70, 87, 106, 124, 149, 170, 194, 217},
{26, 33, 53, 64, 83, 117, 152, 173, 204, 225},
{27, 34, 65, 95, 108, 129, 155, 174, 210, 225},
{20, 26, 72, 99, 113, 131, 154, 176, 200, 219},
{34, 43, 61, 78, 93, 114, 155, 177, 205, 229},
{23, 29, 54, 97, 124, 138, 163, 179, 209, 229},
{30, 38, 56, 89, 118, 129, 158, 178, 200, 231},
{21, 29, 49, 63, 85, 111, 142, 163, 193, 222},
{27, 48, 77, 103, 133, 158, 179, 196, 215, 232},
{29, 47, 74, 99, 124, 151, 176, 198, 220, 237},
{33, 42, 61, 76, 93, 121, 155, 174, 207, 225},
{29, 53, 87, 112, 136, 154, 170, 188, 208, 227},
{24, 30, 52, 84, 131, 150, 166, 186, 203, 229},
{37, 48, 64, 84, 104, 118, 156, 177, 201, 230},
}
// +----+------------------------------------------------------------+
// | I1 | Codebook (Q8) |
// +----+------------------------------------------------------------+
// | | 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
// | | |
// | 0 | 7 23 38 54 69 85 100 116 131 147 162 178 193 208 223 239 |
// | | |
// | 1 | 13 25 41 55 69 83 98 112 127 142 157 171 187 203 220 236 |
// | | |
// | 2 | 15 21 34 51 61 78 92 106 126 136 152 167 185 205 225 240 |
// | | |
// | 3 | 10 21 36 50 63 79 95 110 126 141 157 173 189 205 221 237 |
// | | |
// | 4 | 17 20 37 51 59 78 89 107 123 134 150 164 184 205 224 240 |
// | | |
// | 5 | 10 15 32 51 67 81 96 112 129 142 158 173 189 204 220 236 |
// | | |
// | 6 | 8 21 37 51 65 79 98 113 126 138 155 168 179 192 209 218 |
// | | |
// | 7 | 12 15 34 55 63 78 87 108 118 131 148 167 185 203 219 236 |
// | | |
// | 8 | 16 19 32 36 56 79 91 108 118 136 154 171 186 204 220 237 |
// | | |
// | 9 | 11 28 43 58 74 89 105 120 135 150 165 180 196 211 226 241 |
// | | |
// | 10 | 6 16 33 46 60 75 92 107 123 137 156 169 185 199 214 225 |
// | | |
// | 11 | 11 19 30 44 57 74 89 105 121 135 152 169 186 202 218 234 |
// | | |
// | 12 | 12 19 29 46 57 71 88 100 120 132 148 165 182 199 216 233 |
// | | |
// | 13 | 17 23 35 46 56 77 92 106 123 134 152 167 185 204 222 237 |
// | | |
// | 14 | 14 17 45 53 63 75 89 107 115 132 151 171 188 206 221 240 |
// | | |
// | 15 | 9 16 29 40 56 71 88 103 119 137 154 171 189 205 222 237 |
// | | |
// | 16 | 16 19 36 48 57 76 87 105 118 132 150 167 185 202 218 236 |
// | | |
// | 17 | 12 17 29 54 71 81 94 104 126 136 149 164 182 201 221 237 |
// | | |
// | 18 | 15 28 47 62 79 97 115 129 142 155 168 180 194 208 223 238 |
// | | |
// | 19 | 8 14 30 45 62 78 94 111 127 143 159 175 192 207 223 239 |
// | | |
// | 20 | 17 30 49 62 79 92 107 119 132 145 160 174 190 204 220 235 |
// | | |
// | 21 | 14 19 36 45 61 76 91 108 121 138 154 172 189 205 222 238 |
// | | |
// | 22 | 12 18 31 45 60 76 91 107 123 138 154 171 187 204 221 236 |
// | | |
// | 23 | 13 17 31 43 53 70 83 103 114 131 149 167 185 203 220 237 |
// | | |
// | 24 | 17 22 35 42 58 78 93 110 125 139 155 170 188 206 224 240 |
// | | |
// | 25 | 8 15 34 50 67 83 99 115 131 146 162 178 193 209 224 239 |
// | | |
// | 26 | 13 16 41 66 73 86 95 111 128 137 150 163 183 206 225 241 |
// | | |
// | 27 | 17 25 37 52 63 75 92 102 119 132 144 160 175 191 212 231 |
// | | |
// | 28 | 19 31 49 65 83 100 117 133 147 161 174 187 200 213 227 242 |
// | | |
// | 29 | 18 31 52 68 88 103 117 126 138 149 163 177 192 207 223 239 |
// | | |
// | 30 | 16 29 47 61 76 90 106 119 133 147 161 176 193 209 224 240 |
// | | |
// | 31 | 15 21 35 50 61 73 86 97 110 119 129 141 175 198 218 237 |
// +----+------------------------------------------------------------+
// Table 24: WB Normalized LSF Stage-1 Codebook Vectors
codebookNormalizedLSFStageOneWideband = [][]uint{
{7, 23, 38, 54, 69, 85, 100, 116, 131, 147, 162, 178, 193, 208, 223, 239},
{13, 25, 41, 55, 69, 83, 98, 112, 127, 142, 157, 171, 187, 203, 220, 236},
{15, 21, 34, 51, 61, 78, 92, 106, 126, 136, 152, 167, 185, 205, 225, 240},
{10, 21, 36, 50, 63, 79, 95, 110, 126, 141, 157, 173, 189, 205, 221, 237},
{17, 20, 37, 51, 59, 78, 89, 107, 123, 134, 150, 164, 184, 205, 224, 240},
{10, 15, 32, 51, 67, 81, 96, 112, 129, 142, 158, 173, 189, 204, 220, 236},
{8, 21, 37, 51, 65, 79, 98, 113, 126, 138, 155, 168, 179, 192, 209, 218},
{12, 15, 34, 55, 63, 78, 87, 108, 118, 131, 148, 167, 185, 203, 219, 236},
{16, 19, 32, 36, 56, 79, 91, 108, 118, 136, 154, 171, 186, 204, 220, 237},
{11, 28, 43, 58, 74, 89, 105, 120, 135, 150, 165, 180, 196, 211, 226, 241},
{6, 16, 33, 46, 60, 75, 92, 107, 123, 137, 156, 169, 185, 199, 214, 225},
{11, 19, 30, 44, 57, 74, 89, 105, 121, 135, 152, 169, 186, 202, 218, 234},
{12, 19, 29, 46, 57, 71, 88, 100, 120, 132, 148, 165, 182, 199, 216, 233},
{17, 23, 35, 46, 56, 77, 92, 106, 123, 134, 152, 167, 185, 204, 222, 237},
{14, 17, 45, 53, 63, 75, 89, 107, 115, 132, 151, 171, 188, 206, 221, 240},
{9, 16, 29, 40, 56, 71, 88, 103, 119, 137, 154, 171, 189, 205, 222, 237},
{16, 19, 36, 48, 57, 76, 87, 105, 118, 132, 150, 167, 185, 202, 218, 236},
{12, 17, 29, 54, 71, 81, 94, 104, 126, 136, 149, 164, 182, 201, 221, 237},
{15, 28, 47, 62, 79, 97, 115, 129, 142, 155, 168, 180, 194, 208, 223, 238},
{8, 14, 30, 45, 62, 78, 94, 111, 127, 143, 159, 175, 192, 207, 223, 239},
{17, 30, 49, 62, 79, 92, 107, 119, 132, 145, 160, 174, 190, 204, 220, 235},
{14, 19, 36, 45, 61, 76, 91, 108, 121, 138, 154, 172, 189, 205, 222, 238},
{12, 18, 31, 45, 60, 76, 91, 107, 123, 138, 154, 171, 187, 204, 221, 236},
{13, 17, 31, 43, 53, 70, 83, 103, 114, 131, 149, 167, 185, 203, 220, 237},
{17, 22, 35, 42, 58, 78, 93, 110, 125, 139, 155, 170, 188, 206, 224, 240},
{8, 15, 34, 50, 67, 83, 99, 115, 131, 146, 162, 178, 193, 209, 224, 239},
{13, 16, 41, 66, 73, 86, 95, 111, 128, 137, 150, 163, 183, 206, 225, 241},
{17, 25, 37, 52, 63, 75, 92, 102, 119, 132, 144, 160, 175, 191, 212, 231},
{19, 31, 49, 65, 83, 100, 117, 133, 147, 161, 174, 187, 200, 213, 227, 242},
{18, 31, 52, 68, 88, 103, 117, 126, 138, 149, 163, 177, 192, 207, 223, 239},
{16, 29, 47, 61, 76, 90, 106, 119, 133, 147, 161, 176, 193, 209, 224, 240},
{15, 21, 35, 50, 61, 73, 86, 97, 110, 119, 129, 141, 175, 198, 218, 237},
}
)

View file

@ -195,7 +195,7 @@ func (d *Decoder) decodeNormalizedLineSpectralFrequencyStageOne(voiceActivityDet
// Predictive Coding (LPC) coefficients for the current SILK frame.
//
// https://datatracker.ietf.org/doc/html/rfc6716#section-4.2.7.5.2
func (d *Decoder) decodeNormalizedLineSpectralFrequencyStageTwo(voiceActivityDetected bool, bandwidth Bandwidth, I1 uint32) (resQ10 []int16) {
func (d *Decoder) decodeNormalizedLineSpectralFrequencyStageTwo(bandwidth Bandwidth, I1 uint32) (resQ10 []int16) {
// Decoding the second stage residual proceeds as follows. For each
// coefficient, the decoder reads a symbol using the PDF corresponding
// to I1 from either Table 17 or Table 18,
@ -290,6 +290,86 @@ func (d *Decoder) decodeNormalizedLineSpectralFrequencyStageTwo(voiceActivityDet
return
}
// Once the stage-1 index I1 and the stage-2 residual res_Q10[] have
// been decoded, the final normalized LSF coefficients can be
// reconstructed.
//
// https://datatracker.ietf.org/doc/html/rfc6716#section-4.2.7.5.3
func (d *Decoder) reconstructNormalizedLineSpectralFrequencyCoefficients(bandwidth Bandwidth, resQ10 []int16, I1 uint32) (nlsfQ15 []int16) {
// Let d_LPC be the order of the codebook, i.e., 10 for NB and MB, and 16 for WB
dLPC := len(resQ10)
nlsfQ15 = make([]int16, len(resQ10))
w2Q18 := make([]uint, len(resQ10))
wQ9 := make([]int16, len(resQ10))
cb1Q8 := codebookNormalizedLSFStageOneNarrowbandOrMediumband
if bandwidth == BandwidthWideband {
cb1Q8 = codebookNormalizedLSFStageOneWideband
}
// Let cb1_Q8[k] be the k'th entry of the stage-1 codebook vector from Table 23 or Table 24.
// Then, for 0 <= k < d_LPC, the following expression computes the
// square of the weight as a Q18 value:
//
// w2_Q18[k] = (1024/(cb1_Q8[k] - cb1_Q8[k-1])
// + 1024/(cb1_Q8[k+1] - cb1_Q8[k])) << 16
//
// where cb1_Q8[-1] = 0 and cb1_Q8[d_LPC] = 256, and the division is
// integer division. This is reduced to an unsquared, Q9 value using
// the following square-root approximation:
//
// https://datatracker.ietf.org/doc/html/rfc6716#section-4.2.7.5.3
for k := 0; k < dLPC; k++ {
var kMinusOne, kPlusOne uint
if k != 0 {
kMinusOne = cb1Q8[I1][k-1]
}
if k+1 == dLPC {
kPlusOne = 256
} else {
kPlusOne = cb1Q8[I1][k+1]
}
w2Q18[k] = (1024/(cb1Q8[I1][k]-kMinusOne) +
1024/(kPlusOne-cb1Q8[I1][k])) << 16
// This is reduced to an unsquared, Q9 value using
// the following square-root approximation:
//
// i = ilog(w2_Q18[k])
// f = (w2_Q18[k]>>(i-8)) & 127
// y = ((i&1) ? 32768 : 46214) >> ((32-i)>>1)
// w_Q9[k] = y + ((213*f*y)>>16)
//
// https://datatracker.ietf.org/doc/html/rfc6716#section-4.2.7.5.3
i := ilog(int(w2Q18[k]))
f := int((w2Q18[k] >> (i - 8)) & 127)
y := 46214
if (i & 1) != 0 {
y = 32768
}
y = y >> ((32 - i) >> 1)
wQ9[k] = int16(y + ((213 * f * y) >> 16))
// Given the stage-1 codebook entry cb1_Q8[], the stage-2 residual
// res_Q10[], and their corresponding weights, w_Q9[], the reconstructed
// normalized LSF coefficients are
//
// NLSF_Q15[k] = clamp(0,
// (cb1_Q8[k]<<7) + (res_Q10[k]<<14)/w_Q9[k], 32767)
//
// https://datatracker.ietf.org/doc/html/rfc6716#section-4.2.7.5.3
nlsfQ15[k] = int16(clamp(0,
int32((int(cb1Q8[I1][k])<<7)+(int(resQ10[k])<<14)/int(wQ9[k])), 32767))
}
return
}
// Decode decodes many SILK subframes
// An overview of the decoder is given in Figure 14.
//
@ -349,8 +429,11 @@ func (d *Decoder) Decode(in []byte, isStereo bool, nanoseconds int, bandwidth Ba
// https://datatracker.ietf.org/doc/html/rfc6716#section-4.2.7.5.1
I1 := d.decodeNormalizedLineSpectralFrequencyStageOne(voiceActivityDetected, bandwidth)
// https://datatracker.ietf.org/doc/html/rfc6716#section-4.2.7.5.1
d.decodeNormalizedLineSpectralFrequencyStageTwo(voiceActivityDetected, bandwidth, I1)
// https://datatracker.ietf.org/doc/html/rfc6716#section-4.2.7.5.2
resQ10 := d.decodeNormalizedLineSpectralFrequencyStageTwo(bandwidth, I1)
// https://datatracker.ietf.org/doc/html/rfc6716#section-4.2.7.5.3
d.reconstructNormalizedLineSpectralFrequencyCoefficients(bandwidth, resQ10, I1)
return
}