Coverage Report

Created: 2025-11-09 07:01

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
54.0M
#define ABS(A) ((A) < 0 ? (-A) : (A))
59
60
332k
VOID ixheaacd_lpc_coeff_wt_apply(FLOAT32 *a, FLOAT32 *ap) {
61
332k
  FLOAT32 f;
62
332k
  WORD32 i;
63
332k
  ap[0] = a[0];
64
332k
  f = 0.92f;
65
5.64M
  for (i = 1; i <= 16; i++) {
66
5.31M
    ap[i] = f * a[i];
67
5.31M
    f *= 0.92f;
68
5.31M
  }
69
332k
  return;
70
332k
}
71
72
264k
WORD8 ixheaacd_float2fix(FLOAT32 *x, WORD32 *int_x, WORD32 length) {
73
264k
  WORD32 k, itemp;
74
264k
  FLOAT32 ftemp = 0.0;
75
264k
  WORD8 shiftp;
76
53.3M
  for (k = 0; k < length; k++) {
77
53.0M
    if (ABS(x[k]) > ftemp) ftemp = ABS(x[k]);
78
53.0M
  }
79
80
264k
  itemp = (WORD32)(ftemp);
81
264k
  shiftp = ixheaac_norm32(itemp);
82
83
53.3M
  for (k = 0; k < length; k++) {
84
53.0M
    int_x[k] = (WORD32)(x[k] * (FLOAT32)((WORD64)1 << shiftp));
85
53.0M
  }
86
87
264k
  return (shiftp);
88
264k
}
89
90
VOID ixheaacd_fix2float(WORD32 *int_xn1, FLOAT32 *xn1, WORD32 length,
91
264k
                        WORD8 *shiftp, WORD32 *preshift) {
92
264k
  WORD32 k;
93
264k
  FLOAT32 qfac;
94
264k
  if ((*shiftp - *preshift) > 0) {
95
263k
    qfac = 1.0f / (FLOAT32)((WORD64)1 << (*shiftp - *preshift));
96
82.3M
    for (k = 0; k < length; k++) {
97
82.1M
      xn1[k] = (FLOAT32)((FLOAT32)int_xn1[k] * qfac);
98
82.1M
    }
99
263k
  } else {
100
133k
    for (k = 0; k < length; k++) {
101
133k
      xn1[k] = (FLOAT32)((FLOAT32)int_xn1[k] *
102
133k
                         (FLOAT32)((WORD64)1 << (*preshift - *shiftp)));
103
133k
    }
104
799
  }
105
264k
}
106
107
static VOID ixheaacd_low_fq_deemphasis(FLOAT32 x[], WORD32 lg,
108
124k
                                       FLOAT32 gains[]) {
109
124k
  WORD32 i, j, k, i_max;
110
124k
  FLOAT32 max, factor, rm;
111
112
124k
  k = 8;
113
124k
  i_max = lg / 4;
114
115
124k
  max = 0.01f;
116
1.27M
  for (i = 0; i < i_max; i += k) {
117
1.15M
    rm = 0.01f;
118
10.3M
    for (j = i; j < i + k; j++) rm += x[j] * x[j];
119
120
1.15M
    if (rm > max) max = rm;
121
1.15M
  }
122
123
124k
  factor = 0.1f;
124
1.27M
  for (i = 0; i < i_max; i += k) {
125
1.15M
    rm = 0.01f;
126
10.3M
    for (j = i; j < i + k; j++) rm += x[j] * x[j];
127
128
1.15M
    rm = (FLOAT32)sqrt(rm / max);
129
1.15M
    if (rm > factor) factor = rm;
130
131
10.3M
    for (j = i; j < i + k; j++) x[j] *= factor;
132
133
1.15M
    gains[i / k] = factor;
134
1.15M
  }
135
136
124k
  return;
137
124k
}
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
124k
                         ia_usac_lpd_decoder_handle st) {
174
124k
  WORD32 i, mode;
175
124k
  WORD32 *ptr_tcx_quant;
176
124k
  FLOAT32 tmp, gain_tcx = 0.0f, noise_level, energy, temp;
177
124k
  FLOAT32 *ptr_a, i_ap[ORDER + 1];
178
124k
  const FLOAT32 *sine_window_prev, *sine_window;
179
124k
  WORD32 fac_length_prev;
180
124k
  FLOAT32 alfd_gains[LEN_SUPERFRAME / (4 * 8)] = {0};
181
124k
  FLOAT32 x[LEN_SUPERFRAME], buf[ORDER + LEN_SUPERFRAME];
182
124k
  WORD32 int_x[LEN_SUPERFRAME + (2 * FAC_LENGTH)];
183
124k
  WORD32 int_xn1[LEN_SUPERFRAME + (2 * FAC_LENGTH)];
184
124k
  FLOAT32 gain1[LEN_SUPERFRAME], gain2[LEN_SUPERFRAME];
185
124k
  FLOAT32 xn_buf[LEN_SUPERFRAME + (2 * FAC_LENGTH)];
186
124k
  FLOAT32 *xn;
187
124k
  FLOAT32 xn1[2 * FAC_LENGTH], facwindow[2 * FAC_LENGTH];
188
124k
  WORD32 TTT;
189
124k
  WORD8 shiftp = 0;
190
124k
  WORD32 preshift = 0;
191
124k
  WORD32 loop_count = 0;
192
124k
  FLOAT32 *exc = &usac_data->exc_buf[usac_data->len_subfrm * frame_index +
193
124k
                                     MAX_PITCH + INTER_LP_FIL_ORDER + 1];
194
124k
  FLOAT32 *synth =
195
124k
      &usac_data->synth_buf[usac_data->len_subfrm * frame_index + MAX_PITCH +
196
124k
                            (((NUM_FRAMES * usac_data->num_subfrm) / 2) - 1) *
197
124k
                                LEN_SUBFR];
198
199
124k
  WORD32 *ptr_scratch = &usac_data->scratch_buffer[0];
200
201
124k
  WORD32 fac_length = (usac_data->len_subfrm) / 2;
202
124k
  WORD32 err = 0;
203
204
124k
  mode = lg / (usac_data->len_subfrm);
205
124k
  if (mode > 2) mode = 3;
206
207
124k
  if (st->mode_prev == -2)
208
5.60k
    fac_length_prev = (usac_data->ccfl) / 16;
209
210
118k
  else
211
118k
    fac_length_prev = fac_length;
212
213
124k
  if (fac_length == 96)
214
41.7k
    sine_window = ixheaacd_sine_window192;
215
82.7k
  else
216
82.7k
    sine_window = ixheaacd__sine_window256;
217
218
124k
  if (fac_length_prev == 48)
219
931
    sine_window_prev = ixheaacd_sine_window96;
220
221
123k
  else if (fac_length_prev == 64)
222
4.67k
    sine_window_prev = ixheaacd_sine_window128;
223
224
118k
  else if (fac_length_prev == 96)
225
40.7k
    sine_window_prev = ixheaacd_sine_window192;
226
227
78.0k
  else
228
78.0k
    sine_window_prev = ixheaacd__sine_window256;
229
230
124k
  xn = xn_buf + fac_length;
231
232
124k
  if (st->mode_prev != 0) {
233
58.9k
    if (st->mode_prev > 0) {
234
10.7M
      for (i = 0; i < (2 * fac_length_prev); i++) {
235
10.6M
        st->exc_prev[i + fac_length - fac_length_prev + 1] *=
236
10.6M
            sine_window_prev[(2 * fac_length_prev) - 1 - i];
237
10.6M
      }
238
45.2k
    }
239
402k
    for (i = 0; i < fac_length - fac_length_prev; i++) {
240
343k
      st->exc_prev[i + fac_length + fac_length_prev + 1] = 0.0f;
241
343k
    }
242
58.9k
  }
243
124k
  if (usac_data->frame_ok == 1) {
244
124k
    noise_level = 0.0625f * (8.0f - ((FLOAT32)pstr_td_frame_data->noise_factor[frame_index]));
245
246
124k
  ptr_tcx_quant = pstr_td_frame_data->x_tcx_invquant;
247
256k
  for (i = 0; i < frame_index; i++)
248
131k
    ptr_tcx_quant += pstr_td_frame_data->tcx_lg[i];
249
250
36.9M
  for (i = 0; i < lg; i++) x[i] = (FLOAT32)ptr_tcx_quant[i];
251
252
124k
    if (usac_data->ec_flag) {
253
0
      st->last_tcx_pitch = ixheaacd_calc_max_pitch(x, (lg >> 5));
254
0
    }
255
256
3.99M
  for (i = lg / 6; i < lg; i += 8) {
257
3.87M
    WORD32 k, max_k = min(lg, i + 8);
258
3.87M
    FLOAT32 tmp = 0.0f;
259
34.6M
    for (k = i; k < max_k; k++) tmp += ptr_tcx_quant[k] * ptr_tcx_quant[k];
260
261
3.87M
    if (tmp == 0.0f) {
262
16.1M
      for (k = i; k < max_k; k++)
263
14.3M
        x[k] = noise_level *
264
14.3M
               ixheaacd_randomsign(
265
14.3M
                   &(usac_data->seed_value[usac_data->present_chan]));
266
1.81M
    }
267
3.87M
  }
268
269
124k
  ixheaacd_low_fq_deemphasis(x, lg, alfd_gains);
270
271
124k
  ixheaacd_lpc_coeff_wt_apply(lp_flt_coff_a + (ORDER + 1), i_ap);
272
124k
  ixheaacd_lpc_to_td(i_ap, ORDER, gain1, usac_data->len_subfrm / 4);
273
274
124k
  ixheaacd_lpc_coeff_wt_apply(lp_flt_coff_a + (2 * (ORDER + 1)), i_ap);
275
124k
  ixheaacd_lpc_to_td(i_ap, ORDER, gain2, usac_data->len_subfrm / 4);
276
277
124k
  energy = 0.01f;
278
36.9M
  for (i = 0; i < lg; i++) energy += x[i] * x[i];
279
280
124k
  temp = (FLOAT32)sqrt(energy) / lg;
281
282
124k
  gain_tcx =
283
124k
      (FLOAT32)pow(
284
124k
          10.0f,
285
124k
          ((FLOAT32)pstr_td_frame_data->global_gain[frame_index]) / 28.0f) /
286
124k
      (temp * 2.0f);
287
124k
  }
288
124k
  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
124k
  if (usac_data->frame_ok == 1) {
296
124k
  ixheaacd_noise_shaping(x, lg, (usac_data->len_subfrm) / 4, gain1, gain2);
297
124k
  shiftp = ixheaacd_float2fix(x, int_x, lg);
298
124k
  }
299
124k
  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
124k
  } else {
310
124k
    if (lg & (lg - 1)) {
311
41.7k
      if ((lg != 48) && (lg != 96) && (lg != 192) && (lg != 384) && (lg != 768)) {
312
0
        return -1;
313
0
      }
314
41.7k
    }
315
124k
  }
316
317
124k
  ixheaacd_acelp_mdct_main(usac_data, int_x, int_xn1, (2 * fac_length), lg - (2 * fac_length),
318
124k
                           &preshift);
319
320
124k
  ixheaacd_fix2float(int_xn1, xn_buf, (lg + (2 * fac_length)), &shiftp,
321
124k
                     &preshift);
322
323
124k
  ixheaacd_vec_cnst_mul((2.0f / lg), xn_buf, xn_buf, lg + (2 * fac_length));
324
325
124k
  st->fac_gain =
326
124k
      gain_tcx * 0.5f * (FLOAT32)sqrt(((FLOAT32)fac_length) / (FLOAT32)lg);
327
328
3.77M
  for (i = 0; i < fac_length / 4; i++)
329
3.64M
    st->fac_fd_data[i] = alfd_gains[i * lg / (8 * fac_length)];
330
331
124k
  if (st->mode_prev == 0) {
332
7.64M
    for (i = 0; i < fac_length_prev; i++) {
333
7.57M
      facwindow[i] =
334
7.57M
          sine_window_prev[i] * sine_window_prev[(2 * fac_length_prev) - 1 - i];
335
7.57M
      facwindow[fac_length_prev + i] =
336
7.57M
          1.0f - (sine_window_prev[fac_length_prev + i] *
337
7.57M
                  sine_window_prev[fac_length_prev + i]);
338
7.57M
    }
339
340
3.85M
    for (i = 0; i < fac_length / 2; i++) {
341
3.78M
      x[i] = st->fac_gain *
342
3.78M
             (FLOAT32)pstr_td_frame_data->fac[frame_index * FAC_LENGTH + 2 * i];
343
3.78M
      x[fac_length / 2 + i] =
344
3.78M
          st->fac_gain *
345
3.78M
          (FLOAT32)pstr_td_frame_data
346
3.78M
              ->fac[frame_index * FAC_LENGTH + fac_length - 2 * i - 1];
347
3.78M
    }
348
349
1.01M
    for (i = 0; i < fac_length / 8; i++) {
350
946k
      x[i] *= st->fac_fd_data[2 * i];
351
946k
      x[fac_length - i - 1] *= st->fac_fd_data[2 * i + 1];
352
946k
    }
353
354
65.4k
    preshift = 0;
355
65.4k
    shiftp = ixheaacd_float2fix(x, int_x, fac_length);
356
357
65.4k
    if (usac_data->ec_flag == 0) {
358
65.4k
      if (fac_length & (fac_length - 1)) {
359
25.1k
        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
25.1k
      }
364
65.4k
    }
365
366
65.4k
    ixheaacd_acelp_mdct(int_x, int_xn1, &preshift, fac_length, ptr_scratch);
367
368
65.4k
    ixheaacd_fix2float(int_xn1, xn1, fac_length, &shiftp, &preshift);
369
370
65.4k
    ixheaacd_vec_cnst_mul((2.0f / (FLOAT32)fac_length), xn1, xn1, fac_length);
371
372
65.4k
    memset(xn1 + fac_length, 0, fac_length * sizeof(FLOAT32));
373
374
65.4k
    ixheaacd_lpc_coeff_wt_apply(lp_flt_coff_a + (ORDER + 1), i_ap);
375
65.4k
    ixheaacd_synthesis_tool_float(i_ap, xn1, xn1, 2 * fac_length,
376
65.4k
                                  xn1 + fac_length);
377
378
7.64M
    for (i = 0; i < fac_length; i++) {
379
7.57M
      temp = st->exc_prev[1 + fac_length + i] * facwindow[fac_length + i] +
380
7.57M
             st->exc_prev[fac_length - i] * facwindow[fac_length - 1 - i];
381
7.57M
      xn1[i] += temp;
382
7.57M
    }
383
65.4k
  }
384
385
66.1M
  for (i = 0; i < lg + (2 * fac_length); i++) xn_buf[i] *= gain_tcx;
386
387
28.6M
  for (i = 0; i < (2 * fac_length_prev); i++)
388
28.4M
    xn_buf[i + fac_length - fac_length_prev] *= sine_window_prev[i];
389
390
467k
  for (i = 0; i < fac_length - fac_length_prev; i++) xn_buf[i] = 0.0f;
391
392
124k
  if (st->mode_prev != 0) {
393
13.4M
    for (i = fac_length - fac_length_prev; i < (fac_length + fac_length_prev);
394
13.3M
         i++)
395
13.3M
      xn_buf[i] += st->exc_prev[1 + i];
396
65.4k
  } else {
397
15.2M
    for (i = fac_length - fac_length_prev; i < (fac_length + fac_length_prev);
398
15.1M
         i++)
399
15.1M
      xn_buf[i + fac_length] += xn1[i];
400
65.4k
  }
401
402
124k
  ixheaacd_mem_cpy(xn_buf + lg - 1, st->exc_prev, 1 + (2 * fac_length));
403
404
29.3M
  for (i = 0; i < (2 * fac_length); i++) {
405
29.1M
    xn_buf[i + lg] *= sine_window[(2 * fac_length) - 1 - i];
406
29.1M
  }
407
408
124k
  if (st->mode_prev != 0) {
409
58.9k
    ixheaacd_mem_cpy(xn_buf + fac_length - fac_length_prev,
410
58.9k
                     synth - fac_length_prev, fac_length_prev);
411
412
8.01M
    for (i = 0; i < ORDER + fac_length; i++)
413
7.95M
      buf[i] = synth[i - ORDER - fac_length] -
414
7.95M
               (PREEMPH_FILT_FAC * synth[i - ORDER - fac_length - 1]);
415
416
58.9k
    ptr_a = st->lp_flt_coeff_a_prev;
417
58.9k
    TTT = fac_length % LEN_SUBFR;
418
58.9k
    if (TTT != 0)
419
16.5k
      ixheaacd_residual_tool_float(ptr_a, &buf[ORDER], &exc[-fac_length], TTT,
420
16.5k
                                   1);
421
422
58.9k
    loop_count = (fac_length - TTT) / LEN_SUBFR;
423
58.9k
    ixheaacd_residual_tool_float(ptr_a, &buf[ORDER + TTT],
424
58.9k
                                 &exc[TTT - fac_length], LEN_SUBFR, loop_count);
425
58.9k
  }
426
427
124k
  ixheaacd_mem_cpy(xn, synth, lg);
428
429
124k
  ixheaacd_mem_cpy(synth - ORDER - 1, xn - ORDER - 1, ORDER + 1);
430
124k
  tmp = xn[-ORDER - 1];
431
124k
  ixheaacd_preemphsis_tool_float(xn - ORDER, PREEMPH_FILT_FAC, ORDER + lg, tmp);
432
433
124k
  ptr_a = lp_flt_coff_a + (2 * (ORDER + 1));
434
435
124k
  ixheaacd_residual_tool_float(ptr_a, xn, exc, lg, 1);
436
437
124k
  ixheaacd_mem_cpy(ptr_a, st->lp_flt_coeff_a_prev, ORDER + 1);
438
124k
  ixheaacd_mem_cpy(ptr_a, st->lp_flt_coeff_a_prev + ORDER + 1, ORDER + 1);
439
440
124k
  return err;
441
124k
}
442
443
14.3M
FLOAT32 ixheaacd_randomsign(UWORD32 *seed) {
444
14.3M
  FLOAT32 sign = 0.0f;
445
14.3M
  *seed = (UWORD32)(((UWORD64)(*seed) * (UWORD64)69069) + 5);
446
447
14.3M
  if (((*seed) & 0x10000) > 0)
448
7.28M
    sign = -1.f;
449
7.09M
  else
450
7.09M
    sign = +1.f;
451
452
14.3M
  return sign;
453
14.3M
}