Coverage Report

Created: 2026-08-31 07:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libxaac/encoder/iusace_tcx_enc.c
Line
Count
Source
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_tns_usac.h"
41
#include "iusace_config.h"
42
#include "iusace_fft.h"
43
#include "iusace_tcx_mdct.h"
44
#include "iusace_arith_enc.h"
45
#include "iusace_fd_qc_util.h"
46
#include "iusace_fd_quant.h"
47
#include "iusace_block_switch_const.h"
48
#include "iusace_block_switch_struct_def.h"
49
#include "iusace_ms.h"
50
#include "iusace_signal_classifier.h"
51
#include "ixheaace_sbr_header.h"
52
#include "ixheaace_config.h"
53
#include "ixheaace_asc_write.h"
54
#include "iusace_main.h"
55
#include "iusace_lpd_rom.h"
56
#include "iusace_lpd.h"
57
#include "iusace_avq_enc.h"
58
#include "ixheaace_common_utils.h"
59
60
const UWORD32 iusace_pow10_gain_div28[128] = {
61
    1024,     1112,     1207,     1311,     1423,     1545,     1677,     1821,     1977,
62
    2146,     2330,     2530,     2747,     2983,     3238,     3516,     3817,     4144,
63
    4499,     4885,     5304,     5758,     6252,     6788,     7370,     8001,     8687,
64
    9432,     10240,    11118,    12071,    13105,    14228,    15448,    16772,    18210,
65
    19770,    21465,    23305,    25302,    27471,    29825,    32382,    35157,    38171,
66
    41442,    44994,    48851,    53038,    57584,    62519,    67878,    73696,    80012,
67
    86870,    94316,    102400,   111177,   120706,   131052,   142284,   154480,   167720,
68
    182096,   197703,   214649,   233047,   253021,   274708,   298254,   323817,   351572,
69
    381706,   414422,   449943,   488508,   530378,   575838,   625193,   678779,   736958,
70
    800124,   868703,   943161,   1024000,  1111768,  1207059,  1310517,  1422843,  1544797,
71
    1677203,  1820958,  1977034,  2146488,  2330466,  2530213,  2747080,  2982536,  3238172,
72
    3515720,  3817056,  4144220,  4499426,  4885077,  5303782,  5758375,  6251932,  6787792,
73
    7369581,  8001236,  8687031,  9431606,  10240000, 11117682, 12070591, 13105175, 14228434,
74
    15447969, 16772032, 18209581, 19770345, 21464883, 23304662, 25302131, 27470805, 29825358,
75
    32381723, 35157197};
76
77
static const FLOAT64 iusace_lpc_pre_twid_cos[ORDER + 1] = {1.0,
78
                                                           0.99969881867944277,
79
                                                           0.99879545613814691,
80
                                                           0.99729045666498317,
81
                                                           0.99518472640441780,
82
                                                           0.99247953486470630,
83
                                                           0.98917650991010153,
84
                                                           0.98527764316379052,
85
                                                           0.98078527933727178,
86
                                                           0.97570212991605565,
87
                                                           0.97003125425052761,
88
                                                           0.96377606826277584,
89
                                                           0.95694033551585822,
90
                                                           0.94952817722361749,
91
                                                           0.94154406823678738,
92
                                                           0.93299279849944938,
93
                                                           0.92387952832938047};
94
95
static const FLOAT64 iusace_lpc_pre_twid_sin[ORDER + 1] = {0,
96
                                                           0.024541229205697054,
97
                                                           0.049067675691753569,
98
                                                           0.073564563785488826,
99
                                                           0.098017143048367339,
100
                                                           0.12241067304257494,
101
                                                           0.14673047482398086,
102
                                                           0.17096188429473480,
103
                                                           0.19509032737506427,
104
                                                           0.21910124070226658,
105
                                                           0.24298017568754085,
106
                                                           0.26671274855909161,
107
                                                           0.29028467796767482,
108
                                                           0.31368175059826858,
109
                                                           0.33688984485751389,
110
                                                           0.35989503740419343,
111
                                                           0.38268344246110436};
112
113
static VOID iusace_lpc_mdct(FLOAT32 *ptr_lpc_coeffs, FLOAT32 *mdct_gains, WORD32 length,
114
1.76M
                            iusace_scratch_mem *pstr_scratch) {
115
1.76M
  FLOAT32 *in_out = pstr_scratch->p_in_out_tcx;
116
1.76M
  WORD32 i;
117
118
31.8M
  for (i = 0; i < ORDER + 1; i++) {
119
30.0M
    in_out[2 * i] = (FLOAT32)(ptr_lpc_coeffs[i] * iusace_lpc_pre_twid_cos[i]);
120
30.0M
    in_out[2 * i + 1] = (FLOAT32)(-ptr_lpc_coeffs[i] * iusace_lpc_pre_twid_sin[i]);
121
30.0M
  }
122
180M
  for (; i < length; i++) {
123
178M
    in_out[2 * i] = 0.f;
124
178M
    in_out[2 * i + 1] = 0.f;
125
178M
  }
126
127
1.76M
  iusace_complex_fft(in_out, length, pstr_scratch);
128
129
106M
  for (i = 0; i<length>> 1; i++) {
130
104M
    mdct_gains[i] = (FLOAT32)(
131
104M
        1.0f / sqrt(in_out[2 * i] * in_out[2 * i] + in_out[2 * i + 1] * in_out[2 * i + 1]));
132
104M
  }
133
134
1.76M
  return;
135
1.76M
}
136
137
0
UWORD32 iusace_rounded_sqrt(UWORD32 pos_num) {
138
0
  UWORD32 num = pos_num;
139
0
  UWORD32 value = 0;
140
0
  UWORD32 bit_set = 1 << 30;
141
142
0
  while (bit_set > num) {
143
0
    bit_set >>= 2;
144
0
  }
145
0
  while (bit_set) {
146
0
    if (num >= value + bit_set) {
147
0
      num -= value + bit_set;
148
0
      value += bit_set << 1;
149
0
    }
150
0
    value >>= 1;
151
0
    bit_set >>= 2;
152
0
  }
153
0
  num = value + 1;
154
0
  if (num * num - pos_num < pos_num - value * value) {
155
0
    return num;
156
0
  }
157
0
  return value;
158
0
}
159
160
static VOID iusace_noise_shaping(FLOAT32 *rr, WORD32 lg, WORD32 M, FLOAT32 *gain1,
161
883k
                                 FLOAT32 *gain2) {
162
883k
  WORD32 i, k;
163
883k
  FLOAT32 r, r_prev, g1, g2, a = 0, b = 0;
164
165
883k
  k = lg/M;
166
167
883k
  r_prev = 0;
168
359M
  for (i = 0; i < lg; i++) {
169
358M
    if ((i % k) == 0) {
170
52.2M
      g1 = gain1[i / k];
171
52.2M
      g2 = gain2[i / k];
172
52.2M
      a = 2.0f * g1 * g2 / (g1 + g2);
173
52.2M
      b = (g2 - g1) / (g1 + g2);
174
52.2M
    }
175
176
358M
    r = a * rr[i] + b * r_prev;
177
178
358M
    rr[i] = r;
179
358M
    r_prev = r;
180
358M
  }
181
182
883k
  return;
183
883k
}
184
185
883k
static VOID iusace_pre_shaping(FLOAT32 *rr, WORD32 lg, WORD32 M, FLOAT32 *gain1, FLOAT32 *gain2) {
186
883k
  WORD32 i, k;
187
883k
  FLOAT32 r, r_prev, g1, g2, a = 0, b = 0;
188
189
883k
  k = lg / M;
190
191
883k
  r_prev = 0;
192
359M
  for (i = 0; i < lg; i++) {
193
358M
    if ((i % k) == 0) {
194
52.2M
      g1 = gain1[i / k];
195
52.2M
      g2 = gain2[i / k];
196
197
52.2M
      a = (g1 + g2) / (2.0f * g1 * g2);
198
52.2M
      b = (g1 - g2) / (2.0f * g1 * g2);
199
52.2M
    }
200
201
358M
    r = a * rr[i] + b * r_prev;
202
203
358M
    r_prev = rr[i];
204
358M
    rr[i] = r;
205
358M
  }
206
207
883k
  return;
208
883k
}
209
210
883k
static VOID iusace_adapt_lo_freq_emph(FLOAT32 *signal, WORD32 length) {
211
883k
  WORD32 i, j, i_max;
212
883k
  FLOAT32 max_energy, factor, temp;
213
214
883k
  i_max = length >> 2;
215
216
883k
  max_energy = 0.01f;
217
12.0M
  for (i = 0; i < i_max; i += 8) {
218
11.1M
    temp = 0.01f;
219
100M
    for (j = i; j < i + 8; j++) {
220
89.5M
      temp += signal[j] * signal[j];
221
89.5M
    }
222
11.1M
    if (temp > max_energy) {
223
1.14M
      max_energy = temp;
224
1.14M
    }
225
11.1M
  }
226
227
883k
  factor = 10.0f;
228
12.0M
  for (i = 0; i < i_max; i += 8) {
229
11.1M
    temp = 0.01f;
230
100M
    for (j = i; j < i + 8; j++) {
231
89.5M
      temp += signal[j] * signal[j];
232
89.5M
    }
233
11.1M
    temp = (FLOAT32)sqrt(sqrt(max_energy / temp));
234
11.1M
    if (temp < factor) {
235
1.16M
      factor = temp;
236
1.16M
    }
237
100M
    for (j = i; j < i + 8; j++) {
238
89.5M
      signal[j] *= factor;
239
89.5M
    }
240
11.1M
  }
241
883k
  return;
242
883k
}
243
244
883k
static VOID iusace_adapt_lo_freq_deemph(FLOAT32 *signal, WORD32 length, FLOAT32 *gains) {
245
883k
  WORD32 i, j, i_max;
246
883k
  FLOAT32 max_energy, factor, energy, rm;
247
248
883k
  i_max = length >> 2;
249
250
883k
  max_energy = 0.01f;
251
12.0M
  for (i = 0; i < i_max; i += 8) {
252
11.1M
    energy = 0.01f;
253
100M
    for (j = i; j < i + 8; j++) {
254
89.5M
      energy += signal[j] * signal[j];
255
89.5M
    }
256
11.1M
    if (energy > max_energy) {
257
1.05M
      max_energy = energy;
258
1.05M
    }
259
11.1M
  }
260
261
883k
  factor = 0.1f;
262
12.0M
  for (i = 0; i < i_max; i += 8) {
263
11.1M
    energy = 0.01f;
264
100M
    for (j = i; j < i + 8; j++) {
265
89.5M
      energy += signal[j] * signal[j];
266
89.5M
    }
267
268
11.1M
    rm = (FLOAT32)sqrt(energy / max_energy);
269
11.1M
    if (rm > factor) {
270
1.09M
      factor = rm;
271
1.09M
    }
272
100M
    for (j = i; j < i + 8; j++) {
273
89.5M
      signal[j] *= factor;
274
89.5M
    }
275
11.1M
    gains[i / 8] = factor;
276
11.1M
  }
277
278
883k
  return;
279
883k
}
280
281
VOID iusace_tcx_fac_encode(ia_usac_data_struct *usac_data, FLOAT32 *lpc_coeffs,
282
                           FLOAT32 *lpc_coeffs_quant, FLOAT32 *speech, WORD32 frame_len,
283
                           WORD32 num_bits_per_supfrm, ia_usac_lpd_state_struct *lpd_state,
284
883k
                           WORD32 *params, WORD32 *n_param, WORD32 ch_idx, WORD32 k_idx) {
285
883k
  ia_usac_td_encoder_struct *st = usac_data->td_encoder[ch_idx];
286
883k
  iusace_scratch_mem *pstr_scratch = &usac_data->str_scratch;
287
883k
  FLOAT32 *weighted_sig = &pstr_scratch->p_wsig_buf[k_idx * st->len_subfrm];
288
883k
  FLOAT32 *wsynth = pstr_scratch->p_wsyn_tcx_buf;
289
883k
  FLOAT32 *synth = pstr_scratch->p_synth_tcx_buf;
290
883k
  WORD32 i, k, n, mode, i_subfr, lg, lext, index, target_bits;
291
883k
  FLOAT32 tmp, gain, fac_ns, energy, gain_tcx, nsfill_en_thres;
292
883k
  FLOAT32 *ptr_lp_flt_coeffs, lp_flt_coeffs[ORDER + 1];
293
883k
  const FLOAT32 *sine_window_prev, *sine_window;
294
883k
  FLOAT32 mem_tcx_q;
295
883k
  FLOAT32 *xn;
296
883k
  FLOAT32 *xn1 = pstr_scratch->p_xn1_tcx;
297
883k
  FLOAT32 *xn_buf = pstr_scratch->p_xn_buf_tcx;
298
883k
  FLOAT32 *x = pstr_scratch->p_x_tcx;
299
883k
  FLOAT32 *x_tmp = pstr_scratch->p_x_tmp_tcx;
300
883k
  FLOAT32 *en = pstr_scratch->p_en_tcx;
301
883k
  FLOAT32 sq_gain;
302
883k
  FLOAT32 gain_prev, gain_next;
303
883k
  FLOAT32 *alfd_gains = pstr_scratch->p_alfd_gains_tcx;
304
883k
  FLOAT32 *sq_enc = pstr_scratch->p_sq_enc_tcx;
305
883k
  WORD32 *sq_quant = pstr_scratch->p_sq_quant_tcx;
306
883k
  FLOAT32 sq_err_energy;
307
883k
  WORD32 max_k;
308
883k
  FLOAT32 *gain1 = pstr_scratch->p_gain1_tcx;
309
883k
  FLOAT32 *gain2 = pstr_scratch->p_gain2_tcx;
310
883k
  FLOAT32 *facelp = pstr_scratch->p_facelp_tcx;
311
883k
  FLOAT32 *xn2 = pstr_scratch->p_xn2_tcx;
312
883k
  FLOAT32 *fac_window = pstr_scratch->p_fac_window_tcx;
313
883k
  FLOAT32 *x1 = pstr_scratch->p_x1_tcx;
314
883k
  FLOAT32 *x2 = pstr_scratch->p_x2_tcx;
315
883k
  WORD32 *y = pstr_scratch->p_y_tcx;
316
317
883k
  WORD32 TTT;
318
883k
  FLOAT32 corr = 0;
319
883k
  WORD32 len_subfrm = st->len_subfrm;
320
883k
  WORD32 fac_length = len_subfrm >> 1;
321
883k
  WORD32 fac_len_prev, fac_len;
322
323
883k
  if (frame_len == 4 * st->len_subfrm) {
324
126k
    if (st->last_was_short) {
325
0
      fac_len_prev = (st->len_frame) / 16;
326
126k
    } else {
327
126k
      fac_len_prev = st->len_subfrm / 2;
328
126k
    }
329
126k
    if (st->next_is_short) {
330
0
      fac_len = (st->len_frame) / 16;
331
126k
    } else {
332
126k
      fac_len = st->len_subfrm / 2;
333
126k
    }
334
757k
  } else if (frame_len == 2 * st->len_subfrm) {
335
252k
    if (k_idx == 0 && st->last_was_short) {
336
0
      fac_len_prev = (st->len_frame) / 16;
337
252k
    } else {
338
252k
      fac_len_prev = st->len_subfrm / 2;
339
252k
    }
340
252k
    if (k_idx == 2 && st->next_is_short) {
341
0
      fac_len = (st->len_frame) / 16;
342
252k
    } else {
343
252k
      fac_len = st->len_subfrm / 2;
344
252k
    }
345
504k
  } else {
346
504k
    if (k_idx == 0 && st->last_was_short) {
347
0
      fac_len_prev = (st->len_frame) / 16;
348
504k
    } else {
349
504k
      fac_len_prev = st->len_subfrm / 2;
350
504k
    }
351
504k
    if (k_idx == 3 && st->next_is_short) {
352
0
      fac_len = (st->len_frame) / 16;
353
504k
    } else {
354
504k
      fac_len = st->len_subfrm / 2;
355
504k
    }
356
504k
  }
357
358
883k
  memset(xn_buf, 0, (128 + frame_len + 128) * sizeof(FLOAT32));
359
360
883k
  mode = frame_len / len_subfrm;
361
362
883k
  if (mode > 2) {
363
126k
    mode = 3;
364
126k
  }
365
366
883k
  if (lpd_state->mode == 0) {
367
326k
    params += fac_len_prev;
368
326k
  }
369
883k
  switch (fac_len_prev) {
370
0
    case 64:
371
0
      sine_window_prev = iusace_sin_window_128;
372
0
      break;
373
883k
    default:
374
883k
      sine_window_prev = iusace_sin_window_256;
375
883k
      break;
376
883k
  }
377
883k
  switch (fac_len) {
378
0
    case 64:
379
0
      sine_window = iusace_sin_window_128;
380
0
      break;
381
883k
    default:
382
883k
      sine_window = iusace_sin_window_256;
383
883k
      break;
384
883k
  }
385
386
883k
  lg = frame_len;
387
883k
  lext = fac_length;
388
883k
  xn = xn_buf + fac_length;
389
390
883k
  *n_param = lg;
391
392
883k
  target_bits = num_bits_per_supfrm - 10;
393
394
105M
  for (i = 0; i < fac_length; i++) {
395
104M
    xn_buf[i] = lpd_state->tcx_mem[i + 128 - fac_length];
396
104M
  }
397
398
883k
  memcpy(xn, speech, (frame_len + fac_length) * sizeof(FLOAT32));
399
400
883k
  tmp = xn[-1];
401
402
883k
  iusace_apply_deemph(xn, TILT_FAC, frame_len, &tmp);
403
404
883k
  memcpy(lpd_state->tcx_mem, &xn[frame_len - 128], 128 * sizeof(FLOAT32));
405
406
883k
  memcpy(&xn[frame_len], &speech[frame_len], lext * sizeof(FLOAT32));
407
883k
  iusace_apply_deemph(&xn[frame_len], TILT_FAC, lext, &tmp);
408
409
119M
  for (i = 0; i < ORDER + fac_len_prev; i++) {
410
118M
    xn1[i] = xn_buf[fac_length - ORDER + i];
411
118M
  }
412
119M
  for (i = 0; i < ORDER + fac_len; i++) {
413
118M
    xn2[i] = xn_buf[frame_len - ORDER + i];
414
118M
  }
415
416
883k
  if (lpd_state->mode >= -1) {
417
883k
    for (i = 0; i < fac_length - fac_len_prev; i++) {
418
0
      xn_buf[i] = 0.0f;
419
0
    }
420
209M
    for (i = fac_length - fac_len_prev; i < (fac_length + fac_len_prev); i++) {
421
208M
      xn_buf[i] *= sine_window_prev[i - fac_length + fac_len_prev];
422
208M
    }
423
209M
    for (i = 0; i < (2 * fac_len); i++) {
424
208M
      xn_buf[frame_len + fac_length - fac_len + i] *= sine_window[(2 * fac_len) - 1 - i];
425
208M
    }
426
883k
    for (i = 0; i < fac_length - fac_len; i++) {
427
0
      xn_buf[frame_len + fac_length + fac_len + i] = 0.0f;
428
0
    }
429
883k
  }
430
431
883k
  iusace_tcx_mdct_main(xn_buf, x, (2 * fac_length), frame_len - (2 * fac_length),
432
883k
                       (2 * fac_length), pstr_scratch);
433
434
883k
  iusace_get_weighted_lpc(lpc_coeffs_quant + (ORDER + 1), lp_flt_coeffs);
435
883k
  iusace_lpc_mdct(lp_flt_coeffs, gain1, ((FDNS_RESOLUTION * len_subfrm) / LEN_FRAME) << 1,
436
883k
                  pstr_scratch);
437
438
883k
  iusace_get_weighted_lpc(lpc_coeffs_quant + (2 * (ORDER + 1)), lp_flt_coeffs);
439
883k
  iusace_lpc_mdct(lp_flt_coeffs, gain2, ((FDNS_RESOLUTION * len_subfrm) / LEN_FRAME) << 1,
440
883k
                  pstr_scratch);
441
442
883k
  iusace_pre_shaping(x, lg, ((FDNS_RESOLUTION * len_subfrm) / LEN_FRAME), gain1, gain2);
443
444
359M
  for (i = 0; i < lg; i++) {
445
358M
    x_tmp[i] = x[i];
446
358M
  }
447
448
883k
  iusace_adapt_lo_freq_emph(x, lg);
449
450
883k
  sq_gain = iusace_calc_sq_gain(x, target_bits, lg, pstr_scratch->p_sq_gain_en);
451
452
359M
  for (i = 0; i < lg; i++) {
453
358M
    sq_enc[i] = x[i] / sq_gain;
454
455
358M
    if (sq_enc[i] > 0.f)
456
175M
      sq_quant[i] = ((WORD32)(0.5f + sq_enc[i]));
457
182M
    else
458
182M
      sq_quant[i] = ((WORD32)(-0.5f + sq_enc[i]));
459
358M
  }
460
461
359M
  for (i = 0; i < lg; i++) {
462
358M
    params[i + 2] = sq_quant[i];
463
358M
    x[i] = (FLOAT32)sq_quant[i];
464
358M
  }
465
466
359M
  for (i = 0; i < lg; i++) {
467
358M
    en[i] = x[i] * x[i];
468
358M
  }
469
883k
  if (mode == 3) {
470
126k
    tmp = 0.9441f;
471
757k
  } else if (mode == 2) {
472
252k
    tmp = 0.8913f;
473
504k
  } else {
474
504k
    tmp = 0.7943f;
475
504k
  }
476
883k
  energy = 0.0f;
477
359M
  for (i = 0; i < lg; i++) {
478
358M
    if (en[i] > energy) {
479
43.7M
      energy = en[i];
480
43.7M
    }
481
358M
    en[i] = energy;
482
358M
    energy *= tmp;
483
358M
  }
484
883k
  energy = 0.0f;
485
359M
  for (i = lg - 1; i >= 0; i--) {
486
358M
    if (en[i] > energy) {
487
228M
      energy = en[i];
488
228M
    }
489
358M
    en[i] = energy;
490
358M
    energy *= tmp;
491
358M
  }
492
493
883k
  nsfill_en_thres = 0.707f;
494
495
883k
  tmp = 0.0625f;
496
883k
  k = 1;
497
359M
  for (i = 0; i < lg; i++) {
498
358M
    if (en[i] <= nsfill_en_thres) {
499
201M
      tmp += sq_enc[i] * sq_enc[i];
500
201M
      k++;
501
201M
    }
502
358M
  }
503
504
883k
  iusace_adapt_lo_freq_deemph(x, lg, alfd_gains);
505
506
883k
  energy = 1e-6f;
507
359M
  for (i = 0; i < lg; i++) {
508
358M
    corr += x_tmp[i] * x[i];
509
358M
    energy += x[i] * x[i];
510
358M
  }
511
883k
  gain_tcx = (corr / energy);
512
513
883k
  if (gain_tcx == 0.0f) {
514
37.0k
    gain_tcx = sq_gain;
515
37.0k
  }
516
517
883k
  energy = 0.0001f;
518
359M
  for (i = 0; i < lg; i++) {
519
358M
    tmp = x_tmp[i] - gain_tcx * x[i];
520
358M
    energy += tmp * tmp;
521
358M
  }
522
523
883k
  tmp = (FLOAT32)sqrt((energy * (2.0f / (FLOAT32)lg)) / (FLOAT32)lg);
524
525
359M
  for (i = 0; i < frame_len; i++) {
526
358M
    wsynth[i] = weighted_sig[i] + tmp;
527
358M
  }
528
529
883k
  energy = 0.01f;
530
359M
  for (i = 0; i < lg; i++) {
531
358M
    energy += x[i] * x[i];
532
358M
  }
533
534
883k
  tmp = (FLOAT32)(2.0f * sqrt(energy) / (FLOAT32)lg);
535
883k
  gain = gain_tcx * tmp;
536
537
883k
  index = (WORD32)floor(0.5f + (28.0f * (FLOAT32)log10(gain)));
538
883k
  if (index < 0) {
539
249k
    index = 0;
540
249k
  }
541
883k
  if (index > 127) {
542
15.2k
    index = 127;
543
15.2k
  }
544
883k
  params[1] = index;
545
546
883k
  gain_tcx = (FLOAT32)pow(10.0f, ((FLOAT32)index) / 28.0f) / tmp;
547
883k
  st->gain_tcx = gain_tcx;
548
549
883k
  sq_err_energy = 0.f;
550
883k
  n = 0;
551
23.2M
  for (k = lg / 2; k < lg;) {
552
22.3M
    tmp = 0.f;
553
554
22.3M
    max_k = MIN(lg, k + 8);
555
201M
    for (i = k; i < max_k; i++) {
556
179M
      tmp += (FLOAT32)sq_quant[i] * sq_quant[i];
557
179M
    }
558
22.3M
    if (tmp == 0.f) {
559
15.8M
      tmp = 0.f;
560
142M
      for (i = k; i < max_k; i++) {
561
126M
        tmp += sq_enc[i] * sq_enc[i];
562
126M
      }
563
564
15.8M
      sq_err_energy += (FLOAT32)log10((tmp / (FLOAT64)8) + 0.000000001);
565
15.8M
      n += 1;
566
15.8M
    }
567
22.3M
    k = max_k;
568
22.3M
  }
569
883k
  if (n > 0) {
570
866k
    fac_ns = (FLOAT32)pow(10., sq_err_energy / (FLOAT64)(2 * n));
571
866k
  } else {
572
16.7k
    fac_ns = 0.f;
573
16.7k
  }
574
575
883k
  tmp = 8.0f - (16.0f * fac_ns);
576
577
883k
  index = (WORD32)floor(tmp + 0.5);
578
883k
  if (index < 0) {
579
0
    index = 0;
580
0
  }
581
883k
  if (index > 7) {
582
197k
    index = 7;
583
197k
  }
584
585
883k
  params[0] = index;
586
587
883k
  iusace_noise_shaping(x, lg, ((FDNS_RESOLUTION * len_subfrm) / LEN_FRAME), gain1, gain2);
588
589
883k
  iusace_tcx_imdct(x, xn_buf, (2 * fac_length), frame_len - (2 * fac_length), (2 * fac_length),
590
883k
                   pstr_scratch);
591
568M
  for (i = 0; i < frame_len + (2 * fac_length); i++) {
592
567M
    xn_buf[i] = xn_buf[i] * (2.0f / lg);
593
567M
  }
594
595
105M
  for (i = 0; i < fac_len_prev; i++) {
596
104M
    fac_window[i] = sine_window_prev[i] * sine_window_prev[(2 * fac_len_prev) - 1 - i];
597
104M
    fac_window[fac_len_prev + i] =
598
104M
        1.0f - (sine_window_prev[fac_len_prev + i] * sine_window_prev[fac_len_prev + i]);
599
104M
  }
600
601
105M
  for (i = 0; i < fac_len_prev; i++) {
602
104M
    xn1[ORDER + i] -= sq_gain * xn_buf[fac_length + i] * sine_window_prev[fac_len_prev + i];
603
104M
  }
604
105M
  for (i = 0; i < fac_len; i++) {
605
104M
    xn2[ORDER + i] -= sq_gain * xn_buf[i + frame_len] * sine_window[(2 * fac_len) - 1 - i];
606
104M
  }
607
608
15.0M
  for (i = 0; i < ORDER; i++) {
609
14.1M
    xn1[i] -= lpd_state->tcx_quant[1 + 128 - ORDER + i];
610
14.1M
    xn2[i] -= sq_gain * xn_buf[frame_len - ORDER + i];
611
14.1M
  }
612
613
105M
  for (i = 0; i < fac_len_prev; i++) {
614
104M
    facelp[i] = lpd_state->tcx_quant[1 + 128 + i] * fac_window[fac_len_prev + i] +
615
104M
                lpd_state->tcx_quant[1 + 128 - 1 - i] * fac_window[fac_len_prev - 1 - i];
616
104M
  }
617
618
883k
  energy = 0.0f;
619
105M
  for (i = 0; i < fac_len_prev; i++) energy += xn1[ORDER + i] * xn1[ORDER + i];
620
883k
  energy *= 2.0f;
621
883k
  tmp = 0.0f;
622
105M
  for (i = 0; i < fac_len_prev; i++) tmp += facelp[i] * facelp[i];
623
883k
  if (tmp > energy)
624
192k
    gain = (FLOAT32)sqrt(energy / tmp);
625
690k
  else
626
690k
    gain = 1.0f;
627
628
105M
  for (i = 0; i < fac_len_prev; i++) {
629
104M
    xn1[ORDER + i] -= gain * facelp[i];
630
104M
  }
631
632
883k
  iusace_get_weighted_lpc(lpc_coeffs_quant + (ORDER + 1), lp_flt_coeffs);
633
883k
  iusace_compute_lp_residual(lp_flt_coeffs, xn1 + ORDER, x1, fac_len_prev);
634
635
883k
  iusace_get_weighted_lpc(lpc_coeffs_quant + (2 * (ORDER + 1)), lp_flt_coeffs);
636
883k
  iusace_compute_lp_residual(lp_flt_coeffs, xn2 + ORDER, x2, fac_len);
637
638
883k
  iusace_tcx_mdct(x1, x1, fac_len_prev, pstr_scratch);
639
883k
  iusace_tcx_mdct(x2, x2, fac_len, pstr_scratch);
640
641
883k
  gain_prev = (FLOAT32)(sq_gain * 0.5f * sqrt(((FLOAT32)fac_len_prev) / (FLOAT32)frame_len));
642
883k
  gain_next = (FLOAT32)(sq_gain * 0.5f * sqrt(((FLOAT32)fac_len) / (FLOAT32)frame_len));
643
644
105M
  for (i = 0; i < fac_len_prev; i++) {
645
104M
    x1[i] /= gain_prev;
646
104M
  }
647
105M
  for (i = 0; i < fac_len; i++) {
648
104M
    x2[i] /= gain_next;
649
104M
  }
650
27.0M
  for (i = 0; i < fac_len_prev / 4; i++) {
651
26.1M
    k = i * lg / (8 * fac_len_prev);
652
26.1M
    x1[i] /= alfd_gains[k];
653
26.1M
  }
654
27.0M
  for (i = 0; i < fac_len / 4; i++) {
655
26.1M
    k = i * lg / (8 * fac_len);
656
26.1M
    x2[i] /= alfd_gains[k];
657
26.1M
  }
658
659
13.9M
  for (i = 0; i < fac_len; i += 8) {
660
13.0M
    iusace_find_nearest_neighbor(&x2[i], &y[i]);
661
13.0M
  }
662
105M
  for (i = 0; i < fac_len; i++) {
663
104M
    lpd_state->avq_params[i] = y[i];
664
104M
    x2[i] = (FLOAT32)y[i];
665
104M
  }
666
667
13.9M
  for (i = 0; i < fac_len_prev; i += 8) {
668
13.0M
    iusace_find_nearest_neighbor(&x1[i], &y[i]);
669
13.0M
  }
670
671
105M
  for (i = 0; i < fac_len_prev; i++) {
672
104M
    x1[i] = (FLOAT32)y[i];
673
104M
  }
674
675
883k
  gain_prev = (FLOAT32)(gain_tcx * 0.5f * sqrt(((FLOAT32)fac_len_prev) / (FLOAT32)frame_len));
676
883k
  gain_next = (FLOAT32)(gain_tcx * 0.5f * sqrt(((FLOAT32)fac_len) / (FLOAT32)frame_len));
677
678
105M
  for (i = 0; i < fac_len_prev; i++) {
679
104M
    x1[i] *= gain_prev;
680
104M
  }
681
105M
  for (i = 0; i < fac_len; i++) {
682
104M
    x2[i] *= gain_next;
683
104M
  }
684
27.0M
  for (i = 0; i<fac_len_prev>> 2; i++) {
685
26.1M
    k = i * lg / (fac_len_prev << 3);
686
26.1M
    x1[i] *= alfd_gains[k];
687
26.1M
  }
688
27.0M
  for (i = 0; i<fac_len>> 2; i++) {
689
26.1M
    k = i * lg / (fac_len << 3);
690
26.1M
    x2[i] *= alfd_gains[k];
691
26.1M
  }
692
883k
  iusace_tcx_mdct(x1, xn1, fac_len_prev, pstr_scratch);
693
883k
  iusace_tcx_mdct(x2, xn2, fac_len, pstr_scratch);
694
695
883k
  FLOAT32 coeff1 = (2.0f / (FLOAT32)fac_len_prev), coeff2 = (2.0f / (FLOAT32)fac_len);
696
697
105M
  for (i = 0; i < fac_len_prev; i++) {
698
104M
    xn1[i] = xn1[i] * coeff1;
699
104M
  }
700
701
105M
  for (i = 0; i < fac_len; i++) {
702
104M
    xn2[i] = xn2[i] * coeff2;
703
104M
  }
704
705
883k
  memset(xn1 + fac_len_prev, 0, fac_len_prev * sizeof(FLOAT32));
706
883k
  memset(xn2 + fac_len, 0, fac_len * sizeof(FLOAT32));
707
708
883k
  iusace_get_weighted_lpc(lpc_coeffs_quant + (ORDER + 1), lp_flt_coeffs);
709
883k
  iusace_synthesis_tool_float(lp_flt_coeffs, xn1, xn1, 2 * fac_len_prev, xn1 + fac_len_prev,
710
883k
                              pstr_scratch->p_buf_synthesis_tool);
711
712
883k
  iusace_get_weighted_lpc(lpc_coeffs_quant + (2 * (ORDER + 1)), lp_flt_coeffs);
713
883k
  iusace_synthesis_tool_float(lp_flt_coeffs, xn2, xn2, fac_len, xn2 + fac_len,
714
883k
                              pstr_scratch->p_buf_synthesis_tool);
715
716
105M
  for (i = 0; i < fac_len_prev; i++) {
717
104M
    xn1[i] += facelp[i];
718
104M
  }
719
720
568M
  for (i = 0; i < frame_len + (fac_length << 1); i++) {
721
567M
    xn_buf[i] *= gain_tcx;
722
567M
  }
723
724
883k
  if (lpd_state->mode >= -1) {
725
209M
    for (i = 0; i < (2 * fac_len_prev); i++) {
726
208M
      xn_buf[i + fac_length - fac_len_prev] *= sine_window_prev[i];
727
208M
    }
728
883k
    for (i = 0; i < fac_length - fac_len_prev; i++) {
729
0
      xn_buf[i] = 0.0f;
730
0
    }
731
883k
  }
732
209M
  for (i = 0; i < (2 * fac_len); i++) {
733
208M
    xn_buf[i + frame_len + fac_length - fac_len] *= sine_window[(2 * fac_len) - 1 - i];
734
208M
  }
735
883k
  for (i = 0; i < fac_length - fac_len; i++) {
736
0
    xn_buf[i + frame_len + fac_length + fac_len] = 0.0f;
737
0
  }
738
739
883k
  if (lpd_state->mode != 0) {
740
132M
    for (i = 0; i < (2 * fac_length); i++) {
741
132M
      xn_buf[i] += lpd_state->tcx_quant[1 + 128 - fac_length + i];
742
132M
    }
743
744
556k
    mem_tcx_q = lpd_state->tcx_quant[128 - fac_length];
745
556k
  } else {
746
38.5M
    for (i = 0; i < fac_len_prev; i++) {
747
38.2M
      params[i - fac_len_prev] = y[i];
748
38.2M
    }
749
750
76.8M
    for (i = 0; i < (2 * fac_len_prev); i++) {
751
76.5M
      xn_buf[i + fac_length] += xn1[i];
752
76.5M
    }
753
326k
    mem_tcx_q = lpd_state->tcx_quant[128];
754
326k
  }
755
756
883k
  memcpy(lpd_state->tcx_quant, xn_buf + frame_len + fac_length - 128 - 1,
757
883k
         (1 + 256) * sizeof(FLOAT32));
758
759
105M
  for (i = 0; i < fac_len; i++) {
760
104M
    xn_buf[i + frame_len + (fac_length - fac_len)] += xn2[i];
761
104M
  }
762
763
883k
  if (lpd_state->mode > 0) {
764
545k
    iusace_apply_preemph(xn_buf, TILT_FAC, fac_length, &mem_tcx_q);
765
766
545k
    ptr_lp_flt_coeffs = lpd_state->lpc_coeffs_quant;
767
768
545k
    TTT = fac_length % LEN_SUBFR;
769
545k
    if (TTT != 0) {
770
156k
      memcpy(&(lpd_state->synth[ORDER + 128 - fac_length]), &xn_buf[0], TTT * sizeof(FLOAT32));
771
156k
      iusace_compute_lp_residual(ptr_lp_flt_coeffs, &(lpd_state->synth[ORDER + 128 - fac_length]),
772
156k
                                 &(lpd_state->acelp_exc[(2 * len_subfrm) - fac_length]), TTT);
773
774
156k
      ptr_lp_flt_coeffs += (ORDER + 1);
775
156k
    }
776
777
1.47M
    for (i_subfr = TTT; i_subfr < fac_length; i_subfr += LEN_SUBFR) {
778
934k
      memcpy(&(lpd_state->synth[ORDER + 128 - fac_length + i_subfr]), &xn_buf[i_subfr],
779
934k
             LEN_SUBFR * sizeof(FLOAT32));
780
934k
      iusace_compute_lp_residual(
781
934k
          ptr_lp_flt_coeffs, &(lpd_state->synth[ORDER + 128 - fac_length + i_subfr]),
782
934k
          &(lpd_state->acelp_exc[(2 * len_subfrm) - fac_length + i_subfr]), LEN_SUBFR);
783
934k
      ptr_lp_flt_coeffs += (ORDER + 1);
784
934k
    }
785
786
545k
    ptr_lp_flt_coeffs = lpd_state->lpc_coeffs;
787
1.63M
    for (i_subfr = 0; i_subfr < fac_length; i_subfr += LEN_SUBFR) {
788
1.09M
      iusace_get_weighted_lpc(ptr_lp_flt_coeffs, lp_flt_coeffs);
789
1.09M
      iusace_compute_lp_residual(lp_flt_coeffs,
790
1.09M
                                 &(lpd_state->synth[ORDER + 128 - fac_length + i_subfr]),
791
1.09M
                                 &(lpd_state->wsynth[1 + 128 - fac_length + i_subfr]), LEN_SUBFR);
792
1.09M
      ptr_lp_flt_coeffs += (ORDER + 1);
793
1.09M
    }
794
545k
    tmp = lpd_state->wsynth[0 + 128 - fac_length];
795
545k
    iusace_apply_deemph(&(lpd_state->wsynth[1 + 128 - fac_length]), TILT_FAC, fac_length, &tmp);
796
545k
  }
797
798
883k
  k = ((frame_len / LEN_SUBFR) - 2) * (ORDER + 1);
799
883k
  memcpy(lpd_state->lpc_coeffs, lpc_coeffs + k, 2 * (ORDER + 1) * sizeof(FLOAT32));
800
801
883k
  memcpy(lpd_state->lpc_coeffs_quant, lpc_coeffs_quant + (2 * (ORDER + 1)),
802
883k
         (ORDER + 1) * sizeof(FLOAT32));
803
883k
  memcpy(lpd_state->lpc_coeffs_quant + (ORDER + 1), lpd_state->lpc_coeffs_quant,
804
883k
         (ORDER + 1) * sizeof(FLOAT32));
805
806
883k
  memcpy(synth - 128, &(lpd_state->synth[ORDER]), 128 * sizeof(FLOAT32));
807
883k
  lpd_state->tcx_fac = xn[frame_len - 1];
808
809
883k
  iusace_apply_preemph(xn, TILT_FAC, frame_len, &mem_tcx_q);
810
6.48M
  for (i_subfr = 0; i_subfr < frame_len; i_subfr += LEN_SUBFR) {
811
5.59M
    memcpy(&synth[i_subfr], &xn[i_subfr], LEN_SUBFR * sizeof(FLOAT32));
812
5.59M
    iusace_compute_lp_residual(lpc_coeffs_quant + (2 * (ORDER + 1)), &synth[i_subfr],
813
5.59M
                               &xn[i_subfr], LEN_SUBFR);
814
5.59M
  }
815
883k
  memcpy(lpd_state->synth, synth + frame_len - (ORDER + 128), (ORDER + 128) * sizeof(FLOAT32));
816
817
883k
  if (frame_len == len_subfrm) {
818
504k
    memcpy(x, lpd_state->acelp_exc + len_subfrm, len_subfrm * sizeof(FLOAT32));
819
504k
    memcpy(lpd_state->acelp_exc, x, len_subfrm * sizeof(FLOAT32));
820
504k
    memcpy(lpd_state->acelp_exc + len_subfrm, xn, len_subfrm * sizeof(FLOAT32));
821
504k
  } else {
822
378k
    memcpy(lpd_state->acelp_exc, xn + frame_len - (2 * len_subfrm),
823
378k
           2 * len_subfrm * sizeof(FLOAT32));
824
378k
  }
825
826
883k
  memcpy(wsynth - 128, &(lpd_state->wsynth[1]), 128 * sizeof(FLOAT32));
827
828
883k
  ptr_lp_flt_coeffs = lpc_coeffs;
829
6.48M
  for (i_subfr = 0; i_subfr < frame_len; i_subfr += LEN_SUBFR) {
830
5.59M
    iusace_get_weighted_lpc(ptr_lp_flt_coeffs, lp_flt_coeffs);
831
5.59M
    iusace_compute_lp_residual(lp_flt_coeffs, &synth[i_subfr], &wsynth[i_subfr], LEN_SUBFR);
832
5.59M
    ptr_lp_flt_coeffs += (ORDER + 1);
833
5.59M
  }
834
883k
  tmp = wsynth[-1];
835
883k
  iusace_apply_deemph(wsynth, TILT_FAC, frame_len, &tmp);
836
837
883k
  memcpy(lpd_state->wsynth, wsynth + frame_len - (1 + 128), (1 + 128) * sizeof(FLOAT32));
838
839
883k
  lpd_state->mode = mode;
840
841
883k
  lpd_state->num_bits = 10 + target_bits;
842
843
883k
  return;
844
883k
}