Coverage Report

Created: 2026-08-13 06:34

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.72M
                            iusace_scratch_mem *pstr_scratch) {
115
1.72M
  FLOAT32 *in_out = pstr_scratch->p_in_out_tcx;
116
1.72M
  WORD32 i;
117
118
31.1M
  for (i = 0; i < ORDER + 1; i++) {
119
29.3M
    in_out[2 * i] = (FLOAT32)(ptr_lpc_coeffs[i] * iusace_lpc_pre_twid_cos[i]);
120
29.3M
    in_out[2 * i + 1] = (FLOAT32)(-ptr_lpc_coeffs[i] * iusace_lpc_pre_twid_sin[i]);
121
29.3M
  }
122
176M
  for (; i < length; i++) {
123
174M
    in_out[2 * i] = 0.f;
124
174M
    in_out[2 * i + 1] = 0.f;
125
174M
  }
126
127
1.72M
  iusace_complex_fft(in_out, length, pstr_scratch);
128
129
103M
  for (i = 0; i<length>> 1; i++) {
130
102M
    mdct_gains[i] = (FLOAT32)(
131
102M
        1.0f / sqrt(in_out[2 * i] * in_out[2 * i] + in_out[2 * i + 1] * in_out[2 * i + 1]));
132
102M
  }
133
134
1.72M
  return;
135
1.72M
}
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
864k
                                 FLOAT32 *gain2) {
162
864k
  WORD32 i, k;
163
864k
  FLOAT32 r, r_prev, g1, g2, a = 0, b = 0;
164
165
864k
  k = lg/M;
166
167
864k
  r_prev = 0;
168
350M
  for (i = 0; i < lg; i++) {
169
350M
    if ((i % k) == 0) {
170
51.0M
      g1 = gain1[i / k];
171
51.0M
      g2 = gain2[i / k];
172
51.0M
      a = 2.0f * g1 * g2 / (g1 + g2);
173
51.0M
      b = (g2 - g1) / (g1 + g2);
174
51.0M
    }
175
176
350M
    r = a * rr[i] + b * r_prev;
177
178
350M
    rr[i] = r;
179
350M
    r_prev = r;
180
350M
  }
181
182
864k
  return;
183
864k
}
184
185
864k
static VOID iusace_pre_shaping(FLOAT32 *rr, WORD32 lg, WORD32 M, FLOAT32 *gain1, FLOAT32 *gain2) {
186
864k
  WORD32 i, k;
187
864k
  FLOAT32 r, r_prev, g1, g2, a = 0, b = 0;
188
189
864k
  k = lg / M;
190
191
864k
  r_prev = 0;
192
350M
  for (i = 0; i < lg; i++) {
193
350M
    if ((i % k) == 0) {
194
51.0M
      g1 = gain1[i / k];
195
51.0M
      g2 = gain2[i / k];
196
197
51.0M
      a = (g1 + g2) / (2.0f * g1 * g2);
198
51.0M
      b = (g1 - g2) / (2.0f * g1 * g2);
199
51.0M
    }
200
201
350M
    r = a * rr[i] + b * r_prev;
202
203
350M
    r_prev = rr[i];
204
350M
    rr[i] = r;
205
350M
  }
206
207
864k
  return;
208
864k
}
209
210
864k
static VOID iusace_adapt_lo_freq_emph(FLOAT32 *signal, WORD32 length) {
211
864k
  WORD32 i, j, i_max;
212
864k
  FLOAT32 max_energy, factor, temp;
213
214
864k
  i_max = length >> 2;
215
216
864k
  max_energy = 0.01f;
217
11.8M
  for (i = 0; i < i_max; i += 8) {
218
10.9M
    temp = 0.01f;
219
98.4M
    for (j = i; j < i + 8; j++) {
220
87.5M
      temp += signal[j] * signal[j];
221
87.5M
    }
222
10.9M
    if (temp > max_energy) {
223
1.12M
      max_energy = temp;
224
1.12M
    }
225
10.9M
  }
226
227
864k
  factor = 10.0f;
228
11.8M
  for (i = 0; i < i_max; i += 8) {
229
10.9M
    temp = 0.01f;
230
98.4M
    for (j = i; j < i + 8; j++) {
231
87.5M
      temp += signal[j] * signal[j];
232
87.5M
    }
233
10.9M
    temp = (FLOAT32)sqrt(sqrt(max_energy / temp));
234
10.9M
    if (temp < factor) {
235
1.13M
      factor = temp;
236
1.13M
    }
237
98.4M
    for (j = i; j < i + 8; j++) {
238
87.5M
      signal[j] *= factor;
239
87.5M
    }
240
10.9M
  }
241
864k
  return;
242
864k
}
243
244
864k
static VOID iusace_adapt_lo_freq_deemph(FLOAT32 *signal, WORD32 length, FLOAT32 *gains) {
245
864k
  WORD32 i, j, i_max;
246
864k
  FLOAT32 max_energy, factor, energy, rm;
247
248
864k
  i_max = length >> 2;
249
250
864k
  max_energy = 0.01f;
251
11.8M
  for (i = 0; i < i_max; i += 8) {
252
10.9M
    energy = 0.01f;
253
98.4M
    for (j = i; j < i + 8; j++) {
254
87.5M
      energy += signal[j] * signal[j];
255
87.5M
    }
256
10.9M
    if (energy > max_energy) {
257
1.02M
      max_energy = energy;
258
1.02M
    }
259
10.9M
  }
260
261
864k
  factor = 0.1f;
262
11.8M
  for (i = 0; i < i_max; i += 8) {
263
10.9M
    energy = 0.01f;
264
98.4M
    for (j = i; j < i + 8; j++) {
265
87.5M
      energy += signal[j] * signal[j];
266
87.5M
    }
267
268
10.9M
    rm = (FLOAT32)sqrt(energy / max_energy);
269
10.9M
    if (rm > factor) {
270
1.06M
      factor = rm;
271
1.06M
    }
272
98.4M
    for (j = i; j < i + 8; j++) {
273
87.5M
      signal[j] *= factor;
274
87.5M
    }
275
10.9M
    gains[i / 8] = factor;
276
10.9M
  }
277
278
864k
  return;
279
864k
}
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
864k
                           WORD32 *params, WORD32 *n_param, WORD32 ch_idx, WORD32 k_idx) {
285
864k
  ia_usac_td_encoder_struct *st = usac_data->td_encoder[ch_idx];
286
864k
  iusace_scratch_mem *pstr_scratch = &usac_data->str_scratch;
287
864k
  FLOAT32 *weighted_sig = &pstr_scratch->p_wsig_buf[k_idx * st->len_subfrm];
288
864k
  FLOAT32 *wsynth = pstr_scratch->p_wsyn_tcx_buf;
289
864k
  FLOAT32 *synth = pstr_scratch->p_synth_tcx_buf;
290
864k
  WORD32 i, k, n, mode, i_subfr, lg, lext, index, target_bits;
291
864k
  FLOAT32 tmp, gain, fac_ns, energy, gain_tcx, nsfill_en_thres;
292
864k
  FLOAT32 *ptr_lp_flt_coeffs, lp_flt_coeffs[ORDER + 1];
293
864k
  const FLOAT32 *sine_window_prev, *sine_window;
294
864k
  FLOAT32 mem_tcx_q;
295
864k
  FLOAT32 *xn;
296
864k
  FLOAT32 *xn1 = pstr_scratch->p_xn1_tcx;
297
864k
  FLOAT32 *xn_buf = pstr_scratch->p_xn_buf_tcx;
298
864k
  FLOAT32 *x = pstr_scratch->p_x_tcx;
299
864k
  FLOAT32 *x_tmp = pstr_scratch->p_x_tmp_tcx;
300
864k
  FLOAT32 *en = pstr_scratch->p_en_tcx;
301
864k
  FLOAT32 sq_gain;
302
864k
  FLOAT32 gain_prev, gain_next;
303
864k
  FLOAT32 *alfd_gains = pstr_scratch->p_alfd_gains_tcx;
304
864k
  FLOAT32 *sq_enc = pstr_scratch->p_sq_enc_tcx;
305
864k
  WORD32 *sq_quant = pstr_scratch->p_sq_quant_tcx;
306
864k
  FLOAT32 sq_err_energy;
307
864k
  WORD32 max_k;
308
864k
  FLOAT32 *gain1 = pstr_scratch->p_gain1_tcx;
309
864k
  FLOAT32 *gain2 = pstr_scratch->p_gain2_tcx;
310
864k
  FLOAT32 *facelp = pstr_scratch->p_facelp_tcx;
311
864k
  FLOAT32 *xn2 = pstr_scratch->p_xn2_tcx;
312
864k
  FLOAT32 *fac_window = pstr_scratch->p_fac_window_tcx;
313
864k
  FLOAT32 *x1 = pstr_scratch->p_x1_tcx;
314
864k
  FLOAT32 *x2 = pstr_scratch->p_x2_tcx;
315
864k
  WORD32 *y = pstr_scratch->p_y_tcx;
316
317
864k
  WORD32 TTT;
318
864k
  FLOAT32 corr = 0;
319
864k
  WORD32 len_subfrm = st->len_subfrm;
320
864k
  WORD32 fac_length = len_subfrm >> 1;
321
864k
  WORD32 fac_len_prev, fac_len;
322
323
864k
  if (frame_len == 4 * st->len_subfrm) {
324
123k
    if (st->last_was_short) {
325
0
      fac_len_prev = (st->len_frame) / 16;
326
123k
    } else {
327
123k
      fac_len_prev = st->len_subfrm / 2;
328
123k
    }
329
123k
    if (st->next_is_short) {
330
0
      fac_len = (st->len_frame) / 16;
331
123k
    } else {
332
123k
      fac_len = st->len_subfrm / 2;
333
123k
    }
334
740k
  } else if (frame_len == 2 * st->len_subfrm) {
335
246k
    if (k_idx == 0 && st->last_was_short) {
336
0
      fac_len_prev = (st->len_frame) / 16;
337
246k
    } else {
338
246k
      fac_len_prev = st->len_subfrm / 2;
339
246k
    }
340
246k
    if (k_idx == 2 && st->next_is_short) {
341
0
      fac_len = (st->len_frame) / 16;
342
246k
    } else {
343
246k
      fac_len = st->len_subfrm / 2;
344
246k
    }
345
493k
  } else {
346
493k
    if (k_idx == 0 && st->last_was_short) {
347
0
      fac_len_prev = (st->len_frame) / 16;
348
493k
    } else {
349
493k
      fac_len_prev = st->len_subfrm / 2;
350
493k
    }
351
493k
    if (k_idx == 3 && st->next_is_short) {
352
0
      fac_len = (st->len_frame) / 16;
353
493k
    } else {
354
493k
      fac_len = st->len_subfrm / 2;
355
493k
    }
356
493k
  }
357
358
864k
  memset(xn_buf, 0, (128 + frame_len + 128) * sizeof(FLOAT32));
359
360
864k
  mode = frame_len / len_subfrm;
361
362
864k
  if (mode > 2) {
363
123k
    mode = 3;
364
123k
  }
365
366
864k
  if (lpd_state->mode == 0) {
367
312k
    params += fac_len_prev;
368
312k
  }
369
864k
  switch (fac_len_prev) {
370
0
    case 64:
371
0
      sine_window_prev = iusace_sin_window_128;
372
0
      break;
373
864k
    default:
374
864k
      sine_window_prev = iusace_sin_window_256;
375
864k
      break;
376
864k
  }
377
864k
  switch (fac_len) {
378
0
    case 64:
379
0
      sine_window = iusace_sin_window_128;
380
0
      break;
381
864k
    default:
382
864k
      sine_window = iusace_sin_window_256;
383
864k
      break;
384
864k
  }
385
386
864k
  lg = frame_len;
387
864k
  lext = fac_length;
388
864k
  xn = xn_buf + fac_length;
389
390
864k
  *n_param = lg;
391
392
864k
  target_bits = num_bits_per_supfrm - 10;
393
394
102M
  for (i = 0; i < fac_length; i++) {
395
102M
    xn_buf[i] = lpd_state->tcx_mem[i + 128 - fac_length];
396
102M
  }
397
398
864k
  memcpy(xn, speech, (frame_len + fac_length) * sizeof(FLOAT32));
399
400
864k
  tmp = xn[-1];
401
402
864k
  iusace_apply_deemph(xn, TILT_FAC, frame_len, &tmp);
403
404
864k
  memcpy(lpd_state->tcx_mem, &xn[frame_len - 128], 128 * sizeof(FLOAT32));
405
406
864k
  memcpy(&xn[frame_len], &speech[frame_len], lext * sizeof(FLOAT32));
407
864k
  iusace_apply_deemph(&xn[frame_len], TILT_FAC, lext, &tmp);
408
409
116M
  for (i = 0; i < ORDER + fac_len_prev; i++) {
410
115M
    xn1[i] = xn_buf[fac_length - ORDER + i];
411
115M
  }
412
116M
  for (i = 0; i < ORDER + fac_len; i++) {
413
115M
    xn2[i] = xn_buf[frame_len - ORDER + i];
414
115M
  }
415
416
864k
  if (lpd_state->mode >= -1) {
417
864k
    for (i = 0; i < fac_length - fac_len_prev; i++) {
418
0
      xn_buf[i] = 0.0f;
419
0
    }
420
205M
    for (i = fac_length - fac_len_prev; i < (fac_length + fac_len_prev); i++) {
421
204M
      xn_buf[i] *= sine_window_prev[i - fac_length + fac_len_prev];
422
204M
    }
423
205M
    for (i = 0; i < (2 * fac_len); i++) {
424
204M
      xn_buf[frame_len + fac_length - fac_len + i] *= sine_window[(2 * fac_len) - 1 - i];
425
204M
    }
426
864k
    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
864k
  }
430
431
864k
  iusace_tcx_mdct_main(xn_buf, x, (2 * fac_length), frame_len - (2 * fac_length),
432
864k
                       (2 * fac_length), pstr_scratch);
433
434
864k
  iusace_get_weighted_lpc(lpc_coeffs_quant + (ORDER + 1), lp_flt_coeffs);
435
864k
  iusace_lpc_mdct(lp_flt_coeffs, gain1, ((FDNS_RESOLUTION * len_subfrm) / LEN_FRAME) << 1,
436
864k
                  pstr_scratch);
437
438
864k
  iusace_get_weighted_lpc(lpc_coeffs_quant + (2 * (ORDER + 1)), lp_flt_coeffs);
439
864k
  iusace_lpc_mdct(lp_flt_coeffs, gain2, ((FDNS_RESOLUTION * len_subfrm) / LEN_FRAME) << 1,
440
864k
                  pstr_scratch);
441
442
864k
  iusace_pre_shaping(x, lg, ((FDNS_RESOLUTION * len_subfrm) / LEN_FRAME), gain1, gain2);
443
444
350M
  for (i = 0; i < lg; i++) {
445
350M
    x_tmp[i] = x[i];
446
350M
  }
447
448
864k
  iusace_adapt_lo_freq_emph(x, lg);
449
450
864k
  sq_gain = iusace_calc_sq_gain(x, target_bits, lg, pstr_scratch->p_sq_gain_en);
451
452
350M
  for (i = 0; i < lg; i++) {
453
350M
    sq_enc[i] = x[i] / sq_gain;
454
455
350M
    if (sq_enc[i] > 0.f)
456
171M
      sq_quant[i] = ((WORD32)(0.5f + sq_enc[i]));
457
178M
    else
458
178M
      sq_quant[i] = ((WORD32)(-0.5f + sq_enc[i]));
459
350M
  }
460
461
350M
  for (i = 0; i < lg; i++) {
462
350M
    params[i + 2] = sq_quant[i];
463
350M
    x[i] = (FLOAT32)sq_quant[i];
464
350M
  }
465
466
350M
  for (i = 0; i < lg; i++) {
467
350M
    en[i] = x[i] * x[i];
468
350M
  }
469
864k
  if (mode == 3) {
470
123k
    tmp = 0.9441f;
471
740k
  } else if (mode == 2) {
472
246k
    tmp = 0.8913f;
473
493k
  } else {
474
493k
    tmp = 0.7943f;
475
493k
  }
476
864k
  energy = 0.0f;
477
350M
  for (i = 0; i < lg; i++) {
478
350M
    if (en[i] > energy) {
479
42.7M
      energy = en[i];
480
42.7M
    }
481
350M
    en[i] = energy;
482
350M
    energy *= tmp;
483
350M
  }
484
864k
  energy = 0.0f;
485
350M
  for (i = lg - 1; i >= 0; i--) {
486
350M
    if (en[i] > energy) {
487
222M
      energy = en[i];
488
222M
    }
489
350M
    en[i] = energy;
490
350M
    energy *= tmp;
491
350M
  }
492
493
864k
  nsfill_en_thres = 0.707f;
494
495
864k
  tmp = 0.0625f;
496
864k
  k = 1;
497
350M
  for (i = 0; i < lg; i++) {
498
350M
    if (en[i] <= nsfill_en_thres) {
499
196M
      tmp += sq_enc[i] * sq_enc[i];
500
196M
      k++;
501
196M
    }
502
350M
  }
503
504
864k
  iusace_adapt_lo_freq_deemph(x, lg, alfd_gains);
505
506
864k
  energy = 1e-6f;
507
350M
  for (i = 0; i < lg; i++) {
508
350M
    corr += x_tmp[i] * x[i];
509
350M
    energy += x[i] * x[i];
510
350M
  }
511
864k
  gain_tcx = (corr / energy);
512
513
864k
  if (gain_tcx == 0.0f) {
514
35.8k
    gain_tcx = sq_gain;
515
35.8k
  }
516
517
864k
  energy = 0.0001f;
518
350M
  for (i = 0; i < lg; i++) {
519
350M
    tmp = x_tmp[i] - gain_tcx * x[i];
520
350M
    energy += tmp * tmp;
521
350M
  }
522
523
864k
  tmp = (FLOAT32)sqrt((energy * (2.0f / (FLOAT32)lg)) / (FLOAT32)lg);
524
525
350M
  for (i = 0; i < frame_len; i++) {
526
350M
    wsynth[i] = weighted_sig[i] + tmp;
527
350M
  }
528
529
864k
  energy = 0.01f;
530
350M
  for (i = 0; i < lg; i++) {
531
350M
    energy += x[i] * x[i];
532
350M
  }
533
534
864k
  tmp = (FLOAT32)(2.0f * sqrt(energy) / (FLOAT32)lg);
535
864k
  gain = gain_tcx * tmp;
536
537
864k
  index = (WORD32)floor(0.5f + (28.0f * (FLOAT32)log10(gain)));
538
864k
  if (index < 0) {
539
254k
    index = 0;
540
254k
  }
541
864k
  if (index > 127) {
542
14.5k
    index = 127;
543
14.5k
  }
544
864k
  params[1] = index;
545
546
864k
  gain_tcx = (FLOAT32)pow(10.0f, ((FLOAT32)index) / 28.0f) / tmp;
547
864k
  st->gain_tcx = gain_tcx;
548
549
864k
  sq_err_energy = 0.f;
550
864k
  n = 0;
551
22.7M
  for (k = lg / 2; k < lg;) {
552
21.8M
    tmp = 0.f;
553
554
21.8M
    max_k = MIN(lg, k + 8);
555
196M
    for (i = k; i < max_k; i++) {
556
175M
      tmp += (FLOAT32)sq_quant[i] * sq_quant[i];
557
175M
    }
558
21.8M
    if (tmp == 0.f) {
559
15.3M
      tmp = 0.f;
560
138M
      for (i = k; i < max_k; i++) {
561
122M
        tmp += sq_enc[i] * sq_enc[i];
562
122M
      }
563
564
15.3M
      sq_err_energy += (FLOAT32)log10((tmp / (FLOAT64)8) + 0.000000001);
565
15.3M
      n += 1;
566
15.3M
    }
567
21.8M
    k = max_k;
568
21.8M
  }
569
864k
  if (n > 0) {
570
845k
    fac_ns = (FLOAT32)pow(10., sq_err_energy / (FLOAT64)(2 * n));
571
845k
  } else {
572
18.7k
    fac_ns = 0.f;
573
18.7k
  }
574
575
864k
  tmp = 8.0f - (16.0f * fac_ns);
576
577
864k
  index = (WORD32)floor(tmp + 0.5);
578
864k
  if (index < 0) {
579
0
    index = 0;
580
0
  }
581
864k
  if (index > 7) {
582
191k
    index = 7;
583
191k
  }
584
585
864k
  params[0] = index;
586
587
864k
  iusace_noise_shaping(x, lg, ((FDNS_RESOLUTION * len_subfrm) / LEN_FRAME), gain1, gain2);
588
589
864k
  iusace_tcx_imdct(x, xn_buf, (2 * fac_length), frame_len - (2 * fac_length), (2 * fac_length),
590
864k
                   pstr_scratch);
591
555M
  for (i = 0; i < frame_len + (2 * fac_length); i++) {
592
554M
    xn_buf[i] = xn_buf[i] * (2.0f / lg);
593
554M
  }
594
595
102M
  for (i = 0; i < fac_len_prev; i++) {
596
102M
    fac_window[i] = sine_window_prev[i] * sine_window_prev[(2 * fac_len_prev) - 1 - i];
597
102M
    fac_window[fac_len_prev + i] =
598
102M
        1.0f - (sine_window_prev[fac_len_prev + i] * sine_window_prev[fac_len_prev + i]);
599
102M
  }
600
601
102M
  for (i = 0; i < fac_len_prev; i++) {
602
102M
    xn1[ORDER + i] -= sq_gain * xn_buf[fac_length + i] * sine_window_prev[fac_len_prev + i];
603
102M
  }
604
102M
  for (i = 0; i < fac_len; i++) {
605
102M
    xn2[ORDER + i] -= sq_gain * xn_buf[i + frame_len] * sine_window[(2 * fac_len) - 1 - i];
606
102M
  }
607
608
14.6M
  for (i = 0; i < ORDER; i++) {
609
13.8M
    xn1[i] -= lpd_state->tcx_quant[1 + 128 - ORDER + i];
610
13.8M
    xn2[i] -= sq_gain * xn_buf[frame_len - ORDER + i];
611
13.8M
  }
612
613
102M
  for (i = 0; i < fac_len_prev; i++) {
614
102M
    facelp[i] = lpd_state->tcx_quant[1 + 128 + i] * fac_window[fac_len_prev + i] +
615
102M
                lpd_state->tcx_quant[1 + 128 - 1 - i] * fac_window[fac_len_prev - 1 - i];
616
102M
  }
617
618
864k
  energy = 0.0f;
619
102M
  for (i = 0; i < fac_len_prev; i++) energy += xn1[ORDER + i] * xn1[ORDER + i];
620
864k
  energy *= 2.0f;
621
864k
  tmp = 0.0f;
622
102M
  for (i = 0; i < fac_len_prev; i++) tmp += facelp[i] * facelp[i];
623
864k
  if (tmp > energy)
624
194k
    gain = (FLOAT32)sqrt(energy / tmp);
625
669k
  else
626
669k
    gain = 1.0f;
627
628
102M
  for (i = 0; i < fac_len_prev; i++) {
629
102M
    xn1[ORDER + i] -= gain * facelp[i];
630
102M
  }
631
632
864k
  iusace_get_weighted_lpc(lpc_coeffs_quant + (ORDER + 1), lp_flt_coeffs);
633
864k
  iusace_compute_lp_residual(lp_flt_coeffs, xn1 + ORDER, x1, fac_len_prev);
634
635
864k
  iusace_get_weighted_lpc(lpc_coeffs_quant + (2 * (ORDER + 1)), lp_flt_coeffs);
636
864k
  iusace_compute_lp_residual(lp_flt_coeffs, xn2 + ORDER, x2, fac_len);
637
638
864k
  iusace_tcx_mdct(x1, x1, fac_len_prev, pstr_scratch);
639
864k
  iusace_tcx_mdct(x2, x2, fac_len, pstr_scratch);
640
641
864k
  gain_prev = (FLOAT32)(sq_gain * 0.5f * sqrt(((FLOAT32)fac_len_prev) / (FLOAT32)frame_len));
642
864k
  gain_next = (FLOAT32)(sq_gain * 0.5f * sqrt(((FLOAT32)fac_len) / (FLOAT32)frame_len));
643
644
102M
  for (i = 0; i < fac_len_prev; i++) {
645
102M
    x1[i] /= gain_prev;
646
102M
  }
647
102M
  for (i = 0; i < fac_len; i++) {
648
102M
    x2[i] /= gain_next;
649
102M
  }
650
26.3M
  for (i = 0; i < fac_len_prev / 4; i++) {
651
25.5M
    k = i * lg / (8 * fac_len_prev);
652
25.5M
    x1[i] /= alfd_gains[k];
653
25.5M
  }
654
26.3M
  for (i = 0; i < fac_len / 4; i++) {
655
25.5M
    k = i * lg / (8 * fac_len);
656
25.5M
    x2[i] /= alfd_gains[k];
657
25.5M
  }
658
659
13.6M
  for (i = 0; i < fac_len; i += 8) {
660
12.7M
    iusace_find_nearest_neighbor(&x2[i], &y[i]);
661
12.7M
  }
662
102M
  for (i = 0; i < fac_len; i++) {
663
102M
    lpd_state->avq_params[i] = y[i];
664
102M
    x2[i] = (FLOAT32)y[i];
665
102M
  }
666
667
13.6M
  for (i = 0; i < fac_len_prev; i += 8) {
668
12.7M
    iusace_find_nearest_neighbor(&x1[i], &y[i]);
669
12.7M
  }
670
671
102M
  for (i = 0; i < fac_len_prev; i++) {
672
102M
    x1[i] = (FLOAT32)y[i];
673
102M
  }
674
675
864k
  gain_prev = (FLOAT32)(gain_tcx * 0.5f * sqrt(((FLOAT32)fac_len_prev) / (FLOAT32)frame_len));
676
864k
  gain_next = (FLOAT32)(gain_tcx * 0.5f * sqrt(((FLOAT32)fac_len) / (FLOAT32)frame_len));
677
678
102M
  for (i = 0; i < fac_len_prev; i++) {
679
102M
    x1[i] *= gain_prev;
680
102M
  }
681
102M
  for (i = 0; i < fac_len; i++) {
682
102M
    x2[i] *= gain_next;
683
102M
  }
684
26.3M
  for (i = 0; i<fac_len_prev>> 2; i++) {
685
25.5M
    k = i * lg / (fac_len_prev << 3);
686
25.5M
    x1[i] *= alfd_gains[k];
687
25.5M
  }
688
26.3M
  for (i = 0; i<fac_len>> 2; i++) {
689
25.5M
    k = i * lg / (fac_len << 3);
690
25.5M
    x2[i] *= alfd_gains[k];
691
25.5M
  }
692
864k
  iusace_tcx_mdct(x1, xn1, fac_len_prev, pstr_scratch);
693
864k
  iusace_tcx_mdct(x2, xn2, fac_len, pstr_scratch);
694
695
864k
  FLOAT32 coeff1 = (2.0f / (FLOAT32)fac_len_prev), coeff2 = (2.0f / (FLOAT32)fac_len);
696
697
102M
  for (i = 0; i < fac_len_prev; i++) {
698
102M
    xn1[i] = xn1[i] * coeff1;
699
102M
  }
700
701
102M
  for (i = 0; i < fac_len; i++) {
702
102M
    xn2[i] = xn2[i] * coeff2;
703
102M
  }
704
705
864k
  memset(xn1 + fac_len_prev, 0, fac_len_prev * sizeof(FLOAT32));
706
864k
  memset(xn2 + fac_len, 0, fac_len * sizeof(FLOAT32));
707
708
864k
  iusace_get_weighted_lpc(lpc_coeffs_quant + (ORDER + 1), lp_flt_coeffs);
709
864k
  iusace_synthesis_tool_float(lp_flt_coeffs, xn1, xn1, 2 * fac_len_prev, xn1 + fac_len_prev,
710
864k
                              pstr_scratch->p_buf_synthesis_tool);
711
712
864k
  iusace_get_weighted_lpc(lpc_coeffs_quant + (2 * (ORDER + 1)), lp_flt_coeffs);
713
864k
  iusace_synthesis_tool_float(lp_flt_coeffs, xn2, xn2, fac_len, xn2 + fac_len,
714
864k
                              pstr_scratch->p_buf_synthesis_tool);
715
716
102M
  for (i = 0; i < fac_len_prev; i++) {
717
102M
    xn1[i] += facelp[i];
718
102M
  }
719
720
555M
  for (i = 0; i < frame_len + (fac_length << 1); i++) {
721
554M
    xn_buf[i] *= gain_tcx;
722
554M
  }
723
724
864k
  if (lpd_state->mode >= -1) {
725
205M
    for (i = 0; i < (2 * fac_len_prev); i++) {
726
204M
      xn_buf[i + fac_length - fac_len_prev] *= sine_window_prev[i];
727
204M
    }
728
864k
    for (i = 0; i < fac_length - fac_len_prev; i++) {
729
0
      xn_buf[i] = 0.0f;
730
0
    }
731
864k
  }
732
205M
  for (i = 0; i < (2 * fac_len); i++) {
733
204M
    xn_buf[i + frame_len + fac_length - fac_len] *= sine_window[(2 * fac_len) - 1 - i];
734
204M
  }
735
864k
  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
864k
  if (lpd_state->mode != 0) {
740
131M
    for (i = 0; i < (2 * fac_length); i++) {
741
131M
      xn_buf[i] += lpd_state->tcx_quant[1 + 128 - fac_length + i];
742
131M
    }
743
744
551k
    mem_tcx_q = lpd_state->tcx_quant[128 - fac_length];
745
551k
  } else {
746
36.8M
    for (i = 0; i < fac_len_prev; i++) {
747
36.5M
      params[i - fac_len_prev] = y[i];
748
36.5M
    }
749
750
73.4M
    for (i = 0; i < (2 * fac_len_prev); i++) {
751
73.1M
      xn_buf[i + fac_length] += xn1[i];
752
73.1M
    }
753
312k
    mem_tcx_q = lpd_state->tcx_quant[128];
754
312k
  }
755
756
864k
  memcpy(lpd_state->tcx_quant, xn_buf + frame_len + fac_length - 128 - 1,
757
864k
         (1 + 256) * sizeof(FLOAT32));
758
759
102M
  for (i = 0; i < fac_len; i++) {
760
102M
    xn_buf[i + frame_len + (fac_length - fac_len)] += xn2[i];
761
102M
  }
762
763
864k
  if (lpd_state->mode > 0) {
764
540k
    iusace_apply_preemph(xn_buf, TILT_FAC, fac_length, &mem_tcx_q);
765
766
540k
    ptr_lp_flt_coeffs = lpd_state->lpc_coeffs_quant;
767
768
540k
    TTT = fac_length % LEN_SUBFR;
769
540k
    if (TTT != 0) {
770
157k
      memcpy(&(lpd_state->synth[ORDER + 128 - fac_length]), &xn_buf[0], TTT * sizeof(FLOAT32));
771
157k
      iusace_compute_lp_residual(ptr_lp_flt_coeffs, &(lpd_state->synth[ORDER + 128 - fac_length]),
772
157k
                                 &(lpd_state->acelp_exc[(2 * len_subfrm) - fac_length]), TTT);
773
774
157k
      ptr_lp_flt_coeffs += (ORDER + 1);
775
157k
    }
776
777
1.46M
    for (i_subfr = TTT; i_subfr < fac_length; i_subfr += LEN_SUBFR) {
778
923k
      memcpy(&(lpd_state->synth[ORDER + 128 - fac_length + i_subfr]), &xn_buf[i_subfr],
779
923k
             LEN_SUBFR * sizeof(FLOAT32));
780
923k
      iusace_compute_lp_residual(
781
923k
          ptr_lp_flt_coeffs, &(lpd_state->synth[ORDER + 128 - fac_length + i_subfr]),
782
923k
          &(lpd_state->acelp_exc[(2 * len_subfrm) - fac_length + i_subfr]), LEN_SUBFR);
783
923k
      ptr_lp_flt_coeffs += (ORDER + 1);
784
923k
    }
785
786
540k
    ptr_lp_flt_coeffs = lpd_state->lpc_coeffs;
787
1.62M
    for (i_subfr = 0; i_subfr < fac_length; i_subfr += LEN_SUBFR) {
788
1.08M
      iusace_get_weighted_lpc(ptr_lp_flt_coeffs, lp_flt_coeffs);
789
1.08M
      iusace_compute_lp_residual(lp_flt_coeffs,
790
1.08M
                                 &(lpd_state->synth[ORDER + 128 - fac_length + i_subfr]),
791
1.08M
                                 &(lpd_state->wsynth[1 + 128 - fac_length + i_subfr]), LEN_SUBFR);
792
1.08M
      ptr_lp_flt_coeffs += (ORDER + 1);
793
1.08M
    }
794
540k
    tmp = lpd_state->wsynth[0 + 128 - fac_length];
795
540k
    iusace_apply_deemph(&(lpd_state->wsynth[1 + 128 - fac_length]), TILT_FAC, fac_length, &tmp);
796
540k
  }
797
798
864k
  k = ((frame_len / LEN_SUBFR) - 2) * (ORDER + 1);
799
864k
  memcpy(lpd_state->lpc_coeffs, lpc_coeffs + k, 2 * (ORDER + 1) * sizeof(FLOAT32));
800
801
864k
  memcpy(lpd_state->lpc_coeffs_quant, lpc_coeffs_quant + (2 * (ORDER + 1)),
802
864k
         (ORDER + 1) * sizeof(FLOAT32));
803
864k
  memcpy(lpd_state->lpc_coeffs_quant + (ORDER + 1), lpd_state->lpc_coeffs_quant,
804
864k
         (ORDER + 1) * sizeof(FLOAT32));
805
806
864k
  memcpy(synth - 128, &(lpd_state->synth[ORDER]), 128 * sizeof(FLOAT32));
807
864k
  lpd_state->tcx_fac = xn[frame_len - 1];
808
809
864k
  iusace_apply_preemph(xn, TILT_FAC, frame_len, &mem_tcx_q);
810
6.33M
  for (i_subfr = 0; i_subfr < frame_len; i_subfr += LEN_SUBFR) {
811
5.46M
    memcpy(&synth[i_subfr], &xn[i_subfr], LEN_SUBFR * sizeof(FLOAT32));
812
5.46M
    iusace_compute_lp_residual(lpc_coeffs_quant + (2 * (ORDER + 1)), &synth[i_subfr],
813
5.46M
                               &xn[i_subfr], LEN_SUBFR);
814
5.46M
  }
815
864k
  memcpy(lpd_state->synth, synth + frame_len - (ORDER + 128), (ORDER + 128) * sizeof(FLOAT32));
816
817
864k
  if (frame_len == len_subfrm) {
818
493k
    memcpy(x, lpd_state->acelp_exc + len_subfrm, len_subfrm * sizeof(FLOAT32));
819
493k
    memcpy(lpd_state->acelp_exc, x, len_subfrm * sizeof(FLOAT32));
820
493k
    memcpy(lpd_state->acelp_exc + len_subfrm, xn, len_subfrm * sizeof(FLOAT32));
821
493k
  } else {
822
370k
    memcpy(lpd_state->acelp_exc, xn + frame_len - (2 * len_subfrm),
823
370k
           2 * len_subfrm * sizeof(FLOAT32));
824
370k
  }
825
826
864k
  memcpy(wsynth - 128, &(lpd_state->wsynth[1]), 128 * sizeof(FLOAT32));
827
828
864k
  ptr_lp_flt_coeffs = lpc_coeffs;
829
6.33M
  for (i_subfr = 0; i_subfr < frame_len; i_subfr += LEN_SUBFR) {
830
5.46M
    iusace_get_weighted_lpc(ptr_lp_flt_coeffs, lp_flt_coeffs);
831
5.46M
    iusace_compute_lp_residual(lp_flt_coeffs, &synth[i_subfr], &wsynth[i_subfr], LEN_SUBFR);
832
5.46M
    ptr_lp_flt_coeffs += (ORDER + 1);
833
5.46M
  }
834
864k
  tmp = wsynth[-1];
835
864k
  iusace_apply_deemph(wsynth, TILT_FAC, frame_len, &tmp);
836
837
864k
  memcpy(lpd_state->wsynth, wsynth + frame_len - (1 + 128), (1 + 128) * sizeof(FLOAT32));
838
839
864k
  lpd_state->mode = mode;
840
841
864k
  lpd_state->num_bits = 10 + target_bits;
842
843
864k
  return;
844
864k
}