Coverage Report

Created: 2026-01-10 06:30

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