Coverage Report

Created: 2025-08-26 06:53

/src/libxaac/encoder/ixheaace_ps_enc.c
Line
Count
Source (jump to first uncovered line)
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
24
#include "ixheaac_type_def.h"
25
#include "ixheaac_error_standards.h"
26
#include "ixheaace_sbr_def.h"
27
#include "ixheaace_resampler.h"
28
#include "ixheaace_sbr_rom.h"
29
#include "ixheaace_common_rom.h"
30
#include "ixheaace_bitbuffer.h"
31
#include "ixheaace_sbr_hybrid.h"
32
#include "ixheaace_sbr_ps_enc.h"
33
#include "ixheaac_constants.h"
34
#include "ixheaace_aac_constants.h"
35
36
1.06M
static VOID ixheaace_memcpy(void *dest, const void *src, WORD32 count) {
37
1.06M
  memcpy(dest, src, count);
38
1.06M
}
39
40
static VOID ia_enhaacplus_enc_downmix_to_mono(ixheaace_pstr_ps_enc pms, FLOAT32 **qmf_left_real,
41
                                              FLOAT32 **qmf_left_imag, FLOAT32 **qmf_right_real,
42
                                              FLOAT32 **qmf_right_imag,
43
29.4k
                                              ixheaace_str_ps_tab *ps_tables) {
44
29.4k
  WORD32 i;
45
29.4k
  WORD32 group;
46
29.4k
  WORD32 subband;
47
29.4k
  WORD32 max_subband;
48
29.4k
  WORD32 subband_grp;
49
50
29.4k
  FLOAT32 temp_left_real;
51
29.4k
  FLOAT32 temp_left_imag;
52
29.4k
  FLOAT32 temp_right_real;
53
29.4k
  FLOAT32 temp_right_imag;
54
29.4k
  FLOAT32 temp, temp2;
55
56
206k
  for (i = 0; i < IXHEAACE_HYBRID_FILTER_DELAY; i++) {
57
176k
    ixheaace_memcpy(pms->temp_qmf_left_real[i],
58
176k
                    qmf_left_real[NUMBER_OF_SUBSAMPLES - IXHEAACE_HYBRID_FILTER_DELAY + i],
59
176k
                    NUMBER_OF_QMF_BANDS * sizeof(**qmf_left_real));
60
176k
    ixheaace_memcpy(pms->temp_qmf_left_imag[i],
61
176k
                    qmf_left_imag[NUMBER_OF_SUBSAMPLES - IXHEAACE_HYBRID_FILTER_DELAY + i],
62
176k
                    NUMBER_OF_QMF_BANDS * sizeof(**qmf_left_imag));
63
176k
  }
64
65
29.4k
  {
66
29.4k
    FLOAT32 *hybrid_left_real_init, *hybrid_right_real_init;
67
29.4k
    hybrid_left_real_init = pms->m_hybrid_real_left[0];
68
29.4k
    hybrid_right_real_init = pms->m_hybrid_real_right[0];
69
70
501k
    for (group = 0; group < SUBQMF_GROUPS_MIX; group++) {
71
471k
      FLOAT32 *hybrid_left_real, *hybrid_right_real;
72
73
471k
      subband = ps_tables->grp_borders_mix[group];
74
471k
      hybrid_left_real = hybrid_left_real_init + subband;
75
471k
      hybrid_right_real = hybrid_right_real_init + subband;
76
77
15.5M
      for (i = NUMBER_OF_SUBSAMPLES; i != 0; i--) {
78
15.0M
        FLOAT32 temp_left_real_1, temp_left_imag_1;
79
80
15.0M
        temp_left_real = hybrid_left_real[0];
81
15.0M
        hybrid_left_real += 32;
82
15.0M
        temp_left_imag = hybrid_left_real[-16];
83
15.0M
        temp_right_real = hybrid_right_real[0];
84
15.0M
        hybrid_right_real += 32;
85
15.0M
        temp_right_imag = hybrid_right_real[-16];
86
87
15.0M
        temp_left_real_1 = temp_right_real + temp_left_real;
88
15.0M
        temp_left_imag_1 = temp_right_imag + temp_left_imag;
89
90
15.0M
        temp2 = temp_left_real_1 * temp_left_real_1;
91
15.0M
        temp2 += temp_left_imag_1 * temp_left_imag_1;
92
15.0M
        temp2 /= 2.0f;
93
94
15.0M
        temp = temp_left_real * temp_right_real;
95
15.0M
        temp += temp_left_imag * temp_right_imag;
96
97
15.0M
        temp = temp2 - temp;
98
99
15.0M
        if ((temp / 8.0f) >= temp2) {
100
4.61M
          temp_left_real = temp_left_real_1 * 2.0f;
101
4.61M
          temp_left_imag = temp_left_imag_1 * 2.0f;
102
10.4M
        } else {
103
10.4M
          temp = (FLOAT32)sqrt(temp / (temp2 * 2.0f));
104
10.4M
          temp_left_real = temp_left_real_1 * temp;
105
10.4M
          temp_left_imag = temp_left_imag_1 * temp;
106
10.4M
        }
107
108
15.0M
        hybrid_left_real[-32] = temp_left_real;
109
15.0M
        hybrid_left_real[-16] = temp_left_imag;
110
15.0M
      }
111
471k
    }
112
29.4k
  }
113
114
383k
  for (; group < NUMBER_OF_IPD_GROUPS; group++) {
115
353k
    FLOAT32 *hybrid_left_real;
116
353k
    FLOAT32 *qmf_lre = qmf_left_real[NUMBER_OF_SUBSAMPLES - 1];
117
118
353k
    subband_grp = ps_tables->grp_borders_mix[group];
119
353k
    max_subband = ps_tables->grp_borders_mix[group + 1];
120
121
353k
    hybrid_left_real =
122
353k
        (qmf_left_real - IXHEAACE_HYBRID_FILTER_DELAY)[NUMBER_OF_SUBSAMPLES - 1] + 0X1000;
123
353k
    hybrid_left_real += subband_grp;
124
353k
    qmf_lre += subband_grp;
125
126
9.55M
    for (i = NUMBER_OF_SUBSAMPLES - 1; i >= IXHEAACE_HYBRID_FILTER_DELAY; i--) {
127
55.9M
      for (subband = max_subband - subband_grp; subband != 0; subband--) {
128
46.7M
        FLOAT32 temp_left_real_1, temp_left_imag_1;
129
130
46.7M
        temp_left_real = hybrid_left_real[-0X1000];
131
46.7M
        temp_left_imag = hybrid_left_real[0];
132
46.7M
        temp_right_real = hybrid_left_real[-0X800];
133
46.7M
        temp_right_imag = hybrid_left_real[0X800];
134
135
46.7M
        hybrid_left_real += 1;
136
137
46.7M
        temp_left_real_1 = temp_right_real + temp_left_real;
138
46.7M
        temp_left_imag_1 = temp_right_imag + temp_left_imag;
139
140
46.7M
        temp2 = temp_left_real_1 * temp_left_real_1;
141
46.7M
        temp2 += temp_left_imag_1 * temp_left_imag_1;
142
46.7M
        temp2 /= 2.0f;
143
144
46.7M
        temp = temp_left_real * temp_right_real;
145
46.7M
        temp += temp_left_imag * temp_right_imag;
146
147
46.7M
        temp = temp2 - temp;
148
149
46.7M
        if ((temp / 8.0f) >= temp2) {
150
14.6M
          temp_left_real = temp_left_real_1 * 2.0f;
151
14.6M
          temp_left_imag = temp_left_imag_1 * 2.0f;
152
32.0M
        } else {
153
32.0M
          temp = (FLOAT32)sqrt(temp / (2.0f * temp2));
154
32.0M
          temp_left_real = temp_left_real_1 * temp;
155
32.0M
          temp_left_imag = temp_left_imag_1 * temp;
156
32.0M
        }
157
158
46.7M
        qmf_lre[0] = temp_left_real;
159
46.7M
        qmf_lre[0 + 0x1000] = temp_left_imag;
160
46.7M
        qmf_lre += 1;
161
46.7M
      }
162
163
9.20M
      hybrid_left_real -= max_subband - subband_grp;
164
9.20M
      qmf_lre -= max_subband - subband_grp;
165
9.20M
      qmf_lre -= 64;
166
9.20M
      hybrid_left_real -= 64;
167
9.20M
    }
168
169
353k
    hybrid_left_real = pms->hist_qmf_left_real[IXHEAACE_HYBRID_FILTER_DELAY - 1];
170
353k
    hybrid_left_real += subband_grp;
171
172
2.47M
    for (i = IXHEAACE_HYBRID_FILTER_DELAY - 1; i >= 0; i--) {
173
12.9M
      for (subband = max_subband - subband_grp; subband != 0; subband--) {
174
10.7M
        FLOAT32 temp_left_real_1, temp_left_imag_1;
175
176
10.7M
        temp_left_real = hybrid_left_real[0];
177
10.7M
        temp_left_imag = hybrid_left_real[0 + 0x40];
178
10.7M
        temp_right_real = hybrid_left_real[0 + 0x80];
179
10.7M
        temp_right_imag = hybrid_left_real[0 + 0xc0];
180
181
10.7M
        temp_left_real_1 = temp_right_real + temp_left_real;
182
10.7M
        temp_left_imag_1 = temp_right_imag + temp_left_imag;
183
184
10.7M
        temp2 = temp_left_real_1 * temp_left_real_1;
185
10.7M
        temp2 += temp_left_imag_1 * temp_left_imag_1;
186
10.7M
        temp2 /= 2.0f;
187
188
10.7M
        temp = temp_left_real * temp_right_real;
189
10.7M
        temp += temp_left_imag * temp_right_imag;
190
191
10.7M
        temp = temp2 - temp;
192
193
10.7M
        if ((temp / 8.0f) >= temp2) {
194
4.68M
          temp_left_real = temp_left_real_1 * 2.0f;
195
4.68M
          temp_left_imag = temp_left_imag_1 * 2.0f;
196
6.10M
        } else {
197
6.10M
          temp = (FLOAT32)sqrt(temp / (2.0f * temp2));
198
6.10M
          temp_left_real = temp_left_real_1 * temp;
199
6.10M
          temp_left_imag = temp_left_imag_1 * temp;
200
6.10M
        }
201
202
10.7M
        qmf_lre[0] = temp_left_real;
203
10.7M
        qmf_lre[0x1000] = temp_left_imag;
204
10.7M
        qmf_lre += 1;
205
10.7M
        hybrid_left_real += 1;
206
10.7M
      }
207
2.12M
      qmf_lre -= max_subband - subband_grp;
208
2.12M
      hybrid_left_real -= max_subband - subband_grp;
209
2.12M
      qmf_lre -= 64;
210
2.12M
      hybrid_left_real -= 0x100;
211
2.12M
    }
212
353k
  }
213
214
206k
  for (i = 0; i < IXHEAACE_HYBRID_FILTER_DELAY; i++) {
215
176k
    ixheaace_memcpy(pms->hist_qmf_left_real[i], pms->temp_qmf_left_real[i],
216
176k
                    NUMBER_OF_QMF_BANDS * sizeof(**qmf_left_real));
217
176k
    ixheaace_memcpy(pms->hist_qmf_left_imag[i], pms->temp_qmf_left_imag[i],
218
176k
                    NUMBER_OF_QMF_BANDS * sizeof(**qmf_left_imag));
219
176k
    ixheaace_memcpy(pms->hist_qmf_right_real[i],
220
176k
                    qmf_right_real[NUMBER_OF_SUBSAMPLES - IXHEAACE_HYBRID_FILTER_DELAY + i],
221
176k
                    NUMBER_OF_QMF_BANDS * sizeof(**qmf_right_real));
222
176k
    ixheaace_memcpy(pms->hist_qmf_right_imag[i],
223
176k
                    qmf_right_imag[NUMBER_OF_SUBSAMPLES - IXHEAACE_HYBRID_FILTER_DELAY + i],
224
176k
                    NUMBER_OF_QMF_BANDS * sizeof(**qmf_right_imag));
225
176k
  }
226
227
29.4k
  ixheaace_hybrid_synthesis((const FLOAT32 **)pms->m_hybrid_real_left,
228
29.4k
                            (const FLOAT32 **)pms->m_hybrid_imag_left, qmf_left_real,
229
29.4k
                            qmf_left_imag, ps_tables->a_hyb_res);
230
29.4k
}
231
232
IA_ERRORCODE ixheaace_encode_ps_frame(ixheaace_pstr_ps_enc pms, FLOAT32 **i_buffer_left,
233
                                      FLOAT32 **r_buffer_left, FLOAT32 **i_buffer_right,
234
                                      FLOAT32 **r_buffer_right, ixheaace_str_ps_tab *ps_tables,
235
29.4k
                                      ixheaace_comm_tables *common_tab) {
236
29.4k
  IA_ERRORCODE err_code;
237
29.4k
  WORD32 i;
238
29.4k
  WORD32 bin;
239
29.4k
  WORD32 subband, max_subband;
240
241
29.4k
  FLOAT32 band_hist_power_left;
242
29.4k
  FLOAT32 band_hist_power_right;
243
29.4k
  FLOAT32 band_hist_power_corr_real;
244
29.4k
  FLOAT32 band_hist_power_corr_imag;
245
246
29.4k
  FLOAT32 band_power_left_env1;
247
29.4k
  FLOAT32 band_power_right_env1;
248
29.4k
  FLOAT32 band_power_corr_real_env1;
249
29.4k
  FLOAT32 band_power_corr_imag_env1;
250
29.4k
  FLOAT32 scratch_power_left_right_env2[2 * NUMBER_OF_BINS];
251
29.4k
  FLOAT32 scratch_power_corr_real_imag_env2[2 * NUMBER_OF_BINS];
252
253
29.4k
  FLOAT32 **hybrid_left_imag_env1;
254
29.4k
  FLOAT32 **hybrid_left_real_env1;
255
29.4k
  FLOAT32 **hybrid_right_imag_env1;
256
29.4k
  FLOAT32 **hybrid_right_real_env1;
257
258
29.4k
  FLOAT32 **hybrid_left_imag_env2;
259
29.4k
  FLOAT32 **hybrid_left_real_env2;
260
29.4k
  FLOAT32 **hybr_right_imag_env2;
261
29.4k
  FLOAT32 **hybr_right_real_env2;
262
263
29.4k
  FLOAT32 **hist_left_imag;
264
29.4k
  FLOAT32 **hist_left_real;
265
29.4k
  FLOAT32 **hist_right_imag;
266
29.4k
  FLOAT32 **hist_right_real;
267
268
29.4k
  FLOAT32 temp1;
269
29.4k
  FLOAT32 temp2;
270
271
29.4k
  FLOAT32 temp_hist_pow_left;
272
29.4k
  FLOAT32 temp_hist_pow_right;
273
29.4k
  FLOAT32 temp_hist_pow_corr_real;
274
29.4k
  FLOAT32 temp_hist_pow_corr_imag;
275
276
29.4k
  FLOAT32 temp_power_left_env1;
277
29.4k
  FLOAT32 temp_power_right_env1;
278
29.4k
  FLOAT32 temp_power_corr_real_env1;
279
29.4k
  FLOAT32 temp_power_corr_imag_env1;
280
281
29.4k
  FLOAT32 temp_power_left_env2;
282
29.4k
  FLOAT32 temp_power_right_env2;
283
29.4k
  FLOAT32 temp_power_corr_real_env2;
284
29.4k
  FLOAT32 temp_power_corr_imag_env2;
285
286
29.4k
  err_code = ixheaace_hybrid_analysis((const FLOAT32 **)(r_buffer_left),
287
29.4k
                                      (const FLOAT32 **)(i_buffer_left), pms->m_hybrid_real_left,
288
29.4k
                                      pms->m_hybrid_imag_left, pms->ptr_hybrid_left, ps_tables,
289
29.4k
                                      common_tab->pstr_common_tab);
290
29.4k
  if (err_code != IA_NO_ERROR) {
291
0
    return err_code;
292
0
  }
293
294
29.4k
  err_code = ixheaace_hybrid_analysis(
295
29.4k
      (const FLOAT32 **)(r_buffer_right), (const FLOAT32 **)(i_buffer_right),
296
29.4k
      pms->m_hybrid_real_right, pms->m_hybrid_imag_right, pms->ptr_hybrid_right, ps_tables,
297
29.4k
      common_tab->pstr_common_tab);
298
29.4k
  if (err_code != IA_NO_ERROR) {
299
0
    return err_code;
300
0
  }
301
302
29.4k
  {
303
29.4k
    hybrid_left_real_env1 = pms->m_hybrid_real_left;
304
29.4k
    hybrid_left_imag_env1 = pms->m_hybrid_imag_left;
305
29.4k
    hybrid_right_real_env1 = pms->m_hybrid_real_right;
306
29.4k
    hybrid_right_imag_env1 = pms->m_hybrid_imag_right;
307
308
29.4k
    hybrid_left_real_env2 = &(pms->m_hybrid_real_left[NUMBER_OF_SUBSAMPLES / 2]);
309
29.4k
    hybrid_left_imag_env2 = &(pms->m_hybrid_imag_left[NUMBER_OF_SUBSAMPLES / 2]);
310
29.4k
    hybr_right_real_env2 = &(pms->m_hybrid_real_right[NUMBER_OF_SUBSAMPLES / 2]);
311
29.4k
    hybr_right_imag_env2 = &(pms->m_hybrid_imag_right[NUMBER_OF_SUBSAMPLES / 2]);
312
313
265k
    for (bin = 0; bin < SUBQMF_BINS_ENERGY; bin++) {
314
235k
      band_power_left_env1 = 0;
315
235k
      band_power_right_env1 = 0;
316
235k
      band_power_corr_real_env1 = 0;
317
235k
      band_power_corr_imag_env1 = 0;
318
319
235k
      temp_power_left_env2 = 0;
320
235k
      temp_power_right_env2 = 0;
321
235k
      temp_power_corr_real_env2 = 0;
322
235k
      temp_power_corr_imag_env2 = 0;
323
324
235k
      max_subband = ps_tables->hi_res_band_borders[bin] + 1;
325
471k
      for (subband = ps_tables->hi_res_band_borders[bin]; subband < max_subband; subband++) {
326
235k
        FLOAT32 t_left_real, t_left_imag, t_right_real, t_right_imag;
327
328
4.01M
        for (i = 0; i < NUMBER_OF_SUBSAMPLES / 2; i++) {
329
          // Envelope 1
330
3.77M
          t_left_real = hybrid_left_real_env1[i][subband];
331
3.77M
          t_right_real = hybrid_right_real_env1[i][subband];
332
3.77M
          t_left_imag = hybrid_left_imag_env1[i][subband];
333
3.77M
          t_right_imag = hybrid_right_imag_env1[i][subband];
334
335
3.77M
          temp1 = t_left_real * t_left_real;
336
3.77M
          temp2 = t_left_imag * t_left_imag;
337
3.77M
          band_power_left_env1 += (temp1 + temp2);
338
339
3.77M
          temp1 = t_right_real * t_right_real;
340
3.77M
          temp2 = t_right_imag * t_right_imag;
341
3.77M
          band_power_right_env1 += (temp1 + temp2);
342
343
3.77M
          temp1 = t_left_real * t_right_real;
344
3.77M
          temp2 = t_left_imag * t_right_imag;
345
3.77M
          band_power_corr_real_env1 += (temp1 + temp2);
346
347
3.77M
          temp1 = t_left_imag * t_right_real;
348
3.77M
          temp2 = t_left_real * t_right_imag;
349
3.77M
          band_power_corr_imag_env1 += (temp1 - temp2);
350
351
          // Envelope 2
352
3.77M
          t_left_real = hybrid_left_real_env2[i][subband];
353
3.77M
          t_right_real = hybr_right_real_env2[i][subband];
354
3.77M
          t_left_imag = hybrid_left_imag_env2[i][subband];
355
3.77M
          t_right_imag = hybr_right_imag_env2[i][subband];
356
357
3.77M
          temp1 = t_left_real * t_left_real;
358
3.77M
          temp2 = t_left_imag * t_left_imag;
359
3.77M
          temp_power_left_env2 += (temp1 + temp2);
360
361
3.77M
          temp1 = t_right_real * t_right_real;
362
3.77M
          temp2 = t_right_imag * t_right_imag;
363
3.77M
          temp_power_right_env2 += (temp1 + temp2);
364
365
3.77M
          temp1 = t_left_real * t_right_real;
366
3.77M
          temp2 = t_left_imag * t_right_imag;
367
3.77M
          temp_power_corr_real_env2 += (temp1 + temp2);
368
369
3.77M
          temp1 = t_left_imag * t_right_real;
370
3.77M
          temp2 = t_left_real * t_right_imag;
371
3.77M
          temp_power_corr_imag_env2 += (temp1 - temp2);
372
3.77M
        }
373
235k
      }
374
375
235k
      scratch_power_left_right_env2[2 * bin] = temp_power_left_env2;
376
235k
      scratch_power_left_right_env2[2 * bin + 1] = temp_power_right_env2;
377
235k
      scratch_power_corr_real_imag_env2[2 * bin] = temp_power_corr_real_env2;
378
235k
      scratch_power_corr_real_imag_env2[2 * bin + 1] = temp_power_corr_imag_env2;
379
380
235k
      pms->pow_left_right[2 * bin] = pms->pow_left_right[2 * bin] + band_power_left_env1;
381
235k
      pms->pow_left_right[2 * bin + 1] = pms->pow_left_right[2 * bin + 1] + band_power_right_env1;
382
235k
      pms->pow_corr_real_imag[2 * bin] =
383
235k
          pms->pow_corr_real_imag[2 * bin] + band_power_corr_real_env1;
384
235k
      pms->pow_corr_real_imag[2 * bin + 1] =
385
235k
          pms->pow_corr_real_imag[2 * bin + 1] + band_power_corr_imag_env1;
386
235k
    }
387
388
29.4k
    hist_left_real = pms->hist_qmf_left_real;
389
29.4k
    hist_left_imag = pms->hist_qmf_left_imag;
390
29.4k
    hist_right_real = pms->hist_qmf_right_real;
391
29.4k
    hist_right_imag = pms->hist_qmf_right_imag;
392
393
29.4k
    hybrid_left_real_env1 = r_buffer_left;
394
29.4k
    hybrid_left_imag_env1 = i_buffer_left;
395
29.4k
    hybrid_right_real_env1 = r_buffer_right;
396
29.4k
    hybrid_right_imag_env1 = i_buffer_right;
397
398
29.4k
    hybrid_left_real_env2 =
399
29.4k
        &r_buffer_left[NUMBER_OF_SUBSAMPLES / 2 - IXHEAACE_HYBRID_FILTER_DELAY];
400
29.4k
    hybrid_left_imag_env2 =
401
29.4k
        &i_buffer_left[NUMBER_OF_SUBSAMPLES / 2 - IXHEAACE_HYBRID_FILTER_DELAY];
402
29.4k
    hybr_right_real_env2 =
403
29.4k
        &r_buffer_right[NUMBER_OF_SUBSAMPLES / 2 - IXHEAACE_HYBRID_FILTER_DELAY];
404
29.4k
    hybr_right_imag_env2 =
405
29.4k
        &i_buffer_right[NUMBER_OF_SUBSAMPLES / 2 - IXHEAACE_HYBRID_FILTER_DELAY];
406
407
383k
    for (bin = SUBQMF_BINS_ENERGY; bin < NUMBER_OF_BINS; bin++) {
408
353k
      band_power_left_env1 = 0;
409
353k
      band_power_right_env1 = 0;
410
353k
      band_power_corr_real_env1 = 0;
411
353k
      band_power_corr_imag_env1 = 0;
412
413
353k
      scratch_power_left_right_env2[2 * bin] = 0;
414
353k
      scratch_power_left_right_env2[2 * bin + 1] = 0;
415
353k
      scratch_power_corr_real_imag_env2[2 * bin] = 0;
416
353k
      scratch_power_corr_real_imag_env2[2 * bin + 1] = 0;
417
418
353k
      band_hist_power_left = 0;
419
353k
      band_hist_power_right = 0;
420
353k
      band_hist_power_corr_real = 0;
421
353k
      band_hist_power_corr_imag = 0;
422
423
353k
      for (subband = ps_tables->hi_res_band_borders[bin];
424
2.15M
           subband < ps_tables->hi_res_band_borders[bin + 1]; subband++) {
425
1.79M
        FLOAT32 t_left_real, t_left_imag, t_right_real, t_right_imag;
426
1.79M
        temp_hist_pow_left = 0;
427
1.79M
        temp_hist_pow_right = 0;
428
1.79M
        temp_hist_pow_corr_real = 0;
429
1.79M
        temp_hist_pow_corr_imag = 0;
430
431
1.79M
        temp_power_left_env1 = 0;
432
1.79M
        temp_power_right_env1 = 0;
433
1.79M
        temp_power_corr_real_env1 = 0;
434
1.79M
        temp_power_corr_imag_env1 = 0;
435
436
1.79M
        temp_power_left_env2 = 0;
437
1.79M
        temp_power_right_env2 = 0;
438
1.79M
        temp_power_corr_real_env2 = 0;
439
1.79M
        temp_power_corr_imag_env2 = 0;
440
441
12.5M
        for (i = 0; i < 6; i++) {
442
10.7M
          t_left_real = hist_left_real[i][subband];
443
10.7M
          t_left_imag = hist_left_imag[i][subband];
444
10.7M
          t_right_real = hist_right_real[i][subband];
445
10.7M
          t_right_imag = hist_right_imag[i][subband];
446
447
10.7M
          temp1 = t_left_real * t_left_real;
448
10.7M
          temp2 = t_left_imag * t_left_imag;
449
10.7M
          temp_hist_pow_left += (temp1 + temp2);
450
451
10.7M
          temp1 = t_right_real * t_right_real;
452
10.7M
          temp2 = t_right_imag * t_right_imag;
453
10.7M
          temp_hist_pow_right += (temp1 + temp2);
454
455
10.7M
          temp1 = t_left_real * t_right_real;
456
10.7M
          temp2 = t_left_imag * t_right_imag;
457
10.7M
          temp_hist_pow_corr_real += (temp1 + temp2);
458
459
10.7M
          temp1 = t_left_imag * t_right_real;
460
10.7M
          temp2 = t_left_real * t_right_imag;
461
10.7M
          temp_hist_pow_corr_imag += (temp1 - temp2);
462
463
10.7M
          t_left_real = hybrid_left_real_env1[i][subband];
464
10.7M
          t_left_imag = hybrid_left_imag_env1[i][subband];
465
10.7M
          t_right_real = hybrid_right_real_env1[i][subband];
466
10.7M
          t_right_imag = hybrid_right_imag_env1[i][subband];
467
468
10.7M
          temp1 = t_left_real * t_left_real;
469
10.7M
          temp2 = t_left_imag * t_left_imag;
470
10.7M
          temp_power_left_env1 += (temp1 + temp2);
471
472
10.7M
          temp1 = t_right_real * t_right_real;
473
10.7M
          temp2 = t_right_imag * t_right_imag;
474
10.7M
          temp_power_right_env1 += (temp1 + temp2);
475
476
10.7M
          temp1 = t_left_real * t_right_real;
477
10.7M
          temp2 = t_left_imag * t_right_imag;
478
10.7M
          temp_power_corr_real_env1 += (temp1 + temp2);
479
480
10.7M
          temp1 = t_left_imag * t_right_real;
481
10.7M
          temp2 = t_left_real * t_right_imag;
482
10.7M
          temp_power_corr_imag_env1 += (temp1 - temp2);
483
484
10.7M
          t_left_real = hybrid_left_real_env2[i][subband];
485
10.7M
          t_left_imag = hybrid_left_imag_env2[i][subband];
486
10.7M
          t_right_real = hybr_right_real_env2[i][subband];
487
10.7M
          t_right_imag = hybr_right_imag_env2[i][subband];
488
489
10.7M
          temp1 = t_left_real * t_left_real;
490
10.7M
          temp2 = t_left_imag * t_left_imag;
491
10.7M
          temp_power_left_env2 += (temp1 + temp2);
492
493
10.7M
          temp1 = t_right_real * t_right_real;
494
10.7M
          temp2 = t_right_imag * t_right_imag;
495
10.7M
          temp_power_right_env2 += (temp1 + temp2);
496
497
10.7M
          temp1 = t_left_real * t_right_real;
498
10.7M
          temp2 = t_left_imag * t_right_imag;
499
10.7M
          temp_power_corr_real_env2 += (temp1 + temp2);
500
501
10.7M
          temp1 = t_left_imag * t_right_real;
502
10.7M
          temp2 = t_left_real * t_right_imag;
503
10.7M
          temp_power_corr_imag_env2 += (temp1 - temp2);
504
10.7M
        }
505
506
8.99M
        for (i = 6; i < 10; i++) {
507
7.19M
          t_left_real = hybrid_left_real_env1[i][subband];
508
7.19M
          t_left_imag = hybrid_left_imag_env1[i][subband];
509
7.19M
          t_right_real = hybrid_right_real_env1[i][subband];
510
7.19M
          t_right_imag = hybrid_right_imag_env1[i][subband];
511
512
7.19M
          temp1 = t_left_real * t_left_real;
513
7.19M
          temp2 = t_left_imag * t_left_imag;
514
7.19M
          temp_power_left_env1 += (temp1 + temp2);
515
516
7.19M
          temp1 = t_right_real * t_right_real;
517
7.19M
          temp2 = t_right_imag * t_right_imag;
518
7.19M
          temp_power_right_env1 += (temp1 + temp2);
519
520
7.19M
          temp1 = t_left_real * t_right_real;
521
7.19M
          temp2 = t_left_imag * t_right_imag;
522
7.19M
          temp_power_corr_real_env1 += (temp1 + temp2);
523
524
7.19M
          temp1 = t_left_imag * t_right_real;
525
7.19M
          temp2 = t_left_real * t_right_imag;
526
7.19M
          temp_power_corr_imag_env1 += (temp1 - temp2);
527
528
7.19M
          t_left_real = hybrid_left_real_env2[i][subband];
529
7.19M
          t_left_imag = hybrid_left_imag_env2[i][subband];
530
7.19M
          t_right_real = hybr_right_real_env2[i][subband];
531
7.19M
          t_right_imag = hybr_right_imag_env2[i][subband];
532
533
7.19M
          temp1 = t_left_real * t_left_real;
534
7.19M
          temp2 = t_left_imag * t_left_imag;
535
7.19M
          temp_power_left_env2 += (temp1 + temp2);
536
537
7.19M
          temp1 = t_right_real * t_right_real;
538
7.19M
          temp2 = t_right_imag * t_right_imag;
539
7.19M
          temp_power_right_env2 += (temp1 + temp2);
540
541
7.19M
          temp1 = t_left_real * t_right_real;
542
7.19M
          temp2 = t_left_imag * t_right_imag;
543
7.19M
          temp_power_corr_real_env2 += (temp1 + temp2);
544
545
7.19M
          temp1 = t_left_imag * t_right_real;
546
7.19M
          temp2 = t_left_real * t_right_imag;
547
7.19M
          temp_power_corr_imag_env2 += (temp1 - temp2);
548
7.19M
        }
549
550
12.5M
        for (i = 10; i < 16; i++) {
551
10.7M
          t_left_real = hybrid_left_real_env2[i][subband];
552
10.7M
          t_left_imag = hybrid_left_imag_env2[i][subband];
553
10.7M
          t_right_real = hybr_right_real_env2[i][subband];
554
10.7M
          t_right_imag = hybr_right_imag_env2[i][subband];
555
556
10.7M
          temp1 = t_left_real * t_left_real;
557
10.7M
          temp2 = t_left_imag * t_left_imag;
558
10.7M
          temp_power_left_env2 += (temp1 + temp2);
559
560
10.7M
          temp1 = t_right_real * t_right_real;
561
10.7M
          temp2 = t_right_imag * t_right_imag;
562
10.7M
          temp_power_right_env2 += (temp1 + temp2);
563
564
10.7M
          temp1 = t_left_real * t_right_real;
565
10.7M
          temp2 = t_left_imag * t_right_imag;
566
10.7M
          temp_power_corr_real_env2 += (temp1 + temp2);
567
568
10.7M
          temp1 = t_left_imag * t_right_real;
569
10.7M
          temp2 = t_left_real * t_right_imag;
570
10.7M
          temp_power_corr_imag_env2 += (temp1 - temp2);
571
10.7M
        }
572
573
1.79M
        scratch_power_left_right_env2[2 * bin] =
574
1.79M
            scratch_power_left_right_env2[2 * bin] + temp_power_left_env2;
575
1.79M
        scratch_power_left_right_env2[2 * bin + 1] =
576
1.79M
            scratch_power_left_right_env2[2 * bin + 1] + temp_power_right_env2;
577
1.79M
        scratch_power_corr_real_imag_env2[2 * bin] =
578
1.79M
            scratch_power_corr_real_imag_env2[2 * bin] + temp_power_corr_real_env2;
579
1.79M
        scratch_power_corr_real_imag_env2[2 * bin + 1] =
580
1.79M
            scratch_power_corr_real_imag_env2[2 * bin + 1] + temp_power_corr_imag_env2;
581
582
1.79M
        band_power_left_env1 = band_power_left_env1 + temp_power_left_env1;
583
1.79M
        band_power_right_env1 = band_power_right_env1 + temp_power_right_env1;
584
1.79M
        band_power_corr_real_env1 = band_power_corr_real_env1 + temp_power_corr_real_env1;
585
1.79M
        band_power_corr_imag_env1 = band_power_corr_imag_env1 + temp_power_corr_imag_env1;
586
587
1.79M
        band_hist_power_left = band_hist_power_left + temp_hist_pow_left;
588
1.79M
        band_hist_power_right = band_hist_power_right + temp_hist_pow_right;
589
1.79M
        band_hist_power_corr_real = band_hist_power_corr_real + temp_hist_pow_corr_real;
590
1.79M
        band_hist_power_corr_imag = band_hist_power_corr_imag + temp_hist_pow_corr_imag;
591
1.79M
      }
592
593
353k
      pms->pow_left_right[2 * bin] += (band_power_left_env1 + band_hist_power_left);
594
353k
      pms->pow_left_right[2 * bin + 1] += (band_power_right_env1 + band_hist_power_right);
595
353k
      pms->pow_corr_real_imag[2 * bin] += (band_power_corr_real_env1 + band_hist_power_corr_real);
596
353k
      pms->pow_corr_real_imag[2 * bin + 1] +=
597
353k
          (band_power_corr_imag_env1 + band_hist_power_corr_imag);
598
353k
    }
599
29.4k
  }
600
601
29.4k
  {
602
29.4k
    FLOAT32 temp_left;
603
29.4k
    FLOAT32 temp_right;
604
29.4k
    FLOAT32 temp_corr_re;
605
29.4k
    FLOAT32 temp_corr_im;
606
607
548k
    for (bin = 0; bin < pms->iid_icc_bins; bin++) {
608
519k
      if (pms->b_hi_freq_res_iid_icc) {
609
448k
        temp_left = pms->pow_left_right[2 * bin];
610
448k
        temp_right = pms->pow_left_right[2 * bin + 1];
611
448k
        temp_corr_re = pms->pow_corr_real_imag[2 * bin];
612
448k
        temp_corr_im = pms->pow_corr_real_imag[2 * bin + 1];
613
448k
      } else {
614
70.5k
        temp_left = pms->pow_left_right[2 * 2 * bin] + pms->pow_left_right[2 * 2 * bin + 2];
615
70.5k
        temp_right =
616
70.5k
            pms->pow_left_right[2 * 2 * bin + 1] + pms->pow_left_right[2 * (2 * bin + 1) + 1];
617
70.5k
        temp_corr_re =
618
70.5k
            pms->pow_corr_real_imag[2 * 2 * bin] + pms->pow_corr_real_imag[2 * 2 * bin + 2];
619
70.5k
        temp_corr_im = pms->pow_corr_real_imag[2 * 2 * bin + 1] +
620
70.5k
                       pms->pow_corr_real_imag[2 * (2 * bin + 1) + 1];
621
70.5k
      }
622
623
519k
      if (temp_left == 0) {
624
157k
        temp_left = 0.0625f;
625
157k
      }
626
519k
      if (temp_right == 0) {
627
157k
        temp_right = 0.0625f;
628
157k
      }
629
519k
      if (temp_corr_re == 0) {
630
157k
        temp_corr_re = 0.0625f;
631
157k
      }
632
519k
      if (temp_corr_im == 0) {
633
449k
        temp_corr_im = 0.0625f;
634
449k
      }
635
636
519k
      pms->aaa_ICC_data_buf[bin][1] = pms->aaa_ICC_data_buf[bin][0];
637
519k
      if (bin > NUMBER_OF_IPD_BINS) {
638
179k
        temp1 = temp_corr_re * temp_corr_re;
639
179k
        temp2 = temp_corr_im * temp_corr_im;
640
179k
        temp_corr_re = temp1 + temp2;
641
642
179k
        temp1 = temp_left * temp_right;
643
179k
        pms->aaa_ICC_data_buf[bin][0] = (FLOAT32)sqrt(temp_corr_re / temp1);
644
339k
      } else {
645
339k
        temp1 = temp_left * temp_right;
646
339k
        pms->aaa_ICC_data_buf[bin][0] = temp_corr_re / (FLOAT32)sqrt(temp1);
647
339k
      }
648
519k
      if (pms->aaa_ICC_data_buf[bin][0] > 1.0f) {
649
60.1k
        pms->aaa_ICC_data_buf[bin][0] = 0;
650
459k
      } else {
651
459k
        pms->aaa_ICC_data_buf[bin][0] =
652
459k
            (FLOAT32)sqrt(0.5f - (pms->aaa_ICC_data_buf[bin][0] / 2.0f));
653
459k
      }
654
519k
      temp1 = temp_left / temp_right;
655
519k
      temp1 = (FLOAT32)sqrt(temp1);
656
519k
      pms->aaa_IID_data_buf[bin][1] = pms->aaa_IID_data_buf[bin][0];
657
519k
      pms->aaa_IID_data_buf[bin][0] = SBR_INV_LOG_2 * (FLOAT32)log(temp1);
658
519k
    }
659
29.4k
  }
660
29.4k
  {
661
29.4k
    FLOAT32 *pow_left = &pms->pow_left_right[0];
662
29.4k
    FLOAT32 *corr_real = &pms->pow_corr_real_imag[0];
663
664
29.4k
    memcpy(pow_left, scratch_power_left_right_env2, 2 * NUMBER_OF_BINS * sizeof(FLOAT32));
665
29.4k
    memcpy(corr_real, scratch_power_corr_real_imag_env2, 2 * NUMBER_OF_BINS * sizeof(FLOAT32));
666
29.4k
  }
667
668
29.4k
  ia_enhaacplus_enc_downmix_to_mono(pms, r_buffer_left, i_buffer_left, r_buffer_right,
669
29.4k
                                    i_buffer_right, ps_tables);
670
29.4k
  return IA_NO_ERROR;
671
29.4k
}