Line data Source code
1 : /******************************************************************************************************
2 :
3 : (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
4 : Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
5 : Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
6 : Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
7 : contributors to this repository. All Rights Reserved.
8 :
9 : This software is protected by copyright law and by international treaties.
10 : The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
11 : Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
12 : Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
13 : Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
14 : contributors to this repository retain full ownership rights in their respective contributions in
15 : the software. This notice grants no license of any kind, including but not limited to patent
16 : license, nor is any license granted by implication, estoppel or otherwise.
17 :
18 : Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
19 : contributions.
20 :
21 : This software is provided "AS IS", without any express or implied warranties. The software is in the
22 : development stage. It is intended exclusively for experts who have experience with such software and
23 : solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
24 : and fitness for a particular purpose are hereby disclaimed and excluded.
25 :
26 : Any dispute, controversy or claim arising under or in relation to providing this software shall be
27 : submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
28 : accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
29 : the United Nations Convention on Contracts on the International Sales of Goods.
30 :
31 : *******************************************************************************************************/
32 :
33 : /*====================================================================================
34 : EVS Codec 3GPP TS26.443 Nov 04, 2021. Version 12.14.0 / 13.10.0 / 14.6.0 / 15.4.0 / 16.3.0
35 : ====================================================================================*/
36 :
37 : #include <stdint.h>
38 : #include "options.h"
39 : #ifdef DEBUGGING
40 : #include "debug.h"
41 : #endif
42 : #include <math.h>
43 : #include "cnst.h"
44 : #include "prot.h"
45 : #include "rom_com.h"
46 : #include "wmc_auto.h"
47 : #include <assert.h>
48 :
49 : /*--------------------------------------------------------------------------
50 : * Local function prototypes
51 : *--------------------------------------------------------------------------*/
52 :
53 : static void quant_peaks( BSTR_ENC_HANDLE hBstr, const float *vect_in, float *vect_out, const float *peak_gain, int16_t *vq_idx, const int16_t overlap, const int32_t core_brate, const int16_t Npeaks );
54 :
55 : static int16_t hvq_code_pos( BSTR_ENC_HANDLE hBstr, const int16_t *const inp, const int16_t length, const int16_t num_peaks );
56 :
57 : static int16_t sparse_code_pos( const int16_t *inp, const int16_t length, int16_t *result );
58 :
59 :
60 : /*--------------------------------------------------------------------------
61 : * peak_vq_enc()
62 : *
63 : * Vector Quantization of MDCT peaks
64 : *--------------------------------------------------------------------------*/
65 :
66 1528 : int16_t peak_vq_enc(
67 : BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle */
68 : const int16_t bwidth, /* i : audio bandwidth */
69 : #ifdef DEBUGGING
70 : const int16_t idchan, /* i : channel ID */
71 : #endif
72 : const float *coefs, /* i : Input coefficient vector */
73 : float *coefs_out, /* o : Quantized output vector */
74 : const int32_t core_brate, /* i : Core bitrate */
75 : const int16_t num_bits, /* i : Number of bits for HVQ */
76 : const int16_t vq_peaks, /* i : Number of identified peaks */
77 : const int16_t *ynrm, /* i : Envelope coefficients */
78 : int16_t *R, /* i/o: Bit allocation/updated bit allocation */
79 : int16_t *vq_peak_idx, /* i : Peak index vector */
80 : float *nf_gains /* i : Estimated noise floor gains */
81 : )
82 : {
83 : int16_t pos_bits;
84 : float normq;
85 : float pgain_q[HVQ_MAX_PEAKS];
86 : float peak_gains[HVQ_MAX_PEAKS];
87 : float coefs_pvq[HVQ_PVQ_BUF_LEN];
88 : float pvq_vector[HVQ_PVQ_BUF_LEN];
89 : float *pPvqVectorBandStart;
90 : float fg_pred[NB_SFM_MAX];
91 : int16_t i, j, k, m, r, pvq_bands, num_overlap_bins;
92 : int16_t hcode_l, FlagN, low_peak_bin, vq_cb_idx, max_peaks, bin_th;
93 1528 : int16_t bits = 0;
94 : int16_t q_nf_gain_idx[HVQ_NF_GROUPS];
95 1528 : int16_t nf_seed = RANDOM_INITSEED;
96 : int16_t pgain_cb_idx[HVQ_MAX_PEAKS], pgain_difidx[HVQ_MAX_PEAKS];
97 : int16_t pvq_norm[MAX_PVQ_BANDS];
98 : int16_t pvq_bits, bit_budget;
99 : int16_t pos_vec[HVQ_THRES_BIN_32k];
100 : int16_t npulses[MAX_PVQ_BANDS];
101 : int16_t pvq_inp_vector[HVQ_PVQ_BUF_LEN];
102 : int16_t k_sort[MAX_PVQ_BANDS];
103 : int16_t Rk[MAX_PVQ_BANDS];
104 : int16_t Rk_f[MAX_PVQ_BANDS]; /*Q3*/
105 : float gopt[NB_SFM];
106 : int16_t sel_bnds[HVQ_NUM_SFM_24k];
107 : int16_t n_sel_bnds;
108 : int32_t manE_peak, manPkEnrg;
109 : int16_t expE_peak, expPkEnrg;
110 : int16_t hvq_band_end[MAX_PVQ_BANDS];
111 : int16_t hvq_band_start[MAX_PVQ_BANDS];
112 : int16_t hvq_band_width[MAX_PVQ_BANDS];
113 : int16_t n;
114 : int16_t s;
115 1528 : set_f( coefs_pvq, 0.0f, HVQ_PVQ_BUF_LEN );
116 1528 : set_f( pvq_vector, 0.0f, HVQ_PVQ_BUF_LEN );
117 1528 : set_s( npulses, 0, MAX_PVQ_BANDS );
118 :
119 : /* Set bitrate dependent variables */
120 1528 : assert( ( core_brate > HQ_16k40 && core_brate <= HQ_48k ) && "HVQ rate not supported" );
121 1528 : max_peaks = (int16_t) ( ( core_brate * HVQ_PEAKS_PER_DELTA + HVQ_PEAKS_PER_DELTA_OFFS ) / HVQ_PEAKS_BPS_DELTA );
122 1528 : bin_th = HVQ_THRES_BIN_24k;
123 1528 : if ( core_brate >= HQ_BWE_CROSSOVER_BRATE )
124 : {
125 559 : bin_th = HVQ_THRES_BIN_32k;
126 : }
127 :
128 397464 : for ( i = 0; i < bin_th; i++ )
129 : {
130 395936 : pos_vec[i] = 0;
131 : }
132 :
133 : /* Quantize noise floor gains */
134 4584 : for ( i = 0; i < HVQ_NF_GROUPS; i++ )
135 : {
136 3056 : logqnorm( &nf_gains[i], &q_nf_gain_idx[i], 32, 1, &thren_HQ[0] );
137 3056 : nf_gains[i] = 0.5f * dicn[q_nf_gain_idx[i]];
138 3056 : push_indice( hBstr, IND_HVQ_NF_GAIN, q_nf_gain_idx[i], 5 );
139 3056 : bits += 5;
140 : }
141 :
142 : /* Signal number of peaks */
143 1528 : i = max_peaks - vq_peaks;
144 1528 : push_indice( hBstr, IND_NUM_PEAKS, i, 5 );
145 1528 : bits += 5;
146 :
147 : /* Identify position of first peak and arrange peak gains by position */
148 1528 : low_peak_bin = bin_th;
149 26988 : for ( i = 0; i < vq_peaks; i++ )
150 : {
151 25460 : if ( vq_peak_idx[i] < low_peak_bin )
152 : {
153 4286 : low_peak_bin = vq_peak_idx[i];
154 : }
155 25460 : pos_vec[vq_peak_idx[i]] = (int16_t) sign( (float) coefs[vq_peak_idx[i]] );
156 : }
157 :
158 397464 : for ( i = 0, j = 0; i < bin_th; i++ )
159 : {
160 395936 : if ( pos_vec[i] != 0 )
161 : {
162 25460 : peak_gains[j] = (float) fabs( coefs[i] );
163 25460 : vq_peak_idx[j] = i;
164 25460 : j++;
165 : }
166 : }
167 :
168 : /* Scale down peak gains */
169 26988 : for ( i = 0; i < vq_peaks; i++ )
170 : {
171 25460 : peak_gains[i] *= 0.25f;
172 : }
173 :
174 : /* Quantize peak gains */
175 1528 : logqnorm( &peak_gains[0], &pgain_cb_idx[0], 32, 1, &thren_pg[0] );
176 25460 : for ( i = 1; i < vq_peaks; i++ )
177 : {
178 23932 : logqnorm( &peak_gains[i], &pgain_cb_idx[i], 45, 1, &thren_pg[0] );
179 : }
180 :
181 : /* Code quantized peak gain indices */
182 1528 : diffcod( vq_peaks, pgain_cb_idx, &pgain_difidx[1] );
183 26988 : for ( i = 0; i < vq_peaks; i++ )
184 : {
185 25460 : pgain_q[i] = dicn_pg[pgain_cb_idx[i]];
186 : }
187 1528 : pgain_difidx[0] = pgain_cb_idx[0];
188 :
189 : /* Scale up peak gains and accumulate peak energy */
190 1528 : manE_peak = 0;
191 1528 : expE_peak = 32;
192 26988 : for ( i = 0; i < vq_peaks; i++ )
193 : {
194 25460 : pgain_q[i] *= 4.0f;
195 25460 : manPkEnrg = manPkEnrg_tbl[pgain_cb_idx[i]];
196 25460 : expPkEnrg = expPkEnrg_tbl[pgain_cb_idx[i]];
197 25460 : floating_point_add( &manE_peak, &expE_peak, manPkEnrg, expPkEnrg );
198 : }
199 :
200 : /* Huffman coding */
201 1528 : hcode_l = 0;
202 25460 : for ( i = 1; i < vq_peaks; i++ )
203 : {
204 23932 : hcode_l += pgain_huffsizn[pgain_difidx[i]];
205 : }
206 :
207 1528 : FlagN = HUFCODE;
208 :
209 1528 : if ( hcode_l >= GAINI_BITS * ( vq_peaks - 1 ) )
210 : {
211 29 : hcode_l = GAINI_BITS * ( vq_peaks - 1 );
212 29 : FlagN = NOHUFCODE;
213 : }
214 :
215 1528 : push_indice( hBstr, IND_FLAGN, FlagN, 1 );
216 1528 : push_indice( hBstr, IND_PG_IDX, pgain_difidx[0], GAIN0_BITS );
217 :
218 1528 : if ( FlagN )
219 : {
220 25022 : for ( i = 1; i < vq_peaks; i++ )
221 : {
222 23523 : j = pgain_difidx[i];
223 23523 : m = pgain_huffnorm[j];
224 23523 : r = pgain_huffsizn[j];
225 :
226 23523 : push_indice( hBstr, IND_PG_IDX, m, r );
227 : }
228 : }
229 : else
230 : {
231 438 : for ( i = 1; i < vq_peaks; i++ )
232 : {
233 409 : push_indice( hBstr, IND_PG_IDX, pgain_difidx[i], GAINI_BITS );
234 : }
235 : }
236 :
237 : /* Number of bits used for peak gain quantization */
238 1528 : bits += FLAGN_BITS + GAIN0_BITS + hcode_l;
239 :
240 : /* Add sign for peak shape normalization */
241 26988 : for ( i = 0; i < vq_peaks; i++ )
242 : {
243 25460 : peak_gains[i] = pos_vec[vq_peak_idx[i]] * pgain_q[i];
244 : }
245 :
246 : /* Quantize peak shapes */
247 25460 : for ( i = 0; i < vq_peaks - 1; i++ )
248 : {
249 23932 : num_overlap_bins = 5 - ( vq_peak_idx[i + 1] - vq_peak_idx[i] );
250 23932 : quant_peaks( hBstr, &coefs[vq_peak_idx[i] - 2], &coefs_out[vq_peak_idx[i] - 2], &peak_gains[i], &vq_cb_idx, num_overlap_bins, core_brate, vq_peaks );
251 23932 : push_indice( hBstr, IND_HVQ_PEAKS, vq_cb_idx, 8 );
252 23932 : bits += 9;
253 : }
254 :
255 1528 : quant_peaks( hBstr, &coefs[vq_peak_idx[i] - 2], &coefs_out[vq_peak_idx[i] - 2], &peak_gains[i], &vq_cb_idx, 0, core_brate, vq_peaks );
256 1528 : push_indice( hBstr, IND_HVQ_PEAKS, vq_cb_idx, 8 );
257 1528 : bits += 9;
258 :
259 1528 : pos_bits = hvq_code_pos( hBstr, pos_vec, bin_th, vq_peaks );
260 :
261 1528 : bits += pos_bits;
262 1528 : bit_budget = num_bits - bits;
263 :
264 : /* Calculate number of PVQ bands to code and assign bits */
265 1528 : pvq_bands = hvq_pvq_bitalloc( bit_budget, core_brate, bwidth, ynrm, manE_peak, expE_peak, Rk, R, sel_bnds, &n_sel_bnds );
266 :
267 : /* Get band limits for concatenated PVQ target */
268 1528 : hvq_concat_bands( pvq_bands, sel_bnds, n_sel_bnds, hvq_band_start, hvq_band_width, hvq_band_end );
269 :
270 : /* Quantize PVQ bands */
271 1528 : i = 0;
272 1528 : n = 0;
273 1528 : s = 0;
274 4339 : for ( k = 0; k < pvq_bands; k++ )
275 : {
276 2811 : if ( k >= pvq_bands - n_sel_bnds )
277 : {
278 360 : i = band_start_harm[sel_bnds[s]];
279 360 : s++;
280 : }
281 2811 : k_sort[k] = k;
282 2811 : j = 0;
283 2811 : pPvqVectorBandStart = &pvq_vector[n];
284 115030 : while ( j < hvq_band_width[k] )
285 : {
286 112219 : if ( coefs_out[i] == 0 )
287 : {
288 74184 : pvq_vector[n] = coefs[i];
289 74184 : j++;
290 74184 : n++;
291 : }
292 112219 : i++;
293 : }
294 2811 : logqnorm( pPvqVectorBandStart, &pvq_norm[k], 40, hvq_band_width[k], &thren_HQ[0] );
295 : }
296 :
297 1528 : normalizecoefs( pvq_vector, pvq_norm, pvq_bands, hvq_band_start, hvq_band_end );
298 :
299 1528 : bit_budget -= HVQ_PVQ_GAIN_BITS * pvq_bands;
300 4339 : for ( k = 0; k < pvq_bands; k++ )
301 : {
302 2811 : Rk_f[k] = Rk[k] * 8;
303 : }
304 :
305 1528 : pvq_bits = bit_budget;
306 1528 : set_s( npulses, 0, MAX_PVQ_BANDS );
307 :
308 1528 : pvq_encode_frame( hBstr, pvq_vector, coefs_pvq, gopt, npulses, pvq_inp_vector, hvq_band_start, hvq_band_end, hvq_band_width, pvq_bands, Rk_f, pvq_bits, HQ_CORE );
309 :
310 4339 : for ( i = 0; i < pvq_bands; i++ )
311 : {
312 2811 : k_sort[i] = i;
313 : }
314 :
315 : #ifdef DEBUGGING
316 : /* SNR measurement */
317 : for ( i = 0; i < pvq_bands; i++ )
318 : {
319 : float diff[120];
320 :
321 : if ( npulses[i] > 0 )
322 : {
323 : for ( j = 0; j < hvq_band_width[i]; j++ )
324 : {
325 : diff[j] = pvq_vector[hvq_band_start[i] + j] - gopt[i] * coefs_pvq[hvq_band_start[i] + j];
326 : }
327 :
328 : if ( idchan == 0 )
329 : {
330 : snr( &pvq_vector[hvq_band_start[i]], diff, hvq_band_width[i], "HVQ_PVQ_output" );
331 : }
332 : else
333 : {
334 : snr( &pvq_vector[hvq_band_start[i]], diff, hvq_band_width[i], "HVQ_PVQ_output_chan2" );
335 : }
336 : }
337 : }
338 : #endif
339 :
340 1528 : fine_gain_pred( hvq_band_start, hvq_band_end, hvq_band_width, k_sort, npulses, NULL, NULL, pvq_bands, coefs_pvq, pvq_inp_vector, fg_pred, HQ_CORE );
341 :
342 1528 : i = 0;
343 1528 : n = 0;
344 1528 : s = 0;
345 4339 : for ( k = 0; k < pvq_bands; k++ )
346 : {
347 2811 : normq = dicn[pvq_norm[k]] * ( gopt[k] / fg_pred[k] );
348 :
349 2811 : logqnorm( &normq, &pvq_norm[k], 40, 1, &thren_HQ[0] );
350 2811 : pvq_norm[k] -= 8;
351 2811 : if ( pvq_norm[k] < 0 )
352 : {
353 0 : pvq_norm[k] = 0;
354 : }
355 :
356 2811 : push_indice( hBstr, IND_HVQ_PVQ_GAIN, pvq_norm[k], HVQ_PVQ_GAIN_BITS );
357 2811 : pvq_bits += HVQ_PVQ_GAIN_BITS;
358 :
359 2811 : pvq_norm[k] += 8;
360 2811 : j = 0;
361 2811 : if ( k >= pvq_bands - n_sel_bnds )
362 : {
363 360 : i = band_start_harm[sel_bnds[s++]];
364 : }
365 115030 : while ( j < hvq_band_width[k] )
366 : {
367 112219 : normq = dicn[pvq_norm[k]];
368 112219 : if ( coefs_out[i] == 0 )
369 : {
370 74184 : coefs_out[i] = coefs_pvq[n] * fg_pred[k];
371 74184 : coefs_out[i] = coefs_out[i] * normq;
372 74184 : j++;
373 74184 : n++;
374 : }
375 112219 : i++;
376 : }
377 : }
378 :
379 1528 : bits += pvq_bits;
380 :
381 : /* Noise fill unquantized coeffs with one gain per group */
382 4584 : for ( i = 0; i < HVQ_NF_GROUPS; i++ )
383 : {
384 398992 : for ( j = i * ( bin_th / HVQ_NF_GROUPS ); j < ( i + 1 ) * ( bin_th / HVQ_NF_GROUPS ); j++ )
385 : {
386 395936 : if ( coefs_out[j] == 0 )
387 : {
388 232987 : coefs_out[j] = ( (float) own_random( &nf_seed ) / MAX16B ) * nf_gains[i];
389 : }
390 : }
391 : }
392 :
393 1528 : return bits;
394 : }
395 :
396 : /*--------------------------------------------------------------------------
397 : * quant_peaks()
398 : *
399 : * Applies VQ on input vector
400 : *--------------------------------------------------------------------------*/
401 :
402 25460 : static void quant_peaks(
403 : BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle */
404 : const float *vect_in, /* i : Target vector */
405 : float *vect_out, /* i/o: Quantized vector */
406 : const float *peak_gain, /* i : Peak gain vector */
407 : int16_t *vq_idx, /* o : Codebook index */
408 : const int16_t overlap, /* i : Overlap indicator */
409 : const int32_t core_brate, /* i : Core bitrate */
410 : const int16_t Npeaks /* i : Number of peaks */
411 : )
412 : {
413 : float x[4];
414 : float xq[4];
415 : int16_t weights[4];
416 : int16_t i, idx, cb_class, search_overlap;
417 :
418 25460 : set_s( weights, 1, 4 );
419 :
420 25460 : x[0] = vect_in[0] / ( *peak_gain );
421 25460 : x[1] = vect_in[1] / ( *peak_gain );
422 25460 : x[2] = vect_in[3] / ( *peak_gain );
423 25460 : x[3] = vect_in[4] / ( *peak_gain );
424 :
425 25460 : if ( vect_out[0] != 0 )
426 : {
427 2461 : if ( fabs( peak_gain[-1] ) > fabs( *peak_gain ) )
428 : {
429 1738 : weights[0] = 0;
430 :
431 1738 : if ( vect_out[1] != 0 )
432 : {
433 997 : weights[1] = 0;
434 : }
435 : }
436 : }
437 :
438 25460 : if ( overlap > 0 )
439 : {
440 4731 : if ( fabs( peak_gain[1] ) > fabs( *peak_gain ) )
441 : {
442 6177 : for ( i = 3; i > 3 - overlap; i-- )
443 : {
444 3907 : weights[i] = 0;
445 : }
446 : }
447 : }
448 :
449 :
450 : /* Classify */
451 25460 : cb_class = (int16_t) w_vquant( x, 0, weights, 0, hvq_class_c, HVQ_VQ_DIM - 1, HVQ_NUM_CLASS, 0 );
452 :
453 25460 : if ( core_brate < HQ_BWE_CROSSOVER_BRATE )
454 : {
455 13850 : idx = max( 0, HVQ_MAX_PEAKS_24k - Npeaks );
456 13850 : search_overlap = hvq_cb_search_overlap24k[idx];
457 : }
458 : else
459 : {
460 11610 : idx = max( 0, HVQ_MAX_PEAKS_32k - Npeaks );
461 11610 : search_overlap = hvq_cb_search_overlap32k[idx];
462 : }
463 :
464 : /* Quantize */
465 25460 : if ( cb_class == 0 )
466 : {
467 6796 : *vq_idx = (int16_t) w_vquant( x, 0, weights, xq, hvq_peak_cb, 4, HVQ_CB_SIZE / 2 + search_overlap, 0 );
468 6796 : push_indice( hBstr, IND_HVQ_PEAKS, 0, 1 );
469 : }
470 18664 : else if ( cb_class == 1 )
471 : {
472 6113 : *vq_idx = (int16_t) w_vquant( x, 0, weights, xq, &hvq_peak_cb[HVQ_CB_SIZE * 2 - search_overlap * 4], 4, HVQ_CB_SIZE / 2 + search_overlap, 0 );
473 6113 : *vq_idx += HVQ_CB_SIZE / 2 - search_overlap;
474 6113 : push_indice( hBstr, IND_HVQ_PEAKS, 0, 1 );
475 : }
476 12551 : else if ( cb_class == 2 )
477 : {
478 5996 : *vq_idx = (int16_t) w_vquant( x, 0, weights, xq, &hvq_peak_cb[HVQ_CB_SIZE * 2 - search_overlap * 4], 4, HVQ_CB_SIZE / 2 + search_overlap, 1 );
479 5996 : *vq_idx += HVQ_CB_SIZE / 2 - search_overlap;
480 5996 : push_indice( hBstr, IND_HVQ_PEAKS, 1, 1 );
481 : }
482 : else
483 : {
484 6555 : *vq_idx = (int16_t) w_vquant( x, 0, weights, xq, hvq_peak_cb, 4, HVQ_CB_SIZE / 2 + search_overlap, 1 );
485 6555 : push_indice( hBstr, IND_HVQ_PEAKS, 1, 1 );
486 : }
487 :
488 25460 : vect_out[0] = weights[0] * ( xq[0] * ( *peak_gain ) ) + ( weights[0] ^ 1 ) * vect_out[0];
489 25460 : vect_out[1] = weights[1] * ( xq[1] * ( *peak_gain ) ) + ( weights[1] ^ 1 ) * vect_out[1];
490 25460 : vect_out[2] = *peak_gain;
491 25460 : vect_out[3] = weights[2] * ( xq[2] * ( *peak_gain ) ) + ( weights[2] ^ 1 ) * vect_out[3];
492 25460 : vect_out[4] = weights[3] * ( xq[3] * ( *peak_gain ) ) + ( weights[3] ^ 1 ) * vect_out[4];
493 :
494 25460 : return;
495 : }
496 :
497 : /*--------------------------------------------------------------------------
498 : * code_pos()
499 : *
500 : * Code pulse positions
501 : *--------------------------------------------------------------------------*/
502 :
503 : /*! r: number of consumed bits */
504 1528 : static int16_t sparse_code_pos(
505 : const int16_t *inp, /* i : positions to encode */
506 : const int16_t length, /* i : number of positions */
507 : int16_t *result /* o : sparse encoding */
508 : )
509 : {
510 : int16_t layer2[HVQ_CP_L2_MAX];
511 : int16_t layer_length;
512 : int16_t i, j;
513 : int16_t val, idx;
514 1528 : int16_t bits = 0;
515 : int16_t mask;
516 :
517 1528 : set_s( layer2, 0, HVQ_CP_L2_MAX );
518 :
519 1528 : layer_length = (int16_t) ( (float) length / HVQ_CP_L1_LEN + 0.5 );
520 :
521 80909 : for ( j = 0; j < layer_length; j++ )
522 : {
523 402339 : for ( i = j * HVQ_CP_L1_LEN; i < min( ( j + 1 ) * HVQ_CP_L1_LEN, length ); i++ )
524 : {
525 347078 : if ( inp[i] )
526 : {
527 24120 : layer2[j] = 1;
528 24120 : break;
529 : }
530 : }
531 : }
532 :
533 80909 : for ( i = 0; i < layer_length; i++ )
534 : {
535 79381 : result[i] = layer2[i];
536 : }
537 1528 : bits += layer_length;
538 :
539 80909 : for ( j = 0; j < layer_length; j++ )
540 : {
541 79381 : if ( layer2[j] )
542 : {
543 24120 : val = 0;
544 144702 : for ( i = j * HVQ_CP_L1_LEN; i < min( ( j + 1 ) * HVQ_CP_L1_LEN, length ); i++ )
545 : {
546 120582 : val <<= 1;
547 120582 : val |= inp[i];
548 : }
549 :
550 79017 : for ( idx = 0; idx < HVQ_CP_MAP_LEN; idx++ )
551 : {
552 79017 : if ( hvq_cp_layer1_map5[idx] == val )
553 : {
554 24120 : break;
555 : }
556 : }
557 :
558 24120 : mask = 1 << ( HVQ_CP_MAP_IDX_LEN - 1 );
559 96480 : for ( i = 0; i < HVQ_CP_MAP_IDX_LEN; i++ )
560 : {
561 72360 : result[bits++] = ( idx & mask ) >> ( HVQ_CP_MAP_IDX_LEN - 1 - i );
562 72360 : mask >>= 1;
563 : }
564 : }
565 : }
566 :
567 1528 : return bits;
568 : }
569 :
570 : /*--------------------------------------------------------------------------
571 : * hvq_code_pos()
572 : *
573 : * Code pulse positions
574 : *--------------------------------------------------------------------------*/
575 :
576 1528 : static int16_t hvq_code_pos(
577 : BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle */
578 : const int16_t *const inp, /* i : positions to encode */
579 : const int16_t length, /* i : number of positions */
580 : const int16_t num_peaks /* i : number of peaks */
581 : )
582 : {
583 : int16_t sparse_result[4 * HVQ_THRES_BIN_32k / HVQ_CP_L1_LEN];
584 : int16_t delta[HVQ_MAX_PEAKS];
585 : int16_t peak_idx[HVQ_MAX_PEAKS];
586 : int16_t inp_abs[HVQ_THRES_BIN_32k];
587 : int16_t inp_sign[HVQ_MAX_PEAKS];
588 : int16_t i, j;
589 : int16_t bits;
590 : int16_t delta_max;
591 : int16_t delta_bits, sparse_bits;
592 :
593 1528 : bits = 0;
594 :
595 : /* Extract sorted peak index vector and sign vector */
596 397464 : for ( i = 0, j = 0; i < length; i++ )
597 : {
598 395936 : inp_abs[i] = (int16_t) abs( inp[i] );
599 395936 : if ( inp[i] )
600 : {
601 25460 : peak_idx[j] = i;
602 25460 : inp_sign[j++] = inp[i];
603 : }
604 : }
605 :
606 : /* Calculate delta */
607 1528 : delta[0] = peak_idx[0] + HVQ_CP_HUFF_OFFSET;
608 1528 : delta_max = delta[0];
609 25460 : for ( i = 1; i < num_peaks; i++ )
610 : {
611 23932 : delta[i] = peak_idx[i] - peak_idx[i - 1] - HVQ_CP_HUFF_OFFSET;
612 23932 : if ( delta_max < delta[i] )
613 : {
614 3215 : delta_max = delta[i];
615 : }
616 : }
617 :
618 : /* Calculate bits needed for huffman coding of deltas */
619 1528 : delta_bits = -1;
620 1528 : if ( delta_max <= HVQ_CP_HUFF_MAX )
621 : {
622 1455 : delta_bits = 0;
623 25993 : for ( i = 0; i < num_peaks; i++ )
624 : {
625 24538 : delta_bits += hvq_cp_huff_len[delta[i]];
626 : }
627 : }
628 :
629 : /* Calculate bits neeed for sparse coding */
630 1528 : sparse_bits = sparse_code_pos( inp_abs, length, sparse_result );
631 :
632 : /* Decide which coding mode to use */
633 1528 : if ( delta_bits > sparse_bits || delta_bits < 0 )
634 : {
635 224 : push_indice( hBstr, IND_POS_IDX, HVQ_CP_SPARSE, 1 );
636 :
637 19904 : for ( i = 0; i < sparse_bits; i++ )
638 : {
639 19680 : push_indice( hBstr, IND_POS_IDX, sparse_result[i], 1 );
640 : }
641 224 : bits += sparse_bits + 1;
642 : }
643 : else
644 : {
645 1304 : push_indice( hBstr, IND_POS_IDX, HVQ_CP_DELTA, 1 );
646 :
647 23444 : for ( i = 0; i < num_peaks; i++ )
648 : {
649 22140 : j = delta[i];
650 22140 : push_indice( hBstr, IND_POS_IDX, hvq_cp_huff_val[j], hvq_cp_huff_len[j] );
651 : }
652 1304 : bits += delta_bits + 1;
653 : }
654 :
655 : /* Send sign */
656 26988 : for ( i = 0; i < num_peaks; i++ )
657 : {
658 25460 : push_indice( hBstr, IND_POS_IDX, ( inp_sign[i] < 0 ? 0 : 1 ), 1 );
659 : }
660 1528 : bits += num_peaks;
661 :
662 1528 : return bits;
663 : }
|