xref: /aosp_15_r20/external/libxaac/decoder/ixheaacd_mps_m1m2_common.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 #include "ixheaac_type_def.h"
21 #include "ixheaacd_mps_struct_def.h"
22 #include "ixheaacd_error_codes.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_bitdec.h"
43 #include "ixheaacd_mps_calc_m1m2_tree_config.h"
44 
ixheaacd_buffer_m1(ia_heaac_mps_state_struct * pstr_mps_state)45 VOID ixheaacd_buffer_m1(ia_heaac_mps_state_struct *pstr_mps_state) {
46   ia_mps_dec_m1_param_struct *m1_param = pstr_mps_state->array_struct->m1_param;
47   WORD32 pb, row, col;
48 
49   ia_mps_persistent_mem *persistent_mem = &pstr_mps_state->mps_persistent_mem;
50   WORD32 *m1_param_real_prev = persistent_mem->m1_param_real_prev;
51   WORD32 *m1_param_imag_prev = persistent_mem->m1_param_imag_prev;
52 
53   WORD32 *m1_param_real, *m1_param_imag;
54 
55   WORD32 *p_m1_param_real, *p_m1_param_re;
56 
57   WORD32 num_parameter_bands = pstr_mps_state->num_parameter_bands;
58   WORD32 num_v_channels = pstr_mps_state->num_v_channels;
59   WORD32 num_x_channels = pstr_mps_state->num_x_channels;
60   WORD32 m1_param_imag_present = pstr_mps_state->m1_param_imag_present;
61 
62   WORD32 num_parameter_sets_prev;
63 
64   pstr_mps_state->num_parameter_sets_prev = pstr_mps_state->num_parameter_sets;
65   num_parameter_sets_prev = pstr_mps_state->num_parameter_sets_prev;
66 
67   if (m1_param_imag_present) {
68     WORD32 *p_m1_param_imag = &m1_param->m1_param_imag[0][0][0][0];
69     p_m1_param_real = &m1_param->m1_param_real[0][0][0][0];
70     for (row = 0; row < num_v_channels; row++) {
71       WORD32 *p_m1_param_im = p_m1_param_imag;
72       p_m1_param_re = p_m1_param_real;
73 
74       for (col = 0; col < num_x_channels; col++) {
75         m1_param_real = p_m1_param_re + (num_parameter_sets_prev - 1) * MAX_PARAMETER_BANDS;
76         m1_param_imag = p_m1_param_im + (num_parameter_sets_prev - 1) * MAX_PARAMETER_BANDS;
77 
78         for (pb = 0; pb < num_parameter_bands; pb++) {
79           *m1_param_real_prev++ = *m1_param_real++;
80           *m1_param_imag_prev++ = *m1_param_imag++;
81         }
82         p_m1_param_re += PBXPS;
83         p_m1_param_im += PBXPS;
84       }
85       p_m1_param_real += MAX_INPUT_CHANNELS_MPS * PBXPS;
86       p_m1_param_imag += MAX_INPUT_CHANNELS_MPS * PBXPS;
87     }
88   } else {
89     p_m1_param_real = &m1_param->m1_param_real[0][0][0][0];
90     for (row = 0; row < num_v_channels; row++) {
91       p_m1_param_re = p_m1_param_real;
92       for (col = 0; col < num_x_channels; col++) {
93         m1_param_real = p_m1_param_re + (num_parameter_sets_prev - 1) * MAX_PARAMETER_BANDS;
94         for (pb = 0; pb < num_parameter_bands; pb++) *m1_param_real_prev++ = *m1_param_real++;
95 
96         p_m1_param_re += PBXPS;
97       }
98       p_m1_param_real += MAX_INPUT_CHANNELS_MPS * PBXPS;
99     }
100   }
101 }
102 
ixheaacd_buffer_m2(ia_heaac_mps_state_struct * pstr_mps_state)103 VOID ixheaacd_buffer_m2(ia_heaac_mps_state_struct *pstr_mps_state) {
104   WORD32 num_direct_signals = pstr_mps_state->num_direct_signals;
105   WORD32 pb, row, col, col_counter = num_direct_signals + pstr_mps_state->num_decor_signals;
106 
107   ia_mps_persistent_mem *persistent_mem = &pstr_mps_state->mps_persistent_mem;
108   ia_mps_dec_m2_param_struct *m2_param = pstr_mps_state->aux_struct->m2_param;
109 
110   WORD32 *m2_decor_real_prev = persistent_mem->m2_decor_real_prev;
111   WORD32 *m2_decor_imag_prev = persistent_mem->m2_decor_imag_prev;
112 
113   WORD32 *m2_resid_real_prev = persistent_mem->m2_resid_real_prev;
114   WORD32 *m2_resid_imag_prev = persistent_mem->m2_resid_imag_prev;
115 
116   WORD32 *m2_decor_real, *m2_decor_imag, *m2_resid_real, *m2_resid_imag;
117   WORD32 idx = -1;
118 
119   WORD32 resid_col_counter;
120 
121   WORD32 num_parameter_bands = pstr_mps_state->num_parameter_bands;
122   WORD32 num_output_channels = pstr_mps_state->num_output_channels;
123   WORD32 num_parameter_sets_prev = pstr_mps_state->num_parameter_sets_prev;
124 
125   if (pstr_mps_state->residual_coding)
126     resid_col_counter = col_counter;
127   else
128     resid_col_counter = num_direct_signals;
129 
130   if (pstr_mps_state->m2_param_imag_present) {
131     for (row = 0; row < num_output_channels; row++) {
132       for (col = num_direct_signals; col < col_counter; col++) {
133         if (pstr_mps_state->m2_param_present[row][col] & 1) {
134           idx++;
135           m2_decor_real = &m2_param->m2_decor_real[idx][num_parameter_sets_prev - 1][0];
136 
137           for (pb = 0; pb < num_parameter_bands; pb++) *m2_decor_real_prev++ = *m2_decor_real++;
138 
139           m2_decor_imag = &m2_param->m2_decor_imag[idx][num_parameter_sets_prev - 1][0];
140 
141           for (pb = 0; pb < num_parameter_bands; pb++) *m2_decor_imag_prev++ = *m2_decor_imag++;
142         }
143       }
144     }
145 
146     idx = -1;
147     for (row = 0; row < num_output_channels; row++) {
148       for (col = 0; col < resid_col_counter; col++) {
149         if (pstr_mps_state->m2_param_present[row][col] & 2) {
150           idx++;
151           m2_resid_real = &m2_param->m2_resid_real[idx][num_parameter_sets_prev - 1][0];
152 
153           for (pb = 0; pb < num_parameter_bands; pb++) *m2_resid_real_prev++ = *m2_resid_real++;
154           m2_resid_imag = &m2_param->m2_resid_imag[idx][num_parameter_sets_prev - 1][0];
155           for (pb = 0; pb < num_parameter_bands; pb++) *m2_resid_imag_prev++ = *m2_resid_imag++;
156         }
157       }
158     }
159   } else {
160     for (row = 0; row < num_output_channels; row++) {
161       for (col = num_direct_signals; col < col_counter; col++) {
162         if (pstr_mps_state->m2_param_present[row][col] & 1) {
163           idx++;
164           m2_decor_real = &m2_param->m2_decor_real[idx][num_parameter_sets_prev - 1][0];
165 
166           for (pb = 0; pb < num_parameter_bands; pb++) *m2_decor_real_prev++ = *m2_decor_real++;
167         }
168       }
169     }
170 
171     idx = -1;
172     for (row = 0; row < num_output_channels; row++) {
173       for (col = 0; col < resid_col_counter; col++) {
174         if (pstr_mps_state->m2_param_present[row][col] & 2) {
175           idx++;
176           m2_resid_real = &m2_param->m2_resid_real[idx][num_parameter_sets_prev - 1][0];
177 
178           for (pb = 0; pb < num_parameter_bands; pb++) *m2_resid_real_prev++ = *m2_resid_real++;
179         }
180       }
181     }
182   }
183 }
184 
ixheaacd_update_alpha(ia_heaac_mps_state_struct * pstr_mps_state)185 static VOID ixheaacd_update_alpha(ia_heaac_mps_state_struct *pstr_mps_state) {
186   WORD32 alpha;
187 
188   WORD32 *arbdmx_alpha_prev = pstr_mps_state->mps_persistent_mem.arbdmx_alpha_prev;
189   ia_mps_dec_auxilary_struct *p_aux_struct = pstr_mps_state->aux_struct;
190   WORD32 *arbdmx_alpha = p_aux_struct->arbdmx_alpha;
191 
192   WORD32 n_ch_in = pstr_mps_state->num_input_channels;
193   WORD32 ch;
194 
195   for (ch = 0; ch < n_ch_in; ch++) {
196     *arbdmx_alpha_prev++ = arbdmx_alpha[ch];
197 
198     if (pstr_mps_state->arbitrary_downmix == 2) {
199       alpha = arbdmx_alpha[ch];
200 
201       if (p_aux_struct->arbdmx_residual_abs[ch]) {
202         alpha -= POINT_THREE_THREE_Q15;
203 
204         if (alpha < 0) alpha = 0;
205       } else {
206         alpha += POINT_THREE_THREE_Q15;
207 
208         if (alpha > ONE_IN_Q15) alpha = ONE_IN_Q15;
209       }
210     } else {
211       alpha = ONE_IN_Q15;
212     }
213 
214     arbdmx_alpha[ch] = alpha;
215   }
216 }
217 
ixheaacd_calc_m1m2(ia_heaac_mps_state_struct * pstr_mps_state)218 VOID ixheaacd_calc_m1m2(ia_heaac_mps_state_struct *pstr_mps_state) {
219   WORD32 up_mix_type = pstr_mps_state->up_mix_type;
220   WORD32 binaural_quality = pstr_mps_state->binaural_quality;
221 
222   if (pstr_mps_state->arbitrary_downmix != 0) {
223     ixheaacd_update_alpha(pstr_mps_state);
224   }
225 
226   switch (pstr_mps_state->tree_config) {
227     case TREE_5151: {
228       if (up_mix_type == 3) {
229         ixheaacd_calc_m1m2_51s1(pstr_mps_state);
230       } else {
231         ixheaacd_calc_m1m2_5151(pstr_mps_state);
232       }
233     } break;
234     case TREE_5152: {
235       if (up_mix_type == 3) {
236         ixheaacd_calc_m1m2_51s2(pstr_mps_state);
237       } else {
238         ixheaacd_calc_m1m2_5152(pstr_mps_state);
239       }
240     } break;
241     case TREE_525:
242       if (up_mix_type == 1) {
243         ixheaacd_calc_m1m2_emm(pstr_mps_state);
244       } else if (up_mix_type == 2) {
245         if (binaural_quality == 1) {
246           ixheaacd_calc_m1m2_5227(pstr_mps_state);
247         }
248       } else {
249         ixheaacd_calc_m1m2_5251(pstr_mps_state);
250       }
251       break;
252     case TREE_7271:
253       if (up_mix_type == 0) {
254         ixheaacd_calc_m1m2_7271(pstr_mps_state);
255       } else if (up_mix_type == 2) {
256         if (binaural_quality == 1) {
257           ixheaacd_calc_m1m2_5227(pstr_mps_state);
258         }
259       }
260       break;
261     case TREE_7272:
262       if (up_mix_type == 0) {
263         ixheaacd_calc_m1m2_7272(pstr_mps_state);
264       } else if (up_mix_type == 2) {
265         if (binaural_quality == 1) {
266           ixheaacd_calc_m1m2_5227(pstr_mps_state);
267         }
268       }
269       break;
270     case TREE_7571:
271       ixheaacd_calc_m1m2_7571(pstr_mps_state);
272       break;
273     case TREE_7572:
274       ixheaacd_calc_m1m2_7572(pstr_mps_state);
275       break;
276     default:
277       break;
278   };
279 
280   return;
281 }
282