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 <math.h>
7 : #include "cnst.h" /* Common constants */
8 : #include "prot_fx.h" /* Function prototypes */
9 : #include "rom_com.h" /* Static table prototypes */
10 :
11 : /*-----------------------------------------------------------------*
12 : * transf_cdbk_dec()
13 : * Transform domain contribution decoding
14 : *-----------------------------------------------------------------*/
15 62640 : void transf_cdbk_dec_fx(
16 : Decoder_State *st_fx, /* i/o: decoder state structure */
17 : const Word16 harm_flag_acelp, /* i : harmonic flag for higher rates ACELP */
18 : const Word16 i_subfr, /* i : subframe index */
19 : const Word16 Es_pred, /* i : predicited scaled innovation energy (Q8) */
20 : const Word32 gain_code, /* i : innovative excitation gain (Q16) */
21 : Word16 *gain_preQ, /* o : prequantizer excitation gain (Q2) */
22 : Word32 *norm_gain_preQ, /* o : normalized prequantizer excitation gain (Q16) */
23 : Word16 code_preQ[], /* o : prequantizer excitation (Q8) */
24 : Word16 *unbits /* o : number of AVQ unused bits */
25 : )
26 : {
27 : Word16 i, index, nBits;
28 : Word16 nq[L_SUBFR / WIDTH_BAND];
29 : Word16 gain16, exp16, tmp16;
30 : Word32 L_tmp;
31 : Word32 dct_code32[L_SUBFR];
32 : Word16 qdct;
33 : #ifndef ISSUE_1866_replace_overflow_libdec
34 : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
35 : Flag Overflow = 0;
36 : move32();
37 : #endif
38 : #endif
39 : Word16 avq_bit_sFlag;
40 : Word16 trgtSvPos;
41 : Word16 Nsv;
42 62640 : Nsv = 8;
43 62640 : move16();
44 :
45 62640 : avq_bit_sFlag = 0;
46 62640 : move16();
47 62640 : if ( ( st_fx->element_mode > EVS_MONO ) )
48 : {
49 59295 : move16();
50 59295 : avq_bit_sFlag = 1;
51 : }
52 :
53 : /*--------------------------------------------------------------*
54 : * Set bit-allocation
55 : *--------------------------------------------------------------*/
56 :
57 62640 : nBits = st_fx->acelp_cfg.AVQ_cdk_bits[( i_subfr >> 6 )];
58 62640 : move16();
59 :
60 : /* increase # of AVQ allocated bits by unused bits from the previous subframe */
61 62640 : nBits = add( nBits, *unbits );
62 :
63 : /*--------------------------------------------------------------*
64 : * Dequantize prequantizer excitation gain
65 : *--------------------------------------------------------------*/
66 :
67 62640 : index = (Word16) get_next_indice_fx( st_fx, G_AVQ_BITS );
68 :
69 62640 : IF( ( st_fx->coder_type == INACTIVE ) )
70 : {
71 9705 : IF( GT_32( st_fx->core_brate, 56000 ) )
72 : {
73 0 : gain16 = usdequant_fx( index, G_AVQ_MIN_INACT_64k_Q12, G_AVQ_DELTA_INACT_64k_Q12 >> 1 ); // Q12
74 : }
75 9705 : ELSE IF( GT_32( st_fx->core_brate, 42000 ) )
76 : {
77 1140 : gain16 = usdequant_fx( index, G_AVQ_MIN_INACT_48k_Q12, G_AVQ_DELTA_INACT_48k_Q12 >> 1 ); // Q12
78 : }
79 : ELSE
80 : {
81 8565 : gain16 = usdequant_fx( index, G_AVQ_MIN_INACT_Q12, G_AVQ_DELTA_INACT_Q12 >> 1 ); // Q12
82 : }
83 :
84 9705 : L_tmp = Mult_32_16( gain_code, gain16 ); /* Q16 * Q12 - 15 -> Q13*/
85 9705 : L_tmp = L_shl_sat( L_tmp, 5 ); /* Q13 -> Q18*/
86 9705 : *gain_preQ = round_fx_sat( L_tmp ); /* Q2*/
87 9705 : move16();
88 : }
89 : ELSE
90 : {
91 52935 : test();
92 52935 : IF( GT_32( st_fx->core_brate, ACELP_24k40 ) && LE_32( st_fx->core_brate, 42000 ) )
93 : {
94 44580 : gain16 = gain_dequant_fx( index, G_AVQ_MIN_32kbps_Q15, G_AVQ_MAX_Q0, G_AVQ_BITS, &exp16 ); // Q0
95 : }
96 : ELSE
97 : {
98 8355 : gain16 = gain_dequant_fx( index, G_AVQ_MIN_Q15, G_AVQ_MAX_Q0, G_AVQ_BITS, &exp16 ); // Q0
99 : }
100 :
101 52935 : IF( Es_pred < 0 )
102 : {
103 320 : tmp16 = shr( negate( Es_pred ), 2 ); // Q8
104 320 : L_tmp = L_mult( gain16, tmp16 ); /* Q0*Q8 -> Q9*/
105 : }
106 : ELSE
107 : {
108 52615 : L_tmp = L_mult( gain16, Es_pred ); /* Q0*Q8 -> Q9*/
109 : }
110 52935 : L_tmp = L_shl( L_tmp, add( exp16, 9 ) ); /* Q18*/
111 52935 : *gain_preQ = round_fx( L_tmp ); /* Q2*/
112 52935 : move16();
113 : }
114 :
115 62640 : trgtSvPos = sub( Nsv, 1 );
116 62640 : test();
117 62640 : test();
118 62640 : test();
119 62640 : test();
120 62640 : test();
121 62640 : IF( avq_bit_sFlag && GT_16( nBits, 85 ) && !harm_flag_acelp && ( EQ_16( st_fx->coder_type, GENERIC ) || EQ_16( st_fx->coder_type, TRANSITION ) || ( st_fx->coder_type == INACTIVE ) ) )
122 : {
123 16742 : trgtSvPos = 2;
124 16742 : avq_bit_sFlag = 2;
125 16742 : move16();
126 16742 : move16();
127 : }
128 :
129 : /*--------------------------------------------------------------*
130 : * Demultiplex and decode subvectors from bit-stream
131 : *--------------------------------------------------------------*/
132 :
133 62640 : AVQ_demuxdec_fx( st_fx, code_preQ, &nBits, 8, nq, avq_bit_sFlag, trgtSvPos );
134 : Word16 q_Code_preQ;
135 62640 : IF( ( st_fx->element_mode == EVS_MONO ) )
136 : {
137 3345 : q_Code_preQ = Q_AVQ_OUT_DEC;
138 3345 : move16();
139 : }
140 : ELSE
141 : {
142 59295 : q_Code_preQ = Q_AVQ_OUT;
143 59295 : move16();
144 : }
145 4071600 : FOR( i = 0; i < L_SUBFR; i++ )
146 : {
147 : #ifdef ISSUE_1866_replace_overflow_libdec
148 4008960 : code_preQ[i] = shl_sat( code_preQ[i], q_Code_preQ );
149 : #else
150 : code_preQ[i] = shl_o( code_preQ[i], q_Code_preQ, &Overflow );
151 : #endif
152 4008960 : move16(); /* code_preQ in Q6*/
153 : }
154 :
155 : /* save # of AVQ unused bits for next subframe */
156 62640 : *unbits = nBits;
157 62640 : move16();
158 :
159 : /*--------------------------------------------------------------*
160 : * iDCT transform
161 : *--------------------------------------------------------------*/
162 :
163 62640 : test();
164 62640 : test();
165 62640 : IF( ( st_fx->coder_type == INACTIVE ) || GT_32( st_fx->core_brate, MAX_BRATE_AVQ_EXC_TD ) || harm_flag_acelp )
166 : {
167 26865 : qdct = 0;
168 26865 : move16();
169 26865 : edct2_fx( L_SUBFR, 1, code_preQ, dct_code32, &qdct, ip_edct2_64, w_edct2_64_fx );
170 : /*qdct = sub(q_Code_preQ,qdct+q_Code_preQ);*/
171 26865 : qdct = negate( qdct );
172 26865 : Copy_Scale_sig_32_16( dct_code32, code_preQ, L_SUBFR, qdct ); /* Output in q_Code_preQ */
173 : /*qdct = q_Code_preQ;*/
174 : }
175 : /*--------------------------------------------------------------*
176 : * Preemphasise
177 : *--------------------------------------------------------------*/
178 : /* in extreme cases at subframe boundaries, lower the preemphasis memory to avoid a saturation */
179 62640 : test();
180 62640 : IF( ( nq[7] != 0 ) && ( GT_16( sub( st_fx->last_nq_preQ, nq[0] ), 7 ) ) )
181 : {
182 : /* *mem_preemp /= 16; */
183 0 : st_fx->mem_preemp_preQ_fx = shr( st_fx->mem_preemp_preQ_fx, 4 );
184 0 : move16();
185 : }
186 62640 : st_fx->last_nq_preQ = nq[7];
187 62640 : move16();
188 :
189 : /* TD pre-quantizer: in extreme cases at subframe boundaries, lower the preemphasis memory to avoid a saturation */
190 62640 : test();
191 62640 : test();
192 62640 : test();
193 62640 : test();
194 62640 : test();
195 62640 : IF( st_fx->element_mode > EVS_MONO && ( st_fx->coder_type != INACTIVE ) && GE_32( st_fx->core_brate, MIN_BRATE_AVQ_EXC ) && LE_32( st_fx->core_brate, MAX_BRATE_AVQ_EXC_TD ) && !harm_flag_acelp && code_preQ[0] != 0 )
196 : {
197 : // PMT("Fixed point taking accound of the scaling needs to be done here ")
198 15739 : IF( GT_16( abs_s( st_fx->last_code_preq ), shl_sat( abs_s( code_preQ[0] ), 4 ) ) )
199 : {
200 0 : st_fx->mem_preemp_preQ_fx = shr( st_fx->mem_preemp_preQ_fx, 4 );
201 0 : move16();
202 : }
203 15739 : ELSE IF( GT_16( abs_s( ( st_fx->last_code_preq ) ), shl_sat( abs_s( code_preQ[0] ), 3 ) ) )
204 : {
205 3 : st_fx->mem_preemp_preQ_fx = shr( st_fx->mem_preemp_preQ_fx, 3 );
206 3 : move16();
207 : }
208 : }
209 :
210 62640 : st_fx->last_code_preq = code_preQ[L_SUBFR - 1]; // q_Code_preQ
211 62640 : move16();
212 62640 : PREEMPH_FX( code_preQ, FAC_PRE_AVQ_FX, L_SUBFR, &st_fx->mem_preemp_preQ_fx );
213 : /*--------------------------------------------------------------*
214 : * Compute normalized prequantizer excitation gain for FEC
215 : *
216 : * somewhat attenuate pre-quantizer normalized gain for FEC
217 : *--------------------------------------------------------------*/
218 :
219 : /*Ecode = (sum2_f( code_preQ, L_SUBFR ) + 0.01f) / L_SUBFR;*/
220 : /*norm_gain_preQ = 0.8f * (*gain_preQ) * (float)sqrt( Ecode );*/
221 :
222 62640 : L_tmp = Dot_product12( code_preQ, code_preQ, L_SUBFR, &exp16 );
223 :
224 62640 : IF( EQ_32( L_tmp, L_shl( 1, sub( 30, exp16 ) ) ) )
225 : {
226 : /* pre-quantizer contribution is zero */
227 702 : *norm_gain_preQ = 1;
228 702 : move16();
229 : }
230 : ELSE
231 : {
232 61938 : exp16 = sub( exp16, add( imult1616( q_Code_preQ, 2 ), 6 ) ); /* exp: (code_preQ in q_Code_preQ), -6 (/L_SUBFR) */
233 61938 : L_tmp = Isqrt_lc( L_tmp, &exp16 );
234 61938 : tmp16 = extract_h( L_tmp );
235 61938 : exp16 = sub( sub( 15, 10 ), exp16 ); /* tmp16 in Q10+exp16*/
236 61938 : tmp16 = div_s( 16384, tmp16 ); /* Q15+Q14-(Q10+Qexp16) = Q19-exp16*/
237 :
238 61938 : L_tmp = L_mult( *gain_preQ, tmp16 ); /* Q2+Q19-exp16+1 -> Q22-exp16 */
239 61938 : L_tmp = Mult_32_16( L_tmp, 26214 ); /* Q22-Qexp16+Q15+1-16 -> Q22-exp16*/
240 61938 : *norm_gain_preQ = L_shr( L_tmp, sub( 6, exp16 ) );
241 61938 : move32(); /* Q22-exp16 -> Q16*/
242 : }
243 :
244 :
245 62640 : st_fx->use_acelp_preq = 1;
246 62640 : move16();
247 :
248 62640 : return;
249 : }
250 :
251 : /*==========================================================================*/
252 : /* FUNCTION : Word16 gain_dequant_fx () */
253 : /*--------------------------------------------------------------------------*/
254 : /* PURPOSE : */
255 : /* * Returns decoded gain quantized between the specified */
256 : /* * range using the specified number of levels. */
257 : /*--------------------------------------------------------------------------*/
258 : /* INPUT ARGUMENTS : */
259 : /* Word16 index i: quantization index */
260 : /* Word16 min_val i : value of lower limit */
261 : /* Word16 max_val i : value of upper limit */
262 : /* Word16 bits i : number of bits to dequantize */
263 : /*--------------------------------------------------------------------------*/
264 : /* OUTPUT ARGUMENTS : */
265 : /* _Word16 *expg o : */
266 : /*--------------------------------------------------------------------------*/
267 : /* INPUT/OUTPUT ARGUMENTS : */
268 : /*--------------------------------------------------------------------------*/
269 : /* RETURN ARGUMENTS : */
270 : /* Word16 gain Q0 */
271 : /*--------------------------------------------------------------------------*/
272 : /* CALLED FROM : */
273 : /*==========================================================================*/
274 98059 : Word16 gain_dequant_fx( /* o: decoded gain */
275 : Word16 index, /* i: quantization index */
276 : const Word16 min_val, /* i: value of lower limit */
277 : const Word16 max_val, /* i: value of upper limit */
278 : const Word16 bits, /* i: number of bits to dequantize */
279 : Word16 *expg )
280 : {
281 : Word16 gain, c_min, c_max;
282 : Word16 levels;
283 : Word16 e_tmp, f_tmp;
284 : Word16 tmp, frac;
285 : Word32 L_tmp;
286 98059 : levels = shl( 1, bits );
287 :
288 : /*c_min = (float)log10(min);
289 : c_mult = (float) ((levels-1)/(log10(max)-c_min));
290 : gain = (float)pow( 10.0, (((float)index)/c_mult) + c_min );*/
291 :
292 : /*
293 : y =(index/cmult) + c_min
294 : = index/((levels-1)/(log10(max_val)-log10(min))) + log10(min)
295 : = (index*(log10(max_val)-log10(min)))/(levels-1) + log10(min)
296 : = x*log10(max_val) + (1-x)*log10(min)
297 : where x = index/(levels-1)
298 :
299 : gain = pow(10.0,y)
300 : = pow(2,3.321928*y)
301 : */
302 :
303 98059 : tmp = div_s( 1, sub( levels, 1 ) ); /*Q15*/
304 98059 : tmp = extract_l( L_shr( L_mult( index, tmp ), 1 ) ); /*Q15*/
305 :
306 98059 : e_tmp = norm_l( max_val );
307 98059 : f_tmp = Log2_norm_lc( L_shl( max_val, e_tmp ) );
308 98059 : e_tmp = sub( 30, e_tmp ); /*Q(max_val)=0*/
309 98059 : L_tmp = Mpy_32_16( e_tmp, f_tmp, 9864 ); /* Q16 */ /*log10(2) in Q15*/
310 98059 : c_max = round_fx( L_shl( L_tmp, 14 ) ); /* Q14 */
311 :
312 98059 : e_tmp = norm_l( min_val );
313 98059 : f_tmp = Log2_norm_lc( L_shl( min_val, e_tmp ) );
314 98059 : e_tmp = sub( sub( 30, e_tmp ), 15 ); /*Q(min_val)=15*/
315 98059 : L_tmp = Mpy_32_16( e_tmp, f_tmp, 9864 ); /* Q16 */ /*log10(2) in Q15*/
316 98059 : c_min = round_fx( L_shl( L_tmp, 14 ) ); /* Q14 */
317 :
318 98059 : L_tmp = L_mult( tmp, c_max ); /*Q30*/
319 98059 : L_tmp = L_mac( L_tmp, sub( 32767, tmp ), c_min ); /*Q30*/
320 :
321 98059 : L_tmp = Mult_32_16( L_tmp, 27213 ); /*Q28, 3.321928 in Q13*/
322 98059 : L_tmp = L_shr( L_tmp, 12 ); /*Q28->Q16*/
323 :
324 98059 : frac = L_Extract_lc( L_tmp, expg ); /* Extract exponent of gcode0 */
325 :
326 98059 : gain = extract_l( Pow2( 14, frac ) ); /* Put 14 as exponent so that */
327 : /* output of Pow2() will be: */
328 : /* 16384 < Pow2() <= 32767 */
329 98059 : *expg = sub( *expg, 14 );
330 98059 : move16();
331 :
332 98059 : return ( gain );
333 : }
|