Line data Source code
1 : /******************************************************************************
2 : * ETSI TS 103 634 V1.6.1 *
3 : * Low Complexity Communication Codec Plus (LC3plus) *
4 : * *
5 : * Copyright licence is solely granted through ETSI Intellectual Property *
6 : * Rights Policy, 3rd April 2019. No patent licence is granted by implication, *
7 : * estoppel or otherwise. *
8 : ******************************************************************************/
9 :
10 : #include "options.h"
11 : #include "wmc_auto.h"
12 : #include "functions.h"
13 :
14 : #ifdef CR9_C_ADD_1p25MS
15 1186529 : static LC3_INT16 get_continuation (LC3_INT32 fading_case, LC3PLUS_FrameDuration frame_dms, LC3_INT32 pos, LC3_INT32 total)
16 : {
17 : LC3_INT16 retval;
18 :
19 1186529 : if ( frame_dms != LC3PLUS_FRAME_DURATION_1p25MS )
20 : {
21 1186529 : retval = 0;
22 : }
23 : else
24 : {
25 0 : if ( pos == total )
26 : {
27 0 : retval = 0;
28 : }
29 : else
30 : {
31 0 : retval = fading_case;
32 : }
33 : }
34 :
35 1186529 : return retval;
36 : }
37 : #endif
38 :
39 : #ifdef LTPF_ADAPTIVE_GAIN_WITH_NORM_CORR
40 : #ifdef LTPF_PRINT_PARAMS
41 : static bool compare_normalized_corrs(LC3_FLOAT const *sig, LC3_INT32 len, LC3_INT32 pitch_int, LC3_INT32 mem_pitch_int, LC3_FLOAT *corr, LC3_FLOAT *corr_prev)
42 : #else
43 0 : static bool compare_normalized_corrs(LC3_FLOAT const *sig, LC3_INT32 len, LC3_INT32 pitch_int, LC3_INT32 mem_pitch_int)
44 : #endif
45 : {
46 : LC3_FLOAT norm_0, norm_t, norm_t_prev, xcorr, xcorr_prev;
47 0 : norm_0 = norm_t = norm_t_prev = xcorr = xcorr_prev = 0;
48 :
49 0 : for ( int i=0; i < len; i++ )
50 : {
51 0 : norm_0 += sig[i] * sig[i];
52 0 : norm_t += sig[i - pitch_int] * sig[i - pitch_int];
53 0 : xcorr += sig[i] * sig[i - pitch_int];
54 :
55 0 : norm_t_prev += sig[i - mem_pitch_int] * sig[i - mem_pitch_int];
56 0 : xcorr_prev += sig[i] * sig[i - mem_pitch_int];
57 : }
58 :
59 0 : xcorr = MIN( 1.f, MAX( 0.f, xcorr / ( LC3_SQRT( norm_0 * norm_t ) + 1.00e-05f ) ) );
60 0 : xcorr_prev = MIN( 1.f, MAX( 0.f, xcorr_prev / ( LC3_SQRT( norm_0 * norm_t_prev ) + 1.00e-05f ) ) );
61 :
62 : #ifdef LTPF_PRINT_PARAMS
63 : *corr = xcorr;
64 : *corr_prev = xcorr_prev;
65 : #endif
66 :
67 0 : if ( xcorr_prev - xcorr > 5e-4 )
68 : {
69 0 : return true;
70 : }
71 :
72 0 : return false;
73 : }
74 : #endif
75 :
76 1186529 : void process_ltpf_decoder_fl(LC3_FLOAT* x, LC3_INT xLen, LC3_FLOAT* y, LC3_INT fs, LC3_FLOAT* mem_old_x, LC3_FLOAT* mem_old_y,
77 : LC3_INT* mem_pitch_int, LC3_INT* mem_pitch_fr, LC3_FLOAT* mem_gain, LC3_INT* mem_beta_idx, LC3_INT bfi,
78 : LC3_INT* param, LC3_INT* mem_param, LC3_INT conf_beta_idx, LC3_FLOAT *conf_beta, LC3_INT concealMethod, LC3_FLOAT damping
79 : , LC3_INT *mem_ltpf_active
80 : , LC3_FLOAT *rel_pitch_change, LC3_INT hrmode, LC3PLUS_FrameDuration frame_dms
81 : #ifdef CR9_C_ADD_1p25MS
82 : , LC3_INT16* mem_continuation, LC3_INT32* mem_param_prev, LC3_INT16* mem_pitch_int_prev,
83 : LC3_INT16* mem_pitch_fr_prev, LC3_INT32* mem_beta_idx_prev, LC3_FLOAT* mem_gain_prev,
84 : LC3_INT16* pitch_stability_counter,
85 : LC3_FLOAT* gain_step,
86 : LC3_FLOAT conf_beta_max
87 : #endif
88 : )
89 : {
90 : LC3_INT i, j, n, N, L_past_x, N4, N34,
91 1186529 : pitch_int, pitch_fr, p1, p2, L_past_y, inter_len, tilt_len = 0,
92 : tilt_len_r, inter_len_r, old_x_len, old_y_len, fading_case, N4_D, N4_S;
93 :
94 : LC3_FLOAT conf_alpha, gain, a1[12], a2[12], b1[11], b2[11],
95 : buf_x[4 * MAX_LEN], buf_y[4 * MAX_LEN], buf_z[4 * MAX_LEN], pitch, sum1, sum2;
96 : LC3_FLOAT *p_x, *p_y, *p_y2, *p_x_init, *p_y_init, *p_a1, *p_b1, *p_a2, *p_b2, fade_fac, current_fade_fac_up, current_fade_fac_down;
97 : LC3_FLOAT pitch_delta;
98 : const LC3_FLOAT *inter_filter[4], *tilt_filter[4];
99 : #ifdef LTPF_ADAPTIVE_GAIN
100 1186529 : bool ltpf_active = false;
101 1186529 : bool ltpf_active_prev = false;
102 1186529 : bool pitch_changed = false;
103 1186529 : bool pitch_was_stable = false;
104 : #endif
105 :
106 : #ifdef LTPF_ADAPTIVE_GAIN
107 : LC3_INT32 original_param[3];
108 : LC3_INT32 original_pitch_int, original_pitch_fr;
109 : LC3_FLOAT original_gain;
110 :
111 : #ifdef LTPF_PRINT_PARAMS
112 : static int local_frame = 0;
113 : #ifdef LTPF_ADAPTIVE_GAIN_WITH_NORM_CORR
114 : LC3_FLOAT xcorr, xcorr_prev;
115 : #endif
116 : #endif
117 : #endif
118 :
119 : #ifdef FIX_LTPF_DEC_FLFX_MISMATCH
120 : UNUSED(mem_ltpf_active);
121 1186529 : fading_case = 0;
122 : #endif
123 1186529 : tilt_len = 0;
124 1186529 : conf_alpha = 0.85;
125 1186529 : p1 = 0;
126 1186529 : p2 = 0;
127 :
128 : #ifdef WMOPS
129 : push_wmops("process_ltpf_decoder_fl");
130 : #endif
131 :
132 : #ifdef FIX_LTPF_1p25
133 1186529 : if (param[2] == -1) {
134 0 : param[1] = 0;
135 0 : param[0] = 0;
136 : }
137 : #endif
138 1186529 : if (bfi != 1) {
139 : /* Decode pitch */
140 1178242 : if (param[0] == 1) {
141 880910 : if (param[2] < (RES4_PITCH_12K8 - MIN_PITCH_12K8) * 4) {
142 689088 : pitch_int = MIN_PITCH_12K8 + floor(param[2] / 4);
143 689088 : pitch_fr = param[2] - ((pitch_int - MIN_PITCH_12K8) * 4);
144 191822 : } else if (param[2] < ((RES4_PITCH_12K8 - MIN_PITCH_12K8) * 4) + ((RES2_PITCH_12K8 - RES4_PITCH_12K8) * 2)) {
145 35227 : param[2] = param[2] - ((RES4_PITCH_12K8 - MIN_PITCH_12K8) * 4);
146 35227 : pitch_int = RES4_PITCH_12K8 + floor(param[2] / 2);
147 35227 : pitch_fr = param[2] - ((pitch_int - RES4_PITCH_12K8) * 2);
148 35227 : pitch_fr = pitch_fr * 2;
149 : } else {
150 156595 : pitch_int =
151 156595 : param[2] + (RES2_PITCH_12K8 - ((RES4_PITCH_12K8 - MIN_PITCH_12K8) * 4) - ((RES2_PITCH_12K8 - RES4_PITCH_12K8) * 2));
152 156595 : pitch_fr = 0;
153 : }
154 :
155 880910 : pitch = ((LC3_FLOAT)pitch_int + (LC3_FLOAT)pitch_fr / 4.0) * (LC3_FLOAT)fs / 12800.0;
156 880910 : pitch = round(pitch * 4.0) / 4.0;
157 880910 : pitch_int = floor(pitch);
158 880910 : pitch_fr = (LC3_INT)((pitch - (LC3_FLOAT)pitch_int) * 4.0);
159 : } else {
160 297332 : pitch_int = 0;
161 297332 : pitch_fr = 0;
162 : }
163 :
164 : /* Decode gain */
165 1178242 : if (conf_beta_idx < 0) {
166 1178224 : param[1] = 0;
167 : }
168 :
169 1178242 : if (param[1] == 1) {
170 0 : gain = *conf_beta;
171 : } else {
172 1178242 : gain = 0;
173 : }
174 : }
175 8287 : else if ((concealMethod > 0)
176 : #ifdef CR9_C_ADD_1p25MS
177 8287 : && (*mem_continuation == 0)
178 : #endif
179 : ) {
180 8287 : if (conf_beta_idx < 0) {
181 8222 : if (mem_param[1] && *mem_beta_idx >= 0)
182 : {
183 0 : conf_beta_idx = *mem_beta_idx;
184 : }
185 : }
186 :
187 8287 : memmove(param, mem_param, sizeof(LC3_INT32) * 3);
188 8287 : if (concealMethod == 2)
189 : {
190 : /* cause the ltpf to "fade_out" and only filter during initial 2.5 ms and then its buffer during 7.5 ms */
191 0 : assert(bfi == 1);
192 0 : param[1] = 0; /* ltpf_active = 0 */
193 : }
194 :
195 8287 : pitch_int = *mem_pitch_int;
196 8287 : pitch_fr = *mem_pitch_fr;
197 8287 : gain = (LC3_FLOAT) *mem_gain * damping;
198 : }
199 :
200 : #ifdef CR9_C_ADD_1p25MS
201 1186529 : if (*mem_continuation)
202 : {
203 : #ifdef LTPF_ADAPTIVE_GAIN
204 : /* Save original LTPF parameters */
205 0 : move_int(original_param, param, 3);
206 0 : original_pitch_int = pitch_int;
207 0 : original_pitch_fr = pitch_fr;
208 0 : original_gain = gain;
209 : #endif
210 :
211 0 : fading_case = *mem_continuation;
212 0 : move_int(param, mem_param, 3);
213 0 : pitch_int = *mem_pitch_int;
214 0 : pitch_fr = *mem_pitch_fr;
215 0 : gain = *mem_gain;
216 : #ifdef FIX_LTPF_1p25
217 0 : conf_beta_idx = *mem_beta_idx;
218 : #endif
219 :
220 0 : move_int(mem_param, mem_param_prev, 3);
221 0 : *mem_pitch_int = *mem_pitch_int_prev;
222 0 : *mem_pitch_fr = *mem_pitch_fr_prev;
223 0 : *mem_gain = *mem_gain_prev;
224 : #ifdef FIX_LTPF_1p25
225 0 : *mem_beta_idx = *mem_beta_idx_prev;
226 : #endif
227 :
228 : }
229 : #endif
230 :
231 1186529 : if ( fs <= 48000 )
232 : {
233 1186529 : if (fs == 8000 || fs == 16000) {
234 12 : tilt_len = 4 - 2;
235 : }
236 1186517 : else if (fs == 24000) {
237 0 : tilt_len = 6 - 2;
238 : }
239 1186517 : else if (fs == 32000) {
240 12 : tilt_len = 8 - 2;
241 : }
242 1186505 : else if (fs == 44100 || fs == 48000) {
243 1186505 : tilt_len = 12 - 2;
244 : }
245 :
246 1186529 : inter_len = MAX(fs, 16000) / 8000;
247 :
248 : /* Init buffers */
249 1186529 : N = xLen;
250 1186529 : old_x_len = tilt_len;
251 1186529 : old_y_len = ceil(228.0 * fs / 12800.0) + inter_len;
252 1186529 : L_past_x = old_x_len;
253 :
254 1186529 : move_float(buf_x, mem_old_x, old_x_len);
255 1186529 : move_float(&buf_x[old_x_len], x, xLen);
256 1186529 : L_past_y = old_y_len;
257 1186529 : move_float(buf_y, mem_old_y, old_y_len);
258 1186529 : move_float(&buf_y[old_y_len], x, xLen);
259 : }
260 : #ifdef LTPF_ADAPTIVE_GAIN
261 1186529 : if ( frame_dms == LC3PLUS_FRAME_DURATION_1p25MS )
262 : {
263 0 : conf_alpha = 0.98;
264 :
265 : /* Control variables */
266 0 : ltpf_active = param[1];
267 0 : ltpf_active_prev = mem_param[1];
268 0 : pitch_changed = !( ( pitch_int == *mem_pitch_int ) && ( pitch_fr == *mem_pitch_fr ) );
269 0 : pitch_was_stable = ( ( *pitch_stability_counter >= LTPF_PITCH_STABILITY_THRESHOLD ) );
270 :
271 : #ifdef CR9_C_ADD_1p25MS
272 0 : if ( *mem_continuation == 0 )
273 : {
274 : #endif
275 : #ifdef LTPF_ADAPTIVE_GAIN_WITH_NORM_CORR
276 0 : if ( !pitch_was_stable && pitch_changed && pitch_int != 0 && *mem_pitch_int != 0 )
277 : {
278 : #ifdef LTPF_PRINT_PARAMS
279 : pitch_was_stable = compare_normalized_corrs(buf_y + L_past_y, xLen, pitch_int, *mem_pitch_int, &xcorr, &xcorr_prev);
280 : #else
281 0 : pitch_was_stable = compare_normalized_corrs(buf_y + L_past_y, xLen, pitch_int, *mem_pitch_int);
282 : #endif
283 : }
284 : #endif
285 :
286 0 : if ( ltpf_active && !pitch_changed )
287 : {
288 : /* increment gain and increment pitch stability counter */
289 0 : gain = pitch_was_stable ? MIN( conf_beta_max, MAX( gain, *mem_gain ) + *gain_step ) : MAX( gain, *mem_gain );
290 0 : ( *pitch_stability_counter )++;
291 : }
292 0 : else if ( ltpf_active && pitch_changed && !pitch_was_stable )
293 : {
294 : /* decrement gain and reset pitch stability counter */
295 0 : gain = ( *mem_gain > gain ) ? MAX( gain, *mem_gain - ( conf_beta_max / LTPF_ADAPTIVE_GAIN_RATE ) ) : gain;
296 0 : *pitch_stability_counter = 0;
297 : }
298 0 : else if ( !ltpf_active && !pitch_was_stable && ltpf_active_prev && pitch_changed )
299 : {
300 : /* decrement gain, use previous pitch and reset pitch stability counter */
301 0 : gain = *mem_gain - *gain_step;
302 :
303 : #ifdef FIX_LTPF_1p25
304 0 : if (*conf_beta > 0 && (gain - *conf_beta) > -(20.f/(1<<15)))
305 : #else
306 : if ( (gain - *conf_beta) > -(20.f/(1<<15)))
307 : #endif
308 : {
309 0 : move_int( param, mem_param, 3 );
310 0 : pitch_int = *mem_pitch_int;
311 0 : pitch_fr = *mem_pitch_fr;
312 : }
313 : else
314 : {
315 0 : gain = 0.f;
316 : }
317 0 : *pitch_stability_counter = 0;
318 : }
319 0 : else if ( ( ltpf_active && pitch_changed && pitch_was_stable ) || ( !ltpf_active && pitch_was_stable ) || ( !ltpf_active && !pitch_was_stable && ltpf_active_prev && !pitch_changed ) )
320 : {
321 : /* use previous pitch and gain and reset pitch stability counter */
322 0 : move_int( param, mem_param, 3 );
323 0 : pitch_int = *mem_pitch_int;
324 0 : pitch_fr = *mem_pitch_fr;
325 0 : gain = *mem_gain;
326 0 : *pitch_stability_counter = 0;
327 : }
328 : #ifdef CR9_C_ADD_1p25MS /* This is added here for the future when adaptive gain will be enabled for other frame sizes. */
329 : }
330 0 : else if ( *mem_continuation != 0 && original_param[1] == 1
331 : #ifdef FIX_PLC_CONFORM_ISSUES
332 0 : && bfi == 0
333 : #endif
334 : )
335 : {
336 : /* Code enters this block if LTPF is reenabled when adaptive gain is being applied. */
337 : /* In this case, use new LTPF parameters but with a smaller gain than in the prev frame.*/
338 0 : fading_case = 0;
339 0 : *mem_continuation = 0;
340 :
341 0 : move_int( mem_param_prev, mem_param, 3 );
342 0 : *mem_pitch_int_prev = *mem_pitch_int;
343 0 : *mem_pitch_fr_prev = *mem_pitch_fr;
344 0 : *mem_gain_prev = *mem_gain;
345 :
346 0 : move_int( mem_param, param, 3 );
347 0 : *mem_pitch_int = pitch_int;
348 0 : *mem_pitch_fr = pitch_fr;
349 0 : *mem_gain = gain;
350 :
351 0 : move_int( param, original_param, 3 );
352 0 : pitch_int = original_pitch_int;
353 0 : pitch_fr = original_pitch_fr;
354 0 : gain = original_gain;
355 :
356 : /* if prev gain > curr gain, then decrease gain slowly. */
357 0 : gain = ( *mem_gain > gain) ? MAX( gain, *mem_gain - ( conf_beta_max / LTPF_ADAPTIVE_GAIN_RATE ) ) : gain;
358 :
359 0 : *pitch_stability_counter = 0;
360 : }
361 : #endif
362 : }
363 : #endif /* LTPF_ADAPTIVE_GAIN */
364 1186529 : if ( mem_param[1] && *mem_beta_idx < 0 )
365 : {
366 0 : mem_param[1] = 0;
367 : }
368 :
369 1186529 : if ( param[1] && conf_beta_idx < 0 )
370 : {
371 0 : param[1] = 0;
372 : }
373 :
374 1186529 : if ( fs <= 48000 )
375 : {
376 : #ifdef FIX_LTPF_MEM_CONTINUATION
377 : #ifdef FIX_LTPF_DEC_FLFX_MISMATCH
378 1186529 : if ( (( param[1] == 0 ) && ( mem_param[1] == 0 )) || fading_case == 1 )
379 : #else
380 : if ( (( *conf_beta <= 0 ) && ( *mem_ltpf_active == 0 )) || fading_case == 1 )
381 : #endif
382 : #else
383 : if ( ( *conf_beta <= 0 ) && ( *mem_ltpf_active == 0 ) )
384 : #endif
385 : {
386 : #ifndef LTPF_ADAPTIVE_GAIN_WITH_NORM_CORR
387 : if ( fs == 8000 || fs == 16000 )
388 : {
389 : tilt_len = 4 - 2;
390 : }
391 : else if ( fs == 24000 )
392 : {
393 : tilt_len = 6 - 2;
394 : }
395 : else if ( fs == 32000 )
396 : {
397 : tilt_len = 8 - 2;
398 : }
399 : else if ( fs == 44100 || fs == 48000 )
400 : {
401 : tilt_len = 12 - 2;
402 : }
403 : N = xLen;
404 : old_x_len = tilt_len;
405 : inter_len = MAX(fs, 16000) / 8000;
406 : old_y_len = ceilf((LC3_FLOAT)228.0 * fs / 12800.0) + inter_len; /* 228.0 needed to make use of ceil */
407 : #endif
408 :
409 : #ifdef FIX_LTPF_MEM_CONTINUATION
410 : #ifdef CR9_C_ADD_1p25MS
411 1186529 : fading_case = 1;
412 1186529 : N4 = fs * 0.0025;
413 1186529 : N4_S = 0;
414 1186529 : N4_D = N4;
415 1186529 : if ( frame_dms == LC3PLUS_FRAME_DURATION_1p25MS )
416 : {
417 0 : N4_D = 2 * N4;
418 0 : if ( *mem_continuation )
419 : {
420 0 : N4_S = N4;
421 : }
422 : }
423 1186529 : *mem_continuation = get_continuation( fading_case, frame_dms, ( N4 + N4_S ), N4_D );
424 : #endif
425 : #endif
426 :
427 1186529 : move_float(mem_old_y, &mem_old_y[N], (old_y_len - N));
428 1186529 : move_float(&mem_old_y[old_y_len - N], x, N);
429 1186529 : move_float(mem_old_x, &x[N - old_x_len], old_x_len);
430 :
431 : #ifdef FIX_LTPF_DEC_FLFX_MISMATCH
432 1186529 : mem_param[1] = 0;
433 : #else
434 : *mem_ltpf_active = 0;
435 : #endif
436 : }
437 : else
438 : {
439 0 : inter_len_r = 0; tilt_len_r = 0;
440 0 : if (fs == 8000 || fs == 16000) {
441 0 : inter_filter[0] = conf_inter_filter_16[0];
442 0 : inter_filter[1] = conf_inter_filter_16[1];
443 0 : inter_filter[2] = conf_inter_filter_16[2];
444 0 : inter_filter[3] = conf_inter_filter_16[3];
445 0 : inter_len_r = 4;
446 :
447 0 : tilt_filter[0] = conf_tilt_filter_16[0];
448 0 : tilt_filter[1] = conf_tilt_filter_16[1];
449 0 : tilt_filter[2] = conf_tilt_filter_16[2];
450 0 : tilt_filter[3] = conf_tilt_filter_16[3];
451 : #ifndef LTPF_ADAPTIVE_GAIN_WITH_NORM_CORR
452 : tilt_len = 4 - 2;
453 : #endif
454 0 : tilt_len_r = 3;
455 0 : } else if (fs == 24000) {
456 0 : inter_filter[0] = conf_inter_filter_24[0];
457 0 : inter_filter[1] = conf_inter_filter_24[1];
458 0 : inter_filter[2] = conf_inter_filter_24[2];
459 0 : inter_filter[3] = conf_inter_filter_24[3];
460 0 : inter_len_r = 6;
461 :
462 0 : tilt_filter[0] = conf_tilt_filter_24[0];
463 0 : tilt_filter[1] = conf_tilt_filter_24[1];
464 0 : tilt_filter[2] = conf_tilt_filter_24[2];
465 0 : tilt_filter[3] = conf_tilt_filter_24[3];
466 : #ifndef LTPF_ADAPTIVE_GAIN_WITH_NORM_CORR
467 : tilt_len = 6 - 2;
468 : #endif
469 0 : tilt_len_r = 5;
470 0 : } else if (fs == 32000) {
471 0 : inter_filter[0] = conf_inter_filter_32[0];
472 0 : inter_filter[1] = conf_inter_filter_32[1];
473 0 : inter_filter[2] = conf_inter_filter_32[2];
474 0 : inter_filter[3] = conf_inter_filter_32[3];
475 0 : inter_len_r = 8;
476 :
477 0 : tilt_filter[0] = conf_tilt_filter_32[0];
478 0 : tilt_filter[1] = conf_tilt_filter_32[1];
479 0 : tilt_filter[2] = conf_tilt_filter_32[2];
480 0 : tilt_filter[3] = conf_tilt_filter_32[3];
481 : #ifndef LTPF_ADAPTIVE_GAIN_WITH_NORM_CORR
482 : tilt_len = 8 - 2;
483 : #endif
484 0 : tilt_len_r = 7;
485 0 : } else if (fs == 44100 || fs == 48000) {
486 0 : inter_filter[0] = conf_inter_filter_48[0];
487 0 : inter_filter[1] = conf_inter_filter_48[1];
488 0 : inter_filter[2] = conf_inter_filter_48[2];
489 0 : inter_filter[3] = conf_inter_filter_48[3];
490 0 : inter_len_r = 12;
491 :
492 0 : tilt_filter[0] = conf_tilt_filter_48[0];
493 0 : tilt_filter[1] = conf_tilt_filter_48[1];
494 0 : tilt_filter[2] = conf_tilt_filter_48[2];
495 0 : tilt_filter[3] = conf_tilt_filter_48[3];
496 : #ifndef LTPF_ADAPTIVE_GAIN_WITH_NORM_CORR
497 : tilt_len = 12 - 2;
498 : #endif
499 0 : tilt_len_r = 11;
500 : }
501 :
502 : #ifndef LTPF_ADAPTIVE_GAIN_WITH_NORM_CORR
503 : inter_len = MAX( fs, 16000 ) / 8000;
504 :
505 : /* Init buffers */
506 : N = xLen;
507 : old_x_len = tilt_len;
508 : old_y_len = ceilf(228.0 * fs / 12800.0) + inter_len;
509 : L_past_x = old_x_len;
510 : move_float(buf_x, mem_old_x, old_x_len);
511 : move_float(&buf_x[old_x_len], x, xLen);
512 : L_past_y = old_y_len;
513 : move_float(buf_y, mem_old_y, old_y_len);
514 : zero_float( &buf_y[old_y_len], xLen );
515 : #endif
516 :
517 0 : N4 = fs * 0.0025;
518 : #ifdef CR9_C_ADD_1p25MS
519 0 : N4 = MIN(N4, xLen);
520 : #endif
521 0 : N34 = N - N4;
522 :
523 : /* Init filter parameters */
524 0 : if (mem_param[1] == 1) {
525 0 : for (i = 0; i < inter_len_r; i++) {
526 0 : a1[i] = *mem_gain * inter_filter[*mem_pitch_fr][i];
527 : }
528 :
529 0 : assert( *mem_beta_idx >= 0 );
530 0 : for (i = 0; i < tilt_len_r; i++) {
531 0 : b1[i] = conf_alpha * (*mem_gain) * tilt_filter[*mem_beta_idx][i];
532 : }
533 :
534 0 : p1 = *mem_pitch_int;
535 : }
536 :
537 0 : if (param[1] == 1) {
538 0 : assert( conf_beta_idx >= 0 );
539 0 : for (i = 0; i < tilt_len_r; i++) {
540 0 : b2[i] = conf_alpha * gain * tilt_filter[conf_beta_idx][i];
541 : }
542 :
543 0 : for (i = 0; i < inter_len_r; i++) {
544 0 : a2[i] = gain * inter_filter[pitch_fr][i];
545 : }
546 :
547 0 : p2 = pitch_int;
548 : }
549 :
550 : #ifdef CR9_C_ADD_1p25MS
551 0 : if (*mem_continuation == 0) {
552 : #endif
553 : /* check fading case */
554 0 : if (mem_param[1] == 0 && param[1] == 0) {
555 0 : fading_case = 1;
556 0 : } else if (mem_param[1] == 1 && param[1] == 0) {
557 0 : fading_case = 3;
558 0 : } else if (mem_param[1] == 0 && param[1] == 1) {
559 0 : fading_case = 2;
560 0 : } else if (*mem_pitch_int == pitch_int && *mem_pitch_fr == pitch_fr) {
561 0 : fading_case = 4;
562 : } else {
563 0 : fading_case = 5;
564 : }
565 : #ifdef CR9_C_ADD_1p25MS
566 : }
567 : #endif
568 :
569 0 : N4_D = N4;
570 : UNUSED(N4_D); UNUSED(N4_S);
571 0 : N4_S = 0;
572 : #ifdef CR9_C_ADD_1p25MS
573 0 : if (frame_dms == LC3PLUS_FRAME_DURATION_1p25MS)
574 : {
575 0 : N4_D = 2*N4;
576 0 : if (*mem_continuation)
577 : {
578 0 : N4_S = N4;
579 : }
580 : }
581 : #endif
582 :
583 : /* First quarter of the current frame: cross-fading */
584 0 : fade_fac = 1. / (LC3_FLOAT) N4_D;
585 0 : current_fade_fac_up = N4_S*fade_fac;
586 0 : current_fade_fac_down = 1.f - current_fade_fac_up;
587 : (void) p_x; (void) p_y; (void) p_a1; (void) p_b1;
588 :
589 0 : if (fading_case == 1) {
590 :
591 0 : memmove(&buf_y[L_past_y], &buf_x[L_past_x], sizeof(LC3_FLOAT) * N4);
592 : #if defined( CR9_C_ADD_1p25MS ) && defined( FIX_LTPF_MEM_CONTINUATION )
593 0 : *mem_continuation = get_continuation(fading_case, frame_dms, (N4+N4_S), N4_D);
594 : #endif
595 0 : } else if (fading_case == 3) {
596 0 : for (n = 0; n < N4; n++) {
597 0 : sum1 = 0;
598 0 : sum2 = 0;
599 0 : j = 0;
600 0 : for (i = L_past_x + n; i >= L_past_x + n - tilt_len; i--) {
601 0 : sum1 += b1[j] * buf_x[i];
602 0 : j++;
603 : }
604 :
605 0 : j = 0;
606 0 : for (i = L_past_y + n - p1 + inter_len - 1; i >= L_past_y + n - p1 - inter_len; i--) {
607 0 : sum2 += a1[j] * buf_y[i];
608 0 : j++;
609 : }
610 :
611 0 : buf_y[L_past_y + n] = buf_x[L_past_x + n] - current_fade_fac_down * sum1 +
612 0 : current_fade_fac_down * sum2;
613 0 : current_fade_fac_down -= fade_fac;
614 : }
615 : #ifdef CR9_C_ADD_1p25MS
616 0 : *mem_continuation = get_continuation(fading_case, frame_dms, (n+N4_S), N4_D);
617 : #endif
618 0 : } else if (fading_case == 2) {
619 0 : for (n = 0; n < N4; n++) {
620 0 : sum1 = 0;
621 0 : sum2 = 0;
622 0 : j = 0;
623 0 : for (i = L_past_x + n; i >= L_past_x + n - tilt_len; i--) {
624 0 : sum1 += b2[j] * buf_x[i];
625 0 : j++;
626 : }
627 :
628 0 : j = 0;
629 0 : for (i = L_past_y + n - p2 + inter_len - 1; i >= L_past_y + n - p2 - inter_len; i--) {
630 0 : sum2 += a2[j] * buf_y[i];
631 0 : j++;
632 : }
633 :
634 0 : buf_y[L_past_y + n] = buf_x[L_past_x + n] - current_fade_fac_up * sum1 + current_fade_fac_up * sum2;
635 0 : current_fade_fac_up += fade_fac;
636 : }
637 : #ifdef CR9_C_ADD_1p25MS
638 0 : *mem_continuation = get_continuation(fading_case, frame_dms, (n+N4_S), N4_D);
639 : #endif
640 0 : } else if (fading_case == 4) {
641 0 : for (n = 0; n < N4; n++) {
642 0 : sum1 = 0;
643 0 : sum2 = 0;
644 0 : j = 0;
645 0 : for (i = L_past_x + n; i >= L_past_x + n - tilt_len; i--) {
646 0 : sum1 += b2[j] * buf_x[i];
647 0 : j++;
648 : }
649 :
650 0 : j = 0;
651 0 : for (i = L_past_y + n - p2 + inter_len - 1; i >= L_past_y + n - p2 - inter_len; i--) {
652 0 : sum2 += a2[j] * buf_y[i];
653 0 : j++;
654 : }
655 :
656 0 : buf_y[L_past_y + n] = buf_x[L_past_x + n] - sum1 + sum2;
657 : }
658 : #ifdef CR9_C_ADD_1p25MS
659 0 : *mem_continuation = get_continuation(fading_case, frame_dms, (n+N4_S), N4_D);
660 : #endif
661 : } else {
662 0 : p_x_init = &buf_x[L_past_x];
663 0 : p_y_init = &buf_y[L_past_y - p1 + inter_len - 1];
664 0 : p_y2 = &buf_y[L_past_y];
665 0 : for (n = 0; n < N4; n++) {
666 0 : sum1 = 0;
667 0 : sum2 = 0;
668 0 : p_b1 = b1;
669 0 : p_x = p_x_init;
670 0 : for (i = tilt_len; i >= 0; i--) {
671 0 : sum1 += *p_b1 * *p_x;
672 0 : p_b1++;
673 0 : p_x--;
674 : }
675 :
676 0 : p_y = p_y_init;
677 0 : p_a1 = a1;
678 0 : for (i = 2*inter_len - 1; i >= 0; i--) {
679 0 : sum2 += *p_a1 * *p_y;
680 0 : p_a1++;
681 0 : p_y--;
682 : }
683 :
684 0 : *p_y2 = *p_x_init - current_fade_fac_down * sum1 +
685 0 : current_fade_fac_down * sum2;
686 0 : current_fade_fac_down -= fade_fac;
687 0 : p_x_init++;
688 0 : p_y_init++;
689 0 : p_y2++;
690 : }
691 :
692 0 : move_float(buf_z, buf_y, (old_y_len + xLen));
693 0 : p_x_init = &buf_z[L_past_y]; /* buf z in this case */
694 0 : p_y_init = &buf_y[L_past_y - p2 + inter_len - 1];
695 0 : p_y2 = &buf_y[L_past_y];
696 :
697 0 : for (n = 0; n < N4; n++) {
698 0 : sum1 = 0;
699 0 : sum2 = 0;
700 0 : j = 0;
701 0 : p_x = p_x_init;
702 0 : p_b2 = b2;
703 0 : for (i = tilt_len; i >= 0; i--) {
704 0 : sum1 += *p_b2 * *p_x;
705 0 : p_b2++;
706 0 : p_x--;
707 : }
708 :
709 0 : p_y = p_y_init;
710 0 : p_a2 = a2;
711 0 : for (i = 2*inter_len - 1; i >= 0; i--) {
712 0 : sum2 += *p_a2 * *p_y;
713 0 : p_a2++;
714 0 : p_y--;
715 : }
716 :
717 0 : *p_y2 = *p_x_init - current_fade_fac_up * sum1 + current_fade_fac_up * sum2;
718 0 : current_fade_fac_up += fade_fac;
719 0 : p_x_init++;
720 0 : p_y_init++;
721 0 : p_y2++;
722 : }
723 : #ifdef CR9_C_ADD_1p25MS
724 0 : *mem_continuation = get_continuation(fading_case, frame_dms, (n+N4_S), N4_D);
725 : #endif
726 : }
727 :
728 : /* Second quarter of the current frame */
729 0 : if (param[1] == 0) {
730 0 : move_float(&buf_y[L_past_y + N4], &buf_x[L_past_x + N4],
731 : ((L_past_x + N4 + N34) - (L_past_x + N4)));
732 : } else {
733 0 : p_x_init = &buf_x[L_past_x + N4];
734 0 : p_y_init = &buf_y[L_past_y + N4 - p2 + inter_len - 1];
735 0 : p_y2 = &buf_y[L_past_y + N4];
736 0 : for (n = 0; n < N34; n++) {
737 0 : sum1 = 0;
738 0 : sum2 = 0;
739 0 : p_b2 = b2;
740 0 : p_x = p_x_init;
741 :
742 0 : for (i = 0; i <= tilt_len; i++) {
743 0 : sum1 += *p_b2 * *p_x;
744 0 : p_b2++;
745 0 : p_x--;
746 : }
747 :
748 0 : p_a2 = a2;
749 0 : p_y = p_y_init;
750 :
751 0 : for (i = 2*inter_len - 1; i >= 0; i--) {
752 0 : sum2 += *p_a2 * *p_y;
753 0 : p_a2++;
754 0 : p_y--;
755 : }
756 0 : p_y_init++;
757 0 : *p_y2 = *p_x_init - sum1 + sum2;
758 0 : p_x_init++;
759 0 : p_y2++;
760 : }
761 : }
762 : /* Output */
763 0 : move_float(y, &buf_y[L_past_y], N);
764 :
765 : /* Update memory */
766 0 : move_float(mem_old_x, &buf_x[N], old_x_len);
767 0 : move_float(mem_old_y, &buf_y[N], old_y_len);
768 :
769 : #ifndef FIX_LTPF_DEC_FLFX_MISMATCH
770 : *mem_ltpf_active = ( *conf_beta > 0 );
771 : #endif
772 : }
773 : }
774 :
775 :
776 1186529 : if ( bfi == 0 && hrmode == 1 && ( frame_dms == LC3PLUS_FRAME_DURATION_5MS || frame_dms == LC3PLUS_FRAME_DURATION_2p5MS ) )
777 : {
778 0 : pitch_delta = LC3_FABS( (LC3_FLOAT) *mem_pitch_int + (LC3_FLOAT) ( *mem_pitch_fr / 4.0 ) - (LC3_FLOAT) pitch_int - (LC3_FLOAT) ( pitch_fr / 4.0 ) );
779 0 : *rel_pitch_change = pitch_delta / MAX( (LC3_FLOAT) *mem_pitch_int + (LC3_FLOAT) ( *mem_pitch_fr / 4.0 ), 1 );
780 : }
781 :
782 : #ifdef CR9_C_ADD_1p25MS
783 1186529 : if (frame_dms == LC3PLUS_FRAME_DURATION_1p25MS)
784 : {
785 0 : move_int(mem_param_prev, mem_param, 3);
786 0 : *mem_pitch_int_prev = *mem_pitch_int;
787 0 : *mem_pitch_fr_prev = *mem_pitch_fr;
788 0 : *mem_gain_prev = *mem_gain;
789 0 : *mem_beta_idx_prev = *mem_beta_idx;
790 : }
791 : #endif
792 :
793 : /* Update ltpf param memory */
794 1186529 : move_int(mem_param, param, 3);
795 1186529 : *mem_pitch_int = pitch_int;
796 1186529 : *mem_pitch_fr = pitch_fr;
797 1186529 : *mem_gain = gain;
798 1186529 : *mem_beta_idx = conf_beta_idx;
799 :
800 :
801 : #ifdef WMOPS
802 : pop_wmops();
803 : #endif
804 1186529 : }
|