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 : #include <stdint.h>
34 : #include "options.h"
35 : #include "prot_fx.h"
36 : #include "rom_com.h"
37 :
38 : #define MIN_BITS_FIX 0 /* QRk=18 */
39 : #define HQ_16k40_BIT ( HQ_16k40 / 50 ) /* 16400/50=328 */
40 : #define Qbf 14 /* Q value for bits_fact */
41 : #define C1_QRk ( 1 << SWB_BWE_LR_QRk ) /* 1 */
42 : #define C1_Qbf ( 1 << Qbf ) /* 1 */
43 : #define BITS_FACT_1p10 18022 /* (Word16)(1.10f*(float)pow(2, Qbf)+0.5f) */
44 : #define BITS_FACT_1p05 17203 /* (Word16)(1.05f*(float)pow(2, Qbf)+0.5f) */
45 : #define BITS_FACT_1p00 16384 /* (Word16)(1.00f*(float)pow(2, Qbf)+0.5f) */
46 : #define BITS_FACT_0p97 15892 /* (Word16)(0.97f*(float)pow(2, Qbf)+0.5f) */
47 : #define BITS_FACT_0p92 15073 /* (Word16)(0.92f*(float)pow(2, Qbf)+0.5f) */
48 :
49 : /*-------------------------------------------------------------------*
50 : * Bits2indvsb()
51 : *
52 : * Bit allocation to individual SB's in a group
53 : *-------------------------------------------------------------------*/
54 :
55 0 : void Bits2indvsb_fx(
56 : const Word32 *L_be, /* i : Qbe Band Energy of sub-band */
57 : const Word16 start_band, /* i : Q0 start band indices */
58 : const Word16 end_band, /* i : Q0 end band indices */
59 : const Word16 Bits, /* i : Q0 Total number of bits allocated to a group */
60 : const Word32 L_Bits_needed, /* i : QRk smallest bit number for allocation in group */
61 : Word32 *L_Rsubband, /* o : QRk bit allocation of sub-band */
62 : Word16 *p2aflags_fx /* i/o: Q0 peaky/noise subband flag */
63 : )
64 : {
65 : Word16 i, j, k;
66 : Word32 L_R_temp[14]; /* QRk = QL_Rsubband; */
67 : Word16 Ravg_fx;
68 : Word16 QRavg;
69 :
70 : const Word32 *L_y_ptr;
71 : Word32 *L_R_ptr;
72 :
73 : Word16 Bits_avg_fx;
74 : Word16 QBavg;
75 : Word16 scale_fact_fx;
76 :
77 : Word16 band_num_fx;
78 : Word16 index_fx[14];
79 :
80 : Word16 y_index_fx[14];
81 :
82 : Word16 be_sum_fx; /* Q0 */
83 :
84 : Word16 exp_normn, exp_normd;
85 : Word16 enr_diffcnt_fx;
86 : Word16 th_5_fx;
87 : Word16 Rcnt_fx;
88 :
89 : Word16 be_cnt_fx;
90 : Word16 *p2aflags_fx_ptr;
91 :
92 : Word32 L_temp1;
93 : Word32 L_temp2;
94 :
95 0 : band_num_fx = sub( end_band, start_band ); /* Q0 */
96 0 : L_y_ptr = L_be + start_band; /* Qbe */
97 0 : L_R_ptr = L_Rsubband + start_band; /* QRk */
98 0 : p2aflags_fx_ptr = p2aflags_fx + start_band; /* Q0 */
99 :
100 0 : FOR( i = 0; i < band_num_fx; i++ )
101 : {
102 0 : y_index_fx[i] = extract_h( L_shr( L_y_ptr[i], sub( SWB_BWE_LR_Qbe, 16 ) ) ); /* Q16 */
103 0 : move16();
104 0 : index_fx[i] = i;
105 0 : move16();
106 : }
107 :
108 :
109 : /* Rearrange norm vector in decreasing order */
110 0 : reordvct_fx( y_index_fx, band_num_fx, index_fx );
111 :
112 0 : be_sum_fx = 0;
113 0 : move16();
114 0 : be_cnt_fx = 0;
115 0 : move16();
116 0 : FOR( j = 0; j < band_num_fx; j++ )
117 : {
118 0 : test();
119 0 : IF( y_index_fx[j] <= 0 || p2aflags_fx_ptr[index_fx[j]] == 0 )
120 : {
121 0 : y_index_fx[j] = 0;
122 0 : move16();
123 0 : L_R_temp[j] = L_deposit_l( 0 );
124 0 : move32();
125 : }
126 : ELSE
127 : {
128 0 : L_R_temp[j] = C1_QRk; /* QRk */
129 0 : move32(); /* filled not zero value */
130 0 : be_cnt_fx = add( be_cnt_fx, 1 ); /* Q0 */
131 : }
132 : }
133 :
134 0 : i = sub( be_cnt_fx, 1 );
135 0 : FOR( k = 0; k <= i; k++ )
136 : {
137 0 : if ( L_R_temp[k] > 0 )
138 : {
139 0 : be_sum_fx = add( be_sum_fx, y_index_fx[k] );
140 : }
141 : }
142 0 : QBavg = 0;
143 0 : move16();
144 :
145 : /*Ravg = (float) be_sum/be_cnt;*/
146 0 : Ravg_fx = 0;
147 0 : move16();
148 0 : QRavg = 0;
149 0 : move16();
150 0 : IF( be_cnt_fx != 0x0 )
151 : {
152 0 : exp_normn = norm_s( be_sum_fx );
153 0 : exp_normn = sub( exp_normn, 1 );
154 0 : exp_normd = norm_s( be_cnt_fx );
155 0 : Ravg_fx = div_s( shl( be_sum_fx, exp_normn ), shl( be_cnt_fx, exp_normd ) );
156 :
157 0 : Ravg_fx = shr( Ravg_fx, 2 ); /* safe shift */
158 0 : QRavg = add( sub( exp_normn, exp_normd ), 15 - 2 );
159 : }
160 :
161 0 : enr_diffcnt_fx = 0;
162 0 : move16();
163 0 : th_5_fx = shl( 5, QRavg );
164 0 : FOR( j = 0; j < be_cnt_fx; j++ )
165 : {
166 0 : if ( GT_16( abs_s( sub( Ravg_fx, shl( y_index_fx[j], QRavg ) ) ), th_5_fx ) )
167 : {
168 0 : enr_diffcnt_fx = add( enr_diffcnt_fx, 1 );
169 : }
170 : }
171 :
172 0 : scale_fact_fx = 19661;
173 0 : move16(); /* 0.60f 19660.8(Q15) */
174 0 : if ( enr_diffcnt_fx > 0 )
175 : {
176 0 : scale_fact_fx = 11468;
177 0 : move16(); /* 0.35f 11468.8(Q15) */
178 : }
179 :
180 : /* Bits allocation to individual SB's in a group based on Band Energies */
181 0 : FOR( j = 0; j < be_cnt_fx; j++ )
182 : {
183 0 : Rcnt_fx = add( i, 1 );
184 :
185 : /* Ravg = (float) be_sum/Rcnt; */
186 0 : exp_normn = norm_s( be_sum_fx );
187 0 : exp_normn = sub( exp_normn, 1 );
188 0 : exp_normd = norm_s( Rcnt_fx );
189 0 : Ravg_fx = div_s( shl( be_sum_fx, exp_normn ), shl( Rcnt_fx, exp_normd ) );
190 0 : Ravg_fx = shr( Ravg_fx, 2 ); /* safe shift exp_normn - exp_normd + 13*/
191 0 : QRavg = add( sub( exp_normn, exp_normd ), 15 - 2 );
192 :
193 0 : if ( be_sum_fx <= 0 )
194 : {
195 0 : be_sum_fx = 1;
196 0 : move16();
197 : }
198 :
199 : /* Bits_avg = (float) Bits/(be_sum+EPSILON); */
200 0 : Bits_avg_fx = 0;
201 0 : move16();
202 0 : QBavg = 0;
203 0 : move16();
204 0 : IF( Bits != 0 )
205 : {
206 0 : exp_normn = norm_s( Bits );
207 0 : exp_normn = sub( exp_normn, 1 );
208 0 : exp_normd = norm_s( be_sum_fx );
209 0 : Bits_avg_fx = div_s( shl( Bits, exp_normn ), shl( be_sum_fx, exp_normd ) );
210 0 : Bits_avg_fx = shr( Bits_avg_fx, 2 ); /* safe_shift exp_normn - exp_normd + 13*/
211 0 : QBavg = add( sub( exp_normn, exp_normd ), 15 - 2 );
212 : }
213 0 : FOR( k = 0; k <= i; k++ )
214 : {
215 0 : IF( L_R_temp[k] > 0 ) /* Rtemp -> SWB_BWE_LR_QRk */
216 : {
217 : /* Allocate more bits to SB, if SB bandenergy is higher than average energy */
218 : /* R_temp[k] = (float)( Bits_avg * y_index[k]+( scale_fact * (y_index[k] - Ravg))); */
219 0 : L_temp1 = L_mult( Bits_avg_fx, y_index_fx[k] ); /* QBavg+1 */
220 0 : L_temp2 = L_mult( scale_fact_fx, sub( shl( y_index_fx[k], QRavg ), Ravg_fx ) ); /* 15+QRavg+1 */
221 0 : L_R_temp[k] = L_add( L_shr( L_temp1, sub( add( QBavg, 1 ), SWB_BWE_LR_QRk ) ), L_shr( L_temp2, sub( add( QRavg, 16 ), SWB_BWE_LR_QRk ) ) ); /* SWB_BWE_LR_QRk */
222 : }
223 : }
224 0 : IF( LT_32( L_R_temp[i], L_Bits_needed ) )
225 : {
226 0 : L_R_temp[i] = L_deposit_l( 0 );
227 :
228 0 : p2aflags_fx_ptr[index_fx[i]] = 0;
229 0 : move16();
230 :
231 : /* be_sum -= y_index[i]; */
232 0 : be_sum_fx = sub( be_sum_fx, y_index_fx[i] );
233 :
234 0 : i = sub( i, 1 );
235 : }
236 : ELSE
237 : {
238 0 : BREAK;
239 : }
240 : }
241 :
242 : /* Rearrange the bit allocation to align with original */
243 0 : FOR( k = 0; k < band_num_fx; k++ )
244 : {
245 0 : j = index_fx[k];
246 0 : move16();
247 0 : L_R_ptr[j] = L_R_temp[k]; /* SWB_BWE_LR_QRk */
248 0 : move32();
249 : }
250 :
251 0 : return;
252 : }
253 :
254 : /*-------------------------------------------------------------------*
255 : * hq2_bit_alloc_har()
256 : *
257 : * Bit allocation mechanism for HQ_HARMONIC mode
258 : *-------------------------------------------------------------------*/
259 :
260 0 : void hq2_bit_alloc_har_fx(
261 : const Word32 *L_y, /* i : Qbe band energy of sub-vectors */
262 : Word16 B_fx, /* i : Q0 number of available bits */
263 : const Word16 N_fx, /* i : Q0 number of sub-vectors */
264 : Word32 *L_Rsubband, /* o : QRk sub-band bit-allocation vector */
265 : Word16 p2a_bands_fx, /* i : Q0 highfreq bands */
266 : const Word32 L_core_brate, /* i : Q0 core bit rate */
267 : Word16 p2a_flags_fx[], /* i/o: Q0 p2a_flags */
268 : const Word16 band_width_fx[] /* i : Q0 table of band_width */
269 : )
270 : {
271 : Word16 i, j, k;
272 :
273 : Word32 L_norm_sum; /* Qbe */
274 : Word32 L_Ravg_sub[GRP_SB]; /* Qbe */
275 : Word32 L_temp_band_energy[BANDS_MAX]; /* Qbe */
276 :
277 : Word16 j_fx, k_fx, Bits_grp_fx[GRP_SB];
278 :
279 : Word32 L_temp_band_energydiff[BANDS_MAX];
280 : Word16 G1_BE_DIFF_POS_fx; /* Q0 */
281 : Word32 L_G1_BE_DIFF_VAL; /* Qbe Word32 */
282 : Word16 final_gr_fact_pos_fx, gmax_range_fx[2], temp_fx;
283 : Word16 bits_fact_fx, bits_fact1_fx;
284 0 : Word16 grp_rngmax_fx[2] = { 0 };
285 : Word16 index_fx[NB_SWB_SUBBANDS_HAR], y_index_fx[NB_SWB_SUBBANDS_HAR], esthf_bits_fx, grp_bit_avg_fx, harmonic_band_fx;
286 : Word32 L_norm_sum_avg;
287 : Word32 L_norm_diff; /* Qbe */
288 : Word16 bits_allocweigh_fx; /* Q15 */
289 : Word16 grp_bound_fx[5];
290 : Word32 L_grp_thr[GRP_SB]; /* not require Word32 precission */
291 : Word16 lf_hf_ge_r_fx; /* Q15 */
292 : Word32 L_avg_enhf_en_diff; /* Qbe */
293 :
294 : Word16 B_norm_fx;
295 :
296 : Word32 L_temp, L_temp2;
297 : Word16 exp, frac;
298 :
299 : Word32 L_THR1, L_THR2, L_THR3;
300 :
301 : Word16 exp_norm;
302 : Word16 norm_sum_fx;
303 : Word16 Qns; /* Q value for norm_sum_fx */
304 : Word16 Inv_norm_sum_fx; /* 1/norm_sum */
305 : Word16 QIns; /* Q value for Inv_norm_sum_fx */
306 :
307 : Word16 exp_normn, exp_normd;
308 : Word16 div_fx;
309 :
310 : Word16 Inv_p2a_bands_fx;
311 : Word16 QIpb;
312 :
313 : Word16 exp_shift;
314 :
315 0 : L_THR1 = L_shl( L_deposit_l( THR1 ), SWB_BWE_LR_QRk );
316 0 : L_THR2 = L_shl( L_deposit_l( THR2 ), SWB_BWE_LR_QRk );
317 0 : L_THR3 = L_shl( L_deposit_l( THR3 ), SWB_BWE_LR_QRk );
318 :
319 0 : set16_fx( Bits_grp_fx, 0, GRP_SB );
320 :
321 : /* Initialize subbands bits allocation vector based on harmonic bands */
322 0 : harmonic_band_fx = add( sub( N_fx, p2a_bands_fx ), 1 );
323 : /*printf("harmonic_band= %d %d\n", harmonic_band, harmonic_band_fx);*/
324 0 : FOR( k = 0; k < N_fx; k++ )
325 : {
326 0 : L_Rsubband[k] = (Word32) ( C1_QRk );
327 0 : move32(); /* Constant Value */
328 0 : L_temp_band_energy[k] = L_y[k];
329 0 : move32(); /* SWB_BWE_LR_Qbe */
330 : }
331 0 : final_gr_fact_pos_fx = 2;
332 0 : move16();
333 0 : bits_fact_fx = C1_Qbf; /* Qbf */
334 0 : move16();
335 0 : bits_fact1_fx = C1_Qbf; /* Qbf */
336 0 : move16();
337 :
338 0 : gmax_range_fx[0] = G1_RANGE;
339 0 : move16();
340 0 : gmax_range_fx[1] = G1G2_RANGE;
341 0 : move16();
342 :
343 0 : IF( EQ_32( L_core_brate, HQ_16k40 ) )
344 : {
345 0 : gmax_range_fx[1] = add( gmax_range_fx[1], 2 );
346 0 : move16();
347 : }
348 :
349 : /* decide each group range, for grouping spectral coefficients */
350 0 : grp_rngmax_fx[1] = 16;
351 0 : move16();
352 0 : grp_rngmax_fx[0] = 7;
353 0 : move16();
354 0 : temp_fx = 0;
355 0 : move16();
356 0 : FOR( i = 0; i < 2; i++ )
357 : {
358 0 : j_fx = gmax_range_fx[i];
359 0 : move16();
360 0 : k_fx = 0;
361 0 : move16();
362 0 : WHILE( GE_32( L_temp_band_energy[gmax_range_fx[i] - 1], L_temp_band_energy[j_fx] ) && LT_16( j_fx, grp_rngmax_fx[i] ) )
363 : {
364 0 : test();
365 0 : k_fx = add( k_fx, 1 );
366 0 : j_fx = add( j_fx, 1 );
367 : }
368 :
369 0 : temp_fx = k_fx;
370 0 : move16();
371 0 : IF( GT_16( temp_fx, 1 ) )
372 : {
373 0 : FOR( temp_fx = 2; temp_fx <= k_fx; )
374 : {
375 0 : IF( LT_32( L_temp_band_energy[( gmax_range_fx[i] + temp_fx ) - 1], L_temp_band_energy[gmax_range_fx[i] + temp_fx] ) )
376 : {
377 0 : BREAK;
378 : }
379 0 : ELSE IF( GE_32( L_temp_band_energy[( gmax_range_fx[i] + temp_fx ) - 1], L_temp_band_energy[( gmax_range_fx[i] + temp_fx )] ) )
380 : {
381 0 : temp_fx++;
382 0 : IF( GT_16( temp_fx, k_fx ) )
383 : {
384 0 : temp_fx--; /* Q0 */
385 0 : BREAK;
386 : }
387 : }
388 : }
389 :
390 0 : gmax_range_fx[i] = add( gmax_range_fx[i], temp_fx ); /* Q0 */
391 0 : move16();
392 : }
393 : ELSE
394 : {
395 0 : gmax_range_fx[i] = add( gmax_range_fx[i], temp_fx ); /* Q0 */
396 0 : move16();
397 : }
398 : }
399 :
400 0 : grp_bound_fx[0] = 0;
401 0 : move16();
402 0 : FOR( i = 1; i < GRP_SB - 1; i++ )
403 : {
404 0 : grp_bound_fx[i] = gmax_range_fx[i - 1]; /* Q0 */
405 0 : move16();
406 : }
407 0 : grp_bound_fx[i] = harmonic_band_fx; /* Q0 */
408 0 : move16();
409 0 : grp_bound_fx[i + 1] = N_fx; /* Q0 */
410 0 : move16();
411 :
412 :
413 0 : FOR( i = 0; i < GRP_SB; i++ )
414 : {
415 0 : L_Ravg_sub[i] = L_deposit_l( 0 );
416 0 : FOR( j = grp_bound_fx[i]; j < grp_bound_fx[i + 1]; j++ )
417 : {
418 0 : IF( L_temp_band_energy[j] > 0x0L )
419 : {
420 0 : L_Ravg_sub[i] = L_add( L_Ravg_sub[i], L_temp_band_energy[j] ); /* Qbe */
421 0 : move32();
422 : }
423 : }
424 : }
425 :
426 0 : L_temp_band_energydiff[0] = L_temp_band_energy[0];
427 0 : move32();
428 0 : FOR( j = 1; j < harmonic_band_fx; j++ )
429 : {
430 0 : L_temp_band_energydiff[j] = L_abs( L_sub( L_temp_band_energy[j], L_temp_band_energy[j - 1] ) ); /* Qbe */
431 0 : move32();
432 : }
433 :
434 0 : G1_BE_DIFF_POS_fx = 0;
435 0 : move16();
436 0 : L_G1_BE_DIFF_VAL = L_deposit_l( 0 );
437 :
438 0 : FOR( j = 1; j < harmonic_band_fx; j++ )
439 : {
440 0 : IF( GT_32( L_temp_band_energydiff[j], L_G1_BE_DIFF_VAL ) )
441 : {
442 0 : G1_BE_DIFF_POS_fx = j;
443 0 : move16();
444 0 : L_G1_BE_DIFF_VAL = L_temp_band_energydiff[j]; /* Qbe */
445 0 : move32();
446 : }
447 : }
448 :
449 0 : test();
450 0 : test();
451 0 : IF( LT_16( G1_BE_DIFF_POS_fx, gmax_range_fx[0] ) && G1_BE_DIFF_POS_fx > 0 )
452 : {
453 0 : final_gr_fact_pos_fx = 0;
454 0 : move16();
455 : }
456 0 : ELSE IF( GE_16( G1_BE_DIFF_POS_fx, gmax_range_fx[0] ) && LT_16( G1_BE_DIFF_POS_fx, gmax_range_fx[1] ) )
457 : {
458 0 : final_gr_fact_pos_fx = 1;
459 0 : move16();
460 : }
461 : ELSE
462 : {
463 0 : final_gr_fact_pos_fx = 2;
464 0 : move16();
465 : }
466 :
467 0 : test();
468 0 : IF( final_gr_fact_pos_fx == 0 || EQ_16( final_gr_fact_pos_fx, 1 ) )
469 : {
470 0 : IF( EQ_32( L_core_brate, HQ_16k40 ) )
471 : {
472 0 : bits_fact_fx = BITS_FACT_1p10;
473 0 : move16(); /* 1.10f; */ /* G1 */
474 0 : bits_fact1_fx = BITS_FACT_0p92;
475 0 : move16(); /* 0.92f; */ /* G3 */
476 : }
477 : ELSE
478 : {
479 0 : bits_fact_fx = BITS_FACT_1p05;
480 0 : move16(); /* 1.05f; */ /* G1 */
481 0 : bits_fact1_fx = BITS_FACT_0p97;
482 0 : move16(); /* 0.97f; */ /* G3 */
483 : }
484 : }
485 : ELSE
486 : {
487 0 : IF( EQ_32( L_core_brate, HQ_16k40 ) )
488 : {
489 0 : bits_fact_fx = BITS_FACT_0p97;
490 0 : move16(); /* 0.97f; */ /* G1 */
491 0 : bits_fact1_fx = BITS_FACT_1p00;
492 0 : move16(); /* 1.00f; */ /* G3 */
493 : }
494 : ELSE
495 : {
496 0 : bits_fact_fx = BITS_FACT_0p92;
497 0 : move16(); /* 0.92f; */ /* G1 */
498 0 : bits_fact1_fx = BITS_FACT_1p00;
499 0 : move16(); /* 1.00f; */ /* G3 */
500 : }
501 : }
502 :
503 0 : j = sub( N_fx, harmonic_band_fx ); /* Q0 */
504 0 : FOR( i = 0; i < j; i++ )
505 : {
506 0 : y_index_fx[i] = extract_h( L_shl( L_temp_band_energy[harmonic_band_fx + i], sub( 16, SWB_BWE_LR_Qbe ) ) ); /* Q0 */
507 0 : move16();
508 0 : index_fx[i] = add( harmonic_band_fx, i ); /* Q0 */
509 0 : move16();
510 : }
511 :
512 0 : reordvct_fx( y_index_fx, sub( N_fx, harmonic_band_fx ), index_fx );
513 :
514 : /* Log2 */
515 0 : L_temp = L_deposit_l( band_width_fx[index_fx[0]] ); /* Q0 */
516 0 : exp = norm_l( L_temp );
517 0 : frac = Log2_norm_lc( L_shl( L_temp, exp ) );
518 0 : exp = sub( 30, exp );
519 0 : L_temp = L_Comp( exp, frac ); /* Q16 */
520 : /* ceil */
521 0 : if ( L_and( 0x0000ffff, L_temp ) > 0 )
522 : {
523 0 : L_temp = L_add( L_temp, 0x00010000 ); /* Q16 */
524 : }
525 0 : esthf_bits_fx = extract_h( L_temp );
526 :
527 0 : L_grp_thr[0] = L_THR1; /* SWB_BWE_LR_QRk */
528 0 : move32();
529 0 : L_grp_thr[1] = L_THR2; /* SWB_BWE_LR_QRk */
530 0 : move32();
531 0 : L_grp_thr[2] = L_THR3; /* SWB_BWE_LR_QRk */
532 0 : move32();
533 0 : L_grp_thr[3] = L_shl( L_deposit_l( esthf_bits_fx ), SWB_BWE_LR_QRk ); /* SWB_BWE_LR_QRk */
534 0 : move16();
535 :
536 0 : L_norm_sum = L_deposit_l( 1 );
537 0 : FOR( i = 0; i < 3; i++ )
538 : {
539 0 : L_norm_sum = L_add( L_norm_sum, L_Ravg_sub[i] ); /* Qbe */
540 : }
541 :
542 : /*reserve bits for HF coding */
543 0 : L_temp = L_add( L_norm_sum, L_Ravg_sub[GRP_SB - 1] ); /* Qbe */
544 0 : exp_normn = norm_l( L_temp );
545 0 : exp_normn = sub( exp_normn, 1 );
546 0 : exp_normd = norm_s( N_fx );
547 :
548 0 : div_fx = div_l( L_shl( L_temp, exp_normn ), shl( N_fx, exp_normd ) ); /* (Qbe+exp_normn)-(0+exp_normd)-1) */
549 0 : L_norm_sum_avg = L_shr( L_deposit_h( div_fx ), add( sub( exp_normn, exp_normd ), 15 ) ); /* -> Qbe */
550 :
551 0 : exp_norm = norm_l( L_norm_sum );
552 0 : norm_sum_fx = extract_h( L_shl( L_norm_sum, exp_norm ) ); /* SWB_BWE_LR_Qbe+exp_norm-16 */
553 0 : Qns = sub( add( SWB_BWE_LR_Qbe, exp_norm ), 16 );
554 :
555 0 : Inv_norm_sum_fx = div_s( 0x4000 /* 0.5 in Q15 */, norm_sum_fx );
556 0 : QIns = sub( 31, exp_norm ); /* 14 - (14+exp_norm-16) + 15 */
557 :
558 0 : grp_bit_avg_fx = div_s_ss( B_fx, GRP_SB ); /* Q0 */
559 :
560 0 : exp_normd = norm_s( p2a_bands_fx );
561 0 : Inv_p2a_bands_fx = div_s( 0x3fff, shl( p2a_bands_fx, exp_normd ) ); /* 14-exp_normd+15 */
562 0 : QIpb = sub( 29, exp_normd );
563 :
564 0 : L_temp = L_shl( Mult_32_16( L_Ravg_sub[GRP_SB - 1], Inv_p2a_bands_fx ), sub( SWB_BWE_LR_Qbe, sub( QIpb, 1 ) ) ); /* Qbe */
565 0 : L_norm_diff = L_sub( L_temp, L_norm_sum_avg ); /* Qbe */
566 :
567 0 : L_temp = Mult_32_16( L_Ravg_sub[GRP_SB - 1], sub( GRP_SB, 1 ) ); /* Qbe+0+1 */
568 0 : L_temp = Mult_32_16( L_temp, Inv_norm_sum_fx ); /* Qbe+1+QIpb+1 */
569 0 : lf_hf_ge_r_fx = round_fx_sat( L_shl_sat( L_temp, sub( 15 + 16, sub( add( SWB_BWE_LR_Qbe, QIns ), 30 ) ) ) );
570 :
571 0 : exp_normn = norm_s( norm_sum_fx );
572 0 : exp_normn = sub( exp_normn, 1 );
573 0 : exp_normd = norm_s( harmonic_band_fx );
574 :
575 0 : div_fx = div_s( shl( norm_sum_fx, exp_normn ), shl( harmonic_band_fx, exp_normd ) );
576 0 : L_avg_enhf_en_diff = L_sub( L_temp_band_energy[index_fx[0]], L_shl( L_deposit_h( div_fx ), sub( sub( SWB_BWE_LR_Qbe, ( add( Qns, sub( exp_normn, exp_normd ) ) ) ), 31 ) ) ); /* Qbe - (Qns+exp_normn-(exp_normd)+15) -16 */
577 :
578 0 : test();
579 0 : IF( GT_16( lf_hf_ge_r_fx, 26214 ) && GT_32( L_avg_enhf_en_diff, (Word32) ( 8 << SWB_BWE_LR_Qbe ) ) ) /* 0.8=26214.4(Q15) 8.0f=131072(Qbe) */
580 : {
581 0 : bits_allocweigh_fx = 6554;
582 0 : move16(); /* 0.2 6553.6(Q15) */
583 0 : if ( L_norm_diff < 0x0L )
584 : {
585 0 : bits_allocweigh_fx = 13107;
586 0 : move16(); /* 0.4 13107.2(Q15) */
587 : }
588 :
589 : /*allocate bits*/
590 : /*Bits_grp[GRP_SB-1] = (short)min((grp_bit_avg/p2a_bands + bits_allocweigh*norm_diff),10);*/
591 0 : L_temp = L_mult( grp_bit_avg_fx, Inv_p2a_bands_fx ); /* Q0+QIpb+1 */
592 0 : L_temp2 = Mult_32_16( L_norm_diff, bits_allocweigh_fx ); /* Qbe+Q15-15 */
593 :
594 0 : L_temp = L_shr( L_temp, add( QIpb, 1 ) );
595 0 : L_temp = L_add( L_shl( L_temp, SWB_BWE_LR_Qbe ), L_temp2 ); /* Qbe+Q15-15 */
596 :
597 0 : Bits_grp_fx[GRP_SB - 1] = extract_h( L_shl( L_temp, sub( 16, SWB_BWE_LR_Qbe ) ) ); /* Q0 */
598 0 : move16();
599 0 : Bits_grp_fx[GRP_SB - 1] = s_min( Bits_grp_fx[GRP_SB - 1], 10 ); /* Q0 */
600 0 : move16();
601 :
602 0 : if ( LT_16( Bits_grp_fx[GRP_SB - 1], esthf_bits_fx ) )
603 : {
604 0 : Bits_grp_fx[GRP_SB - 1] = 0;
605 0 : move16();
606 : }
607 0 : B_fx = sub( B_fx, Bits_grp_fx[GRP_SB - 1] ); /* Q0 */
608 : }
609 :
610 0 : exp_shift = sub( add( SWB_BWE_LR_Qbe, QIns ), 47 ); /* (SWB_BWE_LR_Qbe+14+1+QIns-15-16) */
611 0 : exp_norm = norm_s( B_fx );
612 0 : B_norm_fx = shl( B_fx, exp_norm );
613 0 : exp_shift = add( exp_shift, exp_norm );
614 :
615 0 : IF( EQ_16( final_gr_fact_pos_fx, 1 ) )
616 : {
617 0 : L_temp = Mult_32_16( L_Ravg_sub[1], extract_h( L_mult( bits_fact_fx, B_norm_fx ) ) );
618 0 : L_temp = Mult_32_16( L_temp, Inv_norm_sum_fx ); /* Q16 + exp_shift */
619 0 : Bits_grp_fx[1] = extract_h( L_shr( L_temp, exp_shift ) ); /* Q0 */
620 0 : move16();
621 :
622 0 : L_temp = Mult_32_16( L_Ravg_sub[2], extract_h( L_mult( bits_fact1_fx, B_norm_fx ) ) );
623 0 : L_temp = Mult_32_16( L_temp, Inv_norm_sum_fx ); /* Q16 + exp_shift */
624 0 : Bits_grp_fx[2] = extract_h( L_shr( L_temp, exp_shift ) ); /* Q0 */
625 0 : move16();
626 :
627 0 : Bits_grp_fx[0] = sub( sub( B_fx, Bits_grp_fx[1] ), Bits_grp_fx[2] ); /* Q0 */
628 0 : move16();
629 : }
630 : ELSE
631 : {
632 0 : L_temp = Mult_32_16( L_Ravg_sub[0], extract_h( L_mult( bits_fact_fx, B_norm_fx ) ) );
633 0 : L_temp = Mult_32_16( L_temp, Inv_norm_sum_fx ); /* Q16 + exp_shift */
634 0 : Bits_grp_fx[0] = extract_h( L_shr( L_temp, exp_shift ) ); /* Q0 */
635 0 : move16();
636 :
637 0 : L_temp = Mult_32_16( L_Ravg_sub[2], extract_h( L_mult( bits_fact1_fx, B_norm_fx ) ) );
638 0 : L_temp = Mult_32_16( L_temp, Inv_norm_sum_fx ); /* Q16 + exp_shift */
639 0 : Bits_grp_fx[2] = extract_h( L_shr( L_temp, exp_shift ) ); /* Q0 */
640 0 : move16();
641 :
642 0 : Bits_grp_fx[1] = sub( sub( B_fx, Bits_grp_fx[0] ), Bits_grp_fx[2] ); /* Q0 */
643 0 : move16();
644 : }
645 :
646 0 : IF( LT_16( Bits_grp_fx[2], THR2 ) )
647 : {
648 0 : Bits_grp_fx[1] = add( Bits_grp_fx[1], Bits_grp_fx[2] ); /* Q0 */
649 0 : move16();
650 0 : Bits_grp_fx[2] = 0;
651 0 : move16();
652 : }
653 :
654 0 : FOR( i = 0; i < GRP_SB; i++ )
655 : {
656 0 : IF( Bits_grp_fx[i] > 0 )
657 : {
658 0 : Bits2indvsb_fx( L_temp_band_energy, grp_bound_fx[i], grp_bound_fx[i + 1], Bits_grp_fx[i], L_grp_thr[i], L_Rsubband, p2a_flags_fx );
659 : }
660 : ELSE
661 : {
662 0 : set32_fx( L_Rsubband + grp_bound_fx[i], 0x0L, sub( grp_bound_fx[i + 1], grp_bound_fx[i] ) );
663 0 : IF( EQ_16( i, GRP_SB - 1 ) )
664 : {
665 0 : set16_fx( p2a_flags_fx + grp_bound_fx[i], 0, sub( grp_bound_fx[i + 1], grp_bound_fx[i] ) );
666 : }
667 : }
668 : }
669 :
670 0 : return;
671 : }
672 :
673 : /*--------------------------------------------------------------------------*
674 : * hq2_bit_alloc()
675 : *
676 : * HQ2 bit-allocation
677 : *--------------------------------------------------------------------------*/
678 :
679 78 : Word32 hq2_bit_alloc_fx(
680 : const Word32 L_band_energy[], /* i : band energy of each subband */
681 : const Word16 bands, /* i : total number of subbands in a frame Q0*/
682 : Word32 L_Rk[], /* i/o: Bit allocation/Adjusted bit alloc. QRk*/
683 : Word16 *bit_budget_fx, /* i/o: bit bugdet Q0*/
684 : Word16 *p2a_flags, /* i : HF tonal indicator Q0*/
685 : const Word16 weight_fx, /* i : weight Q13*/
686 : const Word16 band_width[], /* i : Sub band bandwidth Q0*/
687 : const Word16 num_bits, /* i : available bits Q0*/
688 : const Word16 hqswb_clas, /* i : HQ2 class information Q0*/
689 : const Word16 bwidth, /* i : input bandwidth Q0*/
690 : const Word16 is_transient /* i : indicator HQ_TRANSIENT or not Q0*/
691 : )
692 : {
693 : Word16 j, k;
694 : Word16 tmp;
695 : Word16 bit_budget_norm_fx;
696 :
697 : Word32 L_Rcalc, L_Ravg, L_Rcalc1;
698 :
699 : Word16 exp_normn, exp_normd;
700 :
701 : Word16 Rcnt_fx;
702 :
703 : Word16 div_fx;
704 : Word16 Qdiv;
705 :
706 : Word32 L_tmp;
707 : Word16 tmp_fx;
708 :
709 : Word32 L_maxxy;
710 : Word16 maxdex_fx;
711 : Word32 L_dummy;
712 :
713 : Word16 bit_budget_temp_fx;
714 :
715 : Word16 negflag;
716 :
717 : Word32 L_THR1, L_THR2, L_THR3;
718 :
719 78 : L_THR1 = L_shl( L_deposit_l( THR1 ), SWB_BWE_LR_QRk ); /* SWB_BWE_LR_QRk */
720 78 : L_THR2 = L_shl( L_deposit_l( THR2 ), SWB_BWE_LR_QRk ); /* SWB_BWE_LR_QRk */
721 78 : L_THR3 = L_shl( L_deposit_l( THR3 ), SWB_BWE_LR_QRk ); /* SWB_BWE_LR_QRk */
722 :
723 : /* Init Rk to non-zero values for bands to be allocated bits */
724 78 : IF( LE_16( num_bits, HQ_16k40_BIT ) )
725 : {
726 78 : set32_fx( L_Rk, (Word32) ( C1_QRk ), bands ); /* 1<<SWB_BWE_LR_QRk */
727 :
728 78 : test();
729 78 : IF( is_transient && EQ_16( bands, 32 ) )
730 : {
731 2 : L_Rk[6] = L_deposit_l( 0 );
732 2 : move32();
733 2 : L_Rk[7] = L_deposit_l( 0 );
734 2 : move32();
735 2 : L_Rk[14] = L_deposit_l( 0 );
736 2 : move32();
737 2 : L_Rk[15] = L_deposit_l( 0 );
738 2 : move32();
739 2 : L_Rk[22] = L_deposit_l( 0 );
740 2 : move32();
741 2 : L_Rk[23] = L_deposit_l( 0 );
742 2 : move32();
743 2 : L_Rk[30] = L_deposit_l( 0 );
744 2 : move32();
745 2 : L_Rk[31] = L_deposit_l( 0 );
746 2 : move32();
747 : }
748 : }
749 : ELSE
750 : {
751 : /*mvs2r( p2a_flags, Rk, bands ); */
752 0 : FOR( k = 0; k < bands; k++ )
753 : {
754 0 : L_Rk[k] = L_shl( L_deposit_l( p2a_flags[k] ), SWB_BWE_LR_QRk ); /* QRk */
755 0 : move32();
756 : }
757 : }
758 :
759 78 : L_Rcalc = L_deposit_l( 0 );
760 78 : L_Rcalc1 = L_deposit_l( 0 );
761 :
762 564 : FOR( j = 0; j < bands; j++ )
763 : {
764 564 : Rcnt_fx = 0;
765 564 : move16();
766 564 : L_Ravg = 0x0L;
767 564 : move32();
768 :
769 13052 : FOR( k = 0; k < bands; k++ )
770 : {
771 12488 : IF( L_Rk[k] > 0 )
772 : {
773 10506 : L_Ravg = L_add( L_Ravg, L_shl( L_band_energy[k], sub( SWB_BWE_LR_QRk, SWB_BWE_LR_Qbe ) ) ); /* SWB_BWE_LR_QRk-SWB_BWE_LR_Qbe */
774 10506 : Rcnt_fx = add( Rcnt_fx, 1 );
775 : }
776 : }
777 : /* Ravg Qband_energy */
778 :
779 : /*L_Ravg /= Rcnt; */
780 564 : exp_normd = norm_l( L_Ravg );
781 564 : exp_normd = sub( exp_normd, 1 );
782 564 : exp_normn = norm_s( Rcnt_fx );
783 :
784 564 : tmp = shl( Rcnt_fx, exp_normn );
785 564 : tmp = s_max( tmp, 1 );
786 564 : IF( L_Ravg > 0 )
787 : {
788 564 : div_fx = div_l( L_shl( L_Ravg, exp_normd ), tmp ); /* Qdiv = 14+exp_normd-(exp_normn)-1 */
789 : }
790 : ELSE
791 : {
792 0 : div_fx = div_l( L_shl( L_abs( L_Ravg ), exp_normd ), tmp ); /* Qdiv = 14+exp_normd-(exp_normn)-1 */
793 0 : div_fx = negate( div_fx );
794 : }
795 :
796 564 : Qdiv = sub( sub( add( SWB_BWE_LR_QRk, exp_normd ), exp_normn ), 1 );
797 :
798 564 : L_Ravg = L_shr( L_deposit_l( div_fx ), sub( Qdiv, SWB_BWE_LR_QRk ) ); /* QRk */
799 :
800 564 : exp_normd = norm_s( *bit_budget_fx );
801 564 : exp_normd = sub( exp_normd, 1 );
802 564 : bit_budget_norm_fx = shl( *bit_budget_fx, exp_normd ); /* exp_normd */
803 564 : div_fx = 0;
804 564 : move16();
805 :
806 564 : test();
807 564 : IF( bit_budget_norm_fx > 0 && LT_16( bit_budget_norm_fx, tmp ) )
808 : {
809 564 : div_fx = div_s( bit_budget_norm_fx, tmp );
810 : }
811 564 : Qdiv = add( sub( exp_normd, exp_normn ), 15 );
812 13052 : FOR( k = 0; k < bands; k++ )
813 : {
814 12488 : IF( L_Rk[k] > 0 )
815 : {
816 : /*Rk[k] = ((float) *bit_budget / Rcnt + weight * (band_energy[k] - Ravg)); */
817 :
818 10506 : L_tmp = Mult_32_16( L_sub( L_shl( L_band_energy[k], sub( SWB_BWE_LR_QRk, SWB_BWE_LR_Qbe ) ), L_Ravg ), weight_fx ); /* SWB_BWE_LR_QRk + Q13 - 15 */
819 10506 : L_tmp = L_shl( L_tmp, 2 ); /* -> SWB_BWE_LR_QRk */
820 :
821 10506 : L_Rk[k] = L_add( L_shr( L_deposit_l( div_fx ), sub( Qdiv, SWB_BWE_LR_QRk ) ), L_tmp ); /* SWB_BWE_LR_QRk */
822 10506 : move32();
823 : }
824 : }
825 :
826 564 : negflag = 0;
827 564 : move16();
828 564 : L_Rcalc = L_deposit_l( 0 );
829 13052 : FOR( k = 0; k < bands; k++ )
830 : {
831 12488 : IF( LT_32( L_Rk[k], MIN_BITS_FIX ) )
832 : {
833 30 : L_Rk[k] = L_deposit_l( 0 );
834 30 : move32();
835 30 : negflag = 1;
836 30 : move16();
837 : }
838 12488 : L_Rcalc = L_add( L_Rcalc, L_Rk[k] ); /*SWB_BWE_LR_QRk */
839 : }
840 :
841 : /* prune noiselike bands with low allocation */
842 564 : test();
843 564 : IF( LE_16( num_bits, HQ_16k40_BIT ) && negflag == 0 )
844 : {
845 552 : L_maxxy = L_deposit_l( 0 );
846 552 : maxdex_fx = -1;
847 552 : move16();
848 552 : L_Rcalc = L_deposit_l( 0 );
849 :
850 : /* find worst under-allocation */
851 12736 : FOR( k = bands - 1; k >= 0; k-- )
852 : {
853 12184 : tmp_fx = s_min( band_width[k], s_max( 12, shr( band_width[k], 2 ) ) ); /* Q0 */
854 12184 : L_dummy = L_sub( L_shl( L_deposit_l( tmp_fx ), SWB_BWE_LR_QRk ), L_Rk[k] ); /*SWB_BWE_LR_QRk */
855 12184 : test();
856 12184 : test();
857 12184 : IF( p2a_flags[k] == 0 && GT_32( L_dummy, L_maxxy ) && L_Rk[k] > 0 )
858 : {
859 346 : maxdex_fx = k;
860 346 : move16();
861 346 : L_maxxy = L_dummy; /*SWB_BWE_LR_QRk */
862 346 : move32();
863 : }
864 : }
865 :
866 : /* prune worst allocation and recalculate total allocation */
867 552 : IF( GT_16( maxdex_fx, -1 ) )
868 : {
869 336 : L_Rk[maxdex_fx] = L_deposit_l( 0 );
870 336 : move32();
871 : }
872 12736 : FOR( k = 0; k < bands; k++ )
873 : {
874 12184 : L_Rcalc = L_add( L_Rcalc, L_Rk[k] ); /*SWB_BWE_LR_QRk */
875 : }
876 : }
877 564 : test();
878 564 : test();
879 564 : IF( EQ_32( L_Rcalc, L_Rcalc1 ) && EQ_16( bwidth, SWB ) )
880 : {
881 : /* Reallocate bits to individual subbands for HQ_NORMAL mode */
882 : /* if bits allocated to subbands areless than predefined threshold */
883 108 : test();
884 108 : IF( EQ_16( hqswb_clas, HQ_NORMAL ) && LT_16( num_bits, HQ_16k40_BIT ) )
885 : {
886 106 : L_dummy = L_deposit_l( 0 );
887 2438 : FOR( k = 0; k < bands; k++ )
888 : {
889 2332 : test();
890 2332 : test();
891 2332 : test();
892 2332 : test();
893 2332 : test();
894 2332 : IF( LT_16( k, 11 ) && LT_32( L_Rk[k], L_THR1 ) )
895 : {
896 164 : L_Rk[k] = L_deposit_l( 0 );
897 164 : move32();
898 : }
899 2168 : ELSE IF( GE_16( k, 11 ) && LT_16( k, 16 ) && LT_32( L_Rk[k], L_THR2 ) )
900 : {
901 48 : L_Rk[k] = L_deposit_l( 0 );
902 48 : move32();
903 : }
904 2120 : ELSE if ( GE_16( k, 16 ) && LT_16( k, bands ) && LT_32( L_Rk[k], L_THR3 ) )
905 : {
906 496 : L_Rk[k] = L_deposit_l( 0 );
907 496 : move32();
908 : }
909 :
910 2332 : L_dummy = L_add( L_dummy, L_Rk[k] );
911 : }
912 :
913 106 : IF( EQ_32( L_dummy, L_Rcalc ) )
914 : {
915 80 : test();
916 80 : IF( EQ_16( hqswb_clas, HQ_NORMAL ) && LT_16( num_bits, HQ_16k40_BIT ) )
917 : {
918 80 : bit_budget_temp_fx = *bit_budget_fx; /* Q0 */
919 80 : move16();
920 400 : FOR( k = 0; k < NB_SWB_SUBBANDS; k++ )
921 : {
922 320 : test();
923 320 : IF( EQ_16( p2a_flags[( bands - NB_SWB_SUBBANDS ) + k], 1 ) && L_Rk[( bands - NB_SWB_SUBBANDS ) + k] == 0 )
924 : {
925 4 : p2a_flags[( bands - NB_SWB_SUBBANDS ) + k] = 0;
926 4 : move16();
927 4 : bit_budget_temp_fx = sub( bit_budget_temp_fx, bits_lagIndices_modeNormal[k] );
928 : }
929 : }
930 :
931 80 : IF( LT_16( bit_budget_temp_fx, *bit_budget_fx ) )
932 : {
933 4 : *bit_budget_fx = bit_budget_temp_fx;
934 4 : move16();
935 : /* a negative *bit_budget_fx may occur here due to Bit Errors */
936 : /* handled outside this function to properly set flag: st_fx->BER_detect */
937 : }
938 76 : ELSE IF( EQ_16( bit_budget_temp_fx, *bit_budget_fx ) )
939 : {
940 76 : BREAK;
941 : }
942 : }
943 : ELSE
944 : {
945 : BREAK;
946 : }
947 : }
948 : }
949 : ELSE
950 : {
951 : BREAK;
952 : }
953 : }
954 456 : ELSE IF( EQ_32( L_Rcalc, L_Rcalc1 ) && NE_16( bwidth, SWB ) )
955 : {
956 0 : BREAK;
957 : }
958 :
959 486 : L_Rcalc1 = L_Rcalc;
960 486 : move32();
961 : }
962 :
963 78 : return L_Rcalc;
964 : }
|