Line data Source code
1 : /******************************************************************************************************
2 :
3 : (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
4 : Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
5 : Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
6 : Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
7 : contributors to this repository. All Rights Reserved.
8 :
9 : This software is protected by copyright law and by international treaties.
10 : The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
11 : Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
12 : Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
13 : Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
14 : contributors to this repository retain full ownership rights in their respective contributions in
15 : the software. This notice grants no license of any kind, including but not limited to patent
16 : license, nor is any license granted by implication, estoppel or otherwise.
17 :
18 : Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
19 : contributions.
20 :
21 : This software is provided "AS IS", without any express or implied warranties. The software is in the
22 : development stage. It is intended exclusively for experts who have experience with such software and
23 : solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
24 : and fitness for a particular purpose are hereby disclaimed and excluded.
25 :
26 : Any dispute, controversy or claim arising under or in relation to providing this software shall be
27 : submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
28 : accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
29 : the United Nations Convention on Contracts on the International Sales of Goods.
30 :
31 : *******************************************************************************************************/
32 :
33 : /*====================================================================================
34 : EVS Codec 3GPP TS26.443 Nov 04, 2021. Version 12.14.0 / 13.10.0 / 14.6.0 / 15.4.0 / 16.3.0
35 : ====================================================================================*/
36 :
37 : #include <stdint.h>
38 : #include "options.h"
39 : #ifdef DEBUGGING
40 : #include "debug.h"
41 : #endif
42 : #include <math.h>
43 : #include "cnst.h"
44 : #include "prot.h"
45 : #include "wmc_auto.h"
46 :
47 : /*---------------------------------------------------------------------*
48 : * Local constants
49 : *---------------------------------------------------------------------*/
50 :
51 : #define FACT 3.0f /* background noise energy estimation adjusting factor - to maintain the ADR about the same */
52 : #define TH_COR 0.6f /* Minimum correlation for per bin processing */
53 : #define TH_D 50.0f /* Difference limit between nearest harmonic and a frequency bin */
54 : #define TH_PIT ( INT_FS_12k8 / ( 2.0f * TH_D ) ) /* Maximum pitch for per bin processing */
55 :
56 : /*-------------------------------------------------------------------*
57 : * find_tilt()
58 : *
59 : * Find LF/HF energy ratio
60 : *-------------------------------------------------------------------*/
61 :
62 15631381 : void find_tilt(
63 : const float fr_bands[], /* i : energy in frequency bands */
64 : const float bckr[], /* i : per band background noise energy estimate */
65 : float ee[2], /* o : lf/hf E ration for present frame */
66 : const int16_t pitch[3], /* i : open loop pitch values for 3 half-frames */
67 : const float voicing[3], /* i : normalized correlation for 3 half-frames */
68 : const float *lf_E, /* i : per bin energy for low frequencies */
69 : const float corr_shift, /* i : normalized correlation correction */
70 : const int16_t bwidth, /* i : input signal bandwidth */
71 : const int16_t max_band, /* i : maximum critical band */
72 : float hp_E[], /* o : energy in HF */
73 : const int16_t codec_mode, /* i : Mode 1 or 2 */
74 : float *bckr_tilt_lt, /* i/o: lf/hf E ratio of background noise */
75 : int16_t Opt_vbr_mode )
76 : {
77 : float lp_bckr, hp_bckr, lp_E, freq, f0, f1, f2, mean_voi, bin;
78 : const float *pt_bands, *pt_bckr, *tmp_E, *hf_bands, *pt_E;
79 : int16_t cnt, i, nb_bands;
80 : float th_pit;
81 :
82 : /*-----------------------------------------------------------------*
83 : * Initializations
84 : *-----------------------------------------------------------------*/
85 :
86 15631381 : th_pit = TH_PIT;
87 :
88 15631381 : if ( bwidth != NB )
89 : {
90 : /* WB processing */
91 15452832 : bin = BIN; /* First useful frequency bin ~ 50 Hz */
92 15452832 : pt_bands = fr_bands;
93 15452832 : tmp_E = lf_E;
94 15452832 : pt_bckr = bckr;
95 15452832 : nb_bands = 10;
96 : }
97 : else
98 : {
99 : /* NB processing */
100 178549 : bin = 3.0f * BIN; /* first useful frequency bin ~ 150 Hz */
101 178549 : pt_bands = fr_bands + 1; /* exlcude 1st critical band */
102 178549 : tmp_E = lf_E + 2; /* start at the 3rd bin (150 Hz) */
103 178549 : pt_bckr = bckr + 1; /* exclude 1st critical band */
104 178549 : nb_bands = 9; /* nb. of "low" frequency bands taken into account in NB processing */
105 : }
106 :
107 : /*-----------------------------------------------------------------*
108 : * Find spectrum tilt
109 : *-----------------------------------------------------------------*/
110 :
111 15631381 : pt_E = tmp_E; /* pointer at the 1st useful element of the per-bin energy vector */
112 15631381 : hf_bands = fr_bands;
113 :
114 : /* bckr + voicing */
115 15631381 : lp_bckr = mean( pt_bckr, nb_bands ); /* estimated noise E in first critical bands, up to 1270 Hz */
116 15631381 : hp_bckr = 0.5f * ( bckr[max_band - 1] + bckr[max_band] ); /* estimated noise E in last 2 critical bands */
117 15631381 : *bckr_tilt_lt = 0.9f * *bckr_tilt_lt + 0.1f * lp_bckr / hp_bckr;
118 :
119 15631381 : if ( codec_mode == MODE2 || Opt_vbr_mode )
120 : {
121 43645 : lp_bckr *= FACT;
122 43645 : hp_bckr *= FACT;
123 : }
124 :
125 15631381 : mean_voi = 0.5f * ( voicing[1] + voicing[2] ) + corr_shift;
126 15631381 : f0 = ( (float) INT_FS_12k8 ) / pitch[2];
127 :
128 46894143 : for ( i = 0; i < 2; i++ )
129 : {
130 31262762 : hp_E[i] = 0.5f * ( hf_bands[max_band - 1] + hf_bands[max_band] ) - hp_bckr; /* average E in last 2 critical bands */
131 :
132 31262762 : if ( !Opt_vbr_mode )
133 : {
134 31257982 : if ( hp_E[i] < E_MIN )
135 : {
136 : /* to avoid division by 0 */
137 6620968 : hp_E[i] = E_MIN;
138 : }
139 : }
140 : else
141 : {
142 4780 : if ( hp_E[i] < 1.0f )
143 : {
144 : /* to avoid division by 0 */
145 865 : hp_E[i] = 1.0f;
146 : }
147 : }
148 :
149 :
150 31262762 : if ( ( mean_voi > TH_COR ) && ( pitch[2] < th_pit ) )
151 : {
152 : /* high-pitched voiced frames */
153 13075162 : freq = bin; /* 1st useful frequency bin */
154 13075162 : cnt = 0;
155 13075162 : lp_E = 0.0f;
156 13075162 : f1 = 1.5f * f0; /* middle between 2 harmonics */
157 13075162 : f2 = f0;
158 :
159 90259054 : while ( freq <= 1270.0f ) /* end frequency of 10th critical band */
160 : {
161 : /*pt_E*/
162 432389676 : while ( freq <= f1 )
163 : {
164 355205784 : if ( fabs( freq - f2 ) < TH_D ) /* include only bins sufficiently close to harmonics */
165 : {
166 150625222 : lp_E += *pt_E;
167 150625222 : cnt++;
168 : }
169 355205784 : freq += BIN;
170 355205784 : pt_E++;
171 : }
172 77183892 : f1 += f0;
173 77183892 : f2 += f0; /* next harmonic */
174 : }
175 :
176 13075162 : lp_E = lp_E / (float) cnt - lp_bckr;
177 13075162 : pt_E = tmp_E + VOIC_BINS; /* update for next half-frame */
178 : }
179 : else
180 : {
181 : /* other than high-pitched voiced frames */
182 18187600 : lp_E = mean( pt_bands, nb_bands ) - lp_bckr; /* average E in first critical bands, up to 1270 Hz */
183 : }
184 :
185 31262762 : if ( !Opt_vbr_mode )
186 : {
187 31257982 : if ( lp_E < E_MIN )
188 : {
189 : /* avoid negative E due to noise subtraction */
190 5577180 : lp_E = E_MIN;
191 : }
192 : }
193 : else
194 : {
195 :
196 4780 : if ( lp_E < 0.0f )
197 : {
198 : /* avoid negative E due to noise subtraction */
199 137 : lp_E = 0.0f;
200 : }
201 : }
202 :
203 : /* calculate the tilt (LF/HF ratio) */
204 31262762 : ee[i] = lp_E / hp_E[i];
205 :
206 31262762 : if ( bwidth == NB ) /* for NB input, compensate for the missing bands */
207 : {
208 357098 : ee[i] *= 6.0f;
209 : }
210 :
211 31262762 : pt_bands += NB_BANDS; /* update pointers for the next half-frame */
212 31262762 : hf_bands += NB_BANDS;
213 : }
214 :
215 15631381 : return;
216 : }
|