Line data Source code
1 : /******************************************************************************************************
2 :
3 : (C) 2022-2026 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 66381 : 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 66381 : if ( frac < 0 )
67 : {
68 25929 : frac += 4;
69 25929 : x--;
70 : }
71 :
72 66381 : x1 = &x[0];
73 66381 : x2 = &x[1];
74 66381 : c1 = &E_ROM_inter4_1[frac];
75 66381 : c2 = &E_ROM_inter4_1[4 - frac];
76 66381 : s = x1[0] * c1[0] + x2[0] * c2[0];
77 66381 : s += x1[-1] * c1[4] + x2[1] * c2[4];
78 66381 : s += x1[-2] * c1[8] + x2[2] * c2[8];
79 66381 : s += x1[-3] * c1[12] + x2[3] * c2[12];
80 :
81 66381 : return s;
82 : }
83 :
84 58737 : 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 58737 : if ( frac < 0 )
92 : {
93 23531 : frac += 6;
94 23531 : x--;
95 : }
96 :
97 58737 : x1 = &x[0];
98 58737 : x2 = &x[1];
99 58737 : c1 = &E_ROM_inter6_1[frac];
100 58737 : c2 = &E_ROM_inter6_1[6 - frac];
101 58737 : s = x1[0] * c1[0] + x2[0] * c2[0];
102 58737 : s += x1[-1] * c1[6] + x2[1] * c2[6];
103 58737 : s += x1[-2] * c1[12] + x2[2] * c2[12];
104 58737 : s += x1[-3] * c1[18] + x2[3] * c2[18];
105 :
106 58737 : 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 26317 : 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 : int16_t corr_idx;
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 26317 : if ( t0_min_frac > 0 )
156 : {
157 9719 : t0_min++;
158 : }
159 26317 : t_min = t0_min - L_INTERPOL1;
160 26317 : t_max = t0_max + L_INTERPOL1;
161 :
162 26317 : corr_idx = -t_min;
163 :
164 : /* Compute normalized correlation between target and filtered excitation */
165 26317 : norm_corr( exc, xn, h, t_min, t_max, &corr_v[0] + corr_idx, L_subfr );
166 :
167 : /* find integer pitch */
168 26317 : max_val = corr_v[t0_min + corr_idx];
169 26317 : t0 = t0_min;
170 423398 : for ( i = t0_min + 1; i <= t0_max; i++ )
171 : {
172 : float corr_tmp;
173 :
174 397081 : corr_tmp = corr_v[corr_idx + i];
175 397081 : if ( corr_tmp >= max_val )
176 : {
177 165204 : max_val = corr_tmp;
178 165204 : t0 = i;
179 : }
180 : }
181 :
182 : /* If first subframe and t0 >= pit_fr1, do not search fractionnal pitch */
183 26317 : if ( ( i_subfr == 0 ) & ( t0 >= pit_fr1 ) )
184 : {
185 526 : *pit_frac = 0;
186 526 : *pit_res = 1;
187 526 : return ( t0 );
188 : }
189 :
190 : /*
191 : * Search fractionnal pitch
192 : * Test the fractions around t0 and choose the one which maximizes
193 : * the interpolated normalized correlation.
194 : */
195 :
196 25791 : if ( t0_min_max_res == ( pit_res_max >> 1 ) )
197 : {
198 19511 : t0_min_frac = t0_min_frac << 1;
199 19511 : t0_max_frac = t0_max_frac << 1;
200 : }
201 :
202 25791 : step = 1;
203 25791 : frac1 = -( pit_res_max - 1 );
204 25791 : frac2 = pit_res_max - 1;
205 25791 : if ( ( ( i_subfr == 0 ) & ( t0 >= pit_fr2 ) ) | ( pit_fr2 <= pit_min ) )
206 : {
207 19527 : step = 2;
208 19527 : frac1 = -( pit_res_max - 2 );
209 19527 : frac2 = pit_res_max - 2;
210 : }
211 :
212 25791 : if ( ( t0 == t0_min ) && ( t0_min_frac == 0 ) )
213 : {
214 413 : frac1 = t0_min_frac;
215 : }
216 25378 : else if ( ( t0 == t0_min ) && ( frac1 + pit_res_max < t0_min_frac ) )
217 : {
218 87 : frac1 = t0_min_frac - pit_res_max;
219 : }
220 25791 : if ( t0 == t0_max )
221 : {
222 693 : frac2 = t0_max_frac;
223 : }
224 25791 : assert( frac1 <= 0 && frac2 >= 0 && frac2 > frac1 );
225 25791 : corr_idx += t0;
226 25791 : if ( pit_res_max == 6 )
227 : {
228 11829 : cor_max = E_GAIN_norm_corr_interpolate6( &corr_v[corr_idx], frac1 );
229 11829 : fraction = frac1;
230 58737 : for ( i = ( frac1 + step ); i <= frac2; i += step )
231 : {
232 46908 : temp = E_GAIN_norm_corr_interpolate6( &corr_v[corr_idx], i );
233 46908 : if ( temp > cor_max )
234 : {
235 23004 : cor_max = temp;
236 23004 : fraction = i;
237 : }
238 : }
239 : }
240 : else
241 : {
242 13962 : cor_max = E_GAIN_norm_corr_interpolate( &corr_v[corr_idx], frac1 );
243 13962 : fraction = frac1;
244 66381 : for ( i = ( frac1 + step ); i <= frac2; i += step )
245 : {
246 52419 : temp = E_GAIN_norm_corr_interpolate( &corr_v[corr_idx], i );
247 52419 : if ( temp > cor_max )
248 : {
249 25621 : cor_max = temp;
250 25621 : fraction = i;
251 : }
252 : }
253 : }
254 :
255 : /* limit the fraction value */
256 25791 : if ( fraction < 0 )
257 : {
258 7888 : fraction += pit_res_max;
259 7888 : t0 -= 1;
260 : }
261 25791 : if ( ( ( i_subfr == 0 ) & ( t0 >= pit_fr2 ) ) | ( pit_fr2 <= pit_min ) )
262 : {
263 19527 : *pit_res = pit_res_max >> 1;
264 19527 : *pit_frac = fraction >> 1;
265 : }
266 : else
267 : {
268 6264 : *pit_res = pit_res_max;
269 6264 : *pit_frac = fraction;
270 : }
271 :
272 25791 : return ( t0 );
273 : }
|