1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
use crate::g729::basic_operations::*;
use crate::g729::codebooks::*;
use crate::g729::fixed_point_math::*;
use crate::g729::ld8k::*;
use crate::g729::utils::*;
/* static buffers */
const PREVIOUS_QLSF_INIT: [Word16; NB_LSP_COEFF] = [
2339, 4679, 7018, 9358, 11698, 14037, 16377, 18717, 21056, 23396,
]; /* PI*(float)(j+1)/(float)(M+1) */
/* initialise the stactic buffers */
pub fn init_lsp_quantization(previous_q_lsf: &mut [[Word16; NB_LSP_COEFF]; MA_MAX_K]) {
for i in 0..MA_MAX_K {
previous_q_lsf[i].copy_from_slice(&PREVIOUS_QLSF_INIT);
}
}
/**********************************************************************************/
/* noiseLSPQuantization : Convert LSP to LSF, Quantize LSF and find L parameters, */
/* qLSF->qLSP as described in spec A3.2.4 */
/* parameters: */
/* -(i/o) previousqLSF : 4 previousqLSF, is updated by this function */
/* -(i) LSPCoefficients : 10 LSP coefficients in Q15 */
/* -(o) qLSPCoefficients : 10 qLSP coefficients in Q15 */
/* -(o) parameters : 3 parameters L0, L1, L2 */
/* */
/**********************************************************************************/
pub fn noise_lsp_quantization(
previous_q_lsf: &mut [[Word16; NB_LSP_COEFF]; MA_MAX_K],
lsp_coefficients: &[Word16],
q_lsp_coefficients: &mut [Word16],
parameters: &mut [u8], // Using u8 as in C uint8_t
) {
let mut lsf = [0; NB_LSP_COEFF]; /* LSF coefficients in Q2.13 range [0, Pi[ */
let mut weights = [0 as UWord16; NB_LSP_COEFF]; /* weights in Q11 */
let mut weights_threshold = [0; NB_LSP_COEFF]; /* store in Q13 the threshold used to compute the weights */
let mut l1_index = [0; L0_RANGE];
let mut l2_index = [0; L0_RANGE];
let mut weighted_mean_square_error = [0 as UWord32; L0_RANGE];
let mut quantizer_output = [0; NB_LSP_COEFF];
let mut q_lsf = [0; NB_LSP_COEFF];
/*** compute LSF in Q2.13 : lsf = arcos(lsp) range [0, Pi[ spec 3.2.4 eq18 ***/
for i in 0..NB_LSP_COEFF {
lsf[i] = g729_acos_q15q13(lsp_coefficients[i]);
}
/*** compute the weights vector as in spec 3.2.4 eq22 ***/
weights_threshold[0] = sub16(lsf[1], OO4PIPLUS1_IN_Q13);
for i in 1..NB_LSP_COEFF - 1 {
weights_threshold[i] = sub16(sub16(lsf[i + 1], lsf[i - 1]), ONE_IN_Q13 as Word16);
}
weights_threshold[NB_LSP_COEFF - 1] = sub16(O92PIMINUS1_IN_Q13, lsf[NB_LSP_COEFF - 2]);
for i in 0..NB_LSP_COEFF {
if weights_threshold[i] > 0 {
weights[i] = ONE_IN_Q11 as UWord16;
} else {
weights[i] = usaturate(
add32(
pshr(
mult16_16(
mult16_16_q13(weights_threshold[i], weights_threshold[i]) as Word16,
10,
),
2,
),
ONE_IN_Q11 as Word32,
),
MAX_16 as Word32,
) as UWord16;
}
}
weights[4] = mult16_16_q14(weights[4] as Word16, ONE_POINT_2_IN_Q14) as UWord16;
weights[5] = mult16_16_q14(weights[5] as Word16, ONE_POINT_2_IN_Q14) as UWord16;
/*** compute the coefficients for the two noise noise MA Predictors ***/
for l0 in 0..L0_RANGE {
/* compute the target Vector (l) to be quantized as in spec 3.2.4 eq23 */
let mut target_vector = [0; NB_LSP_COEFF]; /* vector to be quantized in Q13 */
let mut mean_square_diff: Word32 = MAX_32;
let mut quantized_vector = [0; NB_LSP_COEFF]; /* in Q13, the current state of quantized vector */
for i in 0..NB_LSP_COEFF {
let mut acc = shl(lsf[i] as Word32, 15); /* acc in Q2.28 */
for j in 0..MA_MAX_K {
acc = msu16_16(acc, previous_q_lsf[j][i], NOISE_MA_PREDICTOR[l0][j][i]);
/* previousqLSF in Q2.13 and MAPredictor in Q0.15-> acc in Q2.28 */
}
target_vector[i] =
mult16_16_q12(pshr(acc, 15) as Word16, INV_NOISE_MA_PREDICTOR_SUM[l0][i]);
/* acc->Q13 and invMAPredictorSum in Q12 -> targetVector in Q13 */
}
/* find closest match for predictionError (minimize mean square diff) in L1 subset codebook: 32 entries from L1 codebook */
for i in 0..NOISE_L1_RANGE {
let mut acc: Word32 = 0;
for j in 0..NB_LSP_COEFF {
let diff_target_vector_l1 = saturate(
sub32(
target_vector[j] as Word32,
L1[L1_SUBSET_INDEX[i]][j] as Word32,
),
MAX_16 as Word32,
) as Word16;
acc = mac16_16(acc, diff_target_vector_l1, diff_target_vector_l1);
}
if acc < mean_square_diff {
mean_square_diff = acc;
l1_index[l0] = i;
}
}
/* find the closest match in L2 subset wich will minimise the weighted sum of (targetVector - L1 result - L2)^2 */
/* using eq20, eq21 and eq23 in spec 3.2.4 -> l[i] - l^[i] = (wi - w^[i])/(1-SumMAPred[i]) but ITU code ignores this denominator */
/* works on the first five coefficients only */
mean_square_diff = MAX_32;
for i in 0..NOISE_L2_RANGE {
let mut acc: Word32 = 0;
for j in 0..NB_LSP_COEFF / 2 {
/* commented code : compute in the same way of the ITU code: ignore the denonimator and minimize (wi - w^[i])/(1-SumMAPred[i]) instead of (wi - w^[i]) square sum */
let diff_target_vector_l1_l2 = saturate(
mult16_16_q15(
sub32(
sub32(
target_vector[j] as Word32,
L1[L1_SUBSET_INDEX[l1_index[l0]]][j] as Word32,
),
L2L3[L2_SUBSET_INDEX[i]][j] as Word32,
) as Word16,
NOISE_MA_PREDICTOR_SUM[l0][j],
),
MAX_16 as Word32,
) as Word16; /* targetVector, L1 and L2L3 in Q13 -> result in Q13 */
acc = mac16_16(
acc,
diff_target_vector_l1_l2,
mult16_16_q11(diff_target_vector_l1_l2, weights[j] as Word16) as Word16,
); /* weights in Q11, diff in Q13 */
}
for j in NB_LSP_COEFF / 2..NB_LSP_COEFF {
let diff_target_vector_l1_l3 = saturate(
mult16_16_q15(
sub32(
sub32(
target_vector[j] as Word32,
L1[L1_SUBSET_INDEX[l1_index[l0]]][j] as Word32,
),
L2L3[L3_SUBSET_INDEX[i]][j] as Word32,
) as Word16,
NOISE_MA_PREDICTOR_SUM[l0][j],
),
MAX_16 as Word32,
) as Word16; /* targetVector, L1 and L2L3 in Q13 -> result in Q13 */
acc = mac16_16(
acc,
diff_target_vector_l1_l3,
mult16_16_q11(diff_target_vector_l1_l3, weights[j] as Word16) as Word16,
); /* weights in Q11, diff in Q13 */
}
if acc < mean_square_diff {
mean_square_diff = acc;
l2_index[l0] = i;
}
}
/* compute the quantized vector L1+L2/L3 and rearrange it as specified in spec 3.2.4 */
/* Note: according to the spec, the rearrangement shall be done on each candidate while looking for best match, but the ITU code does it after picking the best match and so we do */
for i in 0..NB_LSP_COEFF / 2 {
quantized_vector[i] = add16(
L1[L1_SUBSET_INDEX[l1_index[l0]]][i],
L2L3[L2_SUBSET_INDEX[l2_index[l0]]][i],
);
}
for i in NB_LSP_COEFF / 2..NB_LSP_COEFF {
quantized_vector[i] = add16(
L1[L1_SUBSET_INDEX[l1_index[l0]]][i],
L2L3[L3_SUBSET_INDEX[l2_index[l0]]][i],
);
}
/* rearrange with a minimum distance of 0.0012 */
for i in 1..NB_LSP_COEFF / 2 {
if quantized_vector[i - 1] > sub16(quantized_vector[i], GAP1) {
quantized_vector[i - 1] = pshr(
sub16(add16(quantized_vector[i], quantized_vector[i - 1]), GAP1) as Word32,
1,
) as Word16;
quantized_vector[i] = pshr(
add16(add16(quantized_vector[i], quantized_vector[i - 1]), GAP1) as Word32,
1,
) as Word16;
}
}
for i in NB_LSP_COEFF / 2 + 1..NB_LSP_COEFF {
if quantized_vector[i - 1] > sub16(quantized_vector[i], GAP1) {
quantized_vector[i - 1] = pshr(
sub16(add16(quantized_vector[i], quantized_vector[i - 1]), GAP1) as Word32,
1,
) as Word16;
quantized_vector[i] = pshr(
add16(add16(quantized_vector[i], quantized_vector[i - 1]), GAP1) as Word32,
1,
) as Word16;
}
}
/* rearrange the whole quantizedVector with a distance of 0.0006 */
for i in 1..NB_LSP_COEFF {
if quantized_vector[i - 1] > sub16(quantized_vector[i], GAP2) {
quantized_vector[i - 1] = pshr(
sub16(add16(quantized_vector[i], quantized_vector[i - 1]), GAP2) as Word32,
1,
) as Word16;
quantized_vector[i] = pshr(
add16(add16(quantized_vector[i], quantized_vector[i - 1]), GAP2) as Word32,
1,
) as Word16;
}
}
/* compute the weighted mean square distance using the final quantized vector according to eq21 */
weighted_mean_square_error[l0] = 0;
for i in 0..NB_LSP_COEFF {
let diff_target_vector_quantized_vector = usaturate(
abs(mult16_32_q15(
NOISE_MA_PREDICTOR_SUM[l0][i],
sub32(target_vector[i] as Word32, quantized_vector[i] as Word32),
)) as Word32,
MAX_U16 as Word32,
) as UWord16; /* targetVector and quantizedVector in Q13 -> result in Q13 */
weighted_mean_square_error[l0] = umac16_16(
weighted_mean_square_error[l0],
diff_target_vector_quantized_vector,
mult16_16_q11(
diff_target_vector_quantized_vector as Word16,
weights[i] as Word16,
) as UWord16,
); /* weights in Q11, diff in Q13 */
}
}
/* now select L0 and copy the selected coefficients to the output buffer */
if weighted_mean_square_error[0] < weighted_mean_square_error[1] {
parameters[0] = 0;
parameters[1] = l1_index[0] as u8;
parameters[2] = l2_index[0] as u8;
} else {
parameters[0] = 1;
parameters[1] = l1_index[1] as u8;
parameters[2] = l2_index[1] as u8;
}
/*** Compute the quantized LSF from the L coefficients ***/
/* reconstruct vector from the codebooks using the selected parameters spec 3.2.4 eq19 */
for i in 0..NB_LSP_COEFF / 2 {
quantizer_output[i] = add16(
L1[L1_SUBSET_INDEX[parameters[1] as usize]][i],
L2L3[L2_SUBSET_INDEX[parameters[2] as usize]][i],
); /* codebooks are in Q2.13 for L1 and Q0.13 for L2L3, due to actual values stored in the codebooks, result in Q2.13 */
}
for i in NB_LSP_COEFF / 2..NB_LSP_COEFF {
quantizer_output[i] = add16(
L1[L1_SUBSET_INDEX[parameters[1] as usize]][i],
L2L3[L3_SUBSET_INDEX[parameters[2] as usize]][i],
); /* same as previous, output in Q2.13 */
}
/* rearrange in order to have a minimum distance between two consecutives coefficients spec 3.2.4 */
rearrange_coefficients(&mut quantizer_output, GAP1);
rearrange_coefficients(&mut quantizer_output, GAP2); /* currentqLSF still in Q2.13 */
/* compute qLSF spec 3.2.4 eq20 */
for i in 0..NB_LSP_COEFF {
let mut acc = mult16_16(
NOISE_MA_PREDICTOR_SUM[parameters[0] as usize][i],
quantizer_output[i],
); /* (1 - ∑Pi,k)*lˆi(m) Q15 * Q13 -> Q28 */
for j in 0..MA_MAX_K {
acc = mac16_16(
acc,
NOISE_MA_PREDICTOR[parameters[0] as usize][j][i],
previous_q_lsf[j][i],
);
}
/* acc in Q2.28, shift back the acc to a Q2.13 with rounding */
q_lsf[i] = pshr(acc, 15) as Word16; /* qLSF in Q2.13 */
}
/* update the previousqLSF buffer with current quantizer output */
for i in (1..MA_MAX_K).rev() {
previous_q_lsf[i] = previous_q_lsf[i - 1];
}
previous_q_lsf[0].copy_from_slice(&quantizer_output);
/*** qLSF stability check ***/
insertion_sort(&mut q_lsf, NB_LSP_COEFF);
/* check for low limit on qLSF[0] */
if q_lsf[1] < QLSF_MIN {
q_lsf[1] = QLSF_MIN;
}
/* check and rectify minimum distance between two consecutive qLSF */
for i in 0..NB_LSP_COEFF - 1 {
if sub16(q_lsf[i + 1], q_lsf[i]) < MIN_QLSF_DISTANCE {
q_lsf[i + 1] = q_lsf[i] + MIN_QLSF_DISTANCE;
}
}
/* check for upper limit on qLSF[NB_LSP_COEFF-1] */
if q_lsf[NB_LSP_COEFF - 1] > QLSF_MAX {
q_lsf[NB_LSP_COEFF - 1] = QLSF_MAX;
}
/* convert qLSF to qLSP: qLSP = cos(qLSF) */
for i in 0..NB_LSP_COEFF {
q_lsp_coefficients[i] = g729_cos_q13q15(q_lsf[i]); /* ouput in Q0.15 */
}
}
/*****************************************************************************/
/* LSPQuantization : Convert LSP to LSF, Quantize LSF and find L parameters, */
/* qLSF->qLSP as described in spec A3.2.4 */
/* parameters: */
/* -(i/o) encoderChannelContext : the channel context data */
/* -(i) LSPCoefficients : 10 LSP coefficients in Q15 */
/* -(o) qLSPCoefficients : 10 qLSP coefficients in Q15 */
/* -(o) parameters : 4 parameters L0, L1, L2, L3 */
/* */
/*****************************************************************************/
pub fn lsp_quantization(
previous_q_lsf: &mut [[Word16; NB_LSP_COEFF]; MA_MAX_K],
lsp_coefficients: &[Word16],
q_lsp_coefficients: &mut [Word16],
parameters: &mut [u16], // Using u16 as in C uint16_t
) {
let mut lsf = [0; NB_LSP_COEFF]; /* LSF coefficients in Q2.13 range [0, Pi[ */
let mut weights = [0 as UWord16; NB_LSP_COEFF]; /* weights in Q11 */
let mut weights_threshold = [0; NB_LSP_COEFF]; /* store in Q13 the threshold used to compute the weights */
let mut l1_index = [0; L0_RANGE];
let mut l2_index = [0; L0_RANGE];
let mut l3_index = [0; L0_RANGE];
let mut weighted_mean_square_error = [0 as UWord32; L0_RANGE];
let mut quantizer_output = [0; NB_LSP_COEFF];
let mut q_lsf = [0; NB_LSP_COEFF];
/*** compute LSF in Q2.13 : lsf = arcos(lsp) range [0, Pi[ spec 3.2.4 eq18 ***/
for i in 0..NB_LSP_COEFF {
lsf[i] = g729_acos_q15q13(lsp_coefficients[i]);
}
/*** compute the weights vector as in spec 3.2.4 eq22 ***/
weights_threshold[0] = sub16(lsf[1], OO4PIPLUS1_IN_Q13);
for i in 1..NB_LSP_COEFF - 1 {
weights_threshold[i] = sub16(sub16(lsf[i + 1], lsf[i - 1]), ONE_IN_Q13 as Word16);
}
weights_threshold[NB_LSP_COEFF - 1] = sub16(O92PIMINUS1_IN_Q13, lsf[NB_LSP_COEFF - 2]);
for i in 0..NB_LSP_COEFF {
if weights_threshold[i] > 0 {
weights[i] = ONE_IN_Q11 as UWord16;
} else {
weights[i] = usaturate(
add32(
pshr(
mult16_16(
mult16_16_q13(weights_threshold[i], weights_threshold[i]) as Word16,
10,
),
2,
),
ONE_IN_Q11 as Word32,
),
MAX_16 as Word32,
) as UWord16;
}
}
weights[4] = mult16_16_q14(weights[4] as Word16, ONE_POINT_2_IN_Q14) as UWord16;
weights[5] = mult16_16_q14(weights[5] as Word16, ONE_POINT_2_IN_Q14) as UWord16;
/*** compute the coefficients for the two MA Predictors ***/
for l0 in 0..L0_RANGE {
/* compute the target Vector (l) to be quantized as in spec 3.2.4 eq23 */
let mut target_vector = [0; NB_LSP_COEFF]; /* vector to be quantized in Q13 */
let mut mean_square_diff: Word32 = MAX_32;
let mut quantized_vector = [0; NB_LSP_COEFF]; /* in Q13, the current state of quantized vector */
for i in 0..NB_LSP_COEFF {
let mut acc = shl(lsf[i] as Word32, 15); /* acc in Q2.28 */
for j in 0..MA_MAX_K {
acc = msu16_16(acc, previous_q_lsf[j][i], MA_PREDICTOR[l0][j][i]);
/* previousqLSF in Q2.13 and MAPredictor in Q0.15-> acc in Q2.28 */
}
target_vector[i] = mult16_16_q12(pshr(acc, 15) as Word16, INV_MA_PREDICTOR_SUM[l0][i]);
/* acc->Q13 and invMAPredictorSum in Q12 -> targetVector in Q13 */
}
/* find closest match for predictionError (minimize mean square diff) in L1 subset codebook: 32 entries from L1 codebook */
for i in 0..L1_RANGE {
let mut acc: Word32 = 0;
for j in 0..NB_LSP_COEFF {
let diff_target_vector_l1 = saturate(
sub32(target_vector[j] as Word32, L1[i][j] as Word32),
MAX_16 as Word32,
) as Word16;
acc = mac16_16(acc, diff_target_vector_l1, diff_target_vector_l1);
}
if acc < mean_square_diff {
mean_square_diff = acc;
l1_index[l0] = i;
}
}
/* find the closest match in L2 wich will minimise the weighted sum of (targetVector - L1 result - L2)^2 */
/* using eq20, eq21 and eq23 in spec 3.2.4 -> l[i] - l^[i] = (wi - w^[i])/(1-SumMAPred[i]) but ITU code ignores this denominator */
/* works on the first five coefficients only */
mean_square_diff = MAX_32;
for i in 0..L2_RANGE {
let mut acc: Word32 = 0;
for j in 0..NB_LSP_COEFF / 2 {
/* commented code : compute in the same way of the ITU code: ignore the denonimator and minimize (wi - w^[i])/(1-SumMAPred[i]) instead of (wi - w^[i]) square sum */
let diff_target_vector_l1_l2 = saturate(
mult16_16_q15(
sub32(
sub32(target_vector[j] as Word32, L1[l1_index[l0]][j] as Word32),
L2L3[i][j] as Word32,
) as Word16,
MA_PREDICTOR_SUM[l0][j],
),
MAX_16 as Word32,
) as Word16; /* targetVector, L1 and L2L3 in Q13 -> result in Q13 */
acc = mac16_16(
acc,
diff_target_vector_l1_l2,
mult16_16_q11(diff_target_vector_l1_l2, weights[j] as Word16) as Word16,
); /* weights in Q11, diff in Q13 */
}
if acc < mean_square_diff {
mean_square_diff = acc;
l2_index[l0] = i;
}
}
/* find the closest match in L3 wich will minimise the weighted sum of (targetVector - L1 result - L3)^2 */
/* using eq20, eq21 and eq23 in spec 3.2.4 -> l[i] - l^[i] = (wi - w^[i])/(1-SumMAPred[i]) but ITU code ignores this denominator */
/* works on the first five coefficients only */
mean_square_diff = MAX_32;
for i in 0..L2_RANGE {
let mut acc: Word32 = 0;
for j in NB_LSP_COEFF / 2..NB_LSP_COEFF {
/* commented code : compute in the same way of the ITU code: ignore the denonimator and minimize (wi - w^[i])/(1-SumMAPred[i]) instead of (wi - w^[i]) square sum */
let diff_target_vector_l1_l3 = saturate(
mult16_16_q15(
sub32(
sub32(target_vector[j] as Word32, L1[l1_index[l0]][j] as Word32),
L2L3[i][j] as Word32,
) as Word16,
MA_PREDICTOR_SUM[l0][j],
),
MAX_16 as Word32,
) as Word16; /* targetVector, L1 and L2L3 in Q13 -> result in Q13 */
acc = mac16_16(
acc,
diff_target_vector_l1_l3,
mult16_16_q11(diff_target_vector_l1_l3, weights[j] as Word16) as Word16,
); /* weights in Q11, diff in Q13 */
}
if acc < mean_square_diff {
mean_square_diff = acc;
l3_index[l0] = i;
}
}
/* compute the quantized vector L1+L2/L3 and rearrange it as specified in spec 3.2.4(first the higher part (L2) and then the lower part (L3)) */
/* Note: according to the spec, the rearrangement shall be done on each candidate while looking for best match, but the ITU code does it after picking the best match and so we do */
for i in 0..NB_LSP_COEFF / 2 {
quantized_vector[i] = add16(L1[l1_index[l0]][i], L2L3[l2_index[l0]][i]);
}
for i in NB_LSP_COEFF / 2..NB_LSP_COEFF {
quantized_vector[i] = add16(L1[l1_index[l0]][i], L2L3[l3_index[l0]][i]);
}
/* rearrange with a minimum distance of 0.0012 */
for i in 1..NB_LSP_COEFF / 2 {
if quantized_vector[i - 1] > sub16(quantized_vector[i], GAP1) {
quantized_vector[i - 1] = pshr(
sub16(add16(quantized_vector[i], quantized_vector[i - 1]), GAP1) as Word32,
1,
) as Word16;
quantized_vector[i] = pshr(
add16(add16(quantized_vector[i], quantized_vector[i - 1]), GAP1) as Word32,
1,
) as Word16;
}
}
for i in NB_LSP_COEFF / 2 + 1..NB_LSP_COEFF {
if quantized_vector[i - 1] > sub16(quantized_vector[i], GAP1) {
quantized_vector[i - 1] = pshr(
sub16(add16(quantized_vector[i], quantized_vector[i - 1]), GAP1) as Word32,
1,
) as Word16;
quantized_vector[i] = pshr(
add16(add16(quantized_vector[i], quantized_vector[i - 1]), GAP1) as Word32,
1,
) as Word16;
}
}
/* rearrange the whole quantizedVector with a distance of 0.0006 */
for i in 1..NB_LSP_COEFF {
if quantized_vector[i - 1] > sub16(quantized_vector[i], GAP2) {
quantized_vector[i - 1] = pshr(
sub16(add16(quantized_vector[i], quantized_vector[i - 1]), GAP2) as Word32,
1,
) as Word16;
quantized_vector[i] = pshr(
add16(add16(quantized_vector[i], quantized_vector[i - 1]), GAP2) as Word32,
1,
) as Word16;
}
}
/* compute the weighted mean square distance using the final quantized vector according to eq21 */
weighted_mean_square_error[l0] = 0;
for i in 0..NB_LSP_COEFF {
let diff_target_vector_quantized_vector = usaturate(
abs(mult16_32_q15(
MA_PREDICTOR_SUM[l0][i],
sub32(target_vector[i] as Word32, quantized_vector[i] as Word32),
)) as Word32,
MAX_U16 as Word32,
) as UWord16; /* targetVector and quantizedVector in Q13 -> result in Q13 */
weighted_mean_square_error[l0] = umac16_16(
weighted_mean_square_error[l0],
diff_target_vector_quantized_vector,
mult16_16_q11(
diff_target_vector_quantized_vector as Word16,
weights[i] as Word16,
) as UWord16,
); /* weights in Q11, diff in Q13 */
}
}
/* now select L0 and copy the selected coefficients to the output buffer */
if weighted_mean_square_error[0] < weighted_mean_square_error[1] {
parameters[0] = 0;
parameters[1] = l1_index[0] as u16;
parameters[2] = l2_index[0] as u16;
parameters[3] = l3_index[0] as u16;
} else {
parameters[0] = 1;
parameters[1] = l1_index[1] as u16;
parameters[2] = l2_index[1] as u16;
parameters[3] = l3_index[1] as u16;
}
/*** Compute the quantized LSF from the L coefficients ***/
/* reconstruct vector from the codebooks using the selected parameters spec 3.2.4 eq19 */
for i in 0..NB_LSP_COEFF / 2 {
quantizer_output[i] = add16(
L1[parameters[1] as usize][i],
L2L3[parameters[2] as usize][i],
); /* codebooks are in Q2.13 for L1 and Q0.13 for L2L3, due to actual values stored in the codebooks, result in Q2.13 */
}
for i in NB_LSP_COEFF / 2..NB_LSP_COEFF {
quantizer_output[i] = add16(
L1[parameters[1] as usize][i],
L2L3[parameters[3] as usize][i],
); /* same as previous, output in Q2.13 */
}
/* rearrange in order to have a minimum distance between two consecutives coefficients spec 3.2.4 */
rearrange_coefficients(&mut quantizer_output, GAP1);
rearrange_coefficients(&mut quantizer_output, GAP2); /* currentqLSF still in Q2.13 */
/* compute qLSF spec 3.2.4 eq20 */
for i in 0..NB_LSP_COEFF {
let mut acc = mult16_16(
MA_PREDICTOR_SUM[parameters[0] as usize][i],
quantizer_output[i],
); /* (1 - ∑Pi,k)*lˆi(m) Q15 * Q13 -> Q28 */
for j in 0..MA_MAX_K {
acc = mac16_16(
acc,
MA_PREDICTOR[parameters[0] as usize][j][i],
previous_q_lsf[j][i],
);
}
/* acc in Q2.28, shift back the acc to a Q2.13 with rounding */
q_lsf[i] = pshr(acc, 15) as Word16; /* qLSF in Q2.13 */
}
/* update the previousqLSF buffer with current quantizer output */
for i in (1..MA_MAX_K).rev() {
previous_q_lsf[i] = previous_q_lsf[i - 1];
}
previous_q_lsf[0].copy_from_slice(&quantizer_output);
/*** qLSF stability check ***/
insertion_sort(&mut q_lsf, NB_LSP_COEFF);
/* check for low limit on qLSF[0] */
if q_lsf[1] < QLSF_MIN {
q_lsf[1] = QLSF_MIN;
}
/* check and rectify minimum distance between two consecutive qLSF */
for i in 0..NB_LSP_COEFF - 1 {
if sub16(q_lsf[i + 1], q_lsf[i]) < MIN_QLSF_DISTANCE {
q_lsf[i + 1] = q_lsf[i] + MIN_QLSF_DISTANCE;
}
}
/* check for upper limit on qLSF[NB_LSP_COEFF-1] */
if q_lsf[NB_LSP_COEFF - 1] > QLSF_MAX {
q_lsf[NB_LSP_COEFF - 1] = QLSF_MAX;
}
/* convert qLSF to qLSP: qLSP = cos(qLSF) */
for i in 0..NB_LSP_COEFF {
q_lsp_coefficients[i] = g729_cos_q13q15(q_lsf[i]); /* ouput in Q0.15 */
}
}