• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "ixheaacd_type_def.h"
21 #include "ixheaacd_mps_struct_def.h"
22 #include "ixheaacd_mps_res_rom.h"
23 #include "ixheaacd_mps_aac_struct.h"
24 #include "ixheaacd_constants.h"
25 #include "ixheaacd_basic_ops32.h"
26 #include "ixheaacd_basic_ops40.h"
27 #include "ixheaacd_bitbuffer.h"
28 #include "ixheaacd_common_rom.h"
29 #include "ixheaacd_sbrdecsettings.h"
30 #include "ixheaacd_sbr_scale.h"
31 #include "ixheaacd_env_extr_part.h"
32 #include "ixheaacd_sbr_rom.h"
33 #include "ixheaacd_hybrid.h"
34 #include "ixheaacd_ps_dec.h"
35 #include "ixheaacd_error_standards.h"
36 #include "ixheaacd_mps_polyphase.h"
37 #include "ixheaacd_config.h"
38 #include "ixheaacd_qmf_dec.h"
39 #include "ixheaacd_mps_dec.h"
40 #include "ixheaacd_mps_macro_def.h"
41 #include "ixheaacd_mps_basic_op.h"
42 
ixheaacd_get_matrix_inversion_weights(WORD32 iid_lf_ls_idx,WORD32 iid_rf_rs_idx,WORD32 prediction_mode,WORD32 c1,WORD32 c2,WORD32 * weight1,WORD32 * weight2,ia_mps_dec_mps_tables_struct * ia_mps_dec_mps_table_ptr)43 VOID ixheaacd_get_matrix_inversion_weights(
44     WORD32 iid_lf_ls_idx, WORD32 iid_rf_rs_idx, WORD32 prediction_mode, WORD32 c1, WORD32 c2,
45     WORD32 *weight1, WORD32 *weight2, ia_mps_dec_mps_tables_struct *ia_mps_dec_mps_table_ptr) {
46   WORD32 temp, temp_1, temp_2;
47   WORD16 qtemp;
48   WORD32 w1 = ia_mps_dec_mps_table_ptr->m1_m2_table_ptr->cld_tab_2[iid_lf_ls_idx + 15];
49   WORD32 w2 = ia_mps_dec_mps_table_ptr->m1_m2_table_ptr->cld_tab_2[iid_rf_rs_idx + 15];
50 
51   if (prediction_mode == 1) {
52     if (abs(c1) >= ONE_IN_Q15) {
53       c1 = ONE_IN_Q15;
54     } else if ((c1 < MINUS_ONE_IN_Q14) && (c1 > MINUS_ONE_IN_Q15)) {
55       c1 = MINUS_ONE_IN_Q15 - (c1 << 1);
56     } else {
57       qtemp = 15;
58       temp = ixheaacd_mps_mult32(TWO_BY_THREE_Q15, c1, &qtemp, 15);
59       temp = ixheaacd_mps_convert_to_qn(temp, qtemp, 15);
60       c1 = ONE_BY_THREE_Q15 + temp;
61     }
62 
63     if (abs(c2) >= ONE_IN_Q15) {
64       c2 = ONE_IN_Q15;
65     } else if ((c2 < MINUS_ONE_IN_Q14) && (c2 > MINUS_ONE_IN_Q15)) {
66       c2 = MINUS_ONE_IN_Q15 - (c2 << 1);
67     } else {
68       qtemp = 15;
69       temp = ixheaacd_mps_mult32(TWO_BY_THREE_Q15, c2, &qtemp, 15);
70       temp = ixheaacd_mps_convert_to_qn(temp, qtemp, 15);
71       qtemp = 15;
72       temp = ixheaacd_mps_add32(temp, ONE_BY_THREE_Q15, &qtemp, 15);
73       c2 = ixheaacd_mps_convert_to_qn(temp, qtemp, 15);
74     }
75   } else {
76     WORD32 c1p, c2p;
77     WORD64 acc;
78     const WORD32 *cld_tab_3 = ia_mps_dec_mps_table_ptr->m1_m2_table_ptr->cld_tab_3;
79     const WORD32 *sqrt_tab = ia_mps_dec_mps_table_ptr->common_table_ptr->sqrt_tab;
80 
81     c1p = cld_tab_3[c1 + 15];
82     c2p = cld_tab_3[c2 + 15];
83 
84     acc = (WORD64)((WORD64)c1p * (WORD64)c2p);
85     acc >>= 15;
86     temp = (WORD32)acc;
87     temp_1 = (ONE_IN_Q15 + c2p) << 1;
88     acc += temp_1;
89     temp_2 = (WORD32)acc;
90 
91     temp = ixheaacd_mps_div_32(temp, temp_2, &qtemp);
92 
93     c1 = ixheaacd_mps_sqrt(temp, &qtemp, sqrt_tab);
94     c1 = ixheaacd_mps_convert_to_qn(c1, qtemp, 15);
95 
96     temp_2 = c1p + temp_1;
97     temp = ixheaacd_mps_div_32(c1p, temp_2, &qtemp);
98     c2 = ixheaacd_mps_sqrt(temp, &qtemp, sqrt_tab);
99     c2 = ixheaacd_mps_convert_to_qn(c2, qtemp, 15);
100   }
101   temp_1 = ONE_IN_Q15 + w1;
102   temp_2 = ixheaacd_mps_mult32_shr_15(c1, w1);
103   *weight1 = ixheaacd_mps_div32_in_q15(temp_2, temp_1);
104 
105   temp_1 = ONE_IN_Q15 + w2;
106   temp_2 = ixheaacd_mps_mult32_shr_15(c2, w2);
107   *weight2 = ixheaacd_mps_div32_in_q15(temp_2, temp_1);
108 }
109 
ixheaacd_invert_matrix(WORD32 weight1,WORD32 weight2,WORD32 h_real[][2],WORD32 h_imag[][2],const ia_mps_dec_common_tables_struct * common_tab_ptr)110 VOID ixheaacd_invert_matrix(WORD32 weight1, WORD32 weight2, WORD32 h_real[][2],
111                             WORD32 h_imag[][2],
112                             const ia_mps_dec_common_tables_struct *common_tab_ptr) {
113   WORD32 h11_f_real, h12_f_real, h21_f_real, h22_f_real;
114   WORD32 h11_f_imag, h12_f_imag, h21_f_imag, h22_f_imag;
115 
116   WORD32 inv_norm_real, inv_norm_imag, inv_norm;
117 
118   WORD32 len1, len2;
119   WORD16 q_len1 = 0, q_len2 = 0;
120 
121   WORD64 acc1, acc2;
122 
123   len1 = ixheaacd_mps_sqrt((ONE_IN_Q15 - (weight1 << 1) + ((weight1 * weight1) >> 14)), &q_len1,
124                            common_tab_ptr->sqrt_tab);
125 
126   len2 = ixheaacd_mps_sqrt((ONE_IN_Q15 - (weight2 << 1) + ((weight2 * weight2) >> 14)), &q_len2,
127                            common_tab_ptr->sqrt_tab);
128 
129   len1 = ixheaacd_mps_convert_to_qn(len1, q_len1, 15);
130   len2 = ixheaacd_mps_convert_to_qn(len2, q_len2, 15);
131 
132   h11_f_real = ixheaacd_mps_div32_in_q15((ONE_IN_Q15 - weight1), len1);
133 
134   h11_f_imag = ixheaacd_mps_div32_in_q15(weight1, len1);
135 
136   h22_f_imag = -(ixheaacd_mps_div32_in_q15(weight2, len2));
137 
138   h12_f_real = 0;
139 
140   h12_f_imag = ixheaacd_mps_mult32_shr_15(h22_f_imag, ONE_BY_SQRT_3_Q15);
141 
142   h21_f_real = 0;
143 
144   h21_f_imag = ixheaacd_mps_mult32_shr_15(h11_f_imag, -(ONE_BY_SQRT_3_Q15));
145 
146   h22_f_real = ixheaacd_mps_div32_in_q15((ONE_IN_Q15 - weight2), len2);
147 
148   acc1 =
149       (WORD64)((WORD64)h11_f_real * (WORD64)h22_f_real - (WORD64)h11_f_imag * (WORD64)h22_f_imag);
150   acc1 >>= 15;
151 
152   acc2 =
153       (WORD64)((WORD64)h12_f_real * (WORD64)h21_f_real - (WORD64)h12_f_imag * (WORD64)h21_f_imag);
154   acc2 >>= 15;
155   inv_norm_real = (WORD32)(acc1 - acc2);
156 
157   acc1 =
158       (WORD64)((WORD64)h11_f_real * (WORD64)h22_f_imag + (WORD64)h11_f_imag * (WORD64)h22_f_real);
159   acc1 >>= 15;
160 
161   acc2 =
162       (WORD64)((WORD64)h12_f_real * (WORD64)h21_f_imag + (WORD64)h12_f_imag * (WORD64)h21_f_real);
163   acc2 >>= 15;
164   inv_norm_imag = (WORD32)(acc1 + acc2);
165 
166   acc1 = (WORD64)((WORD64)inv_norm_real * (WORD64)inv_norm_real +
167                   (WORD64)inv_norm_imag * (WORD64)inv_norm_imag);
168   acc1 >>= 15;
169   inv_norm = (WORD32)acc1;
170 
171   inv_norm_real = ixheaacd_mps_div32_in_q15(inv_norm_real, inv_norm);
172   inv_norm_imag = -(ixheaacd_mps_div32_in_q15(inv_norm_imag, inv_norm));
173 
174   acc1 = (WORD64)((WORD64)h22_f_real * (WORD64)inv_norm_real -
175                   (WORD64)h22_f_imag * (WORD64)inv_norm_imag);
176   acc1 >>= 15;
177   h_real[0][0] = (WORD32)acc1;
178 
179   acc1 = (WORD64)((WORD64)h22_f_real * (WORD64)inv_norm_imag +
180                   (WORD64)h22_f_imag * (WORD64)inv_norm_real);
181   acc1 >>= 15;
182   h_imag[0][0] = (WORD32)acc1;
183 
184   acc1 = (WORD64)((WORD64)h12_f_imag * (WORD64)inv_norm_imag -
185                   (WORD64)h12_f_real * (WORD64)inv_norm_real);
186   acc1 >>= 15;
187   h_real[0][1] = (WORD32)acc1;
188 
189   acc1 = (WORD64)((WORD64)h12_f_real * (WORD64)inv_norm_imag +
190                   (WORD64)h12_f_imag * (WORD64)inv_norm_real);
191   acc1 = -(acc1 >> 15);
192   h_imag[0][1] = (WORD32)acc1;
193 
194   acc1 = (WORD64)((WORD64)h21_f_imag * (WORD64)inv_norm_imag -
195                   (WORD64)h21_f_real * (WORD64)inv_norm_real);
196   acc1 >>= 15;
197   h_real[1][0] = (WORD32)acc1;
198 
199   acc1 = (WORD64)((WORD64)h21_f_real * (WORD64)inv_norm_imag +
200                   (WORD64)h21_f_imag * (WORD64)inv_norm_real);
201   acc1 = -(acc1 >> 15);
202   h_imag[1][0] = (WORD32)acc1;
203 
204   acc1 = (WORD64)((WORD64)h11_f_real * (WORD64)inv_norm_real -
205                   (WORD64)h11_f_imag * (WORD64)inv_norm_imag);
206   acc1 >>= 15;
207   h_real[1][1] = (WORD32)acc1;
208 
209   acc1 = (WORD64)((WORD64)h11_f_real * (WORD64)inv_norm_imag +
210                   (WORD64)h11_f_imag * (WORD64)inv_norm_real);
211   acc1 >>= 15;
212   h_imag[1][1] = (WORD32)acc1;
213 }
214 
ixheaacd_dequant_icc_band(WORD32 iccband,WORD32 cldband)215 WORD32 ixheaacd_dequant_icc_band(WORD32 iccband, WORD32 cldband) {
216   if (iccband < 6) {
217     return iccband;
218   }
219   if (iccband == 6) {
220     if (cldband > 9 && cldband < 21) {
221       switch (cldband) {
222         case 10:
223         case 20:
224           return 10;
225         case 11:
226         case 19:
227           return 11;
228         case 12:
229         case 18:
230           return 12;
231         case 13:
232         case 17:
233           return 13;
234         case 14:
235         case 16:
236           return 14;
237         case 15:
238           return 15;
239         default:
240           return iccband;
241       }
242     } else {
243       return iccband;
244     }
245   }
246   if (7 == iccband) {
247     if (cldband > 7 && cldband < 23) {
248       switch (cldband) {
249         case 8:
250         case 22:
251           return 8;
252         case 9:
253         case 21:
254           return 9;
255         case 10:
256         case 20:
257           return 10;
258         case 11:
259         case 19:
260           return 11;
261         case 12:
262         case 18:
263           return 12;
264         case 13:
265         case 17:
266           return 13;
267         case 14:
268         case 16:
269           return 14;
270         case 15:
271           return 15;
272         default:
273           return iccband;
274       }
275     } else {
276       return iccband;
277     }
278   } else {
279     return iccband;
280   }
281 }
282 
ixheaacd_dequant_cld_band(WORD32 cld)283 WORD32 ixheaacd_dequant_cld_band(WORD32 cld) {
284   switch (cld) {
285     case -4915200:
286       return 0;
287     case -1474560:
288       return 1;
289     case -1310720:
290       return 2;
291     case -1146880:
292       return 3;
293     case -983040:
294       return 4;
295     case -819200:
296       return 5;
297     case -720896:
298       return 6;
299     case -622592:
300       return 7;
301     case -524288:
302       return 8;
303     case -425984:
304       return 9;
305     case -327680:
306       return 10;
307     case -262144:
308       return 11;
309     case -196608:
310       return 12;
311     case -131072:
312       return 13;
313     case -65536:
314       return 14;
315     case 0:
316       return 15;
317     case 65536:
318       return 16;
319     case 131072:
320       return 17;
321     case 196608:
322       return 18;
323     case 262144:
324       return 19;
325     case 327680:
326       return 20;
327     case 425984:
328       return 21;
329     case 524288:
330       return 22;
331     case 622592:
332       return 23;
333     case 720896:
334       return 24;
335     case 819200:
336       return 25;
337     case 983040:
338       return 26;
339     case 1146880:
340       return 27;
341     case 1310720:
342       return 28;
343     case 1474560:
344       return 29;
345     case 4915200:
346       return 30;
347     default:
348       return 0;
349   }
350 }
351 
ixheaacd_param_2_umx_ps_core_tables(WORD32 cld[MAX_PARAMETER_BANDS],WORD32 icc[MAX_PARAMETER_BANDS],WORD32 num_ott_bands,WORD32 res_bands,WORD32 h11[MAX_PARAMETER_BANDS],WORD32 h12[MAX_PARAMETER_BANDS],WORD32 h21[MAX_PARAMETER_BANDS],WORD32 h22[MAX_PARAMETER_BANDS],WORD32 h12_res[MAX_PARAMETER_BANDS],WORD32 h22_res[MAX_PARAMETER_BANDS],WORD16 c_l[MAX_PARAMETER_BANDS],WORD16 c_r[MAX_PARAMETER_BANDS],const ia_mps_dec_m1_m2_tables_struct * ixheaacd_mps_dec_m1_m2_tables)352 VOID ixheaacd_param_2_umx_ps_core_tables(
353     WORD32 cld[MAX_PARAMETER_BANDS], WORD32 icc[MAX_PARAMETER_BANDS], WORD32 num_ott_bands,
354     WORD32 res_bands, WORD32 h11[MAX_PARAMETER_BANDS], WORD32 h12[MAX_PARAMETER_BANDS],
355     WORD32 h21[MAX_PARAMETER_BANDS], WORD32 h22[MAX_PARAMETER_BANDS],
356     WORD32 h12_res[MAX_PARAMETER_BANDS], WORD32 h22_res[MAX_PARAMETER_BANDS],
357     WORD16 c_l[MAX_PARAMETER_BANDS], WORD16 c_r[MAX_PARAMETER_BANDS],
358     const ia_mps_dec_m1_m2_tables_struct *ixheaacd_mps_dec_m1_m2_tables) {
359   WORD32 band;
360   WORD32 quant_band_cld, quant_band_icc;
361 
362   for (band = 0; band < num_ott_bands; band++) {
363     quant_band_cld = ixheaacd_dequant_cld_band(cld[band]);
364 
365     c_l[band] = (WORD16)ixheaacd_mps_dec_m1_m2_tables->c_l_table[quant_band_cld];
366     c_r[band] = (WORD16)ixheaacd_mps_dec_m1_m2_tables->c_l_table[30 - quant_band_cld];
367   }
368 
369   for (band = 0; band < num_ott_bands; band++) {
370     if (band < res_bands) {
371       quant_band_cld = ixheaacd_dequant_cld_band(cld[band]);
372       quant_band_icc = ixheaacd_dequant_icc_band(icc[band], quant_band_cld);
373 
374       h11[band] = ixheaacd_mps_dec_m1_m2_tables->cos_table[quant_band_icc][quant_band_cld];
375       h11[band] = ixheaacd_mps_mult32_shr_15(h11[band], c_l[band]);
376       h21[band] = ixheaacd_mps_dec_m1_m2_tables->cos_table[quant_band_icc][30 - quant_band_cld];
377       h21[band] = ixheaacd_mps_mult32_shr_15(h21[band], c_r[band]);
378 
379       h12[band] = 0;
380       h22[band] = 0;
381       h12_res[band] = ONE_IN_Q15;
382       h22_res[band] = MINUS_ONE_IN_Q15;
383     } else {
384       quant_band_cld = ixheaacd_dequant_cld_band(cld[band]);
385       if (quant_band_cld < 0 || quant_band_cld >= 31) {
386         quant_band_cld = 30;
387       }
388 
389       quant_band_icc = icc[band];
390 
391       if (quant_band_icc < 0 || quant_band_icc >= 8) {
392         quant_band_icc = 7;
393       }
394       h11[band] = ixheaacd_mps_dec_m1_m2_tables->cos_table[quant_band_icc][quant_band_cld];
395       h11[band] = ixheaacd_mps_mult32_shr_15(h11[band], c_l[band]);
396       h21[band] = ixheaacd_mps_dec_m1_m2_tables->cos_table[quant_band_icc][30 - quant_band_cld];
397       h21[band] = ixheaacd_mps_mult32_shr_15(h21[band], c_r[band]);
398       h12[band] = ixheaacd_mps_dec_m1_m2_tables->sin_table[quant_band_icc][quant_band_cld];
399       h12[band] = ixheaacd_mps_mult32_shr_15(h12[band], c_l[band]);
400       h22[band] = -ixheaacd_mps_dec_m1_m2_tables->sin_table[quant_band_icc][30 - quant_band_cld];
401       h22[band] = ixheaacd_mps_mult32_shr_15(h22[band], c_r[band]);
402 
403       h12_res[band] = 0;
404       h22_res[band] = 0;
405     }
406   }
407   return;
408 }
409 
ixheaacd_param_2_umx_ps(ia_heaac_mps_state_struct * pstr_mps_state,WORD32 * h11,WORD32 * h12,WORD32 * h21,WORD32 * h22,WORD32 * h12_res,WORD32 * h22_res,WORD16 * c_l,WORD16 * c_r,WORD32 ott_box_indx,WORD32 parameter_set_indx,WORD32 res_bands)410 VOID ixheaacd_param_2_umx_ps(ia_heaac_mps_state_struct *pstr_mps_state, WORD32 *h11, WORD32 *h12,
411                              WORD32 *h21, WORD32 *h22, WORD32 *h12_res, WORD32 *h22_res,
412                              WORD16 *c_l, WORD16 *c_r, WORD32 ott_box_indx,
413                              WORD32 parameter_set_indx, WORD32 res_bands) {
414   WORD32 band;
415   ia_mps_dec_spatial_bs_frame_struct *p_cur_bs = pstr_mps_state->bs_frame;
416   ia_mps_dec_auxilary_struct *p_aux_struct = pstr_mps_state->aux_struct;
417   WORD32 num_parameter_bands = pstr_mps_state->num_parameter_bands;
418 
419   ixheaacd_param_2_umx_ps_core_tables(p_aux_struct->ott_cld[ott_box_indx][parameter_set_indx],
420                                       p_cur_bs->ott_icc_idx[ott_box_indx][parameter_set_indx],
421                                       p_aux_struct->num_ott_bands[ott_box_indx], res_bands, h11,
422                                       h12, h21, h22, h12_res, h22_res, c_l, c_r,
423                                       pstr_mps_state->ia_mps_dec_mps_table.m1_m2_table_ptr);
424 
425   for (band = p_aux_struct->num_ott_bands[ott_box_indx]; band < num_parameter_bands; band++) {
426     h11[band] = h21[band] = h12[band] = h22[band] = h12_res[band] = h22_res[band] = 0;
427   }
428   return;
429 }
430 
ixheaacd_dequant_one_by_icc(WORD32 icc)431 static WORD32 ixheaacd_dequant_one_by_icc(WORD32 icc) {
432   switch (icc) {
433     case 32768:
434       return 32768;
435     case 30704:
436       return 34971;
437     case 27564:
438       return 38955;
439     case 19691:
440       return 54530;
441     case 12047:
442       return 89131;
443     case 0:
444       return 0;
445     case -19300:
446       return -55633;
447     case -32440:
448       return -33099;
449     default:
450       return 0;
451   }
452 }
453 
ixheaacd_map_cld_index(WORD32 cld_val)454 static WORD16 ixheaacd_map_cld_index(WORD32 cld_val) {
455   WORD32 temp = cld_val;
456   WORD16 idx = 0;
457   if (cld_val == 0) {
458     return 15;
459   } else {
460     if (cld_val < 0) {
461       temp = -cld_val;
462     }
463     switch (temp) {
464       case 150:
465         idx = 15;
466         break;
467       case 45:
468         idx = 14;
469         break;
470       case 40:
471         idx = 13;
472         break;
473       case 35:
474         idx = 12;
475         break;
476       case 30:
477         idx = 11;
478         break;
479       case 25:
480         idx = 10;
481         break;
482       case 22:
483         idx = 9;
484         break;
485       case 19:
486         idx = 8;
487         break;
488       case 16:
489         idx = 7;
490         break;
491       case 13:
492         idx = 6;
493         break;
494       case 10:
495         idx = 5;
496         break;
497       case 8:
498         idx = 4;
499         break;
500       case 6:
501         idx = 3;
502         break;
503       case 4:
504         idx = 2;
505         break;
506       case 2:
507         idx = 1;
508         break;
509       default:
510         idx = 0;
511         break;
512     }
513   }
514 
515   return (cld_val >= 0) ? idx + 15 : 15 - idx;
516 }
517 
ixheaacd_calculate_ttt(ia_heaac_mps_state_struct * pstr_mps_state,WORD32 ps,WORD32 pb,WORD32 ttt_mode,WORD32 m_ttt[][3])518 VOID ixheaacd_calculate_ttt(ia_heaac_mps_state_struct *pstr_mps_state, WORD32 ps, WORD32 pb,
519                             WORD32 ttt_mode, WORD32 m_ttt[][3]) {
520   ia_mps_dec_auxilary_struct *p_aux_struct = pstr_mps_state->aux_struct;
521   WORD32 col;
522 
523   if (ttt_mode < 2) {
524     m_ttt[0][0] = (p_aux_struct->ttt_cpc_1[0][ps][pb] + ONE_IN_Q16);
525     m_ttt[0][1] = (p_aux_struct->ttt_cpc_2[0][ps][pb] - ONE_IN_Q15);
526     m_ttt[1][0] = (p_aux_struct->ttt_cpc_1[0][ps][pb] - ONE_IN_Q15);
527     m_ttt[1][1] = (p_aux_struct->ttt_cpc_2[0][ps][pb] + ONE_IN_Q16);
528     m_ttt[2][0] = (ONE_IN_Q15 - p_aux_struct->ttt_cpc_1[0][ps][pb]);
529     m_ttt[2][1] = (ONE_IN_Q15 - p_aux_struct->ttt_cpc_2[0][ps][pb]);
530 
531     if (pb >= pstr_mps_state->res_bands[3]) {
532       WORD32 one_by_icc;
533       one_by_icc = ixheaacd_dequant_one_by_icc(p_aux_struct->ttt_icc[0][ps][pb]);
534 
535       m_ttt[0][0] = ixheaacd_mps_mult32_shr_15(m_ttt[0][0], one_by_icc);
536       m_ttt[0][1] = ixheaacd_mps_mult32_shr_15(m_ttt[0][1], one_by_icc);
537       m_ttt[1][0] = ixheaacd_mps_mult32_shr_15(m_ttt[1][0], one_by_icc);
538       m_ttt[1][1] = ixheaacd_mps_mult32_shr_15(m_ttt[1][1], one_by_icc);
539       m_ttt[2][0] = ixheaacd_mps_mult32_shr_15(m_ttt[2][0], one_by_icc);
540       m_ttt[2][1] = ixheaacd_mps_mult32_shr_15(m_ttt[2][1], one_by_icc);
541     }
542 
543     m_ttt[0][0] = ixheaacd_mult32x16in32(m_ttt[0][0], TWO_BY_THREE_Q15);
544     m_ttt[0][1] = ixheaacd_mult32x16in32(m_ttt[0][1], TWO_BY_THREE_Q15);
545     m_ttt[1][0] = ixheaacd_mult32x16in32(m_ttt[1][0], TWO_BY_THREE_Q15);
546     m_ttt[1][1] = ixheaacd_mult32x16in32(m_ttt[1][1], TWO_BY_THREE_Q15);
547     m_ttt[2][0] = ixheaacd_mult32x16in32(m_ttt[2][0], TWO_BY_THREE_Q15);
548     m_ttt[2][1] = ixheaacd_mult32x16in32(m_ttt[2][1], TWO_BY_THREE_Q15);
549   } else {
550     WORD32 center_wiener;
551     WORD32 center_subtraction;
552     WORD32 c1d, c2d;
553     WORD64 prod;
554     WORD32 w11, w00, w20, w21;
555     WORD16 q_w11, q_w00, q_w20, q_w21;
556 
557     const WORD32 *ten_cld_by_10 =
558         pstr_mps_state->ia_mps_dec_mps_table.m1_m2_table_ptr->ten_cld_by_10;
559 
560     ia_mps_dec_spatial_bs_frame_struct *p_cur_bs = pstr_mps_state->bs_frame;
561     WORD16 index = ixheaacd_map_cld_index(p_aux_struct->ttt_cld_1[0][ps][pb] >> 15);
562 
563     c1d = ten_cld_by_10[index];
564 
565     index = ixheaacd_map_cld_index(p_aux_struct->ttt_cld_2[0][ps][pb] >> 15);
566     c2d = ten_cld_by_10[index];
567 
568     if (p_cur_bs->cmp_ttt_cld_1_idx[0][ps][pb] == 15 ||
569         p_cur_bs->cmp_ttt_cld_2_idx[0][ps][pb] == 15) {
570       if (p_cur_bs->cmp_ttt_cld_1_idx[0][ps][pb] == 15) {
571         if (p_cur_bs->cmp_ttt_cld_2_idx[0][ps][pb] == -15) {
572           w00 = ONE_BY_SQRT_2_Q15;
573           w20 = ONE_BY_SQRT_8_Q15;
574         } else {
575           w00 = ONE_IN_Q15;
576           w20 = 0;
577         }
578 
579         if (p_cur_bs->cmp_ttt_cld_2_idx[0][ps][pb] == 15) {
580           w11 = ONE_BY_SQRT_2_Q15;
581           w21 = ONE_BY_SQRT_8_Q15;
582         } else {
583           w11 = ONE_IN_Q15;
584           w21 = 0;
585         }
586 
587         m_ttt[0][0] = w00;
588         m_ttt[2][0] = w20;
589         m_ttt[2][1] = w21;
590         m_ttt[1][1] = w11;
591       }
592 
593       if (p_cur_bs->cmp_ttt_cld_2_idx[0][ps][pb] == 15) {
594         const WORD32 *w00_cld2_15 =
595             pstr_mps_state->ia_mps_dec_mps_table.m1_m2_table_ptr->w00_cld2_15;
596 
597         if (p_cur_bs->cmp_ttt_cld_1_idx[0][ps][pb] == 15) {
598           w11 = ONE_BY_SQRT_2_Q15;
599           w21 = ONE_BY_SQRT_8_Q15;
600         } else {
601           w11 = 0;
602           w21 = ONE_IN_Q14;
603         }
604 
605         w00 = w00_cld2_15[p_cur_bs->cmp_ttt_cld_1_idx[0][ps][pb] + 15];
606         w20 = w00_cld2_15[15 - p_cur_bs->cmp_ttt_cld_1_idx[0][ps][pb]] / 2;
607         m_ttt[0][0] = w00;
608         m_ttt[2][0] = w20;
609         m_ttt[2][1] = w21;
610         m_ttt[1][1] = w11;
611       }
612 
613       m_ttt[0][1] = 0;
614       m_ttt[1][0] = 0;
615     } else {
616       WORD32 temporary;
617       const WORD32 *sqrt_tab = pstr_mps_state->ia_mps_dec_mps_table.common_table_ptr->sqrt_tab;
618       prod = ixheaacd_mps_mult32_shr_15(c1d, c2d);
619 
620       temporary = ixheaacd_add32_sat(ONE_IN_Q15, c2d);
621       temporary = ixheaacd_add32_sat(temporary, (WORD32)prod);
622       w00 = ixheaacd_mps_div_32((WORD32)prod, temporary, &q_w00);
623 
624       w11 = ixheaacd_mps_div_32(c1d, (ixheaacd_add32_sat3(c1d, c2d, ONE_IN_Q15)), &q_w11);
625 
626       w20 = ixheaacd_mps_div_32((ixheaacd_add32_sat(c2d, ONE_IN_Q15)),
627                                 ixheaacd_add32_sat3(ONE_IN_Q15, (WORD32)prod, c2d), &q_w20);
628 
629       w21 = ixheaacd_mps_div_32(ixheaacd_add32_sat(c2d, ONE_IN_Q15),
630                                 (ixheaacd_add32_sat3(c1d, c2d, ONE_IN_Q15)), &q_w21);
631 
632       m_ttt[0][0] = ixheaacd_mps_sqrt(w00, &q_w00, sqrt_tab);
633       m_ttt[0][0] = ixheaacd_mps_convert_to_qn(m_ttt[0][0], q_w00, 15);
634 
635       m_ttt[0][1] = 0;
636       m_ttt[1][0] = 0;
637 
638       m_ttt[1][1] = ixheaacd_mps_sqrt(w11, &q_w11, sqrt_tab);
639       m_ttt[1][1] = ixheaacd_mps_convert_to_qn(m_ttt[1][1], q_w11, 15);
640 
641       m_ttt[2][0] = ixheaacd_mps_sqrt(w20, &q_w20, sqrt_tab) >> 1;
642 
643       m_ttt[2][0] = ixheaacd_mps_convert_to_qn(m_ttt[2][0], q_w20, 15);
644 
645       m_ttt[2][1] = ixheaacd_mps_sqrt(w21, &q_w21, sqrt_tab) >> 1;
646 
647       m_ttt[2][1] = ixheaacd_mps_convert_to_qn(m_ttt[2][1], q_w21, 15);
648       if (p_aux_struct->ttt_cld_1[0][ps][pb] == 4915200) {
649         m_ttt[0][0] = 32767;
650         m_ttt[1][1] = 32767;
651         m_ttt[2][0] = 0;
652         m_ttt[2][1] = 0;
653       }
654     }
655 
656     center_wiener = 0;
657     center_subtraction = (ttt_mode == 2 || ttt_mode == 3);
658 
659     if (center_wiener) {
660       WORD32 cld_1_idx = p_cur_bs->cmp_ttt_cld_1_idx[0][ps][pb];
661       WORD32 cld_2_idx = p_cur_bs->cmp_ttt_cld_2_idx[0][ps][pb];
662 
663       if (cld_1_idx == 15 && cld_2_idx == 15) {
664         m_ttt[2][0] = 0;
665         m_ttt[2][1] = ONE_BY_SQRT_2_Q15;
666       } else if (cld_1_idx == 15) {
667         if (cld_2_idx == -15)
668           m_ttt[2][0] = ONE_BY_SQRT_2_Q15;
669         else
670           m_ttt[2][0] = 0;
671         m_ttt[2][1] = 0;
672       } else if (cld_2_idx == 15) {
673         m_ttt[2][0] = 0;
674         m_ttt[2][1] = ONE_IN_Q15;
675       } else {
676         WORD32 temp;
677         WORD16 q_temp;
678         const WORD32 *sqrt_tab = pstr_mps_state->ia_mps_dec_mps_table.common_table_ptr->sqrt_tab;
679 
680         prod = ixheaacd_mps_mult32_shr_15(c2d, (c2d + c1d + ONE_IN_Q16)) + ONE_IN_Q15;
681 
682         temp = ixheaacd_mps_div_32((WORD32)ONE_IN_Q15, (WORD32)prod, &q_temp);
683 
684         m_ttt[2][0] = ixheaacd_mps_sqrt(temp, &q_temp, sqrt_tab);
685         m_ttt[2][0] = ixheaacd_mps_convert_to_qn(m_ttt[2][0], q_temp, 15);
686 
687         m_ttt[2][1] = ixheaacd_mps_mult32_shr_15(c2d, m_ttt[2][0]);
688       }
689     }
690 
691     if (center_subtraction) {
692       WORD32 wl1, wl2, wr1, wr2;
693       WORD16 q_wl1, q_wr1;
694       WORD32 cld_1_idx = p_cur_bs->cmp_ttt_cld_1_idx[0][ps][pb];
695       WORD32 cld_2_idx = p_cur_bs->cmp_ttt_cld_2_idx[0][ps][pb];
696 
697       if (cld_1_idx == 15 && cld_2_idx == 15) {
698         m_ttt[0][0] = ONE_IN_Q15;
699         m_ttt[0][1] = MINUS_ONE_IN_Q14;
700         m_ttt[1][1] = ONE_BY_SQRT_2_Q15;
701         m_ttt[1][0] = 0;
702       } else if (cld_1_idx == 15) {
703         if (cld_2_idx == -15) {
704           m_ttt[0][0] = ONE_BY_SQRT_2_Q15;
705           m_ttt[1][0] = MINUS_ONE_IN_Q14;
706         } else {
707           m_ttt[0][0] = ONE_IN_Q15;
708           m_ttt[1][0] = 0;
709         }
710 
711         m_ttt[0][1] = 0;
712         m_ttt[1][1] = ONE_IN_Q15;
713       } else if (cld_2_idx == 15) {
714         m_ttt[0][0] = ONE_IN_Q15;
715         m_ttt[0][1] = MINUS_ONE_IN_Q15;
716         m_ttt[1][1] = 0;
717         m_ttt[1][0] = 0;
718       } else {
719         WORD32 temp, temp_1, q_a;
720         WORD16 q_c, q_l, q_r, q_temp, q_temp1;
721         WORD32 c;
722         WORD32 r;
723         WORD32 l;
724         const WORD32 *sqrt_tab = pstr_mps_state->ia_mps_dec_mps_table.common_table_ptr->sqrt_tab;
725 
726         c = ixheaacd_mps_div_32(ONE_IN_Q15, (ixheaacd_add32_sat(c1d, ONE_IN_Q15)), &q_c);
727         r = ixheaacd_mps_div_32(c1d, (ixheaacd_add32_sat(c2d, ONE_IN_Q15)), &q_r);
728         r = ixheaacd_mps_mult32_shr_30(r, c);
729         q_r = q_r + q_c - 30;
730 
731         l = ixheaacd_mps_mult32_shr_30(c2d, r);
732         q_l = q_r - 15;
733 
734         temp = ixheaacd_mps_div_32(r, l, &q_temp);
735         q_temp += (q_r - q_l);
736 
737         if (q_temp > 28) {
738           temp = temp >> (q_temp - 28);
739           q_temp = 28;
740         }
741 
742         temp += ((1 << q_temp) - 1);
743 
744         temp = ixheaacd_add32_sat(
745             ixheaacd_mps_mult32_shr_n(c, temp, (WORD16)(q_c + q_temp - q_r)), r);
746         q_temp = q_r;
747 
748         if (q_c > q_r) {
749           temp_1 = r + (c >> (q_c - q_r));
750           q_temp1 = q_r;
751         } else {
752           temp_1 = (r >> (q_r - q_c)) + c;
753           q_temp1 = q_c;
754         }
755 
756         temp = ixheaacd_div32(temp_1, temp, &q_a);
757         q_wl1 = q_a + q_temp1 - q_temp;
758         wl1 = ixheaacd_mps_sqrt(temp, &q_wl1, sqrt_tab);
759         m_ttt[0][0] = ixheaacd_mps_convert_to_qn(wl1, q_wl1, 15);
760 
761         temp = ixheaacd_div32(wl1, temp_1, &q_a);
762         q_temp = q_a + (q_wl1 - q_temp1);
763         wl2 = ixheaacd_mps_mult32_shr_n(c, temp, (WORD16)(q_c + q_temp - 15));
764         m_ttt[0][1] = ixheaacd_negate32_sat(wl2);
765 
766         temp = ixheaacd_mps_div_32(l, r, &q_temp);
767         q_temp += (q_l - q_r);
768 
769         if (q_temp > 28) {
770           temp = temp >> (q_temp - 28);
771           q_temp = 28;
772         }
773 
774         temp = ixheaacd_add32_sat((1 << q_temp) - 1, temp);
775 
776         temp = ixheaacd_add32_sat(
777                    ixheaacd_mps_mult32_shr_n(c, temp, (WORD16)(q_c + q_temp - q_l)), l);
778 
779         q_temp = q_l;
780 
781         if (q_c > q_l) {
782           temp_1 = l + (c >> (q_c - q_l));
783           q_temp1 = q_l;
784         } else {
785           temp_1 = (l >> (q_l - q_c)) + c;
786           q_temp1 = q_c;
787         }
788 
789         temp = ixheaacd_div32(temp_1, temp, &q_a);
790         q_wr1 = q_a + q_temp1 - q_temp;
791         wr1 = ixheaacd_mps_sqrt(temp, &q_wr1, sqrt_tab);
792         m_ttt[1][1] = ixheaacd_mps_convert_to_qn(wr1, q_wr1, 15);
793 
794         temp = ixheaacd_div32(wr1, temp_1, &q_a);
795         q_temp = q_a + (q_wl1 - q_temp1);
796         wr2 = ixheaacd_mps_mult32_shr_n(c, temp, (WORD16)(q_c + q_temp - 15));
797         m_ttt[1][0] = ixheaacd_negate32_sat(wr2);
798       }
799     }
800   }
801 
802   m_ttt[0][2] = ONE_BY_THREE_Q15;
803   m_ttt[1][2] = ONE_BY_THREE_Q15;
804   m_ttt[2][2] = MINUS_ONE_BY_THREE_Q15;
805 
806   for (col = 0; col < 3; col++) {
807     m_ttt[2][col] = ixheaacd_mps_mult32_shr_15(m_ttt[2][col], SQRT_TWO_Q15);
808   }
809 }
810 
ixheaacd_calculate_mtx_inv(ia_heaac_mps_state_struct * pstr_mps_state,WORD32 ps,WORD32 pb,WORD32 mode,WORD32 h_real[][2],WORD32 h_imag[][2])811 VOID ixheaacd_calculate_mtx_inv(ia_heaac_mps_state_struct *pstr_mps_state, WORD32 ps, WORD32 pb,
812                                 WORD32 mode, WORD32 h_real[][2], WORD32 h_imag[][2]) {
813   ia_mps_dec_auxilary_struct *p_aux_struct = pstr_mps_state->aux_struct;
814 
815   WORD32 weight1;
816   WORD32 weight2;
817   ia_mps_dec_spatial_bs_frame_struct *p_cur_bs = pstr_mps_state->bs_frame;
818 
819   if (mode < 2) {
820     ixheaacd_get_matrix_inversion_weights(
821         p_cur_bs->ott_cld_idx[1][ps][pb], p_cur_bs->ott_cld_idx[2][ps][pb], 1,
822         p_aux_struct->ttt_cpc_1[0][ps][pb], p_aux_struct->ttt_cpc_2[0][ps][pb], &weight1,
823         &weight2, &(pstr_mps_state->ia_mps_dec_mps_table));
824   } else {
825     ixheaacd_get_matrix_inversion_weights(
826         p_cur_bs->ott_cld_idx[1][ps][pb], p_cur_bs->ott_cld_idx[2][ps][pb], 0,
827         p_cur_bs->cmp_ttt_cld_1_idx[0][ps][pb], p_cur_bs->cmp_ttt_cld_2_idx[0][ps][pb], &weight1,
828         &weight2, &(pstr_mps_state->ia_mps_dec_mps_table));
829   }
830 
831   ixheaacd_invert_matrix(weight1, weight2, h_real, h_imag,
832                          pstr_mps_state->ia_mps_dec_mps_table.common_table_ptr);
833 }
834 
ixheaacd_calculate_arb_dmx_mtx(ia_heaac_mps_state_struct * pstr_mps_state,WORD32 ps,WORD32 pb,WORD32 g_real[])835 VOID ixheaacd_calculate_arb_dmx_mtx(ia_heaac_mps_state_struct *pstr_mps_state, WORD32 ps,
836                                     WORD32 pb, WORD32 g_real[]) {
837   WORD32 ch;
838   WORD32 gain;
839 
840   WORD32 *arbdmx_alpha_prev = pstr_mps_state->mps_persistent_mem.arbdmx_alpha_prev;
841   WORD32 *arbdmx_alpha_upd_set = pstr_mps_state->aux_struct->arbdmx_alpha_upd_set;
842   WORD32 *arbdmx_alpha = pstr_mps_state->aux_struct->arbdmx_alpha;
843 
844   WORD32 n_ch_in = pstr_mps_state->num_input_channels;
845   WORD32 temp_1;
846   for (ch = 0; ch < n_ch_in; ch++) {
847     temp_1 = pstr_mps_state->bs_frame->arbdmx_gain_idx[ch][ps][pb] + 15;
848     gain = pstr_mps_state->ia_mps_dec_mps_table.m1_m2_table_ptr->dec_pow[temp_1];
849 
850     if (pb < pstr_mps_state->arbdmx_residual_bands) {
851       if ((ps == 0) && (arbdmx_alpha_upd_set[ch] == 1)) {
852         g_real[ch] = ixheaacd_mps_mult32_shr_15(*arbdmx_alpha_prev, gain);
853       } else {
854         g_real[ch] = ixheaacd_mps_mult32_shr_15(arbdmx_alpha[ch], gain);
855       }
856     } else {
857       g_real[ch] = gain;
858     }
859     arbdmx_alpha_prev++;
860   }
861 }
862 
ixheaacd_quantize(WORD32 cld)863 WORD32 ixheaacd_quantize(WORD32 cld) {
864   switch (cld) {
865     case -150:
866       return -15;
867     case -45:
868       return -14;
869     case -40:
870       return -13;
871     case -35:
872       return -12;
873     case -30:
874       return -11;
875     case -25:
876       return -10;
877     case -22:
878       return -9;
879     case -19:
880       return -8;
881     case -16:
882       return -7;
883     case -13:
884       return -6;
885     case -10:
886       return -5;
887     case -8:
888       return -4;
889     case -6:
890       return -3;
891     case -4:
892       return -2;
893     case -2:
894       return -1;
895     case 0:
896       return 0;
897     case 2:
898       return 1;
899     case 4:
900       return 2;
901     case 6:
902       return 3;
903     case 8:
904       return 4;
905     case 10:
906       return 5;
907     case 13:
908       return 6;
909     case 16:
910       return 7;
911     case 19:
912       return 8;
913     case 22:
914       return 9;
915     case 25:
916       return 10;
917     case 30:
918       return 11;
919     case 35:
920       return 12;
921     case 40:
922       return 13;
923     case 45:
924       return 14;
925     case 150:
926       return 15;
927     default:
928       return 0;
929   }
930 }
931