Line data Source code
1 : /*====================================================================================
2 : EVS Codec 3GPP TS26.452 Aug 12, 2021. Version 16.3.0
3 : ====================================================================================*/
4 :
5 : #include <assert.h>
6 : #include "options.h" /* Compilation switches */
7 : #include "prot_fx.h"
8 : #include "rom_com.h"
9 : #include "enh64.h"
10 :
11 : #define PK_VQ_NOISE_DELTA ( (Word16) 3277 ) /* 0.1 in Q15 */
12 :
13 : /*------------------------------------------------------------------------*
14 : * Local function prototypes
15 : *------------------------------------------------------------------------*/
16 : static void dequant_peaks_fx( Decoder_State *st_fx, Word32 *vect_out, const Word32 *peak_gain );
17 : static Word16 hvq_dec_pos_fx( Decoder_State *st_fx, Word16 *pos_vec, const Word16 length, const Word16 num_peaks );
18 : static Word16 sparse_dec_pos_fx( Decoder_State *st_fx, Word16 *out, const Word16 length );
19 : static void peak_vq_dec_fx( Decoder_State *st_fx, Word32 *coefs_out, const Word32 brate, const Word16 num_bits, const Word16 *ynrm, Word16 *R, Word16 *vq_peak_idx, Word16 *Npeaks, const Word16 core );
20 : /*--------------------------------------------------------------------------
21 : * hvq_dec_fx()
22 : *
23 : * HVQ decoder
24 : *--------------------------------------------------------------------------*/
25 :
26 1229 : void hvq_dec_fx(
27 : Decoder_State *st_fx, /* i/o: decoder state structure */
28 : const Word16 num_bits, /* i : Number of available bits */
29 : const Word32 core_brate, /* i : Core bit-rate */
30 : const Word16 *ynrm, /* i : Envelope coefficients Q0 */
31 : Word16 *R, /* i/o: Bit allocation/updated bit allocation */
32 : Word16 *noise_level, /* o : Noise level in Q15 */
33 : Word16 *peak_idx, /* o : Peak position vector */
34 : Word16 *Npeaks, /* o : Total number of peaks */
35 : Word32 *coefsq_norm, /* o : Output vector in Q12 */
36 : const Word16 core /* i : Core */
37 : )
38 : {
39 : Word16 i;
40 : Word16 bits;
41 : Word16 noise_level_idx;
42 :
43 1229 : bits = num_bits;
44 1229 : move16();
45 :
46 3687 : FOR( i = 0; i < HVQ_BWE_NOISE_BANDS; i++ )
47 : {
48 2458 : noise_level_idx = get_next_indice_fx( st_fx, 2 ); /* 2-bits => max noise_level-idx = 3 */
49 2458 : noise_level[i] = i_mult( noise_level_idx, PK_VQ_NOISE_DELTA );
50 2458 : move16(); /* max noise_level=3*0.1 => Q15 is good enough */
51 :
52 2458 : bits = sub( bits, 2 );
53 : }
54 :
55 1229 : peak_vq_dec_fx( st_fx, coefsq_norm, core_brate, bits, ynrm, R, peak_idx,
56 : Npeaks, core );
57 1229 : }
58 :
59 : /*--------------------------------------------------------------------------
60 : * peak_vq_dec()
61 : *
62 : * Vector de-quantization of MDCT peaks
63 : *--------------------------------------------------------------------------*/
64 :
65 1229 : static void peak_vq_dec_fx(
66 : Decoder_State *st_fx, /* i/o: decoder state structure */
67 : Word32 *coefs_out, /* o : Output coefficient vector Q12 */
68 : const Word32 core_brate, /* i : Core bitrate */
69 : const Word16 num_bits, /* i : Number of bits for HVQ */
70 : const Word16 *ynrm, /* i : Envelope coefficients Q0 */
71 : Word16 *R, /* i/o: Bit allocation/updated bit allocation */
72 : Word16 *vq_peak_idx, /* o : Peak position vector */
73 : Word16 *Npeaks, /* o : Number of peaks */
74 : const Word16 core )
75 : {
76 : Word16 vq_peaks, i, j, k, FlagN, hcode_l, diff;
77 : Word16 bin_th, bin_th2, max_peaks, pvq_bands;
78 : Word16 nf_gains_idx[HVQ_NF_GROUPS], pgain_difidx[HVQ_MAX_PEAKS], pvq_norm[MAX_PVQ_BANDS];
79 : Word16 gain_bits_array[MAX_PVQ_BANDS];
80 : Word16 pos_bits;
81 : Word32 nf_gains_fx[HVQ_NF_GROUPS], peak_gains_fx[HVQ_MAX_PEAKS];
82 : Word16 pvq_vector[HVQ_PVQ_BUF_LEN];
83 : Word16 res_vec[HVQ_THRES_BIN_32k];
84 : Word16 k_sort[HVQ_MAX_PVQ_WORDS];
85 : Word16 pvq_inp_vector[HVQ_PVQ_BUF_LEN], pvq_maxpulse[HVQ_MAX_PVQ_WORDS];
86 : Word16 npulses[MAX_PVQ_BANDS];
87 : Word16 pvq_bits, Rk[MAX_PVQ_BANDS];
88 : Word16 fg_pred[NB_SFM_MAX];
89 :
90 : Word32 *pCoefsOut;
91 : Word16 whiteNoise;
92 : UWord16 dontCare;
93 : Word32 acc;
94 : Word16 *pPvqVector;
95 : Word32 manE_peak, manPeakGains, manPkEnrg; /* Due to very wide dynamic range, use floating point format, i.e., (man, exp) */
96 : Word16 expE_peak, expPeakGains, expPkEnrg;
97 : Word16 *pSelBnds;
98 : Word16 sel_bnds[HVQ_NUM_SFM_24k];
99 : Word16 hvq_band_end[MAX_PVQ_BANDS];
100 : Word16 hvq_band_start[MAX_PVQ_BANDS];
101 : Word16 hvq_band_width[MAX_PVQ_BANDS];
102 : Word16 n_sel_bnds;
103 : Word32 normq;
104 : UWord32 lsb;
105 : Word32 tmp;
106 1229 : Word16 nf_seed = RANDOM_INITSEED;
107 1229 : move16();
108 :
109 1229 : set16_fx( gain_bits_array, 0, MAX_PVQ_BANDS );
110 1229 : set16_fx( pvq_vector, 0, HVQ_PVQ_BUF_LEN );
111 1229 : set16_fx( npulses, 0, MAX_PVQ_BANDS );
112 1229 : set16_fx( pvq_inp_vector, 0, HVQ_PVQ_BUF_LEN );
113 :
114 1229 : assert( ( core_brate > HQ_16k40 && core_brate <= HQ_48k ) && "HVQ rate not supported" );
115 : // PMT("max_peaks equation needs to be converted")
116 1229 : max_peaks = extract_l( Mpy_32_32( ( L_add( imult3216( core_brate, HVQ_PEAKS_PER_DELTA ), HVQ_PEAKS_PER_DELTA_OFFS ) ), 282564 ) ); /* 1 / HVQ_PEAKS_BPS_DELTA in Q31 = 282564*/
117 1229 : bin_th = HVQ_THRES_BIN_24k;
118 1229 : move16();
119 1229 : bin_th2 = HVQ_THRES_BIN_24k / HVQ_NF_GROUPS;
120 1229 : move16();
121 1229 : IF( GE_32( core_brate, HQ_BWE_CROSSOVER_BRATE ) )
122 : {
123 521 : bin_th = HVQ_THRES_BIN_32k;
124 521 : move16();
125 521 : bin_th2 = HVQ_THRES_BIN_32k / HVQ_NF_GROUPS;
126 521 : move16();
127 : }
128 :
129 : /* Get number of peaks */
130 1229 : vq_peaks = get_next_indice_fx( st_fx, 5 );
131 1229 : vq_peaks = sub( max_peaks, vq_peaks );
132 1229 : *Npeaks = vq_peaks;
133 1229 : move16();
134 1229 : diff = 5;
135 1229 : move16();
136 :
137 : /* safety check in case of bit errors */
138 1229 : IF( LT_16( *Npeaks, HVQ_MIN_PEAKS ) )
139 : {
140 0 : st_fx->BER_detect = 1;
141 0 : move16();
142 0 : vq_peaks = HVQ_MIN_PEAKS;
143 0 : move16();
144 0 : *Npeaks = HVQ_MIN_PEAKS;
145 0 : move16();
146 : }
147 :
148 : /* De-quantize peak positions */
149 326541 : FOR( i = 0; i < bin_th; i++ )
150 : {
151 325312 : res_vec[i] = 0;
152 325312 : move16();
153 : }
154 :
155 : /* Unpack PVQ codewords */
156 1229 : pos_bits = hvq_dec_pos_fx( st_fx, res_vec, bin_th, vq_peaks );
157 1229 : diff = add( diff, pos_bits );
158 :
159 1229 : j = 0;
160 1229 : move16();
161 1229 : test();
162 314529 : FOR( i = 0; i < bin_th && j < vq_peaks; i++ ) /* safety check in case of bit errors */
163 : {
164 313300 : test();
165 313300 : IF( res_vec[i] != 0 )
166 : {
167 21092 : vq_peak_idx[j] = i;
168 21092 : move16();
169 21092 : j = add( j, 1 );
170 : }
171 : }
172 :
173 : /* safety check in case of bit errors */
174 1229 : IF( LT_16( j, vq_peaks ) )
175 : {
176 0 : st_fx->BER_detect = 1;
177 0 : move16();
178 0 : vq_peaks = sub( j, 1 );
179 0 : *Npeaks = sub( j, 1 );
180 0 : move16();
181 : }
182 :
183 : /* Huffman or differential coding */
184 1229 : FlagN = (Word16) get_next_indice_fx( st_fx, 1 );
185 :
186 : /* De-quantize peak gains */
187 1229 : pgain_difidx[0] = get_next_indice_fx( st_fx, GAIN0_BITS );
188 1229 : move16();
189 :
190 : /* safety check in case of bit errors */
191 1229 : IF( GT_16( pgain_difidx[0], 44 ) )
192 : {
193 0 : st_fx->BER_detect = 1;
194 0 : move16();
195 0 : pgain_difidx[0] = 44;
196 0 : move16();
197 : }
198 1229 : peak_gains_fx[0] = dicn_pg_fx[pgain_difidx[0]]; /* Q12 */
199 1229 : move32();
200 1229 : if ( res_vec[vq_peak_idx[0]] < 0 )
201 : {
202 593 : peak_gains_fx[0] = L_negate( peak_gains_fx[0] );
203 593 : move16();
204 : }
205 :
206 1229 : hcode_l = 0;
207 1229 : move16();
208 1229 : IF( FlagN )
209 : {
210 1207 : huff_dec_fx( st_fx, sub( vq_peaks, 1 ), MAX_PG_HUFFLEN, NUM_PG_HUFFLEN, hvq_pg_huff_thres, hvq_pg_huff_offset, hvq_pg_huff_tab, &pgain_difidx[1] );
211 :
212 20753 : FOR( i = 1; i < vq_peaks; i++ )
213 : {
214 19546 : hcode_l = add( hcode_l, pgain_huffsizn[pgain_difidx[i]] );
215 19546 : move16(); /* indirect addressing*/
216 : }
217 : }
218 : ELSE
219 : {
220 339 : FOR( i = 1; i < vq_peaks; i++ )
221 : {
222 317 : pgain_difidx[i] = get_next_indice_fx( st_fx, GAINI_BITS );
223 317 : move16();
224 317 : hcode_l = add( hcode_l, GAINI_BITS );
225 : }
226 : }
227 :
228 21092 : FOR( i = 1; i < vq_peaks; i++ )
229 : {
230 19863 : pgain_difidx[i] = add( pgain_difidx[i], sub( pgain_difidx[i - 1], 15 ) );
231 19863 : move16();
232 :
233 : /* safety check in case of bit errors */
234 19863 : test();
235 19863 : IF( GT_16( pgain_difidx[i], 44 ) || pgain_difidx[i] < 0 )
236 : {
237 0 : st_fx->BER_detect = 1;
238 0 : move16();
239 0 : pgain_difidx[i] = 44;
240 0 : move16();
241 : }
242 :
243 19863 : peak_gains_fx[i] = dicn_pg_fx[pgain_difidx[i]]; // Q12
244 19863 : move32();
245 19863 : if ( res_vec[vq_peak_idx[i]] < 0 )
246 : {
247 10091 : peak_gains_fx[i] = L_negate( peak_gains_fx[i] );
248 10091 : move32();
249 : }
250 : }
251 :
252 : /* Scale up peak gains and accumulate peak energy */
253 1229 : manE_peak = L_deposit_l( 0 );
254 1229 : expE_peak = 32;
255 1229 : move16();
256 22321 : FOR( i = 0; i < vq_peaks; i++ )
257 : {
258 21092 : peak_gains_fx[i] = L_shl( peak_gains_fx[i], 2 ); // Q12 -> Q14
259 21092 : move32();
260 : /* Use floating point operation to deal with wide dynamic range.
261 : * 32-bit mantissa is used here. It should be even more accurate than
262 : * the floating-point reference code with 24-bit mantissa! */
263 21092 : tmp = L_shl( dicn_pg_fx[pgain_difidx[i]], 2 ); // Q14
264 21092 : expPeakGains = norm_l( tmp );
265 21092 : manPeakGains = L_shl( tmp, expPeakGains );
266 21092 : Mpy_32_32_ss( manPeakGains, manPeakGains, &manPkEnrg, &lsb ); /* peak_gains square */
267 21092 : expPkEnrg = shl( expPeakGains, 1 ); /* Multiply by 2 due to squaring. */
268 :
269 21092 : floating_point_add( &manE_peak, &expE_peak, manPkEnrg, expPkEnrg );
270 : }
271 : /* Number of bits used for peak gain quantization */
272 1229 : diff = add( diff, add( FLAGN_BITS + GAIN0_BITS, hcode_l ) );
273 :
274 : /* De-quantize peaks */
275 22321 : FOR( i = 0; i < vq_peaks; i++ )
276 : {
277 21092 : dequant_peaks_fx( st_fx, &coefs_out[vq_peak_idx[i] - 2], &peak_gains_fx[i] ); /* coefs_out in Q12, peak_gains_fx in Q14 */
278 21092 : diff = add( diff, 9 );
279 : }
280 :
281 3687 : FOR( i = 0; i < HVQ_NF_GROUPS; i++ )
282 : {
283 2458 : nf_gains_idx[i] = get_next_indice_fx( st_fx, 5 );
284 2458 : move16();
285 2458 : nf_gains_fx[i] = L_shr( dicn_fx[nf_gains_idx[i]], 1 );
286 2458 : move32(); /* nf_gains in Q14 */
287 2458 : diff = add( diff, 5 );
288 : }
289 1229 : pvq_bits = sub( num_bits, diff );
290 :
291 : /* Calculate number of PVQ bands to code and assign bits */
292 1229 : pvq_bands = hvq_pvq_bitalloc_fx( pvq_bits, core_brate, st_fx->bwidth, ynrm, manE_peak, expE_peak, Rk, R, sel_bnds,
293 : &n_sel_bnds );
294 :
295 : /* safety check in case of bit errors */
296 1229 : test();
297 1229 : if ( pvq_bands == 0 && st_fx->element_mode == EVS_MONO ) /* PVQ bands may be zero for IVAS */
298 : {
299 0 : st_fx->BER_detect = 1;
300 0 : move16();
301 : }
302 :
303 1229 : pvq_bits = sub( pvq_bits, i_mult2( HVQ_PVQ_GAIN_BITS, pvq_bands ) );
304 : /* Get band limits for concatenated PVQ target */
305 1229 : hvq_concat_bands_fx( pvq_bands, sel_bnds, n_sel_bnds, hvq_band_start,
306 : hvq_band_width, hvq_band_end );
307 :
308 3664 : FOR( k = 0; k < pvq_bands; k++ )
309 : {
310 2435 : k_sort[k] = k;
311 2435 : move16();
312 : }
313 :
314 1229 : pvq_decode_frame_fx( st_fx, pvq_vector, npulses, pvq_inp_vector, hvq_band_start, hvq_band_end, hvq_band_width, pvq_bands, Rk, pvq_bits, core );
315 :
316 :
317 1229 : fine_gain_pred_fx( hvq_band_start, hvq_band_end, hvq_band_width, k_sort, npulses, pvq_maxpulse, NULL,
318 : pvq_bands, pvq_vector, pvq_inp_vector, fg_pred, core );
319 :
320 1229 : fine_gain_dec_fx( st_fx, k_sort, pvq_bands, gain_bits_array, fg_pred );
321 :
322 1229 : apply_gain_fx( k_sort, hvq_band_start, hvq_band_end, pvq_bands, fg_pred, pvq_vector );
323 :
324 1229 : pPvqVector = pvq_vector;
325 1229 : pCoefsOut = coefs_out;
326 1229 : pSelBnds = sel_bnds;
327 3664 : FOR( k = 0; k < pvq_bands; k++ )
328 : {
329 2435 : pvq_norm[k] = add( get_next_indice_fx( st_fx, HVQ_PVQ_GAIN_BITS ), 8 );
330 : // pvq_norm[k] = add( pvq_norm[k], 8 );
331 2435 : move16();
332 :
333 2435 : diff = add( diff, HVQ_PVQ_GAIN_BITS );
334 :
335 2435 : j = 0;
336 2435 : move16();
337 2435 : IF( GE_16( k, sub( pvq_bands, n_sel_bnds ) ) )
338 : {
339 325 : i = band_start_harm[*pSelBnds++];
340 325 : move16();
341 325 : pCoefsOut = coefs_out + i;
342 : }
343 2435 : normq = L_add( dicn_fx[pvq_norm[k]], 0 );
344 98200 : WHILE( LT_16( j, hvq_band_width[k] ) )
345 : {
346 95765 : IF( *pCoefsOut == 0 )
347 : {
348 64448 : Mpy_32_16_ss( normq, *pPvqVector++, &acc, &dontCare ); /* acc(Q11), normq(Q14), pvq_vector(Q12) */
349 64448 : *pCoefsOut = L_shl( acc, 12 - 11 ); /* Q12 */
350 64448 : move32();
351 64448 : j = add( j, 1 );
352 : }
353 95765 : pCoefsOut++;
354 : }
355 : }
356 :
357 : /* Noise fill unqantized coeffs with one gain per group */
358 1229 : pCoefsOut = &coefs_out[-1];
359 3687 : FOR( i = 0; i < HVQ_NF_GROUPS; i++ )
360 : {
361 327770 : FOR( j = 0; j < bin_th2; j++ )
362 : {
363 325312 : IF( *( ++pCoefsOut ) == 0 )
364 : {
365 188128 : whiteNoise = Random( &nf_seed ); /* Q15 */
366 188128 : Mpy_32_16_ss( nf_gains_fx[i], whiteNoise, &acc, &dontCare ); /* nf_gains_fx[] in Q14 */
367 188128 : *pCoefsOut = L_shr( acc, 14 - 12 ); /* Q12 */
368 188128 : move32();
369 : }
370 : }
371 : }
372 :
373 1229 : return;
374 : }
375 :
376 : /*--------------------------------------------------------------------------
377 : * dequant_peaks()
378 : *
379 : * Reads codebook vector and scales peak
380 : *--------------------------------------------------------------------------*/
381 :
382 21092 : static void dequant_peaks_fx(
383 : Decoder_State *st_fx, /* i/o: decoder state structure */
384 : Word32 *vect_out, /* o : Quantized vector in Q12 */
385 : const Word32 *peak_gain /* i : Peak gain in Q12 */
386 : )
387 : {
388 : Word16 xq[4];
389 : const Word16 *tmp;
390 : Word16 i, hvq_cb_rev;
391 : Word16 cb_idx, indx;
392 : Word32 absPeakGain1, absPeakGain;
393 : UWord16 dontCare;
394 :
395 21092 : hvq_cb_rev = get_next_indice_fx( st_fx, 1 );
396 21092 : cb_idx = get_next_indice_fx( st_fx, 8 );
397 :
398 21092 : indx = shl( cb_idx, 2 );
399 21092 : IF( hvq_cb_rev )
400 : {
401 10443 : indx = add( indx, 3 );
402 10443 : tmp = &hvq_peak_cb_fx[indx];
403 52215 : FOR( i = 0; i < 4; i++ )
404 : {
405 41772 : xq[i] = *tmp--; /* Q15 */
406 41772 : move16();
407 : }
408 : }
409 : ELSE
410 : {
411 10649 : tmp = &hvq_peak_cb_fx[indx];
412 53245 : FOR( i = 0; i < 4; i++ )
413 : {
414 42596 : xq[i] = *tmp++; /* Q15 */
415 42596 : move16();
416 : }
417 : }
418 :
419 21092 : absPeakGain = L_abs( peak_gain[0] );
420 :
421 21092 : IF( vect_out[0] == 0 )
422 : {
423 17166 : Mpy_32_16_ss( *peak_gain, xq[0], &vect_out[0], &dontCare ); /* vect_out in Q12 */
424 17166 : Mpy_32_16_ss( *peak_gain, xq[1], &vect_out[1], &dontCare ); /* Q12 */
425 : }
426 : ELSE
427 : {
428 3926 : absPeakGain1 = L_abs( peak_gain[-1] );
429 3926 : IF( LE_32( absPeakGain1, absPeakGain ) )
430 : {
431 2489 : Mpy_32_16_ss( *peak_gain, xq[0], &vect_out[0], &dontCare ); /* vect_out in Q12 */
432 2489 : Mpy_32_16_ss( *peak_gain, xq[1], &vect_out[1], &dontCare ); /* Q12 */
433 : }
434 : ELSE
435 : {
436 1437 : test();
437 1437 : IF( vect_out[1] == 0 || ( LE_32( absPeakGain1, absPeakGain ) ) )
438 : {
439 599 : Mpy_32_16_ss( *peak_gain, xq[1], &vect_out[1], &dontCare );
440 : }
441 : }
442 : }
443 21092 : vect_out[2] = *peak_gain; /* vect_out in Q12 */
444 21092 : move16();
445 21092 : Mpy_32_16_ss( *peak_gain, xq[2], &vect_out[3], &dontCare );
446 21092 : Mpy_32_16_ss( *peak_gain, xq[3], &vect_out[4], &dontCare );
447 :
448 21092 : return;
449 : }
450 :
451 :
452 : /*--------------------------------------------------------------------------
453 : * hvq_dec_pos()
454 : *
455 : * HVQ decode peak positions
456 : *--------------------------------------------------------------------------*/
457 :
458 1229 : static Word16 hvq_dec_pos_fx(
459 : Decoder_State *st_fx, /* i/o: decoder state structure */
460 : Word16 *pos_vec, /* o : decoded peak positions */
461 : const Word16 length, /* i : length */
462 : const Word16 num_peaks /* i : number of peaks */
463 : )
464 : {
465 : Word16 peak_idx[HVQ_MAX_PEAKS];
466 : Word16 delta[HVQ_MAX_PEAKS];
467 : Word16 sign_vec[HVQ_MAX_PEAKS];
468 :
469 : Word16 mode;
470 : Word16 num_bits, tmp;
471 : Word16 i, j;
472 :
473 1229 : num_bits = 0;
474 1229 : move16();
475 1229 : set16_fx( pos_vec, 0, length );
476 :
477 1229 : mode = get_next_indice_fx( st_fx, 1 );
478 1229 : num_bits = add( num_bits, 1 );
479 :
480 1229 : IF( ( mode == HVQ_CP_DELTA ) )
481 : {
482 1074 : huff_dec_fx( st_fx, num_peaks, HVQ_CP_HUFF_MAX_CODE, HVQ_CP_HUFF_NUM_LEN, hvq_cp_huff_thres, hvq_cp_huff_offset, hvq_cp_huff_tab, delta );
483 :
484 19801 : FOR( i = 0; i < num_peaks; i++ )
485 : {
486 18727 : num_bits = add( num_bits, hvq_cp_huff_len[delta[i]] );
487 : }
488 :
489 1074 : peak_idx[0] = sub( delta[0], HVQ_CP_HUFF_OFFSET );
490 1074 : move16();
491 : /* safety check in case of bit errors */
492 1074 : IF( LT_16( peak_idx[0], 2 ) )
493 : {
494 0 : peak_idx[0] = 2;
495 0 : move16();
496 0 : st_fx->BER_detect = 1;
497 0 : move16();
498 : }
499 18727 : FOR( i = 1; i < num_peaks; i++ )
500 : {
501 17653 : peak_idx[i] = add( add( delta[i], peak_idx[i - 1] ), HVQ_CP_HUFF_OFFSET );
502 17653 : move16();
503 : /* safety check in case of bit errors */
504 17653 : IF( GE_16( peak_idx[i], HVQ_THRES_BIN_32k ) )
505 : {
506 0 : peak_idx[i] = HVQ_THRES_BIN_32k - 1;
507 0 : move16();
508 0 : st_fx->BER_detect = 1;
509 0 : move16();
510 : }
511 : }
512 :
513 19801 : FOR( i = 0; i < num_peaks; i++ )
514 : {
515 18727 : pos_vec[peak_idx[i]] = 1;
516 18727 : move16();
517 : }
518 : }
519 : ELSE
520 : {
521 155 : tmp = sparse_dec_pos_fx( st_fx, pos_vec, length );
522 155 : num_bits = add( num_bits, tmp );
523 : }
524 :
525 22321 : FOR( i = 0; i < num_peaks; i++ )
526 : {
527 21092 : IF( get_next_indice_1_fx( st_fx ) == 0 )
528 : {
529 10684 : sign_vec[i] = -1;
530 10684 : move16();
531 : }
532 : ELSE
533 : {
534 10408 : sign_vec[i] = 1;
535 10408 : move16();
536 : }
537 : }
538 1229 : num_bits = add( num_bits, num_peaks );
539 :
540 1229 : j = 0;
541 1229 : move16();
542 : /* safety check in case of bit errors */
543 314529 : FOR( i = 0; i < length && j < num_peaks; i++ )
544 : {
545 313300 : test();
546 313300 : IF( EQ_16( pos_vec[i], 1 ) )
547 : {
548 21092 : pos_vec[i] = i_mult2( pos_vec[i], sign_vec[j++] );
549 21092 : move16();
550 : }
551 : }
552 :
553 1229 : return num_bits;
554 : }
555 :
556 : /*--------------------------------------------------------------------------
557 : * sparse_dec_pos()
558 : *
559 : * Sparse decode positions
560 : *--------------------------------------------------------------------------*/
561 :
562 155 : static Word16 sparse_dec_pos_fx(
563 : Decoder_State *st_fx, /* i/o: decoder state structure */
564 : Word16 *out, /* o : decoded peak positions */
565 : const Word16 length /* i : length */
566 : )
567 : {
568 : Word16 layer2[HVQ_CP_L2_MAX];
569 : Word16 layer_length;
570 : Word16 i, j, tmp;
571 : Word16 bits;
572 : Word16 idx, val;
573 :
574 155 : set16_fx( layer2, 0, HVQ_CP_L2_MAX );
575 155 : set16_fx( out, 0, length );
576 155 : bits = 0;
577 155 : move16();
578 :
579 : /*layer_length = (short)((float)length/HVQ_CP_L1_LEN + 0.5); */
580 155 : layer_length = round_fx( L_mult0( length, 13107 ) ); /* 0+16-16, 13107 is 1/5 in Q16 */
581 :
582 7548 : FOR( i = 0; i < layer_length; i++ )
583 : {
584 7393 : layer2[i] = get_next_indice_1_fx( st_fx );
585 7393 : move16();
586 : }
587 155 : bits = add( bits, layer_length );
588 :
589 7548 : FOR( j = 0; j < layer_length; j++ )
590 : {
591 7393 : IF( EQ_16( layer2[j], 1 ) )
592 : {
593 2173 : idx = get_next_indice_fx( st_fx, HVQ_CP_MAP_IDX_LEN );
594 2173 : bits = add( bits, HVQ_CP_MAP_IDX_LEN );
595 :
596 2173 : val = hvq_cp_layer1_map5[idx];
597 2173 : move16();
598 2173 : test(); /* safety check in case of bit errors */
599 2173 : IF( j == 0 && GT_16( val, 4 ) ) /* out[0] and out[1] are invalid positions */
600 : {
601 0 : st_fx->BER_detect = 1;
602 0 : move16();
603 0 : val = 4;
604 0 : move16();
605 : }
606 2173 : tmp = i_mult2( j, HVQ_CP_L1_LEN );
607 13030 : FOR( i = ( s_min( ( j + 1 ) * HVQ_CP_L1_LEN, length ) - 1 );
608 : i >= tmp; i-- )
609 : {
610 10857 : out[i] = s_and( val, 1 );
611 10857 : move16();
612 10857 : val = lshr( val, 1 );
613 : }
614 : }
615 : }
616 :
617 155 : return bits;
618 : }
|