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