Coverage Report

Created: 2025-08-24 07:17

/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
5.74M
                                         FLOAT64 *ptr_highest_tone) {
39
5.74M
  WORD32 n, i;
40
5.74M
  FLOAT64 tone_energy;
41
5.74M
  FLOAT64 tot_tone_energy = 0.0;
42
43
5.74M
  if (!n0_by_4) return;
44
4.25M
  if (cntr <= n0_by_4) return;
45
46
4.25M
  memset(ptr_highest_tone, 0, n0_by_4 * sizeof(*ptr_highest_tone));
47
48
  /* finds the n0_by_4 strongest bins */
49
114M
  for (i = ptr_sfb_offset[sb]; i < ptr_sfb_offset[sb + 1]; i++) {
50
110M
    if (!ptr_quant_spec[i]) {
51
97.1M
      tone_energy = ptr_spec[i] * ptr_spec[i];
52
53
853M
      for (n = 0; n < n0_by_4; n++) {
54
765M
        if (tone_energy > ptr_highest_tone[n]) {
55
9.74M
          memmove(ptr_highest_tone + 1 + n, ptr_highest_tone + n,
56
9.74M
                  (n0_by_4 - n - 1) * sizeof(*ptr_highest_tone));
57
9.74M
          ptr_highest_tone[n] = tone_energy;
58
9.74M
          break;
59
9.74M
        }
60
765M
      }
61
97.1M
    }
62
110M
  }
63
  /* remove the contribution of the highest_tone components */
64
28.2M
  for (n = 0; n < n0_by_4; n++) tot_tone_energy += ptr_highest_tone[n];
65
66
4.25M
  FLOAT64 diff = *energy - tot_tone_energy;
67
  //If the difference is within 1% of total energy, no need to send any energy
68
4.25M
  if (diff < 0.01*(*energy))
69
128k
  {
70
128k
    *energy = 0.0;
71
128k
  }
72
4.12M
  else
73
4.12M
  {
74
4.12M
    *energy = diff;
75
4.12M
  }
76
77
  /* add the average component energy */
78
4.25M
  *energy += n0_by_4 * (*energy) / (cntr - n0_by_4);
79
4.25M
  return;
80
4.25M
}
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
151k
                          WORD32 noise_filling_start_offset, FLOAT64 *ptr_scratch_buf) {
87
151k
  FLOAT64 energy;
88
151k
  FLOAT64 noise_level_temp;
89
151k
  FLOAT64 noise_offset_temp;
90
91
151k
  FLOAT64 sum_sfb_on, sum_sfb_off;
92
151k
  FLOAT64 e_sfb_on, e_sfb_off;
93
94
151k
  WORD32 n0;
95
151k
  WORD32 start_sfb, sfb, i;
96
151k
  WORD32 band_quantized_to_zero;
97
98
151k
  FLOAT64 alpha = 0.15; /* prudence factor */
99
151k
  WORD32 grp = 0;
100
101
151k
  e_sfb_on = 1e-6;
102
151k
  e_sfb_off = 1e-6;
103
104
151k
  sum_sfb_on = 1e-6;
105
151k
  sum_sfb_off = 1e-6;
106
107
151k
  *noise_offset = 0;
108
151k
  *noise_level = 0;
109
110
2.26M
  for (sfb = 0; sfb < max_sfb; sfb++) {
111
2.25M
    if (ptr_sfb_offset[sfb + 1] > noise_filling_start_offset) break;
112
2.25M
  }
113
151k
  start_sfb = sfb;
114
424k
  for (grp = 0; grp < num_window_groups; grp++) {
115
273k
    WORD32 grp_win = 0;
116
4.16M
    for (sfb = start_sfb; sfb < max_sfb; sfb++) {
117
3.88M
      band_quantized_to_zero = 1;
118
9.62M
      for (grp_win = 0; grp_win < ptr_window_group_length[grp]; grp_win++) {
119
5.74M
        WORD32 offset = grp_win * window_size_samples;
120
5.74M
        energy = 0;
121
5.74M
        n0 = 0;
122
144M
        for (i = ptr_sfb_offset[sfb]; i < ptr_sfb_offset[sfb + 1]; i++) {
123
          /* calculate energy if the quantized value is non zero */
124
139M
          if (!pstr_quant_info->quant_degroup[offset + i]) {
125
98.1M
            energy += ptr_quant_spec[offset + i] * ptr_quant_spec[offset + i];
126
98.1M
            n0++;
127
98.1M
          } else {
128
            /* All quantized values are not zero */
129
41.0M
            band_quantized_to_zero = 0;
130
41.0M
          }
131
139M
        }
132
133
        /* Remove highest (tonal) contributions */
134
5.74M
        iusace_noise_filling_limiter(&energy, &ptr_quant_spec[offset],
135
5.74M
                                     &pstr_quant_info->quant_degroup[offset], n0 / 4,
136
5.74M
                                     ptr_sfb_offset, sfb, n0, ptr_scratch_buf);
137
138
5.74M
        if (band_quantized_to_zero == 0) {
139
2.60M
          e_sfb_on += energy;
140
2.60M
          sum_sfb_on += pow(2., 0.5 * pstr_quant_info->scale_factor[sfb] - 50) * n0;
141
2.60M
        } else
142
        /* subband is completely zeroed  */
143
3.13M
        {
144
3.13M
          e_sfb_off += energy;
145
3.13M
          sum_sfb_off += pow(2., 0.5 * pstr_quant_info->scale_factor[sfb] - 58) *
146
3.13M
                         (ptr_sfb_offset[sfb + 1] - ptr_sfb_offset[sfb]);
147
3.13M
        }
148
5.74M
      }
149
3.88M
    }
150
273k
  }
151
152
151k
  if (num_window_groups > 1) alpha = alpha * 0.15;
153
154
151k
  if (sum_sfb_on) {
155
151k
    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
151k
    *noise_level = (WORD32)(noise_level_temp + 0.5);
159
160
    /* noise level limited to quantization range [0,7] */
161
151k
    *noise_level = MAX(*noise_level, 0);
162
151k
    *noise_level = MIN(*noise_level, 7);
163
164
151k
    if (*noise_level != 0) {
165
131k
      noise_offset_temp =
166
131k
          2. * log(alpha * e_sfb_off * sum_sfb_on / sum_sfb_off / e_sfb_on) / log(2.);
167
168
      /* quantize to nearest integer */
169
131k
      *noise_offset = (WORD32)(noise_offset_temp + 0.5);
170
171
      /* noise offset limited to quantization range [0,31] */
172
131k
      *noise_level = *noise_offset <= 0 ? 0 : *noise_level;
173
131k
      *noise_offset = MIN(*noise_offset, 31);
174
131k
      *noise_offset = MAX(*noise_offset, 0);
175
131k
    }
176
151k
  }
177
151k
  return;
178
151k
}