/src/libxaac/encoder/ixheaace_stereo_preproc.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 <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 | | |
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 | 1.99k | FLOAT32 used_scf_ratio) { |
53 | 1.99k | FLOAT32 bpf = bit_rate * 1024.0f / sample_rate; |
54 | 1.99k | FLOAT32 tmp; |
55 | | |
56 | 1.99k | memset(pstr_stereo_pre_pro, 0, sizeof(ixheaace_stereo_pre_pro_struct)); |
57 | | |
58 | 1.99k | if (no_channels == 2) { |
59 | 1.99k | (pstr_stereo_pre_pro)->stereo_attenuation_flag = 1; |
60 | | |
61 | 1.99k | (pstr_stereo_pre_pro)->norm_pe_fac = 230.0f * used_scf_ratio / bpf; |
62 | 1.99k | (pstr_stereo_pre_pro)->impact_factor = |
63 | 1.99k | MIN(1, 400000.0f / (FLOAT32)(bit_rate - sample_rate * sample_rate / 72000.0f)); |
64 | 1.99k | (pstr_stereo_pre_pro)->inc_stereo_attenuation = 22050.0f / sample_rate * 400.0f / bpf; |
65 | 1.99k | (pstr_stereo_pre_pro)->dec_stereo_attenuation = 22050.0f / sample_rate * 200.0f / bpf; |
66 | | |
67 | 1.99k | (pstr_stereo_pre_pro)->const_attenuation = 0.0f; |
68 | 1.99k | (pstr_stereo_pre_pro)->stereo_attenuation_max = 12.0f; |
69 | | |
70 | | /* energy ratio thresholds (dB) */ |
71 | 1.99k | (pstr_stereo_pre_pro)->sm_min = 0.0f; |
72 | 1.99k | (pstr_stereo_pre_pro)->sm_max = 15.0f; |
73 | 1.99k | (pstr_stereo_pre_pro)->lr_min = 10.0f; |
74 | 1.99k | (pstr_stereo_pre_pro)->lr_max = 30.0f; |
75 | | |
76 | | /* pe thresholds */ |
77 | 1.99k | (pstr_stereo_pre_pro)->pe_crit = 1200.0f; |
78 | 1.99k | (pstr_stereo_pre_pro)->pe_min = 700.0f; |
79 | 1.99k | (pstr_stereo_pre_pro)->pe_impact_max = 100.0f; |
80 | | |
81 | | /* init start values */ |
82 | 1.99k | (pstr_stereo_pre_pro)->average_freq_energy_l = 0.0f; |
83 | 1.99k | (pstr_stereo_pre_pro)->average_freq_energy_r = 0.0f; |
84 | 1.99k | (pstr_stereo_pre_pro)->average_freq_energy_s = 0.0f; |
85 | 1.99k | (pstr_stereo_pre_pro)->average_freq_energy_m = 0.0f; |
86 | 1.99k | (pstr_stereo_pre_pro)->smoothed_pe_sum_sum = 7000.0f; /* typical start value */ |
87 | 1.99k | (pstr_stereo_pre_pro)->avg_s_to_m = -10.0f; /* typical start value */ |
88 | 1.99k | (pstr_stereo_pre_pro)->last_l_to_r = 0.0f; |
89 | 1.99k | (pstr_stereo_pre_pro)->last_nrg_lr = 0.0f; |
90 | | |
91 | 1.99k | tmp = 1.0f - (bpf / 2600.0f); |
92 | | |
93 | 1.99k | tmp = MAX(tmp, 0.0f); |
94 | | |
95 | 1.99k | (pstr_stereo_pre_pro)->stereo_attenuation = |
96 | 1.99k | tmp * (pstr_stereo_pre_pro)->stereo_attenuation_max; |
97 | 1.99k | } |
98 | | |
99 | 1.99k | return IA_NO_ERROR; |
100 | 1.99k | } |
101 | | |
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 | 167k | FLOAT32 *ptr_time_data, WORD32 granule_len) { |
105 | 167k | FLOAT32 sm_ratio, s_to_m; |
106 | 167k | FLOAT32 lr_ratio, l_to_r, delta_l_to_r, delta_nrg; |
107 | 167k | FLOAT32 en_impact, pe_impact, pe_norm; |
108 | 167k | FLOAT32 att, att_aimed; |
109 | 167k | FLOAT32 max_inc, max_dec, swift_factor; |
110 | 167k | FLOAT32 delta = 0.1f; |
111 | 167k | FLOAT32 fac = pstr_stereo_pre_pro->stereo_attenuation_fac; |
112 | 167k | FLOAT32 m_part, upper, div; |
113 | 167k | FLOAT32 l_fac, r_fac; |
114 | 167k | FLOAT32 L, R; |
115 | 167k | WORD32 i; |
116 | | |
117 | 167k | if (!pstr_stereo_pre_pro->stereo_attenuation_flag) { |
118 | 58.9k | return; |
119 | 58.9k | } |
120 | | |
121 | 108k | m_part = 2.0f * pstr_stereo_pre_pro->average_freq_energy_m * (1.0f - fac * fac); |
122 | | |
123 | 108k | upper = pstr_stereo_pre_pro->average_freq_energy_l * (1.0f + fac) + |
124 | 108k | pstr_stereo_pre_pro->average_freq_energy_r * (1.0f - fac) - m_part; |
125 | 108k | div = pstr_stereo_pre_pro->average_freq_energy_r * (1.0f + fac) + |
126 | 108k | pstr_stereo_pre_pro->average_freq_energy_l * (1.0f - fac) - m_part; |
127 | | |
128 | 108k | if ((div == 0.0f) || (upper == 0.0f)) { |
129 | 10.9k | l_to_r = pstr_stereo_pre_pro->lr_max; |
130 | 97.8k | } else { |
131 | 97.8k | lr_ratio = (FLOAT32)fabs(upper / div); |
132 | 97.8k | l_to_r = (FLOAT32)fabs(10.0f * log10(lr_ratio)); |
133 | 97.8k | } |
134 | | |
135 | | /* Calculate delta energy to previous frame */ |
136 | 108k | delta_nrg = (pstr_stereo_pre_pro->average_freq_energy_l + |
137 | 108k | pstr_stereo_pre_pro->average_freq_energy_r + 1.0f) / |
138 | 108k | (pstr_stereo_pre_pro->last_nrg_lr + 1.0f); |
139 | | |
140 | 108k | delta_nrg = (FLOAT32)(fabs(10.0f * log10(delta_nrg))); |
141 | | |
142 | | /* Smooth S/M over time */ |
143 | 108k | sm_ratio = (pstr_stereo_pre_pro->average_freq_energy_s + 1.0f) / |
144 | 108k | (pstr_stereo_pre_pro->average_freq_energy_m + 1.0f); |
145 | | |
146 | 108k | s_to_m = (FLOAT32)(10.0f * log10(sm_ratio)); |
147 | | |
148 | 108k | pstr_stereo_pre_pro->avg_s_to_m = |
149 | 108k | delta * s_to_m + (1 - delta) * pstr_stereo_pre_pro->avg_s_to_m; |
150 | | |
151 | 108k | en_impact = 1.0f; |
152 | | |
153 | 108k | if (pstr_stereo_pre_pro->avg_s_to_m > pstr_stereo_pre_pro->sm_min) { |
154 | 1.83k | if (pstr_stereo_pre_pro->avg_s_to_m > pstr_stereo_pre_pro->sm_max) { |
155 | 96 | en_impact = 0.0f; |
156 | 1.73k | } else { |
157 | 1.73k | en_impact = (pstr_stereo_pre_pro->sm_max - pstr_stereo_pre_pro->avg_s_to_m) / |
158 | 1.73k | (pstr_stereo_pre_pro->sm_max - pstr_stereo_pre_pro->sm_min); |
159 | 1.73k | } |
160 | 1.83k | } |
161 | | |
162 | 108k | if (l_to_r > pstr_stereo_pre_pro->lr_min) { |
163 | 14.3k | if (l_to_r > pstr_stereo_pre_pro->lr_max) { |
164 | 1.44k | en_impact = 0.0f; |
165 | 12.9k | } else { |
166 | 12.9k | en_impact *= (pstr_stereo_pre_pro->lr_max - l_to_r) / |
167 | 12.9k | (pstr_stereo_pre_pro->lr_max - pstr_stereo_pre_pro->lr_min); |
168 | 12.9k | } |
169 | 14.3k | } |
170 | | |
171 | 108k | pe_impact = 0.0f; |
172 | | |
173 | 108k | pe_norm = pstr_stereo_pre_pro->smoothed_pe_sum_sum * pstr_stereo_pre_pro->norm_pe_fac; |
174 | | |
175 | 108k | if (pe_norm > pstr_stereo_pre_pro->pe_min) { |
176 | 13.9k | pe_impact = ((pe_norm - pstr_stereo_pre_pro->pe_min) / |
177 | 13.9k | (pstr_stereo_pre_pro->pe_crit - pstr_stereo_pre_pro->pe_min)); |
178 | 13.9k | } |
179 | | |
180 | 108k | pe_impact = MIN(pe_impact, pstr_stereo_pre_pro->pe_impact_max); |
181 | | |
182 | 108k | att_aimed = MIN((en_impact * pe_impact * pstr_stereo_pre_pro->impact_factor), |
183 | 108k | pstr_stereo_pre_pro->stereo_attenuation_max); |
184 | | |
185 | | /* Only accept changes if they are large enough */ |
186 | 108k | if ((fabs(att_aimed - pstr_stereo_pre_pro->stereo_attenuation) < 1.0f) && (att_aimed != 0.0f)) { |
187 | 4.99k | att_aimed = pstr_stereo_pre_pro->stereo_attenuation; |
188 | 4.99k | } |
189 | | |
190 | 108k | att = att_aimed; |
191 | | |
192 | 108k | swift_factor = (6.0f + pstr_stereo_pre_pro->stereo_attenuation) / (10.0f + l_to_r) * |
193 | 108k | MAX(1.0f, 0.2f * delta_nrg); |
194 | | |
195 | 108k | delta_l_to_r = MAX(3.0f, l_to_r - pstr_stereo_pre_pro->last_l_to_r); |
196 | | |
197 | 108k | max_dec = MIN((delta_l_to_r * delta_l_to_r / 9.0f * swift_factor), 5.0f); |
198 | | |
199 | 108k | max_dec *= pstr_stereo_pre_pro->dec_stereo_attenuation; |
200 | | |
201 | 108k | max_dec = MIN(max_dec, pstr_stereo_pre_pro->stereo_attenuation * 0.8f); |
202 | | |
203 | 108k | delta_l_to_r = MAX(3.0f, pstr_stereo_pre_pro->last_l_to_r - l_to_r); |
204 | | |
205 | 108k | max_inc = MIN((delta_l_to_r * delta_l_to_r / 9.0f * swift_factor), 5.0f); |
206 | | |
207 | 108k | max_inc *= pstr_stereo_pre_pro->inc_stereo_attenuation; |
208 | | |
209 | 108k | att = MIN(att, pstr_stereo_pre_pro->stereo_attenuation + max_inc); |
210 | | |
211 | 108k | att = MAX(att, pstr_stereo_pre_pro->stereo_attenuation - max_dec); |
212 | | |
213 | 108k | pstr_stereo_pre_pro->stereo_attenuation = (pstr_stereo_pre_pro->const_attenuation == 0) |
214 | 108k | ? att |
215 | 108k | : pstr_stereo_pre_pro->const_attenuation; |
216 | | |
217 | | /* Perform attenuation of side channel */ |
218 | 108k | pstr_stereo_pre_pro->stereo_attenuation_fac = |
219 | 108k | (FLOAT32)pow(10.0f, 0.05f * (-pstr_stereo_pre_pro->stereo_attenuation)); |
220 | | |
221 | 108k | l_fac = 0.5f * (1.0f + pstr_stereo_pre_pro->stereo_attenuation_fac); |
222 | 108k | r_fac = 0.5f * (1.0f - pstr_stereo_pre_pro->stereo_attenuation_fac); |
223 | | |
224 | 86.9M | for (i = 0; i < granule_len; i++) { |
225 | 86.8M | L = l_fac * ptr_time_data[num_channels * i + pstr_elem_info->channel_index[0]] + |
226 | 86.8M | r_fac * ptr_time_data[num_channels * i + pstr_elem_info->channel_index[1]]; |
227 | 86.8M | R = r_fac * ptr_time_data[num_channels * i + pstr_elem_info->channel_index[0]] + |
228 | 86.8M | l_fac * ptr_time_data[num_channels * i + pstr_elem_info->channel_index[1]]; |
229 | | |
230 | 86.8M | ptr_time_data[num_channels * i + pstr_elem_info->channel_index[0]] = L; |
231 | 86.8M | ptr_time_data[num_channels * i + pstr_elem_info->channel_index[1]] = R; |
232 | 86.8M | } |
233 | | |
234 | 108k | pstr_stereo_pre_pro->last_l_to_r = l_to_r; |
235 | | |
236 | 108k | pstr_stereo_pre_pro->last_nrg_lr = |
237 | 108k | pstr_stereo_pre_pro->average_freq_energy_l + pstr_stereo_pre_pro->average_freq_energy_r; |
238 | 108k | } |
239 | | |
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 | 167k | FLOAT32 weight_pe_fac) { |
244 | 167k | if (pstr_stereo_pre_pro->stereo_attenuation_flag) { |
245 | 108k | FLOAT32 delta = 0.1f; |
246 | | |
247 | 108k | pstr_stereo_pre_pro->average_freq_energy_l = pstr_psy_out[0]->sfb_sum_lr_energy; |
248 | 108k | pstr_stereo_pre_pro->average_freq_energy_r = pstr_psy_out[1]->sfb_sum_lr_energy; |
249 | 108k | pstr_stereo_pre_pro->average_freq_energy_m = pstr_psy_out[0]->sfb_sum_ms_energy; |
250 | 108k | pstr_stereo_pre_pro->average_freq_energy_s = pstr_psy_out[1]->sfb_sum_ms_energy; |
251 | | |
252 | 108k | pstr_stereo_pre_pro->smoothed_pe_sum_sum = |
253 | 108k | delta * pstr_qc_out->pe * weight_pe_fac + |
254 | 108k | (1 - delta) * pstr_stereo_pre_pro->smoothed_pe_sum_sum; |
255 | 108k | } |
256 | 167k | } |