Coverage Report

Created: 2025-07-12 07:02

/src/libxaac/encoder/ixheaace_nf.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 <float.h>
22
#include <string.h>
23
#include <math.h>
24
#include "iusace_cnst.h"
25
#include "iusace_type_def.h"
26
#include "ixheaac_constants.h"
27
#include "iusace_bitbuffer.h"
28
#include "iusace_tns_usac.h"
29
#include "iusace_fd_quant.h"
30
#include "ixheaac_basic_ops32.h"
31
#include "ixheaac_basic_ops40.h"
32
#include "ixheaac_basic_ops.h"
33
#include "ixheaace_nf.h"
34
35
static VOID iusace_noise_filling_limiter(FLOAT64 *energy, FLOAT64 *ptr_spec,
36
                                         WORD32 *ptr_quant_spec, WORD32 n0_by_4,
37
                                         WORD32 *ptr_sfb_offset, WORD32 sb, WORD32 cntr,
38
12.6M
                                         FLOAT64 *ptr_highest_tone) {
39
12.6M
  WORD32 n, i;
40
12.6M
  FLOAT64 tone_energy;
41
12.6M
  FLOAT64 tot_tone_energy = 0.0;
42
43
12.6M
  if (!n0_by_4) return;
44
10.4M
  if (cntr <= n0_by_4) return;
45
46
10.4M
  memset(ptr_highest_tone, 0, n0_by_4 * sizeof(*ptr_highest_tone));
47
48
  /* finds the n0_by_4 strongest bins */
49
290M
  for (i = ptr_sfb_offset[sb]; i < ptr_sfb_offset[sb + 1]; i++) {
50
280M
    if (!ptr_quant_spec[i]) {
51
256M
      tone_energy = ptr_spec[i] * ptr_spec[i];
52
53
2.14G
      for (n = 0; n < n0_by_4; n++) {
54
1.92G
        if (tone_energy > ptr_highest_tone[n]) {
55
33.9M
          memmove(ptr_highest_tone + 1 + n, ptr_highest_tone + n,
56
33.9M
                  (n0_by_4 - n - 1) * sizeof(*ptr_highest_tone));
57
33.9M
          ptr_highest_tone[n] = tone_energy;
58
33.9M
          break;
59
33.9M
        }
60
1.92G
      }
61
256M
    }
62
280M
  }
63
  /* remove the contribution of the highest_tone components */
64
73.4M
  for (n = 0; n < n0_by_4; n++) tot_tone_energy += ptr_highest_tone[n];
65
66
10.4M
  FLOAT64 diff = *energy - tot_tone_energy;
67
  //If the difference is within 1% of total energy, no need to send any energy
68
10.4M
  if (diff < 0.01*(*energy))
69
1.17M
  {
70
1.17M
    *energy = 0.0;
71
1.17M
  }
72
9.31M
  else
73
9.31M
  {
74
9.31M
    *energy = diff;
75
9.31M
  }
76
77
  /* add the average component energy */
78
10.4M
  *energy += n0_by_4 * (*energy) / (cntr - n0_by_4);
79
10.4M
  return;
80
10.4M
}
81
82
VOID iusace_noise_filling(WORD32 *noise_level, WORD32 *noise_offset, FLOAT64 *ptr_quant_spec,
83
                          ia_usac_quant_info_struct *pstr_quant_info, WORD32 *ptr_sfb_offset,
84
                          WORD32 max_sfb, WORD32 window_size_samples, WORD32 num_window_groups,
85
                          const WORD32 *ptr_window_group_length,
86
191k
                          WORD32 noise_filling_start_offset, FLOAT64 *ptr_scratch_buf) {
87
191k
  FLOAT64 energy;
88
191k
  FLOAT64 noise_level_temp;
89
191k
  FLOAT64 noise_offset_temp;
90
91
191k
  FLOAT64 sum_sfb_on, sum_sfb_off;
92
191k
  FLOAT64 e_sfb_on, e_sfb_off;
93
94
191k
  WORD32 n0;
95
191k
  WORD32 start_sfb, sfb, i;
96
191k
  WORD32 band_quantized_to_zero;
97
98
191k
  FLOAT64 alpha = 0.15; /* prudence factor */
99
191k
  WORD32 grp = 0;
100
101
191k
  e_sfb_on = 1e-6;
102
191k
  e_sfb_off = 1e-6;
103
104
191k
  sum_sfb_on = 1e-6;
105
191k
  sum_sfb_off = 1e-6;
106
107
191k
  *noise_offset = 0;
108
191k
  *noise_level = 0;
109
110
1.46M
  for (sfb = 0; sfb < max_sfb; sfb++) {
111
1.45M
    if (ptr_sfb_offset[sfb + 1] > noise_filling_start_offset) break;
112
1.45M
  }
113
191k
  start_sfb = sfb;
114
749k
  for (grp = 0; grp < num_window_groups; grp++) {
115
557k
    WORD32 grp_win = 0;
116
7.51M
    for (sfb = start_sfb; sfb < max_sfb; sfb++) {
117
6.95M
      band_quantized_to_zero = 1;
118
19.6M
      for (grp_win = 0; grp_win < ptr_window_group_length[grp]; grp_win++) {
119
12.6M
        WORD32 offset = grp_win * window_size_samples;
120
12.6M
        energy = 0;
121
12.6M
        n0 = 0;
122
333M
        for (i = ptr_sfb_offset[sfb]; i < ptr_sfb_offset[sfb + 1]; i++) {
123
          /* calculate energy if the quantized value is non zero */
124
320M
          if (!pstr_quant_info->quant_degroup[offset + i]) {
125
257M
            energy += ptr_quant_spec[offset + i] * ptr_quant_spec[offset + i];
126
257M
            n0++;
127
257M
          } else {
128
            /* All quantized values are not zero */
129
62.6M
            band_quantized_to_zero = 0;
130
62.6M
          }
131
320M
        }
132
133
        /* Remove highest (tonal) contributions */
134
12.6M
        iusace_noise_filling_limiter(&energy, &ptr_quant_spec[offset],
135
12.6M
                                     &pstr_quant_info->quant_degroup[offset], n0 / 4,
136
12.6M
                                     ptr_sfb_offset, sfb, n0, ptr_scratch_buf);
137
138
12.6M
        if (band_quantized_to_zero == 0) {
139
5.94M
          e_sfb_on += energy;
140
5.94M
          sum_sfb_on += pow(2., 0.5 * pstr_quant_info->scale_factor[sfb] - 50) * n0;
141
5.94M
        } else
142
        /* subband is completely zeroed  */
143
6.75M
        {
144
6.75M
          e_sfb_off += energy;
145
6.75M
          sum_sfb_off += pow(2., 0.5 * pstr_quant_info->scale_factor[sfb] - 58) *
146
6.75M
                         (ptr_sfb_offset[sfb + 1] - ptr_sfb_offset[sfb]);
147
6.75M
        }
148
12.6M
      }
149
6.95M
    }
150
557k
  }
151
152
191k
  if (num_window_groups > 1) alpha = alpha * 0.15;
153
154
191k
  if (sum_sfb_on) {
155
191k
    noise_level_temp = 1.5 * (log(alpha * e_sfb_on) - log(sum_sfb_on)) / log(2.0) + 14.0;
156
157
    /* quantize to nearest integer */
158
191k
    *noise_level = (WORD32)(noise_level_temp + 0.5);
159
160
    /* noise level limited to quantization range [0,7] */
161
191k
    *noise_level = MAX(*noise_level, 0);
162
191k
    *noise_level = MIN(*noise_level, 7);
163
164
191k
    if (*noise_level != 0) {
165
175k
      noise_offset_temp =
166
175k
          2. * log(alpha * e_sfb_off * sum_sfb_on / sum_sfb_off / e_sfb_on) / log(2.);
167
168
      /* quantize to nearest integer */
169
175k
      *noise_offset = (WORD32)(noise_offset_temp + 0.5);
170
171
      /* noise offset limited to quantization range [0,31] */
172
175k
      *noise_level = *noise_offset <= 0 ? 0 : *noise_level;
173
175k
      *noise_offset = MIN(*noise_offset, 31);
174
175k
      *noise_offset = MAX(*noise_offset, 0);
175
175k
    }
176
191k
  }
177
191k
  return;
178
191k
}