/
qnt12.c
1361 lines (1183 loc) · 46 KB
/
qnt12.c
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
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ================================================================== */
/* */
/* Microsoft Speech coder ANSI-C Source Code */
/* SC1200 1200 bps speech coder */
/* Fixed Point Implementation Version 7.0 */
/* Copyright (C) 2000, Microsoft Corp. */
/* All rights reserved. */
/* */
/* ================================================================== */
/*------------------------------------------------------------------*/
/* */
/* File: qnt12.c */
/* */
/* Description: quantization for 1200bps */
/* */
/*------------------------------------------------------------------*/
#include "sc1200.h"
#include "lpc_lib.h"
#include "vq_lib.h"
#include "global.h"
#include "macro.h"
#include "qnt12_cb.h"
#include "mat_lib.h"
#include "math_lib.h"
#include "qnt12.h"
#include "constant.h"
#include "mathhalf.h"
#include "msvq_cb.h"
#include "fsvq_cb.h"
#include "dsp_sub.h"
#include "melp_sub.h"
#define LSP_INP_CAND 5
#define NF_X_NUM_GAINFR (NF * NUM_GAINFR)
#define X0333_Q15 10923 /* (1/3) * (1 << 15) */
#define X0667_Q15 21845 /* (2/3) * (1 << 15) */
/* ------ Local prototypes ------ */
static void wvq1(int16_t target[], int16_t weights[],
const int16_t codebook[], int16_t dim,
int16_t cbsize, int16_t index[], int32_t dist[],
int16_t cand);
static int16_t InsertCand(int16_t c1, int16_t s1, int16_t dMin[],
int16_t distortion, int16_t entry,
int16_t nextIndex[], int16_t index[]);
static int16_t wvq2(int16_t target[], int16_t weights[],
int16_t codebook[], int16_t dim,
int16_t index[], int32_t dist[], int16_t cand);
static int16_t WeightedMSE(int16_t n, int16_t weight[],
const int16_t x[], int16_t target[],
int16_t max_dMin);
static void lspVQ(int16_t target[], int16_t weight[], int16_t qout[],
const int16_t codebook[], int16_t tos,
const int16_t cb_size[], int16_t cb_index[],
int16_t dim, BOOLEAN flag);
/****************************************************************************
**
** Function: pitch_vq
**
** Description: Pitch values of three frames are vector quantized
**
** Arguments:
**
** melp_param *par ---- input/output melp parameters
**
** Return value: None
**
*****************************************************************************/
void pitch_vq(struct melp_param *par)
{
register int16_t i;
static BOOLEAN prev_uv_flag = TRUE;
static int16_t prev_pitch = LOG_UV_PITCH_Q12; /* Q12 */
static int16_t prev_qpitch = LOG_UV_PITCH_Q12; /* Q12 */
const int16_t *codebook;
int16_t cnt, size, pitch_index;
int16_t temp1, temp2;
int32_t L_temp;
int16_t dcb[PITCH_VQ_CAND * NF]; /* Q12 */
int16_t target[NF], deltp[NF]; /* Q12 */
int16_t deltw[NF]; /* Q0 */
int16_t weights[NF]; /* Q0 */
int16_t indexlist[PITCH_VQ_CAND];
int32_t distlist[PITCH_VQ_CAND]; /* Q25 */
/* ---- Compute pitch in log domain ---- */
for (i = 0; i < NF; i++)
target[i] = log10_fxp(par[i].pitch, 7); /* Q12 */
cnt = 0;
for (i = 0; i < NF; i++){
if (par[i].uv_flag)
weights[i] = 0; /* Q0 */
else {
weights[i] = 1;
cnt++;
}
}
/* ---- calculate delta ---- */
for (i = 0; i < NF; i++){
if (prev_uv_flag || par[i].uv_flag){
deltp[i] = 0;
deltw[i] = 0;
} else {
deltp[i] = sub(target[i], prev_pitch);
deltw[i] = DELTA_PITCH_WEIGHT_Q0;
}
prev_pitch = target[i];
prev_uv_flag = par[i].uv_flag;
}
if (cnt == 0){
for (i = 0; i < NF; i++)
par[i].pitch = UV_PITCH;
prev_qpitch = LOG_UV_PITCH_Q12;
} else if (cnt == 1){
for (i = 0; i < NF; i++){
if (!par[i].uv_flag){
quant_u(&target[i], &(quant_par.pitch_index), PIT_QLO_Q12,
PIT_QUP_Q12, PIT_QLEV_M1, PIT_QLEV_M1_Q8, TRUE, 7);
quant_u_dec(quant_par.pitch_index, &par[i].pitch,
PIT_QLO_Q12, PIT_QUP_Q12, PIT_QLEV_M1_Q8, 7);
} else
par[i].pitch = LOG_UV_PITCH_Q12;
}
/* At this point par[].pitch temporarily holds the pitches in the */
/* log domain with Q12. */
prev_qpitch = par[NF - 1].pitch; /* Q12 */
for (i = 0; i < NF; i++)
par[i].pitch = pow10_fxp(par[i].pitch, 7); /* Q7 */
} else if (cnt > 1){ /* cnt == 2, 3, ......, (NF - 1) */
/* ----- set pointer -----*/
if (cnt == NF){ /* All NF frames are voiced. */
codebook = pitch_vq_cb_vvv;
size = PITCH_VQ_LEVEL_VVV;
} else {
codebook = pitch_vq_cb_uvv;
size = PITCH_VQ_LEVEL_UVV;
} /* This part changed !!! (12/13/99) */
/* ---- select candidate using static pitch distortion ---- */
wvq1(target, weights, codebook, NF, size, indexlist, distlist,
PITCH_VQ_CAND);
/* -- select index using static and delta pitch distortion -- */
temp1 = 0;
for (i = 0; i < PITCH_VQ_CAND; i++){
L_temp = L_mult(indexlist[i], NF);
L_temp = L_shr(L_temp, 1);
temp2 = extract_l(L_temp);
/* Now temp1 is (i*NF) and temp2 is (indexlist[i]*NF). */
dcb[temp1] = sub(codebook[temp2], prev_qpitch); /* Q12 */
v_equ(&dcb[temp1 + 1], &codebook[temp2 + 1], NF - 1);
v_sub(&dcb[temp1 + 1], &codebook[temp2], NF - 1);
temp1 = add(temp1, NF);
}
pitch_index = wvq2(deltp, deltw, dcb, NF, indexlist, distlist,
PITCH_VQ_CAND);
if (par[NF - 1].uv_flag)
prev_qpitch = LOG_UV_PITCH_Q12;
else
prev_qpitch = codebook[pitch_index*NF + NF - 1]; /* Q12 */
for (i = 0; i < NF; i++){
if (par[i].uv_flag)
par[i].pitch = UV_PITCH_Q7;
else
par[i].pitch = pow10_fxp(codebook[pitch_index*NF + i], 7);
}
quant_par.pitch_index = pitch_index;
}
}
/****************************************************************************
**
** Function: wvq1
**
** Description: Pitch vq the first stage
**
** The purpose of wvq1() is to loop through all the "cbsize" entries of
** codebook[] (PITCH_VQ_LEVEL_VVV or PITCH_VQ_LEVEL_UVV, 512 or 2048) and
** record the "cand" entries (PITCH_VQ_CAND, 16) which yields the minimum
** errors.
**
** Arguments:
** int16_t target[] : target vector (Q12)
** int16_t weights[] : weighting vector (Q0)
** int16_t codebook[]: codebook (Q12)
** int16_t dim : vector dimension
** int16_t cbsize : codebook size
** int16_t index[] : output candidate index list
** int32_t dist[] : output candidate distortion list (Q25)
** int16_t cand : number of output candidates
**
** Return value: None
**
*****************************************************************************/
static void wvq1(int16_t target[], int16_t weights[],
const int16_t codebook[], int16_t dim,
int16_t cbsize, int16_t index[], int32_t dist[],
int16_t cand)
{
register int16_t i, j;
int16_t maxindex;
int32_t err, maxdist; /* Q25 */
int32_t L_temp;
int16_t temp; /* Q12 */
/* ------ Initialize the distortion ------ */
L_fill(dist, LW_MAX, cand);
maxdist = LW_MAX;
maxindex = 0;
/* ------ Search the codebook ------ */
for (i = 0; i < cbsize; i++){
err = 0;
/* Here the for loop computes the distortion between target[] and */
/* codebook[] and stores the result in int32_t err. If */
/* (err < maxdist) then we execute some actions. If err is already */
/* larger than or equal to maxdist, there is no need to keep */
/* computing the distortion. This improvement is only marginal */
/* because "dim" is small (NF == 3). */
for (j = 0; j < dim; j++){
if (weights[j] > 0){ /* weights[] is either 1 or 0. */
/* err += SQR(target[j] - codebook[j]); */
temp = sub(target[j], codebook[j]); /* Q12 */
L_temp = L_mult(temp, temp); /* Q25 */
L_temp = L_shr(L_temp, 2); /* Q23 */
err = L_add(err, L_temp);
if (err >= maxdist)
break;
}
}
if (err < maxdist){
index[maxindex] = i;
dist[maxindex] = err;
/* The following loop forgets maxindex and maxdist and finds them */
/* from scratch. This is very inefficient because "cand" is */
/* PITCH_VQ_CAND (== 16) and we know the maximum is always */
/* replaced just now by the new "err". However, an attempt of */
/* keeping dist[] sorted (so the following loop is not needed */
/* every time we update maxdist) only shows minimal improvement. */
maxdist = 0;
for (j = 0; j < cand; j++){
if (dist[j] > maxdist){
maxdist = dist[j];
maxindex = j;
}
}
}
codebook += dim; /* Pointer arithmetics. */
}
}
/****************************************************************************
**
** Function: wvq2
**
** Description: Pitch vq the second stage
**
** Arguments:
** int16_t target[] : target vector (Q12)
** int16_t weights[] : weighting vector (Q0)
** int16_t codebook[]: codebook (Q12)
** int16_t dim : vector dimension
** int16_t index[] : codebook index
** int32_t dist[] : distortion (Q25)
** int16_t cand : number of input candidates
**
** Return value: int16_t ---- the final codebook index
**
*****************************************************************************/
static int16_t wvq2(int16_t target[], int16_t weights[],
int16_t codebook[], int16_t dim,
int16_t index[], int32_t dist[], int16_t cand)
{
register int16_t i, j;
int16_t ind;
int32_t err, min;
int32_t L_temp;
int16_t temp;
/* To reduce the complexity, we should try to increase the opportunity of */
/* making (err >= min). In other words, set "min" as small as possible */
/* before the loop begins. One idea is to find the i which minimizes */
/* dist[] and use it to compute "min" and "ind" first, then loop through */
/* all candidates except this i. This scheme only reduces about 1/100 of */
/* the execution time, so it is not implemented here. */
min = LW_MAX;
ind = 0;
for (i = 0; i < cand; i++){
err = dist[i];
for (j = 0; j < dim; j++){
if (weights[j] > 0){
/* weights[] are either 0 or a positive constant */
/* DELTA_PITCH_WEIGHT_Q0 == 1. Note that the following code */
/* segment no longer works correctly if DELTA_PITCH_WEIGHT_Q0 */
/* is changed to other value. */
/* err += weights[j] * SQR(target[j] - codebook[j]); */
temp = sub(target[j], codebook[j]); /* Q12 */
L_temp = L_mult(temp, temp); /* Q25 */
L_temp = L_shr(L_temp, 2); /* Q23 */
err = L_add(err, L_temp); /* Q23 */
/* Exit the loop if (err >= min). */
if (err >= min)
break;
}
}
if (err < min){
min = err;
ind = index[i];
}
codebook += dim; /* Pointer arithmetics. */
}
return(ind);
}
/****************************************************************************
**
** Function: gain_vq
**
** Description: Gain quantization for 1200bps
**
** Arguments:
**
** melp_param *par ---- input/output melp parameters
**
** Return value: None
**
*****************************************************************************/
void gain_vq(struct melp_param *par)
{
register int16_t i, j;
int16_t index;
int16_t temp, temp2;
int16_t gain_target[NF_X_NUM_GAINFR];
int32_t err, minErr; /* Q17 */
int32_t L_temp;
/* Reshape par[i].gain[j] into a one-dimensional vector gain_target[]. */
temp = 0;
for (i = 0; i < NF; i++){
v_equ(&(gain_target[temp]), par[i].gain, NUM_GAINFR);
temp = add(temp, NUM_GAINFR);
}
minErr = LW_MAX;
index = 0;
temp2 = 0;
for (i = 0; i < GAIN_VQ_SIZE; i++){
/* temp2 = i * NF * NUM_GAINFR; */
err = 0;
/* j = 0 for the following for loop. */
temp = sub(gain_target[0], gain_vq_cb[temp2]); /* Q8 */
L_temp = L_mult(temp, temp); /* Q17 */
L_temp = L_shr(L_temp, 3); /* Q14 */
err = L_add(err, L_temp); /* Q14 */
/* For the sum of 6 terms, if the first term already exceeds minErr, */
/* there is no need to keep computing. */
if (err < minErr){
for (j = 1; j < NF_X_NUM_GAINFR; j++){
/* err += SQR(par[j].gain[k] -
gain_vq_cb[i*NUM_GAINFR*NF + j*NUM_GAINFR + k]); */
temp = sub(gain_target[j], gain_vq_cb[temp2 + j]); /* Q8 */
L_temp = L_mult(temp, temp); /* Q17 */
L_temp = L_shr(L_temp, 3); /* Q14 */
err = L_add(err, L_temp); /* Q14 */
}
if (err < minErr){
minErr = err;
index = i;
}
}
temp2 = add(temp2, NF_X_NUM_GAINFR);
}
/* temp2 = index * NF * NUM_GAINFR; */
L_temp = L_mult(index, NF_X_NUM_GAINFR);
L_temp = L_shr(L_temp, 1);
temp2 = extract_l(L_temp);
for (i = 0; i < NF; i++){
/* v_equ(par[i].gain, &(gain_vq_cb[index*NUM_GAINFR*NF +
i*NUM_GAINFR]), NUM_GAINFR); */
v_equ(par[i].gain, &(gain_vq_cb[temp2]), NUM_GAINFR);
temp2 = add(temp2, NUM_GAINFR);
}
quant_par.gain_index[0] = index;
}
/****************************************************************************
**
** Function: quant_bp
**
** Description: Quantization the band-pass voicing for 1200bps
**
** Arguments:
**
** melp_param *par ---- input/output melp parameters
**
** Return value: None
**
*****************************************************************************/
void quant_bp(struct melp_param *par, int16_t num_frames)
{
register int16_t i;
for (i = 0; i < num_frames; i++){
par[i].uv_flag = q_bpvc(par[i].bpvc, &(quant_par.bpvc_index[i]),
NUM_BANDS);
quant_par.bpvc_index[i] = bp_index_map[quant_par.bpvc_index[i]];
}
}
/********************************************************************
**
** Function: lspVQ ()
**
** Description:
** Vector quantizes a set of int term filter coefficients
** using a multi-stage M-L tree search algorithm.
**
** Arguments:
**
** int16_t target[] : the target coefficients to be quantized (Q15/Q17)
** int16_t weight[] : weights for mse calculation (Q11)
** int16_t qout[] : the output array (Q15/Q17)
** int16_t codebook[] : codebooks, cb[0..numStages-1] (Q15/Q17)
** int16_t tos : the number of stages
** int16_t cb_size[] : codebook size (multistages)
** int16_t cb_index[] : codebook indeces; cb_index[0..numStages-1]
** (output)
** int16_t dim
** BOOLEAN flag
**
** Return value: None
**
***********************************************************************/
static void lspVQ(int16_t target[], int16_t weight[], int16_t qout[],
const int16_t codebook[], int16_t tos,
const int16_t cb_size[], int16_t cb_index[],
int16_t dim, BOOLEAN flag)
{
register int16_t i, entry;
register int16_t c1, s1;
const int16_t *cdbk_ptr, *cdbk_ptr2, *ptr1;
int16_t index[LSP_VQ_CAND][LSP_VQ_STAGES];
int16_t nextIndex[LSP_VQ_CAND][LSP_VQ_STAGES];
int16_t ncPrev;
int16_t cand[LSP_VQ_CAND][2*LPC_ORD];
int16_t max_dMin, dMin[LSP_VQ_CAND], distortion;
int16_t *cand_target;
int32_t L_temp;
int16_t ptr_offset = 0;
int16_t temp1, temp2;
/*==================================================================*
* Initialize the data before starting the tree search. *
* - the number of candidates from the "previous" stage is set *
* to 1 since there is no previous stage! *
* - the candidate vector from the previous stage is set to zero *
* - the list of indeces for each candidate is set to 1 *
*==================================================================*/
for (i = 0; i < LSP_VQ_CAND; i++){
v_zap(cand[i], dim);
v_zap(index[i], LSP_VQ_STAGES);
v_zap(nextIndex[i], LSP_VQ_STAGES);
}
cand_target = v_get(dim);
ncPrev = 1;
/*==================================================================*
* Now we start the search: *
* For each stage *
* For each candidate from the previous stage *
* For each entry in the current stage codebook *
* * add codebook vector to current candidate *
* * compute the distortion with the target *
* * retain candidate if one of the best so far *
*==================================================================*/
cdbk_ptr = codebook;
/* An observation for lspVQ() shows that if "flag" is FALSE, then we only */
/* need to keep track of the best one (instead of the best LSP_VQ_CAND, */
/* 8) cand[][] and index[][]. This has significant influence on */
/* execution speed. */
for (s1 = 0; s1 < tos; s1++){
/* set the distortions to huge values */
fill(dMin, SW_MAX, LSP_VQ_CAND);
max_dMin = SW_MAX;
/* Loop for each previous candidate selected, and try each entry */
for (c1 = 0; c1 < ncPrev; c1++){
ptr_offset = 0;
/* cand_target[] is the target vector with cand[c1] removed. */
/* This moves some operations from the for-entry loop here. */
/* save_saturation(); */
v_equ(cand_target, target, dim);
v_sub(cand_target, cand[c1], dim);
/* restore_saturation(); */
for (entry = 0; entry < cb_size[s1]; entry++){
ptr1 = cdbk_ptr + ptr_offset; /* Pointer arithmetics. */
/* compute the distortion */
distortion = WeightedMSE(dim, weight, ptr1, cand_target,
max_dMin);
/*======================================================*
* If the error for this entry is less than the worst *
* retained candidate so far, keep it. Note that the *
* error list is maintained in order of best to worst. *
*=======================================================*/
if (distortion < max_dMin){
max_dMin = InsertCand(c1, s1, dMin, distortion, entry,
nextIndex[0], index[0]);
}
ptr_offset = add(ptr_offset, dim);
}
}
/* At this point ptr_offset is (cb_size[s1]*dim). */
/*==================================================================*
* Compute the number of candidate vectors which we kept for the *
* next stage. Note that if the size of the stages is less than *
* the number of candidates, we build them up using all entries *
* until we have kept numCand candidates. On the other hand, if *
* flag is FALSE and (s1 == tos - 1), then we only need to use *
* ncPrev = 1 because we only copy the best candidate before *
* exiting lspVQ(). *
*==================================================================*/
if (!flag && s1 == tos - 1)
ncPrev = 1;
else {
/* ncPrev = Min(ncPrev*cb_size[s1], LSP_VQ_CAND) for regular */
/* loops, and ncPrev = Min(ncPrev*cb_size[s1], LSP_INP_CAND) for */
/* the last lap. Explanations are available near the end of this */ /* function. */
L_temp = L_mult(ncPrev, cb_size[s1]);
L_temp = L_shr(L_temp, 1);
temp1 = extract_l(L_temp); /* temp1 = ncPrev * cb_size[s1] */
if (s1 == tos - 1)
temp2 = LSP_INP_CAND;
else
temp2 = LSP_VQ_CAND;
if (temp1 < temp2)
ncPrev = temp1;
else
ncPrev = temp2;
}
/*==================================================================*
* We now have the best indices for the stage just completed, so *
* compute the new candidate vectors for the next stage... *
*==================================================================*/
for (c1 = 0; c1 < ncPrev; c1++){
v_zap(cand[c1], dim);
cdbk_ptr2 = codebook;
temp1 = add(s1, 1);
v_equ(index[c1], nextIndex[c1], temp1);
for (i = 0; i < temp1; i++){
/* v_add(cand[c1], cdbk_ptr2 + index[c1][i]*dim, dim); */
L_temp = L_mult(index[c1][i], dim);
L_temp = L_shr(L_temp, 1);
temp2 = extract_l(L_temp);
ptr1 = cdbk_ptr2 + temp2;
v_add(cand[c1], ptr1, dim);
/* cdbk_ptr2 += cb_size[i]*dim; */
L_temp = L_mult(cb_size[i], dim);
L_temp = L_shr(L_temp, 1);
temp2 = extract_l(L_temp);
cdbk_ptr2 += temp2;
}
}
/* cdbk_ptr += cb_size[s1] * dim; */
cdbk_ptr += ptr_offset;
}
/* Copy best candidate and indices into output. Here we use temp1 and */
/* temp2 to compute (c1*tos) and (c1*dim). */
/* Originally this function copies LSP_VQ_CAND (== 8) vectors before */
/* exiting if flag is TRUE. However, in the calling environment of */
/* lspVQ() when flag is passed in as TRUE, we only used LSP_INP_CAND */
/* (== 5). */
temp1 = 0;
temp2 = 0;
for (i = 0; i < ncPrev; i++){
v_equ(&(cb_index[temp1]), index[i], tos);
v_equ(&qout[temp2], cand[i], dim);
temp1 = add(temp1, tos);
temp2 = add(temp2, dim);
}
v_free(cand_target);
}
/********************************************************************
**
** Function: WeightedMSE
**
** Description:
** Given a weighting function, computes the weighted mean squared
** error between two vectors.
**
** Arguments:
** int16_t n : number of coefficients in the two vectors
** int16_t weight[] : weighting function; weight[1..n] (Q11)
** int16_t x[] : first vector (Q15/Q17)
** int16_t target[] : second vector (Q15/Q17)
**
** Return value:
**
** int16_t WeightedMSE : distortion returned as function value (Q15/Q17)
**
***********************************************************************/
static int16_t WeightedMSE(int16_t n, int16_t weight[],
const int16_t x[], int16_t target[], int16_t max_dMin)
{
register int16_t i;
int32_t distortion;
int16_t temp, half_n;
/* x[] and target[] are either Q15 or Q17. Since the only issue */
/* mattering is the relative magnitude of WeightedMSE() among different */
/* x[]'s, it will be okay to simply treat both x[] and target[] as Q15. */
/* This will make the returned value incorrect in scaling, but it will */
/* not affect the relative magnitudes. Returning a int16_t in Q11 */
/* seems to be fine according to some rough statistics collected. */
distortion = 0;
half_n = shr(n, 1);
for (i = 0; i < half_n; i++){
save_saturation();
temp = sub(x[i], target[i]); /* Q15 */
temp = mult(temp, temp); /* Q15 */
restore_saturation();
distortion = L_mac(distortion, weight[i], temp); /* Q27 */
}
if (round(distortion) >= max_dMin) /* if this situation takes place, */
return(SW_MAX); /* distortion will exceed max_dMin */
/* and we can leave. */
for (i = half_n; i < n; i++){
save_saturation();
temp = sub(x[i], target[i]); /* Q15 */
temp = mult(temp, temp); /* Q15 */
restore_saturation();
distortion = L_mac(distortion, weight[i], temp); /* Q27 */
}
temp = round(distortion);
return(temp);
}
/********************************************************************
**
** Function: InsertCand ()
**
** Description:
**
** Inserts the indeces corresponding to a candidate into the
** candidate index list, which is sorted in order of increasing
** distortion.
**
** Arguments:
**
** int16_t c1 : index of candidate to insert into list
** int16_t s1 : index of current stage we are searching
** int16_t dMin[] : list of distortions of best nc candidates (Q11)
** int16_t distortion[] : distortion of candidate c when used with
** "entry" from current stage (Q11)
** int16_t entry : current stage entry which results in lower
** distortion
** int16_t **index : list of past indices for each candidate
** int16_t **nextIndex : indices for next stage (output)
**
** Return value: int16_t
**
***********************************************************************/
static int16_t InsertCand(int16_t c1, int16_t s1, int16_t dMin[],
int16_t distortion, int16_t entry,
int16_t nextIndex[], int16_t index[])
{
register int16_t i, j;
int16_t ptr_offset;
int16_t temp1, temp2;
int16_t *ptr1, *ptr2;
int32_t L_temp;
/*==================================================================*
* First find the index into the distortion array where this *
* candidate fits. Note that we assume it has been previously *
* verified that this error falls in the range of the candidate *
* errors. *
*==================================================================*/
for (i = 0; (i < LSP_VQ_CAND) && (distortion > dMin[i]); i++);
/* shift the distortions and indices down to make room for the new one */
/* ptr_offset = (LSP_VQ_CAND - 1) * vq_stages; */
L_temp = L_mult((LSP_VQ_CAND - 1) , LSP_VQ_STAGES);
L_temp = L_shr(L_temp, 1);
ptr_offset = extract_l(L_temp);
temp2 = add(s1, 1);
for (j = (LSP_VQ_CAND - 1); j > i; j--){
dMin[j] = dMin[j - 1];
temp1 = sub(ptr_offset, LSP_VQ_STAGES);
ptr1 = nextIndex + ptr_offset; /* Pointer arithmetics. */
ptr2 = nextIndex + temp1;
/* v_equ(nextIndex + j * vq_stages, nextIndex + (j - 1)*vq_stages,
s1 + 1); */
v_equ(ptr1, ptr2, temp2);
ptr_offset = temp1;
}
/* insert the index and distortion into the ith candidate */
dMin[i] = distortion;
/* v_equ(nextIndex + i * vq_stages, index + c1 * vq_stages, s1); */
L_temp = L_mult(i, LSP_VQ_STAGES); /* temp1 = i * vq_stages; */
L_temp = L_shr(L_temp, 1);
temp1 = extract_l(L_temp);
L_temp = L_mult(c1, LSP_VQ_STAGES);
L_temp = L_shr(L_temp, 1);
temp2 = extract_l(L_temp);
ptr1 = nextIndex + temp1; /* Pointer arithmetics. */
ptr2 = index + temp2;
v_equ(ptr1, ptr2, s1);
/* *(nextIndex + i*vq_stages + s1) = entry; */
ptr1 += s1; /* Pointer arithmetics. */
*ptr1 = entry;
return (dMin[LSP_VQ_CAND - 1]);
}
/*********************************************************************
** NAME: lspStable
**
** DESCRIPTION:
** This routines checks the stability of a set of LSP parameters
** by ensuring that all parameters are in the correct order. For
** LSPs, the LSP frequencies must be monotonically increasing.
**
** INPUTS:
** int16_t lsp[] : the LSP coefficients lsp[0..order - 1] (Q15)
** int16_t order : order of the LSP coeffs
**
** OUTPUTS: BOOLEAN : TRUE == stable; FALSE == unstable
**
**********************************************************************/
BOOLEAN lspStable(int16_t lsp[], int16_t order)
{
register int16_t i;
BOOLEAN stable;
int16_t temp;
/* The following loop attempts to ensure lsp[0] is at least 6.37, */
/* lsp[order - 1] is at most 3992.0, and each consecutive pair of lsp[] */
/* is separated by at least 25.0. The sampling frequency is assumed to */
/* be 8000.0. */
if (lsp[0] < 52) /* 52 == (6.37/4000.0 * (1 << 15)) */
lsp[0] = (int16_t) 52;
for (i = 0; i < order - 1; i++){
temp = add(lsp[i], 205);
if (lsp[i + 1] < temp)
lsp[i + 1] = temp;
}
/* 205 == (25.0/4000.0 * (1 << 15)) */
if (lsp[order - 1] > 32702)
lsp[order - 1] = (int16_t) 32702;
/* 32702 == (3992.0/4000.0 * (1 << 15)) */
/* Previously here we use a loop checking whether (lsp[i] < lsp[i - 1]) */
/* for any of the pairs from i = 1 to i < order. It is not needed. The */
/* for loop above essentially guarantees the monotonic ascending of */
/* lsp[]'s (with a guaranteed gap of 25.0 (Hz)). The only possible */
/* violation is between lsp[order - 2] and lsp[order - 1] because of the */
/* modification of lsp[order - 1] after the loop. So now we only check */
/* this pair. */
if (lsp[order - 1] < lsp[order - 2])
stable = FALSE;
else
stable = TRUE;
if (!stable) /* Warning message moved from lspSort() to lspStable(). */
fprintf(stderr, "Unstable filter found in lspStable()...\n");
return(stable);
}
/*********************************************************************
**
** Name: lspSort()
**
** Description:
**
** Uses the very slow Straight Insertion technique...so only
** use for, say, n < 50. This routine is taken from the
** Numerical Recipes in C book.
**
** Arguments:
**
** int16_t lsp[] : array to be sorted arr[1..n] (input/output) (Q15)
** int16_t n : number of samples to sort
**
** Return value: None
**
***********************************************************************/
void lspSort(int16_t lsp[], int16_t order)
{
register int16_t i, j;
int16_t temp; /* Q15 */
for (j = 1; j < order; j++){
temp = lsp[j];
i = (int16_t) (j - 1);
while (i >= 0 && lsp[i] > temp){
lsp[i + 1] = lsp[i];
i--;
}
lsp[i + 1] = temp;
}
}
/****************************************************************************
**
** Function: lsf_vq
**
** Description: lsfs of three frames are vector quantized
**
** Arguments:
**
** melp_param *par ---- input/output melp parameters
**
** Return value: None
**
*****************************************************************************/
void lsf_vq(struct melp_param *par)
{
register int16_t i, j, k;
static BOOLEAN firstTime = TRUE;
static int16_t qplsp[LPC_ORD]; /* Q15 */
const int16_t melp_cb_size[4] = {256, 64, 32, 32}; /* !!! (12/15/99) */
const int16_t res_cb_size[4] = {256, 64, 64, 64};
const int16_t melp_uv_cb_size[1] = {512};
int16_t uv_config; /* Bits of uv_config replace uv1, uv2 and cuv. */
int16_t *lsp[NF];
int32_t err, minErr, acc, bcc; /* !!! (12/15/99), Q11 */
int16_t temp1, temp2;
int16_t lpc[LPC_ORD]; /* Q12 */
int16_t wgt[NF][LPC_ORD]; /* Q11 */
int16_t mwgt[2*LPC_ORD]; /* Q11 */
int16_t bestlsp0[LPC_ORD], bestlsp1[LPC_ORD]; /* Q15 */
int16_t res[2*LPC_ORD]; /* Q17 */
/* The original program declares lsp_cand[LSP_VQ_CAND][] and */
/* lsp_index_cand[LSP_VQ_CAND*LSP_VQ_STAGES] with LSP_VQ_CAND == 8. The */
/* program only uses up to LSP_INP_CAND == 5 and the declaration is */
/* modified. */
int16_t lsp_cand[LSP_INP_CAND][LPC_ORD]; /* Q15 */
int16_t lsp_index_cand[LSP_INP_CAND*LSP_VQ_STAGES];
int16_t ilsp0[LPC_ORD], ilsp1[LPC_ORD]; /* Q15 */
int16_t cand, inp_index_cand, tos, intfact;
if (firstTime){
temp2 = shl(LPC_ORD, 10); /* Q10 */
temp1 = X08_Q10; /* Q10 */
for (i = 0; i < LPC_ORD; i++){
/* qplsp[i] = (i+1)*0.8/LPC_ORD; */
qplsp[i] = divide_s(temp1, temp2);
temp1 = add(temp1, X08_Q10);
}
firstTime = FALSE;
}
/* ==== Compute weights ==== */
for (i = 0; i < NF; i++){
lsp[i] = par[i].lsf;
lpc_lsp2pred(lsp[i], lpc, LPC_ORD);
vq_lspw(wgt[i], lsp[i], lpc, LPC_ORD);
}
uv_config = 0;
for (i = 0; i < NF; i++){
uv_config = shl(uv_config, 1);
if (par[i].uv_flag){
uv_config |= 0x0001;
/* ==== Adjust weights ==== */
if (i == 0) /* Testing for par[0].uv_flag == 1 */
v_scale(wgt[0], X02_Q15, LPC_ORD);
else if (i == 1)
v_scale(wgt[1], X02_Q15, LPC_ORD);
}
}
/* ==== Quantize the lsp according to the UV decisions ==== */
switch (uv_config){
case 7: /* 111, all frames are NOT voiced ---- */
lspVQ(lsp[0], wgt[0], lsp[0], lsp_uv_9, 1, melp_uv_cb_size,
quant_par.lsf_index[0], LPC_ORD, FALSE);
lspVQ(lsp[1], wgt[1], lsp[1], lsp_uv_9, 1, melp_uv_cb_size,
quant_par.lsf_index[1], LPC_ORD, FALSE);
lspVQ(lsp[2], wgt[2], lsp[2], lsp_uv_9, 1, melp_uv_cb_size,
quant_par.lsf_index[2], LPC_ORD, FALSE);
break;
case 6: /* 110 */
lspVQ(lsp[0], wgt[0], lsp[0], lsp_uv_9, 1, melp_uv_cb_size,
quant_par.lsf_index[0], LPC_ORD, FALSE);
lspVQ(lsp[1], wgt[1], lsp[1], lsp_uv_9, 1, melp_uv_cb_size,
quant_par.lsf_index[1], LPC_ORD, FALSE);
lspVQ(lsp[2], wgt[2], lsp[2], lsp_v_256x64x32x32, 4, melp_cb_size, /* !!! (12/15/99) */
quant_par.lsf_index[2], LPC_ORD, FALSE);
break;
case 5: /* 101 */
lspVQ(lsp[0], wgt[0], lsp[0], lsp_uv_9, 1, melp_uv_cb_size,
quant_par.lsf_index[0], LPC_ORD, FALSE);
lspVQ(lsp[1], wgt[1], lsp[1], lsp_v_256x64x32x32, 4, melp_cb_size, /* !!! (12/15/99) */
quant_par.lsf_index[1], LPC_ORD, FALSE);
lspVQ(lsp[2], wgt[2], lsp[2], lsp_uv_9, 1, melp_uv_cb_size,
quant_par.lsf_index[2], LPC_ORD, FALSE);
break;
case 3: /* 011 */
lspVQ(lsp[0], wgt[0], lsp[0], lsp_v_256x64x32x32, 4, melp_cb_size, /* !!! (12/15/99) */
quant_par.lsf_index[0], LPC_ORD, FALSE);
lspVQ(lsp[1], wgt[1], lsp[1], lsp_uv_9, 1, melp_uv_cb_size,
quant_par.lsf_index[1], LPC_ORD, FALSE);
lspVQ(lsp[2], wgt[2], lsp[2], lsp_uv_9, 1, melp_uv_cb_size,
quant_par.lsf_index[2], LPC_ORD, FALSE);