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" /* Common constants */
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_enc.h" /* Function prototypes */
11 :
12 : /*======================================================================*/
13 : /* FUNCTION : encod_gen_voic_fx() */
14 : /*----------------------------------------------------------------------*/
15 : /* PURPOSE : encode generic (GC), voiced (VC) and AMR-WB IO frames */
16 : /* */
17 : /*----------------------------------------------------------------------*/
18 : /* INPUT ARGUMENTS : */
19 : /* _ (Struct) st_fx : encoder static memory */
20 : /* _ (Word16) L_frame_fx : length of the frame Q0 */
21 :
22 : /* _ (Word16[]) speech_fx : input speech Q0 */
23 : /* _ (Word16[]) Aq_fx : LP filter coefficient Q12 */
24 : /* _ (Word16[]) A_fx : unquantized A(z) filter */
25 : /* with bandwidth expansion Q12 */
26 : /* _ (Word16) coder_type : coding type */
27 : /* _ (Word16) Es_pred_fx : predicted scaled innov. energy Q8 */
28 : /* _ (Word16[]) T_op_fx : open loop pitch Q0 */
29 : /* _ (Word16[]) voicing_fx : floating pitch values for each subframe Q15*/
30 : /* _ (Word16[]) res_fx : residual signal Q_new */
31 : /* _ (Word16[]) exc_fx : adapt. excitation exc (Q0) */
32 : /* _ (Word16[]) exc2_fx : adapt. excitation/total exc (Q0) */
33 : /* _ (Word16[]) pitch_buf_fx : floating pitch values for each subframe Q6*/
34 : /* _ (Word16) shift : shift */
35 : /* _ (Word16) Q_new : */
36 : /*-----------------------------------------------------------------------*/
37 : /* OUTPUT ARGUMENTS : */
38 : /* _ (Word16[]) exc_fx : adapt. excitation exc (Q0) */
39 : /* _ (Word16[]) exc2_fx : adapt. excitation/total exc (Q0) */
40 : /* _ (Word16[]) syn_fx :core synthesis Q_new */
41 : /* _ (Word16[]) voice_factors_fx: voicing factors Q15 */
42 : /* _ (Word16[]) bwe_exc_fx : excitation for SWB TBE Q0 */
43 : /*-----------------------------------------------------------------------*/
44 :
45 :
46 : /*-----------------------------------------------------------------------*/
47 : /* RETURN ARGUMENTS : */
48 : /* _ None */
49 : /*=======================================================================*/
50 :
51 1301 : void encod_gen_voic_fx(
52 : Encoder_State *st_fx, /* i/o: state structure */
53 : const Word16 speech_fx[], /* i : input speech Q0*/
54 : const Word16 Aw_fx[], /* i : weighted A(z) unquantized for subframes Q12*/
55 : const Word16 Aq_fx[], /* i : 12k8 Lp coefficient Q12*/
56 : const Word16 Es_pred_fx, /* i : predicted scaled innov. energy Q8*/
57 : const Word16 *res_fx, /* i : residual signal Q_new*/
58 : Word16 *syn_fx, /* i/o: core synthesis Q_new*/
59 : Word16 *exc_fx, /* i/o: current non-enhanced excitation Q0*/
60 : Word16 *exc2_fx, /* i/o: current enhanced excitation Q0*/
61 : Word16 *pitch_buf_fx, /* i/o: floating pitch values for each subframe Q6*/
62 : Word16 *voice_factors_fx, /* o : voicing factors Q15*/
63 : Word16 *bwe_exc_fx, /* o : excitation for SWB TBE Q0*/
64 : Word16 *unbits_fx, /* i/o: number of unused bits Q0*/
65 : const Word16 tdm_Pitch_reuse_flag, /* i : primary channel pitch reuse flag Q0*/
66 : const Word16 tdm_Pri_pitch_buf[], /* i : primary channel pitch buffer Q6*/
67 : Word16 shift,
68 : Word16 Q_new )
69 : {
70 : Word16 xn_fx[L_SUBFR]; /* Target vector for pitch search */
71 : Word16 xn2_fx[L_SUBFR]; /* Target vector for codebook search */
72 : Word16 cn_fx[L_SUBFR]; /* Target vector in residual domain */
73 : Word16 h1_fx[L_SUBFR + ( M + 1 )]; /* Impulse response vector */
74 : Word16 h2_fx[L_SUBFR + ( M + 1 )]; /* Impulse response vector */
75 : Word16 code_fx[L_SUBFR]; /* Fixed codebook excitation */
76 1301 : Word16 y1_fx[L_SUBFR] = { 0 }; /* Filtered adaptive excitation */
77 : Word16 y2_fx[L_SUBFR]; /* Filtered algebraic excitation */
78 1301 : Word16 gain_pit_fx = 0; /* Pitch gain */
79 1301 : move16();
80 : Word16 voice_fac_fx; /* Voicing factor */
81 1301 : Word32 gain_code_fx = 0; /* Gain of code */
82 1301 : move32();
83 1301 : Word16 gain_inov_fx = 0; /* inovation gain */
84 1301 : move16();
85 : Word32 gc_mem[NB_SUBFR - 1]; /* gain_code from previous subframes */
86 : Word16 gp_mem[NB_SUBFR - 1]; /* gain_pitch from previous subframes*/
87 : Word16 i, i_subfr_fx; /* tmp variables */
88 1301 : Word16 T0_fx = 0, T0_frac_fx = 0; /* close loop integer pitch and fractional part */
89 1301 : move16();
90 1301 : move16();
91 : Word16 T0_min_fx, T0_max_fx; /* pitch variables */
92 : Word16 *pt_pitch_fx; /* pointer to floating pitch buffer */
93 : Word16 g_corr_fx[10]; /* ACELP correl, values + gain pitch */
94 : Word16 clip_gain_fx; /* LSF clip gain */
95 : const Word16 *p_Aw_fx, *p_Aq_fx; /* pointer to LP filter coeff. vector*/
96 1301 : Word16 error_fx = 0;
97 1301 : Word16 gain_preQ_fx = 0; /* Gain of prequantizer excitation */
98 1301 : move16();
99 1301 : move16();
100 : Word16 code_preQ_fx[L_SUBFR]; /* Prequantizer excitation */
101 1301 : Word16 unbits_PI_fx = 0; /* number of unused bits for PI */
102 1301 : Word32 norm_gain_code_fx = 0;
103 1301 : move16();
104 1301 : move32();
105 : Word16 pitch_limit_flag;
106 : Word16 gcode16;
107 : Word32 Ltmp;
108 : Word32 Ltmp1;
109 : Word32 Lgcode;
110 : Word16 tmp1_fx;
111 : Word16 shift_wsp;
112 : Word16 harm_flag_acelp;
113 : Word16 lp_select, lp_flag, L_frame;
114 : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
115 1301 : Flag Overflow = 0;
116 1301 : move32();
117 : #endif
118 1301 : SC_VBR_ENC_HANDLE hSC_VBR = st_fx->hSC_VBR;
119 1301 : BSTR_ENC_HANDLE hBstr = st_fx->hBstr;
120 1301 : SP_MUS_CLAS_HANDLE hSpMusClas = st_fx->hSpMusClas;
121 1301 : LPD_state_HANDLE hLPDmem = st_fx->hLPDmem;
122 :
123 : /*------------------------------------------------------------------*
124 : * Initializations
125 : *------------------------------------------------------------------*/
126 :
127 1301 : gain_pit_fx = 0;
128 1301 : move16();
129 1301 : gain_code_fx = L_deposit_l( 0 );
130 1301 : gain_preQ_fx = 0;
131 1301 : move16();
132 1301 : unbits_PI_fx = 0;
133 1301 : move16();
134 1301 : error_fx = 0;
135 1301 : move16();
136 1301 : L_frame = st_fx->L_frame;
137 1301 : move16();
138 :
139 :
140 1301 : IF( EQ_16( L_frame, L_FRAME ) )
141 : {
142 677 : T0_max_fx = PIT_MAX;
143 677 : move16();
144 677 : T0_min_fx = PIT_MIN;
145 677 : move16();
146 : }
147 : ELSE /* L_frame == L_FRAME16k */
148 : {
149 624 : T0_max_fx = PIT16k_MAX;
150 624 : move16();
151 624 : T0_min_fx = PIT16k_MIN;
152 624 : move16();
153 : }
154 1301 : lp_flag = st_fx->acelp_cfg.ltf_mode;
155 1301 : move16();
156 :
157 1301 : *unbits_fx = 0;
158 1301 : move16();
159 :
160 1301 : p_Aw_fx = Aw_fx; /* Q12 */
161 1301 : p_Aq_fx = Aq_fx; /* Q12 */
162 1301 : pt_pitch_fx = pitch_buf_fx; /* Q6 */
163 1301 : gain_preQ_fx = 0;
164 1301 : move16();
165 1301 : set16_fx( code_preQ_fx, 0, L_SUBFR );
166 :
167 1301 : shift_wsp = add( Q_new, shift );
168 :
169 : /* set and write harmonicity flag */
170 1301 : harm_flag_acelp = 0;
171 1301 : move16();
172 1301 : test();
173 1301 : test();
174 1301 : IF( GE_32( st_fx->core_brate, MIN_BRATE_AVQ_EXC ) && LE_32( st_fx->core_brate, MAX_BRATE_AVQ_EXC_TD ) && EQ_16( st_fx->coder_type, GENERIC ) )
175 : {
176 0 : if ( GT_16( st_fx->last_harm_flag_acelp, 2 ) )
177 : {
178 0 : harm_flag_acelp = 1;
179 0 : move16();
180 : }
181 :
182 0 : push_indice( hBstr, IND_HARM_FLAG_ACELP, harm_flag_acelp, 1 );
183 : }
184 :
185 : /*------------------------------------------------------------------*
186 : * ACELP subframe loop
187 : *------------------------------------------------------------------*/
188 :
189 7129 : FOR( i_subfr_fx = 0; i_subfr_fx < L_frame; i_subfr_fx += L_SUBFR )
190 : {
191 :
192 : /*----------------------------------------------------------------*
193 : * Find the the excitation search target "xn" and innovation
194 : * target in residual domain "cn"
195 : * Compute impulse response, h1[], of weighted synthesis filter
196 : *----------------------------------------------------------------*/
197 :
198 5828 : Copy( &res_fx[i_subfr_fx], &exc_fx[i_subfr_fx], L_SUBFR ); /*Q_new*/
199 :
200 5828 : find_targets_fx( speech_fx, hLPDmem->mem_syn, i_subfr_fx, &hLPDmem->mem_w0, p_Aq_fx,
201 5828 : res_fx, L_SUBFR, p_Aw_fx, st_fx->preemph_fac, xn_fx, cn_fx, h1_fx );
202 :
203 5828 : Copy_Scale_sig( h1_fx, h2_fx, L_SUBFR, -2 ); /*Q11*/
204 5828 : Scale_sig( h1_fx, L_SUBFR, add( 1, shift ) ); /* set h1[] in Q14 with scaling for convolution Q(14+shift)*/
205 :
206 : /* scaling of xn[] to limit dynamic at 12 bits */
207 5828 : Scale_sig( xn_fx, L_SUBFR, shift );
208 :
209 11656 : *pt_pitch_fx = pit_encode_fx( hBstr, st_fx->acelp_cfg.pitch_bits, st_fx->core_brate, 0, L_frame, st_fx->coder_type, &pitch_limit_flag, i_subfr_fx, exc_fx,
210 5828 : L_SUBFR, st_fx->pitch, &T0_min_fx, &T0_max_fx, &T0_fx, &T0_frac_fx, h1_fx, xn_fx, tdm_Pitch_reuse_flag, tdm_Pri_pitch_buf ); /* Q6 */
211 :
212 5828 : tbe_celp_exc( L_frame, i_subfr_fx, T0_fx, T0_frac_fx, &error_fx, bwe_exc_fx );
213 :
214 : /*-----------------------------------------------------------------*
215 : * Find adaptive exitation
216 : *-----------------------------------------------------------------*/
217 :
218 5828 : pred_lt4( &exc_fx[i_subfr_fx], &exc_fx[i_subfr_fx], T0_fx, T0_frac_fx, L_SUBFR + 1, pitch_inter4_2, L_INTERPOL2, PIT_UP_SAMP );
219 :
220 : /*-----------------------------------------------------------------*
221 : * Gain clipping test to avoid unstable synthesis on frame erasure
222 : *-----------------------------------------------------------------*/
223 :
224 5828 : clip_gain_fx = gp_clip_fx( st_fx->element_mode, st_fx->core_brate, st_fx->voicing_fx, i_subfr_fx, st_fx->coder_type, xn_fx, st_fx->clip_var_fx, sub( shift_wsp, 1 ) );
225 :
226 5828 : if ( EQ_16( st_fx->coder_type, INACTIVE ) )
227 : {
228 : /* in case of AVQ inactive, limit the gain to 0.65 */
229 0 : clip_gain_fx = 2;
230 0 : move16();
231 : }
232 :
233 : /*-----------------------------------------------------------------*
234 : * LP filtering of the adaptive excitation, codebook target computation
235 : *-----------------------------------------------------------------*/
236 :
237 5828 : lp_select = lp_filt_exc_enc_fx( MODE1, st_fx->coder_type, i_subfr_fx, exc_fx, h1_fx,
238 : xn_fx, y1_fx, xn2_fx, L_SUBFR, L_frame, g_corr_fx, clip_gain_fx, &gain_pit_fx, &lp_flag );
239 :
240 5828 : IF( EQ_16( lp_flag, NORMAL_OPERATION ) )
241 : {
242 5828 : push_indice( hBstr, IND_LP_FILT_SELECT, lp_select, 1 );
243 : }
244 :
245 : /*st_fx->lowrate_pitchGain = 0.9f * st_fx->lowrate_pitchGain + 0.1f * gain_pit_fx;*/
246 5828 : hSpMusClas->lowrate_pitchGain = round_fx_o( L_mac_o( L_mult( 29491, hSpMusClas->lowrate_pitchGain ), 6554, gain_pit_fx, &Overflow ), &Overflow ); /*Q14*Q16(0.1) + Q15 -> Q15*/
247 :
248 : /*-----------------------------------------------------------------*
249 : * Transform domain contribution encoding - active frames
250 : *-----------------------------------------------------------------*/
251 :
252 5828 : test();
253 5828 : IF( GE_32( st_fx->core_brate, MIN_BRATE_AVQ_EXC ) && NE_16( st_fx->coder_type, INACTIVE ) )
254 : {
255 3120 : transf_cdbk_enc_fx( st_fx, harm_flag_acelp, i_subfr_fx, cn_fx, exc_fx, p_Aq_fx, p_Aw_fx, h1_fx, xn_fx,
256 : xn2_fx, y1_fx, y2_fx, Es_pred_fx, &gain_pit_fx, gain_code_fx, g_corr_fx, clip_gain_fx, &gain_preQ_fx, code_preQ_fx, unbits_fx, Q_new, shift );
257 : }
258 :
259 : /*-----------------------------------------------------------------*
260 : * Innovation encoding
261 : *-----------------------------------------------------------------*/
262 :
263 5828 : inov_encode_fx( st_fx, st_fx->core_brate, 0, L_frame, st_fx->last_L_frame,
264 5828 : st_fx->coder_type, st_fx->bwidth, st_fx->sharpFlag, i_subfr_fx, -1, p_Aq_fx,
265 5828 : gain_pit_fx, cn_fx, exc_fx, h2_fx, hLPDmem->tilt_code, *pt_pitch_fx, xn2_fx, code_fx, y2_fx, &unbits_PI_fx, L_SUBFR, shift );
266 :
267 : /*-----------------------------------------------------------------*
268 : * Gain encoding
269 : *-----------------------------------------------------------------*/
270 :
271 5828 : IF( LE_32( st_fx->core_brate, ACELP_8k00 ) )
272 : {
273 0 : gain_enc_lbr_fx( hBstr, st_fx->acelp_cfg.gains_mode, st_fx->coder_type, i_subfr_fx, xn_fx, y1_fx, shift_wsp, y2_fx, code_fx,
274 : &gain_pit_fx, &gain_code_fx, &gain_inov_fx, &norm_gain_code_fx, g_corr_fx, gc_mem, gp_mem, clip_gain_fx, L_SUBFR );
275 : }
276 5828 : ELSE IF( GT_32( st_fx->core_brate, ACELP_32k ) )
277 : {
278 3120 : gain_enc_SQ_fx( hBstr, st_fx->acelp_cfg.gains_mode, i_subfr_fx, xn_fx, y1_fx, y2_fx, code_fx, Es_pred_fx,
279 : &gain_pit_fx, &gain_code_fx, &gain_inov_fx, &norm_gain_code_fx, g_corr_fx, clip_gain_fx, shift_wsp );
280 : }
281 : ELSE
282 : {
283 2708 : gain_enc_mless_fx( hBstr, st_fx->acelp_cfg.gains_mode, st_fx->element_mode, L_frame, i_subfr_fx, -1, xn_fx, y1_fx, shift_wsp, y2_fx, code_fx, Es_pred_fx,
284 : &gain_pit_fx, &gain_code_fx, &gain_inov_fx, &norm_gain_code_fx, g_corr_fx, clip_gain_fx );
285 : }
286 5828 : IF( st_fx->Opt_SC_VBR )
287 : {
288 0 : if ( EQ_16( hSC_VBR->last_ppp_mode, 1 ) )
289 : {
290 : /* SC-VBR - all other st->clip_var values will be updated even in a PPP frame */
291 0 : st_fx->clip_var_fx[1] = gain_pit_fx; /*Q14*/
292 0 : move16();
293 : }
294 : }
295 5828 : gp_clip_test_gain_pit_fx( st_fx->element_mode, st_fx->core_brate, gain_pit_fx, st_fx->clip_var_fx );
296 :
297 5828 : Lgcode = L_shl_o( gain_code_fx, Q_new, &Overflow ); /* scaled gain_code with Qnew -> Q16*/
298 5828 : gcode16 = round_fx_o( Lgcode, &Overflow ); /*Q0*/
299 :
300 5828 : hLPDmem->tilt_code = Est_tilt2( &exc_fx[i_subfr_fx], gain_pit_fx, code_fx, Lgcode, &voice_fac_fx, shift ); /* Q15 */
301 5828 : move16();
302 :
303 : /*-----------------------------------------------------------------*
304 : * Transform domain contribution encoding - inactive frames
305 : *-----------------------------------------------------------------*/
306 :
307 5828 : test();
308 5828 : IF( GE_32( st_fx->core_brate, MAX_GSC_INACTIVE_BRATE ) && EQ_16( st_fx->coder_type, INACTIVE ) )
309 : {
310 0 : transf_cdbk_enc_fx( st_fx, 0, i_subfr_fx, cn_fx, exc_fx, p_Aq_fx, p_Aw_fx, h1_fx, xn_fx, xn2_fx, y1_fx, y2_fx,
311 : Es_pred_fx, &gain_pit_fx, gain_code_fx, g_corr_fx, clip_gain_fx, &gain_preQ_fx, code_preQ_fx, unbits_fx, Q_new, shift );
312 : }
313 :
314 : /*-----------------------------------------------------------------*
315 : * Update memory of the weighting filter
316 : *-----------------------------------------------------------------*/
317 :
318 : /* st_fx->mem_w0 = xn[L_SUBFR-1] - (gain_pit*y1[L_SUBFR-1]) - (gain_code*y2[L_SUBFR-1]); */
319 5828 : Ltmp = L_mult( gcode16, y2_fx[L_SUBFR - 1] ); /*Q10*/
320 5828 : Ltmp = L_shl( Ltmp, add( 5, shift ) ); /*Q15+shift*/
321 5828 : Ltmp = L_negate( Ltmp );
322 5828 : Ltmp = L_mac( Ltmp, xn_fx[L_SUBFR - 1], 16384 /*Q14*/ ); /* Q_new-1+shift+14+1 */
323 5828 : Ltmp = L_msu( Ltmp, y1_fx[L_SUBFR - 1], gain_pit_fx /*Q14*/ ); /* Q_new-1+shift+14+1 */
324 5828 : Ltmp = L_shl_sat( Ltmp, sub( 1, shift ) ); /* 15 + Q_new */
325 5828 : hLPDmem->mem_w0 = round_fx_sat( Ltmp ); /*Q_new-1 */
326 5828 : move16();
327 5828 : IF( gain_preQ_fx != 0 )
328 : {
329 3120 : tmp1_fx = add( 16 - ( 2 + Q_AVQ_OUT_DEC + 1 ), Q_new );
330 :
331 202800 : FOR( i = 0; i < L_SUBFR; i++ )
332 : {
333 : /* Contribution from AVQ layer */
334 199680 : Ltmp1 = L_mult_o( gain_preQ_fx, code_preQ_fx[i], &Overflow ); /* Q2 + Q6 -> Q9*/
335 199680 : Ltmp1 = L_shl_o( Ltmp1, tmp1_fx, &Overflow ); /* Q16 + Q_exc */
336 :
337 : /* Compute exc2 */
338 199680 : Ltmp = L_shl_o( L_mult( gain_pit_fx, exc_fx[i + i_subfr_fx] ), 1, &Overflow ); /* Q16 */
339 199680 : exc2_fx[i + i_subfr_fx] = round_fx_o( L_add_o( Ltmp, Ltmp1, &Overflow ), &Overflow ); /* Q0 */
340 199680 : move16();
341 :
342 : /* code in Q9, gain_pit in Q14 */
343 199680 : Ltmp = L_mult( gcode16, code_fx[i] ); /* Q10 */
344 199680 : Ltmp = L_shl( Ltmp, 5 ); /* Q15 */
345 199680 : Ltmp = L_mac( Ltmp, exc_fx[i + i_subfr_fx], gain_pit_fx ); /* Q15 */
346 199680 : Ltmp = L_shl_o( Ltmp, 1, &Overflow ); /* saturation can occur here Q16*/
347 199680 : exc_fx[i + i_subfr_fx] = round_fx_o( L_add_o( Ltmp, Ltmp1, &Overflow ), &Overflow ); /* Q0 */
348 : }
349 : }
350 : ELSE
351 : {
352 : /*-----------------------------------------------------------------*
353 : * Construct adaptive part of the excitation
354 : * Save the non-enhanced excitation for FEC_exc
355 : *-----------------------------------------------------------------*/
356 176020 : FOR( i = 0; i < L_SUBFR; i++ )
357 : {
358 : /* code in Q9, gain_pit in Q14 */
359 173312 : Ltmp = L_mult( gcode16, code_fx[i] ); /* Q10 */
360 173312 : Ltmp = L_shl_o( Ltmp, 5, &Overflow ); /* Q15 */
361 173312 : Ltmp = L_mac_o( Ltmp, exc_fx[i + i_subfr_fx], gain_pit_fx, &Overflow ); /* Q15 */
362 173312 : Ltmp = L_shl_o( Ltmp, 1, &Overflow ); /* saturation can occur here Q16*/
363 173312 : exc_fx[i + i_subfr_fx] = round_fx_o( Ltmp, &Overflow ); /* Q0 */
364 : }
365 : }
366 :
367 : /*-----------------------------------------------------------------*
368 : * Prepare TBE excitation
369 : *-----------------------------------------------------------------*/
370 :
371 5828 : prep_tbe_exc_fx( L_frame, L_SUBFR, i_subfr_fx, gain_pit_fx, gain_code_fx, code_fx, voice_fac_fx,
372 5828 : &voice_factors_fx[i_subfr_fx / L_SUBFR], bwe_exc_fx, gain_preQ_fx, code_preQ_fx, Q_new,
373 5828 : T0_fx, T0_frac_fx, st_fx->coder_type, st_fx->core_brate,
374 5828 : st_fx->element_mode, st_fx->idchan, st_fx->hBWE_TD != NULL, st_fx->tdm_LRTD_flag );
375 :
376 : /*-----------------------------------------------------------------*
377 : * Synthesize speech to update mem_syn[].
378 : * Update A(z) filters
379 : *-----------------------------------------------------------------*/
380 :
381 5828 : Syn_filt_s( 1, p_Aq_fx, M, &exc_fx[i_subfr_fx], &syn_fx[i_subfr_fx], L_SUBFR, hLPDmem->mem_syn, 1 );
382 :
383 5828 : p_Aw_fx += ( M + 1 );
384 5828 : p_Aq_fx += ( M + 1 );
385 5828 : pt_pitch_fx++;
386 : }
387 :
388 : /* write reserved bits */
389 1301 : WHILE( unbits_PI_fx > 0 )
390 : {
391 0 : i = s_min( unbits_PI_fx, 16 );
392 0 : push_indice( hBstr, IND_UNUSED, 0, i );
393 0 : unbits_PI_fx -= i;
394 : }
395 1301 : IF( st_fx->Opt_SC_VBR )
396 : {
397 : /* SC-VBR */
398 0 : hSC_VBR->prev_ppp_gain_pit_fx = gain_pit_fx; /* Q14 */
399 0 : move16();
400 0 : hSC_VBR->prev_tilt_code_fx = hLPDmem->tilt_code; /* Q15 */
401 0 : move16();
402 : }
403 :
404 1301 : return;
405 : }
406 :
407 :
408 117513 : void encod_gen_voic_ivas_fx(
409 : Encoder_State *st_fx, /* i/o: state structure */
410 : const Word16 speech_fx[], /* i : input speech Qnew -1 */
411 : const Word16 Aw_fx[], /* i : weighted A(z) unquantized for subframes Q12*/
412 : const Word16 Aq_fx[], /* i : 12k8 Lp coefficient Q12*/
413 : const Word16 Es_pred_fx, /* i : predicted scaled innov. energy Q8*/
414 : const Word16 *res_fx, /* i : residual signal Q_new*/
415 : Word16 *syn_fx, /* i/o: core synthesis Q_new - 1*/
416 : Word16 *exc_fx, /* i/o: current non-enhanced excitation Q_new*/
417 : Word16 *exc2_fx, /* i/o: current enhanced excitation Q_new*/
418 : Word16 *pitch_buf_fx, /* i/o: floating pitch values for each subframe Q6*/
419 : Word16 *voice_factors_fx, /* o : voicing factors Q15*/
420 : Word16 *bwe_exc_fx, /* o : excitation for SWB TBE Q0*/
421 : Word16 *unbits_fx, /* i/o: number of unused bits Q0*/
422 : const Word16 tdm_Pitch_reuse_flag, /* i : primary channel pitch reuse flag Q0*/
423 : const Word16 tdm_Pri_pitch_buf[], /* i : primary channel pitch buffer Q6*/
424 : Word16 shift,
425 : Word16 Q_new )
426 : {
427 : Word16 xn_fx[L_SUBFR]; /* Target vector for pitch search */
428 : Word16 xn2_fx[L_SUBFR]; /* Target vector for codebook search */
429 : Word16 cn_fx[L_SUBFR]; /* Target vector in residual domain */
430 : Word16 h1_fx[L_SUBFR + ( M + 1 )]; /* Impulse response vector */
431 : Word16 h2_fx[L_SUBFR + ( M + 1 )]; /* Impulse response vector */
432 : Word16 code_fx[L_SUBFR]; /* Fixed codebook excitation */
433 117513 : Word16 y1_fx[L_SUBFR] = { 0 }; /* Filtered adaptive excitation */
434 : Word16 y2_fx[L_SUBFR]; /* Filtered algebraic excitation */
435 117513 : Word16 gain_pit_fx = 0; /* Pitch gain */
436 117513 : move16();
437 : Word16 voice_fac_fx; /* Voicing factor */
438 117513 : Word32 gain_code_fx = 0; /* Gain of code */
439 117513 : Word16 gain_inov_fx = 0; /* inovation gain */
440 117513 : move32();
441 117513 : move16();
442 : Word32 gc_mem[NB_SUBFR - 1]; /* gain_code from previous subframes */
443 : Word16 gp_mem[NB_SUBFR - 1]; /* gain_pitch from previous subframes*/
444 : Word16 i, i_subfr_fx; /* tmp variables */
445 117513 : Word16 T0_fx = 0, T0_frac_fx = 0; /* close loop integer pitch and fractional part */
446 117513 : move16();
447 117513 : move16();
448 : Word16 T0_min_fx, T0_max_fx; /* pitch variables */
449 : Word16 *pt_pitch_fx; /* pointer to floating pitch buffer */
450 : Word16 g_corr_fx[10]; /* ACELP correl, values + gain pitch */
451 : Word16 clip_gain_fx; /* LSF clip gain */
452 : const Word16 *p_Aw_fx, *p_Aq_fx; /* pointer to LP filter coeff. vector*/
453 117513 : Word16 error_fx = 0;
454 117513 : Word16 gain_preQ_fx = 0; /* Gain of prequantizer excitation */
455 117513 : move16();
456 117513 : move16();
457 : Word16 code_preQ_fx[L_SUBFR]; /* Prequantizer excitation */
458 117513 : Word16 unbits_PI_fx = 0; /* number of unused bits for PI */
459 117513 : Word32 norm_gain_code_fx = 0;
460 117513 : move16();
461 117513 : move32();
462 : Word16 pitch_limit_flag;
463 : Word16 gcode16;
464 : Word32 Ltmp;
465 : Word32 Ltmp1;
466 : Word32 Lgcode;
467 : Word16 tmp1_fx;
468 : Word16 shift_wsp;
469 : Word16 harm_flag_acelp;
470 : Word16 lp_select, lp_flag, L_frame;
471 : Word16 q_h1;
472 : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
473 117513 : Flag Overflow = 0;
474 117513 : move32();
475 : #endif
476 117513 : SC_VBR_ENC_HANDLE hSC_VBR = st_fx->hSC_VBR;
477 117513 : BSTR_ENC_HANDLE hBstr = st_fx->hBstr;
478 117513 : LPD_state_HANDLE hLPDmem = st_fx->hLPDmem;
479 :
480 : /*------------------------------------------------------------------*
481 : * Initializations
482 : *------------------------------------------------------------------*/
483 :
484 117513 : gain_pit_fx = 0;
485 117513 : move16();
486 117513 : gain_code_fx = L_deposit_l( 0 );
487 117513 : gain_preQ_fx = 0;
488 117513 : move16();
489 117513 : unbits_PI_fx = 0;
490 117513 : move16();
491 117513 : error_fx = 0;
492 117513 : move16();
493 117513 : L_frame = st_fx->L_frame;
494 117513 : move16();
495 :
496 :
497 117513 : IF( EQ_16( L_frame, L_FRAME ) )
498 : {
499 53669 : T0_max_fx = PIT_MAX;
500 53669 : move16();
501 53669 : T0_min_fx = PIT_MIN;
502 53669 : move16();
503 : }
504 : ELSE /* L_frame == L_FRAME16k */
505 : {
506 63844 : T0_max_fx = PIT16k_MAX;
507 63844 : move16();
508 63844 : T0_min_fx = PIT16k_MIN;
509 63844 : move16();
510 : }
511 117513 : lp_flag = st_fx->acelp_cfg.ltf_mode;
512 117513 : move16();
513 :
514 117513 : *unbits_fx = 0;
515 117513 : move16();
516 :
517 117513 : p_Aw_fx = Aw_fx; /*Q12*/
518 117513 : p_Aq_fx = Aq_fx; /*Q12*/
519 117513 : pt_pitch_fx = pitch_buf_fx; /*Q6*/
520 117513 : gain_preQ_fx = 0;
521 117513 : move16();
522 117513 : set16_fx( code_preQ_fx, 0, L_SUBFR );
523 :
524 117513 : shift_wsp = add( Q_new, shift );
525 117513 : if ( LT_32( st_fx->core_brate, MIN_BRATE_AVQ_EXC ) )
526 : {
527 104991 : shift_wsp = sub( shift_wsp, 1 );
528 : }
529 : /* set and write harmonicity flag */
530 117513 : harm_flag_acelp = 0;
531 117513 : move16();
532 117513 : test();
533 117513 : test();
534 117513 : IF( GE_32( st_fx->core_brate, MIN_BRATE_AVQ_EXC ) && LE_32( st_fx->core_brate, MAX_BRATE_AVQ_EXC_TD ) && EQ_16( st_fx->coder_type, GENERIC ) )
535 : {
536 8018 : if ( GT_16( st_fx->last_harm_flag_acelp, 2 ) )
537 : {
538 721 : harm_flag_acelp = 1;
539 721 : move16();
540 : }
541 :
542 8018 : push_indice( hBstr, IND_HARM_FLAG_ACELP, harm_flag_acelp, 1 );
543 : }
544 :
545 : /*------------------------------------------------------------------*
546 : * ACELP subframe loop
547 : *------------------------------------------------------------------*/
548 :
549 651409 : FOR( i_subfr_fx = 0; i_subfr_fx < L_frame; i_subfr_fx += L_SUBFR )
550 : {
551 :
552 : /*----------------------------------------------------------------*
553 : * Find the the excitation search target "xn" and innovation
554 : * target in residual domain "cn"
555 : * Compute impulse response, h1[], of weighted synthesis filter
556 : *----------------------------------------------------------------*/
557 :
558 533896 : Copy( &res_fx[i_subfr_fx], &exc_fx[i_subfr_fx], L_SUBFR ); /*Q_new*/
559 :
560 533896 : find_targets_ivas_fx( speech_fx, hLPDmem->mem_syn, i_subfr_fx, &hLPDmem->mem_w0, p_Aq_fx,
561 533896 : res_fx, L_SUBFR, p_Aw_fx, st_fx->preemph_fac, xn_fx, cn_fx, h1_fx );
562 :
563 533896 : q_h1 = sub( 14, norm_s( h1_fx[0] ) );
564 533896 : Copy_Scale_sig( h1_fx, h2_fx, L_SUBFR, sub( 11, q_h1 ) ); /*Q11*/
565 533896 : Scale_sig( h1_fx, L_SUBFR, sub( 14, q_h1 ) ); /* set h1[] in Q14 with scaling for convolution Q14*/
566 : /* scaling of xn[] to limit dynamic at 12 bits */
567 533896 : Scale_sig( xn_fx, L_SUBFR, shift );
568 :
569 1067792 : *pt_pitch_fx = pit_encode_ivas_fx( hBstr, st_fx->acelp_cfg.pitch_bits, st_fx->core_brate, 0, L_frame, st_fx->coder_type, &pitch_limit_flag, i_subfr_fx, exc_fx,
570 533896 : L_SUBFR, st_fx->pitch, &T0_min_fx, &T0_max_fx, &T0_fx, &T0_frac_fx, h1_fx, xn_fx, tdm_Pitch_reuse_flag, tdm_Pri_pitch_buf, Q_new ); /* Q6 */
571 533896 : move16();
572 :
573 533896 : tbe_celp_exc_ivas( st_fx->element_mode, st_fx->idchan, L_frame, L_SUBFR, i_subfr_fx, T0_fx, T0_frac_fx, &error_fx, bwe_exc_fx, st_fx->tdm_LRTD_flag );
574 :
575 : /*-----------------------------------------------------------------*
576 : * Find adaptive exitation
577 : *-----------------------------------------------------------------*/
578 :
579 533896 : pred_lt4_ivas_fx( &exc_fx[i_subfr_fx], &exc_fx[i_subfr_fx], T0_fx, T0_frac_fx, L_SUBFR + 1, L_pitch_inter4_2, L_INTERPOL2, PIT_UP_SAMP );
580 :
581 : /*-----------------------------------------------------------------*
582 : * Gain clipping test to avoid unstable synthesis on frame erasure
583 : *-----------------------------------------------------------------*/
584 :
585 533896 : clip_gain_fx = gp_clip_fx( st_fx->element_mode, st_fx->core_brate, st_fx->voicing_fx, i_subfr_fx, st_fx->coder_type, xn_fx, st_fx->clip_var_fx, sub( shift_wsp, 1 ) );
586 :
587 533896 : if ( EQ_16( st_fx->coder_type, INACTIVE ) )
588 : {
589 : /* in case of AVQ inactive, limit the gain to 0.65 */
590 13030 : clip_gain_fx = 2;
591 13030 : move16();
592 : }
593 :
594 : /*-----------------------------------------------------------------*
595 : * LP filtering of the adaptive excitation, codebook target computation
596 : *-----------------------------------------------------------------*/
597 :
598 533896 : lp_select = lp_filt_exc_enc_ivas_fx( MODE1, st_fx->coder_type, i_subfr_fx, exc_fx, h1_fx,
599 : xn_fx, y1_fx, xn2_fx, L_SUBFR, L_frame, g_corr_fx, clip_gain_fx, &gain_pit_fx, &lp_flag );
600 :
601 533896 : IF( EQ_16( lp_flag, NORMAL_OPERATION ) )
602 : {
603 465128 : push_indice( hBstr, IND_LP_FILT_SELECT, lp_select, 1 );
604 : }
605 :
606 : /*-----------------------------------------------------------------*
607 : * Transform domain contribution encoding - active frames
608 : *-----------------------------------------------------------------*/
609 :
610 533896 : test();
611 533896 : IF( GE_32( st_fx->core_brate, MIN_BRATE_AVQ_EXC ) && NE_16( st_fx->coder_type, INACTIVE ) )
612 : {
613 51355 : transf_cdbk_enc_ivas_fx( st_fx, harm_flag_acelp, i_subfr_fx, cn_fx, exc_fx, p_Aq_fx, p_Aw_fx, h1_fx, xn_fx,
614 : xn2_fx, y1_fx, y2_fx, Es_pred_fx, &gain_pit_fx, gain_code_fx, g_corr_fx, clip_gain_fx, &gain_preQ_fx, code_preQ_fx, unbits_fx, Q_new, shift );
615 : }
616 :
617 : /*-----------------------------------------------------------------*
618 : * Innovation encoding
619 : *-----------------------------------------------------------------*/
620 :
621 533896 : inov_encode_ivas_fx( st_fx, st_fx->core_brate, 0, L_frame, st_fx->last_L_frame,
622 533896 : st_fx->coder_type, st_fx->bwidth, st_fx->sharpFlag, i_subfr_fx, -1, p_Aq_fx,
623 533896 : gain_pit_fx, cn_fx, exc_fx, h2_fx, hLPDmem->tilt_code, *pt_pitch_fx, xn2_fx, code_fx, y2_fx, &unbits_PI_fx, L_SUBFR, shift, Q_new );
624 :
625 : /*-----------------------------------------------------------------*
626 : * Gain encoding
627 : *-----------------------------------------------------------------*/
628 :
629 533896 : IF( LE_32( st_fx->core_brate, ACELP_8k00 ) )
630 : {
631 13552 : gain_enc_lbr_ivas_fx( hBstr, st_fx->acelp_cfg.gains_mode, st_fx->coder_type, i_subfr_fx, xn_fx, y1_fx, shift_wsp, y2_fx, code_fx,
632 : &gain_pit_fx, &gain_code_fx, &gain_inov_fx, &norm_gain_code_fx, g_corr_fx, gc_mem, gp_mem, clip_gain_fx, L_SUBFR );
633 : }
634 520344 : ELSE IF( GT_32( st_fx->core_brate, ACELP_32k ) )
635 : {
636 40485 : gain_enc_SQ_ivas_fx( hBstr, st_fx->acelp_cfg.gains_mode, i_subfr_fx, xn_fx, y1_fx, y2_fx, code_fx, Es_pred_fx,
637 : &gain_pit_fx, &gain_code_fx, &gain_inov_fx, &norm_gain_code_fx, g_corr_fx, clip_gain_fx, shift_wsp );
638 : }
639 : ELSE
640 : {
641 479859 : gain_enc_mless_ivas_fx( hBstr, st_fx->acelp_cfg.gains_mode, st_fx->element_mode, L_frame, i_subfr_fx, -1, xn_fx, y1_fx, shift_wsp, y2_fx, code_fx, Es_pred_fx,
642 : &gain_pit_fx, &gain_code_fx, &gain_inov_fx, &norm_gain_code_fx, g_corr_fx, clip_gain_fx );
643 : }
644 533896 : IF( st_fx->Opt_SC_VBR )
645 : {
646 0 : if ( EQ_16( hSC_VBR->last_ppp_mode, 1 ) )
647 : {
648 : /* SC-VBR - all other st->clip_var values will be updated even in a PPP frame */
649 0 : st_fx->clip_var_fx[1] = gain_pit_fx; /* Q14 */
650 0 : move16();
651 : }
652 : }
653 533896 : gp_clip_test_gain_pit_fx( st_fx->element_mode, st_fx->core_brate, gain_pit_fx, st_fx->clip_var_fx );
654 :
655 533896 : Lgcode = L_shl_o( gain_code_fx, Q_new, &Overflow ); /* scaled gain_code with Qnew -> Q16*/
656 533896 : gcode16 = round_fx_o( Lgcode, &Overflow );
657 :
658 533896 : hLPDmem->tilt_code = est_tilt_ivas_fx( &exc_fx[i_subfr_fx], gain_pit_fx, code_fx, gain_code_fx, &voice_fac_fx, Q_new, L_SUBFR, 0 ); /* Q15 */
659 533896 : move16();
660 :
661 : /*-----------------------------------------------------------------*
662 : * Transform domain contribution encoding - inactive frames
663 : *-----------------------------------------------------------------*/
664 :
665 533896 : test();
666 533896 : IF( !st_fx->inactive_coder_type_flag && EQ_16( st_fx->coder_type, INACTIVE ) )
667 : {
668 13030 : transf_cdbk_enc_ivas_fx( st_fx, 0, i_subfr_fx, cn_fx, exc_fx, p_Aq_fx, p_Aw_fx, h1_fx, xn_fx, xn2_fx, y1_fx, y2_fx,
669 : Es_pred_fx, &gain_pit_fx, gain_code_fx, g_corr_fx, clip_gain_fx, &gain_preQ_fx, code_preQ_fx, unbits_fx, Q_new, shift );
670 : }
671 :
672 : /*-----------------------------------------------------------------*
673 : * Update memory of the weighting filter
674 : *-----------------------------------------------------------------*/
675 :
676 :
677 : /*At this point
678 : xn has to be in Qnew
679 : yn has to be in Qnew
680 : y2_fx has to be in Q9
681 : gcode16 in Q_new
682 : gain_pit_fx in Q14
683 : */
684 533896 : Scale_sig( xn_fx, L_SUBFR, 1 );
685 533896 : Scale_sig( y1_fx, L_SUBFR, 1 );
686 : /* st_fx->mem_w0 = xn[L_SUBFR-1] - (gain_pit*y1[L_SUBFR-1]) - (gain_code*y2[L_SUBFR-1]); */
687 533896 : Ltmp = L_mult0( gcode16, y2_fx[L_SUBFR - 1] );
688 533896 : Ltmp = L_shl( Ltmp, add( 5, shift ) );
689 533896 : Ltmp = L_negate( Ltmp );
690 533896 : Word32 Ltmp2 = L_mult0( xn_fx[L_SUBFR - 1], 16384 );
691 533896 : Ltmp = L_add( Ltmp, Ltmp2 );
692 533896 : Word32 Ltmp3 = L_mult0( y1_fx[L_SUBFR - 1], gain_pit_fx );
693 533896 : Ltmp = L_sub( Ltmp, Ltmp3 );
694 :
695 : /*Ltmp is in Q14 + Qnew here
696 : We need memWo in Qnew -1 */
697 :
698 533896 : Ltmp = L_shl( Ltmp, sub( 1, shift ) ); // Q14 + Qnew + 1
699 533896 : hLPDmem->mem_w0 = round_fx_sat( Ltmp ); /*Q_new-1 */
700 533896 : move16();
701 533896 : IF( gain_preQ_fx != 0 )
702 : {
703 64371 : tmp1_fx = add( 16 - ( 2 + Q_AVQ_OUT_DEC + 1 ), Q_new );
704 :
705 4184115 : FOR( i = 0; i < L_SUBFR; i++ )
706 : {
707 : /* Contribution from AVQ layer */
708 4119744 : Ltmp1 = L_mult_o( gain_preQ_fx, code_preQ_fx[i], &Overflow );
709 4119744 : Ltmp1 = L_shl_o( Ltmp1, tmp1_fx, &Overflow );
710 :
711 : /* Compute exc2 */
712 4119744 : Ltmp = L_shl_o( L_mult( gain_pit_fx, exc_fx[i + i_subfr_fx] ), 1, &Overflow );
713 4119744 : exc2_fx[i + i_subfr_fx] = round_fx_o( L_add_o( Ltmp, Ltmp1, &Overflow ), &Overflow );
714 4119744 : move16();
715 :
716 : /* code in Q9, gain_pit in Q14 */
717 4119744 : Ltmp = L_mult( gcode16, code_fx[i] ); /*Qnew + 9 + 1 */
718 4119744 : Ltmp = L_shl( Ltmp, 5 ); /*Qnew + 9+ 1+5 */
719 4119744 : Ltmp = L_mac( Ltmp, exc_fx[i + i_subfr_fx], gain_pit_fx ); /*Qnew + 14 + 1*/
720 4119744 : Ltmp = L_shl_o( Ltmp, 1, &Overflow ); /*Qnew + 14 + 1 +1 */ /* saturation can occur here Q16*/
721 4119744 : exc_fx[i + i_subfr_fx] = round_fx_o( L_add_o( Ltmp, Ltmp1, &Overflow ), &Overflow );
722 4119744 : move16();
723 : }
724 : }
725 : ELSE
726 : {
727 : /*-----------------------------------------------------------------*
728 : * Construct adaptive part of the excitation
729 : * Save the non-enhanced excitation for FEC_exc
730 : *-----------------------------------------------------------------*/
731 30519125 : FOR( i = 0; i < L_SUBFR; i++ )
732 : {
733 : /* code in Q9, gain_pit in Q14 */
734 : /*gcode16 in Qnew*/
735 30049600 : Ltmp = L_mult( gcode16, code_fx[i] ); /*Qnew + 9 + 1 */
736 30049600 : Ltmp = L_shl_o( Ltmp, 5, &Overflow ); /*Qnew + 9+ 1+5 */
737 30049600 : Ltmp = L_mac_o( Ltmp, exc_fx[i + i_subfr_fx], gain_pit_fx, &Overflow ); /*Qnew + 14 + 1*/
738 30049600 : Ltmp = L_shl_o( Ltmp, 1, &Overflow ); /*Qnew + 14 + 1 +1 */
739 30049600 : exc_fx[i + i_subfr_fx] = round_fx_o( Ltmp, &Overflow );
740 : }
741 : }
742 : /*-----------------------------------------------------------------*
743 : * Prepare TBE excitation
744 : *-----------------------------------------------------------------*/
745 :
746 533896 : prep_tbe_exc_fx( L_frame, L_SUBFR, i_subfr_fx, gain_pit_fx, gain_code_fx, code_fx, voice_fac_fx,
747 533896 : &voice_factors_fx[i_subfr_fx / L_SUBFR], bwe_exc_fx, gain_preQ_fx, code_preQ_fx, Q_new,
748 533896 : T0_fx, T0_frac_fx, st_fx->coder_type, st_fx->core_brate,
749 533896 : st_fx->element_mode, st_fx->idchan, st_fx->hBWE_TD != NULL, st_fx->tdm_LRTD_flag );
750 :
751 : /*-----------------------------------------------------------------*
752 : * Synthesize speech to update mem_syn[].
753 : * Update A(z) filters
754 : *-----------------------------------------------------------------*/
755 :
756 533896 : syn_filt_fx( 1, p_Aq_fx, M, &exc_fx[i_subfr_fx], &syn_fx[i_subfr_fx], L_SUBFR, hLPDmem->mem_syn, 1 );
757 :
758 533896 : p_Aw_fx += ( M + 1 );
759 533896 : p_Aq_fx += ( M + 1 );
760 533896 : pt_pitch_fx++;
761 : }
762 :
763 : /* write reserved bits */
764 117513 : WHILE( unbits_PI_fx > 0 )
765 : {
766 0 : i = s_min( unbits_PI_fx, 16 );
767 0 : push_indice( hBstr, IND_UNUSED, 0, i );
768 0 : unbits_PI_fx -= i;
769 : }
770 117513 : IF( st_fx->Opt_SC_VBR )
771 : {
772 : /* SC-VBR */
773 0 : hSC_VBR->prev_ppp_gain_pit_fx = gain_pit_fx;
774 0 : move16();
775 0 : hSC_VBR->prev_tilt_code_fx = hLPDmem->tilt_code;
776 0 : move16();
777 : }
778 117513 : return;
779 : }
|