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 <math.h> 21 #include <stdio.h> 22 #include <string.h> 23 #include "ixheaacd_type_def.h" 24 25 #include "ixheaacd_acelp_com.h" 26 #include "ixheaacd_windows.h" 27 #include "ixheaacd_vec_baisc_ops.h" 28 #include "ixheaacd_bitbuffer.h" 29 30 #include "ixheaacd_interface.h" 31 32 #include "ixheaacd_tns_usac.h" 33 #include "ixheaacd_cnst.h" 34 35 #include "ixheaacd_acelp_info.h" 36 37 #include "ixheaacd_sbrdecsettings.h" 38 #include "ixheaacd_info.h" 39 #include "ixheaacd_sbr_common.h" 40 #include "ixheaacd_drc_data_struct.h" 41 #include "ixheaacd_drc_dec.h" 42 #include "ixheaacd_sbrdecoder.h" 43 #include "ixheaacd_mps_polyphase.h" 44 #include "ixheaacd_sbr_const.h" 45 #include "ixheaacd_main.h" 46 #include "ixheaacd_arith_dec.h" 47 #include "ixheaacd_bit_extract.h" 48 #include "ixheaacd_main.h" 49 #include "ixheaacd_func_def.h" 50 #include "ixheaacd_constants.h" 51 #include "ixheaacd_basic_ops32.h" 52 #include "ixheaacd_basic_ops40.h" 53 54 WORD32 ixheaacd_get_mode_lpc(WORD32 lpc_set, ia_bit_buf_struct *it_bit_buff, 55 WORD32 *nk_mode) { 56 WORD32 mode_lpc = 0; 57 switch (lpc_set) { 58 case 4: 59 mode_lpc = 0; 60 break; 61 case 0: 62 case 2: 63 mode_lpc = ixheaacd_read_bits_buf(it_bit_buff, 1); 64 if (mode_lpc == 1) *nk_mode = 3; 65 break; 66 case 1: 67 if (ixheaacd_read_bits_buf(it_bit_buff, 1) == 0) 68 mode_lpc = *nk_mode = 2; 69 else { 70 if (ixheaacd_read_bits_buf(it_bit_buff, 1) == 0) 71 mode_lpc = *nk_mode = 0; 72 else 73 mode_lpc = *nk_mode = 1; 74 } 75 break; 76 case 3: 77 if (ixheaacd_read_bits_buf(it_bit_buff, 1) == 0) 78 mode_lpc = *nk_mode = 1; 79 else { 80 if (ixheaacd_read_bits_buf(it_bit_buff, 1) == 0) 81 mode_lpc = *nk_mode = 0; 82 else { 83 if (ixheaacd_read_bits_buf(it_bit_buff, 1) == 0) 84 mode_lpc = 2; 85 else 86 mode_lpc = 3; 87 *nk_mode = 2; 88 } 89 } 90 break; 91 } 92 return mode_lpc; 93 } 94 95 VOID ixheaacd_qn_data(WORD32 nk_mode, WORD32 *qn, 96 ia_bit_buf_struct *it_bit_buff) { 97 WORD32 k; 98 switch (nk_mode) { 99 case 1: 100 for (k = 0; k < 2; k++) { 101 while (ixheaacd_read_bits_buf(it_bit_buff, 1) == 1) { 102 qn[k] += 1; 103 } 104 if (qn[k] > 0) qn[k] += 1; 105 } 106 break; 107 case 0: 108 case 2: 109 case 3: 110 for (k = 0; k < 2; k++) 111 qn[k] = 2 + ixheaacd_read_bits_buf(it_bit_buff, 2); 112 113 if (nk_mode == 2) { 114 for (k = 0; k < 2; k++) { 115 if (qn[k] > 4) { 116 qn[k] = 0; 117 118 while (ixheaacd_read_bits_buf(it_bit_buff, 1) == 1) qn[k] += 1; 119 120 if (qn[k] > 0) qn[k] += 4; 121 } 122 } 123 } else { 124 for (k = 0; k < 2; k++) { 125 if (qn[k] > 4) { 126 WORD32 qn_ext = 0; 127 while (ixheaacd_read_bits_buf(it_bit_buff, 1) == 1) qn_ext += 1; 128 129 switch (qn_ext) { 130 case 0: 131 qn[k] = 5; 132 break; 133 case 1: 134 qn[k] = 6; 135 break; 136 case 2: 137 qn[k] = 0; 138 break; 139 default: 140 qn[k] = qn_ext + 4; 141 break; 142 } 143 } 144 } 145 } 146 break; 147 } 148 return; 149 } 150 151 VOID ixheaacd_code_book_indices(ia_td_frame_data_struct *pstr_td_frame_data, 152 WORD32 nk_mode, WORD32 *pos, 153 ia_bit_buf_struct *it_bit_buff) { 154 WORD32 k, qn[2] = {0, 0}, nk, n, i; 155 156 ixheaacd_qn_data(nk_mode, &qn[0], it_bit_buff); 157 158 pstr_td_frame_data->lpc_first_approx_idx[(*pos)++] = qn[0]; 159 pstr_td_frame_data->lpc_first_approx_idx[(*pos)++] = qn[1]; 160 161 for (k = 0; k < 2; k++) { 162 if (qn[k] > 0) { 163 if (qn[k] > 4) { 164 nk = (qn[k] - 3) / 2; 165 n = qn[k] - nk * 2; 166 } else { 167 nk = 0; 168 n = qn[k]; 169 } 170 pstr_td_frame_data->lpc_first_approx_idx[(*pos)++] = 171 ixheaacd_read_bits_buf(it_bit_buff, 4 * n); 172 173 for (i = 0; i < 8; i++) 174 pstr_td_frame_data->lpc_first_approx_idx[(*pos)++] = 175 ixheaacd_read_bits_buf(it_bit_buff, nk); 176 } 177 } 178 return; 179 } 180 181 VOID ixheaacd_lpc_data(WORD32 first_lpd_flag, WORD32 mod[], 182 ia_td_frame_data_struct *pstr_td_frame_data, 183 ia_bit_buf_struct *it_bit_buff) { 184 WORD32 mode_lpc, nk_mode = 0, j = 0; 185 186 mode_lpc = ixheaacd_get_mode_lpc(4, it_bit_buff, &nk_mode); 187 188 pstr_td_frame_data->lpc_first_approx_idx[j++] = 189 ixheaacd_read_bits_buf(it_bit_buff, 8); 190 191 ixheaacd_code_book_indices(pstr_td_frame_data, nk_mode, &j, it_bit_buff); 192 if (first_lpd_flag) { 193 mode_lpc = ixheaacd_get_mode_lpc(0, it_bit_buff, &nk_mode); 194 pstr_td_frame_data->lpc_first_approx_idx[j++] = mode_lpc; 195 196 if (mode_lpc == 0) 197 pstr_td_frame_data->lpc_first_approx_idx[j++] = 198 ixheaacd_read_bits_buf(it_bit_buff, 8); 199 200 ixheaacd_code_book_indices(pstr_td_frame_data, nk_mode, &j, it_bit_buff); 201 } 202 if (mod[0] < 3) { 203 mode_lpc = ixheaacd_get_mode_lpc(2, it_bit_buff, &nk_mode); 204 pstr_td_frame_data->lpc_first_approx_idx[j++] = mode_lpc; 205 206 if (mode_lpc == 0) 207 pstr_td_frame_data->lpc_first_approx_idx[j++] = 208 ixheaacd_read_bits_buf(it_bit_buff, 8); 209 210 ixheaacd_code_book_indices(pstr_td_frame_data, nk_mode, &j, it_bit_buff); 211 } 212 if (mod[0] < 2) { 213 mode_lpc = ixheaacd_get_mode_lpc(1, it_bit_buff, &nk_mode); 214 pstr_td_frame_data->lpc_first_approx_idx[j++] = mode_lpc; 215 216 if (mode_lpc == 0) 217 pstr_td_frame_data->lpc_first_approx_idx[j++] = 218 ixheaacd_read_bits_buf(it_bit_buff, 8); 219 220 if (mode_lpc != 1) 221 ixheaacd_code_book_indices(pstr_td_frame_data, nk_mode, &j, it_bit_buff); 222 } 223 if (mod[2] < 2) { 224 mode_lpc = ixheaacd_get_mode_lpc(3, it_bit_buff, &nk_mode); 225 pstr_td_frame_data->lpc_first_approx_idx[j++] = mode_lpc; 226 227 if (mode_lpc == 0) 228 pstr_td_frame_data->lpc_first_approx_idx[j++] = 229 ixheaacd_read_bits_buf(it_bit_buff, 8); 230 231 ixheaacd_code_book_indices(pstr_td_frame_data, nk_mode, &j, it_bit_buff); 232 } 233 return; 234 } 235 236 VOID ixheaacd_fac_decoding(WORD32 fac_length, WORD32 k, WORD32 *fac_prm, 237 ia_bit_buf_struct *it_bit_buff) { 238 WORD32 i, j, n, qn, nk, kv[8]; 239 long code_book_index; 240 241 for (i = 0; i < fac_length; i += 8) { 242 qn = 0; 243 while (ixheaacd_read_bits_buf(it_bit_buff, 1) == 1) { 244 qn += 1; 245 } 246 if (qn != 0) qn += 1; 247 248 nk = 0; 249 n = qn; 250 if (qn > 4) { 251 nk = (qn - 3) >> 1; 252 n = qn - nk * 2; 253 } 254 255 code_book_index = ixheaacd_read_bits_buf(it_bit_buff, 4 * n); 256 257 for (j = 0; j < 8; j++) { 258 kv[j] = ixheaacd_read_bits_buf(it_bit_buff, nk); 259 } 260 261 ixheaacd_rotated_gosset_mtx_dec(qn, code_book_index, kv, 262 &fac_prm[k * FAC_LENGTH + i]); 263 } 264 } 265 266 const UWORD8 ixheaacd_num_bites_celp_coding[8][4] = { 267 {5, 5, 5, 5}, {9, 9, 5, 5}, {9, 9, 9, 9}, {13, 13, 9, 9}, 268 {13, 13, 13, 13}, {16, 16, 16, 16}, {1, 5, 1, 5}, {1, 5, 5, 5}}; 269 270 VOID ixheaacd_acelp_decoding(WORD32 k, ia_usac_data_struct *usac_data, 271 ia_td_frame_data_struct *pstr_td_frame_data, 272 ia_bit_buf_struct *it_bit_buff, WORD32 chan) { 273 WORD32 sfr, kk; 274 WORD32 nb_subfr = usac_data->num_subfrm; 275 const UWORD8 *ptr_num_bits = 276 &ixheaacd_num_bites_celp_coding[pstr_td_frame_data->acelp_core_mode][0]; 277 278 chan = 0; 279 pstr_td_frame_data->mean_energy[k] = ixheaacd_read_bits_buf(it_bit_buff, 2); 280 281 for (sfr = 0; sfr < nb_subfr; sfr++) { 282 kk = k * 4 + sfr; 283 284 if ((sfr == 0) || ((nb_subfr == 4) && (sfr == 2))) 285 pstr_td_frame_data->acb_index[kk] = 286 ixheaacd_read_bits_buf(it_bit_buff, 9); 287 288 else 289 pstr_td_frame_data->acb_index[kk] = 290 ixheaacd_read_bits_buf(it_bit_buff, 6); 291 292 pstr_td_frame_data->ltp_filtering_flag[kk] = 293 ixheaacd_read_bits_buf(it_bit_buff, 1); 294 295 if (pstr_td_frame_data->acelp_core_mode == 5) { 296 pstr_td_frame_data->icb_index[kk][0] = 297 ixheaacd_read_bits_buf(it_bit_buff, 2); 298 pstr_td_frame_data->icb_index[kk][1] = 299 ixheaacd_read_bits_buf(it_bit_buff, 2); 300 pstr_td_frame_data->icb_index[kk][2] = 301 ixheaacd_read_bits_buf(it_bit_buff, 2); 302 pstr_td_frame_data->icb_index[kk][3] = 303 ixheaacd_read_bits_buf(it_bit_buff, 2); 304 pstr_td_frame_data->icb_index[kk][4] = 305 ixheaacd_read_bits_buf(it_bit_buff, 14); 306 pstr_td_frame_data->icb_index[kk][5] = 307 ixheaacd_read_bits_buf(it_bit_buff, 14); 308 pstr_td_frame_data->icb_index[kk][6] = 309 ixheaacd_read_bits_buf(it_bit_buff, 14); 310 pstr_td_frame_data->icb_index[kk][7] = 311 ixheaacd_read_bits_buf(it_bit_buff, 14); 312 } else { 313 pstr_td_frame_data->icb_index[kk][0] = 314 ixheaacd_read_bits_buf(it_bit_buff, ptr_num_bits[0]); 315 pstr_td_frame_data->icb_index[kk][1] = 316 ixheaacd_read_bits_buf(it_bit_buff, ptr_num_bits[1]); 317 pstr_td_frame_data->icb_index[kk][2] = 318 ixheaacd_read_bits_buf(it_bit_buff, ptr_num_bits[2]); 319 pstr_td_frame_data->icb_index[kk][3] = 320 ixheaacd_read_bits_buf(it_bit_buff, ptr_num_bits[3]); 321 } 322 323 pstr_td_frame_data->gains[kk] = ixheaacd_read_bits_buf(it_bit_buff, 7); 324 } 325 } 326 327 VOID ixheaacd_tcx_coding(ia_usac_data_struct *usac_data, pWORD32 quant, 328 WORD32 k, WORD32 first_tcx_flag, 329 ia_td_frame_data_struct *pstr_td_frame_data, 330 ia_bit_buf_struct *it_bit_buff 331 332 ) { 333 pstr_td_frame_data->noise_factor[k] = ixheaacd_read_bits_buf(it_bit_buff, 3); 334 335 pstr_td_frame_data->global_gain[k] = ixheaacd_read_bits_buf(it_bit_buff, 7); 336 337 switch (pstr_td_frame_data->mod[k]) { 338 case 1: 339 pstr_td_frame_data->tcx_lg[k] = usac_data->len_subfrm; 340 break; 341 case 2: 342 pstr_td_frame_data->tcx_lg[k] = 2 * (usac_data->len_subfrm); 343 break; 344 case 3: 345 pstr_td_frame_data->tcx_lg[k] = 4 * (usac_data->len_subfrm); 346 break; 347 } 348 349 if (first_tcx_flag) { 350 if (usac_data->usac_independency_flg) { 351 pstr_td_frame_data->arith_reset_flag = 1; 352 } else { 353 pstr_td_frame_data->arith_reset_flag = 354 ixheaacd_read_bits_buf(it_bit_buff, 1); 355 } 356 } 357 358 ixheaacd_arith_data(pstr_td_frame_data, quant, usac_data, it_bit_buff, 359 (first_tcx_flag), k); 360 } 361 362 WORD32 ixheaacd_lpd_channel_stream(ia_usac_data_struct *usac_data, 363 ia_td_frame_data_struct *pstr_td_frame_data, 364 ia_bit_buf_struct *it_bit_buff, 365 FLOAT32 *synth 366 367 ) 368 369 { 370 WORD32 lpd_mode, k, cnt, ii; 371 WORD32 first_tcx_flag; 372 WORD32 *quant; 373 WORD32 core_mode_last, fac_data_present; 374 WORD32 *fac_data; 375 WORD32 first_lpd_flag; 376 WORD32 short_fac_flag; 377 WORD32 bpf_control_info; 378 WORD32 chan = usac_data->present_chan; 379 WORD32 last_lpd_mode = usac_data->str_tddec[chan]->mode_prev; 380 WORD32 err = 0; 381 short_fac_flag = 0; 382 383 pstr_td_frame_data->acelp_core_mode = ixheaacd_read_bits_buf(it_bit_buff, 3); 384 385 lpd_mode = ixheaacd_read_bits_buf(it_bit_buff, 5); 386 387 if (lpd_mode == 25) { 388 pstr_td_frame_data->mod[0] = pstr_td_frame_data->mod[1] = 389 pstr_td_frame_data->mod[2] = pstr_td_frame_data->mod[3] = 3; 390 } else if (lpd_mode == 24) { 391 pstr_td_frame_data->mod[0] = pstr_td_frame_data->mod[1] = 392 pstr_td_frame_data->mod[2] = pstr_td_frame_data->mod[3] = 2; 393 } else { 394 if (lpd_mode >= 20) { 395 pstr_td_frame_data->mod[0] = lpd_mode & 1; 396 pstr_td_frame_data->mod[1] = (lpd_mode >> 1) & 1; 397 pstr_td_frame_data->mod[2] = pstr_td_frame_data->mod[3] = 2; 398 } else if (lpd_mode >= 16) { 399 pstr_td_frame_data->mod[0] = pstr_td_frame_data->mod[1] = 2; 400 pstr_td_frame_data->mod[2] = lpd_mode & 1; 401 pstr_td_frame_data->mod[3] = (lpd_mode >> 1) & 1; 402 } else { 403 pstr_td_frame_data->mod[0] = lpd_mode & 1; 404 pstr_td_frame_data->mod[1] = (lpd_mode >> 1) & 1; 405 pstr_td_frame_data->mod[2] = (lpd_mode >> 2) & 1; 406 pstr_td_frame_data->mod[3] = (lpd_mode >> 3) & 1; 407 } 408 } 409 410 bpf_control_info = ixheaacd_read_bits_buf(it_bit_buff, 1); 411 412 core_mode_last = ixheaacd_read_bits_buf(it_bit_buff, 1); 413 414 fac_data_present = ixheaacd_read_bits_buf(it_bit_buff, 1); 415 416 first_lpd_flag = (core_mode_last == 0) ? 1 : 0; 417 418 quant = pstr_td_frame_data->x_tcx_invquant; 419 first_tcx_flag = 1; 420 k = 0; 421 while (k < 4) { 422 if (k == 0) { 423 if ((core_mode_last == 1) && (fac_data_present == 1)) 424 ixheaacd_fac_decoding((usac_data->len_subfrm) / 2, k, 425 pstr_td_frame_data->fac, it_bit_buff); 426 } else { 427 if (((last_lpd_mode == 0) && (pstr_td_frame_data->mod[k] > 0)) || 428 ((last_lpd_mode > 0) && (pstr_td_frame_data->mod[k] == 0))) 429 ixheaacd_fac_decoding((usac_data->len_subfrm) / 2, k, 430 pstr_td_frame_data->fac, it_bit_buff); 431 } 432 433 if (pstr_td_frame_data->mod[k] == 0) { 434 ixheaacd_acelp_decoding(k, usac_data, pstr_td_frame_data, it_bit_buff, 435 chan); 436 last_lpd_mode = 0; 437 pstr_td_frame_data->tcx_lg[k] = 0; 438 k += 1; 439 } else { 440 ixheaacd_tcx_coding(usac_data, quant, k, first_tcx_flag, 441 pstr_td_frame_data, it_bit_buff); 442 last_lpd_mode = pstr_td_frame_data->mod[k]; 443 quant += pstr_td_frame_data->tcx_lg[k]; 444 445 cnt = 1 << (pstr_td_frame_data->mod[k] - 1); 446 447 for (ii = 0; ii < cnt - 1; ii++) 448 pstr_td_frame_data->tcx_lg[k + 1 + ii] = 0; 449 450 k += cnt; 451 first_tcx_flag = 0; 452 } 453 } 454 455 ixheaacd_lpc_data(first_lpd_flag, pstr_td_frame_data->mod, pstr_td_frame_data, 456 it_bit_buff); 457 458 if ((core_mode_last == 0) && (fac_data_present == 1)) { 459 WORD32 fac_length; 460 short_fac_flag = ixheaacd_read_bits_buf(it_bit_buff, 1); 461 462 fac_length = 463 (short_fac_flag) ? ((usac_data->ccfl) / 16) : ((usac_data->ccfl) / 8); 464 465 fac_data = pstr_td_frame_data->fac_data; 466 fac_data[0] = ixheaacd_read_bits_buf(it_bit_buff, 7); 467 ixheaacd_fac_decoding(fac_length, 0, &fac_data[1], it_bit_buff); 468 } 469 470 err = ixheaacd_lpd_dec(usac_data, usac_data->str_tddec[chan], 471 pstr_td_frame_data, synth, first_lpd_flag, 472 short_fac_flag, bpf_control_info); 473 474 return (err); 475 } 476 477 WORD32 ixheaacd_tw_buff_update(ia_usac_data_struct *usac_data, WORD32 i, 478 ia_usac_lpd_decoder_handle st) { 479 WORD32 *p_ioverlap = usac_data->overlap_data_ptr[i]; 480 WORD32 td_frame_prev = usac_data->td_frame_prev[i]; 481 WORD32 window_sequence_last = usac_data->window_sequence_last[i]; 482 WORD32 tw_mdct = usac_data->tw_mdct[0]; 483 484 if (!td_frame_prev) { 485 if (tw_mdct) { 486 return -1; 487 } else { 488 ixheaacd_reset_acelp_data_fix( 489 usac_data, st, p_ioverlap, 490 (window_sequence_last == EIGHT_SHORT_SEQUENCE), 0); 491 } 492 } 493 return 0; 494 } 495 496 VOID ixheaacd_td_frm_dec(ia_usac_data_struct *usac_data, WORD32 k, 497 WORD32 mod0) { 498 WORD32 i; 499 WORD32 lfac = 0; 500 501 WORD32 *p_out_idata = usac_data->output_data_ptr[k]; 502 WORD32 *p_ioverlap = usac_data->overlap_data_ptr[k]; 503 WORD32 nlong = usac_data->ccfl; 504 WORD32 window_sequence_last = usac_data->window_sequence_last[k]; 505 WORD32 td_frame_prev = usac_data->td_frame_prev[k]; 506 WORD32 tw_mdct = usac_data->tw_mdct[0]; 507 WORD32 nshort = nlong / 8; 508 WORD32 *p_in_idata = p_out_idata; 509 510 if (!td_frame_prev) { 511 if (window_sequence_last == EIGHT_SHORT_SEQUENCE) { 512 lfac = nshort / 2; 513 } else { 514 lfac = nshort; 515 } 516 } 517 518 if (!td_frame_prev && (mod0 == 0)) { 519 for (i = 0; i < (nlong / 2) - lfac - (LEN_SUBFR); i++) { 520 p_in_idata[i] = 0; 521 } 522 } else if (!td_frame_prev && (mod0 > 0)) { 523 for (i = 0; i < (nlong / 2) - lfac; i++) { 524 p_in_idata[i] = 0; 525 } 526 } 527 528 if (tw_mdct) { 529 if (!td_frame_prev && (mod0 == 0)) { 530 for (i = (nlong / 2) - lfac - (LEN_SUBFR); i < nlong / 2; i++) { 531 p_ioverlap[i + (nlong / 2)] = 0; 532 } 533 } 534 for (i = 0; i < nlong / 2; i++) { 535 p_out_idata[i] = p_ioverlap[i] << 1; 536 p_out_idata[i + nlong / 2] = 537 ixheaacd_add32_sat(p_ioverlap[i + nlong / 2] << 1, p_in_idata[i]); 538 p_ioverlap[i] = ixheaacd_add32_sat(p_in_idata[i + (nlong / 2)] >> 1, 539 p_ioverlap[i + nlong]); 540 p_ioverlap[i + (nlong / 2)] = 0; 541 p_ioverlap[i + nlong] = 0; 542 p_ioverlap[i + nlong + (nlong / 2)] = 0; 543 } 544 } else { 545 if (!td_frame_prev && (mod0 == 0)) { 546 for (i = (nlong / 2) - lfac - (LEN_SUBFR); i < nlong / 2; i++) { 547 p_ioverlap[i] = 0; 548 } 549 } else if (!td_frame_prev) { 550 for (i = (nlong / 2) - lfac; i < nlong; i++) { 551 p_ioverlap[i] = 0; 552 } 553 } 554 for (i = 0; i < nlong; i++) { 555 p_out_idata[i] = ixheaacd_add32_sat(p_ioverlap[i] << 1, p_in_idata[i]); 556 p_ioverlap[i] = 0; 557 } 558 } 559 } 560