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 "prot.h"
44 : #include "rom_com.h"
45 : #include "wmc_auto.h"
46 :
47 : /*-------------------------------------------------------------------*
48 : * en_band_quant()
49 : *
50 : * Quantize the band envelop
51 : *-------------------------------------------------------------------*/
52 :
53 : /*! r: quantization index */
54 1466 : static int16_t en_band_quant(
55 : float *en_band, /* i/o: (un)quantized envelope value */
56 : const float *env_code, /* i : envelope codebook */
57 : const int16_t N /* i : codebook dimension */
58 : )
59 : {
60 : float maxerr, err;
61 : int16_t i, j, ind;
62 :
63 1466 : maxerr = FLT_MAX;
64 1466 : ind = 0;
65 :
66 62106 : for ( i = 0; i < N; i++ )
67 : {
68 60640 : err = FLT_MIN;
69 181920 : for ( j = 0; j < 2; j++ )
70 : {
71 121280 : err += ( en_band[j] - env_code[i * 2 + j] ) * ( en_band[j] - env_code[i * 2 + j] );
72 : }
73 60640 : if ( err < maxerr )
74 : {
75 16844 : maxerr = err;
76 16844 : ind = i;
77 : }
78 : }
79 :
80 1466 : en_band[0] = env_code[2 * ind];
81 1466 : en_band[1] = env_code[2 * ind + 1];
82 :
83 1466 : return ( ind );
84 : }
85 :
86 : /*-------------------------------------------------------------------*
87 : * swb_bwe_enc_hr()
88 : *
89 : * HR SWB BWE encoder
90 : *-------------------------------------------------------------------*/
91 :
92 669 : void swb_bwe_enc_hr(
93 : Encoder_State *st, /* i/o: encoder state structure */
94 : const float *new_input, /* i : input signal */
95 : const int16_t input_frame, /* i : frame length */
96 : const int16_t unbits /* i : number of core unused bits */
97 : )
98 : {
99 : int16_t i, j, k, nBits, nBits_total, nBits_block, Nsv, Nsv2, width_noncoded;
100 : int16_t is_transient, pos;
101 : int16_t x_norm[NSV_MAX * ( WIDTH_BAND + 1 )], x_norm1[NSV_MAX * ( WIDTH_BAND + 1 )];
102 : float t_audio[L_FRAME48k], t_audio_tmp[L_FRAME48k];
103 : float gain, gain2, en_band[N_BANDS_BWE_HR];
104 : int16_t ind1, ind2;
105 : int16_t nq[NSV_MAX], nq2[NSV_MAX];
106 : float tmpF, min_env;
107 : float en_noncoded;
108 669 : BSTR_ENC_HANDLE hBstr = st->hBstr;
109 :
110 : /*---------------------------------------------------------------------*
111 : * initializations
112 : *---------------------------------------------------------------------*/
113 :
114 669 : ind2 = 0; /* only to suppress warnings */
115 669 : Nsv2 = 0; /* only to suppress warnings */
116 669 : gain2 = 0; /* only to suppress warnings */
117 669 : en_noncoded = 0; /* only to suppress warnings */
118 :
119 : /* reset memories in case that last frame was a different technology */
120 669 : if ( st->last_core == HQ_CORE || st->last_extl != st->extl )
121 : {
122 11 : set_f( st->hBWE_FD->old_wtda_swb, 0, L_FRAME48k );
123 : }
124 :
125 : /* calculate SWB BWE bit-budget (extension layer bitrate + AVQ unused bits from the core layer) */
126 669 : nBits = (int16_t) ( st->extl_brate ) / FRAMES_PER_SEC + unbits;
127 669 : nBits_total = nBits;
128 :
129 :
130 : /*---------------------------------------------------------------------*
131 : * detect transient frames
132 : *---------------------------------------------------------------------*/
133 :
134 669 : is_transient = detect_transient( st, new_input, input_frame );
135 669 : push_indice( hBstr, IND_HR_IS_TRANSIENT, is_transient, 1 );
136 :
137 : /*---------------------------------------------------------------------*
138 : * OLA and MDCT
139 : *---------------------------------------------------------------------*/
140 :
141 669 : wtda( new_input, t_audio_tmp, st->hBWE_FD->old_wtda_swb, ALDO_WINDOW, ALDO_WINDOW, input_frame );
142 :
143 669 : direct_transform( t_audio_tmp, t_audio, is_transient, input_frame, st->element_mode );
144 :
145 669 : if ( is_transient )
146 : {
147 64 : nBits = -1; /* is_transient flag */
148 64 : nBits_block = nBits_total / NUM_TIME_SWITCHING_BLOCKS;
149 64 : nBits += nBits_total % NUM_TIME_SWITCHING_BLOCKS;
150 :
151 : /* set width of noncoded (blind estimated) spectrum */
152 64 : if ( st->extl == SWB_BWE_HIGHRATE )
153 : {
154 64 : width_noncoded = L_FRAME32k / NUM_TIME_SWITCHING_BLOCKS - NUM_TRANS_END_FREQ_COEF;
155 : }
156 : else /* st->extl == FB_BWE_HIGHRATE */
157 : {
158 0 : width_noncoded = ( 2 * END_FREQ_BWE_FULL_FB / FRAMES_PER_SEC ) / NUM_TIME_SWITCHING_BLOCKS - NUM_TRANS_END_FREQ_COEF;
159 : }
160 :
161 : /*---------------------------------------------------------------------*
162 : * transient frames: processing in blocks (subframes)
163 : *---------------------------------------------------------------------*/
164 :
165 320 : for ( k = 0; k < NUM_TIME_SWITCHING_BLOCKS; k++ )
166 : {
167 256 : nBits += nBits_block;
168 :
169 : /* compute energy of noncoded (14.4-20kHz) spectrum */
170 256 : if ( st->extl == FB_BWE_HIGHRATE )
171 : {
172 0 : en_noncoded = sum2_f( t_audio + k * input_frame / NUM_TIME_SWITCHING_BLOCKS + NUM_TRANS_END_FREQ_COEF, width_noncoded ) + 0.001f;
173 0 : en_noncoded = (float) sqrt( en_noncoded / width_noncoded );
174 : }
175 :
176 : /* keep only frequencies in interest */
177 256 : set_f( t_audio + k * input_frame / NUM_TIME_SWITCHING_BLOCKS, 0, NUM_TRANS_START_FREQ_COEF );
178 256 : set_f( t_audio + k * input_frame / NUM_TIME_SWITCHING_BLOCKS + L_FRAME32k / NUM_TIME_SWITCHING_BLOCKS, 0, ( input_frame - L_FRAME32k ) / NUM_TIME_SWITCHING_BLOCKS );
179 :
180 : /*---------------------------------------------------------------------*
181 : * global gain coding
182 : *---------------------------------------------------------------------*/
183 :
184 : /* compute and quantize global energy */
185 256 : gain = sum2_f( t_audio + k * input_frame / NUM_TIME_SWITCHING_BLOCKS + NUM_TRANS_START_FREQ_COEF, WIDTH_TRANS_FREQ_COEF * N_BANDS_TRANS_BWE_HR ) + 0.001f;
186 256 : gain = (float) sqrt( gain ) / ( WIDTH_TRANS_FREQ_COEF * N_BANDS_TRANS_BWE_HR );
187 256 : ind1 = gain_quant( &gain, MIN_GLOB_GAIN_BWE_HR, MAX_GLOB_GAIN_BWE_HR, NBITS_GLOB_GAIN_BWE_HR );
188 :
189 256 : push_indice( hBstr, IND_HR_GAIN, ind1, NBITS_GLOB_GAIN_BWE_HR );
190 256 : nBits -= NBITS_GLOB_GAIN_BWE_HR;
191 :
192 : /* normalization with global gain */
193 256 : tmpF = 1 / gain;
194 17664 : for ( i = 0; i < WIDTH_TRANS_FREQ_COEF * N_BANDS_TRANS_BWE_HR; i++ )
195 : {
196 17408 : t_audio[NUM_TRANS_START_FREQ_COEF + k * input_frame / NUM_TIME_SWITCHING_BLOCKS + i] *= tmpF;
197 : }
198 :
199 : /*---------------------------------------------------------------------*
200 : * envelope coding
201 : *---------------------------------------------------------------------*/
202 :
203 : /* compute energy per band */
204 768 : for ( i = 0; i < N_BANDS_TRANS_BWE_HR; i++ )
205 : {
206 512 : en_band[i] = sum2_f( t_audio + k * input_frame / NUM_TIME_SWITCHING_BLOCKS + NUM_TRANS_START_FREQ_COEF + i * WIDTH_TRANS_FREQ_COEF, WIDTH_TRANS_FREQ_COEF ) + 0.001f;
207 512 : en_band[i] = (float) sqrt( en_band[i] / ( WIDTH_TRANS_FREQ_COEF ) );
208 : }
209 :
210 : /* Q energy per band */
211 256 : if ( k == 0 )
212 : {
213 64 : ind1 = en_band_quant( en_band, swb_hr_env_code3, NUM_ENVLOPE_CODE_HR_TR );
214 64 : push_indice( hBstr, IND_HR_ENVELOPE, ind1, NBITS_ENVELOPE_BWE_HR_TR );
215 64 : nBits -= NBITS_ENVELOPE_BWE_HR_TR;
216 64 : ind2 = ind1;
217 : }
218 : else
219 : {
220 192 : if ( ind2 < NUM_ENVLOPE_CODE_HR_TR2 )
221 : {
222 42 : ind1 = en_band_quant( en_band, swb_hr_env_code3, NUM_ENVLOPE_CODE_HR_TR2 );
223 : }
224 : else
225 : {
226 150 : ind1 = en_band_quant( en_band, swb_hr_env_code3 + ( NUM_ENVLOPE_CODE_HR_TR2 * 2 ), NUM_ENVLOPE_CODE_HR_TR2 );
227 : }
228 :
229 192 : push_indice( hBstr, IND_HR_ENVELOPE, ind1, NBITS_ENVELOPE_BWE_HR_TR - 1 );
230 192 : nBits -= ( NBITS_ENVELOPE_BWE_HR_TR - 1 );
231 : }
232 :
233 : /* normalize spectrum per bands */
234 768 : for ( i = 0; i < N_BANDS_TRANS_BWE_HR; i++ )
235 : {
236 512 : tmpF = 1 / en_band[i];
237 17920 : for ( j = 0; j < WIDTH_TRANS_FREQ_COEF; j++ )
238 : {
239 17408 : t_audio[k * input_frame / NUM_TIME_SWITCHING_BLOCKS + NUM_TRANS_START_FREQ_COEF + i * WIDTH_TRANS_FREQ_COEF + j] *= tmpF;
240 : }
241 : }
242 :
243 : /*---------------------------------------------------------------------*
244 : * estimate energy of noncoded spectrum (14.4-20kHz)
245 : *---------------------------------------------------------------------*/
246 :
247 256 : if ( st->extl == SWB_BWE_HIGHRATE )
248 : {
249 256 : en_noncoded = en_band[N_BANDS_TRANS_BWE_HR - 1];
250 : }
251 : else /* st->extl == FB_BWE_HIGHRATE */
252 : {
253 0 : en_noncoded /= ( gain * en_band[N_BANDS_TRANS_BWE_HR - 1] );
254 :
255 0 : ind1 = 0;
256 0 : if ( en_noncoded < BWE_HR_TRANS_EN_LIMIT1 )
257 : {
258 0 : ind1 = 1;
259 0 : en_noncoded = en_band[N_BANDS_TRANS_BWE_HR - 1] * BWE_HR_TRANS_EN_LIMIT1;
260 : }
261 0 : else if ( en_noncoded < BWE_HR_TRANS_EN_LIMIT2 )
262 : {
263 0 : ind1 = 2;
264 0 : en_noncoded = en_band[N_BANDS_TRANS_BWE_HR - 1] * BWE_HR_TRANS_EN_LIMIT2;
265 : }
266 0 : else if ( en_noncoded < BWE_HR_TRANS_EN_LIMIT3 )
267 : {
268 0 : ind1 = 3;
269 0 : en_noncoded = en_band[N_BANDS_TRANS_BWE_HR - 1] * BWE_HR_TRANS_EN_LIMIT3;
270 : }
271 : else
272 : {
273 0 : en_noncoded = en_band[N_BANDS_TRANS_BWE_HR - 1];
274 : }
275 0 : push_indice( hBstr, IND_HR_HF_GAIN, ind1, NBITS_HF_GAIN_BWE_HR );
276 0 : nBits -= NBITS_HF_GAIN_BWE_HR;
277 : }
278 :
279 : /*---------------------------------------------------------------------*
280 : * AVQ coding (quantize normalized spectrum)
281 : *---------------------------------------------------------------------*/
282 :
283 256 : Nsv = ( NUM_TRANS_END_FREQ_COEF - NUM_TRANS_START_FREQ_COEF ) / WIDTH_BAND;
284 256 : AVQ_cod( t_audio + k * input_frame / NUM_TIME_SWITCHING_BLOCKS + NUM_TRANS_START_FREQ_COEF, x_norm, nBits, Nsv );
285 256 : AVQ_encmux( hBstr, st->extl, x_norm, &nBits, Nsv, nq, 0, Nsv - 1 );
286 : }
287 : }
288 : else /* !is_transient */
289 : {
290 605 : nBits--; /* is_transient flag */
291 :
292 : /*---------------------------------------------------------------------*
293 : * processing of normal (non-transient) frames
294 : *---------------------------------------------------------------------*/
295 :
296 : /* set width of noncoded (blind estimated) spectrum */
297 605 : if ( st->extl == SWB_BWE_HIGHRATE )
298 : {
299 605 : width_noncoded = L_FRAME32k - NUM_NONTRANS_END_FREQ_COEF;
300 : }
301 : else /* st->extl == FB_BWE_HIGHRATE */
302 : {
303 0 : width_noncoded = 2 * END_FREQ_BWE_FULL_FB / FRAMES_PER_SEC - NUM_NONTRANS_END_FREQ_COEF;
304 : }
305 :
306 : /* compute energy of noncoded (14.4-20kHz) spectrum */
307 605 : if ( st->extl == FB_BWE_HIGHRATE )
308 : {
309 0 : en_noncoded = sum2_f( t_audio + NUM_NONTRANS_END_FREQ_COEF, width_noncoded ) + 0.001f;
310 0 : en_noncoded = (float) sqrt( en_noncoded / width_noncoded );
311 : }
312 :
313 : /* keep only frequencies in interest */
314 605 : set_f( t_audio, 0, NUM_NONTRANS_START_FREQ_COEF );
315 605 : set_f( t_audio + NUM_NONTRANS_END_FREQ_COEF, 0, input_frame - NUM_NONTRANS_END_FREQ_COEF );
316 :
317 : /*---------------------------------------------------------------------*
318 : * global gain coding
319 : *---------------------------------------------------------------------*/
320 :
321 : /* compute and quantize global gain */
322 605 : gain = sum2_f( t_audio + NUM_NONTRANS_START_FREQ_COEF, WIDTH_NONTRANS_FREQ_COEF * N_BANDS_BWE_HR ) + 0.001f;
323 605 : gain = (float) sqrt( gain ) / ( WIDTH_NONTRANS_FREQ_COEF * N_BANDS_BWE_HR );
324 605 : ind1 = gain_quant( &gain, MIN_GLOB_GAIN_BWE_HR, MAX_GLOB_GAIN_BWE_HR, NBITS_GLOB_GAIN_BWE_HR );
325 :
326 605 : push_indice( hBstr, IND_HR_GAIN, ind1, NBITS_GLOB_GAIN_BWE_HR );
327 605 : nBits -= NBITS_GLOB_GAIN_BWE_HR;
328 :
329 : /* normalization with global gain */
330 605 : tmpF = 1 / gain;
331 165165 : for ( i = 0; i < WIDTH_NONTRANS_FREQ_COEF * N_BANDS_BWE_HR; i++ )
332 : {
333 164560 : t_audio[NUM_NONTRANS_START_FREQ_COEF + i] *= tmpF;
334 : }
335 :
336 : /*---------------------------------------------------------------------*
337 : * envelope coding
338 : *---------------------------------------------------------------------*/
339 :
340 : /* compute energy per band */
341 3025 : for ( i = 0; i < N_BANDS_BWE_HR; i++ )
342 : {
343 2420 : en_band[i] = sum2_f( t_audio + NUM_NONTRANS_START_FREQ_COEF + i * WIDTH_NONTRANS_FREQ_COEF, WIDTH_NONTRANS_FREQ_COEF ) + 0.001f;
344 2420 : en_band[i] = (float) sqrt( en_band[i] / WIDTH_NONTRANS_FREQ_COEF );
345 : }
346 :
347 : /* Q energy per band */
348 605 : ind1 = en_band_quant( en_band, swb_hr_env_code1, NUM_ENVLOPE_CODE_HR1 );
349 605 : ind2 = en_band_quant( en_band + 2, swb_hr_env_code2, NUM_ENVLOPE_CODE_HR2 );
350 :
351 605 : push_indice( hBstr, IND_HR_ENVELOPE, ind1, NBITS_ENVELOPE_BWE_HR1 );
352 605 : push_indice( hBstr, IND_HR_ENVELOPE, ind2, NBITS_ENVELOPE_BWE_HR2 );
353 :
354 605 : nBits -= NBITS_ENVELOPE_BWE_HR1 + NBITS_ENVELOPE_BWE_HR2;
355 :
356 : /* normalize spectrum per bands */
357 3025 : for ( i = 0; i < N_BANDS_BWE_HR; i++ )
358 : {
359 2420 : tmpF = 1 / en_band[i];
360 166980 : for ( j = 0; j < WIDTH_NONTRANS_FREQ_COEF; j++ )
361 : {
362 164560 : t_audio[NUM_NONTRANS_START_FREQ_COEF + i * WIDTH_NONTRANS_FREQ_COEF + j] *= tmpF;
363 : }
364 : }
365 :
366 : /*---------------------------------------------------------------------*
367 : * choose sub-bands to be quantized
368 : *---------------------------------------------------------------------*/
369 :
370 : /* find the subband with the min envelope */
371 605 : pos = minimum( en_band, N_BANDS_BWE_HR, &min_env );
372 :
373 : /* decide the spectrum to be quantized */
374 605 : if ( nBits_total > NBITS_THRESH_BWE_HR )
375 : {
376 0 : i = NUM_NONTRANS_END_FREQ_COEF - NUM_NONTRANS_START_FREQ_COEF;
377 0 : mvr2r( t_audio + NUM_NONTRANS_START_FREQ_COEF, t_audio_tmp, NUM_NONTRANS_END_FREQ_COEF - NUM_NONTRANS_START_FREQ_COEF );
378 : }
379 : else
380 : {
381 : /* reorder the spectrum */
382 605 : ind1 = ( pos * 64 + pos / 2 * WIDTH_BAND );
383 605 : mvr2r( t_audio + NUM_NONTRANS_START_FREQ_COEF, t_audio_tmp, ind1 );
384 :
385 605 : ind2 = ( ( pos + 1 ) * 64 + ( pos + 1 ) / 2 * WIDTH_BAND );
386 605 : mvr2r( t_audio + NUM_NONTRANS_START_FREQ_COEF + ind2, t_audio_tmp + ind1, NUM_NONTRANS_END_FREQ_COEF - NUM_NONTRANS_START_FREQ_COEF - ind2 );
387 :
388 605 : i = ind1 + NUM_NONTRANS_END_FREQ_COEF - NUM_NONTRANS_START_FREQ_COEF - ind2;
389 : }
390 :
391 : /*---------------------------------------------------------------------*
392 : * estimate energy of noncoded spectrum (14.4-20kHz)
393 : *---------------------------------------------------------------------*/
394 :
395 605 : if ( st->extl == SWB_BWE_HIGHRATE )
396 : {
397 605 : en_noncoded = 0.5f * min_env;
398 : }
399 : else /* st->extl == FB_BWE_HIGHRATE */
400 : {
401 0 : en_noncoded /= ( gain * min_env );
402 :
403 0 : ind1 = 0;
404 0 : if ( en_noncoded < BWE_HR_NONTRANS_EN_LIMIT1 )
405 : {
406 0 : ind1 = 1;
407 0 : en_noncoded = 0.5f * min_env * BWE_HR_NONTRANS_EN_LIMIT1;
408 : }
409 0 : else if ( en_noncoded > BWE_HR_NONTRANS_EN_LIMIT2 )
410 : {
411 0 : ind1 = 2;
412 0 : en_noncoded = min_env * BWE_HR_NONTRANS_EN_LIMIT2;
413 : }
414 0 : else if ( en_noncoded > BWE_HR_NONTRANS_EN_LIMIT3 )
415 : {
416 0 : ind1 = 3;
417 0 : en_noncoded = min_env * BWE_HR_NONTRANS_EN_LIMIT3;
418 : }
419 : else
420 : {
421 0 : en_noncoded = 0.5f * min_env;
422 : }
423 :
424 0 : push_indice( hBstr, IND_HR_HF_GAIN, ind1, NBITS_HF_GAIN_BWE_HR );
425 0 : nBits -= NBITS_HF_GAIN_BWE_HR;
426 : }
427 :
428 : /*---------------------------------------------------------------------*
429 : * AVQ coding (quantize normalized spectrum)
430 : *---------------------------------------------------------------------*/
431 :
432 605 : Nsv = i / WIDTH_BAND;
433 605 : AVQ_cod( t_audio_tmp, x_norm, nBits, Nsv );
434 605 : AVQ_encmux( hBstr, st->extl, x_norm, &nBits, Nsv, nq, 0, Nsv - 1 );
435 : /*---------------------------------------------------------------------*
436 : * second stage coding
437 : *---------------------------------------------------------------------*/
438 :
439 605 : if ( nBits >= 9 + NBITS_GLOB_GAIN_BWE_HR && sum_s( nq, Nsv ) > 0 )
440 : {
441 : /* select spectrum of the second stage coding */
442 592 : k = 0;
443 15465 : for ( i = 0; i < Nsv; i++ )
444 : {
445 14873 : if ( nq[i] == 0 )
446 : {
447 26451 : for ( j = 0; j < WIDTH_BAND; j++ )
448 : {
449 23512 : t_audio[k++] = t_audio_tmp[i * WIDTH_BAND + j];
450 : }
451 : }
452 : }
453 :
454 15465 : for ( i = 0; i < Nsv; i++ )
455 : {
456 14873 : if ( nq[i] != 0 )
457 : {
458 107406 : for ( j = 0; j < WIDTH_BAND; j++ )
459 : {
460 95472 : t_audio[k++] = t_audio_tmp[i * WIDTH_BAND + j] - x_norm[i * WIDTH_BAND + j];
461 : }
462 : }
463 : }
464 :
465 : /* calculate the number of subbands according to the rest bits */
466 592 : if ( nBits > 396 )
467 : {
468 0 : Nsv2 = 33;
469 : }
470 : else
471 : {
472 592 : Nsv2 = nBits / 12;
473 : }
474 :
475 : /* second stage global gain estimation and coding */
476 592 : gain2 = sum2_f( t_audio, Nsv2 * WIDTH_BAND ) + 0.001f;
477 592 : gain2 = (float) ( 16 * sqrt( gain2 / ( Nsv2 * WIDTH_BAND ) ) );
478 592 : ind1 = gain_quant( &gain2, MIN_GLOB_GAIN_BWE_HR, MAX_GLOB_GAIN_BWE_HR, NBITS_GLOB_GAIN_BWE_HR );
479 :
480 592 : push_indice( hBstr, IND_HR_GAIN, ind1, NBITS_GLOB_GAIN_BWE_HR );
481 592 : nBits -= NBITS_GLOB_GAIN_BWE_HR;
482 :
483 : /* normalize with global gain */
484 592 : gain2 *= 0.0625f; /* 1/16 */
485 592 : tmpF = 1 / gain2;
486 21624 : for ( i = 0; i < Nsv2 * WIDTH_BAND; i++ )
487 : {
488 21032 : t_audio[i] *= tmpF;
489 : }
490 :
491 592 : set_s( nq2, 0, Nsv );
492 :
493 592 : AVQ_cod( t_audio, x_norm1, nBits, Nsv2 );
494 592 : AVQ_encmux( hBstr, st->extl, x_norm1, &nBits, Nsv2, nq2, 0, Nsv2 - 1 );
495 : }
496 : }
497 :
498 : /* write unused bits */
499 1359 : while ( nBits > 0 )
500 : {
501 690 : i = min( nBits, 16 );
502 690 : push_indice( hBstr, IND_UNUSED, 0, i );
503 690 : nBits -= i;
504 : }
505 :
506 :
507 669 : return;
508 : }
|