Line data Source code
1 : /******************************************************************************************************
2 :
3 : (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
4 : Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
5 : Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
6 : Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
7 : contributors to this repository. All Rights Reserved.
8 :
9 : This software is protected by copyright law and by international treaties.
10 : The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
11 : Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
12 : Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
13 : Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
14 : contributors to this repository retain full ownership rights in their respective contributions in
15 : the software. This notice grants no license of any kind, including but not limited to patent
16 : license, nor is any license granted by implication, estoppel or otherwise.
17 :
18 : Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
19 : contributions.
20 :
21 : This software is provided "AS IS", without any express or implied warranties. The software is in the
22 : development stage. It is intended exclusively for experts who have experience with such software and
23 : solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
24 : and fitness for a particular purpose are hereby disclaimed and excluded.
25 :
26 : Any dispute, controversy or claim arising under or in relation to providing this software shall be
27 : submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
28 : accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
29 : the United Nations Convention on Contracts on the International Sales of Goods.
30 :
31 : *******************************************************************************************************/
32 :
33 : /*====================================================================================
34 : EVS Codec 3GPP TS26.443 Nov 04, 2021. Version 12.14.0 / 13.10.0 / 14.6.0 / 15.4.0 / 16.3.0
35 : ====================================================================================*/
36 :
37 : #include <stdint.h>
38 : #include "options.h"
39 : #ifdef DEBUGGING
40 : #include "debug.h"
41 : #endif
42 : #include "cnst.h"
43 : #include "rom_com.h"
44 : #include "prot.h"
45 : #include "basop_util.h"
46 : #include "basop_proto_func.h"
47 : #include "wmc_auto.h"
48 :
49 : /*--------------------------------------------------------------------------
50 : * bitalloc()
51 : *
52 : * Adaptive bit allocation for 20kHz audio codec
53 : *--------------------------------------------------------------------------*/
54 :
55 198242 : void bitalloc(
56 : int16_t *y, /* i : reordered norm of sub-vectors */
57 : int16_t *idx, /* i : reordered sub-vector indices */
58 : int16_t sum, /* i : number of available bits */
59 : int16_t N, /* i : number of norms */
60 : int16_t K, /* i : maximum number of bits per dimension */
61 : int16_t *r, /* o : bit-allacation vector */
62 : const int16_t *sfmsize, /* i : band length */
63 : const int16_t hqswb_clas /* i : signal classification flag */
64 : )
65 : {
66 : int16_t i, j, k, n, m, v, im;
67 : int16_t diff, temp;
68 : int16_t fac;
69 : int16_t ii;
70 198242 : int16_t SFM_thr = SFM_G1G2;
71 :
72 198242 : N -= 1;
73 :
74 198242 : if ( hqswb_clas == HQ_HARMONIC )
75 : {
76 36198 : SFM_thr = 22;
77 : }
78 :
79 198242 : fac = 3;
80 198242 : K -= 2;
81 198242 : im = 1;
82 198242 : diff = sum;
83 198242 : n = sum >> 3;
84 10060196 : for ( i = 0; i < n; i++ )
85 : {
86 10060196 : k = 0;
87 10060196 : temp = y[0];
88 139466404 : for ( m = 1; m < im; m++ )
89 : {
90 129406208 : if ( temp < y[m] )
91 : {
92 11768455 : temp = y[m];
93 11768455 : k = m;
94 : }
95 : }
96 :
97 10060196 : if ( temp < y[m] )
98 : {
99 4563844 : k = m;
100 4563844 : if ( im < N )
101 : {
102 4552381 : im++;
103 : }
104 : }
105 :
106 10060196 : j = idx[k];
107 10060196 : if ( sum >= sfmsize[j] && r[j] < K )
108 : {
109 9979884 : y[k] -= fac;
110 9979884 : r[j]++;
111 9979884 : if ( r[j] >= K )
112 : {
113 111469 : y[k] = MIN16B;
114 : }
115 9979884 : sum -= sfmsize[j];
116 : }
117 : else
118 : {
119 80312 : y[k] = MIN16B;
120 80312 : k++;
121 80312 : if ( k == im && im < N )
122 : {
123 48215 : im++;
124 : }
125 : }
126 :
127 10060196 : if ( sum < sfmsize[SFM_G1 - 1] || diff == sum ) /* sfmsize[SFM_G1-1] matches WID_G1, but also allows for extended BWs used in ACELP->HQ switching. */
128 : {
129 : break;
130 : }
131 :
132 9861954 : diff = sum;
133 9861954 : v = N - 1;
134 :
135 9861954 : if ( k > v )
136 : {
137 25324 : for ( ii = 0; ii <= N; ii++ )
138 : {
139 25324 : if ( y[ii] > MIN16B )
140 : {
141 10617 : if ( ii < N )
142 : {
143 10617 : im = ii + 1;
144 : }
145 :
146 10617 : break;
147 : }
148 : }
149 : }
150 : }
151 :
152 198242 : if ( sum >= sfmsize[SFM_G1] ) /* sfmsize[SFM_G1] matches WID_G2, but also allows for extended BWs used in ACELP->HQ switching. */
153 : {
154 859274 : for ( i = 0; i <= N; i++ )
155 : {
156 848788 : j = idx[i];
157 848788 : if ( j >= SFM_G1 && j < SFM_thr && r[j] == 0 )
158 : {
159 19395 : r[j] = 1;
160 19395 : sum -= sfmsize[j];
161 19395 : if ( sum < sfmsize[SFM_G1] )
162 : {
163 18513 : break;
164 : }
165 : }
166 : }
167 : }
168 :
169 198242 : if ( sum >= sfmsize[SFM_G1] )
170 : {
171 225913 : for ( i = 0; i <= N; i++ )
172 : {
173 224150 : j = idx[i];
174 224150 : if ( j >= SFM_G1 && j < SFM_thr && r[j] == 1 )
175 : {
176 9112 : r[j] = 2;
177 9112 : sum -= sfmsize[j];
178 9112 : if ( sum < sfmsize[SFM_G1] )
179 : {
180 8723 : break;
181 : }
182 : }
183 : }
184 : }
185 :
186 198242 : if ( sum >= sfmsize[SFM_G1 - 1] )
187 : {
188 2079605 : for ( i = 0; i <= N; i++ )
189 : {
190 2040444 : j = idx[i];
191 2040444 : if ( j < SFM_G1 && r[j] == 0 )
192 : {
193 22280 : r[j] = 1;
194 22280 : sum -= sfmsize[j];
195 22280 : if ( sum < sfmsize[SFM_G1 - 1] )
196 : {
197 22240 : break;
198 : }
199 : }
200 : }
201 : }
202 :
203 198242 : if ( sum >= sfmsize[SFM_G1 - 1] )
204 : {
205 1165202 : for ( i = 0; i <= N; i++ )
206 : {
207 1143105 : j = idx[i];
208 1143105 : if ( j < SFM_G1 && r[j] == 1 )
209 : {
210 17314 : r[j] = 2;
211 17314 : sum -= sfmsize[j];
212 17314 : if ( sum < sfmsize[SFM_G1 - 1] )
213 : {
214 17064 : break;
215 : }
216 : }
217 : }
218 : }
219 :
220 198242 : return;
221 : }
222 :
223 :
224 : #define WMC_TOOL_SKIP
225 :
226 : /*-------------------------------------------------------------------*
227 : * BitAllocF()
228 : *
229 : * Fractional bit allocation
230 : *-------------------------------------------------------------------*/
231 :
232 : /*! r: Integer (truncated) number of allocated bits */
233 55869 : int16_t BitAllocF(
234 : int16_t *y, /* i : norm of sub-vectors */
235 : int32_t bit_rate, /* i : bitrate */
236 : int16_t B, /* i : number of available bits */
237 : int16_t N, /* i : number of sub-vectors */
238 : int16_t *R, /* o : bit-allocation indicator */
239 : int16_t *Rsubband, /* o : sub-band bit-allocation vector (Q3) */
240 : const int16_t hqswb_clas, /* i : hq swb class */
241 : const int16_t num_env_bands /* i : Number sub bands to be encoded for HQ_SWB_BWE */
242 : )
243 : {
244 : Word16 fac;
245 55869 : Word16 i, n, Nmin, Bits, bs, low_rate = 0;
246 :
247 : Word16 m_fx;
248 : Word32 t_fx, B_fx;
249 : Word32 L_tmp1, L_tmp2;
250 : Word16 tmp, exp1, exp2;
251 : Word32 Rsubband_w32_fx[NB_SFM]; /* Q15 */
252 : Word16 B_w16_fx;
253 : #ifdef BASOP_NOGLOB
254 : Flag Overflow;
255 : #endif /* BASOP_NOGLOB */
256 :
257 55869 : set_l( Rsubband_w32_fx, 0, NB_SFM );
258 :
259 55869 : fac = 3;
260 55869 : if ( L_sub( bit_rate, 32000 ) < 0 )
261 : {
262 54486 : bs = 2;
263 : }
264 : else
265 : {
266 1383 : bs = 3;
267 : }
268 55869 : low_rate = 1;
269 :
270 55869 : Nmin = N;
271 55869 : if ( sub( Nmin, SFM_N ) > 0 )
272 : {
273 55869 : Nmin = SFM_N;
274 : }
275 :
276 : /* Initial bits distribution */
277 55869 : if ( sub( hqswb_clas, HQ_GEN_SWB ) == 0 || sub( hqswb_clas, HQ_GEN_FB ) == 0 )
278 : {
279 : /* Initial bits distribution */
280 49676 : L_tmp1 = 0;
281 49676 : m_fx = 0;
282 1394600 : for ( i = 0; i < num_env_bands; i++ )
283 : {
284 1344924 : L_tmp1 = L_mac0( L_tmp1, Nb[i], y[i] );
285 : }
286 49676 : L_tmp1 = L_msu0( L_tmp1, fac, B );
287 :
288 49676 : t_fx = 0;
289 49676 : n = 0;
290 49676 : tmp = add( band_end_HQ[num_env_bands - 1], shl( band_end_HQ[num_env_bands - 1], 1 ) );
291 49676 : exp1 = norm_s( tmp );
292 49676 : tmp = div_s( 16384, shl( tmp, exp1 ) ); /*15 + 14 - exp1*/
293 49676 : exp2 = norm_s( tmp );
294 49676 : tmp = shl( tmp, exp2 );
295 49676 : exp1 = add( 29, sub( exp2, exp1 ) );
296 :
297 1987040 : for ( i = 0; i < N; i++ )
298 : {
299 1937364 : L_tmp2 = L_sub( L_mult0( y[i], band_end_HQ[num_env_bands - 1] ), L_tmp1 );
300 1937364 : Rsubband_w32_fx[i] = L_mult0( extract_l( L_tmp2 ), Nb[i] );
301 1937364 : move32(); /*Q0*/
302 1937364 : if ( Rsubband_w32_fx[i] > 0 )
303 : {
304 1097704 : n = add( n, Nb[i] );
305 1097704 : Rsubband_w32_fx[i] = Mpy_32_16( Rsubband_w32_fx[i], tmp );
306 1097704 : move32(); /*exp1 - 15*/
307 1097704 : Rsubband_w32_fx[i] = L_shl( Rsubband_w32_fx[i], sub( 30, exp1 ) ); /*Q15*/
308 :
309 1097704 : t_fx = L_add( t_fx, Rsubband_w32_fx[i] ); /*Q0*/
310 : }
311 : else
312 : {
313 839660 : Rsubband_w32_fx[i] = 0;
314 839660 : move32();
315 : }
316 : }
317 : }
318 : else
319 : {
320 : /* Initial bits distribution */
321 6193 : L_tmp1 = 0;
322 6193 : m_fx = 0;
323 247720 : for ( i = 0; i < N; i++ )
324 : {
325 241527 : L_tmp1 = L_mac0( L_tmp1, Nb[i], y[i] );
326 : }
327 6193 : L_tmp1 = L_msu0( L_tmp1, fac, B );
328 :
329 :
330 6193 : t_fx = 0;
331 6193 : n = 0;
332 6193 : tmp = add( band_end_HQ[N - 1], shl( band_end_HQ[N - 1], 1 ) );
333 6193 : exp1 = norm_s( tmp );
334 6193 : tmp = div_s( 16384, shl( tmp, exp1 ) ); /*15 + 14 - exp1*/
335 6193 : exp2 = norm_s( tmp );
336 6193 : tmp = shl( tmp, exp2 );
337 6193 : exp1 = add( 29, sub( exp2, exp1 ) );
338 247720 : for ( i = 0; i < N; i++ )
339 : {
340 241527 : L_tmp2 = L_sub( L_mult0( y[i], band_end_HQ[N - 1] ), L_tmp1 );
341 241527 : Rsubband_w32_fx[i] = L_mult0( extract_l( L_tmp2 ), Nb[i] );
342 241527 : move32(); /*Q0*/
343 241527 : if ( Rsubband_w32_fx[i] > 0 )
344 : {
345 146835 : n = add( n, Nb[i] );
346 146835 : Rsubband_w32_fx[i] = Mpy_32_16( Rsubband_w32_fx[i], tmp );
347 146835 : move32(); /*exp1 - 15*/
348 146835 : Rsubband_w32_fx[i] = L_shl( Rsubband_w32_fx[i], sub( 30, exp1 ) ); /*Q15*/
349 :
350 146835 : t_fx = L_add( t_fx, Rsubband_w32_fx[i] ); /*Q0*/
351 : }
352 : else
353 : {
354 94692 : Rsubband_w32_fx[i] = 0;
355 94692 : move32();
356 : }
357 : }
358 : }
359 :
360 : /* Distribute the remaining bits to subbands with non-zero bits */
361 55869 : B_fx = L_shl( B, 15 );
362 129770 : WHILE( L_sub( L_shr( L_add( t_fx, 16384 ), 15 ), B ) != 0 )
363 : {
364 73901 : L_tmp1 = L_sub( t_fx, B_fx );
365 73901 : exp1 = sub( norm_l( L_tmp1 ), 1 );
366 73901 : exp2 = norm_s( n );
367 73901 : tmp = div_s( extract_h( L_shl( L_tmp1, exp1 ) ), shl( n, exp2 ) ); /*15 + 15 + exp1 - 16 - exp2*/
368 : #ifndef BASOP_NOGLOB
369 : m_fx = shl( tmp, sub( exp2, exp1 ) ); /*Q14*/
370 : #else
371 73901 : m_fx = shl_o( tmp, sub( exp2, exp1 ), &Overflow ); /*Q14*/
372 : #endif
373 :
374 73901 : t_fx = 0;
375 73901 : n = 0;
376 2956040 : for ( i = 0; i < N; i++ )
377 : {
378 2882139 : if ( Rsubband_w32_fx[i] > 0 )
379 : {
380 1480308 : Rsubband_w32_fx[i] = L_msu( Rsubband_w32_fx[i], m_fx, Nb[i] );
381 1480308 : move32();
382 :
383 1480308 : if ( Rsubband_w32_fx[i] > 0 )
384 : {
385 1394918 : n = add( n, Nb[i] );
386 :
387 1394918 : t_fx = L_add( t_fx, Rsubband_w32_fx[i] );
388 : }
389 : else
390 : {
391 85390 : Rsubband_w32_fx[i] = 0;
392 85390 : move32();
393 : }
394 : }
395 : }
396 : }
397 55869 : Bits = B;
398 :
399 : /* Impose bit-constraints to subbands with less than minimum bits*/
400 55869 : t_fx = 0;
401 55869 : n = 0;
402 2234760 : for ( i = 0; i < N; i++ )
403 : {
404 2178891 : if ( Rsubband_w32_fx[i] > 0 )
405 : {
406 1159149 : test();
407 1159149 : test();
408 1159149 : if ( ( L_sub( Rsubband_w32_fx[i], L_shl( add( bs, LNb[i] ), 15 ) ) < 0 ) && ( sub( low_rate, 1 ) == 0 ) )
409 : {
410 218360 : Rsubband_w32_fx[i] = 0;
411 218360 : move32();
412 : }
413 940789 : else if ( L_sub( Rsubband_w32_fx[i], L_shl( Nb[i], 15 ) ) <= 0 )
414 : {
415 306116 : B = sub( B, Nb[i] );
416 306116 : Rsubband_w32_fx[i] = L_shl( Nb[i], 15 );
417 306116 : move32();
418 : }
419 : else
420 : {
421 634673 : n = add( n, Nb[i] );
422 634673 : t_fx = L_add( t_fx, Rsubband_w32_fx[i] );
423 : }
424 : }
425 : }
426 :
427 : /* Distribute the remaining bits to subbands with more than 1-bit per sample */
428 126510 : WHILE( L_sub( L_shr( L_add( t_fx, 16384 ), 15 ), B ) != 0 )
429 : {
430 74849 : L_tmp1 = L_sub( t_fx, L_shl( B, 15 ) );
431 74849 : L_tmp2 = L_abs( L_tmp1 );
432 :
433 74849 : if ( n > 0 )
434 : {
435 74821 : exp1 = sub( norm_l( L_tmp2 ), 1 );
436 74821 : exp2 = norm_s( n );
437 74821 : tmp = div_s( extract_h( L_shl( L_tmp2, exp1 ) ), shl( n, exp2 ) ); /*15 + 15 + exp1 - 16 - exp2*/
438 : #ifndef BASOP_NOGLOB
439 : m_fx = shl( tmp, sub( exp2, exp1 ) ); /*Q14*/
440 : #else /* BASOP_NOGLOB */
441 74821 : m_fx = shl_o( tmp, sub( exp2, exp1 ), &Overflow ); /*Q14*/
442 : #endif /* BASOP_NOGLOB */
443 74821 : if ( L_tmp1 < 0 )
444 : {
445 14489 : m_fx = negate( m_fx );
446 : }
447 :
448 74821 : t_fx = 0;
449 74821 : n = 0;
450 2992840 : for ( i = 0; i < N; i++ )
451 : {
452 2918019 : if ( L_sub( Rsubband_w32_fx[i], L_shl( Nb[i], 15 ) ) > 0 )
453 : {
454 751673 : Rsubband_w32_fx[i] = L_msu( Rsubband_w32_fx[i], m_fx, Nb[i] );
455 751673 : if ( L_sub( Rsubband_w32_fx[i], L_shl( Nb[i], 15 ) ) > 0 )
456 : {
457 664051 : n = add( n, Nb[i] );
458 :
459 664051 : t_fx = L_add( t_fx, Rsubband_w32_fx[i] );
460 : }
461 : else
462 : {
463 87622 : B = sub( B, Nb[i] );
464 :
465 87622 : Rsubband_w32_fx[i] = L_shl( Nb[i], 15 );
466 87622 : move32();
467 : }
468 : }
469 : }
470 : }
471 : /*In case no subband has enough bits more than 1-bit per sample, take bits off the higher subbands */
472 74849 : if ( t_fx == 0 )
473 : {
474 31546 : for ( i = N - 1; i >= 0; i-- )
475 : {
476 31546 : if ( Rsubband_w32_fx[i] > 0 )
477 : {
478 7700 : B = add( B, Nb[i] );
479 7700 : Rsubband_w32_fx[i] = 0;
480 7700 : move32();
481 7700 : if ( B >= 0 )
482 : {
483 4208 : BREAK;
484 : }
485 : }
486 : }
487 4208 : BREAK;
488 : }
489 : }
490 :
491 : /* fine redistribution of over-allocated or under-allocated bits */
492 55869 : tmp = 0;
493 2234760 : for ( i = 0; i < N; i++ )
494 : {
495 2178891 : Rsubband[i] = extract_l( L_shr( Rsubband_w32_fx[i], 12 ) );
496 2178891 : tmp = add( tmp, Rsubband[i] );
497 : }
498 :
499 55869 : B = Bits;
500 55869 : B_w16_fx = shl( B, 3 );
501 55869 : if ( sub( tmp, B_w16_fx ) > 0 )
502 : {
503 416 : tmp = sub( tmp, B_w16_fx );
504 1780 : for ( i = 0; i < N; i++ )
505 : {
506 1780 : if ( sub( Rsubband[i], add( shl( Nb[i], 3 ), tmp ) ) >= 0 )
507 : {
508 416 : Rsubband[i] = sub( Rsubband[i], tmp );
509 416 : BREAK;
510 : }
511 : }
512 : }
513 : else
514 : {
515 55453 : tmp = sub( tmp, B_w16_fx );
516 124204 : for ( i = 0; i < N; i++ )
517 : {
518 124204 : if ( Rsubband[i] > 0 )
519 : {
520 55453 : Rsubband[i] = sub( Rsubband[i], tmp );
521 55453 : BREAK;
522 : }
523 : }
524 : }
525 :
526 : /* Calculate total used bits and initialize R to be used for Noise Filling */
527 55869 : tmp = 0;
528 2234760 : for ( i = 0; i < N; i++ )
529 : {
530 2178891 : tmp = add( tmp, Rsubband[i] );
531 2178891 : R[i] = shr( Rsubband[i], 3 );
532 : }
533 :
534 55869 : return shr( tmp, 3 );
535 : }
536 :
537 : /*-------------------------------------------------------------------*
538 : * Bit_group()
539 : *
540 : * bit allocation in group
541 : *-------------------------------------------------------------------*/
542 127419 : static void Bit_group_fx(
543 : Word16 *y, /* i : norm of sub-band Q0*/
544 : Word16 start_band, /* i : start band indices Q0*/
545 : Word16 end_band, /* i : end band indices Q0*/
546 : Word16 Bits, /* i : number of allocation bits in group Q0*/
547 : Word16 thr, /* i : smallest bit number for allocation in group Q0*/
548 : Word32 *Rsubband_fx, /* o : bit allocation of sub-band Q21*/
549 : Word16 *fac_fx /* i : weight factor for norm of sub-band Q13*/
550 : )
551 : {
552 : Word16 i, j, k, m, y_index[16], index[16], bit_band, band_num, norm_sum;
553 : Word16 tmp, exp;
554 : Word16 factor_fx;
555 127419 : Word32 R_temp_fx[16], R_sum_fx = 0, R_sum_org_fx = 0, Bits_avg_fx = 0;
556 : Word32 L_tmp;
557 : UWord32 lo;
558 :
559 : /* initialization for bit allocation in one group*/
560 127419 : tmp = 6554;
561 127419 : move16(); /*Q15 1/5 */
562 127419 : IF( sub( thr, 5 ) == 0 )
563 : {
564 42473 : tmp = 6554;
565 42473 : move16(); /*Q15 1/5 */
566 : }
567 127419 : IF( sub( thr, 6 ) == 0 )
568 : {
569 42473 : tmp = 5462;
570 42473 : move16(); /*Q15 1/6 */
571 : }
572 127419 : IF( sub( thr, 7 ) == 0 )
573 : {
574 42473 : tmp = 4682;
575 42473 : move16(); /*Q15 1/7 */
576 : }
577 127419 : bit_band = mult( tmp, Bits ); /*0+15-15=0, Q0 */
578 127419 : band_num = sub( end_band, start_band );
579 :
580 1231717 : FOR( i = 0; i < band_num; i++ )
581 : {
582 1104298 : y_index[i] = y[add( i, start_band )];
583 1104298 : move16();
584 1104298 : index[i] = i;
585 1104298 : move16();
586 : }
587 :
588 : /* Rearrange norm vector in decreasing order */
589 127419 : reordvct( y_index, band_num, index );
590 : /* norm vector modification */
591 :
592 127419 : factor_fx = div_s( 1, band_num ); /*Q15 */
593 127419 : IF( sub( thr, 5 ) > 0 )
594 : {
595 509676 : FOR( i = 0; i < band_num; i++ )
596 : {
597 424730 : L_tmp = L_mult( i, factor_fx ); /*Q16 */
598 424730 : tmp = extract_h( L_shl( L_tmp, 13 ) ); /*Q13 */
599 424730 : tmp = sub( fac_fx[1], tmp ); /*Q13 */
600 424730 : L_tmp = L_mult( y_index[i], tmp ); /*Q14 */
601 424730 : y_index[i] = extract_h( L_shl( L_tmp, 2 ) ); /*Q0 */
602 : }
603 : }
604 : ELSE
605 : {
606 722041 : FOR( i = 0; i < band_num; i++ )
607 : {
608 679568 : L_tmp = L_mult( i, factor_fx ); /*Q16 */
609 679568 : tmp = extract_h( L_shl( L_tmp, 13 ) ); /*Q13 */
610 679568 : tmp = sub( fac_fx[0], tmp ); /*Q13 */
611 679568 : L_tmp = L_mult( y_index[i], tmp ); /*Q14 */
612 679568 : y_index[i] = extract_h( L_shl( L_tmp, 2 ) ); /*Q0 */
613 : }
614 : }
615 :
616 : /* bit allocation based on modified norm */
617 127419 : L_tmp = L_mult( band_num, 24576 ); /*Q16 */
618 127419 : tmp = extract_h( L_shl( L_tmp, 7 ) ); /*Q7 */
619 127419 : IF( sub( shl( bit_band, 7 ), tmp ) >= 0 )
620 : {
621 1183904 : FOR( j = 0; j < band_num; j++ )
622 : {
623 1065190 : IF( y_index[j] < 0 )
624 : {
625 9991 : y_index[j] = 0;
626 9991 : move16();
627 : }
628 1065190 : R_temp_fx[j] = 2097152;
629 1065190 : move16(); /*Q21 = 1 move16(); */
630 : }
631 :
632 118714 : i = sub( band_num, 1 );
633 118714 : norm_sum = 0; /*Q0 */
634 1183904 : FOR( k = 0; k <= i; k++ )
635 : {
636 1065190 : norm_sum = add( norm_sum, y_index[k] );
637 : }
638 :
639 237483 : FOR( j = 0; j < band_num; j++ )
640 : {
641 237483 : IF( norm_sum == 0 )
642 : {
643 0 : FOR( k = 0; k <= i; k++ )
644 : {
645 0 : R_temp_fx[k] = 0;
646 0 : move32(); /*Q21 */
647 : }
648 : }
649 : ELSE
650 : {
651 237483 : exp = norm_s( norm_sum );
652 237483 : tmp = shl( norm_sum, exp ); /*Q(exp) */
653 237483 : tmp = div_s( 16384, tmp ); /*Q(15+14-exp) */
654 237483 : Bits_avg_fx = L_mult( tmp, Bits ); /*Q(30-exp) */
655 :
656 2366301 : FOR( k = 0; k <= i; k++ )
657 : {
658 2128818 : L_tmp = L_shl( L_deposit_l( y_index[k] ), 24 );
659 2128818 : Mpy_32_32_ss( Bits_avg_fx, L_tmp, &L_tmp, &lo );
660 :
661 2128818 : R_temp_fx[k] = L_shl( L_tmp, sub( exp, 2 ) );
662 2128818 : move32(); /*Q21 */
663 : }
664 : }
665 :
666 237483 : L_tmp = L_shl( L_deposit_l( thr ), 21 ); /*Q21 */
667 237483 : IF( L_sub( R_temp_fx[i], L_tmp ) < 0 )
668 : {
669 118769 : R_temp_fx[i] = 0;
670 118769 : move32();
671 118769 : norm_sum = sub( norm_sum, y_index[i] );
672 118769 : i--;
673 : }
674 : ELSE
675 : {
676 118714 : BREAK;
677 : }
678 : }
679 : }
680 : ELSE
681 : {
682 15973 : FOR( j = 0; j < bit_band; j++ )
683 : {
684 7268 : IF( y_index[j] < 0 )
685 : {
686 425 : y_index[j] = 0;
687 425 : move16();
688 : }
689 7268 : R_temp_fx[j] = 2097152;
690 7268 : move32(); /*Q21 = 1 */
691 : }
692 :
693 40545 : FOR( j = bit_band; j < band_num; j++ )
694 : {
695 31840 : R_temp_fx[j] = 0;
696 31840 : move32();
697 : }
698 :
699 8705 : norm_sum = 0;
700 15973 : FOR( k = 0; k < bit_band; k++ )
701 : {
702 7268 : norm_sum = add( norm_sum, y_index[k] );
703 : }
704 :
705 8705 : i = bit_band;
706 13172 : FOR( j = 0; j < bit_band; j++ )
707 : {
708 5529 : IF( norm_sum == 0 )
709 : {
710 8 : FOR( k = 0; k < i; k++ )
711 : {
712 4 : R_temp_fx[k] = 0;
713 4 : move32(); /*Q21 */
714 : }
715 : }
716 : ELSE
717 : {
718 5525 : exp = norm_s( norm_sum );
719 5525 : tmp = shl( norm_sum, exp ); /*Q(exp) */
720 5525 : tmp = div_s( 16384, tmp ); /*Q(15+14-exp) */
721 5525 : Bits_avg_fx = L_mult( tmp, Bits ); /*Q(30-exp) */
722 17176 : FOR( k = 0; k < i; k++ )
723 : {
724 11651 : L_tmp = L_shl( L_deposit_l( y_index[k] ), 24 );
725 11651 : Mpy_32_32_ss( Bits_avg_fx, L_tmp, &L_tmp, &lo );
726 11651 : R_temp_fx[k] = L_shl( L_tmp, sub( exp, 2 ) );
727 11651 : move32(); /*Q21 */
728 : }
729 : }
730 5529 : R_sum_fx = 0;
731 5529 : L_tmp = L_shl( L_deposit_l( thr ), 21 ); /*Q21 */
732 14467 : FOR( k = 0; k < i; k++ )
733 : {
734 10361 : IF( L_sub( R_temp_fx[k], L_tmp ) < 0 )
735 : {
736 4140 : FOR( m = k; m < i; m++ )
737 : {
738 2717 : norm_sum = sub( norm_sum, y_index[m] );
739 2717 : R_temp_fx[m] = 0;
740 2717 : move32(); /*Q21 */
741 : }
742 1423 : i = k;
743 1423 : BREAK;
744 : }
745 : ELSE
746 : {
747 8938 : R_sum_fx = L_add( R_sum_fx, R_temp_fx[k] );
748 : }
749 : }
750 5529 : IF( L_sub( R_sum_fx, R_sum_org_fx ) == 0 )
751 : {
752 1062 : BREAK;
753 : }
754 :
755 4467 : R_sum_org_fx = R_sum_fx;
756 : }
757 : }
758 :
759 : /* index comeback */
760 1231717 : FOR( k = 0; k < band_num; k++ )
761 : {
762 1104298 : j = index[k];
763 1104298 : move16();
764 1104298 : Rsubband_fx[add( j, start_band )] = R_temp_fx[k];
765 1104298 : move32();
766 : }
767 :
768 127419 : return;
769 : }
770 :
771 : /*-------------------------------------------------------------------*
772 : * BitAllocWB()
773 : *
774 : * WB bit allocation
775 : *-------------------------------------------------------------------*/
776 :
777 : /*! r: Integer (truncated) number of allocated bits */
778 42473 : int16_t BitAllocWB(
779 : int16_t *y, /* i : norm of sub-vectors */
780 : int16_t B, /* i : number of available bits */
781 : int16_t N, /* i : number of sub-vectors */
782 : int16_t *R, /* o : bit-allocation indicator */
783 : int16_t *Rsubband /* o : sub-band bit-allocation vector (Q3) */
784 : )
785 : {
786 : Word16 t_fx;
787 : Word16 i, j, k, B1, B2, B3, B_saved;
788 : Word16 Rsum_fx, Rsum_sub_fx[3];
789 : Word32 Ravg_sub_32_fx[3], R_diff_32_fx[2];
790 : Word16 factor_fx[2]; /*Q13 */
791 : Word16 BANDS;
792 : Word16 tmp, exp;
793 : Word32 L_tmp, L_tmp1;
794 : Word32 Rsubband_buf[NB_SFM];
795 : UWord16 lo;
796 :
797 42473 : BANDS = N;
798 42473 : move16();
799 42473 : IF( sub( BANDS, SFM_N ) > 0 )
800 : {
801 0 : BANDS = SFM_N;
802 0 : move16();
803 : }
804 : /* Init Rsubband to non-zero values for bands to be allocated bits */
805 1146771 : FOR( k = 0; k < BANDS; k++ )
806 : {
807 1104298 : Rsubband_buf[k] = 2097152;
808 1104298 : move32(); /*Q21 */
809 : }
810 : /* Calculate the norm sum and average of sub-band */
811 42473 : Rsum_sub_fx[0] = 0;
812 722041 : FOR( j = 0; j < SFM_G1; j++ )
813 : {
814 679568 : IF( y[j] > 0 )
815 : {
816 665214 : Rsum_sub_fx[0] = add( Rsum_sub_fx[0], y[j] );
817 665214 : move16(); /*Q0 */
818 : }
819 : }
820 42473 : Ravg_sub_32_fx[0] = L_mult( Rsum_sub_fx[0], 2048 );
821 42473 : move32(); /*Q16 0+15+1, q15 1/16 =2048 */
822 :
823 42473 : Rsum_sub_fx[1] = 0;
824 42473 : move32();
825 382257 : FOR( j = SFM_G1; j < SFM_G1G2; j++ )
826 : {
827 339784 : IF( y[j] > 0 )
828 : {
829 316793 : Rsum_sub_fx[1] = add( Rsum_sub_fx[1], y[j] );
830 316793 : move16(); /*Q0 */
831 : }
832 : }
833 42473 : Ravg_sub_32_fx[1] = L_mult( Rsum_sub_fx[1], 4096 ); /*16 0+15+1, q15 1/8 =4096 */
834 :
835 42473 : Rsum_sub_fx[2] = 0;
836 42473 : move16();
837 127419 : FOR( j = SFM_G1G2; j < BANDS; j++ )
838 : {
839 84946 : IF( y[j] > 0 )
840 : {
841 78494 : Rsum_sub_fx[2] = add( Rsum_sub_fx[2], y[j] );
842 78494 : move16(); /*Q0 */
843 : }
844 : }
845 42473 : tmp = div_s( 1, BANDS - SFM_G1G2 ); /*Q15 */
846 42473 : Ravg_sub_32_fx[2] = L_mult( Rsum_sub_fx[2], tmp );
847 42473 : move32(); /*Q16 */
848 :
849 : /* Bit allocation for every group */
850 42473 : tmp = add( Rsum_sub_fx[0], Rsum_sub_fx[1] );
851 42473 : Rsum_fx = add( tmp, Rsum_sub_fx[2] ); /*Q0 */
852 :
853 42473 : factor_fx[0] = 16384; /*Q13 move16(); */
854 42473 : factor_fx[1] = 24576; /*Q13 move16(); */
855 : {
856 42473 : R_diff_32_fx[0] = L_sub( Ravg_sub_32_fx[0], Ravg_sub_32_fx[1] );
857 42473 : move32(); /*Q16 */
858 42473 : R_diff_32_fx[1] = L_sub( Ravg_sub_32_fx[1], Ravg_sub_32_fx[2] );
859 42473 : move32(); /*Q16 */
860 :
861 42473 : IF( L_sub( R_diff_32_fx[0], 393216 ) < 0 && L_sub( R_diff_32_fx[1], 245760 ) < 0 )
862 : {
863 35085 : IF( Rsum_fx == 0 )
864 : {
865 0 : B1 = 0;
866 0 : move16();
867 0 : B2 = 0;
868 0 : move16();
869 0 : B3 = 0;
870 0 : move16();
871 : }
872 : ELSE
873 : {
874 35085 : exp = norm_s( Rsum_fx );
875 35085 : tmp = shl( Rsum_fx, exp ); /*Q(exp) */
876 35085 : tmp = div_s( 16384, tmp ); /*Q(15+14-exp) */
877 35085 : L_tmp1 = L_mult( B, Rsum_sub_fx[0] ); /*Q1 */
878 35085 : Mpy_32_16_ss( L_tmp1, tmp, &L_tmp, &lo );
879 35085 : B1 = extract_h( L_shl( L_tmp, add( exp, 1 ) ) ); /*Q0 */
880 35085 : IF( L_sub( L_tmp1, L_mult( B1, Rsum_fx ) ) > 0 && L_sub( L_tmp1, L_mult( add( B1, 1 ), Rsum_fx ) ) >= 0 )
881 : {
882 1447 : B1 = add( B1, 1 );
883 : }
884 35085 : L_tmp1 = L_mult( B, Rsum_sub_fx[1] ); /*Q1 */
885 35085 : Mpy_32_16_ss( L_tmp1, tmp, &L_tmp, &lo );
886 35085 : B2 = extract_h( L_shl( L_tmp, add( exp, 1 ) ) ); /*Q0 */
887 35085 : IF( L_sub( L_tmp1, L_mult( B2, Rsum_fx ) ) > 0 && L_sub( L_tmp1, L_mult( add( B2, 1 ), Rsum_fx ) ) >= 0 )
888 : {
889 614 : B2 = add( B2, 1 );
890 : }
891 35085 : L_tmp1 = L_mult( B, Rsum_sub_fx[2] ); /*Q1 */
892 35085 : Mpy_32_16_ss( L_tmp1, tmp, &L_tmp, &lo );
893 35085 : B3 = extract_h( L_shl( L_tmp, add( exp, 1 ) ) ); /*Q0 */
894 35085 : IF( L_sub( L_tmp1, L_mult( B3, Rsum_fx ) ) > 0 && L_sub( L_tmp1, L_mult( add( B3, 1 ), Rsum_fx ) ) >= 0 )
895 : {
896 392 : B3 = add( B3, 1 );
897 : }
898 : }
899 35085 : IF( L_sub( Ravg_sub_32_fx[2], 786432 ) > 0 )
900 : {
901 20293 : B_saved = 0;
902 20293 : move16();
903 20293 : IF( sub( B1, 288 ) > 0 )
904 : {
905 463 : B_saved = sub( B1, 288 );
906 463 : B1 = 288;
907 463 : move16();
908 : }
909 :
910 20293 : IF( sub( B2, 256 ) > 0 )
911 : {
912 0 : tmp = sub( B2, 256 );
913 0 : B_saved = add( B_saved, tmp );
914 0 : B2 = 256;
915 0 : move16();
916 : }
917 :
918 20293 : IF( sub( B3, 96 ) > 0 )
919 : {
920 80 : tmp = sub( B3, 96 );
921 80 : B_saved = add( B_saved, tmp );
922 80 : B3 = 96;
923 80 : move16();
924 : }
925 :
926 20293 : IF( B_saved > 0 )
927 : {
928 543 : IF( sub( B1, 288 ) == 0 )
929 : {
930 463 : tmp = shr( B_saved, 1 );
931 463 : B2 = add( B2, tmp );
932 463 : tmp = sub( B, B1 );
933 463 : B3 = sub( tmp, B2 );
934 : }
935 : ELSE
936 : {
937 80 : tmp = shr( B_saved, 1 );
938 80 : B1 = add( B1, tmp );
939 80 : IF( sub( B2, 256 ) == 0 )
940 : {
941 0 : tmp = sub( B, B1 );
942 0 : B3 = sub( tmp, B2 );
943 : }
944 : ELSE
945 : {
946 80 : tmp = sub( B, B1 );
947 80 : B2 = sub( tmp, B3 );
948 : }
949 : }
950 : }
951 : }
952 :
953 35085 : factor_fx[0] = 16384;
954 35085 : move16(); /*Q13 */
955 35085 : factor_fx[1] = 12288;
956 35085 : move16(); /*Q13 */
957 : }
958 : ELSE
959 : {
960 7388 : IF( Rsum_fx == 0 )
961 : {
962 0 : B1 = 0;
963 0 : move16();
964 0 : B2 = 0;
965 0 : move16();
966 0 : B3 = B;
967 0 : move16();
968 : }
969 : ELSE
970 : {
971 7388 : exp = norm_s( Rsum_fx );
972 7388 : tmp = shl( Rsum_fx, exp ); /*Q(exp) */
973 7388 : tmp = div_s( 16384, tmp ); /*Q(15+14-exp) */
974 7388 : L_tmp1 = L_mult( B, Rsum_sub_fx[0] ); /*Q1 */
975 7388 : Mpy_32_16_ss( L_tmp1, tmp, &L_tmp, &lo );
976 7388 : B1 = extract_h( L_shl( L_tmp, add( exp, 1 ) ) ); /*Q0 */
977 7388 : IF( L_sub( L_tmp1, L_mult( B1, Rsum_fx ) ) > 0 && L_sub( L_tmp1, L_mult( add( B1, 1 ), Rsum_fx ) ) >= 0 )
978 : {
979 660 : B1 = add( B1, 1 );
980 : }
981 7388 : Mpy_32_16_ss( 1975684956, shl( B, 5 ), &L_tmp1, &lo );
982 7388 : Mpy_32_16_ss( L_tmp1, shl( Rsum_sub_fx[1], 7 ), &L_tmp1, &lo );
983 7388 : Mpy_32_16_ss( L_tmp1, tmp, &L_tmp, &lo );
984 7388 : B2 = extract_h( L_shl( L_tmp, sub( exp, 11 ) ) ); /*Q0 */
985 7388 : IF( L_sub( L_tmp1, L_shl( L_mult( B2, Rsum_fx ), 12 ) ) > 0 && L_sub( L_add( L_tmp1, 2 ), L_shl( L_mult( add( B2, 1 ), Rsum_fx ), 12 ) ) >= 0 )
986 : {
987 16 : B2 = add( B2, 1 );
988 : }
989 7388 : tmp = sub( B, B1 );
990 7388 : B3 = sub( tmp, B2 );
991 : }
992 : }
993 : }
994 :
995 42473 : IF( sub( Rsum_sub_fx[2], 3 ) < 0 )
996 : {
997 3697 : B2 = add( B2, B3 );
998 3697 : B3 = 0;
999 3697 : move16();
1000 : }
1001 :
1002 : /* Bit allocation in group */
1003 42473 : Bit_group_fx( y, 0, SFM_G1, B1, 5, Rsubband_buf, factor_fx );
1004 42473 : Bit_group_fx( y, SFM_G1, SFM_G1G2, B2, 6, Rsubband_buf, factor_fx );
1005 42473 : Bit_group_fx( y, SFM_G1G2, BANDS, B3, 7, Rsubband_buf, factor_fx );
1006 1146771 : FOR( i = 0; i < BANDS; i++ )
1007 : {
1008 1104298 : Rsubband[i] = extract_l( L_shr( Rsubband_buf[i], 18 ) );
1009 1104298 : move16();
1010 : }
1011 :
1012 : /* Calcuate total used bits and initialize R to be used for Noise Filling */
1013 42473 : L_tmp = 0;
1014 42473 : move32();
1015 1146771 : FOR( i = 0; i < N; i++ )
1016 : {
1017 1104298 : L_tmp = L_add( L_tmp, Rsubband_buf[i] ); /*Q21 */
1018 1104298 : R[i] = extract_h( L_shr( Rsubband_buf[i], 5 ) ); /*Q0 */
1019 : }
1020 42473 : t_fx = extract_h( L_shr( L_tmp, 5 ) ); /*Q0 */
1021 :
1022 42473 : return (Word16) t_fx;
1023 : }
1024 :
1025 : #undef WMC_TOOL_SKIP
|