Coverage Report

Created: 2025-08-24 07:17

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