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