/src/libxaac/encoder/iusace_lpc_avq.c
Line | Count | Source |
1 | | /****************************************************************************** |
2 | | * * |
3 | | * Copyright (C) 2023 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 | | |
21 | | #include <math.h> |
22 | | #include "ixheaac_type_def.h" |
23 | | #include "ixheaace_adjust_threshold_data.h" |
24 | | #include "iusace_bitbuffer.h" |
25 | | |
26 | | /* DRC */ |
27 | | #include "impd_drc_common_enc.h" |
28 | | #include "impd_drc_uni_drc.h" |
29 | | #include "impd_drc_tables.h" |
30 | | #include "impd_drc_api.h" |
31 | | #include "impd_drc_uni_drc_eq.h" |
32 | | #include "impd_drc_uni_drc_filter_bank.h" |
33 | | #include "impd_drc_gain_enc.h" |
34 | | #include "impd_drc_struct_def.h" |
35 | | |
36 | | #include "iusace_cnst.h" |
37 | | #include "iusace_tns_usac.h" |
38 | | #include "iusace_psy_mod.h" |
39 | | #include "iusace_config.h" |
40 | | #include "iusace_arith_enc.h" |
41 | | #include "iusace_block_switch_const.h" |
42 | | #include "iusace_block_switch_struct_def.h" |
43 | | #include "iusace_fd_qc_util.h" |
44 | | #include "iusace_fd_quant.h" |
45 | | #include "iusace_ms.h" |
46 | | #include "iusace_signal_classifier.h" |
47 | | #include "ixheaace_sbr_header.h" |
48 | | #include "ixheaace_config.h" |
49 | | #include "ixheaace_asc_write.h" |
50 | | #include "iusace_main.h" |
51 | | #include "iusace_func_prototypes.h" |
52 | | #include "iusace_avq_enc.h" |
53 | | |
54 | 2.40M | static VOID iusace_lsf_weight_2st_flt(FLOAT32 *lsfq, FLOAT32 *w, WORD32 mode) { |
55 | 2.40M | WORD32 i; |
56 | 2.40M | FLOAT32 d[ORDER + 1]; |
57 | | |
58 | 2.40M | d[0] = lsfq[0]; |
59 | 2.40M | d[ORDER] = FREQ_MAX - lsfq[ORDER - 1]; |
60 | 38.4M | for (i = 1; i < ORDER; i++) { |
61 | 36.0M | d[i] = lsfq[i] - lsfq[i - 1]; |
62 | 36.0M | } |
63 | | |
64 | 40.8M | for (i = 0; i < ORDER; i++) { |
65 | 38.4M | w[i] = (FLOAT32)(iusace_wlsf_factor_table[mode] / (FREQ_DIV / sqrt(d[i] * d[i + 1]))); |
66 | 38.4M | } |
67 | 2.40M | } |
68 | | |
69 | 537k | static VOID iusace_lsf_weight(FLOAT32 *lsf, FLOAT32 *ptr_w) { |
70 | 537k | WORD32 i; |
71 | 537k | FLOAT32 d[ORDER + 1]; |
72 | | |
73 | 537k | d[0] = lsf[0]; |
74 | 537k | d[ORDER] = FREQ_MAX - lsf[ORDER - 1]; |
75 | 8.59M | for (i = 1; i < ORDER; i++) { |
76 | 8.05M | d[i] = lsf[i] - lsf[i - 1]; |
77 | 8.05M | } |
78 | | |
79 | 9.13M | for (i = 0; i < ORDER; i++) { |
80 | 8.59M | ptr_w[i] = (1.0f / d[i]) + (1.0f / d[i + 1]); |
81 | 8.59M | } |
82 | | |
83 | 537k | return; |
84 | 537k | } |
85 | | |
86 | 537k | static WORD32 iusace_avq_first_approx_abs(FLOAT32 *lsf, FLOAT32 *lsfq) { |
87 | 537k | WORD32 i, j, index; |
88 | 537k | FLOAT32 w[ORDER]; |
89 | 537k | FLOAT32 dist_min, dist, temp; |
90 | 537k | const FLOAT32 *p_dico; |
91 | | |
92 | 537k | iusace_lsf_weight(lsf, w); |
93 | | |
94 | 537k | dist_min = 1.0e30f; |
95 | 537k | p_dico = iusace_dico_lsf_abs_8b_flt; |
96 | 537k | index = 0; |
97 | | |
98 | 138M | for (i = 0; i < 256; i++) { |
99 | 137M | dist = 0.0; |
100 | 2.33G | for (j = 0; j < ORDER; j++) { |
101 | 2.19G | temp = lsf[j] - *p_dico++; |
102 | 2.19G | dist += w[j] * temp * temp; |
103 | 2.19G | } |
104 | 137M | if (dist < dist_min) { |
105 | 3.59M | dist_min = dist; |
106 | 3.59M | index = i; |
107 | 3.59M | } |
108 | 137M | } |
109 | | |
110 | 9.13M | for (j = 0; j < ORDER; j++) { |
111 | 8.59M | lsfq[j] = iusace_dico_lsf_abs_8b_flt[index * ORDER + j]; |
112 | 8.59M | } |
113 | | |
114 | 537k | return index; |
115 | 537k | } |
116 | | |
117 | | static WORD32 iusace_avq_first_approx_rel(FLOAT32 *ptr_lsf, FLOAT32 *ptr_lsfq, WORD32 *idx, |
118 | 1.34M | WORD32 mode) { |
119 | 1.34M | WORD32 i, num_bits; |
120 | 1.34M | FLOAT32 w[ORDER], x[ORDER], temp; |
121 | 1.34M | WORD32 nq, avq[ORDER]; |
122 | 1.34M | FLOAT32 lsf_min; |
123 | | |
124 | 1.34M | iusace_lsf_weight_2st_flt(ptr_lsf, w, 1); |
125 | | |
126 | 1.34M | temp = 0.0f; |
127 | 22.7M | for (i = 0; i < ORDER; i++) { |
128 | 21.4M | x[i] = (ptr_lsf[i] - ptr_lsfq[i]) / w[i]; |
129 | 21.4M | temp += x[i] * x[i]; |
130 | 21.4M | } |
131 | | |
132 | 1.34M | if (temp < 8.0f) { |
133 | 280k | idx[0] = 0; |
134 | 280k | idx[1] = 0; |
135 | 280k | if ((mode == 0) || (mode == 3)) { |
136 | 48.6k | return (10); |
137 | 232k | } else if (mode == 1) { |
138 | 81.1k | return (2); |
139 | 150k | } else { |
140 | 150k | return (6); |
141 | 150k | } |
142 | 280k | } |
143 | | |
144 | 1.05M | iusace_lsf_weight_2st_flt(ptr_lsfq, w, mode); |
145 | | |
146 | 18.0M | for (i = 0; i < ORDER; i++) { |
147 | 16.9M | x[i] = (ptr_lsf[i] - ptr_lsfq[i]) / w[i]; |
148 | 16.9M | } |
149 | | |
150 | 1.05M | iusace_alg_vec_quant(x, avq, idx); |
151 | | |
152 | 18.0M | for (i = 0; i < ORDER; i++) { |
153 | 16.9M | ptr_lsfq[i] += (w[i] * (FLOAT32)avq[i]); |
154 | 16.9M | } |
155 | | |
156 | 1.05M | num_bits = 0; |
157 | 3.17M | for (i = 0; i < 2; i++) { |
158 | 2.11M | nq = idx[i]; |
159 | | |
160 | 2.11M | if ((mode == 0) || (mode == 3)) { |
161 | 1.25M | num_bits += (2 + (nq * 4)); |
162 | 1.25M | if (nq > 6) { |
163 | 126k | num_bits += nq - 3; |
164 | 1.12M | } else if (nq > 4) { |
165 | 438k | num_bits += nq - 4; |
166 | 686k | } else if (nq == 0) { |
167 | 221k | num_bits += 3; |
168 | 221k | } |
169 | 1.25M | } else if (mode == 1) { |
170 | 370k | num_bits += nq * 5; |
171 | 370k | if (nq == 0) { |
172 | 21.9k | num_bits += 1; |
173 | 21.9k | } |
174 | 498k | } else { |
175 | 498k | num_bits += (2 + (nq * 4)); |
176 | 498k | if (nq == 0) { |
177 | 30.0k | num_bits += 1; |
178 | 467k | } else if (nq > 4) { |
179 | 220k | num_bits += nq - 3; |
180 | 220k | } |
181 | 498k | } |
182 | 2.11M | } |
183 | | |
184 | 1.05M | lsf_min = LSF_GAP; |
185 | 18.0M | for (i = 0; i < ORDER; i++) { |
186 | 16.9M | if (ptr_lsfq[i] < lsf_min) { |
187 | 391k | ptr_lsfq[i] = lsf_min; |
188 | 391k | } |
189 | | |
190 | 16.9M | lsf_min = ptr_lsfq[i] + LSF_GAP; |
191 | 16.9M | } |
192 | | |
193 | 1.05M | lsf_min = FREQ_MAX - LSF_GAP; |
194 | 18.0M | for (i = ORDER - 1; i >= 0; i--) { |
195 | 16.9M | if (ptr_lsfq[i] > lsf_min) { |
196 | 48.6k | ptr_lsfq[i] = lsf_min; |
197 | 48.6k | } |
198 | | |
199 | 16.9M | lsf_min = ptr_lsfq[i] - LSF_GAP; |
200 | 16.9M | } |
201 | | |
202 | 1.05M | return (num_bits); |
203 | 1.34M | } |
204 | | |
205 | | VOID iusace_quantize_lpc_avq(FLOAT32 *ptr_lsf, FLOAT32 *ptr_lsfq, WORD32 lpc0, |
206 | 133k | WORD32 *ptr_lpc_idx, WORD32 *nb_indices, WORD32 *nbbits) { |
207 | 133k | WORD32 i; |
208 | 133k | FLOAT32 lsfq[ORDER]; |
209 | 133k | WORD32 *ptr_index, indxt[100], num_bits, nbt, nit; |
210 | | |
211 | 133k | ptr_index = &ptr_lpc_idx[0]; |
212 | 133k | *nb_indices = 0; |
213 | 133k | *nbbits = 0; |
214 | | |
215 | 133k | ptr_index[0] = iusace_avq_first_approx_abs(&ptr_lsf[3 * ORDER], &ptr_lsfq[3 * ORDER]); |
216 | | |
217 | 133k | nbt = iusace_avq_first_approx_rel(&ptr_lsf[3 * ORDER], &ptr_lsfq[3 * ORDER], &ptr_index[1], 0); |
218 | 133k | nit = 1 + iusace_get_num_params(&ptr_lpc_idx[1]); |
219 | | |
220 | 133k | ptr_index += nit; |
221 | 133k | *nb_indices += nit; |
222 | 133k | *nbbits += 8 + nbt; |
223 | | |
224 | 133k | if (lpc0) { |
225 | 3.89k | *ptr_index = 0; |
226 | 3.89k | ptr_index++; |
227 | 3.89k | *nb_indices += 1; |
228 | 3.89k | *nbbits += 1; |
229 | | |
230 | 3.89k | ptr_index[0] = iusace_avq_first_approx_abs(&ptr_lsf[-ORDER], &ptr_lsfq[-ORDER]); |
231 | | |
232 | 3.89k | num_bits = iusace_avq_first_approx_rel(&ptr_lsf[-ORDER], &ptr_lsfq[-ORDER], &ptr_index[1], 0); |
233 | 3.89k | nbt = 8 + num_bits; |
234 | 3.89k | nit = 1 + iusace_get_num_params(&ptr_index[1]); |
235 | | |
236 | 66.2k | for (i = 0; i < ORDER; i++) lsfq[i] = ptr_lsfq[3 * ORDER + i]; |
237 | | |
238 | 3.89k | num_bits = iusace_avq_first_approx_rel(&ptr_lsf[-ORDER], &lsfq[0], indxt, 3); |
239 | | |
240 | 3.89k | if (num_bits < nbt) { |
241 | 3.78k | nbt = num_bits; |
242 | 3.78k | nit = iusace_get_num_params(&indxt[0]); |
243 | 3.78k | ptr_index[-1] = 1; |
244 | 64.3k | for (i = 0; i < ORDER; i++) ptr_lsfq[-ORDER + i] = lsfq[i]; |
245 | 43.2k | for (i = 0; i < nit; i++) ptr_index[i] = indxt[i]; |
246 | 3.78k | } |
247 | | |
248 | 3.89k | ptr_index += nit; |
249 | 3.89k | *nb_indices += nit; |
250 | 3.89k | *nbbits += nbt; |
251 | 3.89k | } |
252 | | |
253 | 133k | *ptr_index = 0; |
254 | 133k | ptr_index++; |
255 | 133k | *nb_indices += 1; |
256 | 133k | *nbbits += 1; |
257 | | |
258 | 133k | ptr_index[0] = iusace_avq_first_approx_abs(&ptr_lsf[ORDER], &ptr_lsfq[ORDER]); |
259 | | |
260 | 133k | num_bits = iusace_avq_first_approx_rel(&ptr_lsf[ORDER], &ptr_lsfq[ORDER], &ptr_index[1], 0); |
261 | 133k | nbt = 8 + num_bits; |
262 | 133k | nit = 1 + iusace_get_num_params(&ptr_index[1]); |
263 | | |
264 | 2.26M | for (i = 0; i < ORDER; i++) lsfq[i] = ptr_lsfq[3 * ORDER + i]; |
265 | | |
266 | 133k | num_bits = iusace_avq_first_approx_rel(&ptr_lsf[ORDER], &lsfq[0], indxt, 3); |
267 | | |
268 | 133k | if (num_bits < nbt) { |
269 | 64.1k | nbt = num_bits; |
270 | 64.1k | nit = iusace_get_num_params(&indxt[0]); |
271 | 64.1k | ptr_index[-1] = 1; |
272 | 1.09M | for (i = 0; i < ORDER; i++) ptr_lsfq[ORDER + i] = lsfq[i]; |
273 | 663k | for (i = 0; i < nit; i++) ptr_index[i] = indxt[i]; |
274 | 64.1k | } |
275 | | |
276 | 133k | ptr_index += nit; |
277 | 133k | *nb_indices += nit; |
278 | 133k | *nbbits += nbt; |
279 | | |
280 | 133k | *ptr_index = 0; |
281 | 133k | ptr_index++; |
282 | 133k | *nb_indices += 1; |
283 | | |
284 | 133k | ptr_index[0] = iusace_avq_first_approx_abs(&ptr_lsf[0], &ptr_lsfq[0]); |
285 | | |
286 | 133k | num_bits = iusace_avq_first_approx_rel(&ptr_lsf[0], &ptr_lsfq[0], &ptr_index[1], 0); |
287 | 133k | nbt = 2 + 8 + num_bits; |
288 | 133k | nit = 1 + iusace_get_num_params(&ptr_index[1]); |
289 | | |
290 | 2.26M | for (i = 0; i < ORDER; i++) lsfq[i] = 0.5f * (ptr_lsfq[-ORDER + i] + ptr_lsfq[ORDER + i]); |
291 | | |
292 | 133k | num_bits = iusace_avq_first_approx_rel(&ptr_lsf[0], lsfq, indxt, 1); |
293 | | |
294 | 133k | if (num_bits < 10) { |
295 | 39.2k | nbt = 2; |
296 | 39.2k | nit = 0; |
297 | 39.2k | ptr_index[-1] = 1; |
298 | 667k | for (i = 0; i < ORDER; i++) ptr_lsfq[i] = lsfq[i]; |
299 | 39.2k | } |
300 | | |
301 | 2.26M | for (i = 0; i < ORDER; i++) lsfq[i] = ptr_lsfq[ORDER + i]; |
302 | | |
303 | 133k | num_bits = iusace_avq_first_approx_rel(&ptr_lsf[0], lsfq, indxt, 2); |
304 | 133k | num_bits += 1; |
305 | | |
306 | 133k | if (num_bits < nbt) { |
307 | 74.4k | nbt = num_bits; |
308 | 74.4k | nit = iusace_get_num_params(&indxt[0]); |
309 | 74.4k | ptr_index[-1] = 2; |
310 | 1.26M | for (i = 0; i < ORDER; i++) ptr_lsfq[i] = lsfq[i]; |
311 | 812k | for (i = 0; i < nit; i++) ptr_index[i] = indxt[i]; |
312 | 74.4k | } |
313 | | |
314 | 133k | ptr_index += nit; |
315 | 133k | *nb_indices += nit; |
316 | 133k | *nbbits += nbt; |
317 | | |
318 | 133k | *ptr_index = 0; |
319 | 133k | ptr_index++; |
320 | 133k | *nb_indices += 1; |
321 | | |
322 | 133k | ptr_index[0] = iusace_avq_first_approx_abs(&ptr_lsf[2 * ORDER], &ptr_lsfq[2 * ORDER]); |
323 | | |
324 | 133k | num_bits = |
325 | 133k | iusace_avq_first_approx_rel(&ptr_lsf[2 * ORDER], &ptr_lsfq[2 * ORDER], &ptr_index[1], 0); |
326 | 133k | nbt = 2 + 8 + num_bits; |
327 | 133k | nit = 1 + iusace_get_num_params(&ptr_index[1]); |
328 | | |
329 | 2.26M | for (i = 0; i < ORDER; i++) lsfq[i] = 0.5f * (ptr_lsfq[ORDER + i] + ptr_lsfq[3 * ORDER + i]); |
330 | | |
331 | 133k | num_bits = iusace_avq_first_approx_rel(&ptr_lsf[2 * ORDER], lsfq, indxt, 1); |
332 | 133k | num_bits += 1; |
333 | | |
334 | 133k | if (num_bits < nbt) { |
335 | 111k | nbt = num_bits; |
336 | 111k | nit = iusace_get_num_params(&indxt[0]); |
337 | 111k | ptr_index[-1] = 1; |
338 | 1.88M | for (i = 0; i < ORDER; i++) ptr_lsfq[2 * ORDER + i] = lsfq[i]; |
339 | 1.49M | for (i = 0; i < nit; i++) ptr_index[i] = indxt[i]; |
340 | 111k | } |
341 | | |
342 | 2.26M | for (i = 0; i < ORDER; i++) lsfq[i] = ptr_lsfq[ORDER + i]; |
343 | | |
344 | 133k | num_bits = iusace_avq_first_approx_rel(&ptr_lsf[2 * ORDER], lsfq, indxt, 2); |
345 | 133k | num_bits += 3; |
346 | | |
347 | 133k | if (num_bits < nbt) { |
348 | 19.1k | nbt = num_bits; |
349 | 19.1k | nit = iusace_get_num_params(&indxt[0]); |
350 | 19.1k | ptr_index[-1] = 2; |
351 | 325k | for (i = 0; i < ORDER; i++) ptr_lsfq[2 * ORDER + i] = lsfq[i]; |
352 | 359k | for (i = 0; i < nit; i++) ptr_index[i] = indxt[i]; |
353 | 19.1k | } |
354 | | |
355 | 2.26M | for (i = 0; i < ORDER; i++) lsfq[i] = ptr_lsfq[3 * ORDER + i]; |
356 | | |
357 | 133k | num_bits = iusace_avq_first_approx_rel(&ptr_lsf[2 * ORDER], lsfq, indxt, 2); |
358 | 133k | num_bits += 3; |
359 | | |
360 | 133k | if (num_bits < nbt) { |
361 | 32.8k | nbt = num_bits; |
362 | 32.8k | nit = iusace_get_num_params(&indxt[0]); |
363 | 32.8k | ptr_index[-1] = 3; |
364 | 559k | for (i = 0; i < ORDER; i++) ptr_lsfq[2 * ORDER + i] = lsfq[i]; |
365 | 508k | for (i = 0; i < nit; i++) ptr_index[i] = indxt[i]; |
366 | 32.8k | } |
367 | | |
368 | 133k | *nb_indices += nit; |
369 | 133k | *nbbits += nbt; |
370 | | |
371 | 133k | return; |
372 | 133k | } |