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 295956 : 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 295956 : if ( frac < 0 )
92 : {
93 118360 : frac += 6;
94 118360 : x--;
95 : }
96 :
97 295956 : x1 = &x[0];
98 295956 : x2 = &x[1];
99 295956 : c1 = &E_ROM_inter6_1[frac];
100 295956 : c2 = &E_ROM_inter6_1[6 - frac];
101 295956 : s = x1[0] * c1[0] + x2[0] * c2[0];
102 295956 : s += x1[-1] * c1[6] + x2[1] * c2[6];
103 295956 : s += x1[-2] * c1[12] + x2[2] * c2[12];
104 295956 : s += x1[-3] * c1[18] + x2[3] * c2[18];
105 :
106 295956 : 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 85393 : 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 : #ifdef FIX_2278_OOB_INDEXING_IN_CLOSED_LOOP_PIT_SEARCH
151 : int16_t corr_idx;
152 : #else
153 : float *corr;
154 : #endif
155 : int16_t i, fraction, frac1, frac2, step;
156 : int16_t t0, t_min, t_max;
157 :
158 : /* Find interval to compute normalized correlation */
159 85393 : if ( t0_min_frac > 0 )
160 : {
161 55674 : t0_min++;
162 : }
163 85393 : t_min = t0_min - L_INTERPOL1;
164 85393 : t_max = t0_max + L_INTERPOL1;
165 :
166 : #ifdef FIX_2278_OOB_INDEXING_IN_CLOSED_LOOP_PIT_SEARCH
167 85393 : corr_idx = -t_min;
168 : #else
169 : /* allocate memory to normalized correlation vector */
170 : corr = &corr_v[-t_min]; /* corr[t_min..t_max] */
171 : #endif
172 :
173 : /* Compute normalized correlation between target and filtered excitation */
174 : #ifdef FIX_2278_OOB_INDEXING_IN_CLOSED_LOOP_PIT_SEARCH
175 85393 : norm_corr( exc, xn, h, t_min, t_max, &corr_v[0] + corr_idx, L_subfr );
176 : #else
177 : norm_corr( exc, xn, h, t_min, t_max, corr, L_subfr );
178 : #endif
179 :
180 : /* find integer pitch */
181 : #ifdef FIX_2278_OOB_INDEXING_IN_CLOSED_LOOP_PIT_SEARCH
182 85393 : max_val = corr_v[t0_min + corr_idx];
183 : #else
184 : max_val = corr[t0_min];
185 : #endif
186 85393 : t0 = t0_min;
187 1519232 : for ( i = t0_min + 1; i <= t0_max; i++ )
188 : {
189 : #ifdef FIX_2278_OOB_INDEXING_IN_CLOSED_LOOP_PIT_SEARCH
190 : float corr_tmp;
191 :
192 1433839 : corr_tmp = corr_v[corr_idx + i];
193 1433839 : if ( corr_tmp >= max_val )
194 : {
195 535789 : max_val = corr_tmp;
196 535789 : t0 = i;
197 : }
198 : #else
199 : if ( corr[i] >= max_val )
200 : {
201 : max_val = corr[i];
202 : t0 = i;
203 : }
204 : #endif
205 : }
206 :
207 : /* If first subframe and t0 >= pit_fr1, do not search fractionnal pitch */
208 85393 : if ( ( i_subfr == 0 ) & ( t0 >= pit_fr1 ) )
209 : {
210 5489 : *pit_frac = 0;
211 5489 : *pit_res = 1;
212 5489 : return ( t0 );
213 : }
214 :
215 : /*
216 : * Search fractionnal pitch
217 : * Test the fractions around t0 and choose the one which maximizes
218 : * the interpolated normalized correlation.
219 : */
220 :
221 79904 : if ( t0_min_max_res == ( pit_res_max >> 1 ) )
222 : {
223 73612 : t0_min_frac = t0_min_frac << 1;
224 73612 : t0_max_frac = t0_max_frac << 1;
225 : }
226 :
227 79904 : step = 1;
228 79904 : frac1 = -( pit_res_max - 1 );
229 79904 : frac2 = pit_res_max - 1;
230 79904 : if ( ( ( i_subfr == 0 ) & ( t0 >= pit_fr2 ) ) | ( pit_fr2 <= pit_min ) )
231 : {
232 73629 : step = 2;
233 73629 : frac1 = -( pit_res_max - 2 );
234 73629 : frac2 = pit_res_max - 2;
235 : }
236 :
237 79904 : if ( ( t0 == t0_min ) && ( t0_min_frac == 0 ) )
238 : {
239 1434 : frac1 = t0_min_frac;
240 : }
241 78470 : else if ( ( t0 == t0_min ) && ( frac1 + pit_res_max < t0_min_frac ) )
242 : {
243 506 : frac1 = t0_min_frac - pit_res_max;
244 : }
245 79904 : if ( t0 == t0_max )
246 : {
247 2909 : frac2 = t0_max_frac;
248 : }
249 79904 : assert( frac1 <= 0 && frac2 >= 0 && frac2 > frac1 );
250 : #ifdef FIX_2278_OOB_INDEXING_IN_CLOSED_LOOP_PIT_SEARCH
251 79904 : corr_idx += t0;
252 : #endif
253 79904 : if ( pit_res_max == 6 )
254 : {
255 : #ifdef FIX_2278_OOB_INDEXING_IN_CLOSED_LOOP_PIT_SEARCH
256 59844 : cor_max = E_GAIN_norm_corr_interpolate6( &corr_v[corr_idx], frac1 );
257 : #else
258 : cor_max = E_GAIN_norm_corr_interpolate6( &corr[t0], frac1 );
259 : #endif
260 59844 : fraction = frac1;
261 295956 : for ( i = ( frac1 + step ); i <= frac2; i += step )
262 : {
263 : #ifdef FIX_2278_OOB_INDEXING_IN_CLOSED_LOOP_PIT_SEARCH
264 236112 : temp = E_GAIN_norm_corr_interpolate6( &corr_v[corr_idx], i );
265 : #else
266 : temp = E_GAIN_norm_corr_interpolate6( &corr[t0], i );
267 : #endif
268 236112 : if ( temp > cor_max )
269 : {
270 117973 : cor_max = temp;
271 117973 : fraction = i;
272 : }
273 : }
274 : }
275 : else
276 : {
277 : #ifdef FIX_2278_OOB_INDEXING_IN_CLOSED_LOOP_PIT_SEARCH
278 20060 : cor_max = E_GAIN_norm_corr_interpolate( &corr_v[corr_idx], frac1 );
279 : #else
280 : cor_max = E_GAIN_norm_corr_interpolate( &corr[t0], frac1 );
281 : #endif
282 20060 : fraction = frac1;
283 84087 : for ( i = ( frac1 + step ); i <= frac2; i += step )
284 : {
285 : #ifdef FIX_2278_OOB_INDEXING_IN_CLOSED_LOOP_PIT_SEARCH
286 64027 : temp = E_GAIN_norm_corr_interpolate( &corr_v[corr_idx], i );
287 : #else
288 : temp = E_GAIN_norm_corr_interpolate( &corr[t0], i );
289 : #endif
290 64027 : if ( temp > cor_max )
291 : {
292 31605 : cor_max = temp;
293 31605 : fraction = i;
294 : }
295 : }
296 : }
297 :
298 : /* limit the fraction value */
299 79904 : if ( fraction < 0 )
300 : {
301 21473 : fraction += pit_res_max;
302 21473 : t0 -= 1;
303 : }
304 79904 : if ( ( ( i_subfr == 0 ) & ( t0 >= pit_fr2 ) ) | ( pit_fr2 <= pit_min ) )
305 : {
306 73629 : *pit_res = pit_res_max >> 1;
307 73629 : *pit_frac = fraction >> 1;
308 : }
309 : else
310 : {
311 6275 : *pit_res = pit_res_max;
312 6275 : *pit_frac = fraction;
313 : }
314 :
315 79904 : return ( t0 );
316 : }
|