Line data Source code
1 : /*====================================================================================
2 : EVS Codec 3GPP TS26.452 Aug 12, 2021. Version 16.3.0
3 : ====================================================================================*/
4 :
5 : #include "options.h" /* Compilation switches */
6 : #include "cnst.h" /* Common constants */
7 : #include "rom_com.h" /* Static table prototypes */
8 : #include "prot_fx.h" /* Function prototypes */
9 : #include "prot_fx_enc.h" /* Function prototypes */
10 : #include "stl.h"
11 :
12 :
13 : /*-------------------------------------------------------------------*
14 : * Scale_wsp
15 : *
16 : * Find scaling factor for weighted speech input
17 : *-------------------------------------------------------------------*/
18 :
19 3100 : void Scale_wsp(
20 : Word16 *wsp, /* i : Weigthed speech */
21 : Word16 *old_wsp_max, /* i : Last weigthed speech maximal valu */
22 : Word16 *shift, /* i/o: Scaling of current frame */
23 : Word16 *Q_exp, /* i/o: Differential scaling factor */
24 : Word16 *old_wsp_shift, /* i/o: Last wsp scaling */
25 : Word16 *old_wsp, /* i/o: Old weighted speech buffer */
26 : Word16 *mem_decim2, /* i/o: Decimation buffer */
27 : Word16 *old_wsp12k8, /* i/o: wsp memory @ 12.8 kHz used in pitol2 */
28 : const Word16 Len_p_look /* i : L_frame + look ahead Q0*/
29 : )
30 : {
31 : Word16 max, i, tmp;
32 :
33 : /* find maximum value on wsp[] for 12 bits scaling */
34 3100 : max = 0;
35 3100 : move16();
36 1143900 : FOR( i = 0; i < Len_p_look; i++ )
37 : {
38 1140800 : tmp = abs_s( wsp[i] );
39 1140800 : max = s_max( max, tmp );
40 : }
41 3100 : tmp = *old_wsp_max;
42 3100 : move16();
43 3100 : tmp = s_max( max, tmp );
44 3100 : *old_wsp_max = max;
45 3100 : move16();
46 :
47 3100 : *shift = sub( norm_s( tmp ), 3 );
48 3100 : move16();
49 :
50 3100 : *shift = s_min( *shift, 0 );
51 3100 : move16(); /* shift = 0..-3 */
52 :
53 :
54 3100 : Scale_sig( wsp, Len_p_look, *shift );
55 : /* scale old_wsp (warning: exp must be Q_new-Q_old) */
56 3100 : *Q_exp = add( *Q_exp, sub( *shift, *old_wsp_shift ) );
57 3100 : move16();
58 3100 : *old_wsp_shift = *shift;
59 3100 : move16();
60 3100 : Scale_sig( old_wsp12k8, L_WSP_MEM, *Q_exp ); /* Already scaled with premphasis */
61 3100 : Scale_sig( old_wsp, ( L_WSP_MEM - L_INTERPOL ) / OPL_DECIM, *Q_exp );
62 3100 : Scale_sig( mem_decim2, 3, *Q_exp );
63 :
64 3100 : Copy( old_wsp12k8, wsp - L_WSP_MEM, L_WSP_MEM ); /* Now memory and wsp vector have the same scaling */
65 :
66 3100 : return;
67 : }
68 :
69 : /*-------------------------------------------------------------------*
70 : * Preemph_scaled
71 : *
72 : * Find scaled preemphasis vector and its scaling factor
73 : *-------------------------------------------------------------------*/
74 6200 : void Preemph_scaled(
75 : Word16 new_speech[], /* i : Speech to scale already on 14 bits Q_new*/
76 : Word16 *Q_new, /* o : Scaling factor */
77 : Word16 *mem_preemph, /* i/o: Preemph memory Q(-1)*/
78 : Word16 *Q_max, /* i/o: Q_new limitation */
79 : const Word16 Preemph_factor, /* i : Preemphasis factor Q15*/
80 : const Word16 bits, /* i : Bit to remove from the output to (15-bits) */
81 : const Word16 bit1, /* i : Limit the output scaling to ((15-bits)-bit1) bits */
82 : const Word16 L_Q_mem, /* i : Number of old scaling to take into account Q0*/
83 : const Word16 Lframe, /* i : Frame length Q0*/
84 : const Word16 last_coder_type, /* i : coder_type Q0*/
85 : const Word16 Search_scaling /* i : enable the search of a proper scaling factor Q0*/
86 : )
87 : {
88 : Word16 i, tmp_fixed;
89 : Word16 mu, shift, QVal;
90 : Word32 L_tmp, L_maxloc;
91 : Word16 Q_min;
92 :
93 : /*---------------------------------------------------------------*
94 : * Perform fixed preemphasis through 1 - g z^-1 *
95 : * Scale signal to get maximum of precision in filtering *
96 : *---------------------------------------------------------------*/
97 :
98 : BASOP_SATURATE_WARNING_OFF_EVS
99 6200 : QVal = shl_sat( 1, sub( 15, bits ) ); //?sat
100 : BASOP_SATURATE_WARNING_ON_EVS
101 6200 : mu = shr( Preemph_factor, bits ); /* Q15 --> Q(15-bits) */
102 :
103 6200 : IF( EQ_16( Search_scaling, 1 ) )
104 : {
105 : /* get max of new preemphased samples (L_FRAME+L_FILT) */
106 :
107 6200 : L_tmp = L_mult( new_speech[0], QVal );
108 6200 : L_tmp = L_msu_sat( L_tmp, *mem_preemph, mu );
109 6200 : L_maxloc = L_abs( L_tmp );
110 :
111 1785600 : FOR( i = 1; i < Lframe; i++ )
112 : {
113 : /* Equivalent to tmp = max((abs(x[i] - mu*x[i-1]),tmp)
114 : * finds the max of preemphasized signal */
115 1779400 : L_tmp = L_mult( new_speech[i], QVal );
116 1779400 : L_tmp = L_msu_sat( L_tmp, new_speech[i - 1], mu );
117 1779400 : L_tmp = L_abs( L_tmp );
118 1779400 : L_maxloc = L_max( L_tmp, L_maxloc );
119 : }
120 :
121 : /* get scaling factor for new and previous samples */
122 : /* limit scaling to Q_MAX to keep dynamic for ringing in low signal */
123 : /* limit scaling to Q_MAX also to avoid a[0]<1 in syn_filt_32 */
124 6200 : tmp_fixed = s_max( extract_h( L_maxloc ), 1 );
125 :
126 : /* output on 14 bits: needed unless the resampling itself removes 1 bit*/
127 6200 : shift = sub( norm_s( tmp_fixed ), add( bits, bit1 ) );
128 6200 : shift = s_max( shift, 0 );
129 6200 : shift = s_min( shift, Q_MAX );
130 :
131 6200 : minimum_fx( Q_max, L_Q_mem, &Q_min );
132 6200 : *Q_new = s_min( shift, Q_min );
133 6200 : move16();
134 :
135 6200 : IF( tmp_fixed == 0 )
136 : {
137 0 : IF( NE_16( last_coder_type, UNVOICED ) )
138 : {
139 0 : *Q_new = s_min( *Q_new, 1 );
140 0 : move16();
141 : }
142 : }
143 :
144 31000 : FOR( i = L_Q_mem - 1; i > 0; i-- )
145 : {
146 24800 : Q_max[i] = Q_max[i - 1];
147 24800 : move16();
148 : }
149 6200 : Q_max[i] = shift;
150 6200 : move16();
151 : }
152 :
153 : /*---------------------------------------------------------------*
154 : * preemphasis with scaling (L_FRAME+L_FILT)
155 : * now do the actual preemphasis, since we have the
156 : * proper scaling factor.
157 : * Done backwards to save storage space
158 : *---------------------------------------------------------------*/
159 :
160 6200 : tmp_fixed = new_speech[Lframe - 1]; // Q_new
161 6200 : move16();
162 :
163 1785600 : FOR( i = sub( Lframe, 1 ); i > 0; i-- )
164 : {
165 1779400 : L_tmp = L_mult( new_speech[i], QVal );
166 1779400 : L_tmp = L_msu_sat( L_tmp, new_speech[i - 1], mu );
167 1779400 : L_tmp = L_shl( L_tmp, *Q_new );
168 1779400 : new_speech[i] = round_fx_sat( L_tmp ); // Q_new
169 : }
170 :
171 6200 : L_tmp = L_mult( new_speech[0], QVal );
172 6200 : L_tmp = L_msu_sat( L_tmp, *mem_preemph, mu );
173 6200 : L_tmp = L_shl_sat( L_tmp, *Q_new );
174 6200 : new_speech[0] = round_fx_sat( L_tmp );
175 6200 : *mem_preemph = tmp_fixed;
176 6200 : move16();
177 :
178 6200 : return;
179 : }
180 :
181 :
182 : /*-------------------------------------------------------------------*
183 : * Scale_mem
184 : *
185 : * Rescale memories
186 : *-------------------------------------------------------------------*/
187 3100 : Word32 Scale_mem_pre_proc( /* o : Min energy scaled */
188 : Word16 ini_frame_fx, /* i : Frame number Q0*/
189 : Word16 Q_exp, /* i : Diff scaling factor */
190 : Word16 *Q_new, /* i/o: Absolute scaling factor */
191 : Word16 *old_speech, /* i/o: Speech memory */
192 : Word16 *mem_wsp, /* i/o: wsp vector memory st->mem_wsp_q*/
193 : Word32 *enrO, /* i/o: Enr mem q_enrO*/
194 : Word32 *bckr, /* i/o: Back ground_fx ener mem q_bckr*/
195 : Word32 *ave_enr, /* i/o: Ave_enr mem Q_new + QSCALE*/
196 : Word32 *ave_enr2, /* i/o: Ave_enr2 mem Q_new + QSCALE*/
197 : Word32 *st_fr_bands1, /* i/o: spectrum per critical bands of the previous frame Q_new + QSCALE*/
198 : Word32 *st_fr_bands2, /* i/o: spectrum per critical bands 2 frames ago Q_new + QSCALE*/
199 : Word32 *st_Bin_E_old )
200 : {
201 : Word16 i;
202 : Word32 e_min_scaled;
203 :
204 3100 : e_min_scaled = L_shr_r( L_add( L_shr( E_MIN_FXQ15, sub( 14, add( *Q_new, QSCALE ) ) ), 1 ), 1 );
205 :
206 : /* scale previous samples and memory (Q_exp - Q_new - Q_old) */
207 : /* Scale( x, y, z ) : shift left vector x of size y by z bits ) */
208 3100 : IF( Q_exp != 0 )
209 : {
210 567 : IF( old_speech != NULL )
211 : {
212 567 : Scale_sig( old_speech, L_INP_MEM, Q_exp );
213 : }
214 567 : Scale_sig( mem_wsp, 1, Q_exp );
215 567 : IF( ini_frame_fx == 0 )
216 : {
217 : /* Scaling noise vectors if frame ==1*/
218 3 : Scale_sig32( enrO, NB_BANDS, *Q_new );
219 3 : Scale_sig32( bckr, NB_BANDS, *Q_new );
220 3 : Scale_sig32( ave_enr, NB_BANDS, *Q_new );
221 3 : Scale_sig32( ave_enr2, NB_BANDS, *Q_new );
222 3 : Scale_sig32( st_fr_bands1, NB_BANDS, *Q_new );
223 3 : Scale_sig32( st_fr_bands2, NB_BANDS, *Q_new );
224 3 : Scale_sig32( st_Bin_E_old, L_FFT / 2, *Q_new );
225 : }
226 : ELSE
227 : {
228 : /* Do scaling and valide minimum energy value */
229 11844 : FOR( i = 0; i < NB_BANDS; i++ )
230 : {
231 11280 : enrO[i] = L_max( L_shl_sat( enrO[i], Q_exp ), e_min_scaled );
232 11280 : bckr[i] = L_max( L_shl_sat( bckr[i], Q_exp ), e_min_scaled );
233 11280 : ave_enr[i] = L_max( L_shl_sat( ave_enr[i], Q_exp ), e_min_scaled );
234 11280 : ave_enr2[i] = L_max( L_shl_sat( ave_enr2[i], Q_exp ), e_min_scaled );
235 11280 : st_fr_bands1[i] = L_max( L_shl_sat( st_fr_bands1[i], Q_exp ), e_min_scaled );
236 11280 : st_fr_bands2[i] = L_max( L_shl_sat( st_fr_bands2[i], Q_exp ), e_min_scaled );
237 11280 : move32();
238 11280 : move32();
239 11280 : move32();
240 11280 : move32();
241 11280 : move32();
242 11280 : move32();
243 : }
244 : }
245 : }
246 :
247 3100 : return e_min_scaled;
248 : }
249 :
250 :
251 3100 : void Scale_mem_enc(
252 : Word16 Q_exp, /* i : Diff scaling factor */
253 : Word16 *old_speech16k, /* i/o: Speech memory */
254 : Word16 *old_exc, /* i/o: excitation memory */
255 : Word16 *old_bwe_exc, /* i/o: BWE excitation memory */
256 : Word16 *mem_w0, /* i/o: target vector memory */
257 : Word16 *mem_syn, /* i/o: synthesis memory */
258 : Word16 *mem_syn2, /* i/o: synthesis memory */
259 : Word16 *mem_deemp_preQ_fx, /*i/o: deemphasis memory for the high rate celp codec */
260 : Word16 *last_exc_dct_in,
261 : Word16 *old_input_lp )
262 : {
263 : /* scale previous samples and memory (Q_exp - Q_new - Q_old) */
264 : /* Scale( x, y, z ) : shift left vector x of size y by z bits ) */
265 3100 : IF( Q_exp != 0 )
266 : {
267 582 : Scale_sig( old_speech16k, L_INP_MEM, Q_exp );
268 582 : Scale_sig( mem_w0, 1, Q_exp );
269 : /* Scaling excitation */
270 582 : Scale_sig( old_exc, L_EXC_MEM, Q_exp );
271 582 : Scale_sig( old_bwe_exc, PIT16k_MAX * 2, Q_exp );
272 582 : Scale_sig( mem_syn, M, Q_exp );
273 582 : Scale_sig( mem_syn2, M, Q_exp );
274 582 : Scale_sig( last_exc_dct_in, L_FRAME, Q_exp );
275 582 : Scale_sig( mem_deemp_preQ_fx, 1, Q_exp );
276 582 : Scale_sig( old_input_lp, NS2SA( 16000, ACELP_LOOK_NS + DELAY_SWB_TBE_16k_NS + DELAY_FIR_RESAMPL_NS ), Q_exp );
277 : }
278 :
279 3100 : return;
280 : }
|