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 125 : 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 125 : Word16 gain_pit = 0, Gain_pitX2, gcode16; /* Pitch gain */
76 : Word16 voice_fac; /* Voicing factor */
77 125 : Word32 gain_code = 0; /* Gain of code */
78 : Word32 Lgcode;
79 125 : 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 125 : Word16 gain_preQ = 0; /* Gain of prequantizer excitation */
89 : Word16 code_preQ[L_SUBFR]; /* Prequantizer excitation */
90 : Word16 Jopt_flag; /* joint optimization flag */
91 125 : Word16 unbits_PI = 0; /* saved bits for PI */
92 125 : 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 125 : Flag Overflow = 0;
98 125 : move32();
99 : #endif
100 125 : BSTR_ENC_HANDLE hBstr = st_fx->hBstr;
101 125 : SC_VBR_ENC_HANDLE hSC_VBR = st_fx->hSC_VBR;
102 125 : LPD_state_HANDLE hLPDmem = st_fx->hLPDmem;
103 :
104 125 : L_frame_fx = st_fx->L_frame;
105 125 : move16();
106 : /*------------------------------------------------------------------*
107 : * Initializations
108 : *------------------------------------------------------------------*/
109 125 : gain_pit = 0;
110 125 : move16();
111 125 : gain_code = L_deposit_l( 0 );
112 125 : gain_preQ = 0;
113 125 : move16();
114 125 : unbits_PI = 0;
115 125 : move16();
116 125 : IF( EQ_16( L_frame_fx, L_FRAME ) )
117 : {
118 75 : T0_max = PIT_MAX;
119 75 : move16();
120 75 : T0_min = PIT_MIN;
121 75 : 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 125 : Jopt_flag = 0;
133 125 : move16();
134 125 : unbits_ACELP = *unbits; /* Q0 */
135 125 : move16();
136 125 : *unbits = 0;
137 125 : move16();
138 :
139 125 : p_Aw = Aw_fx; /* Q12 */
140 125 : p_Aq = Aq_fx; /* Q12 */
141 125 : pt_pitch = pitch_buf_fx; /* Q6 */
142 125 : gain_preQ = 0;
143 125 : move16();
144 125 : set16_fx( code_preQ, 0, L_SUBFR );
145 125 : shift_wsp = add( Q_new, shift );
146 :
147 : /*----------------------------------------------------------------*
148 : * ACELP subframe loop
149 : *----------------------------------------------------------------*/
150 :
151 675 : 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 550 : Copy( &res_fx[i_subfr], &exc_fx[i_subfr], L_SUBFR ); /* Q_new */
160 :
161 550 : find_targets_fx( speech_fx, hLPDmem->mem_syn, i_subfr, &hLPDmem->mem_w0, p_Aq,
162 550 : res_fx, L_SUBFR, p_Aw, st_fx->preemph_fac, xn, cn, h1 );
163 :
164 550 : Copy_Scale_sig( h1, h2_fx, L_SUBFR, -2 );
165 550 : 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 550 : Scale_sig( xn, L_SUBFR, shift );
169 :
170 : /*-----------------------------------------------------------------*
171 : * TC: subframe determination &
172 : * adaptive/glottal part of excitation construction
173 : *-----------------------------------------------------------------*/
174 :
175 550 : transition_enc_fx( st_fx, i_subfr, &tc_subfr, &Jopt_flag, &position, &T0, &T0_frac, &T0_min, &T0_max, exc_fx, y1,
176 550 : 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 550 : 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 550 : 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 550 : 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 550 : test();
196 550 : test();
197 550 : test();
198 550 : 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 550 : IF( Jopt_flag == 0 )
209 : {
210 : /* SQ gain_code */
211 102 : 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 448 : 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 210 : 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 550 : gp_clip_test_gain_pit_fx( st_fx->element_mode, st_fx->core_brate, gain_pit, st_fx->clip_var_fx );
230 :
231 550 : Lgcode = L_shl_o( gain_code, Q_new, &Overflow ); /* scaled gain_code with Qnew -> Q16*/
232 550 : gcode16 = round_fx_o( Lgcode, &Overflow );
233 550 : 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 550 : L_tmp = L_mult( gcode16, y2[L_SUBFR - 1] ); /* Q10 + Q_new */
240 550 : L_tmp = L_shl( L_tmp, add( 5, shift ) ); /* Q15 + Q_new + shift */
241 550 : L_tmp = L_negate( L_tmp );
242 550 : L_tmp = L_mac( L_tmp, xn[L_SUBFR - 1], 16384 );
243 550 : L_tmp = L_msu( L_tmp, y1[L_SUBFR - 1], gain_pit );
244 550 : L_tmp = L_shl_sat( L_tmp, sub( 1, shift ) ); /* Q_new + 15 */
245 550 : 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 550 : IF( norm_s( gain_pit ) == 0 )
258 : {
259 13065 : FOR( i = 0; i < L_SUBFR; i++ )
260 : {
261 12864 : 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 349 : Gain_pitX2 = shl( gain_pit, 1 ); /* Q15 */
267 22685 : FOR( i = 0; i < L_SUBFR; i++ )
268 : {
269 22336 : 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 35750 : FOR( i = 0; i < L_SUBFR; i++ )
278 : {
279 : /* code in Q9, gain_pit in Q14 */
280 35200 : L_tmp = L_mult( gcode16, code[i] ); /* Q10 + Q_new */
281 35200 : L_tmp = L_shl_o( L_tmp, 5, &Overflow ); /* Q15 + Q_new */
282 35200 : L_tmp = L_mac_o( L_tmp, exc_fx[i + i_subfr], gain_pit, &Overflow ); /* Q15 + Q_new */
283 35200 : L_tmp = L_shl_o( L_tmp, 1, &Overflow ); /* saturation can occur here Q16 + Q_new */
284 35200 : 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 550 : 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 550 : 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 550 : 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 550 : Syn_filt_s( 1, p_Aq, M, &exc_fx[i_subfr], &syn_fx[i_subfr], L_SUBFR, hLPDmem->mem_syn, 1 );
320 :
321 550 : p_Aw += ( M + 1 );
322 550 : p_Aq += ( M + 1 );
323 550 : pt_pitch++;
324 : }
325 :
326 : /* write reserved bits */
327 125 : 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 125 : IF( EQ_16( L_frame_fx, L_FRAME ) )
336 : {
337 75 : IF( EQ_16( tc_subfr, TC_0_0 ) )
338 : {
339 30 : push_indice( hBstr, IND_TC_SUBFR, 1, 1 );
340 : }
341 45 : 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 39 : ELSE IF( EQ_16( tc_subfr, TC_0_128 ) )
349 : {
350 6 : push_indice( hBstr, IND_TC_SUBFR, 0, 1 );
351 6 : push_indice( hBstr, IND_TC_SUBFR, 1, 1 );
352 6 : push_indice( hBstr, IND_TC_SUBFR, 0, 1 );
353 6 : push_indice( hBstr, IND_TC_SUBFR, 0, 1 );
354 : }
355 33 : ELSE IF( EQ_16( tc_subfr, TC_0_192 ) )
356 : {
357 4 : push_indice( hBstr, IND_TC_SUBFR, 0, 1 );
358 4 : push_indice( hBstr, IND_TC_SUBFR, 1, 1 );
359 4 : push_indice( hBstr, IND_TC_SUBFR, 1, 1 );
360 : }
361 29 : 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 24 : 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 23 : ELSE IF( EQ_16( tc_subfr, 3 * L_SUBFR ) )
375 : {
376 23 : push_indice( hBstr, IND_TC_SUBFR, 0, 1 );
377 23 : push_indice( hBstr, IND_TC_SUBFR, 0, 1 );
378 23 : push_indice( hBstr, IND_TC_SUBFR, 0, 1 );
379 23 : 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 125 : 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 125 : return tc_subfr;
418 : }
419 :
420 14086 : 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 14086 : Word16 gain_pit = 0, Gain_pitX2, gcode16; /* Pitch gain */
449 : Word16 voice_fac; /* Voicing factor */
450 14086 : Word32 gain_code = 0; /* Gain of code */
451 : Word32 Lgcode;
452 14086 : 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 14086 : Word16 gain_preQ = 0; /* Gain of prequantizer excitation */
462 : Word16 code_preQ[L_SUBFR]; /* Prequantizer excitation */
463 : Word16 Jopt_flag; /* joint optimization flag */
464 14086 : Word16 unbits_PI = 0; /* saved bits for PI */
465 14086 : 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 14086 : Flag Overflow = 0;
472 14086 : move32();
473 : #endif
474 14086 : BSTR_ENC_HANDLE hBstr = st_fx->hBstr;
475 14086 : SC_VBR_ENC_HANDLE hSC_VBR = st_fx->hSC_VBR;
476 14086 : LPD_state_HANDLE hLPDmem = st_fx->hLPDmem;
477 :
478 14086 : L_frame_fx = st_fx->L_frame;
479 14086 : move16();
480 :
481 14086 : set16_fx( h1, 0, L_SUBFR + ( M + 1 ) );
482 :
483 : /*------------------------------------------------------------------*
484 : * Initializations
485 : *------------------------------------------------------------------*/
486 14086 : gain_pit = 0;
487 14086 : move16();
488 14086 : gain_code = L_deposit_l( 0 );
489 14086 : gain_preQ = 0;
490 14086 : move16();
491 14086 : unbits_PI = 0;
492 14086 : move16();
493 14086 : IF( EQ_16( L_frame_fx, L_FRAME ) )
494 : {
495 7347 : T0_max = PIT_MAX;
496 7347 : move16();
497 7347 : T0_min = PIT_MIN;
498 7347 : move16();
499 : }
500 : ELSE /* L_frame == L_FRAME16k */
501 : {
502 6739 : T0_max = PIT16k_MAX;
503 6739 : move16();
504 6739 : T0_min = PIT16k_MIN;
505 6739 : move16();
506 : }
507 :
508 : /**unbits = 0;move16();*/
509 14086 : Jopt_flag = 0;
510 14086 : move16();
511 14086 : unbits_ACELP = *unbits; /* Q0 */
512 14086 : move16();
513 14086 : *unbits = 0;
514 14086 : move16();
515 :
516 14086 : p_Aw = Aw_fx; /* Q12 */
517 14086 : p_Aq = Aq_fx; /* Q12 */
518 14086 : pt_pitch = pitch_buf_fx;
519 14086 : gain_preQ = 0;
520 14086 : move16();
521 14086 : set16_fx( code_preQ, 0, L_SUBFR );
522 14086 : shift_wsp = add( Q_new, shift );
523 : #ifdef TEST_HR
524 14086 : if ( LT_32( st_fx->core_brate, MIN_BRATE_AVQ_EXC ) )
525 : {
526 12818 : shift_wsp = sub( shift_wsp, 1 );
527 : }
528 : #endif
529 : /*----------------------------------------------------------------*
530 : * ACELP subframe loop
531 : *----------------------------------------------------------------*/
532 :
533 77169 : FOR( i_subfr = 0; i_subfr < L_frame_fx; i_subfr += L_SUBFR )
534 : {
535 : /*----------------------------------------------------------------*
536 : * Find the the excitation search target "xn" and innovation
537 : * target in residual domain "cn"
538 : * Compute impulse response, h1[], of weighted synthesis filter
539 : *----------------------------------------------------------------*/
540 :
541 63083 : Copy( &res_fx[i_subfr], &exc_fx[i_subfr], L_SUBFR ); /* Q_new */
542 :
543 :
544 63083 : find_targets_ivas_fx( speech_fx, hLPDmem->mem_syn, i_subfr, &hLPDmem->mem_w0, p_Aq,
545 63083 : res_fx, L_SUBFR, p_Aw, st_fx->preemph_fac, xn, cn, h1 );
546 :
547 63083 : q_h1 = sub( 14, norm_s( h1[0] ) );
548 63083 : Copy_Scale_sig( h1, h2_fx, L_SUBFR, sub( 11, q_h1 ) ); /*Q11*/
549 : #ifdef TEST_HR
550 63083 : Scale_sig( h1, L_SUBFR, sub( 14, q_h1 ) );
551 : #else
552 : Scale_sig( h1, L_SUBFR, sub( 13, q_h1 ) );
553 : #endif
554 :
555 : /* scaling of xn[] to limit dynamic at 12 bits */
556 63083 : Scale_sig( xn, L_SUBFR, shift );
557 :
558 : /*-----------------------------------------------------------------*
559 : * TC: subframe determination &
560 : * adaptive/glottal part of excitation construction
561 : *-----------------------------------------------------------------*/
562 :
563 63083 : transition_enc_ivas_fx( st_fx, i_subfr, &tc_subfr, &Jopt_flag, &position, &T0, &T0_frac, &T0_min, &T0_max, exc_fx, y1,
564 63083 : h1, xn, xn2, st_fx->clip_var_fx, &gain_pit, g_corr, &clip_gain, &pt_pitch, bwe_exc_fx, &unbits_ACELP, Q_new, shift );
565 :
566 : /*-----------------------------------------------------------------*
567 : * Transform domain contribution encoding - active frames
568 : *-----------------------------------------------------------------*/
569 :
570 63083 : IF( GE_32( st_fx->core_brate, MIN_BRATE_AVQ_EXC ) )
571 : {
572 : #ifndef TEST_HR
573 : Scale_sig( h1, L_SUBFR, add( 1, shift ) ); /* set h1[] in Q14 with scaling for convolution */
574 : #endif
575 6340 : 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,
576 : &gain_pit, gain_code, g_corr, clip_gain, &gain_preQ, code_preQ, unbits, Q_new, shift );
577 : }
578 :
579 : /*-----------------------------------------------------------------*
580 : * ACELP codebook search + pitch sharpening
581 : *-----------------------------------------------------------------*/
582 :
583 63083 : 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,
584 63083 : 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 );
585 :
586 63083 : test();
587 63083 : test();
588 63083 : test();
589 63083 : if ( ( EQ_16( st_fx->L_frame, L_FRAME16k ) ) && ( tc_subfr == 0 ) && ( EQ_16( i_subfr, L_SUBFR ) ) && ( EQ_16( T0, 2 * L_SUBFR ) ) )
590 : {
591 478 : Jopt_flag = 1;
592 478 : move16();
593 : }
594 : /*-----------------------------------------------------------------*
595 : * Quantize the gains
596 : * Test quantized gain of pitch for pitch clipping algorithm
597 : * Update tilt of code: 0.0 (unvoiced) to 0.5 (voiced)
598 : *-----------------------------------------------------------------*/
599 63083 : IF( Jopt_flag == 0 )
600 : {
601 : /* SQ gain_code */
602 : #ifdef TEST_HR
603 20107 : gain_enc_tc_ivas_fx( hBstr, st_fx->acelp_cfg.gains_mode, i_subfr, xn, y2, code, Es_pred_fx,
604 : &gain_pit, &gain_code, &gain_inov, &norm_gain_code, shift_wsp );
605 : #else
606 : gain_enc_tc_ivas_fx( hBstr, st_fx->acelp_cfg.gains_mode, i_subfr, xn, y2, code, Es_pred_fx,
607 : &gain_pit, &gain_code, &gain_inov, &norm_gain_code, sub( shift_wsp, 1 ) );
608 : #endif
609 : }
610 : ELSE
611 : {
612 42976 : IF( GT_32( st_fx->core_brate, ACELP_32k ) )
613 : {
614 : /* SQ gain_pit and gain_code */
615 : #ifdef TEST_HR
616 2770 : gain_enc_SQ_ivas_fx( hBstr, st_fx->acelp_cfg.gains_mode, i_subfr, xn, y1, y2, code, Es_pred_fx,
617 : &gain_pit, &gain_code, &gain_inov, &norm_gain_code, g_corr, clip_gain, shift_wsp );
618 : #else
619 : gain_enc_SQ_ivas_fx( hBstr, st_fx->acelp_cfg.gains_mode, i_subfr, xn, y1, y2, code, Es_pred_fx,
620 : &gain_pit, &gain_code, &gain_inov, &norm_gain_code, g_corr, clip_gain, sub( shift_wsp, 1 ) );
621 : #endif
622 : }
623 : ELSE
624 : {
625 : /* VQ gain_pit and gain_code */
626 : #ifdef TEST_HR
627 40206 : 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,
628 : &gain_pit, &gain_code, &gain_inov, &norm_gain_code, g_corr, clip_gain );
629 : #else
630 : 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, sub( shift_wsp, 1 ), y2, code, Es_pred_fx,
631 : &gain_pit, &gain_code, &gain_inov, &norm_gain_code, g_corr, clip_gain );
632 : #endif
633 : }
634 : }
635 63083 : gp_clip_test_gain_pit_fx( st_fx->element_mode, st_fx->core_brate, gain_pit, st_fx->clip_var_fx );
636 :
637 63083 : Lgcode = L_shl_o( gain_code, Q_new, &Overflow ); /* scaled gain_code with Qnew -> Q16*/
638 63083 : gcode16 = round_fx_o( Lgcode, &Overflow );
639 63083 : hLPDmem->tilt_code = est_tilt_ivas_fx( &exc_fx[i_subfr], gain_pit, code, gain_code, &voice_fac, Q_new, L_SUBFR, 0 ); // Q15
640 : /*-----------------------------------------------------------------*
641 : * Update memory of the weighting filter
642 : *-----------------------------------------------------------------*/
643 :
644 : /*st->mem_w0 = xn[L_SUBFR-1] - (gain_pit*y1[L_SUBFR-1]) - (gain_code*y2[L_SUBFR-1]);*/
645 63083 : L_tmp = L_mult0( gcode16, y2[L_SUBFR - 1] );
646 63083 : L_tmp = L_shl( L_tmp, add( 5, shift ) ); // Q_new+14+shift
647 63083 : L_tmp = L_negate( L_tmp );
648 63083 : L_tmp = L_mac( L_tmp, xn[L_SUBFR - 1], 16384 ); // Q_new-1+15+shift
649 63083 : L_tmp = L_msu( L_tmp, y1[L_SUBFR - 1], gain_pit ); // Q_new-1+15+shift
650 63083 : L_tmp = L_shl_sat( L_tmp, sub( 1, shift ) ); // Q_new+15
651 63083 : hLPDmem->mem_w0 = round_fx_sat( L_tmp ); /*Q_new-1*/
652 : /*-----------------------------------------------------------------*
653 : * Construct adaptive part of the excitation
654 : * Save the non-enhanced excitation for FEC_exc
655 : *-----------------------------------------------------------------*/
656 :
657 : /* Here, all these conditions have one purpose: to use */
658 : /* the most efficient loop (the one with the least ops) */
659 : /* This is done by upscaling gain_pit_fx and/or gain_code16 */
660 : /* when they don't use all 16 bits of precision */
661 :
662 : /* exc Q_exc, gpit Q14, code Q12, gcode Q0 */
663 63083 : IF( norm_s( gain_pit ) == 0 )
664 : {
665 1699100 : FOR( i = 0; i < L_SUBFR; i++ )
666 : {
667 1672960 : exc2_fx[i + i_subfr] = round_fx_sat( L_shl_sat( L_mult_sat( gain_pit, exc_fx[i + i_subfr] ), 1 ) ); /* Q_exc */
668 : }
669 : }
670 : ELSE
671 : {
672 36943 : Gain_pitX2 = shl( gain_pit, 1 );
673 2401295 : FOR( i = 0; i < L_SUBFR; i++ )
674 : {
675 2364352 : exc2_fx[i + i_subfr] = mult_r( Gain_pitX2, exc_fx[i + i_subfr] ); /* Q_exc */
676 : }
677 : }
678 :
679 : /*-----------------------------------------------------------------*
680 : * Construct adaptive part of the excitation
681 : * Save the non-enhanced excitation for FEC_exc
682 : *-----------------------------------------------------------------*/
683 4100395 : FOR( i = 0; i < L_SUBFR; i++ )
684 : {
685 : /* code in Q9, gain_pit in Q14 */
686 4037312 : L_tmp = L_mult( gcode16, code[i] ); /* Q10 + Q_new */
687 4037312 : L_tmp = L_shl_o( L_tmp, 5, &Overflow ); /* Q15 + Q_new */
688 4037312 : L_tmp = L_mac_o( L_tmp, exc_fx[i + i_subfr], gain_pit, &Overflow ); /* Q15 + Q_new */
689 4037312 : L_tmp = L_shl_o( L_tmp, 1, &Overflow ); /* saturation can occur here Q16 + Q_new */
690 4037312 : exc_fx[i + i_subfr] = round_fx_o( L_tmp, &Overflow ); /* Q_new */
691 : }
692 :
693 : /*-----------------------------------------------------------------*
694 : * Add the ACELP pre-quantizer contribution
695 : *-----------------------------------------------------------------*/
696 :
697 63083 : IF( GE_32( st_fx->core_brate, MIN_BRATE_AVQ_EXC ) )
698 : {
699 6340 : tmp1_fx = add( 16 - ( 2 + Q_AVQ_OUT_DEC + 1 ), Q_new );
700 412100 : FOR( i = 0; i < L_SUBFR; i++ )
701 : {
702 405760 : L_tmp = L_mult( gain_preQ, code_preQ[i] ); /* Q2 + Q10 -> Q13*/
703 405760 : L_tmp = L_shl_o( L_tmp, tmp1_fx, &Overflow ); /* Q16 + Q_exc */
704 405760 : tmp_fx = round_fx_o( L_tmp, &Overflow );
705 :
706 405760 : exc2_fx[i + i_subfr] = add_o( exc2_fx[i + i_subfr], tmp_fx, &Overflow ); /* Q_exc */
707 405760 : move16();
708 405760 : exc_fx[i + i_subfr] = add_o( exc_fx[i + i_subfr], tmp_fx, &Overflow ); /* Q_exc */
709 405760 : move16();
710 : }
711 : }
712 :
713 : /*-----------------------------------------------------------------*
714 : * Prepare TBE excitation
715 : *-----------------------------------------------------------------*/
716 :
717 63083 : prep_tbe_exc_fx( L_frame_fx, L_SUBFR, i_subfr, gain_pit, gain_code, code, voice_fac, &voice_factors[i_subfr / L_SUBFR],
718 63083 : bwe_exc_fx, gain_preQ, code_preQ, Q_new, T0, T0_frac, st_fx->coder_type, st_fx->core_brate,
719 63083 : st_fx->element_mode, st_fx->idchan, st_fx->hBWE_TD != NULL, st_fx->tdm_LRTD_flag );
720 :
721 : /*-----------------------------------------------------------------*
722 : * Synthesize speech to update mem_syn[].
723 : * Update A(z) filters
724 : *-----------------------------------------------------------------*/
725 :
726 63083 : Syn_filt_s( 1, p_Aq, M, &exc_fx[i_subfr], &syn_fx[i_subfr], L_SUBFR, hLPDmem->mem_syn, 1 );
727 :
728 63083 : p_Aw += ( M + 1 );
729 63083 : p_Aq += ( M + 1 );
730 63083 : pt_pitch++;
731 : }
732 :
733 : /* write reserved bits */
734 14086 : WHILE( unbits_PI > 0 )
735 : {
736 0 : i = s_min( unbits_PI, 16 );
737 0 : push_indice( hBstr, IND_UNUSED, 0, i );
738 0 : unbits_PI -= i;
739 : }
740 :
741 : /* write TC configuration */
742 14086 : IF( EQ_16( L_frame_fx, L_FRAME ) )
743 : {
744 7347 : IF( EQ_16( tc_subfr, TC_0_0 ) )
745 : {
746 1625 : push_indice( hBstr, IND_TC_SUBFR, 1, 1 );
747 : }
748 5722 : ELSE IF( EQ_16( tc_subfr, TC_0_64 ) )
749 : {
750 1316 : push_indice( hBstr, IND_TC_SUBFR, 0, 1 );
751 1316 : push_indice( hBstr, IND_TC_SUBFR, 1, 1 );
752 1316 : push_indice( hBstr, IND_TC_SUBFR, 0, 1 );
753 1316 : push_indice( hBstr, IND_TC_SUBFR, 1, 1 );
754 : }
755 4406 : ELSE IF( EQ_16( tc_subfr, TC_0_128 ) )
756 : {
757 678 : push_indice( hBstr, IND_TC_SUBFR, 0, 1 );
758 678 : push_indice( hBstr, IND_TC_SUBFR, 1, 1 );
759 678 : push_indice( hBstr, IND_TC_SUBFR, 0, 1 );
760 678 : push_indice( hBstr, IND_TC_SUBFR, 0, 1 );
761 : }
762 3728 : ELSE IF( EQ_16( tc_subfr, TC_0_192 ) )
763 : {
764 203 : push_indice( hBstr, IND_TC_SUBFR, 0, 1 );
765 203 : push_indice( hBstr, IND_TC_SUBFR, 1, 1 );
766 203 : push_indice( hBstr, IND_TC_SUBFR, 1, 1 );
767 : }
768 3525 : ELSE IF( EQ_16( tc_subfr, L_SUBFR ) )
769 : {
770 1175 : push_indice( hBstr, IND_TC_SUBFR, 0, 1 );
771 1175 : push_indice( hBstr, IND_TC_SUBFR, 0, 1 );
772 1175 : push_indice( hBstr, IND_TC_SUBFR, 1, 1 );
773 : }
774 2350 : ELSE IF( EQ_16( tc_subfr, 2 * L_SUBFR ) )
775 : {
776 548 : push_indice( hBstr, IND_TC_SUBFR, 0, 1 );
777 548 : push_indice( hBstr, IND_TC_SUBFR, 0, 1 );
778 548 : push_indice( hBstr, IND_TC_SUBFR, 0, 1 );
779 548 : push_indice( hBstr, IND_TC_SUBFR, 1, 1 );
780 : }
781 1802 : ELSE IF( EQ_16( tc_subfr, 3 * L_SUBFR ) )
782 : {
783 1802 : push_indice( hBstr, IND_TC_SUBFR, 0, 1 );
784 1802 : push_indice( hBstr, IND_TC_SUBFR, 0, 1 );
785 1802 : push_indice( hBstr, IND_TC_SUBFR, 0, 1 );
786 1802 : push_indice( hBstr, IND_TC_SUBFR, 0, 1 );
787 : }
788 : }
789 : ELSE /* L_frame == L_FRAME16k */
790 : {
791 6739 : IF( tc_subfr == 0 )
792 : {
793 2769 : push_indice( hBstr, IND_TC_SUBFR, 0, 2 );
794 : }
795 3970 : ELSE IF( EQ_16( tc_subfr, L_SUBFR ) )
796 : {
797 979 : push_indice( hBstr, IND_TC_SUBFR, 1, 2 );
798 : }
799 2991 : ELSE IF( EQ_16( tc_subfr, 2 * L_SUBFR ) )
800 : {
801 680 : push_indice( hBstr, IND_TC_SUBFR, 2, 2 );
802 : }
803 2311 : ELSE IF( EQ_16( tc_subfr, 3 * L_SUBFR ) )
804 : {
805 237 : push_indice( hBstr, IND_TC_SUBFR, 3, 2 );
806 237 : push_indice( hBstr, IND_TC_SUBFR, 0, 1 );
807 : }
808 2074 : ELSE IF( EQ_16( tc_subfr, 4 * L_SUBFR ) )
809 : {
810 2074 : push_indice( hBstr, IND_TC_SUBFR, 3, 2 );
811 2074 : push_indice( hBstr, IND_TC_SUBFR, 1, 1 );
812 : }
813 : }
814 :
815 14086 : IF( st_fx->Opt_SC_VBR )
816 : {
817 : /* SC-VBR */
818 0 : hSC_VBR->prev_ppp_gain_pit_fx = gain_pit; /* Q14 */
819 0 : move16();
820 0 : hSC_VBR->prev_tilt_code_fx = hLPDmem->tilt_code; /* Q15 */
821 0 : move16();
822 : }
823 :
824 14086 : return tc_subfr;
825 : }
|