Coverage Report

Created: 2026-02-26 06:35

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libxaac/decoder/ixheaacd_sbrdec_lpfuncs.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 <string.h>
21
#include <math.h>
22
#include "ixheaacd_sbr_common.h"
23
#include "ixheaac_type_def.h"
24
25
#include "ixheaac_constants.h"
26
#include "ixheaac_basic_ops32.h"
27
#include "ixheaac_basic_ops16.h"
28
#include "ixheaac_basic_ops40.h"
29
#include "ixheaac_basic_ops.h"
30
#include "ixheaacd_defines.h"
31
32
#include "ixheaacd_intrinsics.h"
33
#include "ixheaac_sbr_const.h"
34
#include "ixheaac_basic_op.h"
35
#include "ixheaacd_defines.h"
36
#include "ixheaacd_bitbuffer.h"
37
#include "ixheaacd_pns.h"
38
39
#include "ixheaacd_aac_rom.h"
40
#include "ixheaacd_pulsedata.h"
41
42
#include "ixheaacd_drc_data_struct.h"
43
#include "ixheaacd_lt_predict.h"
44
#include "ixheaacd_cnst.h"
45
#include "ixheaacd_ec_defines.h"
46
#include "ixheaacd_ec_struct_def.h"
47
#include "ixheaacd_channelinfo.h"
48
#include "ixheaacd_drc_dec.h"
49
50
#include "ixheaacd_sbrdecoder.h"
51
52
#include "ixheaacd_sbrdecsettings.h"
53
#include "ixheaacd_sbr_scale.h"
54
#include "ixheaacd_lpp_tran.h"
55
#include "ixheaacd_env_extr_part.h"
56
#include "ixheaacd_sbr_rom.h"
57
#include "ixheaacd_hybrid.h"
58
#include "ixheaacd_ps_dec.h"
59
#include "ixheaacd_ps_bitdec.h"
60
#include "ixheaacd_env_extr.h"
61
#include "ixheaacd_common_rom.h"
62
#include "ixheaacd_freq_sca.h"
63
64
#include "ixheaacd_qmf_dec.h"
65
66
#include "ixheaacd_env_calc.h"
67
68
#include "ixheaacd_pvc_dec.h"
69
#include "ixheaacd_sbr_dec.h"
70
#include "ixheaacd_env_dec.h"
71
#include "ixheaacd_basic_funcs.h"
72
#include "ixheaacd_sbr_crc.h"
73
#include "ixheaacd_function_selector.h"
74
75
#include "ixheaacd_audioobjtypes.h"
76
#include "ixheaacd_error_codes.h"
77
78
#define ALIGN_SIZE64(x) ((((x) + 7) >> 3) << 3)
79
80
static const FLOAT32 ixheaacd_new_bw_table[4][4] = {
81
    {0.00f, 0.60f, 0.90f, 0.98f},
82
    {0.60f, 0.75f, 0.90f, 0.98f},
83
    {0.00f, 0.75f, 0.90f, 0.98f},
84
    {0.00f, 0.75f, 0.90f, 0.98f}};
85
static const WORD32 ixheaacd_inew_bw_table[4][4] = {
86
    {0x00000000, 0x4ccccccd, 0x73333333, 0x7d70a3d7},
87
    {0x4ccccccd, 0x60000000, 0x73333333, 0x7d70a3d7},
88
    {0x00000000, 0x60000000, 0x73333333, 0x7d70a3d7},
89
    {0x00000000, 0x60000000, 0x73333333, 0x7d70a3d7}};
90
91
67.6k
VOID ixheaacd_reset_sbrenvelope_calc(ia_sbr_calc_env_struct *h_cal_env) {
92
67.6k
  h_cal_env->ph_index = 0;
93
67.6k
  h_cal_env->filt_buf_noise_e = 0;
94
67.6k
  h_cal_env->start_up = 1;
95
67.6k
}
96
97
VOID ixheaacd_derive_lim_band_tbl(
98
    ia_sbr_header_data_struct *ptr_header_data,
99
    const ia_patch_param_struct *p_str_patch_param, WORD16 num_patches,
100
12.7k
    ixheaacd_misc_tables *pstr_common_tables) {
101
12.7k
  WORD32 i, k, k_1;
102
12.7k
  WORD32 nr_lim, patch_border_k, patch_border_k_1, temp_nr_lim;
103
104
12.7k
  WORD16 lim_table[MAX_FREQ_COEFFS / 2 + MAX_NUM_PATCHES + 1];
105
12.7k
  WORD16 patch_borders[MAX_NUM_PATCHES + 1];
106
12.7k
  WORD16 kx, k2;
107
12.7k
  WORD16 temp, lim_bands, num_octaves;
108
109
12.7k
  WORD16 *f_lim_tbl = ptr_header_data->pstr_freq_band_data->freq_band_tbl_lim;
110
12.7k
  WORD16 *num_lf_bands = &ptr_header_data->pstr_freq_band_data->num_lf_bands;
111
12.7k
  WORD16 *f_low_tbl =
112
12.7k
      ptr_header_data->pstr_freq_band_data->freq_band_table[LOW];
113
12.7k
  WORD16 num_low_bnd = ptr_header_data->pstr_freq_band_data->num_sf_bands[LOW];
114
12.7k
  WORD16 limiter_bands = ptr_header_data->limiter_bands;
115
116
12.7k
  WORD16 sub_band_start = f_low_tbl[0];
117
12.7k
  WORD16 sub_band_end = f_low_tbl[num_low_bnd];
118
12.7k
  const WORD16 limbnd_per_oct[4] = {(WORD16)0x2000, (WORD16)0x2666, (WORD16)0x4000,
119
12.7k
                                    (WORD16)0x6000};
120
121
12.7k
  if (limiter_bands == 0) {
122
1.52k
    f_lim_tbl[0] = 0;
123
1.52k
    f_lim_tbl[1] = sub_band_end - sub_band_start;
124
1.52k
    nr_lim = 1;
125
11.2k
  } else {
126
39.7k
    for (k = 0; k < num_patches; k++) {
127
28.5k
      patch_borders[k] = p_str_patch_param[k].guard_start_band - sub_band_start;
128
28.5k
    }
129
11.2k
    patch_borders[k] = sub_band_end - sub_band_start;
130
131
92.6k
    for (k = 0; k <= num_low_bnd; k++) {
132
81.4k
      lim_table[k] = f_low_tbl[k] - sub_band_start;
133
81.4k
    }
134
28.9k
    for (k = 1; k < num_patches; k++) {
135
17.7k
      lim_table[num_low_bnd + k] = patch_borders[k];
136
17.7k
    }
137
138
11.2k
    temp_nr_lim = nr_lim = (num_low_bnd + num_patches) - 1;
139
11.2k
    ixheaacd_aac_shellsort(lim_table, (temp_nr_lim + 1));
140
141
11.2k
    k = 1;
142
11.2k
    k_1 = 0;
143
144
11.2k
    lim_bands = limbnd_per_oct[limiter_bands];
145
146
98.7k
    while ((k - temp_nr_lim) <= 0) {
147
87.5k
      k2 = lim_table[k] + sub_band_start;
148
87.5k
      kx = lim_table[k_1] + sub_band_start;
149
150
87.5k
      num_octaves = pstr_common_tables->log_dual_is_table[k2];
151
87.5k
      num_octaves -= pstr_common_tables->log_dual_is_table[kx];
152
153
87.5k
      temp = (WORD16)(((WORD32)lim_bands * (WORD32)num_octaves) >> 15);
154
155
87.5k
      if (temp < 0x01f6) {
156
54.1k
        if (lim_table[k_1] == lim_table[k]) {
157
7.92k
          lim_table[k] = sub_band_end;
158
7.92k
          nr_lim = nr_lim - 1;
159
7.92k
          k = (k + 1);
160
7.92k
          continue;
161
7.92k
        }
162
46.2k
        patch_border_k_1 = patch_border_k = 0;
163
164
232k
        for (i = 0; i <= num_patches; i++) {
165
185k
          if (lim_table[k] == patch_borders[i]) {
166
15.7k
            patch_border_k = 1;
167
15.7k
          }
168
185k
          if (lim_table[k_1] == patch_borders[i]) {
169
27.0k
            patch_border_k_1 = 1;
170
27.0k
          }
171
185k
        }
172
46.2k
        if (!patch_border_k) {
173
30.5k
          lim_table[k] = sub_band_end;
174
30.5k
          nr_lim = nr_lim - 1;
175
30.5k
          k = (k + 1);
176
30.5k
          continue;
177
30.5k
        }
178
179
15.7k
        if (!patch_border_k_1) {
180
10.5k
          lim_table[k_1] = sub_band_end;
181
10.5k
          nr_lim = nr_lim - 1;
182
10.5k
        }
183
15.7k
      }
184
49.1k
      k_1 = k;
185
49.1k
      k = (k + 1);
186
49.1k
    }
187
11.2k
    ixheaacd_aac_shellsort(lim_table, (temp_nr_lim + 1));
188
189
11.2k
    memcpy(f_lim_tbl, lim_table, sizeof(WORD16) * (nr_lim + 1));
190
11.2k
  }
191
12.7k
  *num_lf_bands = nr_lim;
192
193
12.7k
  return;
194
12.7k
}
195
196
VOID ixheaacd_lean_sbrconcealment(
197
    ia_sbr_header_data_struct *ptr_header_data,
198
    ia_sbr_frame_info_data_struct *ptr_sbr_data,
199
22.6k
    ia_sbr_prev_frame_data_struct *ptr_prev_data) {
200
22.6k
  WORD32 target;
201
22.6k
  WORD32 step;
202
22.6k
  WORD32 i;
203
204
22.6k
  WORD16 cur_start_pos;
205
22.6k
  WORD16 cur_stop_pos;
206
207
22.6k
  ptr_sbr_data->amp_res = ptr_prev_data->amp_res;
208
22.6k
  ptr_sbr_data->coupling_mode = ptr_prev_data->coupling_mode;
209
22.6k
  ptr_sbr_data->max_qmf_subband_aac = ptr_prev_data->max_qmf_subband_aac;
210
211
22.6k
  memcpy(ptr_sbr_data->sbr_invf_mode, ptr_prev_data->sbr_invf_mode,
212
22.6k
         sizeof(WORD32) * MAX_INVF_BANDS);
213
214
22.6k
  ptr_sbr_data->str_frame_info_details.num_env = 1;
215
216
22.6k
  cur_start_pos = ptr_prev_data->end_position - ptr_header_data->num_time_slots;
217
22.6k
  cur_stop_pos = ptr_header_data->num_time_slots;
218
219
22.6k
  ptr_sbr_data->str_frame_info_details.border_vec[0] = cur_start_pos;
220
22.6k
  ptr_sbr_data->str_frame_info_details.border_vec[1] = cur_stop_pos;
221
222
22.6k
  ptr_sbr_data->str_frame_info_details.noise_border_vec[0] = cur_start_pos;
223
22.6k
  ptr_sbr_data->str_frame_info_details.noise_border_vec[1] = cur_stop_pos;
224
22.6k
  ;
225
226
22.6k
  ptr_sbr_data->str_frame_info_details.freq_res[0] = 1;
227
22.6k
  ptr_sbr_data->str_frame_info_details.transient_env = -1;
228
22.6k
  ptr_sbr_data->str_frame_info_details.num_noise_env = 1;
229
230
22.6k
  ptr_sbr_data->num_env_sfac =
231
22.6k
      ptr_header_data->pstr_freq_band_data->num_sf_bands[1];
232
233
22.6k
  ptr_sbr_data->del_cod_dir_arr[0] = DTDF_DIR_TIME;
234
235
22.6k
  if (ptr_sbr_data->coupling_mode == COUPLING_BAL) {
236
3.42k
    target = SBR_ENERGY_PAN_OFFSET;
237
19.2k
  } else {
238
19.2k
    target = 0;
239
19.2k
  }
240
241
22.6k
  step = 1;
242
243
22.6k
  if (ptr_header_data->amp_res - SBR_AMPLITUDE_RESOLUTION_1_5 == 0) {
244
14.1k
    target = (target << 1);
245
14.1k
    step = (step << 1);
246
14.1k
  }
247
248
281k
  for (i = 0; i < ptr_sbr_data->num_env_sfac; i++) {
249
258k
    if (ptr_prev_data->sfb_nrg_prev[i] > target)
250
150k
      ptr_sbr_data->int_env_sf_arr[i] = -(step);
251
108k
    else
252
108k
      ptr_sbr_data->int_env_sf_arr[i] = step;
253
258k
  }
254
255
22.6k
  ptr_sbr_data->del_cod_dir_noise_arr[0] = DTDF_DIR_TIME;
256
257
22.6k
  memset(ptr_sbr_data->int_noise_floor, 0,
258
22.6k
         sizeof(ptr_sbr_data->int_noise_floor));
259
260
22.6k
  memset(ptr_sbr_data->add_harmonics, 0, sizeof(FLAG) * MAX_FREQ_COEFFS);
261
22.6k
}
262
263
static WORD16 ixheaacd_find_closest_entry(WORD32 goal_sb, WORD16 *f_master_tbl,
264
                                          WORD16 num_mf_bands,
265
223k
                                          WORD16 direction) {
266
223k
  WORD32 index;
267
268
223k
  if (goal_sb <= f_master_tbl[0]) return f_master_tbl[0];
269
270
223k
  if (goal_sb >= f_master_tbl[num_mf_bands]) return f_master_tbl[num_mf_bands];
271
272
202k
  if (direction) {
273
734
    index = 0;
274
5.12k
    while (f_master_tbl[index] < goal_sb) {
275
4.39k
      index++;
276
4.39k
    }
277
201k
  } else {
278
201k
    index = num_mf_bands;
279
1.22M
    while (f_master_tbl[index] > goal_sb) {
280
1.02M
      index--;
281
1.02M
    }
282
201k
  }
283
284
202k
  return f_master_tbl[index];
285
223k
}
286
287
WORD32 ixheaacd_reset_hf_generator(ia_sbr_hf_generator_struct *ptr_hf_gen_str,
288
                                   ia_sbr_header_data_struct *ptr_header_data,
289
12.7k
                                   WORD audio_object_type) {
290
12.7k
  WORD32 patch, sb;
291
12.7k
  WORD32 temp;
292
12.7k
  WORD16 *ptr_noise_freq_tbl;
293
12.7k
  WORD32 num_nf_bands;
294
295
12.7k
  ia_transposer_settings_struct *pstr_transposer_settings =
296
12.7k
      ptr_hf_gen_str->pstr_settings;
297
12.7k
  ia_patch_param_struct *p_str_patch_param =
298
12.7k
      pstr_transposer_settings->str_patch_param;
299
300
12.7k
  WORD32 sub_band_start = ptr_header_data->pstr_freq_band_data->sub_band_start;
301
12.7k
  WORD16 *f_master_tbl = ptr_header_data->pstr_freq_band_data->f_master_tbl;
302
12.7k
  WORD16 num_mf_bands = ptr_header_data->pstr_freq_band_data->num_mf_bands;
303
12.7k
  WORD16 usb = ptr_header_data->pstr_freq_band_data->sub_band_end;
304
305
12.7k
  WORD32 src_start_band;
306
12.7k
  WORD32 patch_stride;
307
12.7k
  WORD32 num_bands_in_patch;
308
309
12.7k
  WORD32 lsb = f_master_tbl[0];
310
12.7k
  WORD16 xover_offset = sub_band_start - lsb;
311
312
12.7k
  WORD16 goal_sb, flag_break_1 = 0;
313
12.7k
  WORD32 fs = ptr_header_data->out_sampling_freq;
314
315
12.7k
  if (lsb < (SHIFT_START_SB + 4)) {
316
0
    return (1);
317
0
  }
318
12.7k
  switch (fs) {
319
857
    case 16000:
320
949
    case 22050:
321
1.05k
    case 24000:
322
10.8k
    case 32000:
323
10.8k
      goal_sb = 64;
324
10.8k
      break;
325
41
    case 44100:
326
41
      goal_sb = 46;
327
41
      break;
328
20
    case 48000:
329
20
      goal_sb = 43;
330
20
      break;
331
89
    case 64000:
332
89
      goal_sb = 32;
333
89
      break;
334
628
    case 88200:
335
628
      goal_sb = 23;
336
628
      break;
337
694
    case 96000:
338
694
      goal_sb = 21;
339
694
      break;
340
464
    default:
341
464
      return (0);
342
12.7k
  }
343
344
12.2k
  goal_sb = ixheaacd_find_closest_entry(goal_sb, f_master_tbl, num_mf_bands, 1);
345
12.2k
  if (audio_object_type != AOT_ER_AAC_ELD &&
346
428
      audio_object_type != AOT_ER_AAC_LD) {
347
428
    if (ixheaac_abs16_sat((WORD16)(goal_sb - usb)) < 4) {
348
357
      goal_sb = usb;
349
357
    }
350
428
  }
351
352
12.2k
  src_start_band = SHIFT_START_SB + xover_offset;
353
12.2k
  sb = (lsb + xover_offset);
354
355
12.2k
  patch = 0;
356
357
12.2k
  if ((goal_sb < sb) && (lsb > src_start_band)) {
358
9
    return -1;
359
9
  }
360
361
45.8k
  while (((sb - usb) < 0) && (patch < MAX_NUM_PATCHES)) {
362
33.5k
    ia_patch_param_struct *ptr_loc_patch_param = &p_str_patch_param[patch];
363
33.5k
    WORD16 abs_sb, flag_break = 0;
364
33.5k
    ptr_loc_patch_param->guard_start_band = sb;
365
33.5k
    sb = (sb + GUARDBANDS);
366
33.5k
    ptr_loc_patch_param->dst_start_band = sb;
367
368
33.5k
    num_bands_in_patch = (goal_sb - sb);
369
33.5k
    if ((num_bands_in_patch <= 0) &&
370
54
        ((num_bands_in_patch - (lsb - src_start_band)) < 0)) {
371
54
      flag_break = 1;
372
54
    }
373
33.5k
    if ((num_bands_in_patch - (lsb - src_start_band)) >= 0) {
374
21.6k
      patch_stride = sb - src_start_band;
375
21.6k
      patch_stride = (WORD16)(patch_stride & ~1);
376
21.6k
      num_bands_in_patch = (lsb - (sb - patch_stride));
377
21.6k
      num_bands_in_patch = ixheaacd_find_closest_entry(
378
21.6k
          sb + num_bands_in_patch, f_master_tbl, num_mf_bands, 0);
379
21.6k
      num_bands_in_patch -= sb;
380
21.6k
    }
381
382
33.5k
    patch_stride = ((num_bands_in_patch + sb) - lsb);
383
33.5k
    patch_stride = (WORD16)((patch_stride + 1) & ~1);
384
385
33.5k
    if (num_bands_in_patch > 0) {
386
33.0k
      ptr_loc_patch_param->src_start_band = (sb - patch_stride);
387
33.0k
      ptr_loc_patch_param->dst_end_band = patch_stride;
388
33.0k
      ptr_loc_patch_param->num_bands_in_patch = num_bands_in_patch;
389
33.0k
      ptr_loc_patch_param->src_end_band =
390
33.0k
          (ptr_loc_patch_param->src_start_band + num_bands_in_patch);
391
392
33.0k
      sb = (sb + ptr_loc_patch_param->num_bands_in_patch);
393
33.0k
      patch++;
394
33.0k
    }
395
396
33.5k
    src_start_band = SHIFT_START_SB;
397
33.5k
    abs_sb = ixheaac_abs16_sat((WORD16)((sb - goal_sb))) - 3;
398
399
33.5k
    if (num_bands_in_patch <= 0 && flag_break_1 == 1) {
400
0
      break;
401
0
    }
402
403
33.5k
    if (abs_sb < 0) {
404
13.1k
      goal_sb = usb;
405
20.4k
    } else {
406
20.4k
      if (flag_break == 1) break;
407
20.4k
    }
408
409
33.5k
    if (num_bands_in_patch <= 0) {
410
549
      flag_break_1 = 1;
411
33.0k
    } else {
412
33.0k
      flag_break_1 = 0;
413
33.0k
    }
414
33.5k
  }
415
416
12.2k
  patch--;
417
418
12.2k
  if ((patch > 0) && (p_str_patch_param[patch].num_bands_in_patch < 3)) {
419
657
    patch--;
420
657
    sb = p_str_patch_param[patch].dst_start_band +
421
657
         p_str_patch_param[patch].num_bands_in_patch;
422
657
  }
423
424
12.2k
  if (patch >= MAX_NUM_PATCHES) {
425
0
    return -1;
426
0
  }
427
428
12.2k
  pstr_transposer_settings->num_patches = patch + 1;
429
430
12.2k
  temp = 0;
431
432
44.6k
  for (patch = 0; patch < pstr_transposer_settings->num_patches; patch++) {
433
32.3k
    sb = ixheaac_min32(sb, p_str_patch_param[patch].src_start_band);
434
32.3k
    temp = ixheaac_max32(temp, p_str_patch_param[patch].src_end_band);
435
32.3k
  }
436
437
12.2k
  if (sb > temp) return IA_FATAL_ERROR;
438
439
12.2k
  pstr_transposer_settings->start_patch = sb;
440
12.2k
  pstr_transposer_settings->stop_patch = temp;
441
442
12.2k
  ptr_noise_freq_tbl =
443
12.2k
      ptr_header_data->pstr_freq_band_data->freq_band_tbl_noise;
444
12.2k
  num_nf_bands = ptr_header_data->pstr_freq_band_data->num_nf_bands;
445
446
12.2k
  memcpy(&pstr_transposer_settings->bw_borders[0], &ptr_noise_freq_tbl[1],
447
12.2k
         sizeof(WORD16) * num_nf_bands);
448
449
12.2k
  memset(ptr_hf_gen_str->bw_array_prev, 0, sizeof(WORD32) * MAX_NUM_PATCHES);
450
451
12.2k
  return 0;
452
12.2k
}
453
VOID ixheaacd_rescale_x_overlap(
454
    ia_sbr_dec_struct *ptr_sbr_dec, ia_sbr_header_data_struct *ptr_header_data,
455
    ia_sbr_frame_info_data_struct *ptr_frame_data,
456
    ia_sbr_prev_frame_data_struct *ptr_frame_data_prev,
457
    WORD32 **pp_overlap_buffer_real, WORD32 **pp_overlap_buffer_imag,
458
33.4k
    FLAG low_pow_flag) {
459
33.4k
  WORD32 k, l;
460
33.4k
  WORD32 start_band, end_band;
461
33.4k
  WORD32 target_lsb, target_usb;
462
33.4k
  WORD32 source_scale, target_scale, delta_scale, reserve;
463
464
33.4k
  WORD32 old_lsb = ptr_frame_data_prev->max_qmf_subband_aac;
465
33.4k
  WORD32 start_slot =
466
33.4k
      (ptr_header_data->time_step *
467
33.4k
       (ptr_frame_data_prev->end_position - ptr_header_data->num_time_slots));
468
33.4k
  WORD32 new_lsb = ptr_frame_data->max_qmf_subband_aac;
469
470
33.4k
  ptr_sbr_dec->str_codec_qmf_bank.usb = new_lsb;
471
33.4k
  ptr_sbr_dec->str_synthesis_qmf_bank.lsb = new_lsb;
472
473
33.4k
  start_band = ixheaac_min32(old_lsb, new_lsb);
474
33.4k
  end_band = ixheaac_max32(old_lsb, new_lsb);
475
476
33.4k
  if (new_lsb != old_lsb && old_lsb > 0) {
477
38.9k
    for (l = start_slot; l < 6; l++) {
478
142k
      for (k = old_lsb; k < new_lsb; k++) {
479
108k
        pp_overlap_buffer_real[l][k] = 0L;
480
481
108k
        if (!low_pow_flag) {
482
108k
          pp_overlap_buffer_imag[l][k] = 0L;
483
108k
        }
484
108k
      }
485
33.3k
    }
486
487
5.56k
    if (new_lsb > old_lsb) {
488
2.57k
      source_scale = ptr_sbr_dec->str_sbr_scale_fact.ov_hb_scale;
489
2.57k
      target_scale = ptr_sbr_dec->str_sbr_scale_fact.ov_lb_scale;
490
2.57k
      target_lsb = 0;
491
2.57k
      target_usb = old_lsb;
492
2.99k
    } else {
493
2.99k
      source_scale = ptr_sbr_dec->str_sbr_scale_fact.ov_lb_scale;
494
2.99k
      target_scale = ptr_sbr_dec->str_sbr_scale_fact.ov_hb_scale;
495
2.99k
      target_lsb = old_lsb;
496
2.99k
      target_usb = ptr_sbr_dec->str_synthesis_qmf_bank.usb;
497
2.99k
    }
498
499
5.56k
    reserve = (*ixheaacd_ixheaacd_expsubbandsamples)(
500
5.56k
        pp_overlap_buffer_real, pp_overlap_buffer_imag, start_band, end_band, 0,
501
5.56k
        start_slot, low_pow_flag);
502
503
5.56k
    (*ixheaacd_adjust_scale)(pp_overlap_buffer_real, pp_overlap_buffer_imag,
504
5.56k
                             start_band, end_band, 0, start_slot, reserve,
505
5.56k
                             low_pow_flag);
506
507
5.56k
    source_scale += reserve;
508
509
5.56k
    delta_scale = (target_scale - source_scale);
510
511
5.56k
    if (delta_scale > 0) {
512
32
      delta_scale = -(delta_scale);
513
32
      start_band = target_lsb;
514
32
      end_band = target_usb;
515
516
32
      if (new_lsb > old_lsb) {
517
0
        ptr_sbr_dec->str_sbr_scale_fact.ov_lb_scale = source_scale;
518
32
      } else {
519
32
        ptr_sbr_dec->str_sbr_scale_fact.ov_hb_scale = source_scale;
520
32
      }
521
32
    }
522
523
5.56k
    (*ixheaacd_adjust_scale)(pp_overlap_buffer_real, pp_overlap_buffer_imag,
524
5.56k
                             start_band, end_band, 0, start_slot, delta_scale,
525
5.56k
                             low_pow_flag);
526
5.56k
  }
527
33.4k
}
528
529
VOID ixheaacd_map_sineflags(WORD16 *freq_band_table, WORD16 num_sf_bands,
530
                            FLAG *add_harmonics, WORD8 *harm_flags_prev,
531
                            WORD16 transient_env, WORD8 *sine_mapped)
532
533
30.0k
{
534
30.0k
  WORD32 qmfband2, li, ui, i;
535
30.0k
  WORD32 low_subband_sec;
536
30.0k
  WORD32 oldflags;
537
538
30.0k
  low_subband_sec = (freq_band_table[0] << 1);
539
540
30.0k
  memset(sine_mapped, MAX_ENVELOPES, sizeof(WORD8) * MAX_FREQ_COEFFS);
541
542
386k
  for (i = (num_sf_bands - 1); i >= 0; i--) {
543
356k
    oldflags = *harm_flags_prev;
544
356k
    *harm_flags_prev++ = add_harmonics[i];
545
546
356k
    if (add_harmonics[i]) {
547
16.9k
      li = freq_band_table[i];
548
549
16.9k
      ui = freq_band_table[i + 1];
550
551
16.9k
      qmfband2 = ((ui + li) - low_subband_sec) >> 1;
552
553
16.9k
      if (oldflags)
554
1.90k
        sine_mapped[qmfband2] = 0;
555
15.0k
      else
556
15.0k
        sine_mapped[qmfband2] = (WORD8)transient_env;
557
16.9k
    }
558
356k
  }
559
30.0k
}
560
561
1.61k
VOID ixheaacd_map_34_params_to_20(WORD16 *params) {
562
1.61k
  params[0] = ixheaacd_divideby3(params[0] + params[0] + params[1]);
563
1.61k
  params[1] = ixheaacd_divideby3(params[1] + params[2] + params[2]);
564
1.61k
  params[2] = ixheaacd_divideby3(params[3] + params[3] + params[4]);
565
1.61k
  params[3] = ixheaacd_divideby3(params[4] + params[5] + params[5]);
566
1.61k
  params[4] = ixheaacd_divideby2(params[6] + params[7]);
567
1.61k
  params[5] = ixheaacd_divideby2(params[8] + params[9]);
568
1.61k
  params[6] = params[10];
569
1.61k
  params[7] = params[11];
570
1.61k
  params[8] = ixheaacd_divideby2(params[12] + params[13]);
571
1.61k
  params[9] = ixheaacd_divideby2(params[14] + params[15]);
572
1.61k
  params[10] = params[16];
573
1.61k
  params[11] = params[17];
574
1.61k
  params[12] = params[18];
575
1.61k
  params[13] = params[19];
576
1.61k
  params[14] = ixheaacd_divideby2(params[20] + params[21]);
577
1.61k
  params[15] = ixheaacd_divideby2(params[22] + params[23]);
578
1.61k
  params[16] = ixheaacd_divideby2(params[24] + params[25]);
579
1.61k
  params[17] = ixheaacd_divideby2(params[26] + params[27]);
580
1.61k
  params[18] = ixheaacd_divideby2(
581
1.61k
      ixheaacd_divideby2(params[28] + params[29] + params[30] + params[31]));
582
1.61k
  params[19] = ixheaacd_divideby2(params[32] + params[33]);
583
1.61k
}
584
585
extern const WORD16 ixheaacd_num_bands[3];
586
587
IA_ERRORCODE ixheaacd_read_ps_data(ia_ps_dec_struct *ptr_ps_dec, ia_bit_buf_struct *it_bit_buff,
588
                                   WORD16 num_bits_left, ia_ps_tables_struct *ps_tables_ptr)
589
2.09k
{
590
2.09k
  WORD b, e, temp;
591
2.09k
  const WORD16 num_env_tab[4] = {0, 1, 2, 4};
592
2.09k
  WORD cnt_bits;
593
2.09k
  ia_huffman_data_type huffman_table, huffman_df_table, huffman_dt_table;
594
2.09k
  FLAG enable_ps_header;
595
596
2.09k
  if (!ptr_ps_dec) {
597
0
    return 0;
598
0
  }
599
600
2.09k
  cnt_bits = it_bit_buff->cnt_bits;
601
602
2.09k
  enable_ps_header = ixheaacd_read_bits_buf(it_bit_buff, 1);
603
604
2.09k
  if (enable_ps_header) {
605
446
    ptr_ps_dec->enable_iid = ixheaacd_read_bits_buf(it_bit_buff, 1);
606
446
    if (ptr_ps_dec->enable_iid) {
607
361
      ptr_ps_dec->iid_mode = ixheaacd_read_bits_buf(it_bit_buff, 3);
608
361
    }
609
610
446
    if (ptr_ps_dec->iid_mode > 2) {
611
271
      ptr_ps_dec->iid_quant = 1;
612
271
      ptr_ps_dec->iid_mode -= 3;
613
271
    } else {
614
175
      ptr_ps_dec->iid_quant = 0;
615
175
    }
616
617
446
    ptr_ps_dec->enable_icc = ixheaacd_read_bits_buf(it_bit_buff, 1);
618
446
    if (ptr_ps_dec->enable_icc) {
619
270
      ptr_ps_dec->icc_mode = ixheaacd_read_bits_buf(it_bit_buff, 3);
620
270
    }
621
622
446
    ptr_ps_dec->enable_ext = ixheaacd_read_bits_buf(it_bit_buff, 1);
623
624
446
    if (ptr_ps_dec->icc_mode > 2) {
625
215
      ptr_ps_dec->icc_mode -= 3;
626
215
      ptr_ps_dec->use_pca_rot_flg = 1;
627
231
    } else {
628
231
      ptr_ps_dec->use_pca_rot_flg = 0;
629
231
    }
630
446
    ptr_ps_dec->freq_res_ipd = ptr_ps_dec->iid_mode;
631
446
    if (ptr_ps_dec->freq_res_ipd > 2) {
632
3
      return IA_FATAL_ERROR;
633
3
    }
634
446
  }
635
636
2.09k
  ptr_ps_dec->use_34_st_bands = 0;
637
2.09k
  ptr_ps_dec->use_pca_rot_flg = 0;
638
639
2.09k
  if ((ptr_ps_dec->enable_iid && ptr_ps_dec->iid_mode > 2) ||
640
2.09k
      (ptr_ps_dec->enable_icc && ptr_ps_dec->icc_mode > 2)) {
641
364
    ptr_ps_dec->ps_data_present = 0;
642
643
364
    num_bits_left -= (cnt_bits - it_bit_buff->cnt_bits);
644
645
1.62k
    while (num_bits_left > 8) {
646
1.26k
      ixheaacd_read_bits_buf(it_bit_buff, 8);
647
1.26k
      num_bits_left -= 8;
648
1.26k
    }
649
364
    if (num_bits_left >= 0) {
650
337
      ixheaacd_read_bits_buf(it_bit_buff, num_bits_left);
651
337
    }
652
653
364
    return (cnt_bits - it_bit_buff->cnt_bits);
654
364
  }
655
656
1.72k
  ptr_ps_dec->frame_class = (FLAG)ixheaacd_read_bits_buf(it_bit_buff, 1);
657
658
1.72k
  temp = ixheaacd_read_bits_buf(it_bit_buff, 2);
659
660
1.72k
  if (ptr_ps_dec->frame_class == 0) {
661
1.41k
    ptr_ps_dec->num_env = num_env_tab[temp];
662
1.41k
  } else {
663
309
    ptr_ps_dec->num_env = (((1 + temp) << 8) >> 8);
664
665
1.25k
    for (e = 1; e < ptr_ps_dec->num_env + 1; e++) {
666
944
      ptr_ps_dec->border_position[e] =
667
944
          (((ixheaacd_read_bits_buf(it_bit_buff, 5) + 1) << 8) >> 8);
668
944
    }
669
309
  }
670
671
1.72k
  if (ptr_ps_dec->enable_iid) {
672
528
    if (ptr_ps_dec->iid_quant) {
673
432
      huffman_df_table = (ia_huffman_data_type)&ps_tables_ptr->huff_iid_df_fine;
674
432
      huffman_dt_table = (ia_huffman_data_type)&ps_tables_ptr->huff_iid_dt_fine;
675
432
    } else {
676
96
      huffman_df_table = (ia_huffman_data_type)&ps_tables_ptr->huff_iid_df;
677
96
      huffman_dt_table = (ia_huffman_data_type)&ps_tables_ptr->huff_iid_dt;
678
96
    }
679
680
1.32k
    for (e = 0; e < ptr_ps_dec->num_env; e++) {
681
794
      ptr_ps_dec->iid_dt[e] = (FLAG)ixheaacd_read_bits_buf(it_bit_buff, 1);
682
683
794
      if (ptr_ps_dec->iid_dt[e]) {
684
425
        huffman_table = huffman_dt_table;
685
425
      } else {
686
369
        huffman_table = huffman_df_table;
687
369
      }
688
689
20.0k
      for (b = 0; b < ixheaacd_num_bands[ptr_ps_dec->iid_mode]; b++) {
690
19.2k
        ptr_ps_dec->iid_par_table[e][b] =
691
19.2k
            ixheaacd_ssc_huff_dec(huffman_table, it_bit_buff);
692
19.2k
      }
693
794
    }
694
528
  }
695
696
1.72k
  if (ptr_ps_dec->enable_icc) {
697
361
    huffman_df_table = (ia_huffman_data_type)&ps_tables_ptr->huff_icc_df;
698
361
    huffman_dt_table = (ia_huffman_data_type)&ps_tables_ptr->huff_icc_dt;
699
700
1.21k
    for (e = 0; e < ptr_ps_dec->num_env; e++) {
701
849
      ptr_ps_dec->icc_dt[e] = ixheaacd_read_bits_buf(it_bit_buff, 1);
702
703
849
      if (ptr_ps_dec->icc_dt[e]) {
704
293
        huffman_table = huffman_dt_table;
705
556
      } else {
706
556
        huffman_table = huffman_df_table;
707
556
      }
708
709
11.1k
      for (b = 0; b < ixheaacd_num_bands[ptr_ps_dec->icc_mode]; b++) {
710
10.3k
        ptr_ps_dec->icc_par_table[e][b] = ixheaacd_ssc_huff_dec(huffman_table, it_bit_buff);
711
10.3k
      }
712
849
    }
713
361
  }
714
715
1.72k
  if (ptr_ps_dec->enable_ext) {
716
192
    WORD32 cnt;
717
192
    if (it_bit_buff->cnt_bits < 4)
718
1
      cnt = ixheaacd_read_bits_buf(it_bit_buff, it_bit_buff->cnt_bits);
719
191
    else
720
191
      cnt = ixheaacd_read_bits_buf(it_bit_buff, 4);
721
722
192
    if (cnt == 15) {
723
40
      cnt += ixheaacd_read_bits_buf(it_bit_buff, 8);
724
40
    }
725
9.55k
    while (cnt--) {
726
9.36k
      ixheaacd_read_bits_buf(it_bit_buff, 8);
727
9.36k
    }
728
192
  }
729
730
1.72k
  ptr_ps_dec->ps_data_present = 1;
731
732
1.72k
  return (cnt_bits - it_bit_buff->cnt_bits);
733
2.09k
}
734
735
VOID ixheaacd_invfilt_level_emphasis(ia_sbr_hf_generator_struct *ptr_hf_gen_str,
736
                                     WORD32 num_if_bands, WORD32 *inv_filt_mode,
737
                                     WORD32 *inv_filt_mode_prev,
738
30.0k
                                     WORD32 *bw_array) {
739
30.0k
  WORD32 i;
740
30.0k
  WORD32 accu;
741
30.0k
  WORD16 w1, w2;
742
743
102k
  for (i = 0; i < num_if_bands; i++) {
744
72.1k
    bw_array[i] =
745
72.1k
        ixheaacd_inew_bw_table[inv_filt_mode_prev[i]][inv_filt_mode[i]];
746
747
72.1k
    if (bw_array[i] < ptr_hf_gen_str->bw_array_prev[i]) {
748
6.89k
      w1 = 0x6000;
749
6.89k
      w2 = 0x2000;
750
65.2k
    } else {
751
65.2k
      w1 = 0x7400;
752
65.2k
      w2 = 0x0c00;
753
65.2k
    }
754
72.1k
    accu = ixheaac_add32(
755
72.1k
        ixheaac_mult32x16in32_shl(bw_array[i], w1),
756
72.1k
        ixheaac_mult32x16in32_shl(ptr_hf_gen_str->bw_array_prev[i], w2));
757
758
72.1k
    if (accu < 0x02000000) {
759
47.3k
      accu = 0;
760
47.3k
    }
761
762
72.1k
    if (accu >= 0x7f800000) {
763
0
      accu = 0x7f800000;
764
0
    }
765
72.1k
    bw_array[i] = accu;
766
72.1k
  }
767
30.0k
}
768
769
typedef struct {
770
  FLOAT32 phi_0_1_real;
771
  FLOAT32 phi_0_1_imag;
772
  FLOAT32 phi_0_2_real;
773
  FLOAT32 phi_0_2_imag;
774
  FLOAT32 phi_1_1;
775
  FLOAT32 phi_1_2_real;
776
  FLOAT32 phi_1_2_imag;
777
  FLOAT32 phi_2_2;
778
  FLOAT32 det;
779
} ia_auto_corr_ele_struct;
780
781
static VOID ixheaacd_esbr_calc_co_variance(
782
    ia_auto_corr_ele_struct *pstr_auto_corr, FLOAT32 vec_x_real[][64],
783
2.74M
    FLOAT32 vec_x_imag[][64], WORD32 bd, WORD32 len) {
784
2.74M
  WORD32 j, jminus1, jminus2;
785
786
2.74M
  memset(pstr_auto_corr, 0, sizeof(ia_auto_corr_ele_struct));
787
788
110M
  for (j = 0; j < len; j++) {
789
108M
    jminus1 = j - 1;
790
108M
    jminus2 = jminus1 - 1;
791
792
108M
    pstr_auto_corr->phi_0_1_real +=
793
108M
        vec_x_real[j][bd] * vec_x_real[jminus1][bd] +
794
108M
        vec_x_imag[j][bd] * vec_x_imag[jminus1][bd];
795
796
108M
    pstr_auto_corr->phi_0_1_imag +=
797
108M
        vec_x_imag[j][bd] * vec_x_real[jminus1][bd] -
798
108M
        vec_x_real[j][bd] * vec_x_imag[jminus1][bd];
799
800
108M
    pstr_auto_corr->phi_0_2_real +=
801
108M
        vec_x_real[j][bd] * vec_x_real[jminus2][bd] +
802
108M
        vec_x_imag[j][bd] * vec_x_imag[jminus2][bd];
803
804
108M
    pstr_auto_corr->phi_0_2_imag +=
805
108M
        vec_x_imag[j][bd] * vec_x_real[jminus2][bd] -
806
108M
        vec_x_real[j][bd] * vec_x_imag[jminus2][bd];
807
808
108M
    pstr_auto_corr->phi_1_1 +=
809
108M
        vec_x_real[jminus1][bd] * vec_x_real[jminus1][bd] +
810
108M
        vec_x_imag[jminus1][bd] * vec_x_imag[jminus1][bd];
811
812
108M
    pstr_auto_corr->phi_1_2_real +=
813
108M
        vec_x_real[jminus1][bd] * vec_x_real[jminus2][bd] +
814
108M
        vec_x_imag[jminus1][bd] * vec_x_imag[jminus2][bd];
815
816
108M
    pstr_auto_corr->phi_1_2_imag +=
817
108M
        vec_x_imag[jminus1][bd] * vec_x_real[jminus2][bd] -
818
108M
        vec_x_real[jminus1][bd] * vec_x_imag[jminus2][bd];
819
820
108M
    pstr_auto_corr->phi_2_2 +=
821
108M
        vec_x_real[jminus2][bd] * vec_x_real[jminus2][bd] +
822
108M
        vec_x_imag[jminus2][bd] * vec_x_imag[jminus2][bd];
823
108M
  }
824
825
2.74M
  pstr_auto_corr->det =
826
2.74M
      pstr_auto_corr->phi_1_1 * pstr_auto_corr->phi_2_2 -
827
2.74M
      (pstr_auto_corr->phi_1_2_real * pstr_auto_corr->phi_1_2_real +
828
2.74M
       pstr_auto_corr->phi_1_2_imag * pstr_auto_corr->phi_1_2_imag) *
829
2.74M
          SBR_HF_RELAXATION_PARAM;
830
2.74M
}
831
832
static void ixheaacd_esbr_chirp_fac_calc(WORD32 *inv_filt_mode,
833
                                         WORD32 *inv_filt_mode_prev,
834
                                         WORD32 num_if_bands, FLOAT32 *bw_array,
835
163k
                                         FLOAT32 *bw_array_prev) {
836
163k
  WORD32 i;
837
838
589k
  for (i = 0; i < num_if_bands; i++) {
839
426k
    bw_array[i] =
840
426k
        ixheaacd_new_bw_table[inv_filt_mode_prev[i]][inv_filt_mode[i]];
841
842
426k
    if (bw_array[i] < bw_array_prev[i])
843
133k
      bw_array[i] = 0.75000f * bw_array[i] + 0.25000f * bw_array_prev[i];
844
292k
    else
845
292k
      bw_array[i] = 0.90625f * bw_array[i] + 0.09375f * bw_array_prev[i];
846
847
426k
    if (bw_array[i] < 0.015625) bw_array[i] = 0;
848
426k
  }
849
163k
}
850
851
static void ixheaacd_gausssolve(WORD32 n, FLOAT32 a[][MAXDEG + 1], FLOAT32 b[],
852
15.4k
                                FLOAT32 y[]) {
853
15.4k
  WORD32 i, j, k, imax;
854
15.4k
  FLOAT32 v;
855
856
77.3k
  for (i = 0; i < n; i++) {
857
61.8k
    imax = i;
858
154k
    for (k = i + 1; k < n; k++) {
859
92.7k
      if (fabs(a[k][i]) > fabs(a[imax][i])) {
860
2.73k
        imax = k;
861
2.73k
      }
862
92.7k
    }
863
61.8k
    if (imax != i) {
864
2.72k
      v = b[imax];
865
2.72k
      b[imax] = b[i];
866
2.72k
      b[i] = v;
867
8.19k
      for (j = i; j < n; j++) {
868
5.46k
        v = a[imax][j];
869
5.46k
        a[imax][j] = a[i][j];
870
5.46k
        a[i][j] = v;
871
5.46k
      }
872
2.72k
    }
873
874
61.8k
    v = a[i][i];
875
876
61.8k
    b[i] /= v;
877
216k
    for (j = i; j < n; j++) {
878
154k
      a[i][j] /= v;
879
154k
    }
880
881
154k
    for (k = i + 1; k < n; k++) {
882
92.7k
      v = a[k][i];
883
92.7k
      b[k] -= v * b[i];
884
309k
      for (j = i + 1; j < n; j++) {
885
216k
        a[k][j] -= v * a[i][j];
886
216k
      }
887
92.7k
    }
888
61.8k
  }
889
890
77.3k
  for (i = n - 1; i >= 0; i--) {
891
61.8k
    y[i] = b[i];
892
154k
    for (j = i + 1; j < n; j++) {
893
92.7k
      y[i] -= a[i][j] * y[j];
894
92.7k
    }
895
61.8k
  }
896
15.4k
}
897
898
15.4k
void ixheaacd_polyfit(WORD32 n, FLOAT32 y[], FLOAT32 p[]) {
899
15.4k
  WORD32 i, j, k;
900
15.4k
  FLOAT32 a[MAXDEG + 1][MAXDEG + 1];
901
15.4k
  FLOAT32 b[MAXDEG + 1];
902
15.4k
  FLOAT32 v[2 * MAXDEG + 1];
903
904
77.3k
  for (i = 0; i <= MAXDEG; i++) {
905
61.8k
    b[i] = 0.0f;
906
309k
    for (j = 0; j <= MAXDEG; j++) {
907
247k
      a[i][j] = 0.0f;
908
247k
    }
909
61.8k
  }
910
911
280k
  for (k = 0; k < n; k++) {
912
264k
    v[0] = 1.0;
913
1.85M
    for (i = 1; i <= 2 * MAXDEG; i++) {
914
1.58M
      v[i] = k * v[i - 1];
915
1.58M
    }
916
917
1.32M
    for (i = 0; i <= MAXDEG; i++) {
918
1.05M
      b[i] += v[MAXDEG - i] * y[k];
919
5.29M
      for (j = 0; j <= MAXDEG; j++) {
920
4.23M
        a[i][j] += v[2 * MAXDEG - i - j];
921
4.23M
      }
922
1.05M
    }
923
264k
  }
924
925
15.4k
  ixheaacd_gausssolve(MAXDEG + 1, a, b, p);
926
15.4k
}
927
928
VOID ixheaacd_pre_processing(FLOAT32 ptr_src_buf_real[][64],
929
                             FLOAT32 ptr_src_buf_imag[][64],
930
                             FLOAT32 gain_vector[], WORD32 num_bands,
931
15.4k
                             WORD32 start_sample, WORD32 end_sample) {
932
15.4k
  WORD32 k, i;
933
15.4k
  FLOAT32 poly_coeff[4];
934
15.4k
  FLOAT32 mean_enrg = 0;
935
15.4k
  FLOAT32 low_env_slope[64];
936
15.4k
  FLOAT32 low_env[64] = {0};
937
15.4k
  FLOAT32 a0;
938
15.4k
  FLOAT32 a1;
939
15.4k
  FLOAT32 a2;
940
15.4k
  FLOAT32 a3;
941
942
15.4k
  if (num_bands != 0 && end_sample != start_sample) {
943
276k
    for (k = 0; k < num_bands; k++) {
944
261k
      FLOAT32 temp = 0;
945
8.76M
      for (i = start_sample; i < end_sample; i++) {
946
8.50M
        temp += ptr_src_buf_real[i][k] * ptr_src_buf_real[i][k] +
947
8.50M
                ptr_src_buf_imag[i][k] * ptr_src_buf_imag[i][k];
948
8.50M
      }
949
261k
      temp /= (end_sample - start_sample);
950
261k
      low_env[k] = (FLOAT32)(10 * log10(temp + 1));
951
261k
      mean_enrg = mean_enrg + low_env[k];
952
261k
    }
953
15.3k
    mean_enrg /= num_bands;
954
15.3k
  }
955
956
15.4k
  ixheaacd_polyfit(num_bands, low_env, poly_coeff);
957
958
15.4k
  a0 = poly_coeff[0];
959
15.4k
  a1 = poly_coeff[1];
960
15.4k
  a2 = poly_coeff[2];
961
15.4k
  a3 = poly_coeff[3];
962
280k
  for (k = 0; k < num_bands; k++) {
963
264k
    FLOAT32 x_low_l = (FLOAT32)k;
964
264k
    FLOAT32 low_env_slope_l = a3;
965
264k
    low_env_slope_l = low_env_slope_l + a2 * x_low_l;
966
967
264k
    x_low_l = x_low_l * x_low_l;
968
264k
    low_env_slope_l = low_env_slope_l + a1 * x_low_l;
969
970
264k
    x_low_l = x_low_l * (FLOAT32)k;
971
264k
    low_env_slope_l = low_env_slope_l + a0 * x_low_l;
972
973
264k
    low_env_slope[k] = low_env_slope_l;
974
264k
  }
975
976
280k
  for (i = 0; i < num_bands; i++) {
977
264k
    gain_vector[i] = (FLOAT32)pow(10, (mean_enrg - low_env_slope[i]) / 20.0f);
978
264k
  }
979
15.4k
}
980
981
WORD32 ixheaacd_generate_hf(FLOAT32 ptr_src_buf_real[][64], FLOAT32 ptr_src_buf_imag[][64],
982
                            FLOAT32 ptr_ph_vocod_buf_real[][64],
983
                            FLOAT32 ptr_ph_vocod_buf_imag[][64], FLOAT32 ptr_dst_buf_real[][64],
984
                            FLOAT32 ptr_dst_buf_imag[][64],
985
                            ia_sbr_frame_info_data_struct *ptr_frame_data,
986
                            ia_sbr_header_data_struct *ptr_header_data, WORD32 ldmps_present,
987
163k
                            WORD32 time_slots, WORD32 ec_flag) {
988
163k
  WORD32 bw_index, i, k, k2, patch = 0;
989
163k
  WORD32 co_var_len;
990
163k
  WORD32 start_sample, end_sample, goal_sb;
991
163k
  WORD32 sb, source_start_band, patch_stride, num_bands_in_patch;
992
163k
  WORD32 hbe_flag = ptr_header_data->hbe_flag;
993
163k
  FLOAT32 a0r, a0i, a1r, a1i;
994
163k
  FLOAT32 bw_array[MAX_NUM_PATCHES] = {0};
995
996
163k
  ia_auto_corr_ele_struct str_auto_corr;
997
998
163k
  WORD16 *ptr_invf_band_tbl =
999
163k
      &ptr_header_data->pstr_freq_band_data
1000
163k
           ->freq_band_tbl_noise[1];  // offest 1 used as base address of
1001
                                      // ptr_invf_band_tbl
1002
163k
  WORD32 num_if_bands = ptr_header_data->pstr_freq_band_data->num_nf_bands;
1003
163k
  WORD32 sub_band_start = ptr_header_data->pstr_freq_band_data->sub_band_start;
1004
163k
  WORD16 *f_master_tbl = ptr_header_data->pstr_freq_band_data->f_master_tbl;
1005
163k
  WORD32 num_mf_bands = ptr_header_data->pstr_freq_band_data->num_mf_bands;
1006
163k
  WORD32 *inv_filt_mode = ptr_frame_data->sbr_invf_mode;
1007
163k
  WORD32 *inv_filt_mode_prev = ptr_frame_data->sbr_invf_mode_prev;
1008
163k
  WORD32 sbr_patching_mode = ptr_frame_data->sbr_patching_mode;
1009
163k
  ia_frame_info_struct *p_frame_info = &ptr_frame_data->str_frame_info_details;
1010
163k
  WORD32 pre_proc_flag = ptr_header_data->pre_proc_flag;
1011
163k
  WORD32 is_usf_4 = ptr_header_data->is_usf_4;
1012
163k
  WORD32 fs = ptr_header_data->out_sampling_freq;
1013
163k
  WORD32 cov_count;
1014
163k
  WORD32 lsb = f_master_tbl[0];
1015
163k
  WORD32 usb = f_master_tbl[num_mf_bands];
1016
163k
  WORD32 xover_offset = sub_band_start - f_master_tbl[0];
1017
1018
163k
  FLOAT32 bw = 0.0f;
1019
163k
  FLOAT32 fac = 0.0f;
1020
1021
163k
  FLOAT32 gain;
1022
163k
  FLOAT32 gain_vector[64];
1023
1024
163k
  WORD32 slope_length = 0;
1025
163k
  WORD32 first_slot_offset = p_frame_info->border_vec[0];
1026
163k
  WORD32 end_slot_offs = 0;
1027
1028
163k
  FLOAT32 *bw_array_prev = ptr_frame_data->bw_array_prev;
1029
1030
163k
  end_slot_offs = p_frame_info->border_vec[p_frame_info->num_env] - 16;
1031
1032
163k
  if (ldmps_present == 1)
1033
3.36k
    end_slot_offs =
1034
3.36k
        p_frame_info->border_vec[p_frame_info->num_env] - time_slots;
1035
1036
163k
  if (is_usf_4) {
1037
18.8k
    start_sample = first_slot_offset * 4;
1038
18.8k
    end_sample = 64 + end_slot_offs * 4;
1039
18.8k
    co_var_len = 76;
1040
144k
  } else {
1041
144k
    start_sample = first_slot_offset * 2;
1042
144k
    end_sample = 32 + end_slot_offs * 2;
1043
144k
    co_var_len = 38;
1044
144k
  }
1045
1046
163k
  if (ldmps_present == 1) {
1047
3.36k
    start_sample = 0;
1048
3.36k
    end_sample = time_slots;
1049
3.36k
    co_var_len = time_slots;
1050
3.36k
  }
1051
1052
163k
  if (pre_proc_flag) {
1053
15.4k
    ixheaacd_pre_processing(ptr_src_buf_real, ptr_src_buf_imag, gain_vector,
1054
15.4k
                            f_master_tbl[0], start_sample, end_sample);
1055
15.4k
  }
1056
1057
163k
  ixheaacd_esbr_chirp_fac_calc(inv_filt_mode, inv_filt_mode_prev, num_if_bands,
1058
163k
                               bw_array, bw_array_prev);
1059
1060
5.93M
  for (i = start_sample; i < end_sample; i++) {
1061
5.77M
    memset(ptr_dst_buf_real[i] + usb, 0, (64 - usb) * sizeof(FLOAT32));
1062
5.77M
    memset(ptr_dst_buf_imag[i] + usb, 0, (64 - usb) * sizeof(FLOAT32));
1063
5.77M
  }
1064
1065
163k
  if (sbr_patching_mode || !hbe_flag) {
1066
89.3k
    WORD32 flag_break = 0;
1067
89.3k
    FLOAT32 alpha_real[64][2] = {{0}}, alpha_imag[64][2] = {{0}};
1068
89.3k
    if (ptr_frame_data->mps_sbr_flag) {
1069
36.6k
      cov_count = (f_master_tbl[0] < ptr_frame_data->cov_count)
1070
36.6k
                      ? f_master_tbl[0]
1071
36.6k
                      : ptr_frame_data->cov_count;
1072
52.7k
    } else {
1073
52.7k
      cov_count = f_master_tbl[0];
1074
52.7k
    }
1075
1076
1.43M
    for (k = 1; k < cov_count; k++) {
1077
1.34M
      ixheaacd_esbr_calc_co_variance(&str_auto_corr, &ptr_src_buf_real[0],
1078
1.34M
                                     &ptr_src_buf_imag[0], k, co_var_len);
1079
1.34M
      if (str_auto_corr.det == 0.0f) {
1080
528k
        alpha_real[k][1] = alpha_imag[k][1] = 0;
1081
819k
      } else {
1082
819k
        fac = 1.0f / str_auto_corr.det;
1083
819k
        alpha_real[k][1] =
1084
819k
            (str_auto_corr.phi_0_1_real * str_auto_corr.phi_1_2_real -
1085
819k
             str_auto_corr.phi_0_1_imag * str_auto_corr.phi_1_2_imag -
1086
819k
             str_auto_corr.phi_0_2_real * str_auto_corr.phi_1_1) *
1087
819k
            fac;
1088
819k
        alpha_imag[k][1] =
1089
819k
            (str_auto_corr.phi_0_1_imag * str_auto_corr.phi_1_2_real +
1090
819k
             str_auto_corr.phi_0_1_real * str_auto_corr.phi_1_2_imag -
1091
819k
             str_auto_corr.phi_0_2_imag * str_auto_corr.phi_1_1) *
1092
819k
            fac;
1093
819k
      }
1094
1095
1.34M
      if (str_auto_corr.phi_1_1 == 0) {
1096
527k
        alpha_real[k][0] = alpha_imag[k][0] = 0;
1097
820k
      } else {
1098
820k
        fac = 1.0f / str_auto_corr.phi_1_1;
1099
820k
        alpha_real[k][0] = -(str_auto_corr.phi_0_1_real +
1100
820k
                             alpha_real[k][1] * str_auto_corr.phi_1_2_real +
1101
820k
                             alpha_imag[k][1] * str_auto_corr.phi_1_2_imag) *
1102
820k
                           fac;
1103
820k
        alpha_imag[k][0] = -(str_auto_corr.phi_0_1_imag +
1104
820k
                             alpha_imag[k][1] * str_auto_corr.phi_1_2_real -
1105
820k
                             alpha_real[k][1] * str_auto_corr.phi_1_2_imag) *
1106
820k
                           fac;
1107
820k
      }
1108
1109
1.34M
      if ((alpha_real[k][0] * alpha_real[k][0] +
1110
1.34M
               alpha_imag[k][0] * alpha_imag[k][0] >=
1111
1.34M
           16.0f) ||
1112
1.34M
          (alpha_real[k][1] * alpha_real[k][1] +
1113
1.34M
               alpha_imag[k][1] * alpha_imag[k][1] >=
1114
1.34M
           16.0f)) {
1115
2.39k
        alpha_real[k][0] = 0.0f;
1116
2.39k
        alpha_imag[k][0] = 0.0f;
1117
2.39k
        alpha_real[k][1] = 0.0f;
1118
2.39k
        alpha_imag[k][1] = 0.0f;
1119
2.39k
      }
1120
1.34M
    }
1121
1122
89.3k
    goal_sb = (WORD32)(2.048e6f / fs + 0.5f);
1123
89.3k
    {
1124
89.3k
      WORD32 index;
1125
89.3k
      if (goal_sb < f_master_tbl[num_mf_bands]) {
1126
40.2k
        for (index = 0; (f_master_tbl[index] < goal_sb); index++)
1127
35.0k
          ;
1128
5.16k
        goal_sb = f_master_tbl[index];
1129
84.2k
      } else {
1130
84.2k
        goal_sb = f_master_tbl[num_mf_bands];
1131
84.2k
      }
1132
89.3k
    }
1133
1134
89.3k
    source_start_band = xover_offset + 1;
1135
89.3k
    sb = lsb + xover_offset;
1136
1137
89.3k
    patch = 0;
1138
335k
    while (sb < usb) {
1139
273k
      if (MAX_NUM_PATCHES <= patch) {
1140
17
        if (ec_flag) {
1141
0
          break;
1142
17
        } else {
1143
17
          return -1;
1144
17
        }
1145
17
      }
1146
273k
      ptr_frame_data->patch_param.start_subband[patch] = sb;
1147
273k
      num_bands_in_patch = goal_sb - sb;
1148
1149
273k
      if (num_bands_in_patch >= lsb - source_start_band) {
1150
189k
        patch_stride = sb - source_start_band;
1151
189k
        patch_stride = patch_stride & ~1;
1152
189k
        num_bands_in_patch = lsb - (sb - patch_stride);
1153
189k
        num_bands_in_patch =
1154
189k
            ixheaacd_find_closest_entry(sb + num_bands_in_patch, f_master_tbl,
1155
189k
                                        (WORD16)(num_mf_bands), 0) -
1156
189k
            (WORD32)(sb);
1157
189k
      }
1158
1159
273k
      patch_stride = num_bands_in_patch + sb - lsb;
1160
273k
      patch_stride = (patch_stride + 1) & ~1;
1161
1162
273k
      source_start_band = 1;
1163
1164
273k
      if (goal_sb - (sb + num_bands_in_patch) < 3) {
1165
116k
        goal_sb = usb;
1166
116k
      }
1167
1168
273k
      if ((num_bands_in_patch < 3) && (patch > 0) &&
1169
27.4k
          (sb + num_bands_in_patch == usb)) {
1170
868k
        for (i = start_sample + slope_length; i < end_sample + slope_length;
1171
842k
             i++) {
1172
2.52M
          for (k2 = sb; k2 < sb + num_bands_in_patch; k2++) {
1173
1.68M
            ptr_dst_buf_real[i][k2] = 0.0f;
1174
1.68M
            ptr_dst_buf_imag[i][k2] = 0.0f;
1175
1.68M
          }
1176
842k
        }
1177
1178
25.9k
        break;
1179
25.9k
      }
1180
1181
247k
      if (num_bands_in_patch < 0 && flag_break == 1) {
1182
1.06k
        break;
1183
1.06k
      }
1184
1185
246k
      if (num_bands_in_patch < 0) {
1186
6.55k
        flag_break = 1;
1187
6.55k
        continue;
1188
239k
      } else {
1189
239k
        flag_break = 0;
1190
239k
      }
1191
1192
2.42M
      for (k2 = sb; k2 < sb + num_bands_in_patch; k2++) {
1193
2.18M
        k = k2 - patch_stride;
1194
2.18M
        bw_index = 0;
1195
5.18M
        while (k2 >= ptr_invf_band_tbl[bw_index]) {
1196
2.99M
          bw_index++;
1197
2.99M
          if (bw_index >= MAX_NOISE_COEFFS) {
1198
0
            if (ec_flag) {
1199
0
              bw_index = MAX_NOISE_COEFFS - 1;
1200
0
              break;
1201
0
            } else
1202
0
              return -1;
1203
0
          }
1204
2.99M
        }
1205
1206
2.18M
        if (bw_index >= MAX_NUM_PATCHES) {
1207
0
          if (ec_flag)
1208
0
            bw_index = MAX_NUM_PATCHES - 1;
1209
0
          else
1210
0
            return -1;
1211
0
        }
1212
2.18M
        bw = bw_array[bw_index];
1213
1214
2.18M
        a0r = bw * alpha_real[k][0];
1215
2.18M
        a0i = bw * alpha_imag[k][0];
1216
2.18M
        bw *= bw;
1217
2.18M
        a1r = bw * alpha_real[k][1];
1218
2.18M
        a1i = bw * alpha_imag[k][1];
1219
1220
2.18M
        if (pre_proc_flag) {
1221
241k
          gain = gain_vector[k];
1222
1.94M
        } else {
1223
1.94M
          gain = 1.0f;
1224
1.94M
        }
1225
1226
71.9M
        for (i = start_sample + slope_length; i < end_sample + slope_length;
1227
69.7M
             i++) {
1228
69.7M
          ptr_dst_buf_real[i][k2] = ptr_src_buf_real[i][k] * gain;
1229
1230
69.7M
          ptr_dst_buf_imag[i][k2] = ptr_src_buf_imag[i][k] * gain;
1231
1232
69.7M
          if (bw > 0.0f) {
1233
48.1M
            ptr_dst_buf_real[i][k2] += (a0r * ptr_src_buf_real[i - 1][k] -
1234
48.1M
                                        a0i * ptr_src_buf_imag[i - 1][k] +
1235
48.1M
                                        a1r * ptr_src_buf_real[i - 2][k] -
1236
48.1M
                                        a1i * ptr_src_buf_imag[i - 2][k]) *
1237
48.1M
                                       gain;
1238
48.1M
            ptr_dst_buf_imag[i][k2] += (a0i * ptr_src_buf_real[i - 1][k] +
1239
48.1M
                                        a0r * ptr_src_buf_imag[i - 1][k] +
1240
48.1M
                                        a1i * ptr_src_buf_real[i - 2][k] +
1241
48.1M
                                        a1r * ptr_src_buf_imag[i - 2][k]) *
1242
48.1M
                                       gain;
1243
48.1M
          }
1244
69.7M
        }
1245
2.18M
      }
1246
239k
      sb += num_bands_in_patch;
1247
239k
      patch++;
1248
239k
    }
1249
89.3k
  }
1250
1251
163k
  if (NULL != ptr_ph_vocod_buf_real && NULL != ptr_ph_vocod_buf_imag) {
1252
108k
    if (hbe_flag && !sbr_patching_mode) {
1253
56.0k
      FLOAT32 alpha_real[2], alpha_imag[2];
1254
1255
56.0k
      bw_index = 0, patch = 1;
1256
1257
1.45M
      for (k2 = sub_band_start; k2 < f_master_tbl[num_mf_bands]; k2++) {
1258
1.39M
        ixheaacd_esbr_calc_co_variance(&str_auto_corr, &ptr_ph_vocod_buf_real[0],
1259
1.39M
                                       &ptr_ph_vocod_buf_imag[0], k2, co_var_len);
1260
1261
1.39M
        if (str_auto_corr.det == 0.0f) {
1262
635k
          alpha_real[1] = alpha_imag[1] = 0;
1263
759k
        } else {
1264
759k
          fac = 1.0f / str_auto_corr.det;
1265
759k
          alpha_real[1] = (str_auto_corr.phi_0_1_real * str_auto_corr.phi_1_2_real -
1266
759k
                           str_auto_corr.phi_0_1_imag * str_auto_corr.phi_1_2_imag -
1267
759k
                           str_auto_corr.phi_0_2_real * str_auto_corr.phi_1_1) *
1268
759k
                          fac;
1269
759k
          alpha_imag[1] = (str_auto_corr.phi_0_1_imag * str_auto_corr.phi_1_2_real +
1270
759k
                           str_auto_corr.phi_0_1_real * str_auto_corr.phi_1_2_imag -
1271
759k
                           str_auto_corr.phi_0_2_imag * str_auto_corr.phi_1_1) *
1272
759k
                          fac;
1273
759k
        }
1274
1275
1.39M
        if (str_auto_corr.phi_1_1 == 0) {
1276
627k
          alpha_real[0] = alpha_imag[0] = 0;
1277
767k
        } else {
1278
767k
          fac = 1.0f / str_auto_corr.phi_1_1;
1279
767k
          alpha_real[0] =
1280
767k
              -(str_auto_corr.phi_0_1_real + alpha_real[1] * str_auto_corr.phi_1_2_real +
1281
767k
                alpha_imag[1] * str_auto_corr.phi_1_2_imag) *
1282
767k
              fac;
1283
767k
          alpha_imag[0] =
1284
767k
              -(str_auto_corr.phi_0_1_imag + alpha_imag[1] * str_auto_corr.phi_1_2_real -
1285
767k
                alpha_real[1] * str_auto_corr.phi_1_2_imag) *
1286
767k
              fac;
1287
767k
        }
1288
1289
1.39M
        if (alpha_real[0] * alpha_real[0] + alpha_imag[0] * alpha_imag[0] >= 16.0f ||
1290
1.38M
            alpha_real[1] * alpha_real[1] + alpha_imag[1] * alpha_imag[1] >= 16.0f) {
1291
24.9k
          alpha_real[0] = 0.0f;
1292
24.9k
          alpha_imag[0] = 0.0f;
1293
24.9k
          alpha_real[1] = 0.0f;
1294
24.9k
          alpha_imag[1] = 0.0f;
1295
24.9k
        }
1296
1297
1.51M
        while (k2 >= ptr_invf_band_tbl[bw_index]) {
1298
118k
          bw_index++;
1299
118k
          if (bw_index >= MAX_NOISE_COEFFS) {
1300
0
            if (ec_flag) {
1301
0
              bw_index = MAX_NOISE_COEFFS - 1;
1302
0
              break;
1303
0
            } else
1304
0
              return -1;
1305
0
          }
1306
118k
        }
1307
1308
1.39M
        if (bw_index >= MAX_NUM_PATCHES) {
1309
0
          if (ec_flag)
1310
0
            bw_index = MAX_NUM_PATCHES - 1;
1311
0
          else
1312
0
            return -1;
1313
0
        }
1314
1.39M
        bw = bw_array[bw_index];
1315
1316
1.39M
        a0r = bw * alpha_real[0];
1317
1.39M
        a0i = bw * alpha_imag[0];
1318
1.39M
        bw *= bw;
1319
1.39M
        a1r = bw * alpha_real[1];
1320
1.39M
        a1i = bw * alpha_imag[1];
1321
1322
1.39M
        if (bw > 0.0f) {
1323
26.4M
          for (i = start_sample; i < end_sample; i++) {
1324
25.6M
            FLOAT32 real1, imag1, real2, imag2, realTarget, imag_target;
1325
1326
25.6M
            realTarget = ptr_ph_vocod_buf_real[i][k2];
1327
25.6M
            imag_target = ptr_ph_vocod_buf_imag[i][k2];
1328
25.6M
            real1 = ptr_ph_vocod_buf_real[i - 1][k2];
1329
25.6M
            imag1 = ptr_ph_vocod_buf_imag[i - 1][k2];
1330
25.6M
            real2 = ptr_ph_vocod_buf_real[i - 2][k2];
1331
25.6M
            imag2 = ptr_ph_vocod_buf_imag[i - 2][k2];
1332
25.6M
            realTarget += ((a0r * real1 - a0i * imag1) + (a1r * real2 - a1i * imag2));
1333
25.6M
            imag_target += ((a0i * real1 + a0r * imag1) + (a1i * real2 + a1r * imag2));
1334
1335
25.6M
            ptr_dst_buf_real[i][k2] = realTarget;
1336
25.6M
            ptr_dst_buf_imag[i][k2] = imag_target;
1337
25.6M
          }
1338
761k
        } else {
1339
23.6M
          for (i = start_sample; i < end_sample; i++) {
1340
23.0M
            ptr_dst_buf_real[i][k2] = ptr_ph_vocod_buf_real[i][k2];
1341
23.0M
            ptr_dst_buf_imag[i][k2] = ptr_ph_vocod_buf_imag[i][k2];
1342
23.0M
          }
1343
633k
        }
1344
1.39M
      }
1345
56.0k
    }
1346
108k
  }
1347
163k
  if ((MAX_NUM_PATCHES + 1) <= patch) {
1348
0
    if (ec_flag) {
1349
0
      patch = MAX_NUM_PATCHES;
1350
0
    } else {
1351
0
      return -1;
1352
0
    }
1353
0
  }
1354
163k
  ptr_frame_data->patch_param.num_patches = patch;
1355
589k
  for (i = 0; i < num_if_bands; i++) {
1356
426k
    bw_array_prev[i] = bw_array[i];
1357
426k
  }
1358
163k
  return 0;
1359
163k
}