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 : #ifndef ISSUE_1836_replace_overflow_libcom
316 : Flag Overflow;
317 : Overflow = 0;
318 : move32();
319 : #endif
320 :
321 0 : L_THR1 = L_shl( L_deposit_l( THR1 ), SWB_BWE_LR_QRk );
322 0 : L_THR2 = L_shl( L_deposit_l( THR2 ), SWB_BWE_LR_QRk );
323 0 : L_THR3 = L_shl( L_deposit_l( THR3 ), SWB_BWE_LR_QRk );
324 :
325 0 : set16_fx( Bits_grp_fx, 0, GRP_SB );
326 :
327 : /* Initialize subbands bits allocation vector based on harmonic bands */
328 0 : harmonic_band_fx = add( sub( N_fx, p2a_bands_fx ), 1 );
329 : /*printf("harmonic_band= %d %d\n", harmonic_band, harmonic_band_fx);*/
330 0 : FOR( k = 0; k < N_fx; k++ )
331 : {
332 0 : L_Rsubband[k] = (Word32) ( C1_QRk );
333 0 : move32(); /* Constant Value */
334 0 : L_temp_band_energy[k] = L_y[k];
335 0 : move32(); /* SWB_BWE_LR_Qbe */
336 : }
337 0 : final_gr_fact_pos_fx = 2;
338 0 : move16();
339 0 : bits_fact_fx = C1_Qbf; /* Qbf */
340 0 : move16();
341 0 : bits_fact1_fx = C1_Qbf; /* Qbf */
342 0 : move16();
343 :
344 0 : gmax_range_fx[0] = G1_RANGE;
345 0 : move16();
346 0 : gmax_range_fx[1] = G1G2_RANGE;
347 0 : move16();
348 :
349 0 : IF( EQ_32( L_core_brate, HQ_16k40 ) )
350 : {
351 0 : gmax_range_fx[1] = add( gmax_range_fx[1], 2 );
352 0 : move16();
353 : }
354 :
355 : /* decide each group range, for grouping spectral coefficients */
356 0 : grp_rngmax_fx[1] = 16;
357 0 : move16();
358 0 : grp_rngmax_fx[0] = 7;
359 0 : move16();
360 0 : temp_fx = 0;
361 0 : move16();
362 0 : FOR( i = 0; i < 2; i++ )
363 : {
364 0 : j_fx = gmax_range_fx[i];
365 0 : move16();
366 0 : k_fx = 0;
367 0 : move16();
368 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] ) )
369 : {
370 0 : test();
371 0 : k_fx = add( k_fx, 1 );
372 0 : j_fx = add( j_fx, 1 );
373 : }
374 :
375 0 : temp_fx = k_fx;
376 0 : move16();
377 0 : IF( GT_16( temp_fx, 1 ) )
378 : {
379 0 : FOR( temp_fx = 2; temp_fx <= k_fx; )
380 : {
381 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] ) )
382 : {
383 0 : BREAK;
384 : }
385 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 )] ) )
386 : {
387 0 : temp_fx++;
388 0 : IF( GT_16( temp_fx, k_fx ) )
389 : {
390 0 : temp_fx--; /* Q0 */
391 0 : BREAK;
392 : }
393 : }
394 : }
395 :
396 0 : gmax_range_fx[i] = add( gmax_range_fx[i], temp_fx ); /* Q0 */
397 0 : move16();
398 : }
399 : ELSE
400 : {
401 0 : gmax_range_fx[i] = add( gmax_range_fx[i], temp_fx ); /* Q0 */
402 0 : move16();
403 : }
404 : }
405 :
406 0 : grp_bound_fx[0] = 0;
407 0 : move16();
408 0 : FOR( i = 1; i < GRP_SB - 1; i++ )
409 : {
410 0 : grp_bound_fx[i] = gmax_range_fx[i - 1]; /* Q0 */
411 0 : move16();
412 : }
413 0 : grp_bound_fx[i] = harmonic_band_fx; /* Q0 */
414 0 : move16();
415 0 : grp_bound_fx[i + 1] = N_fx; /* Q0 */
416 0 : move16();
417 :
418 :
419 0 : FOR( i = 0; i < GRP_SB; i++ )
420 : {
421 0 : L_Ravg_sub[i] = L_deposit_l( 0 );
422 0 : FOR( j = grp_bound_fx[i]; j < grp_bound_fx[i + 1]; j++ )
423 : {
424 0 : IF( L_temp_band_energy[j] > 0x0L )
425 : {
426 0 : L_Ravg_sub[i] = L_add( L_Ravg_sub[i], L_temp_band_energy[j] ); /* Qbe */
427 0 : move32();
428 : }
429 : }
430 : }
431 :
432 0 : L_temp_band_energydiff[0] = L_temp_band_energy[0];
433 0 : move32();
434 0 : FOR( j = 1; j < harmonic_band_fx; j++ )
435 : {
436 0 : L_temp_band_energydiff[j] = L_abs( L_sub( L_temp_band_energy[j], L_temp_band_energy[j - 1] ) ); /* Qbe */
437 0 : move32();
438 : }
439 :
440 0 : G1_BE_DIFF_POS_fx = 0;
441 0 : move16();
442 0 : L_G1_BE_DIFF_VAL = L_deposit_l( 0 );
443 :
444 0 : FOR( j = 1; j < harmonic_band_fx; j++ )
445 : {
446 0 : IF( GT_32( L_temp_band_energydiff[j], L_G1_BE_DIFF_VAL ) )
447 : {
448 0 : G1_BE_DIFF_POS_fx = j;
449 0 : move16();
450 0 : L_G1_BE_DIFF_VAL = L_temp_band_energydiff[j]; /* Qbe */
451 0 : move32();
452 : }
453 : }
454 :
455 0 : test();
456 0 : test();
457 0 : IF( LT_16( G1_BE_DIFF_POS_fx, gmax_range_fx[0] ) && G1_BE_DIFF_POS_fx > 0 )
458 : {
459 0 : final_gr_fact_pos_fx = 0;
460 0 : move16();
461 : }
462 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] ) )
463 : {
464 0 : final_gr_fact_pos_fx = 1;
465 0 : move16();
466 : }
467 : ELSE
468 : {
469 0 : final_gr_fact_pos_fx = 2;
470 0 : move16();
471 : }
472 :
473 0 : test();
474 0 : IF( final_gr_fact_pos_fx == 0 || EQ_16( final_gr_fact_pos_fx, 1 ) )
475 : {
476 0 : IF( EQ_32( L_core_brate, HQ_16k40 ) )
477 : {
478 0 : bits_fact_fx = BITS_FACT_1p10;
479 0 : move16(); /* 1.10f; */ /* G1 */
480 0 : bits_fact1_fx = BITS_FACT_0p92;
481 0 : move16(); /* 0.92f; */ /* G3 */
482 : }
483 : ELSE
484 : {
485 0 : bits_fact_fx = BITS_FACT_1p05;
486 0 : move16(); /* 1.05f; */ /* G1 */
487 0 : bits_fact1_fx = BITS_FACT_0p97;
488 0 : move16(); /* 0.97f; */ /* G3 */
489 : }
490 : }
491 : ELSE
492 : {
493 0 : IF( EQ_32( L_core_brate, HQ_16k40 ) )
494 : {
495 0 : bits_fact_fx = BITS_FACT_0p97;
496 0 : move16(); /* 0.97f; */ /* G1 */
497 0 : bits_fact1_fx = BITS_FACT_1p00;
498 0 : move16(); /* 1.00f; */ /* G3 */
499 : }
500 : ELSE
501 : {
502 0 : bits_fact_fx = BITS_FACT_0p92;
503 0 : move16(); /* 0.92f; */ /* G1 */
504 0 : bits_fact1_fx = BITS_FACT_1p00;
505 0 : move16(); /* 1.00f; */ /* G3 */
506 : }
507 : }
508 :
509 0 : j = sub( N_fx, harmonic_band_fx ); /* Q0 */
510 0 : FOR( i = 0; i < j; i++ )
511 : {
512 0 : y_index_fx[i] = extract_h( L_shl( L_temp_band_energy[harmonic_band_fx + i], sub( 16, SWB_BWE_LR_Qbe ) ) ); /* Q0 */
513 0 : move16();
514 0 : index_fx[i] = add( harmonic_band_fx, i ); /* Q0 */
515 0 : move16();
516 : }
517 :
518 0 : reordvct_fx( y_index_fx, sub( N_fx, harmonic_band_fx ), index_fx );
519 :
520 : /* Log2 */
521 0 : L_temp = L_deposit_l( band_width_fx[index_fx[0]] ); /* Q0 */
522 0 : exp = norm_l( L_temp );
523 0 : frac = Log2_norm_lc( L_shl( L_temp, exp ) );
524 0 : exp = sub( 30, exp );
525 0 : L_temp = L_Comp( exp, frac ); /* Q16 */
526 : /* ceil */
527 0 : if ( L_and( 0x0000ffff, L_temp ) > 0 )
528 : {
529 0 : L_temp = L_add( L_temp, 0x00010000 ); /* Q16 */
530 : }
531 0 : esthf_bits_fx = extract_h( L_temp );
532 :
533 0 : L_grp_thr[0] = L_THR1; /* SWB_BWE_LR_QRk */
534 0 : move32();
535 0 : L_grp_thr[1] = L_THR2; /* SWB_BWE_LR_QRk */
536 0 : move32();
537 0 : L_grp_thr[2] = L_THR3; /* SWB_BWE_LR_QRk */
538 0 : move32();
539 0 : L_grp_thr[3] = L_shl( L_deposit_l( esthf_bits_fx ), SWB_BWE_LR_QRk ); /* SWB_BWE_LR_QRk */
540 0 : move16();
541 :
542 0 : L_norm_sum = L_deposit_l( 1 );
543 0 : FOR( i = 0; i < 3; i++ )
544 : {
545 0 : L_norm_sum = L_add( L_norm_sum, L_Ravg_sub[i] ); /* Qbe */
546 : }
547 :
548 : /*reserve bits for HF coding */
549 0 : L_temp = L_add( L_norm_sum, L_Ravg_sub[GRP_SB - 1] ); /* Qbe */
550 0 : exp_normn = norm_l( L_temp );
551 0 : exp_normn = sub( exp_normn, 1 );
552 0 : exp_normd = norm_s( N_fx );
553 :
554 0 : div_fx = div_l( L_shl( L_temp, exp_normn ), shl( N_fx, exp_normd ) ); /* (Qbe+exp_normn)-(0+exp_normd)-1) */
555 0 : L_norm_sum_avg = L_shr( L_deposit_h( div_fx ), add( sub( exp_normn, exp_normd ), 15 ) ); /* -> Qbe */
556 :
557 0 : exp_norm = norm_l( L_norm_sum );
558 0 : norm_sum_fx = extract_h( L_shl( L_norm_sum, exp_norm ) ); /* SWB_BWE_LR_Qbe+exp_norm-16 */
559 0 : Qns = sub( add( SWB_BWE_LR_Qbe, exp_norm ), 16 );
560 :
561 0 : Inv_norm_sum_fx = div_s( 0x4000 /* 0.5 in Q15 */, norm_sum_fx );
562 0 : QIns = sub( 31, exp_norm ); /* 14 - (14+exp_norm-16) + 15 */
563 :
564 0 : grp_bit_avg_fx = div_s_ss( B_fx, GRP_SB ); /* Q0 */
565 :
566 0 : exp_normd = norm_s( p2a_bands_fx );
567 0 : Inv_p2a_bands_fx = div_s( 0x3fff, shl( p2a_bands_fx, exp_normd ) ); /* 14-exp_normd+15 */
568 0 : QIpb = sub( 29, exp_normd );
569 :
570 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 */
571 0 : L_norm_diff = L_sub( L_temp, L_norm_sum_avg ); /* Qbe */
572 :
573 0 : L_temp = Mult_32_16( L_Ravg_sub[GRP_SB - 1], sub( GRP_SB, 1 ) ); /* Qbe+0+1 */
574 0 : L_temp = Mult_32_16( L_temp, Inv_norm_sum_fx ); /* Qbe+1+QIpb+1 */
575 : #ifdef ISSUE_1836_replace_overflow_libcom
576 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 ) ) ) );
577 : #else
578 : lf_hf_ge_r_fx = round_fx_o( L_shl_o( L_temp, sub( 15 + 16, sub( add( SWB_BWE_LR_Qbe, QIns ), 30 ) ), &Overflow ), &Overflow );
579 : Overflow = 0; /* reset BASOP Overflow */
580 : move32();
581 : #endif
582 :
583 0 : exp_normn = norm_s( norm_sum_fx );
584 0 : exp_normn = sub( exp_normn, 1 );
585 0 : exp_normd = norm_s( harmonic_band_fx );
586 :
587 0 : div_fx = div_s( shl( norm_sum_fx, exp_normn ), shl( harmonic_band_fx, exp_normd ) );
588 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 */
589 :
590 0 : test();
591 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) */
592 : {
593 0 : bits_allocweigh_fx = 6554;
594 0 : move16(); /* 0.2 6553.6(Q15) */
595 0 : if ( L_norm_diff < 0x0L )
596 : {
597 0 : bits_allocweigh_fx = 13107;
598 0 : move16(); /* 0.4 13107.2(Q15) */
599 : }
600 :
601 : /*allocate bits*/
602 : /*Bits_grp[GRP_SB-1] = (short)min((grp_bit_avg/p2a_bands + bits_allocweigh*norm_diff),10);*/
603 0 : L_temp = L_mult( grp_bit_avg_fx, Inv_p2a_bands_fx ); /* Q0+QIpb+1 */
604 0 : L_temp2 = Mult_32_16( L_norm_diff, bits_allocweigh_fx ); /* Qbe+Q15-15 */
605 :
606 0 : L_temp = L_shr( L_temp, add( QIpb, 1 ) );
607 0 : L_temp = L_add( L_shl( L_temp, SWB_BWE_LR_Qbe ), L_temp2 ); /* Qbe+Q15-15 */
608 :
609 0 : Bits_grp_fx[GRP_SB - 1] = extract_h( L_shl( L_temp, sub( 16, SWB_BWE_LR_Qbe ) ) ); /* Q0 */
610 0 : move16();
611 0 : Bits_grp_fx[GRP_SB - 1] = s_min( Bits_grp_fx[GRP_SB - 1], 10 ); /* Q0 */
612 0 : move16();
613 :
614 0 : if ( LT_16( Bits_grp_fx[GRP_SB - 1], esthf_bits_fx ) )
615 : {
616 0 : Bits_grp_fx[GRP_SB - 1] = 0;
617 0 : move16();
618 : }
619 0 : B_fx = sub( B_fx, Bits_grp_fx[GRP_SB - 1] ); /* Q0 */
620 : }
621 :
622 0 : exp_shift = sub( add( SWB_BWE_LR_Qbe, QIns ), 47 ); /* (SWB_BWE_LR_Qbe+14+1+QIns-15-16) */
623 0 : exp_norm = norm_s( B_fx );
624 0 : B_norm_fx = shl( B_fx, exp_norm );
625 0 : exp_shift = add( exp_shift, exp_norm );
626 :
627 0 : IF( EQ_16( final_gr_fact_pos_fx, 1 ) )
628 : {
629 0 : L_temp = Mult_32_16( L_Ravg_sub[1], extract_h( L_mult( bits_fact_fx, B_norm_fx ) ) );
630 0 : L_temp = Mult_32_16( L_temp, Inv_norm_sum_fx ); /* Q16 + exp_shift */
631 0 : Bits_grp_fx[1] = extract_h( L_shr( L_temp, exp_shift ) ); /* Q0 */
632 0 : move16();
633 :
634 0 : L_temp = Mult_32_16( L_Ravg_sub[2], extract_h( L_mult( bits_fact1_fx, B_norm_fx ) ) );
635 0 : L_temp = Mult_32_16( L_temp, Inv_norm_sum_fx ); /* Q16 + exp_shift */
636 0 : Bits_grp_fx[2] = extract_h( L_shr( L_temp, exp_shift ) ); /* Q0 */
637 0 : move16();
638 :
639 0 : Bits_grp_fx[0] = sub( sub( B_fx, Bits_grp_fx[1] ), Bits_grp_fx[2] ); /* Q0 */
640 0 : move16();
641 : }
642 : ELSE
643 : {
644 0 : L_temp = Mult_32_16( L_Ravg_sub[0], extract_h( L_mult( bits_fact_fx, B_norm_fx ) ) );
645 0 : L_temp = Mult_32_16( L_temp, Inv_norm_sum_fx ); /* Q16 + exp_shift */
646 0 : Bits_grp_fx[0] = extract_h( L_shr( L_temp, exp_shift ) ); /* Q0 */
647 0 : move16();
648 :
649 0 : L_temp = Mult_32_16( L_Ravg_sub[2], extract_h( L_mult( bits_fact1_fx, B_norm_fx ) ) );
650 0 : L_temp = Mult_32_16( L_temp, Inv_norm_sum_fx ); /* Q16 + exp_shift */
651 0 : Bits_grp_fx[2] = extract_h( L_shr( L_temp, exp_shift ) ); /* Q0 */
652 0 : move16();
653 :
654 0 : Bits_grp_fx[1] = sub( sub( B_fx, Bits_grp_fx[0] ), Bits_grp_fx[2] ); /* Q0 */
655 0 : move16();
656 : }
657 :
658 0 : IF( LT_16( Bits_grp_fx[2], THR2 ) )
659 : {
660 0 : Bits_grp_fx[1] = add( Bits_grp_fx[1], Bits_grp_fx[2] ); /* Q0 */
661 0 : move16();
662 0 : Bits_grp_fx[2] = 0;
663 0 : move16();
664 : }
665 :
666 0 : FOR( i = 0; i < GRP_SB; i++ )
667 : {
668 0 : IF( Bits_grp_fx[i] > 0 )
669 : {
670 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 );
671 : }
672 : ELSE
673 : {
674 0 : set32_fx( L_Rsubband + grp_bound_fx[i], 0x0L, sub( grp_bound_fx[i + 1], grp_bound_fx[i] ) );
675 0 : IF( EQ_16( i, GRP_SB - 1 ) )
676 : {
677 0 : set16_fx( p2a_flags_fx + grp_bound_fx[i], 0, sub( grp_bound_fx[i + 1], grp_bound_fx[i] ) );
678 : }
679 : }
680 : }
681 :
682 0 : return;
683 : }
684 :
685 : /*--------------------------------------------------------------------------*
686 : * hq2_bit_alloc()
687 : *
688 : * HQ2 bit-allocation
689 : *--------------------------------------------------------------------------*/
690 :
691 34 : Word32 hq2_bit_alloc_fx(
692 : const Word32 L_band_energy[], /* i : band energy of each subband */
693 : const Word16 bands, /* i : total number of subbands in a frame Q0*/
694 : Word32 L_Rk[], /* i/o: Bit allocation/Adjusted bit alloc. QRk*/
695 : Word16 *bit_budget_fx, /* i/o: bit bugdet Q0*/
696 : Word16 *p2a_flags, /* i : HF tonal indicator Q0*/
697 : const Word16 weight_fx, /* i : weight Q13*/
698 : const Word16 band_width[], /* i : Sub band bandwidth Q0*/
699 : const Word16 num_bits, /* i : available bits Q0*/
700 : const Word16 hqswb_clas, /* i : HQ2 class information Q0*/
701 : const Word16 bwidth, /* i : input bandwidth Q0*/
702 : const Word16 is_transient /* i : indicator HQ_TRANSIENT or not Q0*/
703 : )
704 : {
705 : Word16 j, k;
706 : Word16 tmp;
707 : Word16 bit_budget_norm_fx;
708 :
709 : Word32 L_Rcalc, L_Ravg, L_Rcalc1;
710 :
711 : Word16 exp_normn, exp_normd;
712 :
713 : Word16 Rcnt_fx;
714 :
715 : Word16 div_fx;
716 : Word16 Qdiv;
717 :
718 : Word32 L_tmp;
719 : Word16 tmp_fx;
720 :
721 : Word32 L_maxxy;
722 : Word16 maxdex_fx;
723 : Word32 L_dummy;
724 :
725 : Word16 bit_budget_temp_fx;
726 :
727 : Word16 negflag;
728 :
729 : Word32 L_THR1, L_THR2, L_THR3;
730 :
731 34 : L_THR1 = L_shl( L_deposit_l( THR1 ), SWB_BWE_LR_QRk ); /* SWB_BWE_LR_QRk */
732 34 : L_THR2 = L_shl( L_deposit_l( THR2 ), SWB_BWE_LR_QRk ); /* SWB_BWE_LR_QRk */
733 34 : L_THR3 = L_shl( L_deposit_l( THR3 ), SWB_BWE_LR_QRk ); /* SWB_BWE_LR_QRk */
734 :
735 : /* Init Rk to non-zero values for bands to be allocated bits */
736 34 : IF( LE_16( num_bits, HQ_16k40_BIT ) )
737 : {
738 34 : set32_fx( L_Rk, (Word32) ( C1_QRk ), bands ); /* 1<<SWB_BWE_LR_QRk */
739 :
740 34 : test();
741 34 : IF( is_transient && EQ_16( bands, 32 ) )
742 : {
743 3 : L_Rk[6] = L_deposit_l( 0 );
744 3 : move32();
745 3 : L_Rk[7] = L_deposit_l( 0 );
746 3 : move32();
747 3 : L_Rk[14] = L_deposit_l( 0 );
748 3 : move32();
749 3 : L_Rk[15] = L_deposit_l( 0 );
750 3 : move32();
751 3 : L_Rk[22] = L_deposit_l( 0 );
752 3 : move32();
753 3 : L_Rk[23] = L_deposit_l( 0 );
754 3 : move32();
755 3 : L_Rk[30] = L_deposit_l( 0 );
756 3 : move32();
757 3 : L_Rk[31] = L_deposit_l( 0 );
758 3 : move32();
759 : }
760 : }
761 : ELSE
762 : {
763 : /*mvs2r( p2a_flags, Rk, bands ); */
764 0 : FOR( k = 0; k < bands; k++ )
765 : {
766 0 : L_Rk[k] = L_shl( L_deposit_l( p2a_flags[k] ), SWB_BWE_LR_QRk ); /* QRk */
767 0 : move32();
768 : }
769 : }
770 :
771 34 : L_Rcalc = L_deposit_l( 0 );
772 34 : L_Rcalc1 = L_deposit_l( 0 );
773 :
774 246 : FOR( j = 0; j < bands; j++ )
775 : {
776 246 : Rcnt_fx = 0;
777 246 : move16();
778 246 : L_Ravg = 0x0L;
779 246 : move32();
780 :
781 5758 : FOR( k = 0; k < bands; k++ )
782 : {
783 5512 : IF( L_Rk[k] > 0 )
784 : {
785 4530 : 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 */
786 4530 : Rcnt_fx = add( Rcnt_fx, 1 );
787 : }
788 : }
789 : /* Ravg Qband_energy */
790 :
791 : /*L_Ravg /= Rcnt; */
792 246 : exp_normd = norm_l( L_Ravg );
793 246 : exp_normd = sub( exp_normd, 1 );
794 246 : exp_normn = norm_s( Rcnt_fx );
795 :
796 246 : tmp = shl( Rcnt_fx, exp_normn );
797 246 : tmp = s_max( tmp, 1 );
798 246 : IF( L_Ravg > 0 )
799 : {
800 246 : div_fx = div_l( L_shl( L_Ravg, exp_normd ), tmp ); /* Qdiv = 14+exp_normd-(exp_normn)-1 */
801 : }
802 : ELSE
803 : {
804 0 : div_fx = div_l( L_shl( L_abs( L_Ravg ), exp_normd ), tmp ); /* Qdiv = 14+exp_normd-(exp_normn)-1 */
805 0 : div_fx = negate( div_fx );
806 : }
807 :
808 246 : Qdiv = sub( sub( add( SWB_BWE_LR_QRk, exp_normd ), exp_normn ), 1 );
809 :
810 246 : L_Ravg = L_shr( L_deposit_l( div_fx ), sub( Qdiv, SWB_BWE_LR_QRk ) ); /* QRk */
811 :
812 246 : exp_normd = norm_s( *bit_budget_fx );
813 246 : exp_normd = sub( exp_normd, 1 );
814 246 : bit_budget_norm_fx = shl( *bit_budget_fx, exp_normd ); /* exp_normd */
815 246 : div_fx = 0;
816 246 : move16();
817 :
818 246 : test();
819 246 : IF( bit_budget_norm_fx > 0 && LT_16( bit_budget_norm_fx, tmp ) )
820 : {
821 246 : div_fx = div_s( bit_budget_norm_fx, tmp );
822 : }
823 246 : Qdiv = add( sub( exp_normd, exp_normn ), 15 );
824 5758 : FOR( k = 0; k < bands; k++ )
825 : {
826 5512 : IF( L_Rk[k] > 0 )
827 : {
828 : /*Rk[k] = ((float) *bit_budget / Rcnt + weight * (band_energy[k] - Ravg)); */
829 :
830 4530 : 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 */
831 4530 : L_tmp = L_shl( L_tmp, 2 ); /* -> SWB_BWE_LR_QRk */
832 :
833 4530 : L_Rk[k] = L_add( L_shr( L_deposit_l( div_fx ), sub( Qdiv, SWB_BWE_LR_QRk ) ), L_tmp ); /* SWB_BWE_LR_QRk */
834 4530 : move32();
835 : }
836 : }
837 :
838 246 : negflag = 0;
839 246 : move16();
840 246 : L_Rcalc = L_deposit_l( 0 );
841 5758 : FOR( k = 0; k < bands; k++ )
842 : {
843 5512 : IF( LT_32( L_Rk[k], MIN_BITS_FIX ) )
844 : {
845 30 : L_Rk[k] = L_deposit_l( 0 );
846 30 : move32();
847 30 : negflag = 1;
848 30 : move16();
849 : }
850 5512 : L_Rcalc = L_add( L_Rcalc, L_Rk[k] ); /*SWB_BWE_LR_QRk */
851 : }
852 :
853 : /* prune noiselike bands with low allocation */
854 246 : test();
855 246 : IF( LE_16( num_bits, HQ_16k40_BIT ) && negflag == 0 )
856 : {
857 236 : L_maxxy = L_deposit_l( 0 );
858 236 : maxdex_fx = -1;
859 236 : move16();
860 236 : L_Rcalc = L_deposit_l( 0 );
861 :
862 : /* find worst under-allocation */
863 5488 : FOR( k = bands - 1; k >= 0; k-- )
864 : {
865 5252 : tmp_fx = s_min( band_width[k], s_max( 12, shr( band_width[k], 2 ) ) ); /* Q0 */
866 5252 : L_dummy = L_sub( L_shl( L_deposit_l( tmp_fx ), SWB_BWE_LR_QRk ), L_Rk[k] ); /*SWB_BWE_LR_QRk */
867 5252 : test();
868 5252 : test();
869 5252 : IF( p2a_flags[k] == 0 && GT_32( L_dummy, L_maxxy ) && L_Rk[k] > 0 )
870 : {
871 146 : maxdex_fx = k;
872 146 : move16();
873 146 : L_maxxy = L_dummy; /*SWB_BWE_LR_QRk */
874 146 : move32();
875 : }
876 : }
877 :
878 : /* prune worst allocation and recalculate total allocation */
879 236 : IF( GT_16( maxdex_fx, -1 ) )
880 : {
881 140 : L_Rk[maxdex_fx] = L_deposit_l( 0 );
882 140 : move32();
883 : }
884 5488 : FOR( k = 0; k < bands; k++ )
885 : {
886 5252 : L_Rcalc = L_add( L_Rcalc, L_Rk[k] ); /*SWB_BWE_LR_QRk */
887 : }
888 : }
889 246 : test();
890 246 : test();
891 246 : IF( EQ_32( L_Rcalc, L_Rcalc1 ) && EQ_16( bwidth, SWB ) )
892 : {
893 : /* Reallocate bits to individual subbands for HQ_NORMAL mode */
894 : /* if bits allocated to subbands areless than predefined threshold */
895 48 : test();
896 48 : IF( EQ_16( hqswb_clas, HQ_NORMAL ) && LT_16( num_bits, HQ_16k40_BIT ) )
897 : {
898 45 : L_dummy = L_deposit_l( 0 );
899 1035 : FOR( k = 0; k < bands; k++ )
900 : {
901 990 : test();
902 990 : test();
903 990 : test();
904 990 : test();
905 990 : test();
906 990 : IF( LT_16( k, 11 ) && LT_32( L_Rk[k], L_THR1 ) )
907 : {
908 62 : L_Rk[k] = L_deposit_l( 0 );
909 62 : move32();
910 : }
911 928 : ELSE IF( GE_16( k, 11 ) && LT_16( k, 16 ) && LT_32( L_Rk[k], L_THR2 ) )
912 : {
913 22 : L_Rk[k] = L_deposit_l( 0 );
914 22 : move32();
915 : }
916 906 : ELSE if ( GE_16( k, 16 ) && LT_16( k, bands ) && LT_32( L_Rk[k], L_THR3 ) )
917 : {
918 219 : L_Rk[k] = L_deposit_l( 0 );
919 219 : move32();
920 : }
921 :
922 990 : L_dummy = L_add( L_dummy, L_Rk[k] );
923 : }
924 :
925 45 : IF( EQ_32( L_dummy, L_Rcalc ) )
926 : {
927 33 : test();
928 33 : IF( EQ_16( hqswb_clas, HQ_NORMAL ) && LT_16( num_bits, HQ_16k40_BIT ) )
929 : {
930 33 : bit_budget_temp_fx = *bit_budget_fx; /* Q0 */
931 33 : move16();
932 165 : FOR( k = 0; k < NB_SWB_SUBBANDS; k++ )
933 : {
934 132 : test();
935 132 : IF( EQ_16( p2a_flags[( bands - NB_SWB_SUBBANDS ) + k], 1 ) && L_Rk[( bands - NB_SWB_SUBBANDS ) + k] == 0 )
936 : {
937 2 : p2a_flags[( bands - NB_SWB_SUBBANDS ) + k] = 0;
938 2 : move16();
939 2 : bit_budget_temp_fx = sub( bit_budget_temp_fx, bits_lagIndices_modeNormal[k] );
940 : }
941 : }
942 :
943 33 : IF( LT_16( bit_budget_temp_fx, *bit_budget_fx ) )
944 : {
945 2 : *bit_budget_fx = bit_budget_temp_fx;
946 2 : move16();
947 : /* a negative *bit_budget_fx may occur here due to Bit Errors */
948 : /* handled outside this function to properly set flag: st_fx->BER_detect */
949 : }
950 31 : ELSE IF( EQ_16( bit_budget_temp_fx, *bit_budget_fx ) )
951 : {
952 31 : BREAK;
953 : }
954 : }
955 : ELSE
956 : {
957 : BREAK;
958 : }
959 : }
960 : }
961 : ELSE
962 : {
963 : BREAK;
964 : }
965 : }
966 198 : ELSE IF( EQ_32( L_Rcalc, L_Rcalc1 ) && NE_16( bwidth, SWB ) )
967 : {
968 0 : BREAK;
969 : }
970 :
971 212 : L_Rcalc1 = L_Rcalc;
972 212 : move32();
973 : }
974 :
975 34 : return L_Rcalc;
976 : }
|