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