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 <assert.h> 21 #include <float.h> 22 #include <stdlib.h> 23 #include <stdio.h> 24 #include <math.h> 25 #include <string.h> 26 27 #include "ixheaacd_type_def.h" 28 #include "ixheaacd_bitbuffer.h" 29 #include "ixheaacd_interface.h" 30 #include "ixheaacd_tns_usac.h" 31 #include "ixheaacd_cnst.h" 32 #include "ixheaacd_acelp_info.h" 33 34 #include "ixheaacd_sbrdecsettings.h" 35 #include "ixheaacd_info.h" 36 #include "ixheaacd_sbr_common.h" 37 #include "ixheaacd_drc_data_struct.h" 38 #include "ixheaacd_drc_dec.h" 39 #include "ixheaacd_sbrdecoder.h" 40 #include "ixheaacd_mps_polyphase.h" 41 #include "ixheaacd_sbr_const.h" 42 #include "ixheaacd_main.h" 43 #include "ixheaacd_arith_dec.h" 44 45 #include "ixheaacd_func_def.h" 46 #include "ixheaacd_windows.h" 47 #include "ixheaacd_acelp_com.h" 48 #include "ixheaacd_constants.h" 49 #include "ixheaacd_basic_ops32.h" 50 #include "ixheaacd_basic_ops40.h" 51 52 #define LSF_GAP_F 50.0f 53 #define FREQ_MAX_F 6400.0f 54 #define FREQ_DIV_F 400.0f 55 56 extern const FLOAT32 lsf_init[ORDER]; 57 58 extern const FLOAT32 ixheaacd_fir_lp_filt[1 + FILTER_DELAY]; 59 60 const WORD32 ixheaacd_pow_10_i_by_128[128] = { 61 16384, 17788, 19312, 20968, 22765, 24716, 26835, 62 29135, 31632, 34343, 37287, 40483, 43953, 47720, 63 51810, 56251, 61072, 66307, 71990, 78161, 84860, 64 92134, 100030, 108604, 117913, 128019, 138992, 150905, 65 163840, 177882, 193129, 209682, 227654, 247167, 268352, 66 291353, 316325, 343438, 372874, 404834, 439532, 477205, 67 518107, 562515, 610728, 663075, 719908, 781612, 848605, 68 921340, 1000309, 1086046, 1179133, 1280197, 1389925, 1509057, 69 1638400, 1778829, 1931294, 2096827, 2276549, 2471675, 2683525, 70 2913532, 3163255, 3434381, 3728745, 4048340, 4395328, 4772057, 71 5181075, 5625151, 6107289, 6630752, 7199081, 7816122, 8486051, 72 9213400, 10003091, 10860467, 11791330, 12801978, 13899250, 15090570, 73 16384000, 17788290, 19312945, 20968279, 22765494, 24716750, 26835250, 74 29135329, 31632551, 34343813, 37287459, 40483409, 43953287, 47720573, 75 51810757, 56251515, 61072895, 66307521, 71990813, 78161226, 84860513, 76 92134002, 100030911, 108604672, 117913300, 128019781, 138992500, 150905703, 77 163840000, 177882909, 193129453, 209682794, 227654941, 247167501, 268352504, 78 291353298, 316325515, 343438130, 372874596, 404834095, 439532879, 477205734, 79 518107571, 562515151}; 80 81 VOID ixheaacd_lsf_weight_2st_flt(float *lsfq, float *w, WORD32 mode); 82 83 static PLATFORM_INLINE WORD32 ixheaacd_mult32_m(WORD32 a, WORD32 b) { 84 WORD32 result; 85 WORD64 temp_result; 86 87 temp_result = (WORD64)a * (WORD64)b; 88 result = (WORD32)(temp_result >> 31); 89 90 return (result); 91 } 92 93 void ixheaacd_reset_acelp_data_fix(ia_usac_data_struct *usac_data, 94 ia_usac_lpd_decoder_handle st, 95 WORD32 *ptr_overlap_buf, 96 WORD32 was_last_short, WORD32 tw_mdct) { 97 WORD32 i; 98 99 if (was_last_short == 1) { 100 st->mode_prev = -2; 101 } else { 102 st->mode_prev = -1; 103 } 104 105 for (i = 0; i < NUM_SUBFR_SUPERFRAME_BY2 - 1; i++) { 106 st->pitch_prev[i] = 64; 107 st->gain_prev[i] = 0; 108 } 109 110 st->bpf_active_prev = 0; 111 112 if (ptr_overlap_buf != NULL && !tw_mdct) { 113 const WORD32 *ptr_window_coeff; 114 WORD32 fac_length; 115 if (was_last_short) { 116 fac_length = (usac_data->ccfl) / 16; 117 } else { 118 fac_length = (usac_data->len_subfrm) / 2; 119 } 120 121 if (fac_length == 48) { 122 ptr_window_coeff = ixheaacd_sine_win_96; 123 } else if (fac_length == 64) { 124 ptr_window_coeff = ixheaacd_sine_win_128; 125 } else if (fac_length == 96) { 126 ptr_window_coeff = ixheaacd_sine_win_192; 127 } else { 128 ptr_window_coeff = ixheaacd_sine_win_256; 129 } 130 131 for (i = 0; i < 2 * fac_length; i++) { 132 ptr_overlap_buf[(usac_data->ccfl) / 2 - fac_length + i] = 133 ixheaacd_mult32_m( 134 ptr_overlap_buf[(usac_data->ccfl) / 2 - fac_length + i], 135 ptr_window_coeff[2 * fac_length - 1 - i]); 136 } 137 for (i = 0; i < (usac_data->ccfl) / 2 - fac_length; i++) { 138 ptr_overlap_buf[(usac_data->ccfl) / 2 + fac_length + i] = 0; 139 } 140 141 if (ptr_overlap_buf != NULL) { 142 for (i = 0; i < (usac_data->len_subfrm) / 2 - fac_length; i++) { 143 st->exc_prev[i] = 0.0f; 144 } 145 for (i = 0; i < 2 * fac_length + 1; i++) { 146 st->exc_prev[(usac_data->len_subfrm) / 2 - fac_length + i] = 147 ptr_overlap_buf[i + usac_data->ccfl / 2 - fac_length - 1] / 148 (float)(16384); 149 } 150 } else { 151 ixheaacd_memset(st->exc_prev, 1 + (2 * FAC_LENGTH)); 152 } 153 } 154 155 return; 156 } 157 158 VOID ixheaacd_fix2flt_data(ia_usac_data_struct *usac_data, 159 ia_usac_lpd_decoder_handle st, WORD32 k) { 160 WORD32 i; 161 WORD32 fac_length; 162 WORD32 window_sequence_last = usac_data->window_sequence_last[k]; 163 WORD32 *p_ola_buffer = usac_data->overlap_data_ptr[k]; 164 if (window_sequence_last == EIGHT_SHORT_SEQUENCE) { 165 fac_length = (usac_data->ccfl) / 16; 166 } else { 167 fac_length = (usac_data->len_subfrm) / 2; 168 } 169 170 ixheaacd_memset(st->lp_flt_coeff_a_prev, 2 * (ORDER + 1)); 171 ixheaacd_memset(st->xcitation_prev, MAX_PITCH + INTER_LP_FIL_ORDER + 1); 172 ixheaacd_memset(st->synth_prev, MAX_PITCH + SYNTH_DELAY_LMAX); 173 ixheaacd_memset(st->bpf_prev, FILTER_DELAY + LEN_SUBFR); 174 175 st->gain_threshold = 0.0f; 176 177 if (p_ola_buffer != NULL) { 178 for (i = 0; i < (usac_data->len_subfrm) / 2 - fac_length; i++) { 179 st->exc_prev[i] = 0; 180 } 181 for (i = 0; i < 2 * fac_length + 1; i++) { 182 st->exc_prev[(usac_data->len_subfrm) / 2 - fac_length + i] = (FLOAT32)( 183 p_ola_buffer[i + usac_data->ccfl / 2 - fac_length - 1] / 16384.0); 184 } 185 } else { 186 ixheaacd_memset(st->exc_prev, 1 + (2 * FAC_LENGTH)); 187 } 188 189 return; 190 } 191 192 void ixheaacd_init_acelp_data(ia_usac_data_struct *usac_data, 193 ia_usac_lpd_decoder_handle st) { 194 ixheaacd_reset_acelp_data_fix(usac_data, st, NULL, 0, 0); 195 } 196 197 #define PI_BY_6400 (PI / 6400.0) 198 #define SCALE1 (6400.0 / PI) 199 200 void ixheaacd_lsp_2_lsf_conversion(float lsp[], float lsf[], WORD32 m) { 201 short i; 202 for (i = 0; i < m; i++) { 203 lsf[i] = (float)(acos(lsp[i]) * SCALE1); 204 } 205 return; 206 } 207 208 static VOID ixheaacd_lsf_2_lsp_conversion_float(FLOAT32 lsf[], FLOAT32 lsp[], 209 WORD32 m) { 210 WORD32 i; 211 for (i = 0; i < m; i++) 212 lsp[i] = (FLOAT32)cos((double)lsf[i] * (double)PI_BY_6400); 213 214 return; 215 } 216 217 static WORD32 ixheaacd_bass_post_filter(FLOAT32 *synth_sig, WORD32 *pitch, 218 FLOAT32 *pitch_gain, FLOAT32 *synth_out, 219 WORD32 len_fr, WORD32 len2, 220 FLOAT32 bpf_prev[]) { 221 WORD32 i, j, sf, num_subfr, pitch_lag, lg; 222 FLOAT32 x_energy, xy_corr, y_energy, norm_corr, energy, gain, tmp, alpha; 223 FLOAT32 noise_buf[FILTER_DELAY + (2 * LEN_SUBFR)], *noise_tmp1, *noise_tmp2, 224 *x, *y; 225 226 noise_tmp1 = noise_buf + FILTER_DELAY; 227 noise_tmp2 = noise_buf + FILTER_DELAY + LEN_SUBFR; 228 229 memcpy(synth_out, synth_sig - LEN_SUBFR, len_fr * sizeof(FLOAT32)); 230 231 if (len_fr % 64) 232 memset(synth_out + len_fr, 0, (LEN_SUBFR - len_fr % 64) * sizeof(FLOAT32)); 233 234 sf = 0; 235 for (num_subfr = 0; num_subfr < len_fr; num_subfr += LEN_SUBFR, sf++) { 236 pitch_lag = pitch[sf]; 237 gain = pitch_gain[sf]; 238 if (((pitch_lag >> 1) + 96 - num_subfr) > MAX_PITCH) return -1; 239 if (gain > 1.0f) gain = 1.0f; 240 if (gain < 0.0f) gain = 0.0f; 241 242 x = &synth_sig[num_subfr - 96]; 243 y = &synth_sig[num_subfr - pitch_lag / 2 - 96]; 244 245 x_energy = 0.01f; 246 xy_corr = 0.01f; 247 y_energy = 0.01f; 248 for (i = 0; i < LEN_SUBFR + 96; i++) { 249 x_energy += x[i] * x[i]; 250 xy_corr += x[i] * y[i]; 251 y_energy += y[i] * y[i]; 252 } 253 254 norm_corr = xy_corr / (FLOAT32)sqrt(x_energy * y_energy); 255 256 if (norm_corr > 0.95f) pitch_lag >>= 1; 257 258 lg = len_fr + len2 - pitch_lag - num_subfr; 259 if (lg < 0) lg = 0; 260 if (lg > LEN_SUBFR) lg = LEN_SUBFR; 261 262 if (gain > 0) { 263 if (lg > 0) { 264 tmp = 0.01f; 265 for (i = 0; i < lg; i++) { 266 tmp += synth_sig[i + num_subfr] * synth_sig[i + num_subfr]; 267 } 268 energy = 0.01f; 269 for (i = 0; i < lg; i++) { 270 energy += synth_sig[i + num_subfr + pitch_lag] * 271 synth_sig[i + num_subfr + pitch_lag]; 272 } 273 tmp = (FLOAT32)sqrt(tmp / energy); 274 if (tmp < gain) gain = tmp; 275 } 276 277 alpha = 0.5f * gain; 278 for (i = 0; i < lg; i++) { 279 noise_tmp2[i] = alpha * (synth_sig[i + num_subfr] - 280 0.5f * synth_sig[i + num_subfr - pitch_lag] - 281 0.5f * synth_sig[i + num_subfr + pitch_lag]); 282 } 283 for (i = lg; i < LEN_SUBFR; i++) { 284 noise_tmp2[i] = alpha * (synth_sig[i + num_subfr] - 285 synth_sig[i + num_subfr - pitch_lag]); 286 } 287 } else { 288 memset(noise_tmp2, 0, LEN_SUBFR * sizeof(FLOAT32)); 289 } 290 291 memcpy(noise_buf, bpf_prev, (FILTER_DELAY + LEN_SUBFR) * sizeof(FLOAT32)); 292 memcpy(bpf_prev, noise_buf + LEN_SUBFR, 293 (FILTER_DELAY + LEN_SUBFR) * sizeof(FLOAT32)); 294 295 for (i = 0; i < LEN_SUBFR; i++) { 296 tmp = ixheaacd_fir_lp_filt[0] * noise_tmp1[i]; 297 for (j = 1; j <= FILTER_DELAY; j++) { 298 tmp += 299 ixheaacd_fir_lp_filt[j] * (noise_tmp1[i - j] + noise_tmp1[i + j]); 300 } 301 synth_out[i + num_subfr] -= tmp; 302 } 303 } 304 305 return 0; 306 } 307 308 void ixheaacd_reorder_lsf(float *lsf, float min_dist, int n) { 309 int i; 310 float lsf_min; 311 312 lsf_min = min_dist; 313 for (i = 0; i < n; i++) { 314 if (lsf[i] < lsf_min) lsf[i] = lsf_min; 315 316 lsf_min = lsf[i] + min_dist; 317 } 318 319 lsf_min = FREQ_MAX_F - min_dist; 320 for (i = n - 1; i >= 0; i--) { 321 if (lsf[i] > lsf_min) lsf[i] = lsf_min; 322 323 lsf_min = lsf[i] - min_dist; 324 } 325 326 return; 327 } 328 329 WORD32 ixheaacd_lpd_dec(ia_usac_data_struct *usac_data, 330 ia_usac_lpd_decoder_handle st, 331 ia_td_frame_data_struct *pstr_td_frame_data, 332 FLOAT32 fsynth[], WORD32 first_lpd_flag, 333 WORD32 short_fac_flag, WORD32 bpf_control_info) { 334 FLOAT32 *synth_buf = usac_data->synth_buf; 335 FLOAT32 *xcitation_buff = usac_data->exc_buf; 336 FLOAT32 lsp_curr[ORDER]; 337 FLOAT32 lsf_curr[ORDER]; 338 FLOAT32 *lp_flt_coff_a = usac_data->lp_flt_coff; 339 FLOAT32 *synth, *xcitation_curr; 340 WORD32 *pitch = usac_data->pitch; 341 FLOAT32 *pitch_gain = usac_data->pitch_gain; 342 FLOAT32 lsf_flt[(2 * NUM_FRAMES + 1) * ORDER]; 343 344 WORD32 i, k, tp, mode; 345 WORD32 *mod; 346 FLOAT32 gain, stability_factor; 347 FLOAT32 tmp, synth_corr, synth_energy; 348 349 WORD32 len_fr; 350 WORD32 len_subfrm; 351 WORD32 num_subfr; 352 WORD32 num_subfr_in_superfr; 353 WORD32 num_subfr_by2; 354 WORD32 synth_delay; 355 WORD32 num_samples = 0; 356 357 WORD32 *ptr_scratch = &usac_data->scratch_buffer[0]; 358 359 WORD32 subfr_len, n_subfr; 360 WORD32 err = 0; 361 362 len_fr = usac_data->ccfl; 363 len_subfrm = usac_data->len_subfrm; 364 num_subfr = usac_data->num_subfrm; 365 num_subfr_in_superfr = NUM_FRAMES * num_subfr; 366 num_subfr_by2 = (num_subfr_in_superfr / 2) - 1; 367 synth_delay = num_subfr_by2 * LEN_SUBFR; 368 369 synth = synth_buf + MAX_PITCH + synth_delay; 370 ixheaacd_mem_cpy(st->synth_prev, synth_buf, MAX_PITCH + synth_delay); 371 ixheaacd_memset(synth, SYNTH_DELAY_LMAX + LEN_SUPERFRAME - synth_delay); 372 373 xcitation_curr = xcitation_buff + MAX_PITCH + INTER_LP_FIL_ORDER + 1; 374 ixheaacd_mem_cpy(st->xcitation_prev, xcitation_buff, 375 MAX_PITCH + INTER_LP_FIL_ORDER + 1); 376 memset(xcitation_curr, 0, sizeof(FLOAT32) * (LEN_SUPERFRAME + 1)); 377 378 mod = pstr_td_frame_data->mod; 379 380 for (i = 0; i < num_subfr_by2; i++) { 381 pitch[i] = st->pitch_prev[i]; 382 pitch_gain[i] = st->gain_prev[i]; 383 } 384 for (i = 0; i < num_subfr_in_superfr; i++) { 385 pitch[i + num_subfr_by2] = 64; 386 pitch_gain[i + num_subfr_by2] = 0.0f; 387 } 388 if (!first_lpd_flag) { 389 ixheaacd_lsp_2_lsf_conversion(st->lspold, lsf_flt, ORDER); 390 } 391 392 ixheaacd_alg_vec_dequant(pstr_td_frame_data, first_lpd_flag, lsf_flt, 393 pstr_td_frame_data->mod); 394 395 if (first_lpd_flag) { 396 ixheaacd_mem_cpy(&lsf_flt[0], st->lsf_prev, ORDER); 397 ixheaacd_lsf_2_lsp_conversion_float(st->lsf_prev, st->lspold, ORDER); 398 } 399 400 if ((first_lpd_flag && mod[0] == 0) || (first_lpd_flag && mod[1] == 0) || 401 ((first_lpd_flag && mod[2] == 0 && len_subfrm != LEN_FRAME))) { 402 FLOAT32 lp_flt_coeff_a[9 * (ORDER + 1)]; 403 FLOAT32 tmp_buf[3 * LEN_FRAME + ORDER]; 404 FLOAT32 tmp_res_buf[3 * LEN_FRAME]; 405 FLOAT32 *tmp = &(tmp_buf[LEN_FRAME]); 406 FLOAT32 *ptr_tmp = &(tmp_res_buf[LEN_FRAME]); 407 WORD32 tmp_start; 408 FLOAT32 mem = 0; 409 WORD32 gain; 410 WORD32 length; 411 412 ixheaacd_interpolation_lsp_params(st->lspold, st->lspold, lp_flt_coeff_a, 413 8); 414 415 memcpy(st->lp_flt_coeff_a_prev, lp_flt_coeff_a, 416 (ORDER + 1) * sizeof(FLOAT32)); 417 memcpy(st->lp_flt_coeff_a_prev + ORDER + 1, lp_flt_coeff_a, 418 (ORDER + 1) * sizeof(FLOAT32)); 419 420 if (mod[0] == 0) { 421 WORD32 fac_length; 422 if (short_fac_flag) { 423 fac_length = (len_subfrm * NUM_FRAMES) / 16; 424 } else { 425 fac_length = len_subfrm / 2; 426 } 427 if ((pstr_td_frame_data->fac_data[0] < 0) || 428 (pstr_td_frame_data->fac_data[0] > 128)) { 429 return -1; 430 } 431 gain = ixheaacd_pow_10_i_by_128[pstr_td_frame_data->fac_data[0]]; 432 433 memcpy(ptr_scratch, &pstr_td_frame_data->fac_data[0], 434 129 * sizeof(WORD32)); 435 436 for (i = 0; i < fac_length / 2; i++) { 437 pstr_td_frame_data->fac_data[i] = ptr_scratch[2 * i + 1] << 16; 438 pstr_td_frame_data->fac_data[fac_length / 2 + i] = 439 ptr_scratch[fac_length - 2 * i] << 16; 440 } 441 442 err = ixheaacd_fwd_alias_cancel_tool(usac_data, pstr_td_frame_data, 443 fac_length, lp_flt_coeff_a, gain); 444 if (err == -1) return err; 445 446 memset( 447 &usac_data->overlap_data_ptr[usac_data->present_chan][(len_fr / 2)], 448 0, fac_length * sizeof(WORD32)); 449 } 450 451 for (i = 0; i < 2 * len_subfrm; i++) 452 st->fd_synth[ORDER + i] = (FLOAT32)( 453 (FLOAT32)usac_data->overlap_data_ptr[usac_data->present_chan][i] / 454 16384.0); 455 num_samples = min(2 * len_subfrm, MAX_PITCH + synth_delay); 456 457 ixheaacd_mem_cpy(st->fd_synth + ORDER, synth - 2 * len_subfrm, 458 2 * len_subfrm); 459 460 ixheaacd_preemphsis_tool_float(st->fd_synth + ORDER, PREEMPH_FILT_FAC, 461 2 * len_subfrm, mem); 462 463 ixheaacd_memset(tmp, ORDER); 464 ixheaacd_mem_cpy(st->fd_synth + ORDER, tmp + ORDER, 2 * len_subfrm); 465 tmp_start = 0; 466 467 ixheaacd_memset(ptr_tmp - len_subfrm, 3 * len_subfrm); 468 memset(st->fd_synth, 0, ORDER * sizeof(WORD32)); 469 length = (2 * len_subfrm - tmp_start) / LEN_SUBFR; 470 471 ixheaacd_residual_tool_float1(lp_flt_coeff_a, 472 &st->fd_synth[ORDER + tmp_start], 473 &ptr_tmp[tmp_start], LEN_SUBFR, length); 474 475 if (mod[0] != 0 && (len_subfrm == LEN_FRAME || mod[1] != 0)) { 476 num_samples = min(len_subfrm, MAX_PITCH + INTER_LP_FIL_ORDER + 1); 477 } else { 478 num_samples = min(2 * len_subfrm, MAX_PITCH + INTER_LP_FIL_ORDER + 1); 479 } 480 ixheaacd_mem_cpy(ptr_tmp + 2 * len_subfrm - num_samples, 481 xcitation_curr - num_samples, num_samples); 482 } 483 484 k = 0; 485 486 while (k < 4) { 487 mode = mod[k]; 488 if ((st->mode_prev == 0) && (mode > 0) && 489 (k != 0 || st->bpf_active_prev == 1)) { 490 i = (k * num_subfr) + num_subfr_by2; 491 pitch[i + 1] = pitch[i] = pitch[i - 1]; 492 pitch_gain[i + 1] = pitch_gain[i] = pitch_gain[i - 1]; 493 } 494 495 if ((mode == 0) || (mode == 1)) 496 memcpy(lsf_curr, &lsf_flt[(k + 1) * ORDER], ORDER * sizeof(FLOAT32)); 497 else if (mode == 2) 498 memcpy(lsf_curr, &lsf_flt[(k + 2) * ORDER], ORDER * sizeof(FLOAT32)); 499 else 500 memcpy(lsf_curr, &lsf_flt[(k + 4) * ORDER], ORDER * sizeof(FLOAT32)); 501 502 ixheaacd_lsf_2_lsp_conversion_float(lsf_curr, lsp_curr, ORDER); 503 504 tmp = 0.0f; 505 for (i = 0; i < ORDER; i++) { 506 tmp += (lsf_curr[i] - st->lsf_prev[i]) * (lsf_curr[i] - st->lsf_prev[i]); 507 } 508 stability_factor = (FLOAT32)(1.25f - (tmp / 400000.0f)); 509 if (stability_factor > 1.0f) { 510 stability_factor = 1.0f; 511 } 512 if (stability_factor < 0.0f) { 513 stability_factor = 0.0f; 514 } 515 516 if (mode == 0) { 517 ixheaacd_interpolation_lsp_params(st->lspold, lsp_curr, lp_flt_coff_a, 518 num_subfr); 519 520 ixheaacd_acelp_alias_cnx(usac_data, pstr_td_frame_data, k, lp_flt_coff_a, 521 stability_factor, st); 522 523 if ((st->mode_prev != 0) && bpf_control_info) { 524 i = (k * num_subfr) + num_subfr_by2; 525 pitch[i - 1] = pitch[i]; 526 pitch_gain[i - 1] = pitch_gain[i]; 527 if (st->mode_prev != -2) { 528 pitch[i - 2] = pitch[i]; 529 pitch_gain[i - 2] = pitch_gain[i]; 530 } 531 } 532 k++; 533 } else { 534 if (mode == 1) { 535 subfr_len = len_subfrm; 536 n_subfr = num_subfr; 537 } else if (mode == 2) { 538 subfr_len = len_subfrm << 1; 539 n_subfr = num_subfr_in_superfr / 2; 540 } else if (mode == 3) { 541 subfr_len = len_subfrm << 2; 542 n_subfr = num_subfr_in_superfr; 543 } 544 545 ixheaacd_lpc_coef_gen(st->lspold, lsp_curr, lp_flt_coff_a, n_subfr, 546 ORDER); 547 548 ixheaacd_tcx_mdct(usac_data, pstr_td_frame_data, k, lp_flt_coff_a, 549 subfr_len, st); 550 k += (1 << (mode - 1)); 551 } 552 553 st->mode_prev = mode; 554 555 ixheaacd_mem_cpy(lsp_curr, st->lspold, ORDER); 556 ixheaacd_mem_cpy(lsf_curr, st->lsf_prev, ORDER); 557 } 558 559 ixheaacd_mem_cpy(xcitation_buff + len_fr, st->xcitation_prev, 560 MAX_PITCH + INTER_LP_FIL_ORDER + 1); 561 562 ixheaacd_mem_cpy(synth_buf + len_fr, st->synth_prev, MAX_PITCH + synth_delay); 563 564 if (!bpf_control_info) { 565 if (mod[0] != 0 && st->bpf_active_prev) { 566 for (i = 2; i < num_subfr_in_superfr; i++) 567 pitch_gain[num_subfr_by2 + i] = 0.0; 568 } else { 569 for (i = 0; i < num_subfr_in_superfr; i++) 570 pitch_gain[num_subfr_by2 + i] = 0.0; 571 } 572 } 573 st->bpf_active_prev = bpf_control_info; 574 575 for (i = 0; i < num_subfr_by2; i++) { 576 st->pitch_prev[i] = pitch[num_subfr_in_superfr + i]; 577 st->gain_prev[i] = pitch_gain[num_subfr_in_superfr + i]; 578 } 579 580 synth = synth_buf + MAX_PITCH; 581 582 for (i = 0; i < num_subfr_in_superfr; i++) { 583 tp = pitch[i]; 584 gain = pitch_gain[i]; 585 if (gain > 0.0f) { 586 synth_corr = 0.0f, synth_energy = 1e-6f; 587 if ((((i * LEN_SUBFR) + LEN_SUBFR) > LEN_SUPERFRAME) || 588 ((((i * LEN_SUBFR) + LEN_SUBFR) - tp) > LEN_SUPERFRAME)) 589 return -1; 590 for (k = 0; k < LEN_SUBFR; k++) { 591 synth_corr += 592 synth[i * LEN_SUBFR + k] * synth[(i * LEN_SUBFR) - tp + k]; 593 synth_energy += 594 synth[(i * LEN_SUBFR) - tp + k] * synth[(i * LEN_SUBFR) - tp + k]; 595 } 596 pitch_gain[i] = synth_corr / synth_energy; 597 } 598 } 599 600 if (mod[3] == 0) { 601 err = ixheaacd_bass_post_filter(synth, pitch, pitch_gain, fsynth, len_fr, 602 synth_delay, st->bpf_prev); 603 } else { 604 err = 605 ixheaacd_bass_post_filter(synth, pitch, pitch_gain, fsynth, len_fr, 606 synth_delay - (len_subfrm / 2), st->bpf_prev); 607 } 608 return err; 609 } 610 611 WORD32 ixheaacd_lpd_dec_update(ia_usac_lpd_decoder_handle tddec, 612 ia_usac_data_struct *usac_data, WORD32 i_ch) { 613 WORD32 err = 0, i, k; 614 615 WORD32 *ptr_overlap = &usac_data->overlap_data_ptr[i_ch][0]; 616 WORD32 len_fr, lpd_sbf_len, lpd_delay, num_subfr_by2, synth_delay, fac_length; 617 618 if (usac_data->tw_mdct[0]) 619 ptr_overlap = &usac_data->overlap_data_ptr[i_ch][usac_data->ccfl / 2]; 620 621 len_fr = usac_data->ccfl; 622 lpd_sbf_len = (NUM_FRAMES * usac_data->num_subfrm) / 2; 623 lpd_delay = lpd_sbf_len * LEN_SUBFR; 624 num_subfr_by2 = lpd_sbf_len - 1; 625 synth_delay = num_subfr_by2 * LEN_SUBFR; 626 fac_length = (usac_data->len_subfrm) / 2; 627 628 for (i = 0; i < LEN_SUBFR + synth_delay; i++) 629 ptr_overlap[i] = (WORD32)( 630 (FLOAT32)tddec->synth_prev[MAX_PITCH - (LEN_SUBFR) + i] * 16384.0); 631 632 ptr_overlap += LEN_SUBFR + synth_delay - fac_length; 633 634 for (k = 0; k < 2 * fac_length; k++) 635 ptr_overlap[k] = (WORD32)((FLOAT32)tddec->exc_prev[k + 1] * 16384.0); 636 637 ptr_overlap = &usac_data->overlap_data_ptr[i_ch][lpd_delay + fac_length]; 638 639 for (i = 0; i < len_fr - lpd_delay - fac_length; i++) ptr_overlap[i] = 0; 640 641 usac_data->window_shape[i_ch] = WIN_SEL_0; 642 usac_data->window_sequence_last[i_ch] = EIGHT_SHORT_SEQUENCE; 643 usac_data->td_frame_prev[i_ch] = 1; 644 645 if (tddec->mode_prev == 0) { 646 memmove(usac_data->lpc_prev[i_ch], &tddec->lp_flt_coeff_a_prev[ORDER + 1], 647 (ORDER + 1) * sizeof(FLOAT32)); 648 memmove(usac_data->acelp_in[i_ch], tddec->exc_prev, 649 (1 + (2 * FAC_LENGTH)) * sizeof(FLOAT32)); 650 } 651 652 return err; 653 } 654 655 WORD32 ixheaacd_lpd_bpf_fix(ia_usac_data_struct *usac_data, 656 WORD32 is_short_flag, FLOAT32 out_buffer[], 657 ia_usac_lpd_decoder_handle st) { 658 WORD32 i, tp, k; 659 float synth_buf[MAX_PITCH + SYNTH_DELAY_LMAX + LEN_SUPERFRAME]; 660 float signal_out[LEN_SUPERFRAME]; 661 float *synth, synth_corr, synth_energy; 662 WORD32 pitch[NUM_SUBFR_SUPERFRAME_BY2 + 3]; 663 float pitch_gain[NUM_SUBFR_SUPERFRAME_BY2 + 3]; 664 WORD32 len_fr, lpd_sbf_len, lpd_delay, num_subfr_by2, synth_delay, fac_length; 665 WORD32 err = 0; 666 667 len_fr = usac_data->ccfl; 668 lpd_sbf_len = (NUM_FRAMES * usac_data->num_subfrm) / 2; 669 lpd_delay = lpd_sbf_len * LEN_SUBFR; 670 num_subfr_by2 = lpd_sbf_len - 1; 671 synth_delay = num_subfr_by2 * LEN_SUBFR; 672 fac_length = (usac_data->len_subfrm) / 2; 673 674 ixheaacd_memset(synth_buf, MAX_PITCH + synth_delay + len_fr); 675 ixheaacd_mem_cpy(st->synth_prev, synth_buf, MAX_PITCH + synth_delay); 676 ixheaacd_mem_cpy(out_buffer, synth_buf + MAX_PITCH - (LEN_SUBFR), 677 synth_delay + len_fr + (LEN_SUBFR)); 678 679 for (i = 0; i < num_subfr_by2; i++) { 680 pitch[i] = st->pitch_prev[i]; 681 pitch_gain[i] = st->gain_prev[i]; 682 } 683 for (i = num_subfr_by2; i < lpd_sbf_len + 3; i++) { 684 pitch[i] = 64; 685 pitch_gain[i] = 0.0f; 686 } 687 if (st->mode_prev == 0) { 688 pitch[num_subfr_by2] = pitch[num_subfr_by2 - 1]; 689 pitch_gain[num_subfr_by2] = pitch_gain[num_subfr_by2 - 1]; 690 if (!is_short_flag) { 691 pitch[num_subfr_by2 + 1] = pitch[num_subfr_by2]; 692 pitch_gain[num_subfr_by2 + 1] = pitch_gain[num_subfr_by2]; 693 } 694 } 695 696 synth = synth_buf + MAX_PITCH; 697 698 for (i = 0; i < num_subfr_by2 + 2; i++) { 699 tp = pitch[i]; 700 if ((i * LEN_SUBFR + MAX_PITCH) < tp) { 701 return -1; 702 } else if (((i * LEN_SUBFR + MAX_PITCH - tp) >= 1883) || 703 (((i * LEN_SUBFR) + LEN_SUBFR) > LEN_SUPERFRAME) || 704 ((((i * LEN_SUBFR) + LEN_SUBFR) - tp) > LEN_SUPERFRAME)) { 705 return -1; 706 } 707 708 if (pitch_gain[i] > 0.0f) { 709 synth_corr = 0.0f, synth_energy = 1e-6f; 710 for (k = 0; k < LEN_SUBFR; k++) { 711 synth_corr += 712 synth[i * LEN_SUBFR + k] * synth[(i * LEN_SUBFR) - tp + k]; 713 synth_energy += 714 synth[(i * LEN_SUBFR) - tp + k] * synth[(i * LEN_SUBFR) - tp + k]; 715 } 716 pitch_gain[i] = synth_corr / synth_energy; 717 } 718 } 719 720 err = ixheaacd_bass_post_filter(synth, pitch, pitch_gain, signal_out, 721 (lpd_sbf_len + 2) * LEN_SUBFR + LEN_SUBFR, 722 len_fr - (lpd_sbf_len + 4) * LEN_SUBFR, 723 st->bpf_prev); 724 if (err != 0) return err; 725 726 ixheaacd_mem_cpy(signal_out, out_buffer, 727 (lpd_sbf_len + 2) * LEN_SUBFR + LEN_SUBFR); 728 return err; 729 } 730