/src/pjsip/third_party/ilbc/gainquant.c
Line | Count | Source |
1 | | |
2 | | /****************************************************************** |
3 | | |
4 | | iLBC Speech Coder ANSI-C Source Code |
5 | | |
6 | | |
7 | | |
8 | | |
9 | | |
10 | | |
11 | | gainquant.c |
12 | | |
13 | | Copyright (C) The Internet Society (2004). |
14 | | All Rights Reserved. |
15 | | |
16 | | ******************************************************************/ |
17 | | |
18 | | #include <string.h> |
19 | | #include <math.h> |
20 | | #include "constants.h" |
21 | | #include "filter.h" |
22 | | |
23 | | /*----------------------------------------------------------------* |
24 | | * quantizer for the gain in the gain-shape coding of residual |
25 | | *---------------------------------------------------------------*/ |
26 | | |
27 | | float gainquant(/* (o) quantized gain value */ |
28 | | float in, /* (i) gain value */ |
29 | | float maxIn,/* (i) maximum of gain value */ |
30 | | int cblen, /* (i) number of quantization indices */ |
31 | | int *index /* (o) quantization index */ |
32 | 6.42k | ){ |
33 | 6.42k | int i, tindex; |
34 | 6.42k | float minmeasure,measure, *cb, scale; |
35 | | |
36 | | /* ensure a lower bound on the scaling factor */ |
37 | | |
38 | 6.42k | scale=maxIn; |
39 | | |
40 | 6.42k | if (scale<0.1) { |
41 | 1.27k | scale=(float)0.1; |
42 | 1.27k | } |
43 | | |
44 | | /* select the quantization table */ |
45 | | |
46 | 6.42k | if (cblen == 8) { |
47 | 2.14k | cb = gain_sq3Tbl; |
48 | 4.28k | } else if (cblen == 16) { |
49 | 2.14k | cb = gain_sq4Tbl; |
50 | 2.14k | } else { |
51 | 2.14k | cb = gain_sq5Tbl; |
52 | 2.14k | } |
53 | | |
54 | | /* select the best index in the quantization table */ |
55 | | |
56 | 6.42k | minmeasure=10000000.0; |
57 | 6.42k | tindex=0; |
58 | 126k | for (i=0; i<cblen; i++) { |
59 | | |
60 | | |
61 | | |
62 | | |
63 | | |
64 | 119k | measure=(in-scale*cb[i])*(in-scale*cb[i]); |
65 | | |
66 | 119k | if (measure<minmeasure) { |
67 | 50.4k | tindex=i; |
68 | 50.4k | minmeasure=measure; |
69 | 50.4k | } |
70 | 119k | } |
71 | 6.42k | *index=tindex; |
72 | | |
73 | | /* return the quantized value */ |
74 | | |
75 | 6.42k | return scale*cb[tindex]; |
76 | 6.42k | } |
77 | | |
78 | | /*----------------------------------------------------------------* |
79 | | * decoder for quantized gains in the gain-shape coding of |
80 | | * residual |
81 | | *---------------------------------------------------------------*/ |
82 | | |
83 | | float gaindequant( /* (o) quantized gain value */ |
84 | | int index, /* (i) quantization index */ |
85 | | float maxIn,/* (i) maximum of unquantized gain */ |
86 | | int cblen /* (i) number of quantization indices */ |
87 | 9.16k | ){ |
88 | 9.16k | float scale; |
89 | | |
90 | | /* obtain correct scale factor */ |
91 | | |
92 | 9.16k | scale=(float)fabs(maxIn); |
93 | | |
94 | 9.16k | if (scale<0.1) { |
95 | 1.72k | scale=(float)0.1; |
96 | 1.72k | } |
97 | | |
98 | | /* select the quantization table and return the decoded value */ |
99 | | |
100 | 9.16k | if (cblen==8) { |
101 | 3.05k | return scale*gain_sq3Tbl[index]; |
102 | 6.11k | } else if (cblen==16) { |
103 | 3.05k | return scale*gain_sq4Tbl[index]; |
104 | 3.05k | } |
105 | 3.05k | else if (cblen==32) { |
106 | 3.05k | return scale*gain_sq5Tbl[index]; |
107 | 3.05k | } |
108 | | |
109 | 0 | return 0.0; |
110 | 9.16k | } |
111 | | |
112 | | |
113 | | |
114 | | |
115 | | |
116 | | |