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