/src/libxaac/decoder/ixheaacd_lpc_dec.c
Line | Count | Source (jump to first uncovered line) |
1 | | /****************************************************************************** |
2 | | * * |
3 | | * Copyright (C) 2018 The Android Open Source Project |
4 | | * |
5 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
6 | | * you may not use this file except in compliance with the License. |
7 | | * You may obtain a copy of the License at: |
8 | | * |
9 | | * http://www.apache.org/licenses/LICENSE-2.0 |
10 | | * |
11 | | * Unless required by applicable law or agreed to in writing, software |
12 | | * distributed under the License is distributed on an "AS IS" BASIS, |
13 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 | | * See the License for the specific language governing permissions and |
15 | | * limitations under the License. |
16 | | * |
17 | | ***************************************************************************** |
18 | | * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore |
19 | | */ |
20 | | #include <math.h> |
21 | | #include "ixheaac_type_def.h" |
22 | | #include "ixheaacd_bitbuffer.h" |
23 | | #include "ixheaacd_interface.h" |
24 | | #include "ixheaacd_tns_usac.h" |
25 | | #include "ixheaacd_cnst.h" |
26 | | #include "ixheaacd_acelp_info.h" |
27 | | #include "ixheaacd_sbrdecsettings.h" |
28 | | #include "ixheaacd_info.h" |
29 | | #include "ixheaacd_sbr_common.h" |
30 | | #include "ixheaacd_drc_data_struct.h" |
31 | | #include "ixheaacd_drc_dec.h" |
32 | | #include "ixheaacd_sbrdecoder.h" |
33 | | #include "ixheaacd_mps_polyphase.h" |
34 | | #include "ixheaacd_ec_defines.h" |
35 | | #include "ixheaacd_ec_struct_def.h" |
36 | | #include "ixheaacd_main.h" |
37 | | #include "ixheaacd_arith_dec.h" |
38 | | #include "ixheaacd_func_def.h" |
39 | | #include "ixheaacd_acelp_com.h" |
40 | | |
41 | | #include "ixheaac_basic_op.h" |
42 | | #include "ixheaac_constants.h" |
43 | | #include "ixheaac_basic_ops32.h" |
44 | | #include "ixheaac_basic_ops40.h" |
45 | | |
46 | 9.10M | #define LSF_GAP 50.0f |
47 | 337k | #define FREQ_MAX 6400.0f |
48 | 2.23M | #define FREQ_DIV 400.0f |
49 | | |
50 | | static const FLOAT32 factor_table[4] = {60.0f, 65.0f, 64.0f, 63.0f}; |
51 | | |
52 | 139k | VOID ixheaacd_lsf_weight_2st_flt(FLOAT32 *lsfq, FLOAT32 *w, WORD32 mode) { |
53 | 139k | WORD32 i; |
54 | 139k | FLOAT32 d[ORDER + 1]; |
55 | | |
56 | 139k | d[0] = lsfq[0]; |
57 | 139k | d[ORDER] = FREQ_MAX - lsfq[ORDER - 1]; |
58 | 2.23M | for (i = 1; i < ORDER; i++) { |
59 | 2.09M | d[i] = lsfq[i] - lsfq[i - 1]; |
60 | 2.09M | } |
61 | | |
62 | 2.37M | for (i = 0; i < ORDER; i++) { |
63 | 2.23M | w[i] = (FLOAT32)((factor_table[mode] * sqrt(d[i] * d[i + 1])) / FREQ_DIV); |
64 | 2.23M | } |
65 | | |
66 | 139k | return; |
67 | 139k | } |
68 | | |
69 | 337k | static WORD32 ixheaacd_decoding_avq_tool(WORD32 *read_arr, WORD32 *nvecq) { |
70 | 337k | WORD32 i, k, qn, kv[8] = {0}; |
71 | 337k | WORD32 code_book_idx; |
72 | 337k | WORD32 *ptr_kv = &kv[0]; |
73 | | |
74 | 337k | WORD32 position = 2; |
75 | | |
76 | 1.01M | for (k = 0; k < 2; k++) { |
77 | 675k | qn = read_arr[k]; |
78 | | |
79 | 675k | if (qn > 0) { |
80 | 586k | code_book_idx = read_arr[position++]; |
81 | | |
82 | 586k | ptr_kv = &read_arr[position]; |
83 | 586k | position += 8; |
84 | | |
85 | 586k | } else { |
86 | 89.1k | code_book_idx = 0; |
87 | 802k | for (i = 0; i < 8; i++) ptr_kv = &kv[0]; |
88 | 89.1k | } |
89 | | |
90 | 675k | ixheaacd_rotated_gosset_mtx_dec(qn, code_book_idx, ptr_kv, &nvecq[k * 8]); |
91 | 675k | } |
92 | | |
93 | 337k | return position; |
94 | 337k | } |
95 | | |
96 | 197k | static WORD32 ixheaacd_avq_first_approx_abs(FLOAT32 *lsf, WORD32 *indx) { |
97 | 197k | WORD32 i; |
98 | 197k | extern const FLOAT32 ixheaacd_dico_lsf_abs_8b_flt[]; |
99 | 197k | extern const FLOAT32 ixheaacd_weight_table_avq[]; |
100 | 197k | WORD32 position = 0; |
101 | 197k | WORD32 avq[ORDER]; |
102 | 197k | FLOAT32 lsf_min; |
103 | 197k | const FLOAT32 *ptr_w; |
104 | | |
105 | 197k | ptr_w = &ixheaacd_weight_table_avq[(indx[0] * ORDER)]; |
106 | | |
107 | 197k | position++; |
108 | | |
109 | 3.36M | for (i = 0; i < ORDER; i++) { |
110 | 3.16M | lsf[i] = ixheaacd_dico_lsf_abs_8b_flt[indx[0] * ORDER + i]; |
111 | 3.16M | } |
112 | | |
113 | 197k | position += ixheaacd_decoding_avq_tool(&indx[position], avq); |
114 | | |
115 | 197k | lsf_min = LSF_GAP; |
116 | 3.36M | for (i = 0; i < ORDER; i++) { |
117 | 3.16M | lsf[i] += (ptr_w[i] * avq[i]); |
118 | | |
119 | 3.16M | if (lsf[i] < lsf_min) lsf[i] = lsf_min; |
120 | | |
121 | 3.16M | lsf_min = lsf[i] + LSF_GAP; |
122 | 3.16M | } |
123 | | |
124 | 197k | lsf_min = FREQ_MAX - LSF_GAP; |
125 | 3.36M | for (i = ORDER - 1; i >= 0; i--) { |
126 | 3.16M | if (lsf[i] > lsf_min) { |
127 | 32.8k | lsf[i] = lsf_min; |
128 | 32.8k | } |
129 | | |
130 | 3.16M | lsf_min = lsf[i] - LSF_GAP; |
131 | 3.16M | } |
132 | | |
133 | 197k | return position; |
134 | 197k | } |
135 | | |
136 | 139k | WORD32 ixheaacd_avq_first_approx_rel(FLOAT32 *lsf, WORD32 *indx, WORD32 mode) { |
137 | 139k | WORD32 i; |
138 | 139k | FLOAT32 w[ORDER]; |
139 | 139k | WORD32 avq[ORDER]; |
140 | 139k | WORD32 position = 0; |
141 | 139k | FLOAT32 lsf_min; |
142 | | |
143 | 139k | ixheaacd_lsf_weight_2st_flt(lsf, w, mode); |
144 | | |
145 | 139k | position = ixheaacd_decoding_avq_tool(indx, avq); |
146 | | |
147 | 139k | lsf_min = LSF_GAP; |
148 | | |
149 | 2.37M | for (i = 0; i < ORDER; i++) { |
150 | 2.23M | lsf[i] += (w[i] * avq[i]); |
151 | | |
152 | 2.23M | if (lsf[i] < lsf_min) lsf[i] = lsf_min; |
153 | | |
154 | 2.23M | lsf_min = lsf[i] + LSF_GAP; |
155 | 2.23M | } |
156 | | |
157 | 139k | return position; |
158 | 139k | } |
159 | | |
160 | | VOID ixheaacd_alg_vec_dequant(ia_td_frame_data_struct *pstr_td_frame_data, WORD32 first_lpd_flag, |
161 | 78.6k | FLOAT32 *lsf, WORD32 mod[], WORD32 ec_flag) { |
162 | 78.6k | WORD32 i; |
163 | 78.6k | WORD32 *lpc_index, mode_lpc, pos = 0; |
164 | 78.6k | WORD32 lpc_present[5] = {0, 0, 0, 0, 0}; |
165 | 78.6k | lpc_index = pstr_td_frame_data->lpc_first_approx_idx; |
166 | 78.6k | lpc_present[4] = 1; |
167 | 78.6k | pos = ixheaacd_avq_first_approx_abs(&lsf[4 * ORDER], &lpc_index[0]); |
168 | | |
169 | 78.6k | lpc_index += pos; |
170 | | |
171 | 78.6k | if (first_lpd_flag) { |
172 | 53.6k | mode_lpc = lpc_index[0]; |
173 | 53.6k | lpc_index++; |
174 | | |
175 | 53.6k | if (mode_lpc == 0) { |
176 | 35.8k | pos = ixheaacd_avq_first_approx_abs(&lsf[0], &lpc_index[0]); |
177 | | |
178 | 35.8k | } else if (mode_lpc == 1) { |
179 | 303k | for (i = 0; i < ORDER; i++) lsf[i] = lsf[4 * ORDER + i]; |
180 | 17.8k | pos = ixheaacd_avq_first_approx_rel(&lsf[0], &lpc_index[0], 3); |
181 | 17.8k | } |
182 | | |
183 | 53.6k | lpc_index += pos; |
184 | 53.6k | } |
185 | 78.6k | lpc_present[0] = 1; |
186 | | |
187 | 78.6k | if (mod[0] < 3) { |
188 | 78.0k | mode_lpc = lpc_index[0]; |
189 | 78.0k | lpc_index++; |
190 | 78.0k | lpc_present[2] = 1; |
191 | | |
192 | 78.0k | if (mode_lpc == 0) { |
193 | 53.8k | pos = ixheaacd_avq_first_approx_abs(&lsf[2 * ORDER], &lpc_index[0]); |
194 | 53.8k | } else if (mode_lpc == 1) { |
195 | 410k | for (i = 0; i < ORDER; i++) lsf[2 * ORDER + i] = lsf[4 * ORDER + i]; |
196 | 24.1k | pos = ixheaacd_avq_first_approx_rel(&lsf[2 * ORDER], &lpc_index[0], 3); |
197 | 24.1k | } |
198 | | |
199 | 78.0k | lpc_index += pos; |
200 | 78.0k | } |
201 | | |
202 | 78.6k | if (mod[0] < 2) { |
203 | 58.0k | mode_lpc = lpc_index[0]; |
204 | 58.0k | lpc_index++; |
205 | 58.0k | lpc_present[1] = 1; |
206 | | |
207 | 58.0k | if (mode_lpc == 1) { |
208 | 66.3k | for (i = 0; i < ORDER; i++) |
209 | 62.4k | lsf[ORDER + i] = 0.5f * (lsf[i] + lsf[2 * ORDER + i]); |
210 | 54.1k | } else { |
211 | 54.1k | if (mode_lpc == 0) { |
212 | 11.4k | pos = ixheaacd_avq_first_approx_abs(&lsf[ORDER], &lpc_index[0]); |
213 | 42.7k | } else if (mode_lpc == 2) { |
214 | 726k | for (i = 0; i < ORDER; i++) lsf[ORDER + i] = lsf[2 * ORDER + i]; |
215 | 42.7k | pos = ixheaacd_avq_first_approx_rel(&lsf[ORDER], &lpc_index[0], 2); |
216 | 42.7k | } |
217 | | |
218 | 54.1k | lpc_index += pos; |
219 | 54.1k | } |
220 | 58.0k | } |
221 | | |
222 | 78.6k | if (mod[2] < 2) { |
223 | 73.2k | mode_lpc = lpc_index[0]; |
224 | 73.2k | lpc_index++; |
225 | 73.2k | lpc_present[3] = 1; |
226 | | |
227 | 73.2k | if (mode_lpc == 0) { |
228 | 17.9k | pos = ixheaacd_avq_first_approx_abs(&lsf[3 * ORDER], &lpc_index[0]); |
229 | 55.2k | } else if (mode_lpc == 1) { |
230 | 816k | for (i = 0; i < ORDER; i++) |
231 | 768k | lsf[3 * ORDER + i] = 0.5f * (lsf[2 * ORDER + i] + lsf[4 * ORDER + i]); |
232 | 48.0k | pos = ixheaacd_avq_first_approx_rel(&lsf[3 * ORDER], &lpc_index[0], 1); |
233 | 48.0k | } else if (mode_lpc == 2) { |
234 | 31.4k | for (i = 0; i < ORDER; i++) lsf[3 * ORDER + i] = lsf[2 * ORDER + i]; |
235 | 1.85k | pos = ixheaacd_avq_first_approx_rel(&lsf[3 * ORDER], &lpc_index[0], 2); |
236 | 5.34k | } else if (mode_lpc == 3) { |
237 | 90.9k | for (i = 0; i < ORDER; i++) lsf[3 * ORDER + i] = lsf[4 * ORDER + i]; |
238 | 5.34k | pos = ixheaacd_avq_first_approx_rel(&lsf[3 * ORDER], &lpc_index[0], 2); |
239 | 5.34k | } |
240 | | |
241 | 73.2k | lpc_index += pos; |
242 | 73.2k | } |
243 | 78.6k | if (ec_flag) { |
244 | 0 | WORD32 last, k; |
245 | 0 | WORD32 num_lpc = 0, num_div = 4; |
246 | 0 | FLOAT32 div_fac; |
247 | 0 | FLOAT32 *lsf4 = &lsf[4 * ORDER]; |
248 | 0 | for (i = 0; i < ORDER; i++) { |
249 | 0 | pstr_td_frame_data->lpc4_lsf[i] = lsf4[i]; |
250 | 0 | } |
251 | 0 | i = num_div; |
252 | 0 | do { |
253 | 0 | num_lpc += lpc_present[i--]; |
254 | 0 | } while (i >= 0 && num_lpc < 3); |
255 | |
|
256 | 0 | last = i; |
257 | |
|
258 | 0 | switch (num_lpc) { |
259 | 0 | case 3: |
260 | 0 | div_fac = (1.0f / 3.0f); |
261 | 0 | break; |
262 | 0 | case 2: |
263 | 0 | div_fac = (1.0f / 2.0f); |
264 | 0 | break; |
265 | 0 | default: |
266 | 0 | div_fac = (1.0f); |
267 | 0 | break; |
268 | 0 | } |
269 | 0 | for (k = 0; k < ORDER; k++) { |
270 | 0 | FLOAT32 temp = 0; |
271 | 0 | for (i = 4; i > last; i--) { |
272 | 0 | if (lpc_present[i]) { |
273 | 0 | temp = temp + (lsf[i * ORDER + k] * div_fac); |
274 | 0 | } |
275 | 0 | } |
276 | 0 | pstr_td_frame_data->lsf_adaptive_mean_cand[k] = temp; |
277 | 0 | } |
278 | 0 | } |
279 | 78.6k | } |