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