-
Notifications
You must be signed in to change notification settings - Fork 1
/
sp_enc.c
11607 lines (10510 loc) · 305 KB
/
sp_enc.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
/*
* ===================================================================
* TS 26.104
* REL-5 V5.4.0 2004-03
* REL-6 V6.1.0 2004-03
* REL-15 V15.1.0 2018-07
* 3GPP AMR Floating-point Speech Codec
* ===================================================================
*
*/
/*
* sp_enc.c
*
*
* Project:
* AMR Floating-Point Codec
*
* Contains:
* This module contains all the functions needed encoding 160
* 16-bit speech samples to AMR encoder parameters.
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <float.h>
#include "sp_enc.h"
#include "rom_enc.h"
/*
* Definition of structures used in encoding process
*/
typedef struct
{
Float32 y2;
Float32 y1;
Float32 x0;
Float32 x1;
} Pre_ProcessState;
#ifdef VAD2
/* Defines for VAD2 */
#define FRM_LEN1 80
#define DELAY0 24
#define FFT_LEN1 128
#define UPDATE_CNT_THLD1 50
#define INIT_FRAMES 4
#define CNE_SM_FAC1 0.1
#define CEE_SM_FAC1 0.55
#define HYSTER_CNT_THLD1 6 /* forced update constants... */
#define HIGH_ALPHA1 0.9
#define LOW_ALPHA1 0.7
#define ALPHA_RANGE1 (HIGH_ALPHA1 - LOW_ALPHA1)
#define NORM_ENRG (4.0) /* account for div by 2 by the HPF */
#define MIN_CHAN_ENRG (0.0625 / NORM_ENRG)
#define INE (16.0 / NORM_ENRG)
#define NOISE_FLOOR (1.0 / NORM_ENRG)
#define PRE_EMP_FAC1 (-0.8)
#define NUM_CHAN 16
#define LO_CHAN 0
#define HI_CHAN 15
#define UPDATE_THLD 35
#define SINE_START_CHAN 2
#define P2A_THRESH 10.0
#define DEV_THLD1 28.0
/* Defines for the FFT function */
#define SIZE 128
#define SIZE_BY_TWO 64
#define NUM_STAGE 6
#define PI 3.141592653589793
#define TRUE 1
#define FALSE 0
/* Macros */
#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b))
#define square(a) ((a) * (a))
/* structures */
typedef struct
{
Float32 pre_emp_mem;
Word16 update_cnt;
Word16 hyster_cnt;
Word16 last_update_cnt;
Float32 ch_enrg_long_db[NUM_CHAN];
Word32 Lframe_cnt;
Float32 ch_enrg[NUM_CHAN];
Float32 ch_noise[NUM_CHAN];
Float32 tsnr;
Word16 hangover;
Word16 burstcount;
Word16 fupdate_flag;
Float32 negSNRvar;
Float32 negSNRbias;
Float32 R0;
Float32 Rmax;
Word16 LTP_flag;
} vadState;
#else
typedef struct
{
Float32 bckr_est[COMPLEN]; /* background noise estimate */
Float32 ave_level[COMPLEN];
/* averaged input components for stationary estimation */
Float32 old_level[COMPLEN]; /* input levels of the previous frame */
Float32 sub_level[COMPLEN];
/* input levels calculated at the end of a frame (lookahead) */
Float32 a_data5[3][2]; /* memory for the filter bank */
Float32 a_data3[5]; /* memory for the filter bank */
Float32 best_corr_hp; /* FIP filtered value */
/* counts length of a speech burst incl HO addition */
Float32 corr_hp_fast; /* filtered value */
Word32 vadreg; /* flags for intermediate VAD decisions */
Word32 pitch; /* flags for pitch detection */
Word32 oldlag_count, oldlag; /* variables for pitch detection */
Word32 complex_high; /* flags for complex detection */
Word32 complex_low; /* flags for complex detection */
Word32 complex_warning; /* complex background warning */
Word32 tone; /* flags for tone detection */
Word16 burst_count; /* counts length of a speech burst */
Word16 hang_count; /* hangover counter */
Word16 stat_count; /* stationary counter */
Word16 complex_hang_count; /* complex hangover counter, used by VAD */
Word16 complex_hang_timer; /* hangover initiator, used by CAD */
Word16 speech_vad_decision; /* final decision */
Word16 sp_burst_count;
} vadState;
#endif
#define DTX_HIST_SIZE 8
#define DTX_ELAPSED_FRAMES_THRESH (24 + 7 - 1)
#define DTX_HANG_CONST 7 /* yields eight frames of SP HANGOVER */
typedef struct
{
Float32 lsp_hist[M * DTX_HIST_SIZE];
Float32 log_en_hist[DTX_HIST_SIZE];
Word32 init_lsf_vq_index;
Word16 hist_ptr;
Word16 log_en_index;
Word16 lsp_index[3];
/* DTX handler stuff */
Word16 dtxHangoverCount;
Word16 decAnaElapsedCount;
} dtx_encState;
typedef struct
{
/* gain history */
Float32 gp[N_FRAME];
/* counters */
Word16 count;
} tonStabState;
typedef struct
{
Word32 past_qua_en[4];
/* normal MA predictor memory, (contains 20*log10(qua_err)) */
} gc_predState;
typedef struct
{
Float32 prev_alpha; /* previous adaptor output, */
Float32 prev_gc; /* previous code gain, */
Float32 ltpg_mem[LTPG_MEM_SIZE]; /* LTP coding gain history, */
Word16 onset; /* onset state, */
/* (ltpg_mem[0] not used for history) */
} gain_adaptState;
typedef struct
{
Float32 sf0_target_en;
Float32 sf0_coeff[5];
Word32 sf0_gcode0_exp;
Word32 sf0_gcode0_fra;
Word16 *gain_idx_ptr;
gc_predState *gc_predSt;
gc_predState *gc_predUncSt;
gain_adaptState *adaptSt;
} gainQuantState;
typedef struct
{
Word32 T0_prev_subframe; /* integer pitch lag of previous sub-frame */
} Pitch_frState;
typedef struct
{
Pitch_frState *pitchSt;
} clLtpState;
typedef struct
{
Float32 ada_w;
Word32 old_T0_med;
Word16 wght_flg;
} pitchOLWghtState;
typedef struct
{
Float32 past_rq[M]; /* Past quantized prediction error */
} Q_plsfState;
typedef struct
{
/* Past LSPs */
Float32 lsp_old[M];
Float32 lsp_old_q[M];
/* Quantization state */
Q_plsfState *qSt;
} lspState;
typedef struct
{
Float32 old_A[M + 1]; /* Last A(z) for case of unstable filter */
} LevinsonState;
typedef struct
{
LevinsonState *LevinsonSt;
} lpcState;
typedef struct
{
/* Speech vector */
Float32 old_speech[L_TOTAL];
Float32 *speech, *p_window, *p_window_12k2;
Float32 *new_speech; /* Global variable */
/* Weight speech vector */
Float32 old_wsp[L_FRAME + PIT_MAX];
Float32 *wsp;
/* OL LTP states */
Word32 old_lags[5];
Float32 ol_gain_flg[2];
/* Excitation vector */
Float32 old_exc[L_FRAME + PIT_MAX + L_INTERPOL];
Float32 *exc;
/* Zero vector */
Float32 ai_zero[L_SUBFR + MP1];
Float32 *zero;
/* Impulse response vector */
Float32 *h1;
Float32 hvec[L_SUBFR * 2];
/* Substates */
lpcState *lpcSt;
lspState *lspSt;
clLtpState *clLtpSt;
gainQuantState *gainQuantSt;
pitchOLWghtState *pitchOLWghtSt;
tonStabState *tonStabSt;
vadState *vadSt;
Word32 dtx;
dtx_encState *dtxEncSt;
/* Filter's memory */
Float32 mem_syn[M], mem_w0[M], mem_w[M];
Float32 mem_err[M + L_SUBFR], *error;
Float32 sharp;
} cod_amrState;
typedef struct
{
cod_amrState *cod_amr_state;
Pre_ProcessState *pre_state;
Word32 dtx;
} Speech_Encode_FrameState;
/*
* Dotproduct40
*
*
* Parameters:
* x I: First input
* y I: Second input
* Function:
* Computes dot product size 40
*
* Returns:
* acc dot product
*/
static Float64 Dotproduct40(Float32 *x, Float32 *y)
{
Float64 acc;
acc = x[0] * y[0] + x[1] * y[1] + x[2] * y[2] + x[3] * y[3];
acc += x[4] * y[4] + x[5] * y[5] + x[6] * y[6] + x[7] * y[7];
acc += x[8] * y[8] + x[9] * y[9] + x[10] * y[10] + x[11] * y[11];
acc += x[12] * y[12] + x[13] * y[13] + x[14] * y[14] + x[15] * y[15];
acc += x[16] * y[16] + x[17] * y[17] + x[18] * y[18] + x[19] * y[19];
acc += x[20] * y[20] + x[21] * y[21] + x[22] * y[22] + x[23] * y[23];
acc += x[24] * y[24] + x[25] * y[25] + x[26] * y[26] + x[27] * y[27];
acc += x[28] * y[28] + x[29] * y[29] + x[30] * y[30] + x[31] * y[31];
acc += x[32] * y[32] + x[33] * y[33] + x[34] * y[34] + x[35] * y[35];
acc += x[36] * y[36] + x[37] * y[37] + x[38] * y[38] + x[39] * y[39];
return (acc);
}
/*
* Autocorr
*
*
* Parameters:
* x I: Input signal
* r O: Autocorrelations
* wind I: Window for LPC analysis
* Function:
* Calculate autocorrelation with window, LPC order = M
*
* Returns:
* void
*/
static void Autocorr(Float32 x[], Float32 r[], const Float32 wind[])
{
Word32 i, j; /* Counters */
Float32 y[L_WINDOW + M + 1]; /* Windowed signal */
Float64 sum; /* temp */
/*
* Windowing of signal
*/
for (i = 0; i < L_WINDOW; i++)
{
y[i] = x[i] * wind[i];
}
/*
* Zero remaining memory
*/
memset(&y[L_WINDOW], 0, sizeof(Float32) * (M + 1));
/*
* Autocorrelation
*/
for (i = 0; i <= M; i++)
{
sum = 0;
for (j = 0; j < L_WINDOW; j += 40)
{
sum += Dotproduct40(&y[j], &y[j + i]);
}
r[i] = (Float32)sum;
}
}
/*
* Levinson
*
*
* Parameters:
* old_A I: Vector of old LP coefficients [M+1]
* r I: Vector of autocorrelations [M+1]
* a O: LP coefficients [M+1]
* rc O: Reflection coefficients [4]
* Function:
* Levinson-Durbin algorithm
*
* Returns:
* void
*
*/
static void Levinson(Float32 *old_A, Float32 *r, Float32 *A, Float32 *rc)
{
Float32 sum, at, err;
Word32 l, j, i;
Float32 rct[M]; /* temporary reflection coefficients 0,...,m-1 */
rct[0] = (-r[1]) / r[0];
A[0] = 1.0F;
A[1] = rct[0];
err = r[0] + r[1] * rct[0];
if (err <= 0.0)
err = 0.01f;
for (i = 2; i <= M; i++)
{
sum = 0.0F;
for (j = 0; j < i; j++)
sum += r[i - j] * A[j];
rct[i - 1] = (-sum) / (err);
for (j = 1; j <= (i / 2); j++)
{
l = i - j;
at = A[j] + rct[i - 1] * A[l];
A[l] += rct[i - 1] * A[j];
A[j] = at;
}
A[i] = rct[i - 1];
err += rct[i - 1] * sum;
if (err <= 0.0)
err = 0.01F;
}
memcpy(rc, rct, 4 * sizeof(Float32));
memcpy(old_A, A, MP1 * sizeof(Float32));
}
/*
* lpc
*
*
* Parameters:
* old_A O: Vector of old LP coefficients [M+1]
* x I: Input signal
* x_12k2 I: Input signal 12.2k
* a O: predictor coefficients
* mode I: AMR mode
* Function:
* LP analysis
*
* In 12.2 kbit/s mode linear prediction (LP) analysis is performed
* twice per speech frame using the auto-correlation approach with
* 30 ms asymmetric windows. No lookahead is used in
* the auto-correlation computation.
*
* In other modes analysis is performed once per speech frame
* using the auto-correlation approach with 30 ms asymmetric windows.
* A lookahead of 40 samples (5 ms) is used in the auto-correlation computation.
*
* The auto-correlations of windowed speech are converted to the LP
* coefficients using the Levinson-Durbin algorithm.
* Then the LP coefficients are transformed to the Line Spectral Pair
* (LSP) domain for quantization and interpolation purposes.
* The interpolated quantified and unquantized filter coefficients
* are converted back to the LP filter coefficients
* (to construct the synthesis and weighting filters at each subframe).
*
* Returns:
* void
*
*/
static void lpc(Float32 *old_A, Float32 x[], Float32 x_12k2[], Float32 a[], enum Mode mode)
{
Word32 i;
Float32 r[MP1];
Float32 rc[4];
if (mode == MR122)
{
Autocorr(x_12k2, r, window_160_80);
/*
* Lag windowing
*/
for (i = 1; i <= M; i++)
{
r[i] = r[i] * lag_wind[i - 1];
}
r[0] *= 1.0001F;
if (r[0] < 1.0F)
r[0] = 1.0F;
/*
* Levinson Durbin
*/
Levinson(old_A, r, &a[MP1], rc);
/*
* Autocorrelations
*/
Autocorr(x_12k2, r, window_232_8);
/*
* Lag windowing
*/
for (i = 1; i <= M; i++)
{
r[i] = r[i] * lag_wind[i - 1];
}
r[0] *= 1.0001F;
if (r[0] < 1.0F)
r[0] = 1.0F;
/*
* Levinson Durbin
*/
Levinson(old_A, r, &a[MP1 * 3], rc);
}
else
{
/*
* Autocorrelations
*/
Autocorr(x, r, window_200_40);
/*
* a 60 Hz bandwidth expansion is used by lag windowing
* the auto-correlations. Further, auto-correlation[0] is
* multiplied by the white noise correction factor 1.0001
* which is equivalent to adding a noise floor at -40 dB.
*/
for (i = 1; i <= M; i++)
{
r[i] = r[i] * lag_wind[i - 1];
}
r[0] *= 1.0001F;
if (r[0] < 1.0F)
r[0] = 1.0F;
/*
* Levinson Durbin
*/
Levinson(old_A, r, &a[MP1 * 3], rc);
}
}
/*
* Chebps
*
*
* Parameters:
* x I: Cosine value for the freqency given
* f I: angular freqency
* Function:
* Evaluates the Chebyshev polynomial series
*
* Returns:
* result of polynomial evaluation
*/
static Float32 Chebps(Float32 x, Float32 f[])
{
Float32 b0, b1, b2, x2;
Word32 i;
x2 = 2.0F * x;
b2 = 1.0F;
b1 = x2 + f[1];
for (i = 2; i < 5; i++)
{
b0 = x2 * b1 - b2 + f[i];
b2 = b1;
b1 = b0;
}
return (x * b1 - b2 + f[i]);
}
/*
* Az_lsp
*
*
* Parameters:
* a I: Predictor coefficients [MP1]
* lsp O: Line spectral pairs [M]
* old_lsp I: Old lsp, in case not found 10 roots [M]
*
* Function:
* LP to LSP conversion
*
* The LP filter coefficients A[], are converted to the line spectral pair
* (LSP) representation for quantization and interpolation purposes.
*
* Returns:
* void
*/
static void Az_lsp(Float32 a[], Float32 lsp[], Float32 old_lsp[])
{
Word32 i, j, nf, ip;
Float32 xlow, ylow, xhigh, yhigh, xmid, ymid, xint;
Float32 y;
Float32 *coef;
Float32 f1[6], f2[6];
/*
* find the sum and diff. pol. F1(z) and F2(z)
*/
f1[0] = 1.0F;
f2[0] = 1.0F;
for (i = 0; i < (NC); i++)
{
f1[i + 1] = a[i + 1] + a[M - i] - f1[i];
f2[i + 1] = a[i + 1] - a[M - i] + f2[i];
}
f1[NC] *= 0.5F;
f2[NC] *= 0.5F;
/*
* find the LSPs using the Chebychev pol. evaluation
*/
nf = 0; /* number of found frequencies */
ip = 0; /* indicator for f1 or f2 */
coef = f1;
xlow = grid[0];
ylow = Chebps(xlow, coef);
j = 0;
while ((nf < M) && (j < 60))
{
j++;
xhigh = xlow;
yhigh = ylow;
xlow = grid[j];
ylow = Chebps(xlow, coef);
if (ylow * yhigh <= 0)
{
/* divide 4 times the interval */
for (i = 0; i < 4; i++)
{
xmid = (xlow + xhigh) * 0.5F;
ymid = Chebps(xmid, coef);
if (ylow * ymid <= 0.0F)
{
yhigh = ymid;
xhigh = xmid;
}
else
{
ylow = ymid;
xlow = xmid;
}
}
/*
* Linear interpolation
* xint = xlow - ylow*(xhigh-xlow)/(yhigh-ylow)
*/
y = yhigh - ylow;
if (y == 0)
{
xint = xlow;
}
else
{
y = (xhigh - xlow) / (yhigh - ylow);
xint = xlow - ylow * y;
}
lsp[nf] = xint;
xlow = xint;
nf++;
if (ip == 0)
{
ip = 1;
coef = f2;
}
else
{
ip = 0;
coef = f1;
}
ylow = Chebps(xlow, coef);
}
}
/* Check if M roots found */
if (nf < M)
{
memcpy(lsp, old_lsp, sizeof(Float32) * M);
}
return;
}
/*
* Get_lsp_pol
*
*
* Parameters:
* lsp I: line spectral frequencies
* f O: polynomial F1(z) or F2(z)
*
* Function:
* Find the polynomial F1(z) or F2(z) from the LSPs.
*
* F1(z) = product ( 1 - 2 lsp[i] z^-1 + z^-2 )
* i=0,2,4,6,8
* F2(z) = product ( 1 - 2 lsp[i] z^-1 + z^-2 )
* i=1,3,5,7,9
*
* where lsp[] is the LSP vector in the cosine domain.
*
* The expansion is performed using the following recursion:
*
* f[0] = 1
* b = -2.0 * lsp[0]
* f[1] = b
* for i=2 to 5 do
* b = -2.0 * lsp[2*i-2];
* f[i] = b*f[i-1] + 2.0*f[i-2];
* for j=i-1 down to 2 do
* f[j] = f[j] + b*f[j-1] + f[j-2];
* f[1] = f[1] + b;
*
* Returns:
* void
*/
static void Get_lsp_pol(Float32 *lsp, Float32 *f)
{
Word32 i, j;
Float32 T0;
f[0] = 1.0F;
f[1] = -2.0F * lsp[0];
for (i = 2; i <= 5; i++)
{
T0 = -2.0F * lsp[2 * i - 2];
f[i] = (Float32)(T0 * f[i - 1] + 2.0F * f[i - 2]);
for (j = i - 1; j >= 2; j--)
{
f[j] = f[j] + T0 * f[j - 1] + f[j - 2];
}
f[1] = f[1] + T0;
}
return;
}
/*
* Lsp_Az
*
*
* Parameters:
* lsp I: Line spectral frequencies
* a O: Predictor coefficients
*
* Function:
* Converts from the line spectral pairs (LSP) to LP coefficients,
* for a 10th order filter.
*
* Returns:
* void
*/
static void Lsp_Az(Float32 lsp[], Float32 a[])
{
Float32 f1[6], f2[6];
Word32 i, j;
Get_lsp_pol(&lsp[0], f1);
Get_lsp_pol(&lsp[1], f2);
for (i = 5; i > 0; i--)
{
f1[i] += f1[i - 1];
f2[i] -= f2[i - 1];
}
a[0] = 1;
for (i = 1, j = 10; i <= 5; i++, j--)
{
a[i] = (Float32)((f1[i] + f2[i]) * 0.5F);
a[j] = (Float32)((f1[i] - f2[i]) * 0.5F);
}
return;
}
/*
* Int_lpc_1and3_2
*
*
* Parameters:
* lsp_old I: LSP vector at the 4th subfr. of past frame [M]
* lsp_mid I: LSP vector at the 2nd subframe of present frame [M]
* lsp_new I: LSP vector at the 4th subframe of present frame [M]
* az O: interpolated LP parameters in subframes 1 and 3
* [AZ_SIZE]
*
* Function:
* Interpolation of the LPC parameters. Same as the Int_lpc
* function but we do not recompute Az() for subframe 2 and
* 4 because it is already available.
*
* Returns:
* void
*/
static void Int_lpc_1and3_2(Float32 lsp_old[], Float32 lsp_mid[], Float32 lsp_new[], Float32 az[])
{
Float32 lsp[M];
Word32 i;
for (i = 0; i < M; i += 2)
{
lsp[i] = (lsp_mid[i] + lsp_old[i]) * 0.5F;
lsp[i + 1] = (lsp_mid[i + 1] + lsp_old[i + 1]) * 0.5F;
}
/* Subframe 1 */
Lsp_Az(lsp, az);
az += MP1 * 2;
for (i = 0; i < M; i += 2)
{
lsp[i] = (lsp_mid[i] + lsp_new[i]) * 0.5F;
lsp[i + 1] = (lsp_mid[i + 1] + lsp_new[i + 1]) * 0.5F;
}
/* Subframe 3 */
Lsp_Az(lsp, az);
return;
}
/*
* Lsp_lsf
*
*
* Parameters:
* lsp I: LSP vector
* lsf O: LSF vector
*
* Function:
* Transformation lsp to lsf, LPC order M
*
* Returns:
* void
*/
static void Lsp_lsf(Float32 lsp[], Float32 lsf[])
{
Word32 i;
for (i = 0; i < M; i++)
{
lsf[i] = (Float32)(acos(lsp[i]) * SCALE_LSP_FREQ);
}
return;
}
/*
* Lsf_wt
*
*
* Parameters:
* lsf I: LSF vector
* wf O: square of weighting factors
*
* Function:
* Compute LSF weighting factors
*
* Returns:
* void
*/
static void Lsf_wt(Float32 *lsf, Float32 *wf)
{
Float32 temp;
Word32 i;
wf[0] = lsf[1];
for (i = 1; i < 9; i++)
{
wf[i] = lsf[i + 1] - lsf[i - 1];
}
wf[9] = 4000.0F - lsf[8];
for (i = 0; i < 10; i++)
{
if (wf[i] < 450.0F)
{
temp = 3.347F - SLOPE1_WGHT_LSF * wf[i];
}
else
{
temp = 1.8F - SLOPE2_WGHT_LSF * (wf[i] - 450.0F);
}
wf[i] = temp * temp;
}
return;
}
/*
* Vq_subvec
*
*
* Parameters:
* lsf_r1 I: 1st LSF residual vector
* lsf_r2 I: 2nd LSF residual vector
* dico I: quantization codebook
* wf1 I: 1st LSF weighting factors
* wf2 I: 2nd LSF weighting factors
* dico_size I: size of quantization codebook
* Function:
* Quantization of a 4 dimensional subvector
*
* Returns:
* index quantization index
*/
static Word16 Vq_subvec(Float32 *lsf_r1, Float32 *lsf_r2, const Float32 *dico,
Float32 *wf1, Float32 *wf2, Word16 dico_size)
{
Float64 temp, dist, dist_min;
const Float32 *p_dico;
Word32 i, index = 0;
dist_min = DBL_MAX;
p_dico = dico;
for (i = 0; i < dico_size; i++)
{
temp = lsf_r1[0] - *p_dico++;
dist = temp * temp * wf1[0];
temp = lsf_r1[1] - *p_dico++;
dist += temp * temp * wf1[1];
temp = lsf_r2[0] - *p_dico++;
dist += temp * temp * wf2[0];
temp = lsf_r2[1] - *p_dico++;
dist += temp * temp * wf2[1];
if (dist < dist_min)
{
dist_min = dist;
index = i;
}
}
/* Reading the selected vector */
p_dico = &dico[index << 2];
lsf_r1[0] = *p_dico++;
lsf_r1[1] = *p_dico++;
lsf_r2[0] = *p_dico++;
lsf_r2[1] = *p_dico++;
return (Word16)index;
}
/*
* Vq_subvec_s
*
*
* Parameters:
* lsf_r1 I: 1st LSF residual vector
* lsf_r2 I: 2nd LSF residual vector
* dico I: quantization codebook
* wf1 I: 1st LSF weighting factors
* wf2 I: 2nd LSF weighting factors
* dico_size I: size of quantization codebook
* Function:
* Quantization of a 4 dimensional subvector with a signed codebook
*
* Returns:
* index quantization index
*/
static Word16 Vq_subvec_s(Float32 *lsf_r1, Float32 *lsf_r2, const Float32 *dico, Float32 *wf1, Float32 *wf2, Word16 dico_size)
{
Float64 dist_min, dist1, dist2, temp1, temp2;
const Float32 *p_dico;
Word32 i, index = 0;
Word16 sign = 0;
dist_min = DBL_MAX;
p_dico = dico;
for (i = 0; i < dico_size; i++)
{
temp1 = lsf_r1[0] - *p_dico;
temp2 = lsf_r1[0] + *p_dico++;
dist1 = temp1 * temp1 * wf1[0];
dist2 = temp2 * temp2 * wf1[0];
temp1 = lsf_r1[1] - *p_dico;
temp2 = lsf_r1[1] + *p_dico++;
dist1 += temp1 * temp1 * wf1[1];
dist2 += temp2 * temp2 * wf1[1];
temp1 = lsf_r2[0] - *p_dico;
temp2 = lsf_r2[0] + *p_dico++;
dist1 += temp1 * temp1 * wf2[0];
dist2 += temp2 * temp2 * wf2[0];
temp1 = lsf_r2[1] - *p_dico;
temp2 = lsf_r2[1] + *p_dico++;
dist1 += temp1 * temp1 * wf2[1];
dist2 += temp2 * temp2 * wf2[1];
if (dist1 < dist_min)
{
dist_min = dist1;