Line data Source code
1 : /*====================================================================================
2 : EVS Codec 3GPP TS26.452 Aug 12, 2021. Version 16.3.0
3 : ====================================================================================*/
4 : #include <stdint.h>
5 : #include "options.h" /* Compilation switches */
6 : #include "rom_com.h"
7 :
8 : #include "prot_fx.h" /* Function prototypes */
9 : #include "cnst.h" /* Common constants */
10 :
11 : /*--------------------------------------------------------------------------*/
12 : /* Function hvq_pvq_bitalloc */
13 : /* ~~~~~~~~~~~~~~~~~~~~~~~~ */
14 : /* */
15 : /* Calculate the number of PVQ bands to code and allocate bits based on */
16 : /* the number of available bits. */
17 : /*--------------------------------------------------------------------------*/
18 2830 : Word16 hvq_pvq_bitalloc_fx(
19 : Word16 num_bits, /* i/o: Number of available bits (including gain bits) */
20 : const Word32 core_brate, /* i : core bitrate */
21 : const Word16 bwidth_fx, /* i : Encoded bandwidth */
22 : const Word16 *ynrm, /* i : Envelope coefficients */
23 : const Word32 manE_peak, /* i : Peak energy mantissa */
24 : const Word16 expE_peak, /* i : Peak energy exponent */
25 : Word16 *Rk, /* Q3 o : bit allocation for concatenated vector */
26 : Word16 *R, /* Q0 i/o: Global bit allocation */
27 : Word16 *sel_bands, /* Q0 o : Selected bands for encoding */
28 : Word16 *n_sel_bands /* Q0 o : No. of selected bands for encoding */
29 : )
30 : {
31 : Word16 num_bands, band_max_bits;
32 : Word16 one_over_band_max_bits;
33 : Word16 k;
34 : Word16 reciprocal, envSum, expo, align, m, n, indx;
35 : Word16 k_max;
36 : Word16 k_start;
37 : Word32 E_max, E_max5;
38 : Word32 tmp, acc;
39 : Word32 env_mean;
40 : UWord16 lsb;
41 : Word16 num_sfm;
42 :
43 2830 : IF( EQ_16( bwidth_fx, FB ) )
44 : {
45 2368 : num_sfm = SFM_N_HARM_FB;
46 : }
47 : ELSE
48 : {
49 462 : num_sfm = SFM_N_HARM;
50 : }
51 2830 : move16();
52 :
53 2830 : IF( LT_32( core_brate, HQ_BWE_CROSSOVER_BRATE ) )
54 : {
55 1647 : band_max_bits = HVQ_BAND_MAX_BITS_24k; /*Q0*/
56 1647 : move16();
57 1647 : one_over_band_max_bits = ONE_OVER_HVQ_BAND_MAX_BITS_24k_FX; /*Q15*/
58 1647 : move16();
59 1647 : k_start = HVQ_THRES_SFM_24k; /*Q0*/
60 1647 : move16();
61 1647 : IF( EQ_16( bwidth_fx, FB ) )
62 : {
63 1201 : reciprocal = 2731; /* Q15, 1/(SFM_N_HARM_FB + 1 - k_start) */
64 1201 : move16();
65 : }
66 : ELSE
67 : {
68 446 : reciprocal = 3277; /* Q15, 1/(SFM_N_HARM + 1 - k_start) */
69 446 : move16();
70 : }
71 : }
72 : ELSE
73 : {
74 1183 : band_max_bits = HVQ_BAND_MAX_BITS_32k; /*Q0*/
75 1183 : move16();
76 1183 : one_over_band_max_bits = ONE_OVER_HVQ_BAND_MAX_BITS_32k_FX; /*Q15*/
77 1183 : move16();
78 1183 : k_start = HVQ_THRES_SFM_32k; /*Q0*/
79 1183 : move16();
80 1183 : IF( EQ_16( bwidth_fx, FB ) )
81 : {
82 1167 : reciprocal = 3641; /* Q15, 1/(SFM_N_HARM_FB + 1 - k_start) */
83 1167 : move16();
84 : }
85 : ELSE
86 : {
87 16 : reciprocal = 4681; /* Q15, 1/(SFM_N_HARM + 1 - k_start) */
88 16 : move16();
89 : }
90 : }
91 :
92 2830 : num_bands = mult( num_bits, one_over_band_max_bits ); /* Q0 */
93 2830 : num_bits = sub( num_bits, i_mult( num_bands, band_max_bits ) ); /* Q0 */
94 :
95 2830 : IF( GE_16( num_bits, HVQ_NEW_BAND_BIT_THR ) )
96 : {
97 1538 : num_bands = add( num_bands, 1 );
98 : }
99 : ELSE
100 : {
101 1292 : num_bits = add( num_bits, band_max_bits );
102 : }
103 :
104 : /* safety check in case of bit errors */
105 2830 : IF( LT_16( num_bands, 1 ) )
106 : {
107 14 : return 0;
108 : }
109 :
110 2816 : *n_sel_bands = 0;
111 2816 : move16();
112 2816 : envSum = 0;
113 2816 : move16();
114 2816 : E_max = L_deposit_l( 0 );
115 2816 : k_max = k_start;
116 2816 : move16();
117 29319 : FOR( k = k_start; k < num_sfm; k++ )
118 : {
119 26503 : indx = ynrm[k];
120 26503 : move16();
121 26503 : tmp = dicn_fx[indx]; /* Q14 */
122 26503 : move32();
123 26503 : envSum = add( envSum, indx ); /* Since the size of dicn_fx = 40, ynrm[k] must be less than 41. 16 bits are enough for envSum.*/
124 26503 : IF( GT_32( tmp, E_max ) )
125 : {
126 5361 : E_max = tmp;
127 5361 : move32();
128 5361 : k_max = k;
129 5361 : move16();
130 : }
131 : }
132 2816 : env_mean = L_mult( envSum, reciprocal ); /* env_mean in Q16 */
133 2816 : IF( GT_32( L_sub( env_mean, L_deposit_h( ynrm[k_max] ) ), 0x30000L ) ) /* condition: env_mean - ynrm[k_max] > 3 */
134 : {
135 1705 : expo = norm_l( E_max );
136 1705 : E_max = L_shl( E_max, expo );
137 1705 : Mpy_32_16_ss( E_max, 0x7a12, &E_max5, &lsb ); /* NB: 5.0e5 = 0x7a12(Q15) x 2^19. */
138 : /* True floating point value of E_max*5e5 = E_max5 x 2^(19 - expo - 14).
139 : * In this context, the 32-bit E_max5 is in Q0, and
140 : * -14 is due to Emax in Q14.
141 : * True floating point value of E_peak = manE_peak x 2^(31 - expE_peak - 2*12). See peak_vq_enc_fx().
142 : */
143 :
144 : /* Align the Q-points of the floating point Emax*5e5 and E_peak. */
145 1705 : align = sub( expo, expE_peak );
146 1705 : align = add( align, ( 19 - 14 ) - ( 31 - 2 * 12 ) );
147 1705 : IF( align < 0 )
148 : {
149 1705 : acc = L_sub( E_max5, L_shl( manE_peak, align ) );
150 : }
151 : ELSE
152 : {
153 0 : acc = L_sub( L_shr( E_max5, align ), manE_peak );
154 : }
155 :
156 1705 : IF( acc > 0 ) /* condition: E_max*5.e5 > E_peak */
157 : {
158 1705 : IF( EQ_16( band_len_harm[k_max], 96 ) )
159 : {
160 0 : n = 61;
161 0 : move16();
162 : }
163 : ELSE
164 : {
165 1705 : QuantaPerDsDirac_fx( band_len_harm[k_max], 1, hBitsN, &n );
166 : }
167 1705 : m = shl( sub( num_bits, HVQ_PVQ_GAIN_BITS ), 3 ); /*Q3*/
168 1705 : IF( GE_16( m, n ) )
169 : {
170 1705 : IF( GT_16( num_bands, 1 ) ) /* condition: num_bands > 1 */
171 : {
172 705 : sel_bands[*n_sel_bands] = k_max; /*Q0*/
173 705 : move16();
174 705 : *n_sel_bands = add( *n_sel_bands, 1 ); /*Q0*/
175 705 : move16();
176 705 : R[k_max] = 1; /* Mark that the band has been encoded for fill_spectrum */
177 705 : move16();
178 : }
179 : }
180 : }
181 : }
182 :
183 : /* Allocate bits */
184 2816 : tmp = sub( num_bands, 1 );
185 5526 : FOR( k = 0; k < tmp; k++ )
186 : {
187 2710 : Rk[k] = shl( sub( band_max_bits, HVQ_PVQ_GAIN_BITS ), 3 ); /*Q3*/
188 2710 : move16();
189 : }
190 : /* NB: When it exits the above loop, k = num_bands - 1. */
191 2816 : Rk[k] = shl( sub( num_bits, HVQ_PVQ_GAIN_BITS ), 3 ); /*Q3*/
192 2816 : move16();
193 :
194 2816 : return num_bands;
195 : }
|