• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *                                                                            *
3  * Copyright (C) 2018 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_bitbuffer.h"
23 #include "ixheaacd_interface.h"
24 #include "ixheaacd_sbr_common.h"
25 #include "ixheaacd_drc_data_struct.h"
26 #include "ixheaacd_drc_dec.h"
27 
28 #include "ixheaacd_sbr_const.h"
29 #include "ixheaacd_sbrdecsettings.h"
30 #include "ixheaacd_sbrdecoder.h"
31 #include "ixheaacd_env_extr_part.h"
32 #include "ixheaacd_sbr_rom.h"
33 #include "ixheaacd_common_rom.h"
34 #include "ixheaacd_hybrid.h"
35 #include "ixheaacd_sbr_scale.h"
36 #include "ixheaacd_ps_dec.h"
37 #include "ixheaacd_freq_sca.h"
38 #include "ixheaacd_lpp_tran.h"
39 #include "ixheaacd_bitbuffer.h"
40 #include "ixheaacd_env_extr.h"
41 #include "ixheaacd_qmf_dec.h"
42 #include "ixheaacd_env_calc.h"
43 #include "ixheaacd_pvc_dec.h"
44 #include "ixheaacd_sbr_dec.h"
45 #include "ixheaacd_qmf_poly.h"
46 #include "ixheaacd_esbr_rom.h"
47 
ixheaacd_complex_anal_filt(ia_esbr_hbe_txposer_struct * ptr_hbe_txposer)48 WORD32 ixheaacd_complex_anal_filt(ia_esbr_hbe_txposer_struct *ptr_hbe_txposer) {
49   WORD32 idx;
50   WORD32 anal_size = 2 * ptr_hbe_txposer->synth_size;
51   WORD32 N = (10 * anal_size);
52   WORD32 no_bins = ptr_hbe_txposer->no_bins >> 1;
53 
54   if (ptr_hbe_txposer->esbr_hq != 0) {
55     anal_size = 2 * ptr_hbe_txposer->analy_size;
56     no_bins = ptr_hbe_txposer->no_bins;
57   }
58 
59   for (idx = 0; idx < no_bins; idx++) {
60     WORD32 i, j, k, l;
61     FLOAT32 window_output[640];
62     FLOAT32 u[128], u_in[256], u_out[256];
63     FLOAT32 accu_r, accu_i;
64     const FLOAT32 *inp_signal;
65     FLOAT32 *anal_buf;
66 
67     FLOAT32 *analy_cos_sin_tab = ptr_hbe_txposer->analy_cos_sin_tab;
68     const FLOAT32 *interp_window_coeff = ptr_hbe_txposer->analy_wind_coeff;
69     FLOAT32 *x = ptr_hbe_txposer->analy_buf;
70     if (ptr_hbe_txposer->esbr_hq != 0) {
71       memset(ptr_hbe_txposer->qmf_in_buf[idx], 0,
72           TWICE_QMF_SYNTH_CHANNELS_NUM * sizeof(FLOAT32));
73       inp_signal = ptr_hbe_txposer->ptr_output_buf +
74           idx * ptr_hbe_txposer->analy_size + 1;
75       anal_buf = &ptr_hbe_txposer->qmf_in_buf[idx]
76           [4 * ptr_hbe_txposer->a_start];
77     } else {
78     memset(ptr_hbe_txposer->qmf_in_buf[idx + HBE_OPER_WIN_LEN - 1], 0,
79            TWICE_QMF_SYNTH_CHANNELS_NUM * sizeof(FLOAT32));
80 
81     inp_signal = ptr_hbe_txposer->ptr_input_buf +
82                  idx * 2 * ptr_hbe_txposer->synth_size + 1;
83     anal_buf = &ptr_hbe_txposer->qmf_in_buf[idx + HBE_OPER_WIN_LEN - 1]
84                                            [4 * ptr_hbe_txposer->k_start];
85     }
86 
87     for (i = N - 1; i >= anal_size; i--) {
88       x[i] = x[i - anal_size];
89     }
90 
91     for (i = anal_size - 1; i >= 0; i--) {
92       x[i] = inp_signal[anal_size - 1 - i];
93     }
94 
95     for (i = 0; i < N; i++) {
96       window_output[i] = x[i] * interp_window_coeff[i];
97     }
98 
99     for (i = 0; i < 2 * anal_size; i++) {
100       accu_r = 0.0;
101       for (j = 0; j < 5; j++) {
102         accu_r = accu_r + window_output[i + j * 2 * anal_size];
103       }
104       u[i] = accu_r;
105     }
106     if (anal_size == 40 || anal_size == 56) {
107       for (i = 1; i < anal_size; i++) {
108         FLOAT32 temp1 = u[i] + u[2 * anal_size - i];
109         FLOAT32 temp2 = u[i] - u[2 * anal_size - i];
110         u[i] = temp1;
111         u[2 * anal_size - i] = temp2;
112       }
113 
114       for (k = 0; k < anal_size; k++) {
115         accu_r = u[anal_size];
116         if (k & 1)
117           accu_i = u[0];
118         else
119           accu_i = -u[0];
120         for (l = 1; l < anal_size; l++) {
121           accu_r = accu_r + u[0 + l] * analy_cos_sin_tab[2 * l + 0];
122           accu_i = accu_i + u[2 * anal_size - l] * analy_cos_sin_tab[2 * l + 1];
123         }
124         analy_cos_sin_tab += (2 * anal_size);
125         *anal_buf++ = (FLOAT32)accu_r;
126         *anal_buf++ = (FLOAT32)accu_i;
127       }
128     } else {
129       FLOAT32 *ptr_u = u_in;
130       FLOAT32 *ptr_v = u_out;
131       for (k = 0; k < anal_size * 2; k++) {
132         *ptr_u++ = ((*analy_cos_sin_tab++) * u[k]);
133         *ptr_u++ = ((*analy_cos_sin_tab++) * u[k]);
134       }
135       if (ptr_hbe_txposer->ixheaacd_cmplx_anal_fft != NULL)
136         (*(ptr_hbe_txposer->ixheaacd_cmplx_anal_fft))(u_in, u_out,
137                                                       anal_size * 2);
138       else
139         return -1;
140 
141       for (k = 0; k < anal_size / 2; k++) {
142         *(anal_buf + 1) = -*ptr_v++;
143         *anal_buf = *ptr_v++;
144 
145         anal_buf += 2;
146 
147         *(anal_buf + 1) = *ptr_v++;
148         *anal_buf = -*ptr_v++;
149 
150         anal_buf += 2;
151       }
152     }
153   }
154   return 0;
155 }
156 
ixheaacd_real_synth_filt(ia_esbr_hbe_txposer_struct * ptr_hbe_txposer,WORD32 num_columns,FLOAT32 qmf_buf_real[][64],FLOAT32 qmf_buf_imag[][64])157 WORD32 ixheaacd_real_synth_filt(ia_esbr_hbe_txposer_struct *ptr_hbe_txposer,
158                                 WORD32 num_columns, FLOAT32 qmf_buf_real[][64],
159                                 FLOAT32 qmf_buf_imag[][64]) {
160   WORD32 i, j, k, l, idx;
161   FLOAT32 g[640];
162   FLOAT32 w[640];
163   FLOAT32 synth_out[128];
164   FLOAT32 accu_r;
165   WORD32 synth_size = ptr_hbe_txposer->synth_size;
166   FLOAT32 *ptr_cos_tab_trans_qmf =
167       (FLOAT32 *)&ixheaacd_cos_table_trans_qmf[0][0] +
168       ptr_hbe_txposer->k_start * 32;
169   FLOAT32 *buffer = ptr_hbe_txposer->synth_buf;
170   FLOAT32 *ptr_inp_buf = ptr_hbe_txposer->ptr_input_buf + ptr_hbe_txposer->ana_fft_size[0];
171 
172   for (idx = 0; idx < num_columns; idx++) {
173     FLOAT32 loc_qmf_buf[64];
174     FLOAT32 *synth_buf_r = loc_qmf_buf;
175     FLOAT32 *out_buf;
176     if (ptr_hbe_txposer->esbr_hq == 1) {
177         out_buf = ptr_inp_buf +
178                   (idx - 1) * ptr_hbe_txposer->synth_size;
179     } else {
180       out_buf = ptr_hbe_txposer->ptr_input_buf +
181                        (idx + 1) * ptr_hbe_txposer->synth_size;
182     }
183     FLOAT32 *synth_cos_tab = ptr_hbe_txposer->synth_cos_tab;
184     const FLOAT32 *interp_window_coeff = ptr_hbe_txposer->synth_wind_coeff;
185     if (ptr_hbe_txposer->k_start < 0) return -1;
186     for (k = 0; k < synth_size; k++) {
187       WORD32 ki = ptr_hbe_txposer->k_start + k;
188       synth_buf_r[k] = (FLOAT32)(
189           ptr_cos_tab_trans_qmf[(k << 1) + 0] * qmf_buf_real[idx][ki] +
190           ptr_cos_tab_trans_qmf[(k << 1) + 1] * qmf_buf_imag[idx][ki]);
191 
192       synth_buf_r[k + ptr_hbe_txposer->synth_size] = 0;
193     }
194 
195     for (l = (20 * synth_size - 1); l >= 2 * synth_size; l--) {
196       buffer[l] = buffer[l - 2 * synth_size];
197     }
198 
199     if (synth_size == 20) {
200       FLOAT32 *psynth_cos_tab = synth_cos_tab;
201 
202       for (l = 0; l < (synth_size + 1); l++) {
203         accu_r = 0.0;
204         for (k = 0; k < synth_size; k++) {
205           accu_r += synth_buf_r[k] * psynth_cos_tab[k];
206         }
207         buffer[0 + l] = accu_r;
208         buffer[synth_size - l] = accu_r;
209         psynth_cos_tab = psynth_cos_tab + synth_size;
210       }
211       for (l = (synth_size + 1); l < (2 * synth_size - synth_size / 2); l++) {
212         accu_r = 0.0;
213         for (k = 0; k < synth_size; k++) {
214           accu_r += synth_buf_r[k] * psynth_cos_tab[k];
215         }
216         buffer[0 + l] = accu_r;
217         buffer[3 * synth_size - l] = -accu_r;
218         psynth_cos_tab = psynth_cos_tab + synth_size;
219       }
220       accu_r = 0.0;
221       for (k = 0; k < synth_size; k++) {
222         accu_r += synth_buf_r[k] * psynth_cos_tab[k];
223       }
224       buffer[3 * synth_size >> 1] = accu_r;
225     } else {
226       FLOAT32 tmp;
227       FLOAT32 *ptr_u = synth_out;
228       WORD32 kmax = (synth_size >> 1);
229       FLOAT32 *syn_buf = &buffer[kmax];
230       kmax += synth_size;
231 
232       if (ptr_hbe_txposer->ixheaacd_real_synth_fft != NULL)
233         (*(ptr_hbe_txposer->ixheaacd_real_synth_fft))(synth_buf_r, synth_out,
234                                                       synth_size * 2);
235       else
236         return -1;
237 
238       for (k = 0; k < kmax; k++) {
239         tmp = ((*ptr_u++) * (*synth_cos_tab++));
240         tmp -= ((*ptr_u++) * (*synth_cos_tab++));
241         *syn_buf++ = tmp;
242       }
243 
244       syn_buf = &buffer[0];
245       kmax -= synth_size;
246 
247       for (k = 0; k < kmax; k++) {
248         tmp = ((*ptr_u++) * (*synth_cos_tab++));
249         tmp -= ((*ptr_u++) * (*synth_cos_tab++));
250         *syn_buf++ = tmp;
251       }
252     }
253 
254     for (i = 0; i < 5; i++) {
255       memcpy(&g[(2 * i + 0) * synth_size], &buffer[(4 * i + 0) * synth_size],
256              sizeof(FLOAT32) * synth_size);
257       memcpy(&g[(2 * i + 1) * synth_size], &buffer[(4 * i + 3) * synth_size],
258              sizeof(FLOAT32) * synth_size);
259     }
260 
261     for (k = 0; k < 10 * synth_size; k++) {
262       w[k] = g[k] * interp_window_coeff[k];
263     }
264 
265     for (i = 0; i < synth_size; i++) {
266       accu_r = 0.0;
267       for (j = 0; j < 10; j++) {
268         accu_r = accu_r + w[synth_size * j + i];
269       }
270       out_buf[i] = (FLOAT32)accu_r;
271     }
272   }
273   return 0;
274 }
275 
ixheaacd_dft_hbe_cplx_anal_filt(ia_esbr_hbe_txposer_struct * ptr_hbe_txposer,FLOAT32 qmf_buf_real[][64],FLOAT32 qmf_buf_imag[][64])276 WORD32 ixheaacd_dft_hbe_cplx_anal_filt(ia_esbr_hbe_txposer_struct *ptr_hbe_txposer,
277                                        FLOAT32 qmf_buf_real[][64],
278                                        FLOAT32 qmf_buf_imag[][64]) {
279   WORD32 idx;
280 
281   WORD32 anal_size = ptr_hbe_txposer->analy_size;
282 
283   WORD32 N = (10 * ptr_hbe_txposer->analy_size);
284 
285   for (idx = 0; idx < ptr_hbe_txposer->no_bins; idx++) {
286     WORD32 i, j, k, l;
287     FLOAT32 window_output[640];
288     FLOAT32 u[128];
289     FLOAT32 accu_r, accu_i;
290     const FLOAT32 *inp_signal;
291     FLOAT32 *qmf_buf_r = &qmf_buf_real[idx][ptr_hbe_txposer->a_start];
292     FLOAT32 *qmf_buf_i = &qmf_buf_imag[idx][ptr_hbe_txposer->a_start];
293 
294     const FLOAT32 *interp_window_coeff = ptr_hbe_txposer->analy_wind_coeff;
295     FLOAT32 *x = ptr_hbe_txposer->analy_buf;
296 
297     memset(&qmf_buf_real[idx][ptr_hbe_txposer->a_start], 0,
298         (NO_QMF_SYNTH_CHANNELS - ptr_hbe_txposer->a_start) *
299         sizeof(qmf_buf_real[idx][ptr_hbe_txposer->a_start]));
300     memset(&qmf_buf_imag[idx][ptr_hbe_txposer->a_start], 0,
301         TWICE_QMF_SYNTH_CHANNELS_NUM * sizeof(qmf_buf_imag[idx][ptr_hbe_txposer->a_start]));
302 
303     inp_signal = ptr_hbe_txposer->ptr_output_buf +
304         idx * ptr_hbe_txposer->analy_size + 1;
305 
306     for (i = N - 1; i >= anal_size; i--) {
307         x[i] = x[i - anal_size];
308     }
309 
310     for (i = anal_size - 1; i >= 0; i--) {
311         x[i] = inp_signal[anal_size - 1 - i];
312     }
313 
314     for (i = 0; i < N; i++) {
315         window_output[i] = x[i] * interp_window_coeff[i];
316     }
317 
318     for (i = 0; i < 2 * anal_size; i++) {
319       accu_r = 0.0;
320       for (j = 0; j < 5; j++) {
321         accu_r = accu_r + window_output[i + j * 2 * anal_size];
322       }
323       u[i] = accu_r;
324     }
325 
326     for (k = 0; k < anal_size; k++) {
327       accu_r = 0;
328       accu_i = 0;
329       for (l = 0; l < 2 * anal_size; l++) {
330         accu_r = accu_r + u[l] * ptr_hbe_txposer->str_dft_hbe_anal_coeff.real[k][l];
331         accu_i = accu_i + u[l] * ptr_hbe_txposer->str_dft_hbe_anal_coeff.imag[k][l];
332       }
333       qmf_buf_r[k] = (FLOAT32)accu_r;
334       qmf_buf_i[k] = (FLOAT32)accu_i;
335     }
336   }
337   return 0;
338 }
339