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 <string.h>
21 #include "ixheaac_type_def.h"
22 #include "ixheaacd_mps_struct_def.h"
23 #include "ixheaacd_mps_res_rom.h"
24 #include "ixheaacd_mps_aac_struct.h"
25 #include "ixheaac_constants.h"
26 #include "ixheaac_basic_ops32.h"
27 #include "ixheaac_basic_ops40.h"
28 #include "ixheaacd_bitbuffer.h"
29 #include "ixheaacd_common_rom.h"
30 #include "ixheaacd_sbrdecsettings.h"
31 #include "ixheaacd_sbr_scale.h"
32 #include "ixheaacd_env_extr_part.h"
33 #include "ixheaacd_sbr_rom.h"
34 #include "ixheaacd_hybrid.h"
35 #include "ixheaacd_ps_dec.h"
36 #include "ixheaac_error_standards.h"
37 #include "ixheaacd_mps_polyphase.h"
38 #include "ixheaacd_config.h"
39 #include "ixheaacd_qmf_dec.h"
40 #include "ixheaacd_mps_dec.h"
41 #include "ixheaacd_mps_macro_def.h"
42 #include "ixheaacd_mps_basic_op.h"
43 #include "ixheaacd_mps_calc_m1m2_common.h"
44 #include "ixheaacd_mps_bitdec.h"
45
46 #undef ABS_THR
47 #define ABS_THR 1
48
ixheaacd_calc_m1m2_51s1(ia_heaac_mps_state_struct * pstr_mps_state)49 VOID ixheaacd_calc_m1m2_51s1(ia_heaac_mps_state_struct *pstr_mps_state) {
50 ia_mps_dec_auxilary_struct *p_aux_struct = pstr_mps_state->aux_struct;
51 ia_mps_dec_m2_param_struct *m2_param = p_aux_struct->m2_param;
52 ia_mps_dec_m1_param_struct *m1_param = pstr_mps_state->array_struct->m1_param;
53 WORD32 ps, pb;
54
55 WORD32 *iid;
56 WORD32 *icc;
57 WORD32 *h11;
58 WORD32 *h12;
59 WORD32 *h21;
60 WORD32 *h22;
61 WORD32 *h12_res;
62 WORD32 *h22_res;
63 WORD16 *c_l;
64 WORD16 *c_r;
65
66 const WORD32 *sqrt_tab = pstr_mps_state->ia_mps_dec_mps_table.common_table_ptr->sqrt_tab;
67 WORD32 num_parameter_sets = pstr_mps_state->num_parameter_sets;
68 WORD32 num_parameter_bands = pstr_mps_state->num_parameter_bands;
69 iid = pstr_mps_state->mps_scratch_mem_v;
70 icc = iid + IXHEAAC_GET_SIZE_ALIGNED_TYPE(MAX_PARAMETER_BANDS, sizeof(*icc), BYTE_ALIGN_8);
71 h11 = icc + IXHEAAC_GET_SIZE_ALIGNED_TYPE(MAX_PARAMETER_BANDS, sizeof(*h11), BYTE_ALIGN_8);
72 h12 = h11 + IXHEAAC_GET_SIZE_ALIGNED_TYPE(MAX_PARAMETER_BANDS, sizeof(*h12), BYTE_ALIGN_8);
73 h21 = h12 + IXHEAAC_GET_SIZE_ALIGNED_TYPE(MAX_PARAMETER_BANDS, sizeof(*h21), BYTE_ALIGN_8);
74 h22 = h21 + IXHEAAC_GET_SIZE_ALIGNED_TYPE(MAX_PARAMETER_BANDS, sizeof(*h22), BYTE_ALIGN_8);
75 h12_res =
76 h22 + IXHEAAC_GET_SIZE_ALIGNED_TYPE(MAX_PARAMETER_BANDS, sizeof(*h12_res), BYTE_ALIGN_8);
77 h22_res = h12_res +
78 IXHEAAC_GET_SIZE_ALIGNED_TYPE(MAX_PARAMETER_BANDS, sizeof(*h22_res), BYTE_ALIGN_8);
79 c_l = (WORD16 *)pstr_mps_state->mps_scratch_mem_v +
80 IXHEAAC_GET_SIZE_ALIGNED_TYPE(PARAMETER_BANDSX16, sizeof(*c_l), BYTE_ALIGN_8);
81 c_r = c_l + IXHEAAC_GET_SIZE_ALIGNED_TYPE(MAX_PARAMETER_BANDS, sizeof(*c_r), BYTE_ALIGN_8);
82
83 for (ps = 0; ps < num_parameter_sets; ps++) {
84 for (pb = 0; pb < num_parameter_bands; pb++) {
85 WORD32 p_l_fs, p_r_fs;
86 WORD32 p_l_c, p_r_c;
87 WORD32 p_l_s, p_r_s;
88 WORD32 p_l_f, p_r_f;
89 WORD32 left_f;
90 WORD32 right_f;
91 WORD32 center;
92 WORD32 left_s;
93 WORD32 right_s;
94 WORD32 left;
95 WORD32 right;
96 WORD32 cross;
97 WORD32 temp_1, temp_2;
98 WORD16 qtemp1, qtemp2;
99 ia_mps_dec_spatial_bs_frame_struct *p_cur_bs = pstr_mps_state->bs_frame;
100
101 p_l_fs = pstr_mps_state->ia_mps_dec_mps_table.m1_m2_table_ptr
102 ->cld_tab_1[p_cur_bs->ott_cld_idx[0][ps][pb] + 15];
103 p_r_fs = pstr_mps_state->ia_mps_dec_mps_table.m1_m2_table_ptr
104 ->cld_tab_1[15 - p_cur_bs->ott_cld_idx[0][ps][pb]];
105
106 p_l_c = pstr_mps_state->ia_mps_dec_mps_table.m1_m2_table_ptr
107 ->cld_tab_1[p_cur_bs->ott_cld_idx[1][ps][pb] + 15];
108 p_r_c = pstr_mps_state->ia_mps_dec_mps_table.m1_m2_table_ptr
109 ->cld_tab_1[15 - p_cur_bs->ott_cld_idx[1][ps][pb]];
110
111 p_l_s = pstr_mps_state->ia_mps_dec_mps_table.m1_m2_table_ptr
112 ->cld_tab_1[p_cur_bs->ott_cld_idx[2][ps][pb] + 15];
113 p_r_s = pstr_mps_state->ia_mps_dec_mps_table.m1_m2_table_ptr
114 ->cld_tab_1[15 - p_cur_bs->ott_cld_idx[2][ps][pb]];
115
116 p_l_f = pstr_mps_state->ia_mps_dec_mps_table.m1_m2_table_ptr
117 ->cld_tab_1[p_cur_bs->ott_cld_idx[3][ps][pb] + 15];
118 p_r_f = pstr_mps_state->ia_mps_dec_mps_table.m1_m2_table_ptr
119 ->cld_tab_1[15 - p_cur_bs->ott_cld_idx[3][ps][pb]];
120
121 left_f = ixheaacd_mps_mult32_shr_15(ixheaacd_mps_mult32_shr_15(p_l_fs, p_l_c), p_l_f);
122 right_f = ixheaacd_mps_mult32_shr_15(ixheaacd_mps_mult32_shr_15(p_l_fs, p_l_c), p_r_f);
123 center = ixheaacd_mps_mult32_shr_16(p_l_fs, p_r_c);
124 left_s = ixheaacd_mps_mult32_shr_15(p_r_fs, p_l_s);
125 right_s = ixheaacd_mps_mult32_shr_15(p_r_fs, p_r_s);
126
127 left = center + left_f + left_s;
128 right = center + right_f + right_s;
129
130 temp_1 = ixheaacd_mps_mult32_shr_15(left_f, right_f);
131 temp_1 = ixheaacd_mps_mult32_shr_15(temp_1, p_aux_struct->ott_icc[3][ps][pb]);
132
133 temp_2 = ixheaacd_mps_mult32_shr_15(left_f, right_f);
134 temp_2 = ixheaacd_mps_mult32_shr_15(temp_2, p_aux_struct->ott_icc[3][ps][pb]);
135
136 temp_1 = ixheaac_add32_sat(temp_1, temp_2);
137 cross = ixheaac_add32_sat(center, temp_1);
138
139 temp_1 = ixheaacd_mps_div32_in_q15(left, right);
140 qtemp1 = 15;
141
142 iid[pb] = (10 * ixheaacd_mps_log10(temp_1, qtemp1)) >> 1;
143
144 temp_1 = ixheaacd_mps_mult32_shr_15(left, right);
145 qtemp1 = 15;
146 temp_1 = ixheaacd_mps_add32(temp_1, ABS_THR, &qtemp1, 30);
147 temp_1 = ixheaacd_mps_sqrt(temp_1, &qtemp1, sqrt_tab);
148 temp_2 = ixheaacd_mps_div_32(cross, temp_1, &qtemp2);
149 qtemp2 = qtemp2 + 15 - qtemp1;
150 icc[pb] = ixheaacd_mps_convert_to_qn(temp_2, qtemp2, 15);
151
152 if (icc[pb] > ONE_IN_Q15) {
153 icc[pb] = ONE_IN_Q15;
154 } else {
155 if (icc[pb] < MINUS_POINT_NINE_NINE_Q15) {
156 icc[pb] = MINUS_POINT_NINE_NINE_Q15;
157 }
158 }
159
160 iid[pb] =
161 ixheaacd_quantize_cld(iid[pb], pstr_mps_state->ia_mps_dec_mps_table.bitdec_table_ptr);
162 icc[pb] =
163 ixheaacd_quantize_icc(icc[pb], pstr_mps_state->ia_mps_dec_mps_table.bitdec_table_ptr);
164 }
165
166 ixheaacd_param_2_umx_ps_core_tables(iid, icc, num_parameter_bands, 0, h11, h12, h21, h22,
167 h12_res, h22_res, c_l, c_r,
168 pstr_mps_state->ia_mps_dec_mps_table.m1_m2_table_ptr);
169
170 for (pb = 0; pb < num_parameter_bands; pb++) {
171 m1_param->m1_param_real[0][0][ps][pb] = ONE_IN_Q15;
172 m1_param->m1_param_real[3][0][ps][pb] = ONE_IN_Q15;
173 }
174
175 memcpy(m2_param->m2_decor_real[0][ps], h12, num_parameter_bands * sizeof(h12[0]));
176 memcpy(m2_param->m2_decor_real[1][ps], h22, num_parameter_bands * sizeof(h22[0]));
177 memcpy(m2_param->m2_resid_real[0][ps], h11, num_parameter_bands * sizeof(h11[0]));
178 memcpy(m2_param->m2_resid_real[1][ps], h21, num_parameter_bands * sizeof(h21[0]));
179 }
180 return;
181 }
182
ixheaacd_calc_m1m2_51s2(ia_heaac_mps_state_struct * pstr_mps_state)183 VOID ixheaacd_calc_m1m2_51s2(ia_heaac_mps_state_struct *pstr_mps_state) {
184 ia_mps_dec_auxilary_struct *p_aux_struct = pstr_mps_state->aux_struct;
185 ia_mps_dec_m2_param_struct *m2_param = p_aux_struct->m2_param;
186 ia_mps_dec_m1_param_struct *m1_param = pstr_mps_state->array_struct->m1_param;
187 WORD32 ps, pb;
188
189 WORD32 *iid;
190 WORD32 *icc;
191 WORD32 *h11;
192 WORD32 *h12;
193 WORD32 *h21;
194 WORD32 *h22;
195 WORD32 *h12_res;
196 WORD32 *h22_res;
197 WORD32 *g_s;
198 WORD16 *c_l;
199 WORD16 *c_r;
200
201 const WORD32 *sqrt_tab = pstr_mps_state->ia_mps_dec_mps_table.common_table_ptr->sqrt_tab;
202 WORD32 num_parameter_sets = pstr_mps_state->num_parameter_sets;
203 WORD32 num_parameter_bands = pstr_mps_state->num_parameter_bands;
204
205 iid = pstr_mps_state->mps_scratch_mem_v;
206 icc = iid + IXHEAAC_GET_SIZE_ALIGNED_TYPE(MAX_PARAMETER_BANDS, sizeof(*icc), BYTE_ALIGN_8);
207 h11 = icc + IXHEAAC_GET_SIZE_ALIGNED_TYPE(MAX_PARAMETER_BANDS, sizeof(*h11), BYTE_ALIGN_8);
208 h12 = h11 + IXHEAAC_GET_SIZE_ALIGNED_TYPE(MAX_PARAMETER_BANDS, sizeof(*h12), BYTE_ALIGN_8);
209 h21 = h12 + IXHEAAC_GET_SIZE_ALIGNED_TYPE(MAX_PARAMETER_BANDS, sizeof(*h21), BYTE_ALIGN_8);
210 h22 = h21 + IXHEAAC_GET_SIZE_ALIGNED_TYPE(MAX_PARAMETER_BANDS, sizeof(*h22), BYTE_ALIGN_8);
211 h12_res =
212 h22 + IXHEAAC_GET_SIZE_ALIGNED_TYPE(MAX_PARAMETER_BANDS, sizeof(*h12_res), BYTE_ALIGN_8);
213 h22_res = h12_res +
214 IXHEAAC_GET_SIZE_ALIGNED_TYPE(MAX_PARAMETER_BANDS, sizeof(*h22_res), BYTE_ALIGN_8);
215 g_s = h22_res + IXHEAAC_GET_SIZE_ALIGNED_TYPE(MAX_PARAMETER_BANDS, sizeof(*g_s), BYTE_ALIGN_8);
216 c_l = (WORD16 *)pstr_mps_state->mps_scratch_mem_v +
217 IXHEAAC_GET_SIZE_ALIGNED_TYPE(PARAMETER_BANDSX18, sizeof(*c_l), BYTE_ALIGN_8);
218 c_r = c_l + IXHEAAC_GET_SIZE_ALIGNED_TYPE(MAX_PARAMETER_BANDS, sizeof(*c_r), BYTE_ALIGN_8);
219
220 for (ps = 0; ps < num_parameter_sets; ps++) {
221 for (pb = 0; pb < num_parameter_bands; pb++) {
222 WORD32 p_l_c, p_r_c;
223 WORD32 p_l_lr, p_r_lr;
224 WORD32 left;
225 WORD32 right;
226 WORD32 center;
227 WORD32 cross;
228 WORD32 temp_1;
229 WORD16 qtemp1;
230 ia_mps_dec_spatial_bs_frame_struct *p_cur_bs = pstr_mps_state->bs_frame;
231
232 p_l_c = pstr_mps_state->ia_mps_dec_mps_table.m1_m2_table_ptr
233 ->cld_tab_1[p_cur_bs->ott_cld_idx[0][ps][pb] + 15];
234 p_r_c = pstr_mps_state->ia_mps_dec_mps_table.m1_m2_table_ptr
235 ->cld_tab_1[15 - p_cur_bs->ott_cld_idx[0][ps][pb]];
236
237 p_l_lr = pstr_mps_state->ia_mps_dec_mps_table.m1_m2_table_ptr
238 ->cld_tab_1[p_cur_bs->ott_cld_idx[1][ps][pb] + 15];
239 p_r_lr = pstr_mps_state->ia_mps_dec_mps_table.m1_m2_table_ptr
240 ->cld_tab_1[15 - p_cur_bs->ott_cld_idx[1][ps][pb]];
241
242 left = ixheaacd_mps_mult32_shr_15(p_l_c, p_l_lr);
243 right = ixheaacd_mps_mult32_shr_15(p_l_c, p_r_lr);
244 center = (p_r_c) >> 1;
245
246 temp_1 = ixheaacd_mps_mult32_shr_15(left, right);
247 qtemp1 = 15;
248 temp_1 = ixheaacd_mps_sqrt(temp_1, &qtemp1, sqrt_tab);
249 temp_1 = ixheaacd_mps_convert_to_qn(temp_1, qtemp1, 15);
250 cross = ixheaacd_mps_mult32_shr_15(temp_1, p_aux_struct->ott_icc[1][ps][pb]);
251
252 temp_1 = ixheaac_add32_sat((left + right), cross);
253 temp_1 = ixheaacd_mps_mult32_shr_15(temp_1, center);
254 qtemp1 = 15;
255 temp_1 = ixheaacd_mps_sqrt(temp_1, &qtemp1, sqrt_tab);
256 temp_1 = ixheaacd_mps_convert_to_qn(temp_1, qtemp1, 15);
257 temp_1 = ixheaacd_mps_mult32_shr_15(temp_1, p_aux_struct->ott_icc[0][ps][pb]);
258 temp_1 = ixheaac_add32_sat(temp_1, center);
259 cross = ixheaac_add32_sat(cross, temp_1);
260
261 temp_1 = ixheaacd_mps_mult32_shr_15(left, center);
262 qtemp1 = 15;
263 temp_1 = ixheaacd_mps_sqrt(temp_1, &qtemp1, sqrt_tab);
264 temp_1 = ixheaacd_mps_convert_to_qn(2 * temp_1, qtemp1, 15);
265 temp_1 = ixheaacd_mps_mult32_shr_15(temp_1, p_aux_struct->ott_icc[0][ps][pb]);
266 temp_1 = ixheaac_add32_sat(temp_1, center);
267 left = ixheaac_add32_sat(left, temp_1);
268
269 temp_1 = ixheaacd_mps_mult32_shr_15(right, center);
270 qtemp1 = 15;
271 temp_1 = ixheaacd_mps_sqrt(temp_1, &qtemp1, sqrt_tab);
272 temp_1 = ixheaacd_mps_convert_to_qn(2 * temp_1, qtemp1, 15);
273 temp_1 = ixheaacd_mps_mult32_shr_15(temp_1, p_aux_struct->ott_icc[0][ps][pb]);
274 temp_1 = ixheaac_add32_sat(temp_1, center);
275 right = ixheaac_add32_sat(right, temp_1);
276
277 temp_1 = ixheaacd_mps_div32_in_q15(left, right);
278 qtemp1 = 15;
279 iid[pb] = ((10 * ixheaacd_mps_log10(temp_1, qtemp1)) >> 1);
280
281 temp_1 = ixheaacd_mps_mult32_shr_15(left, right);
282 qtemp1 = 15;
283 temp_1 = ixheaacd_mps_sqrt(temp_1, &qtemp1, sqrt_tab);
284 temp_1 = ixheaacd_mps_convert_to_qn(temp_1, qtemp1, 15);
285 if (temp_1 == 0) {
286 temp_1 = 1;
287 }
288
289 icc[pb] = ixheaacd_mps_div32_in_q15(cross, temp_1);
290
291 temp_1 = ixheaac_add32_sat(left, right);
292 qtemp1 = 15;
293 temp_1 = ixheaacd_mps_sqrt(temp_1, &qtemp1, sqrt_tab);
294 g_s[pb] = ixheaacd_mps_convert_to_qn(temp_1, qtemp1, 15);
295
296 if (icc[pb] > ONE_IN_Q15) {
297 icc[pb] = ONE_IN_Q15;
298 } else {
299 if (icc[pb] < MINUS_POINT_NINE_NINE_Q15) {
300 icc[pb] = MINUS_POINT_NINE_NINE_Q15;
301 }
302 }
303
304 iid[pb] =
305 ixheaacd_quantize_cld(iid[pb], pstr_mps_state->ia_mps_dec_mps_table.bitdec_table_ptr);
306 icc[pb] =
307 ixheaacd_quantize_icc(icc[pb], pstr_mps_state->ia_mps_dec_mps_table.bitdec_table_ptr);
308 }
309
310 ixheaacd_param_2_umx_ps_core_tables(iid, icc, num_parameter_bands, 0, h11, h12, h21, h22,
311 h12_res, h22_res, c_l, c_r,
312 pstr_mps_state->ia_mps_dec_mps_table.m1_m2_table_ptr);
313
314 for (pb = 0; pb < num_parameter_bands; pb++) {
315 m1_param->m1_param_real[0][0][ps][pb] = ONE_IN_Q15;
316 m1_param->m1_param_real[2][0][ps][pb] = ONE_IN_Q15;
317 }
318
319 for (pb = 0; pb < num_parameter_bands; pb++) {
320 m2_param->m2_decor_real[0][ps][pb] = ixheaacd_mps_mult32_shr_15(g_s[pb], h12[pb]);
321 m2_param->m2_decor_real[1][ps][pb] = ixheaacd_mps_mult32_shr_15(g_s[pb], h22[pb]);
322 }
323
324 for (pb = 0; pb < num_parameter_bands; pb++) {
325 m2_param->m2_resid_real[0][ps][pb] = ixheaacd_mps_mult32_shr_15(g_s[pb], h11[pb]);
326 m2_param->m2_resid_real[1][ps][pb] = ixheaacd_mps_mult32_shr_15(g_s[pb], h21[pb]);
327 }
328 }
329 return;
330 }
331