Coverage Report

Created: 2025-10-13 07:04

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