Line data Source code
1 : /*====================================================================================
2 : EVS Codec 3GPP TS26.452 Aug 12, 2021. Version 16.3.0
3 : ====================================================================================*/
4 : #include <stdint.h>
5 : #include "options.h" /* Compilation switches */
6 : #include "cnst.h" /* Compilation switches */
7 : // #include "prot_fx.h" /* Function prototypes */
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_enc.h" /* Function prototypes */
12 :
13 : /*=================================================================================*/
14 : /* FUNCTION : void encod_tran_fx () */
15 : /*---------------------------------------------------------------------------------*/
16 : /* PURPOSE : */
17 : /*---------------------------------------------------------------------------------*/
18 : /* INPUT ARGUMENTS : */
19 : /* (Word16) L_frame_fx : length of the frame Q0 */
20 : /* (Word16[]) speech_fx : input speech Q0 */
21 : /* (Word16[]) Aq_fx : 12k8 Lp coefficient Q12 */
22 : /* (Word16[]) A_fx : unquantized A(z) filter with bandwidth expansion Q12 */
23 : /* (Word16) coder_type : coding type Q0 */
24 : /* (Word16) Es_pred_fx : predicted scaled innov. energy Q8 */
25 : /* (Word16[]) T_op_fx : open loop pitch Q0 */
26 : /* (Word16[]) voicing_fx : voicing Q15 */
27 : /* (Word16*) res_fx : residual signal Q_new*/
28 : /* (Word16) gsc_attack_flag : Flag to indicate when an audio attack is deal with TM*/
29 : /* (Word16) shift : shift factor */
30 : /* (Word16[]) Q_new : input scaling */
31 : /*---------------------------------------------------------------------------------*/
32 : /* OUTPUT ARGUMENTS : */
33 : /* (Word16*) voice_factors : voicing factors Q15 */
34 : /*---------------------------------------------------------------------------------*/
35 : /* INPUT/OUTPUT ARGUMENTS : */
36 : /* Encoder_State *st_fx :Encoder state structure */
37 : /* (Word16*) syn_fx :core synthesis Qnew */
38 : /* (Word16*) exc_fx :current non-enhanced excitation Q0 */
39 : /* (Word16*) exc2_fx :current enhanced excitation Q0 */
40 : /* (Word16*) pitch_buf_fx :floating pitch values for each subframe Q6 */
41 : /* (Word16*) bwe_exc_fx :excitation for SWB TBE Q0 */
42 : /*---------------------------------------------------------------------------------*/
43 : /* RETURN ARGUMENTS : */
44 : /* _ None */
45 : /*---------------------------------------------------------------------------------*/
46 :
47 121 : Word16 encod_tran_fx(
48 : Encoder_State *st_fx, /* i/o: state structure */
49 : const Word16 speech_fx[], /* i : input speech Q0*/
50 : const Word16 Aw_fx[], /* i : weighted A(z) unquantized for subframes Q12*/
51 : const Word16 Aq_fx[], /* i : 12k8 Lp coefficient Q12*/
52 : const Word16 Es_pred_fx, /* i : predicted scaled innov. energy Q8*/
53 : const Word16 *res_fx, /* i : residual signal Q_new*/
54 : Word16 *syn_fx, /* i/o: core synthesis Q_new*/
55 : Word16 *exc_fx, /* i/o: current non-enhanced excitation Q0*/
56 : Word16 *exc2_fx, /* i/o: current enhanced excitation Q0*/
57 : Word16 *pitch_buf_fx, /* i/o: floating pitch values for each subframe Q6*/
58 : Word16 *voice_factors, /* o : voicing factors Q15*/
59 : Word16 *bwe_exc_fx, /* i/o: excitation for SWB TBE Q0*/
60 : Word16 tc_subfr, /* i/o: TC subframe classification Q0*/
61 : Word16 position, /* i : maximum of residual signal index Q0*/
62 : Word16 *unbits, /* i/o: number of unused bits Q0*/
63 : const Word16 shift, /* i : Scaling to get 12 bits */
64 : const Word16 Q_new /* i : Input scaling */
65 : )
66 : {
67 : Word16 xn[L_SUBFR]; /* Target vector for pitch search */
68 : Word16 xn2[L_SUBFR]; /* Target vector for codebook search */
69 : Word16 cn[L_SUBFR]; /* Target vector in residual domain */
70 : Word16 h1[L_SUBFR + ( M + 1 )]; /* Impulse response vector */
71 : Word16 h2_fx[L_SUBFR + ( M + 1 )]; /* Impulse response vector */
72 : Word16 code[L_SUBFR]; /* Fixed codebook excitation */
73 : Word16 y1[L_SUBFR]; /* Filtered adaptive excitation */
74 : Word16 y2[L_SUBFR]; /* Filtered algebraic excitation */
75 121 : Word16 gain_pit = 0, Gain_pitX2, gcode16; /* Pitch gain */
76 : Word16 voice_fac; /* Voicing factor */
77 121 : Word32 gain_code = 0; /* Gain of code */
78 : Word32 Lgcode;
79 121 : Word16 gain_inov = 0; /* inovation gain */
80 : Word16 i, i_subfr, tmp1_fx, tmp_fx; /* tmp variables */
81 : Word16 unbits_ACELP;
82 : Word16 T0_min, T0_max; /* pitch and TC variables */
83 : Word16 T0, T0_frac; /* close loop integer pitch and fractional part */
84 : Word16 *pt_pitch; /* pointer to floating pitch buffer */
85 : Word16 g_corr[10]; /* ACELP correlation values and gain pitch */
86 : Word16 clip_gain; /* LSF clip gain */
87 : const Word16 *p_Aw, *p_Aq; /* pointer to LP filter coefficient vector */
88 121 : Word16 gain_preQ = 0; /* Gain of prequantizer excitation */
89 : Word16 code_preQ[L_SUBFR]; /* Prequantizer excitation */
90 : Word16 Jopt_flag; /* joint optimization flag */
91 121 : Word16 unbits_PI = 0; /* saved bits for PI */
92 121 : Word32 norm_gain_code = 0;
93 : Word16 L_frame_fx;
94 : Word16 shift_wsp;
95 : Word32 L_tmp;
96 : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
97 121 : Flag Overflow = 0;
98 121 : move32();
99 : #endif
100 121 : BSTR_ENC_HANDLE hBstr = st_fx->hBstr;
101 121 : SC_VBR_ENC_HANDLE hSC_VBR = st_fx->hSC_VBR;
102 121 : LPD_state_HANDLE hLPDmem = st_fx->hLPDmem;
103 :
104 121 : L_frame_fx = st_fx->L_frame;
105 121 : move16();
106 : /*------------------------------------------------------------------*
107 : * Initializations
108 : *------------------------------------------------------------------*/
109 121 : gain_pit = 0;
110 121 : move16();
111 121 : gain_code = L_deposit_l( 0 );
112 121 : gain_preQ = 0;
113 121 : move16();
114 121 : unbits_PI = 0;
115 121 : move16();
116 121 : IF( EQ_16( L_frame_fx, L_FRAME ) )
117 : {
118 71 : T0_max = PIT_MAX;
119 71 : move16();
120 71 : T0_min = PIT_MIN;
121 71 : move16();
122 : }
123 : ELSE /* L_frame == L_FRAME16k */
124 : {
125 50 : T0_max = PIT16k_MAX;
126 50 : move16();
127 50 : T0_min = PIT16k_MIN;
128 50 : move16();
129 : }
130 :
131 : /**unbits = 0;move16();*/
132 121 : Jopt_flag = 0;
133 121 : move16();
134 121 : unbits_ACELP = *unbits; /* Q0 */
135 121 : move16();
136 121 : *unbits = 0;
137 121 : move16();
138 :
139 121 : p_Aw = Aw_fx; /* Q12 */
140 121 : p_Aq = Aq_fx; /* Q12 */
141 121 : pt_pitch = pitch_buf_fx; /* Q6 */
142 121 : gain_preQ = 0;
143 121 : move16();
144 121 : set16_fx( code_preQ, 0, L_SUBFR );
145 121 : shift_wsp = add( Q_new, shift );
146 :
147 : /*----------------------------------------------------------------*
148 : * ACELP subframe loop
149 : *----------------------------------------------------------------*/
150 :
151 655 : FOR( i_subfr = 0; i_subfr < L_frame_fx; i_subfr += L_SUBFR )
152 : {
153 : /*----------------------------------------------------------------*
154 : * Find the the excitation search target "xn" and innovation
155 : * target in residual domain "cn"
156 : * Compute impulse response, h1[], of weighted synthesis filter
157 : *----------------------------------------------------------------*/
158 :
159 534 : Copy( &res_fx[i_subfr], &exc_fx[i_subfr], L_SUBFR ); /* Q_new */
160 :
161 534 : find_targets_fx( speech_fx, hLPDmem->mem_syn, i_subfr, &hLPDmem->mem_w0, p_Aq,
162 534 : res_fx, L_SUBFR, p_Aw, st_fx->preemph_fac, xn, cn, h1 );
163 :
164 534 : Copy_Scale_sig( h1, h2_fx, L_SUBFR, -2 );
165 534 : Scale_sig( h1, L_SUBFR, add( 1, shift ) ); /* set h1[] in Q14 with scaling for convolution */
166 :
167 : /* scaling of xn[] to limit dynamic at 12 bits */
168 534 : Scale_sig( xn, L_SUBFR, shift );
169 :
170 : /*-----------------------------------------------------------------*
171 : * TC: subframe determination &
172 : * adaptive/glottal part of excitation construction
173 : *-----------------------------------------------------------------*/
174 :
175 534 : transition_enc_fx( st_fx, i_subfr, &tc_subfr, &Jopt_flag, &position, &T0, &T0_frac, &T0_min, &T0_max, exc_fx, y1,
176 534 : h1, xn, xn2, st_fx->clip_var_fx, &gain_pit, g_corr, &clip_gain, &pt_pitch, bwe_exc_fx, &unbits_ACELP, Q_new, shift );
177 :
178 : /*-----------------------------------------------------------------*
179 : * Transform domain contribution encoding - active frames
180 : *-----------------------------------------------------------------*/
181 :
182 534 : IF( GE_32( st_fx->core_brate, MIN_BRATE_AVQ_EXC ) )
183 : {
184 250 : transf_cdbk_enc_fx( st_fx, 0, i_subfr, cn, exc_fx, p_Aq, Aw_fx, h1, xn, xn2, y1, y2, Es_pred_fx,
185 : &gain_pit, gain_code, g_corr, clip_gain, &gain_preQ, code_preQ, unbits, Q_new, shift );
186 : }
187 :
188 : /*-----------------------------------------------------------------*
189 : * ACELP codebook search + pitch sharpening
190 : *-----------------------------------------------------------------*/
191 :
192 534 : inov_encode_fx( st_fx, st_fx->core_brate, 0, L_frame_fx, st_fx->last_L_frame, st_fx->coder_type, st_fx->bwidth, st_fx->sharpFlag,
193 534 : i_subfr, tc_subfr, p_Aq, gain_pit, cn, exc_fx, h2_fx, hLPDmem->tilt_code, *pt_pitch, xn2, code, y2, &unbits_PI, L_SUBFR, shift );
194 :
195 534 : test();
196 534 : test();
197 534 : test();
198 534 : if ( ( EQ_16( st_fx->L_frame, L_FRAME16k ) ) && ( tc_subfr == 0 ) && ( EQ_16( i_subfr, L_SUBFR ) ) && ( EQ_16( T0, 2 * L_SUBFR ) ) )
199 : {
200 7 : Jopt_flag = 1;
201 7 : move16();
202 : }
203 : /*-----------------------------------------------------------------*
204 : * Quantize the gains
205 : * Test quantized gain of pitch for pitch clipping algorithm
206 : * Update tilt of code: 0.0 (unvoiced) to 0.5 (voiced)
207 : *-----------------------------------------------------------------*/
208 534 : IF( Jopt_flag == 0 )
209 : {
210 : /* SQ gain_code */
211 90 : gain_enc_tc_fx( hBstr, st_fx->acelp_cfg.gains_mode, i_subfr, xn, y2, code, Es_pred_fx,
212 : &gain_pit, &gain_code, &gain_inov, &norm_gain_code, shift_wsp );
213 : }
214 : ELSE
215 : {
216 444 : IF( GT_32( st_fx->core_brate, ACELP_32k ) )
217 : {
218 : /* SQ gain_pit and gain_code */
219 238 : gain_enc_SQ_fx( hBstr, st_fx->acelp_cfg.gains_mode, i_subfr, xn, y1, y2, code, Es_pred_fx,
220 : &gain_pit, &gain_code, &gain_inov, &norm_gain_code, g_corr, clip_gain, shift_wsp );
221 : }
222 : ELSE
223 : {
224 : /* VQ gain_pit and gain_code */
225 206 : gain_enc_mless_fx( hBstr, st_fx->acelp_cfg.gains_mode, st_fx->element_mode, L_frame_fx, i_subfr, tc_subfr, xn, y1, shift_wsp, y2, code, Es_pred_fx,
226 : &gain_pit, &gain_code, &gain_inov, &norm_gain_code, g_corr, clip_gain );
227 : }
228 : }
229 534 : gp_clip_test_gain_pit_fx( st_fx->element_mode, st_fx->core_brate, gain_pit, st_fx->clip_var_fx );
230 :
231 534 : Lgcode = L_shl_o( gain_code, Q_new, &Overflow ); /* scaled gain_code with Qnew -> Q16*/
232 534 : gcode16 = round_fx_o( Lgcode, &Overflow );
233 534 : hLPDmem->tilt_code = Est_tilt2( &exc_fx[i_subfr], gain_pit, code, Lgcode, &voice_fac, shift ); /* Q15 */
234 : /*-----------------------------------------------------------------*
235 : * Update memory of the weighting filter
236 : *-----------------------------------------------------------------*/
237 :
238 : /*st->mem_w0 = xn[L_SUBFR-1] - (gain_pit*y1[L_SUBFR-1]) - (gain_code*y2[L_SUBFR-1]);*/
239 534 : L_tmp = L_mult( gcode16, y2[L_SUBFR - 1] ); /* Q10 + Q_new */
240 534 : L_tmp = L_shl( L_tmp, add( 5, shift ) ); /* Q15 + Q_new + shift */
241 534 : L_tmp = L_negate( L_tmp );
242 534 : L_tmp = L_mac( L_tmp, xn[L_SUBFR - 1], 16384 );
243 534 : L_tmp = L_msu( L_tmp, y1[L_SUBFR - 1], gain_pit );
244 534 : L_tmp = L_shl_sat( L_tmp, sub( 1, shift ) ); /* Q_new + 15 */
245 534 : hLPDmem->mem_w0 = round_fx_sat( L_tmp ); /*Q_new-1*/
246 : /*-----------------------------------------------------------------*
247 : * Construct adaptive part of the excitation
248 : * Save the non-enhanced excitation for FEC_exc
249 : *-----------------------------------------------------------------*/
250 :
251 : /* Here, all these conditions have one purpose: to use */
252 : /* the most efficient loop (the one with the least ops) */
253 : /* This is done by upscaling gain_pit_fx and/or gain_code16 */
254 : /* when they don't use all 16 bits of precision */
255 :
256 : /* exc Q_exc, gpit Q14, code Q12, gcode Q0 */
257 534 : IF( norm_s( gain_pit ) == 0 )
258 : {
259 12285 : FOR( i = 0; i < L_SUBFR; i++ )
260 : {
261 12096 : exc2_fx[i + i_subfr] = round_fx_sat( L_shl_sat( L_mult_sat( gain_pit, exc_fx[i + i_subfr] ), 1 ) ); /* Q_new */
262 : }
263 : }
264 : ELSE
265 : {
266 345 : Gain_pitX2 = shl( gain_pit, 1 ); /* Q15 */
267 22425 : FOR( i = 0; i < L_SUBFR; i++ )
268 : {
269 22080 : exc2_fx[i + i_subfr] = mult_r( Gain_pitX2, exc_fx[i + i_subfr] ); /* Q_new */
270 : }
271 : }
272 :
273 : /*-----------------------------------------------------------------*
274 : * Construct adaptive part of the excitation
275 : * Save the non-enhanced excitation for FEC_exc
276 : *-----------------------------------------------------------------*/
277 34710 : FOR( i = 0; i < L_SUBFR; i++ )
278 : {
279 : /* code in Q9, gain_pit in Q14 */
280 34176 : L_tmp = L_mult( gcode16, code[i] ); /* Q10 + Q_new */
281 34176 : L_tmp = L_shl_o( L_tmp, 5, &Overflow ); /* Q15 + Q_new */
282 34176 : L_tmp = L_mac_o( L_tmp, exc_fx[i + i_subfr], gain_pit, &Overflow ); /* Q15 + Q_new */
283 34176 : L_tmp = L_shl_o( L_tmp, 1, &Overflow ); /* saturation can occur here Q16 + Q_new */
284 34176 : exc_fx[i + i_subfr] = round_fx_o( L_tmp, &Overflow ); /* Q_new */
285 : }
286 :
287 : /*-----------------------------------------------------------------*
288 : * Add the ACELP pre-quantizer contribution
289 : *-----------------------------------------------------------------*/
290 :
291 534 : IF( GE_32( st_fx->core_brate, MIN_BRATE_AVQ_EXC ) )
292 : {
293 250 : tmp1_fx = add( 16 - ( 2 + Q_AVQ_OUT_DEC + 1 ), Q_new );
294 16250 : FOR( i = 0; i < L_SUBFR; i++ )
295 : {
296 16000 : L_tmp = L_mult( gain_preQ, code_preQ[i] ); /* Q2 + Q10 -> Q13*/
297 16000 : L_tmp = L_shl_o( L_tmp, tmp1_fx, &Overflow ); /* Q16 + Q_exc */
298 16000 : tmp_fx = round_fx_o( L_tmp, &Overflow );
299 :
300 16000 : exc2_fx[i + i_subfr] = add_o( exc2_fx[i + i_subfr], tmp_fx, &Overflow ); /* Q_exc */
301 16000 : move16();
302 16000 : exc_fx[i + i_subfr] = add_o( exc_fx[i + i_subfr], tmp_fx, &Overflow ); /* Q_exc */
303 16000 : move16();
304 : }
305 : }
306 :
307 : /*-----------------------------------------------------------------*
308 : * Prepare TBE excitation
309 : *-----------------------------------------------------------------*/
310 :
311 534 : prep_tbe_exc_fx( L_frame_fx, L_SUBFR, i_subfr, gain_pit, gain_code, code, voice_fac, &voice_factors[i_subfr / L_SUBFR],
312 534 : bwe_exc_fx, gain_preQ, code_preQ, Q_new, T0, T0_frac, st_fx->coder_type, st_fx->core_brate, st_fx->element_mode, st_fx->idchan, st_fx->hBWE_TD != NULL, st_fx->tdm_LRTD_flag );
313 :
314 : /*-----------------------------------------------------------------*
315 : * Synthesize speech to update mem_syn[].
316 : * Update A(z) filters
317 : *-----------------------------------------------------------------*/
318 :
319 534 : Syn_filt_s( 1, p_Aq, M, &exc_fx[i_subfr], &syn_fx[i_subfr], L_SUBFR, hLPDmem->mem_syn, 1 );
320 :
321 534 : p_Aw += ( M + 1 );
322 534 : p_Aq += ( M + 1 );
323 534 : pt_pitch++;
324 : }
325 :
326 : /* write reserved bits */
327 121 : WHILE( unbits_PI > 0 )
328 : {
329 0 : i = s_min( unbits_PI, 16 );
330 0 : push_indice( hBstr, IND_UNUSED, 0, i );
331 0 : unbits_PI -= i;
332 : }
333 :
334 : /* write TC configuration */
335 121 : IF( EQ_16( L_frame_fx, L_FRAME ) )
336 : {
337 71 : IF( EQ_16( tc_subfr, TC_0_0 ) )
338 : {
339 31 : push_indice( hBstr, IND_TC_SUBFR, 1, 1 );
340 : }
341 40 : ELSE IF( EQ_16( tc_subfr, TC_0_64 ) )
342 : {
343 6 : push_indice( hBstr, IND_TC_SUBFR, 0, 1 );
344 6 : push_indice( hBstr, IND_TC_SUBFR, 1, 1 );
345 6 : push_indice( hBstr, IND_TC_SUBFR, 0, 1 );
346 6 : push_indice( hBstr, IND_TC_SUBFR, 1, 1 );
347 : }
348 34 : ELSE IF( EQ_16( tc_subfr, TC_0_128 ) )
349 : {
350 5 : push_indice( hBstr, IND_TC_SUBFR, 0, 1 );
351 5 : push_indice( hBstr, IND_TC_SUBFR, 1, 1 );
352 5 : push_indice( hBstr, IND_TC_SUBFR, 0, 1 );
353 5 : push_indice( hBstr, IND_TC_SUBFR, 0, 1 );
354 : }
355 29 : ELSE IF( EQ_16( tc_subfr, TC_0_192 ) )
356 : {
357 3 : push_indice( hBstr, IND_TC_SUBFR, 0, 1 );
358 3 : push_indice( hBstr, IND_TC_SUBFR, 1, 1 );
359 3 : push_indice( hBstr, IND_TC_SUBFR, 1, 1 );
360 : }
361 26 : ELSE IF( EQ_16( tc_subfr, L_SUBFR ) )
362 : {
363 5 : push_indice( hBstr, IND_TC_SUBFR, 0, 1 );
364 5 : push_indice( hBstr, IND_TC_SUBFR, 0, 1 );
365 5 : push_indice( hBstr, IND_TC_SUBFR, 1, 1 );
366 : }
367 21 : ELSE IF( EQ_16( tc_subfr, 2 * L_SUBFR ) )
368 : {
369 1 : push_indice( hBstr, IND_TC_SUBFR, 0, 1 );
370 1 : push_indice( hBstr, IND_TC_SUBFR, 0, 1 );
371 1 : push_indice( hBstr, IND_TC_SUBFR, 0, 1 );
372 1 : push_indice( hBstr, IND_TC_SUBFR, 1, 1 );
373 : }
374 20 : ELSE IF( EQ_16( tc_subfr, 3 * L_SUBFR ) )
375 : {
376 20 : push_indice( hBstr, IND_TC_SUBFR, 0, 1 );
377 20 : push_indice( hBstr, IND_TC_SUBFR, 0, 1 );
378 20 : push_indice( hBstr, IND_TC_SUBFR, 0, 1 );
379 20 : push_indice( hBstr, IND_TC_SUBFR, 0, 1 );
380 : }
381 : }
382 : ELSE /* L_frame == L_FRAME16k */
383 : {
384 50 : IF( tc_subfr == 0 )
385 : {
386 39 : push_indice( hBstr, IND_TC_SUBFR, 0, 2 );
387 : }
388 11 : ELSE IF( EQ_16( tc_subfr, L_SUBFR ) )
389 : {
390 10 : push_indice( hBstr, IND_TC_SUBFR, 1, 2 );
391 : }
392 1 : ELSE IF( EQ_16( tc_subfr, 2 * L_SUBFR ) )
393 : {
394 1 : push_indice( hBstr, IND_TC_SUBFR, 2, 2 );
395 : }
396 0 : ELSE IF( EQ_16( tc_subfr, 3 * L_SUBFR ) )
397 : {
398 0 : push_indice( hBstr, IND_TC_SUBFR, 3, 2 );
399 0 : push_indice( hBstr, IND_TC_SUBFR, 0, 1 );
400 : }
401 0 : ELSE IF( EQ_16( tc_subfr, 4 * L_SUBFR ) )
402 : {
403 0 : push_indice( hBstr, IND_TC_SUBFR, 3, 2 );
404 0 : push_indice( hBstr, IND_TC_SUBFR, 1, 1 );
405 : }
406 : }
407 :
408 121 : IF( st_fx->Opt_SC_VBR )
409 : {
410 : /* SC-VBR */
411 0 : hSC_VBR->prev_ppp_gain_pit_fx = gain_pit; /* Q14 */
412 0 : move16();
413 0 : hSC_VBR->prev_tilt_code_fx = hLPDmem->tilt_code; /* Q15 */
414 0 : move16();
415 : }
416 :
417 121 : return tc_subfr;
418 : }
419 :
420 14123 : Word16 encod_tran_ivas_fx(
421 : Encoder_State *st_fx, /* i/o: state structure */
422 : const Word16 speech_fx[], /* i : input speech Q0*/
423 : const Word16 Aw_fx[], /* i : weighted A(z) unquantized for subframes Q12*/
424 : const Word16 Aq_fx[], /* i : 12k8 Lp coefficient Q12*/
425 : const Word16 Es_pred_fx, /* i : predicted scaled innov. energy Q8*/
426 : const Word16 *res_fx, /* i : residual signal Q_new*/
427 : Word16 *syn_fx, /* i/o: core synthesis Q_new*/
428 : Word16 *exc_fx, /* i/o: current non-enhanced excitation Q0*/
429 : Word16 *exc2_fx, /* i/o: current enhanced excitation Q0*/
430 : Word16 *pitch_buf_fx, /* i/o: floating pitch values for each subframe Q6*/
431 : Word16 *voice_factors, /* o : voicing factors Q15*/
432 : Word16 *bwe_exc_fx, /* i/o: excitation for SWB TBE Q0*/
433 : Word16 tc_subfr, /* i/o: TC subframe classification Q0*/
434 : Word16 position, /* i : maximum of residual signal index Q0*/
435 : Word16 *unbits, /* i/o: number of unused bits Q0*/
436 : const Word16 shift, /* i : Scaling to get 12 bits */
437 : const Word16 Q_new /* i : Input scaling */
438 : )
439 : {
440 : Word16 xn[L_SUBFR]; /* Target vector for pitch search */
441 : Word16 xn2[L_SUBFR]; /* Target vector for codebook search */
442 : Word16 cn[L_SUBFR]; /* Target vector in residual domain */
443 : Word16 h1[L_SUBFR + ( M + 1 )]; /* Impulse response vector */
444 : Word16 h2_fx[L_SUBFR + ( M + 1 )]; /* Impulse response vector */
445 : Word16 code[L_SUBFR]; /* Fixed codebook excitation */
446 : Word16 y1[L_SUBFR]; /* Filtered adaptive excitation */
447 : Word16 y2[L_SUBFR]; /* Filtered algebraic excitation */
448 14123 : Word16 gain_pit = 0, Gain_pitX2, gcode16; /* Pitch gain */
449 : Word16 voice_fac; /* Voicing factor */
450 14123 : Word32 gain_code = 0; /* Gain of code */
451 : Word32 Lgcode;
452 14123 : Word16 gain_inov = 0; /* inovation gain */
453 : Word16 i, i_subfr, tmp1_fx, tmp_fx; /* tmp variables */
454 : Word16 unbits_ACELP;
455 : Word16 T0_min, T0_max; /* pitch and TC variables */
456 : Word16 T0, T0_frac; /* close loop integer pitch and fractional part */
457 : Word16 *pt_pitch; /* pointer to floating pitch buffer */
458 : Word16 g_corr[10]; /* ACELP correlation values and gain pitch */
459 : Word16 clip_gain; /* LSF clip gain */
460 : const Word16 *p_Aw, *p_Aq; /* pointer to LP filter coefficient vector */
461 14123 : Word16 gain_preQ = 0; /* Gain of prequantizer excitation */
462 : Word16 code_preQ[L_SUBFR]; /* Prequantizer excitation */
463 : Word16 Jopt_flag; /* joint optimization flag */
464 14123 : Word16 unbits_PI = 0; /* saved bits for PI */
465 14123 : Word32 norm_gain_code = 0;
466 : Word16 L_frame_fx;
467 : Word16 shift_wsp;
468 : Word32 L_tmp;
469 : Word16 q_h1;
470 : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
471 14123 : Flag Overflow = 0;
472 14123 : move32();
473 : #endif
474 14123 : BSTR_ENC_HANDLE hBstr = st_fx->hBstr;
475 14123 : SC_VBR_ENC_HANDLE hSC_VBR = st_fx->hSC_VBR;
476 14123 : LPD_state_HANDLE hLPDmem = st_fx->hLPDmem;
477 :
478 14123 : L_frame_fx = st_fx->L_frame;
479 14123 : move16();
480 :
481 14123 : set16_fx( h1, 0, L_SUBFR + ( M + 1 ) );
482 :
483 : /*------------------------------------------------------------------*
484 : * Initializations
485 : *------------------------------------------------------------------*/
486 14123 : gain_pit = 0;
487 14123 : move16();
488 14123 : gain_code = L_deposit_l( 0 );
489 14123 : gain_preQ = 0;
490 14123 : move16();
491 14123 : unbits_PI = 0;
492 14123 : move16();
493 14123 : IF( EQ_16( L_frame_fx, L_FRAME ) )
494 : {
495 7365 : T0_max = PIT_MAX;
496 7365 : move16();
497 7365 : T0_min = PIT_MIN;
498 7365 : move16();
499 : }
500 : ELSE /* L_frame == L_FRAME16k */
501 : {
502 6758 : T0_max = PIT16k_MAX;
503 6758 : move16();
504 6758 : T0_min = PIT16k_MIN;
505 6758 : move16();
506 : }
507 :
508 : /**unbits = 0;move16();*/
509 14123 : Jopt_flag = 0;
510 14123 : move16();
511 14123 : unbits_ACELP = *unbits; /* Q0 */
512 14123 : move16();
513 14123 : *unbits = 0;
514 14123 : move16();
515 :
516 14123 : p_Aw = Aw_fx; /* Q12 */
517 14123 : p_Aq = Aq_fx; /* Q12 */
518 14123 : pt_pitch = pitch_buf_fx;
519 14123 : gain_preQ = 0;
520 14123 : move16();
521 14123 : set16_fx( code_preQ, 0, L_SUBFR );
522 14123 : shift_wsp = add( Q_new, shift );
523 14123 : if ( LT_32( st_fx->core_brate, MIN_BRATE_AVQ_EXC ) )
524 : {
525 12842 : shift_wsp = sub( shift_wsp, 1 );
526 : }
527 : /*----------------------------------------------------------------*
528 : * ACELP subframe loop
529 : *----------------------------------------------------------------*/
530 :
531 77373 : FOR( i_subfr = 0; i_subfr < L_frame_fx; i_subfr += L_SUBFR )
532 : {
533 : /*----------------------------------------------------------------*
534 : * Find the the excitation search target "xn" and innovation
535 : * target in residual domain "cn"
536 : * Compute impulse response, h1[], of weighted synthesis filter
537 : *----------------------------------------------------------------*/
538 :
539 63250 : Copy( &res_fx[i_subfr], &exc_fx[i_subfr], L_SUBFR ); /* Q_new */
540 :
541 :
542 63250 : find_targets_ivas_fx( speech_fx, hLPDmem->mem_syn, i_subfr, &hLPDmem->mem_w0, p_Aq,
543 63250 : res_fx, L_SUBFR, p_Aw, st_fx->preemph_fac, xn, cn, h1 );
544 :
545 63250 : q_h1 = sub( 14, norm_s( h1[0] ) );
546 63250 : Copy_Scale_sig( h1, h2_fx, L_SUBFR, sub( 11, q_h1 ) ); /*Q11*/
547 63250 : Scale_sig( h1, L_SUBFR, sub( 14, q_h1 ) );
548 :
549 : /* scaling of xn[] to limit dynamic at 12 bits */
550 63250 : Scale_sig( xn, L_SUBFR, shift );
551 :
552 : /*-----------------------------------------------------------------*
553 : * TC: subframe determination &
554 : * adaptive/glottal part of excitation construction
555 : *-----------------------------------------------------------------*/
556 :
557 63250 : transition_enc_ivas_fx( st_fx, i_subfr, &tc_subfr, &Jopt_flag, &position, &T0, &T0_frac, &T0_min, &T0_max, exc_fx, y1,
558 63250 : h1, xn, xn2, st_fx->clip_var_fx, &gain_pit, g_corr, &clip_gain, &pt_pitch, bwe_exc_fx, &unbits_ACELP, Q_new, shift );
559 :
560 : /*-----------------------------------------------------------------*
561 : * Transform domain contribution encoding - active frames
562 : *-----------------------------------------------------------------*/
563 :
564 63250 : IF( GE_32( st_fx->core_brate, MIN_BRATE_AVQ_EXC ) )
565 : {
566 6405 : transf_cdbk_enc_ivas_fx( st_fx, 0, i_subfr, cn, exc_fx, p_Aq, Aw_fx, h1, xn, xn2, y1, y2, Es_pred_fx,
567 : &gain_pit, gain_code, g_corr, clip_gain, &gain_preQ, code_preQ, unbits, Q_new, shift );
568 : }
569 :
570 : /*-----------------------------------------------------------------*
571 : * ACELP codebook search + pitch sharpening
572 : *-----------------------------------------------------------------*/
573 :
574 63250 : inov_encode_ivas_fx( st_fx, st_fx->core_brate, 0, L_frame_fx, st_fx->last_L_frame, st_fx->coder_type, st_fx->bwidth, st_fx->sharpFlag,
575 63250 : i_subfr, tc_subfr, p_Aq, gain_pit, cn, exc_fx, h2_fx, hLPDmem->tilt_code, *pt_pitch, xn2, code, y2, &unbits_PI, L_SUBFR, shift, Q_new );
576 :
577 63250 : test();
578 63250 : test();
579 63250 : test();
580 63250 : if ( ( EQ_16( st_fx->L_frame, L_FRAME16k ) ) && ( tc_subfr == 0 ) && ( EQ_16( i_subfr, L_SUBFR ) ) && ( EQ_16( T0, 2 * L_SUBFR ) ) )
581 : {
582 477 : Jopt_flag = 1;
583 477 : move16();
584 : }
585 : /*-----------------------------------------------------------------*
586 : * Quantize the gains
587 : * Test quantized gain of pitch for pitch clipping algorithm
588 : * Update tilt of code: 0.0 (unvoiced) to 0.5 (voiced)
589 : *-----------------------------------------------------------------*/
590 63250 : IF( Jopt_flag == 0 )
591 : {
592 : /* SQ gain_code */
593 20150 : gain_enc_tc_ivas_fx( hBstr, st_fx->acelp_cfg.gains_mode, i_subfr, xn, y2, code, Es_pred_fx,
594 : &gain_pit, &gain_code, &gain_inov, &norm_gain_code, shift_wsp );
595 : }
596 : ELSE
597 : {
598 43100 : IF( GT_32( st_fx->core_brate, ACELP_32k ) )
599 : {
600 : /* SQ gain_pit and gain_code */
601 2801 : gain_enc_SQ_ivas_fx( hBstr, st_fx->acelp_cfg.gains_mode, i_subfr, xn, y1, y2, code, Es_pred_fx,
602 : &gain_pit, &gain_code, &gain_inov, &norm_gain_code, g_corr, clip_gain, shift_wsp );
603 : }
604 : ELSE
605 : {
606 : /* VQ gain_pit and gain_code */
607 40299 : gain_enc_mless_ivas_fx( hBstr, st_fx->acelp_cfg.gains_mode, st_fx->element_mode, L_frame_fx, i_subfr, tc_subfr, xn, y1, shift_wsp, y2, code, Es_pred_fx,
608 : &gain_pit, &gain_code, &gain_inov, &norm_gain_code, g_corr, clip_gain );
609 : }
610 : }
611 63250 : gp_clip_test_gain_pit_fx( st_fx->element_mode, st_fx->core_brate, gain_pit, st_fx->clip_var_fx );
612 :
613 63250 : Lgcode = L_shl_o( gain_code, Q_new, &Overflow ); /* scaled gain_code with Qnew -> Q16*/
614 63250 : gcode16 = round_fx_o( Lgcode, &Overflow );
615 63250 : hLPDmem->tilt_code = est_tilt_ivas_fx( &exc_fx[i_subfr], gain_pit, code, gain_code, &voice_fac, Q_new, L_SUBFR, 0 ); // Q15
616 : /*-----------------------------------------------------------------*
617 : * Update memory of the weighting filter
618 : *-----------------------------------------------------------------*/
619 :
620 : /*st->mem_w0 = xn[L_SUBFR-1] - (gain_pit*y1[L_SUBFR-1]) - (gain_code*y2[L_SUBFR-1]);*/
621 63250 : L_tmp = L_mult0( gcode16, y2[L_SUBFR - 1] );
622 63250 : L_tmp = L_shl( L_tmp, add( 5, shift ) ); // Q_new+14+shift
623 63250 : L_tmp = L_negate( L_tmp );
624 63250 : L_tmp = L_mac( L_tmp, xn[L_SUBFR - 1], 16384 ); // Q_new-1+15+shift
625 63250 : L_tmp = L_msu( L_tmp, y1[L_SUBFR - 1], gain_pit ); // Q_new-1+15+shift
626 63250 : L_tmp = L_shl_sat( L_tmp, sub( 1, shift ) ); // Q_new+15
627 63250 : hLPDmem->mem_w0 = round_fx_sat( L_tmp ); /*Q_new-1*/
628 : /*-----------------------------------------------------------------*
629 : * Construct adaptive part of the excitation
630 : * Save the non-enhanced excitation for FEC_exc
631 : *-----------------------------------------------------------------*/
632 :
633 : /* Here, all these conditions have one purpose: to use */
634 : /* the most efficient loop (the one with the least ops) */
635 : /* This is done by upscaling gain_pit_fx and/or gain_code16 */
636 : /* when they don't use all 16 bits of precision */
637 :
638 : /* exc Q_exc, gpit Q14, code Q12, gcode Q0 */
639 63250 : IF( norm_s( gain_pit ) == 0 )
640 : {
641 1710410 : FOR( i = 0; i < L_SUBFR; i++ )
642 : {
643 1684096 : exc2_fx[i + i_subfr] = round_fx_sat( L_shl_sat( L_mult_sat( gain_pit, exc_fx[i + i_subfr] ), 1 ) ); /* Q_exc */
644 : }
645 : }
646 : ELSE
647 : {
648 36936 : Gain_pitX2 = shl( gain_pit, 1 );
649 2400840 : FOR( i = 0; i < L_SUBFR; i++ )
650 : {
651 2363904 : exc2_fx[i + i_subfr] = mult_r( Gain_pitX2, exc_fx[i + i_subfr] ); /* Q_exc */
652 : }
653 : }
654 :
655 : /*-----------------------------------------------------------------*
656 : * Construct adaptive part of the excitation
657 : * Save the non-enhanced excitation for FEC_exc
658 : *-----------------------------------------------------------------*/
659 4111250 : FOR( i = 0; i < L_SUBFR; i++ )
660 : {
661 : /* code in Q9, gain_pit in Q14 */
662 4048000 : L_tmp = L_mult( gcode16, code[i] ); /* Q10 + Q_new */
663 4048000 : L_tmp = L_shl_o( L_tmp, 5, &Overflow ); /* Q15 + Q_new */
664 4048000 : L_tmp = L_mac_o( L_tmp, exc_fx[i + i_subfr], gain_pit, &Overflow ); /* Q15 + Q_new */
665 4048000 : L_tmp = L_shl_o( L_tmp, 1, &Overflow ); /* saturation can occur here Q16 + Q_new */
666 4048000 : exc_fx[i + i_subfr] = round_fx_o( L_tmp, &Overflow ); /* Q_new */
667 : }
668 :
669 : /*-----------------------------------------------------------------*
670 : * Add the ACELP pre-quantizer contribution
671 : *-----------------------------------------------------------------*/
672 :
673 63250 : IF( GE_32( st_fx->core_brate, MIN_BRATE_AVQ_EXC ) )
674 : {
675 6405 : tmp1_fx = add( 16 - ( 2 + Q_AVQ_OUT_DEC + 1 ), Q_new );
676 416325 : FOR( i = 0; i < L_SUBFR; i++ )
677 : {
678 409920 : L_tmp = L_mult( gain_preQ, code_preQ[i] ); /* Q2 + Q10 -> Q13*/
679 409920 : L_tmp = L_shl_o( L_tmp, tmp1_fx, &Overflow ); /* Q16 + Q_exc */
680 409920 : tmp_fx = round_fx_o( L_tmp, &Overflow );
681 :
682 409920 : exc2_fx[i + i_subfr] = add_o( exc2_fx[i + i_subfr], tmp_fx, &Overflow ); /* Q_exc */
683 409920 : move16();
684 409920 : exc_fx[i + i_subfr] = add_o( exc_fx[i + i_subfr], tmp_fx, &Overflow ); /* Q_exc */
685 409920 : move16();
686 : }
687 : }
688 :
689 : /*-----------------------------------------------------------------*
690 : * Prepare TBE excitation
691 : *-----------------------------------------------------------------*/
692 :
693 63250 : prep_tbe_exc_fx( L_frame_fx, L_SUBFR, i_subfr, gain_pit, gain_code, code, voice_fac, &voice_factors[i_subfr / L_SUBFR],
694 63250 : bwe_exc_fx, gain_preQ, code_preQ, Q_new, T0, T0_frac, st_fx->coder_type, st_fx->core_brate,
695 63250 : st_fx->element_mode, st_fx->idchan, st_fx->hBWE_TD != NULL, st_fx->tdm_LRTD_flag );
696 :
697 : /*-----------------------------------------------------------------*
698 : * Synthesize speech to update mem_syn[].
699 : * Update A(z) filters
700 : *-----------------------------------------------------------------*/
701 :
702 63250 : Syn_filt_s( 1, p_Aq, M, &exc_fx[i_subfr], &syn_fx[i_subfr], L_SUBFR, hLPDmem->mem_syn, 1 );
703 :
704 63250 : p_Aw += ( M + 1 );
705 63250 : p_Aq += ( M + 1 );
706 63250 : pt_pitch++;
707 : }
708 :
709 : /* write reserved bits */
710 14123 : WHILE( unbits_PI > 0 )
711 : {
712 0 : i = s_min( unbits_PI, 16 );
713 0 : push_indice( hBstr, IND_UNUSED, 0, i );
714 0 : unbits_PI -= i;
715 : }
716 :
717 : /* write TC configuration */
718 14123 : IF( EQ_16( L_frame_fx, L_FRAME ) )
719 : {
720 7365 : IF( EQ_16( tc_subfr, TC_0_0 ) )
721 : {
722 1615 : push_indice( hBstr, IND_TC_SUBFR, 1, 1 );
723 : }
724 5750 : ELSE IF( EQ_16( tc_subfr, TC_0_64 ) )
725 : {
726 1354 : push_indice( hBstr, IND_TC_SUBFR, 0, 1 );
727 1354 : push_indice( hBstr, IND_TC_SUBFR, 1, 1 );
728 1354 : push_indice( hBstr, IND_TC_SUBFR, 0, 1 );
729 1354 : push_indice( hBstr, IND_TC_SUBFR, 1, 1 );
730 : }
731 4396 : ELSE IF( EQ_16( tc_subfr, TC_0_128 ) )
732 : {
733 676 : push_indice( hBstr, IND_TC_SUBFR, 0, 1 );
734 676 : push_indice( hBstr, IND_TC_SUBFR, 1, 1 );
735 676 : push_indice( hBstr, IND_TC_SUBFR, 0, 1 );
736 676 : push_indice( hBstr, IND_TC_SUBFR, 0, 1 );
737 : }
738 3720 : ELSE IF( EQ_16( tc_subfr, TC_0_192 ) )
739 : {
740 202 : push_indice( hBstr, IND_TC_SUBFR, 0, 1 );
741 202 : push_indice( hBstr, IND_TC_SUBFR, 1, 1 );
742 202 : push_indice( hBstr, IND_TC_SUBFR, 1, 1 );
743 : }
744 3518 : ELSE IF( EQ_16( tc_subfr, L_SUBFR ) )
745 : {
746 1166 : push_indice( hBstr, IND_TC_SUBFR, 0, 1 );
747 1166 : push_indice( hBstr, IND_TC_SUBFR, 0, 1 );
748 1166 : push_indice( hBstr, IND_TC_SUBFR, 1, 1 );
749 : }
750 2352 : ELSE IF( EQ_16( tc_subfr, 2 * L_SUBFR ) )
751 : {
752 548 : push_indice( hBstr, IND_TC_SUBFR, 0, 1 );
753 548 : push_indice( hBstr, IND_TC_SUBFR, 0, 1 );
754 548 : push_indice( hBstr, IND_TC_SUBFR, 0, 1 );
755 548 : push_indice( hBstr, IND_TC_SUBFR, 1, 1 );
756 : }
757 1804 : ELSE IF( EQ_16( tc_subfr, 3 * L_SUBFR ) )
758 : {
759 1804 : push_indice( hBstr, IND_TC_SUBFR, 0, 1 );
760 1804 : push_indice( hBstr, IND_TC_SUBFR, 0, 1 );
761 1804 : push_indice( hBstr, IND_TC_SUBFR, 0, 1 );
762 1804 : push_indice( hBstr, IND_TC_SUBFR, 0, 1 );
763 : }
764 : }
765 : ELSE /* L_frame == L_FRAME16k */
766 : {
767 6758 : IF( tc_subfr == 0 )
768 : {
769 2771 : push_indice( hBstr, IND_TC_SUBFR, 0, 2 );
770 : }
771 3987 : ELSE IF( EQ_16( tc_subfr, L_SUBFR ) )
772 : {
773 991 : push_indice( hBstr, IND_TC_SUBFR, 1, 2 );
774 : }
775 2996 : ELSE IF( EQ_16( tc_subfr, 2 * L_SUBFR ) )
776 : {
777 668 : push_indice( hBstr, IND_TC_SUBFR, 2, 2 );
778 : }
779 2328 : ELSE IF( EQ_16( tc_subfr, 3 * L_SUBFR ) )
780 : {
781 243 : push_indice( hBstr, IND_TC_SUBFR, 3, 2 );
782 243 : push_indice( hBstr, IND_TC_SUBFR, 0, 1 );
783 : }
784 2085 : ELSE IF( EQ_16( tc_subfr, 4 * L_SUBFR ) )
785 : {
786 2085 : push_indice( hBstr, IND_TC_SUBFR, 3, 2 );
787 2085 : push_indice( hBstr, IND_TC_SUBFR, 1, 1 );
788 : }
789 : }
790 :
791 14123 : IF( st_fx->Opt_SC_VBR )
792 : {
793 : /* SC-VBR */
794 0 : hSC_VBR->prev_ppp_gain_pit_fx = gain_pit; /* Q14 */
795 0 : move16();
796 0 : hSC_VBR->prev_tilt_code_fx = hLPDmem->tilt_code; /* Q15 */
797 0 : move16();
798 : }
799 :
800 14123 : return tc_subfr;
801 : }
|