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