Coverage Report

Created: 2026-06-10 06:29

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libxaac/encoder/ixheaace_sbr_freq_scaling.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 <math.h>
22
#include "ixheaac_type_def.h"
23
#include "ixheaac_constants.h"
24
#include "ixheaace_error_codes.h"
25
#include "ixheaac_error_standards.h"
26
#include "ixheaace_aac_constants.h"
27
#include "ixheaac_basic_ops32.h"
28
#include "ixheaac_basic_ops16.h"
29
#include "ixheaac_basic_ops40.h"
30
#include "ixheaac_basic_ops.h"
31
32
#include "ixheaace_common_rom.h"
33
#include "ixheaace_sbr_header.h"
34
#include "ixheaace_sbr_def.h"
35
#include "ixheaace_sbr_freq_scaling.h"
36
#include "ixheaace_sbr_misc.h"
37
#include "ixheaace_resampler.h"
38
#include "ixheaace_sbr_rom.h"
39
40
568
static WORD32 ixheaace_get_start_freq_4_1(WORD32 fs, WORD32 start_freq) {
41
568
  WORD32 minimum_k0;
42
568
  const WORD32 *ptr_start_offset;
43
44
568
  switch (fs) {
45
97
    case 16000:
46
97
      minimum_k0 = 12;
47
97
      break;
48
48
    case 22050:
49
48
      minimum_k0 = 9;
50
48
      break;
51
254
    case 24000:
52
254
      minimum_k0 = 8;
53
254
      break;
54
163
    case 32000:
55
163
      minimum_k0 = 8;
56
163
      break;
57
2
    case 44100:
58
2
      minimum_k0 = 6;
59
2
      break;
60
1
    case 48000:
61
1
      minimum_k0 = 5;
62
1
      break;
63
3
    default:
64
3
      minimum_k0 = 5; /* illegal fs */
65
568
  }
66
67
568
  switch (fs) {
68
97
    case 16000: {
69
97
      ptr_start_offset = &ixheaace_start_freq_16k_4_1[0];
70
97
    } break;
71
72
48
    case 22050: {
73
48
      ptr_start_offset = &ixheaace_start_freq_22k_4_1[0];
74
48
    } break;
75
76
254
    case 24000: {
77
254
      ptr_start_offset = &ixheaace_start_freq_24k_4_1[0];
78
254
    } break;
79
80
163
    case 32000: {
81
163
      ptr_start_offset = &ixheaace_start_freq_32k_4_1[0];
82
163
    } break;
83
84
2
    case 44100:
85
3
    case 48000:
86
3
    case 64000: {
87
3
      ptr_start_offset = &ixheaace_start_freq_48k_4_1[0];
88
3
    } break;
89
90
0
    case 88200:
91
0
    case 96000: {
92
0
      ptr_start_offset = &ixheaace_start_freq_96k_4_1[0];
93
0
    } break;
94
95
3
    default: {
96
3
      ptr_start_offset = &ixheaace_start_freq_dflt_4_1[0];
97
3
    }
98
568
  }
99
568
  return (minimum_k0 + ptr_start_offset[start_freq]);
100
568
}
101
102
568
static WORD32 ixheaace_get_stop_freq_4_1(WORD32 fs, WORD32 stop_freq) {
103
568
  WORD32 result, i;
104
568
  WORD32 *v_stop_freq = 0;
105
568
  WORD32 k1_min;
106
568
  WORD32 v_dstop[13];
107
108
  /* counting previous operations */
109
568
  switch (fs) {
110
97
    case 16000:
111
97
      k1_min = 24;
112
97
      v_stop_freq = (WORD32 *)&ixheaace_stop_freq_16k_4_1[0];
113
97
      break;
114
48
    case 22050:
115
48
      k1_min = 17;
116
48
      v_stop_freq = (WORD32 *)&ixheaace_stop_freq_22k_4_1[0];
117
48
      break;
118
254
    case 24000:
119
254
      k1_min = 16;
120
254
      v_stop_freq = (WORD32 *)&ixheaace_stop_freq_24k_4_1[0];
121
254
      break;
122
163
    case 32000:
123
163
      k1_min = 16;
124
163
      v_stop_freq = (WORD32 *)&ixheaace_stop_freq_32k_4_1[0];
125
163
      break;
126
127
2
    case 44100:
128
2
      k1_min = 12;
129
2
      v_stop_freq = (WORD32 *)&ixheaace_stop_freq_44k_4_1[0];
130
2
      break;
131
132
1
    case 48000:
133
1
      k1_min = 11;
134
1
      v_stop_freq = (WORD32 *)&ixheaace_stop_freq_48k_4_1[0];
135
1
      break;
136
137
3
    default:
138
3
      v_stop_freq = (WORD32 *)&ixheaace_stop_freq_32k_4_1[0];
139
3
      k1_min = 11; /* illegal fs  */
140
568
  }
141
142
7.95k
  for (i = 0; i <= 12; i++) {
143
7.38k
    v_dstop[i] = v_stop_freq[i + 1] - v_stop_freq[i];
144
7.38k
  }
145
146
568
  ixheaace_shellsort_int(v_dstop, 13);
147
148
568
  result = k1_min;
149
150
6.81k
  for (i = 0; i < stop_freq; i++) {
151
6.24k
    result = result + v_dstop[i];
152
6.24k
  }
153
154
568
  return result;
155
568
}
156
157
13.9k
static WORD32 ixheaace_get_start_freq(WORD32 fs, WORD32 start_freq) {
158
13.9k
  WORD32 minimum_k0;
159
160
13.9k
  switch (fs) {
161
223
    case 16000:
162
223
      minimum_k0 = 24;
163
223
      break;
164
86
    case 22050:
165
86
      minimum_k0 = 17;
166
86
      break;
167
322
    case 24000:
168
322
      minimum_k0 = 16;
169
322
      break;
170
4.62k
    case 32000:
171
4.62k
      minimum_k0 = 16;
172
4.62k
      break;
173
3.62k
    case 44100:
174
3.62k
      minimum_k0 = 12;
175
3.62k
      break;
176
4.52k
    case 48000:
177
4.52k
      minimum_k0 = 11;
178
4.52k
      break;
179
295
    case 64000:
180
295
      minimum_k0 = 10;
181
295
      break;
182
0
    case 88200:
183
0
      minimum_k0 = 7;
184
0
      break;
185
0
    case 96000:
186
0
      minimum_k0 = 7;
187
0
      break;
188
200
    default:
189
200
      minimum_k0 = 11; /* illegal fs */
190
13.9k
  }
191
192
13.9k
  switch (fs) {
193
223
    case 16000: {
194
223
      return (minimum_k0 + vector_offset_16k[start_freq]);
195
0
    } break;
196
197
86
    case 22050: {
198
86
      return (minimum_k0 + vector_offset_22k[start_freq]);
199
0
    } break;
200
201
322
    case 24000: {
202
322
      return (minimum_k0 + vector_offset_24k[start_freq]);
203
0
    } break;
204
205
4.62k
    case 32000: {
206
4.62k
      return (minimum_k0 + vector_offset_32k[start_freq]);
207
0
    } break;
208
209
3.62k
    case 44100:
210
8.15k
    case 48000:
211
8.44k
    case 64000: {
212
8.44k
      return (minimum_k0 + vector_offset_44_48_64[start_freq]);
213
8.15k
    } break;
214
215
0
    case 88200:
216
0
    case 96000: {
217
0
      return (minimum_k0 + vector_offset_88_96[start_freq]);
218
0
    } break;
219
220
200
    default: {
221
200
      return (minimum_k0 + vector_offset_def[start_freq]);
222
0
    }
223
13.9k
  }
224
13.9k
}
225
226
3.04k
static WORD32 ixheaace_get_stop_freq(WORD32 fs, WORD32 stop_freq) {
227
3.04k
  WORD32 result, i;
228
3.04k
  WORD32 *v_stop_freq = 0;
229
3.04k
  WORD32 k1_min;
230
3.04k
  WORD32 v_dstop[13];
231
232
3.04k
  switch (fs) {
233
0
    case 16000:
234
0
      k1_min = ixheaace_stop_freq_16k[0];
235
0
      v_stop_freq = (WORD32 *)&ixheaace_stop_freq_16k[0];
236
0
      break;
237
0
    case 22050:
238
0
      k1_min = ixheaace_stop_freq_22k[0];
239
0
      v_stop_freq = (WORD32 *)&ixheaace_stop_freq_22k[0];
240
0
      break;
241
0
    case 24000:
242
0
      k1_min = ixheaace_stop_freq_24k[0];
243
0
      v_stop_freq = (WORD32 *)&ixheaace_stop_freq_24k[0];
244
0
      break;
245
1.10k
    case 32000:
246
1.10k
      k1_min = 32;
247
248
1.10k
      v_stop_freq = (WORD32 *)vector_stop_freq_32;
249
1.10k
      break;
250
251
918
    case 44100:
252
918
      k1_min = 23;
253
254
918
      v_stop_freq = (WORD32 *)vector_stop_freq_44;
255
918
      break;
256
257
1.02k
    case 48000:
258
1.02k
      k1_min = 21;
259
260
1.02k
      v_stop_freq = (WORD32 *)vector_stop_freq_48;
261
1.02k
      break;
262
263
0
    default:
264
0
      v_stop_freq = (WORD32 *)vector_stop_freq_32;
265
0
      k1_min = 21; /* illegal fs  */
266
3.04k
  }
267
268
42.6k
  for (i = 0; i <= 12; i++) {
269
39.5k
    v_dstop[i] = v_stop_freq[i + 1] - v_stop_freq[i];
270
39.5k
  }
271
272
3.04k
  ixheaace_shellsort_int(v_dstop, 13);
273
274
3.04k
  result = k1_min;
275
276
25.7k
  for (i = 0; i < stop_freq; i++) {
277
22.7k
    result = result + v_dstop[i];
278
22.7k
  }
279
280
3.04k
  return result;
281
3.04k
}
282
283
static WORD32 ixheaace_get_usac_stop_freq(WORD32 fs, WORD32 stop_freq)
284
2.27k
{
285
2.27k
  WORD32 result, i;
286
2.27k
  WORD32 *v_stop_freq = 0;
287
2.27k
  WORD32 k1_min;
288
2.27k
  WORD32 v_dstop[13];
289
290
2.27k
  switch (fs)
291
2.27k
  {
292
85
  case 16000:
293
85
    k1_min = ixheaace_usac_stop_freq_16k[0];
294
85
    v_stop_freq = (WORD32 *)&ixheaace_usac_stop_freq_16k[0];
295
85
    break;
296
32
  case 22050:
297
32
    k1_min = ixheaace_usac_stop_freq_22k[0];
298
32
    v_stop_freq = (WORD32 *)&ixheaace_usac_stop_freq_22k[0];
299
32
    break;
300
129
  case 24000:
301
129
    k1_min = ixheaace_usac_stop_freq_24k[0];
302
129
    v_stop_freq = (WORD32 *)&ixheaace_usac_stop_freq_24k[0];
303
129
    break;
304
735
  case 32000:
305
735
    k1_min = 32;
306
735
    v_stop_freq = (WORD32 *)vector_stop_freq_32;
307
735
    break;
308
309
500
  case 44100:
310
500
    k1_min = 23;
311
500
    v_stop_freq = (WORD32 *)vector_stop_freq_44;
312
500
    break;
313
314
712
  case 48000:
315
712
    k1_min = 21;
316
712
    v_stop_freq = (WORD32 *)vector_stop_freq_48;
317
712
    break;
318
319
79
  default:
320
79
    v_stop_freq = (WORD32 *)vector_stop_freq_32;
321
79
    k1_min = 21; /* illegal fs  */
322
2.27k
  }
323
324
31.8k
  for (i = 0; i <= 12; i++)
325
29.5k
  {
326
29.5k
    v_dstop[i] = v_stop_freq[i + 1] - v_stop_freq[i];
327
29.5k
  }
328
329
2.27k
  ixheaace_shellsort_int(v_dstop, 13);
330
331
2.27k
  result = k1_min;
332
333
26.8k
  for (i = 0; i < stop_freq; i++)
334
24.6k
  {
335
24.6k
    result = result + v_dstop[i];
336
24.6k
  }
337
338
2.27k
  return result;
339
2.27k
}
340
341
WORD32
342
8.59k
ixheaace_get_sbr_start_freq_raw(WORD32 start_freq, WORD32 qmf_bands, WORD32 fs) {
343
8.59k
  WORD32 result;
344
345
8.59k
  if (start_freq < 0 || start_freq > 15) {
346
0
    return -1;
347
0
  }
348
349
8.59k
  result = ixheaace_get_start_freq(fs, start_freq);
350
351
8.59k
  result = (result * fs / qmf_bands + 1) >> 1;
352
353
8.59k
  return result;
354
8.59k
}
355
356
static WORD32 ixheaace_number_of_bands(WORD32 b_p_o, WORD32 start, WORD32 stop,
357
10.0k
                                       FLOAT32 warp_fac) {
358
10.0k
  WORD32 result = 0;
359
10.0k
  result = (WORD32)(b_p_o * log((FLOAT32)(stop) / start) / (2.0 * log(2.0) * warp_fac) + 0.5);
360
10.0k
  result <<= 1;
361
10.0k
  return result;
362
10.0k
}
363
364
10.0k
static VOID ixheaace_calc_bands(WORD32 *ptr_diff, WORD32 start, WORD32 stop, WORD32 num_bands) {
365
10.0k
  WORD32 i;
366
10.0k
  WORD32 previous;
367
10.0k
  WORD32 current;
368
10.0k
  previous = start;
369
85.0k
  for (i = 1; i <= num_bands; i++) {
370
75.0k
    current = (WORD32)((start * pow((FLOAT32)stop / start, (FLOAT32)i / num_bands)) + 0.5f);
371
75.0k
    ptr_diff[i - 1] = current - previous;
372
75.0k
    previous = current;
373
75.0k
  }
374
10.0k
}
375
376
267
static VOID ixheaace_modify_bands(WORD32 max_band_previous, WORD32 *ptr_diff, WORD32 length) {
377
267
  WORD32 change = max_band_previous - ptr_diff[0];
378
379
267
  if (change > (ptr_diff[length - 1] - ptr_diff[0]) / 2) {
380
267
    change = (ptr_diff[length - 1] - ptr_diff[0]) / 2;
381
267
  }
382
383
267
  ptr_diff[0] += change;
384
385
267
  ptr_diff[length - 1] -= change;
386
387
267
  ixheaace_shellsort_int(ptr_diff, length);
388
267
}
389
390
static VOID ixheaace_cum_sum(WORD32 start_value, WORD32 *ptr_diff, WORD32 length,
391
10.0k
                             UWORD8 *ptr_start_adress) {
392
10.0k
  WORD32 i;
393
394
10.0k
  ptr_start_adress[0] = (UWORD8)start_value;
395
396
85.0k
  for (i = 1; i <= length; i++) {
397
74.9k
    ptr_start_adress[i] = ptr_start_adress[i - 1] + (UWORD8)ptr_diff[i - 1];
398
74.9k
  }
399
10.0k
}
400
401
IA_ERRORCODE
402
ixheaace_find_start_and_stop_band(const WORD32 sampling_freq, const WORD32 num_channels,
403
                                  const WORD32 start_freq, const WORD32 stop_freq,
404
                                  const ixheaace_sr_mode sample_rate_mode, WORD32 *ptr_k0,
405
                                  WORD32 *ptr_k2, WORD32 sbr_ratio_idx,
406
5.88k
                                  ixheaace_sbr_codec_type sbr_codec) {
407
5.88k
  switch (sbr_codec) {
408
2.84k
    case USAC_SBR: {
409
2.84k
      if (sbr_ratio_idx == USAC_SBR_RATIO_INDEX_4_1) {
410
568
        *ptr_k0 = ixheaace_get_start_freq_4_1(sampling_freq, start_freq);
411
2.27k
      } else {
412
2.27k
        *ptr_k0 = ixheaace_get_start_freq(sampling_freq, start_freq);
413
2.27k
      }
414
2.84k
      break;
415
0
    }
416
3.04k
    default: {
417
3.04k
      *ptr_k0 = ixheaace_get_start_freq(sampling_freq, start_freq);
418
3.04k
      break;
419
0
    }
420
5.88k
  }
421
5.88k
  if ((sample_rate_mode == 1) && (sampling_freq * num_channels < 2 * *ptr_k0 * sampling_freq)) {
422
0
    return IA_EXHEAACE_INIT_FATAL_SBR_INVALID_SAMPLERATE_MODE;
423
0
  }
424
425
5.88k
  if (stop_freq < 14) {
426
5.88k
    switch (sbr_codec) {
427
2.84k
      case USAC_SBR: {
428
2.84k
        if (USAC_SBR_RATIO_INDEX_4_1 == sbr_ratio_idx) {
429
568
          *ptr_k2 = ixheaace_get_stop_freq_4_1(sampling_freq, stop_freq);
430
2.27k
        } else {
431
2.27k
          *ptr_k2 = ixheaace_get_usac_stop_freq(sampling_freq, stop_freq);
432
2.27k
        }
433
2.84k
        break;
434
0
      }
435
3.04k
      default: {
436
3.04k
        *ptr_k2 = ixheaace_get_stop_freq(sampling_freq, stop_freq);
437
3.04k
        break;
438
0
      }
439
5.88k
    }
440
5.88k
  }
441
442
0
  else {
443
0
    *ptr_k2 = (stop_freq == 14 ? 2 * *ptr_k0 : 3 * *ptr_k0);
444
0
  }
445
446
5.88k
  if (*ptr_k2 > num_channels) {
447
0
    *ptr_k2 = num_channels;
448
0
  }
449
5.88k
  if (sbr_codec == USAC_SBR) {
450
2.84k
    if (sbr_ratio_idx == USAC_SBR_RATIO_INDEX_4_1) {
451
568
      if (((*ptr_k2 - *ptr_k0) > MAXIMUM_FREQ_COEFFS_USAC) || (*ptr_k2 <= *ptr_k0)) {
452
0
        return IA_EXHEAACE_INIT_FATAL_SBR_INVALID_FREQ_COEFFS;
453
0
      }
454
568
      if ((2 * sampling_freq == 44100) && ((*ptr_k2 - *ptr_k0) > MAXIMUM_FREQ_COEFFS_USAC)) {
455
0
        return IA_EXHEAACE_INIT_FATAL_SBR_INVALID_FREQ_COEFFS;
456
0
      }
457
568
      if ((2 * sampling_freq >= 48000) && ((*ptr_k2 - *ptr_k0) > MAXIMUM_FREQ_COEFFS_USAC)) {
458
0
        return IA_EXHEAACE_INIT_FATAL_SBR_INVALID_FREQ_COEFFS;
459
0
      }
460
2.27k
    } else {
461
2.27k
      if (sampling_freq <= 32000) {
462
1.05k
        if ((*ptr_k2 - *ptr_k0) > MAXIMUM_FREQ_COEFFS_LE32KHZ) {
463
0
          return IA_EXHEAACE_INIT_FATAL_SBR_INVALID_FREQ_COEFFS;
464
0
        }
465
1.21k
      } else if (sampling_freq == 44100) {
466
500
        if ((*ptr_k2 - *ptr_k0) > MAXIMUM_FREQ_COEFFS_EQ44KHZ) {
467
0
          return IA_EXHEAACE_INIT_FATAL_SBR_INVALID_FREQ_COEFFS;
468
0
        }
469
713
      } else if (sampling_freq >= 48000) {
470
712
        if ((*ptr_k2 - *ptr_k0) > MAXIMUM_FREQ_COEFFS_GE48KHZ) {
471
7
          return IA_EXHEAACE_INIT_FATAL_SBR_INVALID_FREQ_COEFFS;
472
7
        }
473
712
      } else {
474
1
        return IA_EXHEAACE_INIT_FATAL_SBR_INVALID_FREQ_COEFFS;
475
1
      }
476
2.27k
    }
477
3.04k
  } else {
478
3.04k
    if (sampling_freq <= 32000) {
479
1.10k
      if ((*ptr_k2 - *ptr_k0) > MAXIMUM_FREQ_COEFFS_LE32KHZ) {
480
0
        return IA_EXHEAACE_INIT_FATAL_SBR_INVALID_FREQ_COEFFS;
481
0
      }
482
1.93k
    } else if (sampling_freq == 44100) {
483
918
      if ((*ptr_k2 - *ptr_k0) > MAXIMUM_FREQ_COEFFS_EQ44KHZ) {
484
0
        return IA_EXHEAACE_INIT_FATAL_SBR_INVALID_FREQ_COEFFS;
485
0
      }
486
1.02k
    } else if (sampling_freq >= 48000) {
487
1.02k
      if ((*ptr_k2 - *ptr_k0) > MAXIMUM_FREQ_COEFFS_GE48KHZ) {
488
0
        return IA_EXHEAACE_INIT_FATAL_SBR_INVALID_FREQ_COEFFS;
489
0
      }
490
1.02k
    } else {
491
0
      return IA_EXHEAACE_INIT_FATAL_SBR_INVALID_FREQ_COEFFS;
492
0
    }
493
3.04k
  }
494
495
5.87k
  if ((*ptr_k2 - *ptr_k0) < 0) {
496
0
    return IA_EXHEAACE_INIT_FATAL_SBR_INVALID_FREQ_COEFFS;
497
0
  }
498
499
5.87k
  return IA_NO_ERROR;
500
5.87k
}
501
502
IA_ERRORCODE
503
ixheaace_update_freq_scale(UWORD8 *ptr_k_master, WORD32 *ptr_num_bands, const WORD32 k0,
504
                           const WORD32 k2, const WORD32 freq_scale, const WORD32 alter_scale,
505
                           ixheaace_sr_mode sbr_rate)
506
507
5.87k
{
508
5.87k
  IA_ERRORCODE err_code = IA_NO_ERROR;
509
5.87k
  WORD32 b_p_o = 0;
510
5.87k
  WORD32 dk = 0;
511
512
5.87k
  FLOAT32 warp;
513
5.87k
  WORD32 k1 = 0, i;
514
5.87k
  WORD32 num_bands0;
515
5.87k
  WORD32 num_bands1;
516
5.87k
  WORD32 diff_tot[IXHEAACE_MAXIMUM_OCTAVE + IXHEAACE_MAXIMUM_SECOND_REGION] = {0};
517
5.87k
  WORD32 *diff0 = diff_tot;
518
5.87k
  WORD32 *diff1 = diff_tot + IXHEAACE_MAXIMUM_OCTAVE;
519
5.87k
  WORD32 k2_achived;
520
5.87k
  WORD32 k2_diff;
521
5.87k
  WORD32 incr = 0;
522
523
5.87k
  switch (freq_scale) {
524
3.11k
    case 1:
525
3.11k
      b_p_o = 12;
526
3.11k
      break;
527
1.17k
    case 2:
528
1.17k
      b_p_o = 10;
529
1.17k
      break;
530
1.59k
    case 3:
531
1.59k
      b_p_o = 8;
532
1.59k
      break;
533
5.87k
  }
534
535
5.87k
  if (freq_scale > 0) {
536
5.87k
    if (alter_scale == 0) {
537
0
      warp = 1.0f;
538
5.87k
    } else {
539
5.87k
      warp = 1.3f;
540
5.87k
    }
541
542
5.87k
    if (IXHEAACE_QUAD_RATE == sbr_rate) {
543
568
      if (k0 < b_p_o) {
544
0
        b_p_o = (k0 >> 1) * 2;
545
0
      }
546
568
    }
547
548
5.87k
    if (4 * k2 >= 9 * k0) {
549
4.17k
      k1 = 2 * k0;
550
4.17k
      num_bands0 = ixheaace_number_of_bands(b_p_o, k0, k1, 1.0f);
551
4.17k
      num_bands1 = ixheaace_number_of_bands(b_p_o, k1, k2, warp);
552
553
4.17k
      ixheaace_calc_bands(diff0, k0, k1, num_bands0);
554
4.17k
      ixheaace_shellsort_int(diff0, num_bands0);
555
556
4.17k
      if (diff0[0] == 0) {
557
3
        return IA_EXHEAACE_INIT_FATAL_SBR_INVALID_NUM_BANDS;
558
3
      }
559
560
4.17k
      ixheaace_cum_sum(k0, diff0, num_bands0, ptr_k_master);
561
562
4.17k
      ixheaace_calc_bands(diff1, k1, k2, num_bands1);
563
4.17k
      ixheaace_shellsort_int(diff1, num_bands1);
564
565
4.17k
      if (diff0[num_bands0 - 1] > diff1[0]) {
566
267
        ixheaace_modify_bands(diff0[num_bands0 - 1], diff1, num_bands1);
567
267
      }
568
569
4.17k
      ixheaace_cum_sum(k1, diff1, num_bands1, &ptr_k_master[num_bands0]);
570
4.17k
      *ptr_num_bands = num_bands0 + num_bands1;
571
4.17k
    } else {
572
1.70k
      k1 = k2;
573
1.70k
      num_bands0 = ixheaace_number_of_bands(b_p_o, k0, k1, 1.0f);
574
575
1.70k
      ixheaace_calc_bands(diff0, k0, k1, num_bands0);
576
1.70k
      ixheaace_shellsort_int(diff0, num_bands0);
577
578
1.70k
      if (diff0[0] == 0) {
579
0
        return IA_EXHEAACE_INIT_FATAL_SBR_INVALID_NUM_BANDS;
580
0
      }
581
582
1.70k
      ixheaace_cum_sum(k0, diff0, num_bands0, ptr_k_master);
583
584
1.70k
      *ptr_num_bands = num_bands0;
585
1.70k
    }
586
5.87k
  } else {
587
0
    if (alter_scale == 0) {
588
0
      dk = 1;
589
0
      num_bands0 = 2 * ((k2 - k0) / 2);
590
0
    } else {
591
0
      dk = 2;
592
0
      num_bands0 = 2 * (((k2 - k0) / dk + 1) / 2);
593
0
    }
594
595
0
    k2_achived = k0 + num_bands0 * dk;
596
0
    k2_diff = k2 - k2_achived;
597
598
0
    for (i = 0; i < num_bands0; i++) {
599
0
      diff_tot[i] = dk;
600
0
    }
601
602
0
    if (k2_diff < 0) {
603
0
      incr = 1;
604
0
      i = 0;
605
0
    }
606
607
0
    if (k2_diff > 0) {
608
0
      incr = -1;
609
610
0
      i = num_bands0 - 1;
611
0
    }
612
613
0
    while (k2_diff != 0) {
614
0
      if (i < 0) break;
615
0
      diff_tot[i] = diff_tot[i] - incr;
616
617
0
      i = i + incr;
618
619
0
      k2_diff = k2_diff + incr;
620
0
    }
621
622
0
    ixheaace_cum_sum(k0, diff_tot, num_bands0, ptr_k_master);
623
624
0
    *ptr_num_bands = num_bands0;
625
0
  }
626
627
5.87k
  if (*ptr_num_bands < 1) {
628
0
    return IA_EXHEAACE_INIT_FATAL_SBR_INVALID_NUM_BANDS;
629
0
  }
630
631
5.87k
  if (sbr_rate == IXHEAACE_QUAD_RATE) {
632
9.14k
    for (i = 1; i < *ptr_num_bands; i++) {
633
8.57k
      if (!(ptr_k_master[i] - ptr_k_master[i - 1] <= k0 - 2)) {
634
0
        return IA_EXHEAACE_INIT_FATAL_SBR_INVALID_NUM_BANDS;
635
0
      }
636
8.57k
    }
637
565
  }
638
639
5.87k
  return err_code;
640
5.87k
}
641
642
VOID ixheaace_update_high_res(UWORD8 *ptr_hires, WORD32 *ptr_num_hires, UWORD8 *ptr_k_master,
643
                              WORD32 num_master, WORD32 *ptr_xover_band,
644
5.87k
                              ixheaace_sr_mode dr_or_sr, WORD32 num_qmf_ch) {
645
5.87k
  WORD32 i;
646
5.87k
  WORD32 divider;
647
5.87k
  WORD32 max1, max2;
648
649
5.87k
  divider = (dr_or_sr == IXHEAACE_DUAL_RATE) ? 2 : 1;
650
5.87k
  if (dr_or_sr == IXHEAACE_QUAD_RATE) {
651
565
    divider = 4;
652
565
  }
653
654
5.87k
  if ((ptr_k_master[*ptr_xover_band] > (num_qmf_ch / divider)) ||
655
5.86k
      (*ptr_xover_band > num_master)) {
656
3
    max1 = 0;
657
3
    max2 = num_master;
658
659
3
    while ((ptr_k_master[max1 + 1] < (num_qmf_ch / divider)) && ((max1 + 1) < max2)) {
660
0
      max1++;
661
0
    }
662
663
3
    *ptr_xover_band = max1;
664
3
  }
665
666
5.87k
  *ptr_num_hires = num_master - *ptr_xover_band;
667
668
86.7k
  for (i = *ptr_xover_band; i <= num_master; i++) {
669
80.8k
    ptr_hires[i - *ptr_xover_band] = ptr_k_master[i];
670
80.8k
  }
671
5.87k
}
672
673
VOID ixheaace_update_low_res(UWORD8 *ptr_lores, WORD32 *ptr_num_lores, UWORD8 *ptr_hires,
674
5.87k
                             WORD32 ptr_num_hires) {
675
5.87k
  WORD32 i;
676
677
5.87k
  if (ptr_num_hires % 2 == 0) {
678
5.87k
    *ptr_num_lores = ptr_num_hires / 2;
679
680
49.2k
    for (i = 0; i <= *ptr_num_lores; i++) {
681
43.3k
      ptr_lores[i] = ptr_hires[i * 2];
682
43.3k
    }
683
5.87k
  } else {
684
0
    *ptr_num_lores = (ptr_num_hires + 1) / 2;
685
686
0
    ptr_lores[0] = ptr_hires[0];
687
688
0
    for (i = 1; i <= *ptr_num_lores; i++) {
689
0
      ptr_lores[i] = ptr_hires[i * 2 - 1];
690
0
    }
691
0
  }
692
5.87k
}