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 <assert.h>
38 : #include <stdint.h>
39 : #include "options.h"
40 : #include "prot.h"
41 : #include "rom_enc.h"
42 : #include "rom_com.h"
43 : #include "wmc_auto.h"
44 :
45 :
46 : /*
47 : * E_GAIN_norm_corr_interpolate
48 : *
49 : * Parameters:
50 : * x I: input vector
51 : * frac I: fraction (-4..+3)
52 : *
53 : * Function:
54 : * Interpolating the normalized correlation
55 : *
56 : * Returns:
57 : * interpolated value
58 : */
59 84087 : static float E_GAIN_norm_corr_interpolate(
60 : float *x,
61 : int16_t frac )
62 : {
63 : float s, *x1, *x2;
64 : const float *c1, *c2;
65 :
66 84087 : if ( frac < 0 )
67 : {
68 31417 : frac += 4;
69 31417 : x--;
70 : }
71 :
72 84087 : x1 = &x[0];
73 84087 : x2 = &x[1];
74 84087 : c1 = &E_ROM_inter4_1[frac];
75 84087 : c2 = &E_ROM_inter4_1[4 - frac];
76 84087 : s = x1[0] * c1[0] + x2[0] * c2[0];
77 84087 : s += x1[-1] * c1[4] + x2[1] * c2[4];
78 84087 : s += x1[-2] * c1[8] + x2[2] * c2[8];
79 84087 : s += x1[-3] * c1[12] + x2[3] * c2[12];
80 :
81 84087 : return s;
82 : }
83 :
84 252633 : static float E_GAIN_norm_corr_interpolate6(
85 : float *x,
86 : int16_t frac )
87 : {
88 : float s, *x1, *x2;
89 : const float *c1, *c2;
90 :
91 252633 : if ( frac < 0 )
92 : {
93 101020 : frac += 6;
94 101020 : x--;
95 : }
96 :
97 252633 : x1 = &x[0];
98 252633 : x2 = &x[1];
99 252633 : c1 = &E_ROM_inter6_1[frac];
100 252633 : c2 = &E_ROM_inter6_1[6 - frac];
101 252633 : s = x1[0] * c1[0] + x2[0] * c2[0];
102 252633 : s += x1[-1] * c1[6] + x2[1] * c2[6];
103 252633 : s += x1[-2] * c1[12] + x2[2] * c2[12];
104 252633 : s += x1[-3] * c1[18] + x2[3] * c2[18];
105 :
106 252633 : return s;
107 : }
108 :
109 : /*
110 : * E_GAIN_closed_loop_search
111 : *
112 : * Parameters:
113 : * exc I: excitation buffer
114 : * xn I: target signal
115 : * h I: weighted synthesis filter impulse response
116 : * dn I: residual domain target signal
117 : * t0_min I: minimum value in the searched range
118 : * t0_max I: maximum value in the searched range
119 : * pit_frac O: chosen fraction
120 : * i_subfr I: flag to first subframe
121 : * t0_fr2 I: minimum value for resolution 1/2
122 : * t0_fr1 I: minimum value for resolution 1
123 : *
124 : * Function:
125 : * Find the closed loop pitch period with 1/4 subsample resolution.
126 : *
127 : * Returns:
128 : * chosen integer pitch lag
129 : */
130 76333 : int16_t E_GAIN_closed_loop_search(
131 : float exc[],
132 : float xn[],
133 : float h[],
134 : int16_t t0_min,
135 : int16_t t0_min_frac,
136 : int16_t t0_max,
137 : int16_t t0_max_frac,
138 : const int16_t t0_min_max_res,
139 : int16_t *pit_frac,
140 : int16_t *pit_res,
141 : const int16_t pit_res_max,
142 : const int16_t i_subfr,
143 : const int16_t pit_min,
144 : const int16_t pit_fr2,
145 : const int16_t pit_fr1,
146 : const int16_t L_subfr )
147 : {
148 : float corr_v[32 + 2 * L_INTERPOL1 + 1];
149 : float cor_max, max_val, temp;
150 : float *corr;
151 : int16_t i, fraction, frac1, frac2, step;
152 : int16_t t0, t_min, t_max;
153 :
154 : /* Find interval to compute normalized correlation */
155 76333 : if ( t0_min_frac > 0 )
156 : {
157 47523 : t0_min++;
158 : }
159 76333 : t_min = t0_min - L_INTERPOL1;
160 76333 : t_max = t0_max + L_INTERPOL1;
161 :
162 : /* allocate memory to normalized correlation vector */
163 76333 : corr = &corr_v[-t_min]; /* corr[t_min..t_max] */
164 :
165 : /* Compute normalized correlation between target and filtered excitation */
166 76333 : norm_corr( exc, xn, h, t_min, t_max, corr, L_subfr );
167 :
168 : /* find integer pitch */
169 76333 : max_val = corr[t0_min];
170 76333 : t0 = t0_min;
171 1345961 : for ( i = t0_min + 1; i <= t0_max; i++ )
172 : {
173 1269628 : if ( corr[i] >= max_val )
174 : {
175 475597 : max_val = corr[i];
176 475597 : t0 = i;
177 : }
178 : }
179 :
180 : /* If first subframe and t0 >= pit_fr1, do not search fractionnal pitch */
181 76333 : if ( ( i_subfr == 0 ) & ( t0 >= pit_fr1 ) )
182 : {
183 5165 : *pit_frac = 0;
184 5165 : *pit_res = 1;
185 5165 : return ( t0 );
186 : }
187 :
188 : /*
189 : * Search fractionnal pitch
190 : * Test the fractions around t0 and choose the one which maximizes
191 : * the interpolated normalized correlation.
192 : */
193 :
194 71168 : if ( t0_min_max_res == ( pit_res_max >> 1 ) )
195 : {
196 64876 : t0_min_frac = t0_min_frac << 1;
197 64876 : t0_max_frac = t0_max_frac << 1;
198 : }
199 :
200 71168 : step = 1;
201 71168 : frac1 = -( pit_res_max - 1 );
202 71168 : frac2 = pit_res_max - 1;
203 71168 : if ( ( ( i_subfr == 0 ) & ( t0 >= pit_fr2 ) ) | ( pit_fr2 <= pit_min ) )
204 : {
205 64893 : step = 2;
206 64893 : frac1 = -( pit_res_max - 2 );
207 64893 : frac2 = pit_res_max - 2;
208 : }
209 :
210 71168 : if ( ( t0 == t0_min ) && ( t0_min_frac == 0 ) )
211 : {
212 1404 : frac1 = t0_min_frac;
213 : }
214 69764 : else if ( ( t0 == t0_min ) && ( frac1 + pit_res_max < t0_min_frac ) )
215 : {
216 434 : frac1 = t0_min_frac - pit_res_max;
217 : }
218 71168 : if ( t0 == t0_max )
219 : {
220 2714 : frac2 = t0_max_frac;
221 : }
222 71168 : assert( frac1 <= 0 && frac2 >= 0 && frac2 > frac1 );
223 71168 : if ( pit_res_max == 6 )
224 : {
225 51108 : cor_max = E_GAIN_norm_corr_interpolate6( &corr[t0], frac1 );
226 51108 : fraction = frac1;
227 252633 : for ( i = ( frac1 + step ); i <= frac2; i += step )
228 : {
229 201525 : temp = E_GAIN_norm_corr_interpolate6( &corr[t0], i );
230 201525 : if ( temp > cor_max )
231 : {
232 100420 : cor_max = temp;
233 100420 : fraction = i;
234 : }
235 : }
236 : }
237 : else
238 : {
239 20060 : cor_max = E_GAIN_norm_corr_interpolate( &corr[t0], frac1 );
240 20060 : fraction = frac1;
241 84087 : for ( i = ( frac1 + step ); i <= frac2; i += step )
242 : {
243 64027 : temp = E_GAIN_norm_corr_interpolate( &corr[t0], i );
244 64027 : if ( temp > cor_max )
245 : {
246 31605 : cor_max = temp;
247 31605 : fraction = i;
248 : }
249 : }
250 : }
251 :
252 : /* limit the fraction value */
253 71168 : if ( fraction < 0 )
254 : {
255 19226 : fraction += pit_res_max;
256 19226 : t0 -= 1;
257 : }
258 71168 : if ( ( ( i_subfr == 0 ) & ( t0 >= pit_fr2 ) ) | ( pit_fr2 <= pit_min ) )
259 : {
260 64893 : *pit_res = pit_res_max >> 1;
261 64893 : *pit_frac = fraction >> 1;
262 : }
263 : else
264 : {
265 6275 : *pit_res = pit_res_max;
266 6275 : *pit_frac = fraction;
267 : }
268 :
269 71168 : return ( t0 );
270 : }
|