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 3591296 : 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 3591296 : Np = calc_pvq_splits( hBstr, hPVQ, band_bits, sfmsize, coefs_norm, &split_bit );
93 3591296 : band_bits_tot = band_bits - split_bit;
94 :
95 3591296 : enr = 0.0f;
96 56113758 : for ( j = 0; j < sfmsize; j++ )
97 : {
98 52522462 : enr += coefs_norm[j] * coefs_norm[j];
99 : }
100 :
101 3591296 : dim_parts = (int16_t) intLimCDivPos( sfmsize, Np );
102 3591296 : set_s( dim_part, dim_parts, Np - 1 );
103 3591296 : dim_part[Np - 1] = sfmsize - dim_parts * ( Np - 1 );
104 :
105 3591296 : part_start[0] = 0;
106 3918283 : for ( j = 1; j < Np; j++ )
107 : {
108 326987 : part_start[j] = part_start[j - 1] + dim_part[j - 1];
109 : }
110 :
111 3591296 : set_s( g_part_s, -32768, Np );
112 3591296 : if ( Np > 1 )
113 : {
114 295946 : 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 3295350 : bits_part[0] = band_bits_tot;
119 : }
120 :
121 3591296 : pool_tot = 0;
122 3591296 : pool_part = 0;
123 :
124 7509579 : for ( j = 0; j < Np; j++ )
125 : {
126 3918283 : g_part[j] = -( (float) g_part_s[j] ) / 32768;
127 : /* aligned to BASOP to avoid USAN undefined negation warning with -(-32768) */
128 3918283 : g_part_s[j] = negate( g_part_s[j] );
129 : }
130 :
131 3591296 : srt_vec_ind( g_part_s, sg_part, idx_sort, Np );
132 :
133 7509579 : for ( j = 0; j < Np; j++ )
134 : {
135 3918283 : js = idx_sort[Np - 1 - j];
136 :
137 :
138 3918283 : pool_part = shrtCDivSignedApprox( pool_tot, Np - j );
139 3918283 : bits_part[js] = max( 0, min( bits_part[js] + pool_part, 256 ) );
140 :
141 3918283 : 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 3918283 : if ( K_val >= 1 )
144 : {
145 3003558 : 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 914725 : set_f( coefs_quant + part_start[js], 0.0f, dim_part[js] );
150 914725 : set_s( pulse_vector + part_start[js], 0, dim_part[js] );
151 : }
152 : }
153 :
154 3591296 : return;
155 : }
156 :
157 : /*-------------------------------------------------------------------*
158 : * pvq_encode_frame()
159 : *
160 : *
161 : *--------------------------------------------------------------------*/
162 :
163 464270 : 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 464270 : 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 464270 : PVQ_ENC_HANDLE hPVQ = &pvq_enc;
190 :
191 464270 : rc_enc_init( hPVQ, pvq_bits );
192 464270 : curr_bits = ( pvq_bits - RC_BITS_RESERVED ) << 3;
193 :
194 464270 : bands_to_code = 0;
195 5970454 : for ( i = 0; i < nb_sfm; i++ )
196 : {
197 5506184 : if ( R[i] > 0 )
198 : {
199 3591296 : bands_to_code++;
200 : }
201 : }
202 :
203 464270 : if ( core == ACELP_CORE )
204 : {
205 381523 : strict_bits = 1;
206 381523 : srt_vec_ind( R, R_sort, i_sort, nb_sfm );
207 : }
208 : else
209 : {
210 82747 : strict_bits = 0;
211 2788769 : for ( i = 0; i < nb_sfm; i++ )
212 : {
213 2706022 : i_sort[i] = i;
214 : }
215 : }
216 :
217 464270 : coded_bands = 0;
218 5970454 : for ( i = 0; i < nb_sfm; i++ )
219 : {
220 5506184 : is = i_sort[i];
221 5506184 : gopt[is] = 0;
222 5506184 : if ( R[is] > 0 )
223 : {
224 3591296 : 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 3591296 : 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 3591296 : gopt[is] = dotp( coefs_quant + sfm_start[is], coefs_norm + sfm_start[is], sfmsize[is] ) /
230 3591296 : ( dotp( coefs_quant + sfm_start[is], coefs_quant + sfm_start[is], sfmsize[is] ) + 1e-15f );
231 :
232 3591296 : if ( gopt[is] == 0.0f )
233 : {
234 925095 : gopt[is] = 1e-10f;
235 : }
236 : /* Updates */
237 3591296 : coded_bands++;
238 : }
239 : else
240 : {
241 41433618 : for ( j = sfm_start[is]; j < sfm_end[is]; j++ )
242 : {
243 39518730 : coefs_quant[j] = 0.0f;
244 39518730 : pulse_vector[j] = 0;
245 : }
246 : }
247 : }
248 :
249 464270 : rc_enc_finish( hBstr, hPVQ );
250 :
251 464270 : return;
252 : }
253 :
254 : /*---------------------------------------------------------------------*
255 : * pvq_core_enc()
256 : *
257 : * Main Generic Audio Encoder Routine
258 : *---------------------------------------------------------------------*/
259 :
260 : /*! r: number of bits encoded */
261 452863 : 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 452863 : R_upd = bits_tot * 8;
288 452863 : gain_bits_tot = assign_gain_bits( core, nb_sfm, sfmsize, R, gain_bits_array, &R_upd );
289 452863 : pvq_bits = R_upd >> 3;
290 452863 : 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 452863 : if ( Rs != NULL )
293 : {
294 2760514 : for ( i = 0; i < nb_sfm; i++ )
295 : {
296 2689174 : Rs[i] = Rs[i] * ( npulses[i] > 0 ); /* Update Rs in case no pulses were assigned */
297 : }
298 : }
299 :
300 5942199 : for ( i = 0; i < nb_sfm; i++ )
301 : {
302 5489336 : ord[i] = i;
303 5489336 : R[i] = R[i] * ( npulses[i] > 0 ); /* Update in case no pulses were assigned */
304 : }
305 :
306 452863 : get_max_pulses( sfm_start, sfm_end, ord, npulses, nb_sfm, pulse_vector, maxpulse );
307 :
308 452863 : fine_gain_pred( sfm_start, sfm_end, sfmsize, ord, npulses, maxpulse, R, nb_sfm, coefs_quant, pulse_vector, fg_pred, core );
309 :
310 452863 : fine_gain_quant( hBstr, ord, nb_sfm, gain_bits_array, fg_pred, gopt );
311 :
312 452863 : apply_gain( ord, sfm_start, sfm_end, nb_sfm, fg_pred, coefs_quant );
313 :
314 452863 : return ( pvq_bits + gain_bits_tot );
315 : }
316 :
317 :
318 : /*-------------------------------------------------------------------*
319 : * encode_energies()
320 : *
321 : *
322 : *--------------------------------------------------------------------*/
323 :
324 326987 : 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 326987 : int16_t index_phi = -1;
348 :
349 326987 : l_Np = Np >> 1;
350 326987 : r_Np = Np - l_Np;
351 :
352 326987 : l_enr = 0.0f;
353 326987 : l_bits = 0;
354 326987 : l_dim = 0;
355 657774 : for ( i = 0; i < l_Np; i++ )
356 : {
357 330787 : l_dim += dim_part[i];
358 : }
359 2918393 : for ( j = 0; j < l_dim; j++ )
360 : {
361 2591406 : l_enr += coefs[j] * coefs[j];
362 : }
363 326987 : r_enr = enr - l_enr;
364 326987 : r_dim = dim - l_dim;
365 :
366 326987 : obtainEnergyQuantizerDensity( dim, qband, &density );
367 326987 : obtainEnergyParameter( l_enr, r_enr, &phi );
368 326987 : rangeCoderFinalizationFBits( hPVQ->rc_num_bits, hPVQ->rc_range, &qzero );
369 326987 : densityAngle2RmsProjEnc( density, phi, &index_phi, &ir, &il, &oppRQ3 );
370 326987 : densityIndexSymbolEncode( hBstr, hPVQ, density, r_dim, l_dim, index_phi );
371 :
372 657774 : for ( i = 0; i < l_Np; i++ )
373 : {
374 330787 : g_part[i] = ( g_part[i] * il + 16384 ) >> 15;
375 : }
376 :
377 682269 : for ( i = l_Np; i < Np; i++ )
378 : {
379 355282 : g_part[i] = ( g_part[i] * ir + 16384 ) >> 15;
380 : }
381 :
382 326987 : 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 326987 : if ( l_Np > 1 )
385 : {
386 3635 : 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 323352 : E_part[0] = l_enr;
391 323352 : bits_part[0] = l_bits;
392 : }
393 :
394 326987 : if ( r_Np > 1 )
395 : {
396 27406 : 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 299581 : E_part[1] = r_enr;
401 299581 : bits_part[1] = r_bits;
402 : }
403 :
404 326987 : return;
405 : }
406 :
407 :
408 326987 : 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 326987 : 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 326987 : if ( ( 0xFFFE & density ) != 0 ) /* even */
427 : {
428 326099 : 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 326099 : angle = shl_o( angle, 1, &Overflow );
433 : #endif /* BASOP_NOGLOB */
434 326099 : angle = mult_r( angle, 20861 );
435 326099 : c = mult_r( density, angle );
436 :
437 326099 : densityPlOne = 1 + density;
438 326099 : densitySubC = density - c;
439 :
440 326099 : tot = 2 + density * densityPlOne; /* c==0, c==density*/
441 326099 : sym_freq = 1 + 2 * index_phi; /* c==density */
442 326099 : cum_freq = index_phi * index_phi; /* c==density*/
443 :
444 326099 : if ( c == 0 )
445 : {
446 0 : sym_freq = 2 * densityPlOne - sym_freq;
447 0 : cum_freq = 2 * index_phi * (densityPlOne) -cum_freq;
448 : }
449 326099 : else if ( densitySubC != 0 ) /* c n.eq. density */
450 : {
451 326099 : densitySubIndex = density - index_phi;
452 326099 : nearFlag = ( index_phi <= c );
453 :
454 326099 : tot = densityPlOne + density * c * ( densitySubC );
455 326099 : sym_freq = nearFlag ? ( 1 + 2 * index_phi * densitySubC ) : ( 1 + 2 * densitySubIndex * c );
456 326099 : 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 326099 : rc_encode( hBstr, hPVQ, cum_freq, sym_freq, tot );
460 : }
461 :
462 326987 : 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 3591296 : 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 3591296 : Np = (int16_t) ( intLimCDivPos( band_bits, 67 ) >> 2 );
491 3591296 : if ( band_bits - 268 * Np != 0 || Np == 0 ) /* L_msu */
492 : {
493 3587093 : Np++; /* ceil */
494 : }
495 3591296 : *bits = 0;
496 :
497 3591296 : if ( Np < MAX_SPLITS && ( band_bits - ( 8 * sfmsize * THR_ADD_SPLIT ) > 0 ) )
498 : {
499 1956 : Npart = (int16_t) intLimCDivPos( sfmsize, Np );
500 1956 : *bits = 8;
501 1956 : Emean = 0;
502 1956 : k = 0;
503 7997 : for ( i = 0; i < Np; i++ )
504 : {
505 6041 : E[i] = EPSILON;
506 22188 : for ( j = 0; j < Npart; j++, k++ )
507 : {
508 16147 : E[i] += y[k] * y[k];
509 : }
510 6041 : E[i] = (float) 30 - norm_l( (Word32) max( E[i], 1 ) ); /* a 0 input integer Ei yields a zeroed log2(Ei) out */
511 6041 : Emean += E[i];
512 : }
513 1956 : Emean /= Np;
514 :
515 1956 : max_dev = -1;
516 7997 : for ( i = 0; i < Np; i++ )
517 : {
518 6041 : tmp = (float) fabs( E[i] - Emean );
519 6041 : if ( tmp > max_dev )
520 : {
521 2499 : max_dev = tmp;
522 : }
523 : }
524 :
525 1956 : if ( max_dev > ( 32 - band_bits / ( 8 * Np ) ) )
526 : {
527 352 : rc_enc_bits( hBstr, hPVQ, 1, 1 );
528 352 : Np += 1;
529 : }
530 : else
531 : {
532 1604 : rc_enc_bits( hBstr, hPVQ, 0, 1 );
533 : }
534 : }
535 :
536 3591296 : Np = max( Np, (int16_t) ( ceil( (float) sfmsize / PVQ_MAX_BAND_SIZE ) ) );
537 3591296 : Np = min( MAX_SPLITS, Np );
538 3591296 : Np = min( (int16_t) floor( (float) sfmsize / MIN_BAND_SIZE ), Np );
539 :
540 3591296 : return Np;
541 : }
542 :
543 :
544 : #define EPSILON_obtainEnergyParameter 0.00373f /**/
545 : #define C_obtainEnergyParameter ( (float) ( 1 << 15 ) / EVS_PI + EPSILON_obtainEnergyParameter )
546 :
547 326987 : 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 326987 : if ( !Eopp )
554 : {
555 242 : *param = 0;
556 242 : return;
557 : }
558 326745 : if ( !Enear )
559 : {
560 0 : *param = 1 << 14;
561 0 : return;
562 : }
563 326745 : *param = (int16_t) ( C_obtainEnergyParameter * atan2( (float) sqrt( Eopp ), (float) sqrt( Enear ) ) + 0.5f );
564 :
565 326745 : return;
566 : }
567 : #undef C_obtainEnergyParameter
568 : #undef EPSILON_obtainEnergyParameter
|