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.h" /* Common constants */
9 : #include "prot_fx.h" /* Function prototypes */
10 : #include "prot_fx_enc.h" /* Function prototypes */
11 : #include "basop_util.h" /* Function prototypes */
12 :
13 : /*Temporary location to be move in prot* when merge is done */
14 : void E_LPC_f_lsp_a_conversion( const Word16 *lsp, Word16 *a, const Word16 m );
15 :
16 : /*=======================================================================================*/
17 : /* FUNCTION : encod_ppp_fx() */
18 : /*---------------------------------------------------------------------------------------*/
19 : /* PURPOSE : */
20 : /*---------------------------------------------------------------------------------------*/
21 : /* INPUT ARGUMENTS : */
22 : /* _ (Word16) speech_fx[], i : input speech Q_new */
23 : /* _ (Word16) Aq_fx[], i : 12k8 Lp coefficient Q12 */
24 : /* _ (Word16) A_fx[], i : unquantized A(z) filter with bandwidth expansion Q12*/
25 : /* _ (Word16) coder_type, i : coding type */
26 : /* _ (Word16) T_op_fx[], i : open loop pitch */
27 : /* _ (Word16) voicing_fx[], i : voicing Q15 */
28 : /* _ (Word16) *res_fx, i : residual signal Q_new */
29 : /* _ (Word16) Q_new i : Q factor for res */
30 : /* _ (Word16) vadsnr_fx i : SNR for current frame Q7 */
31 : /*---------------------------------------------------------------------------------------*/
32 : /* OUTPUT ARGUMENTS : */
33 : /* _ (Word16) *exc2_fx, o : current enhanced excitation Q0 */
34 : /* _ (Word16) *pitch_buf_fx, o : floating pitch values for each subframe Q6 */
35 : /* _ (Word16) *synth_fx, o : core synthesis Q-1 */
36 : /* _ Encoder_State *st_fx: */
37 : /* _ lastLgainE_fx - Q11 */
38 : /* _ lastHgainE_fx - Q11 */
39 : /* _ lasterbE_fx - Q13 */
40 : /*---------------------------------------------------------------------------------------*/
41 : /* INPUT/OUTPUT ARGUMENTS : */
42 : /* _ Encoder_State *st_fx: */
43 : /* _ st_fx->dtfs_enc_xxxx */
44 : /* _ a nd b in st_fx->dtfs_enc_Q */
45 : /* rest all in Q0 */
46 : /* - bump_up_fx - Q0 */
47 : /* _ (Word16) *exc_fx, o : current enhanced excitation Q0 */
48 : /*---------------------------------------------------------------------------------------*/
49 : /* RETURN ARGUMENTS : */
50 : /* _ None. */
51 : /*---------------------------------------------------------------------------------------*/
52 : /* CALLED FROM : TX */
53 : /*=======================================================================================*/
54 0 : ivas_error encod_ppp_fx(
55 : Encoder_State *st_fx, /* i/o: state structure */
56 : const Word16 speech_fx[], /* i : input speech Q_new*/
57 : const Word16 Aw_fx[], /* i : weighted A(z) unquantized for subframes Q12*/
58 : const Word16 Aq_fx[], /* i : 12k8 Lp coefficient Q14*/
59 : Word16 *res_fx, /* i : residual signal Q_new*/
60 : Word16 *synth_fx, /* o : core synthesis Q-1*/
61 : Word16 *exc_fx, /* i/o: current non-enhanced excitation Q_new*/
62 : Word16 *exc2_fx, /* o : current enhanced excitation Q0*/
63 : Word16 *pitch_buf_fx, /* o : floating pitch values for each subframe Q6*/
64 : Word16 *voice_factors, /* o : voicing factors Q15*/
65 : Word16 *bwe_exc, /* o : excitation for SWB TBE Q_new*/
66 : Word16 Q_new,
67 : Word16 shift )
68 : {
69 : Word16 xn_fx[L_SUBFR]; /* Target vector for pitch search */
70 : Word16 h1_fx[L_SUBFR + ( M + 1 )]; /* Impulse response vector */
71 : Word16 i_subfr; /* tmp variables */
72 : const Word16 *p_Aw_fx, *p_Aq_fx; /* pointer to LP filter coeff. vector*/
73 :
74 : Word16 k;
75 : Word16 p_Aq_old_fx[M + 1], excQ_ppp_fx[L_FRAME], p_Aq_curr_fx[M], pitch_fx[NB_SUBFR];
76 : Word16 LPC_de_old_fx[M + 1], LPC_de_curr_fx[M + 1];
77 0 : Word16 shift_wsp = add( Q_new, shift );
78 : Word16 rate_ctrl_fx;
79 0 : Word16 saved_Q_new = Q_new;
80 0 : move16();
81 0 : LPD_state_HANDLE hLPDmem = st_fx->hLPDmem;
82 0 : SC_VBR_ENC_HANDLE hSC_VBR = st_fx->hSC_VBR;
83 0 : BSTR_ENC_HANDLE hBstr = st_fx->hBstr;
84 : ivas_error error;
85 :
86 0 : error = IVAS_ERR_OK;
87 0 : move16();
88 0 : rate_ctrl_fx = hSC_VBR->rate_control;
89 0 : move16();
90 :
91 : /*------------------------------------------------------------------*
92 : * ACELP subframe loop
93 : *------------------------------------------------------------------*/
94 0 : p_Aw_fx = Aw_fx;
95 0 : p_Aq_fx = Aq_fx;
96 0 : FOR( i_subfr = 0; i_subfr < L_FRAME; i_subfr += L_SUBFR )
97 : {
98 : /*----------------------------------------------------------------*
99 : * Bandwidth expansion of A(z) filter coefficients
100 : * Find the the excitation search target "xn" and innovation
101 : * target in residual domain "cn"
102 : * Compute impulse response, h1[], of weighted synthesis filter
103 : *----------------------------------------------------------------*/
104 :
105 0 : Copy( &res_fx[i_subfr], &exc_fx[i_subfr], L_SUBFR ); /* Q_new */
106 :
107 0 : find_targets_fx( speech_fx, hLPDmem->mem_syn, i_subfr, &hLPDmem->mem_w0, p_Aq_fx,
108 : res_fx, L_SUBFR, p_Aw_fx, TILT_FAC_FX, xn_fx, NULL, h1_fx );
109 :
110 : /* scale xn[] and h1[] to avoid overflow in dot_product12() */
111 0 : Scale_sig( xn_fx, L_SUBFR, shift ); /* scaling of xn[] to limit dynamic at 12 bits */
112 :
113 : /* call voiced encoder at this point */
114 0 : IF( i_subfr == 0 ) /* generate the L_FRAME exc */
115 : {
116 0 : FOR( k = 0; k < M; k++ )
117 : {
118 0 : p_Aq_curr_fx[k] = p_Aq_fx[k + ( 3 * ( M + 1 ) ) + 1];
119 0 : move16();
120 : }
121 :
122 0 : E_LPC_f_lsp_a_conversion( st_fx->lsp_old_fx, p_Aq_old_fx, M );
123 0 : deemph_lpc_fx( p_Aq_curr_fx, p_Aq_old_fx, LPC_de_curr_fx, LPC_de_old_fx, 1 );
124 : /* both outputs LPC_de_curr_fx and LPC_de_old_fx are in Q12 */
125 :
126 :
127 : /* last frame-end lpc and curr frame-end lpc */
128 0 : IF( ( error = ppp_voiced_encoder_fx( hBstr, hSC_VBR, st_fx->bwidth, st_fx->last_coder_type_raw, st_fx->old_pitch_buf_fx, res_fx,
129 : excQ_ppp_fx, st_fx->pitch[1], LPC_de_old_fx, LPC_de_curr_fx, exc_fx, pitch_fx, Q_new ) ) != IVAS_ERR_OK )
130 : {
131 0 : return error;
132 : }
133 :
134 0 : Scale_sig( exc_fx, L_FRAME, ( saved_Q_new - Q_new ) ); /* saved_Q_new */
135 0 : if ( EQ_16( hSC_VBR->bump_up, 1 ) )
136 : {
137 0 : i_subfr = L_FRAME;
138 0 : move16();
139 : }
140 : }
141 :
142 0 : IF( NE_16( hSC_VBR->bump_up, 1 ) )
143 : {
144 : /*-----------------------------------------------------------------*
145 : * Gain clipping test to avoid unstable synthesis on frame erasure
146 : * or in case of floating point encoder & fixed p. decoder
147 : *-----------------------------------------------------------------*/
148 0 : gp_clip_fx( st_fx->element_mode, st_fx->core_brate, st_fx->voicing_fx, i_subfr, st_fx->coder_type, xn_fx, st_fx->clip_var_fx, sub( shift_wsp, 1 ) );
149 :
150 :
151 : /* run the above to maintain gain clipping memories */
152 0 : gp_clip_test_gain_pit_fx( st_fx->element_mode, st_fx->core_brate, hSC_VBR->prev_ppp_gain_pit_fx, st_fx->clip_var_fx );
153 :
154 :
155 : /*-----------------------------------------------------------------*
156 : * Synthesize speech to update mem_syn[].
157 : * Update A(z) filters
158 : *-----------------------------------------------------------------*/
159 :
160 0 : Syn_filt_s( 1, p_Aq_fx, M, &excQ_ppp_fx[i_subfr], &synth_fx[i_subfr], L_SUBFR, hLPDmem->mem_syn, 1 );
161 :
162 :
163 0 : p_Aw_fx += ( M + 1 );
164 0 : p_Aq_fx += ( M + 1 );
165 : }
166 :
167 : } /* end of subframe loop */
168 :
169 0 : IF( hSC_VBR->bump_up )
170 : {
171 : /* PPP failed, bump up */
172 0 : hSC_VBR->ppp_mode = 0;
173 0 : move16();
174 0 : st_fx->core_brate = ACELP_7k20;
175 0 : move16();
176 0 : hSC_VBR->pppcountE = 0;
177 0 : move16();
178 :
179 0 : IF( hSC_VBR->set_ppp_generic )
180 : {
181 0 : st_fx->coder_type = GENERIC;
182 0 : move16();
183 : }
184 : ELSE
185 : {
186 0 : st_fx->coder_type = VOICED;
187 0 : move16();
188 : }
189 :
190 : /* We write signalling indices again only in case of bump_up */
191 : /* delete previous indices */
192 : #ifdef HARM_PUSH_BIT
193 0 : reset_indices_enc_fx( hBstr, hBstr->nb_ind_tot );
194 : #else
195 : reset_indices_enc_fx( hBstr, MAX_NUM_INDICES );
196 : #endif
197 : /* signalling matrix (writing of signalling bits) */
198 0 : signalling_enc_fx( st_fx );
199 : }
200 : ELSE
201 : {
202 0 : Copy( excQ_ppp_fx, exc_fx, L_FRAME ); /* Q_new */
203 :
204 : /*-----------------------------------------------------------------*
205 : * Updates: last value of new target is stored in mem_w0
206 : *-----------------------------------------------------------------*/
207 :
208 0 : hLPDmem->mem_w0 = sub( shr( xn_fx[L_SUBFR - 1], shift ), shr( exc_fx[L_FRAME - 1], 1 ) ); /*Q_new-1 */
209 0 : move16();
210 :
211 0 : Copy( exc_fx, exc2_fx, L_FRAME ); /* Q_new */
212 :
213 0 : hLPDmem->dm_fx.prev_state = 2;
214 0 : move16(); /*Q0 dispMem index 0 */
215 0 : hLPDmem->dm_fx.prev_gain_pit[0] = hSC_VBR->prev_ppp_gain_pit_fx; /* Q14 */
216 0 : move16(); /*Q14 dispMem index 2 */
217 :
218 0 : FOR( k = 1; k < 5; k++ )
219 : {
220 0 : hLPDmem->dm_fx.prev_gain_pit[k] = hLPDmem->dm_fx.prev_gain_pit[k - 1]; /* Q14 */
221 0 : move16();
222 : }
223 :
224 0 : hLPDmem->tilt_code = hSC_VBR->prev_tilt_code_fx; /* Q15 */
225 0 : move16();
226 0 : Copy( pitch_fx, pitch_buf_fx, NB_SUBFR ); /* Q6 */
227 0 : pitch_buf_fx[NB_SUBFR16k - 1] = pitch_fx[NB_SUBFR - 1]; /* Q6 */
228 :
229 0 : interp_code_5over2_fx( exc2_fx, bwe_exc, L_FRAME );
230 0 : set16_fx( voice_factors, 0, NB_SUBFR16k );
231 : }
232 :
233 0 : hSC_VBR->rate_control = rate_ctrl_fx; /* Q0 */
234 0 : move16();
235 :
236 0 : set16_fx( hSC_VBR->nelp_lp_fit_mem, 0, NELP_LP_ORDER * 2 );
237 :
238 0 : return error;
239 : }
240 :
241 0 : ivas_error encod_ppp_ivas_fx(
242 : Encoder_State *st_fx, /* i/o: state structure */
243 : const Word16 speech_fx[], /* i : input speech Q_new*/
244 : const Word16 Aw_fx[], /* i : weighted A(z) unquantized for subframes Q12*/
245 : const Word16 Aq_fx[], /* i : 12k8 Lp coefficient Q12*/
246 : Word16 *res_fx, /* i : residual signal Q_new*/
247 : Word16 *synth_fx, /* o : core synthesis Q-1*/
248 : Word16 *exc_fx, /* i/o: current non-enhanced excitation Q_new*/
249 : Word16 *exc2_fx, /* o : current enhanced excitation Q0*/
250 : Word16 *pitch_buf_fx, /* o : floating pitch values for each subframe Q6*/
251 : Word16 *voice_factors, /* o : voicing factors Q15*/
252 : Word16 *bwe_exc, /* o : excitation for SWB TBE Q_new*/
253 : Word16 Q_new,
254 : Word16 shift )
255 : {
256 : Word16 xn_fx[L_SUBFR]; /* Target vector for pitch search */
257 : Word16 h1_fx[L_SUBFR + ( M + 1 )]; /* Impulse response vector */
258 : Word16 i_subfr; /* tmp variables */
259 : const Word16 *p_Aw_fx, *p_Aq_fx; /* pointer to LP filter coeff. vector*/
260 :
261 : Word16 k;
262 : Word16 p_Aq_old_fx[M + 1], excQ_ppp_fx[L_FRAME], p_Aq_curr_fx[M], pitch_fx[NB_SUBFR];
263 : Word16 LPC_de_old_fx[M + 1], LPC_de_curr_fx[M + 1];
264 0 : Word16 shift_wsp = add( Q_new, shift );
265 : Word16 rate_ctrl_fx;
266 0 : Word16 saved_Q_new = Q_new;
267 0 : move16();
268 0 : LPD_state_HANDLE hLPDmem = st_fx->hLPDmem;
269 0 : SC_VBR_ENC_HANDLE hSC_VBR = st_fx->hSC_VBR;
270 0 : BSTR_ENC_HANDLE hBstr = st_fx->hBstr;
271 : ivas_error error;
272 :
273 0 : error = IVAS_ERR_OK;
274 0 : move16();
275 0 : rate_ctrl_fx = hSC_VBR->rate_control;
276 0 : move16();
277 :
278 : /*------------------------------------------------------------------*
279 : * ACELP subframe loop
280 : *------------------------------------------------------------------*/
281 0 : p_Aw_fx = Aw_fx;
282 0 : p_Aq_fx = Aq_fx;
283 0 : FOR( i_subfr = 0; i_subfr < L_FRAME; i_subfr += L_SUBFR )
284 : {
285 : /*----------------------------------------------------------------*
286 : * Bandwidth expansion of A(z) filter coefficients
287 : * Find the the excitation search target "xn" and innovation
288 : * target in residual domain "cn"
289 : * Compute impulse response, h1[], of weighted synthesis filter
290 : *----------------------------------------------------------------*/
291 :
292 0 : Copy( &res_fx[i_subfr], &exc_fx[i_subfr], L_SUBFR ); /* Q_new */
293 :
294 0 : find_targets_ivas_fx( speech_fx, hLPDmem->mem_syn, i_subfr, &hLPDmem->mem_w0, p_Aq_fx,
295 : res_fx, L_SUBFR, p_Aw_fx, TILT_FAC_FX, xn_fx, NULL, h1_fx );
296 :
297 : /* scale xn[] and h1[] to avoid overflow in dot_product12() */
298 0 : Scale_sig( xn_fx, L_SUBFR, shift ); /* scaling of xn[] to limit dynamic at 12 bits */
299 :
300 : /* call voiced encoder at this point */
301 0 : IF( i_subfr == 0 ) /* generate the L_FRAME exc */
302 : {
303 0 : FOR( k = 0; k < M; k++ )
304 : {
305 0 : p_Aq_curr_fx[k] = p_Aq_fx[k + ( 3 * ( M + 1 ) ) + 1]; /* Q12 */
306 0 : move16();
307 : }
308 :
309 0 : E_LPC_f_lsp_a_conversion( st_fx->lsp_old_fx, p_Aq_old_fx, M );
310 0 : deemph_lpc_fx( p_Aq_curr_fx, p_Aq_old_fx, LPC_de_curr_fx, LPC_de_old_fx, 1 );
311 : /* both outputs LPC_de_curr_fx and LPC_de_old_fx are in Q12 */
312 :
313 :
314 : /* last frame-end lpc and curr frame-end lpc */
315 0 : IF( ( error = ppp_voiced_encoder_fx( hBstr, hSC_VBR, st_fx->bwidth, st_fx->last_coder_type_raw, st_fx->old_pitch_buf_fx, res_fx,
316 : excQ_ppp_fx, st_fx->pitch[1], LPC_de_old_fx, LPC_de_curr_fx, exc_fx, pitch_fx, Q_new ) ) != IVAS_ERR_OK )
317 : {
318 0 : return error;
319 : }
320 :
321 0 : Scale_sig( exc_fx, L_FRAME, sub( saved_Q_new, Q_new ) ); /* saved_Q_new */
322 0 : if ( EQ_16( hSC_VBR->bump_up, 1 ) )
323 : {
324 0 : i_subfr = L_FRAME;
325 0 : move16();
326 : }
327 : }
328 :
329 0 : IF( NE_16( hSC_VBR->bump_up, 1 ) )
330 : {
331 : /*-----------------------------------------------------------------*
332 : * Gain clipping test to avoid unstable synthesis on frame erasure
333 : * or in case of floating point encoder & fixed p. decoder
334 : *-----------------------------------------------------------------*/
335 0 : gp_clip_fx( st_fx->element_mode, st_fx->core_brate, st_fx->voicing_fx, i_subfr, st_fx->coder_type, xn_fx, st_fx->clip_var_fx, sub( shift_wsp, 1 ) );
336 :
337 :
338 : /* run the above to maintain gain clipping memories */
339 0 : gp_clip_test_gain_pit_fx( st_fx->element_mode, st_fx->core_brate, hSC_VBR->prev_ppp_gain_pit_fx, st_fx->clip_var_fx );
340 :
341 :
342 : /*-----------------------------------------------------------------*
343 : * Synthesize speech to update mem_syn[].
344 : * Update A(z) filters
345 : *-----------------------------------------------------------------*/
346 :
347 0 : Syn_filt_s( 1, p_Aq_fx, M, &excQ_ppp_fx[i_subfr], &synth_fx[i_subfr], L_SUBFR, hLPDmem->mem_syn, 1 );
348 :
349 :
350 0 : p_Aw_fx += ( M + 1 );
351 0 : p_Aq_fx += ( M + 1 );
352 : }
353 :
354 : } /* end of subframe loop */
355 :
356 0 : IF( hSC_VBR->bump_up )
357 : {
358 : /* PPP failed, bump up */
359 0 : hSC_VBR->ppp_mode = 0;
360 0 : move16();
361 0 : st_fx->core_brate = ACELP_7k20;
362 0 : move16();
363 0 : hSC_VBR->pppcountE = 0;
364 0 : move16();
365 :
366 0 : IF( hSC_VBR->set_ppp_generic )
367 : {
368 0 : st_fx->coder_type = GENERIC;
369 0 : move16();
370 : }
371 : ELSE
372 : {
373 0 : st_fx->coder_type = VOICED;
374 0 : move16();
375 : }
376 :
377 : /* We write signalling indices again only in case of bump_up */
378 : /* delete previous indices */
379 : #ifdef HARM_PUSH_BIT
380 0 : reset_indices_enc_fx( hBstr, hBstr->nb_ind_tot );
381 : #else
382 : reset_indices_enc_fx( hBstr, MAX_NUM_INDICES );
383 : #endif
384 : /* signalling matrix (writing of signalling bits) */
385 0 : signalling_enc_fx( st_fx );
386 : }
387 : ELSE
388 : {
389 0 : Copy( excQ_ppp_fx, exc_fx, L_FRAME ); /* Q_new */
390 :
391 : /*-----------------------------------------------------------------*
392 : * Updates: last value of new target is stored in mem_w0
393 : *-----------------------------------------------------------------*/
394 :
395 0 : hLPDmem->mem_w0 = sub( shr( xn_fx[L_SUBFR - 1], shift ), shr( exc_fx[L_FRAME - 1], 1 ) ); /*Q_new-1 */
396 :
397 0 : Copy( exc_fx, exc2_fx, L_FRAME ); /* Q_new */
398 :
399 0 : hLPDmem->dm_fx.prev_state = 2;
400 0 : move16(); /*Q0 dispMem index 0 */
401 0 : hLPDmem->dm_fx.prev_gain_pit[0] = hSC_VBR->prev_ppp_gain_pit_fx;
402 0 : move16(); /*Q14 dispMem index 2 */
403 :
404 0 : FOR( k = 1; k < 5; k++ )
405 : {
406 0 : hLPDmem->dm_fx.prev_gain_pit[k] = hLPDmem->dm_fx.prev_gain_pit[k - 1]; /* Q14 */
407 0 : move16();
408 : }
409 :
410 0 : hLPDmem->tilt_code = hSC_VBR->prev_tilt_code_fx; /* Q15 */
411 0 : move16();
412 0 : Copy( pitch_fx, pitch_buf_fx, NB_SUBFR );
413 0 : pitch_buf_fx[NB_SUBFR16k - 1] = pitch_fx[NB_SUBFR - 1];
414 :
415 0 : interp_code_5over2_fx( exc2_fx, bwe_exc, L_FRAME );
416 0 : set16_fx( voice_factors, 0, NB_SUBFR16k );
417 : }
418 :
419 0 : hSC_VBR->rate_control = rate_ctrl_fx; /* Q0 */
420 0 : move16();
421 :
422 0 : set16_fx( hSC_VBR->nelp_lp_fit_mem, 0, NELP_LP_ORDER * 2 );
423 :
424 0 : return error;
425 : }
|