1*15dc779aSAndroid Build Coastguard Worker /******************************************************************************
2*15dc779aSAndroid Build Coastguard Worker * *
3*15dc779aSAndroid Build Coastguard Worker * Copyright (C) 2023 The Android Open Source Project
4*15dc779aSAndroid Build Coastguard Worker *
5*15dc779aSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
6*15dc779aSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
7*15dc779aSAndroid Build Coastguard Worker * You may obtain a copy of the License at:
8*15dc779aSAndroid Build Coastguard Worker *
9*15dc779aSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
10*15dc779aSAndroid Build Coastguard Worker *
11*15dc779aSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
12*15dc779aSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
13*15dc779aSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14*15dc779aSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
15*15dc779aSAndroid Build Coastguard Worker * limitations under the License.
16*15dc779aSAndroid Build Coastguard Worker *
17*15dc779aSAndroid Build Coastguard Worker *****************************************************************************
18*15dc779aSAndroid Build Coastguard Worker * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
19*15dc779aSAndroid Build Coastguard Worker */
20*15dc779aSAndroid Build Coastguard Worker
21*15dc779aSAndroid Build Coastguard Worker #include <string.h>
22*15dc779aSAndroid Build Coastguard Worker #include <math.h>
23*15dc779aSAndroid Build Coastguard Worker #include "ixheaac_type_def.h"
24*15dc779aSAndroid Build Coastguard Worker #include "ixheaace_adjust_threshold_data.h"
25*15dc779aSAndroid Build Coastguard Worker #include "iusace_bitbuffer.h"
26*15dc779aSAndroid Build Coastguard Worker
27*15dc779aSAndroid Build Coastguard Worker /* DRC */
28*15dc779aSAndroid Build Coastguard Worker #include "impd_drc_common_enc.h"
29*15dc779aSAndroid Build Coastguard Worker #include "impd_drc_uni_drc.h"
30*15dc779aSAndroid Build Coastguard Worker #include "impd_drc_tables.h"
31*15dc779aSAndroid Build Coastguard Worker #include "impd_drc_api.h"
32*15dc779aSAndroid Build Coastguard Worker #include "impd_drc_uni_drc_eq.h"
33*15dc779aSAndroid Build Coastguard Worker #include "impd_drc_uni_drc_filter_bank.h"
34*15dc779aSAndroid Build Coastguard Worker #include "impd_drc_gain_enc.h"
35*15dc779aSAndroid Build Coastguard Worker #include "impd_drc_struct_def.h"
36*15dc779aSAndroid Build Coastguard Worker
37*15dc779aSAndroid Build Coastguard Worker #include "iusace_cnst.h"
38*15dc779aSAndroid Build Coastguard Worker #include "iusace_tns_usac.h"
39*15dc779aSAndroid Build Coastguard Worker #include "iusace_psy_mod.h"
40*15dc779aSAndroid Build Coastguard Worker #include "iusace_psy_utils.h"
41*15dc779aSAndroid Build Coastguard Worker #include "iusace_tns_usac.h"
42*15dc779aSAndroid Build Coastguard Worker #include "iusace_config.h"
43*15dc779aSAndroid Build Coastguard Worker #include "iusace_arith_enc.h"
44*15dc779aSAndroid Build Coastguard Worker #include "iusace_fd_qc_util.h"
45*15dc779aSAndroid Build Coastguard Worker #include "iusace_fd_quant.h"
46*15dc779aSAndroid Build Coastguard Worker #include "iusace_block_switch_const.h"
47*15dc779aSAndroid Build Coastguard Worker #include "iusace_block_switch_struct_def.h"
48*15dc779aSAndroid Build Coastguard Worker #include "iusace_ms.h"
49*15dc779aSAndroid Build Coastguard Worker #include "iusace_signal_classifier.h"
50*15dc779aSAndroid Build Coastguard Worker #include "ixheaace_sbr_header.h"
51*15dc779aSAndroid Build Coastguard Worker #include "ixheaace_config.h"
52*15dc779aSAndroid Build Coastguard Worker #include "ixheaace_asc_write.h"
53*15dc779aSAndroid Build Coastguard Worker #include "iusace_main.h"
54*15dc779aSAndroid Build Coastguard Worker #include "iusace_lpd_rom.h"
55*15dc779aSAndroid Build Coastguard Worker #include "iusace_lpd.h"
56*15dc779aSAndroid Build Coastguard Worker #include "iusace_func_prototypes.h"
57*15dc779aSAndroid Build Coastguard Worker #include "ixheaac_error_standards.h"
58*15dc779aSAndroid Build Coastguard Worker
iusace_init_td_data(ia_usac_td_encoder_struct * st,WORD32 len_frame)59*15dc779aSAndroid Build Coastguard Worker VOID iusace_init_td_data(ia_usac_td_encoder_struct *st, WORD32 len_frame) {
60*15dc779aSAndroid Build Coastguard Worker WORD32 len_window;
61*15dc779aSAndroid Build Coastguard Worker WORD32 num_frames = NUM_FRAMES;
62*15dc779aSAndroid Build Coastguard Worker st->len_subfrm = len_frame / num_frames;
63*15dc779aSAndroid Build Coastguard Worker
64*15dc779aSAndroid Build Coastguard Worker st->len_frame = len_frame;
65*15dc779aSAndroid Build Coastguard Worker st->num_subfrm = (MAX_NUM_SUBFR * len_frame) / LEN_SUPERFRAME;
66*15dc779aSAndroid Build Coastguard Worker
67*15dc779aSAndroid Build Coastguard Worker iusace_reset_td_enc(st);
68*15dc779aSAndroid Build Coastguard Worker st->prev_mode = -1;
69*15dc779aSAndroid Build Coastguard Worker st->arith_reset_flag = 1;
70*15dc779aSAndroid Build Coastguard Worker
71*15dc779aSAndroid Build Coastguard Worker if (st->fscale <= FSCALE_DENOM) {
72*15dc779aSAndroid Build Coastguard Worker len_window = (LEN_LP_WINDOW * len_frame) / LEN_SUPERFRAME;
73*15dc779aSAndroid Build Coastguard Worker } else {
74*15dc779aSAndroid Build Coastguard Worker len_window = (LEN_LP_WINDOW_HIGH_RATE * len_frame) / LEN_SUPERFRAME;
75*15dc779aSAndroid Build Coastguard Worker }
76*15dc779aSAndroid Build Coastguard Worker
77*15dc779aSAndroid Build Coastguard Worker switch (len_window) {
78*15dc779aSAndroid Build Coastguard Worker case 512:
79*15dc779aSAndroid Build Coastguard Worker st->lp_analysis_window = iusace_cos_window_512;
80*15dc779aSAndroid Build Coastguard Worker break;
81*15dc779aSAndroid Build Coastguard Worker case 448:
82*15dc779aSAndroid Build Coastguard Worker st->lp_analysis_window = iusace_cos_window_448;
83*15dc779aSAndroid Build Coastguard Worker break;
84*15dc779aSAndroid Build Coastguard Worker case 384:
85*15dc779aSAndroid Build Coastguard Worker st->lp_analysis_window = iexheaac_cos_window_384;
86*15dc779aSAndroid Build Coastguard Worker break;
87*15dc779aSAndroid Build Coastguard Worker default:
88*15dc779aSAndroid Build Coastguard Worker st->lp_analysis_window = iusace_cos_window_512;
89*15dc779aSAndroid Build Coastguard Worker break;
90*15dc779aSAndroid Build Coastguard Worker }
91*15dc779aSAndroid Build Coastguard Worker return;
92*15dc779aSAndroid Build Coastguard Worker }
93*15dc779aSAndroid Build Coastguard Worker
iusace_config_acelp_core_mode(ia_usac_td_encoder_struct * st,WORD32 sampling_rate,WORD32 bitrate)94*15dc779aSAndroid Build Coastguard Worker VOID iusace_config_acelp_core_mode(ia_usac_td_encoder_struct *st, WORD32 sampling_rate,
95*15dc779aSAndroid Build Coastguard Worker WORD32 bitrate) {
96*15dc779aSAndroid Build Coastguard Worker WORD32 max_bits, coder_bits;
97*15dc779aSAndroid Build Coastguard Worker const WORD32 *p_acelp_core_numbits_table;
98*15dc779aSAndroid Build Coastguard Worker
99*15dc779aSAndroid Build Coastguard Worker p_acelp_core_numbits_table = (WORD32 *)iusace_acelp_core_numbits_1024;
100*15dc779aSAndroid Build Coastguard Worker
101*15dc779aSAndroid Build Coastguard Worker max_bits = (WORD32)((FLOAT32)(bitrate * LEN_SUPERFRAME) / (FLOAT32)sampling_rate);
102*15dc779aSAndroid Build Coastguard Worker
103*15dc779aSAndroid Build Coastguard Worker for (st->acelp_core_mode = 5; st->acelp_core_mode >= 0; st->acelp_core_mode--) {
104*15dc779aSAndroid Build Coastguard Worker coder_bits = p_acelp_core_numbits_table[st->acelp_core_mode];
105*15dc779aSAndroid Build Coastguard Worker if (coder_bits <= max_bits) {
106*15dc779aSAndroid Build Coastguard Worker return;
107*15dc779aSAndroid Build Coastguard Worker }
108*15dc779aSAndroid Build Coastguard Worker }
109*15dc779aSAndroid Build Coastguard Worker if (st->acelp_core_mode == -1) {
110*15dc779aSAndroid Build Coastguard Worker st->acelp_core_mode = 0;
111*15dc779aSAndroid Build Coastguard Worker }
112*15dc779aSAndroid Build Coastguard Worker return;
113*15dc779aSAndroid Build Coastguard Worker }
114*15dc779aSAndroid Build Coastguard Worker
iusace_reset_td_enc(ia_usac_td_encoder_struct * st)115*15dc779aSAndroid Build Coastguard Worker VOID iusace_reset_td_enc(ia_usac_td_encoder_struct *st) {
116*15dc779aSAndroid Build Coastguard Worker WORD32 i;
117*15dc779aSAndroid Build Coastguard Worker
118*15dc779aSAndroid Build Coastguard Worker memset(st->old_speech_pe, 0, (ORDER + LEN_NEXT_HIGH_RATE) * sizeof(FLOAT32));
119*15dc779aSAndroid Build Coastguard Worker memset(st->prev_exc, 0, (MAX_PITCH + LEN_INTERPOL) * sizeof(FLOAT32));
120*15dc779aSAndroid Build Coastguard Worker memset(st->prev_wsp, 0, (MAX_PITCH / OPL_DECIM) * sizeof(FLOAT32));
121*15dc779aSAndroid Build Coastguard Worker memset(st->mem_lp_decim2, 0, 3 * sizeof(FLOAT32));
122*15dc779aSAndroid Build Coastguard Worker memset(st->weighted_sig, 0, 128 * sizeof(FLOAT32));
123*15dc779aSAndroid Build Coastguard Worker
124*15dc779aSAndroid Build Coastguard Worker st->lpd_state.mode = -1;
125*15dc779aSAndroid Build Coastguard Worker st->lpd_state.num_bits = 0;
126*15dc779aSAndroid Build Coastguard Worker memset(st->lpd_state.lpc_coeffs_quant, 0, (2 * (ORDER + 1)) * sizeof(FLOAT32));
127*15dc779aSAndroid Build Coastguard Worker memset(st->lpd_state.lpc_coeffs, 0, (2 * (ORDER + 1)) * sizeof(FLOAT32));
128*15dc779aSAndroid Build Coastguard Worker memset(st->lpd_state.synth, 0, (ORDER + 128) * sizeof(FLOAT32));
129*15dc779aSAndroid Build Coastguard Worker memset(st->lpd_state.wsynth, 0, (1 + 128) * sizeof(FLOAT32));
130*15dc779aSAndroid Build Coastguard Worker
131*15dc779aSAndroid Build Coastguard Worker memset(st->lpd_state.acelp_exc, 0, (2 * LEN_FRAME) * sizeof(FLOAT32));
132*15dc779aSAndroid Build Coastguard Worker
133*15dc779aSAndroid Build Coastguard Worker memset(st->lpd_state.tcx_mem, 0, 128 * sizeof(FLOAT32));
134*15dc779aSAndroid Build Coastguard Worker memset(st->lpd_state.tcx_quant, 0, (1 + 256) * sizeof(FLOAT32));
135*15dc779aSAndroid Build Coastguard Worker st->lpd_state.tcx_fac = 0.0f;
136*15dc779aSAndroid Build Coastguard Worker
137*15dc779aSAndroid Build Coastguard Worker memset(st->prev_hp_wsp, 0,
138*15dc779aSAndroid Build Coastguard Worker (LEN_SUPERFRAME / OPL_DECIM + (MAX_PITCH / OPL_DECIM)) * sizeof(FLOAT32));
139*15dc779aSAndroid Build Coastguard Worker
140*15dc779aSAndroid Build Coastguard Worker memset(st->hp_ol_ltp_mem, 0, (3 * 2 + 1) * sizeof(FLOAT32));
141*15dc779aSAndroid Build Coastguard Worker for (i = 0; i < 5; i++) st->prev_ol_lags[i] = 40;
142*15dc779aSAndroid Build Coastguard Worker st->prev_wsyn_mem = 0.0;
143*15dc779aSAndroid Build Coastguard Worker st->prev_wsp_mem = 0.0;
144*15dc779aSAndroid Build Coastguard Worker st->prev_xnq_mem = 0.0;
145*15dc779aSAndroid Build Coastguard Worker st->mem_wsp = 0.0;
146*15dc779aSAndroid Build Coastguard Worker st->prev_ovlp_size = 0;
147*15dc779aSAndroid Build Coastguard Worker st->prev_pitch_med = 40;
148*15dc779aSAndroid Build Coastguard Worker st->ol_wght_flg = 0;
149*15dc779aSAndroid Build Coastguard Worker st->ada_w = 0.0;
150*15dc779aSAndroid Build Coastguard Worker
151*15dc779aSAndroid Build Coastguard Worker memcpy(st->isf_old, iusace_lsf_init, ORDER * sizeof(FLOAT32));
152*15dc779aSAndroid Build Coastguard Worker memcpy(st->isp_old, iusace_ispold_init, ORDER * sizeof(FLOAT32));
153*15dc779aSAndroid Build Coastguard Worker memcpy(st->isp_old_q, st->isp_old, ORDER * sizeof(FLOAT32));
154*15dc779aSAndroid Build Coastguard Worker
155*15dc779aSAndroid Build Coastguard Worker st->mem_preemph = 0.0;
156*15dc779aSAndroid Build Coastguard Worker memset(st->mem_sig_in, 0, 4 * sizeof(FLOAT32));
157*15dc779aSAndroid Build Coastguard Worker memset(st->xn_buffer, 0, 128 * sizeof(FLOAT32));
158*15dc779aSAndroid Build Coastguard Worker
159*15dc779aSAndroid Build Coastguard Worker return;
160*15dc779aSAndroid Build Coastguard Worker }
161*15dc779aSAndroid Build Coastguard Worker
iusace_highpass_prev_wsp(ia_usac_td_encoder_struct * st,FLOAT32 * decim_sig,WORD32 pitch_max)162*15dc779aSAndroid Build Coastguard Worker VOID iusace_highpass_prev_wsp(ia_usac_td_encoder_struct *st, FLOAT32 *decim_sig,
163*15dc779aSAndroid Build Coastguard Worker WORD32 pitch_max) {
164*15dc779aSAndroid Build Coastguard Worker WORD32 i, k;
165*15dc779aSAndroid Build Coastguard Worker WORD32 wsp_offset = pitch_max / OPL_DECIM;
166*15dc779aSAndroid Build Coastguard Worker WORD32 num_frames = (2 * LEN_SUBFR) / OPL_DECIM;
167*15dc779aSAndroid Build Coastguard Worker FLOAT32 *hp_wsp_mem = st->hp_ol_ltp_mem;
168*15dc779aSAndroid Build Coastguard Worker FLOAT32 *prev_hp_wsp = st->prev_hp_wsp;
169*15dc779aSAndroid Build Coastguard Worker for (i = 0; i < 2 * st->len_subfrm / OPL_DECIM; i += num_frames) {
170*15dc779aSAndroid Build Coastguard Worker FLOAT32 *data_a, *data_b, *hp_wsp, o;
171*15dc779aSAndroid Build Coastguard Worker FLOAT32 *wsp = decim_sig + ORDER + i;
172*15dc779aSAndroid Build Coastguard Worker data_a = hp_wsp_mem;
173*15dc779aSAndroid Build Coastguard Worker data_b = hp_wsp_mem + HP_ORDER;
174*15dc779aSAndroid Build Coastguard Worker hp_wsp = prev_hp_wsp + wsp_offset;
175*15dc779aSAndroid Build Coastguard Worker for (k = 0; k < num_frames; k++) {
176*15dc779aSAndroid Build Coastguard Worker data_b[0] = data_b[1];
177*15dc779aSAndroid Build Coastguard Worker data_b[1] = data_b[2];
178*15dc779aSAndroid Build Coastguard Worker data_b[2] = data_b[3];
179*15dc779aSAndroid Build Coastguard Worker data_b[HP_ORDER] = wsp[k];
180*15dc779aSAndroid Build Coastguard Worker o = data_b[0] * 0.83787057505665F;
181*15dc779aSAndroid Build Coastguard Worker o += data_b[1] * -2.50975570071058F;
182*15dc779aSAndroid Build Coastguard Worker o += data_b[2] * 2.50975570071058F;
183*15dc779aSAndroid Build Coastguard Worker o += data_b[3] * -0.83787057505665F;
184*15dc779aSAndroid Build Coastguard Worker o -= data_a[0] * -2.64436711600664F;
185*15dc779aSAndroid Build Coastguard Worker o -= data_a[1] * 2.35087386625360F;
186*15dc779aSAndroid Build Coastguard Worker o -= data_a[2] * -0.70001156927424F;
187*15dc779aSAndroid Build Coastguard Worker data_a[2] = data_a[1];
188*15dc779aSAndroid Build Coastguard Worker data_a[1] = data_a[0];
189*15dc779aSAndroid Build Coastguard Worker data_a[0] = o;
190*15dc779aSAndroid Build Coastguard Worker hp_wsp[k] = o;
191*15dc779aSAndroid Build Coastguard Worker }
192*15dc779aSAndroid Build Coastguard Worker memmove(prev_hp_wsp, &prev_hp_wsp[num_frames], wsp_offset * sizeof(FLOAT32));
193*15dc779aSAndroid Build Coastguard Worker }
194*15dc779aSAndroid Build Coastguard Worker }
195*15dc779aSAndroid Build Coastguard Worker
iusace_find_weighted_speech(FLOAT32 * filter_coef,FLOAT32 * speech,FLOAT32 * wsp,FLOAT32 * mem_wsp,WORD32 length)196*15dc779aSAndroid Build Coastguard Worker VOID iusace_find_weighted_speech(FLOAT32 *filter_coef, FLOAT32 *speech, FLOAT32 *wsp,
197*15dc779aSAndroid Build Coastguard Worker FLOAT32 *mem_wsp, WORD32 length) {
198*15dc779aSAndroid Build Coastguard Worker WORD32 i_subfr;
199*15dc779aSAndroid Build Coastguard Worker FLOAT32 weighted_lpc[ORDER + 1];
200*15dc779aSAndroid Build Coastguard Worker for (i_subfr = 0; i_subfr < length; i_subfr += LEN_SUBFR) {
201*15dc779aSAndroid Build Coastguard Worker iusace_get_weighted_lpc(filter_coef, weighted_lpc);
202*15dc779aSAndroid Build Coastguard Worker iusace_compute_lp_residual(weighted_lpc, &speech[i_subfr], &wsp[i_subfr], LEN_SUBFR);
203*15dc779aSAndroid Build Coastguard Worker filter_coef += (ORDER + 1);
204*15dc779aSAndroid Build Coastguard Worker }
205*15dc779aSAndroid Build Coastguard Worker iusace_apply_deemph(wsp, TILT_FAC, length, mem_wsp);
206*15dc779aSAndroid Build Coastguard Worker return;
207*15dc779aSAndroid Build Coastguard Worker }
208*15dc779aSAndroid Build Coastguard Worker
iusace_get_interpolated_lpc(FLOAT32 * lsp_old,FLOAT32 * lsp_new,FLOAT32 * lpc,WORD32 num_subfrm)209*15dc779aSAndroid Build Coastguard Worker VOID iusace_get_interpolated_lpc(FLOAT32 *lsp_old, FLOAT32 *lsp_new, FLOAT32 *lpc,
210*15dc779aSAndroid Build Coastguard Worker WORD32 num_subfrm) {
211*15dc779aSAndroid Build Coastguard Worker FLOAT32 lsp[ORDER], *p_lpc, inc, fnew, fold;
212*15dc779aSAndroid Build Coastguard Worker WORD32 i, k;
213*15dc779aSAndroid Build Coastguard Worker
214*15dc779aSAndroid Build Coastguard Worker inc = 1.0f / (FLOAT32)num_subfrm;
215*15dc779aSAndroid Build Coastguard Worker p_lpc = lpc;
216*15dc779aSAndroid Build Coastguard Worker fnew = 0.0f;
217*15dc779aSAndroid Build Coastguard Worker
218*15dc779aSAndroid Build Coastguard Worker for (k = 0; k < num_subfrm; k++) {
219*15dc779aSAndroid Build Coastguard Worker fold = 1.0f - fnew;
220*15dc779aSAndroid Build Coastguard Worker for (i = 0; i < ORDER; i++) {
221*15dc779aSAndroid Build Coastguard Worker lsp[i] = (FLOAT32)(lsp_old[i] * fold + lsp_new[i] * fnew);
222*15dc779aSAndroid Build Coastguard Worker }
223*15dc779aSAndroid Build Coastguard Worker fnew += inc;
224*15dc779aSAndroid Build Coastguard Worker iusace_lsp_to_lp_conversion(lsp, p_lpc);
225*15dc779aSAndroid Build Coastguard Worker p_lpc += (ORDER + 1);
226*15dc779aSAndroid Build Coastguard Worker }
227*15dc779aSAndroid Build Coastguard Worker
228*15dc779aSAndroid Build Coastguard Worker iusace_lsp_to_lp_conversion(lsp_new, p_lpc);
229*15dc779aSAndroid Build Coastguard Worker }
230*15dc779aSAndroid Build Coastguard Worker
iusace_core_lpd_encode(ia_usac_data_struct * usac_data,FLOAT32 * speech,WORD32 * mode,WORD32 * num_tcx_param,WORD32 ch_idx)231*15dc779aSAndroid Build Coastguard Worker VOID iusace_core_lpd_encode(ia_usac_data_struct *usac_data, FLOAT32 *speech, WORD32 *mode,
232*15dc779aSAndroid Build Coastguard Worker WORD32 *num_tcx_param, WORD32 ch_idx) {
233*15dc779aSAndroid Build Coastguard Worker WORD32 first_lpd_flag = (usac_data->core_mode_prev[ch_idx] == CORE_MODE_FD);
234*15dc779aSAndroid Build Coastguard Worker iusace_scratch_mem *pstr_scratch = &usac_data->str_scratch;
235*15dc779aSAndroid Build Coastguard Worker WORD32 pit_adj = usac_data->td_encoder[ch_idx]->fscale;
236*15dc779aSAndroid Build Coastguard Worker WORD32 *num_fac_bits = &usac_data->num_td_fac_bits[ch_idx];
237*15dc779aSAndroid Build Coastguard Worker WORD16 *serial_fac_out = usac_data->fac_out_stream[ch_idx];
238*15dc779aSAndroid Build Coastguard Worker WORD32 *lpc_params = usac_data->param_buf + (NUM_FRAMES * MAX_NUM_TCX_PRM_PER_DIV);
239*15dc779aSAndroid Build Coastguard Worker ia_usac_td_encoder_struct *st = usac_data->td_encoder[ch_idx];
240*15dc779aSAndroid Build Coastguard Worker WORD32 *acelp_tcx_params = usac_data->param_buf;
241*15dc779aSAndroid Build Coastguard Worker WORD16 *codec_mode = &st->acelp_core_mode;
242*15dc779aSAndroid Build Coastguard Worker const FLOAT32 *lp_analysis_window = st->lp_analysis_window;
243*15dc779aSAndroid Build Coastguard Worker FLOAT32 *lp_filter_coeff = pstr_scratch->p_lp_filter_coeff;
244*15dc779aSAndroid Build Coastguard Worker FLOAT32 *lp_filter_coeff_q = pstr_scratch->p_lp_filter_coeff_q;
245*15dc779aSAndroid Build Coastguard Worker
246*15dc779aSAndroid Build Coastguard Worker FLOAT32 *ptr_stack_mem = (FLOAT32 *)pstr_scratch->ptr_stack_mem;
247*15dc779aSAndroid Build Coastguard Worker
248*15dc779aSAndroid Build Coastguard Worker FLOAT32 *auto_corr_vector = ptr_stack_mem;
249*15dc779aSAndroid Build Coastguard Worker ptr_stack_mem += (ORDER + 1);
250*15dc779aSAndroid Build Coastguard Worker memset(auto_corr_vector, 0, sizeof(*auto_corr_vector) * (ORDER + 1));
251*15dc779aSAndroid Build Coastguard Worker
252*15dc779aSAndroid Build Coastguard Worker FLOAT32 *isp_new = ptr_stack_mem;
253*15dc779aSAndroid Build Coastguard Worker ptr_stack_mem += ORDER;
254*15dc779aSAndroid Build Coastguard Worker memset(isp_new, 0, sizeof(*isp_new) * (ORDER));
255*15dc779aSAndroid Build Coastguard Worker
256*15dc779aSAndroid Build Coastguard Worker FLOAT32 *isp_curr = ptr_stack_mem;
257*15dc779aSAndroid Build Coastguard Worker ptr_stack_mem += ((NUM_FRAMES + 1) * ORDER);
258*15dc779aSAndroid Build Coastguard Worker memset(isp_curr, 0, sizeof(*isp_curr) * ((NUM_FRAMES + 1) * ORDER));
259*15dc779aSAndroid Build Coastguard Worker
260*15dc779aSAndroid Build Coastguard Worker FLOAT32 *isp_curr_q = ptr_stack_mem;
261*15dc779aSAndroid Build Coastguard Worker ptr_stack_mem += ((NUM_FRAMES + 1) * ORDER);
262*15dc779aSAndroid Build Coastguard Worker memset(isp_curr_q, 0, sizeof(*isp_curr_q) * ((NUM_FRAMES + 1) * ORDER));
263*15dc779aSAndroid Build Coastguard Worker
264*15dc779aSAndroid Build Coastguard Worker FLOAT32 *isf_curr = ptr_stack_mem;
265*15dc779aSAndroid Build Coastguard Worker ptr_stack_mem += ((NUM_FRAMES + 1) * ORDER);
266*15dc779aSAndroid Build Coastguard Worker memset(isf_curr, 0, sizeof(*isf_curr) * ((NUM_FRAMES + 1) * ORDER));
267*15dc779aSAndroid Build Coastguard Worker
268*15dc779aSAndroid Build Coastguard Worker FLOAT32 *isf_curr_q = ptr_stack_mem;
269*15dc779aSAndroid Build Coastguard Worker ptr_stack_mem += ((NUM_FRAMES + 1) * ORDER);
270*15dc779aSAndroid Build Coastguard Worker memset(isf_curr_q, 0, sizeof(*isf_curr_q) * ((NUM_FRAMES + 1) * ORDER));
271*15dc779aSAndroid Build Coastguard Worker
272*15dc779aSAndroid Build Coastguard Worker FLOAT32 *auto_corr_lp_filter_coeff = ptr_stack_mem;
273*15dc779aSAndroid Build Coastguard Worker ptr_stack_mem += (ORDER + 1);
274*15dc779aSAndroid Build Coastguard Worker memset(auto_corr_lp_filter_coeff, 0, sizeof(*auto_corr_lp_filter_coeff) * (ORDER + 1));
275*15dc779aSAndroid Build Coastguard Worker
276*15dc779aSAndroid Build Coastguard Worker WORD32 num_indices = 0, num_bits = 0;
277*15dc779aSAndroid Build Coastguard Worker WORD32 *prm_tcx = pstr_scratch->p_prm_tcx;
278*15dc779aSAndroid Build Coastguard Worker FLOAT32 *p_wsp_prev_buf;
279*15dc779aSAndroid Build Coastguard Worker FLOAT32 *wsp_prev_buf = pstr_scratch->p_wsp_prev_buf;
280*15dc779aSAndroid Build Coastguard Worker memset(lp_filter_coeff, 0, ((NUM_SUBFR_SUPERFRAME + 1) * (ORDER + 1)) * sizeof(FLOAT32));
281*15dc779aSAndroid Build Coastguard Worker memset(lp_filter_coeff_q, 0, ((NUM_SUBFR_SUPERFRAME + 1) * (ORDER + 1)) * sizeof(FLOAT32));
282*15dc779aSAndroid Build Coastguard Worker memset(wsp_prev_buf, 0, ((MAX_PITCH1 / OPL_DECIM) + LEN_FRAME) * sizeof(FLOAT32));
283*15dc779aSAndroid Build Coastguard Worker WORD32 i, j, k, i1, i2;
284*15dc779aSAndroid Build Coastguard Worker WORD32 *p_params;
285*15dc779aSAndroid Build Coastguard Worker FLOAT32 energy = 0, max_corr = 0, t0 = 0, *p, *p1;
286*15dc779aSAndroid Build Coastguard Worker WORD32 ol_pitch_lag[2 * NUM_FRAMES] = {0};
287*15dc779aSAndroid Build Coastguard Worker FLOAT32 norm_corr[2 * NUM_FRAMES] = {0};
288*15dc779aSAndroid Build Coastguard Worker WORD32 num_params, pitch_min, pitch_max;
289*15dc779aSAndroid Build Coastguard Worker
290*15dc779aSAndroid Build Coastguard Worker ia_usac_lpd_state_struct *lpd_state[6], *lpd_state_temp;
291*15dc779aSAndroid Build Coastguard Worker lpd_state_temp = (ia_usac_lpd_state_struct *)ptr_stack_mem;
292*15dc779aSAndroid Build Coastguard Worker ptr_stack_mem +=
293*15dc779aSAndroid Build Coastguard Worker (sizeof(ia_usac_lpd_state_struct) + sizeof(*ptr_stack_mem)) / (sizeof(*ptr_stack_mem));
294*15dc779aSAndroid Build Coastguard Worker memset(lpd_state_temp, 0, sizeof(*lpd_state_temp));
295*15dc779aSAndroid Build Coastguard Worker for (j = 0; j < 6; j++) {
296*15dc779aSAndroid Build Coastguard Worker lpd_state[j] = (ia_usac_lpd_state_struct *)ptr_stack_mem;
297*15dc779aSAndroid Build Coastguard Worker ptr_stack_mem +=
298*15dc779aSAndroid Build Coastguard Worker (sizeof(ia_usac_lpd_state_struct) + sizeof(*ptr_stack_mem)) / (sizeof(*ptr_stack_mem));
299*15dc779aSAndroid Build Coastguard Worker memset(lpd_state[j], 0, sizeof(*lpd_state[0]));
300*15dc779aSAndroid Build Coastguard Worker }
301*15dc779aSAndroid Build Coastguard Worker
302*15dc779aSAndroid Build Coastguard Worker WORD32 num_bits_acelp = 0, num_bits_tcx = 0;
303*15dc779aSAndroid Build Coastguard Worker WORD32 range_pitch_search = 0;
304*15dc779aSAndroid Build Coastguard Worker WORD32 len_subfrm = 0;
305*15dc779aSAndroid Build Coastguard Worker WORD32 num_subfrm = 0;
306*15dc779aSAndroid Build Coastguard Worker WORD32 num_sbfrm_per_supfrm = 0;
307*15dc779aSAndroid Build Coastguard Worker WORD32 window_len = 0;
308*15dc779aSAndroid Build Coastguard Worker WORD32 len = (MAX_PITCH / OPL_DECIM);
309*15dc779aSAndroid Build Coastguard Worker FLOAT32 mem_wsyn;
310*15dc779aSAndroid Build Coastguard Worker FLOAT32 ssnr_256 = 0.0f, ssnr_512 = 0.0f, ssnr_1024 = 0.0f;
311*15dc779aSAndroid Build Coastguard Worker FLOAT32 tmp_ssnr = 0.0f;
312*15dc779aSAndroid Build Coastguard Worker
313*15dc779aSAndroid Build Coastguard Worker len_subfrm = st->len_subfrm;
314*15dc779aSAndroid Build Coastguard Worker num_subfrm = st->num_subfrm;
315*15dc779aSAndroid Build Coastguard Worker num_sbfrm_per_supfrm = NUM_FRAMES * num_subfrm;
316*15dc779aSAndroid Build Coastguard Worker
317*15dc779aSAndroid Build Coastguard Worker if (pit_adj <= FSCALE_DENOM) {
318*15dc779aSAndroid Build Coastguard Worker window_len = (LEN_LP_WINDOW * len_subfrm) / LEN_FRAME;
319*15dc779aSAndroid Build Coastguard Worker } else {
320*15dc779aSAndroid Build Coastguard Worker window_len = (LEN_LP_WINDOW_HIGH_RATE * len_subfrm) / LEN_FRAME;
321*15dc779aSAndroid Build Coastguard Worker }
322*15dc779aSAndroid Build Coastguard Worker
323*15dc779aSAndroid Build Coastguard Worker memcpy(pstr_scratch->p_wsig_buf, st->weighted_sig, 128 * sizeof(FLOAT32));
324*15dc779aSAndroid Build Coastguard Worker
325*15dc779aSAndroid Build Coastguard Worker num_bits_acelp = (iusace_acelp_core_numbits_1024[*codec_mode] - NBITS_MODE) >> 2;
326*15dc779aSAndroid Build Coastguard Worker
327*15dc779aSAndroid Build Coastguard Worker num_bits_tcx = (WORD32)(0.85f * num_bits_acelp - NBITS_LPC);
328*15dc779aSAndroid Build Coastguard Worker
329*15dc779aSAndroid Build Coastguard Worker if (pit_adj == 0) {
330*15dc779aSAndroid Build Coastguard Worker pitch_min = TMIN;
331*15dc779aSAndroid Build Coastguard Worker pitch_max = TMAX;
332*15dc779aSAndroid Build Coastguard Worker } else {
333*15dc779aSAndroid Build Coastguard Worker i = (((pit_adj * TMIN) + (FSCALE_DENOM / 2)) / FSCALE_DENOM) - TMIN;
334*15dc779aSAndroid Build Coastguard Worker pitch_min = TMIN + i;
335*15dc779aSAndroid Build Coastguard Worker pitch_max = TMAX + (6 * i);
336*15dc779aSAndroid Build Coastguard Worker }
337*15dc779aSAndroid Build Coastguard Worker
338*15dc779aSAndroid Build Coastguard Worker p_wsp_prev_buf = wsp_prev_buf + MAX_PITCH1 / OPL_DECIM;
339*15dc779aSAndroid Build Coastguard Worker memcpy(wsp_prev_buf, st->prev_wsp, (WORD32)((MAX_PITCH / OPL_DECIM) * sizeof(FLOAT32)));
340*15dc779aSAndroid Build Coastguard Worker
341*15dc779aSAndroid Build Coastguard Worker if (first_lpd_flag) {
342*15dc779aSAndroid Build Coastguard Worker memcpy(st->isp_old, iusace_ispold_init, ORDER * sizeof(FLOAT32));
343*15dc779aSAndroid Build Coastguard Worker
344*15dc779aSAndroid Build Coastguard Worker iusace_autocorr_plus(&speech[-(window_len / 2)], auto_corr_vector, window_len,
345*15dc779aSAndroid Build Coastguard Worker (FLOAT32 *)lp_analysis_window, pstr_scratch->p_buf_aut_corr);
346*15dc779aSAndroid Build Coastguard Worker
347*15dc779aSAndroid Build Coastguard Worker for (j = 0; j <= ORDER; j++) {
348*15dc779aSAndroid Build Coastguard Worker auto_corr_vector[j] *= (FLOAT32)iusace_lag_window[j];
349*15dc779aSAndroid Build Coastguard Worker }
350*15dc779aSAndroid Build Coastguard Worker
351*15dc779aSAndroid Build Coastguard Worker iusace_levinson_durbin_algo(auto_corr_vector, auto_corr_lp_filter_coeff);
352*15dc779aSAndroid Build Coastguard Worker iusace_lpc_2_lsp_conversion(auto_corr_lp_filter_coeff, isp_new, st->isp_old);
353*15dc779aSAndroid Build Coastguard Worker memcpy(st->isp_old, isp_new, ORDER * sizeof(FLOAT32));
354*15dc779aSAndroid Build Coastguard Worker iusace_lsp_2_lsf_conversion(isp_new, isf_curr);
355*15dc779aSAndroid Build Coastguard Worker memcpy(st->isf_old, isf_curr, ORDER * sizeof(FLOAT32));
356*15dc779aSAndroid Build Coastguard Worker }
357*15dc779aSAndroid Build Coastguard Worker
358*15dc779aSAndroid Build Coastguard Worker memcpy(isp_curr, st->isp_old, ORDER * sizeof(FLOAT32));
359*15dc779aSAndroid Build Coastguard Worker
360*15dc779aSAndroid Build Coastguard Worker for (i = 0; i < NUM_FRAMES; i++) {
361*15dc779aSAndroid Build Coastguard Worker iusace_autocorr_plus(&speech[((i + 1) * len_subfrm) - (window_len / 2)], auto_corr_vector,
362*15dc779aSAndroid Build Coastguard Worker window_len, (FLOAT32 *)lp_analysis_window, pstr_scratch->p_buf_aut_corr);
363*15dc779aSAndroid Build Coastguard Worker
364*15dc779aSAndroid Build Coastguard Worker for (j = 0; j <= ORDER; j++) {
365*15dc779aSAndroid Build Coastguard Worker auto_corr_vector[j] *= (FLOAT32)iusace_lag_window[j];
366*15dc779aSAndroid Build Coastguard Worker }
367*15dc779aSAndroid Build Coastguard Worker
368*15dc779aSAndroid Build Coastguard Worker iusace_levinson_durbin_algo(auto_corr_vector, auto_corr_lp_filter_coeff);
369*15dc779aSAndroid Build Coastguard Worker iusace_lpc_2_lsp_conversion(auto_corr_lp_filter_coeff, isp_new, st->isp_old);
370*15dc779aSAndroid Build Coastguard Worker memcpy(&isp_curr[(i + 1) * ORDER], isp_new, ORDER * sizeof(FLOAT32));
371*15dc779aSAndroid Build Coastguard Worker iusace_interpolation_lsp_params(st->isp_old, isp_new,
372*15dc779aSAndroid Build Coastguard Worker &lp_filter_coeff[i * num_subfrm * (ORDER + 1)], num_subfrm);
373*15dc779aSAndroid Build Coastguard Worker iusace_lsp_2_lsf_conversion(&isp_curr[(i + 1) * ORDER], &isf_curr[(i + 1) * ORDER]);
374*15dc779aSAndroid Build Coastguard Worker memcpy(st->isp_old, isp_new, ORDER * sizeof(FLOAT32));
375*15dc779aSAndroid Build Coastguard Worker }
376*15dc779aSAndroid Build Coastguard Worker
377*15dc779aSAndroid Build Coastguard Worker memcpy(isf_curr, st->isf_old, ORDER * sizeof(FLOAT32));
378*15dc779aSAndroid Build Coastguard Worker memcpy(st->isf_old, &isf_curr[NUM_FRAMES * ORDER], ORDER * sizeof(FLOAT32));
379*15dc779aSAndroid Build Coastguard Worker
380*15dc779aSAndroid Build Coastguard Worker if (!first_lpd_flag) {
381*15dc779aSAndroid Build Coastguard Worker iusace_lsp_2_lsf_conversion(st->isp_old_q, isf_curr_q);
382*15dc779aSAndroid Build Coastguard Worker }
383*15dc779aSAndroid Build Coastguard Worker
384*15dc779aSAndroid Build Coastguard Worker iusace_quantize_lpc_avq(&isf_curr[ORDER], &isf_curr_q[ORDER], first_lpd_flag, &lpc_params[0],
385*15dc779aSAndroid Build Coastguard Worker &num_indices, &num_bits);
386*15dc779aSAndroid Build Coastguard Worker
387*15dc779aSAndroid Build Coastguard Worker for (i = 0; i < NUM_FRAMES; i++) {
388*15dc779aSAndroid Build Coastguard Worker iusace_lsf_2_lsp_conversion(&isf_curr_q[(i + 1) * ORDER], &isp_curr_q[(i + 1) * ORDER]);
389*15dc779aSAndroid Build Coastguard Worker }
390*15dc779aSAndroid Build Coastguard Worker
391*15dc779aSAndroid Build Coastguard Worker if (first_lpd_flag) {
392*15dc779aSAndroid Build Coastguard Worker iusace_lsf_2_lsp_conversion(isf_curr_q, isp_curr_q);
393*15dc779aSAndroid Build Coastguard Worker memcpy(st->isp_old_q, isp_curr_q, ORDER * sizeof(FLOAT32));
394*15dc779aSAndroid Build Coastguard Worker }
395*15dc779aSAndroid Build Coastguard Worker
396*15dc779aSAndroid Build Coastguard Worker *num_fac_bits = 0;
397*15dc779aSAndroid Build Coastguard Worker if (first_lpd_flag) {
398*15dc779aSAndroid Build Coastguard Worker FLOAT32 *temp_speech = pstr_scratch->p_buf_speech;
399*15dc779aSAndroid Build Coastguard Worker FLOAT32 *temp_res = pstr_scratch->p_buf_res;
400*15dc779aSAndroid Build Coastguard Worker FLOAT32 lpc[9 * (ORDER + 1)];
401*15dc779aSAndroid Build Coastguard Worker FLOAT32 hp_mem[4];
402*15dc779aSAndroid Build Coastguard Worker FLOAT32 premph_mem = 0.0f;
403*15dc779aSAndroid Build Coastguard Worker WORD32 fac_length;
404*15dc779aSAndroid Build Coastguard Worker WORD32 num_bits_fac = (WORD32)((FLOAT32)num_bits_tcx / 2.f);
405*15dc779aSAndroid Build Coastguard Worker FLOAT32 *temp_signal = pstr_scratch->p_buf_signal;
406*15dc779aSAndroid Build Coastguard Worker
407*15dc779aSAndroid Build Coastguard Worker if (st->last_was_short) {
408*15dc779aSAndroid Build Coastguard Worker fac_length = (st->len_frame) / 16;
409*15dc779aSAndroid Build Coastguard Worker } else {
410*15dc779aSAndroid Build Coastguard Worker fac_length = len_subfrm / 2;
411*15dc779aSAndroid Build Coastguard Worker }
412*15dc779aSAndroid Build Coastguard Worker
413*15dc779aSAndroid Build Coastguard Worker iusace_get_interpolated_lpc(st->isp_old_q, st->isp_old_q, lpc, (2 * len_subfrm) / LEN_SUBFR);
414*15dc779aSAndroid Build Coastguard Worker
415*15dc779aSAndroid Build Coastguard Worker memset(temp_speech, 0, (ORDER + 2 * len_subfrm) * sizeof(FLOAT32));
416*15dc779aSAndroid Build Coastguard Worker memset(temp_res, 0, (2 * len_subfrm) * sizeof(FLOAT32));
417*15dc779aSAndroid Build Coastguard Worker
418*15dc779aSAndroid Build Coastguard Worker iusace_fac_apply(&st->fd_orig[1 + ORDER], len_subfrm, fac_length, st->low_pass_line,
419*15dc779aSAndroid Build Coastguard Worker num_bits_fac, &st->fd_synth[1 + ORDER], lpc, serial_fac_out, num_fac_bits,
420*15dc779aSAndroid Build Coastguard Worker pstr_scratch);
421*15dc779aSAndroid Build Coastguard Worker memset(hp_mem, 0, 4 * sizeof(FLOAT32));
422*15dc779aSAndroid Build Coastguard Worker iusace_highpass_50hz_12k8(st->fd_orig, 2 * len_subfrm + 1 + ORDER, hp_mem, pit_adj);
423*15dc779aSAndroid Build Coastguard Worker premph_mem = 0.0f;
424*15dc779aSAndroid Build Coastguard Worker iusace_apply_preemph(st->fd_orig, PREEMPH_FILT_FAC, 2 * len_subfrm + 1 + ORDER, &premph_mem);
425*15dc779aSAndroid Build Coastguard Worker
426*15dc779aSAndroid Build Coastguard Worker memcpy(temp_signal, st->fd_orig + len_subfrm + 1, (len_subfrm + ORDER) * sizeof(FLOAT32));
427*15dc779aSAndroid Build Coastguard Worker premph_mem = temp_signal[0];
428*15dc779aSAndroid Build Coastguard Worker iusace_apply_deemph(temp_signal, PREEMPH_FILT_FAC, len_subfrm + ORDER, &premph_mem);
429*15dc779aSAndroid Build Coastguard Worker memcpy(st->lpd_state.tcx_mem, &temp_signal[len_subfrm + ORDER - 128], 128 * sizeof(FLOAT32));
430*15dc779aSAndroid Build Coastguard Worker
431*15dc779aSAndroid Build Coastguard Worker premph_mem = 0.0f;
432*15dc779aSAndroid Build Coastguard Worker iusace_apply_preemph(st->fd_synth, PREEMPH_FILT_FAC, 2 * len_subfrm + 1 + ORDER, &premph_mem);
433*15dc779aSAndroid Build Coastguard Worker memcpy(st->lpd_state.synth, st->fd_synth + 2 * len_subfrm - ORDER - 128 + 1 + ORDER,
434*15dc779aSAndroid Build Coastguard Worker (ORDER + 128) * sizeof(FLOAT32));
435*15dc779aSAndroid Build Coastguard Worker memcpy(temp_speech + ORDER, st->fd_synth + 1 + ORDER, 2 * len_subfrm * sizeof(FLOAT32));
436*15dc779aSAndroid Build Coastguard Worker
437*15dc779aSAndroid Build Coastguard Worker premph_mem = 0.0f;
438*15dc779aSAndroid Build Coastguard Worker iusace_find_weighted_speech(lpc, temp_speech + ORDER, temp_res, &premph_mem, 2 * len_subfrm);
439*15dc779aSAndroid Build Coastguard Worker st->prev_wsyn_mem = premph_mem;
440*15dc779aSAndroid Build Coastguard Worker memcpy(st->lpd_state.wsynth, temp_res + 2 * len_subfrm - ORDER - 128,
441*15dc779aSAndroid Build Coastguard Worker (ORDER + 128) * sizeof(FLOAT32));
442*15dc779aSAndroid Build Coastguard Worker memcpy(temp_speech + ORDER, st->fd_synth + 1 + ORDER, 2 * len_subfrm * sizeof(FLOAT32));
443*15dc779aSAndroid Build Coastguard Worker memset(temp_res, 0, 2 * len_subfrm * sizeof(FLOAT32));
444*15dc779aSAndroid Build Coastguard Worker for (i = 0; i < 2 * len_subfrm; i += LEN_SUBFR) {
445*15dc779aSAndroid Build Coastguard Worker iusace_compute_lp_residual(lpc, &temp_speech[ORDER + i], &temp_res[i], LEN_SUBFR);
446*15dc779aSAndroid Build Coastguard Worker }
447*15dc779aSAndroid Build Coastguard Worker memcpy(st->lpd_state.acelp_exc, temp_res, 2 * len_subfrm * sizeof(FLOAT32));
448*15dc779aSAndroid Build Coastguard Worker premph_mem = 0.0f;
449*15dc779aSAndroid Build Coastguard Worker iusace_find_weighted_speech(lp_filter_coeff, st->fd_orig + 1 + ORDER, temp_speech + ORDER,
450*15dc779aSAndroid Build Coastguard Worker &(st->mem_wsp), 2 * len_subfrm);
451*15dc779aSAndroid Build Coastguard Worker memcpy(st->weighted_sig, temp_speech + ORDER + 2 * len_subfrm - 128, 128 * sizeof(FLOAT32));
452*15dc779aSAndroid Build Coastguard Worker memcpy(pstr_scratch->p_wsig_buf, st->weighted_sig, 128 * sizeof(FLOAT32));
453*15dc779aSAndroid Build Coastguard Worker for (i = 0; i < 2 * len_subfrm; i += len_subfrm) {
454*15dc779aSAndroid Build Coastguard Worker iusace_decim2_fir_filter(&temp_speech[i + ORDER], len_subfrm, st->mem_lp_decim2,
455*15dc779aSAndroid Build Coastguard Worker pstr_scratch->p_fir_sig_buf);
456*15dc779aSAndroid Build Coastguard Worker memcpy(temp_speech + ORDER + i / OPL_DECIM, temp_speech + ORDER + i,
457*15dc779aSAndroid Build Coastguard Worker (len_subfrm / OPL_DECIM) * sizeof(FLOAT32));
458*15dc779aSAndroid Build Coastguard Worker }
459*15dc779aSAndroid Build Coastguard Worker memcpy(wsp_prev_buf, temp_speech + ORDER + 2 * len_subfrm / OPL_DECIM - MAX_PITCH / OPL_DECIM,
460*15dc779aSAndroid Build Coastguard Worker (WORD32)((MAX_PITCH / OPL_DECIM) * sizeof(FLOAT32)));
461*15dc779aSAndroid Build Coastguard Worker iusace_highpass_prev_wsp(st, temp_speech, pitch_max);
462*15dc779aSAndroid Build Coastguard Worker }
463*15dc779aSAndroid Build Coastguard Worker memcpy(isp_curr_q, st->isp_old_q, ORDER * sizeof(FLOAT32));
464*15dc779aSAndroid Build Coastguard Worker memcpy(st->isp_old_q, &isp_curr_q[NUM_FRAMES * ORDER], ORDER * sizeof(FLOAT32));
465*15dc779aSAndroid Build Coastguard Worker
466*15dc779aSAndroid Build Coastguard Worker for (i = 0; i < NUM_FRAMES; i++) {
467*15dc779aSAndroid Build Coastguard Worker iusace_find_weighted_speech(
468*15dc779aSAndroid Build Coastguard Worker &lp_filter_coeff[i * (num_sbfrm_per_supfrm / NUM_FRAMES) * (ORDER + 1)],
469*15dc779aSAndroid Build Coastguard Worker &speech[i * len_subfrm], &pstr_scratch->p_wsig_buf[i * len_subfrm], &(st->mem_wsp),
470*15dc779aSAndroid Build Coastguard Worker len_subfrm);
471*15dc779aSAndroid Build Coastguard Worker memcpy(p_wsp_prev_buf, &pstr_scratch->p_wsig_buf[i * len_subfrm],
472*15dc779aSAndroid Build Coastguard Worker len_subfrm * sizeof(FLOAT32));
473*15dc779aSAndroid Build Coastguard Worker
474*15dc779aSAndroid Build Coastguard Worker iusace_decim2_fir_filter(p_wsp_prev_buf, len_subfrm, st->mem_lp_decim2,
475*15dc779aSAndroid Build Coastguard Worker pstr_scratch->p_fir_sig_buf);
476*15dc779aSAndroid Build Coastguard Worker range_pitch_search = 2 * LEN_SUBFR;
477*15dc779aSAndroid Build Coastguard Worker if (num_subfrm < 4) {
478*15dc779aSAndroid Build Coastguard Worker range_pitch_search = 3 * LEN_SUBFR;
479*15dc779aSAndroid Build Coastguard Worker }
480*15dc779aSAndroid Build Coastguard Worker
481*15dc779aSAndroid Build Coastguard Worker iusace_open_loop_search(p_wsp_prev_buf, (pitch_min / OPL_DECIM) + 1, pitch_max / OPL_DECIM,
482*15dc779aSAndroid Build Coastguard Worker range_pitch_search / OPL_DECIM, &ol_pitch_lag[i * 2], st);
483*15dc779aSAndroid Build Coastguard Worker
484*15dc779aSAndroid Build Coastguard Worker if (st->ol_gain > 0.6) {
485*15dc779aSAndroid Build Coastguard Worker st->prev_pitch_med = iusace_get_ol_lag_median(ol_pitch_lag[i * 2], st->prev_ol_lags);
486*15dc779aSAndroid Build Coastguard Worker st->ada_w = 1.0;
487*15dc779aSAndroid Build Coastguard Worker } else {
488*15dc779aSAndroid Build Coastguard Worker st->ada_w = st->ada_w * 0.9f;
489*15dc779aSAndroid Build Coastguard Worker }
490*15dc779aSAndroid Build Coastguard Worker if (st->ada_w < 0.8) {
491*15dc779aSAndroid Build Coastguard Worker st->ol_wght_flg = 0;
492*15dc779aSAndroid Build Coastguard Worker } else {
493*15dc779aSAndroid Build Coastguard Worker st->ol_wght_flg = 1;
494*15dc779aSAndroid Build Coastguard Worker }
495*15dc779aSAndroid Build Coastguard Worker
496*15dc779aSAndroid Build Coastguard Worker max_corr = 0.0f;
497*15dc779aSAndroid Build Coastguard Worker p = &p_wsp_prev_buf[0];
498*15dc779aSAndroid Build Coastguard Worker p1 = p_wsp_prev_buf - ol_pitch_lag[i * 2];
499*15dc779aSAndroid Build Coastguard Worker for (j = 0; j < range_pitch_search / OPL_DECIM; j++) {
500*15dc779aSAndroid Build Coastguard Worker max_corr += *p++ * *p1++;
501*15dc779aSAndroid Build Coastguard Worker }
502*15dc779aSAndroid Build Coastguard Worker
503*15dc779aSAndroid Build Coastguard Worker t0 = 0.01f;
504*15dc779aSAndroid Build Coastguard Worker p = p_wsp_prev_buf - ol_pitch_lag[i * 2];
505*15dc779aSAndroid Build Coastguard Worker for (j = 0; j < range_pitch_search / OPL_DECIM; j++, p++) {
506*15dc779aSAndroid Build Coastguard Worker t0 += *p * *p;
507*15dc779aSAndroid Build Coastguard Worker }
508*15dc779aSAndroid Build Coastguard Worker t0 = (FLOAT32)(1.0 / sqrt(t0));
509*15dc779aSAndroid Build Coastguard Worker norm_corr[i * 2] = max_corr * t0;
510*15dc779aSAndroid Build Coastguard Worker
511*15dc779aSAndroid Build Coastguard Worker energy = 0.01f;
512*15dc779aSAndroid Build Coastguard Worker for (j = 0; j < range_pitch_search / OPL_DECIM; j++) {
513*15dc779aSAndroid Build Coastguard Worker energy += p_wsp_prev_buf[j] * p_wsp_prev_buf[j];
514*15dc779aSAndroid Build Coastguard Worker }
515*15dc779aSAndroid Build Coastguard Worker energy = (FLOAT32)(1.0 / sqrt(energy));
516*15dc779aSAndroid Build Coastguard Worker norm_corr[i * 2] *= energy;
517*15dc779aSAndroid Build Coastguard Worker
518*15dc779aSAndroid Build Coastguard Worker if (num_subfrm < 4) {
519*15dc779aSAndroid Build Coastguard Worker ol_pitch_lag[(i * 2) + 1] = ol_pitch_lag[i * 2];
520*15dc779aSAndroid Build Coastguard Worker norm_corr[(i * 2) + 1] = norm_corr[i * 2];
521*15dc779aSAndroid Build Coastguard Worker } else {
522*15dc779aSAndroid Build Coastguard Worker iusace_open_loop_search(p_wsp_prev_buf + ((2 * LEN_SUBFR) / OPL_DECIM),
523*15dc779aSAndroid Build Coastguard Worker (pitch_min / OPL_DECIM) + 1, pitch_max / OPL_DECIM,
524*15dc779aSAndroid Build Coastguard Worker (2 * LEN_SUBFR) / OPL_DECIM, &ol_pitch_lag[(i * 2) + 1], st);
525*15dc779aSAndroid Build Coastguard Worker
526*15dc779aSAndroid Build Coastguard Worker if (st->ol_gain > 0.6) {
527*15dc779aSAndroid Build Coastguard Worker st->prev_pitch_med =
528*15dc779aSAndroid Build Coastguard Worker iusace_get_ol_lag_median(ol_pitch_lag[(i * 2) + 1], st->prev_ol_lags);
529*15dc779aSAndroid Build Coastguard Worker st->ada_w = 1.0;
530*15dc779aSAndroid Build Coastguard Worker } else {
531*15dc779aSAndroid Build Coastguard Worker st->ada_w = st->ada_w * 0.9f;
532*15dc779aSAndroid Build Coastguard Worker }
533*15dc779aSAndroid Build Coastguard Worker if (st->ada_w < 0.8) {
534*15dc779aSAndroid Build Coastguard Worker st->ol_wght_flg = 0;
535*15dc779aSAndroid Build Coastguard Worker } else {
536*15dc779aSAndroid Build Coastguard Worker st->ol_wght_flg = 1;
537*15dc779aSAndroid Build Coastguard Worker }
538*15dc779aSAndroid Build Coastguard Worker max_corr = 0.0f;
539*15dc779aSAndroid Build Coastguard Worker p = p_wsp_prev_buf + (2 * LEN_SUBFR) / OPL_DECIM;
540*15dc779aSAndroid Build Coastguard Worker p1 = p_wsp_prev_buf + ((2 * LEN_SUBFR) / OPL_DECIM) - ol_pitch_lag[(i * 2) + 1];
541*15dc779aSAndroid Build Coastguard Worker for (j = 0; j < (2 * LEN_SUBFR) / OPL_DECIM; j++) {
542*15dc779aSAndroid Build Coastguard Worker max_corr += *p++ * *p1++;
543*15dc779aSAndroid Build Coastguard Worker }
544*15dc779aSAndroid Build Coastguard Worker
545*15dc779aSAndroid Build Coastguard Worker t0 = 0.01f;
546*15dc779aSAndroid Build Coastguard Worker p = p_wsp_prev_buf + ((2 * LEN_SUBFR) / OPL_DECIM) - ol_pitch_lag[(i * 2) + 1];
547*15dc779aSAndroid Build Coastguard Worker for (j = 0; j < (2 * LEN_SUBFR) / OPL_DECIM; j++, p++) {
548*15dc779aSAndroid Build Coastguard Worker t0 += *p * *p;
549*15dc779aSAndroid Build Coastguard Worker }
550*15dc779aSAndroid Build Coastguard Worker t0 = (FLOAT32)(1.0 / sqrt(t0));
551*15dc779aSAndroid Build Coastguard Worker norm_corr[(i * 2) + 1] = max_corr * t0;
552*15dc779aSAndroid Build Coastguard Worker
553*15dc779aSAndroid Build Coastguard Worker energy = 0.01f;
554*15dc779aSAndroid Build Coastguard Worker for (j = 0; j < (2 * LEN_SUBFR) / OPL_DECIM; j++) {
555*15dc779aSAndroid Build Coastguard Worker energy += p_wsp_prev_buf[((2 * LEN_SUBFR) / OPL_DECIM) + j] *
556*15dc779aSAndroid Build Coastguard Worker p_wsp_prev_buf[((2 * LEN_SUBFR) / OPL_DECIM) + j];
557*15dc779aSAndroid Build Coastguard Worker }
558*15dc779aSAndroid Build Coastguard Worker energy = (FLOAT32)(1.0 / sqrt(energy));
559*15dc779aSAndroid Build Coastguard Worker norm_corr[(i * 2) + 1] *= energy;
560*15dc779aSAndroid Build Coastguard Worker }
561*15dc779aSAndroid Build Coastguard Worker
562*15dc779aSAndroid Build Coastguard Worker memmove(wsp_prev_buf, &wsp_prev_buf[len_subfrm / OPL_DECIM],
563*15dc779aSAndroid Build Coastguard Worker (WORD32)((MAX_PITCH / OPL_DECIM) * sizeof(FLOAT32)));
564*15dc779aSAndroid Build Coastguard Worker }
565*15dc779aSAndroid Build Coastguard Worker
566*15dc779aSAndroid Build Coastguard Worker memcpy(lpd_state[0], &st->lpd_state, sizeof(*lpd_state[0]));
567*15dc779aSAndroid Build Coastguard Worker
568*15dc779aSAndroid Build Coastguard Worker ssnr_1024 = 0;
569*15dc779aSAndroid Build Coastguard Worker if (usac_data->use_acelp_only) {
570*15dc779aSAndroid Build Coastguard Worker for (i1 = 0; i1 < 2; i1++) {
571*15dc779aSAndroid Build Coastguard Worker for (i2 = 0; i2 < 2; i2++) {
572*15dc779aSAndroid Build Coastguard Worker k = (i1 * 2) + i2;
573*15dc779aSAndroid Build Coastguard Worker p_params = acelp_tcx_params + (k * MAX_NUM_TCX_PRM_PER_DIV);
574*15dc779aSAndroid Build Coastguard Worker
575*15dc779aSAndroid Build Coastguard Worker iusace_interpolation_lsp_params(&isp_curr_q[k * ORDER], &isp_curr_q[(k + 1) * ORDER],
576*15dc779aSAndroid Build Coastguard Worker lp_filter_coeff_q, st->num_subfrm);
577*15dc779aSAndroid Build Coastguard Worker
578*15dc779aSAndroid Build Coastguard Worker memcpy(lpd_state[k + 1], lpd_state[k], sizeof(*lpd_state[0]));
579*15dc779aSAndroid Build Coastguard Worker
580*15dc779aSAndroid Build Coastguard Worker iusace_acelp_encode(
581*15dc779aSAndroid Build Coastguard Worker &lp_filter_coeff[k * (num_sbfrm_per_supfrm / 4) * (ORDER + 1)], lp_filter_coeff_q,
582*15dc779aSAndroid Build Coastguard Worker &speech[k * st->len_subfrm], &pstr_scratch->p_wsig_buf[k * st->len_subfrm],
583*15dc779aSAndroid Build Coastguard Worker &pstr_scratch->p_synth_buf[k * st->len_subfrm],
584*15dc779aSAndroid Build Coastguard Worker &pstr_scratch->p_wsyn_buf[k * st->len_subfrm], *codec_mode, lpd_state[k + 1],
585*15dc779aSAndroid Build Coastguard Worker st->len_subfrm, norm_corr[k * 2], norm_corr[(k * 2) + 1], ol_pitch_lag[k * 2],
586*15dc779aSAndroid Build Coastguard Worker ol_pitch_lag[(k * 2) + 1], pit_adj, p_params, pstr_scratch);
587*15dc779aSAndroid Build Coastguard Worker
588*15dc779aSAndroid Build Coastguard Worker mem_wsyn = lpd_state[k]->mem_wsyn;
589*15dc779aSAndroid Build Coastguard Worker
590*15dc779aSAndroid Build Coastguard Worker iusace_find_weighted_speech(&lp_filter_coeff[k * (num_sbfrm_per_supfrm / 4) *
591*15dc779aSAndroid Build Coastguard Worker (ORDER + 1)], &pstr_scratch->p_synth_buf[k * LEN_FRAME],
592*15dc779aSAndroid Build Coastguard Worker pstr_scratch->p_temp_wsyn_buf, &mem_wsyn, LEN_FRAME);
593*15dc779aSAndroid Build Coastguard Worker
594*15dc779aSAndroid Build Coastguard Worker lpd_state[k + 1]->mem_wsyn = mem_wsyn;
595*15dc779aSAndroid Build Coastguard Worker mode[k] = 0;
596*15dc779aSAndroid Build Coastguard Worker num_tcx_param[k] = 0;
597*15dc779aSAndroid Build Coastguard Worker }
598*15dc779aSAndroid Build Coastguard Worker }
599*15dc779aSAndroid Build Coastguard Worker memcpy(&st->lpd_state, lpd_state[4], sizeof(*lpd_state[4]));
600*15dc779aSAndroid Build Coastguard Worker memcpy(st->weighted_sig, pstr_scratch->p_wsig_buf + (st->len_frame), 128 * sizeof(FLOAT32));
601*15dc779aSAndroid Build Coastguard Worker memcpy(st->prev_wsp, wsp_prev_buf, (len * sizeof(FLOAT32)));
602*15dc779aSAndroid Build Coastguard Worker return;
603*15dc779aSAndroid Build Coastguard Worker }
604*15dc779aSAndroid Build Coastguard Worker for (i1 = 0; i1 < 2; i1++) {
605*15dc779aSAndroid Build Coastguard Worker ssnr_512 = 0;
606*15dc779aSAndroid Build Coastguard Worker for (i2 = 0; i2 < 2; i2++) {
607*15dc779aSAndroid Build Coastguard Worker k = (i1 * 2) + i2;
608*15dc779aSAndroid Build Coastguard Worker p_params = acelp_tcx_params + (k * MAX_NUM_TCX_PRM_PER_DIV);
609*15dc779aSAndroid Build Coastguard Worker
610*15dc779aSAndroid Build Coastguard Worker iusace_interpolation_lsp_params(&isp_curr_q[k * ORDER], &isp_curr_q[(k + 1) * ORDER],
611*15dc779aSAndroid Build Coastguard Worker lp_filter_coeff_q, st->num_subfrm);
612*15dc779aSAndroid Build Coastguard Worker
613*15dc779aSAndroid Build Coastguard Worker memcpy(lpd_state[k + 1], lpd_state[k], sizeof(*lpd_state[0]));
614*15dc779aSAndroid Build Coastguard Worker
615*15dc779aSAndroid Build Coastguard Worker iusace_acelp_encode(
616*15dc779aSAndroid Build Coastguard Worker &lp_filter_coeff[k * (num_sbfrm_per_supfrm / 4) * (ORDER + 1)], lp_filter_coeff_q,
617*15dc779aSAndroid Build Coastguard Worker &speech[k * st->len_subfrm], &pstr_scratch->p_wsig_buf[k * st->len_subfrm],
618*15dc779aSAndroid Build Coastguard Worker &pstr_scratch->p_synth_buf[k * st->len_subfrm],
619*15dc779aSAndroid Build Coastguard Worker &pstr_scratch->p_wsyn_buf[k * st->len_subfrm], *codec_mode, lpd_state[k + 1],
620*15dc779aSAndroid Build Coastguard Worker st->len_subfrm, norm_corr[k * 2], norm_corr[(k * 2) + 1], ol_pitch_lag[k * 2],
621*15dc779aSAndroid Build Coastguard Worker ol_pitch_lag[(k * 2) + 1], pit_adj, p_params, pstr_scratch);
622*15dc779aSAndroid Build Coastguard Worker
623*15dc779aSAndroid Build Coastguard Worker mem_wsyn = lpd_state[k]->mem_wsyn;
624*15dc779aSAndroid Build Coastguard Worker
625*15dc779aSAndroid Build Coastguard Worker iusace_find_weighted_speech(&lp_filter_coeff[k * (num_sbfrm_per_supfrm / 4) * (ORDER + 1)],
626*15dc779aSAndroid Build Coastguard Worker &pstr_scratch->p_synth_buf[k * LEN_FRAME],
627*15dc779aSAndroid Build Coastguard Worker pstr_scratch->p_temp_wsyn_buf, &mem_wsyn, LEN_FRAME);
628*15dc779aSAndroid Build Coastguard Worker
629*15dc779aSAndroid Build Coastguard Worker lpd_state[k + 1]->mem_wsyn = mem_wsyn;
630*15dc779aSAndroid Build Coastguard Worker
631*15dc779aSAndroid Build Coastguard Worker ssnr_256 = iusace_cal_segsnr(&pstr_scratch->p_wsig_buf[k * LEN_FRAME],
632*15dc779aSAndroid Build Coastguard Worker pstr_scratch->p_temp_wsyn_buf, LEN_FRAME, LEN_SUBFR);
633*15dc779aSAndroid Build Coastguard Worker
634*15dc779aSAndroid Build Coastguard Worker mode[k] = 0;
635*15dc779aSAndroid Build Coastguard Worker num_tcx_param[k] = 0;
636*15dc779aSAndroid Build Coastguard Worker
637*15dc779aSAndroid Build Coastguard Worker iusace_lpc_coef_gen(&isp_curr_q[k * ORDER], &isp_curr_q[(k + 1) * ORDER], lp_filter_coeff_q,
638*15dc779aSAndroid Build Coastguard Worker st->num_subfrm, ORDER);
639*15dc779aSAndroid Build Coastguard Worker
640*15dc779aSAndroid Build Coastguard Worker memcpy(lpd_state_temp, lpd_state[k], sizeof(*lpd_state[0]));
641*15dc779aSAndroid Build Coastguard Worker
642*15dc779aSAndroid Build Coastguard Worker iusace_tcx_fac_encode(usac_data,
643*15dc779aSAndroid Build Coastguard Worker &lp_filter_coeff[k * (num_sbfrm_per_supfrm / 4) * (ORDER + 1)],
644*15dc779aSAndroid Build Coastguard Worker lp_filter_coeff_q, &speech[k * st->len_subfrm], st->len_subfrm,
645*15dc779aSAndroid Build Coastguard Worker num_bits_tcx, lpd_state_temp, prm_tcx, &num_params, ch_idx, k);
646*15dc779aSAndroid Build Coastguard Worker
647*15dc779aSAndroid Build Coastguard Worker mem_wsyn = lpd_state[k]->mem_wsyn;
648*15dc779aSAndroid Build Coastguard Worker
649*15dc779aSAndroid Build Coastguard Worker iusace_find_weighted_speech(&lp_filter_coeff[k * (num_sbfrm_per_supfrm / 4) * (ORDER + 1)],
650*15dc779aSAndroid Build Coastguard Worker pstr_scratch->p_synth_tcx_buf, pstr_scratch->p_temp_wsyn_buf,
651*15dc779aSAndroid Build Coastguard Worker &mem_wsyn, LEN_FRAME);
652*15dc779aSAndroid Build Coastguard Worker
653*15dc779aSAndroid Build Coastguard Worker lpd_state_temp->mem_wsyn = mem_wsyn;
654*15dc779aSAndroid Build Coastguard Worker
655*15dc779aSAndroid Build Coastguard Worker tmp_ssnr = iusace_cal_segsnr(&pstr_scratch->p_wsig_buf[k * LEN_FRAME],
656*15dc779aSAndroid Build Coastguard Worker pstr_scratch->p_temp_wsyn_buf, LEN_FRAME, LEN_SUBFR);
657*15dc779aSAndroid Build Coastguard Worker
658*15dc779aSAndroid Build Coastguard Worker if (tmp_ssnr > ssnr_256) {
659*15dc779aSAndroid Build Coastguard Worker ssnr_256 = tmp_ssnr;
660*15dc779aSAndroid Build Coastguard Worker mode[k] = 1;
661*15dc779aSAndroid Build Coastguard Worker num_tcx_param[k] = num_params;
662*15dc779aSAndroid Build Coastguard Worker memcpy(lpd_state[k + 1], lpd_state_temp, sizeof(*lpd_state[0]));
663*15dc779aSAndroid Build Coastguard Worker
664*15dc779aSAndroid Build Coastguard Worker memcpy(&pstr_scratch->p_synth_buf[(k * st->len_subfrm) - 128],
665*15dc779aSAndroid Build Coastguard Worker pstr_scratch->p_synth_tcx_buf - 128, (st->len_subfrm + 128) * sizeof(FLOAT32));
666*15dc779aSAndroid Build Coastguard Worker
667*15dc779aSAndroid Build Coastguard Worker memcpy(&pstr_scratch->p_wsyn_buf[(k * st->len_subfrm) - 128],
668*15dc779aSAndroid Build Coastguard Worker pstr_scratch->p_wsyn_tcx_buf - 128, (st->len_subfrm + 128) * sizeof(FLOAT32));
669*15dc779aSAndroid Build Coastguard Worker
670*15dc779aSAndroid Build Coastguard Worker memcpy(p_params, prm_tcx, NUM_TCX20_PRM * sizeof(WORD32));
671*15dc779aSAndroid Build Coastguard Worker }
672*15dc779aSAndroid Build Coastguard Worker ssnr_512 += 0.50f * ssnr_256;
673*15dc779aSAndroid Build Coastguard Worker }
674*15dc779aSAndroid Build Coastguard Worker
675*15dc779aSAndroid Build Coastguard Worker k = i1 * 2;
676*15dc779aSAndroid Build Coastguard Worker
677*15dc779aSAndroid Build Coastguard Worker p_params = acelp_tcx_params + (k * MAX_NUM_TCX_PRM_PER_DIV);
678*15dc779aSAndroid Build Coastguard Worker
679*15dc779aSAndroid Build Coastguard Worker iusace_lpc_coef_gen(&isp_curr_q[2 * i1 * ORDER], &isp_curr_q[(2 * i1 + 2) * ORDER],
680*15dc779aSAndroid Build Coastguard Worker lp_filter_coeff_q, (num_sbfrm_per_supfrm / 2), ORDER);
681*15dc779aSAndroid Build Coastguard Worker
682*15dc779aSAndroid Build Coastguard Worker memcpy(lpd_state_temp, lpd_state[2 * i1], sizeof(*lpd_state[0]));
683*15dc779aSAndroid Build Coastguard Worker iusace_tcx_fac_encode(usac_data,
684*15dc779aSAndroid Build Coastguard Worker &lp_filter_coeff[2 * i1 * (num_sbfrm_per_supfrm / 4) * (ORDER + 1)],
685*15dc779aSAndroid Build Coastguard Worker lp_filter_coeff_q, &speech[2 * i1 * st->len_subfrm], 2 * st->len_subfrm,
686*15dc779aSAndroid Build Coastguard Worker 2 * num_bits_tcx, lpd_state_temp, prm_tcx, &num_params, ch_idx, 2 * i1);
687*15dc779aSAndroid Build Coastguard Worker
688*15dc779aSAndroid Build Coastguard Worker mem_wsyn = lpd_state[2 * i1]->mem_wsyn;
689*15dc779aSAndroid Build Coastguard Worker
690*15dc779aSAndroid Build Coastguard Worker iusace_find_weighted_speech(
691*15dc779aSAndroid Build Coastguard Worker &lp_filter_coeff[2 * i1 * (num_sbfrm_per_supfrm / 4) * (ORDER + 1)],
692*15dc779aSAndroid Build Coastguard Worker pstr_scratch->p_synth_tcx_buf, pstr_scratch->p_temp_wsyn_buf, &mem_wsyn, LEN_FRAME * 2);
693*15dc779aSAndroid Build Coastguard Worker
694*15dc779aSAndroid Build Coastguard Worker lpd_state_temp->mem_wsyn = mem_wsyn;
695*15dc779aSAndroid Build Coastguard Worker
696*15dc779aSAndroid Build Coastguard Worker tmp_ssnr = iusace_cal_segsnr(&pstr_scratch->p_wsig_buf[2 * i1 * LEN_FRAME],
697*15dc779aSAndroid Build Coastguard Worker pstr_scratch->p_temp_wsyn_buf, LEN_FRAME * 2, LEN_SUBFR);
698*15dc779aSAndroid Build Coastguard Worker
699*15dc779aSAndroid Build Coastguard Worker if (tmp_ssnr > ssnr_512) {
700*15dc779aSAndroid Build Coastguard Worker ssnr_512 = tmp_ssnr;
701*15dc779aSAndroid Build Coastguard Worker for (i = 0; i < 2; i++) {
702*15dc779aSAndroid Build Coastguard Worker mode[k + i] = 2;
703*15dc779aSAndroid Build Coastguard Worker num_tcx_param[k + i] = num_params;
704*15dc779aSAndroid Build Coastguard Worker }
705*15dc779aSAndroid Build Coastguard Worker memcpy(lpd_state[k + 2], lpd_state_temp, sizeof(*lpd_state[0]));
706*15dc779aSAndroid Build Coastguard Worker
707*15dc779aSAndroid Build Coastguard Worker memcpy(&pstr_scratch->p_synth_buf[(2 * i1 * st->len_subfrm) - 128],
708*15dc779aSAndroid Build Coastguard Worker pstr_scratch->p_synth_tcx_buf - 128, ((2 * st->len_subfrm) + 128) * sizeof(FLOAT32));
709*15dc779aSAndroid Build Coastguard Worker memcpy(&pstr_scratch->p_wsyn_buf[(2 * i1 * st->len_subfrm) - 128],
710*15dc779aSAndroid Build Coastguard Worker pstr_scratch->p_wsyn_tcx_buf - 128, ((2 * st->len_subfrm) + 128) * sizeof(FLOAT32));
711*15dc779aSAndroid Build Coastguard Worker memcpy(p_params, prm_tcx, NUM_TCX40_PRM * sizeof(WORD32));
712*15dc779aSAndroid Build Coastguard Worker }
713*15dc779aSAndroid Build Coastguard Worker ssnr_1024 += 0.50f * ssnr_512;
714*15dc779aSAndroid Build Coastguard Worker }
715*15dc779aSAndroid Build Coastguard Worker
716*15dc779aSAndroid Build Coastguard Worker k = 0;
717*15dc779aSAndroid Build Coastguard Worker
718*15dc779aSAndroid Build Coastguard Worker p_params = acelp_tcx_params + (k * MAX_NUM_TCX_PRM_PER_DIV);
719*15dc779aSAndroid Build Coastguard Worker
720*15dc779aSAndroid Build Coastguard Worker iusace_lpc_coef_gen(&isp_curr_q[k * ORDER], &isp_curr_q[(k + 4) * ORDER], lp_filter_coeff_q,
721*15dc779aSAndroid Build Coastguard Worker num_sbfrm_per_supfrm, ORDER);
722*15dc779aSAndroid Build Coastguard Worker
723*15dc779aSAndroid Build Coastguard Worker memcpy(lpd_state_temp, lpd_state[k], sizeof(*lpd_state[0]));
724*15dc779aSAndroid Build Coastguard Worker
725*15dc779aSAndroid Build Coastguard Worker iusace_tcx_fac_encode(usac_data, &lp_filter_coeff[k * (num_sbfrm_per_supfrm / 4) * (ORDER + 1)],
726*15dc779aSAndroid Build Coastguard Worker lp_filter_coeff_q, &speech[k * st->len_subfrm], 4 * st->len_subfrm,
727*15dc779aSAndroid Build Coastguard Worker 4 * num_bits_tcx, lpd_state_temp, prm_tcx, &num_params, ch_idx, k);
728*15dc779aSAndroid Build Coastguard Worker
729*15dc779aSAndroid Build Coastguard Worker mem_wsyn = lpd_state[k]->mem_wsyn;
730*15dc779aSAndroid Build Coastguard Worker
731*15dc779aSAndroid Build Coastguard Worker iusace_find_weighted_speech(&lp_filter_coeff[k * (num_sbfrm_per_supfrm / 4) * (ORDER + 1)],
732*15dc779aSAndroid Build Coastguard Worker pstr_scratch->p_synth_tcx_buf, pstr_scratch->p_temp_wsyn_buf,
733*15dc779aSAndroid Build Coastguard Worker &mem_wsyn, LEN_FRAME * 4);
734*15dc779aSAndroid Build Coastguard Worker
735*15dc779aSAndroid Build Coastguard Worker lpd_state_temp->mem_wsyn = mem_wsyn;
736*15dc779aSAndroid Build Coastguard Worker
737*15dc779aSAndroid Build Coastguard Worker tmp_ssnr = iusace_cal_segsnr(&pstr_scratch->p_wsig_buf[k * LEN_FRAME],
738*15dc779aSAndroid Build Coastguard Worker pstr_scratch->p_temp_wsyn_buf, LEN_FRAME * 4, LEN_SUBFR);
739*15dc779aSAndroid Build Coastguard Worker
740*15dc779aSAndroid Build Coastguard Worker if (tmp_ssnr > ssnr_1024) {
741*15dc779aSAndroid Build Coastguard Worker for (i = 0; i < 4; i++) {
742*15dc779aSAndroid Build Coastguard Worker mode[k + i] = 3;
743*15dc779aSAndroid Build Coastguard Worker num_tcx_param[k + i] = num_params;
744*15dc779aSAndroid Build Coastguard Worker }
745*15dc779aSAndroid Build Coastguard Worker memcpy(lpd_state[k + 4], lpd_state_temp, sizeof(*lpd_state[0]));
746*15dc779aSAndroid Build Coastguard Worker
747*15dc779aSAndroid Build Coastguard Worker memcpy(&pstr_scratch->p_synth_buf[(k * st->len_subfrm) - 128],
748*15dc779aSAndroid Build Coastguard Worker pstr_scratch->p_synth_tcx_buf - 128, ((4 * st->len_subfrm) + 128) * sizeof(FLOAT32));
749*15dc779aSAndroid Build Coastguard Worker memcpy(&pstr_scratch->p_wsyn_buf[(k * st->len_subfrm) - 128],
750*15dc779aSAndroid Build Coastguard Worker pstr_scratch->p_wsyn_tcx_buf - 128, ((4 * st->len_subfrm) + 128) * sizeof(FLOAT32));
751*15dc779aSAndroid Build Coastguard Worker memcpy(p_params, prm_tcx, NUM_TCX80_PRM * sizeof(WORD32));
752*15dc779aSAndroid Build Coastguard Worker }
753*15dc779aSAndroid Build Coastguard Worker memcpy(&st->lpd_state, lpd_state[4], sizeof(*lpd_state[4]));
754*15dc779aSAndroid Build Coastguard Worker
755*15dc779aSAndroid Build Coastguard Worker memcpy(st->weighted_sig, pstr_scratch->p_wsig_buf + (st->len_frame), 128 * sizeof(FLOAT32));
756*15dc779aSAndroid Build Coastguard Worker memcpy(st->prev_wsp, wsp_prev_buf, (len * sizeof(FLOAT32)));
757*15dc779aSAndroid Build Coastguard Worker
758*15dc779aSAndroid Build Coastguard Worker return;
759*15dc779aSAndroid Build Coastguard Worker }
760*15dc779aSAndroid Build Coastguard Worker
iusace_lpd_frm_enc(ia_usac_data_struct * usac_data,WORD32 * mod_out,WORD32 const usac_independency_flg,WORD32 len_frame,WORD32 i_ch,ia_bit_buf_struct * pstr_it_bit_buff)761*15dc779aSAndroid Build Coastguard Worker IA_ERRORCODE iusace_lpd_frm_enc(ia_usac_data_struct *usac_data, WORD32 *mod_out,
762*15dc779aSAndroid Build Coastguard Worker WORD32 const usac_independency_flg, WORD32 len_frame, WORD32 i_ch,
763*15dc779aSAndroid Build Coastguard Worker ia_bit_buf_struct *pstr_it_bit_buff) {
764*15dc779aSAndroid Build Coastguard Worker WORD32 i;
765*15dc779aSAndroid Build Coastguard Worker WORD32 len_next_high_rate = (LEN_NEXT_HIGH_RATE * len_frame) / LEN_SUPERFRAME;
766*15dc779aSAndroid Build Coastguard Worker WORD32 len_lpc0 = (LEN_LPC0 * len_frame) / LEN_SUPERFRAME;
767*15dc779aSAndroid Build Coastguard Worker FLOAT32 *input_data = &usac_data->td_in_buf[i_ch][len_next_high_rate];
768*15dc779aSAndroid Build Coastguard Worker ia_usac_td_encoder_struct *td_encoder = usac_data->td_encoder[i_ch];
769*15dc779aSAndroid Build Coastguard Worker WORD32 fscale = usac_data->td_encoder[i_ch]->fscale;
770*15dc779aSAndroid Build Coastguard Worker WORD32 first_lpd_flag = (usac_data->core_mode_prev[i_ch] == CORE_MODE_FD);
771*15dc779aSAndroid Build Coastguard Worker FLOAT32 *speech_buf = usac_data->speech_buf;
772*15dc779aSAndroid Build Coastguard Worker FLOAT32 *ptr_scratch_buf = usac_data->str_scratch.p_lpd_frm_enc_scratch;
773*15dc779aSAndroid Build Coastguard Worker FLOAT32 *speech, *new_speech;
774*15dc779aSAndroid Build Coastguard Worker WORD32 mode_buf[1 + NUM_FRAMES] = {0}, *mode;
775*15dc779aSAndroid Build Coastguard Worker WORD32 num_tcx_params[NUM_FRAMES] = {0};
776*15dc779aSAndroid Build Coastguard Worker WORD32 len_subfrm;
777*15dc779aSAndroid Build Coastguard Worker
778*15dc779aSAndroid Build Coastguard Worker len_subfrm = td_encoder->len_subfrm;
779*15dc779aSAndroid Build Coastguard Worker
780*15dc779aSAndroid Build Coastguard Worker if (usac_data->core_mode_prev[i_ch] == CORE_MODE_FD) {
781*15dc779aSAndroid Build Coastguard Worker iusace_reset_td_enc(usac_data->td_encoder[i_ch]);
782*15dc779aSAndroid Build Coastguard Worker
783*15dc779aSAndroid Build Coastguard Worker FLOAT32 *in_data = usac_data->td_in_prev_buf[i_ch];
784*15dc779aSAndroid Build Coastguard Worker FLOAT32 *ptr_speech = usac_data->speech_buf;
785*15dc779aSAndroid Build Coastguard Worker WORD32 length = len_next_high_rate + len_lpc0;
786*15dc779aSAndroid Build Coastguard Worker ia_usac_td_encoder_struct *st = usac_data->td_encoder[i_ch];
787*15dc779aSAndroid Build Coastguard Worker memcpy(ptr_speech, in_data, length * sizeof(FLOAT32));
788*15dc779aSAndroid Build Coastguard Worker
789*15dc779aSAndroid Build Coastguard Worker iusace_highpass_50hz_12k8(ptr_speech, length, st->mem_sig_in, st->fscale);
790*15dc779aSAndroid Build Coastguard Worker
791*15dc779aSAndroid Build Coastguard Worker iusace_apply_preemph(ptr_speech, PREEMPH_FILT_FAC, length, &(st->mem_preemph));
792*15dc779aSAndroid Build Coastguard Worker memcpy(st->old_speech_pe + ORDER, ptr_speech, length * sizeof(FLOAT32));
793*15dc779aSAndroid Build Coastguard Worker }
794*15dc779aSAndroid Build Coastguard Worker
795*15dc779aSAndroid Build Coastguard Worker if (first_lpd_flag) {
796*15dc779aSAndroid Build Coastguard Worker td_encoder->prev_mode = -1;
797*15dc779aSAndroid Build Coastguard Worker }
798*15dc779aSAndroid Build Coastguard Worker
799*15dc779aSAndroid Build Coastguard Worker mode = mode_buf + 1;
800*15dc779aSAndroid Build Coastguard Worker mode[-1] = td_encoder->prev_mode;
801*15dc779aSAndroid Build Coastguard Worker fscale = (fscale * len_subfrm) / LEN_FRAME;
802*15dc779aSAndroid Build Coastguard Worker
803*15dc779aSAndroid Build Coastguard Worker new_speech = speech_buf + ORDER + (LEN_NEXT_HIGH_RATE * len_subfrm) / LEN_FRAME;
804*15dc779aSAndroid Build Coastguard Worker speech = speech_buf + ORDER;
805*15dc779aSAndroid Build Coastguard Worker if (first_lpd_flag) {
806*15dc779aSAndroid Build Coastguard Worker new_speech += (LEN_LPC0 * len_subfrm) / LEN_FRAME;
807*15dc779aSAndroid Build Coastguard Worker speech += (LEN_LPC0 * len_subfrm) / LEN_FRAME;
808*15dc779aSAndroid Build Coastguard Worker }
809*15dc779aSAndroid Build Coastguard Worker memcpy(new_speech, input_data, td_encoder->len_frame * sizeof(FLOAT32));
810*15dc779aSAndroid Build Coastguard Worker
811*15dc779aSAndroid Build Coastguard Worker iusace_highpass_50hz_12k8(new_speech, td_encoder->len_frame, td_encoder->mem_sig_in, fscale);
812*15dc779aSAndroid Build Coastguard Worker iusace_apply_preemph(new_speech, PREEMPH_FILT_FAC, td_encoder->len_frame,
813*15dc779aSAndroid Build Coastguard Worker &(td_encoder->mem_preemph));
814*15dc779aSAndroid Build Coastguard Worker
815*15dc779aSAndroid Build Coastguard Worker if (first_lpd_flag) {
816*15dc779aSAndroid Build Coastguard Worker memcpy(speech_buf, td_encoder->old_speech_pe,
817*15dc779aSAndroid Build Coastguard Worker ((ORDER + (((LEN_NEXT_HIGH_RATE + LEN_LPC0) * len_subfrm) / LEN_FRAME))) *
818*15dc779aSAndroid Build Coastguard Worker sizeof(FLOAT32));
819*15dc779aSAndroid Build Coastguard Worker for (i = 0; i < (len_subfrm + 1); i++) {
820*15dc779aSAndroid Build Coastguard Worker ptr_scratch_buf[i] = speech[-len_subfrm - 1 + i];
821*15dc779aSAndroid Build Coastguard Worker }
822*15dc779aSAndroid Build Coastguard Worker iusace_apply_deemph(ptr_scratch_buf, PREEMPH_FILT_FAC, len_subfrm + 1, &ptr_scratch_buf[0]);
823*15dc779aSAndroid Build Coastguard Worker memcpy(td_encoder->lpd_state.tcx_mem, &ptr_scratch_buf[len_subfrm - 128 + 1],
824*15dc779aSAndroid Build Coastguard Worker 128 * sizeof(FLOAT32));
825*15dc779aSAndroid Build Coastguard Worker } else {
826*15dc779aSAndroid Build Coastguard Worker memcpy(speech_buf, td_encoder->old_speech_pe,
827*15dc779aSAndroid Build Coastguard Worker ((ORDER + ((LEN_NEXT_HIGH_RATE * len_subfrm) / LEN_FRAME))) * sizeof(FLOAT32));
828*15dc779aSAndroid Build Coastguard Worker }
829*15dc779aSAndroid Build Coastguard Worker
830*15dc779aSAndroid Build Coastguard Worker iusace_core_lpd_encode(usac_data, speech, mode, num_tcx_params, i_ch);
831*15dc779aSAndroid Build Coastguard Worker
832*15dc779aSAndroid Build Coastguard Worker if (first_lpd_flag) {
833*15dc779aSAndroid Build Coastguard Worker memcpy(td_encoder->old_speech_pe,
834*15dc779aSAndroid Build Coastguard Worker &speech_buf[(td_encoder->len_frame) + (LEN_LPC0 * len_subfrm) / LEN_FRAME],
835*15dc779aSAndroid Build Coastguard Worker (ORDER + ((LEN_NEXT_HIGH_RATE * len_subfrm) / LEN_FRAME)) * sizeof(FLOAT32));
836*15dc779aSAndroid Build Coastguard Worker } else {
837*15dc779aSAndroid Build Coastguard Worker memcpy(td_encoder->old_speech_pe, &speech_buf[(td_encoder->len_frame)],
838*15dc779aSAndroid Build Coastguard Worker (ORDER + ((LEN_NEXT_HIGH_RATE * len_subfrm) / LEN_FRAME)) * sizeof(FLOAT32));
839*15dc779aSAndroid Build Coastguard Worker }
840*15dc779aSAndroid Build Coastguard Worker
841*15dc779aSAndroid Build Coastguard Worker iusace_encode_fac_params(mode, num_tcx_params, usac_data, usac_independency_flg,
842*15dc779aSAndroid Build Coastguard Worker pstr_it_bit_buff, i_ch);
843*15dc779aSAndroid Build Coastguard Worker
844*15dc779aSAndroid Build Coastguard Worker td_encoder->prev_mode = (WORD16)mode[3];
845*15dc779aSAndroid Build Coastguard Worker
846*15dc779aSAndroid Build Coastguard Worker memcpy(mod_out, mode, 4 * sizeof(WORD32));
847*15dc779aSAndroid Build Coastguard Worker return IA_NO_ERROR;
848*15dc779aSAndroid Build Coastguard Worker }
849