Coverage Report

Created: 2026-03-31 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libxaac/decoder/ixheaacd_tcx_fwd_alcnx.c
Line
Count
Source
1
/******************************************************************************
2
 *                                                                            *
3
 * Copyright (C) 2018 The Android Open Source Project
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at:
8
 *
9
 * http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 *
17
 *****************************************************************************
18
 * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
19
*/
20
#include <float.h>
21
#include <stdlib.h>
22
#include <stdio.h>
23
#include <math.h>
24
#include <string.h>
25
#include "ixheaac_type_def.h"
26
#include "ixheaacd_bitbuffer.h"
27
28
#include "ixheaacd_interface.h"
29
30
#include "ixheaacd_tns_usac.h"
31
#include "ixheaacd_cnst.h"
32
33
#include "ixheaacd_acelp_info.h"
34
35
#include "ixheaacd_td_mdct.h"
36
37
#include "ixheaacd_sbrdecsettings.h"
38
#include "ixheaacd_info.h"
39
#include "ixheaacd_sbr_common.h"
40
#include "ixheaacd_drc_data_struct.h"
41
#include "ixheaacd_drc_dec.h"
42
#include "ixheaacd_sbrdecoder.h"
43
#include "ixheaacd_mps_polyphase.h"
44
#include "ixheaac_sbr_const.h"
45
46
#include "ixheaacd_ec_defines.h"
47
#include "ixheaacd_ec_struct_def.h"
48
#include "ixheaacd_main.h"
49
#include "ixheaacd_arith_dec.h"
50
#include "ixheaacd_func_def.h"
51
#include "ixheaacd_windows.h"
52
#include "ixheaacd_acelp_com.h"
53
54
#include "ixheaac_constants.h"
55
#include "ixheaac_basic_ops32.h"
56
#include "ixheaac_basic_ops40.h"
57
58
53.0M
#define ABS(A) ((A) < 0 ? (-A) : (A))
59
60
324k
VOID ixheaacd_lpc_coeff_wt_apply(FLOAT32 *a, FLOAT32 *ap) {
61
324k
  FLOAT32 f;
62
324k
  WORD32 i;
63
324k
  ap[0] = a[0];
64
324k
  f = 0.92f;
65
5.51M
  for (i = 1; i <= 16; i++) {
66
5.19M
    ap[i] = f * a[i];
67
5.19M
    f *= 0.92f;
68
5.19M
  }
69
324k
  return;
70
324k
}
71
72
254k
WORD8 ixheaacd_float2fix(FLOAT32 *x, WORD32 *int_x, WORD32 length) {
73
254k
  WORD32 k, itemp;
74
254k
  FLOAT32 ftemp = 0.0;
75
254k
  WORD8 shiftp;
76
52.3M
  for (k = 0; k < length; k++) {
77
52.0M
    if (ABS(x[k]) > ftemp) ftemp = ABS(x[k]);
78
52.0M
  }
79
80
254k
  itemp = (WORD32)(ftemp);
81
254k
  shiftp = ixheaac_norm32(itemp);
82
83
52.3M
  for (k = 0; k < length; k++) {
84
52.0M
    int_x[k] = (WORD32)(x[k] * (FLOAT32)((WORD64)1 << shiftp));
85
52.0M
  }
86
87
254k
  return (shiftp);
88
254k
}
89
90
VOID ixheaacd_fix2float(WORD32 *int_xn1, FLOAT32 *xn1, WORD32 length,
91
254k
                        WORD8 *shiftp, WORD32 *preshift) {
92
254k
  WORD32 k;
93
254k
  FLOAT32 qfac;
94
254k
  if ((*shiftp - *preshift) > 0) {
95
254k
    qfac = 1.0f / (FLOAT32)((WORD64)1 << (*shiftp - *preshift));
96
80.5M
    for (k = 0; k < length; k++) {
97
80.2M
      xn1[k] = (FLOAT32)((FLOAT32)int_xn1[k] * qfac);
98
80.2M
    }
99
254k
  } else {
100
120k
    for (k = 0; k < length; k++) {
101
120k
      xn1[k] = (FLOAT32)((FLOAT32)int_xn1[k] *
102
120k
                         (FLOAT32)((WORD64)1 << (*preshift - *shiftp)));
103
120k
    }
104
664
  }
105
254k
}
106
107
static VOID ixheaacd_low_fq_deemphasis(FLOAT32 x[], WORD32 lg,
108
121k
                                       FLOAT32 gains[]) {
109
121k
  WORD32 i, j, k, i_max;
110
121k
  FLOAT32 max, factor, rm;
111
112
121k
  k = 8;
113
121k
  i_max = lg / 4;
114
115
121k
  max = 0.01f;
116
1.26M
  for (i = 0; i < i_max; i += k) {
117
1.14M
    rm = 0.01f;
118
10.2M
    for (j = i; j < i + k; j++) rm += x[j] * x[j];
119
120
1.14M
    if (rm > max) max = rm;
121
1.14M
  }
122
123
121k
  factor = 0.1f;
124
1.26M
  for (i = 0; i < i_max; i += k) {
125
1.14M
    rm = 0.01f;
126
10.2M
    for (j = i; j < i + k; j++) rm += x[j] * x[j];
127
128
1.14M
    rm = (FLOAT32)sqrt(rm / max);
129
1.14M
    if (rm > factor) factor = rm;
130
131
10.2M
    for (j = i; j < i + k; j++) x[j] *= factor;
132
133
1.14M
    gains[i / k] = factor;
134
1.14M
  }
135
136
121k
  return;
137
121k
}
138
139
0
static WORD32 ixheaacd_calc_max_pitch(FLOAT32 x[LEN_SUPERFRAME], WORD32 n) {
140
0
  FLOAT32 max_m;
141
0
  FLOAT32 t_est;
142
0
  WORD32 i, i_max, pitch_tcx;
143
144
0
  max_m = 0;
145
0
  i_max = 1;
146
147
0
  for (i = 1; i < n; i++) {
148
0
    FLOAT32 mag = (x[2 * i] * x[2 * i]) + (x[2 * i + 1] * x[2 * i + 1]);
149
0
    if (mag > max_m) {
150
0
      max_m = mag;
151
0
      i_max = i;
152
0
    }
153
0
  }
154
155
0
  t_est = (n / (FLOAT32)i_max);
156
157
0
  if (t_est >= 256) {
158
0
    pitch_tcx = 256;
159
0
  } else {
160
0
    FLOAT32 tmp_est = t_est;
161
0
    while (tmp_est < 256) {
162
0
      tmp_est += t_est;
163
0
    }
164
0
    pitch_tcx = (WORD32)(tmp_est - t_est);
165
0
  }
166
167
0
  return (pitch_tcx);
168
0
}
169
170
WORD32 ixheaacd_tcx_mdct(ia_usac_data_struct *usac_data,
171
                         ia_td_frame_data_struct *pstr_td_frame_data,
172
                         WORD32 frame_index, FLOAT32 lp_flt_coff_a[], WORD32 lg,
173
121k
                         ia_usac_lpd_decoder_handle st) {
174
121k
  WORD32 i, mode;
175
121k
  WORD32 *ptr_tcx_quant;
176
121k
  FLOAT32 tmp, gain_tcx = 0.0f, noise_level, energy, temp;
177
121k
  FLOAT32 *ptr_a, i_ap[ORDER + 1];
178
121k
  const FLOAT32 *sine_window_prev, *sine_window;
179
121k
  WORD32 fac_length_prev;
180
121k
  FLOAT32 alfd_gains[LEN_SUPERFRAME / (4 * 8)] = {0};
181
121k
  FLOAT32 x[LEN_SUPERFRAME], buf[ORDER + LEN_SUPERFRAME];
182
121k
  WORD32 int_x[LEN_SUPERFRAME + (2 * FAC_LENGTH)];
183
121k
  WORD32 int_xn1[LEN_SUPERFRAME + (2 * FAC_LENGTH)];
184
121k
  FLOAT32 gain1[LEN_SUPERFRAME], gain2[LEN_SUPERFRAME];
185
121k
  FLOAT32 xn_buf[LEN_SUPERFRAME + (2 * FAC_LENGTH)];
186
121k
  FLOAT32 *xn;
187
121k
  FLOAT32 xn1[2 * FAC_LENGTH], facwindow[2 * FAC_LENGTH];
188
121k
  WORD32 TTT;
189
121k
  WORD8 shiftp = 0;
190
121k
  WORD32 preshift = 0;
191
121k
  WORD32 loop_count = 0;
192
121k
  FLOAT32 *exc = &usac_data->exc_buf[usac_data->len_subfrm * frame_index +
193
121k
                                     MAX_PITCH + INTER_LP_FIL_ORDER + 1];
194
121k
  FLOAT32 *synth =
195
121k
      &usac_data->synth_buf[usac_data->len_subfrm * frame_index + MAX_PITCH +
196
121k
                            (((NUM_FRAMES * usac_data->num_subfrm) / 2) - 1) *
197
121k
                                LEN_SUBFR];
198
199
121k
  WORD32 *ptr_scratch = &usac_data->scratch_buffer[0];
200
201
121k
  WORD32 fac_length = (usac_data->len_subfrm) / 2;
202
121k
  WORD32 err = 0;
203
204
121k
  mode = lg / (usac_data->len_subfrm);
205
121k
  if (mode > 2) mode = 3;
206
207
121k
  if (st->mode_prev == -2)
208
4.31k
    fac_length_prev = (usac_data->ccfl) / 16;
209
210
117k
  else
211
117k
    fac_length_prev = fac_length;
212
213
121k
  if (fac_length == 96)
214
43.4k
    sine_window = ixheaacd_sine_window192;
215
77.8k
  else
216
77.8k
    sine_window = ixheaacd__sine_window256;
217
218
121k
  if (fac_length_prev == 48)
219
914
    sine_window_prev = ixheaacd_sine_window96;
220
221
120k
  else if (fac_length_prev == 64)
222
3.40k
    sine_window_prev = ixheaacd_sine_window128;
223
224
117k
  else if (fac_length_prev == 96)
225
42.5k
    sine_window_prev = ixheaacd_sine_window192;
226
227
74.4k
  else
228
74.4k
    sine_window_prev = ixheaacd__sine_window256;
229
230
121k
  xn = xn_buf + fac_length;
231
232
121k
  if (st->mode_prev != 0) {
233
57.6k
    if (st->mode_prev > 0) {
234
11.0M
      for (i = 0; i < (2 * fac_length_prev); i++) {
235
11.0M
        st->exc_prev[i + fac_length - fac_length_prev + 1] *=
236
11.0M
            sine_window_prev[(2 * fac_length_prev) - 1 - i];
237
11.0M
      }
238
47.5k
    }
239
319k
    for (i = 0; i < fac_length - fac_length_prev; i++) {
240
261k
      st->exc_prev[i + fac_length + fac_length_prev + 1] = 0.0f;
241
261k
    }
242
57.6k
  }
243
121k
  if (usac_data->frame_ok == 1) {
244
121k
    noise_level = 0.0625f * (8.0f - ((FLOAT32)pstr_td_frame_data->noise_factor[frame_index]));
245
246
121k
  ptr_tcx_quant = pstr_td_frame_data->x_tcx_invquant;
247
260k
  for (i = 0; i < frame_index; i++)
248
139k
    ptr_tcx_quant += pstr_td_frame_data->tcx_lg[i];
249
250
36.7M
  for (i = 0; i < lg; i++) x[i] = (FLOAT32)ptr_tcx_quant[i];
251
252
121k
    if (usac_data->ec_flag) {
253
0
      st->last_tcx_pitch = ixheaacd_calc_max_pitch(x, (lg >> 5));
254
0
    }
255
256
3.96M
  for (i = lg / 6; i < lg; i += 8) {
257
3.84M
    WORD32 k, max_k = min(lg, i + 8);
258
3.84M
    FLOAT32 tmp = 0.0f;
259
34.3M
    for (k = i; k < max_k; k++) tmp += ptr_tcx_quant[k] * ptr_tcx_quant[k];
260
261
3.84M
    if (tmp == 0.0f) {
262
15.9M
      for (k = i; k < max_k; k++)
263
14.1M
        x[k] = noise_level *
264
14.1M
               ixheaacd_randomsign(
265
14.1M
                   &(usac_data->seed_value[usac_data->present_chan]));
266
1.79M
    }
267
3.84M
  }
268
269
121k
  ixheaacd_low_fq_deemphasis(x, lg, alfd_gains);
270
271
121k
  ixheaacd_lpc_coeff_wt_apply(lp_flt_coff_a + (ORDER + 1), i_ap);
272
121k
  ixheaacd_lpc_to_td(i_ap, ORDER, gain1, usac_data->len_subfrm / 4);
273
274
121k
  ixheaacd_lpc_coeff_wt_apply(lp_flt_coff_a + (2 * (ORDER + 1)), i_ap);
275
121k
  ixheaacd_lpc_to_td(i_ap, ORDER, gain2, usac_data->len_subfrm / 4);
276
277
121k
  energy = 0.01f;
278
36.7M
  for (i = 0; i < lg; i++) energy += x[i] * x[i];
279
280
121k
  temp = (FLOAT32)sqrt(energy) / lg;
281
282
121k
  gain_tcx =
283
121k
      (FLOAT32)pow(
284
121k
          10.0f,
285
121k
          ((FLOAT32)pstr_td_frame_data->global_gain[frame_index]) / 28.0f) /
286
121k
      (temp * 2.0f);
287
121k
  }
288
121k
  if (usac_data->ec_flag) {
289
0
    if (usac_data->frame_ok == 1) {
290
0
      usac_data->past_gain_tcx[usac_data->present_chan] = gain_tcx;
291
0
    } else {
292
0
      gain_tcx = usac_data->past_gain_tcx[usac_data->present_chan];
293
0
    }
294
0
  }
295
121k
  if (usac_data->frame_ok == 1) {
296
121k
  ixheaacd_noise_shaping(x, lg, (usac_data->len_subfrm) / 4, gain1, gain2);
297
121k
  shiftp = ixheaacd_float2fix(x, int_x, lg);
298
121k
  }
299
121k
  if (usac_data->ec_flag == 1) {
300
0
    if (st->mode_prev != 0) {
301
0
      if (usac_data->frame_ok == 1) {
302
0
        memcpy(usac_data->tcx_spec_coeffs[usac_data->present_chan], int_x, lg * sizeof(int_x[0]));
303
0
        usac_data->last_shiftp = shiftp;
304
0
      } else {
305
0
        memcpy(int_x, usac_data->tcx_spec_coeffs[usac_data->present_chan], lg * sizeof(int_x[0]));
306
0
        shiftp = usac_data->last_shiftp;
307
0
      }
308
0
    }
309
121k
  } else {
310
121k
    if (lg & (lg - 1)) {
311
43.4k
      if ((lg != 48) && (lg != 96) && (lg != 192) && (lg != 384) && (lg != 768)) {
312
0
        return -1;
313
0
      }
314
43.4k
    }
315
121k
  }
316
317
121k
  ixheaacd_acelp_mdct_main(usac_data, int_x, int_xn1, (2 * fac_length), lg - (2 * fac_length),
318
121k
                           &preshift);
319
320
121k
  ixheaacd_fix2float(int_xn1, xn_buf, (lg + (2 * fac_length)), &shiftp,
321
121k
                     &preshift);
322
323
121k
  ixheaacd_vec_cnst_mul((2.0f / lg), xn_buf, xn_buf, lg + (2 * fac_length));
324
325
121k
  st->fac_gain =
326
121k
      gain_tcx * 0.5f * (FLOAT32)sqrt(((FLOAT32)fac_length) / (FLOAT32)lg);
327
328
3.65M
  for (i = 0; i < fac_length / 4; i++)
329
3.53M
    st->fac_fd_data[i] = alfd_gains[i * lg / (8 * fac_length)];
330
331
121k
  if (st->mode_prev == 0) {
332
7.46M
    for (i = 0; i < fac_length_prev; i++) {
333
7.40M
      facwindow[i] =
334
7.40M
          sine_window_prev[i] * sine_window_prev[(2 * fac_length_prev) - 1 - i];
335
7.40M
      facwindow[fac_length_prev + i] =
336
7.40M
          1.0f - (sine_window_prev[fac_length_prev + i] *
337
7.40M
                  sine_window_prev[fac_length_prev + i]);
338
7.40M
    }
339
340
3.76M
    for (i = 0; i < fac_length / 2; i++) {
341
3.70M
      x[i] = st->fac_gain *
342
3.70M
             (FLOAT32)pstr_td_frame_data->fac[frame_index * FAC_LENGTH + 2 * i];
343
3.70M
      x[fac_length / 2 + i] =
344
3.70M
          st->fac_gain *
345
3.70M
          (FLOAT32)pstr_td_frame_data
346
3.70M
              ->fac[frame_index * FAC_LENGTH + fac_length - 2 * i - 1];
347
3.70M
    }
348
349
989k
    for (i = 0; i < fac_length / 8; i++) {
350
925k
      x[i] *= st->fac_fd_data[2 * i];
351
925k
      x[fac_length - i - 1] *= st->fac_fd_data[2 * i + 1];
352
925k
    }
353
354
63.7k
    preshift = 0;
355
63.7k
    shiftp = ixheaacd_float2fix(x, int_x, fac_length);
356
357
63.7k
    if (usac_data->ec_flag == 0) {
358
63.7k
      if (fac_length & (fac_length - 1)) {
359
23.7k
        if ((fac_length != 48) && (fac_length != 96) && (fac_length != 192) &&
360
0
            (fac_length != 384) && (fac_length != 768)) {
361
0
          return -1;
362
0
        }
363
23.7k
      }
364
63.7k
    }
365
366
63.7k
    ixheaacd_acelp_mdct(int_x, int_xn1, &preshift, fac_length, ptr_scratch);
367
368
63.7k
    ixheaacd_fix2float(int_xn1, xn1, fac_length, &shiftp, &preshift);
369
370
63.7k
    ixheaacd_vec_cnst_mul((2.0f / (FLOAT32)fac_length), xn1, xn1, fac_length);
371
372
63.7k
    memset(xn1 + fac_length, 0, fac_length * sizeof(FLOAT32));
373
374
63.7k
    ixheaacd_lpc_coeff_wt_apply(lp_flt_coff_a + (ORDER + 1), i_ap);
375
63.7k
    ixheaacd_synthesis_tool_float(i_ap, xn1, xn1, 2 * fac_length,
376
63.7k
                                  xn1 + fac_length);
377
378
7.46M
    for (i = 0; i < fac_length; i++) {
379
7.40M
      temp = st->exc_prev[1 + fac_length + i] * facwindow[fac_length + i] +
380
7.40M
             st->exc_prev[fac_length - i] * facwindow[fac_length - 1 - i];
381
7.40M
      xn1[i] += temp;
382
7.40M
    }
383
63.7k
  }
384
385
65.0M
  for (i = 0; i < lg + (2 * fac_length); i++) xn_buf[i] *= gain_tcx;
386
387
27.8M
  for (i = 0; i < (2 * fac_length_prev); i++)
388
27.7M
    xn_buf[i + fac_length - fac_length_prev] *= sine_window_prev[i];
389
390
382k
  for (i = 0; i < fac_length - fac_length_prev; i++) xn_buf[i] = 0.0f;
391
392
121k
  if (st->mode_prev != 0) {
393
13.0M
    for (i = fac_length - fac_length_prev; i < (fac_length + fac_length_prev);
394
12.9M
         i++)
395
12.9M
      xn_buf[i] += st->exc_prev[1 + i];
396
63.7k
  } else {
397
14.8M
    for (i = fac_length - fac_length_prev; i < (fac_length + fac_length_prev);
398
14.8M
         i++)
399
14.8M
      xn_buf[i + fac_length] += xn1[i];
400
63.7k
  }
401
402
121k
  ixheaacd_mem_cpy(xn_buf + lg - 1, st->exc_prev, 1 + (2 * fac_length));
403
404
28.4M
  for (i = 0; i < (2 * fac_length); i++) {
405
28.2M
    xn_buf[i + lg] *= sine_window[(2 * fac_length) - 1 - i];
406
28.2M
  }
407
408
121k
  if (st->mode_prev != 0) {
409
57.6k
    ixheaacd_mem_cpy(xn_buf + fac_length - fac_length_prev,
410
57.6k
                     synth - fac_length_prev, fac_length_prev);
411
412
7.72M
    for (i = 0; i < ORDER + fac_length; i++)
413
7.66M
      buf[i] = synth[i - ORDER - fac_length] -
414
7.66M
               (PREEMPH_FILT_FAC * synth[i - ORDER - fac_length - 1]);
415
416
57.6k
    ptr_a = st->lp_flt_coeff_a_prev;
417
57.6k
    TTT = fac_length % LEN_SUBFR;
418
57.6k
    if (TTT != 0)
419
19.7k
      ixheaacd_residual_tool_float(ptr_a, &buf[ORDER], &exc[-fac_length], TTT,
420
19.7k
                                   1);
421
422
57.6k
    loop_count = (fac_length - TTT) / LEN_SUBFR;
423
57.6k
    ixheaacd_residual_tool_float(ptr_a, &buf[ORDER + TTT],
424
57.6k
                                 &exc[TTT - fac_length], LEN_SUBFR, loop_count);
425
57.6k
  }
426
427
121k
  ixheaacd_mem_cpy(xn, synth, lg);
428
429
121k
  ixheaacd_mem_cpy(synth - ORDER - 1, xn - ORDER - 1, ORDER + 1);
430
121k
  tmp = xn[-ORDER - 1];
431
121k
  ixheaacd_preemphsis_tool_float(xn - ORDER, PREEMPH_FILT_FAC, ORDER + lg, tmp);
432
433
121k
  ptr_a = lp_flt_coff_a + (2 * (ORDER + 1));
434
435
121k
  ixheaacd_residual_tool_float(ptr_a, xn, exc, lg, 1);
436
437
121k
  ixheaacd_mem_cpy(ptr_a, st->lp_flt_coeff_a_prev, ORDER + 1);
438
121k
  ixheaacd_mem_cpy(ptr_a, st->lp_flt_coeff_a_prev + ORDER + 1, ORDER + 1);
439
440
121k
  return err;
441
121k
}
442
443
14.1M
FLOAT32 ixheaacd_randomsign(UWORD32 *seed) {
444
14.1M
  FLOAT32 sign = 0.0f;
445
14.1M
  *seed = (UWORD32)(((UWORD64)(*seed) * (UWORD64)69069) + 5);
446
447
14.1M
  if (((*seed) & 0x10000) > 0)
448
7.16M
    sign = -1.f;
449
7.00M
  else
450
7.00M
    sign = +1.f;
451
452
14.1M
  return sign;
453
14.1M
}