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 11407 : 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 11407 : int16_t bits = 0;
94 : int16_t q_nf_gain_idx[HVQ_NF_GROUPS];
95 11407 : 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 11407 : set_f( coefs_pvq, 0.0f, HVQ_PVQ_BUF_LEN );
116 11407 : set_f( pvq_vector, 0.0f, HVQ_PVQ_BUF_LEN );
117 11407 : set_s( npulses, 0, MAX_PVQ_BANDS );
118 :
119 : /* Set bitrate dependent variables */
120 11407 : assert( ( core_brate > HQ_16k40 && core_brate <= HQ_48k ) && "HVQ rate not supported" );
121 11407 : max_peaks = (int16_t) ( ( core_brate * HVQ_PEAKS_PER_DELTA + HVQ_PEAKS_PER_DELTA_OFFS ) / HVQ_PEAKS_BPS_DELTA );
122 11407 : bin_th = HVQ_THRES_BIN_24k;
123 11407 : if ( core_brate >= HQ_BWE_CROSSOVER_BRATE )
124 : {
125 2677 : bin_th = HVQ_THRES_BIN_32k;
126 : }
127 :
128 2823567 : for ( i = 0; i < bin_th; i++ )
129 : {
130 2812160 : pos_vec[i] = 0;
131 : }
132 :
133 : /* Quantize noise floor gains */
134 34221 : for ( i = 0; i < HVQ_NF_GROUPS; i++ )
135 : {
136 22814 : logqnorm( &nf_gains[i], &q_nf_gain_idx[i], 32, 1, &thren_HQ[0] );
137 22814 : nf_gains[i] = 0.5f * dicn[q_nf_gain_idx[i]];
138 22814 : push_indice( hBstr, IND_HVQ_NF_GAIN, q_nf_gain_idx[i], 5 );
139 22814 : bits += 5;
140 : }
141 :
142 : /* Signal number of peaks */
143 11407 : i = max_peaks - vq_peaks;
144 11407 : push_indice( hBstr, IND_NUM_PEAKS, i, 5 );
145 11407 : bits += 5;
146 :
147 : /* Identify position of first peak and arrange peak gains by position */
148 11407 : low_peak_bin = bin_th;
149 187220 : for ( i = 0; i < vq_peaks; i++ )
150 : {
151 175813 : if ( vq_peak_idx[i] < low_peak_bin )
152 : {
153 33303 : low_peak_bin = vq_peak_idx[i];
154 : }
155 175813 : pos_vec[vq_peak_idx[i]] = (int16_t) sign( (float) coefs[vq_peak_idx[i]] );
156 : }
157 :
158 2823567 : for ( i = 0, j = 0; i < bin_th; i++ )
159 : {
160 2812160 : if ( pos_vec[i] != 0 )
161 : {
162 175813 : peak_gains[j] = (float) fabs( coefs[i] );
163 175813 : vq_peak_idx[j] = i;
164 175813 : j++;
165 : }
166 : }
167 :
168 : /* Scale down peak gains */
169 187220 : for ( i = 0; i < vq_peaks; i++ )
170 : {
171 175813 : peak_gains[i] *= 0.25f;
172 : }
173 :
174 : /* Quantize peak gains */
175 11407 : logqnorm( &peak_gains[0], &pgain_cb_idx[0], 32, 1, &thren_pg[0] );
176 175813 : for ( i = 1; i < vq_peaks; i++ )
177 : {
178 164406 : logqnorm( &peak_gains[i], &pgain_cb_idx[i], 45, 1, &thren_pg[0] );
179 : }
180 :
181 : /* Code quantized peak gain indices */
182 11407 : diffcod( vq_peaks, pgain_cb_idx, &pgain_difidx[1] );
183 187220 : for ( i = 0; i < vq_peaks; i++ )
184 : {
185 175813 : pgain_q[i] = dicn_pg[pgain_cb_idx[i]];
186 : }
187 11407 : pgain_difidx[0] = pgain_cb_idx[0];
188 :
189 : /* Scale up peak gains and accumulate peak energy */
190 11407 : manE_peak = 0;
191 11407 : expE_peak = 32;
192 187220 : for ( i = 0; i < vq_peaks; i++ )
193 : {
194 175813 : pgain_q[i] *= 4.0f;
195 175813 : manPkEnrg = manPkEnrg_tbl[pgain_cb_idx[i]];
196 175813 : expPkEnrg = expPkEnrg_tbl[pgain_cb_idx[i]];
197 175813 : floating_point_add( &manE_peak, &expE_peak, manPkEnrg, expPkEnrg );
198 : }
199 :
200 : /* Huffman coding */
201 11407 : hcode_l = 0;
202 175813 : for ( i = 1; i < vq_peaks; i++ )
203 : {
204 164406 : hcode_l += pgain_huffsizn[pgain_difidx[i]];
205 : }
206 :
207 11407 : FlagN = HUFCODE;
208 :
209 11407 : if ( hcode_l >= GAINI_BITS * ( vq_peaks - 1 ) )
210 : {
211 62 : hcode_l = GAINI_BITS * ( vq_peaks - 1 );
212 62 : FlagN = NOHUFCODE;
213 : }
214 :
215 11407 : push_indice( hBstr, IND_FLAGN, FlagN, 1 );
216 11407 : push_indice( hBstr, IND_PG_IDX, pgain_difidx[0], GAIN0_BITS );
217 :
218 11407 : if ( FlagN )
219 : {
220 174832 : for ( i = 1; i < vq_peaks; i++ )
221 : {
222 163487 : j = pgain_difidx[i];
223 163487 : m = pgain_huffnorm[j];
224 163487 : r = pgain_huffsizn[j];
225 :
226 163487 : push_indice( hBstr, IND_PG_IDX, m, r );
227 : }
228 : }
229 : else
230 : {
231 981 : for ( i = 1; i < vq_peaks; i++ )
232 : {
233 919 : push_indice( hBstr, IND_PG_IDX, pgain_difidx[i], GAINI_BITS );
234 : }
235 : }
236 :
237 : /* Number of bits used for peak gain quantization */
238 11407 : bits += FLAGN_BITS + GAIN0_BITS + hcode_l;
239 :
240 : /* Add sign for peak shape normalization */
241 187220 : for ( i = 0; i < vq_peaks; i++ )
242 : {
243 175813 : peak_gains[i] = pos_vec[vq_peak_idx[i]] * pgain_q[i];
244 : }
245 :
246 : /* Quantize peak shapes */
247 175813 : for ( i = 0; i < vq_peaks - 1; i++ )
248 : {
249 164406 : num_overlap_bins = 5 - ( vq_peak_idx[i + 1] - vq_peak_idx[i] );
250 164406 : 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 164406 : push_indice( hBstr, IND_HVQ_PEAKS, vq_cb_idx, 8 );
252 164406 : bits += 9;
253 : }
254 :
255 11407 : 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 11407 : push_indice( hBstr, IND_HVQ_PEAKS, vq_cb_idx, 8 );
257 11407 : bits += 9;
258 :
259 11407 : pos_bits = hvq_code_pos( hBstr, pos_vec, bin_th, vq_peaks );
260 :
261 11407 : bits += pos_bits;
262 11407 : bit_budget = num_bits - bits;
263 :
264 : /* Calculate number of PVQ bands to code and assign bits */
265 11407 : 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 11407 : 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 11407 : i = 0;
272 11407 : n = 0;
273 11407 : s = 0;
274 28255 : for ( k = 0; k < pvq_bands; k++ )
275 : {
276 16848 : if ( k >= pvq_bands - n_sel_bnds )
277 : {
278 1933 : i = band_start_harm[sel_bnds[s]];
279 1933 : s++;
280 : }
281 16848 : k_sort[k] = k;
282 16848 : j = 0;
283 16848 : pPvqVectorBandStart = &pvq_vector[n];
284 727276 : while ( j < hvq_band_width[k] )
285 : {
286 710428 : if ( coefs_out[i] == 0 )
287 : {
288 432840 : pvq_vector[n] = coefs[i];
289 432840 : j++;
290 432840 : n++;
291 : }
292 710428 : i++;
293 : }
294 16848 : logqnorm( pPvqVectorBandStart, &pvq_norm[k], 40, hvq_band_width[k], &thren_HQ[0] );
295 : }
296 :
297 11407 : normalizecoefs( pvq_vector, pvq_norm, pvq_bands, hvq_band_start, hvq_band_end );
298 :
299 11407 : bit_budget -= HVQ_PVQ_GAIN_BITS * pvq_bands;
300 28255 : for ( k = 0; k < pvq_bands; k++ )
301 : {
302 16848 : Rk_f[k] = Rk[k] * 8;
303 : }
304 :
305 11407 : pvq_bits = bit_budget;
306 11407 : set_s( npulses, 0, MAX_PVQ_BANDS );
307 :
308 11407 : 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 28255 : for ( i = 0; i < pvq_bands; i++ )
311 : {
312 16848 : 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 11407 : 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 11407 : i = 0;
343 11407 : n = 0;
344 11407 : s = 0;
345 28255 : for ( k = 0; k < pvq_bands; k++ )
346 : {
347 16848 : normq = dicn[pvq_norm[k]] * ( gopt[k] / fg_pred[k] );
348 :
349 16848 : logqnorm( &normq, &pvq_norm[k], 40, 1, &thren_HQ[0] );
350 16848 : pvq_norm[k] -= 8;
351 16848 : if ( pvq_norm[k] < 0 )
352 : {
353 0 : pvq_norm[k] = 0;
354 : }
355 :
356 16848 : push_indice( hBstr, IND_HVQ_PVQ_GAIN, pvq_norm[k], HVQ_PVQ_GAIN_BITS );
357 16848 : pvq_bits += HVQ_PVQ_GAIN_BITS;
358 :
359 16848 : pvq_norm[k] += 8;
360 16848 : j = 0;
361 16848 : if ( k >= pvq_bands - n_sel_bnds )
362 : {
363 1933 : i = band_start_harm[sel_bnds[s++]];
364 : }
365 727276 : while ( j < hvq_band_width[k] )
366 : {
367 710428 : normq = dicn[pvq_norm[k]];
368 710428 : if ( coefs_out[i] == 0 )
369 : {
370 432840 : coefs_out[i] = coefs_pvq[n] * fg_pred[k];
371 432840 : coefs_out[i] = coefs_out[i] * normq;
372 432840 : j++;
373 432840 : n++;
374 : }
375 710428 : i++;
376 : }
377 : }
378 :
379 11407 : bits += pvq_bits;
380 :
381 : /* Noise fill unquantized coeffs with one gain per group */
382 34221 : for ( i = 0; i < HVQ_NF_GROUPS; i++ )
383 : {
384 2834974 : for ( j = i * ( bin_th / HVQ_NF_GROUPS ); j < ( i + 1 ) * ( bin_th / HVQ_NF_GROUPS ); j++ )
385 : {
386 2812160 : if ( coefs_out[j] == 0 )
387 : {
388 1733254 : coefs_out[j] = ( (float) own_random( &nf_seed ) / MAX16B ) * nf_gains[i];
389 : }
390 : }
391 : }
392 :
393 11407 : return bits;
394 : }
395 :
396 : /*--------------------------------------------------------------------------
397 : * quant_peaks()
398 : *
399 : * Applies VQ on input vector
400 : *--------------------------------------------------------------------------*/
401 :
402 175813 : 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 175813 : set_s( weights, 1, 4 );
419 :
420 175813 : x[0] = vect_in[0] / ( *peak_gain );
421 175813 : x[1] = vect_in[1] / ( *peak_gain );
422 175813 : x[2] = vect_in[3] / ( *peak_gain );
423 175813 : x[3] = vect_in[4] / ( *peak_gain );
424 :
425 175813 : if ( vect_out[0] != 0 )
426 : {
427 19297 : if ( fabs( peak_gain[-1] ) > fabs( *peak_gain ) )
428 : {
429 14666 : weights[0] = 0;
430 :
431 14666 : if ( vect_out[1] != 0 )
432 : {
433 8102 : weights[1] = 0;
434 : }
435 : }
436 : }
437 :
438 175813 : if ( overlap > 0 )
439 : {
440 33004 : if ( fabs( peak_gain[1] ) > fabs( *peak_gain ) )
441 : {
442 37077 : for ( i = 3; i > 3 - overlap; i-- )
443 : {
444 23370 : weights[i] = 0;
445 : }
446 : }
447 : }
448 :
449 :
450 : /* Classify */
451 175813 : cb_class = (int16_t) w_vquant( x, 0, weights, 0, hvq_class_c, HVQ_VQ_DIM - 1, HVQ_NUM_CLASS, 0 );
452 :
453 175813 : if ( core_brate < HQ_BWE_CROSSOVER_BRATE )
454 : {
455 123157 : idx = max( 0, HVQ_MAX_PEAKS_24k - Npeaks );
456 123157 : search_overlap = hvq_cb_search_overlap24k[idx];
457 : }
458 : else
459 : {
460 52656 : idx = max( 0, HVQ_MAX_PEAKS_32k - Npeaks );
461 52656 : search_overlap = hvq_cb_search_overlap32k[idx];
462 : }
463 :
464 : /* Quantize */
465 175813 : if ( cb_class == 0 )
466 : {
467 48702 : *vq_idx = (int16_t) w_vquant( x, 0, weights, xq, hvq_peak_cb, 4, HVQ_CB_SIZE / 2 + search_overlap, 0 );
468 48702 : push_indice( hBstr, IND_HVQ_PEAKS, 0, 1 );
469 : }
470 127111 : else if ( cb_class == 1 )
471 : {
472 41552 : *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 41552 : *vq_idx += HVQ_CB_SIZE / 2 - search_overlap;
474 41552 : push_indice( hBstr, IND_HVQ_PEAKS, 0, 1 );
475 : }
476 85559 : else if ( cb_class == 2 )
477 : {
478 39861 : *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 39861 : *vq_idx += HVQ_CB_SIZE / 2 - search_overlap;
480 39861 : push_indice( hBstr, IND_HVQ_PEAKS, 1, 1 );
481 : }
482 : else
483 : {
484 45698 : *vq_idx = (int16_t) w_vquant( x, 0, weights, xq, hvq_peak_cb, 4, HVQ_CB_SIZE / 2 + search_overlap, 1 );
485 45698 : push_indice( hBstr, IND_HVQ_PEAKS, 1, 1 );
486 : }
487 :
488 175813 : vect_out[0] = weights[0] * ( xq[0] * ( *peak_gain ) ) + ( weights[0] ^ 1 ) * vect_out[0];
489 175813 : vect_out[1] = weights[1] * ( xq[1] * ( *peak_gain ) ) + ( weights[1] ^ 1 ) * vect_out[1];
490 175813 : vect_out[2] = *peak_gain;
491 175813 : vect_out[3] = weights[2] * ( xq[2] * ( *peak_gain ) ) + ( weights[2] ^ 1 ) * vect_out[3];
492 175813 : vect_out[4] = weights[3] * ( xq[3] * ( *peak_gain ) ) + ( weights[3] ^ 1 ) * vect_out[4];
493 :
494 175813 : return;
495 : }
496 :
497 : /*--------------------------------------------------------------------------
498 : * code_pos()
499 : *
500 : * Code pulse positions
501 : *--------------------------------------------------------------------------*/
502 :
503 : /*! r: number of consumed bits */
504 11407 : 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 11407 : int16_t bits = 0;
515 : int16_t mask;
516 :
517 11407 : set_s( layer2, 0, HVQ_CP_L2_MAX );
518 :
519 11407 : layer_length = (int16_t) ( (float) length / HVQ_CP_L1_LEN + 0.5 );
520 :
521 575585 : for ( j = 0; j < layer_length; j++ )
522 : {
523 2871261 : for ( i = j * HVQ_CP_L1_LEN; i < min( ( j + 1 ) * HVQ_CP_L1_LEN, length ); i++ )
524 : {
525 2474761 : if ( inp[i] )
526 : {
527 167678 : layer2[j] = 1;
528 167678 : break;
529 : }
530 : }
531 : }
532 :
533 575585 : for ( i = 0; i < layer_length; i++ )
534 : {
535 564178 : result[i] = layer2[i];
536 : }
537 11407 : bits += layer_length;
538 :
539 575585 : for ( j = 0; j < layer_length; j++ )
540 : {
541 564178 : if ( layer2[j] )
542 : {
543 167678 : val = 0;
544 1005864 : for ( i = j * HVQ_CP_L1_LEN; i < min( ( j + 1 ) * HVQ_CP_L1_LEN, length ); i++ )
545 : {
546 838186 : val <<= 1;
547 838186 : val |= inp[i];
548 : }
549 :
550 544634 : for ( idx = 0; idx < HVQ_CP_MAP_LEN; idx++ )
551 : {
552 544634 : if ( hvq_cp_layer1_map5[idx] == val )
553 : {
554 167678 : break;
555 : }
556 : }
557 :
558 167678 : mask = 1 << ( HVQ_CP_MAP_IDX_LEN - 1 );
559 670712 : for ( i = 0; i < HVQ_CP_MAP_IDX_LEN; i++ )
560 : {
561 503034 : result[bits++] = ( idx & mask ) >> ( HVQ_CP_MAP_IDX_LEN - 1 - i );
562 503034 : mask >>= 1;
563 : }
564 : }
565 : }
566 :
567 11407 : return bits;
568 : }
569 :
570 : /*--------------------------------------------------------------------------
571 : * hvq_code_pos()
572 : *
573 : * Code pulse positions
574 : *--------------------------------------------------------------------------*/
575 :
576 11407 : 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 11407 : bits = 0;
594 :
595 : /* Extract sorted peak index vector and sign vector */
596 2823567 : for ( i = 0, j = 0; i < length; i++ )
597 : {
598 2812160 : inp_abs[i] = (int16_t) abs( inp[i] );
599 2812160 : if ( inp[i] )
600 : {
601 175813 : peak_idx[j] = i;
602 175813 : inp_sign[j++] = inp[i];
603 : }
604 : }
605 :
606 : /* Calculate delta */
607 11407 : delta[0] = peak_idx[0] + HVQ_CP_HUFF_OFFSET;
608 11407 : delta_max = delta[0];
609 175813 : for ( i = 1; i < num_peaks; i++ )
610 : {
611 164406 : delta[i] = peak_idx[i] - peak_idx[i - 1] - HVQ_CP_HUFF_OFFSET;
612 164406 : if ( delta_max < delta[i] )
613 : {
614 27555 : delta_max = delta[i];
615 : }
616 : }
617 :
618 : /* Calculate bits needed for huffman coding of deltas */
619 11407 : delta_bits = -1;
620 11407 : if ( delta_max <= HVQ_CP_HUFF_MAX )
621 : {
622 10835 : delta_bits = 0;
623 179556 : for ( i = 0; i < num_peaks; i++ )
624 : {
625 168721 : delta_bits += hvq_cp_huff_len[delta[i]];
626 : }
627 : }
628 :
629 : /* Calculate bits neeed for sparse coding */
630 11407 : sparse_bits = sparse_code_pos( inp_abs, length, sparse_result );
631 :
632 : /* Decide which coding mode to use */
633 11407 : if ( delta_bits > sparse_bits || delta_bits < 0 )
634 : {
635 1982 : push_indice( hBstr, IND_POS_IDX, HVQ_CP_SPARSE, 1 );
636 :
637 171624 : for ( i = 0; i < sparse_bits; i++ )
638 : {
639 169642 : push_indice( hBstr, IND_POS_IDX, sparse_result[i], 1 );
640 : }
641 1982 : bits += sparse_bits + 1;
642 : }
643 : else
644 : {
645 9425 : push_indice( hBstr, IND_POS_IDX, HVQ_CP_DELTA, 1 );
646 :
647 156924 : for ( i = 0; i < num_peaks; i++ )
648 : {
649 147499 : j = delta[i];
650 147499 : push_indice( hBstr, IND_POS_IDX, hvq_cp_huff_val[j], hvq_cp_huff_len[j] );
651 : }
652 9425 : bits += delta_bits + 1;
653 : }
654 :
655 : /* Send sign */
656 187220 : for ( i = 0; i < num_peaks; i++ )
657 : {
658 175813 : push_indice( hBstr, IND_POS_IDX, ( inp_sign[i] < 0 ? 0 : 1 ), 1 );
659 : }
660 11407 : bits += num_peaks;
661 :
662 11407 : return bits;
663 : }
|