xref: /aosp_15_r20/external/libxaac/encoder/iusace_lpc_avq.c (revision 15dc779a375ca8b5125643b829a8aa4b70d7f451)
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 
iusace_lsf_weight_2st_flt(FLOAT32 * lsfq,FLOAT32 * w,WORD32 mode)54 static VOID iusace_lsf_weight_2st_flt(FLOAT32 *lsfq, FLOAT32 *w, WORD32 mode) {
55   WORD32 i;
56   FLOAT32 d[ORDER + 1];
57 
58   d[0] = lsfq[0];
59   d[ORDER] = FREQ_MAX - lsfq[ORDER - 1];
60   for (i = 1; i < ORDER; i++) {
61     d[i] = lsfq[i] - lsfq[i - 1];
62   }
63 
64   for (i = 0; i < ORDER; i++) {
65     w[i] = (FLOAT32)(iusace_wlsf_factor_table[mode] / (FREQ_DIV / sqrt(d[i] * d[i + 1])));
66   }
67 }
68 
iusace_lsf_weight(FLOAT32 * lsf,FLOAT32 * ptr_w)69 static VOID iusace_lsf_weight(FLOAT32 *lsf, FLOAT32 *ptr_w) {
70   WORD32 i;
71   FLOAT32 d[ORDER + 1];
72 
73   d[0] = lsf[0];
74   d[ORDER] = FREQ_MAX - lsf[ORDER - 1];
75   for (i = 1; i < ORDER; i++) {
76     d[i] = lsf[i] - lsf[i - 1];
77   }
78 
79   for (i = 0; i < ORDER; i++) {
80     ptr_w[i] = (1.0f / d[i]) + (1.0f / d[i + 1]);
81   }
82 
83   return;
84 }
85 
iusace_avq_first_approx_abs(FLOAT32 * lsf,FLOAT32 * lsfq)86 static WORD32 iusace_avq_first_approx_abs(FLOAT32 *lsf, FLOAT32 *lsfq) {
87   WORD32 i, j, index;
88   FLOAT32 w[ORDER];
89   FLOAT32 dist_min, dist, temp;
90   const FLOAT32 *p_dico;
91 
92   iusace_lsf_weight(lsf, w);
93 
94   dist_min = 1.0e30f;
95   p_dico = iusace_dico_lsf_abs_8b_flt;
96   index = 0;
97 
98   for (i = 0; i < 256; i++) {
99     dist = 0.0;
100     for (j = 0; j < ORDER; j++) {
101       temp = lsf[j] - *p_dico++;
102       dist += w[j] * temp * temp;
103     }
104     if (dist < dist_min) {
105       dist_min = dist;
106       index = i;
107     }
108   }
109 
110   for (j = 0; j < ORDER; j++) {
111     lsfq[j] = iusace_dico_lsf_abs_8b_flt[index * ORDER + j];
112   }
113 
114   return index;
115 }
116 
iusace_avq_first_approx_rel(FLOAT32 * ptr_lsf,FLOAT32 * ptr_lsfq,WORD32 * idx,WORD32 mode)117 static WORD32 iusace_avq_first_approx_rel(FLOAT32 *ptr_lsf, FLOAT32 *ptr_lsfq, WORD32 *idx,
118                                           WORD32 mode) {
119   WORD32 i, num_bits;
120   FLOAT32 w[ORDER], x[ORDER], temp;
121   WORD32 nq, avq[ORDER];
122   FLOAT32 lsf_min;
123 
124   iusace_lsf_weight_2st_flt(ptr_lsf, w, 1);
125 
126   temp = 0.0f;
127   for (i = 0; i < ORDER; i++) {
128     x[i] = (ptr_lsf[i] - ptr_lsfq[i]) / w[i];
129     temp += x[i] * x[i];
130   }
131 
132   if (temp < 8.0f) {
133     idx[0] = 0;
134     idx[1] = 0;
135     if ((mode == 0) || (mode == 3)) {
136       return (10);
137     } else if (mode == 1) {
138       return (2);
139     } else {
140       return (6);
141     }
142   }
143 
144   iusace_lsf_weight_2st_flt(ptr_lsfq, w, mode);
145 
146   for (i = 0; i < ORDER; i++) {
147     x[i] = (ptr_lsf[i] - ptr_lsfq[i]) / w[i];
148   }
149 
150   iusace_alg_vec_quant(x, avq, idx);
151 
152   for (i = 0; i < ORDER; i++) {
153     ptr_lsfq[i] += (w[i] * (FLOAT32)avq[i]);
154   }
155 
156   num_bits = 0;
157   for (i = 0; i < 2; i++) {
158     nq = idx[i];
159 
160     if ((mode == 0) || (mode == 3)) {
161       num_bits += (2 + (nq * 4));
162       if (nq > 6) {
163         num_bits += nq - 3;
164       } else if (nq > 4) {
165         num_bits += nq - 4;
166       } else if (nq == 0) {
167         num_bits += 3;
168       }
169     } else if (mode == 1) {
170       num_bits += nq * 5;
171       if (nq == 0) {
172         num_bits += 1;
173       }
174     } else {
175       num_bits += (2 + (nq * 4));
176       if (nq == 0) {
177         num_bits += 1;
178       } else if (nq > 4) {
179         num_bits += nq - 3;
180       }
181     }
182   }
183 
184   lsf_min = LSF_GAP;
185   for (i = 0; i < ORDER; i++) {
186     if (ptr_lsfq[i] < lsf_min) {
187       ptr_lsfq[i] = lsf_min;
188     }
189 
190     lsf_min = ptr_lsfq[i] + LSF_GAP;
191   }
192 
193   lsf_min = FREQ_MAX - LSF_GAP;
194   for (i = ORDER - 1; i >= 0; i--) {
195     if (ptr_lsfq[i] > lsf_min) {
196       ptr_lsfq[i] = lsf_min;
197     }
198 
199     lsf_min = ptr_lsfq[i] - LSF_GAP;
200   }
201 
202   return (num_bits);
203 }
204 
iusace_quantize_lpc_avq(FLOAT32 * ptr_lsf,FLOAT32 * ptr_lsfq,WORD32 lpc0,WORD32 * ptr_lpc_idx,WORD32 * nb_indices,WORD32 * nbbits)205 VOID iusace_quantize_lpc_avq(FLOAT32 *ptr_lsf, FLOAT32 *ptr_lsfq, WORD32 lpc0,
206                              WORD32 *ptr_lpc_idx, WORD32 *nb_indices, WORD32 *nbbits) {
207   WORD32 i;
208   FLOAT32 lsfq[ORDER];
209   WORD32 *ptr_index, indxt[100], num_bits, nbt, nit;
210 
211   ptr_index = &ptr_lpc_idx[0];
212   *nb_indices = 0;
213   *nbbits = 0;
214 
215   ptr_index[0] = iusace_avq_first_approx_abs(&ptr_lsf[3 * ORDER], &ptr_lsfq[3 * ORDER]);
216 
217   nbt = iusace_avq_first_approx_rel(&ptr_lsf[3 * ORDER], &ptr_lsfq[3 * ORDER], &ptr_index[1], 0);
218   nit = 1 + iusace_get_num_params(&ptr_lpc_idx[1]);
219 
220   ptr_index += nit;
221   *nb_indices += nit;
222   *nbbits += 8 + nbt;
223 
224   if (lpc0) {
225     *ptr_index = 0;
226     ptr_index++;
227     *nb_indices += 1;
228     *nbbits += 1;
229 
230     ptr_index[0] = iusace_avq_first_approx_abs(&ptr_lsf[-ORDER], &ptr_lsfq[-ORDER]);
231 
232     num_bits = iusace_avq_first_approx_rel(&ptr_lsf[-ORDER], &ptr_lsfq[-ORDER], &ptr_index[1], 0);
233     nbt = 8 + num_bits;
234     nit = 1 + iusace_get_num_params(&ptr_index[1]);
235 
236     for (i = 0; i < ORDER; i++) lsfq[i] = ptr_lsfq[3 * ORDER + i];
237 
238     num_bits = iusace_avq_first_approx_rel(&ptr_lsf[-ORDER], &lsfq[0], indxt, 3);
239 
240     if (num_bits < nbt) {
241       nbt = num_bits;
242       nit = iusace_get_num_params(&indxt[0]);
243       ptr_index[-1] = 1;
244       for (i = 0; i < ORDER; i++) ptr_lsfq[-ORDER + i] = lsfq[i];
245       for (i = 0; i < nit; i++) ptr_index[i] = indxt[i];
246     }
247 
248     ptr_index += nit;
249     *nb_indices += nit;
250     *nbbits += nbt;
251   }
252 
253   *ptr_index = 0;
254   ptr_index++;
255   *nb_indices += 1;
256   *nbbits += 1;
257 
258   ptr_index[0] = iusace_avq_first_approx_abs(&ptr_lsf[ORDER], &ptr_lsfq[ORDER]);
259 
260   num_bits = iusace_avq_first_approx_rel(&ptr_lsf[ORDER], &ptr_lsfq[ORDER], &ptr_index[1], 0);
261   nbt = 8 + num_bits;
262   nit = 1 + iusace_get_num_params(&ptr_index[1]);
263 
264   for (i = 0; i < ORDER; i++) lsfq[i] = ptr_lsfq[3 * ORDER + i];
265 
266   num_bits = iusace_avq_first_approx_rel(&ptr_lsf[ORDER], &lsfq[0], indxt, 3);
267 
268   if (num_bits < nbt) {
269     nbt = num_bits;
270     nit = iusace_get_num_params(&indxt[0]);
271     ptr_index[-1] = 1;
272     for (i = 0; i < ORDER; i++) ptr_lsfq[ORDER + i] = lsfq[i];
273     for (i = 0; i < nit; i++) ptr_index[i] = indxt[i];
274   }
275 
276   ptr_index += nit;
277   *nb_indices += nit;
278   *nbbits += nbt;
279 
280   *ptr_index = 0;
281   ptr_index++;
282   *nb_indices += 1;
283 
284   ptr_index[0] = iusace_avq_first_approx_abs(&ptr_lsf[0], &ptr_lsfq[0]);
285 
286   num_bits = iusace_avq_first_approx_rel(&ptr_lsf[0], &ptr_lsfq[0], &ptr_index[1], 0);
287   nbt = 2 + 8 + num_bits;
288   nit = 1 + iusace_get_num_params(&ptr_index[1]);
289 
290   for (i = 0; i < ORDER; i++) lsfq[i] = 0.5f * (ptr_lsfq[-ORDER + i] + ptr_lsfq[ORDER + i]);
291 
292   num_bits = iusace_avq_first_approx_rel(&ptr_lsf[0], lsfq, indxt, 1);
293 
294   if (num_bits < 10) {
295     nbt = 2;
296     nit = 0;
297     ptr_index[-1] = 1;
298     for (i = 0; i < ORDER; i++) ptr_lsfq[i] = lsfq[i];
299   }
300 
301   for (i = 0; i < ORDER; i++) lsfq[i] = ptr_lsfq[ORDER + i];
302 
303   num_bits = iusace_avq_first_approx_rel(&ptr_lsf[0], lsfq, indxt, 2);
304   num_bits += 1;
305 
306   if (num_bits < nbt) {
307     nbt = num_bits;
308     nit = iusace_get_num_params(&indxt[0]);
309     ptr_index[-1] = 2;
310     for (i = 0; i < ORDER; i++) ptr_lsfq[i] = lsfq[i];
311     for (i = 0; i < nit; i++) ptr_index[i] = indxt[i];
312   }
313 
314   ptr_index += nit;
315   *nb_indices += nit;
316   *nbbits += nbt;
317 
318   *ptr_index = 0;
319   ptr_index++;
320   *nb_indices += 1;
321 
322   ptr_index[0] = iusace_avq_first_approx_abs(&ptr_lsf[2 * ORDER], &ptr_lsfq[2 * ORDER]);
323 
324   num_bits =
325       iusace_avq_first_approx_rel(&ptr_lsf[2 * ORDER], &ptr_lsfq[2 * ORDER], &ptr_index[1], 0);
326   nbt = 2 + 8 + num_bits;
327   nit = 1 + iusace_get_num_params(&ptr_index[1]);
328 
329   for (i = 0; i < ORDER; i++) lsfq[i] = 0.5f * (ptr_lsfq[ORDER + i] + ptr_lsfq[3 * ORDER + i]);
330 
331   num_bits = iusace_avq_first_approx_rel(&ptr_lsf[2 * ORDER], lsfq, indxt, 1);
332   num_bits += 1;
333 
334   if (num_bits < nbt) {
335     nbt = num_bits;
336     nit = iusace_get_num_params(&indxt[0]);
337     ptr_index[-1] = 1;
338     for (i = 0; i < ORDER; i++) ptr_lsfq[2 * ORDER + i] = lsfq[i];
339     for (i = 0; i < nit; i++) ptr_index[i] = indxt[i];
340   }
341 
342   for (i = 0; i < ORDER; i++) lsfq[i] = ptr_lsfq[ORDER + i];
343 
344   num_bits = iusace_avq_first_approx_rel(&ptr_lsf[2 * ORDER], lsfq, indxt, 2);
345   num_bits += 3;
346 
347   if (num_bits < nbt) {
348     nbt = num_bits;
349     nit = iusace_get_num_params(&indxt[0]);
350     ptr_index[-1] = 2;
351     for (i = 0; i < ORDER; i++) ptr_lsfq[2 * ORDER + i] = lsfq[i];
352     for (i = 0; i < nit; i++) ptr_index[i] = indxt[i];
353   }
354 
355   for (i = 0; i < ORDER; i++) lsfq[i] = ptr_lsfq[3 * ORDER + i];
356 
357   num_bits = iusace_avq_first_approx_rel(&ptr_lsf[2 * ORDER], lsfq, indxt, 2);
358   num_bits += 3;
359 
360   if (num_bits < nbt) {
361     nbt = num_bits;
362     nit = iusace_get_num_params(&indxt[0]);
363     ptr_index[-1] = 3;
364     for (i = 0; i < ORDER; i++) ptr_lsfq[2 * ORDER + i] = lsfq[i];
365     for (i = 0; i < nit; i++) ptr_index[i] = indxt[i];
366   }
367 
368   *nb_indices += nit;
369   *nbbits += nbt;
370 
371   return;
372 }
373