Coverage Report

Created: 2025-08-29 06:15

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