• 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 <string.h>
21 #include "ixheaacd_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 "ixheaacd_constants.h"
26 #include "ixheaacd_basic_ops32.h"
27 #include "ixheaacd_basic_ops40.h"
28 #include "ixheaacd_bitbuffer.h"
29 #include "ixheaacd_error_codes.h"
30 #include "ixheaacd_common_rom.h"
31 #include "ixheaacd_sbrdecsettings.h"
32 #include "ixheaacd_sbr_scale.h"
33 #include "ixheaacd_env_extr_part.h"
34 #include "ixheaacd_sbr_rom.h"
35 #include "ixheaacd_lpp_tran.h"
36 #include "ixheaacd_hybrid.h"
37 #include "ixheaacd_ps_dec.h"
38 #include "ixheaacd_env_extr.h"
39 #include "ixheaacd_mps_polyphase.h"
40 #include "ixheaacd_config.h"
41 #include "ixheaacd_qmf_dec.h"
42 #include "ixheaacd_mps_dec.h"
43 #include "ixheaacd_mps_bitdec.h"
44 #include "ixheaacd_mps_macro_def.h"
45 #include "ixheaacd_mps_basic_op.h"
46 
ixheaacd_init_tonality(ia_heaac_mps_state_struct * pstr_mps_state)47 VOID ixheaacd_init_tonality(ia_heaac_mps_state_struct *pstr_mps_state) {
48   ia_mps_dec_tonality_state_struct *ton_state = pstr_mps_state->mps_persistent_mem.ton_state;
49   WORD32 cnt = pstr_mps_state->qmf_bands * 8;
50   WORD32 qmf_bands = pstr_mps_state->qmf_bands;
51 
52   memset(ton_state->spec_prev_real, 0, cnt * sizeof(ton_state->spec_prev_real[0]));
53   memset(ton_state->spec_prev_imag, 0, cnt * sizeof(ton_state->spec_prev_imag[0]));
54   memset(ton_state->p_cross_real, 0, cnt * sizeof(ton_state->p_cross_real[0]));
55   memset(ton_state->p_cross_imag, 0, cnt * sizeof(ton_state->p_cross_imag[0]));
56   memset(ton_state->p_sum, 0, cnt * sizeof(ton_state->p_sum[0]));
57   memset(ton_state->p_sum_prev, 0, cnt * sizeof(ton_state->p_sum_prev[0]));
58 
59   memset(ton_state->buf_real, 0, qmf_bands * 6 * sizeof(ton_state->buf_real[0][0]));
60   memset(ton_state->buf_imag, 0, qmf_bands * 6 * sizeof(ton_state->buf_imag[0][0]));
61   memset(ton_state->win_buf_real, 0, qmf_bands * 16 * sizeof(ton_state->win_buf_real[0][0]));
62   memset(ton_state->win_buf_imag, 0, qmf_bands * 16 * sizeof(ton_state->win_buf_imag[0][0]));
63 }
64 
ixheaacd_zoom_fft16(WORD32 * in_real,WORD32 * in_imag,WORD32 * out_real,WORD32 * out_imag,WORD32 qmf_band,WORD32 dfrac,ia_mps_dec_mps_tables_struct * ia_mps_dec_mps_table_ptr)65 VOID ixheaacd_zoom_fft16(WORD32 *in_real, WORD32 *in_imag, WORD32 *out_real, WORD32 *out_imag,
66                          WORD32 qmf_band, WORD32 dfrac,
67                          ia_mps_dec_mps_tables_struct *ia_mps_dec_mps_table_ptr) {
68   WORD32 blackman[16];
69 
70   WORD32 v_real[16], v_imag[16];
71   WORD32 t_real, t_imag;
72   WORD32 e_real, e_imag;
73 
74   WORD32 temp_1, temp_2, temp3, temp4;
75   const WORD32 *bitrev = ia_mps_dec_mps_table_ptr->tonality_table_ptr->bitrev;
76   const WORD32 *w_real = ia_mps_dec_mps_table_ptr->tonality_table_ptr->w_real;
77   const WORD32 *w_imag = ia_mps_dec_mps_table_ptr->tonality_table_ptr->w_imag;
78   const WORD32 *cos_tab = ia_mps_dec_mps_table_ptr->hybrid_table_ptr->cosine_array;
79   const WORD32 *sin_tab = ia_mps_dec_mps_table_ptr->hybrid_table_ptr->sine_array;
80 
81   WORD32 i, j, s1, s2;
82 
83   temp3 = TWO_PI_BY_FIFTEEN_Q15;
84 
85   for (i = 0; i < 16; i++) {
86     temp_1 = (i << 15) + dfrac;
87     temp_2 = ixheaacd_mps_mult32_shr_15(temp_1, temp3);
88     temp_2 = ixheaacd_mps_cos(temp_2, cos_tab);
89     temp_2 >>= 1;
90 
91     temp_1 <<= 1;
92     temp_1 = ixheaacd_mps_cos(temp_1, cos_tab);
93 
94     temp_1 = ixheaacd_mps_mult32x16_shr_16(temp_1, TWO_BY_TWENTYFIVE_Q16);
95 
96     temp4 = POINT_FOUR_TWO_Q15 - temp_2;
97 
98     blackman[i] = temp_1 + temp4;
99   }
100 
101   for (i = 0; i < 16; i++) {
102     WORD32 idx = bitrev[i];
103     temp_1 = ixheaacd_mps_mult32_shr_30(in_real[i], w_real[i]) -
104              ixheaacd_mps_mult32_shr_30(in_imag[i], w_imag[i]);
105     v_real[idx] = ixheaacd_mps_mult32_shr_30(temp_1, blackman[i]);
106 
107     temp_1 = ixheaacd_mps_mult32_shr_30(in_real[i], w_imag[i]) +
108              ixheaacd_mps_mult32_shr_30(in_imag[i], w_real[i]);
109     v_imag[idx] = ixheaacd_mps_mult32_shr_30(temp_1, blackman[i]);
110   }
111 
112   for (s1 = 1, s2 = 16; s1 < 8; s1 <<= 1, s2 >>= 1) {
113     for (i = 0; i < 16; i += 2 * s1) {
114       for (j = 0; j < s1; j++) {
115         t_real = ixheaacd_mps_mult32_shr_30(v_real[i + j + s1], w_real[j * s2]) -
116                  ixheaacd_mps_mult32_shr_30(v_imag[i + j + s1], w_imag[j * s2]);
117         t_imag = ixheaacd_mps_mult32_shr_30(v_real[i + j + s1], w_imag[j * s2]) +
118                  ixheaacd_mps_mult32_shr_30(v_imag[i + j + s1], w_real[j * s2]);
119 
120         v_real[i + j + s1] = v_real[i + j] - t_real;
121         v_imag[i + j + s1] = v_imag[i + j] - t_imag;
122 
123         v_real[i + j] += t_real;
124         v_imag[i + j] += t_imag;
125       }
126     }
127   }
128 
129   for (j = 0; j < 8; j++) {
130     WORD32 idx = j << 1;
131     t_real = ixheaacd_mps_mult32_shr_30(v_real[j + 8], w_real[idx]) -
132              ixheaacd_mps_mult32_shr_30(v_imag[j + 8], w_imag[idx]);
133     t_imag = ixheaacd_mps_mult32_shr_30(v_real[j + 8], w_imag[idx]) +
134              ixheaacd_mps_mult32_shr_30(v_imag[j + 8], w_real[idx]);
135 
136     if ((qmf_band & ONE_BIT_MASK) == 0) {
137       out_real[j] = v_real[j] + t_real;
138       out_imag[j] = v_imag[j] + t_imag;
139     } else {
140       out_real[j] = v_real[j] - t_real;
141       out_imag[j] = v_imag[j] - t_imag;
142     }
143   }
144 
145   temp3 = MINUS_PI_BY_EIGHT_Q15;
146   for (i = 0; i < 8; i++) {
147     if ((qmf_band & ONE_BIT_MASK) == 0) {
148       temp_1 = dfrac * i;
149       temp_1 = ixheaacd_mps_mult32_shr_15(temp_1, temp3);
150       e_real = ixheaacd_mps_cos(temp_1, cos_tab);
151       e_imag = ixheaacd_mps_sin(temp_1, sin_tab);
152     } else {
153       temp_1 = dfrac * (i - 8);
154       temp_1 = ixheaacd_mps_mult32_shr_15(temp_1, temp3);
155       e_real = ixheaacd_mps_cos(temp_1, cos_tab);
156       e_imag = ixheaacd_mps_sin(temp_1, sin_tab);
157     }
158 
159     t_real = ixheaacd_mps_mult32_shr_15(out_real[i], e_real) -
160              ixheaacd_mps_mult32_shr_15(out_imag[i], e_imag);
161     out_imag[i] = ixheaacd_mps_mult32_shr_15(out_real[i], e_imag) +
162                   ixheaacd_mps_mult32_shr_15(out_imag[i], e_real);
163     out_real[i] = t_real;
164   }
165 }
166 
ixheaacd_measure_tonality(ia_heaac_mps_state_struct * pstr_mps_state,WORD32 * tonality)167 VOID ixheaacd_measure_tonality(ia_heaac_mps_state_struct *pstr_mps_state, WORD32 *tonality) {
168   ia_mps_dec_tonality_state_struct *ton_state = pstr_mps_state->mps_persistent_mem.ton_state;
169 
170   WORD32 *qmf_real;
171   WORD32 *qmf_imag;
172 
173   WORD32 *spec_zoom_real;
174   WORD32 *spec_zoom_imag;
175 
176   WORD32 *spec_prev_real = ton_state->spec_prev_real;
177   WORD32 *spec_prev_imag = ton_state->spec_prev_imag;
178 
179   WORD32 *p_cross_real = ton_state->p_cross_real;
180   WORD32 *p_cross_imag = ton_state->p_cross_imag;
181 
182   WORD32 *p_sum = ton_state->p_sum;
183   WORD32 *p_sum_prev = ton_state->p_sum_prev;
184 
185   WORD32 *p_max;
186 
187   WORD32 *coh_spec;
188   WORD32 *pow_spec;
189 
190   WORD32 *p_buf_real, *p_buf_imag, *p_buf_re, *p_buf_im;
191   WORD32 *buf_real, *buf_imag;
192   WORD32 g, gmax;
193   WORD32 i, j, q, s, c, cnt;
194 
195   WORD32 const *part;
196   WORD32 pstart;
197   WORD32 pstop = 0;
198   WORD32 pqmf, num, den, tmp_ton, beta, dwin, dfrac;
199   WORD16 q_beta, q_tmp_ton;
200 
201   WORD32 qmf_bands = pstr_mps_state->qmf_bands;
202   WORD32 time_slots = pstr_mps_state->time_slots;
203   WORD32 num_input_channels = pstr_mps_state->num_input_channels;
204   WORD32 num_parameter_bands = pstr_mps_state->num_parameter_bands;
205   WORD32 sampling_freq = pstr_mps_state->sampling_freq;
206   const WORD32 *sqrt_tab = pstr_mps_state->ia_mps_dec_mps_table.common_table_ptr->sqrt_tab;
207 
208   WORD32 nstart;
209 
210   WORD32 tmp_real, tmp_imag;
211 
212   WORD32 temp_1, temp;
213   WORD16 qtemp1, qtemp2;
214 
215   spec_zoom_real =
216       (WORD32 *)((WORD8 *)pstr_mps_state->mps_scratch_mem_v + SCRATCH_OFFSET_SMOOTHING);
217   spec_zoom_imag = spec_zoom_real + QMF_BANDSX8;
218   p_max = spec_zoom_imag + QMF_BANDSX8;
219   coh_spec = p_max + QMF_BANDSX8;
220   pow_spec = coh_spec + QMF_BANDSX8;
221 
222   qmf_real = pow_spec + QMF_BANDSX8;
223   qmf_imag = qmf_real + QBXTS;
224 
225   switch (num_parameter_bands) {
226     case PARAMETER_BANDS_4:
227       part = pstr_mps_state->ia_mps_dec_mps_table.tonality_table_ptr->part4;
228       break;
229     case PARAMETER_BANDS_5:
230       part = pstr_mps_state->ia_mps_dec_mps_table.tonality_table_ptr->part5;
231       break;
232     case PARAMETER_BANDS_7:
233       part = pstr_mps_state->ia_mps_dec_mps_table.tonality_table_ptr->part7;
234       break;
235     case PARAMETER_BANDS_10:
236       part = pstr_mps_state->ia_mps_dec_mps_table.tonality_table_ptr->part10;
237       break;
238     case PARAMETER_BANDS_14:
239       part = pstr_mps_state->ia_mps_dec_mps_table.tonality_table_ptr->part14;
240       break;
241     case PARAMETER_BANDS_20:
242       part = pstr_mps_state->ia_mps_dec_mps_table.tonality_table_ptr->part20;
243       break;
244     case PARAMETER_BANDS_28:
245       part = pstr_mps_state->ia_mps_dec_mps_table.tonality_table_ptr->part28;
246       break;
247     case PARAMETER_BANDS_40:
248       part = pstr_mps_state->ia_mps_dec_mps_table.tonality_table_ptr->part40;
249       break;
250     default:
251       part = pstr_mps_state->ia_mps_dec_mps_table.tonality_table_ptr->part4;
252       break;
253   }
254 
255   temp = time_slots - 6;
256 
257   p_buf_real = pstr_mps_state->array_struct->buf_real;
258   p_buf_imag = pstr_mps_state->array_struct->buf_imag;
259 
260   for (q = 0; q < qmf_bands; q++) {
261     qmf_real += 6;
262     qmf_imag += 6;
263 
264     p_buf_re = p_buf_real;
265     p_buf_im = p_buf_imag;
266     for (s = 0; s < time_slots; s++) {
267       tmp_real = 0;
268       tmp_imag = 0;
269 
270       buf_real = p_buf_re;
271       buf_imag = p_buf_im;
272 
273       for (c = 0; c < num_input_channels; c++) {
274         tmp_real += *buf_real;
275         tmp_imag += *buf_imag;
276 
277         buf_real += TSXHB;
278         buf_imag += TSXHB;
279       }
280 
281       if (s == temp) {
282         qmf_real -= time_slots;
283         qmf_imag -= time_slots;
284       }
285 
286       if (s + 6 < time_slots) {
287         *qmf_real++ = tmp_real;
288         *qmf_imag++ = tmp_imag;
289       } else {
290         *qmf_real++ = ton_state->buf_real[q][s + 6 - time_slots];
291         *qmf_imag++ = ton_state->buf_imag[q][s + 6 - time_slots];
292 
293         ton_state->buf_real[q][s + 6 - time_slots] = tmp_real;
294         ton_state->buf_imag[q][s + 6 - time_slots] = tmp_imag;
295       }
296       p_buf_re += MAX_HYBRID_BANDS;
297       p_buf_re += MAX_HYBRID_BANDS;
298     }
299     qmf_real += temp;
300     qmf_imag += temp;
301 
302     p_buf_real++;
303     p_buf_imag++;
304   }
305 
306   gmax = pstr_mps_state->ia_mps_dec_mps_table.tonality_table_ptr->gmax_fix[time_slots];
307   dwin = pstr_mps_state->ia_mps_dec_mps_table.tonality_table_ptr->dwin_fix[time_slots];
308 
309   qtemp1 = 15;
310   temp_1 = ixheaacd_mps_mult32(dwin, (40 * (qmf_bands)), &qtemp1, 0);
311   beta = ixheaacd_mps_div_32(temp_1, sampling_freq, &q_beta);
312   q_beta = q_beta + qtemp1;
313   beta = ixheaacd_mps_convert_to_qn(beta, q_beta, 15);
314 
315   for (i = 0; i < num_parameter_bands; i++) {
316     tonality[i] = ONE_IN_Q15;
317   }
318 
319   for (g = 0; g < gmax; g++) {
320     nstart = pstr_mps_state->ia_mps_dec_mps_table.tonality_table_ptr->nstart_fix[g][time_slots];
321     if (time_slots <= 16)
322       dfrac = 0;
323     else
324       dfrac =
325           pstr_mps_state->ia_mps_dec_mps_table.tonality_table_ptr->dfrac_fix[g][time_slots - 16];
326 
327     qmf_real = pow_spec + QBX48;
328     qmf_imag = qmf_real + QMF_BANDSXTSX6;
329     for (q = 0; q < qmf_bands; q++) {
330       for (i = 0; i < 16; i++) {
331         if (nstart + i < 0) {
332           ton_state->win_buf_real[q][i] = ton_state->win_buf_real[q][16 + nstart + i];
333           ton_state->win_buf_imag[q][i] = ton_state->win_buf_imag[q][16 + nstart + i];
334         } else {
335           ton_state->win_buf_real[q][i] = qmf_real[nstart + i];
336           ton_state->win_buf_imag[q][i] = qmf_imag[nstart + i];
337         }
338       }
339       qmf_real += time_slots;
340       qmf_imag += time_slots;
341     }
342 
343     for (q = 0; q < qmf_bands; q++) {
344       ixheaacd_zoom_fft16(&(ton_state->win_buf_real[q][0]), &(ton_state->win_buf_imag[q][0]),
345                           &(spec_zoom_real[q * 8]), &(spec_zoom_imag[q * 8]), q, dfrac,
346                           &(pstr_mps_state->ia_mps_dec_mps_table));
347     }
348 
349     cnt = 8 * qmf_bands;
350     for (i = 0; i < cnt; i++) {
351       WORD64 temp;
352       WORD32 one_minus_beta = ONE_IN_Q15 - beta;
353       WORD32 x = *spec_zoom_real;
354       WORD32 y = *spec_zoom_imag;
355 
356       temp = x * spec_prev_real[i] + y * spec_prev_imag[i];
357       temp_1 = (WORD32)(temp >> 10);
358       temp_1 = ixheaacd_mps_mult32_shr_15(temp_1, beta);
359 
360       p_cross_real[i] = ixheaacd_mps_mult32_shr_15(p_cross_real[i], one_minus_beta);
361       p_cross_real[i] += temp_1;
362 
363       temp = y * spec_prev_real[i] - x * spec_prev_imag[i];
364       temp_1 = (WORD32)(temp >> 10);
365       temp_1 = ixheaacd_mps_mult32_shr_15(temp_1, beta);
366 
367       p_cross_imag[i] = ixheaacd_mps_mult32_shr_15(p_cross_imag[i], one_minus_beta);
368       p_cross_imag[i] += temp_1;
369 
370       temp = x * x + y * y;
371       temp_1 = (WORD32)(temp >> 10);
372       temp_1 = ixheaacd_mps_mult32_shr_15(temp_1, beta);
373 
374       p_sum[i] = ixheaacd_mps_mult32_shr_15(p_sum[i], one_minus_beta);
375       p_sum[i] += temp_1;
376 
377       *p_max = (p_sum[i] > p_sum_prev[i]) ? p_sum[i] : p_sum_prev[i];
378 
379       p_sum_prev[i] = p_sum[i];
380 
381       temp = p_cross_real[i] * p_cross_real[i] + p_cross_imag[i] * p_cross_imag[i];
382       temp_1 = (WORD32)(temp >> 10);
383       qtemp1 = 10;
384       temp_1 = ixheaacd_mps_sqrt(temp_1, &qtemp1, sqrt_tab);
385       *coh_spec = ixheaacd_mps_div_32(temp_1, *p_max++, &qtemp2);
386       qtemp2 = qtemp2 + qtemp1 - 10;
387       *coh_spec = ixheaacd_mps_convert_to_qn(*coh_spec, qtemp2, 10);
388       coh_spec++;
389 
390       temp = x * x + y * y + spec_prev_real[i] * spec_prev_real[i] +
391              spec_prev_imag[i] * spec_prev_imag[i];
392       *pow_spec = (WORD32)(temp >> 10);
393 
394       spec_prev_real[i] = *spec_zoom_real++;
395       spec_prev_imag[i] = *spec_zoom_imag++;
396     }
397     spec_zoom_real -= i;
398     spec_zoom_imag -= i;
399     p_max -= i;
400     coh_spec -= i;
401     pow_spec -= i;
402 
403     pstart = 0;
404     pqmf = 0;
405     for (i = 0; i < num_parameter_bands; i++) {
406       pqmf += part[i];
407       pstop = ((pqmf << 3) + ONE_IN_Q14) >> 15;
408 
409       num = 0;
410       den = 0;
411       for (j = pstart; j < pstop; j++) {
412         num += ixheaacd_mps_mult32_shr_n(*pow_spec, *coh_spec, 10);
413         coh_spec++;
414         den += *pow_spec++;
415       }
416 
417       tmp_ton = ixheaacd_mps_div_32(num, den, &q_tmp_ton);
418       ixheaacd_mps_convert_to_qn(tmp_ton, q_tmp_ton, 15);
419 
420       if (tmp_ton > 32767) {
421         tmp_ton = 32767;
422       }
423 
424       if (tmp_ton < tonality[i]) tonality[i] = tmp_ton;
425 
426       pstart = pstop;
427     }
428     coh_spec -= pstop;
429     pow_spec -= pstop;
430   }
431   return;
432 }
433