Line data Source code
1 : /*====================================================================================
2 : EVS Codec 3GPP TS26.452 Aug 12, 2021. Version 16.3.0
3 : ====================================================================================*/
4 :
5 : #include <stdint.h>
6 : #include "options.h" /* Compilation switches */
7 : #include "rom_com.h" /* Static table prototypes */
8 : #include "prot_fx.h" /* Function prototypes */
9 : #include "ivas_prot_fx.h"
10 :
11 :
12 : /*--------------------------------------------------------------------------
13 : * subband_gain_bits()
14 : *
15 : * HQ core encoder
16 : *--------------------------------------------------------------------------*/
17 784 : static void subband_gain_bits_fx(
18 : const Word16 *Rk, /* i : bit allocation per band Q3 */
19 : const Word16 N, /* i : number of bands */
20 : Word16 *bits, /* o : gain bits per band */
21 : const Word16 *sfmsize /* i : Size of bands */
22 : )
23 : {
24 : Word16 i, b, tot;
25 : Word16 bps;
26 :
27 784 : tot = 0;
28 784 : move16();
29 :
30 31040 : FOR( i = 0; i < N; i++ )
31 : {
32 : /*bps = (short)(Rk[i]*((word16)min(32767, ceil(32767.0f/sfmsize[i]); inexact C-integer division approx. */
33 30256 : bps = extract_l( L_shr( L_mult0( Rk[i], inv_tbl_fx[sfmsize[i]] ), 18 ) ); /* 3+15 */
34 30256 : if ( EQ_32( L_shl( L_mult0( sfmsize[i], add( bps, 1 ) ), 3 ), Rk[i] ) )
35 : {
36 7076 : bps = add( bps, 1 );
37 : }
38 :
39 30256 : bps = s_min( 7, bps );
40 30256 : b = fine_gain_bits[bps];
41 30256 : move16();
42 30256 : bits[i] = b;
43 30256 : move16();
44 30256 : tot = add( tot, b );
45 : }
46 :
47 784 : if ( tot == 0 )
48 : {
49 : /* If no gain bits were assigned, use one bit anyway for potential PVQ overage */
50 18 : bits[0] = 1;
51 18 : move16();
52 : }
53 :
54 784 : return;
55 : }
56 :
57 : /*--------------------------------------------------------------------------*
58 : * assign_gain_bits()
59 : *
60 : * Assign gain adjustment bits and update bit budget
61 : *--------------------------------------------------------------------------*/
62 :
63 21214 : Word16 assign_gain_bits_fx( /* o : Number of assigned gain bits */
64 : const Word16 core, /* i : HQ core */
65 : const Word16 BANDS, /* i : Number of bands */
66 : const Word16 *band_width, /* i : Sub band bandwidth */
67 : Word16 *Rk, /* i/o: Bit allocation/Adjusted bit alloc. Q3 */
68 : Word16 *gain_bits_array, /* o : Assigned gain bits */
69 : Word16 *Rcalc /* o : Bit budget for shape quantizer Q3 */
70 : )
71 : {
72 : Word16 gain_bits_tot;
73 : Word16 i;
74 :
75 : /* Allocate gain bits for every subband used, based on bit rate and bandwidth */
76 21214 : IF( EQ_16( core, HQ_CORE ) )
77 : {
78 784 : subband_gain_bits_fx( Rk, BANDS, gain_bits_array, band_width );
79 : }
80 : ELSE
81 : {
82 20430 : set16_fx( gain_bits_array, 0, BANDS );
83 : }
84 :
85 : /* Re-adjust bit budget for gain quantization */
86 21214 : gain_bits_tot = 0;
87 21214 : move16();
88 21214 : *Rcalc = 0;
89 21214 : move16();
90 197293 : FOR( i = 0; i < BANDS; i++ )
91 : {
92 176079 : IF( Rk[i] > 0 )
93 : {
94 152611 : Rk[i] = sub( Rk[i], shl( gain_bits_array[i], 3 ) );
95 152611 : move16();
96 152611 : gain_bits_tot = add( gain_bits_tot, gain_bits_array[i] );
97 152611 : *Rcalc = add( *Rcalc, Rk[i] );
98 152611 : move16();
99 : }
100 : }
101 :
102 21214 : return gain_bits_tot;
103 : }
104 :
105 12190 : static void ivas_subband_gain_bits_fx(
106 : const Word16 *Rk, /* i : bit allocation per band Q3 */
107 : const Word16 N, /* i : number of bands */
108 : Word16 *bits, /* o : gain bits per band */
109 : const Word16 *sfmsize /* i : Size of bands */
110 : )
111 : {
112 : Word16 i, b, tot;
113 : Word16 bps;
114 :
115 12190 : tot = 0;
116 12190 : move16();
117 :
118 505301 : FOR( i = 0; i < N; i++ )
119 : {
120 : /*bps = (short)(Rk[i]*((word16)min(32767, ceil(32767.0f/sfmsize[i]); inexact C-integer division approx. */
121 493111 : bps = extract_l( L_shr( L_mult0( Rk[i], fg_inv_tbl_fx[sfmsize[i] >> 3] ), 18 ) ); /* 3+15 */
122 493111 : if ( EQ_32( L_shl( L_mult0( sfmsize[i], add( bps, 1 ) ), 3 ), Rk[i] ) )
123 : {
124 0 : bps = add( bps, 1 );
125 : }
126 :
127 493111 : bps = s_min( 7, bps );
128 493111 : b = fine_gain_bits[bps];
129 493111 : move16();
130 493111 : bits[i] = b;
131 493111 : move16();
132 493111 : tot = add( tot, b );
133 : }
134 :
135 12190 : if ( tot == 0 )
136 : {
137 : /* If no gain bits were assigned, use one bit anyway for potential PVQ overage */
138 2462 : bits[0] = 1;
139 2462 : move16();
140 : }
141 :
142 12190 : return;
143 : }
144 :
145 34844 : Word16 ivas_assign_gain_bits_fx( /* o : Number of assigned gain bits */
146 : const Word16 core, /* i : HQ core */
147 : const Word16 BANDS, /* i : Number of bands */
148 : const Word16 *band_width, /* i : Sub band bandwidth */
149 : Word16 *Rk, /* i/o: Bit allocation/Adjusted bit alloc. Q3 */
150 : Word16 *gain_bits_array, /* o : Assigned gain bits */
151 : Word16 *Rcalc /* o : Bit budget for shape quantizer Q3 */
152 : )
153 : {
154 : Word16 gain_bits_tot;
155 : Word16 i;
156 :
157 : /* Allocate gain bits for every subband used, based on bit rate and bandwidth */
158 34844 : IF( EQ_16( core, HQ_CORE ) )
159 : {
160 12190 : ivas_subband_gain_bits_fx( Rk, BANDS, gain_bits_array, band_width );
161 : }
162 : ELSE
163 : {
164 22654 : set16_fx( gain_bits_array, 0, BANDS );
165 : }
166 :
167 : /* Re-adjust bit budget for gain quantization */
168 34844 : gain_bits_tot = 0;
169 34844 : move16();
170 34844 : *Rcalc = 0;
171 34844 : move16();
172 690632 : FOR( i = 0; i < BANDS; i++ )
173 : {
174 655788 : IF( Rk[i] > 0 )
175 : {
176 400363 : Rk[i] = sub( Rk[i], shl( gain_bits_array[i], 3 ) );
177 400363 : move16();
178 400363 : gain_bits_tot = add( gain_bits_tot, gain_bits_array[i] );
179 400363 : *Rcalc = add( *Rcalc, Rk[i] );
180 400363 : move16();
181 : }
182 : }
183 :
184 34844 : return gain_bits_tot;
185 : }
|