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 : static void gain_dec_gacelp_uv_fx(
9 : Word16 index, /* i : Quantization index vector Q0 */
10 : Word16 *code, /* i : algebraic code excitation Q9 */
11 : Word16 *code2, /* i : algebraic code excitation Q9 */
12 : Word16 mean_ener, /* i : Q8 */
13 : Word16 lcode, /* i : Subframe size Q0 */
14 : Word16 *gain_pit, /* o : Quantized pitch gain 1Q14 */
15 : Word32 *gain_code, /* o : Quantized codebook gain Q16 */
16 : Word32 *gain_code2, /* o : Quantized codebook gain Q16 */
17 : Word16 *past_gpit, /* i/o: past gain of pitch 1Q14 */
18 : Word32 *past_gcode, /* i/o: past energy of code Q16 */
19 : Word16 *gain_inov /* o : unscaled innovation gain 3Q12 */
20 : );
21 :
22 : /*-------------------------------------------------------------------*
23 : * decod_unvoiced()
24 : *
25 : * Decode unvoiced (UC) frames
26 : *-------------------------------------------------------------------*/
27 :
28 1374 : void decod_unvoiced_fx(
29 : Decoder_State *st_fx, /* i/o: decoder static memory */
30 : const Word16 *Aq_fx, /* Q12 i : LP filter coefficient */
31 : const Word16 Es_pred_fx, /* i : predicted scaled innov. energy */
32 : const Word16 uc_two_stage_flag, /* i : flag indicating two-stage UC */
33 : const Word16 coder_type, /* Q0 i : coding type */
34 : Word16 *tmp_noise_fx, /* Q0 o : long term temporary noise energy */
35 : Word16 *pitch_buf_fx, /* Q6 o : floating pitch values for each subframe*/
36 : Word16 *voice_factors_fx, /* Q15 o : voicing factors */
37 : Word16 *exc_fx, /* Q_X o : adapt. excitation exc */
38 : Word16 *exc2_fx, /* Q_X o : adapt. excitation/total exc */
39 : Word16 *bwe_exc_fx, /* Q_X i/o: excitation for SWB TBE */
40 : Word16 *gain_buf )
41 : {
42 : Word16 gain_pit_fx; /* Quantized pitch gain */
43 : Word32 gain_code_fx; /* Quantized algebraic codeebook gain */
44 : Word32 gain_code2_fx;
45 : Word16 gain_inov_fx; /* inovation gain */
46 : Word32 norm_gain_code_fx; /* normalized algebraic codeebook gain */
47 : Word16 voice_fac_fx; /* Voicing factor */
48 : Word16 code_fx[L_SUBFR]; /* algebraic codevector */
49 : Word16 i_subfr_fx;
50 : const Word16 *p_Aq_fx;
51 : Word16 *pt_pitch_fx;
52 : Word16 code2_fx[L_SUBFR];
53 :
54 1374 : gain_pit_fx = 0; /* Quantized pitch gain */
55 1374 : move16();
56 :
57 1374 : test();
58 1374 : IF( EQ_16( st_fx->last_ppp_mode_dec, 1 ) || EQ_16( st_fx->last_nelp_mode_dec, 1 ) )
59 : {
60 : /* SC_VBR - reset the decoder, to avoid memory not updated issue for this unrealistic case */
61 0 : CNG_reset_dec_fx( st_fx, pitch_buf_fx, voice_factors_fx );
62 : }
63 :
64 1374 : p_Aq_fx = Aq_fx; /*Q12*/ /* pointer to interpolated LPC parameters */
65 1374 : pt_pitch_fx = pitch_buf_fx; /* pointer to the pitch buffer */
66 :
67 6870 : FOR( i_subfr_fx = 0; i_subfr_fx < L_FRAME; i_subfr_fx += L_SUBFR )
68 : {
69 5496 : IF( !uc_two_stage_flag )
70 : {
71 : /*----------------------------------------------------------------*
72 : * Unvoiced subframe processing
73 : *----------------------------------------------------------------*/
74 :
75 0 : gaus_dec_fx( st_fx, i_subfr_fx, code_fx, &norm_gain_code_fx, &st_fx->lp_gainp_fx, &st_fx->lp_gainc_fx, &gain_inov_fx, &st_fx->tilt_code_fx,
76 0 : &voice_fac_fx, &gain_pit_fx, pt_pitch_fx, exc_fx, &gain_code_fx, exc2_fx, bwe_exc_fx, &( st_fx->Q_exc ), st_fx->Q_subfr );
77 : }
78 : ELSE
79 : { /*----------------------------------------------------------------*
80 : * Unvoiced subframe processing in two stages
81 : *----------------------------------------------------------------*/
82 :
83 : /* No adaptive codebook (UC) */
84 5496 : set16_fx( exc_fx + i_subfr_fx, 0, L_SUBFR );
85 :
86 5496 : *pt_pitch_fx = shl( L_SUBFR, Q6 );
87 5496 : move16();
88 :
89 : /*--------------------------------------------------------------*
90 : * Innovation decoding
91 : *--------------------------------------------------------------*/
92 :
93 : // inov_decode(st, st->core_brate, 0, L_FRAME, 1, i_subfr, p_Aq, st->tilt_code, *pt_pitch, code, L_SUBFR);
94 5496 : inov_decode_fx( st_fx, st_fx->core_brate, 0, L_FRAME, 1, i_subfr_fx, p_Aq_fx, st_fx->tilt_code_fx, *pt_pitch_fx, code_fx, L_SUBFR );
95 :
96 : /*-------------------------------------------------------*
97 : * Generate Gaussian excitation *
98 : *-------------------------------------------------------*/
99 :
100 : // gaus_L2_dec_flt(code2, st->tilt_code, p_Aq, FORMANT_SHARPENING_G1_FLT, &(st->seed_acelp));
101 5496 : gaus_L2_dec( code2_fx, st_fx->tilt_code_fx, p_Aq_fx, FORMANT_SHARPENING_G1, &( st_fx->seed_acelp ) );
102 :
103 : /*-----------------------------------------------------------------*
104 : * Gain encoding
105 : *-----------------------------------------------------------------*/
106 : Word16 tmp_idx_1;
107 5496 : tmp_idx_1 = 0;
108 5496 : move16();
109 5496 : if ( i_subfr_fx != 0 )
110 : {
111 4122 : tmp_idx_1 = idiv1616( i_subfr_fx, L_SUBFR );
112 : }
113 : Word16 index;
114 5496 : index = get_next_indice_fx( st_fx, st_fx->acelp_cfg.gains_mode[tmp_idx_1] );
115 :
116 5496 : gain_dec_gacelp_uv_fx( index, code_fx, code2_fx, Es_pred_fx, L_SUBFR, &gain_pit_fx, &gain_code_fx, &gain_code2_fx, &( st_fx->past_gpit ), &norm_gain_code_fx, &gain_inov_fx );
117 :
118 5496 : st_fx->tilt_code_fx = est_tilt_ivas_fx( exc_fx + i_subfr_fx, gain_pit_fx, code_fx, gain_code_fx, &voice_fac_fx, st_fx->Q_exc, L_SUBFR, 0 );
119 5496 : move16();
120 :
121 : /* update LP filtered gains for the case of frame erasures */
122 5496 : IF( EQ_16( st_fx->element_mode, EVS_MONO ) )
123 : {
124 0 : lp_gain_updt_fx( i_subfr_fx, gain_pit_fx, norm_gain_code_fx, &st_fx->lp_gainp_fx, &st_fx->lp_gainc_fx, L_FRAME );
125 : }
126 : ELSE
127 : {
128 5496 : lp_gain_updt_ivas_fx( i_subfr_fx, gain_pit_fx, norm_gain_code_fx, &st_fx->lp_gainp_fx, &st_fx->lp_gainc_fx, L_FRAME );
129 : }
130 :
131 : /*-------------------------------------------------------*
132 : * - Find the total excitation. *
133 : *-------------------------------------------------------*/
134 :
135 5496 : IF( EQ_16( st_fx->L_frame, L_FRAME ) )
136 : {
137 :
138 5496 : Rescale_exc( st_fx->hMusicPF->dct_post_old_exc_fx, &exc_fx[i_subfr_fx], ( bwe_exc_fx != NULL ) ? &bwe_exc_fx[( ( i_subfr_fx * 2 * HIBND_ACB_L_FAC ) >> 1 )] : NULL, st_fx->hGSCDec->last_exc_dct_in_fx,
139 5496 : L_SUBFR, L_SUBFR * HIBND_ACB_L_FAC, L_max( gain_code2_fx, gain_code_fx ), &( st_fx->Q_exc ), st_fx->Q_subfr, exc2_fx, i_subfr_fx, UNVOICED );
140 : }
141 : ELSE
142 : {
143 0 : Rescale_exc( st_fx->hMusicPF->dct_post_old_exc_fx, &exc_fx[i_subfr_fx], ( bwe_exc_fx != NULL ) ? &bwe_exc_fx[i_subfr_fx * 2] : NULL, st_fx->hGSCDec->last_exc_dct_in_fx,
144 0 : L_SUBFR, L_SUBFR * 2, L_max( gain_code2_fx, gain_code_fx ), &( st_fx->Q_exc ), st_fx->Q_subfr, exc2_fx, i_subfr_fx, UNVOICED );
145 : }
146 :
147 357240 : FOR( Word16 i = 0; i < L_SUBFR; i++ )
148 : {
149 : /*exc2[i + i_subfr] = gain_pit * exc[i + i_subfr]; //Q14
150 : exc2[i + i_subfr] += gain_code2 * code2[i]; //Q16
151 : exc[i + i_subfr] = exc2[i + i_subfr] + gain_code * code[i];*/
152 : // Q16
153 351744 : exc2_fx[i + i_subfr_fx] = shl( mult_r( gain_pit_fx, exc_fx[i + i_subfr_fx] ), 2 ); // Q_exc
154 351744 : exc2_fx[i + i_subfr_fx] = add( exc2_fx[i + i_subfr_fx], extract_l( L_shr( Mpy_32_16_1( gain_code2_fx, code2_fx[i] ), sub( Q10, st_fx->Q_exc ) ) ) ); // Q_exc
155 351744 : exc_fx[i + i_subfr_fx] = add( exc2_fx[i + i_subfr_fx], extract_l( L_shr( Mpy_32_16_1( gain_code_fx, code_fx[i] ), sub( Q10, st_fx->Q_exc ) ) ) ); // Q_exc
156 351744 : move16();
157 351744 : move16();
158 351744 : move16();
159 : }
160 : // Scale_sig(code_fx, L_SUBFR, 3); //Q12
161 : }
162 :
163 5496 : *tmp_noise_fx = extract_h( norm_gain_code_fx ); /*Q16*/
164 5496 : move16();
165 :
166 : /*----------------------------------------------------------------*
167 : * Excitation enhancements (update of total excitation signal)
168 : *----------------------------------------------------------------*/
169 :
170 5496 : IF( EQ_16( st_fx->element_mode, EVS_MONO ) )
171 : {
172 0 : enhancer_fx( st_fx->core_brate, 0, coder_type, i_subfr_fx, L_FRAME, voice_fac_fx, st_fx->stab_fac_fx,
173 0 : norm_gain_code_fx, gain_inov_fx, &st_fx->gc_threshold_fx, code_fx, exc2_fx, gain_pit_fx, &( st_fx->dm_fx ), st_fx->Q_exc );
174 : }
175 : ELSE
176 : {
177 5496 : enhancer_ivas_fx( MODE1, st_fx->core_brate, uc_two_stage_flag, 0, coder_type, i_subfr_fx, L_FRAME, voice_fac_fx, st_fx->stab_fac_fx,
178 5496 : norm_gain_code_fx, gain_inov_fx, &st_fx->gc_threshold_fx, code_fx /*Q9/Q12?*/, exc2_fx, gain_pit_fx, &( st_fx->dm_fx ), st_fx->Q_exc );
179 : }
180 :
181 : Word16 tmp_idx;
182 5496 : tmp_idx = 0;
183 5496 : move16();
184 5496 : IF( i_subfr_fx != 0 )
185 : {
186 4122 : tmp_idx = idiv1616( i_subfr_fx, L_SUBFR );
187 : }
188 5496 : voice_factors_fx[tmp_idx] = 0;
189 5496 : move16();
190 :
191 5496 : interp_code_5over2_fx( &exc_fx[i_subfr_fx], &bwe_exc_fx[( ( i_subfr_fx * 2 * HIBND_ACB_L_FAC ) >> 1 )], L_SUBFR );
192 :
193 5496 : p_Aq_fx += ( M + 1 );
194 5496 : pt_pitch_fx++;
195 5496 : st_fx->tilt_code_dec_fx[tmp_idx] = st_fx->tilt_code_fx;
196 5496 : move16();
197 : }
198 :
199 : /* SC-VBR */
200 1374 : st_fx->prev_gain_pit_dec_fx = gain_pit_fx;
201 1374 : move16();
202 :
203 1374 : set16_fx( gain_buf, 0, NB_SUBFR );
204 :
205 1374 : return;
206 : }
207 :
208 :
209 : /*---------------------------------------------------------------------*
210 : * gain_dec_gacelp_uv_fx
211 : *
212 : * Decoding of pitch and codebook gains for Unvoiced mode
213 : *---------------------------------------------------------------------*/
214 :
215 5496 : static void gain_dec_gacelp_uv_fx(
216 : Word16 index, /* i : Quantization index vector Q0 */
217 : Word16 *code, /* i : algebraic code excitation Q9 */
218 : Word16 *code2, /* i : algebraic code excitation Q9 */
219 : Word16 mean_ener, /* i : Q8 */
220 : Word16 lcode, /* i : Subframe size Q0 */
221 : Word16 *gain_pit, /* o : Quantized pitch gain 1Q14 */
222 : Word32 *gain_code, /* o : Quantized codebook gain Q16 */
223 : Word32 *gain_code2, /* o : Quantized codebook gain Q16 */
224 : Word16 *past_gpit, /* i/o: past gain of pitch 1Q14 */
225 : Word32 *past_gcode, /* i/o: past energy of code Q16 */
226 : Word16 *gain_inov /* o : unscaled innovation gain 3Q12 */
227 : )
228 : {
229 : Word16 i, exp_L_tmp1;
230 : Word16 exp_gcode;
231 : Word16 g_code;
232 : Word32 L_tmp, L_tmp1;
233 : Word32 pred_nrg_frame;
234 : Word16 exp_gcode2, g_code2, norm_code2;
235 : Word16 index2, s;
236 :
237 :
238 : /* pred_nrg_frame = (float)pow(10.0,mean_ener/20.0); */
239 5496 : L_tmp = L_mult( mean_ener, 10885 /*0.166096f * 2 Q15*/ ); /* 6Q25 */
240 5496 : pred_nrg_frame = BASOP_Util_InvLog2( L_sub( L_tmp, 503316480l /*15.f Q25*/ ) ); /* 15Q16 */
241 :
242 : /*-----------------------------------------------------------------*
243 : * Prediction gains
244 : *-----------------------------------------------------------------*/
245 : /* gain_inov = 1.0f / sqrt((dot_product(code, code, L_SUBFR) + 0.01) / L_SUBFR) */
246 5496 : L_tmp = calc_gain_inov( code, lcode, NULL, NULL );
247 5496 : *gain_inov = round_fx( L_shl( L_tmp, 15 - 3 ) ); /* gain_inov in Q12 */
248 5496 : move16();
249 : /* gcode = pred_nrg_frame * (*gain_inov); */
250 5496 : L_tmp = Mpy_32_16_1( pred_nrg_frame, *gain_inov ); /* 18Q13 */
251 5496 : i = sub( norm_l( L_tmp ), 1 ); // gaurd-bits
252 5496 : g_code = round_fx( L_shl( L_tmp, i ) );
253 5496 : exp_gcode = sub( 18, i );
254 :
255 : /* norm_code2 = 1.0f / sqrt((dot_product(code2, code2, lcode) + 0.01f) / lcode); */
256 5496 : L_tmp = calc_gain_inov( code2, lcode, NULL, NULL );
257 5496 : norm_code2 = round_fx( L_shl( L_tmp, 15 - 3 ) ); /* Q12 */
258 :
259 : /* g_code2 = pred_nrg_frame * norm_code2; */
260 5496 : L_tmp = Mpy_32_16_1( pred_nrg_frame, norm_code2 ); /* 18Q13 */
261 5496 : i = norm_l( L_tmp );
262 5496 : g_code2 = round_fx_sat( L_shl_sat( L_tmp, i ) );
263 5496 : exp_gcode2 = sub( 18, i );
264 :
265 : /*-----------------------------------------------------------------*
266 : * Decode pitch gain
267 : *-----------------------------------------------------------------*/
268 5496 : *gain_pit = 0;
269 5496 : move16();
270 5496 : *past_gpit = *gain_pit;
271 5496 : move16();
272 :
273 : /*-----------------------------------------------------------------*
274 : * past gains for error concealment
275 : *-----------------------------------------------------------------*/
276 5496 : index2 = shr( index, 5 );
277 5496 : index = s_and( index, 0x1F );
278 :
279 : /**gain_code= (float)pow(10.f,(((index*1.25f)-20.f)/20.f))*gcode;*/
280 :
281 5496 : L_tmp = L_mac( -111465139l /*-0.166096*20.0f Q25*/, shl( index, 16 - 7 ), 6803 /*0.166096f*1.25f Q15*/ );
282 :
283 5496 : i = add( 1, extract_l( L_shr( L_tmp, 25 ) ) );
284 5496 : L_tmp = L_lshl( L_tmp, 6 );
285 5496 : L_tmp = L_and( 0x7FFFFFFF, L_tmp );
286 :
287 5496 : L_tmp = Pow2( 30, round_fx( L_tmp ) );
288 5496 : L_tmp = L_shl( L_tmp, i - ( 31 - 16 ) ); /* Q16 */
289 :
290 : /* *past_gcode = L_tmp * pred_nrg_frame; */
291 5496 : i = norm_l( L_tmp );
292 5496 : L_tmp1 = L_shl( L_tmp, i );
293 5496 : exp_L_tmp1 = sub( 15, i );
294 :
295 5496 : i = norm_l( pred_nrg_frame );
296 5496 : L_tmp1 = Mpy_32_32( L_tmp1, L_shl( pred_nrg_frame, i ) );
297 5496 : exp_L_tmp1 = add( exp_L_tmp1, sub( 15, i ) );
298 :
299 5496 : *past_gcode = L_shl( L_tmp1, sub( exp_L_tmp1, 15 ) ); /* Q16 */
300 5496 : move32();
301 :
302 5496 : *gain_code = L_shl_sat( Mpy_32_16_1( *past_gcode, *gain_inov ), 3 );
303 5496 : move32();
304 :
305 :
306 5496 : L_tmp = Mpy_32_16_1( *gain_code, BASOP_Util_Divide1616_Scale( g_code2, g_code, &s ) );
307 5496 : L_tmp = L_shl( L_tmp, sub( sub( add( s, exp_gcode2 ), exp_gcode ), 2 ) ); /* Q16 */
308 5496 : L_tmp1 = L_add( L_tmp, 0 );
309 16447 : FOR( i = 0; i < index2; i++ )
310 : {
311 10951 : L_tmp1 = L_add( L_tmp1, L_tmp );
312 : }
313 5496 : *gain_code2 = L_tmp1;
314 :
315 :
316 5496 : return;
317 : }
|