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 "rom_com.h"
44 : #include "prot.h"
45 : #include "prot.h"
46 : #include "stl.h"
47 : #include "wmc_auto.h"
48 :
49 : /*-------------------------------------------------------------------*
50 : * Local function prototypes
51 : *--------------------------------------------------------------------*/
52 :
53 : static int16_t calc_pvq_splits( BSTR_ENC_HANDLE hBstr, PVQ_ENC_HANDLE hPVQ, const int16_t band_bits, const int16_t sfmsize, const float *y, int16_t *bits );
54 :
55 : static void obtainEnergyParameter( const float Enear, const float Eopp, int16_t *param );
56 :
57 : static void densityIndexSymbolEncode( BSTR_ENC_HANDLE hBstr, PVQ_ENC_HANDLE hPVQ, const int16_t density, const int16_t r_dim, const int16_t l_dim, const int16_t index_phi );
58 :
59 : static void encode_energies( BSTR_ENC_HANDLE hBstr, PVQ_ENC_HANDLE hPVQ, const float *coefs, const int16_t Np, int16_t *dim_part, float *E_part, int16_t *bits_part, int16_t *g_part, const int16_t qband, int16_t *bits_left, const float enr, const int16_t n, const int16_t strict_bits );
60 :
61 :
62 : /*-------------------------------------------------------------------*
63 : * pvq_encode_band()
64 : *
65 : * Encode band with PVQ
66 : *--------------------------------------------------------------------*/
67 :
68 298047 : static void pvq_encode_band(
69 : BSTR_ENC_HANDLE hBstr, /* i/o: Encoder bitstream handle */
70 : PVQ_ENC_HANDLE hPVQ, /* i/o: PVQ encoder handle */
71 : const float *coefs_norm, /* i : normalized vector to encode */
72 : int16_t *pulse_vector, /* o : quantized vector, integer */
73 : int16_t *npulses, /* o : number of pulses */
74 : float *coefs_quant, /* o : quantized vector */
75 : const int16_t sfmsize, /* i : length of vector */
76 : const int16_t band_bits, /* i : assigned bits */
77 : int16_t *bits_left, /* o : bits remaining */
78 : const int16_t strict_bits /* i : conservative rounding flag */
79 : )
80 : {
81 : int16_t K_val;
82 : int16_t j, Np;
83 : float enr, E_part[MAX_SPLITS + 1];
84 : int16_t part_start[MAX_SPLITS + 1], dim_part[MAX_SPLITS + 1], bits_part[MAX_SPLITS + 1];
85 : int16_t pool_tot, pool_part, dim_parts;
86 : float g_part[MAX_SPLITS];
87 : int16_t g_part_s[MAX_SPLITS];
88 : int16_t sg_part[MAX_SPLITS + 1];
89 : int16_t idx_sort[MAX_SPLITS + 1];
90 : int16_t js, band_bits_tot, split_bit;
91 :
92 298047 : Np = calc_pvq_splits( hBstr, hPVQ, band_bits, sfmsize, coefs_norm, &split_bit );
93 298047 : band_bits_tot = band_bits - split_bit;
94 :
95 298047 : enr = 0.0f;
96 4618779 : for ( j = 0; j < sfmsize; j++ )
97 : {
98 4320732 : enr += coefs_norm[j] * coefs_norm[j];
99 : }
100 :
101 298047 : dim_parts = (int16_t) intLimCDivPos( sfmsize, Np );
102 298047 : set_s( dim_part, dim_parts, Np - 1 );
103 298047 : dim_part[Np - 1] = sfmsize - dim_parts * ( Np - 1 );
104 :
105 298047 : part_start[0] = 0;
106 330120 : for ( j = 1; j < Np; j++ )
107 : {
108 32073 : part_start[j] = part_start[j - 1] + dim_part[j - 1];
109 : }
110 :
111 298047 : set_s( g_part_s, -32768, Np );
112 298047 : if ( Np > 1 )
113 : {
114 28647 : encode_energies( hBstr, hPVQ, coefs_norm, Np, dim_part, E_part, bits_part, g_part_s, band_bits_tot, bits_left, enr, sfmsize, strict_bits );
115 : }
116 : else
117 : {
118 269400 : bits_part[0] = band_bits_tot;
119 : }
120 :
121 298047 : pool_tot = 0;
122 298047 : pool_part = 0;
123 :
124 628167 : for ( j = 0; j < Np; j++ )
125 : {
126 330120 : g_part[j] = -( (float) g_part_s[j] ) / 32768;
127 : /* aligned to BASOP to avoid USAN undefined negation warning with -(-32768) */
128 330120 : g_part_s[j] = negate( g_part_s[j] );
129 : }
130 :
131 298047 : srt_vec_ind( g_part_s, sg_part, idx_sort, Np );
132 :
133 628167 : for ( j = 0; j < Np; j++ )
134 : {
135 330120 : js = idx_sort[Np - 1 - j];
136 :
137 :
138 330120 : pool_part = shrtCDivSignedApprox( pool_tot, Np - j );
139 330120 : bits_part[js] = max( 0, min( bits_part[js] + pool_part, 256 ) );
140 :
141 330120 : conservativeL1Norm( dim_part[js], bits_part[js], strict_bits, *bits_left, pool_tot, *npulses, /* inputs */
142 : &K_val, bits_left, &pool_tot, npulses ); /* outputs */
143 330120 : if ( K_val >= 1 )
144 : {
145 299764 : pvq_encode( hBstr, hPVQ, coefs_norm + part_start[js], pulse_vector + part_start[js], coefs_quant + part_start[js], K_val, dim_part[js], g_part[js] );
146 : }
147 : else
148 : {
149 30356 : set_f( coefs_quant + part_start[js], 0.0f, dim_part[js] );
150 30356 : set_s( pulse_vector + part_start[js], 0, dim_part[js] );
151 : }
152 : }
153 :
154 298047 : return;
155 : }
156 :
157 : /*-------------------------------------------------------------------*
158 : * pvq_encode_frame()
159 : *
160 : *
161 : *--------------------------------------------------------------------*/
162 :
163 31229 : void pvq_encode_frame(
164 : BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle */
165 : const float *coefs_norm, /* i : normalized coefficients to encode */
166 : float *coefs_quant, /* o : quantized coefficients */
167 : float *gopt, /* o : optimal shape gains */
168 : int16_t *npulses, /* o : number of pulses per band */
169 : int16_t *pulse_vector, /* o : non-normalized pulse shapes */
170 : const int16_t *sfm_start, /* i : indices of first coefficients in the bands */
171 : const int16_t *sfm_end, /* i : indices of last coefficients in the bands */
172 : const int16_t *sfmsize, /* i : band sizes */
173 : const int16_t nb_sfm, /* i : total number of bands */
174 : const int16_t *R, /* i : bitallocation per band (Q3)*/
175 : const int16_t pvq_bits, /* i : number of bits avaiable */
176 : const int16_t core /* i : core */
177 : )
178 : {
179 : int16_t i, j;
180 : int16_t band_bits, bits_left;
181 31229 : int16_t bit_pool = 0;
182 : int16_t coded_bands, bands_to_code;
183 : int16_t curr_bits;
184 : int16_t R_sort[NB_SFM]; /*Q3*/
185 : int16_t is, i_sort[NB_SFM];
186 : int16_t strict_bits;
187 : PVQ_ENC_DATA pvq_enc;
188 :
189 31229 : PVQ_ENC_HANDLE hPVQ = &pvq_enc;
190 :
191 31229 : rc_enc_init( hPVQ, pvq_bits );
192 31229 : curr_bits = ( pvq_bits - RC_BITS_RESERVED ) << 3;
193 :
194 31229 : bands_to_code = 0;
195 483006 : for ( i = 0; i < nb_sfm; i++ )
196 : {
197 451777 : if ( R[i] > 0 )
198 : {
199 298047 : bands_to_code++;
200 : }
201 : }
202 :
203 31229 : if ( core == ACELP_CORE )
204 : {
205 22806 : strict_bits = 1;
206 22806 : srt_vec_ind( R, R_sort, i_sort, nb_sfm );
207 : }
208 : else
209 : {
210 8423 : strict_bits = 0;
211 296373 : for ( i = 0; i < nb_sfm; i++ )
212 : {
213 287950 : i_sort[i] = i;
214 : }
215 : }
216 :
217 31229 : coded_bands = 0;
218 483006 : for ( i = 0; i < nb_sfm; i++ )
219 : {
220 451777 : is = i_sort[i];
221 451777 : gopt[is] = 0;
222 451777 : if ( R[is] > 0 )
223 : {
224 298047 : bandBitsAdjustment( hPVQ->rc_num_bits, hPVQ->rc_range, curr_bits, bands_to_code, bands_to_code - coded_bands, sfmsize[is], R[is], bit_pool, /* inputs */
225 : &band_bits, &bits_left, &bit_pool ); /* outputs */
226 :
227 298047 : pvq_encode_band( hBstr, hPVQ, &coefs_norm[sfm_start[is]], &pulse_vector[sfm_start[is]], &npulses[is], &coefs_quant[sfm_start[is]], sfmsize[is], band_bits, &bits_left, strict_bits );
228 :
229 298047 : gopt[is] = dotp( coefs_quant + sfm_start[is], coefs_norm + sfm_start[is], sfmsize[is] ) /
230 298047 : ( dotp( coefs_quant + sfm_start[is], coefs_quant + sfm_start[is], sfmsize[is] ) + 1e-15f );
231 :
232 298047 : if ( gopt[is] == 0.0f )
233 : {
234 30198 : gopt[is] = 1e-10f;
235 : }
236 : /* Updates */
237 298047 : coded_bands++;
238 : }
239 : else
240 : {
241 3697350 : for ( j = sfm_start[is]; j < sfm_end[is]; j++ )
242 : {
243 3543620 : coefs_quant[j] = 0.0f;
244 3543620 : pulse_vector[j] = 0;
245 : }
246 : }
247 : }
248 :
249 31229 : rc_enc_finish( hBstr, hPVQ );
250 :
251 31229 : return;
252 : }
253 :
254 : /*---------------------------------------------------------------------*
255 : * pvq_core_enc()
256 : *
257 : * Main Generic Audio Encoder Routine
258 : *---------------------------------------------------------------------*/
259 :
260 : /*! r: number of bits encoded */
261 29902 : int16_t pvq_core_enc(
262 : BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle */
263 : float coefs_norm[], /* i/o: normalized coefficients to encode */
264 : float coefs_quant[], /* o : quantized coefficients */
265 : const int16_t bits_tot, /* i : total number of bits */
266 : const int16_t nb_sfm, /* i : number of bands */
267 : const int16_t *sfm_start, /* i : Subband start coefficient */
268 : const int16_t *sfm_end, /* i : Subband end coefficient */
269 : const int16_t *sfmsize, /* i : subband width */
270 : int16_t *R, /* i/o: Bit allocation/Adjusted bit alloc. (Q3) */
271 : int16_t *Rs, /* i/o: Integer bit allocation */
272 : int16_t *npulses, /* o : number of pulses */
273 : int16_t *maxpulse, /* i : maximum pulse per band */
274 : const int16_t core /* i : number of bands */
275 : )
276 : {
277 : int16_t i;
278 : int16_t R_upd; /*Q3*/
279 : int16_t ord[NB_SFM_MAX];
280 : float fg_pred[NB_SFM_MAX];
281 : int16_t pvq_bits;
282 : int16_t pulse_vector[L_SPEC48k_EXT];
283 : float gopt[NB_SFM];
284 : int16_t gain_bits_array[NB_SFM];
285 : int16_t gain_bits_tot;
286 :
287 29902 : R_upd = bits_tot * 8;
288 29902 : gain_bits_tot = assign_gain_bits( core, nb_sfm, sfmsize, R, gain_bits_array, &R_upd );
289 29902 : pvq_bits = R_upd >> 3;
290 29902 : pvq_encode_frame( hBstr, coefs_norm, coefs_quant, gopt, npulses, pulse_vector, sfm_start, sfm_end, sfmsize, nb_sfm, R, pvq_bits, core );
291 :
292 29902 : if ( Rs != NULL )
293 : {
294 292454 : for ( i = 0; i < nb_sfm; i++ )
295 : {
296 285358 : Rs[i] = Rs[i] * ( npulses[i] > 0 ); /* Update Rs in case no pulses were assigned */
297 : }
298 : }
299 :
300 479087 : for ( i = 0; i < nb_sfm; i++ )
301 : {
302 449185 : ord[i] = i;
303 449185 : R[i] = R[i] * ( npulses[i] > 0 ); /* Update in case no pulses were assigned */
304 : }
305 :
306 29902 : get_max_pulses( sfm_start, sfm_end, ord, npulses, nb_sfm, pulse_vector, maxpulse );
307 :
308 29902 : fine_gain_pred( sfm_start, sfm_end, sfmsize, ord, npulses, maxpulse, R, nb_sfm, coefs_quant, pulse_vector, fg_pred, core );
309 :
310 29902 : fine_gain_quant( hBstr, ord, nb_sfm, gain_bits_array, fg_pred, gopt );
311 :
312 29902 : apply_gain( ord, sfm_start, sfm_end, nb_sfm, fg_pred, coefs_quant );
313 :
314 29902 : return ( pvq_bits + gain_bits_tot );
315 : }
316 :
317 :
318 : /*-------------------------------------------------------------------*
319 : * encode_energies()
320 : *
321 : *
322 : *--------------------------------------------------------------------*/
323 :
324 32073 : static void encode_energies(
325 : BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle */
326 : PVQ_ENC_HANDLE hPVQ, /* i/o: PVQ encoder handle */
327 : const float *coefs, /* i : coefficients to encode */
328 : const int16_t Np, /* i : number of parts */
329 : int16_t *dim_part, /* o : sizes of parts */
330 : float *E_part, /* o : energies of parts */
331 : int16_t *bits_part, /* o : assigned bits per part */
332 : int16_t *g_part, /* o : gains */
333 : const int16_t qband, /* i : assigned quanta per band */
334 : int16_t *bits_left, /* o : remaining bits */
335 : const float enr, /* i : energy of vector */
336 : const int16_t dim, /* i : size of vector */
337 : const int16_t strict_bits /* i : conservative rounding flag */
338 : )
339 : {
340 : int16_t i, j, l_Np, r_Np;
341 : int16_t l_bits, r_bits, l_dim, r_dim;
342 : float l_enr, r_enr;
343 : int16_t il, ir;
344 : int16_t oppRQ3, qzero;
345 : int16_t density;
346 : int16_t phi;
347 32073 : int16_t index_phi = -1;
348 :
349 32073 : l_Np = Np >> 1;
350 32073 : r_Np = Np - l_Np;
351 :
352 32073 : l_enr = 0.0f;
353 32073 : l_bits = 0;
354 32073 : l_dim = 0;
355 64405 : for ( i = 0; i < l_Np; i++ )
356 : {
357 32332 : l_dim += dim_part[i];
358 : }
359 314698 : for ( j = 0; j < l_dim; j++ )
360 : {
361 282625 : l_enr += coefs[j] * coefs[j];
362 : }
363 32073 : r_enr = enr - l_enr;
364 32073 : r_dim = dim - l_dim;
365 :
366 32073 : obtainEnergyQuantizerDensity( dim, qband, &density );
367 32073 : obtainEnergyParameter( l_enr, r_enr, &phi );
368 32073 : rangeCoderFinalizationFBits( hPVQ->rc_num_bits, hPVQ->rc_range, &qzero );
369 32073 : densityAngle2RmsProjEnc( density, phi, &index_phi, &ir, &il, &oppRQ3 );
370 32073 : densityIndexSymbolEncode( hBstr, hPVQ, density, r_dim, l_dim, index_phi );
371 :
372 64405 : for ( i = 0; i < l_Np; i++ )
373 : {
374 32332 : g_part[i] = ( g_part[i] * il + 16384 ) >> 15;
375 : }
376 :
377 67376 : for ( i = l_Np; i < Np; i++ )
378 : {
379 35303 : g_part[i] = ( g_part[i] * ir + 16384 ) >> 15;
380 : }
381 :
382 32073 : NearOppSplitAdjustment( qband, qzero, hPVQ->rc_num_bits, hPVQ->rc_range, *bits_left, strict_bits, Np, dim_part[0], dim_part[Np - 1], l_dim, r_dim, oppRQ3, &l_bits, &r_bits, bits_left );
383 :
384 32073 : if ( l_Np > 1 )
385 : {
386 252 : encode_energies( hBstr, hPVQ, coefs, l_Np, dim_part, E_part, bits_part, g_part, l_bits, bits_left, l_enr, l_dim, strict_bits );
387 : }
388 : else
389 : {
390 31821 : E_part[0] = l_enr;
391 31821 : bits_part[0] = l_bits;
392 : }
393 :
394 32073 : if ( r_Np > 1 )
395 : {
396 3174 : encode_energies( hBstr, hPVQ, &coefs[l_dim], r_Np, &dim_part[l_Np], &E_part[l_Np], &bits_part[l_Np], &g_part[l_Np], r_bits, bits_left, r_enr, r_dim, strict_bits );
397 : }
398 : else
399 : {
400 28899 : E_part[1] = r_enr;
401 28899 : bits_part[1] = r_bits;
402 : }
403 :
404 32073 : return;
405 : }
406 :
407 :
408 32073 : static void densityIndexSymbolEncode(
409 : BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle */
410 : PVQ_ENC_HANDLE hPVQ, /* i/o: PVQ encoder handle */
411 : const int16_t density, /* i : Current density */
412 : const int16_t opp_sz, /* i : Opposite size */
413 : const int16_t near_sz, /* i : Near size */
414 : const int16_t index_phi /* i : Index */
415 : )
416 : {
417 : int16_t angle, c;
418 32073 : uint32_t sym_freq = 1;
419 : int32_t cum_freq, tot;
420 : int16_t densityPlOne, densitySubC;
421 : int16_t densitySubIndex, nearFlag;
422 : #ifdef BASOP_NOGLOB
423 : Flag Overflow;
424 : #endif /* BASOP_NOGLOB */
425 :
426 32073 : if ( ( 0xFFFE & density ) != 0 ) /* even */
427 : {
428 31892 : angle = atan2_fx( SQRT_DIM_fx[opp_sz], SQRT_DIM_fx[near_sz] );
429 : #ifndef BASOP_NOGLOB
430 : angle = shl( angle, 1 );
431 : #else /* BASOP_NOGLOB */
432 31892 : angle = shl_o( angle, 1, &Overflow );
433 : #endif /* BASOP_NOGLOB */
434 31892 : angle = mult_r( angle, 20861 );
435 31892 : c = mult_r( density, angle );
436 :
437 31892 : densityPlOne = 1 + density;
438 31892 : densitySubC = density - c;
439 :
440 31892 : tot = 2 + density * densityPlOne; /* c==0, c==density*/
441 31892 : sym_freq = 1 + 2 * index_phi; /* c==density */
442 31892 : cum_freq = index_phi * index_phi; /* c==density*/
443 :
444 31892 : if ( c == 0 )
445 : {
446 0 : sym_freq = 2 * densityPlOne - sym_freq;
447 0 : cum_freq = 2 * index_phi * (densityPlOne) -cum_freq;
448 : }
449 31892 : else if ( densitySubC != 0 ) /* c n.eq. density */
450 : {
451 31892 : densitySubIndex = density - index_phi;
452 31892 : nearFlag = ( index_phi <= c );
453 :
454 31892 : tot = densityPlOne + density * c * ( densitySubC );
455 31892 : sym_freq = nearFlag ? ( 1 + 2 * index_phi * densitySubC ) : ( 1 + 2 * densitySubIndex * c );
456 31892 : cum_freq = nearFlag ? ( index_phi * ( ( index_phi - 1 ) * densitySubC + 1 ) ) : ( tot - densityPlOne - densitySubIndex * ( densitySubIndex + 1 ) * c + index_phi );
457 : }
458 : /* else keep values for c==density*/
459 31892 : rc_encode( hBstr, hPVQ, cum_freq, sym_freq, tot );
460 : }
461 :
462 32073 : return;
463 : }
464 :
465 :
466 : /*--------------------------------------------------------------------------*
467 : * calc_pvq_splits()
468 : *
469 : * Calculate the number of segments needed
470 : *--------------------------------------------------------------------------*/
471 :
472 : /*! r: Number of segments */
473 298047 : static int16_t calc_pvq_splits(
474 : BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle */
475 : PVQ_ENC_HANDLE hPVQ, /* i/o: PVQ encoder handle */
476 : const int16_t band_bits, /* i : Band bit-budget */
477 : const int16_t sfmsize, /* i : Band width */
478 : const float *y, /* i : Target vector */
479 : int16_t *bits /* o : Consumed bits */
480 : )
481 : {
482 : int16_t Np;
483 : int16_t Npart;
484 : int16_t i, j, k;
485 : float E[MAX_SPLITS];
486 : float Emean;
487 : float tmp;
488 : float max_dev;
489 :
490 298047 : Np = (int16_t) ( intLimCDivPos( band_bits, 67 ) >> 2 );
491 298047 : if ( band_bits - 268 * Np != 0 || Np == 0 ) /* L_msu */
492 : {
493 297798 : Np++; /* ceil */
494 : }
495 298047 : *bits = 0;
496 :
497 298047 : if ( Np < MAX_SPLITS && ( band_bits - ( 8 * sfmsize * THR_ADD_SPLIT ) > 0 ) )
498 : {
499 73 : Npart = (int16_t) intLimCDivPos( sfmsize, Np );
500 73 : *bits = 8;
501 73 : Emean = 0;
502 73 : k = 0;
503 297 : for ( i = 0; i < Np; i++ )
504 : {
505 224 : E[i] = EPSILON;
506 754 : for ( j = 0; j < Npart; j++, k++ )
507 : {
508 530 : E[i] += y[k] * y[k];
509 : }
510 224 : E[i] = (float) 30 - norm_l( (Word32) max( E[i], 1 ) ); /* a 0 input integer Ei yields a zeroed log2(Ei) out */
511 224 : Emean += E[i];
512 : }
513 73 : Emean /= Np;
514 :
515 73 : max_dev = -1;
516 297 : for ( i = 0; i < Np; i++ )
517 : {
518 224 : tmp = (float) fabs( E[i] - Emean );
519 224 : if ( tmp > max_dev )
520 : {
521 98 : max_dev = tmp;
522 : }
523 : }
524 :
525 73 : if ( max_dev > ( 32 - band_bits / ( 8 * Np ) ) )
526 : {
527 16 : rc_enc_bits( hBstr, hPVQ, 1, 1 );
528 16 : Np += 1;
529 : }
530 : else
531 : {
532 57 : rc_enc_bits( hBstr, hPVQ, 0, 1 );
533 : }
534 : }
535 :
536 298047 : Np = max( Np, (int16_t) ( ceil( (float) sfmsize / PVQ_MAX_BAND_SIZE ) ) );
537 298047 : Np = min( MAX_SPLITS, Np );
538 298047 : Np = min( (int16_t) floor( (float) sfmsize / MIN_BAND_SIZE ), Np );
539 :
540 298047 : return Np;
541 : }
542 :
543 :
544 : #define EPSILON_obtainEnergyParameter 0.00373f /**/
545 : #define C_obtainEnergyParameter ( (float) ( 1 << 15 ) / EVS_PI + EPSILON_obtainEnergyParameter )
546 :
547 32073 : static void obtainEnergyParameter(
548 : const float Enear, /* i : Near energy */
549 : const float Eopp, /* i : Opposite energy */
550 : int16_t *param /* o : Energy parameter */
551 : )
552 : {
553 32073 : if ( !Eopp )
554 : {
555 0 : *param = 0;
556 0 : return;
557 : }
558 32073 : if ( !Enear )
559 : {
560 0 : *param = 1 << 14;
561 0 : return;
562 : }
563 32073 : *param = (int16_t) ( C_obtainEnergyParameter * atan2( (float) sqrt( Eopp ), (float) sqrt( Enear ) ) + 0.5f );
564 :
565 32073 : return;
566 : }
567 : #undef C_obtainEnergyParameter
568 : #undef EPSILON_obtainEnergyParameter
|