Line data Source code
1 : /*====================================================================================
2 : EVS Codec 3GPP TS26.452 Aug 12, 2021. Version 16.3.0
3 : ====================================================================================*/
4 :
5 : #include <stdint.h>
6 : #include "options.h" /* Compilation switches */
7 : #include "rom_enc.h"
8 : #include "rom_com_fx.h" /* Static table prototypes */
9 : #include "rom_com.h" /* Static table prototypes */
10 : //#include "prot_fx.h" /* Function prototypes */
11 : #include "prot_fx.h" /* Function prototypes */
12 : #include "prot_fx_enc.h" /* Function prototypes */
13 : #include "ivas_prot_fx.h" /* Function prototypes */
14 :
15 :
16 : /*-----------------------------------------------------------------*
17 : * Local constants
18 : *-----------------------------------------------------------------*/
19 : #define MIN_LOG_FX 0
20 : #define MIN_LOG_VAL_FX -15360 /* -60.0f in Q8 */
21 : #define MSVQ_MAXCNT 3000
22 : #define MAXINT32 MAX_32
23 :
24 : /*---------------------------------------------------------------------*
25 : * Local function prototypes
26 : *---------------------------------------------------------------------*/
27 :
28 : static void lsfq_CNG_fx( BSTR_ENC_HANDLE hBstr, const Word16 *lsf, const Word16 *wghts, Word16 *qlsf, Word32 *p_offset_scale1, Word32 *p_offset_scale2, Word16 *p_no_scales );
29 :
30 : static Word32 vq_lvq_lsf_enc( Word16 pred_flag, Word16 mode, Word16 u[], Word16 *levels, Word16 stages, Word16 w[], Word16 Idx[], const Word16 *lsf, const Word16 *pred, Word32 p_offset_scale1[][MAX_NO_SCALES + 1], Word32 p_offset_scale2[][MAX_NO_SCALES + 1], Word16 p_no_scales[][2], Word16 *resq, Word16 *lsfq );
31 :
32 : static Word32 vq_lvq_lsf_enc_ivas_fx( Word16 pred_flag, Word16 mode, Word16 u[], Word16 *levels, Word16 stages, Word16 w[], Word16 Idx[], const Word16 *lsf, const Word16 *pred, Word16 *resq, Word16 *lsfq );
33 :
34 : static void lsf_mid_enc_fx( BSTR_ENC_HANDLE hBstr, Word16 nb_bits, const Word16 int_fs, const Word16 qlsp0[], const Word16 qlsp1[], Word16 lsp[], const Word16 coder_type, const Word16 bwidth, Word32 Bin_Ener_old[], Word32 Bin_Ener[], Word16 Q_ener, Word16 ppp_mode, Word16 nelp_mode );
35 :
36 : static void lsf_mid_enc_ivas_fx( BSTR_ENC_HANDLE hBstr, Word16 nb_bits, const Word32 int_fs, const Word16 qlsp0[], const Word16 qlsp1[], Word16 lsp[], const Word16 coder_type, const Word16 bwidth, Word32 Bin_Ener_old[], Word16 Q_ener, Word16 ppp_mode, Word16 nelp_mode );
37 :
38 : /*===========================================================================*/
39 : /* FUNCTION : lsf_enc_fx() */
40 : /*---------------------------------------------------------------------------*/
41 : /* PURPOSE : Quantization of LSF vector */
42 : /*---------------------------------------------------------------------------*/
43 : /* INPUT ARGUMENTS : */
44 : /* _ (Word16) L_frame : length of the frame */
45 : /* _ (Word16) coder_type : coding type */
46 : /*---------------------------------------------------------------------------*/
47 : /* OUTPUT ARGUMENTS : */
48 : /* _ (Word16*) Aq : LP filter coefficient Q12 */
49 : /* _ (Word16*) lsf_ne w : LP filter coefficient Q(x2.56) */
50 : /* _ (Word16) st_fx->stab_fac_fx : LSF stability factor Q15 */
51 : /*---------------------------------------------------------------------------*/
52 : /* INPUT/OUTPUT ARGUMENTS : */
53 : /* _ (Struct) st_fx : state structure */
54 : /* _ (Word16*) lsp_new : LP filter coefficient Q15 */
55 : /* _ (Word16*) lsp_mid : LP filter coefficient Q15 */
56 : /* _ (Word16[]) st_fx->mem_AR_fx : AR memory of LSF quantizer */
57 : /* (past quantized LSFs without mean) x2.56 */
58 : /* _ (Word16[]) st_fx->clip_var_fx : pitch gain clipping memory x2.56*/
59 : /*---------------------------------------------------------------------------*/
60 :
61 : /*---------------------------------------------------------------------------*/
62 : /* RETURN ARGUMENTS : */
63 : /* _ None */
64 : /*===========================================================================*/
65 1427 : void lsf_enc_fx(
66 : Encoder_State *st_fx, /* i/o: state structure */
67 : Word16 *lsf_new, /* o : quantized LSF vector */
68 : Word16 *lsp_new, /* i/o: LSP vector to quantize/quantized */
69 : Word16 *lsp_mid, /* i/o : mid-frame LSP vector */
70 : Word16 *Aq, /* o : quantized A(z) for 4 subframes */
71 : const Word16 Nb_ACELP_frames,
72 : const Word16 tdm_low_rate_mode, /* i : secondary channel low rate mode flag */
73 : const Word16 GSC_IVAS_mode, /* i : GSC IVAS mode */
74 : const Word16 Q_new )
75 : {
76 1427 : Word16 nBits = 0;
77 1427 : move16();
78 : Word16 int_fs;
79 1427 : Word16 force_sf = 0;
80 1427 : move16();
81 : Word16 fec_lsf[M], stab, i;
82 : Word32 L_tmp;
83 : Word16 coder_type, ppp_mode, nelp_mode;
84 :
85 1427 : test();
86 1427 : IF( EQ_32( st_fx->core_brate, SID_2k40 ) || EQ_32( st_fx->core_brate, SID_1k75 ) )
87 : {
88 0 : coder_type = INACTIVE;
89 0 : move16();
90 : }
91 : ELSE
92 : {
93 1427 : coder_type = st_fx->coder_type;
94 1427 : move16();
95 : }
96 :
97 1427 : test();
98 1427 : if ( EQ_16( coder_type, AUDIO ) && GSC_IVAS_mode > 0 )
99 : {
100 0 : coder_type = GENERIC;
101 0 : move16();
102 : }
103 :
104 1427 : move16();
105 :
106 1427 : IF( st_fx->Opt_SC_VBR )
107 : {
108 0 : ppp_mode = st_fx->hSC_VBR->ppp_mode;
109 0 : nelp_mode = st_fx->hSC_VBR->nelp_mode;
110 0 : move16();
111 0 : move16();
112 : }
113 : ELSE
114 : {
115 1427 : ppp_mode = 0;
116 1427 : nelp_mode = 0;
117 1427 : move16();
118 1427 : move16();
119 : }
120 :
121 : /* initialize */
122 1427 : assert( st_fx->sr_core <= 32000 );
123 1427 : int_fs = extract_l( st_fx->sr_core );
124 1427 : move32();
125 : /* convert LSPs to LSFs */
126 1427 : lsp2lsf_fx( lsp_new, lsf_new, M, int_fs );
127 :
128 : /* check resonance for pitch clipping algorithm */
129 1427 : gp_clip_test_lsf_fx( st_fx->element_mode, lsf_new, st_fx->clip_var_fx, 0 );
130 :
131 : /* Find the number of bits for LSF quantization */
132 1427 : nBits = 0;
133 1427 : move16();
134 1427 : IF( EQ_32( st_fx->core_brate, SID_2k40 ) )
135 : {
136 0 : nBits = LSF_BITS_CNG;
137 0 : move16();
138 : }
139 : ELSE
140 : {
141 1427 : test();
142 1427 : IF( ( nelp_mode == 0 ) && ( ppp_mode == 0 ) )
143 : {
144 1427 : nBits = st_fx->acelp_cfg.lsf_bits;
145 1427 : move16();
146 : }
147 0 : ELSE IF( EQ_16( nelp_mode, 1 ) )
148 : {
149 0 : nBits = 30;
150 0 : move16();
151 :
152 0 : if ( EQ_16( st_fx->bwidth, NB ) )
153 : {
154 0 : nBits = 32;
155 0 : move16();
156 : }
157 : }
158 0 : ELSE IF( EQ_16( ppp_mode, 1 ) )
159 : {
160 0 : nBits = 26;
161 0 : move16();
162 : }
163 : }
164 1427 : force_sf = 0;
165 1427 : move16();
166 : /* first three ACELP frames after an HQ frame shall be processed only with safety-net quantizer */
167 1427 : test();
168 1427 : if ( LT_16( Nb_ACELP_frames, 3 ) && NE_32( st_fx->core_brate, SID_2k40 ) )
169 : {
170 60 : force_sf = 1;
171 60 : move16();
172 : }
173 :
174 : /* in case of unstable filter in decoder FEC, choose safety-net to help FEC */
175 1427 : IF( EQ_16( st_fx->next_force_safety_net, 1 ) )
176 : {
177 123 : force_sf = 1;
178 123 : move16();
179 123 : st_fx->next_force_safety_net = 0;
180 123 : move16();
181 : }
182 :
183 : /*-------------------------------------------------------------------------------------*
184 : * Frame end LSF quantization
185 : *-------------------------------------------------------------------------------------*/
186 :
187 1427 : lsf_end_enc_fx( st_fx, lsf_new, lsf_new, nBits, coder_type, Q_new + QSCALE - 2,
188 1427 : force_sf, NULL, NULL, NULL, st_fx->coder_type_raw );
189 :
190 : /* convert quantized LSFs back to LSPs */
191 1427 : lsf2lsp_fx( lsf_new, lsp_new, M, int_fs );
192 :
193 1427 : test();
194 1427 : IF( EQ_16( st_fx->last_core, HQ_CORE ) && ( st_fx->core == ACELP_CORE ) )
195 : {
196 : /* don't use old LSF values if this is the first ACELP frame after HQ frames */
197 29 : Copy( lsf_new, st_fx->lsf_old_fx, M );
198 : }
199 : /* set seed_acelp used in UC mode */
200 1427 : IF( EQ_32( st_fx->core_brate, SID_2k40 ) )
201 : {
202 : /* return if SID frame (conversion to A(z) done in the calling function) */
203 0 : return;
204 : }
205 :
206 : /*-------------------------------------------------------------------------------------*
207 : * FEC - enforce safety-net in the next frame in case of unstable filter
208 : *-------------------------------------------------------------------------------------*/
209 :
210 1427 : IF( NE_16( st_fx->last_L_frame, st_fx->L_frame ) )
211 : {
212 : /* FEC - in case of core switching, use old LSFs */
213 0 : Copy( st_fx->lsf_old_fx, st_fx->lsfoldbfi1_fx, M );
214 0 : Copy( st_fx->lsf_old_fx, st_fx->lsfoldbfi0_fx, M );
215 0 : Copy( st_fx->lsf_old_fx, st_fx->lsf_adaptive_mean_fx, M );
216 : }
217 :
218 1427 : FEC_lsf_estim_enc_fx( st_fx, fec_lsf );
219 :
220 : /* in case of FEC in decoder - calculate LSF stability */
221 1427 : stab = lsf_stab_fx( lsf_new, fec_lsf, 0, st_fx->L_frame ); // Q15
222 :
223 1427 : test();
224 1427 : test();
225 1427 : test();
226 : /* If decoder FEC frame may be unstable force safety-net usage */
227 1427 : IF( ( EQ_16( st_fx->L_frame, L_FRAME16k ) ) && ( LT_16( stab, STAB_FAC_LIMIT_FX ) ) && ( EQ_16( coder_type, GENERIC ) ) )
228 : {
229 90 : st_fx->next_force_safety_net = 1;
230 90 : move16();
231 : }
232 1337 : ELSE IF( ( LT_16( stab, STAB_FAC_LIMIT_FX ) ) && ( EQ_16( st_fx->clas, VOICED_CLAS ) || ( LT_16( st_fx->clas, VOICED_CLAS ) && EQ_16( coder_type, AUDIO ) ) ) )
233 : {
234 34 : st_fx->next_force_safety_net = 1;
235 34 : move16();
236 : }
237 :
238 :
239 : /* FEC - update adaptive LSF mean vector */
240 24259 : FOR( i = 0; i < M; i++ )
241 : {
242 22832 : L_tmp = L_mult( lsf_new[i], 10922 ); /*Q(x2.56+16)*/
243 22832 : L_tmp = L_mac( L_tmp, st_fx->lsfoldbfi1_fx[i], 10922 ); /*Q(x2.56+16)*/
244 22832 : L_tmp = L_mac( L_tmp, st_fx->lsfoldbfi0_fx[i], 10922 ); /*Q(x2.56+16)*/
245 22832 : st_fx->lsf_adaptive_mean_fx[i] = round_fx( L_tmp ); /*Q(x2.56)*/
246 : }
247 :
248 : /* FEC - update LSF memories */
249 1427 : Copy( st_fx->lsfoldbfi0_fx, st_fx->lsfoldbfi1_fx, M );
250 1427 : Copy( lsf_new, st_fx->lsfoldbfi0_fx, M );
251 :
252 :
253 : /*-------------------------------------------------------------------------------------*
254 : * Mid-frame LSF encoding
255 : * LSP interpolation and conversion of LSPs to A(z)
256 : *-------------------------------------------------------------------------------------*/
257 1427 : if ( st_fx->rate_switching_reset )
258 : {
259 : /*extrapolation in case of unstable LSF convert*/
260 0 : Copy( lsp_new, st_fx->lsp_old_fx, M ); // Q15
261 0 : Copy( lsf_new, st_fx->lsf_old_fx, M ); // Q15
262 : }
263 : /* Mid-frame LSF encoding */
264 1427 : lsf_mid_enc_fx( st_fx->hBstr, st_fx->acelp_cfg.mid_lsf_bits, int_fs, st_fx->lsp_old_fx, lsp_new, lsp_mid, coder_type, st_fx->bwidth, st_fx->Bin_E_old_fx, st_fx->Bin_E_fx, Q_new + QSCALE - 2, ppp_mode, nelp_mode );
265 :
266 1427 : test();
267 1427 : IF( EQ_16( st_fx->last_core, HQ_CORE ) && EQ_16( st_fx->core, ACELP_CORE ) )
268 : {
269 : /* don't use old LSP/LSF values if this is the first ACELP frame after HQ frames */
270 29 : Copy( lsp_mid, st_fx->lsp_old_fx, M ); // Q15
271 29 : lsp2lsf_fx( lsp_mid, st_fx->lsf_old_fx, M, int_fs );
272 : }
273 :
274 : /* LSP interpolation and conversion of LSPs to A(z) */
275 1427 : test();
276 1427 : IF( EQ_16( tdm_low_rate_mode, 1 ) && GT_16( coder_type, UNVOICED ) )
277 : {
278 0 : IF( EQ_16( st_fx->active_cnt, 1 ) )
279 : {
280 0 : Copy( lsp_mid, st_fx->lsp_old_fx, M );
281 0 : lsp2lsf_fx( lsp_mid, st_fx->lsf_old_fx, M, int_fs );
282 0 : Copy( lsp_new, lsp_mid, M );
283 : }
284 :
285 : /* LSP interpolation and conversion of LSPs to A(z) - two-subframe mode */
286 0 : int_lsp4_fx( st_fx->L_frame, st_fx->lsp_old_fx, lsp_mid, lsp_new, Aq, M, -2 );
287 : }
288 : ELSE
289 : {
290 1427 : int_lsp4_fx( st_fx->L_frame, st_fx->lsp_old_fx, lsp_mid, lsp_new, Aq, M, 0 );
291 : }
292 : /*------------------------------------------------------------------*
293 : * Check LSF stability (distance between old LSFs and current LSFs)
294 : *------------------------------------------------------------------*/
295 1427 : IF( NE_32( st_fx->core_brate, SID_2k40 ) )
296 : {
297 1427 : st_fx->stab_fac_fx = lsf_stab_fx( lsf_new, st_fx->lsf_old_fx, 0, st_fx->L_frame );
298 : }
299 1427 : return;
300 : }
301 :
302 156234 : void lsf_enc_ivas_fx(
303 : Encoder_State *st, /* i/o: state structure */
304 : Word16 *lsf_new, /* o : quantized LSF vector Q(x2.56)*/
305 : Word16 *lsp_new, /* i/o: LSP vector to quantize/quantized Q15*/
306 : Word16 *lsp_mid, /* i/o : mid-frame LSP vector Q15*/
307 : Word16 *Aq, /* o : quantized A(z) for 4 subframes Q12*/
308 : const Word16 tdm_low_rate_mode, /* i : secondary channel low rate mode flag */
309 : const Word16 GSC_IVAS_mode, /* i : GSC IVAS mode */
310 : const Word16 tdm_lsfQ_PCh[M], /* i : Q LSFs for primary channel */
311 : const Word16 Q_new )
312 : {
313 : Word16 nBits;
314 : Word16 force_sf;
315 : Word16 fec_lsf[M], stab, i;
316 : Word16 no_param_lpc;
317 :
318 : Word16 param_lpc[NPRM_LPC_NEW];
319 : Word32 L_tmp;
320 : Word16 coder_type, ppp_mode, nelp_mode;
321 :
322 156234 : nBits = 0;
323 156234 : force_sf = 0;
324 156234 : move16();
325 156234 : move16();
326 :
327 156234 : test();
328 156234 : IF( EQ_32( st->core_brate, SID_2k40 ) || EQ_32( st->core_brate, SID_1k75 ) )
329 : {
330 448 : coder_type = INACTIVE;
331 448 : move16();
332 : }
333 : ELSE
334 : {
335 155786 : coder_type = st->coder_type;
336 155786 : move16();
337 : }
338 :
339 156234 : test();
340 156234 : if ( EQ_16( coder_type, AUDIO ) && GSC_IVAS_mode > 0 )
341 : {
342 4718 : coder_type = GENERIC;
343 4718 : move16();
344 : }
345 :
346 156234 : no_param_lpc = 0;
347 156234 : move16();
348 :
349 156234 : IF( st->Opt_SC_VBR )
350 : {
351 0 : ppp_mode = st->hSC_VBR->ppp_mode;
352 0 : nelp_mode = st->hSC_VBR->nelp_mode;
353 0 : move16();
354 0 : move16();
355 : }
356 : ELSE
357 : {
358 156234 : ppp_mode = 0;
359 156234 : nelp_mode = 0;
360 156234 : move16();
361 156234 : move16();
362 : }
363 :
364 : /* convert LSPs to LSFs */
365 156234 : lsp2lsf_fx( lsp_new, lsf_new, M, st->sr_core );
366 :
367 : /* check resonance for pitch clipping algorithm */
368 156234 : gp_clip_test_lsf_ivas_fx( st->element_mode, st->core_brate, lsf_new, st->clip_var_fx, 0 );
369 :
370 : /* Find the number of bits for LSF quantization */
371 156234 : nBits = 0;
372 156234 : move16();
373 156234 : IF( EQ_32( st->core_brate, SID_2k40 ) )
374 : {
375 448 : nBits = LSF_BITS_CNG;
376 448 : move16();
377 : }
378 : ELSE
379 : {
380 155786 : test();
381 155786 : IF( ( nelp_mode == 0 ) && ( ppp_mode == 0 ) )
382 : {
383 155786 : nBits = st->acelp_cfg.lsf_bits;
384 155786 : move16();
385 : }
386 0 : ELSE IF( EQ_16( nelp_mode, 1 ) )
387 : {
388 0 : IF( st->bwidth == NB )
389 : {
390 0 : nBits = 32;
391 0 : move16();
392 : }
393 0 : ELSE IF( EQ_16( st->bwidth, WB ) )
394 : {
395 0 : nBits = 30;
396 0 : move16();
397 : }
398 : }
399 0 : ELSE IF( EQ_16( ppp_mode, 1 ) )
400 : {
401 0 : nBits = 26;
402 0 : move16();
403 : }
404 : }
405 156234 : force_sf = 0;
406 156234 : move16();
407 : /* first three ACELP frames after an HQ frame shall be processed only with safety-net quantizer */
408 156234 : test();
409 156234 : if ( LT_16( st->Nb_ACELP_frames, 3 ) && NE_32( st->core_brate, SID_2k40 ) )
410 : {
411 18542 : force_sf = 1;
412 18542 : move16();
413 : }
414 :
415 : /* in case of unstable filter in decoder FEC, choose safety-net to help FEC */
416 156234 : IF( EQ_16( st->next_force_safety_net, 1 ) )
417 : {
418 13740 : force_sf = 1;
419 13740 : move16();
420 13740 : st->next_force_safety_net = 0;
421 13740 : move16();
422 : }
423 :
424 : /*-------------------------------------------------------------------------------------*
425 : * Frame end LSF quantization
426 : *-------------------------------------------------------------------------------------*/
427 156234 : lsf_end_enc_ivas_fx( st, lsf_new, lsf_new, nBits, coder_type, Q_new + QSCALE - 2,
428 156234 : force_sf, param_lpc, &no_param_lpc, NULL, st->coder_type_raw, tdm_lsfQ_PCh );
429 :
430 : /* convert quantized LSFs back to LSPs */
431 156234 : lsf2lsp_fx( lsf_new, lsp_new, M, st->sr_core );
432 :
433 156234 : test();
434 156234 : IF( EQ_16( st->last_core, HQ_CORE ) && ( st->core == ACELP_CORE ) )
435 : {
436 : /* don't use old LSF values if this is the first ACELP frame after HQ frames */
437 221 : Copy( lsf_new, st->lsf_old_fx, M );
438 : }
439 : /* set seed_acelp used in UC mode */
440 :
441 156234 : test();
442 156234 : IF( EQ_16( coder_type, UNVOICED ) && ( st->element_mode > EVS_MONO ) )
443 : {
444 2019 : st->seed_acelp = 0;
445 2019 : move16();
446 2019 : FOR( i = no_param_lpc - 1; i >= 0; i-- )
447 : {
448 : /* rightshift before *seed_acelp+param_lpc[i] to avoid overflows*/
449 0 : st->seed_acelp = add( i_mult( add( shr( st->seed_acelp, 1 ), param_lpc[i] ), 31821 ), 13849 );
450 0 : move16();
451 : }
452 : }
453 :
454 156234 : IF( EQ_32( st->core_brate, SID_2k40 ) )
455 : {
456 : /* return if SID frame (conversion to A(z) done in the calling function) */
457 448 : return;
458 : }
459 :
460 : /*-------------------------------------------------------------------------------------*
461 : * FEC - enforce safety-net in the next frame in case of unstable filter
462 : *-------------------------------------------------------------------------------------*/
463 :
464 155786 : IF( NE_16( st->last_L_frame, st->L_frame ) )
465 : {
466 : /* FEC - in case of core switching, use old LSFs */
467 1682 : Copy( st->lsf_old_fx, st->lsfoldbfi1_fx, M ); // Q15
468 1682 : Copy( st->lsf_old_fx, st->lsfoldbfi0_fx, M ); // Q15
469 1682 : Copy( st->lsf_old_fx, st->lsf_adaptive_mean_fx, M ); // Q15
470 : }
471 :
472 155786 : FEC_lsf_estim_enc_fx( st, fec_lsf );
473 :
474 : /* in case of FEC in decoder - calculate LSF stability */
475 155786 : stab = lsf_stab_ivas_fx( lsf_new, fec_lsf, 0, st->L_frame ); // Q15
476 :
477 155786 : test();
478 155786 : test();
479 155786 : test();
480 : /* If decoder FEC frame may be unstable force safety-net usage */
481 155786 : IF( ( EQ_16( st->L_frame, L_FRAME16k ) ) && ( LT_16( stab, STAB_FAC_LIMIT_FX ) ) && ( EQ_16( coder_type, GENERIC ) ) )
482 : {
483 9460 : st->next_force_safety_net = 1;
484 9460 : move16();
485 : }
486 146326 : ELSE IF( ( LT_16( stab, STAB_FAC_LIMIT_FX ) ) && ( EQ_16( st->clas, VOICED_CLAS ) || ( LT_16( st->clas, VOICED_CLAS ) && EQ_16( coder_type, AUDIO ) ) ) )
487 : {
488 4583 : st->next_force_safety_net = 1;
489 4583 : move16();
490 : }
491 :
492 :
493 : /* FEC - update adaptive LSF mean vector */
494 2648362 : FOR( i = 0; i < M; i++ )
495 : {
496 2492576 : L_tmp = L_mult( lsf_new[i], 10922 ); /*Q(x2.56+16)*/
497 2492576 : L_tmp = L_mac( L_tmp, st->lsfoldbfi1_fx[i], 10922 ); /*Q(x2.56+16)*/
498 2492576 : L_tmp = L_mac( L_tmp, st->lsfoldbfi0_fx[i], 10922 ); /*Q(x2.56+16)*/
499 2492576 : st->lsf_adaptive_mean_fx[i] = extract_h( L_tmp ); /*Q(x2.56)*/
500 : }
501 :
502 : /* FEC - update LSF memories */
503 155786 : Copy( st->lsfoldbfi0_fx, st->lsfoldbfi1_fx, M );
504 155786 : Copy( lsf_new, st->lsfoldbfi0_fx, M );
505 :
506 :
507 : /*-------------------------------------------------------------------------------------*
508 : * Mid-frame LSF encoding
509 : * LSP interpolation and conversion of LSPs to A(z)
510 : *-------------------------------------------------------------------------------------*/
511 155786 : IF( st->rate_switching_reset )
512 : {
513 : /*extrapolation in case of unstable LSF convert*/
514 0 : Copy( lsp_new, st->lsp_old_fx, M ); // Q15
515 0 : Copy( lsf_new, st->lsf_old_fx, M ); // Q15
516 : }
517 : /* Mid-frame LSF encoding */
518 155786 : lsf_mid_enc_ivas_fx( st->hBstr, st->acelp_cfg.mid_lsf_bits, st->sr_core, st->lsp_old_fx, lsp_new, lsp_mid, coder_type, st->bwidth, st->Bin_E_old_fx, Q_new + QSCALE - 2, ppp_mode, nelp_mode );
519 :
520 155786 : test();
521 155786 : IF( EQ_16( st->last_core, HQ_CORE ) && EQ_16( st->core, ACELP_CORE ) )
522 : {
523 : /* don't use old LSP/LSF values if this is the first ACELP frame after HQ frames */
524 221 : Copy( lsp_mid, st->lsp_old_fx, M );
525 221 : lsp2lsf_fx( lsp_mid, st->lsf_old_fx, M, st->sr_core );
526 : }
527 :
528 : /* LSP interpolation and conversion of LSPs to A(z) */
529 155786 : test();
530 155786 : IF( EQ_16( tdm_low_rate_mode, 1 ) && GT_16( coder_type, UNVOICED ) )
531 : {
532 5 : IF( EQ_16( st->active_cnt, 1 ) )
533 : {
534 0 : Copy( lsp_mid, st->lsp_old_fx, M ); // Q15
535 0 : lsp2lsf_fx( lsp_mid, st->lsf_old_fx, M, st->sr_core );
536 0 : Copy( lsp_new, lsp_mid, M ); // Q15
537 : }
538 :
539 : /* LSP interpolation and conversion of LSPs to A(z) - two-subframe mode */
540 5 : int_lsp4_ivas_fx( st->L_frame, st->lsp_old_fx, lsp_mid, lsp_new, Aq, M, -2 );
541 : }
542 : ELSE
543 : {
544 155781 : int_lsp4_ivas_fx( st->L_frame, st->lsp_old_fx, lsp_mid, lsp_new, Aq, M, 0 );
545 : }
546 : /*------------------------------------------------------------------*
547 : * Check LSF stability (distance between old LSFs and current LSFs)
548 : *------------------------------------------------------------------*/
549 155786 : IF( NE_32( st->core_brate, SID_2k40 ) )
550 : {
551 155786 : st->stab_fac_fx = lsf_stab_ivas_fx( lsf_new, st->lsf_old_fx, 0, st->L_frame ); // Q15
552 : }
553 155786 : return;
554 : }
555 : /*-------------------------------------------------------------------*
556 : * lsfq_CNG_fx()
557 : *
558 : * LSF quantizer for SID frames (uses 29 bits, 4 for VQ, 25 for LVQ)
559 : * Note:
560 : * The sampling frequency of the LP-CNG frame can be determined by checking the value of the highest order LSF
561 : * coefficient (last coefficient of lsf). If the last LSF coefficient (lsf[M-1]) is larger than 6350
562 : * the decoded frame is WB2 with sampling rate of 16 kHz, otherwise it is sampled at 12.8kHz and contains
563 : * either NB or WB LSF data.
564 : *-------------------------------------------------------------------*/
565 :
566 0 : static void lsfq_CNG_fx(
567 : BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle */
568 : const Word16 *lsf, /*x2.56 unquantized LSF vector */
569 : const Word16 *wghts, /*Q10 LSF weights */
570 : Word16 *qlsf, /*x2.56 quantized LSF vecotor */
571 : Word32 *p_offset_scale1,
572 : Word32 *p_offset_scale2,
573 : Word16 *p_no_scales )
574 : {
575 : Word16 i, j, idx_cv, idx_lvq[3];
576 : Word32 min_dist, dist;
577 : Word16 dd[M], ddq[M];
578 : const Word16 *p_cb;
579 : Word16 first_cb, last_cb;
580 : Word16 idx_lead_cng[2], idx_scale_cng[2];
581 : Word16 tmp;
582 :
583 0 : idx_cv = 0;
584 0 : move16();
585 :
586 : /* quantize first stage with 4 bits
587 : The sampling frequency of the LP-CNG frame can be determined by checking the value of the highest order LSF
588 : coefficient (last coefficient of lsf). If the last LSF coefficient (lsf[M-1]) is larger than 6350
589 : the decoded frame is WB2 with sampling rate of 16 kHz, otherwise it is sampled at 12.8kHz and contains
590 : either NB or WB LSF data. */
591 0 : IF( GT_16( lsf[M - 1], WB_LIMIT_LSF_FX ) ) /* 16kHz sampled LSF vector*/
592 : {
593 0 : p_cb = &CNG_SN1_fx[0];
594 0 : move16();
595 0 : first_cb = 0;
596 0 : move16();
597 0 : last_cb = 6;
598 0 : move16();
599 : }
600 : ELSE /* 12.8kHz sampled LSF vector*/
601 : {
602 0 : p_cb = &CNG_SN1_fx[6 * M];
603 0 : move16();
604 0 : first_cb = 6;
605 0 : move16();
606 0 : last_cb = M;
607 0 : move16();
608 : }
609 :
610 :
611 0 : min_dist = L_add( MAXINT32, 0 );
612 0 : FOR( i = first_cb; i < last_cb; i++ )
613 : {
614 0 : tmp = sub( *p_cb, shl( lsf[0], 1 ) ); /*x2.56 */
615 0 : dist = Mult_32_16( L_mult0( wghts[0], *p_cb ), tmp ); /*Q8 + x2.56 -Q15 + x2.56 = Q-7 + x2.56+x.256 */
616 0 : p_cb++;
617 0 : FOR( j = 1; j < M; j++ )
618 : {
619 0 : tmp = sub( *p_cb, lsf[j] );
620 0 : tmp = sub( tmp, lsf[j] );
621 :
622 0 : dist = L_add( dist, Mult_32_16( L_mult0( wghts[j], *p_cb ), tmp ) );
623 0 : p_cb++;
624 : }
625 0 : IF( LT_32( dist, min_dist ) )
626 : {
627 0 : min_dist = dist;
628 0 : move16(); /*Q-4 */
629 0 : idx_cv = i;
630 0 : move16();
631 : }
632 : }
633 :
634 : /* calculate difference */
635 0 : FOR( i = 0; i < M; i++ )
636 : {
637 0 : dd[i] = sub( lsf[i], CNG_SN1_fx[idx_cv * M + i] ); /*x2.56 */
638 0 : move16();
639 : }
640 :
641 : /* quantize the difference with LVQ */
642 : /* MSVQ_ROM to be updated */
643 0 : mslvq_cng_fx( idx_cv, dd, qlsf, ddq, idx_lead_cng, idx_scale_cng, wghts, p_no_scales );
644 :
645 0 : index_lvq_fx( ddq, idx_lead_cng, idx_scale_cng, START_CNG + idx_cv, idx_lvq,
646 : p_offset_scale1, p_offset_scale2, p_no_scales );
647 0 : Vr_add( qlsf, &CNG_SN1_fx[idx_cv * M], qlsf, M );
648 :
649 : /* write the VQ index to the bitstream */
650 0 : push_indice( hBstr, IND_ISF_0_0, idx_cv, 4 );
651 :
652 : /* write the LVQ index to the bitstream */
653 0 : push_indice( hBstr, IND_ISF_0_1, idx_lvq[0], LEN_INDICE );
654 0 : push_indice( hBstr, IND_ISF_0_1, idx_lvq[1], LSF_BITS_CNG - 4 - LEN_INDICE );
655 :
656 0 : return;
657 : }
658 :
659 448 : static void lsfq_CNG_ivas_fx(
660 : BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle */
661 : const Word16 *lsf, /*x2.56 unquantized LSF vector */
662 : const Word16 *wghts, /*Q10 LSF weights */
663 : Word16 *qlsf /*x2.56 quantized LSF vecotor */
664 : )
665 : {
666 : Word16 i, j, idx_cv, idx_lvq[3];
667 : Word32 min_dist, dist;
668 : Word16 dd[M], ddq[M];
669 : const Word16 *p_cb;
670 : Word16 first_cb, last_cb;
671 : Word16 idx_lead_cng[2], idx_scale_cng[2];
672 : Word16 tmp;
673 :
674 448 : idx_cv = 0;
675 448 : move16();
676 :
677 : /* quantize first stage with 4 bits
678 : The sampling frequency of the LP-CNG frame can be determined by checking the value of the highest order LSF
679 : coefficient (last coefficient of lsf). If the last LSF coefficient (lsf[M-1]) is larger than 6350
680 : the decoded frame is WB2 with sampling rate of 16 kHz, otherwise it is sampled at 12.8kHz and contains
681 : either NB or WB LSF data. */
682 448 : IF( GT_16( lsf[M - 1], WB_LIMIT_LSF_FX ) ) /* 16kHz sampled LSF vector*/
683 : {
684 278 : p_cb = &CNG_SN1_fx[0];
685 278 : move16();
686 278 : first_cb = 0;
687 278 : move16();
688 278 : last_cb = 6;
689 278 : move16();
690 : }
691 : ELSE /* 12.8kHz sampled LSF vector*/
692 : {
693 170 : p_cb = &CNG_SN1_fx[6 * M];
694 170 : move16();
695 170 : first_cb = 6;
696 170 : move16();
697 170 : last_cb = M;
698 170 : move16();
699 : }
700 :
701 :
702 448 : min_dist = L_add( MAXINT32, 0 );
703 3816 : FOR( i = first_cb; i < last_cb; i++ )
704 : {
705 3368 : tmp = sub( *p_cb, shl( lsf[0], 1 ) ); /*x2.56 */
706 3368 : dist = Mult_32_16( L_mult0( wghts[0], *p_cb ), tmp ); /*Q8 + x2.56 -Q15 + x2.56 = Q-7 + x2.56+x.256 */
707 3368 : p_cb++;
708 53888 : FOR( j = 1; j < M; j++ )
709 : {
710 50520 : tmp = sub( *p_cb, lsf[j] );
711 50520 : tmp = sub( tmp, lsf[j] );
712 :
713 50520 : dist = L_add( dist, Mult_32_16( L_mult0( wghts[j], *p_cb ), tmp ) );
714 50520 : p_cb++;
715 : }
716 3368 : IF( LT_32( dist, min_dist ) )
717 : {
718 1179 : min_dist = dist;
719 1179 : move16(); /*Q-4 */
720 1179 : idx_cv = i;
721 1179 : move16();
722 : }
723 : }
724 :
725 : /* calculate difference */
726 7616 : FOR( i = 0; i < M; i++ )
727 : {
728 7168 : dd[i] = sub( lsf[i], CNG_SN1_fx[idx_cv * M + i] ); /*x2.56 */
729 7168 : move16();
730 : }
731 :
732 : /* quantize the difference with LVQ */
733 : /* MSVQ_ROM to be updated */
734 448 : mslvq_cng_ivas_fx( idx_cv, dd, qlsf, ddq, idx_lead_cng, idx_scale_cng, wghts );
735 :
736 448 : index_lvq_ivas_fx( ddq, idx_lead_cng, idx_scale_cng, START_CNG_IVAS + idx_cv, idx_lvq, 0 );
737 448 : Vr_add( qlsf, &CNG_SN1_fx[idx_cv * M], qlsf, M );
738 :
739 : /* write the VQ index to the bitstream */
740 448 : push_indice( hBstr, IND_ISF_0_0, idx_cv, 4 );
741 :
742 : /* write the LVQ index to the bitstream */
743 448 : push_indice( hBstr, IND_ISF_0_1, idx_lvq[0], LEN_INDICE );
744 448 : push_indice( hBstr, IND_ISF_0_1, idx_lvq[1], LSF_BITS_CNG - 4 - LEN_INDICE );
745 :
746 448 : return;
747 : }
748 : /*-------------------------------------------------------------------*
749 : * qlsf_Mode_Select_fx()
750 : *
751 : * Mode selection for LSF quantizer
752 : *-------------------------------------------------------------------*/
753 :
754 :
755 625 : static Word16 qlsf_Mode_Select_fx(
756 : const Word16 *w, /* i : weighting vector Q8 */
757 : const Word16 *pred1, /* i : prediction vector x2.56 */
758 : const Word16 streaklimit, /* i : predictive streak limit Q15 */
759 : const Word32 op_loop_thr /* i : Open-loop Threshold */
760 : )
761 : {
762 : Word16 pred_pow2[M];
763 625 : Word32 temp32, En = 0;
764 : Word16 safety_net;
765 : Word16 i, cs, cl;
766 : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
767 625 : Flag Overflow = 0;
768 : #endif
769 :
770 : /* calculate the prediction residual */
771 625 : cl = 0;
772 625 : move16();
773 10625 : FOR( i = 0; i < M; i++ )
774 : {
775 10000 : cl = s_max( cl, abs_s( pred1[i] ) );
776 : }
777 625 : cs = norm_s( cl );
778 625 : En = 0;
779 625 : move16();
780 10625 : FOR( i = 0; i < M; i++ )
781 : {
782 10000 : pred_pow2[i] = shl( pred1[i], cs );
783 10000 : move16();
784 10000 : En = L_mac_o( En, mult( pred_pow2[i], shl_o( w[i], 2, &Overflow ) ), pred_pow2[i], &Overflow ); /* 2.56*2.56 at Q-4 */
785 : }
786 :
787 625 : cs = shl( cs, 1 );
788 625 : En = L_shr( En, cs );
789 625 : temp32 = Mult_32_16( op_loop_thr, streaklimit );
790 :
791 : /* choose the mode */
792 625 : IF( GT_32( En, temp32 ) )
793 : {
794 : /* Safety-net */
795 54 : safety_net = 1;
796 54 : move16();
797 : }
798 : ELSE
799 : {
800 : /* Predictive */
801 571 : safety_net = 0;
802 571 : move16();
803 : }
804 625 : return safety_net;
805 : }
806 :
807 :
808 : /*========================================================================*/
809 : /* FUNCTION : lsf_end_enc_fx() */
810 : /*------------------------------------------------------------------------*/
811 : /* PURPOSE : Quantization of LSF parameters */
812 : /*------------------------------------------------------------------------*/
813 : /* INPUT ARGUMENTS : */
814 : /* _ (Word16*) lsf : LSF in the frequency domain (0..6400) x2.56 */
815 : /* _ (Word16) coder_type : coding type */
816 : /* _ (Word16) bwidth : input signal bandwidth */
817 : /* _ (Word16) nBits : number of bits used for ISF quantization */
818 : /* _ (Word16*) stable_isp : most recent stable ISP (can be */
819 : /* removed after passing to LSF) Q15 */
820 : /* _ (Word16*) stable_lsp : most recent stable LSP Q15 */
821 : /* _ (Word32*) grid : Table of 100 grid points for evaluating */
822 : /* Chebyshev polynomials Q31 */
823 : /* _ (Word16) int_fs : sampling frequency */
824 : /* _ (Word32) core_brate : Coding Bit Rate */
825 : /* _ (Word16) force_sf : Force safety-net usage if possible */
826 : /* _ (Word32*) Bin_Ener : FFT Bin energy 128 *2 sets Q_ener */
827 : /* _ (Word16) Q_ener : Q format of Bin_Ener */
828 : /* _ (Word32*) offset_scale1: offsets for LSF LVQ structure 1st */
829 : /* 8-dim subvector Q0 */
830 : /* _ (Word32*) offset_scale2: offsets for LSF LVQ structure 2nd */
831 : /* 8-dim subvector Q0 */
832 : /* _ (Word32*) offset_scale1_p: offsets for LSF LVQ structure, pred. */
833 : /* case, 1st 8-dim subvector Q0 */
834 : /* _ (Word32*) offset_scale2_p: offsets for LSF LVQ structure, */
835 : /* pred. case, 2nd 8-dim subvector Q0 */
836 : /* _ (Word16*) no_scales : LSF LVQ structure Q0 */
837 : /* _ (Word16*) no_scales_p : LSF LVQ structure Q0 */
838 : /*------------------------------------------------------------------------*/
839 : /* INPUT/OUTPUT ARGUMENTS : */
840 : /* _ (Word16*) mem_AR : quantizer memory for AR model x2.56 */
841 : /*------------------------------------------------------------------------*/
842 : /* OUTPUT ARGUMENTS : */
843 : /* _ (Word16*) qlsf : quantized LSFs in the cosine domain x2.56 */
844 : /*------------------------------------------------------------------------*/
845 :
846 : /*------------------------------------------------------------------------*/
847 : /* RETURN ARGUMENTS : */
848 : /* _ None */
849 : /*========================================================================*/
850 2693 : void lsf_end_enc_fx(
851 : Encoder_State *st, /* i/o: encoder state structure */
852 : const Word16 *lsf, /* i : LSF in the frequency domain (0..6400) x2.56*/
853 : Word16 *qlsf, /* o : quantized LSF x2.56*/
854 : const Word16 nBits_in, /* i : number of bits to spend on ISF quantization */
855 : const Word16 coder_type_org, /* i : coding type */
856 : Word16 Q_ener, /* i : Q valuen for Bin_Ener */
857 : Word16 force_sf, /* i : Force safety-net usage if coding type supports */
858 : Word16 *lpc_param,
859 : Word16 *no_indices,
860 : Word16 *bits_param_lpc,
861 : Word16 coder_type_raw /* i : Coder type (LSF coder_type have some special cases)*/
862 : )
863 : {
864 : Word16 i;
865 : Word16 Idx0[MAX_VQ_STAGES + 3]; /* Optimal codebook indices for safety-net quantizer */
866 : Word16 Idx1[MAX_VQ_STAGES + 3]; /* Optimal codebook indices for predictive quantizer */
867 : Word16 indice[MAX_VQ_STAGES + 3]; /* Temp. array of indice for vector de-quantizer */
868 2693 : Word16 mode_lvq = 0, mode_lvq_p = 0; /* LVQ mode and predictive LVQ mode */
869 : Word16 bits0[MAX_VQ_STAGES], bits1[MAX_VQ_STAGES];
870 2693 : const Word16 *Bit_alloc1 = NULL;
871 : Word32 Err[2]; /* Quantization error for safety-net(0) and predictive(1) quantizers */
872 : Word16 Tmp[M]; /* Temporary target vector (mean and prediction removed) */
873 : Word16 pred0[M]; /* Prediction for the safety-net quantizer (usually mean) */
874 : Word16 pred1[M]; /* Prediction for the predictive quantizer */
875 : Word16 pred2[M]; /* Prediction for the predictive quantizer */
876 : Word16 wghts[M]; /* Weighting used for quantizer (currently GSM based) */
877 : Word16 stages0; /* Amount of stages used by safety-net quantizer */
878 : Word16 stages1; /* Amount of stages used by predictive quantizer */
879 : Word16 levels0[MAX_VQ_STAGES]; /* Sizes of different codebook stages for safety-net quantizer */
880 : Word16 levels1[MAX_VQ_STAGES]; /* Sizes of different codebook stages for predictive quantizer */
881 : Word16 predmode; /* 0: safety-net only, 1: predictive only, 2: best of the two */
882 : Word16 safety_net, cumleft, num_bits;
883 : Word16 *Idx, stages, *bits;
884 : Word16 Tmp2[M], Tmp1[M];
885 : Word32 abs_threshold; /* Absolute threshold depending on signal bandwidth, that indicates
886 : very good perceptual LSF quantization performance */
887 : Word16 lsfq[M * 2], resq[M * 2];
888 : Word16 coder_type; /* coder type (from LSF quantizer point of view) */
889 : Word16 nBits; /* Number of bits */
890 : Word16 TCQIdx0[M + 2]; /* Optimal codebook indices for VQ-TCQ quantizer */
891 : Word16 *TCQIdx;
892 : Word16 tmp;
893 : Word16 flag_1bit_gran;
894 2693 : BSTR_ENC_HANDLE hBstr = st->hBstr;
895 :
896 2693 : flag_1bit_gran = (Word16) GT_16( st->element_mode, EVS_MONO );
897 :
898 2693 : nBits = nBits_in;
899 2693 : move16();
900 : /* Update LSF coder_type for LSF quantizer for some special cases */
901 2693 : test();
902 2693 : test();
903 2693 : test();
904 2693 : IF( EQ_16( coder_type_org, GENERIC ) && EQ_32( st->sr_core, INT_FS_16k ) && EQ_16( st->codec_mode, MODE1 ) )
905 : {
906 624 : IF( EQ_16( coder_type_raw, VOICED ) )
907 : {
908 359 : coder_type = VOICED;
909 359 : move16(); /* Reflect Inactive mode */
910 359 : if ( EQ_16( flag_1bit_gran, 1 ) )
911 : {
912 0 : nBits = sub( nBits, 1 ); /* This is for real Generic*/
913 : }
914 : }
915 : ELSE
916 : {
917 265 : nBits = sub( nBits, 1 ); /* This is for real Generic*/
918 265 : coder_type = coder_type_org;
919 265 : move16();
920 : }
921 : }
922 : ELSE
923 : {
924 2069 : coder_type = coder_type_org;
925 2069 : move16();
926 : }
927 :
928 : /*----------------------------------------------------------------------------------- -*
929 : * Calculate the number of stages and levels for each stage based on allowed bit budget
930 : * Set absolute threshold for codebook-type decision logic depending on signal bandwidth
931 : *------------------------------------------------------------------------------------ -*/
932 2693 : IF( EQ_16( st->bwidth, NB ) )
933 : {
934 0 : abs_threshold = L_add( SFNETLOWLIMIT_NB, 0 );
935 : }
936 : ELSE
937 : {
938 2693 : abs_threshold = L_add( SFNETLOWLIMIT_WB, 0 );
939 : }
940 : /* Calculate LSF weighting coefficients */
941 2693 : Unified_weighting_fx( &st->Bin_E_fx[L_FFT / 2], Q_ener, lsf, wghts, (Word16) EQ_16( st->bwidth, NB ), (Word16) EQ_16( coder_type, UNVOICED ), st->sr_core, M );
942 :
943 : /*--------------------------------------------------------------------------------*
944 : * LSF quantization of SID frames
945 : *--------------------------------------------------------------------------------*/
946 2693 : IF( EQ_32( st->core_brate, SID_2k40 ) )
947 : {
948 0 : lsfq_CNG_fx( hBstr, lsf, wghts, qlsf, &st->offset_scale1_fx[0][0], &st->offset_scale2_fx[0][0], &st->no_scales_fx[0][0] );
949 0 : sort_fx( qlsf, 0, M - 1 );
950 0 : reorder_lsf_fx( qlsf, MODE1_LSF_GAP_FX, M, st->sr_core );
951 :
952 0 : return;
953 : }
954 : /* Find allowed predictor mode for current coder_type. (SN only (0), SN/AR switched (2) or MA predictive (1) */
955 2693 : find_pred_mode( &predmode, coder_type, st->bwidth, st->sr_core, &mode_lvq, &mode_lvq_p, st->total_brate );
956 :
957 : /*----------------------------------------------------------------*
958 : * Calculate number of stages and levels for each stage based on the allowed bit allocation
959 : * (subtract one bit for LSF predictor selection)
960 : *----------------------------------------------------------------*/
961 2693 : lsf_allocate_fx( sub( nBits, shr( predmode, 1 ) ), mode_lvq, mode_lvq_p, &stages0, &stages1, levels0, levels1, bits0, bits1 );
962 :
963 :
964 : /*--------------------------------------------------------------------------------*
965 : * LSF quantization of all other frames but SID frames
966 : * Select safety-net or predictive mode
967 : *--------------------------------------------------------------------------------*/
968 :
969 2693 : Err[0] = MAXINT32;
970 2693 : move32();
971 2693 : Err[1] = MAXINT32;
972 2693 : move32();
973 : /* for mem_MA update */
974 45781 : FOR( i = 0; i < M; i++ )
975 : {
976 43088 : pred1[i] = add( ModeMeans_fx[mode_lvq][i], mult_r( MU_MA_FX, st->mem_MA_fx[i] ) );
977 43088 : move16();
978 : }
979 :
980 2693 : IF( predmode == 0 )
981 : {
982 : /* Subtract only mean */
983 125 : Copy( ModeMeans_fx[mode_lvq], pred0, M );
984 125 : Vr_subt( lsf, pred0, Tmp, M );
985 :
986 : /* LVQ quantization (safety-net only) */
987 250 : Err[0] = vq_lvq_lsf_enc( 0, mode_lvq, Tmp, levels0, stages0, wghts, Idx0, lsf, pred0,
988 125 : st->offset_scale1_fx, st->offset_scale2_fx, st->no_scales_fx, resq, lsfq );
989 125 : safety_net = 1;
990 125 : move16();
991 125 : st->pstreaklen = 0;
992 125 : move16(); /* predictive LSF quantizer streak is ended with safety-net */
993 : }
994 2568 : ELSE IF( EQ_16( predmode, 1 ) ) /* only MA prediction */
995 : {
996 1471 : Vr_subt( lsf, pred1, Tmp1, M );
997 2942 : Err[1] = vq_lvq_lsf_enc( 2, mode_lvq_p, Tmp1, levels1, stages1, wghts, Idx1, lsf, pred1,
998 1471 : st->offset_scale1_p_fx, st->offset_scale2_p_fx, st->no_scales_p_fx, resq, lsfq );
999 :
1000 1471 : safety_net = 0;
1001 1471 : move16();
1002 : }
1003 : ELSE
1004 : {
1005 : /* Adaptive scaling factor (multiplier) is updated in order to reduce the amount of consecutive predictive frames in
1006 : case of possible frame erasure. AR-predictive usage for VOICED mode is allowed to be higher than other modes. */
1007 1097 : test();
1008 1097 : test();
1009 1097 : test();
1010 1097 : IF( ( ( GT_16( st->pstreaklen, ( STREAKLEN + 3 ) ) ) && ( EQ_16( coder_type, VOICED ) ) ) || ( ( GT_16( st->pstreaklen, ( STREAKLEN ) ) ) && ( NE_16( coder_type, VOICED ) ) ) )
1011 : {
1012 : /* update the adaptive scaling factor to become smaller with increasing number of concecutive predictive frames. */
1013 216 : st->streaklimit_fx = mult( st->streaklimit_fx, STREAKMULT_FX ); // Q15
1014 216 : move16();
1015 : }
1016 :
1017 1097 : IF( st->pstreaklen == 0 )
1018 : {
1019 : /* reset the consecutive AR-predictor multiplier */
1020 320 : st->streaklimit_fx = 32767; /*1.0 in Q15 */
1021 320 : move16();
1022 : }
1023 :
1024 : /* VOICED_WB@16kHz */
1025 1097 : test();
1026 1097 : IF( EQ_32( st->sr_core, INT_FS_16k ) && EQ_16( coder_type, VOICED ) && flag_1bit_gran == 0 )
1027 : {
1028 : /* Subtract mean and AR prediction */
1029 625 : Copy( ModeMeans_fx[mode_lvq], pred0, M );
1030 : /* subtract only mean */
1031 625 : Vr_subt( lsf, pred0, Tmp, M );
1032 :
1033 10625 : FOR( i = 0; i < M; i++ )
1034 : {
1035 : /* subtract mean and AR prediction */
1036 10000 : pred2[i] = mult( Predictors_fx[mode_lvq_p][i], sub( st->mem_AR_fx[i], pred0[i] ) );
1037 10000 : Tmp2[i] = sub( Tmp[i], pred2[i] );
1038 10000 : pred2[i] = add( pred2[i], pred0[i] );
1039 : }
1040 :
1041 : /* select safety_net or predictive */
1042 625 : safety_net = qlsf_Mode_Select_fx( wghts, Tmp2, st->streaklimit_fx, OP_LOOP_THR_HVO );
1043 625 : IF( EQ_16( force_sf, 1 ) )
1044 : {
1045 54 : safety_net = 1;
1046 54 : move16();
1047 : }
1048 :
1049 625 : IF( safety_net )
1050 : {
1051 : /* Safety-net - BC-TCQ quantization : SN */
1052 101 : Err[0] = qlsf_ARSN_tcvq_Enc_16k_fx( Tmp, lsfq, TCQIdx0, wghts, sub( nBits, 1 ), safety_net );
1053 101 : st->pstreaklen = 0;
1054 101 : move16();
1055 : }
1056 : ELSE
1057 : {
1058 : /* predictive - BC-TCQ quantization : AR */
1059 524 : Err[1] = qlsf_ARSN_tcvq_Enc_16k_fx( Tmp2, lsfq, TCQIdx0, wghts, sub( nBits, 1 ), safety_net );
1060 524 : st->pstreaklen = add( st->pstreaklen, 1 );
1061 : }
1062 : }
1063 : /* all other frames (not VOICED@16kHz) */
1064 : ELSE
1065 : {
1066 : /* Subtract mean and AR prediction */
1067 472 : Copy( ModeMeans_fx[mode_lvq], pred0, M );
1068 : /* subtract only mean */
1069 472 : Vr_subt( lsf, pred0, Tmp, M );
1070 :
1071 8024 : FOR( i = 0; i < M; i++ )
1072 : {
1073 : /* subtract mean and AR prediction */
1074 7552 : pred2[i] = add( pred0[i], mult( Predictors_fx[mode_lvq_p][i], sub( st->mem_AR_fx[i], pred0[i] ) ) );
1075 7552 : Tmp2[i] = sub( lsf[i], pred2[i] );
1076 : }
1077 :
1078 : /* safety-net */
1079 472 : Err[0] = vq_lvq_lsf_enc( 0, mode_lvq, Tmp, levels0, stages0, wghts, Idx0, lsf, pred0, st->offset_scale1_fx, st->offset_scale2_fx, st->no_scales_fx, resq, lsfq );
1080 : /* Predictive quantizer is calculated only if it can be selected */
1081 472 : test();
1082 472 : IF( !force_sf || GT_32( Err[0], abs_threshold ) )
1083 : {
1084 467 : Err[1] = vq_lvq_lsf_enc( 2, mode_lvq_p, Tmp2, levels1, stages1, wghts, Idx1, lsf, pred2,
1085 467 : st->offset_scale1_p_fx, st->offset_scale2_p_fx, st->no_scales_p_fx, &resq[M], &lsfq[M] );
1086 : }
1087 472 : test();
1088 472 : test();
1089 : /* Select whether to use safety-net (non-predictive) or predictive LSF quantizer. The decision is based on following:
1090 : if the non-predictive (safety-net) quantization error (Err[0]) is low enough (spectral distortion is low) it is selected
1091 : or if the predictively quantized error (Err[1]) is by at least adaptive margin smaller than non-predictive quantizer.
1092 : or if the in case of frame erasure the resulting concealed predictive LSF would be unstable safety-net is selected */
1093 472 : IF( force_sf || LT_32( Mult_32_16( Err[0], ( st->streaklimit_fx ) ), L_add( Err[1], Mult_32_16( Err[1], PREFERSFNET_FX ) ) ) || LT_32( Err[0], abs_threshold ) )
1094 : {
1095 163 : safety_net = 1;
1096 163 : move16();
1097 163 : st->pstreaklen = 0;
1098 163 : move16(); /* Reset the consecutive predictive frame counter */
1099 : }
1100 : ELSE
1101 : {
1102 309 : safety_net = 0;
1103 309 : move16(); /* Increase the consecutive predictive frame counter by one */
1104 309 : st->pstreaklen = add( st->pstreaklen, 1 );
1105 : }
1106 : }
1107 : }
1108 :
1109 : /*--------------------------------------------------------------------------* \
1110 : * Write indices to array \
1111 : *--------------------------------------------------------------------------*/
1112 :
1113 2693 : IF( EQ_16( st->codec_mode, MODE1 ) && EQ_16( st->core, ACELP_CORE ) )
1114 : {
1115 : /* write coder_type bit for VOICED@16kHz or GENERIC@16kHz */
1116 1427 : test();
1117 1427 : IF( EQ_16( coder_type_org, GENERIC ) && EQ_32( st->sr_core, INT_FS_16k ) )
1118 : {
1119 : /* VOICED =2 and GENERIC=3, so "coder_type-2" means VOICED =0 and GENERIC=1*/
1120 624 : push_indice( hBstr, IND_LSF_PREDICTOR_SELECT_BIT, sub( coder_type, 2 ), 1 );
1121 : }
1122 :
1123 : /* write predictor selection bit */
1124 1427 : IF( EQ_16( predmode, 2 ) )
1125 : {
1126 571 : push_indice( st->hBstr, IND_LSF_PREDICTOR_SELECT_BIT, safety_net, 1 );
1127 : }
1128 :
1129 1427 : test();
1130 1427 : IF( EQ_16( coder_type, VOICED ) && EQ_32( st->sr_core, INT_FS_16k ) && flag_1bit_gran == 0 )
1131 : {
1132 : /* BC-TCVQ (only for VOICED@16kHz) */
1133 359 : TCQIdx = &TCQIdx0[1];
1134 359 : Bit_alloc1 = &BC_TCVQ_BIT_ALLOC_40B[1];
1135 4308 : FOR( i = 0; i < ( M / 2 ) + 3; i++ )
1136 : {
1137 3949 : push_indice( hBstr, IND_LSF, TCQIdx[i], Bit_alloc1[i] );
1138 : }
1139 : }
1140 : ELSE
1141 : {
1142 1068 : cumleft = nBits;
1143 1068 : move16();
1144 1068 : IF( EQ_16( predmode, 2 ) )
1145 : {
1146 : /* subtract predictor selection bit */
1147 212 : cumleft = sub( nBits, 1 );
1148 : }
1149 :
1150 1068 : IF( safety_net )
1151 : {
1152 208 : stages = stages0;
1153 208 : move16();
1154 208 : Idx = Idx0;
1155 208 : move16();
1156 208 : bits = bits0;
1157 208 : move16();
1158 : }
1159 : ELSE
1160 : {
1161 860 : stages = stages1;
1162 860 : move16();
1163 860 : Idx = Idx1;
1164 860 : move16();
1165 860 : bits = bits1;
1166 860 : move16();
1167 : }
1168 :
1169 1068 : tmp = sub( stages, 1 );
1170 2473 : FOR( i = 0; i < tmp; i++ )
1171 : {
1172 1405 : indice[i] = Idx[i];
1173 1405 : move16();
1174 1405 : num_bits = bits[i];
1175 1405 : move16();
1176 1405 : cumleft -= num_bits;
1177 1405 : move16();
1178 1405 : push_indice( hBstr, IND_LSF, indice[i], num_bits );
1179 : }
1180 :
1181 4053 : WHILE( cumleft > 0 )
1182 : {
1183 2985 : indice[i] = Idx[i];
1184 2985 : move16();
1185 :
1186 2985 : IF( GT_16( cumleft, LEN_INDICE ) )
1187 : {
1188 1917 : num_bits = LEN_INDICE;
1189 1917 : move16();
1190 : }
1191 : ELSE
1192 : {
1193 1068 : num_bits = cumleft;
1194 1068 : move16();
1195 : }
1196 :
1197 2985 : cumleft = sub( cumleft, num_bits );
1198 2985 : push_indice( hBstr, IND_LSF, indice[i], num_bits );
1199 2985 : i = add( i, 1 );
1200 : }
1201 : }
1202 : }
1203 : ELSE
1204 : {
1205 1266 : test();
1206 1266 : IF( EQ_16( coder_type, VOICED ) && EQ_32( st->sr_core, INT_FS_16k ) )
1207 : {
1208 : /* BC-TCVQ (only for VOICED@16kHz) */
1209 : /* Number of quantization indices */
1210 266 : *no_indices = 10;
1211 266 : move16();
1212 2926 : FOR( i = 0; i < *no_indices; i++ )
1213 : {
1214 2660 : lpc_param[i] = TCQIdx0[i];
1215 2660 : move16();
1216 2660 : bits_param_lpc[i] = BC_TCVQ_BIT_ALLOC_40B[i]; // Q0
1217 2660 : move16();
1218 : }
1219 : }
1220 : ELSE
1221 : {
1222 : /* Number of quantization indices */
1223 :
1224 : /* there are 31 bits */
1225 1000 : IF( EQ_16( safety_net, 1 ) )
1226 : {
1227 80 : Idx = Idx0;
1228 80 : move16();
1229 80 : *no_indices = add( stages0, 1 );
1230 240 : FOR( i = 0; i < stages0; i++ )
1231 : {
1232 160 : lpc_param[i] = Idx[i];
1233 160 : move16();
1234 160 : indice[i] = Idx[i];
1235 160 : move16();
1236 160 : bits_param_lpc[i] = bits0[i];
1237 160 : move16();
1238 : }
1239 80 : lpc_param[stages0] = Idx[stages0];
1240 80 : move16();
1241 80 : indice[stages0] = Idx[stages0];
1242 80 : move16();
1243 80 : tmp = sub( stages0, 1 );
1244 80 : bits_param_lpc[tmp] = LEN_INDICE;
1245 80 : move16();
1246 80 : bits_param_lpc[stages0] = sub( bits0[tmp], LEN_INDICE );
1247 : }
1248 : ELSE
1249 : {
1250 920 : *no_indices = add( stages1, 1 );
1251 920 : Idx = Idx1;
1252 920 : move16();
1253 2580 : FOR( i = 0; i < stages1; i++ )
1254 : {
1255 1660 : lpc_param[i] = ( Idx[i] );
1256 1660 : move16();
1257 1660 : indice[i] = Idx[i];
1258 1660 : move16();
1259 1660 : bits_param_lpc[i] = bits1[i];
1260 1660 : move16();
1261 : }
1262 920 : lpc_param[stages1] = ( Idx[stages1] );
1263 920 : move16();
1264 920 : indice[stages1] = Idx[stages1];
1265 920 : move16();
1266 920 : tmp = sub( stages1, 1 );
1267 920 : bits_param_lpc[tmp] = LEN_INDICE;
1268 920 : move16();
1269 920 : bits_param_lpc[stages1] = sub( bits1[tmp], LEN_INDICE );
1270 : }
1271 1000 : IF( EQ_16( predmode, 2 ) )
1272 : {
1273 860 : FOR( i = *no_indices; i > 0; i-- )
1274 : {
1275 600 : tmp = sub( i, 1 );
1276 600 : lpc_param[i] = lpc_param[tmp];
1277 600 : move16();
1278 600 : bits_param_lpc[i] = bits_param_lpc[tmp];
1279 600 : move16();
1280 : }
1281 260 : lpc_param[0] = safety_net;
1282 260 : move16(); /* put the safety net info on the last param */
1283 260 : bits_param_lpc[0] = 1;
1284 260 : move16();
1285 260 : *no_indices = add( *no_indices, 1 );
1286 : }
1287 : }
1288 : }
1289 :
1290 :
1291 : /*--------------------------------------------------------------------------*
1292 : * De-quantize encoded LSF vector
1293 : *--------------------------------------------------------------------------*/
1294 :
1295 2693 : IF( safety_net )
1296 : {
1297 : /* Safety-net */
1298 389 : test();
1299 389 : IF( EQ_16( coder_type, VOICED ) && EQ_32( st->sr_core, INT_FS_16k ) && flag_1bit_gran == 0 )
1300 : {
1301 : /* BC-TCQ */
1302 101 : Copy( lsfq, st->mem_MA_fx, M );
1303 101 : Vr_add( lsfq, pred0, qlsf, M );
1304 : }
1305 : ELSE
1306 : {
1307 : {
1308 288 : vq_dec_lvq_fx( 1, qlsf, &indice[0], stages0, M, mode_lvq, levels0[stages0 - 1],
1309 : &st->offset_scale1_fx[0][0], &st->offset_scale2_fx[0][0], &st->offset_scale1_p_fx[0][0], &st->offset_scale2_p_fx[0][0],
1310 : &st->no_scales_fx[0][0], &st->no_scales_p_fx[0][0] );
1311 288 : Vr_add( qlsf, pred0, qlsf, M );
1312 288 : Vr_subt( qlsf, pred1, st->mem_MA_fx, M );
1313 : }
1314 : }
1315 : }
1316 : ELSE
1317 : {
1318 2304 : test();
1319 2304 : IF( EQ_16( coder_type, VOICED ) && EQ_32( st->sr_core, INT_FS_16k ) && flag_1bit_gran == 0 )
1320 : {
1321 : /* BC-TCVQ */
1322 524 : Copy( lsfq, st->mem_MA_fx, M );
1323 524 : Vr_add( lsfq, pred2, qlsf, M );
1324 : }
1325 : ELSE
1326 : {
1327 : /* LVQ */
1328 1780 : vq_dec_lvq_fx( 0, qlsf, &indice[0], stages1, M, mode_lvq_p, levels1[stages1 - 1],
1329 : &st->offset_scale1_fx[0][0], &st->offset_scale2_fx[0][0], &st->offset_scale1_p_fx[0][0],
1330 : &st->offset_scale2_p_fx[0][0], &st->no_scales_fx[0][0], &st->no_scales_p_fx[0][0] );
1331 1780 : IF( EQ_16( predmode, 1 ) )
1332 : {
1333 1471 : Copy( qlsf, st->mem_MA_fx, M );
1334 1471 : Vr_add( qlsf, pred1, qlsf, M );
1335 : }
1336 : ELSE
1337 : {
1338 309 : Vr_add( qlsf, pred2, qlsf, M );
1339 309 : Vr_subt( qlsf, pred1, st->mem_MA_fx, M );
1340 : }
1341 : }
1342 : }
1343 :
1344 : /* Sort the quantized vector to ascending order */
1345 2693 : sort_fx( qlsf, 0, M - 1 );
1346 :
1347 : /* Verify stability by adding minimum separation */
1348 2693 : reorder_lsf_fx( qlsf, MODE1_LSF_GAP_FX, M, st->sr_core );
1349 :
1350 : /* Update AR-predictor memories */
1351 2693 : Copy( qlsf, st->mem_AR_fx, M );
1352 2693 : return;
1353 : }
1354 :
1355 267644 : void lsf_end_enc_ivas_fx(
1356 : Encoder_State *st, /* i/o: encoder state structure */
1357 : const Word16 *lsf, /* i : LSF in the frequency domain (0..6400) x2.56*/
1358 : Word16 *qlsf, /* o : quantized LSF x2.56*/
1359 : const Word16 nBits_in, /* i : number of bits to spend on ISF quantization */
1360 : const Word16 coder_type_org, /* i : coding type */
1361 : Word16 Q_ener, /* i : Q valuen for Bin_Ener */
1362 : Word16 force_sf, /* i : Force safety-net usage if coding type supports */
1363 : Word16 *lpc_param,
1364 : Word16 *no_indices,
1365 : Word16 *bits_param_lpc,
1366 : Word16 coder_type_raw /* i : Coder type (LSF coder_type have some special cases)*/
1367 : ,
1368 : const Word16 tdm_lsfQ_PCh[M] /* i : Q LSFs for primary channel */
1369 : )
1370 : {
1371 : Word16 i;
1372 : Word16 Idx0[MAX_VQ_STAGES + 3]; /* Optimal codebook indices for safety-net quantizer */
1373 : Word16 Idx1[MAX_VQ_STAGES + 3]; /* Optimal codebook indices for predictive quantizer */
1374 : Word16 indice[MAX_VQ_STAGES + 3]; /* Temp. array of indice for vector de-quantizer */
1375 267644 : Word16 mode_lvq = 0, mode_lvq_p = 0; /* LVQ mode and predictive LVQ mode */
1376 : Word16 bits0[MAX_VQ_STAGES], bits1[MAX_VQ_STAGES];
1377 267644 : const Word16 *Bit_alloc1 = NULL;
1378 : Word32 Err[2]; /* Quantization error for safety-net(0) and predictive(1) quantizers */
1379 : Word16 Tmp[M]; /* Temporary target vector (mean and prediction removed) */
1380 : Word16 pred0[M]; /* Prediction for the safety-net quantizer (usually mean) */
1381 : Word16 pred1[M]; /* Prediction for the predictive quantizer */
1382 : Word16 pred2[M]; /* Prediction for the predictive quantizer */
1383 : Word16 wghts[M]; /* Weighting used for quantizer (currently GSM based) */
1384 : Word16 stages0; /* Amount of stages used by safety-net quantizer */
1385 : Word16 stages1; /* Amount of stages used by predictive quantizer */
1386 : Word16 levels0[MAX_VQ_STAGES]; /* Sizes of different codebook stages for safety-net quantizer */
1387 : Word16 levels1[MAX_VQ_STAGES]; /* Sizes of different codebook stages for predictive quantizer */
1388 : Word16 predmode; /* 0: safety-net only, 1: predictive only, 2: best of the two */
1389 : Word16 safety_net, cumleft, num_bits;
1390 : Word16 *Idx, stages, *bits;
1391 : Word16 Tmp2[M], Tmp1[M];
1392 : Word32 abs_threshold; /* Absolute threshold depending on signal bandwidth, that indicates
1393 : very good perceptual LSF quantization performance */
1394 : Word16 lsfq[M * 2], resq[M * 2];
1395 : Word16 coder_type; /* coder type (from LSF quantizer point of view) */
1396 : Word16 nBits; /* Number of bits */
1397 : Word16 TCQIdx0[M + 2]; /* Optimal codebook indices for VQ-TCQ quantizer */
1398 : Word16 *TCQIdx;
1399 : Word16 tmp;
1400 : Word16 flag_1bit_gran;
1401 267644 : BSTR_ENC_HANDLE hBstr = st->hBstr;
1402 : Word16 pred3[M];
1403 : Word16 dummy, dummy_v[5];
1404 :
1405 267644 : flag_1bit_gran = (Word16) GT_16( st->element_mode, EVS_MONO );
1406 :
1407 267644 : nBits = nBits_in;
1408 267644 : move16();
1409 : /* Update LSF coder_type for LSF quantizer for some special cases */
1410 267644 : test();
1411 267644 : test();
1412 267644 : test();
1413 :
1414 267644 : IF( EQ_16( coder_type_org, GENERIC ) && EQ_32( st->sr_core, INT_FS_16k ) && EQ_16( st->codec_mode, MODE1 ) && ( st->idchan == 0 ) ) /* this bit is used only for primary channel or mono */
1415 : {
1416 61236 : IF( EQ_16( coder_type_raw, VOICED ) )
1417 : {
1418 18759 : coder_type = VOICED;
1419 18759 : move16(); /* Reflect Inactive mode */
1420 18759 : if ( EQ_16( flag_1bit_gran, 1 ) )
1421 : {
1422 18759 : nBits = sub( nBits, 1 ); /* This is for real Generic*/
1423 : }
1424 : }
1425 : ELSE
1426 : {
1427 42477 : nBits = sub( nBits, 1 ); /* This is for real Generic*/
1428 42477 : coder_type = coder_type_org;
1429 42477 : move16();
1430 : }
1431 : }
1432 : ELSE
1433 : {
1434 206408 : coder_type = coder_type_org;
1435 206408 : move16();
1436 : }
1437 :
1438 : /*----------------------------------------------------------------------------------- -*
1439 : * Calculate the number of stages and levels for each stage based on allowed bit budget
1440 : * Set absolute threshold for codebook-type decision logic depending on signal bandwidth
1441 : *------------------------------------------------------------------------------------ -*/
1442 267644 : IF( st->bwidth == NB )
1443 : {
1444 0 : abs_threshold = SFNETLOWLIMIT_NB / 2;
1445 0 : move32();
1446 : }
1447 : ELSE
1448 : {
1449 267644 : abs_threshold = SFNETLOWLIMIT_WB / 2;
1450 267644 : move32();
1451 : }
1452 : /* Calculate LSF weighting coefficients */
1453 267644 : Unified_weighting_fx( &st->Bin_E_fx[L_FFT / 2], Q_ener, lsf, wghts, (Word16) EQ_16( st->bwidth, NB ), (Word16) EQ_16( coder_type, UNVOICED ), st->sr_core, M );
1454 :
1455 : /*--------------------------------------------------------------------------------*
1456 : * LSF quantization of SID frames
1457 : *--------------------------------------------------------------------------------*/
1458 267644 : IF( EQ_32( st->core_brate, SID_2k40 ) )
1459 : {
1460 448 : lsfq_CNG_ivas_fx( hBstr, lsf, wghts, qlsf );
1461 448 : sort_fx( qlsf, 0, M - 1 );
1462 448 : reorder_lsf_fx( qlsf, MODE1_LSF_GAP_FX, M, st->sr_core );
1463 :
1464 448 : return;
1465 : }
1466 : /* Find allowed predictor mode for current coder_type. (SN only (0), SN/AR switched (2) or MA predictive (1) */
1467 267196 : find_pred_mode( &predmode, coder_type, st->bwidth, st->sr_core, &mode_lvq, &mode_lvq_p, st->total_brate );
1468 :
1469 : /*----------------------------------------------------------------*
1470 : * Calculate number of stages and levels for each stage based on the allowed bit allocation
1471 : * (subtract one bit for LSF predictor selection)
1472 : *----------------------------------------------------------------*/
1473 267196 : lsf_allocate_fx( sub( nBits, shr( predmode, 1 ) ), mode_lvq, mode_lvq_p, &stages0, &stages1, levels0, levels1, bits0, bits1 );
1474 :
1475 :
1476 : /*--------------------------------------------------------------------------------*
1477 : * LSF quantization of all other frames but SID frames
1478 : * Select safety-net or predictive mode
1479 : *--------------------------------------------------------------------------------*/
1480 :
1481 267196 : Err[0] = MAXINT32;
1482 267196 : move32();
1483 267196 : Err[1] = MAXINT32;
1484 267196 : move32();
1485 : /* for mem_MA update */
1486 4542332 : FOR( i = 0; i < M; i++ )
1487 : {
1488 4275136 : pred1[i] = add( ModeMeans_fx[mode_lvq][i], mult_r( MU_MA_FX, st->mem_MA_fx[i] ) );
1489 4275136 : move16();
1490 : }
1491 :
1492 267196 : test();
1493 267196 : test();
1494 : /* TD stereo SCh: perform intra-frame prediction with pulling-to-mean */
1495 267196 : IF( st->tdm_LRTD_flag == 0 && st->idchan == 1 && tdm_lsfQ_PCh != NULL )
1496 : {
1497 : /* if secondary channel predmode is set to be > 2 */
1498 126 : predmode = add( predmode, 3 );
1499 :
1500 126 : tdm_SCh_LSF_intra_pred_fx( st->element_brate, tdm_lsfQ_PCh, pred3 );
1501 : }
1502 :
1503 267196 : IF( predmode == 0 )
1504 : {
1505 : /* Subtract only mean */
1506 14056 : Copy( ModeMeans_fx[mode_lvq], pred0, M );
1507 14056 : Vr_subt( lsf, pred0, Tmp, M );
1508 :
1509 : /* LVQ quantization (safety-net only) */
1510 14056 : Err[0] = vq_lvq_lsf_enc_ivas_fx( 0, mode_lvq, Tmp, levels0, stages0, wghts, Idx0, lsf, pred0, resq, lsfq );
1511 14056 : safety_net = 1;
1512 14056 : move16();
1513 14056 : st->pstreaklen = 0;
1514 14056 : move16(); /* predictive LSF quantizer streak is ended with safety-net */
1515 : }
1516 253140 : ELSE IF( EQ_16( predmode, 1 ) ) /* only MA prediction */
1517 : {
1518 176425 : Vr_subt( lsf, pred1, Tmp1, M );
1519 176425 : Err[1] = vq_lvq_lsf_enc_ivas_fx( 2, mode_lvq_p, Tmp1, levels1, stages1, wghts, Idx1, lsf, pred1, resq, lsfq );
1520 :
1521 176425 : safety_net = 0;
1522 176425 : move16();
1523 : }
1524 : ELSE
1525 : {
1526 76715 : IF( EQ_16( predmode, 2 ) )
1527 : {
1528 : /* Subtract mean and AR prediction */
1529 76589 : Copy( ModeMeans_fx[mode_lvq], pred0, M );
1530 :
1531 : /* subtract only mean */
1532 76589 : Vr_subt( lsf, pred0, Tmp, M );
1533 :
1534 1302013 : FOR( i = 0; i < M; i++ )
1535 : {
1536 : /* subtract mean and AR prediction */
1537 1225424 : pred2[i] = add( pred0[i], mult( Predictors_fx[mode_lvq_p][i], sub( st->mem_AR_fx[i], pred0[i] ) ) );
1538 1225424 : Tmp2[i] = sub( lsf[i], pred2[i] );
1539 : }
1540 : /* Adaptive scaling factor (multiplier) is updated in order to reduce the amount of consecutive predictive frames in
1541 : case of possible frame erasure. AR-predictive usage for VOICED mode is allowed to be higher than other modes. */
1542 76589 : test();
1543 76589 : test();
1544 76589 : test();
1545 76589 : if ( ( ( GT_16( st->pstreaklen, ( STREAKLEN + 3 ) ) ) && ( EQ_16( coder_type, VOICED ) ) ) || ( ( GT_16( st->pstreaklen, ( STREAKLEN ) ) ) && ( NE_16( coder_type, VOICED ) ) ) )
1546 : {
1547 : /* update the adaptive scaling factor to become smaller with increasing number of concecutive predictive frames. */
1548 12070 : st->streaklimit_fx = mult( st->streaklimit_fx, STREAKMULT_FX );
1549 12070 : move16();
1550 : }
1551 :
1552 76589 : if ( st->pstreaklen == 0 )
1553 : {
1554 : /* reset the consecutive AR-predictor multiplier */
1555 27984 : st->streaklimit_fx = 32767; /*1.0 in Q15 */
1556 27984 : move16();
1557 : }
1558 :
1559 : /* VOICED_WB@16kHz */
1560 76589 : test();
1561 76589 : test();
1562 76589 : IF( EQ_32( st->sr_core, INT_FS_16k ) && EQ_16( coder_type, VOICED ) && flag_1bit_gran == 0 )
1563 : {
1564 : /* select safety_net or predictive */
1565 0 : safety_net = qlsf_Mode_Select_fx( wghts, Tmp2, st->streaklimit_fx, OP_LOOP_THR_HVO );
1566 0 : if ( EQ_16( force_sf, 1 ) )
1567 : {
1568 0 : safety_net = 1;
1569 0 : move16();
1570 : }
1571 :
1572 0 : IF( safety_net )
1573 : {
1574 : /* Safety-net - BC-TCQ quantization : SN */
1575 0 : Err[0] = qlsf_ARSN_tcvq_Enc_16k_fx( Tmp, lsfq, TCQIdx0, wghts, sub( nBits, 1 ), safety_net );
1576 0 : st->pstreaklen = 0;
1577 0 : move16();
1578 : }
1579 : ELSE
1580 : {
1581 : /* predictive - BC-TCQ quantization : AR */
1582 0 : Err[1] = qlsf_ARSN_tcvq_Enc_16k_fx( Tmp2, lsfq, TCQIdx0, wghts, sub( nBits, 1 ), safety_net );
1583 0 : st->pstreaklen = add( st->pstreaklen, 1 );
1584 : }
1585 : }
1586 : /* all other frames (not VOICED@16kHz) */
1587 : ELSE
1588 : {
1589 : /* safety-net */
1590 :
1591 76589 : Err[0] = vq_lvq_lsf_enc_ivas_fx( 0, mode_lvq, Tmp, levels0, stages0, wghts, Idx0, lsf, pred0, resq, lsfq );
1592 : /* Predictive quantizer is calculated only if it can be selected */
1593 76589 : test();
1594 76589 : IF( !force_sf || GT_32( Err[0], abs_threshold ) )
1595 : {
1596 76557 : Err[1] = vq_lvq_lsf_enc_ivas_fx( 2, mode_lvq_p, Tmp2, levels1, stages1, wghts, Idx1, lsf, pred2, &resq[M], &lsfq[M] );
1597 : }
1598 76589 : test();
1599 76589 : test();
1600 : /* Select whether to use safety-net (non-predictive) or predictive LSF quantizer. The decision is based on following:
1601 : if the non-predictive (safety-net) quantization error (Err[0]) is low enough (spectral distortion is low) it is selected
1602 : or if the predictively quantized error (Err[1]) is by at least adaptive margin smaller than non-predictive quantizer.
1603 : or if the in case of frame erasure the resulting concealed predictive LSF would be unstable safety-net is selected */
1604 76589 : IF( force_sf || LT_32( Mult_32_16( Err[0], ( st->streaklimit_fx ) ), L_add( Err[1], Mult_32_16( Err[1], PREFERSFNET_FX ) ) ) || LT_32( Err[0], abs_threshold ) )
1605 : {
1606 23487 : safety_net = 1;
1607 23487 : move16();
1608 23487 : st->pstreaklen = 0;
1609 23487 : move16(); /* Reset the consecutive predictive frame counter */
1610 : }
1611 : ELSE
1612 : {
1613 53102 : safety_net = 0;
1614 53102 : move16(); /* Increase the consecutive predictive frame counter by one */
1615 53102 : st->pstreaklen = add( st->pstreaklen, 1 );
1616 : }
1617 : }
1618 : }
1619 : ELSE /* of "if (predmode==2)" */
1620 : {
1621 126 : Copy( ModeMeans_fx[mode_lvq], pred0, M );
1622 :
1623 126 : if ( EQ_16( predmode, 4 ) )
1624 : {
1625 105 : mode_lvq_p = 9; /* force to Generic WB with AR*/
1626 105 : move16();
1627 : }
1628 :
1629 2142 : FOR( i = 0; i < M; i++ )
1630 : {
1631 : /* subtract mean and AR prediction */
1632 2016 : pred2[i] = add( pred0[i], mult( Predictors_fx[mode_lvq_p][i], sub( st->mem_AR_fx[i], pred0[i] ) ) );
1633 2016 : Tmp[i] = sub( lsf[i], pred2[i] );
1634 2016 : Tmp2[i] = sub( lsf[i], pred3[i] );
1635 : }
1636 :
1637 : /* Adaptive scaling factor (multiplier) is updated in order to reduce the amount of consecutive predictive frames in
1638 : case of possible frame erasure. AR-predictive usage for VOICED mode is allowed to be higher than other modes. */
1639 126 : IF( GT_16( st->pstreaklen, ( STREAKLEN ) ) )
1640 : {
1641 : /* update the adaptive scaling factor to become smaller with increasing number of concecutive predictive frames. */
1642 11 : st->streaklimit_fx = mult( st->streaklimit_fx, STREAKMULT_FX );
1643 11 : move16();
1644 : }
1645 :
1646 126 : if ( st->pstreaklen == 0 )
1647 : {
1648 : /* reset the adaptive scaling factor */
1649 74 : st->streaklimit_fx = 32767;
1650 74 : move16();
1651 : }
1652 :
1653 : /* intra pred */
1654 : /* use G AR pred for the intra mode (as a safety mode, this is why the indexes 0/1 are interchanged)*/
1655 :
1656 126 : lsf_allocate_fx( sub( nBits, 1 ), mode_lvq, 9, &stages1, &stages0, levels1, levels0, bits1, bits0 );
1657 126 : Err[0] = vq_lvq_lsf_enc_ivas_fx( 2, 9, Tmp2, levels0, stages0, wghts, Idx0, lsf, pred3, &resq[M], &lsfq[M] );
1658 126 : IF( force_sf )
1659 : {
1660 6 : safety_net = 1; /* intra-frame prediction */
1661 6 : st->pstreaklen = 0; /* Reset the consecutive predictive frame counter */
1662 : }
1663 : ELSE
1664 : { /* try also the inter frame prediction */
1665 :
1666 : /* AR inter-frame prediction */
1667 120 : lsf_allocate_fx( nBits - 1, mode_lvq, mode_lvq_p, &dummy, &stages1, dummy_v, levels1, dummy_v, bits1 );
1668 120 : Err[1] = vq_lvq_lsf_enc_ivas_fx( 2, mode_lvq_p, Tmp, levels1, stages1, wghts, Idx1, lsf, pred2, resq, lsfq );
1669 120 : IF( LT_32( Mult_32_16( Err[0], ( st->streaklimit_fx ) ), L_add( Err[1], Mult_32_16( Err[1], PREFERSFNET_FX ) ) ) )
1670 : {
1671 53 : safety_net = 1;
1672 53 : move16();
1673 53 : st->pstreaklen = 0; /* Reset the consecutive predictive frame counter */
1674 53 : move16();
1675 : }
1676 : ELSE
1677 : {
1678 67 : safety_net = 0;
1679 67 : move16();
1680 67 : st->pstreaklen = add( st->pstreaklen, 1 ); /* Increase the consecutive predictive frame counter by one */
1681 67 : move16();
1682 : }
1683 : }
1684 : }
1685 : }
1686 : /*--------------------------------------------------------------------------*
1687 : * Write indices to array
1688 : *--------------------------------------------------------------------------*/
1689 :
1690 267196 : test();
1691 267196 : IF( EQ_16( st->codec_mode, MODE1 ) && EQ_16( st->core, ACELP_CORE ) )
1692 : {
1693 : /* write coder_type bit for VOICED@16kHz or GENERIC@16kHz */
1694 155786 : test();
1695 155786 : test();
1696 155786 : IF( EQ_16( coder_type_org, GENERIC ) && EQ_32( st->sr_core, INT_FS_16k ) && ( st->idchan == 0 ) )
1697 : {
1698 : /* VOICED =2 and GENERIC=3, so "coder_type-2" means VOICED =0 and GENERIC=1*/
1699 61236 : push_indice( hBstr, IND_LSF_PREDICTOR_SELECT_BIT, sub( coder_type, 2 ), 1 );
1700 : }
1701 :
1702 : /* write predictor selection bit */
1703 155786 : IF( GE_16( predmode, 2 ) )
1704 : {
1705 39612 : push_indice( st->hBstr, IND_LSF_PREDICTOR_SELECT_BIT, safety_net, 1 );
1706 : }
1707 :
1708 155786 : test();
1709 155786 : test();
1710 155786 : IF( EQ_16( coder_type, VOICED ) && EQ_32( st->sr_core, INT_FS_16k ) && flag_1bit_gran == 0 )
1711 : {
1712 : /* BC-TCVQ (only for VOICED@16kHz) */
1713 0 : TCQIdx = &TCQIdx0[1];
1714 0 : Bit_alloc1 = &BC_TCVQ_BIT_ALLOC_40B[1];
1715 0 : FOR( i = 0; i < ( M / 2 ) + 3; i++ )
1716 : {
1717 0 : push_indice( hBstr, IND_LSF, TCQIdx[i], Bit_alloc1[i] );
1718 : }
1719 : }
1720 : ELSE
1721 : {
1722 155786 : cumleft = nBits;
1723 155786 : move16();
1724 155786 : IF( GE_16( predmode, 2 ) )
1725 : {
1726 : /* subtract predictor selection bit */
1727 39612 : cumleft = sub( nBits, 1 );
1728 : }
1729 :
1730 155786 : IF( safety_net )
1731 : {
1732 25004 : stages = stages0;
1733 25004 : move16();
1734 25004 : Idx = Idx0;
1735 25004 : move16();
1736 25004 : bits = bits0;
1737 25004 : move16();
1738 : }
1739 : ELSE
1740 : {
1741 130782 : stages = stages1;
1742 130782 : move16();
1743 130782 : Idx = Idx1;
1744 130782 : move16();
1745 130782 : bits = bits1;
1746 130782 : move16();
1747 : }
1748 :
1749 155786 : tmp = sub( stages, 1 );
1750 359254 : FOR( i = 0; i < tmp; i++ )
1751 : {
1752 203468 : indice[i] = Idx[i];
1753 203468 : move16();
1754 203468 : num_bits = bits[i];
1755 203468 : move16();
1756 203468 : cumleft -= num_bits;
1757 203468 : move16();
1758 203468 : push_indice( hBstr, IND_LSF, indice[i], num_bits );
1759 : }
1760 :
1761 586637 : WHILE( cumleft > 0 )
1762 : {
1763 430851 : indice[i] = Idx[i];
1764 430851 : move16();
1765 :
1766 430851 : IF( GT_16( cumleft, LEN_INDICE ) )
1767 : {
1768 275065 : num_bits = LEN_INDICE;
1769 275065 : move16();
1770 : }
1771 : ELSE
1772 : {
1773 155786 : num_bits = cumleft;
1774 155786 : move16();
1775 : }
1776 :
1777 430851 : cumleft = sub( cumleft, num_bits );
1778 430851 : push_indice( hBstr, IND_LSF, indice[i], num_bits );
1779 430851 : i = add( i, 1 );
1780 : }
1781 : }
1782 : }
1783 : ELSE
1784 : {
1785 111410 : test();
1786 111410 : test();
1787 111410 : IF( EQ_16( coder_type, VOICED ) && EQ_32( st->sr_core, INT_FS_16k ) && flag_1bit_gran == 0 )
1788 : {
1789 : /* BC-TCVQ (only for VOICED@16kHz) */
1790 : /* Number of quantization indices */
1791 0 : *no_indices = 10;
1792 0 : move16();
1793 0 : FOR( i = 0; i < *no_indices; i++ )
1794 : {
1795 0 : lpc_param[i] = TCQIdx0[i];
1796 0 : move16();
1797 0 : bits_param_lpc[i] = BC_TCVQ_BIT_ALLOC_40B[i];
1798 0 : move16();
1799 : }
1800 : }
1801 : ELSE
1802 : {
1803 : /* Number of quantization indices */
1804 :
1805 : /* there are 31 bits */
1806 111410 : IF( EQ_16( safety_net, 1 ) )
1807 : {
1808 12598 : Idx = Idx0;
1809 12598 : move16();
1810 12598 : *no_indices = add( stages0, 1 );
1811 37794 : FOR( i = 0; i < stages0; i++ )
1812 : {
1813 25196 : lpc_param[i] = Idx[i];
1814 25196 : move16();
1815 25196 : indice[i] = Idx[i];
1816 25196 : move16();
1817 25196 : bits_param_lpc[i] = bits0[i];
1818 25196 : move16();
1819 : }
1820 12598 : lpc_param[stages0] = Idx[stages0];
1821 12598 : move16();
1822 12598 : indice[stages0] = Idx[stages0];
1823 12598 : move16();
1824 12598 : tmp = sub( stages0, 1 );
1825 12598 : bits_param_lpc[tmp] = LEN_INDICE;
1826 12598 : move16();
1827 12598 : bits_param_lpc[stages0] = sub( bits0[tmp], LEN_INDICE );
1828 : }
1829 : ELSE
1830 : {
1831 98812 : *no_indices = add( stages1, 1 );
1832 98812 : Idx = Idx1;
1833 98812 : move16();
1834 271931 : FOR( i = 0; i < stages1; i++ )
1835 : {
1836 173119 : lpc_param[i] = ( Idx[i] );
1837 173119 : move16();
1838 173119 : indice[i] = Idx[i];
1839 173119 : move16();
1840 173119 : bits_param_lpc[i] = bits1[i];
1841 173119 : move16();
1842 : }
1843 98812 : lpc_param[stages1] = ( Idx[stages1] );
1844 98812 : move16();
1845 98812 : indice[stages1] = Idx[stages1];
1846 98812 : move16();
1847 98812 : tmp = sub( stages1, 1 );
1848 98812 : bits_param_lpc[tmp] = LEN_INDICE;
1849 98812 : move16();
1850 98812 : bits_param_lpc[stages1] = sub( bits1[tmp], LEN_INDICE );
1851 : }
1852 111410 : IF( EQ_16( predmode, 2 ) )
1853 : {
1854 123907 : FOR( i = *no_indices; i > 0; i-- )
1855 : {
1856 86804 : tmp = sub( i, 1 );
1857 86804 : lpc_param[i] = lpc_param[tmp];
1858 86804 : move16();
1859 86804 : bits_param_lpc[i] = bits_param_lpc[tmp];
1860 86804 : move16();
1861 : }
1862 37103 : lpc_param[0] = safety_net;
1863 37103 : move16(); /* put the safety net info on the last param */
1864 37103 : bits_param_lpc[0] = 1;
1865 37103 : move16();
1866 37103 : *no_indices = add( *no_indices, 1 );
1867 : }
1868 : }
1869 : }
1870 :
1871 :
1872 : /*--------------------------------------------------------------------------*
1873 : * De-quantize encoded LSF vector
1874 : *--------------------------------------------------------------------------*/
1875 :
1876 267196 : IF( safety_net )
1877 : {
1878 : /* Safety-net */
1879 37602 : test();
1880 37602 : test();
1881 37602 : IF( EQ_16( coder_type, VOICED ) && EQ_32( st->sr_core, INT_FS_16k ) && flag_1bit_gran == 0 )
1882 : {
1883 : /* BC-TCQ */
1884 0 : Copy( lsfq, st->mem_MA_fx, M );
1885 0 : Vr_add( lsfq, pred0, qlsf, M );
1886 : }
1887 : ELSE
1888 : {
1889 37602 : IF( EQ_16( st->tdm_LRTD_flag, 0 ) && EQ_16( st->idchan, 1 ) && tdm_lsfQ_PCh != NULL )
1890 : {
1891 : /* intra mode*/
1892 59 : vq_dec_lvq_ivas_fx( 0, qlsf, &indice[0], stages0, M, 9, levels0[stages0 - 1] );
1893 59 : Vr_add( qlsf, pred3, qlsf, M );
1894 59 : Vr_subt( qlsf, pred1, st->mem_MA_fx, M );
1895 : }
1896 : ELSE
1897 : {
1898 37543 : vq_dec_lvq_ivas_fx( 1, qlsf, &indice[0], stages0, M, mode_lvq, levels0[stages0 - 1] );
1899 37543 : Vr_add( qlsf, pred0, qlsf, M );
1900 37543 : Vr_subt( qlsf, pred1, st->mem_MA_fx, M );
1901 : }
1902 : }
1903 : }
1904 : ELSE
1905 : {
1906 229594 : test();
1907 229594 : IF( EQ_16( coder_type, VOICED ) && EQ_32( st->sr_core, INT_FS_16k ) && flag_1bit_gran == 0 )
1908 : {
1909 : /* BC-TCVQ */
1910 0 : Copy( lsfq, st->mem_MA_fx, M );
1911 0 : Vr_add( lsfq, pred2, qlsf, M );
1912 : }
1913 : ELSE
1914 : {
1915 : /* LVQ */
1916 229594 : vq_dec_lvq_ivas_fx( 0, qlsf, &indice[0], stages1, M, mode_lvq_p, levels1[stages1 - 1] );
1917 229594 : IF( EQ_16( predmode, 1 ) )
1918 : {
1919 176425 : Copy( qlsf, st->mem_MA_fx, M );
1920 176425 : Vr_add( qlsf, pred1, qlsf, M );
1921 : }
1922 : ELSE
1923 : {
1924 53169 : Vr_add( qlsf, pred2, qlsf, M );
1925 53169 : Vr_subt( qlsf, pred1, st->mem_MA_fx, M );
1926 : }
1927 : }
1928 : }
1929 :
1930 : /* Sort the quantized vector to ascending order */
1931 267196 : sort_fx( qlsf, 0, M - 1 );
1932 :
1933 : /* Verify stability by adding minimum separation */
1934 267196 : reorder_lsf_fx( qlsf, MODE1_LSF_GAP_FX, M, st->sr_core );
1935 :
1936 : /* Update AR-predictor memories */
1937 267196 : Copy( qlsf, st->mem_AR_fx, M );
1938 267196 : return;
1939 : }
1940 :
1941 : /*-------------------------------------------------------------------*
1942 : * first_VQstages()
1943 : *
1944 : *
1945 : *-------------------------------------------------------------------*/
1946 :
1947 2276 : static void first_VQstages(
1948 : const Word16 *const *cb,
1949 : Word16 u[], /* i : vector to be encoded (prediction and mean removed) x2.56*/
1950 : Word16 *levels, /* i : number of levels in each stage */
1951 : Word16 stagesVQ, /* i : number of stages */
1952 : Word16 w[], /* i : weights Q8*/
1953 : Word16 N, /* i : vector dimension */
1954 : Word16 max_inner, /* i : maximum number of swaps in inner loop */
1955 : Word16 indices_VQstage[] )
1956 : {
1957 : Word16 resid_buf[2 * LSFMBEST * M], *resid[2];
1958 : Word32 dist_buf[2 * LSFMBEST], *dist[2], en;
1959 : Word32 f_tmp, L_tmp, L_tmp1, *pTmp32;
1960 : Word16 Tmp[M], *pTmp, cs;
1961 2276 : Word16 *pTmp_short, idx_buf[2 * LSFMBEST * MAX_VQ_STAGES], parents[LSFMBEST], counter = 0, j,
1962 : m, s, c, c2, p_max, *indices[2];
1963 2276 : Word16 maxC = LSFMBEST;
1964 : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
1965 2276 : Flag Overflow = 0;
1966 : #endif
1967 :
1968 : /*float dd[16];*/
1969 : const Word16 *cb_stage, *cbp;
1970 :
1971 : /* Set pointers to previous (parent) and current node (parent node is indexed [0], current node is indexed [1]) */
1972 2276 : indices[0] = idx_buf;
1973 2276 : move16();
1974 2276 : indices[1] = idx_buf + maxC * stagesVQ;
1975 2276 : move16();
1976 2276 : resid[0] = resid_buf;
1977 2276 : move16();
1978 2276 : resid[1] = resid_buf + maxC * N;
1979 2276 : move16();
1980 2276 : dist[0] = dist_buf;
1981 2276 : move16();
1982 2276 : dist[1] = dist_buf + maxC;
1983 2276 : move16();
1984 :
1985 2276 : set16_fx( idx_buf, 0, ( const Word16 )( 2 * stagesVQ * maxC ) );
1986 2276 : set16_fx( parents, 0, maxC );
1987 :
1988 : /* Set up inital distance vector */
1989 2276 : L_tmp = L_deposit_l( 0 );
1990 38692 : FOR( j = 0; j < N; j++ )
1991 : {
1992 36416 : L_tmp1 = L_shl_o( L_mult0( u[j], w[j] ), 7, &Overflow ); /*x2.56 + Q8 + Q7 */
1993 36416 : L_tmp1 = Mult_32_16( L_tmp1, u[j] ); /*x2.56 + Q15 + x2.56 -Q15 */
1994 36416 : L_tmp = L_add( L_tmp, L_tmp1 ); /*Q0 + x2.56 +x2.56 */
1995 : }
1996 2276 : set32_fx( dist[1], L_tmp, maxC );
1997 :
1998 : /* Set up initial error (residual) vectors */
1999 2276 : pTmp = resid[1];
2000 2276 : move16();
2001 6828 : FOR( c = 0; c < maxC; c++ )
2002 : {
2003 4552 : Copy( u, pTmp, N );
2004 4552 : pTmp += N;
2005 : }
2006 :
2007 : /*----------------------------------------------------------------*
2008 : * LSF quantization
2009 : *----------------------------------------------------------------*/
2010 :
2011 : /* Loop over all stages */
2012 2276 : m = 1;
2013 2276 : move16();
2014 5097 : FOR( s = 0; s < stagesVQ; s++ )
2015 : {
2016 : /* set codebook pointer to point to first stage */
2017 2821 : cbp = cb[s];
2018 2821 : move16();
2019 :
2020 : /* save pointer to the beginning of the current stage */
2021 2821 : cb_stage = cbp;
2022 2821 : move16();
2023 :
2024 : /* swap pointers to parent and current nodes */
2025 2821 : pTmp_short = indices[0];
2026 2821 : indices[0] = indices[1];
2027 2821 : move16();
2028 2821 : indices[1] = pTmp_short;
2029 2821 : move16();
2030 :
2031 2821 : pTmp = resid[0];
2032 2821 : resid[0] = resid[1];
2033 2821 : move16();
2034 2821 : resid[1] = pTmp;
2035 2821 : move16();
2036 :
2037 2821 : pTmp32 = dist[0];
2038 2821 : dist[0] = dist[1];
2039 2821 : move32();
2040 2821 : dist[1] = pTmp32;
2041 2821 : move32();
2042 :
2043 : /* p_max points to maximum distortion node (worst of best) */
2044 2821 : p_max = 0;
2045 2821 : move16();
2046 :
2047 : /* set distortions to a large value */
2048 2821 : set32_fx( dist[1], MAXINT32, maxC );
2049 :
2050 69365 : FOR( j = 0; j < levels[s]; j++ )
2051 : {
2052 : /* compute weighted codebook element and its energy */
2053 1131248 : FOR( c2 = 0; c2 < N; c2++ )
2054 : {
2055 1064704 : Tmp[c2] = shl( mult( w[c2], cbp[c2] ), 2 ); /* Q8 + x2.56 -Q15 +Q2 */
2056 1064704 : move16();
2057 : }
2058 :
2059 66544 : en = L_mult( cbp[0], Tmp[0] );
2060 :
2061 1064704 : FOR( c2 = 1; c2 < N; c2++ )
2062 : {
2063 998160 : en = L_mac( en, cbp[c2], Tmp[c2] ); /*x2.56 + x2.56 + Q-5 +Q1 */
2064 : }
2065 66544 : cbp += N;
2066 66544 : move16();
2067 :
2068 : /* iterate over all parent nodes */
2069 140144 : FOR( c = 0; c < m; c++ )
2070 : {
2071 73600 : pTmp = &resid[0][c * N];
2072 73600 : move16();
2073 73600 : L_tmp = L_mult( pTmp[0], Tmp[0] );
2074 1177600 : FOR( c2 = 1; c2 < N; c2++ )
2075 : {
2076 1104000 : L_tmp = L_mac( L_tmp, pTmp[c2], Tmp[c2] ); /* */
2077 : }
2078 :
2079 73600 : L_tmp = L_add( dist[0][c], L_sub( en, L_shl( L_tmp, 1 ) ) );
2080 :
2081 73600 : IF( LE_32( L_tmp, dist[1][p_max] ) )
2082 : {
2083 : /* replace worst */
2084 21041 : dist[1][p_max] = L_tmp;
2085 21041 : move32();
2086 21041 : indices[1][p_max * stagesVQ + s] = j;
2087 21041 : move16();
2088 21041 : parents[p_max] = c;
2089 21041 : move16();
2090 :
2091 : /* limit number of times inner loop is entered */
2092 21041 : IF( LT_16( counter, max_inner ) )
2093 : {
2094 21041 : counter = add( counter, 1 );
2095 21041 : IF( LT_16( counter, max_inner ) )
2096 : {
2097 : /* find new worst */
2098 21041 : p_max = maximum_32_fx( dist[1], maxC, &f_tmp );
2099 : }
2100 : ELSE
2101 : {
2102 : /* find minimum distortion */
2103 0 : p_max = minimum_32_fx( dist[1], maxC, &f_tmp );
2104 : }
2105 : }
2106 : }
2107 : }
2108 : }
2109 :
2110 : /*------------------------------------------------------------*
2111 : * Compute error vectors for each node
2112 : *------------------------------------------------------------*/
2113 2821 : cs = 0;
2114 2821 : move16();
2115 8463 : FOR( c = 0; c < maxC; c++ )
2116 : {
2117 : /* subtract codebook entry from the residual vector of the parent node */
2118 5642 : pTmp = resid[1] + c * N;
2119 5642 : move16();
2120 5642 : Copy( resid[0] + parents[c] * N, pTmp, N );
2121 5642 : Vr_subt( pTmp, cb_stage + ( indices[1][cs + s] ) * N, pTmp, N );
2122 :
2123 : /* get indices that were used for parent node */
2124 5642 : Copy( indices[0] + parents[c] * stagesVQ, indices[1] + cs, s );
2125 5642 : cs = add( cs, stagesVQ );
2126 : }
2127 :
2128 2821 : m = maxC;
2129 2821 : move16();
2130 : }
2131 :
2132 2276 : Copy( indices[1], indices_VQstage, maxC * stagesVQ );
2133 :
2134 2276 : return;
2135 : }
2136 :
2137 300518 : static void first_VQstages_ivas_fx(
2138 : const Word16 *const *cb,
2139 : Word16 u[], /* i : vector to be encoded (prediction and mean removed) */
2140 : Word16 *levels, /* i : number of levels in each stage */
2141 : Word16 stagesVQ, /* i : number of stages */
2142 : Word16 w[], /* i : weights */
2143 : Word16 N, /* i : vector dimension */
2144 : Word16 max_inner, /* i : maximum number of swaps in inner loop */
2145 : Word16 indices_VQstage[] )
2146 : {
2147 : Word16 resid_buf[2 * LSFMBEST * M], *resid[2];
2148 : Word32 dist_buf[2 * LSFMBEST], *dist[2], en;
2149 : Word32 f_tmp, L_tmp, L_tmp1, *pTmp32;
2150 : Word16 Tmp[M], *pTmp, cs;
2151 300518 : Word16 *pTmp_short, idx_buf[2 * LSFMBEST * MAX_VQ_STAGES], parents[LSFMBEST], counter = 0, j,
2152 : m, s, c, c2, p_max, *indices[2];
2153 300518 : Word16 maxC = LSFMBEST;
2154 : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
2155 300518 : Flag Overflow = 0;
2156 : #endif
2157 :
2158 : /*float dd[16];*/
2159 : const Word16 *cb_stage, *cbp;
2160 :
2161 : /* Set pointers to previous (parent) and current node (parent node is indexed [0], current node is indexed [1]) */
2162 300518 : indices[0] = idx_buf;
2163 300518 : move16();
2164 300518 : indices[1] = idx_buf + maxC * stagesVQ;
2165 300518 : move16();
2166 300518 : resid[0] = resid_buf;
2167 300518 : move16();
2168 300518 : resid[1] = resid_buf + maxC * N;
2169 300518 : move16();
2170 300518 : dist[0] = dist_buf;
2171 300518 : move16();
2172 300518 : dist[1] = dist_buf + maxC;
2173 300518 : move16();
2174 :
2175 300518 : set16_fx( idx_buf, 0, ( const Word16 )( 2 * stagesVQ * maxC ) );
2176 300518 : set16_fx( parents, 0, maxC );
2177 :
2178 : /* Set up inital distance vector */
2179 300518 : L_tmp = L_deposit_l( 0 );
2180 5108806 : FOR( j = 0; j < N; j++ )
2181 : {
2182 4808288 : L_tmp1 = L_shl_o( L_mult0( u[j], w[j] ), 7, &Overflow ); /*x2.56 + Q8 + Q7 */
2183 4808288 : L_tmp1 = Mult_32_16( L_tmp1, u[j] ); /*x2.56 + Q15 + x2.56 -Q15 */
2184 4808288 : L_tmp = L_add( L_tmp, L_tmp1 ); /*Q0 + x2.56 +x2.56 */
2185 : }
2186 300518 : set32_fx( dist[1], L_shr( L_tmp, 1 ), maxC ); /*Q-1 + x2.56 +x2.56 */
2187 :
2188 : /* Set up initial error (residual) vectors */
2189 300518 : pTmp = resid[1];
2190 300518 : move16();
2191 901554 : FOR( c = 0; c < maxC; c++ )
2192 : {
2193 601036 : Copy( u, pTmp, N );
2194 601036 : pTmp += N;
2195 : }
2196 :
2197 : /*----------------------------------------------------------------*
2198 : * LSF quantization
2199 : *----------------------------------------------------------------*/
2200 :
2201 : /* Loop over all stages */
2202 300518 : m = 1;
2203 300518 : move16();
2204 685781 : FOR( s = 0; s < stagesVQ; s++ )
2205 : {
2206 : /* set codebook pointer to point to first stage */
2207 385263 : cbp = cb[s];
2208 385263 : move16();
2209 :
2210 : /* save pointer to the beginning of the current stage */
2211 385263 : cb_stage = cbp;
2212 385263 : move16();
2213 :
2214 : /* swap pointers to parent and current nodes */
2215 385263 : pTmp_short = indices[0];
2216 385263 : indices[0] = indices[1];
2217 385263 : move16();
2218 385263 : indices[1] = pTmp_short;
2219 385263 : move16();
2220 :
2221 385263 : pTmp = resid[0];
2222 385263 : resid[0] = resid[1];
2223 385263 : move16();
2224 385263 : resid[1] = pTmp;
2225 385263 : move16();
2226 :
2227 385263 : pTmp32 = dist[0];
2228 385263 : dist[0] = dist[1];
2229 385263 : move32();
2230 385263 : dist[1] = pTmp32;
2231 385263 : move32();
2232 :
2233 : /* p_max points to maximum distortion node (worst of best) */
2234 385263 : p_max = 0;
2235 385263 : move16();
2236 :
2237 : /* set distortions to a large value */
2238 385263 : set32_fx( dist[1], MAXINT32, maxC );
2239 :
2240 9016175 : FOR( j = 0; j < levels[s]; j++ )
2241 : {
2242 : /* compute weighted codebook element and its energy */
2243 146725504 : FOR( c2 = 0; c2 < N; c2++ )
2244 : {
2245 138094592 : Tmp[c2] = extract_h( L_shl( L_mult0( w[c2], cbp[c2] ), 6 ) ); /* Q8 + x2.56 + q6 -q16 */
2246 138094592 : move16();
2247 : }
2248 :
2249 8630912 : en = L_mult( cbp[0], Tmp[0] ); /*x2.56 + x2.56 + Q-2 +Q1 */
2250 :
2251 138094592 : FOR( c2 = 1; c2 < N; c2++ )
2252 : {
2253 129463680 : en = L_mac( en, cbp[c2], Tmp[c2] ); /*x2.56 + x2.56 + Q-2 +Q1 */
2254 : }
2255 8630912 : cbp += N;
2256 8630912 : move16();
2257 :
2258 : /* iterate over all parent nodes */
2259 18350160 : FOR( c = 0; c < m; c++ )
2260 : {
2261 9719248 : pTmp = &resid[0][c * N]; /*x2.56*/
2262 9719248 : move16();
2263 9719248 : L_tmp = L_mult( pTmp[0], Tmp[0] ); /*x2.56 + x2.56 + Q-2 +Q1 */
2264 155507968 : FOR( c2 = 1; c2 < N; c2++ )
2265 : {
2266 145788720 : L_tmp = L_mac( L_tmp, pTmp[c2], Tmp[c2] ); /*x2.56 + x2.56 + Q-2 +Q1 */
2267 : }
2268 :
2269 9719248 : L_tmp = L_add( dist[0][c], L_sub( en, L_shl( L_tmp, 1 ) ) ); /*x2.56 + x2.56 -1*/
2270 :
2271 9719248 : IF( LE_32( L_tmp, dist[1][p_max] ) )
2272 : {
2273 : /* replace worst */
2274 2819677 : dist[1][p_max] = L_tmp;
2275 2819677 : move32();
2276 2819677 : indices[1][p_max * stagesVQ + s] = j;
2277 2819677 : move16();
2278 2819677 : parents[p_max] = c;
2279 2819677 : move16();
2280 :
2281 : /* limit number of times inner loop is entered */
2282 2819677 : IF( LT_16( counter, max_inner ) )
2283 : {
2284 2819677 : counter = add( counter, 1 );
2285 2819677 : IF( LT_16( counter, max_inner ) )
2286 : {
2287 : /* find new worst */
2288 2819677 : p_max = maximum_32_fx( dist[1], maxC, &f_tmp );
2289 : }
2290 : ELSE
2291 : {
2292 : /* find minimum distortion */
2293 0 : p_max = minimum_32_fx( dist[1], maxC, &f_tmp );
2294 : }
2295 : }
2296 : }
2297 : }
2298 : }
2299 :
2300 : /*------------------------------------------------------------*
2301 : * Compute error vectors for each node
2302 : *------------------------------------------------------------*/
2303 385263 : cs = 0;
2304 385263 : move16();
2305 1155789 : FOR( c = 0; c < maxC; c++ )
2306 : {
2307 : /* subtract codebook entry from the residual vector of the parent node */
2308 770526 : pTmp = resid[1] + c * N;
2309 770526 : move16();
2310 770526 : Copy( resid[0] + parents[c] * N, pTmp, N );
2311 770526 : Vr_subt( pTmp, cb_stage + ( indices[1][cs + s] ) * N, pTmp, N );
2312 :
2313 : /* get indices that were used for parent node */
2314 770526 : Copy( indices[0] + parents[c] * stagesVQ, indices[1] + cs, s );
2315 770526 : cs = add( cs, stagesVQ );
2316 : }
2317 :
2318 385263 : m = maxC;
2319 385263 : move16();
2320 : }
2321 :
2322 300518 : Copy( indices[1], indices_VQstage, maxC * stagesVQ );
2323 :
2324 300518 : return;
2325 : }
2326 : /*---------------------------------------------------------------------------
2327 : * vq_enc_lsf_lvq()
2328 : *
2329 : * Multi-stage VQ encoder for LSF quantization. Trained codebooks are used in initial stages
2330 : * and lattice-VQ quantization is applied on residual vector in other stages.
2331 : *
2332 : * Note:
2333 : * Compared to normal multistage VQ resulting LSF vector is reordered to ascending order before
2334 : * weighted error calculation (spectral distortion) at the final stage.
2335 : *
2336 : * Returns:
2337 : * Weighted error
2338 : *--------------------------------------------------------------------------*/
2339 :
2340 2535 : static Word32 vq_lvq_lsf_enc(
2341 : Word16 pred_flag,
2342 : Word16 mode,
2343 : Word16 u[], // x2.56
2344 : Word16 *levels,
2345 : Word16 stages,
2346 : Word16 w[], // Q8
2347 : Word16 Idx[],
2348 : const Word16 *lsf,
2349 : const Word16 *pred,
2350 : Word32 p_offset_scale1[][MAX_NO_SCALES + 1],
2351 : Word32 p_offset_scale2[][MAX_NO_SCALES + 1],
2352 : Word16 p_no_scales[][2],
2353 : Word16 *resq,
2354 : Word16 *lsfq )
2355 : {
2356 : Word16 i;
2357 : const Word16 *const *cb, *cb_stage;
2358 : Word16 cand[LSFMBEST][M];
2359 2535 : Word16 maxC = LSFMBEST, stagesVQ;
2360 : Word16 mode_glb, j, indices_firstVQ[LSFMBEST * MAX_VQ_STAGES], c2;
2361 : Word32 e[LSFMBEST], L_tmp, L_ftmp;
2362 : Word16 quant[LSFMBEST][M], diff[M], dd[M];
2363 : Word16 lat_cv[LSFMBEST][M];
2364 : Word16 idx_lead[LSFMBEST][2], idx_scale[LSFMBEST][2];
2365 : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
2366 2535 : Flag Overflow = 0;
2367 : #endif
2368 :
2369 2535 : stagesVQ = sub( stages, 1 );
2370 : /* Codebook selection */
2371 2535 : IF( pred_flag == 0 ) /* safety net*/
2372 : {
2373 597 : cb = &Quantizers_fx[CB_lsf[mode]];
2374 597 : move16();
2375 597 : mode_glb = add( offset_lvq_modes_SN_fx[mode], offset_in_lvq_mode_SN_fx[mode][( levels[stagesVQ] - min_lat_bits_SN_fx[mode] )] ); // Q0
2376 : }
2377 : ELSE /* predictive */
2378 : {
2379 1938 : cb = &Quantizers_p_fx[CB_p_lsf[mode]];
2380 1938 : move16();
2381 1938 : mode_glb = add( offset_lvq_modes_pred_fx[mode], offset_in_lvq_mode_pred_fx[mode][( levels[stagesVQ] - min_lat_bits_pred_fx[mode] )] ); // Q0
2382 : }
2383 2535 : IF( stagesVQ > 0 )
2384 : {
2385 : /* first VQ stages */
2386 2276 : first_VQstages( cb, u, levels, stagesVQ, w, M, MSVQ_MAXCNT, indices_firstVQ );
2387 : }
2388 :
2389 :
2390 7605 : FOR( i = 0; i < maxC; i++ )
2391 : {
2392 5070 : Copy( pred, cand[i], M );
2393 10712 : FOR( j = 0; j < stagesVQ; j++ )
2394 : {
2395 5642 : Idx[j] = indices_firstVQ[i * stagesVQ + j];
2396 5642 : move16();
2397 : }
2398 :
2399 10712 : FOR( j = 0; j < stagesVQ; j++ )
2400 : {
2401 5642 : cb_stage = cb[j];
2402 5642 : move16();
2403 5642 : Vr_add( cand[i], cb_stage + Idx[j] * M, cand[i], M );
2404 : }
2405 :
2406 : /* LVQ quantization */
2407 5070 : Vr_subt( lsf, cand[i], dd, M );
2408 5070 : mslvq_fx( dd, quant[i], lat_cv[i], idx_lead[i], idx_scale[i], w, mode, mode_glb, pred_flag, p_no_scales );
2409 5070 : Vr_add( cand[i], quant[i], cand[i], M );
2410 :
2411 : /* arrange the LSF candidate vector prior to selection to an ascending order*/
2412 5070 : sort_fx( cand[i], 0, M - 1 );
2413 :
2414 : /* calculate the spectral distortion using weighted MSE of sorted LSF vector*/
2415 5070 : Vr_subt( cand[i], lsf, diff, M );
2416 86190 : FOR( j = 0; j < M; j++ )
2417 : {
2418 81120 : diff[j] = shl_o( diff[j], 4, &Overflow );
2419 81120 : move16();
2420 : }
2421 5070 : L_tmp = L_mult( mult( diff[0], shl_o( w[0], 1, &Overflow ) ), diff[0] ); /*(2.56+Q5+ Q10 -Q15) + 2.56+ Q5 + Q1 = 2.56 + 2.56 + Q6 */
2422 81120 : FOR( j = 1; j < M; j++ )
2423 : {
2424 76050 : L_tmp = L_mac( L_tmp, mult( diff[j], shl_o( w[j], 1, &Overflow ) ), diff[j] ); /*(2.56+Q5+ Q10 -Q15) + 2.56+ Q5 + Q1 = 2.56 + 2.56 + Q6 */
2425 : }
2426 5070 : e[i] = L_tmp; /*(2.56+Q5+ Q10 -Q15) + 2.56+ Q5 + Q1 = 2.56 + 2.56 + Q6 */
2427 5070 : move32();
2428 : }
2429 :
2430 : /* find the optimal candidate */
2431 2535 : c2 = minimum_32_fx( e, maxC, &L_ftmp );
2432 2535 : set16_fx( resq, 0, M );
2433 5356 : FOR( j = 0; j < stagesVQ; j++ )
2434 : {
2435 2821 : Idx[j] = indices_firstVQ[c2 * stagesVQ + j];
2436 2821 : move16();
2437 2821 : cb_stage = cb[j];
2438 2821 : move16();
2439 2821 : Vr_add( resq, cb_stage + Idx[j] * M, resq, M );
2440 : }
2441 2535 : Vr_add( resq, quant[c2], resq, M ); /* quantized prediction residual */
2442 2535 : Copy( cand[c2], lsfq, M );
2443 2535 : index_lvq_fx( lat_cv[c2], idx_lead[c2], idx_scale[c2], mode_glb, &Idx[stagesVQ],
2444 : &p_offset_scale1[0][0], &p_offset_scale2[0][0], &p_no_scales[0][0] );
2445 :
2446 2535 : return e[c2];
2447 : }
2448 :
2449 343873 : static Word32 vq_lvq_lsf_enc_ivas_fx(
2450 : Word16 pred_flag,
2451 : Word16 mode,
2452 : Word16 u[], // x2.56
2453 : Word16 *levels,
2454 : Word16 stages,
2455 : Word16 w[], // Q8
2456 : Word16 Idx[],
2457 : const Word16 *lsf,
2458 : const Word16 *pred,
2459 : Word16 *resq,
2460 : Word16 *lsfq )
2461 : {
2462 : Word16 i;
2463 : const Word16 *const *cb, *cb_stage;
2464 : Word16 cand[LSFMBEST][M];
2465 343873 : Word16 maxC = LSFMBEST, stagesVQ;
2466 : Word16 mode_glb, j, indices_firstVQ[LSFMBEST * MAX_VQ_STAGES], c2;
2467 : Word32 e[LSFMBEST], L_tmp, L_ftmp;
2468 : Word16 quant[LSFMBEST][M], diff[M], dd[M];
2469 : Word16 lat_cv[LSFMBEST][M];
2470 : Word16 idx_lead[LSFMBEST][2], idx_scale[LSFMBEST][2];
2471 : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
2472 343873 : Flag Overflow = 0;
2473 : #endif
2474 :
2475 343873 : stagesVQ = sub( stages, 1 );
2476 : /* Codebook selection */
2477 343873 : IF( pred_flag == 0 ) /* safety net*/
2478 : {
2479 90645 : cb = &Quantizers_fx[CB_lsf[mode]];
2480 90645 : IF( LT_16( mode, 6 ) )
2481 : {
2482 0 : move16();
2483 0 : mode_glb = add( offset_lvq_modes_SN[mode], offset_in_lvq_mode_SN[mode][( levels[stagesVQ] - min_lat_bits_SN[mode] )] );
2484 : }
2485 : ELSE
2486 : {
2487 90645 : move16();
2488 90645 : mode_glb = add( offset_lvq_modes_SN[mode], levels[stagesVQ] - min_lat_bits_SN[mode] );
2489 : }
2490 : }
2491 : ELSE /* predictive */
2492 : {
2493 253228 : cb = &Quantizers_p_fx[CB_p_lsf[mode]];
2494 253228 : IF( LT_16( mode, 6 ) || EQ_16( mode, 12 ) )
2495 : {
2496 5253 : move16();
2497 5253 : mode_glb = add( offset_lvq_modes_pred[mode], offset_in_lvq_mode_pred[mode][( levels[stagesVQ] - min_lat_bits_pred_fx[mode] )] );
2498 : }
2499 : ELSE
2500 : {
2501 247975 : move16();
2502 247975 : mode_glb = sub( add( offset_lvq_modes_pred[mode], levels[stagesVQ] ), min_lat_bits_pred_fx[mode] );
2503 : }
2504 : }
2505 :
2506 343873 : IF( stagesVQ > 0 )
2507 : {
2508 : /* first VQ stages */
2509 300518 : first_VQstages_ivas_fx( cb, u, levels, stagesVQ, w, M, MSVQ_MAXCNT, indices_firstVQ );
2510 : }
2511 :
2512 :
2513 1031619 : FOR( i = 0; i < maxC; i++ )
2514 : {
2515 687746 : Copy( pred, cand[i], M );
2516 1458272 : FOR( j = 0; j < stagesVQ; j++ )
2517 : {
2518 770526 : Idx[j] = indices_firstVQ[i * stagesVQ + j];
2519 770526 : move16();
2520 : }
2521 :
2522 1458272 : FOR( j = 0; j < stagesVQ; j++ )
2523 : {
2524 770526 : cb_stage = cb[j];
2525 770526 : move16();
2526 770526 : Vr_add( cand[i], cb_stage + Idx[j] * M, cand[i], M );
2527 : }
2528 :
2529 : /* LVQ quantization */
2530 687746 : Vr_subt( lsf, cand[i], dd, M );
2531 687746 : mslvq_ivas_16( dd, quant[i], lat_cv[i], idx_lead[i], idx_scale[i], w, mode, mode_glb, pred_flag );
2532 687746 : Vr_add( cand[i], quant[i], cand[i], M );
2533 :
2534 : /* arrange the LSF candidate vector prior to selection to an ascending order*/
2535 687746 : sort_fx( cand[i], 0, M - 1 );
2536 :
2537 : /* calculate the spectral distortion using weighted MSE of sorted LSF vector*/
2538 687746 : Vr_subt( cand[i], lsf, diff, M );
2539 11691682 : FOR( j = 0; j < M; j++ )
2540 : {
2541 11003936 : diff[j] = shl_o( diff[j], 4, &Overflow );
2542 11003936 : move16();
2543 : }
2544 687746 : L_tmp = L_mult( mult( diff[0], shl_o( w[0], 1, &Overflow ) ), diff[0] ); /*(2.56+Q5+ Q10 -Q15) + 2.56+ Q5 + Q1 = 2.56 + 2.56 + Q6 */
2545 11003936 : FOR( j = 1; j < M; j++ )
2546 : {
2547 10316190 : L_tmp = L_mac( L_tmp, mult( diff[j], shl_o( w[j], 1, &Overflow ) ), diff[j] ); /*(2.56+Q5+ Q10 -Q15) + 2.56+ Q5 + Q1 = 2.56 + 2.56 + Q6 */
2548 : }
2549 687746 : e[i] = L_tmp;
2550 687746 : move32();
2551 : }
2552 :
2553 : /* find the optimal candidate */
2554 343873 : c2 = minimum_32_fx( e, maxC, &L_ftmp );
2555 343873 : set16_fx( resq, 0, M );
2556 729136 : FOR( j = 0; j < stagesVQ; j++ )
2557 : {
2558 385263 : Idx[j] = indices_firstVQ[c2 * stagesVQ + j];
2559 385263 : move16();
2560 385263 : cb_stage = cb[j];
2561 385263 : move16();
2562 385263 : Vr_add( resq, cb_stage + Idx[j] * M, resq, M );
2563 : }
2564 343873 : Vr_add( resq, quant[c2], resq, M ); /* quantized prediction residual */
2565 343873 : Copy( cand[c2], lsfq, M );
2566 343873 : index_lvq_ivas_fx( lat_cv[c2], idx_lead[c2], idx_scale[c2], mode_glb, &Idx[stagesVQ], pred_flag );
2567 :
2568 343873 : return e[c2];
2569 : }
2570 625 : static void BcTcvq_1st_fx(
2571 : Word16 x_fx[][2], /*x2.56*/
2572 : const Word16 CB_fx[][128][2], /*x2.56*/
2573 : Word16 s[][16],
2574 : Word16 c[][16],
2575 : Word32 cDist_fx[][16], /*2.56*2.56*Q(-5 - 2)*/
2576 : Word16 Q_fx[][16][2],
2577 : Word16 W_fx[][2] /*Q8*/
2578 : )
2579 : {
2580 : Word16 state, prev_state;
2581 : Word16 index, bestCode;
2582 : Word32 dist_fx, minDist_fx;
2583 : Word16 temp16_fx;
2584 :
2585 5625 : FOR( state = 0; state < NUM_STATE; state += 2 )
2586 : {
2587 5000 : prev_state = NTRANS[0][state];
2588 5000 : move16();
2589 5000 : index = NTRANS[2][state];
2590 5000 : move16();
2591 5000 : temp16_fx = sub( x_fx[0][0], CB_fx[0][index][0] );
2592 5000 : minDist_fx = Mult_32_16( L_mult0( temp16_fx, temp16_fx ), W_fx[0][0] ); /* 2.56*2.56*Q(-5) */
2593 5000 : temp16_fx = sub( x_fx[0][1], CB_fx[0][index][1] );
2594 5000 : minDist_fx = L_add( minDist_fx, Mult_32_16( L_mult0( temp16_fx, temp16_fx ), W_fx[0][1] ) ); /* 2.56*2.56*Q(-5) */
2595 5000 : bestCode = index;
2596 5000 : move16();
2597 :
2598 80000 : FOR( index = index + 8; index < 128; index += 8 )
2599 : {
2600 75000 : temp16_fx = sub( x_fx[0][0], CB_fx[0][index][0] );
2601 75000 : dist_fx = Mult_32_16( L_mult0( temp16_fx, temp16_fx ), W_fx[0][0] ); /* 2.56*2.56*Q(-5) */
2602 75000 : temp16_fx = sub( x_fx[0][1], CB_fx[0][index][1] );
2603 75000 : dist_fx = L_add( dist_fx, Mult_32_16( L_mult0( temp16_fx, temp16_fx ), W_fx[0][1] ) ); /* 2.56*2.56*Q(-5) */
2604 :
2605 75000 : if ( LT_32( dist_fx, minDist_fx ) )
2606 : {
2607 16500 : bestCode = index;
2608 16500 : move16();
2609 : }
2610 75000 : minDist_fx = L_min( minDist_fx, dist_fx );
2611 : }
2612 :
2613 : /* Update */
2614 5000 : s[0][state] = prev_state;
2615 5000 : move16();
2616 5000 : c[0][state] = bestCode;
2617 5000 : move16();
2618 :
2619 5000 : cDist_fx[0][state] = L_shr( minDist_fx, 2 );
2620 5000 : move32(); /*2.56*2.56*Q(-5 - 2)*/
2621 5000 : Q_fx[0][state][0] = CB_fx[0][bestCode][0];
2622 5000 : move16();
2623 5000 : Q_fx[0][state][1] = CB_fx[0][bestCode][1];
2624 5000 : move16();
2625 : }
2626 :
2627 625 : return;
2628 : }
2629 :
2630 :
2631 625 : static void BcTcvq_2nd_fx(
2632 : Word16 x_fx[][2], /*x2.56*/
2633 : const Word16 CB_fx[][128][2], /*x2.56*/
2634 : Word16 s[][16],
2635 : Word16 c[][16],
2636 : Word32 cDist_fx[][16], /*2.56*2.56*Q(-5 - 2) */
2637 : Word16 Q_fx[][16][2], /*x2.56*/
2638 : Word16 W_fx[][2], /*Q8*/
2639 : const Word16 itc_fx[][2][2] /*Q15*/
2640 : )
2641 : {
2642 : Word16 state, prev_state;
2643 : Word16 index, bestCode;
2644 : Word32 dist_fx, minDist_fx;
2645 : Word16 pred_fx[N_DIM], target_fx[N_DIM];
2646 : Word16 temp16_fx;
2647 :
2648 10625 : FOR( state = 0; state < NUM_STATE; state++ )
2649 : {
2650 10000 : prev_state = NTRANS[0][state];
2651 10000 : move16();
2652 10000 : index = NTRANS[2][state];
2653 10000 : move16();
2654 :
2655 : /* Prediction */
2656 10000 : pred_fx[0] = add( mult_r( itc_fx[0][0][0], Q_fx[0][prev_state][0] ), mult_r( itc_fx[0][0][1], Q_fx[0][prev_state][1] ) );
2657 10000 : move16();
2658 10000 : pred_fx[1] = add( mult_r( itc_fx[0][1][0], Q_fx[0][prev_state][0] ), mult_r( itc_fx[0][1][1], Q_fx[0][prev_state][1] ) );
2659 10000 : move16();
2660 10000 : target_fx[0] = sub( x_fx[1][0], pred_fx[0] );
2661 10000 : move16(); /* x2.65 */
2662 10000 : target_fx[1] = sub( x_fx[1][1], pred_fx[1] );
2663 10000 : move16(); /* x2.65 */
2664 :
2665 10000 : temp16_fx = sub( target_fx[0], CB_fx[1][index][0] );
2666 10000 : minDist_fx = Mult_32_16( L_mult0( temp16_fx, temp16_fx ), W_fx[1][0] ); /* 2.65*2.65*Q(-5) */
2667 10000 : temp16_fx = sub( target_fx[1], CB_fx[1][index][1] );
2668 10000 : minDist_fx = L_add( minDist_fx, Mult_32_16( L_mult0( temp16_fx, temp16_fx ), W_fx[1][1] ) ); /* 2.65*2.65*Q(-5) */
2669 :
2670 10000 : bestCode = index;
2671 10000 : move16();
2672 :
2673 160000 : FOR( index = index + 8; index < 128; index += 8 )
2674 : {
2675 150000 : temp16_fx = sub( target_fx[0], CB_fx[1][index][0] );
2676 150000 : dist_fx = Mult_32_16( L_mult0( temp16_fx, temp16_fx ), W_fx[1][0] ); /* 2.65*2.65*Q(-5) */
2677 150000 : temp16_fx = sub( target_fx[1], CB_fx[1][index][1] );
2678 150000 : dist_fx = L_add( dist_fx, Mult_32_16( L_mult0( temp16_fx, temp16_fx ), W_fx[1][1] ) ); /* 2.65*2.65*Q(-5) */
2679 :
2680 150000 : if ( LT_32( dist_fx, minDist_fx ) )
2681 : {
2682 29077 : bestCode = index;
2683 29077 : move16();
2684 : }
2685 150000 : minDist_fx = L_min( minDist_fx, dist_fx );
2686 : }
2687 :
2688 : /* Update */
2689 10000 : s[1][state] = prev_state;
2690 10000 : move16();
2691 10000 : c[1][state] = bestCode;
2692 10000 : move16();
2693 :
2694 10000 : cDist_fx[1][state] = L_add( cDist_fx[0][prev_state], L_shr( minDist_fx, 2 ) );
2695 10000 : move32(); /* 2.56*2.56*Q(-5 - 2) */
2696 10000 : Q_fx[1][state][0] = add( CB_fx[1][bestCode][0], pred_fx[0] );
2697 10000 : move16();
2698 10000 : Q_fx[1][state][1] = add( CB_fx[1][bestCode][1], pred_fx[1] );
2699 10000 : move16();
2700 : }
2701 :
2702 625 : return;
2703 : }
2704 :
2705 1250 : static void BcTcvq_SubBlock_fx(
2706 : Word16 x_fx[][2], /*x2.56*/
2707 : const Word16 CB_fx[][64][2], /*x2.56*/
2708 : Word16 s[][16],
2709 : Word16 c[][16],
2710 : Word32 cDist_fx[][16], /*2.56*2.56*Q(-5 - 2)*/
2711 : Word16 Q_fx[][16][2],
2712 : Word16 stage,
2713 : Word16 W_fx[][2], /*Q10*/
2714 : const Word16 itc_fx[][2][2] /*Q15*/
2715 : )
2716 : {
2717 : Word16 stage1, stage2, state, prev_state, branch;
2718 : Word16 index, bestCode, brCode[N_DIM];
2719 : Word16 temp16_fx;
2720 :
2721 : Word32 dist_fx, minDist_fx, brDist_fx[N_DIM];
2722 : Word16 pred_fx[N_DIM], target_fx[N_DIM], brQuant_fx[N_DIM][N_DIM];
2723 :
2724 1250 : stage1 = sub( stage, 1 );
2725 1250 : stage2 = sub( stage, 2 );
2726 :
2727 21250 : FOR( state = 0; state < NUM_STATE; state++ )
2728 : {
2729 :
2730 : /* 1st branch search */
2731 20000 : prev_state = NTRANS[0][state];
2732 20000 : move16();
2733 20000 : index = NTRANS[2][state];
2734 20000 : move16();
2735 :
2736 : /* Prediction */
2737 20000 : pred_fx[0] = add( mult_r( itc_fx[stage1][0][0], Q_fx[stage1][prev_state][0] ), mult_r( itc_fx[stage1][0][1], Q_fx[stage1][prev_state][1] ) );
2738 20000 : move16();
2739 20000 : pred_fx[1] = add( mult_r( itc_fx[stage1][1][0], Q_fx[stage1][prev_state][0] ), mult_r( itc_fx[stage1][1][1], Q_fx[stage1][prev_state][1] ) );
2740 20000 : move16();
2741 20000 : target_fx[0] = sub( x_fx[stage][0], pred_fx[0] );
2742 20000 : move16();
2743 20000 : target_fx[1] = sub( x_fx[stage][1], pred_fx[1] );
2744 20000 : move16();
2745 :
2746 20000 : temp16_fx = sub( target_fx[0], CB_fx[stage2][index][0] );
2747 20000 : minDist_fx = Mult_32_16( L_mult0( temp16_fx, temp16_fx ), W_fx[stage][0] ); /* 2.65*2.65*Q(-5) */
2748 20000 : temp16_fx = sub( target_fx[1], CB_fx[stage2][index][1] );
2749 20000 : minDist_fx = L_add( minDist_fx, Mult_32_16( L_mult0( temp16_fx, temp16_fx ), W_fx[stage][1] ) ); /* 2.65*2.65*Q(-5) */
2750 :
2751 20000 : bestCode = index;
2752 20000 : move16();
2753 :
2754 160000 : FOR( index = index + 8; index < 64; index += 8 )
2755 : {
2756 140000 : temp16_fx = sub( target_fx[0], CB_fx[stage2][index][0] );
2757 140000 : dist_fx = Mult_32_16( L_mult0( temp16_fx, temp16_fx ), W_fx[stage][0] );
2758 140000 : temp16_fx = sub( target_fx[1], CB_fx[stage2][index][1] );
2759 140000 : dist_fx = L_add( dist_fx, Mult_32_16( L_mult0( temp16_fx, temp16_fx ), W_fx[stage][1] ) );
2760 :
2761 140000 : if ( LT_32( dist_fx, minDist_fx ) )
2762 : {
2763 38892 : bestCode = index;
2764 38892 : move16();
2765 : }
2766 140000 : minDist_fx = L_min( minDist_fx, dist_fx );
2767 : }
2768 :
2769 20000 : brCode[0] = bestCode;
2770 20000 : move16();
2771 :
2772 20000 : brDist_fx[0] = L_add( cDist_fx[stage1][prev_state], L_shr( minDist_fx, 2 ) );
2773 20000 : move32();
2774 20000 : brQuant_fx[0][0] = add( CB_fx[stage2][bestCode][0], pred_fx[0] );
2775 20000 : move16();
2776 20000 : brQuant_fx[0][1] = add( CB_fx[stage2][bestCode][1], pred_fx[1] );
2777 20000 : move16();
2778 :
2779 : /* 2nd branch search */
2780 20000 : prev_state = NTRANS[1][state];
2781 20000 : move16();
2782 20000 : index = NTRANS[3][state];
2783 20000 : move16();
2784 :
2785 : /* Prediction */
2786 20000 : pred_fx[0] = add( mult_r( itc_fx[stage1][0][0], Q_fx[stage1][prev_state][0] ), mult_r( itc_fx[stage1][0][1], Q_fx[stage1][prev_state][1] ) );
2787 20000 : move16();
2788 20000 : pred_fx[1] = add( mult_r( itc_fx[stage1][1][0], Q_fx[stage1][prev_state][0] ), mult_r( itc_fx[stage1][1][1], Q_fx[stage1][prev_state][1] ) );
2789 20000 : move16();
2790 20000 : target_fx[0] = sub( x_fx[stage][0], pred_fx[0] );
2791 20000 : move16();
2792 20000 : target_fx[1] = sub( x_fx[stage][1], pred_fx[1] );
2793 20000 : move16();
2794 :
2795 20000 : temp16_fx = sub( target_fx[0], CB_fx[stage2][index][0] );
2796 20000 : minDist_fx = Mult_32_16( L_mult0( temp16_fx, temp16_fx ), W_fx[stage][0] ); /* 2.65*2.65*Q(-5) */
2797 20000 : temp16_fx = sub( target_fx[1], CB_fx[stage2][index][1] );
2798 20000 : minDist_fx = L_add( minDist_fx, Mult_32_16( L_mult0( temp16_fx, temp16_fx ), W_fx[stage][1] ) ); /* 2.65*2.65*Q(-5) */
2799 :
2800 20000 : bestCode = index;
2801 20000 : move16();
2802 :
2803 160000 : FOR( index = index + 8; index < 64; index += 8 )
2804 : {
2805 140000 : temp16_fx = sub( target_fx[0], CB_fx[stage2][index][0] );
2806 140000 : dist_fx = Mult_32_16( L_mult0( temp16_fx, temp16_fx ), W_fx[stage][0] );
2807 140000 : temp16_fx = sub( target_fx[1], CB_fx[stage2][index][1] );
2808 140000 : dist_fx = L_add( dist_fx, Mult_32_16( L_mult0( temp16_fx, temp16_fx ), W_fx[stage][1] ) );
2809 140000 : if ( LT_32( dist_fx, minDist_fx ) )
2810 : {
2811 39360 : bestCode = index;
2812 39360 : move16();
2813 : }
2814 140000 : minDist_fx = L_min( minDist_fx, dist_fx );
2815 : }
2816 :
2817 20000 : brCode[1] = bestCode;
2818 20000 : move16();
2819 :
2820 20000 : brDist_fx[1] = L_add( cDist_fx[stage1][prev_state], L_shr( minDist_fx, 2 ) );
2821 20000 : move32();
2822 20000 : brQuant_fx[1][0] = add( CB_fx[stage2][bestCode][0], pred_fx[0] );
2823 20000 : move16();
2824 20000 : brQuant_fx[1][1] = add( CB_fx[stage2][bestCode][1], pred_fx[1] );
2825 20000 : move16();
2826 :
2827 : /* Select Best branch */
2828 20000 : branch = 1;
2829 20000 : move16();
2830 :
2831 20000 : if ( LE_32( brDist_fx[0], brDist_fx[1] ) )
2832 : {
2833 9422 : branch = 0;
2834 9422 : move16();
2835 : }
2836 :
2837 : /* Update */
2838 20000 : s[stage][state] = NTRANS[branch][state];
2839 20000 : move16();
2840 20000 : c[stage][state] = brCode[branch];
2841 20000 : move16();
2842 :
2843 20000 : cDist_fx[stage][state] = brDist_fx[branch];
2844 20000 : move32();
2845 20000 : Q_fx[stage][state][0] = brQuant_fx[branch][0];
2846 20000 : move16();
2847 20000 : Q_fx[stage][state][1] = brQuant_fx[branch][1];
2848 20000 : move16();
2849 : }
2850 :
2851 1250 : return;
2852 : }
2853 :
2854 160000 : static Word32 BcTcvq_FixSearch_fx(
2855 : Word16 x_fx[][2], /*x2.56*/
2856 : const Word16 CB_fx[][32][2], /*x2.56*/
2857 : Word16 c[][4],
2858 : Word16 Q_fx[][16][2],
2859 : const Word16 FixBranch[][4][4],
2860 : Word16 stage,
2861 : Word16 inis,
2862 : Word16 fins,
2863 : Word16 *prev_state,
2864 : Word16 W_fx[][2], /*Q10*/
2865 : const Word16 itc_fx[][2][2] /*Q15*/
2866 : )
2867 : {
2868 : Word16 stage1, stage4, branch;
2869 : Word16 index, bestCode;
2870 : Word32 dist_fx, minDist_fx;
2871 : Word16 pred_fx[N_DIM], target_fx[N_DIM];
2872 : Word16 temp16_fx;
2873 :
2874 160000 : stage1 = sub( stage, 1 );
2875 160000 : stage4 = sub( stage, 4 );
2876 :
2877 160000 : branch = FixBranch[shr( inis, 2 )][fins][stage4];
2878 160000 : move16();
2879 160000 : index = NTRANS2[add( branch, 2 )][*prev_state];
2880 160000 : move16();
2881 :
2882 : /* Prediction */
2883 160000 : pred_fx[0] = add( mult_r( itc_fx[stage1][0][0], Q_fx[stage1][*prev_state][0] ), mult_r( itc_fx[stage1][0][1], Q_fx[stage1][*prev_state][1] ) );
2884 160000 : move16();
2885 160000 : pred_fx[1] = add( mult_r( itc_fx[stage1][1][0], Q_fx[stage1][*prev_state][0] ), mult_r( itc_fx[stage1][1][1], Q_fx[stage1][*prev_state][1] ) );
2886 160000 : move16();
2887 160000 : target_fx[0] = sub( x_fx[stage][0], pred_fx[0] );
2888 160000 : move16();
2889 160000 : target_fx[1] = sub( x_fx[stage][1], pred_fx[1] );
2890 160000 : move16();
2891 :
2892 160000 : temp16_fx = sub( target_fx[0], CB_fx[stage4][index][0] );
2893 160000 : minDist_fx = Mult_32_16( L_mult0( temp16_fx, temp16_fx ), W_fx[stage][0] ); /* 2.65*2.65*Q(-5) */
2894 160000 : temp16_fx = sub( target_fx[1], CB_fx[stage4][index][1] );
2895 160000 : minDist_fx = L_add( minDist_fx, Mult_32_16( L_mult0( temp16_fx, temp16_fx ), W_fx[stage][1] ) ); /* 2.65*2.65*Q(-5) */
2896 :
2897 160000 : bestCode = index;
2898 160000 : move16();
2899 :
2900 640000 : FOR( index = index + 8; index < 32; index += 8 )
2901 : {
2902 480000 : temp16_fx = sub( target_fx[0], CB_fx[stage4][index][0] );
2903 480000 : dist_fx = Mult_32_16( L_mult0( temp16_fx, temp16_fx ), W_fx[stage][0] );
2904 480000 : temp16_fx = sub( target_fx[1], CB_fx[stage4][index][1] );
2905 480000 : dist_fx = L_add( dist_fx, Mult_32_16( L_mult0( temp16_fx, temp16_fx ), W_fx[stage][1] ) );
2906 :
2907 480000 : if ( LT_32( dist_fx, minDist_fx ) )
2908 : {
2909 196072 : bestCode = index;
2910 196072 : move16();
2911 : }
2912 480000 : minDist_fx = L_min( minDist_fx, dist_fx );
2913 : }
2914 :
2915 : /* Update */
2916 160000 : *prev_state = NTRANS2[branch][*prev_state];
2917 160000 : move16();
2918 160000 : c[fins][stage4] = bestCode;
2919 160000 : move16();
2920 :
2921 160000 : Q_fx[stage][*prev_state][0] = add( CB_fx[stage4][bestCode][0], pred_fx[0] );
2922 160000 : move16();
2923 160000 : Q_fx[stage][*prev_state][1] = add( CB_fx[stage4][bestCode][1], pred_fx[1] );
2924 160000 : move16();
2925 :
2926 160000 : minDist_fx = L_shr( minDist_fx, 2 ); /*2.56*2.56*Q(-5 - 2)*/
2927 160000 : return minDist_fx;
2928 : }
2929 625 : static Word16 optimalPath_fx(
2930 : Word32 cDist_fx[][16], /*2.56*2.56*Q(-5 - 2)*/
2931 : Word32 blockDist_fx[], /*2.56*2.56*Q(-5 - 2)*/
2932 : Word16 blockCodeword[][4],
2933 : Word16 bestCodeword[],
2934 : Word16 codeWord[][16],
2935 : Word16 bestState[],
2936 : Word16 preState[][16] )
2937 : {
2938 : Word16 stage, state;
2939 : Word32 opDist_fx[NUM_STATE];
2940 : Word32 minDist_fx;
2941 : Word16 fBlock;
2942 : Word16 prev_state;
2943 :
2944 10625 : FOR( state = 0; state < NUM_STATE; state++ )
2945 : {
2946 10000 : opDist_fx[state] = L_add( L_shr( cDist_fx[3][state], 1 ), L_shr( blockDist_fx[state], 1 ) );
2947 10000 : move32();
2948 : }
2949 :
2950 625 : minDist_fx = L_add( opDist_fx[0], 0 );
2951 625 : fBlock = 0;
2952 625 : move16();
2953 :
2954 10000 : FOR( state = 1; state < NUM_STATE; state++ )
2955 : {
2956 9375 : if ( LT_32( opDist_fx[state], minDist_fx ) )
2957 : {
2958 1655 : fBlock = state;
2959 1655 : move16();
2960 : }
2961 9375 : minDist_fx = L_min( minDist_fx, opDist_fx[state] );
2962 : }
2963 :
2964 625 : prev_state = bestState[4] = fBlock;
2965 625 : move16();
2966 625 : move16();
2967 :
2968 3125 : FOR( stage = N_STAGE_VQ - 5; stage >= 0; stage-- )
2969 : {
2970 2500 : bestCodeword[stage] = codeWord[stage][prev_state];
2971 2500 : move16();
2972 2500 : bestState[stage] = preState[stage][prev_state];
2973 2500 : move16();
2974 2500 : prev_state = bestState[stage];
2975 2500 : move16();
2976 : }
2977 :
2978 3125 : FOR( stage = 0; stage < 4; stage++ )
2979 : {
2980 2500 : bestCodeword[stage + 4] = blockCodeword[fBlock][stage];
2981 2500 : move16();
2982 : }
2983 :
2984 625 : return fBlock;
2985 : }
2986 :
2987 625 : static void quantEnc_fx(
2988 : Word16 *y_fx,
2989 : Word16 c[],
2990 : const Word16 CB_SUB1_fx[][128][2], /*x2.56*/
2991 : const Word16 CB_SUB2_fx[][64][2], /*x2.56*/
2992 : const Word16 CB_SUB3_fx[][32][2], /*x2.56*/
2993 : const Word16 itc_fx[][2][2] // Q15
2994 : )
2995 : {
2996 : Word16 i, j;
2997 : Word16 stage;
2998 : Word16 pred_fx[N_DIM], Y_fx[8][2];
2999 :
3000 : /* stage #1 */
3001 625 : Y_fx[0][0] = CB_SUB1_fx[0][c[0]][0];
3002 625 : move16();
3003 625 : Y_fx[0][1] = CB_SUB1_fx[0][c[0]][1];
3004 625 : move16();
3005 :
3006 : /* stage #2 */
3007 625 : pred_fx[0] = add( mult_r( itc_fx[0][0][0], Y_fx[0][0] ), mult_r( itc_fx[0][0][1], Y_fx[0][1] ) );
3008 625 : move16();
3009 625 : pred_fx[1] = add( mult_r( itc_fx[0][1][0], Y_fx[0][0] ), mult_r( itc_fx[0][1][1], Y_fx[0][1] ) );
3010 625 : move16();
3011 625 : Y_fx[1][0] = add( CB_SUB1_fx[1][c[1]][0], pred_fx[0] );
3012 625 : move16();
3013 625 : Y_fx[1][1] = add( CB_SUB1_fx[1][c[1]][1], pred_fx[1] );
3014 625 : move16();
3015 :
3016 : /* stage #3 - #4 */
3017 1875 : FOR( stage = 2; stage < N_STAGE_VQ - 4; stage++ )
3018 : {
3019 1250 : pred_fx[0] = add( mult_r( itc_fx[stage - 1][0][0], Y_fx[stage - 1][0] ), mult_r( itc_fx[stage - 1][0][1], Y_fx[stage - 1][1] ) );
3020 1250 : move16();
3021 1250 : pred_fx[1] = add( mult_r( itc_fx[stage - 1][1][0], Y_fx[stage - 1][0] ), mult_r( itc_fx[stage - 1][1][1], Y_fx[stage - 1][1] ) );
3022 1250 : move16();
3023 :
3024 1250 : Y_fx[stage][0] = add( CB_SUB2_fx[stage - 2][c[stage]][0], pred_fx[0] );
3025 1250 : move16();
3026 1250 : Y_fx[stage][1] = add( CB_SUB2_fx[stage - 2][c[stage]][1], pred_fx[1] );
3027 1250 : move16();
3028 : }
3029 :
3030 : /* stage #5 - #8 */
3031 3125 : FOR( stage = N_STAGE_VQ - 4; stage < N_STAGE_VQ; stage++ )
3032 : {
3033 2500 : pred_fx[0] = add( mult_r( itc_fx[stage - 1][0][0], Y_fx[stage - 1][0] ), mult_r( itc_fx[stage - 1][0][1], Y_fx[stage - 1][1] ) );
3034 2500 : move16();
3035 2500 : pred_fx[1] = add( mult_r( itc_fx[stage - 1][1][0], Y_fx[stage - 1][0] ), mult_r( itc_fx[stage - 1][1][1], Y_fx[stage - 1][1] ) );
3036 2500 : move16();
3037 :
3038 2500 : Y_fx[stage][0] = add( CB_SUB3_fx[stage - 4][c[stage]][0], pred_fx[0] );
3039 2500 : move16();
3040 2500 : Y_fx[stage][1] = add( CB_SUB3_fx[stage - 4][c[stage]][1], pred_fx[1] );
3041 2500 : move16();
3042 : }
3043 :
3044 : /* Transform Vector to Scalar */
3045 5625 : FOR( i = 0; i < N_STAGE_VQ; i++ )
3046 : {
3047 15000 : FOR( j = 0; j < N_DIM; j++ )
3048 : {
3049 10000 : y_fx[i * N_DIM + j] = Y_fx[i][j];
3050 10000 : move16();
3051 : }
3052 : }
3053 :
3054 625 : return;
3055 : }
3056 :
3057 625 : static void buildCode_fx(
3058 : Word16 *ind,
3059 : Word16 fins,
3060 : Word16 c[],
3061 : Word16 s[] )
3062 : {
3063 : Word16 stage;
3064 : Word16 BrIndex[4];
3065 :
3066 625 : set16_fx( BrIndex, 0, ( N_STAGE_VQ - 4 ) );
3067 :
3068 :
3069 3125 : FOR( stage = N_STAGE_VQ - 4; stage >= 1; stage-- )
3070 : {
3071 2500 : if ( GT_16( s[stage], 7 ) )
3072 : {
3073 1249 : BrIndex[stage - 1] = 1;
3074 1249 : move16();
3075 : }
3076 : }
3077 625 : ind[0] = fins;
3078 625 : move16();
3079 :
3080 : /* stage #1 - #2 */
3081 1875 : FOR( stage = 0; stage < 2; stage++ )
3082 : {
3083 1250 : ind[stage + 1] = shl( BrIndex[stage], 4 );
3084 1250 : move16();
3085 1250 : ind[stage + 1] = add( ind[stage + 1], shr( c[stage], 3 ) );
3086 1250 : move16();
3087 : }
3088 :
3089 : /* stage #3 - #4 */
3090 1875 : FOR( stage = 2; stage < N_STAGE_VQ - 4; stage++ )
3091 : {
3092 1250 : ind[stage + 1] = shl( BrIndex[stage], 3 );
3093 1250 : move16();
3094 1250 : ind[stage + 1] = add( ind[stage + 1], shr( c[stage], 3 ) );
3095 1250 : move16();
3096 : }
3097 :
3098 : /* Stage #5 - #8 */
3099 3125 : FOR( stage = N_STAGE_VQ - 4; stage < N_STAGE_VQ; stage++ )
3100 : {
3101 2500 : ind[stage + 1] = shr( c[stage], 3 );
3102 2500 : move16();
3103 : }
3104 :
3105 625 : return;
3106 : }
3107 625 : static void BcTcvq_fx(
3108 : Word16 snFlag,
3109 : const Word16 *x_fx, // x2.65
3110 : Word16 *y_fx, // x2.65
3111 : const Word16 *weight_fx, // Q8
3112 : Word16 *ind )
3113 : {
3114 : Word16 X_fx[N_STAGE_VQ][N_DIM], W_fx[N_STAGE_VQ][N_DIM];
3115 :
3116 : /* Count Variable */
3117 : Word16 i, j;
3118 :
3119 : /* TCVQ Structure */
3120 : Word16 stage, state, prev_state;
3121 : Word16 preState[N_STAGE_VQ][NUM_STATE];
3122 : Word16 codeWord[N_STAGE_VQ][NUM_STATE];
3123 : Word32 acumDist_fx[N_STAGE_VQ - 4][NUM_STATE];
3124 : Word16 inis, fins, ptr_fins;
3125 : Word16 fBlock;
3126 : Word16 fState[NUM_STATE];
3127 : Word16 fCodeword[4][4];
3128 : Word16 iniBlock[NUM_STATE];
3129 : Word16 blockCodeword[NUM_STATE][4];
3130 :
3131 : /* Prediction variable */
3132 : Word16 quant_fx[N_STAGE_VQ][NUM_STATE][N_DIM];
3133 :
3134 : /* Distortion variable */
3135 : Word32 minDist_fx;
3136 : Word32 fDist_fx;
3137 : Word32 blockDist_fx[NUM_STATE];
3138 :
3139 : /* Decoding variable */
3140 : Word16 bestCodeword[N_STAGE_VQ];
3141 : Word16 bestState[N_STAGE_VQ];
3142 :
3143 : /* Code Share variable */
3144 : const Word16( *TCVQ_CB_SUB1_fx )[128][2], ( *TCVQ_CB_SUB2_fx )[64][2], ( *TCVQ_CB_SUB3_fx )[32][2] /**/;
3145 : const Word16( *IntraCoeff_fx )[2][2];
3146 :
3147 : /* Memoryless Module */
3148 625 : IF( snFlag )
3149 : {
3150 101 : TCVQ_CB_SUB1_fx = SN_TCVQ_CB_SUB1_fx;
3151 101 : TCVQ_CB_SUB2_fx = SN_TCVQ_CB_SUB2_fx;
3152 101 : TCVQ_CB_SUB3_fx = SN_TCVQ_CB_SUB3_fx;
3153 101 : IntraCoeff_fx = SN_IntraCoeff_fx;
3154 : }
3155 : ELSE /* Memory Module */
3156 : {
3157 524 : TCVQ_CB_SUB1_fx = AR_TCVQ_CB_SUB1_fx;
3158 524 : TCVQ_CB_SUB2_fx = AR_TCVQ_CB_SUB2_fx;
3159 524 : TCVQ_CB_SUB3_fx = AR_TCVQ_CB_SUB3_fx;
3160 524 : IntraCoeff_fx = AR_IntraCoeff_fx;
3161 : }
3162 :
3163 : /* Transform Scalar to Vector */
3164 5625 : FOR( i = 0; i < N_STAGE_VQ; i++ )
3165 : {
3166 15000 : FOR( j = 0; j < N_DIM; j++ )
3167 : {
3168 10000 : X_fx[i][j] = x_fx[( N_DIM * i ) + j]; // x2.65
3169 10000 : move16();
3170 10000 : W_fx[i][j] = weight_fx[( N_DIM * i ) + j]; // x2.56
3171 10000 : move16();
3172 : }
3173 : }
3174 :
3175 : /* Initialzie */
3176 3125 : FOR( i = 0; i < N_STAGE_VQ - 4; i++ )
3177 : {
3178 42500 : FOR( j = 0; j < NUM_STATE; j++ )
3179 : {
3180 40000 : acumDist_fx[i][j] = L_deposit_l( 0 );
3181 : }
3182 : }
3183 :
3184 : /* BcTcvq Search */
3185 : /* stage #1 */
3186 625 : BcTcvq_1st_fx( X_fx, TCVQ_CB_SUB1_fx, preState, codeWord, acumDist_fx, quant_fx, W_fx );
3187 :
3188 : /* stage #2 */
3189 625 : BcTcvq_2nd_fx( X_fx, TCVQ_CB_SUB1_fx, preState, codeWord, acumDist_fx, quant_fx, W_fx, IntraCoeff_fx );
3190 :
3191 : /* stage #3 - #4 */
3192 1875 : FOR( stage = 2; stage < N_STAGE_VQ - 4; stage++ )
3193 : {
3194 1250 : BcTcvq_SubBlock_fx( X_fx, TCVQ_CB_SUB2_fx, preState, codeWord, acumDist_fx, quant_fx, stage, W_fx, IntraCoeff_fx );
3195 : }
3196 :
3197 : /* Search initial state at each block */
3198 10625 : FOR( state = 0; state < NUM_STATE; state++ )
3199 : {
3200 10000 : prev_state = state;
3201 10000 : move16();
3202 :
3203 50000 : FOR( stage = N_STAGE_VQ - 5; stage >= 0; stage-- )
3204 : {
3205 40000 : prev_state = preState[stage][prev_state];
3206 40000 : move16();
3207 : }
3208 10000 : iniBlock[state] = prev_state;
3209 10000 : move16();
3210 : }
3211 :
3212 : /* stage #5 - #8 */
3213 10625 : FOR( state = 0; state < NUM_STATE; state++ )
3214 : {
3215 10000 : inis = iniBlock[state];
3216 10000 : move16();
3217 10000 : ptr_fins = shr( inis, 2 );
3218 10000 : minDist_fx = L_add( MAX_32, 0 );
3219 :
3220 50000 : FOR( i = 0; i < 4; i++ )
3221 : {
3222 40000 : fins = add( shl( ptr_fins, 2 ), i );
3223 40000 : prev_state = state;
3224 40000 : move16();
3225 40000 : fDist_fx = BcTcvq_FixSearch_fx( X_fx, TCVQ_CB_SUB3_fx, fCodeword, quant_fx, FixBranch_tbl, N_STAGE_VQ - 4, inis, i, &prev_state, W_fx, IntraCoeff_fx );
3226 :
3227 160000 : FOR( stage = N_STAGE_VQ - 3; stage < N_STAGE_VQ; stage++ )
3228 : {
3229 120000 : fDist_fx = L_add( fDist_fx, BcTcvq_FixSearch_fx( X_fx, TCVQ_CB_SUB3_fx, fCodeword, quant_fx, FixBranch_tbl, stage, inis, i, &prev_state, W_fx, IntraCoeff_fx ) );
3230 : }
3231 40000 : IF( LT_32( fDist_fx, minDist_fx ) )
3232 : {
3233 21259 : minDist_fx = L_add( fDist_fx, 0 );
3234 21259 : blockDist_fx[state] = minDist_fx;
3235 21259 : move32();
3236 :
3237 21259 : fState[state] = fins;
3238 21259 : move16();
3239 :
3240 21259 : blockCodeword[state][0] = fCodeword[i][0];
3241 21259 : move16();
3242 21259 : blockCodeword[state][1] = fCodeword[i][1];
3243 21259 : move16();
3244 21259 : blockCodeword[state][2] = fCodeword[i][2];
3245 21259 : move16();
3246 21259 : blockCodeword[state][3] = fCodeword[i][3];
3247 21259 : move16();
3248 : }
3249 : }
3250 : }
3251 :
3252 : /* Select optimal path */
3253 625 : fBlock = optimalPath_fx( acumDist_fx, blockDist_fx, blockCodeword, bestCodeword, codeWord, bestState, preState );
3254 :
3255 : /* Select Quantized Value */
3256 625 : quantEnc_fx( y_fx, bestCodeword, TCVQ_CB_SUB1_fx, TCVQ_CB_SUB2_fx, TCVQ_CB_SUB3_fx, IntraCoeff_fx );
3257 :
3258 : /* Buid Code for Decoder */
3259 625 : buildCode_fx( ind, fState[fBlock], bestCodeword, bestState );
3260 :
3261 625 : return;
3262 : }
3263 :
3264 718 : static Word16 SVQ_2d_fx(
3265 : Word16 *x_fx, // x2.65
3266 : Word16 *y_fx, // x2.65
3267 : const Word16 *W_fx, // Q8
3268 : const Word16 CB_fx[][8], // x2.65
3269 : Word16 Size )
3270 : {
3271 : Word16 i, j;
3272 718 : Word16 index = 0;
3273 : Word32 distortion_fx;
3274 : Word32 temp_fx;
3275 : Word16 temp16_fx;
3276 :
3277 718 : temp_fx = L_add( MAX_32, 0 );
3278 :
3279 17950 : FOR( i = 0; i < Size; i++ )
3280 : {
3281 17232 : distortion_fx = L_deposit_l( 0 );
3282 155088 : FOR( j = 0; j < 8; j++ )
3283 : {
3284 137856 : temp16_fx = sub( x_fx[j], CB_fx[i][j] );
3285 137856 : distortion_fx = L_add( distortion_fx,
3286 137856 : L_shr( Mult_32_16( L_mult( temp16_fx, temp16_fx ), W_fx[j] ), 1 ) ); // (2*x2.65 + Q1 + Q8) - Q15 - Q1
3287 : }
3288 :
3289 17232 : IF( LT_32( distortion_fx, temp_fx ) )
3290 : {
3291 2778 : temp_fx = L_add( distortion_fx, 0 );
3292 2778 : index = i;
3293 2778 : move16();
3294 : }
3295 : }
3296 :
3297 6462 : FOR( i = 0; i < M / 2; i++ )
3298 : {
3299 5744 : y_fx[i] = CB_fx[index][i];
3300 5744 : move16();
3301 : }
3302 :
3303 718 : return index;
3304 : }
3305 :
3306 :
3307 625 : Word32 qlsf_ARSN_tcvq_Enc_16k_fx(
3308 : const Word16 *x_fx, /* i : Vector to be encoded x2.65 */
3309 : Word16 *y_fx, /* o : Quantized LSF vector x2.65 */
3310 : Word16 *indice, /* o : Indices */
3311 : const Word16 *w_fx, /* i : LSF Weights Q8 */
3312 : const Word16 nBits, /* i : number of bits */
3313 : Word16 safety_net /* i : safety_net flag */
3314 : )
3315 : {
3316 : Word16 i;
3317 : Word16 x_q_fx[M];
3318 : Word16 yy_fx[M];
3319 : Word16 error_svq_fx[M], error_svq_q_fx[M];
3320 : Word16 cl, cs;
3321 : Word32 temp_l;
3322 : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
3323 625 : Flag Overflow = 0;
3324 : #endif
3325 625 : IF( EQ_16( safety_net, 1 ) )
3326 : {
3327 101 : indice[0] = 1;
3328 101 : move16();
3329 101 : BcTcvq_fx( 1, /*x, x_q, w, */ x_fx, x_q_fx, w_fx, &indice[1] );
3330 :
3331 101 : IF( GT_16( nBits, 30 ) )
3332 : {
3333 : /* SVQ */
3334 1003 : FOR( i = 0; i < M; i++ )
3335 : {
3336 944 : error_svq_fx[i] = mult_r( sub( x_fx[i], x_q_fx[i] ), scale_inv_ARSN_fx[i] );
3337 944 : move16();
3338 : }
3339 :
3340 : /* 5bits 1st Split VQ for Residual*/
3341 59 : indice[10] = SVQ_2d_fx( error_svq_fx, error_svq_q_fx, w_fx, AR_SVQ_CB1_fx, 32 );
3342 : /* 4bits 2nd Split VQ for Residual*/
3343 59 : indice[11] = SVQ_2d_fx( &error_svq_fx[8], &error_svq_q_fx[8], &w_fx[8], AR_SVQ_CB2_fx, 16 );
3344 :
3345 1003 : FOR( i = 0; i < M; i++ )
3346 : {
3347 944 : x_q_fx[i] = add( x_q_fx[i], extract_h( L_shl( L_mult0( error_svq_q_fx[i], scale_ARSN_fx[i] ), 2 ) ) );
3348 944 : move16();
3349 : }
3350 : }
3351 : }
3352 : ELSE
3353 : {
3354 524 : indice[0] = 0;
3355 524 : move16();
3356 524 : BcTcvq_fx( 0, /*x, x_q, w, */ x_fx, x_q_fx, w_fx, &indice[1] );
3357 :
3358 524 : IF( GT_16( nBits, 30 ) )
3359 : {
3360 : /* SVQ */
3361 5100 : FOR( i = 0; i < M; i++ )
3362 : {
3363 4800 : error_svq_fx[i] = sub( x_fx[i], x_q_fx[i] );
3364 4800 : move16();
3365 : }
3366 :
3367 : /* 5bits 1st Split VQ for Residual*/
3368 300 : indice[10] = SVQ_2d_fx( error_svq_fx, error_svq_q_fx, w_fx, AR_SVQ_CB1_fx, 32 );
3369 : /* 4bits 2nd Split VQ for Residual*/
3370 300 : indice[11] = SVQ_2d_fx( &error_svq_fx[8], &error_svq_q_fx[8], &w_fx[8], AR_SVQ_CB2_fx, 16 );
3371 :
3372 5100 : FOR( i = 0; i < M; i++ )
3373 : {
3374 4800 : x_q_fx[i] = add( x_q_fx[i], error_svq_q_fx[i] );
3375 4800 : move16();
3376 : }
3377 : }
3378 : }
3379 :
3380 625 : cl = 0;
3381 625 : move16();
3382 10625 : FOR( i = 0; i < M; i++ )
3383 : {
3384 10000 : yy_fx[i] = sub( x_fx[i], x_q_fx[i] );
3385 10000 : move16();
3386 10000 : cl = s_max( cl, abs_s( yy_fx[i] ) );
3387 : }
3388 625 : cs = norm_s( cl );
3389 625 : temp_l = 0;
3390 625 : move16();
3391 :
3392 10625 : FOR( i = 0; i < M; i++ )
3393 : {
3394 10000 : yy_fx[i] = shl( yy_fx[i], cs );
3395 10000 : move16();
3396 10000 : temp_l = L_mac_o( temp_l, mult( yy_fx[i], shl_o( w_fx[i], 2, &Overflow ) ), yy_fx[i], &Overflow );
3397 : }
3398 625 : cs = shl( cs, 1 );
3399 625 : temp_l = L_shr( temp_l, cs );
3400 625 : temp_l = Mult_32_16( temp_l, LSF_1_OVER_256SQ ); /* Q-4 */
3401 :
3402 : /* Recover the quantized LSF */
3403 625 : Copy( x_q_fx, y_fx, M );
3404 :
3405 625 : return temp_l;
3406 : }
3407 :
3408 1427 : static void FFT_Mid_Interpol_16k_fx(
3409 : Word32 Bin_Ener_old[], /* i/o: Old 2nd FFT Bin energy (128) Q_ener*/
3410 : Word32 Bin_Ener[], /* i : Current 2nd FFT Bin energy (128) Q_ener*/
3411 : Word32 Bin_Ener_mid[] /* o : LP weighting filter (numerator) Q_ener*/
3412 : )
3413 : {
3414 : Word16 i;
3415 : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
3416 1427 : Flag Overflow = 0;
3417 : #endif
3418 :
3419 184083 : FOR( i = 0; i < L_FFT / 2; i++ )
3420 : {
3421 : /* Interpolation */
3422 182656 : Bin_Ener_mid[i] = L_shr( L_add_o( Bin_Ener_old[i], Bin_Ener[i], &Overflow ), 1 );
3423 :
3424 : /* Memory update */
3425 182656 : Bin_Ener_old[i] = Bin_Ener[i];
3426 182656 : move32();
3427 : }
3428 :
3429 1427 : return;
3430 : }
3431 :
3432 :
3433 : /*========================================================================*/
3434 : /* FUNCTION : lsf_mid_enc_fx() */
3435 : /*------------------------------------------------------------------------*/
3436 : /* PURPOSE : Mid-frame LSF quantization */
3437 : /*------------------------------------------------------------------------*/
3438 : /* INPUT ARGUMENTS : */
3439 : /* _ (Word16) coder_type : Coder type */
3440 : /* _ (Word16) bwidth : input signal bandwidth */
3441 : /* _ (Word16) int_fs : internal (ACELP) sampling frequency */
3442 : /* _ (Word32) core_brate : core bitrate */
3443 : /* _ (Word32) ppp_mode : PPP mode */
3444 : /* _ (Word32) nelp_mode : NELP mode */
3445 : /* _ (Word16[]) qlsp0 : quantized LSPs from frame beginning Q15 */
3446 : /* _ (Word16[]) qlsp1 : quantized LSPs from frame end Q15 */
3447 : /* _ (Word16[]) Bin_Ener : per bin log energy spectrum Q_ener */
3448 : /* _ (Word16) Q_ener : */
3449 : /* _ (Word16) ppp_mode : */
3450 : /* _ (Word16) nelp_mode : */
3451 : /*------------------------------------------------------------------------*/
3452 : /* INPUT/OUTPUT ARGUMENTS : */
3453 : /* _ (Word16[]) lsp : quantized LSPs Q15 */
3454 : /* _ (Word16[]) Bin_Ener_old : per bin old log energy spectrum Q_ener */
3455 : /*------------------------------------------------------------------------*/
3456 : /* OUTPUT ARGUMENTS : */
3457 : /*------------------------------------------------------------------------*/
3458 :
3459 : /*------------------------------------------------------------------------*/
3460 : /* RETURN ARGUMENTS : */
3461 : /* _ None */
3462 : /*========================================================================*/
3463 :
3464 :
3465 1427 : static void lsf_mid_enc_fx(
3466 : BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle */
3467 : Word16 nb_bits, /* i : number of bits */
3468 : const Word16 int_fs, /* i : internal (ACELP) sampling frequency*/
3469 : const Word16 qlsp0[], /* i : quantized LSPs from frame beginning*/
3470 : const Word16 qlsp1[], /* i : quantized LSPs from frame end */
3471 : Word16 lsp[], /* i/o: mid-frame LSP */
3472 : const Word16 coder_type, /* i : coding type */
3473 : const Word16 bwidth, /* i : input signal bandwidth */
3474 : Word32 Bin_Ener_old[], /* i/o: per bin old log energy spectrum */
3475 : Word32 Bin_Ener[], /* i : per bin log energy spectrum */
3476 : Word16 Q_ener, /* i : Q value of Bin_ener */
3477 : Word16 ppp_mode,
3478 : Word16 nelp_mode )
3479 : {
3480 : Word16 lsf[M], qlsf[M], qlsf1[M], qlsf0[M], wghts[M];
3481 : Word32 err, err_min;
3482 1427 : Word16 j, k, idx, size = 0;
3483 : Word32 Bin_Ener_mid[L_FFT / 2];
3484 : Word32 L_tmp;
3485 : Word16 tmp, k1;
3486 1427 : const Word16 *ratio = NULL;
3487 : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
3488 1427 : Flag Overflow = 0;
3489 : #endif
3490 :
3491 : /* convert LSPs to LSFs */
3492 1427 : lsp2lsf_fx( lsp, lsf, M, int_fs );
3493 1427 : lsp2lsf_fx( qlsp0, qlsf0, M, int_fs );
3494 1427 : lsp2lsf_fx( qlsp1, qlsf1, M, int_fs );
3495 :
3496 : /* calculate weights */
3497 1427 : FFT_Mid_Interpol_16k_fx( Bin_Ener_old, &Bin_Ener[L_FFT / 2], Bin_Ener_mid );
3498 :
3499 : /* LSF weighting */
3500 1427 : Unified_weighting_fx( Bin_Ener_mid, Q_ener, lsf, wghts, (Word16) EQ_16( bwidth, NB ), (Word16) EQ_16( coder_type, UNVOICED ), int_fs, M );
3501 1427 : move16();
3502 : /* codebook selection, number of bits, size of the codebook */
3503 1427 : test();
3504 1427 : IF( ppp_mode == 0 && nelp_mode == 0 )
3505 : {
3506 : /* codebook selection */
3507 1427 : IF( EQ_16( coder_type, VOICED ) )
3508 : {
3509 212 : SWITCH( nb_bits )
3510 : {
3511 0 : case 5:
3512 : {
3513 0 : ratio = tbl_mid_voi_wb_5b_fx;
3514 0 : move16();
3515 0 : BREAK;
3516 : }
3517 212 : case 4:
3518 : {
3519 212 : ratio = tbl_mid_voi_wb_4b_fx;
3520 212 : move16();
3521 212 : BREAK;
3522 : }
3523 : }
3524 212 : }
3525 1215 : ELSE IF( EQ_16( coder_type, UNVOICED ) )
3526 : {
3527 0 : ratio = tbl_mid_unv_wb_5b_fx;
3528 : }
3529 : ELSE
3530 : {
3531 : /* GENERIC, TRANSITION, AUDIO and INACTIVE */
3532 1215 : SWITCH( nb_bits )
3533 : {
3534 1208 : case 5:
3535 : {
3536 1208 : ratio = tbl_mid_gen_wb_5b_fx;
3537 1208 : move16();
3538 1208 : BREAK;
3539 : }
3540 7 : case 2:
3541 : {
3542 7 : ratio = tbl_mid_gen_wb_2b_fx;
3543 7 : move16();
3544 7 : BREAK;
3545 : }
3546 : }
3547 1427 : }
3548 :
3549 1427 : size = (Word16) pow2[nb_bits];
3550 1427 : move16();
3551 : }
3552 0 : ELSE IF( EQ_16( ppp_mode, 1 ) )
3553 : {
3554 0 : ratio = tbl_mid_voi_wb_1b_fx;
3555 0 : move16();
3556 0 : nb_bits = 1;
3557 0 : move16();
3558 0 : size = 2;
3559 0 : move16();
3560 : }
3561 0 : ELSE IF( EQ_16( nelp_mode, 1 ) )
3562 : {
3563 0 : ratio = tbl_mid_unv_wb_4b_fx;
3564 0 : move16();
3565 0 : nb_bits = 4;
3566 0 : move16();
3567 0 : size = 16;
3568 0 : move16();
3569 : }
3570 :
3571 : /* loop over codevectors */
3572 1427 : err_min = MAXINT32;
3573 1427 : move16();
3574 1427 : idx = 0;
3575 1427 : move16();
3576 1427 : k1 = 0;
3577 1427 : move16();
3578 43503 : FOR( k = 0; k < size; k++ )
3579 : {
3580 42076 : err = L_deposit_l( 0 );
3581 :
3582 715292 : FOR( j = 0; j < M; j++ )
3583 : {
3584 : /* qlsf[j] = (1.0f - ratio[k*M+j]) * qlsf0[j] + ratio[k*M+j] * qlsf1[j]; */
3585 673216 : L_tmp = L_mult( sub( 0x2000, ratio[k1 + j] ), qlsf0[j] );
3586 673216 : L_tmp = L_mac( L_tmp, ratio[k1 + j], qlsf1[j] );
3587 673216 : qlsf[j] = round_fx( L_shl( L_tmp, 2 ) );
3588 :
3589 673216 : test();
3590 673216 : test();
3591 673216 : IF( j > 0 && LT_16( j, M ) && LT_16( qlsf[j], add( qlsf[j - 1], LSF_GAP_MID_FX ) ) )
3592 : {
3593 6421 : qlsf[j] = add( qlsf[j - 1], LSF_GAP_MID_FX );
3594 6421 : move16();
3595 : }
3596 :
3597 673216 : tmp = sub( lsf[j], qlsf[j] );
3598 : /* err += wghts[j] * ftemp * ftemp; */
3599 : /* tmp is usually very small, we can have some extra precision with very rare saturation */
3600 673216 : tmp = shl_o( tmp, 4, &Overflow );
3601 673216 : tmp = mult_ro( tmp, tmp, &Overflow );
3602 673216 : err = L_mac_o( err, tmp, shl_o( wghts[j], 2, &Overflow ), &Overflow );
3603 : }
3604 : /* err = L_shl(err,Wscale); */
3605 42076 : err = Mult_32_16( err, LSF_1_OVER_256SQ );
3606 : /* err = Mult_32_16(err,Wmult); */
3607 :
3608 42076 : IF( LT_32( err, err_min ) )
3609 : {
3610 5476 : err_min = L_add( err, 0 );
3611 5476 : idx = k;
3612 5476 : move16();
3613 : }
3614 42076 : k1 += M;
3615 42076 : move16();
3616 : }
3617 :
3618 : /* calculate the quantized LSF vector */
3619 24259 : FOR( j = 0; j < M; j++ )
3620 : {
3621 : /* qlsf[j] = (1.0f - ratio[idx*M+j]) * qlsf0[j] + ratio[idx*M+j] * qlsf1[j]; */
3622 22832 : L_tmp = L_mult( sub( 0x2000, ratio[idx * M + j] ), qlsf0[j] );
3623 22832 : L_tmp = L_mac( L_tmp, ratio[idx * M + j], qlsf1[j] );
3624 22832 : qlsf[j] = round_fx( L_shl( L_tmp, 2 ) );
3625 :
3626 22832 : test();
3627 22832 : test();
3628 22832 : IF( j > 0 && LT_16( j, M ) && LT_16( qlsf[j], add( qlsf[j - 1], LSF_GAP_MID_FX ) ) )
3629 : {
3630 237 : qlsf[j] = add( qlsf[j - 1], LSF_GAP_MID_FX );
3631 237 : move16();
3632 : }
3633 : }
3634 :
3635 1427 : reorder_lsf_fx( qlsf, LSF_GAP_MID_FX, M, int_fs );
3636 :
3637 : /* convert LSFs back to LSPs */
3638 1427 : lsf2lsp_fx( qlsf, lsp, M, int_fs );
3639 1427 : push_indice( hBstr, IND_MID_FRAME_LSF_INDEX, idx, nb_bits );
3640 :
3641 1427 : return;
3642 : }
3643 :
3644 155786 : static void lsf_mid_enc_ivas_fx(
3645 : BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle */
3646 : Word16 nb_bits, /* i : number of bits */
3647 : const Word32 int_fs, /* i : internal (ACELP) sampling frequency*/
3648 : const Word16 qlsp0[], /* i : quantized LSPs from frame beginning Q15*/
3649 : const Word16 qlsp1[], /* i : quantized LSPs from frame end Q15*/
3650 : Word16 lsp[], /* i/o: mid-frame LSP Q15*/
3651 : const Word16 coder_type, /* i : coding type */
3652 : const Word16 bwidth, /* i : input signal bandwidth */
3653 : Word32 Bin_Ener[], /* i : per bin log energy spectrum Q_ener*/
3654 : Word16 Q_ener, /* i : Q value of Bin_ener */
3655 : Word16 ppp_mode,
3656 : Word16 nelp_mode )
3657 : {
3658 : Word16 lsf[M], qlsf[M], qlsf1[M], qlsf0[M], wghts[M];
3659 : Word32 err, err_min;
3660 155786 : Word16 j, k, idx, size = 0;
3661 : Word32 L_tmp;
3662 : Word16 tmp, k1;
3663 155786 : const Word16 *ratio = NULL;
3664 : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
3665 155786 : Flag Overflow = 0;
3666 : #endif
3667 :
3668 : /* convert LSPs to LSFs */
3669 155786 : lsp2lsf_fx( lsp, lsf, M, int_fs );
3670 155786 : lsp2lsf_fx( qlsp0, qlsf0, M, int_fs );
3671 155786 : lsp2lsf_fx( qlsp1, qlsf1, M, int_fs );
3672 :
3673 : /* LSF weighting */
3674 155786 : Unified_weighting_fx( Bin_Ener, Q_ener, lsf, wghts, (Word16) EQ_16( bwidth, NB ), (Word16) EQ_16( coder_type, UNVOICED ), int_fs, M );
3675 155786 : move16();
3676 : /* codebook selection, number of bits, size of the codebook */
3677 155786 : test();
3678 155786 : IF( ppp_mode == 0 && nelp_mode == 0 )
3679 : {
3680 : /* codebook selection */
3681 155786 : IF( EQ_16( coder_type, VOICED ) )
3682 : {
3683 8608 : SWITCH( nb_bits )
3684 : {
3685 1693 : case 5:
3686 : {
3687 1693 : ratio = tbl_mid_voi_wb_5b_fx; // Q13
3688 1693 : move16();
3689 1693 : BREAK;
3690 : }
3691 6915 : case 4:
3692 : {
3693 6915 : ratio = tbl_mid_voi_wb_4b_fx; // Q13
3694 6915 : move16();
3695 6915 : BREAK;
3696 : }
3697 0 : case 1:
3698 : {
3699 0 : ratio = tbl_mid_voi_wb_1b_fx; // Q13
3700 0 : move16();
3701 0 : BREAK;
3702 : }
3703 : }
3704 8608 : }
3705 147178 : ELSE IF( EQ_16( coder_type, UNVOICED ) )
3706 : {
3707 2019 : ratio = tbl_mid_unv_wb_5b_fx; // Q13
3708 : }
3709 : ELSE
3710 : {
3711 : /* GENERIC, TRANSITION, AUDIO and INACTIVE */
3712 145159 : SWITCH( nb_bits )
3713 : {
3714 135999 : case 5:
3715 : {
3716 135999 : ratio = tbl_mid_gen_wb_5b_fx; // Q13
3717 135999 : move16();
3718 135999 : BREAK;
3719 : }
3720 732 : case 4:
3721 : {
3722 732 : ratio = tbl_mid_gen_wb_4b_fx; // Q13
3723 732 : move16();
3724 732 : BREAK;
3725 : }
3726 8428 : case 2:
3727 : {
3728 8428 : ratio = tbl_mid_gen_wb_2b_fx; // Q13
3729 8428 : move16();
3730 8428 : BREAK;
3731 : }
3732 : }
3733 155786 : }
3734 :
3735 155786 : size = (Word16) pow2[nb_bits];
3736 155786 : move16();
3737 : }
3738 0 : ELSE IF( EQ_16( ppp_mode, 1 ) )
3739 : {
3740 0 : ratio = tbl_mid_voi_wb_1b_fx; // Q13
3741 0 : move16();
3742 0 : nb_bits = 1;
3743 0 : move16();
3744 0 : size = 2;
3745 0 : move16();
3746 : }
3747 0 : ELSE IF( EQ_16( nelp_mode, 1 ) )
3748 : {
3749 0 : ratio = tbl_mid_unv_wb_4b_fx; // Q13
3750 0 : move16();
3751 0 : nb_bits = 4;
3752 0 : move16();
3753 0 : size = 16;
3754 0 : move16();
3755 : }
3756 :
3757 : /* loop over codevectors */
3758 155786 : err_min = MAXINT32;
3759 155786 : move16();
3760 155786 : idx = 0;
3761 155786 : move16();
3762 155786 : k1 = 0;
3763 155786 : move16();
3764 4782602 : FOR( k = 0; k < size; k++ )
3765 : {
3766 4626816 : err = L_deposit_l( 0 );
3767 :
3768 78655872 : FOR( j = 0; j < M; j++ )
3769 : {
3770 : /* qlsf[j] = (1.0f - ratio[k*M+j]) * qlsf0[j] + ratio[k*M+j] * qlsf1[j]; */
3771 74029056 : L_tmp = L_mult( sub( 0x2000 /*1.Q13*/, ratio[k1 + j] ), qlsf0[j] );
3772 74029056 : L_tmp = L_mac( L_tmp, ratio[k1 + j], qlsf1[j] );
3773 74029056 : qlsf[j] = round_fx( L_shl( L_tmp, 2 ) );
3774 :
3775 74029056 : test();
3776 74029056 : test();
3777 74029056 : IF( j > 0 && LT_16( j, M ) && LT_16( qlsf[j], add( qlsf[j - 1], LSF_GAP_MID_FX ) ) )
3778 : {
3779 446248 : qlsf[j] = add( qlsf[j - 1], LSF_GAP_MID_FX );
3780 446248 : move16();
3781 : }
3782 :
3783 74029056 : tmp = sub( lsf[j], qlsf[j] );
3784 : /* err += wghts[j] * ftemp * ftemp; */
3785 : /* tmp is usually very small, we can have some extra precision with very rare saturation */
3786 74029056 : tmp = shl_o( tmp, 4, &Overflow );
3787 74029056 : tmp = mult_ro( tmp, tmp, &Overflow );
3788 74029056 : err = L_mac_o( err, tmp, shl_o( wghts[j], 2, &Overflow ), &Overflow );
3789 : }
3790 : /* err = L_shl(err,Wscale); */
3791 4626816 : err = Mult_32_16( err, LSF_1_OVER_256SQ );
3792 : /* err = Mult_32_16(err,Wmult); */
3793 :
3794 4626816 : IF( LT_32( err, err_min ) )
3795 : {
3796 608050 : err_min = L_add( err, 0 );
3797 608050 : idx = k;
3798 608050 : move16();
3799 : }
3800 4626816 : k1 += M;
3801 4626816 : move16();
3802 : }
3803 :
3804 : /* calculate the quantized LSF vector */
3805 2648362 : FOR( j = 0; j < M; j++ )
3806 : {
3807 : /* qlsf[j] = (1.0f - ratio[idx*M+j]) * qlsf0[j] + ratio[idx*M+j] * qlsf1[j]; */
3808 2492576 : L_tmp = L_mult( sub( 0x2000 /*1.Q13*/, ratio[idx * M + j] ), qlsf0[j] );
3809 2492576 : L_tmp = L_mac( L_tmp, ratio[idx * M + j], qlsf1[j] );
3810 2492576 : qlsf[j] = round_fx( L_shl( L_tmp, 2 ) );
3811 :
3812 2492576 : test();
3813 2492576 : test();
3814 2492576 : IF( j > 0 && LT_16( j, M ) && LT_16( qlsf[j], add( qlsf[j - 1], LSF_GAP_MID_FX ) ) )
3815 : {
3816 13893 : qlsf[j] = add( qlsf[j - 1], LSF_GAP_MID_FX );
3817 13893 : move16();
3818 : }
3819 : }
3820 :
3821 155786 : reorder_lsf_fx( qlsf, LSF_GAP_MID_FX, M, int_fs );
3822 :
3823 : /* convert LSFs back to LSPs */
3824 155786 : lsf2lsp_fx( qlsf, lsp, M, int_fs );
3825 155786 : push_indice( hBstr, IND_MID_FRAME_LSF_INDEX, idx, nb_bits );
3826 :
3827 155786 : return;
3828 : }
|