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