comparison libgsmefr/cod_12k2.c @ 58:d285877fc4cc

libgsmefr: cod_12k2.c compiles
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 25 Nov 2022 18:50:50 +0000
parents 49dd1ac8e75b
children d9ad0f5121e8
comparison
equal deleted inserted replaced
57:e005e7b91f3c 58:d285877fc4cc
13 * Speech encoder routine operating on a frame basis. 13 * Speech encoder routine operating on a frame basis.
14 * 14 *
15 15
16 ***************************************************************************/ 16 ***************************************************************************/
17 17
18 #include "gsm_efr.h"
18 #include "typedef.h" 19 #include "typedef.h"
20 #include "namespace.h"
19 #include "basic_op.h" 21 #include "basic_op.h"
20 #include "sig_proc.h" 22 #include "sig_proc.h"
21 #include "count.h" 23 #include "memops.h"
24 #include "no_count.h"
22 #include "codec.h" 25 #include "codec.h"
23 #include "cnst.h" 26 #include "cnst.h"
27 #include "enc_state.h"
24 28
25 #include "window2.tab" 29 #include "window2.tab"
26 30
27 #include "vad.h" 31 #include "vad.h"
28 #include "dtx.h" 32 #include "dtx.h"
40 * PIT_MIN : Minimum pitch lag * 44 * PIT_MIN : Minimum pitch lag *
41 * PIT_MAX : Maximum pitch lag * 45 * PIT_MAX : Maximum pitch lag *
42 * L_INTERPOL : Length of filter for interpolation * 46 * L_INTERPOL : Length of filter for interpolation *
43 *-----------------------------------------------------------*/ 47 *-----------------------------------------------------------*/
44 48
45 /*--------------------------------------------------------*
46 * Static memory allocation. *
47 *--------------------------------------------------------*/
48
49 /* Speech vector */
50
51 static Word16 old_speech[L_TOTAL];
52 static Word16 *speech, *p_window, *p_window_mid;
53 Word16 *new_speech; /* Global variable */
54
55 /* Weight speech vector */
56
57 static Word16 old_wsp[L_FRAME + PIT_MAX];
58 static Word16 *wsp;
59
60 /* Excitation vector */
61
62 static Word16 old_exc[L_FRAME + PIT_MAX + L_INTERPOL];
63 static Word16 *exc;
64
65 /* Zero vector */
66
67 static Word16 ai_zero[L_SUBFR + MP1];
68 static Word16 *zero;
69
70 /* Impulse response vector */
71
72 static Word16 *h1;
73 static Word16 hvec[L_SUBFR * 2];
74
75 /* Spectral expansion factors */ 49 /* Spectral expansion factors */
76 50
77 static const Word16 F_gamma1[M] = 51 static const Word16 F_gamma1[M] =
78 { 52 {
79 29491, 26542, 23888, 21499, 19349, 53 29491, 26542, 23888, 21499, 19349,
83 { 57 {
84 19661, 11797, 7078, 4247, 2548, 58 19661, 11797, 7078, 4247, 2548,
85 1529, 917, 550, 330, 198 59 1529, 917, 550, 330, 198
86 }; 60 };
87 61
88 /* Lsp (Line spectral pairs) */
89
90 static Word16 lsp_old[M];
91 static Word16 lsp_old_q[M];
92
93 /* Filter's memory */
94
95 static Word16 mem_syn[M], mem_w0[M], mem_w[M];
96 static Word16 mem_err[M + L_SUBFR], *error;
97
98 /*************************************************************************** 62 /***************************************************************************
99 * FUNCTION: Init_Coder_12k2 63 * FUNCTION: Init_Coder_12k2
100 * 64 *
101 * PURPOSE: Initialization of variables for the coder section. 65 * PURPOSE: Initialization of variables for the coder section.
102 * 66 *
105 * - initialize static pointers 69 * - initialize static pointers
106 * - set static vectors to zero 70 * - set static vectors to zero
107 * 71 *
108 ***************************************************************************/ 72 ***************************************************************************/
109 73
110 void Init_Coder_12k2 (void) 74 void Init_Coder_12k2 (struct EFR_encoder_state *st)
111 { 75 {
112
113 /*--------------------------------------------------------------------------*
114 * Initialize pointers to speech vector. *
115 *--------------------------------------------------------------------------*/
116
117 new_speech = old_speech + L_TOTAL - L_FRAME;/* New speech */
118 speech = new_speech; /* Present frame */
119 p_window = old_speech + L_TOTAL - L_WINDOW; /* For LPC window */
120 p_window_mid = p_window; /* For LPC window */
121
122 /* Initialize static pointers */
123
124 wsp = old_wsp + PIT_MAX;
125 exc = old_exc + PIT_MAX + L_INTERPOL;
126 zero = ai_zero + MP1;
127 error = mem_err + M;
128 h1 = &hvec[L_SUBFR];
129
130 /* Static vectors to zero */ 76 /* Static vectors to zero */
131 77
132 Set_zero (old_speech, L_TOTAL); 78 Set_zero (st->old_speech, L_TOTAL);
133 Set_zero (old_exc, PIT_MAX + L_INTERPOL); 79 Set_zero (st->old_exc, PIT_MAX + L_INTERPOL);
134 Set_zero (old_wsp, PIT_MAX); 80 Set_zero (st->old_wsp, PIT_MAX);
135 Set_zero (mem_syn, M); 81 Set_zero (st->mem_syn, M);
136 Set_zero (mem_w, M); 82 Set_zero (st->mem_w, M);
137 Set_zero (mem_w0, M); 83 Set_zero (st->mem_w0, M);
138 Set_zero (mem_err, M); 84 Set_zero (st->mem_err, M);
139 Set_zero (zero, L_SUBFR); 85 Set_zero (st->ai_zero + MP1, L_SUBFR);
140 Set_zero (hvec, L_SUBFR); /* set to zero "h1[-L_SUBFR..-1]" */ 86 Set_zero (st->hvec, L_SUBFR); /* set to zero "h1[-L_SUBFR..-1]" */
141 87
142 /* Initialize lsp_old [] */ 88 /* Initialize lsp_old [] */
143 89
144 lsp_old[0] = 30000; 90 st->lsp_old[0] = 30000;
145 lsp_old[1] = 26000; 91 st->lsp_old[1] = 26000;
146 lsp_old[2] = 21000; 92 st->lsp_old[2] = 21000;
147 lsp_old[3] = 15000; 93 st->lsp_old[3] = 15000;
148 lsp_old[4] = 8000; 94 st->lsp_old[4] = 8000;
149 lsp_old[5] = 0; 95 st->lsp_old[5] = 0;
150 lsp_old[6] = -8000; 96 st->lsp_old[6] = -8000;
151 lsp_old[7] = -15000; 97 st->lsp_old[7] = -15000;
152 lsp_old[8] = -21000; 98 st->lsp_old[8] = -21000;
153 lsp_old[9] = -26000; 99 st->lsp_old[9] = -26000;
154 100
155 /* Initialize lsp_old_q[] */ 101 /* Initialize lsp_old_q[] */
156 102
157 Copy (lsp_old, lsp_old_q, M); 103 Copy (st->lsp_old, st->lsp_old_q, M);
158 104
159 return; 105 return;
160 } 106 }
161 107
162 /*************************************************************************** 108 /***************************************************************************
182 * synth[]: Local synthesis speech (for debugging purposes) 128 * synth[]: Local synthesis speech (for debugging purposes)
183 * 129 *
184 ***************************************************************************/ 130 ***************************************************************************/
185 131
186 void Coder_12k2 ( 132 void Coder_12k2 (
133 struct EFR_encoder_state *st,
187 Word16 ana[], /* output : Analysis parameters */ 134 Word16 ana[], /* output : Analysis parameters */
188 Word16 synth[] /* output : Local synthesis */ 135 Word16 synth[] /* output : Local synthesis */
189 ) 136 )
190 { 137 {
138 /* handy pointers that were static vars in the original code */
139 Word16 *speech = st->old_speech + L_TOTAL - L_FRAME;
140 Word16 *p_window = st->old_speech + L_TOTAL - L_WINDOW;
141 Word16 *wsp = st->old_wsp + PIT_MAX;
142 Word16 *exc = st->old_exc + PIT_MAX + L_INTERPOL;
143 Word16 *zero = st->ai_zero + MP1;
144 Word16 *h1 = st->hvec + L_SUBFR;
145 Word16 *error = st->mem_err + M;
146
191 /* LPC coefficients */ 147 /* LPC coefficients */
192 148
193 Word16 r_l[MP1], r_h[MP1]; /* Autocorrelations lo and hi */ 149 Word16 r_l[MP1], r_h[MP1]; /* Autocorrelations lo and hi */
194 Word16 A_t[(MP1) * 4]; /* A(z) unquantized for the 4 subframes */ 150 Word16 A_t[(MP1) * 4]; /* A(z) unquantized for the 4 subframes */
195 Word16 Aq_t[(MP1) * 4]; /* A(z) quantized for the 4 subframes */ 151 Word16 Aq_t[(MP1) * 4]; /* A(z) quantized for the 4 subframes */
215 Word16 gain_pit, gain_code, pit_flag, pit_sharp; 171 Word16 gain_pit, gain_code, pit_flag, pit_sharp;
216 Word16 temp; 172 Word16 temp;
217 Word32 L_temp; 173 Word32 L_temp;
218 174
219 Word16 scal_acf, VAD_flag, lags[2], rc[4]; 175 Word16 scal_acf, VAD_flag, lags[2], rc[4];
220
221 extern Word16 ptch;
222 extern Word16 txdtx_ctrl, CN_excitation_gain;
223 extern Word32 L_pn_seed_tx;
224 extern Word16 dtx_mode;
225 176
226 /*----------------------------------------------------------------------* 177 /*----------------------------------------------------------------------*
227 * - Perform LPC analysis: (twice per frame) * 178 * - Perform LPC analysis: (twice per frame) *
228 * * autocorrelation + lag windowing * 179 * * autocorrelation + lag windowing *
229 * * Levinson-Durbin algorithm to find a[] * 180 * * Levinson-Durbin algorithm to find a[] *
233 * subframes (both quantized and unquantized) * 184 * subframes (both quantized and unquantized) *
234 *----------------------------------------------------------------------*/ 185 *----------------------------------------------------------------------*/
235 186
236 /* LP analysis centered at 2nd subframe */ 187 /* LP analysis centered at 2nd subframe */
237 188
238 189 scal_acf = Autocorr (p_window, M, r_h, r_l, window_160_80);
239 scal_acf = Autocorr (p_window_mid, M, r_h, r_l, window_160_80);
240 /* Autocorrelations */ 190 /* Autocorrelations */
241 191
242 #if (WMOPS)
243 fwc (); /* function worst case */
244 #endif
245
246 Lag_window (M, r_h, r_l); /* Lag windowing */ 192 Lag_window (M, r_h, r_l); /* Lag windowing */
247 193
248 #if (WMOPS) 194 Levinson (st, r_h, r_l, &A_t[MP1], rc); /* Levinson-Durbin */
249 fwc (); /* function worst case */ 195
250 #endif 196 Az_lsp (&A_t[MP1], lsp_mid, st->lsp_old); /* From A(z) to lsp */
251
252 Levinson (r_h, r_l, &A_t[MP1], rc); /* Levinson-Durbin */
253
254 #if (WMOPS)
255 fwc (); /* function worst case */
256 #endif
257
258 Az_lsp (&A_t[MP1], lsp_mid, lsp_old); /* From A(z) to lsp */
259
260 #if (WMOPS)
261 fwc (); /* function worst case */
262 #endif
263 197
264 /* LP analysis centered at 4th subframe */ 198 /* LP analysis centered at 4th subframe */
265 199
266 /* Autocorrelations */ 200 /* Autocorrelations */
267 scal_acf = Autocorr (p_window, M, r_h, r_l, window_232_8); 201 scal_acf = Autocorr (p_window, M, r_h, r_l, window_232_8);
268 202
269 #if (WMOPS)
270 fwc (); /* function worst case */
271 #endif
272
273 Lag_window (M, r_h, r_l); /* Lag windowing */ 203 Lag_window (M, r_h, r_l); /* Lag windowing */
274 204
275 #if (WMOPS) 205 Levinson (st, r_h, r_l, &A_t[MP1 * 3], rc); /* Levinson-Durbin */
276 fwc (); /* function worst case */
277 #endif
278
279 Levinson (r_h, r_l, &A_t[MP1 * 3], rc); /* Levinson-Durbin */
280
281 #if (WMOPS)
282 fwc (); /* function worst case */
283 #endif
284 206
285 Az_lsp (&A_t[MP1 * 3], lsp_new, lsp_mid); /* From A(z) to lsp */ 207 Az_lsp (&A_t[MP1 * 3], lsp_new, lsp_mid); /* From A(z) to lsp */
286 208
287 #if (WMOPS) 209 if (st->dtx_mode)
288 fwc (); /* function worst case */
289 #endif
290
291 if (dtx_mode == 1)
292 { 210 {
293 /* DTX enabled, make voice activity decision */ 211 /* DTX enabled, make voice activity decision */
294 VAD_flag = vad_computation (r_h, r_l, scal_acf, rc, ptch); 212 VAD_flag = vad_computation (st, r_h, r_l, scal_acf, rc, st->ptch);
295 move16 (); 213 tx_dtx (st, VAD_flag); /* TX DTX handler */
296
297 tx_dtx (VAD_flag, &txdtx_ctrl); /* TX DTX handler */
298 } 214 }
299 else 215 else
300 { 216 {
301 /* DTX disabled, active speech in every frame */ 217 /* DTX disabled, active speech in every frame */
302 VAD_flag = 1; 218 VAD_flag = 1;
303 txdtx_ctrl = TX_VAD_FLAG | TX_SP_FLAG; 219 st->txdtx_ctrl = TX_VAD_FLAG | TX_SP_FLAG;
304 } 220 }
305 221
306 /* LSP quantization (lsp_mid[] and lsp_new[] jointly quantized) */ 222 /* LSP quantization (lsp_mid[] and lsp_new[] jointly quantized) */
307 223
308 Q_plsf_5 (lsp_mid, lsp_new, lsp_mid_q, lsp_new_q, ana, txdtx_ctrl); 224 Q_plsf_5 (st, lsp_mid, lsp_new, lsp_mid_q, lsp_new_q, ana, st->txdtx_ctrl);
309 225
310 #if (WMOPS) 226 ana += 5;
311 fwc (); /* function worst case */
312 #endif
313 ana += 5; move16 ();
314 227
315 /*--------------------------------------------------------------------* 228 /*--------------------------------------------------------------------*
316 * Find interpolated LPC parameters in all subframes (both quantized * 229 * Find interpolated LPC parameters in all subframes (both quantized *
317 * and unquantized). * 230 * and unquantized). *
318 * The interpolated parameters are in array A_t[] of size (M+1)*4 * 231 * The interpolated parameters are in array A_t[] of size (M+1)*4 *
319 * and the quantized interpolated parameters are in array Aq_t[] * 232 * and the quantized interpolated parameters are in array Aq_t[] *
320 *--------------------------------------------------------------------*/ 233 *--------------------------------------------------------------------*/
321 234
322 Int_lpc2 (lsp_old, lsp_mid, lsp_new, A_t); 235 Int_lpc2 (st->lsp_old, lsp_mid, lsp_new, A_t);
323 236
324 #if (WMOPS) 237 if ((st->txdtx_ctrl & TX_SP_FLAG) != 0)
325 fwc (); /* function worst case */ 238 {
326 #endif 239 Int_lpc (st->lsp_old_q, lsp_mid_q, lsp_new_q, Aq_t);
327
328 test (); logic16 ();
329 if ((txdtx_ctrl & TX_SP_FLAG) != 0)
330 {
331 Int_lpc (lsp_old_q, lsp_mid_q, lsp_new_q, Aq_t);
332 240
333 /* update the LSPs for the next frame */ 241 /* update the LSPs for the next frame */
334 for (i = 0; i < M; i++) 242 Copy (lsp_new, st->lsp_old, M);
335 { 243 Copy (lsp_new_q, st->lsp_old_q, M);
336 lsp_old[i] = lsp_new[i]; move16 ();
337 lsp_old_q[i] = lsp_new_q[i]; move16 ();
338 }
339 } 244 }
340 else 245 else
341 { 246 {
342 /* Use unquantized LPC parameters in case of no speech activity */ 247 /* Use unquantized LPC parameters in case of no speech activity */
343 for (i = 0; i < MP1; i++) 248 for (i = 0; i < MP1; i++)
347 Aq_t[i + MP1 * 2] = A_t[i + MP1 * 2]; move16 (); 252 Aq_t[i + MP1 * 2] = A_t[i + MP1 * 2]; move16 ();
348 Aq_t[i + MP1 * 3] = A_t[i + MP1 * 3]; move16 (); 253 Aq_t[i + MP1 * 3] = A_t[i + MP1 * 3]; move16 ();
349 } 254 }
350 255
351 /* update the LSPs for the next frame */ 256 /* update the LSPs for the next frame */
352 for (i = 0; i < M; i++) 257 Copy (lsp_new, st->lsp_old, M);
353 { 258 Copy (lsp_new_q, st->lsp_old_q, M);
354 lsp_old[i] = lsp_new[i]; move16 (); 259 }
355 lsp_old_q[i] = lsp_new[i]; move16 ();
356 }
357 }
358
359 #if (WMOPS)
360 fwc (); /* function worst case */
361 #endif
362 260
363 /*----------------------------------------------------------------------* 261 /*----------------------------------------------------------------------*
364 * - Find the weighted input speech wsp[] for the whole speech frame * 262 * - Find the weighted input speech wsp[] for the whole speech frame *
365 * - Find the open-loop pitch delay for first 2 subframes * 263 * - Find the open-loop pitch delay for first 2 subframes *
366 * - Set the range for searching closed-loop pitch in 1st subframe * 264 * - Set the range for searching closed-loop pitch in 1st subframe *
370 A = A_t; move16 (); 268 A = A_t; move16 ();
371 for (i = 0; i < L_FRAME; i += L_SUBFR) 269 for (i = 0; i < L_FRAME; i += L_SUBFR)
372 { 270 {
373 Weight_Ai (A, F_gamma1, Ap1); 271 Weight_Ai (A, F_gamma1, Ap1);
374 272
375 #if (WMOPS)
376 fwc (); /* function worst case */
377 #endif
378
379 Weight_Ai (A, F_gamma2, Ap2); 273 Weight_Ai (A, F_gamma2, Ap2);
380 274
381 #if (WMOPS)
382 fwc (); /* function worst case */
383 #endif
384
385 Residu (Ap1, &speech[i], &wsp[i], L_SUBFR); 275 Residu (Ap1, &speech[i], &wsp[i], L_SUBFR);
386 276
387 #if (WMOPS) 277 Syn_filt (Ap2, &wsp[i], &wsp[i], L_SUBFR, st->mem_w, 1);
388 fwc (); /* function worst case */
389 #endif
390
391 Syn_filt (Ap2, &wsp[i], &wsp[i], L_SUBFR, mem_w, 1);
392
393 #if (WMOPS)
394 fwc (); /* function worst case */
395 #endif
396 278
397 A += MP1; move16 (); 279 A += MP1; move16 ();
398 } 280 }
399 281
400 /* Find open loop pitch lag for first two subframes */ 282 /* Find open loop pitch lag for first two subframes */
401 283
402 T_op = Pitch_ol (wsp, PIT_MIN, PIT_MAX, L_FRAME_BY2); move16 (); 284 T_op = Pitch_ol (wsp, PIT_MIN, PIT_MAX, L_FRAME_BY2); move16 ();
403 285
404 #if (WMOPS)
405 fwc (); /* function worst case */
406 #endif
407
408 lags[0] = T_op; move16 (); 286 lags[0] = T_op; move16 ();
409 287
410 test (); logic16 (); 288 if ((st->txdtx_ctrl & TX_SP_FLAG) != 0)
411 if ((txdtx_ctrl & TX_SP_FLAG) != 0)
412 { 289 {
413 /* Range for closed loop pitch search in 1st subframe */ 290 /* Range for closed loop pitch search in 1st subframe */
414 291
415 T0_min = sub (T_op, 3); 292 T0_min = sub (T_op, 3);
416 test (); 293 if (T0_min < PIT_MIN)
417 if (sub (T0_min, PIT_MIN) < 0)
418 { 294 {
419 T0_min = PIT_MIN; move16 (); 295 T0_min = PIT_MIN; move16 ();
420 } 296 }
421 T0_max = add (T0_min, 6); 297 T0_max = add (T0_min, 6);
422 test (); 298 if (T0_max > PIT_MAX)
423 if (sub (T0_max, PIT_MAX) > 0)
424 { 299 {
425 T0_max = PIT_MAX; move16 (); 300 T0_max = PIT_MAX; move16 ();
426 T0_min = sub (T0_max, 6); 301 T0_min = sub (T0_max, 6);
427 } 302 }
428 #if (WMOPS)
429 fwc (); /* function worst case */
430 #endif
431 } 303 }
432 /* Find open loop pitch lag for last two subframes */ 304 /* Find open loop pitch lag for last two subframes */
433 305
434 T_op = Pitch_ol (&wsp[L_FRAME_BY2], PIT_MIN, PIT_MAX, L_FRAME_BY2); 306 T_op = Pitch_ol (&wsp[L_FRAME_BY2], PIT_MIN, PIT_MAX, L_FRAME_BY2);
435 move16 (); 307
436 308 if (st->dtx_mode)
437 #if (WMOPS)
438 fwc (); /* function worst case */
439 #endif
440
441 if (dtx_mode == 1)
442 { 309 {
443 lags[1] = T_op; move16 (); 310 lags[1] = T_op; move16 ();
444 periodicity_update (lags, &ptch); 311 periodicity_update (st, lags);
445 } 312 }
446 /*----------------------------------------------------------------------* 313 /*----------------------------------------------------------------------*
447 * Loop for every subframe in the analysis frame * 314 * Loop for every subframe in the analysis frame *
448 *----------------------------------------------------------------------* 315 *----------------------------------------------------------------------*
449 * To find the pitch and innovation parameters. The subframe size is * 316 * To find the pitch and innovation parameters. The subframe size is *
468 /* pointer to interpolated quantized LPC parameters */ 335 /* pointer to interpolated quantized LPC parameters */
469 Aq = Aq_t; move16 (); 336 Aq = Aq_t; move16 ();
470 337
471 for (i_subfr = 0; i_subfr < L_FRAME; i_subfr += L_SUBFR) 338 for (i_subfr = 0; i_subfr < L_FRAME; i_subfr += L_SUBFR)
472 { 339 {
473 340 if ((st->txdtx_ctrl & TX_SP_FLAG) != 0)
474 test (); logic16 ();
475 if ((txdtx_ctrl & TX_SP_FLAG) != 0)
476 { 341 {
477 342
478 /*---------------------------------------------------------------* 343 /*---------------------------------------------------------------*
479 * Find the weighted LPC coefficients for the weighting filter. * 344 * Find the weighted LPC coefficients for the weighting filter. *
480 *---------------------------------------------------------------*/ 345 *---------------------------------------------------------------*/
481 346
482 Weight_Ai (A, F_gamma1, Ap1); 347 Weight_Ai (A, F_gamma1, Ap1);
483 348
484 #if (WMOPS)
485 fwc (); /* function worst case */
486 #endif
487
488 Weight_Ai (A, F_gamma2, Ap2); 349 Weight_Ai (A, F_gamma2, Ap2);
489
490 #if (WMOPS)
491 fwc (); /* function worst case */
492 #endif
493 350
494 /*---------------------------------------------------------------* 351 /*---------------------------------------------------------------*
495 * Compute impulse response, h1[], of weighted synthesis filter * 352 * Compute impulse response, h1[], of weighted synthesis filter *
496 *---------------------------------------------------------------*/ 353 *---------------------------------------------------------------*/
497 354
498 for (i = 0; i <= M; i++) 355 Copy (Ap1, st->ai_zero, M+1);
499 { 356
500 ai_zero[i] = Ap1[i]; move16 (); 357 Syn_filt (Aq, st->ai_zero, h1, L_SUBFR, zero, 0);
501 }
502
503 Syn_filt (Aq, ai_zero, h1, L_SUBFR, zero, 0);
504
505 #if (WMOPS)
506 fwc (); /* function worst case */
507 #endif
508 358
509 Syn_filt (Ap2, h1, h1, L_SUBFR, zero, 0); 359 Syn_filt (Ap2, h1, h1, L_SUBFR, zero, 0);
510
511 #if (WMOPS)
512 fwc (); /* function worst case */
513 #endif
514 360
515 } 361 }
516 /*---------------------------------------------------------------* 362 /*---------------------------------------------------------------*
517 * Find the target vector for pitch search: * 363 * Find the target vector for pitch search: *
518 *---------------------------------------------------------------*/ 364 *---------------------------------------------------------------*/
519 365
520 Residu (Aq, &speech[i_subfr], res2, L_SUBFR); /* LPC residual */ 366 Residu (Aq, &speech[i_subfr], res2, L_SUBFR); /* LPC residual */
521 367
522 #if (WMOPS) 368 if ((st->txdtx_ctrl & TX_SP_FLAG) == 0)
523 fwc (); /* function worst case */
524 #endif
525
526 test (); logic16 ();
527 if ((txdtx_ctrl & TX_SP_FLAG) == 0)
528 { 369 {
529 /* Compute comfort noise excitation gain based on 370 /* Compute comfort noise excitation gain based on
530 LP residual energy */ 371 LP residual energy */
531 372
532 CN_excitation_gain = compute_CN_excitation_gain (res2); 373 st->CN_excitation_gain = compute_CN_excitation_gain (res2);
533 move16 ();
534 } 374 }
535 else 375 else
536 { 376 {
537 Copy (res2, &exc[i_subfr], L_SUBFR); 377 Copy (res2, &exc[i_subfr], L_SUBFR);
538 378
539 #if (WMOPS) 379 Syn_filt (Aq, &exc[i_subfr], error, L_SUBFR, st->mem_err, 0);
540 fwc (); /* function worst case */
541 #endif
542
543 Syn_filt (Aq, &exc[i_subfr], error, L_SUBFR, mem_err, 0);
544
545 #if (WMOPS)
546 fwc (); /* function worst case */
547 #endif
548 380
549 Residu (Ap1, error, xn, L_SUBFR); 381 Residu (Ap1, error, xn, L_SUBFR);
550 382
551 #if (WMOPS) 383 /* target signal xn[] */
552 fwc (); /* function worst case */ 384 Syn_filt (Ap2, xn, xn, L_SUBFR, st->mem_w0, 0);
553 #endif
554
555 Syn_filt (Ap2, xn, xn, L_SUBFR, mem_w0, 0); /* target signal xn[]*/
556
557 #if (WMOPS)
558 fwc (); /* function worst case */
559 #endif
560 385
561 /*--------------------------------------------------------------* 386 /*--------------------------------------------------------------*
562 * Closed-loop fractional pitch search * 387 * Closed-loop fractional pitch search *
563 *--------------------------------------------------------------*/ 388 *--------------------------------------------------------------*/
564 389
565 /* flag for first and 3th subframe */ 390 /* flag for first and 3th subframe */
566 pit_flag = i_subfr; move16 (); 391 pit_flag = i_subfr; move16 ();
567 test ();
568 /* set t0_min and t0_max for 3th subf.*/ 392 /* set t0_min and t0_max for 3th subf.*/
569 if (sub (i_subfr, L_FRAME_BY2) == 0) 393 if (i_subfr == L_FRAME_BY2)
570 { 394 {
571 T0_min = sub (T_op, 3); 395 T0_min = sub (T_op, 3);
572 396
573 test (); 397 if (T0_min < PIT_MIN)
574 if (sub (T0_min, PIT_MIN) < 0)
575 { 398 {
576 T0_min = PIT_MIN; move16 (); 399 T0_min = PIT_MIN; move16 ();
577 } 400 }
578 T0_max = add (T0_min, 6); 401 T0_max = add (T0_min, 6);
579 test (); 402 if (T0_max > PIT_MAX)
580 if (sub (T0_max, PIT_MAX) > 0)
581 { 403 {
582 T0_max = PIT_MAX; move16 (); 404 T0_max = PIT_MAX; move16 ();
583 T0_min = sub (T0_max, 6); 405 T0_min = sub (T0_max, 6);
584 } 406 }
585 pit_flag = 0; move16 (); 407 pit_flag = 0; move16 ();
586 } 408 }
587 #if (WMOPS)
588 fwc (); /* function worst case */
589 #endif
590 409
591 T0 = Pitch_fr6 (&exc[i_subfr], xn, h1, L_SUBFR, T0_min, T0_max, 410 T0 = Pitch_fr6 (&exc[i_subfr], xn, h1, L_SUBFR, T0_min, T0_max,
592 pit_flag, &T0_frac); move16 (); 411 pit_flag, &T0_frac); move16 ();
593 412
594 #if (WMOPS)
595 fwc (); /* function worst case */
596 #endif
597
598 *ana = Enc_lag6 (T0, &T0_frac, &T0_min, &T0_max, PIT_MIN, 413 *ana = Enc_lag6 (T0, &T0_frac, &T0_min, &T0_max, PIT_MIN,
599 PIT_MAX, pit_flag); 414 PIT_MAX, pit_flag);
600 move16 ();
601
602 #if (WMOPS)
603 fwc (); /* function worst case */
604 #endif
605 } 415 }
606 ana++; 416 ana++;
607 /* Incrementation of ana is done here to work also 417 /* Incrementation of ana is done here to work also
608 when no speech activity is present */ 418 when no speech activity is present */
609 419
610 test (); logic16 (); 420 if ((st->txdtx_ctrl & TX_SP_FLAG) != 0)
611
612 if ((txdtx_ctrl & TX_SP_FLAG) != 0)
613 { 421 {
614 422
615 /*---------------------------------------------------------------* 423 /*---------------------------------------------------------------*
616 * - find unity gain pitch excitation (adaptive codebook entry) * 424 * - find unity gain pitch excitation (adaptive codebook entry) *
617 * with fractional interpolation. * 425 * with fractional interpolation. *
621 * - find LTP residual. * 429 * - find LTP residual. *
622 *---------------------------------------------------------------*/ 430 *---------------------------------------------------------------*/
623 431
624 Pred_lt_6 (&exc[i_subfr], T0, T0_frac, L_SUBFR); 432 Pred_lt_6 (&exc[i_subfr], T0, T0_frac, L_SUBFR);
625 433
626 #if (WMOPS)
627 fwc (); /* function worst case */
628 #endif
629
630 Convolve (&exc[i_subfr], h1, y1, L_SUBFR); 434 Convolve (&exc[i_subfr], h1, y1, L_SUBFR);
631 435
632 #if (WMOPS)
633 fwc (); /* function worst case */
634 #endif
635
636 gain_pit = G_pitch (xn, y1, L_SUBFR); move16 (); 436 gain_pit = G_pitch (xn, y1, L_SUBFR); move16 ();
637 437
638 #if (WMOPS)
639 fwc (); /* function worst case */
640 #endif
641
642 *ana = q_gain_pitch (&gain_pit); move16 (); 438 *ana = q_gain_pitch (&gain_pit); move16 ();
643
644 #if (WMOPS)
645 fwc (); /* function worst case */
646 #endif
647 439
648 } 440 }
649 else 441 else
650 { 442 {
651 gain_pit = 0; move16 (); 443 gain_pit = 0; move16 ();
652 } 444 }
653 445
654 ana++; /* Incrementation of ana is done here to work 446 ana++; /* Incrementation of ana is done here to work
655 also when no speech activity is present */ 447 also when no speech activity is present */
656 448
657 test (); logic16 (); 449 if ((st->txdtx_ctrl & TX_SP_FLAG) != 0)
658
659 if ((txdtx_ctrl & TX_SP_FLAG) != 0)
660 { 450 {
661 /* xn2[i] = xn[i] - y1[i] * gain_pit */ 451 /* xn2[i] = xn[i] - y1[i] * gain_pit */
662 /* res2[i] -= exc[i+i_subfr] * gain_pit */ 452 /* res2[i] -= exc[i+i_subfr] * gain_pit */
663 453
664 for (i = 0; i < L_SUBFR; i++) 454 for (i = 0; i < L_SUBFR; i++)
670 L_temp = L_mult (exc[i + i_subfr], gain_pit); 460 L_temp = L_mult (exc[i + i_subfr], gain_pit);
671 L_temp = L_shl (L_temp, 3); 461 L_temp = L_shl (L_temp, 3);
672 res2[i] = sub (res2[i], extract_h (L_temp)); move16 (); 462 res2[i] = sub (res2[i], extract_h (L_temp)); move16 ();
673 } 463 }
674 464
675 #if (WMOPS)
676 fwc (); /* function worst case */
677 #endif
678
679 /*-------------------------------------------------------------* 465 /*-------------------------------------------------------------*
680 * - include pitch contribution into impulse resp. h1[] * 466 * - include pitch contribution into impulse resp. h1[] *
681 *-------------------------------------------------------------*/ 467 *-------------------------------------------------------------*/
682 468
683 /* pit_sharp = gain_pit; */ 469 /* pit_sharp = gain_pit; */
689 { 475 {
690 temp = mult (h1[i - T0], pit_sharp); 476 temp = mult (h1[i - T0], pit_sharp);
691 h1[i] = add (h1[i], temp); move16 (); 477 h1[i] = add (h1[i], temp); move16 ();
692 } 478 }
693 479
694 #if (WMOPS)
695 fwc (); /* function worst case */
696 #endif
697
698 /*--------------------------------------------------------------* 480 /*--------------------------------------------------------------*
699 * - Innovative codebook search (find index and gain) * 481 * - Innovative codebook search (find index and gain) *
700 *--------------------------------------------------------------*/ 482 *--------------------------------------------------------------*/
701 483
702 code_10i40_35bits (xn2, res2, h1, code, y2, ana); 484 code_10i40_35bits (xn2, res2, h1, code, y2, ana);
703
704 #if (WMOPS)
705 fwc (); /* function worst case */
706 #endif
707
708 } 485 }
709 else 486 else
710 { 487 {
711 build_CN_code (code, &L_pn_seed_tx); 488 build_CN_code (code, &st->L_pn_seed_tx);
712 } 489 }
713 ana += 10; move16 (); 490 ana += 10; move16 ();
714 491
715 test (); logic16 (); 492 if ((st->txdtx_ctrl & TX_SP_FLAG) != 0)
716 if ((txdtx_ctrl & TX_SP_FLAG) != 0)
717 { 493 {
718 494
719 /*-------------------------------------------------------* 495 /*-------------------------------------------------------*
720 * - Add the pitch contribution to code[]. * 496 * - Add the pitch contribution to code[]. *
721 *-------------------------------------------------------*/ 497 *-------------------------------------------------------*/
724 { 500 {
725 temp = mult (code[i - T0], pit_sharp); 501 temp = mult (code[i - T0], pit_sharp);
726 code[i] = add (code[i], temp); move16 (); 502 code[i] = add (code[i], temp); move16 ();
727 } 503 }
728 504
729 #if (WMOPS)
730 fwc (); /* function worst case */
731 #endif
732
733 /*------------------------------------------------------* 505 /*------------------------------------------------------*
734 * - Quantization of fixed codebook gain. * 506 * - Quantization of fixed codebook gain. *
735 *------------------------------------------------------*/ 507 *------------------------------------------------------*/
736 508
737 gain_code = G_code (xn2, y2); move16 (); 509 gain_code = G_code (xn2, y2); move16 ();
738 510 }
739 #if (WMOPS) 511 *ana++ = q_gain_code (st, code, L_SUBFR, &gain_code, st->txdtx_ctrl,
740 fwc (); /* function worst case */ 512 i_subfr);
741 #endif
742
743 }
744 *ana++ = q_gain_code (code, L_SUBFR, &gain_code, txdtx_ctrl, i_subfr);
745 move16 ();
746
747 #if (WMOPS)
748 fwc (); /* function worst case */
749 #endif
750 513
751 /*------------------------------------------------------* 514 /*------------------------------------------------------*
752 * - Find the total excitation * 515 * - Find the total excitation *
753 * - find synthesis speech corresponding to exc[] * 516 * - find synthesis speech corresponding to exc[] *
754 * - update filter memories for finding the target * 517 * - update filter memories for finding the target *
764 L_temp = L_mac (L_temp, code[i], gain_code); 527 L_temp = L_mac (L_temp, code[i], gain_code);
765 L_temp = L_shl (L_temp, 3); 528 L_temp = L_shl (L_temp, 3);
766 exc[i + i_subfr] = round (L_temp); move16 (); 529 exc[i + i_subfr] = round (L_temp); move16 ();
767 } 530 }
768 531
769 #if (WMOPS) 532 Syn_filt (Aq, &exc[i_subfr], &synth[i_subfr], L_SUBFR, st->mem_syn, 1);
770 fwc (); /* function worst case */ 533
771 #endif 534 if ((st->txdtx_ctrl & TX_SP_FLAG) != 0)
772
773 Syn_filt (Aq, &exc[i_subfr], &synth[i_subfr], L_SUBFR, mem_syn, 1);
774
775 #if (WMOPS)
776 fwc (); /* function worst case */
777 #endif
778
779 test (); logic16 ();
780 if ((txdtx_ctrl & TX_SP_FLAG) != 0)
781 { 535 {
782 536
783 for (i = L_SUBFR - M, j = 0; i < L_SUBFR; i++, j++) 537 for (i = L_SUBFR - M, j = 0; i < L_SUBFR; i++, j++)
784 { 538 {
785 mem_err[j] = sub (speech[i_subfr + i], synth[i_subfr + i]); 539 st->mem_err[j] = sub (speech[i_subfr + i], synth[i_subfr + i]);
786 move16 ();
787 temp = extract_h (L_shl (L_mult (y1[i], gain_pit), 3)); 540 temp = extract_h (L_shl (L_mult (y1[i], gain_pit), 3));
788 k = extract_h (L_shl (L_mult (y2[i], gain_code), 5)); 541 k = extract_h (L_shl (L_mult (y2[i], gain_code), 5));
789 mem_w0[j] = sub (xn[i], add (temp, k)); move16 (); 542 st->mem_w0[j] = sub (xn[i], add (temp, k));
790 } 543 }
791 } 544 }
792 else 545 else
793 { 546 {
794 for (j = 0; j < M; j++) 547 Set_zero (st->mem_err, M);
795 { 548 Set_zero (st->mem_w0, M);
796 mem_err[j] = 0; move16 (); 549 }
797 mem_w0[j] = 0; move16 (); 550
798 }
799 }
800
801 #if (WMOPS)
802 fwc (); /* function worst case */
803 #endif
804 /* interpolated LPC parameters for next subframe */ 551 /* interpolated LPC parameters for next subframe */
805 A += MP1; move16 (); 552 A += MP1; move16 ();
806 Aq += MP1; move16 (); 553 Aq += MP1; move16 ();
807 } 554 }
808 555
810 * Update signal for next frame. * 557 * Update signal for next frame. *
811 * -> shift to the left by L_FRAME: * 558 * -> shift to the left by L_FRAME: *
812 * speech[], wsp[] and exc[] * 559 * speech[], wsp[] and exc[] *
813 *--------------------------------------------------*/ 560 *--------------------------------------------------*/
814 561
815 Copy (&old_speech[L_FRAME], &old_speech[0], L_TOTAL - L_FRAME); 562 Copy (&st->old_speech[L_FRAME], &st->old_speech[0], L_TOTAL - L_FRAME);
816 563
817 #if (WMOPS) 564 Copy (&st->old_wsp[L_FRAME], &st->old_wsp[0], PIT_MAX);
818 fwc (); /* function worst case */ 565
819 #endif 566 Copy (&st->old_exc[L_FRAME], &st->old_exc[0], PIT_MAX + L_INTERPOL);
820
821 Copy (&old_wsp[L_FRAME], &old_wsp[0], PIT_MAX);
822
823 #if (WMOPS)
824 fwc (); /* function worst case */
825 #endif
826
827 Copy (&old_exc[L_FRAME], &old_exc[0], PIT_MAX + L_INTERPOL);
828
829 #if (WMOPS)
830 fwc (); /* function worst case */
831 #endif
832 567
833 return; 568 return;
834 } 569 }