xref: /aosp_15_r20/external/libxaac/encoder/ixheaace_stereo_preproc.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 <math.h>
22 #include <stdlib.h>
23 #include <string.h>
24 
25 #include "ixheaac_type_def.h"
26 #include "ixheaac_constants.h"
27 #include "ixheaace_aac_constants.h"
28 #include "ixheaac_basic_ops32.h"
29 #include "ixheaac_basic_ops16.h"
30 #include "ixheaac_basic_ops40.h"
31 #include "ixheaac_basic_ops.h"
32 
33 #include "ixheaace_psy_const.h"
34 #include "ixheaace_tns.h"
35 #include "ixheaace_tns_params.h"
36 #include "ixheaace_rom.h"
37 #include "ixheaace_common_rom.h"
38 #include "ixheaace_bitbuffer.h"
39 
40 #include "ixheaace_block_switch.h"
41 #include "ixheaace_psy_data.h"
42 #include "ixheaace_interface.h"
43 #include "ixheaace_adjust_threshold_data.h"
44 #include "ixheaace_dynamic_bits.h"
45 #include "ixheaace_qc_data.h"
46 #include "ixheaac_error_standards.h"
47 #include "ixheaace_stereo_preproc.h"
48 #include "ixheaace_common_utils.h"
49 
iaace_init_stereo_pre_processing(ixheaace_stereo_pre_pro_pstr pstr_stereo_pre_pro,WORD32 no_channels,WORD32 bit_rate,WORD32 sample_rate,FLOAT32 used_scf_ratio)50 IA_ERRORCODE iaace_init_stereo_pre_processing(ixheaace_stereo_pre_pro_pstr pstr_stereo_pre_pro,
51                                         WORD32 no_channels, WORD32 bit_rate, WORD32 sample_rate,
52                                         FLOAT32 used_scf_ratio) {
53   FLOAT32 bpf = bit_rate * 1024.0f / sample_rate;
54   FLOAT32 tmp;
55 
56   memset(pstr_stereo_pre_pro, 0, sizeof(ixheaace_stereo_pre_pro_struct));
57 
58   if (no_channels == 2) {
59     (pstr_stereo_pre_pro)->stereo_attenuation_flag = 1;
60 
61     (pstr_stereo_pre_pro)->norm_pe_fac = 230.0f * used_scf_ratio / bpf;
62     (pstr_stereo_pre_pro)->impact_factor =
63         MIN(1, 400000.0f / (FLOAT32)(bit_rate - sample_rate * sample_rate / 72000.0f));
64     (pstr_stereo_pre_pro)->inc_stereo_attenuation = 22050.0f / sample_rate * 400.0f / bpf;
65     (pstr_stereo_pre_pro)->dec_stereo_attenuation = 22050.0f / sample_rate * 200.0f / bpf;
66 
67     (pstr_stereo_pre_pro)->const_attenuation = 0.0f;
68     (pstr_stereo_pre_pro)->stereo_attenuation_max = 12.0f;
69 
70     /* energy ratio thresholds (dB) */
71     (pstr_stereo_pre_pro)->sm_min = 0.0f;
72     (pstr_stereo_pre_pro)->sm_max = 15.0f;
73     (pstr_stereo_pre_pro)->lr_min = 10.0f;
74     (pstr_stereo_pre_pro)->lr_max = 30.0f;
75 
76     /* pe thresholds */
77     (pstr_stereo_pre_pro)->pe_crit = 1200.0f;
78     (pstr_stereo_pre_pro)->pe_min = 700.0f;
79     (pstr_stereo_pre_pro)->pe_impact_max = 100.0f;
80 
81     /* init start values */
82     (pstr_stereo_pre_pro)->average_freq_energy_l = 0.0f;
83     (pstr_stereo_pre_pro)->average_freq_energy_r = 0.0f;
84     (pstr_stereo_pre_pro)->average_freq_energy_s = 0.0f;
85     (pstr_stereo_pre_pro)->average_freq_energy_m = 0.0f;
86     (pstr_stereo_pre_pro)->smoothed_pe_sum_sum = 7000.0f; /* typical start value */
87     (pstr_stereo_pre_pro)->avg_s_to_m = -10.0f;           /* typical start value */
88     (pstr_stereo_pre_pro)->last_l_to_r = 0.0f;
89     (pstr_stereo_pre_pro)->last_nrg_lr = 0.0f;
90 
91     tmp = 1.0f - (bpf / 2600.0f);
92 
93     tmp = MAX(tmp, 0.0f);
94 
95     (pstr_stereo_pre_pro)->stereo_attenuation =
96         tmp * (pstr_stereo_pre_pro)->stereo_attenuation_max;
97   }
98 
99   return IA_NO_ERROR;
100 }
101 
iaace_apply_stereo_preproc(ixheaace_stereo_pre_pro_pstr pstr_stereo_pre_pro,WORD32 num_channels,ixheaace_element_info * pstr_elem_info,FLOAT32 * ptr_time_data,WORD32 granule_len)102 VOID iaace_apply_stereo_preproc(ixheaace_stereo_pre_pro_pstr pstr_stereo_pre_pro,
103                                 WORD32 num_channels, ixheaace_element_info *pstr_elem_info,
104                                 FLOAT32 *ptr_time_data, WORD32 granule_len) {
105   FLOAT32 sm_ratio, s_to_m;
106   FLOAT32 lr_ratio, l_to_r, delta_l_to_r, delta_nrg;
107   FLOAT32 en_impact, pe_impact, pe_norm;
108   FLOAT32 att, att_aimed;
109   FLOAT32 max_inc, max_dec, swift_factor;
110   FLOAT32 delta = 0.1f;
111   FLOAT32 fac = pstr_stereo_pre_pro->stereo_attenuation_fac;
112   FLOAT32 m_part, upper, div;
113   FLOAT32 l_fac, r_fac;
114   FLOAT32 L, R;
115   WORD32 i;
116 
117   if (!pstr_stereo_pre_pro->stereo_attenuation_flag) {
118     return;
119   }
120 
121   m_part = 2.0f * pstr_stereo_pre_pro->average_freq_energy_m * (1.0f - fac * fac);
122 
123   upper = pstr_stereo_pre_pro->average_freq_energy_l * (1.0f + fac) +
124           pstr_stereo_pre_pro->average_freq_energy_r * (1.0f - fac) - m_part;
125   div = pstr_stereo_pre_pro->average_freq_energy_r * (1.0f + fac) +
126         pstr_stereo_pre_pro->average_freq_energy_l * (1.0f - fac) - m_part;
127 
128   if ((div == 0.0f) || (upper == 0.0f)) {
129     l_to_r = pstr_stereo_pre_pro->lr_max;
130   } else {
131     lr_ratio = (FLOAT32)fabs(upper / div);
132     l_to_r = (FLOAT32)fabs(10.0f * log10(lr_ratio));
133   }
134 
135   /* Calculate delta energy to previous frame */
136   delta_nrg = (pstr_stereo_pre_pro->average_freq_energy_l +
137                pstr_stereo_pre_pro->average_freq_energy_r + 1.0f) /
138               (pstr_stereo_pre_pro->last_nrg_lr + 1.0f);
139 
140   delta_nrg = (FLOAT32)(fabs(10.0f * log10(delta_nrg)));
141 
142   /* Smooth S/M over time */
143   sm_ratio = (pstr_stereo_pre_pro->average_freq_energy_s + 1.0f) /
144              (pstr_stereo_pre_pro->average_freq_energy_m + 1.0f);
145 
146   s_to_m = (FLOAT32)(10.0f * log10(sm_ratio));
147 
148   pstr_stereo_pre_pro->avg_s_to_m =
149       delta * s_to_m + (1 - delta) * pstr_stereo_pre_pro->avg_s_to_m;
150 
151   en_impact = 1.0f;
152 
153   if (pstr_stereo_pre_pro->avg_s_to_m > pstr_stereo_pre_pro->sm_min) {
154     if (pstr_stereo_pre_pro->avg_s_to_m > pstr_stereo_pre_pro->sm_max) {
155       en_impact = 0.0f;
156     } else {
157       en_impact = (pstr_stereo_pre_pro->sm_max - pstr_stereo_pre_pro->avg_s_to_m) /
158                   (pstr_stereo_pre_pro->sm_max - pstr_stereo_pre_pro->sm_min);
159     }
160   }
161 
162   if (l_to_r > pstr_stereo_pre_pro->lr_min) {
163     if (l_to_r > pstr_stereo_pre_pro->lr_max) {
164       en_impact = 0.0f;
165     } else {
166       en_impact *= (pstr_stereo_pre_pro->lr_max - l_to_r) /
167                    (pstr_stereo_pre_pro->lr_max - pstr_stereo_pre_pro->lr_min);
168     }
169   }
170 
171   pe_impact = 0.0f;
172 
173   pe_norm = pstr_stereo_pre_pro->smoothed_pe_sum_sum * pstr_stereo_pre_pro->norm_pe_fac;
174 
175   if (pe_norm > pstr_stereo_pre_pro->pe_min) {
176     pe_impact = ((pe_norm - pstr_stereo_pre_pro->pe_min) /
177                  (pstr_stereo_pre_pro->pe_crit - pstr_stereo_pre_pro->pe_min));
178   }
179 
180   pe_impact = MIN(pe_impact, pstr_stereo_pre_pro->pe_impact_max);
181 
182   att_aimed = MIN((en_impact * pe_impact * pstr_stereo_pre_pro->impact_factor),
183                   pstr_stereo_pre_pro->stereo_attenuation_max);
184 
185   /* Only accept changes if they are large enough */
186   if ((fabs(att_aimed - pstr_stereo_pre_pro->stereo_attenuation) < 1.0f) && (att_aimed != 0.0f)) {
187     att_aimed = pstr_stereo_pre_pro->stereo_attenuation;
188   }
189 
190   att = att_aimed;
191 
192   swift_factor = (6.0f + pstr_stereo_pre_pro->stereo_attenuation) / (10.0f + l_to_r) *
193                  MAX(1.0f, 0.2f * delta_nrg);
194 
195   delta_l_to_r = MAX(3.0f, l_to_r - pstr_stereo_pre_pro->last_l_to_r);
196 
197   max_dec = MIN((delta_l_to_r * delta_l_to_r / 9.0f * swift_factor), 5.0f);
198 
199   max_dec *= pstr_stereo_pre_pro->dec_stereo_attenuation;
200 
201   max_dec = MIN(max_dec, pstr_stereo_pre_pro->stereo_attenuation * 0.8f);
202 
203   delta_l_to_r = MAX(3.0f, pstr_stereo_pre_pro->last_l_to_r - l_to_r);
204 
205   max_inc = MIN((delta_l_to_r * delta_l_to_r / 9.0f * swift_factor), 5.0f);
206 
207   max_inc *= pstr_stereo_pre_pro->inc_stereo_attenuation;
208 
209   att = MIN(att, pstr_stereo_pre_pro->stereo_attenuation + max_inc);
210 
211   att = MAX(att, pstr_stereo_pre_pro->stereo_attenuation - max_dec);
212 
213   pstr_stereo_pre_pro->stereo_attenuation = (pstr_stereo_pre_pro->const_attenuation == 0)
214                                                 ? att
215                                                 : pstr_stereo_pre_pro->const_attenuation;
216 
217   /* Perform attenuation of side channel */
218   pstr_stereo_pre_pro->stereo_attenuation_fac =
219       (FLOAT32)pow(10.0f, 0.05f * (-pstr_stereo_pre_pro->stereo_attenuation));
220 
221   l_fac = 0.5f * (1.0f + pstr_stereo_pre_pro->stereo_attenuation_fac);
222   r_fac = 0.5f * (1.0f - pstr_stereo_pre_pro->stereo_attenuation_fac);
223 
224   for (i = 0; i < granule_len; i++) {
225     L = l_fac * ptr_time_data[num_channels * i + pstr_elem_info->channel_index[0]] +
226         r_fac * ptr_time_data[num_channels * i + pstr_elem_info->channel_index[1]];
227     R = r_fac * ptr_time_data[num_channels * i + pstr_elem_info->channel_index[0]] +
228         l_fac * ptr_time_data[num_channels * i + pstr_elem_info->channel_index[1]];
229 
230     ptr_time_data[num_channels * i + pstr_elem_info->channel_index[0]] = L;
231     ptr_time_data[num_channels * i + pstr_elem_info->channel_index[1]] = R;
232   }
233 
234   pstr_stereo_pre_pro->last_l_to_r = l_to_r;
235 
236   pstr_stereo_pre_pro->last_nrg_lr =
237       pstr_stereo_pre_pro->average_freq_energy_l + pstr_stereo_pre_pro->average_freq_energy_r;
238 }
239 
iaace_update_stereo_pre_process(ixheaace_psy_out_channel ** pstr_psy_out,ixheaace_qc_out_element * pstr_qc_out,ixheaace_stereo_pre_pro_pstr pstr_stereo_pre_pro,FLOAT32 weight_pe_fac)240 VOID iaace_update_stereo_pre_process(ixheaace_psy_out_channel **pstr_psy_out,
241                                      ixheaace_qc_out_element *pstr_qc_out,
242                                      ixheaace_stereo_pre_pro_pstr pstr_stereo_pre_pro,
243                                      FLOAT32 weight_pe_fac) {
244   if (pstr_stereo_pre_pro->stereo_attenuation_flag) {
245     FLOAT32 delta = 0.1f;
246 
247     pstr_stereo_pre_pro->average_freq_energy_l = pstr_psy_out[0]->sfb_sum_lr_energy;
248     pstr_stereo_pre_pro->average_freq_energy_r = pstr_psy_out[1]->sfb_sum_lr_energy;
249     pstr_stereo_pre_pro->average_freq_energy_m = pstr_psy_out[0]->sfb_sum_ms_energy;
250     pstr_stereo_pre_pro->average_freq_energy_s = pstr_psy_out[1]->sfb_sum_ms_energy;
251 
252     pstr_stereo_pre_pro->smoothed_pe_sum_sum =
253         delta * pstr_qc_out->pe * weight_pe_fac +
254         (1 - delta) * pstr_stereo_pre_pro->smoothed_pe_sum_sum;
255   }
256 }
257