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 <ixheaacd_type_def.h>
21 #include "ixheaacd_bitbuffer.h"
22 #include "ixheaacd_interface.h"
23 #include "ixheaacd_sbr_common.h"
24 #include "ixheaacd_drc_data_struct.h"
25 #include "ixheaacd_drc_dec.h"
26 
27 #include "ixheaacd_sbr_const.h"
28 #include "ixheaacd_sbrdecsettings.h"
29 #include "ixheaacd_sbrdecoder.h"
30 #include "ixheaacd_env_extr_part.h"
31 #include <ixheaacd_sbr_rom.h>
32 #include "ixheaacd_common_rom.h"
33 #include "ixheaacd_hybrid.h"
34 #include "ixheaacd_sbr_scale.h"
35 #include "ixheaacd_ps_dec.h"
36 #include "ixheaacd_freq_sca.h"
37 #include "ixheaacd_lpp_tran.h"
38 #include "ixheaacd_bitbuffer.h"
39 #include "ixheaacd_env_extr.h"
40 #include "ixheaacd_qmf_dec.h"
41 #include "ixheaacd_env_calc.h"
42 #include "ixheaacd_pvc_dec.h"
43 #include "ixheaacd_sbr_dec.h"
44 #include "ixheaacd_qmf_poly.h"
45 #include "ixheaacd_esbr_rom.h"
46 
47 #include "string.h"
48 
ixheaacd_complex_anal_filt(ia_esbr_hbe_txposer_struct * ptr_hbe_txposer)49 WORD32 ixheaacd_complex_anal_filt(ia_esbr_hbe_txposer_struct *ptr_hbe_txposer) {
50   WORD32 idx;
51   WORD32 anal_size = 2 * ptr_hbe_txposer->synth_size;
52   WORD32 N = (10 * anal_size);
53 
54   for (idx = 0; idx < (ptr_hbe_txposer->no_bins >> 1); idx++) {
55     WORD32 i, j, k, l;
56     FLOAT32 window_output[640];
57     FLOAT32 u[128], u_in[256], u_out[256];
58     FLOAT32 accu_r, accu_i;
59     const FLOAT32 *inp_signal;
60     FLOAT32 *anal_buf;
61 
62     FLOAT32 *analy_cos_sin_tab = ptr_hbe_txposer->analy_cos_sin_tab;
63     const FLOAT32 *interp_window_coeff = ptr_hbe_txposer->analy_wind_coeff;
64     FLOAT32 *x = ptr_hbe_txposer->analy_buf;
65 
66     memset(ptr_hbe_txposer->qmf_in_buf[idx + HBE_OPER_WIN_LEN - 1], 0,
67            TWICE_QMF_SYNTH_CHANNELS_NUM * sizeof(FLOAT32));
68 
69     inp_signal = ptr_hbe_txposer->ptr_input_buf +
70                  idx * 2 * ptr_hbe_txposer->synth_size + 1;
71     anal_buf = &ptr_hbe_txposer->qmf_in_buf[idx + HBE_OPER_WIN_LEN - 1]
72                                            [4 * ptr_hbe_txposer->k_start];
73 
74     for (i = N - 1; i >= anal_size; i--) {
75       x[i] = x[i - anal_size];
76     }
77 
78     for (i = anal_size - 1; i >= 0; i--) {
79       x[i] = inp_signal[anal_size - 1 - i];
80     }
81 
82     for (i = 0; i < N; i++) {
83       window_output[i] = x[i] * interp_window_coeff[i];
84     }
85 
86     for (i = 0; i < 2 * anal_size; i++) {
87       accu_r = 0.0;
88       for (j = 0; j < 5; j++) {
89         accu_r = accu_r + window_output[i + j * 2 * anal_size];
90       }
91       u[i] = accu_r;
92     }
93 
94     if (anal_size == 40) {
95       for (i = 1; i < anal_size; i++) {
96         FLOAT32 temp1 = u[i] + u[2 * anal_size - i];
97         FLOAT32 temp2 = u[i] - u[2 * anal_size - i];
98         u[i] = temp1;
99         u[2 * anal_size - i] = temp2;
100       }
101 
102       for (k = 0; k < anal_size; k++) {
103         accu_r = u[anal_size];
104         if (k & 1)
105           accu_i = u[0];
106         else
107           accu_i = -u[0];
108         for (l = 1; l < anal_size; l++) {
109           accu_r = accu_r + u[0 + l] * analy_cos_sin_tab[2 * l + 0];
110           accu_i = accu_i + u[2 * anal_size - l] * analy_cos_sin_tab[2 * l + 1];
111         }
112         analy_cos_sin_tab += (2 * anal_size);
113         *anal_buf++ = (FLOAT32)accu_r;
114         *anal_buf++ = (FLOAT32)accu_i;
115       }
116     } else {
117       FLOAT32 *ptr_u = u_in;
118       FLOAT32 *ptr_v = u_out;
119       for (k = 0; k < anal_size * 2; k++) {
120         *ptr_u++ = ((*analy_cos_sin_tab++) * u[k]);
121         *ptr_u++ = ((*analy_cos_sin_tab++) * u[k]);
122       }
123       if (ixheaacd_cmplx_anal_fft != NULL)
124         (*ixheaacd_cmplx_anal_fft)(u_in, u_out, anal_size * 2);
125       else
126         return -1;
127 
128       for (k = 0; k < anal_size / 2; k++) {
129         *(anal_buf + 1) = -*ptr_v++;
130         *anal_buf = *ptr_v++;
131 
132         anal_buf += 2;
133 
134         *(anal_buf + 1) = *ptr_v++;
135         *anal_buf = -*ptr_v++;
136 
137         anal_buf += 2;
138       }
139     }
140   }
141   return 0;
142 }
143 
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])144 WORD32 ixheaacd_real_synth_filt(ia_esbr_hbe_txposer_struct *ptr_hbe_txposer,
145                                 WORD32 num_columns, FLOAT32 qmf_buf_real[][64],
146                                 FLOAT32 qmf_buf_imag[][64]) {
147   WORD32 i, j, k, l, idx;
148   FLOAT32 g[640];
149   FLOAT32 w[640];
150   FLOAT32 synth_out[128];
151   FLOAT32 accu_r;
152   WORD32 synth_size = ptr_hbe_txposer->synth_size;
153   FLOAT32 *ptr_cos_tab_trans_qmf =
154       (FLOAT32 *)&ixheaacd_cos_table_trans_qmf[0][0] +
155       ptr_hbe_txposer->k_start * 32;
156   FLOAT32 *buffer = ptr_hbe_txposer->synth_buf;
157 
158   for (idx = 0; idx < num_columns; idx++) {
159     FLOAT32 loc_qmf_buf[64];
160     FLOAT32 *synth_buf_r = loc_qmf_buf;
161     FLOAT32 *out_buf = ptr_hbe_txposer->ptr_input_buf +
162                        (idx + 1) * ptr_hbe_txposer->synth_size;
163     FLOAT32 *synth_cos_tab = ptr_hbe_txposer->synth_cos_tab;
164     const FLOAT32 *interp_window_coeff = ptr_hbe_txposer->synth_wind_coeff;
165     if (ptr_hbe_txposer->k_start < 0) return -1;
166     for (k = 0; k < synth_size; k++) {
167       WORD32 ki = ptr_hbe_txposer->k_start + k;
168       synth_buf_r[k] = (FLOAT32)(
169           ptr_cos_tab_trans_qmf[(k << 1) + 0] * qmf_buf_real[idx][ki] +
170           ptr_cos_tab_trans_qmf[(k << 1) + 1] * qmf_buf_imag[idx][ki]);
171 
172       synth_buf_r[k + ptr_hbe_txposer->synth_size] = 0;
173     }
174 
175     for (l = (20 * synth_size - 1); l >= 2 * synth_size; l--) {
176       buffer[l] = buffer[l - 2 * synth_size];
177     }
178 
179     if (synth_size == 20) {
180       FLOAT32 *psynth_cos_tab = synth_cos_tab;
181 
182       for (l = 0; l < (synth_size + 1); l++) {
183         accu_r = 0.0;
184         for (k = 0; k < synth_size; k++) {
185           accu_r += synth_buf_r[k] * psynth_cos_tab[k];
186         }
187         buffer[0 + l] = accu_r;
188         buffer[synth_size - l] = accu_r;
189         psynth_cos_tab = psynth_cos_tab + synth_size;
190       }
191       for (l = (synth_size + 1); l < (2 * synth_size - synth_size / 2); l++) {
192         accu_r = 0.0;
193         for (k = 0; k < synth_size; k++) {
194           accu_r += synth_buf_r[k] * psynth_cos_tab[k];
195         }
196         buffer[0 + l] = accu_r;
197         buffer[3 * synth_size - l] = -accu_r;
198         psynth_cos_tab = psynth_cos_tab + synth_size;
199       }
200       accu_r = 0.0;
201       for (k = 0; k < synth_size; k++) {
202         accu_r += synth_buf_r[k] * psynth_cos_tab[k];
203       }
204       buffer[3 * synth_size >> 1] = accu_r;
205     } else {
206       FLOAT32 tmp;
207       FLOAT32 *ptr_u = synth_out;
208       WORD32 kmax = (synth_size >> 1);
209       FLOAT32 *syn_buf = &buffer[kmax];
210       kmax += synth_size;
211 
212       if (ixheaacd_real_synth_fft != NULL)
213         (*ixheaacd_real_synth_fft)(synth_buf_r, synth_out, synth_size * 2);
214       else
215         return -1;
216 
217       for (k = 0; k < kmax; k++) {
218         tmp = ((*ptr_u++) * (*synth_cos_tab++));
219         tmp -= ((*ptr_u++) * (*synth_cos_tab++));
220         *syn_buf++ = tmp;
221       }
222 
223       syn_buf = &buffer[0];
224       kmax -= synth_size;
225 
226       for (k = 0; k < kmax; k++) {
227         tmp = ((*ptr_u++) * (*synth_cos_tab++));
228         tmp -= ((*ptr_u++) * (*synth_cos_tab++));
229         *syn_buf++ = tmp;
230       }
231     }
232 
233     for (i = 0; i < 5; i++) {
234       memcpy(&g[(2 * i + 0) * synth_size], &buffer[(4 * i + 0) * synth_size],
235              sizeof(FLOAT32) * synth_size);
236       memcpy(&g[(2 * i + 1) * synth_size], &buffer[(4 * i + 3) * synth_size],
237              sizeof(FLOAT32) * synth_size);
238     }
239 
240     for (k = 0; k < 10 * synth_size; k++) {
241       w[k] = g[k] * interp_window_coeff[k];
242     }
243 
244     for (i = 0; i < synth_size; i++) {
245       accu_r = 0.0;
246       for (j = 0; j < 10; j++) {
247         accu_r = accu_r + w[synth_size * j + i];
248       }
249       out_buf[i] = (FLOAT32)accu_r;
250     }
251   }
252   return 0;
253 }
254