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" /* Static table prototypes */
9 : #include "basop32.h"
10 :
11 : /*---------------------------------------------------------------------*
12 : * Local constants
13 : *---------------------------------------------------------------------*/
14 :
15 : #define L_SUBFR48k 240
16 : #define L_SUBFR32k 160
17 :
18 : /*---------------------------------------------------------------------*
19 : * Local function prototypes
20 : *---------------------------------------------------------------------*/
21 :
22 : static void filt_6k_7k_scale_fx( Word16 signal[], Word16 lg, Word16 mem[], Word16 fact, Word16 exp );
23 : static void hf_synthesis_fx( ZERO_BWE_DEC_HANDLE hBWE_zero, const Word32 core_brate, const Word16 output_subfr, const Word16 Aq[], const Word16 exc[], const Word16 Q_exc, Word16 synth[], Word16 synth16k[], const Word16 Q_syn );
24 : static void hf_synthesis_amr_wb_fx( const Word32 core_brate, const Word16 output_subfr, const Word16 Ap[], Word16 exc16k[], Word16 synth_out[], Word16 *mem_syn_hf, Word16 *delay_syn_hf, Word16 *mem_hp_interp, Word16 p_r, Word16 HF_corr_gain, Word16 til, Word16 voice_factors, const Word16 exc[], const Word16 Q_exc, const Word16 Q_out, Word16 qhf );
25 : static void envelope_fx( AMRWB_IO_DEC_HANDLE hAmrwb_IO, const Word32 core_brate, const Word16 Aq[], Word16 Ap[], Word16 *r, Word16 tilt0, Word16 tilt, Word16 voice_factor );
26 : static void AdaptiveStartBand_fx( Word16 *start_band, const Word32 rate, const Word16 *lsf, const Word16 voicing_fac, const Word16 clas, Word16 *voicing_flag, Word16 *start_band_old, Word32 *OptCrit_old );
27 :
28 :
29 : /*-------------------------------------------------------------------*
30 : * hf_synth_init()
31 : *
32 : * hf synthesis filters initialization
33 : * - initialization of 400 Hz high pass filter
34 : * - initialization of band pass 6kHz to 7kHz FIR filter
35 : *-------------------------------------------------------------------*/
36 :
37 3256 : void hf_synth_init_fx(
38 : ZERO_BWE_DEC_HANDLE hBWE_zero /* o : zero BWE decoder handle */
39 : )
40 : {
41 3256 : hBWE_zero->seed2 = RANDOM_INITSEED;
42 3256 : move16();
43 3256 : set16_fx( hBWE_zero->mem_hf_fx, 0, ( L_FIR - 1 ) );
44 3256 : set16_fx( hBWE_zero->mem_syn_hf_fx, 0, M );
45 3256 : set16_fx( hBWE_zero->mem_hp400_fx, 0, 4 );
46 3256 : set16_fx( hBWE_zero->delay_syn_hf_fx, 0, NS2SA( 16000, DELAY_CLDFB_NS ) );
47 3256 : set16_fx( hBWE_zero->mem_hp_interp_fx, 0, INTERP_3_1_MEM_LEN );
48 3256 : hBWE_zero->memExp1 = 0;
49 3256 : move16();
50 3256 : return;
51 : }
52 350067 : void hf_synth_reset_fx(
53 : ZERO_BWE_DEC_HANDLE hBWE_zero /* o : zero BWE decoder handle */
54 : )
55 : {
56 : Word16 i;
57 :
58 112371507 : FOR( i = 0; i < L_FRAME16k; i++ )
59 : {
60 112021440 : Random( &hBWE_zero->seed2 );
61 : }
62 :
63 350067 : set16_fx( hBWE_zero->mem_hf_fx, 0, ( L_FIR - 1 ) );
64 350067 : set16_fx( hBWE_zero->mem_syn_hf_fx, 0, M );
65 350067 : set16_fx( hBWE_zero->mem_hp400_fx, 0, 4 );
66 :
67 350067 : set16_fx( hBWE_zero->delay_syn_hf_fx, 0, NS2SA( 16000, DELAY_CLDFB_NS ) );
68 350067 : set16_fx( hBWE_zero->mem_hp_interp_fx, 0, INTERP_3_1_MEM_LEN );
69 350067 : hBWE_zero->memExp1 = 0;
70 350067 : move16();
71 350067 : return;
72 : }
73 :
74 : /*---------------------------------------------------------------------*
75 : * hf_synth()
76 : *
77 : * High frequency regeneration
78 : *---------------------------------------------------------------------*/
79 :
80 14951 : void hf_synth_fx(
81 : ZERO_BWE_DEC_HANDLE hBWE_zero, /* i/o: handle to 0 bit BWE parameters */
82 : const Word32 core_brate, /* i : core bitrate Q0*/
83 : const Word16 output_frame, /* i : output frame length Q0*/
84 : const Word16 *Aq, /* i : quantized Az Q12*/
85 : const Word16 *exc, /* i : excitation at 12.8 kHz Q_exc*/
86 : Word16 *synth, /* i : 12.8kHz synthesis signal Q_syn2*/
87 : Word16 *synth16k, /* o : 16kHz synthesis signal Q_syn2*/
88 : const Word16 Q_exc, /* i : excitation scaling */
89 : const Word16 Q_syn2 /* i : synthesis scaling */
90 : )
91 : {
92 : const Word16 *p_Aq;
93 : Word16 i_subfr, output_subfr;
94 :
95 14951 : output_subfr = output_frame / NB_SUBFR;
96 14951 : move16();
97 :
98 14951 : p_Aq = Aq; /* Q12 */
99 74755 : FOR( i_subfr = 0; i_subfr < L_FRAME; i_subfr += L_SUBFR )
100 : {
101 59804 : hf_synthesis_fx( hBWE_zero, core_brate, output_subfr, p_Aq, &exc[i_subfr], Q_exc, &synth[i_subfr], &synth16k[imult3216( (Word32) i_subfr, output_subfr ) / L_SUBFR], Q_syn2 );
102 :
103 59804 : p_Aq += ( M + 1 ); /* Q12 */
104 : }
105 :
106 14951 : return;
107 : }
108 :
109 : /*-----------------------------------------------------------------------------------*
110 : * hf_synthesis()
111 : *
112 : * HF noise synthesis
113 : * - Generate HF noise between 6 and 7 kHz.
114 : * - Set energy of noise according to synthesis tilt.
115 : * tilt > 0.8 ==> - 14 dB (voiced)
116 : * tilt 0.5 ==> - 6 dB (voiced or noise)
117 : * tilt < 0.0 ==> 0 dB (noise)
118 : *-----------------------------------------------------------------------------------*/
119 :
120 59804 : static void hf_synthesis_fx(
121 : ZERO_BWE_DEC_HANDLE hBWE_zero,
122 : const Word32 core_brate, /* i : core bitrate Q0*/
123 : const Word16 output_subfr, /* i : output sub-frame length Q0*/
124 : const Word16 Aq[], /* i : quantized Az Q12*/
125 : const Word16 exc[], /* i : excitation at 12.8 kHz Q_exc*/
126 : const Word16 Q_exc, /* i : excitation scaling */
127 : Word16 synth[], /* i : 12.8kHz synthesis signal Q_syn*/
128 : Word16 synth16k[], /* i/o: 16kHz synthesis signal Q_syn*/
129 : const Word16 Q_syn /* i : synthesis scaling */
130 : )
131 : {
132 : Word16 i, s;
133 : Word16 HF_syn[L_SUBFR16k], upsampled_HF_syn[L_FRAME48k / NB_SUBFR];
134 : Word16 HF_exc[L_SUBFR16k];
135 : Word16 tmp, ener, exp1, exp2, scale;
136 : Word32 L_tmp;
137 : Word16 Ap[M16k + 1];
138 : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
139 59804 : Flag Overflow = 0;
140 59804 : move32();
141 : #endif
142 :
143 : /*-----------------------------------------------------------------*
144 : * generate white noise vector
145 : *-----------------------------------------------------------------*/
146 :
147 59804 : Random_Fill( &hBWE_zero->seed2, L_SUBFR16k, HF_exc, 3 ); /* 3 = Shift Right by 3 */
148 :
149 : /* o: HF_exc in Q-3 */
150 :
151 : /*-----------------------------------------------------------------*
152 : * calculate energy scaling factor so that white noise would have the
153 : * same energy as exc12k8
154 : *-----------------------------------------------------------------*/
155 :
156 : /*ener = sum2_f( exc, L_SUBFR ) + 0.01f*/
157 59804 : ener = extract_h( Dot_product12( exc, exc, L_SUBFR, &exp2 ) ); /* Q15 */
158 59804 : exp2 = sub( exp2, add( Q_exc, Q_exc ) );
159 :
160 : /*tmp = round_fx(Dot_product12(HF_exc, HF_exc, output_subfr, &exp1)); */
161 59804 : L_tmp = Dot_product12( HF_exc, HF_exc, L_SUBFR16k, &exp1 );
162 59804 : tmp = round_fx( L_tmp ); /* Q15 */
163 : /* tmp = (float)(sqrt(ener/tmp)) */
164 : /* scale is -1 if tmp > ener */
165 59804 : scale = shr( sub( ener, tmp ), 15 ); /* Q0 */
166 59804 : tmp = shl( tmp, scale ); /* Q15 + scale*/
167 59804 : exp1 = sub( exp1, scale );
168 :
169 59804 : tmp = div_s( tmp, ener ); /* Q15 */
170 59804 : exp1 = sub( exp1, exp2 );
171 :
172 59804 : L_tmp = L_deposit_h( tmp ); /* Q31 */
173 :
174 59804 : L_tmp = Isqrt_lc( L_tmp, &exp1 );
175 59804 : scale = round_fx( L_tmp ); /* Q18 when Q_exc=-1, HF_exc in Q-3 */
176 :
177 59804 : exp2 = sub( hBWE_zero->memExp1, exp1 );
178 59804 : hBWE_zero->memExp1 = exp1;
179 59804 : move16();
180 :
181 : /*-----------------------------------------------------------------*
182 : * calculate energy scaling factor to respect tilt of synth12k8
183 : * (tilt: 1=voiced, -1=unvoiced)
184 : *-----------------------------------------------------------------*/
185 : #ifdef EVS_MONO
186 59804 : hp400_12k8_fx( synth, L_SUBFR, hBWE_zero->mem_hp400_fx );
187 : #else
188 : hp400_12k8_ivas_fx( synth, L_SUBFR, hBWE_zero->mem_hp400_fx );
189 : #endif
190 : /* i: mem_hp400 in Q_syn */
191 : /* i: synth in Q_syn */
192 : /* o: synth in Q_syn-3 */
193 :
194 59804 : L_tmp = L_mac( 1L, synth[0], synth[0] ); /* 2*(Q_syn-3)+1 */
195 3827456 : FOR( i = 1; i < L_SUBFR; i++ )
196 : {
197 3767652 : L_tmp = L_mac_sat( L_tmp, synth[i], synth[i] ); /* 2*(Q_syn-3)+1 */
198 : }
199 59804 : tmp = norm_l( L_tmp );
200 59804 : ener = extract_h( L_shl( L_tmp, tmp ) ); /* ener = r[0] */
201 : /*ener in Q = 2*(Q_syn-3)+1 = Q-5 when Q_syn=0*/
202 :
203 59804 : L_tmp = L_mac( 1L, synth[1], synth[0] ); /* 2*(Q_syn-3)+1 */
204 3767652 : FOR( i = 2; i < L_SUBFR; i++ )
205 : {
206 3707848 : L_tmp = L_mac_sat( L_tmp, synth[i], synth[i - 1] ); /* 2*(Q_syn-3)+1 */
207 : }
208 59804 : tmp = extract_h( L_shl( L_tmp, tmp ) ); /* tmp = r[1] */
209 : /*tmp in Q = 2*(Q_syn-3)+1 = Q-5 when Q_syn=0 */
210 : /*we use the same normalization factor for both ener and tmp, */
211 : /*if the headroom in "tmp" is less than "ener", tmp can saturate */
212 : /*but this is ok since below we apply some thresholds to tmp */
213 :
214 59804 : tmp = s_max( 0, tmp );
215 59804 : if ( tmp > 0 )
216 : {
217 59752 : tmp = div_s( tmp, ener ); /* Q15 */
218 : }
219 :
220 : /*-----------------------------------------------------------------*
221 : * modify energy of white noise according to synthesis tilt
222 : *-----------------------------------------------------------------*/
223 :
224 : /* tmp = 1.0 - fac */
225 59804 : tmp = add_o( 1, sub( 32767 /* 1 in Q15 */, tmp ), &Overflow );
226 59804 : test();
227 59804 : if ( core_brate == FRAME_NO_DATA || EQ_32( core_brate, SID_2k40 ) )
228 : {
229 : /* emphasize HF noise in CNG */
230 : /*fac *= 2.0f;*/
231 29384 : tmp = add_o( tmp, tmp, &Overflow ); /* Q15 */
232 : }
233 59804 : tmp = s_max( tmp, 3277 ); /* 0.1 in Q15 */
234 :
235 : /*scale *= fac;*/
236 59804 : tmp = mult_r( scale, tmp );
237 : /*-----------------------------------------------------------------*
238 : * modify HF excitation according to both calculated scaling factors
239 : * high pass filtering (0.94ms of delay)
240 : *-----------------------------------------------------------------*/
241 :
242 59804 : filt_6k_7k_scale_fx( HF_exc, L_SUBFR16k, hBWE_zero->mem_hf_fx, tmp, exp2 );
243 : /* i: input HF_exc is scaled in float, here scaling is done inside this filter */
244 : /* i: mem_hf in Q-2 */
245 : /* o: HF_exc in Q0 */
246 :
247 : /*-----------------------------------------------------------------*
248 : * synthesis of noise: 4.8kHz..5.6kHz --> 6kHz..7kHz
249 : *-----------------------------------------------------------------*/
250 :
251 : /*weight_a( Aq, Ap, 0.6f, M );*/
252 59804 : weight_a_lc_fx( Aq, Ap, Gamma_19661_Tbl_fx, M );
253 : /* o: Ap in Q14 */
254 :
255 59804 : Syn_filt_s( 0, Ap, M, HF_exc, HF_syn, L_SUBFR16k, hBWE_zero->mem_syn_hf_fx, 1 );
256 : /* i: Ap in Q14 */
257 : /* i: HF_exc in Q0 */
258 : /* o: HF_syn in Q0 */
259 : /* i/o: mem_syn_hf in Q0 */
260 :
261 59804 : Scale_sig( HF_syn, L_SUBFR16k, ( add( Q_syn, exp1 ) ) ); /* bring HF_syn to (Q_syn+exp1) */
262 :
263 : /*-----------------------------------------------------------------*
264 : * add filtered HF noise to speech synthesis
265 : *-----------------------------------------------------------------*/
266 :
267 : /* delay by 5 samples @16kHz to compensate CLDFB resampling delay (20samples) and HP filtering delay (roughly 15 samples) */
268 59804 : delay_signal_fx( HF_syn, L_SUBFR16k, hBWE_zero->delay_syn_hf_fx, NS2SA_FX2( 16000, DELAY_CLDFB_NS ) - 15 );
269 :
270 : /* interpolate the HF synthesis */
271 59804 : IF( EQ_16( output_subfr, L_SUBFR48k ) ) /* 48kHz sampled output */
272 : {
273 50072 : s = s_max( s_min( sub( s_min( Find_Max_Norm16( HF_syn, L_SUBFR16k ), Find_Max_Norm16( hBWE_zero->mem_hp_interp_fx, INTERP_3_1_MEM_LEN - 3 ) ), 3 ),
274 50072 : sub( Find_Max_Norm16( hBWE_zero->mem_hp_interp_fx + INTERP_3_1_MEM_LEN - 3, 3 ), 1 ) ),
275 : 0 );
276 50072 : Scale_sig( HF_syn, L_SUBFR16k, s ); /* Q_syn+exp1+s */
277 50072 : Scale_sig( hBWE_zero->mem_hp_interp_fx, INTERP_3_1_MEM_LEN, s ); /* Qx + s */
278 50072 : interpolate_3_over_1_allpass_fx( HF_syn, L_SUBFR16k, upsampled_HF_syn, hBWE_zero->mem_hp_interp_fx );
279 50072 : Scale_sig( upsampled_HF_syn, 3 * L_SUBFR16k, -s ); /* Q_syn + exp1 + s */
280 50072 : Scale_sig( hBWE_zero->mem_hp_interp_fx, INTERP_3_1_MEM_LEN, -s ); /* Qx */
281 50072 : Scale_sig( HF_syn, L_SUBFR16k, -s ); /* Q_syn+exp1 */
282 50072 : Scale_sig( upsampled_HF_syn, L_SUBFR48k, -1 );
283 : }
284 9732 : ELSE IF( EQ_16( output_subfr, L_SUBFR32k ) ) /* 32kHz sampled output */
285 : {
286 6368 : s = s_max( sub( s_min( Find_Max_Norm16( HF_syn, L_SUBFR16k ), Find_Max_Norm16( hBWE_zero->mem_hp_interp_fx, 2 * ALLPASSSECTIONS_STEEP ) ), 2 ), 0 );
287 6368 : Scale_sig( HF_syn, L_SUBFR16k, s ); /* Q_syn+exp1+s */
288 6368 : Scale_sig( hBWE_zero->mem_hp_interp_fx, 2 * ALLPASSSECTIONS_STEEP, s ); /* Qx + s */
289 6368 : Interpolate_allpass_steep_fx( HF_syn, hBWE_zero->mem_hp_interp_fx, L_SUBFR16k, upsampled_HF_syn );
290 6368 : Scale_sig( upsampled_HF_syn, 2 * L_SUBFR16k, -s ); /* Q_syn + exp1 */
291 6368 : Scale_sig( hBWE_zero->mem_hp_interp_fx, 2 * ALLPASSSECTIONS_STEEP, -s ); /* Qx */
292 6368 : Scale_sig( HF_syn, L_SUBFR16k, -s ); /* Q_syn+exp1 */
293 : }
294 : ELSE /* 16kHz sampled output */
295 : {
296 3364 : Copy( HF_syn, upsampled_HF_syn, L_SUBFR16k ); /* Q_syn */
297 : }
298 :
299 59804 : Vr_add( synth16k, upsampled_HF_syn, synth16k, output_subfr );
300 :
301 59804 : return;
302 : }
303 :
304 : /*-------------------------------------------------------------------*
305 : * filt_6k_7k:
306 : *
307 : * 15th order band pass 6kHz to 7kHz FIR filter.
308 : *
309 : * frequency: 4kHz 5kHz 5.5kHz 6kHz 6.5kHz 7kHz 7.5kHz 8kHz
310 : * dB loss: -60dB -45dB -13dB -3dB 0dB -3dB -13dB -45dB
311 : * (gain=4.0)
312 : *-------------------------------------------------------------------*/
313 59804 : static void filt_6k_7k_scale_fx(
314 : Word16 signal[], /* i/o: signal Qx*/
315 : Word16 lg, /* i : length of input Q0*/
316 : Word16 mem[], /* i/o: memory (size=30) exp*/
317 : Word16 fact, /* i : multiply factor Q0*/
318 : Word16 exp /* i : Mem Exponent */
319 : )
320 : {
321 : Word16 i, x[L_FRAME48k / NB_SUBFR + ( L_FIR - 1 )];
322 : Word32 L_tmp;
323 : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
324 59804 : Flag Overflow = 0;
325 59804 : move32();
326 : #endif
327 59804 : Copy_Scale_sig( mem, x, L_FIR - 1, exp );
328 :
329 4844124 : FOR( i = 0; i < lg; i++ )
330 : {
331 4784320 : x[i + L_FIR - 1] = shr( mult( signal[i], fact ), 2 ); /* Qx - 2 */
332 4784320 : move16(); /* gain of filter = 4 */
333 : }
334 4844124 : FOR( i = 0; i < lg; i++ )
335 : {
336 :
337 : Word16 j;
338 4784320 : L_tmp = 0;
339 4784320 : move32();
340 153098240 : FOR( j = 0; j < 31; j++ )
341 : {
342 148313920 : L_tmp = L_mac_o( L_tmp, x[i + j], fir_6k_7k_fx[j], &Overflow ); /* Q16 */
343 : }
344 :
345 4784320 : signal[i] = round_fx_o( L_tmp, &Overflow ); /* Q0 */
346 4784320 : move16();
347 : }
348 59804 : Copy( x + lg, mem, L_FIR - 1 ); /* Qx - 2 */
349 59804 : }
350 :
351 : /*-------------------------------------------------------------------*
352 : * hf_synth_amr_wb_init()
353 : *
354 : * hf synthesis filters initialization
355 : * - initialization of 1600 Hz low pass filter
356 : * - initialization of band pass 6kHz to 8kHz FIR filter for noise and line resampled signals
357 : *-------------------------------------------------------------------*/
358 :
359 6 : void hf_synth_amr_wb_init_fx(
360 : AMRWB_IO_DEC_HANDLE hAmrwb_IO /* i/o: AMR-WB IO data handle */
361 : )
362 : {
363 6 : hAmrwb_IO->prev_r_fx = 0;
364 6 : move16();
365 6 : hAmrwb_IO->fmerit_w_sm_fx = 0;
366 6 : move16();
367 6 : hAmrwb_IO->frame_count_fx = 0;
368 6 : move16();
369 6 : hAmrwb_IO->ne_min_fx = -7680;
370 6 : move16(); /*-30.0f in Q8*/
371 6 : hAmrwb_IO->fmerit_m_sm_fx = 0;
372 6 : move16();
373 6 : hAmrwb_IO->voice_fac_amr_wb_hf = 0;
374 6 : move16();
375 6 : hAmrwb_IO->unvoicing_fx = 0;
376 6 : move16();
377 6 : hAmrwb_IO->unvoicing_sm_fx = 32767;
378 6 : move16(); /*1.0f in Q15*/
379 6 : hAmrwb_IO->unvoicing_flag_fx = 0;
380 6 : move16();
381 6 : hAmrwb_IO->voicing_flag_fx = 0;
382 6 : move16();
383 6 : hAmrwb_IO->start_band_old_fx = 160;
384 6 : move16();
385 6 : hAmrwb_IO->OptCrit_old_fx = 32768;
386 6 : move32(); /*1.0f in Q15*/
387 : ;
388 6 : return;
389 : }
390 :
391 :
392 : /*-------------------------------------------------------------------*
393 : * hf_synth_amr_wb_reset()
394 : *
395 : * reset of HF synthesis filters
396 : * - needed in switching scenarios
397 : *-------------------------------------------------------------------*/
398 0 : void hf_synth_amr_wb_reset_fx(
399 : ZERO_BWE_DEC_HANDLE hBWE_zero, /* o : zero BWE decoder handle */
400 : AMRWB_IO_DEC_HANDLE hAmrwb_IO /* i/o: AMR-WB IO data handle */
401 : )
402 : {
403 : Word16 i;
404 :
405 :
406 0 : FOR( i = 0; i < L_FRAME16k; i++ )
407 : {
408 0 : Random( &hBWE_zero->seed2 );
409 : }
410 :
411 0 : set16_fx( hBWE_zero->mem_syn_hf_fx, 0, M );
412 0 : set16_fx( hBWE_zero->delay_syn_hf_fx, 0, NS2SA( 16000, DELAY_CLDFB_NS ) );
413 0 : set16_fx( hBWE_zero->mem_hp_interp_fx, 0, INTERP_3_1_MEM_LEN );
414 :
415 0 : hAmrwb_IO->prev_r_fx = 0;
416 0 : move16();
417 0 : hAmrwb_IO->fmerit_w_sm_fx = 0;
418 0 : move16();
419 0 : hAmrwb_IO->frame_count_fx = 0;
420 0 : move16();
421 0 : hAmrwb_IO->ne_min_fx = -7680;
422 0 : move16(); /*-30.0f in Q8*/
423 0 : hAmrwb_IO->fmerit_m_sm_fx = 0;
424 0 : move16();
425 0 : hAmrwb_IO->voice_fac_amr_wb_hf = 0;
426 0 : move16();
427 0 : hAmrwb_IO->unvoicing_fx = 0;
428 0 : move16();
429 0 : hAmrwb_IO->unvoicing_sm_fx = 32767;
430 0 : move16(); /*1.0f in Q15*/
431 0 : hAmrwb_IO->unvoicing_flag_fx = 0;
432 0 : move16();
433 0 : hAmrwb_IO->voicing_flag_fx = 0;
434 0 : move16();
435 0 : hAmrwb_IO->start_band_old_fx = 160;
436 0 : move16();
437 0 : hAmrwb_IO->OptCrit_old_fx = 32768;
438 0 : move32(); /*1.0f in Q15*/
439 :
440 0 : return;
441 : }
442 :
443 : /*-------------------------------------------------------------------*
444 : * hf_synth_amr_wb()
445 : *
446 : * HF synthesis in AMR-WB IO
447 : *-------------------------------------------------------------------*/
448 :
449 0 : void hf_synth_amr_wb_fx(
450 : AMRWB_IO_DEC_HANDLE hAmrwb_IO, /* i/o: AMR-WB IO data handle */
451 : ZERO_BWE_DEC_HANDLE hBWE_zero, /* o : zero BWE decoder handle */
452 : const Word32 core_brate, /* i : core bitrate Q0*/
453 : const Word16 output_frame, /* i : output frame length Q0*/
454 : const Word16 *Aq, /* i : quantized Az : Q12*/
455 : const Word16 *exc, /* i : excitation at 12.8 kHz : Q_exc*/
456 : Word16 *synth, /* i/o: synthesis signal at 12.8k : Q_syn*/
457 : Word16 *amr_io_class, /* i : signal class (determined by FEC algorithm) Q0*/
458 : Word16 *synth_out, /* i/o: output signal at output Fs : Q_out*/
459 : Word16 fmerit, /* i : classify parameter from FEC : Q14*/
460 : const Word16 *hf_gain, /* i : decoded HF gain Q0*/
461 : const Word16 *voice_factors, /* i : voicing factors : Q15*/
462 : const Word16 pitch_buf[], /* i : pitch buffer : Q5*/
463 : const Word16 ng_ener_ST, /* i : Noise gate - short-term energy : Q8*/
464 : const Word16 *lsf_new, /* i : ISF vector : Q2*/
465 : const Word16 Q_exc, /* i : exc scaling */
466 : const Word16 Q_out /* i : Q_syn2-1 */
467 : )
468 : {
469 0 : Word16 core_type = 1;
470 : Word16 start_band;
471 : Word16 HF_corr_gain[NB_SUBFR];
472 : Word16 filt_weight_coeff;
473 : Word16 i, j, output_subfr;
474 : Word16 *pt3, *pt4, *pt5;
475 : const Word16 *pt6, *pt7;
476 : Word16 *pt1, *pt2;
477 : Word32 enr1, enr2;
478 : Word16 til[NB_SUBFR], til0[NB_SUBFR];
479 : Word16 tmp1, tmp2;
480 : Word32 L_tmp;
481 : Word16 exp;
482 : Word16 pitch_var_cur;
483 : Word16 voice_fac;
484 : Word16 fmerit_w;
485 : Word16 fmerit_m;
486 : Word16 *p_Ap;
487 : const Word16 *p_Aq;
488 : Word16 Ap[( M16k + 1 ) * NB_SUBFR];
489 : Word16 sub_gain[NB_SUBFR];
490 : Word16 exc16k[L_FRAME16k];
491 : Word16 qhf;
492 : Word16 dct_exc[L_FRAME], dct_hb[L_FRAME16k], filt_weight[L_SUBFR16k];
493 : Word16 hb_ener, g, hb_tonal[L_SUBFR16k], tonal_ener, hb_amb[L_SUBFR16k], inv_g;
494 : Word16 fb, fn, signum[L_SUBFR16k];
495 : Word16 alpha, beta, ener, tmp, scale, rev_filt_weight_coeff, qdct, q1, q2, q3, q4, shift;
496 : Word16 e_subfr1, e_subfr2;
497 : Word32 exc32[L_FRAME], dct_exc32[L_FRAME], dct_hb32[L_FRAME16k], exc16k32[L_FRAME16k];
498 : Word16 q_tmp;
499 : Word16 gamma;
500 0 : move16();
501 :
502 0 : Scale_sig( synth, L_FRAME, -3 ); /* Q_syn - 3 */
503 :
504 0 : pt1 = synth + 1; /* Q_syn - 3 */
505 0 : pt2 = synth; /* Q_syn - 3 */
506 0 : pt3 = til;
507 0 : pt4 = til0;
508 0 : FOR( i = 0; i < NB_SUBFR; i++ )
509 : {
510 0 : enr1 = Dot_product( pt2, pt2, L_SUBFR ); /*2*(Q_syn-3)+1 */
511 0 : enr2 = Dot_product( pt1, pt2, L_SUBFR - 1 ); /*2*(Q_syn-3)+1 */
512 :
513 0 : tmp1 = extract_h( enr1 ); /*2*(Q_syn-3) - 15 */
514 0 : tmp1 = s_max( 1, tmp1 );
515 0 : tmp2 = extract_h( enr2 ); /*2*(Q_syn-3) - 15 */
516 :
517 0 : exp = norm_s( tmp1 );
518 0 : tmp1 = div_s( shl( 1, sub( 14, exp ) ), tmp1 ); /*Q(29-exp-2*(Q_syn-3)-1) */
519 0 : L_tmp = L_mult( tmp2, tmp1 ); /*30-exp-2*(Q_syn-3)-1+2*(Q_syn-3)+1 - >30-exp */
520 0 : *pt3 = round_fx( L_shl( L_tmp, sub( exp, 1 ) ) ); /*13 */
521 0 : move16();
522 0 : *pt4++ = *pt3++;
523 0 : move16();
524 :
525 0 : pt1 += L_SUBFR;
526 0 : pt2 += L_SUBFR;
527 : }
528 :
529 0 : output_subfr = shr( output_frame, 2 ); /* Q0 */
530 :
531 0 : if ( NE_16( *amr_io_class, 7 ) )
532 : {
533 0 : core_type = 0;
534 0 : move16();
535 : }
536 :
537 : /* modify LF parameters for excitation weighting or sub-frame gains calculating */
538 0 : pitch_var_cur = 0;
539 0 : move16();
540 0 : pt6 = pitch_buf; /* Q5 */
541 0 : pt7 = pitch_buf + 1; /* Q5 */
542 0 : FOR( i = 0; i < 3; i++ )
543 : {
544 0 : tmp1 = abs_s( sub( *pt6++, *pt7++ ) ); /*Q5 */
545 0 : pitch_var_cur = add( pitch_var_cur, shr( tmp1, 1 ) ); /*Q6 -> Q5 */
546 : }
547 0 : test();
548 0 : IF( GT_16( hAmrwb_IO->frame_count_fx, FRAME_COUNT_HF_SYNTH ) && *amr_io_class == UNVOICED_CLAS )
549 : {
550 0 : hAmrwb_IO->frame_count_fx = 0;
551 0 : hAmrwb_IO->ne_min_fx = -7680; /* -30.0f in Q8 */
552 0 : move16();
553 0 : move16(); /*Q8; */
554 : }
555 : ELSE
556 : {
557 0 : hAmrwb_IO->frame_count_fx = s_min( hAmrwb_IO->frame_count_fx, 2 * FRAME_COUNT_HF_SYNTH - 1 ); /* Q0 */
558 0 : move16();
559 0 : hAmrwb_IO->frame_count_fx = add( hAmrwb_IO->frame_count_fx, 1 ); /* Q0 */
560 0 : move16();
561 0 : hAmrwb_IO->ne_min_fx = s_min( hAmrwb_IO->ne_min_fx, ng_ener_ST ); /* Q8 */
562 0 : move16();
563 : }
564 :
565 0 : pt6 = voice_factors;
566 0 : L_tmp = L_mult( *pt6++, 4096 /* 0.25 IN Q14 */ ); /* Q30 */
567 0 : FOR( i = 1; i < 4; i++ )
568 : {
569 0 : L_tmp = L_mac( L_tmp, *pt6++, 4096 /* 0.25 IN Q14 */ ); /* Q30 */
570 : }
571 :
572 0 : voice_fac = round_fx( L_tmp ); /* Q14 */
573 :
574 : /*fmerit_w = fmerit > 5734 ? 5734 : (fmerit < 2458 ? 2458 : fmerit); //Q14 */
575 :
576 0 : fmerit_w = s_min( fmerit, 5734 /* 0.35 IN Q14 */ ); /* Q14 */
577 0 : fmerit_w = s_max( fmerit_w, 2458 /* 0.15 IN Q14 */ ); /* Q14 */
578 :
579 0 : if ( EQ_16( core_type, 1 ) )
580 : {
581 0 : fmerit_w = shr( fmerit_w, 1 ); /* Q14 */
582 : }
583 :
584 0 : L_tmp = L_mult( fmerit_w, add_sat( 16384, voice_fac ) ); /* Q14 */
585 0 : fmerit_w = extract_l( L_shr( L_tmp, 15 ) ); /*Q14 */
586 : /**fmerit_w_sm = add(mult_r(*fmerit_w_sm, 29491), mult_r(fmerit_w, 3277)); //Q14 */
587 0 : hAmrwb_IO->fmerit_w_sm_fx = round_fx( L_mac( L_mult( hAmrwb_IO->fmerit_w_sm_fx, 29491 ), fmerit_w, 3277 ) ); /*Q14 */
588 0 : move16();
589 0 : fmerit_w = hAmrwb_IO->fmerit_w_sm_fx;
590 :
591 0 : move16();
592 :
593 0 : tmp1 = fmerit; /* Q14 */
594 0 : move16();
595 0 : if ( LT_16( fmerit, 8192 /* 0.5 in Q14 */ ) )
596 : {
597 0 : tmp1 = 16384; /* 1.0f in Q14 */
598 0 : move16();
599 : }
600 0 : fmerit_m = negate( add( -32768, tmp1 ) );
601 0 : hAmrwb_IO->fmerit_m_sm_fx = add( shr( hAmrwb_IO->fmerit_m_sm_fx, 1 ), shr( fmerit_m, 1 ) ); /*Q14 */
602 0 : move16();
603 0 : fmerit_m = hAmrwb_IO->fmerit_m_sm_fx; /* Q14 */
604 0 : move16();
605 :
606 0 : pt1 = til;
607 0 : FOR( i = 0; i < NB_SUBFR; i++ )
608 : {
609 0 : tmp = sub( pitch_var_cur, 320 );
610 0 : tmp = s_and( tmp, *pt1 );
611 0 : if ( tmp < 0 )
612 : {
613 0 : *pt1 = 1638; /* 0.2 in Q13 */
614 0 : move16();
615 : }
616 :
617 0 : tmp1 = sub( 8192 /* 1.0 in Q13 */, *pt1 ); /* Q13 */
618 0 : *pt1 = s_max( 6554 /* 0.8 in Q13 */, tmp1 ); /* Q13 */
619 0 : move16();
620 0 : tmp1 = add( hAmrwb_IO->ne_min_fx, 7680 /* 30.0 in Q30 */ ); /*Q8 */
621 0 : tmp1 = mult_r( tmp1, 7340 ); /*Q20 - > 0.007 //Q13 */
622 0 : *pt1 = add( *pt1, tmp1 );
623 0 : move16(); /*Q13 */
624 :
625 0 : L_tmp = L_mult0( *pt1, fmerit_m ); /*Q13+14 */
626 0 : *pt1++ = extract_l( L_shr( L_tmp, 14 ) ); /*Q13 */
627 0 : move16();
628 : }
629 : /* predict LPC coefficents and calculate sub-frame gains */
630 0 : p_Aq = Aq;
631 0 : p_Ap = Ap;
632 0 : pt1 = sub_gain;
633 0 : pt2 = til0;
634 0 : pt3 = til;
635 0 : pt6 = voice_factors;
636 0 : FOR( i = 0; i < NB_SUBFR; i++ )
637 : {
638 0 : envelope_fx( hAmrwb_IO, core_brate, p_Aq, p_Ap, pt1, *pt2, *pt3, *pt6 );
639 0 : pt1++;
640 0 : pt2++;
641 0 : pt3++;
642 0 : pt6++;
643 :
644 0 : p_Aq += ( M + 1 );
645 0 : p_Ap += ( M + 1 );
646 : }
647 :
648 0 : AdaptiveStartBand_fx( &start_band, core_brate, lsf_new, voice_fac, *amr_io_class, &hAmrwb_IO->voicing_flag_fx, &hAmrwb_IO->start_band_old_fx, &hAmrwb_IO->OptCrit_old_fx );
649 :
650 0 : q_tmp = Exp16Array( L_FRAME, exc );
651 0 : qdct = sub( q_tmp, 1 );
652 0 : Copy_Scale_sig_16_32_DEPREC( exc, exc32, L_FRAME, qdct ); /* Qexc + qdct */
653 :
654 0 : qdct = add( qdct, Q_exc );
655 0 : edct_fx( exc32, dct_exc32, L_FRAME, &qdct );
656 0 : q_tmp = Exp32Array( L_FRAME, dct_exc32 );
657 :
658 0 : q_tmp = sub( q_tmp, 16 );
659 0 : Copy_Scale_sig_32_16( dct_exc32, dct_exc, L_FRAME, q_tmp ); /* qdct + qtmp */
660 0 : qdct = add( qdct, q_tmp );
661 :
662 0 : set16_fx( dct_hb, 0, L_FRAME16k );
663 0 : pt1 = &dct_hb[200]; /* qdct */
664 0 : pt2 = &dct_exc[200]; /* qdct */
665 0 : FOR( i = 200; i < 240; i++ )
666 : {
667 0 : *pt1++ = *pt2++;
668 0 : move16(); /*qdct */
669 : }
670 0 : set16_fx( signum, 1, L_SUBFR16k );
671 0 : pt1 = dct_hb + 240;
672 0 : pt2 = dct_exc + start_band;
673 0 : pt3 = signum;
674 0 : FOR( i = 240; i < L_FRAME16k; i++ )
675 : {
676 0 : if ( *pt2 < 0 )
677 : {
678 0 : *pt3 = -1;
679 0 : move16();
680 : }
681 0 : *pt1++ = abs_s( *pt2++ );
682 0 : move16(); /*qdct */
683 0 : pt3++;
684 : }
685 0 : hb_ener = dot_prod_satcontr( &dct_hb[240], &dct_hb[240], qdct, qdct, &q1, L_SUBFR16k );
686 :
687 0 : L_tmp = L_shl( L_mult( start_band, 205 /* 0.00625f in Q15 */ ), 14 ); /*Q30 */
688 0 : tmp = round_fx( L_tmp ); /*Q14 */
689 0 : tmp = sub( 18022 /* 1.1f in Q14 */, tmp ); /*Q14 */
690 0 : fmerit_w = round_fx( L_shl( L_mult( fmerit_w, tmp ), 1 ) ); /*Q: 14+14+1+1-16 = 14 */
691 :
692 :
693 0 : L_tmp = L_deposit_l( fmerit_w ); /*Q14 */
694 0 : L_tmp = Isqrt( L_tmp ); /*Q(31-7) */
695 0 : tmp = round_fx( L_tmp ); /*Q8 */
696 0 : q2 = norm_s( tmp );
697 0 : alpha = div_s( shl( 1, sub( 14, q2 ) ), tmp ); /*Q(29-q2-8); */
698 0 : alpha = shl( alpha, sub( q2, 7 ) ); /*Q14 */
699 :
700 0 : beta = sub( 16384 /* 1.0f in Q14 */, fmerit_w ); /*Q14 */
701 :
702 0 : L_tmp = L_mult( alpha, 31130 /* 0.95f in Q15 */ ); /*Q30 */
703 0 : gamma = round_fx( L_tmp ); /*Q14 */
704 0 : gamma = sub( 17203 /* 1.05f in Q14 */, gamma ); /*Q14 */
705 0 : gamma = s_min( 16384 /* 1.0f in Q14 */, gamma ); /* Q14 */
706 0 : gamma = s_max( 4915 /* 0.3f in Q14 */, gamma ); /* Q14 */
707 :
708 0 : IF( LT_16( beta, 16384 /* 1 in Q14 */ ) )
709 : {
710 0 : L_tmp = 1; /*variable for tonal energy*/
711 0 : move32();
712 :
713 0 : pt1 = hb_amb;
714 0 : pt2 = hb_tonal;
715 0 : pt3 = &dct_hb[240];
716 0 : FOR( i = 0; i < 8; i++ )
717 : {
718 0 : fn = add( i, 8 ); /* Q0 */
719 0 : tmp1 = div_s( 1, fn ); /*Q15 */
720 0 : tmp = 0;
721 0 : move16();
722 0 : pt4 = &dct_hb[240];
723 0 : FOR( j = 0; j < fn; j++ )
724 : {
725 0 : tmp = add_sat( tmp, shr( *pt4++, 2 ) ); /*qdct-2 */
726 : }
727 0 : *pt1 = round_fx( L_shl( L_mult( tmp, tmp1 ), 2 ) ); /*qdct */
728 0 : move16();
729 0 : *pt2 = sub( *pt3, *pt1 ); /* qdct */
730 0 : move16();
731 0 : IF( *pt2 > 0 )
732 : {
733 0 : L_tmp = L_mac0( L_tmp, shr( *pt2, 1 ), shr( *pt2, 1 ) ); /*2*qdct-2 */
734 : }
735 0 : pt1++;
736 0 : pt2++;
737 0 : pt3++;
738 : }
739 0 : FOR( ; i < L_SUBFR16k - 8; i++ )
740 : {
741 0 : fb = sub( i, 7 ); /* Q0 */
742 0 : fn = add( fb, 15 ); /* Q0 */
743 0 : tmp = 0;
744 0 : move16();
745 0 : pt4 = &dct_hb[fb + 240];
746 0 : FOR( j = fb; j < fn; j++ )
747 : {
748 0 : tmp = add_sat( tmp, shr( *pt4++, 2 ) ); /*qdct-2 */
749 : }
750 0 : *pt1 = mult_r( tmp, 8738 );
751 0 : move16(); /* 8738 = 1/15 in Q17 qdct */
752 0 : *pt2 = sub( *pt3, *pt1 );
753 0 : move16();
754 0 : IF( *pt2 > 0 )
755 : {
756 0 : L_tmp = L_mac0_sat( L_tmp, shr( *pt2, 1 ), shr( *pt2, 1 ) ); /*2*qdct-2 */
757 : }
758 0 : pt1++;
759 0 : pt2++;
760 0 : pt3++;
761 : }
762 0 : FOR( ; i < L_SUBFR16k; i++ )
763 : {
764 0 : fb = sub( i, 7 ); /* Q0 */
765 0 : tmp = 0;
766 0 : move16();
767 0 : fn = sub( L_SUBFR16k, fb ); /* Q0 */
768 0 : tmp1 = div_s( 1, fn ); /*Q15 */
769 0 : pt4 = &dct_hb[fb + 240];
770 0 : FOR( j = fb; j < L_SUBFR16k; j++ )
771 : {
772 0 : tmp = add_sat( tmp, shr( *pt4++, 2 ) ); /*qdct-2 */
773 : }
774 0 : *pt1 = round_fx( L_shl( L_mult( tmp, tmp1 ), 2 ) ); /*qdct */
775 0 : move16();
776 0 : *pt2 = sub( *pt3, *pt1 );
777 0 : move16();
778 0 : IF( *pt2 > 0 )
779 : {
780 0 : L_tmp = L_mac0_sat( L_tmp, shr( *pt2, 1 ), shr( *pt2, 1 ) ); /*2*qdct-2 */
781 : }
782 0 : pt1++;
783 0 : pt2++;
784 0 : pt3++;
785 : }
786 :
787 : /*scaling of hb_ener is q1; tonal_ener<hb_ener, so scaling of tonal_ener can be q1 too*/
788 0 : tonal_ener = round_fx( L_shr( L_tmp, sub( shl( qdct, 1 ), add( q1, 18 ) ) ) );
789 0 : tmp = sub( hb_ener, tonal_ener ); /*q1 */
790 0 : tmp1 = round_fx( L_shl( L_mult( beta, tonal_ener ), 1 ) ); /*q1 */
791 0 : tmp1 = sub( hb_ener, tmp1 ); /*q1 */
792 0 : tmp = div_s( tmp, s_max( tmp1, 1 ) ); /*Q15 */
793 0 : g = shl( mult( beta, tmp ), 1 ); /*Q15 */
794 0 : q2 = norm_s( g );
795 0 : inv_g = div_s( shl( 1, sub( 14, q2 ) ), g ); /*Q(29-q2-15); */
796 0 : inv_g = shl( inv_g, sub( q2, 2 ) ); /*Q12 */
797 0 : pt1 = hb_tonal;
798 0 : pt2 = hb_amb;
799 0 : pt3 = &dct_hb[240];
800 0 : pt4 = signum;
801 0 : FOR( i = 0; i < L_SUBFR16k; i++ )
802 : {
803 0 : IF( *pt1 > 0 )
804 : {
805 0 : *pt1 = mult_r( *pt1, g ); /*qdct */
806 0 : move16();
807 : }
808 0 : *pt2 = round_fx_sat( L_shl_sat( L_mult( *pt2, inv_g ), 3 ) ); /*qdct */
809 0 : move16();
810 0 : *pt3 = add_sat( *pt1, *pt2 );
811 0 : move16();
812 0 : *pt3 = extract_l( L_mult0( *pt3, *pt4 ) ); /*qdct */
813 0 : move16();
814 0 : pt1++;
815 0 : pt2++;
816 0 : pt3++;
817 0 : pt4++;
818 : }
819 :
820 0 : ener = dot_prod_satcontr( &dct_hb[240], &dct_hb[240], qdct, qdct, &q2, L_SUBFR16k ); /* q2 */
821 0 : scale = div_s( shl( 1, 14 ), hb_ener ); /*Q(29-q1) */
822 0 : L_tmp = L_mult( ener, scale ); /*30-q1+q2 */
823 0 : q2 = sub( q1, q2 ); /*30-q2 */
824 0 : scale = round_fx( Isqrt( L_shl( L_tmp, sub( q2, 24 ) ) ) ); /*Q12 */
825 0 : scale = round_fx_sat( L_shl_sat( L_mult( scale, gamma ), 4 ) ); /*Q15 */
826 : }
827 : ELSE
828 : {
829 0 : scale = 32767; /*~1 in Q15 */
830 0 : move16();
831 : }
832 :
833 0 : IF( EQ_32( core_brate, ACELP_6k60 ) )
834 : {
835 0 : filt_weight_coeff = 60; /* Q0 */
836 0 : move16();
837 0 : rev_filt_weight_coeff = 555;
838 0 : move16(); /* 1/(filt_weight_coeff-1) Q15 */
839 : }
840 0 : ELSE IF( EQ_32( core_brate, ACELP_8k85 ) )
841 : {
842 0 : filt_weight_coeff = 40; /* Q0 */
843 0 : move16();
844 0 : rev_filt_weight_coeff = 840; /* 1/(filt_weight_coeff-1) Q15 */
845 0 : move16();
846 : }
847 : ELSE
848 : {
849 0 : filt_weight_coeff = 20; /* Q0 */
850 0 : move16();
851 0 : rev_filt_weight_coeff = 1725; /* 1/(filt_weight_coeff-1) Q15 */
852 0 : move16();
853 : }
854 :
855 0 : pt1 = filt_weight;
856 0 : FOR( i = 0; i < filt_weight_coeff; i++ )
857 : {
858 0 : L_tmp = L_mult( -32735, rev_filt_weight_coeff ); /*Q31 */
859 0 : L_tmp = L_shl( Mult_32_16( L_tmp, i ), 14 ); /*Q16+14 */
860 0 : *pt1++ = add( round_fx( L_tmp ), 16384 );
861 0 : move16(); /*Q14 */
862 : }
863 :
864 0 : IF( EQ_32( core_brate, ACELP_23k85 ) )
865 : {
866 0 : pt1 = dct_hb + 240;
867 0 : tmp = sub( filt_weight_coeff, 80 ); /* Q0 */
868 0 : pt3 = filt_weight + tmp;
869 0 : FOR( i = 240; i < L_FRAME16k; i++ )
870 : {
871 0 : *pt1 = mult_r( *pt1, scale ); /*qdct */
872 0 : move16();
873 :
874 0 : IF( GE_16( i, sub( L_FRAME16k, filt_weight_coeff ) ) )
875 : {
876 0 : *pt1 = round_fx( L_shl( L_mult( *pt3, *pt1 ), 1 ) ); /*qdct */
877 0 : move16();
878 : }
879 0 : pt1++;
880 0 : pt3++;
881 : }
882 0 : pt1 = dct_hb + 200;
883 0 : pt6 = filt_hp_fx;
884 0 : FOR( i = 200; i < 256; i++ )
885 : {
886 0 : *pt1 = mult_r( *pt6++, *pt1 );
887 0 : move16(); /*qdct */
888 0 : pt1++;
889 : }
890 0 : pt1 = HF_corr_gain;
891 0 : pt6 = hf_gain;
892 0 : FOR( i = 0; i < NB_SUBFR; i++ )
893 : {
894 0 : tmp = *pt6++;
895 0 : move16();
896 0 : *pt1++ = HP_gain_fx[tmp]; /* Q15 */
897 0 : move16();
898 : }
899 : }
900 : ELSE
901 : {
902 0 : pt1 = dct_hb + 240;
903 0 : tmp = sub( filt_weight_coeff, 80 ); /* Q0 */
904 0 : pt3 = filt_weight + tmp;
905 0 : FOR( i = 240; i < L_FRAME16k; i++ )
906 : {
907 0 : *pt1 = mult_r( *pt1, scale ); /*qdct */
908 0 : IF( GT_16( i, 255 ) )
909 : {
910 0 : *pt1 = mult_r( 19505 /* 0.6 in Q15 */, *pt1 );
911 0 : move16();
912 : }
913 :
914 0 : IF( GE_16( i, sub( L_FRAME16k, filt_weight_coeff ) ) )
915 : {
916 0 : *pt1 = round_fx( L_shl( L_mult( *pt3, *pt1 ), 1 ) ); /*qdct */
917 0 : move16();
918 : }
919 0 : pt1++;
920 0 : pt3++;
921 : }
922 :
923 0 : pt1 = dct_hb + 200;
924 0 : pt6 = filt_hp_fx;
925 0 : pt7 = deem_tab_fx; /* Q15 */
926 0 : FOR( i = 200; i < 256; i++ )
927 : {
928 0 : *pt1 = mult_r( *pt6++, *pt1 );
929 0 : move16(); /*qdct */
930 0 : *pt1 = mult_r( *pt7++, *pt1 );
931 0 : move16(); /* qdct */
932 0 : pt1++;
933 : }
934 : }
935 :
936 0 : q_tmp = Exp16Array( L_FRAME16k, dct_hb );
937 0 : qhf = sub( q_tmp, 1 );
938 0 : Copy_Scale_sig_16_32_DEPREC( dct_hb, dct_hb32, L_FRAME16k, qhf ); /* qhf + qdct */
939 0 : qhf = add( qhf, qdct );
940 0 : edct_fx( dct_hb32, exc16k32, L_FRAME16k, &qhf );
941 0 : q_tmp = Exp32Array( L_FRAME16k, exc16k32 );
942 0 : q_tmp = sub( q_tmp, 16 );
943 0 : Copy_Scale_sig_32_16( exc16k32, exc16k, L_FRAME16k, q_tmp ); /* qhf + qtmp */
944 0 : qhf = add( qhf, q_tmp );
945 :
946 0 : ener = dot_prod_satcontr( exc, exc, Q_exc, Q_exc, &q1, L_FRAME );
947 0 : tmp = dot_prod_satcontr( exc16k, exc16k, qhf, qhf, &q2, L_FRAME16k );
948 :
949 0 : pt6 = exc;
950 0 : pt2 = exc16k;
951 :
952 0 : FOR( i = 0; i < NB_SUBFR; i++ )
953 : {
954 0 : e_subfr1 = dot_prod_satcontr( pt6, pt6, Q_exc, Q_exc, &q3, L_SUBFR );
955 0 : e_subfr2 = dot_prod_satcontr( pt2, pt2, qhf, qhf, &q4, L_SUBFR16k );
956 :
957 0 : L_tmp = L_mult( e_subfr1, tmp ); /*Q(q2+q3+1) */
958 0 : q3 = add( add( q2, q3 ), 1 );
959 0 : shift = norm_l( L_tmp );
960 0 : L_tmp = L_shl( L_tmp, shift ); /*Q(q3+shift); */
961 0 : q3 = add( q3, shift );
962 0 : scale = round_fx_sat( L_tmp ); /*Q(q3-16); */
963 0 : q3 = sub( q3, 16 );
964 0 : scale = div_s( shl( 1, 14 ), scale ); /*Q(29-q3) */
965 0 : L_tmp = L_mult( scale, ener ); /*Q(29-q3+q1+1) */
966 0 : shift = norm_l( L_tmp );
967 0 : L_tmp = L_shl( L_tmp, shift ); /*Q(29-q3+q1+1+shift) */
968 0 : scale = round_fx_sat( L_tmp ); /*Q(29-q3+q1+1+shift-16) */
969 0 : L_tmp = L_mult( scale, e_subfr2 ); /*Q(29-q3+q1+1+shift-16+q4+1)=Q(15+q1-q3+q4+shift) */
970 0 : q3 = sub( 15, q3 );
971 0 : q3 = add( q3, q1 );
972 0 : q3 = add( q3, shift );
973 0 : q3 = add( q3, q4 );
974 0 : scale = round_fx_sat( Isqrt( L_shl_sat( L_tmp, sub( 6, q3 ) ) ) ); /*Q12 */
975 0 : pt6 += L_SUBFR;
976 0 : FOR( j = 0; j < L_SUBFR16k; j++ )
977 : {
978 0 : *pt2 = round_fx_sat( L_shl_sat( L_mult( *pt2, scale ), 2 ) ); /*qhf-1 */
979 0 : move16();
980 0 : pt2++;
981 : }
982 : }
983 0 : qhf = sub( qhf, 1 );
984 :
985 0 : p_Ap = Ap;
986 0 : pt1 = exc16k;
987 0 : pt2 = synth_out;
988 0 : pt3 = sub_gain;
989 0 : pt4 = HF_corr_gain;
990 0 : pt5 = til0;
991 0 : pt6 = voice_factors;
992 0 : pt7 = exc;
993 :
994 0 : FOR( i = 0; i < NB_SUBFR; i++ )
995 : {
996 0 : hf_synthesis_amr_wb_fx( core_brate, output_subfr, p_Ap, pt1, pt2, hBWE_zero->mem_syn_hf_fx, hBWE_zero->delay_syn_hf_fx,
997 0 : hBWE_zero->mem_hp_interp_fx, *pt3, *pt4, *pt5, *pt6, pt7, Q_exc, Q_out, qhf );
998 0 : p_Ap += ( M + 1 );
999 0 : pt1 += L_SUBFR16k;
1000 0 : pt2 += output_subfr;
1001 0 : pt3++;
1002 0 : pt4++;
1003 0 : pt5++;
1004 0 : pt6++;
1005 0 : pt7 += L_SUBFR;
1006 : }
1007 :
1008 0 : return;
1009 : }
1010 0 : static void hf_synthesis_amr_wb_fx(
1011 : const Word32 core_brate, /* i : core bitrate : Q0*/
1012 : const Word16 output_subfr, /* i : output sub-frame length : Q0*/
1013 : const Word16 Ap[], /* i : quantized Aq : Q12*/
1014 : Word16 exc16k[], /* i : excitation at 16 kHz : qhf*/
1015 : Word16 synth_out[], /* i/o: synthesis signal at output Fs : Q_out*/
1016 : Word16 *mem_syn_hf, /* i/o: HF synthesis memory : Q_out*/
1017 : Word16 *delay_syn_hf, /* i/o: HF synthesis memory : Q_out*/
1018 : Word16 *mem_hp_interp, /* i/o: interpol. memory : Q_out*/
1019 : Word16 p_r, /* i : sub-frame gain : Q12*/
1020 : Word16 HF_corr_gain, /* i : HF gain index : Q14*/
1021 : Word16 til, /* Q14 */
1022 : Word16 voice_factors, /* Q14 */
1023 : const Word16 exc[], /* i : excitation at 12.8 kHz : Qexc*/
1024 : const Word16 Q_exc, /*exc scaling*/
1025 : const Word16 Q_out, /*synth_out scaling*/
1026 : Word16 qhf /*exc16k scaling*/
1027 : )
1028 : {
1029 : Word16 i;
1030 : Word16 HF_syn[L_SUBFR16k], upsampled_HF_syn[L_SUBFR48k];
1031 : Word16 ener, tmp, scale, exc2385[L_SUBFR16k];
1032 : Word32 L_tmp;
1033 : Word16 q1, q2, q3, shift;
1034 : Word16 *pt1, *pt2, flag;
1035 0 : IF( EQ_32( core_brate, ACELP_23k85 ) )
1036 : {
1037 0 : ener = dot_prod_satcontr( exc, exc, Q_exc, Q_exc, &q1, L_SUBFR );
1038 0 : tmp = dot_prod_satcontr( exc16k, exc16k, qhf, qhf, &q2, L_SUBFR16k );
1039 :
1040 0 : L_tmp = L_mult( ener, 6554 /* 0.2 in Q15 */ ); /*Q(q1+16) */
1041 0 : q3 = norm_l( L_tmp );
1042 0 : L_tmp = L_shl( L_tmp, q3 ); /*Q(q1+q3+16) */
1043 0 : ener = extract_h( L_tmp ); /*Q(q1+q3); */
1044 0 : q1 = add( q1, q3 );
1045 :
1046 0 : scale = div_s( shl( 1, 14 ), ener ); /*Q(29-q1) */
1047 0 : L_tmp = L_mult( tmp, scale ); /*30-q1+q2 */
1048 0 : q2 = sub( q1, q2 ); /*30-q2 */
1049 0 : scale = round_fx_sat( Isqrt( L_shl_sat( L_tmp, sub( q2, 24 ) ) ) ); /*Q12 */
1050 0 : pt1 = exc16k;
1051 0 : pt2 = exc2385;
1052 0 : FOR( i = 0; i < L_SUBFR16k; i++ )
1053 : {
1054 0 : L_tmp = L_mult( *pt1++, HF_corr_gain ); /*qhf+15*/
1055 0 : L_tmp = Mult_32_16( L_tmp, scale ); /*qhf-1+12+1*/
1056 0 : *pt2++ = round_fx( L_shl( L_tmp, 1 ) ); /*qhf-3*/
1057 0 : move16();
1058 : }
1059 :
1060 0 : pt1 = exc16k;
1061 0 : FOR( i = 0; i < L_SUBFR16k; i++ )
1062 : {
1063 0 : *pt1 = mult_r( *pt1, p_r ); /*qhf-3*/
1064 0 : move16();
1065 0 : pt1++;
1066 : }
1067 :
1068 0 : qhf = sub( qhf, 3 );
1069 :
1070 0 : ener = dot_prod_satcontr( exc16k, exc16k, qhf, qhf, &q1, L_SUBFR16k );
1071 0 : tmp = dot_prod_satcontr( exc2385, exc2385, qhf, qhf, &q2, L_SUBFR16k );
1072 0 : L_tmp = L_mult( ener, 9830 ); /*Q(q1+16) */
1073 0 : q3 = norm_l( L_tmp );
1074 0 : L_tmp = L_shl( L_tmp, q3 ); /*Q(q1+q3+16) */
1075 0 : ener = extract_h( L_tmp ); /*Q(q1+q3); */
1076 0 : q1 = add( q1, q3 );
1077 :
1078 0 : scale = div_s( shl( 1, 14 ), ener ); /*Q(29-q1) */
1079 0 : L_tmp = L_mult( tmp, scale ); /*30-q1+q2 */
1080 0 : q2 = sub( q1, q2 ); /*30-q2 */
1081 0 : scale = round_fx_sat( Isqrt( L_shl_sat( L_tmp, sub( q2, 24 ) ) ) ); /*Q12 */
1082 0 : flag = negate( s_and( til, -0x8000 ) );
1083 0 : if ( GT_16( scale, 4096 /* 1.0f in Q12 */ ) )
1084 : {
1085 0 : flag = 1;
1086 0 : move16();
1087 : }
1088 0 : IF( flag )
1089 : {
1090 0 : Copy( exc2385, exc16k, L_SUBFR16k ); /* qhf */
1091 : }
1092 : ELSE
1093 : {
1094 0 : pt1 = exc16k;
1095 0 : pt2 = exc2385;
1096 0 : FOR( i = 0; i < L_SUBFR16k; i++ )
1097 : {
1098 0 : tmp = sub( 16348 /* 1 in Q14 */, shl( til, 1 ) ); /*Q14 */
1099 0 : L_tmp = L_mult( tmp, sub( 26214 /* 1.6f in Q14 */, shr( voice_factors, 1 ) ) ); /*Q29*/
1100 0 : tmp = round_fx( L_shr( L_tmp, 1 ) ); /*Q12*/
1101 0 : tmp = s_min( tmp, 4096 /* 1 in Q12 */ );
1102 0 : tmp = s_max( tmp, scale );
1103 0 : *pt1++ = round_fx( L_shl( L_mult( *pt2++, tmp ), 3 ) ) /*qhf*/;
1104 0 : move16();
1105 : }
1106 : }
1107 : }
1108 : ELSE
1109 : {
1110 0 : pt1 = exc16k;
1111 0 : FOR( i = 0; i < L_SUBFR16k; i++ )
1112 : {
1113 0 : *pt1 = mult_r( *pt1, p_r ); /*qhf-3*/
1114 0 : move16();
1115 0 : pt1++;
1116 : }
1117 :
1118 0 : qhf = sub( qhf, 3 );
1119 : }
1120 :
1121 0 : shift = sub( qhf, Q_out );
1122 0 : Syn_filt_s( shift, Ap, M, exc16k, HF_syn, L_SUBFR16k, mem_syn_hf, 1 ); /*Q_out=qhf-shift */
1123 :
1124 : /*-----------------------------------------------------------------*
1125 : * Resample to output sampling rate
1126 : * Synchronize LB and HB components (delay componsation)
1127 : * Add synthesised high band to speech synthesis
1128 : *-----------------------------------------------------------------*/
1129 :
1130 0 : delay_signal_fx( HF_syn, L_SUBFR16k, delay_syn_hf, NS2SA( 16000, DELAY_CLDFB_NS ) );
1131 :
1132 0 : IF( EQ_16( output_subfr, L_SUBFR48k ) ) /* 48kHz sampled output */
1133 : {
1134 : Word16 s;
1135 0 : s = s_max( s_min( sub( s_min( Find_Max_Norm16( HF_syn, L_SUBFR16k ), Find_Max_Norm16( mem_hp_interp, INTERP_3_1_MEM_LEN - 3 ) ), 3 ),
1136 0 : sub( Find_Max_Norm16( mem_hp_interp + INTERP_3_1_MEM_LEN - 3, 3 ), 1 ) ),
1137 : 0 );
1138 0 : Scale_sig( HF_syn, L_SUBFR16k, s ); /* Qout + s */
1139 0 : Scale_sig( mem_hp_interp, INTERP_3_1_MEM_LEN, s ); /* Qout + s */
1140 :
1141 0 : interpolate_3_over_1_allpass_fx( HF_syn, L_SUBFR16k, upsampled_HF_syn, mem_hp_interp );
1142 :
1143 0 : Scale_sig( upsampled_HF_syn, L_SUBFR48k, add( -s, -1 ) ); /* Qout - 1 */
1144 0 : Scale_sig( mem_hp_interp, INTERP_3_1_MEM_LEN, -s ); /* Qout */
1145 0 : Scale_sig( HF_syn, L_SUBFR16k, -s ); /* Qout */
1146 : }
1147 0 : ELSE IF( EQ_16( output_subfr, L_SUBFR32k ) ) /* 32kHz sampled output */
1148 : {
1149 : Word16 s;
1150 0 : s = s_max( sub( s_min( Find_Max_Norm16( HF_syn, L_SUBFR16k ), Find_Max_Norm16( mem_hp_interp, 2 * ALLPASSSECTIONS_STEEP ) ), 2 ), 0 );
1151 0 : Scale_sig( HF_syn, L_SUBFR16k, s ); /* Qout + s */
1152 0 : Scale_sig( mem_hp_interp, 2 * ALLPASSSECTIONS_STEEP, s ); /* Qout + s */
1153 :
1154 0 : Interpolate_allpass_steep_fx( HF_syn, mem_hp_interp, L_SUBFR16k, upsampled_HF_syn );
1155 :
1156 0 : Scale_sig( upsampled_HF_syn, 2 * L_SUBFR16k, -s ); /* Qout */
1157 0 : Scale_sig( mem_hp_interp, 2 * ALLPASSSECTIONS_STEEP, -s ); /* Qout */
1158 0 : Scale_sig( HF_syn, L_SUBFR16k, -s ); /* Qout */
1159 : }
1160 : ELSE /* 16kHz sampled output */
1161 : {
1162 0 : Copy( HF_syn, upsampled_HF_syn, L_SUBFR16k ); /* Qout */
1163 : }
1164 :
1165 0 : Vr_add( synth_out, upsampled_HF_syn, synth_out, output_subfr );
1166 :
1167 0 : return;
1168 : }
1169 :
1170 0 : static Word16 EnhanceClass_fx(
1171 : const Word16 qq_fx, /* Qx */
1172 : const Word16 pp_fx, /* Qx */
1173 : const Word16 tilt0_fx, /* i : spectrum tilt Q13*/
1174 : const Word16 tilt_fx, /* i : spectrum tilt Q13*/
1175 : const Word16 voice_factor_fx, /* i : voice factor Q15*/
1176 : Word16 *voice_fac_fx, /* i/o: smoothed voiced parameter Q15*/
1177 : Word16 *unvoicing_fx, /* i/o: unvoiced parameter Q15*/
1178 : Word16 *unvoicing_sm_fx, /* i/o: smoothed unvoiced parameter Q15*/
1179 : Word16 *unvoicing_flag /* i/o: unvoiced flag Q0*/
1180 : )
1181 : {
1182 : Word16 unvoicing_tmp_fx;
1183 : Word16 tmp, tmp1;
1184 : Word32 L_tmp;
1185 : Flag Overflow;
1186 :
1187 :
1188 : /* Decide (*unvoicing_flag) to allow BWE enhancement when qq>pp */
1189 : /**voice_fac_fx = add(mult_r(*voice_fac_fx, 24576), mult_r(voice_factor_fx, 8192)); //Q15 */
1190 0 : *voice_fac_fx = round_fx( L_mac( L_mult( *voice_fac_fx, 24576 /* 0.75 in Q15 */ ), voice_factor_fx, 8192 /* 0.25 in Q15 */ ) ); /*Q15 */
1191 0 : move16();
1192 :
1193 0 : tmp = mult_r( sub( 8192 /* 1.0f in Q13 */, tilt0_fx ), 16384 /* 1.0f in Q15 */ ); /*Q13 */
1194 :
1195 0 : L_tmp = L_sub( 32768 /* 1.0 in Q15 */, *voice_fac_fx ); /*Q15 */
1196 :
1197 0 : L_tmp = Mult_32_16( L_tmp, tmp ); /*Q13 */
1198 0 : tmp = extract_l( L_tmp ); /*Q13 */
1199 :
1200 0 : tmp1 = mult_r( tilt_fx, 21845 ); /*Q15->1/1.5 ->Q13+15-15->Q13 */
1201 0 : tmp1 = s_min( tmp1, 8192 /* 1.0f in Q13 */ );
1202 :
1203 0 : L_tmp = L_mult( tmp, tmp1 ); /*Q13+Q13+1 */
1204 0 : unvoicing_tmp_fx = extract_l( L_shr( L_tmp, 12 ) ); /*Q15 */
1205 :
1206 : /**unvoicing_fx = add(mult_r(16384, *unvoicing_fx), mult_r(16384, unvoicing_tmp_fx)); //Q15 */
1207 0 : *unvoicing_fx = round_fx( L_mac( L_mult( 16384 /* 0.5 in Q15 */, *unvoicing_fx ), 16384 /* 0.5 in Q15 */, unvoicing_tmp_fx ) ); /*Q15 */
1208 0 : move16();
1209 :
1210 0 : IF( GT_16( *unvoicing_sm_fx, *unvoicing_fx ) )
1211 : {
1212 : /**unvoicing_sm_fx = add(mult_r(29491, *unvoicing_sm_fx), mult_r(3277, *unvoicing_fx)); //Q15 */
1213 0 : *unvoicing_sm_fx = round_fx( L_mac( L_mult( 29491 /* 0.9 in Q15 */, *unvoicing_sm_fx ), 3277 /* 0.1 in Q15 */, *unvoicing_fx ) ); /*Q15 */
1214 0 : move16();
1215 : }
1216 : ELSE
1217 : {
1218 : /**unvoicing_sm_fx = add(mult_r(32440, *unvoicing_sm_fx), mult_r(328, *unvoicing_fx)); //Q15 */
1219 0 : *unvoicing_sm_fx = round_fx( L_mac( L_mult( 32440 /* 0.99 in Q15 */, *unvoicing_sm_fx ), 328 /* 0.01 in Q15 */, *unvoicing_fx ) ); /*Q15 */
1220 0 : move16();
1221 : }
1222 :
1223 0 : if ( GT_16( sub_o( *unvoicing_fx, *unvoicing_sm_fx, &Overflow ), 3277 /* 0.1 in Q15 */ ) )
1224 : {
1225 0 : *unvoicing_flag = 1;
1226 0 : move16();
1227 : }
1228 :
1229 0 : if ( LT_16( sub_o( *unvoicing_fx, *unvoicing_sm_fx, &Overflow ), 1638 /* 0.05 in Q15 */ ) )
1230 : {
1231 0 : *unvoicing_flag = 0;
1232 0 : move16();
1233 : }
1234 0 : test();
1235 0 : return ( *unvoicing_flag && GT_16( qq_fx, pp_fx ) );
1236 : }
1237 :
1238 0 : static void envelope_fx(
1239 : AMRWB_IO_DEC_HANDLE hAmrwb_IO,
1240 : const Word32 core_brate, /* i : core bitrate Q0*/
1241 : const Word16 Aq_dyn_scal[], /* i : de-quant. LPC coefficents, dynamic scaling Q12*/
1242 : Word16 Ap[], /* o : extended LPC coefficents, Q12*/
1243 : Word16 *sub_gain, /* o : sub-frame gain, Q12*/
1244 : Word16 tilt0, /* i : spectrum tilt, Q14*/
1245 : Word16 tilt, /* i : spectrum tilt, Q13*/
1246 : Word16 voice_factor /* i : voice factor, Q15*/
1247 : )
1248 : {
1249 :
1250 : Word16 px, py, rx, ry, pp, rr, qx, qy, qq; /*Q10*/
1251 : Word16 i, Unvoicing_flag;
1252 : Word16 alpha; /*Q14*/
1253 : Word16 est_level1, est_level2; /*Q10*/
1254 : Word32 L_tmp;
1255 : Word16 tmp, q1, q2, q3, shift;
1256 : Word16 As[3], k1, k2;
1257 : Word16 *pt1;
1258 : const Word16 *pt2, *pt3;
1259 : Word16 Aq[M + 1];
1260 :
1261 :
1262 : /* Aq has dynamic scaling
1263 : go back to Q12 to make sure there's no overflow while calculating qx,qy*/
1264 0 : shift = sub( norm_s( Aq_dyn_scal[0] ), 2 );
1265 0 : Copy_Scale_sig( Aq_dyn_scal, Aq, M + 1, shift ); /* Q12 + shift */
1266 :
1267 : /* LPC envelope weighting */
1268 0 : IF( EQ_32( core_brate, ACELP_6k60 ) )
1269 : {
1270 0 : weight_a_lc_fx( Aq, Ap, Gamma_29491_Tbl, M );
1271 : }
1272 : ELSE
1273 : {
1274 0 : weight_a_lc_fx( Aq, Ap, Gamma_19661_Tbl_fx, M );
1275 : }
1276 : /* Ap has dynamic scaling
1277 : go back to Q12 to make sure there's no overflow while calculating px,py*/
1278 0 : shift = sub( norm_s( Ap[0] ), 2 );
1279 0 : IF( shift != 0 )
1280 : {
1281 0 : Scale_sig( Ap, M + 1, shift ); /* Q12 + shift */
1282 : }
1283 :
1284 : /* LPC envelope level estimate */
1285 0 : L_tmp = 0;
1286 0 : move32();
1287 0 : pt1 = Ap; /* Q12 */
1288 0 : pt2 = exp_tab_p_fx; /* Q14 */
1289 0 : FOR( i = 0; i <= M; i++ )
1290 : {
1291 0 : L_tmp = L_mac( L_tmp, *pt1++, *pt2++ ); /* Q27 */
1292 : }
1293 0 : q1 = norm_l( L_tmp );
1294 0 : L_tmp = L_shl( L_tmp, q1 ); /*Q(27+q1)*/
1295 0 : px = round_fx( L_shr( L_tmp, 1 ) ); /*Q(10+q1)*/
1296 :
1297 0 : L_tmp = L_deposit_l( 0 );
1298 :
1299 0 : pt1 = Ap; /* Q12 */
1300 0 : pt2 = exp_tab_p_fx + 33; /* Q14 */
1301 0 : FOR( i = 0; i <= M; i++ )
1302 : {
1303 0 : L_tmp = L_mac( L_tmp, *pt1++, *pt2-- ); /* Q27 */
1304 : }
1305 0 : q2 = norm_l( L_tmp );
1306 0 : shift = sub( q1, q2 );
1307 0 : IF( shift >= 0 )
1308 : {
1309 0 : px = shr( px, shift );
1310 0 : L_tmp = L_shl( L_tmp, q2 ); /*Q(27+q2)*/
1311 0 : q1 = q2;
1312 0 : move16();
1313 : }
1314 : ELSE
1315 : {
1316 0 : L_tmp = L_shl( L_tmp, q1 ); /*Q(27+q1)*/
1317 : }
1318 0 : py = round_fx( L_shr( L_tmp, 1 ) ); /*Q(10+q1)*/
1319 :
1320 0 : L_tmp = L_deposit_l( 0 );
1321 0 : pt2 = Aq; /* Q12 */
1322 0 : pt3 = exp_tab_q_fx; /* Q14 */
1323 0 : FOR( i = 0; i <= M; i++ )
1324 : {
1325 0 : L_tmp = L_mac_sat( L_tmp, *pt2++, *pt3++ ); /* Q27 */
1326 : }
1327 0 : q2 = norm_l( L_tmp );
1328 0 : L_tmp = L_shl( L_tmp, q2 ); /*Q(27+q2)*/
1329 0 : rx = round_fx( L_shr( L_tmp, 1 ) ); /*Q(10+q2)*/
1330 :
1331 0 : L_tmp = L_deposit_l( 0 );
1332 0 : pt2 = Aq; /* Q12 */
1333 0 : pt3 = exp_tab_q_fx + 33; /* Q14 */
1334 0 : FOR( i = 0; i <= M; i++ )
1335 : {
1336 0 : L_tmp = L_mac_sat( L_tmp, *pt2++, *pt3-- ); /* Q14 */
1337 : }
1338 0 : q3 = norm_l( L_tmp );
1339 0 : shift = sub( q2, q3 );
1340 0 : IF( shift >= 0 )
1341 : {
1342 0 : rx = shr( rx, shift );
1343 0 : L_tmp = L_shl( L_tmp, q3 ); /*Q(27+q3)*/
1344 0 : q2 = q3;
1345 0 : move16();
1346 : }
1347 : ELSE
1348 : {
1349 0 : L_tmp = L_shl( L_tmp, q2 ); /*Q(27+q2)*/
1350 : }
1351 0 : ry = round_fx( L_shr( L_tmp, 1 ) ); /*Q(10+q2)*/
1352 :
1353 0 : L_tmp = L_mult( px, px );
1354 0 : L_tmp = L_mac( L_tmp, py, py ); /*Q(21+2*q1)*/
1355 0 : pp = round_fx_sat( Isqrt( L_shr( L_tmp, add( 11, shl( q1, 1 ) ) ) ) ); /*Q10*/
1356 0 : L_tmp = L_mult( rx, rx );
1357 0 : L_tmp = L_mac( L_tmp, ry, ry ); /*Q(21+2*q1)*/
1358 0 : rr = round_fx_sat( Isqrt( L_shr( L_tmp, add( 11, shl( q2, 1 ) ) ) ) ); /*Q10*/
1359 0 : Copy( Aq, As, 3 );
1360 0 : IF( EQ_16( shr( As[2], 1 ), -2048 /* -1.0f in Q11 */ ) )
1361 : {
1362 0 : k2 = -2458; /* -0.6f in Q12 */
1363 0 : move16();
1364 0 : k1 = 4055; /* 0.99f in Q12 */
1365 0 : move16();
1366 0 : if ( As[1] < 0 )
1367 : {
1368 0 : k1 = -k1; /* Q12 */
1369 0 : move16();
1370 : }
1371 : }
1372 : ELSE
1373 : {
1374 0 : k1 = add( 2048 /* 1 in Q11 */, shr( As[2], 1 ) ); /*Q11 */
1375 0 : q1 = 11;
1376 0 : move16();
1377 0 : q2 = norm_s( k1 );
1378 0 : k1 = ( shl( k1, q2 ) ); /*q1+q2 */
1379 0 : tmp = abs_s( k1 );
1380 0 : q1 = add( q1, q2 );
1381 0 : tmp = div_s( shl( 1, 14 ), tmp ); /*Q(29-q1) */
1382 0 : if ( k1 < 0 )
1383 : {
1384 0 : tmp = negate( tmp );
1385 : }
1386 :
1387 0 : L_tmp = L_mult( As[1], tmp ); /*Q(42-q1) */
1388 0 : q1 = sub( q1, 14 );
1389 0 : k1 = round_fx_sat( L_shl_sat( L_tmp, q1 ) ); /*Q12 */
1390 0 : k2 = As[2];
1391 0 : move16(); /*Q12 */
1392 0 : if ( GT_16( k2, 2458 /* 0.6f in Q12 */ ) )
1393 : {
1394 0 : k2 = 2458; /* 0.6f in Q12 */
1395 0 : move16();
1396 : }
1397 0 : if ( LT_16( k2, -2458 /* -0.6f in Q12 */ ) )
1398 : {
1399 0 : k2 = -2458; /* -0.6f in Q12 */
1400 0 : move16();
1401 : }
1402 0 : if ( GT_16( k1, 4055 /* 0.99f in Q12 */ ) )
1403 : {
1404 0 : k1 = 4055; /* 0.99f in Q12 */
1405 0 : move16();
1406 : }
1407 0 : if ( LT_16( k1, -4055 /* -0.99f in Q12 */ ) )
1408 : {
1409 0 : k1 = -4055; /* -0.99f in Q12 */
1410 0 : move16();
1411 : }
1412 : }
1413 0 : As[1] = add( 4096 /* 1 in Q12 */, k2 );
1414 0 : move16();
1415 0 : L_tmp = L_mult( As[1], k1 ); /*Q25 */
1416 0 : As[1] = round_fx( L_shl( L_tmp, 3 ) ); /*Q12 */
1417 0 : move16();
1418 0 : As[2] = k2;
1419 0 : move16(); /*Q12 */
1420 :
1421 0 : L_tmp = L_deposit_l( 0 );
1422 0 : pt1 = As; /* Q12 */
1423 0 : pt2 = exp_tab_q_fx; /* Q14 */
1424 0 : FOR( i = 0; i < 3; i++ )
1425 : {
1426 0 : L_tmp = L_mac( L_tmp, *pt1++, *pt2++ ); /* Q27 */
1427 : }
1428 0 : q1 = norm_l( L_tmp );
1429 0 : L_tmp = L_shl( L_tmp, q1 ); /*Q(27+q1)*/
1430 0 : qx = round_fx( L_shr( L_tmp, 1 ) ); /*Q(10+q1)*/
1431 :
1432 :
1433 0 : L_tmp = L_deposit_l( 0 );
1434 0 : pt1 = As; /* Q12 */
1435 0 : pt2 = exp_tab_q_fx + 33; /* Q14 */
1436 0 : FOR( i = 0; i < 3; i++ )
1437 : {
1438 0 : L_tmp = L_mac( L_tmp, *pt1++, *pt2-- ); /* Q27 */
1439 : }
1440 0 : q2 = norm_l( L_tmp );
1441 0 : shift = sub( q1, q2 );
1442 0 : IF( shift >= 0 )
1443 : {
1444 0 : qx = shr( qx, shift );
1445 0 : L_tmp = L_shl( L_tmp, q2 ); /*Q(27+q2)*/
1446 0 : q1 = q2;
1447 0 : move16();
1448 : }
1449 : ELSE
1450 : {
1451 0 : L_tmp = L_shl( L_tmp, q1 ); /*Q(27+q1)*/
1452 : }
1453 0 : qy = round_fx( L_shr( L_tmp, 1 ) ); /*Q(10+q1)*/
1454 :
1455 :
1456 0 : L_tmp = L_mult( qx, qx ); /* Q21 + 2q1 */
1457 0 : L_tmp = L_mac( L_tmp, qy, qy ); /* Q21 + 2q1 */
1458 0 : qq = round_fx( Isqrt( L_shr( L_tmp, add( 11, shl( q1, 1 ) ) ) ) ); /*Q10*/
1459 :
1460 0 : Unvoicing_flag = EnhanceClass_fx( rr, pp, tilt0, tilt, voice_factor, &hAmrwb_IO->voice_fac_amr_wb_hf, &hAmrwb_IO->unvoicing_fx, &hAmrwb_IO->unvoicing_sm_fx, &hAmrwb_IO->unvoicing_flag_fx ); /* Q0 */
1461 0 : alpha = 0;
1462 0 : move16();
1463 0 : IF( Unvoicing_flag )
1464 : {
1465 0 : IF( GT_16( rr, ( hAmrwb_IO->prev_r_fx ) ) )
1466 : {
1467 0 : rr = shr( add_sat( rr, ( hAmrwb_IO->prev_r_fx ) ), 1 );
1468 : }
1469 :
1470 0 : hAmrwb_IO->prev_r_fx = rr;
1471 0 : move16();
1472 :
1473 0 : L_tmp = L_mult( tilt, sub( 26214 /* 1.6f in Q14 */, shr( voice_factor, 1 ) ) ); /*Q28*/
1474 :
1475 0 : L_tmp = L_min( L_tmp, 268435456 /* 1 in Q28 */ );
1476 :
1477 0 : L_tmp = Mult_32_16( L_tmp, rr ); /*Q23*/
1478 0 : rr = round_fx( L_shl( L_tmp, 3 ) ); /*Q10*/
1479 0 : L_tmp = L_mult( tilt, sub( 26214 /* 1.6f in Q14 */, shr( voice_factor, 1 ) ) ); /*Q28*/
1480 0 : L_tmp = L_max( L_tmp, 268435456 /* 1 in Q28 */ );
1481 0 : L_tmp = Mult_32_16( L_tmp, qq ); /*Q23*/
1482 0 : qq = round_fx_sat( L_shl_sat( L_tmp, 3 ) ); /*Q10*/
1483 0 : rr = s_min( rr, qq ); /* Q10 */
1484 0 : rr = s_max( rr, pp ); /* Q10 */
1485 : }
1486 : ELSE
1487 : {
1488 0 : test();
1489 0 : IF( LT_16( rr, 1024 /* 1 in Q10 */ ) && LT_16( ( hAmrwb_IO->prev_r_fx ), 1024 /* 1 in Q10 */ ) )
1490 : {
1491 0 : L_tmp = L_mult( rr, rr ); /*Q21*/
1492 0 : tmp = round_fx( L_shl( L_tmp, 9 ) ); /*Q14*/
1493 0 : L_tmp = L_sub( 2097152 /* 1 in Q21 */, L_tmp ); /*Q21*/
1494 0 : alpha = round_fx( L_shl( L_tmp, 9 ) ); /*Q14*/
1495 0 : L_tmp = L_mult( alpha, ( hAmrwb_IO->prev_r_fx ) ); /*Q25*/
1496 0 : L_tmp = L_mac( L_tmp, tmp, rr ); /*Q25*/
1497 0 : rr = round_fx_sat( L_shl_sat( L_tmp, 1 ) ); /*Q10*/
1498 : }
1499 :
1500 0 : hAmrwb_IO->prev_r_fx = rr;
1501 0 : move16();
1502 :
1503 0 : L_tmp = L_mult( tilt, sub( 26214 /* 1.6f in Q14 */, shr( voice_factor, 1 ) ) ); /*Q28*/
1504 :
1505 0 : L_tmp = L_min( L_tmp, 268435456 /* 1 in Q28 */ );
1506 0 : L_tmp = Mult_32_16( L_tmp, qq ); /*Q23*/
1507 0 : est_level1 = round_fx( L_shl( L_tmp, 3 ) ); /*Q10*/
1508 :
1509 0 : tmp = pp;
1510 0 : move16();
1511 0 : tmp = s_min( tmp, qq );
1512 0 : rr = s_min( tmp, rr );
1513 :
1514 0 : L_tmp = L_mult( abs_s( sub( tilt, 8192 /* 1 in Q13 */ ) ), sub( 26214 /* 1.6 in Q14 */, shr( voice_factor, 1 ) ) ); /*Q28*/
1515 0 : L_tmp = L_add( L_tmp, 268435456 /* 1 in Q28 */ );
1516 0 : L_tmp = Mult_32_16( L_tmp, rr ); /*Q23*/
1517 0 : est_level2 = round_fx( L_shl( L_tmp, 3 ) ); /*Q10*/
1518 :
1519 0 : rr = s_min( est_level1, est_level2 );
1520 : }
1521 :
1522 :
1523 0 : q1 = norm_s( pp );
1524 0 : tmp = div_s( shl( 1, sub( 14, q1 ) ), pp ); /*Q(29-q1-10) */
1525 0 : L_tmp = L_mult( rr, tmp ); /*Q(30-q1-10+10) */
1526 :
1527 0 : *sub_gain = s_min( 20480 /* 5.0f in Q12 */, round_fx_sat( L_shl_sat( L_tmp, sub( q1, 2 ) ) ) ); /*Q12 */
1528 0 : move16();
1529 0 : return;
1530 : }
1531 :
1532 : /*---------------------------------------------------------------------*
1533 : * AdaptiveStartBand_fx()
1534 : *
1535 : * adaptively select the start band of bandwidth extension
1536 : *---------------------------------------------------------------------*/
1537 :
1538 0 : static void AdaptiveStartBand_fx(
1539 : Word16 *start_band, /* o : start point of copied band Q0*/
1540 : const Word32 core_rate, /* i : core bitrate Q0*/
1541 : const Word16 *lsf_fx, /* i : lsf frequency Q2*/
1542 : const Word16 voicing_fac_fx, /* i : voicing factors Q14*/
1543 : const Word16 clas, /* i : signal class (determined by FEC algorithm) Q0*/
1544 : Word16 *voicing_flag, /* Q0 */
1545 : Word16 *start_band_old, /* Q0 */
1546 : Word32 *OptCrit_old_fx /*i/o : Q15 */
1547 : )
1548 : {
1549 : Word16 i, pos, M2, voicing_flag_old;
1550 : Word16 lsf_diff_fx[M];
1551 : Word16 W_fx;
1552 : Word16 tmp1, tmp2;
1553 : Word32 L_tmp;
1554 0 : Word32 OptCrit_fx = 32768, Crit_fx;
1555 : Word16 *pt1, flag;
1556 : const Word16 *pt2, *pt3;
1557 :
1558 0 : move32(); // corresponding to initialization of OptCrit_fx
1559 : /*voicing switching flag : to avoid switching start band frequently in VOICED or AUDIO area*/
1560 0 : voicing_flag_old = *voicing_flag; /* Q0 */
1561 0 : move16();
1562 0 : test();
1563 0 : test();
1564 0 : test();
1565 0 : if ( GT_16( voicing_fac_fx, 6554 /* 0.4 in Q14 */ ) || ( GT_16( voicing_fac_fx, 4915 /* 0.3 in Q14 */ ) && GE_16( clas, VOICED_CLAS ) ) || EQ_16( clas, AUDIO_CLAS ) )
1566 : {
1567 0 : *voicing_flag = 1; /* Q0 */
1568 0 : move16();
1569 : }
1570 :
1571 0 : test();
1572 0 : if ( LT_16( voicing_fac_fx, 3277 /* 0.2 in Q14 */ ) && LT_16( clas, VOICED_CLAS ) )
1573 : {
1574 0 : *voicing_flag = 0;
1575 0 : move16();
1576 : }
1577 :
1578 : /* core_rate adaptive start band */
1579 0 : *start_band = 160;
1580 0 : move16();
1581 0 : IF( LT_32( core_rate, ACELP_23k05 ) )
1582 : {
1583 0 : pt1 = lsf_diff_fx + 1;
1584 0 : pt2 = lsf_fx + 1;
1585 0 : pt3 = lsf_fx;
1586 0 : FOR( i = 1; i < ( M - 1 ); i++ )
1587 : {
1588 0 : *pt1++ = sub( *pt2++, *pt3++ );
1589 0 : move16(); /*Q2 */
1590 : }
1591 0 : tmp1 = extract_l( Mult_32_16( core_rate, 27046 /* 0.00005 in Q29 */ ) ); /*Q14 */
1592 0 : L_tmp = L_shr( L_mult0( tmp1, 22370 /* 1/6000 in Q27 */ ), 15 ); /*Q27->1/6000 ->Q26 */
1593 0 : tmp2 = extract_l( L_tmp ); /*Q26 */
1594 0 : W_fx = mult_r( tmp1, tmp2 ); /*Q25 */
1595 :
1596 0 : IF( EQ_16( clas, AUDIO_CLAS ) )
1597 : {
1598 0 : W_fx = mult_r( W_fx, 24576 /* 0.75 in Q15 */ ); /*Q25 */
1599 : }
1600 :
1601 0 : pos = 2;
1602 0 : move16();
1603 0 : M2 = M - 2;
1604 0 : move16();
1605 0 : IF( EQ_16( *voicing_flag, 1 ) )
1606 : {
1607 0 : IF( LE_32( core_rate, ACELP_8k85 ) )
1608 : {
1609 0 : M2 = M - 8;
1610 0 : move16();
1611 : }
1612 0 : ELSE IF( LE_32( core_rate, ACELP_12k65 ) )
1613 : {
1614 0 : M2 = M - 6;
1615 0 : move16();
1616 : }
1617 0 : ELSE IF( LE_32( core_rate, ACELP_15k85 ) )
1618 : {
1619 0 : M2 = M - 4;
1620 0 : move16();
1621 : }
1622 : }
1623 :
1624 : /*do the procedure for i==2*/
1625 0 : L_tmp = L_max( L_msu( 171798692 /* 1 in Q2.56+25+1 */, lsf_fx[2], W_fx ), 171799 /* 0.001 in Q2.56+25+1 */ ); /* Q2.56+25+1 */
1626 0 : Crit_fx = Mult_32_16( L_tmp, lsf_diff_fx[2] ); /* Q2.56+25+1+2.56-15 = Q11+2.56+2.56 */
1627 :
1628 0 : OptCrit_fx = L_add( Crit_fx, 0 );
1629 0 : pos = 2;
1630 0 : move16();
1631 : /*----------------------------------------------------------------*/
1632 :
1633 0 : pt2 = &lsf_fx[3];
1634 0 : pt1 = &lsf_diff_fx[3];
1635 0 : FOR( i = 3; i < M2; i++ )
1636 : {
1637 0 : L_tmp = L_max( L_msu( 171798692 /* 1 in Q2.56+25+1 */, *pt2++, W_fx ), 171799 /* 0.001 in Q2.56+25+1 */ ); /* Q2.56+25+1 */
1638 :
1639 0 : Crit_fx = Mult_32_16( L_tmp, *pt1++ ); /* Q2.56+25+1+2.56-15 = Q11+2.56+2.56 */
1640 :
1641 0 : IF( LE_32( Crit_fx, OptCrit_fx ) )
1642 : {
1643 0 : OptCrit_fx = L_add( Crit_fx, 0 ); /* Q11+2.56+2.56 */
1644 0 : pos = i; /* Q0 */
1645 0 : move16();
1646 : }
1647 : }
1648 :
1649 0 : tmp1 = add( mult_r( lsf_fx[pos - 1], 256 ), mult_r( lsf_fx[pos], 256 ) ); /* Q0 */
1650 0 : *start_band = s_min( s_max( sub( tmp1, 40 ), 40 ), 160 ); /* Q0 */
1651 0 : move16();
1652 :
1653 0 : L_tmp = Mult_32_16( *OptCrit_old_fx, 22938 /* 0.7 in Q15 */ ); /* Q11+2.56+2.56 */
1654 0 : test();
1655 0 : test();
1656 0 : test();
1657 0 : test();
1658 0 : IF( NE_16( voicing_flag_old, *voicing_flag ) || ( *voicing_flag == 0 && LT_32( OptCrit_fx, *OptCrit_old_fx ) ) ||
1659 : ( LT_32( OptCrit_fx, L_tmp ) && GT_32( *OptCrit_old_fx, 858993 /* 64 in Q11+2.56+2.56 */ ) ) )
1660 : {
1661 0 : *OptCrit_old_fx = OptCrit_fx; /* Q15 */
1662 0 : move16();
1663 0 : test();
1664 0 : test();
1665 0 : if ( LT_16( abs_s( sub( ( *start_band ), ( *start_band_old ) ) ), 20 ) && EQ_16( *voicing_flag, 1 ) && EQ_16( voicing_flag_old, 1 ) )
1666 : {
1667 0 : *start_band = *start_band_old; /* Q0 */
1668 0 : move16();
1669 : }
1670 : }
1671 : ELSE
1672 : {
1673 0 : test();
1674 0 : if ( LT_32( OptCrit_fx, ( *OptCrit_old_fx ) ) && EQ_16( ( *voicing_flag ), 1 ) )
1675 : {
1676 0 : *OptCrit_old_fx = OptCrit_fx; /* Q15 */
1677 0 : move16();
1678 : }
1679 :
1680 0 : *start_band = *start_band_old; /* Q0 */
1681 0 : move16();
1682 : }
1683 :
1684 0 : IF( EQ_16( clas, AUDIO_CLAS ) )
1685 : {
1686 0 : *start_band = s_min( *start_band, 120 ); /* Q0 */
1687 0 : move16();
1688 : }
1689 :
1690 0 : flag = sub( s_and( *start_band, 0x0001 ), 1 );
1691 0 : IF( flag == 0 )
1692 : {
1693 0 : *start_band = sub( *start_band, 1 ); /* Q0 */
1694 0 : move16();
1695 : }
1696 : }
1697 :
1698 0 : *start_band_old = *start_band; /* Q0 */
1699 0 : move16();
1700 :
1701 0 : return;
1702 : }
|