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
21 #include <math.h>
22 #include <string.h>
23 #include "ixheaac_type_def.h"
24 #include "ixheaace_adjust_threshold_data.h"
25 #include "iusace_bitbuffer.h"
26 #include "ixheaace_mps_common_define.h"
27 /* DRC */
28 #include "impd_drc_common_enc.h"
29 #include "impd_drc_uni_drc.h"
30 #include "impd_drc_tables.h"
31 #include "impd_drc_api.h"
32 #include "impd_drc_uni_drc_eq.h"
33 #include "impd_drc_uni_drc_filter_bank.h"
34 #include "impd_drc_gain_enc.h"
35 #include "impd_drc_struct_def.h"
36
37 #include "iusace_cnst.h"
38 #include "iusace_tns_usac.h"
39 #include "iusace_psy_mod.h"
40 #include "iusace_ms.h"
41 #include "iusace_fd_qc_util.h"
42 #include "ixheaace_memory_standards.h"
43 #include "iusace_config.h"
44 #include "iusace_tcx_mdct.h"
45 #include "iusace_arith_enc.h"
46 #include "iusace_fd_quant.h"
47 #include "iusace_signal_classifier.h"
48 #include "iusace_block_switch_const.h"
49 #include "iusace_block_switch_struct_def.h"
50 #include "ixheaace_sbr_header.h"
51 #include "ixheaace_config.h"
52 #include "ixheaace_asc_write.h"
53 #include "iusace_main.h"
54 #include "iusace_func_prototypes.h"
55 #include "iusace_lpd_rom.h"
56 #include "iusace_lpd.h"
57 #include "iusace_avq_enc.h"
58 #include "ixheaac_error_standards.h"
59 #include "ixheaace_error_codes.h"
60
iusace_decode_fd_fac(WORD32 * ptr_fac_prms,WORD32 len_subfrm,WORD32 fac_len,FLOAT32 * ptr_lpc_coeffs,FLOAT32 * zir_sig,FLOAT32 * ptr_fac_dec,iusace_scratch_mem * pstr_scratch)61 static VOID iusace_decode_fd_fac(WORD32 *ptr_fac_prms, WORD32 len_subfrm, WORD32 fac_len,
62 FLOAT32 *ptr_lpc_coeffs, FLOAT32 *zir_sig, FLOAT32 *ptr_fac_dec,
63 iusace_scratch_mem *pstr_scratch) {
64 FLOAT32 *x = pstr_scratch->p_x;
65 FLOAT32 *xn2 = pstr_scratch->p_xn_2;
66 FLOAT32 fac_gain;
67 WORD32 i;
68 const FLOAT32 *sin_window;
69 FLOAT32 *fac_window = pstr_scratch->p_fac_window;
70 FLOAT32 ap[ORDER + 1];
71
72 if (fac_len == 64) {
73 sin_window = iusace_sin_window_128;
74 } else {
75 sin_window = iusace_sin_window_256;
76 }
77
78 if (ptr_lpc_coeffs != NULL && ptr_fac_dec != NULL) {
79 fac_gain = (FLOAT32)pow(10.0f, ((FLOAT32)ptr_fac_prms[0]) / 28.0f);
80 for (i = 0; i < fac_len; i++) {
81 x[i] = (FLOAT32)ptr_fac_prms[i + 1] * fac_gain;
82 }
83
84 iusace_tcx_mdct(x, xn2, fac_len, pstr_scratch);
85
86 iusace_get_weighted_lpc(ptr_lpc_coeffs, ap);
87
88 memset(xn2 + fac_len, 0, fac_len * sizeof(FLOAT32));
89 iusace_synthesis_tool_float(ap, xn2, ptr_fac_dec, 2 * fac_len, xn2 + fac_len,
90 pstr_scratch->p_buf_synthesis_tool);
91
92 if (zir_sig != NULL) {
93 for (i = 0; i < fac_len; i++) {
94 fac_window[i] = sin_window[i] * sin_window[(2 * fac_len) - 1 - i];
95 fac_window[fac_len + i] = 1.0f - (sin_window[fac_len + i] * sin_window[fac_len + i]);
96 }
97 for (i = 0; i < fac_len; i++) {
98 ptr_fac_dec[i] += zir_sig[1 + (len_subfrm / 2) + i] * fac_window[fac_len + i] +
99 zir_sig[1 + (len_subfrm / 2) - 1 - i] * fac_window[fac_len - 1 - i];
100 }
101 }
102 }
103
104 return;
105 }
106
iusace_fac_apply(FLOAT32 * orig,WORD32 len_subfrm,WORD32 fac_len,WORD32 low_pass_line,WORD32 target_br,FLOAT32 * synth,FLOAT32 * ptr_lpc_coeffs,WORD16 * fac_bits_word,WORD32 * num_fac_bits,iusace_scratch_mem * pstr_scratch)107 VOID iusace_fac_apply(FLOAT32 *orig, WORD32 len_subfrm, WORD32 fac_len, WORD32 low_pass_line,
108 WORD32 target_br, FLOAT32 *synth, FLOAT32 *ptr_lpc_coeffs,
109 WORD16 *fac_bits_word, WORD32 *num_fac_bits,
110 iusace_scratch_mem *pstr_scratch) {
111 FLOAT32 *xn2 = pstr_scratch->p_xn2;
112 FLOAT32 *fac_dec = pstr_scratch->p_fac_dec;
113 FLOAT32 *right_fac_spec = pstr_scratch->p_right_fac_spec;
114 FLOAT32 *x2 = pstr_scratch->p_x2;
115 WORD32 *param = pstr_scratch->p_param;
116 FLOAT32 ap[ORDER + 1];
117 FLOAT32 fac_gain;
118 WORD32 i, index;
119 WORD32 num_enc_bits = 0;
120 WORD32 start_right = 2 * len_subfrm - fac_len;
121
122 *num_fac_bits = 0;
123
124 memset(xn2, 0, (FAC_LENGTH + ORDER) * sizeof(FLOAT32));
125
126 memcpy(xn2 + ORDER, &orig[start_right], fac_len * sizeof(FLOAT32));
127 for (i = 0; i < fac_len; i++) {
128 xn2[ORDER + i] -= synth[start_right + i];
129 }
130
131 iusace_get_weighted_lpc(ptr_lpc_coeffs, ap);
132 iusace_compute_lp_residual(ap, xn2 + ORDER, x2, fac_len);
133 for (i = 0; i < fac_len; i++) {
134 x2[i] = x2[i] * (2.0f / (FLOAT32)fac_len);
135 }
136
137 iusace_tcx_mdct(x2, right_fac_spec, fac_len, pstr_scratch);
138
139 memset(&right_fac_spec[low_pass_line], 0, (fac_len - low_pass_line) * sizeof(FLOAT32));
140
141 fac_gain = iusace_calc_sq_gain(right_fac_spec, target_br, fac_len, pstr_scratch->p_sq_gain_en);
142 index = (WORD32)floor(0.5f + (28.0f * (FLOAT32)log10(fac_gain)));
143 if (index < 0) index = 0;
144 if (index > 127) index = 127;
145 param[0] = index;
146 fac_gain = (FLOAT32)pow(10.0f, ((FLOAT32)index) / 28.0f);
147 for (i = 0; i < fac_len; i++) right_fac_spec[i] /= fac_gain;
148
149 for (i = 0; i < fac_len; i += 8) {
150 iusace_find_nearest_neighbor(&right_fac_spec[i], ¶m[i + 1]);
151 }
152
153 iusace_write_bits2buf(index, 7, fac_bits_word);
154 num_enc_bits += 7;
155 num_enc_bits += iusace_fd_encode_fac(¶m[1], &fac_bits_word[7], fac_len);
156 iusace_decode_fd_fac(¶m[0], len_subfrm, fac_len, ptr_lpc_coeffs, NULL, fac_dec,
157 pstr_scratch);
158 *num_fac_bits = num_enc_bits;
159
160 for (i = 0; i < fac_len; i++) {
161 synth[start_right + i] += fac_dec[i];
162 }
163 return;
164 }
165
iusace_fd_fac(WORD32 * sfb_offsets,WORD32 sfb_active,FLOAT64 * orig_sig_dbl,WORD32 window_sequence,FLOAT64 * synth_time,ia_usac_td_encoder_struct * pstr_acelp,WORD32 last_subfr_was_acelp,WORD32 next_frm_lpd,WORD16 * fac_prm_out,WORD32 * num_fac_bits,iusace_scratch_mem * pstr_scratch)166 IA_ERRORCODE iusace_fd_fac(WORD32 *sfb_offsets, WORD32 sfb_active, FLOAT64 *orig_sig_dbl,
167 WORD32 window_sequence, FLOAT64 *synth_time,
168 ia_usac_td_encoder_struct *pstr_acelp, WORD32 last_subfr_was_acelp,
169 WORD32 next_frm_lpd, WORD16 *fac_prm_out, WORD32 *num_fac_bits,
170 iusace_scratch_mem *pstr_scratch) {
171 const FLOAT32 *sin_window = NULL;
172 LOOPIDX i;
173 FLOAT32 *zir_sig = NULL;
174 FLOAT32 *lpc_coeffs_q = NULL;
175 WORD32 index;
176 WORD32 low_pass_line = 0;
177 WORD32 fac_len;
178 FLOAT64 *left_fac_time_data = pstr_scratch->p_left_fac_time_data;
179 FLOAT32 *left_fac_timedata_flt = pstr_scratch->p_left_fac_timedata_flt;
180 FLOAT32 *left_fac_spec = pstr_scratch->p_left_fac_spec;
181 FLOAT64 *fac_win = pstr_scratch->p_fac_win;
182 WORD32 *fac_prm = pstr_scratch->p_fac_prm;
183 WORD16 *fac_bits_word = pstr_scratch->p_fac_bits_word;
184 FLOAT32 *acelp_folded = pstr_scratch->p_acelp_folded_scratch;
185
186 *num_fac_bits = 0;
187
188 if (last_subfr_was_acelp || next_frm_lpd)
189 {
190 if (window_sequence == EIGHT_SHORT_SEQUENCE)
191 fac_len = (pstr_acelp->len_frame / 16);
192 else
193 fac_len = (pstr_acelp->len_frame / 8);
194
195 low_pass_line = (WORD32)(sfb_offsets[sfb_active] * fac_len /
196 (FLOAT32)pstr_acelp->len_frame);
197 }
198
199 if (last_subfr_was_acelp) {
200 FLOAT32 *tmp_lp_res = pstr_scratch->ptr_tmp_lp_res;
201 FLOAT32 lpc_coeffs[ORDER + 1];
202 FLOAT32 ener, fac_gain;
203 WORD32 left_start;
204
205 switch (fac_len) {
206 case 48:
207 sin_window = iusace_sin_window_96;
208 break;
209 case 64:
210 sin_window = iusace_sin_window_128;
211 break;
212 case 96:
213 sin_window = iusace_sin_window_192;
214 break;
215 case 128:
216 sin_window = iusace_sin_window_256;
217 break;
218 default:
219 return IA_EXHEAACE_EXE_FATAL_USAC_INVALID_FAC_LEN;
220 }
221
222 for (i = 0; i < fac_len; i++) {
223 fac_win[i] = sin_window[i] * sin_window[(2 * fac_len) - 1 - i];
224 fac_win[fac_len + i] = 1.0f - (sin_window[fac_len + i] * sin_window[fac_len + i]);
225 }
226
227 left_start = (pstr_acelp->len_frame / 2) - fac_len - ORDER;
228
229 for (i = 0; i < 2 * fac_len + ORDER; i++) {
230 left_fac_time_data[i] = orig_sig_dbl[left_start + i];
231 }
232
233 for (i = 0; i < fac_len; i++) {
234 left_fac_time_data[fac_len + ORDER + i] =
235 left_fac_time_data[fac_len + ORDER + i] - synth_time[left_start + fac_len + ORDER + i];
236 }
237
238 zir_sig = pstr_acelp->lpd_state.tcx_quant;
239
240 for (i = 0; i < ORDER; i++) {
241 left_fac_time_data[fac_len + i] =
242 left_fac_time_data[fac_len + i] - zir_sig[1 + 128 - ORDER + i];
243 }
244
245 for (i = 0; i < fac_len; i++) {
246 acelp_folded[i] = zir_sig[1 + 128 + i] * (FLOAT32)fac_win[fac_len + i] +
247 zir_sig[1 + 128 - 1 - i] * (FLOAT32)fac_win[fac_len - 1 - i];
248 }
249
250 {
251 FLOAT32 ener_tmp;
252 ener = 0.0f;
253 ener_tmp = 0.0f;
254
255 for (i = 0; i < fac_len; i++) {
256 ener += (FLOAT32)(left_fac_time_data[i + ORDER + fac_len] *
257 left_fac_time_data[i + ORDER + fac_len]);
258 }
259 ener *= 2.0f;
260
261 for (i = 0; i < fac_len; i++) {
262 ener_tmp += acelp_folded[i] * acelp_folded[i];
263 }
264
265 if (ener_tmp > ener)
266 fac_gain = (FLOAT32)sqrt(ener / ener_tmp);
267 else
268 fac_gain = 1.0f;
269
270 for (i = 0; i < fac_len; i++) {
271 left_fac_time_data[i + ORDER + fac_len] -= fac_gain * acelp_folded[i];
272 }
273 }
274
275 for (i = 0; i < 2 * fac_len + ORDER; i++) {
276 left_fac_timedata_flt[i] = (FLOAT32)left_fac_time_data[i];
277 }
278
279 lpc_coeffs_q = pstr_acelp->lpd_state.lpc_coeffs_quant;
280 lpc_coeffs_q += ORDER + 1;
281 iusace_get_weighted_lpc(lpc_coeffs_q, lpc_coeffs);
282 iusace_compute_lp_residual(lpc_coeffs, left_fac_timedata_flt + ORDER + fac_len, tmp_lp_res,
283 fac_len);
284 FLOAT32 coeff = (2.0f / (FLOAT32)fac_len);
285 for (i = 0; i < fac_len; i++) {
286 tmp_lp_res[i] = tmp_lp_res[i] * coeff;
287 }
288
289 iusace_tcx_mdct(tmp_lp_res, left_fac_spec, fac_len, pstr_scratch);
290 memset(&left_fac_spec[low_pass_line], 0, (fac_len - low_pass_line) * sizeof(FLOAT32));
291
292 fac_gain = iusace_calc_sq_gain(left_fac_spec, 240, fac_len, pstr_scratch->p_sq_gain_en);
293
294 index = (WORD32)floor(0.5f + (28.0f * (FLOAT32)log10(fac_gain)));
295 if (index < 0) index = 0;
296 if (index > 127) index = 127;
297 iusace_write_bits2buf(index, 7, fac_bits_word);
298 *num_fac_bits += 7;
299 fac_gain = (FLOAT32)pow(10.0f, ((FLOAT32)index) / 28.0f);
300
301 for (i = 0; i < fac_len; i++) {
302 left_fac_spec[i] /= fac_gain;
303 }
304
305 for (i = 0; i < fac_len; i += 8) {
306 iusace_find_nearest_neighbor(&left_fac_spec[i], &fac_prm[i]);
307 }
308
309 *num_fac_bits += iusace_fd_encode_fac(fac_prm, &fac_bits_word[7], fac_len);
310
311 for (i = 0; i < (*num_fac_bits + 7) / 8; i++) {
312 fac_prm_out[i] =
313 (WORD16)((fac_bits_word[8 * i + 0] & 0x1) << 7 | (fac_bits_word[8 * i + 1] & 0x1) << 6 |
314 (fac_bits_word[8 * i + 2] & 0x1) << 5 | (fac_bits_word[8 * i + 3] & 0x1) << 4 |
315 (fac_bits_word[8 * i + 4] & 0x1) << 3 | (fac_bits_word[8 * i + 5] & 0x1) << 2 |
316 (fac_bits_word[8 * i + 6] & 0x1) << 1 | (fac_bits_word[8 * i + 7] & 0x1) << 0);
317 }
318 } else {
319 *num_fac_bits = 0;
320 }
321
322 if (next_frm_lpd) {
323 for (i = 0; i < 1024 / 2 + 1 + ORDER; i++) {
324 pstr_acelp->fd_synth[i] = (FLOAT32)synth_time[pstr_acelp->len_frame - 1 + i - ORDER];
325 pstr_acelp->fd_orig[i] = (FLOAT32)orig_sig_dbl[pstr_acelp->len_frame + i - ORDER];
326 }
327
328 pstr_acelp->low_pass_line = low_pass_line;
329 }
330
331 return IA_NO_ERROR;
332 }
333